fix(process): bound normalized terminal retention before allocation

Pre-count ordered normalization stages before materializing output. Charge original and normalized terminal bytes independently, omit whole over-budget chunks and preserve exact raw counts plus explicit unknown final sizes. Bound rendered normalization, retain legacy capture compatibility, and remove temporary per-match output arrays.

Validation: UTF-8 surrogate boundaries, shrinking/no-op/literal rules, capture validation and public MCP expansion/equal-size controls with verified cleanup. CPU, process-family memory and token changes are unmeasured; no performance speedup claimed. Nonterminal derived payload bounds remain separate work.

Fixes #3168
This commit is contained in:
morluto committed 2026-10-14 16:54:08 +08:00
1 parent 7fb0a0cc57
commit 1d826f304f
14 files changed
+894 -76

No files matched your search

@@ -66,7 +66,12 @@ it("returns detached terminal observations", async () => {
rows: 12,
scrollback: 100,
maxBytes: 100_000,
normalize: (value) => value,
normalize: (value, maxBytes) => {
const utf8Bytes = Buffer.byteLength(value);
return utf8Bytes <= maxBytes
? { state: "normalized", value, utf8Bytes }
: { state: "over_budget" };
},
recordEvent: (collection, index) =>
observed.push(`${collection}:${String(index)}`),
});