{"record":{"id":"87c6b1c87f7efb80","repo":"astrid-runtime/astrid","slug":"encoded-evidence-delta-did-not-reconstruct-its-tar","errorCode":null,"errorMessage":"encoded evidence delta did not reconstruct its target","messagePattern":"encoded evidence delta did not reconstruct its target","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/astrid-storage-chunker-evidence/src/sketch.rs","lineNumber":606,"sourceCode":"    for target_chunk in &target.chunks {\n        if let Some(base_chunk) = by_id.get(&target_chunk.id) {\n            if base.record(base_chunk.id)?.canonical_bytes()\n                != target.record(target_chunk.id)?.canonical_bytes()\n            {\n                bail!(\"equal chunk identities named different bytes in delta evidence\");\n            }\n            push_copy(&mut operations, base_chunk.offset, base_chunk.length)?;\n        } else {\n            push_add(\n                &mut operations,\n                target.record(target_chunk.id)?.canonical_bytes(),\n            )?;\n        }\n    }\n    let encoded = encode_delta(base.file, target.file, target.logical_bytes, &operations)?;\n    let reconstructed = apply_delta(&base.materialize()?, &encoded)?;\n    if reconstructed != target.materialize()? {\n        bail!(\"encoded evidence delta did not reconstruct its target\");\n    }\n    Ok(u64::try_from(encoded.len())?)\n}\n\nfn push_copy(operations: &mut Vec<DeltaOperation>, offset: u64, length: u64) -> Result<()> {\n    if let Some(DeltaOperation::Copy {\n        offset: previous_offset,\n        length: previous_length,\n    }) = operations.last_mut()\n        && previous_offset.checked_add(*previous_length) == Some(offset)\n    {\n        *previous_length = checked_add(*previous_length, length, \"copy extent length\")?;\n        return Ok(());\n    }\n    operations.push(DeltaOperation::Copy { offset, length });\n    Ok(())\n}\n","sourceCodeStart":588,"sourceCodeEnd":624,"githubUrl":"https://github.com/astrid-runtime/astrid/blob/affd8760f44190dbdfbec23403f4c4b642c33112/crates/astrid-storage-chunker-evidence/src/sketch.rs#L588-L624","documentation":"After encoding a delta (Copy/Add operations) between two versions, `delta_size` self-verifies by applying the encoded delta to the base materialization. If the reconstructed bytes differ from the target's materialization, the delta evidence is wrong (bad chunk map, offset corruption, or encoder bug) and the measurement aborts rather than reporting an incorrect delta size.","triggerScenarios":"Calling `measure_target` where the delta produced by `delta_size` fails its round-trip check: `apply_delta(base.materialize(), encode_delta(...))` yields bytes != target.materialize(). Happens when chunk offsets/lengths in either version's chunk list are wrong, or records were mutated between encode and apply.","commonSituations":"Corrupted chunk descriptors with stale offsets after a store was rewritten; concurrent mutation of records during measurement; a bug in custom delta operation generation.","solutions":["Re-chunk/rebuild both versions' descriptors so chunk offsets and lengths match their actual byte layout, then re-measure.","Check for concurrent access: ensure records are not mutated while delta_size runs (snapshot or lock the store).","Validate each chunk's offset+length fits within logical_bytes before encoding.","If a custom operation producer was added to delta_size, test its operations against apply_delta in isolation."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"fn descriptors_consistent(version: &Version) -> bool {\n    version.chunks.iter().all(|c| {\n        c.offset.saturating_add(c.length) <= version.logical_bytes\n    })\n}","typeGuard":null,"tryCatchPattern":"let size = match delta_size(&base, &target) {\n    Ok(size) => size,\n    Err(e) if e.to_string().contains(\"did not reconstruct its target\") => {\n        log::warn!(\"delta round-trip failed; falling back to full add-size estimate\");\n        target.materialize()?.len() as u64\n    },\n    Err(e) => return Err(e),\n};","preventionTips":["Snapshot records (no concurrent mutation) for the duration of delta measurement.","Validate chunk offset+length against logical_bytes before measuring.","Rebuild descriptors after any store rewrite instead of reusing cached chunk lists."],"tags":["rust","storage","delta-encoding","round-trip-failure"],"backgroundTag":"internal-invariant-violation","analyzedSha":"affd8760f44190dbdfbec23403f4c4b642c33112","analyzedAt":"2026-09-09T21:28:12.402Z","contentChangedAt":"2026-09-09T21:28:12.402Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}