@tangle-network/agent-runtime 0.235.0 → 0.236.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 (53) hide show
  1. package/dist/{activation-BqEP6ExG.d.ts → activation-BRI-PlIJ.d.ts} +2 -2
  2. package/dist/{activation-BJakmnI1.js → activation-i0X3nbzs.js} +2 -2
  3. package/dist/{activation-BJakmnI1.js.map → activation-i0X3nbzs.js.map} +1 -1
  4. package/dist/agent.d.ts +2 -2
  5. package/dist/agent.js +2 -2
  6. package/dist/{coordination-driver-mujMubUb.js → coordination-driver-CVJVrk2v.js} +2 -2
  7. package/dist/{coordination-driver-mujMubUb.js.map → coordination-driver-CVJVrk2v.js.map} +1 -1
  8. package/dist/{delegate-BqN82j4Z.js → delegate-DZzAL4Wm.js} +2 -2
  9. package/dist/{delegate-BqN82j4Z.js.map → delegate-DZzAL4Wm.js.map} +1 -1
  10. package/dist/durable.d.ts +2 -2
  11. package/dist/durable.js +2 -2
  12. package/dist/{graph-B-Sbq3QW.js → graph-BaE-P5QS.js} +3 -3
  13. package/dist/{graph-B-Sbq3QW.js.map → graph-BaE-P5QS.js.map} +1 -1
  14. package/dist/{improvement-cycle-DpX5QVx0.js → improvement-cycle-2RWFyW4D.js} +3 -3
  15. package/dist/{improvement-cycle-DpX5QVx0.js.map → improvement-cycle-2RWFyW4D.js.map} +1 -1
  16. package/dist/{index-DgFsQnzq.d.ts → index-CEWz0yNr.d.ts} +6 -6
  17. package/dist/index.d.ts +6 -6
  18. package/dist/index.js +7 -7
  19. package/dist/intelligence.d.ts +3 -3
  20. package/dist/intelligence.js +3 -3
  21. package/dist/kernel.d.ts +3 -3
  22. package/dist/kernel.js +8 -8
  23. package/dist/{loop-runner-bin-Dyc1IS0f.js → loop-runner-bin-hloQ3MT_.js} +3 -3
  24. package/dist/{loop-runner-bin-Dyc1IS0f.js.map → loop-runner-bin-hloQ3MT_.js.map} +1 -1
  25. package/dist/{loop-runner-bin-3H3Iq9iY.d.ts → loop-runner-bin-tPYqizeb.d.ts} +3 -3
  26. package/dist/loop-runner-bin.d.ts +1 -1
  27. package/dist/loop-runner-bin.js +1 -1
  28. package/dist/mcp/bin.js +3 -3
  29. package/dist/mcp/index.d.ts +5 -5
  30. package/dist/mcp/index.js +4 -4
  31. package/dist/{openai-tools-CoAfqfko.d.ts → openai-tools-DcJtr_0L.d.ts} +2 -2
  32. package/dist/profiles.d.ts +2 -2
  33. package/dist/{provision-supervisor-Wh8JAo-o.js → provision-supervisor-CfT7yEeg.js} +3 -3
  34. package/dist/{provision-supervisor-Wh8JAo-o.js.map → provision-supervisor-CfT7yEeg.js.map} +1 -1
  35. package/dist/{runtime-Dffe3FZC.js → runtime-DqxluIn_.js} +8 -8
  36. package/dist/{runtime-Dffe3FZC.js.map → runtime-DqxluIn_.js.map} +1 -1
  37. package/dist/{server-BdKJxqUd.js → server-BNsUopoF.js} +3 -3
  38. package/dist/{server-BdKJxqUd.js.map → server-BNsUopoF.js.map} +1 -1
  39. package/dist/{stream-agent-turn-r1mtjn0i.d.ts → stream-agent-turn-BYupQNrs.d.ts} +2 -2
  40. package/dist/{structural-rollout-BLTQRy-B.js → structural-rollout-C6FsCpeq.js} +2 -2
  41. package/dist/{structural-rollout-BLTQRy-B.js.map → structural-rollout-C6FsCpeq.js.map} +1 -1
  42. package/dist/{substrate-BfNaAM8h.d.ts → substrate-C7ivCM2O.d.ts} +2 -2
  43. package/dist/{supervise-DMiJQB2_.js → supervise-mvykoSSc.js} +3 -3
  44. package/dist/{supervise-DMiJQB2_.js.map → supervise-mvykoSSc.js.map} +1 -1
  45. package/dist/{supervisor-CBtuIoqP.js → supervisor-DGsHiL1-.js} +6 -2
  46. package/dist/supervisor-DGsHiL1-.js.map +1 -0
  47. package/dist/testing.d.ts +2 -2
  48. package/dist/testing.js +12 -12
  49. package/dist/tui/index.d.ts +1 -1
  50. package/dist/tui/index.js +1 -1
  51. package/dist/{types-BxjpKpb-.d.ts → types-DANs9CAb.d.ts} +15 -5
  52. package/package.json +1 -1
  53. package/dist/supervisor-CBtuIoqP.js.map +0 -1
@@ -1,7 +1,7 @@
1
- import { Qn as buildLoopSpanNodes } from "./supervisor-CBtuIoqP.js";
1
+ import { Qn as buildLoopSpanNodes } from "./supervisor-DGsHiL1-.js";
2
2
  import { m as ValidationError, t as AgentEvalError } from "./errors-DodWX-cb.js";
3
3
  import { n as createStdioToolServer } from "./tool-server-x9NEhvHZ.js";
4
- import { n as delegate } from "./delegate-BqN82j4Z.js";
4
+ import { n as delegate } from "./delegate-DZzAL4Wm.js";
5
5
  import { _ as InMemoryFeedbackStore, c as DELEGATION_HISTORY_TOOL_NAME, f as DELEGATE_FEEDBACK_DESCRIPTION, h as createDelegateFeedbackHandler, i as createDelegationStatusHandler, l as createDelegationHistoryHandler, m as DELEGATE_FEEDBACK_TOOL_NAME, n as DELEGATION_STATUS_INPUT_SCHEMA, o as DELEGATION_HISTORY_DESCRIPTION, p as DELEGATE_FEEDBACK_INPUT_SCHEMA, r as DELEGATION_STATUS_TOOL_NAME, s as DELEGATION_HISTORY_INPUT_SCHEMA, t as DELEGATION_STATUS_DESCRIPTION } from "./delegation-status-CF4D0NZ_.js";
6
6
  import { n as UI_LENSES } from "./substrate-B0TYNrXn.js";
7
7
  import path, { dirname } from "node:path";
@@ -1308,4 +1308,4 @@ function createInProcessTransport() {
1308
1308
  //#endregion
1309
1309
  export { composeLoopTraceEmitters as C, capDelegationTrace as S, FileDelegationStore as _, DELEGATE_UI_AUDIT_TOOL_NAME as a, DELEGATION_TRACE_MAX_SPANS as b, DELEGATE_DESCRIPTION as c, createDelegateHandler as d, validateDelegateArgs as f, DelegationStateCorruptError as g, DelegationPersistenceError as h, DELEGATE_UI_AUDIT_INPUT_SCHEMA as i, DELEGATE_INPUT_SCHEMA as l, hashIdempotencyInput as m, createMcpServer as n, createDelegateUiAuditHandler as o, DelegationTaskQueue as p, DELEGATE_UI_AUDIT_DESCRIPTION as r, validateDelegateUiAuditArgs as s, createInProcessTransport as t, DELEGATE_TOOL_NAME as u, InMemoryDelegationStore as v, createDelegationTraceCollector as w, buildDelegationTraceSpans as x, DELEGATION_TRACE_MAX_BYTES as y };
1310
1310
 
1311
- //# sourceMappingURL=server-BdKJxqUd.js.map
1311
+ //# sourceMappingURL=server-BNsUopoF.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"server-BdKJxqUd.js","names":[],"sources":["../src/mcp/delegation-trace.ts","../src/mcp/delegation-store.ts","../src/mcp/task-queue.ts","../src/mcp/tools/delegate.ts","../src/mcp/tools/delegate-ui-audit.ts","../src/mcp/server.ts"],"sourcesContent":["/**\n *\n * Compact loop-trace tee for the delegation journal.\n *\n * The OTEL exporter ({@link createPropagatingTraceEmitter}) is a no-op\n * without `OTEL_EXPORTER_OTLP_ENDPOINT`, which leaves delegated work streams\n * dark in practice. This module derives the same loop → round → branch span\n * tree (via the shared {@link buildLoopSpanNodes} builder) into a small,\n * JSON-safe shape persisted directly on the `DelegationRecord` — observable\n * through `delegation_status` with no collector infrastructure. Both sinks\n * coexist: the OTEL export path is unchanged.\n *\n * Payload discipline: a record's trace is hard-capped (spans + serialized\n * bytes). Past the cap the OLDEST spans are dropped and the record carries a\n * `traceTruncated: true` marker — truncation is never silent.\n *\n * @experimental\n */\n\nimport { buildLoopSpanNodes } from '../otel-export'\nimport type { LoopTraceEmitter, LoopTraceEvent } from '../runtime/types'\n\n/**\n * One span of a delegation's compact trace. Flat (parent linkage by id), all\n * values JSON-safe scalars — `FileDelegationStore` round-trips records\n * through `JSON.stringify`. `meta` carries the span's attributes (GenAI\n * semconv keys + `tangle.loop.*` extensions) exactly as the OTEL sink emits\n * them, so a consumer can re-export journal traces losslessly.\n *\n * @experimental\n */\nexport interface DelegationTraceSpan {\n spanId: string\n /** Absent on the tree root. */\n parentSpanId?: string\n /** `'loop'` | `'loop.round'` | `'loop.iteration'` (or a sink-specific name). */\n name: string\n /** Topology level: loop root, plan round, or iteration branch. */\n kind: 'loop' | 'round' | 'branch'\n startMs: number\n endMs: number\n meta?: Record<string, string | number | boolean>\n}\n\n/** Default cap on spans retained per delegation record. @experimental */\nexport const DELEGATION_TRACE_MAX_SPANS = 512\n\n/** Default cap on the serialized trace payload per record, in bytes. @experimental */\nexport const DELEGATION_TRACE_MAX_BYTES = 256 * 1024\n\n/** @experimental */\nexport interface DelegationTraceCaps {\n /** Default {@link DELEGATION_TRACE_MAX_SPANS}. */\n maxSpans?: number\n /** Default {@link DELEGATION_TRACE_MAX_BYTES}. Approximate — measured as the\n * sum of per-span `JSON.stringify` lengths. */\n maxBytes?: number\n}\n\n/** @experimental */\nexport interface CappedDelegationTrace {\n trace: DelegationTraceSpan[]\n /** True when oldest spans were dropped to honor the caps. */\n truncated: boolean\n}\n\n/**\n * Derive the compact span tree for ONE loop run from its buffered\n * `LoopTraceEvent` stream. Same reconstruction as the OTEL exporter\n * ({@link buildLoopSpanNodes}); tolerates partial streams.\n *\n * @experimental\n */\nexport function buildDelegationTraceSpans(\n events: ReadonlyArray<LoopTraceEvent>,\n): DelegationTraceSpan[] {\n return buildLoopSpanNodes(events).map((node) => ({\n spanId: node.spanId,\n ...(node.parentSpanId !== undefined ? { parentSpanId: node.parentSpanId } : {}),\n name: node.name,\n kind: node.kind,\n startMs: node.startMs,\n endMs: node.endMs,\n ...(Object.keys(node.attrs).length > 0 ? { meta: node.attrs } : {}),\n }))\n}\n\n/**\n * Enforce the trace caps over an ordered (oldest-first) span list. Drops the\n * OLDEST spans first and reports `truncated: true` when anything was dropped;\n * the newest span always survives, so a non-empty input never caps to empty.\n * Dropping a parent may orphan surviving children's `parentSpanId` references\n * — acceptable for the flat journal shape; consumers treat unresolved parents\n * as roots.\n *\n * @experimental\n */\nexport function capDelegationTrace(\n spans: ReadonlyArray<DelegationTraceSpan>,\n caps?: DelegationTraceCaps,\n): CappedDelegationTrace {\n const maxSpans = caps?.maxSpans ?? DELEGATION_TRACE_MAX_SPANS\n const maxBytes = caps?.maxBytes ?? DELEGATION_TRACE_MAX_BYTES\n let start = Math.max(0, spans.length - maxSpans)\n const sizes = spans.map((span) => JSON.stringify(span).length + 1)\n let total = 0\n for (let i = start; i < sizes.length; i += 1) total += sizes[i]!\n while (start < spans.length - 1 && total > maxBytes) {\n total -= sizes[start]!\n start += 1\n }\n return { trace: spans.slice(start), truncated: start > 0 }\n}\n\n/**\n * Per-delegation trace collector. Buffers `LoopTraceEvent`s per runId\n * (mirroring the OTEL emitter's buffering) and hands the derived compact\n * spans to `onSpans` when a run reaches `loop.ended`. `settle()` drains runs\n * that never ended — a hard-aborted loop still leaves its partial tree in the\n * journal, unlike the OTEL path which drops it.\n *\n * @experimental\n */\nexport interface DelegationTraceCollector {\n emitter: LoopTraceEmitter\n /** Flush buffered events of runs that never reached `loop.ended`. */\n settle(): void\n}\n\n/** Build a `DelegationTraceCollector` that buffers loop-trace events and converts them to spans on settle. @experimental */\nexport function createDelegationTraceCollector(\n onSpans: (spans: DelegationTraceSpan[]) => void,\n): DelegationTraceCollector {\n const buffers = new Map<string, LoopTraceEvent[]>()\n const flush = (events: LoopTraceEvent[]): void => {\n const spans = buildDelegationTraceSpans(events)\n if (spans.length > 0) onSpans(spans)\n }\n return {\n emitter: {\n emit(event: LoopTraceEvent): void {\n const buf = buffers.get(event.runId)\n if (buf) buf.push(event)\n else buffers.set(event.runId, [event])\n if (event.kind === 'loop.ended') {\n const events = buffers.get(event.runId) ?? [event]\n buffers.delete(event.runId)\n flush(events)\n }\n },\n },\n settle(): void {\n for (const events of buffers.values()) flush(events)\n buffers.clear()\n },\n }\n}\n\n/**\n * 16-hex-char span id for journal spans synthesized outside the shared loop\n * builder (e.g. the queue's detached-resume segment).\n *\n * @experimental\n */\nexport function generateDelegationSpanId(): string {\n const bytes = new Uint8Array(8)\n if (typeof globalThis.crypto?.getRandomValues === 'function') {\n globalThis.crypto.getRandomValues(bytes)\n } else {\n for (let i = 0; i < 8; i += 1) bytes[i] = Math.floor(Math.random() * 256)\n }\n return Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, '0'))\n .join('')\n}\n\n/**\n * Fan one `LoopTraceEvent` stream into several emitters — e.g. the\n * process-wide OTEL exporter AND the per-delegation journal collector.\n * `undefined` entries are skipped; returns `undefined` when nothing is left\n * so callers keep the kernel's \"no emitter, no events\" fast path.\n *\n * @experimental\n */\nexport function composeLoopTraceEmitters(\n ...emitters: ReadonlyArray<LoopTraceEmitter | undefined>\n): LoopTraceEmitter | undefined {\n const live = emitters.filter((e): e is LoopTraceEmitter => e !== undefined)\n if (live.length === 0) return undefined\n if (live.length === 1) return live[0]\n return {\n emit(event: LoopTraceEvent): void | Promise<void> {\n const pending: Promise<void>[] = []\n for (const emitter of live) {\n const result = emitter.emit(event)\n if (result) pending.push(result)\n }\n if (pending.length > 0) return Promise.all(pending).then(() => undefined)\n },\n }\n}\n","/**\n *\n * Persistence port for the MCP delegation queue.\n *\n * `DelegationTaskQueue` keeps its working set in memory (status/history\n * reads stay synchronous) and journals every record mutation through a\n * `DelegationStore`. `DelegationTaskQueue.restore({ store })` is the load\n * path: it reads the full record set once at construction and rehydrates\n * the queue from it. After that the store only sees writes.\n *\n * Records MUST be JSON-safe — `FileDelegationStore` round-trips them\n * through `JSON.stringify`/`JSON.parse`, so a `Date`, `Map`, or function\n * smuggled into `args`/`result` would corrupt the journal.\n *\n * @stable\n */\n\nimport { mkdir, readFile, rename, writeFile } from 'node:fs/promises'\nimport { dirname } from 'node:path'\nimport { AgentEvalError } from '../errors'\nimport type { DelegationRecord } from './task-queue'\n\n/** @stable */\nexport interface DelegationStore {\n /**\n * Read every persisted record. Called once, by\n * `DelegationTaskQueue.restore`, before any write. A missing backing\n * file is an empty store; an unparseable one throws\n * `DelegationStateCorruptError`.\n */\n loadAll(): Promise<DelegationRecord[]>\n /** Insert or replace the record keyed by `record.taskId`. */\n upsert(record: DelegationRecord): Promise<void>\n /**\n * Resolve an idempotency key to the taskId that claimed it, if any.\n * The queue serves submit-time dedupe from its rehydrated in-memory\n * index; this read exists for consumers that share a store across\n * processes without holding the full record set.\n */\n lookupIdempotencyKey(key: string): Promise<string | undefined>\n /** Delete the named records — the retention-cap eviction path. */\n remove(taskIds: readonly string[]): Promise<void>\n}\n\n/**\n * The persisted delegation state exists but cannot be parsed into\n * records. Fail loud: silently starting empty over a corrupt journal\n * would erase delegation history and re-run idempotent work. Opt into\n * recovery explicitly via `FileDelegationStoreOptions.recoverCorrupt`\n * (the bin maps `AGENT_RUNTIME_DELEGATION_STATE_RECOVER=1` onto it),\n * which archives the corrupt file and starts fresh.\n *\n * @stable\n */\nexport class DelegationStateCorruptError extends AgentEvalError {\n constructor(message: string, options?: { cause?: unknown }) {\n super('validation', message, options)\n }\n}\n\n/**\n * A delegation-store read or write failed (filesystem error, store\n * called before `loadAll`, ...). Once the queue observes one, it stops\n * accepting new submissions — accepting work it cannot journal would\n * silently demote durable mode to in-memory mode.\n *\n * @stable\n */\nexport class DelegationPersistenceError extends AgentEvalError {\n constructor(message: string, options?: { cause?: unknown }) {\n super('config', message, options)\n }\n}\n\n/** In-memory `DelegationStore` — suitable for single-process use and tests. @stable */\nexport class InMemoryDelegationStore implements DelegationStore {\n private readonly records = new Map<string, DelegationRecord>()\n\n async loadAll(): Promise<DelegationRecord[]> {\n return [...this.records.values()].map(cloneRecord)\n }\n\n async upsert(record: DelegationRecord): Promise<void> {\n this.records.set(record.taskId, cloneRecord(record))\n }\n\n async lookupIdempotencyKey(key: string): Promise<string | undefined> {\n for (const record of this.records.values()) {\n if (record.idempotencyKey === key) return record.taskId\n }\n return undefined\n }\n\n async remove(taskIds: readonly string[]): Promise<void> {\n for (const taskId of taskIds) this.records.delete(taskId)\n }\n}\n\n/** @stable */\nexport interface FileDelegationStoreOptions {\n /** Absolute path of the JSON state file. Parent directories are created on first write. */\n filePath: string\n /**\n * When the state file exists but cannot be parsed, archive it to\n * `<filePath>.corrupt-<timestamp>` and start empty instead of\n * throwing `DelegationStateCorruptError`. Default false.\n */\n recoverCorrupt?: boolean\n}\n\ninterface PersistedDelegationState {\n version: 1\n records: DelegationRecord[]\n}\n\nconst STATE_FORMAT_VERSION = 1\n\n/**\n * JSON-file persistence for the delegation queue. Each write serializes\n * the full record set and lands it atomically (write to a sibling tmp\n * file, then `rename`), so readers never observe a torn file — a crash\n * mid-write leaves the previous snapshot intact. Writes are serialized\n * internally; concurrent `upsert`/`remove` calls cannot interleave.\n *\n * Built for the MCP server's scale (one stdio process, hundreds of\n * records): full-snapshot writes keep the format trivially inspectable\n * and corruption-detectable without a database dependency.\n *\n * @stable\n */\nexport class FileDelegationStore implements DelegationStore {\n private readonly filePath: string\n private readonly recoverCorrupt: boolean\n private readonly records = new Map<string, DelegationRecord>()\n private loaded = false\n private writeTail: Promise<void> = Promise.resolve()\n private tmpSeq = 0\n\n constructor(options: FileDelegationStoreOptions) {\n this.filePath = options.filePath\n this.recoverCorrupt = options.recoverCorrupt ?? false\n }\n\n async loadAll(): Promise<DelegationRecord[]> {\n let raw: string\n try {\n raw = await readFile(this.filePath, 'utf8')\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') {\n this.loaded = true\n return []\n }\n throw new DelegationPersistenceError(\n `FileDelegationStore: failed to read ${this.filePath}: ${errorMessage(err)}`,\n { cause: err },\n )\n }\n let state: PersistedDelegationState\n try {\n state = parsePersistedState(raw)\n } catch (err) {\n if (!this.recoverCorrupt) {\n throw new DelegationStateCorruptError(\n `FileDelegationStore: state file ${this.filePath} is corrupt (${errorMessage(err)}). ` +\n 'Repair or archive the file, or opt into automatic recovery ' +\n '(recoverCorrupt / AGENT_RUNTIME_DELEGATION_STATE_RECOVER=1) to archive it and start empty.',\n { cause: err },\n )\n }\n const archivePath = `${this.filePath}.corrupt-${Date.now()}`\n await rename(this.filePath, archivePath)\n this.loaded = true\n return []\n }\n this.records.clear()\n for (const record of state.records) this.records.set(record.taskId, record)\n this.loaded = true\n return [...this.records.values()].map(cloneRecord)\n }\n\n async upsert(record: DelegationRecord): Promise<void> {\n this.assertLoaded('upsert')\n this.records.set(record.taskId, cloneRecord(record))\n await this.enqueueWrite()\n }\n\n async lookupIdempotencyKey(key: string): Promise<string | undefined> {\n this.assertLoaded('lookupIdempotencyKey')\n for (const record of this.records.values()) {\n if (record.idempotencyKey === key) return record.taskId\n }\n return undefined\n }\n\n async remove(taskIds: readonly string[]): Promise<void> {\n this.assertLoaded('remove')\n let changed = false\n for (const taskId of taskIds) {\n if (this.records.delete(taskId)) changed = true\n }\n if (changed) await this.enqueueWrite()\n }\n\n private assertLoaded(op: string): void {\n if (this.loaded) return\n // Writing before load would snapshot only the new records and clobber\n // whatever the file already holds.\n throw new DelegationPersistenceError(\n `FileDelegationStore: ${op} called before loadAll() — the on-disk state has not been read yet`,\n )\n }\n\n private enqueueWrite(): Promise<void> {\n const write = this.writeTail.then(() => this.writeSnapshot())\n this.writeTail = write.catch(() => {})\n return write\n }\n\n private async writeSnapshot(): Promise<void> {\n const state: PersistedDelegationState = {\n version: STATE_FORMAT_VERSION,\n records: [...this.records.values()],\n }\n const payload = `${JSON.stringify(state)}\\n`\n this.tmpSeq += 1\n const tmpPath = `${this.filePath}.tmp-${process.pid}-${this.tmpSeq}`\n try {\n await mkdir(dirname(this.filePath), { recursive: true })\n await writeFile(tmpPath, payload, 'utf8')\n await rename(tmpPath, this.filePath)\n } catch (err) {\n throw new DelegationPersistenceError(\n `FileDelegationStore: failed to write ${this.filePath}: ${errorMessage(err)}`,\n { cause: err },\n )\n }\n }\n}\n\nfunction parsePersistedState(raw: string): PersistedDelegationState {\n const parsed: unknown = JSON.parse(raw)\n if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {\n throw new Error('top-level value is not an object')\n }\n const state = parsed as Record<string, unknown>\n if (state.version !== STATE_FORMAT_VERSION) {\n throw new Error(`unsupported state version ${JSON.stringify(state.version)}`)\n }\n if (!Array.isArray(state.records)) {\n throw new Error('`records` is not an array')\n }\n for (const record of state.records) {\n if (record === null || typeof record !== 'object') {\n throw new Error('a record entry is not an object')\n }\n const candidate = record as Record<string, unknown>\n if (typeof candidate.taskId !== 'string' || typeof candidate.status !== 'string') {\n throw new Error('a record entry is missing `taskId`/`status`')\n }\n }\n return { version: STATE_FORMAT_VERSION, records: state.records as DelegationRecord[] }\n}\n\nfunction cloneRecord(record: DelegationRecord): DelegationRecord {\n return structuredClone(record)\n}\n\nfunction errorMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n","/**\n *\n * State machine for async MCP delegations:\n *\n * pending → running → completed | failed\n * ↘ cancelled (from any non-terminal state via cancel())\n *\n * Each `submit` returns a `taskId` immediately and kicks the work off in the\n * background. The work function receives an `AbortSignal` the queue fires\n * when `cancel(taskId)` is called. The queue does NOT supervise runtime\n * timeouts — the underlying `runAgentRounds` driver / sandbox imposes those.\n *\n * Idempotency: callers may supply an `idempotencyKey` (hash of the input).\n * A duplicate `submit` with a known key returns the existing task instead of\n * starting a new one. Mutated input → different key → different task.\n *\n * Durability: the working set lives in memory (reads stay synchronous) and\n * every record mutation is journaled through a `DelegationStore`. The default\n * `InMemoryDelegationStore` keeps today's semantics — a process restart drops\n * all state. Construct via `DelegationTaskQueue.restore({ store })` with a\n * `FileDelegationStore` to reload prior records on startup: terminal records\n * stay queryable, in-flight records either re-attach through the\n * `resumeDelegate` seam (when they carry a `detachedSessionRef`) or fail\n * loud with a driver-restart error so `delegation_status` tells the truth.\n *\n * @stable\n */\n\nimport { ValidationError } from '../errors'\nimport type { LoopTraceEmitter } from '../runtime/types'\nimport {\n DelegationPersistenceError,\n type DelegationStore,\n InMemoryDelegationStore,\n} from './delegation-store'\nimport {\n capDelegationTrace,\n createDelegationTraceCollector,\n type DelegationTraceSpan,\n generateDelegationSpanId,\n} from './delegation-trace'\nimport type { TraceContext } from './trace-propagation'\nimport type {\n DelegateCodeArgs,\n DelegateResearchArgs,\n DelegateUiAuditArgs,\n DelegationError,\n DelegationFeedbackSnapshot,\n DelegationHistoryArgs,\n DelegationHistoryEntry,\n DelegationProfile,\n DelegationProgress,\n DelegationResultPayload,\n DelegationStatus,\n DelegationStatusResult,\n} from './types'\n\n/** Arguments accepted by the durable delegation queue. @stable */\nexport type DelegationArgs = DelegateCodeArgs | DelegateResearchArgs | DelegateUiAuditArgs\n\n/**\n * Must be JSON-safe end to end (`args`, `result`, `error`, `feedback`) —\n * persistent stores round-trip records through `JSON.stringify`.\n *\n * @stable\n */\nexport interface DelegationRecord {\n taskId: string\n profile: DelegationProfile\n namespace?: string\n args: DelegationArgs\n status: DelegationStatus\n progress?: DelegationProgress\n result?: DelegationResultPayload\n error?: DelegationError\n costUsd?: number\n startedAt: string\n completedAt?: string\n /** Sha-prefix hash of the canonical input — used for idempotency lookup. */\n idempotencyKey?: string\n /**\n * Caller-generated deterministic id of a detached run (e.g. the sandbox\n * session id a single-tick driver resumes by). Presence is what makes a\n * restored in-flight record resumable via `resumeDelegate`; without it a\n * restart settles the record as failed.\n */\n detachedSessionRef?: string\n /** Feedback events keyed by this delegation's taskId. */\n feedback: DelegationFeedbackSnapshot[]\n /**\n * Compact loop-trace span tree teed from the delegation's run, oldest\n * spans first. Appended when a delegated loop reaches `loop.ended` and\n * settled (partial buffers included) at the terminal transition. Capped\n * via `capDelegationTrace` — see `traceTruncated`.\n */\n trace?: DelegationTraceSpan[]\n /** Present when oldest trace spans were dropped to honor the trace caps. */\n traceTruncated?: true\n /**\n * Inherited trace identity (the queue's `traceContext` at submit time —\n * typically `readTraceContextFromEnv()`), distinct from the span payload:\n * a journal consumer joins records into the parent trace by these ids\n * without parsing spans. Restored records keep their persisted identity.\n */\n traceId?: string\n /** Caller span that dispatched the delegation, when one was inherited. */\n parentSpanId?: string\n}\n\n/** @stable */\nexport interface SubmitInput<Args extends DelegationArgs> {\n profile: DelegationProfile\n args: Args\n namespace?: string\n idempotencyKey?: string\n /**\n * Records the detached-run resume key on the new record. The submitted\n * `run` function still executes in-process exactly as without it — the\n * ref only matters after a restart, when `DelegationTaskQueue.restore`\n * hands it to the `resumeDelegate` seam instead of failing the record.\n */\n detachedSessionRef?: string\n /**\n * Runs the underlying delegation. The queue passes a fresh `AbortSignal`\n * and a `report` channel for incremental progress updates. The function\n * MUST resolve with the typed `DelegationResultPayload['output']`; the\n * queue wraps it with the profile tag.\n */\n run: (ctx: DelegationRunContext) => Promise<DelegationResultPayload['output']>\n}\n\n/** @stable Context handed to a `SubmitInput.run` function. */\nexport interface DelegationRunContext {\n signal: AbortSignal\n report(progress: DelegationProgress): void\n /** The `detachedSessionRef` recorded at submit, when one was supplied. */\n detachedSessionRef?: string\n /**\n * Replace the record's detached-run resume key — the detached dispatch path\n * calls this once the sandbox id is known so the persisted ref names a\n * resolvable box. Ignored after the record settles (a cancel racing the\n * rebind is legitimate; the ref no longer matters then). Throws on an empty\n * ref — erasing the resume key would silently make the record unresumable.\n */\n updateDetachedSessionRef(ref: string): void\n /**\n * Per-delegation loop-trace sink, always provided by the queue. Events\n * emitted here are journaled onto the record as a compact span tree\n * (`record.trace`) when each loop run ends and at the delegation's\n * terminal transition. Delegates forward it into their `runAgentRounds` ctx,\n * composed with any process-wide OTEL emitter\n * (`composeLoopTraceEmitters`). Optional in the type so consumer-built\n * contexts stay source-compatible.\n */\n traceEmitter?: LoopTraceEmitter\n}\n\n/** @stable */\nexport interface SubmitOutput {\n taskId: string\n /** True when a prior matching `idempotencyKey` returned an existing record. */\n reused: boolean\n}\n\n/**\n * One observation of a detached run, mapped 1:1 from a single-tick driver\n * (e.g. the sandbox SDK's `driveTurn`, which reports\n * completed | running | failed per pass). `running` schedules another tick\n * after `intervalMs`; `completed` / `failed` settle the record.\n *\n * @stable\n */\nexport type DelegationResumeTick =\n | { state: 'running' }\n | { state: 'completed'; output: DelegationResultPayload['output']; costUsd?: number }\n | { state: 'failed'; error: DelegationError }\n\n/** @stable */\nexport interface DelegationResumeContext {\n /** Fired by `cancel(taskId)`; the driver should stop the remote run when it can. */\n signal: AbortSignal\n report(progress: DelegationProgress): void\n}\n\n/**\n * Re-attaches restored in-flight records to their detached runs. The queue\n * calls `tick` repeatedly — it never awaits a whole run — so the driver can\n * be a thin wrapper over a one-pass primitive: resolve the run named by\n * `detachedSessionRef`, advance/poll it once, report where it stands. A\n * thrown error settles the record as failed; `failed` ticks are treated as\n * terminal and are not retried.\n *\n * @stable\n */\nexport interface DelegationResumeDriver {\n tick(\n task: { record: DelegationRecord; detachedSessionRef: string },\n ctx: DelegationResumeContext,\n ): Promise<DelegationResumeTick>\n /** Delay between `running` ticks, in milliseconds. Default 5000. */\n intervalMs?: number\n}\n\n/** @stable */\nexport interface DelegationTaskQueueOptions {\n /** ID generator override; default `randomTaskId`. */\n generateId?: () => string\n /** Clock override; default `() => new Date().toISOString()`. */\n now?: () => string\n /**\n * Journal for record mutations and the `restore()` load source. Default\n * `InMemoryDelegationStore` — observably identical to an unjournaled\n * queue. Pass a `FileDelegationStore` through\n * `DelegationTaskQueue.restore` for state that survives a restart;\n * constructing with `new` never loads prior state.\n */\n store?: DelegationStore\n /** Resume seam for restored in-flight records that carry a `detachedSessionRef`. */\n resumeDelegate?: DelegationResumeDriver\n /**\n * Maximum number of terminal (completed | failed | cancelled) records\n * retained; the oldest (by `completedAt`) are evicted from memory and\n * store once the cap is exceeded. Default unbounded.\n */\n maxTerminalRecords?: number\n /**\n * Observes the first store failure. After it fires, the queue refuses\n * new submissions and `flush()` rejects with the same error. Default:\n * rethrow on a microtask — an unhandled crash — because silently\n * degrading durable mode to memory-only would lie to the caller.\n */\n onPersistError?: (error: DelegationPersistenceError) => void\n /**\n * Inherited trace identity stamped on every submitted record\n * (`traceId` / `parentSpanId`). The bin passes\n * `readTraceContextFromEnv()` so journal consumers can join delegation\n * records into the caller's trace. Restored records keep the identity\n * they were persisted with.\n */\n traceContext?: TraceContext\n}\n\n/** In-process queue for async delegation tasks — submit, cancel, poll status, and read history. @stable */\nexport class DelegationTaskQueue {\n private readonly records = new Map<string, DelegationRecord>()\n private readonly controllers = new Map<string, AbortController>()\n private readonly byIdempotencyKey = new Map<string, string>()\n private readonly generateId: () => string\n private readonly now: () => string\n private readonly store: DelegationStore\n private readonly resumeDelegate?: DelegationResumeDriver\n private readonly maxTerminalRecords: number\n private readonly onPersistError: (error: DelegationPersistenceError) => void\n private readonly traceContext: TraceContext | undefined\n private persistTail: Promise<void> = Promise.resolve()\n private persistFailure: DelegationPersistenceError | undefined\n\n constructor(options: DelegationTaskQueueOptions = {}) {\n this.generateId = options.generateId ?? randomTaskId\n this.now = options.now ?? (() => new Date().toISOString())\n this.store = options.store ?? new InMemoryDelegationStore()\n this.resumeDelegate = options.resumeDelegate\n if (options.maxTerminalRecords !== undefined) {\n if (!Number.isInteger(options.maxTerminalRecords) || options.maxTerminalRecords < 1) {\n throw new ValidationError(\n `DelegationTaskQueue: maxTerminalRecords must be a positive integer, got ${String(options.maxTerminalRecords)}`,\n )\n }\n }\n this.maxTerminalRecords = options.maxTerminalRecords ?? Number.POSITIVE_INFINITY\n this.traceContext = options.traceContext\n this.onPersistError =\n options.onPersistError ??\n ((error) => {\n queueMicrotask(() => {\n throw error\n })\n })\n }\n\n /**\n * Construct a queue from previously-persisted state. Loads every record\n * from `options.store`, rebuilds the idempotency index (so a re-submitted\n * identical task returns the prior taskId and its terminal state), then:\n *\n * - terminal records stay queryable via `status()` / `history()`\n * - in-flight records with a `detachedSessionRef` re-attach through\n * `options.resumeDelegate` and report `running`\n * - other in-flight records settle as failed — their driver died with\n * the previous process and the result is unrecoverable\n *\n * The retention cap applies to the loaded set as well.\n */\n static async restore(options: DelegationTaskQueueOptions = {}): Promise<DelegationTaskQueue> {\n const queue = new DelegationTaskQueue(options)\n const loaded = await queue.store.loadAll()\n await queue.rehydrate(loaded)\n return queue\n }\n\n /**\n * Kick off a delegation in the background. Returns immediately. The\n * `taskId` is queryable via `status` once this method returns. Throws\n * the recorded `DelegationPersistenceError` once the store has failed —\n * the queue does not accept work it cannot journal.\n */\n submit<Args extends DelegationArgs>(input: SubmitInput<Args>): SubmitOutput {\n if (this.persistFailure) throw this.persistFailure\n if (input.idempotencyKey) {\n const existing = this.byIdempotencyKey.get(input.idempotencyKey)\n if (existing && this.records.has(existing)) {\n return { taskId: existing, reused: true }\n }\n }\n const taskId = this.generateId()\n const controller = new AbortController()\n const record: DelegationRecord = {\n taskId,\n profile: input.profile,\n namespace: input.namespace,\n args: input.args,\n status: 'pending',\n startedAt: this.now(),\n feedback: [],\n idempotencyKey: input.idempotencyKey,\n detachedSessionRef: input.detachedSessionRef,\n ...(this.traceContext !== undefined\n ? {\n traceId: this.traceContext.traceId,\n ...(this.traceContext.parentSpanId !== undefined\n ? { parentSpanId: this.traceContext.parentSpanId }\n : {}),\n }\n : {}),\n }\n this.records.set(taskId, record)\n this.controllers.set(taskId, controller)\n if (input.idempotencyKey) this.byIdempotencyKey.set(input.idempotencyKey, taskId)\n this.persist(record)\n\n // Fire-and-forget the run function. Errors flow into the record so the\n // status poll surfaces them; the promise itself is intentionally\n // unobserved by the queue.\n queueMicrotask(() => {\n this.execute(taskId, input, controller)\n })\n\n return { taskId, reused: false }\n }\n\n /**\n * Snapshot the current state of a delegation. Returns `undefined` for\n * unknown ids so callers can distinguish missing from terminal.\n * `includeTrace` attaches the journaled loop-trace span tree — off by\n * default so status polls stay light.\n */\n status(taskId: string, opts?: { includeTrace?: boolean }): DelegationStatusResult | undefined {\n const record = this.records.get(taskId)\n if (!record) return undefined\n return toStatusResult(record, opts)\n }\n\n /**\n * Abort an in-flight delegation. Returns `false` if the task is unknown\n * or already terminal. The underlying `run` function MUST honor the\n * abort signal for the cancel to take effect; the queue marks the\n * record `cancelled` regardless so a misbehaving runner cannot pin the\n * UI on `running` forever.\n */\n cancel(taskId: string): boolean {\n const record = this.records.get(taskId)\n if (!record) return false\n if (isTerminal(record.status)) return false\n const controller = this.controllers.get(taskId)\n controller?.abort()\n record.status = 'cancelled'\n record.completedAt = this.now()\n record.error = { message: 'cancelled by caller', kind: 'CancelledError' }\n this.persist(record)\n this.enforceRetention()\n return true\n }\n\n /**\n * Append a feedback event to the matching delegation. Returns `false`\n * when `ref` does not name a known taskId — the caller should still\n * record the feedback through a different surface (artifact/outcome\n * kinds are not queue-bound).\n */\n attachFeedback(taskId: string, snapshot: DelegationFeedbackSnapshot): boolean {\n const record = this.records.get(taskId)\n if (!record) return false\n record.feedback.push(snapshot)\n this.persist(record)\n return true\n }\n\n /**\n * Query the recorded delegations. Returns entries newest-first (by\n * `startedAt`), truncated to `limit`.\n */\n history(args: DelegationHistoryArgs = {}): DelegationHistoryEntry[] {\n const limit = clampLimit(args.limit)\n const since = args.since ? Date.parse(args.since) : Number.NEGATIVE_INFINITY\n const out: DelegationHistoryEntry[] = []\n for (const record of this.records.values()) {\n if (args.namespace && record.namespace !== args.namespace) continue\n if (args.profile && record.profile !== args.profile) continue\n if (Number.isFinite(since) && Date.parse(record.startedAt) < since) continue\n out.push(toHistoryEntry(record))\n }\n out.sort((a, b) => b.startedAt.localeCompare(a.startedAt))\n return out.slice(0, limit)\n }\n\n /**\n * Await every journal write issued so far. Rejects with the recorded\n * `DelegationPersistenceError` when any of them failed. Call before\n * handing the store's backing file to another process.\n */\n async flush(): Promise<void> {\n // Drain to quiescence: a write that completes while we await the tail can\n // chain another (the resume loop persisting a settle, a late `report`,\n // retention removals). Awaiting a single snapshot of `persistTail` would\n // miss those follow-on writes, letting a `store.upsert` land after the\n // caller has torn the backing dir down. Re-await until the tail stops\n // advancing.\n let tail: typeof this.persistTail | undefined\n while (this.persistTail !== tail) {\n tail = this.persistTail\n await tail\n }\n if (this.persistFailure) throw this.persistFailure\n }\n\n /** Test-only — number of in-flight (non-terminal) records. */\n inflightCount(): number {\n let n = 0\n for (const record of this.records.values()) {\n if (!isTerminal(record.status)) n += 1\n }\n return n\n }\n\n private async execute<Args extends DelegationArgs>(\n taskId: string,\n input: SubmitInput<Args>,\n controller: AbortController,\n ): Promise<void> {\n const record = this.records.get(taskId)\n if (!record) return\n record.status = 'running'\n this.persist(record)\n // Journal tee: each finished loop run inside the delegation appends its\n // compact span tree to the record immediately (a long delegation's trace\n // is durable before the terminal transition); `settle()` below drains\n // partial buffers from runs that never reached `loop.ended`.\n const traceCollector = createDelegationTraceCollector((spans) => {\n if (isTerminal(currentStatus(record))) return\n this.appendTrace(record, spans)\n this.persist(record)\n })\n try {\n const output = await input.run({\n signal: controller.signal,\n report: (progress) => {\n if (record.status === 'running') {\n record.progress = progress\n this.persist(record)\n }\n },\n traceEmitter: traceCollector.emitter,\n ...(record.detachedSessionRef !== undefined\n ? { detachedSessionRef: record.detachedSessionRef }\n : {}),\n updateDetachedSessionRef: (ref) => {\n if (typeof ref !== 'string' || ref.length === 0) {\n throw new ValidationError(\n 'DelegationTaskQueue: updateDetachedSessionRef requires a non-empty ref',\n )\n }\n if (isTerminal(currentStatus(record))) return\n record.detachedSessionRef = ref\n this.persist(record)\n },\n })\n traceCollector.settle()\n // `cancel()` may have flipped the status to `cancelled` while the\n // run promise was pending. Read the field through a widening\n // helper so the narrowed `'running'` type from the assignment\n // above does not exclude that case at compile time.\n if (currentStatus(record) === 'cancelled') return\n record.status = 'completed'\n record.completedAt = this.now()\n record.result = { profile: input.profile, output } as DelegationResultPayload\n this.persist(record)\n this.enforceRetention()\n } catch (err) {\n traceCollector.settle()\n if (currentStatus(record) === 'cancelled') return\n record.status = 'failed'\n record.completedAt = this.now()\n record.error = errorToShape(err)\n this.persist(record)\n this.enforceRetention()\n } finally {\n this.controllers.delete(taskId)\n }\n }\n\n private appendTrace(record: DelegationRecord, spans: DelegationTraceSpan[]): void {\n if (spans.length === 0) return\n const { trace, truncated } = capDelegationTrace([...(record.trace ?? []), ...spans])\n record.trace = trace\n if (truncated) record.traceTruncated = true\n }\n\n private async rehydrate(loaded: DelegationRecord[]): Promise<void> {\n const records = [...loaded].sort((a, b) => a.startedAt.localeCompare(b.startedAt))\n for (const record of records) {\n this.records.set(record.taskId, record)\n if (record.idempotencyKey) this.byIdempotencyKey.set(record.idempotencyKey, record.taskId)\n }\n const restoreWrites: Promise<void>[] = []\n for (const record of this.records.values()) {\n if (isTerminal(record.status)) continue\n if (record.detachedSessionRef && this.resumeDelegate) {\n record.status = 'running'\n restoreWrites.push(this.persist(record))\n this.startResume(record, record.detachedSessionRef, this.resumeDelegate)\n continue\n }\n record.status = 'failed'\n record.completedAt = this.now()\n record.error = {\n message: record.detachedSessionRef\n ? `delegation driver restarted while the task was in flight; detached session \"${record.detachedSessionRef}\" needs a resumeDelegate to be resumed`\n : 'delegation driver restarted while the task was in flight; the run was not detached and cannot be resumed',\n kind: 'DriverRestartError',\n }\n restoreWrites.push(this.persist(record))\n }\n const retentionWrite = this.enforceRetention()\n if (retentionWrite) restoreWrites.push(retentionWrite)\n await Promise.all(restoreWrites)\n if (this.persistFailure) throw this.persistFailure\n }\n\n private startResume(\n record: DelegationRecord,\n detachedSessionRef: string,\n driver: DelegationResumeDriver,\n ): void {\n const controller = new AbortController()\n this.controllers.set(record.taskId, controller)\n void this.driveResume(record, detachedSessionRef, driver, controller)\n }\n\n private async driveResume(\n record: DelegationRecord,\n detachedSessionRef: string,\n driver: DelegationResumeDriver,\n controller: AbortController,\n ): Promise<void> {\n const intervalMs = driver.intervalMs ?? 5000\n const resumeStartMs = Date.parse(this.now())\n const ctx: DelegationResumeContext = {\n signal: controller.signal,\n report: (progress) => {\n if (currentStatus(record) !== 'running') return\n record.progress = progress\n this.persist(record)\n },\n }\n try {\n while (!controller.signal.aborted && currentStatus(record) === 'running') {\n const tick = await driver.tick({ record: structuredClone(record), detachedSessionRef }, ctx)\n if (currentStatus(record) === 'cancelled') return\n if (tick.state === 'completed') {\n this.appendResumeSpan(record, detachedSessionRef, resumeStartMs)\n record.status = 'completed'\n record.completedAt = this.now()\n record.result = {\n profile: record.profile,\n output: tick.output,\n } as DelegationResultPayload\n if (tick.costUsd !== undefined) record.costUsd = tick.costUsd\n this.persist(record)\n this.enforceRetention()\n return\n }\n if (tick.state === 'failed') {\n this.appendResumeSpan(record, detachedSessionRef, resumeStartMs, tick.error.message)\n record.status = 'failed'\n record.completedAt = this.now()\n record.error = tick.error\n this.persist(record)\n this.enforceRetention()\n return\n }\n await abortableDelay(intervalMs, controller.signal)\n }\n } catch (err) {\n if (currentStatus(record) === 'cancelled') return\n this.appendResumeSpan(record, detachedSessionRef, resumeStartMs, errorToShape(err).message)\n record.status = 'failed'\n record.completedAt = this.now()\n record.error = errorToShape(err)\n this.persist(record)\n this.enforceRetention()\n } finally {\n this.controllers.delete(record.taskId)\n }\n }\n\n /**\n * Journal the resumed segment of a detached run as one compact span. The\n * resume driver re-attaches after a process restart, so the original\n * process's loop events are gone — this span records the post-restart\n * observation window (re-attach → terminal tick) under the\n * `'detached-resume'` driver tag, keeping restored delegations observable\n * in the journal alongside trace-carrying live runs.\n */\n private appendResumeSpan(\n record: DelegationRecord,\n detachedSessionRef: string,\n startMs: number,\n error?: string,\n ): void {\n this.appendTrace(record, [\n {\n spanId: generateDelegationSpanId(),\n name: 'loop',\n kind: 'loop',\n startMs,\n endMs: Date.parse(this.now()),\n meta: {\n 'tangle.loop.driver': 'detached-resume',\n 'tangle.loop.detached_session_ref': detachedSessionRef,\n ...(error !== undefined ? { 'tangle.loop.error': error } : {}),\n },\n },\n ])\n }\n\n private persist(record: DelegationRecord): Promise<void> {\n if (this.persistFailure) return Promise.resolve()\n const snapshot = structuredClone(record)\n this.persistTail = this.persistTail.then(async () => {\n if (this.persistFailure) return\n try {\n await this.store.upsert(snapshot)\n } catch (err) {\n this.failPersistence(err)\n }\n })\n return this.persistTail\n }\n\n private persistRemoval(taskIds: string[]): Promise<void> | undefined {\n if (this.persistFailure || taskIds.length === 0) return undefined\n this.persistTail = this.persistTail.then(async () => {\n if (this.persistFailure) return\n try {\n await this.store.remove(taskIds)\n } catch (err) {\n this.failPersistence(err)\n }\n })\n return this.persistTail\n }\n\n private failPersistence(cause: unknown): void {\n if (this.persistFailure) return\n const error =\n cause instanceof DelegationPersistenceError\n ? cause\n : new DelegationPersistenceError(\n `DelegationTaskQueue: store write failed: ${cause instanceof Error ? cause.message : String(cause)}`,\n { cause },\n )\n this.persistFailure = error\n this.onPersistError(error)\n }\n\n private enforceRetention(): Promise<void> | undefined {\n if (!Number.isFinite(this.maxTerminalRecords)) return undefined\n const terminal: DelegationRecord[] = []\n for (const record of this.records.values()) {\n if (isTerminal(record.status)) terminal.push(record)\n }\n const excess = terminal.length - this.maxTerminalRecords\n if (excess <= 0) return undefined\n terminal.sort((a, b) =>\n (a.completedAt ?? a.startedAt).localeCompare(b.completedAt ?? b.startedAt),\n )\n const evicted = terminal.slice(0, excess)\n for (const record of evicted) {\n this.records.delete(record.taskId)\n if (\n record.idempotencyKey &&\n this.byIdempotencyKey.get(record.idempotencyKey) === record.taskId\n ) {\n this.byIdempotencyKey.delete(record.idempotencyKey)\n }\n }\n return this.persistRemoval(evicted.map((record) => record.taskId))\n }\n}\n\nfunction isTerminal(status: DelegationStatus): boolean {\n return status === 'completed' || status === 'failed' || status === 'cancelled'\n}\n\nfunction currentStatus(record: DelegationRecord): DelegationStatus {\n return record.status\n}\n\nfunction clampLimit(raw: number | undefined): number {\n if (!Number.isFinite(raw)) return 50\n const n = Math.trunc(raw as number)\n if (n <= 0) return 50\n return Math.min(n, 500)\n}\n\nfunction abortableDelay(ms: number, signal: AbortSignal): Promise<void> {\n return new Promise((resolve) => {\n if (signal.aborted) {\n resolve()\n return\n }\n const onAbort = () => {\n clearTimeout(timer)\n resolve()\n }\n const timer = setTimeout(() => {\n signal.removeEventListener('abort', onAbort)\n resolve()\n }, ms)\n signal.addEventListener('abort', onAbort, { once: true })\n })\n}\n\nfunction toStatusResult(\n record: DelegationRecord,\n opts?: { includeTrace?: boolean },\n): DelegationStatusResult {\n const out: DelegationStatusResult = {\n taskId: record.taskId,\n profile: record.profile,\n status: record.status,\n startedAt: record.startedAt,\n }\n if (record.progress) out.progress = record.progress\n if (record.result) out.result = record.result\n if (record.error) out.error = record.error\n if (record.costUsd !== undefined) out.costUsd = record.costUsd\n if (record.completedAt) out.completedAt = record.completedAt\n if (record.traceId !== undefined) out.traceId = record.traceId\n if (record.parentSpanId !== undefined) out.parentSpanId = record.parentSpanId\n if (opts?.includeTrace === true && record.trace && record.trace.length > 0) {\n out.trace = record.trace.map((span) => ({ ...span }))\n if (record.traceTruncated) out.traceTruncated = true\n }\n return out\n}\n\nfunction toHistoryEntry(record: DelegationRecord): DelegationHistoryEntry {\n const entry: DelegationHistoryEntry = {\n taskId: record.taskId,\n profile: record.profile,\n args: record.args,\n status: record.status,\n startedAt: record.startedAt,\n hasTrace: record.trace !== undefined && record.trace.length > 0,\n }\n if (record.namespace) entry.namespace = record.namespace\n if (record.completedAt) entry.completedAt = record.completedAt\n if (record.costUsd !== undefined) entry.costUsd = record.costUsd\n if (record.feedback.length > 0) entry.feedback = [...record.feedback]\n if (record.traceId !== undefined) entry.traceId = record.traceId\n return entry\n}\n\nfunction errorToShape(err: unknown): DelegationError {\n if (err instanceof Error) {\n return { message: err.message, kind: err.name || 'Error' }\n }\n return { message: String(err), kind: 'NonError' }\n}\n\nfunction randomTaskId(): string {\n // Caller-stable id: `dlg-${timestamp}-${random}`. The timestamp portion\n // makes lexicographic sort match chronological order in history queries\n // even when the system clock skews under the second.\n const t = Date.now().toString(36)\n const r = Math.random().toString(36).slice(2, 10)\n return `dlg-${t}-${r}`\n}\n\n/**\n * Best-effort stable hash for use as `idempotencyKey`. Not cryptographic;\n * collisions only affect dedupe, never correctness.\n *\n * @stable\n */\nexport function hashIdempotencyInput(value: unknown): string {\n let str: string\n try {\n str = JSON.stringify(canonicalize(value))\n } catch {\n str = String(value)\n }\n // FNV-1a 32-bit\n let h = 0x811c9dc5\n for (let i = 0; i < str.length; i += 1) {\n h ^= str.charCodeAt(i)\n h = Math.imul(h, 0x01000193)\n }\n return (h >>> 0).toString(16).padStart(8, '0')\n}\n\nfunction canonicalize(value: unknown): unknown {\n if (value === null || typeof value !== 'object') return value\n if (Array.isArray(value)) return value.map(canonicalize)\n const entries = Object.entries(value as Record<string, unknown>)\n .filter(([, v]) => v !== undefined)\n .sort(([a], [b]) => a.localeCompare(b))\n const out: Record<string, unknown> = {}\n for (const [k, v] of entries) out[k] = canonicalize(v)\n return out\n}\n\n// Re-exports re-used by the feedback-store + handler glue. Kept local so\n// consumers of the queue don't have to import from `./types` separately.\nexport type {\n DelegateCodeArgs,\n DelegateCodeResult,\n DelegateFeedbackArgs,\n DelegateFeedbackResult,\n DelegateResearchArgs,\n DelegateResearchResult,\n DelegateUiAuditArgs,\n DelegateUiAuditConfig,\n DelegateUiAuditResult,\n DelegateUiAuditRoute,\n DelegationError,\n DelegationFeedbackSnapshot,\n DelegationHistoryArgs,\n DelegationHistoryEntry,\n DelegationHistoryResult,\n DelegationProfile,\n DelegationProgress,\n DelegationResultPayload,\n DelegationStatus,\n DelegationStatusArgs,\n DelegationStatusResult,\n UiAuditorDelegationOutput,\n} from './types'\n","/**\n *\n * `delegate` MCP tool — the ONE generic delegation verb, the agent-facing front door to\n * `delegate()` / `supervise()`. The agent hands it an INTENT (what it wants done); a default\n * authoring supervisor decomposes the intent and AUTHORS the worker profile it needs — there is no\n * hardcoded coder/researcher profile, so one verb covers code, research, and anything else.\n *\n * `delegate` is SYNCHRONOUS: it awaits the full supervised run and returns the delivered output\n * TOGETHER WITH `spentTotal` — the conserved cost of the whole delegation (`iterations` / `tokens` /\n * `usd` / `ms`), so the caller always learns what the delegation actually spent.\n *\n * The supervisor's substrate (its brain `router`, the worker `backend`, the completion `deliverable`)\n * is INJECTED at server construction — never an agent-supplied arg. The agent supplies only the\n * intent (+ an optional per-call `model` / `runId`).\n *\n * @stable\n */\n\nimport type { AgentProfile } from '@tangle-network/agent-interface'\nimport type { RouterTransportConfig } from '../../runtime/router-client'\nimport type { DeliverableSpec } from '../../runtime/supervise/completion-gate'\nimport { type DelegateOptions, delegate } from '../../runtime/supervise/delegate'\nimport type { ExecutorConfig } from '../../runtime/supervise/runtime'\nimport type { Spend, SupervisedResult } from '../../runtime/supervise/types'\n\n/** MCP tool name for the `delegate` generic-delegation tool. @stable */\nexport const DELEGATE_TOOL_NAME = 'delegate'\n\n/** Human-readable description of the `delegate` MCP tool, injected into the tool manifest. @stable */\nexport const DELEGATE_DESCRIPTION = [\n 'Delegate an INTENT to a supervisor that AUTHORS and drives whatever worker the intent needs.',\n '',\n 'Use when: you want a task done but do not want to specify HOW. State the outcome — \"fix the',\n 'failing auth test\", \"research competitor pricing with citations\", \"refactor the parser for',\n 'clarity\" — and the supervisor decomposes it, writes a tailored worker profile per sub-task, runs',\n 'the workers over a conserved compute budget, and settles only when a deployable check passes.',\n '',\n 'There is no fixed worker type: this ONE verb replaces separate code / research delegation. The',\n 'supervisor picks the worker shape from your intent.',\n '',\n 'Returns synchronously with the delivered result AND the real cost of the whole delegation',\n '(spentTotal: iterations, input/output tokens, usd, ms) — so you always know what it spent. A run',\n 'that produced no delivered worker returns status \"no-winner\" with the reason; it never fabricates',\n 'a success.',\n].join('\\n')\n\n/** JSON Schema for `delegate` tool arguments (`intent` + optional trace id). @stable */\nexport const DELEGATE_INPUT_SCHEMA = {\n type: 'object',\n properties: {\n intent: {\n type: 'string',\n description: 'What you want accomplished, as an outcome. The supervisor authors the worker.',\n },\n runId: {\n type: 'string',\n description: 'Optional trace-correlation id for this delegation.',\n },\n },\n required: ['intent'],\n additionalProperties: false,\n} as const\n\n/** Parsed `delegate` tool arguments. */\nexport interface DelegateArgs {\n intent: string\n runId?: string\n}\n\n/** Parse and validate raw MCP tool input into typed `DelegateArgs`; throws `TypeError` on bad input. @stable */\nexport function validateDelegateArgs(raw: unknown): DelegateArgs {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError('delegate: arguments must be an object')\n }\n const value = raw as Record<string, unknown>\n const unknown = Object.keys(value).filter((key) => key !== 'intent' && key !== 'runId')\n if (unknown.length > 0) {\n throw new TypeError(`delegate: unknown arguments: ${unknown.join(', ')}`)\n }\n const intent = value.intent\n if (typeof intent !== 'string' || intent.trim().length === 0) {\n throw new TypeError('delegate: `intent` must be a non-empty string')\n }\n const args: DelegateArgs = { intent: intent.trim() }\n if (value.runId !== undefined) {\n if (typeof value.runId !== 'string') throw new TypeError('delegate: `runId` must be a string')\n args.runId = value.runId\n }\n return args\n}\n\n/** The synchronous result the `delegate` tool returns to the calling agent: the delivered output (or\n * the no-winner reason) PLUS the conserved spend of the whole delegation. */\nexport type DelegateResult =\n | { status: 'winner'; out: unknown; outRef: string; spentTotal: Spend }\n | { status: 'no-winner'; reason: string; error?: DelegateError; spentTotal: Spend }\n\n/** What killed a delegation, projected for the calling agent: the rejection's name and message.\n * `reason` is a four-word code (`driver-failed`), and a code alone is not diagnosable — the\n * supervisor attaches the rejection to the result precisely so a caller can read it. */\nexport interface DelegateError {\n name: string\n message: string\n}\n\n/** @stable */\nexport interface DelegateHandlerOptions {\n /** The supervisor brain's router substrate (REQUIRED — the default supervisor is router-brained). */\n router: RouterTransportConfig\n /** Exact executable supervisor identity selected by the trusted composition root. */\n supervisorProfile: AgentProfile\n /** WHERE the authored workers run. Required for `supervise()` to spawn anything. */\n backend: ExecutorConfig\n /** The completion oracle the authored workers settle against (settled ⟺ delivered). */\n deliverable?: DeliverableSpec\n /** Restrict the run to this subset of models. */\n allowedModels?: readonly string[]\n}\n\n/** Project a `SupervisedResult` onto the tool's flat `DelegateResult`. Both variants carry the real\n * conserved `spentTotal`, so the agent always learns the cost — even on a no-winner, never a faked\n * output and never a fabricated zero spend. */\nfunction toDelegateResult(result: SupervisedResult<unknown>): DelegateResult {\n if (result.kind === 'no-winner') {\n // `stack` stays behind: an agent reads a cause, not a frame list.\n const rejection = (result as { error?: { name?: unknown; message?: unknown } }).error\n const error: DelegateError | undefined =\n typeof rejection?.name === 'string' && typeof rejection.message === 'string'\n ? { name: rejection.name, message: rejection.message }\n : undefined\n return {\n status: 'no-winner',\n reason: result.reason,\n ...(error ? { error } : {}),\n spentTotal: result.spentTotal,\n }\n }\n return {\n status: 'winner',\n out: result.out,\n outRef: result.outRef,\n spentTotal: result.spentTotal,\n }\n}\n\n/**\n * Build the `delegate` tool handler. Closes over the injected supervisor substrate (`router` /\n * `backend` / `deliverable`); each call routes the agent's intent to `delegate()` and returns the\n * delivered output with its conserved cost.\n */\nexport function createDelegateHandler(\n options: DelegateHandlerOptions,\n): (raw: unknown) => Promise<DelegateResult> {\n return async (raw) => {\n const args = validateDelegateArgs(raw)\n const opts: DelegateOptions = {\n backend: options.backend,\n router: options.router,\n supervisorProfile: options.supervisorProfile,\n ...(options.deliverable ? { deliverable: options.deliverable } : {}),\n ...(options.allowedModels ? { allowedModels: options.allowedModels } : {}),\n ...(args.runId ? { runId: args.runId } : {}),\n }\n const result = await delegate(args.intent, opts)\n return toDelegateResult(result)\n }\n}\n","/**\n *\n * `delegate_ui_audit` MCP tool — async kickoff for UI audit runs. Validates\n * the input, computes an idempotency key over the canonical fields, hands\n * the task to the queue, and returns a taskId. Identical inputs return\n * the same taskId.\n *\n * The handler does not import the auditor profile directly — consumers\n * inject a `UiAuditorDelegate` via `createMcpServer({ uiAuditorDelegate })`.\n * The delegate is the seam where the consumer chooses the judge (vision\n * model) and the `SandboxClient` (in-process Playwright vs fleet vs\n * remote browser). agent-runtime ships the in-process client under\n * `./profiles` so consumers who want the canonical setup can wire it\n * with a few lines.\n *\n * @experimental\n */\n\nimport path from 'node:path'\nimport { UI_LENSES, type UiLens } from '../../profiles/ui-auditor/substrate'\nimport type { UiAuditorDelegate } from '../delegates'\nimport {\n type DelegateUiAuditArgs,\n type DelegateUiAuditResult,\n type DelegationTaskQueue,\n hashIdempotencyInput,\n} from '../task-queue'\n\n/** MCP tool name for the `delegate_ui_audit` async kickoff tool. @experimental */\nexport const DELEGATE_UI_AUDIT_TOOL_NAME = 'delegate_ui_audit'\n\n/** Human-readable description of the `delegate_ui_audit` MCP tool, injected into the tool manifest. @experimental */\nexport const DELEGATE_UI_AUDIT_DESCRIPTION = [\n 'Delegate a UI/UX audit to a vision-driven auditor that produces self-contained',\n 'GitHub-issue-ready Markdown findings — one file per finding, with embedded',\n 'screenshot evidence and a suggested fix.',\n '',\n 'Use when: you want a thorough pass over a running web app for consistency,',\n 'hierarchy, layout, ux-flow, duplication, accessibility, responsive, states,',\n 'content, interaction, or perceived-performance issues. The auditor iterates',\n 'lens-by-lens so each pass finds new classes of issues; the workspace registry',\n 'deduplicates across iterations.',\n '',\n 'Returns immediately with a taskId. Poll delegation_status to retrieve the',\n 'workspace path + indexed findings (typically minutes per audited route).',\n 'Identical inputs return the same taskId — safe to retry.',\n '',\n 'Output layout under workspaceDir:',\n ' registry.json — finding index + capture sidecar',\n ' index.md — human-readable rollup',\n ' issues/NNN--<lens>--<slug>.md — one self-contained GitHub-issue',\n ' screenshots/<route>--<viewport>.png — capture archive',\n '',\n 'Multi-tenant isolation: every finding is scoped to `namespace` when set.',\n \"Never pass another tenant's namespace.\",\n].join('\\n')\n\nconst VIEWPORT_SCHEMA = {\n type: 'object',\n properties: {\n width: { type: 'integer', minimum: 1 },\n height: { type: 'integer', minimum: 1 },\n },\n required: ['width', 'height'],\n additionalProperties: false,\n} as const\n\nconst ROUTE_SCHEMA = {\n type: 'object',\n properties: {\n name: { type: 'string', description: 'Stable route name (used in screenshot filenames).' },\n url: { type: 'string', description: 'Fully-qualified URL.' },\n viewports: {\n type: 'array',\n items: VIEWPORT_SCHEMA,\n description: 'Viewports to capture at. Default [{1280, 800}].',\n },\n fullPage: { type: 'boolean' },\n waitFor: { type: 'string', description: 'CSS selector to wait for before capturing.' },\n },\n required: ['name', 'url'],\n additionalProperties: false,\n} as const\n\n/** JSON Schema for `delegate_ui_audit` tool arguments (`workspaceDir`, `routes`, optional config). @experimental */\nexport const DELEGATE_UI_AUDIT_INPUT_SCHEMA = {\n type: 'object',\n properties: {\n workspaceDir: { type: 'string', description: 'Absolute path for the audit workspace.' },\n routes: { type: 'array', items: ROUTE_SCHEMA, minItems: 1 },\n namespace: { type: 'string', description: 'Multi-tenant scope.' },\n config: {\n type: 'object',\n properties: {\n lenses: {\n type: 'array',\n items: { type: 'string', enum: [...UI_LENSES] },\n description: 'Lenses to iterate. Default: every lens except \"other\".',\n },\n maxIterations: { type: 'integer', minimum: 1 },\n maxConcurrency: { type: 'integer', minimum: 1 },\n productContext: { type: 'string' },\n },\n additionalProperties: false,\n },\n },\n required: ['workspaceDir', 'routes'],\n additionalProperties: false,\n} as const\n\nconst PER_LENS_PER_ROUTE_ESTIMATE_MS = 45_000\n\n/** Parse and validate raw MCP tool input into typed `DelegateUiAuditArgs`; throws `TypeError` on bad input. @experimental */\nexport function validateDelegateUiAuditArgs(raw: unknown): DelegateUiAuditArgs {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError('delegate_ui_audit: arguments must be an object')\n }\n const value = raw as Record<string, unknown>\n const workspaceDir = value.workspaceDir\n if (typeof workspaceDir !== 'string' || workspaceDir.trim().length === 0) {\n throw new TypeError('delegate_ui_audit: `workspaceDir` must be a non-empty string')\n }\n // Reject relative paths at the boundary. Without this, the writer joins a\n // relative workspaceDir against the process CWD, so the same MCP call\n // produces different file layouts depending on where the server was\n // launched — a silent fail-loud violation for a public API.\n const trimmedWs = workspaceDir.trim()\n if (!path.isAbsolute(trimmedWs)) {\n throw new TypeError(\n `delegate_ui_audit: \\`workspaceDir\\` must be an absolute path (got ${JSON.stringify(workspaceDir)})`,\n )\n }\n // Reject `..` segments. An absolute path like `/tmp/../../etc` passes\n // `isAbsolute`, then `path.join(workspaceDir, 'issues', …)` normalises it\n // to `/etc/issues/…`, escaping the intended workspace. Check the RAW\n // input split on the platform separator; `path.resolve` already collapses\n // `..` and would silently launder a traversal attempt past us.\n if (trimmedWs.split(path.sep).includes('..')) {\n throw new TypeError(\n `delegate_ui_audit: \\`workspaceDir\\` must not contain '..' segments (got ${JSON.stringify(workspaceDir)})`,\n )\n }\n const routesRaw = value.routes\n if (!Array.isArray(routesRaw) || routesRaw.length === 0) {\n throw new TypeError('delegate_ui_audit: `routes` must be a non-empty array')\n }\n const routes = routesRaw.map((r, i) => validateRoute(r, i))\n const args: DelegateUiAuditArgs = { workspaceDir: workspaceDir.trim(), routes }\n if (value.namespace !== undefined) {\n if (typeof value.namespace !== 'string' || value.namespace.trim().length === 0) {\n throw new TypeError('delegate_ui_audit: `namespace` must be a non-empty string when set')\n }\n args.namespace = value.namespace.trim()\n }\n if (value.config !== undefined) {\n args.config = validateConfig(value.config)\n }\n return args\n}\n\nfunction validateRoute(raw: unknown, index: number): DelegateUiAuditArgs['routes'][number] {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError(`delegate_ui_audit: routes[${index}] must be an object`)\n }\n const v = raw as Record<string, unknown>\n if (typeof v.name !== 'string' || v.name.trim().length === 0) {\n throw new TypeError(`delegate_ui_audit: routes[${index}].name must be a non-empty string`)\n }\n // Defense-in-depth: the slug helpers downstream strip non-alphanumerics, so\n // a route name with `..`, `/`, `\\`, or NUL cannot actually escape the\n // workspace. Rejecting them at the wire boundary keeps the invariant\n // explicit and matches the lens-allowlist style of validation.\n const trimmedName = v.name.trim()\n if (/[./\\\\]/.test(trimmedName) || trimmedName.includes('\\u0000')) {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].name must not contain path separators, dots, or NUL (got ${JSON.stringify(v.name)})`,\n )\n }\n if (typeof v.url !== 'string' || v.url.trim().length === 0) {\n throw new TypeError(`delegate_ui_audit: routes[${index}].url must be a non-empty string`)\n }\n // Parse + restrict to http/https. The in-process auditor client navigates\n // to whatever URL the MCP caller supplies; permitting `file://`, `data:`,\n // `javascript:` would let an attacker controlling the tool input pull\n // local files or run inline scripts.\n let parsedUrl: URL\n try {\n parsedUrl = new URL(v.url)\n } catch {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].url is not a parseable URL (got ${JSON.stringify(v.url)})`,\n )\n }\n if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].url must use http or https (got ${parsedUrl.protocol})`,\n )\n }\n const out: DelegateUiAuditArgs['routes'][number] = { name: v.name.trim(), url: v.url.trim() }\n if (v.viewports !== undefined) {\n if (!Array.isArray(v.viewports) || v.viewports.length === 0) {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].viewports must be a non-empty array when set`,\n )\n }\n out.viewports = v.viewports.map((vp, j) => validateViewport(vp, index, j))\n }\n if (v.fullPage !== undefined) {\n if (typeof v.fullPage !== 'boolean') {\n throw new TypeError(`delegate_ui_audit: routes[${index}].fullPage must be a boolean`)\n }\n out.fullPage = v.fullPage\n }\n if (v.waitFor !== undefined) {\n if (typeof v.waitFor !== 'string' || v.waitFor.trim().length === 0) {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].waitFor must be a non-empty string when set`,\n )\n }\n out.waitFor = v.waitFor.trim()\n }\n return out\n}\n\nfunction validateViewport(\n raw: unknown,\n routeIndex: number,\n viewportIndex: number,\n): { width: number; height: number } {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError(\n `delegate_ui_audit: routes[${routeIndex}].viewports[${viewportIndex}] must be an object`,\n )\n }\n const v = raw as Record<string, unknown>\n const w = Number(v.width)\n const h = Number(v.height)\n if (!Number.isInteger(w) || w <= 0 || !Number.isInteger(h) || h <= 0) {\n throw new RangeError(\n `delegate_ui_audit: routes[${routeIndex}].viewports[${viewportIndex}] must have positive integer width/height`,\n )\n }\n return { width: w, height: h }\n}\n\nfunction validateConfig(raw: unknown): DelegateUiAuditArgs['config'] {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError('delegate_ui_audit: `config` must be an object')\n }\n const v = raw as Record<string, unknown>\n const out: NonNullable<DelegateUiAuditArgs['config']> = {}\n if (v.lenses !== undefined) {\n if (!Array.isArray(v.lenses) || v.lenses.length === 0) {\n throw new TypeError('delegate_ui_audit: `config.lenses` must be a non-empty array when set')\n }\n const knownSet = new Set<string>(UI_LENSES)\n const lenses: UiLens[] = []\n for (let i = 0; i < v.lenses.length; i += 1) {\n const lens = v.lenses[i]\n if (typeof lens !== 'string' || !knownSet.has(lens)) {\n throw new TypeError(\n `delegate_ui_audit: config.lenses[${i}] must be one of ${UI_LENSES.join('|')}`,\n )\n }\n lenses.push(lens as UiLens)\n }\n out.lenses = lenses\n }\n if (v.maxIterations !== undefined) {\n const n = Number(v.maxIterations)\n if (!Number.isInteger(n) || n < 1) {\n throw new RangeError('delegate_ui_audit: `config.maxIterations` must be a positive integer')\n }\n out.maxIterations = n\n }\n if (v.maxConcurrency !== undefined) {\n const n = Number(v.maxConcurrency)\n if (!Number.isInteger(n) || n < 1) {\n throw new RangeError('delegate_ui_audit: `config.maxConcurrency` must be a positive integer')\n }\n out.maxConcurrency = n\n }\n if (v.productContext !== undefined) {\n if (typeof v.productContext !== 'string') {\n throw new TypeError('delegate_ui_audit: `config.productContext` must be a string')\n }\n out.productContext = v.productContext\n }\n return out\n}\n\n/** @experimental */\nexport interface DelegateUiAuditHandlerOptions {\n queue: DelegationTaskQueue\n delegate: UiAuditorDelegate\n estimateDurationMs?: (args: DelegateUiAuditArgs) => number\n}\n\n/** Build the MCP tool handler that validates input, deduplicates via idempotency key, and enqueues a UI audit. @experimental */\nexport function createDelegateUiAuditHandler(\n options: DelegateUiAuditHandlerOptions,\n): (raw: unknown) => Promise<DelegateUiAuditResult> {\n const estimateDurationMs = options.estimateDurationMs ?? defaultEstimate\n return async (raw) => {\n const args = validateDelegateUiAuditArgs(raw)\n const idempotencyKey = hashIdempotencyInput({\n profile: 'ui-auditor',\n workspaceDir: args.workspaceDir,\n routes: args.routes,\n namespace: args.namespace,\n config: args.config,\n })\n const submitted = options.queue.submit<DelegateUiAuditArgs>({\n profile: 'ui-auditor',\n args,\n namespace: args.namespace,\n idempotencyKey,\n run: async (ctx) => options.delegate(args, ctx),\n })\n return {\n taskId: submitted.taskId,\n estimatedDurationMs: estimateDurationMs(args),\n }\n }\n}\n\nfunction defaultEstimate(args: DelegateUiAuditArgs): number {\n const lenses = args.config?.lenses?.length ?? UI_LENSES.length - 1\n const routes = args.routes.length\n return PER_LENS_PER_ROUTE_ESTIMATE_MS * lenses * routes\n}\n","/**\n *\n * Stdio JSON-RPC MCP server exposing the delegation tools to sandbox\n * coding-harness agents (claude-code, codex, opencode, ...): the generic\n * `delegate` verb plus the queue-bound `delegate_feedback`,\n * `delegation_status`, and `delegation_history`. `delegate_ui_audit` is served\n * when a `uiAuditorDelegate` is wired.\n *\n * The server is transport-bound but topology-free: tool execution is\n * delegated to handler functions composed from a queue, a feedback\n * store, and the wired run delegates. Consumers wire those at\n * construction time. The `agent-runtime-mcp` bin serves the generic\n * `delegate` verb over a real sandbox client when `MCP_ENABLE_DELEGATE=1`.\n *\n * Wire protocol: line-delimited JSON-RPC 2.0 over stdio. Each line is\n * one request; each response is one line. `tools/list` and `tools/call`\n * mirror the MCP 2024-11-05 spec. The production server does not depend on\n * `@modelcontextprotocol/sdk`; integration tests validate this wire with the official client.\n *\n * @experimental\n */\n\nimport { Readable, Writable } from 'node:stream'\nimport { ValidationError } from '../errors'\nimport type { UiAuditorDelegate } from './delegates'\nimport { type FeedbackStore, InMemoryFeedbackStore } from './feedback-store'\nimport type { JsonRpcMessage, JsonRpcResponse, McpToolDescriptor, McpTransport } from './protocol'\nimport { DelegationTaskQueue } from './task-queue'\nimport { createStdioToolServer } from './tool-server'\nimport {\n createDelegateHandler,\n DELEGATE_DESCRIPTION,\n DELEGATE_INPUT_SCHEMA,\n DELEGATE_TOOL_NAME,\n type DelegateHandlerOptions,\n} from './tools/delegate'\nimport {\n createDelegateFeedbackHandler,\n DELEGATE_FEEDBACK_DESCRIPTION,\n DELEGATE_FEEDBACK_INPUT_SCHEMA,\n DELEGATE_FEEDBACK_TOOL_NAME,\n} from './tools/delegate-feedback'\nimport {\n createDelegateUiAuditHandler,\n DELEGATE_UI_AUDIT_DESCRIPTION,\n DELEGATE_UI_AUDIT_INPUT_SCHEMA,\n DELEGATE_UI_AUDIT_TOOL_NAME,\n} from './tools/delegate-ui-audit'\nimport {\n createDelegationHistoryHandler,\n DELEGATION_HISTORY_DESCRIPTION,\n DELEGATION_HISTORY_INPUT_SCHEMA,\n DELEGATION_HISTORY_TOOL_NAME,\n} from './tools/delegation-history'\nimport {\n createDelegationStatusHandler,\n DELEGATION_STATUS_DESCRIPTION,\n DELEGATION_STATUS_INPUT_SCHEMA,\n DELEGATION_STATUS_TOOL_NAME,\n} from './tools/delegation-status'\nimport type { TraceContext } from './trace-propagation'\n\nexport type { JsonRpcMessage, JsonRpcResponse, McpToolDescriptor, McpTransport } from './protocol'\n\n/** @experimental */\nexport interface McpServerOptions {\n /**\n * Required to enable `delegate` — the ONE generic delegation verb. Inject the supervisor\n * substrate: its brain `router`, the worker `backend`, and the completion `deliverable`. The\n * supervisor AUTHORS its own worker from the agent's intent, so there is no worker profile to\n * wire here.\n */\n delegateSupervisor?: DelegateHandlerOptions\n /**\n * Required to enable delegate_ui_audit. Wire one that executes an exact\n * agent profile through Runtime and returns the provider-neutral UI audit result.\n */\n uiAuditorDelegate?: UiAuditorDelegate\n /** Override the default in-memory feedback store. */\n feedbackStore?: FeedbackStore\n /** Override the default in-memory task queue. */\n queue?: DelegationTaskQueue\n /**\n * Extra tools to serve alongside the delegation tools, for example\n * `createCoordinationTools(...).tools`. Registered after the built-ins; a\n * duplicate name throws so delegation tools cannot be shadowed silently.\n */\n extraTools?: McpToolDescriptor[]\n /**\n * Inherited trace identity (`readTraceContextFromEnv()`) stamped on every\n * record the DEFAULT queue creates. Ignored when `queue` is supplied —\n * pass `traceContext` to that queue's constructor instead.\n */\n traceContext?: TraceContext\n /** Server display name surfaced via `initialize`. Default `'agent-runtime-mcp'`. */\n serverName?: string\n /** Server version surfaced via `initialize`. Default = the package version baked at build time. */\n serverVersion?: string\n}\n\n/** @experimental */\nexport interface McpServer {\n /** Tools currently registered (depend on which delegates were wired). */\n readonly tools: ReadonlyMap<string, McpToolDescriptor>\n /** The underlying queue — exposed so tests can introspect it. */\n readonly queue: DelegationTaskQueue\n /** The feedback store — exposed for the same reason. */\n readonly feedbackStore: FeedbackStore\n /** Handle a single parsed JSON-RPC message. Returns the response object (or `null` for notifications). */\n handle(message: JsonRpcMessage): Promise<JsonRpcResponse | null>\n /** Drive the server on a stdio-shaped transport until `stop()` is called. */\n serve(transport?: McpTransport): Promise<void>\n /** Stop a `serve` call. Subsequent requests are rejected. */\n stop(): void\n}\n\nconst DEFAULT_SERVER_NAME = 'agent-runtime-mcp'\nconst DEFAULT_SERVER_VERSION = '0.22.0'\n\n/**\n * Stdio JSON-RPC MCP server exposing the delegation tools (`delegate`, `delegate_feedback`, `delegation_status`, `delegation_history`, optional `delegate_ui_audit`) to sandbox coding-harness agents.\n *\n * @experimental\n */\nexport function createMcpServer(options: McpServerOptions = {}): McpServer {\n const queue =\n options.queue ??\n new DelegationTaskQueue(\n options.traceContext !== undefined ? { traceContext: options.traceContext } : {},\n )\n const feedbackStore = options.feedbackStore ?? new InMemoryFeedbackStore()\n const serverName = options.serverName ?? DEFAULT_SERVER_NAME\n const serverVersion = options.serverVersion ?? DEFAULT_SERVER_VERSION\n\n const tools = new Map<string, McpToolDescriptor>()\n\n if (options.delegateSupervisor) {\n tools.set(DELEGATE_TOOL_NAME, {\n name: DELEGATE_TOOL_NAME,\n description: DELEGATE_DESCRIPTION,\n inputSchema: DELEGATE_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateHandler(options.delegateSupervisor),\n })\n }\n if (options.uiAuditorDelegate) {\n tools.set(DELEGATE_UI_AUDIT_TOOL_NAME, {\n name: DELEGATE_UI_AUDIT_TOOL_NAME,\n description: DELEGATE_UI_AUDIT_DESCRIPTION,\n inputSchema: DELEGATE_UI_AUDIT_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateUiAuditHandler({ queue, delegate: options.uiAuditorDelegate }),\n })\n }\n tools.set(DELEGATE_FEEDBACK_TOOL_NAME, {\n name: DELEGATE_FEEDBACK_TOOL_NAME,\n description: DELEGATE_FEEDBACK_DESCRIPTION,\n inputSchema: DELEGATE_FEEDBACK_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateFeedbackHandler({ queue, store: feedbackStore }),\n })\n tools.set(DELEGATION_STATUS_TOOL_NAME, {\n name: DELEGATION_STATUS_TOOL_NAME,\n description: DELEGATION_STATUS_DESCRIPTION,\n inputSchema: DELEGATION_STATUS_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegationStatusHandler({ queue }),\n })\n tools.set(DELEGATION_HISTORY_TOOL_NAME, {\n name: DELEGATION_HISTORY_TOOL_NAME,\n description: DELEGATION_HISTORY_DESCRIPTION,\n inputSchema: DELEGATION_HISTORY_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegationHistoryHandler({ queue }),\n })\n for (const tool of options.extraTools ?? []) {\n if (tools.has(tool.name)) {\n throw new ValidationError(\n `createMcpServer: extra tool \"${tool.name}\" shadows a built-in tool`,\n )\n }\n tools.set(tool.name, tool)\n }\n\n const stdio = createStdioToolServer({\n serverName,\n serverVersion,\n tools: [...tools.values()],\n })\n\n return {\n tools: stdio.tools,\n queue,\n feedbackStore,\n handle: stdio.handle,\n serve: stdio.serve,\n stop: stdio.stop,\n }\n}\n\n/**\n * In-process pair of `Readable` + `Writable` streams suitable for driving\n * `server.serve(...)` from a test. Returns the agent-side stream (the\n * client writes to it) and the server-side stream (the test reads from it).\n *\n * @experimental\n */\nexport function createInProcessTransport(): {\n transport: McpTransport\n clientWrite(line: string): void\n clientClose(): void\n readServer(): Promise<JsonRpcResponse[]>\n} {\n const responses: JsonRpcResponse[] = []\n const input = new Readable({ read() {} })\n const output = new Writable({\n write(chunk, _enc, cb) {\n const text = chunk.toString('utf8')\n for (const line of text.split('\\n')) {\n const trimmed = line.trim()\n if (!trimmed) continue\n try {\n responses.push(JSON.parse(trimmed) as JsonRpcResponse)\n } catch {\n // Non-JSON output should never appear; drop it silently in the\n // test transport rather than crashing.\n }\n }\n cb()\n },\n })\n return {\n transport: { input, output },\n clientWrite(line: string) {\n input.push(`${line}\\n`)\n },\n clientClose() {\n input.push(null)\n },\n async readServer() {\n // Yield to the event loop a few times so async handlers drain.\n for (let i = 0; i < 5; i += 1) await new Promise((r) => setImmediate(r))\n return [...responses]\n },\n }\n}\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AAE;GAAO,QAAA,IAjBH,SAAS,EAC1B,MAAM,OAAO,MAAM,IAAI;IACrB,MAAM,OAAO,MAAM,SAAS,MAAM;IAClC,KAAK,MAAM,QAAQ,KAAK,MAAM,IAAI,GAAG;KACnC,MAAM,UAAU,KAAK,KAAK;KAC1B,IAAI,CAAC,SAAS;KACd,IAAI;MACF,UAAU,KAAK,KAAK,MAAM,OAAO,CAAoB;KACvD,QAAQ,CAGR;IACF;IACA,GAAG;GACL,EACF,CAE2B;EAAE;EAC3B,YAAY,MAAc;GACxB,MAAM,KAAK,GAAG,KAAK,GAAG;EACxB;EACA,cAAc;GACZ,MAAM,KAAK,IAAI;EACjB;EACA,MAAM,aAAa;GAEjB,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,MAAM,IAAI,SAAS,MAAM,aAAa,CAAC,CAAC;GACvE,OAAO,CAAC,GAAG,SAAS;EACtB;CACF;AACF"}
1
+ {"version":3,"file":"server-BNsUopoF.js","names":[],"sources":["../src/mcp/delegation-trace.ts","../src/mcp/delegation-store.ts","../src/mcp/task-queue.ts","../src/mcp/tools/delegate.ts","../src/mcp/tools/delegate-ui-audit.ts","../src/mcp/server.ts"],"sourcesContent":["/**\n *\n * Compact loop-trace tee for the delegation journal.\n *\n * The OTEL exporter ({@link createPropagatingTraceEmitter}) is a no-op\n * without `OTEL_EXPORTER_OTLP_ENDPOINT`, which leaves delegated work streams\n * dark in practice. This module derives the same loop → round → branch span\n * tree (via the shared {@link buildLoopSpanNodes} builder) into a small,\n * JSON-safe shape persisted directly on the `DelegationRecord` — observable\n * through `delegation_status` with no collector infrastructure. Both sinks\n * coexist: the OTEL export path is unchanged.\n *\n * Payload discipline: a record's trace is hard-capped (spans + serialized\n * bytes). Past the cap the OLDEST spans are dropped and the record carries a\n * `traceTruncated: true` marker — truncation is never silent.\n *\n * @experimental\n */\n\nimport { buildLoopSpanNodes } from '../otel-export'\nimport type { LoopTraceEmitter, LoopTraceEvent } from '../runtime/types'\n\n/**\n * One span of a delegation's compact trace. Flat (parent linkage by id), all\n * values JSON-safe scalars — `FileDelegationStore` round-trips records\n * through `JSON.stringify`. `meta` carries the span's attributes (GenAI\n * semconv keys + `tangle.loop.*` extensions) exactly as the OTEL sink emits\n * them, so a consumer can re-export journal traces losslessly.\n *\n * @experimental\n */\nexport interface DelegationTraceSpan {\n spanId: string\n /** Absent on the tree root. */\n parentSpanId?: string\n /** `'loop'` | `'loop.round'` | `'loop.iteration'` (or a sink-specific name). */\n name: string\n /** Topology level: loop root, plan round, or iteration branch. */\n kind: 'loop' | 'round' | 'branch'\n startMs: number\n endMs: number\n meta?: Record<string, string | number | boolean>\n}\n\n/** Default cap on spans retained per delegation record. @experimental */\nexport const DELEGATION_TRACE_MAX_SPANS = 512\n\n/** Default cap on the serialized trace payload per record, in bytes. @experimental */\nexport const DELEGATION_TRACE_MAX_BYTES = 256 * 1024\n\n/** @experimental */\nexport interface DelegationTraceCaps {\n /** Default {@link DELEGATION_TRACE_MAX_SPANS}. */\n maxSpans?: number\n /** Default {@link DELEGATION_TRACE_MAX_BYTES}. Approximate — measured as the\n * sum of per-span `JSON.stringify` lengths. */\n maxBytes?: number\n}\n\n/** @experimental */\nexport interface CappedDelegationTrace {\n trace: DelegationTraceSpan[]\n /** True when oldest spans were dropped to honor the caps. */\n truncated: boolean\n}\n\n/**\n * Derive the compact span tree for ONE loop run from its buffered\n * `LoopTraceEvent` stream. Same reconstruction as the OTEL exporter\n * ({@link buildLoopSpanNodes}); tolerates partial streams.\n *\n * @experimental\n */\nexport function buildDelegationTraceSpans(\n events: ReadonlyArray<LoopTraceEvent>,\n): DelegationTraceSpan[] {\n return buildLoopSpanNodes(events).map((node) => ({\n spanId: node.spanId,\n ...(node.parentSpanId !== undefined ? { parentSpanId: node.parentSpanId } : {}),\n name: node.name,\n kind: node.kind,\n startMs: node.startMs,\n endMs: node.endMs,\n ...(Object.keys(node.attrs).length > 0 ? { meta: node.attrs } : {}),\n }))\n}\n\n/**\n * Enforce the trace caps over an ordered (oldest-first) span list. Drops the\n * OLDEST spans first and reports `truncated: true` when anything was dropped;\n * the newest span always survives, so a non-empty input never caps to empty.\n * Dropping a parent may orphan surviving children's `parentSpanId` references\n * — acceptable for the flat journal shape; consumers treat unresolved parents\n * as roots.\n *\n * @experimental\n */\nexport function capDelegationTrace(\n spans: ReadonlyArray<DelegationTraceSpan>,\n caps?: DelegationTraceCaps,\n): CappedDelegationTrace {\n const maxSpans = caps?.maxSpans ?? DELEGATION_TRACE_MAX_SPANS\n const maxBytes = caps?.maxBytes ?? DELEGATION_TRACE_MAX_BYTES\n let start = Math.max(0, spans.length - maxSpans)\n const sizes = spans.map((span) => JSON.stringify(span).length + 1)\n let total = 0\n for (let i = start; i < sizes.length; i += 1) total += sizes[i]!\n while (start < spans.length - 1 && total > maxBytes) {\n total -= sizes[start]!\n start += 1\n }\n return { trace: spans.slice(start), truncated: start > 0 }\n}\n\n/**\n * Per-delegation trace collector. Buffers `LoopTraceEvent`s per runId\n * (mirroring the OTEL emitter's buffering) and hands the derived compact\n * spans to `onSpans` when a run reaches `loop.ended`. `settle()` drains runs\n * that never ended — a hard-aborted loop still leaves its partial tree in the\n * journal, unlike the OTEL path which drops it.\n *\n * @experimental\n */\nexport interface DelegationTraceCollector {\n emitter: LoopTraceEmitter\n /** Flush buffered events of runs that never reached `loop.ended`. */\n settle(): void\n}\n\n/** Build a `DelegationTraceCollector` that buffers loop-trace events and converts them to spans on settle. @experimental */\nexport function createDelegationTraceCollector(\n onSpans: (spans: DelegationTraceSpan[]) => void,\n): DelegationTraceCollector {\n const buffers = new Map<string, LoopTraceEvent[]>()\n const flush = (events: LoopTraceEvent[]): void => {\n const spans = buildDelegationTraceSpans(events)\n if (spans.length > 0) onSpans(spans)\n }\n return {\n emitter: {\n emit(event: LoopTraceEvent): void {\n const buf = buffers.get(event.runId)\n if (buf) buf.push(event)\n else buffers.set(event.runId, [event])\n if (event.kind === 'loop.ended') {\n const events = buffers.get(event.runId) ?? [event]\n buffers.delete(event.runId)\n flush(events)\n }\n },\n },\n settle(): void {\n for (const events of buffers.values()) flush(events)\n buffers.clear()\n },\n }\n}\n\n/**\n * 16-hex-char span id for journal spans synthesized outside the shared loop\n * builder (e.g. the queue's detached-resume segment).\n *\n * @experimental\n */\nexport function generateDelegationSpanId(): string {\n const bytes = new Uint8Array(8)\n if (typeof globalThis.crypto?.getRandomValues === 'function') {\n globalThis.crypto.getRandomValues(bytes)\n } else {\n for (let i = 0; i < 8; i += 1) bytes[i] = Math.floor(Math.random() * 256)\n }\n return Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, '0'))\n .join('')\n}\n\n/**\n * Fan one `LoopTraceEvent` stream into several emitters — e.g. the\n * process-wide OTEL exporter AND the per-delegation journal collector.\n * `undefined` entries are skipped; returns `undefined` when nothing is left\n * so callers keep the kernel's \"no emitter, no events\" fast path.\n *\n * @experimental\n */\nexport function composeLoopTraceEmitters(\n ...emitters: ReadonlyArray<LoopTraceEmitter | undefined>\n): LoopTraceEmitter | undefined {\n const live = emitters.filter((e): e is LoopTraceEmitter => e !== undefined)\n if (live.length === 0) return undefined\n if (live.length === 1) return live[0]\n return {\n emit(event: LoopTraceEvent): void | Promise<void> {\n const pending: Promise<void>[] = []\n for (const emitter of live) {\n const result = emitter.emit(event)\n if (result) pending.push(result)\n }\n if (pending.length > 0) return Promise.all(pending).then(() => undefined)\n },\n }\n}\n","/**\n *\n * Persistence port for the MCP delegation queue.\n *\n * `DelegationTaskQueue` keeps its working set in memory (status/history\n * reads stay synchronous) and journals every record mutation through a\n * `DelegationStore`. `DelegationTaskQueue.restore({ store })` is the load\n * path: it reads the full record set once at construction and rehydrates\n * the queue from it. After that the store only sees writes.\n *\n * Records MUST be JSON-safe — `FileDelegationStore` round-trips them\n * through `JSON.stringify`/`JSON.parse`, so a `Date`, `Map`, or function\n * smuggled into `args`/`result` would corrupt the journal.\n *\n * @stable\n */\n\nimport { mkdir, readFile, rename, writeFile } from 'node:fs/promises'\nimport { dirname } from 'node:path'\nimport { AgentEvalError } from '../errors'\nimport type { DelegationRecord } from './task-queue'\n\n/** @stable */\nexport interface DelegationStore {\n /**\n * Read every persisted record. Called once, by\n * `DelegationTaskQueue.restore`, before any write. A missing backing\n * file is an empty store; an unparseable one throws\n * `DelegationStateCorruptError`.\n */\n loadAll(): Promise<DelegationRecord[]>\n /** Insert or replace the record keyed by `record.taskId`. */\n upsert(record: DelegationRecord): Promise<void>\n /**\n * Resolve an idempotency key to the taskId that claimed it, if any.\n * The queue serves submit-time dedupe from its rehydrated in-memory\n * index; this read exists for consumers that share a store across\n * processes without holding the full record set.\n */\n lookupIdempotencyKey(key: string): Promise<string | undefined>\n /** Delete the named records — the retention-cap eviction path. */\n remove(taskIds: readonly string[]): Promise<void>\n}\n\n/**\n * The persisted delegation state exists but cannot be parsed into\n * records. Fail loud: silently starting empty over a corrupt journal\n * would erase delegation history and re-run idempotent work. Opt into\n * recovery explicitly via `FileDelegationStoreOptions.recoverCorrupt`\n * (the bin maps `AGENT_RUNTIME_DELEGATION_STATE_RECOVER=1` onto it),\n * which archives the corrupt file and starts fresh.\n *\n * @stable\n */\nexport class DelegationStateCorruptError extends AgentEvalError {\n constructor(message: string, options?: { cause?: unknown }) {\n super('validation', message, options)\n }\n}\n\n/**\n * A delegation-store read or write failed (filesystem error, store\n * called before `loadAll`, ...). Once the queue observes one, it stops\n * accepting new submissions — accepting work it cannot journal would\n * silently demote durable mode to in-memory mode.\n *\n * @stable\n */\nexport class DelegationPersistenceError extends AgentEvalError {\n constructor(message: string, options?: { cause?: unknown }) {\n super('config', message, options)\n }\n}\n\n/** In-memory `DelegationStore` — suitable for single-process use and tests. @stable */\nexport class InMemoryDelegationStore implements DelegationStore {\n private readonly records = new Map<string, DelegationRecord>()\n\n async loadAll(): Promise<DelegationRecord[]> {\n return [...this.records.values()].map(cloneRecord)\n }\n\n async upsert(record: DelegationRecord): Promise<void> {\n this.records.set(record.taskId, cloneRecord(record))\n }\n\n async lookupIdempotencyKey(key: string): Promise<string | undefined> {\n for (const record of this.records.values()) {\n if (record.idempotencyKey === key) return record.taskId\n }\n return undefined\n }\n\n async remove(taskIds: readonly string[]): Promise<void> {\n for (const taskId of taskIds) this.records.delete(taskId)\n }\n}\n\n/** @stable */\nexport interface FileDelegationStoreOptions {\n /** Absolute path of the JSON state file. Parent directories are created on first write. */\n filePath: string\n /**\n * When the state file exists but cannot be parsed, archive it to\n * `<filePath>.corrupt-<timestamp>` and start empty instead of\n * throwing `DelegationStateCorruptError`. Default false.\n */\n recoverCorrupt?: boolean\n}\n\ninterface PersistedDelegationState {\n version: 1\n records: DelegationRecord[]\n}\n\nconst STATE_FORMAT_VERSION = 1\n\n/**\n * JSON-file persistence for the delegation queue. Each write serializes\n * the full record set and lands it atomically (write to a sibling tmp\n * file, then `rename`), so readers never observe a torn file — a crash\n * mid-write leaves the previous snapshot intact. Writes are serialized\n * internally; concurrent `upsert`/`remove` calls cannot interleave.\n *\n * Built for the MCP server's scale (one stdio process, hundreds of\n * records): full-snapshot writes keep the format trivially inspectable\n * and corruption-detectable without a database dependency.\n *\n * @stable\n */\nexport class FileDelegationStore implements DelegationStore {\n private readonly filePath: string\n private readonly recoverCorrupt: boolean\n private readonly records = new Map<string, DelegationRecord>()\n private loaded = false\n private writeTail: Promise<void> = Promise.resolve()\n private tmpSeq = 0\n\n constructor(options: FileDelegationStoreOptions) {\n this.filePath = options.filePath\n this.recoverCorrupt = options.recoverCorrupt ?? false\n }\n\n async loadAll(): Promise<DelegationRecord[]> {\n let raw: string\n try {\n raw = await readFile(this.filePath, 'utf8')\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') {\n this.loaded = true\n return []\n }\n throw new DelegationPersistenceError(\n `FileDelegationStore: failed to read ${this.filePath}: ${errorMessage(err)}`,\n { cause: err },\n )\n }\n let state: PersistedDelegationState\n try {\n state = parsePersistedState(raw)\n } catch (err) {\n if (!this.recoverCorrupt) {\n throw new DelegationStateCorruptError(\n `FileDelegationStore: state file ${this.filePath} is corrupt (${errorMessage(err)}). ` +\n 'Repair or archive the file, or opt into automatic recovery ' +\n '(recoverCorrupt / AGENT_RUNTIME_DELEGATION_STATE_RECOVER=1) to archive it and start empty.',\n { cause: err },\n )\n }\n const archivePath = `${this.filePath}.corrupt-${Date.now()}`\n await rename(this.filePath, archivePath)\n this.loaded = true\n return []\n }\n this.records.clear()\n for (const record of state.records) this.records.set(record.taskId, record)\n this.loaded = true\n return [...this.records.values()].map(cloneRecord)\n }\n\n async upsert(record: DelegationRecord): Promise<void> {\n this.assertLoaded('upsert')\n this.records.set(record.taskId, cloneRecord(record))\n await this.enqueueWrite()\n }\n\n async lookupIdempotencyKey(key: string): Promise<string | undefined> {\n this.assertLoaded('lookupIdempotencyKey')\n for (const record of this.records.values()) {\n if (record.idempotencyKey === key) return record.taskId\n }\n return undefined\n }\n\n async remove(taskIds: readonly string[]): Promise<void> {\n this.assertLoaded('remove')\n let changed = false\n for (const taskId of taskIds) {\n if (this.records.delete(taskId)) changed = true\n }\n if (changed) await this.enqueueWrite()\n }\n\n private assertLoaded(op: string): void {\n if (this.loaded) return\n // Writing before load would snapshot only the new records and clobber\n // whatever the file already holds.\n throw new DelegationPersistenceError(\n `FileDelegationStore: ${op} called before loadAll() — the on-disk state has not been read yet`,\n )\n }\n\n private enqueueWrite(): Promise<void> {\n const write = this.writeTail.then(() => this.writeSnapshot())\n this.writeTail = write.catch(() => {})\n return write\n }\n\n private async writeSnapshot(): Promise<void> {\n const state: PersistedDelegationState = {\n version: STATE_FORMAT_VERSION,\n records: [...this.records.values()],\n }\n const payload = `${JSON.stringify(state)}\\n`\n this.tmpSeq += 1\n const tmpPath = `${this.filePath}.tmp-${process.pid}-${this.tmpSeq}`\n try {\n await mkdir(dirname(this.filePath), { recursive: true })\n await writeFile(tmpPath, payload, 'utf8')\n await rename(tmpPath, this.filePath)\n } catch (err) {\n throw new DelegationPersistenceError(\n `FileDelegationStore: failed to write ${this.filePath}: ${errorMessage(err)}`,\n { cause: err },\n )\n }\n }\n}\n\nfunction parsePersistedState(raw: string): PersistedDelegationState {\n const parsed: unknown = JSON.parse(raw)\n if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {\n throw new Error('top-level value is not an object')\n }\n const state = parsed as Record<string, unknown>\n if (state.version !== STATE_FORMAT_VERSION) {\n throw new Error(`unsupported state version ${JSON.stringify(state.version)}`)\n }\n if (!Array.isArray(state.records)) {\n throw new Error('`records` is not an array')\n }\n for (const record of state.records) {\n if (record === null || typeof record !== 'object') {\n throw new Error('a record entry is not an object')\n }\n const candidate = record as Record<string, unknown>\n if (typeof candidate.taskId !== 'string' || typeof candidate.status !== 'string') {\n throw new Error('a record entry is missing `taskId`/`status`')\n }\n }\n return { version: STATE_FORMAT_VERSION, records: state.records as DelegationRecord[] }\n}\n\nfunction cloneRecord(record: DelegationRecord): DelegationRecord {\n return structuredClone(record)\n}\n\nfunction errorMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n","/**\n *\n * State machine for async MCP delegations:\n *\n * pending → running → completed | failed\n * ↘ cancelled (from any non-terminal state via cancel())\n *\n * Each `submit` returns a `taskId` immediately and kicks the work off in the\n * background. The work function receives an `AbortSignal` the queue fires\n * when `cancel(taskId)` is called. The queue does NOT supervise runtime\n * timeouts — the underlying `runAgentRounds` driver / sandbox imposes those.\n *\n * Idempotency: callers may supply an `idempotencyKey` (hash of the input).\n * A duplicate `submit` with a known key returns the existing task instead of\n * starting a new one. Mutated input → different key → different task.\n *\n * Durability: the working set lives in memory (reads stay synchronous) and\n * every record mutation is journaled through a `DelegationStore`. The default\n * `InMemoryDelegationStore` keeps today's semantics — a process restart drops\n * all state. Construct via `DelegationTaskQueue.restore({ store })` with a\n * `FileDelegationStore` to reload prior records on startup: terminal records\n * stay queryable, in-flight records either re-attach through the\n * `resumeDelegate` seam (when they carry a `detachedSessionRef`) or fail\n * loud with a driver-restart error so `delegation_status` tells the truth.\n *\n * @stable\n */\n\nimport { ValidationError } from '../errors'\nimport type { LoopTraceEmitter } from '../runtime/types'\nimport {\n DelegationPersistenceError,\n type DelegationStore,\n InMemoryDelegationStore,\n} from './delegation-store'\nimport {\n capDelegationTrace,\n createDelegationTraceCollector,\n type DelegationTraceSpan,\n generateDelegationSpanId,\n} from './delegation-trace'\nimport type { TraceContext } from './trace-propagation'\nimport type {\n DelegateCodeArgs,\n DelegateResearchArgs,\n DelegateUiAuditArgs,\n DelegationError,\n DelegationFeedbackSnapshot,\n DelegationHistoryArgs,\n DelegationHistoryEntry,\n DelegationProfile,\n DelegationProgress,\n DelegationResultPayload,\n DelegationStatus,\n DelegationStatusResult,\n} from './types'\n\n/** Arguments accepted by the durable delegation queue. @stable */\nexport type DelegationArgs = DelegateCodeArgs | DelegateResearchArgs | DelegateUiAuditArgs\n\n/**\n * Must be JSON-safe end to end (`args`, `result`, `error`, `feedback`) —\n * persistent stores round-trip records through `JSON.stringify`.\n *\n * @stable\n */\nexport interface DelegationRecord {\n taskId: string\n profile: DelegationProfile\n namespace?: string\n args: DelegationArgs\n status: DelegationStatus\n progress?: DelegationProgress\n result?: DelegationResultPayload\n error?: DelegationError\n costUsd?: number\n startedAt: string\n completedAt?: string\n /** Sha-prefix hash of the canonical input — used for idempotency lookup. */\n idempotencyKey?: string\n /**\n * Caller-generated deterministic id of a detached run (e.g. the sandbox\n * session id a single-tick driver resumes by). Presence is what makes a\n * restored in-flight record resumable via `resumeDelegate`; without it a\n * restart settles the record as failed.\n */\n detachedSessionRef?: string\n /** Feedback events keyed by this delegation's taskId. */\n feedback: DelegationFeedbackSnapshot[]\n /**\n * Compact loop-trace span tree teed from the delegation's run, oldest\n * spans first. Appended when a delegated loop reaches `loop.ended` and\n * settled (partial buffers included) at the terminal transition. Capped\n * via `capDelegationTrace` — see `traceTruncated`.\n */\n trace?: DelegationTraceSpan[]\n /** Present when oldest trace spans were dropped to honor the trace caps. */\n traceTruncated?: true\n /**\n * Inherited trace identity (the queue's `traceContext` at submit time —\n * typically `readTraceContextFromEnv()`), distinct from the span payload:\n * a journal consumer joins records into the parent trace by these ids\n * without parsing spans. Restored records keep their persisted identity.\n */\n traceId?: string\n /** Caller span that dispatched the delegation, when one was inherited. */\n parentSpanId?: string\n}\n\n/** @stable */\nexport interface SubmitInput<Args extends DelegationArgs> {\n profile: DelegationProfile\n args: Args\n namespace?: string\n idempotencyKey?: string\n /**\n * Records the detached-run resume key on the new record. The submitted\n * `run` function still executes in-process exactly as without it — the\n * ref only matters after a restart, when `DelegationTaskQueue.restore`\n * hands it to the `resumeDelegate` seam instead of failing the record.\n */\n detachedSessionRef?: string\n /**\n * Runs the underlying delegation. The queue passes a fresh `AbortSignal`\n * and a `report` channel for incremental progress updates. The function\n * MUST resolve with the typed `DelegationResultPayload['output']`; the\n * queue wraps it with the profile tag.\n */\n run: (ctx: DelegationRunContext) => Promise<DelegationResultPayload['output']>\n}\n\n/** @stable Context handed to a `SubmitInput.run` function. */\nexport interface DelegationRunContext {\n signal: AbortSignal\n report(progress: DelegationProgress): void\n /** The `detachedSessionRef` recorded at submit, when one was supplied. */\n detachedSessionRef?: string\n /**\n * Replace the record's detached-run resume key — the detached dispatch path\n * calls this once the sandbox id is known so the persisted ref names a\n * resolvable box. Ignored after the record settles (a cancel racing the\n * rebind is legitimate; the ref no longer matters then). Throws on an empty\n * ref — erasing the resume key would silently make the record unresumable.\n */\n updateDetachedSessionRef(ref: string): void\n /**\n * Per-delegation loop-trace sink, always provided by the queue. Events\n * emitted here are journaled onto the record as a compact span tree\n * (`record.trace`) when each loop run ends and at the delegation's\n * terminal transition. Delegates forward it into their `runAgentRounds` ctx,\n * composed with any process-wide OTEL emitter\n * (`composeLoopTraceEmitters`). Optional in the type so consumer-built\n * contexts stay source-compatible.\n */\n traceEmitter?: LoopTraceEmitter\n}\n\n/** @stable */\nexport interface SubmitOutput {\n taskId: string\n /** True when a prior matching `idempotencyKey` returned an existing record. */\n reused: boolean\n}\n\n/**\n * One observation of a detached run, mapped 1:1 from a single-tick driver\n * (e.g. the sandbox SDK's `driveTurn`, which reports\n * completed | running | failed per pass). `running` schedules another tick\n * after `intervalMs`; `completed` / `failed` settle the record.\n *\n * @stable\n */\nexport type DelegationResumeTick =\n | { state: 'running' }\n | { state: 'completed'; output: DelegationResultPayload['output']; costUsd?: number }\n | { state: 'failed'; error: DelegationError }\n\n/** @stable */\nexport interface DelegationResumeContext {\n /** Fired by `cancel(taskId)`; the driver should stop the remote run when it can. */\n signal: AbortSignal\n report(progress: DelegationProgress): void\n}\n\n/**\n * Re-attaches restored in-flight records to their detached runs. The queue\n * calls `tick` repeatedly — it never awaits a whole run — so the driver can\n * be a thin wrapper over a one-pass primitive: resolve the run named by\n * `detachedSessionRef`, advance/poll it once, report where it stands. A\n * thrown error settles the record as failed; `failed` ticks are treated as\n * terminal and are not retried.\n *\n * @stable\n */\nexport interface DelegationResumeDriver {\n tick(\n task: { record: DelegationRecord; detachedSessionRef: string },\n ctx: DelegationResumeContext,\n ): Promise<DelegationResumeTick>\n /** Delay between `running` ticks, in milliseconds. Default 5000. */\n intervalMs?: number\n}\n\n/** @stable */\nexport interface DelegationTaskQueueOptions {\n /** ID generator override; default `randomTaskId`. */\n generateId?: () => string\n /** Clock override; default `() => new Date().toISOString()`. */\n now?: () => string\n /**\n * Journal for record mutations and the `restore()` load source. Default\n * `InMemoryDelegationStore` — observably identical to an unjournaled\n * queue. Pass a `FileDelegationStore` through\n * `DelegationTaskQueue.restore` for state that survives a restart;\n * constructing with `new` never loads prior state.\n */\n store?: DelegationStore\n /** Resume seam for restored in-flight records that carry a `detachedSessionRef`. */\n resumeDelegate?: DelegationResumeDriver\n /**\n * Maximum number of terminal (completed | failed | cancelled) records\n * retained; the oldest (by `completedAt`) are evicted from memory and\n * store once the cap is exceeded. Default unbounded.\n */\n maxTerminalRecords?: number\n /**\n * Observes the first store failure. After it fires, the queue refuses\n * new submissions and `flush()` rejects with the same error. Default:\n * rethrow on a microtask — an unhandled crash — because silently\n * degrading durable mode to memory-only would lie to the caller.\n */\n onPersistError?: (error: DelegationPersistenceError) => void\n /**\n * Inherited trace identity stamped on every submitted record\n * (`traceId` / `parentSpanId`). The bin passes\n * `readTraceContextFromEnv()` so journal consumers can join delegation\n * records into the caller's trace. Restored records keep the identity\n * they were persisted with.\n */\n traceContext?: TraceContext\n}\n\n/** In-process queue for async delegation tasks — submit, cancel, poll status, and read history. @stable */\nexport class DelegationTaskQueue {\n private readonly records = new Map<string, DelegationRecord>()\n private readonly controllers = new Map<string, AbortController>()\n private readonly byIdempotencyKey = new Map<string, string>()\n private readonly generateId: () => string\n private readonly now: () => string\n private readonly store: DelegationStore\n private readonly resumeDelegate?: DelegationResumeDriver\n private readonly maxTerminalRecords: number\n private readonly onPersistError: (error: DelegationPersistenceError) => void\n private readonly traceContext: TraceContext | undefined\n private persistTail: Promise<void> = Promise.resolve()\n private persistFailure: DelegationPersistenceError | undefined\n\n constructor(options: DelegationTaskQueueOptions = {}) {\n this.generateId = options.generateId ?? randomTaskId\n this.now = options.now ?? (() => new Date().toISOString())\n this.store = options.store ?? new InMemoryDelegationStore()\n this.resumeDelegate = options.resumeDelegate\n if (options.maxTerminalRecords !== undefined) {\n if (!Number.isInteger(options.maxTerminalRecords) || options.maxTerminalRecords < 1) {\n throw new ValidationError(\n `DelegationTaskQueue: maxTerminalRecords must be a positive integer, got ${String(options.maxTerminalRecords)}`,\n )\n }\n }\n this.maxTerminalRecords = options.maxTerminalRecords ?? Number.POSITIVE_INFINITY\n this.traceContext = options.traceContext\n this.onPersistError =\n options.onPersistError ??\n ((error) => {\n queueMicrotask(() => {\n throw error\n })\n })\n }\n\n /**\n * Construct a queue from previously-persisted state. Loads every record\n * from `options.store`, rebuilds the idempotency index (so a re-submitted\n * identical task returns the prior taskId and its terminal state), then:\n *\n * - terminal records stay queryable via `status()` / `history()`\n * - in-flight records with a `detachedSessionRef` re-attach through\n * `options.resumeDelegate` and report `running`\n * - other in-flight records settle as failed — their driver died with\n * the previous process and the result is unrecoverable\n *\n * The retention cap applies to the loaded set as well.\n */\n static async restore(options: DelegationTaskQueueOptions = {}): Promise<DelegationTaskQueue> {\n const queue = new DelegationTaskQueue(options)\n const loaded = await queue.store.loadAll()\n await queue.rehydrate(loaded)\n return queue\n }\n\n /**\n * Kick off a delegation in the background. Returns immediately. The\n * `taskId` is queryable via `status` once this method returns. Throws\n * the recorded `DelegationPersistenceError` once the store has failed —\n * the queue does not accept work it cannot journal.\n */\n submit<Args extends DelegationArgs>(input: SubmitInput<Args>): SubmitOutput {\n if (this.persistFailure) throw this.persistFailure\n if (input.idempotencyKey) {\n const existing = this.byIdempotencyKey.get(input.idempotencyKey)\n if (existing && this.records.has(existing)) {\n return { taskId: existing, reused: true }\n }\n }\n const taskId = this.generateId()\n const controller = new AbortController()\n const record: DelegationRecord = {\n taskId,\n profile: input.profile,\n namespace: input.namespace,\n args: input.args,\n status: 'pending',\n startedAt: this.now(),\n feedback: [],\n idempotencyKey: input.idempotencyKey,\n detachedSessionRef: input.detachedSessionRef,\n ...(this.traceContext !== undefined\n ? {\n traceId: this.traceContext.traceId,\n ...(this.traceContext.parentSpanId !== undefined\n ? { parentSpanId: this.traceContext.parentSpanId }\n : {}),\n }\n : {}),\n }\n this.records.set(taskId, record)\n this.controllers.set(taskId, controller)\n if (input.idempotencyKey) this.byIdempotencyKey.set(input.idempotencyKey, taskId)\n this.persist(record)\n\n // Fire-and-forget the run function. Errors flow into the record so the\n // status poll surfaces them; the promise itself is intentionally\n // unobserved by the queue.\n queueMicrotask(() => {\n this.execute(taskId, input, controller)\n })\n\n return { taskId, reused: false }\n }\n\n /**\n * Snapshot the current state of a delegation. Returns `undefined` for\n * unknown ids so callers can distinguish missing from terminal.\n * `includeTrace` attaches the journaled loop-trace span tree — off by\n * default so status polls stay light.\n */\n status(taskId: string, opts?: { includeTrace?: boolean }): DelegationStatusResult | undefined {\n const record = this.records.get(taskId)\n if (!record) return undefined\n return toStatusResult(record, opts)\n }\n\n /**\n * Abort an in-flight delegation. Returns `false` if the task is unknown\n * or already terminal. The underlying `run` function MUST honor the\n * abort signal for the cancel to take effect; the queue marks the\n * record `cancelled` regardless so a misbehaving runner cannot pin the\n * UI on `running` forever.\n */\n cancel(taskId: string): boolean {\n const record = this.records.get(taskId)\n if (!record) return false\n if (isTerminal(record.status)) return false\n const controller = this.controllers.get(taskId)\n controller?.abort()\n record.status = 'cancelled'\n record.completedAt = this.now()\n record.error = { message: 'cancelled by caller', kind: 'CancelledError' }\n this.persist(record)\n this.enforceRetention()\n return true\n }\n\n /**\n * Append a feedback event to the matching delegation. Returns `false`\n * when `ref` does not name a known taskId — the caller should still\n * record the feedback through a different surface (artifact/outcome\n * kinds are not queue-bound).\n */\n attachFeedback(taskId: string, snapshot: DelegationFeedbackSnapshot): boolean {\n const record = this.records.get(taskId)\n if (!record) return false\n record.feedback.push(snapshot)\n this.persist(record)\n return true\n }\n\n /**\n * Query the recorded delegations. Returns entries newest-first (by\n * `startedAt`), truncated to `limit`.\n */\n history(args: DelegationHistoryArgs = {}): DelegationHistoryEntry[] {\n const limit = clampLimit(args.limit)\n const since = args.since ? Date.parse(args.since) : Number.NEGATIVE_INFINITY\n const out: DelegationHistoryEntry[] = []\n for (const record of this.records.values()) {\n if (args.namespace && record.namespace !== args.namespace) continue\n if (args.profile && record.profile !== args.profile) continue\n if (Number.isFinite(since) && Date.parse(record.startedAt) < since) continue\n out.push(toHistoryEntry(record))\n }\n out.sort((a, b) => b.startedAt.localeCompare(a.startedAt))\n return out.slice(0, limit)\n }\n\n /**\n * Await every journal write issued so far. Rejects with the recorded\n * `DelegationPersistenceError` when any of them failed. Call before\n * handing the store's backing file to another process.\n */\n async flush(): Promise<void> {\n // Drain to quiescence: a write that completes while we await the tail can\n // chain another (the resume loop persisting a settle, a late `report`,\n // retention removals). Awaiting a single snapshot of `persistTail` would\n // miss those follow-on writes, letting a `store.upsert` land after the\n // caller has torn the backing dir down. Re-await until the tail stops\n // advancing.\n let tail: typeof this.persistTail | undefined\n while (this.persistTail !== tail) {\n tail = this.persistTail\n await tail\n }\n if (this.persistFailure) throw this.persistFailure\n }\n\n /** Test-only — number of in-flight (non-terminal) records. */\n inflightCount(): number {\n let n = 0\n for (const record of this.records.values()) {\n if (!isTerminal(record.status)) n += 1\n }\n return n\n }\n\n private async execute<Args extends DelegationArgs>(\n taskId: string,\n input: SubmitInput<Args>,\n controller: AbortController,\n ): Promise<void> {\n const record = this.records.get(taskId)\n if (!record) return\n record.status = 'running'\n this.persist(record)\n // Journal tee: each finished loop run inside the delegation appends its\n // compact span tree to the record immediately (a long delegation's trace\n // is durable before the terminal transition); `settle()` below drains\n // partial buffers from runs that never reached `loop.ended`.\n const traceCollector = createDelegationTraceCollector((spans) => {\n if (isTerminal(currentStatus(record))) return\n this.appendTrace(record, spans)\n this.persist(record)\n })\n try {\n const output = await input.run({\n signal: controller.signal,\n report: (progress) => {\n if (record.status === 'running') {\n record.progress = progress\n this.persist(record)\n }\n },\n traceEmitter: traceCollector.emitter,\n ...(record.detachedSessionRef !== undefined\n ? { detachedSessionRef: record.detachedSessionRef }\n : {}),\n updateDetachedSessionRef: (ref) => {\n if (typeof ref !== 'string' || ref.length === 0) {\n throw new ValidationError(\n 'DelegationTaskQueue: updateDetachedSessionRef requires a non-empty ref',\n )\n }\n if (isTerminal(currentStatus(record))) return\n record.detachedSessionRef = ref\n this.persist(record)\n },\n })\n traceCollector.settle()\n // `cancel()` may have flipped the status to `cancelled` while the\n // run promise was pending. Read the field through a widening\n // helper so the narrowed `'running'` type from the assignment\n // above does not exclude that case at compile time.\n if (currentStatus(record) === 'cancelled') return\n record.status = 'completed'\n record.completedAt = this.now()\n record.result = { profile: input.profile, output } as DelegationResultPayload\n this.persist(record)\n this.enforceRetention()\n } catch (err) {\n traceCollector.settle()\n if (currentStatus(record) === 'cancelled') return\n record.status = 'failed'\n record.completedAt = this.now()\n record.error = errorToShape(err)\n this.persist(record)\n this.enforceRetention()\n } finally {\n this.controllers.delete(taskId)\n }\n }\n\n private appendTrace(record: DelegationRecord, spans: DelegationTraceSpan[]): void {\n if (spans.length === 0) return\n const { trace, truncated } = capDelegationTrace([...(record.trace ?? []), ...spans])\n record.trace = trace\n if (truncated) record.traceTruncated = true\n }\n\n private async rehydrate(loaded: DelegationRecord[]): Promise<void> {\n const records = [...loaded].sort((a, b) => a.startedAt.localeCompare(b.startedAt))\n for (const record of records) {\n this.records.set(record.taskId, record)\n if (record.idempotencyKey) this.byIdempotencyKey.set(record.idempotencyKey, record.taskId)\n }\n const restoreWrites: Promise<void>[] = []\n for (const record of this.records.values()) {\n if (isTerminal(record.status)) continue\n if (record.detachedSessionRef && this.resumeDelegate) {\n record.status = 'running'\n restoreWrites.push(this.persist(record))\n this.startResume(record, record.detachedSessionRef, this.resumeDelegate)\n continue\n }\n record.status = 'failed'\n record.completedAt = this.now()\n record.error = {\n message: record.detachedSessionRef\n ? `delegation driver restarted while the task was in flight; detached session \"${record.detachedSessionRef}\" needs a resumeDelegate to be resumed`\n : 'delegation driver restarted while the task was in flight; the run was not detached and cannot be resumed',\n kind: 'DriverRestartError',\n }\n restoreWrites.push(this.persist(record))\n }\n const retentionWrite = this.enforceRetention()\n if (retentionWrite) restoreWrites.push(retentionWrite)\n await Promise.all(restoreWrites)\n if (this.persistFailure) throw this.persistFailure\n }\n\n private startResume(\n record: DelegationRecord,\n detachedSessionRef: string,\n driver: DelegationResumeDriver,\n ): void {\n const controller = new AbortController()\n this.controllers.set(record.taskId, controller)\n void this.driveResume(record, detachedSessionRef, driver, controller)\n }\n\n private async driveResume(\n record: DelegationRecord,\n detachedSessionRef: string,\n driver: DelegationResumeDriver,\n controller: AbortController,\n ): Promise<void> {\n const intervalMs = driver.intervalMs ?? 5000\n const resumeStartMs = Date.parse(this.now())\n const ctx: DelegationResumeContext = {\n signal: controller.signal,\n report: (progress) => {\n if (currentStatus(record) !== 'running') return\n record.progress = progress\n this.persist(record)\n },\n }\n try {\n while (!controller.signal.aborted && currentStatus(record) === 'running') {\n const tick = await driver.tick({ record: structuredClone(record), detachedSessionRef }, ctx)\n if (currentStatus(record) === 'cancelled') return\n if (tick.state === 'completed') {\n this.appendResumeSpan(record, detachedSessionRef, resumeStartMs)\n record.status = 'completed'\n record.completedAt = this.now()\n record.result = {\n profile: record.profile,\n output: tick.output,\n } as DelegationResultPayload\n if (tick.costUsd !== undefined) record.costUsd = tick.costUsd\n this.persist(record)\n this.enforceRetention()\n return\n }\n if (tick.state === 'failed') {\n this.appendResumeSpan(record, detachedSessionRef, resumeStartMs, tick.error.message)\n record.status = 'failed'\n record.completedAt = this.now()\n record.error = tick.error\n this.persist(record)\n this.enforceRetention()\n return\n }\n await abortableDelay(intervalMs, controller.signal)\n }\n } catch (err) {\n if (currentStatus(record) === 'cancelled') return\n this.appendResumeSpan(record, detachedSessionRef, resumeStartMs, errorToShape(err).message)\n record.status = 'failed'\n record.completedAt = this.now()\n record.error = errorToShape(err)\n this.persist(record)\n this.enforceRetention()\n } finally {\n this.controllers.delete(record.taskId)\n }\n }\n\n /**\n * Journal the resumed segment of a detached run as one compact span. The\n * resume driver re-attaches after a process restart, so the original\n * process's loop events are gone — this span records the post-restart\n * observation window (re-attach → terminal tick) under the\n * `'detached-resume'` driver tag, keeping restored delegations observable\n * in the journal alongside trace-carrying live runs.\n */\n private appendResumeSpan(\n record: DelegationRecord,\n detachedSessionRef: string,\n startMs: number,\n error?: string,\n ): void {\n this.appendTrace(record, [\n {\n spanId: generateDelegationSpanId(),\n name: 'loop',\n kind: 'loop',\n startMs,\n endMs: Date.parse(this.now()),\n meta: {\n 'tangle.loop.driver': 'detached-resume',\n 'tangle.loop.detached_session_ref': detachedSessionRef,\n ...(error !== undefined ? { 'tangle.loop.error': error } : {}),\n },\n },\n ])\n }\n\n private persist(record: DelegationRecord): Promise<void> {\n if (this.persistFailure) return Promise.resolve()\n const snapshot = structuredClone(record)\n this.persistTail = this.persistTail.then(async () => {\n if (this.persistFailure) return\n try {\n await this.store.upsert(snapshot)\n } catch (err) {\n this.failPersistence(err)\n }\n })\n return this.persistTail\n }\n\n private persistRemoval(taskIds: string[]): Promise<void> | undefined {\n if (this.persistFailure || taskIds.length === 0) return undefined\n this.persistTail = this.persistTail.then(async () => {\n if (this.persistFailure) return\n try {\n await this.store.remove(taskIds)\n } catch (err) {\n this.failPersistence(err)\n }\n })\n return this.persistTail\n }\n\n private failPersistence(cause: unknown): void {\n if (this.persistFailure) return\n const error =\n cause instanceof DelegationPersistenceError\n ? cause\n : new DelegationPersistenceError(\n `DelegationTaskQueue: store write failed: ${cause instanceof Error ? cause.message : String(cause)}`,\n { cause },\n )\n this.persistFailure = error\n this.onPersistError(error)\n }\n\n private enforceRetention(): Promise<void> | undefined {\n if (!Number.isFinite(this.maxTerminalRecords)) return undefined\n const terminal: DelegationRecord[] = []\n for (const record of this.records.values()) {\n if (isTerminal(record.status)) terminal.push(record)\n }\n const excess = terminal.length - this.maxTerminalRecords\n if (excess <= 0) return undefined\n terminal.sort((a, b) =>\n (a.completedAt ?? a.startedAt).localeCompare(b.completedAt ?? b.startedAt),\n )\n const evicted = terminal.slice(0, excess)\n for (const record of evicted) {\n this.records.delete(record.taskId)\n if (\n record.idempotencyKey &&\n this.byIdempotencyKey.get(record.idempotencyKey) === record.taskId\n ) {\n this.byIdempotencyKey.delete(record.idempotencyKey)\n }\n }\n return this.persistRemoval(evicted.map((record) => record.taskId))\n }\n}\n\nfunction isTerminal(status: DelegationStatus): boolean {\n return status === 'completed' || status === 'failed' || status === 'cancelled'\n}\n\nfunction currentStatus(record: DelegationRecord): DelegationStatus {\n return record.status\n}\n\nfunction clampLimit(raw: number | undefined): number {\n if (!Number.isFinite(raw)) return 50\n const n = Math.trunc(raw as number)\n if (n <= 0) return 50\n return Math.min(n, 500)\n}\n\nfunction abortableDelay(ms: number, signal: AbortSignal): Promise<void> {\n return new Promise((resolve) => {\n if (signal.aborted) {\n resolve()\n return\n }\n const onAbort = () => {\n clearTimeout(timer)\n resolve()\n }\n const timer = setTimeout(() => {\n signal.removeEventListener('abort', onAbort)\n resolve()\n }, ms)\n signal.addEventListener('abort', onAbort, { once: true })\n })\n}\n\nfunction toStatusResult(\n record: DelegationRecord,\n opts?: { includeTrace?: boolean },\n): DelegationStatusResult {\n const out: DelegationStatusResult = {\n taskId: record.taskId,\n profile: record.profile,\n status: record.status,\n startedAt: record.startedAt,\n }\n if (record.progress) out.progress = record.progress\n if (record.result) out.result = record.result\n if (record.error) out.error = record.error\n if (record.costUsd !== undefined) out.costUsd = record.costUsd\n if (record.completedAt) out.completedAt = record.completedAt\n if (record.traceId !== undefined) out.traceId = record.traceId\n if (record.parentSpanId !== undefined) out.parentSpanId = record.parentSpanId\n if (opts?.includeTrace === true && record.trace && record.trace.length > 0) {\n out.trace = record.trace.map((span) => ({ ...span }))\n if (record.traceTruncated) out.traceTruncated = true\n }\n return out\n}\n\nfunction toHistoryEntry(record: DelegationRecord): DelegationHistoryEntry {\n const entry: DelegationHistoryEntry = {\n taskId: record.taskId,\n profile: record.profile,\n args: record.args,\n status: record.status,\n startedAt: record.startedAt,\n hasTrace: record.trace !== undefined && record.trace.length > 0,\n }\n if (record.namespace) entry.namespace = record.namespace\n if (record.completedAt) entry.completedAt = record.completedAt\n if (record.costUsd !== undefined) entry.costUsd = record.costUsd\n if (record.feedback.length > 0) entry.feedback = [...record.feedback]\n if (record.traceId !== undefined) entry.traceId = record.traceId\n return entry\n}\n\nfunction errorToShape(err: unknown): DelegationError {\n if (err instanceof Error) {\n return { message: err.message, kind: err.name || 'Error' }\n }\n return { message: String(err), kind: 'NonError' }\n}\n\nfunction randomTaskId(): string {\n // Caller-stable id: `dlg-${timestamp}-${random}`. The timestamp portion\n // makes lexicographic sort match chronological order in history queries\n // even when the system clock skews under the second.\n const t = Date.now().toString(36)\n const r = Math.random().toString(36).slice(2, 10)\n return `dlg-${t}-${r}`\n}\n\n/**\n * Best-effort stable hash for use as `idempotencyKey`. Not cryptographic;\n * collisions only affect dedupe, never correctness.\n *\n * @stable\n */\nexport function hashIdempotencyInput(value: unknown): string {\n let str: string\n try {\n str = JSON.stringify(canonicalize(value))\n } catch {\n str = String(value)\n }\n // FNV-1a 32-bit\n let h = 0x811c9dc5\n for (let i = 0; i < str.length; i += 1) {\n h ^= str.charCodeAt(i)\n h = Math.imul(h, 0x01000193)\n }\n return (h >>> 0).toString(16).padStart(8, '0')\n}\n\nfunction canonicalize(value: unknown): unknown {\n if (value === null || typeof value !== 'object') return value\n if (Array.isArray(value)) return value.map(canonicalize)\n const entries = Object.entries(value as Record<string, unknown>)\n .filter(([, v]) => v !== undefined)\n .sort(([a], [b]) => a.localeCompare(b))\n const out: Record<string, unknown> = {}\n for (const [k, v] of entries) out[k] = canonicalize(v)\n return out\n}\n\n// Re-exports re-used by the feedback-store + handler glue. Kept local so\n// consumers of the queue don't have to import from `./types` separately.\nexport type {\n DelegateCodeArgs,\n DelegateCodeResult,\n DelegateFeedbackArgs,\n DelegateFeedbackResult,\n DelegateResearchArgs,\n DelegateResearchResult,\n DelegateUiAuditArgs,\n DelegateUiAuditConfig,\n DelegateUiAuditResult,\n DelegateUiAuditRoute,\n DelegationError,\n DelegationFeedbackSnapshot,\n DelegationHistoryArgs,\n DelegationHistoryEntry,\n DelegationHistoryResult,\n DelegationProfile,\n DelegationProgress,\n DelegationResultPayload,\n DelegationStatus,\n DelegationStatusArgs,\n DelegationStatusResult,\n UiAuditorDelegationOutput,\n} from './types'\n","/**\n *\n * `delegate` MCP tool — the ONE generic delegation verb, the agent-facing front door to\n * `delegate()` / `supervise()`. The agent hands it an INTENT (what it wants done); a default\n * authoring supervisor decomposes the intent and AUTHORS the worker profile it needs — there is no\n * hardcoded coder/researcher profile, so one verb covers code, research, and anything else.\n *\n * `delegate` is SYNCHRONOUS: it awaits the full supervised run and returns the delivered output\n * TOGETHER WITH `spentTotal` — the conserved cost of the whole delegation (`iterations` / `tokens` /\n * `usd` / `ms`), so the caller always learns what the delegation actually spent.\n *\n * The supervisor's substrate (its brain `router`, the worker `backend`, the completion `deliverable`)\n * is INJECTED at server construction — never an agent-supplied arg. The agent supplies only the\n * intent (+ an optional per-call `model` / `runId`).\n *\n * @stable\n */\n\nimport type { AgentProfile } from '@tangle-network/agent-interface'\nimport type { RouterTransportConfig } from '../../runtime/router-client'\nimport type { DeliverableSpec } from '../../runtime/supervise/completion-gate'\nimport { type DelegateOptions, delegate } from '../../runtime/supervise/delegate'\nimport type { ExecutorConfig } from '../../runtime/supervise/runtime'\nimport type { Spend, SupervisedResult } from '../../runtime/supervise/types'\n\n/** MCP tool name for the `delegate` generic-delegation tool. @stable */\nexport const DELEGATE_TOOL_NAME = 'delegate'\n\n/** Human-readable description of the `delegate` MCP tool, injected into the tool manifest. @stable */\nexport const DELEGATE_DESCRIPTION = [\n 'Delegate an INTENT to a supervisor that AUTHORS and drives whatever worker the intent needs.',\n '',\n 'Use when: you want a task done but do not want to specify HOW. State the outcome — \"fix the',\n 'failing auth test\", \"research competitor pricing with citations\", \"refactor the parser for',\n 'clarity\" — and the supervisor decomposes it, writes a tailored worker profile per sub-task, runs',\n 'the workers over a conserved compute budget, and settles only when a deployable check passes.',\n '',\n 'There is no fixed worker type: this ONE verb replaces separate code / research delegation. The',\n 'supervisor picks the worker shape from your intent.',\n '',\n 'Returns synchronously with the delivered result AND the real cost of the whole delegation',\n '(spentTotal: iterations, input/output tokens, usd, ms) — so you always know what it spent. A run',\n 'that produced no delivered worker returns status \"no-winner\" with the reason; it never fabricates',\n 'a success.',\n].join('\\n')\n\n/** JSON Schema for `delegate` tool arguments (`intent` + optional trace id). @stable */\nexport const DELEGATE_INPUT_SCHEMA = {\n type: 'object',\n properties: {\n intent: {\n type: 'string',\n description: 'What you want accomplished, as an outcome. The supervisor authors the worker.',\n },\n runId: {\n type: 'string',\n description: 'Optional trace-correlation id for this delegation.',\n },\n },\n required: ['intent'],\n additionalProperties: false,\n} as const\n\n/** Parsed `delegate` tool arguments. */\nexport interface DelegateArgs {\n intent: string\n runId?: string\n}\n\n/** Parse and validate raw MCP tool input into typed `DelegateArgs`; throws `TypeError` on bad input. @stable */\nexport function validateDelegateArgs(raw: unknown): DelegateArgs {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError('delegate: arguments must be an object')\n }\n const value = raw as Record<string, unknown>\n const unknown = Object.keys(value).filter((key) => key !== 'intent' && key !== 'runId')\n if (unknown.length > 0) {\n throw new TypeError(`delegate: unknown arguments: ${unknown.join(', ')}`)\n }\n const intent = value.intent\n if (typeof intent !== 'string' || intent.trim().length === 0) {\n throw new TypeError('delegate: `intent` must be a non-empty string')\n }\n const args: DelegateArgs = { intent: intent.trim() }\n if (value.runId !== undefined) {\n if (typeof value.runId !== 'string') throw new TypeError('delegate: `runId` must be a string')\n args.runId = value.runId\n }\n return args\n}\n\n/** The synchronous result the `delegate` tool returns to the calling agent: the delivered output (or\n * the no-winner reason) PLUS the conserved spend of the whole delegation. */\nexport type DelegateResult =\n | { status: 'winner'; out: unknown; outRef: string; spentTotal: Spend }\n | { status: 'no-winner'; reason: string; error?: DelegateError; spentTotal: Spend }\n\n/** What killed a delegation, projected for the calling agent: the rejection's name and message.\n * `reason` is a four-word code (`driver-failed`), and a code alone is not diagnosable — the\n * supervisor attaches the rejection to the result precisely so a caller can read it. */\nexport interface DelegateError {\n name: string\n message: string\n}\n\n/** @stable */\nexport interface DelegateHandlerOptions {\n /** The supervisor brain's router substrate (REQUIRED — the default supervisor is router-brained). */\n router: RouterTransportConfig\n /** Exact executable supervisor identity selected by the trusted composition root. */\n supervisorProfile: AgentProfile\n /** WHERE the authored workers run. Required for `supervise()` to spawn anything. */\n backend: ExecutorConfig\n /** The completion oracle the authored workers settle against (settled ⟺ delivered). */\n deliverable?: DeliverableSpec\n /** Restrict the run to this subset of models. */\n allowedModels?: readonly string[]\n}\n\n/** Project a `SupervisedResult` onto the tool's flat `DelegateResult`. Both variants carry the real\n * conserved `spentTotal`, so the agent always learns the cost — even on a no-winner, never a faked\n * output and never a fabricated zero spend. */\nfunction toDelegateResult(result: SupervisedResult<unknown>): DelegateResult {\n if (result.kind === 'no-winner') {\n // `stack` stays behind: an agent reads a cause, not a frame list.\n const rejection = (result as { error?: { name?: unknown; message?: unknown } }).error\n const error: DelegateError | undefined =\n typeof rejection?.name === 'string' && typeof rejection.message === 'string'\n ? { name: rejection.name, message: rejection.message }\n : undefined\n return {\n status: 'no-winner',\n reason: result.reason,\n ...(error ? { error } : {}),\n spentTotal: result.spentTotal,\n }\n }\n return {\n status: 'winner',\n out: result.out,\n outRef: result.outRef,\n spentTotal: result.spentTotal,\n }\n}\n\n/**\n * Build the `delegate` tool handler. Closes over the injected supervisor substrate (`router` /\n * `backend` / `deliverable`); each call routes the agent's intent to `delegate()` and returns the\n * delivered output with its conserved cost.\n */\nexport function createDelegateHandler(\n options: DelegateHandlerOptions,\n): (raw: unknown) => Promise<DelegateResult> {\n return async (raw) => {\n const args = validateDelegateArgs(raw)\n const opts: DelegateOptions = {\n backend: options.backend,\n router: options.router,\n supervisorProfile: options.supervisorProfile,\n ...(options.deliverable ? { deliverable: options.deliverable } : {}),\n ...(options.allowedModels ? { allowedModels: options.allowedModels } : {}),\n ...(args.runId ? { runId: args.runId } : {}),\n }\n const result = await delegate(args.intent, opts)\n return toDelegateResult(result)\n }\n}\n","/**\n *\n * `delegate_ui_audit` MCP tool — async kickoff for UI audit runs. Validates\n * the input, computes an idempotency key over the canonical fields, hands\n * the task to the queue, and returns a taskId. Identical inputs return\n * the same taskId.\n *\n * The handler does not import the auditor profile directly — consumers\n * inject a `UiAuditorDelegate` via `createMcpServer({ uiAuditorDelegate })`.\n * The delegate is the seam where the consumer chooses the judge (vision\n * model) and the `SandboxClient` (in-process Playwright vs fleet vs\n * remote browser). agent-runtime ships the in-process client under\n * `./profiles` so consumers who want the canonical setup can wire it\n * with a few lines.\n *\n * @experimental\n */\n\nimport path from 'node:path'\nimport { UI_LENSES, type UiLens } from '../../profiles/ui-auditor/substrate'\nimport type { UiAuditorDelegate } from '../delegates'\nimport {\n type DelegateUiAuditArgs,\n type DelegateUiAuditResult,\n type DelegationTaskQueue,\n hashIdempotencyInput,\n} from '../task-queue'\n\n/** MCP tool name for the `delegate_ui_audit` async kickoff tool. @experimental */\nexport const DELEGATE_UI_AUDIT_TOOL_NAME = 'delegate_ui_audit'\n\n/** Human-readable description of the `delegate_ui_audit` MCP tool, injected into the tool manifest. @experimental */\nexport const DELEGATE_UI_AUDIT_DESCRIPTION = [\n 'Delegate a UI/UX audit to a vision-driven auditor that produces self-contained',\n 'GitHub-issue-ready Markdown findings — one file per finding, with embedded',\n 'screenshot evidence and a suggested fix.',\n '',\n 'Use when: you want a thorough pass over a running web app for consistency,',\n 'hierarchy, layout, ux-flow, duplication, accessibility, responsive, states,',\n 'content, interaction, or perceived-performance issues. The auditor iterates',\n 'lens-by-lens so each pass finds new classes of issues; the workspace registry',\n 'deduplicates across iterations.',\n '',\n 'Returns immediately with a taskId. Poll delegation_status to retrieve the',\n 'workspace path + indexed findings (typically minutes per audited route).',\n 'Identical inputs return the same taskId — safe to retry.',\n '',\n 'Output layout under workspaceDir:',\n ' registry.json — finding index + capture sidecar',\n ' index.md — human-readable rollup',\n ' issues/NNN--<lens>--<slug>.md — one self-contained GitHub-issue',\n ' screenshots/<route>--<viewport>.png — capture archive',\n '',\n 'Multi-tenant isolation: every finding is scoped to `namespace` when set.',\n \"Never pass another tenant's namespace.\",\n].join('\\n')\n\nconst VIEWPORT_SCHEMA = {\n type: 'object',\n properties: {\n width: { type: 'integer', minimum: 1 },\n height: { type: 'integer', minimum: 1 },\n },\n required: ['width', 'height'],\n additionalProperties: false,\n} as const\n\nconst ROUTE_SCHEMA = {\n type: 'object',\n properties: {\n name: { type: 'string', description: 'Stable route name (used in screenshot filenames).' },\n url: { type: 'string', description: 'Fully-qualified URL.' },\n viewports: {\n type: 'array',\n items: VIEWPORT_SCHEMA,\n description: 'Viewports to capture at. Default [{1280, 800}].',\n },\n fullPage: { type: 'boolean' },\n waitFor: { type: 'string', description: 'CSS selector to wait for before capturing.' },\n },\n required: ['name', 'url'],\n additionalProperties: false,\n} as const\n\n/** JSON Schema for `delegate_ui_audit` tool arguments (`workspaceDir`, `routes`, optional config). @experimental */\nexport const DELEGATE_UI_AUDIT_INPUT_SCHEMA = {\n type: 'object',\n properties: {\n workspaceDir: { type: 'string', description: 'Absolute path for the audit workspace.' },\n routes: { type: 'array', items: ROUTE_SCHEMA, minItems: 1 },\n namespace: { type: 'string', description: 'Multi-tenant scope.' },\n config: {\n type: 'object',\n properties: {\n lenses: {\n type: 'array',\n items: { type: 'string', enum: [...UI_LENSES] },\n description: 'Lenses to iterate. Default: every lens except \"other\".',\n },\n maxIterations: { type: 'integer', minimum: 1 },\n maxConcurrency: { type: 'integer', minimum: 1 },\n productContext: { type: 'string' },\n },\n additionalProperties: false,\n },\n },\n required: ['workspaceDir', 'routes'],\n additionalProperties: false,\n} as const\n\nconst PER_LENS_PER_ROUTE_ESTIMATE_MS = 45_000\n\n/** Parse and validate raw MCP tool input into typed `DelegateUiAuditArgs`; throws `TypeError` on bad input. @experimental */\nexport function validateDelegateUiAuditArgs(raw: unknown): DelegateUiAuditArgs {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError('delegate_ui_audit: arguments must be an object')\n }\n const value = raw as Record<string, unknown>\n const workspaceDir = value.workspaceDir\n if (typeof workspaceDir !== 'string' || workspaceDir.trim().length === 0) {\n throw new TypeError('delegate_ui_audit: `workspaceDir` must be a non-empty string')\n }\n // Reject relative paths at the boundary. Without this, the writer joins a\n // relative workspaceDir against the process CWD, so the same MCP call\n // produces different file layouts depending on where the server was\n // launched — a silent fail-loud violation for a public API.\n const trimmedWs = workspaceDir.trim()\n if (!path.isAbsolute(trimmedWs)) {\n throw new TypeError(\n `delegate_ui_audit: \\`workspaceDir\\` must be an absolute path (got ${JSON.stringify(workspaceDir)})`,\n )\n }\n // Reject `..` segments. An absolute path like `/tmp/../../etc` passes\n // `isAbsolute`, then `path.join(workspaceDir, 'issues', …)` normalises it\n // to `/etc/issues/…`, escaping the intended workspace. Check the RAW\n // input split on the platform separator; `path.resolve` already collapses\n // `..` and would silently launder a traversal attempt past us.\n if (trimmedWs.split(path.sep).includes('..')) {\n throw new TypeError(\n `delegate_ui_audit: \\`workspaceDir\\` must not contain '..' segments (got ${JSON.stringify(workspaceDir)})`,\n )\n }\n const routesRaw = value.routes\n if (!Array.isArray(routesRaw) || routesRaw.length === 0) {\n throw new TypeError('delegate_ui_audit: `routes` must be a non-empty array')\n }\n const routes = routesRaw.map((r, i) => validateRoute(r, i))\n const args: DelegateUiAuditArgs = { workspaceDir: workspaceDir.trim(), routes }\n if (value.namespace !== undefined) {\n if (typeof value.namespace !== 'string' || value.namespace.trim().length === 0) {\n throw new TypeError('delegate_ui_audit: `namespace` must be a non-empty string when set')\n }\n args.namespace = value.namespace.trim()\n }\n if (value.config !== undefined) {\n args.config = validateConfig(value.config)\n }\n return args\n}\n\nfunction validateRoute(raw: unknown, index: number): DelegateUiAuditArgs['routes'][number] {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError(`delegate_ui_audit: routes[${index}] must be an object`)\n }\n const v = raw as Record<string, unknown>\n if (typeof v.name !== 'string' || v.name.trim().length === 0) {\n throw new TypeError(`delegate_ui_audit: routes[${index}].name must be a non-empty string`)\n }\n // Defense-in-depth: the slug helpers downstream strip non-alphanumerics, so\n // a route name with `..`, `/`, `\\`, or NUL cannot actually escape the\n // workspace. Rejecting them at the wire boundary keeps the invariant\n // explicit and matches the lens-allowlist style of validation.\n const trimmedName = v.name.trim()\n if (/[./\\\\]/.test(trimmedName) || trimmedName.includes('\\u0000')) {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].name must not contain path separators, dots, or NUL (got ${JSON.stringify(v.name)})`,\n )\n }\n if (typeof v.url !== 'string' || v.url.trim().length === 0) {\n throw new TypeError(`delegate_ui_audit: routes[${index}].url must be a non-empty string`)\n }\n // Parse + restrict to http/https. The in-process auditor client navigates\n // to whatever URL the MCP caller supplies; permitting `file://`, `data:`,\n // `javascript:` would let an attacker controlling the tool input pull\n // local files or run inline scripts.\n let parsedUrl: URL\n try {\n parsedUrl = new URL(v.url)\n } catch {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].url is not a parseable URL (got ${JSON.stringify(v.url)})`,\n )\n }\n if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].url must use http or https (got ${parsedUrl.protocol})`,\n )\n }\n const out: DelegateUiAuditArgs['routes'][number] = { name: v.name.trim(), url: v.url.trim() }\n if (v.viewports !== undefined) {\n if (!Array.isArray(v.viewports) || v.viewports.length === 0) {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].viewports must be a non-empty array when set`,\n )\n }\n out.viewports = v.viewports.map((vp, j) => validateViewport(vp, index, j))\n }\n if (v.fullPage !== undefined) {\n if (typeof v.fullPage !== 'boolean') {\n throw new TypeError(`delegate_ui_audit: routes[${index}].fullPage must be a boolean`)\n }\n out.fullPage = v.fullPage\n }\n if (v.waitFor !== undefined) {\n if (typeof v.waitFor !== 'string' || v.waitFor.trim().length === 0) {\n throw new TypeError(\n `delegate_ui_audit: routes[${index}].waitFor must be a non-empty string when set`,\n )\n }\n out.waitFor = v.waitFor.trim()\n }\n return out\n}\n\nfunction validateViewport(\n raw: unknown,\n routeIndex: number,\n viewportIndex: number,\n): { width: number; height: number } {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError(\n `delegate_ui_audit: routes[${routeIndex}].viewports[${viewportIndex}] must be an object`,\n )\n }\n const v = raw as Record<string, unknown>\n const w = Number(v.width)\n const h = Number(v.height)\n if (!Number.isInteger(w) || w <= 0 || !Number.isInteger(h) || h <= 0) {\n throw new RangeError(\n `delegate_ui_audit: routes[${routeIndex}].viewports[${viewportIndex}] must have positive integer width/height`,\n )\n }\n return { width: w, height: h }\n}\n\nfunction validateConfig(raw: unknown): DelegateUiAuditArgs['config'] {\n if (raw === null || typeof raw !== 'object') {\n throw new TypeError('delegate_ui_audit: `config` must be an object')\n }\n const v = raw as Record<string, unknown>\n const out: NonNullable<DelegateUiAuditArgs['config']> = {}\n if (v.lenses !== undefined) {\n if (!Array.isArray(v.lenses) || v.lenses.length === 0) {\n throw new TypeError('delegate_ui_audit: `config.lenses` must be a non-empty array when set')\n }\n const knownSet = new Set<string>(UI_LENSES)\n const lenses: UiLens[] = []\n for (let i = 0; i < v.lenses.length; i += 1) {\n const lens = v.lenses[i]\n if (typeof lens !== 'string' || !knownSet.has(lens)) {\n throw new TypeError(\n `delegate_ui_audit: config.lenses[${i}] must be one of ${UI_LENSES.join('|')}`,\n )\n }\n lenses.push(lens as UiLens)\n }\n out.lenses = lenses\n }\n if (v.maxIterations !== undefined) {\n const n = Number(v.maxIterations)\n if (!Number.isInteger(n) || n < 1) {\n throw new RangeError('delegate_ui_audit: `config.maxIterations` must be a positive integer')\n }\n out.maxIterations = n\n }\n if (v.maxConcurrency !== undefined) {\n const n = Number(v.maxConcurrency)\n if (!Number.isInteger(n) || n < 1) {\n throw new RangeError('delegate_ui_audit: `config.maxConcurrency` must be a positive integer')\n }\n out.maxConcurrency = n\n }\n if (v.productContext !== undefined) {\n if (typeof v.productContext !== 'string') {\n throw new TypeError('delegate_ui_audit: `config.productContext` must be a string')\n }\n out.productContext = v.productContext\n }\n return out\n}\n\n/** @experimental */\nexport interface DelegateUiAuditHandlerOptions {\n queue: DelegationTaskQueue\n delegate: UiAuditorDelegate\n estimateDurationMs?: (args: DelegateUiAuditArgs) => number\n}\n\n/** Build the MCP tool handler that validates input, deduplicates via idempotency key, and enqueues a UI audit. @experimental */\nexport function createDelegateUiAuditHandler(\n options: DelegateUiAuditHandlerOptions,\n): (raw: unknown) => Promise<DelegateUiAuditResult> {\n const estimateDurationMs = options.estimateDurationMs ?? defaultEstimate\n return async (raw) => {\n const args = validateDelegateUiAuditArgs(raw)\n const idempotencyKey = hashIdempotencyInput({\n profile: 'ui-auditor',\n workspaceDir: args.workspaceDir,\n routes: args.routes,\n namespace: args.namespace,\n config: args.config,\n })\n const submitted = options.queue.submit<DelegateUiAuditArgs>({\n profile: 'ui-auditor',\n args,\n namespace: args.namespace,\n idempotencyKey,\n run: async (ctx) => options.delegate(args, ctx),\n })\n return {\n taskId: submitted.taskId,\n estimatedDurationMs: estimateDurationMs(args),\n }\n }\n}\n\nfunction defaultEstimate(args: DelegateUiAuditArgs): number {\n const lenses = args.config?.lenses?.length ?? UI_LENSES.length - 1\n const routes = args.routes.length\n return PER_LENS_PER_ROUTE_ESTIMATE_MS * lenses * routes\n}\n","/**\n *\n * Stdio JSON-RPC MCP server exposing the delegation tools to sandbox\n * coding-harness agents (claude-code, codex, opencode, ...): the generic\n * `delegate` verb plus the queue-bound `delegate_feedback`,\n * `delegation_status`, and `delegation_history`. `delegate_ui_audit` is served\n * when a `uiAuditorDelegate` is wired.\n *\n * The server is transport-bound but topology-free: tool execution is\n * delegated to handler functions composed from a queue, a feedback\n * store, and the wired run delegates. Consumers wire those at\n * construction time. The `agent-runtime-mcp` bin serves the generic\n * `delegate` verb over a real sandbox client when `MCP_ENABLE_DELEGATE=1`.\n *\n * Wire protocol: line-delimited JSON-RPC 2.0 over stdio. Each line is\n * one request; each response is one line. `tools/list` and `tools/call`\n * mirror the MCP 2024-11-05 spec. The production server does not depend on\n * `@modelcontextprotocol/sdk`; integration tests validate this wire with the official client.\n *\n * @experimental\n */\n\nimport { Readable, Writable } from 'node:stream'\nimport { ValidationError } from '../errors'\nimport type { UiAuditorDelegate } from './delegates'\nimport { type FeedbackStore, InMemoryFeedbackStore } from './feedback-store'\nimport type { JsonRpcMessage, JsonRpcResponse, McpToolDescriptor, McpTransport } from './protocol'\nimport { DelegationTaskQueue } from './task-queue'\nimport { createStdioToolServer } from './tool-server'\nimport {\n createDelegateHandler,\n DELEGATE_DESCRIPTION,\n DELEGATE_INPUT_SCHEMA,\n DELEGATE_TOOL_NAME,\n type DelegateHandlerOptions,\n} from './tools/delegate'\nimport {\n createDelegateFeedbackHandler,\n DELEGATE_FEEDBACK_DESCRIPTION,\n DELEGATE_FEEDBACK_INPUT_SCHEMA,\n DELEGATE_FEEDBACK_TOOL_NAME,\n} from './tools/delegate-feedback'\nimport {\n createDelegateUiAuditHandler,\n DELEGATE_UI_AUDIT_DESCRIPTION,\n DELEGATE_UI_AUDIT_INPUT_SCHEMA,\n DELEGATE_UI_AUDIT_TOOL_NAME,\n} from './tools/delegate-ui-audit'\nimport {\n createDelegationHistoryHandler,\n DELEGATION_HISTORY_DESCRIPTION,\n DELEGATION_HISTORY_INPUT_SCHEMA,\n DELEGATION_HISTORY_TOOL_NAME,\n} from './tools/delegation-history'\nimport {\n createDelegationStatusHandler,\n DELEGATION_STATUS_DESCRIPTION,\n DELEGATION_STATUS_INPUT_SCHEMA,\n DELEGATION_STATUS_TOOL_NAME,\n} from './tools/delegation-status'\nimport type { TraceContext } from './trace-propagation'\n\nexport type { JsonRpcMessage, JsonRpcResponse, McpToolDescriptor, McpTransport } from './protocol'\n\n/** @experimental */\nexport interface McpServerOptions {\n /**\n * Required to enable `delegate` — the ONE generic delegation verb. Inject the supervisor\n * substrate: its brain `router`, the worker `backend`, and the completion `deliverable`. The\n * supervisor AUTHORS its own worker from the agent's intent, so there is no worker profile to\n * wire here.\n */\n delegateSupervisor?: DelegateHandlerOptions\n /**\n * Required to enable delegate_ui_audit. Wire one that executes an exact\n * agent profile through Runtime and returns the provider-neutral UI audit result.\n */\n uiAuditorDelegate?: UiAuditorDelegate\n /** Override the default in-memory feedback store. */\n feedbackStore?: FeedbackStore\n /** Override the default in-memory task queue. */\n queue?: DelegationTaskQueue\n /**\n * Extra tools to serve alongside the delegation tools, for example\n * `createCoordinationTools(...).tools`. Registered after the built-ins; a\n * duplicate name throws so delegation tools cannot be shadowed silently.\n */\n extraTools?: McpToolDescriptor[]\n /**\n * Inherited trace identity (`readTraceContextFromEnv()`) stamped on every\n * record the DEFAULT queue creates. Ignored when `queue` is supplied —\n * pass `traceContext` to that queue's constructor instead.\n */\n traceContext?: TraceContext\n /** Server display name surfaced via `initialize`. Default `'agent-runtime-mcp'`. */\n serverName?: string\n /** Server version surfaced via `initialize`. Default = the package version baked at build time. */\n serverVersion?: string\n}\n\n/** @experimental */\nexport interface McpServer {\n /** Tools currently registered (depend on which delegates were wired). */\n readonly tools: ReadonlyMap<string, McpToolDescriptor>\n /** The underlying queue — exposed so tests can introspect it. */\n readonly queue: DelegationTaskQueue\n /** The feedback store — exposed for the same reason. */\n readonly feedbackStore: FeedbackStore\n /** Handle a single parsed JSON-RPC message. Returns the response object (or `null` for notifications). */\n handle(message: JsonRpcMessage): Promise<JsonRpcResponse | null>\n /** Drive the server on a stdio-shaped transport until `stop()` is called. */\n serve(transport?: McpTransport): Promise<void>\n /** Stop a `serve` call. Subsequent requests are rejected. */\n stop(): void\n}\n\nconst DEFAULT_SERVER_NAME = 'agent-runtime-mcp'\nconst DEFAULT_SERVER_VERSION = '0.22.0'\n\n/**\n * Stdio JSON-RPC MCP server exposing the delegation tools (`delegate`, `delegate_feedback`, `delegation_status`, `delegation_history`, optional `delegate_ui_audit`) to sandbox coding-harness agents.\n *\n * @experimental\n */\nexport function createMcpServer(options: McpServerOptions = {}): McpServer {\n const queue =\n options.queue ??\n new DelegationTaskQueue(\n options.traceContext !== undefined ? { traceContext: options.traceContext } : {},\n )\n const feedbackStore = options.feedbackStore ?? new InMemoryFeedbackStore()\n const serverName = options.serverName ?? DEFAULT_SERVER_NAME\n const serverVersion = options.serverVersion ?? DEFAULT_SERVER_VERSION\n\n const tools = new Map<string, McpToolDescriptor>()\n\n if (options.delegateSupervisor) {\n tools.set(DELEGATE_TOOL_NAME, {\n name: DELEGATE_TOOL_NAME,\n description: DELEGATE_DESCRIPTION,\n inputSchema: DELEGATE_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateHandler(options.delegateSupervisor),\n })\n }\n if (options.uiAuditorDelegate) {\n tools.set(DELEGATE_UI_AUDIT_TOOL_NAME, {\n name: DELEGATE_UI_AUDIT_TOOL_NAME,\n description: DELEGATE_UI_AUDIT_DESCRIPTION,\n inputSchema: DELEGATE_UI_AUDIT_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateUiAuditHandler({ queue, delegate: options.uiAuditorDelegate }),\n })\n }\n tools.set(DELEGATE_FEEDBACK_TOOL_NAME, {\n name: DELEGATE_FEEDBACK_TOOL_NAME,\n description: DELEGATE_FEEDBACK_DESCRIPTION,\n inputSchema: DELEGATE_FEEDBACK_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateFeedbackHandler({ queue, store: feedbackStore }),\n })\n tools.set(DELEGATION_STATUS_TOOL_NAME, {\n name: DELEGATION_STATUS_TOOL_NAME,\n description: DELEGATION_STATUS_DESCRIPTION,\n inputSchema: DELEGATION_STATUS_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegationStatusHandler({ queue }),\n })\n tools.set(DELEGATION_HISTORY_TOOL_NAME, {\n name: DELEGATION_HISTORY_TOOL_NAME,\n description: DELEGATION_HISTORY_DESCRIPTION,\n inputSchema: DELEGATION_HISTORY_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegationHistoryHandler({ queue }),\n })\n for (const tool of options.extraTools ?? []) {\n if (tools.has(tool.name)) {\n throw new ValidationError(\n `createMcpServer: extra tool \"${tool.name}\" shadows a built-in tool`,\n )\n }\n tools.set(tool.name, tool)\n }\n\n const stdio = createStdioToolServer({\n serverName,\n serverVersion,\n tools: [...tools.values()],\n })\n\n return {\n tools: stdio.tools,\n queue,\n feedbackStore,\n handle: stdio.handle,\n serve: stdio.serve,\n stop: stdio.stop,\n }\n}\n\n/**\n * In-process pair of `Readable` + `Writable` streams suitable for driving\n * `server.serve(...)` from a test. Returns the agent-side stream (the\n * client writes to it) and the server-side stream (the test reads from it).\n *\n * @experimental\n */\nexport function createInProcessTransport(): {\n transport: McpTransport\n clientWrite(line: string): void\n clientClose(): void\n readServer(): Promise<JsonRpcResponse[]>\n} {\n const responses: JsonRpcResponse[] = []\n const input = new Readable({ read() {} })\n const output = new Writable({\n write(chunk, _enc, cb) {\n const text = chunk.toString('utf8')\n for (const line of text.split('\\n')) {\n const trimmed = line.trim()\n if (!trimmed) continue\n try {\n responses.push(JSON.parse(trimmed) as JsonRpcResponse)\n } catch {\n // Non-JSON output should never appear; drop it silently in the\n // test transport rather than crashing.\n }\n }\n cb()\n },\n })\n return {\n transport: { input, output },\n clientWrite(line: string) {\n input.push(`${line}\\n`)\n },\n clientClose() {\n input.push(null)\n },\n async readServer() {\n // Yield to the event loop a few times so async handlers drain.\n for (let i = 0; i < 5; i += 1) await new Promise((r) => setImmediate(r))\n return [...responses]\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,MAAa,6BAA6B;;AAG1C,MAAa,6BAA6B,MAAM;;;;;;;;AAyBhD,SAAgB,0BACd,QACuB;CACvB,OAAO,mBAAmB,MAAM,CAAC,CAAC,KAAK,UAAU;EAC/C,QAAQ,KAAK;EACb,GAAI,KAAK,iBAAiB,KAAA,IAAY,EAAE,cAAc,KAAK,aAAa,IAAI,CAAC;EAC7E,MAAM,KAAK;EACX,MAAM,KAAK;EACX,SAAS,KAAK;EACd,OAAO,KAAK;EACZ,GAAI,OAAO,KAAK,KAAK,KAAK,CAAC,CAAC,SAAS,IAAI,EAAE,MAAM,KAAK,MAAM,IAAI,CAAC;CACnE,EAAE;AACJ;;;;;;;;;;;AAYA,SAAgB,mBACd,OACA,MACuB;CACvB,MAAM,WAAW,MAAM,YAAA;CACvB,MAAM,WAAW,MAAM,YAAA;CACvB,IAAI,QAAQ,KAAK,IAAI,GAAG,MAAM,SAAS,QAAQ;CAC/C,MAAM,QAAQ,MAAM,KAAK,SAAS,KAAK,UAAU,IAAI,CAAC,CAAC,SAAS,CAAC;CACjE,IAAI,QAAQ;CACZ,KAAK,IAAI,IAAI,OAAO,IAAI,MAAM,QAAQ,KAAK,GAAG,SAAS,MAAM;CAC7D,OAAO,QAAQ,MAAM,SAAS,KAAK,QAAQ,UAAU;EACnD,SAAS,MAAM;EACf,SAAS;CACX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KAAK,MAAM,IAAI;CAC3B;CAEA,MAAM,QAAQ,sBAAsB;EAClC;EACA;EACA,OAAO,CAAC,GAAG,MAAM,OAAO,CAAC;CAC3B,CAAC;CAED,OAAO;EACL,OAAO,MAAM;EACb;EACA;EACA,QAAQ,MAAM;EACd,OAAO,MAAM;EACb,MAAM,MAAM;CACd;AACF;;;;;;;;AASA,SAAgB,2BAKd;CACA,MAAM,YAA+B,CAAC;CACtC,MAAM,QAAQ,IAAI,SAAS,EAAE,OAAO,CAAC,EAAE,CAAC;CAiBxC,OAAO;EACL,WAAW;GAAE;GAAO,QAAA,IAjBH,SAAS,EAC1B,MAAM,OAAO,MAAM,IAAI;IACrB,MAAM,OAAO,MAAM,SAAS,MAAM;IAClC,KAAK,MAAM,QAAQ,KAAK,MAAM,IAAI,GAAG;KACnC,MAAM,UAAU,KAAK,KAAK;KAC1B,IAAI,CAAC,SAAS;KACd,IAAI;MACF,UAAU,KAAK,KAAK,MAAM,OAAO,CAAoB;KACvD,QAAQ,CAGR;IACF;IACA,GAAG;GACL,EACF,CAE2B;EAAE;EAC3B,YAAY,MAAc;GACxB,MAAM,KAAK,GAAG,KAAK,GAAG;EACxB;EACA,cAAc;GACZ,MAAM,KAAK,IAAI;EACjB;EACA,MAAM,aAAa;GAEjB,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,MAAM,IAAI,SAAS,MAAM,aAAa,CAAC,CAAC;GACvE,OAAO,CAAC,GAAG,SAAS;EACtB;CACF;AACF"}
@@ -1,4 +1,4 @@
1
- import { C as ExecutorToolCall, J as Scope, Ni as AgentTaskStatus, Pi as BackendErrorDetail, Pn as ExecutorProgress, Tn as TraceSource, Ur as ExecCtx, Wi as RuntimeStreamEvent, b as ExecutorRegistry, di as SandboxClient, f as ExecutorCancellation, ft as UsageEvent, g as ExecutorFactory, mi as Validator, p as ExecutorCancellationRequest, q as Runtime, ri as LoopTokenUsage, s as DefaultVerdict, tt as Spend, y as ExecutorProgressEvent } from "./types-BxjpKpb-.js";
1
+ import { C as ExecutorToolCall, J as Scope, Ni as AgentTaskStatus, Pi as BackendErrorDetail, Pn as ExecutorProgress, Tn as TraceSource, Ur as ExecCtx, Wi as RuntimeStreamEvent, b as ExecutorRegistry, di as SandboxClient, f as ExecutorCancellation, ft as UsageEvent, g as ExecutorFactory, mi as Validator, p as ExecutorCancellationRequest, q as Runtime, ri as LoopTokenUsage, s as DefaultVerdict, tt as Spend, y as ExecutorProgressEvent } from "./types-DANs9CAb.js";
2
2
  import { AgentProfile, AgentProfileMcpServer, AgentProfileValidationResult, HarnessType, ReasoningEffort, StreamEvent } from "@tangle-network/agent-interface";
3
3
  import { AgentRunOutcome } from "@tangle-network/sandbox/runtime";
4
4
  import { ChildProcess } from "node:child_process";
@@ -2046,4 +2046,4 @@ declare function streamAgentTurn(backend: AgentTurnBackend, input: AgentTurnInpu
2046
2046
  declare function collectAgentTurn(stream: AsyncIterable<RuntimeStreamEvent>): Promise<CollectedAgentTurn>;
2047
2047
  //#endregion
2048
2048
  export { decodeHarnessUsage as $, McpTransport as $n, WorktreeCheckRunner as $t, createInbox as A, ToolLoopMessageRecord as An, CreateAgentEnvironmentInput$1 as At, secretEnvOfMcpServer as B, PeerMailReadout as Bn, ResourceRequest as Bt, SteerableSandboxArgs as C, runLocalHarness as Cn, AgentProfileRef$1 as Ct, Inbox as D, ToolLoopChat as Dn, AgentTurnResult$2 as Dt, AuthorityInboxMessage as E, ToolLoopCallContext as En, AgentSessionStatus$1 as Et, ResolvedMcpServerLaunch as F, PeerMailEnvelope as Fn, PlacementInfo as Ft, CodexRolloutStoreRef as G, claimsAuthority as Gn, providerAsSandboxClient as Gt, CodexRolloutIdentity as H, PeerMailSendInput as Hn, WorkspaceRequest as Ht, envKeyProvider as I, PeerMailEvent as In, ProviderAsSandboxClientOptions as It, addHarnessUsage as J, peerMailTools as Jn, CreateTangleSandboxExactProcessProviderOptions as Jt, CodexRolloutTurn as K, createPeerMailbox as Kn, resolveAgentEnvironmentProvider as Kt, mcpSecretEnvMetadataKey as L, PeerMailKind as Ln, ProviderExecutorOptions as Lt, BridgeModelCredential as M, AUTHORITY_MARKERS as Mn, ExecRequest as Mt, BridgeSeam as N, DEFAULT_PEER_MAIL_LIMITS as Nn, ExecResult as Nt, InboxMessage as O, ToolLoopCompaction as On, CheckpointRef as Ot, KeyProvider as P, PEER_MAIL_WIRE_KEY as Pn, ForkRequest as Pt, HarnessUsage as Q, McpToolDescriptor as Qn, InPlaceHarnessResult as Qt, resolveMcpServerLaunch as R, PeerMailLimits as Rn, ProviderLeafOut as Rt, SandboxSteeringOptions as S, parseCodexTokenUsage as Sn, AgentEnvironmentSummary as St, createSteerableSandboxSession as T, ToolSpec as Tn, AgentSessionRef as Tt, CodexRolloutSession as U, PeerMailbox as Un, createAgentEnvironmentProviderRegistry as Ut, CodexForkBoundary as V, PeerMailRefusal as Vn, SandboxClientProviderOptions as Vt, CodexRolloutStoreReader as W, PeerMailboxOptions as Wn, providerAsExecutor as Wt, harnessUsageIsEmpty as X, JsonRpcMessage as Xn, createTangleSandboxExactProcessProvider as Xt, createCodexRolloutStoreReader as Y, peerMailVerbNames as Yn, SandboxControlClient as Yt, readCodexRolloutSession as Z, JsonRpcResponse as Zn, ProviderPlacement as Zt, cliInPlaceExecutor as _, LocalHarness as _n, AgentEnvironmentProvider$1 as _t, StreamAgentTurnOptions as a, DiffResult as an, assertSandboxServedModel as at, createExecutorRegistry as b, harnessSupportsReasoningEffort as bn, AgentEnvironmentQuery as bt, CliInPlaceSeam as c, WorktreeHandle as cn, extractLlmCallEvent as ct, CliWorktreeSeam as d, removeWorktree as dn, sandboxEventServedBackend as dt, WorktreeCommandResult as en, Redactor as er, SandboxLeafOut as et, ExecutorConfig as f, CodexExecutionEvidence as fn, sandboxProgressEvents as ft, SandboxSeam as g, LOCAL_HARNESSES as gn, AgentEnvironmentEvent$1 as gt, RouterToolsSeam as h, DEFAULT_LOCAL_HARNESS as hn, AgentEnvironmentCapabilities$1 as ht, CollectedAgentTurn as i, DiffOptions as in, SandboxUsageLedger as it, BridgeHarnessStore as j, ToolLoopToolCall as jn, DEFAULT_SANDBOX_IDLE_TIMEOUT_SECONDS as jt, PeerInboxMessage as k, ToolLoopCompactionOptions as kn, CheckpointRequest as kt, CliSeam as l, captureWorktreeDiff as ln, mapSandboxEvent as lt, RouterSeam as m, CodexTokenUsage as mn, AgentEnvironment$1 as mt, AgentTurnInput$1 as n, WorktreeProfileMaterializationReceipt as nn, resolveRedactor as nr, SandboxServedBackend as nt, collectAgentTurn as o, GitRunner as on, createSandboxToolPartState as ot, ProviderSeam as p, CodexExecutionPolicy as pn, sumSandboxUsage as pt, CodexStoreDelta as q, isPeerMailEnvelope as qn, sandboxClientAsProvider as qt, AgentTurnUsage as r, CreateWorktreeOptions as rn, SandboxToolPartState as rt, streamAgentTurn as s, RemoveWorktreeOptions as sn, createSandboxUsageLedger as st, AgentTurnBackend as t, WorktreeHarnessResult as tn, defaultRedactor as tr, SandboxOutputMarker as tt, CliWorktreeBridgeSeam as u, createWorktree as un, mapSandboxToolEvent as ut, cliWorktreeExecutor as v, LocalHarnessResult as vn, AgentEnvironmentProviderRef as vt, SteerableSandboxSession as w, RouterTransportConfig as wn, AgentSession as wt, DEFAULT_SANDBOX_STEERING_MAX_TURNS as x, localHarnessExecutable as xn, AgentEnvironmentStatus as xt, createExecutor as y, RunLocalHarnessOptions as yn, AgentEnvironmentProviderRegistry as yt, resolveSecretEnv as z, PeerMailOutcome as zn, ProviderPromptOptions as zt };
2049
- //# sourceMappingURL=stream-agent-turn-r1mtjn0i.d.ts.map
2049
+ //# sourceMappingURL=stream-agent-turn-BYupQNrs.d.ts.map
@@ -1,4 +1,4 @@
1
- import { $i as providerMessageText, Qi as promptOptionsFromAgentTurnInput, Si as mapSandboxToolEvent, Xr as knownExecutionBindingReceipt, Yi as armDeadlineTimer, Zi as promptFromAgentTurnInput, Zr as knownMaterializationReceipt, _i as createSandboxToolPartState, ai as runtimeOwnedPendingExecutorMaterialization, bi as isSandboxTerminalEvent, ci as unknownMaterializationReceipt, et as captureReusableExecutorConfig, ft as InMemoryResultBlobStore, gi as canonicalStreamEventFromSandboxEvent, h as withDriverExecutor, it as createExecutorRegistry, li as executableAgentProfileSnapshot, mi as readSandboxOutcome, n as createSupervisor, ni as runtimeOwnedExecutorExecutionBinding, pi as projectSandboxOutcome, pt as InMemorySpawnJournal, qi as awaitAbortable, qr as authoredProfileDigest, ri as runtimeOwnedExecutorMaterialization, rt as createExecutor, si as unknownExecutionBindingReceipt, vi as createSandboxUsageLedger, vn as observedModelMatchesDeclared, xi as mapSandboxEvent } from "./supervisor-CBtuIoqP.js";
1
+ import { $i as providerMessageText, Qi as promptOptionsFromAgentTurnInput, Si as mapSandboxToolEvent, Xr as knownExecutionBindingReceipt, Yi as armDeadlineTimer, Zi as promptFromAgentTurnInput, Zr as knownMaterializationReceipt, _i as createSandboxToolPartState, ai as runtimeOwnedPendingExecutorMaterialization, bi as isSandboxTerminalEvent, ci as unknownMaterializationReceipt, et as captureReusableExecutorConfig, ft as InMemoryResultBlobStore, gi as canonicalStreamEventFromSandboxEvent, h as withDriverExecutor, it as createExecutorRegistry, li as executableAgentProfileSnapshot, mi as readSandboxOutcome, n as createSupervisor, ni as runtimeOwnedExecutorExecutionBinding, pi as projectSandboxOutcome, pt as InMemorySpawnJournal, qi as awaitAbortable, qr as authoredProfileDigest, ri as runtimeOwnedExecutorMaterialization, rt as createExecutor, si as unknownExecutionBindingReceipt, vi as createSandboxUsageLedger, vn as observedModelMatchesDeclared, xi as mapSandboxEvent } from "./supervisor-DGsHiL1-.js";
2
2
  import { m as ValidationError, p as SessionMismatchError, r as BackendTransportError } from "./errors-DodWX-cb.js";
3
3
  import { c as profileModelExecutionSettings, i as concreteModelId, l as profileProviderModel, o as enforceTokenLimits, s as profileBridgeWireModel, t as assertExecutableAgentProfile } from "./model-policy-BbSCSak0.js";
4
4
  import { agentProfileSchema, canonicalAgentProfileDigest, canonicalCandidateDigest, renderInputPartsAsText } from "@tangle-network/agent-interface";
@@ -2869,4 +2869,4 @@ function structuralRollout(config = {}) {
2869
2869
  //#endregion
2870
2870
  export { collectAgentTurn as A, defaultAnalystInstruction as C, profileOptimizerModelCall as D, profileChatClient as E, InMemoryRuntimeSessionStore as F, newRuntimeSession as I, nowIso as L, createIterableBackend as M, createSandboxPromptBackend as N, optimizerMethod as O, normalizeBackendStreamEvent as P, startOrResumeRuntimeSession as R, ObservationError as S, renderReport as T, depthStrategy as _, defaultStructuralRolloutPolicy as a, sample as b, officialChecksFromMeta as c, selectBestIndex as d, structuralRollout as f, defineStrategy as g, breadthStrategy as h, defaultExtractCandidate as i, streamAgentTurn as j, strategyAuthorMethod as k, resolveEntrySymbol as l, adaptiveRefine as m, compareCheckOutcomes as n, filterAuthoredAsserts as o, visibleCheckScore as p, composeCheckSources as r, modelAuthoredChecks as s, canDisplace as t, sandboxCheckRunner as u, refine as v, observe as w, sampleThenRefine as x, runAgentic as y, touchSession as z };
2871
2871
 
2872
- //# sourceMappingURL=structural-rollout-BLTQRy-B.js.map
2872
+ //# sourceMappingURL=structural-rollout-C6FsCpeq.js.map