perf(process): wake OwnedCommand on output close, lazily materialize diagnostic snapshots, and bound tail copies (#13261)

* perf(process): wake OwnedCommand on output close, lazily materialize diagnostic snapshots, and bound tail copies

* fix(process): capture event-time bytes in diagnostic snapshots and make wrap test independent
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 03:15:02 -05:00
1 parent 9e379c9589
commit b795aee553
6 files changed
+638 -84

No files matched your search

+42 -11
View File
@@ -10,6 +10,34 @@ export interface ProviderProcessOutputToken {
/** True when the line's right edge is cut or its end has not been observed. */
readonly trailingContextIncomplete?: true;
}
/** Immutable raw byte snapshot of a confirmed token before UTF-8 decoding. */
export interface RawProcessOutputToken {
readonly offset: number;
readonly prefix: Buffer | undefined;
readonly suffix: Buffer;
readonly truncated: boolean;
readonly trailingContextIncomplete: boolean;
}
/** Decode an immutable raw token snapshot into its UTF-8 projection. */
export const materializeProcessOutputToken = (
raw: RawProcessOutputToken,
): ProviderProcessOutputToken => {
const bytes =
raw.prefix === undefined
? raw.suffix
: Buffer.concat([raw.prefix, raw.suffix]);
return {
offset: raw.offset,
line: bytes.toString("utf8"),
...(!isUtf8(bytes) ? { invalidUtf8: true as const } : {}),
truncated: raw.truncated,
...(raw.trailingContextIncomplete
? { trailingContextIncomplete: true as const }
: {}),
};
};
const PROCESS_OUTPUT_TOKEN_CONTEXT_BYTES = 1024;
const NEWLINE = 0x0a;
const isWordByte = (byte: number): boolean =>
@@ -74,30 +102,33 @@ export class ProcessOutputTokenScanner {
this.#pending = undefined;
if (this.#latest !== undefined) this.#latest.open = false;
}
latestWithin(
/** Capture event-time bytes and truncation flags without UTF-8 decoding. */
snapshotLatestWithin(
start: number,
end: number,
): ProviderProcessOutputToken | undefined {
): RawProcessOutputToken | undefined {
const latest = this.#latest;
if (latest === undefined || latest.offset < start || latest.offset >= end)
return undefined;
// Omit a prefix crossing the checkpoint in full: neither its old cause
// text nor a credential fragment cut on the left belongs to this scope.
const prefixInRange = latest.offset - latest.prefix.length >= start;
const bytes = Buffer.concat([
...(prefixInRange ? [latest.prefix] : []),
latest.suffix,
]);
return {
offset: latest.offset,
line: bytes.toString("utf8"),
...(!isUtf8(bytes) ? { invalidUtf8: true as const } : {}),
prefix: prefixInRange ? latest.prefix : undefined,
suffix: latest.suffix,
truncated: latest.prefixCut || latest.suffixCut || !prefixInRange,
...(latest.suffixCut || latest.open
? { trailingContextIncomplete: true as const }
: {}),
trailingContextIncomplete: latest.suffixCut || latest.open,
};
}
latestWithin(
start: number,
end: number,
): ProviderProcessOutputToken | undefined {
const raw = this.snapshotLatestWithin(start, end);
return raw === undefined ? undefined : materializeProcessOutputToken(raw);
}
#record(searched: Buffer, index: number, searchedStart: number): Match {
const lineStart =
index === 0 ? 0 : searched.lastIndexOf(NEWLINE, index - 1) + 1;