@tangle-network/agent-runtime 0.103.1 → 0.104.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 (67) hide show
  1. package/README.md +4 -0
  2. package/dist/agent.d.ts +1 -1
  3. package/dist/agent.js +5 -5
  4. package/dist/analyst-loop.d.ts +1 -1
  5. package/dist/{chunk-TUSOOIDV.js → chunk-2QJSNOGK.js} +2 -2
  6. package/dist/{chunk-ZOYN3JR5.js → chunk-2UABGGUB.js} +3 -3
  7. package/dist/{chunk-6WZZXQV5.js → chunk-2X65HYMI.js} +1 -1
  8. package/dist/chunk-2X65HYMI.js.map +1 -0
  9. package/dist/{chunk-AAN2MB2X.js → chunk-3PPVCS75.js} +3 -3
  10. package/dist/chunk-3PPVCS75.js.map +1 -0
  11. package/dist/{chunk-EAQ5YRRY.js → chunk-5HNDYWUT.js} +39 -22
  12. package/dist/chunk-5HNDYWUT.js.map +1 -0
  13. package/dist/{chunk-3LJF5XSE.js → chunk-ATDFUJYI.js} +6 -6
  14. package/dist/chunk-ATDFUJYI.js.map +1 -0
  15. package/dist/{chunk-LFM4JBRW.js → chunk-FJI3EXU3.js} +46 -5
  16. package/dist/chunk-FJI3EXU3.js.map +1 -0
  17. package/dist/{chunk-5AITUUHO.js → chunk-HAO4CXHP.js} +2 -2
  18. package/dist/{chunk-M6MD6JBS.js → chunk-KRBFHMV6.js} +429 -1
  19. package/dist/chunk-KRBFHMV6.js.map +1 -0
  20. package/dist/{chunk-AYU35OTU.js → chunk-PZZKQVQV.js} +1 -1
  21. package/dist/chunk-PZZKQVQV.js.map +1 -0
  22. package/dist/{chunk-QYCKIV6C.js → chunk-STJEHM5F.js} +2 -2
  23. package/dist/{completion-gate-DLINnrkM.d.ts → completion-gate-Bv47cOnn.d.ts} +2 -2
  24. package/dist/{coordination-DTehA977.d.ts → coordination-CxtzMxSM.d.ts} +27 -27
  25. package/dist/environment-provider.d.ts +18 -7
  26. package/dist/environment-provider.js +3 -1
  27. package/dist/{improve-B40dRu0X.d.ts → improve--2A0gK1-.d.ts} +10 -4
  28. package/dist/index.d.ts +13 -13
  29. package/dist/index.js +10 -10
  30. package/dist/index.js.map +1 -1
  31. package/dist/intelligence.d.ts +46 -9
  32. package/dist/intelligence.js +117 -20
  33. package/dist/intelligence.js.map +1 -1
  34. package/dist/knowledge.d.ts +5 -5
  35. package/dist/knowledge.js +7 -7
  36. package/dist/{loop-runner-bin-DhIXsLAd.d.ts → loop-runner-bin-DUOGtvOl.d.ts} +3 -3
  37. package/dist/loop-runner-bin.d.ts +5 -5
  38. package/dist/loop-runner-bin.js +6 -6
  39. package/dist/loops.d.ts +66 -41
  40. package/dist/loops.js +9 -5
  41. package/dist/mcp/bin.js +4 -4
  42. package/dist/mcp/index.d.ts +6 -6
  43. package/dist/mcp/index.js +10 -10
  44. package/dist/mcp/index.js.map +1 -1
  45. package/dist/primeintellect/index.d.ts +1 -1
  46. package/dist/primeintellect/index.js.map +1 -1
  47. package/dist/profiles.d.ts +6 -6
  48. package/dist/profiles.js.map +1 -1
  49. package/dist/{structural-rollout-BFPIy3iw.d.ts → structural-rollout-C4mabf87.d.ts} +3 -3
  50. package/dist/{supervise-Cp8bNcJi.d.ts → supervise-7q9F5brx.d.ts} +3 -3
  51. package/dist/testing.js +8 -8
  52. package/dist/testing.js.map +1 -1
  53. package/dist/{types-DK11_O4L.d.ts → types-BCoemcXU.d.ts} +4 -4
  54. package/dist/{types-lZTE_LBd.d.ts → types-CvNwMzQt.d.ts} +4 -4
  55. package/dist/{worktree-fanout-DiiJWjJO.d.ts → worktree-fanout-WsOzt8qu.d.ts} +5 -5
  56. package/package.json +3 -3
  57. package/dist/chunk-3LJF5XSE.js.map +0 -1
  58. package/dist/chunk-6WZZXQV5.js.map +0 -1
  59. package/dist/chunk-AAN2MB2X.js.map +0 -1
  60. package/dist/chunk-AYU35OTU.js.map +0 -1
  61. package/dist/chunk-EAQ5YRRY.js.map +0 -1
  62. package/dist/chunk-LFM4JBRW.js.map +0 -1
  63. package/dist/chunk-M6MD6JBS.js.map +0 -1
  64. /package/dist/{chunk-TUSOOIDV.js.map → chunk-2QJSNOGK.js.map} +0 -0
  65. /package/dist/{chunk-ZOYN3JR5.js.map → chunk-2UABGGUB.js.map} +0 -0
  66. /package/dist/{chunk-5AITUUHO.js.map → chunk-HAO4CXHP.js.map} +0 -0
  67. /package/dist/{chunk-QYCKIV6C.js.map → chunk-STJEHM5F.js.map} +0 -0
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/durable/chat-engine.ts","../src/durable/execution-handle.ts","../src/improvement/build-prompts.ts","../src/improvement/campaign-otlp.ts","../src/improvement/driver-loop-generator.ts","../src/improvement/mcp-serve-verifier.ts","../src/improvement/profile-diff-proposer.ts","../src/improvement/reflective-generator.ts","../src/readiness.ts","../src/resolve-agent-backend.ts","../src/run.ts","../src/runtime-run.ts","../src/sse.ts","../src/tool-loop.ts"],"sourcesContent":["/**\n * `handleChatTurn` — framework-neutral chat-turn HTTP orchestrator.\n * Owns the NDJSON `ChatStreamEvent` line protocol, the `session.run.*`\n * lifecycle vocabulary, and the persist / post-process / trace-flush\n * hook order. Returns a `ReadableStream` body the product hands to its\n * platform `Response`.\n *\n * Execution durability is the substrate's concern: `box.streamPrompt`\n * auto-reconnects in-call; cross-process reconnect via `X-Execution-ID`\n * is the product's job. The producer this engine wraps already speaks\n * that protocol — the engine just frames the events.\n *\n * Hooks (`ChatTurnHooks`):\n * - `produce` — build the backend event stream\n * - `persistAssistantMessage` — write the assistant turn to the product DB\n * - `onTurnComplete?` — post-process (proposals, citations, …)\n * - `onEvent?` — per-event side channel (e.g. DO broadcast)\n * - `transformFinalText?` — pre-persist transform (e.g. PII redact)\n * - `traceFlush?` — handed to waitUntil so OTLP export lands\n *\n * Framework neutrality: takes already-resolved values (`identity` tuple,\n * a `waitUntil`), never a `Request` or a `Context`. The product's thin\n * route adapter does auth + parse + access-control, then calls\n * `handleChatTurn(...)` and returns `result.body` as its platform `Response`.\n */\n\n/** The NDJSON line protocol every product chat client already speaks. */\nexport interface ChatStreamEvent {\n type: string\n data?: Record<string, unknown>\n}\n\n/** Identity of a chat turn. `tenantId` is the workspace id for workspace-\n * scoped products and the user id for session-scoped products. */\nexport interface ChatTurnIdentity {\n tenantId: string\n /** Thread / session id. */\n sessionId: string\n userId: string\n /** Monotonic 0-based turn index within the session. */\n turnIndex: number\n}\n\n/** The live side of a turn — what the product's `produce` hook returns. */\nexport interface ChatTurnProducer<TEvent extends ChatStreamEvent = ChatStreamEvent> {\n /** The turn's event stream. Forwarded verbatim to the caller. */\n stream: AsyncGenerator<TEvent, void, unknown>\n /** The turn's final assistant text. Read once, after `stream` drains. */\n finalText(): string\n}\n\nexport interface ChatTurnHooks {\n /** Build the backend stream. The engine forwards events verbatim and\n * reads `finalText()` once the stream drains. */\n produce(): ChatTurnProducer\n /** Persist the assistant message to the product's own store. Called\n * once, after drain, with the assembled (transform-applied) text. */\n persistAssistantMessage(input: { identity: ChatTurnIdentity; finalText: string }): Promise<void>\n /** Optional post-processing (proposals, citations, credit metering …).\n * Errors are swallowed + logged — post-process must never fail a turn\n * that already streamed successfully. */\n onTurnComplete?(input: { identity: ChatTurnIdentity; finalText: string }): Promise<void>\n /** Optional per-event side channel (e.g. DO broadcast). Runs for every\n * emitted event, lifecycle envelope included. Errors swallowed — a\n * broadcast failure must not break the chat stream. */\n onEvent?(event: ChatStreamEvent): void | Promise<void>\n /** Optional pre-persist transform of the final text (e.g. PII\n * redaction). Affects only what is persisted; the live stream is\n * never altered. */\n transformFinalText?(text: string): string | Promise<string>\n /** Optional trace flush — resolves when OTLP export completes. Handed\n * to `waitUntil` so the worker isolate stays alive for the POST. */\n traceFlush?(): Promise<void>\n}\n\nexport interface RunChatTurnInput {\n identity: ChatTurnIdentity\n hooks: ChatTurnHooks\n /** Worker liveness hook. When omitted, trace flush is awaited inline\n * before the stream closes. */\n waitUntil?: (p: Promise<unknown>) => void\n /** Structured logger for swallowed hook errors. Defaults to\n * `console.error` so failures surface without product wiring. */\n log?: (message: string, meta?: Record<string, unknown>) => void\n}\n\nexport interface ChatTurnResult {\n /** NDJSON body — return this as the platform `Response` body. */\n body: ReadableStream<Uint8Array>\n /** Content type for the response. */\n contentType: 'application/x-ndjson'\n}\n\nconst encoder = new TextEncoder()\n\nfunction encodeLine(event: ChatStreamEvent): Uint8Array {\n return encoder.encode(`${JSON.stringify(event)}\\n`)\n}\n\nfunction defaultLog(message: string, meta?: Record<string, unknown>): void {\n if (meta) console.error(message, meta)\n else console.error(message)\n}\n\n/**\n * Run one chat turn. Returns immediately with a `ReadableStream` body;\n * the turn executes as the body is pulled. Never rejects — backend\n * failures surface as `error` + `session.run.failed` events.\n */\nexport function handleChatTurn(input: RunChatTurnInput): ChatTurnResult {\n const log = input.log ?? defaultLog\n const { identity, hooks } = input\n\n const body = new ReadableStream<Uint8Array>({\n start: async (controller) => {\n const emit = async (event: ChatStreamEvent): Promise<void> => {\n controller.enqueue(encodeLine(event))\n if (hooks.onEvent) {\n try {\n await hooks.onEvent(event)\n } catch (err) {\n log('[chat-engine] onEvent hook threw', {\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n }\n\n try {\n await emit({\n type: 'session.run.started',\n data: {\n sessionId: identity.sessionId,\n tenantId: identity.tenantId,\n turnIndex: identity.turnIndex,\n },\n })\n\n const producer = hooks.produce()\n for await (const event of producer.stream) {\n await emit(event)\n }\n const rawFinal = producer.finalText()\n const finalText = hooks.transformFinalText\n ? await hooks.transformFinalText(rawFinal)\n : rawFinal\n\n await hooks.persistAssistantMessage({ identity, finalText })\n if (hooks.onTurnComplete) {\n try {\n await hooks.onTurnComplete({ identity, finalText })\n } catch (err) {\n log('[chat-engine] onTurnComplete threw', {\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n\n await emit({\n type: 'session.run.completed',\n data: { sessionId: identity.sessionId },\n })\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err)\n log('[chat-engine] turn failed', { error: message })\n await emit({ type: 'error', data: { message } })\n await emit({\n type: 'session.run.failed',\n data: { sessionId: identity.sessionId, message },\n })\n } finally {\n if (hooks.traceFlush) {\n const flush = hooks.traceFlush().catch((err) =>\n log('[chat-engine] traceFlush threw', {\n error: err instanceof Error ? err.message : String(err),\n }),\n )\n if (input.waitUntil) input.waitUntil(flush)\n else await flush\n }\n controller.close()\n }\n },\n })\n\n return { body, contentType: 'application/x-ndjson' }\n}\n","/**\n * Derive a stable executionId from the run identity. The same\n * `(projectId, sessionId, turnIndex)` tuple yields the same id — so a\n * client retry of the same turn lands on the same substrate execution\n * and the orchestrator's buffer replays instead of starting a second\n * prompt.\n *\n * Format is readable, not hashed: operators grepping orchestrator logs\n * for `gtm-agent:thread-abc:3` find the run without translating an\n * opaque id. Substrate executionIds are not a secrecy boundary.\n *\n * Wire integration:\n * - Sandbox PromptOptions accepts `executionId` and `lastEventId`.\n * Products pass this id to make cross-process reconnect land on the\n * same substrate execution instead of spawning a duplicate run.\n */\nexport function deriveExecutionId(input: {\n projectId: string\n sessionId: string\n turnIndex: number\n}): string {\n return `${input.projectId}:${input.sessionId}:${input.turnIndex}`\n}\n","/**\n * Build-prompt starting points for the two buildable artifact types. There is\n * NO `toolGenerator`/`mcpGenerator` wrapper — the factory is `agenticGenerator`\n * + a verifier (docs/artifact-lifecycle-frontier.md), so a tool or an MCP\n * server is built by composing the pieces directly:\n *\n * // a tool:\n * agenticGenerator({ buildPrompt: toolBuildPrompt, verify: commandVerifier('pnpm', ['test']) })\n * // an MCP server:\n * agenticGenerator({ buildPrompt: mcpBuildPrompt, verify: mcpServeVerifier({ command: 'node', args: ['server.mjs'] }) })\n *\n * These are the only type-specific bit (the phrasing that points the agent at a\n * tool vs. an MCP); the worktree, resume-on-failure loop, and improvement-loop\n * wrapper are shared. MCP is the load-bearing target — it is how a harness\n * acquires tools; raw tools matter where we control the loader.\n */\n\nimport type { AnalystFinding } from '@tangle-network/agent-eval'\nimport { optimizerMethod } from './optimizer-prompt'\n\ntype FindingsArg = { report: unknown; findings: AnalystFinding[] }\n\n/** Render findings as the ranked-evidence block every build prompt ends with. */\nexport function findingLines(findings: AnalystFinding[]): string[] {\n return findings.map((f) => {\n const where = f.subject ? ` [${f.subject}]` : ''\n const action = f.recommended_action ? ` → ${f.recommended_action}` : ''\n return `- (${f.severity})${where} ${f.claim}${action}`\n })\n}\n\n/** Build the starting instruction for a coder agent tasked with implementing a new tool. */\nexport function toolBuildPrompt(args: FindingsArg): string {\n return [\n 'You are building a new TOOL for this codebase — a capability the agent measurably lacks,',\n 'evidenced by the failure findings at the bottom. The tool is an experiment: after it is',\n 'built and verified, its marginal lift is measured on held-out tasks, and only a real lift',\n 'promotes it.',\n '',\n optimizerMethod,\n '',\n 'THE SURFACE — what a deliverable tool looks like here:',\n '- ONE small, self-contained module PLUS tests that exercise its contract (what callers rely',\n ' on), not its internals. The tests are the experiment for sub-goal correctness — write the',\n ' test that would fail if your hypothesis about the gap were wrong.',\n '- It must compile and its tests must pass — they run automatically; on failure you get the',\n ' verifier output and another attempt, resuming on top of your own edits (fix in place, do',\n ' not start over).',\n '- Match the codebase grain: reuse its existing helpers, style, and test framework; a tool',\n ' that fights the codebase is the wrong tool even if it passes.',\n '- Do not commit; leave the changes in the working tree.',\n '',\n 'FINDINGS — ranked evidence from real failed runs (the gaps the tool must close):',\n ...findingLines(args.findings),\n ].join('\\n')\n}\n\n/** Build the starting instruction for a coder agent tasked with implementing a new MCP server. */\nexport function mcpBuildPrompt(args: FindingsArg): string {\n return [\n 'You are building a new MCP SERVER (Model Context Protocol) exposing tool(s) that close the',\n 'capability gaps evidenced by the failure findings at the bottom, so any harness can mount',\n 'them. The server is an experiment: after it is built and boot-verified, its marginal lift is',\n 'measured on held-out tasks, and only a real lift promotes it.',\n '',\n optimizerMethod,\n '',\n 'RESEARCH FIRST — ADOPT BEFORE BUILD: you may discover and ADOPT an existing external MCP',\n 'server if it fits the gaps better than building one. Registries and vendor docs list',\n 'maintained servers for most common capabilities (web search, fetch, GitHub, filesystems,',\n 'databases). To adopt, deliver a short adoption note instead of an implementation: the',\n \"server's launch command or HTTP endpoint, and the API key it needs BY NAME (e.g.\",\n 'EXA_API_KEY) — never a key value; provisioning injects the value at materialize time. If',\n 'your environment has no web access, decide from what you already know and say so.',\n '',\n 'THE SURFACE — what a deliverable MCP server looks like here (checked by BOOTING it):',\n '- it starts over stdio and answers the MCP `initialize` handshake,',\n '- `tools/list` returns at least one tool with a valid input schema,',\n '- newline-delimited JSON-RPC 2.0, protocol version 2024-11-05,',\n '- a clear start command (a package.json `start` script or an obvious entrypoint).',\n 'Design the tool surface for the FINDINGS, not for generality: each exposed tool should map to',\n 'a named failure mechanism, with a description that tells the agent when to reach for it (a',\n 'tool the agent never calls measures zero). If the boot-and-probe fails you get the error and',\n 'another attempt, resuming on top of your own edits. Do not commit; leave the changes in the',\n 'working tree.',\n '',\n 'FINDINGS — ranked evidence from real failed runs (the capabilities the server must provide):',\n ...findingLines(args.findings),\n ].join('\\n')\n}\n","/**\n * Campaign `spans.jsonl` → OTLP-flat JSONL — the missing converter between\n * what the substrate RECORDS and what its trace analysts READ.\n *\n * agent-eval's `runCampaign` durably records one `spans.jsonl` per cell\n * (`defaultBuildTraceWriter`): flat records\n * `{ name, cellId, startMs, durationMs?, ...attributes }` with no trace/span\n * ids. Its trace consumers (`OtlpFileTraceStore`, the trace-analyst registry,\n * `haloProposer`/`traceAnalystProposer` via `resolveTraces`) read OTLP-flat\n * JSONL (`trace_id`/`span_id`/ISO times/status/attributes — the shape\n * `projectOtlpFlatLine` parses). Nothing shipped converts between the two, so\n * the traces real optimization runs write could never reach the trace-native\n * proposers. This module is that wire adapter:\n *\n * - {@link campaignCellSpansToOtlp} — one cell's `spans.jsonl` content →\n * OTLP lines (a per-cell root AGENT anchor span + one child per record).\n * - {@link convertCampaignDirToOtlp} — walk any campaign run/generation dir\n * for `spans.jsonl` files and concatenate their OTLP lines.\n * - {@link campaignTraceResolver} — the `resolveTraces` implementation for\n * `traceAnalystProposer`/`haloProposer`: proposing generation g reads the\n * traces the loop just recorded (`gen-<g-1>`, or `baseline` for g = 0)\n * under the same `runDir` handed to `improve()`/`selfImprove()`.\n *\n * Trace identity: one trace per CELL, keyed on the cell's on-disk path — the\n * same sanitized `cellId` recurs across the baseline and every candidate\n * campaign, so folding the id alone would merge distinct runs into one trace.\n * Ids are deterministic FNV-1a folds to OTLP's 32/16-hex width, so re-converts\n * are stable and byte-identical.\n */\n\nimport { type Dirent, readdirSync, readFileSync } from 'node:fs'\nimport { basename, dirname, join, resolve } from 'node:path'\nimport type { ProposeContext } from '@tangle-network/agent-eval/campaign'\nimport { OPENINFERENCE_SPAN_KIND } from '@tangle-network/agent-eval/traces'\n\n/** How deep {@link convertCampaignDirToOtlp} recurses looking for\n * `spans.jsonl`. The deepest shipped layout is\n * `<runDir>/gen-<g>/candidate-<i>/<cell>/spans.jsonl` (3 levels); one spare\n * level tolerates a wrapper dir without letting a mis-pointed root scan a\n * whole filesystem. */\nconst MAX_WALK_DEPTH = 4\n\nexport interface CampaignOtlpOptions {\n /** OTLP `service.name` on every emitted span. Default `'campaign'`. */\n serviceName?: string\n}\n\ninterface CampaignSpanRecord {\n name: string\n cellId: string\n startMs: number\n durationMs?: number\n attributes: Record<string, unknown>\n error?: string\n}\n\n/**\n * Convert ONE cell's `spans.jsonl` content to OTLP-flat JSONL lines.\n * `cellKey` is the identity the trace id folds from — pass the cell's on-disk\n * path (unique per campaign); `cellId` is the display/attribute label.\n * Returns `[]` for empty/recordless content (a dispatch that never touched\n * `ctx.trace`/`ctx.cost` writes an empty file — that is data, not an error).\n */\nexport function campaignCellSpansToOtlp(\n content: string,\n cell: { cellId: string; cellKey?: string },\n opts: CampaignOtlpOptions = {},\n): string[] {\n const records = parseCampaignSpans(content, cell.cellId)\n if (records.length === 0) return []\n\n const serviceName = opts.serviceName ?? 'campaign'\n const key = cell.cellKey ?? cell.cellId\n const traceId = foldTo32Hex(key)\n const rootSpanId = foldTo16Hex(`${key}::root`)\n\n const startMs = Math.min(...records.map((r) => r.startMs))\n const endMs = Math.max(...records.map((r) => r.startMs + (r.durationMs ?? 0)))\n const anyError = records.some((r) => r.error !== undefined)\n\n const resource = {\n attributes: {\n 'service.name': serviceName,\n 'campaign.cell_id': cell.cellId,\n ...(cell.cellKey ? { 'campaign.cell_dir': cell.cellKey } : {}),\n },\n }\n\n const lines: string[] = [\n JSON.stringify({\n trace_id: traceId,\n span_id: rootSpanId,\n parent_span_id: '',\n name: `cell.${cell.cellId}`,\n start_time: msToIso(startMs),\n end_time: msToIso(endMs),\n status: anyError\n ? { code: 'STATUS_CODE_ERROR', message: 'one or more spans errored' }\n : { code: 'STATUS_CODE_OK', message: '' },\n resource,\n attributes: {\n [OPENINFERENCE_SPAN_KIND]: 'AGENT',\n 'agent.name': cell.cellId,\n },\n }),\n ]\n\n for (let i = 0; i < records.length; i++) {\n const r = records[i]!\n lines.push(\n JSON.stringify({\n trace_id: traceId,\n span_id: foldTo16Hex(`${key}::${i}`),\n parent_span_id: rootSpanId,\n name: r.name,\n start_time: msToIso(r.startMs),\n end_time: msToIso(r.startMs + (r.durationMs ?? 0)),\n status:\n r.error === undefined\n ? { code: 'STATUS_CODE_OK', message: '' }\n : { code: 'STATUS_CODE_ERROR', message: r.error },\n resource,\n attributes: r.attributes,\n }),\n )\n }\n return lines\n}\n\n/**\n * Walk `dir` (a campaign run dir, a generation dir, or a whole `selfImprove`\n * run root) for `spans.jsonl` files and return their concatenated OTLP-flat\n * JSONL — the exact string the `resolveTraces` contract expects. `''` when no\n * spans exist (the proposers fail loud on empty by design).\n */\nexport function convertCampaignDirToOtlp(dir: string, opts: CampaignOtlpOptions = {}): string {\n const root = resolve(dir)\n const files = findSpansFiles(root, MAX_WALK_DEPTH)\n const lines: string[] = []\n for (const file of files) {\n let content: string\n try {\n content = readFileSync(file, 'utf8')\n } catch {\n continue\n }\n const cellDir = dirname(file)\n lines.push(\n ...campaignCellSpansToOtlp(content, { cellId: basename(cellDir), cellKey: cellDir }, opts),\n )\n }\n return lines.length > 0 ? `${lines.join('\\n')}\\n` : ''\n}\n\nexport interface CampaignTraceResolverOptions extends CampaignOtlpOptions {\n /** The `selfImprove`/`improve()` run root — the SAME `runDir` the loop\n * records under (`<runDir>/baseline/...`, `<runDir>/gen-<g>/candidate-<i>/...`).\n * Must be a real path; a `mem://` run records nothing to resolve. */\n runDir: string\n}\n\n/**\n * Build the `resolveTraces` function `traceAnalystProposer`/`haloProposer`\n * take: proposing generation g reads the traces of the campaigns the loop just\n * scored — `gen-<g-1>` (or `baseline` when g = 0), falling back to every trace\n * under the run root when that directory has none (e.g. a caller pointing at a\n * single campaign dir rather than a loop root).\n *\n * traceAnalystProposer({ ..., resolveTraces: campaignTraceResolver({ runDir }) })\n */\nexport function campaignTraceResolver(\n opts: CampaignTraceResolverOptions,\n): (ctx: Pick<ProposeContext, 'generation'>) => string {\n return (ctx) => {\n const priorDir =\n ctx.generation <= 0\n ? join(opts.runDir, 'baseline')\n : join(opts.runDir, `gen-${ctx.generation - 1}`)\n const prior = convertCampaignDirToOtlp(priorDir, opts)\n if (prior) return prior\n return convertCampaignDirToOtlp(opts.runDir, opts)\n }\n}\n\n/** Parse `spans.jsonl` content into records, splitting the trace writer's\n * reserved fields from the free-form attributes. Malformed lines are skipped\n * (one torn write must not censor the rest of the cell). */\nfunction parseCampaignSpans(content: string, cellId: string): CampaignSpanRecord[] {\n const out: CampaignSpanRecord[] = []\n for (const line of content.split('\\n')) {\n const trimmed = line.trim()\n if (!trimmed) continue\n let raw: unknown\n try {\n raw = JSON.parse(trimmed)\n } catch {\n continue\n }\n if (!raw || typeof raw !== 'object' || Array.isArray(raw)) continue\n const o = raw as Record<string, unknown>\n if (typeof o.name !== 'string' || typeof o.startMs !== 'number') continue\n\n const attributes: Record<string, unknown> = { 'campaign.cell_id': cellId }\n for (const [k, v] of Object.entries(o)) {\n if (k === 'name' || k === 'cellId' || k === 'startMs' || k === 'durationMs') continue\n attributes[k] = v\n }\n out.push({\n name: o.name,\n cellId,\n startMs: o.startMs,\n ...(typeof o.durationMs === 'number' ? { durationMs: o.durationMs } : {}),\n attributes,\n ...(typeof o.error === 'string' ? { error: o.error } : {}),\n })\n }\n return out\n}\n\n/** All `spans.jsonl` files under `dir`, depth-bounded, symlink-dirs skipped\n * (a cycle under a run dir must not hang the proposal round). Sorted for a\n * deterministic concatenation order. */\nfunction findSpansFiles(dir: string, depth: number): string[] {\n const out: string[] = []\n let entries: Dirent[]\n try {\n entries = readdirSync(dir, { withFileTypes: true })\n } catch {\n return out\n }\n for (const entry of entries) {\n const full = join(dir, entry.name)\n if (entry.isFile() && entry.name === 'spans.jsonl') {\n out.push(full)\n } else if (depth > 0 && entry.isDirectory() && !entry.isSymbolicLink()) {\n out.push(...findSpansFiles(full, depth - 1))\n }\n }\n return out.sort()\n}\n\nfunction msToIso(ms: number): string {\n return Number.isFinite(ms) && ms > 0 ? new Date(ms).toISOString() : new Date(0).toISOString()\n}\n\n/** Deterministic FNV-1a fold to OTLP's 16-hex span-id width. */\nfunction foldTo16Hex(s: string): string {\n const a = fnv1a(s)\n const b = fnv1a(`${s}::salt`)\n return a + b\n}\n\n/** Deterministic FNV-1a fold to OTLP's 32-hex trace-id width. */\nfunction foldTo32Hex(s: string): string {\n return foldTo16Hex(s) + foldTo16Hex(`${s}::trace`)\n}\n\nfunction fnv1a(s: string): string {\n let h = 0x811c9dc5\n for (let i = 0; i < s.length; i++) {\n h ^= s.charCodeAt(i)\n h = Math.imul(h, 0x01000193) >>> 0\n }\n return h.toString(16).padStart(8, '0')\n}\n","/**\n * `driverLoopGenerator` — the driver→worker `CandidateGenerator`: the build\n * loop run by the ATOM instead of the canned respawn.\n *\n * `agenticGenerator` steers with three hardcoded conditions picking a canned\n * note (`EMPTY_TREE_NOTE` / `failureNote`) and respawns. This generator swaps\n * that respawn brain for a real driver: an LLM on the canonical tool-loop seam\n * (`runBrainLoop` + `ToolLoopChat` — the exact loop `driverAgent` runs its\n * brain on) that AUTHORS each worker instruction, OBSERVES what the session\n * actually produced (diff, files, verifier output), RATES it, and DECIDES\n * refine / re-scope / decompose — prompted with the senior scientific-method\n * doctrine (`buildDriverSystem`).\n *\n * The worker stays the proven primitive: `runLocalHarness` in the candidate\n * worktree, same as `agenticGenerator` — only the brain between sessions\n * changes. The worktree machinery (`worktreeBuildCandidate`) and verifiers\n * (`commandVerifier` / `mcpServeVerifier`) are reused verbatim.\n *\n * Completion-oracle invariant (the supervisor doctrine, kept): the driver's\n * prose NEVER decides the outcome. After the loop, code re-checks ground\n * truth — tree dirty, raw-trace evidence present, verifier green — and only\n * that decides `applied`. A driver that claims success over a failing verifier\n * produces a discarded candidate, not a shipped one.\n *\n * @experimental\n */\n\nimport { spawnSync } from 'node:child_process'\nimport { readFileSync, statSync } from 'node:fs'\nimport { resolve, sep } from 'node:path'\nimport type { AnalystFinding } from '@tangle-network/agent-eval'\nimport { type LocalHarness, runLocalHarness } from '../mcp/local-harness'\nimport { runBrainLoop, type ToolLoopChat } from '../runtime/tool-loop'\nimport {\n defaultBuildPrompt,\n rawTraceEvidenceProblem,\n requiresRawTraceEvidence,\n summarizeFindings,\n type Verifier,\n worktreeChangedPaths,\n} from './agentic-generator'\nimport type { CandidateGenerator } from './improvement-driver'\nimport { buildDriverSystem, researchDriverNote } from './optimizer-prompt'\n\nexport interface DriverLoopGeneratorOptions {\n /** The driver-LLM seam — ONE inference turn over the conversation + tool specs (the canonical\n * `ToolLoopChat`, same seam as `driverAgent`): `routerBrain(cfg)` in production, a scripted\n * mock in tests. */\n brain: ToolLoopChat\n /** Local coding harness the driver's worker sessions run in the worktree. Default `claude`. */\n harness?: LocalHarness\n /** Per-worker-session wall-clock timeout (ms). Default = `runLocalHarness` default (5m). */\n timeoutMs?: number\n /** Build the driver's task briefing (domain framing + method + findings) — the same senior\n * prompt the worker path uses (`toolBuildPrompt` / `mcpBuildPrompt`). The driver reads it and\n * folds what each worker needs into its instruction. Default `defaultBuildPrompt`. */\n buildPrompt?: (args: { report: unknown; findings: AnalystFinding[] }) => string\n /** Verify the worktree (the intrinsic check). Exposed to the driver as `run_verifier` AND\n * re-run by code as the final keep/discard gate. Omitted ⇒ the final gate is dirty-tree only\n * (legacy `agenticGenerator` behavior sans verifier). */\n verify?: Verifier\n /** Max driver inference turns. Default `max(8, 2 + maxShots * 3)` — room for one\n * observe/rate/decide cycle per worker session plus orientation. */\n maxTurns?: number\n /** The research seam (adopt-not-build): when set, the driver gets a\n * `research{query}` tool + the `researchDriverNote` doctrine, so it can\n * discover an EXISTING external MCP instead of building one. Wire a real\n * web/search backend here — none is provisioned by default (the build\n * harness has no live web access yet; flagged). */\n research?: (query: string) => Promise<string>\n /** Test seam — inject the harness runner (defaults to `runLocalHarness`). */\n runHarness?: typeof runLocalHarness\n /** Test seam — inject the worktree diff reader (defaults to `git diff` in the worktree). */\n readDiff?: (worktreePath: string) => string\n /** Test seam — inject the changed-paths reader (defaults to `git status --porcelain`). */\n changedPaths?: (worktreePath: string) => string[]\n}\n\nconst workerOutputTailChars = 2_000\nconst diffMaxChars = 6_000\nconst readFileDefaultBytes = 8_192\nconst researchResultMaxChars = 8_000\n\n/** Driver→worker `CandidateGenerator`: an LLM driver on the canonical tool-loop authors, observes, rates, and steers coding-harness sessions in the worktree until the verifier passes or the session budget is spent. */\nexport function driverLoopGenerator(opts: DriverLoopGeneratorOptions): CandidateGenerator {\n const harness = opts.harness ?? 'claude'\n const buildPrompt = opts.buildPrompt ?? defaultBuildPrompt\n const run = opts.runHarness ?? runLocalHarness\n const changed = opts.changedPaths ?? worktreeChangedPaths\n const readDiff = opts.readDiff ?? worktreeDiff\n const verify = opts.verify\n\n return {\n kind: `driver-loop:${harness}`,\n async generate({ worktreePath, report, findings, maxShots, signal }) {\n signal.throwIfAborted()\n const briefing = buildPrompt({ report, findings })\n const needsRawTraceEvidence = requiresRawTraceEvidence(findings)\n const sessionCap = Math.max(1, maxShots)\n let sessionsUsed = 0\n\n // Ground-truth verification shared by the driver's `run_verifier` tool and the final\n // code-owned gate — ONE definition of \"delivered\" so the driver can never see a different\n // check than the one that decides the keep.\n const groundVerify = async (): Promise<{ ok: boolean; feedback?: string }> => {\n signal.throwIfAborted()\n if (changed(worktreePath).length === 0) {\n return { ok: false, feedback: 'the working tree has no changes — nothing to verify' }\n }\n if (needsRawTraceEvidence) {\n const problem = rawTraceEvidenceProblem(worktreePath, findings)\n if (problem) return { ok: false, feedback: problem }\n }\n if (!verify) {\n return { ok: true, feedback: 'no verifier configured: a dirty tree is the candidate' }\n }\n const result = await verify(worktreePath, signal)\n signal.throwIfAborted()\n return result\n }\n\n const execute = async (name: string, args: Record<string, unknown>): Promise<string> => {\n signal.throwIfAborted()\n switch (name) {\n case 'run_worker': {\n const instruction = typeof args.instruction === 'string' ? args.instruction.trim() : ''\n if (instruction.length === 0) {\n return 'error: run_worker requires a non-empty `instruction`'\n }\n if (sessionsUsed >= sessionCap) {\n return `error: worker-session budget exhausted (${sessionsUsed}/${sessionCap} used). Inspect and verify what exists, then stop with your final assessment.`\n }\n sessionsUsed += 1\n const result = await run({\n harness,\n cwd: worktreePath,\n taskPrompt: instruction,\n ...(opts.timeoutMs !== undefined ? { timeoutMs: opts.timeoutMs } : {}),\n signal,\n })\n signal.throwIfAborted()\n if (result.aborted) {\n throw new Error('driverLoopGenerator: worker session was cancelled by the caller')\n }\n return JSON.stringify({\n session: `${sessionsUsed}/${sessionCap}`,\n exitCode: result.exitCode,\n timedOut: result.timedOut,\n aborted: result.aborted ?? false,\n killedBySignal: result.killedBySignal,\n durationMs: result.durationMs,\n changedPaths: changed(worktreePath),\n stdoutTail: tail(result.stdout, workerOutputTailChars),\n stderrTail: tail(result.stderr, workerOutputTailChars),\n })\n }\n case 'inspect_worktree': {\n const paths = changed(worktreePath)\n const diff = truncate(readDiff(worktreePath), diffMaxChars)\n return JSON.stringify({\n changedPaths: paths,\n diff:\n diff.length > 0\n ? diff\n : '(no tracked-file diff — new files are untracked; read_file them)',\n })\n }\n case 'read_file':\n return readWorktreeFile(worktreePath, args)\n case 'research': {\n if (!opts.research) return 'error: research tool is not provisioned in this run'\n const query = typeof args.query === 'string' ? args.query.trim() : ''\n if (query.length === 0) return 'error: research requires a non-empty `query`'\n const result = await opts.research(query)\n signal.throwIfAborted()\n return truncate(result, researchResultMaxChars)\n }\n case 'run_verifier': {\n const result = await groundVerify()\n return JSON.stringify({\n ok: result.ok,\n feedback: truncate(result.feedback ?? '', 4_000),\n })\n }\n default:\n return `error: unknown tool: ${name}`\n }\n }\n\n await runBrainLoop({\n chat: opts.brain,\n tools: opts.research ? [...driverToolSpecs, researchToolSpec] : driverToolSpecs,\n execute,\n initialMessages: [\n {\n role: 'system',\n content: opts.research\n ? `${buildDriverSystem}\\n\\n${researchDriverNote}`\n : buildDriverSystem,\n },\n {\n role: 'user',\n content: [\n `THE BUILD BRIEF (the contract your workers must satisfy — fold what each needs into its instruction; workers never see this brief):`,\n '',\n briefing,\n '',\n `Worker-session budget: ${sessionCap}. The worktree is a fresh checkout at ${worktreePath}.`,\n ].join('\\n'),\n },\n ],\n maxTurns: opts.maxTurns ?? Math.max(8, 2 + sessionCap * 3),\n hooks: { stopBefore: () => signal.aborted },\n })\n signal.throwIfAborted()\n\n // The completion oracle: the driver stopped (or ran out of turns) — ground truth decides.\n const verdict = await groundVerify()\n signal.throwIfAborted()\n if (!verdict.ok) return { applied: false, summary: '' }\n return { applied: true, summary: summarizeFindings(findings) }\n },\n }\n}\n\nconst driverToolSpecs = [\n {\n type: 'function' as const,\n function: {\n name: 'run_worker',\n description:\n 'Run ONE coding-harness session in the worktree with your instruction as its entire goal. The worktree persists between sessions. Sessions are capped — author each instruction richly (outcome, context, placement, the check it is held to).',\n parameters: {\n type: 'object',\n properties: {\n instruction: {\n type: 'string',\n description: 'The complete, self-contained goal for this worker session.',\n },\n },\n required: ['instruction'],\n },\n },\n },\n {\n type: 'function' as const,\n function: {\n name: 'inspect_worktree',\n description:\n 'Current git state of the worktree: changed paths + the tracked-file diff (truncated). New untracked files show in changedPaths only — read_file them.',\n parameters: { type: 'object', properties: {} },\n },\n },\n {\n type: 'function' as const,\n function: {\n name: 'read_file',\n description: 'Read one file from the worktree (paths are worktree-relative).',\n parameters: {\n type: 'object',\n properties: {\n path: { type: 'string', description: 'Worktree-relative file path.' },\n maxBytes: { type: 'number', description: 'Byte cap (default 8192).' },\n },\n required: ['path'],\n },\n },\n },\n {\n type: 'function' as const,\n function: {\n name: 'run_verifier',\n description:\n 'Run the intrinsic check of the surface (compile+tests / boot-and-probe). Its result — not your judgment — decides whether the candidate is kept.',\n parameters: { type: 'object', properties: {} },\n },\n },\n]\n\n/** Only offered when `opts.research` is wired — a tool the driver cannot call\n * must never appear in its tool list. */\nconst researchToolSpec = {\n type: 'function' as const,\n function: {\n name: 'research',\n description:\n 'Search external sources (MCP registries, vendor docs) for an EXISTING server that closes the capability gap — the adopt-not-build check. Returns text findings.',\n parameters: {\n type: 'object',\n properties: {\n query: { type: 'string', description: 'What capability / server to search for.' },\n },\n required: ['query'],\n },\n },\n}\n\n/** `git diff` over the worktree (tracked files). Fails loud like `worktreeChangedPaths` — a git\n * fault on a fresh checkout is a broken setup, not an empty diff. */\nfunction worktreeDiff(worktreePath: string): string {\n const result = spawnSync('git', ['diff'], { cwd: worktreePath, encoding: 'utf-8' })\n if (result.error) {\n throw new Error(\n `driverLoopGenerator: git diff failed to spawn in ${worktreePath}: ${result.error.message}`,\n )\n }\n if (result.status !== 0) {\n throw new Error(\n `driverLoopGenerator: git diff exited ${result.status} in ${worktreePath}: ${result.stderr.trim()}`,\n )\n }\n return result.stdout\n}\n\n/** Bounded, worktree-jailed file read for the driver's `read_file`. A path escaping the worktree\n * is refused (the driver only rates work in the candidate tree; it has no business elsewhere). */\nfunction readWorktreeFile(worktreePath: string, args: Record<string, unknown>): string {\n const rel = typeof args.path === 'string' ? args.path : ''\n if (rel.length === 0) return 'error: read_file requires `path`'\n const root = resolve(worktreePath)\n const target = resolve(root, rel)\n if (target !== root && !target.startsWith(root + sep)) {\n return `error: path escapes the worktree: ${rel}`\n }\n const maxBytes =\n typeof args.maxBytes === 'number' && args.maxBytes > 0\n ? Math.min(args.maxBytes, 65_536)\n : readFileDefaultBytes\n try {\n const size = statSync(target).size\n const body = readFileSync(target, 'utf-8').slice(0, maxBytes)\n return size > maxBytes ? `${body}\\n… (${size - maxBytes} bytes truncated)` : body\n } catch (e) {\n return `error: ${e instanceof Error ? e.message : String(e)}`\n }\n}\n\nfunction tail(s: string, n: number): string {\n const trimmed = s.trim()\n return trimmed.length <= n ? trimmed : `…${trimmed.slice(-n)}`\n}\n\nfunction truncate(s: string, n: number): string {\n return s.length <= n ? s : `${s.slice(0, n - 1)}…`\n}\n","/**\n * `mcpServeVerifier` — the intrinsic verifier for a built MCP server: the\n * boot-and-probe checker named in docs/artifact-lifecycle-frontier.md. A\n * generated MCP server is only a candidate if it actually *serves* — so this\n * boots it over stdio (the default local MCP transport) and runs the real\n * handshake: `initialize` → `notifications/initialized` → `tools/list`, and\n * asserts the server answers with at least `minTools` tools.\n *\n * The spawn + handshake is the SHARED same-host stdio connection\n * (`connectStdioMcp`) — the same code path that later serves the built server\n * LIVE to a scored run (`materializeLocalMcp`), so \"verified it serves\" and\n * \"served while scored\" can never drift apart.\n *\n * Outcomes follow the `Verifier` contract: a server that fails to start, exits\n * early, errors the handshake, times out, or exposes no tools is a FAILED\n * candidate (`{ok:false}`, fed back into the next generation shot); a missing\n * start binary or spawn fault THROWS (a setup bug, never a silent fallback).\n */\n\nimport {\n connectStdioMcp,\n McpSpawnFault,\n type StdioMcpConnection,\n} from '../runtime/stdio-mcp-client'\nimport type { Verifier, VerifyResult } from './agentic-generator'\n\nexport interface McpServeSpec {\n /** Command that starts the built MCP server in the worktree (stdio transport). */\n command: string\n args?: string[]\n /** Extra env for the server process (merged over `process.env`). */\n env?: Record<string, string>\n /** Handshake timeout (ms). Default 30s. */\n timeoutMs?: number\n /** Minimum tools the server must expose to pass. Default 1. */\n minTools?: number\n}\n\n/** Build a `Verifier` that boots a generated MCP server over stdio and checks it exposes tools. */\nexport function mcpServeVerifier(spec: McpServeSpec): Verifier {\n const minTools = spec.minTools ?? 1\n\n return async (worktreePath: string): Promise<VerifyResult> => {\n let conn: StdioMcpConnection\n try {\n conn = await connectStdioMcp({\n command: spec.command,\n ...(spec.args ? { args: spec.args } : {}),\n cwd: worktreePath,\n ...(spec.env ? { env: spec.env } : {}),\n ...(spec.timeoutMs !== undefined ? { timeoutMs: spec.timeoutMs } : {}),\n })\n } catch (err) {\n if (err instanceof McpSpawnFault) {\n throw new Error(`mcpServeVerifier: ${err.message}`)\n }\n // Crash-on-boot / handshake error / timeout: a failed candidate, with the\n // connection's stderr-tailed message as the feedback for the next shot.\n return { ok: false, feedback: err instanceof Error ? err.message : String(err) }\n }\n try {\n if (conn.tools.length < minTools) {\n return {\n ok: false,\n feedback: `tools/list returned ${conn.tools.length} tool(s), need >= ${minTools}`,\n }\n }\n return { ok: true }\n } finally {\n await conn.close()\n }\n }\n}\n","import { canonicalJson } from '@tangle-network/agent-eval'\nimport type {\n MutableSurface,\n ProposeContext,\n ProposedCandidate,\n SurfaceProposer,\n} from '@tangle-network/agent-eval/campaign'\nimport {\n type AgentProfile,\n type AgentProfileDiff,\n changedAgentProfileAxes,\n} from '@tangle-network/agent-interface'\nimport { canonicalCandidateDigest } from '../candidate-execution/digest'\nimport {\n applyExactAgentProfileDiff,\n parseExactAgentProfile,\n parseExactAgentProfileDiff,\n} from '../candidate-execution/profile'\n\nexport type ProfileDiffProposerContext<TFindings = unknown> = ProposeContext<TFindings> & {\n profile: AgentProfile\n}\n\nexport interface ProfileDiffProposerOptions<TFindings = unknown> {\n proposeDiffs(\n context: ProfileDiffProposerContext<TFindings>,\n ): Promise<readonly AgentProfileDiff[]> | readonly AgentProfileDiff[]\n}\n\n/**\n * Turn exact AgentProfileDiffs from any source into full profile candidates for\n * the shared optimization loop. Research, catalogs, humans, and trace miners\n * differ only in `proposeDiffs`; measurement and promotion stay identical.\n */\nexport function profileDiffProposer<TFindings = unknown>(\n options: ProfileDiffProposerOptions<TFindings>,\n): SurfaceProposer<TFindings> {\n return {\n kind: 'agent-profile-diff',\n async propose(ctx): Promise<Array<MutableSurface | ProposedCandidate>> {\n if (typeof ctx.currentSurface !== 'string') {\n throw new Error('profileDiffProposer requires a serialized AgentProfile surface')\n }\n const profile = parseSerializedProfile(ctx.currentSurface)\n const proposals = await options.proposeDiffs({\n ...ctx,\n profile,\n })\n const candidates: ProposedCandidate[] = []\n const normalizedBaseline = applyExactAgentProfileDiff(\n profile,\n { kind: 'agent-profile-diff' },\n 'profile diff baseline',\n )\n const seen = new Set<string>([canonicalJson(normalizedBaseline)])\n for (const [index, proposal] of proposals.entries()) {\n if (candidates.length >= ctx.populationSize || ctx.signal.aborted) break\n const diff = parseExactAgentProfileDiff(proposal, `profile diff proposal ${index}`)\n const axes = changedAgentProfileAxes(diff)\n if (axes.length === 0) continue\n const candidate = applyExactAgentProfileDiff(\n profile,\n diff,\n `profile diff candidate ${index}`,\n )\n const surface = canonicalJson(candidate)\n if (seen.has(surface)) continue\n seen.add(surface)\n candidates.push({\n surface,\n label: diff.title ?? diff.id ?? `Change ${axes.join(', ')}`,\n rationale: candidateRationale(diff, axes),\n })\n }\n return candidates\n },\n }\n}\n\nfunction candidateRationale(diff: AgentProfileDiff, axes: readonly string[]): string {\n const reason = diff.rationale ?? `Changes profile axes: ${axes.join(', ')}.`\n const source = diff.source\n ? [diff.source.kind, ...(diff.source.artifacts ?? [])].join(' · ')\n : 'source unspecified'\n return `${reason} Source: ${source}. Diff: ${canonicalCandidateDigest(diff)}.`\n}\n\nfunction parseSerializedProfile(surface: string): AgentProfile {\n let raw: unknown\n try {\n raw = JSON.parse(surface)\n } catch (cause) {\n throw new Error('profileDiffProposer current surface is not valid AgentProfile JSON', {\n cause,\n })\n }\n return parseExactAgentProfile(raw, 'profileDiffProposer current profile')\n}\n","/**\n *\n * `reflectiveGenerator` — the cheap, no-sandbox `CandidateGenerator`. It drafts\n * surface edits via the existing improvement proposer (`proposeFromFindings`,\n * one LLM patch per finding) and applies them as ONE coherent improvement into\n * the candidate worktree. `maxShots` is ignored — reflection is single-shot by\n * construction (the patches are already drafted).\n *\n * This is the `shots=1, sandbox=off` setting of the one improvement driver.\n * The `agenticGenerator` (a multi-shot verify-in-session loop) is the\n * `shots=N` setting — both plug into the same `improvementDriver`.\n *\n * @experimental\n */\n\nimport { spawnSync } from 'node:child_process'\nimport type { SurfaceImprovementEdit } from '../agent/improvement-adapter'\nimport type { ImprovementProposalSource } from '../analyst-loop/types'\nimport type { CandidateGenerator } from './improvement-driver'\n\nexport interface ReflectiveGeneratorOptions {\n improvementProposalSource: ImprovementProposalSource<SurfaceImprovementEdit>\n}\n\n/** Cheap no-sandbox `CandidateGenerator` (the `shots=1` setting): draft surface edits via the improvement adapter and apply them as one coherent candidate. */\nexport function reflectiveGenerator(opts: ReflectiveGeneratorOptions): CandidateGenerator {\n return {\n kind: 'reflective',\n async generate({ worktreePath, findings }) {\n const batch = await opts.improvementProposalSource.proposeFromFindings(findings)\n if (batch.edits.length === 0) return { applied: false, summary: '' }\n\n let applied = 0\n for (const edit of batch.edits) {\n if (applyPatch(edit.patch, worktreePath)) applied++\n }\n if (applied === 0) return { applied: false, summary: '' }\n\n const summary =\n batch.edits.length === 1\n ? batch.edits[0]!.summary\n : `analyst: ${applied} surface edit${applied === 1 ? '' : 's'}`\n return { applied: true, summary }\n },\n }\n}\n\n/** Apply a proposed patch inside the isolated candidate worktree.\n * candidate worktree (a fresh checkout of baseRef, so `-p0` paths match). */\nfunction applyPatch(patch: string, cwd: string): boolean {\n const result = spawnSync('git', ['apply', '--whitespace=fix', '-p0', '-'], {\n cwd,\n input: patch,\n encoding: 'utf-8',\n })\n return result.status === 0\n}\n","/**\n *\n * Pure readiness-decision helper. Maps a `KnowledgeReadinessReport` from\n * `@tangle-network/agent-eval` to a three-state branch (`ready` / `blocked` /\n * `caveat`) the runtime, route handlers, and UI shells can all switch on.\n *\n * Default `minimumScore` of 0.7 mirrors the readiness scoring scale in\n * agent-eval; callers tightening or loosening this should keep it consistent\n * across all entry points for the same product so the UI / metrics agree on\n * what \"caveat\" means.\n *\n * @stable\n */\n\nimport type { KnowledgeReadinessReport } from '@tangle-network/agent-eval'\n\nimport { ValidationError } from './errors'\nimport type { KnowledgeReadinessDecision } from './types'\n\nconst DEFAULT_MINIMUM_READINESS_SCORE = 0.7\n\n/**\n * Map a `KnowledgeReadinessReport` to a three-state branch (`ready` / `blocked` / `caveat`) the runtime, route handlers, and UI shells all switch on.\n *\n * @stable\n */\nexport function decideKnowledgeReadiness(\n report: KnowledgeReadinessReport,\n options: { minimumScore?: number } = {},\n): KnowledgeReadinessDecision {\n const minimumScore = options.minimumScore ?? DEFAULT_MINIMUM_READINESS_SCORE\n if (!Number.isFinite(minimumScore) || minimumScore < 0 || minimumScore > 1) {\n throw new ValidationError(\n `minimumScore must be a finite number in [0, 1]; received ${String(minimumScore)}`,\n )\n }\n const blockingGapIds = report.blockingMissingRequirements.map((requirement) => requirement.id)\n const nonBlockingGapIds = report.nonBlockingGaps.map((requirement) => requirement.id)\n if (blockingGapIds.length > 0) {\n return {\n passed: false,\n status: 'blocked',\n reason: report.reason,\n readinessScore: report.readinessScore,\n recommendedAction: report.recommendedAction,\n severity: report.severity,\n blockingGapIds,\n nonBlockingGapIds,\n }\n }\n if (report.readinessScore < minimumScore) {\n return {\n passed: false,\n status: 'caveat',\n reason: `Knowledge readiness score ${report.readinessScore.toFixed(3)} is below minimum ${minimumScore.toFixed(3)}.`,\n readinessScore: report.readinessScore,\n recommendedAction: report.recommendedAction,\n severity: report.severity,\n blockingGapIds,\n nonBlockingGapIds,\n }\n }\n return {\n passed: true,\n status: 'ready',\n reason: report.reason,\n readinessScore: report.readinessScore,\n recommendedAction: report.recommendedAction,\n severity: report.severity,\n blockingGapIds,\n nonBlockingGapIds,\n }\n}\n","/**\n * The product-facing backend selector for `runChatThroughRuntime` /\n * `runAgentTaskStream`: one call turns a `--backend {router,tcloud,cli-bridge,\n * sandbox}` choice into the `AgentExecutionBackend` the chat leg runs on.\n *\n * It is the `AgentExecutionBackend` sibling of `resolveSandboxClient` (which\n * resolves the `SandboxClient` a `runLoop` drives). Both exist for the same\n * reason: every in-process eval product hand-rolled the identical\n * \"`backend-name` → `createOpenAICompatibleBackend`\" branch, and the copies\n * drift. This is the single generic resolver they share.\n *\n * - `router` / `tcloud` / `cli-bridge` → OpenAI-compatible chat completions.\n * All three speak `POST {baseUrl}/chat/completions` in OpenAI's SSE shape —\n * the router (a.k.a. tcloud) IS that endpoint, and cli-bridge fronts a\n * harness CLI behind the same protocol at its own `/v1`. They differ only\n * in `baseUrl` / `apiKey` and the `kind` label a product wants on its\n * traces. cli-bridge REQUIRES `model` in the request body, so it MUST route\n * through `createOpenAICompatibleBackend` (which sends it), never a\n * transport that drops the field.\n * - `sandbox` → the caller's own domain backend. The sandbox variant carries\n * product specifics (system prompt, workspace id, in-box D1 executor) that\n * do NOT belong in the substrate, so the product passes a `sandboxBackend()`\n * seam that this resolver simply invokes.\n *\n * This resolver is PURE backend selection. Product concerns — credit hard-cuts,\n * fetch-capture shims, D1 platform wiring — stay as product-side WRAPPERS\n * around the returned backend. The OpenAI-compat passthrough fields (`tools`,\n * `toolChoice`, `responseFormat`, `temperature`, `maxTokens`, `fetchImpl`,\n * `retry`) are forwarded verbatim so a product can advertise its app tools,\n * preserve generation settings, or install a capturing fetch without\n * re-opening the branch this consolidation closes.\n */\n\nimport { createOpenAICompatibleBackend } from './backends'\nimport type { AgentBackendInput, AgentExecutionBackend } from './types'\n\n/** The transport a chat backend runs on. */\nexport type AgentBackendKind = 'router' | 'tcloud' | 'cli-bridge' | 'sandbox'\n\n/**\n * OpenAI-compat passthrough forwarded to `createOpenAICompatibleBackend` for\n * the `router` / `tcloud` / `cli-bridge` kinds. Mirrors that factory's optional\n * inputs so a product keeps its tool advertising / capture-fetch without\n * re-implementing the backend branch.\n */\ntype OpenAICompatPassthrough = Pick<\n Parameters<typeof createOpenAICompatibleBackend>[0],\n 'tools' | 'toolChoice' | 'responseFormat' | 'temperature' | 'maxTokens' | 'fetchImpl' | 'retry'\n>\n\nexport interface ResolveAgentBackendOptions<TInput extends AgentBackendInput = AgentBackendInput>\n extends OpenAICompatPassthrough {\n /** The chat transport to resolve. */\n kind: AgentBackendKind\n /**\n * Bearer credential for the OpenAI-compat kinds. Empty string is valid for a\n * loopback-anonymous cli-bridge; a `router`/`tcloud` route with an empty key\n * is a caller bug the product surfaces before calling in.\n */\n apiKey: string\n /** Base URL for the OpenAI-compat kinds. cli-bridge's is its `/v1`. */\n baseUrl: string\n /** Model id sent on every request. cli-bridge rejects a request without it. */\n model: string\n /** `kind` label stamped on the resolved backend + its traces. Defaults to `kind`. */\n label?: string\n /**\n * `sandbox` kind: the product's own domain backend. Required for that kind —\n * the substrate owns no product sandbox shape, so a `sandbox` resolution with\n * no seam is a caller bug, not a silent fallback.\n */\n sandboxBackend?: () => AgentExecutionBackend<TInput>\n}\n\n/**\n * Resolve the `AgentExecutionBackend` for the chosen `kind`. Reuse this instead\n * of hand-rolling the `createOpenAICompatibleBackend` branch in each product.\n */\nexport function resolveAgentBackend<TInput extends AgentBackendInput = AgentBackendInput>(\n opts: ResolveAgentBackendOptions<TInput>,\n): AgentExecutionBackend<TInput> {\n switch (opts.kind) {\n case 'router':\n case 'tcloud':\n case 'cli-bridge': {\n const passthrough: OpenAICompatPassthrough = {}\n // Forward only the fields a caller actually set — an explicit\n // `tools: []` / `undefined` would otherwise reach the factory and change\n // its request shape (some providers reject an empty `tools` array).\n if (opts.tools !== undefined) passthrough.tools = opts.tools\n if (opts.toolChoice !== undefined) passthrough.toolChoice = opts.toolChoice\n if (opts.responseFormat !== undefined) passthrough.responseFormat = opts.responseFormat\n if (opts.temperature !== undefined) passthrough.temperature = opts.temperature\n if (opts.maxTokens !== undefined) passthrough.maxTokens = opts.maxTokens\n if (opts.fetchImpl !== undefined) passthrough.fetchImpl = opts.fetchImpl\n if (opts.retry !== undefined) passthrough.retry = opts.retry\n return createOpenAICompatibleBackend<TInput>({\n apiKey: opts.apiKey,\n baseUrl: opts.baseUrl,\n model: opts.model,\n kind: opts.label ?? opts.kind,\n ...passthrough,\n })\n }\n case 'sandbox': {\n if (!opts.sandboxBackend) {\n throw new Error(\"resolveAgentBackend: kind 'sandbox' requires opts.sandboxBackend\")\n }\n return opts.sandboxBackend()\n }\n }\n}\n","/**\n *\n * The two top-level entry points:\n *\n * - `runAgentTask` — single-shot lifecycle for adapter-driven tasks.\n * - `runAgentTaskStream` — streaming lifecycle that delegates execution to an\n * `AgentExecutionBackend` (model API, sandbox, or custom iterable).\n *\n * Both gate the run on `KnowledgeReadinessReport` from `agent-eval`, emit the\n * same lifecycle event vocabulary (under different shapes — see `types.ts`),\n * and route session lifecycle through a pluggable `RuntimeSessionStore`.\n *\n * @stable\n */\n\nimport {\n acquisitionPlansForKnowledgeGaps,\n blockingKnowledgeEval,\n type ControlContext,\n type ControlEvalResult,\n type ControlRunResult,\n type DataAcquisitionPlan,\n FAILURE_CLASSES,\n type FailureClass,\n type KnowledgeReadinessReport,\n type RunRecord,\n runAgentControlLoop,\n scoreKnowledgeReadiness,\n type UserQuestion,\n userQuestionsForKnowledgeGaps,\n} from '@tangle-network/agent-eval'\n\nconst FAILURE_CLASS_SET = new Set<string>(FAILURE_CLASSES)\n\n/** True when a free-form control failure string is a canonical taxonomy\n * class — so only real taxonomy tags are promoted to the cross-agent\n * `RunRecord.failureClass` key; novel strings stay as `failureMode` detail. */\nfunction asFailureClass(value: string | undefined): FailureClass | undefined {\n return value && FAILURE_CLASS_SET.has(value) ? (value as FailureClass) : undefined\n}\n\n/** Stamp cross-cutting defaults onto adapter-projected RunRecords without\n * overriding anything the adapter set explicitly:\n * - `scenarioId` — the run's scenario, when the record omits one.\n * - `failureClass` — the control layer's failure classification promoted\n * onto the canonical cross-agent key, but ONLY when it's a real taxonomy\n * class. This is what lets the substrate aggregate failures across every\n * agent in one vocabulary instead of per-agent ad-hoc strings. */\nexport function applyRunRecordDefaults(\n records: RunRecord[],\n scenarioId: string,\n controlFailureClass: string | undefined,\n): RunRecord[] {\n const fc = asFailureClass(controlFailureClass)\n return records.map((record) => {\n let r = record\n if (r.scenarioId === undefined) r = { ...r, scenarioId }\n if (r.failureClass === undefined && fc) r = { ...r, failureClass: fc }\n return r\n })\n}\n\nimport { normalizeBackendStreamEvent } from './backends'\nimport { BackendTransportError } from './errors'\nimport { decideKnowledgeReadiness } from './readiness'\nimport { nowIso, startOrResumeRuntimeSession, touchSession } from './sessions'\nimport type {\n AgentBackendInput,\n AgentKnowledgeProvider,\n AgentRuntimeEventSink,\n AgentTaskContext,\n AgentTaskRunResult,\n AgentTaskSpec,\n AgentTaskStatus,\n BackendErrorDetail,\n RunAgentTaskOptions,\n RunAgentTaskStreamOptions,\n RuntimeStreamEvent,\n} from './types'\n\n/**\n * Single-shot task lifecycle for adapter-driven tasks: readiness-gated, emits the runtime lifecycle event vocabulary, session-store pluggable.\n *\n * @stable\n */\nexport async function runAgentTask<\n TState,\n TAction,\n TActionResult,\n TEval extends ControlEvalResult = ControlEvalResult,\n>(\n options: RunAgentTaskOptions<TState, TAction, TActionResult, TEval>,\n): Promise<AgentTaskRunResult<TState, TAction, TActionResult, TEval>> {\n const task = options.task\n await emit(options.onEvent, { type: 'task_start', task })\n await emit(options.onEvent, { type: 'readiness_start', task })\n let knowledge = await buildReadiness(task, options.knowledge)\n await emit(options.onEvent, { type: 'readiness_end', task, knowledge })\n const questions = userQuestionsForKnowledgeGaps(knowledge.blockingMissingRequirements)\n const acquisitionPlans = acquisitionPlansForKnowledgeGaps([\n ...knowledge.blockingMissingRequirements,\n ...knowledge.nonBlockingGaps,\n ])\n const preflight = await runKnowledgePreflight(\n task,\n questions,\n acquisitionPlans,\n options.knowledge,\n options.onEvent,\n )\n if (\n options.knowledge?.refreshReadiness &&\n (Object.keys(preflight.userAnswers).length > 0 || preflight.acquiredEvidenceIds.length > 0)\n ) {\n await emit(options.onEvent, { type: 'readiness_start', task })\n knowledge = await options.knowledge.refreshReadiness({\n task,\n previous: knowledge,\n userAnswers: preflight.userAnswers,\n acquiredEvidenceIds: preflight.acquiredEvidenceIds,\n })\n await emit(options.onEvent, { type: 'readiness_end', task, knowledge })\n }\n\n await emit(options.onEvent, { type: 'control_start', task, knowledge })\n const scenarioId = options.scenarioId ?? task.id\n const control = await runAgentControlLoop<TState, TAction, TActionResult, TEval>({\n intent: task.intent,\n budget: task.budget,\n signal: options.signal,\n store: options.store,\n scenarioId,\n projectId: options.projectId,\n variantId: options.variantId,\n observe: ({ history, abortSignal }) =>\n options.adapter.observe({ task, knowledge, history, abortSignal }),\n validate: async ({ state, history, abortSignal }) => {\n const readinessEval = blockingKnowledgeEval(knowledge, {\n minimumScore: options.minimumReadinessScore,\n })\n const evals = await options.adapter.validate({\n task,\n knowledge,\n state,\n history,\n abortSignal,\n })\n return [readinessEval as TEval, ...evals]\n },\n decide: (ctx) => {\n if (isKnowledgeBlocked(ctx.evals)) {\n return (\n options.adapter.onKnowledgeBlocked?.({\n task,\n knowledge,\n questions,\n acquisitionPlans,\n }) ?? {\n type: 'stop',\n pass: false,\n score: knowledge.readinessScore,\n reason: `knowledge readiness blocked: ${knowledge.reason}`,\n }\n )\n }\n return options.adapter.decide(toAgentContext(task, knowledge, ctx))\n },\n act: (action, ctx) => options.adapter.act(action, toAgentContext(task, knowledge, ctx)),\n shouldStop: options.adapter.shouldStop\n ? (ctx) => options.adapter.shouldStop!(toAgentContext(task, knowledge, ctx))\n : undefined,\n getActionCostUsd: options.adapter.getActionCostUsd\n ? ({ action, result, state, evals, history }) =>\n options.adapter.getActionCostUsd!({ action, result, task, state, evals, history })\n : undefined,\n onStep: (step) => emit(options.onEvent, { type: 'control_step', task, step }),\n })\n await emit(options.onEvent, { type: 'control_end', task, control })\n const status = statusFromControl(control)\n await emit(options.onEvent, { type: 'task_end', task, status, reason: control.reason })\n\n return {\n task,\n status,\n knowledge,\n questions,\n acquisitionPlans,\n userAnswers: preflight.userAnswers,\n acquiredEvidenceIds: preflight.acquiredEvidenceIds,\n control,\n runRecords: applyRunRecordDefaults(\n options.adapter.projectRunRecords?.(control, task) ?? [],\n scenarioId,\n control.failureClass,\n ),\n }\n}\n\n/**\n * Streaming task lifecycle: delegates execution to an `AgentExecutionBackend` (model API, sandbox, or custom iterable) and yields lifecycle events as they happen.\n *\n * @stable\n */\nexport async function* runAgentTaskStream<TInput extends AgentBackendInput = AgentBackendInput>(\n options: RunAgentTaskStreamOptions<TInput>,\n): AsyncIterable<RuntimeStreamEvent> {\n const task = options.task\n const input = { task, ...(options.input ?? {}) } as TInput\n yield streamEvent({ type: 'task_start', task })\n\n yield streamEvent({ type: 'readiness_start', task })\n let knowledge = await buildReadiness(task, options.knowledge)\n const questions = userQuestionsForKnowledgeGaps(knowledge.blockingMissingRequirements)\n const acquisitionPlans = acquisitionPlansForKnowledgeGaps([\n ...knowledge.blockingMissingRequirements,\n ...knowledge.nonBlockingGaps,\n ])\n const preflight = await runKnowledgePreflightStream(\n task,\n questions,\n acquisitionPlans,\n options.knowledge,\n )\n for (const event of preflight.events) yield event\n if (\n options.knowledge?.refreshReadiness &&\n (Object.keys(preflight.userAnswers).length > 0 || preflight.acquiredEvidenceIds.length > 0)\n ) {\n yield streamEvent({ type: 'readiness_start', task })\n knowledge = await options.knowledge.refreshReadiness({\n task,\n previous: knowledge,\n userAnswers: preflight.userAnswers,\n acquiredEvidenceIds: preflight.acquiredEvidenceIds,\n })\n }\n const decision = decideKnowledgeReadiness(knowledge, {\n minimumScore: options.minimumReadinessScore,\n })\n yield streamEvent({ type: 'readiness_end', task, knowledge, decision })\n if (!decision.passed && decision.status === 'blocked') {\n const reason = `knowledge readiness blocked: ${decision.reason}`\n yield streamEvent({ type: 'task_end', task, status: 'blocked', reason })\n yield streamEvent({ type: 'final', task, status: 'blocked', reason })\n return\n }\n\n const store = options.sessionStore\n const opened = await startOrResumeRuntimeSession({\n backend: options.backend,\n input,\n context: { task, knowledge, signal: options.signal },\n store,\n sessionId: options.sessionId,\n resume: options.resume,\n })\n let session = opened.session\n const shouldResume = opened.resumed\n const sessionEvent = streamEvent({\n type: shouldResume ? 'session_resumed' : 'session_created',\n task,\n session,\n })\n await store?.appendEvent?.(session.id, sessionEvent)\n yield sessionEvent\n\n const backendStart = streamEvent({\n type: 'backend_start',\n task,\n session,\n backend: options.backend.kind,\n })\n await store?.appendEvent?.(session.id, backendStart)\n yield backendStart\n\n let finalText = ''\n try {\n for await (const rawEvent of options.backend.stream(opened.input, {\n task,\n knowledge,\n session,\n signal: options.signal,\n })) {\n const event = normalizeBackendStreamEvent(rawEvent, task, session)\n if (event.type === 'text_delta') finalText += event.text\n await store?.appendEvent?.(session.id, event)\n yield event\n }\n const completedStatus: AgentTaskStatus = 'completed'\n session = touchSession({ ...session, status: completedStatus })\n await store?.put(session)\n const backendEnd = streamEvent({\n type: 'backend_end',\n task,\n session,\n backend: options.backend.kind,\n })\n await store?.appendEvent?.(session.id, backendEnd)\n yield backendEnd\n const reason = 'backend completed'\n const taskEnd = streamEvent({ type: 'task_end', task, status: completedStatus, reason })\n await store?.appendEvent?.(session.id, taskEnd)\n yield taskEnd\n const final = streamEvent({\n type: 'final',\n task,\n session,\n status: completedStatus,\n reason,\n text: finalText || undefined,\n })\n await store?.appendEvent?.(session.id, final)\n yield final\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err)\n session = touchSession({ ...session, status: options.signal?.aborted ? 'aborted' : 'failed' })\n await store?.put(session)\n let stopErrorMessage: string | undefined\n try {\n await options.backend.stop?.(session, message)\n } catch (stopErr) {\n stopErrorMessage = stopErr instanceof Error ? stopErr.message : String(stopErr)\n }\n const combinedMessage = stopErrorMessage\n ? `${message}; backend stop failed: ${stopErrorMessage}`\n : message\n // Typed transport detail — preserves status code + truncated body so\n // consumers can map onto `RunRecord.error` without re-parsing the log\n // string. Required by the runtime's fail-loud contract: silent empty\n // output for a 402 / 401 / 5xx hides the real failure mode.\n const errorDetail: BackendErrorDetail =\n err instanceof BackendTransportError\n ? {\n kind: 'transport',\n message: combinedMessage,\n status: err.status,\n body: err.body,\n }\n : { kind: 'backend', message: combinedMessage }\n const backendError = streamEvent({\n type: 'backend_error',\n task,\n session,\n backend: options.backend.kind,\n message: combinedMessage,\n recoverable: !options.signal?.aborted,\n error: errorDetail,\n })\n await store?.appendEvent?.(session.id, backendError)\n yield backendError\n const status: AgentTaskStatus = options.signal?.aborted ? 'aborted' : 'failed'\n const taskEnd = streamEvent({ type: 'task_end', task, status, reason: message })\n await store?.appendEvent?.(session.id, taskEnd)\n yield taskEnd\n const final = streamEvent({\n type: 'final',\n task,\n session,\n status,\n reason: message,\n text: finalText || undefined,\n error: errorDetail,\n })\n await store?.appendEvent?.(session.id, final)\n yield final\n }\n}\n\nasync function runKnowledgePreflight<\n TState,\n TAction,\n TActionResult,\n TEval extends ControlEvalResult,\n>(\n task: AgentTaskSpec,\n questions: UserQuestion[],\n acquisitionPlans: DataAcquisitionPlan[],\n provider: AgentKnowledgeProvider | undefined,\n onEvent: AgentRuntimeEventSink<TState, TAction, TActionResult, TEval> | undefined,\n): Promise<{ userAnswers: Record<string, string>; acquiredEvidenceIds: string[] }> {\n let userAnswers: Record<string, string> = {}\n let acquiredEvidenceIds: string[] = []\n if (questions.length > 0 && provider?.answerQuestions) {\n await emit(onEvent, { type: 'questions_start', task, questions })\n userAnswers = await provider.answerQuestions(questions, task)\n await emit(onEvent, { type: 'questions_end', task, questions, userAnswers })\n }\n if (acquisitionPlans.length > 0 && provider?.executeAcquisitionPlans) {\n await emit(onEvent, { type: 'acquisition_start', task, acquisitionPlans })\n acquiredEvidenceIds = await provider.executeAcquisitionPlans(acquisitionPlans, task)\n await emit(onEvent, {\n type: 'acquisition_end',\n task,\n acquisitionPlans,\n acquiredEvidenceIds,\n })\n }\n return { userAnswers, acquiredEvidenceIds }\n}\n\nasync function runKnowledgePreflightStream(\n task: AgentTaskSpec,\n questions: UserQuestion[],\n acquisitionPlans: DataAcquisitionPlan[],\n provider: AgentKnowledgeProvider | undefined,\n): Promise<{\n userAnswers: Record<string, string>\n acquiredEvidenceIds: string[]\n events: RuntimeStreamEvent[]\n}> {\n const events: RuntimeStreamEvent[] = []\n let userAnswers: Record<string, string> = {}\n let acquiredEvidenceIds: string[] = []\n if (questions.length > 0 && provider?.answerQuestions) {\n events.push(streamEvent({ type: 'questions_start', task, questions }))\n userAnswers = await provider.answerQuestions(questions, task)\n events.push(streamEvent({ type: 'questions_end', task, questions, userAnswers }))\n }\n if (acquisitionPlans.length > 0 && provider?.executeAcquisitionPlans) {\n events.push(streamEvent({ type: 'acquisition_start', task, acquisitionPlans }))\n acquiredEvidenceIds = await provider.executeAcquisitionPlans(acquisitionPlans, task)\n events.push(\n streamEvent({ type: 'acquisition_end', task, acquisitionPlans, acquiredEvidenceIds }),\n )\n }\n return { userAnswers, acquiredEvidenceIds, events }\n}\n\nfunction streamEvent<T extends Omit<RuntimeStreamEvent, 'timestamp'>>(\n event: T,\n): T & { timestamp: string } {\n return { ...event, timestamp: nowIso() }\n}\n\nfunction buildReadiness(\n task: AgentTaskSpec,\n provider: AgentKnowledgeProvider | undefined,\n): Promise<KnowledgeReadinessReport> | KnowledgeReadinessReport {\n if (provider?.buildReadiness) return provider.buildReadiness(task)\n return scoreKnowledgeReadiness({\n taskId: task.id,\n requirements: task.requiredKnowledge ?? [],\n metadata: { domain: task.domain, ...task.metadata },\n })\n}\n\nfunction isKnowledgeBlocked(evals: ControlEvalResult[]): boolean {\n return evals.some((evalResult) => evalResult.id === 'knowledge-ready' && !evalResult.passed)\n}\n\nfunction statusFromControl(\n control: ControlRunResult<unknown, unknown, unknown, ControlEvalResult>,\n): AgentTaskStatus {\n if (control.stoppedBy === 'abort') return 'aborted'\n if (control.reason.includes('knowledge readiness blocked')) return 'blocked'\n if (control.pass) return 'completed'\n return 'failed'\n}\n\nasync function emit<TState, TAction, TActionResult, TEval extends ControlEvalResult>(\n sink: AgentRuntimeEventSink<TState, TAction, TActionResult, TEval> | undefined,\n event: Parameters<AgentRuntimeEventSink<TState, TAction, TActionResult, TEval>>[0],\n): Promise<void> {\n await sink?.(event)\n}\n\nfunction toAgentContext<TState, TAction, TActionResult, TEval extends ControlEvalResult>(\n task: AgentTaskSpec,\n knowledge: KnowledgeReadinessReport,\n ctx: ControlContext<TState, TAction, TActionResult, TEval>,\n): AgentTaskContext<TState, TAction, TActionResult, TEval> {\n return {\n task,\n knowledge,\n state: ctx.state,\n evals: ctx.evals,\n history: ctx.history,\n budget: ctx.budget,\n stepIndex: ctx.stepIndex,\n wallMs: ctx.wallMs,\n spentCostUsd: ctx.spentCostUsd,\n remainingCostUsd: ctx.remainingCostUsd,\n abortSignal: ctx.abortSignal,\n }\n}\n","/**\n *\n * Production-run lifecycle: record what the agent did on behalf of a customer,\n * what it cost, and how it ended.\n *\n * Three concerns live in this module:\n *\n * 1. **Lifecycle state machine** — `running` -> `completed | failed | cancelled`,\n * enforced by `RuntimeRunStateError`. Completion is idempotent for the same\n * status (a second `complete()` call is a no-op so retries / cleanup paths\n * don't double-fire side effects). A different terminal status is a state\n * error.\n *\n * 2. **Cost ledger** — every `llm_call` event the handle observes contributes\n * `tokensIn`, `tokensOut`, `costUsd`, and bumps `llmCalls`. Wall time is\n * measured from `startRuntimeRun()` to `complete()`. Surface via\n * `handle.cost()` for cost-per-task dashboards.\n *\n * 3. **Persistence adapter** — `RuntimeRunPersistenceAdapter` is the seam\n * consumers plug in to write a `RuntimeRunRow` to their D1 / postgres /\n * KV store. The adapter receives a sanitized row shape; no telemetry\n * payload bytes flow through it unless the consumer opts in via\n * `RuntimeRunOptions.telemetryEvents`.\n *\n * @stable\n */\n\nimport { RuntimeRunStateError, ValidationError } from './errors'\nimport type { AgentTaskSpec, RuntimeStreamEvent } from './types'\n\n/** @stable */\nexport type RuntimeRunStatus = 'running' | 'completed' | 'failed' | 'cancelled'\n\n/** @stable */\nexport interface RuntimeRunCost {\n /** Cumulative input tokens across every observed `llm_call` event. */\n tokensIn: number\n /** Cumulative output tokens across every observed `llm_call` event. */\n tokensOut: number\n /** Sum of `costUsd` from every observed `llm_call` event. */\n costUsd: number\n /** Wall time from `startRuntimeRun()` to `complete()` (or `now()` if not yet completed). */\n wallMs: number\n /** Count of `llm_call` events observed during the run. */\n llmCalls: number\n}\n\n/** @stable */\nexport interface RuntimeRunCompleteInput {\n status: Exclude<RuntimeRunStatus, 'running'>\n resultSummary?: string\n /** Optional explicit cost override; if omitted, the accumulated ledger is used. */\n cost?: Partial<RuntimeRunCost>\n /** Stable error message when `status === 'failed'`. */\n error?: string\n /** Additional adapter-specific fields merged into the persisted row. */\n metadata?: Record<string, unknown>\n}\n\n/** @stable */\nexport interface RuntimeRunRow {\n /** Stable runtime-side identifier. Adapters may translate to their own primary key. */\n id: string\n workspaceId: string\n sessionId?: string\n agentId?: string\n domain?: string\n taskId: string\n scenarioId?: string\n status: RuntimeRunStatus\n resultSummary?: string\n error?: string\n cost: RuntimeRunCost\n startedAt: string\n completedAt?: string\n metadata?: Record<string, unknown>\n}\n\n/** @stable */\nexport interface RuntimeRunPersistenceAdapter {\n /**\n * Called once when `handle.persist()` runs. Implementations write `row` to\n * their durable store (D1, postgres, KV) and return whatever the consumer\n * wants the caller to see (often the storage-side row id). Errors thrown\n * here propagate out of `persist()` so the caller can decide whether to\n * retry or log-and-continue.\n */\n upsert(row: RuntimeRunRow): Promise<void> | void\n}\n\n/** @stable */\nexport interface RuntimeRunOptions {\n workspaceId: string\n sessionId?: string\n agentId?: string\n taskSpec: AgentTaskSpec\n scenarioId?: string\n /** Optional persistence adapter; if omitted, `persist()` is a no-op. */\n adapter?: RuntimeRunPersistenceAdapter\n /** Override the row id; default = `${taskSpec.id}:${random suffix}`. */\n id?: string\n /** Override the clock; default = `Date.now()`. Useful for deterministic tests. */\n now?: () => number\n}\n\n/** @stable */\nexport interface RuntimeRunHandle {\n /** Stable id assigned at start. */\n readonly id: string\n readonly workspaceId: string\n readonly sessionId: string | undefined\n readonly taskSpec: AgentTaskSpec\n readonly status: RuntimeRunStatus\n\n /**\n * Observe a single `RuntimeStreamEvent`. The handle ignores non-cost events\n * (text deltas, tool calls) silently so consumers can pipe the whole stream\n * through `handle.observe`. `llm_call` events update the ledger.\n */\n observe(event: RuntimeStreamEvent): void\n\n /** Snapshot of the current cost ledger. Safe to call at any time. */\n cost(): RuntimeRunCost\n\n /**\n * Transition to a terminal state. Idempotent for the same status; throws\n * `RuntimeRunStateError` for a different terminal status (state machines\n * don't time-travel).\n */\n complete(input: RuntimeRunCompleteInput): void\n\n /** Build the current row without writing it. Useful for tests + dry runs. */\n toRow(metadata?: Record<string, unknown>): RuntimeRunRow\n\n /**\n * Persist the current row via the configured adapter. Must be called after\n * `complete()`. Idempotent for the same terminal state (the adapter sees\n * the same row on retry).\n */\n persist(metadata?: Record<string, unknown>): Promise<void>\n}\n\n/**\n *\n * Construct a runtime-run handle. The returned handle is mutable across its\n * lifetime; consumers should not share it across requests.\n *\n * @stable\n */\nexport function startRuntimeRun(options: RuntimeRunOptions): RuntimeRunHandle {\n if (!options.workspaceId) {\n throw new ValidationError('startRuntimeRun: workspaceId is required')\n }\n if (!options.taskSpec?.id) {\n throw new ValidationError('startRuntimeRun: taskSpec.id is required')\n }\n const now = options.now ?? Date.now\n const startedAtMs = now()\n const startedAt = new Date(startedAtMs).toISOString()\n const id = options.id ?? `${options.taskSpec.id}:${randomSuffix()}`\n\n let status: RuntimeRunStatus = 'running'\n let completedAtMs: number | undefined\n let resultSummary: string | undefined\n let error: string | undefined\n let completionMetadata: Record<string, unknown> | undefined\n\n const ledger: RuntimeRunCost = {\n tokensIn: 0,\n tokensOut: 0,\n costUsd: 0,\n wallMs: 0,\n llmCalls: 0,\n }\n\n const snapshotCost = (): RuntimeRunCost => ({\n tokensIn: ledger.tokensIn,\n tokensOut: ledger.tokensOut,\n costUsd: ledger.costUsd,\n wallMs: (completedAtMs ?? now()) - startedAtMs,\n llmCalls: ledger.llmCalls,\n })\n\n const buildRow = (extraMetadata?: Record<string, unknown>): RuntimeRunRow => ({\n id,\n workspaceId: options.workspaceId,\n sessionId: options.sessionId,\n agentId: options.agentId,\n domain: options.taskSpec.domain,\n taskId: options.taskSpec.id,\n scenarioId: options.scenarioId,\n status,\n resultSummary,\n error,\n cost: snapshotCost(),\n startedAt,\n completedAt: completedAtMs !== undefined ? new Date(completedAtMs).toISOString() : undefined,\n metadata: mergeMetadata(completionMetadata, extraMetadata),\n })\n\n return {\n id,\n workspaceId: options.workspaceId,\n sessionId: options.sessionId,\n taskSpec: options.taskSpec,\n get status() {\n return status\n },\n observe(event) {\n if (event.type !== 'llm_call') return\n ledger.llmCalls += 1\n if (typeof event.tokensIn === 'number' && Number.isFinite(event.tokensIn)) {\n ledger.tokensIn += event.tokensIn\n }\n if (typeof event.tokensOut === 'number' && Number.isFinite(event.tokensOut)) {\n ledger.tokensOut += event.tokensOut\n }\n if (typeof event.costUsd === 'number' && Number.isFinite(event.costUsd)) {\n ledger.costUsd += event.costUsd\n }\n },\n cost: snapshotCost,\n complete(input) {\n // JS callers can bypass the `Exclude<…, 'running'>` type; enforce the\n // state machine at runtime as well.\n if ((input.status as RuntimeRunStatus) === 'running') {\n throw new ValidationError('complete() requires a terminal status, got \"running\"')\n }\n if (status !== 'running') {\n if (status === input.status) return\n throw new RuntimeRunStateError(\n `Cannot transition runtime run from \"${status}\" to \"${input.status}\"`,\n )\n }\n status = input.status\n completedAtMs = now()\n resultSummary = input.resultSummary\n error = input.error\n completionMetadata = input.metadata\n if (input.cost) {\n if (typeof input.cost.tokensIn === 'number' && Number.isFinite(input.cost.tokensIn)) {\n ledger.tokensIn = input.cost.tokensIn\n }\n if (typeof input.cost.tokensOut === 'number' && Number.isFinite(input.cost.tokensOut)) {\n ledger.tokensOut = input.cost.tokensOut\n }\n if (typeof input.cost.costUsd === 'number' && Number.isFinite(input.cost.costUsd)) {\n ledger.costUsd = input.cost.costUsd\n }\n if (typeof input.cost.llmCalls === 'number' && Number.isFinite(input.cost.llmCalls)) {\n ledger.llmCalls = input.cost.llmCalls\n }\n }\n },\n toRow(metadata) {\n return buildRow(metadata)\n },\n async persist(metadata) {\n if (status === 'running') {\n throw new RuntimeRunStateError('Cannot persist a runtime run before complete() is called')\n }\n if (!options.adapter) return\n await options.adapter.upsert(buildRow(metadata))\n },\n }\n}\n\nfunction mergeMetadata(\n base: Record<string, unknown> | undefined,\n extra: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n if (!base && !extra) return undefined\n return { ...(base ?? {}), ...(extra ?? {}) }\n}\n\nfunction randomSuffix(): string {\n // 8 chars of base36 — sufficient for in-process uniqueness. Callers needing\n // stronger guarantees pass `options.id` explicitly.\n return Math.random().toString(36).slice(2, 10)\n}\n","/**\n *\n * Server-Sent Events serialization for runtime telemetry streams.\n *\n * Newline-safe by construction: any newline in `id` or `event` is collapsed to\n * a space (browsers terminate fields on newline), and multi-line `data`\n * payloads are split into one `data:` line per source line so JSON.stringify\n * output transports cleanly.\n *\n * @stable\n */\n\nimport type { KnowledgeReadinessReport } from '@tangle-network/agent-eval'\nimport type { RuntimeTelemetryOptions } from './sanitize'\nimport { sanitizeKnowledgeReadinessReport, sanitizeRuntimeStreamEvent } from './sanitize'\nimport type { RuntimeStreamEvent } from './types'\n\n/** @stable */\nexport interface ServerSentEventOptions {\n event?: string\n id?: string\n retry?: number\n}\n\n/** @stable */\nexport function encodeServerSentEvent(data: unknown, options: ServerSentEventOptions = {}): string {\n const lines: string[] = []\n if (options.id) lines.push(`id: ${stripNewlines(options.id)}`)\n if (options.event) lines.push(`event: ${stripNewlines(options.event)}`)\n if (typeof options.retry === 'number' && Number.isFinite(options.retry) && options.retry >= 0) {\n lines.push(`retry: ${Math.floor(options.retry)}`)\n }\n\n const payload = typeof data === 'string' ? data : JSON.stringify(data)\n for (const line of payload.split(/\\r?\\n/)) {\n lines.push(`data: ${line}`)\n }\n return `${lines.join('\\n')}\\n\\n`\n}\n\n/** Serialize a `KnowledgeReadinessReport` as a Server-Sent Event string. @stable */\nexport function readinessServerSentEvent(\n report: KnowledgeReadinessReport,\n options: RuntimeTelemetryOptions & ServerSentEventOptions = {},\n): string {\n const { event, id, retry, ...telemetryOptions } = options\n return encodeServerSentEvent(\n {\n type: 'readiness',\n readiness: sanitizeKnowledgeReadinessReport(report, telemetryOptions),\n },\n { event, id, retry },\n )\n}\n\n/** Serialize a `RuntimeStreamEvent` as a Server-Sent Event string. @stable */\nexport function runtimeStreamServerSentEvent(\n event: RuntimeStreamEvent,\n options: RuntimeTelemetryOptions & ServerSentEventOptions = {},\n): string {\n const { event: sseEvent, id, retry, ...telemetryOptions } = options\n return encodeServerSentEvent(sanitizeRuntimeStreamEvent(event, telemetryOptions), {\n event: sseEvent,\n id,\n retry,\n })\n}\n\nfunction stripNewlines(value: string): string {\n return value.replace(/[\\r\\n]/g, ' ')\n}\n","/**\n * Bounded turn-level tool-dispatch loop.\n *\n * `runAgentTaskStream` runs ONE model turn; `runLoop` orchestrates DELEGATED\n * multi-agent topologies (refine / fanout-vote). Neither is the everyday\n * interactive shape: a chat turn where the model may emit tool calls, each is\n * executed, the results are folded back, and the turn re-runs until the model\n * stops (or a turn cap). Every agent app hand-rolls that loop — this is it,\n * as a reusable primitive.\n *\n * Substrate-neutral by design: the caller supplies `streamTurn` (wrapping\n * whatever backend / `runAgentTaskStream` it uses) and `executeToolCall`\n * (routing to its executors). This module owns the LOOP; the caller owns the\n * model and the executors. `Raw` (streaming variant) is the caller's own\n * event type. The only imported contract is the runtime hook type: hooks are\n * execution-scoped observers, not part of the agent profile.\n */\n\nimport type { RuntimeDecisionEvidenceRef, RuntimeHooks } from './runtime-hooks'\nimport { notifyRuntimeDecisionPoint, notifyRuntimeHookEvent } from './runtime-hooks'\n\nexport interface ToolLoopCall {\n toolCallId?: string\n toolName: string\n args: Record<string, unknown>\n}\n\n/** Outcome of one tool dispatch — structurally compatible with a hub/integration\n * tool-outcome union, so callers can fold either through the loop. */\nexport type ToolCallOutcome =\n | { ok: true; result: unknown }\n | { ok: false; code: string; message: string; status?: number }\n\n/** Runaway-backstop: stops an infinite tool loop where cost is unmetered. Set\n * far above any legitimate workflow — this is a watchdog, not a policy cap.\n * Legitimate per-call budgets come from `maxCostUsd` + `costOf`. */\nconst RUNAWAY_BACKSTOP_TURNS = 200\nconst DEFAULT_DECISION_CONTEXT_CHARS = 12_000\nconst FAILURE_RECOVERY_ACTIONS = ['retry', 'verify', 'continue', 'stop']\n/** Consecutive identical calls (same tool + canonical-JSON args) that trigger\n * stuck-loop detection. The window resets on any different call. */\nconst STUCK_LOOP_THRESHOLD = 3\n\n/** One OpenAI-shaped tool-call entry carried on an assistant message. */\nexport interface ToolLoopAssistantToolCall {\n id: string\n type: 'function'\n function: { name: string; arguments: string }\n}\n\n/**\n * A message in the running conversation the loop sends to `streamTurn`.\n *\n * The base `{ role, content }` covers `system` / `user` / plain `assistant`\n * turns. Two optional fields carry the OpenAI function-calling contract so a\n * strict model (Claude, and any OpenAI-compatible provider that validates tool\n * history) reads its own tool use back instead of re-issuing the same call:\n *\n * - an assistant turn that emitted tool calls carries `tool_calls`, and its\n * `content` is `null` when the turn was tool-only;\n * - each tool result is its own `{ role: 'tool', tool_call_id, content }`\n * message keyed to the call that produced it.\n *\n * Widening is additive: a `streamTurn` that reads only `role` + `content` still\n * works; one that forwards the whole message to an OpenAI-compatible endpoint\n * now sends correct tool history.\n */\nexport type ToolLoopMessage = {\n role: string\n content: string | null\n tool_calls?: ToolLoopAssistantToolCall[]\n tool_call_id?: string\n}\n\n/** A tool-call id is required to key a `role: 'tool'` result back to its call.\n * When the model omitted one, derive a stable id from the tool name so the\n * assistant `tool_calls` entry and its `tool` result still match. */\nfunction toolCallId(call: ToolLoopCall): string {\n return call.toolCallId ?? `call_${call.toolName}`\n}\n\n/** The assistant turn that emitted `pending`, in OpenAI shape: text content\n * (null when the turn was tool-only) plus its `tool_calls` array. */\nfunction assistantToolCallMessage(turnText: string, pending: ToolLoopCall[]): ToolLoopMessage {\n return {\n role: 'assistant',\n content: turnText.trim() || null,\n tool_calls: pending.map((call) => ({\n id: toolCallId(call),\n type: 'function',\n function: { name: call.toolName, arguments: JSON.stringify(call.args) },\n })),\n }\n}\n\n/** One `role: 'tool'` result message keyed to its call by `tool_call_id`. */\nfunction toolResultMessage(call: ToolLoopCall, content: string): ToolLoopMessage {\n return { role: 'tool', tool_call_id: toolCallId(call), content }\n}\n\nfunction defaultRender(label: string, outcome: ToolCallOutcome): string {\n if (outcome.ok) return `- ${label} → ok: ${JSON.stringify(outcome.result)}`\n return `- ${label} → failed (${outcome.code}): ${outcome.message}`\n}\n\n// ── Awaitable variant (drain-only callers, tests) ──────────────────────────\n\nexport type ToolLoopEvent =\n | { type: 'text'; text: string }\n | { type: 'tool_call'; call: ToolLoopCall }\n | { type: 'other'; event: unknown }\n\n/** Why the loop stopped. `completed` = model finished naturally; `stuck-loop` =\n * ≥3 consecutive identical tool calls (same tool + args); `backstop` = hit the\n * runaway-backstop cap (200 by default); `deadline` = wall-clock deadlineMs\n * exceeded; `budget` = maxCostUsd exhausted. Non-`completed` stops are infra /\n * resource outcomes — eval scoring must distinguish them from capability failure. */\nexport type ToolLoopStopReason = 'completed' | 'stuck-loop' | 'backstop' | 'deadline' | 'budget'\n\nexport interface ToolLoopResult {\n finalText: string\n toolResults: Array<{ call: ToolLoopCall; label: string; outcome: ToolCallOutcome }>\n turns: number\n stopReason: ToolLoopStopReason\n /** @deprecated Use `stopReason !== 'completed'` instead. */\n cappedOut: boolean\n}\n\nexport interface RunToolLoopOptions {\n systemPrompt: string\n userMessage: string\n priorMessages?: ToolLoopMessage[]\n streamTurn: (messages: ToolLoopMessage[]) => AsyncIterable<ToolLoopEvent>\n executeToolCall: (call: ToolLoopCall) => Promise<ToolCallOutcome>\n isExecutableTool: (toolName: string) => boolean\n /** Runaway-backstop cap. Default 200 — set far above any legitimate workflow.\n * For per-workflow limits, use `maxCostUsd` or `deadlineMs` instead. */\n maxToolTurns?: number\n /** Wall-clock deadline in ms since epoch (Date.now()-based). When exceeded the\n * loop stops with stopReason `deadline`. */\n deadlineMs?: number\n /** Maximum total cost in USD. Requires `costOf` to meter each tool call. */\n maxCostUsd?: number\n /** Return the USD cost of one outcome. Required for `maxCostUsd` to work. */\n costOf?: (call: ToolLoopCall, outcome: ToolCallOutcome) => number\n renderResult?: (label: string, outcome: ToolCallOutcome) => string\n labelFor?: (call: ToolLoopCall) => string\n runId?: string\n scenarioId?: string\n hooks?: RuntimeHooks\n}\n\n/** Run the bounded tool loop and return the final text + every executed tool\n * outcome. Awaitable — callers needing to stream events to a UI use\n * {@link streamToolLoop}. */\nexport async function runToolLoop(opts: RunToolLoopOptions): Promise<ToolLoopResult> {\n const backstop = opts.maxToolTurns ?? RUNAWAY_BACKSTOP_TURNS\n const render = opts.renderResult ?? defaultRender\n const labelFor = opts.labelFor ?? ((c: ToolLoopCall) => c.toolName)\n const runId = opts.runId ?? `agent-run-${randomSuffix()}`\n const messages: ToolLoopMessage[] = [\n { role: 'system', content: opts.systemPrompt },\n ...(opts.priorMessages ?? []),\n { role: 'user', content: opts.userMessage },\n ]\n const observer = createToolLoopObserver(opts.hooks, runId, opts.scenarioId)\n const toolResults: ToolLoopResult['toolResults'] = []\n let finalText = ''\n let turns = 0\n let accumulatedCostUsd = 0\n // Stuck-loop detection: track the last canonical-JSON call signature and how\n // many consecutive times we've seen it.\n let lastCallHash: string | null = null\n let consecutiveCount = 0\n\n observer.loopBefore(backstop, messages.length)\n\n for (let toolTurn = 0; ; toolTurn++) {\n turns++\n\n // Wall-clock deadline check — before every new turn.\n if (opts.deadlineMs !== undefined && Date.now() >= opts.deadlineMs) {\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'deadline' })\n return { finalText, toolResults, turns, stopReason: 'deadline', cappedOut: true }\n }\n\n let turnText = ''\n const pending: ToolLoopCall[] = []\n const turnEventId = observer.turnBefore(toolTurn, messages.length)\n for await (const ev of opts.streamTurn([...messages])) {\n if (ev.type === 'text') {\n turnText += ev.text\n finalText += ev.text\n } else if (ev.type === 'tool_call' && opts.isExecutableTool(ev.call.toolName)) {\n pending.push(ev.call)\n }\n }\n if (pending.length === 0) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: 0,\n finalTextChars: finalText.length,\n })\n break\n }\n\n // Runaway backstop — the model keeps emitting calls past the safety ceiling.\n if (toolTurn >= backstop) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'backstop',\n })\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'backstop' })\n return { finalText, toolResults, turns, stopReason: 'backstop', cappedOut: true }\n }\n\n // The assistant turn that emitted the calls, carrying its tool_calls array,\n // so a strict model reads its own tool use back in OpenAI shape.\n messages.push(assistantToolCallMessage(turnText, pending))\n const outcomes: ExecutedToolCall[] = []\n for (const [callIndex, call] of pending.entries()) {\n // Stuck-loop detection: hash the first pending call each turn (a model\n // stuck in a loop re-issues the same call repeatedly, not alternating calls).\n const callHash = canonicalCallHash(call)\n if (callHash === lastCallHash) {\n consecutiveCount++\n } else {\n lastCallHash = callHash\n consecutiveCount = 1\n }\n if (consecutiveCount >= STUCK_LOOP_THRESHOLD) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'stuck-loop',\n })\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'stuck-loop' })\n return { finalText, toolResults, turns, stopReason: 'stuck-loop', cappedOut: true }\n }\n\n const callEventId = observer.toolCallBefore(toolTurn, turnEventId, callIndex, call)\n let outcome: ToolCallOutcome\n try {\n outcome = await opts.executeToolCall(call)\n } catch (err) {\n outcome = {\n ok: false,\n code: 'executor_error',\n message: err instanceof Error ? err.message : String(err),\n }\n }\n\n // Budget check after each tool call.\n if (opts.maxCostUsd !== undefined && opts.costOf !== undefined) {\n accumulatedCostUsd += opts.costOf(call, outcome)\n if (accumulatedCostUsd >= opts.maxCostUsd) {\n const label = labelFor(call)\n toolResults.push({ call, label, outcome })\n messages.push(toolResultMessage(call, render(label, outcome)))\n observer.toolCallAfter(toolTurn, callEventId, call, outcome)\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'budget',\n })\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'budget' })\n return { finalText, toolResults, turns, stopReason: 'budget', cappedOut: true }\n }\n }\n\n const label = labelFor(call)\n const rendered = render(label, outcome)\n toolResults.push({ call, label, outcome })\n outcomes.push({ call, label, outcome, rendered })\n // One role:'tool' message per result, keyed to its call by tool_call_id.\n messages.push(toolResultMessage(call, rendered))\n observer.toolCallAfter(toolTurn, callEventId, call, outcome)\n }\n observer.failureRecovery({\n toolTurn,\n messages,\n turnText,\n outcomes,\n })\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n toolResults: outcomes.map((item) => ({\n toolName: item.call.toolName,\n toolCallId: item.call.toolCallId,\n ok: item.outcome.ok,\n })),\n failedToolCalls: outcomes.filter((item) => !item.outcome.ok).length,\n })\n }\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'completed' })\n return { finalText, toolResults, turns, stopReason: 'completed', cappedOut: false }\n}\n\n// ── Streaming variant (SSE chat runtimes + per-event telemetry) ────────────\n\nexport type StreamToolLoopYield<Raw> =\n | { kind: 'event'; event: Raw }\n | {\n kind: 'tool_result'\n toolName: string\n toolCallId?: string\n label: string\n outcome: ToolCallOutcome\n }\n | { kind: 'capped'; pending: number; stopReason: Exclude<ToolLoopStopReason, 'completed'> }\n\nexport interface StreamToolLoopOptions<Raw> {\n systemPrompt: string\n userMessage: string\n priorMessages?: ToolLoopMessage[]\n streamTurn: (messages: ToolLoopMessage[]) => AsyncIterable<Raw>\n extractText: (event: Raw) => string\n extractToolCall: (event: Raw) => ToolLoopCall | null\n isExecutableTool: (toolName: string) => boolean\n executeToolCall: (call: ToolLoopCall) => Promise<ToolCallOutcome>\n /** Runaway-backstop cap. Default 200 — set far above any legitimate workflow. */\n maxToolTurns?: number\n /** Wall-clock deadline in ms since epoch (Date.now()-based). */\n deadlineMs?: number\n /** Maximum total cost in USD. Requires `costOf` to meter each tool call. */\n maxCostUsd?: number\n /** Return the USD cost of one outcome. Required for `maxCostUsd` to work. */\n costOf?: (call: ToolLoopCall, outcome: ToolCallOutcome) => number\n renderResult?: (label: string, outcome: ToolCallOutcome) => string\n labelFor?: (call: ToolLoopCall) => string\n runId?: string\n scenarioId?: string\n hooks?: RuntimeHooks\n}\n\n/** Streaming bounded tool loop: yields each raw turn event (the caller maps +\n * telemetries + re-emits it) and each executed `tool_result`; emits one\n * `capped` if it stops for any non-completed reason with calls still pending. */\nexport async function* streamToolLoop<Raw>(\n opts: StreamToolLoopOptions<Raw>,\n): AsyncGenerator<StreamToolLoopYield<Raw>, void, unknown> {\n const backstop = opts.maxToolTurns ?? RUNAWAY_BACKSTOP_TURNS\n const render = opts.renderResult ?? defaultRender\n const labelFor = opts.labelFor ?? ((c: ToolLoopCall) => c.toolName)\n const runId = opts.runId ?? `agent-run-${randomSuffix()}`\n const messages: ToolLoopMessage[] = [\n { role: 'system', content: opts.systemPrompt },\n ...(opts.priorMessages ?? []),\n { role: 'user', content: opts.userMessage },\n ]\n const observer = createToolLoopObserver(opts.hooks, runId, opts.scenarioId)\n let accumulatedCostUsd = 0\n let lastCallHash: string | null = null\n let consecutiveCount = 0\n\n observer.loopBefore(backstop, messages.length)\n\n for (let toolTurn = 0; ; toolTurn++) {\n // Wall-clock deadline check before every new turn.\n if (opts.deadlineMs !== undefined && Date.now() >= opts.deadlineMs) {\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'deadline' })\n yield { kind: 'capped', pending: 0, stopReason: 'deadline' }\n return\n }\n\n let turnText = ''\n const pending: ToolLoopCall[] = []\n const turnEventId = observer.turnBefore(toolTurn, messages.length)\n for await (const event of opts.streamTurn([...messages])) {\n yield { kind: 'event', event }\n turnText += opts.extractText(event)\n const call = opts.extractToolCall(event)\n if (call && opts.isExecutableTool(call.toolName)) pending.push(call)\n }\n if (pending.length === 0) {\n observer.turnAfter(toolTurn, turnEventId, { pendingToolCalls: 0 })\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'completed' })\n return\n }\n\n // Runaway backstop.\n if (toolTurn >= backstop) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'backstop',\n })\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'backstop' })\n yield { kind: 'capped', pending: pending.length, stopReason: 'backstop' }\n return\n }\n\n // The assistant turn that emitted the calls, carrying its tool_calls array,\n // so a strict model reads its own tool use back in OpenAI shape.\n messages.push(assistantToolCallMessage(turnText, pending))\n const outcomes: ExecutedToolCall[] = []\n for (const [callIndex, call] of pending.entries()) {\n // Stuck-loop detection.\n const callHash = canonicalCallHash(call)\n if (callHash === lastCallHash) {\n consecutiveCount++\n } else {\n lastCallHash = callHash\n consecutiveCount = 1\n }\n if (consecutiveCount >= STUCK_LOOP_THRESHOLD) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'stuck-loop',\n })\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'stuck-loop' })\n yield { kind: 'capped', pending: pending.length, stopReason: 'stuck-loop' }\n return\n }\n\n const callEventId = observer.toolCallBefore(toolTurn, turnEventId, callIndex, call)\n let outcome: ToolCallOutcome\n try {\n outcome = await opts.executeToolCall(call)\n } catch (err) {\n outcome = {\n ok: false,\n code: 'executor_error',\n message: err instanceof Error ? err.message : String(err),\n }\n }\n\n // Budget check after each tool call.\n if (opts.maxCostUsd !== undefined && opts.costOf !== undefined) {\n accumulatedCostUsd += opts.costOf(call, outcome)\n if (accumulatedCostUsd >= opts.maxCostUsd) {\n const label = labelFor(call)\n yield {\n kind: 'tool_result',\n toolName: call.toolName,\n toolCallId: call.toolCallId,\n label,\n outcome,\n }\n messages.push(toolResultMessage(call, render(label, outcome)))\n observer.toolCallAfter(toolTurn, callEventId, call, outcome)\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'budget',\n })\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'budget' })\n yield { kind: 'capped', pending: pending.length, stopReason: 'budget' }\n return\n }\n }\n\n const label = labelFor(call)\n yield {\n kind: 'tool_result',\n toolName: call.toolName,\n toolCallId: call.toolCallId,\n label,\n outcome,\n }\n const rendered = render(label, outcome)\n outcomes.push({ call, label, outcome, rendered })\n // One role:'tool' message per result, keyed to its call by tool_call_id.\n messages.push(toolResultMessage(call, rendered))\n observer.toolCallAfter(toolTurn, callEventId, call, outcome)\n }\n observer.failureRecovery({\n toolTurn,\n messages,\n turnText,\n outcomes,\n })\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n toolResults: outcomes.map((item) => ({\n toolName: item.call.toolName,\n toolCallId: item.call.toolCallId,\n ok: item.outcome.ok,\n })),\n failedToolCalls: outcomes.filter((item) => !item.outcome.ok).length,\n })\n }\n}\n\ninterface ExecutedToolCall {\n call: ToolLoopCall\n label: string\n outcome: ToolCallOutcome\n rendered: string\n}\n\ninterface NotifyToolFailureRecoveryOptions {\n hooks?: RuntimeHooks\n runId: string\n scenarioId?: string\n stepIndex: number\n messages: ToolLoopMessage[]\n turnText: string\n outcomes: ExecutedToolCall[]\n}\n\ninterface NotifyToolLoopEventOptions {\n hooks?: RuntimeHooks\n runId: string\n scenarioId?: string\n target: 'agent.run' | 'agent.turn' | 'agent.tool_call'\n phase: 'before' | 'after' | 'error' | 'event'\n id?: string\n stepIndex?: number\n parentId?: string\n payload?: Record<string, unknown>\n metadata?: Record<string, unknown>\n}\n\ninterface ToolLoopObserver {\n loopBefore(maxToolTurns: number, messageCount: number): void\n loopAfter(payload: Record<string, unknown>): void\n turnBefore(toolTurn: number, messageCount: number): string\n turnAfter(toolTurn: number, turnEventId: string, payload: Record<string, unknown>): void\n toolCallBefore(\n toolTurn: number,\n turnEventId: string,\n callIndex: number,\n call: ToolLoopCall,\n ): string\n toolCallAfter(\n toolTurn: number,\n callEventId: string,\n call: ToolLoopCall,\n outcome: ToolCallOutcome,\n ): void\n failureRecovery(options: {\n toolTurn: number\n messages: ToolLoopMessage[]\n turnText: string\n outcomes: ExecutedToolCall[]\n }): void\n}\n\nfunction createToolLoopObserver(\n hooks: RuntimeHooks | undefined,\n runId: string,\n scenarioId: string | undefined,\n): ToolLoopObserver {\n const loopEventId = `${runId}:agent.run`\n return {\n loopBefore: (maxToolTurns, messageCount) => {\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.run',\n phase: 'before',\n id: `${loopEventId}:before`,\n payload: { maxToolTurns, messageCount },\n })\n },\n loopAfter: (payload) => {\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.run',\n phase: 'after',\n id: `${loopEventId}:after`,\n payload,\n })\n },\n turnBefore: (toolTurn, messageCount) => {\n const turnEventId = `${loopEventId}:${toolTurn}`\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.turn',\n phase: 'before',\n id: turnEventId,\n stepIndex: toolTurn,\n parentId: loopEventId,\n payload: { messageCount },\n })\n return turnEventId\n },\n turnAfter: (toolTurn, turnEventId, payload) => {\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.turn',\n phase: 'after',\n id: `${turnEventId}:after`,\n stepIndex: toolTurn,\n parentId: turnEventId,\n payload,\n })\n },\n toolCallBefore: (toolTurn, turnEventId, callIndex, call) => {\n const callEventId = `${turnEventId}:tool-call:${callIndex}`\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.tool_call',\n phase: 'before',\n id: callEventId,\n stepIndex: toolTurn,\n parentId: turnEventId,\n payload: toolCallPayload(call),\n })\n return callEventId\n },\n toolCallAfter: (toolTurn, callEventId, call, outcome) => {\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.tool_call',\n phase: 'after',\n id: `${callEventId}:after`,\n stepIndex: toolTurn,\n parentId: callEventId,\n payload: { ...toolCallPayload(call), outcome: outcomePayload(outcome) },\n })\n },\n failureRecovery: (options) => {\n notifyToolFailureRecovery({\n hooks,\n runId,\n scenarioId,\n stepIndex: options.toolTurn,\n messages: options.messages,\n turnText: options.turnText,\n outcomes: options.outcomes,\n })\n },\n }\n}\n\nfunction notifyToolLoopEvent(options: NotifyToolLoopEventOptions): void {\n notifyRuntimeHookEvent(options.hooks, {\n id: options.id ?? `${options.runId}:${options.target}:${options.phase}`,\n runId: options.runId,\n scenarioId: options.scenarioId,\n target: options.target,\n phase: options.phase,\n timestamp: Date.now(),\n stepIndex: options.stepIndex,\n parentId: options.parentId,\n payload: options.payload,\n metadata: { producer: 'tool-loop', ...options.metadata },\n })\n}\n\nfunction notifyToolFailureRecovery(options: NotifyToolFailureRecoveryOptions): void {\n const failed = options.outcomes.filter((item) => !item.outcome.ok)\n if (failed.length === 0) return\n\n const evidence: RuntimeDecisionEvidenceRef[] = []\n for (const item of failed) {\n const id = item.call.toolCallId ?? `${options.stepIndex}:${item.label}`\n evidence.push({\n source: 'tool_call',\n id,\n detail: `${item.call.toolName} ${stringifySafe(item.call.args, 2_000)}`,\n metadata: { toolName: item.call.toolName, label: item.label },\n })\n evidence.push({\n source: 'tool_result',\n id: `${id}:result`,\n detail: item.rendered,\n metadata: failureMetadata(item.outcome),\n })\n }\n\n notifyRuntimeDecisionPoint(options.hooks, {\n id: `${options.runId}:agent.turn:${options.stepIndex}:failure-recovery`,\n runId: options.runId,\n scenarioId: options.scenarioId,\n stepIndex: options.stepIndex,\n kind: 'retry',\n candidateActions: [...FAILURE_RECOVERY_ACTIONS],\n context: renderDecisionContext(options.messages, options.turnText, options.outcomes),\n evidence,\n metadata: {\n target: 'failure-recovery',\n source: 'agent.turn',\n failedToolCount: failed.length,\n toolNames: failed.map((item) => item.call.toolName),\n },\n })\n}\n\nfunction toolCallPayload(call: ToolLoopCall): Record<string, unknown> {\n return {\n toolName: call.toolName,\n toolCallId: call.toolCallId,\n argsPreview: stringifySafe(call.args, 2_000),\n }\n}\n\nfunction outcomePayload(outcome: ToolCallOutcome): Record<string, unknown> {\n if (!outcome.ok) {\n return {\n ok: false,\n code: outcome.code,\n message: trimText(outcome.message, 2_000),\n status: outcome.status,\n }\n }\n return {\n ok: true,\n resultPreview: stringifySafe(outcome.result, 2_000),\n }\n}\n\nfunction failureMetadata(outcome: ToolCallOutcome): Record<string, unknown> | undefined {\n if (outcome.ok) return undefined\n return {\n code: outcome.code,\n message: outcome.message,\n status: outcome.status,\n }\n}\n\nfunction renderDecisionContext(\n messages: ToolLoopMessage[],\n turnText: string,\n outcomes: ExecutedToolCall[],\n): string {\n const recent = messages.slice(-6).map((message) => `[${message.role}]\\n${message.content ?? ''}`)\n const assistant = turnText.trim() ? [`[assistant]\\n${turnText}`] : []\n const toolResults = [`[tool results]\\n${outcomes.map((item) => item.rendered).join('\\n')}`]\n return trimText(\n [...recent, ...assistant, ...toolResults].join('\\n\\n'),\n DEFAULT_DECISION_CONTEXT_CHARS,\n )\n}\n\n/** Canonical identifier for a tool call used by stuck-loop detection.\n * Keys are sorted so `{b:1,a:2}` and `{a:2,b:1}` produce the same hash. */\nfunction canonicalCallHash(call: ToolLoopCall): string {\n const sortedArgs = Object.fromEntries(\n Object.entries(call.args).sort(([a], [b]) => a.localeCompare(b)),\n )\n return `${call.toolName}:${JSON.stringify(sortedArgs)}`\n}\n\nfunction stringifySafe(value: unknown, max: number): string {\n let text: string\n try {\n text = JSON.stringify(value) ?? String(value)\n } catch {\n text = String(value)\n }\n return trimText(text, max)\n}\n\nfunction trimText(text: string, max: number): string {\n if (text.length <= max) return text\n return `${text.slice(0, max)}…`\n}\n\nfunction randomSuffix(len = 8): string {\n return Math.random()\n .toString(36)\n .slice(2, 2 + len)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AC,GAAG,CAAC,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;AAAA,EACjE;AACA,SAAO,GAAG,KAAK,QAAQ,IAAI,KAAK,UAAU,UAAU,CAAC;AACvD;AAEA,SAAS,cAAc,OAAgB,KAAqB;AAC1D,MAAI;AACJ,MAAI;AACF,WAAO,KAAK,UAAU,KAAK,KAAK,OAAO,KAAK;AAAA,EAC9C,QAAQ;AACN,WAAO,OAAO,KAAK;AAAA,EACrB;AACA,SAAO,SAAS,MAAM,GAAG;AAC3B;AAEA,SAAS,SAAS,MAAc,KAAqB;AACnD,MAAI,KAAK,UAAU,IAAK,QAAO;AAC/B,SAAO,GAAG,KAAK,MAAM,GAAG,GAAG,CAAC;AAC9B;AAEA,SAASD,cAAa,MAAM,GAAW;AACrC,SAAO,KAAK,OAAO,EAChB,SAAS,EAAE,EACX,MAAM,GAAG,IAAI,GAAG;AACrB;","names":["emit","readFileSync","resolve","resolve","readFileSync","spawnSync","randomSuffix","label"]}
1
+ {"version":3,"sources":["../src/durable/chat-engine.ts","../src/durable/execution-handle.ts","../src/improvement/build-prompts.ts","../src/improvement/campaign-otlp.ts","../src/improvement/driver-loop-generator.ts","../src/improvement/mcp-serve-verifier.ts","../src/improvement/profile-diff-proposer.ts","../src/improvement/reflective-generator.ts","../src/readiness.ts","../src/resolve-agent-backend.ts","../src/run.ts","../src/runtime-run.ts","../src/sse.ts","../src/tool-loop.ts"],"sourcesContent":["/**\n * `handleChatTurn` — framework-neutral chat-turn HTTP orchestrator.\n * Owns the NDJSON `ChatStreamEvent` line protocol, the `session.run.*`\n * lifecycle vocabulary, and the persist / post-process / trace-flush\n * hook order. Returns a `ReadableStream` body the product hands to its\n * platform `Response`.\n *\n * Execution durability is the substrate's concern: `box.streamPrompt`\n * auto-reconnects in-call; cross-process reconnect via `X-Execution-ID`\n * is the product's job. The producer this engine wraps already speaks\n * that protocol — the engine just frames the events.\n *\n * Hooks (`ChatTurnHooks`):\n * - `produce` — build the backend event stream\n * - `persistAssistantMessage` — write the assistant turn to the product DB\n * - `onTurnComplete?` — post-process (proposals, citations, …)\n * - `onEvent?` — per-event side channel (e.g. DO broadcast)\n * - `transformFinalText?` — pre-persist transform (e.g. PII redact)\n * - `traceFlush?` — handed to waitUntil so OTLP export lands\n *\n * Framework neutrality: takes already-resolved values (`identity` tuple,\n * a `waitUntil`), never a `Request` or a `Context`. The product's thin\n * route adapter does auth + parse + access-control, then calls\n * `handleChatTurn(...)` and returns `result.body` as its platform `Response`.\n */\n\n/** The NDJSON line protocol every product chat client already speaks. */\nexport interface ChatStreamEvent {\n type: string\n data?: Record<string, unknown>\n}\n\n/** Identity of a chat turn. `tenantId` is the workspace id for workspace-\n * scoped products and the user id for session-scoped products. */\nexport interface ChatTurnIdentity {\n tenantId: string\n /** Thread / session id. */\n sessionId: string\n userId: string\n /** Monotonic 0-based turn index within the session. */\n turnIndex: number\n}\n\n/** The live side of a turn — what the product's `produce` hook returns. */\nexport interface ChatTurnProducer<TEvent extends ChatStreamEvent = ChatStreamEvent> {\n /** The turn's event stream. Forwarded verbatim to the caller. */\n stream: AsyncGenerator<TEvent, void, unknown>\n /** The turn's final assistant text. Read once, after `stream` drains. */\n finalText(): string\n}\n\nexport interface ChatTurnHooks {\n /** Build the backend stream. The engine forwards events verbatim and\n * reads `finalText()` once the stream drains. */\n produce(): ChatTurnProducer\n /** Persist the assistant message to the product's own store. Called\n * once, after drain, with the assembled (transform-applied) text. */\n persistAssistantMessage(input: { identity: ChatTurnIdentity; finalText: string }): Promise<void>\n /** Optional post-processing (proposals, citations, credit metering …).\n * Errors are swallowed + logged — post-process must never fail a turn\n * that already streamed successfully. */\n onTurnComplete?(input: { identity: ChatTurnIdentity; finalText: string }): Promise<void>\n /** Optional per-event side channel (e.g. DO broadcast). Runs for every\n * emitted event, lifecycle envelope included. Errors swallowed — a\n * broadcast failure must not break the chat stream. */\n onEvent?(event: ChatStreamEvent): void | Promise<void>\n /** Optional pre-persist transform of the final text (e.g. PII\n * redaction). Affects only what is persisted; the live stream is\n * never altered. */\n transformFinalText?(text: string): string | Promise<string>\n /** Optional trace flush — resolves when OTLP export completes. Handed\n * to `waitUntil` so the worker isolate stays alive for the POST. */\n traceFlush?(): Promise<void>\n}\n\nexport interface RunChatTurnInput {\n identity: ChatTurnIdentity\n hooks: ChatTurnHooks\n /** Worker liveness hook. When omitted, trace flush is awaited inline\n * before the stream closes. */\n waitUntil?: (p: Promise<unknown>) => void\n /** Structured logger for swallowed hook errors. Defaults to\n * `console.error` so failures surface without product wiring. */\n log?: (message: string, meta?: Record<string, unknown>) => void\n}\n\nexport interface ChatTurnResult {\n /** NDJSON body — return this as the platform `Response` body. */\n body: ReadableStream<Uint8Array>\n /** Content type for the response. */\n contentType: 'application/x-ndjson'\n}\n\nconst encoder = new TextEncoder()\n\nfunction encodeLine(event: ChatStreamEvent): Uint8Array {\n return encoder.encode(`${JSON.stringify(event)}\\n`)\n}\n\nfunction defaultLog(message: string, meta?: Record<string, unknown>): void {\n if (meta) console.error(message, meta)\n else console.error(message)\n}\n\n/**\n * Run one chat turn. Returns immediately with a `ReadableStream` body;\n * the turn executes as the body is pulled. Never rejects — backend\n * failures surface as `error` + `session.run.failed` events.\n */\nexport function handleChatTurn(input: RunChatTurnInput): ChatTurnResult {\n const log = input.log ?? defaultLog\n const { identity, hooks } = input\n\n const body = new ReadableStream<Uint8Array>({\n start: async (controller) => {\n const emit = async (event: ChatStreamEvent): Promise<void> => {\n controller.enqueue(encodeLine(event))\n if (hooks.onEvent) {\n try {\n await hooks.onEvent(event)\n } catch (err) {\n log('[chat-engine] onEvent hook threw', {\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n }\n\n try {\n await emit({\n type: 'session.run.started',\n data: {\n sessionId: identity.sessionId,\n tenantId: identity.tenantId,\n turnIndex: identity.turnIndex,\n },\n })\n\n const producer = hooks.produce()\n for await (const event of producer.stream) {\n await emit(event)\n }\n const rawFinal = producer.finalText()\n const finalText = hooks.transformFinalText\n ? await hooks.transformFinalText(rawFinal)\n : rawFinal\n\n await hooks.persistAssistantMessage({ identity, finalText })\n if (hooks.onTurnComplete) {\n try {\n await hooks.onTurnComplete({ identity, finalText })\n } catch (err) {\n log('[chat-engine] onTurnComplete threw', {\n error: err instanceof Error ? err.message : String(err),\n })\n }\n }\n\n await emit({\n type: 'session.run.completed',\n data: { sessionId: identity.sessionId },\n })\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err)\n log('[chat-engine] turn failed', { error: message })\n await emit({ type: 'error', data: { message } })\n await emit({\n type: 'session.run.failed',\n data: { sessionId: identity.sessionId, message },\n })\n } finally {\n if (hooks.traceFlush) {\n const flush = hooks.traceFlush().catch((err) =>\n log('[chat-engine] traceFlush threw', {\n error: err instanceof Error ? err.message : String(err),\n }),\n )\n if (input.waitUntil) input.waitUntil(flush)\n else await flush\n }\n controller.close()\n }\n },\n })\n\n return { body, contentType: 'application/x-ndjson' }\n}\n","/**\n * Derive a stable executionId from the run identity. The same\n * `(projectId, sessionId, turnIndex)` tuple yields the same id — so a\n * client retry of the same turn lands on the same substrate execution\n * and the orchestrator's buffer replays instead of starting a second\n * prompt.\n *\n * Format is readable, not hashed: operators grepping orchestrator logs\n * for `gtm-agent:thread-abc:3` find the run without translating an\n * opaque id. Substrate executionIds are not a secrecy boundary.\n *\n * Wire integration:\n * - Sandbox PromptOptions accepts `executionId` and `lastEventId`.\n * Products pass this id to make cross-process reconnect land on the\n * same substrate execution instead of spawning a duplicate run.\n */\nexport function deriveExecutionId(input: {\n projectId: string\n sessionId: string\n turnIndex: number\n}): string {\n return `${input.projectId}:${input.sessionId}:${input.turnIndex}`\n}\n","/**\n * Build-prompt starting points for the two buildable artifact types. There is\n * NO `toolGenerator`/`mcpGenerator` wrapper — the factory is `agenticGenerator`\n * + a verifier (docs/artifact-lifecycle-frontier.md), so a tool or an MCP\n * server is built by composing the pieces directly:\n *\n * // a tool:\n * agenticGenerator({ buildPrompt: toolBuildPrompt, verify: commandVerifier('pnpm', ['test']) })\n * // an MCP server:\n * agenticGenerator({ buildPrompt: mcpBuildPrompt, verify: mcpServeVerifier({ command: 'node', args: ['server.mjs'] }) })\n *\n * These are the only type-specific bit (the phrasing that points the agent at a\n * tool vs. an MCP); the worktree, resume-on-failure loop, and improvement-loop\n * wrapper are shared. MCP is the load-bearing target — it is how a harness\n * acquires tools; raw tools matter where we control the loader.\n */\n\nimport type { AnalystFinding } from '@tangle-network/agent-eval'\nimport { optimizerMethod } from './optimizer-prompt'\n\ntype FindingsArg = { report: unknown; findings: AnalystFinding[] }\n\n/** Render findings as the ranked-evidence block every build prompt ends with. */\nexport function findingLines(findings: AnalystFinding[]): string[] {\n return findings.map((f) => {\n const where = f.subject ? ` [${f.subject}]` : ''\n const action = f.recommended_action ? ` → ${f.recommended_action}` : ''\n return `- (${f.severity})${where} ${f.claim}${action}`\n })\n}\n\n/** Build the starting instruction for a coder agent tasked with implementing a new tool. */\nexport function toolBuildPrompt(args: FindingsArg): string {\n return [\n 'You are building a new TOOL for this codebase — a capability the agent measurably lacks,',\n 'evidenced by the failure findings at the bottom. The tool is an experiment: after it is',\n 'built and verified, its marginal lift is measured on held-out tasks, and only a real lift',\n 'promotes it.',\n '',\n optimizerMethod,\n '',\n 'THE SURFACE — what a deliverable tool looks like here:',\n '- ONE small, self-contained module PLUS tests that exercise its contract (what callers rely',\n ' on), not its internals. The tests are the experiment for sub-goal correctness — write the',\n ' test that would fail if your hypothesis about the gap were wrong.',\n '- It must compile and its tests must pass — they run automatically; on failure you get the',\n ' verifier output and another attempt, resuming on top of your own edits (fix in place, do',\n ' not start over).',\n '- Match the codebase grain: reuse its existing helpers, style, and test framework; a tool',\n ' that fights the codebase is the wrong tool even if it passes.',\n '- Do not commit; leave the changes in the working tree.',\n '',\n 'FINDINGS — ranked evidence from real failed runs (the gaps the tool must close):',\n ...findingLines(args.findings),\n ].join('\\n')\n}\n\n/** Build the starting instruction for a coder agent tasked with implementing a new MCP server. */\nexport function mcpBuildPrompt(args: FindingsArg): string {\n return [\n 'You are building a new MCP SERVER (Model Context Protocol) exposing tool(s) that close the',\n 'capability gaps evidenced by the failure findings at the bottom, so any harness can mount',\n 'them. The server is an experiment: after it is built and boot-verified, its marginal lift is',\n 'measured on held-out tasks, and only a real lift promotes it.',\n '',\n optimizerMethod,\n '',\n 'RESEARCH FIRST — ADOPT BEFORE BUILD: you may discover and ADOPT an existing external MCP',\n 'server if it fits the gaps better than building one. Registries and vendor docs list',\n 'maintained servers for most common capabilities (web search, fetch, GitHub, filesystems,',\n 'databases). To adopt, deliver a short adoption note instead of an implementation: the',\n \"server's launch command or HTTP endpoint, and the API key it needs BY NAME (e.g.\",\n 'EXA_API_KEY) — never a key value; provisioning injects the value at materialize time. If',\n 'your environment has no web access, decide from what you already know and say so.',\n '',\n 'THE SURFACE — what a deliverable MCP server looks like here (checked by BOOTING it):',\n '- it starts over stdio and answers the MCP `initialize` handshake,',\n '- `tools/list` returns at least one tool with a valid input schema,',\n '- newline-delimited JSON-RPC 2.0, protocol version 2024-11-05,',\n '- a clear start command (a package.json `start` script or an obvious entrypoint).',\n 'Design the tool surface for the FINDINGS, not for generality: each exposed tool should map to',\n 'a named failure mechanism, with a description that tells the agent when to reach for it (a',\n 'tool the agent never calls measures zero). If the boot-and-probe fails you get the error and',\n 'another attempt, resuming on top of your own edits. Do not commit; leave the changes in the',\n 'working tree.',\n '',\n 'FINDINGS — ranked evidence from real failed runs (the capabilities the server must provide):',\n ...findingLines(args.findings),\n ].join('\\n')\n}\n","/**\n * Campaign `spans.jsonl` → OTLP-flat JSONL — the missing converter between\n * what the substrate RECORDS and what its trace analysts READ.\n *\n * agent-eval's `runCampaign` durably records one `spans.jsonl` per cell\n * (`defaultBuildTraceWriter`): flat records\n * `{ name, cellId, startMs, durationMs?, ...attributes }` with no trace/span\n * ids. Its trace consumers (`OtlpFileTraceStore`, the trace-analyst registry,\n * `haloProposer`/`traceAnalystProposer` via `resolveTraces`) read OTLP-flat\n * JSONL (`trace_id`/`span_id`/ISO times/status/attributes — the shape\n * `projectOtlpFlatLine` parses). Nothing shipped converts between the two, so\n * the traces real optimization runs write could never reach the trace-native\n * proposers. This module is that wire adapter:\n *\n * - {@link campaignCellSpansToOtlp} — one cell's `spans.jsonl` content →\n * OTLP lines (a per-cell root AGENT anchor span + one child per record).\n * - {@link convertCampaignDirToOtlp} — walk any campaign run/generation dir\n * for `spans.jsonl` files and concatenate their OTLP lines.\n * - {@link campaignTraceResolver} — the `resolveTraces` implementation for\n * `traceAnalystProposer`/`haloProposer`: proposing generation g reads the\n * traces the loop just recorded (`gen-<g-1>`, or `baseline` for g = 0)\n * under the same `runDir` handed to `improve()`/`selfImprove()`.\n *\n * Trace identity: one trace per CELL, keyed on the cell's on-disk path — the\n * same sanitized `cellId` recurs across the baseline and every candidate\n * campaign, so folding the id alone would merge distinct runs into one trace.\n * Ids are deterministic FNV-1a folds to OTLP's 32/16-hex width, so re-converts\n * are stable and byte-identical.\n */\n\nimport { type Dirent, readdirSync, readFileSync } from 'node:fs'\nimport { basename, dirname, join, resolve } from 'node:path'\nimport type { ProposeContext } from '@tangle-network/agent-eval/campaign'\nimport { OPENINFERENCE_SPAN_KIND } from '@tangle-network/agent-eval/traces'\n\n/** How deep {@link convertCampaignDirToOtlp} recurses looking for\n * `spans.jsonl`. The deepest shipped layout is\n * `<runDir>/gen-<g>/candidate-<i>/<cell>/spans.jsonl` (3 levels); one spare\n * level tolerates a wrapper dir without letting a mis-pointed root scan a\n * whole filesystem. */\nconst MAX_WALK_DEPTH = 4\n\nexport interface CampaignOtlpOptions {\n /** OTLP `service.name` on every emitted span. Default `'campaign'`. */\n serviceName?: string\n}\n\ninterface CampaignSpanRecord {\n name: string\n cellId: string\n startMs: number\n durationMs?: number\n attributes: Record<string, unknown>\n error?: string\n}\n\n/**\n * Convert ONE cell's `spans.jsonl` content to OTLP-flat JSONL lines.\n * `cellKey` is the identity the trace id folds from — pass the cell's on-disk\n * path (unique per campaign); `cellId` is the display/attribute label.\n * Returns `[]` for empty/recordless content (a dispatch that never touched\n * `ctx.trace`/`ctx.cost` writes an empty file — that is data, not an error).\n */\nexport function campaignCellSpansToOtlp(\n content: string,\n cell: { cellId: string; cellKey?: string },\n opts: CampaignOtlpOptions = {},\n): string[] {\n const records = parseCampaignSpans(content, cell.cellId)\n if (records.length === 0) return []\n\n const serviceName = opts.serviceName ?? 'campaign'\n const key = cell.cellKey ?? cell.cellId\n const traceId = foldTo32Hex(key)\n const rootSpanId = foldTo16Hex(`${key}::root`)\n\n const startMs = Math.min(...records.map((r) => r.startMs))\n const endMs = Math.max(...records.map((r) => r.startMs + (r.durationMs ?? 0)))\n const anyError = records.some((r) => r.error !== undefined)\n\n const resource = {\n attributes: {\n 'service.name': serviceName,\n 'campaign.cell_id': cell.cellId,\n ...(cell.cellKey ? { 'campaign.cell_dir': cell.cellKey } : {}),\n },\n }\n\n const lines: string[] = [\n JSON.stringify({\n trace_id: traceId,\n span_id: rootSpanId,\n parent_span_id: '',\n name: `cell.${cell.cellId}`,\n start_time: msToIso(startMs),\n end_time: msToIso(endMs),\n status: anyError\n ? { code: 'STATUS_CODE_ERROR', message: 'one or more spans errored' }\n : { code: 'STATUS_CODE_OK', message: '' },\n resource,\n attributes: {\n [OPENINFERENCE_SPAN_KIND]: 'AGENT',\n 'agent.name': cell.cellId,\n },\n }),\n ]\n\n for (let i = 0; i < records.length; i++) {\n const r = records[i]!\n lines.push(\n JSON.stringify({\n trace_id: traceId,\n span_id: foldTo16Hex(`${key}::${i}`),\n parent_span_id: rootSpanId,\n name: r.name,\n start_time: msToIso(r.startMs),\n end_time: msToIso(r.startMs + (r.durationMs ?? 0)),\n status:\n r.error === undefined\n ? { code: 'STATUS_CODE_OK', message: '' }\n : { code: 'STATUS_CODE_ERROR', message: r.error },\n resource,\n attributes: r.attributes,\n }),\n )\n }\n return lines\n}\n\n/**\n * Walk `dir` (a campaign run dir, a generation dir, or a whole `selfImprove`\n * run root) for `spans.jsonl` files and return their concatenated OTLP-flat\n * JSONL — the exact string the `resolveTraces` contract expects. `''` when no\n * spans exist (the proposers fail loud on empty by design).\n */\nexport function convertCampaignDirToOtlp(dir: string, opts: CampaignOtlpOptions = {}): string {\n const root = resolve(dir)\n const files = findSpansFiles(root, MAX_WALK_DEPTH)\n const lines: string[] = []\n for (const file of files) {\n let content: string\n try {\n content = readFileSync(file, 'utf8')\n } catch {\n continue\n }\n const cellDir = dirname(file)\n lines.push(\n ...campaignCellSpansToOtlp(content, { cellId: basename(cellDir), cellKey: cellDir }, opts),\n )\n }\n return lines.length > 0 ? `${lines.join('\\n')}\\n` : ''\n}\n\nexport interface CampaignTraceResolverOptions extends CampaignOtlpOptions {\n /** The `selfImprove`/`improve()` run root — the SAME `runDir` the loop\n * records under (`<runDir>/baseline/...`, `<runDir>/gen-<g>/candidate-<i>/...`).\n * Must be a real path; a `mem://` run records nothing to resolve. */\n runDir: string\n}\n\n/**\n * Build the `resolveTraces` function `traceAnalystProposer`/`haloProposer`\n * take: proposing generation g reads the traces of the campaigns the loop just\n * scored — `gen-<g-1>` (or `baseline` when g = 0), falling back to every trace\n * under the run root when that directory has none (e.g. a caller pointing at a\n * single campaign dir rather than a loop root).\n *\n * traceAnalystProposer({ ..., resolveTraces: campaignTraceResolver({ runDir }) })\n */\nexport function campaignTraceResolver(\n opts: CampaignTraceResolverOptions,\n): (ctx: Pick<ProposeContext, 'generation'>) => string {\n return (ctx) => {\n const priorDir =\n ctx.generation <= 0\n ? join(opts.runDir, 'baseline')\n : join(opts.runDir, `gen-${ctx.generation - 1}`)\n const prior = convertCampaignDirToOtlp(priorDir, opts)\n if (prior) return prior\n return convertCampaignDirToOtlp(opts.runDir, opts)\n }\n}\n\n/** Parse `spans.jsonl` content into records, splitting the trace writer's\n * reserved fields from the free-form attributes. Malformed lines are skipped\n * (one torn write must not censor the rest of the cell). */\nfunction parseCampaignSpans(content: string, cellId: string): CampaignSpanRecord[] {\n const out: CampaignSpanRecord[] = []\n for (const line of content.split('\\n')) {\n const trimmed = line.trim()\n if (!trimmed) continue\n let raw: unknown\n try {\n raw = JSON.parse(trimmed)\n } catch {\n continue\n }\n if (!raw || typeof raw !== 'object' || Array.isArray(raw)) continue\n const o = raw as Record<string, unknown>\n if (typeof o.name !== 'string' || typeof o.startMs !== 'number') continue\n\n const attributes: Record<string, unknown> = { 'campaign.cell_id': cellId }\n for (const [k, v] of Object.entries(o)) {\n if (k === 'name' || k === 'cellId' || k === 'startMs' || k === 'durationMs') continue\n attributes[k] = v\n }\n out.push({\n name: o.name,\n cellId,\n startMs: o.startMs,\n ...(typeof o.durationMs === 'number' ? { durationMs: o.durationMs } : {}),\n attributes,\n ...(typeof o.error === 'string' ? { error: o.error } : {}),\n })\n }\n return out\n}\n\n/** All `spans.jsonl` files under `dir`, depth-bounded, symlink-dirs skipped\n * (a cycle under a run dir must not hang the proposal round). Sorted for a\n * deterministic concatenation order. */\nfunction findSpansFiles(dir: string, depth: number): string[] {\n const out: string[] = []\n let entries: Dirent[]\n try {\n entries = readdirSync(dir, { withFileTypes: true })\n } catch {\n return out\n }\n for (const entry of entries) {\n const full = join(dir, entry.name)\n if (entry.isFile() && entry.name === 'spans.jsonl') {\n out.push(full)\n } else if (depth > 0 && entry.isDirectory() && !entry.isSymbolicLink()) {\n out.push(...findSpansFiles(full, depth - 1))\n }\n }\n return out.sort()\n}\n\nfunction msToIso(ms: number): string {\n return Number.isFinite(ms) && ms > 0 ? new Date(ms).toISOString() : new Date(0).toISOString()\n}\n\n/** Deterministic FNV-1a fold to OTLP's 16-hex span-id width. */\nfunction foldTo16Hex(s: string): string {\n const a = fnv1a(s)\n const b = fnv1a(`${s}::salt`)\n return a + b\n}\n\n/** Deterministic FNV-1a fold to OTLP's 32-hex trace-id width. */\nfunction foldTo32Hex(s: string): string {\n return foldTo16Hex(s) + foldTo16Hex(`${s}::trace`)\n}\n\nfunction fnv1a(s: string): string {\n let h = 0x811c9dc5\n for (let i = 0; i < s.length; i++) {\n h ^= s.charCodeAt(i)\n h = Math.imul(h, 0x01000193) >>> 0\n }\n return h.toString(16).padStart(8, '0')\n}\n","/**\n * `driverLoopGenerator` — the driver→worker `CandidateGenerator`: the build\n * loop run by the ATOM instead of the canned respawn.\n *\n * `agenticGenerator` steers with three hardcoded conditions picking a canned\n * note (`EMPTY_TREE_NOTE` / `failureNote`) and respawns. This generator swaps\n * that respawn brain for a real driver: an LLM on the canonical tool-loop seam\n * (`runBrainLoop` + `ToolLoopChat` — the exact loop `driverAgent` runs its\n * brain on) that AUTHORS each worker instruction, OBSERVES what the session\n * actually produced (diff, files, verifier output), RATES it, and DECIDES\n * refine / re-scope / decompose — prompted with the senior scientific-method\n * doctrine (`buildDriverSystem`).\n *\n * The worker stays the proven primitive: `runLocalHarness` in the candidate\n * worktree, same as `agenticGenerator` — only the brain between sessions\n * changes. The worktree machinery (`worktreeBuildCandidate`) and verifiers\n * (`commandVerifier` / `mcpServeVerifier`) are reused verbatim.\n *\n * Completion-oracle invariant (the supervisor doctrine, kept): the driver's\n * prose NEVER decides the outcome. After the loop, code re-checks ground\n * truth — tree dirty, raw-trace evidence present, verifier green — and only\n * that decides `applied`. A driver that claims success over a failing verifier\n * produces a discarded candidate, not a shipped one.\n *\n * @experimental\n */\n\nimport { spawnSync } from 'node:child_process'\nimport { readFileSync, statSync } from 'node:fs'\nimport { resolve, sep } from 'node:path'\nimport type { AnalystFinding } from '@tangle-network/agent-eval'\nimport { type LocalHarness, runLocalHarness } from '../mcp/local-harness'\nimport { runBrainLoop, type ToolLoopChat } from '../runtime/tool-loop'\nimport {\n defaultBuildPrompt,\n rawTraceEvidenceProblem,\n requiresRawTraceEvidence,\n summarizeFindings,\n type Verifier,\n worktreeChangedPaths,\n} from './agentic-generator'\nimport type { CandidateGenerator } from './improvement-driver'\nimport { buildDriverSystem, researchDriverNote } from './optimizer-prompt'\n\nexport interface DriverLoopGeneratorOptions {\n /** The driver-LLM seam — ONE inference turn over the conversation + tool specs (the canonical\n * `ToolLoopChat`, same seam as `driverAgent`): `routerBrain(cfg)` in production, a scripted\n * mock in tests. */\n brain: ToolLoopChat\n /** Local coding harness the driver's worker sessions run in the worktree. Default `claude`. */\n harness?: LocalHarness\n /** Per-worker-session wall-clock timeout (ms). Default = `runLocalHarness` default (5m). */\n timeoutMs?: number\n /** Build the driver's task briefing (domain framing + method + findings) — the same senior\n * prompt the worker path uses (`toolBuildPrompt` / `mcpBuildPrompt`). The driver reads it and\n * folds what each worker needs into its instruction. Default `defaultBuildPrompt`. */\n buildPrompt?: (args: { report: unknown; findings: AnalystFinding[] }) => string\n /** Verify the worktree (the intrinsic check). Exposed to the driver as `run_verifier` AND\n * re-run by code as the final keep/discard gate. Omitted ⇒ the final gate is dirty-tree only\n * (legacy `agenticGenerator` behavior sans verifier). */\n verify?: Verifier\n /** Max driver inference turns. Default `max(8, 2 + maxShots * 3)` — room for one\n * observe/rate/decide cycle per worker session plus orientation. */\n maxTurns?: number\n /** The research seam (adopt-not-build): when set, the driver gets a\n * `research{query}` tool + the `researchDriverNote` doctrine, so it can\n * discover an EXISTING external MCP instead of building one. Wire a real\n * web/search backend here — none is provisioned by default (the build\n * harness has no live web access yet; flagged). */\n research?: (query: string) => Promise<string>\n /** Test seam — inject the harness runner (defaults to `runLocalHarness`). */\n runHarness?: typeof runLocalHarness\n /** Test seam — inject the worktree diff reader (defaults to `git diff` in the worktree). */\n readDiff?: (worktreePath: string) => string\n /** Test seam — inject the changed-paths reader (defaults to `git status --porcelain`). */\n changedPaths?: (worktreePath: string) => string[]\n}\n\nconst workerOutputTailChars = 2_000\nconst diffMaxChars = 6_000\nconst readFileDefaultBytes = 8_192\nconst researchResultMaxChars = 8_000\n\n/** Driver→worker `CandidateGenerator`: an LLM driver on the canonical tool-loop authors, observes, rates, and steers coding-harness sessions in the worktree until the verifier passes or the session budget is spent. */\nexport function driverLoopGenerator(opts: DriverLoopGeneratorOptions): CandidateGenerator {\n const harness = opts.harness ?? 'claude'\n const buildPrompt = opts.buildPrompt ?? defaultBuildPrompt\n const run = opts.runHarness ?? runLocalHarness\n const changed = opts.changedPaths ?? worktreeChangedPaths\n const readDiff = opts.readDiff ?? worktreeDiff\n const verify = opts.verify\n\n return {\n kind: `driver-loop:${harness}`,\n async generate({ worktreePath, report, findings, maxShots, signal }) {\n signal.throwIfAborted()\n const briefing = buildPrompt({ report, findings })\n const needsRawTraceEvidence = requiresRawTraceEvidence(findings)\n const sessionCap = Math.max(1, maxShots)\n let sessionsUsed = 0\n\n // Ground-truth verification shared by the driver's `run_verifier` tool and the final\n // code-owned gate — ONE definition of \"delivered\" so the driver can never see a different\n // check than the one that decides the keep.\n const groundVerify = async (): Promise<{ ok: boolean; feedback?: string }> => {\n signal.throwIfAborted()\n if (changed(worktreePath).length === 0) {\n return { ok: false, feedback: 'the working tree has no changes — nothing to verify' }\n }\n if (needsRawTraceEvidence) {\n const problem = rawTraceEvidenceProblem(worktreePath, findings)\n if (problem) return { ok: false, feedback: problem }\n }\n if (!verify) {\n return { ok: true, feedback: 'no verifier configured: a dirty tree is the candidate' }\n }\n const result = await verify(worktreePath, signal)\n signal.throwIfAborted()\n return result\n }\n\n const execute = async (name: string, args: Record<string, unknown>): Promise<string> => {\n signal.throwIfAborted()\n switch (name) {\n case 'run_worker': {\n const instruction = typeof args.instruction === 'string' ? args.instruction.trim() : ''\n if (instruction.length === 0) {\n return 'error: run_worker requires a non-empty `instruction`'\n }\n if (sessionsUsed >= sessionCap) {\n return `error: worker-session budget exhausted (${sessionsUsed}/${sessionCap} used). Inspect and verify what exists, then stop with your final assessment.`\n }\n sessionsUsed += 1\n const result = await run({\n harness,\n cwd: worktreePath,\n taskPrompt: instruction,\n ...(opts.timeoutMs !== undefined ? { timeoutMs: opts.timeoutMs } : {}),\n signal,\n })\n signal.throwIfAborted()\n if (result.aborted) {\n throw new Error('driverLoopGenerator: worker session was cancelled by the caller')\n }\n return JSON.stringify({\n session: `${sessionsUsed}/${sessionCap}`,\n exitCode: result.exitCode,\n timedOut: result.timedOut,\n aborted: result.aborted ?? false,\n killedBySignal: result.killedBySignal,\n durationMs: result.durationMs,\n changedPaths: changed(worktreePath),\n stdoutTail: tail(result.stdout, workerOutputTailChars),\n stderrTail: tail(result.stderr, workerOutputTailChars),\n })\n }\n case 'inspect_worktree': {\n const paths = changed(worktreePath)\n const diff = truncate(readDiff(worktreePath), diffMaxChars)\n return JSON.stringify({\n changedPaths: paths,\n diff:\n diff.length > 0\n ? diff\n : '(no tracked-file diff — new files are untracked; read_file them)',\n })\n }\n case 'read_file':\n return readWorktreeFile(worktreePath, args)\n case 'research': {\n if (!opts.research) return 'error: research tool is not provisioned in this run'\n const query = typeof args.query === 'string' ? args.query.trim() : ''\n if (query.length === 0) return 'error: research requires a non-empty `query`'\n const result = await opts.research(query)\n signal.throwIfAborted()\n return truncate(result, researchResultMaxChars)\n }\n case 'run_verifier': {\n const result = await groundVerify()\n return JSON.stringify({\n ok: result.ok,\n feedback: truncate(result.feedback ?? '', 4_000),\n })\n }\n default:\n return `error: unknown tool: ${name}`\n }\n }\n\n await runBrainLoop({\n chat: opts.brain,\n tools: opts.research ? [...driverToolSpecs, researchToolSpec] : driverToolSpecs,\n execute,\n initialMessages: [\n {\n role: 'system',\n content: opts.research\n ? `${buildDriverSystem}\\n\\n${researchDriverNote}`\n : buildDriverSystem,\n },\n {\n role: 'user',\n content: [\n `THE BUILD BRIEF (the contract your workers must satisfy — fold what each needs into its instruction; workers never see this brief):`,\n '',\n briefing,\n '',\n `Worker-session budget: ${sessionCap}. The worktree is a fresh checkout at ${worktreePath}.`,\n ].join('\\n'),\n },\n ],\n maxTurns: opts.maxTurns ?? Math.max(8, 2 + sessionCap * 3),\n hooks: { stopBefore: () => signal.aborted },\n })\n signal.throwIfAborted()\n\n // The completion oracle: the driver stopped (or ran out of turns) — ground truth decides.\n const verdict = await groundVerify()\n signal.throwIfAborted()\n if (!verdict.ok) return { applied: false, summary: '' }\n return { applied: true, summary: summarizeFindings(findings) }\n },\n }\n}\n\nconst driverToolSpecs = [\n {\n type: 'function' as const,\n function: {\n name: 'run_worker',\n description:\n 'Run ONE coding-harness session in the worktree with your instruction as its entire goal. The worktree persists between sessions. Sessions are capped — author each instruction richly (outcome, context, placement, the check it is held to).',\n parameters: {\n type: 'object',\n properties: {\n instruction: {\n type: 'string',\n description: 'The complete, self-contained goal for this worker session.',\n },\n },\n required: ['instruction'],\n },\n },\n },\n {\n type: 'function' as const,\n function: {\n name: 'inspect_worktree',\n description:\n 'Current git state of the worktree: changed paths + the tracked-file diff (truncated). New untracked files show in changedPaths only — read_file them.',\n parameters: { type: 'object', properties: {} },\n },\n },\n {\n type: 'function' as const,\n function: {\n name: 'read_file',\n description: 'Read one file from the worktree (paths are worktree-relative).',\n parameters: {\n type: 'object',\n properties: {\n path: { type: 'string', description: 'Worktree-relative file path.' },\n maxBytes: { type: 'number', description: 'Byte cap (default 8192).' },\n },\n required: ['path'],\n },\n },\n },\n {\n type: 'function' as const,\n function: {\n name: 'run_verifier',\n description:\n 'Run the intrinsic check of the surface (compile+tests / boot-and-probe). Its result — not your judgment — decides whether the candidate is kept.',\n parameters: { type: 'object', properties: {} },\n },\n },\n]\n\n/** Only offered when `opts.research` is wired — a tool the driver cannot call\n * must never appear in its tool list. */\nconst researchToolSpec = {\n type: 'function' as const,\n function: {\n name: 'research',\n description:\n 'Search external sources (MCP registries, vendor docs) for an EXISTING server that closes the capability gap — the adopt-not-build check. Returns text findings.',\n parameters: {\n type: 'object',\n properties: {\n query: { type: 'string', description: 'What capability / server to search for.' },\n },\n required: ['query'],\n },\n },\n}\n\n/** `git diff` over the worktree (tracked files). Fails loud like `worktreeChangedPaths` — a git\n * fault on a fresh checkout is a broken setup, not an empty diff. */\nfunction worktreeDiff(worktreePath: string): string {\n const result = spawnSync('git', ['diff'], { cwd: worktreePath, encoding: 'utf-8' })\n if (result.error) {\n throw new Error(\n `driverLoopGenerator: git diff failed to spawn in ${worktreePath}: ${result.error.message}`,\n )\n }\n if (result.status !== 0) {\n throw new Error(\n `driverLoopGenerator: git diff exited ${result.status} in ${worktreePath}: ${result.stderr.trim()}`,\n )\n }\n return result.stdout\n}\n\n/** Bounded, worktree-jailed file read for the driver's `read_file`. A path escaping the worktree\n * is refused (the driver only rates work in the candidate tree; it has no business elsewhere). */\nfunction readWorktreeFile(worktreePath: string, args: Record<string, unknown>): string {\n const rel = typeof args.path === 'string' ? args.path : ''\n if (rel.length === 0) return 'error: read_file requires `path`'\n const root = resolve(worktreePath)\n const target = resolve(root, rel)\n if (target !== root && !target.startsWith(root + sep)) {\n return `error: path escapes the worktree: ${rel}`\n }\n const maxBytes =\n typeof args.maxBytes === 'number' && args.maxBytes > 0\n ? Math.min(args.maxBytes, 65_536)\n : readFileDefaultBytes\n try {\n const size = statSync(target).size\n const body = readFileSync(target, 'utf-8').slice(0, maxBytes)\n return size > maxBytes ? `${body}\\n… (${size - maxBytes} bytes truncated)` : body\n } catch (e) {\n return `error: ${e instanceof Error ? e.message : String(e)}`\n }\n}\n\nfunction tail(s: string, n: number): string {\n const trimmed = s.trim()\n return trimmed.length <= n ? trimmed : `…${trimmed.slice(-n)}`\n}\n\nfunction truncate(s: string, n: number): string {\n return s.length <= n ? s : `${s.slice(0, n - 1)}…`\n}\n","/**\n * `mcpServeVerifier` — the intrinsic verifier for a built MCP server: the\n * boot-and-probe checker named in docs/artifact-lifecycle-frontier.md. A\n * generated MCP server is only a candidate if it actually *serves* — so this\n * boots it over stdio (the default local MCP transport) and runs the real\n * handshake: `initialize` → `notifications/initialized` → `tools/list`, and\n * asserts the server answers with at least `minTools` tools.\n *\n * The spawn + handshake is the SHARED same-host stdio connection\n * (`connectStdioMcp`) — the same code path that later serves the built server\n * LIVE to a scored run (`materializeLocalMcp`), so \"verified it serves\" and\n * \"served while scored\" can never drift apart.\n *\n * Outcomes follow the `Verifier` contract: a server that fails to start, exits\n * early, errors the handshake, times out, or exposes no tools is a FAILED\n * candidate (`{ok:false}`, fed back into the next generation shot); a missing\n * start binary or spawn fault THROWS (a setup bug, never a silent fallback).\n */\n\nimport {\n connectStdioMcp,\n McpSpawnFault,\n type StdioMcpConnection,\n} from '../runtime/stdio-mcp-client'\nimport type { Verifier, VerifyResult } from './agentic-generator'\n\nexport interface McpServeSpec {\n /** Command that starts the built MCP server in the worktree (stdio transport). */\n command: string\n args?: string[]\n /** Extra env for the server process (merged over `process.env`). */\n env?: Record<string, string>\n /** Handshake timeout (ms). Default 30s. */\n timeoutMs?: number\n /** Minimum tools the server must expose to pass. Default 1. */\n minTools?: number\n}\n\n/** Build a `Verifier` that boots a generated MCP server over stdio and checks it exposes tools. */\nexport function mcpServeVerifier(spec: McpServeSpec): Verifier {\n const minTools = spec.minTools ?? 1\n\n return async (worktreePath: string): Promise<VerifyResult> => {\n let conn: StdioMcpConnection\n try {\n conn = await connectStdioMcp({\n command: spec.command,\n ...(spec.args ? { args: spec.args } : {}),\n cwd: worktreePath,\n ...(spec.env ? { env: spec.env } : {}),\n ...(spec.timeoutMs !== undefined ? { timeoutMs: spec.timeoutMs } : {}),\n })\n } catch (err) {\n if (err instanceof McpSpawnFault) {\n throw new Error(`mcpServeVerifier: ${err.message}`)\n }\n // Crash-on-boot / handshake error / timeout: a failed candidate, with the\n // connection's stderr-tailed message as the feedback for the next shot.\n return { ok: false, feedback: err instanceof Error ? err.message : String(err) }\n }\n try {\n if (conn.tools.length < minTools) {\n return {\n ok: false,\n feedback: `tools/list returned ${conn.tools.length} tool(s), need >= ${minTools}`,\n }\n }\n return { ok: true }\n } finally {\n await conn.close()\n }\n }\n}\n","import { canonicalJson } from '@tangle-network/agent-eval'\nimport type {\n MutableSurface,\n ProposeContext,\n ProposedCandidate,\n SurfaceProposer,\n} from '@tangle-network/agent-eval/campaign'\nimport {\n type AgentProfile,\n type AgentProfileDiff,\n changedAgentProfileAxes,\n} from '@tangle-network/agent-interface'\nimport { canonicalCandidateDigest } from '../candidate-execution/digest'\nimport {\n applyExactAgentProfileDiff,\n parseExactAgentProfile,\n parseExactAgentProfileDiff,\n} from '../candidate-execution/profile'\n\nexport type ProfileDiffProposerContext<TFindings = unknown> = ProposeContext<TFindings> & {\n profile: AgentProfile\n}\n\nexport interface ProfileDiffProposerOptions<TFindings = unknown> {\n proposeDiffs(\n context: ProfileDiffProposerContext<TFindings>,\n ): Promise<readonly AgentProfileDiff[]> | readonly AgentProfileDiff[]\n}\n\n/**\n * Turn exact AgentProfileDiffs from any source into full profile candidates for\n * the shared optimization loop. Research, catalogs, humans, and trace miners\n * differ only in `proposeDiffs`; measurement and promotion stay identical.\n */\nexport function profileDiffProposer<TFindings = unknown>(\n options: ProfileDiffProposerOptions<TFindings>,\n): SurfaceProposer<TFindings> {\n return {\n kind: 'agent-profile-diff',\n async propose(ctx): Promise<Array<MutableSurface | ProposedCandidate>> {\n if (typeof ctx.currentSurface !== 'string') {\n throw new Error('profileDiffProposer requires a serialized AgentProfile surface')\n }\n const profile = parseSerializedProfile(ctx.currentSurface)\n const proposals = await options.proposeDiffs({\n ...ctx,\n profile,\n })\n const candidates: ProposedCandidate[] = []\n const normalizedBaseline = applyExactAgentProfileDiff(\n profile,\n { kind: 'agent-profile-diff' },\n 'profile diff baseline',\n )\n const seen = new Set<string>([canonicalJson(normalizedBaseline)])\n for (const [index, proposal] of proposals.entries()) {\n if (candidates.length >= ctx.populationSize || ctx.signal.aborted) break\n const diff = parseExactAgentProfileDiff(proposal, `profile diff proposal ${index}`)\n const axes = changedAgentProfileAxes(diff)\n if (axes.length === 0) continue\n const candidate = applyExactAgentProfileDiff(\n profile,\n diff,\n `profile diff candidate ${index}`,\n )\n const surface = canonicalJson(candidate)\n if (seen.has(surface)) continue\n seen.add(surface)\n candidates.push({\n surface,\n label: diff.title ?? diff.id ?? `Change ${axes.join(', ')}`,\n rationale: candidateRationale(diff, axes),\n })\n }\n return candidates\n },\n }\n}\n\nfunction candidateRationale(diff: AgentProfileDiff, axes: readonly string[]): string {\n const reason = diff.rationale ?? `Changes profile axes: ${axes.join(', ')}.`\n const source = diff.source\n ? [diff.source.kind, ...(diff.source.artifacts ?? [])].join(' · ')\n : 'source unspecified'\n return `${reason} Source: ${source}. Diff: ${canonicalCandidateDigest(diff)}.`\n}\n\nfunction parseSerializedProfile(surface: string): AgentProfile {\n let raw: unknown\n try {\n raw = JSON.parse(surface)\n } catch (cause) {\n throw new Error('profileDiffProposer current surface is not valid AgentProfile JSON', {\n cause,\n })\n }\n return parseExactAgentProfile(raw, 'profileDiffProposer current profile')\n}\n","/**\n *\n * `reflectiveGenerator` — the cheap, no-sandbox `CandidateGenerator`. It drafts\n * surface edits via the existing improvement proposer (`proposeFromFindings`,\n * one LLM patch per finding) and applies them as ONE coherent improvement into\n * the candidate worktree. `maxShots` is ignored — reflection is single-shot by\n * construction (the patches are already drafted).\n *\n * This is the `shots=1, sandbox=off` setting of the one improvement driver.\n * The `agenticGenerator` (a multi-shot verify-in-session loop) is the\n * `shots=N` setting — both plug into the same `improvementDriver`.\n *\n * @experimental\n */\n\nimport { spawnSync } from 'node:child_process'\nimport type { SurfaceImprovementEdit } from '../agent/improvement-adapter'\nimport type { ImprovementProposalSource } from '../analyst-loop/types'\nimport type { CandidateGenerator } from './improvement-driver'\n\nexport interface ReflectiveGeneratorOptions {\n improvementProposalSource: ImprovementProposalSource<SurfaceImprovementEdit>\n}\n\n/** Cheap no-sandbox `CandidateGenerator` (the `shots=1` setting): draft surface edits via the improvement adapter and apply them as one coherent candidate. */\nexport function reflectiveGenerator(opts: ReflectiveGeneratorOptions): CandidateGenerator {\n return {\n kind: 'reflective',\n async generate({ worktreePath, findings }) {\n const batch = await opts.improvementProposalSource.proposeFromFindings(findings)\n if (batch.edits.length === 0) return { applied: false, summary: '' }\n\n let applied = 0\n for (const edit of batch.edits) {\n if (applyPatch(edit.patch, worktreePath)) applied++\n }\n if (applied === 0) return { applied: false, summary: '' }\n\n const summary =\n batch.edits.length === 1\n ? batch.edits[0]!.summary\n : `analyst: ${applied} surface edit${applied === 1 ? '' : 's'}`\n return { applied: true, summary }\n },\n }\n}\n\n/** Apply a proposed patch inside the isolated candidate worktree.\n * candidate worktree (a fresh checkout of baseRef, so `-p0` paths match). */\nfunction applyPatch(patch: string, cwd: string): boolean {\n const result = spawnSync('git', ['apply', '--whitespace=fix', '-p0', '-'], {\n cwd,\n input: patch,\n encoding: 'utf-8',\n })\n return result.status === 0\n}\n","/**\n *\n * Pure readiness-decision helper. Maps a `KnowledgeReadinessReport` from\n * `@tangle-network/agent-eval` to a three-state branch (`ready` / `blocked` /\n * `caveat`) the runtime, route handlers, and UI shells can all switch on.\n *\n * Default `minimumScore` of 0.7 mirrors the readiness scoring scale in\n * agent-eval; callers tightening or loosening this should keep it consistent\n * across all entry points for the same product so the UI / metrics agree on\n * what \"caveat\" means.\n *\n * @stable\n */\n\nimport type { KnowledgeReadinessReport } from '@tangle-network/agent-eval'\n\nimport { ValidationError } from './errors'\nimport type { KnowledgeReadinessDecision } from './types'\n\nconst DEFAULT_MINIMUM_READINESS_SCORE = 0.7\n\n/**\n * Map a `KnowledgeReadinessReport` to a three-state branch (`ready` / `blocked` / `caveat`) the runtime, route handlers, and UI shells all switch on.\n *\n * @stable\n */\nexport function decideKnowledgeReadiness(\n report: KnowledgeReadinessReport,\n options: { minimumScore?: number } = {},\n): KnowledgeReadinessDecision {\n const minimumScore = options.minimumScore ?? DEFAULT_MINIMUM_READINESS_SCORE\n if (!Number.isFinite(minimumScore) || minimumScore < 0 || minimumScore > 1) {\n throw new ValidationError(\n `minimumScore must be a finite number in [0, 1]; received ${String(minimumScore)}`,\n )\n }\n const blockingGapIds = report.blockingMissingRequirements.map((requirement) => requirement.id)\n const nonBlockingGapIds = report.nonBlockingGaps.map((requirement) => requirement.id)\n if (blockingGapIds.length > 0) {\n return {\n passed: false,\n status: 'blocked',\n reason: report.reason,\n readinessScore: report.readinessScore,\n recommendedAction: report.recommendedAction,\n severity: report.severity,\n blockingGapIds,\n nonBlockingGapIds,\n }\n }\n if (report.readinessScore < minimumScore) {\n return {\n passed: false,\n status: 'caveat',\n reason: `Knowledge readiness score ${report.readinessScore.toFixed(3)} is below minimum ${minimumScore.toFixed(3)}.`,\n readinessScore: report.readinessScore,\n recommendedAction: report.recommendedAction,\n severity: report.severity,\n blockingGapIds,\n nonBlockingGapIds,\n }\n }\n return {\n passed: true,\n status: 'ready',\n reason: report.reason,\n readinessScore: report.readinessScore,\n recommendedAction: report.recommendedAction,\n severity: report.severity,\n blockingGapIds,\n nonBlockingGapIds,\n }\n}\n","/**\n * The product-facing backend selector for `runChatThroughRuntime` /\n * `runAgentTaskStream`: one call turns a `--backend {router,tcloud,cli-bridge,\n * sandbox}` choice into the `AgentExecutionBackend` the chat leg runs on.\n *\n * It is the `AgentExecutionBackend` sibling of `resolveSandboxClient` (which\n * resolves the `SandboxClient` a `runAgentRounds` drives). Both exist for the same\n * reason: every in-process eval product hand-rolled the identical\n * \"`backend-name` → `createOpenAICompatibleBackend`\" branch, and the copies\n * drift. This is the single generic resolver they share.\n *\n * - `router` / `tcloud` / `cli-bridge` → OpenAI-compatible chat completions.\n * All three speak `POST {baseUrl}/chat/completions` in OpenAI's SSE shape —\n * the router (a.k.a. tcloud) IS that endpoint, and cli-bridge fronts a\n * harness CLI behind the same protocol at its own `/v1`. They differ only\n * in `baseUrl` / `apiKey` and the `kind` label a product wants on its\n * traces. cli-bridge REQUIRES `model` in the request body, so it MUST route\n * through `createOpenAICompatibleBackend` (which sends it), never a\n * transport that drops the field.\n * - `sandbox` → the caller's own domain backend. The sandbox variant carries\n * product specifics (system prompt, workspace id, in-box D1 executor) that\n * do NOT belong in the substrate, so the product passes a `sandboxBackend()`\n * seam that this resolver simply invokes.\n *\n * This resolver is PURE backend selection. Product concerns — credit hard-cuts,\n * fetch-capture shims, D1 platform wiring — stay as product-side WRAPPERS\n * around the returned backend. The OpenAI-compat passthrough fields (`tools`,\n * `toolChoice`, `responseFormat`, `temperature`, `maxTokens`, `fetchImpl`,\n * `retry`) are forwarded verbatim so a product can advertise its app tools,\n * preserve generation settings, or install a capturing fetch without\n * re-opening the branch this consolidation closes.\n */\n\nimport { createOpenAICompatibleBackend } from './backends'\nimport type { AgentBackendInput, AgentExecutionBackend } from './types'\n\n/** The transport a chat backend runs on. */\nexport type AgentBackendKind = 'router' | 'tcloud' | 'cli-bridge' | 'sandbox'\n\n/**\n * OpenAI-compat passthrough forwarded to `createOpenAICompatibleBackend` for\n * the `router` / `tcloud` / `cli-bridge` kinds. Mirrors that factory's optional\n * inputs so a product keeps its tool advertising / capture-fetch without\n * re-implementing the backend branch.\n */\ntype OpenAICompatPassthrough = Pick<\n Parameters<typeof createOpenAICompatibleBackend>[0],\n 'tools' | 'toolChoice' | 'responseFormat' | 'temperature' | 'maxTokens' | 'fetchImpl' | 'retry'\n>\n\nexport interface ResolveAgentBackendOptions<TInput extends AgentBackendInput = AgentBackendInput>\n extends OpenAICompatPassthrough {\n /** The chat transport to resolve. */\n kind: AgentBackendKind\n /**\n * Bearer credential for the OpenAI-compat kinds. Empty string is valid for a\n * loopback-anonymous cli-bridge; a `router`/`tcloud` route with an empty key\n * is a caller bug the product surfaces before calling in.\n */\n apiKey: string\n /** Base URL for the OpenAI-compat kinds. cli-bridge's is its `/v1`. */\n baseUrl: string\n /** Model id sent on every request. cli-bridge rejects a request without it. */\n model: string\n /** `kind` label stamped on the resolved backend + its traces. Defaults to `kind`. */\n label?: string\n /**\n * `sandbox` kind: the product's own domain backend. Required for that kind —\n * the substrate owns no product sandbox shape, so a `sandbox` resolution with\n * no seam is a caller bug, not a silent fallback.\n */\n sandboxBackend?: () => AgentExecutionBackend<TInput>\n}\n\n/**\n * Resolve the `AgentExecutionBackend` for the chosen `kind`. Reuse this instead\n * of hand-rolling the `createOpenAICompatibleBackend` branch in each product.\n */\nexport function resolveAgentBackend<TInput extends AgentBackendInput = AgentBackendInput>(\n opts: ResolveAgentBackendOptions<TInput>,\n): AgentExecutionBackend<TInput> {\n switch (opts.kind) {\n case 'router':\n case 'tcloud':\n case 'cli-bridge': {\n const passthrough: OpenAICompatPassthrough = {}\n // Forward only the fields a caller actually set — an explicit\n // `tools: []` / `undefined` would otherwise reach the factory and change\n // its request shape (some providers reject an empty `tools` array).\n if (opts.tools !== undefined) passthrough.tools = opts.tools\n if (opts.toolChoice !== undefined) passthrough.toolChoice = opts.toolChoice\n if (opts.responseFormat !== undefined) passthrough.responseFormat = opts.responseFormat\n if (opts.temperature !== undefined) passthrough.temperature = opts.temperature\n if (opts.maxTokens !== undefined) passthrough.maxTokens = opts.maxTokens\n if (opts.fetchImpl !== undefined) passthrough.fetchImpl = opts.fetchImpl\n if (opts.retry !== undefined) passthrough.retry = opts.retry\n return createOpenAICompatibleBackend<TInput>({\n apiKey: opts.apiKey,\n baseUrl: opts.baseUrl,\n model: opts.model,\n kind: opts.label ?? opts.kind,\n ...passthrough,\n })\n }\n case 'sandbox': {\n if (!opts.sandboxBackend) {\n throw new Error(\"resolveAgentBackend: kind 'sandbox' requires opts.sandboxBackend\")\n }\n return opts.sandboxBackend()\n }\n }\n}\n","/**\n *\n * The two top-level entry points:\n *\n * - `runAgentTask` — single-shot lifecycle for adapter-driven tasks.\n * - `runAgentTaskStream` — streaming lifecycle that delegates execution to an\n * `AgentExecutionBackend` (model API, sandbox, or custom iterable).\n *\n * Both gate the run on `KnowledgeReadinessReport` from `agent-eval`, emit the\n * same lifecycle event vocabulary (under different shapes — see `types.ts`),\n * and route session lifecycle through a pluggable `RuntimeSessionStore`.\n *\n * @stable\n */\n\nimport {\n acquisitionPlansForKnowledgeGaps,\n blockingKnowledgeEval,\n type ControlContext,\n type ControlEvalResult,\n type ControlRunResult,\n type DataAcquisitionPlan,\n FAILURE_CLASSES,\n type FailureClass,\n type KnowledgeReadinessReport,\n type RunRecord,\n runAgentControlLoop,\n scoreKnowledgeReadiness,\n type UserQuestion,\n userQuestionsForKnowledgeGaps,\n} from '@tangle-network/agent-eval'\n\nconst FAILURE_CLASS_SET = new Set<string>(FAILURE_CLASSES)\n\n/** True when a free-form control failure string is a canonical taxonomy\n * class — so only real taxonomy tags are promoted to the cross-agent\n * `RunRecord.failureClass` key; novel strings stay as `failureMode` detail. */\nfunction asFailureClass(value: string | undefined): FailureClass | undefined {\n return value && FAILURE_CLASS_SET.has(value) ? (value as FailureClass) : undefined\n}\n\n/** Stamp cross-cutting defaults onto adapter-projected RunRecords without\n * overriding anything the adapter set explicitly:\n * - `scenarioId` — the run's scenario, when the record omits one.\n * - `failureClass` — the control layer's failure classification promoted\n * onto the canonical cross-agent key, but ONLY when it's a real taxonomy\n * class. This is what lets the substrate aggregate failures across every\n * agent in one vocabulary instead of per-agent ad-hoc strings. */\nexport function applyRunRecordDefaults(\n records: RunRecord[],\n scenarioId: string,\n controlFailureClass: string | undefined,\n): RunRecord[] {\n const fc = asFailureClass(controlFailureClass)\n return records.map((record) => {\n let r = record\n if (r.scenarioId === undefined) r = { ...r, scenarioId }\n if (r.failureClass === undefined && fc) r = { ...r, failureClass: fc }\n return r\n })\n}\n\nimport { normalizeBackendStreamEvent } from './backends'\nimport { BackendTransportError } from './errors'\nimport { decideKnowledgeReadiness } from './readiness'\nimport { nowIso, startOrResumeRuntimeSession, touchSession } from './sessions'\nimport type {\n AgentBackendInput,\n AgentKnowledgeProvider,\n AgentRuntimeEventSink,\n AgentTaskContext,\n AgentTaskRunResult,\n AgentTaskSpec,\n AgentTaskStatus,\n BackendErrorDetail,\n RunAgentTaskOptions,\n RunAgentTaskStreamOptions,\n RuntimeStreamEvent,\n} from './types'\n\n/**\n * Single-shot task lifecycle for adapter-driven tasks: readiness-gated, emits the runtime lifecycle event vocabulary, session-store pluggable.\n *\n * @stable\n */\nexport async function runAgentTask<\n TState,\n TAction,\n TActionResult,\n TEval extends ControlEvalResult = ControlEvalResult,\n>(\n options: RunAgentTaskOptions<TState, TAction, TActionResult, TEval>,\n): Promise<AgentTaskRunResult<TState, TAction, TActionResult, TEval>> {\n const task = options.task\n await emit(options.onEvent, { type: 'task_start', task })\n await emit(options.onEvent, { type: 'readiness_start', task })\n let knowledge = await buildReadiness(task, options.knowledge)\n await emit(options.onEvent, { type: 'readiness_end', task, knowledge })\n const questions = userQuestionsForKnowledgeGaps(knowledge.blockingMissingRequirements)\n const acquisitionPlans = acquisitionPlansForKnowledgeGaps([\n ...knowledge.blockingMissingRequirements,\n ...knowledge.nonBlockingGaps,\n ])\n const preflight = await runKnowledgePreflight(\n task,\n questions,\n acquisitionPlans,\n options.knowledge,\n options.onEvent,\n )\n if (\n options.knowledge?.refreshReadiness &&\n (Object.keys(preflight.userAnswers).length > 0 || preflight.acquiredEvidenceIds.length > 0)\n ) {\n await emit(options.onEvent, { type: 'readiness_start', task })\n knowledge = await options.knowledge.refreshReadiness({\n task,\n previous: knowledge,\n userAnswers: preflight.userAnswers,\n acquiredEvidenceIds: preflight.acquiredEvidenceIds,\n })\n await emit(options.onEvent, { type: 'readiness_end', task, knowledge })\n }\n\n await emit(options.onEvent, { type: 'control_start', task, knowledge })\n const scenarioId = options.scenarioId ?? task.id\n const control = await runAgentControlLoop<TState, TAction, TActionResult, TEval>({\n intent: task.intent,\n budget: task.budget,\n signal: options.signal,\n store: options.store,\n scenarioId,\n projectId: options.projectId,\n variantId: options.variantId,\n observe: ({ history, abortSignal }) =>\n options.adapter.observe({ task, knowledge, history, abortSignal }),\n validate: async ({ state, history, abortSignal }) => {\n const readinessEval = blockingKnowledgeEval(knowledge, {\n minimumScore: options.minimumReadinessScore,\n })\n const evals = await options.adapter.validate({\n task,\n knowledge,\n state,\n history,\n abortSignal,\n })\n return [readinessEval as TEval, ...evals]\n },\n decide: (ctx) => {\n if (isKnowledgeBlocked(ctx.evals)) {\n return (\n options.adapter.onKnowledgeBlocked?.({\n task,\n knowledge,\n questions,\n acquisitionPlans,\n }) ?? {\n type: 'stop',\n pass: false,\n score: knowledge.readinessScore,\n reason: `knowledge readiness blocked: ${knowledge.reason}`,\n }\n )\n }\n return options.adapter.decide(toAgentContext(task, knowledge, ctx))\n },\n act: (action, ctx) => options.adapter.act(action, toAgentContext(task, knowledge, ctx)),\n shouldStop: options.adapter.shouldStop\n ? (ctx) => options.adapter.shouldStop!(toAgentContext(task, knowledge, ctx))\n : undefined,\n getActionCostUsd: options.adapter.getActionCostUsd\n ? ({ action, result, state, evals, history }) =>\n options.adapter.getActionCostUsd!({ action, result, task, state, evals, history })\n : undefined,\n onStep: (step) => emit(options.onEvent, { type: 'control_step', task, step }),\n })\n await emit(options.onEvent, { type: 'control_end', task, control })\n const status = statusFromControl(control)\n await emit(options.onEvent, { type: 'task_end', task, status, reason: control.reason })\n\n return {\n task,\n status,\n knowledge,\n questions,\n acquisitionPlans,\n userAnswers: preflight.userAnswers,\n acquiredEvidenceIds: preflight.acquiredEvidenceIds,\n control,\n runRecords: applyRunRecordDefaults(\n options.adapter.projectRunRecords?.(control, task) ?? [],\n scenarioId,\n control.failureClass,\n ),\n }\n}\n\n/**\n * Streaming task lifecycle: delegates execution to an `AgentExecutionBackend` (model API, sandbox, or custom iterable) and yields lifecycle events as they happen.\n *\n * @stable\n */\nexport async function* runAgentTaskStream<TInput extends AgentBackendInput = AgentBackendInput>(\n options: RunAgentTaskStreamOptions<TInput>,\n): AsyncIterable<RuntimeStreamEvent> {\n const task = options.task\n const input = { task, ...(options.input ?? {}) } as TInput\n yield streamEvent({ type: 'task_start', task })\n\n yield streamEvent({ type: 'readiness_start', task })\n let knowledge = await buildReadiness(task, options.knowledge)\n const questions = userQuestionsForKnowledgeGaps(knowledge.blockingMissingRequirements)\n const acquisitionPlans = acquisitionPlansForKnowledgeGaps([\n ...knowledge.blockingMissingRequirements,\n ...knowledge.nonBlockingGaps,\n ])\n const preflight = await runKnowledgePreflightStream(\n task,\n questions,\n acquisitionPlans,\n options.knowledge,\n )\n for (const event of preflight.events) yield event\n if (\n options.knowledge?.refreshReadiness &&\n (Object.keys(preflight.userAnswers).length > 0 || preflight.acquiredEvidenceIds.length > 0)\n ) {\n yield streamEvent({ type: 'readiness_start', task })\n knowledge = await options.knowledge.refreshReadiness({\n task,\n previous: knowledge,\n userAnswers: preflight.userAnswers,\n acquiredEvidenceIds: preflight.acquiredEvidenceIds,\n })\n }\n const decision = decideKnowledgeReadiness(knowledge, {\n minimumScore: options.minimumReadinessScore,\n })\n yield streamEvent({ type: 'readiness_end', task, knowledge, decision })\n if (!decision.passed && decision.status === 'blocked') {\n const reason = `knowledge readiness blocked: ${decision.reason}`\n yield streamEvent({ type: 'task_end', task, status: 'blocked', reason })\n yield streamEvent({ type: 'final', task, status: 'blocked', reason })\n return\n }\n\n const store = options.sessionStore\n const opened = await startOrResumeRuntimeSession({\n backend: options.backend,\n input,\n context: { task, knowledge, signal: options.signal },\n store,\n sessionId: options.sessionId,\n resume: options.resume,\n })\n let session = opened.session\n const shouldResume = opened.resumed\n const sessionEvent = streamEvent({\n type: shouldResume ? 'session_resumed' : 'session_created',\n task,\n session,\n })\n await store?.appendEvent?.(session.id, sessionEvent)\n yield sessionEvent\n\n const backendStart = streamEvent({\n type: 'backend_start',\n task,\n session,\n backend: options.backend.kind,\n })\n await store?.appendEvent?.(session.id, backendStart)\n yield backendStart\n\n let finalText = ''\n try {\n for await (const rawEvent of options.backend.stream(opened.input, {\n task,\n knowledge,\n session,\n signal: options.signal,\n })) {\n const event = normalizeBackendStreamEvent(rawEvent, task, session)\n if (event.type === 'text_delta') finalText += event.text\n await store?.appendEvent?.(session.id, event)\n yield event\n }\n const completedStatus: AgentTaskStatus = 'completed'\n session = touchSession({ ...session, status: completedStatus })\n await store?.put(session)\n const backendEnd = streamEvent({\n type: 'backend_end',\n task,\n session,\n backend: options.backend.kind,\n })\n await store?.appendEvent?.(session.id, backendEnd)\n yield backendEnd\n const reason = 'backend completed'\n const taskEnd = streamEvent({ type: 'task_end', task, status: completedStatus, reason })\n await store?.appendEvent?.(session.id, taskEnd)\n yield taskEnd\n const final = streamEvent({\n type: 'final',\n task,\n session,\n status: completedStatus,\n reason,\n text: finalText || undefined,\n })\n await store?.appendEvent?.(session.id, final)\n yield final\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err)\n session = touchSession({ ...session, status: options.signal?.aborted ? 'aborted' : 'failed' })\n await store?.put(session)\n let stopErrorMessage: string | undefined\n try {\n await options.backend.stop?.(session, message)\n } catch (stopErr) {\n stopErrorMessage = stopErr instanceof Error ? stopErr.message : String(stopErr)\n }\n const combinedMessage = stopErrorMessage\n ? `${message}; backend stop failed: ${stopErrorMessage}`\n : message\n // Typed transport detail — preserves status code + truncated body so\n // consumers can map onto `RunRecord.error` without re-parsing the log\n // string. Required by the runtime's fail-loud contract: silent empty\n // output for a 402 / 401 / 5xx hides the real failure mode.\n const errorDetail: BackendErrorDetail =\n err instanceof BackendTransportError\n ? {\n kind: 'transport',\n message: combinedMessage,\n status: err.status,\n body: err.body,\n }\n : { kind: 'backend', message: combinedMessage }\n const backendError = streamEvent({\n type: 'backend_error',\n task,\n session,\n backend: options.backend.kind,\n message: combinedMessage,\n recoverable: !options.signal?.aborted,\n error: errorDetail,\n })\n await store?.appendEvent?.(session.id, backendError)\n yield backendError\n const status: AgentTaskStatus = options.signal?.aborted ? 'aborted' : 'failed'\n const taskEnd = streamEvent({ type: 'task_end', task, status, reason: message })\n await store?.appendEvent?.(session.id, taskEnd)\n yield taskEnd\n const final = streamEvent({\n type: 'final',\n task,\n session,\n status,\n reason: message,\n text: finalText || undefined,\n error: errorDetail,\n })\n await store?.appendEvent?.(session.id, final)\n yield final\n }\n}\n\nasync function runKnowledgePreflight<\n TState,\n TAction,\n TActionResult,\n TEval extends ControlEvalResult,\n>(\n task: AgentTaskSpec,\n questions: UserQuestion[],\n acquisitionPlans: DataAcquisitionPlan[],\n provider: AgentKnowledgeProvider | undefined,\n onEvent: AgentRuntimeEventSink<TState, TAction, TActionResult, TEval> | undefined,\n): Promise<{ userAnswers: Record<string, string>; acquiredEvidenceIds: string[] }> {\n let userAnswers: Record<string, string> = {}\n let acquiredEvidenceIds: string[] = []\n if (questions.length > 0 && provider?.answerQuestions) {\n await emit(onEvent, { type: 'questions_start', task, questions })\n userAnswers = await provider.answerQuestions(questions, task)\n await emit(onEvent, { type: 'questions_end', task, questions, userAnswers })\n }\n if (acquisitionPlans.length > 0 && provider?.executeAcquisitionPlans) {\n await emit(onEvent, { type: 'acquisition_start', task, acquisitionPlans })\n acquiredEvidenceIds = await provider.executeAcquisitionPlans(acquisitionPlans, task)\n await emit(onEvent, {\n type: 'acquisition_end',\n task,\n acquisitionPlans,\n acquiredEvidenceIds,\n })\n }\n return { userAnswers, acquiredEvidenceIds }\n}\n\nasync function runKnowledgePreflightStream(\n task: AgentTaskSpec,\n questions: UserQuestion[],\n acquisitionPlans: DataAcquisitionPlan[],\n provider: AgentKnowledgeProvider | undefined,\n): Promise<{\n userAnswers: Record<string, string>\n acquiredEvidenceIds: string[]\n events: RuntimeStreamEvent[]\n}> {\n const events: RuntimeStreamEvent[] = []\n let userAnswers: Record<string, string> = {}\n let acquiredEvidenceIds: string[] = []\n if (questions.length > 0 && provider?.answerQuestions) {\n events.push(streamEvent({ type: 'questions_start', task, questions }))\n userAnswers = await provider.answerQuestions(questions, task)\n events.push(streamEvent({ type: 'questions_end', task, questions, userAnswers }))\n }\n if (acquisitionPlans.length > 0 && provider?.executeAcquisitionPlans) {\n events.push(streamEvent({ type: 'acquisition_start', task, acquisitionPlans }))\n acquiredEvidenceIds = await provider.executeAcquisitionPlans(acquisitionPlans, task)\n events.push(\n streamEvent({ type: 'acquisition_end', task, acquisitionPlans, acquiredEvidenceIds }),\n )\n }\n return { userAnswers, acquiredEvidenceIds, events }\n}\n\nfunction streamEvent<T extends Omit<RuntimeStreamEvent, 'timestamp'>>(\n event: T,\n): T & { timestamp: string } {\n return { ...event, timestamp: nowIso() }\n}\n\nfunction buildReadiness(\n task: AgentTaskSpec,\n provider: AgentKnowledgeProvider | undefined,\n): Promise<KnowledgeReadinessReport> | KnowledgeReadinessReport {\n if (provider?.buildReadiness) return provider.buildReadiness(task)\n return scoreKnowledgeReadiness({\n taskId: task.id,\n requirements: task.requiredKnowledge ?? [],\n metadata: { domain: task.domain, ...task.metadata },\n })\n}\n\nfunction isKnowledgeBlocked(evals: ControlEvalResult[]): boolean {\n return evals.some((evalResult) => evalResult.id === 'knowledge-ready' && !evalResult.passed)\n}\n\nfunction statusFromControl(\n control: ControlRunResult<unknown, unknown, unknown, ControlEvalResult>,\n): AgentTaskStatus {\n if (control.stoppedBy === 'abort') return 'aborted'\n if (control.reason.includes('knowledge readiness blocked')) return 'blocked'\n if (control.pass) return 'completed'\n return 'failed'\n}\n\nasync function emit<TState, TAction, TActionResult, TEval extends ControlEvalResult>(\n sink: AgentRuntimeEventSink<TState, TAction, TActionResult, TEval> | undefined,\n event: Parameters<AgentRuntimeEventSink<TState, TAction, TActionResult, TEval>>[0],\n): Promise<void> {\n await sink?.(event)\n}\n\nfunction toAgentContext<TState, TAction, TActionResult, TEval extends ControlEvalResult>(\n task: AgentTaskSpec,\n knowledge: KnowledgeReadinessReport,\n ctx: ControlContext<TState, TAction, TActionResult, TEval>,\n): AgentTaskContext<TState, TAction, TActionResult, TEval> {\n return {\n task,\n knowledge,\n state: ctx.state,\n evals: ctx.evals,\n history: ctx.history,\n budget: ctx.budget,\n stepIndex: ctx.stepIndex,\n wallMs: ctx.wallMs,\n spentCostUsd: ctx.spentCostUsd,\n remainingCostUsd: ctx.remainingCostUsd,\n abortSignal: ctx.abortSignal,\n }\n}\n","/**\n *\n * Production-run lifecycle: record what the agent did on behalf of a customer,\n * what it cost, and how it ended.\n *\n * Three concerns live in this module:\n *\n * 1. **Lifecycle state machine** — `running` -> `completed | failed | cancelled`,\n * enforced by `RuntimeRunStateError`. Completion is idempotent for the same\n * status (a second `complete()` call is a no-op so retries / cleanup paths\n * don't double-fire side effects). A different terminal status is a state\n * error.\n *\n * 2. **Cost ledger** — every `llm_call` event the handle observes contributes\n * `tokensIn`, `tokensOut`, `costUsd`, and bumps `llmCalls`. Wall time is\n * measured from `startRuntimeRun()` to `complete()`. Surface via\n * `handle.cost()` for cost-per-task dashboards.\n *\n * 3. **Persistence adapter** — `RuntimeRunPersistenceAdapter` is the seam\n * consumers plug in to write a `RuntimeRunRow` to their D1 / postgres /\n * KV store. The adapter receives a sanitized row shape; no telemetry\n * payload bytes flow through it unless the consumer opts in via\n * `RuntimeRunOptions.telemetryEvents`.\n *\n * @stable\n */\n\nimport { RuntimeRunStateError, ValidationError } from './errors'\nimport type { AgentTaskSpec, RuntimeStreamEvent } from './types'\n\n/** @stable */\nexport type RuntimeRunStatus = 'running' | 'completed' | 'failed' | 'cancelled'\n\n/** @stable */\nexport interface RuntimeRunCost {\n /** Cumulative input tokens across every observed `llm_call` event. */\n tokensIn: number\n /** Cumulative output tokens across every observed `llm_call` event. */\n tokensOut: number\n /** Sum of `costUsd` from every observed `llm_call` event. */\n costUsd: number\n /** Wall time from `startRuntimeRun()` to `complete()` (or `now()` if not yet completed). */\n wallMs: number\n /** Count of `llm_call` events observed during the run. */\n llmCalls: number\n}\n\n/** @stable */\nexport interface RuntimeRunCompleteInput {\n status: Exclude<RuntimeRunStatus, 'running'>\n resultSummary?: string\n /** Optional explicit cost override; if omitted, the accumulated ledger is used. */\n cost?: Partial<RuntimeRunCost>\n /** Stable error message when `status === 'failed'`. */\n error?: string\n /** Additional adapter-specific fields merged into the persisted row. */\n metadata?: Record<string, unknown>\n}\n\n/** @stable */\nexport interface RuntimeRunRow {\n /** Stable runtime-side identifier. Adapters may translate to their own primary key. */\n id: string\n workspaceId: string\n sessionId?: string\n agentId?: string\n domain?: string\n taskId: string\n scenarioId?: string\n status: RuntimeRunStatus\n resultSummary?: string\n error?: string\n cost: RuntimeRunCost\n startedAt: string\n completedAt?: string\n metadata?: Record<string, unknown>\n}\n\n/** @stable */\nexport interface RuntimeRunPersistenceAdapter {\n /**\n * Called once when `handle.persist()` runs. Implementations write `row` to\n * their durable store (D1, postgres, KV) and return whatever the consumer\n * wants the caller to see (often the storage-side row id). Errors thrown\n * here propagate out of `persist()` so the caller can decide whether to\n * retry or log-and-continue.\n */\n upsert(row: RuntimeRunRow): Promise<void> | void\n}\n\n/** @stable */\nexport interface RuntimeRunOptions {\n workspaceId: string\n sessionId?: string\n agentId?: string\n taskSpec: AgentTaskSpec\n scenarioId?: string\n /** Optional persistence adapter; if omitted, `persist()` is a no-op. */\n adapter?: RuntimeRunPersistenceAdapter\n /** Override the row id; default = `${taskSpec.id}:${random suffix}`. */\n id?: string\n /** Override the clock; default = `Date.now()`. Useful for deterministic tests. */\n now?: () => number\n}\n\n/** @stable */\nexport interface RuntimeRunHandle {\n /** Stable id assigned at start. */\n readonly id: string\n readonly workspaceId: string\n readonly sessionId: string | undefined\n readonly taskSpec: AgentTaskSpec\n readonly status: RuntimeRunStatus\n\n /**\n * Observe a single `RuntimeStreamEvent`. The handle ignores non-cost events\n * (text deltas, tool calls) silently so consumers can pipe the whole stream\n * through `handle.observe`. `llm_call` events update the ledger.\n */\n observe(event: RuntimeStreamEvent): void\n\n /** Snapshot of the current cost ledger. Safe to call at any time. */\n cost(): RuntimeRunCost\n\n /**\n * Transition to a terminal state. Idempotent for the same status; throws\n * `RuntimeRunStateError` for a different terminal status (state machines\n * don't time-travel).\n */\n complete(input: RuntimeRunCompleteInput): void\n\n /** Build the current row without writing it. Useful for tests + dry runs. */\n toRow(metadata?: Record<string, unknown>): RuntimeRunRow\n\n /**\n * Persist the current row via the configured adapter. Must be called after\n * `complete()`. Idempotent for the same terminal state (the adapter sees\n * the same row on retry).\n */\n persist(metadata?: Record<string, unknown>): Promise<void>\n}\n\n/**\n *\n * Construct a runtime-run handle. The returned handle is mutable across its\n * lifetime; consumers should not share it across requests.\n *\n * @stable\n */\nexport function startRuntimeRun(options: RuntimeRunOptions): RuntimeRunHandle {\n if (!options.workspaceId) {\n throw new ValidationError('startRuntimeRun: workspaceId is required')\n }\n if (!options.taskSpec?.id) {\n throw new ValidationError('startRuntimeRun: taskSpec.id is required')\n }\n const now = options.now ?? Date.now\n const startedAtMs = now()\n const startedAt = new Date(startedAtMs).toISOString()\n const id = options.id ?? `${options.taskSpec.id}:${randomSuffix()}`\n\n let status: RuntimeRunStatus = 'running'\n let completedAtMs: number | undefined\n let resultSummary: string | undefined\n let error: string | undefined\n let completionMetadata: Record<string, unknown> | undefined\n\n const ledger: RuntimeRunCost = {\n tokensIn: 0,\n tokensOut: 0,\n costUsd: 0,\n wallMs: 0,\n llmCalls: 0,\n }\n\n const snapshotCost = (): RuntimeRunCost => ({\n tokensIn: ledger.tokensIn,\n tokensOut: ledger.tokensOut,\n costUsd: ledger.costUsd,\n wallMs: (completedAtMs ?? now()) - startedAtMs,\n llmCalls: ledger.llmCalls,\n })\n\n const buildRow = (extraMetadata?: Record<string, unknown>): RuntimeRunRow => ({\n id,\n workspaceId: options.workspaceId,\n sessionId: options.sessionId,\n agentId: options.agentId,\n domain: options.taskSpec.domain,\n taskId: options.taskSpec.id,\n scenarioId: options.scenarioId,\n status,\n resultSummary,\n error,\n cost: snapshotCost(),\n startedAt,\n completedAt: completedAtMs !== undefined ? new Date(completedAtMs).toISOString() : undefined,\n metadata: mergeMetadata(completionMetadata, extraMetadata),\n })\n\n return {\n id,\n workspaceId: options.workspaceId,\n sessionId: options.sessionId,\n taskSpec: options.taskSpec,\n get status() {\n return status\n },\n observe(event) {\n if (event.type !== 'llm_call') return\n ledger.llmCalls += 1\n if (typeof event.tokensIn === 'number' && Number.isFinite(event.tokensIn)) {\n ledger.tokensIn += event.tokensIn\n }\n if (typeof event.tokensOut === 'number' && Number.isFinite(event.tokensOut)) {\n ledger.tokensOut += event.tokensOut\n }\n if (typeof event.costUsd === 'number' && Number.isFinite(event.costUsd)) {\n ledger.costUsd += event.costUsd\n }\n },\n cost: snapshotCost,\n complete(input) {\n // JS callers can bypass the `Exclude<…, 'running'>` type; enforce the\n // state machine at runtime as well.\n if ((input.status as RuntimeRunStatus) === 'running') {\n throw new ValidationError('complete() requires a terminal status, got \"running\"')\n }\n if (status !== 'running') {\n if (status === input.status) return\n throw new RuntimeRunStateError(\n `Cannot transition runtime run from \"${status}\" to \"${input.status}\"`,\n )\n }\n status = input.status\n completedAtMs = now()\n resultSummary = input.resultSummary\n error = input.error\n completionMetadata = input.metadata\n if (input.cost) {\n if (typeof input.cost.tokensIn === 'number' && Number.isFinite(input.cost.tokensIn)) {\n ledger.tokensIn = input.cost.tokensIn\n }\n if (typeof input.cost.tokensOut === 'number' && Number.isFinite(input.cost.tokensOut)) {\n ledger.tokensOut = input.cost.tokensOut\n }\n if (typeof input.cost.costUsd === 'number' && Number.isFinite(input.cost.costUsd)) {\n ledger.costUsd = input.cost.costUsd\n }\n if (typeof input.cost.llmCalls === 'number' && Number.isFinite(input.cost.llmCalls)) {\n ledger.llmCalls = input.cost.llmCalls\n }\n }\n },\n toRow(metadata) {\n return buildRow(metadata)\n },\n async persist(metadata) {\n if (status === 'running') {\n throw new RuntimeRunStateError('Cannot persist a runtime run before complete() is called')\n }\n if (!options.adapter) return\n await options.adapter.upsert(buildRow(metadata))\n },\n }\n}\n\nfunction mergeMetadata(\n base: Record<string, unknown> | undefined,\n extra: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n if (!base && !extra) return undefined\n return { ...(base ?? {}), ...(extra ?? {}) }\n}\n\nfunction randomSuffix(): string {\n // 8 chars of base36 — sufficient for in-process uniqueness. Callers needing\n // stronger guarantees pass `options.id` explicitly.\n return Math.random().toString(36).slice(2, 10)\n}\n","/**\n *\n * Server-Sent Events serialization for runtime telemetry streams.\n *\n * Newline-safe by construction: any newline in `id` or `event` is collapsed to\n * a space (browsers terminate fields on newline), and multi-line `data`\n * payloads are split into one `data:` line per source line so JSON.stringify\n * output transports cleanly.\n *\n * @stable\n */\n\nimport type { KnowledgeReadinessReport } from '@tangle-network/agent-eval'\nimport type { RuntimeTelemetryOptions } from './sanitize'\nimport { sanitizeKnowledgeReadinessReport, sanitizeRuntimeStreamEvent } from './sanitize'\nimport type { RuntimeStreamEvent } from './types'\n\n/** @stable */\nexport interface ServerSentEventOptions {\n event?: string\n id?: string\n retry?: number\n}\n\n/** @stable */\nexport function encodeServerSentEvent(data: unknown, options: ServerSentEventOptions = {}): string {\n const lines: string[] = []\n if (options.id) lines.push(`id: ${stripNewlines(options.id)}`)\n if (options.event) lines.push(`event: ${stripNewlines(options.event)}`)\n if (typeof options.retry === 'number' && Number.isFinite(options.retry) && options.retry >= 0) {\n lines.push(`retry: ${Math.floor(options.retry)}`)\n }\n\n const payload = typeof data === 'string' ? data : JSON.stringify(data)\n for (const line of payload.split(/\\r?\\n/)) {\n lines.push(`data: ${line}`)\n }\n return `${lines.join('\\n')}\\n\\n`\n}\n\n/** Serialize a `KnowledgeReadinessReport` as a Server-Sent Event string. @stable */\nexport function readinessServerSentEvent(\n report: KnowledgeReadinessReport,\n options: RuntimeTelemetryOptions & ServerSentEventOptions = {},\n): string {\n const { event, id, retry, ...telemetryOptions } = options\n return encodeServerSentEvent(\n {\n type: 'readiness',\n readiness: sanitizeKnowledgeReadinessReport(report, telemetryOptions),\n },\n { event, id, retry },\n )\n}\n\n/** Serialize a `RuntimeStreamEvent` as a Server-Sent Event string. @stable */\nexport function runtimeStreamServerSentEvent(\n event: RuntimeStreamEvent,\n options: RuntimeTelemetryOptions & ServerSentEventOptions = {},\n): string {\n const { event: sseEvent, id, retry, ...telemetryOptions } = options\n return encodeServerSentEvent(sanitizeRuntimeStreamEvent(event, telemetryOptions), {\n event: sseEvent,\n id,\n retry,\n })\n}\n\nfunction stripNewlines(value: string): string {\n return value.replace(/[\\r\\n]/g, ' ')\n}\n","/**\n * Bounded turn-level tool-dispatch loop.\n *\n * `runAgentTaskStream` runs ONE model turn; `runAgentRounds` orchestrates DELEGATED\n * multi-agent topologies (refine / fanout-vote). Neither is the everyday\n * interactive shape: a chat turn where the model may emit tool calls, each is\n * executed, the results are folded back, and the turn re-runs until the model\n * stops (or a turn cap). Every agent app hand-rolls that loop — this is it,\n * as a reusable primitive.\n *\n * Substrate-neutral by design: the caller supplies `streamTurn` (wrapping\n * whatever backend / `runAgentTaskStream` it uses) and `executeToolCall`\n * (routing to its executors). This module owns the LOOP; the caller owns the\n * model and the executors. `Raw` (streaming variant) is the caller's own\n * event type. The only imported contract is the runtime hook type: hooks are\n * execution-scoped observers, not part of the agent profile.\n */\n\nimport type { RuntimeDecisionEvidenceRef, RuntimeHooks } from './runtime-hooks'\nimport { notifyRuntimeDecisionPoint, notifyRuntimeHookEvent } from './runtime-hooks'\n\nexport interface ToolLoopCall {\n toolCallId?: string\n toolName: string\n args: Record<string, unknown>\n}\n\n/** Outcome of one tool dispatch — structurally compatible with a hub/integration\n * tool-outcome union, so callers can fold either through the loop. */\nexport type ToolCallOutcome =\n | { ok: true; result: unknown }\n | { ok: false; code: string; message: string; status?: number }\n\n/** Runaway-backstop: stops an infinite tool loop where cost is unmetered. Set\n * far above any legitimate workflow — this is a watchdog, not a policy cap.\n * Legitimate per-call budgets come from `maxCostUsd` + `costOf`. */\nconst RUNAWAY_BACKSTOP_TURNS = 200\nconst DEFAULT_DECISION_CONTEXT_CHARS = 12_000\nconst FAILURE_RECOVERY_ACTIONS = ['retry', 'verify', 'continue', 'stop']\n/** Consecutive identical calls (same tool + canonical-JSON args) that trigger\n * stuck-loop detection. The window resets on any different call. */\nconst STUCK_LOOP_THRESHOLD = 3\n\n/** One OpenAI-shaped tool-call entry carried on an assistant message. */\nexport interface ToolLoopAssistantToolCall {\n id: string\n type: 'function'\n function: { name: string; arguments: string }\n}\n\n/**\n * A message in the running conversation the loop sends to `streamTurn`.\n *\n * The base `{ role, content }` covers `system` / `user` / plain `assistant`\n * turns. Two optional fields carry the OpenAI function-calling contract so a\n * strict model (Claude, and any OpenAI-compatible provider that validates tool\n * history) reads its own tool use back instead of re-issuing the same call:\n *\n * - an assistant turn that emitted tool calls carries `tool_calls`, and its\n * `content` is `null` when the turn was tool-only;\n * - each tool result is its own `{ role: 'tool', tool_call_id, content }`\n * message keyed to the call that produced it.\n *\n * Widening is additive: a `streamTurn` that reads only `role` + `content` still\n * works; one that forwards the whole message to an OpenAI-compatible endpoint\n * now sends correct tool history.\n */\nexport type ToolLoopMessage = {\n role: string\n content: string | null\n tool_calls?: ToolLoopAssistantToolCall[]\n tool_call_id?: string\n}\n\n/** A tool-call id is required to key a `role: 'tool'` result back to its call.\n * When the model omitted one, derive a stable id from the tool name so the\n * assistant `tool_calls` entry and its `tool` result still match. */\nfunction toolCallId(call: ToolLoopCall): string {\n return call.toolCallId ?? `call_${call.toolName}`\n}\n\n/** The assistant turn that emitted `pending`, in OpenAI shape: text content\n * (null when the turn was tool-only) plus its `tool_calls` array. */\nfunction assistantToolCallMessage(turnText: string, pending: ToolLoopCall[]): ToolLoopMessage {\n return {\n role: 'assistant',\n content: turnText.trim() || null,\n tool_calls: pending.map((call) => ({\n id: toolCallId(call),\n type: 'function',\n function: { name: call.toolName, arguments: JSON.stringify(call.args) },\n })),\n }\n}\n\n/** One `role: 'tool'` result message keyed to its call by `tool_call_id`. */\nfunction toolResultMessage(call: ToolLoopCall, content: string): ToolLoopMessage {\n return { role: 'tool', tool_call_id: toolCallId(call), content }\n}\n\nfunction defaultRender(label: string, outcome: ToolCallOutcome): string {\n if (outcome.ok) return `- ${label} → ok: ${JSON.stringify(outcome.result)}`\n return `- ${label} → failed (${outcome.code}): ${outcome.message}`\n}\n\n// ── Awaitable variant (drain-only callers, tests) ──────────────────────────\n\nexport type ToolLoopEvent =\n | { type: 'text'; text: string }\n | { type: 'tool_call'; call: ToolLoopCall }\n | { type: 'other'; event: unknown }\n\n/** Why the loop stopped. `completed` = model finished naturally; `stuck-loop` =\n * ≥3 consecutive identical tool calls (same tool + args); `backstop` = hit the\n * runaway-backstop cap (200 by default); `deadline` = wall-clock deadlineMs\n * exceeded; `budget` = maxCostUsd exhausted. Non-`completed` stops are infra /\n * resource outcomes — eval scoring must distinguish them from capability failure. */\nexport type ToolLoopStopReason = 'completed' | 'stuck-loop' | 'backstop' | 'deadline' | 'budget'\n\nexport interface ToolLoopResult {\n finalText: string\n toolResults: Array<{ call: ToolLoopCall; label: string; outcome: ToolCallOutcome }>\n turns: number\n stopReason: ToolLoopStopReason\n /** @deprecated Use `stopReason !== 'completed'` instead. */\n cappedOut: boolean\n}\n\nexport interface RunToolLoopOptions {\n systemPrompt: string\n userMessage: string\n priorMessages?: ToolLoopMessage[]\n streamTurn: (messages: ToolLoopMessage[]) => AsyncIterable<ToolLoopEvent>\n executeToolCall: (call: ToolLoopCall) => Promise<ToolCallOutcome>\n isExecutableTool: (toolName: string) => boolean\n /** Runaway-backstop cap. Default 200 — set far above any legitimate workflow.\n * For per-workflow limits, use `maxCostUsd` or `deadlineMs` instead. */\n maxToolTurns?: number\n /** Wall-clock deadline in ms since epoch (Date.now()-based). When exceeded the\n * loop stops with stopReason `deadline`. */\n deadlineMs?: number\n /** Maximum total cost in USD. Requires `costOf` to meter each tool call. */\n maxCostUsd?: number\n /** Return the USD cost of one outcome. Required for `maxCostUsd` to work. */\n costOf?: (call: ToolLoopCall, outcome: ToolCallOutcome) => number\n renderResult?: (label: string, outcome: ToolCallOutcome) => string\n labelFor?: (call: ToolLoopCall) => string\n runId?: string\n scenarioId?: string\n hooks?: RuntimeHooks\n}\n\n/** Run the bounded tool loop and return the final text + every executed tool\n * outcome. Awaitable — callers needing to stream events to a UI use\n * {@link streamToolLoop}. */\nexport async function runToolLoop(opts: RunToolLoopOptions): Promise<ToolLoopResult> {\n const backstop = opts.maxToolTurns ?? RUNAWAY_BACKSTOP_TURNS\n const render = opts.renderResult ?? defaultRender\n const labelFor = opts.labelFor ?? ((c: ToolLoopCall) => c.toolName)\n const runId = opts.runId ?? `agent-run-${randomSuffix()}`\n const messages: ToolLoopMessage[] = [\n { role: 'system', content: opts.systemPrompt },\n ...(opts.priorMessages ?? []),\n { role: 'user', content: opts.userMessage },\n ]\n const observer = createToolLoopObserver(opts.hooks, runId, opts.scenarioId)\n const toolResults: ToolLoopResult['toolResults'] = []\n let finalText = ''\n let turns = 0\n let accumulatedCostUsd = 0\n // Stuck-loop detection: track the last canonical-JSON call signature and how\n // many consecutive times we've seen it.\n let lastCallHash: string | null = null\n let consecutiveCount = 0\n\n observer.loopBefore(backstop, messages.length)\n\n for (let toolTurn = 0; ; toolTurn++) {\n turns++\n\n // Wall-clock deadline check — before every new turn.\n if (opts.deadlineMs !== undefined && Date.now() >= opts.deadlineMs) {\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'deadline' })\n return { finalText, toolResults, turns, stopReason: 'deadline', cappedOut: true }\n }\n\n let turnText = ''\n const pending: ToolLoopCall[] = []\n const turnEventId = observer.turnBefore(toolTurn, messages.length)\n for await (const ev of opts.streamTurn([...messages])) {\n if (ev.type === 'text') {\n turnText += ev.text\n finalText += ev.text\n } else if (ev.type === 'tool_call' && opts.isExecutableTool(ev.call.toolName)) {\n pending.push(ev.call)\n }\n }\n if (pending.length === 0) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: 0,\n finalTextChars: finalText.length,\n })\n break\n }\n\n // Runaway backstop — the model keeps emitting calls past the safety ceiling.\n if (toolTurn >= backstop) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'backstop',\n })\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'backstop' })\n return { finalText, toolResults, turns, stopReason: 'backstop', cappedOut: true }\n }\n\n // The assistant turn that emitted the calls, carrying its tool_calls array,\n // so a strict model reads its own tool use back in OpenAI shape.\n messages.push(assistantToolCallMessage(turnText, pending))\n const outcomes: ExecutedToolCall[] = []\n for (const [callIndex, call] of pending.entries()) {\n // Stuck-loop detection: hash the first pending call each turn (a model\n // stuck in a loop re-issues the same call repeatedly, not alternating calls).\n const callHash = canonicalCallHash(call)\n if (callHash === lastCallHash) {\n consecutiveCount++\n } else {\n lastCallHash = callHash\n consecutiveCount = 1\n }\n if (consecutiveCount >= STUCK_LOOP_THRESHOLD) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'stuck-loop',\n })\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'stuck-loop' })\n return { finalText, toolResults, turns, stopReason: 'stuck-loop', cappedOut: true }\n }\n\n const callEventId = observer.toolCallBefore(toolTurn, turnEventId, callIndex, call)\n let outcome: ToolCallOutcome\n try {\n outcome = await opts.executeToolCall(call)\n } catch (err) {\n outcome = {\n ok: false,\n code: 'executor_error',\n message: err instanceof Error ? err.message : String(err),\n }\n }\n\n // Budget check after each tool call.\n if (opts.maxCostUsd !== undefined && opts.costOf !== undefined) {\n accumulatedCostUsd += opts.costOf(call, outcome)\n if (accumulatedCostUsd >= opts.maxCostUsd) {\n const label = labelFor(call)\n toolResults.push({ call, label, outcome })\n messages.push(toolResultMessage(call, render(label, outcome)))\n observer.toolCallAfter(toolTurn, callEventId, call, outcome)\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'budget',\n })\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'budget' })\n return { finalText, toolResults, turns, stopReason: 'budget', cappedOut: true }\n }\n }\n\n const label = labelFor(call)\n const rendered = render(label, outcome)\n toolResults.push({ call, label, outcome })\n outcomes.push({ call, label, outcome, rendered })\n // One role:'tool' message per result, keyed to its call by tool_call_id.\n messages.push(toolResultMessage(call, rendered))\n observer.toolCallAfter(toolTurn, callEventId, call, outcome)\n }\n observer.failureRecovery({\n toolTurn,\n messages,\n turnText,\n outcomes,\n })\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n toolResults: outcomes.map((item) => ({\n toolName: item.call.toolName,\n toolCallId: item.call.toolCallId,\n ok: item.outcome.ok,\n })),\n failedToolCalls: outcomes.filter((item) => !item.outcome.ok).length,\n })\n }\n observer.loopAfter({ turns, toolResults: toolResults.length, stopReason: 'completed' })\n return { finalText, toolResults, turns, stopReason: 'completed', cappedOut: false }\n}\n\n// ── Streaming variant (SSE chat runtimes + per-event telemetry) ────────────\n\nexport type StreamToolLoopYield<Raw> =\n | { kind: 'event'; event: Raw }\n | {\n kind: 'tool_result'\n toolName: string\n toolCallId?: string\n label: string\n outcome: ToolCallOutcome\n }\n | { kind: 'capped'; pending: number; stopReason: Exclude<ToolLoopStopReason, 'completed'> }\n\nexport interface StreamToolLoopOptions<Raw> {\n systemPrompt: string\n userMessage: string\n priorMessages?: ToolLoopMessage[]\n streamTurn: (messages: ToolLoopMessage[]) => AsyncIterable<Raw>\n extractText: (event: Raw) => string\n extractToolCall: (event: Raw) => ToolLoopCall | null\n isExecutableTool: (toolName: string) => boolean\n executeToolCall: (call: ToolLoopCall) => Promise<ToolCallOutcome>\n /** Runaway-backstop cap. Default 200 — set far above any legitimate workflow. */\n maxToolTurns?: number\n /** Wall-clock deadline in ms since epoch (Date.now()-based). */\n deadlineMs?: number\n /** Maximum total cost in USD. Requires `costOf` to meter each tool call. */\n maxCostUsd?: number\n /** Return the USD cost of one outcome. Required for `maxCostUsd` to work. */\n costOf?: (call: ToolLoopCall, outcome: ToolCallOutcome) => number\n renderResult?: (label: string, outcome: ToolCallOutcome) => string\n labelFor?: (call: ToolLoopCall) => string\n runId?: string\n scenarioId?: string\n hooks?: RuntimeHooks\n}\n\n/** Streaming bounded tool loop: yields each raw turn event (the caller maps +\n * telemetries + re-emits it) and each executed `tool_result`; emits one\n * `capped` if it stops for any non-completed reason with calls still pending. */\nexport async function* streamToolLoop<Raw>(\n opts: StreamToolLoopOptions<Raw>,\n): AsyncGenerator<StreamToolLoopYield<Raw>, void, unknown> {\n const backstop = opts.maxToolTurns ?? RUNAWAY_BACKSTOP_TURNS\n const render = opts.renderResult ?? defaultRender\n const labelFor = opts.labelFor ?? ((c: ToolLoopCall) => c.toolName)\n const runId = opts.runId ?? `agent-run-${randomSuffix()}`\n const messages: ToolLoopMessage[] = [\n { role: 'system', content: opts.systemPrompt },\n ...(opts.priorMessages ?? []),\n { role: 'user', content: opts.userMessage },\n ]\n const observer = createToolLoopObserver(opts.hooks, runId, opts.scenarioId)\n let accumulatedCostUsd = 0\n let lastCallHash: string | null = null\n let consecutiveCount = 0\n\n observer.loopBefore(backstop, messages.length)\n\n for (let toolTurn = 0; ; toolTurn++) {\n // Wall-clock deadline check before every new turn.\n if (opts.deadlineMs !== undefined && Date.now() >= opts.deadlineMs) {\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'deadline' })\n yield { kind: 'capped', pending: 0, stopReason: 'deadline' }\n return\n }\n\n let turnText = ''\n const pending: ToolLoopCall[] = []\n const turnEventId = observer.turnBefore(toolTurn, messages.length)\n for await (const event of opts.streamTurn([...messages])) {\n yield { kind: 'event', event }\n turnText += opts.extractText(event)\n const call = opts.extractToolCall(event)\n if (call && opts.isExecutableTool(call.toolName)) pending.push(call)\n }\n if (pending.length === 0) {\n observer.turnAfter(toolTurn, turnEventId, { pendingToolCalls: 0 })\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'completed' })\n return\n }\n\n // Runaway backstop.\n if (toolTurn >= backstop) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'backstop',\n })\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'backstop' })\n yield { kind: 'capped', pending: pending.length, stopReason: 'backstop' }\n return\n }\n\n // The assistant turn that emitted the calls, carrying its tool_calls array,\n // so a strict model reads its own tool use back in OpenAI shape.\n messages.push(assistantToolCallMessage(turnText, pending))\n const outcomes: ExecutedToolCall[] = []\n for (const [callIndex, call] of pending.entries()) {\n // Stuck-loop detection.\n const callHash = canonicalCallHash(call)\n if (callHash === lastCallHash) {\n consecutiveCount++\n } else {\n lastCallHash = callHash\n consecutiveCount = 1\n }\n if (consecutiveCount >= STUCK_LOOP_THRESHOLD) {\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'stuck-loop',\n })\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'stuck-loop' })\n yield { kind: 'capped', pending: pending.length, stopReason: 'stuck-loop' }\n return\n }\n\n const callEventId = observer.toolCallBefore(toolTurn, turnEventId, callIndex, call)\n let outcome: ToolCallOutcome\n try {\n outcome = await opts.executeToolCall(call)\n } catch (err) {\n outcome = {\n ok: false,\n code: 'executor_error',\n message: err instanceof Error ? err.message : String(err),\n }\n }\n\n // Budget check after each tool call.\n if (opts.maxCostUsd !== undefined && opts.costOf !== undefined) {\n accumulatedCostUsd += opts.costOf(call, outcome)\n if (accumulatedCostUsd >= opts.maxCostUsd) {\n const label = labelFor(call)\n yield {\n kind: 'tool_result',\n toolName: call.toolName,\n toolCallId: call.toolCallId,\n label,\n outcome,\n }\n messages.push(toolResultMessage(call, render(label, outcome)))\n observer.toolCallAfter(toolTurn, callEventId, call, outcome)\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n stopReason: 'budget',\n })\n observer.loopAfter({ turns: toolTurn + 1, stopReason: 'budget' })\n yield { kind: 'capped', pending: pending.length, stopReason: 'budget' }\n return\n }\n }\n\n const label = labelFor(call)\n yield {\n kind: 'tool_result',\n toolName: call.toolName,\n toolCallId: call.toolCallId,\n label,\n outcome,\n }\n const rendered = render(label, outcome)\n outcomes.push({ call, label, outcome, rendered })\n // One role:'tool' message per result, keyed to its call by tool_call_id.\n messages.push(toolResultMessage(call, rendered))\n observer.toolCallAfter(toolTurn, callEventId, call, outcome)\n }\n observer.failureRecovery({\n toolTurn,\n messages,\n turnText,\n outcomes,\n })\n observer.turnAfter(toolTurn, turnEventId, {\n pendingToolCalls: pending.length,\n toolResults: outcomes.map((item) => ({\n toolName: item.call.toolName,\n toolCallId: item.call.toolCallId,\n ok: item.outcome.ok,\n })),\n failedToolCalls: outcomes.filter((item) => !item.outcome.ok).length,\n })\n }\n}\n\ninterface ExecutedToolCall {\n call: ToolLoopCall\n label: string\n outcome: ToolCallOutcome\n rendered: string\n}\n\ninterface NotifyToolFailureRecoveryOptions {\n hooks?: RuntimeHooks\n runId: string\n scenarioId?: string\n stepIndex: number\n messages: ToolLoopMessage[]\n turnText: string\n outcomes: ExecutedToolCall[]\n}\n\ninterface NotifyToolLoopEventOptions {\n hooks?: RuntimeHooks\n runId: string\n scenarioId?: string\n target: 'agent.run' | 'agent.turn' | 'agent.tool_call'\n phase: 'before' | 'after' | 'error' | 'event'\n id?: string\n stepIndex?: number\n parentId?: string\n payload?: Record<string, unknown>\n metadata?: Record<string, unknown>\n}\n\ninterface ToolLoopObserver {\n loopBefore(maxToolTurns: number, messageCount: number): void\n loopAfter(payload: Record<string, unknown>): void\n turnBefore(toolTurn: number, messageCount: number): string\n turnAfter(toolTurn: number, turnEventId: string, payload: Record<string, unknown>): void\n toolCallBefore(\n toolTurn: number,\n turnEventId: string,\n callIndex: number,\n call: ToolLoopCall,\n ): string\n toolCallAfter(\n toolTurn: number,\n callEventId: string,\n call: ToolLoopCall,\n outcome: ToolCallOutcome,\n ): void\n failureRecovery(options: {\n toolTurn: number\n messages: ToolLoopMessage[]\n turnText: string\n outcomes: ExecutedToolCall[]\n }): void\n}\n\nfunction createToolLoopObserver(\n hooks: RuntimeHooks | undefined,\n runId: string,\n scenarioId: string | undefined,\n): ToolLoopObserver {\n const loopEventId = `${runId}:agent.run`\n return {\n loopBefore: (maxToolTurns, messageCount) => {\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.run',\n phase: 'before',\n id: `${loopEventId}:before`,\n payload: { maxToolTurns, messageCount },\n })\n },\n loopAfter: (payload) => {\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.run',\n phase: 'after',\n id: `${loopEventId}:after`,\n payload,\n })\n },\n turnBefore: (toolTurn, messageCount) => {\n const turnEventId = `${loopEventId}:${toolTurn}`\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.turn',\n phase: 'before',\n id: turnEventId,\n stepIndex: toolTurn,\n parentId: loopEventId,\n payload: { messageCount },\n })\n return turnEventId\n },\n turnAfter: (toolTurn, turnEventId, payload) => {\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.turn',\n phase: 'after',\n id: `${turnEventId}:after`,\n stepIndex: toolTurn,\n parentId: turnEventId,\n payload,\n })\n },\n toolCallBefore: (toolTurn, turnEventId, callIndex, call) => {\n const callEventId = `${turnEventId}:tool-call:${callIndex}`\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.tool_call',\n phase: 'before',\n id: callEventId,\n stepIndex: toolTurn,\n parentId: turnEventId,\n payload: toolCallPayload(call),\n })\n return callEventId\n },\n toolCallAfter: (toolTurn, callEventId, call, outcome) => {\n notifyToolLoopEvent({\n hooks,\n runId,\n scenarioId,\n target: 'agent.tool_call',\n phase: 'after',\n id: `${callEventId}:after`,\n stepIndex: toolTurn,\n parentId: callEventId,\n payload: { ...toolCallPayload(call), outcome: outcomePayload(outcome) },\n })\n },\n failureRecovery: (options) => {\n notifyToolFailureRecovery({\n hooks,\n runId,\n scenarioId,\n stepIndex: options.toolTurn,\n messages: options.messages,\n turnText: options.turnText,\n outcomes: options.outcomes,\n })\n },\n }\n}\n\nfunction notifyToolLoopEvent(options: NotifyToolLoopEventOptions): void {\n notifyRuntimeHookEvent(options.hooks, {\n id: options.id ?? `${options.runId}:${options.target}:${options.phase}`,\n runId: options.runId,\n scenarioId: options.scenarioId,\n target: options.target,\n phase: options.phase,\n timestamp: Date.now(),\n stepIndex: options.stepIndex,\n parentId: options.parentId,\n payload: options.payload,\n metadata: { producer: 'tool-loop', ...options.metadata },\n })\n}\n\nfunction notifyToolFailureRecovery(options: NotifyToolFailureRecoveryOptions): void {\n const failed = options.outcomes.filter((item) => !item.outcome.ok)\n if (failed.length === 0) return\n\n const evidence: RuntimeDecisionEvidenceRef[] = []\n for (const item of failed) {\n const id = item.call.toolCallId ?? `${options.stepIndex}:${item.label}`\n evidence.push({\n source: 'tool_call',\n id,\n detail: `${item.call.toolName} ${stringifySafe(item.call.args, 2_000)}`,\n metadata: { toolName: item.call.toolName, label: item.label },\n })\n evidence.push({\n source: 'tool_result',\n id: `${id}:result`,\n detail: item.rendered,\n metadata: failureMetadata(item.outcome),\n })\n }\n\n notifyRuntimeDecisionPoint(options.hooks, {\n id: `${options.runId}:agent.turn:${options.stepIndex}:failure-recovery`,\n runId: options.runId,\n scenarioId: options.scenarioId,\n stepIndex: options.stepIndex,\n kind: 'retry',\n candidateActions: [...FAILURE_RECOVERY_ACTIONS],\n context: renderDecisionContext(options.messages, options.turnText, options.outcomes),\n evidence,\n metadata: {\n target: 'failure-recovery',\n source: 'agent.turn',\n failedToolCount: failed.length,\n toolNames: failed.map((item) => item.call.toolName),\n },\n })\n}\n\nfunction toolCallPayload(call: ToolLoopCall): Record<string, unknown> {\n return {\n toolName: call.toolName,\n toolCallId: call.toolCallId,\n argsPreview: stringifySafe(call.args, 2_000),\n }\n}\n\nfunction outcomePayload(outcome: ToolCallOutcome): Record<string, unknown> {\n if (!outcome.ok) {\n return {\n ok: false,\n code: outcome.code,\n message: trimText(outcome.message, 2_000),\n status: outcome.status,\n }\n }\n return {\n ok: true,\n resultPreview: stringifySafe(outcome.result, 2_000),\n }\n}\n\nfunction failureMetadata(outcome: ToolCallOutcome): Record<string, unknown> | undefined {\n if (outcome.ok) return undefined\n return {\n code: outcome.code,\n message: outcome.message,\n status: outcome.status,\n }\n}\n\nfunction renderDecisionContext(\n messages: ToolLoopMessage[],\n turnText: string,\n outcomes: ExecutedToolCall[],\n): string {\n const recent = messages.slice(-6).map((message) => `[${message.role}]\\n${message.content ?? ''}`)\n const assistant = turnText.trim() ? [`[assistant]\\n${turnText}`] : []\n const toolResults = [`[tool results]\\n${outcomes.map((item) => item.rendered).join('\\n')}`]\n return trimText(\n [...recent, ...assistant, ...toolResults].join('\\n\\n'),\n DEFAULT_DECISION_CONTEXT_CHARS,\n )\n}\n\n/** Canonical identifier for a tool call used by stuck-loop detection.\n * Keys are sorted so `{b:1,a:2}` and `{a:2,b:1}` produce the same hash. */\nfunction canonicalCallHash(call: ToolLoopCall): string {\n const sortedArgs = Object.fromEntries(\n Object.entries(call.args).sort(([a], [b]) => a.localeCompare(b)),\n )\n return `${call.toolName}:${JSON.stringify(sortedArgs)}`\n}\n\nfunction stringifySafe(value: unknown, max: number): string {\n let text: string\n try {\n text = JSON.stringify(value) ?? String(value)\n } catch {\n text = String(value)\n }\n return trimText(text, max)\n}\n\nfunction trimText(text: string, max: number): string {\n if (text.length <= max) return text\n return `${text.slice(0, max)}…`\n}\n\nfunction randomSuffix(len = 8): string {\n return Math.random()\n .toString(36)\n .slice(2, 2 + len)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6FA,IAAM,UAAU,IAAI,YAAY;AAEhC,SAAS,WAAW,OAAoC;AACtD,SAAO,QAAQ,OAAO,GAAG,KAAK,UAAU,KAAK,CAAC;AAAA,CAAI;AACpD;AAEA,SAAS,WAAW,SAAiB,MAAsC;AACzE,MAAI,KAAM,SAAQ,MAAM,SAAS,IAAI;AAAA,MAChC,SAAQ,MAAM,OAAO;AAC5B;AAOO,SAAS,eAAe,OAAyC;AACtE,QAAM,MAAM,MAAM,OAAO;AACzB,QAAM,EAAE,UAAU,MAAM,IAAI;AAE5B,QAAM,OAAO,IAAI,eAA2B;AAAA,IAC1C,OAAO,OAAO,eAAe;AAC3B,YAAMA,QAAO,OAAO,UAA0C;AAC5D,mBAAW,QAAQ,WAAW,KAAK,CAAC;AACpC,YAAI,MAAM,SAAS;AACjB,cAAI;AACF,kBAAM,MAAM,QAAQ,KAAK;AAAA,UAC3B,SAAS,KAAK;AACZ,gBAAI,oCAAoC;AAAA,cACtC,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAAA,YACxD,CAAC;AAAA,UACH;AAAA,QACF;AAAA,MACF;AAEA,UAAI;AACF,cAAMA,MAAK;AAAA,UACT,MAAM;AAAA,UACN,MAAM;AAAA,YACJ,WAAW,SAAS;AAAA,YACpB,UAAU,SAAS;AAAA,YACnB,WAAW,SAAS;AAAA,UACtB;AA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