@theokit/sdk 4.2.1 → 4.2.2

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 (76) hide show
  1. package/CHANGELOG.md +97 -86
  2. package/dist/a2a/index.cjs.map +1 -1
  3. package/dist/a2a/index.js.map +1 -1
  4. package/dist/a2a/subagent.d.cts +2 -2
  5. package/dist/a2a/subagent.d.ts +2 -2
  6. package/dist/{cron-Y_aM4m4-.d.cts → cron-DxBlp3i4.d.ts} +2 -2
  7. package/dist/{cron-pjzn3jxf.d.ts → cron-afuxJHol.d.cts} +2 -2
  8. package/dist/cron.cjs.map +1 -1
  9. package/dist/cron.d.cts +2 -2
  10. package/dist/cron.d.ts +2 -2
  11. package/dist/cron.js.map +1 -1
  12. package/dist/{errors-B4QB3-Wu.d.ts → errors-DzCWdfkp.d.ts} +1 -1
  13. package/dist/{errors-B03TBTc_.d.cts → errors-VY7_0ki7.d.cts} +1 -1
  14. package/dist/errors.d.cts +2 -2
  15. package/dist/eval.cjs.map +1 -1
  16. package/dist/eval.js.map +1 -1
  17. package/dist/filesystem/index.cjs.map +1 -1
  18. package/dist/filesystem/index.js.map +1 -1
  19. package/dist/filesystem/local-filesystem.d.cts +1 -1
  20. package/dist/filesystem/local-filesystem.d.ts +1 -1
  21. package/dist/filesystem/types.d.cts +1 -1
  22. package/dist/filesystem/types.d.ts +1 -1
  23. package/dist/index.cjs +1 -9
  24. package/dist/index.cjs.map +1 -1
  25. package/dist/index.d.cts +10 -10
  26. package/dist/index.d.ts +10 -10
  27. package/dist/index.js +1 -9
  28. package/dist/index.js.map +1 -1
  29. package/dist/internal/agent-loop/doom-loop-tracker.d.ts +1 -1
  30. package/dist/internal/budget/usage-accumulator.d.ts +1 -1
  31. package/dist/internal/llm/model-identifier.d.ts +1 -1
  32. package/dist/internal/llm/ollama-native.d.ts +2 -2
  33. package/dist/internal/llm/openai.d.ts +1 -1
  34. package/dist/internal/llm/router.d.ts +3 -4
  35. package/dist/internal/memory/adapters/ollama-embedding.d.ts +1 -1
  36. package/dist/internal/memory/embedding-adapter.d.ts +3 -3
  37. package/dist/internal/persistence/index.cjs.map +1 -1
  38. package/dist/internal/persistence/index.js.map +1 -1
  39. package/dist/internal/providers/builtin/vertex.d.ts +1 -1
  40. package/dist/internal/resilience/circuit-breaker.d.ts +1 -1
  41. package/dist/internal/runtime/concurrency/abort-utils.d.ts +1 -1
  42. package/dist/internal/runtime/registry/run-registry.d.ts +0 -1
  43. package/dist/internal/runtime/skills/skill-frontmatter.d.ts +0 -1
  44. package/dist/internal/task/registry.d.ts +0 -3
  45. package/dist/internal/telemetry/span-names.d.ts +0 -1
  46. package/dist/internal/workflow/telemetry.d.ts +0 -2
  47. package/dist/models.cjs.map +1 -1
  48. package/dist/models.js.map +1 -1
  49. package/dist/permission-engine.d.ts +3 -3
  50. package/dist/persistence.cjs.map +1 -1
  51. package/dist/persistence.js.map +1 -1
  52. package/dist/project.cjs.map +1 -1
  53. package/dist/project.js.map +1 -1
  54. package/dist/{run-CqihtbtU.d.cts → run-CsIsK9Gx.d.cts} +7 -7
  55. package/dist/{run-CqihtbtU.d.ts → run-CsIsK9Gx.d.ts} +7 -7
  56. package/dist/skills.cjs.map +1 -1
  57. package/dist/skills.js.map +1 -1
  58. package/dist/subscription/index.cjs.map +1 -1
  59. package/dist/subscription/index.js.map +1 -1
  60. package/dist/subscription/internal/sse-encoder.d.cts +1 -1
  61. package/dist/subscription/internal/sse-encoder.d.ts +1 -1
  62. package/dist/subscription/internal/sse-parser.d.cts +1 -1
  63. package/dist/subscription/internal/sse-parser.d.ts +1 -1
  64. package/dist/task-store.cjs.map +1 -1
  65. package/dist/task-store.js.map +1 -1
  66. package/dist/types/agent.d.ts +1 -1
  67. package/dist/types/processors.d.ts +1 -1
  68. package/dist/types/run.d.ts +2 -2
  69. package/dist/types/usage.d.ts +1 -1
  70. package/dist/workflow.cjs +1 -9
  71. package/dist/workflow.cjs.map +1 -1
  72. package/dist/workflow.d.cts +1 -1
  73. package/dist/workflow.d.ts +1 -1
  74. package/dist/workflow.js +1 -9
  75. package/dist/workflow.js.map +1 -1
  76. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/a2a/agent-mailbox.ts","../../src/a2a/message-bus.ts","../../src/internal/runtime/registry/agent-factory-registry.ts","../../src/a2a/subagent.ts"],"names":["z"],"mappings":";;;;;AAYO,IAAM,eAAN,MAAmB;AAAA,EACf,OAAA;AAAA,EACQ,IAAA;AAAA,EACT,QAAA,GAAkC,IAAA;AAAA,EAE1C,WAAA,CAAY,SAAiB,GAAA,EAAiB;AAC5C,IAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AACf,IAAA,IAAA,CAAK,IAAA,GAAO,GAAA;AACZ,IAAA,IAAA,CAAK,IAAA,CAAK,SAAS,OAAA,EAAS,CAAC,QAAQ,IAAA,CAAK,SAAA,CAAU,GAAG,CAAC,CAAA;AAAA,EAC1D;AAAA,EAEA,UAAU,OAAA,EAA+B;AACvC,IAAA,IAAA,CAAK,QAAA,GAAW,OAAA;AAAA,EAClB;AAAA,EAEA,MAAM,IAAA,CAAK,EAAA,EAAY,OAAA,EAA4D;AACjF,IAAA,OAAO,KAAK,IAAA,CAAK,IAAA,CAAK,IAAA,CAAK,OAAA,EAAS,IAAI,OAAO,CAAA;AAAA,EACjD;AAAA,EAEA,MAAM,OAAA,CACJ,EAAA,EACA,OAAA,EACA,IAAA,EACkB;AAClB,IAAA,OAAO,KAAK,IAAA,CAAK,OAAA,CAAQ,KAAK,OAAA,EAAS,EAAA,EAAI,SAAS,IAAI,CAAA;AAAA,EAC1D;AAAA,EAEA,OAAA,GAAgB;AACd,IAAA,IAAA,CAAK,IAAA,CAAK,UAAA,CAAW,IAAA,CAAK,OAAO,CAAA;AACjC,IAAA,IAAA,CAAK,QAAA,GAAW,IAAA;AAAA,EAClB;AAAA,EAEQ,UAAU,GAAA,EAA0B;AAC1C,IAAA,IAAI,KAAK,QAAA,EAAU;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O,IAAM,WAAN,MAAe;AAAA,EACZ,WAAA,GAAc;AAAA,EAAC;AAAA,EACvB,OAAO,MAAA,CAAO,IAAA,EAAoB,WAAA,GAAc,CAAA,EAAe;AAC7D,IAAA,OAAO,cAAA,CAAe,MAAM,WAAW,CAAA;AAAA,EACzC;AACF","file":"index.cjs","sourcesContent":["/**\n * AgentMailbox — per-agent inbox for A2A communication (T20.1, ADR D453).\n *\n * Wraps MessageBus registration with a convenient API for sending,\n * receiving, and requesting messages.\n *\n * @public\n */\n\nimport type { MessageBus, RequestOptions } from \"./message-bus.js\";\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\nexport class AgentMailbox {\n readonly agentId: string;\n private readonly _bus: MessageBus;\n private _handler: MessageHandler | null = null;\n\n constructor(agentId: string, bus: MessageBus) {\n this.agentId = agentId;\n this._bus = bus;\n this._bus.register(agentId, (msg) => this._dispatch(msg));\n }\n\n onMessage(handler: MessageHandler): void {\n this._handler = handler;\n }\n\n async send(to: string, partial: { type: string; payload: unknown }): Promise<void> {\n return this._bus.send(this.agentId, to, partial);\n }\n\n async request(\n to: string,\n partial: { type: string; payload: unknown },\n opts?: RequestOptions,\n ): Promise<unknown> {\n return this._bus.request(this.agentId, to, partial, opts);\n }\n\n dispose(): void {\n this._bus.unregister(this.agentId);\n this._handler = null;\n }\n\n private _dispatch(msg: A2AMessage): unknown {\n if (this._handler) {\n return this._handler(msg);\n }\n }\n}\n","/**\n * MessageBus — in-process typed message router for A2A (T20.1, ADR D453).\n *\n * Routes messages between agents by ID. Supports fire-and-forget (send)\n * and request/response (request with timeout).\n *\n * @public\n */\n\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\nexport interface RequestOptions {\n timeoutMs?: number;\n}\n\nexport class MessageBus {\n private readonly _handlers = new Map<string, MessageHandler>();\n\n register(agentId: string, handler: MessageHandler): void {\n this._handlers.set(agentId, handler);\n }\n\n unregister(agentId: string): void {\n this._handlers.delete(agentId);\n }\n\n async send(from: string, to: string, partial: { type: string; payload: unknown }): Promise<void> {\n const handler = this._handlers.get(to);\n if (!handler) {\n throw new Error(`Agent \"${to}\" not registered on MessageBus`);\n }\n const message: A2AMessage = {\n ...partial,\n from,\n to,\n timestamp: Date.now(),\n // SE3 — provenance projection of the sender address (thin view over `from`).\n origin: { kind: \"peer\", from },\n };\n // Fire-and-forget: invoke handler but don't await result\n handler(message);\n }\n\n async request(\n from: string,\n to: string,\n partial: { type: string; payload: unknown },\n opts?: RequestOptions,\n ): Promise<unknown> {\n const handler = this._handlers.get(to);\n if (!handler) {\n throw new Error(`Agent \"${to}\" not registered on MessageBus`);\n }\n const timeoutMs = opts?.timeoutMs ?? 30_000;\n const message: A2AMessage = {\n ...partial,\n from,\n to,\n timestamp: Date.now(),\n // SE3 — provenance projection of the sender address (thin view over `from`).\n origin: { kind: \"peer\", from },\n };\n // Hold the timer so it can be cleared once the race settles — otherwise a\n // successful request leaks a live `setTimeout` that keeps the Node event loop\n // alive (the process hangs after the reply). Cleared in `finally`.\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n Promise.resolve(handler(message)),\n new Promise((_, reject) => {\n timer = setTimeout(\n () =>\n reject(new Error(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer !== undefined) clearTimeout(timer);\n }\n }\n\n has(agentId: string): boolean {\n return this._handlers.has(agentId);\n }\n\n listAgents(): string[] {\n return [...this._handlers.keys()];\n }\n}\n","/**\n * Internal DI seam for the public `Agent` facade.\n *\n * Several internal subsystems (`LocalAgent.runUntil`/`fork`, eval, scorers,\n * cron) need to invoke `Agent.create`/`prompt`/`get`/`resume`/`batch`. A direct\n * `import { Agent }` would invert the public-api -> internal dependency\n * direction (and `agent.ts` already imports much of `internal/`). This module\n * is the inversion seam: `agent.ts` registers the facade at module-init time;\n * internal consumers read it on-demand via `getAgentFacade()`. The\n * `internal-must-not-import-facade` dependency-cruiser rule enforces that no\n * `internal/*` module bypasses this seam with a direct facade import.\n *\n * Throws if accessed before registration — should never happen because\n * `agent.ts` evaluation always precedes any internal call (the consumer must\n * obtain a handle from the `Agent` facade first).\n *\n * @internal\n */\n\nimport type { AgentOptions, SDKAgent, SDKAgentInfo } from \"../../../types/agent.js\";\nimport type { BatchItem, BatchOptions, BatchResult } from \"../../../types/batch.js\";\nimport type { RunResult } from \"../../../types/run.js\";\n\n/**\n * The subset of the public `Agent` static surface that internal subsystems\n * consume through the inversion seam. Each member mirrors the matching\n * `Agent.*` signature so registration is a thin pass-through.\n *\n * @internal\n */\nexport interface AgentFacadePort {\n create: (options: AgentOptions) => Promise<SDKAgent>;\n delete: (agentId: string) => Promise<void>;\n prompt: (message: string, options: AgentOptions) => Promise<RunResult>;\n get: (agentId: string) => Promise<SDKAgentInfo>;\n resume: (agentId: string, options?: Partial<AgentOptions>) => Promise<SDKAgent>;\n batch: (\n prompts: ReadonlyArray<string | BatchItem>,\n options: BatchOptions,\n ) => Promise<BatchResult[]>;\n}\n\nlet registered: AgentFacadePort | undefined;\n\n/**\n * Registered by `agent.ts` at module-init time. Idempotent: re-registration\n * replaces the previous reference (useful for tests that swap implementations).\n *\n * @internal\n */\nexport function setAgentFacade(facade: AgentFacadePort): void {\n registered = facade;\n}\n\n/**\n * Resolve the registered `Agent` facade for internal subsystems.\n *\n * @internal\n */\nexport function getAgentFacade(): AgentFacadePort {\n if (registered === undefined) {\n throw new Error(\n \"internal: Agent facade not registered. The `agent.ts` module must be loaded before internal subsystems (LocalAgent.runUntil/fork, eval, scorers, cron) invoke it.\",\n );\n }\n return registered;\n}\n","/**\n * Subagent delegation — declarative child agent invocable as a tool.\n *\n * Per ADR D2: `defineSubAgent(spec)` returns a `CustomTool` that, when\n * invoked by the LLM, creates a child agent and sends the input as a\n * message. EC-2: delegation depth tracked to prevent infinite recursion.\n *\n * SE10 — the handler forwards the parent run's `AbortSignal` to the child.\n * SE11 — optional `onDelegationStart` / `onDelegationComplete` lifecycle hooks\n * let the caller reject, rewrite, observe, or annotate a delegation.\n *\n * @public\n */\n\nimport { z } from \"zod\";\nimport type { Plugin } from \"../internal/plugins/types.js\";\nimport { getAgentFacade } from \"../internal/runtime/registry/agent-factory-registry.js\";\nimport type {\n AgentDefinition,\n AgentOptions,\n CustomTool,\n ToolContextMessage,\n} from \"../types/agent.js\";\nimport type { ModelSelection } from \"../types/agent-prims.js\";\nimport type { Run } from \"../types/run.js\";\n\n/**\n * Credentials a parent agent hands down to its subagent tools so the child\n * inherits the parent's auth (and, absent an explicit `spec.model`, its model).\n * @internal\n */\nexport interface InheritedCredentials {\n readonly apiKey?: string;\n readonly model?: ModelSelection;\n /**\n * #55 — the parent's code-registered plugins (e.g. a `PermissionPlugin`) handed\n * down so the child runs under the SAME policy. Without this, a delegated child's\n * inner tool calls escape the parent's argument-level permission gate. First-party\n * delegation path only — never exposed to third-party tool `ctx`.\n */\n readonly plugins?: readonly Plugin[];\n}\n\n/**\n * Private per-tool setter installed on subagent tools. Kept off the public\n * `CustomTool` shape (and off every tool handler's `ctx`) so the parent's API\n * key never reaches third-party tool code — only the SDK's own delegation path.\n */\nconst INHERIT_CREDENTIALS = Symbol(\"theokit.subagent.inheritCredentials\");\n\ntype CredentialSink = (creds: InheritedCredentials) => void;\n\n/**\n * Called by the local runtime for every tool in a run: if the tool is a\n * subagent, its child agent inherits the parent's `apiKey`/`model`. A no-op for\n * any other tool (third-party tools never receive the parent's credentials).\n * @internal\n */\nexport function inheritSubAgentCredentials(tool: CustomTool, creds: InheritedCredentials): void {\n const sink = (tool as { [INHERIT_CREDENTIALS]?: CredentialSink })[INHERIT_CREDENTIALS];\n if (typeof sink === \"function\") sink(creds);\n}\n\n/** Arguments passed to {@link SubAgentSpec.messageFilter} (SE12). */\nexport interface MessageFilterArgs {\n /** The supervisor transcript (read-only text projection) available to this delegation. */\n messages: readonly ToolContextMessage[];\n /** The prompt about to be delegated (after any `onDelegationStart` rewrite). */\n input: string;\n /** The subagent's name. */\n name: string;\n}\n\n/** Context passed to {@link SubAgentSpec.onDelegationStart} before the child runs. */\nexport interface DelegationStartContext {\n input: string;\n name: string;\n /**\n * SE15 — 1-based count of times THIS subagent tool has been invoked (a\n * per-`defineSubAgent`-instance counter). Incremented before this hook runs;\n * a rejected delegation still counts. Enables reject-after-N patterns.\n */\n iteration: number;\n}\n\n/**\n * Decision returned from {@link SubAgentSpec.onDelegationStart}. Discriminated on\n * `proceed` so a rejection (`proceed: false` + `rejectionReason`) and an approval\n * (`modifiedInput`) cannot be mixed into one nonsensical object.\n */\nexport type DelegationStartDecision =\n | { proceed: false; rejectionReason?: string }\n | {\n proceed?: true;\n modifiedInput?: string;\n /** SE13 — cap the child's iteration count (forwarded as `SendOptions.maxIterations`). */\n modifiedMaxSteps?: number;\n };\n\n/** Context passed to {@link SubAgentSpec.onDelegationComplete} after the child settles. */\nexport interface DelegationCompleteContext {\n input: string;\n name: string;\n /** The child's text result (present on success). */\n result?: string;\n /** The error the child threw (present on failure); the error is still re-thrown. */\n error?: unknown;\n /** SE15 — the same 1-based iteration this delegation's `onDelegationStart` saw. */\n iteration: number;\n}\n\n/**\n * The return of a delegation hook: a decision, a promise of one, or nothing —\n * `void` lets a side-effect-only callback (`(ctx) => { log(ctx) }`) type-check,\n * which is the common case (mirrors Mastra's `async ctx => { ... }` hooks).\n */\n// biome-ignore lint/suspicious/noConfusingVoidType: `void` is the idiomatic return for an optional-return callback; the rule false-positives on callback return unions.\ntype DelegationHookResult<T> = T | void | Promise<T | void>;\n\n/** Decision returned from {@link SubAgentSpec.onDelegationComplete}. */\nexport interface DelegationCompleteDecision {\n /** Appended to the child's result string. */\n feedback?: string;\n}\n\nexport interface SubAgentSpec {\n name: string;\n description: string;\n instructions: string;\n model?: string;\n tools?: CustomTool[];\n maxDelegationDepth?: number;\n /**\n * SE11 — called before the supervisor delegates. Return `{ proceed: false }`\n * to reject (the child never runs and `rejectionReason` becomes the tool\n * result), or `{ modifiedInput }` to rewrite the delegated prompt. A throwing\n * hook surfaces (never silently swallowed).\n */\n onDelegationStart?: (\n ctx: DelegationStartContext,\n ) => DelegationHookResult<DelegationStartDecision>;\n /**\n * SE11 — called after the delegation settles. On success `ctx.result` is set\n * and an optional `{ feedback }` is appended to it. On failure `ctx.error` is\n * set and the original error is ALWAYS re-thrown after this hook runs — a throw\n * from this hook on the error path is suppressed so it cannot mask the\n * delegation's real failure (on the success path a throw does propagate).\n */\n onDelegationComplete?: (\n ctx: DelegationCompleteContext,\n ) => DelegationHookResult<DelegationCompleteDecision>;\n /**\n * SE12 — opt-in parent-context forwarding. When set, the supervisor transcript\n * (`ctx.messages`, a read-only text projection) is passed to this filter and the\n * returned subset is forwarded to the child as a role-tagged context preamble\n * prepended to the delegated input. When ABSENT the child runs input-only —\n * memory isolation stays the default. A filter returning `[]` forwards nothing.\n * A throwing filter propagates (fail-fast, never swallowed — same contract as\n * `onDelegationStart`); the delegation surfaces as a tool error.\n */\n messageFilter?: (args: MessageFilterArgs) => readonly ToolContextMessage[];\n /**\n * SE14 — opt-in subagent result-context control. When `true`, the child's\n * completed tool-call results (name + result) are appended to the delegation\n * payload returned to the supervisor, inside a `<subagent-tool-results>` block.\n * When absent/`false` the delegation returns the child's final text only —\n * text-only stays the default (Mastra's scoped posture). See ADR 0006.\n */\n includeToolResults?: boolean;\n}\n\nexport class MaxDelegationDepthError extends Error {\n readonly code = \"max_delegation_depth\" as const;\n constructor(\n public readonly currentDepth: number,\n public readonly maxDepth: number,\n ) {\n super(`Max delegation depth ${maxDepth} exceeded (current: ${currentDepth})`);\n this.name = \"MaxDelegationDepthError\";\n }\n}\n\n/**\n * Run the `onDelegationStart` hook; returns either a rejection or the (possibly\n * rewritten) input plus the optional SE13 `maxSteps` cap.\n */\nasync function applyDelegationStart(\n spec: SubAgentSpec,\n input: string,\n iteration: number,\n): Promise<{ reject: string } | { input: string; maxSteps?: number }> {\n if (spec.onDelegationStart === undefined) return { input };\n const decision = await spec.onDelegationStart({ input, name: spec.name, iteration });\n if (decision === undefined) return { input };\n if (decision.proceed === false)\n return { reject: decision.rejectionReason ?? \"(delegation rejected)\" };\n return {\n input: decision.modifiedInput ?? input,\n ...(decision.modifiedMaxSteps !== undefined ? { maxSteps: decision.modifiedMaxSteps } : {}),\n };\n}\n\n/**\n * SE14 — replay the child run's stream (a safe post-`wait()` idiom — the run buffers\n * events, `stream()` replays them) and collect every completed tool-call result into\n * a delimited block. Returns `\"\"` when the child ran no completed tool calls. See ADR 0006.\n */\nasync function collectChildToolResults(run: Run): Promise<string> {\n const lines: string[] = [];\n for await (const event of run.stream()) {\n if (event.type === \"tool_call\" && event.status === \"completed\") {\n const rendered =\n typeof event.result === \"string\" ? event.result : JSON.stringify(event.result ?? null);\n lines.push(`${event.name}: ${rendered}`);\n }\n }\n if (lines.length === 0) return \"\";\n return `\\n\\n<subagent-tool-results>\\n${lines.join(\"\\n\")}\\n</subagent-tool-results>`;\n}\n\n/**\n * Create the transient child agent and send the input, composing every forwarded\n * `SendOptions` onto ONE `send` call — SE10 `signal` + SE13 `maxIterations`. Absent\n * every option ⇒ the pre-SE10 single-arg `send(input)` shape. SE14 — when\n * `includeToolResults` is set, append the child's completed tool results. Dispose in `finally`.\n */\n/**\n * Build the child agent's `Agent.create` options: the child inherits the parent's\n * apiKey (else `Agent.create` throws \"Missing API key\"), its model (unless the spec\n * overrides it), and — #55 — the parent's plugins (permission gate/guards) so the\n * child's inner tool calls run under the same policy.\n */\nfunction buildChildCreateOptions(\n spec: SubAgentSpec,\n inherited: InheritedCredentials | undefined,\n): AgentOptions {\n const model: string | ModelSelection | undefined = spec.model\n ? { id: spec.model }\n : inherited?.model;\n return {\n ...(inherited?.apiKey !== undefined ? { apiKey: inherited.apiKey } : {}),\n ...(model !== undefined ? { model } : {}),\n ...(inherited?.plugins !== undefined ? { plugins: inherited.plugins } : {}),\n systemPrompt: spec.instructions,\n tools: spec.tools ?? [],\n };\n}\n\nasync function runChildAgent(\n spec: SubAgentSpec,\n input: string,\n signal: AbortSignal | undefined,\n maxSteps: number | undefined,\n inherited: InheritedCredentials | undefined,\n): Promise<string> {\n // SE45 cycle 3 — use the registered Agent facade via the DIP seam\n // (agent-factory-registry) instead of a dynamic `import(\"../agent.js\")`.\n // This removes the last madge cycle (a2a/subagent -> agent -> ... -> real-local-run-tools\n // -> a2a/subagent): the facade registers itself at module-init via setAgentFacade,\n // so subagent depends only on the registry port, never on the facade module.\n const agent = await getAgentFacade().create(buildChildCreateOptions(spec, inherited));\n try {\n const sendOptions: {\n signal?: AbortSignal;\n maxIterations?: number;\n origin?: import(\"../types/run.js\").MessageOrigin;\n } = {\n ...(signal !== undefined ? { signal } : {}),\n ...(maxSteps !== undefined ? { maxIterations: maxSteps } : {}),\n // SE3 — a delegated child's turn is initiated by the coordinating parent.\n origin: { kind: \"coordinator\" },\n };\n const run = await agent.send(input, sendOptions);\n const result = await run.wait();\n const text = result.result ?? \"(no response)\";\n // SE14 — text-only by default; opt-in appends the child's tool results.\n return spec.includeToolResults === true ? text + (await collectChildToolResults(run)) : text;\n } finally {\n agent.dispose();\n }\n}\n\n/**\n * Best-effort error-path notification: run `onDelegationComplete` with the child's\n * error so the caller can observe the failure. The observer's own throw (sync or\n * async) is suppressed here so it cannot mask the delegation's real error, which the\n * handler re-throws next.\n */\nasync function notifyDelegationError(\n spec: SubAgentSpec,\n input: string,\n error: unknown,\n iteration: number,\n): Promise<void> {\n if (spec.onDelegationComplete === undefined) return;\n try {\n await spec.onDelegationComplete({ input, name: spec.name, error, iteration });\n } catch {\n // Subordinate to `error`; the child's real cause wins.\n }\n}\n\n/**\n * SE12 — apply `messageFilter` (if set) and prepend the filtered supervisor\n * transcript to the delegated input as a role-tagged context preamble. Absent\n * filter OR no messages OR an empty filtered subset ⇒ the original input\n * (isolation-by-default preserved).\n */\nfunction applyMessageFilter(\n spec: SubAgentSpec,\n input: string,\n messages: readonly ToolContextMessage[] | undefined,\n): string {\n if (spec.messageFilter === undefined || messages === undefined) return input;\n const filtered = spec.messageFilter({ messages, input, name: spec.name });\n if (filtered.length === 0) return input;\n const preamble = filtered.map((m) => `${m.role}: ${m.content}`).join(\"\\n\");\n return `Prior conversation:\\n${preamble}\\n\\nTask:\\n${input}`;\n}\n\n/** Run the success-path `onDelegationComplete` hook; appends its `feedback` to the result. */\nasync function applyDelegationComplete(\n spec: SubAgentSpec,\n input: string,\n result: string,\n iteration: number,\n): Promise<string> {\n if (spec.onDelegationComplete === undefined) return result;\n const completion = await spec.onDelegationComplete({ input, name: spec.name, result, iteration });\n return completion?.feedback !== undefined ? result + completion.feedback : result;\n}\n\nfunction defineSubAgent(spec: SubAgentSpec, _parentDepth = 0): CustomTool {\n const currentDepth = _parentDepth + 1;\n const maxDepth = spec.maxDelegationDepth ?? 3;\n\n if (currentDepth > maxDepth) {\n throw new MaxDelegationDepthError(currentDepth, maxDepth);\n }\n\n // Zod for RUNTIME validation of the tool_use input …\n const inputZod = z.object({\n input: z.string().describe(\"Task for the subagent\"),\n });\n // … and a real Draft-7 JSON Schema for the LLM. `CustomTool.inputSchema` is sent\n // to the model verbatim; a raw Zod object would serialize to garbage, so the\n // model emits malformed input that fails `inputZod.parse` and the delegation\n // never runs (the previous bug — the schema and the validator are now distinct).\n const inputSchema: Record<string, unknown> = {\n type: \"object\",\n properties: {\n input: { type: \"string\", description: \"Task for the subagent\" },\n },\n required: [\"input\"],\n additionalProperties: false,\n };\n\n // SE15 — per-instance delegation counter, surfaced as `iteration` on the hook\n // contexts. Incremented once per handler invocation before onDelegationStart.\n let iteration = 0;\n\n // Credentials handed down by the parent runtime (see `inheritSubAgentCredentials`).\n let inherited: InheritedCredentials | undefined;\n\n const tool: CustomTool = {\n name: spec.name,\n description: spec.description,\n inputSchema,\n handler: async (\n rawInput: Record<string, unknown>,\n ctx?: {\n signal?: AbortSignal;\n context?: unknown;\n messages?: readonly ToolContextMessage[];\n },\n ): Promise<string> => {\n const { input: parsed } = inputZod.parse(rawInput);\n iteration += 1; // SE15 — before onDelegationStart; a rejected delegation still counts.\n // Pin THIS invocation's iteration before any await so a concurrent invocation\n // bumping the shared counter cannot change the value onDelegationComplete /\n // notifyDelegationError observe — they see the same iteration onDelegationStart did.\n const capturedIteration = iteration;\n\n const start = await applyDelegationStart(spec, parsed, capturedIteration);\n if (\"reject\" in start) return start.reject;\n // SE12 — opt-in: forward the filtered supervisor transcript as a preamble.\n const input = applyMessageFilter(spec, start.input, ctx?.messages);\n\n let result: string;\n try {\n // SE13 — apply the optional onDelegationStart maxSteps cap on the child send.\n result = await runChildAgent(spec, input, ctx?.signal, start.maxSteps, inherited);\n } catch (error) {\n // SE11 — notify the completion hook of the failure (best-effort observer),\n // then re-throw the ORIGINAL error (Rule 8: never swallow the delegation's\n // own failure).\n await notifyDelegationError(spec, input, error, capturedIteration);\n throw error;\n }\n return applyDelegationComplete(spec, input, result, capturedIteration);\n },\n };\n\n // Install the private credential sink (off the public shape + off every ctx).\n Object.defineProperty(tool, INHERIT_CREDENTIALS, {\n value: ((creds: InheritedCredentials) => {\n inherited = creds;\n }) satisfies CredentialSink,\n enumerable: false,\n });\n\n return tool;\n}\n\n/** SE36 — `SubAgent.create` replaces `defineSubAgent` (ADR 0015). @public */\nexport class SubAgent {\n private constructor() {}\n static create(spec: SubAgentSpec, parentDepth = 0): CustomTool {\n return defineSubAgent(spec, parentDepth);\n }\n}\n\n/**\n * Convert a parent's declarative `agents` map ({@link AgentDefinition} per key)\n * into delegation tools for the LOCAL runtime — the counterpart of the\n * cloud/fixture subagent wiring. Each child inherits the parent's `apiKey`/model\n * via {@link inheritSubAgentCredentials}; `def.model` overrides the model\n * (`\"inherit\"` keeps the parent's), and `def.tools` scopes the child to that\n * subset of the parent's tools (absent → the parent's full toolset, per the\n * `AgentDefinition.tools` contract).\n *\n * Per-subagent `mcpServers` on an `AgentDefinition` is honored by the cloud\n * runtime only; it is not wired into local delegation here.\n *\n * @internal\n */\nexport function subAgentToolsFromDefinitions(\n agents: Record<string, AgentDefinition>,\n parentTools: readonly CustomTool[],\n): CustomTool[] {\n return Object.entries(agents).map(([name, def]) => {\n const whitelist =\n Array.isArray(def.tools) && def.tools.length > 0 ? new Set(def.tools) : undefined;\n const childTools = whitelist ? parentTools.filter((t) => whitelist.has(t.name)) : parentTools;\n return defineSubAgent({\n name,\n description: def.description,\n instructions: def.prompt,\n ...(def.model !== undefined && def.model !== \"inherit\" ? { model: def.model.id } : {}),\n tools: [...childTools],\n });\n });\n}\n"]}
1
+ {"version":3,"sources":["../../src/a2a/agent-mailbox.ts","../../src/a2a/message-bus.ts","../../src/internal/runtime/registry/agent-factory-registry.ts","../../src/a2a/subagent.ts"],"names":["z"],"mappings":";;;;;AAYO,IAAM,eAAN,MAAmB;AAAA,EACf,OAAA;AAAA,EACQ,IAAA;AAAA,EACT,QAAA,GAAkC,IAAA;AAAA,EAE1C,WAAA,CAAY,SAAiB,GAAA,EAAiB;AAC5C,IAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AACf,IAAA,IAAA,CAAK,IAAA,GAAO,GAAA;AACZ,IAAA,IAAA,CAAK,IAAA,CAAK,SAAS,OAAA,EAAS,CAAC,QAAQ,IAAA,CAAK,SAAA,CAAU,GAAG,CAAC,CAAA;AAAA,EAC1D;AAAA,EAEA,UAAU,OAAA,EAA+B;AACvC,IAAA,IAAA,CAAK,QAAA,GAAW,OAAA;AAAA,EAClB;AAAA,EAEA,MAAM,IAAA,CAAK,EAAA,EAAY,OAAA,EAA4D;AACjF,IAAA,OAAO,KAAK,IAAA,CAAK,IAAA,CAAK,IAAA,CAAK,OAAA,EAAS,IAAI,OAAO,CAAA;AAAA,EACjD;AAAA,EAEA,MAAM,OAAA,CACJ,EAAA,EACA,OAAA,EACA,IAAA,EACkB;AAClB,IAAA,OAAO,KAAK,IAAA,CAAK,OAAA,CAAQ,KAAK,OAAA,EAAS,EAAA,EAAI,SAAS,IAAI,CAAA;AAAA,EAC1D;AAAA,EAEA,OAAA,GAAgB;AACd,IAAA,IAAA,CAAK,IAAA,CAAK,UAAA,CAAW,IAAA,CAAK,OAAO,CAAA;AACj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C,MAAA,SAAA,GAAY,KAAA;AAAA,IACd,CAAA,CAAA;AAAA,IACA,UAAA,EAAY;AAAA,GACb,CAAA;AAED,EAAA,OAAO,IAAA;AACT;AAGO,IAAM,WAAN,MAAe;AAAA,EACZ,WAAA,GAAc;AAAA,EAAC;AAAA,EACvB,OAAO,MAAA,CAAO,IAAA,EAAoB,WAAA,GAAc,CAAA,EAAe;AAC7D,IAAA,OAAO,cAAA,CAAe,MAAM,WAAW,CAAA;AAAA,EACzC;AACF","file":"index.cjs","sourcesContent":["/**\n * AgentMailbox — per-agent inbox for A2A communication (T20.1, ADR D453).\n *\n * Wraps MessageBus registration with a convenient API for sending,\n * receiving, and requesting messages.\n *\n * @public\n */\n\nimport type { MessageBus, RequestOptions } from \"./message-bus.js\";\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\nexport class AgentMailbox {\n readonly agentId: string;\n private readonly _bus: MessageBus;\n private _handler: MessageHandler | null = null;\n\n constructor(agentId: string, bus: MessageBus) {\n this.agentId = agentId;\n this._bus = bus;\n this._bus.register(agentId, (msg) => this._dispatch(msg));\n }\n\n onMessage(handler: MessageHandler): void {\n this._handler = handler;\n }\n\n async send(to: string, partial: { type: string; payload: unknown }): Promise<void> {\n return this._bus.send(this.agentId, to, partial);\n }\n\n async request(\n to: string,\n partial: { type: string; payload: unknown },\n opts?: RequestOptions,\n ): Promise<unknown> {\n return this._bus.request(this.agentId, to, partial, opts);\n }\n\n dispose(): void {\n this._bus.unregister(this.agentId);\n this._handler = null;\n }\n\n private _dispatch(msg: A2AMessage): unknown {\n if (this._handler) {\n return this._handler(msg);\n }\n }\n}\n","/**\n * MessageBus — in-process typed message router for A2A (T20.1, ADR D453).\n *\n * Routes messages between agents by ID. Supports fire-and-forget (send)\n * and request/response (request with timeout).\n *\n * @public\n */\n\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\nexport interface RequestOptions {\n timeoutMs?: number;\n}\n\nexport class MessageBus {\n private readonly _handlers = new Map<string, MessageHandler>();\n\n register(agentId: string, handler: MessageHandler): void {\n this._handlers.set(agentId, handler);\n }\n\n unregister(agentId: string): void {\n this._handlers.delete(agentId);\n }\n\n async send(from: string, to: string, partial: { type: string; payload: unknown }): Promise<void> {\n const handler = this._handlers.get(to);\n if (!handler) {\n throw new Error(`Agent \"${to}\" not registered on MessageBus`);\n }\n const message: A2AMessage = {\n ...partial,\n from,\n to,\n timestamp: Date.now(),\n // SE3 — provenance projection of the sender address (thin view over `from`).\n origin: { kind: \"peer\", from },\n };\n // Fire-and-forget: invoke handler but don't await result\n handler(message);\n }\n\n async request(\n from: string,\n to: string,\n partial: { type: string; payload: unknown },\n opts?: RequestOptions,\n ): Promise<unknown> {\n const handler = this._handlers.get(to);\n if (!handler) {\n throw new Error(`Agent \"${to}\" not registered on MessageBus`);\n }\n const timeoutMs = opts?.timeoutMs ?? 30_000;\n const message: A2AMessage = {\n ...partial,\n from,\n to,\n timestamp: Date.now(),\n // SE3 — provenance projection of the sender address (thin view over `from`).\n origin: { kind: \"peer\", from },\n };\n // Hold the timer so it can be cleared once the race settles — otherwise a\n // successful request leaks a live `setTimeout` that keeps the Node event loop\n // alive (the process hangs after the reply). Cleared in `finally`.\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n Promise.resolve(handler(message)),\n new Promise((_, reject) => {\n timer = setTimeout(\n () =>\n reject(new Error(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer !== undefined) clearTimeout(timer);\n }\n }\n\n has(agentId: string): boolean {\n return this._handlers.has(agentId);\n }\n\n listAgents(): string[] {\n return [...this._handlers.keys()];\n }\n}\n","/**\n * Internal DI seam for the public `Agent` facade.\n *\n * Several internal subsystems (`LocalAgent.runUntil`/`fork`, eval, scorers,\n * cron) need to invoke `Agent.create`/`prompt`/`get`/`resume`/`batch`. A direct\n * `import { Agent }` would invert the public-api -> internal dependency\n * direction (and `agent.ts` already imports much of `internal/`). This module\n * is the inversion seam: `agent.ts` registers the facade at module-init time;\n * internal consumers read it on-demand via `getAgentFacade()`. The\n * `internal-must-not-import-facade` dependency-cruiser rule enforces that no\n * `internal/*` module bypasses this seam with a direct facade import.\n *\n * Throws if accessed before registration — should never happen because\n * `agent.ts` evaluation always precedes any internal call (the consumer must\n * obtain a handle from the `Agent` facade first).\n *\n * @internal\n */\n\nimport type { AgentOptions, SDKAgent, SDKAgentInfo } from \"../../../types/agent.js\";\nimport type { BatchItem, BatchOptions, BatchResult } from \"../../../types/batch.js\";\nimport type { RunResult } from \"../../../types/run.js\";\n\n/**\n * The subset of the public `Agent` static surface that internal subsystems\n * consume through the inversion seam. Each member mirrors the matching\n * `Agent.*` signature so registration is a thin pass-through.\n *\n * @internal\n */\nexport interface AgentFacadePort {\n create: (options: AgentOptions) => Promise<SDKAgent>;\n delete: (agentId: string) => Promise<void>;\n prompt: (message: string, options: AgentOptions) => Promise<RunResult>;\n get: (agentId: string) => Promise<SDKAgentInfo>;\n resume: (agentId: string, options?: Partial<AgentOptions>) => Promise<SDKAgent>;\n batch: (\n prompts: ReadonlyArray<string | BatchItem>,\n options: BatchOptions,\n ) => Promise<BatchResult[]>;\n}\n\nlet registered: AgentFacadePort | undefined;\n\n/**\n * Registered by `agent.ts` at module-init time. Idempotent: re-registration\n * replaces the previous reference (useful for tests that swap implementations).\n *\n * @internal\n */\nexport function setAgentFacade(facade: AgentFacadePort): void {\n registered = facade;\n}\n\n/**\n * Resolve the registered `Agent` facade for internal subsystems.\n *\n * @internal\n */\nexport function getAgentFacade(): AgentFacadePort {\n if (registered === undefined) {\n throw new Error(\n \"internal: Agent facade not registered. The `agent.ts` module must be loaded before internal subsystems (LocalAgent.runUntil/fork, eval, scorers, cron) invoke it.\",\n );\n }\n return registered;\n}\n","/**\n * Subagent delegation — declarative child agent invocable as a tool.\n *\n * Per ADR D2: `defineSubAgent(spec)` returns a `CustomTool` that, when\n * invoked by the LLM, creates a child agent and sends the input as a\n * message. EC-2: delegation depth tracked to prevent infinite recursion.\n *\n * SE10 — the handler forwards the parent run's `AbortSignal` to the child.\n * SE11 — optional `onDelegationStart` / `onDelegationComplete` lifecycle hooks\n * let the caller reject, rewrite, observe, or annotate a delegation.\n *\n * @public\n */\n\nimport { z } from \"zod\";\nimport type { Plugin } from \"../internal/plugins/types.js\";\nimport { getAgentFacade } from \"../internal/runtime/registry/agent-factory-registry.js\";\nimport type {\n AgentDefinition,\n AgentOptions,\n CustomTool,\n ToolContextMessage,\n} from \"../types/agent.js\";\nimport type { ModelSelection } from \"../types/agent-prims.js\";\nimport type { Run } from \"../types/run.js\";\n\n/**\n * Credentials a parent agent hands down to its subagent tools so the child\n * inherits the parent's auth (and, absent an explicit `spec.model`, its model).\n * @internal\n */\nexport interface InheritedCredentials {\n readonly apiKey?: string;\n readonly model?: ModelSelection;\n /**\n * #55 — the parent's code-registered plugins (e.g. a `PermissionPlugin`) handed\n * down so the child runs under the SAME policy. Without this, a delegated child's\n * inner tool calls escape the parent's argument-level permission gate. First-party\n * delegation path only — never exposed to third-party tool `ctx`.\n */\n readonly plugins?: readonly Plugin[];\n}\n\n/**\n * Private per-tool setter installed on subagent tools. Kept off the public\n * `CustomTool` shape (and off every tool handler's `ctx`) so the parent's API\n * key never reaches third-party tool code — only the SDK's own delegation path.\n */\nconst INHERIT_CREDENTIALS = Symbol(\"theokit.subagent.inheritCredentials\");\n\ntype CredentialSink = (creds: InheritedCredentials) => void;\n\n/**\n * Called by the local runtime for every tool in a run: if the tool is a\n * subagent, its child agent inherits the parent's `apiKey`/`model`. A no-op for\n * any other tool (third-party tools never receive the parent's credentials).\n * @internal\n */\nexport function inheritSubAgentCredentials(tool: CustomTool, creds: InheritedCredentials): void {\n const sink = (tool as { [INHERIT_CREDENTIALS]?: CredentialSink })[INHERIT_CREDENTIALS];\n if (typeof sink === \"function\") sink(creds);\n}\n\n/** Arguments passed to {@link SubAgentSpec.messageFilter} (SE12). */\nexport interface MessageFilterArgs {\n /** The supervisor transcript (read-only text projection) available to this delegation. */\n messages: readonly ToolContextMessage[];\n /** The prompt about to be delegated (after any `onDelegationStart` rewrite). */\n input: string;\n /** The subagent's name. */\n name: string;\n}\n\n/** Context passed to {@link SubAgentSpec.onDelegationStart} before the child runs. */\nexport interface DelegationStartContext {\n input: string;\n name: string;\n /**\n * SE15 — 1-based count of times THIS subagent tool has been invoked (a\n * per-`defineSubAgent`-instance counter). Incremented before this hook runs;\n * a rejected delegation still counts. Enables reject-after-N patterns.\n */\n iteration: number;\n}\n\n/**\n * Decision returned from {@link SubAgentSpec.onDelegationStart}. Discriminated on\n * `proceed` so a rejection (`proceed: false` + `rejectionReason`) and an approval\n * (`modifiedInput`) cannot be mixed into one nonsensical object.\n */\nexport type DelegationStartDecision =\n | { proceed: false; rejectionReason?: string }\n | {\n proceed?: true;\n modifiedInput?: string;\n /** SE13 — cap the child's iteration count (forwarded as `SendOptions.maxIterations`). */\n modifiedMaxSteps?: number;\n };\n\n/** Context passed to {@link SubAgentSpec.onDelegationComplete} after the child settles. */\nexport interface DelegationCompleteContext {\n input: string;\n name: string;\n /** The child's text result (present on success). */\n result?: string;\n /** The error the child threw (present on failure); the error is still re-thrown. */\n error?: unknown;\n /** SE15 — the same 1-based iteration this delegation's `onDelegationStart` saw. */\n iteration: number;\n}\n\n/**\n * The return of a delegation hook: a decision, a promise of one, or nothing —\n * `void` lets a side-effect-only callback (`(ctx) => { log(ctx) }`) type-check,\n * which is the common case (mirrors a peer framework's `async ctx => { ... }` hooks).\n */\n// biome-ignore lint/suspicious/noConfusingVoidType: `void` is the idiomatic return for an optional-return callback; the rule false-positives on callback return unions.\ntype DelegationHookResult<T> = T | void | Promise<T | void>;\n\n/** Decision returned from {@link SubAgentSpec.onDelegationComplete}. */\nexport interface DelegationCompleteDecision {\n /** Appended to the child's result string. */\n feedback?: string;\n}\n\nexport interface SubAgentSpec {\n name: string;\n description: string;\n instructions: string;\n model?: string;\n tools?: CustomTool[];\n maxDelegationDepth?: number;\n /**\n * SE11 — called before the supervisor delegates. Return `{ proceed: false }`\n * to reject (the child never runs and `rejectionReason` becomes the tool\n * result), or `{ modifiedInput }` to rewrite the delegated prompt. A throwing\n * hook surfaces (never silently swallowed).\n */\n onDelegationStart?: (\n ctx: DelegationStartContext,\n ) => DelegationHookResult<DelegationStartDecision>;\n /**\n * SE11 — called after the delegation settles. On success `ctx.result` is set\n * and an optional `{ feedback }` is appended to it. On failure `ctx.error` is\n * set and the original error is ALWAYS re-thrown after this hook runs — a throw\n * from this hook on the error path is suppressed so it cannot mask the\n * delegation's real failure (on the success path a throw does propagate).\n */\n onDelegationComplete?: (\n ctx: DelegationCompleteContext,\n ) => DelegationHookResult<DelegationCompleteDecision>;\n /**\n * SE12 — opt-in parent-context forwarding. When set, the supervisor transcript\n * (`ctx.messages`, a read-only text projection) is passed to this filter and the\n * returned subset is forwarded to the child as a role-tagged context preamble\n * prepended to the delegated input. When ABSENT the child runs input-only —\n * memory isolation stays the default. A filter returning `[]` forwards nothing.\n * A throwing filter propagates (fail-fast, never swallowed — same contract as\n * `onDelegationStart`); the delegation surfaces as a tool error.\n */\n messageFilter?: (args: MessageFilterArgs) => readonly ToolContextMessage[];\n /**\n * SE14 — opt-in subagent result-context control. When `true`, the child's\n * completed tool-call results (name + result) are appended to the delegation\n * payload returned to the supervisor, inside a `<subagent-tool-results>` block.\n * When absent/`false` the delegation returns the child's final text only —\n * text-only stays the default (a peer framework's scoped posture). See ADR 0006.\n */\n includeToolResults?: boolean;\n}\n\nexport class MaxDelegationDepthError extends Error {\n readonly code = \"max_delegation_depth\" as const;\n constructor(\n public readonly currentDepth: number,\n public readonly maxDepth: number,\n ) {\n super(`Max delegation depth ${maxDepth} exceeded (current: ${currentDepth})`);\n this.name = \"MaxDelegationDepthError\";\n }\n}\n\n/**\n * Run the `onDelegationStart` hook; returns either a rejection or the (possibly\n * rewritten) input plus the optional SE13 `maxSteps` cap.\n */\nasync function applyDelegationStart(\n spec: SubAgentSpec,\n input: string,\n iteration: number,\n): Promise<{ reject: string } | { input: string; maxSteps?: number }> {\n if (spec.onDelegationStart === undefined) return { input };\n const decision = await spec.onDelegationStart({ input, name: spec.name, iteration });\n if (decision === undefined) return { input };\n if (decision.proceed === false)\n return { reject: decision.rejectionReason ?? \"(delegation rejected)\" };\n return {\n input: decision.modifiedInput ?? input,\n ...(decision.modifiedMaxSteps !== undefined ? { maxSteps: decision.modifiedMaxSteps } : {}),\n };\n}\n\n/**\n * SE14 — replay the child run's stream (a safe post-`wait()` idiom — the run buffers\n * events, `stream()` replays them) and collect every completed tool-call result into\n * a delimited block. Returns `\"\"` when the child ran no completed tool calls. See ADR 0006.\n */\nasync function collectChildToolResults(run: Run): Promise<string> {\n const lines: string[] = [];\n for await (const event of run.stream()) {\n if (event.type === \"tool_call\" && event.status === \"completed\") {\n const rendered =\n typeof event.result === \"string\" ? event.result : JSON.stringify(event.result ?? null);\n lines.push(`${event.name}: ${rendered}`);\n }\n }\n if (lines.length === 0) return \"\";\n return `\\n\\n<subagent-tool-results>\\n${lines.join(\"\\n\")}\\n</subagent-tool-results>`;\n}\n\n/**\n * Create the transient child agent and send the input, composing every forwarded\n * `SendOptions` onto ONE `send` call — SE10 `signal` + SE13 `maxIterations`. Absent\n * every option ⇒ the pre-SE10 single-arg `send(input)` shape. SE14 — when\n * `includeToolResults` is set, append the child's completed tool results. Dispose in `finally`.\n */\n/**\n * Build the child agent's `Agent.create` options: the child inherits the parent's\n * apiKey (else `Agent.create` throws \"Missing API key\"), its model (unless the spec\n * overrides it), and — #55 — the parent's plugins (permission gate/guards) so the\n * child's inner tool calls run under the same policy.\n */\nfunction buildChildCreateOptions(\n spec: SubAgentSpec,\n inherited: InheritedCredentials | undefined,\n): AgentOptions {\n const model: string | ModelSelection | undefined = spec.model\n ? { id: spec.model }\n : inherited?.model;\n return {\n ...(inherited?.apiKey !== undefined ? { apiKey: inherited.apiKey } : {}),\n ...(model !== undefined ? { model } : {}),\n ...(inherited?.plugins !== undefined ? { plugins: inherited.plugins } : {}),\n systemPrompt: spec.instructions,\n tools: spec.tools ?? [],\n };\n}\n\nasync function runChildAgent(\n spec: SubAgentSpec,\n input: string,\n signal: AbortSignal | undefined,\n maxSteps: number | undefined,\n inherited: InheritedCredentials | undefined,\n): Promise<string> {\n // SE45 cycle 3 — use the registered Agent facade via the DIP seam\n // (agent-factory-registry) instead of a dynamic `import(\"../agent.js\")`.\n // This removes the last madge cycle (a2a/subagent -> agent -> ... -> real-local-run-tools\n // -> a2a/subagent): the facade registers itself at module-init via setAgentFacade,\n // so subagent depends only on the registry port, never on the facade module.\n const agent = await getAgentFacade().create(buildChildCreateOptions(spec, inherited));\n try {\n const sendOptions: {\n signal?: AbortSignal;\n maxIterations?: number;\n origin?: import(\"../types/run.js\").MessageOrigin;\n } = {\n ...(signal !== undefined ? { signal } : {}),\n ...(maxSteps !== undefined ? { maxIterations: maxSteps } : {}),\n // SE3 — a delegated child's turn is initiated by the coordinating parent.\n origin: { kind: \"coordinator\" },\n };\n const run = await agent.send(input, sendOptions);\n const result = await run.wait();\n const text = result.result ?? \"(no response)\";\n // SE14 — text-only by default; opt-in appends the child's tool results.\n return spec.includeToolResults === true ? text + (await collectChildToolResults(run)) : text;\n } finally {\n agent.dispose();\n }\n}\n\n/**\n * Best-effort error-path notification: run `onDelegationComplete` with the child's\n * error so the caller can observe the failure. The observer's own throw (sync or\n * async) is suppressed here so it cannot mask the delegation's real error, which the\n * handler re-throws next.\n */\nasync function notifyDelegationError(\n spec: SubAgentSpec,\n input: string,\n error: unknown,\n iteration: number,\n): Promise<void> {\n if (spec.onDelegationComplete === undefined) return;\n try {\n await spec.onDelegationComplete({ input, name: spec.name, error, iteration });\n } catch {\n // Subordinate to `error`; the child's real cause wins.\n }\n}\n\n/**\n * SE12 — apply `messageFilter` (if set) and prepend the filtered supervisor\n * transcript to the delegated input as a role-tagged context preamble. Absent\n * filter OR no messages OR an empty filtered subset ⇒ the original input\n * (isolation-by-default preserved).\n */\nfunction applyMessageFilter(\n spec: SubAgentSpec,\n input: string,\n messages: readonly ToolContextMessage[] | undefined,\n): string {\n if (spec.messageFilter === undefined || messages === undefined) return input;\n const filtered = spec.messageFilter({ messages, input, name: spec.name });\n if (filtered.length === 0) return input;\n const preamble = filtered.map((m) => `${m.role}: ${m.content}`).join(\"\\n\");\n return `Prior conversation:\\n${preamble}\\n\\nTask:\\n${input}`;\n}\n\n/** Run the success-path `onDelegationComplete` hook; appends its `feedback` to the result. */\nasync function applyDelegationComplete(\n spec: SubAgentSpec,\n input: string,\n result: string,\n iteration: number,\n): Promise<string> {\n if (spec.onDelegationComplete === undefined) return result;\n const completion = await spec.onDelegationComplete({ input, name: spec.name, result, iteration });\n return completion?.feedback !== undefined ? result + completion.feedback : result;\n}\n\nfunction defineSubAgent(spec: SubAgentSpec, _parentDepth = 0): CustomTool {\n const currentDepth = _parentDepth + 1;\n const maxDepth = spec.maxDelegationDepth ?? 3;\n\n if (currentDepth > maxDepth) {\n throw new MaxDelegationDepthError(currentDepth, maxDepth);\n }\n\n // Zod for RUNTIME validation of the tool_use input …\n const inputZod = z.object({\n input: z.string().describe(\"Task for the subagent\"),\n });\n // … and a real Draft-7 JSON Schema for the LLM. `CustomTool.inputSchema` is sent\n // to the model verbatim; a raw Zod object would serialize to garbage, so the\n // model emits malformed input that fails `inputZod.parse` and the delegation\n // never runs (the previous bug — the schema and the validator are now distinct).\n const inputSchema: Record<string, unknown> = {\n type: \"object\",\n properties: {\n input: { type: \"string\", description: \"Task for the subagent\" },\n },\n required: [\"input\"],\n additionalProperties: false,\n };\n\n // SE15 — per-instance delegation counter, surfaced as `iteration` on the hook\n // contexts. Incremented once per handler invocation before onDelegationStart.\n let iteration = 0;\n\n // Credentials handed down by the parent runtime (see `inheritSubAgentCredentials`).\n let inherited: InheritedCredentials | undefined;\n\n const tool: CustomTool = {\n name: spec.name,\n description: spec.description,\n inputSchema,\n handler: async (\n rawInput: Record<string, unknown>,\n ctx?: {\n signal?: AbortSignal;\n context?: unknown;\n messages?: readonly ToolContextMessage[];\n },\n ): Promise<string> => {\n const { input: parsed } = inputZod.parse(rawInput);\n iteration += 1; // SE15 — before onDelegationStart; a rejected delegation still counts.\n // Pin THIS invocation's iteration before any await so a concurrent invocation\n // bumping the shared counter cannot change the value onDelegationComplete /\n // notifyDelegationError observe — they see the same iteration onDelegationStart did.\n const capturedIteration = iteration;\n\n const start = await applyDelegationStart(spec, parsed, capturedIteration);\n if (\"reject\" in start) return start.reject;\n // SE12 — opt-in: forward the filtered supervisor transcript as a preamble.\n const input = applyMessageFilter(spec, start.input, ctx?.messages);\n\n let result: string;\n try {\n // SE13 — apply the optional onDelegationStart maxSteps cap on the child send.\n result = await runChildAgent(spec, input, ctx?.signal, start.maxSteps, inherited);\n } catch (error) {\n // SE11 — notify the completion hook of the failure (best-effort observer),\n // then re-throw the ORIGINAL error (Rule 8: never swallow the delegation's\n // own failure).\n await notifyDelegationError(spec, input, error, capturedIteration);\n throw error;\n }\n return applyDelegationComplete(spec, input, result, capturedIteration);\n },\n };\n\n // Install the private credential sink (off the public shape + off every ctx).\n Object.defineProperty(tool, INHERIT_CREDENTIALS, {\n value: ((creds: InheritedCredentials) => {\n inherited = creds;\n }) satisfies CredentialSink,\n enumerable: false,\n });\n\n return tool;\n}\n\n/** SE36 — `SubAgent.create` replaces `defineSubAgent` (ADR 0015). @public */\nexport class SubAgent {\n private constructor() {}\n static create(spec: SubAgentSpec, parentDepth = 0): CustomTool {\n return defineSubAgent(spec, parentDepth);\n }\n}\n\n/**\n * Convert a parent's declarative `agents` map ({@link AgentDefinition} per key)\n * into delegation tools for the LOCAL runtime — the counterpart of the\n * cloud/fixture subagent wiring. Each child inherits the parent's `apiKey`/model\n * via {@link inheritSubAgentCredentials}; `def.model` overrides the model\n * (`\"inherit\"` keeps the parent's), and `def.tools` scopes the child to that\n * subset of the parent's tools (absent → the parent's full toolset, per the\n * `AgentDefinition.tools` contract).\n *\n * Per-subagent `mcpServers` on an `AgentDefinition` is honored by the cloud\n * runtime only; it is not wired into local delegation here.\n *\n * @internal\n */\nexport function subAgentToolsFromDefinitions(\n agents: Record<string, AgentDefinition>,\n parentTools: readonly CustomTool[],\n): CustomTool[] {\n return Object.entries(agents).map(([name, def]) => {\n const whitelist =\n Array.isArray(def.tools) && def.tools.length > 0 ? new Set(def.tools) : undefined;\n const childTools = whitelist ? parentTools.filter((t) => whitelist.has(t.name)) : parentTools;\n return defineSubAgent({\n name,\n description: def.description,\n instructions: def.prompt,\n ...(def.model !== undefined && def.model !== \"inherit\" ? { model: def.model.id } : {}),\n tools: [...childTools],\n });\n });\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/a2a/agent-mailbox.ts","../../src/a2a/message-bus.ts","../../src/internal/runtime/registry/agent-factory-registry.ts","../../src/a2a/subagent.ts"],"names":[],"mappings":";;;AAYO,IAAM,eAAN,MAAmB;AAAA,EACf,OAAA;AAAA,EACQ,IAAA;AAAA,EACT,QAAA,GAAkC,IAAA;AAAA,EAE1C,WAAA,CAAY,SAAiB,GAAA,EAAiB;AAC5C,IAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AACf,IAAA,IAAA,CAAK,IAAA,GAAO,GAAA;AACZ,IAAA,IAAA,CAAK,IAAA,CAAK,SAAS,OAAA,EAAS,CAAC,QAAQ,IAAA,CAAK,SAAA,CAAU,GAAG,CAAC,CAAA;AAAA,EAC1D;AAAA,EAEA,UAAU,OAAA,EAA+B;AACvC,IAAA,IAAA,CAAK,QAAA,GAAW,OAAA;AAAA,EAClB;AAAA,EAEA,MAAM,IAAA,CAAK,EAAA,EAAY,OAAA,EAA4D;AACjF,IAAA,OAAO,KAAK,IAAA,CAAK,IAAA,CAAK,IAAA,CAAK,OAAA,EAAS,IAAI,OAAO,CAAA;AAAA,EACjD;AAAA,EAEA,MAAM,OAAA,CACJ,EAAA,EACA,OAAA,EACA,IAAA,EACkB;AAClB,IAAA,OAAO,KAAK,IAAA,CAAK,OAAA,CAAQ,KAAK,OAAA,EAAS,EAAA,EAAI,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AAAA,IACvE;AAAA,GACF;AAGA,EAAA,MAAA,CAAO,cAAA,CAAe,MAAM,mBAAA,EAAqB;AAAA,IAC/C,KAAA,GAAQ,CAAC,KAAA,KAAgC;AACvC,MAAA,SAAA,GAAY,KAAA;AAAA,IACd,CAAA,CAAA;AAAA,IACA,UAAA,EAAY;AAAA,GACb,CAAA;AAED,EAAA,OAAO,IAAA;AACT;AAGO,IAAM,WAAN,MAAe;AAAA,EACZ,WAAA,GAAc;AAAA,EAAC;AAAA,EACvB,OAAO,MAAA,CAAO,IAAA,EAAoB,WAAA,GAAc,CAAA,EAAe;AAC7D,IAAA,OAAO,cAAA,CAAe,MAAM,WAAW,CAAA;AAAA,EACzC;AACF","file":"index.js","sourcesContent":["/**\n * AgentMailbox — per-agent inbox for A2A communication (T20.1, ADR D453).\n *\n * Wraps MessageBus registration with a convenient API for sending,\n * receiving, and requesting messages.\n *\n * @public\n */\n\nimport type { MessageBus, RequestOptions } from \"./message-bus.js\";\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\nexport class AgentMailbox {\n readonly agentId: string;\n private readonly _bus: MessageBus;\n private _handler: MessageHandler | null = null;\n\n constructor(agentId: string, bus: MessageBus) {\n this.agentId = agentId;\n this._bus = bus;\n this._bus.register(agentId, (msg) => this._dispatch(msg));\n }\n\n onMessage(handler: MessageHandler): void {\n this._handler = handler;\n }\n\n async send(to: string, partial: { type: string; payload: unknown }): Promise<void> {\n return this._bus.send(this.agentId, to, partial);\n }\n\n async request(\n to: string,\n partial: { type: string; payload: unknown },\n opts?: RequestOptions,\n ): Promise<unknown> {\n return this._bus.request(this.agentId, to, partial, opts);\n }\n\n dispose(): void {\n this._bus.unregister(this.agentId);\n this._handler = null;\n }\n\n private _dispatch(msg: A2AMessage): unknown {\n if (this._handler) {\n return this._handler(msg);\n }\n }\n}\n","/**\n * MessageBus — in-process typed message router for A2A (T20.1, ADR D453).\n *\n * Routes messages between agents by ID. Supports fire-and-forget (send)\n * and request/response (request with timeout).\n *\n * @public\n */\n\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\nexport interface RequestOptions {\n timeoutMs?: number;\n}\n\nexport class MessageBus {\n private readonly _handlers = new Map<string, MessageHandler>();\n\n register(agentId: string, handler: MessageHandler): void {\n this._handlers.set(agentId, handler);\n }\n\n unregister(agentId: string): void {\n this._handlers.delete(agentId);\n }\n\n async send(from: string, to: string, partial: { type: string; payload: unknown }): Promise<void> {\n const handler = this._handlers.get(to);\n if (!handler) {\n throw new Error(`Agent \"${to}\" not registered on MessageBus`);\n }\n const message: A2AMessage = {\n ...partial,\n from,\n to,\n timestamp: Date.now(),\n // SE3 — provenance projection of the sender address (thin view over `from`).\n origin: { kind: \"peer\", from },\n };\n // Fire-and-forget: invoke handler but don't await result\n handler(message);\n }\n\n async request(\n from: string,\n to: string,\n partial: { type: string; payload: unknown },\n opts?: RequestOptions,\n ): Promise<unknown> {\n const handler = this._handlers.get(to);\n if (!handler) {\n throw new Error(`Agent \"${to}\" not registered on MessageBus`);\n }\n const timeoutMs = opts?.timeoutMs ?? 30_000;\n const message: A2AMessage = {\n ...partial,\n from,\n to,\n timestamp: Date.now(),\n // SE3 — provenance projection of the sender address (thin view over `from`).\n origin: { kind: \"peer\", from },\n };\n // Hold the timer so it can be cleared once the race settles — otherwise a\n // successful request leaks a live `setTimeout` that keeps the Node event loop\n // alive (the process hangs after the reply). Cleared in `finally`.\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n Promise.resolve(handler(message)),\n new Promise((_, reject) => {\n timer = setTimeout(\n () =>\n reject(new Error(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer !== undefined) clearTimeout(timer);\n }\n }\n\n has(agentId: string): boolean {\n return this._handlers.has(agentId);\n }\n\n listAgents(): string[] {\n return [...this._handlers.keys()];\n }\n}\n","/**\n * Internal DI seam for the public `Agent` facade.\n *\n * Several internal subsystems (`LocalAgent.runUntil`/`fork`, eval, scorers,\n * cron) need to invoke `Agent.create`/`prompt`/`get`/`resume`/`batch`. A direct\n * `import { Agent }` would invert the public-api -> internal dependency\n * direction (and `agent.ts` already imports much of `internal/`). This module\n * is the inversion seam: `agent.ts` registers the facade at module-init time;\n * internal consumers read it on-demand via `getAgentFacade()`. The\n * `internal-must-not-import-facade` dependency-cruiser rule enforces that no\n * `internal/*` module bypasses this seam with a direct facade import.\n *\n * Throws if accessed before registration — should never happen because\n * `agent.ts` evaluation always precedes any internal call (the consumer must\n * obtain a handle from the `Agent` facade first).\n *\n * @internal\n */\n\nimport type { AgentOptions, SDKAgent, SDKAgentInfo } from \"../../../types/agent.js\";\nimport type { BatchItem, BatchOptions, BatchResult } from \"../../../types/batch.js\";\nimport type { RunResult } from \"../../../types/run.js\";\n\n/**\n * The subset of the public `Agent` static surface that internal subsystems\n * consume through the inversion seam. Each member mirrors the matching\n * `Agent.*` signature so registration is a thin pass-through.\n *\n * @internal\n */\nexport interface AgentFacadePort {\n create: (options: AgentOptions) => Promise<SDKAgent>;\n delete: (agentId: string) => Promise<void>;\n prompt: (message: string, options: AgentOptions) => Promise<RunResult>;\n get: (agentId: string) => Promise<SDKAgentInfo>;\n resume: (agentId: string, options?: Partial<AgentOptions>) => Promise<SDKAgent>;\n batch: (\n prompts: ReadonlyArray<string | BatchItem>,\n options: BatchOptions,\n ) => Promise<BatchResult[]>;\n}\n\nlet registered: AgentFacadePort | undefined;\n\n/**\n * Registered by `agent.ts` at module-init time. Idempotent: re-registration\n * replaces the previous reference (useful for tests that swap implementations).\n *\n * @internal\n */\nexport function setAgentFacade(facade: AgentFacadePort): void {\n registered = facade;\n}\n\n/**\n * Resolve the registered `Agent` facade for internal subsystems.\n *\n * @internal\n */\nexport function getAgentFacade(): AgentFacadePort {\n if (registered === undefined) {\n throw new Error(\n \"internal: Agent facade not registered. The `agent.ts` module must be loaded before internal subsystems (LocalAgent.runUntil/fork, eval, scorers, cron) invoke it.\",\n );\n }\n return registered;\n}\n","/**\n * Subagent delegation — declarative child agent invocable as a tool.\n *\n * Per ADR D2: `defineSubAgent(spec)` returns a `CustomTool` that, when\n * invoked by the LLM, creates a child agent and sends the input as a\n * message. EC-2: delegation depth tracked to prevent infinite recursion.\n *\n * SE10 — the handler forwards the parent run's `AbortSignal` to the child.\n * SE11 — optional `onDelegationStart` / `onDelegationComplete` lifecycle hooks\n * let the caller reject, rewrite, observe, or annotate a delegation.\n *\n * @public\n */\n\nimport { z } from \"zod\";\nimport type { Plugin } from \"../internal/plugins/types.js\";\nimport { getAgentFacade } from \"../internal/runtime/registry/agent-factory-registry.js\";\nimport type {\n AgentDefinition,\n AgentOptions,\n CustomTool,\n ToolContextMessage,\n} from \"../types/agent.js\";\nimport type { ModelSelection } from \"../types/agent-prims.js\";\nimport type { Run } from \"../types/run.js\";\n\n/**\n * Credentials a parent agent hands down to its subagent tools so the child\n * inherits the parent's auth (and, absent an explicit `spec.model`, its model).\n * @internal\n */\nexport interface InheritedCredentials {\n readonly apiKey?: string;\n readonly model?: ModelSelection;\n /**\n * #55 — the parent's code-registered plugins (e.g. a `PermissionPlugin`) handed\n * down so the child runs under the SAME policy. Without this, a delegated child's\n * inner tool calls escape the parent's argument-level permission gate. First-party\n * delegation path only — never exposed to third-party tool `ctx`.\n */\n readonly plugins?: readonly Plugin[];\n}\n\n/**\n * Private per-tool setter installed on subagent tools. Kept off the public\n * `CustomTool` shape (and off every tool handler's `ctx`) so the parent's API\n * key never reaches third-party tool code — only the SDK's own delegation path.\n */\nconst INHERIT_CREDENTIALS = Symbol(\"theokit.subagent.inheritCredentials\");\n\ntype CredentialSink = (creds: InheritedCredentials) => void;\n\n/**\n * Called by the local runtime for every tool in a run: if the tool is a\n * subagent, its child agent inherits the parent's `apiKey`/`model`. A no-op for\n * any other tool (third-party tools never receive the parent's credentials).\n * @internal\n */\nexport function inheritSubAgentCredentials(tool: CustomTool, creds: InheritedCredentials): void {\n const sink = (tool as { [INHERIT_CREDENTIALS]?: CredentialSink })[INHERIT_CREDENTIALS];\n if (typeof sink === \"function\") sink(creds);\n}\n\n/** Arguments passed to {@link SubAgentSpec.messageFilter} (SE12). */\nexport interface MessageFilterArgs {\n /** The supervisor transcript (read-only text projection) available to this delegation. */\n messages: readonly ToolContextMessage[];\n /** The prompt about to be delegated (after any `onDelegationStart` rewrite). */\n input: string;\n /** The subagent's name. */\n name: string;\n}\n\n/** Context passed to {@link SubAgentSpec.onDelegationStart} before the child runs. */\nexport interface DelegationStartContext {\n input: string;\n name: string;\n /**\n * SE15 — 1-based count of times THIS subagent tool has been invoked (a\n * per-`defineSubAgent`-instance counter). Incremented before this hook runs;\n * a rejected delegation still counts. Enables reject-after-N patterns.\n */\n iteration: number;\n}\n\n/**\n * Decision returned from {@link SubAgentSpec.onDelegationStart}. Discriminated on\n * `proceed` so a rejection (`proceed: false` + `rejectionReason`) and an approval\n * (`modifiedInput`) cannot be mixed into one nonsensical object.\n */\nexport type DelegationStartDecision =\n | { proceed: false; rejectionReason?: string }\n | {\n proceed?: true;\n modifiedInput?: string;\n /** SE13 — cap the child's iteration count (forwarded as `SendOptions.maxIterations`). */\n modifiedMaxSteps?: number;\n };\n\n/** Context passed to {@link SubAgentSpec.onDelegationComplete} after the child settles. */\nexport interface DelegationCompleteContext {\n input: string;\n name: string;\n /** The child's text result (present on success). */\n result?: string;\n /** The error the child threw (present on failure); the error is still re-thrown. */\n error?: unknown;\n /** SE15 — the same 1-based iteration this delegation's `onDelegationStart` saw. */\n iteration: number;\n}\n\n/**\n * The return of a delegation hook: a decision, a promise of one, or nothing —\n * `void` lets a side-effect-only callback (`(ctx) => { log(ctx) }`) type-check,\n * which is the common case (mirrors Mastra's `async ctx => { ... }` hooks).\n */\n// biome-ignore lint/suspicious/noConfusingVoidType: `void` is the idiomatic return for an optional-return callback; the rule false-positives on callback return unions.\ntype DelegationHookResult<T> = T | void | Promise<T | void>;\n\n/** Decision returned from {@link SubAgentSpec.onDelegationComplete}. */\nexport interface DelegationCompleteDecision {\n /** Appended to the child's result string. */\n feedback?: string;\n}\n\nexport interface SubAgentSpec {\n name: string;\n description: string;\n instructions: string;\n model?: string;\n tools?: CustomTool[];\n maxDelegationDepth?: number;\n /**\n * SE11 — called before the supervisor delegates. Return `{ proceed: false }`\n * to reject (the child never runs and `rejectionReason` becomes the tool\n * result), or `{ modifiedInput }` to rewrite the delegated prompt. A throwing\n * hook surfaces (never silently swallowed).\n */\n onDelegationStart?: (\n ctx: DelegationStartContext,\n ) => DelegationHookResult<DelegationStartDecision>;\n /**\n * SE11 — called after the delegation settles. On success `ctx.result` is set\n * and an optional `{ feedback }` is appended to it. On failure `ctx.error` is\n * set and the original error is ALWAYS re-thrown after this hook runs — a throw\n * from this hook on the error path is suppressed so it cannot mask the\n * delegation's real failure (on the success path a throw does propagate).\n */\n onDelegationComplete?: (\n ctx: DelegationCompleteContext,\n ) => DelegationHookResult<DelegationCompleteDecision>;\n /**\n * SE12 — opt-in parent-context forwarding. When set, the supervisor transcript\n * (`ctx.messages`, a read-only text projection) is passed to this filter and the\n * returned subset is forwarded to the child as a role-tagged context preamble\n * prepended to the delegated input. When ABSENT the child runs input-only —\n * memory isolation stays the default. A filter returning `[]` forwards nothing.\n * A throwing filter propagates (fail-fast, never swallowed — same contract as\n * `onDelegationStart`); the delegation surfaces as a tool error.\n */\n messageFilter?: (args: MessageFilterArgs) => readonly ToolContextMessage[];\n /**\n * SE14 — opt-in subagent result-context control. When `true`, the child's\n * completed tool-call results (name + result) are appended to the delegation\n * payload returned to the supervisor, inside a `<subagent-tool-results>` block.\n * When absent/`false` the delegation returns the child's final text only —\n * text-only stays the default (Mastra's scoped posture). See ADR 0006.\n */\n includeToolResults?: boolean;\n}\n\nexport class MaxDelegationDepthError extends Error {\n readonly code = \"max_delegation_depth\" as const;\n constructor(\n public readonly currentDepth: number,\n public readonly maxDepth: number,\n ) {\n super(`Max delegation depth ${maxDepth} exceeded (current: ${currentDepth})`);\n this.name = \"MaxDelegationDepthError\";\n }\n}\n\n/**\n * Run the `onDelegationStart` hook; returns either a rejection or the (possibly\n * rewritten) input plus the optional SE13 `maxSteps` cap.\n */\nasync function applyDelegationStart(\n spec: SubAgentSpec,\n input: string,\n iteration: number,\n): Promise<{ reject: string } | { input: string; maxSteps?: number }> {\n if (spec.onDelegationStart === undefined) return { input };\n const decision = await spec.onDelegationStart({ input, name: spec.name, iteration });\n if (decision === undefined) return { input };\n if (decision.proceed === false)\n return { reject: decision.rejectionReason ?? \"(delegation rejected)\" };\n return {\n input: decision.modifiedInput ?? input,\n ...(decision.modifiedMaxSteps !== undefined ? { maxSteps: decision.modifiedMaxSteps } : {}),\n };\n}\n\n/**\n * SE14 — replay the child run's stream (a safe post-`wait()` idiom — the run buffers\n * events, `stream()` replays them) and collect every completed tool-call result into\n * a delimited block. Returns `\"\"` when the child ran no completed tool calls. See ADR 0006.\n */\nasync function collectChildToolResults(run: Run): Promise<string> {\n const lines: string[] = [];\n for await (const event of run.stream()) {\n if (event.type === \"tool_call\" && event.status === \"completed\") {\n const rendered =\n typeof event.result === \"string\" ? event.result : JSON.stringify(event.result ?? null);\n lines.push(`${event.name}: ${rendered}`);\n }\n }\n if (lines.length === 0) return \"\";\n return `\\n\\n<subagent-tool-results>\\n${lines.join(\"\\n\")}\\n</subagent-tool-results>`;\n}\n\n/**\n * Create the transient child agent and send the input, composing every forwarded\n * `SendOptions` onto ONE `send` call — SE10 `signal` + SE13 `maxIterations`. Absent\n * every option ⇒ the pre-SE10 single-arg `send(input)` shape. SE14 — when\n * `includeToolResults` is set, append the child's completed tool results. Dispose in `finally`.\n */\n/**\n * Build the child agent's `Agent.create` options: the child inherits the parent's\n * apiKey (else `Agent.create` throws \"Missing API key\"), its model (unless the spec\n * overrides it), and — #55 — the parent's plugins (permission gate/guards) so the\n * child's inner tool calls run under the same policy.\n */\nfunction buildChildCreateOptions(\n spec: SubAgentSpec,\n inherited: InheritedCredentials | undefined,\n): AgentOptions {\n const model: string | ModelSelection | undefined = spec.model\n ? { id: spec.model }\n : inherited?.model;\n return {\n ...(inherited?.apiKey !== undefined ? { apiKey: inherited.apiKey } : {}),\n ...(model !== undefined ? { model } : {}),\n ...(inherited?.plugins !== undefined ? { plugins: inherited.plugins } : {}),\n systemPrompt: spec.instructions,\n tools: spec.tools ?? [],\n };\n}\n\nasync function runChildAgent(\n spec: SubAgentSpec,\n input: string,\n signal: AbortSignal | undefined,\n maxSteps: number | undefined,\n inherited: InheritedCredentials | undefined,\n): Promise<string> {\n // SE45 cycle 3 — use the registered Agent facade via the DIP seam\n // (agent-factory-registry) instead of a dynamic `import(\"../agent.js\")`.\n // This removes the last madge cycle (a2a/subagent -> agent -> ... -> real-local-run-tools\n // -> a2a/subagent): the facade registers itself at module-init via setAgentFacade,\n // so subagent depends only on the registry port, never on the facade module.\n const agent = await getAgentFacade().create(buildChildCreateOptions(spec, inherited));\n try {\n const sendOptions: {\n signal?: AbortSignal;\n maxIterations?: number;\n origin?: import(\"../types/run.js\").MessageOrigin;\n } = {\n ...(signal !== undefined ? { signal } : {}),\n ...(maxSteps !== undefined ? { maxIterations: maxSteps } : {}),\n // SE3 — a delegated child's turn is initiated by the coordinating parent.\n origin: { kind: \"coordinator\" },\n };\n const run = await agent.send(input, sendOptions);\n const result = await run.wait();\n const text = result.result ?? \"(no response)\";\n // SE14 — text-only by default; opt-in appends the child's tool results.\n return spec.includeToolResults === true ? text + (await collectChildToolResults(run)) : text;\n } finally {\n agent.dispose();\n }\n}\n\n/**\n * Best-effort error-path notification: run `onDelegationComplete` with the child's\n * error so the caller can observe the failure. The observer's own throw (sync or\n * async) is suppressed here so it cannot mask the delegation's real error, which the\n * handler re-throws next.\n */\nasync function notifyDelegationError(\n spec: SubAgentSpec,\n input: string,\n error: unknown,\n iteration: number,\n): Promise<void> {\n if (spec.onDelegationComplete === undefined) return;\n try {\n await spec.onDelegationComplete({ input, name: spec.name, error, iteration });\n } catch {\n // Subordinate to `error`; the child's real cause wins.\n }\n}\n\n/**\n * SE12 — apply `messageFilter` (if set) and prepend the filtered supervisor\n * transcript to the delegated input as a role-tagged context preamble. Absent\n * filter OR no messages OR an empty filtered subset ⇒ the original input\n * (isolation-by-default preserved).\n */\nfunction applyMessageFilter(\n spec: SubAgentSpec,\n input: string,\n messages: readonly ToolContextMessage[] | undefined,\n): string {\n if (spec.messageFilter === undefined || messages === undefined) return input;\n const filtered = spec.messageFilter({ messages, input, name: spec.name });\n if (filtered.length === 0) return input;\n const preamble = filtered.map((m) => `${m.role}: ${m.content}`).join(\"\\n\");\n return `Prior conversation:\\n${preamble}\\n\\nTask:\\n${input}`;\n}\n\n/** Run the success-path `onDelegationComplete` hook; appends its `feedback` to the result. */\nasync function applyDelegationComplete(\n spec: SubAgentSpec,\n input: string,\n result: string,\n iteration: number,\n): Promise<string> {\n if (spec.onDelegationComplete === undefined) return result;\n const completion = await spec.onDelegationComplete({ input, name: spec.name, result, iteration });\n return completion?.feedback !== undefined ? result + completion.feedback : result;\n}\n\nfunction defineSubAgent(spec: SubAgentSpec, _parentDepth = 0): CustomTool {\n const currentDepth = _parentDepth + 1;\n const maxDepth = spec.maxDelegationDepth ?? 3;\n\n if (currentDepth > maxDepth) {\n throw new MaxDelegationDepthError(currentDepth, maxDepth);\n }\n\n // Zod for RUNTIME validation of the tool_use input …\n const inputZod = z.object({\n input: z.string().describe(\"Task for the subagent\"),\n });\n // … and a real Draft-7 JSON Schema for the LLM. `CustomTool.inputSchema` is sent\n // to the model verbatim; a raw Zod object would serialize to garbage, so the\n // model emits malformed input that fails `inputZod.parse` and the delegation\n // never runs (the previous bug — the schema and the validator are now distinct).\n const inputSchema: Record<string, unknown> = {\n type: \"object\",\n properties: {\n input: { type: \"string\", description: \"Task for the subagent\" },\n },\n required: [\"input\"],\n additionalProperties: false,\n };\n\n // SE15 — per-instance delegation counter, surfaced as `iteration` on the hook\n // contexts. Incremented once per handler invocation before onDelegationStart.\n let iteration = 0;\n\n // Credentials handed down by the parent runtime (see `inheritSubAgentCredentials`).\n let inherited: InheritedCredentials | undefined;\n\n const tool: CustomTool = {\n name: spec.name,\n description: spec.description,\n inputSchema,\n handler: async (\n rawInput: Record<string, unknown>,\n ctx?: {\n signal?: AbortSignal;\n context?: unknown;\n messages?: readonly ToolContextMessage[];\n },\n ): Promise<string> => {\n const { input: parsed } = inputZod.parse(rawInput);\n iteration += 1; // SE15 — before onDelegationStart; a rejected delegation still counts.\n // Pin THIS invocation's iteration before any await so a concurrent invocation\n // bumping the shared counter cannot change the value onDelegationComplete /\n // notifyDelegationError observe — they see the same iteration onDelegationStart did.\n const capturedIteration = iteration;\n\n const start = await applyDelegationStart(spec, parsed, capturedIteration);\n if (\"reject\" in start) return start.reject;\n // SE12 — opt-in: forward the filtered supervisor transcript as a preamble.\n const input = applyMessageFilter(spec, start.input, ctx?.messages);\n\n let result: string;\n try {\n // SE13 — apply the optional onDelegationStart maxSteps cap on the child send.\n result = await runChildAgent(spec, input, ctx?.signal, start.maxSteps, inherited);\n } catch (error) {\n // SE11 — notify the completion hook of the failure (best-effort observer),\n // then re-throw the ORIGINAL error (Rule 8: never swallow the delegation's\n // own failure).\n await notifyDelegationError(spec, input, error, capturedIteration);\n throw error;\n }\n return applyDelegationComplete(spec, input, result, capturedIteration);\n },\n };\n\n // Install the private credential sink (off the public shape + off every ctx).\n Object.defineProperty(tool, INHERIT_CREDENTIALS, {\n value: ((creds: InheritedCredentials) => {\n inherited = creds;\n }) satisfies CredentialSink,\n enumerable: false,\n });\n\n return tool;\n}\n\n/** SE36 — `SubAgent.create` replaces `defineSubAgent` (ADR 0015). @public */\nexport class SubAgent {\n private constructor() {}\n static create(spec: SubAgentSpec, parentDepth = 0): CustomTool {\n return defineSubAgent(spec, parentDepth);\n }\n}\n\n/**\n * Convert a parent's declarative `agents` map ({@link AgentDefinition} per key)\n * into delegation tools for the LOCAL runtime — the counterpart of the\n * cloud/fixture subagent wiring. Each child inherits the parent's `apiKey`/model\n * via {@link inheritSubAgentCredentials}; `def.model` overrides the model\n * (`\"inherit\"` keeps the parent's), and `def.tools` scopes the child to that\n * subset of the parent's tools (absent → the parent's full toolset, per the\n * `AgentDefinition.tools` contract).\n *\n * Per-subagent `mcpServers` on an `AgentDefinition` is honored by the cloud\n * runtime only; it is not wired into local delegation here.\n *\n * @internal\n */\nexport function subAgentToolsFromDefinitions(\n agents: Record<string, AgentDefinition>,\n parentTools: readonly CustomTool[],\n): CustomTool[] {\n return Object.entries(agents).map(([name, def]) => {\n const whitelist =\n Array.isArray(def.tools) && def.tools.length > 0 ? new Set(def.tools) : undefined;\n const childTools = whitelist ? parentTools.filter((t) => whitelist.has(t.name)) : parentTools;\n return defineSubAgent({\n name,\n description: def.description,\n instructions: def.prompt,\n ...(def.model !== undefined && def.model !== \"inherit\" ? { model: def.model.id } : {}),\n tools: [...childTools],\n });\n });\n}\n"]}
1
+ {"version":3,"sources":["../../src/a2a/agent-mailbox.ts","../../src/a2a/message-bus.ts","../../src/internal/runtime/registry/agent-factory-registry.ts","../../src/a2a/subagent.ts"],"names":[],"mappings":";;;AAYO,IAAM,eAAN,MAAmB;AAAA,EACf,OAAA;AAAA,EACQ,IAAA;AAAA,EACT,QAAA,GAAkC,IAAA;AAAA,EAE1C,WAAA,CAAY,SAAiB,GAAA,EAAiB;AAC5C,IAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AACf,IAAA,IAAA,CAAK,IAAA,GAAO,GAAA;AACZ,IAAA,IAAA,CAAK,IAAA,CAAK,SAAS,OAAA,EAAS,CAAC,QAAQ,IAAA,CAAK,SAAA,CAAU,GAAG,CAAC,CAAA;AAAA,EAC1D;AAAA,EAEA,UAAU,OAAA,EAA+B;AACvC,IAAA,IAAA,CAAK,QAAA,GAAW,OAAA;AAAA,EAClB;AAAA,EAEA,MAAM,IAAA,CAAK,EAAA,EAAY,OAAA,EAA4D;AACjF,IAAA,OAAO,KAAK,IAAA,CAAK,IAAA,CAAK,IAAA,CAAK,OAAA,EAAS,IAAI,OAAO,CAAA;AAAA,EACjD;AAAA,EAEA,MAAM,OAAA,CACJ,EA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AAA,MAAM,KAAA;AAAA,MACR;AACA,MAAA,OAAO,uBAAA,CAAwB,IAAA,EAAM,KAAA,EAAO,MAAA,EAAQ,iBAAiB,CAAA;AAAA,IACvE;AAAA,GACF;AAGA,EAAA,MAAA,CAAO,cAAA,CAAe,MAAM,mBAAA,EAAqB;AAAA,IAC/C,KAAA,GAAQ,CAAC,KAAA,KAAgC;AACvC,MAAA,SAAA,GAAY,KAAA;AAAA,IACd,CAAA,CAAA;AAAA,IACA,UAAA,EAAY;AAAA,GACb,CAAA;AAED,EAAA,OAAO,IAAA;AACT;AAGO,IAAM,WAAN,MAAe;AAAA,EACZ,WAAA,GAAc;AAAA,EAAC;AAAA,EACvB,OAAO,MAAA,CAAO,IAAA,EAAoB,WAAA,GAAc,CAAA,EAAe;AAC7D,IAAA,OAAO,cAAA,CAAe,MAAM,WAAW,CAAA;AAAA,EACzC;AACF","file":"index.js","sourcesContent":["/**\n * AgentMailbox — per-agent inbox for A2A communication (T20.1, ADR D453).\n *\n * Wraps MessageBus registration with a convenient API for sending,\n * receiving, and requesting messages.\n *\n * @public\n */\n\nimport type { MessageBus, RequestOptions } from \"./message-bus.js\";\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\nexport class AgentMailbox {\n readonly agentId: string;\n private readonly _bus: MessageBus;\n private _handler: MessageHandler | null = null;\n\n constructor(agentId: string, bus: MessageBus) {\n this.agentId = agentId;\n this._bus = bus;\n this._bus.register(agentId, (msg) => this._dispatch(msg));\n }\n\n onMessage(handler: MessageHandler): void {\n this._handler = handler;\n }\n\n async send(to: string, partial: { type: string; payload: unknown }): Promise<void> {\n return this._bus.send(this.agentId, to, partial);\n }\n\n async request(\n to: string,\n partial: { type: string; payload: unknown },\n opts?: RequestOptions,\n ): Promise<unknown> {\n return this._bus.request(this.agentId, to, partial, opts);\n }\n\n dispose(): void {\n this._bus.unregister(this.agentId);\n this._handler = null;\n }\n\n private _dispatch(msg: A2AMessage): unknown {\n if (this._handler) {\n return this._handler(msg);\n }\n }\n}\n","/**\n * MessageBus — in-process typed message router for A2A (T20.1, ADR D453).\n *\n * Routes messages between agents by ID. Supports fire-and-forget (send)\n * and request/response (request with timeout).\n *\n * @public\n */\n\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\nexport interface RequestOptions {\n timeoutMs?: number;\n}\n\nexport class MessageBus {\n private readonly _handlers = new Map<string, MessageHandler>();\n\n register(agentId: string, handler: MessageHandler): void {\n this._handlers.set(agentId, handler);\n }\n\n unregister(agentId: string): void {\n this._handlers.delete(agentId);\n }\n\n async send(from: string, to: string, partial: { type: string; payload: unknown }): Promise<void> {\n const handler = this._handlers.get(to);\n if (!handler) {\n throw new Error(`Agent \"${to}\" not registered on MessageBus`);\n }\n const message: A2AMessage = {\n ...partial,\n from,\n to,\n timestamp: Date.now(),\n // SE3 — provenance projection of the sender address (thin view over `from`).\n origin: { kind: \"peer\", from },\n };\n // Fire-and-forget: invoke handler but don't await result\n handler(message);\n }\n\n async request(\n from: string,\n to: string,\n partial: { type: string; payload: unknown },\n opts?: RequestOptions,\n ): Promise<unknown> {\n const handler = this._handlers.get(to);\n if (!handler) {\n throw new Error(`Agent \"${to}\" not registered on MessageBus`);\n }\n const timeoutMs = opts?.timeoutMs ?? 30_000;\n const message: A2AMessage = {\n ...partial,\n from,\n to,\n timestamp: Date.now(),\n // SE3 — provenance projection of the sender address (thin view over `from`).\n origin: { kind: \"peer\", from },\n };\n // Hold the timer so it can be cleared once the race settles — otherwise a\n // successful request leaks a live `setTimeout` that keeps the Node event loop\n // alive (the process hangs after the reply). Cleared in `finally`.\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n Promise.resolve(handler(message)),\n new Promise((_, reject) => {\n timer = setTimeout(\n () =>\n reject(new Error(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer !== undefined) clearTimeout(timer);\n }\n }\n\n has(agentId: string): boolean {\n return this._handlers.has(agentId);\n }\n\n listAgents(): string[] {\n return [...this._handlers.keys()];\n }\n}\n","/**\n * Internal DI seam for the public `Agent` facade.\n *\n * Several internal subsystems (`LocalAgent.runUntil`/`fork`, eval, scorers,\n * cron) need to invoke `Agent.create`/`prompt`/`get`/`resume`/`batch`. A direct\n * `import { Agent }` would invert the public-api -> internal dependency\n * direction (and `agent.ts` already imports much of `internal/`). This module\n * is the inversion seam: `agent.ts` registers the facade at module-init time;\n * internal consumers read it on-demand via `getAgentFacade()`. The\n * `internal-must-not-import-facade` dependency-cruiser rule enforces that no\n * `internal/*` module bypasses this seam with a direct facade import.\n *\n * Throws if accessed before registration — should never happen because\n * `agent.ts` evaluation always precedes any internal call (the consumer must\n * obtain a handle from the `Agent` facade first).\n *\n * @internal\n */\n\nimport type { AgentOptions, SDKAgent, SDKAgentInfo } from \"../../../types/agent.js\";\nimport type { BatchItem, BatchOptions, BatchResult } from \"../../../types/batch.js\";\nimport type { RunResult } from \"../../../types/run.js\";\n\n/**\n * The subset of the public `Agent` static surface that internal subsystems\n * consume through the inversion seam. Each member mirrors the matching\n * `Agent.*` signature so registration is a thin pass-through.\n *\n * @internal\n */\nexport interface AgentFacadePort {\n create: (options: AgentOptions) => Promise<SDKAgent>;\n delete: (agentId: string) => Promise<void>;\n prompt: (message: string, options: AgentOptions) => Promise<RunResult>;\n get: (agentId: string) => Promise<SDKAgentInfo>;\n resume: (agentId: string, options?: Partial<AgentOptions>) => Promise<SDKAgent>;\n batch: (\n prompts: ReadonlyArray<string | BatchItem>,\n options: BatchOptions,\n ) => Promise<BatchResult[]>;\n}\n\nlet registered: AgentFacadePort | undefined;\n\n/**\n * Registered by `agent.ts` at module-init time. Idempotent: re-registration\n * replaces the previous reference (useful for tests that swap implementations).\n *\n * @internal\n */\nexport function setAgentFacade(facade: AgentFacadePort): void {\n registered = facade;\n}\n\n/**\n * Resolve the registered `Agent` facade for internal subsystems.\n *\n * @internal\n */\nexport function getAgentFacade(): AgentFacadePort {\n if (registered === undefined) {\n throw new Error(\n \"internal: Agent facade not registered. The `agent.ts` module must be loaded before internal subsystems (LocalAgent.runUntil/fork, eval, scorers, cron) invoke it.\",\n );\n }\n return registered;\n}\n","/**\n * Subagent delegation — declarative child agent invocable as a tool.\n *\n * Per ADR D2: `defineSubAgent(spec)` returns a `CustomTool` that, when\n * invoked by the LLM, creates a child agent and sends the input as a\n * message. EC-2: delegation depth tracked to prevent infinite recursion.\n *\n * SE10 — the handler forwards the parent run's `AbortSignal` to the child.\n * SE11 — optional `onDelegationStart` / `onDelegationComplete` lifecycle hooks\n * let the caller reject, rewrite, observe, or annotate a delegation.\n *\n * @public\n */\n\nimport { z } from \"zod\";\nimport type { Plugin } from \"../internal/plugins/types.js\";\nimport { getAgentFacade } from \"../internal/runtime/registry/agent-factory-registry.js\";\nimport type {\n AgentDefinition,\n AgentOptions,\n CustomTool,\n ToolContextMessage,\n} from \"../types/agent.js\";\nimport type { ModelSelection } from \"../types/agent-prims.js\";\nimport type { Run } from \"../types/run.js\";\n\n/**\n * Credentials a parent agent hands down to its subagent tools so the child\n * inherits the parent's auth (and, absent an explicit `spec.model`, its model).\n * @internal\n */\nexport interface InheritedCredentials {\n readonly apiKey?: string;\n readonly model?: ModelSelection;\n /**\n * #55 — the parent's code-registered plugins (e.g. a `PermissionPlugin`) handed\n * down so the child runs under the SAME policy. Without this, a delegated child's\n * inner tool calls escape the parent's argument-level permission gate. First-party\n * delegation path only — never exposed to third-party tool `ctx`.\n */\n readonly plugins?: readonly Plugin[];\n}\n\n/**\n * Private per-tool setter installed on subagent tools. Kept off the public\n * `CustomTool` shape (and off every tool handler's `ctx`) so the parent's API\n * key never reaches third-party tool code — only the SDK's own delegation path.\n */\nconst INHERIT_CREDENTIALS = Symbol(\"theokit.subagent.inheritCredentials\");\n\ntype CredentialSink = (creds: InheritedCredentials) => void;\n\n/**\n * Called by the local runtime for every tool in a run: if the tool is a\n * subagent, its child agent inherits the parent's `apiKey`/`model`. A no-op for\n * any other tool (third-party tools never receive the parent's credentials).\n * @internal\n */\nexport function inheritSubAgentCredentials(tool: CustomTool, creds: InheritedCredentials): void {\n const sink = (tool as { [INHERIT_CREDENTIALS]?: CredentialSink })[INHERIT_CREDENTIALS];\n if (typeof sink === \"function\") sink(creds);\n}\n\n/** Arguments passed to {@link SubAgentSpec.messageFilter} (SE12). */\nexport interface MessageFilterArgs {\n /** The supervisor transcript (read-only text projection) available to this delegation. */\n messages: readonly ToolContextMessage[];\n /** The prompt about to be delegated (after any `onDelegationStart` rewrite). */\n input: string;\n /** The subagent's name. */\n name: string;\n}\n\n/** Context passed to {@link SubAgentSpec.onDelegationStart} before the child runs. */\nexport interface DelegationStartContext {\n input: string;\n name: string;\n /**\n * SE15 — 1-based count of times THIS subagent tool has been invoked (a\n * per-`defineSubAgent`-instance counter). Incremented before this hook runs;\n * a rejected delegation still counts. Enables reject-after-N patterns.\n */\n iteration: number;\n}\n\n/**\n * Decision returned from {@link SubAgentSpec.onDelegationStart}. Discriminated on\n * `proceed` so a rejection (`proceed: false` + `rejectionReason`) and an approval\n * (`modifiedInput`) cannot be mixed into one nonsensical object.\n */\nexport type DelegationStartDecision =\n | { proceed: false; rejectionReason?: string }\n | {\n proceed?: true;\n modifiedInput?: string;\n /** SE13 — cap the child's iteration count (forwarded as `SendOptions.maxIterations`). */\n modifiedMaxSteps?: number;\n };\n\n/** Context passed to {@link SubAgentSpec.onDelegationComplete} after the child settles. */\nexport interface DelegationCompleteContext {\n input: string;\n name: string;\n /** The child's text result (present on success). */\n result?: string;\n /** The error the child threw (present on failure); the error is still re-thrown. */\n error?: unknown;\n /** SE15 — the same 1-based iteration this delegation's `onDelegationStart` saw. */\n iteration: number;\n}\n\n/**\n * The return of a delegation hook: a decision, a promise of one, or nothing —\n * `void` lets a side-effect-only callback (`(ctx) => { log(ctx) }`) type-check,\n * which is the common case (mirrors a peer framework's `async ctx => { ... }` hooks).\n */\n// biome-ignore lint/suspicious/noConfusingVoidType: `void` is the idiomatic return for an optional-return callback; the rule false-positives on callback return unions.\ntype DelegationHookResult<T> = T | void | Promise<T | void>;\n\n/** Decision returned from {@link SubAgentSpec.onDelegationComplete}. */\nexport interface DelegationCompleteDecision {\n /** Appended to the child's result string. */\n feedback?: string;\n}\n\nexport interface SubAgentSpec {\n name: string;\n description: string;\n instructions: string;\n model?: string;\n tools?: CustomTool[];\n maxDelegationDepth?: number;\n /**\n * SE11 — called before the supervisor delegates. Return `{ proceed: false }`\n * to reject (the child never runs and `rejectionReason` becomes the tool\n * result), or `{ modifiedInput }` to rewrite the delegated prompt. A throwing\n * hook surfaces (never silently swallowed).\n */\n onDelegationStart?: (\n ctx: DelegationStartContext,\n ) => DelegationHookResult<DelegationStartDecision>;\n /**\n * SE11 — called after the delegation settles. On success `ctx.result` is set\n * and an optional `{ feedback }` is appended to it. On failure `ctx.error` is\n * set and the original error is ALWAYS re-thrown after this hook runs — a throw\n * from this hook on the error path is suppressed so it cannot mask the\n * delegation's real failure (on the success path a throw does propagate).\n */\n onDelegationComplete?: (\n ctx: DelegationCompleteContext,\n ) => DelegationHookResult<DelegationCompleteDecision>;\n /**\n * SE12 — opt-in parent-context forwarding. When set, the supervisor transcript\n * (`ctx.messages`, a read-only text projection) is passed to this filter and the\n * returned subset is forwarded to the child as a role-tagged context preamble\n * prepended to the delegated input. When ABSENT the child runs input-only —\n * memory isolation stays the default. A filter returning `[]` forwards nothing.\n * A throwing filter propagates (fail-fast, never swallowed — same contract as\n * `onDelegationStart`); the delegation surfaces as a tool error.\n */\n messageFilter?: (args: MessageFilterArgs) => readonly ToolContextMessage[];\n /**\n * SE14 — opt-in subagent result-context control. When `true`, the child's\n * completed tool-call results (name + result) are appended to the delegation\n * payload returned to the supervisor, inside a `<subagent-tool-results>` block.\n * When absent/`false` the delegation returns the child's final text only —\n * text-only stays the default (a peer framework's scoped posture). See ADR 0006.\n */\n includeToolResults?: boolean;\n}\n\nexport class MaxDelegationDepthError extends Error {\n readonly code = \"max_delegation_depth\" as const;\n constructor(\n public readonly currentDepth: number,\n public readonly maxDepth: number,\n ) {\n super(`Max delegation depth ${maxDepth} exceeded (current: ${currentDepth})`);\n this.name = \"MaxDelegationDepthError\";\n }\n}\n\n/**\n * Run the `onDelegationStart` hook; returns either a rejection or the (possibly\n * rewritten) input plus the optional SE13 `maxSteps` cap.\n */\nasync function applyDelegationStart(\n spec: SubAgentSpec,\n input: string,\n iteration: number,\n): Promise<{ reject: string } | { input: string; maxSteps?: number }> {\n if (spec.onDelegationStart === undefined) return { input };\n const decision = await spec.onDelegationStart({ input, name: spec.name, iteration });\n if (decision === undefined) return { input };\n if (decision.proceed === false)\n return { reject: decision.rejectionReason ?? \"(delegation rejected)\" };\n return {\n input: decision.modifiedInput ?? input,\n ...(decision.modifiedMaxSteps !== undefined ? { maxSteps: decision.modifiedMaxSteps } : {}),\n };\n}\n\n/**\n * SE14 — replay the child run's stream (a safe post-`wait()` idiom — the run buffers\n * events, `stream()` replays them) and collect every completed tool-call result into\n * a delimited block. Returns `\"\"` when the child ran no completed tool calls. See ADR 0006.\n */\nasync function collectChildToolResults(run: Run): Promise<string> {\n const lines: string[] = [];\n for await (const event of run.stream()) {\n if (event.type === \"tool_call\" && event.status === \"completed\") {\n const rendered =\n typeof event.result === \"string\" ? event.result : JSON.stringify(event.result ?? null);\n lines.push(`${event.name}: ${rendered}`);\n }\n }\n if (lines.length === 0) return \"\";\n return `\\n\\n<subagent-tool-results>\\n${lines.join(\"\\n\")}\\n</subagent-tool-results>`;\n}\n\n/**\n * Create the transient child agent and send the input, composing every forwarded\n * `SendOptions` onto ONE `send` call — SE10 `signal` + SE13 `maxIterations`. Absent\n * every option ⇒ the pre-SE10 single-arg `send(input)` shape. SE14 — when\n * `includeToolResults` is set, append the child's completed tool results. Dispose in `finally`.\n */\n/**\n * Build the child agent's `Agent.create` options: the child inherits the parent's\n * apiKey (else `Agent.create` throws \"Missing API key\"), its model (unless the spec\n * overrides it), and — #55 — the parent's plugins (permission gate/guards) so the\n * child's inner tool calls run under the same policy.\n */\nfunction buildChildCreateOptions(\n spec: SubAgentSpec,\n inherited: InheritedCredentials | undefined,\n): AgentOptions {\n const model: string | ModelSelection | undefined = spec.model\n ? { id: spec.model }\n : inherited?.model;\n return {\n ...(inherited?.apiKey !== undefined ? { apiKey: inherited.apiKey } : {}),\n ...(model !== undefined ? { model } : {}),\n ...(inherited?.plugins !== undefined ? { plugins: inherited.plugins } : {}),\n systemPrompt: spec.instructions,\n tools: spec.tools ?? [],\n };\n}\n\nasync function runChildAgent(\n spec: SubAgentSpec,\n input: string,\n signal: AbortSignal | undefined,\n maxSteps: number | undefined,\n inherited: InheritedCredentials | undefined,\n): Promise<string> {\n // SE45 cycle 3 — use the registered Agent facade via the DIP seam\n // (agent-factory-registry) instead of a dynamic `import(\"../agent.js\")`.\n // This removes the last madge cycle (a2a/subagent -> agent -> ... -> real-local-run-tools\n // -> a2a/subagent): the facade registers itself at module-init via setAgentFacade,\n // so subagent depends only on the registry port, never on the facade module.\n const agent = await getAgentFacade().create(buildChildCreateOptions(spec, inherited));\n try {\n const sendOptions: {\n signal?: AbortSignal;\n maxIterations?: number;\n origin?: import(\"../types/run.js\").MessageOrigin;\n } = {\n ...(signal !== undefined ? { signal } : {}),\n ...(maxSteps !== undefined ? { maxIterations: maxSteps } : {}),\n // SE3 — a delegated child's turn is initiated by the coordinating parent.\n origin: { kind: \"coordinator\" },\n };\n const run = await agent.send(input, sendOptions);\n const result = await run.wait();\n const text = result.result ?? \"(no response)\";\n // SE14 — text-only by default; opt-in appends the child's tool results.\n return spec.includeToolResults === true ? text + (await collectChildToolResults(run)) : text;\n } finally {\n agent.dispose();\n }\n}\n\n/**\n * Best-effort error-path notification: run `onDelegationComplete` with the child's\n * error so the caller can observe the failure. The observer's own throw (sync or\n * async) is suppressed here so it cannot mask the delegation's real error, which the\n * handler re-throws next.\n */\nasync function notifyDelegationError(\n spec: SubAgentSpec,\n input: string,\n error: unknown,\n iteration: number,\n): Promise<void> {\n if (spec.onDelegationComplete === undefined) return;\n try {\n await spec.onDelegationComplete({ input, name: spec.name, error, iteration });\n } catch {\n // Subordinate to `error`; the child's real cause wins.\n }\n}\n\n/**\n * SE12 — apply `messageFilter` (if set) and prepend the filtered supervisor\n * transcript to the delegated input as a role-tagged context preamble. Absent\n * filter OR no messages OR an empty filtered subset ⇒ the original input\n * (isolation-by-default preserved).\n */\nfunction applyMessageFilter(\n spec: SubAgentSpec,\n input: string,\n messages: readonly ToolContextMessage[] | undefined,\n): string {\n if (spec.messageFilter === undefined || messages === undefined) return input;\n const filtered = spec.messageFilter({ messages, input, name: spec.name });\n if (filtered.length === 0) return input;\n const preamble = filtered.map((m) => `${m.role}: ${m.content}`).join(\"\\n\");\n return `Prior conversation:\\n${preamble}\\n\\nTask:\\n${input}`;\n}\n\n/** Run the success-path `onDelegationComplete` hook; appends its `feedback` to the result. */\nasync function applyDelegationComplete(\n spec: SubAgentSpec,\n input: string,\n result: string,\n iteration: number,\n): Promise<string> {\n if (spec.onDelegationComplete === undefined) return result;\n const completion = await spec.onDelegationComplete({ input, name: spec.name, result, iteration });\n return completion?.feedback !== undefined ? result + completion.feedback : result;\n}\n\nfunction defineSubAgent(spec: SubAgentSpec, _parentDepth = 0): CustomTool {\n const currentDepth = _parentDepth + 1;\n const maxDepth = spec.maxDelegationDepth ?? 3;\n\n if (currentDepth > maxDepth) {\n throw new MaxDelegationDepthError(currentDepth, maxDepth);\n }\n\n // Zod for RUNTIME validation of the tool_use input …\n const inputZod = z.object({\n input: z.string().describe(\"Task for the subagent\"),\n });\n // … and a real Draft-7 JSON Schema for the LLM. `CustomTool.inputSchema` is sent\n // to the model verbatim; a raw Zod object would serialize to garbage, so the\n // model emits malformed input that fails `inputZod.parse` and the delegation\n // never runs (the previous bug — the schema and the validator are now distinct).\n const inputSchema: Record<string, unknown> = {\n type: \"object\",\n properties: {\n input: { type: \"string\", description: \"Task for the subagent\" },\n },\n required: [\"input\"],\n additionalProperties: false,\n };\n\n // SE15 — per-instance delegation counter, surfaced as `iteration` on the hook\n // contexts. Incremented once per handler invocation before onDelegationStart.\n let iteration = 0;\n\n // Credentials handed down by the parent runtime (see `inheritSubAgentCredentials`).\n let inherited: InheritedCredentials | undefined;\n\n const tool: CustomTool = {\n name: spec.name,\n description: spec.description,\n inputSchema,\n handler: async (\n rawInput: Record<string, unknown>,\n ctx?: {\n signal?: AbortSignal;\n context?: unknown;\n messages?: readonly ToolContextMessage[];\n },\n ): Promise<string> => {\n const { input: parsed } = inputZod.parse(rawInput);\n iteration += 1; // SE15 — before onDelegationStart; a rejected delegation still counts.\n // Pin THIS invocation's iteration before any await so a concurrent invocation\n // bumping the shared counter cannot change the value onDelegationComplete /\n // notifyDelegationError observe — they see the same iteration onDelegationStart did.\n const capturedIteration = iteration;\n\n const start = await applyDelegationStart(spec, parsed, capturedIteration);\n if (\"reject\" in start) return start.reject;\n // SE12 — opt-in: forward the filtered supervisor transcript as a preamble.\n const input = applyMessageFilter(spec, start.input, ctx?.messages);\n\n let result: string;\n try {\n // SE13 — apply the optional onDelegationStart maxSteps cap on the child send.\n result = await runChildAgent(spec, input, ctx?.signal, start.maxSteps, inherited);\n } catch (error) {\n // SE11 — notify the completion hook of the failure (best-effort observer),\n // then re-throw the ORIGINAL error (Rule 8: never swallow the delegation's\n // own failure).\n await notifyDelegationError(spec, input, error, capturedIteration);\n throw error;\n }\n return applyDelegationComplete(spec, input, result, capturedIteration);\n },\n };\n\n // Install the private credential sink (off the public shape + off every ctx).\n Object.defineProperty(tool, INHERIT_CREDENTIALS, {\n value: ((creds: InheritedCredentials) => {\n inherited = creds;\n }) satisfies CredentialSink,\n enumerable: false,\n });\n\n return tool;\n}\n\n/** SE36 — `SubAgent.create` replaces `defineSubAgent` (ADR 0015). @public */\nexport class SubAgent {\n private constructor() {}\n static create(spec: SubAgentSpec, parentDepth = 0): CustomTool {\n return defineSubAgent(spec, parentDepth);\n }\n}\n\n/**\n * Convert a parent's declarative `agents` map ({@link AgentDefinition} per key)\n * into delegation tools for the LOCAL runtime — the counterpart of the\n * cloud/fixture subagent wiring. Each child inherits the parent's `apiKey`/model\n * via {@link inheritSubAgentCredentials}; `def.model` overrides the model\n * (`\"inherit\"` keeps the parent's), and `def.tools` scopes the child to that\n * subset of the parent's tools (absent → the parent's full toolset, per the\n * `AgentDefinition.tools` contract).\n *\n * Per-subagent `mcpServers` on an `AgentDefinition` is honored by the cloud\n * runtime only; it is not wired into local delegation here.\n *\n * @internal\n */\nexport function subAgentToolsFromDefinitions(\n agents: Record<string, AgentDefinition>,\n parentTools: readonly CustomTool[],\n): CustomTool[] {\n return Object.entries(agents).map(([name, def]) => {\n const whitelist =\n Array.isArray(def.tools) && def.tools.length > 0 ? new Set(def.tools) : undefined;\n const childTools = whitelist ? parentTools.filter((t) => whitelist.has(t.name)) : parentTools;\n return defineSubAgent({\n name,\n description: def.description,\n instructions: def.prompt,\n ...(def.model !== undefined && def.model !== \"inherit\" ? { model: def.model.id } : {}),\n tools: [...childTools],\n });\n });\n}\n"]}
@@ -60,7 +60,7 @@ export interface DelegationCompleteContext {
60
60
  /**
61
61
  * The return of a delegation hook: a decision, a promise of one, or nothing —
62
62
  * `void` lets a side-effect-only callback (`(ctx) => { log(ctx) }`) type-check,
63
- * which is the common case (mirrors Mastra's `async ctx => { ... }` hooks).
63
+ * which is the common case (mirrors a peer framework's `async ctx => { ... }` hooks).
64
64
  */
65
65
  type DelegationHookResult<T> = T | void | Promise<T | void>;
66
66
  /** Decision returned from {@link SubAgentSpec.onDelegationComplete}. */
@@ -105,7 +105,7 @@ export interface SubAgentSpec {
105
105
  * completed tool-call results (name + result) are appended to the delegation
106
106
  * payload returned to the supervisor, inside a `<subagent-tool-results>` block.
107
107
  * When absent/`false` the delegation returns the child's final text only —
108
- * text-only stays the default (Mastra's scoped posture). See ADR 0006.
108
+ * text-only stays the default (a peer framework's scoped posture). See ADR 0006.
109
109
  */
110
110
  includeToolResults?: boolean;
111
111
  }
@@ -60,7 +60,7 @@ export interface DelegationCompleteContext {
60
60
  /**
61
61
  * The return of a delegation hook: a decision, a promise of one, or nothing —
62
62
  * `void` lets a side-effect-only callback (`(ctx) => { log(ctx) }`) type-check,
63
- * which is the common case (mirrors Mastra's `async ctx => { ... }` hooks).
63
+ * which is the common case (mirrors a peer framework's `async ctx => { ... }` hooks).
64
64
  */
65
65
  type DelegationHookResult<T> = T | void | Promise<T | void>;
66
66
  /** Decision returned from {@link SubAgentSpec.onDelegationComplete}. */
@@ -105,7 +105,7 @@ export interface SubAgentSpec {
105
105
  * completed tool-call results (name + result) are appended to the delegation
106
106
  * payload returned to the supervisor, inside a `<subagent-tool-results>` block.
107
107
  * When absent/`false` the delegation returns the child's final text only —
108
- * text-only stays the default (Mastra's scoped posture). See ADR 0006.
108
+ * text-only stays the default (a peer framework's scoped posture). See ADR 0006.
109
109
  */
110
110
  includeToolResults?: boolean;
111
111
  }
@@ -1,6 +1,6 @@
1
1
  import * as zod from 'zod';
2
2
  import { ZodType } from 'zod';
3
- import { M as ModelSelection, ac as SDKUserMessage, ae as SendOptions, b as Run, G as GenerateOptions, n as GenerateRunResult, _ as RunToCompletionOptions, $ as RunToCompletionResult, S as SDKMessage, al as StreamToCompletionResult, C as CustomTool, c as PermissionMode, a as McpServerConfig, P as Processor, u as MessageOrigin } from './run-CqihtbtU.cjs';
3
+ import { M as ModelSelection, ac as SDKUserMessage, ae as SendOptions, b as Run, G as GenerateOptions, n as GenerateRunResult, _ as RunToCompletionOptions, $ as RunToCompletionResult, S as SDKMessage, al as StreamToCompletionResult, C as CustomTool, c as PermissionMode, a as McpServerConfig, P as Processor, u as MessageOrigin } from './run-CsIsK9Gx.js';
4
4
 
5
5
  /**
6
6
  * Public `MemoryAdapter` contract (T1.1, ADRs D141 / D147).
@@ -1501,7 +1501,7 @@ interface MemorySettings {
1501
1501
  */
1502
1502
  autoInject?: boolean;
1503
1503
  /**
1504
- * Index + tools configuration (memory-system-openclaw-parity).
1504
+ * Index + tools configuration (memory-system-peer-project-parity).
1505
1505
  *
1506
1506
  * When `tools !== false`, the SDK registers `memory_search` and
1507
1507
  * `memory_get` with the LLM. Backed by SQLite + FTS5 (and sqlite-vec
@@ -1,6 +1,6 @@
1
1
  import * as zod from 'zod';
2
2
  import { ZodType } from 'zod';
3
- import { M as ModelSelection, ac as SDKUserMessage, ae as SendOptions, b as Run, G as GenerateOptions, n as GenerateRunResult, _ as RunToCompletionOptions, $ as RunToCompletionResult, S as SDKMessage, al as StreamToCompletionResult, C as CustomTool, c as PermissionMode, a as McpServerConfig, P as Processor, u as MessageOrigin } from './run-CqihtbtU.js';
3
+ import { M as ModelSelection, ac as SDKUserMessage, ae as SendOptions, b as Run, G as GenerateOptions, n as GenerateRunResult, _ as RunToCompletionOptions, $ as RunToCompletionResult, S as SDKMessage, al as StreamToCompletionResult, C as CustomTool, c as PermissionMode, a as McpServerConfig, P as Processor, u as MessageOrigin } from './run-CsIsK9Gx.cjs';
4
4
 
5
5
  /**
6
6
  * Public `MemoryAdapter` contract (T1.1, ADRs D141 / D147).
@@ -1501,7 +1501,7 @@ interface MemorySettings {
1501
1501
  */
1502
1502
  autoInject?: boolean;
1503
1503
  /**
1504
- * Index + tools configuration (memory-system-openclaw-parity).
1504
+ * Index + tools configuration (memory-system-peer-project-parity).
1505
1505
  *
1506
1506
  * When `tools !== false`, the SDK registers `memory_search` and
1507
1507
  * `memory_get` with the LLM. Backed by SQLite + FTS5 (and sqlite-vec