@animalabs/agent-framework 0.11.0 → 0.13.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 (160) hide show
  1. package/dist/src/agent.d.ts +11 -1
  2. package/dist/src/agent.d.ts.map +1 -1
  3. package/dist/src/agent.js +60 -4
  4. package/dist/src/agent.js.map +1 -1
  5. package/dist/src/framework.d.ts +72 -1
  6. package/dist/src/framework.d.ts.map +1 -1
  7. package/dist/src/framework.js +873 -123
  8. package/dist/src/framework.js.map +1 -1
  9. package/dist/src/gate/event-gate.d.ts +7 -1
  10. package/dist/src/gate/event-gate.d.ts.map +1 -1
  11. package/dist/src/gate/event-gate.js +15 -1
  12. package/dist/src/gate/event-gate.js.map +1 -1
  13. package/dist/src/kv-unified-wire.d.ts +16 -0
  14. package/dist/src/kv-unified-wire.d.ts.map +1 -0
  15. package/dist/src/kv-unified-wire.js +2 -0
  16. package/dist/src/kv-unified-wire.js.map +1 -0
  17. package/dist/src/mcpl/channel-registry.d.ts +84 -1
  18. package/dist/src/mcpl/channel-registry.d.ts.map +1 -1
  19. package/dist/src/mcpl/channel-registry.js +188 -5
  20. package/dist/src/mcpl/channel-registry.js.map +1 -1
  21. package/dist/src/mcpl/hook-orchestrator.d.ts.map +1 -1
  22. package/dist/src/mcpl/hook-orchestrator.js +28 -2
  23. package/dist/src/mcpl/hook-orchestrator.js.map +1 -1
  24. package/dist/src/mcpl/push-handler.d.ts +6 -1
  25. package/dist/src/mcpl/push-handler.d.ts.map +1 -1
  26. package/dist/src/mcpl/push-handler.js +29 -2
  27. package/dist/src/mcpl/push-handler.js.map +1 -1
  28. package/dist/src/mcpl/reference-fetcher.d.ts +59 -0
  29. package/dist/src/mcpl/reference-fetcher.d.ts.map +1 -0
  30. package/dist/src/mcpl/reference-fetcher.js +240 -0
  31. package/dist/src/mcpl/reference-fetcher.js.map +1 -0
  32. package/dist/src/mcpl/references.d.ts +125 -0
  33. package/dist/src/mcpl/references.d.ts.map +1 -0
  34. package/dist/src/mcpl/references.js +262 -0
  35. package/dist/src/mcpl/references.js.map +1 -0
  36. package/dist/src/mcpl/types.d.ts +32 -0
  37. package/dist/src/mcpl/types.d.ts.map +1 -1
  38. package/dist/src/mcpl/types.js.map +1 -1
  39. package/dist/src/modules/index.d.ts +2 -1
  40. package/dist/src/modules/index.d.ts.map +1 -1
  41. package/dist/src/modules/index.js +1 -1
  42. package/dist/src/modules/index.js.map +1 -1
  43. package/dist/src/modules/workspace/index.d.ts +125 -2
  44. package/dist/src/modules/workspace/index.d.ts.map +1 -1
  45. package/dist/src/modules/workspace/index.js +354 -58
  46. package/dist/src/modules/workspace/index.js.map +1 -1
  47. package/dist/src/modules/workspace/types.d.ts +7 -0
  48. package/dist/src/modules/workspace/types.d.ts.map +1 -1
  49. package/dist/src/modules/workspace/types.js.map +1 -1
  50. package/dist/src/tool-result-history.d.ts.map +1 -1
  51. package/dist/src/tool-result-history.js +14 -0
  52. package/dist/src/tool-result-history.js.map +1 -1
  53. package/dist/src/tool-wrapper-prose-guard.d.ts +8 -0
  54. package/dist/src/tool-wrapper-prose-guard.d.ts.map +1 -0
  55. package/dist/src/tool-wrapper-prose-guard.js +37 -0
  56. package/dist/src/tool-wrapper-prose-guard.js.map +1 -0
  57. package/dist/src/tune-out/coordinator.d.ts +144 -0
  58. package/dist/src/tune-out/coordinator.d.ts.map +1 -0
  59. package/dist/src/tune-out/coordinator.js +444 -0
  60. package/dist/src/tune-out/coordinator.js.map +1 -0
  61. package/dist/src/tune-out/tools.d.ts +43 -0
  62. package/dist/src/tune-out/tools.d.ts.map +1 -0
  63. package/dist/src/tune-out/tools.js +93 -0
  64. package/dist/src/tune-out/tools.js.map +1 -0
  65. package/dist/src/types/agent.d.ts +15 -0
  66. package/dist/src/types/agent.d.ts.map +1 -1
  67. package/dist/src/types/framework.d.ts +6 -0
  68. package/dist/src/types/framework.d.ts.map +1 -1
  69. package/dist/src/usage-accounting.d.ts +5 -0
  70. package/dist/src/usage-accounting.d.ts.map +1 -0
  71. package/dist/src/usage-accounting.js +11 -0
  72. package/dist/src/usage-accounting.js.map +1 -0
  73. package/dist/test/channel-incoming-targeting.test.d.ts +2 -0
  74. package/dist/test/channel-incoming-targeting.test.d.ts.map +1 -0
  75. package/dist/test/channel-incoming-targeting.test.js +81 -0
  76. package/dist/test/channel-incoming-targeting.test.js.map +1 -0
  77. package/dist/test/conversation-routing.test.js +39 -0
  78. package/dist/test/conversation-routing.test.js.map +1 -1
  79. package/dist/test/kv-unified-wiring.test.d.ts +2 -0
  80. package/dist/test/kv-unified-wiring.test.d.ts.map +1 -0
  81. package/dist/test/kv-unified-wiring.test.js +56 -0
  82. package/dist/test/kv-unified-wiring.test.js.map +1 -0
  83. package/dist/test/mcpl-push-convert.test.d.ts +2 -0
  84. package/dist/test/mcpl-push-convert.test.d.ts.map +1 -0
  85. package/dist/test/mcpl-push-convert.test.js +42 -0
  86. package/dist/test/mcpl-push-convert.test.js.map +1 -0
  87. package/dist/test/mcpl-reference-fetcher.test.d.ts +2 -0
  88. package/dist/test/mcpl-reference-fetcher.test.d.ts.map +1 -0
  89. package/dist/test/mcpl-reference-fetcher.test.js +199 -0
  90. package/dist/test/mcpl-reference-fetcher.test.js.map +1 -0
  91. package/dist/test/per-agent-delivery.test.d.ts +2 -0
  92. package/dist/test/per-agent-delivery.test.d.ts.map +1 -0
  93. package/dist/test/per-agent-delivery.test.js +108 -0
  94. package/dist/test/per-agent-delivery.test.js.map +1 -0
  95. package/dist/test/present-while-acting.test.js +64 -0
  96. package/dist/test/present-while-acting.test.js.map +1 -1
  97. package/dist/test/tool-result-history.test.js +78 -0
  98. package/dist/test/tool-result-history.test.js.map +1 -1
  99. package/dist/test/tool-wrapper-prose-guard-integration.test.d.ts +2 -0
  100. package/dist/test/tool-wrapper-prose-guard-integration.test.d.ts.map +1 -0
  101. package/dist/test/tool-wrapper-prose-guard-integration.test.js +485 -0
  102. package/dist/test/tool-wrapper-prose-guard-integration.test.js.map +1 -0
  103. package/dist/test/tool-wrapper-prose-guard.test.d.ts +2 -0
  104. package/dist/test/tool-wrapper-prose-guard.test.d.ts.map +1 -0
  105. package/dist/test/tool-wrapper-prose-guard.test.js +34 -0
  106. package/dist/test/tool-wrapper-prose-guard.test.js.map +1 -0
  107. package/dist/test/tune-out-e2e.test.d.ts +2 -0
  108. package/dist/test/tune-out-e2e.test.d.ts.map +1 -0
  109. package/dist/test/tune-out-e2e.test.js +142 -0
  110. package/dist/test/tune-out-e2e.test.js.map +1 -0
  111. package/dist/test/tune-out-gate-composition.test.d.ts +2 -0
  112. package/dist/test/tune-out-gate-composition.test.d.ts.map +1 -0
  113. package/dist/test/tune-out-gate-composition.test.js +221 -0
  114. package/dist/test/tune-out-gate-composition.test.js.map +1 -0
  115. package/dist/test/tune-out-lifecycle.test.d.ts +2 -0
  116. package/dist/test/tune-out-lifecycle.test.d.ts.map +1 -0
  117. package/dist/test/tune-out-lifecycle.test.js +121 -0
  118. package/dist/test/tune-out-lifecycle.test.js.map +1 -0
  119. package/dist/test/turn-trigger-provenance.test.d.ts +2 -0
  120. package/dist/test/turn-trigger-provenance.test.d.ts.map +1 -0
  121. package/dist/test/turn-trigger-provenance.test.js +85 -0
  122. package/dist/test/turn-trigger-provenance.test.js.map +1 -0
  123. package/dist/test/usage-accounting.test.d.ts +2 -0
  124. package/dist/test/usage-accounting.test.d.ts.map +1 -0
  125. package/dist/test/usage-accounting.test.js +13 -0
  126. package/dist/test/usage-accounting.test.js.map +1 -0
  127. package/dist/test/workspace-branch-continuation.test.d.ts +15 -0
  128. package/dist/test/workspace-branch-continuation.test.d.ts.map +1 -0
  129. package/dist/test/workspace-branch-continuation.test.js +183 -0
  130. package/dist/test/workspace-branch-continuation.test.js.map +1 -0
  131. package/dist/test/workspace-read-file-from-disk.test.d.ts +17 -0
  132. package/dist/test/workspace-read-file-from-disk.test.d.ts.map +1 -0
  133. package/dist/test/workspace-read-file-from-disk.test.js +174 -0
  134. package/dist/test/workspace-read-file-from-disk.test.js.map +1 -0
  135. package/dist/test/workspace-read-image.test.js +8 -0
  136. package/dist/test/workspace-read-image.test.js.map +1 -1
  137. package/dist/test/workspace-restore-order.test.js +1 -1
  138. package/dist/test/workspace-restore-order.test.js.map +1 -1
  139. package/dist/tsconfig.tsbuildinfo +1 -1
  140. package/package.json +2 -2
  141. package/src/agent.ts +77 -4
  142. package/src/framework.ts +953 -128
  143. package/src/gate/event-gate.ts +28 -3
  144. package/src/kv-unified-wire.ts +12 -0
  145. package/src/mcpl/channel-registry.ts +264 -8
  146. package/src/mcpl/hook-orchestrator.ts +29 -2
  147. package/src/mcpl/push-handler.ts +30 -2
  148. package/src/mcpl/reference-fetcher.ts +240 -0
  149. package/src/mcpl/references.ts +292 -0
  150. package/src/mcpl/types.ts +29 -0
  151. package/src/modules/index.ts +2 -1
  152. package/src/modules/workspace/index.ts +425 -66
  153. package/src/modules/workspace/types.ts +7 -0
  154. package/src/tool-result-history.ts +11 -0
  155. package/src/tool-wrapper-prose-guard.ts +38 -0
  156. package/src/tune-out/coordinator.ts +548 -0
  157. package/src/tune-out/tools.ts +129 -0
  158. package/src/types/agent.ts +16 -0
  159. package/src/types/framework.ts +7 -0
  160. package/src/usage-accounting.ts +8 -0
@@ -6,6 +6,7 @@
6
6
  *
7
7
  * Spec reference: Section 9 (Push Events).
8
8
  */
9
+ import { INLINE_WITHHELD_TEXT, isInlineContradiction, referenceStubOrNull } from './references.js';
9
10
  import { McplFeatureSetError } from './feature-set-manager.js';
10
11
  import { expandCoreTags } from './tags.js';
11
12
  // ============================================================================
@@ -15,11 +16,21 @@ import { expandCoreTags } from './tags.js';
15
16
  * Convert a single MCPL wire-format content block to a membrane ContentBlock.
16
17
  * Same logic as hook-orchestrator.ts.
17
18
  */
18
- function convertBlock(block) {
19
+ export function convertBlock(block) {
19
20
  switch (block.type) {
20
21
  case 'text':
21
22
  return { type: 'text', text: block.text };
22
23
  case 'image':
24
+ if (isInlineContradiction(block)) {
25
+ // RFC-005 vector 2: inline data claiming bulk disposition — fail
26
+ // closed, withhold the data (checked BEFORE the data branch).
27
+ return { type: 'text', text: INLINE_WITHHELD_TEXT };
28
+ }
29
+ if (block.uri && block.disposition) {
30
+ // RFC-005 uri-form media with a disposition claim: stub, do not
31
+ // hand the URI to the provider or inline it.
32
+ return { type: 'text', text: referenceStubOrNull(block, 'attachment on push event') ?? '[reference]' };
33
+ }
23
34
  if (block.data && block.mimeType) {
24
35
  return {
25
36
  type: 'image',
@@ -34,6 +45,16 @@ function convertBlock(block) {
34
45
  }
35
46
  return { type: 'text', text: '[Image: no data]' };
36
47
  case 'audio':
48
+ if (isInlineContradiction(block)) {
49
+ // RFC-005 vector 2: inline data claiming bulk disposition — fail
50
+ // closed, withhold the data (checked BEFORE the data branch).
51
+ return { type: 'text', text: INLINE_WITHHELD_TEXT };
52
+ }
53
+ if (block.uri && block.disposition) {
54
+ // RFC-005 uri-form media with a disposition claim: stub, do not
55
+ // hand the URI to the provider or inline it.
56
+ return { type: 'text', text: referenceStubOrNull(block, 'attachment on push event') ?? '[reference]' };
57
+ }
37
58
  if (block.data && block.mimeType) {
38
59
  return {
39
60
  type: 'audio',
@@ -42,7 +63,13 @@ function convertBlock(block) {
42
63
  }
43
64
  return { type: 'text', text: '[Audio: no data]' };
44
65
  case 'resource':
45
- return { type: 'text', text: `[Resource: ${block.uri}]` };
66
+ // RFC-005: reference blocks become bounded stubs — never raw URIs
67
+ // (a signed URL is a bearer credential that looks like a location).
68
+ return { type: 'text', text: referenceStubOrNull(block, 'attachment on push event') ?? '[reference]' };
69
+ default:
70
+ // Unknown wire block types previously fell off the exhaustive switch
71
+ // and propagated `undefined` into ContentBlock[]. Fail visibly.
72
+ return { type: 'text', text: `[unrecognized content block: ${block.type ?? 'untyped'}]` };
46
73
  }
47
74
  }
48
75
  // ============================================================================
@@ -1 +1 @@
1
- {"version":3,"file":"push-handler.js","sourceRoot":"","sources":["../../../src/mcpl/push-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;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AAC;gBACxB,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAC9C,CAAC;YACF,gBAAgB,GAAG,IAAI,CAAC,sBAAsB,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;QACxE,CAAC;QAED,0BAA0B;QAC1B,MAAM,WAAW,GAAG,GAAG,QAAQ,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;QAE3F,yBAAyB;QACzB,MAAM,SAAS,GAAkB;YAC/B,IAAI,EAAE,iBAAiB;YACvB,QAAQ;YACR,UAAU,EAAE,MAAM,CAAC,UAAU;YAC7B,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,OAAO;YACP,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,SAAS,EAAE,MAAM,CAAC,SAAS;YAC3B,WAAW;YACX,gBAAgB;SACjB,CAAC;QACF,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;QAE5B,gBAAgB;QAChB,IAAI,CAAC,WAAW,CAAC;YACf,IAAI,EAAE,iBAAiB;YACvB,QAAQ;YACR,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,UAAU,EAAE,MAAM,CAAC,UAAU;SAC9B,CAAC,CAAC;QAEH,aAAa;QACb,SAAS,EAAE,OAAO,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;IACtD,CAAC;CACF"}
@@ -0,0 +1,59 @@
1
+ import type { ReferenceRecord } from './references.js';
2
+ /** Per-fetch hard ceiling on actual bytes (overridable per server / call). */
3
+ export declare const DEFAULT_FETCH_MAX_BYTES: number;
4
+ /** Eager (autofetch) threshold: at most what could have shipped inline. */
5
+ export declare const DEFAULT_EAGER_FETCH_MAX_BYTES: number;
6
+ /** Cumulative per-server budget for this process. */
7
+ export declare const DEFAULT_MAX_TOTAL_BYTES: number;
8
+ declare const EAGER_FETCH_TIMEOUT_MS = 10000;
9
+ export interface ResolvedServerBinding {
10
+ /** The url the connection was dialed to (ws/wss/http/https). */
11
+ url: string;
12
+ /** Connection credential, applied host-side as Authorization: Bearer. */
13
+ token?: string;
14
+ autofetch?: {
15
+ maxBytes?: number;
16
+ maxTotalBytes?: number;
17
+ };
18
+ }
19
+ export interface FetchOutcome {
20
+ ok: boolean;
21
+ /** Mount-prefixed workspace path of the saved payload (on success). */
22
+ path?: string;
23
+ bytes?: number;
24
+ mimeType?: string;
25
+ digestVerified?: boolean;
26
+ error?: string;
27
+ }
28
+ /** Magic-byte sniff for the formats this pipeline actually moves. The
29
+ * header is testimony too (vector 13): sniffed type wins for the storage
30
+ * extension and the record's verified type; header, then testimony, are
31
+ * fallbacks for containers the sniffer doesn't know. */
32
+ export declare function sniffMime(data: Buffer): string | null;
33
+ /** ws→http / wss→https with default ports elided, for origin comparison. */
34
+ export declare function normalizedOrigin(url: string): string | null;
35
+ export declare class ReferenceFetcher {
36
+ private readonly resolveServer;
37
+ /** Persist the payload; returns a mount-prefixed path or null when no
38
+ * writable workspace exists. File name is host-generated (§7.3). */
39
+ private readonly saveFile;
40
+ private totals;
41
+ private inFlight;
42
+ constructor(resolveServer: (serverId: string) => ResolvedServerBinding | undefined,
43
+ /** Persist the payload; returns a mount-prefixed path or null when no
44
+ * writable workspace exists. File name is host-generated (§7.3). */
45
+ saveFile: (fileName: string, data: Buffer, mimeType: string) => Promise<string | null>);
46
+ /** Would the eager (autofetch) default fetch this record? */
47
+ isEagerEligible(record: ReferenceRecord): boolean;
48
+ /** §6.1/§7.1 policy check; returns a refusal reason or null. */
49
+ private originCheck;
50
+ /** Fetch a reference to workspace storage. Idempotent per record; concurrent
51
+ * callers share one in-flight fetch. */
52
+ fetch(record: ReferenceRecord, opts?: {
53
+ maxBytes?: number;
54
+ timeoutMs?: number;
55
+ }): Promise<FetchOutcome>;
56
+ private doFetch;
57
+ }
58
+ export { EAGER_FETCH_TIMEOUT_MS };
59
+ //# sourceMappingURL=reference-fetcher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reference-fetcher.d.ts","sourceRoot":"","sources":["../../../src/mcpl/reference-fetcher.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGvD,8EAA8E;AAC9E,eAAO,MAAM,uBAAuB,QAAoB,CAAC;AACzD,2EAA2E;AAC3E,eAAO,MAAM,6BAA6B,QAAa,CAAC;AACxD,qDAAqD;AACrD,eAAO,MAAM,uBAAuB,QAAqB,CAAC;AAE1D,QAAA,MAAM,sBAAsB,QAAS,CAAC;AAEtC,MAAM,WAAW,qBAAqB;IACpC,gEAAgE;IAChE,GAAG,EAAE,MAAM,CAAC;IACZ,yEAAyE;IACzE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC3D;AAED,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,OAAO,CAAC;IACZ,uEAAuE;IACvE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;yDAGyD;AACzD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAerD;AAUD,4EAA4E;AAC5E,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAU3D;AAED,qBAAa,gBAAgB;IAKzB,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B;yEACqE;IACrE,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAP3B,OAAO,CAAC,MAAM,CAA6B;IAC3C,OAAO,CAAC,QAAQ,CAA4C;gBAGzC,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,qBAAqB,GAAG,SAAS;IACvF;yEACqE;IACpD,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAGzG,6DAA6D;IAC7D,eAAe,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO;IAUjD,gEAAgE;IAChE,OAAO,CAAC,WAAW;IAgBnB;6CACyC;IACzC,KAAK,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,YAAY,CAAC;YAYzF,OAAO;CAkFtB;AAED,OAAO,EAAE,sBAAsB,EAAE,CAAC"}
@@ -0,0 +1,240 @@
1
+ /**
2
+ * RFC-005 host-mediated reference fetcher (§6, §7) — the only code that ever
3
+ * dereferences a reference, and it fails closed on every axis the RFC names:
4
+ *
5
+ * - **origin binding (§6.1, revision 3):** authenticated fetches go only to
6
+ * the origin the reference's server connection was dialed to. This v1 does
7
+ * not fetch third-party origins at all (deferred with declared reference
8
+ * origins); the connection credential is applied as an Authorization
9
+ * header by this code alone and never surfaces to model, script, or URI.
10
+ * - **scheme allowlist (§7.1, vector 11):** https; http only when the dialed
11
+ * origin itself is plaintext (a ws:// server references its own origin).
12
+ * - **no redirect traversal (§7.2, vector 12):** any 3xx refuses.
13
+ * - **actual-byte ceiling (§7.3 list, vector 10):** streamed with abort the
14
+ * moment real octets exceed the ceiling, regardless of claimed sizeBytes.
15
+ * A claim already over the ceiling refuses without dialing (vector 4).
16
+ * - **digest verification (§7.4 list, vector 5):** sha256 computed while
17
+ * streaming; a mismatch is discarded, never presented.
18
+ * - **expiry (§7.4, vector 16):** expired testimony refuses locally.
19
+ * - **storage naming (§7.3):** the file name is host-generated from the
20
+ * reference id + verified type; the server's `name` is display-only.
21
+ *
22
+ * Autofetch policy (host default, not protocol): eager only below
23
+ * DEFAULT_EAGER_FETCH_MAX_BYTES from the connection origin — anything under
24
+ * that threshold the server could legitimately have inlined, so eager
25
+ * materialization is never worse than the status quo. Everything larger is
26
+ * fetched on demand (the fetch_reference tool, or the code-execution result
27
+ * hook, where a script receiving a result is the strongest available signal
28
+ * that the bytes are wanted). Per-server budgets cap cumulative appetite.
29
+ */
30
+ import { createHash } from 'node:crypto';
31
+ import { isReferenceExpired } from './references.js';
32
+ /** Per-fetch hard ceiling on actual bytes (overridable per server / call). */
33
+ export const DEFAULT_FETCH_MAX_BYTES = 256 * 1024 * 1024;
34
+ /** Eager (autofetch) threshold: at most what could have shipped inline. */
35
+ export const DEFAULT_EAGER_FETCH_MAX_BYTES = 256 * 1024;
36
+ /** Cumulative per-server budget for this process. */
37
+ export const DEFAULT_MAX_TOTAL_BYTES = 1024 * 1024 * 1024;
38
+ const FETCH_TIMEOUT_MS = 60_000;
39
+ const EAGER_FETCH_TIMEOUT_MS = 10_000;
40
+ /** Magic-byte sniff for the formats this pipeline actually moves. The
41
+ * header is testimony too (vector 13): sniffed type wins for the storage
42
+ * extension and the record's verified type; header, then testimony, are
43
+ * fallbacks for containers the sniffer doesn't know. */
44
+ export function sniffMime(data) {
45
+ if (data.length < 12)
46
+ return null;
47
+ if (data.subarray(0, 4).toString('latin1') === 'RIFF' && data.subarray(8, 12).toString('latin1') === 'WAVE')
48
+ return 'audio/wav';
49
+ if (data.subarray(0, 4).toString('latin1') === 'RIFF')
50
+ return 'audio/wav';
51
+ if (data.subarray(0, 4).toString('latin1') === 'fLaC')
52
+ return 'audio/flac';
53
+ if (data.subarray(0, 4).toString('latin1') === 'OggS')
54
+ return 'audio/ogg';
55
+ if (data.subarray(0, 4).toString('latin1') === 'MThd')
56
+ return 'audio/midi';
57
+ if (data.subarray(0, 3).toString('latin1') === 'ID3' || (data[0] === 0xff && (data[1] & 0xe0) === 0xe0))
58
+ return 'audio/mpeg';
59
+ if (data[0] === 0x89 && data.subarray(1, 4).toString('latin1') === 'PNG')
60
+ return 'image/png';
61
+ if (data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff)
62
+ return 'image/jpeg';
63
+ if (data.subarray(0, 6).toString('latin1') === 'GIF87a' || data.subarray(0, 6).toString('latin1') === 'GIF89a')
64
+ return 'image/gif';
65
+ if (data.subarray(4, 8).toString('latin1') === 'ftyp')
66
+ return 'video/mp4';
67
+ if (data.subarray(0, 5).toString('latin1') === '%PDF-')
68
+ return 'application/pdf';
69
+ if (data[0] === 0x50 && data[1] === 0x4b)
70
+ return 'application/zip';
71
+ return null;
72
+ }
73
+ const EXT_BY_MIME = {
74
+ 'audio/wav': 'wav', 'audio/x-wav': 'wav', 'audio/mpeg': 'mp3', 'audio/mp4': 'm4a',
75
+ 'audio/flac': 'flac', 'audio/midi': 'mid', 'audio/ogg': 'ogg',
76
+ 'image/png': 'png', 'image/jpeg': 'jpg', 'image/gif': 'gif', 'image/webp': 'webp',
77
+ 'video/mp4': 'mp4', 'application/json': 'json', 'text/plain': 'txt',
78
+ 'application/pdf': 'pdf', 'application/zip': 'zip',
79
+ };
80
+ /** ws→http / wss→https with default ports elided, for origin comparison. */
81
+ export function normalizedOrigin(url) {
82
+ try {
83
+ const u = new URL(url);
84
+ const scheme = u.protocol === 'ws:' ? 'http:' : u.protocol === 'wss:' ? 'https:' : u.protocol;
85
+ const defaultPort = scheme === 'https:' ? '443' : '80';
86
+ const port = u.port === '' || u.port === defaultPort ? '' : `:${u.port}`;
87
+ return `${scheme}//${u.hostname.toLowerCase()}${port}`;
88
+ }
89
+ catch {
90
+ return null;
91
+ }
92
+ }
93
+ export class ReferenceFetcher {
94
+ resolveServer;
95
+ saveFile;
96
+ totals = new Map();
97
+ inFlight = new Map();
98
+ constructor(resolveServer,
99
+ /** Persist the payload; returns a mount-prefixed path or null when no
100
+ * writable workspace exists. File name is host-generated (§7.3). */
101
+ saveFile) {
102
+ this.resolveServer = resolveServer;
103
+ this.saveFile = saveFile;
104
+ }
105
+ /** Would the eager (autofetch) default fetch this record? */
106
+ isEagerEligible(record) {
107
+ if (record.fetchedPath)
108
+ return false;
109
+ if (record.serverId === undefined)
110
+ return false;
111
+ const binding = this.resolveServer(record.serverId);
112
+ if (!binding)
113
+ return false;
114
+ const cap = binding.autofetch?.maxBytes ?? DEFAULT_EAGER_FETCH_MAX_BYTES;
115
+ if (record.testimony.sizeBytes === undefined || record.testimony.sizeBytes > cap)
116
+ return false;
117
+ return this.originCheck(record, binding) === null;
118
+ }
119
+ /** §6.1/§7.1 policy check; returns a refusal reason or null. */
120
+ originCheck(record, binding) {
121
+ const refOrigin = normalizedOrigin(record.testimony.uri);
122
+ const dialedOrigin = normalizedOrigin(binding.url);
123
+ if (!refOrigin)
124
+ return 'reference uri does not parse';
125
+ if (!dialedOrigin)
126
+ return 'server url does not parse';
127
+ const scheme = refOrigin.split('//')[0];
128
+ if (scheme !== 'https:' && scheme !== 'http:')
129
+ return `scheme not allowed: ${scheme}`;
130
+ if (refOrigin !== dialedOrigin) {
131
+ return `third-party origin (${refOrigin} != dialed ${dialedOrigin}); not fetched by this host`;
132
+ }
133
+ if (scheme === 'http:' && dialedOrigin.startsWith('https:')) {
134
+ return 'plaintext reference from a secure origin';
135
+ }
136
+ return null;
137
+ }
138
+ /** Fetch a reference to workspace storage. Idempotent per record; concurrent
139
+ * callers share one in-flight fetch. */
140
+ fetch(record, opts) {
141
+ if (record.fetchedPath) {
142
+ return Promise.resolve({ ok: true, path: record.fetchedPath, bytes: record.verifiedBytes,
143
+ mimeType: record.verifiedMimeType, digestVerified: record.digestVerified });
144
+ }
145
+ const existing = this.inFlight.get(record.refId);
146
+ if (existing)
147
+ return existing;
148
+ const p = this.doFetch(record, opts).finally(() => this.inFlight.delete(record.refId));
149
+ this.inFlight.set(record.refId, p);
150
+ return p;
151
+ }
152
+ async doFetch(record, opts) {
153
+ const t = record.testimony;
154
+ if (record.serverId === undefined)
155
+ return { ok: false, error: 'reference has no server binding' };
156
+ const binding = this.resolveServer(record.serverId);
157
+ if (!binding)
158
+ return { ok: false, error: `unknown server: ${record.serverId}` };
159
+ const refusal = this.originCheck(record, binding);
160
+ if (refusal)
161
+ return { ok: false, error: refusal };
162
+ if (isReferenceExpired(t))
163
+ return { ok: false, error: 'reference expired (or unparseable expiry — fail closed)' };
164
+ const ceiling = opts?.maxBytes ?? DEFAULT_FETCH_MAX_BYTES;
165
+ // Vector 4: a claim already over the ceiling refuses without dialing.
166
+ if (t.sizeBytes !== undefined && t.sizeBytes > ceiling) {
167
+ return { ok: false, error: `claimed ${t.sizeBytes} bytes exceeds fetch ceiling ${ceiling}` };
168
+ }
169
+ const budget = binding.autofetch?.maxTotalBytes ?? DEFAULT_MAX_TOTAL_BYTES;
170
+ const used = this.totals.get(record.serverId) ?? 0;
171
+ if (used >= budget) {
172
+ return { ok: false, error: `per-server fetch budget exhausted (${used}/${budget} bytes)` };
173
+ }
174
+ const controller = new AbortController();
175
+ const timer = setTimeout(() => controller.abort(), opts?.timeoutMs ?? FETCH_TIMEOUT_MS);
176
+ try {
177
+ const headers = {};
178
+ if (binding.token)
179
+ headers.authorization = `Bearer ${binding.token}`;
180
+ const resp = await fetch(t.uri, { headers, redirect: 'manual', signal: controller.signal });
181
+ if (resp.status >= 300 && resp.status < 400) {
182
+ return { ok: false, error: `redirect refused (HTTP ${resp.status}) — no redirect traversal` };
183
+ }
184
+ if (!resp.ok)
185
+ return { ok: false, error: `HTTP ${resp.status}` };
186
+ if (!resp.body)
187
+ return { ok: false, error: 'no response body' };
188
+ const hash = createHash('sha256');
189
+ const chunks = [];
190
+ let received = 0;
191
+ const reader = resp.body.getReader();
192
+ for (;;) {
193
+ const { done, value } = await reader.read();
194
+ if (done)
195
+ break;
196
+ received += value.byteLength;
197
+ if (received > ceiling || received > budget - used) {
198
+ controller.abort();
199
+ const which = received > ceiling ? 'ceiling' : 'per-server budget';
200
+ return { ok: false, error: `aborted: actual bytes exceeded ${which} at ${received}` };
201
+ }
202
+ const buf = Buffer.from(value);
203
+ hash.update(buf);
204
+ chunks.push(buf);
205
+ }
206
+ const data = Buffer.concat(chunks);
207
+ let digestVerified;
208
+ if (t.digest) {
209
+ const got = 'sha256:' + hash.digest('base64url');
210
+ if (got !== t.digest) {
211
+ // Vector 5: never presented as the described content.
212
+ return { ok: false, error: `digest mismatch (claimed ${t.digest.slice(0, 20)}…, got ${got.slice(0, 20)}…)` };
213
+ }
214
+ digestVerified = true;
215
+ }
216
+ const headerMime = resp.headers.get('content-type')?.split(';')[0].trim() || null;
217
+ const observedMime = sniffMime(data) ?? headerMime ?? t.mimeType ?? 'application/octet-stream';
218
+ const ext = EXT_BY_MIME[observedMime] ?? EXT_BY_MIME[headerMime ?? ''] ?? EXT_BY_MIME[t.mimeType ?? ''] ?? 'bin';
219
+ // §7.3: host-generated storage name; the server's `name` is display-only.
220
+ const path = await this.saveFile(`${record.refId}.${ext}`, data, observedMime);
221
+ if (path === null)
222
+ return { ok: false, error: 'no writable workspace mount to store the payload' };
223
+ this.totals.set(record.serverId, used + data.length);
224
+ record.fetchedPath = path;
225
+ record.verifiedBytes = data.length;
226
+ record.verifiedMimeType = observedMime;
227
+ record.digestVerified = digestVerified;
228
+ return { ok: true, path, bytes: data.length, mimeType: observedMime, digestVerified };
229
+ }
230
+ catch (e) {
231
+ const msg = e.name === 'AbortError' ? 'fetch timed out or aborted' : e.message;
232
+ return { ok: false, error: msg };
233
+ }
234
+ finally {
235
+ clearTimeout(timer);
236
+ }
237
+ }
238
+ }
239
+ export { EAGER_FETCH_TIMEOUT_MS };
240
+ //# sourceMappingURL=reference-fetcher.js.map
@@ -0,0 +1 @@
1
+ {"version":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W,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,4BAA4B,CAAC,CAAC,CAAE,CAAW,CAAC,OAAO,CAAC;YACrG,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;QACnC,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;CACF;AAED,OAAO,EAAE,sBAAsB,EAAE,CAAC"}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * RFC-005 bulk content references — host treatment (agent-framework side).
3
+ *
4
+ * Mirrors the pure module in mcpl-core-ts `src/references.ts` (this framework
5
+ * deliberately does not depend on that package; hand-maintained MCPL types
6
+ * are the house convention — see types.ts header). Semantics are the RFC's,
7
+ * revision 3:
8
+ *
9
+ * - every parsed field is server *testimony*, never a fact (§3);
10
+ * - the one invalid-field rule (§8): a bad optional field is dropped while
11
+ * the block and its subtractive `disposition` survive; a bad `uri`
12
+ * rejects the block whole;
13
+ * - `disposition:"never"` withholds payload AND uri from model context
14
+ * unconditionally — enforced in code here, not by caller discipline (§5);
15
+ * - stub size is independent of every server-supplied field length (§5);
16
+ * - unparseable `expiresAt` fails closed (§7.4);
17
+ * - unknown block types never propagate `undefined` (the pre-existing
18
+ * convertBlock switches had no default — a latent corruption this module
19
+ * retires).
20
+ *
21
+ * The ReferenceRegistry is the host-private reference record (§6.2): raw
22
+ * URIs and testimony live here and never in model-visible content. Fetching
23
+ * (§6/§7 origin binding, ceilings, redirects) is deliberately not here yet —
24
+ * the registry is what makes a future host-mediated fetcher and the
25
+ * code-execution materialization hook possible.
26
+ */
27
+ export declare const REFERENCE_LIMITS: {
28
+ readonly uri: 4096;
29
+ readonly mimeType: 255;
30
+ readonly name: 255;
31
+ readonly expiresAt: 64;
32
+ };
33
+ export declare const DIGEST_PATTERN: RegExp;
34
+ export type ReferenceDisposition = 'never' | 'ref';
35
+ export interface ReferenceTestimony {
36
+ uri: string;
37
+ mimeType?: string;
38
+ sizeBytes?: number;
39
+ digest?: string;
40
+ expiresAt?: string;
41
+ name?: string;
42
+ disposition?: ReferenceDisposition;
43
+ rejectedFields: string[];
44
+ truncatedFields: string[];
45
+ }
46
+ export type BlockClassification = {
47
+ kind: 'text';
48
+ } | {
49
+ kind: 'inline';
50
+ contradiction: boolean;
51
+ } | {
52
+ kind: 'reference';
53
+ testimony: ReferenceTestimony;
54
+ } | {
55
+ kind: 'invalid';
56
+ reason: string;
57
+ };
58
+ export declare function classifyBlock(block: unknown): BlockClassification;
59
+ /** RFC-005 vector 2: an inline-data block claiming bulk disposition (or
60
+ * smuggling a uri beside its data) is an emitter contradiction — the host
61
+ * fails closed by withholding the inline data from context. Every lane that
62
+ * handles inline media MUST consult this before its data branch. */
63
+ export declare function isInlineContradiction(block: unknown): boolean;
64
+ export declare const INLINE_WITHHELD_TEXT = "[inline content withheld: nonconformant bulk disposition on inline data]";
65
+ export declare function isReferenceExpired(t: Pick<ReferenceTestimony, 'expiresAt'>, nowMs?: number): boolean;
66
+ /** Strip C0/C1 controls plus bidi marks/overrides/isolates; bound length.
67
+ * Labels are labels — storage naming is host-generated elsewhere (§7.3). */
68
+ export declare function sanitizeLabel(s: string, maxChars: number): string;
69
+ export declare function formatSizeBytes(n: number): string;
70
+ export interface ReferenceRecord {
71
+ refId: string;
72
+ serverId?: string;
73
+ testimony: ReferenceTestimony;
74
+ receivedAt: number;
75
+ /** Set by the host-mediated fetcher after a successful, verified fetch. */
76
+ fetchedPath?: string;
77
+ verifiedBytes?: number;
78
+ verifiedMimeType?: string;
79
+ digestVerified?: boolean;
80
+ }
81
+ /**
82
+ * Host-private store of received references. Raw URIs live here and in stubs
83
+ * never (under `never`) or by policy only (`ref`/absent). Ids are stable and
84
+ * never reused within the process (vector 20); eviction is FIFO past
85
+ * MAX_RECORDS and a stale lookup returns undefined — a defined miss, never a
86
+ * different record.
87
+ *
88
+ * Process-wide singleton by design: ids are host-local *names*, not
89
+ * capabilities, and the record itself carries serverId for any future
90
+ * origin-bound fetcher.
91
+ */
92
+ export declare class ReferenceRegistry {
93
+ private records;
94
+ /** uri → refId. The uri ALONE is the identity key: stub sites (the two
95
+ * tool-result serializers and the three converters) are free functions
96
+ * with no server context, while dispatch-time pre-registration carries a
97
+ * serverId — keying on both split every reference into two records, the
98
+ * model-visible one unbound (fetch_reference failed on it) and the bound
99
+ * one invisible. One key, and a later registration that knows the server
100
+ * UPGRADES the record's binding instead of forking it. If two servers ever
101
+ * reference the same uri, the first binding wins (the record's serverId
102
+ * feeds the origin check, which fails closed on a mismatch). */
103
+ private byKey;
104
+ private counter;
105
+ register(testimony: ReferenceTestimony, serverId?: string): ReferenceRecord;
106
+ get(refId: string): ReferenceRecord | undefined;
107
+ /** Find the record for a uri. */
108
+ findByUri(uri: string): ReferenceRecord | undefined;
109
+ }
110
+ export declare const referenceRegistry: ReferenceRegistry;
111
+ /**
112
+ * Build the model-visible stub for a reference. Output length is bounded by
113
+ * host constants and independent of every server-supplied field length.
114
+ * Under `disposition:"never"` the uri is omitted unconditionally; under
115
+ * `ref`/absent it is omitted too — this host's default policy is the opaque
116
+ * id everywhere (the safe default of §5), with retrieval via the record.
117
+ */
118
+ export declare function buildReferenceStub(t: ReferenceTestimony, provenance?: string, serverId?: string): string;
119
+ /**
120
+ * RFC-005 treatment for one MCPL wire block, for the model-visible text
121
+ * lanes. Returns a replacement string, or null when the block should keep
122
+ * its existing (inline/text) handling.
123
+ */
124
+ export declare function referenceStubOrNull(block: unknown, provenance?: string, serverId?: string): string | null;
125
+ //# sourceMappingURL=references.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"references.d.ts","sourceRoot":"","sources":["../../../src/mcpl/references.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,eAAO,MAAM,gBAAgB;;;;;CAAkE,CAAC;AAChG,eAAO,MAAM,cAAc,QAA+B,CAAC;AAI3D,MAAM,MAAM,oBAAoB,GAAG,OAAO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