From 17aea02bbc3c7c1a3553a0a8b9967e7c53c997ef Mon Sep 17 00:00:00 2001 From: Alex Flom Date: Tue, 3 Nov 2026 08:19:21 -0700 Subject: [PATCH] fix(snapshot): canonicalize structured values without a redundant depth-limited parse --- VERIFICATION.md | 18 ++++++ crates/lexlean/src/artifact/canonical_json.rs | 4 +- crates/lexlean/src/artifact/snapshot.rs | 64 ++++++++++++++++++- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/VERIFICATION.md b/VERIFICATION.md index 090cf8165e..da42aa66c0 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -39,6 +39,24 @@ Outside `vv`: ## Falsifiability records +### semantic snapshot nesting can fail + +Before the correction for #430, the focused +`artifact::snapshot::tests` library filter ran two cases: existing shallow +snapshot bytes passed, while serializing a 160-level linked value panicked +at `snapshot.rs` with `recursion limit exceeded`. The snapshot DTO had +already been constructed; its redundant byte parse imposed serde_json's +raw-reader nesting limit on that structured value. + +Snapshot serialization now converts its structured value directly into the +canonical JSON domain. The checks excluding null and non-integer numbers +remain shared with raw parsing; raw parsing still checks duplicate keys. +The deep regression round-trips the complete snapshot and its content digest, +and the shallow regression compares exact bytes with the previous recipe. +All 221 LexLean library tests pass on release/v0.4 with ordinary test stacks. +This is library evidence, not a complete `vv` or consuming-app attestation. + + Each gate below was made to fail by planting a defect, running the gate's command, recording the failure, and removing the defect. The observed lines are verbatim gate output (paths abbreviated to the repository root). `cargo xtask release-check` requires a `### can fail` record for every gate and audit named in `repo_model::release::GATES`. ### production bundle inventory can fail diff --git a/crates/lexlean/src/artifact/canonical_json.rs b/crates/lexlean/src/artifact/canonical_json.rs index 7d26a88905..74c326ded0 100644 --- a/crates/lexlean/src/artifact/canonical_json.rs +++ b/crates/lexlean/src/artifact/canonical_json.rs @@ -111,7 +111,9 @@ impl Json { Self::from_serde(&value) } - fn from_serde(value: &serde_json::Value) -> Result { + /// Convert an already structured value while retaining the canonical domain checks. + /// Raw byte inputs must use `parse` so duplicate keys are checked before decoding. + pub(crate) fn from_serde(value: &serde_json::Value) -> Result { match value { serde_json::Value::Null => Err("canonical JSON has no null".to_owned()), serde_json::Value::Bool(b) => Ok(Self::Bool(*b)), diff --git a/crates/lexlean/src/artifact/snapshot.rs b/crates/lexlean/src/artifact/snapshot.rs index aecf0d64d0..5c1c4f3af0 100644 --- a/crates/lexlean/src/artifact/snapshot.rs +++ b/crates/lexlean/src/artifact/snapshot.rs @@ -442,8 +442,9 @@ impl SemanticSnapshot { #[must_use] pub fn to_json(&self) -> Json { let value = serde_json::to_value(self).expect("snapshot DTO serializes"); - let bytes = serde_json::to_vec(&value).expect("snapshot JSON serializes"); - Json::parse(&bytes).expect("snapshot DTO uses the canonical JSON domain") + // The DTO already has unique object keys. Re-parsing its serialization + // imposes the raw JSON reader's nesting limit on accepted linked IR. + Json::from_serde(&value).expect("snapshot DTO uses the canonical JSON domain") } /// Canonical JSON file bytes, including LexLean's single final LF. @@ -594,3 +595,62 @@ impl SnapshotAxiomPolicy { &self.axioms } } + +#[cfg(test)] +mod tests { + use super::*; + + fn nested_snapshot(depth: usize) -> SemanticSnapshot { + let mut linked_ir = serde_json::json!("leaf"); + for _ in 0..depth { + linked_ir = serde_json::Value::Array(vec![linked_ir]); + } + let digest = Sha256Digest::of(b"snapshot fixture"); + SemanticSnapshot { + spec: "lexlean/semantic-snapshot/2".into(), + source_id: digest, + semantic_id: digest, + compiler_semantics_id: digest, + language: "1.2".into(), + modules: vec![SnapshotModule { + name: "Main".into(), + lean_module: "Main".into(), + origin: None, + imports: vec![], + glossary: vec![], + visible_packages: vec![], + source: SnapshotSource { + path: "src/Main.lex.tex".into(), + sha256: digest, + }, + declarations: vec![], + core: None, + semantic: None, + linked_ir, + }], + lexicon_closure: serde_json::json!({}), + } + } + + #[test] + fn canonical_snapshot_preserves_deep_linked_values() { + let snapshot = nested_snapshot(160); + let bytes = snapshot.canonical_bytes(); + let mut reader = serde_json::Deserializer::from_slice(&bytes); + reader.disable_recursion_limit(); + let restored = SemanticSnapshot::deserialize(&mut reader).expect("deep snapshot JSON"); + reader.end().expect("complete snapshot"); + assert_eq!(restored, snapshot); + assert_eq!(restored.canonical_bytes(), bytes); + assert_eq!(snapshot.snapshot_id(), Sha256Digest::of(&bytes)); + } + + #[test] + fn canonical_snapshot_preserves_existing_bytes() { + let snapshot = nested_snapshot(4); + let prior = Json::parse(&serde_json::to_vec(&snapshot).expect("snapshot JSON")) + .expect("canonical domain") + .to_file_bytes(); + assert_eq!(snapshot.canonical_bytes(), prior); + } +}