Skip to content

feat: enable to inject diversity cross async multi-trace#1173

Merged
xuangu-fang merged 7 commits intomainfrom
fsk/async_inject_diverse
Aug 18, 2025
Merged

feat: enable to inject diversity cross async multi-trace#1173
xuangu-fang merged 7 commits intomainfrom
fsk/async_inject_diverse

Conversation

@xuangu-fang
Copy link
Copy Markdown
Collaborator

@xuangu-fang xuangu-fang commented Aug 11, 2025

1. 目标

trace_scheduler 决定开启一个新的探索分支(trace)时,系统应能参考其他已存在分支(包括正在进行但未提交的分支)的探索方向(即Hypothesis),并基于这些信息生成一个具有多样性、避免重复的新探索方向。此功能应可通过配置开启或关闭。

2. 核心设计思想的演进

核心思想如下:

  • 避免修改接口签名: 我们不再通过 **kwargs 或修改 gen() 方法的签名来传递上下文。
  • Trace 即上下文: DSTrace 对象将作为信息传递的核心载体。在生成新假说之前,相关的多样性上下文信息会被临时注入到 DSTrace 实例中。DSProposalV2ExpGen 在执行时直接从 DSTrace 对象中读取这些上下文,执行完毕后,上下文将被清理。
  • 明确区分与解耦: 新功能将与现有的 inject_diverse 逻辑明确区分开,使用新的配置项和变量名,避免混淆。

3. 模块化设计与修改计划

3.1. 配置 (rdagent/app/data_science/conf.py)

  • 新增配置项: 在 DataScienceBasePropSetting 类中增加一个新的布尔型标志位,用于控制此功能的开关。
    • enable_cross_trace_diversity: bool = True
    • 注释: 需要添加清晰的注释,说明此标志位用于在创建新探索分支时,从其他并行分支收集信息以注入多样性,并区别于 enable_inject_diverse

3.2. TraceScheduler 模块 (rdagent/scenarios/data_science/proposal/exp_gen/trace_scheduler.py)

TraceScheduler 的核心职责不变,但其与 router 的交互方式将简化。

  • BaseScheduler:
    • 临时存储 (维持不变): 继续使用 self.uncommitted_experiments: dict[int, DSExperiment] = {} 来追踪那些由 async_gen 创建但尚未被 record 方法提交的实验。
    • 公共方法 (维持不变): 保留 register_uncommitted_expderegister_uncommitted_exp 方法。新增 get_uncommitted_hypotheses() 方法,用于返回所有未提交实验的 Hypothesis 列表。
    • next() 方法 (签名简化): 返回签名恢复为 tuple[int, ...]。它不再需要返回 context 字典,只负责选择下一个节点。

3.3. DSTrace 模块 (rdagent/scenarios/data_science/proposal/exp_gen/base.py)

DSTrace 将变得更加智能,能够提供多样性分析所需的上下文。

  • 新增属性:
    • contextual_hypotheses: list[DSHypothesis] | None = None: 一个临时属性,用于存储需要用于多样性生成的其他分支的假说。
  • 新增方法:
    • set_contextual_hypotheses(self, hypotheses: list[DSHypothesis] | None) -> None: 用于设置和清理 contextual_hypotheses
    • get_committed_root_hypotheses(self) -> list[DSHypothesis]: (实现细节澄清) 此方法将遍历 self.dag_parent,找到所有父节点为 self.NEW_ROOT (()) 的条目,这些条目对应的索引就是每个分支的根节点实验。然后从 self.hist 中获取这些实验并提取其 Hypothesis。这个方法提供了已提交分支的探索起点。

3.4. Router 模块 (rdagent/scenarios/data_science/proposal/exp_gen/router/__init__.py)

ParallelMultiTraceExpGen 将成为协调者,负责组装上下文并注入 DSTrace

  • ParallelMultiTraceExpGen.async_gen():
    • 检查配置: 首先检查 DS_RD_SETTING.enable_cross_trace_diversity 是否为 True。如果不是,则跳过所有多样性注入逻辑。
    • 决策与上下文准备: 当 self.trace_scheduler.next() 返回一个新分支选择(trace.NEW_ROOT)时:
      1. 调用 self.trace_scheduler.get_uncommitted_hypotheses() 获取所有正在进行的实验假说。
      2. 调用 trace.get_committed_root_hypotheses() 获取所有已提交分支的根假说。
      3. 将上述两种假说合并成一个列表 all_hypotheses
      4. 调用 trace.set_contextual_hypotheses(all_hypotheses),将上下文注入 trace 对象。
    • 调用 gen 与清理:
      • 使用 try...finally 结构来确保上下文在之后被清理。
      try:
          # ... 准备上下文并调用 trace.set_contextual_hypotheses()
          exp = self.exp_gen.gen(trace, plan=ds_plan) # 签名不变
          # ...
      finally:
          trace.set_contextual_hypotheses(None) # 确保清理
    • 注册未提交实验: 在 exp 对象成功创建后,立即调用 self.trace_scheduler.register_uncommitted_exp(exp, loop.loop_idx)

3.5. Proposal 模块 (rdagent/scenarios/data_science/proposal/exp_gen/proposal.py)

DSProposalV2ExpGen 现在可以从 trace 中直接获取所需信息。

  • DSProposalV2ExpGen.gen():
    • 方法签名保持不变
    • 在方法内部,通过 trace.contextual_hypotheses 获取用于多样性注入的假说列表。
    • 变量名区分: 将这些假说命名为 cross_trace_hypotheses 以区别于现有的 inject_diverse 逻辑。
    • 如果 cross_trace_hypotheses 存在,则将其格式化并整合到传递给 identify_problem 方法的上下文中。
  • identify_problem() 及相关 prompt:
    • identify_problem 中,将 cross_trace_hypotheses 的信息格式化为一个字符串。
    • Prompt 修改: 修改 .prompts_v2:scenario_problem.system (位于 prompts_v2.yaml),增加一个新的输入字段,用于接收其他分支的假说信息,并明确指示 LLM 基于这些信息提出不同于已有方向的新挑战。
    • 注释: 在代码和 prompt 模板中添加注释,清晰地解释这一新机制的目的和作用。

📚 Documentation preview 📚: https://RDAgent--1173.org.readthedocs.build/en/1173/

@xuangu-fang xuangu-fang merged commit bcdd957 into main Aug 18, 2025
9 checks passed
@xuangu-fang xuangu-fang deleted the fsk/async_inject_diverse branch August 18, 2025 04:43
licong01-cloud pushed a commit to licong01-cloud/RD-Agent that referenced this pull request Dec 13, 2025
)

* init multi_trace_async_ diversity inject

* lint

* add task in context for diversity injection, add abstract class for diversity injection

* lint

* feat: update diversity injection strategy and enhance sibling context handling in experiment generation

* add always inject

---------

Co-authored-by: Xu Yang <peteryang@vip.qq.com>
yongbin4 pushed a commit to yongbin4/RD-Agent that referenced this pull request Mar 8, 2026
)

* init multi_trace_async_ diversity inject

* lint

* add task in context for diversity injection, add abstract class for diversity injection

* lint

* feat: update diversity injection strategy and enhance sibling context handling in experiment generation

* add always inject

---------

Co-authored-by: Xu Yang <peteryang@vip.qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants