Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/subagent-handback-leaves.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 16 additions & 0 deletions packages/core/fixtures/06-subagent-handback.jsonl
Original file line number Diff line number Diff line change
@@ -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."}
9 changes: 9 additions & 0 deletions packages/core/fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
18 changes: 18 additions & 0 deletions packages/core/src/__tests__/claude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }));

Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/__tests__/director.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }));
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/__tests__/directorFixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 20 additions & 1 deletion packages/core/src/__tests__/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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'));
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/adapters/claude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const TOOL_KINDS_BY_NAME: Record<string, ToolKind> = {

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';
Expand Down Expand Up @@ -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,
Expand Down