import { createUserMessage } from '@deepseek-ai/dsh-llm' import { describe, expect, it } from 'vitest' import { Context } from 'cordis' import { type Agent, type AgentOptions } from '@deepseek-ai/dsh-agent' import { Session, SessionId } from '@deepseek-ai/dsh-session' import AgentLoop from '@deepseek-ai/dsh-agent-loop' import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit' import InvariantService from '@deepseek-ai/dsh-invariants' import * as SessionInvariant from '@deepseek-ai/dsh-session/invariant' import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant' import * as AgentLoopInvariant from '@deepseek-ai/dsh-agent-loop/invariant' import SubagentService, { SUBAGENT_DESCRIPTOR_VERSION, SubagentError } from '@deepseek-ai/dsh-subagent' import { defineContentToolFixture } from '@deepseek-ai/dsh-tools' import { maxTokensResponse, MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts' import { resumeInProcessRun, startInProcessRun } from '../src/index.ts' type Script = ConstructorParameters[0] async function mountInvariants(ctx: Context): Promise { await ctx.plugin(InvariantService) await ctx.plugin(SessionInvariant) await ctx.plugin(AgentInvariant) await ctx.plugin(AgentLoopInvariant) } async function setup(script: Script, parentOptions: Partial = {}) { const ctx = new Context() await mountAgentLoopTestDependencies(ctx) await mountInvariants(ctx) await ctx.plugin(AgentLoop, { agents: [] }) await ctx.plugin(SubagentService) const adapter = new MockAdapter(script) ctx.llm.registerAdapter(['mock'], adapter) const parent = ctx.agentLoop.create(SessionId('parent'), { provider: 'mock', model: 'mock', ...parentOptions }) return { ctx, parent, adapter } } function request(parent: Agent, signal = new AbortController().signal) { return { prompt: [{ type: 'text' as const, text: 'child task' }], parent, signal } } function continuableRequest(parent: Agent) { const sessionId = SessionId('continuable-child') return { ...request(parent), continuation: { sessionId, descriptor: { version: SUBAGENT_DESCRIPTOR_VERSION, provider: 'spawn', agentProvider: 'mock', agentModel: 'mock', }, }, } } function text(blocks: readonly { type: string; text?: string }[]): string { return blocks.filter(block => block.type === 'text').map(block => block.text).join('') } describe('startInProcessRun', () => { it('returns only after publication, drives a fresh child, and disposes it', async () => { const { ctx, parent } = await setup([textResponse('driver answer')]) const run = await startInProcessRun(request(parent), {}) expect(ctx.agents.get(run.id)).toBeDefined() const result = await run.result expect(result.stopReason).toBe('completed') expect(text(result.output)).toBe('driver answer') expect(ctx.agents.get(run.id)!.options.subagentDepth).toBe(1) await run.dispose() await run.dispose() expect(ctx.agents.get(run.id)).toBeUndefined() }) it('requires a final durability checkpoint for a continuable child', async () => { const { ctx, parent } = await setup([textResponse('driver answer')]) const failure = new Error('disk full') let flushes = 0 ctx.on('session/flush', (session) => { if (session.header.parentSession === undefined) return flushes++ throw failure }) const run = await startInProcessRun(continuableRequest(parent), {}) const caught: unknown = await run.result.catch((error: unknown) => error) expect(caught).toBeInstanceOf(SubagentError) const durabilityError = caught as SubagentError expect(durabilityError.code).toBe('DURABILITY_FAILED') expect(durabilityError.cause).toBe(failure) expect(durabilityError.message).toContain( 'the latest child state was not confirmed persisted and may be unavailable or stale on resume: disk full', ) expect(flushes).toBe(2) await run.dispose() }) it('completes a continuable child when the final checkpoint retries a transient flush failure', async () => { const { ctx, parent } = await setup([textResponse('driver answer')]) let flushes = 0 ctx.on('session/flush', (session) => { if (session.header.parentSession === undefined) return flushes++ if (flushes === 1) throw new Error('temporary append failure') }) const run = await startInProcessRun(continuableRequest(parent), {}) await expect(run.result).resolves.toMatchObject({ stopReason: 'completed' }) expect(flushes).toBe(2) await run.dispose() }) it.each([ { checkpoint: 'succeeds', failure: undefined }, { checkpoint: 'fails', failure: new Error('disk full') }, ])('lets cancellation own the result when the final durability checkpoint $checkpoint', async ({ failure }) => { const { ctx, parent } = await setup([textResponse('driver answer')]) const checkpointStarted = Promise.withResolvers() const releaseCheckpoint = Promise.withResolvers() let flushes = 0 ctx.on('session/flush', async (session) => { if (session.header.parentSession === undefined) return flushes++ if (flushes !== 2) return checkpointStarted.resolve(undefined) await releaseCheckpoint.promise if (failure !== undefined) throw failure }) const controller = new AbortController() const run = await startInProcessRun({ ...continuableRequest(parent), signal: controller.signal, }, {}) await checkpointStarted.promise controller.abort() releaseCheckpoint.resolve(undefined) await expect(run.result).resolves.toMatchObject({ stopReason: 'aborted' }) expect(flushes).toBe(2) await run.dispose() }) it('keeps foreground runs best-effort when their turn checkpoint fails', async () => { const { ctx, parent } = await setup([textResponse('driver answer')]) let flushes = 0 ctx.on('session/flush', (session) => { if (session.header.parentSession === undefined) return flushes++ throw new Error('disk full') }) const run = await startInProcessRun(request(parent), {}) await expect(run.result).resolves.toMatchObject({ stopReason: 'completed' }) expect(flushes).toBe(1) await run.dispose() }) it('reports the message-turn outcome when a later non-message turn completes during flush', async () => { const { ctx, parent } = await setup([maxTokensResponse('partial answer')]) let injected = false ctx.on('session/flush', (session) => { if (injected || session.header.parentSession === undefined) return const lastEnd = session.events.findLast(event => event.type === 'turn/end') if (lastEnd?.type !== 'turn/end' || lastEnd.data.reason.kind !== 'max-tokens') return injected = true const turn = lastEnd.data.turn + 1 session.append('turn/start', { turn, trigger: { kind: 'injection', source: { kind: 'plugin', plugin: 'late-metadata' } }, }) session.append('user/message', createUserMessage({ content: [{ type: 'text', text: 'late metadata' }], source: { kind: 'plugin', plugin: 'late-metadata' }, }), { surfaceOp: 'append' }) session.append('turn/end', { turn, reason: { kind: 'completed' } }) }) const run = await startInProcessRun(request(parent), {}) const result = await run.result const child = ctx.agents.get(run.id)! expect(injected).toBe(true) expect(child.session.events.findLast(event => event.type === 'turn/end')) .toMatchObject({ data: { reason: { kind: 'completed' } } }) expect(result.stopReason).toBe('max-tokens') await run.dispose() }) it('seeds a forked child but reads only the child-owned output', async () => { const { ctx, parent } = await setup([textResponse('parent answer'), textResponse('child answer')]) parent.followup(createUserMessage({ content: [{ type: 'text', text: 'parent question' }], source: { kind: 'user' } })) await parent.whenIdle() const seed = parent.session.events.slice() const run = await startInProcessRun(request(parent), { seed }) const result = await run.result expect(text(result.output)).toBe('child answer') const child = ctx.agents.get(run.id)! expect(child.session.header.seedLength).toBe(seed.length) expect(child.session.events.slice(0, seed.length)).toEqual(seed) await run.dispose() }) it('persists the child depth in its session header', async () => { const { ctx, parent } = await setup([textResponse('child answer')]) const run = await startInProcessRun(request(parent), {}) await run.result // The recursion budget is durable session data, not only runtime options — // a depth that lived only in AgentOptions would reset to 0 on resume. expect(ctx.agents.get(run.id)!.session.header.delegationDepth).toBe(1) await run.dispose() }) it('inherits the parent output-token cap and accepts an explicit child override', async () => { const { ctx, parent, adapter } = await setup( [textResponse('inherited'), textResponse('overridden')], { maxTokens: 111 }, ) const inherited = await startInProcessRun(request(parent), {}) await inherited.result expect(adapter.requests[0]?.maxTokens).toBe(111) expect(ctx.agents.get(inherited.id)?.options.maxTokens).toBe(111) await inherited.dispose() const overridden = await startInProcessRun({ ...request(parent), agentOptions: { maxTokens: 222 }, }, {}) await overridden.result expect(adapter.requests[1]?.maxTokens).toBe(222) expect(ctx.agents.get(overridden.id)?.options.maxTokens).toBe(222) await overridden.dispose() }) it('counts a RESUMED child by its persisted header depth, not the absent runtime depth', async () => { // Resume rebuilds runtime options, so the durable header must keep this // depth-1 child from delegating as though it were top-level. const { ctx } = await setup([textResponse('unused')]) const resumed = (await ctx.agents.create({ sessionId: SessionId('resumed-child'), meta: { parentSession: SessionId('root'), delegationDepth: 1 }, agentOptions: { provider: 'mock', model: 'mock' }, signal: new AbortController().signal, })).agent await expect(startInProcessRun({ ...request(resumed), maxDepth: 1 }, {})) .rejects.toMatchObject({ name: 'SubagentDepthError', attemptedDepth: 2, maxDepth: 1 }) }) it('lets runtime options deepen but never lower the persisted depth', async () => { const { ctx } = await setup([textResponse('unused')]) const parent = (await ctx.agents.create({ sessionId: SessionId('deep-parent'), meta: { delegationDepth: 2 }, agentOptions: { provider: 'mock', model: 'mock', subagentDepth: 1 }, signal: new AbortController().signal, })).agent // Persisted 2 vs runtime 1: the child is depth 3, so maxDepth 2 rejects. await expect(startInProcessRun({ ...request(parent), maxDepth: 2 }, {})) .rejects.toMatchObject({ name: 'SubagentDepthError', attemptedDepth: 3, maxDepth: 2 }) }) it('rejects invalid and exceeded depth before publication', async () => { const { parent } = await setup([]) await expect(startInProcessRun({ ...request(parent), maxDepth: -1 }, {})) .rejects.toThrow('non-negative safe integer') await expect(startInProcessRun({ ...request(parent), maxDepth: 0 }, {})) .rejects.toMatchObject({ name: 'SubagentDepthError' }) for (const value of [Number.NaN, 1.5, -1, -0, Number.MAX_SAFE_INTEGER + 1]) { const malformed = { options: { subagentDepth: value }, session: { header: {} } } as unknown as Agent await expect(startInProcessRun(request(malformed), {})) .rejects.toThrow('agent subagentDepth must be a non-negative safe integer') } const maxParent = { options: { subagentDepth: Number.MAX_SAFE_INTEGER }, session: { header: {} } } as unknown as Agent await expect(startInProcessRun(request(maxParent), {})).rejects.toBeInstanceOf(RangeError) }) it('rejects an already-aborted request without publishing a child', async () => { const { ctx, parent } = await setup([]) const beforeAgents = ctx.agents.list().length const beforeSessions = ctx.sessions.list().length const controller = new AbortController() controller.abort('too late') await expect(startInProcessRun(request(parent, controller.signal), {})) .rejects.toThrow('aborted before child publication') expect(ctx.agents.list()).toHaveLength(beforeAgents) expect(ctx.sessions.list()).toHaveLength(beforeSessions) }) it('rejects an already-aborted resume before publication', async () => { const { parent } = await setup([]) const controller = new AbortController() controller.abort('too late') await expect(resumeInProcessRun({ sessionId: SessionId('resumed-child'), prompt: [{ type: 'text', text: 'continue' }], source: { kind: 'user' }, parent, signal: controller.signal, descriptor: { version: SUBAGENT_DESCRIPTOR_VERSION, provider: 'spawn' }, })).rejects.toThrow('aborted before child publication') }) it('resumes without inventing undeclared agent model options', async () => { const childId = SessionId('resumed-child') let flushes = 0 const child = { id: childId, options: {}, session: new Session(childId), status: 'idle', acceptsNextStep: false, ctx: { sessions: { flush: () => { flushes++ return Promise.resolve() }, }, } as unknown as Context, send(): void {}, reserveTurnAdmission: () => undefined, updateInbox: () => 'not-found', followup(): void {}, steer() { return { outcome: Promise.resolve({ status: 'rejected' as const }) } }, inject(): void {}, cancel(): void {}, whenIdle: () => Promise.resolve(), } as Agent let resumedOptions: unknown const parent = { ctx: { agents: { resume: (options: { agentOptions: unknown }) => { resumedOptions = options.agentOptions return Promise.resolve({ agent: child, dispose: () => Promise.resolve() }) }, }, }, } as unknown as Agent const run = await resumeInProcessRun({ sessionId: childId, prompt: [{ type: 'text', text: 'continue' }], source: { kind: 'plugin', plugin: 'test-coordinator' }, parent, signal: new AbortController().signal, descriptor: { version: SUBAGENT_DESCRIPTOR_VERSION, provider: 'spawn' }, }) expect(resumedOptions).toEqual({}) await expect(run.result).resolves.toMatchObject({ stopReason: 'error' }) expect(flushes).toBe(1) await run.dispose() }) it('uses the request signal after publication and dispose as cancellation paths', async () => { const { parent, adapter } = await setup(['hang', 'hang']) const controller = new AbortController() const signalled = await startInProcessRun(request(parent, controller.signal), {}) await new Promise(resolve => setTimeout(resolve, 30)) controller.abort('stop child') await expect(signalled.result).resolves.toMatchObject({ stopReason: 'aborted' }) expect(adapter.requests[0]?.signal?.reason).toEqual({ kind: 'parent' }) const child = parent.ctx.agents.get(signalled.id) const turnEnd = child?.session.events.findLast(event => event.type === 'turn/end') expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason).toEqual({ kind: 'aborted' }) await signalled.dispose() const disposed = await startInProcessRun(request(parent), {}) await new Promise(resolve => setTimeout(resolve, 30)) await disposed.dispose() await expect(disposed.result).resolves.toMatchObject({ stopReason: 'aborted' }) }) it('cleans a failed unpublished setup before rejecting', async () => { const { ctx, parent } = await setup([]) const beforeAgents = ctx.agents.list().length const beforeSessions = ctx.sessions.list().length await expect(startInProcessRun({ ...request(parent), toolFilter: { deny: ['unknown-tool'] }, }, {})).rejects.toThrow('unknown global tool') expect(ctx.agents.list()).toHaveLength(beforeAgents) expect(ctx.sessions.list()).toHaveLength(beforeSessions) }) it('closes the abort handoff after the factory detaches its creation listener', async () => { const { ctx, parent } = await setup([]) const controller = new AbortController() const beforeAgents = ctx.agents.list().length const beforeSessions = ctx.sessions.list().length const parentWithAbortAtHandoff = { options: parent.options, session: parent.session, ctx: { // The driver's synchronous inheritance capture probes both policy // services opportunistically; this stub composes neither. get: () => undefined, agents: { create: async (options: Parameters[0]) => { const handle = await ctx.agents.create(options) // `create()` has detached its creation-only listener, but the // provider continuation has not installed its live-run listener. controller.abort('handoff race') return handle }, }, }, } as unknown as Agent await expect(startInProcessRun(request(parentWithAbortAtHandoff, controller.signal), {})) .rejects.toThrow('aborted before child publication') expect(ctx.agents.list()).toHaveLength(beforeAgents) expect(ctx.sessions.list()).toHaveLength(beforeSessions) }) it('confirmed steering rejects a settled child instead of queueing an untracked turn', async () => { const { ctx, parent } = await setup([textResponse('done')]) const run = await startInProcessRun(request(parent), {}) await run.result await expect(run.steer!([{ type: 'text', text: 'late' }], { kind: 'user' })) .rejects.toThrow(/not running; the message was not delivered/) const child = ctx.agents.get(run.id)! expect(child.session.events.some(event => event.type === 'steering/message')).toBe(false) await run.dispose() }) it('confirmed steering rejects when a concluding tool prevents request admission', async () => { const { ctx, parent } = await setup([toolCallResponse('c1', 'finalize', {})]) const enteredTool = Promise.withResolvers() const releaseTool = Promise.withResolvers() ctx.tools.register(defineContentToolFixture({ name: 'finalize', description: 'Finish the child run.', parameters: {}, async execute(_args, exec) { enteredTool.resolve(undefined) await releaseTool.promise exec.concludeTurn() return [{ type: 'text', text: 'final' }] }, })) const run = await startInProcessRun(request(parent), {}) const child = ctx.agents.get(run.id)! await enteredTool.promise const delivery = run.steer!([{ type: 'text', text: 'terminal race' }], { kind: 'user' }) releaseTool.resolve(undefined) await expect(delivery).rejects.toThrow(/stopped before steering admission; the message was not delivered/) await run.result expect(child.session.events.some(event => event.type === 'steering/message')).toBe(false) await run.dispose() }) it('confirmed steering fulfills only after the next request snapshot admits it', async () => { const { ctx, parent, adapter } = await setup([textResponse('first'), textResponse('second')]) const enteredStopping = Promise.withResolvers() const releaseStopping = Promise.withResolvers() let held = false ctx.on('agent/turn-stopping', (agent) => { if (agent.session.header.parentSession === undefined || held) return held = true enteredStopping.resolve(undefined) return releaseStopping.promise }) const run = await startInProcessRun(request(parent), {}) const child = ctx.agents.get(run.id)! await enteredStopping.promise let settled = false const delivery = run.steer!([{ type: 'text', text: 'after the first step' }], { kind: 'user' }) .then(() => { settled = true }) await Promise.resolve() expect(settled).toBe(false) releaseStopping.resolve(undefined) await delivery const result = await run.result expect(adapter.requests).toHaveLength(2) expect(JSON.stringify(adapter.requests[1]?.messages)).toContain('after the first step') expect((result.output[0] as { text?: string }).text).toBe('second') const steering = child.session.events.find(event => event.type === 'steering/message') expect(steering?.type === 'steering/message' && steering.data.message.source).toEqual({ kind: 'user' }) await run.dispose() }) it('carries steering from a non-terminal flush window into a tracked next turn', async () => { const { ctx, parent, adapter } = await setup([textResponse('first'), textResponse('second')]) const enteredFlush = Promise.withResolvers() const releaseFlush = Promise.withResolvers() let held = false ctx.on('session/flush', (session) => { if (session.header.parentSession === undefined || held) return if (!session.events.some(event => event.type === 'turn/end')) return held = true enteredFlush.resolve(undefined) return releaseFlush.promise }) const run = await startInProcessRun(request(parent), {}) const child = ctx.agents.get(run.id)! await enteredFlush.promise expect(child.status).toBe('running') const delivery = run.steer!([{ type: 'text', text: 'next tracked turn' }], { kind: 'user' }) releaseFlush.resolve(undefined) await delivery const result = await run.result expect(adapter.requests).toHaveLength(2) expect(child.session.events.filter(event => event.type === 'turn/start')).toHaveLength(2) expect(child.session.events.some(event => event.type === 'steering/message')).toBe(false) expect((result.output[0] as { text?: string }).text).toBe('second') await run.dispose() }) })