Skip to content

fix(harness): guard fresh input lifecycle - #405

Open
openjiuwen-sync-bot[bot] wants to merge 3 commits into
openJiuwen-ai:developfrom
openjiuwenai:sync/pr-2243
Open

fix(harness): guard fresh input lifecycle#405
openjiuwen-sync-bot[bot] wants to merge 3 commits into
openJiuwen-ai:developfrom
openjiuwenai:sync/pr-2243

Conversation

@openjiuwen-sync-bot

@openjiuwen-sync-bot openjiuwen-sync-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Paired: GitHub #405GitCode !2243

/kind bug

修复的问题

本 PR 修复 DeepAgent 交互生命周期中的并发竞态:

  • runtime 已开始或完成终止后,等待异步边界的迟到生产者仍可能重新入队;
  • supervisor 或 round 收尾逻辑可能把 TERMINATED 重新写回 IDLE
  • start/stop 的异步过程可能交叉执行;
  • 宿主在调用 send_input() 前完成的运行状态检查,可能在 DeepAgent 实际判定并入队 fresh turn 前失效。

根因

现有实现分散地直接修改 _interaction_phase,部分 producer 只在进入异步边界前检查一次生命周期。此外,fresh/continuation 的最终判定和 work 入队都在 DeepAgent 的内部锁中完成。宿主只能在调用 send_input() 前检查运行状态,无法保证该检查在实际入队时仍然有效。

这是一个跨异步边界的 TOCTOU 竞态:运行状态在入口处完成检查,但真正入队或修改阶段发生在等待内部锁或异步调用之后;在此期间,并发 stop() 或配置更新可能使先前检查失效。

修复方案

  1. 修复交互生命周期竞态。 统一通过状态转换函数修改交互阶段,并让 InteractionPhase.TERMINATED 成为吸收态;在 send、attach、active round 和 supervisor 的关键异步边界后重新检查 runtime;复用现有生命周期锁串行化完整的 start/stop。该修复对所有 DeepAgent 默认生效,不依赖任何宿主注册。

  2. 提供 FreshInputContextFactory 通过 DeepAgent.set_fresh_input_context_factory() 提供可选的宿主扩展点,仅包围 fresh enqueue,使宿主能够在 DeepAgent 已判定为 fresh turn、但尚未入队时完成需要原子保护的准备工作。上下文进入失败时不入队,退出异常或取消不覆盖主要入队异常;STEER、FOLLOW_UP 和中断恢复等 continuation 路径保持原行为。该 factory 不承载 Agent Core 自身的生命周期修复;只有需要 fresh-turn 宿主上下文的调用方才需显式注册。

验证结果

  • uv run --frozen pytest tests/unit_tests/harness/test_deep_agent_interaction.py:33 项通过;
  • uv run --frozen ruff check --ignore F841 openjiuwen/harness/deep_agent.py tests/unit_tests/harness/test_deep_agent_interaction.py:通过;
  • git diff --check:通过。

自检清单

  • 设计:并发生命周期边界与最小修复方案已经过独立评审;
  • 测试:新增 UT 覆盖 fresh/continuation、异常、取消以及 start/stop 竞争;
  • 验证:上述定向测试与 lint 均已通过;
  • 接口:生命周期修复默认生效;新增接口仅作为可选的宿主扩展点;
  • 文档:不涉及官网文档变更。

Linked Closing Issues:

  • #​1492

Linked Closing Issues:

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@openjiuwen-sync-bot openjiuwen-sync-bot Bot changed the title feat(harness): guard fresh input lifecycle fix(harness): guard fresh input lifecycle Aug 7, 2026
@openjiuwen-collaboration-bot

Copy link
Copy Markdown

head_sha: bd3c2037d85d6ea735b17eaf241ededb6427643b

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 N/A N/A
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DeepAgent 终止后可能重新入队任务或恢复为非终止状态

2 participants