test(subagent): inspect children before activation completion

This commit is contained in:
Dudu-0223 committed 2026-09-21 10:43:43 +08:00
1 parent fb41430414
commit 1e437229da
3 files changed
+14 -15

No files matched your search

@@ -791,14 +791,14 @@ describe('dsh-subagent-dsh-sdk provider', () => {
it('wraps a shutdown rejection with safe facts after the runtime is reaped', async () => {
const rawCleanup = 'shutdown failed at /private/path SECRET_TOKEN'
const ctx = await setup()
const run = await startExternalActivation(ctx, 'dsh-sdk', request())
await run.result
const spy = vi.spyOn(DeepSeekHarness.prototype, 'close').mockImplementation(async function (this: DeepSeekHarness) {
spy.mockRestore()
await this.close()
throw new Error(rawCleanup)
})
try {
const run = await startExternalActivation(ctx, 'dsh-sdk', request())
await run.result
const error = await run.dispose().catch((cause: unknown) => cause)
expect(error).toBeInstanceOf(Error)
expect((error as Error).message).toBe(
@@ -82,7 +82,7 @@ describe('dsh-subagent-fork-in-process', () => {
const result = await run.result
expect(result.stopReason).toBe('completed')
expect(text(result.output)).toBe('fresh child')
const child = ctx.agents.get(run.id)!
const child = run.localAgent
// Only the child's own turn — no seeded parent turns.
expect(child.session.snapshotEvents().filter(e => e.type === 'turn/end')).toHaveLength(1)
expect(child.session.header.isSeeded).toBe(false)
@@ -100,7 +100,7 @@ describe('dsh-subagent-fork-in-process', () => {
const run = await start(ctx, 'fork', { prompt: [{ type: 'text', text: 'child q' }], parent })
await run.result
const child = ctx.agents.get(run.id)!
const child = run.localAgent
expect(child.session.header.isSeeded).toBe(true)
expect(child.session.inheritedEventCount).toBe(parentPrefixLen)
expect(child.session.snapshotEvents().slice(0, parentPrefixLen).at(-1)?.type).toBe('turn/end')
@@ -119,7 +119,7 @@ describe('dsh-subagent-fork-in-process', () => {
expect(result.stopReason).toBe('completed')
expect(text(result.output)).toBe('child answer')
const child = ctx.agents.get(run.id)!
const child = run.localAgent
// The child's log STARTS with the parent's prefix (seeded), then its own turn.
expect(child.session.snapshotEvents().length).toBeGreaterThan(parentPrefixLen)
// The seeded prefix carried the parent's user message.
@@ -151,7 +151,7 @@ describe('dsh-subagent-fork-in-process', () => {
expect(result.stopReason).toBe('completed')
expect(text(result.output)).toBe('child')
const child = ctx.agents.get(run.id)!
const child = run.localAgent
// The child's seed has exactly the ONE completed parent turn (the open one excluded).
const seedTurnEnds = child.session.snapshotEvents().filter(e => e.type === 'turn/end')
// 1 from the seeded parent turn + 1 from the child's own completed turn.
@@ -107,7 +107,7 @@ describe('dsh-subagent-spawn-in-process', () => {
const { ctx, parent } = await setup([textResponse('hi')])
const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
await run.result
const child = ctx.agents.get(run.id)!
const child = run.localAgent
expect(child.session.header.id).not.toBe(parent.session.header.id)
expect(child.session.header.parentSession).toBe(parent.session.header.id)
await run.dispose()
@@ -123,21 +123,20 @@ describe('dsh-subagent-spawn-in-process', () => {
const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'child prompt' }], parent })
await run.result
const child = ctx.agents.get(run.id)!
const child = run.localAgent
// The child's first user/message is its OWN prompt, not the parent's history.
const firstUser = child.session.snapshotEvents().find(e => e.type === 'user/message')
expect(firstUser).toBeDefined()
await run.dispose()
})
it('disposes the child to quiescence (agent removed from the registry)', async () => {
it('releases the child before delivering its result', async () => {
const { ctx, parent } = await setup([textResponse('x')])
const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
await run.result
expect(ctx.agents.get(run.id)).toBeDefined()
await run.dispose()
// After dispose, the child is unregistered (the AgentHandle teardown ran).
await run.result
expect(ctx.agents.get(run.id)).toBeUndefined()
await run.dispose()
})
it('stamps child depth = parent depth + 1 (via the merged AgentOptions field)', async () => {
@@ -145,7 +144,7 @@ describe('dsh-subagent-spawn-in-process', () => {
expect(parent.options.subagentDepth).toBeUndefined()
const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
await run.result
const child = ctx.agents.get(run.id)!
const child = run.localAgent
expect(child.options.subagentDepth).toBe(1)
await run.dispose()
})
@@ -258,7 +257,7 @@ describe('dsh-subagent-spawn-in-process', () => {
})
const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent: parentHandle.agent })
await run.result
const child = ctx.agents.get(run.id)!
const child = run.localAgent
expect(child.session.header.cwd).toBe('/tmp/parent-workspace')
await run.dispose()
await parentHandle.dispose()
@@ -432,7 +431,7 @@ describe('dsh-subagent-spawn-in-process', () => {
const childRequest = adapter.requests[0]!
expect((childRequest.tools ?? []).map(t => t.name)).not.toContain('forbidden_tool')
// …and the attempted call executed as UNKNOWN_TOOL (visible in the log).
const child = ctx.agents.get(run.id)!
const child = run.localAgent
const toolResult = child.session.snapshotEvents().find(e => e.type === 'tool/result')!
expect(JSON.stringify(toolResult.data)).toContain('unknown tool')
await run.dispose()