From ec29a6deae22ba82c5a88db092fbe65bd03b8bd4 Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:09:44 -0500 Subject: [PATCH] fix(process): retain proven file differences with partial coverage (#13364) --- docs/guides/runtime/process-capture.md | 6 +- .../process/processCapture.comparison.test.ts | 169 ++++++++++++++++- src/domain/process/processComparison.ts | 177 +++++++++++------- 3 files changed, 274 insertions(+), 78 deletions(-) diff --git a/docs/guides/runtime/process-capture.md b/docs/guides/runtime/process-capture.md index 2ba05ac39b..08d9b90b5d 100644 --- a/docs/guides/runtime/process-capture.md +++ b/docs/guides/runtime/process-capture.md @@ -620,8 +620,10 @@ Every capture requires `truncation_details`, with separate accounting for: remain available and issued reads settle before final coverage is returned. The aggregate `truncated` flag summarizes these observations. Comparisons retain -results for unaffected dimensions and mark affected dimensions unknown; an -incomplete capture cannot locate the first divergence across all dimensions. +results for unaffected dimensions. If the hash budget omits a file's digest, +different retained digests for another path still mark the filesystem dimension +`changed`. Incomplete captures leave the first divergence across all dimensions +unknown. Trace assertions use coverage for the sources they select, so an assertion about complete raw terminal output need not fail because rendered snapshots were omitted. A trace difference applies to the dimensions identified by its diff --git a/src/domain/process/processCapture.comparison.test.ts b/src/domain/process/processCapture.comparison.test.ts index be305ca673..35663f9d9a 100644 --- a/src/domain/process/processCapture.comparison.test.ts +++ b/src/domain/process/processCapture.comparison.test.ts @@ -552,11 +552,168 @@ it("preserves independently observed file changes alongside an unknown effect", expect(compareFullyObserved(a, b, options)).toMatchObject({ filesystem: "changed", status: "changed", + terminal: "unchanged", + interaction: "unchanged", + exit: "unchanged", + process: "unchanged", first_divergence: { status: "unknown" }, }); } }); +const captureWithFileHashBudget = ( + sha256: string, + omitDigest: boolean, + conflictingDigest?: string, +) => { + const base = emptyCapture(); + const known = { + path: "root_0:known.txt", + type: "file" as const, + mode: 0o644, + size: 4, + sha256, + symlink_target: null, + }; + const large = { + ...known, + path: "root_0:large.bin", + size: 4096, + sha256: omitDigest ? null : "c".repeat(64), + }; + const files = [ + known, + ...(conflictingDigest === undefined + ? [] + : [{ ...known, sha256: conflictingDigest }]), + large, + ]; + const hashedBytes = + known.size + + (omitDigest ? 0 : large.size) + + (conflictingDigest === undefined ? 0 : known.size); + const coverage = { + ...base.truncation_details.filesystem_before, + hash_budget_bytes: hashedBytes, + hashed_bytes: hashedBytes, + hash_omissions: omitDigest + ? [ + { + path: large.path, + size_bytes: large.size, + remaining_budget_bytes: 0, + reason: "file_bytes_budget" as const, + system_code: null, + }, + ] + : [], + }; + return { + ...base, + files_before: files, + files_after: files, + filesystem_checkpoints: base.filesystem_checkpoints.map((checkpoint) => ({ + ...checkpoint, + files, + truncated: omitDigest, + })), + truncated: omitDigest, + residual_unknowns: omitDigest + ? [ + { + scope: "filesystem" as const, + reason: "Whole-file digest omitted.", + }, + ] + : [], + truncation_details: { + ...base.truncation_details, + filesystem_before: coverage, + filesystem_after: coverage, + }, + }; +}; + +it("preserves digest differences when another file exceeds the hash budget", () => { + const left = captureWithFileHashBudget("a".repeat(64), true); + const right = captureWithFileHashBudget("b".repeat(64), true); + const completeLeft = captureWithFileHashBudget("a".repeat(64), false); + const completeRight = captureWithFileHashBudget("b".repeat(64), false); + + for (const [a, b] of [ + [left, left], + [left, completeLeft], + [completeLeft, left], + ] as const) + expect(compareProcessCaptures(a, b)).toMatchObject({ + status: "unknown", + filesystem: "unknown", + first_divergence: { status: "unknown" }, + }); + expect(compareProcessCaptures(completeLeft, completeRight)).toMatchObject({ + status: "changed", + filesystem: "changed", + first_divergence: { + status: "found", + dimension: "filesystem", + fields: ["files[0].sha256"], + }, + }); + for (const [a, b] of [ + [left, right], + [right, left], + [left, completeRight], + [completeRight, left], + ] as const) + expect(compareProcessCaptures(a, b)).toMatchObject({ + status: "changed", + filesystem: "changed", + first_divergence: { status: "unknown" }, + }); +}); + +it("keeps conflicting digests for one path unknown under partial coverage", () => { + const known = captureWithFileHashBudget("a".repeat(64), true); + const ambiguous = captureWithFileHashBudget( + "a".repeat(64), + true, + "b".repeat(64), + ); + for (const [left, right] of [ + [ambiguous, ambiguous], + [known, ambiguous], + [ambiguous, known], + ] as const) + expect(compareProcessCaptures(left, right)).toMatchObject({ + status: "unknown", + filesystem: "unknown", + first_divergence: { status: "unknown" }, + }); +}); + +it("keeps partial file differences unknown when legacy normalization differs", () => { + const left = captureWithFileHashBudget("a".repeat(64), true); + const right = captureWithFileHashBudget("b".repeat(64), true); + const normalization = { + ...right.normalization, + time_bucket_ms: right.normalization.time_bucket_ms + 1, + }; + expect( + compareProcessCaptures(left, { + ...right, + normalization, + manifest: { + ...right.manifest, + normalization_sha256: digestProcessCommitment(normalization), + }, + }), + ).toMatchObject({ + status: "unknown", + filesystem: "unknown", + first_divergence: { status: "unknown" }, + }); +}); + it("compares exact symlink bytes beside an unrelated unknown effect", () => { const base = emptyCapture(); const link = { @@ -1118,10 +1275,14 @@ it("keeps filesystem comparison unknown without coverage and reports observed ch { scope: "filesystem" as const, reason: "watcher unavailable" }, ], }; - expect(compareFullyObserved(incomplete, incomplete)).toMatchObject({ - filesystem: "unknown", - status: "unknown", - }); + for (const [left, right] of [ + [capture, incomplete], + [incomplete, capture], + ] as const) + expect(compareFullyObserved(left, right)).toMatchObject({ + filesystem: "unknown", + status: "unknown", + }); const finalFile = { path: "root_0:created.txt", diff --git a/src/domain/process/processComparison.ts b/src/domain/process/processComparison.ts index 15278d2bab..14ed56d5f6 100644 --- a/src/domain/process/processComparison.ts +++ b/src/domain/process/processComparison.ts @@ -7,6 +7,7 @@ import type { } from "./processObservation.js"; import type { ProcessCapture } from "./processCaptureParsing.js"; +import type { FileState } from "./processCapture.js"; import { comparableProcessObservationPayload, comparableTerminalFrame, @@ -311,12 +312,20 @@ const hasUnknownFilesystemEffects = (capture: ProcessCapture): boolean => effects.some(({ status }) => status === "unknown"), ); +/** A repeated path has no single retained state that can prove a difference. */ +const indexCheckpointFiles = (files: readonly FileState[]) => { + const index = new Map(); + for (const file of files) + index.set(file.path, index.has(file.path) ? null : file); + return index; +}; + /** A missing descendant is evidence only under an observed, fully enumerated root. */ const checkpointProvesAbsence = ( capture: ProcessCapture, checkpoint: ProcessCapture["filesystem_checkpoints"][number], path: string, - files: ReadonlyMap, + files: ReadonlyMap, ): boolean => { const colon = path.indexOf(":"); if (colon < 1) return false; @@ -353,38 +362,45 @@ const hasKnownFilesystemDifference = ( ({ name }) => name === checkpoint.name, ); if (peer === undefined) return false; - const files = new Map(peer.files.map((file) => [file.path, file])); - return checkpoint.files.some((file) => { - const other = files.get(file.path); - if (other === undefined) - return checkpointProvesAbsence(right, peer, file.path, files); + const files = indexCheckpointFiles(checkpoint.files); + const peers = indexCheckpointFiles(peer.files); + for (const [path, file] of files) { + if (file === null) continue; + const other = peers.get(path); + if (other === undefined) { + if (checkpointProvesAbsence(right, peer, path, peers)) return true; + continue; + } + if (other === null) continue; if ( file.type !== other.type || file.mode !== other.mode || file.size !== other.size ) return true; - if (file.type === "file" && other.type === "file") - return ( - file.sha256 !== null && - other.sha256 !== null && - file.sha256 !== other.sha256 - ); - if (file.type === "symlink" && other.type === "symlink") - return file.symlink_target_hex !== other.symlink_target_hex; - return false; - }); - }) || - right.filesystem_checkpoints.some((checkpoint) => { - const peer = left.filesystem_checkpoints.find( - ({ name }) => name === checkpoint.name, - ); - if (peer === undefined) return false; - const files = new Map(peer.files.map((file) => [file.path, file])); - return checkpoint.files.some( - ({ path }) => - !files.has(path) && checkpointProvesAbsence(left, peer, path, files), - ); + if ( + file.type === "file" && + other.type === "file" && + file.sha256 !== null && + other.sha256 !== null && + file.sha256 !== other.sha256 + ) + return true; + if ( + file.type === "symlink" && + other.type === "symlink" && + file.symlink_target_hex !== other.symlink_target_hex + ) + return true; + } + for (const [path, file] of peers) + if ( + file !== null && + !files.has(path) && + checkpointProvesAbsence(left, checkpoint, path, files) + ) + return true; + return false; }); type DivergenceDimension = Exclude< @@ -733,21 +749,30 @@ const compareDimensions = ( const classify = ( dimension: ProcessComparisonDimension, source: ProcessObservationSource, - ): ComparisonStatus => - !sameNormalization || - hasUnknown(left, dimension) || - hasUnknown(right, dimension) || - (dimension === "filesystem" && - (hasUnknownFilesystemEffects(left) || - hasUnknownFilesystemEffects(right)) && - !hasKnownFilesystemDifference(left, right)) || - processSourceTruncated(left, source) || - processSourceTruncated(right, source) - ? "unknown" - : classifyCollection( - comparableProcessDimensionObservations(left, dimension), - comparableProcessDimensionObservations(right, dimension), - ); + ): ComparisonStatus => { + if (!sameNormalization) return "unknown"; + const truncated = + processSourceTruncated(left, source) || + processSourceTruncated(right, source); + if ( + dimension === "filesystem" && + (truncated || + hasUnknownFilesystemEffects(left) || + hasUnknownFilesystemEffects(right)) + ) { + return hasKnownFilesystemDifference(left, right) ? "changed" : "unknown"; + } + if ( + truncated || + hasUnknown(left, dimension) || + hasUnknown(right, dimension) + ) + return "unknown"; + return classifyCollection( + comparableProcessDimensionObservations(left, dimension), + comparableProcessDimensionObservations(right, dimension), + ); + }; return { terminal: classify("terminal", "terminal_rendered"), interaction: classify("interaction", "interaction"), @@ -801,9 +826,18 @@ export const compareProcessCaptures = ( new Set(timedSources.keys()), ); const ignoredInteractionFields = timedSources.get("interaction"); + const capturesTruncated = left.truncated || right.truncated; + const unresolvedFilesystemEffects = + !capturesTruncated && + (hasUnknownFilesystemEffects(left) || hasUnknownFilesystemEffects(right)); const divergenceCandidates = PROCESS_COMPARISON_DIMENSIONS.map( (dimension) => { - if (dimensions[dimension] === "unknown") return null; + if ( + capturesTruncated || + unresolvedFilesystemEffects || + dimensions[dimension] === "unknown" + ) + return null; const observedLeft = processDimensionObservations(left, dimension); const observedRight = processDimensionObservations(right, dimension); // A trace may explicitly make retained observer timing part of the @@ -869,43 +903,42 @@ export const compareProcessCaptures = ( : candidate, ), ); - const firstDivergence = - left.truncated || right.truncated + const firstDivergence = capturesTruncated + ? ({ + status: "unknown", + reason: + "Incomplete observations prevent locating the first divergence; unaffected dimensions retain their comparisons.", + } as const) + : unresolvedFilesystemEffects ? ({ status: "unknown", reason: - "Incomplete observations prevent locating the first divergence; unaffected dimensions retain their comparisons.", + "Unresolved filesystem effects prevent locating the first divergence, even when another change is observed.", } as const) - : hasUnknownFilesystemEffects(left) || hasUnknownFilesystemEffects(right) - ? ({ - status: "unknown", - reason: - "Unresolved filesystem effects prevent locating the first divergence, even when another change is observed.", - } as const) - : trace?.verdict === "equivalent" && - deriveProcessComparisonStatus( - PROCESS_COMPARISON_DIMENSIONS.map( - (dimension) => dimensions[dimension], - ), - ) === "unchanged" - ? ({ status: "none" } as const) - : observedFirstDivergence.status === "found" - ? observedFirstDivergence - : trace?.verdict === "different" || trace?.verdict === "unknown" + : trace?.verdict === "equivalent" && + deriveProcessComparisonStatus( + PROCESS_COMPARISON_DIMENSIONS.map( + (dimension) => dimensions[dimension], + ), + ) === "unchanged" + ? ({ status: "none" } as const) + : observedFirstDivergence.status === "found" + ? observedFirstDivergence + : trace?.verdict === "different" || trace?.verdict === "unknown" + ? ({ + status: "unknown", + reason: + "The declared trace differs or has insufficient journal evidence; per-source observations cannot locate its first cross-source divergence.", + } as const) + : !sameNormalization || + left.residual_unknowns.length > 0 || + right.residual_unknowns.length > 0 ? ({ status: "unknown", reason: - "The declared trace differs or has insufficient journal evidence; per-source observations cannot locate its first cross-source divergence.", + "Residual unknowns prevent proving that no divergence occurred.", } as const) - : !sameNormalization || - left.residual_unknowns.length > 0 || - right.residual_unknowns.length > 0 - ? ({ - status: "unknown", - reason: - "Residual unknowns prevent proving that no divergence occurred.", - } as const) - : observedFirstDivergence; + : observedFirstDivergence; const observedStatus = deriveProcessComparisonStatus( PROCESS_COMPARISON_DIMENSIONS.map((dimension) => dimensions[dimension]), );