@graphorin/agent 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/README.md +31 -11
  3. package/dist/errors/index.d.ts +17 -4
  4. package/dist/errors/index.d.ts.map +1 -1
  5. package/dist/errors/index.js +19 -3
  6. package/dist/errors/index.js.map +1 -1
  7. package/dist/factory.d.ts.map +1 -1
  8. package/dist/factory.js +153 -1930
  9. package/dist/factory.js.map +1 -1
  10. package/dist/fanout/index.d.ts +13 -1
  11. package/dist/fanout/index.d.ts.map +1 -1
  12. package/dist/fanout/index.js +13 -4
  13. package/dist/fanout/index.js.map +1 -1
  14. package/dist/index.d.ts +7 -6
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +11 -9
  17. package/dist/index.js.map +1 -1
  18. package/dist/lateral-leak/index.js +1 -1
  19. package/dist/package.js +6 -0
  20. package/dist/package.js.map +1 -0
  21. package/dist/run-state/index.d.ts +32 -6
  22. package/dist/run-state/index.d.ts.map +1 -1
  23. package/dist/run-state/index.js +46 -22
  24. package/dist/run-state/index.js.map +1 -1
  25. package/dist/runtime/agent-surface.js +122 -0
  26. package/dist/runtime/agent-surface.js.map +1 -0
  27. package/dist/runtime/agent-to-tool.d.ts +51 -0
  28. package/dist/runtime/agent-to-tool.d.ts.map +1 -0
  29. package/dist/runtime/agent-to-tool.js +145 -0
  30. package/dist/runtime/agent-to-tool.js.map +1 -0
  31. package/dist/runtime/approvals.js +0 -0
  32. package/dist/runtime/approvals.js.map +1 -0
  33. package/dist/runtime/dispatch.js +108 -0
  34. package/dist/runtime/dispatch.js.map +1 -0
  35. package/dist/runtime/executor-wiring.js +128 -0
  36. package/dist/runtime/executor-wiring.js.map +1 -0
  37. package/dist/runtime/fallback-chain.js +139 -0
  38. package/dist/runtime/fallback-chain.js.map +1 -0
  39. package/dist/runtime/handoff.js +307 -0
  40. package/dist/runtime/handoff.js.map +1 -0
  41. package/dist/runtime/messages.d.ts +22 -0
  42. package/dist/runtime/messages.d.ts.map +1 -0
  43. package/dist/runtime/messages.js +204 -0
  44. package/dist/runtime/messages.js.map +1 -0
  45. package/dist/runtime/provider-events.js +117 -0
  46. package/dist/runtime/provider-events.js.map +1 -0
  47. package/dist/runtime/run-compaction.js +210 -0
  48. package/dist/runtime/run-compaction.js.map +1 -0
  49. package/dist/runtime/run-finish.js +48 -0
  50. package/dist/runtime/run-finish.js.map +1 -0
  51. package/dist/runtime/run-gates.js +336 -0
  52. package/dist/runtime/run-gates.js.map +1 -0
  53. package/dist/runtime/run-init.js +81 -0
  54. package/dist/runtime/run-init.js.map +1 -0
  55. package/dist/runtime/run-input.js +46 -0
  56. package/dist/runtime/run-input.js.map +1 -0
  57. package/dist/runtime/step-catalogue.js +173 -0
  58. package/dist/runtime/step-catalogue.js.map +1 -0
  59. package/dist/runtime/tool-call-walk.js +189 -0
  60. package/dist/runtime/tool-call-walk.js.map +1 -0
  61. package/dist/runtime/tool-wiring.js +159 -0
  62. package/dist/runtime/tool-wiring.js.map +1 -0
  63. package/dist/tooling/adapters.js +1 -1
  64. package/dist/tooling/dataflow.js +1 -1
  65. package/dist/tooling/policy.js +2 -0
  66. package/dist/tooling/policy.js.map +1 -1
  67. package/dist/types.d.ts +63 -13
  68. package/dist/types.d.ts.map +1 -1
  69. package/package.json +20 -20
  70. package/src/errors/index.ts +320 -0
  71. package/src/evaluator-optimizer/index.ts +212 -0
  72. package/src/factory.ts +957 -0
  73. package/src/fallback/index.ts +108 -0
  74. package/src/fanout/index.ts +523 -0
  75. package/src/filters/index.ts +347 -0
  76. package/src/index.ts +180 -0
  77. package/src/internal/ids.ts +46 -0
  78. package/src/internal/usage-accumulator.ts +90 -0
  79. package/src/lateral-leak/causality-monitor.ts +221 -0
  80. package/src/lateral-leak/index.ts +35 -0
  81. package/src/lateral-leak/merge-guard.ts +151 -0
  82. package/src/lateral-leak/protocol-guard.ts +222 -0
  83. package/src/preferred-model/index.ts +210 -0
  84. package/src/progress/index.ts +238 -0
  85. package/src/run-state/index.ts +607 -0
  86. package/src/runtime/agent-surface.ts +218 -0
  87. package/src/runtime/agent-to-tool.ts +323 -0
  88. package/src/runtime/approvals.ts +0 -0
  89. package/src/runtime/dispatch.ts +183 -0
  90. package/src/runtime/executor-wiring.ts +331 -0
  91. package/src/runtime/fallback-chain.ts +250 -0
  92. package/src/runtime/handoff.ts +428 -0
  93. package/src/runtime/messages.ts +309 -0
  94. package/src/runtime/provider-events.ts +175 -0
  95. package/src/runtime/run-compaction.ts +288 -0
  96. package/src/runtime/run-finish.ts +93 -0
  97. package/src/runtime/run-gates.ts +419 -0
  98. package/src/runtime/run-init.ts +169 -0
  99. package/src/runtime/run-input.ts +102 -0
  100. package/src/runtime/step-catalogue.ts +338 -0
  101. package/src/runtime/tool-call-walk.ts +301 -0
  102. package/src/runtime/tool-wiring.ts +218 -0
  103. package/src/testing/replay-provider.ts +121 -0
  104. package/src/tooling/adapters.ts +403 -0
  105. package/src/tooling/catalogue.ts +36 -0
  106. package/src/tooling/dataflow.ts +171 -0
  107. package/src/tooling/plan.ts +123 -0
  108. package/src/tooling/policy.ts +67 -0
  109. package/src/tooling/registry-build.ts +191 -0
  110. package/src/types.ts +696 -0
@@ -1 +1 @@
1
- {"version":3,"file":"factory.js","names":["lastError: unknown","executeTool: CodeExecuteBridge","CANONICAL_PROVIDER_ERROR_KINDS: ReadonlySet<string>","parts: MessageContent[]","out: Message[]","parts: string[]","fallbackPolicy: AgentFallbackPolicy","subAgent: Agent<TDeps, unknown>","pendingSteer: Message[]","pendingFollowUp: Message[]","abortController: AbortController | undefined","pendingAbort: AbortOptions | undefined","activeRunState: RunState | undefined","activeRunCapability: 'read-only' | undefined","externalEventQueue: AgentEvent<TOutput>[]","pendingManualCompacts: PendingManualCompact[]","memory: Memory | undefined","progressIO: ProgressIO","resultReader: ResultReader","activeExecutorBridge: ExecutorEventBridge | undefined","toolApprovalGate: NonNullable<ExecutorOptions['approvalGate']>","toolStreamingSink: NonNullable<ExecutorOptions['streamingSink']>","codeModeAdvertised: ReadonlyArray<Tool<unknown, unknown, TDeps>>","messages: Message[]","instructionsText: string","finalSnapshot: InternalRunSnapshot<TOutput>","currentStepSpan: AISpan<'agent.step'> | undefined","rewritten: Message","resumedApprovedCalls: ToolCall[]","grantedApprovals: ToolApproval[]","stillPending: ToolApproval[]","runContextBase: RunContext<TDeps>","rebuilt: Message[]","envelope: CompactionEnvelope","lastStepCalledToolNames: ReadonlyArray<string>","stepCtx: RunContext<TDeps>","stepRegistry: ToolRegistry","stepExecutor: ToolExecutor","handoffTools: Tool<unknown, unknown, TDeps>[]","stepTools: ReadonlyArray<Tool<unknown, unknown, TDeps>>","primary: PreferredModelResolution","fallbackChain: Provider[]","toolDefs: ReadonlyArray<ToolDefinition>","baseRequest: ProviderRequest","stepUsage: Usage","lastError: ProviderError | undefined","finalCalls: ToolCall[]","stepReasoningParts: ReasoningContent[]","evState: ProviderEventCollector","providerError: ProviderError | undefined","providerStepUsage: Usage","stream","assistant: AssistantMessage","stepRecord: RunStep","execRunContext: RunContext<TDeps>","batch: ToolCall[]","filterLib","handoffRec: HandoffRecord","subOutputs: string[]","subResult: AgentResult<unknown> | undefined","toolError: ToolError","completed: CompletedToolCall","gateInput: unknown","approval: ToolApproval","feedbacks: string[]","result: VerifierResult","feedbackMessage: Message","parsed: unknown","callOpts: AgentCallOptions<TDeps>","seed: AgentInput","turns: string[]","endResult: AgentResult<TOutput> | undefined","progress: AgentProgressIO","probeCtx: ToolExecutionContext"],"sources":["../src/factory.ts"],"sourcesContent":["/**\n * `createAgent({...})` - the agent factory entry point.\n *\n * Wires the typed `model -> tool calls -> model` loop, the streamed\n * event surface, the steering / followUp queues, durable HITL via\n * `RunState`, the multi-agent handoff layer, the agent-level model\n * fallback chain, and the per-tool preferred-model resolution.\n *\n * Custom adapters override behaviour by supplying alternative\n * `Provider` / `Memory` / `CheckpointStore` instances; the loop\n * never reaches into adapter internals.\n *\n * @packageDocumentation\n */\n\nimport { createHash } from 'node:crypto';\nimport type {\n AgentEvent,\n AgentResult,\n AISpan,\n AssistantMessage,\n CompletedToolCall,\n HandoffRecord,\n Message,\n MessageContent,\n ModelSpec,\n Provider,\n ProviderError,\n ProviderErrorKind,\n ProviderEvent,\n ProviderRequest,\n ReasoningContent,\n ReasoningRetention,\n RunContext,\n RunState,\n RunStep,\n Sensitivity,\n Tool,\n ToolApproval,\n ToolCall,\n ToolChoice,\n ToolDefinition,\n ToolDefinitionExample,\n ToolError,\n ToolExecutionContext,\n Usage,\n} from '@graphorin/core';\nimport { isStepCount, NOOP_LOGGER, NOOP_TRACER, zeroUsage } from '@graphorin/core';\nimport type { Memory } from '@graphorin/memory';\n\n/** Return envelope of `ContextEngine.compactNow` (shared splice paths, CE-3). */\ntype CompactionEnvelope = Awaited<ReturnType<Memory['contextEngine']['compactNow']>>;\n\n/**\n * Replacement content committed in place of assistant commentary the\n * causality monitor blocked (AG-10). Worded to not match any built-in\n * denial pattern, so the notice itself never re-triggers the monitor.\n */\nconst LATERAL_LEAK_BLOCKED_NOTICE =\n '[graphorin] assistant commentary withheld by the lateral-leak defense.';\n\nimport { composeGuardrails } from '@graphorin/security/guardrails';\nimport { createReadResultTool, createToolSearchTool } from '@graphorin/tools/built-in';\nimport {\n type CodeExecuteBridge,\n createCodeExecuteTool,\n createCodeSearchTool,\n type ProjectableTool,\n projectToolApi,\n} from '@graphorin/tools/code-mode';\nimport {\n createToolExecutor,\n type ExecutorOptions,\n type ToolExecutor,\n} from '@graphorin/tools/executor';\nimport type { ToolRegistry } from '@graphorin/tools/registry';\nimport {\n createDefaultSpillWriter,\n createFileResultReader,\n type ResultReader,\n} from '@graphorin/tools/result';\nimport { projectSchemaToJsonSchema } from '@graphorin/tools/schema';\nimport {\n AgentRuntimeError,\n ConcurrentRunError,\n InvalidAgentConfigError,\n InvalidPreferredModelError,\n MultipleHandoffsInStepError,\n ToolNotFoundError,\n} from './errors/index.js';\nimport { type AgentFallbackPolicy, isAgentFallbackEligible } from './fallback/index.js';\nimport {\n type FanOutResult,\n type FanOutOptions as RunFanOutOptions,\n runFanOut,\n} from './fanout/index.js';\nimport { type DescribedFilter, filters as filterLib } from './filters/index.js';\nimport { newId } from './internal/ids.js';\nimport { InMemoryUsageAccumulator } from './internal/usage-accumulator.js';\nimport { CausalityMonitor } from './lateral-leak/causality-monitor.js';\nimport { type PreferredModelResolution, resolvePreferredModel } from './preferred-model/index.js';\nimport {\n createProgressIO,\n type ProgressIO,\n type ProgressReadOptions,\n type ProgressWriteOptions,\n} from './progress/index.js';\nimport { addModelUsage, createInitialRunState, serializeRunState } from './run-state/index.js';\nimport {\n buildMemoryGuard,\n buildSecretResolver,\n buildToolTokenCounter,\n createExecutorEventBridge,\n createMemoryRegionReader,\n type ExecutorEventBridge,\n} from './tooling/adapters.js';\nimport { orderPromotedTools } from './tooling/catalogue.js';\nimport { buildDataFlowGuard } from './tooling/dataflow.js';\nimport { createPlanTool, PLAN_TOOL_NAME, renderPlanRecitation } from './tooling/plan.js';\nimport { buildToolArgumentPolicy } from './tooling/policy.js';\nimport { buildToolRegistry } from './tooling/registry-build.js';\nimport type {\n AbortOptions,\n Agent,\n AgentCallOptions,\n AgentConfig,\n AgentFanOutOptions,\n AgentInput,\n AgentProgressIO,\n AgentToToolOptions,\n CompactionApiResult,\n CompactOptions,\n VerifierResult,\n} from './types.js';\n\nconst sha256Hex = (input: string): string =>\n createHash('sha256').update(input, 'utf8').digest('hex');\n\nconst HANDOFF_TOOL_PREFIX = 'transfer_to_';\n\n/** The built-in deferred-discovery tool's stable name (WI-05 / P0-3). */\nconst TOOL_SEARCH_NAME = 'tool_search';\n\n/**\n * Register the built-in `tool_search` into `registry` when - and only\n * when - the registry holds at least one deferred tool\n * (`defer_loading: true`). `tool_search` is itself eager (so it is\n * always advertised while a deferred pool exists) and resolvable by the\n * executor like any other tool, so a model can both *see* it in the\n * per-step catalogue and *call* it.\n *\n * No-op when nothing defers (zero overhead - the tool never appears) or\n * when a user tool already occupies the name (the user's tool wins; we\n * never clobber it). Because deferral is decided at registration time\n * (`normaliseTool`), the deferred set is fixed for the life of the\n * registry - this runs once per registry, not per step.\n */\nfunction registerToolSearch(registry: ToolRegistry, availability?: 'next-step' | 'next-run'): void {\n if (registry.listDeferred().length === 0) return;\n if (registry.get(TOOL_SEARCH_NAME) !== undefined) return;\n registry.register(\n createToolSearchTool({ registry, ...(availability !== undefined ? { availability } : {}) }),\n {\n kind: 'built-in',\n subsystem: 'tool-discovery',\n },\n );\n}\n\n/** The built-in result-handle reader tool's stable name (WI-10 / P1-4). */\nconst READ_RESULT_NAME = 'read_result';\n\n/**\n * Register the built-in `read_result` into `registry` when at least one\n * registered tool opts into the `'spill-to-file'` truncation strategy\n * (the sole producer of spill handles today) - or when `force` is set,\n * which the agent passes when an operator wires external result readers\n * (e.g. an MCP `resource_link` reader; WI-13). The tool is eager, so it\n * is advertised alongside the producing tool and the model can fetch a\n * handle back on demand instead of inlining the full blob. No-op when\n * nothing produces handles (zero overhead) or when a user tool already\n * occupies the name (the user's tool wins).\n */\nfunction registerReadResult(\n registry: ToolRegistry,\n reader: ResultReader,\n opts?: { readonly force?: boolean },\n): void {\n if (\n opts?.force !== true &&\n !registry.list().some((entry) => entry.truncationStrategy === 'spill-to-file')\n ) {\n return;\n }\n if (registry.get(READ_RESULT_NAME) !== undefined) return;\n registry.register(createReadResultTool({ reader }), {\n kind: 'built-in',\n subsystem: 'result-handle',\n });\n}\n\n/**\n * Compose result readers into one that tries each in order, returning\n * the first that resolves the handle (WI-13). The spill-file reader is\n * placed first so `graphorin-spill:` handles resolve locally; operator\n * readers (e.g. an MCP resource reader) resolve the rest. Each reader\n * rejects handles it does not own, so resolution falls through cleanly.\n */\nfunction composeResultReaders(readers: ReadonlyArray<ResultReader>): ResultReader {\n return {\n async read(uri, range) {\n let lastError: unknown;\n for (const r of readers) {\n try {\n return await r.read(uri, range);\n } catch (err) {\n lastError = err;\n }\n }\n throw lastError instanceof Error\n ? lastError\n : new Error(`No result reader resolved handle ${JSON.stringify(uri)}.`);\n },\n };\n}\n\n/** The code-mode meta-tools' stable names (WI-11 / P1-2). */\nconst CODE_EXECUTE_NAME = 'code_execute';\nconst CODE_SEARCH_NAME = 'code_search';\n\n/** Structural check: is this tool approval-gated (static or predicate form)? */\nconst isApprovalGated = (t: { readonly needsApproval?: unknown }): boolean =>\n t.needsApproval === true || typeof t.needsApproval === 'function';\n\n/**\n * Wire code-mode (P1-2) into `registry`: register the `code_search` /\n * `code_execute` meta-tools and return them as the tools to advertise in\n * place of the full catalogue. The model reaches every other tool through\n * `code_execute`, whose in-script `tools.<name>(args)` calls route back\n * through `quietExecutor.executeOne(...)` under the calling step's\n * `runContext` - so per-tool ACL / sanitization / truncation still apply,\n * exactly as in direct mode. `quietExecutor` carries no `streamingSink`,\n * so the inner calls do not interleave `tool.execute.*` events into the\n * outer stream.\n *\n * Excluded from the code API (`reservedNames`): the meta-tools, the\n * discovery / handle built-ins, handoff tools (which stay first-class\n * provider tools), and - supplied by the caller - any approval-gated\n * tool, since code-mode has no durable-HITL path to suspend mid-script.\n *\n * Returns `[]` (registering nothing) when no real tool is exposable.\n */\nfunction registerCodeMode(\n registry: ToolRegistry,\n quietExecutor: ToolExecutor,\n reservedNames: ReadonlySet<string>,\n getRunCapability?: () => 'read-only' | undefined,\n): ReadonlyArray<Tool<unknown, unknown, unknown>> {\n if (registry.get(CODE_EXECUTE_NAME) !== undefined) return []; // already wired\n const codeTools = registry\n .list()\n .filter((entry) => !reservedNames.has(entry.name) && !isApprovalGated(entry));\n if (codeTools.length === 0) return [];\n\n // TL-8: gated tools cannot suspend for HITL mid-script, so they are\n // excluded from the code API - but VISIBLY: they appear in the\n // catalogue/search with a call-directly marker, and a bridged call\n // fails with the same hint instead of an opaque unknown-tool error.\n const approvalGatedTools = registry\n .list()\n .filter((entry) => !reservedNames.has(entry.name) && isApprovalGated(entry))\n .map((entry) => entry.name);\n const approvalGatedSet = new Set(approvalGatedTools);\n\n const allowedTools = [...codeTools.map((entry) => entry.name), ...approvalGatedTools];\n const allowedSet = new Set(codeTools.map((entry) => entry.name));\n const eagerProjectable = codeTools.filter(\n (entry) => entry.__effectiveDeferLoading !== true,\n ) as unknown as ReadonlyArray<ProjectableTool>;\n const projection = projectToolApi(eagerProjectable);\n\n const executeTool: CodeExecuteBridge = async (call, ctx) => {\n if (approvalGatedSet.has(call.name)) {\n throw new Error(\n `${call.name} requires human approval and cannot run inside code_execute - call it directly as a standalone tool call so the run can suspend for the approval.`,\n );\n }\n const runCapability = getRunCapability?.();\n const completed = await quietExecutor.executeOne({\n call: { toolCallId: newId('codecall'), toolName: call.name, args: call.args },\n runContext: ctx.runContext,\n stepNumber: ctx.runContext.stepNumber,\n // D2: in-script calls inherit the active run's capability.\n ...(runCapability !== undefined ? { capability: runCapability } : {}),\n });\n const { outcome } = completed;\n if ('kind' in outcome) throw new Error(`${call.name}: ${outcome.message}`);\n return outcome.output;\n };\n\n const codeSearch = createCodeSearchTool({\n projection,\n approvalGatedTools,\n searchDeferred: async (query, k) =>\n (await registry.searchDeferred(query, k)).filter((match) => allowedSet.has(match.name)),\n });\n const codeExecute = createCodeExecuteTool({\n projection,\n allowedTools,\n executeTool,\n approvalGatedTools,\n });\n registry.register(codeSearch, { kind: 'built-in', subsystem: 'code-mode' });\n registry.register(codeExecute, { kind: 'built-in', subsystem: 'code-mode' });\n return [codeSearch, codeExecute] as ReadonlyArray<Tool<unknown, unknown, unknown>>;\n}\n\n/**\n * Fold a completed `tool_search` result into the per-run promotion set:\n * every matched tool name becomes advertised (and thus callable) on the\n * next step. Tolerant of unexpected shapes (e.g. a user-shadowed\n * `tool_search`) - only string `name`s inside a `matches` array promote.\n */\nfunction recordToolSearchPromotions(output: unknown, promoted: Set<string>): void {\n if (typeof output !== 'object' || output === null) return;\n const matches = (output as { readonly matches?: unknown }).matches;\n if (!Array.isArray(matches)) return;\n for (const match of matches) {\n const name = (match as { readonly name?: unknown } | null)?.name;\n if (typeof name === 'string') promoted.add(name);\n }\n}\n\n/**\n * Internal mutable view of {@link RunState}. The public type marks\n * most fields `readonly` to guard against accidental mutation by\n * consumers; the runtime owns the lifecycle and writes through\n * this view.\n */\ninterface MutableRunState {\n status: RunState['status'];\n currentAgentId: string;\n readonly steps: RunStep[];\n readonly messages: Message[];\n readonly pendingApprovals: ToolApproval[];\n readonly handoffs: HandoffRecord[];\n usage: Usage;\n error?: RunState['error'];\n finishedAt?: string;\n usageByModel?: RunState['usageByModel'];\n}\n\ninterface InternalRunSnapshot<TOutput> {\n output: TOutput;\n}\n\nfunction isModelHintLike(value: unknown): boolean {\n return value === 'fast' || value === 'balanced' || value === 'smart';\n}\n\nfunction isModelSpecLike(value: unknown): boolean {\n if (typeof value !== 'object' || value === null) return false;\n const v = value as Record<string, unknown>;\n if (typeof v.modelId === 'string' && typeof v.name === 'string') return true;\n if (typeof v.provider === 'object' && v.provider !== null && typeof v.model === 'string') {\n return true;\n }\n return false;\n}\n\nfunction validatePreferredModel(value: unknown): void {\n if (value === undefined) return;\n if (isModelHintLike(value)) return;\n if (isModelSpecLike(value)) return;\n throw new InvalidPreferredModelError(value);\n}\n\nfunction isMessageObject(value: unknown): value is Message {\n if (typeof value !== 'object' || value === null) return false;\n const role = (value as { role?: unknown }).role;\n return role === 'system' || role === 'user' || role === 'assistant' || role === 'tool';\n}\n\nfunction isRunStateObject(value: unknown): value is RunState {\n if (typeof value !== 'object' || value === null) return false;\n const v = value as Record<string, unknown>;\n return (\n typeof v.id === 'string' &&\n typeof v.agentId === 'string' &&\n Array.isArray(v.messages) &&\n Array.isArray(v.steps)\n );\n}\n\nfunction asMessages(input: AgentInput | RunState): {\n readonly seed: Message[];\n readonly resumed?: RunState;\n} {\n if (typeof input === 'string') {\n return { seed: [{ role: 'user', content: input }] };\n }\n if (Array.isArray(input)) {\n return { seed: [...input] as Message[] };\n }\n if (isMessageObject(input)) {\n return { seed: [input] };\n }\n if (isRunStateObject(input)) {\n return { seed: [], resumed: input };\n }\n throw new InvalidAgentConfigError(`unrecognized AgentInput shape`);\n}\n\n/**\n * Strip a single Markdown code fence around a JSON payload (AG-3).\n * Models often wrap structured output in ```json fences even when told\n * not to. ReDoS-safe: the info string is matched with `[^\\n]*`.\n */\nfunction stripJsonFence(text: string): string {\n const trimmed = text.trim();\n const match = /^```[^\\n]*\\n([\\s\\S]*?)\\n?```$/.exec(trimmed);\n return match?.[1] ?? trimmed;\n}\n\n/**\n * The AG-3 fallback instruction: one trailing system message that\n * pins JSON-only output and embeds the wire schema / description.\n * This is the documented structured-output contract for adapters that\n * do not yet consume `ProviderRequest.outputType` natively (PS-24).\n */\nfunction buildStructuredInstruction(spec: {\n readonly description?: string;\n readonly jsonSchema?: Readonly<Record<string, unknown>>;\n}): string {\n const parts = [\n 'Respond with a single valid JSON value only - no prose, no Markdown code fences.',\n ];\n if (spec.description !== undefined && spec.description.length > 0) {\n parts.push(spec.description);\n }\n if (spec.jsonSchema !== undefined) {\n parts.push(`The JSON MUST conform to this JSON Schema:\\n${JSON.stringify(spec.jsonSchema)}`);\n }\n return parts.join('\\n');\n}\n\n/** Most-recent user-role text in `messages` (for context-engine auto-recall). */\nfunction lastUserText(messages: ReadonlyArray<Message>): string | undefined {\n for (let i = messages.length - 1; i >= 0; i--) {\n const m = messages[i];\n if (m?.role !== 'user') continue;\n if (typeof m.content === 'string') return m.content;\n const text = m.content\n .filter((p): p is { readonly type: 'text'; readonly text: string } => p.type === 'text')\n .map((p) => p.text)\n .join(' ');\n return text.length > 0 ? text : undefined;\n }\n return undefined;\n}\n\n/**\n * AG-21: classify a **thrown** provider error into a {@link ProviderErrorKind}\n * so the fallback chain can act on it, instead of flattening every exception to\n * `'unknown'` (which is always fallback-ineligible). Structural - reads the\n * `kind` carried by `@graphorin/provider`'s `GraphorinProviderError` subclasses\n * without importing them, keeping the agent decoupled from the provider package.\n */\n/** Canonical `ProviderErrorKind` values honoured off a thrown error's `errorKind`. */\nconst CANONICAL_PROVIDER_ERROR_KINDS: ReadonlySet<string> = new Set([\n 'rate-limit',\n 'capacity',\n 'context-length',\n 'transient',\n 'invalid-request',\n 'unauthorized',\n 'content-filter',\n]);\n\nfunction classifyThrownProviderErrorKind(cause: unknown): ProviderErrorKind {\n if (typeof cause === 'object' && cause !== null) {\n // B1: `ProviderHttpError` carries the canonical mapped kind on\n // `errorKind` (its `kind` stays the stable 'provider-http'\n // discriminant) - honour it so a thrown 429 / context overflow is\n // classified like the structured-event equivalent.\n const errorKind = (cause as { readonly errorKind?: unknown }).errorKind;\n if (typeof errorKind === 'string' && CANONICAL_PROVIDER_ERROR_KINDS.has(errorKind)) {\n return errorKind as ProviderErrorKind;\n }\n switch ((cause as { readonly kind?: unknown }).kind) {\n case 'rate-limit-exceeded':\n return 'rate-limit';\n }\n }\n return 'unknown';\n}\n\n/** WARN-once keys for schemas the projection cannot read (per process). */\nconst unprojectableSchemaWarned = new Set<string>();\n\n/**\n * Resolve a tool's declared schema - plain Zod (v3/v4), `toJSON()`-bearing,\n * or already-JSON-Schema data - to a JSON Schema record via the shared\n * projection (tools-01). Pre-fix this only honoured `toJSON()` and passed\n * everything else through verbatim, so every plain-Zod tool serialised as\n * `{\"_def\":...}` internals on OpenAI-shaped/Ollama/vercel wire bodies.\n * `undefined` when nothing usable can be projected (caller substitutes a\n * permissive `{}`), with a WARN so the degradation is never silent.\n */\nfunction projectSchema(\n raw: unknown,\n toolName: string,\n slot: 'input' | 'output',\n): Readonly<Record<string, unknown>> | undefined {\n return projectSchemaToJsonSchema(raw, {\n onUnsupported: (detail) => {\n const key = `${toolName}:${slot}:${detail}`;\n if (unprojectableSchemaWarned.has(key)) return;\n unprojectableSchemaWarned.add(key);\n console.warn(\n `[graphorin/agent] tool '${toolName}' ${slot} schema: '${detail}' cannot be projected ` +\n 'to JSON Schema - that fragment degrades to a permissive {} on the provider wire body.',\n );\n },\n });\n}\n\nfunction toolToDefinition(tool: Tool): ToolDefinition {\n const ts = tool as Tool & {\n readonly inputSchema?: unknown;\n readonly outputSchema?: unknown;\n };\n const inputSchema = projectSchema(ts.inputSchema, tool.name, 'input') ?? {};\n // A5: project the output schema so structured-output providers + typed\n // code-mode see the tool's result shape.\n const outputSchema = projectSchema(ts.outputSchema, tool.name, 'output');\n const examples = renderToolExamples(tool);\n return {\n name: tool.name,\n description: tool.description,\n inputSchema,\n ...(outputSchema !== undefined ? { outputSchema } : {}),\n ...(examples !== undefined ? { examples } : {}),\n };\n}\n\n/**\n * Project a tool's worked `examples` onto the provider wire contract\n * (WI-06 / P2-3). Examples are rendered only when the tool eagerly\n * renders them: the registry resolves the `defer_loading` auto-rule into\n * `examplesEagerlyRendered`, so a deferred tool resolves to `false` and is\n * skipped (its examples stay out of context even once `tool_search`\n * promotes it). `undefined` - the \"runtime decides\" case for a plain\n * eager tool - renders, since the tool is already advertised this step.\n *\n * Bounded to ≤5 to honour the `ToolExample` `[1,5]` contract even when a\n * tool slipped past the registry's overflow WARN (which does not truncate).\n */\nfunction renderToolExamples(tool: Tool): ReadonlyArray<ToolDefinitionExample> | undefined {\n const examples = tool.examples;\n if (examples === undefined || examples.length === 0) return undefined;\n if (tool.examplesEagerlyRendered === false) return undefined;\n return examples.slice(0, 5).map((ex) => ({\n input: ex.input,\n output: ex.output,\n ...(ex.comment !== undefined ? { comment: ex.comment } : {}),\n }));\n}\n\nconst PASSTHROUGH_SCHEMA = {\n parse: <T>(value: unknown): T => value as T,\n safeParse: <T>(value: unknown) => ({ success: true as const, data: value as T }),\n toJSON: (): Record<string, unknown> => ({ type: 'object' }),\n} as const;\n\nfunction isDescribedFilter(value: unknown): value is DescribedFilter {\n return (\n typeof value === 'function' &&\n 'descriptor' in value &&\n typeof (value as DescribedFilter).descriptor === 'object'\n );\n}\n\nfunction buildHandoffTool<TDeps>(target: Agent<TDeps, unknown>): Tool<unknown, unknown, TDeps> {\n const cfg = target.config;\n const name = `${HANDOFF_TOOL_PREFIX}${cfg.name}`;\n const tool: Tool<unknown, unknown, TDeps> = {\n name,\n description: `Hand off control to agent '${cfg.name}'.`,\n inputSchema: PASSTHROUGH_SCHEMA as unknown as Tool<unknown, unknown, TDeps>['inputSchema'],\n sideEffectClass: 'pure',\n async execute(): Promise<string> {\n return `[handoff: ${cfg.name}]`;\n },\n };\n return tool;\n}\n\nfunction specToProvider(spec: ModelSpec): Provider {\n if ('provider' in spec) return spec.provider as Provider;\n return spec as Provider;\n}\n\ninterface ToolCallAccumulator {\n readonly toolCallId: string;\n toolName: string;\n argsBuffer: string;\n}\n\ninterface ProviderEventOutcome {\n readonly emit?: AgentEvent;\n readonly providerError?: ProviderError;\n readonly usage?: Usage;\n readonly finished?: boolean;\n}\n\ninterface ProviderEventCollector {\n textBuffer: string;\n reasoningBuffer: string;\n reasoningParts: ReasoningContent[];\n calls: Map<string, ToolCallAccumulator>;\n finalCalls: ToolCall[];\n}\n\nfunction handleProviderEvent(\n ev: ProviderEvent,\n state: ProviderEventCollector,\n): ProviderEventOutcome {\n switch (ev.type) {\n case 'stream-start':\n return {};\n case 'reasoning-delta':\n state.reasoningBuffer += ev.delta;\n return { emit: { type: 'reasoning.delta', delta: ev.delta } };\n case 'text-delta':\n state.textBuffer += ev.delta;\n return { emit: { type: 'text.delta', delta: ev.delta } };\n case 'tool-call-start':\n state.calls.set(ev.toolCallId, {\n toolCallId: ev.toolCallId,\n toolName: ev.toolName,\n argsBuffer: '',\n });\n return {\n emit: {\n type: 'tool.call.start',\n toolCallId: ev.toolCallId,\n toolName: ev.toolName,\n args: undefined,\n },\n };\n case 'tool-call-input-delta': {\n const acc = state.calls.get(ev.toolCallId);\n if (acc !== undefined) acc.argsBuffer += ev.argsDelta;\n return {\n emit: { type: 'tool.call.delta', toolCallId: ev.toolCallId, argsDelta: ev.argsDelta },\n };\n }\n case 'tool-call-end': {\n const acc = state.calls.get(ev.toolCallId);\n if (acc === undefined) {\n // AG-26: an end without a matching start has no tool name - the\n // old path dispatched it as the unknown tool ''. Drop it loudly.\n process.stderr.write(\n `[graphorin/agent] dropped tool-call-end '${ev.toolCallId}' with no matching tool-call-start.\\n`,\n );\n return {};\n }\n state.finalCalls.push({\n toolCallId: ev.toolCallId,\n toolName: acc.toolName,\n args: ev.finalArgs,\n });\n return {\n emit: { type: 'tool.call.end', toolCallId: ev.toolCallId, finalArgs: ev.finalArgs },\n };\n }\n // AG-26: provider-generated files / citations are consumer-observable\n // events instead of silently vanishing.\n case 'file':\n return { emit: { type: 'file.generated', mimeType: ev.mimeType, data: ev.data } };\n case 'source':\n return {\n emit: {\n type: 'source.cited',\n uri: ev.uri,\n ...(ev.title !== undefined ? { title: ev.title } : {}),\n },\n };\n case 'finish':\n return { usage: ev.usage, finished: true };\n case 'error':\n return { providerError: ev.error };\n default: {\n const _exhaustive: never = ev;\n void _exhaustive;\n return {};\n }\n }\n}\n\n/**\n * Resolve the effective {@link ReasoningRetention} for a step. The\n * agent-level setting wins over the provider-level default; when\n * neither is supplied, the provider's `reasoningContract`\n * capability drives the default per RB-42 / suggested DEC-158.\n */\nfunction effectiveReasoningRetention(\n agentOverride: ReasoningRetention | undefined,\n provider: Provider,\n): ReasoningRetention {\n if (agentOverride !== undefined) return agentOverride;\n const contract = provider.capabilities.reasoningContract;\n switch (contract) {\n case 'round-trip-required':\n return 'pass-through-claude';\n case 'optional':\n return 'pass-through-all';\n case 'hidden':\n return 'strip';\n default:\n return 'strip';\n }\n}\n\n/**\n * Build the assistant message that the runtime appends to the\n * message buffer after a successful provider call. When the\n * effective {@link ReasoningRetention} is not `'strip'`, the\n * assembled `reasoning` content parts ride along on `content` so\n * the next provider call honours the wire-correct round-trip\n * contract per RB-42.\n */\nfunction buildAssistantMessage(\n text: string,\n reasoningParts: ReadonlyArray<ReasoningContent>,\n toolCalls: ReadonlyArray<ToolCall>,\n agentId: string,\n retention: ReasoningRetention,\n): AssistantMessage {\n const preserveReasoning = retention !== 'strip' && reasoningParts.length > 0;\n if (preserveReasoning) {\n const parts: MessageContent[] = [...reasoningParts];\n if (text.length > 0) parts.push({ type: 'text', text });\n return {\n role: 'assistant',\n content: parts,\n ...(toolCalls.length > 0 ? { toolCalls } : {}),\n agentId,\n };\n }\n return {\n role: 'assistant',\n content: text,\n ...(toolCalls.length > 0 ? { toolCalls } : {}),\n agentId,\n };\n}\n\n/**\n * Strip every {@link ReasoningContent} part from each message in\n * the supplied list. Used at the swap point when `prepareStep`\n * downgrades the provider's `reasoningContract` mid-run.\n */\nfunction stripReasoningFromMessages(messages: Message[]): { stripped: number } {\n let stripped = 0;\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n if (msg === undefined) continue;\n if (msg.role === 'system' || msg.role === 'tool') continue;\n if (typeof msg.content === 'string') continue;\n const filtered = msg.content.filter((p) => p.type !== 'reasoning');\n if (filtered.length === msg.content.length) continue;\n stripped += msg.content.length - filtered.length;\n if (msg.role === 'assistant') {\n messages[i] = { ...msg, content: filtered };\n } else {\n messages[i] = { ...msg, content: filtered };\n }\n }\n return { stripped };\n}\n\n/**\n * Count the leading contiguous run of `system` messages in the initial\n * buffer - the trusted, KV-cache-stable instruction prefix. Captured\n * once at run start (WI-09 / P1-1): auto-compaction summarises only the\n * messages after this prefix, so the prefix stays byte-identical across\n * every step (the provider's cache breakpoint is real) and a long run\n * never re-pays for the system prompt.\n *\n * The length is fixed for the run rather than re-derived per compaction\n * on purpose: each compaction inserts its summary as a `system` message\n * right after the prefix, so re-scanning the leading run would absorb\n * that summary into the prefix and shield it from the next compaction -\n * summaries would stack unbounded. Pinning the original length keeps\n * each prior summary inside the compactable body, where the next pass\n * folds it into a fresh summary-of-summary.\n */\n/**\n * Marker prefix stamped on every compaction summary (see the memory\n * package's summary template). context-engine-05: the prefix scan must\n * stop at it - after a compact-then-suspend cycle the summary is a\n * SYSTEM message sitting right after the true prefix, and counting it\n * in would pin it (and every later summary) outside the compactable\n * window forever, growing the uncompactable prefix by one summary per\n * cycle.\n */\nconst COMPACTION_SUMMARY_MARKER = '<graphorin_compaction_summary';\n\nfunction countLeadingSystemMessages(messages: ReadonlyArray<Message>): number {\n let i = 0;\n while (i < messages.length) {\n const msg = messages[i];\n if (msg?.role !== 'system') break;\n if (typeof msg.content === 'string' && msg.content.startsWith(COMPACTION_SUMMARY_MARKER)) {\n break;\n }\n i += 1;\n }\n return i;\n}\n\n/**\n * Immutable usage sum. Optional token fields (reasoning + prompt-cache\n * legs, core-provider-02) appear in the result only when at least one\n * side carries them, so pre-cache serialized shapes stay byte-identical.\n */\n/**\n * D6: assemble the per-step request messages. Trailing, request-only\n * additions ride the LAST prompt-cache anchor and never touch the shared\n * `messages` buffer or the persisted RunState: the structured-output\n * instruction (AG-3) and the attention-recitation plan block are both\n * appended here, in that order, so the stable prompt prefix is unchanged\n * and only the small trailing tail is re-sent each step.\n */\nfunction buildStepMessages(\n messages: ReadonlyArray<Message>,\n structuredInstruction: string | undefined,\n todos: ReadonlyArray<import('@graphorin/core').TodoItem> | undefined,\n): Message[] {\n const out: Message[] = [...messages];\n if (structuredInstruction !== undefined) {\n out.push({ role: 'system', content: structuredInstruction });\n }\n const recitation = renderPlanRecitation(todos);\n if (recitation !== null) {\n out.push({ role: 'system', content: recitation });\n }\n return out;\n}\n\nfunction addUsage(a: Usage, b: Usage): Usage {\n const optional = (x: number | undefined, y: number | undefined): number | undefined =>\n x === undefined && y === undefined ? undefined : (x ?? 0) + (y ?? 0);\n const reasoningTokens = optional(a.reasoningTokens, b.reasoningTokens);\n const cachedReadTokens = optional(a.cachedReadTokens, b.cachedReadTokens);\n const cacheWriteTokens = optional(a.cacheWriteTokens, b.cacheWriteTokens);\n return {\n promptTokens: a.promptTokens + b.promptTokens,\n completionTokens: a.completionTokens + b.completionTokens,\n totalTokens: a.totalTokens + b.totalTokens,\n ...(reasoningTokens !== undefined ? { reasoningTokens } : {}),\n ...(cachedReadTokens !== undefined ? { cachedReadTokens } : {}),\n ...(cacheWriteTokens !== undefined ? { cacheWriteTokens } : {}),\n };\n}\n\n/**\n * C3: render a ToolError for the model. The first line keeps the\n * long-standing `Error: <message>` shape; a bracketed second line carries\n * the typed kind + the recovery envelope, which is what actually changes\n * model behaviour after a failure (retry vs. fix args vs. give up).\n */\nfunction renderToolErrorMessage(error: ToolError): string {\n const parts: string[] = [`kind: ${error.kind}`];\n if (error.recoverable !== undefined) {\n parts.push(error.recoverable ? 'recoverable: yes' : 'recoverable: no');\n }\n if (error.recoveryHint !== undefined) parts.push(`suggested action: ${error.recoveryHint}`);\n if (error.hint !== undefined) parts.push(error.hint);\n return `Error: ${error.message}\\n[${parts.join('; ')}]`;\n}\n\n/** In-place variant of {@link addUsage} for mutable accumulators. */\nfunction accumulateUsage(target: Usage, delta: Usage): void {\n target.promptTokens += delta.promptTokens;\n target.completionTokens += delta.completionTokens;\n target.totalTokens += delta.totalTokens;\n if (delta.reasoningTokens !== undefined) {\n target.reasoningTokens = (target.reasoningTokens ?? 0) + delta.reasoningTokens;\n }\n if (delta.cachedReadTokens !== undefined) {\n target.cachedReadTokens = (target.cachedReadTokens ?? 0) + delta.cachedReadTokens;\n }\n if (delta.cacheWriteTokens !== undefined) {\n target.cacheWriteTokens = (target.cacheWriteTokens ?? 0) + delta.cacheWriteTokens;\n }\n}\n\n/**\n * Build a fresh {@link Agent} from the supplied configuration.\n *\n * @stable\n */\nexport function createAgent<TDeps = unknown, TOutput = string>(\n config: AgentConfig<TDeps, TOutput>,\n): Agent<TDeps, TOutput> {\n if (typeof config.name !== 'string' || config.name.length === 0) {\n throw new InvalidAgentConfigError(\"missing 'name'\");\n }\n if (config.provider === undefined || config.provider === null) {\n throw new InvalidAgentConfigError(\"missing 'provider'\");\n }\n // AG-3: a schema on a text-kind output spec is a config mistake (the\n // schema would never run) - reject instead of silently ignoring.\n if (config.outputType?.kind === 'text' && config.outputType.schema !== undefined) {\n throw new InvalidAgentConfigError(\n \"outputType.kind 'text' with a schema - did you mean kind: 'structured'?\",\n );\n }\n validatePreferredModel(config.preferredModel);\n if (config.modelTierMap !== undefined) {\n for (const [tier, spec] of Object.entries(config.modelTierMap)) {\n if (!isModelHintLike(tier)) throw new InvalidPreferredModelError({ tier });\n if (spec === undefined) continue;\n if (!isModelSpecLike(spec)) throw new InvalidPreferredModelError(spec);\n }\n }\n if (config.fallbackModels !== undefined) {\n for (const spec of config.fallbackModels) {\n if (!isModelSpecLike(spec)) throw new InvalidPreferredModelError(spec);\n }\n }\n\n const agentId = newId('agent');\n const tracer = config.tracer ?? NOOP_TRACER;\n const stopWhen = config.stopWhen ?? isStepCount(50);\n const fallbackPolicy: AgentFallbackPolicy = config.fallbackPolicy ?? {};\n const handoffMap = new Map<\n string,\n { readonly agent: Agent<TDeps, unknown>; readonly filter: DescribedFilter | undefined }\n >();\n for (const entry of config.handoffs ?? []) {\n const isWrappedHandoff = typeof entry === 'object' && entry !== null && 'target' in entry;\n const subAgent: Agent<TDeps, unknown> = isWrappedHandoff\n ? (entry as { readonly target: Agent<TDeps, unknown> }).target\n : (entry as Agent<TDeps, unknown>);\n const userFilter = isWrappedHandoff\n ? (\n entry as {\n readonly inputFilter?:\n | DescribedFilter\n | ((history: readonly Message[]) => readonly Message[]);\n }\n ).inputFilter\n : undefined;\n const filter = isDescribedFilter(userFilter) ? userFilter : undefined;\n const toolName = `${HANDOFF_TOOL_PREFIX}${subAgent.config.name}`;\n handoffMap.set(toolName, { agent: subAgent, filter });\n }\n\n let pendingSteer: Message[] = [];\n const pendingFollowUp: Message[] = [];\n let abortController: AbortController | undefined;\n let pendingAbort: AbortOptions | undefined;\n // Per-run scratch refs surfaced through the public surface for\n // event emission from `steer(...)` / `followUp(...)`.\n let activeRunState: RunState | undefined;\n // D2: capability of the ACTIVE run - read by the code-mode bridge so\n // in-script tool calls inherit the run's single-writer restriction.\n let activeRunCapability: 'read-only' | undefined;\n /** AG-11: guards the one-in-flight-run-per-instance invariant. */\n let runInFlight = false;\n const externalEventQueue: AgentEvent<TOutput>[] = [];\n\n /**\n * Manual-compaction requests enqueued by `agent.compact()` (CE-3 /\n * AG-13). The run loop owns the live message buffer, so the splice\n * must happen inside the loop: `maybeAutoCompact` services the queue\n * at the next step boundary, and `finishRun` settles leftovers as\n * explicit no-ops.\n */\n interface PendingManualCompact {\n readonly options: CompactOptions | undefined;\n readonly resolve: (result: CompactionApiResult) => void;\n readonly reject: (cause: unknown) => void;\n }\n const pendingManualCompacts: PendingManualCompact[] = [];\n\n const noopCompactionResult = (\n skippedReason: NonNullable<CompactionApiResult['skippedReason']>,\n ): CompactionApiResult => ({\n beforeTokens: 0,\n afterTokens: 0,\n summaryTokens: 0,\n durationMs: 0,\n hooksFiredCount: 0,\n summary: '',\n applied: false,\n skippedReason,\n });\n\n const memory: Memory | undefined = config.memory;\n const progressIO: ProgressIO = createProgressIO({\n ...(config.sensitivity !== undefined ? { defaultSensitivity: config.sensitivity } : {}),\n });\n\n // Assemble the unified tool registry at warm-up (Principle #12): one\n // registry across first-party + skill sources, with deterministic\n // cross-source collision resolution. Exposed read-only as\n // `agent.registry`; the run loop and `tool_search` consume it. The\n // registry is the tool-validation authority, so a malformed tool\n // fails fast here at construction.\n const toolRegistry = buildToolRegistry({\n ...(config.tools !== undefined\n ? { tools: config.tools as ReadonlyArray<Tool<unknown, unknown, unknown>> }\n : {}),\n ...(config.skills !== undefined ? { skills: config.skills } : {}),\n }).registry;\n\n // WI-05 (deferred loading + tool_search / P0-3): if any registered\n // tool sets `defer_loading: true`, register the built-in `tool_search`\n // so the model can discover those tools on demand. Tools that defer are\n // withheld from the per-step catalogue (see the loop below) until a\n // `tool_search` match promotes them, keeping large tool sets out of the\n // context window. When nothing defers this is a no-op.\n registerToolSearch(\n toolRegistry,\n config.toolPromotion === 'run-boundary' ? 'next-run' : 'next-step',\n );\n\n // D6: opt-in structured plan tool (TodoWrite-style, journaled). It\n // mutates the ACTIVE run's todos through a closure; attention\n // recitation renders them back into each step's request copy.\n if (config.plan === true && toolRegistry.get(PLAN_TOOL_NAME) === undefined) {\n toolRegistry.register(\n createPlanTool((todos) => {\n if (activeRunState !== undefined) {\n (activeRunState as { todos?: ReadonlyArray<import('@graphorin/core').TodoItem> }).todos =\n todos;\n }\n }) as unknown as Parameters<typeof toolRegistry.register>[0],\n { kind: 'built-in', subsystem: 'planning' },\n );\n }\n\n // WI-10 (result references / handles / P1-4): construct one spill\n // writer + reader pair at warm-up. The writer is handed to the executor\n // so a tool's `'spill-to-file'` truncation strategy externalises large\n // bodies to disk (0600, run-scoped); the reader - over the *same*\n // artifact root - backs the built-in `read_result` tool so the model can\n // page through a spilled artifact on demand instead of inlining the\n // whole blob. `read_result` is registered only when some tool spills.\n const spillWriter = createDefaultSpillWriter();\n const fileResultReader = createFileResultReader({ artifactRoot: spillWriter.artifactRoot });\n // WI-13 (P2-2): compose any operator-supplied result readers (e.g. an\n // MCP resource reader from `createMcpResourceReader`, for resolving\n // `resource_link` handles) after the spill-file reader. `read_result`\n // then pages both `graphorin-spill:` artifacts and external handles,\n // and is force-registered when external readers exist (even if no tool\n // spills) so those handles are resolvable.\n const externalResultReaders = config.resultReaders ?? [];\n const resultReader: ResultReader =\n externalResultReaders.length === 0\n ? fileResultReader\n : composeResultReaders([fileResultReader, ...externalResultReaders]);\n registerReadResult(toolRegistry, resultReader, { force: externalResultReaders.length > 0 });\n\n // Construct the unified ToolExecutor once at warm-up (WI-03 / P0-1),\n // bound to the registry above. Routing tool execution through the\n // executor activates the documented tool fields the inline loop\n // bypassed: per-tool `secretsAllowed` ACL, result truncation\n // (`maxResultTokens` / `truncationStrategy`), inbound sanitization,\n // memory-guard, idempotency keys and single-round repair.\n //\n // Durable HITL stays in the agent: approval is pre-screened below and\n // suspends the run, so the executor's `ApprovalGate` only ever sees\n // no-approval / pre-approved calls - it auto-grants and never blocks\n // the generator (Adapter G).\n //\n // Sandbox note: `config.tools` are inline `tool({...})` closures that\n // cannot be serialised to an out-of-process sandbox, and\n // `resolveSandbox` defaults user-defined tools to `worker-threads`.\n // Wiring a resolver that returned a real sandbox for that kind would\n // break every inline tool, so `sandboxResolver` is intentionally left\n // unset (the executor then runs inline - its documented fallback).\n // The resolved policy is still surfaced on the tool-execute span /\n // audit; real isolation applies to module-loadable (skill / MCP)\n // tools and is wired when those land.\n let activeExecutorBridge: ExecutorEventBridge | undefined;\n const toolApprovalGate: NonNullable<ExecutorOptions['approvalGate']> = {\n request: async () => ({ granted: true }),\n };\n const toolSecretResolver = buildSecretResolver();\n const toolTokenCounter = buildToolTokenCounter();\n // SDF-1: when memory is wired, bind a scope-aware region reader so the\n // executor's DEC-153 snapshot/verify cycle actually runs (the scope\n // resolves lazily from the in-flight run - the executor only invokes\n // the reader mid-run). Without memory the guard tiers stay skipped,\n // and a one-time WARN below makes that silent no-op visible.\n const memoryGuardWiring = buildMemoryGuard(\n memory,\n memory === undefined\n ? {}\n : {\n regionReader: createMemoryRegionReader(['working'], async (region) => {\n if (region !== 'working') return '';\n const scope = {\n userId: activeRunState?.userId ?? agentId,\n ...(activeRunState?.sessionId !== undefined\n ? { sessionId: activeRunState.sessionId }\n : {}),\n agentId,\n };\n const working = (\n memory as unknown as {\n working?: { compile?: (s: unknown, a?: string) => Promise<string> };\n }\n ).working;\n if (working?.compile === undefined) return '';\n try {\n return await working.compile(scope, agentId);\n } catch {\n // The reader is best-effort: a failed region read must not\n // turn the guard step into a run failure.\n return '';\n }\n }),\n },\n );\n const toolMemoryGuardFactory = memoryGuardWiring.memoryGuardFactory;\n if (memory === undefined) {\n const guarded = (config.tools ?? []).filter((t) => t.memoryGuardTier !== undefined);\n if (guarded.length > 0) {\n console.warn(\n `[graphorin/agent] ${guarded.length} tool(s) declare memoryGuardTier but no memory is wired - ` +\n 'the DEC-153 snapshot/verify guard is skipped (SDF-1). Wire `memory` to activate it.',\n );\n }\n }\n // Provenance / data-flow guard (WI-12 / P1-3, opt-in). Built once and\n // shared by every executor (direct + code-mode quiet), so the sink gate\n // and taint recording apply uniformly. Off unless configured with a\n // non-`'off'` mode - zero overhead on the default path.\n const toolDataFlowGuard =\n config.dataFlowPolicy !== undefined && config.dataFlowPolicy.mode !== 'off'\n ? buildDataFlowGuard(config.dataFlowPolicy)\n : undefined;\n // D4 Progent tool-argument policy + Rule-of-Two floor. `ruleOfTwo`\n // compiles to a policy (+ a read-only capability floor when it denies\n // external side effects); an explicit `toolPolicy` composes on top\n // (its rules are appended, so an explicit forbid still wins). Built\n // once, shared by every executor.\n const { guard: toolArgumentPolicyGuard, capabilityFloor: ruleOfTwoCapabilityFloor } =\n buildToolArgumentPolicy(config.toolPolicy, config.ruleOfTwo);\n const toolStreamingSink: NonNullable<ExecutorOptions['streamingSink']> = (event) =>\n activeExecutorBridge?.sink(event);\n // `quiet` builds an executor without the streaming sink - used for\n // code-mode's in-script tool calls (WI-11), whose `tool.execute.*`\n // events must not interleave into the outer agent stream.\n const makeToolExecutor = (\n registry: ToolRegistry,\n opts?: { readonly quiet?: boolean },\n ): ToolExecutor =>\n createToolExecutor({\n registry,\n approvalGate: toolApprovalGate,\n secretResolver: toolSecretResolver,\n tokenCounter: toolTokenCounter,\n memoryGuardFactory: toolMemoryGuardFactory,\n ...(memoryGuardWiring.memoryRegionReader !== undefined\n ? { memoryRegionReader: memoryGuardWiring.memoryRegionReader }\n : {}),\n spill: spillWriter,\n ...(toolDataFlowGuard !== undefined ? { dataFlowGuard: toolDataFlowGuard } : {}),\n ...(toolArgumentPolicyGuard !== undefined ? { argumentPolicy: toolArgumentPolicyGuard } : {}),\n ...(opts?.quiet === true ? {} : { streamingSink: toolStreamingSink }),\n ...(config.maxParallelTools !== undefined\n ? { maxParallelTools: config.maxParallelTools }\n : {}),\n ...(config.toolRetry !== undefined ? { retry: config.toolRetry } : {}),\n });\n const toolExecutor = makeToolExecutor(toolRegistry);\n\n // Code-mode (WI-11 / P1-2, opt-in): advertise only the `code_search` /\n // `code_execute` meta-tools and let the model orchestrate tools inside a\n // sandbox, so intermediate results stay out of context. A quiet executor\n // backs the in-script tool bridge (same per-tool governance as direct\n // mode). `read_result` is registered after the meta-tools because\n // `code_execute` opts into `'spill-to-file'`, so a large final result can\n // be fetched back on demand. Default `'direct'` mode leaves all of this\n // untouched - `codeModeAdvertised` stays empty and the loop is unchanged.\n const isCodeMode = config.toolInvocation === 'code-mode';\n let codeModeAdvertised: ReadonlyArray<Tool<unknown, unknown, TDeps>> = [];\n if (isCodeMode) {\n const reserved = new Set<string>([\n CODE_EXECUTE_NAME,\n CODE_SEARCH_NAME,\n TOOL_SEARCH_NAME,\n READ_RESULT_NAME,\n ...handoffMap.keys(),\n ]);\n const metas = registerCodeMode(\n toolRegistry,\n makeToolExecutor(toolRegistry, { quiet: true }),\n reserved,\n () => activeRunCapability,\n );\n registerReadResult(toolRegistry, resultReader);\n const readResult = toolRegistry.get(READ_RESULT_NAME);\n codeModeAdvertised = [\n ...metas,\n ...(readResult !== undefined ? [readResult] : []),\n ] as ReadonlyArray<Tool<unknown, unknown, TDeps>>;\n }\n\n const causalityMonitor = config.causalityMonitor\n ? new CausalityMonitor(config.causalityMonitor)\n : undefined;\n\n /**\n * AG-11: one in-flight run per Agent instance. `steer` / `followUp` /\n * `abort` / `compact` address \"the run\" with no run handle, so\n * overlapping runs on one instance cannot be expressed safely - they\n * would share the abort controller, steer queue, active-run ref and\n * executor bridge. A second concurrent `run()` / `stream()` rejects\n * with {@link ConcurrentRunError}; run-scoped state is reset on entry\n * (a steer/abort queued after the previous run ended belongs to NO\n * run) and cleared in a `finally` that also covers abandoned streams\n * (consumer `break`) and thrown runs.\n */\n async function* runLoop(\n input: AgentInput | RunState,\n options: AgentCallOptions<TDeps>,\n ): AsyncGenerator<AgentEvent<TOutput>, AgentResult<TOutput>, void> {\n if (runInFlight) {\n throw new ConcurrentRunError();\n }\n runInFlight = true;\n pendingSteer = [];\n pendingAbort = undefined;\n // D2 + D4: per-run capability - the call-level override wins, then\n // the agent default, then the Rule-of-Two floor (a profile denying\n // external side effects forces read-only even without an explicit\n // capability). Absent ⇒ all capabilities (legacy behaviour).\n activeRunCapability = options.capability ?? config.capability ?? ruleOfTwoCapabilityFloor;\n // AG-10: the causality chain is a per-run artifact - a denial\n // recorded in one run must not poison detection in the next.\n causalityMonitor?.reset();\n try {\n return yield* runLoopInner(input, options);\n } finally {\n runInFlight = false;\n activeRunState = undefined;\n activeRunCapability = undefined;\n // Backstop for exits that bypass `finishRun` (abandoned stream,\n // generator teardown): settle queued manual compactions so no\n // `agent.compact()` promise is left hanging (CE-3/AG-13).\n while (pendingManualCompacts.length > 0) {\n pendingManualCompacts.shift()?.resolve(noopCompactionResult('no-active-run'));\n }\n }\n }\n\n async function* runLoopInner(\n input: AgentInput | RunState,\n options: AgentCallOptions<TDeps>,\n ): AsyncGenerator<AgentEvent<TOutput>, AgentResult<TOutput>, void> {\n const { seed: rawSeed, resumed } = asMessages(input);\n // AG-12: queued follow-ups are next-turn metadata - they ride into\n // the next FRESH run as leading user turns. The old path mutated a\n // finished run back to 'running' and appended the message to a loop\n // that never processed it, leaving a non-terminal persisted RunState\n // with a dangling user turn. Resumed runs keep the queue intact.\n const seed =\n resumed === undefined && pendingFollowUp.length > 0\n ? [...pendingFollowUp.splice(0, pendingFollowUp.length), ...rawSeed]\n : rawSeed;\n const sessionId = options.sessionId ?? config.sessionId ?? `session_${newId()}`;\n const userId = options.userId ?? config.userId;\n const localCtl = new AbortController();\n abortController = localCtl;\n // AG-5: the loop + every provider request must observe the LOCAL\n // controller, so `agent.abort()` (which aborts `localCtl`) is honoured even\n // when the caller supplied their own `options.signal`. The caller's signal\n // is propagated INTO `localCtl` by the listener below; the listener is torn\n // down in the run's `finally` so it does not accumulate across runs that\n // share one long-lived parent signal.\n const signal = localCtl.signal;\n const parentSignal = options.signal;\n const onParentAbort = (): void => localCtl.abort();\n if (parentSignal !== undefined) {\n if (parentSignal.aborted) localCtl.abort();\n else parentSignal.addEventListener('abort', onParentAbort);\n }\n\n const usageAcc = new InMemoryUsageAccumulator();\n const baseState: RunState = resumed\n ? resumed\n : createInitialRunState({\n id: newId('run'),\n agentId,\n sessionId,\n ...(userId !== undefined ? { userId } : {}),\n });\n // Mutable view (the public RunState is `readonly` but the runtime\n // owns the lifecycle; cast to a writable shape here).\n const state = baseState as RunState as unknown as MutableRunState & RunState;\n activeRunState = state;\n\n // AG-19: rehydrate the run-scoped security state BEFORE any tool runs this\n // resume. Seeding the data-flow ledger with the persisted coarse taint\n // summary keeps an enforce-mode sink gated across the suspend/resume\n // boundary (the promoted-tool set is restored below, once it exists).\n if (resumed && state.taintSummary !== undefined) {\n toolDataFlowGuard?.seedLedger(state.id, state.taintSummary);\n }\n\n // WI-05: deferred tools promoted by a `tool_search` call this run.\n // Membership grows as the model discovers tools and gates which\n // deferred entries the per-step catalogue advertises. TL-7/AG-19:\n // persisted onto `RunState.promotedTools` at every exit and\n // rehydrated here, so a resumed run keeps its discoveries.\n const promotedDeferred = new Set<string>();\n // AG-19: restore deferred tools promoted by `tool_search` before the suspend\n // so they remain in the per-step catalogue after a resume.\n if (resumed && state.promotedTools !== undefined) {\n for (const name of state.promotedTools) promotedDeferred.add(name);\n }\n // C1: under `toolPromotion: 'run-boundary'` the advertised catalogue is\n // frozen to the promotions known at run start (incl. those restored\n // above), keeping the provider prompt cache byte-stable for the whole\n // run. New promotions still land in `promotedDeferred` (and persist),\n // taking effect on the next run / resume.\n const runStartPromotions =\n config.toolPromotion === 'run-boundary' ? new Set(promotedDeferred) : undefined;\n\n // agent-08 (F4): capture the run-scoped security state on EVERY exit\n // through finishRun - not just the approval suspend. An 'aborted' run\n // is resumable (the AG-14 guard blocks only awaiting_approval/failed)\n // and a 'completed' run re-enters as a follow-up; both must rehydrate\n // the enforce-mode sink gate and the discovered-tool catalogue.\n // Shadows the factory-scope finishRunBase for every call in this run.\n async function* finishRun(\n s: MutableRunState & RunState,\n snapshot: InternalRunSnapshot<TOutput>,\n ): AsyncGenerator<AgentEvent<TOutput>, AgentResult<TOutput>, void> {\n const taintSnap = toolDataFlowGuard?.snapshotLedger(s.id);\n if (taintSnap !== undefined) {\n (s as { taintSummary?: typeof taintSnap }).taintSummary = taintSnap;\n }\n if (promotedDeferred.size > 0) {\n (s as { promotedTools?: readonly string[] }).promotedTools = [...promotedDeferred];\n }\n return yield* finishRunBase(s, snapshot);\n }\n\n const messages: Message[] = resumed ? [...state.messages] : [];\n if (!resumed) {\n // Inject the agent's system prompt at the top of the buffer\n // exactly once per run, before any seed messages.\n const instructionsRaw = config.instructions;\n // AG-8: resolve the function form of `instructions` (sync or async). It is\n // resolved ONCE per run (the per-run contract documented on `AgentConfig`),\n // against a RunContext snapshot at step 0; the result is pinned as the\n // run's system-prompt prefix. A function that previously returned nothing\n // observable now actually seeds the system message.\n let instructionsText: string;\n if (typeof instructionsRaw === 'string') {\n instructionsText = instructionsRaw;\n } else {\n const instructionsCtx: RunContext<TDeps> = {\n runId: state.id,\n sessionId,\n ...(userId !== undefined ? { userId } : {}),\n agentId,\n deps: (options.deps ?? config.deps) as TDeps,\n tracer,\n signal,\n usage: usageAcc,\n stepNumber: 0,\n messages,\n state,\n };\n instructionsText = await instructionsRaw(instructionsCtx);\n }\n let systemPrompt = instructionsText;\n if (config.autoAssembleContext === true && memory !== undefined) {\n // CE-1 (opt-in): build the memory-aware 6-layer system prompt via the\n // context engine. The instructions become Layer 2; the engine prepends\n // the memory base and appends working blocks, procedural rules, skill\n // cards, the metadata counts, and (when `factsAutoRecall` is configured)\n // auto-recalled facts. Default-off keeps the explicit memory-tools\n // pattern, so the system prompt is `instructions` alone.\n const lastUser = lastUserText(seed);\n const assembled = await memory.contextEngine.assemble(memory, {\n scope: { userId: userId ?? agentId, sessionId, agentId },\n agentId,\n sessionId,\n runId: state.id,\n ...(instructionsText.length > 0 ? { agentInstructions: instructionsText } : {}),\n ...(lastUser !== undefined ? { lastUserMessage: lastUser } : {}),\n });\n systemPrompt = assembled.systemMessage.content;\n }\n if (systemPrompt.length > 0) {\n messages.push({ role: 'system', content: systemPrompt });\n }\n messages.push(...seed);\n // Mirror the assembled messages into RunState so the JSONL\n // session export and any downstream consumers see what the\n // agent saw.\n for (const m of messages) state.messages.push(m);\n }\n\n const finalSnapshot: InternalRunSnapshot<TOutput> = {\n output: '' as unknown as TOutput,\n };\n\n // C7: one agent.run span per run; step/tool/provider spans parent\n // under it so the whole run is a single trace tree. Attributes follow\n // the OTel GenAI semantic conventions (gen_ai.*).\n const runSpan = tracer.startSpan({\n type: 'agent.run',\n attrs: {\n 'gen_ai.operation.name': 'invoke_agent',\n 'gen_ai.agent.id': agentId,\n 'gen_ai.agent.name': config.name,\n 'graphorin.run.id': state.id,\n 'graphorin.session.id': sessionId,\n 'graphorin.run.resumed': resumed !== undefined,\n },\n });\n let currentStepSpan: AISpan<'agent.step'> | undefined;\n\n yield { type: 'agent.start', runId: state.id, agentId };\n\n // AG-2 / SDF-4: input guardrails screen each fresh-run seed user\n // message (string content) BEFORE the first provider call, using the\n // canonical `@graphorin/security` composer. 'block' fails the run\n // without reaching the model; 'rewrite' replaces the content in both\n // the working buffer and the persisted RunState; 'warn' logs and\n // continues. Resumed runs skip the pass - their seed was screened\n // when first submitted.\n const inputGuards = config.guardrails?.input;\n if (!resumed && inputGuards !== undefined && inputGuards.length > 0) {\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n if (msg === undefined || msg.role !== 'user' || typeof msg.content !== 'string') continue;\n const composed = await composeGuardrails(inputGuards, msg.content, {\n stage: 'input',\n runId: state.id,\n sessionId,\n agentId,\n });\n if (!composed.ok) {\n yield {\n type: 'guardrail.tripped',\n guardrailName: composed.name,\n phase: 'input',\n reason: composed.message,\n };\n const message = `input guardrail '${composed.name}' blocked the run: ${composed.message}`;\n yield { type: 'agent.error', error: { message, code: 'guardrail-blocked' } };\n state.status = 'failed';\n state.error = { message, code: 'guardrail-blocked' };\n return yield* finishRun(state, finalSnapshot);\n }\n if (composed.value !== msg.content) {\n const rewritten: Message = { ...msg, content: composed.value };\n const stateIdx = state.messages.indexOf(msg);\n messages[i] = rewritten;\n if (stateIdx !== -1) state.messages[stateIdx] = rewritten;\n }\n }\n }\n\n // AG-3: one per-run JSON instruction for structured output, appended\n // to each provider request (never the shared buffer / RunState).\n const structuredInstruction =\n config.outputType?.kind === 'structured'\n ? buildStructuredInstruction(config.outputType)\n : undefined;\n\n // AG-1: approved gated calls collected from a resume directive, executed\n // for real once every approval is resolved (see the dispatch below).\n const resumedApprovedCalls: ToolCall[] = [];\n // agent-02: the ToolApproval records behind `resumedApprovedCalls`,\n // kept so the write-ahead intent checkpoint can re-attach them to\n // `pendingApprovals` (a crash-retry against the intent re-dispatches).\n const grantedApprovals: ToolApproval[] = [];\n // Step-journal: tool calls already completed on a prior resume are recorded\n // in the journal (`state.steps`); a re-resume must not run their side effects\n // again. Collect their ids so an approved call already journaled is replayed,\n // not re-executed (exactly-once; AG-1).\n const journaledCallIds = new Set<string>();\n for (const step of state.steps) {\n for (const completed of step.toolCalls) journaledCallIds.add(completed.call.toolCallId);\n }\n\n // Process resume directive - apply approval decisions to any\n // pending approvals captured in the previous suspend.\n if (\n resumed &&\n options.directive?.approvals !== undefined &&\n state.pendingApprovals.length > 0\n ) {\n const decisions = new Map(options.directive.approvals.map((d) => [d.toolCallId, d]));\n const stillPending: ToolApproval[] = [];\n for (const approval of state.pendingApprovals) {\n const decision = decisions.get(approval.toolCallId);\n if (decision === undefined) {\n stillPending.push(approval);\n continue;\n }\n if (decision.granted) {\n yield {\n type: 'tool.approval.granted',\n toolCallId: approval.toolCallId,\n };\n // Step-journal: if this approved call already ran on a prior resume -\n // journaled in `state.steps` with its result still in the message\n // buffer - replay it instead of running the side effect again\n // (exactly-once across re-resumes). If the journal entry exists but its\n // result message was lost, fall through to a single re-execution (the\n // documented \"at most one re-execution\" bound).\n if (\n journaledCallIds.has(approval.toolCallId) &&\n messages.some((m) => m.role === 'tool' && m.toolCallId === approval.toolCallId)\n ) {\n continue;\n }\n // AG-1: queue the approved call for REAL execution (dispatched\n // below). It runs through the same ToolExecutor as any other tool\n // call - taint / audit / result recording - instead of pushing a\n // \"[not actually executed]\" placeholder that left the gated side\n // effect unreachable.\n resumedApprovedCalls.push({\n toolCallId: approval.toolCallId,\n toolName: approval.toolName,\n args: approval.args,\n });\n grantedApprovals.push(approval);\n } else {\n yield {\n type: 'tool.approval.denied',\n toolCallId: approval.toolCallId,\n ...(decision.reason !== undefined ? { reason: decision.reason } : {}),\n };\n messages.push({\n role: 'tool',\n toolCallId: approval.toolCallId,\n content: `Error: tool approval denied${decision.reason ? `: ${decision.reason}` : ''}`,\n });\n state.messages.push({\n role: 'tool',\n toolCallId: approval.toolCallId,\n content: `Error: tool approval denied${decision.reason ? `: ${decision.reason}` : ''}`,\n });\n }\n }\n // Clear the queue + restore the running status so the loop\n // resumes from where it paused.\n state.pendingApprovals.splice(0, state.pendingApprovals.length, ...stillPending);\n if (stillPending.length === 0) {\n state.status = 'running';\n }\n }\n // AG-14: the resumed status is left untouched here. A 'failed' run is NOT\n // silently rewritten to 'completed' (the terminal/suspended guard below\n // returns it as-is); a 'completed' run keeps its status and re-enters the\n // loop for a follow-up; an unresolved 'awaiting_approval' run is caught by\n // that same guard.\n\n // WI-09: pin the trusted system-prompt prefix length now, on the\n // fully-assembled initial buffer, so auto-compaction never rewrites\n // it and prior summaries stay re-compactable (see\n // `countLeadingSystemMessages`).\n const systemPrefixLength = countLeadingSystemMessages(messages);\n\n const runContextBase: RunContext<TDeps> = {\n runId: state.id,\n sessionId,\n ...(userId !== undefined ? { userId } : {}),\n agentId,\n deps: (options.deps ?? config.deps) as TDeps,\n tracer,\n signal,\n usage: usageAcc,\n stepNumber: 0,\n messages,\n state,\n };\n\n // AG-14 (failed half): a terminal-failed run must never re-enter the\n // provider loop or dispatch anything - that would silently complete a\n // failed run. Return it as-is.\n if (resumed && state.status === 'failed') {\n return yield* finishRun(state, finalSnapshot);\n }\n\n // AG-1: execute the approved gated calls for REAL before the provider\n // loop - the model sees their genuine results on the first step. They\n // run through the shared ToolExecutor (taint / audit) and record\n // CompletedToolCalls in a resume step. Dispatching here (outside the\n // loop's approval pre-screen) also means the gated call never\n // re-suspends, so there is no livelock.\n //\n // agent-07: this dispatch runs even when OTHER approvals remain\n // pending. A granted call has already been removed from\n // `pendingApprovals`, so skipping it (as the old order did - the\n // suspended-guard returned before the dispatch) stranded it\n // unrunnable forever; the run re-suspends with the remainder below.\n if (resumed && resumedApprovedCalls.length > 0) {\n // agent-02 write-ahead intent: persist a checkpoint equivalent to\n // the pre-dispatch suspended state (granted approvals re-attached\n // to `pendingApprovals`) BEFORE any side effect runs. A crash-retry\n // against this checkpoint re-resumes with the same directive and\n // re-dispatches - the documented at-most-one-re-execution bound -\n // and its nodeName records that a grant arrived and dispatch was in\n // flight.\n if (config.checkpointStore !== undefined) {\n const prevStatus = state.status;\n state.status = 'awaiting_approval';\n state.pendingApprovals.unshift(...grantedApprovals);\n const intentState = serializeRunState(state, { stripTracingApiKey: true });\n state.pendingApprovals.splice(0, grantedApprovals.length);\n state.status = prevStatus;\n await config.checkpointStore.put(\n state.id,\n 'agent',\n {\n id: state.id,\n threadId: state.id,\n namespace: 'agent',\n state: intentState,\n channelVersions: {},\n stepNumber: 0,\n createdAt: new Date().toISOString(),\n },\n { source: 'sync', status: 'suspended', nodeName: 'agent.resume.intent' },\n );\n }\n state.steps.push({\n stepNumber: 0,\n startedAt: new Date().toISOString(),\n endedAt: new Date().toISOString(),\n usage: zeroUsage(),\n toolCalls: [],\n agentId: state.currentAgentId,\n });\n // tools-02: a human granted exactly `approval.args` - the repair\n // hook must not rewrite a pre-approved payload behind the grant, so\n // a (should-be-impossible) validation failure surfaces as\n // `invalid_input` instead of executing args nobody saw.\n yield* dispatchBatch(\n resumedApprovedCalls,\n toolExecutor,\n { ...runContextBase, stepNumber: 0, messages },\n 0,\n { disableRepair: true },\n );\n // agent-02: persist the journaled post-dispatch state. From THIS\n // checkpoint a re-delivered resume is exactly-once: the granted ids\n // are no longer pending and their journal entries + tool messages\n // are present, so nothing re-dispatches. (For the manual JSON flow,\n // the same state is returned as `result.state` - persist it after\n // every resume to get the same guarantee.)\n if (config.checkpointStore !== undefined) {\n await config.checkpointStore.put(\n state.id,\n 'agent',\n {\n id: state.id,\n threadId: state.id,\n namespace: 'agent',\n state: serializeRunState(state, { stripTracingApiKey: true }),\n channelVersions: {},\n stepNumber: 0,\n createdAt: new Date().toISOString(),\n },\n {\n source: 'sync',\n status: state.status === 'awaiting_approval' ? 'suspended' : 'running',\n nodeName: 'agent.resume.dispatched',\n },\n );\n }\n }\n\n // AG-14 (suspended half): a resumed run still awaiting approvals the\n // directive did not resolve must not re-enter the provider loop - that\n // would re-issue a dangling tool_use real providers reject. The granted\n // subset (above) HAS executed and is journaled; return the re-suspended\n // state carrying its results.\n if (resumed && state.status === 'awaiting_approval') {\n return yield* finishRun(state, finalSnapshot);\n }\n\n /**\n * Dispatch a batch of (non-handoff) tool calls through the\n * {@link ToolExecutor} and surface the results as `AgentEvent`s.\n *\n * The agent owns the `tool.execute.start` / `.end` / `.error`\n * lifecycle (derived deterministically from the returned\n * {@link CompletedToolCall} outcomes) so every outcome kind -\n * success, unknown-tool, invalid-input, sanitization-blocked,\n * execution error - yields a consistent event and tool message,\n * preserving the pre-WI-03 stream shape (R10). The executor's\n * genuinely-live streaming events (`tool.execute.progress` /\n * `.partial`, emitted only by streaming-hint tools) are bridged\n * through Adapter E while the batch runs and are purely additive.\n *\n * Parallelism (WI-04): the executor runs independent calls in this\n * batch concurrently, bounded by `maxParallelTools`. `tool.execute.start`\n * is emitted up-front in call order and `.end` / `.error` after the\n * batch settles, also in call order - so result mapping and tool-message\n * history are deterministic regardless of which call finishes first,\n * while `.progress` / `.partial` events for concurrent calls interleave\n * (keyed by `toolCallId`). Tools declaring `executionMode: 'sequential'`\n * are serialised by the executor and never overlap.\n */\n async function* dispatchBatch(\n calls: ReadonlyArray<ToolCall>,\n executor: ToolExecutor,\n runContext: RunContext<TDeps>,\n stepNum: number,\n dispatchOpts: { readonly disableRepair?: boolean } = {},\n ): AsyncGenerator<AgentEvent<TOutput>, void, void> {\n if (calls.length === 0) return;\n for (const call of calls) {\n yield { type: 'tool.execute.start', toolCallId: call.toolCallId };\n }\n\n const bridge = createExecutorEventBridge();\n activeExecutorBridge = bridge;\n const resultsPromise = executor.executeBatch({\n calls,\n runContext,\n stepNumber: stepNum,\n ...(dispatchOpts.disableRepair !== undefined\n ? { disableRepair: dispatchOpts.disableRepair }\n : {}),\n // D2: the run's capability restriction rides every batch.\n ...(activeRunCapability !== undefined ? { capability: activeRunCapability } : {}),\n });\n // Close the bridge once the batch settles so `drain()` ends; the\n // executor catches per-call errors, so the batch never rejects.\n const closeOnSettle = resultsPromise.then(\n () => bridge.close(),\n () => bridge.close(),\n );\n for await (const event of bridge.drain()) {\n if (event.type === 'tool.execute.progress' || event.type === 'tool.execute.partial') {\n yield event as AgentEvent<TOutput>;\n }\n }\n await closeOnSettle;\n activeExecutorBridge = undefined;\n\n const completed = await resultsPromise;\n const byCallId = new Map(completed.map((c) => [c.outcome.toolCallId, c]));\n const stepEntry = state.steps[state.steps.length - 1];\n for (const call of calls) {\n const result = byCallId.get(call.toolCallId);\n if (result === undefined) continue;\n if (stepEntry !== undefined) {\n (stepEntry.toolCalls as CompletedToolCall[]).push(result);\n }\n const outcome = result.outcome;\n if ('kind' in outcome) {\n yield { type: 'tool.execute.error', toolCallId: call.toolCallId, error: outcome };\n const text = renderToolErrorMessage(outcome);\n messages.push({ role: 'tool', toolCallId: call.toolCallId, content: text });\n state.messages.push({ role: 'tool', toolCallId: call.toolCallId, content: text });\n causalityMonitor?.recordCall(`tool.error:${call.toolName}`);\n } else {\n const output = outcome.output;\n yield {\n type: 'tool.execute.end',\n toolCallId: call.toolCallId,\n result: output,\n durationMs: outcome.durationMs,\n };\n // WI-10 (P1-4): when the result spilled to a handle, inline only\n // the bounded preview plus a retrieval hint so the full blob never\n // enters the context window - the model fetches the rest on demand\n // via `read_result`. Inlined results serialise exactly as before\n // (preserves the happy-path message contract, R10).\n const handle = outcome.resultHandle;\n const rendered =\n handle !== undefined\n ? `${handle.preview}\\n\\n[Full result${\n handle.bytes !== undefined ? ` (${handle.bytes} bytes)` : ''\n } stored behind a handle. Call read_result with handle ${JSON.stringify(\n handle.uri,\n )} to retrieve it - optionally narrow with offset/length (bytes) or startLine/endLine.]`\n : typeof output === 'string'\n ? output\n : JSON.stringify(output);\n // C3 (ACI): an empty body reads as a glitch to models; say\n // explicitly that the tool ran and simply had nothing to print.\n const text =\n rendered === undefined || rendered.trim().length === 0\n ? '(tool ran successfully with no output)'\n : rendered;\n messages.push({ role: 'tool', toolCallId: call.toolCallId, content: text });\n state.messages.push({ role: 'tool', toolCallId: call.toolCallId, content: text });\n causalityMonitor?.recordCall(`tool:${call.toolName}`);\n // WI-05: a successful `tool_search` promotes its matches so the\n // catalogue advertises them on the next step.\n if (call.toolName === TOOL_SEARCH_NAME) {\n recordToolSearchPromotions(output, promotedDeferred);\n }\n }\n }\n }\n\n /**\n * Auto-compaction trigger (WI-09 / P1-1). Before assembling each\n * provider request, ask the memory {@link ContextEngine} whether the\n * in-flight buffer has crossed its per-provider threshold\n * (`shouldCompact`); when it has, summarise the older turns\n * (`compactNow`, `source: 'auto-trigger'`), splice the result back in\n * - preserving the byte-stable system prefix and the most-recent\n * turns verbatim - and emit `context.compacted`. The compaction is\n * configured on the memory facade (`createMemory({ contextEngine })`,\n * RB-46); there is no parallel agent-level knob.\n *\n * No-op when no memory is wired, when compaction is disabled or below\n * threshold (the engine returns `false`), or for `secret`-tier runs\n * (secret history is never shipped to the summarizer - a less-trusted\n * external sink; per-result handle references land in WI-10). Best\n * effort: a misconfigured engine (e.g. no summarizer) is swallowed and\n * the run proceeds uncompacted rather than aborting mid-flight.\n *\n * Operator-requested compactions (`agent.compact()`, CE-3/AG-13) are\n * serviced here too, FIRST - the queue carries the `compact()` promise\n * resolvers, and manual requests bypass the trigger evaluation.\n */\n async function* maybeAutoCompact(): AsyncGenerator<AgentEvent<TOutput>, void, void> {\n while (pendingManualCompacts.length > 0) {\n const pending = pendingManualCompacts.shift();\n if (pending !== undefined) yield* serviceManualCompact(pending);\n }\n const mem = memory;\n if (mem === undefined) return;\n // Sensitivity gate (WI-09 step 2): drop, never re-route, secret-tier\n // content. Auto-compaction is an LLM summarizer call, so a secret\n // run is left un-compacted here.\n if (config.sensitivity === 'secret') return;\n const engine = mem.contextEngine;\n // context-engine-04: trigger, reclaim floor, and anti-thrash guard\n // share one basis - the engine sees the full buffer for the trigger\n // total, learns where the pinned (uncompactable) prefix ends, and\n // receives the prefix messages so the guard arms against the FULL\n // post-splice context instead of the sliced body.\n const triggered = await engine\n .shouldCompact(messages, { compactableFromIndex: systemPrefixLength })\n .catch(() => false);\n if (!triggered) return;\n\n const startedAt = Date.now();\n const envelope = await engine\n .compactNow({\n scope: { userId: state.userId ?? agentId, sessionId, agentId },\n runId: state.id,\n sessionId,\n agentId,\n source: 'auto-trigger',\n messages: messages.slice(systemPrefixLength),\n prefixMessages: messages.slice(0, systemPrefixLength),\n memory: mem,\n })\n // No summarizer configured (or the strategy threw) - proceed with\n // the un-compacted buffer rather than failing a live run.\n .catch(() => undefined);\n if (envelope === undefined) return;\n // Nothing was old enough to trim (body ≤ preserve-recent) - skip the\n // splice + event so `context.compacted` only fires on real work.\n if (envelope.result.droppedMessageIndices.length === 0) return;\n\n spliceCompacted(envelope);\n yield {\n type: 'context.compacted',\n runId: state.id,\n sessionId,\n agentId,\n beforeTokens: envelope.result.beforeTokens,\n afterTokens: envelope.result.afterTokens,\n summaryTokens: envelope.result.summaryTokens,\n durationMs: Date.now() - startedAt,\n source: 'auto-trigger',\n hooksFiredCount: envelope.result.hooksFiredCount,\n };\n }\n\n /**\n * context-engine-06: last-resort tier at hard context overflow. When\n * a provider rejects the request as over-window, force ONE aggressive\n * compaction (`preserveRecentTurns: 2`, trigger evaluation bypassed)\n * and let the caller retry the same provider - the fallback chain's\n * members usually share the same window, so without this the run just\n * dies. Returns `true` when the buffer actually shrank (retry is\n * worthwhile); `false` when memory is not wired, the run is\n * secret-tier, compaction trimmed nothing, or the engine threw.\n */\n async function* tryEmergencyCompact(): AsyncGenerator<AgentEvent<TOutput>, boolean, void> {\n const mem = memory;\n if (mem === undefined || config.sensitivity === 'secret') return false;\n const startedAt = Date.now();\n const envelope = await mem.contextEngine\n .compactNow({\n scope: { userId: state.userId ?? agentId, sessionId, agentId },\n runId: state.id,\n sessionId,\n agentId,\n source: 'auto-trigger',\n messages: messages.slice(systemPrefixLength),\n prefixMessages: messages.slice(0, systemPrefixLength),\n memory: mem,\n preserveRecentTurns: 2,\n })\n .catch(() => undefined);\n if (envelope === undefined || envelope.result.droppedMessageIndices.length === 0) {\n return false;\n }\n spliceCompacted(envelope);\n yield {\n type: 'context.compacted',\n runId: state.id,\n sessionId,\n agentId,\n beforeTokens: envelope.result.beforeTokens,\n afterTokens: envelope.result.afterTokens,\n summaryTokens: envelope.result.summaryTokens,\n durationMs: Date.now() - startedAt,\n source: 'auto-trigger',\n hooksFiredCount: envelope.result.hooksFiredCount,\n };\n return true;\n }\n\n /**\n * Prefix-pinned splice shared by the auto + manual compaction paths\n * (CE-3): stable system prefix + [summary, ...recent turns], with the\n * post-compaction hooks' text Context Essentials re-anchored as a\n * trailing system message so they survive the trim (RB-46). Mutates\n * BOTH the live loop buffer and `state.messages`.\n */\n function spliceCompacted(envelope: CompactionEnvelope): void {\n const prefix = messages.slice(0, systemPrefixLength);\n const rebuilt: Message[] = [...prefix, ...envelope.result.trimmedMessages];\n const essentials = envelope.extraContent\n .map((part) =>\n typeof part === 'object' && part !== null && 'text' in part\n ? String((part as { readonly text: unknown }).text)\n : '',\n )\n .filter((text) => text.length > 0)\n .join('\\n\\n');\n if (essentials.length > 0) {\n rebuilt.push({ role: 'system', content: essentials });\n }\n messages.splice(0, messages.length, ...rebuilt);\n state.messages.splice(0, state.messages.length, ...rebuilt);\n }\n\n /**\n * Service one `agent.compact()` request inside the loop (CE-3/AG-13):\n * same prefix-pinned splice as the auto path, `source: 'manual'` (or\n * the caller's `'pre-step'`), `preserveRecentTurns` forwarded as a\n * per-call strategy override. An engine failure rejects the caller's\n * promise but never aborts the live run; a summarize that trims\n * nothing resolves `applied: false` without an event.\n */\n async function* serviceManualCompact(\n pending: PendingManualCompact,\n ): AsyncGenerator<AgentEvent<TOutput>, void, void> {\n const mem = memory;\n if (mem === undefined) {\n pending.resolve(noopCompactionResult('no-memory'));\n return;\n }\n const source = pending.options?.source ?? 'manual';\n const startedAt = Date.now();\n let envelope: CompactionEnvelope;\n try {\n envelope = await mem.contextEngine.compactNow({\n scope: { userId: state.userId ?? agentId, sessionId, agentId },\n runId: state.id,\n sessionId,\n agentId,\n source,\n messages: messages.slice(systemPrefixLength),\n // context-engine-04: same accounting basis as the auto path.\n prefixMessages: messages.slice(0, systemPrefixLength),\n memory: mem,\n ...(pending.options?.preserveRecentTurns !== undefined\n ? { preserveRecentTurns: pending.options.preserveRecentTurns }\n : {}),\n });\n } catch (cause) {\n pending.reject(cause);\n return;\n }\n const { result } = envelope;\n const applied = result.droppedMessageIndices.length > 0;\n if (applied) {\n spliceCompacted(envelope);\n yield {\n type: 'context.compacted',\n runId: state.id,\n sessionId,\n agentId,\n beforeTokens: result.beforeTokens,\n afterTokens: result.afterTokens,\n summaryTokens: result.summaryTokens,\n durationMs: Date.now() - startedAt,\n source,\n hooksFiredCount: result.hooksFiredCount,\n };\n }\n pending.resolve({\n beforeTokens: result.beforeTokens,\n afterTokens: result.afterTokens,\n summaryTokens: result.summaryTokens,\n durationMs: Date.now() - startedAt,\n hooksFiredCount: result.hooksFiredCount,\n summary: result.summary ?? '',\n applied,\n ...(applied ? {} : { skippedReason: 'nothing-to-trim' as const }),\n });\n }\n\n const handoffNames = Array.from(handoffMap.keys());\n let stepNumber = 0;\n // C3: verifier-triggered continuation rounds consumed this run.\n let verifierRoundsUsed = 0;\n // AG-15: tools the model actually called on the PREVIOUS step - the\n // per-tool preferred-model ladder consults these, never the full\n // advertised catalogue.\n let lastStepCalledToolNames: ReadonlyArray<string> = [];\n\n // AG-6: shared cancellation path for both the loop-top abort check and a\n // mid-stream provider abort. Yields `agent.cancelling`, applies the\n // `onPendingApprovals` policy, and returns `true` when the run was finalized\n // as 'failed' (the 'fail' policy - the caller must `return finalize(...)`);\n // otherwise it sets `state.status = 'aborted'` and returns `false`.\n async function* emitCancellation(): AsyncGenerator<AgentEvent<TOutput>, boolean, void> {\n yield {\n type: 'agent.cancelling',\n runId: state.id,\n drain: pendingAbort?.drain ?? false,\n onPendingApprovals: pendingAbort?.onPendingApprovals ?? 'deny',\n };\n const policy = pendingAbort?.onPendingApprovals ?? 'deny';\n if (policy === 'deny') {\n const drained = state.pendingApprovals.splice(0, state.pendingApprovals.length);\n for (const approval of drained) {\n yield {\n type: 'tool.approval.denied',\n toolCallId: approval.toolCallId,\n reason: 'auto-denied: agent.abort()',\n };\n }\n } else if (policy === 'fail') {\n state.status = 'failed';\n state.error = { message: 'aborted with pending approvals', code: 'run-aborted' };\n yield {\n type: 'agent.error',\n error: { message: 'aborted with pending approvals', code: 'run-aborted' },\n };\n return true;\n }\n state.status = 'aborted';\n return false;\n }\n\n try {\n while (!stopWhen.check(state)) {\n // Drain any externally-queued lifecycle events\n // (`agent.steered`, `agent.followup.queued`).\n while (externalEventQueue.length > 0) {\n const ev = externalEventQueue.shift();\n if (ev !== undefined) yield ev;\n }\n if (signal.aborted) {\n if (yield* emitCancellation()) return yield* finishRun(state, finalSnapshot);\n break;\n }\n stepNumber += 1;\n const stepStart = new Date().toISOString();\n\n // Drain steering queue.\n if (pendingSteer.length > 0) {\n for (const m of pendingSteer) {\n messages.push(m);\n state.messages.push(m);\n }\n pendingSteer = [];\n }\n\n yield { type: 'step.start', stepNumber };\n // C7: defensive end for a span left open by a mid-step exit path.\n currentStepSpan?.end();\n currentStepSpan = tracer.startSpan({\n type: 'agent.step',\n parent: runSpan,\n attrs: {\n 'gen_ai.operation.name': 'invoke_agent',\n 'gen_ai.agent.id': agentId,\n 'gen_ai.agent.name': config.name,\n 'graphorin.run.id': state.id,\n 'graphorin.step.number': stepNumber,\n },\n });\n\n // WI-09 (P1-1): bound context growth before the provider call.\n // Fires `context.compacted` and rewrites the buffer in place only\n // when the memory ContextEngine's trigger crosses threshold; a\n // no-memory / below-threshold / secret-tier step is a no-op, so\n // the happy-path event stream is unchanged (R10).\n yield* maybeAutoCompact();\n\n const stepCtx: RunContext<TDeps> = {\n ...runContextBase,\n stepNumber,\n messages,\n ...(currentStepSpan !== undefined ? { span: currentStepSpan } : {}),\n };\n const overrides = config.prepareStep ? await config.prepareStep(stepCtx) : {};\n\n // Resolve the registry + executor for this step. The warm-up\n // pair is bound to config.tools + skills; a `prepareStep` tool\n // override builds a step-scoped pair so the advertised catalogue\n // and the executor agree on the same tool set (incl. deferred\n // discovery for the overridden set). Code-mode does not honour a\n // per-step `tools` override (the meta-tools + bridge are bound to\n // the warm-up registry), so it always uses the warm-up pair.\n const useOverrideRegistry = overrides.tools !== undefined && !isCodeMode;\n const stepRegistry: ToolRegistry = useOverrideRegistry\n ? buildToolRegistry({\n tools: overrides.tools as ReadonlyArray<Tool<unknown, unknown, unknown>>,\n }).registry\n : toolRegistry;\n if (useOverrideRegistry) {\n registerToolSearch(\n stepRegistry,\n config.toolPromotion === 'run-boundary' ? 'next-run' : 'next-step',\n );\n registerReadResult(stepRegistry, resultReader);\n }\n const stepExecutor: ToolExecutor = useOverrideRegistry\n ? makeToolExecutor(stepRegistry)\n : toolExecutor;\n\n // Build the per-step tool catalogue. Handoff tools are synthetic\n // per-step entries and are always advertised.\n const handoffTools: Tool<unknown, unknown, TDeps>[] = handoffNames.map((n) => {\n const h = handoffMap.get(n);\n if (h === undefined) throw new ToolNotFoundError(n);\n return buildHandoffTool<TDeps>(h.agent);\n });\n // Code-mode (WI-11): advertise only the `code_search` /\n // `code_execute` (+ `read_result`) meta-tools - the model reaches\n // every real tool through `code_execute`, so the real tools stay\n // registered (executable via the in-script bridge) but out of the\n // model's catalogue. Otherwise (WI-05): advertise the eager tools\n // (`tool_search` is itself eager iff a deferred tool exists) plus\n // any deferred tools already promoted by a `tool_search` this run -\n // never the rest of the deferred pool.\n // D2 single-writer constraint: a read-only run never ADVERTISES\n // writer tools (side-effecting / external-stateful) nor handoffs\n // (a transfer hands the writer pen to another agent). The\n // executor-level capability gate backs this up for calls the\n // model fabricates anyway.\n const readOnlyRun = activeRunCapability === 'read-only';\n const keepReadOnly = (t: Tool<unknown, unknown, TDeps>): boolean => {\n const cls = (t as { __sideEffectClass?: string }).__sideEffectClass ?? t.sideEffectClass;\n return cls === 'pure' || cls === 'read-only';\n };\n let stepTools: ReadonlyArray<Tool<unknown, unknown, TDeps>>;\n if (isCodeMode) {\n stepTools = readOnlyRun\n ? [...codeModeAdvertised.filter(keepReadOnly)]\n : [...codeModeAdvertised, ...handoffTools];\n } else {\n const eagerTools = stepRegistry.listEager() as ReadonlyArray<\n Tool<unknown, unknown, TDeps>\n >;\n // A7: emit promoted deferred tools in PROMOTION order (append-only) so\n // a later promotion joins the END and the prompt-cache prefix stays\n // byte-stable across steps. C1 (agent-11): handoff tools serialize\n // BEFORE the growing promoted section - handoffs are fixed per run,\n // so the stable prefix is now eager + handoffs + earlier promotions\n // and a new promotion shifts nothing that came before it.\n // 'run-boundary' promotion advertises only the run-start snapshot.\n const advertisedPromotions = runStartPromotions ?? promotedDeferred;\n const promotedTools = (\n advertisedPromotions.size === 0\n ? []\n : orderPromotedTools(advertisedPromotions, stepRegistry.listDeferred())\n ) as ReadonlyArray<Tool<unknown, unknown, TDeps>>;\n stepTools = readOnlyRun\n ? [...eagerTools.filter(keepReadOnly), ...promotedTools.filter(keepReadOnly)]\n : [...eagerTools, ...handoffTools, ...promotedTools];\n }\n\n // AG-15: consult the hints of the tools the model CALLED on the\n // previous step - a smart-hinted but never-called tool must not\n // pin the whole conversation to the top cost tier. Steps with no\n // prior calls fall through to the agent-preferred default.\n const calledLastStep = new Set(lastStepCalledToolNames);\n const toolPreferences = stepTools\n .filter((t) => calledLastStep.has(t.name))\n .map((t) => {\n const tt = t as Tool<unknown, unknown, TDeps> & { readonly preferredModel?: unknown };\n return tt.preferredModel as Parameters<\n typeof resolvePreferredModel\n >[0]['toolPreferredModels'][number];\n });\n\n const primary: PreferredModelResolution = resolvePreferredModel({\n ...(overrides.provider !== undefined ? { prepareStepProvider: overrides.provider } : {}),\n toolPreferredModels: toolPreferences,\n ...(config.preferredModel !== undefined\n ? { agentPreferredModel: config.preferredModel }\n : {}),\n agentDefaultProvider: config.provider,\n ...(config.modelTierMap !== undefined ? { modelTierMap: config.modelTierMap } : {}),\n });\n\n // RB-48: when `prepareStep` returns an explicit provider\n // override, the fallback chain is NOT consulted for this\n // step (the operator's explicit choice supersedes the\n // implicit fallback chain).\n const fallbackChain: Provider[] =\n primary.source === 'prepare-step'\n ? [primary.resolvedProvider]\n : [primary.resolvedProvider, ...(config.fallbackModels ?? []).map(specToProvider)];\n\n // Resolve the effective reasoning-retention policy for\n // this step (RB-42). Drop any buffered reasoning when the\n // contract downgrades to `'strip'`.\n const reasoningPolicy = effectiveReasoningRetention(\n config.reasoningRetention,\n primary.resolvedProvider,\n );\n if (reasoningPolicy === 'strip') {\n const { stripped } = stripReasoningFromMessages(messages);\n // Mirror the strip into RunState so the persisted state\n // matches the in-flight buffer.\n if (stripped > 0) {\n // The structural drop is bytes-equal across `messages`\n // and `state.messages` (both arrays carry the same\n // references); re-strip RunState explicitly to be safe.\n stripReasoningFromMessages(state.messages);\n }\n }\n\n const toolDefs: ReadonlyArray<ToolDefinition> = stepTools.map((t) =>\n toolToDefinition(t as Tool<unknown, unknown, unknown>),\n );\n\n const baseRequest: ProviderRequest = {\n // AG-3 fallback contract: for structured output the request\n // carries ONE trailing system instruction (JSON-only + schema)\n // in the request copy - never in the shared buffer or the\n // persisted RunState. Adapters with native structured output\n // additionally receive `outputType` below (PS-24 consumes it).\n messages: buildStepMessages(messages, structuredInstruction, activeRunState?.todos),\n ...(config.outputType !== undefined\n ? {\n outputType: {\n kind: config.outputType.kind,\n ...(config.outputType.description !== undefined\n ? { description: config.outputType.description }\n : {}),\n ...(config.outputType.jsonSchema !== undefined\n ? { jsonSchema: config.outputType.jsonSchema }\n : {}),\n },\n }\n : {}),\n tools: toolDefs,\n ...(overrides.toolChoice !== undefined\n ? { toolChoice: overrides.toolChoice }\n : config.toolChoice !== undefined\n ? { toolChoice: config.toolChoice as ToolChoice }\n : {}),\n metadata: {\n sessionId,\n agentId,\n ...(userId !== undefined ? { userId } : {}),\n runId: state.id,\n stepNumber,\n },\n signal,\n ...(overrides.temperature !== undefined ? { temperature: overrides.temperature } : {}),\n ...(overrides.maxTokens !== undefined ? { maxTokens: overrides.maxTokens } : {}),\n ...(config.cachePolicy !== undefined ? { cachePolicy: config.cachePolicy } : {}),\n ...(currentStepSpan !== undefined ? { parentSpan: currentStepSpan } : {}),\n reasoningRetention: reasoningPolicy,\n };\n\n const stepUsage: Usage = zeroUsage();\n let attempt = 0;\n let textBuffer = '';\n let providerForStep = primary.resolvedProvider;\n let lastModelId = primary.resolvedModelId;\n let modelSucceeded = false;\n let lastError: ProviderError | undefined;\n let finalCalls: ToolCall[] = [];\n let stepReasoningParts: ReasoningContent[] = [];\n // context-engine-06: the request actually sent - rebuilt after an\n // emergency compaction so the retry carries the shrunk buffer\n // even on the structured-output path (which snapshots messages).\n let requestForStep = baseRequest;\n let emergencyCompactTried = false;\n\n for (let chainIdx = 0; chainIdx < fallbackChain.length; chainIdx++) {\n const candidate = fallbackChain[chainIdx];\n if (candidate === undefined) continue;\n providerForStep = candidate;\n const providerModelId = providerForStep.modelId;\n if (chainIdx > 0) {\n attempt += 1;\n const reason = lastError\n ? (isAgentFallbackEligible(lastError, fallbackPolicy).reason ?? 'transient')\n : 'transient';\n yield {\n type: 'agent.model.fellback',\n runId: state.id,\n sessionId,\n agentId,\n from: lastModelId,\n to: providerModelId,\n reason,\n stepNumber,\n attempt,\n };\n lastModelId = providerModelId;\n }\n const evState: ProviderEventCollector = {\n textBuffer: '',\n reasoningBuffer: '',\n reasoningParts: [],\n calls: new Map<string, ToolCallAccumulator>(),\n finalCalls: [] as ToolCall[],\n };\n let providerError: ProviderError | undefined;\n let providerCallCompleted = false;\n let providerStepUsage: Usage = zeroUsage();\n try {\n const stream = providerForStep.stream(requestForStep);\n for await (const ev of stream) {\n // AG-6 `drain`: the default hard-kills the in-flight provider\n // stream mid-event; `abort({ drain: true })` instead lets the\n // current step finish (the documented \"wait for the current step\n // to complete\") and stops gracefully at the next loop-top check.\n if (signal.aborted && pendingAbort?.drain !== true) {\n throw new AgentRuntimeError('run-aborted', 'aborted');\n }\n const out = handleProviderEvent(ev, evState);\n if (out.emit !== undefined) {\n yield out.emit as AgentEvent<TOutput>;\n }\n if (out.providerError !== undefined) {\n providerError = out.providerError;\n }\n if (out.usage !== undefined) {\n providerStepUsage = addUsage(providerStepUsage, out.usage);\n }\n if (out.finished === true) providerCallCompleted = true;\n }\n } catch (cause) {\n // AG-6: a mid-stream abort (our run-aborted sentinel, or any error\n // once the signal is aborted - e.g. a native AbortError from the\n // provider) is NOT a provider failure. Break out of the fallback\n // chain WITHOUT a providerError; the post-stream abort check below\n // ends the run as 'aborted', never 'no-provider-completed'. Don't\n // continue the fallback chain against an already-aborted signal.\n if (\n signal.aborted ||\n (cause instanceof AgentRuntimeError && cause.code === 'run-aborted')\n ) {\n break;\n }\n const message = cause instanceof Error ? cause.message : String(cause);\n // AG-21: preserve the thrown error's kind (e.g. a RateLimitExceededError\n // from `withRateLimit`) so the fallback chain treats it like the same\n // error emitted as a structured event, instead of flattening it to\n // an always-ineligible 'unknown'.\n providerError = { kind: classifyThrownProviderErrorKind(cause), message, cause };\n }\n if (providerError !== undefined) {\n lastError = providerError;\n // context-engine-06: a hard context overflow gets ONE\n // emergency-compaction retry against the SAME candidate\n // before the fallback chain (whose members usually share the\n // window) or a terminal failure.\n if (providerError.kind === 'context-length' && !emergencyCompactTried) {\n emergencyCompactTried = true;\n const shrank = yield* tryEmergencyCompact();\n if (shrank) {\n requestForStep = {\n ...baseRequest,\n messages: buildStepMessages(\n messages,\n structuredInstruction,\n activeRunState?.todos,\n ),\n };\n chainIdx -= 1; // negate the loop increment: retry this candidate\n continue;\n }\n }\n const eligibility = isAgentFallbackEligible(providerError, fallbackPolicy);\n if (!eligibility.eligible || chainIdx === fallbackChain.length - 1) {\n yield {\n type: 'agent.error',\n error: { message: providerError.message, code: providerError.kind },\n };\n state.status = 'failed';\n state.error = { message: providerError.message, code: providerError.kind };\n return yield* finishRun(state, finalSnapshot);\n }\n continue;\n }\n if (providerCallCompleted) {\n modelSucceeded = true;\n textBuffer = evState.textBuffer;\n finalCalls = evState.finalCalls;\n // Materialize the streamed reasoning deltas into a\n // single `ReasoningContent` part. Adapters that expose\n // structured reasoning blocks may emit multiple\n // deltas; v0.1 collapses them into one part - Phase\n // 06 owns the per-block structure when it lands.\n if (evState.reasoningBuffer.length > 0) {\n stepReasoningParts = [\n {\n type: 'reasoning',\n text: evState.reasoningBuffer,\n },\n ];\n }\n accumulateUsage(stepUsage, providerStepUsage);\n break;\n }\n }\n\n // AG-6: a mid-stream abort that interrupted the stream (no completed\n // model) ends the run as a cancellation ('aborted', or 'failed' under\n // the `onPendingApprovals: 'fail'` policy) rather than falling through\n // to a 'no-provider-completed' failure. When the model DID complete\n // (e.g. `drain: true` let the step finish), fall through so the step's\n // tool calls run and the graceful stop happens at the loop top.\n if (signal.aborted && !modelSucceeded) {\n yield* emitCancellation();\n return yield* finishRun(state, finalSnapshot);\n }\n\n if (!modelSucceeded) {\n yield {\n type: 'agent.error',\n error: {\n message: 'all configured providers failed without finishing',\n code: 'no-provider-completed',\n },\n };\n state.status = 'failed';\n state.error = { message: 'no provider completed', code: 'no-provider-completed' };\n return yield* finishRun(state, finalSnapshot);\n }\n\n usageAcc.add(lastModelId, stepUsage);\n addModelUsage(state, lastModelId, stepUsage);\n accumulateUsage(state.usage, stepUsage);\n\n // Lateral-leak (RB-55 / AG-10): scan the outgoing assistant\n // content BEFORE it is appended, so a 'block' decision keeps\n // the laundered commentary out of the durable history - and\n // therefore out of every subsequent provider request. The\n // deltas already streamed; what 'block' protects is the\n // persistent buffer and the run's final output.\n const leakCheck =\n causalityMonitor !== undefined && textBuffer.length > 0\n ? causalityMonitor.checkMessage(textBuffer)\n : undefined;\n const leakBlocked = leakCheck?.leakDetected === true && leakCheck.decision === 'block';\n\n const assistant: AssistantMessage = buildAssistantMessage(\n leakBlocked ? LATERAL_LEAK_BLOCKED_NOTICE : textBuffer,\n stepReasoningParts,\n finalCalls,\n agentId,\n reasoningPolicy,\n );\n messages.push(assistant);\n state.messages.push(assistant);\n\n // C6: once the run is tainted, the model's own TEXT output is\n // derived from untrusted context - record it so a later sink call\n // copying the model's phrasing still trips the verbatim probe\n // (no-op on untainted runs). Tool-call args are deliberately NOT\n // recorded: the sink gate inspects those same args next, and\n // recording them first would self-match every post-taint call,\n // collapsing the precise verbatim signal into the coarse one.\n if (toolDataFlowGuard !== undefined && textBuffer.length > 0 && !leakBlocked) {\n toolDataFlowGuard.recordAssistant(state.id, textBuffer);\n }\n\n if (leakCheck?.leakDetected === true) {\n const sha = sha256Hex(textBuffer);\n yield {\n type: 'agent.lateral-leak.detected',\n runId: state.id,\n sessionId,\n agentId,\n vector: leakCheck.vector,\n severity: leakCheck.severity,\n causalityChain: leakCheck.causalityChain,\n messageContentSha256: sha,\n ...(leakCheck.matchedPattern !== undefined\n ? { matchedPattern: leakCheck.matchedPattern }\n : {}),\n decision: leakCheck.decision,\n detectedAtIso: new Date().toISOString(),\n };\n }\n\n const handoffCalls = finalCalls.filter((c) => handoffMap.has(c.toolName));\n if (handoffCalls.length > 1) {\n throw new MultipleHandoffsInStepError(handoffCalls.map((c) => c.toolName));\n }\n\n const stepRecord: RunStep = {\n stepNumber,\n startedAt: stepStart,\n endedAt: new Date().toISOString(),\n usage: stepUsage,\n toolCalls: [],\n agentId: state.currentAgentId,\n // C3: journal the RAW model response (pre-leak-block text) so\n // createReplayProvider(state) can re-drive the run offline.\n ...(config.recordProviderResponses === true\n ? {\n providerResponse: {\n modelId: lastModelId,\n ...(textBuffer.length > 0 ? { text: textBuffer } : {}),\n ...(finalCalls.length > 0 ? { toolCalls: [...finalCalls] } : {}),\n },\n }\n : {}),\n };\n state.steps.push(stepRecord);\n lastStepCalledToolNames = finalCalls.map((c) => c.toolName);\n\n if (textBuffer.length > 0 && !leakBlocked) {\n finalSnapshot.output = textBuffer as unknown as TOutput;\n yield { type: 'text.complete', text: textBuffer };\n }\n\n if (finalCalls.length > 0) {\n // `stepRegistry` / `stepExecutor` were resolved with the\n // catalogue above (so the advertised tools and the executor's\n // resolvable tools agree, including any `prepareStep` override).\n const execRunContext: RunContext<TDeps> = {\n ...runContextBase,\n stepNumber,\n messages,\n ...(currentStepSpan !== undefined ? { span: currentStepSpan } : {}),\n };\n\n // Walk calls in finalCalls order. Handoffs are special-cased\n // inline (≤1 per step) and never routed through the executor.\n // Non-handoff calls accumulate into a batch dispatched through\n // the ToolExecutor; the batch is flushed before a handoff and\n // before each approval-gated call so execution order is kept.\n // Gated calls are COLLECTED (all of them, agent-01) and the run\n // suspends once after the walk, so every non-gated toolCallId\n // has a tool message before the suspend snapshot - a dropped\n // call would persist a dangling `tool_use` that real providers\n // reject on resume.\n let batch: ToolCall[] = [];\n let stepApprovalsRequested = 0;\n\n for (const call of finalCalls) {\n const handoff = handoffMap.get(call.toolName);\n if (handoff !== undefined) {\n if (batch.length > 0) {\n yield* dispatchBatch(batch, stepExecutor, execRunContext, stepNumber);\n batch = [];\n }\n yield { type: 'tool.execute.start', toolCallId: call.toolCallId };\n const filter = (handoff.filter ??\n filterLib.defaultHandoffFilter()) as DescribedFilter;\n const filtered = filter(messages);\n const targetId = handoff.agent.id;\n // The secrets fields record the structural reality: no\n // inheritance mechanism exists at this boundary, so the\n // target receives nothing - an empty allowlist is the\n // factually-true provenance (AG-17).\n const handoffRec: HandoffRecord = {\n fromAgentId: agentId,\n toAgentId: targetId,\n stepNumber,\n at: new Date().toISOString(),\n inputFilter: filter.descriptor,\n secretsInheritance: 'inherit-allowlist',\n inheritedSecrets: [],\n };\n state.handoffs.push(handoffRec);\n yield { type: 'handoff', fromAgentId: agentId, toAgentId: targetId };\n state.currentAgentId = targetId;\n const subAgent = handoff.agent;\n // AG-22: the sub-agent inherits the parent's abort signal,\n // deps, and sessionId; its terminal `agent.end` is observed\n // so a failed/aborted sub-run surfaces as a TOOL ERROR -\n // never an empty-string success with durationMs 0.\n const subStart = Date.now();\n const subOutputs: string[] = [];\n let subResult: AgentResult<unknown> | undefined;\n const subStream = subAgent.stream(filtered as Message[], {\n signal,\n ...(options.deps !== undefined || config.deps !== undefined\n ? { deps: (options.deps ?? config.deps) as TDeps }\n : {}),\n sessionId,\n });\n for await (const subEv of subStream) {\n if (subEv.type === 'text.complete') subOutputs.push(subEv.text);\n else if (subEv.type === 'agent.end') {\n subResult = subEv.result as AgentResult<unknown>;\n }\n }\n const subDurationMs = Date.now() - subStart;\n const stepEntry = state.steps[state.steps.length - 1];\n if (subResult !== undefined && subResult.status !== 'completed') {\n const toolError: ToolError = {\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n kind: subResult.status === 'aborted' ? 'aborted' : 'execution_failed',\n message: `handoff to '${targetId}' ${subResult.status}${\n subResult.error !== undefined ? `: ${subResult.error.message}` : ''\n }`,\n };\n if (stepEntry !== undefined) {\n (stepEntry.toolCalls as CompletedToolCall[]).push({\n call,\n outcome: toolError,\n stepNumber,\n });\n }\n yield {\n type: 'tool.execute.error',\n toolCallId: call.toolCallId,\n error: toolError,\n };\n const text = renderToolErrorMessage(toolError);\n messages.push({ role: 'tool', toolCallId: call.toolCallId, content: text });\n state.messages.push({ role: 'tool', toolCallId: call.toolCallId, content: text });\n continue;\n }\n const result = subOutputs.join('');\n const completed: CompletedToolCall = {\n call,\n outcome: {\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n output: result,\n durationMs: subDurationMs,\n },\n stepNumber,\n };\n if (stepEntry !== undefined) {\n (stepEntry.toolCalls as CompletedToolCall[]).push(completed);\n }\n yield {\n type: 'tool.execute.end',\n toolCallId: call.toolCallId,\n result,\n durationMs: subDurationMs,\n };\n messages.push({ role: 'tool', toolCallId: call.toolCallId, content: result });\n state.messages.push({\n role: 'tool',\n toolCallId: call.toolCallId,\n content: result,\n });\n continue;\n }\n\n // Approval pre-screen (Adapter G / durable HITL). Evaluate the\n // registry-resolved `needsApproval`; a gated call flushes the\n // queued batch (prior calls' side-effects complete first) and\n // is recorded as a pending approval. The walk CONTINUES: later\n // gated calls are collected too, and later non-gated calls\n // still execute before the suspend (agent-01 - previously\n // everything after the first gated call was silently dropped,\n // never executed and never approvable, leaving dangling\n // `tool_use` ids in the persisted transcript).\n const resolvedTool = stepRegistry.get(call.toolName);\n // tools-02 (agent mirror): the approval decision must be made\n // on the input that will actually execute. For gated tools the\n // args are validated HERE: a schema failure fails the call fast\n // as `invalid_input` (a human is never asked to approve args\n // that cannot run, and the resumed dispatch can therefore never\n // hit the repair hook), and the predicate receives the parsed\n // value its typed signature promises - not raw pre-coercion\n // JSON.\n let gateInput: unknown = call.args;\n if (resolvedTool !== undefined && isApprovalGated(resolvedTool)) {\n const parsed = safeParseGatedArgs(resolvedTool, call.args);\n if (parsed !== undefined && !parsed.success) {\n const toolError: ToolError = {\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n kind: 'invalid_input',\n message: `Invalid arguments for approval-gated tool '${call.toolName}': ${parsed.message}`,\n };\n const stepEntry = state.steps[state.steps.length - 1];\n if (stepEntry !== undefined) {\n (stepEntry.toolCalls as CompletedToolCall[]).push({\n call,\n outcome: toolError,\n stepNumber,\n });\n }\n yield { type: 'tool.execute.start', toolCallId: call.toolCallId };\n yield {\n type: 'tool.execute.error',\n toolCallId: call.toolCallId,\n error: toolError,\n };\n const text = renderToolErrorMessage(toolError);\n messages.push({ role: 'tool', toolCallId: call.toolCallId, content: text });\n state.messages.push({ role: 'tool', toolCallId: call.toolCallId, content: text });\n continue;\n }\n if (parsed !== undefined) gateInput = parsed.data;\n }\n const needsApproval = await invokeNeedsApproval(\n resolvedTool,\n gateInput,\n execRunContext,\n signal,\n );\n if (needsApproval) {\n if (batch.length > 0) {\n yield* dispatchBatch(batch, stepExecutor, execRunContext, stepNumber);\n batch = [];\n }\n yield { type: 'tool.execute.start', toolCallId: call.toolCallId };\n const approval: ToolApproval = {\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n args: call.args,\n requestedAt: new Date().toISOString(),\n };\n state.pendingApprovals.push(approval);\n stepApprovalsRequested += 1;\n yield {\n type: 'tool.approval.requested',\n toolCallId: call.toolCallId,\n };\n continue;\n }\n\n batch.push(call);\n }\n\n if (batch.length > 0) {\n yield* dispatchBatch(batch, stepExecutor, execRunContext, stepNumber);\n }\n\n // Durable-HITL suspend: once per step, carrying EVERY gated call\n // the model batched. Runs after the final batch flush so the\n // suspend snapshot already contains tool messages for the whole\n // non-gated remainder of the step.\n if (stepApprovalsRequested > 0) {\n state.status = 'awaiting_approval';\n // AG-19: persist the coarse taint summary + promoted-tool set into\n // the suspended state so a resume rehydrates the sink gate and the\n // discovered-tool catalogue instead of starting empty.\n const taintSnap = toolDataFlowGuard?.snapshotLedger(state.id);\n if (taintSnap !== undefined) state.taintSummary = taintSnap;\n if (promotedDeferred.size > 0) state.promotedTools = [...promotedDeferred];\n if (config.checkpointStore !== undefined) {\n await config.checkpointStore.put(\n state.id,\n 'agent',\n {\n id: state.id,\n threadId: state.id,\n namespace: 'agent',\n // AG-23: persist a detached, secret-redacted snapshot -\n // never the live MutableRunState reference.\n state: serializeRunState(state, { stripTracingApiKey: true }),\n channelVersions: {},\n stepNumber,\n createdAt: new Date().toISOString(),\n },\n { source: 'sync', status: 'suspended', nodeName: 'agent.run' },\n );\n }\n return yield* finishRun(state, finalSnapshot);\n }\n }\n\n currentStepSpan?.setAttributes({\n 'gen_ai.usage.input_tokens': stepUsage.promptTokens,\n 'gen_ai.usage.output_tokens': stepUsage.completionTokens,\n });\n currentStepSpan?.end();\n currentStepSpan = undefined;\n yield { type: 'step.end', stepNumber, usage: stepUsage };\n\n if (finalCalls.length === 0) {\n // C3: verifier seam - deterministic checks run on EVERY terminal\n // response (so the outcome is always observable via\n // verifier.result events). Failures feed structured feedback back\n // as a user message and the loop continues, but only while\n // continuation rounds remain (maxVerifierRounds, default 1);\n // exhausted rounds complete with the last output. A verifier that\n // throws is treated as passed (a buggy verifier must not fail the\n // run).\n if (config.verifiers !== undefined && config.verifiers.length > 0) {\n const feedbacks: string[] = [];\n for (const verifier of config.verifiers) {\n let result: VerifierResult;\n try {\n result = await verifier.verify({\n output: String(finalSnapshot.output ?? ''),\n state,\n stepNumber,\n });\n } catch {\n result = { ok: true };\n }\n yield {\n type: 'verifier.result',\n verifierId: verifier.id,\n ok: result.ok,\n ...(result.ok ? {} : { feedback: result.feedback }),\n stepNumber,\n };\n if (!result.ok) feedbacks.push(`[verifier:${verifier.id}] ${result.feedback}`);\n }\n if (feedbacks.length > 0 && verifierRoundsUsed < (config.maxVerifierRounds ?? 1)) {\n verifierRoundsUsed += 1;\n const feedbackMessage: Message = {\n role: 'user',\n content: `Your response failed ${feedbacks.length} verification check(s). Address the feedback and answer again:\\n${feedbacks.join('\\n')}`,\n };\n messages.push(feedbackMessage);\n state.messages.push(feedbackMessage);\n continue;\n }\n }\n state.status = 'completed';\n break;\n }\n }\n } catch (cause) {\n const message = cause instanceof Error ? cause.message : String(cause);\n const code = cause instanceof AgentRuntimeError ? (cause.code as string) : 'unknown';\n yield { type: 'agent.error', error: { message, code } };\n state.status = 'failed';\n state.error = { message, code };\n return yield* finishRun(state, finalSnapshot);\n } finally {\n // C7: close the trace tree on every exit path.\n currentStepSpan?.end();\n currentStepSpan = undefined;\n runSpan.setAttributes({\n 'gen_ai.usage.input_tokens': state.usage.promptTokens,\n 'gen_ai.usage.output_tokens': state.usage.completionTokens,\n 'graphorin.run.status': state.status,\n });\n runSpan.setStatus(state.status === 'failed' ? 'error' : 'ok');\n runSpan.end();\n // AG-5: drop the parent-signal listener so it does not accumulate across\n // runs that share one long-lived `options.signal`. Runs after this point\n // (the follow-up loop) keep working via `agent.abort()` on `localCtl`.\n if (parentSignal !== undefined) {\n parentSignal.removeEventListener('abort', onParentAbort);\n }\n }\n\n if (state.status === 'running') {\n // AG-24: the loop exited via the stop condition (default\n // isStepCount(50)) with work still pending - that is a CUT run,\n // not a completion. Surface it as a typed failure so consumers\n // can tell it apart from a clean finish.\n const message = `run stopped by stop condition: ${stopWhen.description}`;\n state.status = 'failed';\n state.error = { message, code: 'stop-condition' };\n yield { type: 'agent.error', error: { message, code: 'stop-condition' } };\n }\n // AG-3: structured output is parsed + validated on the completed\n // path - a failure is a typed run failure (`output-validation-failed`),\n // never a silent text-cast. Runs BEFORE output guardrails so they\n // screen the PARSED value.\n if (state.status === 'completed' && config.outputType?.kind === 'structured') {\n const raw = String(finalSnapshot.output ?? '');\n try {\n const parsed: unknown = JSON.parse(stripJsonFence(raw));\n finalSnapshot.output = (\n config.outputType.schema !== undefined ? config.outputType.schema.parse(parsed) : parsed\n ) as TOutput;\n } catch (cause) {\n const message = `structured output validation failed: ${\n cause instanceof Error ? cause.message : String(cause)\n }`;\n yield { type: 'agent.error', error: { message, code: 'output-validation-failed' } };\n state.status = 'failed';\n state.error = { message, code: 'output-validation-failed' };\n }\n }\n\n // AG-2 / SDF-4: output guardrails screen the final output on the\n // completed path before `agent.end`. 'block' fails the run;\n // 'rewrite' replaces the durable result (`result.output`) - the\n // text deltas were already streamed, so the rewrite governs what\n // is persisted/returned, not the live token stream.\n const outputGuards = config.guardrails?.output;\n if (state.status === 'completed' && outputGuards !== undefined && outputGuards.length > 0) {\n const composed = await composeGuardrails(outputGuards, finalSnapshot.output, {\n stage: 'output',\n runId: state.id,\n sessionId,\n agentId,\n });\n if (!composed.ok) {\n yield {\n type: 'guardrail.tripped',\n guardrailName: composed.name,\n phase: 'output',\n reason: composed.message,\n };\n const message = `output guardrail '${composed.name}' blocked the run: ${composed.message}`;\n yield { type: 'agent.error', error: { message, code: 'guardrail-blocked' } };\n state.status = 'failed';\n state.error = { message, code: 'guardrail-blocked' };\n } else if (composed.value !== finalSnapshot.output) {\n finalSnapshot.output = composed.value;\n }\n }\n activeRunState = undefined;\n return yield* finishRun(state, finalSnapshot);\n }\n\n /**\n * Terminal wrapper around {@link finalize}: every exit path of the run\n * loop - completed, failed, aborted, suspended - ends the stream with\n * an `agent.end` event carrying the final {@link AgentResult} (AG-20).\n */\n async function* finishRunBase(\n state: MutableRunState,\n snapshot: InternalRunSnapshot<TOutput>,\n ): AsyncGenerator<AgentEvent<TOutput>, AgentResult<TOutput>, void> {\n // CE-3/AG-13: settle manual-compact requests the loop never serviced -\n // the run is over, there is no live buffer left to splice.\n while (pendingManualCompacts.length > 0) {\n pendingManualCompacts.shift()?.resolve(noopCompactionResult('no-active-run'));\n }\n const result = finalize(state, snapshot);\n // TL-10: spill artifacts are run-scoped scratch - drop them once the\n // run is terminal. `awaiting_approval` and `aborted` runs keep\n // theirs (handles must survive resume); orphans fall to the writer's\n // startup TTL sweep.\n if (result.status === 'completed' || result.status === 'failed') {\n await spillWriter.clear?.(result.state.id).catch(() => {});\n }\n yield { type: 'agent.end', runId: result.state.id, result };\n return result;\n }\n\n function finalize(\n state: MutableRunState,\n snapshot: InternalRunSnapshot<TOutput>,\n ): AgentResult<TOutput> {\n state.finishedAt = state.finishedAt ?? new Date().toISOString();\n // AG-9: the result carries the terminal status, the failure (when\n // any), and the final RunState - a suspended run is resumable from\n // the result alone, no checkpointStore required.\n return {\n output: snapshot.output,\n usage: state.usage,\n status: state.status,\n ...(state.error !== undefined ? { error: state.error } : {}),\n state: state as unknown as RunState,\n };\n }\n\n const stream = (\n input: AgentInput | RunState,\n options?: AgentCallOptions<TDeps>,\n ): AsyncIterable<AgentEvent<TOutput>> => {\n const opts = options ?? {};\n return {\n [Symbol.asyncIterator]: () => {\n const gen = runLoop(input, opts);\n return {\n async next(): Promise<IteratorResult<AgentEvent<TOutput>, void>> {\n const r = await gen.next();\n if (r.done === true) {\n return { done: true, value: undefined };\n }\n return { done: false, value: r.value };\n },\n async return(): Promise<IteratorResult<AgentEvent<TOutput>, void>> {\n await gen.return(undefined as unknown as AgentResult<TOutput>);\n return { done: true, value: undefined };\n },\n };\n },\n };\n };\n\n const run = async (\n input: AgentInput | RunState,\n options?: AgentCallOptions<TDeps>,\n ): Promise<AgentResult<TOutput>> => {\n const opts = options ?? {};\n const gen = runLoop(input, opts);\n let next = await gen.next();\n while (next.done !== true) {\n next = await gen.next();\n }\n // Every terminal path of the run loop returns `finalize(...)`; an\n // undefined return value would mean the generator was torn down\n // externally - an invariant violation, not a run outcome (AG-9).\n const result = next.value;\n if (result === undefined) {\n throw new Error('unreachable: agent run loop ended without a result');\n }\n return result;\n };\n\n const steer = (message: AgentInput): void => {\n const { seed } = asMessages(message);\n pendingSteer.push(...seed);\n if (activeRunState !== undefined) {\n externalEventQueue.push({\n type: 'agent.steered',\n runId: activeRunState.id,\n } as AgentEvent<TOutput>);\n }\n };\n\n const followUp = (message: AgentInput): void => {\n const { seed } = asMessages(message);\n pendingFollowUp.push(...seed);\n if (activeRunState !== undefined) {\n externalEventQueue.push({\n type: 'agent.followup.queued',\n runId: activeRunState.id,\n } as AgentEvent<TOutput>);\n }\n };\n\n const abort = (options?: AbortOptions): void => {\n pendingAbort = options ?? {};\n abortController?.abort();\n };\n\n // D2: distil a completed child run into a compact, bounded outcome the\n // parent folds into its context instead of the raw transcript/output.\n const foldRunOutcome = (result: AgentResult<TOutput>, maxChars: number): string => {\n const steps = result.state.steps;\n const toolNames = [\n ...new Set(steps.flatMap((step) => step.toolCalls.map((c) => c.call.toolName))),\n ];\n const header =\n `[sub-agent '${config.name}' outcome] status=${result.status}; ` +\n `steps=${steps.length}; toolCalls=${steps.reduce((n, st) => n + st.toolCalls.length, 0)}` +\n (toolNames.length > 0 ? `; tools=${toolNames.join(', ')}` : '');\n const text = typeof result.output === 'string' ? result.output : JSON.stringify(result.output);\n const body =\n text.length > maxChars\n ? `${text.slice(0, maxChars)}\\n[... ${text.length - maxChars} chars truncated by contextFold]`\n : text;\n return `${header}\\n${body}`;\n };\n\n // D2: carry the child's coarse taint flags across the fold so the\n // parent's data-flow ledger re-arms (widen-only; a no-op when the\n // parent has no dataFlowPolicy).\n const taintFromChildState = (\n state: RunState,\n ): { untrusted?: boolean; sensitive?: boolean; sourceKind?: string } | undefined => {\n const summary = state.taintSummary;\n if (summary === undefined || (!summary.untrustedSeen && !summary.sensitiveSeen)) {\n return undefined;\n }\n return {\n ...(summary.untrustedSeen ? { untrusted: true } : {}),\n ...(summary.sensitiveSeen ? { sensitive: true } : {}),\n sourceKind: 'sub-agent',\n };\n };\n\n const toTool = (\n options: AgentToToolOptions = {},\n ): Tool<{ readonly input: string }, TOutput, TDeps> => {\n const exposeTurns = options.exposeTurns ?? 'final';\n const foldMaxChars =\n options.contextFold === undefined || options.contextFold === false\n ? null\n : typeof options.contextFold === 'object'\n ? (options.contextFold.maxChars ?? 2000)\n : 2000;\n const propagateTaint = options.propagateTaint !== false;\n const toolName = options.name ?? `subagent_${config.name}`;\n const description = options.description ?? `Invoke sub-agent '${config.name}'.`;\n const schema = {\n parse: (v: unknown): { readonly input: string } => v as { readonly input: string },\n safeParse: (v: unknown) => ({\n success: true as const,\n data: v as { readonly input: string },\n }),\n toJSON: (): Record<string, unknown> => ({\n type: 'object',\n properties: { input: { type: 'string' } },\n required: ['input'],\n }),\n };\n const tool: Tool<{ readonly input: string }, TOutput, TDeps> = {\n name: toolName,\n description,\n inputSchema: schema as unknown as Tool<\n { readonly input: string },\n TOutput,\n TDeps\n >['inputSchema'],\n sideEffectClass: 'side-effecting',\n async execute(input, ctx) {\n // AG-17: the parent ToolExecutionContext propagates - the\n // parent's abort stops the sub-agent, deps/sessionId flow\n // through, and the optional `inputFilter` shapes a seed from\n // the parent history. Least authority by default: without a\n // filter the sub-agent sees ONLY the input string, never the\n // parent conversation.\n const callOpts: AgentCallOptions<TDeps> = {\n ...(ctx?.signal !== undefined ? { signal: ctx.signal } : {}),\n ...(ctx?.runContext.deps !== undefined ? { deps: ctx.runContext.deps as TDeps } : {}),\n ...(ctx?.runContext.sessionId !== undefined\n ? { sessionId: ctx.runContext.sessionId }\n : {}),\n // D2: run the child under a restricted capability (read-only\n // workers in an orchestrator-worker fan-out).\n ...(options.capability !== undefined ? { capability: options.capability } : {}),\n };\n const seed: AgentInput =\n options.inputFilter !== undefined && ctx !== undefined\n ? ([\n ...options.inputFilter(ctx.runContext.messages),\n { role: 'user' as const, content: input.input },\n ] as Message[])\n : input.input;\n if (exposeTurns === 'all') {\n // Replay the streamed text completions as the result so\n // the parent agent sees every turn the sub-agent\n // produced. `exposeTurns: 'final'` (default) and\n // `'none'` skip the per-turn assembly.\n const turns: string[] = [];\n let endResult: AgentResult<TOutput> | undefined;\n for await (const ev of stream(seed, callOpts)) {\n if (ev.type === 'text.complete') turns.push(ev.text);\n else if (ev.type === 'agent.end') endResult = ev.result;\n }\n if (endResult !== undefined && endResult.status !== 'completed') {\n throw new Error(\n `sub-agent '${config.name}' ${endResult.status}${\n endResult.error !== undefined ? `: ${endResult.error.message}` : ''\n }`,\n );\n }\n const allOutput = (foldMaxChars !== null && endResult !== undefined\n ? foldRunOutcome(endResult, foldMaxChars)\n : turns.join('\\n\\n')) as unknown as TOutput;\n const allTaint =\n propagateTaint && endResult !== undefined\n ? taintFromChildState(endResult.state)\n : undefined;\n return (allTaint !== undefined\n ? { output: allOutput, taint: allTaint }\n : allOutput) as unknown as TOutput;\n }\n const result = await run(seed, callOpts);\n // AG-17/AG-22 class: a non-completed sub-run is a TOOL ERROR,\n // never an empty-string success.\n if (result.status !== 'completed') {\n throw new Error(\n `sub-agent '${config.name}' ${result.status}${\n result.error !== undefined ? `: ${result.error.message}` : ''\n }`,\n );\n }\n const taint = propagateTaint ? taintFromChildState(result.state) : undefined;\n const output = (exposeTurns === 'none'\n ? ''\n : foldMaxChars !== null\n ? foldRunOutcome(result, foldMaxChars)\n : result.output) as unknown as TOutput;\n return (taint !== undefined ? { output, taint } : output) as unknown as TOutput;\n },\n };\n return tool;\n };\n\n const compact = async (options?: CompactOptions): Promise<CompactionApiResult> => {\n // No memory wired - an explicit no-op (AG-13), intentionally\n // forgiving so example apps that don't wire memory don't crash\n // on `agent.compact()`.\n if (memory === undefined) return noopCompactionResult('no-memory');\n // Sensitivity gate (WI-09 step 2) applies to MANUAL compaction\n // too: secret-tier history never ships to the summarizer.\n if (config.sensitivity === 'secret') return noopCompactionResult('sensitivity-gated');\n // Idle - there is no live buffer to splice (AG-13's explicit\n // no-op marker, where the old surface silently reported zeros).\n if (activeRunState === undefined) return noopCompactionResult('no-active-run');\n // CE-3/AG-13: the run loop owns the live buffer, so the splice\n // happens there - enqueue and let `maybeAutoCompact` service the\n // request at the next step boundary with the same prefix-pinned\n // splice as auto-compaction. Don't await this from inside a tool\n // handler: the loop can't reach the next step until the tool\n // returns.\n return await new Promise<CompactionApiResult>((resolve, reject) => {\n pendingManualCompacts.push({ options, resolve, reject });\n });\n };\n\n const fanOut = async <TFanOutOutput = unknown>(\n options: AgentFanOutOptions<TFanOutOutput>,\n ): Promise<FanOutResult<TFanOutOutput>> => {\n const runId = activeRunState?.id ?? `run_${newId()}`;\n const sessionId = activeRunState?.sessionId ?? `session_${newId()}`;\n const fanOutOptions: RunFanOutOptions<TFanOutOutput> = {\n children: options.children,\n ...(options.maxConcurrentChildren !== undefined\n ? { maxConcurrentChildren: options.maxConcurrentChildren }\n : {}),\n ...(options.perBudget !== undefined ? { perBudget: options.perBudget } : {}),\n ...(options.mergeStrategy !== undefined ? { mergeStrategy: options.mergeStrategy } : {}),\n ...(options.signal !== undefined ? { signal: options.signal } : {}),\n // AG-7: fanout lifecycle events reach the agent stream - queued\n // on the external-event queue and drained into the active (or\n // next consumed) run, like steer/follow-up/progress events.\n emit: (event) => {\n externalEventQueue.push(event as AgentEvent<TOutput>);\n },\n // AG-7: the configured sideways-injection merge guard finally\n // applies to the judge-merge path.\n ...(config.mergeGuard !== undefined ? { mergeGuard: config.mergeGuard } : {}),\n runId,\n sessionId,\n agentId,\n };\n return runFanOut<TFanOutOutput>(fanOutOptions);\n };\n\n // Stable fallback id so out-of-run `progress.write` → `progress.read`\n // pairs resolve to the same artifact directory (a fresh id per call\n // could never find what it just wrote).\n const progressFallbackRunId = `run_${newId()}`;\n const progress: AgentProgressIO = {\n write: async (content: string, opts?: ProgressWriteOptions) => {\n const runId = activeRunState?.id ?? progressFallbackRunId;\n const ref = await progressIO.write(runId, content, opts);\n // AG-20: surface the documented `agent.progress.written` event -\n // queued here and drained into the active (or next consumed) stream.\n externalEventQueue.push({\n type: 'agent.progress.written',\n runId,\n sessionId: activeRunState?.sessionId ?? '',\n agentId,\n ref,\n } as AgentEvent<TOutput>);\n return ref;\n },\n read: async (opts?: ProgressReadOptions) => {\n const queriedRunId = opts?.runId ?? activeRunState?.id ?? progressFallbackRunId;\n const refs = await progressIO.read(queriedRunId, opts);\n externalEventQueue.push({\n type: 'agent.progress.read',\n runId: activeRunState?.id ?? queriedRunId,\n sessionId: activeRunState?.sessionId ?? '',\n agentId,\n refs,\n queriedRunId,\n queriedRole: opts?.role,\n } as AgentEvent<TOutput>);\n return refs;\n },\n };\n\n void config.sensitivity as Sensitivity | undefined;\n\n const agent: Agent<TDeps, TOutput> = {\n id: agentId,\n config,\n stream,\n run,\n steer,\n followUp,\n abort,\n toTool,\n compact,\n fanOut,\n progress,\n registry: toolRegistry,\n };\n\n return agent;\n}\n\n/**\n * Pre-execution approval screen (Adapter G / durable HITL). Evaluates a\n * (registry-resolved) tool's `needsApproval` against the realized args.\n * Returns `true` when the run must suspend before the tool executes.\n *\n * Actual execution flows through the `@graphorin/tools` executor, whose\n * `ApprovalGate` auto-grants because only no-approval / pre-approved\n * calls ever reach it; this probe is what keeps the suspend in the\n * agent so the durable-HITL contract (persist `RunState`, resume via\n * directive) is preserved.\n */\nasync function invokeNeedsApproval(\n tool: Pick<Tool, 'needsApproval'> | undefined,\n args: unknown,\n baseCtx: RunContext,\n signal: AbortSignal,\n): Promise<boolean> {\n const predicate = tool?.needsApproval;\n if (predicate === undefined || predicate === false) return false;\n if (predicate === true) return true;\n const probeCtx: ToolExecutionContext = {\n toolCallId: 'probe',\n runContext: baseCtx,\n signal,\n tracer: baseCtx.tracer,\n logger: NOOP_LOGGER,\n secrets: probeSecretsAccessor(),\n reportProgress: () => {},\n streamContent: () => {},\n };\n return Boolean(await predicate(args as never, probeCtx));\n}\n\n/**\n * Rejecting secrets accessor used only by the {@link invokeNeedsApproval}\n * probe. Real tool execution resolves secrets through the executor's\n * ACL-scoped accessor; an approval predicate has no legitimate need to\n * read secret material, so every `require(...)` rejects.\n */\nfunction probeSecretsAccessor(): ToolExecutionContext['secrets'] {\n const rejector = (_key: string, _options?: { readonly optional?: boolean }): Promise<never> =>\n Promise.reject(new Error('secrets.require is unavailable inside a needsApproval predicate'));\n return { require: rejector } as unknown as ToolExecutionContext['secrets'];\n}\n\n/**\n * tools-02: validate an approval-gated call's args at the pre-screen so\n * the gate decision - and what a human is asked to approve - is the input\n * that will actually execute. Structural + defensive: `undefined` when\n * the tool exposes no callable `safeParse` (nothing to validate here; the\n * executor still validates at dispatch); a throwing schema counts as a\n * validation failure rather than crashing the loop.\n */\nfunction safeParseGatedArgs(\n tool: { readonly inputSchema?: unknown },\n args: unknown,\n):\n | { readonly success: true; readonly data: unknown }\n | { readonly success: false; readonly message: string }\n | undefined {\n const schema = tool.inputSchema as { safeParse?: (value: unknown) => unknown } | null | undefined;\n const safeParse = schema?.safeParse;\n if (typeof safeParse !== 'function') return undefined;\n try {\n const parsed = safeParse.call(schema, args) as {\n readonly success?: boolean;\n readonly data?: unknown;\n readonly error?: { readonly message?: string };\n };\n if (parsed.success === true) return { success: true, data: parsed.data };\n return {\n success: false,\n message: parsed.error?.message ?? 'schema validation failed',\n };\n } catch (cause) {\n return {\n success: false,\n message: cause instanceof Error ? cause.message : String(cause),\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,MAAM,8BACJ;AA4EF,MAAM,aAAa,UACjB,WAAW,SAAS,CAAC,OAAO,OAAO,OAAO,CAAC,OAAO,MAAM;AAE1D,MAAM,sBAAsB;;AAG5B,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F,QAAO,EAAE,SAAS,UAAU;;;;;;;;;;AAW9B,SAAS,mBACP,MACA,MAIY;CACZ,MAAM,SAAS,KAAK;CACpB,MAAM,YAAY,QAAQ;AAC1B,KAAI,OAAO,cAAc,WAAY,QAAO;AAC5C,KAAI;EACF,MAAM,SAAS,UAAU,KAAK,QAAQ,KAAK;AAK3C,MAAI,OAAO,YAAY,KAAM,QAAO;GAAE,SAAS;GAAM,MAAM,OAAO;GAAM;AACxE,SAAO;GACL,SAAS;GACT,SAAS,OAAO,OAAO,WAAW;GACnC;UACM,OAAO;AACd,SAAO;GACL,SAAS;GACT,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;GAChE"}
1
+ {"version":3,"file":"factory.js","names":["fallbackPolicy: AgentFallbackPolicy","subAgent: Agent<TDeps, unknown>","pendingSteer: Message[]","pendingFollowUp: Message[]","abortController: AbortController | undefined","pendingAbort: AbortOptions | undefined","activeRunState: RunState | undefined","activeRunCapability: 'read-only' | undefined","externalEventQueue: AgentEvent<TOutput>[]","pendingManualCompacts: PendingManualCompact[]","memory: Memory | undefined","progressIO: ProgressIO","executorBridgeSlot: { current: ExecutorEventBridge | undefined }","messages: Message[]","finalSnapshot: InternalRunSnapshot<TOutput>","currentStepSpan: AISpan<'agent.step'> | undefined","resumedApprovedCalls: ToolCall[]","grantedApprovals: ToolApproval[]","runContextBase: RunContext<TDeps>","runEnv: RunLoopEnv","lastStepCalledToolNames: ReadonlyArray<string>","stepCtx: RunContext<TDeps>","stepRecord: RunStep","progress: AgentProgressIO"],"sources":["../src/factory.ts"],"sourcesContent":["/**\n * `createAgent({...})` - the agent factory entry point.\n *\n * Wires the typed `model -> tool calls -> model` loop, the streamed\n * event surface, the steering / followUp queues, durable HITL via\n * `RunState`, the multi-agent handoff layer, the agent-level model\n * fallback chain, and the per-tool preferred-model resolution.\n *\n * Custom adapters override behaviour by supplying alternative\n * `Provider` / `Memory` / `CheckpointStore` instances; the loop\n * never reaches into adapter internals.\n *\n * @packageDocumentation\n */\n\nimport type {\n AgentEvent,\n AgentResult,\n AISpan,\n Message,\n ProviderRequest,\n RunContext,\n RunState,\n RunStep,\n Sensitivity,\n Tool,\n ToolApproval,\n ToolCall,\n ToolDefinition,\n} from '@graphorin/core';\nimport { isStepCount, NOOP_TRACER } from '@graphorin/core';\nimport type { Memory } from '@graphorin/memory';\n\nimport {\n AgentRuntimeError,\n ConcurrentRunError,\n InvalidAgentConfigError,\n InvalidPreferredModelError,\n MultipleHandoffsInStepError,\n} from './errors/index.js';\nimport type { AgentFallbackPolicy } from './fallback/index.js';\nimport type { DescribedFilter } from './filters/index.js';\nimport { newId } from './internal/ids.js';\nimport { InMemoryUsageAccumulator } from './internal/usage-accumulator.js';\nimport { CausalityMonitor } from './lateral-leak/causality-monitor.js';\nimport { createProgressIO, type ProgressIO } from './progress/index.js';\nimport { addModelUsage, serializeRunState } from './run-state/index.js';\nimport {\n createCompactMethod,\n createFanOutMethod,\n createProgressSurface,\n createRunMethods,\n} from './runtime/agent-surface.js';\nimport { createToTool } from './runtime/agent-to-tool.js';\nimport {\n dispatchResumedApprovals,\n processResumeDirective,\n processSubRunResumes,\n type ResumedDispatchEnv,\n type ResumeRunEnv,\n type RoutedSubRunDecision,\n type SubRunResumeEnv,\n} from './runtime/approvals.js';\nimport { type DispatchRunEnv, dispatchToolBatch } from './runtime/dispatch.js';\nimport { wireToolExecution } from './runtime/executor-wiring.js';\nimport { type FallbackChainEnv, runProviderFallbackChain } from './runtime/fallback-chain.js';\nimport { HANDOFF_TOOL_PREFIX, isDescribedFilter } from './runtime/handoff.js';\nimport {\n accumulateUsage,\n applyReasoningRetention,\n countLeadingSystemMessages,\n} from './runtime/messages.js';\nimport {\n type CompactionRunEnv,\n maybeAutoCompact,\n noopCompactionResult,\n type PendingManualCompact,\n tryEmergencyCompact,\n} from './runtime/run-compaction.js';\nimport { createRunFinisher } from './runtime/run-finish.js';\nimport {\n type AssistantCommitEnv,\n buildStructuredInstruction,\n type CancellationEnv,\n commitAssistantMessage,\n emitCancellation,\n finalizeRunOutput,\n type GuardrailScreenEnv,\n type RunOutputEnv,\n runVerifierGate,\n screenInputGuardrails,\n type VerifierGateEnv,\n} from './runtime/run-gates.js';\nimport { initializeRunState, seedInitialMessages } from './runtime/run-init.js';\nimport {\n asMessages,\n type InternalRunSnapshot,\n isModelHintLike,\n isModelSpecLike,\n type MutableRunState,\n validatePreferredModel,\n} from './runtime/run-input.js';\nimport {\n buildBaseRequest,\n resolveStepToolContext,\n type StepCatalogueEnv,\n type StepRequestEnv,\n toolToDefinition,\n} from './runtime/step-catalogue.js';\nimport { processStepToolCalls, type ToolCallWalkEnv } from './runtime/tool-call-walk.js';\nimport type { ExecutorEventBridge } from './tooling/adapters.js';\nimport type {\n AbortOptions,\n Agent,\n AgentCallOptions,\n AgentConfig,\n AgentInput,\n AgentProgressIO,\n SubagentForwardPolicy,\n} from './types.js';\n\n/**\n * Build a fresh {@link Agent} from the supplied configuration.\n *\n * @stable\n */\nexport function createAgent<TDeps = unknown, TOutput = string>(\n config: AgentConfig<TDeps, TOutput>,\n): Agent<TDeps, TOutput> {\n if (typeof config.name !== 'string' || config.name.length === 0) {\n throw new InvalidAgentConfigError(\"missing 'name'\");\n }\n if (config.provider === undefined || config.provider === null) {\n throw new InvalidAgentConfigError(\"missing 'provider'\");\n }\n // AG-3: a schema on a text-kind output spec is a config mistake (the\n // schema would never run) - reject instead of silently ignoring.\n if (config.outputType?.kind === 'text' && config.outputType.schema !== undefined) {\n throw new InvalidAgentConfigError(\n \"outputType.kind 'text' with a schema - did you mean kind: 'structured'?\",\n );\n }\n validatePreferredModel(config.preferredModel);\n if (config.modelTierMap !== undefined) {\n for (const [tier, spec] of Object.entries(config.modelTierMap)) {\n if (!isModelHintLike(tier)) throw new InvalidPreferredModelError({ tier });\n if (spec === undefined) continue;\n if (!isModelSpecLike(spec)) throw new InvalidPreferredModelError(spec);\n }\n }\n if (config.fallbackModels !== undefined) {\n for (const spec of config.fallbackModels) {\n if (!isModelSpecLike(spec)) throw new InvalidPreferredModelError(spec);\n }\n }\n\n const agentId = newId('agent');\n const tracer = config.tracer ?? NOOP_TRACER;\n const stopWhen = config.stopWhen ?? isStepCount(50);\n const fallbackPolicy: AgentFallbackPolicy = config.fallbackPolicy ?? {};\n const handoffMap = new Map<\n string,\n {\n readonly agent: Agent<TDeps, unknown>;\n readonly filter: DescribedFilter | undefined;\n readonly forwardEvents: SubagentForwardPolicy | undefined;\n }\n >();\n for (const entry of config.handoffs ?? []) {\n const isWrappedHandoff = typeof entry === 'object' && entry !== null && 'target' in entry;\n const subAgent: Agent<TDeps, unknown> = isWrappedHandoff\n ? (entry as { readonly target: Agent<TDeps, unknown> }).target\n : (entry as Agent<TDeps, unknown>);\n const userFilter = isWrappedHandoff\n ? (\n entry as {\n readonly inputFilter?:\n | DescribedFilter\n | ((history: readonly Message[]) => readonly Message[]);\n }\n ).inputFilter\n : undefined;\n const filter = isDescribedFilter(userFilter) ? userFilter : undefined;\n const forwardEvents = isWrappedHandoff\n ? (entry as { readonly forwardEvents?: SubagentForwardPolicy }).forwardEvents\n : undefined;\n const toolName = `${HANDOFF_TOOL_PREFIX}${subAgent.config.name}`;\n handoffMap.set(toolName, { agent: subAgent, filter, forwardEvents });\n }\n\n let pendingSteer: Message[] = [];\n const pendingFollowUp: Message[] = [];\n let abortController: AbortController | undefined;\n let pendingAbort: AbortOptions | undefined;\n // Per-run scratch refs surfaced through the public surface for\n // event emission from `steer(...)` / `followUp(...)`.\n let activeRunState: RunState | undefined;\n // D2: capability of the ACTIVE run - read by the code-mode bridge so\n // in-script tool calls inherit the run's single-writer restriction.\n let activeRunCapability: 'read-only' | undefined;\n /** AG-11: guards the one-in-flight-run-per-instance invariant. */\n let runInFlight = false;\n const externalEventQueue: AgentEvent<TOutput>[] = [];\n\n const pendingManualCompacts: PendingManualCompact[] = [];\n\n const memory: Memory | undefined = config.memory;\n const progressIO: ProgressIO = createProgressIO({\n ...(config.sensitivity !== undefined ? { defaultSensitivity: config.sensitivity } : {}),\n });\n\n // Warm-up tool stack (Principle #12 / WI-03 / WI-05 / WI-10 / WI-11 /\n // WI-13 / D6): registry assembly, built-in registration (tool_search,\n // plan, read_result), guard hooks, the shared ToolExecutor factory and\n // the code-mode surface - wired in `runtime/executor-wiring.ts`. The\n // registry is exposed read-only as `agent.registry`.\n const executorBridgeSlot: { current: ExecutorEventBridge | undefined } = { current: undefined };\n const {\n toolRegistry,\n spillWriter,\n resultReader,\n makeToolExecutor,\n toolExecutor,\n toolDataFlowGuard,\n ruleOfTwoCapabilityFloor,\n isCodeMode,\n codeModeAdvertised,\n } = wireToolExecution<TDeps, TOutput>({\n config,\n memory,\n agentId,\n handoffToolNames: handoffMap.keys(),\n getActiveRunState: () => activeRunState,\n getActiveRunCapability: () => activeRunCapability,\n executorBridgeSlot,\n });\n\n const causalityMonitor = config.causalityMonitor\n ? new CausalityMonitor(config.causalityMonitor)\n : undefined;\n\n /**\n * AG-11: one in-flight run per Agent instance. `steer` / `followUp` /\n * `abort` / `compact` address \"the run\" with no run handle, so\n * overlapping runs on one instance cannot be expressed safely - they\n * would share the abort controller, steer queue, active-run ref and\n * executor bridge. A second concurrent `run()` / `stream()` rejects\n * with {@link ConcurrentRunError}; run-scoped state is reset on entry\n * (a steer/abort queued after the previous run ended belongs to NO\n * run) and cleared in a `finally` that also covers abandoned streams\n * (consumer `break`) and thrown runs.\n */\n async function* runLoop(\n input: AgentInput | RunState,\n options: AgentCallOptions<TDeps>,\n ): AsyncGenerator<AgentEvent<TOutput>, AgentResult<TOutput>, void> {\n if (runInFlight) {\n throw new ConcurrentRunError();\n }\n runInFlight = true;\n pendingSteer = [];\n pendingAbort = undefined;\n // D2 + D4: per-run capability - the call-level override wins, then\n // the agent default, then the Rule-of-Two floor (a profile denying\n // external side effects forces read-only even without an explicit\n // capability). Absent ⇒ all capabilities (legacy behaviour).\n activeRunCapability = options.capability ?? config.capability ?? ruleOfTwoCapabilityFloor;\n // AG-10: the causality chain is a per-run artifact - a denial\n // recorded in one run must not poison detection in the next.\n causalityMonitor?.reset();\n try {\n return yield* runLoopInner(input, options);\n } finally {\n runInFlight = false;\n activeRunState = undefined;\n activeRunCapability = undefined;\n // Backstop for exits that bypass `finishRun` (abandoned stream,\n // generator teardown): settle queued manual compactions so no\n // `agent.compact()` promise is left hanging (CE-3/AG-13).\n while (pendingManualCompacts.length > 0) {\n pendingManualCompacts.shift()?.resolve(noopCompactionResult('no-active-run'));\n }\n }\n }\n\n async function* runLoopInner(\n input: AgentInput | RunState,\n options: AgentCallOptions<TDeps>,\n ): AsyncGenerator<AgentEvent<TOutput>, AgentResult<TOutput>, void> {\n const { seed: rawSeed, resumed } = asMessages(input);\n // AG-12: queued follow-ups are next-turn metadata - they ride into\n // the next FRESH run as leading user turns. The old path mutated a\n // finished run back to 'running' and appended the message to a loop\n // that never processed it, leaving a non-terminal persisted RunState\n // with a dangling user turn. Resumed runs keep the queue intact.\n const seed =\n resumed === undefined && pendingFollowUp.length > 0\n ? [...pendingFollowUp.splice(0, pendingFollowUp.length), ...rawSeed]\n : rawSeed;\n const sessionId = options.sessionId ?? config.sessionId ?? `session_${newId()}`;\n const userId = options.userId ?? config.userId;\n const localCtl = new AbortController();\n abortController = localCtl;\n // AG-5: the loop + every provider request must observe the LOCAL\n // controller, so `agent.abort()` (which aborts `localCtl`) is honoured even\n // when the caller supplied their own `options.signal`. The caller's signal\n // is propagated INTO `localCtl` by the listener below; the listener is torn\n // down in the run's `finally` so it does not accumulate across runs that\n // share one long-lived parent signal.\n const signal = localCtl.signal;\n const parentSignal = options.signal;\n const onParentAbort = (): void => localCtl.abort();\n if (parentSignal !== undefined) {\n if (parentSignal.aborted) localCtl.abort();\n else parentSignal.addEventListener('abort', onParentAbort);\n }\n\n const usageAcc = new InMemoryUsageAccumulator();\n // Bootstrap the run state, the AG-19 security rehydration and the\n // `tool_search` promotion set (see `runtime/run-init.ts`).\n const { state, promotedDeferred, runStartPromotions } = initializeRunState<TDeps, TOutput>(\n { config, agentId, sessionId, userId, toolDataFlowGuard },\n resumed,\n );\n activeRunState = state;\n\n // agent-08 (F4): capture the run-scoped security state on EVERY exit\n // through finishRun - not just the approval suspend. An 'aborted' run\n // is resumable (the AG-14 guard blocks only awaiting_approval/failed)\n // and a 'completed' run re-enters as a follow-up; both must rehydrate\n // the enforce-mode sink gate and the discovered-tool catalogue.\n // Shadows the factory-scope finishRunBase for every call in this run.\n async function* finishRun(\n s: MutableRunState & RunState,\n snapshot: InternalRunSnapshot<TOutput>,\n ): AsyncGenerator<AgentEvent<TOutput>, AgentResult<TOutput>, void> {\n const taintSnap = toolDataFlowGuard?.snapshotLedger(s.id);\n if (taintSnap !== undefined) {\n (s as { taintSummary?: typeof taintSnap }).taintSummary = taintSnap;\n }\n if (promotedDeferred.size > 0) {\n (s as { promotedTools?: readonly string[] }).promotedTools = [...promotedDeferred];\n }\n return yield* finishRunBase(s, snapshot);\n }\n\n const messages: Message[] = resumed ? [...state.messages] : [];\n if (!resumed) {\n // Fresh run: resolve `instructions` (AG-8), optionally assemble\n // the memory-aware system prompt (CE-1), and seed the buffer +\n // RunState mirror (see `runtime/run-init.ts`).\n await seedInitialMessages<TDeps, TOutput>(\n { config, options, memory, agentId, sessionId, userId, tracer, signal, usageAcc, state },\n messages,\n seed,\n );\n }\n\n const finalSnapshot: InternalRunSnapshot<TOutput> = {\n output: '' as unknown as TOutput,\n };\n\n // C7: one agent.run span per run; step/tool/provider spans parent\n // under it so the whole run is a single trace tree. Attributes follow\n // the OTel GenAI semantic conventions (gen_ai.*).\n const runSpan = tracer.startSpan({\n type: 'agent.run',\n // W-036: a sub-agent invocation parents under the caller's live\n // step/tool span so multi-agent load forms ONE trace tree.\n ...(options.parentSpan !== undefined ? { parent: options.parentSpan } : {}),\n attrs: {\n 'gen_ai.operation.name': 'invoke_agent',\n 'gen_ai.agent.id': agentId,\n 'gen_ai.agent.name': config.name,\n 'graphorin.run.id': state.id,\n 'graphorin.session.id': sessionId,\n 'graphorin.run.resumed': resumed !== undefined,\n },\n });\n let currentStepSpan: AISpan<'agent.step'> | undefined;\n\n yield { type: 'agent.start', runId: state.id, agentId };\n\n // AG-2 / SDF-4: input guardrails screen each fresh-run seed user\n // message (string content) BEFORE the first provider call, using the\n // canonical `@graphorin/security` composer. 'block' fails the run\n // without reaching the model; 'rewrite' replaces the content in both\n // the working buffer and the persisted RunState; 'warn' logs and\n // continues. Resumed runs skip the pass - their seed was screened\n // when first submitted.\n const inputGuards = config.guardrails?.input;\n if (!resumed && inputGuards !== undefined && inputGuards.length > 0) {\n const blocked = yield* screenInputGuardrails<TOutput>(\n { state, messages, sessionId, agentId },\n inputGuards,\n );\n if (blocked) {\n return yield* finishRun(state, finalSnapshot);\n }\n }\n\n // AG-3: one per-run JSON instruction for structured output, appended\n // to each provider request (never the shared buffer / RunState).\n const structuredInstruction =\n config.outputType?.kind === 'structured'\n ? buildStructuredInstruction(config.outputType)\n : undefined;\n\n // AG-1: approved gated calls collected from a resume directive, executed\n // for real once every approval is resolved (see the dispatch below).\n const resumedApprovedCalls: ToolCall[] = [];\n // agent-02: the ToolApproval records behind `resumedApprovedCalls`,\n // kept so the write-ahead intent checkpoint can re-attach them to\n // `pendingApprovals` (a crash-retry against the intent re-dispatches).\n const grantedApprovals: ToolApproval[] = [];\n // W-001: decisions addressed to PARKED sub-agent runs, grouped by the\n // parent toolCallId of the park (routed into the children below).\n const subRunDecisions = new Map<string, RoutedSubRunDecision[]>();\n // Process resume directive - apply approval decisions to any\n // pending approvals captured in the previous suspend.\n if (\n resumed &&\n options.directive?.approvals !== undefined &&\n state.pendingApprovals.length > 0\n ) {\n yield* processResumeDirective<TOutput>(\n { state, messages },\n options.directive.approvals,\n resumedApprovedCalls,\n grantedApprovals,\n subRunDecisions,\n );\n }\n // AG-14: the resumed status is left untouched here. A 'failed' run is NOT\n // silently rewritten to 'completed' (the terminal/suspended guard below\n // returns it as-is); a 'completed' run keeps its status and re-enters the\n // loop for a follow-up; an unresolved 'awaiting_approval' run is caught by\n // that same guard.\n\n // WI-09: pin the trusted system-prompt prefix length now, on the\n // fully-assembled initial buffer, so auto-compaction never rewrites\n // it and prior summaries stay re-compactable (see\n // `countLeadingSystemMessages`).\n const systemPrefixLength = countLeadingSystemMessages(messages);\n\n const runContextBase: RunContext<TDeps> = {\n runId: state.id,\n sessionId,\n ...(userId !== undefined ? { userId } : {}),\n agentId,\n deps: (options.deps ?? config.deps) as TDeps,\n tracer,\n signal,\n usage: usageAcc,\n stepNumber: 0,\n messages,\n state,\n };\n\n const handoffNames = Array.from(handoffMap.keys());\n\n /**\n * One run-scoped env threaded into every extracted runtime module\n * (issue #23): the intersection structurally satisfies each\n * module's env interface, so a single object carries the live loop\n * references the former closures captured (field names mirror the\n * original locals). `getPendingAbort` / `getActiveTodos` are live\n * reads of the factory's mutable scratch; `tryEmergencyCompact` and\n * `dispatchBatch` are pre-bound to this same env.\n */\n type RunLoopEnv = DispatchRunEnv &\n CompactionRunEnv &\n CancellationEnv &\n GuardrailScreenEnv &\n AssistantCommitEnv &\n FallbackChainEnv<TOutput> &\n StepCatalogueEnv<TDeps, TOutput> &\n StepRequestEnv<TDeps, TOutput> &\n ToolCallWalkEnv<TDeps, TOutput> &\n ResumeRunEnv &\n ResumedDispatchEnv<TDeps, TOutput> &\n SubRunResumeEnv<TDeps, TOutput> &\n VerifierGateEnv<TDeps, TOutput> &\n RunOutputEnv<TDeps, TOutput>;\n const runEnv: RunLoopEnv = {\n config,\n options,\n memory,\n state,\n messages,\n sessionId,\n agentId,\n userId,\n signal,\n usageAcc,\n stopWhen,\n fallbackPolicy,\n structuredInstruction,\n systemPrefixLength,\n runContextBase,\n handoffMap,\n handoffNames,\n isCodeMode,\n toolRegistry,\n toolExecutor,\n makeToolExecutor,\n resultReader,\n codeModeAdvertised,\n causalityMonitor,\n toolDataFlowGuard,\n promotedDeferred,\n runStartPromotions,\n activeRunCapability,\n executorBridgeSlot,\n pendingManualCompacts,\n getPendingAbort: () => pendingAbort,\n // W-036: live read of the current step span for sub-agent trace\n // stitching (the child's agent.run parents under it).\n getCurrentStepSpan: () => currentStepSpan,\n getActiveTodos: () => activeRunState?.todos,\n tryEmergencyCompact: () => tryEmergencyCompact<TOutput>(runEnv),\n dispatchBatch: (calls, executor, runContext, stepNum, dispatchOpts) =>\n dispatchToolBatch<TDeps, TOutput>(\n runEnv,\n calls,\n executor,\n runContext,\n stepNum,\n dispatchOpts,\n ),\n };\n\n // AG-14 (failed half): a terminal-failed run must never re-enter the\n // provider loop or dispatch anything - that would silently complete a\n // failed run. Return it as-is.\n if (resumed && state.status === 'failed') {\n return yield* finishRun(state, finalSnapshot);\n }\n\n // AG-1 / agent-02 / agent-07: execute the approved gated calls for\n // REAL before the provider loop, bracketed by the write-ahead intent\n // + post-dispatch checkpoints (see `runtime/approvals.ts`).\n if (resumed && resumedApprovedCalls.length > 0) {\n yield* dispatchResumedApprovals<TDeps, TOutput>(\n runEnv,\n resumedApprovedCalls,\n grantedApprovals,\n );\n }\n\n // W-001: route sub-run decisions into their parked children (grant\n // and deny alike - the child settles them in its own transcript). A\n // child that completes folds back as the parent's tool message; one\n // that suspends again re-parks and the parent re-suspends below.\n if (resumed && subRunDecisions.size > 0) {\n yield* processSubRunResumes<TDeps, TOutput>(runEnv, subRunDecisions);\n }\n\n // AG-14 (suspended half): a resumed run still awaiting approvals the\n // directive did not resolve must not re-enter the provider loop - that\n // would re-issue a dangling tool_use real providers reject. The granted\n // subset (above) HAS executed and is journaled; return the re-suspended\n // state carrying its results. The same guard covers a held 'aborted'\n // state (W-038, `onPendingApprovals: 'hold'`): its pending approvals\n // still map to dangling tool_use, so it resumes only through an\n // explicit directive.\n if (\n resumed &&\n (state.status === 'awaiting_approval' ||\n (state.status === 'aborted' && state.pendingApprovals.length > 0))\n ) {\n return yield* finishRun(state, finalSnapshot);\n }\n\n // W-035: seed from the journal so post-resume steps continue the\n // numbering instead of colliding with pre-suspend steps (a fresh\n // run's empty journal keeps the historical 0 start).\n let stepNumber = state.steps.reduce((max, s) => Math.max(max, s.stepNumber), 0);\n // C3: verifier-triggered continuation rounds consumed this run.\n let verifierRoundsUsed = 0;\n // AG-15: tools the model actually called on the PREVIOUS step - the\n // per-tool preferred-model ladder consults these, never the full\n // advertised catalogue.\n let lastStepCalledToolNames: ReadonlyArray<string> = [];\n\n try {\n while (!stopWhen.check(state)) {\n // Drain any externally-queued lifecycle events\n // (`agent.steered`, `agent.followup.queued`).\n while (externalEventQueue.length > 0) {\n const ev = externalEventQueue.shift();\n if (ev !== undefined) yield ev;\n }\n if (signal.aborted) {\n if (yield* emitCancellation<TOutput>(runEnv)) {\n return yield* finishRun(state, finalSnapshot);\n }\n break;\n }\n stepNumber += 1;\n const stepStart = new Date().toISOString();\n\n // Drain steering queue.\n if (pendingSteer.length > 0) {\n for (const m of pendingSteer) {\n messages.push(m);\n state.messages.push(m);\n }\n pendingSteer = [];\n }\n\n yield { type: 'step.start', stepNumber };\n // C7: defensive end for a span left open by a mid-step exit path.\n currentStepSpan?.end();\n currentStepSpan = tracer.startSpan({\n type: 'agent.step',\n parent: runSpan,\n attrs: {\n 'gen_ai.operation.name': 'invoke_agent',\n 'gen_ai.agent.id': agentId,\n 'gen_ai.agent.name': config.name,\n 'graphorin.run.id': state.id,\n 'graphorin.step.number': stepNumber,\n },\n });\n\n // WI-09 (P1-1): bound context growth before the provider call.\n // Fires `context.compacted` and rewrites the buffer in place only\n // when the memory ContextEngine's trigger crosses threshold; a\n // no-memory / below-threshold / secret-tier step is a no-op, so\n // the happy-path event stream is unchanged (R10).\n yield* maybeAutoCompact<TOutput>(runEnv);\n\n const stepCtx: RunContext<TDeps> = {\n ...runContextBase,\n stepNumber,\n messages,\n ...(currentStepSpan !== undefined ? { span: currentStepSpan } : {}),\n };\n const overrides = config.prepareStep ? await config.prepareStep(stepCtx) : {};\n\n // Resolve the step's registry / executor / catalogue / preferred\n // model / fallback chain (see `runtime/step-catalogue.ts`).\n const { stepRegistry, stepExecutor, stepTools, primary, fallbackChain } =\n resolveStepToolContext<TDeps, TOutput>(runEnv, overrides, lastStepCalledToolNames);\n\n // Resolve the effective reasoning-retention policy for this step\n // (RB-42), dropping buffered reasoning when the contract\n // downgrades to `'strip'` (see `runtime/messages.ts`).\n const reasoningPolicy = applyReasoningRetention(\n config.reasoningRetention,\n primary.resolvedProvider,\n messages,\n state.messages,\n );\n\n const toolDefs: ReadonlyArray<ToolDefinition> = stepTools.map((t) =>\n toolToDefinition(t as Tool<unknown, unknown, unknown>),\n );\n\n // Assemble the step's base provider request (AG-3 / D6 / RB-42;\n // see `runtime/step-catalogue.ts`).\n const baseRequest: ProviderRequest = buildBaseRequest<TDeps, TOutput>(\n runEnv,\n overrides,\n toolDefs,\n reasoningPolicy,\n stepNumber,\n currentStepSpan,\n );\n\n // Stream the step's provider call across the fallback chain\n // (see `runtime/fallback-chain.ts`); a terminal provider\n // failure is already recorded on `state` when `failed` is set.\n const chain = yield* runProviderFallbackChain<TOutput>(\n runEnv,\n fallbackChain,\n baseRequest,\n primary,\n stepNumber,\n );\n if (chain.failed) {\n return yield* finishRun(state, finalSnapshot);\n }\n const { modelSucceeded, textBuffer, finalCalls, stepReasoningParts } = chain;\n const { stepUsage, lastModelId } = chain;\n\n // AG-6: a mid-stream abort that interrupted the stream (no completed\n // model) ends the run as a cancellation - 'aborted', or 'failed'\n // under `onPendingApprovals: 'fail'` ONLY when approvals are\n // actually pending (W-038; an empty queue aborts plainly) - rather\n // than falling through to a 'no-provider-completed' failure. When\n // the model DID complete (e.g. `drain: true` let the step finish),\n // fall through so the step's tool calls run and the graceful stop\n // happens at the loop top.\n if (signal.aborted && !modelSucceeded) {\n yield* emitCancellation<TOutput>(runEnv);\n return yield* finishRun(state, finalSnapshot);\n }\n\n if (!modelSucceeded) {\n yield {\n type: 'agent.error',\n error: {\n message: 'all configured providers failed without finishing',\n code: 'no-provider-completed',\n },\n };\n state.status = 'failed';\n state.error = { message: 'no provider completed', code: 'no-provider-completed' };\n return yield* finishRun(state, finalSnapshot);\n }\n\n usageAcc.add(lastModelId, stepUsage);\n addModelUsage(state, lastModelId, stepUsage);\n accumulateUsage(state.usage, stepUsage);\n\n // Lateral-leak commit gate (RB-55 / AG-10 / C6): scan, commit\n // the assistant message, record the taint span, and emit the\n // detection event (see `runtime/run-gates.ts`).\n const leakBlocked = yield* commitAssistantMessage<TOutput>(\n runEnv,\n textBuffer,\n stepReasoningParts,\n finalCalls,\n reasoningPolicy,\n );\n\n const handoffCalls = finalCalls.filter((c) => handoffMap.has(c.toolName));\n if (handoffCalls.length > 1) {\n throw new MultipleHandoffsInStepError(handoffCalls.map((c) => c.toolName));\n }\n\n const stepRecord: RunStep = {\n stepNumber,\n startedAt: stepStart,\n endedAt: new Date().toISOString(),\n usage: stepUsage,\n toolCalls: [],\n agentId: state.currentAgentId,\n // C3: journal the RAW model response (pre-leak-block text) so\n // createReplayProvider(state) can re-drive the run offline.\n ...(config.recordProviderResponses === true\n ? {\n providerResponse: {\n modelId: lastModelId,\n ...(textBuffer.length > 0 ? { text: textBuffer } : {}),\n ...(finalCalls.length > 0 ? { toolCalls: [...finalCalls] } : {}),\n },\n }\n : {}),\n };\n state.steps.push(stepRecord);\n lastStepCalledToolNames = finalCalls.map((c) => c.toolName);\n\n if (textBuffer.length > 0 && !leakBlocked) {\n finalSnapshot.output = textBuffer as unknown as TOutput;\n yield { type: 'text.complete', text: textBuffer };\n }\n\n if (finalCalls.length > 0) {\n // `stepRegistry` / `stepExecutor` were resolved with the\n // catalogue above (so the advertised tools and the executor's\n // resolvable tools agree, including any `prepareStep` override).\n const execRunContext: RunContext<TDeps> = {\n ...runContextBase,\n stepNumber,\n messages,\n ...(currentStepSpan !== undefined ? { span: currentStepSpan } : {}),\n };\n\n // Walk the calls (batch dispatch, inline handoff, approval\n // pre-screen, once-per-step durable-HITL suspend; see\n // `runtime/tool-call-walk.ts`).\n const walked = yield* processStepToolCalls<TDeps, TOutput>(\n runEnv,\n finalCalls,\n stepRegistry,\n stepExecutor,\n execRunContext,\n stepNumber,\n );\n if (walked.suspended) {\n if (walked.abortPending === true) {\n // W-038: the abort raced the suspend - apply the\n // `onPendingApprovals` policy to the just-collected\n // approvals, then persist the FINAL policy-consistent\n // state as the last checkpoint (the walk skipped its\n // 'suspended' put so no stale awaiting_approval trail\n // outlives the aborted outcome).\n yield* emitCancellation<TOutput>(runEnv);\n if (config.checkpointStore !== undefined) {\n await config.checkpointStore.put(\n state.id,\n 'agent',\n {\n id: state.id,\n threadId: state.id,\n namespace: 'agent',\n state: serializeRunState(state, { stripTracingApiKey: true }),\n channelVersions: {},\n stepNumber,\n createdAt: new Date().toISOString(),\n },\n {\n source: 'sync',\n status: state.status === 'failed' ? 'failed' : 'aborted',\n nodeName: 'agent.abort',\n sessionId: state.sessionId,\n },\n );\n }\n }\n return yield* finishRun(state, finalSnapshot);\n }\n }\n\n currentStepSpan?.setAttributes({\n 'gen_ai.usage.input_tokens': stepUsage.promptTokens,\n 'gen_ai.usage.output_tokens': stepUsage.completionTokens,\n });\n currentStepSpan?.end();\n currentStepSpan = undefined;\n yield { type: 'step.end', stepNumber, usage: stepUsage };\n\n if (finalCalls.length === 0) {\n // C3: verifier gate on the terminal response (see\n // `runtime/run-gates.ts`); a failed round feeds back and the\n // loop takes another step while rounds remain.\n if (config.verifiers !== undefined && config.verifiers.length > 0) {\n const gate = yield* runVerifierGate<TDeps, TOutput>(\n runEnv,\n finalSnapshot,\n stepNumber,\n verifierRoundsUsed,\n );\n verifierRoundsUsed = gate.verifierRoundsUsed;\n if (gate.continueRun) continue;\n }\n state.status = 'completed';\n break;\n }\n }\n } catch (cause) {\n const message = cause instanceof Error ? cause.message : String(cause);\n const code = cause instanceof AgentRuntimeError ? (cause.code as string) : 'unknown';\n yield { type: 'agent.error', error: { message, code } };\n state.status = 'failed';\n state.error = { message, code };\n return yield* finishRun(state, finalSnapshot);\n } finally {\n // C7: close the trace tree on every exit path.\n currentStepSpan?.end();\n currentStepSpan = undefined;\n runSpan.setAttributes({\n 'gen_ai.usage.input_tokens': state.usage.promptTokens,\n 'gen_ai.usage.output_tokens': state.usage.completionTokens,\n 'graphorin.run.status': state.status,\n });\n runSpan.setStatus(state.status === 'failed' ? 'error' : 'ok');\n runSpan.end();\n // AG-5: drop the parent-signal listener so it does not accumulate across\n // runs that share one long-lived `options.signal`. Runs after this point\n // (the follow-up loop) keep working via `agent.abort()` on `localCtl`.\n if (parentSignal !== undefined) {\n parentSignal.removeEventListener('abort', onParentAbort);\n }\n }\n\n // Terminal output phases in their frozen order: stop-condition cut\n // (AG-24), structured-output parse (AG-3), output guardrails\n // (AG-2 / SDF-4) - see `runtime/run-gates.ts`.\n yield* finalizeRunOutput<TDeps, TOutput>(runEnv, finalSnapshot);\n activeRunState = undefined;\n return yield* finishRun(state, finalSnapshot);\n }\n\n // Terminal path: settle the manual-compact queue, finalize the\n // result, clear terminal-run spill artifacts and emit `agent.end`\n // (see `runtime/run-finish.ts`).\n const finishRunBase = createRunFinisher<TOutput>({\n pendingManualCompacts,\n spillWriter,\n checkpointStore: config.checkpointStore,\n checkpointPolicy: config.checkpointPolicy,\n });\n\n // The public call surface over the run loop (AG-9; see\n // `runtime/agent-surface.ts`).\n const { stream, run } = createRunMethods<TDeps, TOutput>(runLoop);\n\n const steer = (message: AgentInput): void => {\n const { seed } = asMessages(message);\n pendingSteer.push(...seed);\n if (activeRunState !== undefined) {\n externalEventQueue.push({\n type: 'agent.steered',\n runId: activeRunState.id,\n } as AgentEvent<TOutput>);\n }\n };\n\n const followUp = (message: AgentInput): void => {\n const { seed } = asMessages(message);\n pendingFollowUp.push(...seed);\n if (activeRunState !== undefined) {\n externalEventQueue.push({\n type: 'agent.followup.queued',\n runId: activeRunState.id,\n } as AgentEvent<TOutput>);\n }\n };\n\n const abort = (options?: AbortOptions): void => {\n pendingAbort = options ?? {};\n abortController?.abort();\n };\n\n // `agent.toTool()` - the sub-agent tool surface, incl. contextFold\n // and child-taint propagation (see `runtime/agent-to-tool.ts`).\n const toTool = createToTool<TDeps, TOutput>({ config, run, stream });\n\n // `compact` / `fanOut` / the progress IO surface (AG-13 / AG-7 /\n // AG-20; see `runtime/agent-surface.ts`). The deps object reads the\n // factory's mutable `activeRunState` scratch through a live getter.\n const surfaceDeps = {\n config,\n memory,\n agentId,\n getActiveRunState: () => activeRunState,\n externalEventQueue,\n pendingManualCompacts,\n progressIO,\n };\n const compact = createCompactMethod<TDeps, TOutput>(surfaceDeps);\n const fanOut = createFanOutMethod<TDeps, TOutput>(surfaceDeps);\n const progress: AgentProgressIO = createProgressSurface<TDeps, TOutput>(surfaceDeps);\n\n void config.sensitivity as Sensitivity | undefined;\n\n const agent: Agent<TDeps, TOutput> = {\n id: agentId,\n config,\n stream,\n run,\n steer,\n followUp,\n abort,\n toTool,\n compact,\n fanOut,\n progress,\n registry: toolRegistry,\n };\n\n return agent;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8HA,SAAgB,YACd,QACuB;AACvB,KAAI,OAAO,OAAO,SAAS,YAAY,OAAO,KAAK,WAAW,EAC5D,OAAM,IAAI,wBAAwB,iBAAiB;AAErD,KAAI,OAAO,aAAa,UAAa,OAAO,aAAa,KACvD,OAAM,IAAI,wBAAwB,qBAAqB;AAIzD,KAAI,OAAO,YAAY,SAAS,UAAU,OAAO,WAAW,WAAW,OACrE,OAAM,IAAI,wBACR,0EACD;AAEH,wBAAuB,OAAO,eAAe;AAC7C,KAAI,OAAO,iBAAiB,OAC1B,MAAK,MAAM,CAAC,MAAM,SAAS,OAAO,QAAQ,OAAO,aAAa,EAAE;AAC9D,MAAI,CAAC,gBAAgB,KAAK,CAAE,OAAM,IAAI,2BAA2B,EAAE,MAAM,CAAC;AAC1E,MAAI,SAAS,OAAW;AACxB,MAAI,CAAC,gBAAgB,KAAK,CAAE,OAAM,IAAI,2BAA2B,KAAK;;AAG1E,KAAI,OAAO,mBAAmB,QAC5B;OAAK,MAAM,QAAQ,OAAO,eACxB,KAAI,CAAC,gBAAgB,KAAK,CAAE,OAAM,IAAI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