fix(process): localize capture truncation diagnostics (#1284)

Report per-source capture coverage and existing file-hash omissions while preserving comparable observations in unaffected dimensions. Keep legacy truncated captures conservative and retain known partial trace events.

Closes #1250
Partially addresses #1251; independent hashing remains out of scope.
This commit is contained in:
Ysopwnable
2026-10-09 21:56:43 +08:00
committed by GitHub
parent 7e96f25f8b
commit 33bf585185
14 changed files with 739 additions and 50 deletions
@@ -1,5 +1,6 @@
import type { UnverifiedProcessCapture } from "./processCapture.js";
import { digestProcessCommitment } from "./processScenario.js";
import { hasCaptureTruncation } from "./processCaptureCoverage.js";
/** One pure semantic validation failure in a shaped process capture. */
export interface ProcessCaptureValidationIssue {
@@ -121,6 +122,81 @@ const validateLifecycle = (
null, "exit", "deadline termination cannot declare a normal exit code");
};
const validateCoverage = (
capture: UnverifiedProcessCapture,
require: RequireInvariant,
): void => {
const details = capture.truncation_details;
if (details === undefined) return;
require(details.process.retained_samples === capture.process_samples.length &&
details.process.retained_samples <= details.process.sample_limit &&
details.process.sampling_partial ===
(details.process.sample_limit_reached ||
details.process.sampling_failures > 0) &&
(details.process.sampling_failures === 0) ===
(details.process.first_sampling_failure ===
null), "truncation_details.process", "sampling coverage must agree with retained samples and observed failures");
require(capture.truncated ===
hasCaptureTruncation(
details,
), "truncation_details", "aggregate truncation must match producer coverage");
for (const [name, retention, count] of [
["raw_terminal", details.raw_terminal, capture.frames.length],
[
"rendered_terminal",
details.rendered_terminal,
capture.rendered_frames.length,
],
] as const) {
require(retention.retained_frames === count &&
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");
}
for (const [name, coverage, files, checkpoint] of [
[
"filesystem_before",
details.filesystem_before,
capture.files_before,
capture.filesystem_checkpoints[0],
],
[
"filesystem_after",
details.filesystem_after,
capture.files_after,
capture.filesystem_checkpoints[1],
],
] as const) {
const unhashedFiles = new Map(
files
.filter((file) => file.type === "file" && file.sha256 === null)
.map((file) => [file.path, file]),
);
require(coverage.hash_omissions.length === unhashedFiles.size &&
new Set(coverage.hash_omissions.map(({ path }) => path)).size ===
unhashedFiles.size &&
coverage.hash_omissions.every(
(omission) =>
unhashedFiles.get(omission.path)?.size === omission.size_bytes &&
omission.remaining_budget_bytes <= coverage.hash_budget_bytes,
), `truncation_details.${name}.hash_omissions`, "every retained regular file without a digest must have one matching omission reason");
require(coverage.hashed_bytes ===
files
.filter((file) => file.type === "file" && file.sha256 !== null)
.reduce((sum, file) => sum + file.size, 0) &&
coverage.hashed_bytes <= coverage.hash_budget_bytes &&
files.length <=
coverage.files_limit, `truncation_details.${name}`, "filesystem budget accounting does not match retained observations");
require(coverage.enumeration_truncated ===
coverage.enumeration_reasons.length > 0 &&
checkpoint?.truncated ===
(coverage.enumeration_truncated ||
coverage.hash_omissions.length >
0), `truncation_details.${name}`, "filesystem checkpoint truncation must match enumeration and digest coverage");
}
};
/** Recompute commitments and cross-field invariants without side effects. */
export const collectProcessCaptureIssues = (
capture: UnverifiedProcessCapture,
@@ -133,5 +209,6 @@ export const collectProcessCaptureIssues = (
validateOrdering(capture, require);
validateEventJournal(capture, require);
validateLifecycle(capture, require);
validateCoverage(capture, require);
return issues;
};