@theokit/sdk 4.55.0 → 4.56.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/dist/a2a/index.cjs +13 -4
  3. package/dist/a2a/index.cjs.map +1 -1
  4. package/dist/a2a/index.d.cts +1 -1
  5. package/dist/a2a/index.d.ts +1 -1
  6. package/dist/a2a/index.js +13 -5
  7. package/dist/a2a/index.js.map +1 -1
  8. package/dist/a2a/message-bus.d.cts +33 -4
  9. package/dist/a2a/message-bus.d.ts +33 -4
  10. package/dist/{agent-HDEVVUBS.js → agent-K3PQBS7D.js} +6 -5
  11. package/dist/{agent-HDEVVUBS.js.map → agent-K3PQBS7D.js.map} +1 -1
  12. package/dist/{agent-R2HOJFZJ.cjs → agent-PJMBK7HQ.cjs} +7 -6
  13. package/dist/{agent-R2HOJFZJ.cjs.map → agent-PJMBK7HQ.cjs.map} +1 -1
  14. package/dist/{chunk-5UOVYM3P.cjs → chunk-6VLKVX7O.cjs} +131 -54
  15. package/dist/chunk-6VLKVX7O.cjs.map +1 -0
  16. package/dist/{chunk-5PHVENFV.js → chunk-7FHZ4VQX.js} +3 -55
  17. package/dist/chunk-7FHZ4VQX.js.map +1 -0
  18. package/dist/{chunk-Q47R5E2X.cjs → chunk-E3WZ6Y4H.cjs} +7 -59
  19. package/dist/chunk-E3WZ6Y4H.cjs.map +1 -0
  20. package/dist/{chunk-ITSSOVOB.cjs → chunk-H5GOSXSC.cjs} +4 -4
  21. package/dist/{chunk-ITSSOVOB.cjs.map → chunk-H5GOSXSC.cjs.map} +1 -1
  22. package/dist/{chunk-I5BE5M5L.js → chunk-KBXWEWKS.js} +4 -4
  23. package/dist/{chunk-I5BE5M5L.js.map → chunk-KBXWEWKS.js.map} +1 -1
  24. package/dist/chunk-KRD3GQAA.cjs +71 -0
  25. package/dist/chunk-KRD3GQAA.cjs.map +1 -0
  26. package/dist/chunk-KVNWIAO4.js +68 -0
  27. package/dist/chunk-KVNWIAO4.js.map +1 -0
  28. package/dist/{chunk-F5WMX4EA.cjs → chunk-LQYQHXIW.cjs} +19 -19
  29. package/dist/{chunk-F5WMX4EA.cjs.map → chunk-LQYQHXIW.cjs.map} +1 -1
  30. package/dist/{chunk-IVNNSANC.js → chunk-M7UHYBEY.js} +3 -3
  31. package/dist/{chunk-IVNNSANC.js.map → chunk-M7UHYBEY.js.map} +1 -1
  32. package/dist/{chunk-DAPSQZT4.js → chunk-ODTZ7HX3.js} +131 -54
  33. package/dist/chunk-ODTZ7HX3.js.map +1 -0
  34. package/dist/context/index.cjs +6 -5
  35. package/dist/context/index.cjs.map +1 -1
  36. package/dist/context/index.js +3 -2
  37. package/dist/context/index.js.map +1 -1
  38. package/dist/cron.cjs +6 -5
  39. package/dist/cron.js +5 -4
  40. package/dist/eval.cjs +5 -4
  41. package/dist/eval.cjs.map +1 -1
  42. package/dist/eval.js +4 -3
  43. package/dist/eval.js.map +1 -1
  44. package/dist/{index-manager-ZMRJ6ZII.js → index-manager-WBIMDVQT.js} +4 -3
  45. package/dist/{index-manager-ZMRJ6ZII.js.map → index-manager-WBIMDVQT.js.map} +1 -1
  46. package/dist/{index-manager-NG5YENWO.cjs → index-manager-WC5ZVJWO.cjs} +5 -4
  47. package/dist/{index-manager-NG5YENWO.cjs.map → index-manager-WC5ZVJWO.cjs.map} +1 -1
  48. package/dist/index.cjs +28 -27
  49. package/dist/index.cjs.map +1 -1
  50. package/dist/index.js +8 -7
  51. package/dist/index.js.map +1 -1
  52. package/dist/internal/memory/storage/markdown-store.d.ts +14 -2
  53. package/dist/internal/memory/storage/memory-file.d.ts +54 -0
  54. package/dist/internal/memory/types.d.ts +28 -0
  55. package/dist/sandbox/index.cjs +1 -1
  56. package/dist/sandbox/index.cjs.map +1 -1
  57. package/dist/sandbox/index.js +1 -1
  58. package/dist/sandbox/index.js.map +1 -1
  59. package/docs/error-codes.md +3 -1
  60. package/docs/harness-capability-map.md +2 -1
  61. package/package.json +1 -1
  62. package/dist/chunk-5PHVENFV.js.map +0 -1
  63. package/dist/chunk-5UOVYM3P.cjs.map +0 -1
  64. package/dist/chunk-DAPSQZT4.js.map +0 -1
  65. package/dist/chunk-Q47R5E2X.cjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,68 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.56.0
4
+
5
+ ### Minor Changes
6
+
7
+ - b08f696: `MessageBus.request` now rejects a timeout with `A2ARequestTimeoutError`, carrying
8
+ `code: "a2a_request_timeout"` plus the peer's address and the limit as fields.
9
+
10
+ It used to reject with a plain `Error` and no code, so the only way to identify a timeout was to
11
+ match the message — the practice `docs/error-codes.md` tells consumers never to rely on, and that
12
+ message embeds the address and the limit, so it changes with context exactly as the document warns.
13
+
14
+ The distinction this restores is what a retry policy is built on: a peer that did not answer is
15
+ transient and worth retrying, a peer whose handler threw is likely deterministic. A handler's own
16
+ error still propagates unchanged and is not this type.
17
+
18
+ - f7e70e4: The memory store now writes the layout the Claude Code CLI reads.
19
+
20
+ This SDK's differentiator is that it emits the formats that CLI opens — point `local.sessionDir` at
21
+ `~/.claude` and `--continue` a session your agent wrote. Memory did not hold that line: a fact was a
22
+ bullet under `## Facts`, so pointing a memory directory at `~/.claude/projects/<project>/memory/`
23
+ produced nothing the CLI could read.
24
+
25
+ Now each memory is its own file with the frontmatter Claude Code writes — `name`, `description`, and
26
+ `metadata` carrying `type` and an ISO 8601 `modified` — and `MEMORY.md` is the index that points at
27
+ them.
28
+
29
+ Legacy `## Facts` bullets are still read, so no store loses what it recorded. The brief encoding that
30
+ put a fact's kind in a trailing HTML comment never reached a published version, so there is nothing
31
+ to migrate from it.
32
+
33
+ `parseSimpleYaml` also stops flattening nested maps: `metadata:` with indented keys used to yield
34
+ `metadata: []` plus the nested keys as top-level entries, so `metadata.type` read as `undefined`
35
+ while `type` appeared where it never was.
36
+
37
+ - 5036f04: A memory fact can now say what it IS and when it was learned.
38
+
39
+ `MemoryFact` gains an optional `kind` — `user`, `feedback`, `project` or `reference` — and a
40
+ `modified` timestamp. Without them a durable preference and a project note that went stale were
41
+ indistinguishable: no staleness signal, no way for recall to filter, no basis for selective
42
+ retention, and no way for a surface to separate "what I remember about you" from "what I know about
43
+ this project".
44
+
45
+ Additive, so existing stores keep working. A hand-written bullet under `## Facts` still parses and
46
+ stays untyped — a kind is never inferred, because a wrong one makes recall confident about the wrong
47
+ thing. `modified` is stamped by the SDK and ignored when supplied by a caller: a timestamp a caller
48
+ can set is one that can lie about when something was learned.
49
+
50
+ ### Patch Changes
51
+
52
+ - 7587c00: A confined command that spawns a child no longer loses its output.
53
+
54
+ The restricted-network seccomp filter denied `getsockname`, `getpeername`, `setsockopt` and
55
+ `getsockopt`. Those four take an already-open fd, and cBPF cannot dereference one to learn its
56
+ address family — so they were denied on AF_UNIX too, which is what libuv uses for a child's IPC
57
+ channel. Any command that spawned a child died, and the parent's buffered stdout died with it:
58
+ `node --test` returned zero lines through `shell_exec`, and an agent reading test output saw an
59
+ empty string.
60
+
61
+ The four leave the denied set. Everything that takes an address or changes an fd's role —
62
+ `connect`, `bind`, `listen`, `accept`, `accept4`, `sendto`, `sendmmsg`, `recvmmsg`, `shutdown` —
63
+ stays denied, and `socket()` still refuses every family but AF_UNIX. Measured across the fix: an
64
+ AF_INET socket is `EPERM` before and after.
65
+
3
66
  ## 4.55.0
4
67
 
5
68
  ### Minor Changes
@@ -35,6 +35,17 @@ var AgentMailbox = class {
35
35
  };
36
36
 
37
37
  // src/a2a/message-bus.ts
38
+ var A2ARequestTimeoutError = class extends Error {
39
+ constructor(to, timeoutMs) {
40
+ super(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`);
41
+ this.to = to;
42
+ this.timeoutMs = timeoutMs;
43
+ this.name = "A2ARequestTimeoutError";
44
+ }
45
+ to;
46
+ timeoutMs;
47
+ code = "a2a_request_timeout";
48
+ };
38
49
  var MessageBus = class {
39
50
  _handlers = /* @__PURE__ */ new Map();
40
51
  register(agentId, handler) {
@@ -81,10 +92,7 @@ var MessageBus = class {
81
92
  return await Promise.race([
82
93
  Promise.resolve(handler(message)),
83
94
  new Promise((_, reject) => {
84
- timer = setTimeout(
85
- () => reject(new Error(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`)),
86
- timeoutMs
87
- );
95
+ timer = setTimeout(() => reject(new A2ARequestTimeoutError(to, timeoutMs)), timeoutMs);
88
96
  })
89
97
  ]);
90
98
  } finally {
@@ -107,6 +115,7 @@ Object.defineProperty(exports, "SubAgent", {
107
115
  enumerable: true,
108
116
  get: function () { return chunk4O5TGQBW_cjs.SubAgent; }
109
117
  });
118
+ exports.A2ARequestTimeoutError = A2ARequestTimeoutError;
110
119
  exports.AgentMailbox = AgentMailbox;
111
120
  exports.MessageBus = MessageBus;
112
121
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/a2a/agent-mailbox.ts","../../src/a2a/message-bus.ts"],"names":["diag"],"mappings":";;;;;;;AAuCO,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,MAA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,EAAA,CAAI,CAAC,CAAA;AAAA,YACtF;AAAA,WACF;AAAA,QACF,CAAC;AAAA,OACF,CAAA;AAAA,IACH,CAAA,SAAE;AACA,MAAA,IAAI,KAAA,KAAU,MAAA,EAAW,YAAA,CAAa,KAAK,CAAA;AAAA,IAC7C;AAAA,EACF;AAAA,EAEA,IAAI,OAAA,EAA0B;AAC5B,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,GAAA,CAAI,OAAO,CAAA;AAAA,EACnC;AAAA,EAEA,UAAA,GAAuB;AACrB,IAAA,OAAO,CAAC,GAAG,IAAA,CAAK,SAAA,CAAU,MAAM,CAAA;AAAA,EAClC;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\n/**\n * One agent's endpoint on a {@link MessageBus}: registers `agentId` on\n * construction and forwards inbound messages to the handler installed by\n * `onMessage`.\n *\n * const mailbox = new AgentMailbox(\"worker\", bus);\n * mailbox.onMessage((m) => `echo:${m.payload}`);\n * await mailbox.send(\"boss\", { type: \"ready\", payload: null });\n *\n * Needs an already-constructed `MessageBus` — the mailbox does not create one, and\n * two mailboxes reach each other only through the SAME bus instance.\n *\n * How it fails: `send` and `request` are thin forwards, so they carry the bus's\n * failures unchanged (plain `Error` for an unregistered peer, plus a timeout\n * rejection for `request`).\n *\n * Traps:\n * - The constructor registers IMMEDIATELY, before `onMessage` can be called. A\n * message arriving in that window is dropped silently, and a peer's `request`\n * RESOLVES WITH `undefined` instead of timing out — install the handler in the\n * same tick as construction.\n * - Constructing a second mailbox with the same `agentId` on the same bus takes\n * the id over silently (`MessageBus.register` overwrites), and the loser's\n * `dispose()` then unregisters the WINNER.\n * - `dispose()` only unregisters and clears the handler. Requests already in\n * flight are neither cancelled nor rejected; they time out on the caller's side.\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 { diag } from \"../internal/diagnostics.js\";\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\n/**\n * Per-call knobs for {@link MessageBus.request} / `AgentMailbox.request`.\n */\nexport interface RequestOptions {\n /**\n * How long to wait for the peer handler to settle before rejecting.\n * Default `30_000` (30 s). The rejection is a plain `Error` reading\n * `A2A request timeout: <to> did not respond within <n>ms` — there is no typed\n * timeout class and no `code` field, so a caller that must distinguish a\n * timeout from a handler failure has to match on the message.\n */\n timeoutMs?: number;\n}\n\n/**\n * In-process router that delivers A2A messages between agents keyed by string id.\n *\n * Purely in-memory and single-process: nothing is persisted and nothing crosses a\n * process or network boundary. Register one handler per agent id, then `send`\n * (fire-and-forget) or `request` (await the handler's return value).\n *\n * const bus = new MessageBus();\n * bus.register(\"worker\", (m) => `echo:${m.payload}`);\n * const reply = await bus.request(\"boss\", \"worker\", { type: \"ping\", payload: 1 });\n *\n * Callers supply only `{ type, payload }`; the bus stamps `from`, `to`,\n * `timestamp` and `origin` onto the delivered {@link A2AMessage}.\n *\n * How it fails: `send` and `request` both reject with a plain `Error`\n * (`Agent \"<to>\" not registered on MessageBus`) when `to` has no handler, and\n * `request` additionally rejects on timeout. No typed error class is exported for\n * either, so there is nothing to `instanceof`.\n *\n * Traps:\n * - `register` silently OVERWRITES an existing id; the previous handler simply\n * stops receiving anything. Call `has(id)` first when that matters.\n * - `send` is declared `async` but never awaits the handler. A handler that\n * rejects produces an UNHANDLED rejection that `await bus.send(...)` does not\n * observe — only the \"not registered\" failure reaches the caller. Use\n * `request` when the handler's failure has to be visible.\n * - `from` is whatever the caller passes; the bus never checks that the sender\n * is registered. It is self-asserted provenance, not authentication.\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: the SENDER does not wait for the result, which is the whole point of\n // `send`. It does not mean nobody is told when delivery fails — that is the difference\n // between asynchronous and silent (#365).\n //\n // The returned promise used to be dropped on the floor. `MessageHandler` may return one, so a\n // rejecting handler became an unhandled rejection: fatal under Node's default\n // `--unhandled-rejections=throw`, and invisible to the caller, whose `await send(...)`\n // resolved cleanly either way. Attaching a catch keeps the call non-blocking and reports the\n // failure instead of discarding it (`error-handling.md` § 5).\n void Promise.resolve(handler(message)).catch((cause: unknown) => {\n const reason = cause instanceof Error ? cause.message : String(cause);\n diag(`[theokit-sdk] a2a: handler for \"${to}\" failed on a fire-and-forget send: ${reason}\\n`);\n });\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"]}
1
+ {"version":3,"sources":["../../src/a2a/agent-mailbox.ts","../../src/a2a/message-bus.ts"],"names":["diag"],"mappings":";;;;;;;AAuCO,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;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GAAsB;AAAA,MAC1B,GAAG,OAAA;AAAA,MACH,IAAA;AAAA,MACA,EAAA;AAAA,MACA,SAAA,EAAW,KAAK,GAAA,EAAI;AAAA;AAAA,MAEpB,MAAA,EAAQ,EAAE,IAAA,EAAM,MAAA,EAAQ,IAAA;AAAK,KAC/B;AAIA,IAAA,IAAI,KAAA;AACJ,IAAA,IAAI;AACF,MAAA,OAAO,MAAM,QAAQ,IAAA,CAAK;AAAA,QACxB,OAAA,CAAQ,OAAA,CAAQ,OAAA,CAAQ,OAAO,CAAC,CAAA;AAAA,QAChC,IAAI,OAAA,CAAQ,CAAC,CAAA,EAAG,MAAA,KAAW;AACzB,UAAA,KAAA,GAAQ,UAAA,CAAW,MAAM,MAAA,CAAO,IAAI,uBAAuB,EAAA,EAAI,SAAS,CAAC,CAAA,EAAG,SAAS,CAAA;AAAA,QACvF,CAAC;AAAA,OACF,CAAA;AAAA,IACH,CAAA,SAAE;AACA,MAAA,IAAI,KAAA,KAAU,MAAA,EAAW,YAAA,CAAa,KAAK,CAAA;AAAA,IAC7C;AAAA,EACF;AAAA,EAEA,IAAI,OAAA,EAA0B;AAC5B,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,GAAA,CAAI,OAAO,CAAA;AAAA,EACnC;AAAA,EAEA,UAAA,GAAuB;AACrB,IAAA,OAAO,CAAC,GAAG,IAAA,CAAK,SAAA,CAAU,MAAM,CAAA;AAAA,EAClC;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\n/**\n * One agent's endpoint on a {@link MessageBus}: registers `agentId` on\n * construction and forwards inbound messages to the handler installed by\n * `onMessage`.\n *\n * const mailbox = new AgentMailbox(\"worker\", bus);\n * mailbox.onMessage((m) => `echo:${m.payload}`);\n * await mailbox.send(\"boss\", { type: \"ready\", payload: null });\n *\n * Needs an already-constructed `MessageBus` — the mailbox does not create one, and\n * two mailboxes reach each other only through the SAME bus instance.\n *\n * How it fails: `send` and `request` are thin forwards, so they carry the bus's\n * failures unchanged (plain `Error` for an unregistered peer, plus a timeout\n * rejection for `request`).\n *\n * Traps:\n * - The constructor registers IMMEDIATELY, before `onMessage` can be called. A\n * message arriving in that window is dropped silently, and a peer's `request`\n * RESOLVES WITH `undefined` instead of timing out — install the handler in the\n * same tick as construction.\n * - Constructing a second mailbox with the same `agentId` on the same bus takes\n * the id over silently (`MessageBus.register` overwrites), and the loser's\n * `dispose()` then unregisters the WINNER.\n * - `dispose()` only unregisters and clears the handler. Requests already in\n * flight are neither cancelled nor rejected; they time out on the caller's side.\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 { diag } from \"../internal/diagnostics.js\";\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\n/**\n * A peer did not answer a {@link MessageBus.request} within its timeout.\n *\n * Carries `code: \"a2a_request_timeout\"`, the peer's address and the limit as FIELDS, so a caller\n * branches on the contract rather than on prose — `docs/error-codes.md`: \"Branch on `code`, never\n * on the message: messages carry context (an id, a path, a limit) and change with it, while a code\n * is the contract.\" Until #380 this rejection was a plain `Error` with neither, and the message it\n * carried embeds both the address and the limit, so matching on it changed with context exactly as\n * that document warns.\n *\n * The distinction it restores is what a retry policy is built on. A peer that did not answer in\n * time is transient and worth retrying; a peer whose handler threw is likely deterministic and\n * retrying repeats the failure. A handler's own error propagates unchanged and is NOT this type —\n * an unattended agent can therefore tell the two apart without reading English.\n *\n * @public\n */\nexport class A2ARequestTimeoutError extends Error {\n readonly code = \"a2a_request_timeout\" as const;\n constructor(\n /** The peer that did not answer. */\n public readonly to: string,\n /** The limit it exceeded, in milliseconds. */\n public readonly timeoutMs: number,\n ) {\n super(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`);\n this.name = \"A2ARequestTimeoutError\";\n }\n}\n\n/**\n * Per-call knobs for {@link MessageBus.request} / `AgentMailbox.request`.\n */\nexport interface RequestOptions {\n /**\n * How long to wait for the peer handler to settle before rejecting.\n * Default `30_000` (30 s). The rejection is an {@link A2ARequestTimeoutError} carrying\n * `code: \"a2a_request_timeout\"`, `to` and `timeoutMs` (#380), so a caller distinguishes a\n * timeout from a handler failure by branching on the code — a handler's own error propagates\n * unchanged and is not that type.\n */\n timeoutMs?: number;\n}\n\n/**\n * In-process router that delivers A2A messages between agents keyed by string id.\n *\n * Purely in-memory and single-process: nothing is persisted and nothing crosses a\n * process or network boundary. Register one handler per agent id, then `send`\n * (fire-and-forget) or `request` (await the handler's return value).\n *\n * const bus = new MessageBus();\n * bus.register(\"worker\", (m) => `echo:${m.payload}`);\n * const reply = await bus.request(\"boss\", \"worker\", { type: \"ping\", payload: 1 });\n *\n * Callers supply only `{ type, payload }`; the bus stamps `from`, `to`,\n * `timestamp` and `origin` onto the delivered {@link A2AMessage}.\n *\n * How it fails: `send` and `request` both reject with a plain `Error`\n * (`Agent \"<to>\" not registered on MessageBus`) when `to` has no handler, and\n * `request` additionally rejects on timeout. No typed error class is exported for\n * either, so there is nothing to `instanceof`.\n *\n * Traps:\n * - `register` silently OVERWRITES an existing id; the previous handler simply\n * stops receiving anything. Call `has(id)` first when that matters.\n * - `send` is declared `async` but never awaits the handler. A handler that\n * rejects produces an UNHANDLED rejection that `await bus.send(...)` does not\n * observe — only the \"not registered\" failure reaches the caller. Use\n * `request` when the handler's failure has to be visible.\n * - `from` is whatever the caller passes; the bus never checks that the sender\n * is registered. It is self-asserted provenance, not authentication.\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: the SENDER does not wait for the result, which is the whole point of\n // `send`. It does not mean nobody is told when delivery fails — that is the difference\n // between asynchronous and silent (#365).\n //\n // The returned promise used to be dropped on the floor. `MessageHandler` may return one, so a\n // rejecting handler became an unhandled rejection: fatal under Node's default\n // `--unhandled-rejections=throw`, and invisible to the caller, whose `await send(...)`\n // resolved cleanly either way. Attaching a catch keeps the call non-blocking and reports the\n // failure instead of discarding it (`error-handling.md` § 5).\n void Promise.resolve(handler(message)).catch((cause: unknown) => {\n const reason = cause instanceof Error ? cause.message : String(cause);\n diag(`[theokit-sdk] a2a: handler for \"${to}\" failed on a fire-and-forget send: ${reason}\\n`);\n });\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(() => reject(new A2ARequestTimeoutError(to, timeoutMs)), timeoutMs);\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"]}
@@ -5,6 +5,6 @@
5
5
  */
6
6
  export type { ToolContextMessage } from "../types/agent-prims.js";
7
7
  export { AgentMailbox } from "./agent-mailbox.js";
8
- export { MessageBus, type RequestOptions } from "./message-bus.js";
8
+ export { A2ARequestTimeoutError, MessageBus, type RequestOptions } from "./message-bus.js";
9
9
  export { type DelegationCompleteContext, type DelegationCompleteDecision, type DelegationStartContext, type DelegationStartDecision, MaxDelegationDepthError, type MessageFilterArgs, SubAgent, type SubAgentSpec, } from "./subagent.js";
10
10
  export type { A2AMessage, MessageHandler } from "./types.js";
@@ -5,6 +5,6 @@
5
5
  */
6
6
  export type { ToolContextMessage } from "../types/agent-prims.js";
7
7
  export { AgentMailbox } from "./agent-mailbox.js";
8
- export { MessageBus, type RequestOptions } from "./message-bus.js";
8
+ export { A2ARequestTimeoutError, MessageBus, type RequestOptions } from "./message-bus.js";
9
9
  export { type DelegationCompleteContext, type DelegationCompleteDecision, type DelegationStartContext, type DelegationStartDecision, MaxDelegationDepthError, type MessageFilterArgs, SubAgent, type SubAgentSpec, } from "./subagent.js";
10
10
  export type { A2AMessage, MessageHandler } from "./types.js";
package/dist/a2a/index.js CHANGED
@@ -33,6 +33,17 @@ var AgentMailbox = class {
33
33
  };
34
34
 
35
35
  // src/a2a/message-bus.ts
36
+ var A2ARequestTimeoutError = class extends Error {
37
+ constructor(to, timeoutMs) {
38
+ super(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`);
39
+ this.to = to;
40
+ this.timeoutMs = timeoutMs;
41
+ this.name = "A2ARequestTimeoutError";
42
+ }
43
+ to;
44
+ timeoutMs;
45
+ code = "a2a_request_timeout";
46
+ };
36
47
  var MessageBus = class {
37
48
  _handlers = /* @__PURE__ */ new Map();
38
49
  register(agentId, handler) {
@@ -79,10 +90,7 @@ var MessageBus = class {
79
90
  return await Promise.race([
80
91
  Promise.resolve(handler(message)),
81
92
  new Promise((_, reject) => {
82
- timer = setTimeout(
83
- () => reject(new Error(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`)),
84
- timeoutMs
85
- );
93
+ timer = setTimeout(() => reject(new A2ARequestTimeoutError(to, timeoutMs)), timeoutMs);
86
94
  })
87
95
  ]);
88
96
  } finally {
@@ -97,6 +105,6 @@ var MessageBus = class {
97
105
  }
98
106
  };
99
107
 
100
- export { AgentMailbox, MessageBus };
108
+ export { A2ARequestTimeoutError, AgentMailbox, MessageBus };
101
109
  //# sourceMappingURL=index.js.map
102
110
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/a2a/agent-mailbox.ts","../../src/a2a/message-bus.ts"],"names":[],"mappings":";;;;;AAuCO,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,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AA;AACJ,IAAA,IAAI;AACF,MAAA,OAAO,MAAM,QAAQ,IAAA,CAAK;AAAA,QACxB,OAAA,CAAQ,OAAA,CAAQ,OAAA,CAAQ,OAAO,CAAC,CAAA;AAAA,QAChC,IAAI,OAAA,CAAQ,CAAC,CAAA,EAAG,MAAA,KAAW;AACzB,UAAA,KAAA,GAAQ,UAAA;AAAA,YACN,MACE,OAAO,IAAI,KAAA,CAAM,wBAAwB,EAAE,CAAA,wBAAA,EAA2B,SAAS,CAAA,EAAA,CAAI,CAAC,CAAA;AAAA,YACtF;AAAA,WACF;AAAA,QACF,CAAC;AAAA,OACF,CAAA;AAAA,IACH,CAAA,SAAE;AACA,MAAA,IAAI,KAAA,KAAU,MAAA,EAAW,YAAA,CAAa,KAAK,CAAA;AAAA,IAC7C;AAAA,EACF;AAAA,EAEA,IAAI,OAAA,EAA0B;AAC5B,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,GAAA,CAAI,OAAO,CAAA;AAAA,EACnC;AAAA,EAEA,UAAA,GAAuB;AACrB,IAAA,OAAO,CAAC,GAAG,IAAA,CAAK,SAAA,CAAU,MAAM,CAAA;AAAA,EAClC;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\n/**\n * One agent's endpoint on a {@link MessageBus}: registers `agentId` on\n * construction and forwards inbound messages to the handler installed by\n * `onMessage`.\n *\n * const mailbox = new AgentMailbox(\"worker\", bus);\n * mailbox.onMessage((m) => `echo:${m.payload}`);\n * await mailbox.send(\"boss\", { type: \"ready\", payload: null });\n *\n * Needs an already-constructed `MessageBus` — the mailbox does not create one, and\n * two mailboxes reach each other only through the SAME bus instance.\n *\n * How it fails: `send` and `request` are thin forwards, so they carry the bus's\n * failures unchanged (plain `Error` for an unregistered peer, plus a timeout\n * rejection for `request`).\n *\n * Traps:\n * - The constructor registers IMMEDIATELY, before `onMessage` can be called. A\n * message arriving in that window is dropped silently, and a peer's `request`\n * RESOLVES WITH `undefined` instead of timing out — install the handler in the\n * same tick as construction.\n * - Constructing a second mailbox with the same `agentId` on the same bus takes\n * the id over silently (`MessageBus.register` overwrites), and the loser's\n * `dispose()` then unregisters the WINNER.\n * - `dispose()` only unregisters and clears the handler. Requests already in\n * flight are neither cancelled nor rejected; they time out on the caller's side.\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 { diag } from \"../internal/diagnostics.js\";\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\n/**\n * Per-call knobs for {@link MessageBus.request} / `AgentMailbox.request`.\n */\nexport interface RequestOptions {\n /**\n * How long to wait for the peer handler to settle before rejecting.\n * Default `30_000` (30 s). The rejection is a plain `Error` reading\n * `A2A request timeout: <to> did not respond within <n>ms` — there is no typed\n * timeout class and no `code` field, so a caller that must distinguish a\n * timeout from a handler failure has to match on the message.\n */\n timeoutMs?: number;\n}\n\n/**\n * In-process router that delivers A2A messages between agents keyed by string id.\n *\n * Purely in-memory and single-process: nothing is persisted and nothing crosses a\n * process or network boundary. Register one handler per agent id, then `send`\n * (fire-and-forget) or `request` (await the handler's return value).\n *\n * const bus = new MessageBus();\n * bus.register(\"worker\", (m) => `echo:${m.payload}`);\n * const reply = await bus.request(\"boss\", \"worker\", { type: \"ping\", payload: 1 });\n *\n * Callers supply only `{ type, payload }`; the bus stamps `from`, `to`,\n * `timestamp` and `origin` onto the delivered {@link A2AMessage}.\n *\n * How it fails: `send` and `request` both reject with a plain `Error`\n * (`Agent \"<to>\" not registered on MessageBus`) when `to` has no handler, and\n * `request` additionally rejects on timeout. No typed error class is exported for\n * either, so there is nothing to `instanceof`.\n *\n * Traps:\n * - `register` silently OVERWRITES an existing id; the previous handler simply\n * stops receiving anything. Call `has(id)` first when that matters.\n * - `send` is declared `async` but never awaits the handler. A handler that\n * rejects produces an UNHANDLED rejection that `await bus.send(...)` does not\n * observe — only the \"not registered\" failure reaches the caller. Use\n * `request` when the handler's failure has to be visible.\n * - `from` is whatever the caller passes; the bus never checks that the sender\n * is registered. It is self-asserted provenance, not authentication.\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: the SENDER does not wait for the result, which is the whole point of\n // `send`. It does not mean nobody is told when delivery fails — that is the difference\n // between asynchronous and silent (#365).\n //\n // The returned promise used to be dropped on the floor. `MessageHandler` may return one, so a\n // rejecting handler became an unhandled rejection: fatal under Node's default\n // `--unhandled-rejections=throw`, and invisible to the caller, whose `await send(...)`\n // resolved cleanly either way. Attaching a catch keeps the call non-blocking and reports the\n // failure instead of discarding it (`error-handling.md` § 5).\n void Promise.resolve(handler(message)).catch((cause: unknown) => {\n const reason = cause instanceof Error ? cause.message : String(cause);\n diag(`[theokit-sdk] a2a: handler for \"${to}\" failed on a fire-and-forget send: ${reason}\\n`);\n });\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"]}
1
+ {"version":3,"sources":["../../src/a2a/agent-mailbox.ts","../../src/a2a/message-bus.ts"],"names":[],"mappings":";;;;;AAuCO,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;A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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\n/**\n * One agent's endpoint on a {@link MessageBus}: registers `agentId` on\n * construction and forwards inbound messages to the handler installed by\n * `onMessage`.\n *\n * const mailbox = new AgentMailbox(\"worker\", bus);\n * mailbox.onMessage((m) => `echo:${m.payload}`);\n * await mailbox.send(\"boss\", { type: \"ready\", payload: null });\n *\n * Needs an already-constructed `MessageBus` — the mailbox does not create one, and\n * two mailboxes reach each other only through the SAME bus instance.\n *\n * How it fails: `send` and `request` are thin forwards, so they carry the bus's\n * failures unchanged (plain `Error` for an unregistered peer, plus a timeout\n * rejection for `request`).\n *\n * Traps:\n * - The constructor registers IMMEDIATELY, before `onMessage` can be called. A\n * message arriving in that window is dropped silently, and a peer's `request`\n * RESOLVES WITH `undefined` instead of timing out — install the handler in the\n * same tick as construction.\n * - Constructing a second mailbox with the same `agentId` on the same bus takes\n * the id over silently (`MessageBus.register` overwrites), and the loser's\n * `dispose()` then unregisters the WINNER.\n * - `dispose()` only unregisters and clears the handler. Requests already in\n * flight are neither cancelled nor rejected; they time out on the caller's side.\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 { diag } from \"../internal/diagnostics.js\";\nimport type { A2AMessage, MessageHandler } from \"./types.js\";\n\n/**\n * A peer did not answer a {@link MessageBus.request} within its timeout.\n *\n * Carries `code: \"a2a_request_timeout\"`, the peer's address and the limit as FIELDS, so a caller\n * branches on the contract rather than on prose — `docs/error-codes.md`: \"Branch on `code`, never\n * on the message: messages carry context (an id, a path, a limit) and change with it, while a code\n * is the contract.\" Until #380 this rejection was a plain `Error` with neither, and the message it\n * carried embeds both the address and the limit, so matching on it changed with context exactly as\n * that document warns.\n *\n * The distinction it restores is what a retry policy is built on. A peer that did not answer in\n * time is transient and worth retrying; a peer whose handler threw is likely deterministic and\n * retrying repeats the failure. A handler's own error propagates unchanged and is NOT this type —\n * an unattended agent can therefore tell the two apart without reading English.\n *\n * @public\n */\nexport class A2ARequestTimeoutError extends Error {\n readonly code = \"a2a_request_timeout\" as const;\n constructor(\n /** The peer that did not answer. */\n public readonly to: string,\n /** The limit it exceeded, in milliseconds. */\n public readonly timeoutMs: number,\n ) {\n super(`A2A request timeout: ${to} did not respond within ${timeoutMs}ms`);\n this.name = \"A2ARequestTimeoutError\";\n }\n}\n\n/**\n * Per-call knobs for {@link MessageBus.request} / `AgentMailbox.request`.\n */\nexport interface RequestOptions {\n /**\n * How long to wait for the peer handler to settle before rejecting.\n * Default `30_000` (30 s). The rejection is an {@link A2ARequestTimeoutError} carrying\n * `code: \"a2a_request_timeout\"`, `to` and `timeoutMs` (#380), so a caller distinguishes a\n * timeout from a handler failure by branching on the code — a handler's own error propagates\n * unchanged and is not that type.\n */\n timeoutMs?: number;\n}\n\n/**\n * In-process router that delivers A2A messages between agents keyed by string id.\n *\n * Purely in-memory and single-process: nothing is persisted and nothing crosses a\n * process or network boundary. Register one handler per agent id, then `send`\n * (fire-and-forget) or `request` (await the handler's return value).\n *\n * const bus = new MessageBus();\n * bus.register(\"worker\", (m) => `echo:${m.payload}`);\n * const reply = await bus.request(\"boss\", \"worker\", { type: \"ping\", payload: 1 });\n *\n * Callers supply only `{ type, payload }`; the bus stamps `from`, `to`,\n * `timestamp` and `origin` onto the delivered {@link A2AMessage}.\n *\n * How it fails: `send` and `request` both reject with a plain `Error`\n * (`Agent \"<to>\" not registered on MessageBus`) when `to` has no handler, and\n * `request` additionally rejects on timeout. No typed error class is exported for\n * either, so there is nothing to `instanceof`.\n *\n * Traps:\n * - `register` silently OVERWRITES an existing id; the previous handler simply\n * stops receiving anything. Call `has(id)` first when that matters.\n * - `send` is declared `async` but never awaits the handler. A handler that\n * rejects produces an UNHANDLED rejection that `await bus.send(...)` does not\n * observe — only the \"not registered\" failure reaches the caller. Use\n * `request` when the handler's failure has to be visible.\n * - `from` is whatever the caller passes; the bus never checks that the sender\n * is registered. It is self-asserted provenance, not authentication.\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: the SENDER does not wait for the result, which is the whole point of\n // `send`. It does not mean nobody is told when delivery fails — that is the difference\n // between asynchronous and silent (#365).\n //\n // The returned promise used to be dropped on the floor. `MessageHandler` may return one, so a\n // rejecting handler became an unhandled rejection: fatal under Node's default\n // `--unhandled-rejections=throw`, and invisible to the caller, whose `await send(...)`\n // resolved cleanly either way. Attaching a catch keeps the call non-blocking and reports the\n // failure instead of discarding it (`error-handling.md` § 5).\n void Promise.resolve(handler(message)).catch((cause: unknown) => {\n const reason = cause instanceof Error ? cause.message : String(cause);\n diag(`[theokit-sdk] a2a: handler for \"${to}\" failed on a fire-and-forget send: ${reason}\\n`);\n });\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(() => reject(new A2ARequestTimeoutError(to, timeoutMs)), timeoutMs);\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"]}
@@ -7,16 +7,45 @@
7
7
  * @public
8
8
  */
9
9
  import type { MessageHandler } from "./types.js";
10
+ /**
11
+ * A peer did not answer a {@link MessageBus.request} within its timeout.
12
+ *
13
+ * Carries `code: "a2a_request_timeout"`, the peer's address and the limit as FIELDS, so a caller
14
+ * branches on the contract rather than on prose — `docs/error-codes.md`: "Branch on `code`, never
15
+ * on the message: messages carry context (an id, a path, a limit) and change with it, while a code
16
+ * is the contract." Until #380 this rejection was a plain `Error` with neither, and the message it
17
+ * carried embeds both the address and the limit, so matching on it changed with context exactly as
18
+ * that document warns.
19
+ *
20
+ * The distinction it restores is what a retry policy is built on. A peer that did not answer in
21
+ * time is transient and worth retrying; a peer whose handler threw is likely deterministic and
22
+ * retrying repeats the failure. A handler's own error propagates unchanged and is NOT this type —
23
+ * an unattended agent can therefore tell the two apart without reading English.
24
+ *
25
+ * @public
26
+ */
27
+ export declare class A2ARequestTimeoutError extends Error {
28
+ /** The peer that did not answer. */
29
+ readonly to: string;
30
+ /** The limit it exceeded, in milliseconds. */
31
+ readonly timeoutMs: number;
32
+ readonly code: "a2a_request_timeout";
33
+ constructor(
34
+ /** The peer that did not answer. */
35
+ to: string,
36
+ /** The limit it exceeded, in milliseconds. */
37
+ timeoutMs: number);
38
+ }
10
39
  /**
11
40
  * Per-call knobs for {@link MessageBus.request} / `AgentMailbox.request`.
12
41
  */
13
42
  export interface RequestOptions {
14
43
  /**
15
44
  * How long to wait for the peer handler to settle before rejecting.
16
- * Default `30_000` (30 s). The rejection is a plain `Error` reading
17
- * `A2A request timeout: <to> did not respond within <n>ms` there is no typed
18
- * timeout class and no `code` field, so a caller that must distinguish a
19
- * timeout from a handler failure has to match on the message.
45
+ * Default `30_000` (30 s). The rejection is an {@link A2ARequestTimeoutError} carrying
46
+ * `code: "a2a_request_timeout"`, `to` and `timeoutMs` (#380), so a caller distinguishes a
47
+ * timeout from a handler failure by branching on the code a handler's own error propagates
48
+ * unchanged and is not that type.
20
49
  */
21
50
  timeoutMs?: number;
22
51
  }
@@ -7,16 +7,45 @@
7
7
  * @public
8
8
  */
9
9
  import type { MessageHandler } from "./types.js";
10
+ /**
11
+ * A peer did not answer a {@link MessageBus.request} within its timeout.
12
+ *
13
+ * Carries `code: "a2a_request_timeout"`, the peer's address and the limit as FIELDS, so a caller
14
+ * branches on the contract rather than on prose — `docs/error-codes.md`: "Branch on `code`, never
15
+ * on the message: messages carry context (an id, a path, a limit) and change with it, while a code
16
+ * is the contract." Until #380 this rejection was a plain `Error` with neither, and the message it
17
+ * carried embeds both the address and the limit, so matching on it changed with context exactly as
18
+ * that document warns.
19
+ *
20
+ * The distinction it restores is what a retry policy is built on. A peer that did not answer in
21
+ * time is transient and worth retrying; a peer whose handler threw is likely deterministic and
22
+ * retrying repeats the failure. A handler's own error propagates unchanged and is NOT this type —
23
+ * an unattended agent can therefore tell the two apart without reading English.
24
+ *
25
+ * @public
26
+ */
27
+ export declare class A2ARequestTimeoutError extends Error {
28
+ /** The peer that did not answer. */
29
+ readonly to: string;
30
+ /** The limit it exceeded, in milliseconds. */
31
+ readonly timeoutMs: number;
32
+ readonly code: "a2a_request_timeout";
33
+ constructor(
34
+ /** The peer that did not answer. */
35
+ to: string,
36
+ /** The limit it exceeded, in milliseconds. */
37
+ timeoutMs: number);
38
+ }
10
39
  /**
11
40
  * Per-call knobs for {@link MessageBus.request} / `AgentMailbox.request`.
12
41
  */
13
42
  export interface RequestOptions {
14
43
  /**
15
44
  * How long to wait for the peer handler to settle before rejecting.
16
- * Default `30_000` (30 s). The rejection is a plain `Error` reading
17
- * `A2A request timeout: <to> did not respond within <n>ms` there is no typed
18
- * timeout class and no `code` field, so a caller that must distinguish a
19
- * timeout from a handler failure has to match on the message.
45
+ * Default `30_000` (30 s). The rejection is an {@link A2ARequestTimeoutError} carrying
46
+ * `code: "a2a_request_timeout"`, `to` and `timeoutMs` (#380), so a caller distinguishes a
47
+ * timeout from a handler failure by branching on the code a handler's own error propagates
48
+ * unchanged and is not that type.
20
49
  */
21
50
  timeoutMs?: number;
22
51
  }
@@ -1,6 +1,6 @@
1
- export { Agent } from './chunk-I5BE5M5L.js';
1
+ export { Agent } from './chunk-KBXWEWKS.js';
2
2
  import './chunk-OHBIUXKM.js';
3
- import './chunk-DAPSQZT4.js';
3
+ import './chunk-ODTZ7HX3.js';
4
4
  import './chunk-YMA4S2WO.js';
5
5
  import './chunk-2SFBB54R.js';
6
6
  import './chunk-SUKXXLWD.js';
@@ -14,7 +14,8 @@ import './chunk-WTMU7J4U.js';
14
14
  import './chunk-5JLFFPCH.js';
15
15
  import './chunk-BMAZLQQ4.js';
16
16
  import './chunk-HKCKOAIO.js';
17
- import './chunk-5PHVENFV.js';
17
+ import './chunk-7FHZ4VQX.js';
18
+ import './chunk-KVNWIAO4.js';
18
19
  import './chunk-UPRJR6IP.js';
19
20
  import './chunk-FD2UT76F.js';
20
21
  import './chunk-4VPXM6UU.js';
@@ -48,5 +49,5 @@ import './chunk-3OR54XG4.js';
48
49
  import './chunk-IDCKSLYH.js';
49
50
  import './chunk-T7XEKOVW.js';
50
51
  import './chunk-T7O6K6PX.js';
51
- //# sourceMappingURL=agent-HDEVVUBS.js.map
52
- //# sourceMappingURL=agent-HDEVVUBS.js.map
52
+ //# sourceMappingURL=agent-K3PQBS7D.js.map
53
+ //# sourceMappingURL=agent-K3PQBS7D.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","file":"agent-HDEVVUBS.js"}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"agent-K3PQBS7D.js"}
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var chunkF5WMX4EA_cjs = require('./chunk-F5WMX4EA.cjs');
3
+ var chunkLQYQHXIW_cjs = require('./chunk-LQYQHXIW.cjs');
4
4
  require('./chunk-V2UUGUY4.cjs');
5
- require('./chunk-5UOVYM3P.cjs');
5
+ require('./chunk-6VLKVX7O.cjs');
6
6
  require('./chunk-53CBTBWO.cjs');
7
7
  require('./chunk-BV2MWEMV.cjs');
8
8
  require('./chunk-NGESVVJN.cjs');
@@ -16,7 +16,8 @@ require('./chunk-4OTIXDMU.cjs');
16
16
  require('./chunk-DQKERTND.cjs');
17
17
  require('./chunk-CTCGWIUD.cjs');
18
18
  require('./chunk-2ZPEDVLM.cjs');
19
- require('./chunk-Q47R5E2X.cjs');
19
+ require('./chunk-E3WZ6Y4H.cjs');
20
+ require('./chunk-KRD3GQAA.cjs');
20
21
  require('./chunk-U2AC6JUP.cjs');
21
22
  require('./chunk-MUUQ2WFJ.cjs');
22
23
  require('./chunk-WJVHMTKB.cjs');
@@ -55,7 +56,7 @@ require('./chunk-NUKRL3I6.cjs');
55
56
 
56
57
  Object.defineProperty(exports, "Agent", {
57
58
  enumerable: true,
58
- get: function () { return chunkF5WMX4EA_cjs.Agent; }
59
+ get: function () { return chunkLQYQHXIW_cjs.Agent; }
59
60
  });
60
- //# sourceMappingURL=agent-R2HOJFZJ.cjs.map
61
- //# sourceMappingURL=agent-R2HOJFZJ.cjs.map
61
+ //# sourceMappingURL=agent-PJMBK7HQ.cjs.map
62
+ //# sourceMappingURL=agent-PJMBK7HQ.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","file":"agent-R2HOJFZJ.cjs"}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"agent-PJMBK7HQ.cjs"}