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(harness): 修复长任务因子代理失败与流式超时被中断且静默结束的问题
zhangfuzhie2e1ba82ef97
diff --git a/jiuwenswarm/agents/harness/common/rails/last_iteration_finalize_rail.py b/jiuwenswarm/agents/harness/common/rails/last_iteration_finalize_rail.pyindex 75f2d31641..8ccc76a2ca 100644--- a/jiuwenswarm/agents/harness/common/rails/last_iteration_finalize_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/last_iteration_finalize_rail.py@@ -82,7 +82,7 @@ class LastIterationFinalizeRail(DeepAgentRail):iteration,max_iterations,)- ctx.request_force_finish({"output": output, "result_type": "error"})+ ctx.request_force_finish({"output": output, "result_type": "error", "finish_reason": "max_iterations"})@staticmethoddef _read_max_iterations(ctx: Any) -> int | None:diff --git a/jiuwenswarm/agents/harness/common/rails/llm_retry_notify_rail.py b/jiuwenswarm/agents/harness/common/rails/llm_retry_notify_rail.pyindex 69883f1eef..46bc5e6f59 100644--- a/jiuwenswarm/agents/harness/common/rails/llm_retry_notify_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/llm_retry_notify_rail.py@@ -116,6 +116,12 @@ class NotifyingLLMRetryRail(LLMRetryRail):}await super().before_invoke(ctx)+ async def after_model_call(self, ctx: AgentCallbackContext) -> None:+ """A completed model call ends the failure streak for this ctx."""+ await super().after_model_call(ctx)+ if ctx.exception is None:+ ctx.extra[_RETRY_COUNTS_KEY] = dict.fromkeys(_COUNT_ATTRS, 0)+def _retry_counts(self, ctx: AgentCallbackContext) -> dict[str, int]:counts = ctx.extra.get(_RETRY_COUNTS_KEY)if not isinstance(counts, dict):diff --git a/jiuwenswarm/agents/harness/common/tools/subagent_compat.py b/jiuwenswarm/agents/harness/common/tools/subagent_compat.pyindex 0f0ae4d229..2b60ac2f47 100644--- a/jiuwenswarm/agents/harness/common/tools/subagent_compat.py+++ b/jiuwenswarm/agents/harness/common/tools/subagent_compat.py@@ -19,6 +19,7 @@ from openjiuwen.harness.subagent_runtime.models import ( # type: ignore[import-ResumeResult,SubagentStatus,SubagentStatusKind,+ WaitResult,)from openjiuwen.harness.subagent_runtime.status_events import ( # type: ignore[import-untyped]is_turn_finished,@@ -44,6 +45,8 @@ _MIRROR_STRIP_KEYS = ("task_id", "parent_request_id")_REASONING_FLUSH_CHARS = 200_REASONING_FLUSH_INTERVAL_S = 0.3_REASONING_FRAME_CAP = 2000+# An errored turn's message may be the child's whole streamed text; keep the reason readable.+_ERROR_REASON_CHARS = 2000def _child_reasoning_text(payload: object) -> str:@@ -90,6 +93,18 @@ class CompatibleSubagentControl(SubagentControl):await instance.status.set(idle)return replace(result, status=idle)+ async def wait(self, subagent_ids: list[str], *args: Any, **kwargs: Any) -> WaitResult:+ """Surface errored reasons; subagent_wait renders only ``results``, so the parent saw none."""+ result = await super().wait(subagent_ids, *args, **kwargs)+ errors = {+ sid: f"error: {status.message[:_ERROR_REASON_CHARS]}"+ for sid, status in result.statuses.items()+ if status.kind is SubagentStatusKind.ERRORED and status.message+ }+ if not errors:+ return result+ return replace(result, results={**result.results, **errors})+# Toggled by ``install_subagent_control_compat_patch`` from# ``react.subagent_runtime.mirror_child_stream``. Class-level because the# control registry constructs instances itself.diff --git a/jiuwenswarm/agents/harness/common/tools/subagent_executor/execution_timeout.py b/jiuwenswarm/agents/harness/common/tools/subagent_executor/execution_timeout.pyindex 5477fec699..32d9fa9a2f 100644--- a/jiuwenswarm/agents/harness/common/tools/subagent_executor/execution_timeout.py+++ b/jiuwenswarm/agents/harness/common/tools/subagent_executor/execution_timeout.py@@ -29,6 +29,11 @@ _CANCEL_GRACE_SECONDS = 1.0# Admission threshold, not an eviction limit: in-flight failures may add more._MAX_PENDING_CLEANUP = 64_PENDING_CLEANUP: set[asyncio.Task[Any]] = set()+# Fixed text, so it stays safe to surface; without it the parent has to guess and redoes the work.+_MAX_ITERATIONS_REASON = (+ "max_iterations_reached: the subagent ran out of iterations before finishing. "+ "Files it already wrote remain in the workspace; inspect them before retrying or redoing the work."+)@dataclass@@ -52,8 +57,10 @@ def raise_for_subagent_error(result: Any) -> None:"""if isinstance(result, dict) and result.get("result_type") != "interrupt":if result.get("error") or result.get("result_type") == "error":- reason = result.get("error") or result.get("message") or "subagent_error"- raise RuntimeError(f"subagent_failed: {reason}")+ reason = result.get("error") or result.get("message")+ if not reason and result.get("finish_reason") == "max_iterations":+ reason = _MAX_ITERATIONS_REASON+ raise RuntimeError(f"subagent_failed: {reason or 'subagent_error'}")def ensure_subagent_available(child: Any) -> None:diff --git a/jiuwenswarm/agents/harness/docs/specs/S_07_llm-call-failure-recovery.md b/jiuwenswarm/agents/harness/docs/specs/S_07_llm-call-failure-recovery.mdindex 65279c104e..953595fdee 100644--- a/jiuwenswarm/agents/harness/docs/specs/S_07_llm-call-failure-recovery.md+++ b/jiuwenswarm/agents/harness/docs/specs/S_07_llm-call-failure-recovery.md@@ -8,7 +8,7 @@| 编号 / slug | S_07 / llm-call-failure-recovery || 关联模块(jiuwenswarm 仓) | `jiuwenswarm/agents/harness/common/rails/llm_retry_notify_rail.py`、`context_overflow_recovery_rail.py`;装配点 `jiuwenswarm/server/runtime/agent_adapter/interface_deep.py`(`_build_llm_retry_rail` / `_build_context_overflow_recovery_rail`)、`interface_code.py`(`_FIXED_RAIL_NAMES`)、`jiuwenswarm/server/runtime/skill_turbo/executor.py`(`_build_llm_retry_rail`)、`jiuwenswarm/agents/swarm/config_specs.py` + `providers/builtin_rails.py`(成员 rail 投影) || 关联模块(agent-core 仓,跨仓契约) | `openjiuwen/harness/rails/llm_retry_rail.py`(`LLMRetryRail` 父类:repeat 检测参数、`backoff_delay`、`_is_repeat_exception` / `_is_stream_timeout_exception` / `_is_transient_invoke_exception`)、`openjiuwen.harness.rails.base.DeepAgentRail`、`openjiuwen/core/session/stream.OutputSchema` |-| 最近一次修订日期 | 2026-10-28 |+| 最近一次修订日期 | 2026-11-02 || 关联 feature | 暂无 |## 范围 / 边界@@ -36,7 +36,7 @@## 不变量-- **N1(per-call 预算隔离)**:重试计数唯一权威存储是 `ctx.extra["_llm_retry_counts"]`(三键 `repeat` / `stream_timeout` / `transient_invoke` 各自独立计数)。`before_invoke` 只重置**本 ctx** 的计数——并发共享同一 rail 实例的 `call_llm` / `stream_llm`(SkillTurbo PPT 页收集)互不偷、互不清对方的计数。实例属性(`repeat_retry_count` 等,`_COUNT_ATTRS` 映射)只是观测镜像,供日志 / 外部读数,不参与判定。+- **N1(per-call 预算隔离)**:重试计数唯一权威存储是 `ctx.extra["_llm_retry_counts"]`(三键 `repeat` / `stream_timeout` / `transient_invoke` 各自独立计数)。`before_invoke` 只重置**本 ctx** 的计数——并发共享同一 rail 实例的 `call_llm` / `stream_llm`(SkillTurbo PPT 页收集)互不偷、互不清对方的计数。预算语义是**连续失败**:`after_model_call` 且 `ctx.exception is None` 时清零本 ctx 三键(重试中的失败尝试也会触发该 hook,但带异常,不清零)。长 invoke 里互不连续的失败不共用一份预算。实例属性(`repeat_retry_count` 等,`_COUNT_ATTRS` 映射)只是观测镜像,供日志 / 外部读数,不参与判定。- **N2(原因三分流有序互斥)**:`on_model_exception` 依次判 `_is_repeat_exception` → `_is_stream_timeout_exception` → transient_invoke,**先命中先归类**,一次异常只消耗一个原因的预算。transient_invoke 受 `retry_transient_invoke_errors` 开关;前两类不受。- **N3(peek / consume 分离)**:`_peek_retry` 纯预演(供通知文案取 attempt 编号),**不消耗**计数;`_request_retry_or_reset` 才 `counts[reason] += 1` 并 `ctx.request_retry(delay_seconds=delay)`。两处对同一 delay 的 jitter 独立采样——通知展示的 delay 与实际生效 delay 允许不同(均为合法 jitter 值)。- **N4(耗尽重置不吞异常)**:某原因计数达 `max_retries` 后,`_request_retry_or_reset` 把该原因计数与镜像属性归零、**不再** `request_retry`,异常继续向上传播(由上层 / 父类框架决定终态)。rail 永不吞掉模型异常。@@ -101,7 +101,7 @@| 键 | yaml 值 | 装配代码 fallback | 说明 ||---|---|---|---|| `enabled` | true | False(缺省关) | 非 `is True` 即不注册 |-| `max_retries` | 2 | 2 | 单 invoke 内同类异常重试上限 |+| `max_retries` | 2 | 2 | 同类异常连续失败的重试上限(N1) || `retry_transient_invoke_errors` | true | True | N2 第三类开关 || `notify_user_on_retry` / `notify_user_on_exhausted` | true / true | True / True | 通知开关 || `repeat_min_pattern_chars` / `repeat_max_pattern_chars` | 2 / 64 | 2 / 64 | 透传父类 |@@ -116,6 +116,7 @@| 场景 | 位置 | 形态 ||---|---|---|| deep 主链 retry | `interface_deep.py` `_build_llm_retry_rail`(:10113) | enabled 非 True → None;失败 → warning + None(不炸装配) |+| flash 主链 retry | `interface_flash.py` `_FLASH_RAIL_KEEP` 保留 `_llm_retry_rail` | 复用 deep 构建;移出白名单会让工厂回退 SDK 原版 rail,重试耗尽无用户可见错误 || deep 溢出 | `interface_deep.py` `_build_context_overflow_recovery_rail`(:9648) | `max_recovery_attempts=3` || code 侧溢出 | `interface_code.py` `_FIXED_RAIL_NAMES`(:459)+ build callable | 固定名单去重挂载;断言传 3 || skill_turbo | `executor.py` `_build_llm_retry_rail`(:946) | 同一配置节、同一参数集(与 DeepAgent 一致) |@@ -136,7 +137,7 @@- **P3 通知双形态**:will_retry → `retry_notification`(P2 内);耗尽 → `error` + `llm_retry_exhausted`。- **P4 溢出检测与恢复**:`on_model_exception`(priority 100 先跑,N10)→ N9 检测 → 计数 +1 → 未超限 → N11 恢复(force_compact + override)→ `request_retry(0)`。- **P5 溢出熔断**:计数超 `max_recovery_attempts` → `_circuit_break`:error 事件 + 计数归零(N12)。-- **P6 成功归零**:`after_model_call` 且无异常 → `_consecutive_overflow_count = 0`。+- **P6 成功归零**:`after_model_call` 且无异常 → `_consecutive_overflow_count = 0`;重试 rail 同一时机清零本 ctx 预算(N1)。## 异常分支与退出路径@@ -158,6 +159,7 @@| 场景 | 现行为 | 风险 | 测试覆盖 ||---|---|---|---|| 并发 call_llm / stream_llm 共享 rail 实例 | per-ctx 预算独立(N1) | 计数互偷 → 提前耗尽或超限重试 | `test_concurrent_ctx_keep_independent_retry_budgets`、`test_before_invoke_does_not_reset_sibling_ctx_budget` |+| 长 invoke 内互不连续的失败 | 成功调用后预算清零(N1) | 分散在几十分钟里的失败共用预算,任务被提前中断 | `test_completed_model_call_restores_budget_for_its_own_ctx_only`、`test_failed_attempt_does_not_restore_budget` || 未传 / 显式 None `backoff_seconds` | 生成序列 5/10/…/60 封顶 | 误把 None 当显式空表 | `test_generated_backoff_sequence_when_not_provided`、`test_explicit_none_backoff_still_uses_generated_sequence`、`test_generated_backoff_sequence_caps_at_max` || 显式传 `backoff_seconds` | 精确执行、零 jitter | 调用方精确调度被抖动 | `test_explicit_backoff_sequence_is_respected`、`test_explicit_backoff_has_no_jitter` || 生成序列逐请求 jitter | 每次采样(N7),两次请求 delay 不同 | 构造期采样导致并发同步 | `test_generated_backoff_applies_per_request_jitter` |diff --git a/jiuwenswarm/agents/harness/docs/specs/S_12_subagent-runtime-and-lifecycle.md b/jiuwenswarm/agents/harness/docs/specs/S_12_subagent-runtime-and-lifecycle.mdindex 00d95893e5..bb46872751 100644--- a/jiuwenswarm/agents/harness/docs/specs/S_12_subagent-runtime-and-lifecycle.md+++ b/jiuwenswarm/agents/harness/docs/specs/S_12_subagent-runtime-and-lifecycle.md@@ -8,7 +8,7 @@| 编号 / slug | S_12 / subagent-runtime-and-lifecycle || 关联模块(agent-core 仓,机制主体) | `openjiuwen/harness/subagent_runtime/`(`control.py` 1227 行 `SubagentControl`、`instance.py` `SubagentInstance`、`models.py` 状态机、`config.py` 常量、`session_manager.py`、`__init__.py` 事件常量 re-export)、`openjiuwen/harness/tools/subagent/_control_registry.py`(属性挂载协议)、`openjiuwen/harness/tools/subagent/subagent_tools.py`(runtime 六工具)、`openjiuwen/harness/deep_agent.py`(释放三入口 @3027 / @3097) || 关联模块(jiuwenswarm 仓,compat 与投影) | `jiuwenswarm/agents/harness/common/tools/subagent_compat.py`(`CompatibleSubagentControl` + `install_subagent_control_compat_patch`)、`jiuwenswarm/server/runtime/agent_adapter/subagent_stream.py`(3A 事件投影 / 批槽 / 子行持久化)、`interface_deep.py`(`release_subagent_runtime_for_session` @3468、`_session_has_live_subagent_runtime` @3437、compat patch 装配 @9860) |-| 最近一次修订日期 | 2026-10-28 |+| 最近一次修订日期 | 2026-11-03 || 关联 feature | 暂无 |## 范围 / 边界@@ -21,7 +21,7 @@- 超时三级体系(turn 2400s / completion 1800s / wait clamp 1h)- 事件三常量(`subagent_updated` / `subagent_message` / `subagent_activity`)的唯一 re-export 入口- `_control_registry` 属性挂载协议与三释放入口-- jiuwenswarm compat 层:`CompatibleSubagentControl`(resume idle 修复 + 子流镜像 + reasoning 合并)与 `install_subagent_control_compat_patch`+- jiuwenswarm compat 层:`CompatibleSubagentControl`(resume idle 修复 + 子流镜像 + reasoning 合并 + errored 原因回传)与 `install_subagent_control_compat_patch`- jiuwenswarm 投影层:3A 事件 → Web `chat.subtask_update` / legacy 批槽 / 子 transcript 落父 session不管:@@ -48,7 +48,7 @@- **N12(compat 类替换协议)**:`install_subagent_control_compat_patch`(jiuwenswarm subagent_compat.py @220)通过**类属性替换**生效——`_control_registry.SubagentControl = CompatibleSubagentControl`(registry 自行构造实例,无法逐实例注入)。`mirror_child_stream` 是**类级**开关(同理由)。该 patch 在 `_build_subagent_rail`(interface_deep @9860)装配;patch 失败 → `enable_runtime` 降级 False(runtime 整体关闭而非裸奔 stock 类)。- **N13(wait 卡片超时豁免)**:runtime 六工具的 wait 卡片 `_attach_call_timeout(wait_card, None)`(subagent_tools.py @469)——合法等待可跨多个父轮 turn,不适用通用工具超时。- **N14(镜像流白名单与合并)**:子 chunk 镜像到父 session 流仅三类 `_MIRROR_CHUNK_TYPES = {llm_reasoning, tool_call, tool_result}`;`answer`(→ chat.final)/ `content_chunk`(→ chat.delta)/ `error` / 子 `task.*` 留在子会话。镜像帧必剥 `_MIRROR_STRIP_KEYS = (task_id, parent_request_id)`(防父流幽灵任务段)并盖 `stream_source_id = subagent_id`。llm_reasoning 按 200 字符 / 300ms 合并(`_REASONING_FLUSH_CHARS` / `_REASONING_FLUSH_INTERVAL_S`)、单子代理帧上限 `_REASONING_FRAME_CAP = 2000`。镜像写失败只 warning——**镜像永不打断子代理 turn**。-- **N15(镜像尾部与状态清理)**:turn 结束(`is_turn_finished`)必 `_release_mirror_tail`(合并中的短尾不滞留到下一 turn);`_evict_from_memory` finally 必 `_drop_mirror_state`。`CompatibleSubagentControl.resume`:restored 且仍 PENDING_INIT → 置 idle(`SubagentStatus.completed()` 公开态)——OpenJiuWen 用终态 turn status 表示"活跃可输入",COMPLETED 映射公开 idle。+- **N15(镜像尾部与状态清理)**:turn 结束(`is_turn_finished`)必 `_release_mirror_tail`(合并中的短尾不滞留到下一 turn);`_evict_from_memory` finally 必 `_drop_mirror_state`。`CompatibleSubagentControl.resume`:restored 且仍 PENDING_INIT → 置 idle(`SubagentStatus.completed()` 公开态)——OpenJiuWen 用终态 turn status 表示"活跃可输入",COMPLETED 映射公开 idle。`CompatibleSubagentControl.wait`:ERRORED 且有 message 的子代理,把 `error: {message}`(截断至 `_ERROR_REASON_CHARS = 2000`)放进 `WaitResult.results`——stock `SubagentWaitTool` 只渲染 `results`,否则父代理只看到 `status: errored` + `(no result)`,不知道失败原因(如轮数耗尽)而盲目重做。- **N16(投影不覆写权威状态)**:`project_subagent_updated_for_web` 投影 `legacy_status`(closed→completed/error、idle→completed/error、其余 starting)仅供 Web SubtaskProgress 兼容,**不回写** runtime 权威 status。并行批槽(`resolve_subagent_parallel_fields`)按 parent_session_id 记 0-based index/total,完成即从槽中移除、槽空即清 dict——父会话批槽泄漏即清。- **N17(子行落父 session)**:`persist_subagent_transcript_message` / `persist_subagent_activity` / `persist_subagent_roster_history` 全部把子事件写为**父 session** 下的行(`session_id=parent_session_id` + `subagent_id` + `channel_id="subagent"` + `mode="subagent"`),request_id 以 `{subagent_id}:{seq}` / `{subagent_id}:activity:{task_id}:{seq}` / `subagent-roster-{subagent_id}:{revision}` 区分。三写全走 `_safe_persist`(吞异常只 warning)——持久化失败不得打断流转发。- **N18(释放三入口全吞异常)**:DeepAgent `_release_interaction_subagents`(@3027,parent_ended)、`aclose`(@3097,agent_closed,调 `release_all_subagent_controls`)、`SubagentRail.uninit`(rail_uninit)三处释放路径均吞异常(noqa BLE001)——释放失败不得掩盖主流程。`interface_deep.release_subagent_runtime_for_session`(@3316)额外先 `clear_subagent_progress_batch(sid)` 再释放,逐级向 child adapter 转发。@@ -61,7 +61,7 @@| 方法 | 契约 ||---|---|| `spawn(...)` | N2 二段式;任务描述截断至 `_TASK_DESCRIPTION_MAX_LEN = 2000` |-| `wait(...)` | N10 clamp + FIRST_COMPLETED |+| `wait(...)` | N10 clamp + FIRST_COMPLETED;compat 层回传 ERRORED 原因(N15) || `resume(subagent_id)` | N9 校验 → compat 层 idle 修复(N15) || `close(...)` | N8 禁 RUNNING || `cancel_all()` | 全量取消(释放路径内部调用) |diff --git a/jiuwenswarm/agents/harness/docs/specs/S_14_subagent-dispatch-channels-and-host-integration.md b/jiuwenswarm/agents/harness/docs/specs/S_14_subagent-dispatch-channels-and-host-integration.mdindex eef7fcb4bf..cd472a9a61 100644--- a/jiuwenswarm/agents/harness/docs/specs/S_14_subagent-dispatch-channels-and-host-integration.md+++ b/jiuwenswarm/agents/harness/docs/specs/S_14_subagent-dispatch-channels-and-host-integration.md@@ -8,7 +8,7 @@| 编号 / slug | S_14 / subagent-dispatch-channels-and-host-integration || 关联模块(agent-core 仓,机制主体) | `openjiuwen/harness/rails/subagent/subagent_rail.py`(`SubagentRail` @31、priority=95 @42、`_REFRESHABLE_TOOL_NAMES` @44、`_partition_runtime_subagents` @344、`uninit` @199)、`openjiuwen/harness/tools/subagent/session_tools.py`(`SessionsSpawnTool` @235、`SessionsCancelTool` @124、`SessionToolkit` @40)、`openjiuwen/harness/task_loop/session_spawn_executor.py`(`SessionSpawnExecutor` @33)、`openjiuwen/harness/tools/subagent/thinking_hook.py`(进程级 hook 注册表)、`openjiuwen/harness/tools/subagent/task_tool.py`(`_invoke_subagent` @362——被 patch 的窄缝)、`openjiuwen/harness/tools/subagent/subagent_tools.py`(runtime 三工具,语义归 [[S_12]]) || 关联模块(jiuwenswarm 仓,装配与变体) | `jiuwenswarm/agents/harness/common/rails/browser_task_prompt_rail.py`(`BrowserTaskPromptRail` @39)、`jiuwenswarm/agents/harness/flash/rails/flash_subagent_rail.py`(`FlashSubagentRail` @179)、`jiuwenswarm/server/runtime/agent_adapter/interface_deep.py`(`_build_subagent_rail` @9860、`_resolve_enable_subagent_runtime` @10848、`_session_has_live_subagent_runtime` @3437、`release_subagent_runtime_for_session` @3468、statusline 注入 @4696-4716)、`jiuwenswarm/server/runtime/agent_adapter/interface_code.py`(`_RAIL_BUILD_NAMES` @287)、`jiuwenswarm/server/runtime/agent_adapter/code_agent_rail.py`(`AgentTool` @174、`CodeAgentRail` @369)、`jiuwenswarm/server/runtime/agent_adapter/statusline_setup_agent.py`、`jiuwenswarm/agents/harness/common/tools/subagent_executor/execution_timeout.py`(`run_subagent_with_timeout` @123)、`jiuwenswarm/server/runtime/debug_trace/task_tool_patch.py`、`jiuwenswarm/server/runtime/debug_trace/subagent_capture.py`、`jiuwenswarm/perf/subagent_hooks.py` |-| 最近一次修订日期 | 2026-10-28 |+| 最近一次修订日期 | 2026-11-03 || 关联 feature | 暂无 |## 范围 / 边界@@ -49,7 +49,7 @@- **N12(宿主生命周期两钩,保守取向)**:`_session_has_live_subagent_runtime`(@3437)——`_subagent_controls` 缺失视为空闲(dest-stable SDK / 未 spawn 过);`capacity().used > 0` 占用;capacity 探测**抛异常时视为占用**(@3459-3466,TTL 不得驱逐可能持有子代理的 Adapter)。`release_subagent_runtime_for_session`(@3468)——`clear_subagent_progress_batch` → 非 session-scoped 时**递归**委托给子 adapter(@3479-3483)→ `release_subagent_control(deep_agent, sid)`(异常吞掉仅告警 @3491-3496)。- **N13(委托边界五道闸)**:`run_subagent_with_timeout`(execution_timeout.py @123)入口依序检查:跨会话复用禁止(`state.session_id != session_id` raise @138-139)/ `unavailable` 拒绝(@140-141)/ 重入拒绝(`state.active` @142-143)/ pending cleanup 背压(未完成任务 ≥ `_MAX_PENDING_CLEANUP=64` 时拒绝新调用 @144-145,准入阈值非驱逐上限)/ `completion_timeout` 有限正值校验(读 `child.deep_config.completion_timeout` 默认 1800.0,@147-150)。超时值**取自子代理配置**([[S_11]] N4),不引入第二套常量。- **N14(结果三态与永久退役)**:成功与 `interrupt` 结果原样返回(可 resume,同会话新时间预算);error / 超时 / 取消路径 → `state.unavailable = True`(@165)**实例级永久退役**——重试需新实例(factory 路径每次新 delegation 建新实例;预建共享实例的集成方自行替换)。`_cancel_and_observe`(@95)三路并发(`task.cancel` + `child.abort` + `controller.stop`),`_CANCEL_GRACE_SECONDS=1.0` 宽限后未完成的进 `_PENDING_CLEANUP` 观察(done_callback 移除,异常仅告警)。`_CompletionDeadline`(@41)与子代理内部 TimeoutError 类型区分(@167-171)。-- **N15(失败原因不吞不溢)**:`raise_for_subagent_error`(@45)只取 `error` / `message` 字段构造异常(`subagent_failed: {reason}`),**不拷贝完整输出**进异常;interrupt 结果豁免。TaskTool 调用时故意保持 `succeeded=False`——KV affinity 下 SDK 清理据此 evict 失败 sticky 子代理的缓存而非 offload 复用([[S_12]] 呼应)。+- **N15(失败原因不吞不溢)**:`raise_for_subagent_error`(@45)只取 `error` / `message` 字段构造异常(`subagent_failed: {reason}`),**不拷贝完整输出**进异常;`finish_reason == "max_iterations"` 时用固定文案 `_MAX_ITERATIONS_REASON`(告知轮数耗尽且已写文件仍在工作区,防止父代理盲目重做);interrupt 结果豁免。该条只覆盖 sync task_tool 通道;runtime 通道由 compat 层在 `wait` 回传 ERRORED 原因([[S_12]] N15);async 通道 jiuwenswarm 不启用(`enable_async_subagent` 未传,SDK 默认 False),`SessionSpawnExecutor` 对 `result_type=error` 仍产出 TASK_COMPLETION(原因文本在 output 里但状态显示完成)——现状记录,启用 async 前须先修。TaskTool 调用时故意保持 `succeeded=False`——KV affinity 下 SDK 清理据此 evict 失败 sticky 子代理的缓存而非 offload 复用([[S_12]] 呼应)。- **N16(task_tool 通道挂载在窄缝 patch)**:`apply_task_tool_debug_patch`(task_tool_patch.py @75,app_agentserver.py @240 启动应用)patch `TaskTool._invoke_subagent`(类级 setattr @98,幂等标记 `debug_trace_patch_applied` + `debug_trace_patch_mode="dispatch_only"`)。包装后:无 debug run → `run_subagent_with_timeout` 包 original_dispatch(@56-63);有 debug run 且捕获开启 → `invoke_subagent_with_trace`(@64-70)。SDK 不暴露 `_invoke_subagent` 时标记 `unsupported_sdk` 放弃 patch——**TaskTool 行为不变但委托边界丢失**(此时仅 AgentTool 通道有边界)。所有父 agent 模式(含 native team members)共用此 patch。- **N17(debug 二路分发与隔离排水)**:`invoke_subagent_with_trace`(subagent_capture.py @36)外层 `run_subagent_with_timeout`;内层(@69)无 debug logger 或未开启捕获 → 原生 `subagent.invoke`(@95-97);开启捕获 → stream 驱动:`stream(inputs, session=None)` **隔离 session**(@111,传父 session 会让父 run 与本 helper 双排水者轮询分走 token),逐 chunk `ensure_subagent_available`(防取消后晚到 chunk 落入 debug 输出)+ `dbg.feed_subagent`,reduction 镜像 SDK `_result_from_stream_chunk`(@133,内联避免 protected 访问);iterator 在**同任务**内 `aclose`(@120-124,保 rail ContextVar token 在原上下文 reset)。模块只读 ContextVar 不写(绑定生命周期归适配器)。- **N18(perf patch 汇聚点与幂等)**:`apply_create_subagent_perf_patch`(subagent_hooks.py @66)包装 `DeepAgent.create_subagent`——与观测 / 授权 wiring patch 同一汇聚点([[S_11]] N1)。幂等双标记(模块级 `_PATCH_APPLIED` + 类属性 `perf_summary_subagent_patch_applied`);attach 失败仅告警不破坏 spawn(@91-92);`perf.summary.enabled` 关闭时 attach 为 no-op。diff --git a/jiuwenswarm/resources/config.yaml b/jiuwenswarm/resources/config.yamlindex 0b013dccd1..04b02a895c 100644--- a/jiuwenswarm/resources/config.yaml+++ b/jiuwenswarm/resources/config.yaml@@ -317,8 +317,8 @@ models:timeout: 360# 流式:等待首个分片的最大时间(秒)。放宽以兼容 reasoning/思考模型思考期的静默stream_first_chunk_timeout: 300- # 流式:分片之间的最大空闲等待时间(秒)- stream_idle_timeout: 60+ # 流式:分片之间的最大空闲等待时间(秒)。部分模型的 tool 参数不增量下发,生成长参数时会静默较久+ stream_idle_timeout: 180verify_ssl: truecustom_headers: {}model_config_obj:@@ -1003,7 +1003,7 @@ execution_guard:llm_retry_rail:# 总开关。关闭后不注册 LLMRetryRail,也不会额外拦截重复输出/流式帧超时enabled: true- # 单次 invoke 内同一类异常最多重试次数;超过后把异常继续抛给上层+ # 同一类异常连续失败的最多重试次数,模型调用成功后清零;超过后把异常继续抛给上层max_retries: 2# 非流式 invoke 遇到 Connection error / Request timed out 等瞬时错误时是否自动重试retry_transient_invoke_errors: truediff --git a/jiuwenswarm/server/runtime/agent_adapter/deep/model_factory.py b/jiuwenswarm/server/runtime/agent_adapter/deep/model_factory.pyindex a65dcb3966..3748bb84ac 100644--- a/jiuwenswarm/server/runtime/agent_adapter/deep/model_factory.py+++ b/jiuwenswarm/server/runtime/agent_adapter/deep/model_factory.py@@ -310,6 +310,13 @@ def _system_prompt_reload_fingerprint(deep_cfg: Any | None) -> str | None:)+# 对所有模型条目生效(relay 下发、自定义网关、环境变量回退),条目显式配置的值优先;+# config.yaml 模板里的 180 与此保持一致。relay 下发的条目不带该字段,不补默认值会落回+# SDK 的 60s。部分模型(如 glm-5.3)的 tool 参数不增量下发,生成长参数时静默超过 60s,+# 这种超时重试也过不去。+_DEFAULT_STREAM_IDLE_TIMEOUT_SECONDS = 180.0++def _build_model_from_entry(mcc: dict, mco: dict) -> Model:"""根据单个模型条目的 model_client_config / model_config_obj 构建 Model 实例。"""name = mcc.get("model_name", "")@@ -349,6 +356,7 @@ def _build_model_from_entry(mcc: dict, mco: dict) -> Model:mcc_fields = {k: v for k, v in mcc.items() if k != "model_name"}if not mcc_fields.get("client_provider"):mcc_fields["client_provider"] = "OpenAI"+ mcc_fields.setdefault("stream_idle_timeout", _DEFAULT_STREAM_IDLE_TIMEOUT_SECONDS)# 真实鉴权在 tip ``default_headers`` 里同步过来# (Authorization: Basic ...). api_key is only the placeholder# ``huawei-maas-session``. Merge into custom_headers; llm_sse_patch keepsdiff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_flash.py b/jiuwenswarm/server/runtime/agent_adapter/interface_flash.pyindex 57b9c88930..654ba898b7 100644--- a/jiuwenswarm/server/runtime/agent_adapter/interface_flash.py+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_flash.py@@ -17,9 +17,9 @@ config.yaml。父类只提供通用工具工厂与生命周期扩展点,不感不再由 ``flash.enabled`` 将普通 Agent 请求自动转换为 Flash;其它渠道和后台来源沿用各自的既有路由策略。具体准入与会话续接由 ``_shared`` 的模式解析负责。flash 白名单包含 ``_context_overflow_recovery_rail``(仅上下文溢出兜底,-正常路径零开销),但不含 ``_llm_retry_rail`` 等重试类健壮性 rail,-长文档 / 弱网任务的容错仍弱于 normal,部署时需评估所选 Flash profile-的能力边界。+正常路径零开销)与 ``_llm_retry_rail``(不保留时工厂回退 SDK 原版 rail,+重试耗尽没有用户可见错误),其余健壮性 rail 不保留,长文档 / 弱网任务的容错+仍弱于 normal,部署时需评估所选 Flash profile 的能力边界。flash 行为由类常量定义:- :data:`_FLASH_REACT_OVERRIDE` — react/evolution 覆盖(``enable_task_loop=false``@@ -269,6 +269,8 @@ class JiuwenSwarmFlashAdapter(JiuWenSwarmDeepAdapter):# 上下文溢出兜底:413/ModelArts.81001 input-token-limit 时# 由该 rail 设置 force_compact 并在重试前压缩上下文。正常请求不触发。"_context_overflow_recovery_rail",+ # 重试耗尽要发 chat.error:SDK 原版 rail 不通知,长任务失败时用户只看到“完成”。+ "_llm_retry_rail",# _filesystem_rail: SlimSysOperationRail(flash override),init() 注册# Read/Write/Edit/Glob/Grep/Bash,不注册 powershell/list_files;# 丢掉=无文件/shell 能力。diff --git a/tests/unit_tests/agents/harness/common/rails/test_llm_retry_notify_rail_ctx.py b/tests/unit_tests/agents/harness/common/rails/test_llm_retry_notify_rail_ctx.pyindex 19e752e2bd..902d6c3fdd 100644--- a/tests/unit_tests/agents/harness/common/rails/test_llm_retry_notify_rail_ctx.py+++ b/tests/unit_tests/agents/harness/common/rails/test_llm_retry_notify_rail_ctx.py@@ -96,6 +96,54 @@ async def test_before_invoke_does_not_reset_sibling_ctx_budget():assert ctx_a.consume_retry_request() is None+async def test_completed_model_call_restores_budget_for_its_own_ctx_only():+ """Non-consecutive failures in a long invoke must not share one budget."""+ rail = NotifyingLLMRetryRail(+ max_retries=1,+ backoff_seconds=[0.0],+ notify_user_on_retry=False,+ notify_user_on_exhausted=False,+ )+ ctx_a = _ctx()+ ctx_b = _ctx()+ await rail.before_invoke(ctx_a)+ await rail.before_invoke(ctx_b)+ ctx_b.exception = _repeat_exc()+ await rail.on_model_exception(ctx_b)+ assert ctx_b.consume_retry_request() is not None++ for _ in range(3):+ ctx_a.exception = _repeat_exc()+ await rail.on_model_exception(ctx_a)+ assert ctx_a.consume_retry_request() is not None+ ctx_a.exception = None+ await rail.after_model_call(ctx_a)++ await rail.on_model_exception(ctx_b)+ assert ctx_b.consume_retry_request() is None+++async def test_failed_attempt_does_not_restore_budget():+ rail = NotifyingLLMRetryRail(+ max_retries=1,+ backoff_seconds=[0.0],+ notify_user_on_retry=False,+ notify_user_on_exhausted=False,+ )+ ctx = _ctx()+ await rail.before_invoke(ctx)+ ctx.exception = _repeat_exc()+ await rail.on_model_exception(ctx)+ assert ctx.consume_retry_request() is not None++ await rail.after_model_call(ctx)++ await rail.on_model_exception(ctx)+ assert ctx.consume_retry_request() is None++def test_generated_backoff_sequence_when_not_provided():rail = NotifyingLLMRetryRail(max_retries=2,diff --git a/tests/unit_tests/agents/harness/test_subagent_compat.py b/tests/unit_tests/agents/harness/test_subagent_compat.pyindex d766e1dfd6..d30127aca7 100644--- a/tests/unit_tests/agents/harness/test_subagent_compat.py+++ b/tests/unit_tests/agents/harness/test_subagent_compat.py@@ -10,16 +10,20 @@ import pytestpytest.importorskip("openjiuwen.harness.subagent_runtime")+from openjiuwen.core.foundation.tool import ToolCardfrom openjiuwen.harness.subagent_runtime.control import SubagentControlfrom openjiuwen.harness.subagent_runtime.models import (ResumeResult,SubagentStatus,SubagentStatusKind,+ WaitResult,)from openjiuwen.harness.subagent_runtime.status import StatusChannelfrom openjiuwen.harness.subagent_runtime.status_events import (build_subagent_updated_payload,)+from openjiuwen.harness.tools.base_tool import ToolOutput+from openjiuwen.harness.tools.subagent.subagent_tools import SubagentWaitToolfrom jiuwenswarm.agents.harness.common.tools.subagent_compat import (CompatibleSubagentControl,@@ -316,3 +320,30 @@ async def test_evict_drops_mirror_state_when_parent_evict_fails(assert "sub-1" not in control._mirror_reasoning_text # pylint: disable=protected-accessassert "sub-1" not in control._mirror_reasoning_flushed_at # pylint: disable=protected-accessassert "sub-1" not in control._mirror_reasoning_frames # pylint: disable=protected-access+++async def test_wait_hands_errored_reason_to_parent(monkeypatch: pytest.MonkeyPatch) -> None:+ reason = "已达到最大思考轮数(100/100),任务未完成。"++ async def _wait(_self: SubagentControl, subagent_ids: list[str], timeout_ms: int = 0) -> WaitResult:+ return WaitResult(+ statuses={+ "done": SubagentStatus.completed("answer"),+ "failed": SubagentStatus.errored(reason + "x" * 3000),+ "silent": SubagentStatus.errored(""),+ },+ results={"done": "answer"}, output_files={}, timed_out=False,+ )++ monkeypatch.setattr(SubagentControl, "wait", _wait)+ result = await object.__new__(CompatibleSubagentControl).wait(["done", "failed", "silent"], timeout_ms=10)+ tool = SubagentWaitTool(ToolCard(name="subagent_wait", description="wait"), parent_agent=None)+ rendered = tool.render_for_llm(ToolOutput(success=True, data={+ "statuses": {sid: status.kind.value for sid, status in result.statuses.items()},+ "results": dict(result.results), "output_files": {}, "timed_out": False,+ }))+ assert result.results["done"] == "answer"+ assert f"error: {reason}" in rendered+ assert len(result.results["failed"]) < 2100+ assert "silent" not in result.resultsdiff --git a/tests/unit_tests/agentserver/rails/test_last_iteration_finalize_rail.py b/tests/unit_tests/agentserver/rails/test_last_iteration_finalize_rail.pyindex f7ad89108a..eea49e3994 100644--- a/tests/unit_tests/agentserver/rails/test_last_iteration_finalize_rail.py+++ b/tests/unit_tests/agentserver/rails/test_last_iteration_finalize_rail.py@@ -62,6 +62,7 @@ async def test_last_iteration_with_model_call_inputs_returns_actionable_error()ctx.request_force_finish.assert_called_once()payload = ctx.request_force_finish.call_args[0][0]assert payload["result_type"] == "error"+ assert payload["finish_reason"] == "max_iterations"assert "最大思考轮数" in payload["output"]assert "建议" in payload["output"]diff --git a/tests/unit_tests/agentserver/test_flash_adapter_rails.py b/tests/unit_tests/agentserver/test_flash_adapter_rails.pyindex c7e8bda40f..36f9b470d8 100644--- a/tests/unit_tests/agentserver/test_flash_adapter_rails.py+++ b/tests/unit_tests/agentserver/test_flash_adapter_rails.py@@ -118,6 +118,11 @@ def test_context_overflow_recovery_rail_is_in_flash_keep():assert "_context_overflow_recovery_rail" in JiuwenSwarmFlashAdapter._FLASH_RAIL_KEEP+def test_llm_retry_rail_is_in_flash_keep():+ """Without it the factory falls back to the stock rail, which never reports exhaustion."""+ assert "_llm_retry_rail" in JiuwenSwarmFlashAdapter._FLASH_RAIL_KEEP++def test_model_routing_rail_is_in_flash_keep():"""Flash must retain ModelRoutingRail for auto-gear routing.diff --git a/tests/unit_tests/agentserver/test_flash_artifact_post_process.py b/tests/unit_tests/agentserver/test_flash_artifact_post_process.pyindex f05584a86a..120675e12c 100644--- a/tests/unit_tests/agentserver/test_flash_artifact_post_process.py+++ b/tests/unit_tests/agentserver/test_flash_artifact_post_process.py@@ -67,7 +67,7 @@ def _assemble_flash(monkeypatch, *, turbo_enabled=True):monkeypatch.setattr(interface_deep, "_build_skill_protocol_prompt_rail", skill_protocol_builder)monkeypatch.setattr(interface_deep, "_build_heartbeat_rail", lambda: None)monkeypatch.setattr(interface_deep, "_build_avatar_rail", lambda: None)- monkeypatch.setattr(interface_deep, "_build_llm_retry_rail", lambda: None)+ monkeypatch.setattr(interface_deep, "_build_llm_retry_rail", lambda **_: None)rails = adapter._build_agent_rails(config["react"], config, mode="flash")task_builder.assert_not_called()skill_protocol_builder.assert_not_called()diff --git a/tests/unit_tests/agentserver/test_flash_turbo_discovery.py b/tests/unit_tests/agentserver/test_flash_turbo_discovery.pyindex 9cae9ebd88..36f1959604 100644--- a/tests/unit_tests/agentserver/test_flash_turbo_discovery.py+++ b/tests/unit_tests/agentserver/test_flash_turbo_discovery.py@@ -124,7 +124,7 @@ def _assemble_flash(monkeypatch, *, enabled=True, disabled=None, lazy=True, selemonkeypatch.setattr(interface_deep, "_build_skill_protocol_prompt_rail", skill_protocol_builder)monkeypatch.setattr(interface_deep, "_build_heartbeat_rail", lambda: None)monkeypatch.setattr(interface_deep, "_build_avatar_rail", lambda: None)- monkeypatch.setattr(interface_deep, "_build_llm_retry_rail", lambda: None)+ monkeypatch.setattr(interface_deep, "_build_llm_retry_rail", lambda **_: None)rails = adapter._build_agent_rails(config["react"], config, mode="flash")task_builder.assert_not_called()skill_protocol_builder.assert_not_called()diff --git a/tests/unit_tests/agentserver/test_model_cache_reload_retention.py b/tests/unit_tests/agentserver/test_model_cache_reload_retention.pyindex 460dd08a25..2950a69d2f 100644--- a/tests/unit_tests/agentserver/test_model_cache_reload_retention.py+++ b/tests/unit_tests/agentserver/test_model_cache_reload_retention.py@@ -9,6 +9,10 @@ from types import SimpleNamespacefrom openjiuwen.core.foundation.llm import Model, ModelClientConfig, ModelRequestConfigfrom jiuwenswarm.common.model_identity import build_model_identity_reference+from jiuwenswarm.server.runtime.agent_adapter.deep.model_factory import (+ _DEFAULT_STREAM_IDLE_TIMEOUT_SECONDS,+ _build_model_from_entry,+)from jiuwenswarm.server.runtime.agent_adapter.interface_deep import (JiuWenSwarmDeepAdapter,)@@ -26,6 +30,20 @@ def _entry(name: str, *, api_base: str = "https://maas.example/v2", api_key: str}+def test_entry_without_idle_timeout_uses_host_default():+ entry = _entry("glm-5.3")+ model = _build_model_from_entry(entry["model_client_config"], entry["model_config_obj"])+ assert model.model_client_config.stream_idle_timeout == _DEFAULT_STREAM_IDLE_TIMEOUT_SECONDS+ assert "stream_idle_timeout" not in entry["model_client_config"]+++def test_entry_idle_timeout_overrides_host_default():+ entry = _entry("glm-5.3")+ entry["model_client_config"]["stream_idle_timeout"] = 30+ model = _build_model_from_entry(entry["model_client_config"], entry["model_config_obj"])+ assert model.model_client_config.stream_idle_timeout == 30++def _config(*entries: dict) -> dict:return {"models": {"defaults": list(entries)}}diff --git a/tests/unit_tests/agentserver/test_subagent_execution_timeout.py b/tests/unit_tests/agentserver/test_subagent_execution_timeout.pyindex 51fbe32f96..1f973df365 100644--- a/tests/unit_tests/agentserver/test_subagent_execution_timeout.py+++ b/tests/unit_tests/agentserver/test_subagent_execution_timeout.py@@ -85,6 +85,7 @@ async def test_deadline_returns_failure_and_cancels_only_this_call(debug_capture({"error": "completion_timeout"}, "completion_timeout"),({"result_type": "error", "message": "provider unavailable"}, "provider unavailable"),({"result_type": "error", "output": "private output"}, "subagent_error"),+ ({"result_type": "error", "output": "private output", "finish_reason": "max_iterations"}, "max_iterations_reached"),])async def test_error_result_never_becomes_empty_success(debug_capture, result, reason):run = AsyncMock(return_value=result)diff --git a/tests/unit_tests/server/runtime/skill_turbo/test_flash_delivery_summary.py b/tests/unit_tests/server/runtime/skill_turbo/test_flash_delivery_summary.pyindex 7f29830986..b599d049ff 100644--- a/tests/unit_tests/server/runtime/skill_turbo/test_flash_delivery_summary.py+++ b/tests/unit_tests/server/runtime/skill_turbo/test_flash_delivery_summary.py@@ -116,7 +116,7 @@ def assemble_flash(monkeypatch):monkeypatch.setattr(interface_deep, "_build_skill_protocol_prompt_rail", skill_protocol_builder)monkeypatch.setattr(interface_deep, "_build_heartbeat_rail", lambda: None)monkeypatch.setattr(interface_deep, "_build_avatar_rail", lambda: None)- monkeypatch.setattr(interface_deep, "_build_llm_retry_rail", lambda: None)+ monkeypatch.setattr(interface_deep, "_build_llm_retry_rail", lambda **_: None)rails = adapter._build_agent_rails(config["react"], config, mode="flash")task_builder.assert_not_called()adapter._build_skill_turbo_prompt_rail.assert_called_once()