@animalabs/agent-framework 0.13.0 → 0.15.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 (95) hide show
  1. package/dist/src/agent.d.ts +10 -0
  2. package/dist/src/agent.d.ts.map +1 -1
  3. package/dist/src/agent.js +39 -0
  4. package/dist/src/agent.js.map +1 -1
  5. package/dist/src/framework.d.ts +36 -6
  6. package/dist/src/framework.d.ts.map +1 -1
  7. package/dist/src/framework.js +451 -83
  8. package/dist/src/framework.js.map +1 -1
  9. package/dist/src/gate/event-gate.d.ts +55 -1
  10. package/dist/src/gate/event-gate.d.ts.map +1 -1
  11. package/dist/src/gate/event-gate.js +41 -1
  12. package/dist/src/gate/event-gate.js.map +1 -1
  13. package/dist/src/gate/index.d.ts +1 -1
  14. package/dist/src/gate/index.d.ts.map +1 -1
  15. package/dist/src/gate/index.js +1 -1
  16. package/dist/src/gate/index.js.map +1 -1
  17. package/dist/src/index.d.ts +1 -1
  18. package/dist/src/index.d.ts.map +1 -1
  19. package/dist/src/index.js.map +1 -1
  20. package/dist/src/modules/history/index.d.ts +69 -0
  21. package/dist/src/modules/history/index.d.ts.map +1 -0
  22. package/dist/src/modules/history/index.js +519 -0
  23. package/dist/src/modules/history/index.js.map +1 -0
  24. package/dist/src/modules/history/search-regex-worker.d.ts +23 -0
  25. package/dist/src/modules/history/search-regex-worker.d.ts.map +1 -0
  26. package/dist/src/modules/history/search-regex-worker.js +57 -0
  27. package/dist/src/modules/history/search-regex-worker.js.map +1 -0
  28. package/dist/src/modules/index.d.ts +1 -0
  29. package/dist/src/modules/index.d.ts.map +1 -1
  30. package/dist/src/modules/index.js +1 -0
  31. package/dist/src/modules/index.js.map +1 -1
  32. package/dist/src/modules/workspace/index.d.ts.map +1 -1
  33. package/dist/src/modules/workspace/index.js +42 -1
  34. package/dist/src/modules/workspace/index.js.map +1 -1
  35. package/dist/src/modules/workspace/types.d.ts +5 -0
  36. package/dist/src/modules/workspace/types.d.ts.map +1 -1
  37. package/dist/src/modules/workspace/types.js.map +1 -1
  38. package/dist/src/tool-image-ledger.d.ts +175 -0
  39. package/dist/src/tool-image-ledger.d.ts.map +1 -0
  40. package/dist/src/tool-image-ledger.js +257 -0
  41. package/dist/src/tool-image-ledger.js.map +1 -0
  42. package/dist/src/tool-result-history.d.ts +15 -2
  43. package/dist/src/tool-result-history.d.ts.map +1 -1
  44. package/dist/src/tool-result-history.js +16 -9
  45. package/dist/src/tool-result-history.js.map +1 -1
  46. package/dist/src/types/agent.d.ts +10 -0
  47. package/dist/src/types/agent.d.ts.map +1 -1
  48. package/dist/src/types/trace.d.ts +4 -0
  49. package/dist/src/types/trace.d.ts.map +1 -1
  50. package/dist/test/event-gate.test.js +84 -1
  51. package/dist/test/event-gate.test.js.map +1 -1
  52. package/dist/test/history-module.test.d.ts +2 -0
  53. package/dist/test/history-module.test.d.ts.map +1 -0
  54. package/dist/test/history-module.test.js +449 -0
  55. package/dist/test/history-module.test.js.map +1 -0
  56. package/dist/test/kv-unified-open-flight-retry.test.d.ts +2 -0
  57. package/dist/test/kv-unified-open-flight-retry.test.d.ts.map +1 -0
  58. package/dist/test/kv-unified-open-flight-retry.test.js +189 -0
  59. package/dist/test/kv-unified-open-flight-retry.test.js.map +1 -0
  60. package/dist/test/kv-unified-wiring.test.js +39 -0
  61. package/dist/test/kv-unified-wiring.test.js.map +1 -1
  62. package/dist/test/puppet-tool-call.test.js +114 -1
  63. package/dist/test/puppet-tool-call.test.js.map +1 -1
  64. package/dist/test/puppet-turn-reservation.test.d.ts +2 -0
  65. package/dist/test/puppet-turn-reservation.test.d.ts.map +1 -0
  66. package/dist/test/puppet-turn-reservation.test.js +139 -0
  67. package/dist/test/puppet-turn-reservation.test.js.map +1 -0
  68. package/dist/test/save-recent-image.test.d.ts +26 -0
  69. package/dist/test/save-recent-image.test.d.ts.map +1 -0
  70. package/dist/test/save-recent-image.test.js +736 -0
  71. package/dist/test/save-recent-image.test.js.map +1 -0
  72. package/dist/test/tool-result-spill.test.js +114 -0
  73. package/dist/test/tool-result-spill.test.js.map +1 -1
  74. package/dist/test/turn-trigger-provenance.test.js +17 -0
  75. package/dist/test/turn-trigger-provenance.test.js.map +1 -1
  76. package/dist/test/workspace-read-text.test.d.ts +2 -0
  77. package/dist/test/workspace-read-text.test.d.ts.map +1 -0
  78. package/dist/test/workspace-read-text.test.js +101 -0
  79. package/dist/test/workspace-read-text.test.js.map +1 -0
  80. package/dist/tsconfig.tsbuildinfo +1 -1
  81. package/package.json +3 -3
  82. package/src/agent.ts +41 -0
  83. package/src/framework.ts +519 -95
  84. package/src/gate/event-gate.ts +71 -3
  85. package/src/gate/index.ts +1 -1
  86. package/src/index.ts +1 -1
  87. package/src/modules/history/index.ts +613 -0
  88. package/src/modules/history/search-regex-worker.ts +76 -0
  89. package/src/modules/index.ts +2 -0
  90. package/src/modules/workspace/index.ts +42 -1
  91. package/src/modules/workspace/types.ts +5 -0
  92. package/src/tool-image-ledger.ts +334 -0
  93. package/src/tool-result-history.ts +30 -9
  94. package/src/types/agent.ts +10 -0
  95. package/src/types/trace.ts +4 -0
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Per-agent retention of tool-result images, with provenance (issue #104).
3
+ *
4
+ * History keeps a compact text placeholder for every image a tool returns —
5
+ * the bytes only ever ride the live wire copy of that one turn. That policy
6
+ * is right (megabytes of base64 in the persisted window would be sliced,
7
+ * compressed, and replayed forever), but it left `save_recent_image` with no
8
+ * way to reach an image the resident had just seen: the scanner walked past
9
+ * the byteless placeholder and quietly saved whichever OLDER attachment came
10
+ * next, under the filename the resident chose for the new one.
11
+ *
12
+ * This ledger is the missing half. At ingestion every tool-result image is
13
+ * retained here under a stable per-agent ref (`img_k7x3q2_7`) bound to the
14
+ * exact tool call and block it came from, with its digest. The ref carries a
15
+ * per-ledger nonce: ledgers live and die with the process, but the persisted
16
+ * store keeps yesterday's placeholders visible in context, so a bare sequence
17
+ * number would let a stale `img_7` resolve to whatever this process minted
18
+ * seventh. A stale ref must resolve `unknown`, never to other bytes.
19
+ *
20
+ * The history placeholder carries the ref, so the ordered inventory
21
+ * `save_recent_image` walks can
22
+ * include tool images at their true position — and when the bytes behind a
23
+ * ref are gone (bounded eviction, or a restart: the ledger is in-memory by
24
+ * design, so it never adds to blob growth), the save fails AT THAT INDEX with
25
+ * a specific error instead of sliding to an older image. Silent substitution
26
+ * of bytes from another surface is the failure this exists to end.
27
+ */
28
+ export interface RetainedToolImage {
29
+ /** Stable per-ledger handle (`img_<nonce>_<seq>`), minted once per (toolCallId, blockIndex). */
30
+ ref: string;
31
+ toolCallId: string;
32
+ toolName: string;
33
+ /** Position of the image block inside the tool result's content array. */
34
+ blockIndex: number;
35
+ mediaType: string;
36
+ byteSize: number;
37
+ sha256: string;
38
+ retainedAt: number;
39
+ }
40
+ /** A retained image whose bytes are still held. */
41
+ export interface RetainedToolImageWithData extends RetainedToolImage {
42
+ /** Base64 payload, exactly as the tool returned it. */
43
+ data: string;
44
+ }
45
+ export type ToolImageLookup = {
46
+ status: 'retained';
47
+ image: RetainedToolImageWithData;
48
+ }
49
+ /** The ref was issued by this ledger but its bytes were evicted (bounded budget). */
50
+ | {
51
+ status: 'evicted';
52
+ image: RetainedToolImage;
53
+ }
54
+ /** Never issued by this ledger — a typo, or a ref minted before a restart. */
55
+ | {
56
+ status: 'unknown';
57
+ };
58
+ export interface ToolImageLedgerOptions {
59
+ /** Total base64 chars held across retained images (≈ bytes × 4/3). */
60
+ maxChars?: number;
61
+ /** Maximum number of images whose bytes are held. */
62
+ maxEntries?: number;
63
+ }
64
+ /** 96 MB of base64 ≈ 72 MB of image bytes — a few dozen world snapshots. */
65
+ export declare const DEFAULT_TOOL_IMAGE_LEDGER_MAX_CHARS: number;
66
+ export declare const DEFAULT_TOOL_IMAGE_LEDGER_MAX_ENTRIES = 64;
67
+ export declare class ToolImageLedger {
68
+ /** Namespace for every ref this ledger mints; a fresh ledger (new process,
69
+ * agent re-registration) never shares one with its predecessors. */
70
+ readonly nonce: string;
71
+ private nextSeq;
72
+ /** Insertion-ordered (oldest first) — eviction pops from the front. */
73
+ private readonly retained;
74
+ /** Provenance for every ref ever issued, bytes or not (bounded separately). */
75
+ private readonly issued;
76
+ private readonly refByKey;
77
+ private heldChars;
78
+ private readonly maxChars;
79
+ private readonly maxEntries;
80
+ constructor(options?: ToolImageLedgerOptions);
81
+ /**
82
+ * Retain one image block from a tool result. Idempotent per
83
+ * (toolCallId, blockIndex) for as long as that block's provenance record
84
+ * is held (bounded — see trimProvenance): within that window the same
85
+ * block always yields the same ref, so a result serialized twice (or
86
+ * retained early in a tool batch, then serialized at commit) agrees with
87
+ * itself. A block re-serialized after its record aged out mints a fresh
88
+ * ref; the old one resolves `evicted`/`unknown`, never to other bytes.
89
+ */
90
+ retain(source: {
91
+ toolCallId: string;
92
+ toolName: string;
93
+ blockIndex: number;
94
+ data: string;
95
+ mediaType: string;
96
+ }): RetainedToolImage;
97
+ lookup(ref: string): ToolImageLookup;
98
+ /** Ref already minted for this block, if any (no retention side effect). */
99
+ refFor(toolCallId: string, blockIndex: number): string | undefined;
100
+ /** Count of images whose bytes are currently held. */
101
+ get size(): number;
102
+ private evictOverBudget;
103
+ /** Provenance for evicted refs is kept for a while so the error can name
104
+ * the tool call; bounded so a long-lived agent can't grow it forever. */
105
+ private trimProvenance;
106
+ }
107
+ /** Grammar of a ref this module mints: `img_<ledger nonce>_<sequence>`. */
108
+ export declare const TOOL_IMAGE_REF_RE: RegExp;
109
+ export declare const FALLBACK_MEDIA_TYPE = "application/octet-stream";
110
+ /**
111
+ * The one spelling the placeholder grammar accepts: trimmed, lowercased, the
112
+ * `type/subtype` essence only (parameters such as `; charset=binary` are
113
+ * dropped), and — because `mimeType` arrives from external MCPL servers
114
+ * unvalidated — anything that still is not a well-formed type/subtype falls
115
+ * back to `application/octet-stream`. The invariant this buys: the
116
+ * serializer can NEVER write a placeholder that does not re-parse, so a
117
+ * strange mime can never produce an invisible slot (a silent index slide).
118
+ */
119
+ export declare function normalizeMediaType(mediaType: string): string;
120
+ /** History placeholder for a retained tool image: `[image: image/png, ~691KB, ref img_7]`. */
121
+ export declare function formatToolImagePlaceholder(mediaType: string, sizeLabel: string, ref: string | null): string;
122
+ export type ParsedImagePlaceholder =
123
+ /** An inline tool image the serializer replaced with a placeholder. */
124
+ {
125
+ kind: 'inline';
126
+ mediaType: string;
127
+ sizeLabel: string;
128
+ /** null for placeholders written before retention existed (unsaveable). */
129
+ ref: string | null;
130
+ /** Character offset in the scanned text — for ordering within a block. */
131
+ offset: number;
132
+ /** The placeholder exactly as it appears in the text. */
133
+ text: string;
134
+ }
135
+ /** An RFC-005 reference stub whose testimony says image — bytes live
136
+ * behind fetch_reference, never in this ledger. Occupies a slot so the
137
+ * index cannot slide past it. */
138
+ | {
139
+ kind: 'reference';
140
+ refId: string;
141
+ /** Mime sniffed from the stub text; null when the stub names none. */
142
+ mediaType: string | null;
143
+ offset: number;
144
+ /** The stub line exactly as it appears in the text. */
145
+ text: string;
146
+ };
147
+ /**
148
+ * Every image slot in a stored tool-result string, in text order: inline
149
+ * placeholders and image-typed reference stubs. Text-derived and therefore
150
+ * untrusted — a tool result can quote someone else's placeholder verbatim —
151
+ * so the caller MUST cross-check a resolved ref's provenance against the
152
+ * tool_result block it was found in before handing out bytes.
153
+ */
154
+ export declare function parseImagePlaceholders(text: string): ParsedImagePlaceholder[];
155
+ /**
156
+ * Cut `text` to at most `cap` chars WITHOUT losing or splitting an image
157
+ * slot. The stored tool_result text is the only place the inventory
158
+ * discovers tool images, while the live wire still delivers every image
159
+ * regardless of text length — so a slot dropped by truncation is an image
160
+ * the resident saw that the index silently slides past (the #104 failure,
161
+ * one representation over). Returns the head to keep and the slots that
162
+ * fell at or past the cut, to be re-appended verbatim after the truncation
163
+ * notice. A slot straddling the cut moves the cut back to its start, so the
164
+ * head never contains a partial slot (a partial reference-stub line would
165
+ * otherwise still re-parse and be counted twice).
166
+ */
167
+ export declare function splitPreservingImageSlots(text: string, cap: number): {
168
+ head: string;
169
+ tail: string[];
170
+ };
171
+ /** Text to append after a truncation notice so the preserved slots stay
172
+ * parseable (each reference stub on its own line). Empty when none. */
173
+ export declare function formatPreservedImageSlots(tail: string[]): string;
174
+ export declare function sha256Hex(bytes: Buffer): string;
175
+ //# sourceMappingURL=tool-image-ledger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-image-ledger.d.ts","sourceRoot":"","sources":["../../src/tool-image-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAKH,MAAM,WAAW,iBAAiB;IAChC,gGAAgG;IAChG,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,0EAA0E;IAC1E,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,mDAAmD;AACnD,MAAM,WAAW,yBAA0B,SAAQ,iBAAiB;IAClE,uDAAuD;IACvD,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,eAAe,GACvB;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,yBAAyB,CAAA;CAAE;AAC1D,qFAAqF;GACnF;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,iBAAiB,CAAA;CAAE;AACjD,8EAA8E;GAC5E;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,CAAC;AAE1B,MAAM,WAAW,sBAAsB;IACrC,sEAAsE;IACtE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qDAAqD;IACrD,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,4EAA4E;AAC5E,eAAO,MAAM,mCAAmC,QAAmB,CAAC;AACpE,eAAO,MAAM,qCAAqC,KAAK,C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G,MAAM,CAK3G;AAED,MAAM,MAAM,sBAAsB;AAChC,uEAAuE;AACrE;IACA,IAAI,EAAE,QAAQ,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,2EAA2E;IAC3E,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,0EAA0E;IAC1E,MAAM,EAAE,MAAM,CAAC;IACf,yDAAyD;IACzD,IAAI,EAAE,MAAM,CAAC;CACd;AACD;;kCAEkC;GAChC;IACA,IAAI,EAAE,WAAW,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,sEAAsE;IACtE,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,uDAAuD;IACvD,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAOJ;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,sBAAsB,EAAE,CAW7E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAA;CAAE,CAUrG;AAED;wEACwE;AACxE,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAIhE;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE/C"}
@@ -0,0 +1,257 @@
1
+ /**
2
+ * Per-agent retention of tool-result images, with provenance (issue #104).
3
+ *
4
+ * History keeps a compact text placeholder for every image a tool returns —
5
+ * the bytes only ever ride the live wire copy of that one turn. That policy
6
+ * is right (megabytes of base64 in the persisted window would be sliced,
7
+ * compressed, and replayed forever), but it left `save_recent_image` with no
8
+ * way to reach an image the resident had just seen: the scanner walked past
9
+ * the byteless placeholder and quietly saved whichever OLDER attachment came
10
+ * next, under the filename the resident chose for the new one.
11
+ *
12
+ * This ledger is the missing half. At ingestion every tool-result image is
13
+ * retained here under a stable per-agent ref (`img_k7x3q2_7`) bound to the
14
+ * exact tool call and block it came from, with its digest. The ref carries a
15
+ * per-ledger nonce: ledgers live and die with the process, but the persisted
16
+ * store keeps yesterday's placeholders visible in context, so a bare sequence
17
+ * number would let a stale `img_7` resolve to whatever this process minted
18
+ * seventh. A stale ref must resolve `unknown`, never to other bytes.
19
+ *
20
+ * The history placeholder carries the ref, so the ordered inventory
21
+ * `save_recent_image` walks can
22
+ * include tool images at their true position — and when the bytes behind a
23
+ * ref are gone (bounded eviction, or a restart: the ledger is in-memory by
24
+ * design, so it never adds to blob growth), the save fails AT THAT INDEX with
25
+ * a specific error instead of sliding to an older image. Silent substitution
26
+ * of bytes from another surface is the failure this exists to end.
27
+ */
28
+ import { createHash, randomBytes } from 'node:crypto';
29
+ import { safeSlice } from './safe-slice.js';
30
+ /** 96 MB of base64 ≈ 72 MB of image bytes — a few dozen world snapshots. */
31
+ export const DEFAULT_TOOL_IMAGE_LEDGER_MAX_CHARS = 96 * 1024 * 1024;
32
+ export const DEFAULT_TOOL_IMAGE_LEDGER_MAX_ENTRIES = 64;
33
+ export class ToolImageLedger {
34
+ /** Namespace for every ref this ledger mints; a fresh ledger (new process,
35
+ * agent re-registration) never shares one with its predecessors. */
36
+ nonce = mintLedgerNonce();
37
+ nextSeq = 1;
38
+ /** Insertion-ordered (oldest first) — eviction pops from the front. */
39
+ retained = new Map();
40
+ /** Provenance for every ref ever issued, bytes or not (bounded separately). */
41
+ issued = new Map();
42
+ refByKey = new Map();
43
+ heldChars = 0;
44
+ maxChars;
45
+ maxEntries;
46
+ constructor(options = {}) {
47
+ this.maxChars = options.maxChars ?? DEFAULT_TOOL_IMAGE_LEDGER_MAX_CHARS;
48
+ this.maxEntries = options.maxEntries ?? DEFAULT_TOOL_IMAGE_LEDGER_MAX_ENTRIES;
49
+ }
50
+ /**
51
+ * Retain one image block from a tool result. Idempotent per
52
+ * (toolCallId, blockIndex) for as long as that block's provenance record
53
+ * is held (bounded — see trimProvenance): within that window the same
54
+ * block always yields the same ref, so a result serialized twice (or
55
+ * retained early in a tool batch, then serialized at commit) agrees with
56
+ * itself. A block re-serialized after its record aged out mints a fresh
57
+ * ref; the old one resolves `evicted`/`unknown`, never to other bytes.
58
+ */
59
+ retain(source) {
60
+ const key = `${source.toolCallId}:${source.blockIndex}`;
61
+ const existingRef = this.refByKey.get(key);
62
+ if (existingRef !== undefined) {
63
+ const known = this.issued.get(existingRef);
64
+ if (known)
65
+ return known;
66
+ }
67
+ const ref = `img_${this.nonce}_${this.nextSeq++}`;
68
+ // Digest without materializing the whole decoded image: a 96MB base64
69
+ // field would otherwise transiently allocate ~72MB more and the ledger
70
+ // would be the one asking for it. Chunked decode keeps the peak small.
71
+ const { byteSize, sha256 } = digestBase64(source.data);
72
+ const image = {
73
+ ref,
74
+ toolCallId: source.toolCallId,
75
+ toolName: source.toolName,
76
+ blockIndex: source.blockIndex,
77
+ // Tools send mime types unvalidated; the placeholder grammar is
78
+ // lowercase, so normalize here and at format time (same rule) or an
79
+ // `Image/PNG` would write a placeholder that never re-parses — an
80
+ // invisible slot, i.e. a silent index slide.
81
+ mediaType: normalizeMediaType(source.mediaType),
82
+ byteSize,
83
+ sha256,
84
+ retainedAt: Date.now(),
85
+ data: source.data,
86
+ };
87
+ const { data: _data, ...provenance } = image;
88
+ this.refByKey.set(key, ref);
89
+ this.issued.set(ref, provenance);
90
+ // A single image over the whole budget is issued (so the placeholder is
91
+ // honest about its provenance) but never held.
92
+ if (source.data.length <= this.maxChars) {
93
+ this.retained.set(ref, image);
94
+ this.heldChars += source.data.length;
95
+ this.evictOverBudget();
96
+ }
97
+ this.trimProvenance();
98
+ return provenance;
99
+ }
100
+ lookup(ref) {
101
+ const held = this.retained.get(ref);
102
+ if (held)
103
+ return { status: 'retained', image: held };
104
+ const known = this.issued.get(ref);
105
+ if (known)
106
+ return { status: 'evicted', image: known };
107
+ return { status: 'unknown' };
108
+ }
109
+ /** Ref already minted for this block, if any (no retention side effect). */
110
+ refFor(toolCallId, blockIndex) {
111
+ return this.refByKey.get(`${toolCallId}:${blockIndex}`);
112
+ }
113
+ /** Count of images whose bytes are currently held. */
114
+ get size() {
115
+ return this.retained.size;
116
+ }
117
+ evictOverBudget() {
118
+ for (const [ref, image] of this.retained) {
119
+ if (this.retained.size <= this.maxEntries && this.heldChars <= this.maxChars)
120
+ break;
121
+ this.retained.delete(ref);
122
+ this.heldChars -= image.data.length;
123
+ }
124
+ }
125
+ /** Provenance for evicted refs is kept for a while so the error can name
126
+ * the tool call; bounded so a long-lived agent can't grow it forever. */
127
+ trimProvenance() {
128
+ const keep = Math.max(this.maxEntries * 4, 256);
129
+ while (this.issued.size > keep) {
130
+ const oldest = this.issued.keys().next().value;
131
+ if (oldest === undefined)
132
+ break;
133
+ const image = this.issued.get(oldest);
134
+ this.issued.delete(oldest);
135
+ if (image)
136
+ this.refByKey.delete(`${image.toolCallId}:${image.blockIndex}`);
137
+ }
138
+ }
139
+ }
140
+ /** Six random base36 chars — one per ledger lifetime, embedded in every ref. */
141
+ function mintLedgerNonce() {
142
+ return randomBytes(4).readUInt32BE(0).toString(36).padStart(6, '0').slice(-6);
143
+ }
144
+ /** Grammar of a ref this module mints: `img_<ledger nonce>_<sequence>`. */
145
+ export const TOOL_IMAGE_REF_RE = /^img_[0-9a-z]+_\d+$/;
146
+ /** RFC 6838 type/subtype token characters (lowercased); no parameters. */
147
+ const MEDIA_TYPE_ESSENCE_RE = /^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/;
148
+ export const FALLBACK_MEDIA_TYPE = 'application/octet-stream';
149
+ /**
150
+ * The one spelling the placeholder grammar accepts: trimmed, lowercased, the
151
+ * `type/subtype` essence only (parameters such as `; charset=binary` are
152
+ * dropped), and — because `mimeType` arrives from external MCPL servers
153
+ * unvalidated — anything that still is not a well-formed type/subtype falls
154
+ * back to `application/octet-stream`. The invariant this buys: the
155
+ * serializer can NEVER write a placeholder that does not re-parse, so a
156
+ * strange mime can never produce an invisible slot (a silent index slide).
157
+ */
158
+ export function normalizeMediaType(mediaType) {
159
+ const essence = mediaType.split(';', 1)[0].trim().toLowerCase();
160
+ return MEDIA_TYPE_ESSENCE_RE.test(essence) ? essence : FALLBACK_MEDIA_TYPE;
161
+ }
162
+ /** History placeholder for a retained tool image: `[image: image/png, ~691KB, ref img_7]`. */
163
+ export function formatToolImagePlaceholder(mediaType, sizeLabel, ref) {
164
+ const mime = normalizeMediaType(mediaType);
165
+ return ref === null
166
+ ? `[image: ${mime}, ${sizeLabel}]`
167
+ : `[image: ${mime}, ${sizeLabel}, ref ${ref}]`;
168
+ }
169
+ const PLACEHOLDER_RE = /\[image: ([a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+), (~?[\d.]+(?:B|KB|MB))(?:, ref (img_[0-9a-z]+_\d+))?\]/g;
170
+ /** `[ref_1_ab3d] cam.png — image/png, 1.2MB claimed — from tool result — …` (one line). */
171
+ const REFERENCE_STUB_RE = /^\[(ref_[0-9a-z]+_[0-9a-z]+)\] ([^\n]*)$/gm;
172
+ const IMAGE_MIME_RE = /\bimage\/[a-z0-9!#$&^_.+-]+/i;
173
+ /**
174
+ * Every image slot in a stored tool-result string, in text order: inline
175
+ * placeholders and image-typed reference stubs. Text-derived and therefore
176
+ * untrusted — a tool result can quote someone else's placeholder verbatim —
177
+ * so the caller MUST cross-check a resolved ref's provenance against the
178
+ * tool_result block it was found in before handing out bytes.
179
+ */
180
+ export function parseImagePlaceholders(text) {
181
+ const out = [];
182
+ for (const m of text.matchAll(PLACEHOLDER_RE)) {
183
+ out.push({ kind: 'inline', mediaType: m[1], sizeLabel: m[2], ref: m[3] ?? null, offset: m.index ?? 0, text: m[0] });
184
+ }
185
+ for (const m of text.matchAll(REFERENCE_STUB_RE)) {
186
+ const mime = m[2].match(IMAGE_MIME_RE);
187
+ out.push({ kind: 'reference', refId: m[1], mediaType: mime ? mime[0].toLowerCase() : null, offset: m.index ?? 0, text: m[0] });
188
+ }
189
+ out.sort((a, b) => a.offset - b.offset);
190
+ return out;
191
+ }
192
+ /**
193
+ * Cut `text` to at most `cap` chars WITHOUT losing or splitting an image
194
+ * slot. The stored tool_result text is the only place the inventory
195
+ * discovers tool images, while the live wire still delivers every image
196
+ * regardless of text length — so a slot dropped by truncation is an image
197
+ * the resident saw that the index silently slides past (the #104 failure,
198
+ * one representation over). Returns the head to keep and the slots that
199
+ * fell at or past the cut, to be re-appended verbatim after the truncation
200
+ * notice. A slot straddling the cut moves the cut back to its start, so the
201
+ * head never contains a partial slot (a partial reference-stub line would
202
+ * otherwise still re-parse and be counted twice).
203
+ */
204
+ export function splitPreservingImageSlots(text, cap) {
205
+ const slots = parseImagePlaceholders(text);
206
+ let cutAt = cap;
207
+ for (const slot of slots) {
208
+ if (slot.offset < cutAt && slot.offset + slot.text.length > cutAt)
209
+ cutAt = slot.offset;
210
+ }
211
+ return {
212
+ head: safeSlice(text, 0, cutAt),
213
+ tail: slots.filter((slot) => slot.offset >= cutAt).map((slot) => slot.text),
214
+ };
215
+ }
216
+ /** Text to append after a truncation notice so the preserved slots stay
217
+ * parseable (each reference stub on its own line). Empty when none. */
218
+ export function formatPreservedImageSlots(tail) {
219
+ if (tail.length === 0)
220
+ return '';
221
+ return `\n[${tail.length} image slot(s) fell past the cut; kept here in order so save_recent_image still counts them:]\n`
222
+ + tail.join('\n');
223
+ }
224
+ export function sha256Hex(bytes) {
225
+ return createHash('sha256').update(bytes).digest('hex');
226
+ }
227
+ /** Only the base64 alphabet, with nothing but trailing `=` padding after it. */
228
+ function isCleanBase64(data) {
229
+ const m = /[^A-Za-z0-9+/]/.exec(data);
230
+ if (!m)
231
+ return true;
232
+ const rest = data.length - m.index;
233
+ return rest <= 2 && /^=+$/.test(data.slice(m.index));
234
+ }
235
+ /**
236
+ * Byte length and SHA-256 of a base64 payload, decoded in 4-char-aligned
237
+ * chunks so the peak allocation stays at one chunk rather than the whole
238
+ * image. Falls back to a whole decode when the string carries whitespace
239
+ * or embedded padding (chunk alignment would no longer be trustworthy) —
240
+ * the digest must describe exactly the bytes a later whole decode writes.
241
+ */
242
+ function digestBase64(data) {
243
+ if (!isCleanBase64(data)) {
244
+ const bytes = Buffer.from(data, 'base64');
245
+ return { byteSize: bytes.byteLength, sha256: sha256Hex(bytes) };
246
+ }
247
+ const CHUNK = 1 << 20; // multiple of 4
248
+ const hash = createHash('sha256');
249
+ let byteSize = 0;
250
+ for (let i = 0; i < data.length; i += CHUNK) {
251
+ const bytes = Buffer.from(data.slice(i, i + CHUNK), 'base64');
252
+ hash.update(bytes);
253
+ byteSize += bytes.byteLength;
254
+ }
255
+ return { byteSize, sha256: hash.digest('hex') };
256
+ }
257
+ //# sourceMappingURL=tool-image-ledger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-image-ledger.js","sourceRoot":"","sources":["../../src/tool-image-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAmC5C,4EAA4E;AAC5E,MAAM,CAAC,MAAM,mCAAmC,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AACpE,MAAM,CAAC,MAAM,qCAAqC,GAAG,EAAE,CAAC;AAExD,MAAM,OAAO,eAAe;IAC1B;yEACqE;IAC5D,KAAK,GAAW,eAAe,EAAE,CAAC;IACnC,OAAO,GAAG,CAAC,CAAC;IACpB,uEAAuE;IACtD,QAAQ,GAAG,IAAI,GAAG,EAAqC,CAAC;IACzE,+EAA+E;IAC9D,MAAM,GAAG,IAAI,GAAG,EAA6B,CAAC;IAC9C,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC9C,SAAS,GAAG,CAAC,CAAC;IACL,QAAQ,CAAS;IACjB,U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SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;IAClE,CAAC;IACD,MAAM,KAAK,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,gBAAgB;IACvC,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IAClC,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC;QAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,EAAE,QAAQ,CAAC,CAAC;QAC9D,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACnB,QAAQ,IAAI,KAAK,CAAC,UAAU,CAAC;IAC/B,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AAClD,CAAC"}
@@ -13,6 +13,18 @@
13
13
  * replaces image blocks with a short `[image: mimeType, ~NKB]` placeholder.
14
14
  * For anything else it falls back to JSON.
15
15
  */
16
+ export interface HistorySerializeOptions {
17
+ /**
18
+ * Called for every image block the serializer replaces with a placeholder
19
+ * (issue #104). Returns the retention ref to embed in the placeholder —
20
+ * `[image: image/png, ~691KB, ref img_7]` — so the image keeps a saveable
21
+ * handle in history; null leaves the legacy ref-less form.
22
+ */
23
+ imageRef?: (blockIndex: number, image: {
24
+ data: string;
25
+ mimeType: string;
26
+ }) => string | null;
27
+ }
16
28
  /**
17
29
  * House-safe default inline cap (chars) for tool results, error results, and
18
30
  * background-script wake payloads (issue #89: a resident should never eat a
@@ -31,7 +43,8 @@
31
43
  * broad greps) still spill, which is the point.
32
44
  */
33
45
  export declare const DEFAULT_TOOL_RESULT_INLINE_MAX_CHARS = 24000;
34
- export declare function toolResultDataToHistoryString(data: unknown, maxChars?: number): string;
35
- /** Bounded copy of an arbitrary string with the standard truncation notice. */
46
+ export declare function toolResultDataToHistoryString(data: unknown, maxChars?: number, options?: HistorySerializeOptions): string;
47
+ /** Bounded copy of an arbitrary string with the standard truncation notice.
48
+ * Image slots past the cut are re-appended, never dropped (issue #104). */
36
49
  export declare function truncateForHistory(str: string, maxChars: number): string;
37
50
  //# sourceMappingURL=tool-result-history.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tool-result-history.d.ts","sourceRoot":"","sources":["../../src/tool-result-history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,oCAAoC,QAAQ,CAAC;AAE1D,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAItF;AAED,+EAA+E;AAC/E,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAIxE"}
1
+ {"version":3,"file":"tool-result-history.d.ts","sourceRoot":"","sources":["../../src/tool-result-history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,MAAM,WAAW,uBAAuB;IACtC;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,KAAK,MAAM,GAAG,IAAI,CAAC;CAC7F;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,oCAAoC,QAAQ,CAAC;AAE1D,wBAAgB,6BAA6B,CAC3C,IAAI,EAAE,OAAO,EACb,QAAQ,CAAC,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,uBAAuB,GAChC,MAAM,CAIR;AAED;4EAC4E;AAC5E,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAMxE"}
@@ -13,8 +13,8 @@
13
13
  * replaces image blocks with a short `[image: mimeType, ~NKB]` placeholder.
14
14
  * For anything else it falls back to JSON.
15
15
  */
16
- import { safeSlice } from './safe-slice.js';
17
16
  import { INLINE_WITHHELD_TEXT, isInlineContradiction, referenceStubOrNull } from './mcpl/references.js';
17
+ import { formatPreservedImageSlots, formatToolImagePlaceholder, splitPreservingImageSlots } from './tool-image-ledger.js';
18
18
  /**
19
19
  * House-safe default inline cap (chars) for tool results, error results, and
20
20
  * background-script wake payloads (issue #89: a resident should never eat a
@@ -33,28 +33,31 @@ import { INLINE_WITHHELD_TEXT, isInlineContradiction, referenceStubOrNull } from
33
33
  * broad greps) still spill, which is the point.
34
34
  */
35
35
  export const DEFAULT_TOOL_RESULT_INLINE_MAX_CHARS = 24000;
36
- export function toolResultDataToHistoryString(data, maxChars) {
37
- const fromArray = tryHistoryStringFromContentArray(data);
36
+ export function toolResultDataToHistoryString(data, maxChars, options) {
37
+ const fromArray = tryHistoryStringFromContentArray(data, options);
38
38
  const str = fromArray ?? JSON.stringify(data);
39
39
  return maxChars ? truncateForHistory(str, maxChars) : str;
40
40
  }
41
- /** Bounded copy of an arbitrary string with the standard truncation notice. */
41
+ /** Bounded copy of an arbitrary string with the standard truncation notice.
42
+ * Image slots past the cut are re-appended, never dropped (issue #104). */
42
43
  export function truncateForHistory(str, maxChars) {
43
44
  if (str.length <= maxChars)
44
45
  return str;
45
- return safeSlice(str, 0, maxChars)
46
- + '\n\n[truncated — original was ' + str.length + ' chars]';
46
+ const { head, tail } = splitPreservingImageSlots(str, maxChars);
47
+ return head
48
+ + '\n\n[truncated — original was ' + str.length + ' chars]'
49
+ + formatPreservedImageSlots(tail);
47
50
  }
48
51
  /**
49
52
  * If `data` is an MCP content array whose every block has a shape we
50
53
  * recognize, return a history-safe string (images → placeholders). Otherwise
51
54
  * return `null` to defer to the caller's fallback (JSON, usually).
52
55
  */
53
- function tryHistoryStringFromContentArray(data) {
56
+ function tryHistoryStringFromContentArray(data, options) {
54
57
  if (!Array.isArray(data))
55
58
  return null;
56
59
  const parts = [];
57
- for (const raw of data) {
60
+ for (const [blockIndex, raw] of data.entries()) {
58
61
  if (!raw || typeof raw !== 'object')
59
62
  return null;
60
63
  const b = raw;
@@ -69,11 +72,15 @@ function tryHistoryStringFromContentArray(data) {
69
72
  else if (b.type === 'image' && typeof b.data === 'string' && typeof b.mimeType === 'string') {
70
73
  // Decoded byte estimate from base64 length (3/4 ratio, rounded).
71
74
  const approxBytes = Math.floor(b.data.length * 3 / 4);
72
- parts.push(`[image: ${b.mimeType}, ${formatSize(approxBytes)}]`);
75
+ const ref = options?.imageRef?.(blockIndex, { data: b.data, mimeType: b.mimeType }) ?? null;
76
+ parts.push(formatToolImagePlaceholder(b.mimeType, formatSize(approxBytes), ref));
73
77
  }
74
78
  else if (b.type === 'resource' || ((b.type === 'image' || b.type === 'audio') && typeof b.uri === 'string')) {
75
79
  // RFC-005 reference block: a bounded stub, never the raw block JSON
76
80
  // (which used to inline the URI — and the whole array — into history).
81
+ // Image-typed stubs still occupy a slot in save_recent_image's
82
+ // inventory (a failing one — bytes live behind fetch_reference), so
83
+ // the index never slides past an image the resident saw.
77
84
  const stub = referenceStubOrNull(b, 'from tool result');
78
85
  if (stub === null)
79
86
  return null;
@@ -1 +1 @@
1
- {"version":3,"file":"tool-result-history.js","sourceRoot":"","sources":["../../src/tool-result-history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAExG;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,oCAAoC,GAAG,KAAK,CAAC;AAE1D,MAAM,UAAU,6BAA6B,CAAC,IAAa,EAAE,QAAiB;IAC5E,MAAM,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oE;YACpE,uEAAuE;YACvE,+DAA+D;YAC/D,oEAAoE;YACpE,yDAAyD;YACzD,MAAM,IAAI,GAAG,mBAAmB,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC;YACxD,IAAI,IAAI,KAAK,IAAI;gBAAE,OAAO,IAAI,CAAC;YAC/B,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnB,CAAC;aAAM,CAAC;YACN,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED,SAAS,UAAU,CAAC,KAAa;IAC/B,IAAI,KAAK,GAAG,IAAI;QAAE,OAAO,GAAG,KAAK,GAAG,CAAC;IACrC,IAAI,KAAK,GAAG,IAAI,GAAG,IAAI;QAAE,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;IACjE,OAAO,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;AACpD,CAAC"}
@@ -309,5 +309,15 @@ export interface InferenceRequest {
309
309
  * so a household ledger can say who woke whom; carries no content.
310
310
  */
311
311
  counterparty?: string;
312
+ /**
313
+ * Where the wake came from, for telemetry ONLY — never a speech locus.
314
+ * Set by the EventGate for batched wakes (composite channel id of the
315
+ * chosen event when the event carried one; push-event raw ids are not
316
+ * used). Routing keeps reading `channelId`, which only the direct channel
317
+ * paths set from normalized ids — a gate wake leaves it unset, as before.
318
+ */
319
+ wakeChannelId?: string;
320
+ /** Timestamp of the event the wake provenance was taken from (ms). */
321
+ wakeAt?: number;
312
322
  }
313
323
  //# sourceMappingURL=agent.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../../src/types/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,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EAAE,UAAU,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB;;;;;;;;OAQG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;;OAMG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,sEAAsE;IACtE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
@@ -293,6 +293,10 @@ export type TraceEvent = (TraceEventBase & {
293
293
  toolUseId: string;
294
294
  isError: boolean;
295
295
  durationMs: number;
296
+ /** The pair was queued behind a turn that replaced the puppet's
297
+ * reservation (provider-admission re-entry) instead of stored
298
+ * directly; it lands at that turn's end flush. */
299
+ deferred?: boolean;
296
300
  }) | (TraceEventBase & {
297
301
  type: 'mcpl:server-connect-failed';
298
302
  serverId: string;