import { describe, it, expect, vi } from 'vitest'; import { RunState, buildAgentMap, deserializeModelResponse, deserializeItem, rehydrateProcessedResponseTools, CURRENT_SCHEMA_VERSION, } from '../src/runState'; import { processedResponseRequiresExecutionToolRehydration } from '../src/sandbox/runtime/toolRehydration'; import { RunContext } from '../src/runContext'; import { Agent } from '../src/agent'; import { handoff } from '../src/handoff'; import { Usage } from '../src/usage'; import type { ModelResponse } from '../src/model'; import { processModelResponseAsync } from '../src/runner/modelOutputs'; import { RunToolApprovalItem as ToolApprovalItem, RunMessageOutputItem, RunReasoningItem, RunToolCallItem, RunToolCallOutputItem, RunHandoffOutputItem, RunToolSearchCallItem, RunToolSearchOutputItem, } from '../src/items'; import { attachClientToolSearchExecutor, applyPatchTool, computerTool, hostedMcpTool, shellTool, tool, toolNamespace, type Tool, } from '../src/tool'; import * as protocol from '../src/types/protocol'; import { TEST_MODEL_MESSAGE, FakeComputer, FakeShell, FakeEditor, } from './stubs'; import { RunResult } from '../src/result'; import { UserError } from '../src/errors'; import logger from '../src/logger'; import { createAgentSpan } from '../src/tracing'; import { getGlobalTraceProvider } from '../src/tracing/provider'; import type { MCPServer, MCPTool } from '../src/mcp'; import { SANDBOX_SESSION_STATE_VERSION, SandboxAgent } from '../src/sandbox'; import { getFunctionToolStateKey, getFunctionToolStateKeyForCall, } from '../src/toolIdentity'; import { captureCurrentResponseToolOutputGuardrailResultStart, getCurrentResponseToolOutputGuardrailResultStart, } from '../src/runner/blockedOutputPersistence'; import { z, ZodError } from 'zod'; const REDACTED_TOOL_NAME_COLLISION_WARNING = 'Tool name collision detected. Assign unique routed tool names or enable tool data logging for details. Only the current dispatch winner will be exposed.'; function buildCurrentResponseOwnershipState() { const approvalTool = tool({ name: 'serialized_ownership_tool', description: 'Requires approval for serialized ownership tests.', parameters: z.object({ value: z.string() }), needsApproval: true, execute: async ({ value }) => value, }); const agent = new Agent({ name: 'Serialized ownership agent', tools: [approvalTool as Tool], }); const currentCall = { type: 'function_call', name: approvalTool.name, callId: 'reused-ownership-call', status: 'completed', arguments: '{"value":"same"}', } satisfies protocol.FunctionCallItem; const historicalCall = structuredClone(currentCall); const historicalCallItem = new RunToolCallItem(historicalCall, agent); const historicalApprovalItem = new ToolApprovalItem( historicalCall, agent, approvalTool.name, getFunctionToolStateKey(approvalTool), ); const currentCallItem = new RunToolCallItem(currentCall, agent); const currentApprovalItem = new ToolApprovalItem( currentCall, agent, approvalTool.name, getFunctionToolStateKey(approvalTool), ); const response: ModelResponse = { output: [currentCall], usage: new Usage(), responseId: 'serialized-ownership-response', }; const state = new RunState(new RunContext(), 'input', agent, 2); state._generatedItems = [ historicalCallItem, historicalApprovalItem, currentCallItem, currentApprovalItem, ]; state._lastProcessedResponse = { newItems: [currentCallItem], toolsUsed: [approvalTool.name], handoffs: [], functions: [{ toolCall: currentCall, tool: approvalTool as any }], functionToolsNotFound: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [currentApprovalItem] }, }; state._modelResponses = [response]; state._lastTurnResponse = response; state._toolOutputGuardrailResults = [ { guardrail: { type: 'tool_output', name: 'accepted earlier guardrail' }, output: { outputInfo: 'accepted earlier metadata', behavior: { type: 'allow' }, }, }, ]; captureCurrentResponseToolOutputGuardrailResultStart(state, true); return { agent, state }; } async function expectToolNameCollisionWarnings( expectedCount: number, callback: () => Promise, ): Promise { const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {}); try { const result = await callback(); expect(warnSpy.mock.calls).toEqual( Array.from({ length: expectedCount }, () => [ REDACTED_TOOL_NAME_COLLISION_WARNING, ]), ); return result; } finally { warnSpy.mockRestore(); } } type AliasTestKeys = { crmAlias: string; crmCanonical: string; salesAlias: string; salesCanonical: string; }; function sandboxSessionStateEnvelope( providerState: Record, overrides: Record = {}, ): any { return { version: SANDBOX_SESSION_STATE_VERSION, backendId: 'unix-local', manifest: { version: 1, root: '/workspace', entries: {}, environment: {}, }, workspaceReady: true, providerState, ...overrides, }; } export function registerRunStateCoreTests(): void { describe('RunState', () => { it('initializes with default values', () => { const context = new RunContext({ foo: 'bar' }); const agent = new Agent({ name: 'TestAgent' }); const state = new RunState(context, 'input', agent, 3); expect(state._currentTurn).toBe(0); expect(state._currentAgent).toBe(agent); expect(state._originalInput).toBe('input'); expect(state._maxTurns).toBe(3); expect(state._noActiveAgentRun).toBe(true); expect(state._modelResponses).toEqual([]); expect(state._generatedItems).toEqual([]); expect(state._currentStep).toBeUndefined(); expect(state._trace).toBeNull(); expect(state._context.context).toEqual({ foo: 'bar' }); expect(state._toolInputGuardrailResults).toEqual([]); expect(state._toolOutputGuardrailResults).toEqual([]); }); it('clears restored trace state', () => { const context = new RunContext(); const agent = new Agent({ name: 'TraceClearingAgent' }); const state = new RunState(context, 'input', agent, 1); const provider = getGlobalTraceProvider(); provider.setDisabled(false); try { const trace = provider.createTrace({ traceId: 'trace_original', name: 'Original workflow', }); const span = provider.createSpan( { data: { type: 'agent', name: 'OriginalSpan' } }, trace, ); state._trace = trace; state._currentAgentSpan = span; state.clearTrace(); expect(state._trace).toBeNull(); expect(state._currentAgentSpan).toBeUndefined(); const json = state.toJSON(); expect(json.trace).toBeNull(); expect(json.currentAgentSpan).toBeUndefined(); } finally { provider.setDisabled(true); } }); it('exposes the current agent', () => { const context = new RunContext(); const agent = new Agent({ name: 'CurrentAgent' }); const state = new RunState(context, 'input', agent, 1); expect(state.currentAgent).toBe(agent); }); it('returns history including original input and generated items', () => { const context = new RunContext(); const agent = new Agent({ name: 'HistAgent' }); const state = new RunState(context, 'input', agent, 1); state._generatedItems.push( new RunMessageOutputItem(TEST_MODEL_MESSAGE, agent), ); expect(state.history).toEqual([ { type: 'message', role: 'user', content: 'input' }, TEST_MODEL_MESSAGE, ]); }); it('drops orphan hosted shell calls from history', () => { const context = new RunContext(); const agent = new Agent({ name: 'HistAgentShell' }); const state = new RunState(context, 'input', agent, 1); state._generatedItems.push( new RunToolCallItem( { type: 'shell_call', callId: 'shell_orphan', status: 'completed', action: { commands: ['echo hi'] }, }, agent, ), ); expect(state.history).toEqual([ { type: 'message', role: 'user', content: 'input' }, ]); }); it('preserves history after serialization', async () => { const context = new RunContext(); const agent = new Agent({ name: 'HistAgent2' }); const state = new RunState(context, 'input', agent, 1); state._generatedItems.push( new RunMessageOutputItem(TEST_MODEL_MESSAGE, agent), ); const restored = await RunState.fromString(agent, state.toString()); expect(restored.history).toEqual(state.history); }); it('restores exact current-response ownership across repeated round trips', async () => { const { agent, state } = buildCurrentResponseOwnershipState(); const serialized = state.toJSON(); expect(serialized.currentResponseGeneratedItemOwnership).toEqual({ generatedItemStartIndex: 2, generatedItemEndIndexExclusive: 4, interruptionGeneratedItemIndexes: [3], toolOutputGuardrailResultStartIndex: 1, }); const onceRestored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(onceRestored._lastProcessedResponse?.newItems[0]).toBe( onceRestored._generatedItems[2], ); expect( onceRestored._currentStep?.type === 'next_step_interruption' ? onceRestored._currentStep.data.interruptions[0] : undefined, ).toBe(onceRestored._generatedItems[3]); expect(onceRestored._lastTurnResponse).toBe( onceRestored._modelResponses.at(-1), ); expect( getCurrentResponseToolOutputGuardrailResultStart(onceRestored), ).toBe(1); expect(onceRestored._generatedItems[0]).not.toBe( onceRestored._lastProcessedResponse?.newItems[0], ); const twiceSerialized = onceRestored.toJSON(); expect(twiceSerialized.currentResponseGeneratedItemOwnership).toEqual( serialized.currentResponseGeneratedItemOwnership, ); const twiceRestored = await RunState.fromString( agent, JSON.stringify(twiceSerialized), ); expect(twiceRestored._lastProcessedResponse?.newItems[0]).toBe( twiceRestored._generatedItems[2], ); expect( twiceRestored._currentStep?.type === 'next_step_interruption' ? twiceRestored._currentStep.data.interruptions[0] : undefined, ).toBe(twiceRestored._generatedItems[3]); }); it.each([ { name: 'a non-integer start index', mutate: (serialized: any) => { serialized.currentResponseGeneratedItemOwnership.generatedItemStartIndex = 1.5; }, }, { name: 'a non-terminal end index', mutate: (serialized: any) => { serialized.currentResponseGeneratedItemOwnership.generatedItemEndIndexExclusive = serialized.generatedItems.length - 1; }, }, { name: 'an interruption index inside the processed prefix', mutate: (serialized: any) => { serialized.currentResponseGeneratedItemOwnership.interruptionGeneratedItemIndexes = [2]; }, }, { name: 'duplicate interruption indexes', mutate: (serialized: any) => { serialized.currentResponseGeneratedItemOwnership.interruptionGeneratedItemIndexes = [3, 3]; }, }, { name: 'mismatched processed item content', mutate: (serialized: any) => { serialized.lastProcessedResponse.newItems[0].rawItem.arguments = '{"value":"different"}'; }, }, { name: 'an out-of-range guardrail-result boundary', mutate: (serialized: any) => { serialized.currentResponseGeneratedItemOwnership.toolOutputGuardrailResultStartIndex = serialized.toolOutputGuardrailResults.length + 1; }, }, { name: 'ownership data on released schema 1.19', mutate: (serialized: any) => { serialized.$schemaVersion = '1.19'; }, }, ])('rejects $name before returning a RunState', async ({ mutate }) => { const { agent, state } = buildCurrentResponseOwnershipState(); const serialized = JSON.parse(state.toString()); mutate(serialized); await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow(); }); it('keeps a released 1.19 snapshot readable without claiming current-response ownership', async () => { const { agent, state } = buildCurrentResponseOwnershipState(); const serialized = JSON.parse(state.toString()); serialized.$schemaVersion = '1.19'; delete serialized.currentResponseGeneratedItemOwnership; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored.getInterruptions()).toHaveLength(1); expect(restored._lastProcessedResponse?.newItems[0]).not.toBe( restored._generatedItems[2], ); expect( getCurrentResponseToolOutputGuardrailResultStart(restored), ).toBeUndefined(); }); it.each(['commentary', 'final_answer'] as const)( 'preserves assistant phase "%s" across history and serialized model responses', async (phase) => { const context = new RunContext(); const agent = new Agent({ name: 'AssistantPhaseAgent' }); const state = new RunState(context, 'input', agent, 1); const message: protocol.AssistantMessageItem = { ...TEST_MODEL_MESSAGE, phase, }; state._generatedItems.push(new RunMessageOutputItem(message, agent)); state._modelResponses = [ { usage: new Usage(), output: [message], responseId: 'response-phase', }, ]; const restored = await RunState.fromString(agent, state.toString()); expect(restored.history[1]).toMatchObject({ phase }); expect(restored._generatedItems[0]?.rawItem).toMatchObject({ phase }); expect(restored._modelResponses[0]?.output[0]).toMatchObject({ phase }); expect(restored.toJSON().$schemaVersion).toBe('1.23'); }, ); it('rejects invalid assistant message phases when restoring run state', async () => { const agent = new Agent({ name: 'InvalidAssistantPhaseAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push( new RunMessageOutputItem( { ...TEST_MODEL_MESSAGE, phase: 'commentary' }, agent, ), ); const serialized = state.toJSON(); (serialized.generatedItems[0]?.rawItem as any).phase = 'invalid'; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow(); }); it('preserves reasoningItemIdPolicy after serialization', async () => { const context = new RunContext(); const agent = new Agent({ name: 'ReasoningPolicyState' }); const state = new RunState(context, 'input', agent, 1); state.setReasoningItemIdPolicy('omit'); state._generatedItems.push( new RunReasoningItem( { type: 'reasoning', id: 'rs_state', content: [{ type: 'input_text', text: 'thinking' }], }, agent, ), ); const json = state.toJSON(); expect(json.reasoningItemIdPolicy).toBe('omit'); const restored = await RunState.fromString(agent, state.toString()); expect(restored._reasoningItemIdPolicy).toBe('omit'); expect(restored.history[1]).toEqual({ type: 'reasoning', content: [{ type: 'input_text', text: 'thinking' }], }); }); it('restores aggregated run usage after serialization', async () => { const context = new RunContext(); const agent = new Agent({ name: 'UsageState' }); const state = new RunState(context, 'input', agent, 1); state._context.usage.add( new Usage({ requests: 1, inputTokens: 100, outputTokens: 50, totalTokens: 150, }), ); const restored = await RunState.fromString(agent, state.toString()); expect(restored.usage.requests).toBe(1); expect(restored.usage.inputTokens).toBe(100); expect(restored.usage.outputTokens).toBe(50); expect(restored.usage.totalTokens).toBe(150); expect(restored.usage.requestUsageEntries).toHaveLength(1); }); it('does not double-count usage when resuming with an override context', async () => { const agent = new Agent({ name: 'UsageOverrideState' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._context.usage.add( new Usage({ requests: 1, inputTokens: 100, outputTokens: 50, totalTokens: 150, }), ); // The override context is authoritative (e.g. a nested agent-tool resume // passes the live outer context that already shares this usage), so its // usage must be left untouched rather than added to. const overrideContext = new RunContext(); overrideContext.usage.add( new Usage({ requests: 1, inputTokens: 100, outputTokens: 50, totalTokens: 150, }), ); const restored = await RunState.fromStringWithContext( agent, state.toString(), overrideContext, ); expect(restored.usage.requests).toBe(1); expect(restored.usage.inputTokens).toBe(100); expect(restored.usage.outputTokens).toBe(50); expect(restored.usage.totalTokens).toBe(150); }); it('leaves a fresh override context usage untouched', async () => { const agent = new Agent({ name: 'UsageFreshOverrideState' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._context.usage.add( new Usage({ requests: 1, inputTokens: 100, outputTokens: 50, totalTokens: 150, }), ); // A caller-supplied RunContext is authoritative: even a fresh one with zero // usage is left untouched. Its counters do not reveal whether its Usage is // newly owned or shared with another run, so it does not inherit the // serialized usage. const restored = await RunState.fromStringWithContext( agent, state.toString(), new RunContext(), ); expect(restored.usage.requests).toBe(0); expect(restored.usage.inputTokens).toBe(0); expect(restored.usage.outputTokens).toBe(0); expect(restored.usage.totalTokens).toBe(0); }); it('keeps a shared usage reference when resuming a forked context', async () => { const agent = new Agent({ name: 'UsageSharedRefState' }); // A nested run that made no model call before interruption serializes zero // usage. const state = new RunState(new RunContext(), 'input', agent, 1); // A nested agent-tool resume passes a forked context that shares its usage // object with the outer run. const outer = new RunContext(); const forked = new RunContext(); forked.usage = outer.usage; const restored = await RunState.fromStringWithContext( agent, state.toString(), forked, ); // The forked context's Usage must remain the very object shared with the // outer run (a replace would have severed the reference). expect(restored.usage).toBe(outer.usage); // Usage recorded after resume must still reach the outer run through the // shared reference (a replace would have severed it). forked.usage.add( new Usage({ requests: 1, inputTokens: 20, outputTokens: 10, totalTokens: 30, }), ); expect(outer.usage.requests).toBe(1); expect(outer.usage.totalTokens).toBe(30); }); it('preserves requestId on serialized model responses', async () => { const context = new RunContext(); const agent = new Agent({ name: 'RequestIdState' }); const state = new RunState(context, 'input', agent, 1); state._modelResponses = [ { usage: new Usage(), output: [TEST_MODEL_MESSAGE], responseId: 'resp_123', requestId: 'req_123', rawUsage: { input_tokens: 1, provider_metric: 0 }, }, ]; state._lastTurnResponse = state._modelResponses[0]; const serialized = state.toJSON() as any; expect(serialized.modelResponses[0]).not.toHaveProperty('rawUsage'); expect(serialized.lastModelResponse).not.toHaveProperty('rawUsage'); const restored = await RunState.fromString(agent, state.toString()); expect(restored._modelResponses).toHaveLength(1); expect(restored._modelResponses[0].responseId).toBe('resp_123'); expect(restored._modelResponses[0].requestId).toBe('req_123'); expect(restored._modelResponses[0].rawUsage).toBeUndefined(); expect(restored._lastTurnResponse?.requestId).toBe('req_123'); expect(restored._lastTurnResponse?.rawUsage).toBeUndefined(); }); it('preserves toolInput after serialization', async () => { const context = new RunContext({ foo: 'bar' }); context.toolInput = { text: 'hola', source: 'es', target: 'en' }; const agent = new Agent({ name: 'ToolInputAgent' }); const state = new RunState(context, 'input', agent, 1); const restored = await RunState.fromString(agent, state.toString()); expect(restored._context.toolInput).toEqual(context.toolInput); }); it('serializes sandbox envelope version 6 with schema 1.24', async () => { const context = new RunContext({ foo: 'bar' }); const agent = new Agent({ name: 'SandboxStateAgent' }); const state = new RunState(context, 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'sandbox-state-agent', currentAgentName: 'SandboxStateAgent', sessionState: sandboxSessionStateEnvelope( { workspaceId: 'ws_123' }, { snapshotFingerprint: 'fp_123', snapshotFingerprintVersion: 'v1', }, ), sessionsByAgent: { SandboxStateAgent: { backendId: 'unix-local', currentAgentKey: 'sandbox-state-agent', currentAgentName: 'SandboxStateAgent', sessionState: sandboxSessionStateEnvelope( { workspaceId: 'ws_123' }, { snapshotFingerprint: 'fp_123', snapshotFingerprintVersion: 'v1', }, ), preservedOwnedSession: true, reuseLiveSession: false, }, }, }; const serialized = state.toJSON(); expect(serialized.$schemaVersion).toBe('1.24'); expect(serialized.sandbox).toEqual(state._sandbox); const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._sandbox).toEqual(state._sandbox); const schema115Payload = structuredClone(serialized) as unknown as { $schemaVersion: string; sandbox: { sessionState: { version: number }; sessionsByAgent: Record< string, { sessionState: { version: number } } >; }; }; schema115Payload.$schemaVersion = '1.15'; schema115Payload.sandbox.sessionState.version = 2; for (const entry of Object.values( schema115Payload.sandbox.sessionsByAgent, )) { entry.sessionState.version = 2; } const restoredFromSchema115 = await RunState.fromString( agent, JSON.stringify(schema115Payload), ); expect(restoredFromSchema115._sandbox).toEqual(schema115Payload.sandbox); }); it('keeps reading schema 1.8 payloads without sandbox state', async () => { const context = new RunContext({ foo: 'bar' }); const agent = new Agent({ name: 'LegacySandboxStateAgent' }); const state = new RunState(context, 'input', agent, 1); const serialized = state.toJSON(); const legacyPayload = { ...serialized, $schemaVersion: '1.8' as const, }; delete (legacyPayload as { sandbox?: unknown }).sandbox; const restored = await RunState.fromString( agent, JSON.stringify(legacyPayload), ); expect(restored._sandbox).toBeUndefined(); expect(restored._currentAgent.name).toBe('LegacySandboxStateAgent'); }); it('keeps reading schema 1.14 payloads with sandbox session state version 1', async () => { const agent = new Agent({ name: 'LegacySandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'LegacySandboxEnvelopeAgent', currentAgentName: 'LegacySandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_legacy', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.14'; serialized.sandbox!.sessionState.version = 1; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._sandbox?.sessionState.version).toBe(1); }); it('rejects sandbox session state version 2 under schema 1.14', async () => { const agent = new Agent({ name: 'InvalidLegacySandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'InvalidLegacySandboxEnvelopeAgent', currentAgentName: 'InvalidLegacySandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_current', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.14'; serialized.sandbox!.sessionState.version = 2; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state schema version 1.14 does not support sandbox session state version 2.', ); }); it('keeps reading schema 1.17 payloads with sandbox session state version 2', async () => { const agent = new Agent({ name: 'VersionTwoSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'VersionTwoSandboxEnvelopeAgent', currentAgentName: 'VersionTwoSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_version_two', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.17'; serialized.sandbox!.sessionState.version = 2; serialized.sandbox!.sessionState.manifest.environment = { AWS_ACCESS_KEY_ID: { value: 'LEGACY_MANIFEST_SECRET_SENTINEL', }, }; serialized.sandbox!.sessionState.providerState.environment = { AWS_SECRET_ACCESS_KEY: 'LEGACY_PROVIDER_SECRET_SENTINEL', }; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._sandbox?.sessionState.version).toBe(2); expect(() => restored.toString()).toThrow( /must be resumed through the Runner/u, ); }); it('rejects sandbox session state version 3 under released schema 1.17', async () => { const agent = new Agent({ name: 'VersionThreeSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'VersionThreeSandboxEnvelopeAgent', currentAgentName: 'VersionThreeSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_version_three', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.17'; serialized.sandbox!.sessionState.version = 3; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state schema version 1.17 does not support sandbox session state version 3.', ); }); it('keeps reading schema 1.18 payloads with sandbox session state version 3', async () => { const agent = new Agent({ name: 'VersionThreeSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'VersionThreeSandboxEnvelopeAgent', currentAgentName: 'VersionThreeSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_version_three', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.18'; serialized.sandbox!.sessionState.version = 3; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._sandbox?.sessionState.version).toBe(3); expect(() => restored.toString()).toThrow( /must be resumed through the Runner/u, ); }); it('rejects sandbox session state version 4 under released schema 1.18', async () => { const agent = new Agent({ name: 'VersionFourSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'VersionFourSandboxEnvelopeAgent', currentAgentName: 'VersionFourSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_version_four', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.18'; serialized.sandbox!.sessionState.version = 4; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state schema version 1.18 does not support sandbox session state version 4.', ); }); it('keeps reading schema 1.19 payloads with sandbox session state version 4', async () => { const agent = new Agent({ name: 'VersionFourSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'VersionFourSandboxEnvelopeAgent', currentAgentName: 'VersionFourSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_version_four', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.19'; serialized.sandbox!.sessionState.version = 4; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._sandbox?.sessionState.version).toBe(4); expect(() => restored.toString()).toThrow( /must be resumed through the Runner/u, ); }); it('rejects sandbox session state version 5 under released schema 1.19', async () => { const agent = new Agent({ name: 'VersionFiveSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'VersionFiveSandboxEnvelopeAgent', currentAgentName: 'VersionFiveSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_version_five', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.19'; serialized.sandbox!.sessionState.version = 5; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state schema version 1.19 does not support sandbox session state version 5.', ); }); it('keeps reading schema 1.23 payloads with sandbox session state version 5', async () => { const agent = new Agent({ name: 'VersionFiveSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'VersionFiveSandboxEnvelopeAgent', currentAgentName: 'VersionFiveSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_version_five', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.23'; serialized.sandbox!.sessionState.version = 5; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._sandbox?.sessionState.version).toBe(5); expect(JSON.parse(restored.toString()).$schemaVersion).toBe('1.23'); }); it('rejects sandbox session state version 6 under released schema 1.23', async () => { const agent = new Agent({ name: 'VersionSixSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'VersionSixSandboxEnvelopeAgent', currentAgentName: 'VersionSixSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_version_six', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.$schemaVersion = '1.23'; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state schema version 1.23 does not support sandbox session state version 6.', ); }); it('sanitizes untrusted mount authority from current sandbox envelopes', async () => { const agent = new Agent({ name: 'CurrentSandboxEnvelopeAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._sandbox = { backendId: 'unix-local', currentAgentKey: 'CurrentSandboxEnvelopeAgent', currentAgentName: 'CurrentSandboxEnvelopeAgent', sessionState: sandboxSessionStateEnvelope({ workspaceId: 'ws_current_untrusted', }), sessionsByAgent: {}, }; const serialized = state.toJSON(); serialized.sandbox!.backendId = 'docker'; serialized.sandbox!.sessionState.backendId = 'docker'; serialized.sandbox!.sessionState.manifest.entries = { remote: { type: 's3_mount', bucket: 'private', accessKeyId: 'CURRENT_ACCESS_SENTINEL', secretAccessKey: 'CURRENT_SECRET_SENTINEL', mountStrategy: { type: 'in_container' }, }, }; serialized.sandbox!.sessionState.providerState.environment = { AWS_ACCESS_KEY_ID: 'CURRENT_ENV_ACCESS_SENTINEL', AWS_SECRET_ACCESS_KEY: 'CURRENT_ENV_SECRET_SENTINEL', }; serialized.sandbox!.sessionState.providerState.manifest = { entries: { remote: { type: 's3_mount', bucket: 'private', accessKeyId: 'PROVIDER_STATE_ACCESS_SENTINEL', secretAccessKey: 'PROVIDER_STATE_SECRET_SENTINEL', }, }, }; serialized.sandbox!.sessionState.providerState.sessionIdentity = 'PROVIDER_STATE_IDENTITY_SENTINEL'; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const roundTrip = restored.toString(); expect(roundTrip).not.toContain('CURRENT_ACCESS_SENTINEL'); expect(roundTrip).not.toContain('CURRENT_SECRET_SENTINEL'); expect(roundTrip).not.toContain('CURRENT_ENV_ACCESS_SENTINEL'); expect(roundTrip).not.toContain('CURRENT_ENV_SECRET_SENTINEL'); expect(roundTrip).not.toContain('PROVIDER_STATE_ACCESS_SENTINEL'); expect(roundTrip).not.toContain('PROVIDER_STATE_SECRET_SENTINEL'); expect(roundTrip).not.toContain('PROVIDER_STATE_IDENTITY_SENTINEL'); }); it('does not serialize runtime-only agent-tool metadata', async () => { const context = new RunContext({ foo: 'bar' }); const agentToolMetadata = { toolName: 'nested_tool', toolCallId: 'call-outer', toolArguments: '{"input":"hello"}', }; const agent = new Agent({ name: 'AgentToolContextAgent' }); const state = new RunState(context, 'input', agent, 1); state._agentToolInvocation = agentToolMetadata; const serialized = state.toJSON(); expect(serialized).not.toHaveProperty('agentToolInvocation'); const restored = await RunState.fromString(agent, state.toString()); expect( new RunResult(restored as any).agentToolInvocation, ).toBeUndefined(); }); it('does not infer agent-tool metadata from reused public contexts', () => { const agent = new Agent({ name: 'ReusedContextAgent' }); const nestedState = new RunState( new RunContext({ foo: 'bar' }), '', agent, 1, ); nestedState._agentToolInvocation = { toolName: 'nested_tool', toolCallId: 'call-outer', toolArguments: '{"input":"hello"}', }; const nestedResult = new RunResult(nestedState as any); const reusedState = new RunState( nestedResult.runContext as RunContext, 'input', agent, 1, ); expect(reusedState._context).toBe(nestedState._context); expect(reusedState._agentToolInvocation).toBeUndefined(); expect( new RunResult(reusedState as any).agentToolInvocation, ).toBeUndefined(); expect(reusedState.toJSON()).not.toHaveProperty('agentToolInvocation'); }); it('keeps override context instance state when merging agent-tool runs', async () => { class ExtendedRunContext extends RunContext<{ foo: string }> { marker: string; constructor(context: { foo: string }, marker: string) { super(context); this.marker = marker; } } const agent = new Agent({ name: 'MergedAgentToolContextAgent' }); const serializedContext = new RunContext({ foo: 'serialized' }); serializedContext.toolInput = { input: 'stale' }; serializedContext.approveTool( new ToolApprovalItem( { type: 'function_call', name: 'secure_tool', callId: 'call-1', status: 'completed', arguments: '{}', } as any, agent, ), ); const state = new RunState(serializedContext, 'input', agent, 1); const overrideContext = new ExtendedRunContext( { foo: 'fresh' }, 'fresh-marker', ); overrideContext.toolInput = { input: 'fresh' }; const restored = await RunState.fromStringWithContext( agent, state.toString(), overrideContext, { contextStrategy: 'merge' }, ); expect(restored._context).toBe(overrideContext); expect(restored._context).toBeInstanceOf(ExtendedRunContext); expect((restored._context as ExtendedRunContext).marker).toBe( 'fresh-marker', ); expect(restored._context.toolInput).toEqual({ input: 'fresh' }); expect(overrideContext.toJSON().toolInput).toEqual({ input: 'fresh' }); expect( new RunResult( new RunState(overrideContext, 'fresh input', agent, 1) as any, ).agentToolInvocation, ).toBeUndefined(); expect( new RunResult(restored as any).agentToolInvocation, ).toBeUndefined(); expect( restored._context.isToolApproved({ toolName: 'secure_tool', callId: 'call-1', }), ).toBe(true); }); it('prefers override-context rejection messages when merge conflicts occur', async () => { const agent = new Agent({ name: 'MergeRejectMessageAgent' }); const approvalItem = new ToolApprovalItem( { type: 'function_call', name: 'secure_tool', callId: 'call-1', status: 'completed', arguments: '{}', } as any, agent, ); const serializedContext = new RunContext({ foo: 'serialized' }); serializedContext.rejectTool(approvalItem, { alwaysReject: true, message: 'serialized rejection', }); const state = new RunState(serializedContext, 'input', agent, 1); const overrideContext = new RunContext({ foo: 'fresh' }); overrideContext.rejectTool(approvalItem, { alwaysReject: true, message: 'override rejection', }); const restored = await RunState.fromStringWithContext( agent, state.toString(), overrideContext, { contextStrategy: 'merge' }, ); expect(restored._context).toBe(overrideContext); expect( restored._context.getRejectionMessage('secure_tool', 'call-1'), ).toBe('override rejection'); expect( restored._context.getRejectionMessage('secure_tool', 'future-call'), ).toBe('override rejection'); }); it('lets merge override contexts clear serialized rejection messages', async () => { const agent = new Agent({ name: 'MergeRejectMessageClearAgent' }); const approvalItem = new ToolApprovalItem( { type: 'function_call', name: 'secure_tool', callId: 'call-1', status: 'completed', arguments: '{}', } as any, agent, ); const serializedContext = new RunContext({ foo: 'serialized' }); serializedContext.rejectTool(approvalItem, { alwaysReject: true, message: 'serialized rejection', }); const state = new RunState(serializedContext, 'input', agent, 1); const overrideContext = new RunContext({ foo: 'fresh' }); overrideContext.rejectTool(approvalItem, { alwaysReject: true, }); const restored = await RunState.fromStringWithContext( agent, state.toString(), overrideContext, { contextStrategy: 'merge' }, ); expect(restored._context).toBe(overrideContext); expect( restored._context.getRejectionMessage('secure_tool', 'call-1'), ).toBe(undefined); expect( restored._context.getRejectionMessage('secure_tool', 'future-call'), ).toBeUndefined(); expect( restored._context.isToolApproved({ toolName: 'secure_tool', callId: 'future-call', }), ).toBe(false); }); it('keeps override context instance state when replacing agent-tool runs', async () => { class ExtendedRunContext extends RunContext<{ foo: string }> { marker: string; constructor(context: { foo: string }, marker: string) { super(context); this.marker = marker; } } const agent = new Agent({ name: 'ReplacedAgentToolContextAgent' }); const serializedContext = new RunContext({ foo: 'serialized' }); serializedContext.toolInput = { input: 'stale' }; serializedContext.approveTool( new ToolApprovalItem( { type: 'function_call', name: 'secure_tool', callId: 'call-1', status: 'completed', arguments: '{}', } as any, agent, ), ); const state = new RunState(serializedContext, 'input', agent, 1); const overrideContext = new ExtendedRunContext( { foo: 'fresh' }, 'fresh-marker', ); overrideContext.toolInput = { input: 'fresh' }; const restored = await RunState.fromStringWithContext( agent, state.toString(), overrideContext, { contextStrategy: 'replace' }, ); expect(restored._context).toBe(overrideContext); expect(restored._context).toBeInstanceOf(ExtendedRunContext); expect((restored._context as ExtendedRunContext).marker).toBe( 'fresh-marker', ); expect(restored._context.toolInput).toEqual({ input: 'fresh' }); expect(overrideContext.toJSON().toolInput).toEqual({ input: 'fresh' }); expect( new RunResult( new RunState(overrideContext, 'fresh input', agent, 1) as any, ).agentToolInvocation, ).toBeUndefined(); expect( new RunResult(restored as any).agentToolInvocation, ).toBeUndefined(); expect( restored._context.isToolApproved({ toolName: 'secure_tool', callId: 'call-1', }), ).toBeUndefined(); }); it('tracks pending agent tool runs using tool name and call id', async () => { const context = new RunContext(); const agent = new Agent({ name: 'PendingAgent' }); const state = new RunState(context, 'input', agent, 1); state.setPendingAgentToolRun('toolA', 'call-1', 'state-A'); state.setPendingAgentToolRun('toolB', 'call-1', 'state-B'); expect(state.getPendingAgentToolRun('toolA', 'call-1')).toBe('state-A'); expect(state.getPendingAgentToolRun('toolB', 'call-1')).toBe('state-B'); const restored = await RunState.fromString(agent, state.toString()); expect(restored.getPendingAgentToolRun('toolA', 'call-1')).toBe( 'state-A', ); expect(restored.getPendingAgentToolRun('toolB', 'call-1')).toBe( 'state-B', ); }); it('keeps pending agent tool aliases on one canonical state entry', async () => { const context = new RunContext(); const namespacedLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Look up a CRM record.', parameters: z.object({}).strict(), execute: async () => 'lookup', }), ], })[0]!; const agent = new Agent({ name: 'PendingAliasAgent', tools: [namespacedLookup], }); const state = new RunState(context, 'input', agent, 1); const canonicalKey = getFunctionToolStateKey(namespacedLookup)!; const toolCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc-pending-alias', callId: 'call-1', name: 'lookup', namespace: 'crm', status: 'completed', arguments: '{}', }; state._lastProcessedResponse = { newItems: [], functions: [{ toolCall, tool: namespacedLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['crm.lookup'], hasToolsOrApprovalsToRun: () => true, }; state.setPendingAgentToolRun(canonicalKey, 'call-1', 'initial-state', [ 'crm.lookup', ]); expect(state._pendingAgentToolRuns.size).toBe(1); expect(state.getPendingAgentToolRun('crm.lookup', 'call-1')).toBe( 'initial-state', ); state.setPendingAgentToolRun('crm.lookup', 'call-1', 'updated-state'); expect(state._pendingAgentToolRuns.size).toBe(1); expect(state.getPendingAgentToolRun(canonicalKey, 'call-1')).toBe( 'updated-state', ); const restored = await RunState.fromString(agent, state.toString()); expect(restored._pendingAgentToolRuns.size).toBe(1); expect(restored.getPendingAgentToolRun('crm.lookup', 'call-1')).toBe( 'updated-state', ); restored.clearPendingAgentToolRun('crm.lookup', 'call-1'); expect(restored.hasPendingAgentToolRun(canonicalKey, 'call-1')).toBe( false, ); expect(restored.hasPendingAgentToolRun('crm.lookup', 'call-1')).toBe( false, ); expect(restored._pendingAgentToolRuns.size).toBe(0); expect(restored._pendingAgentToolRunAliases.size).toBe(0); }); it.each([ { name: 'dangling target', mutate: (aliases: Record, keys: AliasTestKeys) => { aliases[keys.crmAlias] = `${keys.crmCanonical}:missing-call`; }, }, { name: 'cross-call target', mutate: (aliases: Record, keys: AliasTestKeys) => { aliases[keys.crmAlias] = `${keys.crmCanonical}:call-2`; }, }, { name: 'cross-tool target', mutate: (aliases: Record, keys: AliasTestKeys) => { aliases[keys.crmAlias] = `${keys.salesCanonical}:call-3`; }, }, { name: 'alias chain', mutate: (aliases: Record, keys: AliasTestKeys) => { aliases[keys.crmAlias] = keys.salesAlias; }, }, { name: 'alias cycle', mutate: (aliases: Record, keys: AliasTestKeys) => { aliases[keys.crmAlias] = keys.salesAlias; aliases[keys.salesAlias] = keys.crmAlias; }, }, ])( 'rejects a pending agent tool alias with a $name', async ({ mutate }) => { const [crmLookup, salesLookup] = ['crm', 'sales'].map( (namespace) => toolNamespace({ name: namespace, description: `${namespace} tools.`, tools: [ tool({ name: 'lookup', description: `Look up a ${namespace} record.`, parameters: z.object({}).strict(), execute: async () => namespace, }), ], })[0]!, ); const agent = new Agent({ name: 'Invalid pending alias agent', tools: [crmLookup, salesLookup], }); const state = new RunState(new RunContext(), 'input', agent, 1); const crmCanonical = getFunctionToolStateKey(crmLookup)!; const salesCanonical = getFunctionToolStateKey(salesLookup)!; const createToolCall = ( namespace: string, callId: string, ): protocol.FunctionCallItem => ({ type: 'function_call', id: `fc-${namespace}-${callId}`, callId, name: 'lookup', namespace, status: 'completed', arguments: '{}', }); state._lastProcessedResponse = { newItems: [], functions: [ { toolCall: createToolCall('crm', 'call-1'), tool: crmLookup as any, }, { toolCall: createToolCall('crm', 'call-2'), tool: crmLookup as any, }, { toolCall: createToolCall('sales', 'call-3'), tool: salesLookup as any, }, ], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['crm.lookup', 'sales.lookup'], hasToolsOrApprovalsToRun: () => true, }; state.setPendingAgentToolRun(crmCanonical, 'call-1', 'crm-state', [ 'crm.lookup', ]); state.setPendingAgentToolRun(crmCanonical, 'call-2', 'crm-state-2', [ 'crm.lookup', ]); state.setPendingAgentToolRun(salesCanonical, 'call-3', 'sales-state', [ 'sales.lookup', ]); const serialized = state.toJSON(); const aliases = serialized.pendingAgentToolRunAliases!; mutate(aliases, { crmAlias: 'crm.lookup:call-1', crmCanonical, salesAlias: 'sales.lookup:call-3', salesCanonical, }); await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state pending agent tool aliases do not match the reconstructed pending function calls.', ); }, ); it('toJSON and toString produce valid JSON', () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent1' }); const state = new RunState(context, 'input1', agent, 2); const json = state.toJSON(); expect(json.$schemaVersion).toBe('1.23'); expect(json.currentTurn).toBe(0); expect(json.currentAgent).toEqual({ name: 'Agent1' }); expect(json.originalInput).toEqual('input1'); expect(json.maxTurns).toBe(2); expect(json.generatedItems).toEqual([]); expect(json.modelResponses).toEqual([]); expect(json.trace).toBeNull(); const str = state.toString(); expect(typeof str).toBe('string'); expect(JSON.parse(str)).toEqual(json); }); it.each([ { name: 'null', output: null }, { name: 'boolean', output: true }, { name: 'number', output: 42 }, { name: 'nested object', output: { city: 'sf', readings: [{ temperature: 18 }, null, true], }, }, { name: 'nested array', output: [{ city: 'sf' }, [18, 19], null], }, ])( 'round-trips a JSON-compatible $name tool output as structured data', async ({ output }) => { const agent = new Agent({ name: 'StructuredOutputAgent' }); const rawItem: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'weather', callId: 'structured-output', status: 'completed', output: 'model-visible output', }; const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push( new RunToolCallOutputItem(rawItem, agent, output), ); const serialized = state.toJSON(); expect((serialized.generatedItems[0] as any).output).toEqual(output); const restoredFromJson = await RunState.fromString( agent, JSON.stringify(serialized), ); const restoredFromString = await RunState.fromString( agent, state.toString(), ); for (const restored of [restoredFromJson, restoredFromString]) { expect( (restored._generatedItems[0] as RunToolCallOutputItem).output, ).toEqual(output); const repeated = await RunState.fromString( agent, restored.toString(), ); expect( (repeated._generatedItems[0] as RunToolCallOutputItem).output, ).toEqual(output); } }, ); it('preserves structured output across item carriers without leaking wrapper data', async () => { const agent = new Agent({ name: 'StructuredCarrierAgent' }); const rawItem: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'lookup', callId: 'structured-carrier', status: 'completed', output: 'provider-visible output', }; const output = { sdkOnlyMarker: 'wrapper-output-only', nested: [{ value: 1 }], }; const customData = { uiMarker: 'custom-data-only' }; const outputItem = new RunToolCallOutputItem( rawItem, agent, output, customData, ); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push(outputItem); state._lastProcessedResponse = { newItems: [outputItem], toolsUsed: [], handoffs: [], functions: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], hasToolsOrApprovalsToRun: () => false, }; const serialized = state.toJSON() as any; expect(serialized.generatedItems[0].output).toEqual(output); expect(serialized.lastProcessedResponse.newItems[0].output).toEqual( output, ); const restored = await RunState.fromString(agent, state.toString()); expect( (restored._generatedItems[0] as RunToolCallOutputItem).output, ).toEqual(output); expect( (restored._lastProcessedResponse?.newItems[0] as RunToolCallOutputItem) .output, ).toEqual(output); expect(restored._generatedItems[0].rawItem).toEqual(rawItem); expect( (restored._generatedItems[0] as RunToolCallOutputItem).customData, ).toEqual(customData); expect(JSON.stringify(restored.history)).not.toContain( 'wrapper-output-only', ); expect(JSON.stringify(restored.history)).not.toContain( 'custom-data-only', ); expect(restored.history).toContainEqual(rawItem); }); it('reads schema 1.17 string outputs and reserves structured outputs for 1.18', async () => { const agent = new Agent({ name: 'StructuredOutputVersionAgent' }); const rawItem: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'lookup', callId: 'structured-output-version', status: 'completed', output: 'provider-visible output', }; const legacyState = new RunState(new RunContext(), 'input', agent, 1); legacyState._generatedItems.push( new RunToolCallOutputItem(rawItem, agent, 'legacy output'), ); const legacySerialized = legacyState.toJSON() as any; legacySerialized.$schemaVersion = '1.17'; delete legacySerialized.completedToolInvocations; delete legacySerialized.completedToolInvocationEvidence; delete legacySerialized.ambiguousToolInvocationCallIds; const restoredLegacy = await RunState.fromString( agent, JSON.stringify(legacySerialized), ); expect( (restoredLegacy._generatedItems[0] as RunToolCallOutputItem).output, ).toBe('legacy output'); const structuredState = new RunState(new RunContext(), 'input', agent, 1); structuredState._generatedItems.push( new RunToolCallOutputItem(rawItem, agent, { value: 1 }), ); const structuredSerialized = structuredState.toJSON() as any; structuredSerialized.$schemaVersion = '1.17'; delete structuredSerialized.completedToolInvocations; delete structuredSerialized.completedToolInvocationEvidence; delete structuredSerialized.ambiguousToolInvocationCallIds; await expect( RunState.fromString(agent, JSON.stringify(structuredSerialized)), ).rejects.toThrow( 'Run state schema version 1.17 does not support structured tool output values.', ); }); it('rejects a serialized tool output item with missing output data', async () => { const agent = new Agent({ name: 'MissingOutputAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push( new RunToolCallOutputItem( { type: 'function_call_result', name: 'lookup', callId: 'missing-output', status: 'completed', output: 'provider-visible output', }, agent, 'wrapper output', ), ); const serialized = state.toJSON() as any; delete serialized.generatedItems[0].output; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toBeInstanceOf(ZodError); }); it('keeps unsupported live tool outputs on the released string fallback', async () => { const circular: Record = {}; circular.self = circular; const throwingGetter = Object.defineProperty({}, 'value', { enumerable: true, get() { throw new Error('getter failed'); }, }); class SerializableResult { toJSON() { return { value: 1 }; } } const nonEnumerableToJson = Object.defineProperty( { value: 1 }, 'toJSON', { enumerable: false, value() { return { legacy: true }; }, }, ); let arrayToJsonCalls = 0; const arrayWithToJson = Object.defineProperty([1, 2], 'toJSON', { enumerable: false, value() { arrayToJsonCalls += 1; return { call: arrayToJsonCalls }; }, }); let objectGetterReads = 0; const objectWithGetter = Object.defineProperty({}, 'value', { enumerable: true, get() { objectGetterReads += 1; return objectGetterReads; }, }); let arrayGetterReads = 0; const arrayWithGetter = Object.defineProperty([0], 0, { enumerable: true, get() { arrayGetterReads += 1; return arrayGetterReads; }, }); const unsupportedOutputs: unknown[] = [ circular, BigInt(1), undefined, () => 'value', Symbol('value'), Number.POSITIVE_INFINITY, new Uint8Array([1, 2]), new Map([['value', 1]]), new Set([1]), new Date('2026-08-10T00:00:00.000Z'), new SerializableResult(), nonEnumerableToJson, arrayWithToJson, objectWithGetter, arrayWithGetter, throwingGetter, ]; for (const [index, output] of unsupportedOutputs.entries()) { const agent = new Agent({ name: `UnsupportedOutputAgent${index}` }); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push( new RunToolCallOutputItem( { type: 'function_call_result', name: 'lookup', callId: `unsupported-output-${index}`, status: 'completed', output: 'provider-visible output', }, agent, output, ), ); const serialized = state.toJSON() as any; expect(typeof serialized.generatedItems[0].output).toBe('string'); if (output === nonEnumerableToJson) { expect(serialized.generatedItems[0].output).toBe('{"legacy":true}'); } if (output === arrayWithToJson) { expect(arrayToJsonCalls).toBe(1); expect(serialized.generatedItems[0].output).toBe('{"call":1}'); } if (output === objectWithGetter) { expect(objectGetterReads).toBe(1); expect(serialized.generatedItems[0].output).toBe('{"value":1}'); } if (output === arrayWithGetter) { expect(arrayGetterReads).toBe(1); expect(serialized.generatedItems[0].output).toBe('[1]'); } const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( (restored._generatedItems[0] as RunToolCallOutputItem).output, ).toBe(serialized.generatedItems[0].output); } }); it('serializes null maxTurns', async () => { const context = new RunContext(); const agent = new Agent({ name: 'NoMaxTurns' }); const state = new RunState(context, 'input1', agent, null); const json = state.toJSON(); expect(json.maxTurns).toBeNull(); const restored = await RunState.fromString(agent, state.toString()); expect(restored._maxTurns).toBeNull(); }); it('serializes duplicate-name agents with stable identities', async () => { const childA = new Agent({ name: 'SharedSkill', instructions: 'alpha child', }); const childB = new Agent({ name: 'SharedSkill', instructions: 'bravo child', }); const agentA = new Agent({ name: 'AgentA', handoffs: [childA] }); const agentB = new Agent({ name: 'AgentB', handoffs: [childB] }); const root = new Agent({ name: 'Root', handoffs: [agentA, agentB] }); const state = new RunState(new RunContext(), 'input', root, 2); state._currentAgent = childB; const json = state.toJSON(); expect(json.currentAgent).toEqual({ name: 'SharedSkill', identity: 'SharedSkill#2', }); const restored = await RunState.fromString(root, JSON.stringify(json)); expect(restored._currentAgent).toBe(childB); const restoredFromSchema110 = await RunState.fromString( root, JSON.stringify({ ...json, $schemaVersion: '1.10' as const }), ); expect(restoredFromSchema110._currentAgent).toBe(childB); const restoredFromSchema111 = await RunState.fromString( root, JSON.stringify({ ...json, $schemaVersion: '1.11' as const }), ); expect(restoredFromSchema111._currentAgent).toBe(childB); }); it('keeps literal identity suffixes from colliding with generated identities', () => { const duplicateA = new Agent({ name: 'SharedSkill', instructions: 'alpha child', }); const literalSuffix = new Agent({ name: 'SharedSkill#2' }); const duplicateB = new Agent({ name: 'SharedSkill', instructions: 'bravo child', }); const root = new Agent({ name: 'Root', handoffs: [duplicateA, literalSuffix, duplicateB], }); const state = new RunState(new RunContext(), 'input', root, 2); state._currentAgent = duplicateB; const json = state.toJSON(); expect(json.currentAgent).toEqual({ name: 'SharedSkill', identity: 'SharedSkill#3', }); }); it('restores duplicate-name run item ownership by identity', async () => { const childA = new Agent({ name: 'SharedSkill', instructions: 'alpha child', }); const childB = new Agent({ name: 'SharedSkill', instructions: 'bravo child', }); const root = new Agent({ name: 'Root', handoffs: [childA, childB] }); const approvalCall = { type: 'function_call', name: 'needs_approval', callId: 'approval-call', status: 'completed', arguments: '{}', } satisfies protocol.FunctionCallItem; const state = new RunState(new RunContext(), 'input', root, 2); state._currentAgent = childB; state._generatedItems = [ new ToolApprovalItem(approvalCall, childB), new RunHandoffOutputItem( { type: 'function_call_result', name: 'transfer_to_sharedskill', callId: 'handoff-call', status: 'completed', output: '{"assistant":"SharedSkill"}', }, childA, childB, ), ]; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [state._generatedItems[0]] }, }; state._lastProcessedResponse = { newItems: [state._generatedItems[0]], toolsUsed: [], handoffs: [], functions: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], hasToolsOrApprovalsToRun: () => true, }; const json = state.toJSON(); expect(json.generatedItems[0]).toMatchObject({ type: 'tool_approval_item', agent: { name: 'SharedSkill', identity: 'SharedSkill#2' }, }); expect(json.generatedItems[1]).toMatchObject({ type: 'handoff_output_item', sourceAgent: { name: 'SharedSkill' }, targetAgent: { name: 'SharedSkill', identity: 'SharedSkill#2' }, }); const restored = await RunState.fromString(root, JSON.stringify(json)); expect((restored._generatedItems[0] as ToolApprovalItem).agent).toBe( childB, ); expect( (restored._generatedItems[1] as RunHandoffOutputItem).sourceAgent, ).toBe(childA); expect( (restored._generatedItems[1] as RunHandoffOutputItem).targetAgent, ).toBe(childB); expect(restored.getInterruptions()[0]?.agent).toBe(childB); expect( (restored._lastProcessedResponse?.newItems[0] as ToolApprovalItem) .agent, ).toBe(childB); }); it('preserves live duplicate-name processed response ownership during tool rehydration', async () => { const childA = new Agent({ name: 'SharedSkill', instructions: 'alpha child', }); const childB = new Agent({ name: 'SharedSkill', instructions: 'bravo child', }); const root = new Agent({ name: 'Root', handoffs: [childA, childB] }); const approvalCall = { type: 'function_call', name: 'needs_approval', callId: 'approval-call', status: 'completed', arguments: '{}', } satisfies protocol.FunctionCallItem; const state = new RunState(new RunContext(), 'input', root, 2); state._currentAgent = childB; state._lastProcessedResponse = { newItems: [new ToolApprovalItem(approvalCall, childB)], toolsUsed: [], handoffs: [], functions: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], hasToolsOrApprovalsToRun: () => true, }; await rehydrateProcessedResponseTools(root, state, []); expect( (state._lastProcessedResponse?.newItems[0] as ToolApprovalItem).agent, ).toBe(childB); }); it('rejects duplicate-name states when a saved identity is missing', async () => { const childA = new Agent({ name: 'SharedSkill', instructions: 'alpha child', }); const childB = new Agent({ name: 'SharedSkill', instructions: 'bravo child', }); const root = new Agent({ name: 'Root', handoffs: [childA, childB] }); const state = new RunState(new RunContext(), 'input', root, 2); state._currentAgent = childB; const json = state.toJSON(); json.currentAgent = { name: 'SharedSkill', identity: 'SharedSkill#99' }; await expect(() => RunState.fromString(root, JSON.stringify(json)), ).rejects.toThrow('Agent identity SharedSkill#99 not found'); }); it('keeps legacy duplicate-name payloads rejected', async () => { const childA = new Agent({ name: 'SharedSkill' }); const childB = new Agent({ name: 'SharedSkill' }); const agentA = new Agent({ name: 'AgentA', handoffs: [childA] }); const agentB = new Agent({ name: 'AgentB', handoffs: [childB] }); const root = new Agent({ name: 'Root', handoffs: [agentA, agentB] }); const state = new RunState(new RunContext(), 'input', childB, 2); const json = state.toJSON(); json.$schemaVersion = '1.9'; json.currentAgent = { name: 'SharedSkill' }; await expect(() => RunState.fromString(root, JSON.stringify(json)), ).rejects.toThrow( 'Duplicate agent name "SharedSkill" detected. Use unique agent names when serializing RunState.', ); }); it('only serializes tracing api key when explicitly requested', async () => { const context = new RunContext(); const agent = new Agent({ name: 'Tracey' }); const state = new RunState(context, 'input', agent, 1); const provider = getGlobalTraceProvider(); provider.setDisabled(false); const trace = provider.createTrace({ traceId: 'trace_test', name: 'workflow', tracingApiKey: 'trace-key', }); const agentSpan = provider.createSpan( { data: { type: 'agent', name: 'TestAgentSpan' } }, trace, ); state._currentAgentSpan = agentSpan; state._trace = trace; const defaultJson = state.toJSON(); expect(defaultJson.trace?.tracing_api_key).toBeUndefined(); const optInJson = state.toJSON({ includeTracingApiKey: true }); expect(optInJson.trace?.tracing_api_key).toBe('trace-key'); const restoredWithKey = await RunState.fromString( agent, state.toString({ includeTracingApiKey: true }), ); expect(restoredWithKey._trace?.tracingApiKey).toBe('trace-key'); const restoredWithoutKey = await RunState.fromString( agent, state.toString(), ); expect(restoredWithoutKey._trace?.tracingApiKey).toBeUndefined(); provider.setDisabled(true); }); it('serializes tool_call_output_item for non-function tools', async () => { const context = new RunContext(); const agent = new Agent({ name: 'OutputAgent' }); const rawShellOutput: protocol.ShellCallResultItem = { type: 'shell_call_output', callId: 'call-shell', output: [ { stdout: 'ok', stderr: '', outcome: { type: 'exit', exitCode: 0 } }, ], }; const state = new RunState(context, 'input', agent, 1); state._generatedItems.push( new RunToolCallOutputItem( rawShellOutput, agent, rawShellOutput.output, undefined, 'executed', ), ); const restored = await RunState.fromString(agent, state.toString()); const restoredItem = restored._generatedItems[0]; expect(restoredItem).toBeInstanceOf(RunToolCallOutputItem); expect((restoredItem as RunToolCallOutputItem).rawItem).toEqual( rawShellOutput, ); expect((restoredItem as RunToolCallOutputItem).executionStatus).toBe( 'executed', ); expect(restored._sessionHistoryTransactionId).not.toBe( state._sessionHistoryTransactionId, ); }); it('round-trips Programmatic Tool Calling items in schema 1.14', async () => { const context = new RunContext(); const agent = new Agent({ name: 'ProgramAgent' }); const program: protocol.ProgramCallItem = { type: 'program', id: 'prog_1', callId: 'call_prog_1', code: 'text("ok")', fingerprint: 'fp_1', }; const programOutput: protocol.ProgramCallResultItem = { type: 'program_output', id: 'prog_out_1', callId: 'call_prog_1', output: 'ok', status: 'completed', }; const state = new RunState(context, 'input', agent, 1); state._generatedItems.push( new RunToolCallItem(program, agent), new RunToolCallOutputItem(programOutput, agent, programOutput.output), ); const serialized = state.toJSON(); expect(serialized.$schemaVersion).toBe('1.23'); const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._generatedItems.map((item) => item.rawItem)).toEqual([ program, programOutput, ]); serialized.$schemaVersion = '1.13'; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow('does not support Programmatic Tool Calling items'); }); }); } export function registerRunStateMigrationTests(): void { describe('RunState', () => { it('rejects pre-1.14 state with program-owned hosted calls', async () => { const agent = new Agent({ name: 'HostedProgramAgent' }); const hostedCall: protocol.HostedToolCallItem = { type: 'hosted_tool_call', id: 'ci_1', name: 'code_interpreter_call', status: 'completed', caller: { type: 'program', callerId: 'call_prog_1' }, providerData: { type: 'code_interpreter_call' }, }; const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push(new RunToolCallItem(hostedCall, agent)); const restored = await RunState.fromString(agent, state.toString()); expect(restored._generatedItems[0].rawItem).toEqual(hostedCall); const serialized = state.toJSON(); serialized.$schemaVersion = '1.13'; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow('does not support Programmatic Tool Calling items'); }); it('rechecks allowed callers against rebound tools during resume', async () => { const caller = { type: 'program' as const, callerId: 'call_program', }; const createProcessedResponse = ( overrides: Record, ): NonNullable['_lastProcessedResponse']> => ({ newItems: [], toolsUsed: [], handoffs: [], functions: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], hasToolsOrApprovalsToRun: () => true, ...overrides, }) as NonNullable['_lastProcessedResponse']>; const savedFunction = tool({ name: 'lookup', description: 'Look up a value.', parameters: z.object({}), allowedCallers: ['programmatic'], execute: async () => 'saved', }); const functionCall: protocol.FunctionCallItem = { type: 'function_call', name: 'lookup', callId: 'call_function', arguments: '{}', caller, }; const savedFunctionAgent = new Agent({ name: 'FunctionResumeAgent', tools: [savedFunction], }); const functionState = new RunState( new RunContext(), 'input', savedFunctionAgent, 1, ); functionState._lastProcessedResponse = createProcessedResponse({ functions: [{ toolCall: functionCall, tool: savedFunction }], }); await expect( RunState.fromString(savedFunctionAgent, functionState.toString()), ).resolves.toBeInstanceOf(RunState); const reboundFunctionAgent = new Agent({ name: 'FunctionResumeAgent', tools: [ tool({ name: 'lookup', description: 'Look up a value.', parameters: z.object({}), execute: async () => 'rebound', }), ], }); await expect( RunState.fromString(reboundFunctionAgent, functionState.toString()), ).rejects.toThrow(/caller programmatic/); const savedShell = shellTool({ shell: new FakeShell(), allowedCallers: ['programmatic'], }); const shellCall: protocol.ShellCallItem = { type: 'shell_call', callId: 'call_shell', action: { commands: ['echo hi'] }, caller, }; const savedShellAgent = new Agent({ name: 'ShellResumeAgent', tools: [savedShell], }); const shellState = new RunState( new RunContext(), 'input', savedShellAgent, 1, ); shellState._lastProcessedResponse = createProcessedResponse({ shellActions: [{ toolCall: shellCall, shell: savedShell }], }); const reboundShellAgent = new Agent({ name: 'ShellResumeAgent', tools: [shellTool({ shell: new FakeShell() })], }); await expect( RunState.fromString(reboundShellAgent, shellState.toString()), ).rejects.toThrow(/caller programmatic/); const savedApplyPatch = applyPatchTool({ editor: new FakeEditor(), allowedCallers: ['programmatic'], }); const applyPatchCall: protocol.ApplyPatchCallItem = { type: 'apply_patch_call', callId: 'call_apply_patch', status: 'completed', operation: { type: 'delete_file', path: 'temp.txt' }, caller, }; const savedApplyPatchAgent = new Agent({ name: 'ApplyPatchResumeAgent', tools: [savedApplyPatch], }); const applyPatchState = new RunState( new RunContext(), 'input', savedApplyPatchAgent, 1, ); applyPatchState._lastProcessedResponse = createProcessedResponse({ applyPatchActions: [ { toolCall: applyPatchCall, applyPatch: savedApplyPatch }, ], }); const reboundApplyPatchAgent = new Agent({ name: 'ApplyPatchResumeAgent', tools: [applyPatchTool({ editor: new FakeEditor() })], }); await expect( RunState.fromString(reboundApplyPatchAgent, applyPatchState.toString()), ).rejects.toThrow(/caller programmatic/); const savedMcp = hostedMcpTool({ serverLabel: 'inventory', serverUrl: 'https://inventory.example.com/mcp', requireApproval: 'always', allowedCallers: ['programmatic'], }); const savedMcpAgent = new Agent({ name: 'McpResumeAgent', tools: [savedMcp], }); const mcpApprovalCall: protocol.HostedToolCallItem = { type: 'hosted_tool_call', name: 'lookup_inventory', status: 'in_progress', caller, providerData: { type: 'mcp_approval_request', id: 'approval_inventory', arguments: '{"sku":"sku_123"}', name: 'lookup_inventory', server_label: 'inventory', }, }; const mcpState = new RunState( new RunContext(), 'input', savedMcpAgent, 1, ); mcpState._lastProcessedResponse = createProcessedResponse({ mcpApprovalRequests: [ { requestItem: new ToolApprovalItem(mcpApprovalCall, savedMcpAgent), mcpTool: savedMcp, }, ], }); const reboundMcpAgent = new Agent({ name: 'McpResumeAgent', tools: [ hostedMcpTool({ serverLabel: 'inventory', serverUrl: 'https://inventory.example.com/mcp', requireApproval: 'always', }), ], }); await expect( RunState.fromString(reboundMcpAgent, mcpState.toString()), ).rejects.toThrow(/caller programmatic/); }); it('accepts schema 1.13 application data that resembles PTC items', async () => { const context = new RunContext({ nested: { type: 'program_output' }, }); context.toolInput = { type: 'program', callId: 'tool_input_program' }; const agent = new Agent({ name: 'ProgramLikeDataAgent' }); const state = new RunState(context, 'input', agent, 1); const rawItem: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'lookup', callId: 'call_program_like_data', status: 'completed', output: 'done', providerData: { nested: { type: 'program_output' }, }, }; state._generatedItems.push( new RunToolCallOutputItem(rawItem, agent, 'done', { nested: { type: 'program', callId: 'custom_data_program' }, }), ); const serialized = state.toJSON(); serialized.$schemaVersion = '1.13'; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._context.context).toEqual(context.context); expect(restored._context.toolInput).toEqual(context.toolInput); expect(restored._generatedItems[0].rawItem.providerData).toEqual( rawItem.providerData, ); expect( (restored._generatedItems[0] as RunToolCallOutputItem).customData, ).toEqual({ nested: { type: 'program', callId: 'custom_data_program' }, }); }); it('throws error if schema version is missing or invalid', async () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent1' }); const state = new RunState(context, 'input1', agent, 2); const jsonVersion = state.toJSON() as any; delete jsonVersion.$schemaVersion; const str = JSON.stringify(jsonVersion); await expect(() => RunState.fromString(agent, str)).rejects.toThrow( 'Run state is missing schema version', ); jsonVersion.$schemaVersion = '0.1'; await expect(() => RunState.fromString(agent, JSON.stringify(jsonVersion)), ).rejects.toThrow( `Run state schema version 0.1 is not supported. Please use version ${CURRENT_SCHEMA_VERSION}`, ); }); it('rejects local execution provenance on program output', async () => { const agent = new Agent({ name: 'ProgramExecutionProvenanceAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push( new RunToolCallOutputItem( { type: 'program_output', callId: 'program-execution-provenance', status: 'completed', output: 'done', }, agent, 'done', undefined, 'executed', ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'Run state contains execution provenance for an unsupported tool output type.', ); }); it.each([ { schemaVersion: '1.16', field: 'currentTurnDeferredSessionItemIndexes', value: [0], }, { schemaVersion: '1.16', field: 'currentTurnBlockedSessionStartIndex', value: 0, }, { schemaVersion: '1.15', field: 'currentTurnDeferredSessionItemIndexes', value: [0], }, { schemaVersion: '1.15', field: 'pendingSessionHistoryTransaction', value: { operationId: 'v117-operation', transactionKind: 'blocked_append', runItemIndexes: [], replaceRunItemIndexes: [], alreadyPersistedCount: 0, persistedItemCount: 0, deferredItemIndexes: [], }, }, { schemaVersion: '1.16', field: 'currentTurnExecutedWithSessionBinding', value: true, }, { schemaVersion: '1.16', field: 'currentTurnSessionInputItems', value: [{ type: 'message', role: 'user', content: 'portable input' }], }, ])( 'rejects schema $schemaVersion payloads with $field', async ({ schemaVersion, field, value }) => { const agent = new Agent({ name: 'V116PersistenceEnvelopeAgent' }); const serialized = new RunState( new RunContext(), 'input', agent, 1, ).toJSON() as any; serialized.$schemaVersion = schemaVersion; delete serialized.currentTurnDeferredSessionItemIndexes; delete serialized.currentTurnBlockedSessionStartIndex; delete serialized.currentTurnSessionHistoryTransactionSessionId; delete serialized.currentTurnSessionReasoningItemIdPolicy; delete serialized.currentTurnSessionHistoryTransactionInputItems; delete serialized.sessionHistoryTransactionId; delete serialized.pendingSessionHistoryTransaction; serialized[field] = value; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( `Run state schema version ${schemaVersion} does not support output guardrail session persistence state.`, ); }, ); it('rejects schema 1.16 execution provenance', async () => { const agent = new Agent({ name: 'V116ExecutionProvenanceAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push( new RunToolCallOutputItem( { type: 'function_call_result', name: 'commit', callId: 'call-v117-execution', status: 'completed', output: 'committed', }, agent, 'committed', undefined, 'executed', ), ); const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.16'; delete serialized.currentTurnDeferredSessionItemIndexes; delete serialized.sessionHistoryTransactionId; delete serialized.pendingSessionHistoryTransaction; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state schema version 1.16 does not support output guardrail session persistence state.', ); }); it('rejects serialized local execution that was bound to a session', async () => { const agent = new Agent({ name: 'BoundExecutionAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push( new RunToolCallItem( { type: 'function_call', name: 'commit', callId: 'call-bound-execution', status: 'completed', arguments: '{}', }, agent, ), ); state._generatedItems.push( new RunToolCallOutputItem( { type: 'function_call_result', name: 'commit', callId: 'call-bound-execution', status: 'completed', output: 'committed', }, agent, 'committed', undefined, 'executed', ), ); state._currentTurnSessionHistoryTransactionSessionId = 'original-bound-session'; const serialized = state.toJSON() as any; expect(serialized.currentTurnExecutedWithSessionBinding).toBe(true); const overrideContext = new RunContext({ source: 'override' }); const before = overrideContext.toJSON(); await expect( RunState.fromStringWithContext( agent, JSON.stringify(serialized), overrideContext, { contextStrategy: 'merge' }, ), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); expect(overrideContext.toJSON()).toEqual(before); }); it('rejects serialized retained hosted work that was bound to a session', async () => { const agent = new Agent({ name: 'BoundHostedExecutionAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems.push( new RunToolCallItem( { type: 'hosted_tool_call', id: 'bound-hosted-execution', name: 'web_search_call', status: 'completed', providerData: { type: 'web_search_call' }, }, agent, ), ); state._currentTurnSessionHistoryTransactionSessionId = 'original-hosted-session'; const serialized = state.toJSON() as any; expect(serialized.currentTurnExecutedWithSessionBinding).toBe(true); await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); }); it('rejects a serialized terminal result for a bound persisted approval call', async () => { const agent = new Agent({ name: 'BoundApprovalResultAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems = [ new RunToolCallItem( { type: 'function_call', name: 'approval_tool', callId: 'call-bound-approval', status: 'completed', arguments: '{}', }, agent, ), new RunToolCallOutputItem( { type: 'function_call_result', name: 'approval_tool', callId: 'call-bound-approval', status: 'completed', output: 'rejected', }, agent, 'rejected', ), ]; state._currentTurnPersistedItemCount = 1; state._currentTurnSessionHistoryTransactionSessionId = 'original-approval-session'; const serialized = state.toJSON() as any; expect(serialized.currentTurnExecutedWithSessionBinding).toBe(true); await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); }); it('rejects schema 1.16 execution provenance in the processed response', async () => { const agent = new Agent({ name: 'V116ProcessedExecutionProvenanceAgent', }); const state = new RunState(new RunContext(), 'input', agent, 1); const outputItem = new RunToolCallOutputItem( { type: 'function_call_result', name: 'commit', callId: 'call-v117-processed-execution', status: 'completed', output: 'committed', }, agent, 'committed', undefined, 'executed', ); state._lastProcessedResponse = { newItems: [outputItem], toolsUsed: [], handoffs: [], functions: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], hasToolsOrApprovalsToRun: () => false, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.16'; delete serialized.currentTurnDeferredSessionItemIndexes; delete serialized.sessionHistoryTransactionId; delete serialized.pendingSessionHistoryTransaction; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state schema version 1.16 does not support output guardrail session persistence state.', ); }); it('validates persistence authority before mutating an override context', async () => { const agent = new Agent({ name: 'InvalidPersistenceAuthorityAgent' }); const approvalItem = new ToolApprovalItem( { type: 'function_call', name: 'secure_tool', callId: 'call-invalid-persistence-authority', status: 'completed', arguments: '{}', } as any, agent, ); const serializedContext = new RunContext({ source: 'serialized' }); serializedContext.toolInput = { source: 'serialized' }; serializedContext.approveTool(approvalItem); const serialized = new RunState( serializedContext, 'input', agent, 1, ).toJSON() as any; serialized.currentTurnPersistedItemCount = 1; serialized.currentTurnDeferredSessionItemIndexes = [0]; const overrideContext = new RunContext({ source: 'override' }); const before = overrideContext.toJSON(); await expect( RunState.fromStringWithContext( agent, JSON.stringify(serialized), overrideContext, { contextStrategy: 'merge' }, ), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); expect(overrideContext.toJSON()).toEqual(before); expect( overrideContext.isToolApproved({ toolName: 'secure_tool', callId: 'call-invalid-persistence-authority', }), ).toBeUndefined(); expect(overrideContext.toolInput).toBeUndefined(); }); it.each([ { name: 'empty blocked transaction source', operationId: undefined, runItemIndexes: [] as number[], }, { name: 'mismatched operation id', operationId: 'mismatched-operation-id', runItemIndexes: [0], }, ])( 'rejects a pending $name before mutating an override context', async ({ operationId, runItemIndexes }) => { const agent = new Agent({ name: 'InvalidPendingAuthorityAgent' }); const serializedContext = new RunContext({ source: 'serialized' }); serializedContext.toolInput = { source: 'serialized' }; const state = new RunState(serializedContext, 'input', agent, 1); state._generatedItems.push( new RunMessageOutputItem( { type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: 'generated' }], }, agent, ), ); const serialized = state.toJSON() as any; const expectedOperationId = [ serialized.sessionHistoryTransactionId, serialized.currentTurn, 'blocked_append', 0, 1, ].join(':'); serialized.pendingSessionHistoryTransaction = { operationId: operationId ?? expectedOperationId, transactionKind: 'blocked_append', runItemIndexes, replaceRunItemIndexes: [], alreadyPersistedCount: 0, persistedItemCount: 1, deferredItemIndexes: [], }; serialized.currentTurnBlockedSessionStartIndex = 0; serialized.currentTurnSessionHistoryTransactionSessionId = 'pending-authority-session'; serialized.currentTurnSessionReasoningItemIdPolicy = 'preserve'; serialized.currentTurnSessionHistoryTransactionInputItems = []; const overrideContext = new RunContext({ source: 'override' }); const before = overrideContext.toJSON(); await expect( RunState.fromStringWithContext( agent, JSON.stringify(serialized), overrideContext, { contextStrategy: 'merge' }, ), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); expect(overrideContext.toJSON()).toEqual(before); }, ); it.each([ { name: 'a rejected message inside the processed interval', runItemIndexes: [0], deferredItemIndexes: [1, 2], persistedItemCount: 3, }, { name: 'an item at the processed interval boundary', runItemIndexes: [2], deferredItemIndexes: [0, 1], persistedItemCount: 2, }, ])( 'rejects a pending blocked append sourced from $name before context mutation', async ({ runItemIndexes, deferredItemIndexes, persistedItemCount }) => { const agent = new Agent({ name: 'CanonicalPendingPlanAgent' }); const call: protocol.FunctionCallItem = { type: 'function_call', name: 'commit', callId: 'call-canonical-pending', status: 'completed', arguments: '{}', }; const result: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'commit', callId: call.callId, status: 'completed', output: 'committed', }; const state = new RunState( new RunContext({ source: 'serialized' }), 'input', agent, 1, ); state._generatedItems = [ new RunMessageOutputItem( { type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: 'rejected' }], }, agent, ), new RunToolCallItem(call, agent), new RunToolCallOutputItem( result, agent, result.output, undefined, 'executed', ), ]; const serialized = state.toJSON() as any; serialized.currentTurnBlockedSessionStartIndex = 0; serialized.currentTurnSessionHistoryTransactionSessionId = 'canonical-pending-session'; serialized.currentTurnSessionReasoningItemIdPolicy = 'preserve'; serialized.currentTurnSessionHistoryTransactionInputItems = []; serialized.pendingSessionHistoryTransaction = { operationId: [ serialized.sessionHistoryTransactionId, serialized.currentTurn, 'blocked_append', 0, persistedItemCount, ].join(':'), transactionKind: 'blocked_append', runItemIndexes, replaceRunItemIndexes: [], alreadyPersistedCount: 0, persistedItemCount, deferredItemIndexes, }; const overrideContext = new RunContext({ source: 'override' }); const before = overrideContext.toJSON(); await expect( RunState.fromStringWithContext( agent, JSON.stringify(serialized), overrideContext, { contextStrategy: 'merge' }, ), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); expect(overrideContext.toJSON()).toEqual(before); }, ); it('rejects a pending accepted replacement that differs from the canonical sparse plan', async () => { const agent = new Agent({ name: 'CanonicalAcceptedPlanAgent' }); const call: protocol.FunctionCallItem = { type: 'function_call', name: 'commit', callId: 'call-canonical-accepted', status: 'completed', arguments: '{}', }; const result: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'commit', callId: call.callId, status: 'completed', output: 'committed', }; const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems = [ new RunMessageOutputItem( { type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: 'accepted' }], }, agent, ), new RunToolCallItem(call, agent), new RunToolCallOutputItem( result, agent, result.output, undefined, 'executed', ), ]; state._currentTurnPersistedItemCount = 3; state._currentTurnDeferredSessionItemIndexes = new Set([0]); state._currentTurnBlockedSessionStartIndex = 0; state._currentTurnSessionHistoryTransactionSessionId = 'canonical-accepted-session'; state._currentTurnSessionReasoningItemIdPolicy = 'preserve'; state._currentTurnSessionHistoryTransactionInputItems = []; state._currentTurnSessionHistoryTransactionCanReplaceAcceptedOutput = true; const serialized = state.toJSON() as any; serialized.pendingSessionHistoryTransaction = { operationId: [ serialized.sessionHistoryTransactionId, serialized.currentTurn, 'accepted_replace', 3, 3, ].join(':'), transactionKind: 'accepted_replace', runItemIndexes: [1, 2], replaceRunItemIndexes: [1, 2], alreadyPersistedCount: 3, persistedItemCount: 3, deferredItemIndexes: [], }; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); }); it('rejects generated items outside acknowledged sparse authority', async () => { const agent = new Agent({ name: 'ClosedSparseAuthorityAgent' }); const call: protocol.FunctionCallItem = { type: 'function_call', name: 'commit', callId: 'call-closed-sparse-authority', status: 'completed', arguments: '{}', }; const result: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'commit', callId: call.callId, status: 'completed', output: 'committed', }; const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems = [ new RunMessageOutputItem( { type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: 'guarded output' }], }, agent, ), new RunToolCallItem(call, agent), new RunToolCallOutputItem( result, agent, result.output, undefined, 'executed', ), new RunMessageOutputItem( { type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: 'unguarded tail' }], }, agent, ), ]; state._currentTurnPersistedItemCount = 3; state._currentTurnDeferredSessionItemIndexes = new Set([0]); state._currentTurnBlockedSessionStartIndex = 0; state._currentTurnSessionHistoryTransactionSessionId = 'closed-sparse-authority-session'; state._currentTurnSessionReasoningItemIdPolicy = 'preserve'; state._currentTurnSessionHistoryTransactionInputItems = []; await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); }); it('rejects generated items outside pending sparse authority', async () => { const agent = new Agent({ name: 'ClosedPendingAuthorityAgent' }); const call: protocol.FunctionCallItem = { type: 'function_call', name: 'commit', callId: 'call-closed-pending-authority', status: 'completed', arguments: '{}', }; const result: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'commit', callId: call.callId, status: 'completed', output: 'committed', }; const state = new RunState(new RunContext(), 'input', agent, 1); state._generatedItems = [ new RunToolCallItem(call, agent), new RunToolCallOutputItem( result, agent, result.output, undefined, 'executed', ), new RunMessageOutputItem( { type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: 'unguarded tail' }], }, agent, ), ]; const serialized = state.toJSON() as any; serialized.currentTurnBlockedSessionStartIndex = 0; serialized.currentTurnSessionHistoryTransactionSessionId = 'closed-pending-authority-session'; serialized.currentTurnSessionReasoningItemIdPolicy = 'preserve'; serialized.currentTurnSessionHistoryTransactionInputItems = []; serialized.pendingSessionHistoryTransaction = { operationId: [ serialized.sessionHistoryTransactionId, serialized.currentTurn, 'blocked_append', 0, 2, ].join(':'), transactionKind: 'blocked_append', runItemIndexes: [0, 1], replaceRunItemIndexes: [], alreadyPersistedCount: 0, persistedItemCount: 2, deferredItemIndexes: [], }; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Serialized output guardrail session transaction authority cannot be resumed safely.', ); }); it('defaults output guardrail persistence state for a genuine schema 1.16 payload', async () => { const agent = new Agent({ name: 'GenuineV116PersistenceAgent' }); const serialized = new RunState( new RunContext(), 'input', agent, 1, ).toJSON() as any; serialized.$schemaVersion = '1.16'; delete serialized.currentTurnDeferredSessionItemIndexes; delete serialized.currentTurnSessionInputItems; delete serialized.currentTurnSessionHistoryTransactionInputItems; delete serialized.sessionHistoryTransactionId; delete serialized.pendingSessionHistoryTransaction; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._currentTurnDeferredSessionItemIndexes.size).toBe(0); expect(restored._sessionHistoryTransactionId).toBeTruthy(); expect(restored._pendingSessionHistoryTransaction).toBeUndefined(); }); it('rejects schema version 1.12 payloads with tool output customData', async () => { const context = new RunContext(); const agent = new Agent({ name: 'CustomDataVersionAgent' }); const state = new RunState(context, 'input1', agent, 2); const rawItem: protocol.FunctionCallResultItem = { type: 'function_call_result', name: 'lookup', callId: 'call_custom_data', status: 'completed', output: 'done', }; state._generatedItems.push( new RunToolCallOutputItem(rawItem, agent, 'done', { auditId: 'audit_123', }), ); const jsonVersion = state.toJSON() as any; expect(jsonVersion.$schemaVersion).toBe('1.23'); jsonVersion.$schemaVersion = '1.12'; await expect(() => RunState.fromString(agent, JSON.stringify(jsonVersion)), ).rejects.toThrow( 'Run state schema version 1.12 does not support tool output customData.', ); }); it('reads schema 1.15 approval keys and emits the current category-aware state', async () => { const context = new RunContext(); const agent = new Agent({ name: 'ApprovalKeyMigrationAgent' }); const state = new RunState(context, 'input', agent, 2); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: 'lookup', callId: 'legacy-call', status: 'completed', arguments: '{}', }; state.reject(new ToolApprovalItem(rawItem, agent)); const serialized = state.toJSON() as any; const categoryKey = getFunctionToolStateKeyForCall(rawItem)!; const ownerApprovals = serialized.context.functionApprovals.find( (entry: any) => entry.agentIdentity === agent.name, ).approvals; serialized.$schemaVersion = '1.15'; serialized.context.approvals.lookup = ownerApprovals[categoryKey]; delete serialized.context.functionApprovals; delete serialized.context.legacyFunctionApprovals; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context.isToolApproved({ toolName: 'lookup', callId: 'legacy-call', }), ).toBe(false); expect(restored.toJSON().$schemaVersion).toBe('1.23'); }); it('rehydrates schema 1.15 approval identity from enabled prepared tools', async () => { const enabledLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Look up a CRM record.', parameters: z.object({}), execute: async () => 'enabled', }), ], })[0]!; const disabledLookup = toolNamespace({ name: 'crm', description: 'Disabled CRM tools.', tools: [ tool({ name: 'lookup', description: 'Disabled duplicate.', parameters: z.object({}), isEnabled: false, execute: async () => 'disabled', }), ], })[0]!; const agent = new Agent({ name: 'LegacyApprovalAgent', tools: [enabledLookup, disabledLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: 'lookup', namespace: 'crm', callId: 'legacy-approval-call', status: 'completed', arguments: '{}', }; state._lastProcessedResponse = { newItems: [], functions: [{ toolCall: rawItem, tool: enabledLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['crm.lookup'], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [new ToolApprovalItem(rawItem, agent)], }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; delete serialized.currentStep.data.interruptions[0].functionToolStateKey; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const [approvalItem] = restored.getInterruptions(); expect(approvalItem.functionToolStateKey).toBe( getFunctionToolStateKey(enabledLookup), ); restored.approve(approvalItem); expect( restored._context.isToolApproved({ toolName: getFunctionToolStateKey(enabledLookup)!, callId: rawItem.callId, }), ).toBe(true); }); it.each(['namespaced', 'dotted'] as const)( 'migrates schema 1.15 pending nested state for the exact %s tool category', async (pendingCategory) => { const dottedLookup = tool({ name: 'crm_lookup', description: 'Look up a dotted CRM record.', parameters: z.object({}), execute: async () => 'dotted', }); dottedLookup.name = 'crm.lookup'; const namespacedLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Look up a namespaced CRM record.', parameters: z.object({}), execute: async () => 'namespaced', }), ], })[0]!; const pendingTool = pendingCategory === 'namespaced' ? namespacedLookup : dottedLookup; const toolCall: protocol.FunctionCallItem = { type: 'function_call', name: pendingCategory === 'namespaced' ? 'lookup' : 'crm.lookup', ...(pendingCategory === 'namespaced' ? { namespace: 'crm' } : {}), callId: `legacy-${pendingCategory}-call`, status: 'completed', arguments: '{}', }; const agent = new Agent({ name: `LegacyPending${pendingCategory}`, tools: [dottedLookup, namespacedLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall, tool: pendingTool as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['crm.lookup'], hasToolsOrApprovalsToRun: () => true, }; state.setPendingAgentToolRun( 'crm.lookup', toolCall.callId, `${pendingCategory}-state`, ); const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; delete serialized.pendingAgentToolRunAliases; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const canonicalKey = getFunctionToolStateKey(pendingTool)!; expect( restored.getPendingAgentToolRun(canonicalKey, toolCall.callId), ).toBe(`${pendingCategory}-state`); expect( restored.getPendingAgentToolRun('crm.lookup', toolCall.callId), ).toBe(`${pendingCategory}-state`); expect(restored._pendingAgentToolRuns.size).toBe(1); }, ); it('migrates pending schema 1.15 approvals while preserving ambiguous permanent owners', async () => { const decisions = [ { name: 'approved call', record: (callId: string) => ({ approved: [callId], rejected: [], }), expected: true, }, { name: 'rejected call', record: (callId: string) => ({ approved: [], rejected: [callId], messages: { [callId]: 'Rejected once' }, }), expected: false, message: 'Rejected once', }, { name: 'permanent approval', record: () => ({ approved: true, rejected: [] }), expected: true, }, { name: 'permanent rejection', record: (callId: string) => ({ approved: false, rejected: true, messages: { [callId]: 'Always rejected' }, stickyRejectMessage: 'Always rejected', }), expected: false, message: 'Always rejected', }, ]; for (const pair of ['bare-deferred', 'dotted-namespaced'] as const) { for (const decision of decisions) { const bareTool = tool({ name: pair === 'bare-deferred' ? 'lookup' : 'crm_lookup', description: 'Bare lookup.', parameters: z.object({}), execute: async () => 'bare', }); if (pair === 'dotted-namespaced') { bareTool.name = 'crm.lookup'; } const structuredTool = pair === 'bare-deferred' ? tool({ name: 'lookup', description: 'Deferred lookup.', parameters: z.object({}), deferLoading: true, execute: async () => 'deferred', }) : toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Namespaced lookup.', parameters: z.object({}), execute: async () => 'namespaced', }), ], })[0]!; const callId = `${pair}-${decision.name.replace(/ /g, '-')}`; const toolCall: protocol.FunctionCallItem = { type: 'function_call', name: 'lookup', namespace: pair === 'bare-deferred' ? 'lookup' : 'crm', callId, status: 'completed', arguments: '{}', }; const legacyKey = pair === 'bare-deferred' ? 'lookup' : 'crm.lookup'; const agent = new Agent({ name: `LegacyApproval-${pair}-${decision.name}`, tools: [bareTool, structuredTool], }); const state = new RunState(new RunContext(), 'input', agent, 2); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall, tool: structuredTool as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: [legacyKey], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [new ToolApprovalItem(toolCall, agent)], }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; serialized.context.approvals = { [legacyKey]: decision.record(callId), }; delete serialized.currentStep.data.interruptions[0] .functionToolStateKey; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const canonicalKey = getFunctionToolStateKey(structuredTool)!; const isPermanent = decision.name.startsWith('permanent'); expect( restored._context.isToolApproved({ toolName: canonicalKey, callId, }), `${pair}: ${decision.name}`, ).toBe(decision.expected); expect( restored._context.getRejectionMessage(canonicalKey, callId), ).toBe(decision.message); expect( restored._context.isToolApproved({ toolName: canonicalKey, callId: `${callId}-future`, }), ).toBe(isPermanent ? decision.expected : undefined); expect( restored._context.isToolApproved({ toolName: legacyKey, callId, }), `${pair}: ${decision.name} legacy owner`, ).toBe(isPermanent ? decision.expected : undefined); expect( restored._context.isToolApproved({ toolName: legacyKey, callId: `${callId}-future`, }), `${pair}: ${decision.name} future legacy owner`, ).toBe(isPermanent ? decision.expected : undefined); } } }); it.each([ ['permanent approval', { approved: true, rejected: [] }, true, undefined], [ 'permanent rejection', { approved: false, rejected: true, stickyRejectMessage: 'Always rejected', }, false, 'Always rejected', ], ] as const)( 'migrates a schema 1.15 %s when the legacy function owner is unique', async (_name, approvalRecord, expectedApproval, expectedMessage) => { const namespacedLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Namespaced lookup.', parameters: z.object({}), execute: async () => 'namespaced', }), ], })[0]!; const callId = 'unique-owner-call'; const toolCall: protocol.FunctionCallItem = { type: 'function_call', name: 'lookup', namespace: 'crm', callId, status: 'completed', arguments: '{}', }; const agent = new Agent({ name: 'UniqueLegacyApprovalOwner', tools: [namespacedLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall, tool: namespacedLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['crm.lookup'], hasToolsOrApprovalsToRun: () => true, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; serialized.context.approvals = { 'crm.lookup': approvalRecord, }; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const canonicalKey = getFunctionToolStateKey(namespacedLookup)!; expect( restored._context.isToolApproved({ toolName: canonicalKey, callId, }), ).toBe(expectedApproval); expect( restored._context.getRejectionMessage(canonicalKey, callId), ).toBe(expectedMessage); expect( restored._context.isToolApproved({ toolName: 'crm.lookup', callId, }), ).toBeUndefined(); const publicApproval = restored._context.toJSON().approvals['crm.lookup']; expect(publicApproval).toBeDefined(); expect(publicApproval.approved === true).toBe( approvalRecord.approved === true, ); expect(publicApproval.rejected === true).toBe( approvalRecord.rejected === true, ); expect( restored._context.toJSON().approvals[canonicalKey], ).toBeUndefined(); const durable = restored.toJSON() as any; expect(durable.context.approvals['crm.lookup']).toBeUndefined(); const durableApproval = durable.context.functionApprovals.find( (entry: any) => entry.agentIdentity === agent.name, ).approvals[canonicalKey]; expect(durableApproval.approved === true).toBe( approvalRecord.approved === true, ); expect(durableApproval.rejected === true).toBe( approvalRecord.rejected === true, ); }, ); it('migrates a unique schema 1.15 permanent owner without a current call', async () => { const namespacedLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Namespaced lookup.', parameters: z.object({}), execute: async () => 'namespaced', }), ], })[0]!; const agent = new Agent({ name: 'Unique inactive legacy approval owner', tools: [namespacedLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; serialized.context.approvals = { 'crm.lookup': { approved: true, rejected: [] }, }; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const canonicalKey = getFunctionToolStateKey(namespacedLookup)!; expect( restored._context.isToolApproved({ toolName: canonicalKey, callId: 'future_call', }), ).toBe(true); expect( restored._context.isToolApproved({ toolName: 'crm.lookup', callId: 'future_call', }), ).toBeUndefined(); const durable = restored.toJSON() as any; expect(durable.context.approvals['crm.lookup']).toBeUndefined(); expect(durable.context.legacyFunctionApprovals).toBeUndefined(); expect( durable.context.functionApprovals[0].approvals[canonicalKey], ).toMatchObject({ approved: true, rejected: [] }); }); it('migrates a unique schema 1.15 approval owned by an inactive child agent', async () => { const childLookup = toolNamespace({ name: 'crm', description: 'Child CRM tools.', tools: [ tool({ name: 'lookup', description: 'Child namespaced lookup.', parameters: z.object({}), execute: async () => 'child', }), ], })[0]!; const childAgent = new Agent({ name: 'Inactive legacy approval child', tools: [childLookup], }); const rootAgent = new Agent({ name: 'Inactive legacy approval root', tools: [ childAgent.asTool({ toolName: 'run_child', toolDescription: 'Runs the child agent.', }), ], }); const state = new RunState(new RunContext(), 'input', rootAgent, 2); const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; serialized.context.approvals = { 'crm.lookup': { approved: true, rejected: [] }, }; const restored = await RunState.fromString( rootAgent, JSON.stringify(serialized), ); const canonicalKey = getFunctionToolStateKey(childLookup)!; expect( restored._context.isToolApproved({ toolName: canonicalKey, callId: 'future_child_call', functionTool: false, agent: childAgent, }), ).toBe(true); const durable = restored.toJSON() as any; expect(durable.context.approvals['crm.lookup']).toBeUndefined(); expect(durable.context.legacyFunctionApprovals).toBeUndefined(); expect( durable.context.functionApprovals.find( (entry: any) => entry.agentIdentity === childAgent.name, ).approvals[canonicalKey], ).toMatchObject({ approved: true, rejected: [] }); }); it('keeps an exact non-function override separate from an inactive schema 1.15 function owner', async () => { const namespacedLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Namespaced lookup.', parameters: z.object({}), execute: async () => 'namespaced', }), ], })[0]!; const otherTool = tool({ name: 'other_tool', description: 'Other tool.', parameters: z.object({}), execute: async () => 'other', }); const otherCall: protocol.FunctionCallItem = { type: 'function_call', name: 'other_tool', callId: 'merge_other_call', status: 'completed', arguments: '{}', }; const agent = new Agent({ name: 'Inactive merge approval owner', tools: [namespacedLookup, otherTool], }); const state = new RunState(new RunContext(), 'input', agent, 2); state._lastProcessedResponse = { newItems: [], functions: [ { toolCall: otherCall, tool: otherTool as any, availableFunctionTools: [namespacedLookup, otherTool] as any, }, ], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['other_tool'], hasToolsOrApprovalsToRun: () => true, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; serialized.context.approvals = { 'crm.lookup': { approved: true, rejected: [] }, }; const overrideContext = new RunContext(); overrideContext.rejectTool( new ToolApprovalItem( { type: 'shell_call', callId: 'shell_crm_lookup', status: 'completed', action: { commands: ['echo rejected'] }, }, agent, 'crm.lookup', ), { alwaysReject: true }, ); const restored = await RunState.fromStringWithContext( agent, JSON.stringify(serialized), overrideContext, { contextStrategy: 'merge' }, ); const canonicalKey = getFunctionToolStateKey(namespacedLookup)!; expect( restored._context.isToolApproved({ toolName: canonicalKey, callId: 'future_function_call', }), ).toBe(true); expect( restored._context.isToolApproved({ toolName: 'crm.lookup', callId: 'future_shell_call', functionTool: false, }), ).toBe(false); }); it('keeps ambiguous legacy function fallback separate from an exact non-function override', async () => { const dottedLookup = tool({ name: 'crm_lookup', description: 'Dotted lookup.', parameters: z.object({}), execute: async () => 'dotted', }); dottedLookup.name = 'crm.lookup'; const namespacedLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Namespaced lookup.', parameters: z.object({}), execute: async () => 'namespaced', }), ], })[0]!; const callId = 'ambiguous_merge_call'; const toolCall: protocol.FunctionCallItem = { type: 'function_call', name: 'lookup', namespace: 'crm', callId, status: 'completed', arguments: '{}', }; const agent = new Agent({ name: 'Ambiguous legacy fallback merge', tools: [dottedLookup, namespacedLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall, tool: namespacedLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['crm.lookup'], hasToolsOrApprovalsToRun: () => true, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; serialized.context.approvals = { 'crm.lookup': { approved: false, rejected: true, stickyRejectMessage: 'Legacy rejection', }, }; const overrideContext = new RunContext(); overrideContext.approveTool( new ToolApprovalItem( { type: 'shell_call', callId: 'shell_override_call', status: 'completed', action: { commands: ['echo approved'] }, }, agent, 'crm.lookup', ), { alwaysApprove: true }, ); const restored = await RunState.fromStringWithContext( agent, JSON.stringify(serialized), overrideContext, { contextStrategy: 'merge' }, ); const namespacedKey = getFunctionToolStateKey(namespacedLookup)!; const dottedKey = getFunctionToolStateKey(dottedLookup)!; for (const [toolName, checkedCallId] of [ [namespacedKey, callId], [namespacedKey, 'future_namespaced_call'], [dottedKey, 'future_dotted_call'], ] as const) { expect( restored._context.isToolApproved({ toolName, callId: checkedCallId, functionTool: false, agent, }), ).toBe(false); } expect( restored._context.isToolApproved({ toolName: 'crm.lookup', callId: 'future_shell_call', functionTool: false, }), ).toBe(true); const durable = restored.toJSON() as any; expect(durable.context.approvals['crm.lookup'].approved).toBe(true); expect( durable.context.legacyFunctionApprovals['crm.lookup'].rejected, ).toBe(true); const roundTripped = await RunState.fromString( agent, restored.toString(), ); expect( roundTripped._context.isToolApproved({ toolName: dottedKey, callId: 'round_trip_function_call', functionTool: false, agent, }), ).toBe(false); expect( roundTripped._context.isToolApproved({ toolName: 'crm.lookup', callId: 'round_trip_shell_call', functionTool: false, }), ).toBe(true); }); it('keeps schema 1.15 approval ownership when merging a canonical override context', async () => { const decisions = [ { name: 'per-call approval', record: (callId: string) => ({ approved: [callId], rejected: [], }), expected: true, }, { name: 'permanent rejection', record: (callId: string) => ({ approved: false, rejected: true, messages: { [callId]: 'Always rejected' }, stickyRejectMessage: 'Always rejected', }), expected: false, message: 'Always rejected', }, ]; for (const decision of decisions) { const dottedLookup = tool({ name: 'crm_lookup', description: 'Dotted lookup.', parameters: z.object({}), execute: async () => 'dotted', }); dottedLookup.name = 'crm.lookup'; const namespacedLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Namespaced lookup.', parameters: z.object({}), execute: async () => 'namespaced', }), ], })[0]!; const agent = new Agent({ name: `MergeApproval-${decision.name}`, tools: [dottedLookup, namespacedLookup], }); const callId = `namespaced-${decision.name.replace(/ /g, '-')}`; const namespacedCall: protocol.FunctionCallItem = { type: 'function_call', name: 'lookup', namespace: 'crm', callId, status: 'completed', arguments: '{}', }; const state = new RunState(new RunContext(), 'input', agent, 2); state._lastProcessedResponse = { newItems: [], functions: [ { toolCall: namespacedCall, tool: namespacedLookup as any }, ], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['crm.lookup'], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [new ToolApprovalItem(namespacedCall, agent)], }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; serialized.context.approvals = { 'crm.lookup': decision.record(callId), other: { approved: ['serialized-other-call'], rejected: [], }, }; delete serialized.currentStep.data.interruptions[0] .functionToolStateKey; const overrideContext = new RunContext(); const existingBareCall: protocol.FunctionCallItem = { type: 'function_call', name: 'crm.lookup', callId: 'existing-bare-call', status: 'completed', arguments: '{}', }; overrideContext.approveTool( new ToolApprovalItem( existingBareCall, agent, undefined, getFunctionToolStateKey(dottedLookup), ), ); overrideContext.approveTool( new ToolApprovalItem( { type: 'function_call', name: 'other', callId: 'override-other-call', status: 'completed', arguments: '{}', }, agent, ), ); overrideContext.approveTool( new ToolApprovalItem( { type: 'shell_call', callId: 'override-shell-call', status: 'completed', action: { commands: ['echo approved'] }, }, agent, 'crm.lookup', ), { alwaysApprove: true }, ); const restored = await RunState.fromStringWithContext( agent, JSON.stringify(serialized), overrideContext, { contextStrategy: 'merge' }, ); const namespacedKey = getFunctionToolStateKey(namespacedLookup)!; const bareKey = getFunctionToolStateKey(dottedLookup)!; const isPermanent = decision.name.startsWith('permanent'); expect( restored._context.isToolApproved({ toolName: namespacedKey, callId, }), decision.name, ).toBe(decision.expected); expect( restored._context.getRejectionMessage(namespacedKey, callId), ).toBe(decision.message); expect( restored._context.isToolApproved({ toolName: namespacedKey, callId: `${callId}-future`, }), ).toBe(isPermanent ? decision.expected : undefined); expect( restored._context.isToolApproved({ toolName: 'crm.lookup', callId, functionTool: false, }), ).toBe(true); expect( restored._context.getRejectionMessage('crm.lookup', callId, { functionTool: false, }), ).toBeUndefined(); expect( restored._context.isToolApproved({ toolName: bareKey, callId: 'existing-bare-call', agent, }), ).toBe(true); expect( restored._context.isToolApproved({ toolName: bareKey, callId: 'future-bare-call', agent, }), ).toBe(isPermanent ? decision.expected : undefined); expect( restored._context.isToolApproved({ toolName: 'other', callId: 'serialized-other-call', functionTool: false, }), ).toBe(true); expect( restored._context.isToolApproved({ toolName: '["bare","other"]', callId: 'override-other-call', }), ).toBe(true); expect( restored._context.isToolApproved({ toolName: '["bare","other"]', callId: 'serialized-other-call', }), ).toBeUndefined(); expect( restored._context.isToolApproved({ toolName: 'other', callId: 'override-other-call', functionTool: false, }), ).toBeUndefined(); } }); }); } export function registerRunStateToolSearchTests(): void { describe('RunState', () => { it('accepts schema version 1.6 payloads during deserialization', async () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent16' }); const state = new RunState(context, 'input1', agent, 2); state._modelResponses = [ { usage: new Usage(), output: [TEST_MODEL_MESSAGE], responseId: 'resp_16', requestId: 'req_16', }, ]; state._lastTurnResponse = state._modelResponses[0]; const jsonVersion = state.toJSON() as any; jsonVersion.$schemaVersion = '1.6'; const restored = await RunState.fromString( agent, JSON.stringify(jsonVersion), ); expect(restored._lastTurnResponse?.responseId).toBe('resp_16'); expect(restored._lastTurnResponse?.requestId).toBe('req_16'); }); it('rejects schema version 1.7 payloads with tool_search items during deserialization', async () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent18' }); const state = new RunState(context, 'input1', agent, 2); state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_17', callId: 'ts_call_17', status: 'completed', arguments: { paths: ['crm'], query: 'profile' }, } as any, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_17', callId: 'ts_call_17', status: 'completed', tools: [ { type: 'tool_reference', functionName: 'lookup_account', namespace: 'crm', }, ], } as any, agent, ), ); const jsonVersion = state.toJSON() as any; jsonVersion.$schemaVersion = '1.7'; await expect(() => RunState.fromString(agent, JSON.stringify(jsonVersion)), ).rejects.toThrow( 'Run state schema version 1.7 does not support tool_search items.', ); }); it('accepts schema versions with tool_search support during deserialization', async () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent18' }); const state = new RunState(context, 'input1', agent, 2); state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_18', callId: 'ts_call_18', status: 'completed', arguments: { paths: ['crm'], query: 'profile' }, } as any, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_18', callId: 'ts_call_18', status: 'completed', tools: [ { type: 'tool_reference', functionName: 'lookup_account', namespace: 'crm', }, ], } as any, agent, ), ); for (const $schemaVersion of ['1.8', '1.10', '1.11'] as const) { const jsonVersion = state.toJSON() as any; jsonVersion.$schemaVersion = $schemaVersion; const restored = await RunState.fromString( agent, JSON.stringify(jsonVersion), ); expect(restored._generatedItems[0]).toBeInstanceOf( RunToolSearchCallItem, ); expect(restored._generatedItems[1]).toBeInstanceOf( RunToolSearchOutputItem, ); } }); it('preserves raw tool_search call_id and execution fields through RunState serialization', async () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent18RawSearch' }); const state = new RunState(context, 'input1', agent, 2); state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_raw', call_id: 'call_ts_raw', execution: 'server', status: 'completed', arguments: { paths: ['crm'], query: 'profile' }, } as any, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_raw', call_id: 'call_ts_raw', execution: 'server', status: 'completed', tools: [ { type: 'tool_reference', functionName: 'lookup_account', namespace: 'crm', }, ], } as any, agent, ), ); const restored = await RunState.fromString(agent, state.toString()); const restoredCall = restored._generatedItems[0] as RunToolSearchCallItem; const restoredOutput = restored ._generatedItems[1] as RunToolSearchOutputItem; expect(restoredCall.rawItem).toMatchObject({ type: 'tool_search_call', call_id: 'call_ts_raw', execution: 'server', }); expect(restoredOutput.rawItem).toMatchObject({ type: 'tool_search_output', call_id: 'call_ts_raw', execution: 'server', }); }); it('rehydrates client tool_search outputs without explicit call_id by FIFO order', async () => { const runtimeLookup = tool({ name: 'lookup_runtime', description: 'Look up a runtime record.', parameters: z.object({}), execute: async () => 'runtime', }); const execute = vi.fn(async () => runtimeLookup); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'No call id tool-search restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_without_call_id', execution: 'client', status: 'completed', arguments: {}, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_without_call_id', execution: 'client', status: 'completed', tools: [ { type: 'function', name: 'lookup_runtime', description: 'Look up a runtime record.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], } as protocol.ToolSearchOutputItem, agent, ), ); const restored = await RunState.fromString(agent, state.toString()); expect(execute).toHaveBeenCalledOnce(); expect( await (restored.getToolSearchRuntimeTools(agent)[0] as any).invoke( new RunContext(), '{}', ), ).toBe('runtime'); }); it('skips rehydration for server tool_search outputs with concrete tool payloads', async () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent18ServerSearchConcrete' }); const state = new RunState(context, 'input1', agent, 2); state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_server_concrete', call_id: 'call_ts_server_concrete', execution: 'server', status: 'completed', arguments: { query: 'profile' }, } as any, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_server_concrete', call_id: 'call_ts_server_concrete', execution: 'server', status: 'completed', tools: [ { type: 'function', name: 'lookup_account', description: 'Look up an account.', strict: true, parameters: { type: 'object', properties: { customerId: { type: 'string', }, }, required: ['customerId'], additionalProperties: false, }, }, ], } as any, agent, ), ); const restored = await RunState.fromString(agent, state.toString()); expect(restored._generatedItems[0]).toBeInstanceOf(RunToolSearchCallItem); expect(restored._generatedItems[1]).toBeInstanceOf( RunToolSearchOutputItem, ); expect(restored.getToolSearchRuntimeTools(agent)).toEqual([]); }); it('rehydrates duplicate-name client tool_search calls by agent identity', async () => { const alphaLookup = tool({ name: 'lookup_alpha', description: 'Look up alpha data.', parameters: z.object({}).strict(), execute: async () => 'alpha', }); const betaLookup = tool({ name: 'lookup_beta', description: 'Look up beta data.', parameters: z.object({}).strict(), execute: async () => 'beta', }); let alphaExecuteCount = 0; let betaExecuteCount = 0; const alphaToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', parameters: { type: 'object', properties: {}, additionalProperties: false, }, }, }, async () => { alphaExecuteCount += 1; return alphaLookup; }, ); const betaToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', parameters: { type: 'object', properties: {}, additionalProperties: false, }, }, }, async () => { betaExecuteCount += 1; return betaLookup; }, ); const alphaAgent = new Agent({ name: 'SharedSkill', instructions: 'alpha agent', tools: [alphaToolSearch], }); const betaAgent = new Agent({ name: 'SharedSkill', instructions: 'beta agent', tools: [betaToolSearch], }); const root = new Agent({ name: 'Root', handoffs: [alphaAgent, betaAgent], }); const state = new RunState(new RunContext(), 'input1', root, 2); const sharedCallId = 'call_tool_search_shared'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_alpha', status: 'completed', arguments: {}, providerData: { call_id: sharedCallId, execution: 'client', }, } as protocol.ToolSearchCallItem, alphaAgent, ), new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_beta', status: 'completed', arguments: {}, providerData: { call_id: sharedCallId, execution: 'client', }, } as protocol.ToolSearchCallItem, betaAgent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_alpha', status: 'completed', tools: [ { type: 'function', name: 'lookup_alpha', description: 'Look up alpha data.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: sharedCallId, execution: 'client', }, } as protocol.ToolSearchOutputItem, alphaAgent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_beta', status: 'completed', tools: [ { type: 'function', name: 'lookup_beta', description: 'Look up beta data.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: sharedCallId, execution: 'client', }, } as protocol.ToolSearchOutputItem, betaAgent, ), ); const restored = await RunState.fromString(root, state.toString()); expect(alphaExecuteCount).toBe(1); expect(betaExecuteCount).toBe(1); expect(restored.getToolSearchRuntimeTools(alphaAgent)).toEqual([ alphaLookup, ]); expect(restored.getToolSearchRuntimeTools(betaAgent)).toEqual([ betaLookup, ]); }); it('uses the collision-filtered tool winner during tool-search rehydration', async () => { const firstDuplicate = tool({ name: 'duplicate', description: 'First duplicate.', parameters: z.object({}).strict(), execute: async () => 'first', }); const winningDuplicate = tool({ name: 'duplicate', description: 'Winning duplicate.', parameters: z.object({}).strict(), execute: async () => 'winner', }); const losingRuntimeTool = tool({ name: 'losing_runtime', description: 'Runtime tool selected from the unfiltered list.', parameters: z.object({}).strict(), execute: async () => 'losing runtime', }); const winningRuntimeTool = tool({ name: 'winning_runtime', description: 'Runtime tool selected from the filtered list.', parameters: z.object({}).strict(), execute: async () => 'winning runtime', }); const execute = vi.fn( async ({ availableTools }: { availableTools: Tool[] }) => { const selectedDuplicate = availableTools.find( (candidate) => candidate.type === 'function' && candidate.name === 'duplicate', ); return selectedDuplicate === winningDuplicate ? winningRuntimeTool : losingRuntimeTool; }, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, execute, ); const agent = new Agent({ name: 'Collision-filtered restore agent', tools: [firstDuplicate, winningDuplicate, clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_collision_filtered_restore'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_collision_filtered_restore', status: 'completed', arguments: { query: 'runtime' }, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_collision_filtered_restore', status: 'completed', tools: [ { type: 'function', name: 'winning_runtime', description: 'Runtime tool selected from the filtered list.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); const restored = await expectToolNameCollisionWarnings(1, () => RunState.fromString(agent, state.toString()), ); expect(execute).toHaveBeenCalledTimes(1); expect(execute.mock.calls[0]?.[0].availableTools).toEqual([ winningDuplicate, clientToolSearch, ]); expect(restored.getToolSearchRuntimeTools(agent)).toEqual([ winningRuntimeTool, ]); }); it('freezes collision-filtered capabilities across tool-search rehydration callbacks', async () => { type TestContext = { functionEnabled: boolean; handoffEnabled: boolean; }; const transferFunction = tool({ name: 'transfer', description: 'Function with the same name as a dynamic handoff.', parameters: z.object({}).strict(), isEnabled: ({ runContext }) => (runContext.context as TestContext).functionEnabled, execute: async () => 'function', }); const firstRuntimeTool = tool({ name: 'first_runtime', description: 'First runtime tool.', parameters: z.object({}).strict(), execute: async () => 'first runtime', }); const secondRuntimeTool = tool({ name: 'second_runtime', description: 'Second runtime tool.', parameters: z.object({}).strict(), execute: async () => 'second runtime', }); const availableToolSnapshots: Tool[][] = []; const execute = vi.fn( async ({ availableTools, runContext, }: { availableTools: Tool[]; runContext: RunContext; }) => { availableToolSnapshots.push(availableTools); if (availableToolSnapshots.length === 1) { runContext.context.functionEnabled = false; runContext.context.handoffEnabled = true; return firstRuntimeTool; } return secondRuntimeTool; }, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, execute, ); const target = new Agent({ name: 'Dynamic target' }); const agent = new Agent({ name: 'Frozen capability restore agent', tools: [transferFunction, clientToolSearch], handoffs: [ handoff(target, { toolNameOverride: 'transfer', isEnabled: ({ runContext }) => runContext.context.handoffEnabled, }), ], }); const state = new RunState( new RunContext({ functionEnabled: true, handoffEnabled: false }), 'input', agent, 2, ); const processedToolCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_frozen_capability_restore', callId: 'call_frozen_capability_restore', name: 'transfer', status: 'completed', arguments: '{}', }; state._lastProcessedResponse = { newItems: [], functions: [ { toolCall: processedToolCall, tool: transferFunction as any }, ], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['transfer'], hasToolsOrApprovalsToRun: () => true, }; const addSerializedToolSearchResult = ( callId: string, runtimeToolName: string, ) => { state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: `ts_${callId}`, status: 'completed', arguments: { query: runtimeToolName }, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent as Agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: `tso_${callId}`, status: 'completed', tools: [ { type: 'function', name: runtimeToolName, description: `${runtimeToolName} description.`, strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent as Agent, ), ); }; addSerializedToolSearchResult('call_first', 'first_runtime'); addSerializedToolSearchResult('call_second', 'second_runtime'); const restored = await RunState.fromString(agent, state.toString()); expect(execute).toHaveBeenCalledTimes(2); expect(availableToolSnapshots).toEqual([ [transferFunction, clientToolSearch], [transferFunction, clientToolSearch, firstRuntimeTool], ]); expect(restored.getToolSearchRuntimeTools(agent)).toEqual([ firstRuntimeTool, secondRuntimeTool, ]); expect(restored._lastProcessedResponse?.functions[0]?.tool).toBe( transferFunction, ); }); it.each(['bare', 'namespaced', 'deferred'] as const)( 'round-trips the final same-key %s runtime tool from distinct tool-search calls', async (kind) => { const availableToolNames: string[][] = []; const execute = vi.fn( async ({ availableTools, toolCall, }: { availableTools: Tool[]; toolCall: protocol.ToolSearchCallItem; }) => { availableToolNames.push(availableTools.map((entry) => entry.name)); const result = String( (toolCall.arguments as { result?: string }).result, ); const runtimeTool = tool({ name: 'lookup', description: `${result} lookup result.`, parameters: z.object({}).strict(), ...(kind === 'deferred' ? { deferLoading: true } : {}), execute: async () => result, }); return kind === 'namespaced' ? toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [runtimeTool], })[0] : runtimeTool; }, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, execute, ); const agent = new Agent({ name: 'Same-key round-trip agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const createToolSearchCall = ( id: string, result: string, ): protocol.ToolSearchCallItem => ({ type: 'tool_search_call', id: `ts_${id}`, status: 'completed', arguments: { result }, providerData: { call_id: id, execution: 'client' }, }) as protocol.ToolSearchCallItem; const response: ModelResponse = { output: [ createToolSearchCall('call_first_lookup', 'first'), createToolSearchCall('call_second_lookup', 'second'), ], usage: new Usage(), }; const processed = await processModelResponseAsync( response, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; const liveTools = state.getToolSearchRuntimeTools(agent); expect(liveTools).toHaveLength(1); expect(await (liveTools[0] as any).invoke(new RunContext(), '{}')).toBe( 'second', ); const restored = await RunState.fromString(agent, state.toString()); const restoredTools = restored.getToolSearchRuntimeTools(agent); expect(availableToolNames).toEqual([ ['tool_search'], ['tool_search', 'lookup'], ['tool_search'], ['tool_search', 'lookup'], ]); expect(restoredTools).toHaveLength(1); expect( await (restoredTools[0] as any).invoke(new RunContext(), '{}'), ).toBe('second'); }, ); it('round-trips interleaved handlers and callback inputs for repeated client tool_search call ids', async () => { const execute = vi.fn( async ({ availableTools, toolCall, }: { availableTools: Tool[]; toolCall: protocol.ToolSearchCallItem; }) => { const phase = String( (toolCall.arguments as { phase?: string }).phase, ); const hasFirstResult = availableTools.some( (availableTool) => availableTool.type === 'function' && availableTool.description === 'first lookup result.', ); const result = phase === 'final' && !hasFirstResult ? 'wrong' : phase; return tool({ name: result === 'wrong' ? 'wrong_lookup' : 'lookup', description: `${result} lookup result.`, parameters: z.object({}).strict(), needsApproval: true, execute: async () => result, }); }, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Repeated call id restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const sharedCallId = 'call_repeated_replacement'; const createToolSearchCall = ( id: string, phase: string, ): protocol.ToolSearchCallItem => ({ type: 'tool_search_call', id, status: 'completed', arguments: { phase }, providerData: { call_id: sharedCallId, execution: 'client' }, }) as protocol.ToolSearchCallItem; const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_repeated_replacement', callId: 'call_repeated_replacement_function', name: 'lookup', status: 'completed', arguments: '{}', }; const processed = await processModelResponseAsync( { output: [ createToolSearchCall('ts_repeated_first', 'first'), functionCall, createToolSearchCall('ts_repeated_final', 'final'), ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; expect( await (processed.functions[0]!.tool as any).invoke( new RunContext(), '{}', ), ).toBe('first'); expect(state.getToolSearchRuntimeTools(agent)).toHaveLength(1); expect( await (state.getToolSearchRuntimeTools(agent)[0] as any).invoke( new RunContext(), '{}', ), ).toBe('final'); const restored = await RunState.fromString(agent, state.toString()); expect(execute).toHaveBeenCalledTimes(4); expect(restored.getToolSearchRuntimeTools(agent)).toHaveLength(1); expect( await ( restored._lastProcessedResponse?.functions[0]!.tool as any ).invoke(new RunContext(), '{}'), ).toBe('first'); expect( await (restored.getToolSearchRuntimeTools(agent)[0] as any).invoke( new RunContext(), '{}', ), ).toBe('final'); await rehydrateProcessedResponseTools( agent, restored, restored.getToolSearchRuntimeTools(agent), ); expect( await ( restored._lastProcessedResponse?.functions[0]!.tool as any ).invoke(new RunContext(), '{}'), ).toBe('first'); }); it.each(['empty', 'disabled', 'different'] as const)( 'does not resurrect an older routed owner after a later call is replaced with %s', async (replacement) => { const firstLookup = tool({ name: 'lookup', description: 'First lookup owner.', parameters: z.object({}).strict(), execute: async () => 'first', }); const secondLookup = tool({ name: 'lookup', description: 'Second lookup owner.', parameters: z.object({}).strict(), execute: async () => 'second', }); const disabledLookup = tool({ name: 'lookup', description: 'Disabled final lookup.', parameters: z.object({}).strict(), isEnabled: false, execute: async () => 'disabled', }); const alternateLookup = tool({ name: 'alternate_lookup', description: 'Different final lookup.', parameters: z.object({}).strict(), execute: async () => 'alternate', }); const execute = vi.fn( async ({ toolCall }: { toolCall: protocol.ToolSearchCallItem }) => { const phase = String( (toolCall.arguments as { phase?: string }).phase, ); if (phase === 'first') { return firstLookup; } if (phase === 'second') { return secondLookup; } if (replacement === 'disabled') { return disabledLookup; } if (replacement === 'different') { return alternateLookup; } return []; }, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: `Routed owner ${replacement} agent`, tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const createCall = ( id: string, callId: string, phase: string, ): protocol.ToolSearchCallItem => ({ type: 'tool_search_call', id, status: 'completed', arguments: { phase }, providerData: { call_id: callId, execution: 'client' }, }) as protocol.ToolSearchCallItem; const processed = await processModelResponseAsync( { output: [ createCall('ts_owner_first', 'call_owner_first', 'first'), createCall('ts_owner_second', 'call_owner_second', 'second'), createCall('ts_owner_final', 'call_owner_second', 'final'), ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; const expectedTools = replacement === 'different' ? 1 : 0; expect(state.getToolSearchRuntimeTools(agent)).toHaveLength( expectedTools, ); if (replacement === 'different') { expect( await (state.getToolSearchRuntimeTools(agent)[0] as any).invoke( new RunContext(), '{}', ), ).toBe('alternate'); } const restored = await RunState.fromString(agent, state.toString()); expect(restored.getToolSearchRuntimeTools(agent)).toHaveLength( expectedTools, ); if (replacement === 'different') { expect( await (restored.getToolSearchRuntimeTools(agent)[0] as any).invoke( new RunContext(), '{}', ), ).toBe('alternate'); } }, ); it('keeps a later routed owner when an older call returned the same tool object', async () => { const sharedLookup = tool({ name: 'lookup', description: 'Shared lookup owner.', parameters: z.object({}).strict(), execute: async () => 'shared', }); const execute = vi.fn( async ({ toolCall }: { toolCall: protocol.ToolSearchCallItem }) => (toolCall.arguments as { phase?: string }).phase === 'empty' ? [] : sharedLookup, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Shared object routed owner', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const createSearchCall = ( id: string, callId: string, phase: string, ): protocol.ToolSearchCallItem => ({ type: 'tool_search_call', id, status: 'completed', arguments: { phase }, providerData: { call_id: callId, execution: 'client' }, }) as protocol.ToolSearchCallItem; const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_shared_object_owner', callId: 'call_shared_object_owner', name: 'lookup', status: 'completed', arguments: '{}', }; const processed = await processModelResponseAsync( { output: [ createSearchCall('ts_owner_a', 'owner_a', 'shared'), createSearchCall('ts_owner_b', 'owner_b', 'shared'), createSearchCall('ts_owner_a_empty', 'owner_a', 'empty'), functionCall, ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; expect(processed.functions[0]?.tool).toBe(sharedLookup); expect(state.getToolSearchRuntimeTools(agent)).toEqual([sharedLookup]); const restored = await RunState.fromString(agent, state.toString()); expect(restored._lastProcessedResponse?.functions[0]?.tool).toBe( sharedLookup, ); expect(restored.getToolSearchRuntimeTools(agent)).toEqual([sharedLookup]); }); it('restores the runtime handler bound before a later same-key replacement', async () => { const execute = vi.fn( async ({ toolCall }: { toolCall: protocol.ToolSearchCallItem }) => { const result = String( (toolCall.arguments as { result?: string }).result, ); return tool({ name: 'lookup', description: `${result} lookup result.`, parameters: z.object({}).strict(), needsApproval: true, execute: async () => result, }); }, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Interleaved replacement restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const firstSearchCall = { type: 'tool_search_call', id: 'ts_call_first_interleaved', status: 'completed', arguments: { result: 'first' }, providerData: { call_id: 'call_first_interleaved', execution: 'client', }, } as protocol.ToolSearchCallItem; const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_interleaved_lookup', callId: 'call_interleaved_lookup', name: 'lookup', status: 'completed', arguments: '{}', }; const secondSearchCall = { type: 'tool_search_call', id: 'ts_call_second_interleaved', status: 'completed', arguments: { result: 'second' }, providerData: { call_id: 'call_second_interleaved', execution: 'client', }, } as protocol.ToolSearchCallItem; const response: ModelResponse = { output: [firstSearchCall, functionCall, secondSearchCall], usage: new Usage(), }; const processed = await processModelResponseAsync( response, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; expect( await (processed.functions[0].tool as any).invoke( new RunContext(), '{}', ), ).toBe('first'); expect( await (state.getToolSearchRuntimeTools(agent)[0] as any).invoke( new RunContext(), '{}', ), ).toBe('second'); const restored = await RunState.fromString(agent, state.toString()); expect( await ( restored._lastProcessedResponse?.functions[0].tool as any ).invoke(new RunContext(), '{}'), ).toBe('first'); expect( await (restored.getToolSearchRuntimeTools(agent)[0] as any).invoke( new RunContext(), '{}', ), ).toBe('second'); }); it('round trips a flattened configured namespace call with its canonical approval key', async () => { const namespacedLookup = toolNamespace({ name: 'crm', description: 'CRM tools.', tools: [ tool({ name: 'lookup', description: 'Configured CRM lookup.', parameters: z.object({}).strict(), needsApproval: true, execute: async () => 'configured', }), ], })[0]; const agent = new Agent({ name: 'Flattened configured namespace restore agent', tools: [namespacedLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); const flattenedCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_flattened_configured_namespace', callId: 'call_flattened_configured_namespace', name: 'crm.lookup', status: 'completed', arguments: '{}', }; const processed = await processModelResponseAsync( { output: [flattenedCall], usage: new Usage() }, agent, [namespacedLookup], [], state, [], ); const normalizedCall = processed.functions[0]!.toolCall; const canonicalKey = getFunctionToolStateKey(namespacedLookup)!; state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( normalizedCall, agent, undefined, canonicalKey, ), ], }, }; expect(normalizedCall).toMatchObject({ name: 'lookup', namespace: 'crm', }); const restored = await RunState.fromString(agent, state.toString()); expect( restored._lastProcessedResponse?.functions[0]?.toolCall, ).toMatchObject({ name: 'lookup', namespace: 'crm' }); expect(restored.getInterruptions()[0]?.functionToolStateKey).toBe( canonicalKey, ); expect(restored.getInterruptions()[0]?.rawItem).toMatchObject({ name: 'lookup', namespace: 'crm', }); expect( await ( restored._lastProcessedResponse?.functions[0]?.tool as any ).invoke(new RunContext(), '{}'), ).toBe('configured'); }); it('round trips a flattened namespace call with its runtime tool-search handler', async () => { const execute = vi.fn(async () => toolNamespace({ name: 'crm', description: 'Runtime CRM tools.', tools: [ tool({ name: 'lookup', description: 'Runtime CRM lookup.', parameters: z.object({}).strict(), needsApproval: true, execute: async () => 'runtime', }), ], }), ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Flattened runtime namespace restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const flattenedCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_flattened_runtime_namespace', callId: 'call_flattened_runtime_namespace', name: 'crm.lookup', status: 'completed', arguments: '{}', }; const processed = await processModelResponseAsync( { output: [ { type: 'tool_search_call', id: 'ts_flattened_runtime_namespace', status: 'completed', arguments: {}, providerData: { call_id: 'search_flattened_runtime_namespace', execution: 'client', }, } as protocol.ToolSearchCallItem, flattenedCall, ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); const normalizedCall = processed.functions[0]!.toolCall; const runtimeTool = processed.functions[0]!.tool; const canonicalKey = getFunctionToolStateKey(runtimeTool)!; state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( normalizedCall, agent, undefined, canonicalKey, ), ], }, }; const restored = await RunState.fromString(agent, state.toString()); expect(execute).toHaveBeenCalledTimes(2); expect( restored._lastProcessedResponse?.functions[0]?.toolCall, ).toMatchObject({ name: 'lookup', namespace: 'crm' }); expect(restored.getInterruptions()[0]?.functionToolStateKey).toBe( canonicalKey, ); expect( await ( restored._lastProcessedResponse?.functions[0]?.tool as any ).invoke(new RunContext(), '{}'), ).toBe('runtime'); const restoredProcessedTool = restored._lastProcessedResponse?.functions[0]?.tool; await rehydrateProcessedResponseTools(agent, restored, []); expect(restored._lastProcessedResponse?.functions[0]?.tool).toBe( restoredProcessedTool, ); expect( await ( restored._lastProcessedResponse?.functions[0]?.tool as any ).invoke(new RunContext(), '{}'), ).toBe('runtime'); }); it.each([CURRENT_SCHEMA_VERSION, '1.15'] as const)( 'round trips a deferred runtime tool selected by a legacy bare call in schema %s', async (schemaVersion) => { const deferredLookup = tool({ name: 'lookup', description: 'Deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, needsApproval: true, execute: async () => 'deferred', }); const execute = vi.fn(async () => deferredLookup); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: `Deferred bare restore ${schemaVersion}`, tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: `fc_deferred_bare_${schemaVersion}`, callId: `call_deferred_bare_${schemaVersion}`, name: 'lookup', status: 'completed', arguments: '{}', }; const processed = await processModelResponseAsync( { output: [ { type: 'tool_search_call', id: `ts_call_deferred_bare_${schemaVersion}`, status: 'completed', arguments: {}, providerData: { call_id: `search_deferred_bare_${schemaVersion}`, execution: 'client', }, } as protocol.ToolSearchCallItem, functionCall, ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( functionCall, agent, undefined, getFunctionToolStateKey(deferredLookup), ), ], }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = schemaVersion; if (schemaVersion === '1.15') { delete serialized.currentStep.data.interruptions[0] .functionToolStateKey; } const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(execute).toHaveBeenCalledTimes(2); expect(restored.getInterruptions()[0]?.functionToolStateKey).toBe( getFunctionToolStateKey(deferredLookup), ); expect( await ( restored._lastProcessedResponse?.functions[0].tool as any ).invoke(new RunContext(), '{}'), ).toBe('deferred'); }, ); it('rejects an unproven deferred bare fallback instead of rebinding a bare owner', async () => { const bareLookup = tool({ name: 'lookup', description: 'Bare lookup.', parameters: z.object({}).strict(), execute: async () => 'bare', }); const deferredLookup = tool({ name: 'lookup', description: 'Unproven deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'deferred', }); const agent = new Agent({ name: 'Unproven deferred fallback agent', tools: [bareLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_unproven_deferred_fallback', callId: 'call_unproven_deferred_fallback', name: 'lookup', status: 'completed', arguments: '{}', }; state._generatedItems.push(new RunToolCallItem(functionCall, agent)); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall: functionCall, tool: deferredLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['lookup'], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( functionCall, agent, undefined, getFunctionToolStateKey(deferredLookup), ), ], }, }; await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'function call ID is associated with multiple routed tool identities', ); }); it('round trips a legacy bare call resolved by a configured deferred owner', async () => { const deferredLookup = tool({ name: 'lookup', description: 'Configured deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'deferred', }); const agent = new Agent({ name: 'Configured deferred fallback agent', tools: [deferredLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_configured_deferred_fallback', callId: 'call_configured_deferred_fallback', name: 'lookup', status: 'completed', arguments: '{}', }; state._generatedItems.push(new RunToolCallItem(functionCall, agent)); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall: functionCall, tool: deferredLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['lookup'], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( functionCall, agent, undefined, getFunctionToolStateKey(deferredLookup), ), ], }, }; const restored = await RunState.fromString(agent, state.toString()); expect(restored.getInterruptions()[0]?.functionToolStateKey).toBe( getFunctionToolStateKey(deferredLookup), ); expect( await ( restored._lastProcessedResponse?.functions[0].tool as any ).invoke(new RunContext(), '{}'), ).toBe('deferred'); }); it.each([CURRENT_SCHEMA_VERSION, '1.15'] as const)( 'rejects a configured deferred bare fallback hidden by a handoff in schema %s', async (schemaVersion) => { const deferredLookup = tool({ name: 'lookup', description: 'Configured deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'deferred', }); const target = new Agent({ name: 'Deferred lookup target' }); const agent = new Agent({ name: `Configured deferred handoff collision ${schemaVersion}`, tools: [deferredLookup], handoffs: [handoff(target, { toolNameOverride: 'lookup' })], }); const state = new RunState(new RunContext(), 'input', agent, 2); const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: `fc_configured_deferred_handoff_${schemaVersion}`, callId: `call_configured_deferred_handoff_${schemaVersion}`, name: 'lookup', status: 'completed', arguments: '{}', }; state._generatedItems.push(new RunToolCallItem(functionCall, agent)); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall: functionCall, tool: deferredLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['lookup'], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( functionCall, agent, undefined, getFunctionToolStateKey(deferredLookup), ), ], }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = schemaVersion; if (schemaVersion === '1.15') { delete serialized.currentStep.data.interruptions[0] .functionToolStateKey; } await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'function call ID is associated with multiple routed tool identities', ); }, ); it('scopes repeated function call ids to each interruption agent', async () => { const outerTool = tool({ name: 'outer_tool', description: 'Outer tool.', parameters: z.object({}).strict(), execute: async () => 'outer', }); const childTool = tool({ name: 'child_tool', description: 'Child tool.', parameters: z.object({}).strict(), execute: async () => 'child', }); const child = new Agent({ name: 'Call id child', tools: [childTool] }); const root = new Agent({ name: 'Call id root', tools: [outerTool], handoffs: [child], }); const state = new RunState(new RunContext(), 'input', root, 2); const sharedCallId = 'agent_scoped_call_id'; const outerCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_outer_agent_scoped', callId: sharedCallId, name: 'outer_tool', status: 'completed', arguments: '{}', }; const childCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_child_agent_scoped', callId: sharedCallId, name: 'child_tool', status: 'completed', arguments: '{}', }; state._generatedItems.push( new RunToolCallItem(outerCall, root), new RunToolCallItem(childCall, child), ); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall: outerCall, tool: outerTool as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['outer_tool'], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( childCall, child, undefined, getFunctionToolStateKey(childTool), ), ], }, }; const restored = await RunState.fromString(root, state.toString()); expect(restored.getInterruptions()[0]?.agent).toBe(child); expect(restored.getInterruptions()[0]?.functionToolStateKey).toBe( getFunctionToolStateKey(childTool), ); }); it('preserves independent function approvals with the same identity across agents', async () => { const rootTool = tool({ name: 'shared_tool', description: 'Root shared tool.', parameters: z.object({}).strict(), execute: async () => 'root', }); const childTool = tool({ name: 'shared_tool', description: 'Child shared tool.', parameters: z.object({}).strict(), execute: async () => 'child', }); const child = new Agent({ name: 'Shared approval agent', tools: [childTool], }); const root = new Agent({ name: 'Shared approval agent', tools: [rootTool], handoffs: [child], }); const sharedCallId = 'shared_approval_call_id'; const rootCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_shared_approval_root', callId: sharedCallId, name: 'shared_tool', status: 'completed', arguments: '{}', }; const childCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_shared_approval_child', callId: sharedCallId, name: 'shared_tool', status: 'completed', arguments: '{}', }; const state = new RunState(new RunContext(), 'input', root, 2); state._generatedItems.push( new RunToolCallItem(rootCall, root), new RunToolCallItem(childCall, child), ); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall: rootCall, tool: rootTool as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['shared_tool'], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( rootCall, root, undefined, getFunctionToolStateKey(rootTool), ), new ToolApprovalItem( childCall, child, undefined, getFunctionToolStateKey(childTool), ), ], }, }; const sharedStateKey = getFunctionToolStateKey(rootTool)!; const legacySerialized = state.toJSON() as any; legacySerialized.$schemaVersion = '1.15'; legacySerialized.context.approvals = { shared_tool: { approved: [sharedCallId], rejected: [] }, }; delete legacySerialized.context.functionApprovals; for (const interruption of legacySerialized.currentStep.data .interruptions) { delete interruption.functionToolStateKey; } const legacyRestored = await RunState.fromString( root, JSON.stringify(legacySerialized), ); for (const owner of [root, child]) { expect( legacyRestored._context.isToolApproved({ toolName: sharedStateKey, callId: sharedCallId, functionTool: false, agent: owner, }), ).toBe(true); } const legacyRoundTripped = await RunState.fromString( root, legacyRestored.toString(), ); for (const owner of [root, child]) { expect( legacyRoundTripped._context.isToolApproved({ toolName: sharedStateKey, callId: sharedCallId, functionTool: false, agent: owner, }), ).toBe(true); } const restored = await RunState.fromString(root, state.toString()); const [rootApproval, childApproval] = restored.getInterruptions(); restored.approve(rootApproval, { alwaysApprove: true }); restored.reject(childApproval, { message: 'Child call rejected.' }); const roundTripped = await RunState.fromString(root, restored.toString()); expect( roundTripped._context.isToolApproved({ toolName: sharedStateKey, callId: 'future-root-call', functionTool: false, agent: root, }), ).toBe(true); expect( roundTripped._context.isToolApproved({ toolName: sharedStateKey, callId: sharedCallId, functionTool: false, agent: child, }), ).toBe(false); expect( roundTripped._context.isToolApproved({ toolName: sharedStateKey, callId: 'future-child-call', functionTool: false, agent: child, }), ).toBeUndefined(); expect( roundTripped._context._getFunctionRejectionMessage( sharedStateKey, sharedCallId, child, ), ).toBe('Child call rejected.'); expect( roundTripped._context._getFunctionRejectionMessage( sharedStateKey, sharedCallId, root, ), ).toBeUndefined(); }); it('round trips function approvals for reserved agent identity names', async () => { const agent = new Agent({ name: '__proto__' }); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: 'reserved_identity_tool', callId: 'reserved_identity_call', status: 'completed', arguments: '{}', }; const state = new RunState(new RunContext(), 'input', agent, 1); state.reject(new ToolApprovalItem(rawItem, agent), { message: 'Reserved identity rejected.', }); const serialized = state.toJSON(); expect(serialized.context.functionApprovals).toEqual([ { agentIdentity: '__proto__', approvals: expect.any(Object), }, ]); const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const stateKey = getFunctionToolStateKeyForCall(rawItem)!; expect( restored._context.isToolApproved({ toolName: stateKey, callId: rawItem.callId, functionTool: false, agent, }), ).toBe(false); expect( restored._context._getFunctionRejectionMessage( stateKey, rawItem.callId, agent, ), ).toBe('Reserved identity rejected.'); }); it('validates current function approval state before context replacement', async () => { const agent = new Agent({ name: 'ApprovalOwnerValidationAgent' }); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: 'approval_owner_tool', callId: 'approval_owner_call', status: 'completed', arguments: '{}', }; const state = new RunState(new RunContext(), 'input', agent, 1); state.approve(new ToolApprovalItem(rawItem, agent)); const serialized = state.toJSON() as any; serialized.context.functionApprovals[0].agentIdentity = 'unknown-agent'; await expect( RunState.fromStringWithContext( agent, JSON.stringify(serialized), new RunContext(), { contextStrategy: 'replace' }, ), ).rejects.toBeInstanceOf(UserError); }); it('rejects duplicate function approval owners before tool execution', async () => { const execute = vi.fn(async () => 'executed'); const approvalTool = tool({ name: 'duplicate_owner_tool', description: 'Duplicate owner tool.', parameters: z.object({}), execute, }); const agent = new Agent({ name: 'DuplicateApprovalOwnerAgent', tools: [approvalTool], }); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: approvalTool.name, callId: 'duplicate_owner_call', status: 'completed', arguments: '{}', }; const state = new RunState(new RunContext(), 'input', agent, 1); state.approve(new ToolApprovalItem(rawItem, agent)); const serialized = state.toJSON() as any; serialized.context.functionApprovals.push({ ...serialized.context.functionApprovals[0], approvals: { ...serialized.context.functionApprovals[0].approvals, }, }); await expect( RunState.fromStringWithContext( agent, JSON.stringify(serialized), new RunContext(), { contextStrategy: 'replace' }, ), ).rejects.toBeInstanceOf(UserError); expect(execute).not.toHaveBeenCalled(); }); it('rejects malformed current function approval state with UserError', async () => { const agent = new Agent({ name: 'MalformedApprovalOwnerAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); const serialized = state.toJSON() as any; serialized.context.functionApprovals = [ { agentIdentity: agent.name, approvals: { malformed: { approved: 'yes', rejected: [] }, }, }, ]; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toBeInstanceOf(UserError); }); it('rejects noncanonical current function approval keys with UserError', async () => { const agent = new Agent({ name: 'NoncanonicalApprovalKeyAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); const serialized = state.toJSON() as any; serialized.context.functionApprovals = [ { agentIdentity: agent.name, approvals: { lookup: { approved: true, rejected: [] }, }, }, ]; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toBeInstanceOf(UserError); }); it('preserves ZodError for unrelated malformed run state fields', async () => { const agent = new Agent({ name: 'MalformedUnrelatedStateAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); const serialized = state.toJSON() as any; serialized.currentTurn = 'not-a-number'; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toBeInstanceOf(ZodError); }); it('rejects owner-scoped approval fields in legacy schemas', async () => { const agent = new Agent({ name: 'LegacyApprovalOwnerFieldAgent' }); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: 'legacy_owner_tool', callId: 'legacy_owner_call', status: 'completed', arguments: '{}', }; const state = new RunState(new RunContext(), 'input', agent, 1); state.approve(new ToolApprovalItem(rawItem, agent)); const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state schema version 1.15 does not support owner-scoped function approvals.', ); }); it('rejects interruption identity mismatches before callbacks without a processed response', async () => { const dangerous = tool({ name: 'dangerous', description: 'Dangerous action.', parameters: z.object({}).strict(), execute: async () => 'dangerous', }); const harmless = tool({ name: 'harmless', description: 'Harmless action.', parameters: z.object({}).strict(), execute: async () => 'harmless', }); const runtimeLookup = tool({ name: 'runtime_lookup', description: 'Runtime lookup.', parameters: z.object({}).strict(), execute: async () => 'runtime', }); const execute = vi.fn(async () => runtimeLookup); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'No processed response interruption agent', tools: [dangerous, harmless, clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const rawItem: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_no_processed_response', callId: 'call_no_processed_response', name: 'dangerous', status: 'completed', arguments: '{}', }; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_no_processed_response', status: 'completed', arguments: {}, providerData: { call_id: 'search_no_processed_response', execution: 'client', }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_no_processed_response', status: 'completed', tools: [ { type: 'function', name: 'runtime_lookup', description: 'Runtime lookup.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: 'search_no_processed_response', execution: 'client', }, } as protocol.ToolSearchOutputItem, agent, ), new RunToolCallItem(rawItem, agent), ); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem( rawItem, agent, undefined, getFunctionToolStateKey(harmless), ), ], }, }; await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'function call ID is associated with multiple routed tool identities', ); expect(execute).not.toHaveBeenCalled(); }); it('rejects reused function call ids before tool-search rehydration callbacks', async () => { const runtimeLookup = tool({ name: 'runtime_lookup', description: 'Runtime lookup.', parameters: z.object({}).strict(), execute: async () => 'runtime', }); const configuredLookup = tool({ name: 'configured_lookup', description: 'Configured lookup.', parameters: z.object({}).strict(), execute: async () => 'configured', }); const execute = vi.fn(async () => runtimeLookup); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Reused function call id agent', tools: [configuredLookup, clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const sharedCallId = 'reused_function_call_id'; const runtimeCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_runtime_lookup', callId: sharedCallId, name: 'runtime_lookup', status: 'completed', arguments: '{}', }; const configuredCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_configured_lookup', callId: sharedCallId, name: 'configured_lookup', status: 'completed', arguments: '{}', }; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_reused_function_id', status: 'completed', arguments: {}, providerData: { call_id: 'tool_search_reused_function_id', execution: 'client', }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_reused_function_id', status: 'completed', tools: [ { type: 'function', name: 'runtime_lookup', description: 'Runtime lookup.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: 'tool_search_reused_function_id', execution: 'client', }, } as protocol.ToolSearchOutputItem, agent, ), new RunToolCallItem(runtimeCall, agent), new RunToolCallItem(configuredCall, agent), ); state._lastProcessedResponse = { newItems: [], functions: [ { toolCall: configuredCall, tool: configuredLookup as any }, ], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['configured_lookup'], hasToolsOrApprovalsToRun: () => true, }; await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'function call ID is associated with multiple routed tool identities', ); expect(execute).not.toHaveBeenCalled(); }); it('rejects reused function call ids before legacy state migration', async () => { const bareLookup = tool({ name: 'lookup', description: 'Bare lookup.', parameters: z.object({}).strict(), execute: async () => 'bare', }); const deferredLookup = tool({ name: 'lookup', description: 'Deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'deferred', }); const agent = new Agent({ name: 'Legacy reused function call id agent', tools: [bareLookup, deferredLookup], }); const state = new RunState(new RunContext(), 'input', agent, 2); const sharedCallId = 'legacy_reused_function_call_id'; const bareCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_legacy_bare_lookup', callId: sharedCallId, name: 'lookup', status: 'completed', arguments: '{}', }; const deferredCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_legacy_deferred_lookup', callId: sharedCallId, name: 'lookup', namespace: 'lookup', status: 'completed', arguments: '{}', }; state._lastProcessedResponse = { newItems: [], functions: [ { toolCall: bareCall, tool: bareLookup as any }, { toolCall: deferredCall, tool: deferredLookup as any }, ], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['lookup'], hasToolsOrApprovalsToRun: () => true, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'function call ID is associated with multiple routed tool identities', ); }); it('rejects an interruption whose routed identity differs from its processed call', async () => { const dangerous = tool({ name: 'dangerous', description: 'Dangerous action.', parameters: z.object({}).strict(), execute: async () => 'dangerous', }); const harmless = tool({ name: 'harmless', description: 'Harmless action.', parameters: z.object({}).strict(), execute: async () => 'harmless', }); const agent = new Agent({ name: 'Mismatched interruption identity agent', tools: [dangerous, harmless], }); const state = new RunState(new RunContext(), 'input', agent, 2); const rawItem: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_dangerous', callId: 'mismatched_interruption_identity', name: 'dangerous', status: 'completed', arguments: '{}', }; state._lastProcessedResponse = { newItems: [], functions: [{ toolCall: rawItem, tool: dangerous as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['dangerous'], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [new ToolApprovalItem(rawItem, agent)], }, }; const serialized = state.toJSON() as any; serialized.currentStep.data.interruptions[0].rawItem.name = 'harmless'; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'function call ID is associated with multiple routed tool identities', ); }); it('validates every tool-search output before invoking a rehydration callback', async () => { const runtimeTool = tool({ name: 'lookup', description: 'Lookup result.', parameters: z.object({}).strict(), execute: async () => 'lookup', }); const execute = vi.fn(async () => runtimeTool); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Tool-search preflight agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_preflight_valid', status: 'completed', arguments: {}, providerData: { call_id: 'call_preflight_valid', execution: 'client', }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_preflight_valid', status: 'completed', tools: [ { type: 'function', name: 'lookup', description: 'Lookup result.', strict: true, parameters: { type: 'object', properties: {}, required: [] }, }, ], providerData: { call_id: 'call_preflight_valid', execution: 'client', }, } as protocol.ToolSearchOutputItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_preflight_orphan', status: 'completed', tools: [ { type: 'function', name: 'orphan', description: 'Orphan result.', strict: true, parameters: { type: 'object', properties: {}, required: [] }, }, ], providerData: { call_id: 'call_preflight_orphan', execution: 'client', }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'serialized state is missing the matching tool_search call item', ); expect(execute).not.toHaveBeenCalled(); }); it('rejects a malformed serialized runtime identity before rehydration callbacks', async () => { const execute = vi.fn(); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Malformed tool-search identity agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_malformed_identity'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_malformed_identity', status: 'completed', arguments: {}, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_malformed_identity', status: 'completed', tools: [{ type: 'function' }], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow('function without a valid routed identity'); expect(execute).not.toHaveBeenCalled(); }); it('rejects a reserved serialized namespace before rehydration callbacks', async () => { const execute = vi.fn(async () => []); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Reserved serialized namespace agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_reserved_serialized_namespace'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_reserved_serialized_namespace', status: 'completed', arguments: {}, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_reserved_serialized_namespace', status: 'completed', tools: [ { type: 'namespace', name: 'lookup', description: 'Reserved lookup namespace.', tools: [ { type: 'function', name: 'lookup', description: 'Nested lookup.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'Responses tool search reserves same-name namespaces for deferred top-level function tools.', ); expect(execute).not.toHaveBeenCalled(); }); it('rejects a direct serialized self-namespace before rehydration callbacks', async () => { const runtimeLookup = tool({ name: 'lookup', description: 'Deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'lookup', }); const execute = vi.fn(async () => runtimeLookup); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Direct reserved serialized namespace agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_direct_reserved_serialized_namespace'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_direct_reserved_serialized_namespace', status: 'completed', arguments: {}, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_direct_reserved_serialized_namespace', status: 'completed', tools: [ { type: 'function', name: 'lookup', namespace: 'lookup', description: 'Reserved lookup namespace.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'Responses tool search reserves same-name namespaces for deferred top-level function tools.', ); expect(execute).not.toHaveBeenCalled(); }); it('replays an empty tool-search callback before later callbacks', async () => { type TestContext = { ready: boolean }; const execute = vi.fn( async ({ runContext, toolCall, }: { runContext: RunContext; toolCall: protocol.ToolSearchCallItem; }) => { const phase = (toolCall.arguments as { phase?: string }).phase; if (phase === 'empty') { runContext.context.ready = true; return []; } const name = runContext.context.ready ? 'after_empty' : 'wrong'; return tool({ name, description: 'Result after the empty callback.', parameters: z.object({}).strict(), execute: async () => name, }); }, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Empty callback replay agent', tools: [clientToolSearch], }); const state = new RunState( new RunContext({ ready: false }), 'input', agent, 2, ); const createCall = ( id: string, phase: string, ): protocol.ToolSearchCallItem => ({ type: 'tool_search_call', id: `ts_${id}`, status: 'completed', arguments: { phase }, providerData: { call_id: id, execution: 'client' }, }) as protocol.ToolSearchCallItem; const processed = await processModelResponseAsync( { output: [ createCall('call_empty_replay', 'empty'), createCall('call_after_empty_replay', 'after'), ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; const restored = await RunState.fromStringWithContext( agent, state.toString(), new RunContext({ ready: false }), { contextStrategy: 'replace' }, ); expect(execute).toHaveBeenCalledTimes(4); expect(restored.getToolSearchRuntimeTools(agent)[0]?.name).toBe( 'after_empty', ); }); it('rejects an empty custom tool-search output when its executor is unavailable', async () => { type TestContext = { ready: boolean }; const execute = vi.fn( async ({ runContext }: { runContext: RunContext }) => { runContext.context.ready = true; return []; }, ); const customToolSearchProviderData = { type: 'tool_search' as const, execution: 'client' as const, parameters: { type: 'object', properties: {}, additionalProperties: false, }, }; const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: customToolSearchProviderData, }, execute, ); const agent = new Agent({ name: 'Empty custom callback restore agent', tools: [clientToolSearch], }); const state = new RunState( new RunContext({ ready: false }), 'input', agent, 2, ); const processed = await processModelResponseAsync( { output: [ { type: 'tool_search_call', id: 'ts_call_empty_custom_restore', status: 'completed', arguments: {}, providerData: { call_id: 'call_empty_custom_restore', execution: 'client', }, } as protocol.ToolSearchCallItem, ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; const replacementAgent = new Agent({ name: agent.name, tools: [ { type: 'hosted_tool', name: 'tool_search', providerData: customToolSearchProviderData, }, ], }); const replacementContext = new RunContext({ ready: false }); await expect( RunState.fromStringWithContext( replacementAgent, state.toString(), replacementContext, { contextStrategy: 'replace' }, ), ).rejects.toThrow( 'require toolSearchTool({ execution: "client", execute }) when custom client tool_search parameters are provided', ); expect(execute).toHaveBeenCalledTimes(1); expect(replacementContext.context.ready).toBe(false); }); it('restores an empty built-in tool-search output without an executor', async () => { const clientToolSearch = { type: 'hosted_tool' as const, name: 'tool_search', providerData: { type: 'tool_search' as const, execution: 'client' as const, }, }; const agent = new Agent({ name: 'Empty built-in restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_empty_builtin_restore'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_empty_builtin_restore', status: 'completed', arguments: { paths: [] }, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_empty_builtin_restore', status: 'completed', tools: [], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); const restored = await RunState.fromString(agent, state.toString()); expect(restored.getToolSearchRuntimeTools(agent)).toEqual([]); }); it('keeps disabled callback results visible to later tool-search callbacks', async () => { type TestContext = { enabled: boolean }; const availableToolNames: string[][] = []; const execute = vi.fn( async ({ availableTools, toolCall, }: { availableTools: Tool[]; toolCall: protocol.ToolSearchCallItem; }) => { availableToolNames.push(availableTools.map((entry) => entry.name)); const phase = (toolCall.arguments as { phase?: string }).phase; if (phase === 'disabled') { return tool({ name: 'disabled_runtime', description: 'Disabled runtime result.', parameters: z.object({}).strict(), isEnabled: ({ runContext }) => (runContext.context as TestContext).enabled, execute: async () => 'disabled', }); } const sawDisabled = availableTools.some( (entry) => entry.name === 'disabled_runtime', ); const name = sawDisabled ? 'after_disabled' : 'wrong'; return tool({ name, description: 'Result after the disabled callback.', parameters: z.object({}).strict(), execute: async () => name, }); }, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Disabled callback replay agent', tools: [clientToolSearch], }); const state = new RunState( new RunContext({ enabled: false }), 'input', agent, 2, ); const createCall = ( id: string, phase: string, ): protocol.ToolSearchCallItem => ({ type: 'tool_search_call', id: `ts_${id}`, status: 'completed', arguments: { phase }, providerData: { call_id: id, execution: 'client' }, }) as protocol.ToolSearchCallItem; const processed = await processModelResponseAsync( { output: [ createCall('call_disabled_replay', 'disabled'), createCall('call_after_disabled_replay', 'after'), ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; const restored = await RunState.fromString(agent, state.toString()); expect(availableToolNames).toEqual([ ['tool_search'], ['tool_search', 'disabled_runtime'], ['tool_search'], ['tool_search', 'disabled_runtime'], ]); expect( restored.getToolSearchRuntimeTools(agent).map((entry) => entry.name), ).toEqual(['after_disabled']); }); it.each([ ['an enabled and a disabled result', true], ['two disabled results', false], ] as const)( 'rehydrates client tool-search output after filtering %s', async (_description, includeEnabledResult) => { const enabledLookup = tool({ name: 'lookup', description: 'Enabled lookup.', parameters: z.object({}).strict(), execute: async () => 'enabled', }); const disabledLookup = tool({ name: 'lookup', description: 'Disabled lookup.', parameters: z.object({}).strict(), isEnabled: false, execute: async () => 'disabled', }); const otherDisabledLookup = tool({ name: 'lookup', description: 'Other disabled lookup.', parameters: z.object({}).strict(), isEnabled: false, execute: async () => 'other disabled', }); const execute = vi.fn(async () => [ includeEnabledResult ? enabledLookup : otherDisabledLookup, disabledLookup, ]); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: `Filtered duplicate restore ${includeEnabledResult}`, tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const processed = await processModelResponseAsync( { output: [ { type: 'tool_search_call', id: 'ts_call_filtered_duplicate_restore', status: 'completed', arguments: {}, providerData: { call_id: 'call_filtered_duplicate_restore', execution: 'client', }, } as protocol.ToolSearchCallItem, ], usage: new Usage(), }, agent, [clientToolSearch], [], state, [], ); state._generatedItems.push(...processed.newItems); state._lastProcessedResponse = processed; const restored = await RunState.fromString(agent, state.toString()); expect(execute).toHaveBeenCalledTimes(2); expect(restored.getToolSearchRuntimeTools(agent)).toEqual( includeEnabledResult ? [enabledLookup] : [], ); }, ); it.each([ ['one serialized tool', true], ['an empty serialized result', false], ] as const)( 'rejects extra enabled callback tools when rehydrating %s', async (_description, includeSerializedLookup) => { const lookup = tool({ name: 'lookup', description: 'Serialized lookup.', parameters: z.object({}).strict(), execute: async () => 'lookup', }); const extraLookup = tool({ name: 'extra_lookup', description: 'Unexpected extra lookup.', parameters: z.object({}).strict(), execute: async () => 'extra', }); const execute = vi.fn(async () => [lookup, extraLookup]); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: `Extra enabled restore ${includeSerializedLookup}`, tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_extra_enabled_restore'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_extra_enabled_restore', status: 'completed', arguments: {}, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_extra_enabled_restore', status: 'completed', tools: includeSerializedLookup ? [ { type: 'function', name: 'lookup', description: 'Serialized lookup.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ] : [], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'registered execute callback returned different runtime tools than the serialized state', ); expect(execute).toHaveBeenCalledOnce(); }, ); it('rejects duplicate configured identities returned during tool-search rehydration', async () => { const configuredTool = tool({ name: 'configured', description: 'Configured tool.', parameters: z.object({}).strict(), execute: async () => 'configured', }); const runtimeTool = tool({ name: 'runtime', description: 'Runtime tool.', parameters: z.object({}).strict(), execute: async () => 'runtime', }); const execute = vi.fn(async () => [ configuredTool, configuredTool, runtimeTool, ]); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, execute, ); const agent = new Agent({ name: 'Configured duplicate restore agent', tools: [configuredTool, clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_configured_duplicate_restore'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_configured_duplicate_restore', status: 'completed', arguments: { query: 'runtime' }, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_configured_duplicate_restore', status: 'completed', tools: [ { type: 'function', name: 'configured', description: 'Configured tool.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, { type: 'function', name: 'runtime', description: 'Runtime tool.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'Client tool_search execute() returned multiple tools with the same routed identity.', ); expect(execute).toHaveBeenCalledTimes(1); }); it.each([ ['accepts the final replacement', true], ['rejects a stale first result', false], ] as const)( '%s when rehydrating repeated client tool_search outputs', async (_description, returnFinalReplacement) => { const firstLookup = tool({ name: 'first_lookup', description: 'First lookup result.', parameters: z.object({}).strict(), execute: async () => 'first', }); const latestLookup = tool({ name: 'latest_lookup', description: 'Latest lookup result.', parameters: z.object({}).strict(), execute: async () => 'latest', }); const execute = vi.fn(async () => returnFinalReplacement ? latestLookup : firstLookup, ); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, execute, ); const agent = new Agent({ name: 'Replacement restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_replacement_restore'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_replacement_restore', status: 'completed', arguments: { paths: [] }, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_replacement_first', status: 'completed', tools: [ { type: 'function', name: 'first_lookup', description: 'First lookup result.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_replacement_latest', status: 'completed', tools: [ { type: 'function', name: 'latest_lookup', description: 'Latest lookup result.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); const restoredPromise = RunState.fromString(agent, state.toString()); if (returnFinalReplacement) { const restored = await restoredPromise; expect(restored.getToolSearchRuntimeTools(agent)).toEqual([ latestLookup, ]); } else { await expect(restoredPromise).rejects.toThrow( 'RunState cannot resume custom client tool_search because the registered execute callback returned different runtime tools than the serialized state.', ); } expect(execute).toHaveBeenCalledTimes(1); }, ); it('redacts runtime tool identities from RunState callback mismatches', async () => { const expectedName = 'secret_expected_lookup'; const actualName = 'secret_actual_lookup'; const actualLookup = tool({ name: actualName, description: 'Actual lookup.', parameters: z.object({}).strict(), execute: async () => 'actual', }); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, async () => actualLookup, ); const agent = new Agent({ name: 'Redacted mismatch agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_redacted_mismatch'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_redacted_mismatch', status: 'completed', arguments: {}, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_redacted_mismatch', status: 'completed', tools: [ { type: 'function', name: expectedName, description: 'Expected lookup.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); const redaction = vi .spyOn(logger, 'dontLogToolData', 'get') .mockReturnValue(true); try { const restored = RunState.fromString(agent, state.toString()); await expect(restored).rejects.toThrow( 'RunState cannot resume custom client tool_search because the registered execute callback returned different runtime tools than the serialized state.', ); await expect(restored).rejects.not.toThrow(expectedName); await expect(restored).rejects.not.toThrow(actualName); } finally { redaction.mockRestore(); } }); it('redacts call and agent identities from missing tool-search executors', async () => { const callId = 'secret_missing_executor_call'; const agent = new Agent({ name: 'Secret missing executor agent' }); const state = new RunState(new RunContext(), 'input', agent, 2); state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_missing_executor', status: 'completed', arguments: {}, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_missing_executor', status: 'completed', tools: [ { type: 'function', name: 'secret_runtime_lookup', description: 'Secret runtime lookup.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); const redaction = vi .spyOn(logger, 'dontLogToolData', 'get') .mockReturnValue(true); try { const restored = RunState.fromString(agent, state.toString()); await expect(restored).rejects.toThrow( 'RunState cannot resume custom client tool_search because the agent no longer provides toolSearchTool({ execution: "client", execute }).', ); await expect(restored).rejects.not.toThrow(callId); await expect(restored).rejects.not.toThrow(agent.name); } finally { redaction.mockRestore(); } }); it('rehydrates same-name runtime tools from distinct function categories', async () => { const bareLookup = tool({ name: 'lookup', description: 'Immediate lookup.', parameters: z.object({}).strict(), execute: async () => 'bare', }); const deferredLookup = tool({ name: 'lookup', description: 'Deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'deferred', }); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, async () => [bareLookup, deferredLookup], ); const agent = new Agent({ name: 'Category-aware restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_tool_search_categories'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_categories', status: 'completed', arguments: { paths: [] }, providerData: { call_id: callId, execution: 'client', }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_categories', status: 'completed', tools: [ { type: 'function', name: 'lookup', description: 'Immediate lookup.', strict: true, deferLoading: false, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, { type: 'function', name: 'lookup', description: 'Deferred lookup.', strict: true, deferLoading: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client', }, } as protocol.ToolSearchOutputItem, agent, ), ); const restored = await RunState.fromString(agent, state.toString()); expect(restored.getToolSearchRuntimeTools(agent)).toEqual([ bareLookup, deferredLookup, ]); }); it('rejects a rehydrated bare runtime function that conflicts with an enabled handoff', async () => { const runtimeLookup = tool({ name: 'lookup', description: 'Look up a record.', parameters: z.object({}).strict(), execute: async () => 'runtime', }); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, async () => runtimeLookup, ); const target = new Agent({ name: 'LookupTarget' }); const agent = new Agent({ name: 'RuntimeHandoffCollision', tools: [clientToolSearch], handoffs: [handoff(target, { toolNameOverride: 'lookup' })], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_runtime_handoff_collision'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_runtime_handoff_collision', status: 'completed', arguments: { paths: [] }, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_runtime_handoff_collision', status: 'completed', tools: [ { type: 'function', name: 'lookup', description: 'Look up a record.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'Client tool_search execute() returned a bare function tool that conflicts with an available handoff. Assign a unique tool name or use a namespace.', ); }); it.each([false, true])( 'rejects duplicate routed identities returned during tool-search rehydration (same object: %s)', async (repeatSameObject) => { const first = tool({ name: 'lookup', description: 'First deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'first', }); const second = tool({ name: 'lookup', description: 'Second deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'second', }); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, async () => (repeatSameObject ? [first, first] : [first, second]), ); const agent = new Agent({ name: 'Duplicate restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const callId = 'call_duplicate_restore'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_duplicate_restore', status: 'completed', arguments: { paths: [] }, providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_duplicate_restore', status: 'completed', tools: [ { type: 'function', name: 'lookup', description: 'Deferred lookup.', strict: true, deferLoading: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: callId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'Client tool_search execute() returned multiple tools with the same routed identity.', ); }, ); it('rejects duplicate routed identities in serialized tool-search output before rehydration', async () => { const validRuntimeLookup = tool({ name: 'valid_lookup', description: 'Valid lookup.', parameters: z.object({}).strict(), execute: async () => 'valid', }); const duplicateRuntimeLookup = tool({ name: 'lookup', description: 'Deferred lookup.', parameters: z.object({}).strict(), deferLoading: true, execute: async () => 'lookup', }); const execute = vi.fn(async () => validRuntimeLookup); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, execute, ); const agent = new Agent({ name: 'Duplicate serialized restore agent', tools: [clientToolSearch], }); const state = new RunState(new RunContext(), 'input', agent, 2); const validCallId = 'call_valid_serialized_restore'; const duplicateCallId = 'call_duplicate_serialized_restore'; const validSerializedTool = { type: 'function', name: 'valid_lookup', description: 'Valid lookup.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, } as const; const serializedTool = { type: 'function', name: duplicateRuntimeLookup.name, description: duplicateRuntimeLookup.description, strict: true, deferLoading: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, } as const; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_valid_serialized_restore', status: 'completed', arguments: { query: 'valid' }, providerData: { call_id: validCallId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_valid_serialized_restore', status: 'completed', tools: [validSerializedTool], providerData: { call_id: validCallId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_duplicate_serialized_restore', status: 'completed', arguments: { query: 'duplicate' }, providerData: { call_id: duplicateCallId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_duplicate_serialized_restore', status: 'completed', tools: [serializedTool, serializedTool], providerData: { call_id: duplicateCallId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent, ), ); await expect( RunState.fromString(agent, state.toString()), ).rejects.toThrow( 'Serialized client tool_search output contains multiple tools with the same routed identity. Assign unique tool names or namespaces before resuming RunState.', ); expect(execute).not.toHaveBeenCalled(); }); it('accepts schema version 1.7 payloads when non-item context data mentions tool_search types', async () => { const context = new RunContext({ custom: { type: 'tool_search_output', note: 'This is plain context data, not a serialized run item.', }, }); const agent = new Agent({ name: 'Agent17' }); const state = new RunState(context, 'input1', agent, 2); const jsonVersion = state.toJSON() as any; jsonVersion.$schemaVersion = '1.7'; const restored = await RunState.fromString( agent, JSON.stringify(jsonVersion), ); expect((restored._context.context as any).custom).toEqual({ type: 'tool_search_output', note: 'This is plain context data, not a serialized run item.', }); }); }); } export function registerRunStateApprovalTests(): void { describe('RunState', () => { it('approve updates context approvals correctly', () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent2' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolX', callId: 'cid123', status: 'completed', arguments: 'arguments', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.approve(approvalItem); expect( state._context.isToolApproved({ toolName: 'toolX', callId: 'cid123' }), ).toBe(true); }); it('returns undefined when approval status is unknown', () => { const context = new RunContext(); expect( context.isToolApproved({ toolName: 'unknownTool', callId: 'cid999' }), ).toBeUndefined(); }); it('reject updates context approvals correctly', () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent3' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolY', callId: 'cid456', status: 'completed', arguments: 'arguments', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem); expect( state._context.isToolApproved({ toolName: 'toolY', callId: 'cid456' }), ).toBe(false); }); it('reject with message stores it and includes it in getRejectionMessage', () => { const context = new RunContext(); const agent = new Agent({ name: 'MsgAgent' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolMsg', callId: 'msg-1', status: 'completed', arguments: '{}', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem, { message: 'Not safe to run' }); expect( state._context.isToolApproved({ toolName: 'toolMsg', callId: 'msg-1' }), ).toBe(false); expect(state._context.getRejectionMessage('toolMsg', 'msg-1')).toBe( 'Not safe to run', ); }); it('serialization round-trip preserves rejection messages', async () => { const context = new RunContext(); const agent = new Agent({ name: 'SerializeMsgAgent' }); const state = new RunState(context, 'input', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolSer', callId: 'ser-1', status: 'completed', arguments: '{}', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem, { message: 'Denied for security' }); const restored = await RunState.fromString(agent, state.toString()); expect(restored._context.getRejectionMessage('toolSer', 'ser-1')).toBe( 'Denied for security', ); expect( restored._context.isToolApproved({ toolName: 'toolSer', callId: 'ser-1', }), ).toBe(false); }); it('restores pre-1.7 run states without rejection messages', async () => { const context = new RunContext(); const agent = new Agent({ name: 'LegacyMsgAgent' }); const state = new RunState(context, 'input', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolLegacy', callId: 'legacy-1', status: 'completed', arguments: '{}', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem, { message: 'Denied for security' }); const serialized = JSON.parse(state.toString()); serialized.$schemaVersion = '1.6'; const approvalKey = getFunctionToolStateKeyForCall(rawItem)!; const ownerApprovals = serialized.context.functionApprovals.find( (entry: any) => entry.agentIdentity === agent.name, ).approvals; serialized.context.approvals.toolLegacy = ownerApprovals[approvalKey]; delete serialized.context.functionApprovals; delete serialized.context.approvals.toolLegacy.messages; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context.isToolApproved({ toolName: 'toolLegacy', callId: 'legacy-1', }), ).toBe(false); expect( restored._context.getRejectionMessage('toolLegacy', 'legacy-1'), ).toBeUndefined(); }); it('per-callId messages: two rejections can have different messages', () => { const context = new RunContext(); const agent = new Agent({ name: 'PerCallMsgAgent' }); const state = new RunState(context, '', agent, 1); const rawItem1: protocol.ToolCallItem = { type: 'function_call', name: 'sharedTool', callId: 'call-a', status: 'completed', arguments: '{}', }; const rawItem2: protocol.ToolCallItem = { type: 'function_call', name: 'sharedTool', callId: 'call-b', status: 'completed', arguments: '{}', }; state.reject(new ToolApprovalItem(rawItem1, agent), { message: 'Reason A', }); state.reject(new ToolApprovalItem(rawItem2, agent), { message: 'Reason B', }); expect(state._context.getRejectionMessage('sharedTool', 'call-a')).toBe( 'Reason A', ); expect(state._context.getRejectionMessage('sharedTool', 'call-b')).toBe( 'Reason B', ); }); it('reject with empty message preserves the empty string', () => { const context = new RunContext(); const agent = new Agent({ name: 'EmptyMsgAgent' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolEmpty', callId: 'empty-1', status: 'completed', arguments: '{}', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem, { message: '' }); expect(state._context.getRejectionMessage('toolEmpty', 'empty-1')).toBe( '', ); }); it('reject permanently when alwaysReject option is passed', () => { const context = new RunContext(); const agent = new Agent({ name: 'Agent4' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolZ', callId: 'cid789', status: 'completed', arguments: 'arguments', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem, { alwaysReject: true }); expect( state._context.isToolApproved({ toolName: 'toolZ', callId: 'cid789' }), ).toBe(false); const approvals = state._context.toJSON().approvals; expect(approvals.toolZ.approved).toBe(false); expect(approvals.toolZ.rejected).toBe(true); }); it('alwaysReject with message stores call-specific and sticky rejection messages', () => { const context = new RunContext(); const agent = new Agent({ name: 'AlwaysRejectMsgAgent' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolAR', callId: 'ar-1', status: 'completed', arguments: '{}', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem, { alwaysReject: true, message: 'Blocked by policy', }); expect( state._context.isToolApproved({ toolName: 'toolAR', callId: 'ar-1' }), ).toBe(false); expect(state._context.getRejectionMessage('toolAR', 'ar-1')).toBe( 'Blocked by policy', ); expect(state._context.getRejectionMessage('toolAR', 'ar-2')).toBe( 'Blocked by policy', ); const approvals = state._context.toJSON().approvals; expect(approvals.toolAR.rejected).toBe(true); expect(approvals.toolAR.messages).toEqual({ 'ar-1': 'Blocked by policy', }); expect(approvals.toolAR.stickyRejectMessage).toBe('Blocked by policy'); }); it('alwaysReject with empty message preserves the empty string', () => { const context = new RunContext(); const agent = new Agent({ name: 'AlwaysRejectEmptyMsgAgent' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolAREmpty', callId: 'ar-empty-1', status: 'completed', arguments: '{}', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem, { alwaysReject: true, message: '', }); expect( state._context.getRejectionMessage('toolAREmpty', 'ar-empty-1'), ).toBe(''); expect( state._context.getRejectionMessage('toolAREmpty', 'ar-empty-2'), ).toBe(''); }); it('serialization round-trip preserves alwaysReject sticky rejection messages', async () => { const context = new RunContext(); const agent = new Agent({ name: 'AlwaysRejectSerializeMsgAgent' }); const state = new RunState(context, 'input', agent, 1); const rawItem: protocol.ToolCallItem = { type: 'function_call', name: 'toolSticky', callId: 'sticky-1', status: 'completed', arguments: '{}', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.reject(approvalItem, { alwaysReject: true, message: 'Blocked everywhere', }); const restored = await RunState.fromString(agent, state.toString()); expect( restored._context.getRejectionMessage('toolSticky', 'sticky-1'), ).toBe('Blocked everywhere'); expect( restored._context.getRejectionMessage('toolSticky', 'sticky-2'), ).toBe('Blocked everywhere'); }); it('round-trips server-scoped hosted MCP approvals', async () => { const context = new RunContext(); const agent = new Agent({ name: 'HostedMcpApprovalStateAgent' }); const state = new RunState(context, 'input', agent, 1); const approval = new ToolApprovalItem( { type: 'hosted_tool_call', id: 'request-a', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: 'request-a', server_label: 'server-a', name: 'lookup_account', arguments: '{}', }, }, agent, ); state.approve(approval, { alwaysApprove: true }); const serialized = state.toJSON() as any; const approvalKey = JSON.stringify([ 'hosted_mcp', 'server-a', 'lookup_account', ]); expect(serialized.$schemaVersion).toBe('1.23'); expect(serialized.context.hostedMcpApprovals).toEqual({ [approvalKey]: { approved: true, rejected: [] }, }); expect(serialized.context.approvals).toEqual({}); const restored = await RunState.fromString(agent, state.toString()); const sameServer = new ToolApprovalItem( { ...approval.rawItem, id: 'request-a-next', providerData: { ...(approval.rawItem.providerData as Record), id: 'request-a-next', }, }, agent, ); const otherServer = new ToolApprovalItem( { ...sameServer.rawItem, id: 'request-b', providerData: { ...(sameServer.rawItem.providerData as Record), id: 'request-b', server_label: 'server-b', }, }, agent, ); expect(restored._context._getHostedMcpApprovalStatus(sameServer)).toBe( true, ); expect( restored._context._getHostedMcpApprovalStatus(otherServer), ).toBeUndefined(); }); it('preserves legacy local approvals whose names resemble hosted keys', async () => { const agent = new Agent({ name: 'LegacyCollidingLocalApprovalAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); const collidingName = JSON.stringify([ 'hosted_mcp', 'server-a', 'lookup_account', ]); const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.17'; serialized.context.approvals = { [collidingName]: { approved: true, rejected: [] }, }; delete serialized.context.hostedMcpApprovals; delete serialized.context.approvalInvocations; delete serialized.completedToolInvocations; delete serialized.completedToolInvocationEvidence; delete serialized.ambiguousToolInvocationCallIds; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context.isToolApproved({ toolName: collidingName, callId: 'future-local-call', functionTool: false, }), ).toBe(true); }); it('rejects request-only hosted MCP approvals before serialization', () => { const context = new RunContext(); const agent = new Agent({ name: 'HostedMcpRequestApprovalStateAgent' }); const state = new RunState(context, 'input', agent, 1); const approval = new ToolApprovalItem( { type: 'hosted_tool_call', id: 'request-only', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: 'request-only', name: 'lookup_account', arguments: '{}', }, }, agent, ); expect(() => state.approve(approval)).toThrow( 'Hosted MCP approval decisions require a non-empty server label and tool name.', ); expect(state.toJSON().context.approvals).toEqual({}); }); it('reads schema 1.17 hosted MCP decisions without trusting sticky names', async () => { const agent = new Agent({ name: 'LegacyHostedMcpApprovalStateAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); const pendingStickyApproval = new ToolApprovalItem( { type: 'hosted_tool_call', id: 'pending-sticky-request', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: 'pending-sticky-request', server_label: 'server-a', name: 'lookup_account', arguments: '{}', }, }, agent, ); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [pendingStickyApproval] }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.17'; serialized.context.approvals = { lookup_account: { approved: true, rejected: ['legacy-request'], messages: { 'legacy-request': 'Legacy exact denial.' }, }, }; serialized.context.hostedMcpApprovals = { [JSON.stringify(['hosted_mcp', 'server-a', 'lookup_account'])]: { approved: true, rejected: [], }, }; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const merged = await RunState.fromStringWithContext( agent, JSON.stringify(serialized), new RunContext(), { contextStrategy: 'merge' }, ); const replaced = await RunState.fromStringWithContext( agent, JSON.stringify(serialized), new RunContext(), { contextStrategy: 'replace' }, ); const buildApproval = (requestId: string) => new ToolApprovalItem( { type: 'hosted_tool_call', id: requestId, name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: requestId, server_label: 'server-a', name: 'lookup_account', arguments: '{}', }, }, agent, ); expect( restored._context._getHostedMcpApprovalStatus( buildApproval('future-request'), ), ).toBeUndefined(); expect( restored._context._getHostedMcpApprovalStatus( buildApproval('pending-sticky-request'), ), ).toBe(true); expect( merged._context._getHostedMcpApprovalStatus( buildApproval('pending-sticky-request'), ), ).toBe(true); expect( replaced._context._getHostedMcpApprovalStatus( buildApproval('pending-sticky-request'), ), ).toBeUndefined(); const exactLegacyApproval = buildApproval('legacy-request'); expect( restored._context._getHostedMcpApprovalStatus(exactLegacyApproval), ).toBeUndefined(); expect( restored._context._getHostedMcpRejectionMessage(exactLegacyApproval), ).toBeUndefined(); expect( restored.toJSON().context.hostedMcpApprovals?.[ JSON.stringify(['hosted_mcp', 'server-a', 'lookup_account']) ], ).toEqual({ approved: ['pending-sticky-request'], rejected: [] }); }); it('restores schema 1.17 sticky rejection only for the pending MCP request', async () => { const agent = new Agent({ name: 'LegacyHostedMcpRejectionStateAgent' }); const state = new RunState(new RunContext(), 'input', agent, 1); const buildApproval = (requestId: string) => new ToolApprovalItem( { type: 'hosted_tool_call', id: requestId, name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: requestId, server_label: 'server-a', name: 'lookup_account', arguments: '{}', }, }, agent, ); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [buildApproval('pending-sticky-rejection')], }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.17'; serialized.context.approvals = { lookup_account: { approved: [], rejected: true, stickyRejectMessage: 'Denied before serialization.', }, }; delete serialized.context.hostedMcpApprovals; const serializedState = JSON.stringify(serialized); const restored = await RunState.fromString(agent, serializedState); const merged = await RunState.fromStringWithContext( agent, serializedState, new RunContext(), { contextStrategy: 'merge' }, ); const replaced = await RunState.fromStringWithContext( agent, serializedState, new RunContext(), { contextStrategy: 'replace' }, ); const pendingApproval = buildApproval('pending-sticky-rejection'); expect( restored._context._getHostedMcpApprovalStatus(pendingApproval), ).toBe(false); expect( restored._context._getHostedMcpRejectionMessage(pendingApproval), ).toBe('Denied before serialization.'); expect( restored._context._getHostedMcpApprovalStatus( buildApproval('future-request'), ), ).toBeUndefined(); expect(merged._context._getHostedMcpApprovalStatus(pendingApproval)).toBe( false, ); expect( replaced._context._getHostedMcpApprovalStatus(pendingApproval), ).toBeUndefined(); expect( restored.toJSON().context.hostedMcpApprovals?.[ JSON.stringify(['hosted_mcp', 'server-a', 'lookup_account']) ], ).toEqual({ approved: [], rejected: ['pending-sticky-rejection'], messages: { 'pending-sticky-rejection': 'Denied before serialization.', }, }); }); it('preserves fail-closed sticky rejection precedence during schema 1.17 hosted MCP migration', async () => { const agent = new Agent({ name: 'LegacyHostedMcpPrecedenceAgent' }); const pendingApproval = new ToolApprovalItem( { type: 'hosted_tool_call', id: 'pending-mixed-decision', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: 'pending-mixed-decision', server_label: 'server-a', name: 'lookup_account', arguments: '{}', }, }, agent, ); const state = new RunState(new RunContext(), 'input', agent, 1); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [pendingApproval] }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.17'; serialized.context.approvals = { lookup_account: { approved: ['pending-mixed-decision'], rejected: true, stickyRejectMessage: 'Rejected before serialization.', }, }; delete serialized.context.hostedMcpApprovals; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context._getHostedMcpApprovalStatus(pendingApproval), ).toBe(false); expect( restored._context._getHostedMcpRejectionMessage(pendingApproval), ).toBe('Rejected before serialization.'); }); it('fails closed when a legacy exact approval matches multiple MCP servers', async () => { const agent = new Agent({ name: 'LegacyHostedMcpAmbiguousAgent' }); const buildApproval = (serverLabel: string) => new ToolApprovalItem( { type: 'hosted_tool_call', id: 'shared-legacy-request', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: 'shared-legacy-request', server_label: serverLabel, name: 'lookup_account', arguments: '{}', }, }, agent, ); const serverA = buildApproval('server-a'); const serverB = buildApproval('server-b'); const state = new RunState(new RunContext(), 'input', agent, 1); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [serverA, serverB] }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.17'; serialized.context.approvals = { lookup_account: { approved: ['shared-legacy-request'], rejected: [], }, }; delete serialized.context.hostedMcpApprovals; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context._getHostedMcpApprovalStatus(serverA), ).toBeUndefined(); expect( restored._context._getHostedMcpApprovalStatus(serverB), ).toBeUndefined(); expect( restored.toJSON().context.hostedMcpApprovals?.[ JSON.stringify(['hosted_mcp', 'server-a', 'lookup_account']) ], ).toBeUndefined(); }); it('fails closed when a legacy sticky approval matches multiple pending requests', async () => { const agent = new Agent({ name: 'LegacyHostedMcpStickyAmbiguousAgent' }); const buildApproval = (requestId: string, serverLabel: string) => new ToolApprovalItem( { type: 'hosted_tool_call', id: requestId, name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: requestId, server_label: serverLabel, name: 'lookup_account', arguments: '{}', }, }, agent, ); const serverA = buildApproval('request-a', 'server-a'); const serverB = buildApproval('request-b', 'server-b'); const state = new RunState(new RunContext(), 'input', agent, 1); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [serverA, serverB] }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.17'; serialized.context.approvals = { lookup_account: { approved: true, rejected: [] }, }; delete serialized.context.hostedMcpApprovals; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context._getHostedMcpApprovalStatus(serverA), ).toBeUndefined(); expect( restored._context._getHostedMcpApprovalStatus(serverB), ).toBeUndefined(); }); it.each([ { approved: true, rejected: [] as string[] }, { approved: [] as string[], rejected: true }, ])( 'fails closed when a legacy sticky decision has a same-tool claimant without an id', async (legacyDecision) => { const agent = new Agent({ name: 'LegacyHostedMcpIncompleteStickyAgent', }); const complete = new ToolApprovalItem( { type: 'hosted_tool_call', id: 'complete-request', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: 'complete-request', server_label: 'server-a', name: 'lookup_account', arguments: '{}', }, }, agent, ); const incomplete = new ToolApprovalItem( { type: 'hosted_tool_call', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', server_label: 'server-b', name: 'lookup_account', arguments: '{}', }, }, agent, ); const state = new RunState(new RunContext(), 'input', agent, 1); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [complete, incomplete] }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.17'; serialized.context.approvals = { lookup_account: legacyDecision, }; delete serialized.context.hostedMcpApprovals; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context._getHostedMcpApprovalStatus(complete), ).toBeUndefined(); }, ); it.each(['incomplete hosted MCP', 'same-named shell'] as const)( 'fails closed when a legacy exact approval also matches a %s claimant', async (claimantKind) => { const agent = new Agent({ name: 'LegacyHostedMcpClaimantAgent' }); const hosted = new ToolApprovalItem( { type: 'hosted_tool_call', id: 'shared-claim', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: 'shared-claim', server_label: 'server-a', name: 'lookup_account', arguments: '{}', }, }, agent, ); const otherClaimant = claimantKind === 'incomplete hosted MCP' ? new ToolApprovalItem( { type: 'hosted_tool_call', id: 'shared-claim', name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: 'shared-claim', name: 'lookup_account', arguments: '{}', }, }, agent, ) : new ToolApprovalItem( { type: 'shell_call', callId: 'shared-claim', name: 'lookup_account', status: 'in_progress', action: { commands: ['echo test'] }, } as any, agent, ); const state = new RunState(new RunContext(), 'input', agent, 1); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [hosted, otherClaimant] }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.17'; serialized.context.approvals = { lookup_account: { approved: ['shared-claim'], rejected: [], }, }; delete serialized.context.hostedMcpApprovals; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context._getHostedMcpApprovalStatus(hosted), ).toBeUndefined(); }, ); it('preserves a schema 1.15 exact MCP approval across a function-name collision', async () => { const sameNamedFunction = tool({ name: 'lookup_account', description: 'Look up a local account.', parameters: z.object({}), execute: async () => 'local', }); const agent = new Agent({ name: 'LegacyHostedMcpExactApprovalAgent', tools: [sameNamedFunction], }); const state = new RunState(new RunContext(), 'input', agent, 1); const buildApproval = (requestId: string, serverLabel = 'server-a') => new ToolApprovalItem( { type: 'hosted_tool_call', id: requestId, name: 'lookup_account', status: 'in_progress', providerData: { type: 'mcp_approval_request', id: requestId, server_label: serverLabel, name: 'lookup_account', arguments: '{}', }, }, agent, ); state._currentStep = { type: 'next_step_interruption', data: { interruptions: [buildApproval('legacy-exact-request')] }, }; const serialized = state.toJSON() as any; serialized.$schemaVersion = '1.15'; serialized.context.approvals = { lookup_account: { approved: ['legacy-exact-request'], rejected: [], }, }; delete serialized.context.hostedMcpApprovals; delete serialized.context.functionApprovals; delete serialized.context.legacyFunctionApprovals; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect( restored._context._getHostedMcpApprovalStatus( buildApproval('legacy-exact-request'), ), ).toBe(true); expect( restored._context._getHostedMcpApprovalStatus( buildApproval('future-request'), ), ).toBeUndefined(); expect( restored._context._getHostedMcpApprovalStatus( buildApproval('legacy-exact-request', 'server-b'), ), ).toBeUndefined(); expect( restored.toJSON().context.hostedMcpApprovals?.[ JSON.stringify(['hosted_mcp', 'server-a', 'lookup_account']) ], ).toEqual({ approved: ['legacy-exact-request'], rejected: [] }); }); it('tracks qualified tool names for namespaced approvals', () => { const context = new RunContext(); const agent = new Agent({ name: 'AgentNamespaceApproval' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: 'lookup_account', namespace: 'crm', callId: 'cid_namespace', status: 'completed', arguments: '{}', }; const approvalItem = new ToolApprovalItem(rawItem, agent); state.approve(approvalItem); const approvalKey = getFunctionToolStateKeyForCall(rawItem)!; expect( state._context.isToolApproved({ toolName: approvalKey, callId: 'cid_namespace', }), ).toBe(true); expect(approvalItem.toolName).toBe('crm.lookup_account'); }); it('preserves declared tool names for top-level deferred approvals across resume', async () => { const context = new RunContext(); const agent = new Agent({ name: 'AgentDeferredApproval' }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: 'get_shipping_eta', namespace: 'get_shipping_eta', callId: 'cid_shipping_eta', status: 'completed', arguments: '{}', }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [ new ToolApprovalItem(rawItem, agent, 'get_shipping_eta'), ], }, }; const restored = await RunState.fromString(agent, state.toString()); const [approvalItem] = restored.getInterruptions(); restored.approve(approvalItem); const approvalKey = getFunctionToolStateKeyForCall(rawItem)!; expect( restored._context.isToolApproved({ toolName: approvalKey, callId: 'cid_shipping_eta', }), ).toBe(true); expect( restored._context.isToolApproved({ toolName: 'get_shipping_eta', callId: 'cid_shipping_eta', }), ).toBeUndefined(); expect(approvalItem.toolName).toBe('get_shipping_eta'); }); it('resolves top-level deferred approval names from the agent tool set across resume', async () => { const context = new RunContext(); const shippingEta = tool({ name: 'get_shipping_eta', description: 'Look up a shipping ETA.', parameters: z.object({ trackingNumber: z.string(), }), deferLoading: true, execute: async () => 'tomorrow', }); const agent = new Agent({ name: 'AgentDeferredApprovalResolved', tools: [shippingEta], }); const state = new RunState(context, '', agent, 1); const rawItem: protocol.FunctionCallItem = { type: 'function_call', name: 'get_shipping_eta', namespace: 'get_shipping_eta', callId: 'cid_shipping_eta', status: 'completed', arguments: '{}', }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [new ToolApprovalItem(rawItem, agent)], }, }; const restored = await RunState.fromString(agent, state.toString()); const [approvalItem] = restored.getInterruptions(); restored.approve(approvalItem); const approvalKey = getFunctionToolStateKeyForCall(rawItem)!; expect( restored._context.isToolApproved({ toolName: approvalKey, callId: 'cid_shipping_eta', }), ).toBe(true); expect( restored._context.isToolApproved({ toolName: 'get_shipping_eta', callId: 'cid_shipping_eta', }), ).toBeUndefined(); expect(approvalItem.toolName).toBe('get_shipping_eta'); }); it('fromString reconstructs state for simple agent', async () => { const context = new RunContext({ a: 1 }); const agent = new Agent({ name: 'Solo' }); const state = new RunState(context, 'orig', agent, 7); state._currentTurn = 5; state._noActiveAgentRun = false; const str = state.toString(); const newState = await RunState.fromString(agent, str); expect(newState._maxTurns).toBe(7); expect(newState._currentTurn).toBe(5); expect(newState._currentAgent).toBe(agent); expect(newState._noActiveAgentRun).toBe(false); expect(newState._context.context).toEqual({ a: 1 }); expect(newState._generatedItems).toEqual([]); expect(newState._modelResponses).toEqual([]); expect(newState._trace).toBeNull(); }); it('fromString rehydrates interruption items as RunToolApprovalItem', async () => { const context = new RunContext(); const agent = new Agent({ name: 'InterruptAgent' }); const state = new RunState(context, 'input', agent, 3); const rawApproval: protocol.ToolCallItem = { type: 'function_call', name: 'secure_tool', callId: 'call-secure-1', status: 'completed', arguments: '{}', }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [new ToolApprovalItem(rawApproval, agent)], }, }; const restored = await RunState.fromString(agent, state.toString()); const interruptions = restored.getInterruptions(); expect(interruptions).toHaveLength(1); expect(interruptions[0]).toBeInstanceOf(ToolApprovalItem); expect(interruptions[0].name).toBe('secure_tool'); expect(interruptions[0].agent).toBe(agent); }); it('fromString falls back to current agent for serialized interruptions with unknown agent', async () => { const context = new RunContext(); const agent = new Agent({ name: 'InterruptFallbackAgent' }); const state = new RunState(context, 'input', agent, 3); const rawApproval: protocol.ToolCallItem = { type: 'function_call', name: 'nested_tool', callId: 'call-nested-1', status: 'completed', arguments: '{}', }; const serialized = state.toJSON() as any; serialized.currentStep = { type: 'next_step_interruption', data: { interruptions: [ { type: 'tool_approval_item', rawItem: rawApproval, agent: { name: 'NestedAsToolAgent' }, toolName: 'nested_tool', }, ], }, }; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const interruptions = restored.getInterruptions(); expect(interruptions).toHaveLength(1); expect(interruptions[0]).toBeInstanceOf(ToolApprovalItem); expect(interruptions[0].name).toBe('nested_tool'); expect(interruptions[0].agent).toBe(agent); expect(() => restored.toString()).not.toThrow(); }); it('fromString rehydrates hosted tool interruptions and supports approval', async () => { const context = new RunContext(); const agent = new Agent({ name: 'HostedInterruptAgent' }); const state = new RunState(context, 'input', agent, 3); const rawApproval: protocol.HostedToolCallItem = { type: 'hosted_tool_call', id: 'approval-1', name: 'search_codex_code', arguments: '{}', status: 'completed', }; state._currentStep = { type: 'next_step_interruption', data: { interruptions: [new ToolApprovalItem(rawApproval, agent)], }, }; const restored = await RunState.fromString(agent, state.toString()); const interruptions = restored.getInterruptions(); expect(interruptions).toHaveLength(1); expect(interruptions[0]).toBeInstanceOf(ToolApprovalItem); expect(interruptions[0].name).toBe('search_codex_code'); restored.approve(interruptions[0]); expect( restored._context.isToolApproved({ toolName: 'search_codex_code', callId: 'approval-1', }), ).toBe(true); }); it('fromString rehydrates interruptions from legacy raw interruption shape', async () => { const context = new RunContext(); const agent = new Agent({ name: 'LegacyInterruptAgent' }); const state = new RunState(context, 'input', agent, 3); const rawApproval: protocol.ToolCallItem = { type: 'function_call', name: 'legacy_tool', callId: 'legacy-call-1', status: 'completed', arguments: '{}', }; const serialized = state.toJSON() as any; serialized.currentStep = { type: 'next_step_interruption', data: { interruptions: [ { rawItem: rawApproval, toolName: 'legacy_tool', }, ], }, }; const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); const interruptions = restored.getInterruptions(); expect(interruptions).toHaveLength(1); expect(interruptions[0]).toBeInstanceOf(ToolApprovalItem); expect(interruptions[0].name).toBe('legacy_tool'); expect(interruptions[0].agent).toBe(agent); }); it('serializes and restores guardrail results', async () => { const context = new RunContext(); const agentA = new Agent({ name: 'A' }); const agentB = new Agent({ name: 'B' }); agentA.handoffs = [agentB]; const state = new RunState(context, 'input', agentA, 2); state._inputGuardrailResults = [ { guardrail: { type: 'input', name: 'ig' }, output: { tripwireTriggered: false, outputInfo: { ok: true } }, }, ]; state._outputGuardrailResults = [ { guardrail: { type: 'output', name: 'og' }, agent: agentB, agentOutput: 'final', output: { tripwireTriggered: true, outputInfo: { done: true } }, }, ]; state._toolInputGuardrailResults = [ { guardrail: { type: 'tool_input', name: 'tig' }, output: { behavior: { type: 'rejectContent', message: 'nope' }, outputInfo: { a: 1 }, }, }, ]; state._toolOutputGuardrailResults = [ { guardrail: { type: 'tool_output', name: 'tog' }, output: { behavior: { type: 'allow' }, outputInfo: { b: 2 }, }, }, ]; const str = state.toString(); const newState = await RunState.fromString(agentA, str); expect(newState._inputGuardrailResults).toEqual( state._inputGuardrailResults, ); expect(newState._outputGuardrailResults[0].guardrail).toEqual({ type: 'output', name: 'og', }); expect(newState._outputGuardrailResults[0].agent).toBe(agentB); expect(newState._outputGuardrailResults[0].agentOutput).toBe('final'); expect(newState._outputGuardrailResults[0].output).toEqual({ tripwireTriggered: true, outputInfo: { done: true }, }); expect(newState._toolInputGuardrailResults).toEqual( state._toolInputGuardrailResults, ); expect(newState._toolOutputGuardrailResults).toEqual( state._toolOutputGuardrailResults, ); }); it('buildAgentMap collects agents without looping', () => { const agentA = new Agent({ name: 'AgentA' }); const agentB = new Agent({ name: 'AgentB' }); // Create a cycle A -> B -> A agentA.handoffs = [agentB]; agentB.handoffs = [agentA]; const map = buildAgentMap(agentA); expect(map.get('AgentA')).toBe(agentA); expect(map.get('AgentB')).toBe(agentB); expect(Array.from(map.keys()).sort()).toEqual(['AgentA', 'AgentB']); }); it('buildAgentMap throws when distinct agents share the same name', () => { const childA = new Agent({ name: 'Child' }); const childB = new Agent({ name: 'Child' }); const root = new Agent({ name: 'Root', handoffs: [childA, childB] }); expect(() => buildAgentMap(root)).toThrow( 'Duplicate agent name "Child" detected. Use unique agent names when serializing RunState.', ); }); }); } export function registerRunStateDeserializeTests(): void { describe('deserialize helpers', () => { it('deserializeModelResponse restores response object', () => { const serialized = { usage: { requests: 1, inputTokens: 2, outputTokens: 3, totalTokens: 6 }, output: [TEST_MODEL_MESSAGE], responseId: 'r1', } as any; const resp = deserializeModelResponse(serialized); expect(resp.responseId).toBe('r1'); expect(resp.output[0].type).toBe('message'); }); it('deserializeItem restores MessageOutputItem', () => { const agent = new Agent({ name: 'X' }); const map = new Map([[agent.name, agent]]); const item = deserializeItem( { type: 'message_output_item', rawItem: TEST_MODEL_MESSAGE, agent: { name: 'X' }, }, map, ); expect(item.type).toBe('message_output_item'); expect((item as any).agent).toBe(agent); }); it('deserializeItem restores ToolSearchCallItem', () => { const agent = new Agent({ name: 'SearchAgent' }); const map = new Map([[agent.name, agent]]); const item = deserializeItem( { type: 'tool_search_call_item', rawItem: { type: 'tool_search_call', id: 'ts_call', status: 'completed', arguments: { paths: ['crm'], query: 'profile' }, }, agent: { name: 'SearchAgent' }, }, map, ); expect(item).toBeInstanceOf(RunToolSearchCallItem); expect((item as RunToolSearchCallItem).rawItem.arguments).toEqual({ paths: ['crm'], query: 'profile', }); }); it('deserializeItem restores ToolSearchOutputItem', () => { const agent = new Agent({ name: 'SearchAgent' }); const map = new Map([[agent.name, agent]]); const item = deserializeItem( { type: 'tool_search_output_item', rawItem: { type: 'tool_search_output', id: 'ts_output', status: 'completed', tools: [ { type: 'tool_reference', functionName: 'lookup_account', namespace: 'crm', }, ], }, agent: { name: 'SearchAgent' }, }, map, ); expect(item).toBeInstanceOf(RunToolSearchOutputItem); expect((item as RunToolSearchOutputItem).rawItem.tools).toEqual([ { type: 'tool_reference', functionName: 'lookup_account', namespace: 'crm', }, ]); }); it('deserializeItem restores ToolSearchOutputItem with concrete tool payloads', () => { const agent = new Agent({ name: 'SearchAgent' }); const map = new Map([[agent.name, agent]]); const item = deserializeItem( { type: 'tool_search_output_item', rawItem: { type: 'tool_search_output', id: 'ts_output', status: 'completed', tools: [ { type: 'namespace', name: 'crm', description: 'CRM tools.', tools: [ { type: 'function', name: 'lookup_account', description: 'Look up an account.', deferLoading: true, strict: true, parameters: { type: 'object', properties: { customerId: { type: 'string', }, }, required: ['customerId'], additionalProperties: false, }, }, ], }, ], }, agent: { name: 'SearchAgent' }, }, map, ); expect(item).toBeInstanceOf(RunToolSearchOutputItem); expect((item as RunToolSearchOutputItem).rawItem.tools).toEqual([ { type: 'namespace', name: 'crm', description: 'CRM tools.', tools: [ { type: 'function', name: 'lookup_account', description: 'Look up an account.', deferLoading: true, strict: true, parameters: { type: 'object', properties: { customerId: { type: 'string', }, }, required: ['customerId'], additionalProperties: false, }, }, ], }, ]); }); it('restores the enabled handoff winner instead of a later disabled duplicate', async () => { const enabledTarget = new Agent({ name: 'SharedTarget', instructions: 'enabled target', }); const disabledTarget = new Agent({ name: 'SharedTarget', instructions: 'disabled target', }); const enabledHandoff = handoff(enabledTarget, { toolNameOverride: 'transfer', }); const disabledHandoff = handoff(disabledTarget, { toolNameOverride: 'transfer', isEnabled: false, }); const agent = new Agent({ name: 'HandoffWinnerRestore', handoffs: [enabledHandoff, disabledHandoff], }); const state = new RunState(new RunContext(), '', agent, 1); const toolCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_handoff_winner', callId: 'call_handoff_winner', name: 'transfer', status: 'completed', arguments: '{}', }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [{ toolCall, handoff: enabledHandoff }], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['transfer'], hasToolsOrApprovalsToRun: () => true, }; const serialized = state.toJSON() as any; expect( serialized.lastProcessedResponse.handoffs[0].targetAgent.identity, ).toEqual(expect.any(String)); const restored = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restored._lastProcessedResponse?.handoffs[0]?.handoff).toBe( enabledHandoff, ); delete serialized.lastProcessedResponse.handoffs[0].targetAgent; await expect( RunState.fromString(agent, JSON.stringify(serialized)), ).rejects.toThrow( 'Run state handoff is missing its required target agent identity.', ); serialized.$schemaVersion = '1.15'; const restoredLegacy = await RunState.fromString( agent, JSON.stringify(serialized), ); expect(restoredLegacy._lastProcessedResponse?.handoffs[0]?.handoff).toBe( enabledHandoff, ); }); it('rejects a schema 1.16 handoff without exact identity instead of rebinding a same-name target', async () => { const executeToolSearch = vi.fn(async () => []); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client' }, }, executeToolSearch, ); const firstTarget = new Agent({ name: 'SharedTarget' }); const secondTarget = new Agent({ name: 'SharedTarget' }); const firstHandoff = handoff(firstTarget, { toolNameOverride: 'transfer', }); const secondHandoff = handoff(secondTarget, { toolNameOverride: 'transfer', }); const agent = new Agent({ name: 'AmbiguousHandoffRestore', tools: [clientToolSearch], handoffs: [firstHandoff, secondHandoff], }); const state = new RunState(new RunContext(), '', agent, 1); const toolSearchCallId = 'call_ambiguous_handoff_search'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_ambiguous_handoff_search', status: 'completed', arguments: { paths: [] }, providerData: { call_id: toolSearchCallId, execution: 'client', }, } as protocol.ToolSearchCallItem, agent, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_ambiguous_handoff_search', status: 'completed', tools: [], providerData: { call_id: toolSearchCallId, execution: 'client', }, } as protocol.ToolSearchOutputItem, agent, ), ); state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [ { toolCall: { type: 'function_call', id: 'fc_ambiguous_handoff', callId: 'call_ambiguous_handoff', name: 'transfer', status: 'completed', arguments: '{}', }, handoff: firstHandoff, }, ], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['transfer'], hasToolsOrApprovalsToRun: () => true, }; const serialized = state.toJSON() as any; const serializedJson = JSON.stringify(serialized); const missingTarget = JSON.parse(serializedJson); delete missingTarget.lastProcessedResponse.handoffs[0].targetAgent; const unknownTarget = JSON.parse(serializedJson); unknownTarget.lastProcessedResponse.handoffs[0].targetAgent.identity = 'missing-target-identity'; await expectToolNameCollisionWarnings(3, () => Promise.all([ expect(RunState.fromString(agent, serializedJson)).rejects.toThrow( 'Handoff transfer not found', ), expect( RunState.fromString(agent, JSON.stringify(missingTarget)), ).rejects.toThrow( 'Run state handoff is missing its required target agent identity.', ), expect( RunState.fromString(agent, JSON.stringify(unknownTarget)), ).rejects.toThrow('Agent identity missing-target-identity not found'), ]), ); expect(executeToolSearch).not.toHaveBeenCalled(); }); it('rejects a handoff disabled by a replacement context during restore', async () => { const target = new Agent<{ enabled: boolean }>({ name: 'DynamicTarget' }); const dynamicHandoff = handoff(target, { toolNameOverride: 'transfer', isEnabled: ({ runContext }) => runContext.context.enabled, }); const agent = new Agent<{ enabled: boolean }>({ name: 'DynamicHandoffRestore', handoffs: [dynamicHandoff], }); const state = new RunState( new RunContext({ enabled: true }), '', agent, 1, ); const toolCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_dynamic_handoff', callId: 'call_dynamic_handoff', name: 'transfer', status: 'completed', arguments: '{}', }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [{ toolCall, handoff: dynamicHandoff }], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['transfer'], hasToolsOrApprovalsToRun: () => true, }; await expect( RunState.fromStringWithContext( agent, state.toString(), new RunContext({ enabled: false }), { contextStrategy: 'replace' }, ), ).rejects.toThrow('Handoff transfer not found'); }); it('deserializeProcessedResponse restores namespaced function tools', async () => { const crmLookup = tool({ name: 'lookup_account', description: 'Look up an account in CRM.', parameters: z.object({ accountId: z.string(), }), execute: async () => 'crm', }); const billingLookup = tool({ name: 'lookup_account', description: 'Look up an account in billing.', parameters: z.object({ accountId: z.string(), }), execute: async () => 'billing', }); const crmNamespace = toolNamespace({ name: 'crm', description: 'CRM tools', tools: [crmLookup], }); const billingNamespace = toolNamespace({ name: 'billing', description: 'Billing tools', tools: [billingLookup], }); const agent = new Agent({ name: 'NamespacedRestore', tools: [...crmNamespace, ...billingNamespace], }); const state = new RunState(new RunContext(), '', agent, 1); const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_restore', callId: 'call_restore', name: 'lookup_account', namespace: 'billing', status: 'completed', arguments: '{"accountId":"acct_42"}', }; state._lastProcessedResponse = { newItems: [], functions: [ { toolCall: functionCall, tool: billingNamespace[0] as any }, ], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['billing.lookup_account'], hasToolsOrApprovalsToRun: () => true, }; const restored = await RunState.fromString(agent, state.toString()); expect(restored._lastProcessedResponse?.functions[0]?.tool).toBe( billingNamespace[0], ); expect(restored._lastProcessedResponse?.functions[0]?.toolCall).toEqual( functionCall, ); }); it('deserializeProcessedResponse restores top-level deferred function tools', async () => { const shippingEta = tool({ name: 'get_shipping_eta', description: 'Look up a shipping ETA.', parameters: z.object({ trackingNumber: z.string(), }), deferLoading: true, execute: async () => 'tomorrow', }); const agent = new Agent({ name: 'DeferredRestore', tools: [shippingEta], }); const state = new RunState(new RunContext(), '', agent, 1); const functionCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_shipping_eta', callId: 'call_shipping_eta', name: 'get_shipping_eta', namespace: 'get_shipping_eta', status: 'completed', arguments: '{"trackingNumber":"ZX-123"}', }; state._lastProcessedResponse = { newItems: [], functions: [{ toolCall: functionCall, tool: shippingEta as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['get_shipping_eta'], hasToolsOrApprovalsToRun: () => true, } as any; const restored = await RunState.fromString(agent, state.toString()); expect(restored._lastProcessedResponse?.functions[0]?.tool).toBe( shippingEta, ); expect(restored._lastProcessedResponse?.functions[0]?.toolCall).toEqual( functionCall, ); }); it('deserializeProcessedResponse restores computer actions', async () => { const tool = computerTool({ computer: new FakeComputer() }); const agent = new Agent({ name: 'Comp', tools: [tool] }); const state = new RunState(new RunContext(), '', agent, 1); const call: protocol.ComputerUseCallItem = { type: 'computer_call', callId: 'c1', status: 'completed', action: { type: 'screenshot' } as any, }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [{ toolCall: call, computer: tool }], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => true, }; const restored = await RunState.fromString(agent, state.toString()); expect( restored._lastProcessedResponse?.computerActions[0]?.computer, ).toBe(tool); }); it('deserializeProcessedResponse restores batched computer actions', async () => { const tool = computerTool({ computer: new FakeComputer() }); const agent = new Agent({ name: 'Comp', tools: [tool] }); const state = new RunState(new RunContext(), '', agent, 1); const call: protocol.ComputerUseCallItem = { type: 'computer_call', callId: 'c1-batched', status: 'completed', actions: [ { type: 'move', x: 1, y: 2 }, { type: 'click', x: 1, y: 2, button: 'left' }, ], }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [{ toolCall: call, computer: tool }], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => true, }; const restored = await RunState.fromString(agent, state.toString()); expect( restored._lastProcessedResponse?.computerActions[0]?.toolCall.actions, ).toEqual(call.actions); expect( restored._lastProcessedResponse?.computerActions[0]?.computer, ).toBe(tool); }); it('deserializeProcessedResponse restores GA computer tool aliases', async () => { const tool = computerTool({ computer: new FakeComputer() }); const agent = new Agent({ name: 'Comp', tools: [tool] }); const state = new RunState(new RunContext(), '', agent, 1); const call: protocol.ComputerUseCallItem = { type: 'computer_call', callId: 'c1-alias', status: 'completed', action: { type: 'screenshot' }, }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [{ toolCall: call, computer: tool }], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => true, }; const json = state.toJSON(); json.lastProcessedResponse!.computerActions[0]!.computer.name = 'computer'; const restored = await RunState.fromString(agent, JSON.stringify(json)); expect( restored._lastProcessedResponse?.computerActions[0]?.computer, ).toBe(tool); }); it('deserializeProcessedResponse prefers exact computer tool names', async () => { const gaTool = computerTool({ name: 'computer', computer: new FakeComputer(), }); const previewTool = computerTool({ computer: new FakeComputer() }); const agent = new Agent({ name: 'Comp', tools: [gaTool, previewTool], }); const state = new RunState(new RunContext(), '', agent, 1); const call: protocol.ComputerUseCallItem = { type: 'computer_call', callId: 'c1-exact-name', status: 'completed', action: { type: 'screenshot' }, }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [{ toolCall: call, computer: gaTool }], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => true, }; const restored = await RunState.fromString(agent, state.toString()); expect( restored._lastProcessedResponse?.computerActions[0]?.computer, ).toBe(gaTool); expect( restored._lastProcessedResponse?.computerActions[0]?.computer, ).not.toBe(previewTool); }); it('deserializeProcessedResponse restores shell actions', async () => { const shell = shellTool({ shell: new FakeShell() }); const agent = new Agent({ name: 'Shell', tools: [shell] }); const state = new RunState(new RunContext(), '', agent, 1); const call: protocol.ShellCallItem = { type: 'shell_call', callId: 's1', status: 'completed', action: { commands: ['echo hi'] }, }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [], shellActions: [{ toolCall: call, shell }], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => true, }; const restored = await RunState.fromString(agent, state.toString()); expect(restored._lastProcessedResponse?.shellActions[0]?.shell).toBe( shell, ); }); it('deserializeProcessedResponse restores apply_patch actions', async () => { const editorTool = applyPatchTool({ editor: new FakeEditor() }); const agent = new Agent({ name: 'Editor', tools: [editorTool] }); const state = new RunState(new RunContext(), '', agent, 1); const call: protocol.ApplyPatchCallItem = { type: 'apply_patch_call', callId: 'ap1', status: 'completed', operation: { type: 'delete_file', path: 'tmp.txt' }, }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [{ toolCall: call, applyPatch: editorTool }], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => true, }; const restored = await RunState.fromString(agent, state.toString()); expect( restored._lastProcessedResponse?.applyPatchActions[0]?.applyPatch, ).toBe(editorTool); }); it('deserializeProcessedResponse restores sandbox-injected apply_patch placeholders', async () => { const editorTool = applyPatchTool({ editor: new FakeEditor() }); const agent = new SandboxAgent({ name: 'SandboxEditor' }); const state = new RunState(new RunContext(), '', agent, 1); const call: protocol.ApplyPatchCallItem = { type: 'apply_patch_call', callId: 'ap_sandbox', status: 'completed', operation: { type: 'delete_file', path: 'tmp.txt' }, }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [{ toolCall: call, applyPatch: editorTool }], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => true, }; const restored = await RunState.fromString(agent, state.toString()); expect( restored._lastProcessedResponse?.applyPatchActions[0]?.applyPatch.name, ).toBe('apply_patch'); expect( processedResponseRequiresExecutionToolRehydration( restored._lastProcessedResponse, ), ).toBe(true); }); it('fromString tolerates agents gaining MCP servers after serialization', async () => { const agentWithoutMcp = new Agent({ name: 'McpLite' }); const state = new RunState(new RunContext(), 'input', agentWithoutMcp, 1); state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => false, }; const serialized = state.toString(); const stubMcpServer: MCPServer = { name: 'stub-server', cacheToolsList: false, toolFilter: undefined, async connect() {}, async close() {}, async listTools() { return []; }, async callTool( _toolName: string, _args: Record | null, _meta?: Record | null, ) { return []; }, async invalidateToolsCache() {}, }; const agentWithMcp = new Agent({ name: 'McpLite', mcpServers: [stubMcpServer], }); const restored = await RunState.fromString(agentWithMcp, serialized); expect(restored._currentAgent.mcpServers).toHaveLength(1); expect(restored._lastProcessedResponse?.hasToolsOrApprovalsToRun()).toBe( false, ); }); it('fromString tolerates serialized traces with new MCP servers', async () => { const traceProvider = getGlobalTraceProvider(); const trace = traceProvider.createTrace({ name: 'restore-with-trace' }); const agentWithoutMcp = new Agent({ name: 'McpTracey' }); const state = new RunState(new RunContext(), 'input', agentWithoutMcp, 1); state._trace = trace; state._currentAgentSpan = createAgentSpan( { data: { name: agentWithoutMcp.name } }, trace, ); state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: [], hasToolsOrApprovalsToRun: () => false, }; const serialized = state.toString(); let listCalled = false; const stubMcpTool: MCPTool = { name: 'sample_tool', description: '', inputSchema: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }; const stubMcpServer: MCPServer = { name: 'stub-traced-server', cacheToolsList: false, toolFilter: undefined, async connect() {}, async close() {}, async listTools() { listCalled = true; return [stubMcpTool]; }, async callTool( _toolName: string, _args: Record | null, _meta?: Record | null, ) { return []; }, async invalidateToolsCache() {}, }; const agentWithMcp = new Agent({ name: 'McpTracey', mcpServers: [stubMcpServer], }); const restored = await RunState.fromString(agentWithMcp, serialized); expect(restored._currentAgent.mcpServers).toHaveLength(1); expect(listCalled).toBe(true); }); it('deserializeProcessedResponse restores currentStep', async () => { const tool = computerTool({ computer: new FakeComputer() }); const currentMcpTool = hostedMcpTool({ serverLabel: 'gitmcp', serverUrl: 'https://gitmcp.io/openai/codex', requireApproval: 'always', allowedCallers: ['programmatic'], }); const agent = new Agent({ name: 'Comp', tools: [tool, currentMcpTool], }); const state = new RunState(new RunContext(), '', agent, 1); const call: protocol.ComputerUseCallItem = { type: 'computer_call', callId: 'c1', status: 'completed', action: { type: 'screenshot' } as any, }; state._lastProcessedResponse = { newItems: [], functions: [], handoffs: [], computerActions: [{ toolCall: call, computer: tool }], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [ { requestItem: { rawItem: { type: 'hosted_tool_call', name: 'fetch_generic_url_content', status: 'in_progress', caller: { type: 'program', callerId: 'prog_approval_1', }, providerData: { id: 'mcpr_685bc3c47ed88192977549b5206db77504d4306d5de6ab36', type: 'mcp_approval_request', arguments: '{"url":"https://raw.githubusercontent.com/openai/codex/main/README.md"}', name: 'fetch_generic_url_content', server_label: 'gitmcp', }, }, type: 'tool_approval_item', agent: new Agent({ name: 'foo ' }), name: 'fetch_generic_url_content', arguments: '{"url":"https://raw.githubusercontent.com/openai/codex/main/README.md"}', toJSON: function (): any { throw new Error('Function not implemented.'); }, }, mcpTool: { type: 'hosted_tool', name: 'hosted_mcp', providerData: { type: 'mcp', server_label: 'gitmcp', server_url: 'https://gitmcp.io/openai/codex', require_approval: { always: { tool_names: ['fetch_generic_url_content'], }, never: { tool_names: [ 'search_codex_code', 'fetch_codex_documentation', ], }, }, }, }, }, ], toolsUsed: [], hasToolsOrApprovalsToRun: () => true, }; state._currentStep = { type: 'next_step_handoff', newAgent: agent, }; const restored = await RunState.fromString(agent, state.toString()); expect(restored._currentStep?.type).toBe('next_step_handoff'); if (restored._currentStep?.type === 'next_step_handoff') { expect(restored._currentStep.newAgent).toBe(agent); } expect( restored._lastProcessedResponse?.mcpApprovalRequests[0].mcpTool, ).toBe(currentMcpTool); expect( restored._lastProcessedResponse?.mcpApprovalRequests[0].requestItem .rawItem.providerData, ).toEqual( state._lastProcessedResponse?.mcpApprovalRequests[0].requestItem.rawItem .providerData, ); const restoredApprovalRawItem = restored._lastProcessedResponse?.mcpApprovalRequests[0].requestItem .rawItem; expect(restoredApprovalRawItem?.type).toBe('hosted_tool_call'); if (restoredApprovalRawItem?.type !== 'hosted_tool_call') { throw new Error('Expected a hosted MCP approval item'); } expect(restoredApprovalRawItem.caller).toEqual({ type: 'program', callerId: 'prog_approval_1', }); }); it('uses an explicit prepared tool snapshot without adding stored runtime tools', async () => { const runtimeLookup = tool({ name: 'lookup_runtime', description: 'Look up a runtime record.', parameters: z.object({}), execute: async () => 'runtime', }); const agent = new Agent({ name: 'ExplicitPreparedTools' }); const state = new RunState(new RunContext(), '', agent, 1); const toolCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_runtime_explicit', callId: 'call_runtime_explicit', name: 'lookup_runtime', status: 'completed', arguments: '{}', }; state.recordToolSearchRuntimeTools( agent, { type: 'tool_search_output', id: 'ts_output_runtime_explicit', status: 'completed', tools: [], } as protocol.ToolSearchOutputItem, [runtimeLookup], ); state._lastProcessedResponse = { newItems: [], functions: [{ toolCall, tool: runtimeLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['lookup_runtime'], hasToolsOrApprovalsToRun: () => true, }; await expect( rehydrateProcessedResponseTools(agent, state, []), ).rejects.toThrow('Tool lookup_runtime not found'); }); it('rejects resumed runtime function tools when isEnabled is false in replacement context', async () => { const lookupParams = z.object({}); const runtimeLookup = tool({ name: 'lookup_runtime', description: 'Look up a runtime record.', parameters: lookupParams, isEnabled: async ({ runContext }) => runContext.context.enabled, execute: async () => 'runtime', }); const clientToolSearch = attachClientToolSearchExecutor( { type: 'hosted_tool', name: 'tool_search', providerData: { type: 'tool_search', execution: 'client', }, }, async () => runtimeLookup, ); const agent = new Agent<{ enabled: boolean }>({ name: 'RuntimeEnabledRestore', tools: [clientToolSearch], }); const state = new RunState( new RunContext({ enabled: true }), '', agent, 1, ); const searchCallId = 'call_runtime_enabled_restore'; state._generatedItems.push( new RunToolSearchCallItem( { type: 'tool_search_call', id: 'ts_call_runtime_enabled_restore', status: 'completed', arguments: { paths: [] }, providerData: { call_id: searchCallId, execution: 'client' }, } as protocol.ToolSearchCallItem, agent as any, ), new RunToolSearchOutputItem( { type: 'tool_search_output', id: 'ts_output_runtime_enabled_restore', status: 'completed', tools: [ { type: 'function', name: 'lookup_runtime', description: 'Look up a runtime record.', strict: true, parameters: { type: 'object', properties: {}, required: [], additionalProperties: false, }, }, ], providerData: { call_id: searchCallId, execution: 'client' }, } as protocol.ToolSearchOutputItem, agent as any, ), ); const toolCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_runtime_enabled_restore', callId: 'call_lookup_runtime', name: 'lookup_runtime', status: 'completed', arguments: '{}', }; state._lastProcessedResponse = { newItems: [], functions: [{ toolCall, tool: runtimeLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['lookup_runtime'], hasToolsOrApprovalsToRun: () => true, }; await expect( RunState.fromStringWithContext( agent, state.toString(), new RunContext({ enabled: false }), { contextStrategy: 'replace' }, ), ).rejects.toThrow( 'registered execute callback returned different runtime tools than the serialized state', ); }); it('rejects resumed function tools when isEnabled is false in replacement context', async () => { const lookupAccountParams = z.object({ accountId: z.string(), }); const crmLookup = toolNamespace({ name: 'crm', description: 'CRM tools', tools: [ tool({ name: 'lookup_account', description: 'Look up an account.', parameters: lookupAccountParams, isEnabled: async ({ runContext }) => runContext.context.enabled, execute: async () => 'crm', }), ], })[0]; const agent = new Agent({ name: 'CRM', tools: [crmLookup as any], }); const state = new RunState( new RunContext({ enabled: true }), '', agent, 1, ); const toolCall: protocol.FunctionCallItem = { type: 'function_call', id: 'fc_lookup', callId: 'call_lookup', name: 'lookup_account', namespace: 'crm', status: 'completed', arguments: '{"accountId":"acct_42"}', }; state._lastProcessedResponse = { newItems: [], functions: [{ toolCall, tool: crmLookup as any }], handoffs: [], computerActions: [], shellActions: [], applyPatchActions: [], mcpApprovalRequests: [], toolsUsed: ['crm.lookup_account'], hasToolsOrApprovalsToRun: () => true, }; await expect( RunState.fromStringWithContext( agent, state.toString(), new RunContext({ enabled: false }), { contextStrategy: 'replace' }, ), ).rejects.toThrow(/Tool .*lookup_account.* not found/); }); }); }