fix(process): validate terminal byte totals against retained frames

parseProcessCapture accepted terminal retention accounting that its
retained frames contradict: any retained_bytes within budget, and
observed_bytes above retained_bytes with no omitted frame. A malformed
capture could therefore report truncated: false while its terminal
evidence was incomplete. Recompute retained raw and rendered bytes from
the frames, and require equal observed and retained bytes when no frame
was omitted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-09 14:12:05 +00:00
co-authored by Claude Opus 5.5
parent 33bf585185
commit 39eb0587c9
2 changed files with 48 additions and 2 deletions
+24 -2
View File
@@ -140,17 +140,39 @@ const validateCoverage = (
hasCaptureTruncation(
details,
), "truncation_details", "aggregate truncation must match producer coverage");
for (const [name, retention, count] of [
["raw_terminal", details.raw_terminal, capture.frames.length],
for (const [name, retention, count, bytes] of [
[
"raw_terminal",
details.raw_terminal,
capture.frames.length,
capture.frames.reduce(
(total, frame) =>
total + Buffer.byteLength(frame.raw_data ?? frame.data),
0,
),
],
[
"rendered_terminal",
details.rendered_terminal,
capture.rendered_frames.length,
capture.rendered_frames.reduce(
(total, frame) =>
frame.lines.reduce(
(sum, line) => sum + Buffer.byteLength(line),
total + Buffer.byteLength(frame.serialized_state),
),
0,
),
],
] as const) {
// Whole frames are retained or omitted, so retained bytes are exactly the
// retained frames' bytes, and nothing is unretained when no frame was omitted.
require(retention.retained_frames === count &&
retention.observed_frames >= count &&
retention.retained_bytes === bytes &&
retention.observed_bytes >= retention.retained_bytes &&
(retention.observed_frames > count ||
retention.observed_bytes === retention.retained_bytes) &&
retention.retained_bytes <=
retention.budget_bytes, `truncation_details.${name}`, "retained observations and budget accounting do not agree");
}