From c0d7206aafb466802ff69366d241f29d8b1ac2f4 Mon Sep 17 00:00:00 2001 From: Liren Tu Date: Sun, 22 Nov 2026 15:05:22 -0800 Subject: [PATCH 1/2] Send a helper home when its subagent hands back Some subagents (the web-fetch helper, for one) fire no SubagentStart or SubagentStop, so their helper stayed at the lead's desk for the rest of the session. The adapter now reads a SubagentHandback that went through as worker.stop. Adds a fixture distilled from a real session. --- .../core/fixtures/06-subagent-handback.jsonl | 16 ++++++++++++++ packages/core/fixtures/README.md | 9 ++++++++ packages/core/src/__tests__/claude.test.ts | 18 ++++++++++++++++ packages/core/src/__tests__/director.test.ts | 15 +++++++++++++ .../src/__tests__/directorFixtures.test.ts | 12 +++++++++++ packages/core/src/__tests__/fixtures.test.ts | 21 ++++++++++++++++++- packages/core/src/adapters/claude.ts | 14 +++++++++++++ 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 packages/core/fixtures/06-subagent-handback.jsonl diff --git a/packages/core/fixtures/06-subagent-handback.jsonl b/packages/core/fixtures/06-subagent-handback.jsonl new file mode 100644 index 00000000..8e2696a4 --- /dev/null +++ b/packages/core/fixtures/06-subagent-handback.jsonl @@ -0,0 +1,16 @@ +{"ts":1795285153000,"ev":"agentopia.session","harness":"claude","cwd":"/Users/me/git/demo","argv":[],"version":"0.4.0"} +{"ts":1795285153423,"rid":"aa11bbAA","ev":"SessionStart","h":"claude","sid":"66666666-1111-2222-3333-444455556666","src":"startup"} +{"ts":1795285159928,"rid":"bb22ccBB","ev":"UserPromptSubmit","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","mode":"auto","msg":"Read the hooks docs and tell me what fires for a subagent"} +{"ts":1795285189329,"rid":"cc33ddCC","ev":"PreToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","mode":"auto","tool":"Agent","tuid":"toolu_06A","detail":"Read the hooks docs","spawn":"web-fetch"} +{"ts":1795285190383,"rid":"dd44eeDD","ev":"PreToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","mode":"auto","tool":"Bash","tuid":"toolu_06B","detail":"cat plugins/claude/hooks/hooks.json"} +{"ts":1795285190610,"rid":"ee55ffEE","ev":"PostToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","mode":"auto","tool":"Bash","tuid":"toolu_06B","detail":"cat plugins/claude/hooks/hooks.json","ok":true,"ms":37} +{"ts":1795285192869,"rid":"ff66aaFF","ev":"PreToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","aid":"ae594b27cc0ee8797","atype":"web-fetch","mode":"auto","tool":"WebFetch","tuid":"toolu_06C","detail":"code.claude.com"} +{"ts":1795285203044,"rid":"gg77bbGG","ev":"PostToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","aid":"ae594b27cc0ee8797","atype":"web-fetch","mode":"auto","tool":"WebFetch","tuid":"toolu_06C","detail":"code.claude.com","ok":true,"ms":10077} +{"ts":1795285206592,"rid":"hh88ccHH","ev":"PreToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","aid":"ae594b27cc0ee8797","atype":"web-fetch","mode":"auto","tool":"WebFetch","tuid":"toolu_06D","detail":"code.claude.com"} +{"ts":1795285207215,"rid":"ii99ddII","ev":"PreToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","aid":"ae594b27cc0ee8797","atype":"web-fetch","mode":"auto","tool":"WebFetch","tuid":"toolu_06E","detail":"code.claude.com"} +{"ts":1795285207646,"rid":"jj00eeJJ","ev":"PostToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","aid":"ae594b27cc0ee8797","atype":"web-fetch","mode":"auto","tool":"WebFetch","tuid":"toolu_06E","detail":"code.claude.com","ok":true,"ms":329} +{"ts":1795285224446,"rid":"kk11ffKK","ev":"PostToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","aid":"ae594b27cc0ee8797","atype":"web-fetch","mode":"auto","tool":"WebFetch","tuid":"toolu_06D","detail":"code.claude.com","ok":true,"ms":17808} +{"ts":1795285246040,"rid":"ll22aaLL","ev":"PreToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","aid":"ae594b27cc0ee8797","atype":"web-fetch","mode":"auto","tool":"SubagentHandback","tuid":"toolu_06F"} +{"ts":1795285246310,"rid":"mm33bbMM","ev":"PostToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","aid":"ae594b27cc0ee8797","atype":"web-fetch","mode":"auto","tool":"SubagentHandback","tuid":"toolu_06F","ok":true,"ms":36} +{"ts":1795285246387,"rid":"nn44ccNN","ev":"PostToolUse","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","mode":"auto","tool":"Agent","tuid":"toolu_06A","detail":"Read the hooks docs","spawn":"web-fetch","ok":true,"ms":55825} +{"ts":1795285260000,"rid":"oo55ddOO","ev":"Stop","h":"claude","sid":"66666666-1111-2222-3333-444455556666","pid":"c0ffee06-1111-2222-3333-444455556666","mode":"auto","msg":"Here is what fires for a subagent."} diff --git a/packages/core/fixtures/README.md b/packages/core/fixtures/README.md index e7d48f9e..0d4bad1a 100644 --- a/packages/core/fixtures/README.md +++ b/packages/core/fixtures/README.md @@ -92,3 +92,12 @@ agentopia hooks events rather than tailing transcripts: `PreToolUse` is the human deciding. - `PostToolUseFailure` (`ok:false`) — `yarn build` failed. Monitor sparks, worker slumps. Failure arrives as its own event, not as a status inside a normal response. + +## 06 — a subagent that hands back but never says goodbye + +From a real 2026-11 session: a `web-fetch` subagent whose records carry its `aid` on every +tool call, but which fires **no `SubagentStart` and no `SubagentStop`**. It appears on its +first tool call. What ends it is its `SubagentHandback` call (the tool a subagent uses to +hand its report to the lead, the last thing it does): the adapter reads the hand-back's +`PostToolUse` as `worker.stop`, so the helper walks out instead of staying at its lead's +desk for the rest of the session. diff --git a/packages/core/src/__tests__/claude.test.ts b/packages/core/src/__tests__/claude.test.ts index bd893140..cdcc4e70 100644 --- a/packages/core/src/__tests__/claude.test.ts +++ b/packages/core/src/__tests__/claude.test.ts @@ -90,6 +90,24 @@ describe('normalizeClaudeCode', () => { expect(event?.meta?.spawns).toBe('Explore'); }); + it('sends a subagent home once its hand-back goes through', () => { + // Some subagents fire no SubagentStop, so the hand-back is what ends them. + const event = normalizeClaudeCode( + raw({ ev: 'PostToolUse', tool: 'SubagentHandback', aid: 'ag1', atype: 'web-fetch' }) + ); + + expect(event).toMatchObject({ kind: 'worker.stop', worker: 'ag1', meta: { handback: true } }); + }); + + it.each(['PreToolUse', 'PostToolUseFailure'])( + 'stages nothing for a subagent hand-back %s', + (ev) => { + expect( + normalizeClaudeCode(raw({ ev, tool: 'SubagentHandback', aid: 'ag1', atype: 'web-fetch' })) + ).toBeNull(); + } + ); + it('ignores an agent type that arrives without an agent id', () => { const event = normalizeClaudeCode(raw({ ev: 'PreToolUse', tool: 'Bash', atype: 'Explore' })); diff --git a/packages/core/src/__tests__/director.test.ts b/packages/core/src/__tests__/director.test.ts index 540e557f..75004398 100644 --- a/packages/core/src/__tests__/director.test.ts +++ b/packages/core/src/__tests__/director.test.ts @@ -515,6 +515,21 @@ describe('Director helpers', () => { expect(desk(director.frame())?.subagents).toBe(0); }); + it('sends a helper out once when its hand-back and SubagentStop both say goodbye', () => { + const { clock, director } = setup(); + director.ingest(toolStart({ worker: 'a1', workerType: 'web-fetch' }, { name: 'WebFetch' })); + director.ingest(toolEnd({ worker: 'a1', workerType: 'web-fetch' }, { name: 'WebFetch' })); + run(director, clock, walkMs(6000)); + expect(desk(director.frame())?.subagents).toBe(1); + + director.ingest(ev('worker.stop', { worker: 'a1', meta: { handback: true } })); + run(director, clock, 200); + director.ingest(ev('worker.stop', { worker: 'a1' })); + run(director, clock, walkMs(8000)); + expect(find(director.frame(), 'a1')).toBeUndefined(); + expect(desk(director.frame())?.subagents).toBe(0); + }); + it('keeps a helper working at its own spot while the lead works at the seat', () => { const { clock, director } = setup(); director.ingest(ev('worker.start', { worker: 'a1', workerType: 'Explore' })); diff --git a/packages/core/src/__tests__/directorFixtures.test.ts b/packages/core/src/__tests__/directorFixtures.test.ts index f3463ed8..743a3aa1 100644 --- a/packages/core/src/__tests__/directorFixtures.test.ts +++ b/packages/core/src/__tests__/directorFixtures.test.ts @@ -120,6 +120,18 @@ describe('fixtures through the director', () => { expect(last.desks).toEqual([]); }); + it('06-subagent-handback: a helper that never fires SubagentStop still leaves', () => { + let sawHelper = false; + const last = replay('06-subagent-handback.jsonl', (frame) => { + if (frame.actors.some((a) => a.worker === 'ae594b27cc0ee8797')) { + sawHelper = true; + } + }); + + expect(sawHelper).toBe(true); + expect(last.actors.map((a) => a.worker)).toEqual([LEAD_WORKER]); + }); + it('03-parallel-reads: the whole batch is one stretch at the desk', () => { let workStretches = 0; let previous: string | undefined; diff --git a/packages/core/src/__tests__/fixtures.test.ts b/packages/core/src/__tests__/fixtures.test.ts index 283f6e0f..0da1b2d4 100644 --- a/packages/core/src/__tests__/fixtures.test.ts +++ b/packages/core/src/__tests__/fixtures.test.ts @@ -23,7 +23,7 @@ describe('fixtures stay valid', () => { const names = readdirSync(FIXTURES).filter((name) => name.endsWith('.jsonl')); it('covers every fixture in this directory', () => { - expect(names).toHaveLength(5); + expect(names).toHaveLength(6); }); it.each(names.map((name) => [name]))('%s parses fully and starts with a header', (name) => { @@ -85,6 +85,25 @@ describe('02-subagent', () => { }); }); +describe('06-subagent-handback', () => { + it('lets a subagent without SubagentStop go once it hands back', () => { + const lines = load('06-subagent-handback.jsonl'); + const reader = new BusReader(normalizeClaudeCode); + let state = emptySession(); + const seated: boolean[] = []; + for (const line of lines) { + const sequenced = reader.push(line); + if (sequenced) { + state = reduce(state, sequenced.event); + seated.push('ae594b27cc0ee8797' in state.workers); + } + } + + expect(seated).toContain(true); + expect(Object.keys(state.workers)).toEqual([LEAD_WORKER]); + }); +}); + describe('03-parallel-reads', () => { it('demonstrates why ts cannot order or identify records', () => { const records = parseRecords(load('03-parallel-reads.jsonl').join('\n')); diff --git a/packages/core/src/adapters/claude.ts b/packages/core/src/adapters/claude.ts index 98868867..9932cc9c 100644 --- a/packages/core/src/adapters/claude.ts +++ b/packages/core/src/adapters/claude.ts @@ -30,6 +30,13 @@ const TOOL_KINDS_BY_NAME: Record = { const MCP_PREFIX = 'mcp__'; +/** + * The tool a subagent calls to hand its report back to its lead: its last act. + * Some subagents fire no `SubagentStart` or `SubagentStop` at all (the web-fetch + * helper, for one), so the hand-back is the one sure sign that a helper is done. + */ +const HANDBACK_TOOL = 'SubagentHandback'; + export function toolKindFor(name: string | undefined): ToolKind { if (!name) { return 'other'; @@ -160,6 +167,13 @@ function toEvent(record: RawRecord): AgentopiaEvent | null { case 'PreToolUse': case 'PostToolUse': case 'PostToolUseFailure': { + // A helper that has handed its report back is done, and leaves. Only the + // hand-back that went through counts; the rest of the call is not staged. + if (record.tool === HANDBACK_TOOL && record.aid) { + return record.ev === 'PostToolUse' + ? withMeta({ ...base, kind: 'worker.stop' }, { handback: true }) + : null; + } const status = TOOL_STATUS_BY_EVENT[record.ev]; const event: AgentopiaEvent = { ...base, From 923ce78db7da65077d6d4bd62ddec039b267f81f Mon Sep 17 00:00:00 2001 From: Liren Tu Date: Sun, 22 Nov 2026 15:05:23 -0800 Subject: [PATCH 2/2] Add a patch changeset for helpers that never left --- .changeset/subagent-handback-leaves.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/subagent-handback-leaves.md diff --git a/.changeset/subagent-handback-leaves.md b/.changeset/subagent-handback-leaves.md new file mode 100644 index 00000000..92770804 --- /dev/null +++ b/.changeset/subagent-handback-leaves.md @@ -0,0 +1,5 @@ +--- +'agentopiahq': patch +--- + +Helpers now leave the office when their subagent finishes, even when the agent never says goodbye. Some subagents (the web fetcher, for one) fire no stop hook, so their helper used to stay at the desk for the rest of the session; agentopia now takes the subagent handing its report back as the end of its work.