@earendil-works/pi-durable 1.2.0 → 1.4.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 (142) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +128 -3
  3. package/dist/codemode/codemode.d.ts.map +1 -1
  4. package/dist/codemode/codemode.js +12 -4
  5. package/dist/codemode/codemode.js.map +1 -1
  6. package/dist/codemode/models.d.ts.map +1 -1
  7. package/dist/codemode/models.js +2 -0
  8. package/dist/codemode/models.js.map +1 -1
  9. package/dist/documents.d.ts +1 -1
  10. package/dist/documents.d.ts.map +1 -1
  11. package/dist/documents.js.map +1 -1
  12. package/dist/env/index.d.ts +1 -0
  13. package/dist/env/index.d.ts.map +1 -1
  14. package/dist/env/index.js.map +1 -1
  15. package/dist/env/node.d.ts.map +1 -1
  16. package/dist/env/node.js +11 -3
  17. package/dist/env/node.js.map +1 -1
  18. package/dist/harness/agent.d.ts +13 -2
  19. package/dist/harness/agent.d.ts.map +1 -1
  20. package/dist/harness/agent.js +39 -5
  21. package/dist/harness/agent.js.map +1 -1
  22. package/dist/harness/compaction.d.ts +3 -1
  23. package/dist/harness/compaction.d.ts.map +1 -1
  24. package/dist/harness/compaction.js +19 -4
  25. package/dist/harness/compaction.js.map +1 -1
  26. package/dist/harness/context.d.ts +5 -0
  27. package/dist/harness/context.d.ts.map +1 -1
  28. package/dist/harness/context.js +46 -12
  29. package/dist/harness/context.js.map +1 -1
  30. package/dist/harness/generation.d.ts.map +1 -1
  31. package/dist/harness/generation.js +67 -27
  32. package/dist/harness/generation.js.map +1 -1
  33. package/dist/harness/harness.d.ts +5 -2
  34. package/dist/harness/harness.d.ts.map +1 -1
  35. package/dist/harness/harness.js +78 -13
  36. package/dist/harness/harness.js.map +1 -1
  37. package/dist/harness/inbox.d.ts +2 -2
  38. package/dist/harness/inbox.d.ts.map +1 -1
  39. package/dist/harness/inbox.js +3 -3
  40. package/dist/harness/inbox.js.map +1 -1
  41. package/dist/harness/prompt.d.ts +1 -1
  42. package/dist/harness/prompt.d.ts.map +1 -1
  43. package/dist/harness/prompt.js +1 -1
  44. package/dist/harness/prompt.js.map +1 -1
  45. package/dist/harness/scheduler.d.ts +55 -10
  46. package/dist/harness/scheduler.d.ts.map +1 -1
  47. package/dist/harness/scheduler.js +617 -124
  48. package/dist/harness/scheduler.js.map +1 -1
  49. package/dist/harness/submissions.d.ts +7 -2
  50. package/dist/harness/submissions.d.ts.map +1 -1
  51. package/dist/harness/submissions.js +48 -17
  52. package/dist/harness/submissions.js.map +1 -1
  53. package/dist/harness/task-graph.d.ts +18 -1
  54. package/dist/harness/task-graph.d.ts.map +1 -1
  55. package/dist/harness/task-graph.js +50 -11
  56. package/dist/harness/task-graph.js.map +1 -1
  57. package/dist/harness/telemetry.d.ts +307 -0
  58. package/dist/harness/telemetry.d.ts.map +1 -0
  59. package/dist/harness/telemetry.js +136 -0
  60. package/dist/harness/telemetry.js.map +1 -0
  61. package/dist/harness/tool.d.ts.map +1 -1
  62. package/dist/harness/tool.js +54 -8
  63. package/dist/harness/tool.js.map +1 -1
  64. package/dist/harness/types.d.ts +110 -15
  65. package/dist/harness/types.d.ts.map +1 -1
  66. package/dist/harness/types.js.map +1 -1
  67. package/dist/harness/util.d.ts +4 -0
  68. package/dist/harness/util.d.ts.map +1 -1
  69. package/dist/harness/util.js +4 -0
  70. package/dist/harness/util.js.map +1 -1
  71. package/dist/index.d.ts +6 -4
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +3 -2
  74. package/dist/index.js.map +1 -1
  75. package/dist/session/session.d.ts +17 -6
  76. package/dist/session/session.d.ts.map +1 -1
  77. package/dist/session/session.js +64 -32
  78. package/dist/session/session.js.map +1 -1
  79. package/dist/session/transaction.d.ts +5 -1
  80. package/dist/session/transaction.d.ts.map +1 -1
  81. package/dist/session/transaction.js +26 -1
  82. package/dist/session/transaction.js.map +1 -1
  83. package/dist/storage/indexeddb.d.ts +60 -0
  84. package/dist/storage/indexeddb.d.ts.map +1 -0
  85. package/dist/storage/indexeddb.js +831 -0
  86. package/dist/storage/indexeddb.js.map +1 -0
  87. package/dist/storage/jsonl/storage.d.ts +16 -0
  88. package/dist/storage/jsonl/storage.d.ts.map +1 -1
  89. package/dist/storage/memory.js +1 -1
  90. package/dist/storage/memory.js.map +1 -1
  91. package/dist/storage/sqlite/cloudflare.d.ts +6 -43
  92. package/dist/storage/sqlite/cloudflare.d.ts.map +1 -1
  93. package/dist/storage/sqlite/cloudflare.js +6 -101
  94. package/dist/storage/sqlite/cloudflare.js.map +1 -1
  95. package/dist/storage/sqlite/database.d.ts +1 -36
  96. package/dist/storage/sqlite/database.d.ts.map +1 -1
  97. package/dist/storage/sqlite/database.js.map +1 -1
  98. package/dist/storage/sqlite/index.d.ts +2 -2
  99. package/dist/storage/sqlite/index.d.ts.map +1 -1
  100. package/dist/storage/sqlite/index.js.map +1 -1
  101. package/dist/storage/sqlite/migrations.d.ts +7 -6
  102. package/dist/storage/sqlite/migrations.d.ts.map +1 -1
  103. package/dist/storage/sqlite/migrations.js +116 -106
  104. package/dist/storage/sqlite/migrations.js.map +1 -1
  105. package/dist/storage/sqlite/node.d.ts +6 -39
  106. package/dist/storage/sqlite/node.d.ts.map +1 -1
  107. package/dist/storage/sqlite/node.js +8 -175
  108. package/dist/storage/sqlite/node.js.map +1 -1
  109. package/dist/storage/sqlite/schema.d.ts +45 -0
  110. package/dist/storage/sqlite/schema.d.ts.map +1 -0
  111. package/dist/storage/sqlite/schema.js +50 -0
  112. package/dist/storage/sqlite/schema.js.map +1 -0
  113. package/dist/storage/sqlite/storage.d.ts +10 -2
  114. package/dist/storage/sqlite/storage.d.ts.map +1 -1
  115. package/dist/storage/sqlite/storage.js +46 -43
  116. package/dist/storage/sqlite/storage.js.map +1 -1
  117. package/dist/testing/storage-conformance.d.ts.map +1 -1
  118. package/dist/testing/storage-conformance.js +17 -0
  119. package/dist/testing/storage-conformance.js.map +1 -1
  120. package/dist/timers.d.ts +21 -0
  121. package/dist/timers.d.ts.map +1 -0
  122. package/dist/timers.js +72 -0
  123. package/dist/timers.js.map +1 -0
  124. package/dist/tools/image-processor.d.ts +4 -1
  125. package/dist/tools/image-processor.d.ts.map +1 -1
  126. package/dist/tools/image-processor.js +8 -18
  127. package/dist/tools/image-processor.js.map +1 -1
  128. package/dist/tools/read.d.ts.map +1 -1
  129. package/dist/tools/read.js +1 -1
  130. package/dist/tools/read.js.map +1 -1
  131. package/dist/truncate.d.ts +3 -2
  132. package/dist/truncate.d.ts.map +1 -1
  133. package/dist/truncate.js +4 -34
  134. package/dist/truncate.js.map +1 -1
  135. package/dist/types.d.ts +130 -9
  136. package/dist/types.d.ts.map +1 -1
  137. package/dist/types.js.map +1 -1
  138. package/package.json +15 -5
  139. package/dist/harness/output.d.ts +0 -69
  140. package/dist/harness/output.d.ts.map +0 -1
  141. package/dist/harness/output.js +0 -318
  142. package/dist/harness/output.js.map +0 -1
@@ -1,318 +0,0 @@
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
- /** Slices decode exactly: a U+FEFF at a slice's start is text, not a byte-order mark. */
6
- const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
7
- /** Remove control characters that break display and transcripts; tabs and newlines stay. */
8
- export function sanitizeOutput(text) {
9
- return text.replace(INVALID_OUTPUT, "");
10
- }
11
- /**
12
- * Bound `text` to whole lines within the limits: the first lines for `head`, the last lines for `tail`. The result is an
13
- * exact slice, trailing newline included. A single line longer than `maxBytes` is cut at the byte limit on a character
14
- * boundary.
15
- */
16
- export function boundOutput(text, limits) {
17
- const bytes = encoder.encode(text);
18
- const [from, to] = limits.retain === "head" ? headRange(bytes, limits) : tailRange(bytes, limits);
19
- const kept = bytes.subarray(from, to);
20
- return {
21
- text: kept.length === bytes.length ? text : decoder.decode(kept),
22
- bytes: kept.length,
23
- droppedBytes: bytes.length - kept.length,
24
- droppedLines: lineCount(bytes) - lineCount(kept),
25
- };
26
- }
27
- function headRange(bytes, limits) {
28
- if (limits.maxLines === 0 || limits.maxBytes === 0)
29
- return [0, 0];
30
- let end = bytes.length;
31
- let lines = 0;
32
- for (let index = bytes.indexOf(NEWLINE); index !== -1; index = bytes.indexOf(NEWLINE, index + 1)) {
33
- if (++lines === limits.maxLines) {
34
- end = index + 1;
35
- break;
36
- }
37
- }
38
- if (end > limits.maxBytes) {
39
- const newline = bytes.lastIndexOf(NEWLINE, limits.maxBytes - 1);
40
- end = newline === -1 ? characterEnd(bytes, limits.maxBytes) : newline + 1;
41
- }
42
- return [0, end];
43
- }
44
- function tailRange(bytes, limits) {
45
- if (limits.maxLines === 0 || limits.maxBytes === 0)
46
- return [bytes.length, bytes.length];
47
- // A trailing newline ends the last line rather than starting another.
48
- const last = bytes[bytes.length - 1] === NEWLINE ? bytes.length - 2 : bytes.length - 1;
49
- let start = 0;
50
- let lines = 1;
51
- for (let index = last < 0 ? -1 : bytes.lastIndexOf(NEWLINE, last); index !== -1;) {
52
- if (lines === limits.maxLines) {
53
- start = index + 1;
54
- break;
55
- }
56
- lines++;
57
- index = index === 0 ? -1 : bytes.lastIndexOf(NEWLINE, index - 1);
58
- }
59
- if (bytes.length - start > limits.maxBytes) {
60
- const from = bytes.length - limits.maxBytes;
61
- const newline = bytes.indexOf(NEWLINE, from - 1);
62
- // The first line starting inside the byte window, or a cut of the last line when it alone is too long.
63
- start = newline !== -1 && newline + 1 < bytes.length ? newline + 1 : characterStart(bytes, from);
64
- }
65
- return [start, bytes.length];
66
- }
67
- /** The last character boundary at or before `index`. */
68
- export function characterEnd(bytes, index) {
69
- let end = index;
70
- while (end > 0 && ((bytes[end] ?? 0) & 0xc0) === 0x80)
71
- end--;
72
- return end;
73
- }
74
- /** The first character boundary at or after `index`. */
75
- function characterStart(bytes, index) {
76
- let start = index;
77
- while (start < bytes.length && ((bytes[start] ?? 0) & 0xc0) === 0x80)
78
- start++;
79
- return start;
80
- }
81
- function lineCount(bytes) {
82
- if (bytes.length === 0)
83
- return 0;
84
- let newlines = 0;
85
- for (let index = bytes.indexOf(NEWLINE); index !== -1; index = bytes.indexOf(NEWLINE, index + 1))
86
- newlines++;
87
- return newlines + (bytes[bytes.length - 1] === NEWLINE ? 0 : 1);
88
- }
89
- /**
90
- * Bounded running output of one tool call. Accepting a chunk costs time proportional to the chunk: head retention stops
91
- * storing once the window is full, and tail retention drops stored text the window no longer needs when it snapshots.
92
- * Counts of the whole stream are kept so the dropped totals stay exact.
93
- */
94
- export class OutputBuffer {
95
- #limits;
96
- /**
97
- * Decoder of byte chunks. A string chunk or a skip ends an incomplete character of earlier bytes (it becomes U+FFFD);
98
- * the next byte chunk then starts a new character. As in `StreamDecoder`, only a byte-order mark at the very start of
99
- * the output is dropped, never a U+FEFF later in it.
100
- */
101
- #decoder = new TextDecoder("utf-8", { ignoreBOM: true });
102
- #started = false;
103
- /** Stored chunks: for head the start of the stream, for tail a suffix that still contains the next window. */
104
- #chunks = [];
105
- #storedBytes = 0;
106
- #storedNewlines = 0;
107
- #full = false;
108
- #totalBytes = 0;
109
- #totalNewlines = 0;
110
- #endsWithNewline = true;
111
- constructor(limits) {
112
- this.#limits = limits;
113
- }
114
- /** Bytes currently held; bounded by the limits plus one chunk. */
115
- get storedBytes() {
116
- return this.#storedBytes;
117
- }
118
- /**
119
- * Accept a chunk; returns whether anything was accepted. `skipped` is output omitted right before the chunk, which
120
- * must be more than the tail window by at least one byte or line (`ShellOutputInfo.skipped`); only tail retention
121
- * accepts it.
122
- */
123
- push(chunk, skipped) {
124
- // Bytes of an incomplete character from an earlier byte chunk come first.
125
- const pending = typeof chunk === "string" || skipped !== undefined ? this.#decoder.decode() : "";
126
- let text = typeof chunk === "string" ? chunk : this.#decoder.decode(chunk, { stream: true });
127
- const first = !this.#started && pending === "" && skipped === undefined;
128
- if (pending !== "" || text !== "" || skipped !== undefined)
129
- this.#started = true;
130
- if (first && typeof chunk !== "string" && text.startsWith("\ufeff"))
131
- text = text.slice(1);
132
- if (skipped === undefined)
133
- return this.#accept(pending + text);
134
- if (this.#limits.retain !== "tail")
135
- throw new Error("Skipped output requires tail retention");
136
- this.#accept(pending);
137
- this.#skip(skipped);
138
- this.#accept(text);
139
- return true;
140
- }
141
- /** Count omitted output; nothing stored before it can be in the window once the text after it arrives. */
142
- #skip(skipped) {
143
- if (skipped.bytes === 0)
144
- return;
145
- this.#totalBytes += skipped.bytes;
146
- this.#totalNewlines += skipped.newlines;
147
- this.#endsWithNewline = skipped.endsWithNewline;
148
- this.#chunks = [];
149
- this.#storedBytes = 0;
150
- this.#storedNewlines = 0;
151
- }
152
- /** Flush an incomplete trailing character as a replacement character; call when the stream ends. */
153
- end() {
154
- this.#accept(this.#decoder.decode());
155
- }
156
- #accept(text) {
157
- if (text.length === 0)
158
- return false;
159
- const bytes = utf8ByteLength(text);
160
- const newlines = countNewlines(text);
161
- this.#totalBytes += bytes;
162
- this.#totalNewlines += newlines;
163
- this.#endsWithNewline = text.endsWith("\n");
164
- if (this.#full)
165
- return true;
166
- this.#chunks.push({ text, bytes, newlines });
167
- this.#storedBytes += bytes;
168
- this.#storedNewlines += newlines;
169
- if (this.#limits.retain === "head") {
170
- // Nothing past a full window is ever needed.
171
- this.#full = this.#storedBytes > this.#limits.maxBytes || this.#storedNewlines >= this.#limits.maxLines;
172
- return true;
173
- }
174
- // Drop leading chunks while the rest still holds more than a window: more than `maxBytes` bytes or `maxLines`
175
- // newlines, plus one, so the window's line start can still be found. Each chunk is dropped once.
176
- while (this.#chunks.length > 1) {
177
- const first = this.#chunks[0];
178
- const bytesAfter = this.#storedBytes - first.bytes;
179
- const newlinesAfter = this.#storedNewlines - first.newlines;
180
- if (bytesAfter <= this.#limits.maxBytes + 1 && newlinesAfter <= this.#limits.maxLines + 1)
181
- break;
182
- this.#chunks.shift();
183
- this.#storedBytes = bytesAfter;
184
- this.#storedNewlines = newlinesAfter;
185
- }
186
- return true;
187
- }
188
- /** Retained, sanitized output and what the limits dropped from the whole stream. */
189
- snapshot() {
190
- const stored = this.#chunks.length === 1 ? this.#chunks[0].text : this.#chunks.map((chunk) => chunk.text).join("");
191
- const kept = boundOutput(stored, this.#limits);
192
- const storedLines = lines(this.#storedNewlines, stored === "" || stored.endsWith("\n"));
193
- const keptLines = storedLines - kept.droppedLines;
194
- // Tail windows never reach back before this one, but finding a later window's first line needs what precedes it:
195
- // keep the shortest suffix longer than the window by a byte or a line, as `#accept` does.
196
- if (this.#limits.retain === "tail" || this.#chunks.length > 1) {
197
- const text = this.#limits.retain === "tail" ? tailMargin(stored, this.#limits) : stored;
198
- const bytes = this.#limits.retain === "tail" ? utf8ByteLength(text) : this.#storedBytes;
199
- this.#chunks = text === "" ? [] : [{ text, bytes, newlines: countNewlines(text) }];
200
- this.#storedBytes = bytes;
201
- this.#storedNewlines = this.#chunks[0]?.newlines ?? 0;
202
- }
203
- return {
204
- text: sanitizeOutput(kept.text),
205
- droppedBytes: this.#totalBytes - kept.bytes,
206
- droppedLines: lines(this.#totalNewlines, this.#endsWithNewline) - keptLines,
207
- };
208
- }
209
- }
210
- /**
211
- * The shortest suffix of `text` with more than `maxBytes` bytes or more than `maxLines` newlines, or all of it. The
212
- * tail window of any text that ends with this suffix, followed by anything, is the same as of `text` followed by it.
213
- */
214
- function tailMargin(text, limits) {
215
- const bytes = encoder.encode(text);
216
- const byteStart = bytes.length > limits.maxBytes ? characterEnd(bytes, bytes.length - limits.maxBytes - 1) : 0;
217
- let lineStart = 0;
218
- let newlines = 0;
219
- for (let index = bytes.lastIndexOf(NEWLINE); index !== -1; index = bytes.lastIndexOf(NEWLINE, index - 1)) {
220
- if (++newlines > limits.maxLines) {
221
- lineStart = index;
222
- break;
223
- }
224
- if (index === 0)
225
- break;
226
- }
227
- const start = Math.max(byteStart, lineStart);
228
- return start === 0 ? text : decoder.decode(bytes.subarray(start));
229
- }
230
- /** Lines of text with `newlines` newlines; a final unterminated line counts. */
231
- function lines(newlines, terminated) {
232
- return newlines + (terminated ? 0 : 1);
233
- }
234
- function countNewlines(text) {
235
- let count = 0;
236
- for (let index = text.indexOf("\n"); index !== -1; index = text.indexOf("\n", index + 1))
237
- count++;
238
- return count;
239
- }
240
- /** Each progress commit also buys a pause proportional to what it wrote. */
241
- export const PROGRESS_BYTES_PER_SECOND = 100 * 1024;
242
- /**
243
- * Adaptive progress commits: the first change after an idle period commits at once; each commit then delays the next
244
- * by at least `minIntervalMs` and by its written size at 100 KiB/s. At most one commit is in flight; changes made
245
- * meanwhile coalesce into the next one.
246
- */
247
- export class Progress {
248
- #write;
249
- #onError;
250
- #minIntervalMs;
251
- #waiters = [];
252
- #timer;
253
- #inFlight;
254
- #nextAt = 0;
255
- #dirty = false;
256
- #stopped = false;
257
- constructor(write, onError, minIntervalMs) {
258
- this.#write = write;
259
- this.#onError = onError;
260
- this.#minIntervalMs = minIntervalMs;
261
- }
262
- /** Schedule a commit. */
263
- mark() {
264
- this.#dirty = true;
265
- this.#schedule();
266
- }
267
- /** Schedule a commit; the promise settles with the commit that includes this change. */
268
- markAndWait() {
269
- const waiter = Promise.withResolvers();
270
- this.#waiters.push(waiter);
271
- this.mark();
272
- return waiter.promise;
273
- }
274
- /** Stop committing and wait for the commit in flight; returns the waiters the final commit must settle. */
275
- async stop() {
276
- this.#stopped = true;
277
- clearTimeout(this.#timer);
278
- this.#timer = undefined;
279
- await this.#inFlight;
280
- return this.#waiters.splice(0);
281
- }
282
- #schedule() {
283
- if (this.#stopped || this.#timer !== undefined || this.#inFlight !== undefined)
284
- return;
285
- const wait = this.#nextAt - Date.now();
286
- if (wait <= 0)
287
- this.#flush();
288
- else
289
- this.#timer = setTimeout(() => {
290
- this.#timer = undefined;
291
- this.#flush();
292
- }, wait);
293
- }
294
- #flush() {
295
- if (this.#stopped || !this.#dirty)
296
- return;
297
- this.#dirty = false;
298
- const waiters = this.#waiters.splice(0);
299
- const started = Date.now();
300
- this.#inFlight = this.#write()
301
- .then((bytes) => {
302
- this.#nextAt = started + Math.max(this.#minIntervalMs, (bytes * 1000) / PROGRESS_BYTES_PER_SECOND);
303
- for (const waiter of waiters)
304
- waiter.resolve();
305
- }, (error) => {
306
- this.#nextAt = started + this.#minIntervalMs;
307
- for (const waiter of waiters)
308
- waiter.reject(error);
309
- this.#onError(error);
310
- })
311
- .finally(() => {
312
- this.#inFlight = undefined;
313
- if (this.#dirty)
314
- this.#schedule();
315
- });
316
- }
317
- }
318
- //# sourceMappingURL=output.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"output.js","sourceRoot":"","sources":["../../src/harness/output.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAgBhD,MAAM,OAAO,GAAG,IAAI,CAAC;AACrB,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAC5D,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,yFAAyF;AACzF,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;AAE9D,4FAA4F;AAC5F,MAAM,UAAU,cAAc,CAAC,IAAY;IAC1C,OAAO,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,MAAoB;IAC7D,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACnC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAClG,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACtC,OAAO;QACN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,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,OAAO;QAC1C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACxC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,EAAE;aAC5B,IAAI,CACJ,CAAC,KAAK,EAAE,EAAE;YACT,IAAI,CAAC,OAAO,GAAG,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,yBAAyB,CAAC,CAAC;YACnG,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,IAAI,CAAC,cAAc,CAAC;YAC7C,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 type { ShellOutputSkip } from \"../env/index.ts\";\nimport { 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();\n/** Slices decode exactly: a U+FEFF at a slice's start is text, not a byte-order mark. */\nconst decoder = new TextDecoder(\"utf-8\", { ignoreBOM: true });\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\t/**\n\t * Decoder of byte chunks. A string chunk or a skip ends an incomplete character of earlier bytes (it becomes U+FFFD);\n\t * the next byte chunk then starts a new character. As in `StreamDecoder`, only a byte-order mark at the very start of\n\t * the output is dropped, never a U+FEFF later in it.\n\t */\n\treadonly #decoder = new TextDecoder(\"utf-8\", { ignoreBOM: true });\n\t#started = false;\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/**\n\t * Accept a chunk; returns whether anything was accepted. `skipped` is output omitted right before the chunk, which\n\t * must be more than the tail window by at least one byte or line (`ShellOutputInfo.skipped`); only tail retention\n\t * accepts it.\n\t */\n\tpush(chunk: string | Uint8Array, skipped?: ShellOutputSkip): boolean {\n\t\t// Bytes of an incomplete character from an earlier byte chunk come first.\n\t\tconst pending = typeof chunk === \"string\" || skipped !== undefined ? this.#decoder.decode() : \"\";\n\t\tlet text = typeof chunk === \"string\" ? chunk : this.#decoder.decode(chunk, { stream: true });\n\t\tconst first = !this.#started && pending === \"\" && skipped === undefined;\n\t\tif (pending !== \"\" || text !== \"\" || skipped !== undefined) this.#started = true;\n\t\tif (first && typeof chunk !== \"string\" && text.startsWith(\"\\ufeff\")) text = text.slice(1);\n\t\tif (skipped === undefined) return this.#accept(pending + text);\n\t\tif (this.#limits.retain !== \"tail\") throw new Error(\"Skipped output requires tail retention\");\n\t\tthis.#accept(pending);\n\t\tthis.#skip(skipped);\n\t\tthis.#accept(text);\n\t\treturn true;\n\t}\n\n\t/** Count omitted output; nothing stored before it can be in the window once the text after it arrives. */\n\t#skip(skipped: ShellOutputSkip): void {\n\t\tif (skipped.bytes === 0) return;\n\t\tthis.#totalBytes += skipped.bytes;\n\t\tthis.#totalNewlines += skipped.newlines;\n\t\tthis.#endsWithNewline = skipped.endsWithNewline;\n\t\tthis.#chunks = [];\n\t\tthis.#storedBytes = 0;\n\t\tthis.#storedNewlines = 0;\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, but finding a later window's first line needs what precedes it:\n\t\t// keep the shortest suffix longer than the window by a byte or a line, as `#accept` does.\n\t\tif (this.#limits.retain === \"tail\" || this.#chunks.length > 1) {\n\t\t\tconst text = this.#limits.retain === \"tail\" ? tailMargin(stored, this.#limits) : stored;\n\t\t\tconst bytes = this.#limits.retain === \"tail\" ? utf8ByteLength(text) : 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/**\n * The shortest suffix of `text` with more than `maxBytes` bytes or more than `maxLines` newlines, or all of it. The\n * tail window of any text that ends with this suffix, followed by anything, is the same as of `text` followed by it.\n */\nfunction tailMargin(text: string, limits: OutputLimits): string {\n\tconst bytes = encoder.encode(text);\n\tconst byteStart = bytes.length > limits.maxBytes ? characterEnd(bytes, bytes.length - limits.maxBytes - 1) : 0;\n\tlet lineStart = 0;\n\tlet newlines = 0;\n\tfor (let index = bytes.lastIndexOf(NEWLINE); index !== -1; index = bytes.lastIndexOf(NEWLINE, index - 1)) {\n\t\tif (++newlines > limits.maxLines) {\n\t\t\tlineStart = index;\n\t\t\tbreak;\n\t\t}\n\t\tif (index === 0) break;\n\t}\n\tconst start = Math.max(byteStart, lineStart);\n\treturn start === 0 ? text : decoder.decode(bytes.subarray(start));\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/** Each progress commit also buys a pause proportional to what it wrote. */\nexport const PROGRESS_BYTES_PER_SECOND = 100 * 1024;\n\n/**\n * Adaptive progress commits: the first change after an idle period commits at once; each commit then delays the next\n * by at least `minIntervalMs` and by its written size at 100 KiB/s. At most one commit is in flight; changes made\n * meanwhile coalesce into the next one.\n */\nexport class Progress {\n\treadonly #write: () => Promise<number>;\n\treadonly #onError: (error: unknown) => void;\n\treadonly #minIntervalMs: number;\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, minIntervalMs: number) {\n\t\tthis.#write = write;\n\t\tthis.#onError = onError;\n\t\tthis.#minIntervalMs = minIntervalMs;\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(this.#minIntervalMs, (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 + this.#minIntervalMs;\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"]}