@earendil-works/pi-durable 0.99.1 → 0.99.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/README.md +435 -46
  2. package/dist/entries.d.ts +20 -2
  3. package/dist/entries.d.ts.map +1 -1
  4. package/dist/entries.js +14 -1
  5. package/dist/entries.js.map +1 -1
  6. package/dist/env/index.d.ts +13 -38
  7. package/dist/env/index.d.ts.map +1 -1
  8. package/dist/env/index.js +2 -0
  9. package/dist/env/index.js.map +1 -1
  10. package/dist/env/node.d.ts.map +1 -1
  11. package/dist/env/node.js +51 -58
  12. package/dist/env/node.js.map +1 -1
  13. package/dist/harness/compaction.d.ts +56 -0
  14. package/dist/harness/compaction.d.ts.map +1 -0
  15. package/dist/harness/compaction.js +385 -0
  16. package/dist/harness/compaction.js.map +1 -0
  17. package/dist/harness/config.d.ts +10 -1
  18. package/dist/harness/config.d.ts.map +1 -1
  19. package/dist/harness/config.js +6 -0
  20. package/dist/harness/config.js.map +1 -1
  21. package/dist/harness/context.d.ts +8 -0
  22. package/dist/harness/context.d.ts.map +1 -1
  23. package/dist/harness/context.js +32 -21
  24. package/dist/harness/context.js.map +1 -1
  25. package/dist/harness/events.d.ts +166 -0
  26. package/dist/harness/events.d.ts.map +1 -0
  27. package/dist/harness/events.js +319 -0
  28. package/dist/harness/events.js.map +1 -0
  29. package/dist/harness/generation.d.ts +37 -6
  30. package/dist/harness/generation.d.ts.map +1 -1
  31. package/dist/harness/generation.js +350 -72
  32. package/dist/harness/generation.js.map +1 -1
  33. package/dist/harness/harness.d.ts.map +1 -1
  34. package/dist/harness/harness.js +124 -3
  35. package/dist/harness/harness.js.map +1 -1
  36. package/dist/harness/inbox.d.ts +57 -0
  37. package/dist/harness/inbox.d.ts.map +1 -0
  38. package/dist/harness/inbox.js +99 -0
  39. package/dist/harness/inbox.js.map +1 -0
  40. package/dist/harness/json.d.ts +10 -0
  41. package/dist/harness/json.d.ts.map +1 -0
  42. package/dist/harness/json.js +33 -0
  43. package/dist/harness/json.js.map +1 -0
  44. package/dist/harness/live.d.ts +66 -7
  45. package/dist/harness/live.d.ts.map +1 -1
  46. package/dist/harness/live.js +71 -7
  47. package/dist/harness/live.js.map +1 -1
  48. package/dist/harness/output.d.ts +62 -0
  49. package/dist/harness/output.d.ts.map +1 -0
  50. package/dist/harness/output.js +261 -0
  51. package/dist/harness/output.js.map +1 -0
  52. package/dist/harness/prompt.d.ts +10 -5
  53. package/dist/harness/prompt.d.ts.map +1 -1
  54. package/dist/harness/prompt.js +66 -13
  55. package/dist/harness/prompt.js.map +1 -1
  56. package/dist/harness/registry.d.ts.map +1 -1
  57. package/dist/harness/registry.js +11 -2
  58. package/dist/harness/registry.js.map +1 -1
  59. package/dist/harness/scheduler.d.ts +37 -12
  60. package/dist/harness/scheduler.d.ts.map +1 -1
  61. package/dist/harness/scheduler.js +632 -72
  62. package/dist/harness/scheduler.js.map +1 -1
  63. package/dist/harness/submissions.d.ts +12 -7
  64. package/dist/harness/submissions.d.ts.map +1 -1
  65. package/dist/harness/submissions.js +72 -33
  66. package/dist/harness/submissions.js.map +1 -1
  67. package/dist/harness/tool.d.ts +36 -0
  68. package/dist/harness/tool.d.ts.map +1 -0
  69. package/dist/harness/tool.js +412 -0
  70. package/dist/harness/tool.js.map +1 -0
  71. package/dist/harness/types.d.ts +185 -11
  72. package/dist/harness/types.d.ts.map +1 -1
  73. package/dist/harness/types.js.map +1 -1
  74. package/dist/harness/usage.d.ts +18 -0
  75. package/dist/harness/usage.d.ts.map +1 -0
  76. package/dist/harness/usage.js +57 -0
  77. package/dist/harness/usage.js.map +1 -0
  78. package/dist/harness/view.d.ts +43 -0
  79. package/dist/harness/view.d.ts.map +1 -0
  80. package/dist/harness/view.js +194 -0
  81. package/dist/harness/view.js.map +1 -0
  82. package/dist/index.d.ts +10 -4
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +6 -1
  85. package/dist/index.js.map +1 -1
  86. package/dist/session/observation.d.ts +18 -11
  87. package/dist/session/observation.d.ts.map +1 -1
  88. package/dist/session/observation.js +24 -13
  89. package/dist/session/observation.js.map +1 -1
  90. package/dist/session/session.d.ts +10 -1
  91. package/dist/session/session.d.ts.map +1 -1
  92. package/dist/session/session.js +18 -4
  93. package/dist/session/session.js.map +1 -1
  94. package/dist/session/transaction.d.ts +10 -1
  95. package/dist/session/transaction.d.ts.map +1 -1
  96. package/dist/session/transaction.js +83 -27
  97. package/dist/session/transaction.js.map +1 -1
  98. package/dist/storage/jsonl/storage.d.ts +22 -2
  99. package/dist/storage/jsonl/storage.d.ts.map +1 -1
  100. package/dist/storage/jsonl/storage.js +1 -1
  101. package/dist/storage/jsonl/storage.js.map +1 -1
  102. package/dist/storage/memory.js +1 -1
  103. package/dist/storage/memory.js.map +1 -1
  104. package/dist/storage/sqlite/migrations.js +1 -1
  105. package/dist/storage/sqlite/migrations.js.map +1 -1
  106. package/dist/testing/storage-benchmark.d.ts.map +1 -1
  107. package/dist/testing/storage-benchmark.js +0 -1
  108. package/dist/testing/storage-benchmark.js.map +1 -1
  109. package/dist/testing/storage-conformance.d.ts.map +1 -1
  110. package/dist/testing/storage-conformance.js +34 -2
  111. package/dist/testing/storage-conformance.js.map +1 -1
  112. package/dist/tools/bash.d.ts +28 -0
  113. package/dist/tools/bash.d.ts.map +1 -0
  114. package/dist/tools/bash.js +69 -0
  115. package/dist/tools/bash.js.map +1 -0
  116. package/dist/tools/edit-diff.d.ts +89 -0
  117. package/dist/tools/edit-diff.d.ts.map +1 -0
  118. package/dist/tools/edit-diff.js +386 -0
  119. package/dist/tools/edit-diff.js.map +1 -0
  120. package/dist/tools/edit.d.ts +18 -0
  121. package/dist/tools/edit.d.ts.map +1 -0
  122. package/dist/tools/edit.js +116 -0
  123. package/dist/tools/edit.js.map +1 -0
  124. package/dist/tools/env.d.ts +5 -0
  125. package/dist/tools/env.d.ts.map +1 -0
  126. package/dist/tools/env.js +7 -0
  127. package/dist/tools/env.js.map +1 -0
  128. package/dist/tools/file-mutation-queue.d.ts +5 -0
  129. package/dist/tools/file-mutation-queue.d.ts.map +1 -0
  130. package/dist/tools/file-mutation-queue.js +46 -0
  131. package/dist/tools/file-mutation-queue.js.map +1 -0
  132. package/dist/tools/image.d.ts +2 -0
  133. package/dist/tools/image.d.ts.map +1 -0
  134. package/dist/tools/image.js +92 -0
  135. package/dist/tools/image.js.map +1 -0
  136. package/dist/tools/index.d.ts +5 -0
  137. package/dist/tools/index.d.ts.map +1 -0
  138. package/dist/tools/index.js +5 -0
  139. package/dist/tools/index.js.map +1 -0
  140. package/dist/tools/path-utils.d.ts +5 -0
  141. package/dist/tools/path-utils.d.ts.map +1 -0
  142. package/dist/tools/path-utils.js +26 -0
  143. package/dist/tools/path-utils.js.map +1 -0
  144. package/dist/tools/read.d.ts +17 -0
  145. package/dist/tools/read.d.ts.map +1 -0
  146. package/dist/tools/read.js +100 -0
  147. package/dist/tools/read.js.map +1 -0
  148. package/dist/tools/write.d.ts +10 -0
  149. package/dist/tools/write.d.ts.map +1 -0
  150. package/dist/tools/write.js +30 -0
  151. package/dist/tools/write.js.map +1 -0
  152. package/dist/{env/utils/truncate.d.ts → truncate.d.ts} +1 -17
  153. package/dist/truncate.d.ts.map +1 -0
  154. package/dist/truncate.js +146 -0
  155. package/dist/truncate.js.map +1 -0
  156. package/dist/types.d.ts +86 -19
  157. package/dist/types.d.ts.map +1 -1
  158. package/dist/types.js.map +1 -1
  159. package/docs/pico-v5-handoff.md +366 -86
  160. package/docs/pico-v5.md +1373 -283
  161. package/package.json +11 -3
  162. package/dist/env/utils/adaptive-publisher.d.ts +0 -24
  163. package/dist/env/utils/adaptive-publisher.d.ts.map +0 -1
  164. package/dist/env/utils/adaptive-publisher.js +0 -79
  165. package/dist/env/utils/adaptive-publisher.js.map +0 -1
  166. package/dist/env/utils/output-capture.d.ts +0 -31
  167. package/dist/env/utils/output-capture.d.ts.map +0 -1
  168. package/dist/env/utils/output-capture.js +0 -238
  169. package/dist/env/utils/output-capture.js.map +0 -1
  170. package/dist/env/utils/truncate.d.ts.map +0 -1
  171. package/dist/env/utils/truncate.js +0 -294
  172. package/dist/env/utils/truncate.js.map +0 -1
@@ -0,0 +1,261 @@
1
+ import { utf8ByteLength } from "../truncate.js";
2
+ const NEWLINE = 0x0a;
3
+ const INVALID_OUTPUT = /[\x00-\x08\x0b-\x1f\ufff9-\ufffb]/g;
4
+ const encoder = new TextEncoder();
5
+ const decoder = new TextDecoder();
6
+ /** Remove control characters that break display and transcripts; tabs and newlines stay. */
7
+ export function sanitizeOutput(text) {
8
+ return text.replace(INVALID_OUTPUT, "");
9
+ }
10
+ /**
11
+ * Bound `text` to whole lines within the limits: the first lines for `head`, the last lines for `tail`. The result is an
12
+ * exact slice, trailing newline included. A single line longer than `maxBytes` is cut at the byte limit on a character
13
+ * boundary.
14
+ */
15
+ export function boundOutput(text, limits) {
16
+ const bytes = encoder.encode(text);
17
+ const [from, to] = limits.retain === "head" ? headRange(bytes, limits) : tailRange(bytes, limits);
18
+ const kept = bytes.subarray(from, to);
19
+ return {
20
+ text: kept.length === bytes.length ? text : decoder.decode(kept),
21
+ bytes: kept.length,
22
+ droppedBytes: bytes.length - kept.length,
23
+ droppedLines: lineCount(bytes) - lineCount(kept),
24
+ };
25
+ }
26
+ function headRange(bytes, limits) {
27
+ if (limits.maxLines === 0 || limits.maxBytes === 0)
28
+ return [0, 0];
29
+ let end = bytes.length;
30
+ let lines = 0;
31
+ for (let index = bytes.indexOf(NEWLINE); index !== -1; index = bytes.indexOf(NEWLINE, index + 1)) {
32
+ if (++lines === limits.maxLines) {
33
+ end = index + 1;
34
+ break;
35
+ }
36
+ }
37
+ if (end > limits.maxBytes) {
38
+ const newline = bytes.lastIndexOf(NEWLINE, limits.maxBytes - 1);
39
+ end = newline === -1 ? characterEnd(bytes, limits.maxBytes) : newline + 1;
40
+ }
41
+ return [0, end];
42
+ }
43
+ function tailRange(bytes, limits) {
44
+ if (limits.maxLines === 0 || limits.maxBytes === 0)
45
+ return [bytes.length, bytes.length];
46
+ // A trailing newline ends the last line rather than starting another.
47
+ const last = bytes[bytes.length - 1] === NEWLINE ? bytes.length - 2 : bytes.length - 1;
48
+ let start = 0;
49
+ let lines = 1;
50
+ for (let index = last < 0 ? -1 : bytes.lastIndexOf(NEWLINE, last); index !== -1;) {
51
+ if (lines === limits.maxLines) {
52
+ start = index + 1;
53
+ break;
54
+ }
55
+ lines++;
56
+ index = index === 0 ? -1 : bytes.lastIndexOf(NEWLINE, index - 1);
57
+ }
58
+ if (bytes.length - start > limits.maxBytes) {
59
+ const from = bytes.length - limits.maxBytes;
60
+ const newline = bytes.indexOf(NEWLINE, from - 1);
61
+ // The first line starting inside the byte window, or a cut of the last line when it alone is too long.
62
+ start = newline !== -1 && newline + 1 < bytes.length ? newline + 1 : characterStart(bytes, from);
63
+ }
64
+ return [start, bytes.length];
65
+ }
66
+ /** The last character boundary at or before `index`. */
67
+ export function characterEnd(bytes, index) {
68
+ let end = index;
69
+ while (end > 0 && ((bytes[end] ?? 0) & 0xc0) === 0x80)
70
+ end--;
71
+ return end;
72
+ }
73
+ /** The first character boundary at or after `index`. */
74
+ function characterStart(bytes, index) {
75
+ let start = index;
76
+ while (start < bytes.length && ((bytes[start] ?? 0) & 0xc0) === 0x80)
77
+ start++;
78
+ return start;
79
+ }
80
+ function lineCount(bytes) {
81
+ if (bytes.length === 0)
82
+ return 0;
83
+ let newlines = 0;
84
+ for (let index = bytes.indexOf(NEWLINE); index !== -1; index = bytes.indexOf(NEWLINE, index + 1))
85
+ newlines++;
86
+ return newlines + (bytes[bytes.length - 1] === NEWLINE ? 0 : 1);
87
+ }
88
+ /**
89
+ * Bounded running output of one tool call. Accepting a chunk costs time proportional to the chunk: head retention stops
90
+ * storing once the window is full, and tail retention drops stored text the window no longer needs when it snapshots.
91
+ * Counts of the whole stream are kept so the dropped totals stay exact.
92
+ */
93
+ export class OutputBuffer {
94
+ #limits;
95
+ #decoder = new TextDecoder();
96
+ /** Stored chunks: for head the start of the stream, for tail a suffix that still contains the next window. */
97
+ #chunks = [];
98
+ #storedBytes = 0;
99
+ #storedNewlines = 0;
100
+ #full = false;
101
+ #totalBytes = 0;
102
+ #totalNewlines = 0;
103
+ #endsWithNewline = true;
104
+ constructor(limits) {
105
+ this.#limits = limits;
106
+ }
107
+ /** Bytes currently held; bounded by the limits plus one chunk. */
108
+ get storedBytes() {
109
+ return this.#storedBytes;
110
+ }
111
+ /** Accept a chunk; returns whether anything was accepted. */
112
+ push(chunk) {
113
+ // Bytes of an incomplete character from an earlier byte chunk come first.
114
+ const text = typeof chunk === "string" ? this.#decoder.decode() + chunk : this.#decoder.decode(chunk, { stream: true });
115
+ return this.#accept(text);
116
+ }
117
+ /** Flush an incomplete trailing character as a replacement character; call when the stream ends. */
118
+ end() {
119
+ this.#accept(this.#decoder.decode());
120
+ }
121
+ #accept(text) {
122
+ if (text.length === 0)
123
+ return false;
124
+ const bytes = utf8ByteLength(text);
125
+ const newlines = countNewlines(text);
126
+ this.#totalBytes += bytes;
127
+ this.#totalNewlines += newlines;
128
+ this.#endsWithNewline = text.endsWith("\n");
129
+ if (this.#full)
130
+ return true;
131
+ this.#chunks.push({ text, bytes, newlines });
132
+ this.#storedBytes += bytes;
133
+ this.#storedNewlines += newlines;
134
+ if (this.#limits.retain === "head") {
135
+ // Nothing past a full window is ever needed.
136
+ this.#full = this.#storedBytes > this.#limits.maxBytes || this.#storedNewlines >= this.#limits.maxLines;
137
+ return true;
138
+ }
139
+ // Drop leading chunks while the rest still holds more than a window: more than `maxBytes` bytes or `maxLines`
140
+ // newlines, plus one, so the window's line start can still be found. Each chunk is dropped once.
141
+ while (this.#chunks.length > 1) {
142
+ const first = this.#chunks[0];
143
+ const bytesAfter = this.#storedBytes - first.bytes;
144
+ const newlinesAfter = this.#storedNewlines - first.newlines;
145
+ if (bytesAfter <= this.#limits.maxBytes + 1 && newlinesAfter <= this.#limits.maxLines + 1)
146
+ break;
147
+ this.#chunks.shift();
148
+ this.#storedBytes = bytesAfter;
149
+ this.#storedNewlines = newlinesAfter;
150
+ }
151
+ return true;
152
+ }
153
+ /** Retained, sanitized output and what the limits dropped from the whole stream. */
154
+ snapshot() {
155
+ const stored = this.#chunks.length === 1 ? this.#chunks[0].text : this.#chunks.map((chunk) => chunk.text).join("");
156
+ const kept = boundOutput(stored, this.#limits);
157
+ const storedLines = lines(this.#storedNewlines, stored === "" || stored.endsWith("\n"));
158
+ const keptLines = storedLines - kept.droppedLines;
159
+ // Tail windows never reach back before this one, so only the kept slice needs storing.
160
+ if (this.#limits.retain === "tail" || this.#chunks.length > 1) {
161
+ const text = this.#limits.retain === "tail" ? kept.text : stored;
162
+ const bytes = this.#limits.retain === "tail" ? kept.bytes : this.#storedBytes;
163
+ this.#chunks = text === "" ? [] : [{ text, bytes, newlines: countNewlines(text) }];
164
+ this.#storedBytes = bytes;
165
+ this.#storedNewlines = this.#chunks[0]?.newlines ?? 0;
166
+ }
167
+ return {
168
+ text: sanitizeOutput(kept.text),
169
+ droppedBytes: this.#totalBytes - kept.bytes,
170
+ droppedLines: lines(this.#totalNewlines, this.#endsWithNewline) - keptLines,
171
+ };
172
+ }
173
+ }
174
+ /** Lines of text with `newlines` newlines; a final unterminated line counts. */
175
+ function lines(newlines, terminated) {
176
+ return newlines + (terminated ? 0 : 1);
177
+ }
178
+ function countNewlines(text) {
179
+ let count = 0;
180
+ for (let index = text.indexOf("\n"); index !== -1; index = text.indexOf("\n", index + 1))
181
+ count++;
182
+ return count;
183
+ }
184
+ /** Minimum pause between progress commits; each commit also buys a pause proportional to what it wrote. */
185
+ const MIN_PROGRESS_INTERVAL_MS = 100;
186
+ const PROGRESS_BYTES_PER_SECOND = 100 * 1024;
187
+ /**
188
+ * Adaptive progress commits, like the environment's shell output capture: the first change after an idle period
189
+ * commits at once; each commit then delays the next by at least 100 ms and by its written size at 100 KiB/s. At most
190
+ * one commit is in flight; changes made meanwhile coalesce into the next one.
191
+ */
192
+ export class Progress {
193
+ #write;
194
+ #onError;
195
+ #waiters = [];
196
+ #timer;
197
+ #inFlight;
198
+ #nextAt = 0;
199
+ #dirty = false;
200
+ #stopped = false;
201
+ constructor(write, onError) {
202
+ this.#write = write;
203
+ this.#onError = onError;
204
+ }
205
+ /** Schedule a commit. */
206
+ mark() {
207
+ this.#dirty = true;
208
+ this.#schedule();
209
+ }
210
+ /** Schedule a commit; the promise settles with the commit that includes this change. */
211
+ markAndWait() {
212
+ const waiter = Promise.withResolvers();
213
+ this.#waiters.push(waiter);
214
+ this.mark();
215
+ return waiter.promise;
216
+ }
217
+ /** Stop committing and wait for the commit in flight; returns the waiters the final commit must settle. */
218
+ async stop() {
219
+ this.#stopped = true;
220
+ clearTimeout(this.#timer);
221
+ this.#timer = undefined;
222
+ await this.#inFlight;
223
+ return this.#waiters.splice(0);
224
+ }
225
+ #schedule() {
226
+ if (this.#stopped || this.#timer !== undefined || this.#inFlight !== undefined)
227
+ return;
228
+ const wait = this.#nextAt - Date.now();
229
+ if (wait <= 0)
230
+ this.#flush();
231
+ else
232
+ this.#timer = setTimeout(() => {
233
+ this.#timer = undefined;
234
+ this.#flush();
235
+ }, wait);
236
+ }
237
+ #flush() {
238
+ if (this.#stopped || !this.#dirty)
239
+ return;
240
+ this.#dirty = false;
241
+ const waiters = this.#waiters.splice(0);
242
+ const started = Date.now();
243
+ this.#inFlight = this.#write()
244
+ .then((bytes) => {
245
+ this.#nextAt = started + Math.max(MIN_PROGRESS_INTERVAL_MS, (bytes * 1000) / PROGRESS_BYTES_PER_SECOND);
246
+ for (const waiter of waiters)
247
+ waiter.resolve();
248
+ }, (error) => {
249
+ this.#nextAt = started + MIN_PROGRESS_INTERVAL_MS;
250
+ for (const waiter of waiters)
251
+ waiter.reject(error);
252
+ this.#onError(error);
253
+ })
254
+ .finally(() => {
255
+ this.#inFlight = undefined;
256
+ if (this.#dirty)
257
+ this.#schedule();
258
+ });
259
+ }
260
+ }
261
+ //# sourceMappingURL=output.js.map
@@ -0,0 +1 @@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I,CAAC,OAAO,GAAG,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,wBAAwB,EAAE,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,yBAAyB,CAAC,CAAC;YACxG,KAAK,MAAM,MAAM,IAAI,OAAO;gBAAE,MAAM,CAAC,OAAO,EAAE,CAAC;QAChD,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;YAClB,IAAI,CAAC,OAAO,GAAG,OAAO,GAAG,wBAAwB,CAAC;YAClD,KAAK,MAAM,MAAM,IAAI,OAAO;gBAAE,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACnD,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC,CACD;aACA,OAAO,CAAC,GAAG,EAAE;YACb,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;YAC3B,IAAI,IAAI,CAAC,MAAM;gBAAE,IAAI,CAAC,SAAS,EAAE,CAAC;QACnC,CAAC,CAAC,CAAC;IACL,CAAC;CACD","sourcesContent":["import { utf8ByteLength } from \"../truncate.ts\";\n\n/** Retention limits of one tool's output. */\nexport type OutputLimits = { readonly maxBytes: number; readonly maxLines: number; readonly retain: \"head\" | \"tail\" };\n\n/** Retained output and what the limits dropped. */\nexport type BoundedOutput = { readonly text: string; readonly droppedBytes: number; readonly droppedLines: number };\n\n/** An exact slice of the input within the limits, and what it left out. */\nexport type OutputSlice = {\n\treadonly text: string;\n\treadonly bytes: number;\n\treadonly droppedBytes: number;\n\treadonly droppedLines: number;\n};\n\nconst NEWLINE = 0x0a;\nconst INVALID_OUTPUT = /[\\x00-\\x08\\x0b-\\x1f\\ufff9-\\ufffb]/g;\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\n/** Remove control characters that break display and transcripts; tabs and newlines stay. */\nexport function sanitizeOutput(text: string): string {\n\treturn text.replace(INVALID_OUTPUT, \"\");\n}\n\n/**\n * Bound `text` to whole lines within the limits: the first lines for `head`, the last lines for `tail`. The result is an\n * exact slice, trailing newline included. A single line longer than `maxBytes` is cut at the byte limit on a character\n * boundary.\n */\nexport function boundOutput(text: string, limits: OutputLimits): OutputSlice {\n\tconst bytes = encoder.encode(text);\n\tconst [from, to] = limits.retain === \"head\" ? headRange(bytes, limits) : tailRange(bytes, limits);\n\tconst kept = bytes.subarray(from, to);\n\treturn {\n\t\ttext: kept.length === bytes.length ? text : decoder.decode(kept),\n\t\tbytes: kept.length,\n\t\tdroppedBytes: bytes.length - kept.length,\n\t\tdroppedLines: lineCount(bytes) - lineCount(kept),\n\t};\n}\n\nfunction headRange(bytes: Uint8Array, limits: OutputLimits): [number, number] {\n\tif (limits.maxLines === 0 || limits.maxBytes === 0) return [0, 0];\n\tlet end = bytes.length;\n\tlet lines = 0;\n\tfor (let index = bytes.indexOf(NEWLINE); index !== -1; index = bytes.indexOf(NEWLINE, index + 1)) {\n\t\tif (++lines === limits.maxLines) {\n\t\t\tend = index + 1;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (end > limits.maxBytes) {\n\t\tconst newline = bytes.lastIndexOf(NEWLINE, limits.maxBytes - 1);\n\t\tend = newline === -1 ? characterEnd(bytes, limits.maxBytes) : newline + 1;\n\t}\n\treturn [0, end];\n}\n\nfunction tailRange(bytes: Uint8Array, limits: OutputLimits): [number, number] {\n\tif (limits.maxLines === 0 || limits.maxBytes === 0) return [bytes.length, bytes.length];\n\t// A trailing newline ends the last line rather than starting another.\n\tconst last = bytes[bytes.length - 1] === NEWLINE ? bytes.length - 2 : bytes.length - 1;\n\tlet start = 0;\n\tlet lines = 1;\n\tfor (let index = last < 0 ? -1 : bytes.lastIndexOf(NEWLINE, last); index !== -1; ) {\n\t\tif (lines === limits.maxLines) {\n\t\t\tstart = index + 1;\n\t\t\tbreak;\n\t\t}\n\t\tlines++;\n\t\tindex = index === 0 ? -1 : bytes.lastIndexOf(NEWLINE, index - 1);\n\t}\n\tif (bytes.length - start > limits.maxBytes) {\n\t\tconst from = bytes.length - limits.maxBytes;\n\t\tconst newline = bytes.indexOf(NEWLINE, from - 1);\n\t\t// The first line starting inside the byte window, or a cut of the last line when it alone is too long.\n\t\tstart = newline !== -1 && newline + 1 < bytes.length ? newline + 1 : characterStart(bytes, from);\n\t}\n\treturn [start, bytes.length];\n}\n\n/** The last character boundary at or before `index`. */\nexport function characterEnd(bytes: Uint8Array, index: number): number {\n\tlet end = index;\n\twhile (end > 0 && ((bytes[end] ?? 0) & 0xc0) === 0x80) end--;\n\treturn end;\n}\n\n/** The first character boundary at or after `index`. */\nfunction characterStart(bytes: Uint8Array, index: number): number {\n\tlet start = index;\n\twhile (start < bytes.length && ((bytes[start] ?? 0) & 0xc0) === 0x80) start++;\n\treturn start;\n}\n\nfunction lineCount(bytes: Uint8Array): number {\n\tif (bytes.length === 0) return 0;\n\tlet newlines = 0;\n\tfor (let index = bytes.indexOf(NEWLINE); index !== -1; index = bytes.indexOf(NEWLINE, index + 1)) newlines++;\n\treturn newlines + (bytes[bytes.length - 1] === NEWLINE ? 0 : 1);\n}\n\n/**\n * Bounded running output of one tool call. Accepting a chunk costs time proportional to the chunk: head retention stops\n * storing once the window is full, and tail retention drops stored text the window no longer needs when it snapshots.\n * Counts of the whole stream are kept so the dropped totals stay exact.\n */\nexport class OutputBuffer {\n\treadonly #limits: OutputLimits;\n\treadonly #decoder = new TextDecoder();\n\t/** Stored chunks: for head the start of the stream, for tail a suffix that still contains the next window. */\n\t#chunks: { readonly text: string; readonly bytes: number; readonly newlines: number }[] = [];\n\t#storedBytes = 0;\n\t#storedNewlines = 0;\n\t#full = false;\n\t#totalBytes = 0;\n\t#totalNewlines = 0;\n\t#endsWithNewline = true;\n\n\tconstructor(limits: OutputLimits) {\n\t\tthis.#limits = limits;\n\t}\n\n\t/** Bytes currently held; bounded by the limits plus one chunk. */\n\tget storedBytes(): number {\n\t\treturn this.#storedBytes;\n\t}\n\n\t/** Accept a chunk; returns whether anything was accepted. */\n\tpush(chunk: string | Uint8Array): boolean {\n\t\t// Bytes of an incomplete character from an earlier byte chunk come first.\n\t\tconst text =\n\t\t\ttypeof chunk === \"string\" ? this.#decoder.decode() + chunk : this.#decoder.decode(chunk, { stream: true });\n\t\treturn this.#accept(text);\n\t}\n\n\t/** Flush an incomplete trailing character as a replacement character; call when the stream ends. */\n\tend(): void {\n\t\tthis.#accept(this.#decoder.decode());\n\t}\n\n\t#accept(text: string): boolean {\n\t\tif (text.length === 0) return false;\n\t\tconst bytes = utf8ByteLength(text);\n\t\tconst newlines = countNewlines(text);\n\t\tthis.#totalBytes += bytes;\n\t\tthis.#totalNewlines += newlines;\n\t\tthis.#endsWithNewline = text.endsWith(\"\\n\");\n\t\tif (this.#full) return true;\n\t\tthis.#chunks.push({ text, bytes, newlines });\n\t\tthis.#storedBytes += bytes;\n\t\tthis.#storedNewlines += newlines;\n\t\tif (this.#limits.retain === \"head\") {\n\t\t\t// Nothing past a full window is ever needed.\n\t\t\tthis.#full = this.#storedBytes > this.#limits.maxBytes || this.#storedNewlines >= this.#limits.maxLines;\n\t\t\treturn true;\n\t\t}\n\t\t// Drop leading chunks while the rest still holds more than a window: more than `maxBytes` bytes or `maxLines`\n\t\t// newlines, plus one, so the window's line start can still be found. Each chunk is dropped once.\n\t\twhile (this.#chunks.length > 1) {\n\t\t\tconst first = this.#chunks[0]!;\n\t\t\tconst bytesAfter = this.#storedBytes - first.bytes;\n\t\t\tconst newlinesAfter = this.#storedNewlines - first.newlines;\n\t\t\tif (bytesAfter <= this.#limits.maxBytes + 1 && newlinesAfter <= this.#limits.maxLines + 1) break;\n\t\t\tthis.#chunks.shift();\n\t\t\tthis.#storedBytes = bytesAfter;\n\t\t\tthis.#storedNewlines = newlinesAfter;\n\t\t}\n\t\treturn true;\n\t}\n\n\t/** Retained, sanitized output and what the limits dropped from the whole stream. */\n\tsnapshot(): BoundedOutput {\n\t\tconst stored =\n\t\t\tthis.#chunks.length === 1 ? this.#chunks[0]!.text : this.#chunks.map((chunk) => chunk.text).join(\"\");\n\t\tconst kept = boundOutput(stored, this.#limits);\n\t\tconst storedLines = lines(this.#storedNewlines, stored === \"\" || stored.endsWith(\"\\n\"));\n\t\tconst keptLines = storedLines - kept.droppedLines;\n\t\t// Tail windows never reach back before this one, so only the kept slice needs storing.\n\t\tif (this.#limits.retain === \"tail\" || this.#chunks.length > 1) {\n\t\t\tconst text = this.#limits.retain === \"tail\" ? kept.text : stored;\n\t\t\tconst bytes = this.#limits.retain === \"tail\" ? kept.bytes : this.#storedBytes;\n\t\t\tthis.#chunks = text === \"\" ? [] : [{ text, bytes, newlines: countNewlines(text) }];\n\t\t\tthis.#storedBytes = bytes;\n\t\t\tthis.#storedNewlines = this.#chunks[0]?.newlines ?? 0;\n\t\t}\n\t\treturn {\n\t\t\ttext: sanitizeOutput(kept.text),\n\t\t\tdroppedBytes: this.#totalBytes - kept.bytes,\n\t\t\tdroppedLines: lines(this.#totalNewlines, this.#endsWithNewline) - keptLines,\n\t\t};\n\t}\n}\n\n/** Lines of text with `newlines` newlines; a final unterminated line counts. */\nfunction lines(newlines: number, terminated: boolean): number {\n\treturn newlines + (terminated ? 0 : 1);\n}\n\nfunction countNewlines(text: string): number {\n\tlet count = 0;\n\tfor (let index = text.indexOf(\"\\n\"); index !== -1; index = text.indexOf(\"\\n\", index + 1)) count++;\n\treturn count;\n}\n\n/** Minimum pause between progress commits; each commit also buys a pause proportional to what it wrote. */\nconst MIN_PROGRESS_INTERVAL_MS = 100;\nconst PROGRESS_BYTES_PER_SECOND = 100 * 1024;\n\n/**\n * Adaptive progress commits, like the environment's shell output capture: the first change after an idle period\n * commits at once; each commit then delays the next by at least 100 ms and by its written size at 100 KiB/s. At most\n * one commit is in flight; changes made meanwhile coalesce into the next one.\n */\nexport class Progress {\n\treadonly #write: () => Promise<number>;\n\treadonly #onError: (error: unknown) => void;\n\t#waiters: PromiseWithResolvers<void>[] = [];\n\t#timer: ReturnType<typeof setTimeout> | undefined;\n\t#inFlight: Promise<void> | undefined;\n\t#nextAt = 0;\n\t#dirty = false;\n\t#stopped = false;\n\n\tconstructor(write: () => Promise<number>, onError: (error: unknown) => void) {\n\t\tthis.#write = write;\n\t\tthis.#onError = onError;\n\t}\n\n\t/** Schedule a commit. */\n\tmark(): void {\n\t\tthis.#dirty = true;\n\t\tthis.#schedule();\n\t}\n\n\t/** Schedule a commit; the promise settles with the commit that includes this change. */\n\tmarkAndWait(): Promise<void> {\n\t\tconst waiter = Promise.withResolvers<void>();\n\t\tthis.#waiters.push(waiter);\n\t\tthis.mark();\n\t\treturn waiter.promise;\n\t}\n\n\t/** Stop committing and wait for the commit in flight; returns the waiters the final commit must settle. */\n\tasync stop(): Promise<PromiseWithResolvers<void>[]> {\n\t\tthis.#stopped = true;\n\t\tclearTimeout(this.#timer);\n\t\tthis.#timer = undefined;\n\t\tawait this.#inFlight;\n\t\treturn this.#waiters.splice(0);\n\t}\n\n\t#schedule(): void {\n\t\tif (this.#stopped || this.#timer !== undefined || this.#inFlight !== undefined) return;\n\t\tconst wait = this.#nextAt - Date.now();\n\t\tif (wait <= 0) this.#flush();\n\t\telse\n\t\t\tthis.#timer = setTimeout(() => {\n\t\t\t\tthis.#timer = undefined;\n\t\t\t\tthis.#flush();\n\t\t\t}, wait);\n\t}\n\n\t#flush(): void {\n\t\tif (this.#stopped || !this.#dirty) return;\n\t\tthis.#dirty = false;\n\t\tconst waiters = this.#waiters.splice(0);\n\t\tconst started = Date.now();\n\t\tthis.#inFlight = this.#write()\n\t\t\t.then(\n\t\t\t\t(bytes) => {\n\t\t\t\t\tthis.#nextAt = started + Math.max(MIN_PROGRESS_INTERVAL_MS, (bytes * 1000) / PROGRESS_BYTES_PER_SECOND);\n\t\t\t\t\tfor (const waiter of waiters) waiter.resolve();\n\t\t\t\t},\n\t\t\t\t(error: unknown) => {\n\t\t\t\t\tthis.#nextAt = started + MIN_PROGRESS_INTERVAL_MS;\n\t\t\t\t\tfor (const waiter of waiters) waiter.reject(error);\n\t\t\t\t\tthis.#onError(error);\n\t\t\t\t},\n\t\t\t)\n\t\t\t.finally(() => {\n\t\t\t\tthis.#inFlight = undefined;\n\t\t\t\tif (this.#dirty) this.#schedule();\n\t\t\t});\n\t}\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import type { Context } from "@earendil-works/chord";
2
- import type { Message } from "@earendil-works/pi-ai";
2
+ import type { Message, Tool } from "@earendil-works/pi-ai";
3
3
  import type { TypedEntryDraft } from "../types.ts";
4
4
  import type { ContextView, PromptInput, PromptSection, ToolRegistration } from "./types.ts";
5
5
  /** Sections in effect after replaying system messages in order: set in place, `null` deletes, re-adding appends. */
@@ -10,15 +10,20 @@ export declare function replaySections(messages: readonly Message[]): Map<string
10
10
  */
11
11
  export declare function renderSections<Tool extends ToolRegistration>(sections: readonly PromptSection<Tool>[], input: PromptInput<Tool>, shown: ReadonlyMap<string, string>, report: (error: unknown) => void, context: Context): Promise<Map<string, string>>;
12
12
  type SystemDraft = TypedEntryDraft<never>;
13
+ /** Active names the snapshot resolves, first occurrence of each, in configured order, as composed by wrappers. */
14
+ export declare function desiredTools<T extends ToolRegistration>(activeTools: readonly string[], resolve: (name: string) => T | undefined): T[];
13
15
  /**
14
- * Plan the `pi.system` entries that make the replayed sections of `view` equal `desired` in values and order.
16
+ * Plan the `pi.system` entries that make the replayed sections and tools of `view` equal `desired` and `tools` in values
17
+ * and order.
15
18
  *
16
19
  * - A head marker with no later `pi.system` entry in context: one complete baseline that omits every retained earlier
17
20
  * `pi.system` entry, written even when it restates the replayed values.
18
- * - Otherwise, when a minimal patch would leave a different order: remove every shown section, then re-add every
19
- * desired section in order.
21
+ * - Otherwise, when a minimal section patch would leave a different order: remove every shown section, then re-add
22
+ * every desired section in order.
20
23
  * - Otherwise the minimal patch of changed values and `null` removals, or nothing.
24
+ *
25
+ * Tool changes ride on the last planned entry, or on one entry of their own.
21
26
  */
22
- export declare function planSystemEntries(view: ContextView, desired: ReadonlyMap<string, string>, timestamp: number): SystemDraft[];
27
+ export declare function planSystemEntries(view: ContextView, desired: ReadonlyMap<string, string>, tools: readonly Tool[], timestamp: number): SystemDraft[];
23
28
  export {};
24
29
  //# sourceMappingURL=prompt.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"prompt.d.ts","sourceRoot":"","sources":["../../src/harness/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAiB,MAAM,uBAAuB,CAAC;AAEpE,OAAO,KAAK,EAAe,eAAe,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE5F,oHAAoH;AACpH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAUhF;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAAC,IAAI,SAAS,gBAAgB,EACjE,QAAQ,EAAE,SAAS,aAAa,CAAC,IAAI,CAAC,EAAE,EACxC,KAAK,EAAE,WAAW,CAAC,IAAI,CAAC,EACxB,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,EAClC,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,EAChC,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAiB9B;AAED,KAAK,WAAW,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;AAE1C;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAChC,IAAI,EAAE,WAAW,EACjB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,EACpC,SAAS,EAAE,MAAM,GACf,WAAW,EAAE,CA8Bf"}
1
+ {"version":3,"file":"prompt.d.ts","sourceRoot":"","sources":["../../src/harness/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE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earendil-works/pi-ai/utils/transcript";
1
2
  import { SystemEntry } from "../entries.js";
2
3
  /** Sections in effect after replaying system messages in order: set in place, `null` deletes, re-adding appends. */
3
4
  export function replaySections(messages) {
@@ -40,35 +41,80 @@ export async function renderSections(sections, input, shown, report, context) {
40
41
  }
41
42
  return desired;
42
43
  }
44
+ /** Active names the snapshot resolves, first occurrence of each, in configured order, as composed by wrappers. */
45
+ export function desiredTools(activeTools, resolve) {
46
+ const tools = new Map();
47
+ for (const name of activeTools) {
48
+ const tool = tools.has(name) ? undefined : resolve(name);
49
+ if (tool !== undefined)
50
+ tools.set(name, tool);
51
+ }
52
+ return [...tools.values()];
53
+ }
43
54
  /**
44
- * Plan the `pi.system` entries that make the replayed sections of `view` equal `desired` in values and order.
55
+ * Plan the `pi.system` entries that make the replayed sections and tools of `view` equal `desired` and `tools` in values
56
+ * and order.
45
57
  *
46
58
  * - A head marker with no later `pi.system` entry in context: one complete baseline that omits every retained earlier
47
59
  * `pi.system` entry, written even when it restates the replayed values.
48
- * - Otherwise, when a minimal patch would leave a different order: remove every shown section, then re-add every
49
- * desired section in order.
60
+ * - Otherwise, when a minimal section patch would leave a different order: remove every shown section, then re-add
61
+ * every desired section in order.
50
62
  * - Otherwise the minimal patch of changed values and `null` removals, or nothing.
63
+ *
64
+ * Tool changes ride on the last planned entry, or on one entry of their own.
51
65
  */
52
- export function planSystemEntries(view, desired, timestamp) {
66
+ export function planSystemEntries(view, desired, tools, timestamp) {
53
67
  const head = view.head;
54
68
  if (head !== undefined && !view.entries.some((entry) => SystemEntry.is(entry) && entry.id > head.id)) {
55
69
  const edits = view.entries
56
70
  .filter((entry) => SystemEntry.is(entry))
57
71
  .map((entry) => ({ target: entry.id, action: "omit" }));
58
- const baseline = systemEntry(Object.fromEntries(desired), timestamp);
72
+ const baseline = systemEntry(Object.fromEntries(desired), { toolsRemoved: [], toolsAdded: tools.map(toToolDeclaration) }, timestamp);
59
73
  return [edits.length === 0 ? baseline : { ...baseline, edits }];
60
74
  }
61
- const shown = replaySections(view.messages);
75
+ const sections = planSections(replaySections(view.messages), desired);
76
+ const changes = planTools(getCurrentTools(view.messages), tools);
77
+ if (changes.toolsRemoved.length === 0 && changes.toolsAdded.length === 0) {
78
+ return sections.map((patch) => systemEntry(patch, undefined, timestamp));
79
+ }
80
+ if (sections.length === 0)
81
+ return [systemEntry(undefined, changes, timestamp)];
82
+ return sections.map((patch, index) => systemEntry(patch, index === sections.length - 1 ? changes : undefined, timestamp));
83
+ }
84
+ /**
85
+ * Tool changes from `offered` to `desired`. A changed declaration is removed and re-added. Replay keeps retained tools
86
+ * in place and appends additions; when that would not yield the desired order, every offered tool is removed and
87
+ * every desired tool re-added in order.
88
+ */
89
+ function planTools(offered, desired) {
90
+ const wanted = new Map(desired.map((tool) => [tool.name, tool]));
91
+ const kept = offered.filter((tool) => {
92
+ const next = wanted.get(tool.name);
93
+ return next !== undefined && declarationsEqual(tool, next);
94
+ });
95
+ const keptNames = new Set(kept.map((tool) => tool.name));
96
+ const added = desired.filter((tool) => !keptNames.has(tool.name));
97
+ const replayed = [...kept, ...added];
98
+ if (replayed.some((tool, index) => tool.name !== desired[index].name)) {
99
+ return {
100
+ toolsRemoved: offered.map((tool) => ({ name: tool.name })),
101
+ toolsAdded: desired.map(toToolDeclaration),
102
+ };
103
+ }
104
+ return {
105
+ toolsRemoved: offered.filter((tool) => !keptNames.has(tool.name)).map((tool) => ({ name: tool.name })),
106
+ toolsAdded: added.map(toToolDeclaration),
107
+ };
108
+ }
109
+ /** Section patches: none, the minimal patch, or a remove-all/re-add-all pair when the order would differ. */
110
+ function planSections(shown, desired) {
62
111
  const patchedOrder = [
63
112
  ...[...shown.keys()].filter((key) => desired.has(key)),
64
113
  ...[...desired.keys()].filter((key) => !shown.has(key)),
65
114
  ];
66
115
  const desiredOrder = [...desired.keys()];
67
116
  if (patchedOrder.some((key, index) => key !== desiredOrder[index])) {
68
- return [
69
- systemEntry(Object.fromEntries([...shown.keys()].map((key) => [key, null])), timestamp),
70
- systemEntry(Object.fromEntries(desired), timestamp),
71
- ];
117
+ return [Object.fromEntries([...shown.keys()].map((key) => [key, null])), Object.fromEntries(desired)];
72
118
  }
73
119
  const patch = {};
74
120
  for (const [key, value] of shown) {
@@ -79,10 +125,17 @@ export function planSystemEntries(view, desired, timestamp) {
79
125
  for (const [key, value] of desired)
80
126
  if (!shown.has(key))
81
127
  patch[key] = value;
82
- return Object.keys(patch).length === 0 ? [] : [systemEntry(patch, timestamp)];
128
+ return Object.keys(patch).length === 0 ? [] : [patch];
83
129
  }
84
- function systemEntry(sections, timestamp) {
85
- const message = { role: "system", content: "", sections, timestamp };
130
+ function systemEntry(sections, tools, timestamp) {
131
+ const message = {
132
+ role: "system",
133
+ content: "",
134
+ ...(sections === undefined ? {} : { sections }),
135
+ ...(tools === undefined || tools.toolsRemoved.length === 0 ? {} : { toolsRemoved: tools.toolsRemoved }),
136
+ ...(tools === undefined || tools.toolsAdded.length === 0 ? {} : { toolsAdded: tools.toolsAdded }),
137
+ timestamp,
138
+ };
86
139
  return { model: [message] };
87
140
  }
88
141
  //# sourceMappingURL=prompt.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../src/harness/prompt.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAI5C,oHAAoH;AACpH,MAAM,UAAU,cAAc,CAAC,QAA4B;IAC1D,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC;IACxC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAChC,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,CAAC,QAAQ,KAAK,SAAS;YAAE,SAAS;QAC1E,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC7D,IAAI,KAAK,KAAK,IAAI;gBAAE,KAA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O,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;AAC7B,CAAC","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport type { Message, SystemMessage } from \"@earendil-works/pi-ai\";\nimport { SystemEntry } from \"../entries.ts\";\nimport type { ContextEdit, TypedEntryDraft } from \"../types.ts\";\nimport type { ContextView, PromptInput, PromptSection, ToolRegistration } from \"./types.ts\";\n\n/** Sections in effect after replaying system messages in order: set in place, `null` deletes, re-adding appends. */\nexport function replaySections(messages: readonly Message[]): Map<string, string> {\n\tconst shown = new Map<string, string>();\n\tfor (const message of messages) {\n\t\tif (message.role !== \"system\" || message.sections === undefined) continue;\n\t\tfor (const [key, value] of Object.entries(message.sections)) {\n\t\t\tif (value === null) shown.delete(key);\n\t\t\telse shown.set(key, value);\n\t\t}\n\t}\n\treturn shown;\n}\n\n/**\n * Render sections in registry order. `undefined` omits a section; tagged text is wrapped as `<key>\\n...\\n</key>`. A\n * section that throws keeps its shown text, if any, and is reported; errors after `context` is aborted propagate.\n */\nexport async function renderSections<Tool extends ToolRegistration>(\n\tsections: readonly PromptSection<Tool>[],\n\tinput: PromptInput<Tool>,\n\tshown: ReadonlyMap<string, string>,\n\treport: (error: unknown) => void,\n\tcontext: Context,\n): Promise<Map<string, string>> {\n\tconst desired = new Map<string, string>();\n\tfor (const section of sections) {\n\t\tlet text: string | undefined;\n\t\ttry {\n\t\t\ttext = await section.render(input, context);\n\t\t} catch (error) {\n\t\t\tif (context.abortSignal?.aborted) throw error;\n\t\t\treport(error);\n\t\t\tconst kept = shown.get(section.key);\n\t\t\tif (kept !== undefined) desired.set(section.key, kept);\n\t\t\tcontinue;\n\t\t}\n\t\tif (text === undefined) continue;\n\t\tdesired.set(section.key, section.tag === false ? text : `<${section.key}>\\n${text}\\n</${section.key}>`);\n\t}\n\treturn desired;\n}\n\ntype SystemDraft = TypedEntryDraft<never>;\n\n/**\n * Plan the `pi.system` entries that make the replayed sections of `view` equal `desired` in values and order.\n *\n * - A head marker with no later `pi.system` entry in context: one complete baseline that omits every retained earlier\n * `pi.system` entry, written even when it restates the replayed values.\n * - Otherwise, when a minimal patch would leave a different order: remove every shown section, then re-add every\n * desired section in order.\n * - Otherwise the minimal patch of changed values and `null` removals, or nothing.\n */\nexport function planSystemEntries(\n\tview: ContextView,\n\tdesired: ReadonlyMap<string, string>,\n\ttimestamp: number,\n): SystemDraft[] {\n\tconst head = view.head;\n\tif (head !== undefined && !view.entries.some((entry) => SystemEntry.is(entry) && entry.id > head.id)) {\n\t\tconst edits: ContextEdit[] = view.entries\n\t\t\t.filter((entry) => SystemEntry.is(entry))\n\t\t\t.map((entry) => ({ target: entry.id, action: \"omit\" }));\n\t\tconst baseline = systemEntry(Object.fromEntries(desired), timestamp);\n\t\treturn [edits.length === 0 ? baseline : { ...baseline, edits }];\n\t}\n\n\tconst shown = replaySections(view.messages);\n\tconst patchedOrder = [\n\t\t...[...shown.keys()].filter((key) => desired.has(key)),\n\t\t...[...desired.keys()].filter((key) => !shown.has(key)),\n\t];\n\tconst desiredOrder = [...desired.keys()];\n\tif (patchedOrder.some((key, index) => key !== desiredOrder[index])) {\n\t\treturn [\n\t\t\tsystemEntry(Object.fromEntries([...shown.keys()].map((key) => [key, null])), timestamp),\n\t\t\tsystemEntry(Object.fromEntries(desired), timestamp),\n\t\t];\n\t}\n\n\tconst patch: Record<string, string | null> = {};\n\tfor (const [key, value] of shown) {\n\t\tconst next = desired.get(key);\n\t\tif (next !== value) patch[key] = next ?? null;\n\t}\n\tfor (const [key, value] of desired) if (!shown.has(key)) patch[key] = value;\n\treturn Object.keys(patch).length === 0 ? [] : [systemEntry(patch, timestamp)];\n}\n\nfunction systemEntry(sections: Record<string, string | null>, timestamp: number): SystemDraft {\n\tconst message: SystemMessage = { role: \"system\", content: \"\", sections, timestamp };\n\treturn { model: [message] };\n}\n"]}
1
+ {"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../src/harness/prompt.ts"],"names":[],"mappings":"AAEA,OAAO,E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AE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,CAAC,UAAU,EAAE,CAAC;QACjG,SAAS;KACT,CAAC;IACF,OAAO,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;AAC7B,CAAC","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport type { Message, SystemMessage, Tool, ToolReference } from \"@earendil-works/pi-ai\";\nimport { declarationsEqual, getCurrentTools, toToolDeclaration } from \"@earendil-works/pi-ai/utils/transcript\";\nimport { SystemEntry } from \"../entries.ts\";\nimport type { ContextEdit, TypedEntryDraft } from \"../types.ts\";\nimport type { ContextView, PromptInput, PromptSection, ToolRegistration } from \"./types.ts\";\n\n/** Sections in effect after replaying system messages in order: set in place, `null` deletes, re-adding appends. */\nexport function replaySections(messages: readonly Message[]): Map<string, string> {\n\tconst shown = new Map<string, string>();\n\tfor (const message of messages) {\n\t\tif (message.role !== \"system\" || message.sections === undefined) continue;\n\t\tfor (const [key, value] of Object.entries(message.sections)) {\n\t\t\tif (value === null) shown.delete(key);\n\t\t\telse shown.set(key, value);\n\t\t}\n\t}\n\treturn shown;\n}\n\n/**\n * Render sections in registry order. `undefined` omits a section; tagged text is wrapped as `<key>\\n...\\n</key>`. A\n * section that throws keeps its shown text, if any, and is reported; errors after `context` is aborted propagate.\n */\nexport async function renderSections<Tool extends ToolRegistration>(\n\tsections: readonly PromptSection<Tool>[],\n\tinput: PromptInput<Tool>,\n\tshown: ReadonlyMap<string, string>,\n\treport: (error: unknown) => void,\n\tcontext: Context,\n): Promise<Map<string, string>> {\n\tconst desired = new Map<string, string>();\n\tfor (const section of sections) {\n\t\tlet text: string | undefined;\n\t\ttry {\n\t\t\ttext = await section.render(input, context);\n\t\t} catch (error) {\n\t\t\tif (context.abortSignal?.aborted) throw error;\n\t\t\treport(error);\n\t\t\tconst kept = shown.get(section.key);\n\t\t\tif (kept !== undefined) desired.set(section.key, kept);\n\t\t\tcontinue;\n\t\t}\n\t\tif (text === undefined) continue;\n\t\tdesired.set(section.key, section.tag === false ? text : `<${section.key}>\\n${text}\\n</${section.key}>`);\n\t}\n\treturn desired;\n}\n\ntype SystemDraft = TypedEntryDraft<never>;\n\n/** Active names the snapshot resolves, first occurrence of each, in configured order, as composed by wrappers. */\nexport function desiredTools<T extends ToolRegistration>(\n\tactiveTools: readonly string[],\n\tresolve: (name: string) => T | undefined,\n): T[] {\n\tconst tools = new Map<string, T>();\n\tfor (const name of activeTools) {\n\t\tconst tool = tools.has(name) ? undefined : resolve(name);\n\t\tif (tool !== undefined) tools.set(name, tool);\n\t}\n\treturn [...tools.values()];\n}\n\ntype ToolChanges = { readonly toolsRemoved: ToolReference[]; readonly toolsAdded: Tool[] };\n\n/**\n * Plan the `pi.system` entries that make the replayed sections and tools of `view` equal `desired` and `tools` in values\n * and order.\n *\n * - A head marker with no later `pi.system` entry in context: one complete baseline that omits every retained earlier\n * `pi.system` entry, written even when it restates the replayed values.\n * - Otherwise, when a minimal section patch would leave a different order: remove every shown section, then re-add\n * every desired section in order.\n * - Otherwise the minimal patch of changed values and `null` removals, or nothing.\n *\n * Tool changes ride on the last planned entry, or on one entry of their own.\n */\nexport function planSystemEntries(\n\tview: ContextView,\n\tdesired: ReadonlyMap<string, string>,\n\ttools: readonly Tool[],\n\ttimestamp: number,\n): SystemDraft[] {\n\tconst head = view.head;\n\tif (head !== undefined && !view.entries.some((entry) => SystemEntry.is(entry) && entry.id > head.id)) {\n\t\tconst edits: ContextEdit[] = view.entries\n\t\t\t.filter((entry) => SystemEntry.is(entry))\n\t\t\t.map((entry) => ({ target: entry.id, action: \"omit\" }));\n\t\tconst baseline = systemEntry(\n\t\t\tObject.fromEntries(desired),\n\t\t\t{ toolsRemoved: [], toolsAdded: tools.map(toToolDeclaration) },\n\t\t\ttimestamp,\n\t\t);\n\t\treturn [edits.length === 0 ? baseline : { ...baseline, edits }];\n\t}\n\tconst sections = planSections(replaySections(view.messages), desired);\n\tconst changes = planTools(getCurrentTools(view.messages), tools);\n\tif (changes.toolsRemoved.length === 0 && changes.toolsAdded.length === 0) {\n\t\treturn sections.map((patch) => systemEntry(patch, undefined, timestamp));\n\t}\n\tif (sections.length === 0) return [systemEntry(undefined, changes, timestamp)];\n\treturn sections.map((patch, index) =>\n\t\tsystemEntry(patch, index === sections.length - 1 ? changes : undefined, timestamp),\n\t);\n}\n\n/**\n * Tool changes from `offered` to `desired`. A changed declaration is removed and re-added. Replay keeps retained tools\n * in place and appends additions; when that would not yield the desired order, every offered tool is removed and\n * every desired tool re-added in order.\n */\nfunction planTools(offered: readonly Tool[], desired: readonly Tool[]): ToolChanges {\n\tconst wanted = new Map(desired.map((tool) => [tool.name, tool]));\n\tconst kept = offered.filter((tool) => {\n\t\tconst next = wanted.get(tool.name);\n\t\treturn next !== undefined && declarationsEqual(tool, next);\n\t});\n\tconst keptNames = new Set(kept.map((tool) => tool.name));\n\tconst added = desired.filter((tool) => !keptNames.has(tool.name));\n\tconst replayed = [...kept, ...added];\n\tif (replayed.some((tool, index) => tool.name !== desired[index]!.name)) {\n\t\treturn {\n\t\t\ttoolsRemoved: offered.map((tool) => ({ name: tool.name })),\n\t\t\ttoolsAdded: desired.map(toToolDeclaration),\n\t\t};\n\t}\n\treturn {\n\t\ttoolsRemoved: offered.filter((tool) => !keptNames.has(tool.name)).map((tool) => ({ name: tool.name })),\n\t\ttoolsAdded: added.map(toToolDeclaration),\n\t};\n}\n\n/** Section patches: none, the minimal patch, or a remove-all/re-add-all pair when the order would differ. */\nfunction planSections(\n\tshown: ReadonlyMap<string, string>,\n\tdesired: ReadonlyMap<string, string>,\n): Record<string, string | null>[] {\n\tconst patchedOrder = [\n\t\t...[...shown.keys()].filter((key) => desired.has(key)),\n\t\t...[...desired.keys()].filter((key) => !shown.has(key)),\n\t];\n\tconst desiredOrder = [...desired.keys()];\n\tif (patchedOrder.some((key, index) => key !== desiredOrder[index])) {\n\t\treturn [Object.fromEntries([...shown.keys()].map((key) => [key, null])), Object.fromEntries(desired)];\n\t}\n\n\tconst patch: Record<string, string | null> = {};\n\tfor (const [key, value] of shown) {\n\t\tconst next = desired.get(key);\n\t\tif (next !== value) patch[key] = next ?? null;\n\t}\n\tfor (const [key, value] of desired) if (!shown.has(key)) patch[key] = value;\n\treturn Object.keys(patch).length === 0 ? [] : [patch];\n}\n\nfunction systemEntry(\n\tsections: Record<string, string | null> | undefined,\n\ttools: ToolChanges | undefined,\n\ttimestamp: number,\n): SystemDraft {\n\tconst message: SystemMessage = {\n\t\trole: \"system\",\n\t\tcontent: \"\",\n\t\t...(sections === undefined ? {} : { sections }),\n\t\t...(tools === undefined || tools.toolsRemoved.length === 0 ? {} : { toolsRemoved: tools.toolsRemoved }),\n\t\t...(tools === undefined || tools.toolsAdded.length === 0 ? {} : { toolsAdded: tools.toolsAdded }),\n\t\ttimestamp,\n\t};\n\treturn { model: [message] };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/harness/registry.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACX,OAAO,EAQP,QAAQ,EAGR,gBAAgB,EAEhB,MAAM,YAAY,CAAC;AAIpB,iGAAiG;AACjG,eAAO,MAAM,aAAa,EAAE,SAAS,OAAO,EAAqB,CAAC;AAElE,eAAO,MAAM,iBAAiB,OAAO,CAAC;AAgUtC,uEAAuE;AACvE,wBAAgB,cAAc,CAAC,IAAI,SAAS,gBAAgB,GAAG,gBAAgB,KAAK,QAAQ,CAAC,IAAI,CAAC,CAMjG"}
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/harness/registry.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACX,OAAO,EAQP,QAAQ,EAGR,gBAAgB,EAEhB,MAAM,YAAY,CAAC;AAKpB,iGAAiG;AACjG,eAAO,MAAM,aAAa,EAAE,SAAS,OAAO,EAA+C,CAAC;AAE5F,eAAO,MAAM,iBAAiB,OAAO,CAAC;AAqUtC,uEAAuE;AACvE,wBAAgB,cAAc,CAAC,IAAI,SAAS,gBAAgB,GAAG,gBAAgB,KAAK,QAAQ,CAAC,IAAI,CAAC,CAMjG"}
@@ -1,13 +1,22 @@
1
+ import { CompactionTask } from "./compaction.js";
1
2
  import { ConversationConfig } from "./config.js";
2
3
  import { GenerationTask } from "./generation.js";
4
+ import { InboxDoc } from "./inbox.js";
3
5
  import { LiveDoc } from "./live.js";
6
+ import { ToolTask } from "./tool.js";
7
+ import { UsageDoc } from "./usage.js";
4
8
  const SECTION_KEY = /^[a-z][a-z0-9_-]*$/;
5
9
  /** Built-in task definitions every registry starts with; they cannot be disposed or replaced. */
6
- export const BUILTIN_TASKS = [GenerationTask];
10
+ export const BUILTIN_TASKS = [GenerationTask, ToolTask, CompactionTask];
7
11
  export const BUILTIN_SETUP_KEY = "pi";
8
- /** Built-in documents: an empty `pi.live`, and for a new conversation the default configuration with every registered tool active. */
12
+ /**
13
+ * Built-in documents: empty `pi.live`, `pi.inbox`, and `pi.usage`, and for a new conversation the default
14
+ * configuration with every registered tool active.
15
+ */
9
16
  async function builtinSetup(tx, conversation, registry) {
10
17
  await tx.doc(LiveDoc, conversation.id);
18
+ await tx.doc(InboxDoc, conversation.id);
19
+ await tx.doc(UsageDoc, conversation.id);
11
20
  if (conversation.parent !== undefined)
12
21
  return;
13
22
  (await tx.doc(ConversationConfig, conversation.id)).activeTools = [...registry.toolNames()];