@earendil-works/pi-durable 1.2.0 → 1.3.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.
- package/CHANGELOG.md +15 -0
- package/README.md +17 -0
- package/dist/env/index.d.ts +1 -0
- package/dist/env/index.d.ts.map +1 -1
- package/dist/env/index.js.map +1 -1
- package/dist/harness/context.d.ts +5 -0
- package/dist/harness/context.d.ts.map +1 -1
- package/dist/harness/context.js +46 -12
- package/dist/harness/context.js.map +1 -1
- package/dist/harness/tool.d.ts.map +1 -1
- package/dist/harness/tool.js +1 -1
- package/dist/harness/tool.js.map +1 -1
- package/dist/storage/sqlite/cloudflare.d.ts +17 -5
- package/dist/storage/sqlite/cloudflare.d.ts.map +1 -1
- package/dist/storage/sqlite/cloudflare.js +57 -21
- package/dist/storage/sqlite/cloudflare.js.map +1 -1
- package/dist/storage/sqlite/index.d.ts +1 -1
- package/dist/storage/sqlite/index.d.ts.map +1 -1
- package/dist/storage/sqlite/index.js.map +1 -1
- package/dist/storage/sqlite/migrations.d.ts +5 -1
- package/dist/storage/sqlite/migrations.d.ts.map +1 -1
- package/dist/storage/sqlite/migrations.js +94 -84
- package/dist/storage/sqlite/migrations.js.map +1 -1
- package/dist/storage/sqlite/node.d.ts +6 -4
- package/dist/storage/sqlite/node.d.ts.map +1 -1
- package/dist/storage/sqlite/node.js +6 -2
- package/dist/storage/sqlite/node.js.map +1 -1
- package/dist/storage/sqlite/schema.d.ts +47 -0
- package/dist/storage/sqlite/schema.d.ts.map +1 -0
- package/dist/storage/sqlite/schema.js +57 -0
- package/dist/storage/sqlite/schema.js.map +1 -0
- package/dist/storage/sqlite/storage.d.ts +9 -1
- package/dist/storage/sqlite/storage.d.ts.map +1 -1
- package/dist/storage/sqlite/storage.js +46 -43
- package/dist/storage/sqlite/storage.js.map +1 -1
- package/dist/tools/image-processor.d.ts +4 -1
- package/dist/tools/image-processor.d.ts.map +1 -1
- package/dist/tools/image-processor.js +8 -18
- package/dist/tools/image-processor.js.map +1 -1
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/read.js +1 -1
- package/dist/tools/read.js.map +1 -1
- package/dist/truncate.d.ts +3 -2
- package/dist/truncate.d.ts.map +1 -1
- package/dist/truncate.js +4 -34
- package/dist/truncate.js.map +1 -1
- package/package.json +4 -4
- package/dist/harness/output.d.ts +0 -69
- package/dist/harness/output.d.ts.map +0 -1
- package/dist/harness/output.js +0 -318
- package/dist/harness/output.js.map +0 -1
package/dist/truncate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../src/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,eAAO,MAAM,iBAAiB,OAAO,CAAC;AACtC,eAAO,MAAM,iBAAiB,QAAY,CAAC;AAE3C,MAAM,WAAW,gBAAgB;IAChC,4BAA4B;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,kCAAkC;IAClC,SAAS,EAAE,OAAO,CAAC;IACnB,sEAAsE;IACtE,WAAW,EAAE,OAAO,GAAG,OAAO,GAAG,IAAI,CAAC;IACtC,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,uDAAuD;IACvD,WAAW,EAAE,MAAM,CAAC;IACpB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAC;IACpB,yFAAyF;IACzF,eAAe,EAAE,OAAO,CAAC;IACzB,2EAA2E;IAC3E,qBAAqB,EAAE,OAAO,CAAC;IAC/B,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;IACjB,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IACjC,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;
|
|
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C,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EAC1D,OAAO,GAAE,iBAAsB,GAC7B,gBAAgB,CAgFlB"}
|
package/dist/truncate.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { utf8ByteLength } from "@earendil-works/chord/output";
|
|
1
2
|
/**
|
|
2
3
|
* Shared truncation utilities for tool outputs.
|
|
3
4
|
*
|
|
@@ -5,43 +6,12 @@
|
|
|
5
6
|
* - Line limit (default: 2000 lines)
|
|
6
7
|
* - Byte limit (default: 50KB)
|
|
7
8
|
*
|
|
8
|
-
* Never returns partial lines. Tool output streams are bounded by `
|
|
9
|
+
* Never returns partial lines. Tool output streams are bounded by `OutputBuffer` from `@earendil-works/chord/output` instead.
|
|
9
10
|
*/
|
|
10
11
|
export const DEFAULT_MAX_LINES = 2000;
|
|
11
12
|
export const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export function utf8ByteLength(content) {
|
|
15
|
-
if (runtimeBuffer)
|
|
16
|
-
return runtimeBuffer.byteLength(content, "utf8");
|
|
17
|
-
const firstNonAscii = content.search(nonAsciiPattern);
|
|
18
|
-
if (firstNonAscii === -1)
|
|
19
|
-
return content.length;
|
|
20
|
-
let bytes = firstNonAscii;
|
|
21
|
-
for (let i = firstNonAscii; i < content.length; i++) {
|
|
22
|
-
const code = content.charCodeAt(i);
|
|
23
|
-
if (code <= 0x7f) {
|
|
24
|
-
bytes += 1;
|
|
25
|
-
}
|
|
26
|
-
else if (code <= 0x7ff) {
|
|
27
|
-
bytes += 2;
|
|
28
|
-
}
|
|
29
|
-
else if (code >= 0xd800 && code <= 0xdbff && i + 1 < content.length) {
|
|
30
|
-
const next = content.charCodeAt(i + 1);
|
|
31
|
-
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
32
|
-
bytes += 4;
|
|
33
|
-
i++;
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
bytes += 3;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
bytes += 3;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return bytes;
|
|
44
|
-
}
|
|
13
|
+
// One implementation, shared with the output buffer.
|
|
14
|
+
export { utf8ByteLength };
|
|
45
15
|
function splitLinesForCounting(content) {
|
|
46
16
|
if (content.length === 0)
|
|
47
17
|
return [];
|
package/dist/truncate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"truncate.js","sourceRoot":"","sources":["../src/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,OAAO;AAsCnD,MAAM,aAAa,GAAI,UAAyC,CAAC,MAAM,CAAC;AACxE,MAAM,eAAe,GAAG,cAAc,CAAC;AAEvC,MAAM,UAAU,cAAc,CAAC,OAAe;IAC7C,IAAI,aAAa;QAAE,OAAO,aAAa,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;IACtD,IAAI,aAAa,KAAK,CAAC,CAAC;QAAE,OAAO,OAAO,CAAC,MAAM,CAAC;IAEhD,IAAI,KAAK,GAAG,aAAa,CAAC;IAC1B,KAAK,IAAI,CAAC,GAAG,aAAa,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrD,MAAM,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QACnC,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;YAClB,KAAK,IAAI,CAAC,CAAC;QACZ,CAAC;aAAM,IAAI,IAAI,IAAI,KAAK,EAAE,CAA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B,eAAe,EAAE,KAAK;QACtB,qBAAqB,EAAE,KAAK;QAC5B,QAAQ;QACR,QAAQ;KACR,CAAC;AACH,CAAC","sourcesContent":["/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines. Tool output streams are bounded by `src/harness/output.ts` instead.\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n\t/** The max lines limit that was applied */\n\tmaxLines: number;\n\t/** The max bytes limit that was applied */\n\tmaxBytes: number;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\ninterface RuntimeBuffer {\n\tbyteLength(content: string, encoding: \"utf8\"): number;\n}\n\nconst runtimeBuffer = (globalThis as { Buffer?: RuntimeBuffer }).Buffer;\nconst nonAsciiPattern = /[^\\x00-\\x7f]/;\n\nexport function utf8ByteLength(content: string): number {\n\tif (runtimeBuffer) return runtimeBuffer.byteLength(content, \"utf8\");\n\n\tconst firstNonAscii = content.search(nonAsciiPattern);\n\tif (firstNonAscii === -1) return content.length;\n\n\tlet bytes = firstNonAscii;\n\tfor (let i = firstNonAscii; i < content.length; i++) {\n\t\tconst code = content.charCodeAt(i);\n\t\tif (code <= 0x7f) {\n\t\t\tbytes += 1;\n\t\t} else if (code <= 0x7ff) {\n\t\t\tbytes += 2;\n\t\t} else if (code >= 0xd800 && code <= 0xdbff && i + 1 < content.length) {\n\t\t\tconst next = content.charCodeAt(i + 1);\n\t\t\tif (next >= 0xdc00 && next <= 0xdfff) {\n\t\t\t\tbytes += 4;\n\t\t\t\ti++;\n\t\t\t} else {\n\t\t\t\tbytes += 3;\n\t\t\t}\n\t\t} else {\n\t\t\tbytes += 3;\n\t\t}\n\t}\n\treturn bytes;\n}\n\nfunction splitLinesForCounting(content: string): string[] {\n\tif (content.length === 0) return [];\n\tconst lines = content.split(\"\\n\");\n\tif (content.endsWith(\"\\n\")) lines.pop();\n\treturn lines;\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\treturn truncateHeadOf(\n\t\tcontent,\n\t\t{ lines: splitLinesForCounting(content).length, bytes: utf8ByteLength(content) },\n\t\toptions,\n\t);\n}\n\n/**\n * `truncateHead` of a text known by a prefix and its totals (`lines` counted like `truncateHead`, ignoring a trailing\n * newline). The prefix must be the whole text, or longer than `maxBytes + 1` UTF-8 bytes, or hold at least `maxLines`\n * newlines; then the result equals `truncateHead` of the whole text.\n */\nexport function truncateHeadOf(\n\tprefix: string,\n\ttotals: { readonly lines: number; readonly bytes: number },\n\toptions: TruncationOptions = {},\n): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = totals.bytes;\n\tconst lines = splitLinesForCounting(prefix);\n\tconst totalLines = totals.lines;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent: prefix,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\tconst firstLineBytes = utf8ByteLength(lines[0]);\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Collect complete lines that fit\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tfor (let i = 0; i < lines.length && i < maxLines; i++) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = utf8ByteLength(line) + (i > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.push(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// Without a byte break, only omitted lines prove the line limit was reached; otherwise a trailing newline exceeded bytes.\n\tif (truncatedBy !== \"bytes\") truncatedBy = outputLinesArr.length < totalLines ? \"lines\" : \"bytes\";\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = utf8ByteLength(outputContent);\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"truncate.js","sourceRoot":"","sources":["../src/truncate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,OAAO;AAkCnD,qDAAqD;AACrD,OAAO,EAAE,cAAc,EAAE,CAAC;AAE1B,SAAS,qBAAqB,CAAC,OAAe;IAC7C,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACpC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,KAAK,CAAC,GAAG,EAAE,CAAC;IACxC,OAAO,KAAK,CAAC;AACd,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACvC,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC;QAClB,OAAO,GAAG,KAAK,GAAG,CAAC;IACpB,CAAC;SAAM,IAAI,KAAK,GAAG,IAAI,GAAG,IAAI,EAAE,CAAC;QAChC,OAAO,GAAG,CAAC,KAAK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AG,cAAc,CAAC,aAAa,CAAC,CAAC;IAEvD,OAAO;QACN,OAAO,EAAE,aAAa;QACtB,SAAS,EAAE,IAAI;QACf,WAAW;QACX,UAAU;QACV,UAAU;QACV,WAAW,EAAE,cAAc,CAAC,MAAM;QAClC,WAAW,EAAE,gBAAgB;QAC7B,eAAe,EAAE,KAAK;QACtB,qBAAqB,EAAE,KAAK;QAC5B,QAAQ;QACR,QAAQ;KACR,CAAC;AACH,CAAC","sourcesContent":["import { utf8ByteLength } from \"@earendil-works/chord/output\";\n/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines. Tool output streams are bounded by `OutputBuffer` from `@earendil-works/chord/output` instead.\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n\t/** The max lines limit that was applied */\n\tmaxLines: number;\n\t/** The max bytes limit that was applied */\n\tmaxBytes: number;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\n// One implementation, shared with the output buffer.\nexport { utf8ByteLength };\n\nfunction splitLinesForCounting(content: string): string[] {\n\tif (content.length === 0) return [];\n\tconst lines = content.split(\"\\n\");\n\tif (content.endsWith(\"\\n\")) lines.pop();\n\treturn lines;\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\treturn truncateHeadOf(\n\t\tcontent,\n\t\t{ lines: splitLinesForCounting(content).length, bytes: utf8ByteLength(content) },\n\t\toptions,\n\t);\n}\n\n/**\n * `truncateHead` of a text known by a prefix and its totals (`lines` counted like `truncateHead`, ignoring a trailing\n * newline). The prefix must be the whole text, or longer than `maxBytes + 1` UTF-8 bytes, or hold at least `maxLines`\n * newlines; then the result equals `truncateHead` of the whole text.\n */\nexport function truncateHeadOf(\n\tprefix: string,\n\ttotals: { readonly lines: number; readonly bytes: number },\n\toptions: TruncationOptions = {},\n): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = totals.bytes;\n\tconst lines = splitLinesForCounting(prefix);\n\tconst totalLines = totals.lines;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent: prefix,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\tconst firstLineBytes = utf8ByteLength(lines[0]);\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Collect complete lines that fit\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tfor (let i = 0; i < lines.length && i < maxLines; i++) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = utf8ByteLength(line) + (i > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.push(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// Without a byte break, only omitted lines prove the line limit was reached; otherwise a trailing newline exceeded bytes.\n\tif (truncatedBy !== \"bytes\") truncatedBy = outputLinesArr.length < totalLines ? \"lines\" : \"bytes\";\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = utf8ByteLength(outputContent);\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@earendil-works/pi-durable",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Durable conversation, task, and document runtime for Pi",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -143,9 +143,9 @@
|
|
|
143
143
|
"node": ">=22.19.0"
|
|
144
144
|
},
|
|
145
145
|
"dependencies": {
|
|
146
|
-
"@earendil-works/chord": "
|
|
147
|
-
"@earendil-works/pi-ai": "
|
|
148
|
-
"@earendil-works/pi-codemode": "
|
|
146
|
+
"@earendil-works/chord": "1.3.0",
|
|
147
|
+
"@earendil-works/pi-ai": "1.3.0",
|
|
148
|
+
"@earendil-works/pi-codemode": "1.3.0",
|
|
149
149
|
"@silvia-odwyer/photon": "0.3.3",
|
|
150
150
|
"diff": "8.0.4",
|
|
151
151
|
"typebox": "1.3.27"
|
package/dist/harness/output.d.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import type { ShellOutputSkip } from "../env/index.ts";
|
|
2
|
-
/** Retention limits of one tool's output. */
|
|
3
|
-
export type OutputLimits = {
|
|
4
|
-
readonly maxBytes: number;
|
|
5
|
-
readonly maxLines: number;
|
|
6
|
-
readonly retain: "head" | "tail";
|
|
7
|
-
};
|
|
8
|
-
/** Retained output and what the limits dropped. */
|
|
9
|
-
export type BoundedOutput = {
|
|
10
|
-
readonly text: string;
|
|
11
|
-
readonly droppedBytes: number;
|
|
12
|
-
readonly droppedLines: number;
|
|
13
|
-
};
|
|
14
|
-
/** An exact slice of the input within the limits, and what it left out. */
|
|
15
|
-
export type OutputSlice = {
|
|
16
|
-
readonly text: string;
|
|
17
|
-
readonly bytes: number;
|
|
18
|
-
readonly droppedBytes: number;
|
|
19
|
-
readonly droppedLines: number;
|
|
20
|
-
};
|
|
21
|
-
/** Remove control characters that break display and transcripts; tabs and newlines stay. */
|
|
22
|
-
export declare function sanitizeOutput(text: string): string;
|
|
23
|
-
/**
|
|
24
|
-
* Bound `text` to whole lines within the limits: the first lines for `head`, the last lines for `tail`. The result is an
|
|
25
|
-
* exact slice, trailing newline included. A single line longer than `maxBytes` is cut at the byte limit on a character
|
|
26
|
-
* boundary.
|
|
27
|
-
*/
|
|
28
|
-
export declare function boundOutput(text: string, limits: OutputLimits): OutputSlice;
|
|
29
|
-
/** The last character boundary at or before `index`. */
|
|
30
|
-
export declare function characterEnd(bytes: Uint8Array, index: number): number;
|
|
31
|
-
/**
|
|
32
|
-
* Bounded running output of one tool call. Accepting a chunk costs time proportional to the chunk: head retention stops
|
|
33
|
-
* storing once the window is full, and tail retention drops stored text the window no longer needs when it snapshots.
|
|
34
|
-
* Counts of the whole stream are kept so the dropped totals stay exact.
|
|
35
|
-
*/
|
|
36
|
-
export declare class OutputBuffer {
|
|
37
|
-
#private;
|
|
38
|
-
constructor(limits: OutputLimits);
|
|
39
|
-
/** Bytes currently held; bounded by the limits plus one chunk. */
|
|
40
|
-
get storedBytes(): number;
|
|
41
|
-
/**
|
|
42
|
-
* Accept a chunk; returns whether anything was accepted. `skipped` is output omitted right before the chunk, which
|
|
43
|
-
* must be more than the tail window by at least one byte or line (`ShellOutputInfo.skipped`); only tail retention
|
|
44
|
-
* accepts it.
|
|
45
|
-
*/
|
|
46
|
-
push(chunk: string | Uint8Array, skipped?: ShellOutputSkip): boolean;
|
|
47
|
-
/** Flush an incomplete trailing character as a replacement character; call when the stream ends. */
|
|
48
|
-
end(): void;
|
|
49
|
-
/** Retained, sanitized output and what the limits dropped from the whole stream. */
|
|
50
|
-
snapshot(): BoundedOutput;
|
|
51
|
-
}
|
|
52
|
-
/** Each progress commit also buys a pause proportional to what it wrote. */
|
|
53
|
-
export declare const PROGRESS_BYTES_PER_SECOND: number;
|
|
54
|
-
/**
|
|
55
|
-
* Adaptive progress commits: the first change after an idle period commits at once; each commit then delays the next
|
|
56
|
-
* by at least `minIntervalMs` and by its written size at 100 KiB/s. At most one commit is in flight; changes made
|
|
57
|
-
* meanwhile coalesce into the next one.
|
|
58
|
-
*/
|
|
59
|
-
export declare class Progress {
|
|
60
|
-
#private;
|
|
61
|
-
constructor(write: () => Promise<number>, onError: (error: unknown) => void, minIntervalMs: number);
|
|
62
|
-
/** Schedule a commit. */
|
|
63
|
-
mark(): void;
|
|
64
|
-
/** Schedule a commit; the promise settles with the commit that includes this change. */
|
|
65
|
-
markAndWait(): Promise<void>;
|
|
66
|
-
/** Stop committing and wait for the commit in flight; returns the waiters the final commit must settle. */
|
|
67
|
-
stop(): Promise<PromiseWithResolvers<void>[]>;
|
|
68
|
-
}
|
|
69
|
-
//# sourceMappingURL=output.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../../src/harness/output.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGvD,6CAA6C;AAC7C,MAAM,MAAM,YAAY,GAAG;IAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC;AAEtH,mDAAmD;AACnD,MAAM,MAAM,aAAa,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC;AAEpH,2EAA2E;AAC3E,MAAM,MAAM,WAAW,GAAG;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC9B,CAAC;AAQF,4FAA4F;AAC5F,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,WAAW,CAU3E;AA0CD,wDAAwD;AACxD,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAIrE;AAgBD;;;;GAIG;AACH,qBAAa,YAAY;;IAkBxB,YAAY,MAAM,EAAE,YAAY,EAE/B;IAED,kEAAkE;IAClE,IAAI,WAAW,IAAI,MAAM,CAExB;IAED;;;;OAIG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAanE;IAaD,oGAAoG;IACpG,GAAG,IAAI,IAAI,CAEV;IAgCD,oFAAoF;IACpF,QAAQ,IAAI,aAAa,CAoBxB;CACD;AAiCD,4EAA4E;AAC5E,eAAO,MAAM,yBAAyB,QAAa,CAAC;AAEpD;;;;GAIG;AACH,qBAAa,QAAQ;;IAWpB,YAAY,KAAK,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,EAAE,aAAa,EAAE,MAAM,EAIjG;IAED,yBAAyB;IACzB,IAAI,IAAI,IAAI,CAGX;IAED,wFAAwF;IACxF,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAK3B;IAED,2GAA2G;IACrG,IAAI,IAAI,OAAO,CAAC,oBAAoB,CAAC,IAAI,CAAC,EAAE,CAAC,CAMlD;CAmCD"}
|
package/dist/harness/output.js
DELETED
|
@@ -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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AAE,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"]}
|