JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(deepresearch): HITL 暂停恢复完成后完成态丢失,模型误判任务未完成
KevinLLLove5d6638fc90ea
diff --git a/jiuwenswarm/agents/harness/common/rails/deepresearch_execution_rail.py b/jiuwenswarm/agents/harness/common/rails/deepresearch_execution_rail.pyindex ef10e6b40a..d1f9c89088 100644--- a/jiuwenswarm/agents/harness/common/rails/deepresearch_execution_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/deepresearch_execution_rail.py@@ -38,6 +38,17 @@ DEEPRESEARCH_EXECUTION_ALIAS_KEY = "deepresearch_execution_aliases"_TOKENS_KEY = "__deepresearch_execution_context_tokens__"_TOOL_NAME = "deepresearch_execute"+# HITL 暂停占位文案(interaction 分支写入 tool_msg)。必须注册进+# StreamEventRail 的占位模板白名单(_is_legacy_tool_interrupt_placeholder_text):+# resume 完成后占位与 completed tool_msg 挂同一原始 tool_call_id,+# 白名单识别使孤儿清理按"占位被真实结果替换"收敛,而非 duplicate+# 规则保留占位删掉完成态(后者会让历史停留在"等待答复")。+DEEPRESEARCH_INTERACTION_PLACEHOLDER = (+ "DeepResearch 已暂停并向用户发起结构化问答,正在等待用户答复。"+ "用户答复后本工具将自动恢复执行并返回后续结果;请勿代替用户"+ "作答,也无需重复调用本工具轮询进度。"+)+logger = logging.getLogger(__name__)@@ -97,6 +108,21 @@ def _workflow_id(session: Any, tool_call_id: str) -> str:return _load_aliases(session).get(tool_call_id, tool_call_id)+def _history_tool_call_id(tool_call_id: str, workflow_id: str) -> str:+ """tool_msg 应挂的历史 tool_call_id:须与 assistant 消息 tool_calls+ 的原始 id 对齐,否则被 StreamEventRail 孤儿清理删除。++ resume 执行时 ctx.inputs.tool_call 是 TIE 克隆件,id 已被改写为+ interaction_id({workflow_id}_interaction_N),须映射回原始 id。+ bridge 场景(模型换新 id 重试被桥接旧 state)例外:本次调用 id+ 本身就是历史 id,原样保留——故按 interaction 后缀模式判断,而非+ "是否在 aliases 中"(bridge 的 alias 不带该后缀)。+ """+ if workflow_id and tool_call_id.startswith(f"{workflow_id}_interaction_"):+ return workflow_id+ return tool_call_id++# 修复点3.2:error 相位入桥候选。取消转译的 stopped 写入(phase=new)# 被同 revision 的 error 覆盖写吞掉时,旧 state 实际存有可恢复停止快照,# error 候选仍可桥回复用。cancelled 是用户显式放弃的死终态,不入候选。@@ -304,6 +330,9 @@ class DeepResearchExecutionRail(DeepAgentRail):returntool_call_id = _tool_call_id(ctx)workflow_id = _workflow_id(ctx.session, tool_call_id)+ # tool_msg 必须挂历史原始 id(resume 时 ctx 携带的是 TIE+ # 克隆件的 interaction_id),否则被孤儿清理删除+ history_id = _history_tool_call_id(tool_call_id, workflow_id)state = payload.get("state")states = _load_states(ctx.session)if isinstance(state, Mapping):@@ -340,12 +369,8 @@ class DeepResearchExecutionRail(DeepAgentRail):# 保持不变,打断/恢复流程不受影响。tool_call_id 必须用原始# 调用 id,与 assistant 消息中的 tool_calls 对齐。ctx.inputs.tool_msg = ToolMessage(- content=(- "DeepResearch 已暂停并向用户发起结构化问答,正在等待"- "用户答复。用户答复后本工具将自动恢复执行并返回后续"- "结果;请勿代替用户作答,也无需重复调用本工具轮询进度。"- ),- tool_call_id=tool_call_id,+ content=DEEPRESEARCH_INTERACTION_PLACEHOLDER,+ tool_call_id=history_id,)return@@ -375,7 +400,7 @@ class DeepResearchExecutionRail(DeepAgentRail):"主题,同样会自动认领)。恢复将从停止处继续,已完成""阶段不会重跑。"),- tool_call_id=tool_call_id,+ tool_call_id=history_id,)return@@ -388,6 +413,30 @@ class DeepResearchExecutionRail(DeepAgentRail):}_save_aliases(ctx.session, aliases)content = payload.get("content")++ if payload.get("kind") == "completed":+ # 完成态回写:completed 结果不再以原始 JSON payload(含+ # state/revision/timing/token usage 等内部字段,由框架默认+ # 序列化成 tool_msg)裸入 LLM 历史,改为与 interaction/+ # stopped 同口径的短文案。旁路自恢复(用户答复交互卡后的+ # interrupt continuation)不重入 LLM 主循环,本次执行的+ # tool_msg 是完成态进入历史的唯一通道——若不显式改写,+ # 历史中该 tool_call_id 下最后一条 tool 消息会停留在暂停+ # 占位文案,后续轮模型误判任务未完成(实测事故:任务已+ # 完成,模型答"还在第一阶段暂停等待用户答复")。payload+ # content 即完成摘要(报告名/章节结构/交付说明),短载荷+ # 同时避开部署版按体积拒收 llm_context 的守卫;前端交付+ # 不受影响——chat.tool_result 帧使用 tool_result 原始+ # payload(StreamEventRail),与 tool_msg 解耦。+ ctx.inputs.tool_msg = ToolMessage(+ content=(+ content.strip()+ if isinstance(content, str) and content.strip()+ else "DeepResearch 任务已完成,报告已交付。"+ ),+ tool_call_id=history_id,+ )+if isinstance(content, str) and content.strip():ctx.request_force_finish({"output": content.strip(), "result_type": "answer"}@@ -404,5 +453,6 @@ class DeepResearchExecutionRail(DeepAgentRail):__all__ = ["DEEPRESEARCH_EXECUTION_ALIAS_KEY","DEEPRESEARCH_EXECUTION_STATE_KEY",+ "DEEPRESEARCH_INTERACTION_PLACEHOLDER","DeepResearchExecutionRail",]diff --git a/jiuwenswarm/agents/harness/common/rails/stream_event_rail.py b/jiuwenswarm/agents/harness/common/rails/stream_event_rail.pyindex 6839e33ec6..79b479b609 100644--- a/jiuwenswarm/agents/harness/common/rails/stream_event_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/stream_event_rail.py@@ -719,6 +719,9 @@ class JiuSwarmStreamEventRail(DeepAgentRail):_SKILL_TURBO_HITL_PLACEHOLDER_LEGACY,_SKILL_TURBO_HITL_PLACEHOLDER_LEGACY_V2,)+ from jiuwenswarm.agents.harness.common.rails.deepresearch_execution_rail import (+ DEEPRESEARCH_INTERACTION_PLACEHOLDER,+ )legacy_templates = [f"[Tool execution interrupted] Tool {tool_name} was interrupted by user during execution, "@@ -735,6 +738,12 @@ class JiuSwarmStreamEventRail(DeepAgentRail):_SKILL_TURBO_HITL_PLACEHOLDER_LEGACY_V2if tool_name == "skill_acceleration_exec"else "",+ # HITL 暂停 tool_msg(deepresearch_execute 专用文案)。注册后+ # 孤儿清理把"占位 + resume 完成态同 id 两条消息"收敛为"占位被+ # 真实结果替换",而非 duplicate 规则保留占位删掉完成态。+ DEEPRESEARCH_INTERACTION_PLACEHOLDER+ if tool_name == "deepresearch_execute"+ else "",]normalized_content = self._normalize_tool_interrupt_text(content)return any(diff --git a/tests/unit_tests/agentserver/test_deepresearch_execute_tool.py b/tests/unit_tests/agentserver/test_deepresearch_execute_tool.pyindex 6ccc826048..22dc09ccbe 100644--- a/tests/unit_tests/agentserver/test_deepresearch_execute_tool.py+++ b/tests/unit_tests/agentserver/test_deepresearch_execute_tool.py@@ -13,7 +13,7 @@ from types import SimpleNamespacefrom unittest.mock import AsyncMock, Mock, patchimport pytest-from openjiuwen.core.foundation.llm import ToolMessage+from openjiuwen.core.foundation.llm import AssistantMessage, ToolMessage, UserMessagefrom openjiuwen.core.single_agent.interrupt.exception import ToolInterruptExceptionfrom openjiuwen.core.single_agent.rail.base import ToolCallInputs@@ -23,6 +23,7 @@ from jiuwenswarm.agents.harness.common.rails import (from jiuwenswarm.agents.harness.common.rails.deepresearch_execution_rail import (DEEPRESEARCH_EXECUTION_ALIAS_KEY,DEEPRESEARCH_EXECUTION_STATE_KEY,+ DEEPRESEARCH_INTERACTION_PLACEHOLDER,DeepResearchExecutionRail,)from jiuwenswarm.agents.harness.common.rails.stream_event_rail import (@@ -2183,6 +2184,106 @@ async def test_execution_rail_force_finishes_terminal_result_without_next_llm():{"output": "研究报告已生成并交付。", "result_type": "answer"}]assert session.state[DEEPRESEARCH_EXECUTION_STATE_KEY] == {}+ # 完成态回写:tool_msg 为完成摘要短文案(非原始 payload JSON)+ assert isinstance(ctx.inputs.tool_msg, ToolMessage)+ assert ctx.inputs.tool_msg.tool_call_id == "call-1"+ assert ctx.inputs.tool_msg.content == "研究报告已生成并交付。"+++async def test_execution_rail_writes_back_completion_summary_to_llm_history():+ """完成态回写:completed 的 tool_msg 改写为完成摘要短文案。++ 旁路自恢复(答复交互卡后的 interrupt continuation)不重入 LLM 主循环,+ 本次执行的 tool_msg 是完成态进入 LLM 历史并随快照落盘的唯一通道;+ 不改写时历史中该 tool_call_id 下最后一条 tool 消息停留在暂停占位+ 文案,后续轮模型误判任务未完成(实测事故:任务已完成,模型答+ "还在第一阶段暂停等待用户答复")。tool_call_id 必须用原始调用 id+ (与 assistant 消息中的 tool_calls 对齐);内部 state/token usage+ 不泄露;tool_result 保持原 dict 供 StreamEventRail 推送前端+ chat.tool_result 帧,与 tool_msg 解耦。+ """+ state = {+ "schema_version": 1,+ "phase": "completed",+ "query": "深度研究AI芯片行业",+ "conversation_id": "conversation-1",+ "revision": 6,+ }+ result = {+ "schema_version": de.EXECUTION_SCHEMA,+ "kind": "completed",+ "content": "✅ **深度研究已完成!**\n\nai_chip_report已成功生成并交付。",+ "state": state,+ "workflow_llm_token_usage": {+ "input_tokens": 795008,+ "output_tokens": 38569,+ },+ }+ session = _Session()+ rail = DeepResearchExecutionRail(model_provider=lambda: None)+ ctx = _rail_ctx(result=result, session=session)++ await rail.before_tool_call(ctx)+ await rail.after_tool_call(ctx)++ assert isinstance(ctx.inputs.tool_msg, ToolMessage)+ assert ctx.inputs.tool_msg.tool_call_id == "call-1"+ assert ctx.inputs.tool_msg.content == result["content"]+ # 内部 state / token usage 不泄露进 LLM 历史+ assert "conversation-1" not in ctx.inputs.tool_msg.content+ assert "schema_version" not in ctx.inputs.tool_msg.content+ assert "revision" not in ctx.inputs.tool_msg.content+ assert "795008" not in ctx.inputs.tool_msg.content+ # tool_result 保持原始 dict(前端交付通道,与 tool_msg 解耦)+ assert ctx.inputs.tool_result is result+ # 完成态照常清理 states/aliases 并 force finish+ assert session.state[DEEPRESEARCH_EXECUTION_STATE_KEY] == {}+ assert ctx.force_finish_requests == [+ {"output": result["content"], "result_type": "answer"}+ ]+++async def test_execution_rail_completed_without_content_uses_fallback_notice():+ """content 缺失时回写兜底文案,完成态仍对后续轮模型可见。"""+ result = {+ "schema_version": de.EXECUTION_SCHEMA,+ "kind": "completed",+ "state": {"schema_version": 1, "phase": "completed", "revision": 5},+ }+ rail = DeepResearchExecutionRail(model_provider=lambda: None)+ ctx = _rail_ctx(result=result)++ await rail.before_tool_call(ctx)+ await rail.after_tool_call(ctx)++ assert isinstance(ctx.inputs.tool_msg, ToolMessage)+ assert ctx.inputs.tool_msg.content == "DeepResearch 任务已完成,报告已交付。"+ # content 为空时维持现状:不 force finish+ assert ctx.force_finish_requests == []+++async def test_execution_rail_does_not_write_back_completion_for_cancelled():+ """取消路径不回写完成态(kind=cancelled 不触发 completed 改写),+ 避免把取消/停止的续跑终态伪装成完成。"""+ result = {+ "schema_version": de.EXECUTION_SCHEMA,+ "kind": "cancelled",+ "content": "DeepResearch 任务已取消。",+ "state": {"schema_version": 1, "phase": "cancelled", "revision": 3},+ }+ rail = DeepResearchExecutionRail(model_provider=lambda: None)+ ctx = _rail_ctx(result=result)++ await rail.before_tool_call(ctx)+ await rail.after_tool_call(ctx)++ assert ctx.inputs.tool_msg is None+ assert ctx.force_finish_requests == [+ {"output": "DeepResearch 任务已取消。", "result_type": "answer"}+ ]@pytest.mark.asyncio@@ -2236,6 +2337,229 @@ async def test_execution_rail_rewrites_stopped_result_to_block_auto_restart():assert session.state[DEEPRESEARCH_EXECUTION_STATE_KEY]["call-1"] == state+def _resumed_session() -> _Session:+ """暂停后恢复现场:aliases 已含 interaction 克隆件 id → 原始 id 映射。"""+ session = _Session()+ session.update_state({+ DEEPRESEARCH_EXECUTION_ALIAS_KEY: {"call-1_interaction_1": "call-1"}+ })+ return session+++async def test_execution_rail_completed_after_resume_uses_original_tool_call_id():+ """resume 完成态回写的 tool_msg 必须挂原始 tool_call_id(事故回归)。++ handle_resume 深拷贝 TIE 克隆件恢复执行,ctx.inputs.tool_call.id 是+ interaction 克隆件 id({workflow_id}_interaction_N);若 tool_msg 沿用该+ id,与 assistant 消息 tool_calls 的原始 id 不匹配,追问轮被+ StreamEventRail 孤儿清理删除(orphan_removed),历史停留在暂停占位+ ——实测事故:任务已完成,模型答"还在第一阶段暂停等待用户答复"。+ """+ session = _resumed_session()+ result = {+ "schema_version": de.EXECUTION_SCHEMA,+ "kind": "completed",+ "content": "✅ **深度研究已完成!**\n\nai_chip_report已成功生成并交付。",+ "state": {+ "schema_version": 1,+ "phase": "completed",+ "query": "深度研究AI芯片行业",+ "conversation_id": "conversation-1",+ "revision": 6,+ },+ }+ rail = DeepResearchExecutionRail(model_provider=lambda: None)+ ctx = _rail_ctx(+ result=result,+ session=session,+ tool_call_id="call-1_interaction_1",+ )++ await rail.before_tool_call(ctx)+ await rail.after_tool_call(ctx)++ assert isinstance(ctx.inputs.tool_msg, ToolMessage)+ assert ctx.inputs.tool_msg.tool_call_id == "call-1"+ assert ctx.inputs.tool_msg.content == result["content"]+ # 完成态照常清理 states/aliases(按 workflow_id 收敛)+ assert session.state[DEEPRESEARCH_EXECUTION_STATE_KEY] == {}+ assert session.state[DEEPRESEARCH_EXECUTION_ALIAS_KEY] == {}+++async def test_execution_rail_stopped_after_resume_uses_original_tool_call_id():+ """resume 后停止的 tool_msg 同样挂原始 id(与 completed 同构修复)。"""+ session = _resumed_session()+ result = {+ "schema_version": de.EXECUTION_SCHEMA,+ "kind": "stopped",+ "state": {+ "schema_version": 1,+ "phase": "new",+ "query": "深度研究AI芯片行业",+ "conversation_id": "conversation-1",+ "revision": 2,+ },+ }+ rail = DeepResearchExecutionRail(model_provider=lambda: None)+ ctx = _rail_ctx(+ result=result,+ session=session,+ tool_call_id="call-1_interaction_1",+ )++ await rail.before_tool_call(ctx)+ await rail.after_tool_call(ctx)++ assert isinstance(ctx.inputs.tool_msg, ToolMessage)+ assert ctx.inputs.tool_msg.tool_call_id == "call-1"+ assert "已被用户停止" in ctx.inputs.tool_msg.content+++async def test_execution_rail_bridge_call_keeps_own_tool_call_id():+ """bridge 场景例外:本次调用 id 本身就是历史 id,不得换成旧 workflow id。++ 模型换新 tool_call_id 重试被桥接旧 state 时,aliases 里有新 id → 旧+ workflow_id 的映射,但历史 assistant tool_calls 挂的是本次新 id——+ id 修正须按 interaction 后缀模式判断,而非"是否在 aliases 中"。+ """+ session = _Session()+ session.update_state({+ DEEPRESEARCH_EXECUTION_ALIAS_KEY: {"call-new": "call-old"},+ DEEPRESEARCH_EXECUTION_STATE_KEY: {+ "call-old": {+ "schema_version": 1,+ "phase": "starting",+ "query": "q",+ "file_name": "r",+ "conversation_id": "conversation-1",+ "revision": 0,+ }+ },+ })+ result = {+ "schema_version": de.EXECUTION_SCHEMA,+ "kind": "completed",+ "content": "研究报告已生成并交付。",+ "state": {"schema_version": 1, "phase": "completed", "revision": 1},+ }+ rail = DeepResearchExecutionRail(model_provider=lambda: None)+ ctx = _rail_ctx(result=result, session=session, tool_call_id="call-new")++ await rail.before_tool_call(ctx)+ await rail.after_tool_call(ctx)++ assert isinstance(ctx.inputs.tool_msg, ToolMessage)+ assert ctx.inputs.tool_msg.tool_call_id == "call-new"+ # states/aliases 仍按 workflow_id(call-old)清理+ assert session.state[DEEPRESEARCH_EXECUTION_STATE_KEY] == {}+ assert session.state[DEEPRESEARCH_EXECUTION_ALIAS_KEY] == {}+++class _ModelContext:+ def __init__(self, messages):+ self.messages = list(messages)++ def get_messages(self):+ return list(self.messages)++ def pop_messages(self, size):+ popped = self.messages[:size]+ self.messages = self.messages[size:]+ return popped++ async def add_messages(self, message):+ self.messages.append(message)+++def _deepresearch_history_ctx(tool_messages, tool_call_id="call_00c9"):+ rail_ctx = SimpleNamespace(+ context=_ModelContext([+ UserMessage(content="深度研究AI芯片行业"),+ AssistantMessage(+ content="",+ tool_calls=[{+ "type": "function",+ "id": tool_call_id,+ "function": {+ "name": "deepresearch_execute",+ "arguments": "{\"query\":\"深度研究AI芯片行业\"}",+ },+ }],+ ),+ *tool_messages,+ ]),+ inputs=SimpleNamespace(tools=[]),+ session=None,+ extra={},+ )+ return rail_ctx+++async def test_fix_incomplete_tool_context_replaces_deepresearch_placeholder_with_completion():+ """占位注册白名单后,"占位 + completed 同 id"收敛为占位被真实结果替换。++ 事故链(修复前):deepresearch 占位文案不在白名单 → 占位被当普通+ 消息保留,completed 同 id 第二条被 duplicate 规则删除 → 历史停留在+ "等待答复" → 模型误判任务未完成。注册后:占位被识别为 placeholder,+ real_tool_messages_by_id 命中 completed → 原位替换、后续重复删除。+ """+ tool_call_id = "call_00c9"+ rail = JiuSwarmStreamEventRail()+ ctx = _deepresearch_history_ctx([+ ToolMessage(+ content=DEEPRESEARCH_INTERACTION_PLACEHOLDER,+ tool_call_id=tool_call_id,+ ),+ ToolMessage(+ content="✅ **深度研究已完成!**\n\nai_chip_report已成功生成并交付。",+ tool_call_id=tool_call_id,+ ),+ ])++ await rail._fix_incomplete_tool_context(ctx)++ tool_msgs = [m for m in ctx.context.get_messages() if isinstance(m, ToolMessage)]+ assert len(tool_msgs) == 1+ assert tool_msgs[0].tool_call_id == tool_call_id+ assert "已完成" in tool_msgs[0].content+ assert "等待用户答复" not in tool_msgs[0].content+++async def test_fix_incomplete_tool_context_keeps_deepresearch_placeholder_while_pending():+ """暂停期间(无真实结果):占位原样保留,模型能看到"等待用户答复"。"""+ tool_call_id = "call_00c9"+ rail = JiuSwarmStreamEventRail()+ ctx = _deepresearch_history_ctx([+ ToolMessage(+ content=DEEPRESEARCH_INTERACTION_PLACEHOLDER,+ tool_call_id=tool_call_id,+ ),+ ])++ await rail._fix_incomplete_tool_context(ctx)++ tool_msgs = [m for m in ctx.context.get_messages() if isinstance(m, ToolMessage)]+ assert len(tool_msgs) == 1+ assert tool_msgs[0].content == DEEPRESEARCH_INTERACTION_PLACEHOLDER+++def test_deepresearch_placeholder_registered_in_interrupt_templates():+ """占位文案白名单识别:仅 deepresearch_execute 名下命中。"""+ rail = JiuSwarmStreamEventRail()+ assert rail._is_legacy_tool_interrupt_placeholder_text(+ DEEPRESEARCH_INTERACTION_PLACEHOLDER, "deepresearch_execute"+ )+ # 其他工具名下不识别(防止文案碰撞扩散到无关工具)+ assert not rail._is_legacy_tool_interrupt_placeholder_text(+ DEEPRESEARCH_INTERACTION_PLACEHOLDER, "other_tool"+ )++@pytest.mark.asyncioasync def test_execution_rail_binds_request_id_for_nested_options_llm():result = {@@ -2335,6 +2659,12 @@ async def test_same_outer_tool_call_survives_multiple_native_interrupts():assert second.force_finish_requests[0]["result_type"] == "answer"assert "✅ **深度研究已完成!**" in second.force_finish_requests[0]["output"]+ # 事故回归:resume 完成态 tool_msg 必须挂原始调用 id(本次 ctx 携带+ # 的是 TIE 克隆件 interaction id),否则追问轮被孤儿清理删除,历史+ # 停留在暂停占位,模型误判任务未完成+ assert isinstance(second.inputs.tool_msg, ToolMessage)+ assert second.inputs.tool_msg.tool_call_id == "call-1"+ assert "已完成" in second.inputs.tool_msg.contentassert session.state[DEEPRESEARCH_EXECUTION_STATE_KEY] == {}assert session.state[DEEPRESEARCH_EXECUTION_ALIAS_KEY] == {}diff --git a/tests/unit_tests/gateway/test_runtime_routed_client.py b/tests/unit_tests/gateway/test_runtime_routed_client.pyindex 45fab5a01d..c26410aa37 100644--- a/tests/unit_tests/gateway/test_runtime_routed_client.py+++ b/tests/unit_tests/gateway/test_runtime_routed_client.py@@ -143,6 +143,13 @@ async def test_reverse_rpc_over_real_http_sse(monkeypatch, stream):received.append(params["jsonrpc_id"])assert output.complete_jsonrpc_response(params["jsonrpc_id"], params["response"])return {"ok": True, "data": {}}, 200+ # reverse_rpc_ready 在 push 流断开-重连的空窗内瞬态为 False+ # (SSE EOF → 注销 → 重连 → 注册)。瞬时断言会把该罕见窗口放大成+ # CI 并行负载下的 flaky;改为有界等待,仅当持续未注册才判定违反契约。+ for _ in range(100):+ if registry.reverse_rpc_ready():+ break+ await asyncio.sleep(0.01)assert registry.reverse_rpc_ready(), "chat must not race subscriber registration"result = await GatewayA2AOutboundToolBackend().call(A2A_TOOL_FIND_AGENTS,