From dc96caa63e431da33ddce4108b86fa5e88e67300 Mon Sep 17 00:00:00 2001 From: liukx0205 Date: Fri, 6 Nov 2026 20:23:20 +0800 Subject: [PATCH] test(agent): cover work after creation signal aborts --- packages/core/agent-loop/tests/scope-lifecycle.spec.ts | 7 ++++++- packages/subagent/subagent/README.i18n.yaml | 4 ++-- packages/subagent/subagent/README.md | 2 +- packages/subagent/subagent/README.zh.md | 2 +- .../subagent/subagent/tests/factory-settlement.spec.ts | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/core/agent-loop/tests/scope-lifecycle.spec.ts b/packages/core/agent-loop/tests/scope-lifecycle.spec.ts index d6a6f50730..4b01c61f80 100644 --- a/packages/core/agent-loop/tests/scope-lifecycle.spec.ts +++ b/packages/core/agent-loop/tests/scope-lifecycle.spec.ts @@ -446,7 +446,9 @@ describe('agent scope lifecycle', () => { }) it('uses signal only for creation: aborts pending setup but not a returned live handle', async () => { - const ctx = await harness() + const adapter = new MockAdapter([textResponse('ok')]) + const ctx = await harness(adapter) + onTestFinished(() => ctx.fiber.dispose()) const pendingController = new AbortController() const setupStarted = Promise.withResolvers() const pending = ctx.agents.create({ @@ -474,6 +476,9 @@ describe('agent scope lifecycle', () => { await Promise.resolve() expect(ctx.agents.get(live.agent.id)).toBe(live.agent) expect(live.agent.status).toBe('idle') + live.agent.followup(createUserMessage({ content: text('continue'), source: { kind: 'user' } })) + await live.agent.whenIdle() + expect(adapter.requests).toHaveLength(1) await live.dispose() }) diff --git a/packages/subagent/subagent/README.i18n.yaml b/packages/subagent/subagent/README.i18n.yaml index ba1e09b3ea..d6864b8745 100644 --- a/packages/subagent/subagent/README.i18n.yaml +++ b/packages/subagent/subagent/README.i18n.yaml @@ -54,8 +54,8 @@ en: 61cddba23f4e7d1c zh: 052c8331a747403f /deepseek-ai-dsh-subagent/understand-the-implementation/activation-settlement: - en: 63105e704827a4e4 - zh: 12908b6319fb8b1e + en: ba22f6dd5f15f405 + zh: a469c6a4e49adea9 /deepseek-ai-dsh-subagent/understand-the-implementation/restoring-child-composition: en: 843f9f4319ca2ae3 zh: 218bf355816d4ff6 diff --git a/packages/subagent/subagent/README.md b/packages/subagent/subagent/README.md index ff2ba67405..a212a1ff72 100644 --- a/packages/subagent/subagent/README.md +++ b/packages/subagent/subagent/README.md @@ -124,7 +124,7 @@ External providers mint a fresh `SubagentRun.id` for each execution in the runti ### Activation settlement -The manager reserves child identity and capacity, materializes a local Agent or external execution, and accepts the initial task. Local admission closes after Agent activity and pending input finish, with activity revalidated after the final Session flush. Both local and external activations deliver their result after handle cleanup, capacity release, and settlement notifications. Host waits on the subagent tree root join progressing members and their cleanup, including failed executions; idle members with parked input remain resident without delaying host completion. Ordinary members do not wait for descendants. Closing admission and disposing the handle prevent late work from entering a released Agent. Parent delivery emits its notice after settlement only if the creator is still online; caller delivery leaves collection to the awaiting workflow. Headless hosts repeat `agent.whenIdle()` and `waitForTreeWork(agent)` until no child work remains and the parent stays idle with an unchanged Session sequence across the child check, so completion notices can produce the final parent answer. When the Agent factory disposes a local Agent, the manager settles its Activation after the Agent has drained and left the registry. If teardown fails with input still pending, the result reports `error` and retains any output already produced. +The manager reserves child identity and capacity, materializes a local Agent or external execution, and accepts the initial task. Local admission closes after Agent activity and pending input finish, with activity revalidated after the final Session flush. Both local and external activations deliver their result after handle cleanup, capacity release, and settlement notifications. Host waits on the subagent tree root join progressing members and their cleanup, including failed executions; idle members with parked input remain resident without delaying host completion. Ordinary members do not wait for descendants. Closing admission and disposing the handle prevent late work from entering a released Agent. Parent delivery emits its notice after settlement only if the creator is still online; caller delivery leaves collection to the awaiting workflow. Headless hosts repeat `agent.whenIdle()` and `waitForTreeWork(agent)` until no child work remains and the parent stays idle with an unchanged Session sequence across the child check, so completion notices can produce the final parent answer. When the Agent factory disposes a local Agent, the manager settles its Activation after the Agent has drained and left the registry. If teardown fails with input still pending, an otherwise `completed` result reports `error` and retains any output already produced. Settlement attribution records the exact execution stop reason without altering model-facing notice content. Historical notices can omit this field; readers must not infer it from the English summary. diff --git a/packages/subagent/subagent/README.zh.md b/packages/subagent/subagent/README.zh.md index a93e545587..a1f2745150 100644 --- a/packages/subagent/subagent/README.zh.md +++ b/packages/subagent/subagent/README.zh.md @@ -124,7 +124,7 @@ kind: "package-reference" ### Activation 结算 -管理器预留子级身份与容量,创建本地 Agent 或外部执行,并接受初始任务。本地 Agent 活动和待处理输入结束后,管理器在最终 Session flush 后重新验证活动状态并关闭准入。本地与外部 activation 都在句柄清理、容量归还及结算通知结束后交付结果。宿主在 subagent 树根上等待仍在推进的成员及其清理,包括失败的执行;有停放输入的空闲成员继续驻留,但不延迟宿主完成。普通成员不等待后代。关闭准入并释放句柄可防止迟到工作进入已释放的 Agent。父级投递仅在创建者仍在线时于结算后发送通知;调用方投递由等待中的工作流收集。Headless 宿主交替等待 `agent.whenIdle()` 与 `waitForTreeWork(agent)`,直至没有子级工作,且父级在检查子级期间保持空闲、Session 序号不变,让完成通知可驱动父级生成最终答案。本地 Agent 因工厂卸载而释放时,管理器在 Agent 停稳并离开注册表后结算其 Activation。如果关闭失败时仍有待处理输入,结果报告 `error`,并保留已经生成的输出。 +管理器预留子级身份与容量,创建本地 Agent 或外部执行,并接受初始任务。本地 Agent 活动和待处理输入结束后,管理器在最终 Session flush 后重新验证活动状态并关闭准入。本地与外部 activation 都在句柄清理、容量归还及结算通知结束后交付结果。宿主在 subagent 树根上等待仍在推进的成员及其清理,包括失败的执行;有停放输入的空闲成员继续驻留,但不延迟宿主完成。普通成员不等待后代。关闭准入并释放句柄可防止迟到工作进入已释放的 Agent。父级投递仅在创建者仍在线时于结算后发送通知;调用方投递由等待中的工作流收集。Headless 宿主交替等待 `agent.whenIdle()` 与 `waitForTreeWork(agent)`,直至没有子级工作,且父级在检查子级期间保持空闲、Session 序号不变,让完成通知可驱动父级生成最终答案。本地 Agent 因工厂卸载而释放时,管理器在 Agent 停稳并离开注册表后结算其 Activation。如果关闭失败时仍有待处理输入,原本为 `completed` 的结果改为报告 `error`,并保留已经生成的输出。 结算归属信息记录确切的执行停止原因,不改变面向模型的通知内容。历史通知可以缺少该字段;读取方不得从英文摘要推断它。 diff --git a/packages/subagent/subagent/tests/factory-settlement.spec.ts b/packages/subagent/subagent/tests/factory-settlement.spec.ts index 4a70337c87..032a2adcab 100644 --- a/packages/subagent/subagent/tests/factory-settlement.spec.ts +++ b/packages/subagent/subagent/tests/factory-settlement.spec.ts @@ -51,12 +51,12 @@ it.each([false, true])('settles an activation after factory unload with queue-cl rejectClear() expect(ctx.agents.get(started.childId)).toBeUndefined() release.resolve(undefined) - await vi.waitFor(() => { expect(state.resident.has(started.childId)).toBe(false) }, { timeout: 1000 }) const stopReason = rejectQueueClear ? 'error' : 'completed' expect(await started.result).toMatchObject({ output: [{ type: 'text', text: 'answer' }], stopReason, }) + expect(state.resident.has(started.childId)).toBe(false) expect(released).toHaveBeenCalledTimes(1) expect(ended).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ stopReason })) } finally {