From 837ce0cca3ecc30a3154bbf108f0963f23bb0527 Mon Sep 17 00:00:00 2001 From: Lee Faus Date: Tue, 3 Nov 2026 01:29:32 -0500 Subject: [PATCH 01/14] feat: checkpoint native span integration Add capability-gated schema-3 span changes, native recording and rendering, and transactional insertion of indexed and incoming span changes with portable semantic owners. Preserve schema-2 compatibility and add focused lifecycle and insertion tests. Checkpoint only: spans are not enabled by default. Concurrent span conflicts and span-backed name conflicts currently fail closed; rename with simultaneous content edits and legacy span semantic-owner migration remain incomplete. The diagnostic 25-suite core harness reported 8 passed and 17 failed suites, including 5 that exited before their assertion summaries. --- Cargo.toml | 2 +- atomic-core/src/change/change.rs | 50 +- atomic-core/src/change/format_v3/error.rs | 4 + .../fixtures/span_opaque_v2_reference.hex | 4 + atomic-core/src/change/format_v3/mod.rs | 3 +- .../src/change/format_v3/reader/sections.rs | 10 + .../src/change/format_v3/types/header.rs | 11 +- .../src/change/format_v3/types/section.rs | 13 +- .../src/change/format_v3/types/tests.rs | 21 +- .../src/change/format_v3/writer/sections.rs | 21 + .../change/format_v3/writer/state_machine.rs | 20 +- atomic-core/src/change/mod.rs | 1 + atomic-core/src/change/ops.rs | 98 + atomic-core/src/change/span_patch.rs | 526 ++++++ atomic-core/src/pristine/capability.rs | 15 +- atomic-core/src/pristine/mod.rs | 3 +- .../src/pristine/span_patch_storage.rs | 1170 ++++++++++++ atomic-core/src/pristine/txn/read.rs | 2 +- atomic-core/src/pristine/txn/write/mod.rs | 53 +- atomic-core/src/pristine/txn/write/tests.rs | 34 + .../src/record/workflow/assembly/mod.rs | 69 +- .../src/record/workflow/record/types.rs | 30 +- atomic-repository/src/apply/mod.rs | 74 +- .../src/redb_change_store/mod.rs | 43 +- .../src/redb_change_store/queries.rs | 34 +- .../src/redb_change_store/tests.rs | 92 + atomic-repository/src/repository/changes.rs | 50 +- atomic-repository/src/repository/content.rs | 130 +- atomic-repository/src/repository/insert.rs | 548 +++++- .../src/repository/materialize.rs | 248 ++- atomic-repository/src/repository/record.rs | 719 +++++++- .../src/repository/remote_cache.rs | 6 +- atomic-repository/src/repository/status.rs | 29 + atomic-repository/tests/native_span_record.rs | 1577 +++++++++++++++++ atomic-repository/tests/span_insert.rs | 788 ++++++++ 35 files changed, 6336 insertions(+), 162 deletions(-) create mode 100644 atomic-core/src/change/format_v3/fixtures/span_opaque_v2_reference.hex create mode 100644 atomic-core/src/change/span_patch.rs create mode 100644 atomic-core/src/pristine/span_patch_storage.rs create mode 100644 atomic-repository/tests/native_span_record.rs create mode 100644 atomic-repository/tests/span_insert.rs diff --git a/Cargo.toml b/Cargo.toml index 9ab35e97..d0e29e74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ authors = ["Atomic Contributors"] license = "Apache-2.0" repository = "https://github.com/atomicdotdev/atomic" homepage = "https://atomic.dev" -rust-version = "1.98" +rust-version = "1.90" [workspace.dependencies] # Core diff --git a/atomic-core/src/change/change.rs b/atomic-core/src/change/change.rs index cac65e08..d56eb9a6 100644 --- a/atomic-core/src/change/change.rs +++ b/atomic-core/src/change/change.rs @@ -192,6 +192,7 @@ impl Change { dependencies, extra_known: Vec::new(), metadata: Vec::new(), + span: None, provenance: Vec::new(), hunks, file_ops, @@ -217,6 +218,7 @@ impl Change { dependencies: Vec::new(), extra_known: Vec::new(), metadata: Vec::new(), + span: None, provenance: Vec::new(), hunks: Vec::new(), file_ops: Vec::new(), @@ -328,6 +330,10 @@ impl Change { computed: computed.to_string(), }); } + if let Some(span) = &self.hashed.span { + span.validate_content_ranges(self.contents.len() as u64) + .map_err(|error| ChangeError::Invalid(error.to_string()))?; + } Ok(()) } @@ -447,7 +453,10 @@ impl Change { file_header_builder = file_header_builder.with_signature(); } - let file_header = file_header_builder.build(); + let mut file_header = file_header_builder.build(); + if self.hashed.span.is_some() { + file_header.version = SPAN_FORMAT_VERSION; + } // 4. Create writer and write preamble let mut change_writer = ChangeWriter::new(writer, WriterOptions::default()); @@ -502,6 +511,13 @@ impl Change { change_writer.write_graph_section(&payload_bytes)?; } + if let Some(span) = &self.hashed.span { + let bytes = span + .encode() + .map_err(|error| ChangeError::Invalid(error.to_string()))?; + change_writer.write_span_section(&bytes)?; + } + // 7. Write SEMANTIC section(s) if !self.hashed.file_ops.is_empty() { // Serialize all file_ops together as one semantic section @@ -588,7 +604,7 @@ impl Change { let file_header = *change_reader.file_header(); let format_version = file_header.version; let hash_table = change_reader.hash_table().clone(); - let strict_v2 = format_version == FORMAT_VERSION; + let strict_v2 = format_version == FORMAT_VERSION || format_version == SPAN_FORMAT_VERSION; // 2. Read all sections. V2 enforces the canonical section manifest; // V1 keeps its historical framing and gains only semantic defaults. @@ -597,6 +613,7 @@ impl Change { let mut dependencies: Vec = Vec::new(); let mut provenance: Vec = Vec::new(); let mut hunks: Vec>> = Vec::new(); + let mut span = None; let mut file_ops: Vec = Vec::new(); let mut contents: Vec = Vec::new(); let mut unhashed: Option = None; @@ -688,6 +705,23 @@ impl Change { }); } } + SectionType::Span => { + if format_version != SPAN_FORMAT_VERSION || span.is_some() { + return Err(ChangeError::Invalid( + "unexpected or duplicate SPAN section".into(), + )); + } + let patch = super::span_patch::SpanPatch::decode(§ion.payload) + .map_err(|error| ChangeError::Invalid(error.to_string()))?; + if patch + .encode() + .map_err(|error| ChangeError::Invalid(error.to_string()))? + != section.payload + { + return Err(ChangeError::Invalid("noncanonical SPAN section".into())); + } + span = Some(patch); + } SectionType::Semantic => { semantic_count += 1; let ops: Vec = postcard::from_bytes(§ion.payload) @@ -739,6 +773,7 @@ impl Change { || semantic_count != file_header.semantic_section_count || content_count != file_header.contents_chunks || unhashed_count != expected_unhashed + || (format_version == SPAN_FORMAT_VERSION) != span.is_some() { return Err(ChangeError::Invalid( "V2 section manifest does not match the file header".to_string(), @@ -790,6 +825,7 @@ impl Change { dependencies, extra_known, metadata, + span, provenance, hunks, file_ops, @@ -1066,6 +1102,10 @@ pub struct HashedChange { /// Application-specific metadata that affects the change hash. pub metadata: Vec, + /// Immutable range operations in a separately hashed schema-3 section. + /// Opaque metadata is never interpreted as a span discriminator. + pub span: Option, + /// AI provenance information (optional) /// /// Tracks AI involvement in creating this change, including: @@ -1169,7 +1209,9 @@ impl HashedChange { /// Check if this change has any hunks. pub fn is_empty(&self) -> bool { - self.hunks.is_empty() && self.file_ops.is_empty() + self.hunks.is_empty() + && self.file_ops.is_empty() + && self.span.as_ref().is_none_or(|span| span.ops.is_empty()) } /// Get the number of hunks. @@ -1261,6 +1303,7 @@ mod tests { dependencies: Vec::new(), extra_known: Vec::new(), metadata: Vec::new(), + span: None, provenance: Vec::new(), hunks: Vec::new(), file_ops: Vec::new(), @@ -1290,6 +1333,7 @@ mod tests { dependencies: vec![dep1, dep2], extra_known: vec![known1], metadata: Vec::new(), + span: None, provenance: Vec::new(), hunks: Vec::new(), file_ops: Vec::new(), diff --git a/atomic-core/src/change/format_v3/error.rs b/atomic-core/src/change/format_v3/error.rs index 93b1ad7a..381b7e32 100644 --- a/atomic-core/src/change/format_v3/error.rs +++ b/atomic-core/src/change/format_v3/error.rs @@ -47,6 +47,10 @@ pub const LEGACY_FORMAT_VERSION: u32 = 1; /// Current ATOM streaming schema version emitted by writers. pub const FORMAT_VERSION: u32 = 2; +/// Schema for changes carrying one separately hashed span section. Ordinary +/// changes retain schema 2 and their existing identities. +pub const SPAN_FORMAT_VERSION: u32 = 3; + /// Maximum number of unique hashes in the deduplication table. /// /// This is `u16::MAX - 1` because index `0xFFFF` is reserved as a sentinel diff --git a/atomic-core/src/change/format_v3/fixtures/span_opaque_v2_reference.hex b/atomic-core/src/change/format_v3/fixtures/span_opaque_v2_reference.hex new file mode 100644 index 00000000..8dd8e4d4 --- /dev/null +++ b/atomic-core/src/change/format_v3/fixtures/span_opaque_v2_reference.hex @@ -0,0 +1,4 @@ +41544f4d02000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000013f00000028b52ffd0058b10100116f706171756520763220666978747572650014323032332d31312d31345432323a31333a32305a00000000000007415453 +50010000020a00000028b52ffd005809000000200000000037f971495e33dd1f5fb6a60cfee5f06fadbfdf28ae5f1c35e0dec848ce9a08e60a0000001300000028b52ffd00585100006f6c6420627974 +65730acf90221730dcd4c8f3088b9b00001c317c251a0f66536aabd8a75dd902fcef8e diff --git a/atomic-core/src/change/format_v3/mod.rs b/atomic-core/src/change/format_v3/mod.rs index 90d12ac8..c9d1034d 100644 --- a/atomic-core/src/change/format_v3/mod.rs +++ b/atomic-core/src/change/format_v3/mod.rs @@ -200,7 +200,8 @@ pub mod writer; // Error types pub use error::{ - FormatError, FormatResult, FORMAT_VERSION, LEGACY_FORMAT_VERSION, MAGIC, MAX_HASH_TABLE_ENTRIES, + FormatError, FormatResult, FORMAT_VERSION, LEGACY_FORMAT_VERSION, MAGIC, + MAX_HASH_TABLE_ENTRIES, SPAN_FORMAT_VERSION, }; // Core types diff --git a/atomic-core/src/change/format_v3/reader/sections.rs b/atomic-core/src/change/format_v3/reader/sections.rs index 09fe381f..bcaaa1ad 100644 --- a/atomic-core/src/change/format_v3/reader/sections.rs +++ b/atomic-core/src/change/format_v3/reader/sections.rs @@ -393,10 +393,20 @@ impl<'r, R: Read> ChangeReader<'r, R> { /// /// Skipped sections are still fed through the hasher for verification. fn sections_of_type(&mut self, target: SectionType) -> FormatResult> { + if self.file_header.version == super::super::error::SPAN_FORMAT_VERSION { + return Err(FormatError::InvalidChangeMetadata { + reason: "selective section reads cannot interpret span changes".into(), + }); + } let mut result = Vec::new(); loop { match self.peek_section_type()? { + Some(SectionType::Span) => { + return Err(FormatError::InvalidChangeMetadata { + reason: "SPAN section requires schema 3 and a span-aware reader".into(), + }); + } Some(st) if st == target => { if let Some(section) = self.next_section()? { result.push(section); diff --git a/atomic-core/src/change/format_v3/types/header.rs b/atomic-core/src/change/format_v3/types/header.rs index c6f9d4f1..c6041a1a 100644 --- a/atomic-core/src/change/format_v3/types/header.rs +++ b/atomic-core/src/change/format_v3/types/header.rs @@ -5,7 +5,8 @@ //! [`FileHeaderBuilder`] provides a fluent API for constructing headers. use super::super::error::{ - FormatError, FormatResult, FORMAT_VERSION, LEGACY_FORMAT_VERSION, MAGIC, MAX_HASH_TABLE_ENTRIES, + FormatError, FormatResult, FORMAT_VERSION, LEGACY_FORMAT_VERSION, MAGIC, + MAX_HASH_TABLE_ENTRIES, SPAN_FORMAT_VERSION, }; use super::builder::FileHeaderBuilder; use std::fmt; @@ -291,7 +292,10 @@ impl FileHeader { } let version = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]); - if version != LEGACY_FORMAT_VERSION && version != FORMAT_VERSION { + if version != LEGACY_FORMAT_VERSION + && version != FORMAT_VERSION + && version != SPAN_FORMAT_VERSION + { return Err(FormatError::UnsupportedVersion { expected: FORMAT_VERSION, got: version, @@ -371,6 +375,9 @@ impl FileHeader { count += 1; } count += self.graph_section_count; + if self.version == SPAN_FORMAT_VERSION { + count += 1; + } count += self.semantic_section_count; count += self.contents_chunks; if self.flags.has(FileHeaderFlags::HAS_UNHASHED) { diff --git a/atomic-core/src/change/format_v3/types/section.rs b/atomic-core/src/change/format_v3/types/section.rs index 5e1ab214..bb2406c7 100644 --- a/atomic-core/src/change/format_v3/types/section.rs +++ b/atomic-core/src/change/format_v3/types/section.rs @@ -114,6 +114,10 @@ pub enum SectionType { /// /// Payload: `zstd(json(Value))` Unhashed = 0xF0, + + /// Immutable range operations, stored as one hashed schema-3 section. + /// Kept last so existing postcard enum ordinals remain unchanged. + Span = 0x11, } impl SectionType { @@ -142,6 +146,7 @@ impl SectionType { 0x03 => Ok(SectionType::Provenance), 0x04 => Ok(SectionType::Signature), 0x10 => Ok(SectionType::Graph), + 0x11 => Ok(SectionType::Span), 0x20 => Ok(SectionType::Content), 0x30 => Ok(SectionType::Semantic), 0xF0 => Ok(SectionType::Unhashed), @@ -232,6 +237,7 @@ impl SectionType { SectionType::Provenance => "PROVENANCE", SectionType::Signature => "SIGNATURE", SectionType::Graph => "GRAPH", + SectionType::Span => "SPAN", SectionType::Content => "CONTENT", SectionType::Semantic => "SEMANTIC", SectionType::Unhashed => "UNHASHED", @@ -252,9 +258,10 @@ impl SectionType { SectionType::Provenance => 2, SectionType::Signature => 3, SectionType::Graph => 4, - SectionType::Semantic => 5, - SectionType::Content => 6, - SectionType::Unhashed => 7, + SectionType::Span => 5, + SectionType::Semantic => 6, + SectionType::Content => 7, + SectionType::Unhashed => 8, } } } diff --git a/atomic-core/src/change/format_v3/types/tests.rs b/atomic-core/src/change/format_v3/types/tests.rs index 636056af..5177a852 100644 --- a/atomic-core/src/change/format_v3/types/tests.rs +++ b/atomic-core/src/change/format_v3/types/tests.rs @@ -194,6 +194,7 @@ fn test_section_type_byte_values() { assert_eq!(SectionType::Dependencies.to_byte(), 0x02); assert_eq!(SectionType::Provenance.to_byte(), 0x03); assert_eq!(SectionType::Graph.to_byte(), 0x10); + assert_eq!(SectionType::Span.to_byte(), 0x11); assert_eq!(SectionType::Content.to_byte(), 0x20); assert_eq!(SectionType::Semantic.to_byte(), 0x30); assert_eq!(SectionType::Unhashed.to_byte(), 0xF0); @@ -206,6 +207,7 @@ fn test_section_type_from_byte_roundtrip() { SectionType::Dependencies, SectionType::Provenance, SectionType::Graph, + SectionType::Span, SectionType::Content, SectionType::Semantic, SectionType::Unhashed, @@ -221,12 +223,28 @@ fn test_section_type_from_byte_roundtrip() { #[test] fn test_section_type_from_byte_invalid() { // Test some invalid bytes - for byte in [0x00, 0x0F, 0x11, 0x21, 0x31, 0xFF] { + for byte in [0x00, 0x0F, 0x12, 0x21, 0x31, 0xFF] { let result = SectionType::from_byte(byte); assert!(result.is_err(), "byte 0x{:02X} should be invalid", byte); } } +#[test] +fn old_section_type_postcard_ordinals_are_unchanged() { + for (section, ordinal) in [ + (SectionType::Header, 0), + (SectionType::Dependencies, 1), + (SectionType::Provenance, 2), + (SectionType::Signature, 3), + (SectionType::Graph, 4), + (SectionType::Content, 5), + (SectionType::Semantic, 6), + (SectionType::Unhashed, 7), + ] { + assert_eq!(postcard::to_allocvec(§ion).unwrap(), vec![ordinal]); + } +} + #[test] fn test_section_type_is_hashed() { assert!(SectionType::Header.is_hashed()); @@ -234,6 +252,7 @@ fn test_section_type_is_hashed() { assert!(SectionType::Provenance.is_hashed()); assert!(!SectionType::Signature.is_hashed()); assert!(SectionType::Graph.is_hashed()); + assert!(SectionType::Span.is_hashed()); assert!(SectionType::Content.is_hashed()); assert!(SectionType::Semantic.is_hashed()); diff --git a/atomic-core/src/change/format_v3/writer/sections.rs b/atomic-core/src/change/format_v3/writer/sections.rs index 61c612ca..9b85a528 100644 --- a/atomic-core/src/change/format_v3/writer/sections.rs +++ b/atomic-core/src/change/format_v3/writer/sections.rs @@ -45,6 +45,27 @@ impl<'w, W: Write> ChangeWriter<'w, W> { Ok(()) } + /// Write the single hashed span payload required by schema 3. + pub fn write_span_section(&mut self, payload: &[u8]) -> FormatResult<()> { + use super::super::error::SPAN_FORMAT_VERSION; + self.ensure_metadata_complete()?; + if self.schema_version != SPAN_FORMAT_VERSION || self.wrote_span_section { + return Err(FormatError::InvalidHeader { + reason: "SPAN requires schema 3 and may appear only once".into(), + }); + } + self.transition_to_at_least(WriterState::WritingGraph)?; + if self.state != WriterState::WritingGraph { + return Err(FormatError::UnexpectedSection { + got: "SPAN".into(), + expected: "after GRAPH and before SEMANTIC or CONTENT".into(), + }); + } + self.write_raw_section(SectionType::Span, payload)?; + self.wrote_span_section = true; + Ok(()) + } + // ── Step 5: Per-File Semantic Sections ───────────────────────── /// Write a SEMANTIC section for a single file. diff --git a/atomic-core/src/change/format_v3/writer/state_machine.rs b/atomic-core/src/change/format_v3/writer/state_machine.rs index 4068e069..193cf1ad 100644 --- a/atomic-core/src/change/format_v3/writer/state_machine.rs +++ b/atomic-core/src/change/format_v3/writer/state_machine.rs @@ -4,7 +4,7 @@ //! and all public methods for writing sections of a V3 change file. //! Private helper methods live in the sibling `helpers` module. -use super::super::error::{FormatError, FormatResult, FORMAT_VERSION}; +use super::super::error::{FormatError, FormatResult, FORMAT_VERSION, SPAN_FORMAT_VERSION}; use super::super::hash_table::HashDedupTable; use super::super::types::{ envelope::encode_native_change_header_v2, FileHeader, SectionHeader, SectionType, Trailer, @@ -160,6 +160,9 @@ pub struct ChangeWriter<'w, W: Write> { /// Tracks whether the SIGNATURE section has been written. pub(super) wrote_signature_section: bool, + pub(super) schema_version: u32, + pub(super) wrote_span_section: bool, + /// The highest section ordering value written so far. /// Used to enforce monotonic section ordering. pub(super) last_section_ordering: Option, @@ -194,6 +197,8 @@ impl<'w, W: Write> ChangeWriter<'w, W> { wrote_header_section: false, wrote_deps_section: false, wrote_signature_section: false, + schema_version: 0, + wrote_span_section: false, last_section_ordering: None, } } @@ -224,7 +229,7 @@ impl<'w, W: Write> ChangeWriter<'w, W> { pub fn write_file_header(&mut self, header: &FileHeader) -> FormatResult<()> { self.expect_state(WriterState::Created, "write_file_header")?; header.validate()?; - if header.version != FORMAT_VERSION { + if header.version != FORMAT_VERSION && header.version != SPAN_FORMAT_VERSION { return Err(FormatError::InvalidHeader { reason: format!( "schema version {} is read-only; writers emit version {}", @@ -234,6 +239,7 @@ impl<'w, W: Write> ChangeWriter<'w, W> { } header.write_to(self.writer)?; + self.schema_version = header.version; self.stats.total_bytes_written += FileHeader::SIZE as u64; self.state = WriterState::FileHeaderWritten; Ok(()) @@ -508,6 +514,11 @@ impl<'w, W: Write> ChangeWriter<'w, W> { /// - I/O errors from writing the trailer. pub fn finalize(mut self) -> FormatResult { self.ensure_metadata_complete()?; + if (self.schema_version == SPAN_FORMAT_VERSION) != self.wrote_span_section { + return Err(FormatError::InvalidHeader { + reason: "schema 3 requires exactly one SPAN section; schema 2 forbids it".into(), + }); + } // Compute the final hash let hash = self.hasher.finalize(); @@ -554,6 +565,11 @@ impl<'w, W: Write> ChangeWriter<'w, W> { uncompressed_len: u64, ) -> FormatResult<()> { self.ensure_metadata_complete()?; + if section_type == SectionType::Span { + return Err(FormatError::InvalidHeader { + reason: "SPAN sections must use write_span_section".into(), + }); + } self.check_section_ordering(section_type)?; let section_header = SectionHeader::new(section_type, compressed_data.len() as u32); diff --git a/atomic-core/src/change/mod.rs b/atomic-core/src/change/mod.rs index b85c395c..ffaa8066 100644 --- a/atomic-core/src/change/mod.rs +++ b/atomic-core/src/change/mod.rs @@ -106,6 +106,7 @@ mod provenance; pub mod provenance_graph; pub mod session; pub mod signing; +pub mod span_patch; mod store; // Re-export all public types diff --git a/atomic-core/src/change/ops.rs b/atomic-core/src/change/ops.rs index 5535d933..973b8c93 100644 --- a/atomic-core/src/change/ops.rs +++ b/atomic-core/src/change/ops.rs @@ -305,6 +305,104 @@ impl FileOps { ids } + /// Translate existing semantic identities while preserving ROOT placeholders. + pub fn remap_referenced_node_ids( + &mut self, + mut remap: impl FnMut(crate::types::NodeId) -> Result, + ) -> Result<(), E> { + fn trunk( + id: &mut TrunkId, + f: &mut impl FnMut(crate::types::NodeId) -> Result, + ) -> Result<(), E> { + if !id.change_id().is_root() { + *id = TrunkId::new(f(id.change_id())?, id.file_idx()); + } + Ok(()) + } + fn branch( + id: &mut BranchId, + f: &mut impl FnMut(crate::types::NodeId) -> Result, + ) -> Result<(), E> { + if !id.change_id().is_root() { + *id = BranchId::new(f(id.change_id())?, id.branch_idx()); + } + Ok(()) + } + fn leaf( + id: &mut LeafId, + f: &mut impl FnMut(crate::types::NodeId) -> Result, + ) -> Result<(), E> { + if !id.change_id().is_root() { + *id = LeafId::new(f(id.change_id())?, id.leaf_idx()); + } + Ok(()) + } + fn leaves( + ops: &mut [LeafOp], + f: &mut impl FnMut(crate::types::NodeId) -> Result, + ) -> Result<(), E> { + for op in ops { + match op { + LeafOp::Insert { + after: Some(id), .. + } => leaf(id, f)?, + LeafOp::Delete { leaf: id } + | LeafOp::Replace { leaf: id, .. } + | LeafOp::Restore { leaf: id } => leaf(id, f)?, + LeafOp::Insert { after: None, .. } => {} + } + } + Ok(()) + } + trunk(&mut self.trunk_id, &mut remap)?; + match &mut self.trunk_op { + Some(TrunkOp::Delete { trunk: id }) + | Some(TrunkOp::Move { trunk: id, .. }) + | Some(TrunkOp::Undelete { trunk: id }) + | Some(TrunkOp::SetMode { trunk: id, .. }) + | Some(TrunkOp::SetKind { trunk: id, .. }) => trunk(id, &mut remap)?, + Some(TrunkOp::Create { .. }) | None => {} + } + for line in &mut self.line_ops { + branch(&mut line.branch_id, &mut remap)?; + match &mut line.operation { + BranchOp::Insert { after, content } => { + if let Some(id) = after { + branch(id, &mut remap)?; + } + leaves(content, &mut remap)?; + } + BranchOp::Delete { + branch: id, + content, + } => { + branch(id, &mut remap)?; + leaves(content, &mut remap)?; + } + BranchOp::Modify { + branch: id, + old_content, + new_content, + } => { + branch(id, &mut remap)?; + leaves(old_content, &mut remap)?; + leaves(new_content, &mut remap)?; + } + BranchOp::Restore { branch: id } => branch(id, &mut remap)?, + BranchOp::Reparent { + branch: id, + new_after, + } => { + branch(id, &mut remap)?; + if let Some(id) = new_after { + branch(id, &mut remap)?; + } + } + } + } + Ok(()) + } + /// Adds a line operation. pub fn add_line_op(&mut self, op: LineOps) { self.line_ops.push(op); diff --git a/atomic-core/src/change/span_patch.rs b/atomic-core/src/change/span_patch.rs new file mode 100644 index 00000000..6fa677f7 --- /dev/null +++ b/atomic-core/src/change/span_patch.rs @@ -0,0 +1,526 @@ +//! Immutable, content-addressed span operations in a change. +//! +//! A span refers to authored bytes by change hash and byte range. An edit +//! addresses a subrange of that span; it never rewrites the original bytes. + +use crate::types::Hash; +use serde::{Deserialize, Serialize}; + +const MAGIC: &[u8; 4] = b"ATSP"; +const VERSION: u8 = 2; + +/// Maps an authored repository-local semantic ID to its portable change hash. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct SemanticOwner { + /// Non-root NodeId used in this change's semantic operations. + pub authored_id: u64, + /// Content identity of the change that created that semantic ID. + pub owner: Hash, +} + +/// Portable identity of a file created by a change. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ExternalFile { + /// Hash of the creating change. + pub owner: Hash, + /// Ordinal of the file within that change. + pub ordinal: u32, +} + +/// Immutable authored byte range within a change's contents. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ExternalSpan { + /// Hash of the change that owns the bytes. + pub owner: Hash, + /// Inclusive byte offset in the owner's contents. + pub start: u64, + /// Exclusive byte offset in the owner's contents. + pub end: u64, +} + +/// A file root or an edit relative to an existing authored span. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum SpanOp { + /// Introduce the initial authored range for a file. + Root { + /// Initial path; namespace ownership is represented separately. + path: String, + /// Ordinal used to form the portable file identity. + ordinal: u32, + /// Inclusive offset in this change's contents. + start: u64, + /// Exclusive offset in this change's contents. + end: u64, + }, + /// Replace a range of a previously authored span. + Edit { + /// Identity of the affected file. + file: ExternalFile, + /// Immutable span addressed by this edit. + target: ExternalSpan, + /// Inclusive offset relative to the start of the target span. + start: u64, + /// Exclusive offset relative to the start of the target span. + end: u64, + /// Inclusive offset of inserted bytes in this change's contents. + insert_start: u64, + /// Exclusive offset of inserted bytes in this change's contents. + insert_end: u64, + }, +} + +/// Versioned span operations. The prior metadata field preserves the existing +/// prototype's wire format; ordinary change metadata remains separate. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct SpanPatch { + /// Encoding version. + pub version: u8, + /// Metadata carried by older span payloads. + pub prior_metadata: Vec, + /// Operations in this change. + pub ops: Vec, + /// Canonical mapping for non-placeholder semantic operation IDs. + pub semantic_owners: Vec, +} + +// Preserve version-one payload bytes and hashes exactly. +#[derive(Serialize, Deserialize)] +struct SpanPatchV1 { + version: u8, + prior_metadata: Vec, + ops: Vec, +} + +/// Invalid span encoding or local byte range. +#[derive(Debug, thiserror::Error)] +pub enum SpanPatchError { + /// The payload is not marked as a span patch. + #[error("not span patch metadata")] + WrongMagic, + /// The payload version is unsupported. + #[error("unsupported span patch version {0}")] + UnsupportedVersion(u8), + /// Postcard data is malformed or has trailing bytes. + #[error("invalid span patch encoding")] + InvalidEncoding, + /// An operation has an invalid local range. + #[error("invalid span operation {index}: {reason}")] + InvalidOperation { + /// Index of the invalid operation. + index: usize, + /// Explanation of the invalid range. + reason: &'static str, + }, +} + +impl SpanPatch { + /// Create a span patch with portable semantic-owner support. + pub fn new(prior_metadata: Vec, ops: Vec) -> Self { + Self { + version: VERSION, + prior_metadata, + ops, + semantic_owners: Vec::new(), + } + } + + /// Encode the canonical span payload. + pub fn encode(&self) -> Result, SpanPatchError> { + if self.version != 1 && self.version != VERSION { + return Err(SpanPatchError::UnsupportedVersion(self.version)); + } + let mut bytes = MAGIC.to_vec(); + if self.version == 1 { + if !self.semantic_owners.is_empty() { + return Err(SpanPatchError::InvalidEncoding); + } + let legacy = SpanPatchV1 { + version: 1, + prior_metadata: self.prior_metadata.clone(), + ops: self.ops.clone(), + }; + bytes.extend( + postcard::to_allocvec(&legacy).map_err(|_| SpanPatchError::InvalidEncoding)?, + ); + } else { + bytes.extend(postcard::to_allocvec(self).map_err(|_| SpanPatchError::InvalidEncoding)?); + } + Ok(bytes) + } + + /// Decode one complete supported span payload. + pub fn decode(bytes: &[u8]) -> Result { + let payload = bytes + .strip_prefix(MAGIC) + .ok_or(SpanPatchError::WrongMagic)?; + let version = *payload.first().ok_or(SpanPatchError::InvalidEncoding)?; + if version != 1 && version != VERSION { + return Err(SpanPatchError::UnsupportedVersion(version)); + } + let (patch, remaining) = if version == 1 { + let (legacy, remaining): (SpanPatchV1, _) = + postcard::take_from_bytes(payload).map_err(|_| SpanPatchError::InvalidEncoding)?; + ( + Self { + version: legacy.version, + prior_metadata: legacy.prior_metadata, + ops: legacy.ops, + semantic_owners: Vec::new(), + }, + remaining, + ) + } else { + postcard::take_from_bytes(payload).map_err(|_| SpanPatchError::InvalidEncoding)? + }; + if !remaining.is_empty() { + return Err(SpanPatchError::InvalidEncoding); + } + if patch.version != 1 && patch.version != VERSION { + return Err(SpanPatchError::UnsupportedVersion(patch.version)); + } + Ok(patch) + } + + /// Check local byte bounds. Dependency closure, file ownership, and + /// conflicts must additionally be checked against repository state. + pub fn validate_content_ranges(&self, content_len: u64) -> Result<(), SpanPatchError> { + if self.version != 1 && self.version != VERSION { + return Err(SpanPatchError::UnsupportedVersion(self.version)); + } + if self.version == 1 && !self.semantic_owners.is_empty() { + return Err(SpanPatchError::InvalidEncoding); + } + if self + .semantic_owners + .iter() + .any(|entry| entry.authored_id == 0 || entry.owner == Hash::NONE) + || self + .semantic_owners + .windows(2) + .any(|pair| pair[0].authored_id >= pair[1].authored_id) + { + return Err(SpanPatchError::InvalidEncoding); + } + let mut owner_hashes = std::collections::HashSet::new(); + if self + .semantic_owners + .iter() + .any(|entry| !owner_hashes.insert(entry.owner)) + { + return Err(SpanPatchError::InvalidEncoding); + } + for (index, op) in self.ops.iter().enumerate() { + let reason = match op { + SpanOp::Root { start, end, .. } if start > end || *end > content_len => { + Some("root range is reversed or outside the contents") + } + SpanOp::Edit { file, target, .. } + if file.owner == Hash::NONE || target.owner == Hash::NONE => + { + Some("file and target owners must be real changes") + } + SpanOp::Edit { target, .. } if target.start > target.end => { + Some("target span is reversed") + } + SpanOp::Edit { + target, start, end, .. + } if start > end || *end > target.end.saturating_sub(target.start) => { + Some("edit range is reversed or outside the target span") + } + SpanOp::Edit { + insert_start, + insert_end, + .. + } if insert_start > insert_end || *insert_end > content_len => { + Some("inserted range is reversed or outside the contents") + } + _ => None, + }; + if let Some(reason) = reason { + return Err(SpanPatchError::InvalidOperation { index, reason }); + } + } + Ok(()) + } +} + +/// Resolve a span change's authored semantic IDs under a receiving transaction. +/// The hashed change is never modified. Legacy V1 operations with external +/// IDs cannot be localized because their owner hashes were not recorded. +pub fn localize_file_ops( + txn: &T, + change: &crate::change::Change, +) -> anyhow::Result> { + use crate::pristine::{EffectiveProjectionClosure, ViewMembershipSet}; + use crate::types::NodeId; + use anyhow::{ensure, Context}; + let Some(patch) = &change.hashed.span else { + return Ok(change.file_ops().to_vec()); + }; + patch.validate_content_ranges(change.contents.len().try_into()?)?; + let ids: std::collections::BTreeSet<_> = change + .file_ops() + .iter() + .flat_map(|ops| ops.referenced_node_ids()) + .collect(); + let owners: std::collections::BTreeSet<_> = patch + .semantic_owners + .iter() + .map(|entry| NodeId::new(entry.authored_id)) + .collect(); + ensure!( + ids == owners && patch.semantic_owners.len() == owners.len(), + "span semantic identities lack a complete portable owner mapping" + ); + let deps = change + .dependencies() + .iter() + .map(|hash| { + txn.get_internal(hash)? + .context("semantic dependency missing") + }) + .collect::>>()?; + let closure = EffectiveProjectionClosure::try_from_membership( + txn, + &ViewMembershipSet::from_ordered(deps), + )?; + let mut mapping = std::collections::HashMap::new(); + for entry in &patch.semantic_owners { + let id = txn + .get_internal(&entry.owner)? + .context("semantic owner missing")?; + ensure!( + closure.contains(id) && txn.get_external(id)? == Some(entry.owner), + "span semantic owner is outside its dependency closure" + ); + mapping.insert(NodeId::new(entry.authored_id), id); + } + let mut ops = change.file_ops().to_vec(); + for file in &mut ops { + file.remap_referenced_node_ids(|id| { + mapping + .get(&id) + .copied() + .context("span semantic owner mapping is incomplete") + })?; + } + Ok(ops) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::change::format_v3::{ChangeReader, FORMAT_VERSION, SPAN_FORMAT_VERSION}; + use crate::change::{Change, ChangeHeader}; + use std::io::Cursor; + + #[test] + fn legacy_span_payload_roundtrips_without_changing_its_bytes() { + let legacy = SpanPatchV1 { + version: 1, + prior_metadata: b"legacy".to_vec(), + ops: vec![SpanOp::Root { + path: "file".into(), + ordinal: 0, + start: 0, + end: 4, + }], + }; + let mut bytes = MAGIC.to_vec(); + bytes.extend(postcard::to_allocvec(&legacy).unwrap()); + let decoded = SpanPatch::decode(&bytes).unwrap(); + assert_eq!(decoded.version, 1); + assert!(decoded.semantic_owners.is_empty()); + assert_eq!(decoded.encode().unwrap(), bytes); + let mut change = Change::new( + ChangeHeader::new("legacy span"), + vec![], + b"abc\n".to_vec(), + vec![], + ); + change.hashed.span = Some(decoded); + let mut object = Vec::new(); + let original = change.serialize(&mut object).unwrap(); + let (decoded, hash) = Change::deserialize(&mut Cursor::new(&object)).unwrap(); + assert_eq!(original, hash); + let mut reserialized = Vec::new(); + assert_eq!(decoded.serialize(&mut reserialized).unwrap(), original); + } + + #[test] + fn typed_span_is_distinct_from_opaque_metadata() { + let patch = SpanPatch::new( + Vec::new(), + vec![SpanOp::Root { + path: "file".into(), + ordinal: 0, + start: 0, + end: 4, + }], + ); + let mut ordinary = Change::new( + ChangeHeader::new("ordinary"), + vec![], + b"abc\n".to_vec(), + vec![], + ); + ordinary.hashed.metadata = patch.encode().unwrap(); + let mut ordinary_bytes = Vec::new(); + let ordinary_hash = ordinary.serialize(&mut ordinary_bytes).unwrap(); + assert_eq!( + u32::from_le_bytes(ordinary_bytes[4..8].try_into().unwrap()), + FORMAT_VERSION + ); + let (decoded, hash) = Change::deserialize(&mut Cursor::new(&ordinary_bytes)).unwrap(); + assert_eq!(hash, ordinary_hash); + assert_eq!(decoded.hashed.metadata, ordinary.hashed.metadata); + assert!(decoded.hashed.span.is_none()); + + let mut span = ordinary.clone(); + span.hashed.span = Some(patch.clone()); + let mut span_bytes = Vec::new(); + let span_hash = span.serialize(&mut span_bytes).unwrap(); + assert_ne!(span_hash, ordinary_hash); + assert_eq!( + u32::from_le_bytes(span_bytes[4..8].try_into().unwrap()), + SPAN_FORMAT_VERSION + ); + let (decoded, hash) = Change::deserialize(&mut Cursor::new(&span_bytes)).unwrap(); + assert_eq!(hash, span_hash); + assert_eq!(decoded.hashed.metadata, ordinary.hashed.metadata); + assert_eq!(decoded.hashed.span, Some(patch)); + assert!(!decoded.hashed.is_empty()); + let mut cursor = Cursor::new(&span_bytes); + assert!(ChangeReader::open(&mut cursor) + .unwrap() + .graph_sections() + .is_err()); + // The file header is unhashed. Downgrading it must never reinterpret a + // span object as an ordinary change with the old identity. + span_bytes[4..8].copy_from_slice(&FORMAT_VERSION.to_le_bytes()); + assert!(Change::deserialize(&mut Cursor::new(&span_bytes)).is_err()); + } + + #[test] + fn span_and_semantic_operations_share_one_change() { + use crate::change::FileOps; + use crate::crdt::TrunkId; + use crate::types::NodeId; + let mut change = Change::with_file_ops( + ChangeHeader::new("span and semantic"), + vec![], + vec![FileOps::edit(TrunkId::new(NodeId::ROOT, 0), "file".into())], + b"abc\n".to_vec(), + vec![], + ); + change.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "file".into(), + ordinal: 0, + start: 0, + end: 4, + }], + )); + let mut bytes = Vec::new(); + let hash = change.serialize(&mut bytes).unwrap(); + let (decoded, actual) = Change::deserialize(&mut Cursor::new(bytes)).unwrap(); + assert_eq!(actual, hash); + assert_eq!(decoded.hashed.span, change.hashed.span); + assert_eq!(decoded.file_ops(), change.file_ops()); + } + + #[test] + fn schema_three_requires_one_canonical_span_section() { + use crate::change::format_v3::{ChangeWriter, FileHeader, HashDedupTable, WriterOptions}; + let mut header = FileHeader::builder().hash_table_entries(1).build(); + header.version = SPAN_FORMAT_VERSION; + let hashes = HashDedupTable::new([0; 32]); + let mut missing = Vec::new(); + let mut writer = ChangeWriter::new(&mut missing, WriterOptions::default()); + writer.write_file_header(&header).unwrap(); + writer.write_hash_table(&hashes).unwrap(); + writer + .write_change_header(&ChangeHeader::new("missing")) + .unwrap(); + writer.write_dependencies(&[]).unwrap(); + assert!(writer.finalize().is_err()); + + let mut malformed = SpanPatch::new(Vec::new(), Vec::new()).encode().unwrap(); + malformed.push(0); + let mut bytes = Vec::new(); + let mut writer = ChangeWriter::new(&mut bytes, WriterOptions::default()); + writer.write_file_header(&header).unwrap(); + writer.write_hash_table(&hashes).unwrap(); + writer + .write_change_header(&ChangeHeader::new("malformed")) + .unwrap(); + writer.write_dependencies(&[]).unwrap(); + writer.write_span_section(&malformed).unwrap(); + assert!(writer.write_span_section(&malformed).is_err()); + writer.finalize().unwrap(); + assert!(Change::deserialize(&mut Cursor::new(bytes)).is_err()); + } + + #[test] + fn ranges_and_owners_are_checked() { + let owner = Hash::of(b"owner"); + let edit = SpanOp::Edit { + file: ExternalFile { owner, ordinal: 0 }, + target: ExternalSpan { + owner, + start: 10, + end: 20, + }, + start: 1, + end: 9, + insert_start: 0, + insert_end: 2, + }; + assert!(SpanPatch::new(vec![], vec![edit.clone()]) + .validate_content_ranges(2) + .is_ok()); + let mut bad = edit.clone(); + if let SpanOp::Edit { end, .. } = &mut bad { + *end = 11; + } + assert!(SpanPatch::new(vec![], vec![bad]) + .validate_content_ranges(2) + .is_err()); + let mut bad = edit; + if let SpanOp::Edit { file, .. } = &mut bad { + file.owner = Hash::NONE; + } + assert!(SpanPatch::new(vec![], vec![bad]) + .validate_content_ranges(2) + .is_err()); + } + + #[test] + fn opaque_schema_two_matches_existing_wire_bytes() { + use crate::types::Base32; + let hex: String = include_str!("format_v3/fixtures/span_opaque_v2_reference.hex") + .chars() + .filter(|c| !c.is_ascii_whitespace()) + .collect(); + let expected: Vec = (0..hex.len()) + .step_by(2) + .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap()) + .collect(); + let mut header = ChangeHeader::new("opaque v2 fixture"); + header.timestamp = chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap(); + let mut change = Change::new(header, vec![], b"old bytes\n".to_vec(), vec![]); + change.hashed.metadata = b"ATSP\x01\x00\x00".to_vec(); + let mut actual = Vec::new(); + let hash = change.serialize(&mut actual).unwrap(); + assert_eq!( + hash.to_base32(), + "Z6ICEFZQ3TKMR4YIRONQAAA4GF6CKGQPMZJWVK6YU5O5SAX456HA" + ); + assert_eq!(actual, expected); + let (decoded, verified) = Change::deserialize(&mut expected.as_slice()).unwrap(); + assert_eq!(verified, hash); + assert!(decoded.hashed.span.is_none()); + assert_eq!(decoded.hashed.metadata, change.hashed.metadata); + } +} diff --git a/atomic-core/src/pristine/capability.rs b/atomic-core/src/pristine/capability.rs index e103fe33..a175cf48 100644 --- a/atomic-core/src/pristine/capability.rs +++ b/atomic-core/src/pristine/capability.rs @@ -20,11 +20,18 @@ pub const SHIM_CUTOVER_CAPABILITY: RepositoryCapability = pub const KG_SEMWEB_CAPABILITY: RepositoryCapability = RepositoryCapability::new("kg-semweb-v1", 1); +/// Repository contains immutable, typed span changes. Older readers must not +/// interpret their namespace-only graph hunks as complete file content. +/// Version 3 additionally requires portable semantic-owner mappings. +pub const SPAN_RANGE_CAPABILITY: RepositoryCapability = + RepositoryCapability::new("span-range-graph", 3); + /// Capabilities supported by this Atomic build. pub const SUPPORTED_REPOSITORY_CAPABILITIES: &[RepositoryCapability] = &[ CHANGE_FORMAT_VNEXT_CAPABILITY, SHIM_CUTOVER_CAPABILITY, KG_SEMWEB_CAPABILITY, + SPAN_RANGE_CAPABILITY, ]; /// A repository capability and the minimum version required to use it. @@ -114,7 +121,13 @@ pub(crate) fn unsupported_requirements( .iter() .find(|supported| supported.id == required.id) .map(|supported| supported.minimum_version); - if supported_version.is_some_and(|version| version >= required.minimum_version) { + // Version 1 used an ambiguous metadata envelope, not the typed + // section. It needs explicit migration, not reinterpretation. + let legacy_span = + required.id == SPAN_RANGE_CAPABILITY.id() && required.minimum_version == 1; + if !legacy_span + && supported_version.is_some_and(|version| version >= required.minimum_version) + { None } else { Some(UnsupportedRepositoryCapability { diff --git a/atomic-core/src/pristine/mod.rs b/atomic-core/src/pristine/mod.rs index 476d7fa9..65813849 100644 --- a/atomic-core/src/pristine/mod.rs +++ b/atomic-core/src/pristine/mod.rs @@ -163,6 +163,7 @@ pub mod schema; mod set_id_index; pub mod slice; pub(crate) mod span_index; +pub mod span_patch_storage; pub mod tables; mod traits; mod txn; @@ -178,7 +179,7 @@ pub use bindings::{BindingMutTxnT, BindingStoreOutcome, BindingTxnT}; pub use capability::{ RepositoryCapability, RequiredRepositoryCapability, UnsupportedRepositoryCapability, CHANGE_FORMAT_VNEXT_CAPABILITY, REQUIRED_CAPABILITY_PREFIX, SHIM_CUTOVER_CAPABILITY, - SUPPORTED_REPOSITORY_CAPABILITIES, + SPAN_RANGE_CAPABILITY, SUPPORTED_REPOSITORY_CAPABILITIES, }; pub use error::{PristineError, PristineResult}; mod file_index_v2; diff --git a/atomic-core/src/pristine/span_patch_storage.rs b/atomic-core/src/pristine/span_patch_storage.rs new file mode 100644 index 00000000..826eac70 --- /dev/null +++ b/atomic-core/src/pristine/span_patch_storage.rs @@ -0,0 +1,1170 @@ +//! Transactional index for immutable authored span ranges. +//! +//! Content stays in change objects. These tables contain file identities, +//! authored range identities, and edits against those ranges. Callers must +//! abort the surrounding transaction on error. + +use super::{ + CachedGraphTxn, EffectiveProjectionClosure, GraphTxnT, ReadTxn, ViewMembershipSet, ViewTxnT, + WriteTxn, +}; +use crate::change::span_patch::{ExternalFile, ExternalSpan, SpanOp, SpanPatch}; +use crate::change::{Change, ChangeStore}; +use crate::types::{Hash, Inode, NodeId}; +use anyhow::{bail, ensure, Context, Result}; +use rayon::prelude::*; +use redb::{MultimapTableDefinition, ReadableMultimapTable, ReadableTable, TableDefinition}; +use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::sync::Arc; + +const FILES: TableDefinition<&[u8; 36], u64> = TableDefinition::new("span_patch_files_v1"); +const ROOTS: TableDefinition = TableDefinition::new("span_patch_roots_v1"); +const SPANS: TableDefinition<&[u8; 32], u8> = TableDefinition::new("span_patch_nodes_v1"); +const EDITS: MultimapTableDefinition<&[u8; 32], &[u8; 40]> = + MultimapTableDefinition::new("span_patch_edits_v1"); +const APPLIED: TableDefinition = TableDefinition::new("span_patch_applied_v1"); + +fn file_key(file: &ExternalFile) -> [u8; 36] { + let mut key = [0; 36]; + key[..32].copy_from_slice(file.owner.as_bytes()); + key[32..].copy_from_slice(&file.ordinal.to_be_bytes()); + key +} + +fn words(values: [u64; N]) -> Vec { + values.into_iter().flat_map(u64::to_be_bytes).collect() +} + +fn decode_words(bytes: &[u8]) -> [u64; N] { + std::array::from_fn(|i| u64::from_be_bytes(bytes[i * 8..i * 8 + 8].try_into().unwrap())) +} + +fn span_key(inode: Inode, owner: NodeId, start: u64, end: u64) -> [u8; 32] { + words([inode.get(), owner.get(), start, end]) + .try_into() + .expect("fixed span key size") +} + +/// Index a registered change under the same transaction as its namespace and +/// view publication. Root inode bindings must come from the namespace writer. +/// The caller must abort the transaction if this function returns an error. +pub fn index_change( + txn: &mut WriteTxn<'_>, + store: &S, + view_name: &str, + hash: Hash, + change: &Change, + root_inodes: &BTreeMap, +) -> Result<()> { + ensure!(change.hash()? == hash, "change/hash mismatch"); + let patch = change + .hashed + .span + .as_ref() + .context("typed span section missing")?; + patch.validate_content_ranges(change.contents.len().try_into()?)?; + let id = txn + .get_internal(&hash)? + .context("change is not registered")?; + ensure!( + txn.get_external(id)? == Some(hash), + "change identity mismatch" + ); + let view = txn.get_view(view_name)?.context("view not found")?; + let visible = + EffectiveProjectionClosure::try_from_membership(txn, &txn.view_membership_set(&view)?)?; + let mut dep_ids = Vec::new(); + for dep in change.dependencies() { + let dep_id = txn.get_internal(dep)?.context("missing dependency")?; + ensure!(visible.contains(dep_id), "dependency outside target view"); + dep_ids.push(dep_id); + } + let dependencies = EffectiveProjectionClosure::try_from_membership( + txn, + &ViewMembershipSet::from_ordered(dep_ids), + )?; + let already = txn.txn.open_table(APPLIED)?.get(id.get())?.is_some(); + if already { + return Ok(()); + } + + let mut ordinals = BTreeSet::new(); + let mut inodes = BTreeSet::new(); + for op in &patch.ops { + match op { + SpanOp::Root { + ordinal, + start, + end, + .. + } => { + let inode = *root_inodes.get(ordinal).context("missing root inode")?; + ensure!(ordinals.insert(*ordinal), "duplicate root ordinal"); + ensure!(inodes.insert(inode.get()), "multiple roots for inode"); + ensure!(*start <= *end, "reversed root"); + ensure!( + txn.txn.open_table(ROOTS)?.get(inode.get())?.is_none(), + "root already exists" + ); + let file = ExternalFile { + owner: hash, + ordinal: *ordinal, + }; + ensure!( + txn.txn.open_table(FILES)?.get(&file_key(&file))?.is_none(), + "file identity already exists" + ); + } + SpanOp::Edit { file, target, .. } => { + let file_owner = txn + .get_internal(&file.owner)? + .context("file owner missing")?; + let target_owner = txn + .get_internal(&target.owner)? + .context("target owner missing")?; + ensure!( + dependencies.contains(file_owner) && dependencies.contains(target_owner), + "span owner not in change dependency closure" + ); + let inode = txn + .span_file_inode(file)? + .context("file identity missing")?; + ensure!( + txn.txn + .open_table(SPANS)? + .get(&span_key(inode, target_owner, target.start, target.end))? + .is_some(), + "target span missing" + ); + let source = store.get_change(&target.owner)?; + ensure!( + source.hash()? == target.owner, + "target change hash mismatch" + ); + let source_patch = source + .hashed + .span + .as_ref() + .context("target typed span section missing")?; + let mut matching = 0; + for source_op in &source_patch.ops { + match source_op { + SpanOp::Root { + ordinal, + start, + end, + .. + } if *start == target.start && *end == target.end => { + let source_file = ExternalFile { + owner: target.owner, + ordinal: *ordinal, + }; + if txn.span_file_inode(&source_file)? == Some(inode) { + matching += 1; + } + } + SpanOp::Edit { + file: source_file, + insert_start, + insert_end, + .. + } if *insert_start == target.start + && *insert_end == target.end + && insert_start < insert_end + && txn.span_file_inode(source_file)? == Some(inode) => + { + matching += 1; + } + _ => {} + } + } + ensure!( + matching == 1, + "target span index differs from hashed owner change" + ); + let lo = usize::try_from(target.start).context("target start too large")?; + let hi = usize::try_from(target.end).context("target end too large")?; + source + .contents + .get(lo..hi) + .context("target out of bounds")?; + } + } + } + ensure!( + ordinals.len() == root_inodes.len(), + "unexpected root inode mapping" + ); + + for op in &patch.ops { + match op { + SpanOp::Root { + ordinal, + start, + end, + .. + } => { + let inode = root_inodes[ordinal]; + let root: [u8; 24] = words([id.get(), *start, *end]).try_into().unwrap(); + txn.txn.open_table(ROOTS)?.insert(inode.get(), &root)?; + txn.txn.open_table(FILES)?.insert( + &file_key(&ExternalFile { + owner: hash, + ordinal: *ordinal, + }), + inode.get(), + )?; + txn.txn + .open_table(SPANS)? + .insert(&span_key(inode, id, *start, *end), 1)?; + } + SpanOp::Edit { + file, + target, + start, + end, + insert_start, + insert_end, + } => { + let inode = txn + .span_file_inode(file)? + .context("file identity disappeared")?; + let owner = txn + .get_internal(&target.owner)? + .context("span owner disappeared")?; + let value: [u8; 40] = words([*start, *end, id.get(), *insert_start, *insert_end]) + .try_into() + .unwrap(); + txn.txn + .open_multimap_table(EDITS)? + .insert(&span_key(inode, owner, target.start, target.end), &value)?; + if insert_start < insert_end { + txn.txn + .open_table(SPANS)? + .insert(&span_key(inode, id, *insert_start, *insert_end), 1)?; + } + } + } + } + txn.txn.open_table(APPLIED)?.insert(id.get(), 1)?; + Ok(()) +} + +/// Span index reads bound to the transaction used for visibility. +pub trait SpanPatchReadTxn: GraphTxnT { + /// Whether the change's span operations were indexed transactionally. + fn span_change_indexed(&self, change: NodeId) -> Result; + /// The file's initial authored span, if indexed. + fn span_root(&self, inode: Inode) -> Result>; + /// Resolve a portable file identity. + fn span_file_inode(&self, file: &ExternalFile) -> Result>; + /// Test whether an authored span is indexed. + fn span_exists(&self, key: &[u8; 32]) -> Result; + /// Read all edit rows belonging to an inode. + fn span_edits_for_inode(&self, inode: Inode) -> Result>; + /// Read edits anchored to one authored span. + fn span_edits_at(&self, key: &[u8; 32]) -> Result>; +} + +macro_rules! impl_span_read { + ($txn:ty) => { + impl SpanPatchReadTxn for $txn { + fn span_change_indexed(&self, change: NodeId) -> Result { + let table = match self.txn.open_table(APPLIED) { + Ok(table) => table, + Err(redb::TableError::TableDoesNotExist(_)) => return Ok(false), + Err(error) => return Err(error.into()), + }; + let result = match table.get(change.get())? { + Some(value) if value.value() == 1 => Ok(true), + Some(_) => bail!("invalid span applied marker"), + None => Ok(false), + }; + result + } + fn span_root(&self, inode: Inode) -> Result> { + let table = match self.txn.open_table(ROOTS) { + Ok(table) => table, + Err(redb::TableError::TableDoesNotExist(_)) => return Ok(None), + Err(error) => return Err(error.into()), + }; + let result = table.get(inode.get())?.map(|v| decode_words(v.value())); + Ok(result) + } + fn span_file_inode(&self, file: &ExternalFile) -> Result> { + let table = match self.txn.open_table(FILES) { + Ok(table) => table, + Err(redb::TableError::TableDoesNotExist(_)) => return Ok(None), + Err(error) => return Err(error.into()), + }; + let result = table.get(&file_key(file))?.map(|v| Inode::new(v.value())); + Ok(result) + } + fn span_exists(&self, key: &[u8; 32]) -> Result { + let table = match self.txn.open_table(SPANS) { + Ok(table) => table, + Err(redb::TableError::TableDoesNotExist(_)) => return Ok(false), + Err(error) => return Err(error.into()), + }; + let result = table.get(key)?.is_some(); + Ok(result) + } + fn span_edits_for_inode(&self, inode: Inode) -> Result> { + let table = match self.txn.open_multimap_table(EDITS) { + Ok(table) => table, + Err(redb::TableError::TableDoesNotExist(_)) => return Ok(Vec::new()), + Err(error) => return Err(error.into()), + }; + let lo = span_key(inode, NodeId::new(0), 0, 0); + let hi = span_key(inode, NodeId::new(u64::MAX), u64::MAX, u64::MAX); + let mut rows = Vec::new(); + for row in table.range::<&[u8; 32]>(&lo..=&hi)? { + let (key, values) = row?; + for value in values { + rows.push((*key.value(), *value?.value())); + } + } + Ok(rows) + } + fn span_edits_at(&self, key: &[u8; 32]) -> Result> { + let table = match self.txn.open_multimap_table(EDITS) { + Ok(table) => table, + Err(redb::TableError::TableDoesNotExist(_)) => return Ok(Vec::new()), + Err(error) => return Err(error.into()), + }; + let mut rows = Vec::new(); + for value in table.get(key)? { + rows.push(*value?.value()); + } + Ok(rows) + } + } + }; +} +impl_span_read!(ReadTxn); +impl_span_read!(WriteTxn<'_>); + +impl SpanPatchReadTxn for CachedGraphTxn<'_> { + fn span_change_indexed(&self, change: NodeId) -> Result { + self.txn.span_change_indexed(change) + } + fn span_root(&self, inode: Inode) -> Result> { + self.txn.span_root(inode) + } + fn span_file_inode(&self, file: &ExternalFile) -> Result> { + self.txn.span_file_inode(file) + } + fn span_exists(&self, key: &[u8; 32]) -> Result { + self.txn.span_exists(key) + } + fn span_edits_for_inode(&self, inode: Inode) -> Result> { + self.txn.span_edits_for_inode(inode) + } + fn span_edits_at(&self, key: &[u8; 32]) -> Result> { + self.txn.span_edits_at(key) + } +} + +impl super::CapabilityTxnT for CachedGraphTxn<'_> { + fn required_capability_version(&self, id: &str) -> super::PristineResult> { + super::CapabilityTxnT::required_capability_version(self.txn, id) + } +} + +struct RenderChange { + contents: Vec, + dependencies: Vec, + span: Option, +} + +type ExpectedEdits = HashMap>; +const MAX_PROOF_BYTES: usize = 4 * 1024 * 1024; + +// Transaction-local exact reachability; never use local NodeId ordering as +// causality. When the bounded proof does not fit, use checked graph walks. +struct CausalProof { + ranks: Box<[(NodeId, usize)]>, + bits: Box<[u64]>, + words: usize, +} + +impl CausalProof { + fn build( + txn: &T, + visible: &EffectiveProjectionClosure, + budget: usize, + ) -> Result> { + if budget == 0 { + return Ok(None); + } + let count = visible.len(); + let words = count.div_ceil(64); + let Some(cells) = count.checked_mul(words) else { + return Ok(None); + }; + let Some(bytes) = cells.checked_mul(8).and_then(|size| { + count + .checked_mul(std::mem::size_of::<(NodeId, usize)>()) + .and_then(|ranks| size.checked_add(ranks)) + }) else { + return Ok(None); + }; + if bytes > budget.min(MAX_PROOF_BYTES) { + return Ok(None); + } + let order: Vec<_> = visible.iter_dependency_first().copied().collect(); + let mut ranks: Vec<_> = order + .iter() + .enumerate() + .map(|(rank, &id)| (id, rank)) + .collect(); + ranks.sort_unstable_by_key(|(id, _)| id.get()); + ensure!( + ranks.windows(2).all(|pair| pair[0].0 != pair[1].0), + "duplicate visible change in span causal proof" + ); + let mut bits = Vec::new(); + if bits.try_reserve_exact(cells).is_err() { + return Ok(None); + } + bits.resize(cells, 0u64); + for (rank, &id) in order.iter().enumerate() { + let (earlier, rest) = bits.split_at_mut(rank * words); + let row = &mut rest[..words]; + row[rank / 64] |= 1u64 << (rank % 64); + for dep in txn.get_change_deps(id)? { + let dep_id = txn + .get_internal(&dep)? + .context("span causal proof dependency missing")?; + let index = ranks + .binary_search_by_key(&dep_id.get(), |(id, _)| id.get()) + .map_err(|_| anyhow::anyhow!("span causal proof closure is incomplete"))?; + let dep_rank = ranks[index].1; + ensure!(dep_rank < rank, "span causal proof is not dependency-first"); + for (word, previous) in row + .iter_mut() + .zip(&earlier[dep_rank * words..(dep_rank + 1) * words]) + { + *word |= *previous; + } + } + } + Ok(Some(Self { + ranks: ranks.into_boxed_slice(), + bits: bits.into_boxed_slice(), + words, + })) + } + + fn contains(&self, descendant: NodeId, ancestor: NodeId) -> Option { + let d = self + .ranks + .binary_search_by_key(&descendant.get(), |(id, _)| id.get()) + .ok()?; + let a = self + .ranks + .binary_search_by_key(&ancestor.get(), |(id, _)| id.get()) + .ok()?; + if descendant == ancestor { + return Some(false); + } + let (d, a) = (self.ranks[d].1, self.ranks[a].1); + Some(self.bits[d * self.words + a / 64] & (1u64 << (a % 64)) != 0) + } +} + +/// Validated span content shared by file renders in one read transaction. +pub struct SpanRenderContext<'a, T: SpanPatchReadTxn> { + txn: &'a T, + visible: &'a EffectiveProjectionClosure, + changes: HashMap>, + expected: ExpectedEdits, + proof: Option, +} + +/// One visible portion of an authored span, in rendered file order. +#[derive(Debug, Clone)] +pub struct VisibleSpan { + /// Byte range in the rendered file. + pub output: std::ops::Range, + /// Immutable authored span containing the bytes. + pub target: ExternalSpan, + /// Offset of the visible portion within the target span. + pub target_start: u64, +} + +/// A verified recording baseline and its visible authored ranges. +pub struct SpanRecordState { + /// Portable file identity. + pub file: ExternalFile, + /// Original authored span, including for an empty file. + pub root: ExternalSpan, + /// Bytes visible to the recording view. + pub content: Vec, + /// Authored ranges corresponding to the visible bytes. + pub spans: Vec, + /// Visible changes that edited this file and must remain causal parents. + pub dependencies: Vec, +} + +impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { + /// Verify the closure and content-addressed changes once for this snapshot. + pub fn new( + txn: &'a T, + store: &S, + visible: &'a EffectiveProjectionClosure, + ) -> Result { + Self::with_dependency_proof_budget(txn, store, visible, MAX_PROOF_BYTES) + } + + /// Set the optional causal-proof budget. Zero uses checked graph walks. + pub fn with_dependency_proof_budget( + txn: &'a T, + store: &S, + visible: &'a EffectiveProjectionClosure, + budget: usize, + ) -> Result { + let mut changes = HashMap::new(); + let mut expected = ExpectedEdits::new(); + let ids: Vec<_> = visible.iter_dependency_first().copied().collect(); + // Hashing and reads may run in parallel, but report errors in the + // dependency order and limit speculative work after a bad object. + for batch in ids.chunks(64) { + let hashes: Vec<_> = batch + .iter() + .map(|id| { + txn.get_external(*id) + .map_err(anyhow::Error::from) + .and_then(|hash| hash.context("visible hash missing")) + }) + .collect(); + let loaded: Vec)>> = hashes + .into_par_iter() + .map(|hash| { + let hash = hash?; + let mut change = store.get_change(&hash)?; + ensure!(change.hash()? == hash, "change hash mismatch"); + Ok(( + hash, + Arc::new(RenderChange { + contents: std::mem::take(&mut change.contents), + dependencies: std::mem::take(&mut change.hashed.dependencies), + span: change.hashed.span.take(), + }), + )) + }) + .collect(); + for (id, loaded) in batch.iter().zip(loaded) { + let (hash, change) = loaded?; + let mut indexed = txn.get_change_deps(*id)?; + let mut declared = change.dependencies.clone(); + indexed.sort(); + declared.sort(); + ensure!( + indexed == declared, + "dependency index differs from hashed change" + ); + changes.insert(hash, Arc::clone(&change)); + if let Some(patch) = &change.span { + for op in &patch.ops { + if let SpanOp::Edit { + file, + target, + start, + end, + insert_start, + insert_end, + } = op + { + let inode = txn + .span_file_inode(file)? + .context("span file mapping missing")?; + let owner = txn + .get_internal(&target.owner)? + .context("span target missing")?; + ensure!(visible.contains(owner), "span target outside view"); + let key = span_key(inode, owner, target.start, target.end); + let row: [u8; 40] = + words([*start, *end, id.get(), *insert_start, *insert_end]) + .try_into() + .unwrap(); + ensure!( + expected.entry(inode.get()).or_default().insert((key, row)), + "duplicate hashed span edit" + ); + } + } + } + } + } + let proof = CausalProof::build(txn, visible, budget)?; + Ok(Self { + txn, + visible, + changes, + expected, + proof, + }) + } + + /// Render an indexed root when its creating change is visible. + pub fn render_if_visible(&self, inode: Inode) -> Result>> { + let Some(root) = self.txn.span_root(inode)? else { + return Ok(None); + }; + if !self.visible.contains(NodeId::new(root[0])) { + return Ok(None); + } + self.render(inode).map(Some) + } + + /// Obtain byte-to-span mappings under this exact view closure. + pub fn record_state(&self, inode: Inode) -> Result> { + let Some(root) = self.txn.span_root(inode)? else { + return Ok(None); + }; + if !self.visible.contains(NodeId::new(root[0])) { + return Ok(None); + } + let mut spans = Vec::new(); + let content = self.render_internal(inode, Some(&mut spans))?; + let owner = self + .txn + .get_external(NodeId::new(root[0]))? + .context("root hash missing")?; + let change = self + .changes + .get(&owner) + .context("root change outside closure")?; + let patch = change + .span + .as_ref() + .context("root typed span section missing")?; + let mut found = None; + for op in &patch.ops { + if let SpanOp::Root { + ordinal, + start, + end, + .. + } = op + { + let file = ExternalFile { + owner, + ordinal: *ordinal, + }; + if *start == root[1] + && *end == root[2] + && self.txn.span_file_inode(&file)? == Some(inode) + { + ensure!(found.is_none(), "ambiguous span root"); + found = Some(file); + } + } + } + let file = found.context("root index differs from hashed change")?; + let mut dependencies = BTreeSet::new(); + dependencies.insert(owner); + if let Some(edits) = self.expected.get(&inode.get()) { + for (_, row) in edits { + let author = NodeId::new(decode_words::<5>(row)[2]); + dependencies.insert( + self.txn + .get_external(author)? + .context("span edit author missing")?, + ); + } + } + Ok(Some(SpanRecordState { + file, + root: ExternalSpan { + owner, + start: root[1], + end: root[2], + }, + content, + spans, + dependencies: dependencies.into_iter().collect(), + })) + } + + /// Render the visible span tree for an inode. + pub fn render(&self, inode: Inode) -> Result> { + self.render_internal(inode, None) + } + + fn render_internal( + &self, + inode: Inode, + segments: Option<&mut Vec>, + ) -> Result> { + let root = self.txn.span_root(inode)?.context("span root missing")?; + let root_id = NodeId::new(root[0]); + ensure!(self.visible.contains(root_id), "span root outside view"); + let root_hash = self + .txn + .get_external(root_id)? + .context("root hash missing")?; + let root_change = self + .changes + .get(&root_hash) + .context("root change outside closure")?; + let patch = root_change + .span + .as_ref() + .context("root typed span section missing")?; + let mut roots = 0; + for op in &patch.ops { + if let SpanOp::Root { + ordinal, + start, + end, + .. + } = op + { + if *start == root[1] + && *end == root[2] + && self.txn.span_file_inode(&ExternalFile { + owner: root_hash, + ordinal: *ordinal, + })? == Some(inode) + { + roots += 1; + } + } + } + ensure!(roots == 1, "root index differs from hashed change"); + let observed: BTreeSet<_> = self + .txn + .span_edits_for_inode(inode)? + .into_iter() + .filter(|(_, row)| { + self.visible + .contains(NodeId::new(decode_words::<5>(row)[2])) + }) + .collect(); + ensure!( + observed == self.expected.get(&inode.get()).cloned().unwrap_or_default(), + "visible span index differs from hashed changes" + ); + let mut out = Vec::new(); + self.render_spans(inode, root, &mut out, segments)?; + Ok(out) + } + + fn depends_on(&self, author: u64, predecessor: u64) -> Result { + if let Some(answer) = self + .proof + .as_ref() + .and_then(|proof| proof.contains(NodeId::new(author), NodeId::new(predecessor))) + { + return Ok(answer); + } + let ids = self + .txn + .get_change_deps(NodeId::new(author))? + .into_iter() + .map(|dep| self.txn.get_internal(&dep)?.context("dependency missing")) + .collect::>>()?; + Ok(EffectiveProjectionClosure::try_from_membership( + self.txn, + &ViewMembershipSet::from_ordered(ids), + )? + .contains(NodeId::new(predecessor))) + } + + fn frame(&self, inode: Inode, span: [u64; 3]) -> Result { + let [owner, start, end] = span; + let owner = NodeId::new(owner); + ensure!(self.visible.contains(owner), "span outside view"); + let key = span_key(inode, owner, start, end); + ensure!(self.txn.span_exists(&key)?, "span index row missing"); + let hash = self.txn.get_external(owner)?.context("span hash missing")?; + let change = Arc::clone( + self.changes + .get(&hash) + .context("span change outside closure")?, + ); + let (start, end) = (usize::try_from(start)?, usize::try_from(end)?); + change + .contents + .get(start..end) + .context("span outside change content")?; + let mut edits: Vec<[u64; 5]> = self + .txn + .span_edits_at(&key)? + .into_iter() + .map(|row| decode_words::<5>(&row)) + .filter(|row| self.visible.contains(NodeId::new(row[2]))) + .collect(); + edits.sort_by_key(|row| (row[0], row[1], row[2])); + let mut group = 0; + while group < edits.len() { + let mut end_group = group + 1; + while end_group < edits.len() + && edits[group][0] == edits[group][1] + && edits[end_group][0] == edits[group][0] + && edits[end_group][1] == edits[group][1] + { + end_group += 1; + } + for i in group..end_group { + for j in i + 1..end_group { + let left = edits[i][2]; + let right = edits[j][2]; + ensure!( + left != right, + "ambiguous insertions from one change at the same gap" + ); + if self.depends_on(right, left)? { + edits.swap(i, j); + } else if !self.depends_on(left, right)? { + bail!("unsupported concurrent insertions at the same gap"); + } + } + } + group = end_group; + } + Ok(RenderFrame { + span, + hash, + change, + start, + end, + edits, + next: 0, + cursor: 0, + previous: None, + covered: None, + }) + } + + fn render_spans( + &self, + inode: Inode, + root: [u64; 3], + out: &mut Vec, + mut segments: Option<&mut Vec>, + ) -> Result<()> { + let mut active = BTreeSet::new(); + active.insert(root); + let mut stack = vec![self.frame(inode, root)?]; + while let Some(frame) = stack.last_mut() { + if frame.next == frame.edits.len() { + append_visible( + frame, + frame.cursor, + frame.end - frame.start, + out, + segments.as_deref_mut(), + ); + active.remove(&frame.span); + stack.pop(); + continue; + } + let [lo, hi, author, insert_start, insert_end] = frame.edits[frame.next]; + frame.next += 1; + let (lo, hi) = (usize::try_from(lo)?, usize::try_from(hi)?); + ensure!( + lo <= hi && hi <= frame.end - frame.start, + "invalid stored range" + ); + let interior_gap = if lo < frame.cursor && lo == hi { + match frame.covered { + Some((cover_start, cover_end, cover_author)) + if cover_start < lo && lo < cover_end && cover_author != author => + { + self.depends_on(cover_author, author)? + } + _ => false, + } + } else { + false + }; + ensure!( + lo >= frame.cursor || interior_gap, + "unsupported overlapping or ambiguous edits" + ); + if let Some((p_lo, p_hi, p_author)) = frame.previous { + let touches_gap = lo == p_hi && (lo == hi || p_lo == p_hi); + let distinct_ranges = (p_lo == p_hi) != (lo == hi); + let causal_touch = if touches_gap && distinct_ranges && author != p_author { + self.depends_on(author, p_author)? || self.depends_on(p_author, author)? + } else if touches_gap && lo == hi && p_lo == p_hi && author != p_author { + self.depends_on(p_author, author)? + } else { + false + }; + if (lo < p_hi && !interior_gap) || (touches_gap && !causal_touch) { + bail!("unsupported overlapping or ambiguous edits"); + } + } + if !interior_gap { + append_visible(frame, frame.cursor, lo, out, segments.as_deref_mut()); + frame.cursor = hi; + } + if lo < hi { + frame.covered = Some((lo, hi, author)); + } + frame.previous = Some((lo, hi, author)); + if insert_start < insert_end { + let child = [author, insert_start, insert_end]; + ensure!(active.insert(child), "cyclic span references"); + stack.push(self.frame(inode, child)?); + } + } + Ok(()) + } +} + +struct RenderFrame { + span: [u64; 3], + hash: Hash, + change: Arc, + start: usize, + end: usize, + edits: Vec<[u64; 5]>, + next: usize, + cursor: usize, + previous: Option<(usize, usize, u64)>, + covered: Option<(usize, usize, u64)>, +} + +fn append_visible( + frame: &RenderFrame, + start: usize, + end: usize, + out: &mut Vec, + segments: Option<&mut Vec>, +) { + if start == end { + return; + } + let output_start = out.len(); + out.extend_from_slice(&frame.change.contents[frame.start + start..frame.start + end]); + if let Some(segments) = segments { + segments.push(VisibleSpan { + output: output_start..out.len(), + target: ExternalSpan { + owner: frame.hash, + start: frame.span[1], + end: frame.span[2], + }, + target_start: start as u64, + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::change::span_patch::ExternalSpan; + use crate::change::{ChangeHeader, MemoryChangeStore}; + use crate::pristine::{MutTxnT, Pristine, ViewScope}; + + fn add_change( + repo: &Pristine, + store: &MemoryChangeStore, + view_name: &str, + change: Change, + roots: BTreeMap, + ) -> Result { + let hash = store.insert_change(change.clone())?; + let mut txn = repo.write_txn_immediate()?; + let id = txn.register_change(&hash)?; + txn.put_change_deps(id, change.dependencies())?; + index_change(&mut txn, store, view_name, hash, &change, &roots)?; + let mut view = txn.get_view(view_name)?.context("view missing")?; + txn.put_change(&mut view, id, &hash)?; + txn.update_view(&view)?; + txn.commit()?; + Ok(hash) + } + + fn render( + repo: &Pristine, + store: &MemoryChangeStore, + view_name: &str, + inode: Inode, + ) -> Result> { + let txn = repo.read_txn()?; + let view = txn.get_view(view_name)?.context("view missing")?; + let visible = EffectiveProjectionClosure::try_from_membership( + &txn, + &txn.view_membership_set(&view)?, + )?; + SpanRenderContext::new(&txn, store, &visible)?.render(inode) + } + + #[test] + fn independent_span_edits_render_in_both_insert_orders() -> Result<()> { + let dir = tempfile::tempdir()?; + let path = dir.path().join("pristine.redb"); + let repo = Pristine::open(&path)?; + let store = MemoryChangeStore::new(); + let inode = Inode::new(42); + { + let mut txn = repo.write_txn()?; + let base = txn.open_or_create_view("dev")?; + for name in ["left", "right", "both", "reverse"] { + txn.create_view(name, ViewScope::Draft, Some(base.id))?; + } + txn.commit()?; + } + let initial = b"alpha\nbeta\ngamma\nomega"; + let mut root = Change::new(ChangeHeader::new("root"), vec![], initial.to_vec(), vec![]); + root.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "file.txt".into(), + ordinal: 0, + start: 0, + end: initial.len() as u64, + }], + )); + let root_hash = add_change(&repo, &store, "dev", root, BTreeMap::from([(0, inode)]))?; + let file = ExternalFile { + owner: root_hash, + ordinal: 0, + }; + let span = ExternalSpan { + owner: root_hash, + start: 0, + end: initial.len() as u64, + }; + let edit = |name: &str, bytes: &[u8], start, end| { + let mut change = Change::new( + ChangeHeader::new(name), + vec![], + bytes.to_vec(), + vec![root_hash], + ); + change.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Edit { + file: file.clone(), + target: span.clone(), + start, + end, + insert_start: 0, + insert_end: bytes.len() as u64, + }], + )); + change + }; + let left = edit("left", b"ALPHA", 0, 5); + let right = edit("right", b"GAMMA", 11, 16); + let left_hash = add_change(&repo, &store, "left", left.clone(), BTreeMap::new())?; + let right_hash = add_change(&repo, &store, "right", right.clone(), BTreeMap::new())?; + assert_eq!( + render(&repo, &store, "left", inode)?, + b"ALPHA\nbeta\ngamma\nomega" + ); + assert_eq!( + render(&repo, &store, "right", inode)?, + b"alpha\nbeta\nGAMMA\nomega" + ); + let expected = b"ALPHA\nbeta\nGAMMA\nomega"; + for (view, order) in [("both", [&left, &right]), ("reverse", [&right, &left])] { + for change in order { + add_change(&repo, &store, view, change.clone(), BTreeMap::new())?; + } + assert_eq!(render(&repo, &store, view, inode)?, expected); + let txn = repo.read_txn()?; + let state = txn.get_view(view)?.unwrap(); + let visible = EffectiveProjectionClosure::try_from_membership( + &txn, + &txn.view_membership_set(&state)?, + )?; + assert_eq!( + SpanRenderContext::with_dependency_proof_budget(&txn, &store, &visible, 0)? + .render(inode)?, + expected + ); + } + let txn = repo.read_txn()?; + assert_eq!( + txn.span_edits_for_inode(inode)?.len(), + 2, + "view insertion must not duplicate index rows" + ); + assert_ne!(left_hash, right_hash); + drop(txn); + drop(repo); + let reopened = Pristine::open(path)?; + assert_eq!(render(&reopened, &store, "both", inode)?, expected); + Ok(()) + } + + #[test] + fn overlapping_span_edits_are_not_silently_rendered() -> Result<()> { + let dir = tempfile::tempdir()?; + let repo = Pristine::open(dir.path().join("pristine.redb"))?; + let store = MemoryChangeStore::new(); + let inode = Inode::new(42); + { + let mut txn = repo.write_txn()?; + let base = txn.open_or_create_view("dev")?; + for name in ["left", "right", "both"] { + txn.create_view(name, ViewScope::Draft, Some(base.id))?; + } + txn.commit()?; + } + let mut root = Change::new( + ChangeHeader::new("root"), + vec![], + b"alpha\n".to_vec(), + vec![], + ); + root.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "a".into(), + ordinal: 0, + start: 0, + end: 6, + }], + )); + let root_hash = add_change(&repo, &store, "dev", root, BTreeMap::from([(0, inode)]))?; + let make_edit = |name: &str, content: &[u8]| { + let mut change = Change::new( + ChangeHeader::new(name), + vec![], + content.to_vec(), + vec![root_hash], + ); + change.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Edit { + file: ExternalFile { + owner: root_hash, + ordinal: 0, + }, + target: ExternalSpan { + owner: root_hash, + start: 0, + end: 6, + }, + start: 0, + end: 5, + insert_start: 0, + insert_end: content.len() as u64, + }], + )); + change + }; + let left = make_edit("left", b"left"); + let right = make_edit("right", b"right"); + add_change(&repo, &store, "left", left.clone(), BTreeMap::new())?; + add_change(&repo, &store, "right", right.clone(), BTreeMap::new())?; + add_change(&repo, &store, "both", left, BTreeMap::new())?; + add_change(&repo, &store, "both", right, BTreeMap::new())?; + assert_eq!(render(&repo, &store, "left", inode)?, b"left\n"); + assert_eq!(render(&repo, &store, "right", inode)?, b"right\n"); + assert!(render(&repo, &store, "both", inode) + .unwrap_err() + .to_string() + .contains("overlapping")); + Ok(()) + } +} diff --git a/atomic-core/src/pristine/txn/read.rs b/atomic-core/src/pristine/txn/read.rs index c8c4cec0..8426a7e3 100644 --- a/atomic-core/src/pristine/txn/read.rs +++ b/atomic-core/src/pristine/txn/read.rs @@ -1902,7 +1902,7 @@ impl crate::pristine::traits::CrdtTxnT for ReadTxn { /// reuses the handle for all subsequent operations, eliminating the /// per-call overhead entirely. pub struct CachedGraphTxn<'txn> { - txn: &'txn ReadTxn, + pub(crate) txn: &'txn ReadTxn, graph_table: redb::ReadOnlyMultimapTable<&'static [u8; 24], &'static [u8; 24]>, inode_graph_table: redb::ReadOnlyMultimapTable<&'static [u8; 32], &'static [u8; 24]>, /// Lazily-loaded per-change span index, so `find_block` / `find_block_end` diff --git a/atomic-core/src/pristine/txn/write/mod.rs b/atomic-core/src/pristine/txn/write/mod.rs index b4a2d465..2ec4db4b 100644 --- a/atomic-core/src/pristine/txn/write/mod.rs +++ b/atomic-core/src/pristine/txn/write/mod.rs @@ -102,6 +102,8 @@ pub struct WriteTxn<'a> { pub(crate) next_view_id: &'a AtomicU64, pub(crate) next_inode: &'a AtomicU64, pub(crate) pending_next_inode: Option, + /// Local-only allocation counters for an explicitly abort-only preview. + preview_allocations: Option<(u64, u64, u64)>, } impl<'a> WriteTxn<'a> { @@ -119,6 +121,33 @@ impl<'a> WriteTxn<'a> { next_view_id, next_inode, pending_next_inode: None, + preview_allocations: None, + } + } + + /// Use private IDs for a speculative transaction that must be aborted. + /// This prevents rejected previews from consuming shared allocation IDs. + pub fn enable_abort_only_allocations(&mut self) -> PristineResult<()> { + if self.pending_next_inode.is_some() || self.preview_allocations.is_some() { + return Err(PristineError::Inconsistent { + message: "transaction already has local allocation state".into(), + }); + } + self.preview_allocations = Some(( + self.next_node_id.load(Ordering::SeqCst), + self.next_inode.load(Ordering::SeqCst), + self.next_view_id.load(Ordering::SeqCst), + )); + Ok(()) + } + + fn allocate_view_id(&mut self) -> PristineResult { + if let Some((_, _, next)) = &mut self.preview_allocations { + let id = *next; + *next = next.checked_add(1).ok_or(PristineError::IdSpaceExhausted)?; + Ok(id) + } else { + Ok(self.next_view_id.fetch_add(1, Ordering::SeqCst)) } } @@ -1115,7 +1144,13 @@ impl<'a> WriteTxn<'a> { } // Allocate a new ID - let id = self.next_node_id.fetch_add(1, Ordering::SeqCst); + let id = if let Some((next, _, _)) = &mut self.preview_allocations { + let id = *next; + *next = next.checked_add(1).ok_or(PristineError::IdSpaceExhausted)?; + id + } else { + self.next_node_id.fetch_add(1, Ordering::SeqCst) + }; let node_id = NodeId::new(id); // Insert into both tables @@ -1226,7 +1261,7 @@ impl<'a> MutTxnT for WriteTxn<'a> { } // Create new view (defaults to Shared, no parent for backward compat) - let id = self.next_view_id.fetch_add(1, Ordering::SeqCst); + let id = self.allocate_view_id()?; let state = ViewState::new(id, name.to_string()); // Save it @@ -1294,7 +1329,7 @@ impl<'a> MutTxnT for WriteTxn<'a> { } // Allocate ID and create state - let id = self.next_view_id.fetch_add(1, Ordering::SeqCst); + let id = self.allocate_view_id()?; let state = ViewState::with_scope(id, name.to_string(), kind, parent); // Save it @@ -2093,6 +2128,13 @@ impl<'a> MutTxnT for WriteTxn<'a> { } fn alloc_inode(&mut self) -> PristineResult { + if let Some((_, next, _)) = &mut self.preview_allocations { + let inode = (*next).max(1); + *next = inode + .checked_add(1) + .ok_or(PristineError::IdSpaceExhausted)?; + return Ok(Inode::new(inode)); + } match self.pending_next_inode.as_mut() { Some(pending) => pending.allocate(), None => allocate_shared_inode(self.next_inode), @@ -2214,6 +2256,11 @@ impl<'a> MutTxnT for WriteTxn<'a> { } fn commit(self) -> PristineResult<()> { + if self.preview_allocations.is_some() { + return Err(PristineError::Inconsistent { + message: "abort-only preview transaction cannot commit".into(), + }); + } let pending_next_inode = self.pending_next_inode; let shared_next_inode = self.next_inode; self.txn.commit()?; diff --git a/atomic-core/src/pristine/txn/write/tests.rs b/atomic-core/src/pristine/txn/write/tests.rs index ecb6a4e9..fd343139 100644 --- a/atomic-core/src/pristine/txn/write/tests.rs +++ b/atomic-core/src/pristine/txn/write/tests.rs @@ -45,6 +45,40 @@ mod tests { txn.commit().unwrap(); } + #[test] + fn abort_only_preview_does_not_consume_ids_or_commit() { + let dir = tempdir().unwrap(); + let pristine = Pristine::open(dir.path().join("pristine")).unwrap(); + let hash = Hash::of(b"preview"); + let (node, inode, view) = { + let mut txn = pristine.write_txn().unwrap(); + txn.enable_abort_only_allocations().unwrap(); + let node = txn.register_change(&hash).unwrap(); + let inode = txn.alloc_inode().unwrap(); + let view = txn.open_or_create_view("preview").unwrap().id; + txn.abort().unwrap(); + (node, inode, view) + }; + { + let mut txn = pristine.write_txn().unwrap(); + assert_eq!(txn.register_change(&hash).unwrap(), node); + assert_eq!(txn.alloc_inode().unwrap(), inode); + assert_eq!(txn.open_or_create_view("preview").unwrap().id, view); + txn.abort().unwrap(); + } + { + let mut txn = pristine.write_txn().unwrap(); + txn.enable_abort_only_allocations().unwrap(); + txn.register_change(&Hash::of(b"must not commit")).unwrap(); + assert!(txn.commit().is_err()); + } + let txn = pristine.read_txn().unwrap(); + assert_eq!( + txn.get_internal(&Hash::of(b"must not commit")).unwrap(), + None + ); + } + #[test] fn test_view_operations() { let dir = tempdir().unwrap(); diff --git a/atomic-core/src/record/workflow/assembly/mod.rs b/atomic-core/src/record/workflow/assembly/mod.rs index 2b191aaa..15731d0b 100644 --- a/atomic-core/src/record/workflow/assembly/mod.rs +++ b/atomic-core/src/record/workflow/assembly/mod.rs @@ -115,6 +115,9 @@ pub struct AssemblyContext { /// Accumulated hunks (graph operations). hunks: Vec>>, + /// Immutable range edits, with offsets in the assembled content blob. + span_ops: Vec, + /// Accumulated file operations (semantic layer). file_ops: Vec, @@ -153,6 +156,7 @@ impl AssemblyContext { Self { header, hunks: Vec::new(), + span_ops: Vec::new(), file_ops: Vec::new(), dependencies: HashSet::new(), extra_known: HashSet::new(), @@ -172,6 +176,7 @@ impl AssemblyContext { Self { header, hunks: Vec::with_capacity(hunk_capacity), + span_ops: Vec::new(), file_ops: Vec::new(), dependencies: HashSet::new(), extra_known: HashSet::new(), @@ -336,6 +341,12 @@ impl AssemblyContext { dependencies, ); change.hashed.extra_known = extra_known; + if !self.span_ops.is_empty() { + change.hashed.span = Some(crate::change::span_patch::SpanPatch::new( + Vec::new(), + self.span_ops, + )); + } // Set opaque metadata bytes (e.g., SessionEnvelope from atomic-agent) if !metadata_bytes.is_empty() { @@ -656,6 +667,9 @@ where file.kind(), ); let glob_start = Instant::now(); + // Causal observations apply to both pre-globalized span edits and + // ordinary lifecycle hunks such as deletion of a span-backed file. + ctx.add_dependencies(file.span_dependencies().iter().copied()); // Fast path: if the file has pre-globalized ops (vertex map was // available during content retrieval), skip globalization entirely. @@ -671,11 +685,64 @@ where shift_graph_op_content(&mut op, shift); ctx.add_hunk(op); } + for op in file.span_ops() { + let mut op = op.clone(); + match &mut op { + crate::change::span_patch::SpanOp::Edit { + file: span_file, + target, + insert_start, + insert_end, + .. + } => { + ctx.add_dependency(span_file.owner); + ctx.add_dependency(target.owner); + *insert_start = insert_start.checked_add(shift).ok_or_else(|| { + AssemblyError::Globalize { + path: file.path().to_string(), + source: super::globalize::GlobalizeError::MissingField { + field: "span content offset overflow", + path: file.path().to_string(), + }, + } + })?; + *insert_end = insert_end.checked_add(shift).ok_or_else(|| { + AssemblyError::Globalize { + path: file.path().to_string(), + source: super::globalize::GlobalizeError::MissingField { + field: "span content offset overflow", + path: file.path().to_string(), + }, + } + })?; + } + crate::change::span_patch::SpanOp::Root { .. } => { + return Err(AssemblyError::Globalize { + path: file.path().to_string(), + source: super::globalize::GlobalizeError::MissingField { + field: "span root must use a file addition", + path: file.path().to_string(), + }, + }) + } + } + ctx.span_ops.push(op); + } // Opaque files retain their trunk lifecycle but deliberately omit // line/token semantics. Attribute operations still need that stable // trunk identity. if let Some(crdt_ops) = file.crdt_ops() { let mut crdt_ops = crdt_ops.clone(); + if !file.span_ops().is_empty() { + for line in crdt_ops.line_ops_mut() { + if let Some((start, end)) = line.content_range() { + line.set_content_range( + ChangePosition::new(start.get() + shift), + ChangePosition::new(end.get() + shift), + ); + } + } + } if file.opaque_generated() { crdt_ops.line_ops_mut().clear(); } @@ -788,7 +855,7 @@ where ); // Check if we have any hunks - if ctx.hunk_count() == 0 && !options.get_include_empty_files() { + if ctx.hunk_count() == 0 && ctx.span_ops.is_empty() && !options.get_include_empty_files() { return Err(types::AssemblyError::AllEmpty); } diff --git a/atomic-core/src/record/workflow/record/types.rs b/atomic-core/src/record/workflow/record/types.rs index a5e0fdf9..a9cbcbba 100644 --- a/atomic-core/src/record/workflow/record/types.rs +++ b/atomic-core/src/record/workflow/record/types.rs @@ -199,6 +199,11 @@ pub struct RecordedFile { /// has a vertex map from content retrieval. pre_globalized: Option>>>, + /// Span edits whose inserted ranges are relative to this file's content. + span_ops: Vec, + /// Visible file changes observed while recording these span edits. + span_dependencies: Vec, + /// Causally maximal deletions this record restores. /// /// Empty means this is an ordinary add/edit/delete. Undelete keeps the @@ -250,6 +255,8 @@ impl RecordedFile { name_conflict_resolution: None, name_conflict_bindings: Vec::new(), pre_globalized: None, + span_ops: Vec::new(), + span_dependencies: Vec::new(), undelete_changes: Vec::new(), attrs: Vec::new(), } @@ -559,6 +566,26 @@ impl RecordedFile { self.pre_globalized.as_deref() } + /// Set the range edits produced for a span-backed file. + pub fn set_span_ops(&mut self, ops: Vec) { + self.span_ops = ops; + } + + /// Range edits with insert offsets relative to this file's content. + pub fn span_ops(&self) -> &[crate::change::span_patch::SpanOp] { + &self.span_ops + } + + /// Preserve the causal file history observed during span recording. + pub fn set_span_dependencies(&mut self, dependencies: Vec) { + self.span_dependencies = dependencies; + } + + /// The visible file changes captured by span recording. + pub fn span_dependencies(&self) -> &[Hash] { + &self.span_dependencies + } + /// Take ownership of pre-globalized graph operations. #[must_use] pub fn take_pre_globalized( @@ -589,6 +616,7 @@ impl RecordedFile { || self.is_undelete() || !self.attrs.is_empty() || self.name_conflict_resolution.is_some() + || !self.span_ops.is_empty() { return false; } @@ -598,7 +626,7 @@ impl RecordedFile { /// Get number of hunks. #[must_use] pub fn hunk_count(&self) -> usize { - self.hunks.len() + self.hunks.len() + self.span_ops.len() } /// Get content length. diff --git a/atomic-repository/src/apply/mod.rs b/atomic-repository/src/apply/mod.rs index 63d6d547..76918e3c 100644 --- a/atomic-repository/src/apply/mod.rs +++ b/atomic-repository/src/apply/mod.rs @@ -235,6 +235,56 @@ pub fn write_change_to_graph( change: &Change, options: &InsertOptions, already_in_graph: bool, +) -> InsertResult { + reject_unsupported_span_change(change)?; + write_change_to_graph_inner( + txn, + view_name, + change_id, + change_hash, + change, + options, + already_in_graph, + ) +} + +// The caller must either index the span operations in this transaction or +// have proved the existing ambient index before a reference-only insertion. +// The ordinary public writer must never discard span operations. +pub(crate) fn write_span_hunks_with_index( + txn: &mut WriteTxn<'_>, + view_name: &str, + change_id: NodeId, + change_hash: &Hash, + change: &Change, + options: &InsertOptions, + already_in_graph: bool, +) -> InsertResult { + if change.hashed.span.is_none() { + return Err(InsertError::InvalidChange { + message: "span change required".into(), + }); + } + write_change_to_graph_inner( + txn, + view_name, + change_id, + change_hash, + change, + options, + already_in_graph, + ) +} + +#[allow(clippy::too_many_arguments)] +fn write_change_to_graph_inner( + txn: &mut WriteTxn<'_>, + view_name: &str, + change_id: NodeId, + change_hash: &Hash, + change: &Change, + options: &InsertOptions, + already_in_graph: bool, ) -> InsertResult { let mut workspace = Workspace::new(); let mut conflict_tracker = ConflictTracker::new(); @@ -334,6 +384,7 @@ pub fn apply_change_hunks_without_membership( verified_frontier: &atomic_core::change::VerifiedCausalFrontier, options: &InsertOptions, ) -> InsertResult { + reject_unsupported_span_change(change)?; let mut workspace = Workspace::new(); let mut conflict_tracker = ConflictTracker::new(); let mut stats = InsertStats::new(); @@ -351,6 +402,17 @@ pub fn apply_change_hunks_without_membership( Ok(stats) } +// Until the span index writer is integrated, publishing only the ordinary +// graph hunks would silently discard the span operations. +pub(crate) fn reject_unsupported_span_change(change: &Change) -> InsertResult<()> { + if change.hashed.span.is_some() { + return Err(InsertError::InvalidChange { + message: "span changes require a span-aware repository writer".to_string(), + }); + } + Ok(()) +} + /// The shared hunk/FileOps mutation stage of [`write_change_to_graph`] and /// [`apply_change_hunks_without_membership`]. #[allow(clippy::too_many_arguments)] @@ -418,7 +480,17 @@ fn apply_change_hunks_locked( // Apply FileOps to CRDT tables (semantic layer) // This enables human-readable diffs and token-level blame if change.has_file_ops() { - let file_ops = change.file_ops(); + let mut localized = None; + if change.hashed.span.is_some() { + localized = Some( + atomic_core::change::span_patch::localize_file_ops(txn, change).map_err( + |error| InsertError::InvalidChange { + message: error.to_string(), + }, + )?, + ); + } + let file_ops = localized.as_deref().unwrap_or_else(|| change.file_ops()); let file_ops_count = file_ops.len(); log::debug!( "write_change_to_graph: applying {} FileOps to CRDT tables", diff --git a/atomic-repository/src/redb_change_store/mod.rs b/atomic-repository/src/redb_change_store/mod.rs index 24518510..55362577 100644 --- a/atomic-repository/src/redb_change_store/mod.rs +++ b/atomic-repository/src/redb_change_store/mod.rs @@ -92,6 +92,10 @@ pub enum RedbStoreError { #[error("Corrupt change data: {0}")] Corrupt(String), + /// Selective reads must not silently discard span operations. + #[error("selective section reads cannot interpret span changes")] + UnsupportedSpanSelectiveRead, + #[error("Unsupported provenance store schema version {0}")] UnsupportedProvenanceSchema(u64), @@ -226,8 +230,11 @@ pub struct StoredChangeMeta { #[derive(serde::Serialize, serde::Deserialize)] enum StoredChangeMetaExtension { V1 { header_v2: Vec }, + V2 { header_v2: Vec, span: Vec }, } +type StoredMetaWithSections = (StoredChangeMeta, Option>, Option>); + // ═══════════════════════════════════════════════════════════════════════ // RedbChangeStore — the main store // ═══════════════════════════════════════════════════════════════════════ @@ -376,11 +383,16 @@ impl RedbChangeStore { let mut reader = ChangeReader::open(&mut cursor)?; let file_header = *reader.file_header(); + if file_header.version == format_v3::SPAN_FORMAT_VERSION { + Change::deserialize(&mut Cursor::new(data)) + .map_err(|error| RedbStoreError::Corrupt(error.to_string()))?; + } let hash_table = reader.hash_table().clone(); // Read all sections let mut meta_header: Option = None; let mut header_v2: Option> = None; + let mut span_payload: Option> = None; let mut meta_deps: Vec = Vec::new(); let mut provenance_payload: Option> = None; let mut graph_sections: Vec<(u32, Vec)> = Vec::new(); @@ -399,7 +411,7 @@ impl RedbChangeStore { postcard::from_bytes(§ion.payload) .map_err(|e| RedbStoreError::Serialization(e.to_string()))?, ); - if file_header.version == format_v3::FORMAT_VERSION { + if file_header.version != format_v3::LEGACY_FORMAT_VERSION { header_v2 = Some(section.payload.clone()); } } @@ -414,6 +426,14 @@ impl RedbChangeStore { graph_sections.push((graph_idx, section.payload.clone())); graph_idx += 1; } + SectionType::Span => { + if file_header.version != format_v3::SPAN_FORMAT_VERSION + || span_payload.is_some() + { + return Err(RedbStoreError::Corrupt("unexpected span section".into())); + } + span_payload = Some(section.payload.clone()); + } SectionType::Semantic => { semantic_sections.push((semantic_idx, section.payload.clone())); semantic_idx += 1; @@ -459,7 +479,10 @@ impl RedbChangeStore { let mut meta_bytes = postcard::to_allocvec(&meta) .map_err(|e| RedbStoreError::Serialization(e.to_string()))?; if let Some(header_v2) = header_v2 { - let extension = StoredChangeMetaExtension::V1 { header_v2 }; + let extension = match span_payload { + Some(span) => StoredChangeMetaExtension::V2 { header_v2, span }, + None => StoredChangeMetaExtension::V1 { header_v2 }, + }; meta_bytes.extend( postcard::to_allocvec(&extension) .map_err(|e| RedbStoreError::Serialization(e.to_string()))?, @@ -575,14 +598,14 @@ impl RedbChangeStore { hash: &[u8; 32], ) -> RedbStoreResult { self.load_meta_with_header_in_txn(txn, hash) - .map(|(meta, _)| meta) + .map(|(meta, _, _)| meta) } fn load_meta_with_header_in_txn( &self, txn: &redb::ReadTransaction, hash: &[u8; 32], - ) -> RedbStoreResult<(StoredChangeMeta, Option>)> { + ) -> RedbStoreResult { let table = txn.open_table(tables::CHANGE_META)?; let value = table.get(hash)?.ok_or_else(|| RedbStoreError::NotFound { @@ -597,8 +620,8 @@ impl RedbChangeStore { .map_err(|e| RedbStoreError::Corrupt(format!("meta decompression failed: {}", e)))?; let (meta, suffix) = postcard::take_from_bytes::(&decompressed) .map_err(|e| RedbStoreError::Corrupt(format!("meta deserialization failed: {}", e)))?; - let header_v2 = if suffix.is_empty() { - None + let (header_v2, span) = if suffix.is_empty() { + (None, None) } else { let (extension, remainder) = postcard::take_from_bytes::(suffix).map_err(|e| { @@ -609,10 +632,12 @@ impl RedbChangeStore { "unexpected bytes after meta extension".to_string(), )); } - let StoredChangeMetaExtension::V1 { header_v2 } = extension; - Some(header_v2) + match extension { + StoredChangeMetaExtension::V1 { header_v2 } => (Some(header_v2), None), + StoredChangeMetaExtension::V2 { header_v2, span } => (Some(header_v2), Some(span)), + } }; - Ok((meta, header_v2)) + Ok((meta, header_v2, span)) } // NOTE: load_change(), export_v3_file(), export_v3_bytes(), and diff --git a/atomic-repository/src/redb_change_store/queries.rs b/atomic-repository/src/redb_change_store/queries.rs index b06c1403..94d1e2d9 100644 --- a/atomic-repository/src/redb_change_store/queries.rs +++ b/atomic-repository/src/redb_change_store/queries.rs @@ -91,8 +91,11 @@ impl RedbChangeStore { /// /// A vector of decompressed graph section payloads, ordered by file index. pub fn load_graph_sections(&self, hash: &[u8; 32]) -> RedbStoreResult> { - let meta = self.load_meta(hash)?; let txn = self.db().begin_read()?; + let (meta, _, span) = self.load_meta_with_header_in_txn(&txn, hash)?; + if span.is_some() { + return Err(RedbStoreError::UnsupportedSpanSelectiveRead); + } let table = txn.open_table(tables::CHANGE_GRAPH)?; let mut sections = Vec::with_capacity(meta.graph_section_count as usize); @@ -133,8 +136,11 @@ impl RedbChangeStore { /// /// A vector of decompressed semantic section payloads, ordered by file index. pub fn load_semantic_sections(&self, hash: &[u8; 32]) -> RedbStoreResult> { - let meta = self.load_meta(hash)?; let txn = self.db().begin_read()?; + let (meta, _, span) = self.load_meta_with_header_in_txn(&txn, hash)?; + if span.is_some() { + return Err(RedbStoreError::UnsupportedSpanSelectiveRead); + } let table = txn.open_table(tables::CHANGE_SEMANTIC)?; let mut sections = Vec::with_capacity(meta.semantic_section_count as usize); @@ -171,8 +177,11 @@ impl RedbChangeStore { /// /// A vector of decompressed content chunks, ordered by chunk index. pub fn load_content_chunks(&self, hash: &[u8; 32]) -> RedbStoreResult> { - let meta = self.load_meta(hash)?; let txn = self.db().begin_read()?; + let (meta, _, span) = self.load_meta_with_header_in_txn(&txn, hash)?; + if span.is_some() { + return Err(RedbStoreError::UnsupportedSpanSelectiveRead); + } let manifest_table = txn.open_table(tables::CHANGE_CHUNKS)?; let content_table = txn.open_table(tables::CONTENT_CHUNKS)?; @@ -394,7 +403,8 @@ impl RedbChangeStore { txn: &redb::ReadTransaction, hash: &[u8; 32], ) -> RedbStoreResult> { - let (meta, header_v2) = self.load_meta_with_header_in_txn(txn, hash)?; + let (meta, header_v2, span) = self.load_meta_with_header_in_txn(txn, hash)?; + let span_format = span.is_some(); // Reconstruct the hash dedup table let hash_table = if meta.hash_table.is_empty() { @@ -422,7 +432,10 @@ impl RedbChangeStore { file_header_builder = file_header_builder.with_signature(); } - let file_header = file_header_builder.build(); + let mut file_header = file_header_builder.build(); + if span.is_some() { + file_header.version = atomic_core::change::format_v3::SPAN_FORMAT_VERSION; + } // Write V3 format let mut output = Vec::new(); @@ -481,6 +494,10 @@ impl RedbChangeStore { } } + if let Some(span) = span { + writer.write_span_section(&span)?; + } + // Write SEMANTIC sections { let semantic_table = txn.open_table(tables::CHANGE_SEMANTIC)?; @@ -536,7 +553,12 @@ impl RedbChangeStore { } } - writer.finalize()?; + let outcome = writer.finalize()?; + if span_format && &outcome.content_hash != hash { + return Err(RedbStoreError::Corrupt( + "reconstructed span change hash does not match requested identity".into(), + )); + } Ok(output) } diff --git a/atomic-repository/src/redb_change_store/tests.rs b/atomic-repository/src/redb_change_store/tests.rs index 02f237f8..b3767c1d 100644 --- a/atomic-repository/src/redb_change_store/tests.rs +++ b/atomic-repository/src/redb_change_store/tests.rs @@ -19,6 +19,98 @@ fn make_test_change(message: &str, content: &[u8]) -> Change { Change::new(ChangeHeader::new(message), vec![], content.to_vec(), vec![]) } +#[test] +fn typed_span_survives_redb_reopen_and_export() { + use atomic_core::change::span_patch::{SpanOp, SpanPatch}; + let mut change = make_test_change("typed span", b"one\ntwo\n"); + change.hashed.metadata = b"ATSP arbitrary opaque metadata".to_vec(); + change.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "file.txt".into(), + ordinal: 0, + start: 0, + end: 8, + }], + )); + let mut original = Vec::new(); + let original_hash = change.serialize(&mut original).unwrap(); + let (dir, store) = temp_store(); + let hash = store.import_v3_bytes(&original).unwrap(); + assert_eq!(hash, *original_hash.as_bytes()); + drop(store); + let store = RedbChangeStore::open_existing(dir.path().join("test_change_store.redb")).unwrap(); + assert!(matches!( + store.load_graph_sections(&hash), + Err(RedbStoreError::UnsupportedSpanSelectiveRead) + )); + assert!(matches!( + store.load_semantic_sections(&hash), + Err(RedbStoreError::UnsupportedSpanSelectiveRead) + )); + assert!(matches!( + store.load_content_chunks(&hash), + Err(RedbStoreError::UnsupportedSpanSelectiveRead) + )); + let exported = store.export_v3_bytes(&hash).unwrap(); + let (loaded, exported_hash) = Change::deserialize(&mut Cursor::new(&exported)).unwrap(); + assert_eq!(exported_hash, original_hash); + assert_eq!(loaded.hashed, change.hashed); + assert_eq!(loaded.contents, change.contents); + let (_receiver_dir, receiver) = temp_store(); + assert_eq!(receiver.import_v3_bytes(&exported).unwrap(), hash); + assert_eq!(receiver.load_change(&hash).unwrap().hashed, change.hashed); +} + +#[test] +fn altered_span_cannot_be_exported_under_its_original_hash() { + use atomic_core::change::span_patch::{SpanOp, SpanPatch}; + let (dir, store) = temp_store(); + let mut change = make_test_change("typed span", b"one\n"); + let patch = SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "a".into(), + ordinal: 0, + start: 0, + end: 4, + }], + ); + change.hashed.span = Some(patch.clone()); + let hash = store.save_change(&change).unwrap(); + let (meta, header, _) = { + let txn = store.db().begin_read().unwrap(); + store.load_meta_with_header_in_txn(&txn, &hash).unwrap() + }; + let mut altered = patch; + if let SpanOp::Root { path, .. } = &mut altered.ops[0] { + *path = "b".into(); + } + let mut meta_bytes = postcard::to_allocvec(&meta).unwrap(); + meta_bytes.extend( + postcard::to_allocvec(&StoredChangeMetaExtension::V2 { + header_v2: header.unwrap(), + span: altered.encode().unwrap(), + }) + .unwrap(), + ); + let compressed = zstd::encode_all(meta_bytes.as_slice(), 3).unwrap(); + let txn = store.db().begin_write().unwrap(); + txn.open_table(tables::CHANGE_META) + .unwrap() + .insert(&hash, compressed.as_slice()) + .unwrap(); + txn.commit().unwrap(); + let destination = dir.path().join("existing.change"); + std::fs::write(&destination, b"preserve").unwrap(); + assert!(store + .export_v3_file(&hash, &destination) + .unwrap_err() + .to_string() + .contains("does not match requested identity")); + assert_eq!(std::fs::read(destination).unwrap(), b"preserve"); +} + /// Helper: create a Change with a hunk. fn make_change_with_hunk() -> Change { let test_pos = Position::new(Some(Hash::of(b"test")), ChangePosition::new(0)); diff --git a/atomic-repository/src/repository/changes.rs b/atomic-repository/src/repository/changes.rs index 2814753e..5c06c4e0 100644 --- a/atomic-repository/src/repository/changes.rs +++ b/atomic-repository/src/repository/changes.rs @@ -4,6 +4,7 @@ const ATOM_CHANGE_MAGIC: &[u8; 4] = b"ATOM"; const ATOM_CHANGE_FILE_HEADER_LEN: usize = 64; const ATOM_CHANGE_FILE_VERSION_V1: u32 = 1; const ATOM_CHANGE_FILE_VERSION_V2: u32 = 2; +const ATOM_CHANGE_FILE_VERSION_SPAN: u32 = atomic_core::change::format_v3::SPAN_FORMAT_VERSION; fn atom_change_file_version(bytes: &[u8]) -> Result { if bytes.len() < ATOM_CHANGE_FILE_HEADER_LEN { @@ -21,6 +22,18 @@ fn atom_change_file_version(bytes: &[u8]) -> Result { } impl Repository { + fn require_span_storage_capability(&self) -> Result<(), RepositoryError> { + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + let txn = self.pristine.read_txn()?; + if txn.required_capability_version(SPAN_RANGE_CAPABILITY.id())? + != Some(SPAN_RANGE_CAPABILITY.minimum_version()) + { + return Err(RepositoryError::InvalidOperation { + message: "storing span changes requires the span repository capability".into(), + }); + } + Ok(()) + } // Change Storage Methods /// Get a reference to the change store. @@ -117,6 +130,9 @@ impl Repository { /// println!("Saved change: {}", hash.to_base32()); /// ``` pub fn save_change(&self, change: &Change) -> Result { + if change.hashed.span.is_some() { + self.require_span_storage_capability()?; + } self.pristine .require_repository_capability(CHANGE_FORMAT_VNEXT_CAPABILITY) .map_err(RepositoryError::from)?; @@ -140,6 +156,19 @@ impl Repository { computed.to_base32() ))); } + if change.hashed.span.is_some() { + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + let txn = self.pristine.read_txn()?; + let required = txn.required_capability_version(SPAN_RANGE_CAPABILITY.id())?; + drop(txn); + match required { + None => self.pristine.require_repository_capability(SPAN_RANGE_CAPABILITY)?, + Some(version) if version == SPAN_RANGE_CAPABILITY.minimum_version() => {} + Some(_) => return Err(RepositoryError::InvalidOperation { + message: "existing span capability requires migration before importing new span changes".into(), + }), + } + } let stored = self.save_change_bytes(hash, bytes, &change)?; self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?; if !change.kind().is_snapshot() { @@ -176,7 +205,22 @@ impl Repository { change_bytes: &[u8], change: &Change, ) -> Result { - if atom_change_file_version(change_bytes)? == ATOM_CHANGE_FILE_VERSION_V2 { + let version = atom_change_file_version(change_bytes)?; + if version == ATOM_CHANGE_FILE_VERSION_SPAN { + let (decoded, actual_hash) = + Change::deserialize(&mut std::io::Cursor::new(change_bytes)) + .map_err(|error| RepositoryError::Serialization(error.to_string()))?; + if actual_hash != *hash + || decoded.hashed != change.hashed + || decoded.contents != change.contents + { + return Err(RepositoryError::Serialization( + "span change bytes do not match the supplied change identity".into(), + )); + } + self.require_span_storage_capability()?; + } + if version == ATOM_CHANGE_FILE_VERSION_V2 || version == ATOM_CHANGE_FILE_VERSION_SPAN { self.pristine .require_repository_capability(CHANGE_FORMAT_VNEXT_CAPABILITY) .map_err(RepositoryError::from)?; @@ -193,10 +237,10 @@ impl Repository { _change: &Change, ) -> Result { match atom_change_file_version(change_bytes)? { - ATOM_CHANGE_FILE_VERSION_V1 | ATOM_CHANGE_FILE_VERSION_V2 => {} + ATOM_CHANGE_FILE_VERSION_V1 | ATOM_CHANGE_FILE_VERSION_V2 | ATOM_CHANGE_FILE_VERSION_SPAN => {} version => { return Err(RepositoryError::Serialization(format!( - "unsupported ATOM change file-header version {version}; supported versions are 1 and 2" + "unsupported ATOM change file-header version {version}; supported versions are 1, 2 and 3" ))) } } diff --git a/atomic-repository/src/repository/content.rs b/atomic-repository/src/repository/content.rs index 2b226239..6f6f6173 100644 --- a/atomic-repository/src/repository/content.rs +++ b/atomic-repository/src/repository/content.rs @@ -335,14 +335,19 @@ impl Repository { if item.is_directory { return Ok(MaterializedEntry::absent(normalized, Some(item.inode))); } - let bytes = retrieve_content_with_filter_fast( - &txn, - &self.change_store, - item.inode, - item.position, - atomic_core::output::alive::RetrieveOptions::new().with_graph_visibility(visibility), - ) - .map_err(|error| RepositoryError::Database(error.to_string()))?; + let bytes = + match retrieve_span_if_present(&txn, &self.change_store, item.inode, &visibility)? { + Some(bytes) => bytes, + None => retrieve_content_with_filter_fast( + &txn, + &self.change_store, + item.inode, + item.position, + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(visibility), + ) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + }; Ok(MaterializedEntry::present(normalized, item.inode, bytes)) } @@ -386,14 +391,19 @@ impl Repository { if item.is_directory { return Ok(None); } - let bytes = retrieve_content_with_filter_fast( - &txn, - &self.change_store, - item.inode, - item.position, - atomic_core::output::alive::RetrieveOptions::new().with_graph_visibility(visibility), - ) - .map_err(|error| RepositoryError::Database(error.to_string()))?; + let bytes = + match retrieve_span_if_present(&txn, &self.change_store, item.inode, &visibility)? { + Some(bytes) => bytes, + None => retrieve_content_with_filter_fast( + &txn, + &self.change_store, + item.inode, + item.position, + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(visibility), + ) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + }; Ok(Some(bytes)) } @@ -804,6 +814,8 @@ impl Repository { where T: atomic_core::pristine::GraphTxnT + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CapabilityTxnT + + atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + atomic_core::pristine::InodeGraphOps, { use atomic_core::output::alive::RetrieveOptions; @@ -835,15 +847,23 @@ impl Repository { return Ok(MaterializedEntry::absent(normalized_path, Some(item.inode))); } - let options = RetrieveOptions::new().with_graph_visibility(visibility); - let bytes = retrieve_content_with_filter_fast( - txn, - &self.change_store, - item.inode, - item.position, - options, - ) - .map_err(|e: atomic_core::record::RecordError| RepositoryError::Database(e.to_string()))?; + let bytes = if let Some(bytes) = + retrieve_span_if_present(txn, &self.change_store, item.inode, &visibility)? + { + bytes + } else { + let options = RetrieveOptions::new().with_graph_visibility(visibility); + retrieve_content_with_filter_fast( + txn, + &self.change_store, + item.inode, + item.position, + options, + ) + .map_err(|e: atomic_core::record::RecordError| { + RepositoryError::Database(e.to_string()) + })? + }; Ok(MaterializedEntry::present( normalized_path, @@ -887,6 +907,66 @@ impl Repository { } } +pub(super) fn retrieve_span_if_present( + txn: &T, + store: &C, + inode: Inode, + visibility: &GraphVisibilityClosure, +) -> Result>, RepositoryError> +where + T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CapabilityTxnT, + C: atomic_core::change::ChangeStore, +{ + use atomic_core::pristine::SPAN_RANGE_CAPABILITY; + let capability = txn + .required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .map_err(|error| RepositoryError::Database(error.to_string()))?; + let Some(root) = txn + .span_root(inode) + .map_err(|error| RepositoryError::Database(error.to_string()))? + else { + if capability.is_some() { + let position = txn + .inode_position(inode) + .map_err(|error| RepositoryError::Database(error.to_string()))?; + if let Some(position) = position.filter(|position| !position.change.is_root()) { + let hash = txn + .get_external(position.change) + .map_err(|error| RepositoryError::Database(error.to_string()))? + .ok_or_else(|| { + RepositoryError::Database("file origin hash is missing".into()) + })?; + let origin = store + .get_change(&hash) + .map_err(|error| RepositoryError::Database(error.to_string()))?; + if origin.hashed.span.is_some() { + return Err(RepositoryError::Database(format!( + "span root missing for inode {}", + inode.get() + ))); + } + } + } + return Ok(None); + }; + if !matches!(capability, Some(2) | Some(3)) { + return Err(RepositoryError::Database( + "span index has no matching repository capability".into(), + )); + } + if !visibility.contains(NodeId::new(root[0])) { + return Err(RepositoryError::Database( + "span root is outside the requested view".into(), + )); + } + atomic_core::pristine::span_patch_storage::SpanRenderContext::new(txn, store, visibility) + .and_then(|context| context.render(inode)) + .map(Some) + .map_err(|error| RepositoryError::Database(error.to_string())) +} + pub(crate) fn retrieve_content_with_filter_fast( txn: &T, changes: &C, diff --git a/atomic-repository/src/repository/insert.rs b/atomic-repository/src/repository/insert.rs index 88a6cee0..b2cbcaac 100644 --- a/atomic-repository/src/repository/insert.rs +++ b/atomic-repository/src/repository/insert.rs @@ -38,6 +38,440 @@ use atomic_core::pristine::InodeGraphOps; use atomic_core::types::{ChangePosition, EdgeFlags, GraphNode, SerializedGraphEdge}; use std::collections::{HashMap, HashSet}; +impl Repository { + // Inspect the complete closure before acquiring a reentrant operation lock. + fn span_in_closure(&self, roots: &[Hash]) -> Result { + let mut pending = roots.to_vec(); + let mut seen = HashSet::new(); + let mut has_spans = false; + while let Some(hash) = pending.pop() { + if !seen.insert(hash) { + continue; + } + let change = self.load_change(&hash)?; + has_spans |= change.hashed.span.is_some(); + pending.extend_from_slice(change.dependencies()); + } + Ok(has_spans) + } + + // Simulate the entire closure in an aborted transaction before preparing + // any operation. This validates incoming indexes and the prospective + // render without publishing a partial multi-change insertion. + fn preflight_span_references( + &self, + view_name: &str, + roots: &[Hash], + require_existing_dependencies: bool, + ) -> Result<(), RepositoryError> { + use atomic_core::change::span_patch::{ExternalFile, SpanOp}; + use atomic_core::pristine::span_patch_storage::{SpanPatchReadTxn, SpanRenderContext}; + use atomic_core::pristine::{CapabilityTxnT, PathClaimMutTxnT, SPAN_RANGE_CAPABILITY}; + let mut txn = self + .pristine + .write_txn() + .map_err(|e| RepositoryError::Database(e.to_string()))?; + txn.enable_abort_only_allocations() + .map_err(|e| RepositoryError::Database(e.to_string()))?; + txn.get_view(view_name) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .ok_or_else(|| RepositoryError::ViewNotFound { + name: view_name.to_string(), + })?; + if require_existing_dependencies { + for hash in roots { + let change = self.load_change(hash)?; + for dep in change.dependencies() { + let id = txn + .get_internal(dep) + .map_err(|e| RepositoryError::Database(e.to_string()))?; + if !matches!(id, Some(id) if txn.was_change_applied(id).map_err(|e| RepositoryError::Database(e.to_string()))?) + { + return Err(RepositoryError::Apply(format!( + "missing applied dependency {}", + dep.to_base32() + ))); + } + } + } + } + let mut stack: Vec<_> = roots.iter().rev().map(|hash| (*hash, false)).collect(); + let mut active = HashSet::new(); + let mut done = HashSet::new(); + let mut ordered = Vec::new(); + while let Some((hash, expanded)) = stack.pop() { + if expanded { + active.remove(&hash); + if done.insert(hash) { + ordered.push(hash); + } + continue; + } + if done.contains(&hash) { + continue; + } + if !active.insert(hash) { + return Err(RepositoryError::Apply("cyclic change dependency".into())); + } + let change = self.load_change(&hash)?; + if change + .hash() + .map_err(|e| RepositoryError::Apply(e.to_string()))? + != hash + { + return Err(RepositoryError::Apply("change hash mismatch".into())); + } + stack.push((hash, true)); + for dep in change.dependencies().iter().rev() { + stack.push((*dep, false)); + } + } + let mut inodes = HashSet::new(); + let preserve = view_name != self.current_view; + let options = InsertOptions::default().view(view_name); + for hash in ordered { + let change = self.load_change(&hash)?; + let existing = txn + .get_internal(&hash) + .map_err(|e| RepositoryError::Database(e.to_string()))?; + let applied = match existing { + Some(id) => txn + .was_change_applied(id) + .map_err(|e| RepositoryError::Database(e.to_string()))?, + None => false, + }; + if let Some(patch) = &change.hashed.span { + let capability = txn + .required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .map_err(|e| RepositoryError::Database(e.to_string()))?; + let indexed = match existing { + Some(id) => txn + .span_change_indexed(id) + .map_err(|e| RepositoryError::Database(e.to_string()))?, + None => false, + }; + if applied != indexed { + return Err(RepositoryError::Apply( + "Invalid change: span changes require a span-aware repository writer" + .into(), + )); + } + if capability != Some(SPAN_RANGE_CAPABILITY.minimum_version()) + && !(capability == Some(2) && patch.version == 1 && applied && indexed) + { + return Err(RepositoryError::Apply( + "span index has no matching repository capability".into(), + )); + } + } + let id = txn + .register_change(&hash) + .map_err(|e| RepositoryError::Database(e.to_string()))?; + txn.put_change_deps(id, change.dependencies()) + .map_err(|e| RepositoryError::Database(e.to_string()))?; + let view = txn + .get_view(view_name) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .unwrap(); + let membership = txn + .view_membership_set(&view) + .map_err(|e| RepositoryError::Database(e.to_string()))?; + if !membership.contains(id) { + self.ensure_change_allowed_in_view(&txn, view_name, &change)?; + let projected_change = if applied { + Change::empty(ChangeHeader::new("insert replay (no tree ops)")) + } else { + change.clone() + }; + let projection = self.plan_tree_projection( + &mut txn, + id, + hash, + &projected_change, + &[], + preserve, + )?; + projection.apply_prerequisites(&mut txn)?; + if change.hashed.span.is_some() && !applied { + write_recorded_graph_and_spans( + self, &mut txn, view_name, id, &hash, &change, &options, + )?; + } else if change.hashed.span.is_some() { + crate::apply::write_span_hunks_with_index( + &mut txn, view_name, id, &hash, &change, &options, true, + ) + .map_err(|e| RepositoryError::Apply(e.to_string()))?; + } else { + write_change_to_graph( + &mut txn, view_name, id, &hash, &change, &options, applied, + ) + .map_err(|e| RepositoryError::Apply(e.to_string()))?; + } + // apply_tree_projection persists a filesystem journal; preview + // only records the equivalent immutable claim events in redb. + for entry in super::name_resolution::path_claim_events_for_change( + &txn, + id, + &projected_change, + )? { + txn.put_path_claim(&entry.path, &entry.event) + .map_err(|e| RepositoryError::Database(e.to_string()))?; + } + } else if !applied { + return Err(RepositoryError::Apply( + "visible change is not in the ambient graph".into(), + )); + } + if let Some(patch) = &change.hashed.span { + for op in &patch.ops { + let file = match op { + SpanOp::Root { ordinal, .. } => ExternalFile { + owner: hash, + ordinal: *ordinal, + }, + SpanOp::Edit { file, .. } => file.clone(), + }; + let inode = txn + .span_file_inode(&file) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .ok_or_else(|| { + RepositoryError::Apply("span file mapping missing".into()) + })?; + inodes.insert(inode); + } + } + } + if !inodes.is_empty() { + let view = txn + .get_view(view_name) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .unwrap(); + let visibility = graph_visibility_closure(&txn, &view)?; + let projection = self.project_tree_for_visibility(&txn, &visibility)?; + for (path, conflict) in &projection.name_conflicts { + for side in &conflict.sides { + if txn + .span_root(side.inode) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .is_some_and(|root| visibility.contains(NodeId::new(root[0]))) + { + return Err(RepositoryError::Apply(format!( + "cannot materialize span-backed name conflict at '{path}'" + ))); + } + } + } + let context = SpanRenderContext::new(&txn, &self.change_store, &visibility) + .map_err(|e| RepositoryError::Apply(e.to_string()))?; + for inode in inodes { + context + .render(inode) + .map_err(|e| RepositoryError::Apply(e.to_string()))?; + } + } + txn.abort() + .map_err(|e| RepositoryError::Database(e.to_string())) + } +} + +fn write_recorded_graph_and_spans( + repository: &Repository, + txn: &mut atomic_core::pristine::WriteTxn<'_>, + view_name: &str, + change_id: NodeId, + hash: &Hash, + change: &Change, + options: &InsertOptions, +) -> Result { + let Some(patch) = &change.hashed.span else { + return write_change_to_graph(txn, view_name, change_id, hash, change, options, false) + .map_err(|error| RepositoryError::Apply(error.to_string())); + }; + use atomic_core::change::span_patch::SpanOp; + use atomic_core::pristine::{span_patch_storage::SpanPatchReadTxn, CrdtTxnT}; + let semantic = atomic_core::change::span_patch::localize_file_ops(txn, change) + .map_err(|error| RepositoryError::Apply(error.to_string()))?; + // The portable mapping is author-supplied. A hash in the dependency + // closure is not by itself proof that its semantic branch belongs to the + // file named by a span edit. + for op in &patch.ops { + let SpanOp::Edit { file, .. } = op else { + continue; + }; + let inode = txn + .span_file_inode(file) + .map_err(|e| RepositoryError::Apply(e.to_string()))? + .ok_or_else(|| RepositoryError::Apply("span file mapping missing".into()))?; + let trunk_key = txn + .get_crdt_inode_trunk(inode.get()) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .ok_or_else(|| RepositoryError::Apply("span file has no semantic trunk".into()))?; + let trunk = atomic_core::crdt::TrunkId::from_bytes(&trunk_key); + let matches: Vec<_> = semantic + .iter() + .filter(|ops| ops.trunk_id() == trunk) + .collect(); + if matches.is_empty() { + return Err(RepositoryError::Apply( + "span edit has no matching semantic trunk".into(), + )); + } + let check_branch = |id: atomic_core::crdt::BranchId| -> Result<(), RepositoryError> { + if id.change_id().is_root() { + return Ok(()); + } + let branch = txn + .get_crdt_branch(&id.to_bytes()) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .ok_or_else(|| RepositoryError::Apply("span semantic branch missing".into()))?; + if branch.trunk_id != trunk { + return Err(RepositoryError::Apply( + "span semantic branch belongs to another file".into(), + )); + } + Ok(()) + }; + for line in matches.iter().flat_map(|ops| ops.line_ops()) { + check_branch(line.branch_id())?; + use atomic_core::crdt::BranchOp; + match line.operation() { + BranchOp::Insert { + after: Some(id), .. + } => check_branch(*id)?, + BranchOp::Delete { branch, .. } + | BranchOp::Modify { branch, .. } + | BranchOp::Restore { branch } => { + check_branch(*branch)?; + if *branch != line.branch_id() { + return Err(RepositoryError::Apply( + "span semantic branch identity mismatch".into(), + )); + } + } + BranchOp::Reparent { branch, new_after } => { + check_branch(*branch)?; + if *branch != line.branch_id() { + return Err(RepositoryError::Apply( + "span semantic branch identity mismatch".into(), + )); + } + if let Some(id) = new_after { + check_branch(*id)?; + } + } + BranchOp::Insert { after: None, .. } => {} + } + let leaf_ops: Vec<&atomic_core::crdt::LeafOp> = match line.operation() { + BranchOp::Insert { content, .. } | BranchOp::Delete { content, .. } => { + content.iter().collect() + } + BranchOp::Modify { + old_content, + new_content, + .. + } => old_content.iter().chain(new_content).collect(), + BranchOp::Restore { .. } | BranchOp::Reparent { .. } => Vec::new(), + }; + for op in leaf_ops { + use atomic_core::crdt::LeafOp; + let id = match op { + LeafOp::Insert { after, .. } => *after, + LeafOp::Delete { leaf } + | LeafOp::Replace { leaf, .. } + | LeafOp::Restore { leaf } => Some(*leaf), + }; + let Some(id) = id.filter(|id| !id.change_id().is_root()) else { + continue; + }; + let leaf = txn + .get_crdt_leaf(&id.to_bytes()) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .ok_or_else(|| RepositoryError::Apply("span semantic token missing".into()))?; + if leaf.branch_id != line.branch_id() { + return Err(RepositoryError::Apply( + "span semantic token belongs to another line".into(), + )); + } + } + } + } + let inserted = crate::apply::write_span_hunks_with_index( + txn, view_name, change_id, hash, change, options, false, + ) + .map_err(|error| RepositoryError::Apply(error.to_string()))?; + let mut roots = std::collections::BTreeMap::new(); + for op in &patch.ops { + if let SpanOp::Root { path, ordinal, .. } = op { + let positions: Vec<_> = change + .hunks() + .iter() + .filter_map(|hunk| { + if let GraphOp::FileAdd { + path: added, + add_inode, + contents: None, + .. + } = hunk + { + (added == path).then_some(add_inode.start) + } else { + None + } + }) + .collect(); + if positions.len() != 1 { + return Err(RepositoryError::InvalidOperation { + message: format!("span root '{path}' has no unique namespace creation"), + }); + } + let creators: Vec<_> = semantic + .iter() + .filter(|ops| ops.path() == path && ops.is_create()) + .collect(); + if creators.len() != 1 + || !creators[0].trunk_id().change_id().is_root() + || !matches!(creators[0].trunk_op(), Some(atomic_core::crdt::TrunkOp::Create { path: created, .. }) if created == path) + { + return Err(RepositoryError::InvalidOperation { + message: format!("span root '{path}' has no unique semantic creation"), + }); + } + let position = Position::new(change_id, positions[0]); + let inode = txn + .position_inode(position) + .map_err(|error| RepositoryError::Database(error.to_string()))? + .ok_or_else(|| RepositoryError::InvalidOperation { + message: format!("span root '{path}' has no inode binding"), + })?; + if roots.insert(*ordinal, inode).is_some() { + return Err(RepositoryError::InvalidOperation { + message: "duplicate span root ordinal".into(), + }); + } + } + } + let additions = change + .hunks() + .iter() + .filter(|op| matches!(op, GraphOp::FileAdd { .. })) + .count(); + if additions != roots.len() { + return Err(RepositoryError::InvalidOperation { + message: "span change must bind every file addition to one span root".into(), + }); + } + atomic_core::pristine::span_patch_storage::index_change( + txn, + &repository.change_store, + view_name, + *hash, + change, + &roots, + ) + .map_err(|error| RepositoryError::Apply(error.to_string()))?; + Ok(inserted) +} + fn write_recorded_in_txn( repository: &Repository, txn: &mut atomic_core::pristine::WriteTxn<'_>, @@ -64,8 +498,9 @@ fn write_recorded_in_txn( tree_projection.apply_prerequisites(txn)?; let mut options = InsertOptions::default().view(view_name); options.track_conflicts = false; - let inserted = write_change_to_graph(txn, view_name, change_id, hash, change, &options, false) - .map_err(|error| RepositoryError::Apply(error.to_string()))?; + let inserted = write_recorded_graph_and_spans( + repository, txn, view_name, change_id, hash, change, &options, + )?; repository.apply_tree_projection( txn, &tree_projection, @@ -1758,18 +2193,25 @@ impl Repository { hash: &Hash, options: InsertOptions, ) -> Result { - self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?; - let trace_insert = std::env::var_os("ATOMIC_TRACE_INSERT").is_some(); - let t0 = std::time::Instant::now(); - - // Load the change from the store let change = self.load_change(hash)?; let view_name = options .view .as_deref() .unwrap_or(&self.current_view) .to_string(); + let _span_lock = if self.span_in_closure(std::slice::from_ref(hash))? { + let working_copy = self.require_working_copy_id()?; + let lock = self.try_lock_workspace_operation(working_copy)?; + self.preflight_span_references(&view_name, std::slice::from_ref(hash), true)?; + Some(lock) + } else { + None + }; + self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?; + let trace_insert = std::env::var_os("ATOMIC_TRACE_INSERT").is_some(); + let t0 = std::time::Instant::now(); + // The change was checked before any repository mutation. // CB-12B: trusted provenance publication gate. Before any metadata // operation or mutation on a Shared target, the complete reachable // closure of this change must carry trusted managed-session evidence. @@ -1996,16 +2438,29 @@ impl Repository { tree_projection.apply_prerequisites(&mut txn)?; // Apply to the graph (skips hunk application if already_in_graph) let t_graph = std::time::Instant::now(); - let mut outcome = write_change_to_graph( - &mut txn, - &view_name, - change_id, - hash, - &change, - &options, - already_in_graph, - ) - .map_err(|e| RepositoryError::Apply(e.to_string()))?; + let mut outcome = if change.hashed.span.is_some() { + if already_in_graph { + crate::apply::write_span_hunks_with_index( + &mut txn, &view_name, change_id, hash, &change, &options, true, + ) + .map_err(|e| RepositoryError::Apply(e.to_string()))? + } else { + write_recorded_graph_and_spans( + self, &mut txn, &view_name, change_id, hash, &change, &options, + )? + } + } else { + write_change_to_graph( + &mut txn, + &view_name, + change_id, + hash, + &change, + &options, + already_in_graph, + ) + .map_err(|e| RepositoryError::Apply(e.to_string()))? + }; if trace_insert { eprintln!( @@ -2068,6 +2523,35 @@ impl Repository { .iter() .filter_map(|op| op.path().map(str::to_owned)), ); + if let Some(patch) = &inserted.hashed.span { + use atomic_core::change::span_patch::{ExternalFile, SpanOp}; + use atomic_core::pristine::span_patch_storage::SpanPatchReadTxn; + for op in &patch.ops { + let file = match op { + SpanOp::Root { ordinal, .. } => ExternalFile { + owner: hash, + ordinal: *ordinal, + }, + SpanOp::Edit { file, .. } => file.clone(), + }; + let inode = txn + .span_file_inode(&file) + .map_err(|e| RepositoryError::Apply(e.to_string()))? + .ok_or_else(|| { + RepositoryError::Apply("span file mapping missing".into()) + })?; + for (path, claim) in &after_projection.present_metadata { + if claim.inode == inode { + outcome.stats.affected_paths.insert(path.clone()); + } + } + for (path, conflict) in &after_projection.name_conflicts { + if conflict.sides.iter().any(|side| side.inode == inode) { + outcome.stats.affected_paths.insert(path.clone()); + } + } + } + } } // Commit the transaction @@ -2145,6 +2629,17 @@ impl Repository { hash: &Hash, mut options: InsertOptions, ) -> Result { + let _span_lock = if self.span_in_closure(std::slice::from_ref(hash))? { + let lock = self.try_lock_workspace_operation(self.require_working_copy_id()?)?; + self.preflight_span_references( + options.view.as_deref().unwrap_or(&self.current_view), + std::slice::from_ref(hash), + false, + )?; + Some(lock) + } else { + None + }; let trace_insert = std::env::var_os("ATOMIC_TRACE_INSERT").is_some(); let t0 = std::time::Instant::now(); @@ -2376,11 +2871,9 @@ impl Repository { // Apply to the graph // For write_recorded, the change is always new (just recorded), so // already_in_graph is always false. - let mut apply_outcome = write_change_to_graph( - &mut txn, view_name, change_id, hash, change, &options, - false, // always_in_graph: freshly recorded changes are never in the graph yet - ) - .map_err(|e| RepositoryError::Apply(e.to_string()))?; + let mut apply_outcome = write_recorded_graph_and_spans( + self, &mut txn, view_name, change_id, hash, change, &options, + )?; self.apply_tree_projection( &mut txn, @@ -2697,6 +3190,17 @@ impl Repository { .map(|(_, hash)| hash) .collect() }; + let _span_lock = if self.span_in_closure(&source_changes)? { + let lock = self.try_lock_workspace_operation(self.require_working_copy_id()?)?; + self.preflight_span_references( + &options.to_view, + &source_changes, + !options.apply_dependencies, + )?; + Some(lock) + } else { + None + }; if trace_insert { eprintln!( diff --git a/atomic-repository/src/repository/materialize.rs b/atomic-repository/src/repository/materialize.rs index 43e921ec..b4ca7161 100644 --- a/atomic-repository/src/repository/materialize.rs +++ b/atomic-repository/src/repository/materialize.rs @@ -455,6 +455,11 @@ pub(super) fn render_name_conflict_side( inode: Inode, position: Position, ) -> Result, String> { + if let Some(bytes) = super::content::retrieve_span_if_present(txn, store, inode, visibility) + .map_err(|error| format!("{path}: span: {error}"))? + { + return Ok(bytes); + } use atomic_core::output::repo::{ output_graph_content_resolved, resolve_conflicts_semantically, }; @@ -1411,6 +1416,8 @@ impl Repository { self.validate_working_copy(working_copy)?; use atomic_core::output::repo::OutputItem; use atomic_core::output::RetrieveOptions; + use atomic_core::pristine::span_patch_storage::{SpanPatchReadTxn, SpanRenderContext}; + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; use rayon::prelude::*; let txn = self @@ -1441,6 +1448,74 @@ impl Repository { }) .collect(); + // Name-conflict rendering still uses graph vertices; never render an + // indexed span claimant as an empty graph side. + for (path, conflict) in &name_conflicts { + if only_paths + .as_ref() + .is_some_and(|paths| !paths.contains(path)) + { + continue; + } + for side in conflict.sides_at_path(path) { + if txn + .span_root(side.inode) + .map_err(|error| RepositoryError::Database(error.to_string()))? + .is_some() + { + return Err(RepositoryError::InvalidOperation { + message: format!( + "cannot materialize span-backed name conflict at '{path}'" + ), + }); + } + } + } + let mut has_visible_spans = false; + for item in &file_items { + match txn + .span_root(item.inode) + .map_err(|error| RepositoryError::Database(error.to_string()))? + { + Some(root) if visibility.contains(NodeId::new(root[0])) => { + if !matches!( + txn.required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + Some(2) | Some(3) + ) { + return Err(RepositoryError::Database( + "span index has no matching repository capability".into(), + )); + } + has_visible_spans = true; + } + Some(_) => { + return Err(RepositoryError::Database(format!( + "span root for '{}' is outside the requested view", + item.path + ))) + } + None => { + // Distinguish legacy graph files from a missing span index + // before any materialization operation is prepared. + super::content::retrieve_span_if_present( + &txn, + &self.change_store, + item.inode, + &visibility, + )?; + } + } + } + let span_context = if has_visible_spans { + Some( + SpanRenderContext::new(&txn, &self.change_store, &visibility) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + ) + } else { + None + }; + let mut inode_materialization = std::collections::HashMap::new(); for item in &file_items { let projected = atomic_core::output::project_inode_attributes( @@ -1619,85 +1694,122 @@ impl Repository { .map(|item| { let file_start = std::time::Instant::now(); - // Cloning validated visibility is O(1). - let retrieve_opts = - RetrieveOptions::default().with_graph_visibility(visibility.clone()); - - // Inline the output pipeline so we can trace each phase. - use atomic_core::output::repo::{ - output_graph_content_resolved, resolve_conflicts_semantically, + let span_content = match &span_context { + Some(context) => context + .render_if_visible(item.inode) + .map_err(|error| format!("{}: span: {}", item.path, error))?, + None => None, }; - use atomic_core::output::{compute_order, retrieve_graph, Writer}; - use atomic_core::pristine::InodePreloadTxn; - - // Pre-load ALL edges for this file's inode from INODE_GRAPH - // in a single range scan, then run retrieve_graph over the - // in-memory HashMap. O(M) scan + O(1) lookups vs O(V×log N) - // individual B-tree probes. - let preloaded = InodePreloadTxn::from_table(&txn, item.inode, &inode_graph_table) - .map_err(|e| format!("{}: preload: {:?}", item.path, e))?; - - let t_retrieve = std::time::Instant::now(); - let retrieve_result = retrieve_graph(&preloaded, item.position, retrieve_opts) - .map_err(|e| format!("{}: retrieve: {:?}", item.path, e))?; - - let vertices = retrieve_result.graph.len_vertices(); - let edges = retrieve_result.edges_traversed; - let retrieve_ms = t_retrieve.elapsed(); - let mut graph = retrieve_result.graph; - - let (repository_content, order_ms, content_ms, rendered_conflicts) = if graph - .is_empty() - { + + let ( + repository_content, + vertices, + edges, + retrieve_ms, + order_ms, + content_ms, + rendered_conflicts, + ) = if let Some(content) = span_content { ( - Vec::new(), + content, + 0, + 0, + std::time::Duration::ZERO, std::time::Duration::ZERO, std::time::Duration::ZERO, false, ) } else { - let t_order = std::time::Instant::now(); - let order = compute_order(&mut graph); - let order_ms = t_order.elapsed(); + // Cloning validated visibility is O(1). + let retrieve_opts = + RetrieveOptions::default().with_graph_visibility(visibility.clone()); - let t_content = std::time::Instant::now(); - let resolved = resolve_conflicts_semantically( - &preloaded, store, &graph, &order, - ) - .map_err(|error| format!("{}: semantic resolution: {}", item.path, error))?; - let buffer = Vec::with_capacity(graph.total_bytes()); - let mut writer = Writer::new(buffer); - let hash_fn = |node_id: NodeId| -> Result< - Option, - atomic_core::pristine::PristineError, - > { - if node_id.is_root() { - return Ok(None); - } - external_hashes.get(&node_id).copied().map(Some).ok_or( - atomic_core::pristine::PristineError::ChangeNotFound { - id: node_id.get(), - }, - ) + // Inline the output pipeline so we can trace each phase. + use atomic_core::output::repo::{ + output_graph_content_resolved, resolve_conflicts_semantically, }; - output_graph_content_resolved( - store, - hash_fn, - &graph, - &order, - &mut writer, - &resolved, - ) - .map_err(|e| format!("{}: content: {:?}", item.path, e))?; - // Whether the renderer itself emitted a conflict region. - // A file whose *source* merely contains marker-shaped - // lines (documentation examples, test fixtures) writes - // them through `output_line` and stays marker-free here. - let rendered_conflicts = writer.has_conflict_markers(); + use atomic_core::output::{compute_order, retrieve_graph, Writer}; + use atomic_core::pristine::InodePreloadTxn; + + // Pre-load ALL edges for this file's inode from INODE_GRAPH + // in a single range scan, then run retrieve_graph over the + // in-memory HashMap. O(M) scan + O(1) lookups vs O(V×log N) + // individual B-tree probes. + let preloaded = + InodePreloadTxn::from_table(&txn, item.inode, &inode_graph_table) + .map_err(|e| format!("{}: preload: {:?}", item.path, e))?; + + let t_retrieve = std::time::Instant::now(); + let retrieve_result = retrieve_graph(&preloaded, item.position, retrieve_opts) + .map_err(|e| format!("{}: retrieve: {:?}", item.path, e))?; + + let vertices = retrieve_result.graph.len_vertices(); + let edges = retrieve_result.edges_traversed; + let retrieve_ms = t_retrieve.elapsed(); + let mut graph = retrieve_result.graph; + + let (repository_content, order_ms, content_ms, rendered_conflicts) = + if graph.is_empty() { + ( + Vec::new(), + std::time::Duration::ZERO, + std::time::Duration::ZERO, + false, + ) + } else { + let t_order = std::time::Instant::now(); + let order = compute_order(&mut graph); + let order_ms = t_order.elapsed(); + + let t_content = std::time::Instant::now(); + let resolved = + resolve_conflicts_semantically(&preloaded, store, &graph, &order) + .map_err(|error| { + format!("{}: semantic resolution: {}", item.path, error) + })?; + let buffer = Vec::with_capacity(graph.total_bytes()); + let mut writer = Writer::new(buffer); + let hash_fn = |node_id: NodeId| -> Result< + Option, + atomic_core::pristine::PristineError, + > { + if node_id.is_root() { + return Ok(None); + } + external_hashes.get(&node_id).copied().map(Some).ok_or( + atomic_core::pristine::PristineError::ChangeNotFound { + id: node_id.get(), + }, + ) + }; + output_graph_content_resolved( + store, + hash_fn, + &graph, + &order, + &mut writer, + &resolved, + ) + .map_err(|e| format!("{}: content: {:?}", item.path, e))?; + // Whether the renderer itself emitted a conflict region. + // A file whose *source* merely contains marker-shaped + // lines (documentation examples, test fixtures) writes + // them through `output_line` and stays marker-free here. + let rendered_conflicts = writer.has_conflict_markers(); + ( + writer.into_inner(), + order_ms, + t_content.elapsed(), + rendered_conflicts, + ) + }; ( - writer.into_inner(), + repository_content, + vertices, + edges, + retrieve_ms, order_ms, - t_content.elapsed(), + content_ms, rendered_conflicts, ) }; diff --git a/atomic-repository/src/repository/record.rs b/atomic-repository/src/repository/record.rs index daec8d03..473fae36 100644 --- a/atomic-repository/src/repository/record.rs +++ b/atomic-repository/src/repository/record.rs @@ -15,6 +15,486 @@ type ExistingSemanticState = ( Vec, ); +fn span_namespace_dependencies( + txn: &T, + visibility: &GraphVisibilityClosure, + present: &std::collections::HashMap, + path: &str, + inode: Inode, +) -> Result, RecordError> { + let mut dependencies = Vec::new(); + let mut component = Some(Path::new(path)); + while let Some(current) = component.filter(|current| !current.as_os_str().is_empty()) { + let current_path = current + .to_str() + .ok_or_else(|| RecordError::Database(format!("invalid span path '{path}'")))?; + let claim = present.get(current_path).ok_or_else(|| { + RecordError::Database(format!("missing visible path claim for '{current_path}'")) + })?; + if current_path == path && claim.inode != inode { + return Err(RecordError::Database(format!( + "span path '{path}' changed identity" + ))); + } + let ids = claim + .event_changes + .iter() + .copied() + .chain(claim.claims.iter().flat_map(|claim| { + [ + claim.introduced_by, + claim.claimant.change, + claim.parent.change, + claim.name.change, + ] + })); + for id in ids.filter(|id| !id.is_root()) { + if !visibility.contains(id) { + return Err(RecordError::Database(format!( + "path claim for '{current_path}' is outside the view" + ))); + } + dependencies.push( + txn.get_external(id) + .map_err(|error| RecordError::Database(error.to_string()))? + .ok_or_else(|| { + RecordError::Database(format!("path claim change {} has no hash", id.get())) + })?, + ); + } + component = current.parent(); + } + dependencies.sort(); + dependencies.dedup(); + Ok(dependencies) +} + +/// Replace the linewise content hunks of newly added files with immutable +/// span roots. Namespace hunks and semantic operations are retained. +fn lower_new_files_to_spans(change: &mut Change) -> Result<(), RecordError> { + use atomic_core::change::span_patch::{SpanOp, SpanPatch}; + use atomic_core::change::Atom; + + let mut roots = Vec::<(String, u64, u64, u64)>::new(); + for hunk in change.hunks() { + if let GraphOp::FileAdd { + path, + add_inode, + contents, + .. + } = hunk + { + let (start, end) = contents + .as_ref() + .map(|content| (content.start.get(), content.end.get())) + .unwrap_or((add_inode.start.get(), add_inode.start.get())); + if roots.iter().any(|(existing, ..)| existing == path) { + return Err(RecordError::Database(format!( + "duplicate added file '{path}'" + ))); + } + roots.push((path.clone(), add_inode.start.get(), start, end)); + } + } + if roots.is_empty() { + return Ok(()); + } + for hunk in change.hunks() { + match hunk { + GraphOp::Edit { + change: Atom::Insertion(insert), + local, + .. + } if roots.iter().any(|(path, ..)| path == &local.path) => { + let (_, inode, _, end) = roots + .iter_mut() + .find(|(path, ..)| path == &local.path) + .unwrap(); + if insert.inode.change.is_some() + || insert.inode.pos.get() != *inode + || insert.start.get() != *end + || !insert.successors.is_empty() + || insert.predecessors.len() != 1 + || insert.predecessors[0].change.is_some() + || insert.predecessors[0].pos.get() != *end + { + return Err(RecordError::Database(format!( + "cannot lower added file '{}' to a contiguous span", + local.path + ))); + } + *end = insert.end.get(); + } + GraphOp::Edit { local, .. } | GraphOp::Replacement { local, .. } + if roots.iter().any(|(path, ..)| path == &local.path) => + { + return Err(RecordError::Database(format!( + "cannot lower added file '{}' to a contiguous span", + local.path + ))); + } + _ => {} + } + } + let ops = roots + .iter() + .enumerate() + .map(|(ordinal, (path, _, start, end))| { + let ordinal = u32::try_from(ordinal) + .map_err(|_| RecordError::Database("too many span roots".into()))?; + Ok(SpanOp::Root { + path: path.clone(), + ordinal, + start: *start, + end: *end, + }) + }) + .collect::, RecordError>>()?; + for hunk in &mut change.hashed.hunks { + if let GraphOp::FileAdd { contents, .. } = hunk { + *contents = None; + } + } + change + .hashed + .hunks + .retain(|hunk| !matches!(hunk, GraphOp::Edit { local, .. } if roots.iter().any(|(path, ..)| path == &local.path))); + match &mut change.hashed.span { + Some(patch) => patch.ops.extend(ops), + None => change.hashed.span = Some(SpanPatch::new(Vec::new(), ops)), + } + Ok(()) +} + +fn record_span_edit( + detected: &atomic_core::record::workflow::DetectedFile, + state: &atomic_core::pristine::span_patch_storage::SpanRecordState, + namespace_dependencies: Vec, + new: &[u8], + trunk: atomic_core::crdt::TrunkId, + branches: &[atomic_core::crdt::BranchId], + algorithm: atomic_core::diff::Algorithm, +) -> Result { + use atomic_core::change::span_patch::SpanOp; + use atomic_core::crdt::BranchOp; + use atomic_core::diff::{diff_raw, DiffOp, Line}; + use atomic_core::record::workflow::recipes::{Recipe, RecipeContext}; + use atomic_core::record::workflow::{DetectionKind, RecordedFile}; + use atomic_core::types::ChangePosition; + let path = detected.path.as_str(); + let inode = detected + .inode + .ok_or_else(|| "span edit has no inode".to_string())?; + let position = detected + .position + .ok_or_else(|| "span edit has no position".to_string())?; + let old = state.content.as_slice(); + if atomic_core::record::workflow::compare::detect_encoding(old) + == atomic_core::change::Encoding::Binary + || atomic_core::record::workflow::compare::detect_encoding(new) + == atomic_core::change::Encoding::Binary + { + return Err("span recording cannot yet change between text and binary".into()); + } + let old_lines = Line::from_bytes(old); + let new_lines = Line::from_bytes(new); + if old_lines.len() != branches.len() { + return Err("semantic lines do not match the span baseline".into()); + } + let mut old_offsets = vec![0usize]; + for line in &old_lines { + old_offsets.push(old_offsets.last().copied().unwrap() + line.len()); + } + let mut blocks = Vec::new(); + let mut open: Option<(usize, usize)> = None; + let (mut old_cursor, mut new_cursor) = (0usize, 0usize); + let diff = diff_raw(&old_lines, &new_lines, algorithm); + for op in diff.ops() { + let (old_pos, new_pos, old_len, new_len, equal) = match *op { + DiffOp::Equal { + old_pos, + new_pos, + len, + } => (old_pos, new_pos, len, len, true), + DiffOp::Insert { + old_pos, + new_pos, + len, + } => (old_pos, new_pos, 0, len, false), + DiffOp::Delete { + old_pos, + new_pos, + len, + } => (old_pos, new_pos, len, 0, false), + DiffOp::Replace { + old_pos, + old_len, + new_pos, + new_len, + } => (old_pos, new_pos, old_len, new_len, false), + }; + if old_pos != old_cursor || new_pos != new_cursor { + return Err("invalid line diff ordering".into()); + } + if equal { + if let Some((old_start, new_start)) = open.take() { + blocks.push((old_start, old_cursor, new_start, new_cursor)); + } + if old_lines.get(old_cursor..old_cursor + old_len) + != new_lines.get(new_cursor..new_cursor + new_len) + { + return Err("line diff does not preserve equal bytes".into()); + } + } else if open.is_none() { + open = Some((old_cursor, new_cursor)); + } + old_cursor += old_len; + new_cursor += new_len; + } + if let Some((old_start, new_start)) = open { + blocks.push((old_start, old_cursor, new_start, new_cursor)); + } + if old_cursor != old_lines.len() || new_cursor != new_lines.len() || blocks.is_empty() { + return Err("line diff does not cover the file".into()); + } + + let mut content = Vec::new(); + let mut span_ops = Vec::new(); + let mut line_ranges = std::collections::BTreeMap::new(); + for (old_start, old_end, new_start, new_end) in blocks { + let insert_start = content.len() as u64; + for (index, line) in new_lines[new_start..new_end].iter().enumerate() { + let start = content.len() as u64; + content.extend_from_slice(line.content()); + line_ranges.insert(new_start + index + 1, (start, content.len() as u64)); + } + let insert_end = content.len() as u64; + let start = old_offsets[old_start]; + let end = old_offsets[old_end]; + if start == end { + let segment = state + .spans + .iter() + .find(|span| span.output.start < start && start <= span.output.end) + .or_else(|| state.spans.iter().find(|span| span.output.start == start)); + let (target, offset) = match segment { + Some(segment) => ( + segment.target.clone(), + segment.target_start + (start - segment.output.start) as u64, + ), + None if old.is_empty() && start == 0 => (state.root.clone(), 0), + None => return Err("no authored span anchors the insertion".into()), + }; + span_ops.push(SpanOp::Edit { + file: state.file.clone(), + target, + start: offset, + end: offset, + insert_start, + insert_end, + }); + } else { + let mut cursor = start; + let mut first = true; + for segment in &state.spans { + let lo = start.max(segment.output.start); + let hi = end.min(segment.output.end); + if lo >= hi { + continue; + } + if lo != cursor { + return Err("span baseline does not cover the edit".into()); + } + let target_start = segment.target_start + (lo - segment.output.start) as u64; + span_ops.push(SpanOp::Edit { + file: state.file.clone(), + target: segment.target.clone(), + start: target_start, + end: target_start + (hi - lo) as u64, + insert_start: if first { insert_start } else { insert_end }, + insert_end, + }); + cursor = hi; + first = false; + } + if cursor != end { + return Err("span baseline does not cover the edit".into()); + } + } + } + let encoding = atomic_core::record::workflow::compare::detect_encoding(new); + let context = RecipeContext { + path, + old_content: old, + new_content: new, + existing_branches: Some(branches), + existing_trunk_id: Some(trunk), + encoding, + algorithm, + }; + let (mut ops, stats) = Recipe::InPlaceEdit.build_ops(&context); + let mut mapped = std::collections::BTreeSet::new(); + for line in ops.line_ops_mut() { + if let Some(number) = line.new_line_num() { + if let Some(&(start, end)) = line_ranges.get(&number) { + if matches!( + line.operation(), + BranchOp::Insert { .. } | BranchOp::Modify { .. } + ) { + if !mapped.insert(number) { + return Err("duplicate semantic line mapping".into()); + } + line.set_content_range(ChangePosition::new(start), ChangePosition::new(end)); + } + } + } + } + if mapped.len() != line_ranges.len() { + return Err("semantic edits do not cover the changed lines".into()); + } + let mut recorded = RecordedFile::new(path); + recorded.set_kind(DetectionKind::Modified); + recorded.set_inode(inode); + recorded.set_position(position); + recorded.set_encoding(encoding); + recorded.set_old_line_count(old_lines.len()); + recorded.set_content(content); + recorded.set_pre_globalized(Vec::new()); + recorded.set_span_ops(span_ops); + let mut dependencies = state.dependencies.clone(); + dependencies.extend(namespace_dependencies); + dependencies.sort(); + dependencies.dedup(); + recorded.set_span_dependencies(dependencies); + recorded.set_crdt_ops(ops); + recorded.set_crdt_stats(stats); + Ok(recorded) +} + +fn record_binary_span_edit( + detected: &atomic_core::record::workflow::DetectedFile, + state: &atomic_core::pristine::span_patch_storage::SpanRecordState, + namespace_dependencies: Vec, + new: &[u8], + trunk: atomic_core::crdt::TrunkId, + branches: &[atomic_core::crdt::BranchId], + algorithm: atomic_core::diff::Algorithm, +) -> Result { + use atomic_core::change::span_patch::SpanOp; + use atomic_core::change::{Encoding, FileOps, LineOps}; + use atomic_core::crdt::BranchOp; + use atomic_core::diff::Line; + use atomic_core::record::workflow::recipes::{Recipe, RecipeContext}; + use atomic_core::record::workflow::{DetectionKind, RecordedFile}; + use atomic_core::types::ChangePosition; + let inode = detected + .inode + .ok_or_else(|| "span edit has no inode".to_string())?; + let position = detected + .position + .ok_or_else(|| "span edit has no position".to_string())?; + let mut edits = Vec::new(); + let mut cursor = 0usize; + for segment in &state.spans { + if segment.output.start != cursor { + return Err("binary span baseline is incomplete".into()); + } + let length = segment.output.end - segment.output.start; + edits.push(SpanOp::Edit { + file: state.file.clone(), + target: segment.target.clone(), + start: segment.target_start, + end: segment.target_start + length as u64, + insert_start: if cursor == 0 { 0 } else { new.len() as u64 }, + insert_end: new.len() as u64, + }); + cursor = segment.output.end; + } + if cursor != state.content.len() { + return Err("binary span baseline is incomplete".into()); + } + if edits.is_empty() { + edits.push(SpanOp::Edit { + file: state.file.clone(), + target: state.root.clone(), + start: 0, + end: 0, + insert_start: 0, + insert_end: new.len() as u64, + }); + } + let mut recorded = RecordedFile::new(detected.path.as_str()); + recorded.set_kind(DetectionKind::Modified); + recorded.set_inode(inode); + recorded.set_position(position); + let encoding = atomic_core::record::workflow::compare::detect_encoding(new); + recorded.set_encoding(encoding); + recorded.set_content(new.to_vec()); + recorded.set_pre_globalized(Vec::new()); + recorded.set_span_ops(edits); + let mut dependencies = state.dependencies.clone(); + dependencies.extend(namespace_dependencies); + dependencies.sort(); + dependencies.dedup(); + recorded.set_span_dependencies(dependencies); + let mut ops = if encoding == Encoding::Binary { + let mut ops = FileOps::edit(trunk, detected.path.clone()); + for branch in branches { + ops.add_line_op(LineOps::delete_empty(*branch)); + } + ops + } else { + let context = RecipeContext { + path: &detected.path, + old_content: b"", + new_content: new, + existing_branches: None, + existing_trunk_id: Some(trunk), + encoding, + algorithm, + }; + let (ops, stats) = Recipe::InPlaceEdit.build_ops(&context); + recorded.set_crdt_stats(stats); + ops + }; + if encoding != Encoding::Binary { + if !branches.is_empty() { + ops.line_ops_mut() + .splice(0..0, branches.iter().copied().map(LineOps::delete_empty)); + } + let lines = Line::from_bytes(new); + let mut offsets = Vec::with_capacity(lines.len()); + let mut pos = 0u64; + for line in &lines { + let start = pos; + pos += line.len() as u64; + offsets.push((start, pos)); + } + let mut mapped = std::collections::BTreeSet::new(); + for line in ops.line_ops_mut() { + if let Some(index) = line.new_line_num().and_then(|number| number.checked_sub(1)) { + if matches!( + line.operation(), + BranchOp::Insert { .. } | BranchOp::Modify { .. } + ) { + let &(start, end) = offsets + .get(index) + .ok_or_else(|| "semantic line outside binary transition".to_string())?; + if !mapped.insert(index) { + return Err("duplicate semantic line mapping".into()); + } + line.set_content_range(ChangePosition::new(start), ChangePosition::new(end)); + } + } + } + if mapped.len() != lines.len() { + return Err("semantic lines do not cover binary transition".into()); + } + } + recorded.set_crdt_ops(ops); + Ok(recorded) +} + /// Walk the line order recorded by one effective closure. Ambient AFTER /// rows may have been reparented by a different view and are not a baseline. pub(super) fn semantic_branch_order( @@ -375,7 +855,9 @@ impl Repository { } } dependencies.insert(id, parents); - for ops in change.file_ops() { + let file_ops = atomic_core::change::span_patch::localize_file_ops(&txn, &change) + .map_err(|error| error.to_string())?; + for ops in &file_ops { let raw = ops.trunk_id(); let trunk = if raw.change_id().is_root() { TrunkId::new(id, raw.file_idx()) @@ -1155,7 +1637,12 @@ impl Repository { .get_external(change_id) .map_err(|e| e.to_string())? .ok_or_else(|| format!("closure change {change_id} has no hash"))?; - let change = self.load_change(&hash).map_err(|e| e.to_string())?; + let mut change = self.load_change(&hash).map_err(|e| e.to_string())?; + if change.hashed.span.is_some() { + change.hashed.file_ops = + atomic_core::change::span_patch::localize_file_ops(txn, &change) + .map_err(|e| e.to_string())?; + } memo.fold(change_id, &change); } Ok(memo.trunk(&trunk)) @@ -1221,6 +1708,34 @@ impl Repository { ) -> Result { self.validate_working_copy(working_copy) .map_err(RecordError::Repository)?; + // Keep the selected view and the span baseline stable from status + // through assembly and journaled publication. Nested record operations + // on this thread reuse the workspace lock. + let _span_lock = { + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + let txn = self + .pristine + .read_txn() + .map_err(|error| RecordError::Database(error.to_string()))?; + let capability = txn + .required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .map_err(|error| RecordError::Database(error.to_string()))?; + if capability == Some(2) { + return Err(RecordError::Database( + "span capability version 2 requires migration before recording".into(), + )); + } + let enabled = capability.is_some(); + drop(txn); + if enabled { + Some( + self.try_lock_workspace_operation(working_copy) + .map_err(RecordError::Repository)?, + ) + } else { + None + } + }; let desired_view = self .desired_view_name(working_copy) .map_err(RecordError::Repository)?; @@ -1385,6 +1900,11 @@ impl Repository { .map_err(|e| RecordError::Database(e.to_string()))?; let shared_graph_visibility = graph_visibility_closure(&shared_txn, &view).map_err(RecordError::Repository)?; + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + let span_recording = shared_txn + .required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .map_err(|error| RecordError::Database(error.to_string()))? + == Some(SPAN_RANGE_CAPABILITY.minimum_version()); let tree_projection = self .project_tree_for_visibility(&shared_txn, &shared_graph_visibility) .map_err(RecordError::Repository)?; @@ -1423,6 +1943,18 @@ impl Repository { let shared_cached_txn = CachedGraphTxn::new(&shared_txn).map_err(|e| RecordError::Database(e.to_string()))?; + let span_context = if span_recording { + Some( + atomic_core::pristine::span_patch_storage::SpanRenderContext::new( + &shared_cached_txn, + &self.change_store, + &shared_graph_visibility, + ) + .map_err(|error| RecordError::Database(error.to_string()))?, + ) + } else { + None + }; let move_planning = plan_file_moves( working_copy, &shared_txn, @@ -1790,6 +2322,28 @@ impl Repository { (!branches.is_empty()).then_some(branches.as_slice()), ) .map_err(RecordError::Database)?; + if let Some(context) = &span_context { + if let Some(state) = context + .record_state(planned.inode) + .map_err(|error| RecordError::Database(error.to_string()))? + { + if state.content != filtered.bytes { + return Err(RecordError::Database(format!( + "cannot yet record a span-backed move with content changes: '{}'", + planned.new_path + ))); + } + let mut dependencies = state.dependencies; + dependencies.extend(span_namespace_dependencies( + &shared_txn, + &shared_graph_visibility, + &projected_present, + &planned.old_path, + planned.inode, + )?); + recorded.set_span_dependencies(dependencies); + } + } if opaque { recorded.set_opaque_generated(true); log::warn!( @@ -2246,6 +2800,35 @@ impl Repository { // Record deletion (no content needed) match record_deleted_file(&detected, &core_options) { Ok(mut recorded) => { + let mut span_backed = false; + if let Some(context) = &span_context { + match context.record_state(file_inode) { + Ok(Some(state)) => { + span_backed = true; + let mut dependencies = state.dependencies; + dependencies.extend(span_namespace_dependencies( + &shared_txn, + &shared_graph_visibility, + &projected_present, + &path, + file_inode, + )?); + recorded.set_span_dependencies(dependencies); + } + Ok(None) => { + super::content::retrieve_span_if_present( + &shared_cached_txn, + &self.change_store, + file_inode, + &shared_graph_visibility, + ) + .map_err(RecordError::Repository)?; + } + Err(error) => { + return Err(RecordError::Database(error.to_string())) + } + } + } let (trunk, _) = existing_crdt_identity( self, &shared_txn, @@ -2253,8 +2836,12 @@ impl Repository { &effective_view, &shared_graph_visibility, )?; - if trunk.is_none() { - recorded.clear_crdt_ops(); + match trunk { + Some(trunk) if span_backed => recorded.set_crdt_ops( + atomic_core::change::FileOps::delete(trunk, path.clone()), + ), + None => recorded.clear_crdt_ops(), + Some(_) => {} } stats.files_recorded += 1; stats.hunks_created += recorded.hunk_count(); @@ -2380,7 +2967,23 @@ impl Repository { // state, so a positional diff against it can corrupt the file. // We propagate the flag to `record_modified_file` via // `force_whole_file_replace` below instead of diffing against it. - let (old_content, had_fork_structure) = { + let span_state = match &span_context { + Some(context) => match context.record_state(file_inode) { + Ok(content) => content, + Err(error) => return ModifiedResult::Fatal(RecordError::Database(error.to_string())), + }, + None => None, + }; + if span_recording && span_state.is_none() { + match super::content::retrieve_span_if_present(&shared_cached_txn, &self.change_store, file_inode, &shared_graph_visibility) { + Ok(None) => {} + Ok(Some(_)) => return ModifiedResult::Fatal(RecordError::Database(format!("span baseline changed while recording '{path}'"))), + Err(error) => return ModifiedResult::Fatal(RecordError::Database(error.to_string())), + } + } + let (old_content, had_fork_structure) = if let Some(state) = &span_state { + (state.content.clone(), false) + } else { use atomic_core::output::alive::RetrieveOptions; let opts = RetrieveOptions::new() .with_graph_visibility(shared_graph_visibility.clone()); @@ -2435,7 +3038,7 @@ impl Repository { // branch belongs to a change visible on this view. That keeps // single-view linear histories on the CRDT cleanup path without // leaking sibling-view branches into cross-view recording. - let can_use_crdt_old_content = !existing_branches.is_empty() + let can_use_crdt_old_content = span_state.is_none() && !existing_branches.is_empty() && existing_branches .iter() .all(|branch| shared_graph_visibility.contains(branch.change_id())); @@ -2453,6 +3056,39 @@ impl Repository { Some(existing_branches.as_slice()) }; + if let Some(state) = &span_state { + if state.content == repository_bytes && resolution.is_none() { + return ModifiedResult::Skipped(path.clone(), None); + } + if resolution.is_some() { + return ModifiedResult::Fatal(RecordError::Database(format!("span recording does not yet support this edit to '{path}'"))); + } + let Some(trunk) = existing_trunk_id else { + return ModifiedResult::Fatal(RecordError::Database(format!("span recording requires semantic state for '{path}'"))); + }; + let namespace_dependencies = match span_namespace_dependencies(&shared_txn, &shared_graph_visibility, &projected_present, path, file_inode) { + Ok(dependencies) => dependencies, + Err(error) => return ModifiedResult::Fatal(error), + }; + let mut detected = atomic_core::record::workflow::DetectedFile::modified(path.as_str()); + detected.inode = Some(file_inode); + detected.position = Some(file_position); + let binary = atomic_core::record::workflow::compare::detect_encoding(&old_content) == atomic_core::change::Encoding::Binary + || atomic_core::record::workflow::compare::detect_encoding(&repository_bytes) == atomic_core::change::Encoding::Binary; + if opaque && !binary { + return ModifiedResult::Fatal(RecordError::Database(format!("span recording does not yet support this edit to '{path}'"))); + } + let result = if binary { + record_binary_span_edit(&detected, state, namespace_dependencies, &repository_bytes, trunk, &existing_branches, core_options.get_algorithm()) + } else { + record_span_edit(&detected, state, namespace_dependencies, &repository_bytes, trunk, &existing_branches, core_options.get_algorithm()) + }; + return match result { + Ok(recorded) => ModifiedResult::Recorded(path.clone(), Box::new(recorded), None), + Err(error) => ModifiedResult::Fatal(RecordError::Database(format!("{path}: {error}"))), + }; + } + // Build line-level hunks + CRDT ops via the globalize-compatible // path. `record_modified_file` reads the new content from disk, // handles opaque/binary files, and emits BuiltHunks plus CRDT @@ -2638,6 +3274,34 @@ impl Repository { } stats.content_bytes += hunk.content_len(); } + if !recorded.span_ops().is_empty() { + stats.content_bytes += recorded.content_len() as u64; + for op in recorded.span_ops() { + if let atomic_core::change::span_patch::SpanOp::Edit { + start, + end, + insert_start, + insert_end, + .. + } = op + { + if insert_start < insert_end { + stats.vertices_added += 1; + } + if start < end { + stats.edges_modified += 1; + } + } + } + if let Some(crdt) = recorded.crdt_stats() { + stats.lines_added += crdt.lines_added; + stats.lines_deleted += crdt.lines_deleted; + stats.lines_modified += crdt.lines_modified; + stats.tokens_added += crdt.tokens_added; + stats.tokens_deleted += crdt.tokens_deleted; + stats.tokens_replaced += crdt.tokens_replaced; + } + } recorded_paths.push(path); recorded_files.push(*recorded); } @@ -2844,6 +3508,43 @@ impl Repository { if !application_metadata.is_empty() { change.hashed.metadata = application_metadata.to_vec(); } + if span_recording { + lower_new_files_to_spans(&mut change)?; + // Semantic IDs are repository-local. Carry their content-addressed + // owners in the hashed span section so another repository can + // translate every reference without guessing local allocations. + let ids: std::collections::BTreeSet<_> = change + .file_ops() + .iter() + .flat_map(|ops| ops.referenced_node_ids()) + .collect(); + let mut owners = Vec::with_capacity(ids.len()); + for id in ids { + let owner = shared_txn + .get_external(id) + .map_err(|error| RecordError::Database(error.to_string()))? + .ok_or_else(|| { + RecordError::Database(format!("semantic change {} has no hash", id.get())) + })?; + if !shared_graph_visibility.contains(id) { + return Err(RecordError::Database(format!( + "semantic change {} is outside the recording view", + id.get() + ))); + } + owners.push(atomic_core::change::span_patch::SemanticOwner { + authored_id: id.get(), + owner, + }); + change.hashed.dependencies.push(owner); + } + change.hashed.dependencies.sort(); + change.hashed.dependencies.dedup(); + let patch = change.hashed.span.get_or_insert_with(|| { + atomic_core::change::span_patch::SpanPatch::new(Vec::new(), Vec::new()) + }); + patch.semantic_owners = owners; + } lifecycle.verify_promotion_content(&change, self)?; change = lifecycle.classify(change)?; if trace_record { @@ -3647,6 +4348,12 @@ fn plan_file_moves( let load_old_content = |inode: Inode, position: Position| -> Result, RecordError> { + if let Some(bytes) = + super::content::retrieve_span_if_present(cached, store, inode, visibility) + .map_err(RecordError::Repository)? + { + return Ok(bytes); + } super::content::retrieve_content_with_filter_fast_with_fork_info( cached, store, diff --git a/atomic-repository/src/repository/remote_cache.rs b/atomic-repository/src/repository/remote_cache.rs index 790ba27a..6567f775 100644 --- a/atomic-repository/src/repository/remote_cache.rs +++ b/atomic-repository/src/repository/remote_cache.rs @@ -636,7 +636,11 @@ impl Repository { } } } - for ops in change.file_ops() { + let file_ops = match atomic_core::change::span_patch::localize_file_ops(&txn, &change) { + Ok(ops) => ops, + Err(error) => return Ok(Err(SubmitRejection::Malformed(error.to_string()))), + }; + for ops in &file_ops { if let Some(id) = ops .referenced_node_ids() .into_iter() diff --git a/atomic-repository/src/repository/status.rs b/atomic-repository/src/repository/status.rs index 1d90b1b2..2479ec73 100644 --- a/atomic-repository/src/repository/status.rs +++ b/atomic-repository/src/repository/status.rs @@ -452,6 +452,23 @@ impl Repository { })?; external_hashes.insert(id, hash); } + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + let span_context = if txn + .required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .map_err(|error| RepositoryError::Database(error.to_string()))? + .is_some() + { + Some( + atomic_core::pristine::span_patch_storage::SpanRenderContext::new( + &txn, + &self.change_store, + &visibility, + ) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + ) + } else { + None + }; let index_write_time = now_timestamp(); let verified_scan = verify_candidates( source_result, @@ -469,6 +486,18 @@ impl Repository { })?; // Status must compare against the bytes materialize actually // renders, including semantic resolution of live-parent forks. + if let Some(context) = &span_context { + match context.render_if_visible(item.inode) { + Ok(Some(bytes)) => return Ok(Hash::of(&bytes)), + Ok(None) => {} + Err(error) => { + return Err(ChangeSourceError::CanonicalContent { + path: path.escaped(), + message: error.to_string(), + }) + } + } + } super::materialize::render_name_conflict_side( &txn, &self.change_store, diff --git a/atomic-repository/tests/native_span_record.rs b/atomic-repository/tests/native_span_record.rs new file mode 100644 index 00000000..61cbccc9 --- /dev/null +++ b/atomic-repository/tests/native_span_record.rs @@ -0,0 +1,1577 @@ +//! Native span recording uses the ordinary record and materialization paths. + +use atomic_core::change::{ChangeHeader, GraphOp}; +use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; +use atomic_repository::{RecordOptions, Repository, StatusOptions}; + +#[test] +fn cross_view_span_insert_includes_dependencies_and_composes_independent_edits() { + use atomic_repository::apply::CrossViewInsertOptions; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"alpha\nbeta\ngamma\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + repo.switch_view(wc, "source").unwrap(); + std::fs::write(&path, b"ALPHA\nbeta\ngamma\n").unwrap(); + let first = repo + .record(wc, ChangeHeader::new("first"), options()) + .unwrap(); + std::fs::write(&path, b"ALPHA!\nbeta\ngamma\n").unwrap(); + let second = repo + .record(wc, ChangeHeader::new("second"), options()) + .unwrap(); + repo.switch_view(wc, "target").unwrap(); + std::fs::write(&path, b"alpha\nbeta\nGAMMA\n").unwrap(); + let target = repo + .record(wc, ChangeHeader::new("target"), options()) + .unwrap(); + let outcome = repo + .insert_from_view( + CrossViewInsertOptions::new("source", "target").only_changes(vec![*second.hash()]), + ) + .unwrap(); + assert!(outcome.affected_paths.contains("file.txt")); + let changes = repo.get_view_changes(Some("target")).unwrap(); + assert!(changes.iter().any(|(_, hash)| hash == first.hash())); + assert!(changes.iter().any(|(_, hash)| hash == second.hash())); + assert!(changes.iter().any(|(_, hash)| hash == target.hash())); + assert_eq!( + repo.get_file_content_on_view("file.txt", "target") + .unwrap() + .unwrap(), + b"ALPHA!\nbeta\nGAMMA\n" + ); + assert_eq!( + repo.get_file_content_on_view("file.txt", "source") + .unwrap() + .unwrap(), + b"ALPHA!\nbeta\ngamma\n" + ); + { + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "file.txt") + .unwrap(), + b"ALPHA!\nbeta\nGAMMA\n" + ); + } + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"ALPHA!\nbeta\nGAMMA\n"); + drop(repo); + let reopened = Repository::open(dir.path()).unwrap(); + assert_eq!( + reopened + .get_file_content_on_view("file.txt", "target") + .unwrap() + .unwrap(), + b"ALPHA!\nbeta\nGAMMA\n" + ); +} + +#[test] +fn overlapping_cross_view_span_insert_refuses_before_publication() { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{OperationTxnT, ViewTxnT}; + use atomic_repository::apply::CrossViewInsertOptions; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"base\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + repo.switch_view(wc, "source").unwrap(); + std::fs::write(dir.path().join("safe.txt"), b"safe\n").unwrap(); + repo.add(wc, "safe.txt", Default::default()).unwrap(); + let safe = repo + .record(wc, ChangeHeader::new("safe source change"), options()) + .unwrap(); + std::fs::write(&path, b"source\n").unwrap(); + let source = repo + .record(wc, ChangeHeader::new("source edit"), options()) + .unwrap(); + repo.switch_view(wc, "target").unwrap(); + std::fs::write(&path, b"target\n").unwrap(); + repo.record(wc, ChangeHeader::new("target edit"), options()) + .unwrap(); + let snapshot = || { + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view("target").unwrap().unwrap(); + ( + view.state, + view.change_count, + txn.list_operations().unwrap().len(), + txn.get_operation_heads(OperationScope::WorkingCopy(wc)) + .unwrap(), + ) + }; + let before = snapshot(); + let error = repo + .insert_from_view( + CrossViewInsertOptions::new("source", "target") + .only_changes(vec![*safe.hash(), *source.hash()]), + ) + .unwrap_err(); + assert!(error.to_string().contains("overlapping"), "{error}"); + assert_eq!(snapshot(), before); + assert_eq!(std::fs::read(&path).unwrap(), b"target\n"); + assert_eq!( + repo.get_file_content_on_view("file.txt", "target") + .unwrap() + .unwrap(), + b"target\n" + ); + assert!(repo + .get_file_content_on_view("safe.txt", "target") + .unwrap() + .is_none()); +} + +#[test] +fn insert_span_root_and_nested_edits_into_empty_sibling_view() { + use atomic_repository::apply::CrossViewInsertOptions; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + repo.switch_view(wc, "source").unwrap(); + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"original\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let root = repo + .record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(&path, b"edited\n").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap(); + repo.switch_view(wc, "target").unwrap(); + assert!(!path.exists()); + let result = repo + .insert_from_view( + CrossViewInsertOptions::new("source", "target").only_changes(vec![*edit.hash()]), + ) + .unwrap(); + assert!(result.affected_paths.contains("file.txt")); + let changes = repo.get_view_changes(Some("target")).unwrap(); + let hashes: Vec<_> = changes.iter().map(|(_, hash)| *hash).collect(); + assert!( + hashes.iter().position(|hash| hash == root.hash()).unwrap() + < hashes.iter().position(|hash| hash == edit.hash()).unwrap() + ); + assert_eq!( + repo.get_file_content_on_view("file.txt", "target") + .unwrap() + .unwrap(), + b"edited\n" + ); + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"edited\n"); + assert!(repo + .status(wc, StatusOptions::default()) + .unwrap() + .is_clean()); + let repeat = repo + .insert_from_view( + CrossViewInsertOptions::new("source", "target").only_changes(vec![*edit.hash()]), + ) + .unwrap(); + assert_eq!(repeat.changes_applied, 0); + assert!(repeat.skipped_hashes.contains(edit.hash())); + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert_eq!( + repo.get_file_content_on_view("file.txt", "target") + .unwrap() + .unwrap(), + b"edited\n" + ); +} + +#[test] +fn inserting_span_edit_also_inserts_its_rename_dependency() { + use atomic_repository::apply::CrossViewInsertOptions; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let old = dir.path().join("old.txt"); + let new = dir.path().join("new.txt"); + std::fs::write(&old, b"original\n").unwrap(); + repo.add(wc, "old.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + repo.switch_view(wc, "source").unwrap(); + std::fs::rename(&old, &new).unwrap(); + let moved = repo + .record(wc, ChangeHeader::new("move"), options()) + .unwrap(); + std::fs::write(&new, b"edited\n").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap(); + assert!(edit.change().dependencies().contains(moved.hash())); + repo.switch_view(wc, "target").unwrap(); + let result = repo + .insert_from_view( + CrossViewInsertOptions::new("source", "target").only_changes(vec![*edit.hash()]), + ) + .unwrap(); + assert!(result.affected_paths.contains("old.txt")); + assert!(result.affected_paths.contains("new.txt")); + assert!(repo + .get_file_content_on_view("old.txt", "target") + .unwrap() + .is_none()); + assert_eq!( + repo.get_file_content_on_view("new.txt", "target") + .unwrap() + .unwrap(), + b"edited\n" + ); + repo.materialize(wc).unwrap(); + assert!(!old.exists()); + assert_eq!(std::fs::read(&new).unwrap(), b"edited\n"); +} + +#[test] +fn inserting_span_file_deletion_retains_other_views() { + use atomic_repository::apply::CrossViewInsertOptions; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"base\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + repo.switch_view(wc, "source").unwrap(); + std::fs::write(&path, b"edited\n").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap(); + std::fs::remove_file(&path).unwrap(); + let deletion = repo + .record(wc, ChangeHeader::new("delete"), options()) + .unwrap(); + assert!(deletion.change().dependencies().contains(edit.hash())); + repo.switch_view(wc, "target").unwrap(); + let result = repo + .insert_from_view( + CrossViewInsertOptions::new("source", "target").only_changes(vec![*deletion.hash()]), + ) + .unwrap(); + assert!(result.affected_paths.contains("file.txt")); + assert!(repo + .get_file_content_on_view("file.txt", "target") + .unwrap() + .is_none()); + assert_eq!( + repo.get_file_content_on_view("file.txt", "dev") + .unwrap() + .unwrap(), + b"base\n" + ); + repo.materialize(wc).unwrap(); + assert!(!path.exists()); + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert!(repo + .get_file_content_on_view("file.txt", "target") + .unwrap() + .is_none()); + assert_eq!( + repo.get_file_content_on_view("file.txt", "dev") + .unwrap() + .unwrap(), + b"base\n" + ); +} + +#[test] +fn cross_view_insert_refuses_missing_span_index_before_journal_changes() { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{GraphTxnT, OperationTxnT, ViewTxnT}; + use atomic_repository::apply::CrossViewInsertOptions; + use redb::TableDefinition; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + repo.switch_view(wc, "source").unwrap(); + std::fs::write(dir.path().join("file.txt"), b"source\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let change = repo + .record( + wc, + ChangeHeader::new("root"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + let hash = *change.hash(); + let id = repo + .pristine() + .read_txn() + .unwrap() + .get_internal(&hash) + .unwrap() + .unwrap(); + drop(repo); + let db = redb::Database::open(dir.path().join(".atomic/atomic.redb")).unwrap(); + let txn = db.begin_write().unwrap(); + { + let mut applied = txn + .open_table(TableDefinition::::new("span_patch_applied_v1")) + .unwrap(); + assert!(applied.remove(id.get()).unwrap().is_some()); + } + txn.commit().unwrap(); + drop(db); + let repo = Repository::open(dir.path()).unwrap(); + let snapshot = || { + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view("target").unwrap().unwrap(); + ( + view.state, + view.change_count, + txn.list_operations().unwrap().len(), + txn.get_operation_heads(OperationScope::WorkingCopy(wc)) + .unwrap(), + txn.get_operation_heads(OperationScope::Repository).unwrap(), + ) + }; + let before = snapshot(); + let error = repo + .insert_from_view(CrossViewInsertOptions::new("source", "target").only_changes(vec![hash])) + .unwrap_err(); + assert!( + error.to_string().contains("span-aware repository writer"), + "{error}" + ); + assert_eq!(snapshot(), before); + assert_eq!( + std::fs::read(dir.path().join("file.txt")).unwrap(), + b"source\n" + ); +} + +#[test] +fn cross_view_insert_refuses_missing_span_edit_row() { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::span_patch_storage::SpanPatchReadTxn; + use atomic_core::pristine::{OperationTxnT, TreeTxnT, ViewTxnT}; + use atomic_repository::apply::CrossViewInsertOptions; + use redb::MultimapTableDefinition; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"base\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + repo.switch_view(wc, "source").unwrap(); + std::fs::write(&path, b"edited\n").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap(); + let hash = *edit.hash(); + let (key, value) = { + let txn = repo.pristine().read_txn().unwrap(); + let inode = txn.get_inode("file.txt").unwrap().unwrap(); + let rows = txn.span_edits_for_inode(inode).unwrap(); + assert_eq!(rows.len(), 1); + rows[0] + }; + drop(repo); + let db = redb::Database::open(dir.path().join(".atomic/atomic.redb")).unwrap(); + let txn = db.begin_write().unwrap(); + { + let mut edits = txn + .open_multimap_table(MultimapTableDefinition::<&[u8; 32], &[u8; 40]>::new( + "span_patch_edits_v1", + )) + .unwrap(); + assert!(edits.remove(&key, &value).unwrap()); + } + txn.commit().unwrap(); + drop(db); + let repo = Repository::open(dir.path()).unwrap(); + let snapshot = || { + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view("target").unwrap().unwrap(); + ( + view.state, + view.change_count, + txn.list_operations().unwrap().len(), + txn.get_operation_heads(OperationScope::WorkingCopy(wc)) + .unwrap(), + ) + }; + let before = snapshot(); + let error = repo + .insert_from_view(CrossViewInsertOptions::new("source", "target").only_changes(vec![hash])) + .unwrap_err(); + assert!(error.to_string().contains("span index differs"), "{error}"); + assert_eq!(snapshot(), before); + assert_eq!(std::fs::read(&path).unwrap(), b"edited\n"); + assert_eq!( + repo.get_file_content_on_view("file.txt", "target") + .unwrap() + .unwrap(), + b"base\n" + ); +} + +#[test] +fn span_insert_operation_can_be_undone_and_restored() { + use atomic_core::operation::{OperationKind, OperationScope}; + use atomic_core::pristine::OperationTxnT; + use atomic_repository::apply::CrossViewInsertOptions; + use atomic_repository::OperationVerificationState; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"base\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + repo.switch_view(wc, "source").unwrap(); + std::fs::write(&path, b"edited\n").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap(); + repo.switch_view(wc, "target").unwrap(); + repo.insert_from_view( + CrossViewInsertOptions::new("source", "target").only_changes(vec![*edit.hash()]), + ) + .unwrap(); + let id = { + let txn = repo.pristine().read_txn().unwrap(); + let heads = txn + .get_operation_heads(OperationScope::WorkingCopy(wc)) + .unwrap(); + assert_eq!(heads.as_slice().len(), 1); + heads.as_slice()[0] + }; + let details = repo.operation_details(id).unwrap(); + assert_eq!(details.operation.payload().kind, OperationKind::Insert); + assert_eq!(details.verification, OperationVerificationState::Verified); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + b"edited\n" + ); + repo.undo_operation(wc, None).unwrap(); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + b"base\n" + ); + assert_eq!(std::fs::read(&path).unwrap(), b"base\n"); + repo.restore_operation(wc, id).unwrap(); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + b"edited\n" + ); + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"edited\n"); +} + +#[test] +fn native_span_file_deletion_preserves_sibling_view_and_reopen() { + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"original\nbytes\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(&path, b"edited\nbytes\n").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap(); + repo.create_draft_view("deletion", "dev").unwrap(); + repo.switch_view(wc, "deletion").unwrap(); + std::fs::remove_file(&path).unwrap(); + let deletion = repo + .record(wc, ChangeHeader::new("delete"), options()) + .expect("record span deletion"); + assert!(deletion + .change() + .hunks() + .iter() + .any(|hunk| matches!(hunk, GraphOp::FileDel { .. }))); + assert!(deletion.change().dependencies().contains(edit.hash())); + assert!(repo + .get_file_content_on_view("file.txt", "deletion") + .unwrap() + .is_none()); + assert_eq!( + repo.get_file_content_on_view("file.txt", "dev") + .unwrap() + .unwrap(), + b"edited\nbytes\n" + ); + assert!(repo + .status(wc, StatusOptions::default()) + .unwrap() + .is_clean()); + repo.materialize(wc).unwrap(); + assert!(!path.exists()); + repo.switch_view(wc, "dev").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"edited\nbytes\n"); + repo.switch_view(wc, "deletion").unwrap(); + assert!(!path.exists()); + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert!(repo + .get_file_content_on_view("file.txt", "deletion") + .unwrap() + .is_none()); + assert_eq!( + repo.get_file_content_on_view("file.txt", "dev") + .unwrap() + .unwrap(), + b"edited\nbytes\n" + ); +} + +#[test] +fn native_span_rename_retains_identity_content_and_causality() { + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let old = dir.path().join("old.txt"); + let new = dir.path().join("new.txt"); + std::fs::write(&old, b"first\nsecond\n").unwrap(); + repo.add(wc, "old.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(&old, b"edited\nsecond\n").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap(); + let identity = repo.get_inode_and_position("old.txt").unwrap().unwrap(); + repo.create_draft_view("rename", "dev").unwrap(); + repo.switch_view(wc, "rename").unwrap(); + std::fs::rename(&old, &new).unwrap(); + let moved = repo + .record(wc, ChangeHeader::new("rename"), options()) + .expect("record span rename"); + assert!(moved + .change() + .hunks() + .iter() + .any(|hunk| matches!(hunk, GraphOp::FileMove { path, .. } if path == "new.txt"))); + assert!(moved.change().dependencies().contains(edit.hash())); + assert_eq!( + repo.get_inode_and_position("new.txt").unwrap(), + Some(identity) + ); + assert_eq!( + repo.get_file_content("new.txt").unwrap().unwrap(), + b"edited\nsecond\n" + ); + assert!(repo + .status(wc, StatusOptions::default()) + .unwrap() + .is_clean()); + repo.switch_view(wc, "dev").unwrap(); + assert_eq!(std::fs::read(&old).unwrap(), b"edited\nsecond\n"); + assert!(!new.exists()); + repo.switch_view(wc, "rename").unwrap(); + assert_eq!(std::fs::read(&new).unwrap(), b"edited\nsecond\n"); + std::fs::remove_file(&new).unwrap(); + let deleted = repo + .record(wc, ChangeHeader::new("delete renamed file"), options()) + .unwrap(); + assert!(deleted.change().dependencies().contains(moved.hash())); + assert!(deleted.change().dependencies().contains(edit.hash())); + assert!(repo.get_file_content("new.txt").unwrap().is_none()); + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert!(repo + .get_file_content_on_view("new.txt", "rename") + .unwrap() + .is_none()); + assert_eq!( + repo.get_file_content_on_view("old.txt", "dev") + .unwrap() + .unwrap(), + b"edited\nsecond\n" + ); +} + +#[test] +fn span_move_with_content_change_refuses_before_publication() { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{OperationTxnT, ViewTxnT}; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let old = dir.path().join("old.txt"); + let new = dir.path().join("new.txt"); + std::fs::write(&old, b"original\n").unwrap(); + repo.add(wc, "old.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::rename(&old, &new).unwrap(); + repo.move_file(wc, "old.txt", "new.txt").unwrap(); + std::fs::write(&new, b"user edits\n").unwrap(); + let observe = || { + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view(repo.current_view()).unwrap().unwrap(); + ( + view.state, + view.change_count, + txn.list_operations().unwrap().len(), + txn.get_operation_heads(OperationScope::WorkingCopy(wc)) + .unwrap(), + ) + }; + let before = observe(); + let error = repo + .record(wc, ChangeHeader::new("move and edit"), options()) + .unwrap_err(); + assert!( + error.to_string().contains("move with content changes"), + "{error}" + ); + assert_eq!(observe(), before); + assert_eq!(std::fs::read(&new).unwrap(), b"user edits\n"); + assert_eq!( + repo.get_file_content_on_view("old.txt", "dev") + .unwrap() + .unwrap(), + b"original\n" + ); +} + +#[test] +fn span_untrack_keeps_user_bytes_and_can_share_a_record_with_new_files() { + use atomic_repository::TrackingOptions; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let old = dir.path().join("old.txt"); + std::fs::write(&old, b"recorded\n").unwrap(); + repo.add(wc, "old.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(&old, b"keep user bytes\n").unwrap(); + repo.remove( + wc, + "old.txt", + TrackingOptions::default().with_keep_recorded(true), + ) + .unwrap(); + std::fs::write(dir.path().join("new.txt"), b"new file\n").unwrap(); + repo.add(wc, "new.txt", Default::default()).unwrap(); + let change = repo + .record(wc, ChangeHeader::new("untrack and add"), options()) + .unwrap(); + assert!(change + .change() + .hunks() + .iter() + .any(|hunk| matches!(hunk, GraphOp::FileDel { path, .. } if path == "old.txt"))); + assert!(change.change().hashed.span.is_some()); + assert!(repo.get_file_content("old.txt").unwrap().is_none()); + assert_eq!(std::fs::read(&old).unwrap(), b"keep user bytes\n"); + assert_eq!( + repo.get_file_content("new.txt").unwrap().unwrap(), + b"new file\n" + ); + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert!(repo.get_file_content("old.txt").unwrap().is_none()); + assert_eq!(std::fs::read(&old).unwrap(), b"keep user bytes\n"); + assert_eq!( + repo.get_file_content("new.txt").unwrap().unwrap(), + b"new file\n" + ); +} + +#[test] +fn native_recorded_file_renders_and_reopens() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + // The capability identifies the new representation. Initializing new + // repositories with it will be enabled once native edits are supported. + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + let bytes = b"alpha\nbeta\nomega"; + std::fs::write(&path, bytes).unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let outcome = repo + .record( + wc, + ChangeHeader::new("record file"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .expect("native span record"); + let patch = outcome + .change() + .hashed + .span + .as_ref() + .expect("span operations"); + assert_eq!(patch.ops.len(), 1); + assert!(outcome + .change() + .hunks() + .iter() + .any(|hunk| matches!(hunk, GraphOp::FileAdd { contents: None, .. }))); + assert!(!outcome + .change() + .hunks() + .iter() + .any(|hunk| matches!(hunk, GraphOp::Edit { .. }))); + assert_eq!(repo.get_file_content("file.txt").unwrap().unwrap(), bytes); + { + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "file.txt") + .unwrap(), + bytes + ); + } + assert!(repo + .status(wc, StatusOptions::default()) + .unwrap() + .is_clean()); + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), bytes); + drop(repo); + let reopened = Repository::open(dir.path()).unwrap(); + assert_eq!( + reopened.get_file_content("file.txt").unwrap().unwrap(), + bytes + ); +} + +#[test] +fn native_recorded_line_edit_uses_an_authored_range() { + use atomic_core::change::span_patch::SpanOp; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"alpha\nbeta\nomega").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let root = repo + .record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(&path, b"ALPHA\nbeta\nomega").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("edit first line"), options()) + .expect("record span edit"); + let patch = edit.change().hashed.span.as_ref().expect("span operations"); + assert!( + matches!(&patch.ops[..], [SpanOp::Edit { target, start: 0, end: 6, .. }] if target.owner == *root.hash()) + ); + assert_eq!(edit.change().contents, b"ALPHA\n"); + assert!(!edit.change().file_ops().is_empty()); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + b"ALPHA\nbeta\nomega" + ); + { + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "file.txt") + .unwrap(), + b"ALPHA\nbeta\nomega" + ); + } + assert!(repo + .status(wc, StatusOptions::default()) + .unwrap() + .is_clean()); + std::fs::write(&path, b"ALPHA\nBETA\nomega").unwrap(); + let second = repo + .record(wc, ChangeHeader::new("edit another line"), options()) + .expect("record second span edit"); + assert!(second.change().dependencies().contains(edit.hash())); + assert_eq!(second.change().contents, b"BETA\n"); + std::fs::write(&path, b"Alpha again\nBETA\nomega").unwrap(); + let third = repo + .record( + wc, + ChangeHeader::new("edit previously changed line"), + options(), + ) + .expect("record nested span edit"); + assert!(third.change().dependencies().contains(second.hash())); + assert!( + matches!(&third.change().hashed.span.as_ref().unwrap().ops[..], [SpanOp::Edit { target, .. }] if target.owner == *edit.hash()) + ); + assert_eq!(third.change().contents, b"Alpha again\n"); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + b"Alpha again\nBETA\nomega" + ); + { + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "file.txt") + .unwrap(), + b"Alpha again\nBETA\nomega" + ); + } + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"Alpha again\nBETA\nomega"); + drop(repo); + let reopened = Repository::open(dir.path()).unwrap(); + assert_eq!( + reopened.get_file_content("file.txt").unwrap().unwrap(), + b"Alpha again\nBETA\nomega" + ); +} + +#[test] +fn one_record_preserves_distinct_span_and_semantic_offsets_for_two_files() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + for (path, bytes) in [("a.txt", b"alpha\nend".as_slice()), ("b.txt", b"beta\nend")] { + std::fs::write(dir.path().join(path), bytes).unwrap(); + repo.add(wc, path, Default::default()).unwrap(); + } + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("roots"), options()) + .unwrap(); + std::fs::write(dir.path().join("a.txt"), b"ALPHA\nend").unwrap(); + std::fs::write(dir.path().join("b.txt"), b"BETA\nend").unwrap(); + let edit = repo + .record(wc, ChangeHeader::new("two edits"), options()) + .unwrap(); + assert_eq!(edit.change().hashed.span.as_ref().unwrap().ops.len(), 2); + assert_eq!(edit.change().contents.len(), b"ALPHA\nBETA\n".len()); + for (path, expected) in [("a.txt", b"ALPHA\nend".as_slice()), ("b.txt", b"BETA\nend")] { + assert_eq!(repo.get_file_content(path).unwrap().unwrap(), expected); + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), path) + .unwrap(), + expected + ); + drop(txn); + std::fs::remove_file(dir.path().join(path)).unwrap(); + } + repo.materialize(wc).unwrap(); + assert_eq!( + std::fs::read(dir.path().join("a.txt")).unwrap(), + b"ALPHA\nend" + ); + assert_eq!( + std::fs::read(dir.path().join("b.txt")).unwrap(), + b"BETA\nend" + ); +} + +#[cfg(unix)] +#[test] +fn native_span_mode_change_preserves_content_and_materializes_mode() { + use std::os::unix::fs::PermissionsExt; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"alpha\nomega").unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + repo.record(wc, ChangeHeader::new("mode edit"), options()) + .unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"alpha\nomega"); + assert_eq!( + std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, + 0o755 + ); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + b"alpha\nomega" + ); + std::fs::write(&path, b"ALPHA\nomega").unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); + let combined = repo + .record(wc, ChangeHeader::new("content and mode"), options()) + .unwrap(); + assert!(combined.change().hashed.span.is_some()); + assert!(combined + .change() + .hunks() + .iter() + .any(|hunk| matches!(hunk, GraphOp::SetAttr { .. }))); + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"ALPHA\nomega"); + assert_eq!( + std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, + 0o644 + ); +} + +#[test] +fn native_line_insertion_and_deletion_roundtrip() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"alpha\nbeta\nomega").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + for (message, bytes) in [ + ("insert", b"alpha\nnew line\nbeta\nomega".as_slice()), + ("delete", b"alpha\nnew line\nomega".as_slice()), + ("append", b"alpha\nnew line\nomega\nlast".as_slice()), + ] { + std::fs::write(&path, bytes).unwrap(); + repo.record(wc, ChangeHeader::new(message), options()) + .unwrap_or_else(|error| panic!("{message}: {error}")); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + bytes, + "{message}" + ); + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "file.txt") + .unwrap(), + bytes, + "{message}" + ); + drop(txn); + assert!( + repo.status(wc, StatusOptions::default()) + .unwrap() + .is_clean(), + "{message}" + ); + } + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!( + std::fs::read(&path).unwrap(), + b"alpha\nnew line\nomega\nlast" + ); +} + +#[test] +fn native_text_edits_preserve_bytes_across_common_sequences() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"alpha\nbeta\ngamma\nomega").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + for (message, bytes) in [ + ("prepend", b"head\nalpha\nbeta\ngamma\nomega".as_slice()), + ( + "disjoint changes", + b"HEAD\nalpha\nbeta\nGAMMA\nomega".as_slice(), + ), + ("delete middle", b"HEAD\nomega".as_slice()), + ("append", b"HEAD\nomega\nlast".as_slice()), + ("empty", b"".as_slice()), + ("refill", b"refilled\nvalue".as_slice()), + ] { + std::fs::write(&path, bytes).unwrap(); + repo.record(wc, ChangeHeader::new(message), options()) + .unwrap_or_else(|error| panic!("{message}: {error}")); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + bytes, + "{message}" + ); + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "file.txt") + .unwrap(), + bytes, + "{message}" + ); + drop(txn); + assert!( + repo.status(wc, StatusOptions::default()) + .unwrap() + .is_clean(), + "{message}" + ); + } + drop(repo); + let reopened = Repository::open(dir.path()).unwrap(); + assert_eq!( + reopened.get_file_content("file.txt").unwrap().unwrap(), + b"refilled\nvalue" + ); +} + +#[test] +fn native_record_mixes_existing_span_edits_with_new_files() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + std::fs::create_dir(dir.path().join("dir")).unwrap(); + std::fs::write(dir.path().join("dir/old.txt"), b"old\nend").unwrap(); + repo.add(wc, "dir/old.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(dir.path().join("dir/old.txt"), b"EDITED\nend").unwrap(); + std::fs::write(dir.path().join("dir/new.txt"), b"new\nfile").unwrap(); + repo.add(wc, "dir/new.txt", Default::default()).unwrap(); + let outcome = repo + .record(wc, ChangeHeader::new("mixed"), options()) + .unwrap(); + assert_eq!(outcome.change().hashed.span.as_ref().unwrap().ops.len(), 2); + for (path, expected) in [ + ("dir/old.txt", b"EDITED\nend".as_slice()), + ("dir/new.txt", b"new\nfile"), + ] { + assert_eq!(repo.get_file_content(path).unwrap().unwrap(), expected); + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), path) + .unwrap(), + expected + ); + } +} + +#[test] +fn native_empty_and_binary_roots_materialize_exactly() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + for (path, bytes) in [ + ("empty.txt", b"".as_slice()), + ("data.bin", b"\0\x01\xff\nbytes"), + ] { + std::fs::write(dir.path().join(path), bytes).unwrap(); + repo.add(wc, path, Default::default()).unwrap(); + } + let options = RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true); + let outcome = repo + .record(wc, ChangeHeader::new("roots"), options) + .unwrap(); + assert_eq!(outcome.change().hashed.span.as_ref().unwrap().ops.len(), 2); + for (path, bytes) in [ + ("empty.txt", b"".as_slice()), + ("data.bin", b"\0\x01\xff\nbytes"), + ] { + assert_eq!( + repo.get_file_content(path).unwrap().unwrap(), + bytes, + "{path}" + ); + std::fs::remove_file(dir.path().join(path)).unwrap(); + } + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(dir.path().join("empty.txt")).unwrap(), b""); + assert_eq!( + std::fs::read(dir.path().join("data.bin")).unwrap(), + b"\0\x01\xff\nbytes" + ); +} + +#[test] +fn native_binary_edit_records_and_materializes_exact_bytes() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("data.bin"); + std::fs::write(&path, b"\0\x01\xff\nbytes").unwrap(); + repo.add(wc, "data.bin", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("binary root"), options()) + .unwrap(); + std::fs::write(&path, b"\0\x02\xff\nnew bytes").unwrap(); + let first = repo + .record(wc, ChangeHeader::new("binary edit"), options()) + .expect("record binary edit"); + assert_eq!( + repo.get_file_content("data.bin").unwrap().unwrap(), + b"\0\x02\xff\nnew bytes" + ); + assert!(repo + .status(wc, StatusOptions::default()) + .unwrap() + .is_clean()); + std::fs::write(&path, b"\0\x03\xfe\nfinal").unwrap(); + let second = repo + .record(wc, ChangeHeader::new("second binary edit"), options()) + .unwrap(); + assert!(second.change().dependencies().contains(first.hash())); + assert_eq!( + repo.get_file_content("data.bin").unwrap().unwrap(), + b"\0\x03\xfe\nfinal" + ); + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"\0\x03\xfe\nfinal"); +} + +#[test] +fn native_text_binary_transitions_keep_content_and_semantics() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"first\nsecond").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("text root"), options()) + .unwrap(); + std::fs::write(&path, b"\0binary\xff").unwrap(); + repo.record(wc, ChangeHeader::new("to binary"), options()) + .unwrap(); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + b"\0binary\xff" + ); + { + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "file.txt") + .unwrap(), + b"" + ); + } + std::fs::write(&path, b"new\ntext").unwrap(); + repo.record(wc, ChangeHeader::new("back to text"), options()) + .unwrap(); + assert_eq!( + repo.get_file_content("file.txt").unwrap().unwrap(), + b"new\ntext" + ); + { + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "file.txt") + .unwrap(), + b"new\ntext" + ); + } + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"new\ntext"); +} + +#[test] +fn missing_span_root_refuses_without_overwriting_working_bytes() { + use atomic_core::pristine::TreeTxnT; + use redb::TableDefinition; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"recorded\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + repo.record( + wc, + ChangeHeader::new("root"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + let inode = repo + .pristine() + .read_txn() + .unwrap() + .get_inode("file.txt") + .unwrap() + .unwrap(); + drop(repo); + let db = redb::Database::open(dir.path().join(".atomic/atomic.redb")).unwrap(); + let txn = db.begin_write().unwrap(); + { + let mut roots = txn + .open_table(TableDefinition::::new( + "span_patch_roots_v1", + )) + .unwrap(); + assert!(roots.remove(inode.get()).unwrap().is_some()); + } + txn.commit().unwrap(); + drop(db); + let repo = Repository::open(dir.path()).unwrap(); + let error = repo.get_file_content("file.txt").unwrap_err(); + assert!(error.to_string().contains("span root missing"), "{error}"); + std::fs::write(&path, b"user work\n").unwrap(); + let error = repo.materialize(wc).unwrap_err(); + assert!(error.to_string().contains("span root missing"), "{error}"); + assert_eq!(std::fs::read(&path).unwrap(), b"user work\n"); +} + +#[test] +fn native_span_recording_keeps_sibling_views_isolated() { + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("file.txt"); + std::fs::write(&path, b"alpha\nbeta\ngamma\nomega").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let root = repo + .record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + repo.create_draft_view("left", "dev").unwrap(); + repo.create_draft_view("right", "dev").unwrap(); + repo.switch_view(wc, "left").unwrap(); + std::fs::write(&path, b"ALPHA\nbeta\ngamma\nomega").unwrap(); + let left = repo + .record(wc, ChangeHeader::new("left"), options()) + .unwrap(); + repo.switch_view(wc, "right").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"alpha\nbeta\ngamma\nomega"); + std::fs::write(&path, b"alpha\nbeta\nGAMMA\nomega").unwrap(); + let right = repo + .record(wc, ChangeHeader::new("right"), options()) + .unwrap(); + assert!(left.change().dependencies().contains(root.hash())); + assert!(right.change().dependencies().contains(root.hash())); + assert!(!right.change().dependencies().contains(left.hash())); + assert_eq!( + repo.get_file_content_on_view("file.txt", "left") + .unwrap() + .unwrap(), + b"ALPHA\nbeta\ngamma\nomega" + ); + assert_eq!( + repo.get_file_content_on_view("file.txt", "right") + .unwrap() + .unwrap(), + b"alpha\nbeta\nGAMMA\nomega" + ); + repo.switch_view(wc, "left").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"ALPHA\nbeta\ngamma\nomega"); + assert!(repo + .status(wc, StatusOptions::default()) + .unwrap() + .is_clean()); +} + +#[cfg(unix)] +#[test] +fn native_span_recording_preserves_symlink_target() { + use std::os::unix::fs::symlink; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("link"); + symlink("missing-target", &path).unwrap(); + repo.add(wc, "link", Default::default()).unwrap(); + repo.record( + wc, + ChangeHeader::new("record link"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + assert_eq!( + repo.get_file_content("link").unwrap().unwrap(), + b"missing-target" + ); + std::fs::remove_file(&path).unwrap(); + symlink("different-target", &path).unwrap(); + repo.record( + wc, + ChangeHeader::new("change link"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + assert_eq!( + repo.get_file_content("link").unwrap().unwrap(), + b"different-target" + ); + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!( + std::fs::read_link(&path).unwrap(), + std::path::Path::new("different-target") + ); +} diff --git a/atomic-repository/tests/span_insert.rs b/atomic-repository/tests/span_insert.rs new file mode 100644 index 00000000..a7a886a1 --- /dev/null +++ b/atomic-repository/tests/span_insert.rs @@ -0,0 +1,788 @@ +//! Incoming span changes must be indexed atomically and invalid closures refused. + +use atomic_core::change::span_patch::{SpanOp, SpanPatch}; +use atomic_core::change::{Change, ChangeHeader}; +use atomic_core::operation::OperationScope; +use atomic_core::pristine::{ + CapabilityMutTxnT, MutTxnT, OperationTxnT, ViewTxnT, SPAN_RANGE_CAPABILITY, +}; +use atomic_core::types::{Hash, Merkle, OperationId}; +use atomic_repository::apply::{CrossViewInsertOptions, InsertOptions}; +use atomic_repository::{RecordOptions, Repository, StatusOptions}; + +#[test] +fn incoming_span_root_and_edits_are_indexed_on_insert() { + use atomic_core::pristine::{span_patch_storage::SpanPatchReadTxn, GraphTxnT}; + let source_dir = tempfile::tempdir().unwrap(); + let source = Repository::init(source_dir.path()).unwrap(); + let source_wc = source.require_working_copy_id().unwrap(); + { + let mut txn = source.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = source_dir.path().join("file.txt"); + std::fs::write(&path, b"one\ntwo\n").unwrap(); + source + .add(source_wc, "file.txt", Default::default()) + .unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let root = source + .record(source_wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(&path, b"ONE\ntwo\n").unwrap(); + let edit = source + .record(source_wc, ChangeHeader::new("edit"), options()) + .unwrap(); + std::fs::write(&path, b"ONE!\ntwo\n").unwrap(); + let nested = source + .record(source_wc, ChangeHeader::new("nested"), options()) + .unwrap(); + let target_dir = tempfile::tempdir().unwrap(); + let mut target = Repository::init(target_dir.path()).unwrap(); + let target_wc = target.require_working_copy_id().unwrap(); + target.create_draft_view("target", "dev").unwrap(); + { + let mut txn = target.pristine().write_txn().unwrap(); + // Force different repository-local allocation without adding view history. + let id = txn + .register_change(&Hash::of(b"receiver-local unrelated change")) + .unwrap(); + txn.put_change_deps(id, &[]).unwrap(); + txn.commit().unwrap(); + } + let mut rejected_bytes = Vec::new(); + root.change().serialize(&mut rejected_bytes).unwrap(); + let before_invalid = snapshot(&target); + assert!(target + .import_change_bytes(&Hash::of(b"wrong hash"), &rejected_bytes) + .is_err()); + assert_eq!(snapshot(&target), before_invalid); + { + use atomic_core::pristine::CapabilityTxnT; + assert_eq!( + target + .pristine() + .read_txn() + .unwrap() + .required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .unwrap(), + None + ); + } + for outcome in [&root, &edit, &nested] { + let mut bytes = Vec::new(); + assert_eq!( + outcome.change().serialize(&mut bytes).unwrap(), + *outcome.hash() + ); + assert_eq!( + target.import_change_bytes(outcome.hash(), &bytes).unwrap(), + *outcome.hash() + ); + } + { + use atomic_core::pristine::CapabilityTxnT; + let txn = target.pristine().read_txn().unwrap(); + assert_eq!( + txn.required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .unwrap(), + Some(SPAN_RANGE_CAPABILITY.minimum_version()) + ); + assert!(txn.get_internal(root.hash()).unwrap().is_none()); + } + let before = snapshot(&target); + let error = target + .insert_change(nested.hash(), InsertOptions::default().view("target")) + .unwrap_err(); + assert!( + error.to_string().contains("missing applied dependency"), + "{error}" + ); + assert_eq!(snapshot(&target), before); + assert!(target + .pristine() + .read_txn() + .unwrap() + .get_internal(root.hash()) + .unwrap() + .is_none()); + let result = target + .insert_change_rec(nested.hash(), InsertOptions::default().view("target")) + .unwrap(); + assert!(result.stats.affected_paths.contains("file.txt")); + { + let txn = target.pristine().read_txn().unwrap(); + let source_txn = source.pristine().read_txn().unwrap(); + assert_ne!( + txn.get_internal(root.hash()).unwrap(), + source_txn.get_internal(root.hash()).unwrap(), + "test must exercise different repository-local IDs" + ); + for outcome in [&root, &edit, &nested] { + let id = txn.get_internal(outcome.hash()).unwrap().unwrap(); + assert!(txn.was_change_applied(id).unwrap()); + assert!(txn.span_change_indexed(id).unwrap()); + } + } + assert_eq!( + target + .get_file_content_on_view("file.txt", "target") + .unwrap() + .unwrap(), + b"ONE!\ntwo\n" + ); + target.switch_view(target_wc, "target").unwrap(); + assert_eq!( + std::fs::read(target_dir.path().join("file.txt")).unwrap(), + b"ONE!\ntwo\n" + ); + assert!(target + .status(target_wc, StatusOptions::default()) + .unwrap() + .is_clean()); + { + let txn = target.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt( + &txn, + target.change_store(), + "file.txt" + ) + .unwrap(), + b"ONE!\ntwo\n" + ); + } + std::fs::write(target_dir.path().join("file.txt"), b"ONE!\nTWO\n").unwrap(); + target + .record( + target_wc, + ChangeHeader::new("local edit after import"), + options(), + ) + .expect("record after importing semantic identities"); + assert_eq!( + target.get_file_content("file.txt").unwrap().unwrap(), + b"ONE!\nTWO\n" + ); + { + let txn = target.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt( + &txn, + target.change_store(), + "file.txt" + ) + .unwrap(), + b"ONE!\nTWO\n" + ); + } + drop(target); + let target = Repository::open(target_dir.path()).unwrap(); + assert_eq!( + target + .get_file_content_on_view("file.txt", "target") + .unwrap() + .unwrap(), + b"ONE!\nTWO\n" + ); +} + +#[test] +fn legacy_incoming_semantic_ids_are_refused_before_any_dependency_is_published() { + use atomic_core::pristine::GraphTxnT; + let source_dir = tempfile::tempdir().unwrap(); + let source = Repository::init(source_dir.path()).unwrap(); + let wc = source.require_working_copy_id().unwrap(); + { + let mut txn = source.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + std::fs::write(source_dir.path().join("file.txt"), b"base\n").unwrap(); + source.add(wc, "file.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let root = source + .record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(source_dir.path().join("file.txt"), b"edited\n").unwrap(); + let edit = source + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap(); + assert!(!edit + .change() + .hashed + .span + .as_ref() + .unwrap() + .semantic_owners + .is_empty()); + let mut legacy = edit.change().clone(); + let patch = legacy.hashed.span.as_mut().unwrap(); + patch.version = 1; + patch.semantic_owners.clear(); + let target_dir = tempfile::tempdir().unwrap(); + let mut target = Repository::init(target_dir.path()).unwrap(); + target.create_draft_view("target", "dev").unwrap(); + { + let mut txn = target.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + assert_eq!(target.save_change(root.change()).unwrap(), *root.hash()); + let legacy_hash = target.save_change(&legacy).unwrap(); + let before = snapshot(&target); + let error = target + .insert_change_rec(&legacy_hash, InsertOptions::default().view("target")) + .unwrap_err(); + assert!( + error + .to_string() + .contains("complete portable owner mapping"), + "{error}" + ); + assert_eq!(snapshot(&target), before); + assert!(target + .pristine() + .read_txn() + .unwrap() + .get_internal(root.hash()) + .unwrap() + .is_none()); + assert!(!target_dir.path().join("file.txt").exists()); +} + +#[test] +fn legacy_span_capability_remains_readable_but_recording_requires_migration() { + let source_dir = tempfile::tempdir().unwrap(); + let source = Repository::init(source_dir.path()).unwrap(); + let source_wc = source.require_working_copy_id().unwrap(); + { + let mut txn = source.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + std::fs::write(source_dir.path().join("file.txt"), b"legacy\n").unwrap(); + source + .add(source_wc, "file.txt", Default::default()) + .unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let root = source + .record(source_wc, ChangeHeader::new("root"), options()) + .unwrap(); + let mut legacy = root.change().clone(); + legacy.hashed.span.as_mut().unwrap().version = 1; + let target_dir = tempfile::tempdir().unwrap(); + let mut target = Repository::init(target_dir.path()).unwrap(); + let wc = target.require_working_copy_id().unwrap(); + target.create_draft_view("target", "dev").unwrap(); + { + let mut txn = target.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let hash = target.save_change(&legacy).unwrap(); + target + .insert_change(&hash, InsertOptions::default().view("target")) + .unwrap(); + target.switch_view(wc, "target").unwrap(); + { + let mut txn = target.pristine().write_txn().unwrap(); + txn.put_required_capability_exact(SPAN_RANGE_CAPABILITY.id(), 2) + .unwrap(); + txn.commit().unwrap(); + } + drop(target); + let target = Repository::open(target_dir.path()).unwrap(); + assert_eq!( + target.get_file_content("file.txt").unwrap().unwrap(), + b"legacy\n" + ); + let mut new_bytes = Vec::new(); + root.change().serialize(&mut new_bytes).unwrap(); + let before_import = snapshot(&target); + let import_error = target + .import_change_bytes(root.hash(), &new_bytes) + .unwrap_err(); + assert!( + import_error.to_string().contains("requires migration"), + "{import_error}" + ); + assert_eq!(snapshot(&target), before_import); + assert!(!target.has_change(root.hash())); + let path = target_dir.path().join("file.txt"); + std::fs::write(&path, b"user edit\n").unwrap(); + let before = snapshot(&target); + let error = target + .record(wc, ChangeHeader::new("edit"), options()) + .unwrap_err(); + assert!(error.to_string().contains("requires migration"), "{error}"); + assert_eq!(snapshot(&target), before); + assert_eq!(std::fs::read(path).unwrap(), b"user edit\n"); +} + +#[test] +fn incoming_span_name_conflict_refuses_before_publication() { + use atomic_core::pristine::GraphTxnT; + let source_dir = tempfile::tempdir().unwrap(); + let source = Repository::init(source_dir.path()).unwrap(); + let source_wc = source.require_working_copy_id().unwrap(); + { + let mut txn = source.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + std::fs::write(source_dir.path().join("file.txt"), b"source\n").unwrap(); + source + .add(source_wc, "file.txt", Default::default()) + .unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let incoming = source + .record(source_wc, ChangeHeader::new("source root"), options()) + .unwrap(); + let target_dir = tempfile::tempdir().unwrap(); + let mut target = Repository::init(target_dir.path()).unwrap(); + let target_wc = target.require_working_copy_id().unwrap(); + target.create_draft_view("target", "dev").unwrap(); + { + let mut txn = target.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + target.switch_view(target_wc, "target").unwrap(); + std::fs::write(target_dir.path().join("file.txt"), b"target\n").unwrap(); + target + .add(target_wc, "file.txt", Default::default()) + .unwrap(); + target + .record(target_wc, ChangeHeader::new("target root"), options()) + .unwrap(); + assert_eq!( + target.save_change(incoming.change()).unwrap(), + *incoming.hash() + ); + let before = snapshot(&target); + let error = target + .insert_change(incoming.hash(), InsertOptions::default().view("target")) + .unwrap_err(); + assert!( + error.to_string().contains("span-backed name conflict"), + "{error}" + ); + assert_eq!(snapshot(&target), before); + assert!(target + .pristine() + .read_txn() + .unwrap() + .get_internal(incoming.hash()) + .unwrap() + .is_none()); + assert_eq!( + std::fs::read(target_dir.path().join("file.txt")).unwrap(), + b"target\n" + ); + assert_eq!( + target.get_file_content("file.txt").unwrap().unwrap(), + b"target\n" + ); +} + +#[test] +fn importing_spans_into_legacy_repository_preserves_legacy_edits() { + let source_dir = tempfile::tempdir().unwrap(); + let source = Repository::init(source_dir.path()).unwrap(); + let source_wc = source.require_working_copy_id().unwrap(); + { + let mut txn = source.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + std::fs::write(source_dir.path().join("remote.txt"), b"remote\n").unwrap(); + source + .add(source_wc, "remote.txt", Default::default()) + .unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let incoming = source + .record(source_wc, ChangeHeader::new("incoming"), options()) + .unwrap(); + let target_dir = tempfile::tempdir().unwrap(); + let mut target = Repository::init(target_dir.path()).unwrap(); + let wc = target.require_working_copy_id().unwrap(); + std::fs::write(target_dir.path().join("legacy.txt"), b"before\n").unwrap(); + target.add(wc, "legacy.txt", Default::default()).unwrap(); + let legacy = target + .record(wc, ChangeHeader::new("legacy root"), options()) + .unwrap(); + assert!(legacy.change().hashed.span.is_none()); + target.create_draft_view("target", "dev").unwrap(); + target.switch_view(wc, "target").unwrap(); + let mut bytes = Vec::new(); + incoming.change().serialize(&mut bytes).unwrap(); + target.import_change_bytes(incoming.hash(), &bytes).unwrap(); + target + .insert_change(incoming.hash(), InsertOptions::default().view("target")) + .unwrap(); + target.materialize(wc).unwrap(); + std::fs::write(target_dir.path().join("legacy.txt"), b"after\n").unwrap(); + std::fs::write(target_dir.path().join("new.txt"), b"new span\n").unwrap(); + target.add(wc, "new.txt", Default::default()).unwrap(); + let mixed = target + .record(wc, ChangeHeader::new("legacy edit and span add"), options()) + .unwrap(); + assert!(mixed.change().hashed.span.is_some()); + assert_eq!( + target.get_file_content("legacy.txt").unwrap().unwrap(), + b"after\n" + ); + assert_eq!( + target.get_file_content("new.txt").unwrap().unwrap(), + b"new span\n" + ); + assert_eq!( + target.get_file_content("remote.txt").unwrap().unwrap(), + b"remote\n" + ); + for (name, expected) in [ + ("legacy.txt", b"after\n".as_slice()), + ("new.txt", b"new span\n"), + ] { + let txn = target.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, target.change_store(), name) + .unwrap(), + expected + ); + } + drop(target); + let target = Repository::open(target_dir.path()).unwrap(); + assert_eq!( + target.get_file_content("legacy.txt").unwrap().unwrap(), + b"after\n" + ); + assert_eq!( + target.get_file_content("new.txt").unwrap().unwrap(), + b"new span\n" + ); +} + +#[test] +fn incoming_span_move_and_deletion_preserve_semantic_identity() { + use atomic_core::pristine::{CrdtTxnT, TreeTxnT}; + let source_dir = tempfile::tempdir().unwrap(); + let source = Repository::init(source_dir.path()).unwrap(); + let source_wc = source.require_working_copy_id().unwrap(); + { + let mut txn = source.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let old = source_dir.path().join("old.txt"); + let new = source_dir.path().join("new.txt"); + std::fs::write(&old, b"base\n").unwrap(); + source + .add(source_wc, "old.txt", Default::default()) + .unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let root = source + .record(source_wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::rename(&old, &new).unwrap(); + let moved = source + .record(source_wc, ChangeHeader::new("move"), options()) + .unwrap(); + std::fs::write(&new, b"edited\n").unwrap(); + let edited = source + .record(source_wc, ChangeHeader::new("edit"), options()) + .unwrap(); + std::fs::remove_file(&new).unwrap(); + let deleted = source + .record(source_wc, ChangeHeader::new("delete"), options()) + .unwrap(); + let target_dir = tempfile::tempdir().unwrap(); + let mut target = Repository::init(target_dir.path()).unwrap(); + let target_wc = target.require_working_copy_id().unwrap(); + target.create_draft_view("target", "dev").unwrap(); + { + let mut txn = target.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + for outcome in [&root, &moved, &edited, &deleted] { + assert_eq!( + target.save_change(outcome.change()).unwrap(), + *outcome.hash() + ); + } + target + .insert_change_rec(edited.hash(), InsertOptions::default().view("target")) + .unwrap(); + assert!(target + .get_file_content_on_view("old.txt", "target") + .unwrap() + .is_none()); + assert_eq!( + target + .get_file_content_on_view("new.txt", "target") + .unwrap() + .unwrap(), + b"edited\n" + ); + target.switch_view(target_wc, "target").unwrap(); + let trunk_key = { + let txn = target.pristine().read_txn().unwrap(); + let inode = txn.get_inode("new.txt").unwrap().unwrap(); + txn.get_crdt_inode_trunk(inode.get()).unwrap().unwrap() + }; + { + let txn = target.pristine().read_txn().unwrap(); + assert_eq!( + atomic_core::output::crdt::output_file_via_crdt(&txn, target.change_store(), "new.txt") + .unwrap(), + b"edited\n" + ); + } + target + .insert_change_rec(deleted.hash(), InsertOptions::default().view("target")) + .unwrap(); + assert_eq!( + target + .pristine() + .read_txn() + .unwrap() + .get_crdt_trunk(&trunk_key) + .unwrap() + .unwrap() + .state, + atomic_core::crdt::TrunkState::Deleted + ); + assert!(target.get_file_content("new.txt").unwrap().is_none()); + target.materialize(target_wc).unwrap(); + assert!(!target_dir.path().join("new.txt").exists()); + drop(target); + let target = Repository::open(target_dir.path()).unwrap(); + assert!(target + .get_file_content_on_view("new.txt", "target") + .unwrap() + .is_none()); +} + +#[test] +fn incoming_span_edit_rejects_semantic_trunk_from_another_file() { + use atomic_core::change::FileOps; + use atomic_core::pristine::{CrdtTxnT, GraphTxnT, TreeTxnT}; + let source_dir = tempfile::tempdir().unwrap(); + let source = Repository::init(source_dir.path()).unwrap(); + let wc = source.require_working_copy_id().unwrap(); + { + let mut txn = source.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + for name in ["a.txt", "b.txt"] { + std::fs::write(source_dir.path().join(name), b"base\n").unwrap(); + source.add(wc, name, Default::default()).unwrap(); + } + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let root = source + .record(wc, ChangeHeader::new("root"), options()) + .unwrap(); + std::fs::write(source_dir.path().join("a.txt"), b"edited\n").unwrap(); + let edit = source + .record(wc, ChangeHeader::new("edit a"), options()) + .unwrap(); + let b_trunk = { + let txn = source.pristine().read_txn().unwrap(); + let inode = txn.get_inode("b.txt").unwrap().unwrap(); + atomic_core::crdt::TrunkId::from_bytes( + &txn.get_crdt_inode_trunk(inode.get()).unwrap().unwrap(), + ) + }; + let mut forged = edit.change().clone(); + let mut wrong = FileOps::edit(b_trunk, "a.txt".into()); + for line in edit.change().file_ops()[0].line_ops() { + wrong.add_line_op(line.clone()); + } + forged.hashed.file_ops = vec![wrong]; + let target_dir = tempfile::tempdir().unwrap(); + let mut target = Repository::init(target_dir.path()).unwrap(); + target.create_draft_view("target", "dev").unwrap(); + { + let mut txn = target.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + assert_eq!(target.save_change(root.change()).unwrap(), *root.hash()); + let forged_hash = target.save_change(&forged).unwrap(); + let before = snapshot(&target); + let error = target + .insert_change_rec(&forged_hash, InsertOptions::default().view("target")) + .unwrap_err(); + assert!( + error.to_string().contains("no matching semantic trunk"), + "{error}" + ); + assert_eq!(snapshot(&target), before); + assert!(target + .pristine() + .read_txn() + .unwrap() + .get_internal(root.hash()) + .unwrap() + .is_none()); +} + +fn snapshot( + repo: &Repository, +) -> ( + Merkle, + u64, + Vec, + Vec, + Vec, + usize, +) { + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view("target").unwrap().unwrap(); + let mut operations: Vec<_> = txn + .list_operations() + .unwrap() + .iter() + .map(|op| op.id()) + .collect(); + operations.sort(); + let receipts = operations + .iter() + .map(|id| txn.get_effect_receipts(*id).unwrap().len()) + .sum(); + let mut heads = txn + .get_operation_heads(OperationScope::Repository) + .unwrap() + .as_slice() + .to_vec(); + heads.sort(); + let mut wc_heads = txn + .get_operation_heads(OperationScope::WorkingCopy( + repo.require_working_copy_id().unwrap(), + )) + .unwrap() + .as_slice() + .to_vec(); + wc_heads.sort(); + ( + view.state, + view.change_count, + operations, + heads, + wc_heads, + receipts, + ) +} + +fn fixture() -> (tempfile::TempDir, Repository, Hash, Hash) { + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + repo.create_draft_view("source", "dev").unwrap(); + repo.create_draft_view("target", "dev").unwrap(); + let normal = Change::new(ChangeHeader::new("ordinary"), vec![], vec![], vec![]); + let normal = repo.save_change(&normal).unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let mut change = Change::new(ChangeHeader::new("span"), vec![], b"x\n".to_vec(), vec![]); + change.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "x".into(), + ordinal: 0, + start: 0, + end: 2, + }], + )); + let span = repo.save_change(&change).unwrap(); + (dir, repo, normal, span) +} + +#[test] +fn direct_insert_does_not_publish_span_without_namespace_creation() { + let (_dir, repo, _, span) = fixture(); + let before = snapshot(&repo); + let error = repo + .insert_change(&span, InsertOptions::default().view("target")) + .unwrap_err(); + assert!( + error.to_string().contains("no unique namespace creation"), + "{error}" + ); + assert_eq!(snapshot(&repo), before); +} + +#[test] +fn cross_view_batch_preflights_all_changes() { + let (_dir, repo, normal, span) = fixture(); + let before = snapshot(&repo); + let error = repo + .insert_from_view( + CrossViewInsertOptions::new("source", "target").only_changes(vec![normal, span]), + ) + .unwrap_err(); + assert!( + error.to_string().contains("no unique namespace creation"), + "{error}" + ); + assert_eq!(snapshot(&repo), before); +} + +#[test] +fn recursive_insert_preflights_dependencies() { + let (_dir, repo, normal, span) = fixture(); + let mut dependencies = vec![normal, span]; + dependencies.sort(); + let root = Change::new(ChangeHeader::new("root"), vec![], vec![], dependencies); + let root = repo.save_change(&root).unwrap(); + let before = snapshot(&repo); + let error = repo + .insert_change_rec(&root, InsertOptions::default().view("target")) + .unwrap_err(); + assert!( + error.to_string().contains("no unique namespace creation"), + "{error}" + ); + assert_eq!(snapshot(&repo), before); +} From 33a606867b86c745edc0a043e004a2445c730eca Mon Sep 17 00:00:00 2001 From: Lee Faus Date: Tue, 3 Nov 2026 07:03:57 -0500 Subject: [PATCH 02/14] fixing harness 1-9 for spans --- atomic-cli/src/commands/change/tree.rs | 95 +++- atomic-cli/src/commands/record/format.rs | 74 ++- atomic-cli/src/commands/record/mod.rs | 1 + atomic-cli/src/commands/record/tests.rs | 16 + atomic-cli/src/commands/rpc.rs | 70 ++- atomic-repository/src/repository/switch.rs | 25 +- atomic-repository/tests/native_span_record.rs | 86 ++++ .../repository_mutation_messages.proto | 18 + libatomic/src/daemon/services.rs | 15 + libatomic/tests/local_service_test.rs | 63 +++ tests/harness/01_single_file.sh | 206 ++------ tests/harness/02_multiple_files.sh | 262 ++-------- tests/harness/03_cross_view.sh | 483 +++++++----------- tests/harness/04_directories.sh | 349 ++++--------- tests/harness/05_semantic_layer.sh | 359 ++++++------- tests/harness/05_workspaces.sh | 220 ++++---- tests/harness/06_tags_and_stash.sh | 368 +++++++------ tests/harness/07_server_push.sh | 112 ++-- tests/harness/08_draft_view_insert.sh | 121 ++--- tests/harness/09_semantic_diff_pairing.sh | 120 +++-- tests/harness/helpers.sh | 151 ++++-- tests/harness/helpers_selftest.sh | 77 +++ tests/harness/run_all.sh | 6 +- 23 files changed, 1641 insertions(+), 1656 deletions(-) create mode 100644 tests/harness/helpers_selftest.sh diff --git a/atomic-cli/src/commands/change/tree.rs b/atomic-cli/src/commands/change/tree.rs index 5cf06c57..10101e29 100644 --- a/atomic-cli/src/commands/change/tree.rs +++ b/atomic-cli/src/commands/change/tree.rs @@ -195,6 +195,22 @@ pub(super) fn render( "Graph: +{vertices} vertices, ~{edges} edges, {} bytes", change.contents.len() )); + if let Some(patch) = &change.hashed.span { + use atomic_core::change::span_patch::SpanOp; + let roots = patch + .ops + .iter() + .filter(|op| matches!(op, SpanOp::Root { .. })) + .count(); + let edits = patch + .ops + .iter() + .filter(|op| matches!(op, SpanOp::Edit { .. })) + .count(); + if roots != 0 || edits != 0 { + root.push(format!("Spans: {roots} roots, {edits} edits")); + } + } root.section(files(change, cmd.show_hunks)); // Keep every recorded provenance entry, not just the first provider/model. for prov in &change.hashed.provenance { @@ -225,8 +241,36 @@ fn files(change: &Change, show_hunks: bool) -> Tree { _ => None, }) .collect(); + let mut summaries = hunk_display_summaries(&change.hashed.hunks); + let mut known: BTreeSet = summaries + .iter() + .map(|summary| summary.path.clone()) + .collect(); + let mut span_only = 0; + if change.hashed.span.as_ref().is_some_and(|patch| { + patch + .ops + .iter() + .any(|op| matches!(op, atomic_core::change::span_patch::SpanOp::Edit { .. })) + }) { + // A span edit carries its portable file identity rather than a graph + // hunk path. Its hashed semantic operation retains the recorded path. + for ops in change.file_ops() { + if ops.trunk_op().is_none() + && !ops.path().is_empty() + && known.insert(ops.path().to_owned()) + { + summaries.push(HunkDisplaySummary { + symbol: "~", + path: ops.path().to_owned(), + info: "(span edits)".into(), + }); + span_only += 1; + } + } + } let mut paths = PathTree::default(); - for summary in hunk_display_summaries(&change.hashed.hunks) { + for summary in summaries { let mut node = &mut paths; // Atomic paths use '/', including when the CLI runs on Windows. Do not // interpret platform-specific separators or consult the working copy. @@ -238,7 +282,7 @@ fn files(change: &Change, show_hunks: bool) -> Tree { } let mut section = Tree::new(format!( "Files changed: {}", - count_unique_paths(&change.hashed.hunks) + count_unique_paths(&change.hashed.hunks) + span_only )); section.children = path_nodes(paths, show_hunks); section @@ -451,6 +495,53 @@ mod tests { } } + #[test] + fn span_only_edit_displays_semantic_path() { + use atomic_core::change::span_patch::{ExternalFile, ExternalSpan, SpanOp, SpanPatch}; + use atomic_core::change::FileOps; + use atomic_core::crdt::TrunkId; + use atomic_core::types::NodeId; + let owner = Hash::of(b"prior change"); + let mut change = Change::with_file_ops( + ChangeHeader::new("span edit"), + Vec::new(), + vec![FileOps::edit( + TrunkId::new(NodeId::new(1), 0), + "src/lines.txt".into(), + )], + b"replacement".to_vec(), + vec![owner], + ); + change.hashed.span = Some(SpanPatch::new( + Vec::new(), + vec![SpanOp::Edit { + file: ExternalFile { owner, ordinal: 0 }, + target: ExternalSpan { + owner, + start: 0, + end: 5, + }, + start: 0, + end: 5, + insert_start: 0, + insert_end: 11, + }], + )); + assert_eq!( + files(&change, false).render(), + "Files changed: 1\n└── src/\n └── lines.txt [modified]\n" + ); + assert!(files(&change, true).render().contains("span edits")); + let output = render( + &ChangeCmd::new(), + &change, + &Hash::of(b"change"), + None, + &data(), + ); + assert!(output.contains("Spans: 0 roots, 1 edits")); + } + #[test] fn paths_have_text_status_and_directories_have_slashes_not_emoji() { let change = fixture(); diff --git a/atomic-cli/src/commands/record/format.rs b/atomic-cli/src/commands/record/format.rs index b98a6598..20fde8ab 100644 --- a/atomic-cli/src/commands/record/format.rs +++ b/atomic-cli/src/commands/record/format.rs @@ -36,44 +36,7 @@ impl Record { view_name, sequence, hash_short, message )); - // Stats line - show graph-based stats - let stats = outcome.stats(); - if stats.has_changes() { - output.push_str(&format!( - " {} changed, +{} vertices, ~{} edges, {} bytes\n", - format_count(stats.files_recorded, "file"), - stats.vertices_added, - stats.edges_modified, - stats.content_bytes - )); - - // CRDT token-level statistics (for fine-grained diff tracking) - if stats.has_crdt_stats() { - // Line-level changes - let line_changes = stats.total_line_changes(); - if line_changes > 0 { - output.push_str(&format!( - " {} (+{} -{} ~{})\n", - format_count(line_changes, "line"), - stats.lines_added, - stats.lines_deleted, - stats.lines_modified - )); - } - - // Token-level changes - let token_ops = stats.total_token_ops(); - if token_ops > 0 { - output.push_str(&format!( - " {} (+{} -{} ~{})\n", - format_count(token_ops, "token"), - stats.tokens_added, - stats.tokens_deleted, - stats.tokens_replaced - )); - } - } - } + output.push_str(&format_record_stats(outcome.stats())); if let Ok(Some(evidence)) = outcome.move_evidence() { for moved in evidence.authoritative_moves { @@ -176,6 +139,41 @@ impl Record { } } +/// Shared rendering for native and service-routed record outcomes. +pub(crate) fn format_record_stats(stats: &atomic_repository::record::RecordStats) -> String { + let mut output = String::new(); + if stats.has_changes() { + output.push_str(&format!( + " {} changed, +{} vertices, ~{} edges, {} bytes\n", + format_count(stats.files_recorded, "file"), + stats.vertices_added, + stats.edges_modified, + stats.content_bytes + )); + let line_changes = stats.total_line_changes(); + if line_changes > 0 { + output.push_str(&format!( + " {} (+{} -{} ~{})\n", + format_count(line_changes, "line"), + stats.lines_added, + stats.lines_deleted, + stats.lines_modified + )); + } + let token_ops = stats.total_token_ops(); + if token_ops > 0 { + output.push_str(&format!( + " {} (+{} -{} ~{})\n", + format_count(token_ops, "token"), + stats.tokens_added, + stats.tokens_deleted, + stats.tokens_replaced + )); + } + } + output +} + /// The dry-run preview, pure over its inputs — the same lines the local /// body prints, shared with the routed path (the wire's Status entries /// drive it; the status vocabulary maps to the local descriptions). diff --git a/atomic-cli/src/commands/record/mod.rs b/atomic-cli/src/commands/record/mod.rs index 6ec9edb1..fc05dd96 100644 --- a/atomic-cli/src/commands/record/mod.rs +++ b/atomic-cli/src/commands/record/mod.rs @@ -418,6 +418,7 @@ impl Record { mod builder; mod command; mod format; +pub(crate) use format::format_record_stats; mod provenance; pub(super) use command::*; diff --git a/atomic-cli/src/commands/record/tests.rs b/atomic-cli/src/commands/record/tests.rs index 4a839ac6..10199b52 100644 --- a/atomic-cli/src/commands/record/tests.rs +++ b/atomic-cli/src/commands/record/tests.rs @@ -54,6 +54,22 @@ mod tests { // Record Command Construction Tests + #[test] + fn record_statistics_render_graph_and_semantic_counts() { + let stats = atomic_repository::record::RecordStats { + files_recorded: 1, + vertices_added: 2, + content_bytes: 12, + lines_added: 3, + tokens_added: 4, + ..Default::default() + }; + let output = format_record_stats(&stats); + assert!(output.contains("1 file changed"), "{output}"); + assert!(output.contains("3 lines (+3 -0 ~0)"), "{output}"); + assert!(output.contains("4 tokens (+4 -0 ~0)"), "{output}"); + } + #[test] fn test_record_new() { let record = Record::new(); diff --git a/atomic-cli/src/commands/rpc.rs b/atomic-cli/src/commands/rpc.rs index c38b9d82..2cac6641 100644 --- a/atomic-cli/src/commands/rpc.rs +++ b/atomic-cli/src/commands/rpc.rs @@ -638,7 +638,21 @@ pub fn record(args: &super::record::Record) -> CliResult { short_hash(&change.hash), change.message.clone().unwrap_or_default() ); - if !args.files.is_empty() { + if let Some(statistics) = &response.statistics { + if let Some(stats) = record_statistics_from_proto(statistics) { + print!("{}", crate::commands::record::format_record_stats(&stats)); + } else { + // The record already succeeded. Invalid presentation metadata + // must not turn that durable success into a reported failure. + crate::output::print_warning( + "Record succeeded, but the service returned invalid statistics.", + ); + } + for file in &response.recorded_paths { + println!(" {file}"); + } + } else if !args.files.is_empty() { + // Older services did not return statistics or recorded paths. for file in &args.files { println!(" {file}"); } @@ -646,6 +660,60 @@ pub fn record(args: &super::record::Record) -> CliResult { Ok(true) } +fn record_statistics_from_proto( + statistics: &pb::RecordStatistics, +) -> Option { + let count = |value| usize::try_from(value).ok(); + let stats = atomic_repository::record::RecordStats { + files_recorded: count(statistics.files_recorded)?, + directories_recorded: count(statistics.directories_recorded)?, + hunks_created: count(statistics.hunks_created)?, + vertices_added: count(statistics.vertices_added)?, + edges_modified: count(statistics.edges_modified)?, + content_bytes: statistics.content_bytes, + lines_added: count(statistics.lines_added)?, + lines_deleted: count(statistics.lines_deleted)?, + lines_modified: count(statistics.lines_modified)?, + tokens_added: count(statistics.tokens_added)?, + tokens_deleted: count(statistics.tokens_deleted)?, + tokens_replaced: count(statistics.tokens_replaced)?, + ..Default::default() + }; + stats + .lines_added + .checked_add(stats.lines_deleted)? + .checked_add(stats.lines_modified)?; + stats + .tokens_added + .checked_add(stats.tokens_deleted)? + .checked_add(stats.tokens_replaced)?; + Some(stats) +} + +#[cfg(test)] +mod record_output_tests { + use super::*; + + #[test] + fn record_statistics_keep_semantic_counts_and_reject_overflow() { + let stats = pb::RecordStatistics { + files_recorded: 1, + lines_added: 3, + tokens_added: 4, + ..Default::default() + }; + let decoded = record_statistics_from_proto(&stats).unwrap(); + assert_eq!(decoded.lines_added, 3); + assert_eq!(decoded.tokens_added, 4); + let invalid = pb::RecordStatistics { + lines_added: u64::MAX, + lines_deleted: 1, + ..Default::default() + }; + assert!(record_statistics_from_proto(&invalid).is_none()); + } +} + // --------------------------------------------------------------------------- // intent // --------------------------------------------------------------------------- diff --git a/atomic-repository/src/repository/switch.rs b/atomic-repository/src/repository/switch.rs index 6bebd193..9d9fb6ae 100644 --- a/atomic-repository/src/repository/switch.rs +++ b/atomic-repository/src/repository/switch.rs @@ -112,6 +112,9 @@ pub(super) struct SwitchMaterializationExecutor<'a> { repository: &'a Repository, operation_lock: &'a super::locks::WorkingCopyOperationLockGuard, operation: Operation, + /// Paths the source and target projections classified as removable. + /// Other FILE_INDEX-only paths can be unrecorded user files. + switch_removal_scope: Option>, } impl<'a> SwitchMaterializationExecutor<'a> { @@ -124,9 +127,15 @@ impl<'a> SwitchMaterializationExecutor<'a> { repository, operation_lock, operation, + switch_removal_scope: None, } } + fn with_switch_removal_scope(mut self, paths: HashSet) -> Self { + self.switch_removal_scope = Some(paths); + self + } + fn effect_for_expected_new( &self, path: &str, @@ -400,6 +409,18 @@ impl super::materialize::MaterializationEffectExecutor for SwitchMaterialization } fn remove_path(&mut self, path: &str) -> Result { + // The materializer also discovers historical FILE_INDEX rows. A path + // absent from both the source's live projection and the target's + // explicit deletions is now untracked, not a switch removal. Leave its + // bytes alone; the caller may retire the stale index row. For paths in + // the planned scope the normal lease checks below remain mandatory. + if self + .switch_removal_scope + .as_ref() + .is_some_and(|paths| !paths.contains(path)) + { + return Ok(false); + } if self .effect_for_expected_new(path, |value| *value == EffectValue::Absent) .is_none() @@ -754,6 +775,7 @@ impl Repository { .collect(); removal_paths.extend(old_files.intersection(&new_directories).cloned()); removal_paths.extend(old_directories.intersection(&new_files).cloned()); + let switch_removal_scope = removal_paths.clone(); let mut removal_paths: Vec = removal_paths.into_iter().collect(); removal_paths.sort_by_key(|path| std::cmp::Reverse(path.matches('/').count())); for path in removal_paths { @@ -943,7 +965,8 @@ impl Repository { } let mut filesystem_executor = - SwitchMaterializationExecutor::new(self, operation_lock, prepared.operation().clone()); + SwitchMaterializationExecutor::new(self, operation_lock, prepared.operation().clone()) + .with_switch_removal_scope(switch_removal_scope); // ── Phase 1: Shelve ignored files into the OLD view's workspace ── // diff --git a/atomic-repository/tests/native_span_record.rs b/atomic-repository/tests/native_span_record.rs index 61cbccc9..6449b0db 100644 --- a/atomic-repository/tests/native_span_record.rs +++ b/atomic-repository/tests/native_span_record.rs @@ -4,6 +4,92 @@ use atomic_core::change::{ChangeHeader, GraphOp}; use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; use atomic_repository::{RecordOptions, Repository, StatusOptions}; +#[test] +fn span_unrecorded_file_stays_untracked_when_switching_views() { + use atomic_core::operation::{EffectTarget, OperationKind, OperationScope}; + use atomic_core::pristine::OperationTxnT; + use atomic_repository::OperationVerificationState; + use atomic_repository::{FileStatus, UnrecordOptions}; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + std::fs::write(dir.path().join("base.txt"), b"base").unwrap(); + repo.add(wc, "base.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("base"), options()) + .unwrap(); + repo.create_draft_view("feature", "dev").unwrap(); + repo.switch_view(wc, "feature").unwrap(); + let path = dir.path().join("temp_feature.txt"); + std::fs::write(&path, b"unrecorded user bytes").unwrap(); + repo.add(wc, "temp_feature.txt", Default::default()) + .unwrap(); + let recorded = repo + .record(wc, ChangeHeader::new("feature"), options()) + .unwrap(); + repo.unrecord(recorded.hash(), UnrecordOptions::default()) + .unwrap(); + std::fs::write(&path, b"edited after unrecord").unwrap(); + let is_untracked = |repo: &Repository| { + repo.status(wc, StatusOptions::default()) + .unwrap() + .entries() + .iter() + .any(|entry| { + entry.path() == std::path::Path::new("temp_feature.txt") + && entry.status() == FileStatus::Untracked + }) + }; + assert!(is_untracked(&repo)); + repo.switch_view(wc, "dev") + .expect("switch preserves unrecorded file"); + assert_eq!(std::fs::read(&path).unwrap(), b"edited after unrecord"); + assert!(is_untracked(&repo)); + let operation_id = { + let txn = repo.pristine().read_txn().unwrap(); + let heads = txn + .get_operation_heads(OperationScope::WorkingCopy(wc)) + .unwrap(); + assert_eq!(heads.as_slice().len(), 1); + heads.as_slice()[0] + }; + let operation = repo.operation_details(operation_id).unwrap(); + assert_eq!( + operation.operation.payload().kind, + OperationKind::SwitchView + ); + assert_eq!(operation.verification, OperationVerificationState::Verified); + assert!(!operation + .operation + .payload() + .delta + .effects + .iter() + .any(|effect| matches!( + &effect.target, EffectTarget::FilesystemPath { path } if path == "temp_feature.txt" + ))); + repo.switch_view(wc, "feature") + .expect("switch back with untracked file"); + assert_eq!(std::fs::read(&path).unwrap(), b"edited after unrecord"); + assert!(is_untracked(&repo)); + repo.switch_view(wc, "dev") + .expect("switch again with untracked file"); + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"edited after unrecord"); + assert!(is_untracked(&repo)); +} + #[test] fn cross_view_span_insert_includes_dependencies_and_composes_independent_edits() { use atomic_repository::apply::CrossViewInsertOptions; diff --git a/libatomic/proto/atomic/services/repository_mutation/repository_mutation_messages.proto b/libatomic/proto/atomic/services/repository_mutation/repository_mutation_messages.proto index 5ed32ba1..ff1ccea5 100644 --- a/libatomic/proto/atomic/services/repository_mutation/repository_mutation_messages.proto +++ b/libatomic/proto/atomic/services/repository_mutation/repository_mutation_messages.proto @@ -141,9 +141,27 @@ message RecordResponse { optional Hash view_merkle = 2; // post-record state for the next // expected-state precondition optional string view = 3; // view the change landed on (add-only) + optional RecordStatistics statistics = 4; // actual recorder statistics + repeated string recorded_paths = 5; ResponseMeta meta = 100; } +// Recorded graph and semantic counts. Older services may omit this message. +message RecordStatistics { + uint64 files_recorded = 1; + uint64 directories_recorded = 2; + uint64 hunks_created = 3; + uint64 vertices_added = 4; + uint64 edges_modified = 5; + uint64 content_bytes = 6; + uint64 lines_added = 7; + uint64 lines_deleted = 8; + uint64 lines_modified = 9; + uint64 tokens_added = 10; + uint64 tokens_deleted = 11; + uint64 tokens_replaced = 12; +} + message ReviseRequest { RepositoryRef repository = 1; RequestMeta meta = 2; diff --git a/libatomic/src/daemon/services.rs b/libatomic/src/daemon/services.rs index 2b8b1d74..928b5d69 100644 --- a/libatomic/src/daemon/services.rs +++ b/libatomic/src/daemon/services.rs @@ -1588,6 +1588,21 @@ impl repository_mutation_service_server::RepositoryMutationService for MutationI }), view_merkle: outcome.new_state().map(|state| hash_proto(&state)), view: Some(view), + statistics: Some(RecordStatistics { + files_recorded: outcome.stats().files_recorded as u64, + directories_recorded: outcome.stats().directories_recorded as u64, + hunks_created: outcome.stats().hunks_created as u64, + vertices_added: outcome.stats().vertices_added as u64, + edges_modified: outcome.stats().edges_modified as u64, + content_bytes: outcome.stats().content_bytes, + lines_added: outcome.stats().lines_added as u64, + lines_deleted: outcome.stats().lines_deleted as u64, + lines_modified: outcome.stats().lines_modified as u64, + tokens_added: outcome.stats().tokens_added as u64, + tokens_deleted: outcome.stats().tokens_deleted as u64, + tokens_replaced: outcome.stats().tokens_replaced as u64, + }), + recorded_paths: outcome.recorded_files().to_vec(), meta: Some(ResponseMeta { request_id: meta.request_id, replayed: false, diff --git a/libatomic/tests/local_service_test.rs b/libatomic/tests/local_service_test.rs index e4173a01..186ce734 100644 --- a/libatomic/tests/local_service_test.rs +++ b/libatomic/tests/local_service_test.rs @@ -178,6 +178,11 @@ async fn local_add_record_log_and_change_use_existing_handlers() { }, ) .await; + let statistics = recorded.statistics.as_ref().expect("record statistics"); + assert_eq!(statistics.files_recorded, 1); + assert_eq!(statistics.lines_added, 1); + assert!(statistics.tokens_added > 0); + assert_eq!(recorded.recorded_paths, vec!["hello.txt"]); let hash = recorded.change.unwrap().hash.unwrap(); assert_eq!(hash.value.len(), 32); @@ -235,6 +240,64 @@ async fn local_add_record_log_and_change_use_existing_handlers() { assert!(without.change_bundle.is_none()); } +#[tokio::test] +async fn local_record_reports_native_span_semantic_statistics() { + use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + drop(repo); + let path = dir.path().join("hello.txt"); + std::fs::write(&path, "first\nsecond\n").unwrap(); + let service = LocalService::new(); + let repository = resolve(&service, dir.path()).await; + let _: proto::AddFilesResponse = invoke( + &service, + "/atomic.RepositoryMutationService/AddFiles", + &proto::AddFilesRequest { + repository: Some(repository.clone()), + paths: vec!["hello.txt".into()], + ..Default::default() + }, + ) + .await; + let root: proto::RecordResponse = invoke( + &service, + "/atomic.RepositoryMutationService/Record", + &proto::RecordRequest { + repository: Some(repository.clone()), + message: "root".into(), + ..Default::default() + }, + ) + .await; + let stats = root.statistics.unwrap(); + assert_eq!(stats.files_recorded, 1); + assert_eq!(stats.lines_added, 2); + assert!(stats.tokens_added > 0); + assert_eq!(root.recorded_paths, vec!["hello.txt"]); + std::fs::write(&path, "FIRST\nsecond\n").unwrap(); + let edit: proto::RecordResponse = invoke( + &service, + "/atomic.RepositoryMutationService/Record", + &proto::RecordRequest { + repository: Some(repository), + message: "edit".into(), + ..Default::default() + }, + ) + .await; + let stats = edit.statistics.unwrap(); + assert_eq!(stats.files_recorded, 1); + assert!(stats.lines_added + stats.lines_deleted + stats.lines_modified > 0); + assert!(stats.tokens_added + stats.tokens_deleted + stats.tokens_replaced > 0); + assert_eq!(edit.recorded_paths, vec!["hello.txt"]); +} + #[tokio::test] async fn local_view_lifecycle_uses_existing_handlers() { let dir = tempfile::tempdir().unwrap(); diff --git a/tests/harness/01_single_file.sh b/tests/harness/01_single_file.sh index 862760b8..97dd0311 100755 --- a/tests/harness/01_single_file.sh +++ b/tests/harness/01_single_file.sh @@ -23,18 +23,7 @@ init_repo assert_current_view "initial view is dev" "dev" -# An empty repo should be clean -out="$(get_status)" -if echo "$out" | grep -qiE "clean|no changes|nothing"; then - _pass "empty repo status is clean" -else - # Accept no output at all (also clean) - if [[ -z "$(echo "$out" | xargs)" ]]; then - _pass "empty repo status is clean (empty output)" - else - _fail "empty repo status is clean" "got: $(echo "$out" | head -5)" - fi -fi +assert_clean "empty repo status is clean" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Create → Status (untracked)" @@ -58,7 +47,7 @@ assert_status_flag "short status shows ? for hello.txt" "?" "hello.txt" begin_section "Single File: Add → Status (added)" # ═══════════════════════════════════════════════════════════════════════════ -assert_success "add hello.txt succeeds" atomic add hello.txt +require_success "add hello.txt succeeds" atomic add hello.txt # File should now be tracked assert_success "hello.txt is tracked after add" atomic status @@ -66,34 +55,16 @@ assert_success "hello.txt is tracked after add" atomic status # Short format: A = added assert_status_flag "short status shows A for hello.txt" "A" "hello.txt" -# Adding again should be idempotent (or warn) -add_output="$(atomic add hello.txt 2>&1)" || true -# Should not error fatally -_pass "re-adding an already-tracked file does not crash" +assert_success "re-adding an already-tracked file succeeds" atomic add hello.txt # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Record → Status (clean)" # ═══════════════════════════════════════════════════════════════════════════ -rec_out="$(record_change "Add hello.txt")" -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record succeeds and prints hash" -else - # Even if the output format is different, as long as exit 0 we pass - if atomic status >/dev/null 2>&1; then - _pass "record succeeds (status still works)" - else - _fail "record succeeds" "output: $(echo "$rec_out" | head -5)" - fi -fi +require_success "record hello.txt succeeds" record_change "Add hello.txt" # Status should be clean now -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*hello\.txt"; then - _fail "status is clean after record" "hello.txt still has a dirty flag: $(echo "$out" | head -5)" -else - _pass "status is clean after record" -fi +assert_status_no_entry "status is clean after record" "hello.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Modify → Status (modified)" @@ -109,63 +80,33 @@ assert_status_flag "short status shows M for modified hello.txt" "M" "hello.txt" begin_section "Single File: Record modification → Status (clean)" # ═══════════════════════════════════════════════════════════════════════════ -rec_out2="$(record_change "Modify hello.txt")" -if echo "$rec_out2" | grep -qiE "hash|recorded|created|change"; then - _pass "record modification succeeds" -else - _pass "record modification completes" -fi +require_success "record modification succeeds" record_change "Modify hello.txt" -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*hello\.txt"; then - _fail "status is clean after recording modification" "got: $(echo "$out" | head -5)" -else - _pass "status is clean after recording modification" -fi +assert_status_no_entry "status is clean after recording modification" "hello.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Unrecord → Status" # ═══════════════════════════════════════════════════════════════════════════ -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord succeeds" -else - # As long as it doesn't crash we continue - _pass "unrecord completes without crash" -fi +require_success "unrecord succeeds" unrecord_last # After unrecording the modification, the file should show as modified # (disk has the new content, graph has the old content) -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MA].*hello\.txt"; then - _pass "hello.txt shows as modified or added after unrecord" -else - # Might also be clean if unrecord left content as-is - _pass "status after unrecord completed (may vary by impl)" -fi +assert_status_flag "hello.txt shows as modified after unrecord" "M" "hello.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Delete from disk → Status (deleted)" # ═══════════════════════════════════════════════════════════════════════════ # Re-record so we have a clean state to delete from -record_change "Re-record hello.txt" >/dev/null 2>&1 || true +require_success "re-record hello.txt" record_change "Re-record hello.txt" rm -f hello.txt assert_file_not_exists "hello.txt is gone from disk" "hello.txt" # Status should show as deleted (D) -out="$(get_status_short)" -if echo "$out" | grep -qE "^D.*hello\.txt"; then - _pass "short status shows D for deleted hello.txt" -elif echo "$out" | grep -qF "hello.txt"; then - # Some implementations might use a different flag - _pass "status mentions deleted hello.txt" -else - _fail "status shows hello.txt as deleted" "got: $(echo "$out" | head -5)" -fi +assert_status_flag "short status shows D for deleted hello.txt" "D" "hello.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Empty file" @@ -177,19 +118,19 @@ init_repo create_file "empty.txt" "" assert_file_exists "empty.txt exists" "empty.txt" +if [[ ! -s empty.txt ]]; then + _pass "empty.txt contains zero bytes" +else + _fail "empty.txt contains zero bytes" "file is not empty" +fi # Add and record empty file -assert_success "add empty.txt" atomic add empty.txt +require_success "add empty.txt" atomic add empty.txt assert_status_flag "empty.txt shows as added" "A" "empty.txt" -rec_out="$(record_change "Add empty file" 2>&1)" || true -# Recording an empty file might succeed or might skip (implementation-dependent) -if echo "$rec_out" | grep -qiE "hash|recorded|nothing"; then - _pass "record of empty file completes" -else - _pass "record of empty file does not crash" -fi +require_success "record empty file" record_change "Add empty file" +assert_status_no_entry "empty file is clean after record" "empty.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: File with spaces in name" @@ -202,12 +143,7 @@ create_file "my file.txt" "content with spaces in filename" assert_file_exists "file with spaces exists" "my file.txt" -add_out="$(atomic add "my file.txt" 2>&1)" || true -if [[ $? -eq 0 ]] || echo "$add_out" | grep -qiE "added|tracked"; then - _pass "add file with spaces in name" -else - _skip "add file with spaces in name" "may not be supported yet" -fi +assert_success "add file with spaces in name" atomic add "my file.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: File with unicode name" @@ -220,12 +156,7 @@ create_file "héllo_wörld.txt" "unicode filename content" assert_file_exists "unicode filename exists" "héllo_wörld.txt" -add_out="$(atomic add "héllo_wörld.txt" 2>&1)" || true -if [[ $? -eq 0 ]] || echo "$add_out" | grep -qiE "added|tracked"; then - _pass "add file with unicode name" -else - _skip "add file with unicode name" "may not be supported yet" -fi +assert_success "add file with unicode name" atomic add "héllo_wörld.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Nested file (deep path)" @@ -242,12 +173,7 @@ assert_success "add deeply nested file" atomic add "src/pkg/deep/main.rs" assert_status_flag "nested file shows as added" "A" "src/pkg/deep/main.rs" -rec_out="$(record_change "Add nested file")" -if echo "$rec_out" | grep -qiE "hash|recorded|created"; then - _pass "record nested file succeeds" -else - _pass "record nested file completes" -fi +require_success "record nested file succeeds" record_change "Add nested file" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Binary file" @@ -261,12 +187,7 @@ printf '\x00\x01\x02\xff\xfe' > binary.dat assert_file_exists "binary file exists" "binary.dat" -add_out="$(atomic add binary.dat 2>&1)" || true -if [[ $? -eq 0 ]] || echo "$add_out" | grep -qiE "added|tracked"; then - _pass "add binary file" -else - _skip "add binary file" "may not be supported" -fi +assert_success "add binary file" atomic add binary.dat # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Large file" @@ -280,12 +201,7 @@ dd if=/dev/urandom of=large.bin bs=1024 count=1024 2>/dev/null assert_file_exists "large file exists" "large.bin" -add_out="$(atomic add large.bin 2>&1)" || true -if [[ $? -eq 0 ]] || echo "$add_out" | grep -qiE "added|tracked"; then - _pass "add large file" -else - _skip "add large file" "may not be supported" -fi +assert_success "add large file" atomic add large.bin # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Add file then remove tracking" @@ -300,28 +216,13 @@ assert_success "add removeme.txt" atomic add removeme.txt assert_status_flag "removeme.txt shows as added" "A" "removeme.txt" # Remove from tracking (file stays on disk) -rm_out="$(atomic remove removeme.txt 2>&1)" || true -if echo "$rm_out" | grep -qiE "removed|untracked"; then - _pass "remove from tracking succeeds" -elif [[ $? -eq 0 ]]; then - _pass "remove from tracking completes" -else - _skip "remove from tracking" "remove command may not exist yet" -fi +require_success "remove from tracking succeeds" atomic remove removeme.txt # File should still exist on disk assert_file_exists "file still on disk after remove tracking" "removeme.txt" # File should now be untracked -out="$(get_status_short)" -if echo "$out" | grep -qE "^\?.*removeme\.txt"; then - _pass "removeme.txt is untracked after remove" -elif ! echo "$out" | grep -qF "removeme.txt"; then - # Not in status at all could also be valid - _pass "removeme.txt not in status after remove" -else - _pass "removeme.txt status after remove (implementation dependent)" -fi +assert_status_flag "removeme.txt is untracked after remove" "?" "removeme.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Re-add after unrecord" @@ -332,21 +233,20 @@ init_repo create_file "cycle.txt" "v1" assert_success "add cycle.txt" atomic add cycle.txt -record_change "Add cycle.txt v1" >/dev/null 2>&1 || true +require_success "record cycle.txt v1" record_change "Add cycle.txt v1" # Unrecord -unrecord_last >/dev/null 2>&1 || true +require_success "unrecord cycle.txt v1" unrecord_last +assert_status_flag "unrecorded cycle.txt is untracked" "?" "cycle.txt" # Modify overwrite_file "cycle.txt" "v2" +require_success "re-add cycle.txt after unrecord" atomic add cycle.txt +assert_status_flag "re-added cycle.txt is staged" "A" "cycle.txt" # Re-record -rec_out="$(record_change "Re-add cycle.txt v2" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created"; then - _pass "re-record after unrecord succeeds" -else - _pass "re-record after unrecord completes" -fi +require_success "re-record after unrecord succeeds" record_change "Re-add cycle.txt v2" +assert_status_no_entry "cycle.txt is clean after re-record" "cycle.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Single File: Multiple modifications, single record" @@ -357,7 +257,7 @@ init_repo create_file "evolving.txt" "version 1" assert_success "add evolving.txt" atomic add evolving.txt -record_change "Initial evolving.txt" >/dev/null 2>&1 || true +require_success "record initial evolving.txt" record_change "Initial evolving.txt" # Make multiple modifications without recording overwrite_file "evolving.txt" "version 2" @@ -367,12 +267,7 @@ overwrite_file "evolving.txt" "version 4 final" # Only the final state should be recorded assert_status_flag "evolving.txt is modified" "M" "evolving.txt" -rec_out="$(record_change "Final version of evolving.txt" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created"; then - _pass "record captures final state after multiple edits" -else - _pass "record after multiple edits completes" -fi +require_success "record captures final state after multiple edits" record_change "Final version of evolving.txt" assert_file_content "file has final content after record" "evolving.txt" "version 4 final" @@ -388,12 +283,7 @@ create_file ".config" "dotfile content" assert_file_exists ".config exists" ".config" # Dotfiles may or may not be auto-hidden from status -add_out="$(atomic add ".config" 2>&1)" || true -if [[ $? -eq 0 ]]; then - _pass "add dotfile" -else - _skip "add dotfile" "dotfiles may be ignored by default" -fi +assert_success "add dotfile" atomic add ".config" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Large Record: Many files, performance baseline" @@ -492,15 +382,9 @@ assert_success "add all files" atomic add . # Time the initial record record_start=$SECONDS -record_out="$(record_change "Initial large record" 2>&1)" || true +require_success "initial large record completes" record_change "Initial large record" record_elapsed=$((SECONDS - record_start)) -if echo "$record_out" | grep -qiE "hash|recorded|created|change|file"; then - _pass "initial large record completes ($record_elapsed seconds)" -else - _pass "initial large record completes ($record_elapsed seconds)" -fi - if [ $record_elapsed -le 15 ]; then _pass "initial record within 15s threshold (${record_elapsed}s)" else @@ -528,11 +412,9 @@ for j in $(seq 5001 5100); do done modify_start=$SECONDS -record_out="$(record_change "Modify 10 modules + data" 2>&1)" || true +require_success "modify record completes" record_change "Modify 10 modules + data" modify_elapsed=$((SECONDS - modify_start)) -_pass "modify record completes ($modify_elapsed seconds)" - if [ $modify_elapsed -le 10 ]; then _pass "modify record within 10s threshold (${modify_elapsed}s)" else @@ -549,14 +431,13 @@ begin_section "Large Record: View switch performance after large record" # Validates that the parallel materialize + INODE_GRAPH preload # keeps view switching fast even with many files. -atomic view create feature >/dev/null 2>&1 || true +require_success "create feature view" atomic view create feature switch_start=$SECONDS -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" switch_elapsed=$((SECONDS - switch_start)) assert_current_view "on feature" "feature" -_pass "switch to feature ($switch_elapsed seconds)" if [ $switch_elapsed -le 5 ]; then _pass "switch within 5s threshold (${switch_elapsed}s)" @@ -566,19 +447,18 @@ fi # Switch back switch_start=$SECONDS -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" switch_elapsed=$((SECONDS - switch_start)) assert_current_view "back on dev" "dev" assert_clean "dev still clean after round-trip" -_pass "switch back to dev ($switch_elapsed seconds)" # Verify file count is preserved final_files=$(find . -not -path './.atomic/*' -type f | wc -l | tr -d ' ') -if [ "$final_files" -ge 50 ]; then +if [ "$final_files" -eq "$total_files" ]; then _pass "all files preserved after view round-trip ($final_files files)" else - _fail "all files preserved after view round-trip" "only $final_files files" + _fail "all files preserved after view round-trip" "expected $total_files files, found $final_files" fi # ═══════════════════════════════════════════════════════════════════════════ diff --git a/tests/harness/02_multiple_files.sh b/tests/harness/02_multiple_files.sh index d6203468..ebeba9d3 100755 --- a/tests/harness/02_multiple_files.sh +++ b/tests/harness/02_multiple_files.sh @@ -34,28 +34,18 @@ assert_status_flag "b.txt is untracked" "?" "b.txt" assert_status_flag "c.txt is untracked" "?" "c.txt" # Add all three at once -assert_success "add a.txt b.txt c.txt" atomic add a.txt b.txt c.txt +require_success "add a.txt b.txt c.txt" atomic add a.txt b.txt c.txt assert_status_flag "a.txt is added" "A" "a.txt" assert_status_flag "b.txt is added" "A" "b.txt" assert_status_flag "c.txt is added" "A" "c.txt" # Record all -rec_out="$(record_change "Add three files")" -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record three files succeeds" -else - _pass "record three files completes" -fi +require_success "record three files succeeds" record_change "Add three files" # All should be clean -out="$(get_status_short)" for f in a.txt b.txt c.txt; do - if echo "$out" | grep -qE "^[MADU?].*${f}"; then - _fail "$f is clean after record" "still dirty: $(echo "$out" | grep "$f")" - else - _pass "$f is clean after record" - fi + assert_status_no_entry "$f is clean after record" "$f" done # ═══════════════════════════════════════════════════════════════════════════ @@ -70,7 +60,7 @@ create_file "tracked2.txt" "tracked two" create_file "loose_file.txt" "not tracked" # Add only the first two -assert_success "add tracked1.txt tracked2.txt" atomic add tracked1.txt tracked2.txt +require_success "add tracked1.txt tracked2.txt" atomic add tracked1.txt tracked2.txt # Status should show 2 added + 1 untracked assert_status_flag "tracked1.txt is added" "A" "tracked1.txt" @@ -78,19 +68,14 @@ assert_status_flag "tracked2.txt is added" "A" "tracked2.txt" assert_status_flag "loose_file.txt is untracked" "?" "loose_file.txt" # Record (should pick up only the added files) -record_change "Add tracked files only" >/dev/null 2>&1 || true +require_success "record tracked files only" record_change "Add tracked files only" # loose_file.txt should still be untracked assert_status_flag "loose_file.txt still untracked after record" "?" "loose_file.txt" # tracked files should be clean -out="$(get_status_short)" for f in tracked1.txt tracked2.txt; do - if echo "$out" | grep -qE "^[MADU?].*${f}"; then - _fail "$f is clean after record" "still dirty" - else - _pass "$f is clean after record" - fi + assert_status_no_entry "$f is clean after record" "$f" done # ═══════════════════════════════════════════════════════════════════════════ @@ -104,8 +89,8 @@ create_file "stay.txt" "unchanged" create_file "modify.txt" "original" create_file "delete_me.txt" "will be deleted" -assert_success "add all three" atomic add stay.txt modify.txt delete_me.txt -record_change "Initial three files" >/dev/null 2>&1 || true +require_success "add all three" atomic add stay.txt modify.txt delete_me.txt +require_success "record initial three files" record_change "Initial three files" # Modify one, delete another, leave one untouched overwrite_file "modify.txt" "modified content" @@ -117,52 +102,31 @@ create_file "newcomer.txt" "I'm new" # Status should have a mix assert_status_flag "modify.txt is modified" "M" "modify.txt" -out="$(get_status_short)" - # delete_me.txt should be D -if echo "$out" | grep -qE "^D.*delete_me\.txt"; then - _pass "delete_me.txt shows as deleted" -elif echo "$out" | grep -qF "delete_me.txt"; then - _pass "delete_me.txt appears in status" -else - _fail "delete_me.txt shows as deleted" "not found in status" -fi +assert_status_flag "delete_me.txt shows as deleted" "D" "delete_me.txt" # newcomer.txt should be ? assert_status_flag "newcomer.txt is untracked" "?" "newcomer.txt" # stay.txt should NOT appear in short status (it's clean) -if echo "$out" | grep -qE "^[MADU?].*stay\.txt"; then - _fail "stay.txt is clean (not in short output)" "found: $(echo "$out" | grep stay.txt)" -else - _pass "stay.txt is clean (not in short output)" -fi +assert_status_no_entry "stay.txt is clean (not in short output)" "stay.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Multiple Files: Record only modified + delete" # ═══════════════════════════════════════════════════════════════════════════ # Continuing from previous state -rec_out="$(record_change "Record modification and deletion" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record mixed changes succeeds" -else - _pass "record mixed changes completes" -fi +require_success "record mixed changes succeeds" record_change "Record modification and deletion" # newcomer.txt should still be untracked (was never added) assert_status_flag "newcomer.txt still untracked" "?" "newcomer.txt" # delete_me.txt should no longer appear (it's recorded as deleted) # stay.txt and modify.txt should be clean -out="$(get_status_short)" for f in stay.txt modify.txt; do - if echo "$out" | grep -qE "^[MAD].*${f}"; then - _fail "$f is clean after recording modification" "found dirty entry" - else - _pass "$f is clean after recording modification" - fi + assert_status_no_entry "$f is clean after recording modification" "$f" done +assert_status_no_entry "deleted file is absent from status after record" "delete_me.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Multiple Files: Unrecord last with multiple files" @@ -175,31 +139,16 @@ create_file "x.txt" "x content" create_file "y.txt" "y content" create_file "z.txt" "z content" -assert_success "add x y z" atomic add x.txt y.txt z.txt -record_change "Add x y z" >/dev/null 2>&1 || true +require_success "add x y z" atomic add x.txt y.txt z.txt +require_success "record x y z" record_change "Add x y z" # Unrecord should remove the last change which added all three -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord multiple-file change succeeds" -else - _pass "unrecord multiple-file change completes" -fi - -# After unrecord, files are still on disk and still in TREE (added) -# They should show up as added or modified -out="$(get_status_short)" -found=0 +require_success "unrecord multiple-file change succeeds" unrecord_last + +# Unrecording their original addition leaves the files untracked on disk. for f in x.txt y.txt z.txt; do - if echo "$out" | grep -qE "^[MA].*${f}"; then - found=$((found + 1)) - fi + assert_status_flag "$f is untracked after unrecord" "?" "$f" done -if [[ $found -ge 1 ]]; then - _pass "files revert to added/modified after unrecord ($found/3 found)" -else - _pass "files status after unrecord (implementation-dependent)" -fi # Files still on disk assert_file_exists "x.txt still on disk" "x.txt" @@ -222,26 +171,12 @@ assert_status_flag "auto1.txt is untracked" "?" "auto1.txt" assert_status_flag "auto2.txt is untracked" "?" "auto2.txt" # Use --all to add everything -add_out="$(atomic add --all 2>&1)" || true -if [[ $? -eq 0 ]] || echo "$add_out" | grep -qiE "added|tracked"; then - _pass "add --all adds all untracked files" -else - _skip "add --all" "may not be supported" -fi +require_success "add --all adds all untracked files" atomic add --all # Check that all are now added -out="$(get_status_short)" -added_count=0 -for f in auto1.txt auto2.txt; do - if echo "$out" | grep -qE "^A.*${f}"; then - added_count=$((added_count + 1)) - fi +for f in auto1.txt auto2.txt sub/auto3.txt; do + assert_status_flag "$f is added after --all" "A" "$f" done -if [[ $added_count -ge 2 ]]; then - _pass "all files show as added after --all" -else - _pass "add --all completed (added $added_count files)" -fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Multiple Files: Record with --all flag (auto-add)" @@ -255,26 +190,12 @@ create_file "rec_all2.txt" "auto record two" create_file "rec_all3.txt" "auto record three" # Record with --all should add + record in one step -rec_out="$(atomic record --all -m "Record everything" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record --all auto-adds and records" -else - _pass "record --all completes" -fi +require_success "record --all auto-adds and records" atomic record --all -m "Record everything" # Check status is clean -out="$(get_status_short)" -dirty=0 for f in rec_all1.txt rec_all2.txt rec_all3.txt; do - if echo "$out" | grep -qE "^[MADU?].*${f}"; then - dirty=$((dirty + 1)) - fi + assert_status_no_entry "$f is clean after record --all" "$f" done -if [[ $dirty -eq 0 ]]; then - _pass "all files clean after record --all" -else - _pass "record --all status ($dirty files still dirty)" -fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Multiple Files: Files in different directories" @@ -302,21 +223,12 @@ for f in root.txt src/lib.rs src/utils/helper.rs docs/README.md tests/test1.rs; done # Record -record_change "Add files from multiple directories" >/dev/null 2>&1 || true +require_success "record files from multiple directories" record_change "Add files from multiple directories" # All should be clean -out="$(get_status_short)" -dirty=0 for f in root.txt src/lib.rs src/utils/helper.rs docs/README.md tests/test1.rs; do - if echo "$out" | grep -qE "^[MADU?].*$(echo "$f" | sed 's/\//\\\//g')"; then - dirty=$((dirty + 1)) - fi + assert_status_no_entry "$f is clean after record" "$f" done -if [[ $dirty -eq 0 ]]; then - _pass "all multi-dir files clean after record" -else - _fail "all multi-dir files clean after record" "$dirty files still dirty" -fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Multiple Files: Modify files across directories" @@ -330,15 +242,10 @@ assert_status_flag "src/lib.rs is modified" "M" "src/lib.rs" assert_status_flag "docs/README.md is modified" "M" "docs/README.md" # root.txt should still be clean -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MAD].*root\.txt"; then - _fail "root.txt unchanged" "got dirty" -else - _pass "root.txt unchanged after modifying other files" -fi +assert_status_no_entry "root.txt unchanged after modifying other files" "root.txt" # Record only the modified files -record_change "Update lib and readme" >/dev/null 2>&1 || true +require_success "record updated lib and readme" record_change "Update lib and readme" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Multiple Files: Bulk delete + record" @@ -356,36 +263,24 @@ for i in $(seq 1 5); do atomic add "file${i}.txt" >/dev/null 2>&1 done -record_change "Add five files" >/dev/null 2>&1 || true +require_success "record five files" record_change "Add five files" # Delete files 2, 3, 4 rm -f file2.txt file3.txt file4.txt # Status: 1 and 5 clean, 2/3/4 deleted -out="$(get_status_short)" -deleted_count=0 for i in 2 3 4; do - if echo "$out" | grep -qE "^D.*file${i}\.txt"; then - deleted_count=$((deleted_count + 1)) - fi + assert_status_flag "file${i}.txt is deleted" "D" "file${i}.txt" done -if [[ $deleted_count -ge 2 ]]; then - _pass "bulk deleted files show as deleted ($deleted_count/3)" -else - _pass "bulk delete status (found $deleted_count deleted)" -fi # file1 and file5 should be clean for i in 1 5; do - if echo "$out" | grep -qE "^[MAD].*file${i}\.txt"; then - _fail "file${i}.txt is clean" "found dirty" - else - _pass "file${i}.txt is clean (not deleted)" - fi + assert_status_no_entry "file${i}.txt is clean (not deleted)" "file${i}.txt" done # Record the deletions -record_change "Delete files 2-4" >/dev/null 2>&1 || true +require_success "record deleted files 2-4" record_change "Delete files 2-4" +assert_clean "clean after recording bulk deletions" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Multiple Files: Interleaved add and record" @@ -397,50 +292,32 @@ init_repo # Round 1: add and record one file create_file "round1.txt" "round one" assert_success "add round1.txt" atomic add round1.txt -record_change "Round 1" >/dev/null 2>&1 || true +require_success "record round 1" record_change "Round 1" # Round 2: add and record a second file create_file "round2.txt" "round two" assert_success "add round2.txt" atomic add round2.txt -record_change "Round 2" >/dev/null 2>&1 || true +require_success "record round 2" record_change "Round 2" # Round 3: add and record a third file create_file "round3.txt" "round three" assert_success "add round3.txt" atomic add round3.txt -record_change "Round 3" >/dev/null 2>&1 || true +require_success "record round 3" record_change "Round 3" # All should be clean -out="$(get_status_short)" -dirty=0 for f in round1.txt round2.txt round3.txt; do - if echo "$out" | grep -qE "^[MADU?].*${f}"; then - dirty=$((dirty + 1)) - fi + assert_status_no_entry "$f is clean after interleaved records" "$f" done -if [[ $dirty -eq 0 ]]; then - _pass "all interleaved files clean" -else - _fail "all interleaved files clean" "$dirty still dirty" -fi # Unrecord the last one (round 3) -unrecord_last >/dev/null 2>&1 || true +require_success "unrecord round 3" unrecord_last -# round3.txt should now be added/modified, others clean -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MA].*round3\.txt"; then - _pass "round3.txt reverts after unrecord" -else - _pass "round3.txt status after unrecord (implementation-dependent)" -fi +# The unrecorded addition leaves round3.txt untracked, while others stay clean. +assert_status_flag "round3.txt is untracked after unrecord" "?" "round3.txt" # round1 and round2 should still be clean for f in round1.txt round2.txt; do - if echo "$out" | grep -qE "^[MAD].*${f}"; then - _fail "$f still clean after unrecording round3" "found dirty" - else - _pass "$f still clean after unrecording round3" - fi + assert_status_no_entry "$f still clean after unrecording round3" "$f" done # ═══════════════════════════════════════════════════════════════════════════ @@ -454,8 +331,7 @@ create_file "double.txt" "once" # Add twice — should be idempotent assert_success "first add" atomic add double.txt -add2_out="$(atomic add double.txt 2>&1)" || true -_pass "second add of same file does not crash" +assert_success "second add of same file succeeds" atomic add double.txt # Still shows as added (once) assert_status_flag "double.txt is added" "A" "double.txt" @@ -472,29 +348,15 @@ create_file "mydir/b.txt" "b in dir" create_file "mydir/sub/c.txt" "c in sub" # Add the whole directory -add_out="$(atomic add mydir 2>&1)" || true -if [[ $? -eq 0 ]]; then - _pass "add directory recursively" -else - _skip "add directory recursively" "may not be supported" -fi +require_success "add directory recursively" atomic add mydir # Check that files inside are now added -out="$(get_status_short)" -added=0 for f in mydir/a.txt mydir/b.txt mydir/sub/c.txt; do - if echo "$out" | grep -qE "^A.*${f}"; then - added=$((added + 1)) - fi + assert_status_flag "$f is added recursively" "A" "$f" done -if [[ $added -ge 2 ]]; then - _pass "files inside directory are added ($added/3)" -else - _pass "directory add result ($added files added)" -fi # Record the whole directory -record_change "Add mydir contents" >/dev/null 2>&1 || true +require_success "record mydir contents" record_change "Add mydir contents" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Multiple Files: Mix of adds, modifies, deletes in one record" @@ -510,7 +372,7 @@ create_file "gone.txt" "will go" assert_success "add stable" atomic add stable.txt assert_success "add change" atomic add change.txt assert_success "add gone" atomic add gone.txt -record_change "Initial three" >/dev/null 2>&1 || true +require_success "record initial mixed files" record_change "Initial three" # Now: modify one, delete one, add a new one overwrite_file "change.txt" "changed!" @@ -519,41 +381,19 @@ create_file "brand_new.txt" "hello" assert_success "add brand_new" atomic add brand_new.txt # Verify mixed status -out="$(get_status_short)" - -if echo "$out" | grep -qE "^M.*change\.txt"; then - _pass "change.txt shows as modified" -else - _fail "change.txt shows as modified" "not found as M" -fi - -if echo "$out" | grep -qE "^D.*gone\.txt"; then - _pass "gone.txt shows as deleted" -elif echo "$out" | grep -qF "gone.txt"; then - _pass "gone.txt appears in status" -else - _fail "gone.txt shows as deleted" "not found" -fi - -if echo "$out" | grep -qE "^A.*brand_new\.txt"; then - _pass "brand_new.txt shows as added" -else - _fail "brand_new.txt shows as added" "not found as A" -fi +assert_status_flag "change.txt shows as modified" "M" "change.txt" +assert_status_flag "gone.txt shows as deleted" "D" "gone.txt" +assert_status_flag "brand_new.txt shows as added" "A" "brand_new.txt" # Record all mixed changes -rec_out="$(record_change "Mixed changes: modify, delete, add" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record mixed changes succeeds" -else - _pass "record mixed changes completes" -fi +require_success "record mixed changes succeeds" record_change "Mixed changes: modify, delete, add" # After record, only stable.txt, change.txt, and brand_new.txt should remain assert_file_exists "stable.txt still exists" "stable.txt" assert_file_exists "change.txt still exists" "change.txt" assert_file_exists "brand_new.txt exists" "brand_new.txt" assert_file_not_exists "gone.txt is gone" "gone.txt" +assert_clean "working copy is clean after mixed record" # ═══════════════════════════════════════════════════════════════════════════ diff --git a/tests/harness/03_cross_view.sh b/tests/harness/03_cross_view.sh index aa3e1d69..d2734076 100755 --- a/tests/harness/03_cross_view.sh +++ b/tests/harness/03_cross_view.sh @@ -37,15 +37,15 @@ assert_file_exists "untracked.txt exists on dev" "untracked.txt" assert_status_flag "untracked.txt is ? on dev" "?" "untracked.txt" # Create and switch to feature -new_view "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "switch to feature" switch_view "feature" assert_current_view "now on feature" "feature" assert_file_exists "untracked.txt persists on feature" "untracked.txt" assert_status_flag "untracked.txt is ? on feature" "?" "untracked.txt" # Switch back to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_current_view "back on dev" "dev" assert_file_exists "untracked.txt persists after round-trip" "untracked.txt" @@ -71,33 +71,23 @@ create_file "feature_only.txt" "feature content" assert_file_exists "feature_only.txt created on dev" "feature_only.txt" # Step 2: create and switch to feature -new_view "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "switch to feature" switch_view "feature" assert_current_view "on feature" "feature" # Untracked file should still be here assert_file_exists "feature_only.txt exists on feature (untracked)" "feature_only.txt" # Step 3: add + record on feature -assert_success "add feature_only.txt on feature" atomic add feature_only.txt +require_success "add feature_only.txt on feature" atomic add feature_only.txt -rec_out="$(record_change "Add feature_only.txt" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record on feature succeeds" -else - _pass "record on feature completes" -fi +require_success "record on feature succeeds" record_change "Add feature_only.txt" # Verify it's clean on feature -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*feature_only\.txt"; then - _fail "feature_only.txt is clean on feature" "still dirty: $out" -else - _pass "feature_only.txt is clean on feature after record" -fi +assert_status_no_entry "feature_only.txt is clean on feature after record" "feature_only.txt" # Step 4: switch back to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_current_view "back on dev" "dev" # Step 5: file should NOT exist on disk @@ -111,7 +101,7 @@ assert_status_no_entry \ "feature_only.txt" # Step 7: switch back to feature — file should reappear -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_exists "feature_only.txt reappears on feature" "feature_only.txt" assert_file_content "feature_only.txt has correct content" "feature_only.txt" "feature content" @@ -128,16 +118,10 @@ begin_section "Cross-View: Insert from feature to dev" assert_current_view "still on feature" "feature" # Insert from feature → dev -apply_out="$(insert_from_view "feature" "dev" 2>&1)" || true -if echo "$apply_out" | grep -qiE "inserted|success|change"; then - _pass "insert from feature to dev succeeds" -else - # The command might have a different output format - _pass "insert from feature to dev completes" -fi +require_success "insert from feature to dev succeeds" insert_from_view "feature" "dev" # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev after insert" switch_view "dev" assert_current_view "on dev after insert" "dev" # Now the file SHOULD exist on dev @@ -151,12 +135,7 @@ assert_file_content \ "feature content" # Status should show it as clean (recorded on dev via insert) -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*feature_only\.txt"; then - _fail "feature_only.txt is clean on dev after insert" "still dirty: $out" -else - _pass "feature_only.txt is clean on dev after insert" -fi +assert_status_no_entry "feature_only.txt is clean on dev after insert" "feature_only.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Cross-View: Multiple files, partial overlap" @@ -174,21 +153,13 @@ init_repo # Record shared.txt on dev create_file "shared.txt" "shared content" -assert_success "add shared.txt on dev" atomic add shared.txt -record_change "Add shared.txt on dev" >/dev/null 2>&1 || true +require_success "add shared.txt on dev" atomic add shared.txt +require_success "record shared.txt on dev" record_change "Add shared.txt on dev" # Create feature from dev (inherits shared.txt) -out="$(atomic view create feature --from dev 2>&1)" || true -if echo "$out" | grep -qiE "created|view"; then - _pass "create feature view from dev" -else - # Try without --from (create empty, then insert) - new_view "feature" >/dev/null 2>&1 || true - insert_from_view "dev" "feature" >/dev/null 2>&1 || true - _pass "create feature view (fallback method)" -fi +require_success "create feature view from dev" atomic view create feature --from dev -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" assert_current_view "on feature" "feature" # shared.txt should exist on feature (inherited from dev) @@ -196,15 +167,15 @@ assert_file_exists "shared.txt exists on feature" "shared.txt" # Create and record a feature-only file create_file "feature_file.txt" "feature only" -assert_success "add feature_file.txt" atomic add feature_file.txt -record_change "Add feature_file.txt" >/dev/null 2>&1 || true +require_success "add feature_file.txt" atomic add feature_file.txt +require_success "record feature_file.txt" record_change "Add feature_file.txt" # Both files should exist on feature assert_file_exists "shared.txt on feature" "shared.txt" assert_file_exists "feature_file.txt on feature" "feature_file.txt" # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_current_view "on dev" "dev" # shared.txt should exist (it's on dev) @@ -216,7 +187,7 @@ assert_file_not_exists \ "feature_file.txt" # Switch back to feature — both should be there -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_exists "shared.txt on feature (round 2)" "shared.txt" assert_file_exists "feature_file.txt on feature (round 2)" "feature_file.txt" @@ -233,11 +204,11 @@ make_temp_repo "cross-pending-add" init_repo create_file "pending.txt" "pending content" -assert_success "add pending.txt on dev" atomic add pending.txt +require_success "add pending.txt on dev" atomic add pending.txt assert_status_flag "pending.txt is A on dev" "A" "pending.txt" # Attempt switch — should be blocked -new_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" if atomic view switch feature >/dev/null 2>&1; then _fail "switch blocked with pending add on dev" "switch succeeded (rc=0)" else @@ -247,8 +218,8 @@ assert_current_view "still on dev" "dev" assert_file_exists "pending.txt still on dev" "pending.txt" # Record it, then switch should work -record_change "Record pending.txt on dev" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "record pending.txt on dev" record_change "Record pending.txt on dev" +require_success "switch to feature after recording" switch_view "feature" assert_current_view "on feature after recording" "feature" # ═══════════════════════════════════════════════════════════════════════════ @@ -263,19 +234,15 @@ assert_current_view "on feature" "feature" # pending.txt should exist on feature (visible from dev via parent chain # OR via inherited changes) -if [[ -f "pending.txt" ]]; then - _pass "pending.txt visible on feature (inherited)" -else - _pass "pending.txt not on feature (not yet inserted) — acceptable" -fi +assert_file_exists "pending.txt visible on feature (inherited)" "pending.txt" # Record a feature-only file create_file "feature_only.txt" "feature only" -assert_success "add feature_only.txt" atomic add feature_only.txt -record_change "Record feature_only.txt" >/dev/null 2>&1 || true +require_success "add feature_only.txt" atomic add feature_only.txt +require_success "record feature_only.txt" record_change "Record feature_only.txt" # Switch back to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_not_exists "feature_only.txt NOT on dev" "feature_only.txt" # ═══════════════════════════════════════════════════════════════════════════ @@ -288,17 +255,17 @@ begin_section "Cross-View: Nested directories removed on switch" make_temp_repo "cross-nested-cleanup" init_repo -new_view "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "switch to feature" switch_view "feature" create_file "src/pkg/deep/module.rs" "fn deep() {}" -assert_success "add nested file" atomic add "src/pkg/deep/module.rs" -record_change "Add deep nested file" >/dev/null 2>&1 || true +require_success "add nested file" atomic add "src/pkg/deep/module.rs" +require_success "record nested file" record_change "Add deep nested file" assert_dir_exists "src/pkg/deep exists on feature" "src/pkg/deep" # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_not_exists \ "src/pkg/deep/module.rs NOT on dev" \ @@ -310,7 +277,7 @@ assert_dir_not_exists \ "src/pkg/deep" # Switch back — everything reappears -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_exists "src/pkg/deep/module.rs on feature" "src/pkg/deep/module.rs" assert_dir_exists "src/pkg/deep on feature" "src/pkg/deep" @@ -329,28 +296,28 @@ init_repo # Record base.txt on dev create_file "base.txt" "base content" -assert_success "add base.txt" atomic add base.txt -record_change "Add base.txt on dev" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base.txt on dev" record_change "Add base.txt on dev" # Create staging from dev -new_view "staging" >/dev/null 2>&1 || true +require_success "create staging view" new_view "staging" # Insert dev.s changes to staging so it inherits base.txt -insert_from_view "dev" "staging" >/dev/null 2>&1 || true -switch_view "staging" >/dev/null 2>&1 || true +require_success "insert dev into staging" insert_from_view "dev" "staging" +require_success "switch to staging" switch_view "staging" create_file "staging.txt" "staging content" -assert_success "add staging.txt" atomic add staging.txt -record_change "Add staging.txt" >/dev/null 2>&1 || true +require_success "add staging.txt" atomic add staging.txt +require_success "record staging.txt" record_change "Add staging.txt" # Create feature from dev -switch_view "dev" >/dev/null 2>&1 || true -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "feature.txt" "feature content" -assert_success "add feature.txt" atomic add feature.txt -record_change "Add feature.txt" >/dev/null 2>&1 || true +require_success "add feature.txt" atomic add feature.txt +require_success "record feature.txt" record_change "Add feature.txt" # ── Verify on feature ── assert_file_exists "base.txt on feature" "base.txt" @@ -358,19 +325,19 @@ assert_file_exists "feature.txt on feature" "feature.txt" assert_file_not_exists "staging.txt NOT on feature" "staging.txt" # ── Switch to staging ── -switch_view "staging" >/dev/null 2>&1 || true +require_success "switch to staging" switch_view "staging" assert_file_exists "base.txt on staging" "base.txt" assert_file_exists "staging.txt on staging" "staging.txt" assert_file_not_exists "feature.txt NOT on staging" "feature.txt" # ── Switch to dev ── -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_exists "base.txt on dev" "base.txt" assert_file_not_exists "staging.txt NOT on dev" "staging.txt" assert_file_not_exists "feature.txt NOT on dev" "feature.txt" # ── Round-trip back to feature ── -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_exists "base.txt on feature (round 2)" "base.txt" assert_file_exists "feature.txt on feature (round 2)" "feature.txt" assert_file_not_exists "staging.txt NOT on feature (round 2)" "staging.txt" @@ -381,14 +348,9 @@ begin_section "Cross-View: Insert between non-dev views" # # Insert feature.txt from feature to staging -apply_out="$(insert_from_view "feature" "staging" 2>&1)" || true -if echo "$apply_out" | grep -qiE "inserted|success|change"; then - _pass "insert from feature to staging succeeds" -else - _pass "insert from feature to staging completes" -fi +require_success "insert from feature to staging succeeds" insert_from_view "feature" "staging" -switch_view "staging" >/dev/null 2>&1 || true +require_success "switch to staging" switch_view "staging" # staging should now have all three files assert_file_exists "base.txt on staging" "base.txt" @@ -402,53 +364,41 @@ begin_section "Cross-View: Unrecord on feature, then switch" # Workflow: # 1. Switch to feature # 2. Unrecord the feature.txt change -# 3. feature.txt should still be on disk (reverts to added/modified) +# 3. feature.txt should still be on disk and become untracked # 4. Switch to dev -# 5. feature.txt should NOT be on dev (it's not recorded anywhere reachable from dev) +# 5. untracked feature.txt persists without becoming tracked on dev make_temp_repo "cross-unrecord-switch" init_repo # Record base on dev create_file "base.txt" "base" -assert_success "add base.txt" atomic add base.txt -record_change "Add base.txt" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base.txt" record_change "Add base.txt" # Create feature, insert dev, add feature file -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert base history into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "temp_feature.txt" "temporary" -assert_success "add temp_feature.txt" atomic add temp_feature.txt -record_change "Add temp_feature.txt" >/dev/null 2>&1 || true +require_success "add temp_feature.txt" atomic add temp_feature.txt +require_success "record temp_feature.txt" record_change "Add temp_feature.txt" # Unrecord on feature -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord on feature succeeds" -else - _pass "unrecord on feature completes" -fi +require_success "unrecord on feature succeeds" unrecord_last # File should still be on disk assert_file_exists "temp_feature.txt still on disk after unrecord" "temp_feature.txt" +assert_status_flag "temp_feature.txt is untracked after unrecord" "?" "temp_feature.txt" -# Switch to dev — the file should NOT be on dev -switch_view "dev" >/dev/null 2>&1 || true +# Switch to dev without removing the newly untracked working-copy file. +require_success "switch to dev after unrecord" switch_view "dev" +assert_current_view "on dev after unrecord" "dev" -# Since it was unrecorded from feature and never on dev, and it's a pending -# add (no INODES position after unrecord), it will persist as untracked on disk. -# This is acceptable behavior — the file is just an untracked working copy artifact. assert_file_exists "base.txt on dev" "base.txt" -# The temp_feature.txt might or might not be on disk depending on whether -# unrecord removes the INODES position (making it a pending add that persists). -# Either behavior is acceptable. -if [[ -f "temp_feature.txt" ]]; then - _pass "temp_feature.txt persists as untracked after unrecord+switch" -else - _pass "temp_feature.txt cleaned up after unrecord+switch" -fi +assert_file_content "unrecorded file survives switch byte-for-byte" "temp_feature.txt" "temporary" +assert_status_flag "temp_feature.txt stays untracked on dev" "?" "temp_feature.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Cross-View: Untracked files with same name on different views" @@ -463,13 +413,13 @@ init_repo # Record hello.txt on dev create_file "hello.txt" "dev version" -assert_success "add hello.txt on dev" atomic add hello.txt -record_change "Add hello.txt on dev" >/dev/null 2>&1 || true +require_success "add hello.txt on dev" atomic add hello.txt +require_success "record hello.txt on dev" record_change "Add hello.txt on dev" # Create feature -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # hello.txt should exist (inherited from dev) assert_file_exists "hello.txt on feature (inherited)" "hello.txt" @@ -477,15 +427,15 @@ assert_file_content "hello.txt has dev content on feature" "hello.txt" "dev vers # Modify hello.txt on feature and record overwrite_file "hello.txt" "feature version" -record_change "Modify hello.txt on feature" >/dev/null 2>&1 || true +require_success "record feature modification" record_change "Modify hello.txt on feature" # Switch to dev — hello.txt should have dev version -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_exists "hello.txt on dev" "hello.txt" assert_file_content "hello.txt has dev content on dev" "hello.txt" "dev version" # Switch to feature — hello.txt should have feature version -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_exists "hello.txt on feature" "hello.txt" assert_file_content "hello.txt has feature content" "hello.txt" "feature version" @@ -499,22 +449,22 @@ make_temp_repo "cross-deletion" init_repo create_file "victim.txt" "will be deleted on feature" -assert_success "add victim.txt" atomic add victim.txt -record_change "Add victim.txt on dev" >/dev/null 2>&1 || true +require_success "add victim.txt" atomic add victim.txt +require_success "record victim.txt on dev" record_change "Add victim.txt on dev" # Create feature from dev -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Delete on feature rm -f victim.txt -record_change "Delete victim.txt on feature" >/dev/null 2>&1 || true +require_success "record victim.txt deletion" record_change "Delete victim.txt on feature" assert_file_not_exists "victim.txt deleted on feature" "victim.txt" # Switch to dev — victim.txt should still be there -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_file_exists "victim.txt still on dev" "victim.txt" assert_file_content "victim.txt has original content" "victim.txt" "will be deleted on feature" @@ -528,21 +478,21 @@ begin_section "Cross-View: Multiple files recorded in separate changes" make_temp_repo "cross-multi-changes" init_repo -new_view "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "switch to feature" switch_view "feature" # Record three separate changes create_file "change1.txt" "change one" -assert_success "add change1.txt" atomic add change1.txt -record_change "Feature change 1" >/dev/null 2>&1 || true +require_success "add change1.txt" atomic add change1.txt +require_success "record feature change 1" record_change "Feature change 1" create_file "change2.txt" "change two" -assert_success "add change2.txt" atomic add change2.txt -record_change "Feature change 2" >/dev/null 2>&1 || true +require_success "add change2.txt" atomic add change2.txt +require_success "record feature change 2" record_change "Feature change 2" create_file "change3.txt" "change three" -assert_success "add change3.txt" atomic add change3.txt -record_change "Feature change 3" >/dev/null 2>&1 || true +require_success "add change3.txt" atomic add change3.txt +require_success "record feature change 3" record_change "Feature change 3" # All three should exist on feature assert_file_exists "change1.txt on feature" "change1.txt" @@ -550,13 +500,13 @@ assert_file_exists "change2.txt on feature" "change2.txt" assert_file_exists "change3.txt on feature" "change3.txt" # Switch to dev — none should exist -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_not_exists "change1.txt NOT on dev" "change1.txt" assert_file_not_exists "change2.txt NOT on dev" "change2.txt" assert_file_not_exists "change3.txt NOT on dev" "change3.txt" # Insert all from feature to dev -insert_from_view "feature" "dev" >/dev/null 2>&1 || true +require_success "insert feature changes into dev" insert_from_view "feature" "dev" # Now all three should exist on dev assert_file_exists "change1.txt on dev after insert" "change1.txt" @@ -574,30 +524,28 @@ init_repo # Setup: dev has dev.txt, feature has feature.txt create_file "dev.txt" "dev" -assert_success "add dev.txt" atomic add dev.txt -record_change "Add dev.txt" >/dev/null 2>&1 || true +require_success "add dev.txt" atomic add dev.txt +require_success "record dev.txt" record_change "Add dev.txt" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "feature.txt" "feature" -assert_success "add feature.txt" atomic add feature.txt -record_change "Add feature.txt" >/dev/null 2>&1 || true +require_success "add feature.txt" atomic add feature.txt +require_success "record feature.txt" record_change "Add feature.txt" # Rapid switch 5 times for i in $(seq 1 5); do - switch_view "dev" >/dev/null 2>&1 || true + require_success "switch to dev (iteration $i)" switch_view "dev" assert_file_exists "dev.txt on dev (iteration $i)" "dev.txt" assert_file_not_exists "feature.txt NOT on dev (iteration $i)" "feature.txt" - switch_view "feature" >/dev/null 2>&1 || true + require_success "switch to feature (iteration $i)" switch_view "feature" assert_file_exists "dev.txt on feature (iteration $i)" "dev.txt" assert_file_exists "feature.txt on feature (iteration $i)" "feature.txt" done -_pass "rapid switching stress test: 5 round-trips completed" - # ═══════════════════════════════════════════════════════════════════════════ begin_section "Cross-View: Untracked + tracked coexistence" # ═══════════════════════════════════════════════════════════════════════════ @@ -610,16 +558,16 @@ init_repo # Create and record tracked.txt on dev create_file "tracked.txt" "tracked on dev" -assert_success "add tracked.txt" atomic add tracked.txt -record_change "Add tracked.txt on dev" >/dev/null 2>&1 || true +require_success "add tracked.txt" atomic add tracked.txt +require_success "record tracked.txt on dev" record_change "Add tracked.txt on dev" # Create an untracked file create_file "notes.txt" "my personal notes" # Create feature -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Both should exist assert_file_exists "tracked.txt on feature" "tracked.txt" @@ -627,11 +575,11 @@ assert_file_exists "notes.txt on feature (untracked persists)" "notes.txt" # Record a different file on feature create_file "feature_code.txt" "feature code" -assert_success "add feature_code.txt" atomic add feature_code.txt -record_change "Add feature_code.txt" >/dev/null 2>&1 || true +require_success "add feature_code.txt" atomic add feature_code.txt +require_success "record feature_code.txt" record_change "Add feature_code.txt" # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" # tracked.txt should exist (on dev), feature_code.txt should NOT, # notes.txt should persist (untracked) @@ -649,30 +597,30 @@ begin_section "Cross-View: Insert then unrecord on target" make_temp_repo "cross-insert-unrecord" init_repo -new_view "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "switch to feature" switch_view "feature" create_file "applied.txt" "applied content" -assert_success "add applied.txt" atomic add applied.txt -record_change "Add applied.txt on feature" >/dev/null 2>&1 || true +require_success "add applied.txt" atomic add applied.txt +require_success "record applied.txt on feature" record_change "Add applied.txt on feature" # Insert to dev -insert_from_view "feature" "dev" >/dev/null 2>&1 || true +require_success "insert feature into dev" insert_from_view "feature" "dev" -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_exists "applied.txt on dev after insert" "applied.txt" # Unrecord on dev -unrecord_last >/dev/null 2>&1 || true +require_success "unrecord applied change on dev" unrecord_last # After unrecording the insert, the file's change is no longer on dev # The file might persist on disk as an untracked artifact (since unrecord # doesn't delete files from disk), but it should not be tracked on dev. # When we switch away and back, it would be cleaned up properly. -_pass "unrecord applied change on dev completes" +assert_status_flag "unrecorded applied.txt is untracked on dev" "?" "applied.txt" # Verify feature still has it -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature after unrecord" switch_view "feature" assert_file_exists "applied.txt still on feature" "applied.txt" # ═══════════════════════════════════════════════════════════════════════════ @@ -684,14 +632,14 @@ init_repo # Record a file on dev create_file "dev_file.txt" "dev only" -assert_success "add dev_file.txt" atomic add dev_file.txt -record_change "Add dev_file.txt" >/dev/null 2>&1 || true +require_success "add dev_file.txt" atomic add dev_file.txt +require_success "record dev_file.txt" record_change "Add dev_file.txt" # Create a child view (parented on dev) # In the ambient graph model, child views see parent's files through # the filter chain — there are no truly "empty" views when parented. -new_view "child" >/dev/null 2>&1 || true -switch_view "child" >/dev/null 2>&1 || true +require_success "create child view" new_view "child" +require_success "switch to child" switch_view "child" # dev_file.txt SHOULD exist on the child view — it inherits dev's # changes through the parent filter chain (ambient graph model). @@ -700,7 +648,7 @@ assert_file_exists \ "dev_file.txt" # Switch back to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_file_exists "dev_file.txt back on dev" "dev_file.txt" # ═══════════════════════════════════════════════════════════════════════════ @@ -714,42 +662,28 @@ init_repo # Setup: dev has dev.txt (clean), feature has feature.txt (clean) create_file "dev.txt" "dev" -assert_success "add dev.txt" atomic add dev.txt -record_change "Add dev.txt" >/dev/null 2>&1 || true +require_success "add dev.txt" atomic add dev.txt +require_success "record dev.txt" record_change "Add dev.txt" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "feature.txt" "feature" -assert_success "add feature.txt" atomic add feature.txt -record_change "Add feature.txt" >/dev/null 2>&1 || true +require_success "add feature.txt" atomic add feature.txt +require_success "record feature.txt" record_change "Add feature.txt" # On feature: both should be clean, status should not mention feature.txt or # dev.txt as dirty -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*dev\.txt"; then - _fail "dev.txt clean on feature status" "shown as dirty" -else - _pass "dev.txt clean on feature status" -fi -if echo "$out" | grep -qE "^[MADU?].*feature\.txt"; then - _fail "feature.txt clean on feature status" "shown as dirty" -else - _pass "feature.txt clean on feature status" -fi +assert_status_no_entry "dev.txt clean on feature status" "dev.txt" +assert_status_no_entry "feature.txt clean on feature status" "feature.txt" # Switch to dev: feature.txt should not appear AT ALL in status -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_status_no_entry "feature.txt not in dev status" "feature.txt" -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*dev\.txt"; then - _fail "dev.txt clean on dev status" "shown as dirty" -else - _pass "dev.txt clean on dev status" -fi +assert_status_no_entry "dev.txt clean on dev status" "dev.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Cross-View: File modified on both views independently" @@ -763,31 +697,31 @@ init_repo # Record shared.txt on dev create_file "shared.txt" "original" -assert_success "add shared.txt" atomic add shared.txt -record_change "Add shared.txt" >/dev/null 2>&1 || true +require_success "add shared.txt" atomic add shared.txt +require_success "record shared.txt" record_change "Add shared.txt" # Create feature from dev -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" # Modify on dev overwrite_file "shared.txt" "dev modification" -record_change "Modify shared.txt on dev" >/dev/null 2>&1 || true +require_success "record dev modification" record_change "Modify shared.txt on dev" # Modify on feature -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" overwrite_file "shared.txt" "feature modification" -record_change "Modify shared.txt on feature" >/dev/null 2>&1 || true +require_success "record feature modification" record_change "Modify shared.txt on feature" # Check feature version assert_file_content "shared.txt has feature content" "shared.txt" "feature modification" # Switch to dev — should have dev version -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_content "shared.txt has dev content" "shared.txt" "dev modification" # Switch back to feature -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_content "shared.txt has feature content (round 2)" "shared.txt" "feature modification" # ═══════════════════════════════════════════════════════════════════════════ @@ -880,12 +814,7 @@ assert_file_exists "beta.txt exists on shared-main" "beta.txt" assert_file_exists "gamma.txt exists on shared-main" "gamma.txt" # Fork a deletable draft from the shared view and record one local edit. -draft_out="$(atomic view create local-edit --draft --from shared-main --switch 2>&1)" || true -if echo "$draft_out" | grep -qiE "created|view|switched"; then - _pass "create draft local-edit from shared-main" -else - _pass "create draft local-edit from shared-main completes" -fi +require_success "create draft local-edit from shared-main" atomic view create local-edit --draft --from shared-main --switch assert_current_view "on local-edit draft" "local-edit" assert_file_exists "alpha.txt exists on local-edit" "alpha.txt" @@ -1000,20 +929,10 @@ assert_file_exists "journey.txt on feature before add" "journey.txt" assert_success "add journey.txt on feature" atomic add journey.txt assert_status_flag "journey.txt is A on feature" "A" "journey.txt" -rec_out="$(record_change "Record journey.txt on feature")" -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record journey.txt on feature succeeds" -else - _pass "record journey.txt on feature completes" -fi +require_success "record journey.txt on feature succeeds" record_change "Record journey.txt on feature" # Verify clean on feature after record -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU].*journey\.txt"; then - _fail "journey.txt clean on feature after record" "still dirty: $out" -else - _pass "journey.txt clean on feature after record" -fi +assert_status_no_entry "journey.txt clean on feature after record" "journey.txt" # ── Step 6: Switch to dev — file is GONE ──────────────────────────────── @@ -1057,12 +976,7 @@ assert_file_content "journey.txt has correct content on feature" "journey.txt" " # ── Step 9: Insert from feature to dev ─────────────────────────────────── -apply_out="$(insert_from_view "feature" "dev")" -if echo "$apply_out" | grep -qiE "inserted|success|change"; then - _pass "insert journey.txt from feature to dev" -else - _pass "insert from feature to dev completes" -fi +require_success "insert journey.txt from feature to dev" insert_from_view "feature" "dev" # ── Step 10: Switch to dev — file exists ──────────────────────────────── @@ -1089,12 +1003,7 @@ assert_file_exists "base.txt still on staging" "base.txt" # ── Step 12: Insert from dev to staging ────────────────────────────────── -apply_out2="$(insert_from_view "dev" "staging")" -if echo "$apply_out2" | grep -qiE "inserted|success|change"; then - _pass "insert journey.txt from dev to staging" -else - _pass "insert from dev to staging completes" -fi +require_success "insert journey.txt from dev to staging" insert_from_view "dev" "staging" # ── Step 13: Switch to staging — file exists ──────────────────────────── @@ -1392,12 +1301,7 @@ assert_file_content "draft_a.txt intact after blocked switch" "draft_a.txt" "Dra assert_file_content "draft_b.txt intact after blocked switch" "draft_b.txt" "Draft file beta content" # Step 3: --force should override the block -switch_out="$(atomic view switch dev --force 2>&1)" -if echo "$switch_out" | grep -qi "switched\|materiali"; then - _pass "switch --force overrides the block" -else - _pass "switch --force completes" -fi +require_success "switch --force overrides the block" atomic view switch dev --force assert_current_view "on dev after forced switch" "dev" # Step 4: Switch back (clean working copy, should work) @@ -1476,21 +1380,21 @@ begin_section "Cross-View: Multiple round-trips with pending files" make_temp_repo "cross-roundtrip-stress" init_repo -new_view "wip" >/dev/null 2>&1 || true -switch_view "wip" >/dev/null 2>&1 || true +require_success "create wip view" new_view "wip" +require_success "switch to wip" switch_view "wip" create_file "fragile.txt" "This content must not change" -assert_success "add fragile.txt" atomic add fragile.txt -record_change "Record fragile.txt on wip" >/dev/null 2>&1 || true +require_success "add fragile.txt" atomic add fragile.txt +require_success "record fragile.txt on wip" record_change "Record fragile.txt on wip" # Round-trip 3 times, recording on dev each time for i in 1 2 3; do - switch_view "dev" >/dev/null 2>&1 || true + require_success "switch to dev (iteration $i)" switch_view "dev" create_file "dev_iter_${i}.txt" "dev iteration ${i}" - assert_success "add dev_iter_${i}.txt" atomic add "dev_iter_${i}.txt" - record_change "Dev iteration ${i}" >/dev/null 2>&1 || true + require_success "add dev_iter_${i}.txt" atomic add "dev_iter_${i}.txt" + require_success "record dev iteration $i" record_change "Dev iteration ${i}" - switch_view "wip" >/dev/null 2>&1 || true + require_success "switch to wip (iteration $i)" switch_view "wip" assert_file_exists "fragile.txt exists (iteration $i)" "fragile.txt" assert_file_content \ "fragile.txt content intact (iteration $i)" \ @@ -1498,8 +1402,6 @@ for i in 1 2 3; do "This content must not change" done -_pass "3 round-trips with recorded files: content preserved" - # ═════════════════════════════════════════════════════════════════════ begin_section "Cross-View: --stash auto-saves changes before switch" # ═════════════════════════════════════════════════════════════════════ @@ -1516,12 +1418,12 @@ init_repo # Record a base file on dev create_file "work.txt" "original content" -assert_success "add work.txt" atomic add work.txt -record_change "Add work.txt" >/dev/null 2>&1 || true +require_success "add work.txt" atomic add work.txt +require_success "record work.txt" record_change "Add work.txt" # Create feature view -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" # Modify the file (dirty working copy) overwrite_file "work.txt" "modified content" @@ -1534,11 +1436,7 @@ else fi # Switch with --stash -if atomic view switch feature --stash >/dev/null 2>&1; then - _pass "switch --stash succeeds" -else - _fail "switch --stash succeeds" "stash+switch failed" -fi +require_success "switch --stash succeeds" atomic view switch feature --stash assert_current_view "on feature after stash+switch" "feature" # Verify stash was created @@ -1553,19 +1451,14 @@ fi assert_file_content "work.txt has original content on feature" "work.txt" "original content" # Switch back to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_current_view "back on dev" "dev" # work.txt should have original content (clean state after stash) assert_file_content "work.txt is clean on dev (stashed)" "work.txt" "original content" # Pop the stash — modifications restored -pop_out="$(atomic stash pop 2>&1)" -if echo "$pop_out" | grep -qi "applied\|stash@"; then - _pass "stash pop succeeds" -else - _pass "stash pop completes" -fi +require_success "stash pop succeeds" atomic stash pop # CRITICAL: the stashed modifications are back assert_file_content "work.txt has modified content after pop" "work.txt" "modified content" @@ -1582,35 +1475,27 @@ make_temp_repo "cross-stash-added" init_repo # Create feature view -new_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" # Add new files on dev without recording create_file "new_a.txt" "new file alpha" create_file "new_b.txt" "new file beta" -assert_success "add new_a.txt" atomic add new_a.txt -assert_success "add new_b.txt" atomic add new_b.txt +require_success "add new_a.txt" atomic add new_a.txt +require_success "add new_b.txt" atomic add new_b.txt assert_status_flag "new_a.txt is Added" "A" "new_a.txt" # Switch with --stash -if atomic view switch feature --stash >/dev/null 2>&1; then - _pass "switch --stash with added files succeeds" -else - _fail "switch --stash with added files succeeds" "stash+switch failed" -fi +require_success "switch --stash with added files succeeds" atomic view switch feature --stash assert_current_view "on feature" "feature" # Files may still be on disk (added-but-unrecorded files aren't tracked # in the graph, so materialize doesn't remove them). The stash saved # their content — what matters is that pop restores them correctly. -if [[ -f "new_a.txt" ]]; then - _pass "new_a.txt persists on feature (unrecorded, expected)" -else - _pass "new_a.txt cleaned up on feature" -fi +# The file's presence here is not an assertion; restoration is checked below. # Switch back and pop -switch_view "dev" >/dev/null 2>&1 || true -atomic stash pop >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" +require_success "restore stashed added files" atomic stash pop # Files should be back with original content assert_file_exists "new_a.txt restored after pop" "new_a.txt" @@ -1630,29 +1515,29 @@ init_repo # Base state on dev create_file "base.txt" "base" -assert_success "add base.txt" atomic add base.txt -record_change "Add base" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base" record_change "Add base" # Create feature from dev -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" # Dirty up dev: modify existing + add new overwrite_file "base.txt" "modified base" create_file "wip.txt" "work in progress" -assert_success "add wip.txt" atomic add wip.txt +require_success "add wip.txt" atomic add wip.txt # Stash and switch -atomic view switch feature --stash >/dev/null 2>&1 +require_success "stash and switch to feature" atomic view switch feature --stash assert_current_view "on feature" "feature" # Do work on feature create_file "feature_work.txt" "feature work" -assert_success "add feature_work.txt" atomic add feature_work.txt -record_change "Feature work" >/dev/null 2>&1 || true +require_success "add feature_work.txt" atomic add feature_work.txt +require_success "record feature work" record_change "Feature work" # Switch back to dev (clean, no block) -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_current_view "back on dev" "dev" # base.txt should be clean (original), wip.txt should not exist @@ -1660,15 +1545,13 @@ assert_file_content "base.txt is clean before pop" "base.txt" "base" assert_file_not_exists "wip.txt not on disk before pop" "wip.txt" # Pop stash -atomic stash pop >/dev/null 2>&1 || true +require_success "restore stashed work" atomic stash pop # CRITICAL: everything restored assert_file_content "base.txt has stashed modification" "base.txt" "modified base" assert_file_exists "wip.txt restored after pop" "wip.txt" assert_file_content "wip.txt has stashed content" "wip.txt" "work in progress" -_pass "full stash round-trip: all changes preserved" - # ═════════════════════════════════════════════════════════════════════ print_summary diff --git a/tests/harness/04_directories.sh b/tests/harness/04_directories.sh index e8a975a3..921efacd 100755 --- a/tests/harness/04_directories.sh +++ b/tests/harness/04_directories.sh @@ -38,54 +38,23 @@ begin_section "Directory: Add directory recursively → status(added)" # ═══════════════════════════════════════════════════════════════════════════ # Add the entire directory (should recurse) -add_out="$(atomic add mydir 2>&1)" || true -if [[ $? -eq 0 ]] || echo "$add_out" | grep -qiE "added|tracked"; then - _pass "add mydir recursively" -else - # Try adding files individually as fallback - atomic add mydir/a.txt >/dev/null 2>&1 || true - atomic add mydir/b.txt >/dev/null 2>&1 || true - _pass "add mydir files individually (fallback)" -fi +require_success "add mydir recursively" atomic add mydir # Both files should now be added -out="$(get_status_short)" -added=0 for f in mydir/a.txt mydir/b.txt; do - if echo "$out" | grep -qE "^A.*${f}"; then - added=$((added + 1)) - fi + assert_status_flag "$f is added" "A" "$f" done -if [[ $added -ge 2 ]]; then - _pass "both files in mydir show as added ($added/2)" -else - _pass "directory add result ($added/2 files added)" -fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Record directory → status(clean)" # ═══════════════════════════════════════════════════════════════════════════ -rec_out="$(record_change "Add mydir with contents" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record directory succeeds" -else - _pass "record directory completes" -fi +require_success "record directory succeeds" record_change "Add mydir with contents" # All files should be clean -out="$(get_status_short)" -dirty=0 for f in mydir/a.txt mydir/b.txt; do - if echo "$out" | grep -qE "^[MADU?].*$(echo "$f" | sed 's/\//\\\//g')"; then - dirty=$((dirty + 1)) - fi + assert_status_no_entry "$f is clean after record" "$f" done -if [[ $dirty -eq 0 ]]; then - _pass "all directory files clean after record" -else - _fail "all directory files clean after record" "$dirty files still dirty" -fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Modify file inside directory" @@ -96,14 +65,9 @@ overwrite_file "mydir/a.txt" "modified a" assert_status_flag "mydir/a.txt is modified" "M" "mydir/a.txt" # b.txt should still be clean -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MAD].*mydir/b\.txt"; then - _fail "mydir/b.txt still clean" "shown as dirty" -else - _pass "mydir/b.txt still clean after modifying sibling" -fi +assert_status_no_entry "mydir/b.txt still clean after modifying sibling" "mydir/b.txt" -record_change "Modify mydir/a.txt" >/dev/null 2>&1 || true +require_success "record mydir/a.txt modification" record_change "Modify mydir/a.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Add new file inside existing directory" @@ -113,18 +77,13 @@ create_file "mydir/c.txt" "new file c" assert_status_flag "mydir/c.txt is untracked" "?" "mydir/c.txt" -assert_success "add mydir/c.txt" atomic add mydir/c.txt +require_success "add mydir/c.txt" atomic add mydir/c.txt assert_status_flag "mydir/c.txt is added" "A" "mydir/c.txt" -record_change "Add mydir/c.txt" >/dev/null 2>&1 || true +require_success "record mydir/c.txt" record_change "Add mydir/c.txt" -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*mydir/c\.txt"; then - _fail "mydir/c.txt is clean after record" "still dirty" -else - _pass "mydir/c.txt is clean after record" -fi +assert_status_no_entry "mydir/c.txt is clean after record" "mydir/c.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Delete file inside directory" @@ -134,16 +93,9 @@ rm -f mydir/b.txt assert_file_not_exists "mydir/b.txt removed from disk" "mydir/b.txt" -out="$(get_status_short)" -if echo "$out" | grep -qE "^D.*mydir/b\.txt"; then - _pass "mydir/b.txt shows as deleted" -elif echo "$out" | grep -qF "mydir/b.txt"; then - _pass "mydir/b.txt appears in status after deletion" -else - _fail "mydir/b.txt shows as deleted" "not found in status" -fi +assert_status_flag "mydir/b.txt shows as deleted" "D" "mydir/b.txt" -record_change "Delete mydir/b.txt" >/dev/null 2>&1 || true +require_success "record mydir/b.txt deletion" record_change "Delete mydir/b.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Delete all files → directory remains (or not)" @@ -155,34 +107,21 @@ init_repo create_file "rmdir/x.txt" "x" create_file "rmdir/y.txt" "y" -assert_success "add rmdir/x.txt" atomic add rmdir/x.txt -assert_success "add rmdir/y.txt" atomic add rmdir/y.txt -record_change "Add rmdir files" >/dev/null 2>&1 || true +require_success "add rmdir/x.txt" atomic add rmdir/x.txt +require_success "add rmdir/y.txt" atomic add rmdir/y.txt +require_success "record rmdir files" record_change "Add rmdir files" # Delete both files rm -f rmdir/x.txt rmdir/y.txt -out="$(get_status_short)" -deleted=0 for f in rmdir/x.txt rmdir/y.txt; do - if echo "$out" | grep -qE "^D.*$(echo "$f" | sed 's/\//\\\//g')"; then - deleted=$((deleted + 1)) - fi + assert_status_flag "$f shows as deleted" "D" "$f" done -if [[ $deleted -ge 1 ]]; then - _pass "deleted files show as D ($deleted/2)" -else - _pass "deleted files status ($deleted found)" -fi -record_change "Delete all files in rmdir" >/dev/null 2>&1 || true +require_success "record rmdir deletions" record_change "Delete all files in rmdir" # After recording the deletions, the empty directory may or may not persist -if [[ -d "rmdir" ]]; then - _pass "empty rmdir persists on disk (implementation choice)" -else - _pass "empty rmdir cleaned up (implementation choice)" -fi +# Directory presence is not asserted here; file deletions are verified above. # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Deeply nested directories" @@ -196,22 +135,12 @@ create_file "a/b/c/d/e/deep.txt" "very deep" assert_file_exists "deeply nested file exists" "a/b/c/d/e/deep.txt" assert_dir_exists "deep dir chain exists" "a/b/c/d/e" -assert_success "add deep file" atomic add "a/b/c/d/e/deep.txt" +require_success "add deep file" atomic add "a/b/c/d/e/deep.txt" assert_status_flag "deep file is added" "A" "a/b/c/d/e/deep.txt" -rec_out="$(record_change "Add deeply nested file" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record deeply nested file succeeds" -else - _pass "record deeply nested file completes" -fi +require_success "record deeply nested file succeeds" record_change "Add deeply nested file" -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*a/b/c/d/e/deep\.txt"; then - _fail "deep file is clean after record" "still dirty" -else - _pass "deep file is clean after record" -fi +assert_status_no_entry "deep file is clean after record" "a/b/c/d/e/deep.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Add explicit empty directory" @@ -224,21 +153,10 @@ create_dir "empty_mod" assert_dir_exists "empty_mod exists" "empty_mod" -# Try to add as explicit directory -add_out="$(atomic add --directory empty_mod 2>&1)" || true -if [[ $? -eq 0 ]] || echo "$add_out" | grep -qiE "added|tracked|director"; then - _pass "add --directory empty_mod" -else - _skip "add --directory empty_mod" "explicit directory tracking may not be supported" -fi +require_success "add --directory empty_mod" atomic add --directory empty_mod # Record -rec_out="$(record_change "Add empty directory" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change|nothing"; then - _pass "record empty directory completes" -else - _pass "record empty directory does not crash" -fi +require_success "record empty directory" record_change "Add empty directory" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Multiple directories at same level" @@ -259,20 +177,11 @@ for f in dirA/file1.txt dirB/file2.txt dirC/file3.txt; do assert_status_flag "$f is added" "A" "$f" done -record_change "Add three sibling directories" >/dev/null 2>&1 || true +require_success "record three sibling directories" record_change "Add three sibling directories" -out="$(get_status_short)" -dirty=0 for f in dirA/file1.txt dirB/file2.txt dirC/file3.txt; do - if echo "$out" | grep -qE "^[MADU?].*$(echo "$f" | sed 's/\//\\\//g')"; then - dirty=$((dirty + 1)) - fi + assert_status_no_entry "$f is clean after record" "$f" done -if [[ $dirty -eq 0 ]]; then - _pass "all sibling directory files clean after record" -else - _fail "all sibling directory files clean" "$dirty still dirty" -fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Directory with mixed file types" @@ -289,15 +198,9 @@ printf '\x89PNG\r\n\x1a\n' > mixed/image.png # fake PNG header assert_success "add mixed/readme.md" atomic add mixed/readme.md assert_success "add mixed/code.rs" atomic add mixed/code.rs assert_success "add mixed/data.json" atomic add mixed/data.json -add_out="$(atomic add mixed/image.png 2>&1)" || true -if [[ $? -eq 0 ]]; then - _pass "add binary file in directory" -else - _skip "add binary file in directory" "binary files may be skipped" -fi +require_success "add binary file in directory" atomic add mixed/image.png -record_change "Add mixed directory" >/dev/null 2>&1 || true -_pass "record directory with mixed file types completes" +require_success "record directory with mixed file types" record_change "Add mixed directory" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Unrecord directory change" @@ -311,35 +214,22 @@ create_file "undodir/beta.txt" "beta" assert_success "add undodir/alpha.txt" atomic add undodir/alpha.txt assert_success "add undodir/beta.txt" atomic add undodir/beta.txt -record_change "Add undodir" >/dev/null 2>&1 || true +require_success "record undodir" record_change "Add undodir" # Verify clean -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*undodir/"; then - _fail "undodir files clean before unrecord" "still dirty" -else - _pass "undodir files clean before unrecord" -fi +assert_status_no_entry "undodir/alpha.txt clean before unrecord" "undodir/alpha.txt" +assert_status_no_entry "undodir/beta.txt clean before unrecord" "undodir/beta.txt" # Unrecord -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord directory change succeeds" -else - _pass "unrecord directory change completes" -fi +require_success "unrecord directory change succeeds" unrecord_last # Files should still be on disk assert_file_exists "undodir/alpha.txt still on disk" "undodir/alpha.txt" assert_file_exists "undodir/beta.txt still on disk" "undodir/beta.txt" -# Files should revert to added status -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MA].*undodir/"; then - _pass "undodir files revert to added/modified after unrecord" -else - _pass "undodir status after unrecord (implementation-dependent)" -fi +# The original additions were unrecorded, so both files are untracked. +assert_status_flag "undodir/alpha.txt is untracked" "?" "undodir/alpha.txt" +assert_status_flag "undodir/beta.txt is untracked" "?" "undodir/beta.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Cross-view directory isolation" @@ -350,15 +240,15 @@ begin_section "Directory: Cross-view directory isolation" make_temp_repo "dir-cross-view" init_repo -new_view "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "switch to feature" switch_view "feature" create_file "feature_dir/module.rs" "mod feature;" create_file "feature_dir/types.rs" "struct Foo;" assert_success "add feature_dir/module.rs" atomic add feature_dir/module.rs assert_success "add feature_dir/types.rs" atomic add feature_dir/types.rs -record_change "Add feature directory" >/dev/null 2>&1 || true +require_success "record feature directory" record_change "Add feature directory" # Verify on feature assert_dir_exists "feature_dir on feature" "feature_dir" @@ -366,7 +256,7 @@ assert_file_exists "feature_dir/module.rs on feature" "feature_dir/module.rs" assert_file_exists "feature_dir/types.rs on feature" "feature_dir/types.rs" # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" # Directory and contents should NOT exist assert_file_not_exists "feature_dir/module.rs NOT on dev" "feature_dir/module.rs" @@ -374,7 +264,7 @@ assert_file_not_exists "feature_dir/types.rs NOT on dev" "feature_dir/types.rs" assert_dir_not_exists "feature_dir NOT on dev" "feature_dir" # Switch back to feature — everything reappears -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_dir_exists "feature_dir on feature (round 2)" "feature_dir" assert_file_exists "feature_dir/module.rs on feature (round 2)" "feature_dir/module.rs" assert_file_exists "feature_dir/types.rs on feature (round 2)" "feature_dir/types.rs" @@ -389,20 +279,20 @@ begin_section "Directory: Cross-view nested directory cleanup" make_temp_repo "dir-cross-nested-cleanup" init_repo -new_view "deep-feature" >/dev/null 2>&1 || true -switch_view "deep-feature" >/dev/null 2>&1 || true +require_success "create deep-feature view" new_view "deep-feature" +require_success "switch to deep-feature" switch_view "deep-feature" create_file "src/services/auth/handler.rs" "fn auth() {}" create_file "src/services/auth/middleware.rs" "fn middleware() {}" assert_success "add handler.rs" atomic add src/services/auth/handler.rs assert_success "add middleware.rs" atomic add src/services/auth/middleware.rs -record_change "Add auth service" >/dev/null 2>&1 || true +require_success "record auth service" record_change "Add auth service" assert_dir_exists "src/services/auth on deep-feature" "src/services/auth" # Switch to dev — entire src/services/auth tree should vanish -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_not_exists "handler.rs NOT on dev" "src/services/auth/handler.rs" assert_file_not_exists "middleware.rs NOT on dev" "src/services/auth/middleware.rs" @@ -411,7 +301,7 @@ assert_dir_not_exists "src/services/auth NOT on dev" "src/services/auth" assert_dir_not_exists "src/services NOT on dev" "src/services" # Switch back -switch_view "deep-feature" >/dev/null 2>&1 || true +require_success "switch back to deep-feature" switch_view "deep-feature" assert_dir_exists "src/services/auth reappears" "src/services/auth" assert_file_exists "handler.rs reappears" "src/services/auth/handler.rs" @@ -420,9 +310,9 @@ begin_section "Directory: Insert directory from feature to dev" # ═══════════════════════════════════════════════════════════════════════════ # Continuing from previous -insert_from_view "deep-feature" "dev" >/dev/null 2>&1 || true +require_success "insert deep-feature into dev" insert_from_view "deep-feature" "dev" -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev after insert" switch_view "dev" assert_dir_exists "src/services/auth on dev after insert" "src/services/auth" assert_file_exists "handler.rs on dev after insert" "src/services/auth/handler.rs" @@ -444,16 +334,9 @@ assert_success "add partial/top.txt only" atomic add partial/top.txt assert_status_flag "partial/top.txt is added" "A" "partial/top.txt" # inner.txt should still be untracked -out="$(get_status_short)" -if echo "$out" | grep -qE "^\?.*partial/sub/inner\.txt"; then - _pass "partial/sub/inner.txt still untracked" -elif echo "$out" | grep -qE "^A.*partial/sub/inner\.txt"; then - _fail "partial/sub/inner.txt should be untracked" "shown as added (recursive add when not expected)" -else - _pass "partial add only includes specified file" -fi +assert_status_flag "partial/sub/inner.txt still untracked" "?" "partial/sub/inner.txt" -record_change "Add only top.txt" >/dev/null 2>&1 || true +require_success "record only top.txt" record_change "Add only top.txt" # inner.txt should still be untracked assert_status_flag "partial/sub/inner.txt still untracked after record" "?" "partial/sub/inner.txt" @@ -478,33 +361,15 @@ assert_success "add db/migrations/001.sql" atomic add db/migrations/001.sql assert_success "add config/app.toml" atomic add config/app.toml # All five should be added -added=0 -out="$(get_status_short)" for f in api/routes.rs api/handlers.rs db/schema.rs db/migrations/001.sql config/app.toml; do - if echo "$out" | grep -qE "^A.*$(echo "$f" | sed 's/\//\\\//g')"; then - added=$((added + 1)) - fi + assert_status_flag "$f is added" "A" "$f" done -if [[ $added -ge 4 ]]; then - _pass "all multi-dir files show as added ($added/5)" -else - _pass "multi-dir add result ($added/5 added)" -fi -record_change "Add files across api, db, config" >/dev/null 2>&1 || true +require_success "record files across api, db, config" record_change "Add files across api, db, config" -out="$(get_status_short)" -dirty=0 for f in api/routes.rs api/handlers.rs db/schema.rs db/migrations/001.sql config/app.toml; do - if echo "$out" | grep -qE "^[MADU?].*$(echo "$f" | sed 's/\//\\\//g')"; then - dirty=$((dirty + 1)) - fi + assert_status_no_entry "$f is clean after record" "$f" done -if [[ $dirty -eq 0 ]]; then - _pass "all multi-dir files clean after record" -else - _fail "all multi-dir files clean" "$dirty still dirty" -fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Remove directory from tracking" @@ -554,33 +419,17 @@ assert_success "add child1 file" atomic add parent/child1/file1.txt assert_success "add child2 file" atomic add parent/child2/file2.txt assert_success "add child3 file" atomic add parent/child3/file3.txt -record_change "Add parent with children" >/dev/null 2>&1 || true +require_success "record parent with children" record_change "Add parent with children" # Verify all clean -out="$(get_status_short)" -dirty=0 for f in parent/child1/file1.txt parent/child2/file2.txt parent/child3/file3.txt; do - if echo "$out" | grep -qE "^[MADU?].*$(echo "$f" | sed 's/\//\\\//g')"; then - dirty=$((dirty + 1)) - fi + assert_status_no_entry "$f is clean" "$f" done -if [[ $dirty -eq 0 ]]; then - _pass "all child directory files clean" -else - _fail "all child directory files clean" "$dirty still dirty" -fi # Delete one child directory's contents rm -f parent/child2/file2.txt -out="$(get_status_short)" -if echo "$out" | grep -qE "^D.*parent/child2/file2\.txt"; then - _pass "deleted child2/file2.txt shows as deleted" -elif echo "$out" | grep -qF "parent/child2"; then - _pass "child2 deletion appears in status" -else - _fail "child2/file2.txt shows as deleted" "not in status" -fi +assert_status_flag "deleted child2/file2.txt shows as deleted" "D" "parent/child2/file2.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Hidden directory (.hidden)" @@ -595,12 +444,7 @@ assert_dir_exists ".hidden exists" ".hidden" assert_file_exists ".hidden/secret.txt exists" ".hidden/secret.txt" # Try to add — hidden dirs may be excluded by default -add_out="$(atomic add .hidden/secret.txt 2>&1)" || true -if [[ $? -eq 0 ]]; then - _pass "add file in hidden directory" -else - _skip "add file in hidden directory" "hidden dirs may be ignored" -fi +assert_success "add file in hidden directory" atomic add .hidden/secret.txt # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Cross-view with shared and unique directories" @@ -615,18 +459,18 @@ init_repo # Record src/core on dev create_file "src/core/engine.rs" "fn engine() {}" -assert_success "add src/core/engine.rs" atomic add src/core/engine.rs -record_change "Add core engine" >/dev/null 2>&1 || true +require_success "add src/core/engine.rs" atomic add src/core/engine.rs +require_success "record core engine" record_change "Add core engine" # Create feature from dev -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Add feature-specific directory create_file "src/feature/widget.rs" "fn widget() {}" -assert_success "add src/feature/widget.rs" atomic add src/feature/widget.rs -record_change "Add feature widget" >/dev/null 2>&1 || true +require_success "add src/feature/widget.rs" atomic add src/feature/widget.rs +require_success "record feature widget" record_change "Add feature widget" # On feature: both dirs exist assert_dir_exists "src/core on feature" "src/core" @@ -635,7 +479,7 @@ assert_dir_exists "src/feature on feature" "src/feature" assert_file_exists "src/feature/widget.rs on feature" "src/feature/widget.rs" # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" # src/core should exist, src/feature should NOT assert_dir_exists "src/core on dev" "src/core" @@ -647,7 +491,7 @@ assert_dir_not_exists "src/feature NOT on dev" "src/feature" assert_dir_exists "src/ still exists on dev (has core/)" "src" # Switch back -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_dir_exists "src/feature reappears on feature" "src/feature" assert_file_exists "src/feature/widget.rs reappears" "src/feature/widget.rs" @@ -656,29 +500,21 @@ begin_section "Directory: Unrecord directory change on feature, then switch" # ═══════════════════════════════════════════════════════════════════════════ # Continuing from previous -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord feature directory change succeeds" -else - _pass "unrecord feature directory change completes" -fi +require_success "unrecord feature directory change succeeds" unrecord_last # src/feature/widget.rs should still be on disk (reverted to added) assert_file_exists "widget.rs still on disk after unrecord" "src/feature/widget.rs" # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev after unrecord" switch_view "dev" # src/core should still be here assert_file_exists "src/core/engine.rs still on dev" "src/core/engine.rs" # widget.rs: since it was unrecorded (no INODES position), it persists as # an untracked file across the switch -if [[ -f "src/feature/widget.rs" ]]; then - _pass "unrecorded widget.rs persists as untracked" -else - _pass "unrecorded widget.rs cleaned up on switch" -fi +assert_file_content "unrecorded widget.rs persists with its bytes" "src/feature/widget.rs" "fn widget() {}" +assert_status_flag "unrecorded widget.rs remains untracked" "?" "src/feature/widget.rs" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Stress — many directories" @@ -691,27 +527,18 @@ init_repo for i in $(seq 1 10); do create_file "dir${i}/file_a.txt" "dir${i} a" create_file "dir${i}/file_b.txt" "dir${i} b" - atomic add "dir${i}/file_a.txt" >/dev/null 2>&1 || true - atomic add "dir${i}/file_b.txt" >/dev/null 2>&1 || true + require_success "add dir${i}/file_a.txt" atomic add "dir${i}/file_a.txt" + require_success "add dir${i}/file_b.txt" atomic add "dir${i}/file_b.txt" done -out="$(get_status_short)" -added_count="$(echo "$out" | grep -cE '^A' || true)" -if [[ $added_count -ge 15 ]]; then - _pass "bulk directory add: $added_count files added" -else - _pass "bulk directory add: $added_count files found" -fi +for i in $(seq 1 10); do + assert_status_flag "dir${i}/file_a.txt is added" "A" "dir${i}/file_a.txt" + assert_status_flag "dir${i}/file_b.txt is added" "A" "dir${i}/file_b.txt" +done -record_change "Bulk add 10 directories" >/dev/null 2>&1 || true +require_success "record 10 directories" record_change "Bulk add 10 directories" -out="$(get_status_short)" -dirty_count="$(echo "$out" | grep -cE '^[MADU?].*dir[0-9]' || true)" -if [[ $dirty_count -eq 0 ]]; then - _pass "all 10 directories clean after record" -else - _pass "bulk record status ($dirty_count remaining)" -fi +assert_clean "all 10 directories clean after record" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Directory: Directory with .atomicignore patterns" @@ -721,7 +548,7 @@ make_temp_repo "dir-ignore" init_repo # Create an ignore file -create_file ".atomicignore" "*.log\nbuild/" +create_file ".atomicignore" $'*.log\nbuild/\n' create_file "src/main.rs" "fn main() {}" create_file "debug.log" "log output" @@ -730,23 +557,19 @@ create_file "build/output.bin" "binary output" # main.rs should be addable, debug.log and build/ should be ignored assert_success "add src/main.rs" atomic add src/main.rs -add_log="$(atomic add debug.log 2>&1)" || true -if echo "$add_log" | grep -qiE "ignored|cannot|skip"; then - _pass "debug.log correctly ignored" -elif [[ $? -ne 0 ]]; then - _pass "debug.log rejected (non-zero exit)" -else - _pass "add debug.log result (ignore rules may vary)" +if ! add_log="$(atomic add debug.log 2>&1)"; then + if ! printf '%s\n' "$add_log" | grep -qi 'ignor'; then + _fail "add debug.log reports an ignore refusal" "$add_log" + fi fi +assert_status_no_entry "debug.log is ignored" "debug.log" -add_build="$(atomic add build/output.bin 2>&1)" || true -if echo "$add_build" | grep -qiE "ignored|cannot|skip"; then - _pass "build/ directory correctly ignored" -elif [[ $? -ne 0 ]]; then - _pass "build/ directory rejected (non-zero exit)" -else - _pass "add build/ result (ignore rules may vary)" +if ! add_build="$(atomic add build/output.bin 2>&1)"; then + if ! printf '%s\n' "$add_build" | grep -qi 'ignor'; then + _fail "add build/output.bin reports an ignore refusal" "$add_build" + fi fi +assert_status_no_entry "build/output.bin is ignored" "build/output.bin" # ═══════════════════════════════════════════════════════════════════════════ diff --git a/tests/harness/05_semantic_layer.sh b/tests/harness/05_semantic_layer.sh index 6d75c266..8dbfeaa4 100755 --- a/tests/harness/05_semantic_layer.sh +++ b/tests/harness/05_semantic_layer.sh @@ -33,9 +33,10 @@ create_file "hello.rs" "fn main() { println!(\"hello\"); }" -assert_success "add hello.rs" atomic add hello.rs +require_success "add hello.rs" atomic add hello.rs -rec_out="$(record_change "Add hello.rs")" +require_capture "record hello.rs" record_change "Add hello.rs" +rec_out="$COMMAND_OUTPUT" # Record output should mention lines and tokens if echo "$rec_out" | grep -qiE "line"; then @@ -50,16 +51,11 @@ else _fail "record output includes token stats" "got: $(echo "$rec_out" | head -5)" fi -# Should report at least 3 lines (the three lines of the file) -if echo "$rec_out" | grep -qE "\+[3-9].*line|\+[0-9][0-9].*line|3 line"; then - _pass "record reports correct line count (3+)" +# The file has exactly three lines. +if echo "$rec_out" | grep -qE '3 lines? \(\+3 -0 ~0\)'; then + _pass "record reports exactly three added lines" else - # Accept any positive line count - if echo "$rec_out" | grep -qE "\+[1-9]"; then - _pass "record reports positive line count" - else - _fail "record reports line count" "got: $(echo "$rec_out" | head -5)" - fi + _fail "record reports exactly three added lines" "got: $rec_out" fi # ═══════════════════════════════════════════════════════════════════════════ @@ -72,28 +68,29 @@ make_temp_repo "sem-log-isolation" init_repo create_file "base.txt" "base content" -assert_success "add base.txt" atomic add base.txt -record_change "Add base on dev" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base on dev" record_change "Add base on dev" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "feature.txt" "feature content" -assert_success "add feature.txt" atomic add feature.txt -record_change "Add feature.txt on feature" >/dev/null 2>&1 || true +require_success "add feature.txt" atomic add feature.txt +require_success "record feature.txt" record_change "Add feature.txt on feature" # Log on feature should have 2 entries (base + feature file). Draft views # hide ancestor-inherited entries by default (git-branch-log semantics); # --all shows the full history including what was explicitly pulled in via # insert_from_view. -feature_log="$(atomic log --all 2>/dev/null || true)" +require_capture "read feature log" atomic log --all +feature_log="$COMMAND_OUTPUT" feature_count="$(echo "$feature_log" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" if [[ $feature_count -ge 2 ]]; then _pass "feature log has 2+ changes ($feature_count)" else - _pass "feature log has $feature_count changes" + _fail "feature log has 2+ changes" "got $feature_count" fi # Feature log should mention both messages @@ -110,9 +107,10 @@ else fi # Switch to dev — log should NOT contain the feature change -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" -dev_log="$(atomic log 2>/dev/null || true)" +require_capture "read dev log" atomic log +dev_log="$COMMAND_OUTPUT" if echo "$dev_log" | grep -qF "Add base on dev"; then _pass "dev log contains base change" @@ -134,18 +132,20 @@ begin_section "Semantic: Change detail shows correct change per view" # latest change, not another view.s. # Continuing from previous repo -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" -feature_change="$(atomic change 2>/dev/null || true)" +require_capture "read feature change" atomic change +feature_change="$COMMAND_OUTPUT" if echo "$feature_change" | grep -qF "Add feature.txt on feature"; then _pass "feature's latest change is the feature change" else _fail "feature's latest change is the feature change" "got: $(echo "$feature_change" | head -5)" fi -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" -dev_change="$(atomic change 2>/dev/null || true)" +require_capture "read dev change" atomic change +dev_change="$COMMAND_OUTPUT" if echo "$dev_change" | grep -qF "Add base on dev"; then _pass "dev's latest change is the base change" else @@ -166,11 +166,12 @@ begin_section "Semantic: Insert brings change metadata to target view" # After inserting feature.s changes to dev, dev's log should include them. # Continuing from previous repo -insert_from_view "feature" "dev" >/dev/null 2>&1 || true +require_success "insert feature into dev" insert_from_view "feature" "dev" -switch_view "dev" >/dev/null 2>&1 || true +require_success "select dev" switch_view "dev" -dev_log_after="$(atomic log 2>/dev/null || true)" +require_capture "read dev log after insert" atomic log +dev_log_after="$COMMAND_OUTPUT" dev_count_after="$(echo "$dev_log_after" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" if [[ $dev_count_after -ge 2 ]]; then @@ -198,12 +199,12 @@ init_repo create_file "shared.rs" "fn greet() { println!(\"hello\"); }" -assert_success "add shared.rs" atomic add shared.rs -record_change "Add shared.rs" >/dev/null 2>&1 || true +require_success "add shared.rs" atomic add shared.rs +require_success "record shared.rs" record_change "Add shared.rs" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Modify on feature (working copy change, not yet recorded) overwrite_file "shared.rs" "fn greet() { @@ -211,34 +212,29 @@ overwrite_file "shared.rs" "fn greet() { }" # Diff on feature should show the modification -feature_diff="$(atomic diff 2>/dev/null || true)" -if echo "$feature_diff" | grep -qE "hello|world|shared\.rs"; then +require_capture "read feature diff" atomic diff +feature_diff="$COMMAND_OUTPUT" +if echo "$feature_diff" | grep -qF "hello" && echo "$feature_diff" | grep -qF "world"; then _pass "diff on feature shows modification" else - # Diff might show nothing if it compares against the graph - # which might not have feature's content yet - _pass "diff on feature completed (output may vary)" + _fail "diff on feature shows modification" "got: $feature_diff" fi # Record on feature -record_change "Modify shared.rs on feature" >/dev/null 2>&1 || true +require_success "record feature modification" record_change "Modify shared.rs on feature" # Switch to dev — working copy should have ORIGINAL content -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" # Diff on dev should show NO changes (dev has the original, not feature's mod) -dev_diff="$(atomic diff 2>/dev/null || true)" +require_capture "read dev diff" atomic diff +dev_diff="$COMMAND_OUTPUT" if echo "$dev_diff" | grep -qiE "no changes|no diff"; then _pass "diff on dev shows no changes" elif [[ -z "$(echo "$dev_diff" | xargs)" ]]; then _pass "diff on dev is empty (no changes)" else - # If diff shows something, it should NOT reference "world" (feature's change) - if echo "$dev_diff" | grep -qF "world"; then - _fail "diff on dev should NOT show feature's modification" "found 'world' in diff" - else - _pass "diff on dev does not show feature's modification" - fi + _fail "diff on dev shows no changes" "got: $dev_diff" fi # ═══════════════════════════════════════════════════════════════════════════ @@ -254,29 +250,30 @@ init_repo create_file "code.py" "def hello(): print('hello') return True" -assert_success "add code.py" atomic add code.py -record_change "Add code.py" >/dev/null 2>&1 || true +require_success "add code.py" atomic add code.py +require_success "record code.py" record_change "Add code.py" -new_view "feature-a" >/dev/null 2>&1 || true -insert_from_view "dev" "feature-a" >/dev/null 2>&1 || true -switch_view "feature-a" >/dev/null 2>&1 || true +require_success "create feature-a" new_view "feature-a" +require_success "insert dev into feature-a" insert_from_view "dev" "feature-a" +require_success "switch to feature-a" switch_view "feature-a" # Modify one line on feature-a overwrite_file "code.py" "def hello(): print('goodbye') return True" -rec_a="$(record_change "Change hello to goodbye")" +require_capture "record feature-a edit" record_change "Change hello to goodbye" +rec_a="$COMMAND_OUTPUT" if echo "$rec_a" | grep -qiE "line|token|change"; then _pass "record on feature-a reports stats" else - _pass "record on feature-a completes" + _fail "record on feature-a reports stats" "got: $rec_a" fi # Create feature-b from dev (independent sibling) -switch_view "dev" >/dev/null 2>&1 || true -new_view "feature-b" >/dev/null 2>&1 || true -insert_from_view "dev" "feature-b" >/dev/null 2>&1 || true -switch_view "feature-b" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" +require_success "create feature-b" new_view "feature-b" +require_success "insert dev into feature-b" insert_from_view "dev" "feature-b" +require_success "switch to feature-b" switch_view "feature-b" # Add two new lines on feature-b overwrite_file "code.py" "def hello(): @@ -285,16 +282,18 @@ overwrite_file "code.py" "def hello(): def world(): return False" -rec_b="$(record_change "Add world function")" +require_capture "record feature-b edit" record_change "Add world function" +rec_b="$COMMAND_OUTPUT" if echo "$rec_b" | grep -qiE "line|token|change"; then _pass "record on feature-b reports stats" else - _pass "record on feature-b completes" + _fail "record on feature-b reports stats" "got: $rec_b" fi # Log on feature-a should have feature-a's change, not feature-b's -switch_view "feature-a" >/dev/null 2>&1 || true -fa_log="$(atomic log 2>/dev/null || true)" +require_success "switch to feature-a" switch_view "feature-a" +require_capture "read feature-a log" atomic log +fa_log="$COMMAND_OUTPUT" if echo "$fa_log" | grep -qF "Change hello to goodbye"; then _pass "feature-a log has its own change" else @@ -307,8 +306,9 @@ else fi # Log on feature-b should have feature-b's change, not feature-a's -switch_view "feature-b" >/dev/null 2>&1 || true -fb_log="$(atomic log 2>/dev/null || true)" +require_success "switch to feature-b" switch_view "feature-b" +require_capture "read feature-b log" atomic log +fb_log="$COMMAND_OUTPUT" if echo "$fb_log" | grep -qF "Add world function"; then _pass "feature-b log has its own change" else @@ -331,19 +331,20 @@ make_temp_repo "sem-change-files" init_repo create_file "alpha.txt" "alpha" -assert_success "add alpha.txt" atomic add alpha.txt -record_change "Add alpha on dev" >/dev/null 2>&1 || true +require_success "add alpha.txt" atomic add alpha.txt +require_success "record alpha on dev" record_change "Add alpha on dev" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "beta.txt" "beta" -assert_success "add beta.txt" atomic add beta.txt -record_change "Add beta on feature" >/dev/null 2>&1 || true +require_success "add beta.txt" atomic add beta.txt +require_success "record beta on feature" record_change "Add beta on feature" # Latest change on feature should mention beta.txt -feature_change="$(atomic change 2>/dev/null || true)" +require_capture "read feature change" atomic change +feature_change="$COMMAND_OUTPUT" if echo "$feature_change" | grep -qF "beta.txt"; then _pass "feature change mentions beta.txt" else @@ -359,8 +360,9 @@ else fi # Dev's latest change should mention alpha.txt -switch_view "dev" >/dev/null 2>&1 || true -dev_change="$(atomic change 2>/dev/null || true)" +require_success "switch to dev" switch_view "dev" +require_capture "read dev change" atomic change +dev_change="$COMMAND_OUTPUT" if echo "$dev_change" | grep -qF "alpha.txt"; then _pass "dev change mentions alpha.txt" else @@ -385,18 +387,19 @@ make_temp_repo "sem-unrecord-log" init_repo create_file "removable.txt" "removable content" -assert_success "add removable.txt" atomic add removable.txt -record_change "Add removable on dev" >/dev/null 2>&1 || true +require_success "add removable.txt" atomic add removable.txt +require_success "record removable on dev" record_change "Add removable on dev" # Insert to feature so feature has it too -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" # Verify feature has the change. --all needed: the change is still # present in dev's own log at this point, so the draft-view ancestor # filter would otherwise hide it (it was pulled in via insert_from_view). -switch_view "feature" >/dev/null 2>&1 || true -feature_log_before="$(atomic log --all 2>/dev/null || true)" +require_success "switch to feature" switch_view "feature" +require_capture "read feature log" atomic log --all +feature_log_before="$COMMAND_OUTPUT" if echo "$feature_log_before" | grep -qF "Add removable on dev"; then _pass "feature log has removable change before unrecord" else @@ -404,11 +407,12 @@ else fi # Unrecord on dev -switch_view "dev" >/dev/null 2>&1 || true -unrecord_last >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" +require_success "unrecord on dev" unrecord_last # Dev log should no longer have the change -dev_log_after="$(atomic log 2>/dev/null || true)" +require_capture "read dev log after unrecord" atomic log +dev_log_after="$COMMAND_OUTPUT" if echo "$dev_log_after" | grep -qF "Add removable on dev"; then _fail "dev log should NOT have removable change after unrecord" "still present" else @@ -416,8 +420,9 @@ else fi # Feature should STILL have it (unrecord was only on dev) -switch_view "feature" >/dev/null 2>&1 || true -feature_log_after="$(atomic log 2>/dev/null || true)" +require_success "switch to feature after unrecord" switch_view "feature" +require_capture "read feature log after unrecord" atomic log +feature_log_after="$COMMAND_OUTPUT" if echo "$feature_log_after" | grep -qF "Add removable on dev"; then _pass "feature log still has removable change after dev unrecord" else @@ -438,36 +443,30 @@ create_file "src/main.rs" "fn main() {}" create_file "src/lib.rs" "pub fn lib() {}" create_file "README.md" "# Project" -assert_success "add main.rs" atomic add src/main.rs -assert_success "add lib.rs" atomic add src/lib.rs -assert_success "add README.md" atomic add README.md +require_success "add main.rs" atomic add src/main.rs +require_success "add lib.rs" atomic add src/lib.rs +require_success "add README.md" atomic add README.md -rec_out="$(record_change "Add project files")" +require_capture "record project files" record_change "Add project files" +rec_out="$COMMAND_OUTPUT" # Record should mention multiple files -if echo "$rec_out" | grep -qE "[23] file"; then - _pass "record reports multiple files changed" +if echo "$rec_out" | grep -qE "3 files? changed"; then + _pass "record reports three files changed" else - if echo "$rec_out" | grep -qiE "file"; then - _pass "record mentions file changes" - else - _fail "record reports multiple files" "got: $(echo "$rec_out" | head -5)" - fi + _fail "record reports three files changed" "got: $rec_out" fi # Change detail should list all three files -change_out="$(atomic change 2>/dev/null || true)" -found=0 +require_capture "read project change" atomic change +change_out="$COMMAND_OUTPUT" for f in main.rs lib.rs README.md; do if echo "$change_out" | grep -qF "$f"; then - found=$((found + 1)) + _pass "change detail lists $f" + else + _fail "change detail lists $f" "not found in: $change_out" fi done -if [[ $found -ge 2 ]]; then - _pass "change detail lists $found/3 files" -else - _fail "change detail lists all files" "found $found/3" -fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Semantic: Line-level edit tracking" @@ -484,8 +483,8 @@ line two line three line four line five" -assert_success "add lines.txt" atomic add lines.txt -record_change "Add five lines" >/dev/null 2>&1 || true +require_success "add lines.txt" atomic add lines.txt +require_success "record five lines" record_change "Add five lines" # Modify lines 2 and 4 overwrite_file "lines.txt" "line one @@ -493,7 +492,8 @@ LINE TWO MODIFIED line three LINE FOUR MODIFIED line five" -rec_out="$(record_change "Modify lines 2 and 4")" +require_capture "record line edits" record_change "Modify lines 2 and 4" +rec_out="$COMMAND_OUTPUT" # Record should report line changes if echo "$rec_out" | grep -qiE "line"; then @@ -503,7 +503,8 @@ else fi # Change detail should reference lines.txt -change_out="$(atomic change 2>/dev/null || true)" +require_capture "read line edit change" atomic change +change_out="$COMMAND_OUTPUT" if echo "$change_out" | grep -qF "lines.txt"; then _pass "change detail mentions lines.txt" else @@ -529,8 +530,9 @@ init_repo create_file "tokens.rs" "fn compute(x: i32) -> i32 { x + 1 }" -assert_success "add tokens.rs" atomic add tokens.rs -rec_out="$(record_change "Add tokens.rs")" +require_success "add tokens.rs" atomic add tokens.rs +require_capture "record tokens.rs" record_change "Add tokens.rs" +rec_out="$COMMAND_OUTPUT" # Should report token stats if echo "$rec_out" | grep -qiE "token"; then @@ -543,7 +545,8 @@ fi overwrite_file "tokens.rs" "fn compute(x: i32) -> i32 { x * 1 }" -rec_mod="$(record_change "Change + to *")" +require_capture "record token edit" record_change "Change + to *" +rec_mod="$COMMAND_OUTPUT" if echo "$rec_mod" | grep -qiE "token"; then _pass "modification record includes token stats" else @@ -569,32 +572,35 @@ init_repo create_file "app.py" "def main(): print('version 1') return 0" -assert_success "add app.py" atomic add app.py -record_change "Add app.py v1" >/dev/null 2>&1 || true +require_success "add app.py" atomic add app.py +require_success "record app.py v1" record_change "Add app.py v1" -dev_log_v1="$(atomic log 2>/dev/null || true)" +require_capture "read initial dev log" atomic log +dev_log_v1="$COMMAND_OUTPUT" dev_count_v1="$(echo "$dev_log_v1" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" # Step 2: create feature, insert dev -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Step 3: modify on feature overwrite_file "app.py" "def main(): print('version 2 from feature') return 0" -rec_feature="$(record_change "Update to v2 on feature")" +require_capture "record feature update" record_change "Update to v2 on feature" +rec_feature="$COMMAND_OUTPUT" if echo "$rec_feature" | grep -qiE "line|token"; then _pass "feature record shows semantic stats" else - _pass "feature record completes" + _fail "feature record shows semantic stats" "got: $rec_feature" fi # --all: feature inherited "Add app.py v1" via insert_from_view, which the # draft-view ancestor filter would otherwise hide since it's also on dev. -feature_log="$(atomic log --all 2>/dev/null || true)" +require_capture "read feature log" atomic log --all +feature_log="$COMMAND_OUTPUT" feature_count="$(echo "$feature_log" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" if [[ $feature_count -ge 2 ]]; then _pass "feature log has 2+ entries ($feature_count)" @@ -603,9 +609,10 @@ else fi # Step 4: switch to dev, verify isolation -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" -dev_log_isolated="$(atomic log 2>/dev/null || true)" +require_capture "read isolated dev log" atomic log +dev_log_isolated="$COMMAND_OUTPUT" dev_count_isolated="$(echo "$dev_log_isolated" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" if [[ $dev_count_isolated -eq $dev_count_v1 ]]; then @@ -626,11 +633,12 @@ assert_file_content "app.py has v1 on dev" "app.py" "def main(): return 0" # Step 5: insert feature→dev -insert_from_view "feature" "dev" >/dev/null 2>&1 || true +require_success "insert feature into dev" insert_from_view "feature" "dev" -switch_view "dev" >/dev/null 2>&1 || true +require_success "select dev" switch_view "dev" -dev_log_after_apply="$(atomic log 2>/dev/null || true)" +require_capture "read dev log after insert" atomic log +dev_log_after_apply="$COMMAND_OUTPUT" dev_count_after="$(echo "$dev_log_after_apply" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" if [[ $dev_count_after -gt $dev_count_v1 ]]; then @@ -650,7 +658,7 @@ assert_file_content "app.py has v2 on dev after insert" "app.py" "def main(): print('version 2 from feature') return 0" -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" assert_file_content "app.py has v2 on feature" "app.py" "def main(): print('version 2 from feature') return 0" @@ -667,23 +675,24 @@ make_temp_repo "sem-three-view" init_repo create_file "base.txt" "base" -assert_success "add base.txt" atomic add base.txt -record_change "Base commit" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base" record_change "Base commit" # Create three siblings from dev for stack in alpha beta gamma; do - new_view "$stack" >/dev/null 2>&1 || true - insert_from_view "dev" "$stack" >/dev/null 2>&1 || true - switch_view "$stack" >/dev/null 2>&1 || true + require_success "create $stack view" new_view "$stack" + require_success "insert dev into $stack" insert_from_view "dev" "$stack" + require_success "switch to $stack" switch_view "$stack" create_file "${stack}.txt" "${stack} content" - assert_success "add ${stack}.txt" atomic add "${stack}.txt" - record_change "Add ${stack}.txt" >/dev/null 2>&1 || true + require_success "add ${stack}.txt" atomic add "${stack}.txt" + require_success "record ${stack}.txt" record_change "Add ${stack}.txt" done # Alpha log should have base + alpha, NOT beta or gamma -switch_view "alpha" >/dev/null 2>&1 || true -alpha_log="$(atomic log 2>/dev/null || true)" +require_success "switch to alpha" switch_view "alpha" +require_capture "read alpha log" atomic log +alpha_log="$COMMAND_OUTPUT" if echo "$alpha_log" | grep -qF "Add alpha.txt"; then _pass "alpha log has alpha change" else @@ -701,10 +710,11 @@ else fi # Insert alpha→dev only. Beta and gamma should NOT appear on dev. -insert_from_view "alpha" "dev" >/dev/null 2>&1 || true -switch_view "dev" >/dev/null 2>&1 || true +require_success "insert alpha into dev" insert_from_view "alpha" "dev" +require_success "switch to dev" switch_view "dev" -dev_log="$(atomic log 2>/dev/null || true)" +require_capture "read dev log" atomic log +dev_log="$COMMAND_OUTPUT" if echo "$dev_log" | grep -qF "Add alpha.txt"; then _pass "dev log has alpha change after insert" else @@ -722,10 +732,11 @@ else fi # Insert beta→dev. Now dev has base + alpha + beta, but NOT gamma. -insert_from_view "beta" "dev" >/dev/null 2>&1 || true -switch_view "dev" >/dev/null 2>&1 || true +require_success "insert beta into dev" insert_from_view "beta" "dev" +require_success "select dev" switch_view "dev" -dev_log2="$(atomic log 2>/dev/null || true)" +require_capture "read dev log after beta" atomic log +dev_log2="$COMMAND_OUTPUT" if echo "$dev_log2" | grep -qF "Add beta.txt"; then _pass "dev log has beta change after second insert" else @@ -738,8 +749,9 @@ else fi # Gamma is untouched — should only have base + gamma -switch_view "gamma" >/dev/null 2>&1 || true -gamma_log="$(atomic log 2>/dev/null || true)" +require_success "switch to gamma" switch_view "gamma" +require_capture "read gamma log" atomic log +gamma_log="$COMMAND_OUTPUT" if echo "$gamma_log" | grep -qF "Add gamma.txt"; then _pass "gamma log has gamma change" else @@ -767,21 +779,17 @@ make_temp_repo "sem-diff-clean" init_repo create_file "clean.txt" "clean content" -assert_success "add clean.txt" atomic add clean.txt -record_change "Add clean.txt" >/dev/null 2>&1 || true +require_success "add clean.txt" atomic add clean.txt +require_success "record clean.txt" record_change "Add clean.txt" -diff_out="$(atomic diff 2>/dev/null || true)" +require_capture "read clean diff" atomic diff +diff_out="$COMMAND_OUTPUT" if echo "$diff_out" | grep -qiE "no changes|no diff"; then _pass "diff shows no changes after record" elif [[ -z "$(echo "$diff_out" | xargs)" ]]; then _pass "diff is empty after record (clean)" else - # Check that it doesn't show actual file changes - if echo "$diff_out" | grep -qE "^[-+].*clean"; then - _fail "diff should be clean after record" "shows changes" - else - _pass "diff output after record (no content changes)" - fi + _fail "diff shows no changes after record" "got: $diff_out" fi # ═══════════════════════════════════════════════════════════════════════════ @@ -796,17 +804,19 @@ init_repo # Capture the baseline count from init (init creates 2 changes: # "Initialize repository" and "Initialize vault"). -baseline_log="$(atomic log 2>/dev/null || true)" +require_capture "read baseline log" atomic log +baseline_log="$COMMAND_OUTPUT" BASE="$(echo "$baseline_log" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" # Record 3 changes on dev for i in 1 2 3; do create_file "file${i}.txt" "content ${i}" - assert_success "add file${i}.txt" atomic add "file${i}.txt" - record_change "Add file${i}" >/dev/null 2>&1 || true + require_success "add file${i}.txt" atomic add "file${i}.txt" + require_success "record file${i}.txt" record_change "Add file${i}" done -dev_log="$(atomic log 2>/dev/null || true)" +require_capture "read dev log" atomic log +dev_log="$COMMAND_OUTPUT" dev_count="$(echo "$dev_log" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" expected_dev=$((BASE + 3)) if [[ $dev_count -eq $expected_dev ]]; then @@ -816,19 +826,20 @@ else fi # Create feature from dev, add 2 more changes -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" for i in 4 5; do create_file "file${i}.txt" "content ${i}" - assert_success "add file${i}.txt" atomic add "file${i}.txt" - record_change "Add file${i}" >/dev/null 2>&1 || true + require_success "add file${i}.txt" atomic add "file${i}.txt" + require_success "record file${i}.txt" record_change "Add file${i}" done # --all: expected_feature counts the 3 inherited dev changes too, which # the draft-view ancestor filter would otherwise hide. -feature_log="$(atomic log --all 2>/dev/null || true)" +require_capture "read feature log" atomic log --all +feature_log="$COMMAND_OUTPUT" feature_count="$(echo "$feature_log" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" expected_feature=$((BASE + 5)) if [[ $feature_count -eq $expected_feature ]]; then @@ -838,8 +849,9 @@ else fi # Dev should still have exactly BASE+3 -switch_view "dev" >/dev/null 2>&1 || true -dev_log2="$(atomic log 2>/dev/null || true)" +require_success "switch to dev" switch_view "dev" +require_capture "read dev log" atomic log +dev_log2="$COMMAND_OUTPUT" dev_count2="$(echo "$dev_log2" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" if [[ $dev_count2 -eq $expected_dev ]]; then _pass "dev still has exactly $expected_dev changes" @@ -848,10 +860,11 @@ else fi # Insert feature→dev. Dev should now have BASE+5. -insert_from_view "feature" "dev" >/dev/null 2>&1 || true -switch_view "dev" >/dev/null 2>&1 || true +require_success "insert feature into dev" insert_from_view "feature" "dev" +require_success "select dev" switch_view "dev" -dev_log3="$(atomic log 2>/dev/null || true)" +require_capture "read dev log after insert" atomic log +dev_log3="$COMMAND_OUTPUT" dev_count3="$(echo "$dev_log3" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" expected_after_insert=$((BASE + 5)) if [[ $dev_count3 -eq $expected_after_insert ]]; then @@ -861,8 +874,9 @@ else fi # Unrecord last on dev. Should go back to BASE+4. -unrecord_last >/dev/null 2>&1 || true -dev_log4="$(atomic log 2>/dev/null || true)" +require_success "unrecord dev change" unrecord_last +require_capture "read dev log after unrecord" atomic log +dev_log4="$COMMAND_OUTPUT" dev_count4="$(echo "$dev_log4" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" expected_after_unrecord=$((BASE + 4)) if [[ $dev_count4 -eq $expected_after_unrecord ]]; then @@ -873,8 +887,9 @@ fi # Feature should still have BASE+5 (unrecord was on dev). --all: same # ancestor-filter reasoning as above. -switch_view "feature" >/dev/null 2>&1 || true -feature_log2="$(atomic log --all 2>/dev/null || true)" +require_success "switch to feature" switch_view "feature" +require_capture "read feature log" atomic log --all +feature_log2="$COMMAND_OUTPUT" feature_count2="$(echo "$feature_log2" | grep -cE '^[[:space:]]*#[0-9]+|^[0-9a-f]{8,}' || true)" if [[ $feature_count2 -eq $expected_feature ]]; then _pass "feature still has $expected_feature changes after dev unrecord" diff --git a/tests/harness/05_workspaces.sh b/tests/harness/05_workspaces.sh index e39275cb..15dcc98b 100755 --- a/tests/harness/05_workspaces.sh +++ b/tests/harness/05_workspaces.sh @@ -55,7 +55,7 @@ assert_dir_exists \ ".atomic/workspaces/dev" # Create a feature view -new_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" assert_dir_exists \ ".atomic/workspaces/feature created with view" \ @@ -82,8 +82,8 @@ dist/" # Record a tracked file on dev so the view has content create_file "index.ts" "console.log('hello')" -assert_success "add index.ts" atomic add index.ts -record_change "Add index.ts on dev" >/dev/null 2>&1 || true +require_success "add index.ts" atomic add index.ts +require_success "record index.ts on dev" record_change "Add index.ts on dev" # Simulate build artifacts mkdir -p node_modules/lodash @@ -97,9 +97,9 @@ assert_dir_exists "dist exists on dev" "dist" assert_file_exists "lodash exists on dev" "node_modules/lodash/index.js" # Create feature and switch -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" assert_current_view "on feature" "feature" @@ -134,7 +134,7 @@ begin_section "Workspace: Ignored files restored on switch back" # # Continuing from previous: switch back to dev and verify artifacts return. -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_current_view "back on dev" "dev" # Artifacts should be restored @@ -166,7 +166,7 @@ begin_section "Workspace: Each view has its own artifacts" # Continuing from previous — we're on dev with lodash@4.17.21 -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" assert_current_view "on feature" "feature" # Feature has no node_modules yet @@ -183,7 +183,7 @@ assert_file_content \ '{"name":"lodash","version":"3.10.0"}' # Switch to dev — should see 4.17.21 -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_content \ "dev has lodash 4.17.21" \ @@ -191,7 +191,7 @@ assert_file_content \ '{"name":"lodash","version":"4.17.21"}' # Switch to feature — should see 3.10.0 -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_content \ "feature still has lodash 3.10.0" \ @@ -214,8 +214,8 @@ dist/" # Create a tracked file on dev create_file "app.ts" "const app = 1" -assert_success "add app.ts" atomic add app.ts -record_change "Add app.ts" >/dev/null 2>&1 || true +require_success "add app.ts" atomic add app.ts +require_success "record app.ts" record_change "Add app.ts" # Create an untracked, NON-ignored file (not in .atomicignore) create_file "notes.txt" "my personal notes" @@ -225,9 +225,9 @@ mkdir -p node_modules create_file "node_modules/thing.js" "module.exports = 1" # Switch to feature -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # notes.txt should persist (untracked, not ignored) assert_file_exists "notes.txt persists on feature" "notes.txt" @@ -237,7 +237,7 @@ assert_file_content "notes.txt content intact" "notes.txt" "my personal notes" assert_dir_not_exists "node_modules shelved away" "node_modules" # Switch back — both should be there -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_file_exists "notes.txt persists on dev" "notes.txt" assert_dir_exists "node_modules restored on dev" "node_modules" @@ -255,25 +255,25 @@ create_file ".atomicignore" "build/" # Record different files on dev and feature create_file "shared.ts" "shared code" -assert_success "add shared.ts" atomic add shared.ts -record_change "Add shared.ts on dev" >/dev/null 2>&1 || true +require_success "add shared.ts" atomic add shared.ts +require_success "record shared.ts on dev" record_change "Add shared.ts on dev" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "feature.ts" "feature code" -assert_success "add feature.ts" atomic add feature.ts -record_change "Add feature.ts on feature" >/dev/null 2>&1 || true +require_success "add feature.ts" atomic add feature.ts +require_success "record feature.ts" record_change "Add feature.ts on feature" # Switch to dev — feature.ts should not exist (graph isolation) -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_exists "shared.ts on dev" "shared.ts" assert_file_not_exists "feature.ts NOT on dev (graph isolation)" "feature.ts" # Switch to feature — both should exist -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_exists "shared.ts on feature" "shared.ts" assert_file_exists "feature.ts on feature" "feature.ts" @@ -294,8 +294,8 @@ dist/ # ── dev: record source, create build artifacts ── create_file "src/main.ts" "console.log('dev')" -assert_success "add src/main.ts" atomic add src/main.ts -record_change "Add main.ts on dev" >/dev/null 2>&1 || true +require_success "add src/main.ts" atomic add src/main.ts +require_success "record main.ts on dev" record_change "Add main.ts on dev" mkdir -p node_modules/express create_file "node_modules/express/index.js" "module.exports = express" @@ -305,13 +305,13 @@ mkdir -p .cache create_file ".cache/tsbuildinfo" "{}" # ── feature: different source + different artifacts ── -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Tracked: modify source on feature overwrite_file "src/main.ts" "console.log('feature')" -record_change "Modify main.ts on feature" >/dev/null 2>&1 || true +require_success "record feature modification" record_change "Modify main.ts on feature" # Artifacts: different deps on feature mkdir -p node_modules/fastify @@ -326,7 +326,7 @@ assert_file_not_exists "express NOT on feature" "node_modules/express/index.js" assert_file_content "dist is feature build" "dist/main.js" "console.log('feature compiled')" # ── Switch to dev: full state restored ── -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_content "source is dev version" "src/main.ts" "console.log('dev')" assert_file_exists "express on dev" "node_modules/express/index.js" @@ -335,7 +335,7 @@ assert_file_content "dist is dev build" "dist/main.js" "console.log('dev compile assert_file_exists ".cache on dev" ".cache/tsbuildinfo" # ── Switch to feature: full state restored ── -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_content "source is feature version (round 2)" "src/main.ts" "console.log('feature')" assert_file_exists "fastify on feature (round 2)" "node_modules/fastify/index.js" @@ -359,31 +359,31 @@ dist/" # ── dev: base project with express@4 ── create_file "package.json" '{"dependencies":{"express":"4.18.0"}}' -assert_success "add package.json" atomic add package.json -record_change "Add package.json on dev" >/dev/null 2>&1 || true +require_success "add package.json" atomic add package.json +require_success "record package.json" record_change "Add package.json on dev" mkdir -p node_modules/express create_file "node_modules/express/package.json" '{"version":"4.18.0"}' # ── agent-a: testing express@5 ── -new_view "agent-a" >/dev/null 2>&1 || true -insert_from_view "dev" "agent-a" >/dev/null 2>&1 || true -switch_view "agent-a" >/dev/null 2>&1 || true +require_success "create agent-a view" new_view "agent-a" +require_success "insert dev into agent-a" insert_from_view "dev" "agent-a" +require_success "switch to agent-a" switch_view "agent-a" overwrite_file "package.json" '{"dependencies":{"express":"5.0.0"}}' -record_change "Upgrade to express@5" >/dev/null 2>&1 || true +require_success "record express upgrade" record_change "Upgrade to express@5" mkdir -p node_modules/express create_file "node_modules/express/package.json" '{"version":"5.0.0"}' # ── agent-b: testing fastify migration ── -switch_view "dev" >/dev/null 2>&1 || true -new_view "agent-b" >/dev/null 2>&1 || true -insert_from_view "dev" "agent-b" >/dev/null 2>&1 || true -switch_view "agent-b" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" +require_success "create agent-b view" new_view "agent-b" +require_success "insert dev into agent-b" insert_from_view "dev" "agent-b" +require_success "switch to agent-b" switch_view "agent-b" overwrite_file "package.json" '{"dependencies":{"fastify":"4.0.0"}}' -record_change "Migrate to fastify" >/dev/null 2>&1 || true +require_success "record fastify migration" record_change "Migrate to fastify" mkdir -p node_modules/fastify create_file "node_modules/fastify/package.json" '{"version":"4.0.0"}' @@ -398,7 +398,7 @@ assert_file_content \ assert_file_not_exists "agent-b has no express" "node_modules/express/package.json" # Switch to agent-a -switch_view "agent-a" >/dev/null 2>&1 || true +require_success "switch to agent-a" switch_view "agent-a" assert_file_content \ "agent-a has express@5" \ "node_modules/express/package.json" \ @@ -410,7 +410,7 @@ assert_file_content \ '{"dependencies":{"express":"5.0.0"}}' # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_content \ "dev has express@4" \ "node_modules/express/package.json" \ @@ -422,7 +422,7 @@ assert_file_content \ '{"dependencies":{"express":"4.18.0"}}' # Full round-trip back to agent-b -switch_view "agent-b" >/dev/null 2>&1 || true +require_success "switch back to agent-b" switch_view "agent-b" assert_file_content \ "agent-b still has fastify (round 2)" \ "node_modules/fastify/package.json" \ @@ -441,20 +441,20 @@ begin_section "Workspace: Rapid switching preserves artifacts" # Continuing from previous (dev, agent-a, agent-b all set up) for i in $(seq 1 5); do - switch_view "dev" >/dev/null 2>&1 || true + require_success "switch to dev (iteration $i)" switch_view "dev" assert_file_content \ "dev express@4 (iteration $i)" \ "node_modules/express/package.json" \ '{"version":"4.18.0"}' assert_file_not_exists "dev no fastify (iteration $i)" "node_modules/fastify/package.json" - switch_view "agent-a" >/dev/null 2>&1 || true + require_success "switch to agent-a (iteration $i)" switch_view "agent-a" assert_file_content \ "agent-a express@5 (iteration $i)" \ "node_modules/express/package.json" \ '{"version":"5.0.0"}' - switch_view "agent-b" >/dev/null 2>&1 || true + require_success "switch to agent-b (iteration $i)" switch_view "agent-b" assert_file_content \ "agent-b fastify (iteration $i)" \ "node_modules/fastify/package.json" \ @@ -462,8 +462,6 @@ for i in $(seq 1 5); do assert_file_not_exists "agent-b no express (iteration $i)" "node_modules/express/package.json" done -_pass "rapid switching: 5 round-trips across 3 views completed" - # ═══════════════════════════════════════════════════════════════════════════ begin_section "Workspace: Empty view has no artifacts" # ═══════════════════════════════════════════════════════════════════════════ @@ -477,16 +475,16 @@ init_repo create_file ".atomicignore" "node_modules/" create_file "main.ts" "code" -assert_success "add main.ts" atomic add main.ts -record_change "Add main.ts" >/dev/null 2>&1 || true +require_success "add main.ts" atomic add main.ts +require_success "record main.ts" record_change "Add main.ts" # Create artifacts on dev mkdir -p node_modules create_file "node_modules/dep.js" "dep" # Create empty view and switch -new_view "empty" >/dev/null 2>&1 || true -switch_view "empty" >/dev/null 2>&1 || true +require_success "create empty view" new_view "empty" +require_success "switch to empty view" switch_view "empty" # In the ambient graph model, child views see parent's files through # the filter chain — the view is parented on dev so main.ts is visible. @@ -496,7 +494,7 @@ assert_file_exists "main.ts visible on child view (inherited from dev)" "main.ts assert_dir_not_exists "no node_modules on empty view" "node_modules" # Switch back — everything returns -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_file_exists "main.ts back on dev" "main.ts" assert_dir_exists "node_modules back on dev" "node_modules" assert_file_content "dep.js intact" "node_modules/dep.js" "dep" @@ -516,8 +514,8 @@ dist/ .next/" create_file "page.tsx" "
hello
" -assert_success "add page.tsx" atomic add page.tsx -record_change "Add page.tsx" >/dev/null 2>&1 || true +require_success "add page.tsx" atomic add page.tsx +require_success "record page.tsx" record_change "Add page.tsx" # Create deeply nested build output mkdir -p node_modules/.cache/babel/sub @@ -528,9 +526,9 @@ mkdir -p .next/static/chunks create_file ".next/static/chunks/main.js" "nextjs_chunk" create_file ".next/BUILD_ID" "abc123" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # All nested ignored content should be gone assert_dir_not_exists "node_modules gone" "node_modules" @@ -538,7 +536,7 @@ assert_dir_not_exists "dist gone" "dist" assert_dir_not_exists ".next gone" ".next" # Switch back — deeply nested content restored -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_file_content \ "nested babel cache restored" \ @@ -576,8 +574,8 @@ dist/" # Human's dev state create_file "src/app.ts" "// dev version" -assert_success "add src/app.ts" atomic add src/app.ts -record_change "Add app.ts on dev" >/dev/null 2>&1 || true +require_success "add src/app.ts" atomic add src/app.ts +require_success "record app.ts on dev" record_change "Add app.ts on dev" mkdir -p node_modules/humanlib create_file "node_modules/humanlib/index.js" "human_dep" @@ -585,13 +583,13 @@ mkdir -p dist create_file "dist/app.js" "compiled_dev" # Agent works on feature -new_view "agent-review-test" >/dev/null 2>&1 || true -insert_from_view "dev" "agent-review-test" >/dev/null 2>&1 || true -switch_view "agent-review-test" >/dev/null 2>&1 || true +require_success "create review view" new_view "agent-review-test" +require_success "insert dev into review view" insert_from_view "dev" "agent-review-test" +require_success "switch to review view" switch_view "agent-review-test" # Agent modifies source and builds overwrite_file "src/app.ts" "// agent version with fix" -record_change "Agent fix" >/dev/null 2>&1 || true +require_success "record agent fix" record_change "Agent fix" mkdir -p node_modules/agentlib create_file "node_modules/agentlib/index.js" "agent_dep" @@ -599,7 +597,7 @@ mkdir -p dist create_file "dist/app.js" "compiled_agent" # Switch back to dev (simulates agent session ending) -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" # Human's state is fully restored assert_file_content "human source" "src/app.ts" "// dev version" @@ -608,7 +606,7 @@ assert_file_content "human build" "dist/app.js" "compiled_dev" assert_file_not_exists "no agent deps on dev" "node_modules/agentlib/index.js" # Human switches to agent's view to review -switch_view "agent-review-test" >/dev/null 2>&1 || true +require_success "switch to review view" switch_view "agent-review-test" # Human sees agent's FULL state assert_file_content \ @@ -626,7 +624,7 @@ assert_file_content \ assert_file_not_exists "no human deps on agent view" "node_modules/humanlib/index.js" # Human goes back — their world is intact -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_file_content "human source restored" "src/app.ts" "// dev version" assert_file_content "human deps restored" "node_modules/humanlib/index.js" "human_dep" @@ -645,10 +643,10 @@ init_repo # dev ignores node_modules only create_file ".atomicignore" "node_modules/" -assert_success "add .atomicignore" atomic add .atomicignore +require_success "add .atomicignore" atomic add .atomicignore create_file "src/index.ts" "main" -assert_success "add src/index.ts" atomic add src/index.ts -record_change "Initial dev" >/dev/null 2>&1 || true +require_success "add src/index.ts" atomic add src/index.ts +require_success "record initial dev" record_change "Initial dev" mkdir -p node_modules create_file "node_modules/pkg.js" "pkg" @@ -656,14 +654,14 @@ mkdir -p build create_file "build/output.js" "build_output" # Create feature that ALSO ignores build/ -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Update .atomicignore on feature to also ignore build/ overwrite_file ".atomicignore" "node_modules/ build/" -record_change "Ignore build/ on feature" >/dev/null 2>&1 || true +require_success "record feature ignore rules" record_change "Ignore build/ on feature" # On dev, build/ was NOT ignored, so it should have persisted as untracked # On feature, build/ IS ignored — so when we switch AWAY from feature, @@ -674,7 +672,7 @@ mkdir -p build create_file "build/output.js" "feature_build" # Switch to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" # dev's .atomicignore only has node_modules/, so build/ is untracked-not-ignored. # dev should see its own node_modules restored and build/ from the persistent @@ -692,7 +690,7 @@ if [[ -f "build/output.js" ]]; then "build/output.js" \ "build_output" else - _pass "build/ not present on dev (was shelved with feature)" + assert_file_not_exists "feature build does not leak to dev" "build/output.js" fi # ═══════════════════════════════════════════════════════════════════════════ @@ -708,15 +706,15 @@ init_repo create_file ".atomicignore" "node_modules/" create_file "code.ts" "code" -assert_success "add code.ts" atomic add code.ts -record_change "Add code.ts" >/dev/null 2>&1 || true +require_success "add code.ts" atomic add code.ts +require_success "record code.ts" record_change "Add code.ts" mkdir -p node_modules create_file "node_modules/dep.js" "dep_content" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Verify workspace is inside .atomic/ (durable path) assert_dir_exists \ @@ -727,13 +725,14 @@ assert_dir_exists \ "dev workspace is working-copy scoped" \ "$(working_copy_workspace_path dev)" -# Verify it's the actual content (not a symlink to /tmp) +# The repository itself may be in a temporary directory; the workspace must +# resolve beneath that repository, not to a separate external location. actual_path="$(cd "$(working_copy_workspace_path dev)" 2>/dev/null && pwd -P)" -if echo "$actual_path" | grep -q "/tmp"; then - _fail "workspace is NOT in /tmp" "path resolved to $actual_path" -else - _pass "workspace is NOT in /tmp" -fi +repo_path="$(pwd -P)" +case "$actual_path" in + "$repo_path"/.atomic/working-copies/*) _pass "workspace resolves inside the repository" ;; + *) _fail "workspace resolves inside the repository" "path resolved to $actual_path" ;; +esac # Verify the shelved content is really there assert_file_exists \ @@ -756,31 +755,26 @@ init_repo create_file ".atomicignore" "node_modules/" create_file "base.ts" "base" -assert_success "add base.ts" atomic add base.ts -record_change "Add base.ts" >/dev/null 2>&1 || true +require_success "add base.ts" atomic add base.ts +require_success "record base.ts" record_change "Add base.ts" -new_view "ephemeral" >/dev/null 2>&1 || true -insert_from_view "dev" "ephemeral" >/dev/null 2>&1 || true -switch_view "ephemeral" >/dev/null 2>&1 || true +require_success "create ephemeral view" new_view "ephemeral" +require_success "insert dev into ephemeral" insert_from_view "dev" "ephemeral" +require_success "switch to ephemeral" switch_view "ephemeral" # Create artifacts on ephemeral view mkdir -p node_modules create_file "node_modules/temp.js" "temp" # Switch back to dev (shelves ephemeral's artifacts) -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_dir_exists \ "ephemeral workspace exists before delete" \ ".atomic/workspaces/ephemeral" # Delete the ephemeral view -del_out="$(atomic view delete ephemeral 2>&1)" || true -if echo "$del_out" | grep -qiE "deleted|removed|success"; then - _pass "delete ephemeral view" -else - _pass "delete ephemeral view completes" -fi +require_success "delete ephemeral view" atomic view delete ephemeral # Workspace storage should be cleaned up assert_dir_not_exists \ @@ -800,14 +794,14 @@ init_repo create_file ".atomicignore" "node_modules/" create_file "app.ts" "shared" -assert_success "add app.ts" atomic add app.ts -record_change "Add app.ts" >/dev/null 2>&1 || true +require_success "add app.ts" atomic add app.ts +require_success "record app.ts" record_change "Add app.ts" # Create 5 views with unique node_modules for i in $(seq 1 5); do - new_view "agent-${i}" >/dev/null 2>&1 || true - insert_from_view "dev" "agent-${i}" >/dev/null 2>&1 || true - switch_view "agent-${i}" >/dev/null 2>&1 || true + require_success "create agent-${i} view" new_view "agent-${i}" + require_success "insert dev into agent-${i}" insert_from_view "dev" "agent-${i}" + require_success "switch to agent-${i}" switch_view "agent-${i}" mkdir -p node_modules create_file "node_modules/marker.txt" "agent-${i}-deps" @@ -815,7 +809,7 @@ done # Now cycle through all views and verify each has its own marker for i in $(seq 1 5); do - switch_view "agent-${i}" >/dev/null 2>&1 || true + require_success "switch to agent-${i}" switch_view "agent-${i}" assert_file_content \ "agent-${i} has its own deps" \ "node_modules/marker.txt" \ @@ -823,18 +817,16 @@ for i in $(seq 1 5); do done # Switch to dev — should have no node_modules (dev never created any) -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_dir_not_exists "dev has no node_modules" "node_modules" # One more round trip -switch_view "agent-3" >/dev/null 2>&1 || true +require_success "switch to agent-3" switch_view "agent-3" assert_file_content \ "agent-3 still has its deps after full cycle" \ "node_modules/marker.txt" \ "agent-3-deps" -_pass "5-view stress test completed" - # ═══════════════════════════════════════════════════════════════════════════ print_summary diff --git a/tests/harness/06_tags_and_stash.sh b/tests/harness/06_tags_and_stash.sh index 0ae94538..45c756b9 100755 --- a/tests/harness/06_tags_and_stash.sh +++ b/tests/harness/06_tags_and_stash.sh @@ -31,11 +31,12 @@ make_temp_repo "tag-basic" init_repo create_file "v1.txt" "version 1" -assert_success "add v1.txt" atomic add v1.txt -record_change "Release v1" >/dev/null 2>&1 || true +require_success "add v1.txt" atomic add v1.txt +require_success "record v1" record_change "Release v1" # Create a tag -tag_out="$(atomic tag create v1.0 2>&1)" +require_capture "create tag v1.0" atomic tag create v1.0 +tag_out="$COMMAND_OUTPUT" if echo "$tag_out" | grep -qiE "created|tag|v1.0"; then _pass "tag create v1.0 succeeds" else @@ -43,7 +44,8 @@ else fi # List tags — should contain v1.0 -list_out="$(atomic tag list 2>&1)" +require_capture "list tags" atomic tag list +list_out="$COMMAND_OUTPUT" if echo "$list_out" | grep -qF "v1.0"; then _pass "tag list shows v1.0" else @@ -54,7 +56,8 @@ fi begin_section "Tags: Show tag detail" # ═══════════════════════════════════════════════════════════════════════════ -show_out="$(atomic tag show v1.0 2>&1)" +require_capture "show tag v1.0" atomic tag show v1.0 +show_out="$COMMAND_OUTPUT" if echo "$show_out" | grep -qF "v1.0"; then _pass "tag show displays tag name" else @@ -64,13 +67,13 @@ fi if echo "$show_out" | grep -qiE "view.*dev|dev"; then _pass "tag show mentions the view" else - _pass "tag show completed" + _fail "tag show mentions the view" "got: $show_out" fi if echo "$show_out" | grep -qiE "state|merkle|sequence"; then _pass "tag show includes state info" else - _pass "tag show completed (format may vary)" + _fail "tag show includes state info" "got: $show_out" fi # ═══════════════════════════════════════════════════════════════════════════ @@ -78,17 +81,19 @@ begin_section "Tags: Multiple tags" # ═══════════════════════════════════════════════════════════════════════════ create_file "v2.txt" "version 2" -assert_success "add v2.txt" atomic add v2.txt -record_change "Release v2" >/dev/null 2>&1 || true +require_success "add v2.txt" atomic add v2.txt +require_success "record v2" record_change "Release v2" -tag_out2="$(atomic tag create v2.0 2>&1)" +require_capture "create tag v2.0" atomic tag create v2.0 +tag_out2="$COMMAND_OUTPUT" if echo "$tag_out2" | grep -qiE "created|tag|v2.0"; then _pass "tag create v2.0 succeeds" else _fail "tag create v2.0 succeeds" "got: $(echo "$tag_out2" | head -3)" fi -list_out2="$(atomic tag list 2>&1)" +require_capture "list both tags" atomic tag list +list_out2="$COMMAND_OUTPUT" found_tags=0 for t in v1.0 v2.0; do if echo "$list_out2" | grep -qF "$t"; then @@ -105,8 +110,9 @@ fi begin_section "Tags: Duplicate tag name fails" # ═══════════════════════════════════════════════════════════════════════════ -dup_out="$(atomic tag create v1.0 2>&1)" || true -if echo "$dup_out" | grep -qiE "already|exists|error|duplicate"; then +if dup_out="$(atomic tag create v1.0 2>&1)"; then + _fail "duplicate tag name rejected" "command succeeded: $dup_out" +elif echo "$dup_out" | grep -qiE "already|exists|error|duplicate"; then _pass "duplicate tag name rejected" else _fail "duplicate tag name rejected" "got: $(echo "$dup_out" | head -3)" @@ -116,7 +122,8 @@ fi begin_section "Tags: Delete a tag" # ═══════════════════════════════════════════════════════════════════════════ -del_out="$(atomic tag delete v1.0 2>&1)" +require_capture "delete tag v1.0" atomic tag delete v1.0 +del_out="$COMMAND_OUTPUT" if echo "$del_out" | grep -qiE "deleted|removed|v1.0"; then _pass "tag delete v1.0 succeeds" else @@ -124,7 +131,8 @@ else fi # v1.0 should be gone, v2.0 should remain -list_after="$(atomic tag list 2>&1)" +require_capture "list tags after delete" atomic tag list +list_after="$COMMAND_OUTPUT" if echo "$list_after" | grep -qF "v1.0"; then _fail "v1.0 gone after delete" "still present" else @@ -137,15 +145,17 @@ else fi # ═══════════════════════════════════════════════════════════════════════════ -begin_section "Tags: Delete nonexistent tag fails" +begin_section "Tags: Delete nonexistent tag is idempotent" # ═══════════════════════════════════════════════════════════════════════════ -del_missing="$(atomic tag delete no-such-tag 2>&1)" || true -if echo "$del_missing" | grep -qiE "not found|error|does not exist"; then - _pass "delete nonexistent tag fails gracefully" +require_capture "delete nonexistent tag" atomic tag delete no-such-tag +del_missing="$COMMAND_OUTPUT" +if echo "$del_missing" | grep -qiE "not found|does not exist"; then + _pass "delete nonexistent tag reports absence" else - _fail "delete nonexistent tag fails" "got: $(echo "$del_missing" | head -3)" + _fail "delete nonexistent tag reports absence" "got: $del_missing" fi +assert_output_contains "existing v2 tag remains" "v2.0" atomic tag list # ═══════════════════════════════════════════════════════════════════════════ begin_section "Tags: Tags survive view switches" @@ -155,16 +165,17 @@ make_temp_repo "tag-switch" init_repo create_file "base.txt" "base" -assert_success "add base.txt" atomic add base.txt -record_change "base commit" >/dev/null 2>&1 || true -atomic tag create release-1 >/dev/null 2>&1 +require_success "add base.txt" atomic add base.txt +require_success "record base" record_change "base commit" +require_success "create release-1 tag" atomic tag create release-1 -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" # Tag should still be visible from feature -list_from_feature="$(atomic tag list 2>&1)" +require_capture "list tags on feature" atomic tag list +list_from_feature="$COMMAND_OUTPUT" if echo "$list_from_feature" | grep -qF "release-1"; then _pass "tag visible from feature view" else @@ -172,8 +183,9 @@ else fi # Switch back to dev — tag still there -switch_view "dev" >/dev/null 2>&1 || true -list_back="$(atomic tag list 2>&1)" +require_success "switch to dev" switch_view "dev" +require_capture "list tags on dev" atomic tag list +list_back="$COMMAND_OUTPUT" if echo "$list_back" | grep -qF "release-1"; then _pass "tag persists after view round-trip" else @@ -185,13 +197,14 @@ begin_section "Tags: Tag on feature view" # ═══════════════════════════════════════════════════════════════════════════ # Continuing from previous -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" create_file "feat.txt" "feature" -assert_success "add feat.txt" atomic add feat.txt -record_change "feature work" >/dev/null 2>&1 || true -atomic tag create feature-done >/dev/null 2>&1 +require_success "add feat.txt" atomic add feat.txt +require_success "record feature work" record_change "feature work" +require_success "create feature tag" atomic tag create feature-done -list_feat="$(atomic tag list 2>&1)" +require_capture "list feature tags" atomic tag list +list_feat="$COMMAND_OUTPUT" if echo "$list_feat" | grep -qF "feature-done"; then _pass "tag created on feature view" else @@ -224,15 +237,16 @@ make_temp_repo "stash-basic" init_repo create_file "main.rs" "fn main() { v1 }" -assert_success "add main.rs" atomic add main.rs -record_change "Initial main.rs" >/dev/null 2>&1 || true +require_success "add main.rs" atomic add main.rs +require_success "record initial main.rs" record_change "Initial main.rs" # Dirty the file overwrite_file "main.rs" "fn main() { dirty }" assert_status_flag "main.rs is modified" "M" "main.rs" # Stash -stash_out="$(atomic stash 2>&1)" +require_capture "stash modified main.rs" atomic stash +stash_out="$COMMAND_OUTPUT" if echo "$stash_out" | grep -qiE "saved|stash"; then _pass "stash push succeeds" else @@ -242,15 +256,11 @@ fi # Working copy should be clean (restored to v1) assert_file_content "main.rs restored to v1 after stash" "main.rs" "fn main() { v1 }" -out="$(get_status_short)" -if echo "$out" | grep -qE "^M.*main\.rs"; then - _fail "main.rs clean after stash" "still modified" -else - _pass "main.rs clean after stash" -fi +assert_status_no_entry "main.rs clean after stash" "main.rs" # Pop -pop_out="$(atomic stash pop 2>&1)" +require_capture "pop main.rs stash" atomic stash pop +pop_out="$COMMAND_OUTPUT" if echo "$pop_out" | grep -qiE "applied|pop|stash"; then _pass "stash pop succeeds" else @@ -268,14 +278,15 @@ make_temp_repo "stash-list" init_repo create_file "f.txt" "clean" -assert_success "add f.txt" atomic add f.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record base" record_change "base" overwrite_file "f.txt" "dirty" -atomic stash >/dev/null 2>&1 +require_success "stash modification" atomic stash # List should show the stash -list_out="$(atomic stash list 2>&1)" +require_capture "list stashes" atomic stash list +list_out="$COMMAND_OUTPUT" if echo "$list_out" | grep -qE "stash@\{0\}|On dev"; then _pass "stash list shows stash@{0}" else @@ -283,10 +294,11 @@ else fi # Pop it to clean up -atomic stash pop >/dev/null 2>&1 || true +require_success "pop stash" atomic stash pop # List should be empty now -list_after="$(atomic stash list 2>&1)" +require_capture "list stashes after pop" atomic stash list +list_after="$COMMAND_OUTPUT" if echo "$list_after" | grep -qiE "no stash|empty" || ! echo "$list_after" | grep -qE "stash@"; then _pass "stash list empty after pop" else @@ -301,14 +313,15 @@ make_temp_repo "stash-apply" init_repo create_file "f.txt" "original" -assert_success "add f.txt" atomic add f.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record base" record_change "base" overwrite_file "f.txt" "modified" -atomic stash >/dev/null 2>&1 +require_success "stash modification" atomic stash # Apply (not pop) — restores content but keeps the stash -apply_out="$(atomic stash apply 2>&1)" +require_capture "apply stash" atomic stash apply +apply_out="$COMMAND_OUTPUT" if echo "$apply_out" | grep -qiE "applied|stash"; then _pass "stash apply succeeds" else @@ -319,7 +332,8 @@ fi assert_file_content "f.txt restored by apply" "f.txt" "modified" # Stash should still exist -list_still="$(atomic stash list 2>&1)" +require_capture "list stashes after apply" atomic stash list +list_still="$COMMAND_OUTPUT" if echo "$list_still" | grep -qE "stash@"; then _pass "stash still exists after apply (not pop)" else @@ -327,7 +341,7 @@ else fi # Clean up with drop -atomic stash drop >/dev/null 2>&1 || true +require_success "drop applied stash" atomic stash drop # ═══════════════════════════════════════════════════════════════════════════ begin_section "Stash: Drop a specific stash" @@ -337,14 +351,15 @@ make_temp_repo "stash-drop" init_repo create_file "f.txt" "base" -assert_success "add f.txt" atomic add f.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record base" record_change "base" overwrite_file "f.txt" "stashed" -atomic stash >/dev/null 2>&1 +require_success "stash modification" atomic stash # Drop the stash -drop_out="$(atomic stash drop 2>&1)" +require_capture "drop stash" atomic stash drop +drop_out="$COMMAND_OUTPUT" if echo "$drop_out" | grep -qiE "dropped|removed|stash"; then _pass "stash drop succeeds" else @@ -352,7 +367,8 @@ else fi # List should be empty -list_after_drop="$(atomic stash list 2>&1)" +require_capture "list stashes after drop" atomic stash list +list_after_drop="$COMMAND_OUTPUT" if echo "$list_after_drop" | grep -qE "stash@"; then _fail "stash list empty after drop" "still has entries" else @@ -367,15 +383,16 @@ make_temp_repo "stash-noop" init_repo create_file "f.txt" "clean" -assert_success "add f.txt" atomic add f.txt -record_change "clean state" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record clean state" record_change "clean state" # Stash with no dirty changes -noop_out="$(atomic stash 2>&1)" +require_capture "stash clean working copy" atomic stash +noop_out="$COMMAND_OUTPUT" if echo "$noop_out" | grep -qiE "no.*change|nothing|clean|no local"; then _pass "stash with clean working copy reports nothing to save" else - _pass "stash with clean working copy completed" + _fail "stash with clean working copy reports nothing to save" "got: $noop_out" fi # ═══════════════════════════════════════════════════════════════════════════ @@ -385,8 +402,9 @@ begin_section "Stash: Pop with no stashes fails gracefully" make_temp_repo "stash-no-pop" init_repo -pop_none="$(atomic stash pop 2>&1)" || true -if echo "$pop_none" | grep -qiE "no stash|not found|empty|error|invalid"; then +if pop_none="$(atomic stash pop 2>&1)"; then + _fail "pop with no stashes fails" "command succeeded: $pop_none" +elif echo "$pop_none" | grep -qiE "no stash|not found|empty|error|invalid"; then _pass "pop with no stashes fails gracefully" else _fail "pop with no stashes fails" "got: $(echo "$pop_none" | head -3)" @@ -402,8 +420,8 @@ init_repo create_file "code.py" "def hello(): print('hello') return True" -assert_success "add code.py" atomic add code.py -record_change "initial code.py" >/dev/null 2>&1 || true +require_success "add code.py" atomic add code.py +require_success "record initial code.py" record_change "initial code.py" # Make a specific modification overwrite_file "code.py" "def hello(): @@ -411,7 +429,7 @@ overwrite_file "code.py" "def hello(): return False" # Stash -atomic stash >/dev/null 2>&1 +require_success "stash code.py" atomic stash # Verify clean state assert_file_content "code.py clean after stash" "code.py" "def hello(): @@ -419,7 +437,7 @@ assert_file_content "code.py clean after stash" "code.py" "def hello(): return True" # Pop and verify exact content -atomic stash pop >/dev/null 2>&1 +require_success "pop code.py stash" atomic stash pop assert_file_content "code.py has exact modified content after pop" "code.py" "def hello(): print('goodbye') @@ -435,29 +453,30 @@ make_temp_repo "stash-cross-view" init_repo create_file "shared.txt" "shared" -assert_success "add shared.txt" atomic add shared.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add shared.txt" atomic add shared.txt +require_success "record base" record_change "base" # Dirty the file and stash overwrite_file "shared.txt" "dirty on dev" -atomic stash >/dev/null 2>&1 +require_success "stash dev modification" atomic stash assert_file_content "shared.txt clean after stash" "shared.txt" "shared" # Switch to feature, do work -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "feature.txt" "feature work" -assert_success "add feature.txt" atomic add feature.txt -record_change "feature work" >/dev/null 2>&1 || true +require_success "add feature.txt" atomic add feature.txt +require_success "record feature work" record_change "feature work" # Switch back to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_content "shared.txt still clean on dev" "shared.txt" "shared" # Stash list should still have the stash -list_cross="$(atomic stash list 2>&1)" +require_capture "list cross-view stash" atomic stash list +list_cross="$COMMAND_OUTPUT" if echo "$list_cross" | grep -qE "stash@"; then _pass "stash persists across view switches" else @@ -465,7 +484,7 @@ else fi # Pop the stash -atomic stash pop >/dev/null 2>&1 +require_success "pop dev stash" atomic stash pop assert_file_content "shared.txt dirty after pop" "shared.txt" "dirty on dev" # ═══════════════════════════════════════════════════════════════════════════ @@ -476,19 +495,20 @@ make_temp_repo "stash-multi" init_repo create_file "f.txt" "v0" -assert_success "add f.txt" atomic add f.txt -record_change "v0" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record v0" record_change "v0" # First stash overwrite_file "f.txt" "stash-1" -atomic stash -m "first stash" >/dev/null 2>&1 || true +require_success "create first stash" atomic stash -m "first stash" # Second stash overwrite_file "f.txt" "stash-2" -atomic stash -m "second stash" >/dev/null 2>&1 || true +require_success "create second stash" atomic stash -m "second stash" # Should have 2 stashes -list_multi="$(atomic stash list 2>&1)" +require_capture "list both stashes" atomic stash list +list_multi="$COMMAND_OUTPUT" stash_count="$(echo "$list_multi" | grep -c "stash@" || true)" if [[ $stash_count -ge 2 ]]; then _pass "two stashes in list ($stash_count)" @@ -497,23 +517,23 @@ else fi # Pop most recent first (LIFO) -atomic stash pop >/dev/null 2>&1 || true +require_success "pop second stash" atomic stash pop # After first pop, should have 1 stash left -list_after_pop1="$(atomic stash list 2>&1)" +require_capture "list stashes after first pop" atomic stash list +list_after_pop1="$COMMAND_OUTPUT" stash_count2="$(echo "$list_after_pop1" | grep -c "stash@" || true)" if [[ $stash_count2 -eq 1 ]]; then _pass "one stash left after first pop" -elif [[ $stash_count2 -lt $stash_count ]]; then - _pass "stash count decreased after pop ($stash_count2)" else - _fail "stash count decreased" "still $stash_count2" + _fail "one stash left after first pop" "found $stash_count2" fi # Pop second -atomic stash pop >/dev/null 2>&1 || true +require_success "pop first stash" atomic stash pop -list_after_pop2="$(atomic stash list 2>&1)" +require_capture "list stashes after second pop" atomic stash list +list_after_pop2="$COMMAND_OUTPUT" if echo "$list_after_pop2" | grep -qE "stash@"; then _fail "all stashes popped" "still has entries" else @@ -528,25 +548,27 @@ make_temp_repo "stash-clear" init_repo create_file "f.txt" "base" -assert_success "add f.txt" atomic add f.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record base" record_change "base" # Create a couple of stashes overwrite_file "f.txt" "s1" -atomic stash -m "s1" >/dev/null 2>&1 || true +require_success "create first stash" atomic stash -m "s1" overwrite_file "f.txt" "s2" -atomic stash -m "s2" >/dev/null 2>&1 || true +require_success "create second stash" atomic stash -m "s2" # Clear all -clear_out="$(atomic stash clear --force 2>&1)" +require_capture "clear stashes" atomic stash clear --force +clear_out="$COMMAND_OUTPUT" if echo "$clear_out" | grep -qiE "cleared|removed|dropped|stash"; then _pass "stash clear succeeds" else - _pass "stash clear completed" + _fail "stash clear succeeds" "got: $clear_out" fi # List should be empty -list_cleared="$(atomic stash list 2>&1)" +require_capture "list stashes after clear" atomic stash list +list_cleared="$COMMAND_OUTPUT" if echo "$list_cleared" | grep -qE "stash@"; then _fail "stash list empty after clear" "still has entries" else @@ -561,27 +583,30 @@ make_temp_repo "stash-untracked" init_repo create_file "tracked.txt" "tracked" -assert_success "add tracked.txt" atomic add tracked.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add tracked.txt" atomic add tracked.txt +require_success "record base" record_change "base" # Create an untracked file + modify a tracked one create_file "newfile.txt" "I am new" overwrite_file "tracked.txt" "modified tracked" # Stash with --include-untracked -stash_iu="$(atomic stash push --include-untracked 2>&1)" +require_capture "stash untracked and modified files" atomic stash push --include-untracked +stash_iu="$COMMAND_OUTPUT" if echo "$stash_iu" | grep -qiE "saved|stash"; then _pass "stash with --include-untracked succeeds" else - _pass "stash with --include-untracked completed" + _fail "stash with --include-untracked succeeds" "got: $stash_iu" fi # tracked.txt should be restored assert_file_content "tracked.txt restored" "tracked.txt" "tracked" +assert_file_not_exists "newfile.txt is removed while stashed" "newfile.txt" # Pop and verify -atomic stash pop >/dev/null 2>&1 || true +require_success "pop stash with untracked file" atomic stash pop assert_file_content "tracked.txt modified after pop" "tracked.txt" "modified tracked" +assert_file_content "newfile.txt restored after pop" "newfile.txt" "I am new" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Stash: Stash with custom message" @@ -591,25 +616,21 @@ make_temp_repo "stash-message" init_repo create_file "f.txt" "base" -assert_success "add f.txt" atomic add f.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record base" record_change "base" overwrite_file "f.txt" "wip" -atomic stash -m "work in progress" >/dev/null 2>&1 || true +require_success "stash custom message" atomic stash -m "work in progress" -list_msg="$(atomic stash list 2>&1)" +require_capture "list custom-message stash" atomic stash list +list_msg="$COMMAND_OUTPUT" if echo "$list_msg" | grep -qiE "work.in.progress|work-in-progress|wip"; then _pass "stash list shows custom message" else - # Message might be truncated or reformatted - if echo "$list_msg" | grep -qE "stash@"; then - _pass "stash list shows entry (message format may vary)" - else - _fail "stash list shows custom message" "got: $(echo "$list_msg" | head -3)" - fi + _fail "stash list shows custom message" "got: $list_msg" fi -atomic stash pop >/dev/null 2>&1 || true +require_success "pop custom-message stash" atomic stash pop # ═══════════════════════════════════════════════════════════════════════════ begin_section "Tags + Stash: Tag before stash, pop, verify state" @@ -622,18 +643,19 @@ make_temp_repo "tag-stash-combo" init_repo create_file "app.txt" "version 1" -assert_success "add app.txt" atomic add app.txt -record_change "v1" >/dev/null 2>&1 || true +require_success "add app.txt" atomic add app.txt +require_success "record v1" record_change "v1" # Tag the v1 state -atomic tag create v1-release >/dev/null 2>&1 +require_success "create v1 tag" atomic tag create v1-release # Dirty and stash overwrite_file "app.txt" "work in progress" -atomic stash >/dev/null 2>&1 +require_success "stash WIP" atomic stash # Tag should still be there -list_combo="$(atomic tag list 2>&1)" +require_capture "list tags after stash" atomic tag list +list_combo="$COMMAND_OUTPUT" if echo "$list_combo" | grep -qF "v1-release"; then _pass "tag survives stash operation" else @@ -645,11 +667,12 @@ assert_file_content "app.txt is v1 after stash" "app.txt" "version 1" # Record v2 and tag it overwrite_file "app.txt" "version 2" -record_change "v2" >/dev/null 2>&1 || true -atomic tag create v2-release >/dev/null 2>&1 +require_success "record v2" record_change "v2" +require_success "create v2 tag" atomic tag create v2-release # Both tags should exist -list_both="$(atomic tag list 2>&1)" +require_capture "list both tags" atomic tag list +list_both="$COMMAND_OUTPUT" found=0 for t in v1-release v2-release; do if echo "$list_both" | grep -qF "$t"; then @@ -663,7 +686,7 @@ else fi # Pop the stash — should restore dirty WIP state -atomic stash pop >/dev/null 2>&1 +require_success "pop WIP stash" atomic stash pop assert_file_content "app.txt has WIP after pop" "app.txt" "work in progress" @@ -684,34 +707,35 @@ init_repo # Step 1: v1 on dev + tag create_file "core.txt" "core v1" -assert_success "add core.txt" atomic add core.txt -record_change "Core v1" >/dev/null 2>&1 || true -atomic tag create v1.0 >/dev/null 2>&1 +require_success "add core.txt" atomic add core.txt +require_success "record core v1" record_change "Core v1" +require_success "create v1 tag" atomic tag create v1.0 # Step 2: feature work -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" +require_success "switch to feature" switch_view "feature" create_file "feature.txt" "feature content" -assert_success "add feature.txt" atomic add feature.txt -record_change "Feature work" >/dev/null 2>&1 || true +require_success "add feature.txt" atomic add feature.txt +require_success "record feature work" record_change "Feature work" # Step 3: switch to dev, start work, stash -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" overwrite_file "core.txt" "core v1 + WIP changes" assert_status_flag "core.txt modified on dev" "M" "core.txt" -atomic stash -m "dev WIP" >/dev/null 2>&1 +require_success "stash dev WIP" atomic stash -m "dev WIP" assert_file_content "core.txt clean after stash" "core.txt" "core v1" # Step 4: record v2, tag overwrite_file "core.txt" "core v2" -record_change "Core v2" >/dev/null 2>&1 || true -atomic tag create v2.0 >/dev/null 2>&1 +require_success "record core v2" record_change "Core v2" +require_success "create v2 tag" atomic tag create v2.0 # Verify tags -tags_now="$(atomic tag list 2>&1)" +require_capture "list tags after v2" atomic tag list +tags_now="$COMMAND_OUTPUT" for t in v1.0 v2.0; do if echo "$tags_now" | grep -qF "$t"; then _pass "tag $t exists after v2" @@ -721,16 +745,33 @@ for t in v1.0 v2.0; do done # Step 5: pop stash -atomic stash pop >/dev/null 2>&1 +require_success "pop dev WIP" atomic stash pop assert_file_content "core.txt has WIP after pop" "core.txt" "core v1 + WIP changes" -# Step 6: insert feature to dev -insert_from_view "feature" "dev" >/dev/null 2>&1 || true -switch_view "dev" >/dev/null 2>&1 || true +# Insertion must refuse to refresh a dirty working copy without discarding it. +if insert_out="$(insert_from_view "feature" "dev")"; then + _fail "insert refuses unrecorded WIP" "insert unexpectedly succeeded: $insert_out" + print_summary || true + exit 1 +elif echo "$insert_out" | grep -qi 'unrecorded changes'; then + _pass "insert refuses unrecorded WIP" +else + _fail "insert refuses unrecorded WIP" "unexpected refusal: $insert_out" + print_summary || true + exit 1 +fi +assert_file_content "refused insert preserves WIP" "core.txt" "core v1 + WIP changes" +require_success "record restored WIP" record_change "Record restored WIP" + +# Step 6: insert feature to dev after preserving the WIP in history. +require_success "insert feature into dev" insert_from_view "feature" "dev" +require_success "select dev" switch_view "dev" assert_file_exists "feature.txt on dev after insert" "feature.txt" +assert_file_content "recorded WIP survives insert" "core.txt" "core v1 + WIP changes" # Step 7: verify tags still intact -tags_final="$(atomic tag list 2>&1)" +require_capture "list tags after insert" atomic tag list +tags_final="$COMMAND_OUTPUT" for t in v1.0 v2.0; do if echo "$tags_final" | grep -qF "$t"; then _pass "tag $t survives full workflow" @@ -740,7 +781,7 @@ for t in v1.0 v2.0; do done # Verify feature is intact -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" assert_file_exists "feature.txt still on feature" "feature.txt" assert_file_exists "core.txt on feature" "core.txt" @@ -754,23 +795,24 @@ make_temp_repo "stash-nested" init_repo create_file "f.txt" "base" -assert_success "add f.txt" atomic add f.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record base" record_change "base" -new_view "feature" >/dev/null 2>&1 || true -insert_from_view "dev" "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "insert dev into feature" insert_from_view "dev" "feature" # Stash on dev overwrite_file "f.txt" "dev dirty" -atomic stash -m "dev stash" >/dev/null 2>&1 || true +require_success "stash dev changes" atomic stash -m "dev stash" # Switch to feature, make dirty changes, stash -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature" switch_view "feature" overwrite_file "f.txt" "feature dirty" -atomic stash -m "feature stash" >/dev/null 2>&1 || true +require_success "stash feature changes" atomic stash -m "feature stash" # Should have 2 stashes total -list_nested="$(atomic stash list 2>&1)" +require_capture "list nested stashes" atomic stash list +list_nested="$COMMAND_OUTPUT" nested_count="$(echo "$list_nested" | grep -c "stash@" || true)" if [[ $nested_count -ge 2 ]]; then _pass "two stashes from different views ($nested_count)" @@ -779,14 +821,15 @@ else fi # Switch back to dev, pop most recent stash (feature's) -switch_view "dev" >/dev/null 2>&1 || true -atomic stash pop >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" +require_success "pop most recent stash" atomic stash pop # Pop the dev stash -atomic stash pop >/dev/null 2>&1 || true +require_success "pop remaining stash" atomic stash pop # Should have no stashes left -list_final="$(atomic stash list 2>&1)" +require_capture "list stashes after both pops" atomic stash list +list_final="$COMMAND_OUTPUT" if echo "$list_final" | grep -qE "stash@"; then _fail "all stashes popped" "still has entries" else @@ -801,18 +844,19 @@ make_temp_repo "stash-keep" init_repo create_file "f.txt" "original" -assert_success "add f.txt" atomic add f.txt -record_change "base" >/dev/null 2>&1 || true +require_success "add f.txt" atomic add f.txt +require_success "record base" record_change "base" overwrite_file "f.txt" "dirty" -atomic stash push --keep >/dev/null 2>&1 || true +require_success "stash with keep" atomic stash push --keep # With --keep, the working copy should NOT be restored to clean state # (the dirty content remains on disk) assert_file_content "f.txt still dirty with --keep" "f.txt" "dirty" # But a stash should still exist -list_keep="$(atomic stash list 2>&1)" +require_capture "list kept stash" atomic stash list +list_keep="$COMMAND_OUTPUT" if echo "$list_keep" | grep -qE "stash@"; then _pass "stash created with --keep" else @@ -820,7 +864,7 @@ else fi # Clean up -atomic stash drop >/dev/null 2>&1 || true +require_success "drop kept stash" atomic stash drop # ═══════════════════════════════════════════════════════════════════════════ diff --git a/tests/harness/07_server_push.sh b/tests/harness/07_server_push.sh index dd7676c0..e82ba246 100644 --- a/tests/harness/07_server_push.sh +++ b/tests/harness/07_server_push.sh @@ -11,7 +11,8 @@ # 5. The server switches to that view and outputs the working copy # 6. The file on disk must have correct content — no duplication # -# This is the exact code path that the API push handler follows. +# This simulates transfer by copying change objects; it does not exercise the +# API server, network transport, authentication, or remote push protocol. # The bug being tested: after inserting a change (with dependencies on a # shared view) into a draft view and outputting the working copy, the # file content was duplicated on the server. @@ -29,6 +30,8 @@ HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" +SNAPSHOT_DIR="$(mktemp -d "${TMPDIR:-/tmp}/atomic-test-push-snapshots-XXXXXX")" +_HARNESS_TMPDIRS+=("$SNAPSHOT_DIR") # ═══════════════════════════════════════════════════════════════════════════ begin_section "Server Push: Setup client repo with two changes" @@ -57,13 +60,15 @@ const message = greet("World"); console.log(message); EOF -add_files src/index.ts +require_success "add client source" atomic add src/index.ts +CLIENT_V1="$SNAPSHOT_DIR/client-v1" +cp src/index.ts "$CLIENT_V1" # Extract the exact hash of the change file created by the most recent # record operation. Sorting by filename is unsafe because repo init writes # bootstrap changes whose hashes may sort after later user changes. capture_change_hashes() { - find "$REPO_DIR/.atomic/changes" -name "*.change" -type f 2>/dev/null \ + find "$REPO_DIR/.atomic/changes" -name "*.change" -type f \ | xargs -I{} basename {} .change | sort } @@ -71,17 +76,25 @@ find_new_change_hash() { local before_file="$1" local after_file="$2" - comm -13 "$before_file" "$after_file" | tail -1 + local changes + changes="$(comm -13 "$before_file" "$after_file")" + if [[ -z "$changes" || "$changes" == *$'\n'* ]]; then + echo "expected exactly one new change object, got: $changes" >&2 + return 1 + fi + printf '%s\n' "$changes" } -BEFORE_1="$(mktemp)" -AFTER_1="$(mktemp)" +BEFORE_1="$SNAPSHOT_DIR/before-1" +AFTER_1="$SNAPSHOT_DIR/after-1" capture_change_hashes > "$BEFORE_1" -REC1_OUT="$(record_change "Initial TypeScript file" 2>&1)" +require_capture "record initial TypeScript file" record_change "Initial TypeScript file" +REC1_OUT="$COMMAND_OUTPUT" capture_change_hashes > "$AFTER_1" -HASH1="$(find_new_change_hash "$BEFORE_1" "$AFTER_1")" +require_capture "identify first change object" find_new_change_hash "$BEFORE_1" "$AFTER_1" +HASH1="$COMMAND_OUTPUT" if [[ -n "$HASH1" ]]; then _pass "Recorded change 1 on client: ${HASH1:0:12}" @@ -106,14 +119,16 @@ console.log(message); EOF # Mark before recording so we can find the NEW change file -BEFORE_2="$(mktemp)" -AFTER_2="$(mktemp)" +BEFORE_2="$SNAPSHOT_DIR/before-2" +AFTER_2="$SNAPSHOT_DIR/after-2" capture_change_hashes > "$BEFORE_2" -REC2_OUT="$(record_change "Add ANSI color codes" 2>&1)" +require_capture "record ANSI color codes" record_change "Add ANSI color codes" +REC2_OUT="$COMMAND_OUTPUT" capture_change_hashes > "$AFTER_2" -HASH2="$(find_new_change_hash "$BEFORE_2" "$AFTER_2")" +require_capture "identify second change object" find_new_change_hash "$BEFORE_2" "$AFTER_2" +HASH2="$COMMAND_OUTPUT" if [[ -n "$HASH2" && "$HASH2" != "$HASH1" ]]; then _pass "Recorded change 2 on client: ${HASH2:0:12}" @@ -126,8 +141,8 @@ fi # Save the client's final file content for comparison CLIENT_CONTENT="$(cat src/index.ts)" CLIENT_LINES="$(wc -l < src/index.ts | tr -d ' ')" - -_pass "Client file has $CLIENT_LINES lines" +CLIENT_V2="$SNAPSHOT_DIR/client-v2" +cp src/index.ts "$CLIENT_V2" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Server Push: Init server and copy change files" @@ -159,8 +174,7 @@ copy_change_file() { fi if [[ -n "$src_file" && -f "$src_file" ]]; then - cp "$src_file" "$dst_dir/" - return 0 + cp "$src_file" "$dst_dir/" && cmp -s "$src_file" "$dst_dir/${hash}.change" else echo " DEBUG: looking for ${hash}.change in $src_dir" >&2 echo " DEBUG: files in $src_dir:" >&2 @@ -194,13 +208,7 @@ begin_section "Server Push: Insert change 1 into dev (shared view)" cd "$SERVER_DIR" -APPLY1_OUT="$(atomic insert "$HASH1" --view dev 2>&1)" || true - -if echo "$APPLY1_OUT" | grep -qiE "applied|success|state"; then - _pass "Inserted change 1 into dev" -else - _pass "Insert change 1 into dev completed: $(echo "$APPLY1_OUT" | head -3)" -fi +require_success "insert change 1 into dev" atomic insert "$HASH1" --view dev # Verify the file exists and has correct content assert_file_exists \ @@ -218,8 +226,7 @@ else "'function greet' appears $GREET_COUNT_1 times (expected 1). Content: $(echo "$CONTENT_AFTER_1" | head -20)" fi -LINES_AFTER_1="$(echo "$CONTENT_AFTER_1" | wc -l | tr -d ' ')" -_pass "After change 1: file has $LINES_AFTER_1 lines" +assert_success "first server content matches client bytes" cmp -s "$CLIENT_V1" src/index.ts # ═══════════════════════════════════════════════════════════════════════════ begin_section "Server Push: Create draft view and insert change 2" @@ -231,27 +238,14 @@ begin_section "Server Push: Create draft view and insert change 2" LOCAL_VIEW="agent-ses_test-harness" # Create the draft view (same as what the API push handler does) -NEW_OUT="$(new_view "$LOCAL_VIEW" 2>&1)" || true - -if echo "$NEW_OUT" | grep -qiE "created|view"; then - _pass "Created draft view '$LOCAL_VIEW'" -else - _pass "Draft view creation completed" -fi +require_success "create draft view '$LOCAL_VIEW'" new_view "$LOCAL_VIEW" assert_view_exists "Draft view exists" "$LOCAL_VIEW" # Insert change 2 into the draft view. # Change 2 depends on change 1, which is on dev (shared). -# The draft view's overlay should see change 1's edges through GRAPH, -# and change 2's edges should go to STACK_GRAPH. -APPLY2_OUT="$(atomic insert "$HASH2" --view "$LOCAL_VIEW" 2>&1)" || true - -if echo "$APPLY2_OUT" | grep -qiE "applied|success|state"; then - _pass "Inserted change 2 into draft view" -else - _pass "Insert change 2 completed: $(echo "$APPLY2_OUT" | head -3)" -fi +# The draft inherits change 1 and selects change 2 through its view filter. +require_success "insert change 2 into draft view" atomic insert "$HASH2" --view "$LOCAL_VIEW" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Server Push: Switch to draft view and verify output" @@ -261,7 +255,7 @@ begin_section "Server Push: Switch to draft view and verify output" # This is where the bug manifested — the file was duplicated or showed # the wrong content. -switch_view "$LOCAL_VIEW" >/dev/null 2>&1 || true +require_success "switch to draft view" switch_view "$LOCAL_VIEW" assert_current_view "Switched to draft view" "$LOCAL_VIEW" # The file must exist @@ -336,6 +330,7 @@ else _fail "Draft view: content matches client" \ "Server content differs from client. Server ($LINES_LOCAL lines): $(echo "$CONTENT_LOCAL" | head -15) --- Client ($CLIENT_LINES lines): $(echo "$CLIENT_CONTENT" | head -15)" fi +assert_success "draft content matches client byte-for-byte" cmp -s "$CLIENT_V2" src/index.ts # ═══════════════════════════════════════════════════════════════════════════ begin_section "Server Push: Dev perspective unaffected by draft view" @@ -344,8 +339,9 @@ begin_section "Server Push: Dev perspective unaffected by draft view" # After switching back to dev, the file should show change 1's content # (the original without color codes). Change 2 is only on the draft view. -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_current_view "Back on dev" "dev" +assert_success "dev content matches original client bytes" cmp -s "$CLIENT_V1" src/index.ts CONTENT_DEV="$(cat src/index.ts 2>/dev/null || echo "")" @@ -381,7 +377,7 @@ begin_section "Server Push: Round-trip — draft view still correct" # Switch back to the draft view. The file should still have the correct # content — no corruption from the dev→draft round trip. -switch_view "$LOCAL_VIEW" >/dev/null 2>&1 || true +require_success "switch back to draft" switch_view "$LOCAL_VIEW" assert_current_view "Back on draft view" "$LOCAL_VIEW" CONTENT_ROUNDTRIP="$(cat src/index.ts 2>/dev/null || echo "")" @@ -393,6 +389,7 @@ else _fail "Round-trip: no duplication" \ "'function greet' appears $GREET_COUNT_RT times after round-trip" fi +assert_success "round-trip content matches client byte-for-byte" cmp -s "$CLIENT_V2" src/index.ts if [[ "$CONTENT_ROUNDTRIP" == "$CLIENT_CONTENT" ]]; then _pass "Round-trip: content still matches client" @@ -430,16 +427,20 @@ console.log(message); console.log(farewell("World")); EOF -BEFORE_3="$(mktemp)" -AFTER_3="$(mktemp)" +BEFORE_3="$SNAPSHOT_DIR/before-3" +AFTER_3="$SNAPSHOT_DIR/after-3" capture_change_hashes > "$BEFORE_3" -REC3_OUT="$(record_change "Add farewell function" 2>&1)" +require_capture "record farewell function" record_change "Add farewell function" +REC3_OUT="$COMMAND_OUTPUT" capture_change_hashes > "$AFTER_3" -HASH3="$(find_new_change_hash "$BEFORE_3" "$AFTER_3")" +require_capture "identify third change object" find_new_change_hash "$BEFORE_3" "$AFTER_3" +HASH3="$COMMAND_OUTPUT" CLIENT_CONTENT_V3="$(cat src/index.ts)" +CLIENT_V3="$SNAPSHOT_DIR/client-v3" +cp src/index.ts "$CLIENT_V3" if [[ -n "$HASH3" ]]; then _pass "Recorded change 3 on client: ${HASH3:0:12}" @@ -462,15 +463,9 @@ else fi # Make sure we're on the draft view -switch_view "$LOCAL_VIEW" >/dev/null 2>&1 || true +require_success "select draft view" switch_view "$LOCAL_VIEW" -APPLY3_OUT="$(atomic insert "$HASH3" --view "$LOCAL_VIEW" 2>&1)" || true - -if echo "$APPLY3_OUT" | grep -qiE "applied|success|state"; then - _pass "Inserted change 3 into draft view" -else - _pass "Insert change 3 completed" -fi +require_success "insert change 3 into draft view" atomic insert "$HASH3" --view "$LOCAL_VIEW" # Re-output working copy (switch_view already did this, but be explicit) # In the API, output_working_copy is called after insert. @@ -487,6 +482,7 @@ else _fail "After change 3: no greet duplication" \ "'function greet' appears $GREET_COUNT_V3 times. Content: $(echo "$CONTENT_V3" | head -25)" fi +assert_success "third change matches client byte-for-byte" cmp -s "$CLIENT_V3" src/index.ts if [[ "$FAREWELL_COUNT" -eq 1 ]]; then _pass "After change 3: 'function farewell' appears exactly once" @@ -513,7 +509,9 @@ fi begin_section "Server Push: Dev still unaffected after 3 draft changes" # ═══════════════════════════════════════════════════════════════════════════ -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev after third change" switch_view "dev" +assert_current_view "on dev after third change" "dev" +assert_success "dev still matches original client bytes" cmp -s "$CLIENT_V1" src/index.ts CONTENT_DEV_FINAL="$(cat src/index.ts 2>/dev/null || echo "")" diff --git a/tests/harness/08_draft_view_insert.sh b/tests/harness/08_draft_view_insert.sh index 190bcc40..4f3da246 100644 --- a/tests/harness/08_draft_view_insert.sh +++ b/tests/harness/08_draft_view_insert.sh @@ -1,21 +1,16 @@ #!/usr/bin/env bash # 08_draft_view_insert.sh — Draft view insert correctness at scale. # -# Tests the exact code path that the API server push handler follows: -# changes are recorded on a client repo's draft (agent) view, copied -# to a server repo, and inserted into the same draft view on the server. +# Simulates object transfer without exercising network push or the API server: +# changes are recorded on a client draft view, copied to a server repository, +# and inserted into the same draft view on the server. # # The critical invariant: after inserting N sequential changes that modify # the same file, the server's working copy must contain ONLY the final # state — not a concatenation of all intermediate states. # -# This is the spec for the duplication bug: -# When two or more changes are inserted into the same draft view via -# `atomic insert --view `, the second change's EdgeUpdate -# (which replaces old content with new content) must properly delete -# the old BLOCK edge in STACK_GRAPH before adding the new one. -# Without this, the graph traversal sees BOTH the old and new content -# vertices as alive, producing duplicated file output. +# Each incoming native span edit must replace the intended prior content +# rather than accumulating intermediate versions. # # Invariants tested at every step: # @@ -34,6 +29,8 @@ HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" +SNAPSHOT_DIR="$(mktemp -d "${TMPDIR:-/tmp}/atomic-test-insert-snapshots-XXXXXX")" +_HARNESS_TMPDIRS+=("$SNAPSHOT_DIR") # ═══════════════════════════════════════════════════════════════════════════ # Helper: copy a single change file from client to server @@ -49,51 +46,32 @@ copy_change_to_server() { mkdir -p "$dst_dir" - local src_file - src_file="$(find "$src_dir" -name "${hash}*" -type f 2>/dev/null | head -1)" - - if [[ -z "$src_file" ]]; then - src_file="$(find "$client_changes" -name "${hash}*" -type f 2>/dev/null | head -1)" - fi - - if [[ -n "$src_file" && -f "$src_file" ]]; then - cp "$src_file" "$dst_dir/" - return 0 + local src_file="$src_dir/${hash}.change" + if [[ -f "$src_file" ]]; then + cp "$src_file" "$dst_dir/" && cmp -s "$src_file" "$dst_dir/${hash}.change" + else + echo "missing client change object: $src_file" >&2 + return 1 fi - return 1 } # ═══════════════════════════════════════════════════════════════════════════ # Helper: get the full hash of the most recently recorded change # ═══════════════════════════════════════════════════════════════════════════ -# We track known hashes and find the newest one that isn't already known. -# CRITICAL: changes must be discovered in the order they were recorded, -# not alphabetically by hash. We use modification time (newest first) -# so the most recently written .change file is found. -KNOWN_HASHES="" - -get_newest_change_hash() { - local changes_dir="$1" - local newest="" - - # Sort by modification time (newest last) so the last unknown file - # is the most recently recorded change. - while IFS= read -r f; do - local h - h="$(basename "$f" .change)" - if echo "$KNOWN_HASHES" | grep -qF "$h"; then - continue - fi - newest="$h" - done < <(find "$changes_dir" -name "*.change" -type f -print0 2>/dev/null \ - | xargs -0 ls -tr 2>/dev/null) - - if [[ -n "$newest" ]]; then - KNOWN_HASHES="${KNOWN_HASHES}${newest} -" - echo "$newest" +capture_change_hashes() { + find "$CLIENT_DIR/.atomic/changes" -name '*.change' -type f \ + | xargs -I{} basename {} .change | sort +} + +find_new_change_hash() { + local changes + changes="$(comm -13 "$1" "$2")" + if [[ -z "$changes" || "$changes" == *$'\n'* ]]; then + echo "expected exactly one new change object, got: $changes" >&2 + return 1 fi + printf '%s\n' "$changes" } # ═══════════════════════════════════════════════════════════════════════════ @@ -106,8 +84,8 @@ init_repo AGENT_VIEW="agent-ses_scale-test" -new_view "$AGENT_VIEW" >/dev/null 2>&1 || true -switch_view "$AGENT_VIEW" >/dev/null 2>&1 || true +require_success "create client agent view" new_view "$AGENT_VIEW" +require_success "switch to client agent view" switch_view "$AGENT_VIEW" assert_current_view "Client on agent view" "$AGENT_VIEW" # ═══════════════════════════════════════════════════════════════════════════ @@ -148,9 +126,7 @@ NUM_CHANGES=10 # Change 9: DELETE metrics, MODIFY main back to simple, ADD footer comment # Change 10: MODIFY everything — final clean version # -# At EVERY step, some lines are added, some are modified (replaced), and -# some are deleted. This ensures the EdgeUpdate (del old BLOCK + add new -# DELETED|BLOCK) path is exercised thoroughly. +# The revisions exercise successive additions, replacements, and deletions. generate_version() { local v="$1" @@ -392,15 +368,19 @@ for i in $(seq 1 $NUM_CHANGES); do # Save snapshot SNAPSHOTS[$i]="$(cat src/app.ts)" + cp src/app.ts "$SNAPSHOT_DIR/v$i" EXPECTED_LINE_COUNTS[$i]="$(wc -l < src/app.ts | tr -d ' ')" if [[ "$i" -eq 1 ]]; then - add_files src/app.ts + require_success "add client app.ts" atomic add src/app.ts fi - REC_OUT="$(record_change "Version ${i}" 2>&1)" - - HASH="$(get_newest_change_hash "$CLIENT_DIR/.atomic/changes")" + capture_change_hashes > "$SNAPSHOT_DIR/before" + require_capture "record version $i" record_change "Version ${i}" + REC_OUT="$COMMAND_OUTPUT" + capture_change_hashes > "$SNAPSHOT_DIR/after" + require_capture "identify version $i object" find_new_change_hash "$SNAPSHOT_DIR/before" "$SNAPSHOT_DIR/after" + HASH="$COMMAND_OUTPUT" if [[ -n "$HASH" ]]; then CHANGE_HASHES[$i]="$HASH" @@ -415,7 +395,7 @@ done # Final client state CLIENT_FINAL="$(cat src/app.ts)" CLIENT_FINAL_LINES="$(wc -l < src/app.ts | tr -d ' ')" -_pass "Client final: $CLIENT_FINAL_LINES lines" +assert_success "client final snapshot matches bytes" cmp -s "$SNAPSHOT_DIR/v$NUM_CHANGES" src/app.ts # ═══════════════════════════════════════════════════════════════════════════ begin_section "Setup: Server repo" @@ -431,18 +411,11 @@ SERVER_CHANGES="$SERVER_DIR/.atomic/changes" # Copy ALL change files to server for i in $(seq 1 $NUM_CHANGES); do HASH="${CHANGE_HASHES[$i]}" - if copy_change_to_server "$HASH" "$CLIENT_CHANGES" "$SERVER_CHANGES"; then - : # silent success - else - _fail "Copy change $i to server" "Hash: $HASH" - print_summary - exit 1 - fi + require_success "copy change $i to server byte-for-byte" copy_change_to_server "$HASH" "$CLIENT_CHANGES" "$SERVER_CHANGES" done -_pass "Copied all $NUM_CHANGES change files to server" # Create the same agent view on the server -new_view "$AGENT_VIEW" >/dev/null 2>&1 || true +require_success "create server agent view" new_view "$AGENT_VIEW" assert_view_exists "Server has agent view" "$AGENT_VIEW" # ═══════════════════════════════════════════════════════════════════════════ @@ -462,10 +435,10 @@ for i in $(seq 1 $NUM_CHANGES); do EXPECTED_LINES="${EXPECTED_LINE_COUNTS[$i]}" # Insert - APPLY_OUT="$(atomic insert "$HASH" --view "$AGENT_VIEW" 2>&1)" || true + require_success "insert version $i into agent view" atomic insert "$HASH" --view "$AGENT_VIEW" # Switch to draft view to trigger output_working_copy - switch_view "$AGENT_VIEW" >/dev/null 2>&1 || true + require_success "switch to agent view for version $i" switch_view "$AGENT_VIEW" # Read the file CONTENT="$(cat src/app.ts 2>/dev/null || echo "")" @@ -594,9 +567,10 @@ for i in $(seq 1 $NUM_CHANGES); do _fail "v${i}: content mismatch" \ "Server content differs from client at version $i" fi + assert_success "v${i}: content matches client byte-for-byte" cmp -s "$SNAPSHOT_DIR/v$i" src/app.ts # Switch back to dev between inserts (simulates server staying on dev) - switch_view "dev" >/dev/null 2>&1 || true + require_success "switch to dev after version $i" switch_view "dev" done # ═══════════════════════════════════════════════════════════════════════════ @@ -606,7 +580,7 @@ begin_section "Final verification: dev isolation" # After all 10 changes (with adds, modifies, AND deletes) on the draft # view, dev should still be empty — the app.ts file should NOT exist on dev. -switch_view "dev" >/dev/null 2>&1 || true +require_success "select dev for isolation check" switch_view "dev" assert_current_view "On dev for isolation check" "dev" if [[ ! -f "src/app.ts" ]]; then @@ -622,7 +596,8 @@ fi begin_section "Final verification: draft view round-trip" # ═══════════════════════════════════════════════════════════════════════════ -switch_view "$AGENT_VIEW" >/dev/null 2>&1 || true +require_success "switch to agent for final verification" switch_view "$AGENT_VIEW" +assert_current_view "on agent for final verification" "$AGENT_VIEW" FINAL_CONTENT="$(cat src/app.ts 2>/dev/null || echo "")" FINAL_LINES="$(echo "$FINAL_CONTENT" | wc -l | tr -d ' ')" @@ -655,9 +630,10 @@ for gone_term in "formatName" "try {" "callCount" "emoji"; do if echo "$FINAL_CONTENT" | grep -qF "$gone_term"; then _fail "Final: stale content '$gone_term'" \ "Term '$gone_term' was deleted in an earlier version but still present" + else + _pass "Final: no stale content '$gone_term'" fi done -_pass "Final: no stale content from deleted versions" if [[ "$FINAL_CONTENT" == "$CLIENT_FINAL" ]]; then _pass "Final: server content matches client exactly" @@ -665,6 +641,7 @@ else _fail "Final: content mismatch" \ "Server content differs from client's final state" fi +assert_success "final content matches client byte-for-byte" cmp -s "$SNAPSHOT_DIR/v$NUM_CHANGES" src/app.ts # ═══════════════════════════════════════════════════════════════════════════ begin_section "Scale check: file size growth is linear, not quadratic" diff --git a/tests/harness/09_semantic_diff_pairing.sh b/tests/harness/09_semantic_diff_pairing.sh index b9d92c69..e1febfef 100644 --- a/tests/harness/09_semantic_diff_pairing.sh +++ b/tests/harness/09_semantic_diff_pairing.sh @@ -49,15 +49,27 @@ HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" +SNAPSHOT_DIR="$(mktemp -d "${TMPDIR:-/tmp}/atomic-test-diff-snapshots-XXXXXX")" +_HARNESS_TMPDIRS+=("$SNAPSHOT_DIR") # ═══════════════════════════════════════════════════════════════════════════ # Helper: get the full hash of the latest change # ═══════════════════════════════════════════════════════════════════════════ -get_last_change_hash() { - # Find the newest .change file by modification time - find "$REPO_DIR/.atomic/changes" -name "*.change" -type f -print0 2>/dev/null \ - | xargs -0 ls -t 2>/dev/null | head -1 | xargs basename 2>/dev/null | sed 's/\.change$//' +record_for_diff() { + local message="$1" output hash + find "$REPO_DIR/.atomic/changes" -name '*.change' -type f | xargs -I{} basename {} .change | sort > "$SNAPSHOT_DIR/before" || return 1 + if ! output="$(record_change "$message" 2>&1)"; then + printf '%s\n' "$output" >&2 + return 1 + fi + find "$REPO_DIR/.atomic/changes" -name '*.change' -type f | xargs -I{} basename {} .change | sort > "$SNAPSHOT_DIR/after" || return 1 + hash="$(comm -13 "$SNAPSHOT_DIR/before" "$SNAPSHOT_DIR/after")" || return 1 + if [[ -z "$hash" || "$hash" == *$'\n'* ]]; then + printf 'expected one new change object, got: %s\n' "$hash" >&2 + return 1 + fi + printf '%s\n' "$hash" } # ═══════════════════════════════════════════════════════════════════════════ @@ -154,8 +166,8 @@ function greet(name: string): string { console.log(greet("World")); EOF -add_files src/app.ts -record_change "Initial file" >/dev/null 2>&1 +require_success "add app.ts" atomic add src/app.ts +require_success "record initial file" record_change "Initial file" # Modify: add imports at top, change the console.log call cat > src/app.ts << 'EOF' @@ -175,9 +187,8 @@ rl.question("Name: ", (answer) => { }); EOF -record_change "Add readline, modify console.log" >/dev/null 2>&1 - -HASH="$(get_last_change_hash)" +require_capture "record readline modification" record_for_diff "Add readline, modify console.log" +HASH="$COMMAND_OUTPUT" if [[ -z "$HASH" ]]; then _fail "Get change hash" "Could not find change hash" @@ -185,7 +196,8 @@ if [[ -z "$HASH" ]]; then exit 1 fi -DIFF_OUT="$(atomic diff --no-color -c "$HASH" 2>&1)" +require_capture "diff readline modification" atomic diff --no-color -c "$HASH" +DIFF_OUT="$COMMAND_OUTPUT" # The old line: console.log(greet("World")); # The new line: console.log(greet(name)); @@ -226,8 +238,8 @@ export const RETRIES = 3; export const DEBUG = false; EOF -add_files src/config.ts -record_change "Initial config" >/dev/null 2>&1 +require_success "add config.ts" atomic add src/config.ts +require_success "record initial config" record_change "Initial config" # Change only one value: TIMEOUT 30 → 60 cat > src/config.ts << 'EOF' @@ -237,10 +249,10 @@ export const RETRIES = 3; export const DEBUG = false; EOF -record_change "Increase timeout" >/dev/null 2>&1 - -HASH="$(get_last_change_hash)" -DIFF_OUT="$(atomic diff --no-color -c "$HASH" 2>&1)" +require_capture "record timeout modification" record_for_diff "Increase timeout" +HASH="$COMMAND_OUTPUT" +require_capture "diff timeout modification" atomic diff --no-color -c "$HASH" +DIFF_OUT="$COMMAND_OUTPUT" # The delete should contain "TIMEOUT = 30" and the insert "TIMEOUT = 60" # and they should be adjacent @@ -288,8 +300,8 @@ function start() { start(); EOF -add_files src/server.ts -record_change "Initial server config" >/dev/null 2>&1 +require_success "add server.ts" atomic add src/server.ts +require_success "record initial server config" record_change "Initial server config" # Change HOST, PORT, and DB_HOST (3 modifications, rest unchanged) cat > src/server.ts << 'EOF' @@ -307,10 +319,10 @@ function start() { start(); EOF -record_change "Production config" >/dev/null 2>&1 - -HASH="$(get_last_change_hash)" -DIFF_OUT="$(atomic diff --no-color -c "$HASH" 2>&1)" +require_capture "record production config" record_for_diff "Production config" +HASH="$COMMAND_OUTPUT" +require_capture "diff production config" atomic diff --no-color -c "$HASH" +DIFF_OUT="$COMMAND_OUTPUT" CHANGE_LINES="$(printf '%s\n' "$DIFF_OUT" | extract_atomic_change_lines)" assert_paired_diff \ @@ -362,8 +374,8 @@ function hello(): string { console.log(hello()); EOF -add_files src/main.ts -record_change "Simple hello" >/dev/null 2>&1 +require_success "add main.ts" atomic add src/main.ts +require_success "record simple hello" record_change "Simple hello" # Major refactor: add error handling, logging, config — AND modify the # console.log call (World → name). The modification is line 5 old → line 20+ new. @@ -402,10 +414,10 @@ try { } EOF -record_change "Major refactor with modified hello call" >/dev/null 2>&1 - -HASH="$(get_last_change_hash)" -DIFF_OUT="$(atomic diff --no-color -c "$HASH" 2>&1)" +require_capture "record major refactor" record_for_diff "Major refactor with modified hello call" +HASH="$COMMAND_OUTPUT" +require_capture "diff major refactor" atomic diff --no-color -c "$HASH" +DIFF_OUT="$COMMAND_OUTPUT" # The old line: console.log(hello()); # In the new file, the call is now: console.log(message); @@ -443,8 +455,8 @@ export function add(a: number, b: number): number { } EOF -add_files src/utils.ts -record_change "Initial utils" >/dev/null 2>&1 +require_success "add utils.ts" atomic add src/utils.ts +require_success "record initial utils" record_change "Initial utils" # Add a new function (pure addition) — no modification of existing lines cat > src/utils.ts << 'EOF' @@ -457,10 +469,10 @@ export function multiply(a: number, b: number): number { } EOF -record_change "Add multiply function" >/dev/null 2>&1 - -HASH="$(get_last_change_hash)" -DIFF_OUT="$(atomic diff --no-color -c "$HASH" 2>&1)" +require_capture "record multiply function" record_for_diff "Add multiply function" +HASH="$COMMAND_OUTPUT" +require_capture "diff multiply function" atomic diff --no-color -c "$HASH" +DIFF_OUT="$COMMAND_OUTPUT" # Should have only additions, no deletions # Exclude diff header lines (--- a/file) from the count @@ -496,8 +508,8 @@ export function currentHelper(): void { } EOF -add_files src/legacy.ts -record_change "Initial legacy" >/dev/null 2>&1 +require_success "add legacy.ts" atomic add src/legacy.ts +require_success "record initial legacy" record_change "Initial legacy" # Delete the old function, keep the current one cat > src/legacy.ts << 'EOF' @@ -506,10 +518,10 @@ export function currentHelper(): void { } EOF -record_change "Remove deprecated function" >/dev/null 2>&1 - -HASH="$(get_last_change_hash)" -DIFF_OUT="$(atomic diff --no-color -c "$HASH" 2>&1)" +require_capture "record removal of deprecated function" record_for_diff "Remove deprecated function" +HASH="$COMMAND_OUTPUT" +require_capture "diff deprecated function removal" atomic diff --no-color -c "$HASH" +DIFF_OUT="$COMMAND_OUTPUT" # Should have only deletions, no additions # Exclude diff header lines (+++ b/file) from the count @@ -547,8 +559,8 @@ mkdir -p src done } > src/constants.ts -add_files src/constants.ts -record_change "Initial 20 constants" >/dev/null 2>&1 +require_success "add constants.ts" atomic add src/constants.ts +require_success "record initial constants" record_change "Initial 20 constants" # Modify even-numbered lines (10 modifications) and add 5 new lines # between each pair of constants (50 additions total) @@ -571,10 +583,10 @@ record_change "Initial 20 constants" >/dev/null 2>&1 done } > src/constants.ts -record_change "Update even constants, add comments" >/dev/null 2>&1 - -HASH="$(get_last_change_hash)" -DIFF_OUT="$(atomic diff --no-color -c "$HASH" 2>&1)" +require_capture "record updated constants" record_for_diff "Update even constants, add comments" +HASH="$COMMAND_OUTPUT" +require_capture "diff updated constants" atomic diff --no-color -c "$HASH" +DIFF_OUT="$COMMAND_OUTPUT" CHANGE_LINES="$(printf '%s\n' "$DIFF_OUT" | extract_atomic_change_lines)" # Check that each modified line is paired @@ -611,11 +623,11 @@ for i in 2 4 6 8 10 12 14 16 18 20; do fi done -if [[ "$PAIR_SUCCESS" -ge 8 ]]; then +if [[ "$PAIR_SUCCESS" -eq 10 ]]; then _pass "Scale: $PAIR_SUCCESS/10 modifications correctly paired" else _fail "Scale: pairing at scale" \ - "Only $PAIR_SUCCESS/10 modifications paired ($PAIR_FAIL failed). Expected ≥8." + "Only $PAIR_SUCCESS/10 modifications paired ($PAIR_FAIL failed). Expected 10." fi # Verify unchanged lines are NOT among the +/- change lines. They may appear @@ -672,8 +684,8 @@ export function handle(req: Request): void { } EOF -add_files src/handler.ts -record_change "Initial handler" >/dev/null 2>&1 +require_success "add handler.ts" atomic add src/handler.ts +require_success "record initial handler" record_change "Initial handler" # Complex change: # - ADD: import for Response type (pure add) @@ -711,10 +723,10 @@ export function handle(req: Request, res: Response): void { } EOF -record_change "Refactor handler with types and logging" >/dev/null 2>&1 - -HASH="$(get_last_change_hash)" -DIFF_OUT="$(atomic diff --no-color -c "$HASH" 2>&1)" +require_capture "record handler refactor" record_for_diff "Refactor handler with types and logging" +HASH="$COMMAND_OUTPUT" +require_capture "diff handler refactor" atomic diff --no-color -c "$HASH" +DIFF_OUT="$COMMAND_OUTPUT" # The validate function signature changed: boolean → ValidationResult assert_paired_diff \ diff --git a/tests/harness/helpers.sh b/tests/harness/helpers.sh index 0d4aaeb6..8ccc7ea0 100755 --- a/tests/harness/helpers.sh +++ b/tests/harness/helpers.sh @@ -34,6 +34,7 @@ TESTS_PASSED=0 TESTS_FAILED=0 TESTS_SKIPPED=0 FAIL_MESSAGES=() +_HARNESS_SUMMARY_PRINTED=0 # Exit code used when an entire suite cannot run because a prerequisite is # unavailable. The top-level runner recognizes 77 as SKIPPED, not PASSED. @@ -85,7 +86,19 @@ cleanup_tempdirs() { rm -rf "$d" 2>/dev/null || true done } -trap cleanup_tempdirs EXIT +_harness_exit() { + local status=$? + trap - EXIT + cleanup_tempdirs + if [[ $status -eq 0 && ${HARNESS_EXPECT_SUMMARY:-0} -eq 1 && $_HARNESS_SUMMARY_PRINTED -eq 0 ]]; then + status=1 + echo "${RED}Suite exited successfully without its assertion summary; remaining assertions were not verified.${RESET}" >&2 + elif [[ $status -ne 0 && $status -ne $HARNESS_SKIP_EXIT && $_HARNESS_SUMMARY_PRINTED -eq 0 ]]; then + echo "${RED}Suite ended before its summary (exit $status; $TESTS_RUN assertions completed). Remaining assertions were not run.${RESET}" >&2 + fi + exit "$status" +} +trap _harness_exit EXIT # Create a fresh temp directory and cd into it. # Usage: make_temp_repo [name] @@ -104,14 +117,21 @@ make_temp_repo() { # Initialise an atomic repository in the current directory. # Accepts optional arguments forwarded to `atomic init`. init_repo() { - atomic init "$@" >/dev/null 2>&1 + local out status + if out="$(atomic init "$@" 2>&1)"; then + return 0 + else + status=$? + echo "Repository initialization failed (exit $status): $out" >&2 + return "$status" + fi } # Create a file with given content. Parent dirs are created automatically. # Usage: create_file [content] create_file() { local path="$1" - local content="${2:-hello from $path}" + local content="${2-hello from $path}" mkdir -p "$(dirname "$path")" printf '%s' "$content" > "$path" } @@ -119,14 +139,14 @@ create_file() { # Append content to an existing file. append_file() { local path="$1" - local content="${2:-\nappended content}" + local content="${2-\nappended content}" printf '%s' "$content" >> "$path" } # Overwrite a file with new content. overwrite_file() { local path="$1" - local content="${2:-overwritten content}" + local content="${2-overwritten content}" printf '%s' "$content" > "$path" } @@ -140,12 +160,12 @@ create_dir() { # Run `atomic status` and capture stdout. get_status() { - atomic status 2>/dev/null || true + atomic status } # Run `atomic status --short` and capture stdout. get_status_short() { - atomic status --short 2>/dev/null || true + atomic status --short } # Add one or more paths. @@ -161,7 +181,7 @@ record_change() { # Unrecord the last change. unrecord_last() { - atomic unrecord 2>&1 || true + atomic unrecord 2>&1 } # Create a new view. @@ -186,7 +206,7 @@ insert_from_view() { # List views. list_views() { - atomic view list 2>/dev/null || true + atomic view list } # ── Assertions ────────────────────────────────────────────────────────────── @@ -237,18 +257,55 @@ skip_suite() { # Usage: assert_success "description" command args... assert_success() { local desc="$1"; shift - if "$@" >/dev/null 2>&1; then + local out status + if out="$("$@" 2>&1)"; then + _pass "$desc" + else + status=$? + _fail "$desc" "command failed (exit $status): $*${out:+ +$out}" + fi +} + +# A setup failure invalidates later assertions: report it and stop the suite. +require_success() { + local desc="$1"; shift + local out status + if out="$("$@" 2>&1)"; then _pass "$desc" else - _fail "$desc" "command failed: $*" + status=$? + _fail "$desc" "command failed (exit $status): $*${out:+ +$out}" + echo "${RED}Prerequisite failed; remaining assertions were not run.${RESET}" >&2 + print_summary || true + exit 1 + fi +} + +# Like require_success, retaining the successful output for assertions. +require_capture() { + local desc="$1"; shift + local status + if COMMAND_OUTPUT="$("$@" 2>&1)"; then + _pass "$desc" + else + status=$? + _fail "$desc" "command failed (exit $status): $*${COMMAND_OUTPUT:+ +$COMMAND_OUTPUT}" + echo "${RED}Prerequisite failed; remaining assertions were not run.${RESET}" >&2 + print_summary || true + exit 1 fi } # Assert that the last command failed (non-zero exit). assert_failure() { local desc="$1"; shift - if "$@" >/dev/null 2>&1; then - _fail "$desc" "expected failure but command succeeded: $*" + local out + if out="$("$@" 2>&1)"; then + _fail "$desc" "expected failure but command succeeded: $*${out:+ +$out}" else _pass "$desc" fi @@ -350,7 +407,10 @@ assert_status_contains() { local desc="$1" local needle="$2" local out - out="$(get_status)" + if ! out="$(get_status 2>&1)"; then + _fail "$desc" "status command failed: $out" + return + fi if echo "$out" | grep -qF "$needle"; then _pass "$desc" else @@ -363,7 +423,10 @@ assert_status_not_contains() { local desc="$1" local needle="$2" local out - out="$(get_status)" + if ! out="$(get_status 2>&1)"; then + _fail "$desc" "status command failed: $out" + return + fi if echo "$out" | grep -qF "$needle"; then _fail "$desc" "status should not contain '$needle' but does" else @@ -378,7 +441,10 @@ assert_status_flag() { local flag="$2" local path="$3" local out - out="$(get_status_short)" + if ! out="$(get_status_short 2>&1)"; then + _fail "$desc" "short status command failed: $out" + return + fi # Escape special regex characters in flag (e.g. ? → \?) local escaped_flag escaped_flag="$(printf '%s' "$flag" | sed 's/[][\\.^$*+?{}()|]/\\&/g')" @@ -398,7 +464,10 @@ assert_status_no_entry() { local desc="$1" local path="$2" local out - out="$(get_status_short)" + if ! out="$(get_status_short 2>&1)"; then + _fail "$desc" "short status command failed: $out" + return + fi if echo "$out" | grep -qF -- "$path"; then _fail "$desc" "'$path' should not appear in status but does. Status: $(echo "$out" | head -10)" else @@ -410,19 +479,12 @@ assert_status_no_entry() { assert_clean() { local desc="${1:-working copy is clean}" local out - out="$(get_status)" - # A clean status typically says "clean" or has no entries - if echo "$out" | grep -qiE "clean|no changes|nothing to record"; then + if ! out="$(get_status_short 2>&1)"; then + _fail "$desc" "short status command failed: $out" + elif [[ -z "$out" ]]; then _pass "$desc" else - # Also accept empty-ish output - local entries - entries="$(echo "$out" | grep -cE '^\s*[MADUC?]' || true)" - if [[ "$entries" -eq 0 ]]; then - _pass "$desc" - else - _fail "$desc" "working copy not clean. Status: $(echo "$out" | head -10)" - fi + _fail "$desc" "working copy not clean. Status: $out" fi } @@ -431,22 +493,14 @@ assert_current_view() { local desc="$1" local expected="$2" local out - out="$(list_views)" - # The current view is usually prefixed with * or highlighted - if echo "$out" | grep -qE "^\*?\s*${expected}(\s|$)"; then + if ! out="$(list_views 2>&1)"; then + _fail "$desc" "view list command failed: $out" + return + fi + if printf '%s\n' "$out" | awk -v expected="$expected" '{ for (i = 1; i < NF; i++) if ($i == "*" && $(i + 1) == expected) found = 1 } END { exit !found }'; then _pass "$desc" else - # Fallback: check current_view file - local view_file=".atomic/current_view" - if [[ -f "$view_file" ]]; then - local actual - actual="$(cat "$view_file")" - if [[ "$actual" == "$expected" ]]; then - _pass "$desc" - return - fi - fi - _fail "$desc" "expected current view '$expected'. Views: $(echo "$out" | head -5)" + _fail "$desc" "expected current view '$expected'. Views: $out" fi } @@ -455,8 +509,11 @@ assert_view_exists() { local desc="$1" local name="$2" local out - out="$(list_views)" - if echo "$out" | grep -qF "$name"; then + if ! out="$(atomic view list --all 2>&1)"; then + _fail "$desc" "view list command failed: $out" + return + fi + if printf '%s\n' "$out" | awk -v name="$name" '{ for (i = 1; i <= NF; i++) if ($i == name && (i == 1 || $(i - 1) == "-" || $(i - 1) == "*")) found = 1 } END { exit !found }'; then _pass "$desc" else _fail "$desc" "view '$name' not found. Views: $(echo "$out" | head -5)" @@ -468,7 +525,10 @@ assert_log_count() { local desc="$1" local expected="$2" local out - out="$(atomic log 2>/dev/null || true)" + if ! out="$(atomic log 2>&1)"; then + _fail "$desc" "log command failed: $out" + return + fi local count count="$(echo "$out" | grep -cE '^#|^[0-9a-zA-Z]{10,}' || true)" if [[ "$count" -eq "$expected" ]]; then @@ -649,6 +709,7 @@ begin_section() { # ── Summary ───────────────────────────────────────────────────────────────── print_summary() { + _HARNESS_SUMMARY_PRINTED=1 echo "" echo "${BOLD}═══════════════════════════════════════════════${RESET}" echo "${BOLD} Results: ${GREEN}$TESTS_PASSED passed${RESET}, " \ diff --git a/tests/harness/helpers_selftest.sh b/tests/harness/helpers_selftest.sh new file mode 100644 index 00000000..b3ab5a4d --- /dev/null +++ b/tests/harness/helpers_selftest.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# Verify the shared harness reports failed commands instead of passing them. +set -euo pipefail + +HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" +test_root="$(mktemp -d "${TMPDIR:-/tmp}/atomic-harness-helpers-XXXXXX")" +trap 'rm -rf "$test_root"' EXIT + +printf '%s\n' \ + '#!/usr/bin/env bash' \ + 'if [[ "${1:-}" == "view" && "${2:-}" == "list" ]]; then' \ + ' printf "dev [shared]\n - * feature [draft]\n"' \ + ' if [[ "${3:-}" == "--all" ]]; then printf " - empty [draft]\n"; fi' \ + ' exit 0' \ + 'fi' \ + 'echo "fixture command failed" >&2' \ + 'exit 42' > "$test_root/atomic" +chmod +x "$test_root/atomic" +export ATOMIC_BIN="$test_root/atomic" HARNESS_DIR test_root + +run_expect_failure() { + local description="$1" script="$2" needle="$3" forbidden="${4:-}" output + if output="$(bash -c "$script" 2>&1)"; then + echo "FAIL: $description unexpectedly succeeded" >&2 + exit 1 + fi + if [[ "$output" != *"$needle"* ]]; then + echo "FAIL: $description did not report '$needle': $output" >&2 + exit 1 + fi + if [[ -n "$forbidden" && "$output" == *"$forbidden"* ]]; then + echo "FAIL: $description continued after its failed prerequisite: $output" >&2 + exit 1 + fi + echo "PASS: $description" +} + +run_expect_failure "failed command includes diagnostic" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_success "record" atomic record; print_summary' \ + 'fixture command failed' +run_expect_failure "failed status is not clean" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_clean "clean"; print_summary' \ + 'short status command failed' +run_expect_failure "failed status is not an absent path" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_status_no_entry "absent" file; print_summary' \ + 'short status command failed' +run_expect_failure "non-current view is not current" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_current_view "current" dev; print_summary' \ + "expected current view 'dev'" +run_expect_failure "early exit is reported as incomplete" \ + 'source "$HARNESS_DIR/helpers.sh"; false' \ + 'Remaining assertions were not run' +run_expect_failure "successful early exit is not a pass" \ + 'export HARNESS_EXPECT_SUMMARY=1; source "$HARNESS_DIR/helpers.sh"; true' \ + 'without its assertion summary' +run_expect_failure "failed prerequisite stops the suite" \ + 'source "$HARNESS_DIR/helpers.sh"; require_success "record" atomic record; echo MUST_NOT_RUN' \ + 'fixture command failed' 'MUST_NOT_RUN' +run_expect_failure "failed output capture stops the suite" \ + 'source "$HARNESS_DIR/helpers.sh"; require_capture "record" atomic record; echo MUST_NOT_RUN' \ + 'fixture command failed' 'MUST_NOT_RUN' + +skip_status=0 +HARNESS_EXPECT_SUMMARY=1 bash -c 'source "$HARNESS_DIR/helpers.sh"; skip_suite "fixture"' >/dev/null 2>&1 || skip_status=$? +if [[ "$skip_status" -ne 77 ]]; then + echo "FAIL: skipped suite returned $skip_status instead of 77" >&2 + exit 1 +fi +echo "PASS: explicit suite skip stays skipped" + +bash -c 'source "$HARNESS_DIR/helpers.sh"; assert_current_view "current" feature; print_summary' >/dev/null +bash -c 'source "$HARNESS_DIR/helpers.sh"; assert_view_exists "empty draft" empty; print_summary' >/dev/null +run_expect_failure "view names are matched exactly" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_view_exists "missing" empt; print_summary' \ + "view 'empt' not found" +bash -c 'source "$HARNESS_DIR/helpers.sh"; cd "$test_root"; create_file empty ""; [[ -f empty && ! -s empty ]]; append_file empty ""; [[ ! -s empty ]]; overwrite_file empty "value"; overwrite_file empty ""; [[ ! -s empty ]]' >/dev/null +echo "PASS: current nested view and zero-byte file" diff --git a/tests/harness/run_all.sh b/tests/harness/run_all.sh index 5c0a04f5..18e4539a 100755 --- a/tests/harness/run_all.sh +++ b/tests/harness/run_all.sh @@ -133,7 +133,11 @@ for suite in "${SELECTED_SUITES[@]}"; do # Run the suite. Capture exit code but don't abort run_all. set +e - bash "$suite" + if grep -qE '^[[:space:]]*print_summary([[:space:]]|$)' "$suite"; then + HARNESS_EXPECT_SUMMARY=1 bash "$suite" + else + HARNESS_EXPECT_SUMMARY=0 bash "$suite" + fi suite_exit=$? set -e From e15a7d9d0ef8819764f83d75efc1eba5485cb9bc Mon Sep 17 00:00:00 2001 From: Lee Faus Date: Wed, 4 Nov 2026 10:07:31 -0500 Subject: [PATCH 03/14] feat: checkpoint span bridge and consolidated shim harness for manual testing Preserve the exact working tree developed and tested in atomic-span-clean, including experimental span import, projected semantics, durability fencing, merge fixes, and consolidated shim coverage. AI-assisted development checkpoint; not a production-readiness claim. Span import remains opt-in and strict shim guard failures remain documented. No remote publication. --- atomic-cli/Cargo.toml | 2 + atomic-cli/src/commands/blame.rs | 241 +- atomic-cli/src/commands/diff/format.rs | 6 + atomic-cli/src/commands/diff/helpers.rs | 113 +- atomic-cli/src/commands/diff/json.rs | 16 + atomic-cli/src/commands/diff/mod.rs | 4 +- atomic-cli/src/commands/diff/tests.rs | 19 + atomic-cli/src/commands/diff/types.rs | 13 +- atomic-cli/src/commands/git/import.rs | 979 +++++- atomic-cli/src/commands/git/parallel.rs | 69 +- atomic-cli/src/commands/git/shim.rs | 10 + atomic-cli/src/commands/git/span_compiler.rs | 266 ++ atomic-cli/src/commands/git/stream.rs | 2773 ++++++++++++++++- atomic-cli/src/commands/memory/new.rs | 2 +- atomic-cli/src/commands/rpc.rs | 183 +- atomic-cli/src/service/mod.rs | 153 + .../tests/it/git_shim_workspace_test.rs | 39 + atomic-cli/tests/it/git_span_blame_test.rs | 393 +++ atomic-cli/tests/it/main.rs | 2 + atomic-core/src/apply/file_ops.rs | 6 + atomic-core/src/change/ops.rs | 28 +- atomic-core/src/change/span_patch.rs | 5 + atomic-core/src/crdt/apply/trunk.rs | 4 + atomic-core/src/crdt/trunk.rs | 17 +- atomic-core/src/pristine/capability.rs | 11 + atomic-core/src/pristine/git_closure.rs | 490 +++ atomic-core/src/pristine/merge.rs | 14 +- atomic-core/src/pristine/mod.rs | 6 +- atomic-core/src/pristine/schema.rs | 239 +- .../src/pristine/span_patch_storage.rs | 1269 +++++++- atomic-core/src/pristine/tables.rs | 9 +- atomic-core/src/pristine/txn/pristine.rs | 6 + atomic-core/src/pristine/txn/read.rs | 36 +- .../src/pristine/txn/write/capability.rs | 29 + atomic-core/src/pristine/txn/write/tag.rs | 80 +- atomic-core/src/record/workflow/record/mod.rs | 54 +- .../src/record/workflow/record/tests.rs | 36 + atomic-core/tests/merge_test.rs | 159 +- atomic-repository/src/apply/mod.rs | 25 + atomic-repository/src/diff_engine.rs | 61 +- atomic-repository/src/history/operations.rs | 10 + .../src/redb_change_store/tests.rs | 69 + atomic-repository/src/repository/changes.rs | 242 +- atomic-repository/src/repository/content.rs | 56 +- .../src/repository/git_stream.rs | 1482 ++++++++- atomic-repository/src/repository/insert.rs | 253 +- atomic-repository/src/repository/locks.rs | 134 + atomic-repository/src/repository/mod.rs | 2 +- atomic-repository/src/repository/operation.rs | 55 +- .../src/repository/project_tree.rs | 48 +- atomic-repository/src/repository/query.rs | 41 +- atomic-repository/src/repository/record.rs | 551 +++- .../src/repository/remote_cache.rs | 20 + atomic-repository/src/repository/sandbox.rs | 29 +- .../src/repository/semantic_closure.rs | 6 + .../src/repository/semantic_materialize.rs | 9 + atomic-repository/src/repository/synthesis.rs | 179 +- .../tests/it/cb_n6_merge_rubric_test.rs | 64 + atomic-repository/tests/native_span_record.rs | 389 ++- docs/git-span-import-handoff.md | 311 ++ .../repository_mutation_messages.proto | 9 + .../services/vault/vault_messages.proto | 2 + libatomic/src/daemon/services.rs | 54 +- libatomic/src/daemon/services_agent.rs | 91 +- .../daemon/services_maintenance/compact.rs | 37 +- .../services_maintenance/compact/tests.rs | 44 + libatomic/tests/local_service_test.rs | 11 +- libatomic/tests/record_service_test.rs | 90 + libatomic/tests/vault_local_test.rs | 130 + tests/harness/10_git_import.sh | 152 +- tests/harness/11_diff_git_parity.sh | 209 +- tests/harness/12_full_repo_diff_parity.sh | 213 +- tests/harness/13_import_fidelity.sh | 73 +- tests/harness/14_vault_kg.sh | 59 +- tests/harness/16_vault_pull_bootstrap.sh | 167 +- tests/harness/17_cross_view_merge.sh | 348 ++- tests/harness/18_git_import_hot_file.sh | 50 +- tests/harness/19_unrecord.sh | 274 +- tests/harness/20_sandbox.sh | 68 +- tests/harness/21_status_add_asymmetry.sh | 322 +- tests/harness/22_switch_conflict_markers.sh | 86 +- tests/harness/23_shared_view_parent_chain.sh | 89 +- .../harness/24_concurrent_insert_conflict.sh | 50 +- tests/harness/25_agent_provenance.sh | 134 +- tests/harness/26_vault_intent_inherit.sh | 90 +- .../harness/27_merge_ordering_duplication.sh | 6 +- tests/harness/28_merge_rubric.sh | 26 +- tests/harness/29_rename.sh | 80 +- tests/harness/30_record_conflict_refusal.sh | 80 +- tests/harness/31_init_lazy_untracked.sh | 42 +- tests/harness/31_record_oversized_file.sh | 38 +- tests/harness/32_review_gate_insert.sh | 87 +- tests/harness/35_git_shim_serialization.sh | 37 + tests/harness/39_ambient_inode_views.sh | 74 +- tests/harness/39_git_shim_workflow.sh | 107 + tests/harness/40_git_shim_safety.sh | 89 + tests/harness/40_switch_transactionality.sh | 66 +- tests/harness/41_git_shim_hooks.sh | 51 +- tests/harness/41_view_switch_name_conflict.sh | 46 +- tests/harness/42_git_shim_guard.sh | 53 +- tests/harness/42_view_create_parent.sh | 26 +- .../44_view_split_delete_confirmation.sh | 16 +- tests/harness/helpers.sh | 61 +- tests/harness/helpers_selftest.sh | 53 + .../{ => legacy-shadow}/19_git_shadow.sh | 258 +- .../33_shadow_push_conflict_markers.sh | 47 +- .../34_shadow_provenance_guard.sh | 61 +- .../35_shadow_concurrent_push.sh | 57 +- .../36_shadow_coherence_v2.sh | 37 +- .../37_shadow_view_branch_follow.sh | 59 +- .../{ => legacy-shadow}/38_shadow_drift_v3.sh | 64 +- .../{ => legacy-shadow}/39_git_shim_mvp.sh | 26 +- .../40_git_shim_materialize_safety.sh | 26 +- tests/harness/legacy-shadow/README.md | 10 + tests/harness/merge_helpers.sh | 123 +- tests/harness/run_all.sh | 67 +- tests/harness/shim.md | 102 + tests/harness/shim_harness_selftest.sh | 24 + tests/harness/shim_helpers.sh | 202 ++ 119 files changed, 14772 insertions(+), 2098 deletions(-) create mode 100644 atomic-cli/src/commands/git/span_compiler.rs create mode 100644 atomic-cli/tests/it/git_span_blame_test.rs create mode 100644 atomic-core/src/pristine/git_closure.rs create mode 100644 docs/git-span-import-handoff.md create mode 100755 tests/harness/35_git_shim_serialization.sh create mode 100755 tests/harness/39_git_shim_workflow.sh create mode 100755 tests/harness/40_git_shim_safety.sh rename tests/harness/{ => legacy-shadow}/19_git_shadow.sh (69%) mode change 100755 => 100644 rename tests/harness/{ => legacy-shadow}/33_shadow_push_conflict_markers.sh (72%) mode change 100755 => 100644 rename tests/harness/{ => legacy-shadow}/34_shadow_provenance_guard.sh (63%) mode change 100755 => 100644 rename tests/harness/{ => legacy-shadow}/35_shadow_concurrent_push.sh (54%) mode change 100755 => 100644 rename tests/harness/{ => legacy-shadow}/36_shadow_coherence_v2.sh (62%) mode change 100755 => 100644 rename tests/harness/{ => legacy-shadow}/37_shadow_view_branch_follow.sh (63%) mode change 100755 => 100644 rename tests/harness/{ => legacy-shadow}/38_shadow_drift_v3.sh (55%) mode change 100755 => 100644 rename tests/harness/{ => legacy-shadow}/39_git_shim_mvp.sh (96%) mode change 100755 => 100644 rename tests/harness/{ => legacy-shadow}/40_git_shim_materialize_safety.sh (87%) mode change 100755 => 100644 create mode 100644 tests/harness/legacy-shadow/README.md create mode 100644 tests/harness/shim.md create mode 100755 tests/harness/shim_harness_selftest.sh create mode 100644 tests/harness/shim_helpers.sh diff --git a/atomic-cli/Cargo.toml b/atomic-cli/Cargo.toml index 4d923839..fdea9c41 100644 --- a/atomic-cli/Cargo.toml +++ b/atomic-cli/Cargo.toml @@ -6,6 +6,8 @@ teams = ["atomic-teams"] # test suite builds the binary with the deterministic adoption seams. The # default/shipping configuration compiles no injection code at all. adoption-test-injection = ["atomic-repository/adoption-test-injection"] +# Non-default Git span-import experiment; no native semantic history is invented. +experimental-span-import = [] [package] name = "atomic-cli" diff --git a/atomic-cli/src/commands/blame.rs b/atomic-cli/src/commands/blame.rs index 0a984ef6..80d292d8 100644 --- a/atomic-cli/src/commands/blame.rs +++ b/atomic-cli/src/commands/blame.rs @@ -39,7 +39,7 @@ pub struct Blame { struct LineAttribution { line_number: usize, - owner: Hash, + owners: Vec, content: Vec, } @@ -67,73 +67,169 @@ impl Command for Blame { .ok_or_else(|| CliError::InvalidRepository { reason: format!("view '{view}' not found"), })?; - let _ = view_state; + let mut changes: HashMap = HashMap::new(); + let mut lines: Vec = Vec::new(); + let mut skipped_unbound = 0usize; + + // Span histories imported from Git deliberately have no historical + // CRDT rows. Interpret their verified visible bytes directly instead + // of attributing them to a later native edit. A line can contain bytes + // from several changes, in which case every owner remains visible. + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + let span_enabled = txn + .required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .map_err(|e| CliError::Repository(e.into()))? + .is_some(); + let mut span_handled = false; + if span_enabled { + use atomic_core::pristine::span_patch_storage::{SpanPatchReadTxn, SpanRenderContext}; + if self.path.as_bytes().contains(&b'%') { + return Err(CliError::InvalidArgument { + message: "span blame cannot safely resolve percent-bearing paths yet".into(), + }); + } + let visibility = atomic_repository::graph_visibility_closure(&txn, &view_state) + .map_err(CliError::Repository)?; + let binding = repo + .projected_path_binding_for_visibility(&txn, &visibility, &self.path) + .map_err(CliError::Repository)? + .ok_or_else(|| CliError::InvalidArgument { + message: format!("'{}' is absent or conflicted on view '{}'", self.path, view), + })?; + if !binding.directory + && txn + .span_root(binding.inode) + .map_err(|error| CliError::InvalidArgument { + message: error.to_string(), + })? + .is_some() + { + let visible: std::collections::HashSet<_> = + visibility.iter_dependency_first().copied().collect(); + let attrs = + atomic_core::output::project_inode_attributes(&txn, binding.position, &visible) + .map_err(|error| CliError::Repository(error.into()))?; + if attrs.is_conflicted() + || attrs.materialization.kind != atomic_core::change::InodeKind::Regular + { + return Err(CliError::InvalidArgument { + message: format!("'{}' is not an unambiguous regular text file", self.path), + }); + } + let context = SpanRenderContext::new(&txn, repo.change_store(), &visibility) + .map_err(|error| CliError::InvalidArgument { + message: error.to_string(), + })?; + let state = context + .record_state(binding.inode) + .map_err(|error| CliError::InvalidArgument { + message: error.to_string(), + })? + .ok_or_else(|| CliError::InvalidArgument { + message: format!("'{}' has no visible span state", self.path), + })?; + let semantic = + state + .semantic_snapshot() + .map_err(|error| CliError::InvalidArgument { + message: error.to_string(), + })?; + for (index, line) in semantic.lines.into_iter().enumerate() { + let mut owners = Vec::new(); + for token in line.tokens { + for fragment in token.authors { + let owner = fragment.target.owner; + if !owners.contains(&owner) { + owners.push(owner); + } + if let std::collections::hash_map::Entry::Vacant(slot) = + changes.entry(owner) + { + slot.insert( + repo.load_change(&owner).map_err(CliError::Repository)?, + ); + } + } + } + if owners.is_empty() { + return Err(CliError::InvalidArgument { + message: format!("'{}' has an unattributed visible line", self.path), + }); + } + lines.push(LineAttribution { + line_number: index + 1, + owners, + content: state.content[line.output].to_vec(), + }); + } + span_handled = true; + } + } // The trunk must exist and hold graph content. Binary/opaque trunks // are attributed at line granularity only when the CRDT layer bound // branches; otherwise the command fails explicitly rather than // printing fabricated lines. - let trunk = txn - .get_trunk_by_path(&self.path) - .map_err(|e| CliError::Repository(e.into()))? - .ok_or_else(|| CliError::InvalidArgument { - message: format!( - "'{path}' is not a tracked text file (no semantic trunk)", - path = self.path - ), - })?; + if !span_handled { + let trunk = txn + .get_trunk_by_path(&self.path) + .map_err(|e| CliError::Repository(e.into()))? + .ok_or_else(|| CliError::InvalidArgument { + message: format!( + "'{path}' is not a tracked text file (no semantic trunk)", + path = self.path + ), + })?; - let branch_ids: Vec = - iter_trunk_branches_in_file_order(&txn, trunk) - .map_err(|e| CliError::Repository(e.into()))?; - let branch_keys: Vec<[u8; 12]> = branch_ids - .iter() - .map(atomic_core::crdt::tables::encode_branch_id) - .collect(); + let branch_ids: Vec = + iter_trunk_branches_in_file_order(&txn, trunk) + .map_err(|e| CliError::Repository(e.into()))?; + let branch_keys: Vec<[u8; 12]> = branch_ids + .iter() + .map(atomic_core::crdt::tables::encode_branch_id) + .collect(); - let mut changes: HashMap = HashMap::new(); - let mut lines: Vec = Vec::new(); - let mut skipped_unbound = 0usize; - for branch_key in branch_keys { - let branch = txn - .get_crdt_branch(&branch_key) - .map_err(|e| CliError::Repository(e.into()))?; - let Some(branch) = branch else { continue }; - if !branch.state.is_alive() { - continue; + for branch_key in branch_keys { + let branch = txn + .get_crdt_branch(&branch_key) + .map_err(|e| CliError::Repository(e.into()))?; + let Some(branch) = branch else { continue }; + if !branch.state.is_alive() { + continue; + } + let Some(vertex) = txn + .get_crdt_branch_vertex(&branch_key) + .map_err(|e| CliError::Repository(e.into()))? + else { + skipped_unbound += 1; + continue; + }; + let Some(owner) = txn + .get_external(vertex.change) + .map_err(|e| CliError::Repository(e.into()))? + else { + skipped_unbound += 1; + continue; + }; + let change = match changes.get(&owner) { + Some(change) => change.clone(), + None => repo.load_change(&owner).map_err(CliError::Repository)?, + }; + let start = usize::try_from(vertex.start.get()).unwrap_or(0); + let end = usize::try_from(vertex.end.get()).unwrap_or(0); + let content: Vec = if start <= end && end <= change.contents.len() { + change.contents[start..end].to_vec() + } else { + Vec::new() + }; + changes.insert(owner, change); + let decoded = decode_branch_id(&branch_key); + lines.push(LineAttribution { + line_number: decoded.branch_idx() as usize + 1, + owners: vec![owner], + content, + }); } - let Some(vertex) = txn - .get_crdt_branch_vertex(&branch_key) - .map_err(|e| CliError::Repository(e.into()))? - else { - skipped_unbound += 1; - continue; - }; - let Some(owner) = txn - .get_external(vertex.change) - .map_err(|e| CliError::Repository(e.into()))? - else { - skipped_unbound += 1; - continue; - }; - let change = match changes.get(&owner) { - Some(change) => change.clone(), - None => repo.load_change(&owner).map_err(CliError::Repository)?, - }; - let start = usize::try_from(vertex.start.get()).unwrap_or(0); - let end = usize::try_from(vertex.end.get()).unwrap_or(0); - let content: Vec = if start <= end && end <= change.contents.len() { - change.contents[start..end].to_vec() - } else { - Vec::new() - }; - changes.insert(owner, change); - let decoded = decode_branch_id(&branch_key); - lines.push(LineAttribution { - line_number: decoded.branch_idx() as usize + 1, - owner, - content, - }); } if lines.is_empty() { @@ -151,12 +247,22 @@ impl Command for Blame { let mut stdout = std::io::stdout().lock(); for attribution in &lines { - let hash = if self.short { - attribution.owner.to_base32()[..12].to_string() - } else { - attribution.owner.to_base32() - }; - let change = changes.get(&attribution.owner); + let hash = attribution + .owners + .iter() + .map(|owner| { + let hash = owner.to_base32(); + if self.short { + hash[..12].to_string() + } else { + hash + } + }) + .collect::>() + .join("+"); + let change = (attribution.owners.len() == 1) + .then(|| changes.get(&attribution.owners[0])) + .flatten(); let (author, date) = match change { Some(change) => { let author = change @@ -174,6 +280,9 @@ impl Command for Blame { .to_string(); (author, date) } + None if attribution.owners.len() > 1 => { + ("multiple".to_string(), "mixed".to_string()) + } None => ("unknown".to_string(), "unknown".to_string()), }; let content = String::from_utf8_lossy(&attribution.content); diff --git a/atomic-cli/src/commands/diff/format.rs b/atomic-cli/src/commands/diff/format.rs index 142c1338..1869dfa0 100644 --- a/atomic-cli/src/commands/diff/format.rs +++ b/atomic-cli/src/commands/diff/format.rs @@ -91,6 +91,12 @@ impl Diff { println!("+++ {}", new_path); } + if file_diff.git_rename_identity_unproven { + println!( + "Git-detected rename; Atomic recorded delete+add (inode identity unproven)" + ); + } + // Handle binary files if file_diff.is_binary { println!("Binary files differ"); diff --git a/atomic-cli/src/commands/diff/helpers.rs b/atomic-cli/src/commands/diff/helpers.rs index fa9d08f8..52320704 100644 --- a/atomic-cli/src/commands/diff/helpers.rs +++ b/atomic-cli/src/commands/diff/helpers.rs @@ -13,6 +13,10 @@ use serde::Deserialize; struct GitMetadataDiffFile { path: String, lines: Vec, + #[serde(default)] + old_path: Option, + #[serde(default)] + operation: Option, } /// A changed line paired with the insert/delete offset preceding it. @@ -563,6 +567,30 @@ impl Diff { } if hunk_lines.is_empty() { + if entry.operation.as_deref() == Some("renamed") { + if let Some(old_path) = &entry.old_path { + let unproven = change + .unhashed + .as_ref() + .and_then(|value| value.pointer("/git/stream/rename_identity_loss")) + .and_then(|value| value.as_array()) + .is_some_and(|losses| { + losses.iter().any(|loss| { + loss.get("from").and_then(|v| v.as_str()) + == Some(old_path.as_str()) + && loss.get("to").and_then(|v| v.as_str()) + == Some(entry.path.as_str()) + }) + }); + let mut file_diff = FileDiff::new(&entry.path, FileChangeStatus::Renamed); + file_diff.old_path = old_path.clone(); + file_diff.stats.path = entry.path.clone(); + file_diff.stats.status = 'R'; + file_diff.git_rename_identity_unproven = unproven; + stats.add_file(file_diff.stats.clone()); + file_diffs.push(file_diff); + } + } continue; } @@ -923,15 +951,82 @@ pub(crate) fn change_file_diffs( change_hash: &Hash, config: &DiffOutputConfig, ) -> CliResult<(Vec, DiffStats)> { + projected_change_file_diffs_with( + change, + config, + |path| { + repo.get_file_content_before_change(path, change_hash) + .map_err(CliError::Repository) + }, + |path| { + repo.get_file_content_after_change(path, change_hash) + .map_err(CliError::Repository) + }, + ) +} + +pub(crate) fn projected_change_file_diffs_with( + change: &Change, + config: &DiffOutputConfig, + before_content: B, + after_content: A, +) -> CliResult<(Vec, DiffStats)> +where + B: Fn(&str) -> CliResult>>, + A: Fn(&str) -> CliResult>>, +{ + let projected: Vec<_> = change + .file_ops() + .iter() + .filter(|ops| matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. }))) + .collect(); + if !projected.is_empty() { + let mut ordinary = change.clone(); + ordinary + .hashed + .file_ops + .retain(|ops| !matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. }))); + let (mut files, mut stats) = change_file_diffs_with(&ordinary, config, |path| { + before_content(path).ok().flatten() + })?; + for ops in projected { + let path = ops.path(); + let before = before_content(path)?.ok_or_else(|| CliError::InvalidArgument { + message: format!("projected span file '{path}' is absent before the change"), + })?; + let after = after_content(path)?.ok_or_else(|| CliError::InvalidArgument { + message: format!("projected span file '{path}' is absent after the change"), + })?; + let mut file_diff = FileDiff::modified(path); + let binary = before.contains(&0) + || after.contains(&0) + || std::str::from_utf8(&before).is_err() + || std::str::from_utf8(&after).is_err(); + if binary { + file_diff.is_binary = true; + } else { + let result = diff_text(&before, &after, Algorithm::Myers); + if !result.is_unchanged() { + let old_lines: Vec<_> = before.split(|&byte| byte == b'\n').collect(); + let new_lines: Vec<_> = after.split(|&byte| byte == b'\n').collect(); + for hunk in + build_hunks_from_diff(&result, &old_lines, &new_lines, config.context_lines) + { + file_diff.add_hunk(hunk); + } + } + } + file_diff.compute_stats(); + stats.add_file(file_diff.stats.clone()); + files.push(file_diff); + } + return Ok((files, stats)); + } // The before-content fetch: the file's recorded state prior to the // change, read from the graph. The wire path has no such fetch — it // passes a `None` resolver and the hunks render zero-context, the // same degradation this path applies when the read fails. - change_file_diffs_with(change, config, |path| { - repo.get_file_content_before_change(path, change_hash) - .ok() - .flatten() - }) + change_file_diffs_with(change, config, |path| before_content(path).ok().flatten()) } /// Build the legacy (no-file_ops) change's file diffs from per-file @@ -1051,6 +1146,11 @@ where let trunk_op = ops.trunk_op(); let line_ops = ops.line_ops(); + if matches!(trunk_op, Some(TrunkOp::ProjectedSpanEdit { .. })) { + return Err(CliError::InvalidArgument { + message: "projected span edit requires a span-aware semantic diff".into(), + }); + } // Determine file change status from trunk operation let change_status = match trunk_op { @@ -1059,7 +1159,8 @@ where Some(TrunkOp::Move { .. }) => FileChangeStatus::Renamed, Some(TrunkOp::Undelete { .. }) | Some(TrunkOp::SetMode { .. }) - | Some(TrunkOp::SetKind { .. }) => FileChangeStatus::Modified, + | Some(TrunkOp::SetKind { .. }) + | Some(TrunkOp::ProjectedSpanEdit { .. }) => FileChangeStatus::Modified, None => FileChangeStatus::Modified, }; diff --git a/atomic-cli/src/commands/diff/json.rs b/atomic-cli/src/commands/diff/json.rs index b27551d9..0d466f43 100644 --- a/atomic-cli/src/commands/diff/json.rs +++ b/atomic-cli/src/commands/diff/json.rs @@ -123,6 +123,8 @@ struct JsonDiffFile { code: char, binary: bool, + #[serde(skip_serializing_if = "std::ops::Not::not")] + git_rename_identity_unproven: bool, insertions: usize, deletions: usize, hunks: Vec, @@ -137,6 +139,7 @@ impl From<&FileDiff> for JsonDiffFile { status: diff.status.description(), code: diff.status.status_char(), binary: diff.is_binary, + git_rename_identity_unproven: diff.git_rename_identity_unproven, insertions: diff.stats.insertions, deletions: diff.stats.deletions, hunks: diff.hunks.iter().map(JsonDiffHunk::from).collect(), @@ -327,6 +330,19 @@ mod tests { assert_eq!(file["code"], "A"); } + #[test] + fn git_rename_json_marks_unproven_identity() { + let mut diff = FileDiff::new("new.txt", FileChangeStatus::Renamed); + diff.old_path = "old.txt".into(); + diff.git_rename_identity_unproven = true; + let diffs = vec![diff]; + let document = JsonDiff::new(&diffs, &sample_stats(&diffs), None, None); + let value = serde_json::to_value(document).unwrap(); + assert_eq!(value["files"][0]["old_path"], "old.txt"); + assert_eq!(value["files"][0]["new_path"], "new.txt"); + assert_eq!(value["files"][0]["git_rename_identity_unproven"], true); + } + #[test] fn deleted_file_reports_dev_null_as_new_path() { let mut diff = FileDiff::deleted("src/old.rs"); diff --git a/atomic-cli/src/commands/diff/mod.rs b/atomic-cli/src/commands/diff/mod.rs index fe45920d..413e04a2 100644 --- a/atomic-cli/src/commands/diff/mod.rs +++ b/atomic-cli/src/commands/diff/mod.rs @@ -243,7 +243,9 @@ pub(crate) mod types; const RECENT_CHANGE_SUGGESTIONS: usize = 3; pub use command::*; -pub(crate) use helpers::{change_file_diffs, change_file_diffs_with, legacy_content_file_diffs}; +pub(crate) use helpers::{ + change_file_diffs, legacy_content_file_diffs, projected_change_file_diffs_with, +}; pub(crate) use output::{ build_hunks_from_diff, format_stat_graph, print_semantic_word_diff_line, print_word_diff_line, }; diff --git a/atomic-cli/src/commands/diff/tests.rs b/atomic-cli/src/commands/diff/tests.rs index 4340a806..0e43b7e3 100644 --- a/atomic-cli/src/commands/diff/tests.rs +++ b/atomic-cli/src/commands/diff/tests.rs @@ -898,6 +898,25 @@ mod tests { assert_eq!(stats.total_insertions(), 1); } + #[test] + fn git_stream_pure_rename_is_evidence_not_an_authoritative_move() { + use atomic_core::change::ChangeHeader; + + let mut change = Change::empty(ChangeHeader::new("Git rename evidence")); + change.unhashed = Some(serde_json::json!({"git": { + "diff_lines": [{"path": "new.txt", "old_path": "old.txt", "operation": "renamed", "lines": []}], + "stream": {"rename_identity_loss": [{"from": "old.txt", "to": "new.txt"}]} + }})); + let (files, _) = Diff::build_git_import_file_diffs(&change).unwrap(); + assert_eq!(files.len(), 1); + assert_eq!(files[0].old_path, "old.txt"); + assert_eq!(files[0].new_path, "new.txt"); + assert_eq!(files[0].status, FileChangeStatus::Renamed); + assert!(files[0].hunks.is_empty()); + assert!(files[0].has_changes()); + assert!(files[0].git_rename_identity_unproven); + } + #[test] fn test_diff_with_algorithm() { let diff = Diff::new().with_algorithm("patience"); diff --git a/atomic-cli/src/commands/diff/types.rs b/atomic-cli/src/commands/diff/types.rs index b676c765..ee96b852 100644 --- a/atomic-cli/src/commands/diff/types.rs +++ b/atomic-cli/src/commands/diff/types.rs @@ -528,12 +528,19 @@ pub struct FileDiff { /// Whether this is a binary file. pub is_binary: bool, + + /// Git inferred this rename; the imported Atomic graph retained separate + /// delete/add identities rather than asserting a causal file move. + pub git_rename_identity_unproven: bool, } impl FileDiff { /// Check if this file diff has any changes. pub fn has_changes(&self) -> bool { - !self.hunks.is_empty() + matches!( + self.status, + FileChangeStatus::Renamed | FileChangeStatus::Copied + ) || !self.hunks.is_empty() || self.stats.insertions > 0 || self.stats.deletions > 0 || self.is_binary @@ -554,6 +561,7 @@ impl FileDiff { hunks: Vec::new(), stats: FileDiffStats::default(), is_binary: false, + git_rename_identity_unproven: false, } } @@ -571,6 +579,7 @@ impl FileDiff { ..Default::default() }, is_binary: false, + git_rename_identity_unproven: false, } } @@ -588,6 +597,7 @@ impl FileDiff { ..Default::default() }, is_binary: false, + git_rename_identity_unproven: false, } } @@ -605,6 +615,7 @@ impl FileDiff { ..Default::default() }, is_binary: false, + git_rename_identity_unproven: false, } } diff --git a/atomic-cli/src/commands/git/import.rs b/atomic-cli/src/commands/git/import.rs index c44fedcf..0ed4c7b8 100644 --- a/atomic-cli/src/commands/git/import.rs +++ b/atomic-cli/src/commands/git/import.rs @@ -113,6 +113,16 @@ pub struct Import { #[arg(long = "with-crdt")] pub with_crdt: bool, + /// Experimentally use compact span-backed history for a fresh first-parent stream. + /// + /// Historical semantic FileOps are not synthesized. Native content edits + /// use experimental span-derived semantics; unsupported operations refuse. + /// Incremental import requires unchanged first-parent span history and a + /// clean checked-out Git tip. + #[cfg(feature = "experimental-span-import")] + #[arg(long, conflicts_with_all = ["no_stream", "with_crdt", "all_branches"])] + pub span: bool, + /// Use the rolling first-parent graph stream (default for fresh imports). /// Verifies the final tip, not every historical state. Unsupported input /// refuses; use --no-stream for the ordinary importer. Without --stream, @@ -263,9 +273,21 @@ fn emit_import_synthesis(repo: &Repository, stats: &ImportStats) { } impl Import { + fn uses_span(&self) -> bool { + #[cfg(feature = "experimental-span-import")] + { + self.span + } + #[cfg(not(feature = "experimental-span-import"))] + { + false + } + } + fn uses_stream(&self, repository_exists: bool) -> bool { !self.no_stream && (self.stream + || self.uses_span() || (!repository_exists && !self.with_crdt && !self.incremental @@ -301,6 +323,7 @@ impl Import { repo_name, mainline_only: mode.mainline_only, graph_only: !self.with_crdt, + span_stream: self.uses_span(), preserve_working_copy: mode.preserve_working_copy, target_view: branch_name.to_string(), known_states: known_states.clone(), @@ -777,10 +800,253 @@ impl Command for Import { } } +#[cfg(feature = "experimental-span-import")] +#[derive(Debug, PartialEq, Eq)] +struct SpanIncrementalGitObservation { + branch: String, + tip: git2::Oid, + index: Vec, +} + +#[cfg(all(test, unix, feature = "experimental-span-import"))] +fn pause_span_incremental_before_checkpoint_for_test() { + use std::io::Write; + if std::env::var_os("ATOMIC_TEST_SPAN_INCREMENTAL_CHECKPOINT_PAUSE").is_none() { + return; + } + println!("ATOMIC_SPAN_INCREMENTAL_BEFORE_CHECKPOINT"); + std::io::stdout().flush().unwrap(); + loop { + std::thread::park(); + } +} + +#[cfg(all(test, unix, feature = "experimental-span-import"))] +fn pause_span_incremental_final_git_check_for_test() { + use std::io::{Read, Write}; + if std::env::var_os("ATOMIC_TEST_SPAN_INCREMENTAL_GIT_PAUSE").is_none() { + return; + } + println!("ATOMIC_SPAN_INCREMENTAL_FINAL_GIT_CHECK"); + std::io::stdout().flush().unwrap(); + let mut signal = [0u8; 1]; + std::io::stdin().read_exact(&mut signal).unwrap(); +} + +#[cfg(feature = "experimental-span-import")] +impl SpanIncrementalGitObservation { + fn capture(git: &GitRepository) -> CliResult { + // libgit2's repository index and a Git process using GIT_INDEX_FILE + // need not observe the same index. Until the override is explicitly + // supported, do not claim a clean checkout based on the wrong file. + if std::env::var_os("GIT_INDEX_FILE").is_some() { + return Err(CliError::GitError { + message: + "span incremental import does not support GIT_INDEX_FILE; no import was started" + .into(), + }); + } + if git.state() != git2::RepositoryState::Clean { + return Err(CliError::GitError { + message: "Git has an in-progress operation; span incremental import was refused" + .into(), + }); + } + if git.path().join("index.lock").exists() { + return Err(CliError::GitError { + message: "Git index is locked; span incremental import was refused".into(), + }); + } + let head = git.head().map_err(|error| CliError::GitError { + message: format!("cannot observe Git HEAD: {error}"), + })?; + if !head.is_branch() { + return Err(CliError::GitError { + message: "span incremental import requires an attached Git branch".into(), + }); + } + let branch = head + .shorthand() + .ok_or_else(|| CliError::GitError { + message: "Git branch name is not representable".into(), + })? + .to_string(); + let tip = head.target().ok_or_else(|| CliError::GitError { + message: "Git branch has no commit".into(), + })?; + let index = + std::fs::read(git.path().join("index")).map_err(|error| CliError::GitError { + message: format!("cannot inspect Git index: {error}"), + })?; + let mut parsed_index = git.index().map_err(|error| CliError::GitError { + message: format!("cannot inspect Git index entries: {error}"), + })?; + parsed_index + .read(true) + .map_err(|error| CliError::GitError { + message: format!("cannot refresh Git index observation: {error}"), + })?; + if parsed_index.iter().any(|entry| { + // These flags tell Git status not to compare a tracked file with + // the worktree, so clean status cannot establish its bytes. + entry.flags & 0x8000 != 0 || entry.flags_extended & 0x4000 != 0 + }) { + return Err(CliError::GitError { + message: "span incremental import cannot verify Git index entries marked assume-unchanged or skip-worktree".into(), + }); + } + let mut options = git2::StatusOptions::new(); + options + .include_untracked(false) + .include_ignored(false) + .exclude_submodules(false); + let statuses = git + .statuses(Some(&mut options)) + .map_err(|error| CliError::GitError { + message: format!("cannot inspect Git working tree: {error}"), + })?; + if statuses.iter().any(|entry| !entry.status().is_empty()) { + return Err(CliError::GitError { + message: + "span incremental import requires a clean Git index and tracked working tree" + .into(), + }); + } + // Status and index reads are separate operations. Refuse a snapshot + // assembled from different HEAD/index generations rather than pinning + // an internally inconsistent observation. + let after_head = git.head().map_err(|error| CliError::GitError { + message: format!("cannot reobserve Git HEAD: {error}"), + })?; + let after_index = + std::fs::read(git.path().join("index")).map_err(|error| CliError::GitError { + message: format!("cannot reobserve Git index: {error}"), + })?; + if !after_head.is_branch() + || after_head.shorthand() != Some(branch.as_str()) + || after_head.target() != Some(tip) + || after_index != index + || git.path().join("index.lock").exists() + || git.state() != git2::RepositoryState::Clean + { + return Err(CliError::GitError { + message: "Git HEAD or index changed while observing span incremental import".into(), + }); + } + Ok(Self { branch, tip, index }) + } +} + impl Import { + #[cfg(feature = "experimental-span-import")] + fn run_span_incremental(&self, git: &GitRepository, workdir: &Path) -> CliResult<()> { + if self.dry_run { + return Err(CliError::GitError { + message: "span incremental dry-run is not supported; no import was started".into(), + }); + } + if self.all_branches + || self.with_crdt + || self.no_stream + || self.detached_tip.is_some() + || self.reactive_budget.is_some() + { + return Err(CliError::GitError { + message: "span incremental import requires a checked-out first-parent branch" + .into(), + }); + } + let _preflight = Repository::try_lock_git_import_for_span(workdir, false) + .map_err(|error| CliError::GitError { + message: format!("span import preflight refused: {error}"), + })? + .ok_or_else(|| CliError::GitError { + message: "span incremental import requires an existing span repository".into(), + })?; + let pinned = SpanIncrementalGitObservation::capture(git)?; + if self + .branch + .as_ref() + .is_some_and(|branch| branch != &pinned.branch) + { + return Err(CliError::GitError { + message: "span incremental import only supports the checked-out Git branch".into(), + }); + } + let mut repo = Repository::open(workdir).map_err(CliError::from)?; + if repo.current_view() != pinned.branch { + return Err(CliError::GitError { message: "span incremental import requires the current Atomic view to match the Git branch".into() }); + } + let limits = self.stream_limits()?; + let importer = ParallelImporter::new( + git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + mainline_only: true, + target_view: repo.current_view().to_string(), + ..Default::default() + }, + ); + let plan = + importer.validate_graph_stream_prospectively(&pinned.branch, pinned.tip, limits)?; + let expected_tree = plan.expected_git_tree().ok_or_else(|| CliError::GitError { + message: "span incremental preflight did not produce a Git tree proof".into(), + })?; + let raw_tree = plan.raw_git_tree().ok_or_else(|| CliError::GitError { + message: "span incremental preflight did not record the raw Git tree".into(), + })?; + #[cfg(all(test, unix))] + let mut observation_count = 0usize; + let outcome = + importer.import_span_incremental_prevalidated(&mut repo, plan, limits, || { + #[cfg(all(test, unix))] + { + observation_count += 1; + if observation_count == 3 { + pause_span_incremental_final_git_check_for_test(); + } + } + let current = SpanIncrementalGitObservation::capture(git)?; + if current != pinned { + return Err(CliError::GitError { + message: "Git HEAD or index changed during span incremental import".into(), + }); + } + Ok(()) + })?; + #[cfg(all(test, unix))] + pause_span_incremental_before_checkpoint_for_test(); + let current = SpanIncrementalGitObservation::capture(git)?; + if current != pinned { + return Err(CliError::GitError { + message: "Git HEAD or index changed before span checkpoint refresh; the imported view remains recorded, but the checkpoint was not advanced".into(), + }); + } + let working_copy = repo.require_working_copy_id().map_err(CliError::from)?; + let refresh = super::shim::refresh_checkpoint_after_verified_import( + &repo, + working_copy, + git, + &pinned.branch, + &expected_tree, + &raw_tree, + )?; + if refresh != super::shim::CheckpointRefresh::Refreshed { + return Err(CliError::GitError { + message: "span import was recorded, but its verified Git checkpoint could not be refreshed".into(), + }); + } + print_success(&format!( + "Imported {} incremental span change(s) from branch '{}'", + outcome.commits, pinned.branch + )); + Ok(()) + } + fn run_import(&self) -> CliResult<()> { let run_start = std::time::Instant::now(); - if self.stream || self.stream_window_mib.is_some() { + if self.stream || self.uses_span() || self.stream_window_mib.is_some() { self.stream_limits()?; } if self.no_stream && self.stream { @@ -796,6 +1062,22 @@ impl Import { let workdir = git_repo.workdir().ok_or_else(|| CliError::GitError { message: "Git repository has no working directory (bare repository?)".to_string(), })?; + #[cfg(feature = "experimental-span-import")] + if self.span && self.incremental { + return self.run_span_incremental(&git_repo, workdir); + } + // Keep capability inspection and the later workspace entry under one + // common lock. Never run writable recovery before this check: the + // ordinary importer cannot publish into a span-backed repository. + let _import_preflight = Repository::try_lock_git_import_without_span(workdir, self.dry_run) + .map_err(|error| CliError::GitError { + message: format!("Git import preflight refused: {error}"), + })?; + if self.uses_span() && workdir.join(".atomic").exists() { + return Err(CliError::GitError { + message: "--span requires a fresh Git checkout without an Atomic repository".into(), + }); + } trace_git_import(format!( "discover Git repository: {:?}", run_start.elapsed() @@ -892,7 +1174,7 @@ impl Import { } let mut stream = self.uses_stream(workdir.join(".atomic").exists()); - if stream && !self.stream && has_binding_transport(&git_repo)? { + if stream && !self.stream && !self.uses_span() && has_binding_transport(&git_repo)? { // Classify before preflight or mutation, not a fallback after a // failed stream. Explicit --stream keeps its strict refusal. stream = false; @@ -1007,6 +1289,7 @@ impl Import { repo_name: self.get_repo_name(&git_repo), mainline_only: !self.all_branches, graph_only: !self.with_crdt, + span_stream: self.uses_span(), preserve_working_copy: true, target_view: branch_name.clone(), known_states: known_states.clone(), @@ -1125,7 +1408,9 @@ impl Import { let original_view = workspace.view().name.clone(); let preserve_current_view = repo_exists && self.incremental; - if stream { + if stream && self.uses_span() { + print_hint("Experimental compact span-backed Git history: historical semantic FileOps are not synthesized; native content edits use span-derived semantics and unsupported operations refuse. Incremental span import requires an explicit --span --incremental and a clean, compatible first-parent checkout."); + } else if stream { print_hint("Streaming graph-only Git history without historical CRDT FileOps; final tip verification remains enabled."); } else if self.with_crdt { print_info( @@ -2276,6 +2561,694 @@ mod tests { ); } + #[cfg(feature = "experimental-span-import")] + #[test] + fn span_import_is_explicit_and_rejects_incompatible_modes() { + let import = Import::try_parse_from(["import", "--span"]).unwrap(); + assert!(import.uses_span()); + assert!(import.uses_stream(false)); + assert!(!Import::try_parse_from(["import"]).unwrap().uses_span()); + assert!(Import::try_parse_from(["import", "--span", "--incremental"]).is_ok()); + for mode in ["--no-stream", "--with-crdt", "--all"] { + assert!( + Import::try_parse_from(["import", "--span", mode]).is_err(), + "{mode}" + ); + } + } + + #[cfg(all(unix, feature = "experimental-span-import"))] + #[test] + #[file_serial] + fn explicit_span_incremental_import_preserves_git_and_user_bytes() { + let temp = tempfile::tempdir().unwrap(); + init_git(temp.path()); + fs::write(temp.path().join("tracked.txt"), b"original\nsecond\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "initial"]); + let _guard = DirGuard::new(); + std::env::set_current_dir(temp.path()).unwrap(); + Import { + span: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + fs::write( + temp.path().join("tracked.txt"), + b"original\nchanged in Git\n", + ) + .unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "next"]); + fs::write(temp.path().join("untracked.txt"), b"untracked user bytes\n").unwrap(); + let ignore = fs::read(temp.path().join(".atomicignore")).unwrap(); + let head = fs::read(temp.path().join(".git/HEAD")).unwrap(); + let index = fs::read(temp.path().join(".git/index")).unwrap(); + let import = Import { + span: true, + incremental: true, + no_vault: true, + ..Default::default() + }; + import.run().unwrap(); + let repo = Repository::open(temp.path()).unwrap(); + assert_eq!( + repo.get_file_content("tracked.txt").unwrap(), + Some(b"original\nchanged in Git\n".to_vec()) + ); + assert_eq!( + repo.get_file_content(".atomicignore").unwrap(), + Some(ignore.clone()) + ); + let wc = repo.require_working_copy_id().unwrap(); + let status = repo + .status( + wc, + atomic_repository::StatusOptions::default().with_untracked(false), + ) + .unwrap(); + assert!( + status.is_clean(), + "incremental span import should match the checked-out Git files: {status:?}" + ); + let before_working = repo + .operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + None, + false, + ) + .unwrap(); + let before_global = repo + .operation_log( + atomic_core::operation::OperationScope::Repository, + None, + false, + ) + .unwrap(); + drop(repo); + import.run().unwrap(); + let repo = Repository::open(temp.path()).unwrap(); + assert_eq!( + repo.operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + None, + false + ) + .unwrap(), + before_working + ); + assert_eq!( + repo.operation_log( + atomic_core::operation::OperationScope::Repository, + None, + false + ) + .unwrap(), + before_global + ); + drop(repo); + assert_eq!(fs::read(temp.path().join(".git/HEAD")).unwrap(), head); + assert_eq!(fs::read(temp.path().join(".git/index")).unwrap(), index); + assert_eq!( + fs::read(temp.path().join("tracked.txt")).unwrap(), + b"original\nchanged in Git\n" + ); + assert_eq!( + fs::read(temp.path().join("untracked.txt")).unwrap(), + b"untracked user bytes\n" + ); + assert_eq!(fs::read(temp.path().join(".atomicignore")).unwrap(), ignore); + + git_ok( + temp.path(), + &["commit", "--allow-empty", "-qm", "empty follow-up"], + ); + let empty_head = GitRepository::open(temp.path()) + .unwrap() + .head() + .unwrap() + .target() + .unwrap(); + import.run().unwrap(); + let repo = Repository::open(temp.path()).unwrap(); + let mut empty_changes = Vec::new(); + for entry in repo.effective_history(None).unwrap() { + let change = repo.load_change(&entry.hash).unwrap(); + if change.hashed.header.message == "empty follow-up" { + empty_changes.push(change); + } + } + assert_eq!(empty_changes.len(), 1); + assert!(empty_changes[0].hashed.span.is_some()); + assert!(empty_changes[0].file_ops().is_empty()); + assert_eq!( + repo.get_file_content("tracked.txt").unwrap(), + Some(b"original\nchanged in Git\n".to_vec()) + ); + drop(repo); + assert_eq!( + GitRepository::open(temp.path()) + .unwrap() + .head() + .unwrap() + .target() + .unwrap(), + empty_head + ); + let index = fs::read(temp.path().join(".git/index")).unwrap(); + + fs::write(temp.path().join("tracked.txt"), b"dirty user bytes\n").unwrap(); + let database = temp.path().join(".atomic/atomic.redb"); + let before_database = fs::read(&database).unwrap(); + let error = import.run().unwrap_err(); + assert!(error.to_string().contains("clean Git index"), "{error}"); + assert_eq!(fs::read(&database).unwrap(), before_database); + assert_eq!( + fs::read(temp.path().join("tracked.txt")).unwrap(), + b"dirty user bytes\n" + ); + assert_eq!(fs::read(temp.path().join(".git/index")).unwrap(), index); + } + + #[cfg(all(unix, feature = "experimental-span-import"))] + #[test] + #[ignore = "child entry point, explicitly exercised by span_incremental_checkpoint_kill_retries"] + fn span_incremental_checkpoint_kill_child() { + let root = + std::env::var_os("ATOMIC_TEST_SPAN_INCREMENTAL_CHECKPOINT_ROOT").expect("child root"); + std::env::set_current_dir(root).unwrap(); + Import { + span: true, + incremental: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + panic!("checkpoint pause was not reached"); + } + + #[cfg(all(unix, feature = "experimental-span-import"))] + #[test] + #[file_serial] + fn span_incremental_checkpoint_kill_retries() { + use atomic_core::operation::OperationScope; + use std::io::BufRead; + use std::os::unix::process::ExitStatusExt; + let temp = tempfile::tempdir().unwrap(); + init_git(temp.path()); + fs::write(temp.path().join("tracked.txt"), b"before\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "initial"]); + let _guard = DirGuard::new(); + std::env::set_current_dir(temp.path()).unwrap(); + Import { + span: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + let checkpoint = super::super::checkpoint::checkpoint_path(temp.path()); + let previous_checkpoint = fs::read(&checkpoint).unwrap(); + fs::write(temp.path().join("tracked.txt"), b"after\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "next"]); + fs::write(temp.path().join("user.txt"), b"untracked user bytes\n").unwrap(); + let head = GitRepository::open(temp.path()) + .unwrap() + .head() + .unwrap() + .target() + .unwrap(); + let index = fs::read(temp.path().join(".git/index")).unwrap(); + let mut child = ProcessCommand::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "commands::git::import::tests::span_incremental_checkpoint_kill_child", + "--ignored", + "--nocapture", + ]) + .env("ATOMIC_TEST_SPAN_INCREMENTAL_CHECKPOINT_ROOT", temp.path()) + .env("ATOMIC_TEST_SPAN_INCREMENTAL_CHECKPOINT_PAUSE", "1") + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let stdout = child.stdout.take().unwrap(); + let (send, receive) = std::sync::mpsc::channel(); + let reader = std::thread::spawn(move || { + let mut output = String::new(); + for line in std::io::BufReader::new(stdout).lines() { + match line { + Ok(line) if line.contains("ATOMIC_SPAN_INCREMENTAL_BEFORE_CHECKPOINT") => { + let _ = send.send(Ok(())); + return; + } + Ok(line) => { + output.push_str(&line); + output.push('\n'); + } + Err(error) => { + let _ = send.send(Err(error.to_string())); + return; + } + } + } + let _ = send.send(Err(format!( + "child exited before checkpoint seam:\n{output}" + ))); + }); + let ready = receive.recv_timeout(std::time::Duration::from_secs(60)); + let killed = child.kill(); + let status = child.wait().unwrap(); + if ready.is_ok() { + reader.join().unwrap(); + } + ready + .expect("child timed out") + .expect("checkpoint seam was not reached"); + assert!(killed.is_ok() && status.signal() == Some(9)); + assert_eq!(fs::read(&checkpoint).unwrap(), previous_checkpoint); + assert_eq!( + fs::read(temp.path().join("tracked.txt")).unwrap(), + b"after\n" + ); + assert_eq!( + fs::read(temp.path().join("user.txt")).unwrap(), + b"untracked user bytes\n" + ); + assert_eq!(fs::read(temp.path().join(".git/index")).unwrap(), index); + let repo = Repository::open(temp.path()).unwrap(); + assert_eq!( + repo.get_file_content("tracked.txt").unwrap(), + Some(b"after\n".to_vec()) + ); + let wc = repo.require_working_copy_id().unwrap(); + let before_retry = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let before_global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + drop(repo); + Import { + span: true, + incremental: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + assert_ne!(fs::read(&checkpoint).unwrap(), previous_checkpoint); + assert_eq!( + GitRepository::open(temp.path()) + .unwrap() + .head() + .unwrap() + .target() + .unwrap(), + head + ); + assert_eq!(fs::read(temp.path().join(".git/index")).unwrap(), index); + assert_eq!( + fs::read(temp.path().join("user.txt")).unwrap(), + b"untracked user bytes\n" + ); + let mut repo = Repository::open(temp.path()).unwrap(); + assert_eq!( + repo.operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + before_retry + ); + assert_eq!( + repo.operation_log(OperationScope::Repository, None, false) + .unwrap(), + before_global + ); + assert!( + observe_workspace(&mut repo).unwrap().is_ok(), + "retry must restore a coherent checkpoint" + ); + } + + #[cfg(all(unix, feature = "experimental-span-import"))] + #[test] + #[ignore = "child entry point, explicitly exercised by span_incremental_concurrent_git_edit_refuses"] + fn span_incremental_concurrent_git_edit_child() { + let root = std::env::var_os("ATOMIC_TEST_SPAN_INCREMENTAL_GIT_ROOT").expect("child root"); + std::env::set_current_dir(root).unwrap(); + let error = Import { + span: true, + incremental: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap_err(); + assert!(error.to_string().contains("clean Git index"), "{error}"); + } + + #[cfg(all(unix, feature = "experimental-span-import"))] + #[test] + #[file_serial] + fn span_incremental_concurrent_git_edit_refuses() { + use atomic_core::operation::OperationScope; + use std::io::{BufRead, Write}; + let temp = tempfile::tempdir().unwrap(); + init_git(temp.path()); + fs::write(temp.path().join("tracked.txt"), b"before\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "initial"]); + let _guard = DirGuard::new(); + std::env::set_current_dir(temp.path()).unwrap(); + Import { + span: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + let repo = Repository::open(temp.path()).unwrap(); + let original_state = repo.get_view_info(repo.current_view()).unwrap().state; + let wc = repo.require_working_copy_id().unwrap(); + drop(repo); + fs::write(temp.path().join("tracked.txt"), b"committed Git version\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "next"]); + fs::write(temp.path().join("user.txt"), b"untracked user bytes\n").unwrap(); + let mut child = ProcessCommand::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "commands::git::import::tests::span_incremental_concurrent_git_edit_child", + "--ignored", + "--nocapture", + ]) + .env("ATOMIC_TEST_SPAN_INCREMENTAL_GIT_ROOT", temp.path()) + .env("ATOMIC_TEST_SPAN_INCREMENTAL_GIT_PAUSE", "1") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let stdout = child.stdout.take().unwrap(); + let (send, receive) = std::sync::mpsc::channel(); + let reader = std::thread::spawn(move || { + let mut output = String::new(); + let mut sent = false; + for line in std::io::BufReader::new(stdout).lines() { + let line = line.unwrap(); + if !sent && line.contains("ATOMIC_SPAN_INCREMENTAL_FINAL_GIT_CHECK") { + let _ = send.send(()); + sent = true; + } + output.push_str(&line); + output.push('\n'); + } + output + }); + if receive + .recv_timeout(std::time::Duration::from_secs(60)) + .is_err() + { + let _ = child.kill(); + let _ = child.wait(); + panic!("child did not reach the final Git observation"); + } + fs::write(temp.path().join("tracked.txt"), b"concurrent user edit\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + let staged_index = fs::read(temp.path().join(".git/index")).unwrap(); + child.stdin.take().unwrap().write_all(b"x").unwrap(); + let status = child.wait().unwrap(); + let output = reader.join().unwrap(); + assert!( + status.success(), + "child must observe and refuse the concurrent edit: {output}" + ); + assert_eq!( + fs::read(temp.path().join("tracked.txt")).unwrap(), + b"concurrent user edit\n" + ); + assert_eq!( + fs::read(temp.path().join("user.txt")).unwrap(), + b"untracked user bytes\n" + ); + assert_eq!( + fs::read(temp.path().join(".git/index")).unwrap(), + staged_index + ); + let repo = Repository::open(temp.path()).unwrap(); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + original_state + ); + assert_eq!( + repo.get_file_content("tracked.txt").unwrap(), + Some(b"before\n".to_vec()) + ); + let before_retry = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let before_global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + drop(repo); + let error = Import { + span: true, + incremental: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap_err(); + assert!(error.to_string().contains("clean Git index"), "{error}"); + let repo = Repository::open(temp.path()).unwrap(); + assert_eq!( + repo.operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + before_retry + ); + assert_eq!( + repo.operation_log(OperationScope::Repository, None, false) + .unwrap(), + before_global + ); + assert_eq!( + fs::read(temp.path().join(".git/index")).unwrap(), + staged_index + ); + assert_eq!( + fs::read(temp.path().join("tracked.txt")).unwrap(), + b"concurrent user edit\n" + ); + } + + #[cfg(all(unix, feature = "experimental-span-import"))] + #[test] + #[file_serial] + fn existing_span_repository_rejects_git_reimport_before_any_mutation() { + let temp = tempfile::tempdir().unwrap(); + init_git(temp.path()); + fs::write(temp.path().join("tracked.txt"), b"initial\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "initial"]); + let _guard = DirGuard::new(); + std::env::set_current_dir(temp.path()).unwrap(); + Import { + span: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + + fs::write(temp.path().join("tracked.txt"), b"next Git version\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "next"]); + fs::write(temp.path().join("untracked.txt"), b"untracked user bytes\n").unwrap(); + let database = temp.path().join(".atomic/atomic.redb"); + let index = temp.path().join(".git/index"); + let expected_database = fs::read(&database).unwrap(); + let expected_index = fs::read(&index).unwrap(); + let expected_head = fs::read(temp.path().join(".git/HEAD")).unwrap(); + + for (incremental, dry_run, span) in [ + (true, false, false), + (true, true, false), + (false, false, false), + (false, true, false), + (false, false, true), + ] { + let error = Import { + incremental, + dry_run, + span, + no_vault: true, + ..Default::default() + } + .run() + .unwrap_err(); + assert!( + error.to_string().contains("existing span repository"), + "{error}" + ); + assert_eq!( + fs::read(&database).unwrap(), + expected_database, + "database changed for incremental={incremental} dry_run={dry_run} span={span}" + ); + assert_eq!(fs::read(&index).unwrap(), expected_index); + assert_eq!( + fs::read(temp.path().join(".git/HEAD")).unwrap(), + expected_head + ); + assert_eq!( + fs::read(temp.path().join("tracked.txt")).unwrap(), + b"next Git version\n" + ); + assert_eq!( + fs::read(temp.path().join("untracked.txt")).unwrap(), + b"untracked user bytes\n" + ); + } + } + + #[cfg(all(unix, feature = "experimental-span-import"))] + #[test] + #[file_serial] + fn detached_span_shim_reconcile_refuses_before_view_or_journal_mutation() { + let temp = tempfile::tempdir().unwrap(); + init_git(temp.path()); + fs::write(temp.path().join("tracked.txt"), b"initial\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "initial"]); + let _guard = DirGuard::new(); + std::env::set_current_dir(temp.path()).unwrap(); + Import { + span: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + fs::write(temp.path().join("tracked.txt"), b"new Git version\n").unwrap(); + git_ok(temp.path(), &["add", "tracked.txt"]); + git_ok(temp.path(), &["commit", "-qm", "next"]); + git_ok(temp.path(), &["switch", "--quiet", "--detach", "HEAD"]); + let database = temp.path().join(".atomic/atomic.redb"); + let index = temp.path().join(".git/index"); + let head = temp.path().join(".git/HEAD"); + let expected_database = fs::read(&database).unwrap(); + let expected_index = fs::read(&index).unwrap(); + let expected_head = fs::read(&head).unwrap(); + let error = super::super::shim::reconcile_transaction_budgeted( + temp.path(), + atomic_repository::repository::ReconcileEffectBudget::Command, + None, + ) + .unwrap_err(); + assert!( + error.to_string().contains("existing span repository"), + "{error}" + ); + assert_eq!(fs::read(&database).unwrap(), expected_database); + assert_eq!(fs::read(&index).unwrap(), expected_index); + assert_eq!(fs::read(&head).unwrap(), expected_head); + assert_eq!( + fs::read(temp.path().join("tracked.txt")).unwrap(), + b"new Git version\n" + ); + } + + #[cfg(all(unix, feature = "experimental-span-import"))] + #[test] + #[file_serial] + fn linked_worktree_span_preflight_preserves_common_and_local_state() { + let temp = tempfile::tempdir().unwrap(); + let primary = temp.path().join("primary"); + let linked = temp.path().join("linked"); + fs::create_dir(&primary).unwrap(); + init_git(&primary); + fs::write(primary.join("tracked.txt"), b"original\n").unwrap(); + git_ok(&primary, &["add", "tracked.txt"]); + git_ok(&primary, &["commit", "-qm", "initial"]); + let _guard = DirGuard::new(); + std::env::set_current_dir(&primary).unwrap(); + Import { + span: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + git_ok( + &primary, + &[ + "worktree", + "add", + "--quiet", + "--detach", + linked.to_str().unwrap(), + ], + ); + fs::write(linked.join("tracked.txt"), b"local user edit\n").unwrap(); + let git = GitRepository::open(&linked).unwrap(); + let index = git.path().join("index"); + let head = git.path().join("HEAD"); + let database = primary.join(".atomic/atomic.redb"); + let expected_database = fs::read(&database).unwrap(); + let expected_index = fs::read(&index).unwrap(); + let expected_head = fs::read(&head).unwrap(); + assert!(!linked.join(".atomic").exists()); + std::env::set_current_dir(&linked).unwrap(); + + for dry_run in [false, true] { + let error = Import { + incremental: true, + dry_run, + no_vault: true, + ..Default::default() + } + .run() + .unwrap_err(); + assert!( + error.to_string().contains("existing span repository"), + "{error}" + ); + } + let error = Import { + span: true, + incremental: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap_err(); + assert!( + error.to_string().contains("existing span repository") + || error.to_string().contains("attached Git branch"), + "{error}" + ); + let error = super::super::shim::reconcile_transaction_budgeted( + &linked, + atomic_repository::repository::ReconcileEffectBudget::Command, + None, + ) + .unwrap_err(); + assert!( + error.to_string().contains("existing span repository"), + "{error}" + ); + assert_eq!(fs::read(&database).unwrap(), expected_database); + assert_eq!(fs::read(&index).unwrap(), expected_index); + assert_eq!(fs::read(&head).unwrap(), expected_head); + assert_eq!( + fs::read(linked.join("tracked.txt")).unwrap(), + b"local user edit\n" + ); + assert!(!linked.join(".atomic").exists()); + } + #[test] fn fresh_import_defaults_to_stream_and_advanced_modes_remain_ordinary() { let default = Import::try_parse_from(["import"]).unwrap(); diff --git a/atomic-cli/src/commands/git/parallel.rs b/atomic-cli/src/commands/git/parallel.rs index 0fa16840..6acbefdc 100644 --- a/atomic-cli/src/commands/git/parallel.rs +++ b/atomic-cli/src/commands/git/parallel.rs @@ -723,6 +723,8 @@ pub struct ParallelImportOptions { /// duplicate file content are omitted, which significantly reduces change /// size and import time for large repositories. pub graph_only: bool, + /// Compile fresh first-parent streams as immutable spans rather than line edges. + pub span_stream: bool, /// Keep a foreign Atomic view's working copy untouched while importing. /// /// When an agent draft is current, the files on disk describe that draft, @@ -755,6 +757,7 @@ impl Default for ParallelImportOptions { repo_name: "unknown".to_string(), mainline_only: true, graph_only: false, + span_stream: false, preserve_working_copy: false, target_view: String::new(), known_states: HashSet::new(), @@ -8045,11 +8048,31 @@ impl ParallelImporter { "short_sha": parsed.short_sha, }); + // A pure Git-detected rename has no patch lines, but omitting it makes + // the Git-origin diff fall back to the graph's deliberate delete+add + // representation. Record this as non-authoritative Git evidence only + // when every path has a usable text diff or is an exact-byte rename. + let complete_diff = parsed.files.iter().all(|file| { + file.diff_lines.is_some() + || (file.operation == FileOperation::Renamed + && file.old_content.is_some() + && file.old_content == file.new_content) + }); let diff_files: Vec = parsed .files .iter() + // A partial list would silently omit changed paths when the + // metadata-based renderer takes precedence over graph diffs. + .filter(|_| complete_diff) .filter_map(|file| { - file.diff_lines.as_ref().map(|lines| { + let exact_rename = file.operation == FileOperation::Renamed + && file.old_content.is_some() + && file.old_content == file.new_content; + let lines = file + .diff_lines + .as_deref() + .or_else(|| (complete_diff && exact_rename).then_some(&[][..]))?; + Some({ serde_json::json!({ "path": file.path, "old_path": file.old_path, @@ -9757,6 +9780,50 @@ mod tests { } } + #[test] + fn pure_rename_diff_evidence_requires_a_complete_git_diff() { + let dir = tempfile::tempdir().unwrap(); + let git = GitRepository::init(dir.path()).unwrap(); + let importer = ParallelImporter::new(&git, ParallelImportOptions::default()); + let mut parsed = added_commit("new.txt", b"same\n"); + parsed.files[0].operation = FileOperation::Renamed; + parsed.files[0].old_path = Some("old.txt".into()); + parsed.files[0].old_content = Some(b"same\n".to_vec()); + let metadata = importer.build_git_metadata(&parsed, false, false); + assert_eq!(metadata["git"]["diff_lines"][0]["old_path"], "old.txt"); + assert_eq!(metadata["git"]["diff_lines"][0]["operation"], "renamed"); + assert_eq!( + metadata["git"]["diff_lines"][0]["lines"], + serde_json::json!([]) + ); + + // A missing diff for a different changed file must not make a + // rename-only metadata projection hide that file's changes. + parsed.files.push(ParsedFile { + path: "other.bin".into(), + operation: FileOperation::Modified, + old_content: Some(vec![0]), + new_content: Some(vec![1]), + diff_lines: None, + old_path: None, + new_mode: Some(0o100644), + }); + let metadata = importer.build_git_metadata(&parsed, false, false); + assert!(metadata["git"].get("diff_lines").is_none()); + + parsed.files.push(ParsedFile { + path: "text.txt".into(), + operation: FileOperation::Modified, + old_content: Some(b"old\n".to_vec()), + new_content: Some(b"new\n".to_vec()), + diff_lines: Some(vec![]), + old_path: None, + new_mode: Some(0o100644), + }); + let metadata = importer.build_git_metadata(&parsed, false, false); + assert!(metadata["git"].get("diff_lines").is_none()); + } + #[test] fn touched_expectation_covers_only_the_commit_paths() { let policy = ConversionPolicy::new(GitHashAlgorithm::Sha1); diff --git a/atomic-cli/src/commands/git/shim.rs b/atomic-cli/src/commands/git/shim.rs index 325ba5af..2e1f6dee 100644 --- a/atomic-cli/src/commands/git/shim.rs +++ b/atomic-cli/src/commands/git/shim.rs @@ -949,6 +949,13 @@ pub(crate) fn reconcile_transaction_budgeted( budget: ReconcileEffectBudget, notice: Option<&WatchNoticeSink<'_>>, ) -> CliResult<()> { + // The Git-to-Atomic arm may create a detached view before invoking the + // importer. Until span-aware incremental synthesis exists, refuse before + // opening writable state or appending reconciliation effects. Keep the + // lock for the whole boundary so span capability cannot race this check. + let _span_preflight = + Repository::try_lock_git_import_without_span(root, budget.is_metadata_only()) + .map_err(|error| git_error(format!("Git shim preflight refused: {error}")))?; let emit_notice = |kind: WatchNoticeKind, detail: &str| { if let Some(notice) = notice { notice(kind, detail); @@ -1790,6 +1797,9 @@ fn import_git_to_atomic(root: &Path) -> CliResult<()> { /// defers instead of replaying. #[tracing::instrument(name = "import_head", skip_all, fields(budget = ?budget))] fn import_git_to_atomic_budgeted(root: &Path, budget: ReconcileEffectBudget) -> CliResult<()> { + let _span_preflight = + Repository::try_lock_git_import_without_span(root, budget.is_metadata_only()) + .map_err(|error| git_error(format!("Git shim import preflight refused: {error}")))?; let git = open_git(root)?; let head = observe_head(&git).map_err(observation_error)?; // §7.5: a detached HEAD imports behind its commit into the workspace diff --git a/atomic-cli/src/commands/git/span_compiler.rs b/atomic-cli/src/commands/git/span_compiler.rs new file mode 100644 index 00000000..6a5e021a --- /dev/null +++ b/atomic-cli/src/commands/git/span_compiler.rs @@ -0,0 +1,266 @@ +//! Lower first-parent Git line-index updates into immutable span operations. +//! +//! The graph compiler supplies namespace hunks and content offsets. This +//! index tracks which authored span owns each currently visible line. + +use super::*; +use atomic_core::change::span_patch::{ExternalFile, ExternalSpan, SpanOp}; + +#[derive(Clone)] +struct SpanLine { + span: ExternalSpan, + start: u64, + end: u64, +} + +#[derive(Clone)] +struct SpanFile { + file: ExternalFile, + root: ExternalSpan, + lines: Vec, +} + +#[derive(Default)] +pub(super) struct SpanCompiler { + files: HashMap, +} + +pub(super) struct NewRoot { + path: String, + ordinal: u32, + start: u64, + end: u64, +} + +impl SpanCompiler { + pub(super) fn lower( + &self, + pending: &[PendingLineIndexUpdate], + hunks: &mut Vec>>, + ) -> CliResult<(Vec, Vec)> { + let mut ops = Vec::new(); + let mut roots = Vec::new(); + for update in pending { + match update { + PendingLineIndexUpdate::Add { + path, new_ranges, .. + } => { + let (empty_start, root_path) = hunks + .iter() + .find_map(|hunk| { + if let GraphOp::FileAdd { + path: name, + add_inode, + .. + } = hunk + { + (atomic_repository::canonical_path_identity(name) + == atomic_repository::canonical_path_identity(path)) + .then_some((add_inode.start.get(), name.clone())) + } else { + None + } + }) + .ok_or_else(|| { + git_error(format!("span add has no namespace operation for {path}")) + })?; + let start = new_ranges + .first() + .map_or(empty_start, |range| range.0.get()); + let end = new_ranges.last().map_or(start, |range| range.1.get()); + let ordinal = u32::try_from(roots.len()) + .map_err(|_| git_error("too many span roots in one change"))?; + ops.push(SpanOp::Root { + path: root_path, + ordinal, + start, + end, + }); + roots.push(NewRoot { + path: path.clone(), + ordinal, + start, + end, + }); + } + PendingLineIndexUpdate::Modify { path, replacements } => { + let file = self.files.get(path).ok_or_else(|| { + git_error(format!("span file identity missing for {path}")) + })?; + for replacement in replacements { + let begin = replacement.start_idx; + let end = begin + .checked_add(replacement.old_len) + .ok_or_else(|| git_error("span replacement overflow"))?; + if end > file.lines.len() { + return Err(git_error("span replacement exceeds current file")); + } + let insert_start = replacement + .new_ranges + .first() + .map_or(0, |range| range.0.get()); + let insert_end = replacement + .new_ranges + .last() + .map_or(insert_start, |range| range.1.get()); + if begin == end { + let (target, at) = if begin > 0 { + let line = &file.lines[begin - 1]; + (line.span.clone(), line.end) + } else if let Some(line) = file.lines.first() { + (line.span.clone(), line.start) + } else { + (file.root.clone(), file.root.start) + }; + ops.push(SpanOp::Edit { + file: file.file.clone(), + start: at - target.start, + end: at - target.start, + target, + insert_start, + insert_end, + }); + continue; + } + let mut current = begin; + let mut first = true; + while current < end { + let line = &file.lines[current]; + let target = line.span.clone(); + let start = line.start; + let mut finish = line.end; + current += 1; + while current < end { + let next = &file.lines[current]; + if next.span != target || next.start != finish { + break; + } + finish = next.end; + current += 1; + } + ops.push(SpanOp::Edit { + file: file.file.clone(), + target: target.clone(), + start: start - target.start, + end: finish - target.start, + insert_start: if first { insert_start } else { 0 }, + insert_end: if first { insert_end } else { 0 }, + }); + first = false; + } + } + } + PendingLineIndexUpdate::Rename { .. } => { + return Err(git_error( + "span compiler requires normalized delete/add renames", + )) + } + PendingLineIndexUpdate::DirectoryAdd { .. } + | PendingLineIndexUpdate::Delete { .. } => {} + } + } + // Preserve namespace and attributes, but do not duplicate content as + // per-line vertices or adjacency edges in the ordinary graph tables. + hunks.retain_mut(|hunk| match hunk { + GraphOp::FileAdd { contents, .. } => { + *contents = None; + true + } + GraphOp::FileDel { del, contents, .. } => { + del.edges.clear(); + *contents = None; + true + } + GraphOp::DirAdd { .. } | GraphOp::DirDel { .. } | GraphOp::SetAttr { .. } => true, + GraphOp::Edit { .. } | GraphOp::Replacement { .. } => false, + _ => true, + }); + Ok((ops, roots)) + } + + pub(super) fn finish( + &mut self, + hash: ContentHash, + pending: &[PendingLineIndexUpdate], + roots: &[NewRoot], + ) -> CliResult<()> { + for update in pending { + match update { + PendingLineIndexUpdate::Add { + path, new_ranges, .. + } => { + let root = roots + .iter() + .find(|root| &root.path == path) + .ok_or_else(|| git_error("span root missing after compilation"))?; + let span = ExternalSpan { + owner: hash, + start: root.start, + end: root.end, + }; + let lines = new_ranges + .iter() + .map(|(start, end)| SpanLine { + span: span.clone(), + start: start.get(), + end: end.get(), + }) + .collect(); + self.files.insert( + path.clone(), + SpanFile { + file: ExternalFile { + owner: hash, + ordinal: root.ordinal, + }, + root: span, + lines, + }, + ); + } + PendingLineIndexUpdate::Modify { path, replacements } => { + let file = self + .files + .get_mut(path) + .ok_or_else(|| git_error("span file vanished"))?; + for replacement in replacements.iter().rev() { + let start = replacement.start_idx; + let end = start + replacement.old_len; + let lines = if let (Some(first), Some(last)) = ( + replacement.new_ranges.first(), + replacement.new_ranges.last(), + ) { + let span = ExternalSpan { + owner: hash, + start: first.0.get(), + end: last.1.get(), + }; + replacement + .new_ranges + .iter() + .map(|(lo, hi)| SpanLine { + span: span.clone(), + start: lo.get(), + end: hi.get(), + }) + .collect::>() + } else { + Vec::new() + }; + file.lines.splice(start..end, lines); + } + } + PendingLineIndexUpdate::Delete { path } => { + self.files.remove(path); + } + PendingLineIndexUpdate::DirectoryAdd { .. } => {} + PendingLineIndexUpdate::Rename { .. } => { + return Err(git_error( + "span compiler cannot preserve Git rename identity", + )) + } + } + } + Ok(()) + } +} diff --git a/atomic-cli/src/commands/git/stream.rs b/atomic-cli/src/commands/git/stream.rs index 856c7976..8b82c8a4 100644 --- a/atomic-cli/src/commands/git/stream.rs +++ b/atomic-cli/src/commands/git/stream.rs @@ -9,6 +9,8 @@ use atomic_repository::{ #[path = "rolling_dashboard.rs"] mod rolling_dashboard; +#[path = "span_compiler.rs"] +mod span_compiler; #[cfg(test)] thread_local! { static FORBID_ORDINARY_RECORDER: std::cell::Cell = const { std::cell::Cell::new(false) }; } @@ -86,6 +88,7 @@ struct Compiler { index: ImportLineIndex, attrs: HashMap, last_hash: Option, + spans: Option, } impl Compiler { @@ -332,7 +335,18 @@ impl Compiler { } let mut deps: Vec<_> = deps.into_iter().collect(); deps.sort(); - let compiled = Change::new(header, hunks, change.contents, deps); + let span_lowering = self + .spans + .as_ref() + .map(|spans| spans.lower(&pending, &mut hunks)) + .transpose()?; + let mut compiled = Change::new(header, hunks, change.contents, deps); + if let Some((ops, _)) = &span_lowering { + compiled.hashed.span = Some(atomic_core::change::span_patch::SpanPatch::new( + Vec::new(), + ops.clone(), + )); + } let algorithm = git_algorithm_for_sha(&parsed.git_sha)?; let parents = parsed .parent_oids @@ -367,9 +381,16 @@ impl Compiler { record["tree_algorithm"] = record["algorithm"].clone(); facts = serde_json::to_vec(&record).map_err(|error| git_error(error.to_string()))?; } - let prepared = PreparedGitGraphChange::new(compiled, origin, facts, unhashed) - .map_err(CliError::from)?; + let prepared = if span_lowering.is_some() { + PreparedGitGraphChange::new_span(compiled, origin, facts, unhashed) + } else { + PreparedGitGraphChange::new(compiled, origin, facts, unhashed) + } + .map_err(CliError::from)?; let hash = prepared.hash(); + if let (Some(spans), Some((_, roots))) = (&mut self.spans, &span_lowering) { + spans.finish(hash, &pending, roots)?; + } self.last_hash = Some(hash); apply_line_index_updates(&mut self.index, hash, pending); for path in retired { @@ -485,7 +506,12 @@ impl ParallelImporter { let started = Instant::now(); let outcome = self.import_graph_stream_rolling(repo, stream.tip, limits, &stream.ordered)?; - print_info(&format!("Direct graph stream: {} commits in {} private batches; final tip verified (historical states not independently verified)", outcome.commits, outcome.batches)); + let kind = if self.options.span_stream { + "span" + } else { + "graph" + }; + print_info(&format!("Direct {kind} stream: {} commits in {} private batches; final tip verified (historical states not independently verified)", outcome.commits, outcome.batches)); Ok(ImportStats { commits_found: total, commits_parsed: total, @@ -497,6 +523,198 @@ impl ParallelImporter { ..Default::default() }) } + + /// Rebuild the compiler from the exact persisted prefix, then append only + /// the unimported suffix through the journaled private span stage. + /// This internal path does not inspect Git HEAD/index or the worktree; the + /// command boundary must hold the common lock and prove those separately. + pub(crate) fn import_span_incremental( + &self, + repo: &mut Repository, + tip: Oid, + limits: GitStreamLimits, + expected_order: &[Oid], + mut validate_git: F, + ) -> CliResult + where + F: FnMut() -> CliResult<()>, + { + use atomic_core::pristine::{GraphTxnT, ViewTxnT}; + + if !self.options.graph_only + || !self.options.mainline_only + || !self.options.span_stream + || !self.options.imported_shas.is_empty() + || self.options.target_view != repo.current_view() + { + return Err(git_error("incremental span stream requires an unfiltered first-parent replay into the current view")); + } + let (git, policy, ordered) = self.graph_stream_inputs(tip)?; + if ordered != expected_order { + return Err(git_error( + "pinned Git stream history changed after preflight", + )); + } + let (stage_hashes, target_members, target_state) = { + let txn = repo + .pristine() + .read_txn() + .map_err(|error| git_error(error.to_string()))?; + let target = txn + .get_view(repo.current_view()) + .map_err(|error| git_error(error.to_string()))? + .ok_or_else(|| git_error("incremental span target view missing"))?; + let stage = txn + .get_view("git-stream-private") + .map_err(|error| git_error(error.to_string()))? + .ok_or_else(|| git_error("incremental span private stage missing"))?; + let hashes = |view: &atomic_core::pristine::ViewState| -> CliResult> { + txn.iter_changes(view, 0) + .map_err(|error| git_error(error.to_string()))? + .map(|row| { + let (_, id, _) = row.map_err(|error| git_error(error.to_string()))?; + txn.get_external(id) + .map_err(|error| git_error(error.to_string()))? + .ok_or_else(|| git_error("stream member has no change hash")) + }) + .collect() + }; + ( + hashes(&stage)?, + hashes(&target)?.into_iter().collect::>(), + target.state, + ) + }; + if stage_hashes.is_empty() || stage_hashes.len() > ordered.len() { + return Err(git_error( + "incremental span history is not an ancestor of the pinned Git tip", + )); + } + let base_len = stage_hashes + .iter() + .take_while(|hash| target_members.contains(*hash)) + .count(); + if base_len == 0 + || stage_hashes[base_len..] + .iter() + .any(|hash| target_members.contains(hash)) + { + return Err(git_error( + "incremental span stage and target do not share a contiguous prefix", + )); + } + let positions: HashMap<_, _> = ordered + .iter() + .copied() + .enumerate() + .map(|(index, oid)| (oid, index)) + .collect(); + let mut compiler = Compiler { + spans: Some(Default::default()), + ..Default::default() + }; + let mut prefix = Vec::with_capacity(base_len); + let mut staged = Vec::with_capacity(stage_hashes.len() - base_len); + for (index, oid) in ordered.iter().copied().take(stage_hashes.len()).enumerate() { + let commit = git + .find_commit(oid) + .map_err(|error| git_error(error.to_string()))?; + input_budget(&git, &commit, limits.bytes())?; + let parsed = parse_commit_with_scope(&git, oid, index, &positions, false)?; + let metadata = self.build_git_metadata(&parsed, parsed.is_empty, parsed.is_merge); + let prepared = compiler.compile(&parsed, &policy, &[], metadata)?; + if prepared.hash() != stage_hashes[index] { + return Err(git_error(format!( + "replayed Git commit {oid} differs from the persisted private stage" + ))); + } + if index < base_len { + prefix.push((oid.to_string(), prepared.hash())); + } else { + staged.push((oid.to_string(), prepared.hash())); + } + } + let baseline = import_expected_tree_oid( + &git, + git.find_commit(ordered[base_len - 1]) + .map_err(|error| git_error(error.to_string()))? + .tree_id(), + &policy, + )?; + validate_git()?; + let mut session = if staged.is_empty() { + GitGraphImportSession::begin_span_incremental(repo, limits, &prefix, &policy, &baseline) + } else { + GitGraphImportSession::resume_span_incremental( + repo, limits, &prefix, &staged, &policy, &baseline, + ) + } + .map_err(CliError::from)?; + if stage_hashes.len() == ordered.len() && staged.is_empty() { + return Ok(GitStreamOutcome { + commits: 0, + batches: 0, + state: target_state, + private_publish_seconds: 0.0, + final_proof_promotion_seconds: 0.0, + }); + } + let mut batch = Vec::new(); + let mut bytes = 0usize; + for (index, oid) in ordered.iter().copied().enumerate().skip(stage_hashes.len()) { + let commit = git + .find_commit(oid) + .map_err(|error| git_error(error.to_string()))?; + input_budget(&git, &commit, limits.bytes())?; + let parsed = parse_commit_with_scope(&git, oid, index, &positions, false)?; + let metadata = self.build_git_metadata(&parsed, parsed.is_empty, parsed.is_merge); + let prepared = compiler.compile(&parsed, &policy, &[], metadata)?; + if prepared.payload_bytes() > limits.bytes() { + return Err(git_error( + "single compiled span change exceeds the stream budget", + )); + } + if !batch.is_empty() + && (batch.len() == limits.commits() + || prepared.payload_bytes() > limits.bytes() - bytes) + { + validate_git()?; + session.publish(&batch).map_err(CliError::from)?; + batch.clear(); + bytes = 0; + } + bytes += prepared.payload_bytes(); + batch.push(prepared); + } + if !batch.is_empty() { + validate_git()?; + session.publish(&batch).map_err(CliError::from)?; + } + finish_graph_stream_with_guard(session, &git, &policy, tip, || { + validate_git().map_err( + |error| atomic_repository::RepositoryError::InvalidOperation { + message: format!("Git observation changed before span promotion: {error}"), + }, + ) + }) + } + + pub(crate) fn import_span_incremental_prevalidated( + &self, + repo: &mut Repository, + plan: ProspectiveImportPlan, + limits: GitStreamLimits, + validate_git: F, + ) -> CliResult + where + F: FnMut() -> CliResult<()>, + { + let stream = plan + .stream + .ok_or_else(|| git_error("missing incremental span preflight"))?; + self.import_span_incremental(repo, stream.tip, limits, &stream.ordered, validate_git) + } + fn graph_stream_inputs( &self, tip: Oid, @@ -565,7 +783,12 @@ impl ParallelImporter { .enumerate() .map(|(index, oid)| (oid, index)) .collect(); - let mut session = GitGraphImportSession::begin(repo, limits).map_err(CliError::from)?; + let mut session = if self.options.span_stream { + GitGraphImportSession::begin_span(repo, limits) + } else { + GitGraphImportSession::begin(repo, limits) + } + .map_err(CliError::from)?; let enabled = std::io::stderr().is_terminal() && std::env::var_os("TERM").is_some_and(|term| term != "dumb"); let mut dashboard = rolling_dashboard::Dashboard::start(500, limits.bytes(), enabled); @@ -574,6 +797,9 @@ impl ParallelImporter { state.checked = ordered.len(); }); let mut compiler = Compiler::default(); + if self.options.span_stream { + compiler.spans = Some(Default::default()); + } let mut pending = VecDeque::new(); let (mut next, mut processed, mut pending_bytes) = (0usize, 0usize, 0usize); let started = Instant::now(); @@ -678,8 +904,16 @@ impl ParallelImporter { .enumerate() .map(|(index, oid)| (oid, index)) .collect(); - let mut session = GitGraphImportSession::begin(repo, limits).map_err(CliError::from)?; + let mut session = if self.options.span_stream { + GitGraphImportSession::begin_span(repo, limits) + } else { + GitGraphImportSession::begin(repo, limits) + } + .map_err(CliError::from)?; let mut compiler = Compiler::default(); + if self.options.span_stream { + compiler.spans = Some(Default::default()); + } let mut batch = Vec::new(); let mut bytes = 0usize; let mut staged = 0usize; @@ -731,6 +965,19 @@ fn finish_graph_stream( policy: &ConversionPolicy, tip: Oid, ) -> CliResult { + finish_graph_stream_with_guard(session, git, policy, tip, || Ok(())) +} + +fn finish_graph_stream_with_guard( + session: GitGraphImportSession<'_>, + git: &GitRepository, + policy: &ConversionPolicy, + tip: Oid, + guard: F, +) -> CliResult +where + F: FnOnce() -> Result<(), atomic_repository::RepositoryError>, +{ let tree = git .find_commit(tip) .and_then(|commit| commit.tree()) @@ -747,7 +994,9 @@ fn finish_graph_stream( let expected = import_expected_tree_oid(git, tree.id(), policy)?; let proof = verify_prospective_equivalence(&project, &expected) .map_err(|error| git_error(error.to_string()))?; - session.finish(&project, &proof).map_err(CliError::from) + session + .finish_with_guard(&project, &proof, guard) + .map_err(CliError::from) } #[cfg(test)] @@ -1418,6 +1667,2516 @@ mod tests { (temp, git, previous.unwrap(), files) } + fn assert_imported_authored_ranges( + repo: &Repository, + expected: &[u8], + minimum_owners: usize, + ) -> atomic_core::pristine::span_patch_storage::SpanProjectionId { + use atomic_core::pristine::span_patch_storage::SpanRenderContext; + use atomic_core::pristine::{GraphTxnT, TreeTxnT, ViewTxnT}; + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view(repo.current_view()).unwrap().unwrap(); + let visibility = atomic_repository::graph_visibility_closure(&txn, &view).unwrap(); + let inode = txn.get_inode("hot.txt").unwrap().unwrap(); + let context = SpanRenderContext::new(&txn, repo.change_store(), &visibility).unwrap(); + let state = context.record_state(inode).unwrap().unwrap(); + assert_eq!(state.content, expected); + let mut reconstructed = Vec::new(); + let mut owners = std::collections::BTreeSet::new(); + for range in state.authored_ranges(0..state.content.len()).unwrap() { + let change = repo.load_change(&range.target.owner).unwrap(); + let start = (range.target.start + range.target_start) as usize; + let end = start + range.output.len(); + reconstructed.extend_from_slice(&change.contents[start..end]); + owners.insert(range.target.owner); + } + assert_eq!(reconstructed, state.content); + assert!( + owners.len() >= minimum_owners, + "fixture did not exercise the requested distinct authorship spans" + ); + let mut semantic_bytes = Vec::new(); + let snapshot = state.semantic_snapshot().unwrap(); + let id = snapshot.id; + for line in snapshot.lines { + for token in line.tokens { + let mut token_bytes = Vec::new(); + for fragment in token.authors { + let change = repo.load_change(&fragment.target.owner).unwrap(); + let start = (fragment.target.start + fragment.target_start) as usize; + let end = start + fragment.output.len(); + token_bytes.extend_from_slice(&change.contents[start..end]); + } + assert_eq!(token_bytes, state.content[token.output.clone()]); + semantic_bytes.extend_from_slice(&token_bytes); + } + } + assert_eq!(semantic_bytes, state.content); + id + } + + #[test] + fn span_stream_preserves_each_git_state_and_reopens() { + use atomic_core::pristine::{GitShaIndexTxnT, GraphTxnT}; + + let (_git_dir, git, tip, files) = fixture(16); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + let outcome = importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + assert_eq!(outcome.commits, 16); + let mut commits = Vec::new(); + let mut current = tip; + loop { + commits.push(current); + let commit = git.find_commit(current).unwrap(); + if commit.parent_count() == 0 { + break; + } + current = commit.parent_id(0).unwrap(); + } + commits.reverse(); + let mut expected_closure = Vec::new(); + let mut closures = Vec::new(); + for oid in commits { + let hash = { + let txn = repo.pristine().read_txn().unwrap(); + let id = txn.get_by_git_sha(&oid.to_string()).unwrap().unwrap(); + txn.get_external(id).unwrap().unwrap() + }; + expected_closure.push(hash); + let mut sorted_closure = expected_closure.clone(); + sorted_closure.sort(); + sorted_closure.dedup(); + assert_eq!( + repo.git_commit_closure(&oid.to_string()).unwrap(), + Some(sorted_closure.clone()), + "closure for {oid}" + ); + closures.push((oid, sorted_closure)); + let change = repo.load_change(&hash).unwrap(); + assert!(change.hashed.span.is_some(), "{oid} is not a span change"); + assert!( + change.file_ops().is_empty(), + "Git stream must not synthesize native semantic identity" + ); + let tree = git.find_commit(oid).unwrap().tree().unwrap(); + for path in [ + "hot.txt", + "moved.txt", + "stable.txt", + "nested/deep/empty", + "nested/deep/recreated", + "blob.bin", + ] { + let expected = tree + .get_path(Path::new(path)) + .ok() + .map(|entry| git.find_blob(entry.id()).unwrap().content().to_vec()); + let actual = repo + .get_file_content_after_change_on_view(path, &hash, repo.current_view()) + .unwrap(); + assert_eq!(actual, expected, "{oid} {path}"); + } + } + for (path, (bytes, _)) in &files { + assert_eq!( + repo.get_file_content(path).unwrap().as_deref(), + Some(bytes.as_slice()), + "{path}" + ); + } + assert_imported_authored_ranges(&repo, b"alpha\nrestored\nlast\n", 1); + repo.materialize(repo.require_working_copy_id().unwrap()) + .unwrap(); + for (path, (bytes, _)) in &files { + assert_eq!( + std::fs::read(target.path().join(path)).unwrap(), + *bytes, + "{path}" + ); + } + assert!(repo + .status( + repo.require_working_copy_id().unwrap(), + atomic_repository::StatusOptions::default() + ) + .unwrap() + .is_clean()); + drop(repo); + let reopened = Repository::open(target.path()).unwrap(); + assert_imported_authored_ranges(&reopened, b"alpha\nrestored\nlast\n", 1); + for (oid, closure) in closures { + assert_eq!( + reopened.git_commit_closure(&oid.to_string()).unwrap(), + Some(closure) + ); + } + for (path, (bytes, _)) in &files { + assert_eq!( + reopened.get_file_content(path).unwrap().as_deref(), + Some(bytes.as_slice()), + "{path}" + ); + } + } + + #[test] + fn git_span_authored_ranges_preserve_multiple_change_owners() { + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let snapshot = assert_imported_authored_ranges(&repo, b"alpha\nchanged\nlast\n", 2); + drop(repo); + let reopened = Repository::open(target.path()).unwrap(); + assert_eq!( + assert_imported_authored_ranges(&reopened, b"alpha\nchanged\nlast\n", 2), + snapshot + ); + } + + #[test] + fn span_compiler_replays_imported_prefix_and_matches_fresh_extension() { + use atomic_core::pristine::{GitShaIndexTxnT, GraphTxnT, MutTxnT}; + + let (_git_dir, git, tip, _) = fixture(5); + let mut prefix = tip; + for _ in 0..3 { + prefix = git.find_commit(prefix).unwrap().parent_id(0).unwrap(); + } + let partial_dir = tempfile::tempdir().unwrap(); + let mut partial = Repository::init(partial_dir.path()).unwrap(); + let make_importer = |repo: &Repository| { + ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ) + }; + make_importer(&partial) + .import_graph_stream( + &mut partial, + prefix, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + drop(partial); + let mut partial = Repository::open(partial_dir.path()).unwrap(); + + let full_dir = tempfile::tempdir().unwrap(); + let mut full = Repository::init(full_dir.path()).unwrap(); + let importer = make_importer(&full); + importer + .import_graph_stream( + &mut full, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let (git, policy, ordered) = importer.graph_stream_inputs(tip).unwrap(); + assert_eq!(ordered.len(), 5); + let positions: HashMap<_, _> = ordered + .iter() + .copied() + .enumerate() + .map(|(index, oid)| (oid, index)) + .collect(); + let mut compiler = Compiler { + spans: Some(Default::default()), + ..Default::default() + }; + let partial_txn = partial.pristine().read_txn().unwrap(); + let full_txn = full.pristine().read_txn().unwrap(); + let mut replayed_prefix = Vec::new(); + for (index, oid) in ordered.iter().copied().enumerate() { + let parsed = parse_commit_with_scope(&git, oid, index, &positions, false).unwrap(); + let metadata = importer.build_git_metadata(&parsed, parsed.is_empty, parsed.is_merge); + let replayed = compiler + .compile(&parsed, &policy, &[], metadata) + .unwrap() + .hash(); + let full_id = full_txn.get_by_git_sha(&oid.to_string()).unwrap().unwrap(); + assert_eq!( + full_txn.get_external(full_id).unwrap(), + Some(replayed), + "fresh extension at {oid}" + ); + let existing = partial_txn.get_by_git_sha(&oid.to_string()).unwrap(); + if index < 2 { + assert_eq!( + partial_txn.get_external(existing.unwrap()).unwrap(), + Some(replayed), + "existing prefix at {oid}" + ); + replayed_prefix.push((oid.to_string(), replayed)); + } else { + assert!(existing.is_none(), "replay must not publish {oid}"); + } + } + drop(partial_txn); + drop(full_txn); + partial.create_shared_view("unrelated").unwrap(); + let database = partial_dir.path().join(".atomic/atomic.redb"); + let before = std::fs::read(&database).unwrap(); + partial + .validate_span_git_prefix(partial.current_view(), &replayed_prefix) + .unwrap(); + assert!(partial + .validate_span_git_prefix(partial.current_view(), &[]) + .is_err()); + let mut wrong_hash = replayed_prefix.clone(); + wrong_hash[1].1 = atomic_core::types::Hash::of(b"different change"); + assert!(partial + .validate_span_git_prefix(partial.current_view(), &wrong_hash) + .is_err()); + let mut wrong_order = replayed_prefix.clone(); + wrong_order.reverse(); + assert!(partial + .validate_span_git_prefix(partial.current_view(), &wrong_order) + .is_err()); + let mut unknown = replayed_prefix.clone(); + unknown.push(( + ordered[2].to_string(), + atomic_core::types::Hash::of(b"unimported change"), + )); + assert!(partial + .validate_span_git_prefix(partial.current_view(), &unknown) + .is_err()); + assert!(partial + .validate_span_git_prefix("unrelated", &replayed_prefix) + .is_err()); + assert_eq!( + std::fs::read(database).unwrap(), + before, + "prefix validation must be read-only" + ); + let mut txn = partial.pristine().write_txn().unwrap(); + let id = txn.get_by_git_sha(&replayed_prefix[1].0).unwrap().unwrap(); + txn.put_change_deps(id, &[]).unwrap(); + txn.commit().unwrap(); + assert!( + partial + .validate_span_git_prefix(partial.current_view(), &replayed_prefix) + .is_err(), + "a damaged dependency index must not validate" + ); + } + + #[test] + fn incremental_span_private_stage_promotes_only_after_final_tree_proof() { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{GitShaIndexMutTxnT, GitShaIndexTxnT, MutTxnT}; + + for scenario in ["success", "failed-proof", "abandoned-stage"] { + let (_git_dir, git, tip, files) = fixture(5); + let mut prefix = tip; + for _ in 0..3 { + prefix = git.find_commit(prefix).unwrap().parent_id(0).unwrap(); + } + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + let limits = GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(); + importer + .import_graph_stream(&mut repo, prefix, limits) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + let before_state = repo.get_view_info(repo.current_view()).unwrap().state; + let before_history = repo.effective_history(None).unwrap(); + let before_working = repo.working_copy_record(wc).unwrap(); + + let (git, policy, ordered) = importer.graph_stream_inputs(tip).unwrap(); + let positions: HashMap<_, _> = ordered + .iter() + .copied() + .enumerate() + .map(|(index, oid)| (oid, index)) + .collect(); + let mut compiler = Compiler { + spans: Some(Default::default()), + ..Default::default() + }; + let mut prefix_entries = Vec::new(); + let mut suffix = Vec::new(); + for (index, oid) in ordered.iter().copied().enumerate() { + let parsed = parse_commit_with_scope(&git, oid, index, &positions, false).unwrap(); + let metadata = + importer.build_git_metadata(&parsed, parsed.is_empty, parsed.is_merge); + let prepared = compiler.compile(&parsed, &policy, &[], metadata).unwrap(); + if index < 2 { + prefix_entries.push((oid.to_string(), prepared.hash())); + } else { + suffix.push(prepared); + } + } + let baseline_tree = + import_expected_tree_oid(&git, git.find_commit(prefix).unwrap().tree_id(), &policy) + .unwrap(); + let staged_replay: Vec<_> = ordered[2..] + .iter() + .zip(&suffix) + .map(|(oid, change)| (oid.to_string(), change.hash())) + .collect(); + let wrong_tree = + import_expected_tree_oid(&git, git.find_commit(tip).unwrap().tree_id(), &policy) + .unwrap(); + match GitGraphImportSession::begin_span_incremental( + &mut repo, + limits, + &prefix_entries, + &policy, + &wrong_tree, + ) { + Ok(_) => panic!("wrong Git baseline must be refused"), + Err(error) => assert!( + error.to_string().contains("baseline tree mismatch"), + "{error}" + ), + } + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + before_state + ); + assert_eq!(repo.working_copy_record(wc).unwrap(), before_working); + // An existing global SHA must never be silently rebound by staging. + let next_sha = ordered[2].to_string(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + let existing = txn.get_by_git_sha(&prefix_entries[0].0).unwrap().unwrap(); + txn.put_git_sha(&next_sha, existing).unwrap(); + txn.commit().unwrap(); + } + let before_collision = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let before_global_collision = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + { + let mut session = GitGraphImportSession::begin_span_incremental( + &mut repo, + limits, + &prefix_entries, + &policy, + &baseline_tree, + ) + .unwrap(); + let error = session.publish(&suffix).unwrap_err(); + assert!( + error + .to_string() + .contains("already has a repository identity"), + "{error}" + ); + } + assert_eq!( + repo.operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + before_collision + ); + assert_eq!( + repo.operation_log(OperationScope::Repository, None, false) + .unwrap(), + before_global_collision + ); + { + let mut txn = repo.pristine().write_txn().unwrap(); + assert!(txn.del_git_sha(&next_sha).unwrap()); + txn.commit().unwrap(); + } + let mut session = GitGraphImportSession::begin_span_incremental( + &mut repo, + limits, + &prefix_entries, + &policy, + &baseline_tree, + ) + .unwrap(); + session.publish(&suffix).unwrap(); + if scenario == "abandoned-stage" { + drop(session); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + before_state + ); + assert_eq!(repo.effective_history(None).unwrap(), before_history); + assert_eq!(repo.working_copy_record(wc).unwrap(), before_working); + drop(repo); + let mut reopened = Repository::open(target.path()).unwrap(); + let before_retry = reopened + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let before_global_retry = reopened + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + match GitGraphImportSession::begin_span_incremental( + &mut reopened, + limits, + &prefix_entries, + &policy, + &baseline_tree, + ) { + Ok(_) => { + panic!("abandoned private stage must not be mistaken for a clean baseline") + } + Err(error) => assert!( + error + .to_string() + .contains("unchanged Git-only target and private stage"), + "{error}" + ), + } + assert_eq!( + reopened + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + before_retry + ); + assert_eq!( + reopened + .operation_log(OperationScope::Repository, None, false) + .unwrap(), + before_global_retry + ); + let mut wrong_staged = staged_replay.clone(); + wrong_staged[0].1 = atomic_core::types::Hash::of(b"wrong staged change"); + let refused = GitGraphImportSession::resume_span_incremental( + &mut reopened, + limits, + &prefix_entries, + &wrong_staged, + &policy, + &baseline_tree, + ) + .is_err(); + assert!(refused, "a mismatched staged suffix must not resume"); + assert_eq!( + reopened + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + before_retry + ); + assert_eq!( + reopened + .operation_log(OperationScope::Repository, None, false) + .unwrap(), + before_global_retry + ); + let session = GitGraphImportSession::resume_span_incremental( + &mut reopened, + limits, + &prefix_entries, + &staged_replay, + &policy, + &baseline_tree, + ) + .unwrap(); + let outcome = finish_graph_stream(session, &git, &policy, tip).unwrap(); + assert_eq!(outcome.commits, 3); + for (path, (bytes, _)) in &files { + assert_eq!( + reopened.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + drop(reopened); + let reopened_again = Repository::open(target.path()).unwrap(); + for (path, (bytes, _)) in &files { + assert_eq!( + reopened_again.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + continue; + } + let result = finish_graph_stream( + session, + &git, + &policy, + if scenario == "failed-proof" { + prefix + } else { + tip + }, + ); + if scenario == "failed-proof" { + assert!(result.is_err(), "incorrect final tree must not publish"); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + before_state + ); + assert_eq!(repo.effective_history(None).unwrap(), before_history); + assert_eq!(repo.working_copy_record(wc).unwrap(), before_working); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nchanged\nlast\n".to_vec()) + ); + } else { + let outcome = result.unwrap(); + assert_eq!(outcome.commits, 3); + assert_eq!(repo.effective_history(None).unwrap().len(), 5); + for (path, (bytes, _)) in &files { + assert_eq!( + repo.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + let log = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let original: std::collections::HashSet<_> = before_collision + .entries + .iter() + .map(|entry| entry.operation.id()) + .collect(); + let global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + let appended: Vec<_> = log + .entries + .iter() + .filter(|entry| { + !original.contains(&entry.operation.id()) + && entry.operation.payload().kind + == atomic_core::operation::OperationKind::SynthesizeGit + }) + .collect(); + assert_eq!( + appended.len(), + 2, + "one private batch and one final promotion" + ); + for entry in appended { + assert_eq!( + entry.verification, + atomic_repository::repository::OperationVerificationState::Verified + ); + assert!(global + .entries + .iter() + .any(|other| other.operation.id() == entry.operation.id())); + assert!(!repo + .operation_details(entry.operation.id()) + .unwrap() + .receipts + .is_empty()); + } + } + drop(repo); + let mut reopened = Repository::open(target.path()).unwrap(); + if scenario == "failed-proof" { + assert_eq!( + reopened + .get_view_info(reopened.current_view()) + .unwrap() + .state, + before_state + ); + assert_eq!( + reopened.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nchanged\nlast\n".to_vec()) + ); + let session = GitGraphImportSession::resume_span_incremental( + &mut reopened, + limits, + &prefix_entries, + &staged_replay, + &policy, + &baseline_tree, + ) + .unwrap(); + assert_eq!( + finish_graph_stream(session, &git, &policy, tip) + .unwrap() + .commits, + 3 + ); + for (path, (bytes, _)) in &files { + assert_eq!( + reopened.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + } else { + for (path, (bytes, _)) in &files { + assert_eq!( + reopened.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + } + } + } + + #[test] + fn incremental_span_private_stage_refuses_native_target_changes() { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{GitShaIndexTxnT, GraphTxnT}; + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + let limits = GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(); + importer + .import_graph_stream(&mut repo, tip, limits) + .unwrap(); + let (git, policy, ordered) = importer.graph_stream_inputs(tip).unwrap(); + let prefix: Vec<_> = { + let txn = repo.pristine().read_txn().unwrap(); + ordered + .iter() + .map(|oid| { + let id = txn.get_by_git_sha(&oid.to_string()).unwrap().unwrap(); + (oid.to_string(), txn.get_external(id).unwrap().unwrap()) + }) + .collect() + }; + let tree = import_expected_tree_oid(&git, git.find_commit(tip).unwrap().tree_id(), &policy) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + let path = target.path().join("native.txt"); + std::fs::write(&path, b"native user work\n").unwrap(); + repo.add(wc, "native.txt", Default::default()).unwrap(); + repo.record( + wc, + ChangeHeader::new("native addition"), + atomic_repository::RecordOptions::new().add_path("native.txt"), + ) + .unwrap(); + let before_state = repo.get_view_info(repo.current_view()).unwrap().state; + let before_working = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let before_global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + match GitGraphImportSession::begin_span_incremental( + &mut repo, limits, &prefix, &policy, &tree, + ) { + Ok(_) => panic!("native additions must not enter the restricted incremental path"), + Err(error) => assert!( + error.to_string().contains("unchanged Git-only target"), + "{error}" + ), + } + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + before_state + ); + assert_eq!( + repo.operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + before_working + ); + assert_eq!( + repo.operation_log(OperationScope::Repository, None, false) + .unwrap(), + before_global + ); + assert_eq!(std::fs::read(path).unwrap(), b"native user work\n"); + } + + #[test] + fn incremental_span_private_stage_accepts_two_successive_updates() { + let (_git_dir, git, tip, files) = fixture(7); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + let limits = GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(); + let (git, policy, ordered) = importer.graph_stream_inputs(tip).unwrap(); + assert_eq!(ordered.len(), 7); + importer + .import_graph_stream(&mut repo, ordered[1], limits) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + let ignore = target.path().join(".atomicignore"); + std::fs::write(&ignore, b"# native bootstrap\n").unwrap(); + repo.add(wc, ".atomicignore", Default::default()).unwrap(); + repo.record( + wc, + ChangeHeader::new("Initialize repository"), + atomic_repository::RecordOptions::new().add_path(".atomicignore"), + ) + .unwrap(); + let positions: HashMap<_, _> = ordered + .iter() + .copied() + .enumerate() + .map(|(index, oid)| (oid, index)) + .collect(); + let mut compiler = Compiler { + spans: Some(Default::default()), + ..Default::default() + }; + let mut prefix = Vec::new(); + let mut first = Vec::new(); + let mut second = Vec::new(); + let mut middle_prefix = Vec::new(); + for (index, oid) in ordered.iter().copied().enumerate() { + let parsed = parse_commit_with_scope(&git, oid, index, &positions, false).unwrap(); + let metadata = importer.build_git_metadata(&parsed, parsed.is_empty, parsed.is_merge); + let prepared = compiler.compile(&parsed, &policy, &[], metadata).unwrap(); + if index < 2 { + prefix.push((oid.to_string(), prepared.hash())); + } else if index < 5 { + middle_prefix.push((oid.to_string(), prepared.hash())); + first.push(prepared); + } else { + second.push(prepared); + } + } + let baseline = import_expected_tree_oid( + &git, + git.find_commit(ordered[1]).unwrap().tree_id(), + &policy, + ) + .unwrap(); + let mut session = GitGraphImportSession::begin_span_incremental( + &mut repo, limits, &prefix, &policy, &baseline, + ) + .unwrap(); + session.publish(&first).unwrap(); + assert_eq!( + finish_graph_stream(session, &git, &policy, ordered[4]) + .unwrap() + .commits, + 3 + ); + prefix.extend(middle_prefix); + let middle = import_expected_tree_oid( + &git, + git.find_commit(ordered[4]).unwrap().tree_id(), + &policy, + ) + .unwrap(); + let mut session = GitGraphImportSession::begin_span_incremental( + &mut repo, limits, &prefix, &policy, &middle, + ) + .unwrap(); + session.publish(&second).unwrap(); + assert_eq!( + finish_graph_stream(session, &git, &policy, tip) + .unwrap() + .commits, + 2 + ); + assert_eq!(repo.effective_history(None).unwrap().len(), 8); + assert_eq!( + repo.get_file_content(".atomicignore").unwrap(), + Some(b"# native bootstrap\n".to_vec()) + ); + assert_eq!(std::fs::read(&ignore).unwrap(), b"# native bootstrap\n"); + for (path, (bytes, _)) in &files { + assert_eq!( + repo.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + drop(repo); + let reopened = Repository::open(target.path()).unwrap(); + assert_eq!( + reopened.get_file_content(".atomicignore").unwrap(), + Some(b"# native bootstrap\n".to_vec()) + ); + for (path, (bytes, _)) in &files { + assert_eq!( + reopened.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + } + + #[cfg(feature = "experimental-span-import")] + #[test] + #[serial_test::file_serial] + fn incremental_span_private_stage_accepts_real_cli_bootstrap() { + use crate::commands::Command; + struct RestoreDirectory(std::path::PathBuf); + impl Drop for RestoreDirectory { + fn drop(&mut self) { + std::env::set_current_dir(&self.0).unwrap(); + } + } + + let (source, bare, tip, files) = fixture(5); + let mut prefix = tip; + for _ in 0..3 { + prefix = bare.find_commit(prefix).unwrap().parent_id(0).unwrap(); + } + bare.set_head("refs/heads/main").unwrap(); + let target = tempfile::tempdir().unwrap(); + let git = GitRepository::clone(source.path().to_str().unwrap(), target.path()).unwrap(); + let commit = git.find_commit(prefix).unwrap(); + git.branch("bootstrap", &commit, false).unwrap(); + drop(commit); + git.set_head("refs/heads/bootstrap").unwrap(); + git.checkout_head(Some(git2::build::CheckoutBuilder::new().force())) + .unwrap(); + let _directory = RestoreDirectory(std::env::current_dir().unwrap()); + std::env::set_current_dir(target.path()).unwrap(); + crate::commands::git::import::Import { + span: true, + no_vault: true, + ..Default::default() + } + .run() + .unwrap(); + let ignore = target.path().join(".atomicignore"); + let ignore_bytes = std::fs::read(&ignore).unwrap(); + let user_ignore_bytes = b"# unrecorded user ignore settings\n"; + std::fs::write(&ignore, user_ignore_bytes).unwrap(); + let git_head = git.head().unwrap().target().unwrap(); + let git_index = std::fs::read(git.path().join("index")).unwrap(); + let working_bytes = std::fs::read(target.path().join("hot.txt")).unwrap(); + let mut repo = Repository::open(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + let (git, policy, ordered) = importer.graph_stream_inputs(tip).unwrap(); + let limits = GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(); + let positions: HashMap<_, _> = ordered + .iter() + .copied() + .enumerate() + .map(|(index, oid)| (oid, index)) + .collect(); + let mut compiler = Compiler { + spans: Some(Default::default()), + ..Default::default() + }; + let mut baseline = Vec::new(); + let mut staged = Vec::new(); + for (index, oid) in ordered.iter().copied().take(3).enumerate() { + let parsed = parse_commit_with_scope(&git, oid, index, &positions, false).unwrap(); + let metadata = importer.build_git_metadata(&parsed, parsed.is_empty, parsed.is_merge); + let prepared = compiler.compile(&parsed, &policy, &[], metadata).unwrap(); + if index < 2 { + baseline.push((oid.to_string(), prepared.hash())); + } else { + staged.push(prepared); + } + } + let baseline_tree = + import_expected_tree_oid(&git, git.find_commit(prefix).unwrap().tree_id(), &policy) + .unwrap(); + let mut session = GitGraphImportSession::begin_span_incremental( + &mut repo, + limits, + &baseline, + &policy, + &baseline_tree, + ) + .unwrap(); + session.publish(&staged).unwrap(); + drop(session); + drop(repo); + let mut repo = Repository::open(target.path()).unwrap(); + assert_eq!( + importer + .import_span_incremental(&mut repo, tip, limits, &ordered, || Ok(())) + .unwrap() + .commits, + 3 + ); + let wc = repo.require_working_copy_id().unwrap(); + let before_noop = repo + .operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + None, + false, + ) + .unwrap(); + let before_global_noop = repo + .operation_log( + atomic_core::operation::OperationScope::Repository, + None, + false, + ) + .unwrap(); + assert_eq!( + importer + .import_span_incremental(&mut repo, tip, limits, &ordered, || Ok(())) + .unwrap() + .commits, + 0 + ); + let mut wrong_order = ordered.clone(); + wrong_order.reverse(); + assert!(importer + .import_span_incremental(&mut repo, tip, limits, &wrong_order, || Ok(())) + .is_err()); + assert_eq!( + repo.operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + None, + false + ) + .unwrap(), + before_noop + ); + assert_eq!( + repo.operation_log( + atomic_core::operation::OperationScope::Repository, + None, + false + ) + .unwrap(), + before_global_noop + ); + assert_eq!( + repo.get_file_content(".atomicignore").unwrap(), + Some(ignore_bytes.clone()) + ); + assert_eq!(std::fs::read(&ignore).unwrap(), user_ignore_bytes); + assert_eq!( + std::fs::read(target.path().join("hot.txt")).unwrap(), + working_bytes + ); + assert_eq!(git.head().unwrap().target().unwrap(), git_head); + assert_eq!(std::fs::read(git.path().join("index")).unwrap(), git_index); + for (path, (bytes, _)) in &files { + assert_eq!( + repo.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + drop(repo); + let reopened = Repository::open(target.path()).unwrap(); + assert_eq!( + reopened.get_file_content(".atomicignore").unwrap(), + Some(ignore_bytes) + ); + for (path, (bytes, _)) in &files { + assert_eq!( + reopened.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + } + + #[test] + fn incremental_span_git_observation_failure_keeps_target_and_resumes() { + let (_git_dir, git, tip, files) = fixture(5); + let mut prefix = tip; + for _ in 0..3 { + prefix = git.find_commit(prefix).unwrap().parent_id(0).unwrap(); + } + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + let limits = GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(); + importer + .import_graph_stream(&mut repo, prefix, limits) + .unwrap(); + let (_, _, ordered) = importer.graph_stream_inputs(tip).unwrap(); + let before = repo.get_view_info(repo.current_view()).unwrap().state; + let wc = repo.require_working_copy_id().unwrap(); + let before_working = repo.working_copy_record(wc).unwrap(); + let checks = std::cell::Cell::new(0); + let error = importer + .import_span_incremental(&mut repo, tip, limits, &ordered, || { + let count = checks.get() + 1; + checks.set(count); + if count == 3 { + Err(git_error("injected Git observation drift")) + } else { + Ok(()) + } + }) + .unwrap_err(); + assert_eq!( + checks.get(), + 3, + "final check must be exercised after staging" + ); + assert!( + error.to_string().contains("injected Git observation drift"), + "{error}" + ); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + before + ); + assert_eq!(repo.working_copy_record(wc).unwrap(), before_working); + drop(repo); + let mut reopened = Repository::open(target.path()).unwrap(); + assert_eq!( + reopened + .get_view_info(reopened.current_view()) + .unwrap() + .state, + before + ); + assert_eq!( + importer + .import_span_incremental(&mut reopened, tip, limits, &ordered, || Ok(())) + .unwrap() + .commits, + 3 + ); + for (path, (bytes, _)) in &files { + assert_eq!( + reopened.get_file_content(path).unwrap(), + Some(bytes.clone()), + "{path}" + ); + } + } + + #[test] + fn span_import_without_semantic_history_records_native_edit() { + use atomic_core::operation::OperationScope; + + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let before_state = repo.get_view_info(repo.current_view()).unwrap().state; + let before_ops = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let path = target.path().join("hot.txt"); + std::fs::write(&path, b"alpha\nworking edit\nlast\n").unwrap(); + let outcome = repo + .record( + wc, + ChangeHeader::new("native edit"), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + assert!(outcome.change().file_ops().iter().any(|ops| matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ))); + assert_eq!( + std::fs::read(&path).unwrap(), + b"alpha\nworking edit\nlast\n" + ); + assert_ne!( + repo.get_view_info(repo.current_view()).unwrap().state, + before_state + ); + let after_ops = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + assert!(after_ops.entries.len() > before_ops.entries.len()); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nworking edit\nlast\n".to_vec()) + ); + let (review, _) = atomic_repository::diff_engine::change_file_diffs( + &repo, + outcome.change(), + outcome.hash(), + &Default::default(), + ) + .unwrap(); + let review = review + .iter() + .find(|file| file.new_path == "hot.txt") + .unwrap(); + assert!(review + .hunks + .iter() + .flat_map(|hunk| &hunk.lines) + .any(|line| line.content == "changed")); + assert!(review + .hunks + .iter() + .flat_map(|hunk| &hunk.lines) + .any(|line| line.content == "working edit")); + assert_imported_authored_ranges(&repo, b"alpha\nworking edit\nlast\n", 2); + let error = repo + .materialize_crdt_from_changes(Default::default()) + .unwrap_err(); + assert!( + error + .to_string() + .contains("cannot be materialized as CRDT rows"), + "{error}" + ); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nworking edit\nlast\n".to_vec()) + ); + drop(repo); + let mut repo = Repository::open(target.path()).unwrap(); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nworking edit\nlast\n".to_vec()) + ); + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!(std::fs::read(path).unwrap(), b"alpha\nworking edit\nlast\n"); + } + + #[test] + fn projected_native_span_edits_transfer_and_reopen() { + use atomic_core::pristine::MutTxnT; + let (_git_dir, git, tip, _) = fixture(2); + let source_dir = tempfile::tempdir().unwrap(); + let mut source = Repository::init(source_dir.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: source.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut source, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = source.require_working_copy_id().unwrap(); + source.materialize(wc).unwrap(); + let path = source_dir.path().join("hot.txt"); + for (message, bytes) in [ + ("native one", b"alpha\nfirst native\nlast\n".as_slice()), + ( + "native two", + b"alpha\nfirst native\nsecond native\n".as_slice(), + ), + ] { + std::fs::write(&path, bytes).unwrap(); + source + .record( + wc, + ChangeHeader::new(message), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + } + let expected = b"alpha\nfirst native\nsecond native\n"; + let snapshot = assert_imported_authored_ranges(&source, expected, 3); + let history = source.effective_history(None).unwrap(); + let native = history + .iter() + .map(|entry| entry.hash) + .find(|hash| source.load_change(hash).unwrap().hashed.header.message == "native two") + .unwrap(); + { + use atomic_core::pristine::{ + CapabilityTxnT, GitShaIndexTxnT, GraphTxnT, SPAN_PROJECTED_SEMANTICS_CAPABILITY, + }; + use atomic_core::types::Base32; + let host_dir = tempfile::tempdir().unwrap(); + let mut host = Repository::init(host_dir.path()).unwrap(); + let source_txn = source.pristine().read_txn().unwrap(); + let git_tip_id = source_txn + .get_by_git_sha(&tip.to_string()) + .unwrap() + .unwrap(); + let git_tip_hash = source_txn.get_external(git_tip_id).unwrap().unwrap(); + drop(source_txn); + for entry in &history { + let change = source.load_change(&entry.hash).unwrap(); + if matches!( + change.origin(), + atomic_core::change::ChangeOrigin::GitSynthesized { .. } + ) { + let bytes = + std::fs::read(source.change_store().change_path(&entry.hash)).unwrap(); + host.import_change_bytes(&entry.hash, &bytes).unwrap(); + } + } + let parent = host.current_view().to_string(); + host.create_draft_view("submitted", &parent).unwrap(); + host.insert_change_rec( + &git_tip_hash, + atomic_repository::InsertOptions::default().view("submitted"), + ) + .unwrap(); + assert_eq!( + host.pristine() + .read_txn() + .unwrap() + .required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) + .unwrap(), + None + ); + let first_native = history + .iter() + .map(|entry| entry.hash) + .find(|hash| { + source.load_change(hash).unwrap().hashed.header.message == "native one" + }) + .unwrap(); + let bytes = std::fs::read(source.change_store().change_path(&first_native)).unwrap(); + let base = host + .sandbox_effective_state("submitted") + .unwrap() + .to_base32(); + host.insert_submitted_change("submitted", &base, &first_native, &bytes) + .unwrap() + .unwrap(); + assert_eq!( + host.get_file_content_on_view("hot.txt", "submitted") + .unwrap(), + Some(b"alpha\nfirst native\nlast\n".to_vec()) + ); + assert_eq!( + host.pristine() + .read_txn() + .unwrap() + .required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) + .unwrap(), + Some(SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version()) + ); + } + { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{GitShaIndexMutTxnT, GitShaIndexTxnT}; + let collision_dir = tempfile::tempdir().unwrap(); + let mut collision = Repository::init(collision_dir.path()).unwrap(); + for entry in &history { + let bytes = std::fs::read(source.change_store().change_path(&entry.hash)).unwrap(); + collision.import_change_bytes(&entry.hash, &bytes).unwrap(); + } + let (_, _, commits) = importer.graph_stream_inputs(tip).unwrap(); + let sha = commits[0].to_string(); + let fake = { + let mut txn = collision.pristine().write_txn().unwrap(); + let fake = txn + .register_change(&ContentHash::of(b"foreign Git identity")) + .unwrap(); + txn.put_git_sha(&sha, fake).unwrap(); + txn.commit().unwrap(); + fake + }; + let parent = collision.current_view().to_string(); + collision.create_draft_view("received", &parent).unwrap(); + let wc = collision.require_working_copy_id().unwrap(); + let before = collision.get_view_info("received").unwrap().state; + let working = collision + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let global = collision + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + let error = collision + .insert_change_rec( + &native, + atomic_repository::InsertOptions::default().view("received"), + ) + .unwrap_err(); + assert!( + error.to_string().contains("different repository identity"), + "{error}" + ); + assert_eq!(collision.get_view_info("received").unwrap().state, before); + assert_eq!( + collision + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + working + ); + assert_eq!( + collision + .operation_log(OperationScope::Repository, None, false) + .unwrap(), + global + ); + assert_eq!( + collision + .pristine() + .read_txn() + .unwrap() + .get_by_git_sha(&sha) + .unwrap(), + Some(fake) + ); + } + let receiver_dir = tempfile::tempdir().unwrap(); + let mut receiver = Repository::init(receiver_dir.path()).unwrap(); + { + let mut txn = receiver.pristine().write_txn().unwrap(); + txn.register_change(&ContentHash::of(b"receiver-only identity")) + .unwrap(); + txn.commit().unwrap(); + } + for entry in &history { + let bytes = std::fs::read(source.change_store().change_path(&entry.hash)).unwrap(); + assert_eq!( + receiver.import_change_bytes(&entry.hash, &bytes).unwrap(), + entry.hash + ); + } + let parent = receiver.current_view().to_string(); + receiver.create_draft_view("received", &parent).unwrap(); + receiver + .insert_change_rec( + &native, + atomic_repository::InsertOptions::default().view("received"), + ) + .unwrap(); + { + use atomic_core::pristine::{GitCommitClosureTxnT, GitShaIndexTxnT, GraphTxnT}; + let source_txn = source.pristine().read_txn().unwrap(); + let receiver_txn = receiver.pristine().read_txn().unwrap(); + let (_, _, commits) = importer.graph_stream_inputs(tip).unwrap(); + for oid in commits { + let sha = oid.to_string(); + let original_id = source_txn.get_by_git_sha(&sha).unwrap().unwrap(); + let received_id = receiver_txn.get_by_git_sha(&sha).unwrap().unwrap(); + assert_eq!( + source_txn.get_external(original_id).unwrap(), + receiver_txn.get_external(received_id).unwrap(), + "{sha}" + ); + assert_eq!( + source_txn.get_git_commit_closure(&sha).unwrap(), + receiver_txn.get_git_commit_closure(&sha).unwrap(), + "{sha}" + ); + } + } + let wc = receiver.require_working_copy_id().unwrap(); + std::fs::write(receiver_dir.path().join("untracked.txt"), b"user bytes\n").unwrap(); + receiver.switch_view(wc, "received").unwrap(); + assert_eq!( + std::fs::read(receiver_dir.path().join("hot.txt")).unwrap(), + expected + ); + assert_eq!( + std::fs::read(receiver_dir.path().join("untracked.txt")).unwrap(), + b"user bytes\n" + ); + assert_eq!( + assert_imported_authored_ranges(&receiver, expected, 3), + snapshot + ); + drop(receiver); + let receiver = Repository::open(receiver_dir.path()).unwrap(); + assert_eq!( + receiver.get_file_content("hot.txt").unwrap(), + Some(expected.to_vec()) + ); + assert_eq!( + assert_imported_authored_ranges(&receiver, expected, 3), + snapshot + ); + { + use atomic_core::pristine::{GitCommitClosureTxnT, GitShaIndexTxnT}; + let source_txn = source.pristine().read_txn().unwrap(); + let receiver_txn = receiver.pristine().read_txn().unwrap(); + let (_, _, commits) = importer.graph_stream_inputs(tip).unwrap(); + for oid in commits { + let sha = oid.to_string(); + assert!( + receiver_txn.get_by_git_sha(&sha).unwrap().is_some(), + "{sha}" + ); + assert_eq!( + source_txn.get_git_commit_closure(&sha).unwrap(), + receiver_txn.get_git_commit_closure(&sha).unwrap(), + "{sha}" + ); + } + } + { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{GitShaIndexMutTxnT, GitShaIndexTxnT}; + let (_, _, commits) = importer.graph_stream_inputs(tip).unwrap(); + let sha = commits[0].to_string(); + let mut txn = receiver.pristine().write_txn().unwrap(); + assert!(txn.del_git_sha(&sha).unwrap()); + txn.commit().unwrap(); + let wc = receiver.require_working_copy_id().unwrap(); + let state = receiver + .get_view_info(receiver.current_view()) + .unwrap() + .state; + let working = receiver + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let global = receiver + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + let error = receiver + .insert_change_rec(&native, atomic_repository::InsertOptions::default()) + .unwrap_err(); + assert!(error.to_string().contains("identity is missing"), "{error}"); + assert_eq!( + receiver + .get_view_info(receiver.current_view()) + .unwrap() + .state, + state + ); + assert_eq!( + receiver + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + working + ); + assert_eq!( + receiver + .operation_log(OperationScope::Repository, None, false) + .unwrap(), + global + ); + assert_eq!( + receiver + .pristine() + .read_txn() + .unwrap() + .get_by_git_sha(&sha) + .unwrap(), + None + ); + assert_eq!( + std::fs::read(receiver_dir.path().join("hot.txt")).unwrap(), + expected + ); + } + } + + #[test] + fn projected_native_span_edits_compose_across_views_and_refuse_overlap() { + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let parent = repo.current_view().to_string(); + for name in ["left", "right", "overlap"] { + repo.create_draft_view(name, &parent).unwrap(); + } + let path = target.path().join("hot.txt"); + let options = || { + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.switch_view(wc, "left").unwrap(); + std::fs::write(&path, b"ALPHA\nchanged\nlast\n").unwrap(); + repo.record(wc, ChangeHeader::new("left edit"), options()) + .unwrap(); + repo.switch_view(wc, "right").unwrap(); + std::fs::write(&path, b"alpha\nchanged\nLAST\n").unwrap(); + let right = *repo + .record(wc, ChangeHeader::new("right edit"), options()) + .unwrap() + .hash(); + repo.switch_view(wc, "overlap").unwrap(); + std::fs::write(&path, b"different\nchanged\nlast\n").unwrap(); + let overlap = *repo + .record(wc, ChangeHeader::new("overlapping edit"), options()) + .unwrap() + .hash(); + repo.switch_view(wc, "left").unwrap(); + repo.insert_change_rec( + &right, + atomic_repository::InsertOptions::default().view("left"), + ) + .unwrap(); + let expected = b"ALPHA\nchanged\nLAST\n"; + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(expected.to_vec()) + ); + let before = repo.get_view_info("left").unwrap().state; + let log = repo + .operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + None, + false, + ) + .unwrap(); + let error = repo + .insert_change_rec( + &overlap, + atomic_repository::InsertOptions::default().view("left"), + ) + .unwrap_err(); + assert!( + error.to_string().contains("overlap") || error.to_string().contains("ambiguous"), + "{error}" + ); + assert_eq!(repo.get_view_info("left").unwrap().state, before); + assert_eq!( + repo.operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + None, + false + ) + .unwrap(), + log + ); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(expected.to_vec()) + ); + drop(repo); + let reopened = Repository::open(target.path()).unwrap(); + assert_eq!( + reopened.get_file_content("hot.txt").unwrap(), + Some(expected.to_vec()) + ); + } + + #[test] + fn projected_native_record_undo_keeps_semantic_capability() { + use atomic_core::pristine::{ + CapabilityMutTxnT, CapabilityTxnT, MutTxnT, SPAN_PROJECTED_SEMANTICS_CAPABILITY, + }; + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let path = target.path().join("hot.txt"); + std::fs::write(&path, b"alpha\nworking edit\nlast\n").unwrap(); + let outcome = repo + .record( + wc, + ChangeHeader::new("native edit"), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + let hash = *outcome.hash(); + let log = repo + .operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + None, + false, + ) + .unwrap(); + let recorded_operation = log + .entries + .iter() + .find(|entry| { + entry.operation.payload().kind == atomic_core::operation::OperationKind::Record + && entry.operation.payload().evidence.contains(&hash) + }) + .unwrap() + .operation + .id(); + repo.undo_operation(wc, None).unwrap(); + assert_eq!( + std::fs::read(&path).unwrap(), + b"alpha\nworking edit\nlast\n" + ); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nchanged\nlast\n".to_vec()) + ); + assert_eq!( + repo.pristine() + .read_txn() + .unwrap() + .required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) + .unwrap(), + Some(SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version()) + ); + assert!(repo.load_change(&hash).is_ok()); + { + let mut txn = repo.pristine().write_txn().unwrap(); + assert!(txn + .del_required_capability(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) + .is_err()); + txn.abort().unwrap(); + } + repo.restore_operation(wc, recorded_operation).unwrap(); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nworking edit\nlast\n".to_vec()) + ); + drop(repo); + let repo = Repository::open(target.path()).unwrap(); + assert_eq!( + repo.pristine() + .read_txn() + .unwrap() + .required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) + .unwrap(), + Some(SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version()) + ); + assert_eq!(std::fs::read(path).unwrap(), b"alpha\nworking edit\nlast\n"); + } + + #[test] + fn projected_native_edits_preserve_empty_binary_and_line_endings() { + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let path = target.path().join("hot.txt"); + let cases: &[&[u8]] = &[ + b"", + b"no newline", + b"first\r\nsecond\r\n", + &[0, 255, 1, 0], + b"back to text\n", + b"", + ]; + for (index, bytes) in cases.iter().enumerate() { + std::fs::write(&path, bytes).unwrap(); + repo.record( + wc, + ChangeHeader::new(format!("projected {index}")), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap_or_else(|error| panic!("case {index}: {error}")); + assert_eq!( + repo.get_file_content("hot.txt").unwrap().as_deref(), + Some(*bytes), + "case {index}" + ); + drop(repo); + repo = Repository::open(target.path()).unwrap(); + assert_eq!( + repo.get_file_content("hot.txt").unwrap().as_deref(), + Some(*bytes), + "reopen case {index}" + ); + } + } + + #[test] + fn projected_token_edits_roundtrip_common_sequences() { + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let path = target.path().join("hot.txt"); + let cases: &[&str] = &[ + "let count = 1;\n", + "let total = 1;\n", + "let total = 10;\n", + "pub let total = 10;\n", + "pub let total = 10; // text\n", + "pub let total = 10; // edited\n", + "pub let total = 10;\n", + "let total = 10;\nlet next = 2;\n", + "let total=10;\nlet next = 2;\n", + "let total=10;\r\nlet next = 2;\r\n", + "let total=10;\r\n", + "héllo αβ\n", + "héllo αβ!\n", + "héllo αβ!", + "x", + "", + "x y z\n", + "x z\n", + "x y z\n", + "x zz\n", + ]; + for (index, text) in cases.iter().enumerate() { + std::fs::write(&path, text.as_bytes()).unwrap(); + repo.record( + wc, + ChangeHeader::new(format!("token sequence {index}")), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap_or_else(|error| panic!("case {index} {text:?}: {error}")); + assert_eq!( + repo.get_file_content("hot.txt").unwrap().as_deref(), + Some(text.as_bytes()), + "case {index}" + ); + drop(repo); + repo = Repository::open(target.path()).unwrap(); + assert_eq!( + repo.get_file_content("hot.txt").unwrap().as_deref(), + Some(text.as_bytes()), + "reopen case {index}" + ); + } + } + + #[test] + fn projected_native_edit_retains_unchanged_token_authorship() { + use atomic_core::pristine::span_patch_storage::SpanRenderContext; + use atomic_core::pristine::{TreeTxnT, ViewTxnT}; + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let snapshot = |repo: &Repository| { + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view(repo.current_view()).unwrap().unwrap(); + let visibility = atomic_repository::graph_visibility_closure(&txn, &view).unwrap(); + let inode = txn.get_inode("hot.txt").unwrap().unwrap(); + SpanRenderContext::new(&txn, repo.change_store(), &visibility) + .unwrap() + .record_state(inode) + .unwrap() + .unwrap() + .semantic_snapshot() + .unwrap() + }; + let before = snapshot(&repo); + assert_eq!(before.lines[1].tokens.len(), 2); + let original_word = before.lines[1].tokens[0].id; + let original_newline = before.lines[1].tokens[1].id; + let original_author = before.lines[1].tokens[0].authors[0].target.owner; + std::fs::write( + target.path().join("hot.txt"), + b"alpha\nchanged native\nlast\n", + ) + .unwrap(); + let native = *repo + .record( + wc, + ChangeHeader::new("append token"), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap() + .hash(); + let after = snapshot(&repo); + let tokens = &after.lines[1].tokens; + assert_eq!(tokens.len(), 4); + assert_eq!(tokens[0].id, original_word); + assert_eq!(tokens[0].authors[0].target.owner, original_author); + assert_eq!(tokens[1].authors[0].target.owner, native); + assert_eq!(tokens[2].authors[0].target.owner, native); + assert_eq!(tokens[3].id, original_newline); + assert_eq!(tokens[3].authors[0].target.owner, original_author); + drop(repo); + let repo = Repository::open(target.path()).unwrap(); + let reopened = snapshot(&repo); + assert_eq!(reopened.id, after.id); + let options = || { + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + std::fs::write(target.path().join("hot.txt"), "α\n".as_bytes()).unwrap(); + repo.record(wc, ChangeHeader::new("first scalar"), options()) + .unwrap(); + std::fs::write(target.path().join("hot.txt"), "β\n".as_bytes()).unwrap(); + let beta = *repo + .record(wc, ChangeHeader::new("replace scalar"), options()) + .unwrap() + .hash(); + let unicode = snapshot(&repo); + assert_eq!(unicode.lines[0].tokens.len(), 3); + assert_eq!(unicode.lines[0].tokens[0].authors[0].target.owner, beta); + assert_eq!(unicode.lines[0].tokens[1].authors[0].target.owner, beta); + } + + #[test] + fn projected_token_budget_refuses_before_record_publication() { + use atomic_core::operation::OperationScope; + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let path = target.path().join("hot.txt"); + let first = (0..1100) + .map(|index| format!("old{index} ")) + .collect::(); + let second = (0..1100) + .map(|index| format!("new{index} ")) + .collect::(); + let options = || { + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + std::fs::write(&path, &first).unwrap(); + repo.record(wc, ChangeHeader::new("large first"), options()) + .unwrap(); + let state = repo.get_view_info(repo.current_view()).unwrap().state; + let working = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + std::fs::write(&path, &second).unwrap(); + let error = repo + .record(wc, ChangeHeader::new("too large"), options()) + .unwrap_err(); + assert!(error.to_string().contains("safe memory budget"), "{error}"); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + state + ); + assert_eq!( + repo.operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + working + ); + assert_eq!( + repo.operation_log(OperationScope::Repository, None, false) + .unwrap(), + global + ); + assert_eq!(std::fs::read(&path).unwrap(), second.as_bytes()); + assert_eq!( + repo.get_file_content("hot.txt").unwrap().as_deref(), + Some(first.as_bytes()) + ); + } + + #[test] + fn projected_and_native_crdt_files_can_share_one_record() { + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + std::fs::write( + target.path().join("hot.txt"), + b"alpha\nnew imported edit\nlast\n", + ) + .unwrap(); + std::fs::write(target.path().join("native.txt"), b"native file\n").unwrap(); + repo.add(wc, "native.txt", Default::default()).unwrap(); + let options = || { + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let result = repo + .record(wc, ChangeHeader::new("mixed edit"), options()) + .unwrap(); + assert!(result + .change() + .file_ops() + .iter() + .any(|ops| ops.path() == "hot.txt" + && matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ))); + assert!(result + .change() + .file_ops() + .iter() + .any(|ops| ops.path() == "native.txt" && ops.is_create())); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nnew imported edit\nlast\n".to_vec()) + ); + assert_eq!( + repo.get_file_content("native.txt").unwrap(), + Some(b"native file\n".to_vec()) + ); + drop(repo); + let repo = Repository::open(target.path()).unwrap(); + std::fs::write(target.path().join("native.txt"), b"native file edited\n").unwrap(); + repo.record(wc, ChangeHeader::new("edit native file"), options()) + .unwrap(); + assert_eq!( + repo.get_file_content("native.txt").unwrap(), + Some(b"native file edited\n".to_vec()) + ); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nnew imported edit\nlast\n".to_vec()) + ); + } + + #[cfg(unix)] + #[test] + fn projected_edit_and_mode_change_share_one_record() { + use std::os::unix::fs::PermissionsExt; + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let path = target.path().join("hot.txt"); + std::fs::write(&path, b"alpha\nchanged plus\nlast\n").unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + let result = repo + .record( + wc, + ChangeHeader::new("content and mode"), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + assert_eq!( + result + .change() + .file_ops() + .iter() + .filter(|ops| ops.path() == "hot.txt") + .count(), + 1 + ); + assert!(result.change().hunks().iter().any(|op| matches!(op, atomic_core::change::GraphOp::SetAttr { path, value: atomic_core::change::InodeAttr::Mode(0o755), .. } if path == "hot.txt"))); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nchanged plus\nlast\n".to_vec()) + ); + drop(repo); + let repo = Repository::open(target.path()).unwrap(); + std::fs::remove_file(&path).unwrap(); + repo.materialize(wc).unwrap(); + assert_eq!( + std::fs::read(&path).unwrap(), + b"alpha\nchanged plus\nlast\n" + ); + assert_ne!( + std::fs::metadata(&path).unwrap().permissions().mode() & 0o111, + 0 + ); + } + + #[test] + fn projected_edit_with_wrong_baseline_or_path_refuses_before_publication() { + use atomic_core::change::span_patch::SpanProjectionId; + use atomic_core::crdt::TrunkOp; + use atomic_core::operation::OperationScope; + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + std::fs::write(target.path().join("hot.txt"), b"alpha\nnew content\nlast\n").unwrap(); + let outcome = repo + .record( + wc, + ChangeHeader::new("valid native"), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + let valid = outcome.change(); + let stored_path = repo.change_store().change_path(outcome.hash()); + let original_bytes = std::fs::read(&stored_path).unwrap(); + assert_eq!(repo.save_change(valid).unwrap(), *outcome.hash()); + assert_eq!(std::fs::read(&stored_path).unwrap(), original_bytes); + let mut alternate = valid.clone(); + alternate.unhashed = Some(serde_json::json!({"different": "unhashed metadata"})); + assert_eq!(alternate.hash().unwrap(), *outcome.hash()); + let error = repo.save_change(&alternate).unwrap_err(); + assert!( + error.to_string().contains("refusing to overwrite"), + "{error}" + ); + assert_eq!(std::fs::read(&stored_path).unwrap(), original_bytes); + let marker = valid + .file_ops() + .iter() + .find(|ops| matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. }))) + .unwrap(); + let (file, baseline) = match marker.trunk_op().unwrap() { + TrunkOp::ProjectedSpanEdit { file, baseline } => (file.clone(), *baseline), + _ => unreachable!(), + }; + for (path, baseline, expected) in [ + ( + "hot.txt", + SpanProjectionId(ContentHash::of(b"wrong baseline")), + "baseline", + ), + ("not-hot.txt", baseline, "path"), + ] { + let mut forged = valid.clone(); + forged.hashed.file_ops = vec![atomic_core::change::FileOps::projected_span_edit( + path.into(), + file.clone(), + baseline, + )]; + let hash = repo.save_change(&forged).unwrap(); + let state = repo.get_view_info(repo.current_view()).unwrap().state; + let working = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + let error = repo + .insert_change(&hash, atomic_repository::InsertOptions::default()) + .unwrap_err(); + assert!(error.to_string().contains(expected), "{error}"); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + state + ); + assert_eq!( + repo.operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + working + ); + assert_eq!( + repo.operation_log(OperationScope::Repository, None, false) + .unwrap(), + global + ); + assert_eq!( + std::fs::read(target.path().join("hot.txt")).unwrap(), + b"alpha\nnew content\nlast\n" + ); + } + } + + #[test] + fn projected_record_object_collision_aborts_prepared_operation() { + use atomic_core::operation::{OperationKind, OperationScope}; + use atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY; + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let path = target.path().join("hot.txt"); + std::fs::write(&path, b"alpha\nnew content\nlast\n").unwrap(); + let header = ChangeHeader::new("deterministic collision"); + let dry = repo + .record( + wc, + header.clone(), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(false) + .apply_after_record(false), + ) + .unwrap(); + repo.pristine() + .require_repository_capability(SPAN_PROJECTED_SEMANTICS_CAPABILITY) + .unwrap(); + let mut alternate = dry.change().clone(); + alternate.unhashed = Some(serde_json::json!({"retained": "user metadata"})); + assert_eq!(repo.save_change(&alternate).unwrap(), *dry.hash()); + let stored = repo.change_store().change_path(dry.hash()); + let before_bytes = std::fs::read(&stored).unwrap(); + let before_state = repo.get_view_info(repo.current_view()).unwrap().state; + let error = repo + .record( + wc, + header, + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap_err(); + assert!( + error.to_string().contains("refusing to overwrite"), + "{error}" + ); + assert_eq!(std::fs::read(&stored).unwrap(), before_bytes); + assert_eq!(std::fs::read(&path).unwrap(), b"alpha\nnew content\nlast\n"); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + before_state + ); + let working = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let head = working.entries.iter().find(|entry| entry.is_head).unwrap(); + assert_eq!(head.operation.payload().kind, OperationKind::Recover); + assert_eq!( + head.verification, + atomic_repository::repository::OperationVerificationState::Verified + ); + assert!(!repo + .operation_details(head.operation.id()) + .unwrap() + .receipts + .is_empty()); + let global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + assert!(global + .entries + .iter() + .any(|entry| entry.operation.id() == head.operation.id())); + drop(repo); + let repo = Repository::open(target.path()).unwrap(); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + before_state + ); + assert_eq!(std::fs::read(stored).unwrap(), before_bytes); + } + + #[test] + fn stale_projected_record_refuses_overlap_before_graph_commit() { + use atomic_core::operation::OperationScope; + let (_git_dir, git, tip, _) = fixture(2); + let target = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(target.path()).unwrap(); + let importer = ParallelImporter::new( + &git, + ParallelImportOptions { + graph_only: true, + span_stream: true, + target_view: repo.current_view().into(), + ..Default::default() + }, + ); + importer + .import_graph_stream( + &mut repo, + tip, + GitStreamLimits::new(3, 32 * 1024 * 1024).unwrap(), + ) + .unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + let path = target.path().join("hot.txt"); + std::fs::write(&path, b"alpha\nleft\nlast\n").unwrap(); + let pending = target.path().join("pending.txt"); + std::fs::write(&pending, b"new user file\n").unwrap(); + repo.add(wc, "pending.txt", Default::default()).unwrap(); + let stale = repo + .record( + wc, + ChangeHeader::new("stale edit"), + atomic_repository::RecordOptions::new() + .with_all(true) + .save_to_store(false) + .apply_after_record(false), + ) + .unwrap(); + assert!(stale.change().hunks().iter().any(|op| matches!(op, atomic_core::change::GraphOp::FileAdd { path, .. } if path == "pending.txt"))); + std::fs::write(&path, b"alpha\nright\nlast\n").unwrap(); + repo.record( + wc, + ChangeHeader::new("concurrent edit"), + atomic_repository::RecordOptions::new() + .add_path("hot.txt") + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + assert_eq!(repo.save_change(stale.change()).unwrap(), *stale.hash()); + let before = repo.get_view_info(repo.current_view()).unwrap().state; + let working = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + let journal = target.path().join(".atomic/deferred-tree-ops.json"); + let before_journal = std::fs::read(&journal).ok(); + let error = repo + .write_recorded(&stale, atomic_repository::InsertOptions::default()) + .unwrap_err(); + assert!( + error.to_string().contains("overlap") || error.to_string().contains("ambiguous"), + "{error}" + ); + assert_eq!( + repo.get_view_info(repo.current_view()).unwrap().state, + before + ); + assert_eq!( + repo.operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(), + working + ); + assert_eq!( + repo.operation_log(OperationScope::Repository, None, false) + .unwrap(), + global + ); + assert_eq!(std::fs::read(&path).unwrap(), b"alpha\nright\nlast\n"); + assert_eq!(std::fs::read(&pending).unwrap(), b"new user file\n"); + assert_eq!(std::fs::read(&journal).ok(), before_journal); + assert_eq!( + repo.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nright\nlast\n".to_vec()) + ); + drop(repo); + let reopened = Repository::open(target.path()).unwrap(); + assert_eq!( + reopened.get_file_content("hot.txt").unwrap(), + Some(b"alpha\nright\nlast\n".to_vec()) + ); + } + #[test] fn rolling_sample_matches_direct_import_and_advances_window() { let (_git_dir, git, tip, _) = fixture(32); diff --git a/atomic-cli/src/commands/memory/new.rs b/atomic-cli/src/commands/memory/new.rs index 1639baec..21287891 100644 --- a/atomic-cli/src/commands/memory/new.rs +++ b/atomic-cli/src/commands/memory/new.rs @@ -27,7 +27,7 @@ use crate::error::{CliError, CliResult}; /// counts as text, so a freshly-scaffolded memory lifts to non-empty text and /// its ONLY validate violations are the fillable `attributedTo` + `proof` /// (attest-ready), mirroring intent-new behavior. -const MEMORY_SCAFFOLD: &str = "\ +pub(crate) const MEMORY_SCAFFOLD: &str = "\ :::memory diff --git a/atomic-cli/src/commands/rpc.rs b/atomic-cli/src/commands/rpc.rs index 2cac6641..64133ebe 100644 --- a/atomic-cli/src/commands/rpc.rs +++ b/atomic-cli/src/commands/rpc.rs @@ -319,7 +319,7 @@ pub fn log(args: &crate::commands::log::command::Log) -> CliResult { // -f json, and the styled default format (hash === markers, (tag) // marker, blank line between entries) otherwise. use crate::commands::log::types::{JsonAuthor, JsonLogEntry, LogFormat}; - if matches!(args.format, LogFormat::Json) { + if matches!(args.effective_format(), LogFormat::Json) { let json_entries: Vec = entries .iter() .map(|entry| JsonLogEntry { @@ -349,10 +349,17 @@ pub fn log(args: &crate::commands::log::command::Log) -> CliResult { return Ok(true); } - use crate::commands::{format_hash_with_length, format_timestamp, DEFAULT_HASH_LENGTH}; + use crate::commands::{format_hash_with_length, format_timestamp}; use crate::output::{ author as style_author, hash as style_hash, hint, timestamp as style_timestamp, }; + if matches!( + args.effective_format(), + LogFormat::Short | LogFormat::Oneline + ) { + print!("{}", format_service_compact_log(&entries, args)); + return Ok(true); + } for (i, entry) in entries.iter().enumerate() { // Separator between entries (the old formatter's `i > 0` rule). if i > 0 { @@ -366,7 +373,7 @@ pub fn log(args: &crate::commands::log::command::Log) -> CliResult { .and_then(|h| h.value.clone().try_into().ok()) .unwrap_or([0u8; 32]), ); - let hash_str = format_hash_with_length(&hash, DEFAULT_HASH_LENGTH); + let hash_str = format_hash_with_length(&hash, args.get_hash_length()); let tagged_marker = if entry.is_tagged { " (tag)" } else { "" }; println!( "{} === {} ==={}", @@ -410,6 +417,108 @@ pub fn log(args: &crate::commands::log::command::Log) -> CliResult { Ok(true) } +fn format_service_compact_log( + entries: &[pb::ChangeLogEntry], + args: &crate::commands::log::command::Log, +) -> String { + use crate::commands::log::truncate_string; + use crate::commands::log::types::LogFormat; + use crate::output::{ + author as style_author, hash as style_hash, hint, timestamp as style_timestamp, + }; + let oneline = matches!(args.effective_format(), LogFormat::Oneline); + let width = entries + .iter() + .filter_map(|entry| entry.authors.first()) + .map(|author| author.name.len()) + .max() + .unwrap_or(10) + .min(20); + let mut output = String::new(); + for entry in entries { + let full = wire_hash_base32(&entry.hash); + let hash = full + .get(..args.get_hash_length().min(full.len())) + .unwrap_or(&full); + let message = entry.message.as_deref().unwrap_or("(no message)"); + let first_line = message.lines().next().unwrap_or(message); + if oneline { + let date = entry + .recorded_at + .as_ref() + .and_then(|ts| chrono::DateTime::from_timestamp(ts.seconds, ts.nanos.max(0) as u32)) + .map(|date| date.format("%Y-%m-%d").to_string()) + .unwrap_or_else(|| " ".into()); + let author = entry + .authors + .first() + .map(|author| truncate_string(&author.name, width)) + .unwrap_or_else(|| "(unknown)".into()); + let marker = if entry.is_tagged { "*" } else { " " }; + output.push_str(&format!( + "{}{} {} {:width$} {}\n", + style_hash(hash), + hint(marker), + style_timestamp(&date), + style_author(&author), + first_line + )); + } else { + let marker = if entry.is_tagged { " *" } else { "" }; + output.push_str(&format!( + "{}{} {}\n", + style_hash(hash), + hint(marker), + first_line + )); + } + } + output +} + +#[cfg(test)] +mod service_log_format_tests { + use super::*; + use crate::commands::log::{Log, LogFormat}; + + #[test] + fn compact_formats_honor_full_hash_and_short_alias() { + let hash = Merkle::of(b"log entry"); + let entry = pb::ChangeLogEntry { + hash: Some(pb::Hash { + value: hash.0.to_vec(), + algorithm: pb::HashAlgorithm::Blake3 as i32, + }), + message: Some("first line\nsecond line".into()), + authors: vec![pb::Author { + name: "Alice".into(), + email: None, + }], + is_tagged: true, + ..Default::default() + }; + let full = hash.to_base32(); + let short = Log::new() + .with_format(LogFormat::Short) + .with_full_hash(true); + let rendered = format_service_compact_log(std::slice::from_ref(&entry), &short); + assert_eq!(rendered, format!("{full} * first line\n")); + let mut alias = Log::new(); + alias.short = true; + assert_eq!( + format_service_compact_log(std::slice::from_ref(&entry), &alias), + format!("{} * first line\n", &full[..alias.get_hash_length()]) + ); + let oneline = Log::new() + .with_format(LogFormat::Oneline) + .with_full_hash(true); + let rendered = format_service_compact_log(&[entry], &oneline); + assert!(rendered.starts_with(&format!("{full}* "))); + assert!(rendered.contains("Alice first line")); + assert!(!rendered.contains("second line")); + } +} + /// The Base32 form of a wire `Hash` (the local path's `Hash::to_base32`). fn wire_hash_base32(hash: &Option) -> String { hash.as_ref() @@ -567,6 +676,9 @@ pub fn record(args: &super::record::Record) -> CliResult { (author, None) }; let root = crate::commands::find_repository_root()?; + if args.skip_binary || args.max_size.is_some() { + session.ensure_record_file_options()?; + } // `--all` stages untracked files first — the same client-side // composition `atomic add --all` performs (a Status read + AddFiles), // silently like the local body's pre-record staging. @@ -607,6 +719,8 @@ pub fn record(args: &super::record::Record) -> CliResult { ai_authorship: None, author, identity_name, + skip_binary: args.skip_binary.then_some(true), + max_file_size: args.max_size, }; let response = session.record(request)?; let Some(change) = response.change else { @@ -657,6 +771,20 @@ pub fn record(args: &super::record::Record) -> CliResult { println!(" {file}"); } } + if !response.file_errors.is_empty() { + println!(); + print_warning("Some files had errors:"); + for error in &response.file_errors { + println!(" {}: {}", error.path, error.message); + } + } + if !response.skipped_paths.is_empty() { + println!(); + print_hint(&format!( + "{} skipped (unchanged, empty, binary, or too large)", + crate::commands::record::format_count(response.skipped_paths.len(), "file") + )); + } Ok(true) } @@ -736,6 +864,8 @@ pub fn intent_new(args: &super::intent::new::IntentNew) -> CliResult { model: None, intent_kind: Some(args.kind.clone()), review_target: args.review.clone(), + memory_id: None, + memory_status: None, view: None, expected_snapshot: None, }; @@ -1179,12 +1309,35 @@ pub fn memory_new(args: &super::memory::new::MemoryNew) -> CliResult { let Some(session) = Service::open()? else { return Ok(false); }; + if !atomic_canonical::vocab::MEMORY_KIND.contains(&args.kind.as_str()) { + return Err(CliError::InvalidArgument { + message: format!("unknown memory kind '{}'", args.kind), + }); + } + if !atomic_canonical::vocab::MEMORY_STATUS.contains(&args.status.as_str()) { + return Err(CliError::InvalidArgument { + message: format!("unknown memory status '{}'", args.status), + }); + } + if args + .text + .as_ref() + .is_some_and(|text| text.trim().is_empty()) + { + return Err(CliError::InvalidArgument { + message: "memory text cannot be empty".into(), + }); + } let request = pb::CreateVaultEntityRequest { repository: Some(session.reference.clone()), meta: request_meta(), kind: Kind::Memory as i32, title: String::new(), - body: args.text.clone(), + body: Some( + args.text + .clone() + .unwrap_or_else(|| super::memory::new::MEMORY_SCAFFOLD.to_string()), + ), memory_kind: Some(args.kind.clone()), derived_from: args.derived_from.clone(), about: args.about.clone(), @@ -1193,6 +1346,8 @@ pub fn memory_new(args: &super::memory::new::MemoryNew) -> CliResult { model: None, intent_kind: None, review_target: None, + memory_id: args.id.clone(), + memory_status: (args.status != "active").then_some(args.status.clone()), view: None, expected_snapshot: None, }; @@ -1213,6 +1368,15 @@ pub fn memory_new(args: &super::memory::new::MemoryNew) -> CliResult { println!(" file: {}", entry.vault_path.clone().unwrap_or_default()); println!(" kind: {}", args.kind); println!(" status: {}", args.status); + if !args.derived_from.is_empty() { + println!(" derived from: {} source(s)", args.derived_from.len()); + } + println!(); + if args.text.is_none() { + println!("Edit the :::memory stub, then:"); + } + println!(" atomic memory validate {}", entry.id); + println!(" atomic memory attest {}", entry.id); Ok(true) } @@ -2280,9 +2444,12 @@ fn diff_change(args: &super::diff::command::Diff, change_ref: &str) -> CliResult // The FileOps hunks pad their context from the wire-carried // before-content — the same context the local path reads from // the graph. - super::diff::change_file_diffs_with(&change, &config, |path| { - before_content.get(path).cloned() - })? + super::diff::projected_change_file_diffs_with( + &change, + &config, + |path| Ok(before_content.get(path).cloned()), + |path| Ok(after_content.get(path).cloned()), + )? } else { // Legacy changes carry no file_ops: the diff computes from the // reconstructed before/after content — the SAME builder the @@ -6074,6 +6241,8 @@ pub fn vault_goal_start(args: &super::vault::goal::GoalStart) -> CliResult model: args.model.clone(), intent_kind: None, review_target: None, + memory_id: None, + memory_status: None, view: None, expected_snapshot: None, }; diff --git a/atomic-cli/src/service/mod.rs b/atomic-cli/src/service/mod.rs index 77040acc..40a3339d 100644 --- a/atomic-cli/src/service/mod.rs +++ b/atomic-cli/src/service/mod.rs @@ -257,6 +257,40 @@ impl Service { /// bugs. pub fn status_message(error: tonic::Status) -> CliError { use tonic::Code; + if error.code() == Code::FailedPrecondition + && error + .metadata() + .get("atomic-record-error") + .and_then(|value| value.to_str().ok()) + == Some("file-too-large") + { + let parsed = (|| { + let path = error + .metadata() + .get_bin("atomic-file-path-bin")? + .to_bytes() + .ok()?; + let path = String::from_utf8(path.to_vec()).ok()?; + let size = error + .metadata() + .get("atomic-file-size")? + .to_str() + .ok()? + .parse() + .ok()?; + let limit = error + .metadata() + .get("atomic-file-limit")? + .to_str() + .ok()? + .parse() + .ok()?; + Some(CliError::FileTooLarge { path, size, limit }) + })(); + if let Some(error) = parsed { + return error; + } + } let message = error.message().to_string(); match error.code() { Code::FailedPrecondition @@ -524,6 +558,8 @@ impl Service { } pub fn record(&self, request: pb::RecordRequest) -> CliResult { + let requires_file_options = + request.skip_binary.is_some() || request.max_file_size.is_some(); self.call(move |backend| async move { match backend { Backend::Local(state) => { @@ -533,6 +569,13 @@ impl Service { .map(|response| response.into_inner()) } Backend::Reactor(channel) => { + if requires_file_options { + let capabilities = pb::daemon_service_client::DaemonServiceClient::new(channel.clone()) + .get_capabilities(pb::GetCapabilitiesRequest {}) + .await? + .into_inner(); + require_record_file_options(&capabilities)?; + } let mut mutation = pb::repository_mutation_service_client::RepositoryMutationServiceClient::new(channel); mutation.record(request).await.map(|response| response.into_inner()) @@ -541,6 +584,22 @@ impl Service { }) } + /// Check before `record --all` stages any files on an older Reactor. + pub fn ensure_record_file_options(&self) -> CliResult<()> { + self.call(move |backend| async move { + match backend { + Backend::Local(_) => Ok(()), + Backend::Reactor(channel) => { + let capabilities = pb::daemon_service_client::DaemonServiceClient::new(channel) + .get_capabilities(pb::GetCapabilitiesRequest {}) + .await? + .into_inner(); + require_record_file_options(&capabilities) + } + } + }) + } + pub fn remove_files( &self, request: pb::RemoveFilesRequest, @@ -743,6 +802,8 @@ impl Service { &self, request: pb::CreateVaultEntityRequest, ) -> CliResult { + let requires_memory_options = + request.memory_id.is_some() || request.memory_status.is_some(); self.call(move |backend| async move { match backend { Backend::Local(state) => services_agent::VaultImpl { state } @@ -750,6 +811,14 @@ impl Service { .await .map(|response| response.into_inner()), Backend::Reactor(channel) => { + if requires_memory_options { + let capabilities = + pb::daemon_service_client::DaemonServiceClient::new(channel.clone()) + .get_capabilities(pb::GetCapabilitiesRequest {}) + .await? + .into_inner(); + require_memory_create_options(&capabilities)?; + } let mut vault = pb::vault_service_client::VaultServiceClient::new(channel); vault .create_vault_entity(request) @@ -965,6 +1034,90 @@ impl Service { } } +fn require_memory_create_options(capabilities: &pb::GetCapabilitiesResponse) -> Result<(), Status> { + if capabilities.protocol_version != 2 + || !capabilities + .features + .iter() + .any(|feature| feature == services::VAULT_MEMORY_CREATE_OPTIONS_FEATURE) + { + return Err(Status::failed_precondition( + "the running Reactor does not support explicit memory ID or status; update/restart it before retrying", + )); + } + Ok(()) +} + +fn require_record_file_options(capabilities: &pb::GetCapabilitiesResponse) -> Result<(), Status> { + if capabilities.protocol_version != 2 + || !capabilities + .features + .iter() + .any(|feature| feature == services::RECORD_FILE_OPTIONS_FEATURE) + { + return Err(Status::failed_precondition( + "the running Reactor does not support record size or binary options; update/restart it before retrying", + )); + } + Ok(()) +} + +#[cfg(test)] +mod memory_create_option_tests { + use super::*; + + #[test] + fn explicit_memory_options_require_advertised_capability() { + let mut capabilities = pb::GetCapabilitiesResponse { + protocol_version: 2, + ..Default::default() + }; + assert!(require_memory_create_options(&capabilities).is_err()); + capabilities + .features + .push(services::VAULT_MEMORY_CREATE_OPTIONS_FEATURE.into()); + assert!(require_memory_create_options(&capabilities).is_ok()); + capabilities.protocol_version = 1; + assert!(require_memory_create_options(&capabilities).is_err()); + } + + #[test] + fn record_file_options_require_advertised_capability() { + let mut capabilities = pb::GetCapabilitiesResponse { + protocol_version: 2, + ..Default::default() + }; + assert!(require_record_file_options(&capabilities).is_err()); + capabilities + .features + .push(services::RECORD_FILE_OPTIONS_FEATURE.into()); + assert!(require_record_file_options(&capabilities).is_ok()); + capabilities.protocol_version = 1; + assert!(require_record_file_options(&capabilities).is_err()); + } + + #[test] + fn structured_oversize_status_preserves_utf8_path_and_sizes() { + let mut status = Status::failed_precondition("atomic:CHANGE_REJECTED: file too large"); + status + .metadata_mut() + .insert("atomic-record-error", "file-too-large".parse().unwrap()); + status.metadata_mut().insert_bin( + "atomic-file-path-bin", + tonic::metadata::MetadataValue::from_bytes("data/é.bin".as_bytes()), + ); + status + .metadata_mut() + .insert("atomic-file-size", "123".parse().unwrap()); + status + .metadata_mut() + .insert("atomic-file-limit", "42".parse().unwrap()); + assert!( + matches!(status_message(status), CliError::FileTooLarge { path, size: 123, limit: 42 } if path == "data/é.bin") + ); + } +} + // --------------------------------------------------------------------------- // AttestationService // --------------------------------------------------------------------------- diff --git a/atomic-cli/tests/it/git_shim_workspace_test.rs b/atomic-cli/tests/it/git_shim_workspace_test.rs index 1c0b8368..2690943d 100644 --- a/atomic-cli/tests/it/git_shim_workspace_test.rs +++ b/atomic-cli/tests/it/git_shim_workspace_test.rs @@ -115,6 +115,45 @@ fn preinit_record(native: bool) { f.assert_content("main", "tracked.txt", b"edit\n"); } +#[test] +fn incremental_graph_import_preserves_independent_draft_edit() { + let f = Fixture { + root: TempDir::new().unwrap(), + home: TempDir::new().unwrap(), + }; + let initial = b"app:\n name: TestApp\n version: \"1.0\"\n debug: false\n\ndatabase:\n host: localhost\n port: 5432\n"; + let shared = String::from_utf8(initial.to_vec()) + .unwrap() + .replace("5432", "5433"); + let draft = String::from_utf8(initial.to_vec()) + .unwrap() + .replace("debug: false", "debug: true"); + let merged = shared.replace("debug: false", "debug: true"); + let path = f.root.path().join("config.yml"); + f.git(&["init", "-q", "-b", "main"]); + fs::write(&path, initial).unwrap(); + f.git(&["add", "config.yml"]); + f.git(&["commit", "-qm", "initial"]); + f.atomic(&["git", "import", "--no-vault", "--branch", "main"]); + f.atomic(&["view", "create", "tweak", "--draft", "--parent", "main"]); + f.atomic(&["view", "switch", "tweak", "--force"]); + fs::write(&path, &draft).unwrap(); + f.atomic(&["add", "config.yml"]); + f.atomic(&["record", "-m", "draft debug"]); + f.atomic(&["view", "switch", "main", "--force"]); + fs::write(&path, &shared).unwrap(); + f.git(&["add", "config.yml"]); + f.git(&["commit", "-qm", "upstream port"]); + f.atomic(&["git", "import", "--incremental", "--branch", "main"]); + assert_eq!(fs::read(&path).unwrap(), shared.as_bytes()); + f.assert_content("main", "config.yml", shared.as_bytes()); + f.assert_content("tweak", "config.yml", merged.as_bytes()); + f.atomic(&["view", "switch", "tweak", "--force"]); + assert_eq!(fs::read(&path).unwrap(), merged.as_bytes()); + f.atomic(&["view", "switch", "main", "--force"]); + assert_eq!(fs::read(&path).unwrap(), shared.as_bytes()); +} + #[test] fn preinit_record_all_through_service() { preinit_record(false); diff --git a/atomic-cli/tests/it/git_span_blame_test.rs b/atomic-cli/tests/it/git_span_blame_test.rs new file mode 100644 index 00000000..4bb9288e --- /dev/null +++ b/atomic-cli/tests/it/git_span_blame_test.rs @@ -0,0 +1,393 @@ +//! Span-derived blame must use the original Git changes, not a later editor. + +use std::fs; +use std::path::Path; +use std::process::{Command, Output}; + +fn run(root: &Path, home: &Path, program: &str, args: &[&str]) -> Output { + Command::new(program) + .args(args) + .current_dir(root) + .env("HOME", home) + .env("ATOMIC_HOME", home.join(".atomic")) + .env("ATOMIC_SERVICE", "local") + .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_TERMINAL_PROMPT", "0") + .env_remove("GIT_INDEX_FILE") + .output() + .unwrap() +} + +#[test] +fn incremental_span_import_refuses_alternate_git_index_before_writing() { + let root = tempfile::tempdir().unwrap(); + let home = tempfile::tempdir().unwrap(); + let (root, home) = (root.path(), home.path()); + let atomic = env!("CARGO_BIN_EXE_atomic"); + ok(root, home, "git", &["init", "-q"]); + ok(root, home, "git", &["config", "user.name", "Original"]); + ok( + root, + home, + "git", + &["config", "user.email", "original@example.invalid"], + ); + fs::write(root.join("source.txt"), b"before\n").unwrap(); + ok(root, home, "git", &["add", "source.txt"]); + ok(root, home, "git", &["commit", "-qm", "base"]); + ok( + root, + home, + atomic, + &["git", "import", "--span", "--no-vault"], + ); + fs::write(root.join("source.txt"), b"after\n").unwrap(); + ok(root, home, "git", &["add", "source.txt"]); + ok(root, home, "git", &["commit", "-qm", "next"]); + let index = fs::read(root.join(".git/index")).unwrap(); + let alternate = home.join("alternate-index"); + fs::write(&alternate, &index).unwrap(); + let database = fs::read(root.join(".atomic/atomic.redb")).unwrap(); + let head = fs::read(root.join(".git/HEAD")).unwrap(); + let output = Command::new(atomic) + .args(["git", "import", "--span", "--incremental", "--no-vault"]) + .current_dir(root) + .env("HOME", home) + .env("ATOMIC_HOME", home.join(".atomic")) + .env("ATOMIC_SERVICE", "local") + .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_TERMINAL_PROMPT", "0") + .env("GIT_INDEX_FILE", &alternate) + .output() + .unwrap(); + assert!(!output.status.success()); + let message = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!(message.contains("GIT_INDEX_FILE"), "{message}"); + assert_eq!( + fs::read(root.join(".atomic/atomic.redb")).unwrap(), + database + ); + assert_eq!(fs::read(root.join(".git/HEAD")).unwrap(), head); + assert_eq!(fs::read(root.join(".git/index")).unwrap(), index); + assert_eq!(fs::read(alternate).unwrap(), index); + assert_eq!(fs::read(root.join("source.txt")).unwrap(), b"after\n"); +} + +#[test] +fn incremental_span_import_refuses_hidden_tracked_edits() { + for flag in ["--assume-unchanged", "--skip-worktree"] { + let root = tempfile::tempdir().unwrap(); + let home = tempfile::tempdir().unwrap(); + let (root, home) = (root.path(), home.path()); + let atomic = env!("CARGO_BIN_EXE_atomic"); + ok(root, home, "git", &["init", "-q"]); + ok(root, home, "git", &["config", "user.name", "Original"]); + ok( + root, + home, + "git", + &["config", "user.email", "original@example.invalid"], + ); + fs::write(root.join("source.txt"), b"before\n").unwrap(); + ok(root, home, "git", &["add", "source.txt"]); + ok(root, home, "git", &["commit", "-qm", "base"]); + ok( + root, + home, + atomic, + &["git", "import", "--span", "--no-vault"], + ); + fs::write(root.join("source.txt"), b"committed\n").unwrap(); + ok(root, home, "git", &["add", "source.txt"]); + ok(root, home, "git", &["commit", "-qm", "next"]); + ok(root, home, "git", &["update-index", flag, "source.txt"]); + fs::write(root.join("source.txt"), b"hidden user edit\n").unwrap(); + let status = ok( + root, + home, + "git", + &["status", "--porcelain", "--untracked-files=no"], + ); + assert!( + status.stdout.is_empty(), + "fixture did not hide the edit for {flag}" + ); + let index = fs::read(root.join(".git/index")).unwrap(); + let database = fs::read(root.join(".atomic/atomic.redb")).unwrap(); + let head = fs::read(root.join(".git/HEAD")).unwrap(); + let output = run( + root, + home, + atomic, + &["git", "import", "--span", "--incremental", "--no-vault"], + ); + assert!(!output.status.success(), "{flag} unexpectedly accepted"); + let message = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!( + message.contains("assume-unchanged or skip-worktree"), + "{flag}: {message}" + ); + assert_eq!( + fs::read(root.join(".atomic/atomic.redb")).unwrap(), + database + ); + assert_eq!(fs::read(root.join(".git/HEAD")).unwrap(), head); + assert_eq!(fs::read(root.join(".git/index")).unwrap(), index); + assert_eq!( + fs::read(root.join("source.txt")).unwrap(), + b"hidden user edit\n" + ); + } +} + +fn ok(root: &Path, home: &Path, program: &str, args: &[&str]) -> Output { + let output = run(root, home, program, args); + assert!( + output.status.success(), + "{program} {args:?}: {}", + String::from_utf8_lossy(&output.stderr) + ); + output +} + +#[test] +fn git_span_blame_attributes_each_line_to_original_change_after_incremental_import() { + let root = tempfile::tempdir().unwrap(); + let home = tempfile::tempdir().unwrap(); + let (root, home) = (root.path(), home.path()); + let atomic = env!("CARGO_BIN_EXE_atomic"); + ok(root, home, "git", &["init", "-q"]); + ok(root, home, "git", &["config", "user.name", "Original"]); + ok( + root, + home, + "git", + &["config", "user.email", "original@example.invalid"], + ); + fs::write(root.join("source.txt"), b"alpha\nbeta\n").unwrap(); + ok(root, home, "git", &["add", "source.txt"]); + ok(root, home, "git", &["commit", "-qm", "base"]); + ok( + root, + home, + atomic, + &["git", "import", "--span", "--no-vault"], + ); + ok(root, home, "git", &["config", "user.name", "Editor"]); + ok( + root, + home, + "git", + &["config", "user.email", "editor@example.invalid"], + ); + fs::write(root.join("source.txt"), b"alpha\nchanged\n").unwrap(); + fs::write(root.join("blob.bin"), [0, 1, 255]).unwrap(); + ok(root, home, "git", &["add", "source.txt", "blob.bin"]); + ok(root, home, "git", &["commit", "-qm", "next"]); + ok( + root, + home, + atomic, + &["git", "import", "--span", "--incremental", "--no-vault"], + ); + let log = ok(root, home, atomic, &["log", "--format", "json"]); + let history: serde_json::Value = serde_json::from_slice(&log.stdout).unwrap(); + let hash = |message: &str| { + history + .as_array() + .unwrap() + .iter() + .find(|entry| entry["message"] == message) + .unwrap()["hash"] + .as_str() + .unwrap() + }; + let blame = ok(root, home, atomic, &["blame", "--short", "source.txt"]); + let text = std::str::from_utf8(&blame.stdout).unwrap(); + let lines: Vec<_> = text.lines().collect(); + assert_eq!(lines.len(), 2, "{text}"); + assert!( + lines[0].starts_with(&hash("base")[..12]) + && lines[0].contains("Original") + && lines[0].ends_with("alpha"), + "{text}" + ); + assert!( + lines[1].starts_with(&hash("next")[..12]) + && lines[1].contains("Editor") + && lines[1].ends_with("changed"), + "{text}" + ); + assert_eq!( + fs::read(root.join("source.txt")).unwrap(), + b"alpha\nchanged\n" + ); + let binary_blame = run(root, home, atomic, &["blame", "blob.bin"]); + assert!( + !binary_blame.status.success(), + "binary content must not be fabricated as text blame" + ); + assert_eq!(fs::read(root.join("blob.bin")).unwrap(), [0, 1, 255]); + fs::write( + root.join("source.txt"), + b"temporary before first native record\n", + ) + .unwrap(); + fs::write(root.join("initial-untracked.txt"), b"preserve me\n").unwrap(); + ok(root, home, atomic, &["restore", "--force"]); + assert_eq!( + fs::read(root.join("source.txt")).unwrap(), + b"alpha\nchanged\n" + ); + assert_eq!( + fs::read(root.join("initial-untracked.txt")).unwrap(), + b"preserve me\n" + ); + let head = fs::read(root.join(".git/HEAD")).unwrap(); + fs::write(root.join("source.txt"), b"alpha\nnative edit\n").unwrap(); + ok(root, home, atomic, &["record", "-m", "native edit"]); + let log = ok(root, home, atomic, &["log", "--format", "json"]); + let history: serde_json::Value = serde_json::from_slice(&log.stdout).unwrap(); + let native = history + .as_array() + .unwrap() + .iter() + .find(|entry| entry["message"] == "native edit") + .unwrap()["hash"] + .as_str() + .unwrap(); + let blame = ok(root, home, atomic, &["blame", "--short", "source.txt"]); + let text = std::str::from_utf8(&blame.stdout).unwrap(); + let lines: Vec<_> = text.lines().collect(); + assert_eq!(lines.len(), 2, "{text}"); + assert!(lines[0].starts_with(&hash("base")[..12]), "{text}"); + assert!( + lines[1].starts_with(&native[..12]) + && lines[1].contains(&hash("next")[..12]) + && lines[1].contains("multiple") + && lines[1].ends_with("native edit"), + "{text}" + ); + let diff = ok(root, home, atomic, &["diff", "--no-color", "-c", native]); + let diff = String::from_utf8_lossy(&diff.stdout); + assert!( + diff.contains("-changed") && diff.contains("+native edit"), + "{diff}" + ); + assert_eq!(fs::read(root.join(".git/HEAD")).unwrap(), head); + assert_eq!( + ok(root, home, "git", &["show", ":source.txt"]).stdout, + b"alpha\nnative edit\n" + ); + assert_eq!( + ok(root, home, "git", &["show", ":blob.bin"]).stdout, + [0, 1, 255] + ); + assert_eq!( + fs::read(root.join("source.txt")).unwrap(), + b"alpha\nnative edit\n" + ); + fs::write(root.join("source.txt"), b"discarded draft\n").unwrap(); + fs::write(root.join("user-untracked.txt"), b"keep me\n").unwrap(); + ok(root, home, atomic, &["restore", "--force"]); + assert_eq!( + fs::read(root.join("source.txt")).unwrap(), + b"alpha\nnative edit\n" + ); + assert_eq!( + fs::read(root.join("user-untracked.txt")).unwrap(), + b"keep me\n" + ); + assert_eq!(fs::read(root.join(".git/HEAD")).unwrap(), head); + fs::write(root.join("source.txt"), b"alpha\nnative edit extra\n").unwrap(); + ok(root, home, atomic, &["record", "-m", "append one token"]); + let latest = ok(root, home, atomic, &["log", "--format", "json"]); + let latest_history: serde_json::Value = serde_json::from_slice(&latest.stdout).unwrap(); + let appended = latest_history + .as_array() + .unwrap() + .iter() + .find(|entry| entry["message"] == "append one token") + .unwrap()["hash"] + .as_str() + .unwrap(); + let blame = ok(root, home, atomic, &["blame", "--short", "source.txt"]); + let blame = std::str::from_utf8(&blame.stdout).unwrap(); + let second = blame.lines().nth(1).unwrap(); + assert!( + second.contains(&native[..12]) + && second.contains(&appended[..12]) + && second.contains(&hash("next")[..12]), + "{blame}" + ); + assert!(second.ends_with("native edit extra"), "{blame}"); + fs::write(root.join("blob.bin"), [0, 2, 3, 255]).unwrap(); + ok(root, home, atomic, &["record", "-m", "native binary edit"]); + let log = ok(root, home, atomic, &["log", "--format", "json"]); + let history: serde_json::Value = serde_json::from_slice(&log.stdout).unwrap(); + let binary_change = history + .as_array() + .unwrap() + .iter() + .find(|entry| entry["message"] == "native binary edit") + .unwrap()["hash"] + .as_str() + .unwrap(); + let diff = ok( + root, + home, + atomic, + &["diff", "--no-color", "-c", binary_change], + ); + let diff = String::from_utf8_lossy(&diff.stdout); + assert!(diff.to_ascii_lowercase().contains("binary"), "{diff}"); + assert_eq!(fs::read(root.join("blob.bin")).unwrap(), [0, 2, 3, 255]); + fs::write(root.join("blob.bin"), b"now text\n").unwrap(); + ok(root, home, atomic, &["record", "-m", "binary to text"]); + let blame = ok(root, home, atomic, &["blame", "--short", "blob.bin"]); + assert!(String::from_utf8_lossy(&blame.stdout).contains("now text")); + assert_eq!(fs::read(root.join("blob.bin")).unwrap(), b"now text\n"); + use std::os::unix::fs::PermissionsExt; + fs::write( + root.join("source.txt"), + b"alpha\nnative edit extra executable\n", + ) + .unwrap(); + fs::set_permissions(root.join("source.txt"), fs::Permissions::from_mode(0o755)).unwrap(); + ok(root, home, atomic, &["record", "-m", "edit and chmod"]); + assert_eq!( + fs::read(root.join("source.txt")).unwrap(), + b"alpha\nnative edit extra executable\n" + ); + assert_ne!( + fs::metadata(root.join("source.txt")) + .unwrap() + .permissions() + .mode() + & 0o111, + 0 + ); + assert!(String::from_utf8_lossy( + &ok(root, home, "git", &["ls-files", "--stage", "source.txt"]).stdout + ) + .starts_with("100755 ")); + let compact = ok(root, home, atomic, &["compact", "--json"]); + let report: serde_json::Value = serde_json::from_slice(&compact.stdout).unwrap(); + assert!(report["after_bytes"].as_u64().unwrap() > 0); + let blame = ok(root, home, atomic, &["blame", "--short", "source.txt"]); + assert!(String::from_utf8_lossy(&blame.stdout).contains("native edit extra executable")); + assert_eq!( + fs::read(root.join("source.txt")).unwrap(), + b"alpha\nnative edit extra executable\n" + ); +} diff --git a/atomic-cli/tests/it/main.rs b/atomic-cli/tests/it/main.rs index d98588ff..63dcf3ae 100644 --- a/atomic-cli/tests/it/main.rs +++ b/atomic-cli/tests/it/main.rs @@ -51,6 +51,8 @@ mod git_shim_daily_workflow_regression; mod git_shim_logging_test; mod git_shim_opt_in_test; mod git_shim_workspace_test; +#[cfg(all(unix, feature = "experimental-span-import"))] +mod git_span_blame_test; mod git_transport_cb10b_test; mod git_wip_cb0b_test; mod graph_only_export_cb9b_test; diff --git a/atomic-core/src/apply/file_ops.rs b/atomic-core/src/apply/file_ops.rs index e4122b14..8bbfc0d1 100644 --- a/atomic-core/src/apply/file_ops.rs +++ b/atomic-core/src/apply/file_ops.rs @@ -679,6 +679,12 @@ fn apply_trunk_op( // GraphOp::SetAttr owns persistence; FileOps carries the semantic fact. TrunkOp::SetMode { .. } | TrunkOp::SetKind { .. } => {} + TrunkOp::ProjectedSpanEdit { .. } => { + return Err(PristineError::Inconsistent { + message: "projected span edit requires span-aware validation and application" + .into(), + }) + } } Ok(()) diff --git a/atomic-core/src/change/ops.rs b/atomic-core/src/change/ops.rs index 973b8c93..912e3cc9 100644 --- a/atomic-core/src/change/ops.rs +++ b/atomic-core/src/change/ops.rs @@ -214,6 +214,22 @@ impl FileOps { } } + /// Create a semantic edit against a span projection without fabricating a + /// historical trunk. The repository must validate the baseline against the + /// dependency closure and apply the corresponding span operations. + pub fn projected_span_edit( + path: String, + file: crate::change::span_patch::ExternalFile, + baseline: crate::change::span_patch::SpanProjectionId, + ) -> Self { + Self { + trunk_id: TrunkId::ROOT, + path, + trunk_op: Some(TrunkOp::ProjectedSpanEdit { file, baseline }), + line_ops: Vec::new(), + } + } + /// Returns the trunk ID. #[inline] pub fn trunk_id(&self) -> TrunkId { @@ -268,7 +284,7 @@ impl FileOps { | Some(TrunkOp::SetKind { trunk, .. }) => { ids.insert(trunk.change_id()); } - Some(TrunkOp::Create { .. }) | None => {} + Some(TrunkOp::Create { .. } | TrunkOp::ProjectedSpanEdit { .. }) | None => {} } for line in &self.line_ops { ids.insert(line.branch_id().change_id()); @@ -361,7 +377,7 @@ impl FileOps { | Some(TrunkOp::Undelete { trunk: id }) | Some(TrunkOp::SetMode { trunk: id, .. }) | Some(TrunkOp::SetKind { trunk: id, .. }) => trunk(id, &mut remap)?, - Some(TrunkOp::Create { .. }) | None => {} + Some(TrunkOp::Create { .. } | TrunkOp::ProjectedSpanEdit { .. }) | None => {} } for line in &mut self.line_ops { branch(&mut line.branch_id, &mut remap)?; @@ -475,7 +491,9 @@ impl FileOps { branch_base: u32, leaf_base: u32, ) { - if self.trunk_id.change_id().is_root() { + if self.trunk_id.change_id().is_root() + && !matches!(self.trunk_op, Some(TrunkOp::ProjectedSpanEdit { .. })) + { self.trunk_id = TrunkId::new(self.trunk_id.change_id(), trunk_file_idx); } if let Some(trunk_op) = &mut self.trunk_op { @@ -484,7 +502,7 @@ impl FileOps { TrunkOp::Move { trunk, .. } => Some(trunk), TrunkOp::SetMode { trunk, .. } => Some(trunk), TrunkOp::SetKind { trunk, .. } => Some(trunk), - TrunkOp::Create { .. } => None, + TrunkOp::Create { .. } | TrunkOp::ProjectedSpanEdit { .. } => None, }; if let Some(trunk) = trunk { if trunk.change_id().is_root() { @@ -521,6 +539,7 @@ impl fmt::Display for FileOps { Some(TrunkOp::Undelete { .. }) => "undelete", Some(TrunkOp::SetMode { .. }) => "set-mode", Some(TrunkOp::SetKind { .. }) => "set-kind", + Some(TrunkOp::ProjectedSpanEdit { .. }) => "projected-span-edit", None => "edit", }; write!( @@ -1018,6 +1037,7 @@ impl FileOpsStats { Some(TrunkOp::SetMode { .. }) | Some(TrunkOp::SetKind { .. }) => { stats.files_edited += 1 } + Some(TrunkOp::ProjectedSpanEdit { .. }) => stats.files_edited += 1, None if !file.line_ops().is_empty() => stats.files_edited += 1, None => {} } diff --git a/atomic-core/src/change/span_patch.rs b/atomic-core/src/change/span_patch.rs index 6fa677f7..46e50ee0 100644 --- a/atomic-core/src/change/span_patch.rs +++ b/atomic-core/src/change/span_patch.rs @@ -38,6 +38,11 @@ pub struct ExternalSpan { pub end: u64, } +/// Content-addressed identity of a derived span semantic projection. Unlike a +/// CRDT ID, this does not attribute the projected item to one creating change. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +pub struct SpanProjectionId(pub Hash); + /// A file root or an edit relative to an existing authored span. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum SpanOp { diff --git a/atomic-core/src/crdt/apply/trunk.rs b/atomic-core/src/crdt/apply/trunk.rs index 42f0133a..62c73251 100644 --- a/atomic-core/src/crdt/apply/trunk.rs +++ b/atomic-core/src/crdt/apply/trunk.rs @@ -83,6 +83,10 @@ pub fn apply_trunk_op( // Attribute state is applied by GraphOp::SetAttr; semantic operations // remain in FileOps for review and replay without duplicating storage. TrunkOp::SetMode { .. } | TrunkOp::SetKind { .. } => Ok(()), + TrunkOp::ProjectedSpanEdit { .. } => Err(ApplyError::InvalidContent { + description: "projected span edit requires span-aware validation and application" + .into(), + }), } } diff --git a/atomic-core/src/crdt/trunk.rs b/atomic-core/src/crdt/trunk.rs index 5331679b..8579c4de 100644 --- a/atomic-core/src/crdt/trunk.rs +++ b/atomic-core/src/crdt/trunk.rs @@ -325,6 +325,17 @@ pub enum TrunkOp { /// Add a causal materialized-kind value for this trunk's inode. SetKind { trunk: TrunkId, kind: InodeKind }, + + /// A semantic edit against a span-derived file, whose historical content + /// has no CRDT trunk. The projected baseline is a content-addressed + /// semantic snapshot, not a claim that this change created old tokens. + /// Its corresponding span edits carry the actual content transition. + ProjectedSpanEdit { + /// Portable identity of the affected file. + file: crate::change::span_patch::ExternalFile, + /// Semantic snapshot on the edit's dependency closure. + baseline: crate::change::span_patch::SpanProjectionId, + }, } impl TrunkOp { @@ -333,7 +344,7 @@ impl TrunkOp { /// Returns `None` for `Create` since the ID is assigned later. pub fn trunk_id(&self) -> Option { match self { - TrunkOp::Create { .. } => None, + TrunkOp::Create { .. } | TrunkOp::ProjectedSpanEdit { .. } => None, TrunkOp::Delete { trunk } => Some(*trunk), TrunkOp::Move { trunk, .. } => Some(*trunk), TrunkOp::Undelete { trunk } => Some(*trunk), @@ -374,6 +385,7 @@ impl TrunkOp { TrunkOp::Undelete { .. } => "undelete", TrunkOp::SetMode { .. } => "set-mode", TrunkOp::SetKind { .. } => "set-kind", + TrunkOp::ProjectedSpanEdit { .. } => "projected-span-edit", } } } @@ -395,6 +407,9 @@ impl fmt::Display for TrunkOp { TrunkOp::Undelete { trunk } => write!(f, "undelete {}", trunk), TrunkOp::SetMode { trunk, mode } => write!(f, "set mode of {} to {mode:#o}", trunk), TrunkOp::SetKind { trunk, kind } => write!(f, "set kind of {} to {kind}", trunk), + TrunkOp::ProjectedSpanEdit { file, .. } => { + write!(f, "edit projected span file {:?}", file) + } } } } diff --git a/atomic-core/src/pristine/capability.rs b/atomic-core/src/pristine/capability.rs index a175cf48..de540820 100644 --- a/atomic-core/src/pristine/capability.rs +++ b/atomic-core/src/pristine/capability.rs @@ -20,18 +20,29 @@ pub const SHIM_CUTOVER_CAPABILITY: RepositoryCapability = pub const KG_SEMWEB_CAPABILITY: RepositoryCapability = RepositoryCapability::new("kg-semweb-v1", 1); +/// Required once Git interpretation closures use shared immutable trees. +pub const GIT_CLOSURE_SHARING_CAPABILITY: RepositoryCapability = + RepositoryCapability::new("git-closure-sharing", 1); + /// Repository contains immutable, typed span changes. Older readers must not /// interpret their namespace-only graph hunks as complete file content. /// Version 3 additionally requires portable semantic-owner mappings. pub const SPAN_RANGE_CAPABILITY: RepositoryCapability = RepositoryCapability::new("span-range-graph", 3); +/// Repository contains semantic edits referencing span-derived baselines. +/// Clients must validate those baselines instead of treating them as CRDT IDs. +pub const SPAN_PROJECTED_SEMANTICS_CAPABILITY: RepositoryCapability = + RepositoryCapability::new("span-projected-semantics", 1); + /// Capabilities supported by this Atomic build. pub const SUPPORTED_REPOSITORY_CAPABILITIES: &[RepositoryCapability] = &[ CHANGE_FORMAT_VNEXT_CAPABILITY, SHIM_CUTOVER_CAPABILITY, KG_SEMWEB_CAPABILITY, + GIT_CLOSURE_SHARING_CAPABILITY, SPAN_RANGE_CAPABILITY, + SPAN_PROJECTED_SEMANTICS_CAPABILITY, ]; /// A repository capability and the minimum version required to use it. diff --git a/atomic-core/src/pristine/git_closure.rs b/atomic-core/src/pristine/git_closure.rs new file mode 100644 index 00000000..2def251d --- /dev/null +++ b/atomic-core/src/pristine/git_closure.rs @@ -0,0 +1,490 @@ +//! Exact Git interpretation closures stored in a shared content-addressed trie. +//! Unsorted or duplicate-containing vectors retain the legacy exact encoding. + +use super::{PristineError, PristineResult}; +use crate::types::Hash; + +const MAGIC: &[u8; 8] = b"AGCLT001"; +const DOMAIN: &[u8] = b"atomic:git-interpretation-closure:radix:v1\0"; +const EMPTY: [u8; 32] = [0; 32]; + +fn invalid(message: impl Into) -> PristineError { + PristineError::Serialization { + message: message.into(), + } +} + +fn node_id(node: &[u8; 64]) -> [u8; 32] { + let mut hasher = blake3::Hasher::new(); + hasher.update(DOMAIN); + hasher.update(node); + *hasher.finalize().as_bytes() +} + +pub(crate) fn encode( + closure: &[Hash], + mut intern: impl FnMut(&[u8; 32], &[u8; 64]) -> PristineResult<()>, +) -> PristineResult> { + let count = + u32::try_from(closure.len()).map_err(|_| invalid("Git closure exceeds u32 length"))?; + if !closure + .windows(2) + .all(|pair| pair[0].as_bytes() < pair[1].as_bytes()) + { + let mut bytes = Vec::with_capacity(4 + closure.len() * 32); + bytes.extend_from_slice(&count.to_le_bytes()); + for hash in closure { + bytes.extend_from_slice(hash.as_bytes()); + } + return Ok(bytes); + } + let tree = encode_tree(closure, &mut intern)?; + // 44 bytes is never a valid legacy count + hashes row (4 mod 32). + let mut root = Vec::with_capacity(44); + root.extend_from_slice(MAGIC); + root.extend_from_slice(&count.to_le_bytes()); + root.extend_from_slice(&tree); + Ok(root) +} + +pub(crate) fn is_shared(bytes: &[u8]) -> bool { + bytes.len() == 44 && bytes.starts_with(MAGIC) +} + +fn encode_tree( + hashes: &[Hash], + intern: &mut impl FnMut(&[u8; 32], &[u8; 64]) -> PristineResult<()>, +) -> PristineResult<[u8; 32]> { + if hashes.is_empty() { + return Ok(EMPTY); + } + let mut node = [0u8; 64]; + if hashes.len() == 1 { + node[32..].copy_from_slice(hashes[0].as_bytes()); + } else { + let first = hashes.first().unwrap().as_bytes(); + let last = hashes.last().unwrap().as_bytes(); + let byte = first.iter().zip(last).position(|(a, b)| a != b).unwrap(); + let mask = 0x80u8 >> (first[byte] ^ last[byte]).leading_zeros(); + let split = hashes.partition_point(|hash| hash.as_bytes()[byte] & mask == 0); + let left = encode_tree(&hashes[..split], intern)?; + let right = encode_tree(&hashes[split..], intern)?; + node[..32].copy_from_slice(&left); + node[32..].copy_from_slice(&right); + } + let id = node_id(&node); + if id == EMPTY { + return Err(invalid("Git closure node collides with the empty tree")); + } + intern(&id, &node)?; + Ok(id) +} + +pub(crate) fn decode( + bytes: &[u8], + mut lookup: impl FnMut(&[u8; 32]) -> PristineResult>, +) -> PristineResult> { + if !bytes.starts_with(MAGIC) || bytes.len() % 32 == 4 { + return decode_legacy(bytes); + } + if bytes.len() != 44 { + return Err(invalid( + "Git shared closure root must contain exactly 44 bytes", + )); + } + let count = u32::from_le_bytes(bytes[8..12].try_into().unwrap()); + let root: [u8; 32] = bytes[12..].try_into().unwrap(); + // Never allocate from an untrusted count before checking the tree. + let mut closure = Vec::new(); + if count == 0 && root == EMPTY { + return Ok(closure); + } + let mut stack = vec![(root, 0u16)]; + let mut visited = 0u64; + while let Some((id, depth)) = stack.pop() { + visited += 1; + if id == EMPTY || depth > 256 || visited > 2 * u64::from(count) { + return Err(invalid( + "Git closure tree has invalid structure or declared length", + )); + } + let node = lookup(&id)?.ok_or_else(|| invalid("Git closure tree node is missing"))?; + if node_id(&node) != id { + return Err(invalid( + "Git closure tree node failed content-hash verification", + )); + } + let left: [u8; 32] = node[..32].try_into().unwrap(); + let right: [u8; 32] = node[32..].try_into().unwrap(); + if left == EMPTY { + if closure.len() >= count as usize + || closure + .last() + .is_some_and(|hash: &Hash| hash.as_bytes() >= &right) + { + return Err(invalid( + "Git closure leaves are not strictly ordered or exceed declared length", + )); + } + closure.push(Hash::from_bytes(right)); + } else { + if right == EMPTY { + return Err(invalid("Git closure branch has an empty child")); + } + stack.push((right, depth + 1)); + stack.push((left, depth + 1)); + } + } + if closure.len() != count as usize { + return Err(invalid( + "Git closure tree length differs from its declared length", + )); + } + Ok(closure) +} + +fn decode_legacy(bytes: &[u8]) -> PristineResult> { + let count_bytes = bytes + .get(..4) + .ok_or_else(|| invalid("Git commit closure is truncated"))?; + let count = u32::from_le_bytes(count_bytes.try_into().unwrap()) as u64; + if bytes.len() as u64 != 4 + count * 32 { + return Err(invalid(format!( + "Git commit closure claims {count} hashes but holds {} bytes", + bytes.len() + ))); + } + Ok(bytes[4..] + .as_chunks::<32>() + .0 + .iter() + .map(|hash| Hash::from_bytes(*hash)) + .collect()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::pristine::{ + CapabilityMutTxnT, GitCommitClosureMutTxnT, GitCommitClosureTxnT, MutTxnT, Pristine, + GIT_CLOSURE_NODES, GIT_CLOSURE_SHARING_CAPABILITY, GIT_COMMIT_CLOSURES, + }; + use redb::{ReadableTable, ReadableTableMetadata}; + use std::collections::HashMap; + + fn hash(n: u64) -> Hash { + Hash::of(&n.to_le_bytes()) + } + fn legacy(hashes: &[Hash]) -> Vec { + let mut bytes = (hashes.len() as u32).to_le_bytes().to_vec(); + for hash in hashes { + bytes.extend_from_slice(hash.as_bytes()); + } + bytes + } + + #[test] + fn exact_order_duplicates_empty_and_divergent_sets_roundtrip() { + let mut nodes = HashMap::new(); + for hashes in [ + vec![], + vec![hash(1)], + vec![hash(1), hash(2)], + vec![hash(1), hash(3)], + vec![hash(3), hash(1), hash(1)], + ] { + let root = encode(&hashes, |id, node| { + nodes.insert(*id, *node); + Ok(()) + }) + .unwrap(); + let sorted_unique = hashes.windows(2).all(|p| p[0].as_bytes() < p[1].as_bytes()); + assert_eq!(is_shared(&root), sorted_unique); + if !sorted_unique { + assert_eq!(root, legacy(&hashes)); + } + assert_eq!( + decode(&root, |id| Ok(nodes.get(id).copied())).unwrap(), + hashes + ); + assert_eq!( + decode(&legacy(&hashes), |_| panic!("legacy requested a node")).unwrap(), + hashes + ); + } + assert!(!nodes.is_empty()); + } + + #[quickcheck_macros::quickcheck] + fn shared_and_legacy_encodings_preserve_arbitrary_vectors(values: Vec) -> bool { + let hashes: Vec<_> = values.into_iter().take(256).map(hash).collect(); + let mut nodes = HashMap::new(); + let root = encode(&hashes, |id, node| { + nodes.insert(*id, *node); + Ok(()) + }) + .unwrap(); + if decode(&root, |id| Ok(nodes.get(id).copied())).unwrap() != hashes + || decode(&legacy(&hashes), |_| Ok(None)).unwrap() != hashes + { + return false; + } + let mut sorted = hashes; + sorted.sort(); + sorted.dedup(); + let root = encode(&sorted, |id, node| { + nodes.insert(*id, *node); + Ok(()) + }) + .unwrap(); + is_shared(&root) && decode(&root, |id| Ok(nodes.get(id).copied())).unwrap() == sorted + } + + #[test] + fn invalid_lengths_missing_and_modified_nodes_fail_closed() { + let mut nodes = HashMap::new(); + let mut hashes = vec![hash(1), hash(2)]; + hashes.sort(); + let root = encode(&hashes, |id, node| { + nodes.insert(*id, *node); + Ok(()) + }) + .unwrap(); + assert!(decode(&root, |_| Ok(None)).is_err()); + assert!(decode(&root, |_| Ok(Some([0; 64]))).is_err()); + assert!(decode(&root, |_| Err(invalid("lookup failed"))).is_err()); + for count in [0, 1, 3, u32::MAX] { + let mut bad = root.clone(); + bad[8..12].copy_from_slice(&count.to_le_bytes()); + assert!(decode(&bad, |id| Ok(nodes.get(id).copied())).is_err()); + } + for bad in [ + vec![], + vec![0; 3], + vec![1, 0, 0, 0], + root[..43].to_vec(), + [root.clone(), vec![0]].concat(), + u32::MAX.to_le_bytes().to_vec(), + ] { + assert!(decode(&bad, |id| Ok(nodes.get(id).copied())).is_err()); + } + } + + #[test] + fn content_addressed_but_unordered_duplicate_or_empty_branches_are_rejected() { + let mut nodes = HashMap::new(); + let mut hashes = vec![hash(1), hash(2)]; + hashes.sort(); + let roots: Vec<_> = hashes + .iter() + .map(|hash| { + encode(&[*hash], |id, node| { + nodes.insert(*id, *node); + Ok(()) + }) + .unwrap() + }) + .collect(); + let left: [u8; 32] = roots[0][12..].try_into().unwrap(); + let right: [u8; 32] = roots[1][12..].try_into().unwrap(); + for (a, b) in [(right, left), (left, left), (left, EMPTY)] { + let mut node = [0u8; 64]; + node[..32].copy_from_slice(&a); + node[32..].copy_from_slice(&b); + let id = node_id(&node); + nodes.insert(id, node); + let mut root = MAGIC.to_vec(); + root.extend_from_slice(&2u32.to_le_bytes()); + root.extend_from_slice(&id); + assert!(decode(&root, |id| Ok(nodes.get(id).copied())).is_err()); + } + } + + #[test] + fn sorted_history_shares_subtrees_across_middle_insertions_and_reopen() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("pristine"); + let pristine = Pristine::open(&path).unwrap(); + let mut txn = pristine.write_txn().unwrap(); + let hashes: Vec<_> = (0..256).map(hash).collect(); + let histories: Vec> = (1..=hashes.len()) + .map(|n| { + let mut h = hashes[..n].to_vec(); + h.sort(); + h + }) + .collect(); + for (index, history) in histories.iter().enumerate() { + let sha = format!("{:040x}", index + 1); + txn.put_git_commit_closure(&sha, history).unwrap(); + assert_eq!(txn.get_git_commit_closure(&sha).unwrap().unwrap(), *history); + } + assert!( + txn.txn + .open_table(GIT_CLOSURE_NODES) + .unwrap() + .len() + .unwrap() + < 256 * 16 + ); + assert_eq!( + txn.txn + .open_table(GIT_COMMIT_CLOSURES) + .unwrap() + .len() + .unwrap(), + 256 + ); + txn.commit().unwrap(); + assert!(pristine + .required_repository_capabilities() + .unwrap() + .iter() + .any(|c| c.id == GIT_CLOSURE_SHARING_CAPABILITY.id())); + drop(pristine); + let pristine = Pristine::open_readonly(&path).unwrap(); + let txn = pristine.read_txn().unwrap(); + for (index, history) in histories.iter().enumerate() { + assert_eq!( + txn.get_git_commit_closure(&format!("{:040x}", index + 1)) + .unwrap() + .unwrap(), + *history + ); + } + assert!(!txn.has_git_commit_closure("absent").unwrap()); + } + + #[test] + fn legacy_rows_stay_readable_and_identical_writes_are_noops() { + let dir = tempfile::tempdir().unwrap(); + let pristine = Pristine::open(dir.path().join("pristine")).unwrap(); + let hashes = vec![hash(2), hash(1), hash(1)]; + let bytes = legacy(&hashes); + let mut txn = pristine.write_txn().unwrap(); + txn.txn + .open_table(GIT_COMMIT_CLOSURES) + .unwrap() + .insert("legacy", bytes.as_slice()) + .unwrap(); + txn.put_git_commit_closure("legacy", &hashes).unwrap(); + assert_eq!( + txn.get_git_commit_closure("legacy").unwrap(), + Some(hashes.clone()) + ); + assert!(txn.put_git_commit_closure("legacy", &[hash(3)]).is_err()); + assert_eq!( + txn.txn + .open_table(GIT_COMMIT_CLOSURES) + .unwrap() + .get("legacy") + .unwrap() + .unwrap() + .value(), + bytes + ); + txn.commit().unwrap(); + assert!(!pristine + .required_repository_capabilities() + .unwrap() + .iter() + .any(|c| c.id == GIT_CLOSURE_SHARING_CAPABILITY.id())); + assert_eq!( + pristine + .read_txn() + .unwrap() + .get_git_commit_closure("legacy") + .unwrap(), + Some(hashes) + ); + } + + #[test] + fn shared_idempotence_conflict_empty_and_rollback() { + let dir = tempfile::tempdir().unwrap(); + let pristine = Pristine::open(dir.path().join("pristine")).unwrap(); + let mut txn = pristine.write_txn().unwrap(); + txn.put_git_commit_closure("empty", &[]).unwrap(); + txn.put_git_commit_closure("one", &[hash(1)]).unwrap(); + txn.put_git_commit_closure("one", &[hash(1)]).unwrap(); + assert!(txn.put_git_commit_closure("one", &[hash(2)]).is_err()); + assert_eq!( + txn.get_git_commit_closure("one").unwrap(), + Some(vec![hash(1)]) + ); + assert_eq!( + txn.txn + .open_table(GIT_CLOSURE_NODES) + .unwrap() + .len() + .unwrap(), + 1 + ); + txn.abort().unwrap(); + assert!(!pristine + .required_repository_capabilities() + .unwrap() + .iter() + .any(|c| c.id == GIT_CLOSURE_SHARING_CAPABILITY.id())); + assert!(!pristine + .read_txn() + .unwrap() + .has_git_commit_closure("one") + .unwrap()); + let mut txn = pristine.write_txn().unwrap(); + txn.put_git_commit_closure("empty", &[]).unwrap(); + txn.commit().unwrap(); + assert_eq!( + pristine + .read_txn() + .unwrap() + .get_git_commit_closure("empty") + .unwrap(), + Some(vec![]) + ); + } + + #[test] + fn corrupt_persisted_node_is_rejected_by_both_readers() { + let dir = tempfile::tempdir().unwrap(); + let pristine = Pristine::open(dir.path().join("pristine")).unwrap(); + let mut txn = pristine.write_txn().unwrap(); + txn.put_git_commit_closure("one", &[hash(1)]).unwrap(); + let root = txn + .txn + .open_table(GIT_COMMIT_CLOSURES) + .unwrap() + .get("one") + .unwrap() + .unwrap() + .value() + .to_vec(); + let id: [u8; 32] = root[12..].try_into().unwrap(); + txn.txn + .open_table(GIT_CLOSURE_NODES) + .unwrap() + .insert(&id, &[0u8; 64]) + .unwrap(); + assert!(txn.get_git_commit_closure("one").is_err()); + txn.commit().unwrap(); + assert!(pristine + .read_txn() + .unwrap() + .get_git_commit_closure("one") + .is_err()); + } + + #[test] + fn unsupported_sharing_capability_version_is_fenced_on_reopen() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("pristine"); + let pristine = Pristine::open(&path).unwrap(); + let mut txn = pristine.write_txn().unwrap(); + txn.put_required_capability_exact(GIT_CLOSURE_SHARING_CAPABILITY.id(), 2) + .unwrap(); + txn.commit().unwrap(); + drop(pristine); + assert!(Pristine::open_readonly(&path).is_err()); + assert!(Pristine::open_existing(&path).is_err()); + } +} diff --git a/atomic-core/src/pristine/merge.rs b/atomic-core/src/pristine/merge.rs index 9da8dfc4..1aaf4d09 100644 --- a/atomic-core/src/pristine/merge.rs +++ b/atomic-core/src/pristine/merge.rs @@ -18,8 +18,8 @@ use redb::{ use super::error::{PristineError, PristineResult}; use super::schema::{ - check_schema_version, table_names, visit_tables, TableVisitor, LEGACY_DIR_KEY, RETIRED_TABLES, - SCHEMA_VERSION, SCHEMA_VERSION_KEY, + check_schema_version, table_names, visit_tables, TableVisitor, BASE_SCHEMA_VERSION, + LEGACY_DIR_KEY, RETIRED_TABLES, SCHEMA_VERSION_KEY, }; use super::tables::ATOMIC_META; use super::txn::{open_database, require_supported_repository_capabilities}; @@ -147,7 +147,14 @@ impl LegacyDatabases { let write = merged.begin_write()?; { let mut meta = write.open_table(ATOMIC_META)?; - meta.insert(SCHEMA_VERSION_KEY, SCHEMA_VERSION.to_le_bytes().as_slice())?; + // Preserve a source's higher required schema; ordinary legacy + // repositories stay on the baseline rather than being upgraded + // merely because this build understands projected semantics. + let version = meta + .get(SCHEMA_VERSION_KEY)? + .map(|value| value.value().to_vec()) + .unwrap_or_else(|| BASE_SCHEMA_VERSION.to_le_bytes().to_vec()); + meta.insert(SCHEMA_VERSION_KEY, version.as_slice())?; meta.insert(LEGACY_DIR_KEY, legacy_dir.as_bytes())?; for (key, digest) in source_digests { meta.insert(key.as_str(), digest.as_slice())?; @@ -155,6 +162,7 @@ impl LegacyDatabases { } super::build_metadata::record_migration(&write)?; write.commit()?; + check_schema_version(&merged.begin_read()?)?; Ok(report) } diff --git a/atomic-core/src/pristine/mod.rs b/atomic-core/src/pristine/mod.rs index 65813849..d57cca6c 100644 --- a/atomic-core/src/pristine/mod.rs +++ b/atomic-core/src/pristine/mod.rs @@ -155,6 +155,7 @@ mod bindings; pub mod build_metadata; mod capability; mod error; +mod git_closure; pub(crate) mod inode_graph; pub mod merge; mod path_claim; @@ -178,8 +179,9 @@ pub use attribute::{ pub use bindings::{BindingMutTxnT, BindingStoreOutcome, BindingTxnT}; pub use capability::{ RepositoryCapability, RequiredRepositoryCapability, UnsupportedRepositoryCapability, - CHANGE_FORMAT_VNEXT_CAPABILITY, REQUIRED_CAPABILITY_PREFIX, SHIM_CUTOVER_CAPABILITY, - SPAN_RANGE_CAPABILITY, SUPPORTED_REPOSITORY_CAPABILITIES, + CHANGE_FORMAT_VNEXT_CAPABILITY, GIT_CLOSURE_SHARING_CAPABILITY, REQUIRED_CAPABILITY_PREFIX, + SHIM_CUTOVER_CAPABILITY, SPAN_PROJECTED_SEMANTICS_CAPABILITY, SPAN_RANGE_CAPABILITY, + SUPPORTED_REPOSITORY_CAPABILITIES, }; pub use error::{PristineError, PristineResult}; mod file_index_v2; diff --git a/atomic-core/src/pristine/schema.rs b/atomic-core/src/pristine/schema.rs index 0aacb340..b3e2e62e 100644 --- a/atomic-core/src/pristine/schema.rs +++ b/atomic-core/src/pristine/schema.rs @@ -14,9 +14,15 @@ use crate::crdt::tables as crdt; use crate::pristine::error::{PristineError, PristineResult}; use crate::pristine::tables::*; -/// Schema version written by this build. Opening a database stamped with a -/// newer version fails instead of misreading tables this build does not know. -pub const SCHEMA_VERSION: u64 = 1; +/// Highest schema version understood by this build. +pub const SCHEMA_VERSION: u64 = 2; + +/// New ordinary repositories remain readable by schema-1 clients. +pub const BASE_SCHEMA_VERSION: u64 = 1; + +/// Schema fence for span-projected semantic changes. Older maintenance tools +/// that do not check capability rows do check the schema before compacting. +pub const PROJECTED_SEMANTICS_SCHEMA_VERSION: u64 = 2; /// [`ATOMIC_META`] key for the schema version (little-endian u64). pub const SCHEMA_VERSION_KEY: &str = "schema_version"; @@ -67,6 +73,14 @@ pub(crate) fn visit_tables(visitor: &mut impl TableVisitor) -> PristineResult<() visitor.multimap(POSITION_ATTRS)?; visitor.multimap(INODE_ATTRS)?; + // Span-backed content indexes are part of the same immutable repository + // state and must be copied and checked during legacy database merging. + visitor.table(super::span_patch_storage::FILES)?; + visitor.table(super::span_patch_storage::ROOTS)?; + visitor.table(super::span_patch_storage::SPANS)?; + visitor.multimap(super::span_patch_storage::EDITS)?; + visitor.table(super::span_patch_storage::APPLIED)?; + // Views visitor.table(VIEWS)?; visitor.table(WORKING_COPIES)?; @@ -105,6 +119,7 @@ pub(crate) fn visit_tables(visitor: &mut impl TableVisitor) -> PristineResult<() visitor.table(TAG_NAME_INDEX)?; visitor.table(GIT_SHA_INDEX)?; visitor.table(GIT_COMMIT_CLOSURES)?; + visitor.table(GIT_CLOSURE_NODES)?; visitor.table(BRIDGE_EVENT_CAPTURES)?; visitor.table(BRIDGE_EVENT_CAPTURE_ANCHORS)?; visitor.table(BRIDGE_REF_CAPTURE_TOKENS)?; @@ -202,39 +217,100 @@ pub(crate) fn table_names() -> Vec { names.0 } -/// Record [`SCHEMA_VERSION`] unless the database already carries a version. +/// Record the baseline schema unless the database already carries a version. pub(crate) fn stamp_schema_version(txn: &WriteTransaction) -> PristineResult<()> { let mut meta = txn.open_table(ATOMIC_META)?; if meta.get(SCHEMA_VERSION_KEY)?.is_none() { - meta.insert(SCHEMA_VERSION_KEY, SCHEMA_VERSION.to_le_bytes().as_slice())?; + meta.insert( + SCHEMA_VERSION_KEY, + BASE_SCHEMA_VERSION.to_le_bytes().as_slice(), + )?; + } + Ok(()) +} + +/// Raise the schema fence in the same transaction as the projected-semantics +/// capability. This is monotonic: no operation may downgrade it later. +pub(crate) fn require_projected_semantics_schema(txn: &WriteTransaction) -> PristineResult<()> { + let mut meta = txn.open_table(ATOMIC_META)?; + let found = meta + .get(SCHEMA_VERSION_KEY)? + .map(|stored| { + <[u8; 8]>::try_from(stored.value()) + .map(u64::from_le_bytes) + .map_err(|_| PristineError::Inconsistent { + message: "schema version in atomic_meta is not 8 bytes".to_string(), + }) + }) + .transpose()? + .unwrap_or(BASE_SCHEMA_VERSION); + if found > SCHEMA_VERSION { + return Err(PristineError::UnsupportedSchema { + found, + supported: SCHEMA_VERSION, + }); + } + if found < PROJECTED_SEMANTICS_SCHEMA_VERSION { + meta.insert( + SCHEMA_VERSION_KEY, + PROJECTED_SEMANTICS_SCHEMA_VERSION.to_le_bytes().as_slice(), + )?; } Ok(()) } /// Fail if the database was stamped by a newer schema than this build reads. pub fn check_schema_version(txn: &ReadTransaction) -> PristineResult<()> { - let meta = match txn.open_table(ATOMIC_META) { - Ok(meta) => meta, - Err(TableError::TableDoesNotExist(_)) => return Ok(()), + let found = match txn.open_table(ATOMIC_META) { + Ok(meta) => match meta.get(SCHEMA_VERSION_KEY)? { + Some(stored) => <[u8; 8]>::try_from(stored.value()) + .map(u64::from_le_bytes) + .map_err(|_| PristineError::Inconsistent { + message: "schema version in atomic_meta is not 8 bytes".to_string(), + })?, + None => BASE_SCHEMA_VERSION, + }, + Err(TableError::TableDoesNotExist(_)) => BASE_SCHEMA_VERSION, Err(error) => return Err(error.into()), }; - let Some(stored) = meta.get(SCHEMA_VERSION_KEY)? else { - return Ok(()); - }; - let found = <[u8; 8]>::try_from(stored.value()) - .map(u64::from_le_bytes) - .map_err(|_| PristineError::Inconsistent { - message: "schema version in atomic_meta is not 8 bytes".to_string(), - })?; if found > SCHEMA_VERSION { return Err(PristineError::UnsupportedSchema { found, supported: SCHEMA_VERSION, }); } + let key = format!( + "{}{}", + crate::pristine::REQUIRED_CAPABILITY_PREFIX, + crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id() + ); + let projected = match txn.open_table(PRISTINE_META) { + Ok(table) => table.get(key.as_str())?.map(|value| value.value()), + Err(TableError::TableDoesNotExist(_)) => None, + Err(error) => return Err(error.into()), + }; + if found >= PROJECTED_SEMANTICS_SCHEMA_VERSION { + if projected.is_none_or(|version| { + version < crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version() + }) { + return Err(PristineError::Inconsistent { + message: "projected schema is missing its required semantic capability".into(), + }); + } + } else if projected.is_some() { + return Err(PristineError::Inconsistent { + message: "projected semantic capability is missing its required schema fence".into(), + }); + } Ok(()) } +/// Refuse requirements unknown to this build without opening a writable +/// database handle. Physical maintenance must perform this check too. +pub fn check_required_capabilities(txn: &ReadTransaction) -> PristineResult<()> { + super::txn::require_supported_repository_capabilities(txn) +} + #[cfg(test)] mod tests { use std::collections::BTreeSet; @@ -254,6 +330,7 @@ mod tests { fn registry_lists_every_table_definition() { let defined: BTreeSet<_> = defined_names(include_str!("tables.rs")) .chain(defined_names(include_str!("../crdt/tables.rs"))) + .chain(defined_names(include_str!("span_patch_storage.rs"))) .collect(); let registered = table_names(); let unique: BTreeSet<_> = registered.iter().map(String::as_str).collect(); @@ -313,6 +390,134 @@ mod tests { check_schema_version(&txn).unwrap(); let meta = txn.open_table(ATOMIC_META).unwrap(); let stored = meta.get(SCHEMA_VERSION_KEY).unwrap().unwrap(); - assert_eq!(stored.value(), SCHEMA_VERSION.to_le_bytes().as_slice()); + assert_eq!(stored.value(), BASE_SCHEMA_VERSION.to_le_bytes().as_slice()); + } + + #[test] + fn projected_schema_fence_is_transactional_and_monotonic() { + let dir = tempfile::tempdir().unwrap(); + let db = redb::Database::create(dir.path().join("db")).unwrap(); + let txn = db.begin_write().unwrap(); + stamp_schema_version(&txn).unwrap(); + txn.commit().unwrap(); + let txn = db.begin_write().unwrap(); + require_projected_semantics_schema(&txn).unwrap(); + txn.abort().unwrap(); + let read = db.begin_read().unwrap(); + assert_eq!( + read.open_table(ATOMIC_META) + .unwrap() + .get(SCHEMA_VERSION_KEY) + .unwrap() + .unwrap() + .value(), + BASE_SCHEMA_VERSION.to_le_bytes().as_slice() + ); + drop(read); + let txn = db.begin_write().unwrap(); + require_projected_semantics_schema(&txn).unwrap(); + txn.open_table(PRISTINE_META) + .unwrap() + .insert( + format!( + "{}{}", + crate::pristine::REQUIRED_CAPABILITY_PREFIX, + crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id() + ) + .as_str(), + crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version(), + ) + .unwrap(); + stamp_schema_version(&txn).unwrap(); + txn.commit().unwrap(); + let read = db.begin_read().unwrap(); + check_schema_version(&read).unwrap(); + assert_eq!( + read.open_table(ATOMIC_META) + .unwrap() + .get(SCHEMA_VERSION_KEY) + .unwrap() + .unwrap() + .value(), + PROJECTED_SEMANTICS_SCHEMA_VERSION.to_le_bytes().as_slice() + ); + } + + #[test] + fn projected_schema_without_semantic_capability_is_rejected() { + let dir = tempfile::tempdir().unwrap(); + let db = redb::Database::create(dir.path().join("db")).unwrap(); + let txn = db.begin_write().unwrap(); + require_projected_semantics_schema(&txn).unwrap(); + txn.commit().unwrap(); + let error = check_schema_version(&db.begin_read().unwrap()).unwrap_err(); + assert!(error.to_string().contains("capability"), "{error}"); + } + + #[test] + fn projected_capability_without_schema_fence_is_rejected() { + let dir = tempfile::tempdir().unwrap(); + let db = redb::Database::create(dir.path().join("db")).unwrap(); + let txn = db.begin_write().unwrap(); + let key = format!( + "{}{}", + crate::pristine::REQUIRED_CAPABILITY_PREFIX, + crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id() + ); + txn.open_table(PRISTINE_META) + .unwrap() + .insert(key.as_str(), 1) + .unwrap(); + txn.commit().unwrap(); + let error = check_schema_version(&db.begin_read().unwrap()).unwrap_err(); + assert!(error.to_string().contains("schema fence"), "{error}"); + } + + #[test] + fn projected_exact_capability_lease_cannot_remove_or_lower_the_fence() { + use crate::pristine::{ + CapabilityMutTxnT, CapabilityTxnT, MutTxnT, Pristine, + SPAN_PROJECTED_SEMANTICS_CAPABILITY, + }; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("db"); + let pristine = Pristine::open(&path).unwrap(); + let capability = SPAN_PROJECTED_SEMANTICS_CAPABILITY; + let mut txn = pristine.write_txn().unwrap(); + assert!(txn + .put_required_capability_exact(capability.id(), 0) + .is_err()); + txn.commit().unwrap(); + assert_eq!( + pristine + .read_txn() + .unwrap() + .required_capability_version(capability.id()) + .unwrap(), + None + ); + pristine.require_repository_capability(capability).unwrap(); + let mut txn = pristine.write_txn().unwrap(); + assert!(txn + .put_required_capability_exact(capability.id(), 0) + .is_err()); + txn.commit().unwrap(); + assert_eq!( + pristine + .read_txn() + .unwrap() + .required_capability_version(capability.id()) + .unwrap(), + Some(capability.minimum_version()) + ); + let mut txn = pristine.write_txn().unwrap(); + txn.put_required_capability_exact(capability.id(), 2) + .unwrap(); + assert!(txn + .put_required_capability_exact(capability.id(), 1) + .is_err()); + txn.abort().unwrap(); + drop(pristine); + Pristine::open_readonly_without_recovery(&path).unwrap(); } } diff --git a/atomic-core/src/pristine/span_patch_storage.rs b/atomic-core/src/pristine/span_patch_storage.rs index 826eac70..3536280d 100644 --- a/atomic-core/src/pristine/span_patch_storage.rs +++ b/atomic-core/src/pristine/span_patch_storage.rs @@ -8,6 +8,7 @@ use super::{ CachedGraphTxn, EffectiveProjectionClosure, GraphTxnT, ReadTxn, ViewMembershipSet, ViewTxnT, WriteTxn, }; +pub use crate::change::span_patch::SpanProjectionId; use crate::change::span_patch::{ExternalFile, ExternalSpan, SpanOp, SpanPatch}; use crate::change::{Change, ChangeStore}; use crate::types::{Hash, Inode, NodeId}; @@ -17,12 +18,15 @@ use redb::{MultimapTableDefinition, ReadableMultimapTable, ReadableTable, TableD use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::sync::Arc; -const FILES: TableDefinition<&[u8; 36], u64> = TableDefinition::new("span_patch_files_v1"); -const ROOTS: TableDefinition = TableDefinition::new("span_patch_roots_v1"); -const SPANS: TableDefinition<&[u8; 32], u8> = TableDefinition::new("span_patch_nodes_v1"); -const EDITS: MultimapTableDefinition<&[u8; 32], &[u8; 40]> = +pub(super) const FILES: TableDefinition<&[u8; 36], u64> = + TableDefinition::new("span_patch_files_v1"); +pub(super) const ROOTS: TableDefinition = + TableDefinition::new("span_patch_roots_v1"); +pub(super) const SPANS: TableDefinition<&[u8; 32], u8> = + TableDefinition::new("span_patch_nodes_v1"); +pub(super) const EDITS: MultimapTableDefinition<&[u8; 32], &[u8; 40]> = MultimapTableDefinition::new("span_patch_edits_v1"); -const APPLIED: TableDefinition = TableDefinition::new("span_patch_applied_v1"); +pub(super) const APPLIED: TableDefinition = TableDefinition::new("span_patch_applied_v1"); fn file_key(file: &ExternalFile) -> [u8; 36] { let mut key = [0; 36]; @@ -83,6 +87,7 @@ pub fn index_change( txn, &ViewMembershipSet::from_ordered(dep_ids), )?; + validate_projected_semantics(txn, store, change, &dependencies)?; let already = txn.txn.open_table(APPLIED)?.get(id.get())?.is_some(); if already { return Ok(()); @@ -250,6 +255,83 @@ pub fn index_change( Ok(()) } +/// Validate span-projected FileOps against the exact dependency closure before +/// writing any span index entries. Generic CRDT application cannot apply these +/// operations; their semantic state is derived from the verified span graph. +pub fn validate_projected_semantics( + txn: &T, + store: &S, + change: &Change, + dependencies: &EffectiveProjectionClosure, +) -> Result<()> { + use super::SPAN_PROJECTED_SEMANTICS_CAPABILITY; + use crate::crdt::{TrunkId, TrunkOp}; + let projected: Vec<_> = change + .file_ops() + .iter() + .filter_map(|ops| match ops.trunk_op() { + Some(TrunkOp::ProjectedSpanEdit { file, baseline }) => Some((ops, file, baseline)), + _ => None, + }) + .collect(); + if projected.is_empty() { + return Ok(()); + } + ensure!( + txn.required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id())? + == Some(SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version()), + "projected span semantic capability is missing or unsupported" + ); + let patch = change + .hashed + .span + .as_ref() + .context("projected semantic edit requires a span patch")?; + let context = SpanRenderContext::new(txn, store, dependencies)?; + let mut files = BTreeSet::new(); + for (ops, file, baseline) in projected { + ensure!( + ops.trunk_id() == TrunkId::ROOT && ops.line_ops().is_empty(), + "projected semantic edit cannot contain CRDT identities or line operations" + ); + ensure!( + files.insert(file_key(file)), + "duplicate projected semantic edit for file" + ); + ensure!( + change + .file_ops() + .iter() + .filter(|candidate| candidate.path() == ops.path()) + .count() + == 1, + "projected semantic path has multiple FileOps" + ); + ensure!( + patch + .ops + .iter() + .any(|op| matches!(op, SpanOp::Edit { file: edited, .. } if edited == file)), + "projected semantic edit has no matching span edit" + ); + let inode = txn + .span_file_inode(file)? + .context("projected semantic file missing")?; + let state = context + .record_state(inode)? + .context("projected semantic file is outside the dependency closure")?; + ensure!( + state.file == *file, + "projected semantic file identity differs from span root" + ); + ensure!( + state.semantic_baseline_id()? == *baseline, + "projected semantic baseline differs from dependency closure" + ); + } + Ok(()) +} + /// Span index reads bound to the transaction used for visibility. pub trait SpanPatchReadTxn: GraphTxnT { /// Whether the change's span operations were indexed transactionally. @@ -482,6 +564,13 @@ pub struct SpanRenderContext<'a, T: SpanPatchReadTxn> { proof: Option, } +/// A bounded render never retains more than the requested number of bytes. +#[derive(Debug, PartialEq, Eq)] +pub enum BoundedSpanContent { + Complete(Vec), + Oversized { retained_bytes: usize }, +} + /// One visible portion of an authored span, in rendered file order. #[derive(Debug, Clone)] pub struct VisibleSpan { @@ -507,6 +596,358 @@ pub struct SpanRecordState { pub dependencies: Vec, } +fn projection_id( + file: &ExternalFile, + domain: &[u8], + spans: &[VisibleSpan], +) -> Result { + let mut normalized: Vec<(&ExternalSpan, u64, u64)> = Vec::new(); + for span in spans { + let length = u64::try_from(span.output.len())?; + ensure!(length != 0, "empty projected authored fragment"); + let end = span + .target_start + .checked_add(length) + .context("projected fragment overflow")?; + if let Some((target, _, previous_end)) = normalized.last_mut() { + if *target == &span.target && *previous_end == span.target_start { + *previous_end = end; + continue; + } + } + normalized.push((&span.target, span.target_start, end)); + } + ensure!( + !normalized.is_empty(), + "projected semantic identity has no authored bytes" + ); + let mut bytes = Vec::new(); + bytes.extend_from_slice(b"atomic.span.semantic.projection.v1\0"); + bytes.extend_from_slice(domain); + bytes.push(0); + bytes.extend_from_slice(file.owner.as_bytes()); + bytes.extend_from_slice(&file.ordinal.to_be_bytes()); + bytes.extend_from_slice(&u64::try_from(normalized.len())?.to_be_bytes()); + for (target, start, end) in normalized { + bytes.extend_from_slice(target.owner.as_bytes()); + bytes.extend_from_slice(&target.start.to_be_bytes()); + bytes.extend_from_slice(&target.end.to_be_bytes()); + bytes.extend_from_slice(&start.to_be_bytes()); + bytes.extend_from_slice(&end.to_be_bytes()); + } + Ok(SpanProjectionId(Hash::of(&bytes))) +} + +/// One token interpreted from a span-backed file, with its exact authored +/// byte fragments. A token may cross changes and therefore has no fabricated +/// single author or CRDT identity. +#[derive(Debug, Clone)] +pub struct SpanSemanticToken { + /// Identity derived from its authored byte fragments, independent of the + /// repository-local node IDs and rendered file position. + pub id: SpanProjectionId, + /// Token kind produced by the normal code tokenizer. + pub kind: crate::diff::token::TokenKind, + /// Byte range in the rendered file. + pub output: std::ops::Range, + /// Authored spans covering the token in byte order. + pub authors: Vec, +} + +/// One rendered line and its lossless token-level interpretation. +#[derive(Debug, Clone)] +pub struct SpanSemanticLine { + /// Identity derived from the line's authored byte fragments. + pub id: SpanProjectionId, + /// Byte range in the rendered file, including any line terminator. + pub output: std::ops::Range, + /// Tokens in source order, including the line terminator when present. + pub tokens: Vec, +} + +/// Semantic interpretation of one span-backed file on one view closure. +/// The identity binds the ordered projected lines and the portable file +/// identity; it does not claim authorship for the snapshot as a whole. +#[derive(Debug, Clone)] +pub struct SpanSemanticSnapshot { + /// Portable identity of the file represented by this snapshot. + pub file: ExternalFile, + /// Content-addressed semantic baseline identity. + pub id: SpanProjectionId, + /// Projected lines in file order. + pub lines: Vec, +} + +impl SpanRecordState { + /// Identity of the semantic baseline. Text includes line/token structure; + /// opaque bytes use a separate domain and retain their authored ranges + /// without inventing text tokens. + pub fn semantic_baseline_id(&self) -> Result { + if self.content.contains(&0) || std::str::from_utf8(&self.content).is_err() { + return projection_id( + &self.file, + b"opaque", + &self.authored_ranges(0..self.content.len())?, + ); + } + Ok(self.semantic_snapshot()?.id) + } + + /// Derive a portable semantic baseline for validating an edit on a + /// receiving repository. Identical visible authored spans produce the + /// same identity even if local node IDs or inode numbers differ. The v1 + /// tokenizer, token-kind tags and identity encoding are wire contracts: + /// changes to them require a new projected-semantics capability version. + pub fn semantic_snapshot(&self) -> Result { + let lines = self.semantic_lines()?; + let mut seen = BTreeSet::new(); + let mut seen_tokens = BTreeSet::new(); + let mut bytes = Vec::new(); + bytes.extend_from_slice(b"atomic.span.semantic.snapshot.v1\0"); + bytes.extend_from_slice(self.file.owner.as_bytes()); + bytes.extend_from_slice(&self.file.ordinal.to_be_bytes()); + bytes.extend_from_slice(&u64::try_from(lines.len())?.to_be_bytes()); + for line in &lines { + ensure!(seen.insert(line.id.0), "duplicate projected line identity"); + bytes.extend_from_slice(line.id.0.as_bytes()); + bytes.extend_from_slice(&u64::try_from(line.tokens.len())?.to_be_bytes()); + for token in &line.tokens { + ensure!( + seen_tokens.insert(token.id.0), + "duplicate projected token identity" + ); + use crate::diff::token::TokenKind; + let kind = match token.kind { + TokenKind::Word => 0u8, + TokenKind::Whitespace => 1, + TokenKind::Operator => 2, + TokenKind::Punctuation => 3, + TokenKind::String => 4, + TokenKind::Number => 5, + TokenKind::Comment => 6, + TokenKind::Newline => 7, + TokenKind::Other => 8, + }; + bytes.extend_from_slice(token.id.0.as_bytes()); + bytes.push(kind); + } + } + Ok(SpanSemanticSnapshot { + file: self.file.clone(), + id: SpanProjectionId(Hash::of(&bytes)), + lines, + }) + } + + /// Derive a lossless line/token view without creating CRDT rows or + /// assigning unchanged bytes to the current editor. Binary or invalid UTF-8 + /// data is rejected; callers must handle it as opaque content. + pub fn semantic_lines(&self) -> Result> { + use crate::diff::token::Tokenizer; + std::str::from_utf8(&self.content).context("span semantic content is not UTF-8")?; + ensure!( + !self.content.contains(&0), + "span semantic content is binary" + ); + // Validate the complete map once, then walk it monotonically as + // tokens are emitted. Rescanning all spans per token is quadratic on + // files with many independently authored fragments. + let mut covered = 0usize; + for span in &self.spans { + ensure!( + span.output.start == covered + && span.output.end > covered + && span.output.end <= self.content.len(), + "authored spans do not uniquely cover semantic content" + ); + let length = u64::try_from(span.output.end - span.output.start)?; + let target_len = span + .target + .end + .checked_sub(span.target.start) + .context("invalid authored span range")?; + ensure!( + span.target_start + .checked_add(length) + .is_some_and(|end| end <= target_len), + "visible bytes exceed the authored span" + ); + covered = span.output.end; + } + ensure!( + covered == self.content.len(), + "authored spans do not cover semantic content" + ); + let mut result = Vec::new(); + let mut line_start = 0usize; + let mut span_index = 0usize; + for line in crate::diff::Line::from_bytes(&self.content) { + let bytes = line.content(); + let line_end = line_start + .checked_add(bytes.len()) + .context("semantic line offset overflow")?; + ensure!( + self.content.get(line_start..line_end) == Some(bytes), + "semantic lines do not cover the span content" + ); + let mut token_end = 0usize; + let mut tokens = Vec::new(); + let mut line_authors = Vec::new(); + for token in Tokenizer::new(bytes) { + ensure!( + token.offset() == token_end, + "semantic tokens do not uniquely cover the line" + ); + token_end = token_end + .checked_add(token.content().len()) + .context("semantic token offset overflow")?; + ensure!( + bytes.get(token.offset()..token_end) == Some(token.content()), + "semantic token bytes differ from the line" + ); + let start = line_start + .checked_add(token.offset()) + .context("semantic token offset overflow")?; + let end = line_start + .checked_add(token_end) + .context("semantic token offset overflow")?; + let mut authors = Vec::new(); + let mut cursor = start; + while cursor < end { + let span = self + .spans + .get(span_index) + .context("authored semantic span missing")?; + ensure!( + span.output.start <= cursor && cursor < span.output.end, + "authored semantic span order changed" + ); + let hi = end.min(span.output.end); + let target_start = span + .target_start + .checked_add(u64::try_from(cursor - span.output.start)?) + .context("authored span offset overflow")?; + authors.push(VisibleSpan { + output: cursor..hi, + target: span.target.clone(), + target_start, + }); + cursor = hi; + if cursor == span.output.end { + span_index += 1; + } + } + line_authors.extend(authors.iter().cloned()); + tokens.push(SpanSemanticToken { + id: projection_id(&self.file, b"token", &authors)?, + kind: token.kind(), + output: start..end, + authors, + }); + } + ensure!( + token_end == bytes.len(), + "semantic tokens do not cover the line" + ); + result.push(SpanSemanticLine { + id: projection_id(&self.file, b"line", &line_authors)?, + output: line_start..line_end, + tokens, + }); + line_start = line_end; + } + ensure!( + line_start == self.content.len(), + "semantic lines do not cover the span content" + ); + Ok(result) + } + + /// Resolve an insertion boundary to a verified authored span and offset. + /// Boundaries between spans use the preceding byte; empty files use their + /// original root. The offset is relative to the returned span. + pub fn authored_anchor(&self, position: usize) -> Result<(ExternalSpan, u64)> { + ensure!( + position <= self.content.len(), + "rendered span anchor is out of bounds" + ); + if self.content.is_empty() { + ensure!( + self.root.start <= self.root.end, + "invalid authored root range" + ); + return Ok((self.root.clone(), 0)); + } + let (range, after) = if position == 0 { + (0..1, false) + } else { + (position - 1..position, true) + }; + let segment = self + .authored_ranges(range)? + .into_iter() + .next() + .context("authored anchor missing")?; + let offset = segment.target_start + u64::from(after); + Ok((segment.target, offset)) + } + + /// Return the exact authored ranges covering a rendered byte interval. + /// A line or token can cross multiple spans; each result keeps its actual + /// change owner rather than assigning the entire interval to one author. + pub fn authored_ranges(&self, output: std::ops::Range) -> Result> { + ensure!( + output.start <= output.end && output.end <= self.content.len(), + "rendered span interval is out of bounds" + ); + if output.is_empty() { + return Ok(Vec::new()); + } + let mut cursor = output.start; + let mut ranges = Vec::new(); + for segment in &self.spans { + ensure!( + segment.output.start <= segment.output.end + && segment.output.end <= self.content.len(), + "invalid visible span output range" + ); + let lo = output.start.max(segment.output.start); + let hi = output.end.min(segment.output.end); + if lo >= hi { + continue; + } + ensure!( + lo == cursor, + "authored spans do not uniquely cover the requested interval" + ); + let offset = segment + .target_start + .checked_add((lo - segment.output.start) as u64) + .context("authored span offset overflow")?; + let end = offset + .checked_add((hi - lo) as u64) + .context("authored span length overflow")?; + let target_len = segment + .target + .end + .checked_sub(segment.target.start) + .context("invalid authored span range")?; + ensure!(end <= target_len, "visible bytes exceed the authored span"); + ranges.push(VisibleSpan { + output: lo..hi, + target: segment.target.clone(), + target_start: offset, + }); + cursor = hi; + } + ensure!( + cursor == output.end, + "authored spans do not cover the requested interval" + ); + Ok(ranges) + } +} + impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { /// Verify the closure and content-addressed changes once for this snapshot. pub fn new( @@ -627,7 +1068,8 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { return Ok(None); } let mut spans = Vec::new(); - let content = self.render_internal(inode, Some(&mut spans))?; + let (content, oversized) = self.render_internal(inode, Some(&mut spans), usize::MAX)?; + ensure!(!oversized, "span render exceeds addressable memory"); let owner = self .txn .get_external(NodeId::new(root[0]))? @@ -690,14 +1132,30 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { /// Render the visible span tree for an inode. pub fn render(&self, inode: Inode) -> Result> { - self.render_internal(inode, None) + let (content, oversized) = self.render_internal(inode, None, usize::MAX)?; + ensure!(!oversized, "span render exceeds addressable memory"); + Ok(content) + } + + /// Render at most `max_bytes`, validating the complete span tree even when + /// the visible file is too large to retain. + pub fn render_bounded(&self, inode: Inode, max_bytes: usize) -> Result { + let (content, oversized) = self.render_internal(inode, None, max_bytes)?; + if oversized { + Ok(BoundedSpanContent::Oversized { + retained_bytes: content.len(), + }) + } else { + Ok(BoundedSpanContent::Complete(content)) + } } fn render_internal( &self, inode: Inode, segments: Option<&mut Vec>, - ) -> Result> { + max_bytes: usize, + ) -> Result<(Vec, bool)> { let root = self.txn.span_root(inode)?.context("span root missing")?; let root_id = NodeId::new(root[0]); ensure!(self.visible.contains(root_id), "span root outside view"); @@ -747,9 +1205,14 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { observed == self.expected.get(&inode.get()).cloned().unwrap_or_default(), "visible span index differs from hashed changes" ); - let mut out = Vec::new(); - self.render_spans(inode, root, &mut out, segments)?; - Ok(out) + let mut out = RenderOutput { + bytes: Vec::new(), + limit: max_bytes, + oversized: false, + segments, + }; + self.render_spans(inode, root, &mut out)?; + Ok((out.bytes, out.oversized)) } fn depends_on(&self, author: u64, predecessor: u64) -> Result { @@ -839,25 +1302,13 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { }) } - fn render_spans( - &self, - inode: Inode, - root: [u64; 3], - out: &mut Vec, - mut segments: Option<&mut Vec>, - ) -> Result<()> { + fn render_spans(&self, inode: Inode, root: [u64; 3], out: &mut RenderOutput<'_>) -> Result<()> { let mut active = BTreeSet::new(); active.insert(root); let mut stack = vec![self.frame(inode, root)?]; while let Some(frame) = stack.last_mut() { if frame.next == frame.edits.len() { - append_visible( - frame, - frame.cursor, - frame.end - frame.start, - out, - segments.as_deref_mut(), - ); + append_visible(frame, frame.cursor, frame.end - frame.start, out); active.remove(&frame.span); stack.pop(); continue; @@ -900,7 +1351,7 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { } } if !interior_gap { - append_visible(frame, frame.cursor, lo, out, segments.as_deref_mut()); + append_visible(frame, frame.cursor, lo, out); frame.cursor = hi; } if lo < hi { @@ -930,21 +1381,27 @@ struct RenderFrame { covered: Option<(usize, usize, u64)>, } -fn append_visible( - frame: &RenderFrame, - start: usize, - end: usize, - out: &mut Vec, - segments: Option<&mut Vec>, -) { - if start == end { +struct RenderOutput<'a> { + bytes: Vec, + limit: usize, + oversized: bool, + segments: Option<&'a mut Vec>, +} + +fn append_visible(frame: &RenderFrame, start: usize, end: usize, out: &mut RenderOutput<'_>) { + if start == end || out.oversized { return; } - let output_start = out.len(); - out.extend_from_slice(&frame.change.contents[frame.start + start..frame.start + end]); - if let Some(segments) = segments { + let content = &frame.change.contents[frame.start + start..frame.start + end]; + if content.len() > out.limit.saturating_sub(out.bytes.len()) { + out.oversized = true; + return; + } + let output_start = out.bytes.len(); + out.bytes.extend_from_slice(content); + if let Some(segments) = out.segments.as_deref_mut() { segments.push(VisibleSpan { - output: output_start..out.len(), + output: output_start..out.bytes.len(), target: ExternalSpan { owner: frame.hash, start: frame.span[1], @@ -962,6 +1419,375 @@ mod tests { use crate::change::{ChangeHeader, MemoryChangeStore}; use crate::pristine::{MutTxnT, Pristine, ViewScope}; + fn mapped_record_state() -> SpanRecordState { + let first = Hash::of(b"first"); + let second = Hash::of(b"second"); + let root = ExternalSpan { + owner: first, + start: 10, + end: 20, + }; + SpanRecordState { + file: ExternalFile { + owner: first, + ordinal: 0, + }, + root: root.clone(), + content: b"abcDEFghi".to_vec(), + spans: vec![ + VisibleSpan { + output: 0..3, + target: root.clone(), + target_start: 2, + }, + VisibleSpan { + output: 3..6, + target: ExternalSpan { + owner: second, + start: 30, + end: 33, + }, + target_start: 0, + }, + VisibleSpan { + output: 6..9, + target: root, + target_start: 5, + }, + ], + dependencies: vec![first, second], + } + } + + #[test] + fn authored_ranges_preserve_owners_across_split_lines_and_tokens() -> Result<()> { + let state = mapped_record_state(); + let ranges = state.authored_ranges(1..8)?; + assert_eq!(ranges.len(), 3); + assert_eq!( + ranges + .iter() + .map(|range| range.output.clone()) + .collect::>(), + vec![1..3, 3..6, 6..8] + ); + assert_eq!( + ranges + .iter() + .map(|range| range.target_start) + .collect::>(), + vec![3, 0, 5] + ); + assert_eq!(ranges[0].target.owner, ranges[2].target.owner); + assert_ne!(ranges[0].target.owner, ranges[1].target.owner); + assert!(state.authored_ranges(3..3)?.is_empty()); + assert_eq!(state.authored_ranges(3..6)?.len(), 1); + Ok(()) + } + + #[test] + fn semantic_projection_keeps_cross_change_token_authorship() -> Result<()> { + let state = mapped_record_state(); + let lines = state.semantic_lines()?; + assert_eq!(lines.len(), 1); + assert_eq!(lines[0].output, 0..9); + assert_eq!(lines[0].tokens.len(), 1); + let token = &lines[0].tokens[0]; + assert_eq!(token.kind, crate::diff::token::TokenKind::Word); + assert_eq!(token.output, 0..9); + assert_eq!(token.authors.len(), 3); + assert_eq!(token.authors[0].target.owner, token.authors[2].target.owner); + assert_ne!(token.authors[0].target.owner, token.authors[1].target.owner); + Ok(()) + } + + #[test] + fn semantic_projection_ids_are_portable_across_offsets_and_fragment_splits() -> Result<()> { + let state = mapped_record_state(); + let original = state.semantic_lines()?; + assert_ne!(original[0].id, original[0].tokens[0].id); + let encoded = postcard::to_allocvec(&original[0].id)?; + assert_eq!( + postcard::from_bytes::(&encoded)?, + original[0].id + ); + let mut shifted = mapped_record_state(); + shifted.content.splice(0..0, b"prefix\n".iter().copied()); + for span in &mut shifted.spans { + span.output.start += 7; + span.output.end += 7; + } + let prefix_owner = Hash::of(b"prefix author"); + shifted.spans.insert( + 0, + VisibleSpan { + output: 0..7, + target: ExternalSpan { + owner: prefix_owner, + start: 0, + end: 7, + }, + target_start: 0, + }, + ); + let first = shifted.spans[1].clone(); + shifted.spans[1].output.end = first.output.start + 1; + shifted.spans.insert( + 2, + VisibleSpan { + output: first.output.start + 1..first.output.end, + target: first.target, + target_start: first.target_start + 1, + }, + ); + let later = shifted.semantic_lines()?; + assert_eq!(later.len(), 2); + assert_eq!(later[1].id, original[0].id); + assert_eq!(later[1].tokens[0].id, original[0].tokens[0].id); + shifted.file.ordinal += 1; + assert_ne!(shifted.semantic_lines()?[1].id, original[0].id); + Ok(()) + } + + #[test] + fn projected_semantic_v1_identity_reference_vector() -> Result<()> { + use crate::types::Base32; + let owner = Hash::of(b"projection reference owner"); + let content = b"let x = 1;\n".to_vec(); + let root = ExternalSpan { + owner, + start: 0, + end: content.len() as u64, + }; + let state = SpanRecordState { + file: ExternalFile { owner, ordinal: 7 }, + root: root.clone(), + spans: vec![VisibleSpan { + output: 0..content.len(), + target: root, + target_start: 0, + }], + content, + dependencies: vec![owner], + }; + let snapshot = state.semantic_snapshot()?; + assert_eq!( + snapshot.id.0.to_base32(), + "TTHO66BQVUGLYVDJ7G7OXKX3KOWQQ5Z6UCEXT2Y65HXX245MXUKA" + ); + assert_eq!( + snapshot.lines[0].id.0.to_base32(), + "GBBDUIMWT3HERMRPYH3GQ6XKIF3AFSGW5WKENDWEMD3N4WQ2R3ZQ" + ); + let tokens: Vec<_> = snapshot.lines[0] + .tokens + .iter() + .map(|token| token.id.0.to_base32()) + .collect(); + assert_eq!( + tokens.first().unwrap(), + "XCWKLIVL4AZVFLMUICUOOPNGNS2POKIYY6KEOZN2EMB22ZTDBM3A" + ); + assert_eq!( + tokens.last().unwrap(), + "OSUGQK2YZIJ527KIDII25BX7BU43YESF5ETZRI346QEHOZGZMFAQ" + ); + Ok(()) + } + + #[test] + fn empty_semantic_snapshots_keep_portable_file_identity() -> Result<()> { + let mut state = mapped_record_state(); + state.content.clear(); + state.spans.clear(); + let snapshot = state.semantic_snapshot()?; + assert!(snapshot.lines.is_empty()); + assert_eq!(snapshot.file, state.file); + assert_eq!(state.semantic_snapshot()?.id, snapshot.id); + state.file.ordinal += 1; + assert_ne!(state.semantic_snapshot()?.id, snapshot.id); + Ok(()) + } + + #[test] + fn semantic_snapshot_refuses_duplicate_projected_line_identity() { + let mut state = mapped_record_state(); + state.content = b"a\na\n".to_vec(); + let target = state.root.clone(); + state.spans = vec![ + VisibleSpan { + output: 0..2, + target: target.clone(), + target_start: 0, + }, + VisibleSpan { + output: 2..4, + target, + target_start: 0, + }, + ]; + let error = state.semantic_snapshot().unwrap_err(); + assert!( + error + .to_string() + .contains("duplicate projected line identity"), + "{error}" + ); + } + + #[test] + fn semantic_projection_walks_many_fragments_without_changing_identities() -> Result<()> { + let owner = Hash::of(b"large fragmented source"); + let content = b"alpha beta\n".repeat(1000); + let root = ExternalSpan { + owner, + start: 0, + end: content.len() as u64, + }; + let state = SpanRecordState { + file: ExternalFile { owner, ordinal: 0 }, + root: root.clone(), + spans: (0..content.len()) + .map(|index| VisibleSpan { + output: index..index + 1, + target: root.clone(), + target_start: index as u64, + }) + .collect(), + content, + dependencies: vec![owner], + }; + let snapshot = state.semantic_snapshot()?; + assert_eq!(snapshot.lines.len(), 1000); + assert_eq!(snapshot.lines[0].tokens.len(), 4); + for line in [&snapshot.lines[0], &snapshot.lines[999]] { + assert_eq!( + line.id, + projection_id( + &state.file, + b"line", + &state.authored_ranges(line.output.clone())? + )? + ); + for token in &line.tokens { + assert_eq!( + token.id, + projection_id( + &state.file, + b"token", + &state.authored_ranges(token.output.clone())? + )? + ); + } + } + Ok(()) + } + + #[test] + fn semantic_projection_preserves_split_crlf_and_refuses_binary_or_gaps() -> Result<()> { + let first = Hash::of(b"first newline author"); + let second = Hash::of(b"second newline author"); + let mut state = SpanRecordState { + file: ExternalFile { + owner: first, + ordinal: 0, + }, + root: ExternalSpan { + owner: first, + start: 0, + end: 2, + }, + content: b"a\r\nb\n".to_vec(), + spans: vec![ + VisibleSpan { + output: 0..2, + target: ExternalSpan { + owner: first, + start: 0, + end: 2, + }, + target_start: 0, + }, + VisibleSpan { + output: 2..5, + target: ExternalSpan { + owner: second, + start: 4, + end: 7, + }, + target_start: 0, + }, + ], + dependencies: vec![first, second], + }; + let lines = state.semantic_lines()?; + assert_eq!( + lines + .iter() + .map(|line| line.output.clone()) + .collect::>(), + vec![0..3, 3..5] + ); + let newline = lines[0].tokens.last().unwrap(); + assert_eq!(newline.kind, crate::diff::token::TokenKind::Newline); + assert_eq!(newline.output, 1..3); + assert_eq!(newline.authors.len(), 2); + assert_eq!(newline.authors[0].target.owner, first); + assert_eq!(newline.authors[1].target.owner, second); + state.spans[1].output.start = 3; + assert!(state.semantic_lines().is_err()); + state.content = b"a\0".to_vec(); + assert!(state.semantic_lines().is_err()); + state.content = vec![0xff]; + assert!(state.semantic_lines().is_err()); + Ok(()) + } + + #[test] + fn authored_ranges_reject_gaps_overlaps_and_invalid_offsets() { + let mut state = mapped_record_state(); + assert!(state.authored_ranges(4..10).is_err()); + assert!(state + .authored_ranges(std::ops::Range { start: 3, end: 2 }) + .is_err()); + state.spans[1].output.start = 4; + assert!(state.authored_ranges(0..9).is_err()); + state.spans[1].output.start = 2; + assert!(state.authored_ranges(0..9).is_err()); + state.spans[1].output = 3..6; + state.spans[1].target_start = u64::MAX; + assert!(state.authored_ranges(3..6).is_err()); + state.spans[1].target_start = 1; + assert!(state.authored_ranges(3..6).is_err()); + state.spans[1].target.start = 34; + assert!(state.authored_ranges(3..6).is_err()); + } + + #[test] + fn authored_anchor_preserves_preceding_owner_and_empty_root() -> Result<()> { + let mut state = mapped_record_state(); + let (first, offset) = state.authored_anchor(0)?; + assert_eq!(first, state.spans[0].target); + assert_eq!(offset, 2); + let (at_boundary, offset) = state.authored_anchor(3)?; + assert_eq!(at_boundary, state.spans[0].target); + assert_eq!(offset, 5); + let (second, offset) = state.authored_anchor(4)?; + assert_eq!(second, state.spans[1].target); + assert_eq!(offset, 1); + assert!(state.authored_anchor(10).is_err()); + state.spans[1].target_start = u64::MAX; + assert!(state.authored_anchor(4).is_err()); + state.content.clear(); + state.spans.clear(); + let (root, offset) = state.authored_anchor(0)?; + assert_eq!(root, state.root); + assert_eq!(offset, 0); + state.root.start = state.root.end + 1; + assert!(state.authored_anchor(0).is_err()); + Ok(()) + } + fn add_change( repo: &Pristine, store: &MemoryChangeStore, @@ -998,6 +1824,39 @@ mod tests { #[test] fn independent_span_edits_render_in_both_insert_orders() -> Result<()> { + fn projection_ids( + repo: &Pristine, + store: &MemoryChangeStore, + view_name: &str, + inode: Inode, + ) -> Result<( + SpanProjectionId, + Vec<(SpanProjectionId, Vec)>, + )> { + let txn = repo.read_txn()?; + let view = txn.get_view(view_name)?.context("view missing")?; + let visible = EffectiveProjectionClosure::try_from_membership( + &txn, + &txn.view_membership_set(&view)?, + )?; + let state = SpanRenderContext::new(&txn, store, &visible)? + .record_state(inode)? + .context("span state missing")?; + let snapshot = state.semantic_snapshot()?; + Ok(( + snapshot.id, + snapshot + .lines + .into_iter() + .map(|line| { + ( + line.id, + line.tokens.into_iter().map(|token| token.id).collect(), + ) + }) + .collect(), + )) + } let dir = tempfile::tempdir()?; let path = dir.path().join("pristine.redb"); let repo = Pristine::open(&path)?; @@ -1056,6 +1915,10 @@ mod tests { let right = edit("right", b"GAMMA", 11, 16); let left_hash = add_change(&repo, &store, "left", left.clone(), BTreeMap::new())?; let right_hash = add_change(&repo, &store, "right", right.clone(), BTreeMap::new())?; + assert_ne!( + projection_ids(&repo, &store, "left", inode)?.0, + projection_ids(&repo, &store, "right", inode)?.0 + ); assert_eq!( render(&repo, &store, "left", inode)?, b"ALPHA\nbeta\ngamma\nomega" @@ -1064,6 +1927,55 @@ mod tests { render(&repo, &store, "right", inode)?, b"alpha\nbeta\nGAMMA\nomega" ); + for (view_name, expected_owners) in [ + ( + "left", + vec![ + vec![left_hash, root_hash], + vec![root_hash], + vec![root_hash], + vec![root_hash], + ], + ), + ( + "right", + vec![ + vec![root_hash], + vec![root_hash], + vec![right_hash, root_hash], + vec![root_hash], + ], + ), + ] { + let txn = repo.read_txn()?; + let view = txn.get_view(view_name)?.context("view missing")?; + let visible = EffectiveProjectionClosure::try_from_membership( + &txn, + &txn.view_membership_set(&view)?, + )?; + let state = SpanRenderContext::new(&txn, &store, &visible)? + .record_state(inode)? + .context("span state missing")?; + let actual: Vec> = state + .semantic_lines()? + .iter() + .map(|line| { + let mut owners = Vec::new(); + for token in &line.tokens { + for fragment in &token.authors { + if !owners.contains(&fragment.target.owner) { + owners.push(fragment.target.owner); + } + } + } + owners + }) + .collect(); + assert_eq!( + actual, expected_owners, + "{view_name} must exclude its sibling's edits" + ); + } let expected = b"ALPHA\nbeta\nGAMMA\nomega"; for (view, order) in [("both", [&left, &right]), ("reverse", [&right, &left])] { for change in order { @@ -1081,6 +1993,47 @@ mod tests { .render(inode)?, expected ); + let context = SpanRenderContext::new(&txn, &store, &visible)?; + let projected = context.record_state(inode)?.context("span state missing")?; + let lines = projected.semantic_lines()?; + assert_eq!(lines.len(), 4); + for (line, expected_owners) in lines.iter().zip([ + vec![left_hash, root_hash], + vec![root_hash], + vec![right_hash, root_hash], + vec![root_hash], + ]) { + let mut owners = Vec::new(); + let mut rendered = Vec::new(); + for token in &line.tokens { + for fragment in &token.authors { + let owner = fragment.target.owner; + if !owners.contains(&owner) { + owners.push(owner); + } + let change = store.get_change(&owner)?; + let start = usize::try_from(fragment.target.start + fragment.target_start)?; + let end = start + fragment.output.len(); + rendered.extend_from_slice(&change.contents[start..end]); + } + } + assert_eq!(owners, expected_owners); + assert_eq!(rendered, projected.content[line.output.clone()]); + } + assert_eq!( + context.render_bounded(inode, expected.len())?, + BoundedSpanContent::Complete(expected.to_vec()) + ); + match context.render_bounded(inode, expected.len() - 1)? { + BoundedSpanContent::Oversized { retained_bytes } => { + assert!(retained_bytes < expected.len()); + } + other => panic!("expected bounded oversized result, got {other:?}"), + } + assert_eq!( + context.render_bounded(inode, 0)?, + BoundedSpanContent::Oversized { retained_bytes: 0 } + ); } let txn = repo.read_txn()?; assert_eq!( @@ -1090,9 +2043,238 @@ mod tests { ); assert_ne!(left_hash, right_hash); drop(txn); + let expected_ids = projection_ids(&repo, &store, "both", inode)?; + assert_eq!( + projection_ids(&repo, &store, "reverse", inode)?, + expected_ids + ); + // The receiver allocates its own inode and local change IDs. Only + // immutable change content and portable file identity are transferred. + let receiver_path = dir.path().join("receiver.redb"); + let receiver = Pristine::open(&receiver_path)?; + let receiver_inode = Inode::new(987); + { + let mut txn = receiver.write_txn()?; + txn.open_or_create_view("dev")?; + txn.register_change(&Hash::of(b"unrelated receiver allocation"))?; + txn.commit()?; + } + add_change( + &receiver, + &store, + "dev", + store.get_change(&root_hash)?, + BTreeMap::from([(0, receiver_inode)]), + )?; + add_change(&receiver, &store, "dev", left.clone(), BTreeMap::new())?; + add_change(&receiver, &store, "dev", right.clone(), BTreeMap::new())?; + assert_ne!( + repo.read_txn()?.get_internal(&root_hash)?, + receiver.read_txn()?.get_internal(&root_hash)? + ); + assert_eq!( + projection_ids(&receiver, &store, "dev", receiver_inode)?, + expected_ids + ); + drop(receiver); + let receiver = Pristine::open(receiver_path)?; + assert_eq!( + projection_ids(&receiver, &store, "dev", receiver_inode)?, + expected_ids + ); drop(repo); let reopened = Pristine::open(path)?; assert_eq!(render(&reopened, &store, "both", inode)?, expected); + assert_eq!( + projection_ids(&reopened, &store, "both", inode)?, + expected_ids + ); + Ok(()) + } + + #[test] + fn semantic_projection_refuses_missing_visible_edit_index_after_reopen() -> Result<()> { + let dir = tempfile::tempdir()?; + let path = dir.path().join("pristine.redb"); + let repo = Pristine::open(&path)?; + let store = MemoryChangeStore::new(); + let inode = Inode::new(42); + { + let mut txn = repo.write_txn()?; + txn.open_or_create_view("dev")?; + txn.commit()?; + } + let mut root = Change::new( + ChangeHeader::new("root"), + vec![], + b"hello\n".to_vec(), + vec![], + ); + root.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "file.txt".into(), + ordinal: 0, + start: 0, + end: 6, + }], + )); + let root_hash = add_change(&repo, &store, "dev", root, BTreeMap::from([(0, inode)]))?; + let mut edit = Change::new( + ChangeHeader::new("edit"), + vec![], + b"a".to_vec(), + vec![root_hash], + ); + edit.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Edit { + file: ExternalFile { + owner: root_hash, + ordinal: 0, + }, + target: ExternalSpan { + owner: root_hash, + start: 0, + end: 6, + }, + start: 1, + end: 2, + insert_start: 0, + insert_end: 1, + }], + )); + add_change(&repo, &store, "dev", edit, BTreeMap::new())?; + assert_eq!(render(&repo, &store, "dev", inode)?, b"hallo\n"); + { + let txn = repo.write_txn()?; + let owner = txn.get_internal(&root_hash)?.context("root id missing")?; + let key = span_key(inode, owner, 0, 6); + let mut edits = txn.txn.open_multimap_table(EDITS)?; + assert_eq!(edits.remove_all(&key)?.count(), 1); + drop(edits); + txn.commit()?; + } + drop(repo); + let reopened = Pristine::open(path)?; + let txn = reopened.read_txn()?; + let view = txn.get_view("dev")?.context("view missing")?; + let visible = EffectiveProjectionClosure::try_from_membership( + &txn, + &txn.view_membership_set(&view)?, + )?; + let context = SpanRenderContext::new(&txn, &store, &visible)?; + let error = match context.record_state(inode) { + Err(error) => error, + Ok(_) => panic!("corrupt span index was accepted"), + }; + assert!( + error.to_string().contains("visible span index differs"), + "{error:#}" + ); + Ok(()) + } + + #[test] + fn projected_semantic_edit_validates_capability_and_exact_dependency_snapshot() -> Result<()> { + use crate::pristine::{CapabilityMutTxnT, SPAN_PROJECTED_SEMANTICS_CAPABILITY}; + let dir = tempfile::tempdir()?; + let repo = Pristine::open(dir.path().join("pristine.redb"))?; + let store = MemoryChangeStore::new(); + let inode = Inode::new(42); + { + let mut txn = repo.write_txn()?; + txn.open_or_create_view("dev")?; + txn.commit()?; + } + let mut root = Change::new( + ChangeHeader::new("root"), + vec![], + b"hello\n".to_vec(), + vec![], + ); + root.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "file.txt".into(), + ordinal: 0, + start: 0, + end: 6, + }], + )); + let root_hash = add_change(&repo, &store, "dev", root, BTreeMap::from([(0, inode)]))?; + let file = ExternalFile { + owner: root_hash, + ordinal: 0, + }; + let baseline = { + let txn = repo.read_txn()?; + let view = txn.get_view("dev")?.unwrap(); + let visible = EffectiveProjectionClosure::try_from_membership( + &txn, + &txn.view_membership_set(&view)?, + )?; + SpanRenderContext::new(&txn, &store, &visible)? + .record_state(inode)? + .unwrap() + .semantic_snapshot()? + .id + }; + let make_edit = |baseline| { + let mut change = Change::new( + ChangeHeader::new("edit"), + vec![], + b"a".to_vec(), + vec![root_hash], + ); + change.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Edit { + file: file.clone(), + target: ExternalSpan { + owner: root_hash, + start: 0, + end: 6, + }, + start: 1, + end: 2, + insert_start: 0, + insert_end: 1, + }], + )); + change + .hashed + .file_ops + .push(crate::change::FileOps::projected_span_edit( + "file.txt".into(), + file.clone(), + baseline, + )); + change + }; + let valid = make_edit(baseline); + let error = add_change(&repo, &store, "dev", valid.clone(), BTreeMap::new()).unwrap_err(); + assert!(error.to_string().contains("capability"), "{error:#}"); + assert_eq!(render(&repo, &store, "dev", inode)?, b"hello\n"); + { + let mut txn = repo.write_txn()?; + txn.put_required_capability(SPAN_PROJECTED_SEMANTICS_CAPABILITY)?; + txn.commit()?; + } + let wrong = make_edit(SpanProjectionId(Hash::of(b"wrong baseline"))); + let error = add_change(&repo, &store, "dev", wrong, BTreeMap::new()).unwrap_err(); + assert!(error.to_string().contains("baseline differs"), "{error:#}"); + assert_eq!(render(&repo, &store, "dev", inode)?, b"hello\n"); + { + let mut txn = repo.write_txn()?; + let id = txn.register_change(&Hash::of(b"generic apply attempt"))?; + let error = + crate::apply::apply_file_ops_batched(&mut txn, id, valid.file_ops()).unwrap_err(); + assert!(error.to_string().contains("span-aware"), "{error}"); + txn.abort()?; + } + add_change(&repo, &store, "dev", valid, BTreeMap::new())?; + assert_eq!(render(&repo, &store, "dev", inode)?, b"hallo\n"); Ok(()) } @@ -1165,6 +2347,17 @@ mod tests { .unwrap_err() .to_string() .contains("overlapping")); + let txn = repo.read_txn()?; + let state = txn.get_view("both")?.unwrap(); + let visible = EffectiveProjectionClosure::try_from_membership( + &txn, + &txn.view_membership_set(&state)?, + )?; + assert!(SpanRenderContext::new(&txn, &store, &visible)? + .render_bounded(inode, 0) + .unwrap_err() + .to_string() + .contains("overlapping")); Ok(()) } } diff --git a/atomic-core/src/pristine/tables.rs b/atomic-core/src/pristine/tables.rs index 57c4c724..0a663713 100644 --- a/atomic-core/src/pristine/tables.rs +++ b/atomic-core/src/pristine/tables.rs @@ -407,12 +407,15 @@ pub const GIT_SHA_INDEX: TableDefinition<&str, u64> = TableDefinition::new("git_ /// imported in an earlier run reconstructs the exact parent visibility a /// sibling lineage must be excluded against — a per-run ledger cannot. /// -/// Value encoding: little-endian u32 count, then that many 32-byte change -/// hashes concatenated (canonical order is the caller's responsibility; the -/// count makes decoding total). +/// Legacy rows hold a count and complete hashes. Shared rows hold a versioned +/// root into immutable trie nodes and require `git-closure-sharing`. pub const GIT_COMMIT_CLOSURES: TableDefinition<&str, &[u8]> = TableDefinition::new("git_commit_closures"); +/// Content-addressed Git closure trie nodes: node hash → (left, right). +pub const GIT_CLOSURE_NODES: TableDefinition<&[u8; 32], &[u8; 64]> = + TableDefinition::new("git_closure_nodes"); + // ============================================================================ // Bridge event captures (RFC-ATOMIC-GIT-CAUSAL-BRIDGE §5.4, review R4) // ============================================================================ diff --git a/atomic-core/src/pristine/txn/pristine.rs b/atomic-core/src/pristine/txn/pristine.rs index b82a6e2e..d6bedf1a 100644 --- a/atomic-core/src/pristine/txn/pristine.rs +++ b/atomic-core/src/pristine/txn/pristine.rs @@ -362,6 +362,7 @@ impl Pristine { // Persistent Git commit interpretation closures (CB-9B review R1) write_txn.open_table(GIT_COMMIT_CLOSURES)?; + write_txn.open_table(GIT_CLOSURE_NODES)?; // Immutable captured bridge hook events (CB-9B review R4) write_txn.open_table(BRIDGE_EVENT_CAPTURES)?; @@ -602,6 +603,11 @@ impl Pristine { let mut write_txn = self.db.writable_db()?.begin_write()?; let _ = write_txn.set_durability(redb::Durability::Immediate); + if capabilities.iter().any(|capability| { + capability.id() == crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id() + }) { + crate::pristine::schema::require_projected_semantics_schema(&write_txn)?; + } let mut metadata = write_txn.open_table(PRISTINE_META)?; let mut existing_requirements = Vec::new(); diff --git a/atomic-core/src/pristine/txn/read.rs b/atomic-core/src/pristine/txn/read.rs index 8426a7e3..e375ffb6 100644 --- a/atomic-core/src/pristine/txn/read.rs +++ b/atomic-core/src/pristine/txn/read.rs @@ -3003,32 +3003,16 @@ impl GitCommitClosureTxnT for ReadTxn { }; match table.get(sha)? { Some(bytes) => { - let value = bytes.value(); - if value.len() < 4 { - return Err(PristineError::Serialization { - message: format!( - "git commit closure for {sha} is truncated: {} byte(s)", - value.len() - ), - }); - } - let count = u32::from_le_bytes([value[0], value[1], value[2], value[3]]) as usize; - if value.len() != 4 + count * 32 { - return Err(PristineError::Serialization { - message: format!( - "git commit closure for {sha} claims {count} hashes but holds {} bytes", - value.len() - ), - }); - } - let mut closure = Vec::with_capacity(count); - for index in 0..count { - let start = 4 + index * 32; - let mut hash = [0u8; 32]; - hash.copy_from_slice(&value[start..start + 32]); - closure.push(Hash::from_bytes(hash)); - } - Ok(Some(closure)) + let nodes = match self.txn.open_table(GIT_CLOSURE_NODES) { + Ok(table) => Some(table), + Err(redb::TableError::TableDoesNotExist(_)) => None, + Err(error) => return Err(error.into()), + }; + crate::pristine::git_closure::decode(bytes.value(), |id| match &nodes { + Some(table) => Ok(table.get(id)?.map(|value| *value.value())), + None => Ok(None), + }) + .map(Some) } None => Ok(None), } diff --git a/atomic-core/src/pristine/txn/write/capability.rs b/atomic-core/src/pristine/txn/write/capability.rs index 352c1a36..0cd2e490 100644 --- a/atomic-core/src/pristine/txn/write/capability.rs +++ b/atomic-core/src/pristine/txn/write/capability.rs @@ -28,6 +28,9 @@ impl CapabilityTxnT for WriteTxn<'_> { impl CapabilityMutTxnT for WriteTxn<'_> { fn put_required_capability(&mut self, capability: RepositoryCapability) -> PristineResult { + if capability.id() == crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id() { + crate::pristine::schema::require_projected_semantics_schema(&self.txn)?; + } let mut table = self.txn.open_table(PRISTINE_META)?; let key = capability_metadata_key(capability.id()); let existing = table.get(key.as_str())?.map(|version| version.value()); @@ -42,12 +45,38 @@ impl CapabilityMutTxnT for WriteTxn<'_> { } fn put_required_capability_exact(&mut self, id: &str, version: u32) -> PristineResult { + let projected = crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY; + if id == projected.id() { + if version < projected.minimum_version() { + return Err(crate::pristine::PristineError::Inconsistent { + message: "projected span semantic capability cannot be lowered".into(), + }); + } + let existing = { + let table = self.txn.open_table(PRISTINE_META)?; + let version = table + .get(capability_metadata_key(id).as_str())? + .map(|value| value.value()); + version + }; + if existing.is_some_and(|old| version < old) { + return Err(crate::pristine::PristineError::Inconsistent { + message: "projected span semantic capability cannot be lowered".into(), + }); + } + crate::pristine::schema::require_projected_semantics_schema(&self.txn)?; + } let mut table = self.txn.open_table(PRISTINE_META)?; table.insert(capability_metadata_key(id).as_str(), version)?; Ok(version) } fn del_required_capability(&mut self, id: &str) -> PristineResult { + if id == crate::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id() { + return Err(crate::pristine::PristineError::Inconsistent { + message: "projected span semantic capability cannot be removed".into(), + }); + } let mut table = self.txn.open_table(PRISTINE_META)?; let key = capability_metadata_key(id); let removed = table.remove(key.as_str())?; diff --git a/atomic-core/src/pristine/txn/write/tag.rs b/atomic-core/src/pristine/txn/write/tag.rs index de0702bd..4faeb966 100644 --- a/atomic-core/src/pristine/txn/write/tag.rs +++ b/atomic-core/src/pristine/txn/write/tag.rs @@ -3,8 +3,8 @@ use super::*; use crate::pristine::tables::{ - BRIDGE_EVENT_CAPTURES, BRIDGE_EVENT_CAPTURE_ANCHORS, GIT_COMMIT_CLOSURES, GIT_SHA_INDEX, - TAG_NAME_INDEX, TAG_RECORDS, + BRIDGE_EVENT_CAPTURES, BRIDGE_EVENT_CAPTURE_ANCHORS, GIT_CLOSURE_NODES, GIT_COMMIT_CLOSURES, + GIT_SHA_INDEX, TAG_NAME_INDEX, TAG_RECORDS, }; use crate::pristine::traits::tag::{ BridgeEventCaptureMutTxnT, BridgeEventCaptureTxnT, GitCommitClosureMutTxnT, @@ -288,44 +288,6 @@ impl<'a> GitShaIndexMutTxnT for WriteTxn<'a> { // GIT COMMIT INTERPRETATION CLOSURES (CB-9B review R1) // ============================================================================ -/// Encode a closure as u32 count + concatenated 32-byte hashes. -fn encode_git_commit_closure(closure: &[Hash]) -> Vec { - let mut bytes = Vec::with_capacity(4 + closure.len() * 32); - bytes.extend_from_slice(&(closure.len() as u32).to_le_bytes()); - for hash in closure { - bytes.extend_from_slice(hash.as_bytes()); - } - bytes -} - -fn decode_git_commit_closure(bytes: &[u8]) -> PristineResult> { - if bytes.len() < 4 { - return Err(PristineError::Serialization { - message: format!( - "git commit closure is truncated: {} byte(s), need at least 4", - bytes.len() - ), - }); - } - let count = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize; - if bytes.len() != 4 + count * 32 { - return Err(PristineError::Serialization { - message: format!( - "git commit closure claims {count} hashes but holds {} bytes", - bytes.len() - ), - }); - } - let mut closure = Vec::with_capacity(count); - for index in 0..count { - let start = 4 + index * 32; - let mut hash = [0u8; 32]; - hash.copy_from_slice(&bytes[start..start + 32]); - closure.push(Hash::from_bytes(hash)); - } - Ok(closure) -} - impl<'a> GitCommitClosureTxnT for WriteTxn<'a> { fn get_git_commit_closure(&self, sha: &str) -> PristineResult>> { let decoded = { @@ -340,7 +302,16 @@ impl<'a> GitCommitClosureTxnT for WriteTxn<'a> { None => return Ok(None), } }; - Ok(Some(decode_git_commit_closure(&decoded)?)) + let nodes = match self.txn.open_table(GIT_CLOSURE_NODES) { + Ok(table) => Some(table), + Err(redb::TableError::TableDoesNotExist(_)) => None, + Err(error) => return Err(error.into()), + }; + crate::pristine::git_closure::decode(&decoded, |id| match &nodes { + Some(table) => Ok(table.get(id)?.map(|value| *value.value())), + None => Ok(None), + }) + .map(Some) } fn has_git_commit_closure(&self, sha: &str) -> PristineResult { @@ -354,10 +325,9 @@ impl<'a> GitCommitClosureTxnT for WriteTxn<'a> { impl<'a> GitCommitClosureMutTxnT for WriteTxn<'a> { fn put_git_commit_closure(&mut self, sha: &str, closure: &[Hash]) -> PristineResult<()> { - let encoded = encode_git_commit_closure(closure); - let mut table = self.txn.open_table(GIT_COMMIT_CLOSURES)?; - if let Some(existing) = table.get(sha)? { - if existing.value() != encoded.as_slice() { + use crate::pristine::{CapabilityMutTxnT, GIT_CLOSURE_SHARING_CAPABILITY}; + if let Some(existing) = self.get_git_commit_closure(sha)? { + if existing != closure { return Err(PristineError::Serialization { message: format!( "git commit {sha} already has a different persisted interpretation \ @@ -368,6 +338,26 @@ impl<'a> GitCommitClosureMutTxnT for WriteTxn<'a> { } return Ok(()); } + let encoded = { + let mut nodes = self.txn.open_table(GIT_CLOSURE_NODES)?; + crate::pristine::git_closure::encode(closure, |id, node| { + let existing = nodes.get(id)?.map(|value| *value.value()); + match existing { + Some(existing) if existing != *node => Err(PristineError::Serialization { + message: "Git closure trie node has conflicting content".into(), + }), + Some(_) => Ok(()), + None => { + nodes.insert(id, node)?; + Ok(()) + } + } + })? + }; + if crate::pristine::git_closure::is_shared(&encoded) { + self.put_required_capability(GIT_CLOSURE_SHARING_CAPABILITY)?; + } + let mut table = self.txn.open_table(GIT_COMMIT_CLOSURES)?; table.insert(sha, encoded.as_slice())?; Ok(()) } diff --git a/atomic-core/src/record/workflow/record/mod.rs b/atomic-core/src/record/workflow/record/mod.rs index 5c97d3bb..f6751b51 100644 --- a/atomic-core/src/record/workflow/record/mod.rs +++ b/atomic-core/src/record/workflow/record/mod.rs @@ -156,7 +156,6 @@ pub use types::{RecordedFile, RecordingResult, RecordingStats}; use crate::change::{Encoding, FileOps, Local}; use crate::crdt::{BranchId, TrunkId}; -use crate::diff::DiffOp; use crate::diff::Line; use crate::output::WorkingCopyRead; use crate::pristine::PathClaimId; @@ -614,29 +613,18 @@ fn record_modified_file_with_semantics( // can render exactly. // // The diff is computed before the force decision so the multi-line - // deletion check below can inspect its ops. + // replacement check below can inspect its ops. let comparison = compare_content(old_content, &new_content, options.get_algorithm()); - // ::26 R1 third follow-up (2026-10-13): a contiguous multi-line deletion - // region recorded against a graph whose vertices span an earlier - // insertion commit leaves the additive graph in a state the alive walk - // classifies as a cyclic conflict — the staged render emits conflict - // markers and `atomic status` reports the file modified immediately after - // a clean record. Reproduced natively (record an insertion commit, then a - // commit deleting a 38-line contiguous region: the render keeps the - // deleted lines and adds markers) and through `atomic git import`, whose - // staged verification refused the change ("staged path ... renders 27472 - // byte(s) but the commit tree expects 23819 byte(s)"). Single-line - // deletions are unaffected. Route the modify through the sentinel - // whole-file replace below — the same fail-safe shape used for binary - // content: every alive vertex is deleted and the full content inserted - // fresh, which renders byte-exact regardless of the graph's diamond - // structure. - let has_multi_line_deletion = comparison.diff_ops.iter().any(|op| match op { - DiffOp::Delete { len, .. } => *len > 1, - DiffOp::Replace { old_len, .. } => *old_len > 1, - _ => false, - }); + // Keep the conservative fallback for multi-line replacements, but not + // pure deletions. A deletion must only tombstone its targeted vertices: + // rewriting the surviving prefix/suffix makes concurrent insertions inside + // the deleted region conflict with unrelated survivor content (rubric A9). + // Structural ambiguity still requests a whole-file rewrite explicitly. + let has_multi_line_replacement = comparison + .diff_ops + .iter() + .any(|op| matches!(op, crate::diff::DiffOp::Replace { old_len, .. } if *old_len > 1)); // ::26 R1 fifth follow-up (2026-10-13): count LINES — newline-split // segments including a no-trailing-newline remainder — matching the // graph's per-line vertex split, the CRDT branch-per-line model, and the @@ -646,27 +634,17 @@ fn record_modified_file_with_semantics( let old_line_count = Line::from_bytes(old_content).len(); recorded.set_old_line_count(old_line_count); - // ::26 R1 sixth follow-up (2026-10-14): an edit at the EOF boundary of a - // file without a trailing newline — replacing or extending the final - // no-newline line, or adding one — loses or shifts the final byte in the - // per-line hunk slicing (observed: a +1-byte trailing-newline commit - // rendered 1,934 of 1,935 bytes). Either side lacking the final newline - // puts the edit boundary at EOF, where partial-hunk slicing is - // fragile; route through the sentinel whole-file replace, which - // rewrites the file byte-exactly. - // Identical content (a pure move of the file) is no edit at all: without - // this guard a moved file lacking a trailing newline took the whole-file - // replace below, re-inserting every line of content that never changed. + // A missing final newline alone does not require a whole-file rewrite. + // The hunk byte ranges below clamp to new_content.len(), including the + // final unterminated line. Rewriting unchanged lines here turns independent + // cross-view edits into overlapping replacements and duplicates their + // shared content on insert (harness 17). let content_differs = old_content != new_content.as_slice(); - let eof_boundary_edit = content_differs - && (old_content.last().is_some_and(|&b| b != b'\n') - || new_content.last().is_some_and(|&b| b != b'\n')); // A pure move of an unchanged generated file records only the move. let force_whole_file = options.get_force_whole_file_replace() || (content_differs && opaque_with_semantic_state(&detected.path, existing_trunk_id, existing_branches)) - || has_multi_line_deletion - || eof_boundary_edit; + || has_multi_line_replacement; // Whole-file-replace safety fallback: if the caller's `old_content` came // from resolving a fork or cyclic conflict (see diff --git a/atomic-core/src/record/workflow/record/tests.rs b/atomic-core/src/record/workflow/record/tests.rs index 545ab67c..98b34adc 100644 --- a/atomic-core/src/record/workflow/record/tests.rs +++ b/atomic-core/src/record/workflow/record/tests.rs @@ -796,6 +796,42 @@ fn test_record_modified_file_has_crdt_ops() { assert!(crdt_ops.line_count() > 0); } +#[test] +fn unterminated_file_edits_keep_targeted_hunks_and_complete_eof_bytes() { + for (old, new, expected) in [ + ("first\nlast", "FIRST\nlast", "FIRST\n"), + ("first\nlast", "first\nLAST", "LAST"), + ("first\nlast", "first\nlast\n", "last\n"), + ("first\nlast\n", "first\nlast", "last"), + ] { + let wc = Memory::new(); + wc.add_file("test.txt", new.as_bytes()); + let recorded = record_modified_file( + &wc, + &DetectedFile::modified("test.txt"), + old.as_bytes(), + None, + &RecordingOptions::new(), + None, + None, + ) + .unwrap(); + assert_eq!(recorded.hunks().len(), 1, "{old:?} -> {new:?}"); + let hunk = &recorded.hunks()[0]; + assert!( + !hunk.deleted_lines.contains(&usize::MAX), + "must not rewrite unchanged lines" + ); + let start = hunk.content_start.unwrap() as usize; + let end = hunk.content_end.unwrap() as usize; + assert_eq!( + &new.as_bytes()[start..end], + expected.as_bytes(), + "{old:?} -> {new:?}" + ); + } +} + #[test] fn test_record_modified_file_crdt_stats_tracks_changes() { let wc = Memory::new(); diff --git a/atomic-core/tests/merge_test.rs b/atomic-core/tests/merge_test.rs index 782f1805..093c1e92 100644 --- a/atomic-core/tests/merge_test.rs +++ b/atomic-core/tests/merge_test.rs @@ -5,7 +5,9 @@ use std::path::Path; use atomic_core::crdt::tables as crdt; use atomic_core::pristine::build_metadata::{BUILD_VERSION, CREATED_WITH_KEY, MIGRATED_WITH_KEY}; use atomic_core::pristine::merge::{merged_legacy_dir, LegacyDatabases}; -use atomic_core::pristine::schema::{LEGACY_DIR_KEY, SCHEMA_VERSION, SCHEMA_VERSION_KEY}; +use atomic_core::pristine::schema::{ + BASE_SCHEMA_VERSION, LEGACY_DIR_KEY, PROJECTED_SEMANTICS_SCHEMA_VERSION, SCHEMA_VERSION_KEY, +}; use atomic_core::pristine::tables::{ ATOMIC_META, CHANGE_META, EXTERNAL, GRAPH, PROVENANCE_STORE_META, PROVENANCE_TURNS, }; @@ -135,7 +137,7 @@ fn merge_copies_both_databases_and_records_the_legacy_dir() { let meta = txn.open_table(ATOMIC_META).unwrap(); assert_eq!( meta.get(SCHEMA_VERSION_KEY).unwrap().unwrap().value(), - SCHEMA_VERSION.to_le_bytes().as_slice() + BASE_SCHEMA_VERSION.to_le_bytes().as_slice() ); assert_eq!( meta.get(LEGACY_DIR_KEY).unwrap().unwrap().value(), @@ -168,6 +170,159 @@ fn merge_copies_both_databases_and_records_the_legacy_dir() { assert!(txn.get_internal(&hash).unwrap().is_some()); } +#[test] +fn merge_preserves_projected_semantic_schema_fence() { + use atomic_core::pristine::{CapabilityTxnT, SPAN_PROJECTED_SEMANTICS_CAPABILITY}; + let dir = tempfile::tempdir().unwrap(); + let pristine_path = dir.path().join("pristine.redb"); + let changes_path = dir.path().join("changes.redb"); + let merged_path = dir.path().join("atomic.redb"); + legacy_pristine(&pristine_path); + legacy_changes(&changes_path); + let pristine = Pristine::open(&pristine_path).unwrap(); + pristine + .require_repository_capability(SPAN_PROJECTED_SEMANTICS_CAPABILITY) + .unwrap(); + drop(pristine); + let legacy = open_sources(&[&pristine_path, &changes_path]); + legacy.merge_into(&merged_path, "fixture").unwrap(); + drop(legacy); + let merged = Pristine::open_readonly_without_recovery(&merged_path).unwrap(); + let txn = merged.read_txn().unwrap(); + assert_eq!( + txn.required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) + .unwrap(), + Some(SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version()) + ); + drop(txn); + drop(merged); + let db = Database::open(&merged_path).unwrap(); + let txn = db.begin_read().unwrap(); + let meta = txn.open_table(ATOMIC_META).unwrap(); + assert_eq!( + meta.get(SCHEMA_VERSION_KEY).unwrap().unwrap().value(), + PROJECTED_SEMANTICS_SCHEMA_VERSION.to_le_bytes().as_slice() + ); +} + +#[test] +fn merge_keeps_span_indexes_on_baseline_schema() { + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + const APPLIED: TableDefinition = TableDefinition::new("span_patch_applied_v1"); + let dir = tempfile::tempdir().unwrap(); + let source = dir.path().join("pristine.redb"); + let merged = dir.path().join("atomic.redb"); + legacy_pristine(&source); + let pristine = Pristine::open(&source).unwrap(); + pristine + .require_repository_capability(SPAN_RANGE_CAPABILITY) + .unwrap(); + drop(pristine); + let db = Database::open(&source).unwrap(); + let txn = db.begin_write().unwrap(); + txn.open_table(APPLIED).unwrap().insert(42, 1).unwrap(); + txn.commit().unwrap(); + drop(db); + let legacy = open_sources(&[&source]); + legacy.merge_into(&merged, "fixture").unwrap(); + legacy.verify_unchanged(&merged).unwrap(); + drop(legacy); + let pristine = Pristine::open_readonly_without_recovery(&merged).unwrap(); + let txn = pristine.read_txn().unwrap(); + assert_eq!( + txn.required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .unwrap(), + Some(SPAN_RANGE_CAPABILITY.minimum_version()) + ); + drop(txn); + drop(pristine); + let db = Database::open(&merged).unwrap(); + let txn = db.begin_read().unwrap(); + assert_eq!( + txn.open_table(APPLIED) + .unwrap() + .get(42) + .unwrap() + .unwrap() + .value(), + 1 + ); + assert_eq!( + txn.open_table(ATOMIC_META) + .unwrap() + .get(SCHEMA_VERSION_KEY) + .unwrap() + .unwrap() + .value(), + BASE_SCHEMA_VERSION.to_le_bytes().as_slice() + ); +} + +#[test] +fn merge_copies_span_indexes_and_detects_later_span_mutation() { + use atomic_core::pristine::{SPAN_PROJECTED_SEMANTICS_CAPABILITY, SPAN_RANGE_CAPABILITY}; + const FILES: TableDefinition<&[u8; 36], u64> = TableDefinition::new("span_patch_files_v1"); + const ROOTS: TableDefinition = TableDefinition::new("span_patch_roots_v1"); + const SPANS: TableDefinition<&[u8; 32], u8> = TableDefinition::new("span_patch_nodes_v1"); + const EDITS: MultimapTableDefinition<&[u8; 32], &[u8; 40]> = + MultimapTableDefinition::new("span_patch_edits_v1"); + const APPLIED: TableDefinition = TableDefinition::new("span_patch_applied_v1"); + let dir = tempfile::tempdir().unwrap(); + let source = dir.path().join("pristine.redb"); + let merged = dir.path().join("atomic.redb"); + legacy_pristine(&source); + let pristine = Pristine::open(&source).unwrap(); + pristine + .require_repository_capabilities(&[ + SPAN_RANGE_CAPABILITY, + SPAN_PROJECTED_SEMANTICS_CAPABILITY, + ]) + .unwrap(); + drop(pristine); + let db = Database::open(&source).unwrap(); + let txn = db.begin_write().unwrap(); + txn.open_table(FILES).unwrap().insert(&[1; 36], 7).unwrap(); + txn.open_table(ROOTS).unwrap().insert(7, &[2; 24]).unwrap(); + txn.open_table(SPANS).unwrap().insert(&[3; 32], 4).unwrap(); + txn.open_multimap_table(EDITS) + .unwrap() + .insert(&[3; 32], &[5; 40]) + .unwrap(); + txn.open_multimap_table(EDITS) + .unwrap() + .insert(&[3; 32], &[6; 40]) + .unwrap(); + txn.open_table(APPLIED).unwrap().insert(9, 1).unwrap(); + txn.commit().unwrap(); + drop(db); + let legacy = open_sources(&[&source]); + let report = legacy.merge_into(&merged, "fixture").unwrap(); + for name in [ + "span_patch_files_v1", + "span_patch_roots_v1", + "span_patch_nodes_v1", + "span_patch_edits_v1", + "span_patch_applied_v1", + ] { + assert!(report.tables.contains_key(name), "missing {name}"); + } + assert_eq!(report.tables["span_patch_edits_v1"].rows, 2); + legacy.verify_unchanged(&merged).unwrap(); + drop(legacy); + let db = Database::open(&source).unwrap(); + let txn = db.begin_write().unwrap(); + txn.open_table(SPANS).unwrap().insert(&[3; 32], 5).unwrap(); + txn.commit().unwrap(); + drop(db); + let error = open_sources(&[&source]) + .verify_unchanged(&merged) + .unwrap_err(); + assert!( + error.to_string().contains("changed after migration"), + "{error}" + ); +} + #[test] fn merge_refuses_unknown_tables_that_hold_data() { let dir = tempfile::tempdir().unwrap(); diff --git a/atomic-repository/src/apply/mod.rs b/atomic-repository/src/apply/mod.rs index 76918e3c..eb6e478e 100644 --- a/atomic-repository/src/apply/mod.rs +++ b/atomic-repository/src/apply/mod.rs @@ -491,6 +491,31 @@ fn apply_change_hunks_locked( ); } let file_ops = localized.as_deref().unwrap_or_else(|| change.file_ops()); + if change.hashed.span.is_none() + && file_ops.iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) + { + return Err(InsertError::InvalidChange { + message: "projected semantic edit requires a span change".into(), + }); + } + // Projected span edits are validated and applied by the span index + // writer in this same transaction, not by the CRDT table writer. + let regular_ops: Vec<_> = file_ops + .iter() + .filter(|ops| { + !matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) + .cloned() + .collect(); + let file_ops = regular_ops.as_slice(); let file_ops_count = file_ops.len(); log::debug!( "write_change_to_graph: applying {} FileOps to CRDT tables", diff --git a/atomic-repository/src/diff_engine.rs b/atomic-repository/src/diff_engine.rs index 34298dd7..923cd8b7 100644 --- a/atomic-repository/src/diff_engine.rs +++ b/atomic-repository/src/diff_engine.rs @@ -1416,6 +1416,59 @@ pub fn change_file_diffs( change_hash: &Hash, config: &DiffOutputConfig, ) -> Result<(Vec, DiffStats), crate::error::RepositoryError> { + let projected: Vec<_> = change + .file_ops() + .iter() + .filter(|ops| matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. }))) + .collect(); + if !projected.is_empty() { + let mut ordinary = change.clone(); + ordinary + .hashed + .file_ops + .retain(|ops| !matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. }))); + let (mut files, mut stats) = change_file_diffs_with(&ordinary, config, |path| { + repo.get_file_content_before_change(path, change_hash) + .ok() + .flatten() + })?; + for ops in projected { + let path = ops.path(); + let before = repo + .get_file_content_before_change(path, change_hash)? + .ok_or_else(|| crate::error::RepositoryError::InvalidOperation { + message: format!("projected span file '{path}' is absent before the change"), + })?; + let after = repo + .get_file_content_after_change(path, change_hash)? + .ok_or_else(|| crate::error::RepositoryError::InvalidOperation { + message: format!("projected span file '{path}' is absent after the change"), + })?; + let mut file_diff = FileDiff::modified(path); + if before.contains(&0) + || after.contains(&0) + || std::str::from_utf8(&before).is_err() + || std::str::from_utf8(&after).is_err() + { + file_diff.is_binary = true; + } else { + let result = diff_text(&before, &after, Algorithm::Myers); + if !result.is_unchanged() { + let old_lines: Vec<_> = before.split(|&byte| byte == b'\n').collect(); + let new_lines: Vec<_> = after.split(|&byte| byte == b'\n').collect(); + for hunk in + build_hunks_from_diff(&result, &old_lines, &new_lines, config.context_lines) + { + file_diff.add_hunk(hunk); + } + } + } + file_diff.compute_stats(); + stats.add_file(file_diff.stats.clone()); + files.push(file_diff); + } + return Ok((files, stats)); + } // The before-content fetch: the file's recorded state prior to the // change, read from the graph. The wire path has no such fetch — it // passes a `None` resolver and the hunks render zero-context, the @@ -1605,6 +1658,11 @@ where let trunk_op = ops.trunk_op(); let line_ops = ops.line_ops(); + if matches!(trunk_op, Some(TrunkOp::ProjectedSpanEdit { .. })) { + return Err(crate::error::RepositoryError::InvalidOperation { + message: "projected span edit requires a span-aware semantic diff".into(), + }); + } // Determine file change status from trunk operation let change_status = match trunk_op { @@ -1613,7 +1671,8 @@ where Some(TrunkOp::Move { .. }) => FileChangeStatus::Renamed, Some(TrunkOp::Undelete { .. }) | Some(TrunkOp::SetMode { .. }) - | Some(TrunkOp::SetKind { .. }) => FileChangeStatus::Modified, + | Some(TrunkOp::SetKind { .. }) + | Some(TrunkOp::ProjectedSpanEdit { .. }) => FileChangeStatus::Modified, None => FileChangeStatus::Modified, }; diff --git a/atomic-repository/src/history/operations.rs b/atomic-repository/src/history/operations.rs index c96c4ff2..ca4f668b 100644 --- a/atomic-repository/src/history/operations.rs +++ b/atomic-repository/src/history/operations.rs @@ -386,6 +386,16 @@ pub fn get_files_in_change(change: &Change) -> Vec { files.insert(path.to_string()); } } + // Projected span edits have no graph content hunk: their semantic FileOp + // is the portable path needed to fetch before/after bytes for review. + for ops in change.file_ops() { + if matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) { + files.insert(ops.path().to_string()); + } + } files.into_iter().collect() } diff --git a/atomic-repository/src/redb_change_store/tests.rs b/atomic-repository/src/redb_change_store/tests.rs index b3767c1d..bda8a898 100644 --- a/atomic-repository/src/redb_change_store/tests.rs +++ b/atomic-repository/src/redb_change_store/tests.rs @@ -62,6 +62,75 @@ fn typed_span_survives_redb_reopen_and_export() { assert_eq!(receiver.load_change(&hash).unwrap().hashed, change.hashed); } +#[test] +fn projected_semantic_span_survives_redb_reopen_and_export() { + use atomic_core::change::span_patch::{ + ExternalFile, ExternalSpan, SpanOp, SpanPatch, SpanProjectionId, + }; + let owner = Hash::of(b"historical change"); + let file = ExternalFile { owner, ordinal: 3 }; + let baseline = SpanProjectionId(Hash::of(b"verified semantic baseline")); + let mut change = Change::new( + ChangeHeader::new("native span edit"), + vec![], + b"new".to_vec(), + vec![owner], + ); + change.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Edit { + file: file.clone(), + target: ExternalSpan { + owner, + start: 0, + end: 4, + }, + start: 0, + end: 3, + insert_start: 0, + insert_end: 3, + }], + )); + change + .hashed + .file_ops + .push(atomic_core::change::FileOps::projected_span_edit( + "file.txt".into(), + file, + baseline, + )); + let mut original = Vec::new(); + let original_hash = change.serialize(&mut original).unwrap(); + let (dir, store) = temp_store(); + let hash = store.import_v3_bytes(&original).unwrap(); + assert_eq!(hash, *original_hash.as_bytes()); + drop(store); + let store = RedbChangeStore::open_existing(dir.path().join("test_change_store.redb")).unwrap(); + assert!(matches!( + store.load_graph_sections(&hash), + Err(RedbStoreError::UnsupportedSpanSelectiveRead) + )); + assert!(matches!( + store.load_semantic_sections(&hash), + Err(RedbStoreError::UnsupportedSpanSelectiveRead) + )); + assert!(matches!( + store.load_content_chunks(&hash), + Err(RedbStoreError::UnsupportedSpanSelectiveRead) + )); + let exported = store.export_v3_bytes(&hash).unwrap(); + let (loaded, exported_hash) = Change::deserialize(&mut Cursor::new(&exported)).unwrap(); + assert_eq!(exported_hash, original_hash); + assert_eq!(loaded.hashed, change.hashed); + assert_eq!(loaded.contents, change.contents); + assert!( + matches!(loaded.file_ops()[0].trunk_op(), Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { baseline: saved, .. }) if *saved == baseline) + ); + let (_receiver_dir, receiver) = temp_store(); + assert_eq!(receiver.import_v3_bytes(&exported).unwrap(), hash); + assert_eq!(receiver.load_change(&hash).unwrap().hashed, change.hashed); +} + #[test] fn altered_span_cannot_be_exported_under_its_original_hash() { use atomic_core::change::span_patch::{SpanOp, SpanPatch}; diff --git a/atomic-repository/src/repository/changes.rs b/atomic-repository/src/repository/changes.rs index 5c06c4e0..1343576c 100644 --- a/atomic-repository/src/repository/changes.rs +++ b/atomic-repository/src/repository/changes.rs @@ -22,6 +22,39 @@ fn atom_change_file_version(bytes: &[u8]) -> Result { } impl Repository { + fn require_projected_semantic_storage_capability( + &self, + change: &Change, + ) -> Result<(), RepositoryError> { + if !change.file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) { + return Ok(()); + } + if change.hashed.span.is_none() { + return Err(RepositoryError::InvalidOperation { + message: "projected semantic edit requires a span change".into(), + }); + } + use atomic_core::pristine::{CapabilityTxnT, SPAN_PROJECTED_SEMANTICS_CAPABILITY}; + if self + .pristine + .read_txn()? + .required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id())? + != Some(SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version()) + { + return Err(RepositoryError::InvalidOperation { + message: + "storing projected semantic edits requires the projected semantic capability" + .into(), + }); + } + Ok(()) + } + fn require_span_storage_capability(&self) -> Result<(), RepositoryError> { use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; let txn = self.pristine.read_txn()?; @@ -130,6 +163,19 @@ impl Repository { /// println!("Saved change: {}", hash.to_base32()); /// ``` pub fn save_change(&self, change: &Change) -> Result { + self.require_projected_semantic_storage_capability(change)?; + if change.file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) { + let mut bytes = Vec::new(); + let hash = change + .serialize(&mut bytes) + .map_err(|error| RepositoryError::Serialization(error.to_string()))?; + return self.save_change_bytes(&hash, &bytes, change); + } if change.hashed.span.is_some() { self.require_span_storage_capability()?; } @@ -156,6 +202,23 @@ impl Repository { computed.to_base32() ))); } + let projected = change.file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }); + if projected && cfg!(not(unix)) { + return Err(RepositoryError::InvalidOperation { + message: "projected semantic change import is not supported on this platform" + .into(), + }); + } + if projected && change.hashed.span.is_none() { + return Err(RepositoryError::InvalidOperation { + message: "projected semantic edit requires a span change".into(), + }); + } if change.hashed.span.is_some() { use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; let txn = self.pristine.read_txn()?; @@ -169,6 +232,11 @@ impl Repository { }), } } + if projected { + self.pristine.require_repository_capability( + atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY, + )?; + } let stored = self.save_change_bytes(hash, bytes, &change)?; self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?; if !change.kind().is_snapshot() { @@ -198,7 +266,7 @@ impl Repository { /// /// * `hash` - The content hash (from the original serialization) /// * `change_bytes` - The exact ATOM bytes to write to disk - /// * `_change` - The deserialized Change (unused, kept for API compatibility) + /// * `change` - The deserialized change used for capability checks pub(crate) fn save_change_bytes( &self, hash: &Hash, @@ -206,6 +274,7 @@ impl Repository { change: &Change, ) -> Result { let version = atom_change_file_version(change_bytes)?; + self.require_projected_semantic_storage_capability(change)?; if version == ATOM_CHANGE_FILE_VERSION_SPAN { let (decoded, actual_hash) = Change::deserialize(&mut std::io::Cursor::new(change_bytes)) @@ -234,8 +303,9 @@ impl Repository { &self, hash: &Hash, change_bytes: &[u8], - _change: &Change, + change: &Change, ) -> Result { + self.require_projected_semantic_storage_capability(change)?; match atom_change_file_version(change_bytes)? { ATOM_CHANGE_FILE_VERSION_V1 | ATOM_CHANGE_FILE_VERSION_V2 | ATOM_CHANGE_FILE_VERSION_SPAN => {} version => { @@ -249,6 +319,15 @@ impl Repository { if let Some(parent) = change_path.parent() { std::fs::create_dir_all(parent)?; } + if change.file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) { + save_projected_change_without_overwrite(&change_path, change_bytes)?; + return Ok(*hash); + } std::fs::write(&change_path, change_bytes)?; Ok(*hash) } @@ -1788,3 +1867,162 @@ impl Repository { .unwrap_or(false) } } + +#[cfg(unix)] +fn save_projected_change_without_overwrite( + path: &Path, + bytes: &[u8], +) -> Result<(), RepositoryError> { + use std::io::Write; + fn verify_existing(path: &Path, expected: &[u8]) -> Result<(), RepositoryError> { + let metadata = std::fs::symlink_metadata(path)?; + if !metadata.file_type().is_file() || std::fs::read(path)? != expected { + return Err(RepositoryError::InvalidOperation { + message: format!("projected change object '{}' already exists with different bytes; refusing to overwrite it", path.display()), + }); + } + // A prior process may have linked the complete object but died before + // syncing the containing directory. Finish that durability step when + // reusing an identical object. + std::fs::File::open(path)?.sync_all()?; + let parent = path + .parent() + .ok_or_else(|| RepositoryError::InvalidOperation { + message: "projected change path has no parent".into(), + })?; + std::fs::File::open(parent)?.sync_all()?; + Ok(()) + } + match std::fs::symlink_metadata(path) { + Ok(_) => return verify_existing(path, bytes), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(error.into()), + } + let parent = path + .parent() + .ok_or_else(|| RepositoryError::InvalidOperation { + message: "projected change path has no parent".into(), + })?; + if let Some(ancestor) = parent.parent() { + // The hash-prefix directory may have just been created. Persist its + // entry before publishing an object within it. + std::fs::File::open(ancestor)?.sync_all()?; + } + let mut temporary = tempfile::NamedTempFile::new_in(parent)?; + temporary.write_all(bytes)?; + temporary.as_file().sync_all()?; + #[cfg(all(test, unix))] + if std::env::var_os("ATOMIC_TEST_PROJECTED_STORE_PAUSE").is_some() { + println!("ATOMIC_PROJECTED_STORE_STAGED"); + std::io::stdout().flush()?; + loop { + std::thread::park(); + } + } + match temporary.persist_noclobber(path) { + Ok(_) => { + std::fs::File::open(parent)?.sync_all()?; + Ok(()) + } + Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => { + verify_existing(path, bytes) + } + Err(error) => Err(error.error.into()), + } +} + +#[cfg(not(unix))] +fn save_projected_change_without_overwrite( + _path: &Path, + _bytes: &[u8], +) -> Result<(), RepositoryError> { + Err(RepositoryError::InvalidOperation { + message: "durable projected change publication is not supported on this platform".into(), + }) +} + +#[cfg(all(test, unix))] +mod projected_store_tests { + use super::save_projected_change_without_overwrite; + use std::io::BufRead; + use std::os::unix::process::ExitStatusExt; + + #[test] + fn staged_projected_change_survives_process_kill_without_partial_publication() { + const ROOT: &str = "ATOMIC_TEST_PROJECTED_STORE_ROOT"; + if let Some(root) = std::env::var_os(ROOT) { + let path = std::path::PathBuf::from(root).join("object.change"); + save_projected_change_without_overwrite(&path, b"complete change bytes").unwrap(); + panic!("child did not pause before object publication"); + } + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("object.change"); + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "repository::changes::projected_store_tests::staged_projected_change_survives_process_kill_without_partial_publication", "--nocapture"]) + .env(ROOT, dir.path()) + .env("ATOMIC_TEST_PROJECTED_STORE_PAUSE", "1") + .stdout(std::process::Stdio::piped()).spawn().unwrap(); + let stdout = child.stdout.take().unwrap(); + let (send, receive) = std::sync::mpsc::channel(); + let reader = std::thread::spawn(move || { + for line in std::io::BufReader::new(stdout).lines() { + match line { + Ok(line) if line.contains("ATOMIC_PROJECTED_STORE_STAGED") => { + let _ = send.send(Ok(())); + return; + } + Ok(_) => {} + Err(error) => { + let _ = send.send(Err(error.to_string())); + return; + } + } + } + let _ = send.send(Err("child exited before staging marker".to_string())); + }); + let ready = receive.recv_timeout(std::time::Duration::from_secs(60)); + let killed = child.kill(); + let status = child.wait().unwrap(); + if ready.is_ok() { + reader.join().unwrap(); + } + ready + .expect("child timed out") + .expect("child did not stage object"); + assert!(killed.is_ok() && status.signal() == Some(9)); + assert!(!path.exists(), "partial object must not be published"); + save_projected_change_without_overwrite(&path, b"complete change bytes").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"complete change bytes"); + save_projected_change_without_overwrite(&path, b"complete change bytes").unwrap(); + assert!(save_projected_change_without_overwrite(&path, b"different bytes").is_err()); + assert_eq!(std::fs::read(&path).unwrap(), b"complete change bytes"); + } + + #[test] + fn concurrent_projected_publication_never_overwrites_the_winner() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("object.change"); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(2)); + let handles: Vec<_> = [b"first object".as_slice(), b"second object".as_slice()] + .into_iter() + .map(|bytes| { + let path = path.clone(); + let barrier = barrier.clone(); + std::thread::spawn(move || { + barrier.wait(); + (bytes, save_projected_change_without_overwrite(&path, bytes)) + }) + }) + .collect(); + let results: Vec<_> = handles + .into_iter() + .map(|handle| handle.join().unwrap()) + .collect(); + assert_eq!( + results.iter().filter(|(_, result)| result.is_ok()).count(), + 1 + ); + let winner = results.iter().find(|(_, result)| result.is_ok()).unwrap().0; + assert_eq!(std::fs::read(path).unwrap(), winner); + } +} diff --git a/atomic-repository/src/repository/content.rs b/atomic-repository/src/repository/content.rs index 6f6f6173..fa03ff60 100644 --- a/atomic-repository/src/repository/content.rs +++ b/atomic-repository/src/repository/content.rs @@ -913,6 +913,55 @@ pub(super) fn retrieve_span_if_present( inode: Inode, visibility: &GraphVisibilityClosure, ) -> Result>, RepositoryError> +where + T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CapabilityTxnT, + C: atomic_core::change::ChangeStore, +{ + if !has_visible_span_root(txn, store, inode, visibility)? { + return Ok(None); + } + atomic_core::pristine::span_patch_storage::SpanRenderContext::new(txn, store, visibility) + .and_then(|context| context.render(inode)) + .map(Some) + .map_err(|error| RepositoryError::Database(error.to_string())) +} + +pub(super) fn retrieve_span_bounded_if_present( + txn: &T, + store: &C, + inode: Inode, + visibility: &GraphVisibilityClosure, + max_bytes: usize, +) -> Result, RepositoryError> +where + T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CapabilityTxnT, + C: atomic_core::change::ChangeStore, +{ + use atomic_core::pristine::span_patch_storage::{BoundedSpanContent, SpanRenderContext}; + if !has_visible_span_root(txn, store, inode, visibility)? { + return Ok(None); + } + let rendered = SpanRenderContext::new(txn, store, visibility) + .and_then(|context| context.render_bounded(inode, max_bytes)) + .map_err(|error| RepositoryError::Database(error.to_string()))?; + Ok(Some(match rendered { + BoundedSpanContent::Complete(bytes) => BoundedContent::Complete(bytes), + BoundedSpanContent::Oversized { retained_bytes } => { + BoundedContent::Oversized { retained_bytes } + } + })) +} + +pub(super) fn has_visible_span_root( + txn: &T, + store: &C, + inode: Inode, + visibility: &GraphVisibilityClosure, +) -> Result where T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + atomic_core::pristine::TreeTxnT @@ -949,7 +998,7 @@ where } } } - return Ok(None); + return Ok(false); }; if !matches!(capability, Some(2) | Some(3)) { return Err(RepositoryError::Database( @@ -961,10 +1010,7 @@ where "span root is outside the requested view".into(), )); } - atomic_core::pristine::span_patch_storage::SpanRenderContext::new(txn, store, visibility) - .and_then(|context| context.render(inode)) - .map(Some) - .map_err(|error| RepositoryError::Database(error.to_string())) + Ok(true) } pub(crate) fn retrieve_content_with_filter_fast( diff --git a/atomic-repository/src/repository/git_stream.rs b/atomic-repository/src/repository/git_stream.rs index df25580f..9d9da274 100644 --- a/atomic-repository/src/repository/git_stream.rs +++ b/atomic-repository/src/repository/git_stream.rs @@ -4,15 +4,18 @@ use super::operation::{current_operation_timestamp_ms, working_copy_state_ref}; use super::synthesis::{finalize_classified_change, git_oid_hex, SynthesisClassification}; use super::{ - GitSynthesisOrigin, ProjectTree, Repository, RepositoryError, StagedExpectation, - StagedPathExpectation, StagedTreeExpectation, VerifiedProspectiveEquivalence, + ConversionPolicy, GitSynthesisOrigin, ProjectTree, Repository, RepositoryError, + StagedExpectation, StagedPathExpectation, StagedTreeExpectation, + VerifiedProspectiveEquivalence, }; use atomic_core::change::{Change, GraphOp}; use atomic_core::operation::{ ActorRef, MetadataTarget, MetadataTransition, MetadataValue, OperationKind, RepoStateRef, ViewStateRef, }; -use atomic_core::pristine::{GitShaIndexMutTxnT, GraphTxnT, MutTxnT, ViewScope, ViewTxnT}; +use atomic_core::pristine::{ + GitShaIndexMutTxnT, GitShaIndexTxnT, GraphTxnT, MutTxnT, ViewScope, ViewTxnT, +}; use atomic_core::types::{Hash, Merkle}; use std::collections::{BTreeMap, HashSet}; @@ -22,6 +25,281 @@ fn invalid(message: impl Into) -> RepositoryError { } } +fn verify_incremental_baseline( + repo: &Repository, + view: &str, + policy: &ConversionPolicy, + expected: &atomic_core::operation::GitObjectId, +) -> Result, RepositoryError> { + let project = repo + .project_tree(view, policy) + .map_err(|error| invalid(format!("incremental baseline projection failed: {error}")))?; + super::verify_prospective_equivalence(&project, expected) + .map_err(|error| invalid(format!("incremental baseline tree mismatch: {error}")))?; + let mut retained = BTreeMap::new(); + for entry in &project.manifest.entries { + if entry.disposition == super::ManifestDisposition::Included { + continue; + } + let path = match std::str::from_utf8(entry.path.as_bytes()) { + Ok(path) => crate::canonical_path_identity(path), + Err(_) => crate::escape_repo_path(entry.path.as_bytes()), + }; + if retained + .insert( + path, + StagedPathExpectation { + bytes: entry.repository_bytes.to_vec(), + mode: entry.mode, + kind: entry.kind, + }, + ) + .is_some() + { + return Err(invalid( + "incremental baseline has duplicate private path identities", + )); + } + } + Ok(retained) +} + +fn is_atomicignore_bootstrap(change: &Change) -> bool { + use atomic_core::change::span_patch::SpanOp; + if !change.origin().is_native() || !change.dependencies().is_empty() { + return false; + } + let Some(patch) = &change.hashed.span else { + return false; + }; + if !matches!(patch.ops.as_slice(), [SpanOp::Root { path, .. }] if path == ".atomicignore") { + return false; + } + let mut creators = change.file_ops().iter().filter(|ops| ops.is_create()); + let Some(creator) = creators.next() else { + return false; + }; + if creators.next().is_some() + || creator.path() != ".atomicignore" + || !creator.trunk_id().change_id().is_root() + { + return false; + } + for ops in change.file_ops() { + if ops.path() != ".atomicignore" || ops.trunk_id() != creator.trunk_id() { + return false; + } + match ops.trunk_op() { + Some(atomic_core::crdt::TrunkOp::Create { path, .. }) if path == ".atomicignore" => {} + Some( + atomic_core::crdt::TrunkOp::SetMode { trunk, .. } + | atomic_core::crdt::TrunkOp::SetKind { trunk, .. }, + ) if *trunk == creator.trunk_id() && ops.line_ops().is_empty() => {} + _ => return false, + } + } + let mut inode = None; + for hunk in change.hunks() { + match hunk { + GraphOp::FileAdd { + path, + add_inode, + contents: None, + .. + } if path == ".atomicignore" && inode.is_none() => inode = Some(add_inode.start), + GraphOp::SetAttr { + path, + inode: attr_inode, + .. + } if path == ".atomicignore" + && attr_inode.change.is_none() + && Some(attr_inode.pos) == inode => {} + _ => return false, + } + } + inode.is_some() +} + +fn verified_incremental_stage_batches( + repo: &Repository, + working_copy: atomic_core::WorkingCopyId, + stage: &str, + base_len: usize, + staged: &[(String, Hash)], +) -> Result { + use super::OperationVerificationState; + use atomic_core::operation::OperationScope; + let working = repo.operation_log(OperationScope::WorkingCopy(working_copy), None, false)?; + let global = repo.operation_log(OperationScope::Repository, None, false)?; + let global_ids: HashSet<_> = global + .entries + .iter() + .map(|entry| entry.operation.id()) + .collect(); + let mut batches = HashSet::new(); + for (index, (_, hash)) in staged.iter().enumerate() { + let sequence = u64::try_from( + base_len + .checked_add(index) + .ok_or_else(|| invalid("stage sequence overflow"))?, + ) + .map_err(|_| invalid("stage sequence overflow"))?; + let entry = working.entries.iter().find(|entry| { + let payload = entry.operation.payload(); + entry.verification == OperationVerificationState::Verified + && global_ids.contains(&entry.operation.id()) + && payload.kind == OperationKind::SynthesizeGit + && matches!(&payload.actor, ActorRef::System { name } if name == "git-direct-stream-private") + && payload.evidence.contains(hash) + && payload.delta.metadata.iter().any(|transition| { + matches!(&transition.target, MetadataTarget::ViewChange { view, change } if view == stage && change == hash) + && transition.expected_old == MetadataValue::Absent + && transition.expected_new == MetadataValue::Sequence(sequence) + }) + }).ok_or_else(|| invalid(format!("staged change {hash} lacks a verified private-batch operation")))?; + batches.insert(entry.operation.id()); + } + Ok(batches.len()) +} + +fn recovered_private_stage_change( + working: &super::OperationLog, + global: &super::OperationLog, + stage: &str, + hash: &Hash, + sequence: u64, +) -> bool { + use super::OperationVerificationState; + let global_ids: HashSet<_> = global + .entries + .iter() + .map(|entry| entry.operation.id()) + .collect(); + working.entries.iter().any(|interrupted| { + let original = interrupted.operation.payload(); + if interrupted.verification == OperationVerificationState::Verified + || !global_ids.contains(&interrupted.operation.id()) + || original.kind != OperationKind::SynthesizeGit + || !matches!(&original.actor, ActorRef::System { name } if name == "git-direct-stream-private") + || !original.evidence.contains(hash) + || !original.delta.metadata.iter().any(|transition| { + matches!(&transition.target, MetadataTarget::ViewChange { view, change } if view == stage && change == hash) + && transition.expected_old == MetadataValue::Absent + && transition.expected_new == MetadataValue::Sequence(sequence) + }) + { + return false; + } + working.entries.iter().any(|recovery| { + let inverse = recovery.operation.payload(); + recovery.verification == OperationVerificationState::Verified + && global_ids.contains(&recovery.operation.id()) + && inverse.kind == OperationKind::Recover + && inverse.parents.contains(&interrupted.operation.id()) + && inverse.before == original.delta.after + && inverse.delta.after == original.before + && inverse.delta.metadata.iter().any(|transition| { + matches!(&transition.target, MetadataTarget::ViewChange { view, change } if view == stage && change == hash) + && transition.expected_old == MetadataValue::Sequence(sequence) + && transition.expected_new == MetadataValue::Absent + }) + }) + }) +} + +impl Repository { + /// Validate a replayed first-parent span prefix against the persisted view. + /// + /// This verifies content identities, Git origins, indexed dependencies and + /// exact persisted interpretation closures. It does not verify the Git + /// tree, working-copy cleanliness, or hold a lease after returning. An + /// importer must check those separately and revalidate under its operation + /// locks before it publishes any changes. + pub fn validate_span_git_prefix( + &self, + view_name: &str, + replayed: &[(String, Hash)], + ) -> Result<(), RepositoryError> { + use atomic_core::change::ChangeOrigin; + use atomic_core::pristine::{ + CapabilityTxnT, GitCommitClosureTxnT, GitShaIndexTxnT, SPAN_RANGE_CAPABILITY, + }; + + if replayed.is_empty() { + return Err(invalid("span Git prefix must not be empty")); + } + let txn = self.pristine.read_txn()?; + if txn.required_capability_version(SPAN_RANGE_CAPABILITY.id())? + != Some(SPAN_RANGE_CAPABILITY.minimum_version()) + { + return Err(invalid("span capability is missing or unsupported")); + } + let view = txn + .get_view(view_name)? + .ok_or_else(|| invalid("span Git prefix view does not exist"))?; + let visibility = super::filter::graph_visibility_closure(&txn, &view)?; + let mut hashes = Vec::with_capacity(replayed.len()); + let mut previous: Option<&str> = None; + let mut seen = HashSet::new(); + for (sha, expected) in replayed { + if !seen.insert(*expected) { + return Err(invalid("span Git prefix contains a duplicate change")); + } + let id = txn + .get_by_git_sha(sha)? + .ok_or_else(|| invalid(format!("Git prefix commit {sha} is not indexed")))?; + if !visibility.contains(id) + || txn.get_external(id)? != Some(*expected) + || !txn.was_change_applied(id)? + { + return Err(invalid(format!( + "Git prefix commit {sha} differs from the selected view" + ))); + } + let change = self.load_change(expected)?; + if change.hash().map_err(|error| invalid(error.to_string()))? != *expected + || change.hashed.span.is_none() + || !change.file_ops().is_empty() + { + return Err(invalid(format!( + "Git prefix commit {sha} is not the expected graph-only span change" + ))); + } + match change.origin() { + ChangeOrigin::GitSynthesized { + commit, parents, .. + } if git_oid_hex(commit) == *sha + && parents.first().map(git_oid_hex).as_deref() == previous => {} + _ => { + return Err(invalid(format!( + "Git prefix commit {sha} has a different first-parent origin" + ))) + } + } + let mut indexed = txn.get_change_deps(id)?; + let mut declared = change.dependencies().to_vec(); + indexed.sort(); + declared.sort(); + if indexed != declared { + return Err(invalid(format!( + "Git prefix commit {sha} has inconsistent dependencies" + ))); + } + hashes.push(*expected); + let mut expected_closure = hashes.clone(); + expected_closure.sort(); + expected_closure.dedup(); + if txn.get_git_commit_closure(sha)? != Some(expected_closure) { + return Err(invalid(format!( + "Git prefix commit {sha} has a different interpretation closure" + ))); + } + previous = Some(sha); + } + Ok(()) + } +} + /// Bounds compiled transaction payload, not total current-tree/compiler memory. #[derive(Clone, Copy, Debug)] pub struct GitStreamLimits { @@ -56,11 +334,46 @@ pub struct PreparedGitGraphChange { } impl PreparedGitGraphChange { pub fn new( + change: Change, + origin: GitSynthesisOrigin, + metadata: Vec, + unhashed: serde_json::Value, + ) -> Result { + Self::prepare(change, origin, metadata, unhashed, false) + } + + /// Prepare a typed span change while preserving its opaque Git facts. + pub fn new_span( + change: Change, + origin: GitSynthesisOrigin, + metadata: Vec, + unhashed: serde_json::Value, + ) -> Result { + Self::prepare(change, origin, metadata, unhashed, true) + } + + fn prepare( change: Change, origin: GitSynthesisOrigin, metadata: Vec, mut unhashed: serde_json::Value, + span_mode: bool, ) -> Result { + if change.hashed.span.is_some() != span_mode { + return Err(invalid("stream change format does not match producer mode")); + } + if span_mode { + let patch = change.hashed.span.as_ref().expect("span mode checked"); + if !patch.prior_metadata.is_empty() || !change.hashed.metadata.is_empty() { + return Err(invalid( + "span compiler must not prepopulate foreign metadata", + )); + } + } else if change.hashed.metadata.starts_with(b"ATSP") { + return Err(invalid( + "opaque span-prefixed metadata is not accepted by the graph stream", + )); + } if !change.file_ops().is_empty() { return Err(invalid("direct graph stream does not import native CRDT identities; eager semantics are unsupported")); } @@ -128,11 +441,52 @@ pub struct GitGraphImportSession<'a> { stage: String, target: String, limits: GitStreamLimits, + target_id: u64, + target_state: Merkle, + target_count: u64, + baseline_git_tree: Option<(ConversionPolicy, atomic_core::operation::GitObjectId)>, + retained_private: BTreeMap, + base_prefix: Vec<(String, Hash)>, + base_hashes: Vec, hashes: Vec, last_sha: Option, last_hash: Option, batches: usize, publish_seconds: f64, + span_mode: bool, +} + +#[cfg(all(test, unix))] +fn pause_incremental_batch_for_test( + operation: atomic_core::OperationId, + entries: &[PreparedGitGraphChange], + window: &str, +) { + use atomic_core::types::Base32; + use std::io::Write; + if std::env::var("ATOMIC_TEST_INCREMENTAL_WINDOW") + .ok() + .as_deref() + != Some(window) + || std::env::var("ATOMIC_TEST_INCREMENTAL_SHA").ok().as_deref() + != entries.first().map(|entry| entry.sha.as_str()) + { + return; + } + let hashes = entries + .iter() + .map(|entry| entry.hash.to_base32()) + .collect::>() + .join(" "); + println!( + "ATOMIC_INCREMENTAL_READY {} {}", + operation.to_base32(), + hashes + ); + std::io::stdout().flush().expect("flush crash marker"); + loop { + std::thread::park(); + } } impl<'a> GitGraphImportSession<'a> { pub fn begin( @@ -160,11 +514,215 @@ impl<'a> GitGraphImportSession<'a> { stage, target, limits, + target_id: view.id, + target_state: view.state, + target_count: view.change_count, + baseline_git_tree: None, + retained_private: BTreeMap::new(), + base_prefix: Vec::new(), + base_hashes: Vec::new(), hashes: Vec::new(), last_sha: None, last_hash: None, batches: 0, publish_seconds: 0.0, + span_mode: false, + }) + } + + /// Begin span-backed streaming into a fresh, untracked repository. + pub fn begin_span( + repo: &'a mut Repository, + limits: GitStreamLimits, + ) -> Result { + use atomic_core::pristine::TreeTxnT; + let working_copy = repo.require_working_copy_id()?; + let _lock = repo.try_lock_operation(working_copy)?; + { + let txn = repo.pristine.read_txn()?; + let first = txn.iter_tree()?.next().transpose()?; + if let Some((path, _)) = first { + return Err(invalid(format!( + "span stream requires a fresh repository without tracked or staged paths; found '{path}'" + ))); + } + } + let mut session = Self::begin(repo, limits)?; + session + .repo + .pristine + .require_repository_capability(atomic_core::pristine::SPAN_RANGE_CAPABILITY)?; + session.span_mode = true; + Ok(session) + } + + /// Continue a direct span stream over an unchanged imported prefix, + /// optionally retaining its native `.atomicignore` bootstrap change. + /// + /// The original private stage is reused, not recreated. This deliberately + /// refuses other native changes, inherited members, or a partial prior + /// stage. The supplied baseline tree must be obtained from the pinned Git + /// commit; the repository projection is checked against it under the + /// operation lock. This method does not change or verify working bytes. + /// The CLI does not yet expose this restricted path. + pub fn begin_span_incremental( + repo: &'a mut Repository, + limits: GitStreamLimits, + replayed: &[(String, Hash)], + policy: &ConversionPolicy, + expected_git_tree: &atomic_core::operation::GitObjectId, + ) -> Result { + Self::begin_span_incremental_inner(repo, limits, replayed, &[], policy, expected_git_tree) + } + + /// Resume a previously verified private stage using the exact replayed + /// staged suffix. No unverified batch or mismatched SHA is reused. + pub fn resume_span_incremental( + repo: &'a mut Repository, + limits: GitStreamLimits, + replayed: &[(String, Hash)], + staged: &[(String, Hash)], + policy: &ConversionPolicy, + expected_git_tree: &atomic_core::operation::GitObjectId, + ) -> Result { + if staged.is_empty() { + return Err(invalid( + "resuming an incremental span stage requires staged changes", + )); + } + Self::begin_span_incremental_inner( + repo, + limits, + replayed, + staged, + policy, + expected_git_tree, + ) + } + + fn begin_span_incremental_inner( + repo: &'a mut Repository, + limits: GitStreamLimits, + replayed: &[(String, Hash)], + staged: &[(String, Hash)], + policy: &ConversionPolicy, + expected_git_tree: &atomic_core::operation::GitObjectId, + ) -> Result { + let working_copy = repo.require_working_copy_id()?; + let lock = repo.try_lock_operation(working_copy)?; + let target_name = repo.current_view().to_string(); + repo.validate_span_git_prefix(&target_name, replayed)?; + let stage_name = "git-stream-private".to_string(); + let txn = repo.pristine.read_txn()?; + let target = txn + .get_view(&target_name)? + .ok_or_else(|| invalid("incremental stream target missing"))?; + let stage = txn + .get_view(&stage_name)? + .ok_or_else(|| invalid("incremental stream stage missing"))?; + let base_hashes: Vec<_> = replayed.iter().map(|(_, hash)| *hash).collect(); + let members = + |view: &atomic_core::pristine::ViewState| -> Result, RepositoryError> { + txn.iter_changes(view, 0)? + .map(|row| { + let (_, id, _) = row?; + txn.get_external(id)? + .ok_or_else(|| invalid("stream member has no hash")) + }) + .collect() + }; + let staged_hashes: Vec<_> = staged.iter().map(|(_, hash)| *hash).collect(); + let expected_stage_members: Vec<_> = + base_hashes.iter().chain(&staged_hashes).copied().collect(); + let expected_state = expected_stage_members + .iter() + .fold(Merkle::ZERO, |state, hash| state.next(hash)); + let visibility = super::filter::graph_visibility_closure(&txn, &target)?; + let target_members = members(&target)?; + let mut expected_index = 0usize; + let mut bootstrap_count = 0usize; + for hash in &target_members { + if base_hashes.get(expected_index) == Some(hash) { + expected_index += 1; + } else { + let change = repo.load_change(hash)?; + if change.hash().map_err(|error| invalid(error.to_string()))? != *hash + || !is_atomicignore_bootstrap(&change) + { + return Err(invalid("incremental stream requires an unchanged Git-only target plus optional .atomicignore bootstrap")); + } + bootstrap_count += 1; + } + } + if stage.parent != Some(target.id) + || stage.kind != ViewScope::Draft + || members(&stage)? != expected_stage_members + || expected_index != base_hashes.len() + || bootstrap_count > 1 + || stage.state != expected_state + || visibility + .iter_dependency_first() + .filter(|id| !id.is_root()) + .count() + != target_members.len() + { + return Err(invalid( + "incremental stream requires an unchanged Git-only target and private stage", + )); + } + drop(txn); + let resumed_batches = if staged.is_empty() { + 0 + } else { + let mut all = replayed.to_vec(); + all.extend_from_slice(staged); + repo.validate_span_git_prefix(&stage_name, &all)?; + verified_incremental_stage_batches( + repo, + working_copy, + &stage_name, + base_hashes.len(), + staged, + )? + }; + let working = repo.working_copy_record(working_copy)?; + if working.desired_view != target.id || working.desired_state != target.state { + return Err(invalid( + "working copy does not match the incremental stream target", + )); + } + let retained_private = + verify_incremental_baseline(repo, &target_name, policy, expected_git_tree)?; + if retained_private.keys().any(|path| path != ".atomicignore") { + return Err(invalid( + "incremental stream cannot preserve additional private paths yet", + )); + } + let (sha, hash) = staged + .last() + .or_else(|| replayed.last()) + .ok_or_else(|| invalid("span Git prefix must not be empty"))?; + let last_sha = Some(sha.clone()); + let last_hash = Some(*hash); + drop(lock); + Ok(Self { + repo, + stage: stage_name, + target: target_name, + limits, + target_id: target.id, + target_state: target.state, + target_count: target.change_count, + baseline_git_tree: Some((policy.clone(), expected_git_tree.clone())), + retained_private, + base_prefix: replayed.to_vec(), + base_hashes, + hashes: staged_hashes, + last_sha, + last_hash, + batches: resumed_batches, + publish_seconds: 0.0, + span_mode: true, }) } @@ -185,6 +743,9 @@ impl<'a> GitGraphImportSession<'a> { let mut previous = self.last_sha.as_deref(); let mut unique = HashSet::new(); for entry in entries { + if entry.change.hashed.span.is_some() != self.span_mode { + return Err(invalid("stream change format does not match session mode")); + } if entry.parent.as_deref() != previous || !unique.insert(&entry.sha) { return Err(invalid( "stream batch must follow the exact first-parent chain", @@ -194,6 +755,19 @@ impl<'a> GitGraphImportSession<'a> { } let wc = self.repo.require_working_copy_id()?; let lock = self.repo.try_lock_operation(wc)?; + if !self.base_prefix.is_empty() { + self.repo + .validate_span_git_prefix(&self.target, &self.base_prefix)?; + let (policy, expected) = self + .baseline_git_tree + .as_ref() + .expect("incremental baseline"); + if verify_incremental_baseline(self.repo, &self.target, policy, expected)? + != self.retained_private + { + return Err(invalid("incremental private baseline changed")); + } + } let txn = self.repo.pristine.read_txn()?; let stage = txn .get_view(&self.stage)? @@ -201,14 +775,87 @@ impl<'a> GitGraphImportSession<'a> { let target = txn .get_view(&self.target)? .ok_or_else(|| invalid("target disappeared"))?; - if target.change_count != 0 - || target.state != Merkle::ZERO + let expected_stage = self + .base_hashes + .iter() + .chain(&self.hashes) + .fold(Merkle::ZERO, |state, hash| state.next(hash)); + if target.id != self.target_id + || target.change_count != self.target_count + || target.state != self.target_state || stage.parent != Some(target.id) || stage.kind != ViewScope::Draft - || stage.change_count as usize != self.hashes.len() + || stage.change_count as usize != self.base_hashes.len() + self.hashes.len() + || stage.state != expected_stage { return Err(invalid("stream target/stage changed; refusing publication")); } + if !self.base_prefix.is_empty() { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{ + span_patch_storage::SpanPatchReadTxn, GitCommitClosureTxnT, + }; + let mut closure: Vec<_> = self + .base_hashes + .iter() + .chain(&self.hashes) + .copied() + .collect(); + let mut logs = None; + for (index, entry) in entries.iter().enumerate() { + closure.push(entry.hash); + let sha_id = txn.get_by_git_sha(&entry.sha)?; + let hash_id = txn.get_internal(&entry.hash)?; + if sha_id.is_none() && hash_id.is_none() { + continue; + } + let valid_identity = match (sha_id, hash_id) { + (Some(sha_id), Some(hash_id)) if sha_id == hash_id => { + let mut expected_closure = closure.clone(); + expected_closure.sort(); + expected_closure.dedup(); + txn.get_external(hash_id)? == Some(entry.hash) + && txn.was_change_applied(hash_id)? + && txn + .span_change_indexed(hash_id) + .map_err(|error| invalid(error.to_string()))? + && txn.get_change_seq(&stage, hash_id)?.is_none() + && txn.get_change_seq(&target, hash_id)?.is_none() + && txn.get_git_commit_closure(&entry.sha)? == Some(expected_closure) + && self + .repo + .load_change(&entry.hash)? + .hash() + .map_err(|error| invalid(error.to_string()))? + == entry.hash + } + _ => false, + }; + if logs.is_none() && valid_identity { + logs = Some(( + self.repo + .operation_log(OperationScope::WorkingCopy(wc), None, false)?, + self.repo + .operation_log(OperationScope::Repository, None, false)?, + )); + } + let recovered = logs.as_ref().is_some_and(|(working, global)| { + recovered_private_stage_change( + working, + global, + &self.stage, + &entry.hash, + stage.change_count + index as u64, + ) + }); + if !valid_identity || !recovered { + return Err(invalid(format!( + "incremental stream commit {} already has a repository identity", + entry.sha + ))); + } + } + } drop(txn); let record = self.repo.working_copy_record(wc)?; if record.desired_view != target.id || record.desired_state != target.state { @@ -254,7 +901,8 @@ impl<'a> GitGraphImportSession<'a> { )?; let result = (|| { let mut txn = lock.begin_write_immediate()?; - let mut closure = self.hashes.clone(); + let mut closure = self.base_hashes.clone(); + closure.extend_from_slice(&self.hashes); for entry in entries { self.repo.apply_synthesized_change_in_txn( &mut txn, @@ -276,15 +924,31 @@ impl<'a> GitGraphImportSession<'a> { .ok_or_else(|| invalid("applied stream change has no id"))?; txn.put_git_sha(&entry.sha, id)?; closure.push(entry.hash); + #[cfg(all(test, unix))] + if !self.base_prefix.is_empty() && entry.sha == entries[0].sha { + pause_incremental_batch_for_test(operation.id(), entries, "before-commit"); + } } txn.commit() })(); + #[cfg(all(test, unix))] + if !self.base_prefix.is_empty() && result.is_ok() { + pause_incremental_batch_for_test(operation.id(), entries, "after-commit"); + } match result { Ok(()) => { self.repo .apply_operation_metadata_locked(&lock, operation.id())?; + #[cfg(all(test, unix))] + if !self.base_prefix.is_empty() { + pause_incremental_batch_for_test(operation.id(), entries, "after-metadata"); + } self.repo .finalize_operation_verified(&lock, operation.id())?; + #[cfg(all(test, unix))] + if !self.base_prefix.is_empty() { + pause_incremental_batch_for_test(operation.id(), entries, "after-verified"); + } } Err(error) => { self.repo @@ -307,7 +971,25 @@ impl<'a> GitGraphImportSession<'a> { project: &ProjectTree, proof: &VerifiedProspectiveEquivalence, ) -> Result { + self.finish_with_guard(project, proof, || Ok(())) + } + + /// Check an external observation while holding the operation lock before + /// preparing the final promotion. A failed check leaves the staged changes + /// private and does not start a target-view operation. + pub fn finish_with_guard( + self, + project: &ProjectTree, + proof: &VerifiedProspectiveEquivalence, + guard: F, + ) -> Result + where + F: FnOnce() -> Result<(), RepositoryError>, + { let started = std::time::Instant::now(); + if self.hashes.is_empty() { + return Err(invalid("empty stream has no new commits to promote")); + } if project.git.root != *proof.expected_git_tree() || project.manifest.root() != *proof.manifest_root() { @@ -335,6 +1017,11 @@ impl<'a> GitGraphImportSession<'a> { }, ); } + for (path, retained) in &self.retained_private { + if expected.insert(path.clone(), retained.clone()).is_some() { + return Err(invalid("Git target collides with a retained private path")); + } + } let expectation = StagedExpectation { paths: Vec::new(), tree: Some(StagedTreeExpectation { @@ -348,6 +1035,20 @@ impl<'a> GitGraphImportSession<'a> { }; let wc = self.repo.require_working_copy_id()?; let lock = self.repo.try_lock_operation(wc)?; + guard()?; + if !self.base_prefix.is_empty() { + self.repo + .validate_span_git_prefix(&self.target, &self.base_prefix)?; + let (policy, expected) = self + .baseline_git_tree + .as_ref() + .expect("incremental baseline"); + if verify_incremental_baseline(self.repo, &self.target, policy, expected)? + != self.retained_private + { + return Err(invalid("incremental private baseline changed")); + } + } let txn = self.repo.pristine.read_txn()?; let mut target = txn .get_view(&self.target)? @@ -363,15 +1064,23 @@ impl<'a> GitGraphImportSession<'a> { .ok_or_else(|| invalid("stage member has no hash")) }) .collect::, RepositoryError>>()?; - if target.change_count != 0 - || target.state != Merkle::ZERO + let expected_staged: Vec<_> = self + .base_hashes + .iter() + .chain(&self.hashes) + .copied() + .collect(); + if target.id != self.target_id + || target.change_count != self.target_count + || target.state != self.target_state || stage.parent != Some(target.id) || stage.kind != ViewScope::Draft - || staged != self.hashes + || staged != expected_staged || stage.state != self - .hashes + .base_hashes .iter() + .chain(&self.hashes) .fold(Merkle::ZERO, |state, hash| state.next(hash)) { return Err(invalid("target/stage changed before final promotion")); @@ -379,10 +1088,14 @@ impl<'a> GitGraphImportSession<'a> { drop(txn); let before = self.repo.working_copy_record(wc)?; let mut after = before.clone(); - if after.desired_view != target.id { + if after.desired_view != target.id || after.desired_state != target.state { return Err(invalid("working copy changed during direct stream")); } - after.desired_state = stage.state; + let target_after = self + .hashes + .iter() + .fold(target.state, |state, hash| state.next(hash)); + after.desired_state = target_after; let state = |record, merkle| RepoStateRef { view: Some(ViewStateRef { name: self.target.clone(), @@ -397,7 +1110,7 @@ impl<'a> GitGraphImportSession<'a> { OperationKind::SynthesizeGit, None, state(before, target.state), - state(after, stage.state), + state(after, target_after), self.hashes .iter() .enumerate() @@ -407,7 +1120,7 @@ impl<'a> GitGraphImportSession<'a> { change: *hash, }, expected_old: MetadataValue::Absent, - expected_new: MetadataValue::Sequence(index as u64), + expected_new: MetadataValue::Sequence(target.change_count + index as u64), }) .collect(), self.hashes.clone(), @@ -480,7 +1193,7 @@ impl<'a> GitGraphImportSession<'a> { Ok(GitStreamOutcome { commits: self.hashes.len(), batches: self.batches, - state: stage.state, + state: target_after, private_publish_seconds: self.publish_seconds, final_proof_promotion_seconds: started.elapsed().as_secs_f64(), }) @@ -593,6 +1306,100 @@ mod tests { ) .unwrap() } + + #[cfg(unix)] + fn crash_span_entry( + seed: u8, + parent: Option, + path: &str, + bytes: &[u8], + project: &ProjectTree, + dependency: Option, + ) -> PreparedGitGraphChange { + use atomic_core::change::span_patch::{SpanOp, SpanPatch}; + let mut change = entry(seed, parent, path, bytes, project, false).change; + change.hashed.header.timestamp = + chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap(); + change.hashed.dependencies = dependency.into_iter().collect(); + let (start, end) = match &mut change.hashed.hunks[0] { + GraphOp::FileAdd { contents, .. } => { + let content = contents.take().unwrap(); + (content.start.get(), content.end.get()) + } + _ => panic!("expected file addition"), + }; + change.hashed.span = Some(SpanPatch::new( + Vec::new(), + vec![SpanOp::Root { + path: path.into(), + ordinal: 0, + start, + end, + }], + )); + let oid = |value| GitObjectId::new(GitHashAlgorithm::Sha1, vec![value; 20]).unwrap(); + let origin = match parent { + Some(parent) => { + GitSynthesisOrigin::first_parent(oid(seed), project.git.root.clone(), oid(parent)) + } + None => GitSynthesisOrigin::root(oid(seed), project.git.root.clone()), + } + .unwrap(); + PreparedGitGraphChange::new_span( + change, + origin.clone(), + b"incremental crash fixture".to_vec(), + crate::git_synthesis_metadata(&origin, &[], None), + ) + .unwrap() + } + + #[cfg(unix)] + fn crash_baseline_project() -> (ProjectTree, VerifiedProspectiveEquivalence) { + let policy = ConversionPolicy::new(GitHashAlgorithm::Sha1); + let entry = RepositoryEntry::new( + RepoPath::from_bytes(b"a.txt").unwrap(), + b"one\n".to_vec(), + 0o644, + atomic_core::change::InodeKind::Regular, + None, + ManifestDisposition::Included, + ) + .unwrap(); + let manifest = + RepositoryManifest::new(SetId::ZERO, policy.root().content_key, vec![entry]).unwrap(); + let project = ProjectTree::from_manifest(manifest, &policy).unwrap(); + let proof = crate::verify_prospective_equivalence(&project, &project.git.root).unwrap(); + (project, proof) + } + + #[cfg(unix)] + fn crash_final_project() -> (ProjectTree, VerifiedProspectiveEquivalence) { + let policy = ConversionPolicy::new(GitHashAlgorithm::Sha1); + let entries = [ + ("a.txt", b"one\n".as_slice()), + ("b.txt", b"two\n".as_slice()), + ("c.txt", b"three\n".as_slice()), + ] + .into_iter() + .map(|(path, bytes)| { + RepositoryEntry::new( + RepoPath::from_bytes(path.as_bytes()).unwrap(), + bytes.to_vec(), + 0o644, + atomic_core::change::InodeKind::Regular, + None, + ManifestDisposition::Included, + ) + .unwrap() + }) + .collect(); + let manifest = + RepositoryManifest::new(SetId::ZERO, policy.root().content_key, entries).unwrap(); + let project = ProjectTree::from_manifest(manifest, &policy).unwrap(); + let proof = crate::verify_prospective_equivalence(&project, &project.git.root).unwrap(); + (project, proof) + } #[test] fn prepared_graph_boundary_rejects_file_ops() { let (project, _) = project(); @@ -624,6 +1431,649 @@ mod tests { .contains("eager semantics are unsupported")); } + #[test] + fn prepared_stream_fences_span_and_graph_formats() { + use atomic_core::change::span_patch::{SpanOp, SpanPatch}; + let (project, _) = project(); + let prepared = entry(1, None, "a.txt", b"one\n", &project, false); + let origin = GitSynthesisOrigin::root( + GitObjectId::new(GitHashAlgorithm::Sha1, vec![1; 20]).unwrap(), + project.git.root.clone(), + ) + .unwrap(); + let metadata = crate::git_synthesis_metadata(&origin, &[], None); + let mut graph = prepared.change; + let missing = PreparedGitGraphChange::new_span( + graph.clone(), + origin.clone(), + vec![], + metadata.clone(), + ) + .err() + .unwrap(); + assert!(missing.to_string().contains("format does not match")); + graph.hashed.span = Some(SpanPatch::new( + Vec::new(), + vec![SpanOp::Root { + path: "a.txt".into(), + ordinal: 0, + start: 5, + end: 9, + }], + )); + let error = + PreparedGitGraphChange::new(graph.clone(), origin.clone(), vec![], metadata.clone()) + .err() + .unwrap(); + assert!(error.to_string().contains("format does not match")); + graph.hashed.metadata = b"must not overwrite".to_vec(); + let error = + PreparedGitGraphChange::new_span(graph, origin, b"Git facts".to_vec(), metadata) + .err() + .unwrap(); + assert!(error.to_string().contains("must not prepopulate")); + } + + #[test] + fn span_stream_refuses_existing_staged_paths_before_installing_capability() { + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + std::fs::write(dir.path().join("user.txt"), b"user bytes\n").unwrap(); + repo.add(wc, "user.txt", Default::default()).unwrap(); + let error = match GitGraphImportSession::begin_span( + &mut repo, + GitStreamLimits::new(2, 1024 * 1024).unwrap(), + ) { + Ok(_) => panic!("span stream must not accept a staged path"), + Err(error) => error, + }; + assert!( + error + .to_string() + .contains("fresh repository without tracked or staged paths"), + "{error}" + ); + assert_eq!( + std::fs::read(dir.path().join("user.txt")).unwrap(), + b"user bytes\n" + ); + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + txn.required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .unwrap(), + None + ); + assert!(txn.get_view("git-stream-private").unwrap().is_none()); + } + + #[test] + fn git_import_preflight_lock_excludes_concurrent_span_initialization() { + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + + let dir = tempfile::tempdir().unwrap(); + drop(Repository::init(dir.path()).unwrap()); + let guard = Repository::try_lock_git_import_without_span(dir.path(), false) + .unwrap() + .expect("existing repository needs a preflight lock"); + let path = dir.path().to_path_buf(); + let error = std::thread::spawn(move || { + let mut repo = Repository::open_for_workspace_transaction(&path).unwrap(); + match GitGraphImportSession::begin_span( + &mut repo, + GitStreamLimits::new(2, 1024 * 1024).unwrap(), + ) { + Ok(_) => panic!("span initialization crossed the Git preflight lock"), + Err(error) => error.to_string(), + } + }) + .join() + .unwrap(); + assert!(error.contains("lock") || error.contains("busy"), "{error}"); + drop(guard); + let mut repo = Repository::open(dir.path()).unwrap(); + { + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + txn.required_capability_version(SPAN_RANGE_CAPABILITY.id()) + .unwrap(), + None + ); + } + let session = GitGraphImportSession::begin_span( + &mut repo, + GitStreamLimits::new(2, 1024 * 1024).unwrap(), + ) + .unwrap(); + drop(session); + } + + #[cfg(unix)] + #[test] + fn incremental_span_batch_process_kill_recovers_and_retries() { + use atomic_core::operation::OperationScope; + use atomic_core::types::Base32; + use std::io::BufRead; + use std::os::unix::process::ExitStatusExt; + const CHILD_ROOT: &str = "ATOMIC_TEST_INCREMENTAL_CHILD_ROOT"; + const READY: &str = "ATOMIC_INCREMENTAL_READY "; + if let Some(root) = std::env::var_os(CHILD_ROOT) { + let root = std::path::PathBuf::from(root); + std::fs::write(root.join("user.txt"), b"untracked user bytes\n").unwrap(); + let mut repo = Repository::init(&root).unwrap(); + let (baseline, proof) = crash_baseline_project(); + let (final_project, _) = crash_final_project(); + let limits = GitStreamLimits::new(2, 1024 * 1024).unwrap(); + let first = crash_span_entry(1, None, "a.txt", b"one\n", &baseline, None); + let first_hash = first.hash(); + let mut session = GitGraphImportSession::begin_span(&mut repo, limits).unwrap(); + session.publish(&[first]).unwrap(); + session.finish(&baseline, &proof).unwrap(); + let prefix = vec![("01".repeat(20), first_hash)]; + let policy = ConversionPolicy::new(GitHashAlgorithm::Sha1); + let second = crash_span_entry( + 2, + Some(1), + "b.txt", + b"two\n", + &final_project, + Some(first_hash), + ); + let third = crash_span_entry( + 3, + Some(2), + "c.txt", + b"three\n", + &final_project, + Some(second.hash()), + ); + let mut session = GitGraphImportSession::begin_span_incremental( + &mut repo, + limits, + &prefix, + &policy, + &baseline.git.root, + ) + .unwrap(); + session.publish(&[second, third]).unwrap(); + panic!("child did not reach incremental crash window"); + } + + for window in [ + "before-commit", + "after-commit", + "after-metadata", + "after-verified", + ] { + let dir = tempfile::tempdir().unwrap(); + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "repository::git_stream::tests::incremental_span_batch_process_kill_recovers_and_retries", "--nocapture"]) + .current_dir(std::env::temp_dir()) + .env(CHILD_ROOT, dir.path()) + .env("ATOMIC_TEST_INCREMENTAL_WINDOW", window) + .env("ATOMIC_TEST_INCREMENTAL_SHA", "02".repeat(20)) + .stdout(std::process::Stdio::piped()) + .spawn().unwrap(); + let stdout = child.stdout.take().unwrap(); + let (send, recv) = std::sync::mpsc::channel(); + let reader = std::thread::spawn(move || { + let mut lines = String::new(); + for line in std::io::BufReader::new(stdout).lines() { + match line { + Ok(line) => { + if let Some((_, marker)) = line.split_once(READY) { + let _ = send.send(Ok(marker.to_string())); + return; + } + lines.push_str(&line); + lines.push('\n'); + } + Err(error) => { + let _ = send.send(Err(error.to_string())); + return; + } + } + } + let _ = send.send(Err(format!("child exited before crash point:\n{lines}"))); + }); + let ready = recv.recv_timeout(std::time::Duration::from_secs(60)); + let killed = child.kill(); + let status = child.wait().unwrap(); + if ready.is_ok() { + reader.join().unwrap(); + } + let marker = ready + .expect("child timed out") + .expect("child did not reach crash point"); + assert!( + killed.is_ok() && status.signal() == Some(9), + "child was not killed" + ); + let parts: Vec<_> = marker.split_whitespace().collect(); + assert_eq!(parts.len(), 3, "{marker}"); + let interrupted = atomic_core::OperationId::from_base32(parts[0].as_bytes()).unwrap(); + let staged_hash = Hash::from_base32(parts[1].as_bytes()).unwrap(); + let staged_third = Hash::from_base32(parts[2].as_bytes()).unwrap(); + let mut repo = Repository::open(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + let initial_log = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + let global_log = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + let interrupted_details = repo.operation_details(interrupted).unwrap(); + if window == "after-verified" { + assert_eq!( + interrupted_details.verification, + super::super::OperationVerificationState::Verified + ); + } else { + assert_ne!( + interrupted_details.verification, + super::super::OperationVerificationState::Verified + ); + let recovered = initial_log + .entries + .iter() + .find(|entry| { + entry.operation.payload().kind == OperationKind::Recover + && entry.operation.payload().parents.contains(&interrupted) + && entry.verification + == super::super::OperationVerificationState::Verified + }) + .expect("incomplete batch must have a verified recovery child"); + assert!(!repo + .operation_details(recovered.operation.id()) + .unwrap() + .receipts + .is_empty()); + assert!(global_log + .entries + .iter() + .any(|entry| entry.operation.id() == recovered.operation.id())); + } + assert_eq!( + std::fs::read(dir.path().join("user.txt")).unwrap(), + b"untracked user bytes\n" + ); + assert_eq!( + repo.get_file_content("a.txt").unwrap(), + Some(b"one\n".to_vec()) + ); + assert_eq!(repo.get_file_content("b.txt").unwrap(), None); + assert_eq!(repo.get_file_content("c.txt").unwrap(), None); + let first_hash = { + let txn = repo.pristine().read_txn().unwrap(); + let id = txn.get_by_git_sha(&"01".repeat(20)).unwrap().unwrap(); + txn.get_external(id).unwrap().unwrap() + }; + let prefix = vec![("01".repeat(20), first_hash)]; + let (baseline, _) = crash_baseline_project(); + let (final_project, final_proof) = crash_final_project(); + let policy = ConversionPolicy::new(GitHashAlgorithm::Sha1); + let limits = GitStreamLimits::new(2, 1024 * 1024).unwrap(); + let outcome = if window == "after-verified" { + let staged = vec![ + ("02".repeat(20), staged_hash), + ("03".repeat(20), staged_third), + ]; + let session = GitGraphImportSession::resume_span_incremental( + &mut repo, + limits, + &prefix, + &staged, + &policy, + &baseline.git.root, + ) + .unwrap(); + session.finish(&final_project, &final_proof) + } else { + let second = crash_span_entry( + 2, + Some(1), + "b.txt", + b"two\n", + &final_project, + Some(first_hash), + ); + assert_eq!( + second.hash(), + staged_hash, + "replayed change identity changed" + ); + let third = crash_span_entry( + 3, + Some(2), + "c.txt", + b"three\n", + &final_project, + Some(staged_hash), + ); + assert_eq!( + third.hash(), + staged_third, + "replayed second change identity changed" + ); + let mut session = GitGraphImportSession::begin_span_incremental( + &mut repo, + limits, + &prefix, + &policy, + &baseline.git.root, + ) + .unwrap(); + session.publish(&[second, third]).unwrap(); + session.finish(&final_project, &final_proof) + }; + assert_eq!(outcome.unwrap().commits, 2); + assert_eq!( + repo.get_file_content("b.txt").unwrap(), + Some(b"two\n".to_vec()) + ); + assert_eq!( + repo.get_file_content("c.txt").unwrap(), + Some(b"three\n".to_vec()) + ); + assert_eq!( + std::fs::read(dir.path().join("user.txt")).unwrap(), + b"untracked user bytes\n" + ); + let log = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + assert!(log + .entries + .iter() + .any(|entry| entry.operation.id() == interrupted)); + drop(repo); + let reopened = Repository::open(dir.path()).unwrap(); + assert_eq!( + reopened.get_file_content("a.txt").unwrap(), + Some(b"one\n".to_vec()) + ); + assert_eq!( + reopened.get_file_content("b.txt").unwrap(), + Some(b"two\n".to_vec()) + ); + assert_eq!( + reopened.get_file_content("c.txt").unwrap(), + Some(b"three\n".to_vec()) + ); + eprintln!("incremental span crash window {window}: recovered and retried"); + } + } + + #[cfg(unix)] + #[test] + fn projected_record_process_kill_recovers_and_preserves_capability() { + use atomic_core::operation::OperationScope; + use atomic_core::pristine::{CapabilityTxnT, SPAN_PROJECTED_SEMANTICS_CAPABILITY}; + use atomic_core::types::Base32; + use std::io::BufRead; + use std::os::unix::process::ExitStatusExt; + const CHILD_ROOT: &str = "ATOMIC_TEST_PROJECTED_RECORD_ROOT"; + const READY: &str = "ATOMIC_PROJECTED_RECORD_READY "; + if let Some(root) = std::env::var_os(CHILD_ROOT) { + let root = std::path::PathBuf::from(root); + let mut repo = Repository::init(&root).unwrap(); + let (project, proof) = crash_baseline_project(); + let initial = crash_span_entry(1, None, "a.txt", b"one\n", &project, None); + let mut session = GitGraphImportSession::begin_span( + &mut repo, + GitStreamLimits::new(2, 1024 * 1024).unwrap(), + ) + .unwrap(); + session.publish(&[initial]).unwrap(); + session.finish(&project, &proof).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.materialize(wc).unwrap(); + std::fs::write(root.join("a.txt"), b"native bytes\n").unwrap(); + std::fs::write(root.join("user.txt"), b"untracked user bytes\n").unwrap(); + repo.record( + wc, + ChangeHeader::new("native crash fixture"), + crate::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + panic!("child did not reach projected record kill point"); + } + for window in ["after-fence", "after-save", "after-apply", "after-verified"] { + let dir = tempfile::tempdir().unwrap(); + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "repository::git_stream::tests::projected_record_process_kill_recovers_and_preserves_capability", "--nocapture"]) + .current_dir(std::env::temp_dir()) + .env(CHILD_ROOT, dir.path()) + .env("ATOMIC_TEST_PROJECTED_RECORD_WINDOW", window) + .stdout(std::process::Stdio::piped()).spawn().unwrap(); + let stdout = child.stdout.take().unwrap(); + let (send, receive) = std::sync::mpsc::channel(); + let reader = std::thread::spawn(move || { + let mut output = String::new(); + for line in std::io::BufReader::new(stdout).lines() { + match line { + Ok(line) => { + if let Some((_, marker)) = line.split_once(READY) { + let _ = send.send(Ok(marker.to_string())); + return; + } + output.push_str(&line); + output.push('\n'); + } + Err(error) => { + let _ = send.send(Err(error.to_string())); + return; + } + } + } + let _ = send.send(Err(format!("child exited before kill point:\n{output}"))); + }); + let ready = receive.recv_timeout(std::time::Duration::from_secs(60)); + let killed = child.kill(); + let status = child.wait().unwrap(); + if ready.is_ok() { + reader.join().unwrap(); + } + let marker = ready + .expect("child timed out") + .expect("child did not reach kill point"); + assert!(killed.is_ok() && status.signal() == Some(9)); + let fields: Vec<_> = marker.split_whitespace().collect(); + assert_eq!(fields.len(), 2, "{marker}"); + let interrupted = atomic_core::OperationId::from_base32(fields[0].as_bytes()).unwrap(); + let hash = Hash::from_base32(fields[1].as_bytes()).unwrap(); + let repo = Repository::open(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + assert_eq!( + std::fs::read(dir.path().join("a.txt")).unwrap(), + b"native bytes\n" + ); + assert_eq!( + std::fs::read(dir.path().join("user.txt")).unwrap(), + b"untracked user bytes\n" + ); + assert_eq!( + repo.pristine() + .read_txn() + .unwrap() + .required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) + .unwrap(), + Some(SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version()) + ); + let details = repo.operation_details(interrupted).unwrap(); + let log = repo + .operation_log(OperationScope::WorkingCopy(wc), None, false) + .unwrap(); + assert_eq!( + repo.load_change(&hash).is_ok(), + window != "after-fence", + "stored bytes at {window}" + ); + if window == "after-verified" { + assert_eq!( + details.verification, + super::super::OperationVerificationState::Verified + ); + assert_eq!( + repo.get_file_content("a.txt").unwrap(), + Some(b"native bytes\n".to_vec()) + ); + assert!(repo.load_change(&hash).is_ok()); + } else { + assert_ne!( + details.verification, + super::super::OperationVerificationState::Verified + ); + let recovery = log + .entries + .iter() + .find(|entry| { + entry.operation.payload().kind == OperationKind::Recover + && entry.operation.payload().parents.contains(&interrupted) + && entry.verification + == super::super::OperationVerificationState::Verified + }) + .unwrap_or_else(|| panic!("missing verified recovery for {window}")); + assert!(!repo + .operation_details(recovery.operation.id()) + .unwrap() + .receipts + .is_empty()); + let global = repo + .operation_log(OperationScope::Repository, None, false) + .unwrap(); + assert!(global + .entries + .iter() + .any(|entry| entry.operation.id() == recovery.operation.id())); + assert_eq!( + repo.get_file_content("a.txt").unwrap(), + Some(b"one\n".to_vec()) + ); + repo.record( + wc, + ChangeHeader::new("retry native"), + crate::RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + assert_eq!( + repo.get_file_content("a.txt").unwrap(), + Some(b"native bytes\n".to_vec()) + ); + } + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert_eq!( + repo.get_file_content("a.txt").unwrap(), + Some(b"native bytes\n".to_vec()) + ); + assert_eq!( + std::fs::read(dir.path().join("user.txt")).unwrap(), + b"untracked user bytes\n" + ); + eprintln!("projected record crash window {window}: recovered and verified"); + } + } + + #[test] + fn incremental_bootstrap_classification_rejects_mixed_or_foreign_targets() { + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let session = GitGraphImportSession::begin_span( + &mut repo, + GitStreamLimits::new(2, 1024 * 1024).unwrap(), + ) + .unwrap(); + drop(session); + let wc = repo.require_working_copy_id().unwrap(); + std::fs::write(dir.path().join(".atomicignore"), b"# bootstrap\n").unwrap(); + repo.add(wc, ".atomicignore", Default::default()).unwrap(); + let recorded = repo + .record( + wc, + ChangeHeader::new("Initialize repository"), + crate::RecordOptions::new().add_path(".atomicignore"), + ) + .unwrap(); + let change = recorded.change(); + assert!(is_atomicignore_bootstrap(change)); + + let mut foreign_attribute = change.clone(); + for hunk in &mut foreign_attribute.hashed.hunks { + if let GraphOp::SetAttr { inode, .. } = hunk { + inode.change = Some(Hash::of(b"foreign inode")); + break; + } + } + assert!(!is_atomicignore_bootstrap(&foreign_attribute)); + let mut wrong_path = change.clone(); + wrong_path + .hashed + .file_ops + .push(atomic_core::change::FileOps::create( + atomic_core::crdt::TrunkId::new(atomic_core::types::NodeId::ROOT, 9), + "other.txt".into(), + Some(Encoding::Utf8), + )); + assert!(!is_atomicignore_bootstrap(&wrong_path)); + let mut extra_dependency = change.clone(); + extra_dependency + .hashed + .dependencies + .push(Hash::of(b"other change")); + assert!(!is_atomicignore_bootstrap(&extra_dependency)); + } + + #[test] + fn git_import_preflight_refuses_legacy_span_database_without_migration() { + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let session = GitGraphImportSession::begin_span( + &mut repo, + GitStreamLimits::new(2, 1024 * 1024).unwrap(), + ) + .unwrap(); + drop(session); + drop(repo); + let dot = dir.path().join(".atomic"); + let current = dot.join(super::super::DATABASE_FILE); + let legacy = dot.join(super::super::LEGACY_PRISTINE_FILE); + std::fs::rename(¤t, &legacy).unwrap(); + let before = std::fs::read(&legacy).unwrap(); + + let error = match Repository::try_lock_git_import_without_span(dir.path(), false) { + Ok(_) => panic!("legacy span database must not be imported into"), + Err(error) => error, + }; + assert!( + error.to_string().contains("existing span repository"), + "{error}" + ); + assert_eq!(std::fs::read(&legacy).unwrap(), before); + assert!(!current.exists()); + } + + #[test] + fn git_import_preflight_refuses_unreadable_database_without_repair() { + let dir = tempfile::tempdir().unwrap(); + let dot = dir.path().join(".atomic"); + std::fs::create_dir(&dot).unwrap(); + let database = dot.join(super::super::DATABASE_FILE); + let bytes = b"not a redb database; preserve this evidence"; + std::fs::write(&database, bytes).unwrap(); + + assert!(Repository::try_lock_git_import_without_span(dir.path(), false).is_err()); + assert_eq!(std::fs::read(&database).unwrap(), bytes); + assert!(!dot.join("bridge.lock").exists()); + } + #[test] fn failed_final_proof_never_promotes_private_stream() { let dir = tempfile::tempdir().unwrap(); diff --git a/atomic-repository/src/repository/insert.rs b/atomic-repository/src/repository/insert.rs index b2cbcaac..8645bbf9 100644 --- a/atomic-repository/src/repository/insert.rs +++ b/atomic-repository/src/repository/insert.rs @@ -169,6 +169,9 @@ impl Repository { .map_err(|e| RepositoryError::Database(e.to_string()))?; txn.put_change_deps(id, change.dependencies()) .map_err(|e| RepositoryError::Database(e.to_string()))?; + if applied && is_graph_only_git_span(&change) { + index_replayed_git_span(&mut txn, id, hash, &change, false)?; + } let view = txn .get_view(view_name) .map_err(|e| RepositoryError::Database(e.to_string()))? @@ -287,10 +290,60 @@ fn write_recorded_graph_and_spans( return write_change_to_graph(txn, view_name, change_id, hash, change, options, false) .map_err(|error| RepositoryError::Apply(error.to_string())); }; + if change.file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) { + use atomic_core::pristine::{CapabilityMutTxnT, SPAN_PROJECTED_SEMANTICS_CAPABILITY}; + txn.put_required_capability(SPAN_PROJECTED_SEMANTICS_CAPABILITY)?; + } use atomic_core::change::span_patch::SpanOp; use atomic_core::pristine::{span_patch_storage::SpanPatchReadTxn, CrdtTxnT}; let semantic = atomic_core::change::span_patch::localize_file_ops(txn, change) .map_err(|error| RepositoryError::Apply(error.to_string()))?; + let graph_only_git = is_graph_only_git_span(change); + let projected: Vec<_> = semantic + .iter() + .filter_map(|ops| match ops.trunk_op() { + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { file, .. }) => Some((ops, file)), + _ => None, + }) + .collect(); + if !projected.is_empty() { + let mut ids = Vec::new(); + for hash in change.dependencies() { + ids.push(txn.get_internal(hash)?.ok_or_else(|| { + RepositoryError::Apply("projected semantic dependency missing".into()) + })?); + } + let membership = atomic_core::pristine::ViewMembershipSet::from_ordered(ids); + let visibility = super::filter::graph_visibility_from_membership(txn, &membership)?; + for (ops, file) in &projected { + let inode = txn + .span_file_inode(file) + .map_err(|error| RepositoryError::Apply(error.to_string()))? + .ok_or_else(|| { + RepositoryError::Apply("projected semantic file mapping missing".into()) + })?; + if txn.get_crdt_inode_trunk(inode.get())?.is_some() { + return Err(RepositoryError::Apply( + "projected semantic edit cannot replace an existing CRDT trunk".into(), + )); + } + let binding = repository + .projected_path_binding_for_visibility(txn, &visibility, ops.path())? + .ok_or_else(|| { + RepositoryError::Apply("projected semantic path is absent or conflicted".into()) + })?; + if binding.directory || binding.inode != inode { + return Err(RepositoryError::Apply( + "projected semantic path names a different file".into(), + )); + } + } + } // The portable mapping is author-supplied. A hash in the dependency // closure is not by itself proof that its semantic branch belongs to the // file named by a span edit. @@ -302,6 +355,12 @@ fn write_recorded_graph_and_spans( .span_file_inode(file) .map_err(|e| RepositoryError::Apply(e.to_string()))? .ok_or_else(|| RepositoryError::Apply("span file mapping missing".into()))?; + if graph_only_git { + continue; + } + if semantic.iter().any(|ops| matches!(ops.trunk_op(), Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { file: projected, .. }) if projected == file)) { + continue; + } let trunk_key = txn .get_crdt_inode_trunk(inode.get()) .map_err(|e| RepositoryError::Database(e.to_string()))? @@ -428,9 +487,10 @@ fn write_recorded_graph_and_spans( .iter() .filter(|ops| ops.path() == path && ops.is_create()) .collect(); - if creators.len() != 1 - || !creators[0].trunk_id().change_id().is_root() - || !matches!(creators[0].trunk_op(), Some(atomic_core::crdt::TrunkOp::Create { path: created, .. }) if created == path) + if !graph_only_git + && (creators.len() != 1 + || !creators[0].trunk_id().change_id().is_root() + || !matches!(creators[0].trunk_op(), Some(atomic_core::crdt::TrunkOp::Create { path: created, .. }) if created == path)) { return Err(RepositoryError::InvalidOperation { message: format!("span root '{path}' has no unique semantic creation"), @@ -469,9 +529,126 @@ fn write_recorded_graph_and_spans( &roots, ) .map_err(|error| RepositoryError::Apply(error.to_string()))?; + if graph_only_git { + index_replayed_git_span(txn, change_id, *hash, change, true)?; + } Ok(inserted) } +fn is_graph_only_git_span(change: &Change) -> bool { + change.hashed.span.is_some() + && matches!( + change.origin(), + atomic_core::change::ChangeOrigin::GitSynthesized { .. } + ) + && change.file_ops().is_empty() +} + +fn index_replayed_git_span( + txn: &mut atomic_core::pristine::WriteTxn<'_>, + change_id: NodeId, + hash: Hash, + change: &Change, + allow_create: bool, +) -> Result<(), RepositoryError> { + use atomic_core::pristine::{ + EffectiveProjectionClosure, GitCommitClosureMutTxnT, GitCommitClosureTxnT, + GitShaIndexMutTxnT, GitShaIndexTxnT, ViewMembershipSet, + }; + let atomic_core::change::ChangeOrigin::GitSynthesized { + commit, parents, .. + } = change.origin() + else { + return Err(RepositoryError::Apply( + "replayed Git span has no Git origin".into(), + )); + }; + let sha = super::synthesis::git_oid_hex(commit); + match txn.get_by_git_sha(&sha)? { + Some(existing) if existing != change_id => { + return Err(RepositoryError::Apply(format!( + "Git commit {sha} already has a different repository identity" + ))) + } + None if !allow_create => { + return Err(RepositoryError::Apply(format!( + "Git commit {sha} identity is missing" + ))) + } + _ => {} + } + let mut closure = if let Some(parent) = parents.first() { + let parent_sha = super::synthesis::git_oid_hex(parent); + let parent_id = txn.get_by_git_sha(&parent_sha)?.ok_or_else(|| { + RepositoryError::Apply(format!("Git parent {parent_sha} is not indexed")) + })?; + let dependency_ids = change + .dependencies() + .iter() + .map(|dependency| { + txn.get_internal(dependency)? + .ok_or_else(|| RepositoryError::Apply("Git replay dependency missing".into())) + }) + .collect::, RepositoryError>>()?; + let visible = EffectiveProjectionClosure::try_from_membership( + txn, + &ViewMembershipSet::from_ordered(dependency_ids), + )?; + if !visible.contains(parent_id) { + return Err(RepositoryError::Apply( + "Git first parent is outside the change dependency closure".into(), + )); + } + let parent_hash = txn + .get_external(parent_id)? + .ok_or_else(|| RepositoryError::Apply("Git parent hash missing".into()))?; + let closure = txn.get_git_commit_closure(&parent_sha)?.ok_or_else(|| { + RepositoryError::Apply(format!( + "Git parent {parent_sha} interpretation closure is missing" + )) + })?; + if !closure.contains(&parent_hash) || closure.windows(2).any(|pair| pair[0] >= pair[1]) { + return Err(RepositoryError::Apply( + "Git parent interpretation closure is not canonical".into(), + )); + } + for member in &closure { + let id = txn.get_internal(member)?.ok_or_else(|| { + RepositoryError::Apply("Git interpretation closure member missing".into()) + })?; + if !visible.contains(id) { + return Err(RepositoryError::Apply( + "Git interpretation closure exceeds the change dependency closure".into(), + )); + } + } + closure + } else { + Vec::new() + }; + closure.push(hash); + closure.sort(); + closure.dedup(); + match txn.get_git_commit_closure(&sha)? { + Some(existing) if existing != closure => { + return Err(RepositoryError::Apply(format!( + "Git commit {sha} has a different interpretation closure" + ))) + } + None if !allow_create => { + return Err(RepositoryError::Apply(format!( + "Git commit {sha} interpretation closure is missing" + ))) + } + _ => {} + } + if allow_create { + txn.put_git_sha(&sha, change_id)?; + txn.put_git_commit_closure(&sha, &closure)?; + } + Ok(()) +} + fn write_recorded_in_txn( repository: &Repository, txn: &mut atomic_core::pristine::WriteTxn<'_>, @@ -501,6 +678,7 @@ fn write_recorded_in_txn( let inserted = write_recorded_graph_and_spans( repository, txn, view_name, change_id, hash, change, &options, )?; + validate_recorded_span_projection(repository, txn, view_name, *hash, change)?; repository.apply_tree_projection( txn, &tree_projection, @@ -510,6 +688,50 @@ fn write_recorded_in_txn( Ok(inserted) } +fn validate_recorded_span_projection( + repository: &Repository, + txn: &atomic_core::pristine::WriteTxn<'_>, + view_name: &str, + hash: Hash, + change: &Change, +) -> Result<(), RepositoryError> { + use atomic_core::change::span_patch::{ExternalFile, SpanOp}; + use atomic_core::pristine::span_patch_storage::{SpanPatchReadTxn, SpanRenderContext}; + let Some(patch) = &change.hashed.span else { + return Ok(()); + }; + let view = txn + .get_view(view_name)? + .ok_or_else(|| RepositoryError::ViewNotFound { + name: view_name.to_string(), + })?; + let visibility = graph_visibility_closure(txn, &view)?; + let context = SpanRenderContext::new(txn, &repository.change_store, &visibility) + .map_err(|error| RepositoryError::Apply(error.to_string()))?; + let mut inodes = HashSet::new(); + for op in &patch.ops { + let file = match op { + SpanOp::Root { ordinal, .. } => ExternalFile { + owner: hash, + ordinal: *ordinal, + }, + SpanOp::Edit { file, .. } => file.clone(), + }; + let inode = txn + .span_file_inode(&file) + .map_err(|error| RepositoryError::Apply(error.to_string()))? + .ok_or_else(|| { + RepositoryError::Apply("span file mapping missing after record".into()) + })?; + if inodes.insert(inode) { + context + .render(inode) + .map_err(|error| RepositoryError::Apply(error.to_string()))?; + } + } + Ok(()) +} + /// Timing details for the git-import fresh-write path. #[derive(Debug, Clone, Copy, Default)] pub struct ImportWriteTimings { @@ -2875,13 +3097,6 @@ impl Repository { self, &mut txn, view_name, change_id, hash, change, &options, )?; - self.apply_tree_projection( - &mut txn, - &tree_projection, - view_name, - preserve_existing_tree_paths, - )?; - if let Some((removal_view_name, removed_hash)) = removal { let mut removal_view = txn .get_view(removal_view_name) @@ -2908,9 +3123,6 @@ impl Repository { // pending deletions (or rename paths) from the next durable // record. Preserve the same selected-view boundary as the write // above, including separately staged working-copy paths. - if removal_view_name == self.current_view { - self.realign_tree_projection_in_txn(&mut txn, removal_view_name)?; - } if removal_view_name == view_name { let final_view = txn .get_view(view_name) @@ -2922,6 +3134,21 @@ impl Repository { apply_outcome.sequence = final_view.change_count; } } + // Snapshot replacement deliberately has both the old and new private + // references in the transaction until the old one is removed. Verify + // the final view, not that transient overlap, before committing. + validate_recorded_span_projection(self, &txn, view_name, *hash, change)?; + self.apply_tree_projection( + &mut txn, + &tree_projection, + view_name, + preserve_existing_tree_paths, + )?; + if let Some((removal_view_name, _)) = removal { + if removal_view_name == self.current_view { + self.realign_tree_projection_in_txn(&mut txn, removal_view_name)?; + } + } // Commit the transaction let commit_start = std::time::Instant::now(); diff --git a/atomic-repository/src/repository/locks.rs b/atomic-repository/src/repository/locks.rs index 5e1a4d7c..ad4d485b 100644 --- a/atomic-repository/src/repository/locks.rs +++ b/atomic-repository/src/repository/locks.rs @@ -30,6 +30,20 @@ pub struct RepositoryCommonLockGuard { reentrant: bool, } +/// Holds the common lock across a Git-import capability preflight and import. +/// Like the underlying lock bookkeeping, this guard must stay on its thread. +pub struct GitImportPreflightGuard { + _common: AdvisoryFileLock, + _thread_bound: std::marker::PhantomData>, +} + +/// Holds the repository-common lock during physical database maintenance. +/// It is thread-bound and does not keep a database handle open. +pub struct RepositoryMaintenanceGuard { + _common: AdvisoryFileLock, + _thread_bound: std::marker::PhantomData>, +} + /// A repository operation lock scoped to one persistent working copy. /// /// The embedded common guard keeps the repository-common lock alive until this @@ -135,6 +149,126 @@ enum FinalResource { } impl Repository { + /// Acquire the common operation lock without opening or modifying the + /// repository database. The caller must preflight and recheck repository + /// capabilities and operation heads while this guard is held. + pub fn try_lock_database_maintenance>( + path: P, + ) -> Result { + let layout = super::working_copy::discover_layout(path.as_ref())?; + let common = try_lock_file( + &layout.common_dot_dir.join(COMMON_OPERATION_LOCK), + RepositoryLockKind::Common, + false, + )?; + Ok(RepositoryMaintenanceGuard { + _common: common, + _thread_bound: std::marker::PhantomData, + }) + } + + /// Reject ordinary Git imports into a span repository before any writable + /// open or recovery. Hold the returned guard until import completes. + /// Databases requiring repair or migration are refused rather than opened + /// writable to inspect them. + pub fn try_lock_git_import_without_span>( + path: P, + dry_run: bool, + ) -> Result, RepositoryError> { + Self::try_lock_git_import_preflight(path, dry_run, false) + } + + /// Hold the common lock for an explicitly span-aware Git import. Existing + /// databases must already require the span capability; an unreadable or + /// unsupported database is refused before writable recovery. + pub fn try_lock_git_import_for_span>( + path: P, + dry_run: bool, + ) -> Result, RepositoryError> { + Self::try_lock_git_import_preflight(path, dry_run, true) + } + + fn try_lock_git_import_preflight>( + path: P, + dry_run: bool, + require_span: bool, + ) -> Result, RepositoryError> { + let layout = match super::working_copy::discover_layout(path.as_ref()) { + Ok(layout) => layout, + Err(RepositoryError::NotFound { .. } | RepositoryError::NotInRepository) => { + return Ok(None) + } + Err(error) => return Err(error), + }; + let requested = std::fs::canonicalize(path.as_ref())?; + if layout.working_root != requested { + return Ok(None); + } + let inspect = || -> Result { + let mut found = false; + for name in [super::DATABASE_FILE, super::LEGACY_PRISTINE_FILE] { + let database = layout.common_dot_dir.join(name); + match std::fs::metadata(&database) { + Ok(metadata) if metadata.is_file() => {} + Ok(_) => { + return Err(RepositoryError::InvalidOperation { + message: format!( + "repository database is not a file: {}", + database.display() + ), + }) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue, + Err(error) => return Err(error.into()), + } + found = true; + let pristine = Pristine::open_readonly_without_recovery(&database)?; + let has_span = + pristine + .required_repository_capabilities()? + .iter() + .any(|capability| { + capability.id == atomic_core::pristine::SPAN_RANGE_CAPABILITY.id() + }); + if has_span && !require_span { + return Err(RepositoryError::InvalidOperation { + message: "Git import into an existing span repository is not supported; no import was started".into(), + }); + } + if require_span && !has_span { + return Err(RepositoryError::InvalidOperation { + message: + "span-aware incremental Git import requires an existing span repository" + .into(), + }); + } + } + Ok(found) + }; + // A refusal for an already-span-backed repository must not create a + // lock file. Recheck under the common lock to close the race with a + // concurrent span-capability installation. + if !inspect()? { + return Ok(None); + } + let lock_path = layout.common_dot_dir.join(COMMON_OPERATION_LOCK); + if dry_run && !lock_path.is_file() { + return Err(RepositoryError::InvalidOperation { + message: "Git import dry-run cannot safely lock this repository without creating a lock file".into(), + }); + } + let common = try_lock_file(&lock_path, RepositoryLockKind::Common, true)?; + if !inspect()? { + return Err(RepositoryError::InvalidOperation { + message: "repository database disappeared during Git import preflight".into(), + }); + } + Ok(Some(GitImportPreflightGuard { + _common: common, + _thread_bound: std::marker::PhantomData, + })) + } + pub(super) fn common_operation_lock_path(&self) -> PathBuf { common_operation_lock_path(&self.dot_dir) } diff --git a/atomic-repository/src/repository/mod.rs b/atomic-repository/src/repository/mod.rs index 32538826..51f727eb 100644 --- a/atomic-repository/src/repository/mod.rs +++ b/atomic-repository/src/repository/mod.rs @@ -181,7 +181,7 @@ pub use ignore_mirror::{ check_ignore_policy, mirror_ignores, IgnoreMirrorError, IgnorePolicyReport, MANAGED_BLOCK_BEGIN, MANAGED_BLOCK_END, }; -pub use locks::RepositoryCommonLockGuard; +pub use locks::{GitImportPreflightGuard, RepositoryCommonLockGuard, RepositoryMaintenanceGuard}; pub use operation::{ OperationDetails, OperationHeadState, OperationLog, OperationLogEntry, OperationVerificationState, PreparedRemoteOperation, PreparedShimGitWrite, diff --git a/atomic-repository/src/repository/operation.rs b/atomic-repository/src/repository/operation.rs index 5bcac233..06ef6956 100644 --- a/atomic-repository/src/repository/operation.rs +++ b/atomic-repository/src/repository/operation.rs @@ -970,6 +970,12 @@ impl Repository { .get(&metadata_target) .cloned() .unwrap_or(MetadataValue::Absent); + if is_projected_semantic_capability(&metadata_target) + && matches!(observed, MetadataValue::Sequence(_)) + && matches!(expected_new, MetadataValue::Absent) + { + continue; + } if matches!( (&observed, &expected_new), (MetadataValue::Sequence(_), MetadataValue::Sequence(_)) @@ -1018,6 +1024,9 @@ impl Repository { .delta .metadata .iter() + // Once a projected change is stored or indexed, older clients + // must remain fenced even after its view reference is undone. + .filter(|transition| !is_projected_semantic_capability(&transition.target)) .map(|transition| MetadataTransition { target: transition.target.clone(), expected_old: transition.expected_new.clone(), @@ -3510,11 +3519,15 @@ impl Repository { &transition.expected_new, ) { LeaseClassification::Apply => {} - LeaseClassification::AlreadyApplied => metadata.push(MetadataTransition { - target: transition.target.clone(), - expected_old: transition.expected_new.clone(), - expected_new: transition.expected_old.clone(), - }), + LeaseClassification::AlreadyApplied => { + if !is_projected_semantic_capability(&transition.target) { + metadata.push(MetadataTransition { + target: transition.target.clone(), + expected_old: transition.expected_new.clone(), + expected_new: transition.expected_old.clone(), + }); + } + } LeaseClassification::Diverged => { return Err(metadata_divergence_error(transition, &observed)); } @@ -4041,9 +4054,20 @@ fn validate_capability_lease( ), }); } + if is_projected_semantic_capability(target) && requested < supported.minimum_version() { + return Err(RepositoryError::InvalidOperation { + message: "projected span semantic capability cannot be lowered".into(), + }); + } Ok(()) } MetadataValue::Absent => { + if is_projected_semantic_capability(target) { + return Err(RepositoryError::InvalidOperation { + message: "projected span semantic capability is permanent once installed" + .into(), + }); + } if !SUPPORTED_REPOSITORY_CAPABILITIES .iter() .any(|capability| capability.id() == id.as_str()) @@ -4061,6 +4085,10 @@ fn validate_capability_lease( } } +fn is_projected_semantic_capability(target: &MetadataTarget) -> bool { + matches!(target, MetadataTarget::Capability { id } if id == atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) +} + fn apply_metadata_value_in_write( txn: &mut atomic_core::pristine::WriteTxn<'_>, target: &MetadataTarget, @@ -5368,3 +5396,20 @@ fn sync_directory(path: &Path) -> Result<(), RepositoryError> { } Ok(()) } + +#[cfg(test)] +mod projected_capability_lease_tests { + use super::*; + + #[test] + fn projected_capability_zero_is_rejected_before_preparation() { + let target = MetadataTarget::Capability { + id: atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY + .id() + .to_string(), + }; + assert!(validate_capability_lease(&target, &MetadataValue::Sequence(0)).is_err()); + assert!(validate_capability_lease(&target, &MetadataValue::Sequence(1)).is_ok()); + assert!(validate_capability_lease(&target, &MetadataValue::Absent).is_err()); + } +} diff --git a/atomic-repository/src/repository/project_tree.rs b/atomic-repository/src/repository/project_tree.rs index 97cd75e2..1a945254 100644 --- a/atomic-repository/src/repository/project_tree.rs +++ b/atomic-repository/src/repository/project_tree.rs @@ -1184,8 +1184,8 @@ pub(super) fn git_object_id( } impl Repository { - /// Build the canonical repository manifest and Git tree directly from graph state. - /// Stored graph bytes are used verbatim; no smudge filter or working-copy read occurs. + /// Build the canonical repository manifest and Git tree from recorded view state. + /// Stored bytes are used verbatim; no smudge filter or working-copy read occurs. pub fn project_tree( &self, view_name: &str, @@ -1261,14 +1261,24 @@ impl Repository { ))); } let materialization = attrs.materialization; - let bytes = super::content::retrieve_content_with_filter_fast( + let bytes = match super::content::retrieve_span_if_present( &txn, &self.change_store, item.inode, - item.position, - RetrieveOptions::new().with_graph_visibility(closure.clone()), + &closure, ) - .map_err(|error| ProjectTreeError::Repository(error.to_string()))?; + .map_err(ProjectTreeError::from)? + { + Some(content) => content, + None => super::content::retrieve_content_with_filter_fast( + &txn, + &self.change_store, + item.inode, + item.position, + RetrieveOptions::new().with_graph_visibility(closure.clone()), + ) + .map_err(|error| ProjectTreeError::Repository(error.to_string()))?, + }; let gitlink = if materialization.kind == InodeKind::Gitlink { Some(parse_gitlink_bytes(policy.object_format, &bytes)?) } else { @@ -1371,7 +1381,9 @@ impl Repository { + atomic_core::pristine::TreeTxnT + atomic_core::pristine::PathClaimTxnT + atomic_core::pristine::InodeGraphOps - + atomic_core::pristine::InodeAttrTxnT, + + atomic_core::pristine::InodeAttrTxnT + + atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + + atomic_core::pristine::CapabilityTxnT, { self.project_change_closure_with_conflict_markers(txn, roots, policy, &Default::default()) } @@ -1396,7 +1408,9 @@ impl Repository { + atomic_core::pristine::TreeTxnT + atomic_core::pristine::PathClaimTxnT + atomic_core::pristine::InodeGraphOps - + atomic_core::pristine::InodeAttrTxnT, + + atomic_core::pristine::InodeAttrTxnT + + atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + + atomic_core::pristine::CapabilityTxnT, { use atomic_core::pristine::{EffectiveProjectionClosure, ViewMembershipSet}; @@ -1454,14 +1468,24 @@ impl Repository { let materialization = attrs.materialization; let bytes = match marker_bytes.get(item.path.as_str()) { Some(override_bytes) => override_bytes.clone(), - None => super::content::retrieve_content_with_filter_fast( + None => match super::content::retrieve_span_if_present( txn, &self.change_store, item.inode, - item.position, - RetrieveOptions::new().with_graph_visibility(closure.clone()), + &closure, ) - .map_err(|error| ProjectTreeError::Repository(error.to_string()))?, + .map_err(ProjectTreeError::from)? + { + Some(content) => content, + None => super::content::retrieve_content_with_filter_fast( + txn, + &self.change_store, + item.inode, + item.position, + RetrieveOptions::new().with_graph_visibility(closure.clone()), + ) + .map_err(|error| ProjectTreeError::Repository(error.to_string()))?, + }, }; let gitlink = if materialization.kind == InodeKind::Gitlink && marker_bytes.is_empty() { Some(parse_gitlink_bytes(policy.object_format, &bytes)?) diff --git a/atomic-repository/src/repository/query.rs b/atomic-repository/src/repository/query.rs index dc40eb99..b21b5bc3 100644 --- a/atomic-repository/src/repository/query.rs +++ b/atomic-repository/src/repository/query.rs @@ -100,16 +100,25 @@ impl QueryView<'_> { ) -> Result { plan.run(|path| { let (inode, position) = self.files[path]; - let rendered = super::content::retrieve_content_bounded( + let rendered = match super::content::retrieve_span_bounded_if_present( &self.txn, &self.repo.change_store, inode, - position, - atomic_core::output::alive::RetrieveOptions::new() - .with_graph_visibility(self.visibility.clone()), + &self.visibility, RECORDED_CONTENT_LIMIT, - ) - .map_err(|error| RepositoryError::Database(error.to_string()))?; + )? { + Some(content) => content, + None => super::content::retrieve_content_bounded( + &self.txn, + &self.repo.change_store, + inode, + position, + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(self.visibility.clone()), + RECORDED_CONTENT_LIMIT, + ) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + }; let (bytes, retained_bytes) = match rendered { super::content::BoundedContent::Complete(bytes) => { let retained = bytes.len(); @@ -257,15 +266,23 @@ impl Repository { let scope = self.query_view(view_name)?; let mut sources = Vec::new(); for (path, (inode, position)) in &scope.files { - let bytes = super::content::retrieve_content_with_filter_fast( + let bytes = match super::content::retrieve_span_if_present( &scope.txn, &self.change_store, *inode, - *position, - atomic_core::output::alive::RetrieveOptions::new() - .with_graph_visibility(scope.visibility.clone()), - ) - .map_err(|error| RepositoryError::Database(error.to_string()))?; + &scope.visibility, + )? { + Some(content) => content, + None => super::content::retrieve_content_with_filter_fast( + &scope.txn, + &self.change_store, + *inode, + *position, + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(scope.visibility.clone()), + ) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + }; if let Ok(source) = String::from_utf8(bytes) { sources.push((path.clone(), source)); } diff --git a/atomic-repository/src/repository/record.rs b/atomic-repository/src/repository/record.rs index 473fae36..c4e13d0b 100644 --- a/atomic-repository/src/repository/record.rs +++ b/atomic-repository/src/repository/record.rs @@ -171,7 +171,7 @@ fn record_span_edit( state: &atomic_core::pristine::span_patch_storage::SpanRecordState, namespace_dependencies: Vec, new: &[u8], - trunk: atomic_core::crdt::TrunkId, + trunk: Option, branches: &[atomic_core::crdt::BranchId], algorithm: atomic_core::diff::Algorithm, ) -> Result { @@ -189,6 +189,9 @@ fn record_span_edit( .position .ok_or_else(|| "span edit has no position".to_string())?; let old = state.content.as_slice(); + if trunk.is_none() && cfg!(not(unix)) { + return Err("projected semantic recording is not supported on this platform".into()); + } if atomic_core::record::workflow::compare::detect_encoding(old) == atomic_core::change::Encoding::Binary || atomic_core::record::workflow::compare::detect_encoding(new) @@ -198,13 +201,26 @@ fn record_span_edit( } let old_lines = Line::from_bytes(old); let new_lines = Line::from_bytes(new); - if old_lines.len() != branches.len() { + if trunk.is_some() && old_lines.len() != branches.len() { return Err("semantic lines do not match the span baseline".into()); } + let projection = if trunk.is_none() { + Some( + state + .semantic_snapshot() + .map_err(|error| error.to_string())?, + ) + } else { + None + }; let mut old_offsets = vec![0usize]; for line in &old_lines { old_offsets.push(old_offsets.last().copied().unwrap() + line.len()); } + let mut new_offsets = vec![0usize]; + for line in &new_lines { + new_offsets.push(new_offsets.last().copied().unwrap() + line.len()); + } let mut blocks = Vec::new(); let mut open: Option<(usize, usize)> = None; let (mut old_cursor, mut new_cursor) = (0usize, 0usize); @@ -261,30 +277,49 @@ fn record_span_edit( let mut content = Vec::new(); let mut span_ops = Vec::new(); let mut line_ranges = std::collections::BTreeMap::new(); + let mut ranges = Vec::new(); for (old_start, old_end, new_start, new_end) in blocks { + if trunk.is_none() { + let old_base = old_offsets[old_start]; + let new_base = new_offsets[new_start]; + let old_block = &old[old_base..old_offsets[old_end]]; + let new_block = &new[new_base..new_offsets[new_end]]; + ranges.extend( + projected_token_edit_ranges(old_block, new_block)? + .into_iter() + .map(|(a, b, c, d)| (old_base + a, old_base + b, new_base + c, new_base + d)), + ); + } else { + ranges.push(( + old_offsets[old_start], + old_offsets[old_end], + new_offsets[new_start], + new_offsets[new_end], + )); + } + } + for (start, end, new_start, new_end) in ranges { let insert_start = content.len() as u64; - for (index, line) in new_lines[new_start..new_end].iter().enumerate() { - let start = content.len() as u64; - content.extend_from_slice(line.content()); - line_ranges.insert(new_start + index + 1, (start, content.len() as u64)); + if trunk.is_some() { + let first_line = new_offsets + .binary_search(&new_start) + .map_err(|_| "new line boundary missing")?; + let end_line = new_offsets + .binary_search(&new_end) + .map_err(|_| "new line boundary missing")?; + for (index, line) in new_lines[first_line..end_line].iter().enumerate() { + let start = content.len() as u64; + content.extend_from_slice(line.content()); + line_ranges.insert(first_line + index + 1, (start, content.len() as u64)); + } + } else { + content.extend_from_slice(&new[new_start..new_end]); } let insert_end = content.len() as u64; - let start = old_offsets[old_start]; - let end = old_offsets[old_end]; if start == end { - let segment = state - .spans - .iter() - .find(|span| span.output.start < start && start <= span.output.end) - .or_else(|| state.spans.iter().find(|span| span.output.start == start)); - let (target, offset) = match segment { - Some(segment) => ( - segment.target.clone(), - segment.target_start + (start - segment.output.start) as u64, - ), - None if old.is_empty() && start == 0 => (state.root.clone(), 0), - None => return Err("no authored span anchors the insertion".into()), - }; + let (target, offset) = state + .authored_anchor(start) + .map_err(|error| error.to_string())?; span_ops.push(SpanOp::Edit { file: state.file.clone(), target, @@ -294,45 +329,46 @@ fn record_span_edit( insert_end, }); } else { - let mut cursor = start; let mut first = true; - for segment in &state.spans { - let lo = start.max(segment.output.start); - let hi = end.min(segment.output.end); - if lo >= hi { - continue; - } - if lo != cursor { - return Err("span baseline does not cover the edit".into()); - } - let target_start = segment.target_start + (lo - segment.output.start) as u64; + for segment in state + .authored_ranges(start..end) + .map_err(|error| error.to_string())? + { + let target_start = segment.target_start; span_ops.push(SpanOp::Edit { file: state.file.clone(), - target: segment.target.clone(), + target: segment.target, start: target_start, - end: target_start + (hi - lo) as u64, + end: target_start + (segment.output.end - segment.output.start) as u64, insert_start: if first { insert_start } else { insert_end }, insert_end, }); - cursor = hi; first = false; } - if cursor != end { - return Err("span baseline does not cover the edit".into()); - } } } let encoding = atomic_core::record::workflow::compare::detect_encoding(new); - let context = RecipeContext { - path, - old_content: old, - new_content: new, - existing_branches: Some(branches), - existing_trunk_id: Some(trunk), - encoding, - algorithm, + let (mut ops, stats) = if let Some(trunk) = trunk { + let context = RecipeContext { + path, + old_content: old, + new_content: new, + existing_branches: Some(branches), + existing_trunk_id: Some(trunk), + encoding, + algorithm, + }; + Recipe::InPlaceEdit.build_ops(&context) + } else { + ( + atomic_core::change::FileOps::projected_span_edit( + path.to_string(), + state.file.clone(), + projection.unwrap().id, + ), + Default::default(), + ) }; - let (mut ops, stats) = Recipe::InPlaceEdit.build_ops(&context); let mut mapped = std::collections::BTreeSet::new(); for line in ops.line_ops_mut() { if let Some(number) = line.new_line_num() { @@ -349,7 +385,7 @@ fn record_span_edit( } } } - if mapped.len() != line_ranges.len() { + if trunk.is_some() && mapped.len() != line_ranges.len() { return Err("semantic edits do not cover the changed lines".into()); } let mut recorded = RecordedFile::new(path); @@ -371,12 +407,198 @@ fn record_span_edit( Ok(recorded) } +/// Preserve equal tokens inside a changed line block; assigning the entire +/// replacement line to the current editor would erase original token authors. +fn projected_token_edit_ranges( + old: &[u8], + new: &[u8], +) -> Result, String> { + use atomic_core::diff::{word_diff, Tokenizer, WordDiffOp}; + const MAX_LCS_CELLS: usize = 1_000_000; + let old_full: Vec<_> = Tokenizer::new(old).collect(); + let new_full: Vec<_> = Tokenizer::new(new).collect(); + for (bytes, tokens) in [(old, &old_full), (new, &new_full)] { + let mut cursor = 0usize; + for token in tokens { + let end = cursor + .checked_add(token.len()) + .ok_or("projected semantic token offset overflow")?; + if token.offset() != cursor || bytes.get(cursor..end) != Some(token.content()) { + return Err("projected semantic tokenizer does not cover the content".into()); + } + cursor = end; + } + if cursor != bytes.len() { + return Err("projected semantic tokenizer does not cover the content".into()); + } + } + let prefix = old_full + .iter() + .zip(&new_full) + .take_while(|(a, b)| a == b) + .count(); + let mut suffix = 0usize; + while prefix + suffix < old_full.len() + && prefix + suffix < new_full.len() + && old_full[old_full.len() - suffix - 1] == new_full[new_full.len() - suffix - 1] + { + suffix += 1; + } + let old_begin = old_full + .get(prefix) + .map_or(old.len(), |token| token.offset()); + let new_begin = new_full + .get(prefix) + .map_or(new.len(), |token| token.offset()); + let old_end = old_full + .get(old_full.len() - suffix) + .map_or(old.len(), |token| token.offset()); + let new_end = new_full + .get(new_full.len() - suffix) + .map_or(new.len(), |token| token.offset()); + let old_middle = &old[old_begin..old_end]; + let new_middle = &new[new_begin..new_end]; + let old_count = Tokenizer::new(old_middle).count(); + let new_count = Tokenizer::new(new_middle).count(); + if old_count != 0 + && new_count != 0 + && old_count + .checked_mul(new_count) + .is_none_or(|cells| cells > MAX_LCS_CELLS) + { + return Err("projected semantic token diff exceeds the safe memory budget".into()); + } + let diff = word_diff(old_middle, new_middle); + let old_tokens = diff.old_tokens(); + let new_tokens = diff.new_tokens(); + let old_offset = |index: usize| { + old_tokens + .get(index) + .map_or(old_end, |token| old_begin + token.offset()) + }; + let new_offset = |index: usize| { + new_tokens + .get(index) + .map_or(new_end, |token| new_begin + token.offset()) + }; + let (mut old_cursor, mut new_cursor) = (0usize, 0usize); + let mut open = None; + let mut ranges = Vec::new(); + for op in diff.ops() { + let (old_start, old_end, new_start, new_end, equal) = match op { + WordDiffOp::Equal { + old_range, + new_range, + } => ( + old_range.start, + old_range.end, + new_range.start, + new_range.end, + true, + ), + WordDiffOp::Insert { old_pos, new_range } => { + (*old_pos, *old_pos, new_range.start, new_range.end, false) + } + WordDiffOp::Delete { old_range, new_pos } => { + (old_range.start, old_range.end, *new_pos, *new_pos, false) + } + WordDiffOp::Replace { + old_range, + new_range, + } => ( + old_range.start, + old_range.end, + new_range.start, + new_range.end, + false, + ), + }; + if old_start != old_cursor + || new_start != new_cursor + || old_end > old_tokens.len() + || new_end > new_tokens.len() + { + return Err("invalid projected semantic token diff ordering".into()); + } + if equal { + if let Some((a, c)) = open.take() { + ranges.push(( + old_offset(a), + old_offset(old_cursor), + new_offset(c), + new_offset(new_cursor), + )); + } + if old.get(old_offset(old_start)..old_offset(old_end)) + != new.get(new_offset(new_start)..new_offset(new_end)) + { + return Err("projected semantic token diff changed equal bytes".into()); + } + } else if open.is_none() { + open = Some((old_cursor, new_cursor)); + } + old_cursor = old_end; + new_cursor = new_end; + } + if let Some((a, c)) = open { + ranges.push(( + old_offset(a), + old_offset(old_cursor), + new_offset(c), + new_offset(new_cursor), + )); + } + if old_cursor != old_tokens.len() || new_cursor != new_tokens.len() { + return Err("projected semantic token diff does not cover the content".into()); + } + let old_text = std::str::from_utf8(old).map_err(|error| error.to_string())?; + let new_text = std::str::from_utf8(new).map_err(|error| error.to_string())?; + let mut normalized: Vec<(usize, usize, usize, usize)> = Vec::new(); + for (mut a, mut b, mut c, mut d) in ranges { + // The language-agnostic tokenizer can emit individual UTF-8 bytes. + // Do not assign different authors to fragments of a newly changed + // scalar value simply because its leading bytes happened to match. + while !old_text.is_char_boundary(a) { + a -= 1; + } + while !old_text.is_char_boundary(b) { + b += 1; + } + while !new_text.is_char_boundary(c) { + c -= 1; + } + while !new_text.is_char_boundary(d) { + d += 1; + } + if let Some(previous) = normalized.last_mut() { + if a <= previous.1 || c <= previous.3 { + previous.1 = previous.1.max(b); + previous.3 = previous.3.max(d); + continue; + } + } + normalized.push((a, b, c, d)); + } + let (mut old_at, mut new_at) = (0usize, 0usize); + for &(a, b, c, d) in &normalized { + if a < old_at || c < new_at || old.get(old_at..a) != new.get(new_at..c) { + return Err("projected semantic edits do not preserve equal UTF-8 bytes".into()); + } + old_at = b; + new_at = d; + } + if old.get(old_at..) != new.get(new_at..) { + return Err("projected semantic edits do not preserve equal UTF-8 suffix".into()); + } + Ok(normalized) +} + fn record_binary_span_edit( detected: &atomic_core::record::workflow::DetectedFile, state: &atomic_core::pristine::span_patch_storage::SpanRecordState, namespace_dependencies: Vec, new: &[u8], - trunk: atomic_core::crdt::TrunkId, + trunk: Option, branches: &[atomic_core::crdt::BranchId], algorithm: atomic_core::diff::Algorithm, ) -> Result { @@ -387,6 +609,9 @@ fn record_binary_span_edit( use atomic_core::record::workflow::recipes::{Recipe, RecipeContext}; use atomic_core::record::workflow::{DetectionKind, RecordedFile}; use atomic_core::types::ChangePosition; + if trunk.is_none() && cfg!(not(unix)) { + return Err("projected semantic recording is not supported on this platform".into()); + } let inode = detected .inode .ok_or_else(|| "span edit has no inode".to_string())?; @@ -395,14 +620,14 @@ fn record_binary_span_edit( .ok_or_else(|| "span edit has no position".to_string())?; let mut edits = Vec::new(); let mut cursor = 0usize; - for segment in &state.spans { - if segment.output.start != cursor { - return Err("binary span baseline is incomplete".into()); - } + for segment in state + .authored_ranges(0..state.content.len()) + .map_err(|error| error.to_string())? + { let length = segment.output.end - segment.output.start; edits.push(SpanOp::Edit { file: state.file.clone(), - target: segment.target.clone(), + target: segment.target, start: segment.target_start, end: segment.target_start + length as u64, insert_start: if cursor == 0 { 0 } else { new.len() as u64 }, @@ -410,9 +635,6 @@ fn record_binary_span_edit( }); cursor = segment.output.end; } - if cursor != state.content.len() { - return Err("binary span baseline is incomplete".into()); - } if edits.is_empty() { edits.push(SpanOp::Edit { file: state.file.clone(), @@ -437,6 +659,17 @@ fn record_binary_span_edit( dependencies.sort(); dependencies.dedup(); recorded.set_span_dependencies(dependencies); + let Some(trunk) = trunk else { + let baseline = state + .semantic_baseline_id() + .map_err(|error| error.to_string())?; + recorded.set_crdt_ops(FileOps::projected_span_edit( + detected.path.clone(), + state.file.clone(), + baseline, + )); + return Ok(recorded); + }; let mut ops = if encoding == Encoding::Binary { let mut ops = FileOps::edit(trunk, detected.path.clone()); for branch in branches { @@ -560,6 +793,28 @@ thread_local! { pub(super) static RECORD_APPLY_FAULT: std::cell::Cell = const { std::cell::Cell::new(false) }; } +#[cfg(all(test, unix))] +fn pause_projected_record_for_test(window: &str, operation: atomic_core::OperationId, hash: Hash) { + use atomic_core::types::Base32; + use std::io::Write; + if std::env::var("ATOMIC_TEST_PROJECTED_RECORD_WINDOW") + .ok() + .as_deref() + != Some(window) + { + return; + } + println!( + "ATOMIC_PROJECTED_RECORD_READY {} {}", + operation.to_base32(), + hash.to_base32() + ); + std::io::stdout().flush().unwrap(); + loop { + std::thread::park(); + } +} + #[derive(Debug, Clone)] struct FileMovePlan { old_path: String, @@ -858,6 +1113,9 @@ impl Repository { let file_ops = atomic_core::change::span_patch::localize_file_ops(&txn, &change) .map_err(|error| error.to_string())?; for ops in &file_ops { + if matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. })) { + continue; + } let raw = ops.trunk_id(); let trunk = if raw.change_id().is_root() { TrunkId::new(id, raw.file_idx()) @@ -1107,8 +1365,11 @@ impl Repository { // anchoring corrupts the region. Force the sentinel whole-file // replace: tombstone every prior branch and rebuild fresh, so the // graph and semantic layers agree with the commit tree byte-exactly. - let branch_line_divergence = - existing_branches.len() != atomic_core::diff::Line::from_bytes(old_content).len(); + // Graph-only Git history deliberately has no semantic trunk/branches. + // Absence is not divergence: forcing a full rewrite would replace + // unchanged lines and conflict with independent edits in child views. + let branch_line_divergence = existing_trunk_id.is_some() + && existing_branches.len() != atomic_core::diff::Line::from_bytes(old_content).len(); let graph_line_divergence = self .alive_graph_lines_on_view_excluding(inode, position, view_name, excluded)? != atomic_core::diff::Line::from_bytes(old_content).len(); @@ -2532,6 +2793,10 @@ impl Repository { || crate::content_filter::looks_binary(&content)); if content.len() as u64 > options.max_file_size() && !opaque { if options.skip_binary() { + // No FileAdd will be assembled for this path, so its + // new-inode attributes must not be attached either. + // Attribute-only changes on other files remain intact. + pending_attributes.retain(|attribute| attribute.path != path); skipped_paths.push(path.clone()); stats.files_skipped += 1; continue; @@ -3063,9 +3328,6 @@ impl Repository { if resolution.is_some() { return ModifiedResult::Fatal(RecordError::Database(format!("span recording does not yet support this edit to '{path}'"))); } - let Some(trunk) = existing_trunk_id else { - return ModifiedResult::Fatal(RecordError::Database(format!("span recording requires semantic state for '{path}'"))); - }; let namespace_dependencies = match span_namespace_dependencies(&shared_txn, &shared_graph_visibility, &projected_present, path, file_inode) { Ok(dependencies) => dependencies, Err(error) => return ModifiedResult::Fatal(error), @@ -3079,9 +3341,9 @@ impl Repository { return ModifiedResult::Fatal(RecordError::Database(format!("span recording does not yet support this edit to '{path}'"))); } let result = if binary { - record_binary_span_edit(&detected, state, namespace_dependencies, &repository_bytes, trunk, &existing_branches, core_options.get_algorithm()) + record_binary_span_edit(&detected, state, namespace_dependencies, &repository_bytes, existing_trunk_id, &existing_branches, core_options.get_algorithm()) } else { - record_span_edit(&detected, state, namespace_dependencies, &repository_bytes, trunk, &existing_branches, core_options.get_algorithm()) + record_span_edit(&detected, state, namespace_dependencies, &repository_bytes, existing_trunk_id, &existing_branches, core_options.get_algorithm()) }; return match result { Ok(recorded) => ModifiedResult::Recorded(path.clone(), Box::new(recorded), None), @@ -3293,14 +3555,14 @@ impl Repository { } } } - if let Some(crdt) = recorded.crdt_stats() { - stats.lines_added += crdt.lines_added; - stats.lines_deleted += crdt.lines_deleted; - stats.lines_modified += crdt.lines_modified; - stats.tokens_added += crdt.tokens_added; - stats.tokens_deleted += crdt.tokens_deleted; - stats.tokens_replaced += crdt.tokens_replaced; - } + } + if let Some(crdt) = recorded.crdt_stats() { + stats.lines_added += crdt.lines_added; + stats.lines_deleted += crdt.lines_deleted; + stats.lines_modified += crdt.lines_modified; + stats.tokens_added += crdt.tokens_added; + stats.tokens_deleted += crdt.tokens_deleted; + stats.tokens_replaced += crdt.tokens_replaced; } recorded_paths.push(path); recorded_files.push(*recorded); @@ -3425,6 +3687,12 @@ impl Repository { let semantic_trunks: std::collections::HashMap = change .file_ops() .iter() + .filter(|ops| { + !matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) .map(|ops| (ops.path().to_string(), ops.trunk_id())) .collect(); for pending in pending_attributes { @@ -3727,6 +3995,38 @@ impl Repository { } let mut transitions = Vec::new(); let mut operation_changes = vec![computed_hash]; + if outcome.change().file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) { + use atomic_core::pristine::{ + CapabilityTxnT, SPAN_PROJECTED_SEMANTICS_CAPABILITY, + }; + let capability = txn + .required_capability_version(SPAN_PROJECTED_SEMANTICS_CAPABILITY.id()) + .map_err(|error| RecordError::Database(error.to_string()))?; + match capability { + None => transitions.push(MetadataTransition { + target: MetadataTarget::Capability { + id: SPAN_PROJECTED_SEMANTICS_CAPABILITY.id().to_string(), + }, + expected_old: MetadataValue::Absent, + expected_new: MetadataValue::Sequence(u64::from( + SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version(), + )), + }), + Some(version) + if version == SPAN_PROJECTED_SEMANTICS_CAPABILITY.minimum_version() => { + } + Some(_) => { + return Err(RecordError::Database( + "unsupported projected semantic capability version".into(), + )) + } + } + } if let Some((removal_view_name, removed)) = removal { let removal_view = txn .get_view(removal_view_name) @@ -3847,16 +4147,54 @@ impl Repository { // on disk matches the hash registered in the pristine graph. if options.get_save_to_store() { let save_start = std::time::Instant::now(); - if let Some(v3_bytes) = outcome.v3_bytes() { - // Fast path: write the exact V3 bytes that produced computed_hash - self.save_change_bytes(&computed_hash, v3_bytes, outcome.change()) - .map_err(|e| RecordError::ChangeStore(e.to_string()))?; - } else { - // Fallback: re-serialize (may produce different hash — legacy path) - self.save_change(outcome.change()) - .map_err(|e| RecordError::ChangeStore(e.to_string()))?; + let save_result = (|| -> Result<(), RecordError> { + if outcome.change().file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) { + // The change object may survive an interrupted or undone + // record. Fence older readers before publishing its bytes; + // the prepared metadata operation receipts this monotonic + // capability when it completes. + self.pristine + .require_repository_capability( + atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY, + ) + .map_err(|error| RecordError::Database(error.to_string()))?; + #[cfg(all(test, unix))] + if let Some((_, operation)) = &operation_context { + pause_projected_record_for_test( + "after-fence", + operation.id(), + computed_hash, + ); + } + } + if let Some(v3_bytes) = outcome.v3_bytes() { + // Fast path: write the exact V3 bytes that produced computed_hash + self.save_change_bytes(&computed_hash, v3_bytes, outcome.change()) + .map_err(|e| RecordError::ChangeStore(e.to_string()))?; + } else { + // Fallback: re-serialize (may produce different hash — legacy path) + self.save_change(outcome.change()) + .map_err(|e| RecordError::ChangeStore(e.to_string()))?; + } + Ok(()) + })(); + if let Err(error) = save_result { + if let Some((operation_lock, operation)) = &operation_context { + self.abort_prepared_metadata_operation(operation_lock, operation) + .map_err(RecordError::Repository)?; + } + return Err(error); } outcome.set_saved(true); + #[cfg(all(test, unix))] + if let Some((_, operation)) = &operation_context { + pause_projected_record_for_test("after-save", operation.id(), computed_hash); + } if trace_record { eprintln!( "[record] save_change complete elapsed={:?}", @@ -3886,6 +4224,14 @@ impl Repository { }; match apply_result { Ok(apply_outcome) => { + #[cfg(all(test, unix))] + if let Some((_, operation)) = &operation_context { + pause_projected_record_for_test( + "after-apply", + operation.id(), + computed_hash, + ); + } outcome.set_applied(apply_outcome.new_state); // Every marker this record observed is now consumed (it // emitted its delete) or fulfilled by another route; none @@ -4022,6 +4368,8 @@ impl Repository { if outcome.was_applied() { self.finalize_operation_verified(&operation_lock, operation.id()) .map_err(RecordError::Repository)?; + #[cfg(all(test, unix))] + pause_projected_record_for_test("after-verified", operation.id(), computed_hash); } else { self.abort_prepared_metadata_operation(&operation_lock, &operation) .map_err(RecordError::Repository)?; @@ -4953,3 +5301,46 @@ fn get_inode_position( .ok_or_else(|| format!("Position not found for {}", path))?; Ok((inode, position)) } + +#[cfg(test)] +mod projected_token_tests { + use super::projected_token_edit_ranges; + + #[test] + fn long_equal_prefix_and_suffix_preserve_original_tokens() { + let prefix = "prefix ".repeat(2000); + let suffix = " suffix".repeat(2000); + let old = format!("{prefix}old{suffix}"); + let new = format!("{prefix}new{suffix}"); + let ranges = projected_token_edit_ranges(old.as_bytes(), new.as_bytes()).unwrap(); + assert_eq!( + ranges, + vec![( + prefix.len(), + prefix.len() + 3, + prefix.len(), + prefix.len() + 3 + )] + ); + } + + #[test] + fn token_diff_refuses_large_ambiguous_matrix() { + let old = (0..1100) + .map(|index| format!("old{index} ")) + .collect::(); + let new = (0..1100) + .map(|index| format!("new{index} ")) + .collect::(); + let error = projected_token_edit_ranges(old.as_bytes(), new.as_bytes()).unwrap_err(); + assert!(error.contains("safe memory budget"), "{error}"); + } + + #[test] + fn changed_unicode_scalars_are_never_split_between_authors() { + let ranges = projected_token_edit_ranges("α\n".as_bytes(), "β\n".as_bytes()).unwrap(); + assert_eq!(ranges, vec![(0, 2, 0, 2)]); + let ranges = projected_token_edit_ranges("aé!\n".as_bytes(), "aê!\n".as_bytes()).unwrap(); + assert_eq!(ranges, vec![(1, 3, 1, 3)]); + } +} diff --git a/atomic-repository/src/repository/remote_cache.rs b/atomic-repository/src/repository/remote_cache.rs index 6567f775..4baad67a 100644 --- a/atomic-repository/src/repository/remote_cache.rs +++ b/atomic-repository/src/repository/remote_cache.rs @@ -659,6 +659,26 @@ impl Repository { } } + if change.file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) { + if cfg!(not(unix)) { + return Ok(Err(SubmitRejection::Malformed( + "projected semantic submission is not supported on this platform".into(), + ))); + } + if change.hashed.span.is_none() { + return Ok(Err(SubmitRejection::Malformed( + "projected semantic edit requires a span change".into(), + ))); + } + self.pristine.require_repository_capability( + atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY, + )?; + } self.save_change_bytes(hash, bytes, &change)?; let outcome = match self.insert_change(hash, crate::InsertOptions::default().view(view)) { Ok(outcome) => outcome, diff --git a/atomic-repository/src/repository/sandbox.rs b/atomic-repository/src/repository/sandbox.rs index b498cbd1..441d3e65 100644 --- a/atomic-repository/src/repository/sandbox.rs +++ b/atomic-repository/src/repository/sandbox.rs @@ -427,6 +427,11 @@ impl Repository { })?; let visibility = graph_visibility_closure(&txn, &view)?; let projection = self.project_tree_for_visibility(&txn, &visibility)?; + if !projection.name_conflicts.is_empty() { + return Err(RepositoryError::InvalidOperation { + message: "cannot package a view with unresolved path claims".to_string(), + }); + } let cached_txn = CachedGraphTxn::new(&txn).map_err(|e| RepositoryError::Database(e.to_string()))?; @@ -435,15 +440,25 @@ impl Repository { if item.is_directory { continue; } - let options = RetrieveOptions::new().with_graph_visibility(visibility.clone()); - let bytes = retrieve_content_with_filter_fast( - &cached_txn, + let bytes = match super::content::retrieve_span_if_present( + &txn, &self.change_store, item.inode, - item.position, - options, - ) - .map_err(|e| RepositoryError::Database(e.to_string()))?; + &visibility, + )? { + Some(content) => content, + None => { + let options = RetrieveOptions::new().with_graph_visibility(visibility.clone()); + retrieve_content_with_filter_fast( + &cached_txn, + &self.change_store, + item.inode, + item.position, + options, + ) + .map_err(|e| RepositoryError::Database(e.to_string()))? + } + }; files.insert(item.path, bytes); } Ok(files) diff --git a/atomic-repository/src/repository/semantic_closure.rs b/atomic-repository/src/repository/semantic_closure.rs index 6962d59d..236a2492 100644 --- a/atomic-repository/src/repository/semantic_closure.rs +++ b/atomic-repository/src/repository/semantic_closure.rs @@ -130,6 +130,12 @@ impl SemanticClosureMemo { return; } for ops in change.file_ops() { + if matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) { + continue; + } let trunk = resolve_trunk(change_id, ops.trunk_id()); let state = closure.trunks.entry(trunk).or_default(); for line in ops.line_ops() { diff --git a/atomic-repository/src/repository/semantic_materialize.rs b/atomic-repository/src/repository/semantic_materialize.rs index 430433c6..5f2046ac 100644 --- a/atomic-repository/src/repository/semantic_materialize.rs +++ b/atomic-repository/src/repository/semantic_materialize.rs @@ -107,6 +107,15 @@ impl Repository { hash: hash.to_string(), })?; let change = self.load_change(&hash)?; + if change + .file_ops() + .iter() + .any(|ops| matches!(ops.trunk_op(), Some(TrunkOp::ProjectedSpanEdit { .. }))) + { + return Err(RepositoryError::InvalidOperation { + message: "projected span semantics cannot be materialized as CRDT rows; use the span-derived semantic view".into(), + }); + } if !change.has_file_ops() { continue; } diff --git a/atomic-repository/src/repository/synthesis.rs b/atomic-repository/src/repository/synthesis.rs index 6f68a4ff..fe251ba1 100644 --- a/atomic-repository/src/repository/synthesis.rs +++ b/atomic-repository/src/repository/synthesis.rs @@ -23,6 +23,7 @@ use unicode_normalization::UnicodeNormalization; use atomic_core::change::{ CausalFrontier, Change, ChangeHeader, ChangeKind, ChangeOrigin, ChangeStore, GitDerivation, + GraphOp, }; use atomic_core::operation::{ ActorRef, GitHashAlgorithm, GitObjectId, MetadataTarget, MetadataTransition, MetadataValue, @@ -2956,6 +2957,18 @@ impl Repository { use crate::apply::write_change_to_graph; use atomic_core::apply::compute_new_state; + let span_change = final_change.hashed.span.is_some(); + if span_change { + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + if txn.required_capability_version(SPAN_RANGE_CAPABILITY.id())? + != Some(SPAN_RANGE_CAPABILITY.minimum_version()) + { + return Err(RepositoryError::InvalidOperation { + message: "span capability is not enabled".into(), + }); + } + } + // Deterministic test fault: fails the mutation stage after the // SynthesizeGit intent was journaled, exercising the recovery path. #[cfg(test)] @@ -3046,6 +3059,17 @@ impl Repository { false, crate::InsertStats::new(), ) + } else if span_change { + crate::apply::write_span_hunks_with_index( + txn, + view_name, + change_id, + hash, + final_change, + options, + false, + ) + .map_err(|error| RepositoryError::Apply(error.to_string()))? } else { write_change_to_graph( txn, @@ -3058,6 +3082,87 @@ impl Repository { ) .map_err(|e| RepositoryError::Apply(e.to_string()))? }; + if span_change && !already_applied { + use atomic_core::change::span_patch::SpanOp; + use atomic_core::pristine::{span_patch_storage, TreeTxnT}; + let patch = final_change + .hashed + .span + .as_ref() + .expect("span change checked above"); + let mut roots = std::collections::BTreeMap::new(); + for op in &patch.ops { + if let SpanOp::Root { path, ordinal, .. } = op { + let matches: Vec<_> = final_change + .hunks() + .iter() + .filter_map(|hunk| { + if let GraphOp::FileAdd { + path: added, + add_inode, + contents: None, + .. + } = hunk + { + (crate::canonical_path_identity(added) + == crate::canonical_path_identity(path)) + .then_some(add_inode.start) + } else { + None + } + }) + .collect(); + if matches.len() != 1 { + return Err(RepositoryError::InvalidOperation { + message: format!("span root '{path}' has no unique namespace creation"), + }); + } + let position = atomic_core::types::Position::new(change_id, matches[0]); + let inode = txn.position_inode(position)?.ok_or_else(|| { + RepositoryError::InvalidOperation { + message: format!("span root '{path}' has no inode binding"), + } + })?; + if roots.insert(*ordinal, inode).is_some() { + return Err(RepositoryError::InvalidOperation { + message: "duplicate span root ordinal".into(), + }); + } + } + } + if final_change + .hunks() + .iter() + .filter(|op| matches!(op, GraphOp::FileAdd { .. })) + .count() + != roots.len() + { + return Err(RepositoryError::InvalidOperation { + message: "span change must bind every file addition to one span root".into(), + }); + } + span_patch_storage::index_change( + txn, + &self.change_store, + view_name, + *hash, + final_change, + &roots, + ) + .map_err(|error| RepositoryError::InvalidOperation { + message: format!("span indexing failed: {error}"), + })?; + } else if span_change { + use atomic_core::pristine::span_patch_storage::SpanPatchReadTxn; + if !txn + .span_change_indexed(change_id) + .map_err(|error| RepositoryError::Database(error.to_string()))? + { + return Err(RepositoryError::InvalidOperation { + message: "applied span change is missing its index".into(), + }); + } + } timings.apply_ms = apply_start.elapsed().as_millis(); let commit_start = std::time::Instant::now(); @@ -3411,17 +3516,27 @@ impl Repository { .ok_or_else(|| RepositoryError::InvalidOperation { message: format!("staged path '{path}' inode has no graph position"), })?; - let rendered = retrieve_content_with_filter_fast( + let rendered = match super::content::retrieve_span_if_present( txn, &self.change_store, inode, - position, - atomic_core::output::alive::RetrieveOptions::new() - .with_graph_visibility(visibility.clone()), - ) - .map_err(|error| RepositoryError::InvalidOperation { - message: format!("staged retrieval failed for '{path}': {error}"), - })?; + &visibility, + )? { + Some(bytes) => bytes, + None => retrieve_content_with_filter_fast( + txn, + &self.change_store, + inode, + position, + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(visibility.clone()), + ) + .map_err(|error| { + RepositoryError::InvalidOperation { + message: format!("staged retrieval failed for '{path}': {error}"), + } + })?, + }; if rendered.as_slice() != bytes.as_slice() { return Err(RepositoryError::InvalidOperation { message: format!( @@ -3493,6 +3608,23 @@ impl Repository { ) -> Result<(), RepositoryError> { let verify_start = std::time::Instant::now(); let projection = self.project_tree_for_visibility(txn, visibility)?; + use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; + let span_context = match txn.required_capability_version(SPAN_RANGE_CAPABILITY.id())? { + Some(2 | 3) => Some( + atomic_core::pristine::span_patch_storage::SpanRenderContext::new( + txn, + &self.change_store, + visibility, + ) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + ), + None => None, + Some(version) => { + return Err(RepositoryError::InvalidOperation { + message: format!("unsupported span capability version {version}"), + }) + } + }; let _projection_ms = verify_start.elapsed().as_millis(); // The projection keys paths by the graph's own spelling (raw for // native tracking and current imports, escaped for earlier imports); @@ -3534,17 +3666,32 @@ impl Repository { ), }); } - let rendered = retrieve_content_with_filter_fast( + let rendered = if super::content::has_visible_span_root( txn, &self.change_store, item.inode, - item.position, - atomic_core::output::alive::RetrieveOptions::new() - .with_graph_visibility(visibility.clone()), - ) - .map_err(|error| RepositoryError::InvalidOperation { - message: format!("staged retrieval failed for '{path}': {error}"), - })?; + visibility, + )? { + span_context + .as_ref() + .ok_or_else(|| { + RepositoryError::Database("span context missing for indexed file".into()) + })? + .render(item.inode) + .map_err(|error| RepositoryError::Database(error.to_string()))? + } else { + retrieve_content_with_filter_fast( + txn, + &self.change_store, + item.inode, + item.position, + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(visibility.clone()), + ) + .map_err(|error| RepositoryError::InvalidOperation { + message: format!("staged retrieval failed for '{path}': {error}"), + })? + }; if rendered.as_slice() != expected.bytes.as_slice() { if std::env::var_os("ATOMIC_DEBUG_DELETION").is_some() { let first_diff = rendered diff --git a/atomic-repository/tests/it/cb_n6_merge_rubric_test.rs b/atomic-repository/tests/it/cb_n6_merge_rubric_test.rs index 7d81d107..aeee4c3f 100644 --- a/atomic-repository/tests/it/cb_n6_merge_rubric_test.rs +++ b/atomic-repository/tests/it/cb_n6_merge_rubric_test.rs @@ -121,6 +121,70 @@ fn assert_rename_conflict(repo: &Repository, root: &Path) -> BTreeMap Vec { + let path = image + .join("blobs/sha256") + .join(digest.strip_prefix("sha256:").unwrap()); + let file = std::fs::File::open(path).unwrap(); + let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(file)); + for item in archive.entries().unwrap() { + let mut entry = item.unwrap(); + if entry + .path() + .unwrap() + .to_string_lossy() + .trim_start_matches("./") + == wanted + { + let mut bytes = Vec::new(); + entry.read_to_end(&mut bytes).unwrap(); + return bytes; + } + } + panic!("{wanted} missing from OCI layer {digest}"); + } + fn manifest(image: &std::path::Path, digest: &str) -> serde_json::Value { + let path = image + .join("blobs/sha256") + .join(digest.strip_prefix("sha256:").unwrap()); + serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap() + } + + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().join("repo"); + std::fs::create_dir_all(&root).unwrap(); + let mut repo = Repository::init(&root).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = root.join("source.txt"); + std::fs::write(&path, b"base\n").unwrap(); + repo.add(wc, "source.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("base"), options()) + .unwrap(); + repo.create_draft_view("feature", "dev").unwrap(); + repo.switch_view(wc, "feature").unwrap(); + std::fs::write(&path, b"feature\n").unwrap(); + repo.record(wc, ChangeHeader::new("feature"), options()) + .unwrap(); + std::fs::write(&path, b"unrecorded\n").unwrap(); + + let materialized = dir.path().join("materialized"); + repo.materialize_view_to("feature", &materialized).unwrap(); + assert_eq!( + std::fs::read(materialized.join("source.txt")).unwrap(), + b"feature\n" + ); + let sealed = dir.path().join("sealed"); + let result = repo + .seal(SealOptions { + view: "dev".into(), + out: sealed.clone(), + entrypoint: vec![], + env: vec![], + }) + .unwrap(); + let document = manifest(&sealed, &result.manifest_digest); + assert_eq!(document["layers"].as_array().unwrap().len(), 1); + assert_eq!( + layer_file( + &sealed, + document["layers"][0]["digest"].as_str().unwrap(), + "source.txt" + ), + b"base\n" + ); + let staged = dir.path().join("staged"); + let result = repo + .stage(StageOptions { + view: "feature".into(), + base_view: "dev".into(), + out: staged.clone(), + }) + .unwrap(); + assert_eq!(result.delta_files, 1); + let document = manifest(&staged, &result.manifest_digest); + assert_eq!(document["layers"].as_array().unwrap().len(), 2); + assert_eq!( + layer_file( + &staged, + document["layers"][1]["digest"].as_str().unwrap(), + "source.txt" + ), + b"feature\n" + ); + assert_eq!(std::fs::read(path).unwrap(), b"unrecorded\n"); +} + +#[test] +fn native_span_project_tree_uses_exact_recorded_bytes() { + use atomic_core::operation::GitHashAlgorithm; + use atomic_repository::ConversionPolicy; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let path = dir.path().join("source.txt"); + std::fs::write(&path, b"first\n").unwrap(); + std::fs::write(dir.path().join("empty.txt"), b"").unwrap(); + std::fs::write(dir.path().join("binary.dat"), b"\x00binary\xff").unwrap(); + repo.add(wc, "source.txt", Default::default()).unwrap(); + repo.add(wc, "empty.txt", Default::default()).unwrap(); + repo.add(wc, "binary.dat", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let first = repo + .record(wc, ChangeHeader::new("first"), options()) + .unwrap(); + let first_state = first.new_state().unwrap(); + let policy = ConversionPolicy::new(GitHashAlgorithm::Sha1); + let bytes = |project: &atomic_repository::ProjectTree| { + project + .manifest + .entries + .iter() + .find(|entry| entry.path.as_bytes() == b"source.txt") + .expect("source path in project") + .repository_bytes + .to_vec() + }; + let initial = repo.project_tree("dev", &policy).unwrap(); + assert_eq!(bytes(&initial), b"first\n"); + for (name, expected) in [ + (b"empty.txt".as_slice(), b"".as_slice()), + (b"binary.dat".as_slice(), b"\x00binary\xff".as_slice()), + ] { + assert_eq!( + initial + .manifest + .entries + .iter() + .find(|entry| entry.path.as_bytes() == name) + .unwrap() + .repository_bytes + .as_ref(), + expected + ); + } + std::fs::write(&path, b"second\n").unwrap(); + repo.record(wc, ChangeHeader::new("second"), options()) + .unwrap(); + std::fs::write(&path, b"unrecorded bytes\n").unwrap(); + assert_eq!( + bytes(&repo.project_tree("dev", &policy).unwrap()), + b"second\n" + ); + assert_eq!( + bytes( + &repo + .project_tree_for_change_closure("dev", &[*first.hash()], &policy) + .unwrap() + ), + b"first\n" + ); + assert_eq!( + bytes( + &repo + .project_tree_at_state("dev", first_state, &policy) + .unwrap() + ), + b"first\n" + ); + assert_eq!(std::fs::read(path).unwrap(), b"unrecorded bytes\n"); +} + +#[test] +fn native_span_queries_use_recorded_view_content() { + use atomic_repository::ContentSearchOptions; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + std::fs::create_dir_all(dir.path().join("src")).unwrap(); + let source = dir.path().join("src/lib.rs"); + std::fs::write( + &source, + "pub fn target() {}\npub fn caller() { target(); }\n", + ) + .unwrap(); + repo.add(wc, "src/lib.rs", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + repo.record(wc, ChangeHeader::new("base"), options()) + .unwrap(); + repo.create_draft_view("feature", "dev").unwrap(); + repo.switch_view(wc, "feature").unwrap(); + std::fs::write( + &source, + "pub fn target(secret: SiblingSecret) {}\npub fn caller() {}\n", + ) + .unwrap(); + std::fs::write( + dir.path().join("src/sibling.rs"), + "pub fn sibling_only() { target(); }\n", + ) + .unwrap(); + repo.add(wc, "src/sibling.rs", Default::default()).unwrap(); + repo.record(wc, ChangeHeader::new("feature"), options()) + .unwrap(); + let search = |view: &str, pattern: &str| { + repo.search_content_on_view(view, pattern, ContentSearchOptions::default()) + .unwrap() + .total_matches + }; + let callers = |view: &str| { + repo.query_callers_on_view(view, "src/lib.rs#target") + .unwrap() + .into_iter() + .map(|caller| format!("{}#{}", caller.path, caller.name)) + .collect::>() + }; + assert_eq!(search("feature", "SiblingSecret"), 1); + assert_eq!(search("dev", "SiblingSecret"), 0); + assert_eq!(search("dev", "target"), 2); + assert_eq!(callers("feature"), ["src/sibling.rs#sibling_only"]); + assert_eq!(callers("dev"), ["src/lib.rs#caller"]); + std::fs::write(&source, "pub fn disk_only() {}\n").unwrap(); + assert_eq!(search("feature", "disk_only"), 0); + assert_eq!(callers("dev"), ["src/lib.rs#caller"]); + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert_eq!( + repo.search_content_on_view("feature", "SiblingSecret", ContentSearchOptions::default()) + .unwrap() + .total_matches, + 1 + ); + assert_eq!( + repo.query_callers_on_view("dev", "src/lib.rs#target") + .unwrap()[0] + .name, + "caller" + ); +} + #[test] fn span_unrecorded_file_stays_untracked_when_switching_views() { use atomic_core::operation::{EffectTarget, OperationKind, OperationScope}; @@ -1515,15 +1843,17 @@ fn missing_span_root_refuses_without_overwriting_working_bytes() { let path = dir.path().join("file.txt"); std::fs::write(&path, b"recorded\n").unwrap(); repo.add(wc, "file.txt", Default::default()).unwrap(); - repo.record( - wc, - ChangeHeader::new("root"), - RecordOptions::new() - .with_all(true) - .save_to_store(true) - .apply_after_record(true), - ) - .unwrap(); + let recorded = repo + .record( + wc, + ChangeHeader::new("root"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + let recorded_hash = *recorded.hash(); let inode = repo .pristine() .read_txn() @@ -1547,6 +1877,47 @@ fn missing_span_root_refuses_without_overwriting_working_bytes() { let repo = Repository::open(dir.path()).unwrap(); let error = repo.get_file_content("file.txt").unwrap_err(); assert!(error.to_string().contains("span root missing"), "{error}"); + let error = repo + .search_content_on_view("dev", "recorded", Default::default()) + .unwrap_err(); + assert!(error.to_string().contains("span root missing"), "{error}"); + let error = repo.query_callers_on_view("dev", "target").unwrap_err(); + assert!(error.to_string().contains("span root missing"), "{error}"); + let policy = + atomic_repository::ConversionPolicy::new(atomic_core::operation::GitHashAlgorithm::Sha1); + let error = repo.project_tree("dev", &policy).unwrap_err(); + assert!(error.to_string().contains("span root missing"), "{error}"); + let error = repo + .project_tree_for_change_closure("dev", &[recorded_hash], &policy) + .unwrap_err(); + assert!(error.to_string().contains("span root missing"), "{error}"); + let output = dir.path().join("should-not-materialize"); + let error = repo.materialize_view_to("dev", &output).unwrap_err(); + assert!(error.to_string().contains("span root missing"), "{error}"); + assert!(!output.exists()); + let output = dir.path().join("should-not-stage"); + let error = match repo.stage(atomic_repository::StageOptions { + view: "dev".into(), + base_view: "dev".into(), + out: output.clone(), + }) { + Ok(_) => panic!("stage accepted a missing span root"), + Err(error) => error, + }; + assert!(error.to_string().contains("span root missing"), "{error}"); + assert!(!output.exists()); + let output = dir.path().join("should-not-seal"); + let error = match repo.seal(atomic_repository::SealOptions { + view: "dev".into(), + out: output.clone(), + entrypoint: vec![], + env: vec![], + }) { + Ok(_) => panic!("seal accepted a missing span root"), + Err(error) => error, + }; + assert!(error.to_string().contains("span root missing"), "{error}"); + assert!(!output.exists()); std::fs::write(&path, b"user work\n").unwrap(); let error = repo.materialize(wc).unwrap_err(); assert!(error.to_string().contains("span root missing"), "{error}"); diff --git a/docs/git-span-import-handoff.md b/docs/git-span-import-handoff.md new file mode 100644 index 00000000..98dc271b --- /dev/null +++ b/docs/git-span-import-handoff.md @@ -0,0 +1,311 @@ +# Git span import: working handoff + +This is an **unfinished experiment**, not a production-ready import path. +Work is in `/Users/leefaus/Projects/atomic-span-clean` on +`checkpoint/span-native-integration`, based on Git `33a60686`. The working +copy contains substantial uncommitted harness and source work; in particular, +`atomic-cli/src/commands/git/span_compiler.rs` is a **new untracked file**. +Do not discard or overwrite the existing changes. The original +`/Users/leefaus/Projects/atomic` checkout was inspected read-only and remains +on `fix/insert-session-diff` at `c7e6a68a`. + +## What was ported + +The earlier branch's `atomic-cli/src/commands/git/span_compiler.rs` and its +private streaming approach informed a narrow port. A CLI built with +`--features experimental-span-import` exposes `atomic git import --span` for +a **fresh, first-parent** import. It lowers Git content changes to schema-3 +spans while retaining graph namespace/attribute operations, Git facts, +private batches and final-tip verification. Git similarity renames remain +delete/add, not invented inode continuity. This feature is **not enabled by +default** and does not migrate an existing repository. + +The old branch's work log reports a previous Hyperfine experiment at 6.30 MB +compacted span redb versus 54.27 MB linewise for 1,097 first-parent commits. +Those are historical experimental measurements, **not** measurements of this +port. The historical run skipped native bootstrap and shim checkpointing, so +the two experiments are not identical. + +## Paired storage measurements + +One release run using the same feature-enabled binary for both paths imported +1,097 first-parent commits at Hyperfine tip +`c5161524980522320c7d3425e714cc0965a869a5` and checked all 75 selected files +per clone. The binary SHA-256 was +`a28fc216c3c5cffdec3192c152434102f6aae76b5349b9a873df211b330b7c99`; +it predates the latest import and shim preflight changes. After compaction: + +| Measure | Span | Linewise | +|---|---:|---:| +| redb logical bytes | 35,524,608 | 80,285,696 | +| entire `.atomic` bytes | 44,270,109 | 90,872,593 | +| import time, one run | 49.78 s | 46.07 s | + +Span dirty restore failed separately; it is not a successful benchmark case. +These are individual observations, not a timing distribution or a full +equivalence proof. + +Read-only redb table statistics taken after the restore probes showed that +`git_commit_closures` alone has 1,097 rows, 19,320,364 stored bytes, 45,827 +metadata bytes and 8,068,817 fragmented bytes in each database. The span +tables have 2,362 span nodes and 4,278 edits; the graph and inode-graph tables +have 732 and 688 edges, respectively. The linewise graph has approximately +500,000 edges in each index. Both database SHA-256 hashes were unchanged by +the table inspection. The restore probes mean these table snapshots are not +identical to the preceding immediate post-compaction size snapshots. + +Closure rows persist exact interpreted Git parent visibility and reject a +conflicting reimport. The repeated full hash lists identified a missing port: +the documented historical run used `git-closure-sharing`, a content-addressed +trie sharing unchanged subtrees. That implementation and its capability fence +have now been ported, retaining legacy-row reads, immutable conflict checks, +and fail-closed validation of referenced nodes. +The fresh stream passes the hashes of all preceding streamed commits to +`apply_synthesized_change_in_txn`, which persists the complete closure for +each Git SHA in the same transaction as its change. These rows are therefore +written during the initial import, not by an incremental import. They also +support later parent interpretation, including across runs and views; the + the earlier refusal of incremental import into span repositories did not + remove that persistence requirement. + +A second one-run release comparison after porting closure sharing used the +same Git tip, 1,097 first-parent commits, and 75 checked files per clone. The +binary SHA-256 was +`a1e57c769e7121976c7e8adc0d76cf9cacb6d62f1eaf7c59543b0a66507d45f6`: + +| Measure | Span | Linewise | +|---|---:|---:| +| Compacted redb logical bytes | 9,592,832 | 54,460,416 | +| Entire `.atomic` bytes | 18,338,385 | 65,047,366 | +| Import time, one run | 40.95 s | 32.24 s | + +Span dirty restore still failed separately. Read-only inspection after that +probe showed 10,960 shared closure nodes (1,052,160 stored bytes) and 1,097 +closure roots (92,148 stored bytes), with the database hash unchanged by +inspection. The result is **not the 6.30 MB target**: unlike the historical +experiment, this run retained normal `.atomicignore` bootstrap and shim +checkpointing. KG tables also hold substantial data in the current run; their +contribution to the difference has not been isolated with an otherwise matched +experiment. In particular, native bootstrap's `kg_enrich_change` calls +`project_view_kg`, which creates change nodes and direct membership edges for +every member of every view, including the private import stage. Do not omit +these rows from a normal import to claim the historical size. A later source +change only fixes a Clippy lint and strengthens the 16-commit test; the measured +binary predates those changes. A separate debug benchmark was invalidated when +its binary was replaced during the run and must not be used as evidence. + +## Verified so far + +- The port's 16-commit real-Git unit fixture passed: all selected files at + every imported commit matched the Git trees; current materialization, + clean status and reopen also passed. Every tested Git change had a typed + span section and no fabricated historical FileOps. Exact Git interpretation + closures were also verified for every commit before and after reopen. +- Nine closure-sharing tests passed, covering arbitrary and legacy vectors, + shared histories, corruption, idempotence, rollback, and capability fencing. +- `SpanRecordState::authored_ranges` and `authored_anchor` preserve actual + change owners across rendered span boundaries and reject invalid mappings. + Native span recording now uses these checked mappings. A real two-commit Git + fixture verifies that the resulting bytes come from multiple original Git + changes, both before and after reopening. This is a building block, not a + historical semantic-trunk implementation; the projected native bridge below + uses these mappings without fabricating historical CRDT rows. +- A five-commit compiler replay test verifies that recompiling the first two + imported commits after reopen reproduces their exact persisted hashes, and + the remaining three hashes match a fresh five-commit import. +- `Repository::validate_span_git_prefix` checks a replayed prefix against the + selected view, Git SHA index, content-addressed changes, original Git parents, + dependency index and persisted interpretation closures. The replay test + covers wrong hashes, order, missing commits and a view that cannot see the + prefix, and verifies database bytes remain unchanged. This is a read-only + check, not a tree proof, a cleanliness check, or a lease; callers must repeat + it under operation locks before publication. +- An internal, restricted `GitGraphImportSession::begin_span_incremental` + reuses the existing private stage only when both target and stage contain + the verified Git prefix in order, the target tree matches the pinned baseline, + and no inherited members or unrelated native changes are present. A single + structurally verified native `.atomicignore` creation is retained and checked + by bytes, mode and kind through promotion. It leases the + unchanged target, journals private batches, rejects unrelated existing SHA + identities and reuses a matching identity only after a verified inverse + recovery operation, + and promotes only after final tree verification. Focused real-Git tests cover + two successive updates, wrong baseline, SHA collision, wrong final tree, + native-change refusal and reopening. A real CLI-initialized import preserves + the recorded `.atomicignore`, its unrecorded working edit, Git HEAD and index + while the internal continuation publishes later commits. An abandoned + **verified** private stage can resume only after exact replay and verification + of its reachable batch operations in both journal scopes; a wrong staged hash + refuses without journal changes. Other native changes and additional + vault/private files remain unsupported. +- The feature-enabled CLI accepts `atomic git import --span --incremental` + for an attached, clean, compatible first-parent checkout. It pins and + repeatedly rechecks Git HEAD and index, privately stages verified span changes, + and refreshes the verified shim checkpoint after promotion. Real process + probes passed two updates including deletion, binary content, an executable + and a symlink; status and diff succeeded afterward, while untracked bytes, + Git HEAD and index were preserved. Dry-run remains explicitly unsupported. +- A real child-process kill regression exercises before-commit, after-commit, + after-metadata and after-verification windows in a two-change private batch; + verified recovery and an exact retry preserve prior and untracked bytes. + Another child kill after promotion but before checkpoint refresh verifies an + idempotent retry restores checkpoint coherence. These are targeted macOS + fixtures, not complete CLI or cross-platform crash coverage. +- The span semantic projection is now tested against two independently edited + views in both insertion orders: line/token fragments retain their original + owners and reconstruct the recorded bytes. A separate corruption test removes + a visible edit-index row, reopens the database, and confirms projection refuses + instead of silently attributing incomplete content. +- Projected lines, tokens and complete semantic baselines have domain-separated + identities derived from the portable file and exact authored byte fragments, + not repository-local node IDs or invented single-author CRDT identities. A + test reindexes the same immutable changes into a second pristine with a + different inode and change IDs, compares identities in both view orders, and + checks both repositories after reopening. Projected native edits now store a + baseline identity in a typed semantic FileOp, checked against their exact + dependency closure. Equal tokens keep their original authored spans; large + token matrices fail closed rather than rewriting attribution. The separate + repository transfer regression copies exact change bytes, inserts dependency + closure, materializes and reopens; it does not exercise network transport. +- Incremental span import refuses `GIT_INDEX_FILE` rather than observing a + different index from the caller's Git process. The observer rechecks HEAD, + index bytes, the index lock and Git operation state after collecting status; + a real CLI regression verifies the refusal preserves both index files, HEAD, + working bytes and the database. It also refuses index entries marked + `assume-unchanged` or `skip-worktree`, which can hide tracked edits from Git + status; a process test exercises both flags and preserves the hidden bytes. + These observations do not lock Git itself: + a Git writer can still race after the final check, so this is not a complete + concurrent-mutation guarantee. +- Explicit span/graph format fencing and refusal of an already-staged path + before capability installation passed focused tests. +- Focused native-edit tests now exercise two successive edits, independent + divergent views, overlap refusal, wrong-baseline/path refusal, empty files, + binary/text transitions, CRLF, Unicode, and mixed projected/CRDT files in + one record. Real CLI tests cover record, blame, recorded diff, and dirty + restore. A macOS child-process test kills recording after capability fencing, + object save, graph apply and verification; recovery and retries preserve the + working bytes and capability. Undo retains the capability. A pre-bridge span + binary refused normal access without changing database or Git bytes. +- One broad rerun timed out during an undo test; 60 subsequent isolated repeats + passed. The cause of the timeout is not established, so it is not a full pass. +- A two-commit, one-file *debug* benchmark smoke test checked the Git blobs, + modes, schema headers, import and compaction. It demonstrated that dirty + `restore --force` succeeds on the graph control and refuses on the span + candidate. This tiny fixture is not evidence of a storage improvement. +- Default workspace `cargo check` and Clippy passed on Rust 1.98; core and + repository checked on Rust 1.90. Feature-enabled CLI Clippy passed for the + binary target. Feature-enabled `--all-targets` Clippy did **not** pass: it + reported `manual_is_multiple_of` in the existing real-import sample and + `items_after_test_module` in the previously modified service module. +- `git diff --check` passed at the stopping point. +- Ordinary full, incremental, dry-run and detached shim import into an existing + span repository still refuse at capability preflight. The explicit span-aware + incremental command uses a separate capability-checked path. Focused tests preserve raw + database and Git state; CLI probes also compared both reachable operation + DAGs and receipts. A linked-worktree regression exercises import and shim + refusal without creating local Atomic state. The preflight retains the + common lock through the operation; a concurrent initialization test and + an ordinary incremental-import regression passed. Focused synthetic tests + also refused a legacy-named span database and an unreadable database without + migration or repair; these do not simulate a redb crash-recovery condition. + +The local `dev` release CLI used for read-only project context was built from +Git commit `54efd7452f546b22796c479e5496d77b3651400a` in an isolated source +archive. With `ATOMIC_SERVICE=local` and a read-only sandbox, it can read goals, +memories, intents and indexed code from `/Users/leefaus/Projects/atomic`. +The versioned binary at +`/private/var/folders/hl/fwbg_jc93tvgz43rj7t25x240000gn/T/opencode/atomic-dev-release-jXW6jjFC/target/release/atomic` +reports `atomic 0.20.0 (dev 54efd7452f54)` and has SHA-256 +`82def01327c96273e7b5200a85afe4ec6d3704f27b05ac1199f969217327a391`. +The adjacent `atomic-context` wrapper runs it from the original checkout with +local mode and a sandbox denying writes outside the approved temporary root. +The linked worktree still requires working-copy identity migration; no writable +open or migration was performed to bypass that gate. The original repository's +index describes its own recorded view, not the uncommitted clean-worktree +source, so current code must still be checked directly. + +## Later safety and regression work + +- Installing projected semantics permanently raises the repository schema to + 2 and requires `span-projected-semantics`; ordinary repositories remain on + schema 1. Both directions of the schema/capability consistency check fail + closed, and legacy merge preserves the source fence. Undo cannot remove it; + projected capability leases reject version zero and downgrades. +- The schema registry now includes all five span index tables. Legacy merge + copies and digests their rows, and detects later mutation. This fixes an + earlier fail-closed refusal of real span databases: the previous registry + test did not scan the file defining those tables. +- An older span-capable client refused status and compaction on schema 2. Its + compactor nevertheless opened redb writable before refusing: in a disposable + test the raw database bytes changed. The checked operation histories, change + objects, Git state and working bytes survived. After registering span tables, + the fixture's 80 populated registered tables and 384 rows also matched by + digest before and after the refusal. This is still not a no-mutation pass or + a general guarantee for other databases. Current compaction preflights + read-only, checks operation state and capabilities, and holds the common lock. +- Projected change objects are staged, synced and published without overwrite; + collision, concurrent publication and process-kill tests exercised refusal and + recovery. Projected redb changes survive reopen and export/reimport; selective + span reads remain fenced. Non-Unix projected publication is unsupported. +- A corrected harness run through suite 42 on the candidate identified below + produced 30 passed, 16 failed and two partial suites; suite 15 needs a + separate PostgreSQL-backed server and was not run. Suite 03 passed after an + earlier timeout. The same 23 suite 17 merge failures and the relevant suite + 22, 28 and 40 failures also occur on an independently built pre-bridge + control. Suite 18 exceeded its 30-second budget while other builds ran. + The partial suites skip shared-child coverage and unproven contention, + respectively; none of these failures or skips are passes. +- Suite 05's missing semantic counts came from graph-edit statistics being + accumulated only for span edits. That accounting bug is now fixed, and the + Atomic-only suite passed 209/209 on a subsequent debug build. Its no-final- + newline case still deliberately uses the existing whole-file replacement + safety fallback; this fix does not change provenance or graph operations. +- A later candidate (SHA-256 + `648449396d72a488b7c60d85ac0ce44038fa5e20381b5766aa8a67c694c6b7fa`) + measured 9,515,008 compacted span bytes versus 54,435,840 graph bytes for + the same 1,097 commits and 75 files. Both single-file dirty-restore probes + passed with Git index unchanged. A second run measured 9,576,448 versus + 54,456,320 bytes and preserved exact post-compaction snapshots: both had + 25,345 KG rows and 3,119,615 stored KG bytes. Runs overlapped other work, so + timings are not reliable. These results predate the latest registry and + capability-lease fixes; no final-source matched benchmark or full final + harness pass has been established. + +## Blockers and safety gates + +1. The new projected semantic bridge is experimental and needs independent + review and broader crash, remote transport, recovery and adversarial tests. + Git history still has no fabricated CRDT rows. Generic CRDT-only consumers + must refuse projected operations rather than silently discard them. +2. The explicit incremental span command remains experimental and constrained; + native changes beyond the `.atomicignore` bootstrap, vault/private files, + detached/other views, dry-run, concurrent Git mutation and broader recovery + require further validation. Ordinary Git import and shim reconciliation still + refuse span repositories before writable effects. +3. The imported graph-only span changes need transport, cross-view insertion, + clone, repair, crash and older-client fencing tests before promotion. + The 16-commit test is not a complete history or recovery proof. +4. Check the port's private batch atomicity, capability/lock ordering, staged + verification and identity-loss metadata against the older branch's tests. + Do not weaken operation leases, receipts, recovery or final-tip proof. +5. Repeat the paired storage comparison with a fingerprinted final binary + after source changes stabilize; the historical dirty-restore failure is not + evidence about the new bridge. +6. Rerun the affected harness and workspace checks on the final source. The + previous 01–14 passes were from different builds; 10–13 exercise Git + import and do not prove native span history. Later overlap/name-conflict + and Git-shadow failures remain open. + +## Scratch artifacts + +`/private/var/folders/hl/fwbg_jc93tvgz43rj7t25x240000gn/T/opencode/span-harness01-1gFvn7/` +contains `test-git-span-port.sh`, `probe-git-span-cli.sh`, +`build-git-span-release.sh`, `measure-git-span-storage.py`, the debug candidate +`atomic-debug-git-span`, fixture logs and earlier harness results. This is +temporary storage and may be cleaned by the system. The measured feature-enabled +release build completed with `rust-objcopy` missing-`libLLVM.dylib` warnings; +it is not a final release candidate. + +No remote push or OpenCode hook-pin change was made. No commit or checkpoint +was created for this unfinished work. diff --git a/libatomic/proto/atomic/services/repository_mutation/repository_mutation_messages.proto b/libatomic/proto/atomic/services/repository_mutation/repository_mutation_messages.proto index ff1ccea5..1970bfe3 100644 --- a/libatomic/proto/atomic/services/repository_mutation/repository_mutation_messages.proto +++ b/libatomic/proto/atomic/services/repository_mutation/repository_mutation_messages.proto @@ -121,6 +121,8 @@ message RecordRequest { // AI-assisted (vendor, model, tool, suggestion type, tokens, session); // absent → a human-authored record, provenance-free. optional RecordAiAuthorship ai_authorship = 8; + optional bool skip_binary = 9; + optional uint64 max_file_size = 10; } // One record's AI authorship (add-only): the string vocabularies @@ -143,9 +145,16 @@ message RecordResponse { optional string view = 3; // view the change landed on (add-only) optional RecordStatistics statistics = 4; // actual recorder statistics repeated string recorded_paths = 5; + repeated string skipped_paths = 6; + repeated RecordFileError file_errors = 7; ResponseMeta meta = 100; } +message RecordFileError { + string path = 1; + string message = 2; +} + // Recorded graph and semantic counts. Older services may omit this message. message RecordStatistics { uint64 files_recorded = 1; diff --git a/libatomic/proto/atomic/services/vault/vault_messages.proto b/libatomic/proto/atomic/services/vault/vault_messages.proto index ffa506cc..11d813cb 100644 --- a/libatomic/proto/atomic/services/vault/vault_messages.proto +++ b/libatomic/proto/atomic/services/vault/vault_messages.proto @@ -247,6 +247,8 @@ message CreateVaultEntityRequest { // Intent classification and optional reviewed target (add-only). optional string intent_kind = 12; optional string review_target = 13; + optional string memory_id = 14; // explicit memory filename stem + optional string memory_status = 15; // active|superseded|retracted ViewRef view = 20; ViewSnapshot expected_snapshot = 21; } diff --git a/libatomic/src/daemon/services.rs b/libatomic/src/daemon/services.rs index 928b5d69..adc52ea8 100644 --- a/libatomic/src/daemon/services.rs +++ b/libatomic/src/daemon/services.rs @@ -117,6 +117,11 @@ const METHOD_MATRIX: &[(&str, &str)] = &[ ("ViewService", "SplitView"), ]; +/// Feature gate for the add-only explicit memory ID and status fields. +pub const VAULT_MEMORY_CREATE_OPTIONS_FEATURE: &str = "vault-memory-create-options-v1"; +/// Feature gate for add-only record size and binary options. +pub const RECORD_FILE_OPTIONS_FEATURE: &str = "record-file-options-v1"; + #[tonic::async_trait] impl daemon_service_server::DaemonService for DaemonImpl { async fn health( @@ -203,7 +208,11 @@ impl daemon_service_server::DaemonService for DaemonImpl { implementation_version: env!("CARGO_PKG_VERSION").to_string(), endpoint_kind: EndpointKind::LocalDaemon as i32, methods, - features: vec!["full-turn-slice".to_string()], + features: vec![ + "full-turn-slice".to_string(), + VAULT_MEMORY_CREATE_OPTIONS_FEATURE.to_string(), + RECORD_FILE_OPTIONS_FEATURE.to_string(), + ], limits: Some(Limits { max_message_bytes: 64 * 1024 * 1024, max_frozen_page_fragments: 256, @@ -1498,6 +1507,8 @@ impl repository_mutation_service_server::RepositoryMutationService for MutationI let identity_name = request.identity_name; let expected = request.expected; let ai = request.ai_authorship; + let skip_binary = request.skip_binary.unwrap_or(false); + let max_file_size = request.max_file_size; let result = tokio::task::spawn_blocking(move || { if message.trim().is_empty() { return Err(domain_status( @@ -1529,7 +1540,12 @@ impl repository_mutation_service_server::RepositoryMutationService for MutationI header = header.author(author); } let header = header.build(); - let mut options = RecordOptions::new().message(message.clone()); + let mut options = RecordOptions::new() + .message(message.clone()) + .with_skip_binary(skip_binary); + if let Some(max_file_size) = max_file_size { + options = options.with_max_file_size(max_file_size); + } if !paths.is_empty() { options = options.paths(paths.clone()); } @@ -1563,7 +1579,30 @@ impl repository_mutation_service_server::RepositoryMutationService for MutationI } let outcome = repo .record(working_copy, header, options) - .map_err(|error| domain_status(ErrorCode::ChangeRejected, error.to_string()))?; + .map_err(|error| { + let mut status = domain_status(ErrorCode::ChangeRejected, error.to_string()); + if let atomic_repository::RecordError::FileTooLarge { path, size, limit } = + error + { + status.metadata_mut().insert( + "atomic-record-error", + "file-too-large".parse().expect("static metadata"), + ); + status.metadata_mut().insert_bin( + "atomic-file-path-bin", + tonic::metadata::MetadataValue::from_bytes(path.as_bytes()), + ); + status.metadata_mut().insert( + "atomic-file-size", + size.to_string().parse().expect("numeric metadata"), + ); + status.metadata_mut().insert( + "atomic-file-limit", + limit.to_string().parse().expect("numeric metadata"), + ); + } + status + })?; let change = outcome.change(); Ok::<_, Status>(RecordResponse { change: Some(ChangeInfo { @@ -1603,6 +1642,15 @@ impl repository_mutation_service_server::RepositoryMutationService for MutationI tokens_replaced: outcome.stats().tokens_replaced as u64, }), recorded_paths: outcome.recorded_files().to_vec(), + skipped_paths: outcome.skipped_files().to_vec(), + file_errors: outcome + .errors() + .iter() + .map(|(path, message)| RecordFileError { + path: path.clone(), + message: message.clone(), + }) + .collect(), meta: Some(ResponseMeta { request_id: meta.request_id, replayed: false, diff --git a/libatomic/src/daemon/services_agent.rs b/libatomic/src/daemon/services_agent.rs index d22d8a3f..425a0224 100644 --- a/libatomic/src/daemon/services_agent.rs +++ b/libatomic/src/daemon/services_agent.rs @@ -1342,6 +1342,28 @@ impl vault_service_server::VaultService for VaultImpl { .memory_kind .clone() .unwrap_or_else(|| "context".to_string()); + if !atomic_canonical::vocab::MEMORY_KIND.contains(&memory_kind.as_str()) { + return Err(domain_status( + ErrorCode::InvalidArgument, + format!("unknown memory kind '{memory_kind}'"), + )); + } + let memory_status = request + .memory_status + .clone() + .unwrap_or_else(|| "active".to_string()); + if !atomic_canonical::vocab::MEMORY_STATUS.contains(&memory_status.as_str()) { + return Err(domain_status( + ErrorCode::InvalidArgument, + format!("unknown memory status '{memory_status}'"), + )); + } + let status = match memory_status.as_str() { + "active" => VaultEntityStatus::Active, + "superseded" => VaultEntityStatus::Superseded, + "retracted" => VaultEntityStatus::Retracted, + _ => unreachable!("validated memory status"), + }; let text = request.body.clone().unwrap_or_default(); if text.trim().is_empty() { return Err(domain_status( @@ -1351,8 +1373,24 @@ impl vault_service_server::VaultService for VaultImpl { } let derived_from = request.derived_from.clone(); let about = request.about.clone(); - let id = ulid::Ulid::new().to_string().to_lowercase(); - let vault_path = format!("memory/{id}.md"); + let id = request + .memory_id + .clone() + .unwrap_or_else(|| ulid::Ulid::new().to_string().to_lowercase()); + let stem = id.strip_prefix("memory/").unwrap_or(&id); + let stem = stem.strip_suffix(".md").unwrap_or(stem); + if stem.trim().is_empty() + || stem == "." + || stem == ".." + || stem.contains(['/', '\\']) + || stem.chars().any(char::is_control) + { + return Err(domain_status( + ErrorCode::InvalidArgument, + "memory ID must be a single non-empty filename stem", + )); + } + let vault_path = format!("memory/{stem}.md"); let body = format!("{}\n", text.trim()); let body_for_store = body.clone(); let handle_for_task = handle.clone(); @@ -1363,10 +1401,55 @@ impl vault_service_server::VaultService for VaultImpl { let body = body_for_store; move || { let repo = handle.repository()?; + if repo + .vault_retrieve(&vault_path) + .map_err(repository_error)? + .is_some() + { + return Err(Status::already_exists(format!( + "memory '{id}' already exists" + ))); + } + let vault_root = repo.root().join(".vault"); + for directory in [&vault_root, &vault_root.join("memory")] { + match std::fs::symlink_metadata(directory) { + Ok(metadata) + if metadata.is_dir() && !metadata.file_type().is_symlink() => {} + Ok(_) => { + return Err(Status::failed_precondition(format!( + "memory directory '{}' is not a real directory", + directory.display() + ))) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => { + return Err(Status::internal(format!( + "cannot inspect memory directory '{}': {error}", + directory.display() + ))) + } + } + } + let disk_path = vault_root.join(&vault_path); + match std::fs::symlink_metadata(&disk_path) { + Ok(_) => { + return Err(Status::already_exists(format!( + "memory file '{}' already exists", + disk_path.display() + ))) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => { + return Err(Status::internal(format!( + "cannot inspect memory file '{}': {error}", + disk_path.display() + ))) + } + } let mut frontmatter = Map::new(); frontmatter.insert("uid".into(), Value::String(id)); frontmatter.insert("memoryKind".into(), Value::String(memory_kind)); - frontmatter.insert("status".into(), Value::String("active".into())); + frontmatter.insert("status".into(), Value::String(memory_status)); frontmatter.insert("createdAt".into(), Value::String(now_rfc3339())); if !about.is_empty() { frontmatter.insert( @@ -1409,7 +1492,7 @@ impl vault_service_server::VaultService for VaultImpl { kind: Kind::Memory as i32, id: id.clone(), title: id.clone(), - status: Some(VaultEntityStatus::Active as i32), + status: Some(status as i32), body: Some(body), created_at: None, updated_at: None, diff --git a/libatomic/src/daemon/services_maintenance/compact.rs b/libatomic/src/daemon/services_maintenance/compact.rs index 0b8e35ec..0fac2057 100644 --- a/libatomic/src/daemon/services_maintenance/compact.rs +++ b/libatomic/src/daemon/services_maintenance/compact.rs @@ -76,6 +76,10 @@ async fn compact_inner( .ok_or_else(|| Status::failed_precondition("canonical .atomic has no parent"))? .to_path_buf(), ); + let repository_root = dot_dir + .parent() + .expect("validated canonical parent") + .to_path_buf(); let guard = tokio::time::timeout( wait.saturating_sub(started.elapsed()), handle.exclusive_owned(), @@ -88,7 +92,7 @@ async fn compact_inner( let report = tokio::task::spawn_blocking(move || { // The blocking task, not the RPC future, owns this until redb closes. let _guard = guard; - compact_waiting(&path, started, wait) + compact_waiting(&repository_root, &path, started, wait) }) .await .map_err(|error| Status::internal(format!("compaction task failed: {error}")))??; @@ -108,18 +112,24 @@ async fn compact_inner( } fn compact_waiting( + repository_root: &Path, path: &Path, started: Instant, wait: Duration, ) -> Result { loop { - match compact_file(path) { + match compact_repository(repository_root, path) { Ok(report) => return Ok(report), Err(error) if matches!( error.downcast_ref::(), Some(redb::DatabaseError::DatabaseAlreadyOpen) - ) => + ) || error + .downcast_ref::() + .is_some_and(|error| { + matches!(error, atomic_repository::RepositoryError::DatabaseBusy) + || error.is_lock_contended() + }) => { let remaining = wait.saturating_sub(started.elapsed()); if remaining.is_zero() { @@ -140,9 +150,28 @@ fn compact_waiting( } } +fn compact_repository(root: &Path, path: &Path) -> anyhow::Result { + // Inspect before creating a lock file or opening redb writable. Then hold + // the common operation lock and repeat the complete operation/capability + // gate so no cooperating writer can change it during compaction. + drop(Repository::open_readonly_without_migration(root)?); + let _guard = Repository::try_lock_database_maintenance(root)?; + drop(Repository::open_readonly_without_migration(root)?); + compact_file(path) +} + fn compact_file(path: &Path) -> anyhow::Result { + let readonly = redb::Builder::new().open_read_only(path)?; + let txn = readonly.begin_read()?; + atomic_core::pristine::schema::check_schema_version(&txn)?; + atomic_core::pristine::schema::check_required_capabilities(&txn)?; + drop(txn); + drop(readonly); let mut database = redb::Builder::new().open(path)?; - atomic_core::pristine::schema::check_schema_version(&database.begin_read()?)?; + let txn = database.begin_read()?; + atomic_core::pristine::schema::check_schema_version(&txn)?; + atomic_core::pristine::schema::check_required_capabilities(&txn)?; + drop(txn); let before_bytes = std::fs::metadata(path)?.len(); database.compact()?; // Closing persists allocator metadata; include that in the final size. diff --git a/libatomic/src/daemon/services_maintenance/compact/tests.rs b/libatomic/src/daemon/services_maintenance/compact/tests.rs index 3daaea34..dd7ae597 100644 --- a/libatomic/src/daemon/services_maintenance/compact/tests.rs +++ b/libatomic/src/daemon/services_maintenance/compact/tests.rs @@ -46,6 +46,50 @@ async fn busy_reader_and_writer_time_out_then_retry_succeeds() { assert!(Repository::open_existing(dir.path()).is_ok()); } +#[tokio::test] +async fn unsupported_capability_refuses_before_writable_database_open() { + use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT}; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability_exact("unknown-maintenance-test", 1) + .unwrap(); + txn.commit().unwrap(); + } + drop(repo); + let path = dir.path().join(".atomic/atomic.redb"); + let before = std::fs::read(&path).unwrap(); + let state = Arc::new(DaemonState::new()); + let handle = state.register(dir.path().to_path_buf()); + let error = compact(&state, request(&handle), Duration::from_secs(1)) + .await + .unwrap_err(); + assert_eq!(error.code(), tonic::Code::FailedPrecondition); + assert!( + error.message().contains("unknown-maintenance-test"), + "{error}" + ); + assert_eq!(std::fs::read(path).unwrap(), before); +} + +#[tokio::test] +async fn common_operation_lock_excludes_physical_compaction() { + let dir = tempfile::tempdir().unwrap(); + drop(Repository::init(dir.path()).unwrap()); + let guard = Repository::try_lock_database_maintenance(dir.path()).unwrap(); + let state = Arc::new(DaemonState::new()); + let handle = state.register(dir.path().to_path_buf()); + let error = compact(&state, request(&handle), Duration::from_millis(30)) + .await + .unwrap_err(); + assert_eq!(error.code(), tonic::Code::Unavailable); + drop(guard); + compact(&state, request(&handle), Duration::from_secs(1)) + .await + .unwrap(); +} + #[tokio::test] async fn refuses_invalid_requests_before_opening_a_database() { let dir = tempfile::tempdir().unwrap(); diff --git a/libatomic/tests/local_service_test.rs b/libatomic/tests/local_service_test.rs index 186ce734..ce95ac65 100644 --- a/libatomic/tests/local_service_test.rs +++ b/libatomic/tests/local_service_test.rs @@ -242,10 +242,19 @@ async fn local_add_record_log_and_change_use_existing_handlers() { #[tokio::test] async fn local_record_reports_native_span_semantic_statistics() { + assert_local_record_reports_semantic_statistics(true).await; +} + +#[tokio::test] +async fn local_record_reports_native_graph_semantic_statistics() { + assert_local_record_reports_semantic_statistics(false).await; +} + +async fn assert_local_record_reports_semantic_statistics(span: bool) { use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; let dir = tempfile::tempdir().unwrap(); let repo = Repository::init(dir.path()).unwrap(); - { + if span { let mut txn = repo.pristine().write_txn().unwrap(); txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); txn.commit().unwrap(); diff --git a/libatomic/tests/record_service_test.rs b/libatomic/tests/record_service_test.rs index 9ce69189..ba212884 100644 --- a/libatomic/tests/record_service_test.rs +++ b/libatomic/tests/record_service_test.rs @@ -68,9 +68,99 @@ fn record_request(repository: &RepositoryRef) -> RecordRequest { author: None, identity_name: None, ai_authorship: None, + skip_binary: None, + max_file_size: None, } } +#[tokio::test] +async fn record_file_options_and_oversize_details_preserve_working_bytes() { + let (_dir, root, state, repository) = host(); + let mutation = MutationImpl { + state: state.clone(), + }; + let binary_path = root.join("large-é.bin"); + std::fs::write(&binary_path, vec![0u8; 32]).unwrap(); + { + let repo = Repository::open(&root).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.add(wc, "large-é.bin", TrackingOptions::default()) + .unwrap(); + } + write(&root, "README.md", "new\n"); + let before_state = { + let handle = state.register(root.clone()); + let repo = handle.repository_readonly().unwrap(); + repo.get_view_info("dev").unwrap().state + }; + let mut request = record_request(&repository); + request.max_file_size = Some(8); + let error = mutation + .record(Request::new(request.clone())) + .await + .unwrap_err(); + assert_eq!(error.code(), Code::FailedPrecondition); + assert_eq!( + error + .metadata() + .get("atomic-record-error") + .unwrap() + .to_str() + .unwrap(), + "file-too-large" + ); + assert_eq!( + error + .metadata() + .get_bin("atomic-file-path-bin") + .unwrap() + .to_bytes() + .unwrap() + .as_ref(), + "large-é.bin".as_bytes() + ); + assert_eq!( + error + .metadata() + .get("atomic-file-size") + .unwrap() + .to_str() + .unwrap(), + "32" + ); + assert_eq!( + error + .metadata() + .get("atomic-file-limit") + .unwrap() + .to_str() + .unwrap(), + "8" + ); + assert_eq!(std::fs::read(&binary_path).unwrap(), vec![0u8; 32]); + assert_eq!(std::fs::read(root.join("README.md")).unwrap(), b"new\n"); + { + let handle = state.register(root.clone()); + let repo = handle.repository_readonly().unwrap(); + assert_eq!(repo.get_view_info("dev").unwrap().state, before_state); + } + request.skip_binary = Some(true); + let response = mutation + .record(Request::new(request)) + .await + .unwrap() + .into_inner(); + assert!(response + .recorded_paths + .iter() + .any(|path| path == "README.md")); + assert!(response + .skipped_paths + .iter() + .any(|path| path == "large-é.bin")); + assert_eq!(std::fs::read(&binary_path).unwrap(), vec![0u8; 32]); +} + #[tokio::test] async fn an_ai_assisted_record_carries_its_provenance() { let (_dir, root, state, repository) = host(); diff --git a/libatomic/tests/vault_local_test.rs b/libatomic/tests/vault_local_test.rs index 19f0fee9..dae7c5f8 100644 --- a/libatomic/tests/vault_local_test.rs +++ b/libatomic/tests/vault_local_test.rs @@ -22,6 +22,136 @@ async fn resolve(service: &LocalService, root: &std::path::Path) -> proto::Repos .unwrap() } +#[tokio::test] +async fn memory_create_honors_explicit_identity_and_status_without_overwriting() { + use libatomic::atomic::daemon_service_server::DaemonService; + use libatomic::atomic::vault_service_server::VaultService; + use libatomic::daemon::{services::DaemonImpl, services_agent::VaultImpl, state::DaemonState}; + use std::sync::Arc; + use tonic::Request; + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + repo.init_vault().unwrap(); + drop(repo); + let state = Arc::new(DaemonState::new()); + let daemon = DaemonImpl { + state: state.clone(), + }; + let capabilities = daemon + .get_capabilities(Request::new(proto::GetCapabilitiesRequest {})) + .await + .unwrap() + .into_inner(); + assert!( + capabilities + .features + .iter() + .any(|feature| feature + == libatomic::daemon::services::VAULT_MEMORY_CREATE_OPTIONS_FEATURE) + ); + let reference = daemon + .resolve_repository(Request::new(proto::ResolveRepositoryRequest { + path: dir.path().display().to_string(), + })) + .await + .unwrap() + .into_inner() + .repository + .unwrap(); + let service = VaultImpl { state }; + let request = proto::CreateVaultEntityRequest { + repository: Some(reference.clone()), + kind: proto::VaultEntityKind::Memory as i32, + memory_kind: Some("lesson".into()), + memory_id: Some("inherited-note".into()), + memory_status: Some("superseded".into()), + body: Some("preserve this memory".into()), + ..Default::default() + }; + let entry = service + .create_vault_entity(Request::new(request.clone())) + .await + .unwrap() + .into_inner() + .entry + .unwrap(); + assert_eq!(entry.id, "inherited-note"); + assert_eq!( + entry.status, + Some(proto::VaultEntityStatus::Superseded as i32) + ); + assert_eq!( + entry.vault_path.as_deref(), + Some(".vault/memory/inherited-note.md") + ); + let path = dir.path().join(".vault/memory/inherited-note.md"); + let before = std::fs::read(&path).unwrap(); + let duplicate = service + .create_vault_entity(Request::new(request.clone())) + .await + .unwrap_err(); + assert_eq!(duplicate.code(), tonic::Code::AlreadyExists); + assert_eq!(std::fs::read(&path).unwrap(), before); + + let invalid = proto::CreateVaultEntityRequest { + repository: Some(reference.clone()), + memory_id: Some("../escape".into()), + ..request.clone() + }; + assert_eq!( + service + .create_vault_entity(Request::new(invalid)) + .await + .unwrap_err() + .code(), + tonic::Code::InvalidArgument + ); + assert!(!dir.path().join(".vault/escape.md").exists()); + let untracked_path = dir.path().join(".vault/memory/untracked.md"); + std::fs::write(&untracked_path, b"untracked user bytes").unwrap(); + let untracked = proto::CreateVaultEntityRequest { + repository: Some(reference.clone()), + memory_id: Some("untracked".into()), + ..request.clone() + }; + assert_eq!( + service + .create_vault_entity(Request::new(untracked)) + .await + .unwrap_err() + .code(), + tonic::Code::AlreadyExists + ); + assert_eq!( + std::fs::read(&untracked_path).unwrap(), + b"untracked user bytes" + ); + let invalid_status = proto::CreateVaultEntityRequest { + repository: Some(reference), + memory_id: Some("bad-status".into()), + memory_status: Some("not-a-status".into()), + ..request.clone() + }; + assert_eq!( + service + .create_vault_entity(Request::new(invalid_status)) + .await + .unwrap_err() + .code(), + tonic::Code::InvalidArgument + ); + assert!(!dir.path().join(".vault/memory/bad-status.md").exists()); + let repo = Repository::open(dir.path()).unwrap(); + let stored = repo + .vault_retrieve("memory/inherited-note.md") + .unwrap() + .unwrap(); + let frontmatter: serde_json::Value = serde_json::from_str(&stored.frontmatter_json).unwrap(); + assert_eq!(frontmatter["uid"], "inherited-note"); + assert_eq!(frontmatter["status"], "superseded"); + assert_eq!(stored.content_bytes, b"preserve this memory\n"); +} + fn create() -> (tempfile::TempDir, String, String) { let dir = tempfile::tempdir().unwrap(); let repo = Repository::init(dir.path()).unwrap(); diff --git a/tests/harness/10_git_import.sh b/tests/harness/10_git_import.sh index 3da98169..aed1e7b1 100755 --- a/tests/harness/10_git_import.sh +++ b/tests/harness/10_git_import.sh @@ -13,25 +13,35 @@ HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" count_imported_git_changes() { - local count=0 - local hash + local count=0 hash log_output details + if ! log_output="$(atomic log --format short --no-color --full-hash 2>&1)"; then + echo "atomic log failed: $log_output" >&2 + return 1 + fi while IFS= read -r hash; do [[ -z "$hash" ]] && continue - if atomic change "$hash" 2>/dev/null | grep -q 'Commit:'; then + if ! details="$(atomic change "$hash" --full-hash 2>&1)"; then + echo "atomic change $hash failed: $details" >&2 + return 1 + fi + if printf '%s\n' "$details" | grep -qF 'Git source revision:'; then count=$((count + 1)) fi - done < <(atomic log --format short --no-color --full-hash 2>/dev/null | awk '/^[A-Z2-7]{20,}/ { print $1 }') + done <<< "$(printf '%s\n' "$log_output" | awk '$1 ~ /^[A-Z2-7]+$/ && length($1) == 52 { print $1 }')" echo "$count" } git_first_parent_commit_count() { - git rev-list --first-parent --count HEAD 2>/dev/null || echo "0" + git rev-list --first-parent --count HEAD } assert_imported_git_change_count() { local desc="$1" local expected="$2" local actual - actual="$(count_imported_git_changes)" + if ! actual="$(count_imported_git_changes 2>&1)"; then + _fail "$desc" "failed to count imported changes: $actual" + return + fi if [[ "$actual" -eq "$expected" ]]; then _pass "$desc" else @@ -41,14 +51,23 @@ assert_imported_git_change_count() { find_imported_change_hash_containing() { local needle="$1" - local hash + local hash log_output details + if ! log_output="$(atomic log --format short --no-color --full-hash 2>&1)"; then + echo "atomic log failed: $log_output" >&2 + return 1 + fi while IFS= read -r hash; do [[ -z "$hash" ]] && continue - if atomic change "$hash" 2>/dev/null | grep -qiF "$needle"; then + if ! details="$(atomic change "$hash" --full-hash 2>&1)"; then + echo "atomic change $hash failed: $details" >&2 + return 1 + fi + if printf '%s\n' "$details" | grep -qiF "$needle"; then echo "$hash" return 0 fi - done < <(atomic log --format short --no-color --full-hash 2>/dev/null | awk '/^[A-Z2-7]{20,}/ { print $1 }') + done <<< "$(printf '%s\n' "$log_output" | awk '$1 ~ /^[A-Z2-7]+$/ && length($1) == 52 { print $1 }')" + echo "no imported change contains '$needle'" >&2 return 1 } @@ -86,7 +105,7 @@ default_branch="$(git_default_branch)" echo " Found $expected_commits first-parent commits on branch '$default_branch'" # Initialize atomic and import -assert_success "atomic git import succeeds" atomic git import +require_success "atomic git import succeeds" atomic git import git_status_after_import="$(git status --short)" if [[ -z "$git_status_after_import" ]]; then @@ -101,7 +120,8 @@ assert_view_exists "view '$default_branch' created" "$default_branch" # Verify imported change count is effectively complete. # Some tiny repos contain a non-material merge/empty commit that import # intentionally skips, so allow a one-commit gap here. -actual_imported_commits="$(count_imported_git_changes)" +require_capture "count imported tiny-repository changes" count_imported_git_changes +actual_imported_commits="$COMMAND_OUTPUT" if [[ "$actual_imported_commits" -eq "$expected_commits" ]] || \ [[ "$actual_imported_commits" -eq $((expected_commits - 1)) ]]; then _pass "change count matches git first-parent commits ($expected_commits)" @@ -120,11 +140,11 @@ make_temp_repo "git-import-dry-run" clone_git_repo "https://github.com/hashicorp/go-uuid.git" cd "$GIT_REPO_DIR" -# Remove any existing .atomic from clone (shouldn't exist, but be safe) -rm -rf .atomic 2>/dev/null || true +assert_dir_not_exists "fresh clone has no Atomic repository" ".atomic" # Run dry-run -out="$(atomic git import --dry-run 2>&1)" || true +require_capture "git import dry-run succeeds" atomic git import --dry-run +out="$COMMAND_OUTPUT" # Verify no .atomic created assert_dir_not_exists ".atomic not created in dry-run" ".atomic" @@ -133,7 +153,7 @@ assert_dir_not_exists ".atomic not created in dry-run" ".atomic" if echo "$out" | grep -qiE "would import|preview|commits|dry.run"; then _pass "dry-run output shows preview" else - _pass "dry-run completes without creating repo" + _fail "dry-run output shows preview" "got: $out" fi # ════════════════════════════════════════════════════════════════════════════ @@ -159,15 +179,15 @@ git checkout -b feature --quiet git_commit "Feature commit" "feature.txt" "feature content" # Switch back to the original (main) branch -git checkout "$main_branch" --quiet 2>/dev/null || true +require_success "return to main Git branch" git checkout "$main_branch" --quiet # Import main branch -atomic init >/dev/null 2>&1 -assert_success "import main branch" atomic git import --branch "$main_branch" +require_success "initialize Atomic repository" atomic init +require_success "import main branch" atomic git import --branch "$main_branch" assert_imported_git_change_count "main has 2 commits" 2 # Import feature branch -assert_success "import feature branch" atomic git import --branch feature +require_success "import feature branch" atomic git import --branch feature assert_view_exists "feature view created" "feature" # ════════════════════════════════════════════════════════════════════════════ @@ -185,16 +205,17 @@ cd "$GIT_REPO_DIR" expected_commits="$(git_first_parent_commit_count)" echo " Found $expected_commits first-parent commits" -assert_success "atomic git import succeeds" atomic git import +require_success "atomic git import succeeds" atomic git import -# Check that imported git changes preserve author/message metadata. -medium_change_hash="$(find_imported_change_hash_containing "spark" || true)" -if [[ -n "$medium_change_hash" ]]; then +# Locate an actual first-parent commit by its Git source revision, rather than +# matching the repository name in every imported change's metadata. +medium_git_sha="$(git log --first-parent --author=holman --regexp-ignore-case --grep=spark --format=%H -n 1)" +if [[ -n "$medium_git_sha" ]] && medium_change_hash="$(find_imported_change_hash_containing "$medium_git_sha" 2>&1)"; then assert_change_author "change has author preserved" "$medium_change_hash" "holman" assert_change_message "commit message preserved" "$medium_change_hash" "spark" else - _fail "change has author preserved" "could not find imported git change mentioning 'spark'" - _fail "commit message preserved" "could not find imported git change mentioning 'spark'" + _fail "change has author preserved" "no matching Git commit or imported change: ${medium_change_hash:-$medium_git_sha}" + _fail "commit message preserved" "no matching Git commit or imported change: ${medium_change_hash:-$medium_git_sha}" fi # ════════════════════════════════════════════════════════════════════════════ @@ -213,8 +234,8 @@ git_commit "Add file B" "fileB.txt" "content B" rm fileA.txt && git add fileA.txt && git commit --quiet -m "Delete file A" git mv fileB.txt fileC.txt && git commit --quiet -m "Rename B to C" -atomic init >/dev/null 2>&1 -atomic git import >/dev/null 2>&1 +require_success "initialize Atomic repository" atomic init +require_success "import file operations" atomic git import assert_imported_git_change_count "5 changes imported" 5 @@ -235,8 +256,8 @@ init_git_repo git_commit "Commit 1" "file1.txt" "v1" git_commit "Commit 2" "file2.txt" "v2" -atomic init >/dev/null 2>&1 -atomic git import >/dev/null 2>&1 +require_success "initialize Atomic repository" atomic init +require_success "import initial Git history" atomic git import assert_imported_git_change_count "initial import: 2 changes" 2 # Add more git commits @@ -244,7 +265,7 @@ git_commit "Commit 3" "file3.txt" "v3" git_commit "Commit 4" "file4.txt" "v4" # Incremental import (should only add new commits) -atomic git import --incremental >/dev/null 2>&1 +require_success "import incremental Git history" atomic git import --incremental assert_imported_git_change_count "after incremental: 4 changes" 4 # ════════════════════════════════════════════════════════════════════════════ @@ -263,11 +284,10 @@ expected_commits="$(git_first_parent_commit_count)" echo " Found $expected_commits first-parent commits" start_time=$(date +%s) -atomic git import >/dev/null 2>&1 +require_success "import hyperfine Git history" atomic git import end_time=$(date +%s) duration=$((end_time - start_time)) -assert_success "import completed" true echo " Import took ${duration}s" # Should complete in reasonable time (< 5 minutes) @@ -280,7 +300,8 @@ fi # Verify imported Git mainline commit count exactly. Do not use raw `atomic log` # length here: git import records follow-up Atomic-only changes such as # repository/vault initialization, and those are not Git commits. -actual="$(count_imported_git_changes)" +require_capture "count imported hyperfine changes" count_imported_git_changes +actual="$COMMAND_OUTPUT" if [[ "$actual" -eq "$expected_commits" ]]; then _pass "imported git change count matches ($actual vs $expected_commits)" else @@ -296,7 +317,7 @@ begin_section "Git Import: Error Handling" make_temp_repo "git-import-errors" # Initialize atomic only (not a git repo) -atomic init >/dev/null 2>&1 +require_success "initialize non-Git Atomic repository" atomic init # Should fail with clear error about not being a git repo status=0 @@ -335,15 +356,18 @@ git_commit "Test commit" "test.txt" "test content" sha="$(git_head_sha)" sha_full="$(git_head_sha_full)" -atomic init >/dev/null 2>&1 -atomic git import >/dev/null 2>&1 +require_success "initialize Atomic repository" atomic init +require_success "import Git metadata" atomic git import # Check change details for git metadata (SHA should be preserved) -out="$(atomic change @ 2>/dev/null || true)" -if echo "$out" | grep -qiE "git|sha|commit|$sha"; then +require_capture "read imported Git metadata" find_imported_change_hash_containing "$sha_full" +metadata_hash="$COMMAND_OUTPUT" +require_capture "show imported change metadata" atomic change "$metadata_hash" --full-hash +out="$COMMAND_OUTPUT" +if echo "$out" | grep -qiF "$sha_full"; then _pass "git metadata present in change" else - _pass "change imported (metadata format may vary)" + _fail "git metadata present in change" "expected commit $sha_full: $out" fi # ════════════════════════════════════════════════════════════════════════════ @@ -376,8 +400,8 @@ git_commit "Feature B" "b.txt" "b" # Return to initial branch git checkout "$initial_branch" --quiet -atomic init >/dev/null 2>&1 -atomic git import --all >/dev/null 2>&1 +require_success "initialize Atomic repository" atomic init +require_success "import all Git branches" atomic git import --all # Verify all branches became views assert_view_exists "${initial_branch} view exists" "$initial_branch" @@ -403,12 +427,13 @@ git checkout "$_default_branch" --quiet 2>/dev/null git_commit "Main commit" "main2.txt" "main2" git merge feature --no-ff -m "Merge feature into main" --quiet 2>/dev/null -atomic init >/dev/null 2>&1 -atomic git import >/dev/null 2>&1 +require_success "initialize Atomic repository" atomic init +require_success "import merge history" atomic git import # Should have at least 3 changes (base, feature, main2) # Merge commit handling varies: might be 3 (linearized) or 4 (preserved) -count="$(atomic log 2>/dev/null | grep -cE '^\s*#[0-9]+|^[0-9a-f]{8,}' 2>/dev/null || true)" +require_capture "read imported merge log" atomic log +count="$(printf '%s\n' "$COMMAND_OUTPUT" | grep -cE '^\s*#[0-9]+|^[0-9a-f]{8,}' || true)" count="${count:-0}" count="$(echo "$count" | tr -d '[:space:]')" [[ -z "$count" ]] && count=0 @@ -430,10 +455,11 @@ init_git_repo git_commit "Initial" "main.txt" "main" # Add a small submodule (go-uuid is tiny and fast to clone) -git_add_submodule "https://github.com/hashicorp/go-uuid.git" "vendor/uuid" +require_success "add Git submodule" git_add_submodule "https://github.com/hashicorp/go-uuid.git" "vendor/uuid" -atomic init >/dev/null 2>&1 -out="$(atomic git import 2>&1)" || true +require_success "initialize Atomic repository" atomic init +require_capture "import Git history with submodule" atomic git import +out="$COMMAND_OUTPUT" # Submodules should either: # - Be skipped with a warning @@ -442,7 +468,7 @@ out="$(atomic git import 2>&1)" || true if echo "$out" | grep -qiE "submodule|skip|warn"; then _pass "submodule handled (skipped with warning)" else - _pass "import with submodule completes" + assert_file_exists "submodule checkout remains present" "vendor/uuid/uuid.go" fi # ════════════════════════════════════════════════════════════════════════════ @@ -505,28 +531,31 @@ git -C "$GIT_REPO" add -A >/dev/null 2>&1 git -C "$GIT_REPO" commit -m "v3: modify after rename" --quiet 2>/dev/null # ── Import ────────────────────────────────────────────────────────────── -(cd "$GIT_REPO" && atomic git import 2>/dev/null) || true +require_success "import rename and deletion history" atomic git import # ── Assertions ────────────────────────────────────────────────────────── -# 1. No spurious deleted files in status -_deleted_count=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep -c '^D ' || true) +# 1. No spurious deleted files in status. Capture once so a command failure +# cannot be mistaken for an empty/clean result by any of these checks. +require_capture "read status after import" atomic status --short +_status="$COMMAND_OUTPUT" +_deleted_count=$(printf '%s\n' "$_status" | grep -c '^D ' || true) if [[ "$_deleted_count" -eq 0 ]]; then _pass "no spurious deletions in status after import" else - _deleted_files=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep '^D ') + _deleted_files=$(printf '%s\n' "$_status" | grep '^D ') _fail "spurious deletions in status" "$_deleted_count file(s): $_deleted_files" fi # 2. Renamed files are tracked (not untracked ??) -_untracked_app=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep '?? src/app.rs' || true) +_untracked_app=$(printf '%s\n' "$_status" | grep -F '?? src/app.rs' || true) if [[ -z "$_untracked_app" ]]; then _pass "src/app.rs tracked after rename" else _fail "src/app.rs untracked after rename" "status shows: $_untracked_app" fi -_untracked_bench=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep '?? src/bench.rs' || true) +_untracked_bench=$(printf '%s\n' "$_status" | grep -F '?? src/bench.rs' || true) if [[ -z "$_untracked_bench" ]]; then _pass "src/bench.rs tracked after rename+modify" else @@ -534,14 +563,14 @@ else fi # 3. Deleted files are NOT tracked (should not appear in status at all) -_deleted_travis=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep 'travis' || true) +_deleted_travis=$(printf '%s\n' "$_status" | grep -F 'travis' || true) if [[ -z "$_deleted_travis" ]]; then _pass ".travis.yml not in status (correctly deleted)" else _fail ".travis.yml still in status after delete" "status shows: $_deleted_travis" fi -_deleted_mod=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep 'sub/mod.rs' || true) +_deleted_mod=$(printf '%s\n' "$_status" | grep -F 'sub/mod.rs' || true) if [[ -z "$_deleted_mod" ]]; then _pass "src/sub/mod.rs not in status (correctly deleted)" else @@ -549,7 +578,7 @@ else fi # 4. Old rename source paths are gone from tracking -_old_sub_app=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep 'sub/app.rs' || true) +_old_sub_app=$(printf '%s\n' "$_status" | grep -F 'sub/app.rs' || true) if [[ -z "$_old_sub_app" ]]; then _pass "src/sub/app.rs gone from status (old rename source)" else @@ -557,7 +586,7 @@ else fi # 5. Binary file is tracked and NOT modified (graph content matches disk) -_modified_logo=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep 'docs/logo.png' || true) +_modified_logo=$(printf '%s\n' "$_status" | grep -F 'docs/logo.png' || true) if [[ -z "$_modified_logo" ]]; then _pass "docs/logo.png clean after binary modify (graph matches disk)" else @@ -565,11 +594,11 @@ else fi # 6. Overall: status should be completely clean (no M, D, A, or ?? entries) -_status_lines=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | grep -cE '^[MDAU?]' || true) +_status_lines=$(printf '%s\n' "$_status" | grep -cE '^[MDAU?]' || true) if [[ "$_status_lines" -eq 0 ]]; then _pass "status is completely clean after import (full parity)" else - _dirty=$(cd "$GIT_REPO" && atomic status --short 2>/dev/null | head -10) + _dirty=$(printf '%s\n' "$_status" | head -10) _fail "status not clean after import" "$_status_lines dirty entries: $_dirty" fi @@ -582,7 +611,8 @@ fi # 7. Change count matches git commit count _git_count=$(git -C "$GIT_REPO" rev-list --count HEAD 2>/dev/null) -_atomic_log=$(cd "$GIT_REPO" && count_imported_git_changes) +require_capture "count imported parity changes" count_imported_git_changes +_atomic_log="$COMMAND_OUTPUT" if [[ "$_atomic_log" -eq "$_git_count" ]]; then _pass "change count matches git ($_git_count)" else diff --git a/tests/harness/11_diff_git_parity.sh b/tests/harness/11_diff_git_parity.sh index e3693c62..cb230ffd 100755 --- a/tests/harness/11_diff_git_parity.sh +++ b/tests/harness/11_diff_git_parity.sh @@ -25,12 +25,16 @@ ATOMIC_HASHES=() GIT_SHAS=() GIT_REPO="" +atomic_in_git_repo() { + (cd "$GIT_REPO" && atomic "$@") +} + # ── Helpers ───────────────────────────────────────────────────────────────── # Extract only +/- change lines from git unified diff output. # Strips headers (diff, ---, +++, @@) and context lines. extract_git_change_lines() { - grep -E '^\+[^+]|^-[^-]' || true + awk '/^diff --git / { hunk = 0; next } /^@@ / { hunk = 1; next } hunk && /^[+-]/ { print }' } # Extract only +/- change lines from atomic diff output. @@ -40,9 +44,21 @@ extract_git_change_lines() { # Same as git, so we use the same extractor. # But we also need to skip the change header block (change hash, Date, message). extract_atomic_change_lines() { - grep -E '^\+[^+]|^-[^-]' || true + awk '/^diff --atomic / { hunk = 0; next } /^@@ / { hunk = 1; next } hunk && /^[+-]/ { print }' } +begin_section "Parity: Diff-line extraction" +expected_lines=$'+line\n+--- a/content\n+' +git_fixture=$'diff --git a/file b/file\n--- /dev/null\n+++ b/file\n@@ -0,0 +1,3 @@\n+line\n+--- a/content\n+' +atomic_fixture=$'diff --atomic /dev/null b/file\n--- /dev/null\n+++ b/file\n@@ -0,0 +1,3 @@\n+line\n+--- a/content\n+' +git_extracted="$(printf '%s\n' "$git_fixture" | extract_git_change_lines)" +atomic_extracted="$(printf '%s\n' "$atomic_fixture" | extract_atomic_change_lines)" +if [[ "$git_extracted" == "$expected_lines" && "$atomic_extracted" == "$expected_lines" ]]; then + _pass "diff extraction excludes headers and keeps blank and header-like content" +else + _fail "diff-line extraction" "Git: $git_extracted; Atomic: $atomic_extracted" +fi + # Create a git repo, apply a sequence of file versions, import into atomic. # # Usage: @@ -83,13 +99,14 @@ setup_git_and_import() { done # Run atomic git import - (cd "$GIT_REPO" && atomic git import 2>/dev/null) || true + require_success "import Git fixture $label" atomic_in_git_repo git import # Collect atomic change hashes in order (oldest first). # `atomic log` lists newest first, so we reverse. ATOMIC_HASHES=() local log_output - log_output=$( (cd "$GIT_REPO" && atomic log --format short --no-color --full-hash 2>/dev/null) || true ) + require_capture "read imported log for $label" atomic_in_git_repo log --format short --no-color --full-hash + log_output="$COMMAND_OUTPUT" if [[ -n "${log_output:-}" ]]; then # Log lists newest first. Collect into temp, then reverse. local _tmp=() @@ -145,13 +162,14 @@ assert_commit_parity() { # Git diff (optionally scoped to a single path) local git_lines_file="$REPO_DIR/git_lines_$idx.txt" + local git_raw if [[ -n "$path_filter" ]]; then - git -C "$GIT_REPO" --no-pager diff "$prev_sha" "$curr_sha" -- "$path_filter" 2>/dev/null \ - | extract_git_change_lines > "$git_lines_file" + require_capture "$desc: read Git diff" git -C "$GIT_REPO" --no-pager diff "$prev_sha" "$curr_sha" -- "$path_filter" else - git -C "$GIT_REPO" --no-pager diff "$prev_sha" "$curr_sha" 2>/dev/null \ - | extract_git_change_lines > "$git_lines_file" + require_capture "$desc: read Git diff" git -C "$GIT_REPO" --no-pager diff "$prev_sha" "$curr_sha" fi + git_raw="$COMMAND_OUTPUT" + printf '%s\n' "$git_raw" | extract_git_change_lines > "$git_lines_file" # Atomic diff set +u @@ -164,12 +182,19 @@ assert_commit_parity() { local atomic_lines_file="$REPO_DIR/atomic_lines_$idx.txt" if [[ -z "$atomic_hash" ]]; then - touch "$atomic_lines_file" - else - local raw - raw=$( (cd "$GIT_REPO" && atomic diff -c "$atomic_hash" --no-color 2>/dev/null) || true ) - if [[ -n "$raw" ]]; then - if [[ -n "$path_filter" ]]; then + _fail "$desc: imported change" "missing Atomic change at index $idx" + return + fi + local raw detail + require_capture "$desc: read imported change" atomic_in_git_repo change "$atomic_hash" --full-hash + detail="$COMMAND_OUTPUT" + if [[ "$detail" != *"Git source revision: $curr_sha"* ]]; then + _fail "$desc: imported change identity" "expected Git revision $curr_sha in $atomic_hash: $detail" + return + fi + require_capture "$desc: read Atomic diff" atomic_in_git_repo diff -c "$atomic_hash" --no-color + raw="$COMMAND_OUTPUT" + if [[ -n "$path_filter" ]]; then # Extract only the +/- lines from the section of the atomic diff # that belongs to path_filter. awk handles both cases: when # path_filter is the last section (no following "diff --atomic") @@ -177,13 +202,9 @@ assert_commit_parity() { printf '%s\n' "$raw" | awk -v pat="$path_filter" ' /^diff --atomic/ { in_sec = ($0 ~ pat) ? 1 : 0; next } in_sec { print } - ' | extract_atomic_change_lines > "$atomic_lines_file" || true - else - printf '%s\n' "$raw" | extract_atomic_change_lines > "$atomic_lines_file" - fi - else - touch "$atomic_lines_file" - fi + ' | extract_atomic_change_lines > "$atomic_lines_file" + else + printf '%s\n' "$raw" | extract_atomic_change_lines > "$atomic_lines_file" fi local git_count atomic_count @@ -348,10 +369,11 @@ SHA2=$(git -C "$GIT_REPO" rev-parse HEAD 2>/dev/null) GIT_SHAS=("$SHA1" "$SHA2") -(cd "$GIT_REPO" && atomic git import 2>/dev/null) || true +require_success "import deleted-file fixture" atomic_in_git_repo git import ATOMIC_HASHES=() -log_output=$( (cd "$GIT_REPO" && atomic log --format short --no-color --full-hash 2>/dev/null) || true ) +require_capture "read deleted-file fixture log" atomic_in_git_repo log --format short --no-color --full-hash +log_output="$COMMAND_OUTPUT" if [[ -n "${log_output:-}" ]]; then _tmp=() while IFS= read -r line; do @@ -392,10 +414,11 @@ SHA2=$(git -C "$GIT_REPO" rev-parse HEAD 2>/dev/null) GIT_SHAS=("$SHA1" "$SHA2") -(cd "$GIT_REPO" && atomic git import 2>/dev/null) || true +require_success "import multi-file fixture" atomic_in_git_repo git import ATOMIC_HASHES=() -log_output=$( (cd "$GIT_REPO" && atomic log --format short --no-color --full-hash 2>/dev/null) || true ) +require_capture "read multi-file fixture log" atomic_in_git_repo log --format short --no-color --full-hash +log_output="$COMMAND_OUTPUT" if [[ -n "${log_output:-}" ]]; then _tmp=() while IFS= read -r line; do @@ -443,13 +466,13 @@ begin_section "Parity: Hyperfine first 4 commits (real repo)" make_temp_repo "parity-hyperfine" -if ! git clone --quiet https://github.com/sharkdp/hyperfine.git "$REPO_DIR/hyperfine" 2>/dev/null; then - _skip "hyperfine clone failed (no network?)" +if ! clone_out="$(git clone --quiet https://github.com/sharkdp/hyperfine.git "$REPO_DIR/hyperfine" 2>&1)"; then + _fail "clone hyperfine" "$clone_out" else GIT_REPO="$REPO_DIR/hyperfine" # Import into atomic - (cd "$GIT_REPO" && atomic git import 2>/dev/null) || true + require_success "import hyperfine" atomic_in_git_repo git import # The first 4 commits that touch src/main.rs COMMIT_SHAS=("a658ab8c" "d4ebdd7b" "197f9fb" "68fdc2c") @@ -457,26 +480,29 @@ else # Resolve full SHAs GIT_SHAS=() for short in "${COMMIT_SHAS[@]}"; do - full=$(git -C "$GIT_REPO" rev-parse "$short" 2>/dev/null) || true + require_capture "resolve hyperfine Git revision $short" git -C "$GIT_REPO" rev-parse "$short" + full="$COMMAND_OUTPUT" GIT_SHAS+=("$full") done # Build a map from git short SHA → atomic hash by scanning every - # atomic change's metadata for the "Commit:" line. + # atomic change's metadata for the "Git source revision:" line. # Use parallel arrays for bash 3.2 compatibility (no associative arrays). MAP_GIT_SHAS=() MAP_ATOMIC_HASHES=() - all_hashes_output=$( (cd "$GIT_REPO" && atomic log --format short --no-color --full-hash 2>/dev/null) || true ) + require_capture "read hyperfine imported log" atomic_in_git_repo log --format short --no-color --full-hash --reverse + all_hashes_output="$COMMAND_OUTPUT" while IFS= read -r logline; do atomic_h=$(echo "$logline" | awk '{print $1}') [[ -z "$atomic_h" ]] && continue - change_detail=$( (cd "$GIT_REPO" && atomic change "$atomic_h" 2>/dev/null) || true ) - # Not every change has "Commit:" metadata — the vault-scaffold and + require_capture "read hyperfine change $atomic_h" atomic_in_git_repo change "$atomic_h" --full-hash + change_detail="$COMMAND_OUTPUT" + # Not every change has Git revision metadata — the vault-scaffold and # .atomicignore changes atomic git import creates alongside the # actual git-derived history don't. Under set -e/pipefail, grep # finding no match must not abort the whole script. - git_sha=$(echo "$change_detail" | grep "Commit:" | awk '{print $2}' || true) + git_sha=$(printf '%s\n' "$change_detail" | sed -n 's/^.*Git source revision: //p') if [[ -n "$git_sha" ]]; then # Store the full git SHA so we can match short SHAs of any length MAP_GIT_SHAS+=("$git_sha") @@ -518,8 +544,10 @@ else PREV_SHORT="${COMMIT_SHAS[$((i-1))]}" ATOMIC_H=$(_lookup_atomic_hash "$SHORT") - prev_full=$(git -C "$GIT_REPO" rev-parse "$PREV_SHORT" 2>/dev/null) || true - curr_full=$(git -C "$GIT_REPO" rev-parse "$SHORT" 2>/dev/null) || true + require_capture "resolve previous hyperfine revision $PREV_SHORT" git -C "$GIT_REPO" rev-parse "$PREV_SHORT" + prev_full="$COMMAND_OUTPUT" + require_capture "resolve hyperfine revision $SHORT" git -C "$GIT_REPO" rev-parse "$SHORT" + curr_full="$COMMAND_OUTPUT" if [[ -z "$ATOMIC_H" ]]; then _fail "hyperfine ($SHORT)" "could not find atomic hash for git SHA $SHORT" @@ -528,12 +556,13 @@ else # Git diff scoped to src/main.rs git_lf="$REPO_DIR/hf_git_$i.txt" - git -C "$GIT_REPO" --no-pager diff "$prev_full" "$curr_full" -- src/main.rs 2>/dev/null \ - | extract_git_change_lines > "$git_lf" + require_capture "hyperfine $SHORT: read Git diff" git -C "$GIT_REPO" --no-pager diff "$prev_full" "$curr_full" -- src/main.rs + printf '%s\n' "$COMMAND_OUTPUT" | extract_git_change_lines > "$git_lf" # Atomic diff scoped to src/main.rs atomic_lf="$REPO_DIR/hf_atomic_$i.txt" - hf_raw=$( (cd "$GIT_REPO" && atomic diff -c "$ATOMIC_H" --no-color 2>/dev/null) || true ) + require_capture "hyperfine $SHORT: read Atomic diff" atomic_in_git_repo diff -c "$ATOMIC_H" --no-color + hf_raw="$COMMAND_OUTPUT" if [[ -n "$hf_raw" ]]; then printf '%s\n' "$hf_raw" | awk -v pat="src/main.rs" ' /^diff --atomic/ { in_sec = ($0 ~ pat) ? 1 : 0; next } @@ -579,10 +608,9 @@ fi begin_section "Parity: Pure rename (no content change)" # ═══════════════════════════════════════════════════════════════════════════ # -# A pure rename produces ZERO +/- lines in both git diff and atomic diff. -# git diff --follow sees no content change; atomic treats the rename as a -# TrunkOp::Move which has no associated line operations. -# Both sides must produce an empty set of change lines. +# A pure Git-detected rename produces zero text change lines. The graph-only +# stream records delete+add, not an authoritative inode move; the diff must +# explicitly disclose that distinction while displaying Git's rename evidence. make_temp_repo "parity-rename-pure" GIT_REPO="$REPO_DIR" @@ -604,10 +632,11 @@ SHA2=$(git -C "$GIT_REPO" rev-parse HEAD 2>/dev/null) GIT_SHAS=("$SHA1" "$SHA2") -(cd "$GIT_REPO" && atomic git import 2>/dev/null) || true +require_success "import pure-rename fixture" atomic_in_git_repo git import ATOMIC_HASHES=() -_log=$( (cd "$GIT_REPO" && atomic log --format short --no-color --full-hash 2>/dev/null) || true ) +require_capture "read pure-rename fixture log" atomic_in_git_repo log --format short --no-color --full-hash +_log="$COMMAND_OUTPUT" if [[ -n "${_log:-}" ]]; then _tmp=() while IFS= read -r line; do @@ -627,22 +656,32 @@ if [[ ${#ATOMIC_HASHES[@]} -ge 2 ]]; then _rename_atomic_hash="${ATOMIC_HASHES[1]}" fi -_git_lines_rename=$(git -C "$GIT_REPO" --no-pager diff "$SHA1" "$SHA2" \ - 2>/dev/null | grep -E '^\+[^+]|^-[^-]' || true) +require_capture "read Git pure-rename diff" git -C "$GIT_REPO" --no-pager diff "$SHA1" "$SHA2" +_git_lines_rename=$(printf '%s\n' "$COMMAND_OUTPUT" | extract_git_change_lines) _atomic_lines_rename="" -if [[ -n "$_rename_atomic_hash" ]]; then - _atomic_raw=$( (cd "$GIT_REPO" && atomic diff -c "$_rename_atomic_hash" --no-color 2>/dev/null) || true ) - _atomic_lines_rename=$(printf '%s\n' "$_atomic_raw" | grep -E '^\+[^+]|^-[^-]' || true) +if [[ -z "$_rename_atomic_hash" ]]; then + _fail "pure rename: imported change" "missing change for $SHA2" + print_summary || true + exit 1 fi +require_capture "read pure-rename change" atomic_in_git_repo change "$_rename_atomic_hash" --full-hash +if [[ "$COMMAND_OUTPUT" != *"Git source revision: $SHA2"* ]]; then + _fail "pure rename: imported change identity" "expected Git revision $SHA2: $COMMAND_OUTPUT" + print_summary || true + exit 1 +fi +require_capture "read Atomic pure-rename diff" atomic_in_git_repo diff -c "$_rename_atomic_hash" --no-color +assert_output_contains "pure rename labels unproven inode identity" "Git-detected rename; Atomic recorded delete+add (inode identity unproven)" printf '%s\n' "$COMMAND_OUTPUT" +_atomic_lines_rename=$(printf '%s\n' "$COMMAND_OUTPUT" | extract_atomic_change_lines) if [[ -z "$_git_lines_rename" && -z "$_atomic_lines_rename" ]]; then _pass "pure rename: both sides have zero change lines (correct)" elif [[ "$_git_lines_rename" == "$_atomic_lines_rename" ]]; then _pass "pure rename: change lines match" else - _git_n=$(printf '%s\n' "$_git_lines_rename" | grep -c '.' 2>/dev/null || echo 0) - _atomic_n=$(printf '%s\n' "$_atomic_lines_rename" | grep -c '.' 2>/dev/null || echo 0) + _git_n=$(printf '%s\n' "$_git_lines_rename" | awk 'length($0) { n++ } END { print n + 0 }') + _atomic_n=$(printf '%s\n' "$_atomic_lines_rename" | awk 'length($0) { n++ } END { print n + 0 }') _fail "pure rename: change lines" "git=$_git_n lines, atomic=$_atomic_n lines" fi @@ -685,10 +724,11 @@ SHA2=$(git -C "$GIT_REPO" rev-parse HEAD 2>/dev/null) GIT_SHAS=("$SHA1" "$SHA2") -(cd "$GIT_REPO" && atomic git import 2>/dev/null) || true +require_success "import rename-and-modify fixture" atomic_in_git_repo git import ATOMIC_HASHES=() -_log=$( (cd "$GIT_REPO" && atomic log --format short --no-color --full-hash 2>/dev/null) || true ) +require_capture "read rename-and-modify fixture log" atomic_in_git_repo log --format short --no-color --full-hash +_log="$COMMAND_OUTPUT" if [[ -n "${_log:-}" ]]; then _tmp=() while IFS= read -r line; do @@ -713,8 +753,8 @@ fi # rename to renamed.txt # and the diff shows only the content delta. # We extract only the +/- lines from that section. -_git_full_rmod=$(git -C "$GIT_REPO" --no-pager diff -M10% "$SHA1" "$SHA2" \ - 2>/dev/null || true) +require_capture "read Git rename-and-modify diff" git -C "$GIT_REPO" --no-pager diff -M10% "$SHA1" "$SHA2" +_git_full_rmod="$COMMAND_OUTPUT" _git_lines_rmod=$(printf '%s\n' "$_git_full_rmod" | awk -v pat="renamed.txt" ' /^diff --git/ { # A rename section header looks like: diff --git a/old b/new @@ -727,33 +767,47 @@ _git_lines_rmod=$(printf '%s\n' "$_git_full_rmod" | awk -v pat="renamed.txt" ' next } in_sec { print } -' | grep -E '^\+[^+]|^-[^-]' || true) +' | extract_git_change_lines) # Atomic: extract +/- lines from the "renamed.txt" section. _atomic_lines_rmod="" if [[ -n "$_rename_mod_hash" ]]; then - _raw=$( (cd "$GIT_REPO" && atomic diff -c "$_rename_mod_hash" --no-color 2>/dev/null) || true ) + require_capture "read rename-and-modify change" atomic_in_git_repo change "$_rename_mod_hash" --full-hash + if [[ "$COMMAND_OUTPUT" != *"Git source revision: $SHA2"* ]]; then + _fail "rename+modify: imported change identity" "expected Git revision $SHA2: $COMMAND_OUTPUT" + print_summary || true + exit 1 + fi + require_capture "read Atomic rename-and-modify diff" atomic_in_git_repo diff -c "$_rename_mod_hash" --no-color + _raw="$COMMAND_OUTPUT" _atomic_lines_rmod=$(printf '%s\n' "$_raw" | awk -v pat="renamed.txt" ' /^diff --atomic/ { in_sec = (index($0, " b/" pat) > 0 && \ (index($0, " b/" pat " ") > 0 || \ - substr($0, length($0) - length(pat)) == pat)) ? 1 : 0 + substr($0, length($0) - length(pat) + 1) == pat)) ? 1 : 0 next } in_sec { print } - ' | grep -E '^\+[^+]|^-[^-]' || true) + ' | extract_atomic_change_lines) +else + _fail "rename+modify: imported change" "missing change for $SHA2" + print_summary || true + exit 1 fi # Sort both sides before comparing (hunk-ordering may differ). +if [[ "$_git_lines_rmod" != *'-gamma'* || "$_git_lines_rmod" != *'+GAMMA-MODIFIED'* || "$_git_lines_rmod" != *'+epsilon'* ]]; then + _fail "rename+modify: Git fixture has expected changes" "got: $_git_lines_rmod" +fi _sorted_git=$(printf '%s\n' "$_git_lines_rmod" | sort) _sorted_atomic=$(printf '%s\n' "$_atomic_lines_rmod" | sort) if [[ "$_sorted_git" == "$_sorted_atomic" ]]; then - _n=$(printf '%s\n' "$_git_lines_rmod" | grep -c '.' 2>/dev/null || echo 0) + _n=$(printf '%s\n' "$_git_lines_rmod" | awk 'length($0) { n++ } END { print n + 0 }') _pass "rename+modify: change lines match ($_n lines)" else - _gn=$(printf '%s\n' "$_git_lines_rmod" | grep -c '.' 2>/dev/null || echo 0) - _an=$(printf '%s\n' "$_atomic_lines_rmod" | grep -c '.' 2>/dev/null || echo 0) + _gn=$(printf '%s\n' "$_git_lines_rmod" | awk 'length($0) { n++ } END { print n + 0 }') + _an=$(printf '%s\n' "$_atomic_lines_rmod" | awk 'length($0) { n++ } END { print n + 0 }') _first=$(diff \ <(printf '%s\n' "$_git_lines_rmod" | sort) \ <(printf '%s\n' "$_atomic_lines_rmod" | sort) \ @@ -790,10 +844,11 @@ SHA2=$(git -C "$GIT_REPO" rev-parse HEAD 2>/dev/null) GIT_SHAS=("$SHA1" "$SHA2") -(cd "$GIT_REPO" && atomic git import 2>/dev/null) || true +require_success "import cross-directory rename fixture" atomic_in_git_repo git import ATOMIC_HASHES=() -_log=$( (cd "$GIT_REPO" && atomic log --format short --no-color --full-hash 2>/dev/null) || true ) +require_capture "read cross-directory rename fixture log" atomic_in_git_repo log --format short --no-color --full-hash +_log="$COMMAND_OUTPUT" if [[ -n "${_log:-}" ]]; then _tmp=() while IFS= read -r line; do @@ -810,22 +865,32 @@ if [[ ${#ATOMIC_HASHES[@]} -ge 2 ]]; then _dir_rename_hash="${ATOMIC_HASHES[1]}" fi -_git_lines_dir=$(git -C "$GIT_REPO" --no-pager diff "$SHA1" "$SHA2" \ - 2>/dev/null | grep -E '^\+[^+]|^-[^-]' || true) +require_capture "read Git cross-directory rename diff" git -C "$GIT_REPO" --no-pager diff "$SHA1" "$SHA2" +_git_lines_dir=$(printf '%s\n' "$COMMAND_OUTPUT" | extract_git_change_lines) _atomic_lines_dir="" -if [[ -n "$_dir_rename_hash" ]]; then - _raw=$( (cd "$GIT_REPO" && atomic diff -c "$_dir_rename_hash" --no-color 2>/dev/null) || true ) - _atomic_lines_dir=$(printf '%s\n' "$_raw" | grep -E '^\+[^+]|^-[^-]' || true) +if [[ -z "$_dir_rename_hash" ]]; then + _fail "cross-dir rename: imported change" "missing change for $SHA2" + print_summary || true + exit 1 +fi +require_capture "read cross-directory rename change" atomic_in_git_repo change "$_dir_rename_hash" --full-hash +if [[ "$COMMAND_OUTPUT" != *"Git source revision: $SHA2"* ]]; then + _fail "cross-dir rename: imported change identity" "expected Git revision $SHA2: $COMMAND_OUTPUT" + print_summary || true + exit 1 fi +require_capture "read Atomic cross-directory rename diff" atomic_in_git_repo diff -c "$_dir_rename_hash" --no-color +assert_output_contains "cross-directory rename labels unproven inode identity" "Git-detected rename; Atomic recorded delete+add (inode identity unproven)" printf '%s\n' "$COMMAND_OUTPUT" +_atomic_lines_dir=$(printf '%s\n' "$COMMAND_OUTPUT" | extract_atomic_change_lines) if [[ -z "$_git_lines_dir" && -z "$_atomic_lines_dir" ]]; then _pass "cross-dir rename: both sides have zero change lines (correct)" elif [[ "$_git_lines_dir" == "$_atomic_lines_dir" ]]; then _pass "cross-dir rename: change lines match" else - _gn=$(printf '%s\n' "$_git_lines_dir" | grep -c '.' 2>/dev/null || echo 0) - _an=$(printf '%s\n' "$_atomic_lines_dir" | grep -c '.' 2>/dev/null || echo 0) + _gn=$(printf '%s\n' "$_git_lines_dir" | awk 'length($0) { n++ } END { print n + 0 }') + _an=$(printf '%s\n' "$_atomic_lines_dir" | awk 'length($0) { n++ } END { print n + 0 }') _fail "cross-dir rename: change lines" "git=$_gn lines, atomic=$_an lines" fi diff --git a/tests/harness/12_full_repo_diff_parity.sh b/tests/harness/12_full_repo_diff_parity.sh index 7e0ec9af..2572d864 100755 --- a/tests/harness/12_full_repo_diff_parity.sh +++ b/tests/harness/12_full_repo_diff_parity.sh @@ -10,9 +10,8 @@ # PARITY_REPO_URL — git clone URL to test (default: hyperfine) # PARITY_SAMPLE_N — check every Nth commit (default: 10) # PARITY_MAX_COMMITS— stop after this many sampled commits (default: 50) -# PARITY_SKIP_PATHS — colon-separated glob patterns to skip -# (default: "*.lock:*.sum" — generated lockfiles -# whose diffs vary by algorithm) +# PARITY_SKIP_PATHS — optional colon-separated glob patterns to skip; +# any exclusions make the suite partial # # Examples: # @@ -48,16 +47,24 @@ source "$HARNESS_DIR/helpers.sh" REPO_URL="${PARITY_REPO_URL:-https://github.com/sharkdp/hyperfine.git}" SAMPLE_N="${PARITY_SAMPLE_N:-10}" MAX_COMMITS="${PARITY_MAX_COMMITS:-50}" -SKIP_PATHS="${PARITY_SKIP_PATHS:-*.lock:*.sum:package-lock.json:yarn.lock}" +SKIP_PATHS="${PARITY_SKIP_PATHS-}" +if [[ ! "$SAMPLE_N" =~ ^[1-9][0-9]*$ || ! "$MAX_COMMITS" =~ ^[1-9][0-9]*$ ]]; then + echo "PARITY_SAMPLE_N and PARITY_MAX_COMMITS must be positive integers" >&2 + exit 1 +fi # ── Helpers ────────────────────────────────────────────────────────────────── extract_git_change_lines() { - grep -E '^\+[^+]|^-[^-]' || true + awk '/^diff --git / { hunk = 0; next } /^@@ / { hunk = 1; next } hunk && /^[+-]/ { print }' } extract_atomic_change_lines() { - grep -E '^\+[^+]|^-[^-]' || true + awk '/^diff --atomic / { hunk = 0; next } /^@@ / { hunk = 1; next } hunk && /^[+-]/ { print }' +} + +atomic_in_clone() { + (cd "$CLONE_DIR" && atomic "$@") } # Check if a file path matches any of the skip patterns. @@ -87,10 +94,10 @@ atomic_diff_for_path() { local path_filter="$2" printf '%s\n' "$raw" | awk -v pat="$path_filter" ' /^diff --atomic/ { - # Match only when the path appears after " b/" (the new-file side). - in_sec = (index($0, " b/" pat) > 0 && \ - (index($0, " b/" pat " ") > 0 || \ - substr($0, length($0) - length(pat)) == pat)) ? 1 : 0 + # Additions use the new path; deletions use the old path. + in_sec = (index($0, " b/" pat " ") > 0 || \ + substr($0, length($0) - length(pat) - 2) == " b/" pat || \ + index($0, " a/" pat " /dev/null") > 0) ? 1 : 0 next } in_sec { print } @@ -116,7 +123,6 @@ get_rename_old_path() { # --diff-filter=R limits output to renamed files. # --name-status lines look like: R100old/pathnew/path git -C "$repo" diff --diff-filter=R --name-status "${sha}^" "$sha" \ - 2>/dev/null \ | awk -v np="$new_path" -F'\t' '$3 == np { print $2 }' } @@ -139,15 +145,18 @@ git_change_lines_for_file() { local path="$4" local is_rename="$5" # "1" if this file was renamed, "0" otherwise + local raw if [[ "$is_rename" == "1" ]]; then # -M10% enables rename detection with a low similarity threshold so # git recognises renames even when content changed significantly. # We then extract only the section of the diff that belongs to our # file (matched on the "b/" side of the diff --git header), # and filter to +/- lines. - git -C "$repo" --no-pager diff -M10% "$parent" "$sha" \ - 2>/dev/null \ - | awk -v pat="$path" ' + if ! raw="$(git -C "$repo" --no-pager diff -M10% "$parent" "$sha" 2>&1)"; then + printf 'Git rename diff failed: %s\n' "$raw" >&2 + return 1 + fi + printf '%s\n' "$raw" | awk -v pat="$path" ' /^diff --git/ { n = length($0) plen = length(pat) @@ -157,10 +166,13 @@ git_change_lines_for_file() { next } in_sec { print } - ' | extract_git_change_lines || true + ' | extract_git_change_lines else - git -C "$repo" --no-pager diff "$parent" "$sha" -- "$path" \ - 2>/dev/null | extract_git_change_lines || true + if ! raw="$(git -C "$repo" --no-pager diff "$parent" "$sha" -- "$path" 2>&1)"; then + printf 'Git diff failed: %s\n' "$raw" >&2 + return 1 + fi + printf '%s\n' "$raw" | extract_git_change_lines fi } @@ -184,6 +196,18 @@ change_lines_match() { # ── Main ───────────────────────────────────────────────────────────────────── +begin_section "Full-Repo Parity: Diff extraction" +_fixture=$'diff --atomic /dev/null b/src/file.rs (+2)\n--- /dev/null\n+++ b/src/file.rs\n@@ -0,0 +1,2 @@\n+--- a/content\n+\ndiff --atomic a/src/removed.rs /dev/null (-1)\n--- a/src/removed.rs\n+++ /dev/null\n@@ -1,1 +0,0 @@\n-old\ndiff --atomic /dev/null b/src/file.rs.extra (+1)\n--- /dev/null\n+++ b/src/file.rs.extra\n@@ -0,0 +1,1 @@\n+wrong' +_added="$(atomic_diff_for_path "$_fixture" 'src/file.rs')" +_removed="$(atomic_diff_for_path "$_fixture" 'src/removed.rs')" +if [[ "$_added" == $'+--- a/content\n+' && "$_removed" == '-old' ]]; then + _pass "path extraction keeps hunk content and handles additions and deletions" +else + _fail "path extraction" "addition: $_added; deletion: $_removed" + print_summary || true + exit 1 +fi + begin_section "Full-Repo Import: ${REPO_URL}" make_temp_repo "full-parity" @@ -198,8 +222,7 @@ fi _pass "cloned $REPO_URL" # Import -(cd "$CLONE_DIR" && atomic git import 2>/dev/null) || true -_pass "atomic git import completed" +require_success "atomic git import completed" atomic_in_clone git import # ── Collect commits to sample ──────────────────────────────────────────────── # @@ -210,11 +233,11 @@ begin_section "Sampling commits (every ${SAMPLE_N}, max ${MAX_COMMITS}) and buil # Get all commit SHAs oldest-first, skipping the root (no parent). ALL_COMMITS=() -while IFS= read -r sha; do - parent=$(git -C "$CLONE_DIR" rev-parse "${sha}^" 2>/dev/null) || true - [[ -z "$parent" ]] && continue +require_capture "read Git history for sampling" git -C "$CLONE_DIR" rev-list --reverse --parents HEAD +while IFS=' ' read -r sha parent rest; do + [[ -z "${parent:-}" ]] && continue ALL_COMMITS+=("$sha") -done < <(git -C "$CLONE_DIR" rev-list --reverse HEAD 2>/dev/null) +done <<< "$COMMAND_OUTPUT" TOTAL_COMMITS=${#ALL_COMMITS[@]} @@ -229,7 +252,13 @@ for sha in "${ALL_COMMITS[@]}"; do _idx=$((_idx + 1)) done -_pass "sampling ${#SAMPLED_COMMITS[@]} of $TOTAL_COMMITS commits" +if [[ ${#SAMPLED_COMMITS[@]} -gt 0 ]]; then + _pass "sampling ${#SAMPLED_COMMITS[@]} of $TOTAL_COMMITS commits" +else + _fail "sample commits" "no commits with parents were selected" + print_summary || true + exit 1 +fi # ── Build git-SHA → atomic-hash map (lazy, for sampled commits only) ───────── # @@ -245,7 +274,8 @@ _pass "sampling ${#SAMPLED_COMMITS[@]} of $TOTAL_COMMITS commits" # Our SAMPLED_COMMITS are drawn from git rev-list --reverse (oldest first), # so they cluster near the beginning of the reversed atomic log. # This makes the early-exit trigger much sooner for typical samples. -_all_atomic_log=$( (cd "$CLONE_DIR" && atomic log --format short --no-color --full-hash --reverse 2>/dev/null) || true ) +require_capture "read imported Atomic history" atomic_in_clone log --format short --no-color --full-hash --reverse +_all_atomic_log="$COMMAND_OUTPUT" MAP_GIT_SHAS=() MAP_ATOMIC_HASHES=() @@ -254,12 +284,17 @@ MAP_ATOMIC_HASHES=() # We call `atomic change` for each entry, extract "Commit:", and store. # Progress is shown every 100 entries. _map_count=0 -_total_log=$(printf '%s\n' "$_all_atomic_log" | grep -c '.' 2>/dev/null || echo 0) +_found_all=0 +_total_log=$(printf '%s\n' "$_all_atomic_log" | awk 'length($0) { n++ } END { print n + 0 }') while IFS= read -r logline; do _ah=$(echo "$logline" | awk '{print $1}') [[ -z "$_ah" ]] && continue - _detail=$( (cd "$CLONE_DIR" && atomic change "$_ah" 2>/dev/null) || true ) - _gsha=$(echo "$_detail" | grep "Commit:" | awk '{print $2}') + if ! _detail="$(atomic_in_clone change "$_ah" --full-hash 2>&1)"; then + _fail "read imported change $_ah" "$_detail" + print_summary || true + exit 1 + fi + _gsha=$(printf '%s\n' "$_detail" | sed -n 's/^.*Git source revision: //p') if [[ -n "$_gsha" ]]; then MAP_GIT_SHAS+=("$_gsha") MAP_ATOMIC_HASHES+=("$_ah") @@ -283,7 +318,11 @@ while IFS= read -r logline; do [[ $_found_all -eq 1 ]] && break done <<< "$_all_atomic_log" -_pass "map built: ${#MAP_GIT_SHAS[@]} entries (scanned $_map_count of $_total_log)" +if [[ "$_found_all" -eq 1 ]]; then + _pass "map built: ${#MAP_GIT_SHAS[@]} entries (scanned $_map_count of $_total_log)" +else + _fail "map sampled Git commits" "not all sampled commits were imported (scanned $_map_count of $_total_log)" +fi # Helper: look up atomic hash for a git SHA (full or short prefix match). _lookup_atomic_hash() { @@ -310,16 +349,14 @@ COMMITS_SKIP=0 FILES_PASS=0 FILES_FAIL=0 FILES_SKIP=0 +FILES_EXCLUDED=0 +FILES_EMPTY=0 +FILES_RENAMES=0 for curr_sha in "${SAMPLED_COMMITS[@]}"; do short="${curr_sha:0:8}" - parent=$(git -C "$CLONE_DIR" rev-parse "${curr_sha}^" 2>/dev/null) || true - - if [[ -z "$parent" ]]; then - COMMITS_SKIP=$((COMMITS_SKIP + 1)) - FILES_SKIP=$((FILES_SKIP + 1)) - continue - fi + require_capture "commit $short: resolve parent" git -C "$CLONE_DIR" rev-parse "${curr_sha}^" + parent="$COMMAND_OUTPUT" # Get the atomic hash for this commit. atomic_hash=$(_lookup_atomic_hash "$curr_sha") || true @@ -331,14 +368,17 @@ for curr_sha in "${SAMPLED_COMMITS[@]}"; do # Get files changed in this commit (excluding merges, submodules). changed_files=() + require_capture "commit $short: list changed paths" git -C "$CLONE_DIR" diff --name-only "$parent" "$curr_sha" + changed_output="$COMMAND_OUTPUT" while IFS= read -r fpath; do [[ -z "$fpath" ]] && continue should_skip "$fpath" && { FILES_SKIP=$((FILES_SKIP + 1)) + FILES_EXCLUDED=$((FILES_EXCLUDED + 1)) continue } changed_files+=("$fpath") - done < <(git -C "$CLONE_DIR" diff --name-only "$parent" "$curr_sha" 2>/dev/null) + done <<< "$changed_output" if [[ ${#changed_files[@]} -eq 0 ]]; then # Empty or all-skipped commit @@ -347,28 +387,95 @@ for curr_sha in "${SAMPLED_COMMITS[@]}"; do fi # Get the full atomic diff output once per commit (amortised cost). - atomic_raw=$( (cd "$CLONE_DIR" && atomic diff -c "$atomic_hash" --no-color 2>/dev/null) || true ) + require_capture "commit $short: read Atomic diff" atomic_in_clone diff -c "$atomic_hash" --no-color + atomic_raw="$COMMAND_OUTPUT" commit_ok=1 + checked_files=0 for fpath in "${changed_files[@]}"; do # Detect whether this file is the new path of a rename. _is_rename=0 - if [[ -n "$(get_rename_old_path "$CLONE_DIR" "$curr_sha" "$fpath")" ]]; then + if ! rename_path="$(get_rename_old_path "$CLONE_DIR" "$curr_sha" "$fpath" 2>&1)"; then + _fail "commit $short: detect rename for $fpath" "$rename_path" + FILES_FAIL=$((FILES_FAIL + 1)) + commit_ok=0 + continue + fi + if [[ -n "$rename_path" ]]; then _is_rename=1 fi # Git change lines for this file (rename-aware). - git_lines=$(git_change_lines_for_file \ - "$CLONE_DIR" "$parent" "$curr_sha" "$fpath" "$_is_rename") || true + if ! git_lines=$(git_change_lines_for_file \ + "$CLONE_DIR" "$parent" "$curr_sha" "$fpath" "$_is_rename" 2>&1); then + _fail "commit $short: read Git diff for $fpath" "$git_lines" + FILES_FAIL=$((FILES_FAIL + 1)) + commit_ok=0 + continue + fi # Atomic change lines for this file - atomic_lines=$(atomic_diff_for_path "$atomic_raw" "$fpath") || true + if ! atomic_lines=$(atomic_diff_for_path "$atomic_raw" "$fpath" 2>&1); then + _fail "commit $short: extract Atomic diff for $fpath" "$atomic_lines" + FILES_FAIL=$((FILES_FAIL + 1)) + commit_ok=0 + continue + fi + + # Empty text diffs do not prove parity. A pure Git rename can instead + # be verified by exact blob/mode equality and the imported change's + # explicit Git evidence and graph representation. + if [[ -z "$git_lines" && -z "$atomic_lines" ]]; then + if [[ "$_is_rename" == 1 ]]; then + require_capture "commit $short: read rename diff JSON" atomic_in_clone diff -c "$atomic_hash" --json --no-color + rename_diff_json="$COMMAND_OUTPUT" + require_capture "commit $short: read rename change JSON" atomic_in_clone change "$atomic_hash" --format json + rename_change_json="$COMMAND_OUTPUT" + if python3 -c ' +import json, subprocess, sys +repo,parent,sha,old,new,diff_text,change_text = sys.argv[1:] +def tree(revision, path): + raw = subprocess.check_output(["git", "-C", repo, "ls-tree", "-z", revision, "--", path]) + rows = [row for row in raw.split(b"\0") if row] + assert len(rows) == 1 + meta, name = rows[0].split(b"\t", 1) + assert name.decode() == path + mode, kind, oid = meta.decode().split() + assert kind == "blob" + return mode, oid +assert tree(parent, old) == tree(sha, new), "Git rename changed blob or mode" +doc = json.loads(diff_text) +matches = [item for item in doc["files"] if item["old_path"] == old and item["new_path"] == new] +assert len(matches) == 1 and not matches[0]["hunks"] and matches[0].get("git_rename_identity_unproven") is True +change = json.loads(change_text) +hunks = change["hunks"] +assert any(h["hunk_type"] == "FileDel" and h["path"] == old for h in hunks) +assert any(h["hunk_type"] == "FileAdd" and h["path"] == new for h in hunks) +loss = change["unhashed"]["git"]["stream"]["rename_identity_loss"] +assert any(x["from"] == old and x["to"] == new for x in loss) +' "$CLONE_DIR" "$parent" "$curr_sha" "$rename_path" "$fpath" "$rename_diff_json" "$rename_change_json"; then + FILES_PASS=$((FILES_PASS + 1)) + FILES_RENAMES=$((FILES_RENAMES + 1)) + checked_files=$((checked_files + 1)) + continue + else + _fail "commit $short: verify pure rename $fpath" "Git blobs/modes, diff evidence, or recorded graph operations differ" + FILES_FAIL=$((FILES_FAIL + 1)) + commit_ok=0 + continue + fi + fi + FILES_SKIP=$((FILES_SKIP + 1)) + FILES_EMPTY=$((FILES_EMPTY + 1)) + continue + fi + checked_files=$((checked_files + 1)) if change_lines_match "$git_lines" "$atomic_lines"; then FILES_PASS=$((FILES_PASS + 1)) else - git_count=$(printf '%s\n' "$git_lines" | grep -c '.' 2>/dev/null || echo 0) - atomic_count=$(printf '%s\n' "$atomic_lines" | grep -c '.' 2>/dev/null || echo 0) + git_count=$(printf '%s\n' "$git_lines" | awk 'length($0) { n++ } END { print n + 0 }') + atomic_count=$(printf '%s\n' "$atomic_lines" | awk 'length($0) { n++ } END { print n + 0 }') # Show first diverging line for diagnosis first_diff=$(diff \ <(printf '%s\n' "$git_lines" | sort) \ @@ -382,7 +489,9 @@ for curr_sha in "${SAMPLED_COMMITS[@]}"; do commit_ok=0 fi done - if [[ $commit_ok -eq 1 ]]; then + if [[ $commit_ok -eq 1 && $checked_files -eq 0 ]]; then + COMMITS_SKIP=$((COMMITS_SKIP + 1)) + elif [[ $commit_ok -eq 1 ]]; then COMMITS_PASS=$((COMMITS_PASS + 1)) else COMMITS_FAIL=$((COMMITS_FAIL + 1)) @@ -394,13 +503,25 @@ done echo "" echo " Commits: ${COMMITS_PASS} passed, ${COMMITS_FAIL} failed, ${COMMITS_SKIP} skipped" echo " Files: ${FILES_PASS} passed, ${FILES_FAIL} failed, ${FILES_SKIP} skipped" +echo " ${FILES_RENAMES} verified pure renames (non-text evidence)" echo "" +if [[ "$FILES_EXCLUDED" -gt 0 ]]; then + _skip "excluded $FILES_EXCLUDED generated file paths" "configured PARITY_SKIP_PATHS" +fi +if [[ "$FILES_EMPTY" -gt 0 ]]; then + _skip "$FILES_EMPTY paths had no textual change lines" "no text diff to compare" +fi +if [[ "$COMMITS_SKIP" -gt 0 ]]; then + _skip "$COMMITS_SKIP commits had no checked paths" "paths excluded or no textual change lines" +fi if [[ $COMMITS_FAIL -gt 0 || $FILES_FAIL -gt 0 ]]; then _fail "full-repo parity" \ "${COMMITS_FAIL} commits and ${FILES_FAIL} files failed diff parity" +elif [[ $FILES_PASS -gt 0 && $COMMITS_PASS -gt 0 ]]; then + _pass "full-repo parity: ${FILES_PASS} text diffs or verified pure renames matched Git" else - _pass "full-repo parity: all ${FILES_PASS} file-diffs matched git exactly" + _fail "full-repo parity" "no file-diffs were checked" fi print_summary diff --git a/tests/harness/13_import_fidelity.sh b/tests/harness/13_import_fidelity.sh index 46dae101..9126ed2b 100755 --- a/tests/harness/13_import_fidelity.sh +++ b/tests/harness/13_import_fidelity.sh @@ -21,7 +21,10 @@ source "$HARNESS_DIR/helpers.sh" assert_import_clean_with_bootstrap() { local desc="$1" local out - out="$(get_status_short)" + if ! out="$(get_status_short 2>&1)"; then + _fail "$desc" "status command failed: $out" + return + fi local unexpected="" while IFS= read -r line; do @@ -56,16 +59,26 @@ assert_import_clean_with_bootstrap() { assert_no_diff_except_bootstrap() { local desc="$1" local diff_out - diff_out="$(atomic diff 2>/dev/null || true)" + if ! diff_out="$(atomic diff --no-color 2>&1)"; then + _fail "$desc" "diff command failed: $diff_out" + return + fi local unexpected="" while IFS= read -r line; do - if [[ "$line" =~ ^diff\ --atomic\ a/.*\ b/(.*)\ \( ]]; then - local path="${BASH_REMATCH[1]}" + if [[ "$line" =~ ^diff\ --atomic\ (a/[^[:space:]]+|/dev/null)\ (b/[^[:space:]]+|/dev/null) ]]; then + local old_path="${BASH_REMATCH[1]}" new_path="${BASH_REMATCH[2]}" path + if [[ "$new_path" == /dev/null ]]; then + path="${old_path#a/}" + else + path="${new_path#b/}" + fi case "$path" in .atomicignore | .vault/*) continue ;; *) unexpected+="${path}"$'\n' ;; esac + elif [[ "$line" == 'diff --atomic '* ]]; then + unexpected+="unrecognized diff header: ${line}"$'\n' fi done <<< "$diff_out" @@ -106,8 +119,8 @@ git commit --quiet -m "Delete gamma" # beta should survive git_commit "Modify beta" "beta.txt" "beta v2" -atomic init >/dev/null 2>&1 -assert_success "import succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "import succeeds" atomic git import # Verify deleted files are gone from atomic assert_import_clean_with_bootstrap "working tree clean after import" @@ -136,8 +149,8 @@ done git rm --quiet dir/file1.txt dir/file3.txt dir/file5.txt git commit --quiet -m "Remove odd files" -atomic init >/dev/null 2>&1 -assert_success "import bulk deletion succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "import bulk deletion succeeds" atomic git import assert_import_clean_with_bootstrap "clean after bulk deletion import" assert_file_not_exists "file1 deleted" "dir/file1.txt" @@ -161,9 +174,10 @@ dd if=/dev/urandom bs=1 count=256 >> image.png 2>/dev/null git add image.png git commit --quiet -m "Add binary image" -atomic init >/dev/null 2>&1 -assert_success "import with binary add succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "import with binary add succeeds" atomic git import assert_import_clean_with_bootstrap "clean after binary add import" +assert_success "binary addition matches Git HEAD" git diff --exit-code HEAD -- image.png # ──────────────────────────────────────────────────────────────────────────── @@ -186,9 +200,10 @@ modified_size=$(wc -c < image.png | tr -d ' ') git add image.png git commit --quiet -m "Update binary image" -atomic init >/dev/null 2>&1 -assert_success "import with binary modification succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "import with binary modification succeeds" atomic git import assert_import_clean_with_bootstrap "clean after binary modification import" +assert_success "modified binary matches Git HEAD" git diff --exit-code HEAD -- image.png # Also verify diff shows nothing assert_no_diff_except_bootstrap "no diff after binary modification import" @@ -215,9 +230,10 @@ done final_size=$(wc -c < icon.png | tr -d ' ') -atomic init >/dev/null 2>&1 -assert_success "import with multiple binary mods succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "import with multiple binary mods succeeds" atomic git import assert_import_clean_with_bootstrap "clean after multiple binary modifications" +assert_success "final binary matches Git HEAD" git diff --exit-code HEAD -- icon.png # ──────────────────────────────────────────────────────────────────────────── @@ -234,8 +250,8 @@ git commit --quiet -m "Add logo" git rm --quiet logo.png git commit --quiet -m "Remove logo" -atomic init >/dev/null 2>&1 -assert_success "import with binary deletion succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "import with binary deletion succeeds" atomic git import assert_import_clean_with_bootstrap "clean after binary deletion" assert_file_not_exists "logo.png deleted" "logo.png" @@ -313,8 +329,8 @@ git commit --quiet -m "Rewrite tests, add helpers" git_commit "Update helpers" "helpers.rs" "$(cat helpers.rs) pub fn extra() {}" -atomic init >/dev/null 2>&1 -assert_success "import rewrite+add succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "import rewrite+add succeeds" atomic git import assert_import_clean_with_bootstrap "clean after rewrite+add import" # Both files must be tracked @@ -322,17 +338,8 @@ assert_file_exists "tests.rs exists" "tests.rs" assert_file_exists "helpers.rs exists" "helpers.rs" # Verify status doesn't show either as untracked -status_out="$(atomic status 2>/dev/null || true)" -if echo "$status_out" | grep -qF "tests.rs"; then - _fail "tests.rs not listed in status" "tests.rs appears in status output" -else - _pass "tests.rs not listed in status" -fi -if echo "$status_out" | grep -qF "helpers.rs"; then - _fail "helpers.rs not listed in status" "helpers.rs appears in status output" -else - _pass "helpers.rs not listed in status" -fi +assert_status_no_entry "tests.rs not listed in status" "tests.rs" +assert_status_no_entry "helpers.rs not listed in status" "helpers.rs" # ──────────────────────────────────────────────────────────────────────────── @@ -367,8 +374,8 @@ EOF git add brand_new.txt git commit --quiet -m "Add brand_new.txt" -atomic init >/dev/null 2>&1 -assert_success "import rename-vs-rewrite succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "import rename-vs-rewrite succeeds" atomic git import assert_import_clean_with_bootstrap "clean after rename-vs-rewrite import" assert_file_not_exists "original.txt removed" "original.txt" @@ -461,8 +468,8 @@ deleted_files=( ) # Import -atomic init >/dev/null 2>&1 -assert_success "stress test import succeeds" atomic git import +require_success "initialize Atomic repository" atomic init +require_success "stress test import succeeds" atomic git import assert_import_clean_with_bootstrap "clean after stress test import" # Verify expected files exist diff --git a/tests/harness/14_vault_kg.sh b/tests/harness/14_vault_kg.sh index f66007a6..f7cfb1c2 100755 --- a/tests/harness/14_vault_kg.sh +++ b/tests/harness/14_vault_kg.sh @@ -176,6 +176,11 @@ run_cmd atomic intent new "Fix authentication" assert_ran "intent create returns an ID" "Created intent: [A-Za-z0-9:_-]+::[0-9]+" first_id="$(echo "$OUT" | sed -n 's/.*Created intent: \([A-Za-z0-9:_-]*\).*/\1/p' | head -1)" +if [[ -z "$first_id" ]]; then + _fail "extract first intent ID" "create output: $OUT" + print_summary || true + exit 1 +fi # List intents — the new intent must appear under its assigned key run_cmd atomic intent list --json @@ -227,16 +232,10 @@ init_repo --vault # Create and track a file create_file "src/main.rs" 'fn main() { println!("hello"); }' -add_files "src/main.rs" +require_success "add main.rs" atomic add "src/main.rs" # Record should succeed (and auto-enrich the KG) -rc=0 -record_change "Add main.rs" >/dev/null 2>&1 || rc=$? -if [[ $rc -eq 0 ]]; then - _pass "record succeeds with vault enabled" -else - _fail "record succeeds with vault enabled" "exit $rc" -fi +require_success "record succeeds with vault enabled" record_change "Add main.rs" # Record auto-enriches: search must surface the recorded change or file run_cmd atomic vault query search "main" --json @@ -252,10 +251,10 @@ make_temp_repo "vault-kg" init_repo --vault --view main # Create some tracked files -create_file "src/auth.rs" "pub fn authenticate() { }\npub fn verify() { }" +create_file "src/auth.rs" $'pub fn authenticate() { }\npub fn verify() { }' create_file "src/main.rs" "fn main() { authenticate(); }" -add_files "src/auth.rs" "src/main.rs" -record_change "Add auth module" >/dev/null 2>&1 +require_success "add query source files" atomic add "src/auth.rs" "src/main.rs" +require_success "record auth module" record_change "Add auth module" # The service reports the processed total; keep this strict about successful # execution and a positive count rather than expecting the old local renderer. @@ -322,19 +321,8 @@ else else cd "$GIT_KG_DIR" - run_cmd atomic init --vault --view main - if [[ $RC -eq 0 ]]; then - _pass "init --vault in git checkout" - else - _fail "init --vault in git checkout" "exit $RC: $(echo "$OUT" | head -2 | tr '\n' ' ')" - fi - - run_cmd atomic git import - if [[ $RC -eq 0 ]]; then - _pass "git import completes with vault" - else - _fail "git import completes with vault" "exit $RC: $(echo "$OUT" | tail -2 | tr '\n' ' ')" - fi + require_success "init --vault in git checkout" atomic init --vault --view main + require_success "git import completes with vault" atomic git import # Enrich KG from imported data run_cmd atomic vault query enrich @@ -367,6 +355,11 @@ init_repo --vault run_cmd atomic memory new --kind lesson --text "Prefer atomic intent new over the removed vault verb" assert_ran "memory create returns an ID" "Created memory:" mem_id="$(echo "$OUT" | sed -n 's/.*Created memory: \([A-Za-z0-9]*\).*/\1/p' | head -1)" +if [[ -z "$mem_id" ]]; then + _fail "extract memory ID" "create output: $OUT" + print_summary || true + exit 1 +fi run_cmd atomic memory list --json assert_ran "memory list shows the created memory" "$mem_id" @@ -411,19 +404,19 @@ begin_section "Queries: View Isolation" make_temp_repo "query-view-scope" init_repo create_file "src/lib.rs" $'pub fn target() {}\npub fn caller() { target(); }\n' -add_files "src/lib.rs" -assert_success "record base query source" atomic record -m "base query source" -assert_success "create inheriting query view" atomic view create query-feature --draft --parent dev -assert_success "switch to query feature" atomic view switch query-feature --force +require_success "add base query source" atomic add "src/lib.rs" +require_success "record base query source" atomic record -m "base query source" +require_success "create inheriting query view" atomic view create query-feature --draft --parent dev +require_success "switch to query feature" atomic view switch query-feature --force # Same paths/symbols, different signatures and call bodies: filtering paths alone # would still expose the wrong view's source or caller relationships. create_file "src/lib.rs" $'pub fn target(secret: SiblingSecret) {}\npub fn caller() {}\n' create_file "src/sibling.rs" $'pub fn sibling_only() { target(); }\n// sibling_marker\n' -add_files "src/sibling.rs" -assert_success "record sibling query source" atomic record -m "sibling query source" -assert_success "enrich feature query data" atomic query enrich -assert_success "build working-copy index on feature" atomic query index --rebuild +require_success "add sibling query source" atomic add "src/sibling.rs" +require_success "record sibling query source" atomic record -m "sibling query source" +require_success "enrich feature query data" atomic query enrich +require_success "build working-copy index on feature" atomic query index --rebuild run_cmd atomic query code "SiblingSecret" --json assert_ran "feature code includes its own signature" 'SiblingSecret' @@ -435,7 +428,7 @@ else _fail "feature does not retain base caller's removed call" "exit $RC: $OUT" fi -assert_success "switch back to base query view" atomic view switch dev --force +require_success "switch back to base query view" atomic view switch dev --force create_file "src/lib.rs" $'pub fn disk_only() {}\n' run_cmd atomic query search "SiblingSecret" --json diff --git a/tests/harness/16_vault_pull_bootstrap.sh b/tests/harness/16_vault_pull_bootstrap.sh index b42c8d50..2e4cb67a 100755 --- a/tests/harness/16_vault_pull_bootstrap.sh +++ b/tests/harness/16_vault_pull_bootstrap.sh @@ -12,9 +12,8 @@ # 1. Vault bootstrap: .vault/ files materialized from graph → redb tables # auto-initialized → `atomic vault list` works on the receiving side # 2. KG enrichment runs on the receiving side after bootstrap -# 3. Intent paths are scoped under the current view name -# 4. Two views can each have an intent with the same JIRA-style ID number -# without file-path collision +# 3. Intents on different views materialize to distinct ULID-scoped paths +# 4. Each view can list and inspect its own intent without collision HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" @@ -32,19 +31,28 @@ copy_all_changes() { local src_changes="$src_repo/.atomic/changes" local dst_changes="$dst_repo/.atomic/changes" - find "$src_changes" -name "*.change" -type f 2>/dev/null | while read -r f; do + find "$src_changes" -name "*.change" -type f -print0 | while IFS= read -r -d '' f; do local rel="${f#$src_changes/}" local dst_dir="$dst_changes/$(dirname "$rel")" - mkdir -p "$dst_dir" - cp "$f" "$dst_dir/" + mkdir -p "$dst_dir" || exit 1 + cp "$f" "$dst_dir/" || exit 1 + cmp -s "$f" "$dst_dir/$(basename "$f")" || exit 1 done } -# ── Helper: get all change hashes from a repo (oldest first) ──────────── +# ── Helper: get view change hashes in dependency order ────────────────── get_change_hashes() { - find "$1/.atomic/changes" -name "*.change" -type f 2>/dev/null \ - | sort | xargs -I{} basename {} .change + local log + if ! log="$(cd "$1" && atomic log --format json --reverse 2>&1)"; then + printf 'cannot read source history: %s\n' "$log" >&2 + return 1 + fi + printf '%s\n' "$log" | python3 -c ' +import json, sys +entries = json.load(sys.stdin) +assert entries and all(isinstance(e.get("hash"), str) and len(e["hash"]) == 52 for e in entries) +print("\n".join(e["hash"] for e in entries))' } # ════════════════════════════════════════════════════════════════════════════ @@ -62,23 +70,22 @@ assert_dir_exists "User A: .vault exists" ".vault" assert_file_exists "User A: default skill exists" ".vault/skills/atomic-vault.md" # Record the initial state (includes .atomicignore + .vault/ files) -out="$(atomic log --json 2>/dev/null)" || true -if echo "$out" | grep -qE "hash"; then - _pass "User A: initial changes recorded" -else - _pass "User A: repo initialized with vault" -fi +require_capture "User A: read initial changes" get_change_hashes "$REPO_A" # Create a source file and record it too create_file "src/main.rs" 'fn main() { println!("hello"); }' -add_files "src/main.rs" -record_change "Add main.rs" >/dev/null 2>&1 -_pass "User A: recorded main.rs" +require_success "User A: add main.rs" atomic add "src/main.rs" +require_success "User A: record main.rs" record_change "Add main.rs" # Collect all change hashes -A_HASHES="$(get_change_hashes "$REPO_A")" +require_capture "User A: read ordered change hashes" get_change_hashes "$REPO_A" +A_HASHES="$COMMAND_OUTPUT" A_HASH_COUNT="$(echo "$A_HASHES" | wc -l | tr -d ' ')" -_pass "User A: has $A_HASH_COUNT change(s)" +if [[ "$A_HASH_COUNT" -ge 2 ]]; then + _pass "User A: has $A_HASH_COUNT recorded changes" +else + _fail "User A: has bootstrap and source changes" "found $A_HASH_COUNT" +fi # ═══════════════════════════════════════════════════════════════════════════ begin_section "Vault Bootstrap: User B receives changes" @@ -94,23 +101,23 @@ init_repo --no-vault assert_dir_not_exists "User B: no .vault before pull" ".vault" # Copy all change files from User A → User B -copy_all_changes "$REPO_A" "$REPO_B" -_pass "Copied change files from User A to User B" +require_success "copy change files from User A to User B byte-for-byte" copy_all_changes "$REPO_A" "$REPO_B" # Insert all changes into User B's dev view (simulating pull) cd "$REPO_B" for hash in $A_HASHES; do - atomic insert "$hash" --view dev >/dev/null 2>&1 || true + require_success "insert source change $hash" atomic insert "$hash" --view dev done -_pass "Inserted all changes into User B's dev view" # Materialize the working copy -atomic view switch dev >/dev/null 2>&1 || true +require_success "select and materialize User B dev" atomic view switch dev # The .vault/ directory should now exist on disk (materialized from graph) assert_dir_exists "User B: .vault materialized after insert" ".vault" assert_file_exists "User B: skill file materialized" ".vault/skills/atomic-vault.md" assert_file_exists "User B: main.rs materialized" "src/main.rs" +assert_success "User B: main.rs matches source byte-for-byte" cmp -s "$REPO_A/src/main.rs" "$REPO_B/src/main.rs" +assert_success "User B: vault skill matches source byte-for-byte" cmp -s "$REPO_A/.vault/skills/atomic-vault.md" "$REPO_B/.vault/skills/atomic-vault.md" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Vault Bootstrap: Verify vault operations work on User B" @@ -121,10 +128,11 @@ cd "$REPO_B" # Bootstrap the vault from materialized files # (In production, pull/clone would do this automatically. # Here we call it explicitly since we simulated with insert.) -atomic vault init >/dev/null 2>&1 || true +require_success "User B: bootstrap vault" atomic vault init # Vault list should work and show entries -out="$(atomic vault list --json 2>/dev/null)" || true +require_capture "User B: list vault" atomic vault list --json +out="$COMMAND_OUTPUT" if echo "$out" | grep -q "skills/"; then _pass "User B: vault list shows entries after bootstrap" else @@ -132,7 +140,8 @@ else fi # Creating an intent should work -out="$(atomic intent new "User B task" 2>&1)" || true +require_capture "User B: create intent" atomic intent new "User B task" +out="$COMMAND_OUTPUT" if echo "$out" | grep -qE "Created intent:"; then _pass "User B: intent create works after bootstrap" else @@ -149,40 +158,34 @@ make_temp_repo "view-intents" init_repo --vault # Create a draft view (simulates agent session) -new_view "agent-session-1" --draft --parent dev >/dev/null 2>&1 || \ - new_view "agent-session-1" >/dev/null 2>&1 -switch_view "agent-session-1" >/dev/null 2>&1 +require_success "create agent-session-1" new_view "agent-session-1" --draft --parent dev +require_success "switch to agent-session-1" switch_view "agent-session-1" # Create an intent on the draft view. Intent files are flat and ULID-scoped # (globally unique, not view-scoped): # intents//intent.md -out="$(atomic intent new "Draft intent 1" 2>&1)" || true -intent_file_1="$(echo "$out" | sed -n 's|.*file: \.vault/||p' | head -1)" || true +require_capture "create first draft intent" atomic intent new "Draft intent 1" +out="$COMMAND_OUTPUT" +intent_file_1="$(echo "$out" | sed -n 's|.*file: \.vault/||p' | head -1)" +first_id="$(echo "$out" | sed -n 's/.*Created intent: \([A-Za-z0-9:_-]*\).*/\1/p' | head -1)" # The path is ULID-scoped under intents/ -if echo "$intent_file_1" | grep -qE "intents/[A-Za-z0-9]+/intent\.md"; then +if [[ "$intent_file_1" =~ ^intents/[0-9A-HJKMNP-TV-Z]{26}/intent\.md$ ]]; then _pass "Intent path is scoped: $intent_file_1" else - # Fallback: check if any intent dir exists - found="$(find .vault/intents -name "intent.md" 2>/dev/null | head -1)" || true - if [[ -n "$found" ]]; then - _pass "Intent directory scoped correctly" - else - _fail "Intent path is scoped" "file: $intent_file_1" - fi + _fail "Intent path is scoped" "file: $intent_file_1" fi # The intent file should exist on disk if [[ -n "$intent_file_1" ]] && [[ -f ".vault/$intent_file_1" ]]; then _pass "Intent file exists on disk" else - # Check for any intent file under manual/ or the view directory - found="$(find .vault/intents -name "intent.md" 2>/dev/null | head -1)" || true - if [[ -n "$found" ]]; then - _pass "Intent file found on disk" - else - _fail "Intent file exists on disk" "file: .vault/$intent_file_1" - fi + _fail "Intent file exists on disk" "file: .vault/$intent_file_1" +fi +if [[ -z "$first_id" || -z "$intent_file_1" ]]; then + _fail "first intent identity and path" "create output: $out" + print_summary || true + exit 1 fi # ═══════════════════════════════════════════════════════════════════════════ @@ -190,13 +193,14 @@ begin_section "View-Scoped Intents: No collision across views" # ═══════════════════════════════════════════════════════════════════════════ # Create a second draft view -new_view "agent-session-2" --draft --parent dev >/dev/null 2>&1 || \ - new_view "agent-session-2" >/dev/null 2>&1 -switch_view "agent-session-2" >/dev/null 2>&1 +require_success "create agent-session-2" new_view "agent-session-2" --draft --parent dev +require_success "switch to agent-session-2" switch_view "agent-session-2" # Create an intent on the second view -out2="$(atomic intent new "Draft intent 2" 2>&1)" || true -intent_file_2="$(echo "$out2" | sed -n 's|.*file: \.vault/||p' | head -1)" || true +require_capture "create second draft intent" atomic intent new "Draft intent 2" +out2="$COMMAND_OUTPUT" +intent_file_2="$(echo "$out2" | sed -n 's|.*file: \.vault/||p' | head -1)" +second_id="$(echo "$out2" | sed -n 's/.*Created intent: \([A-Za-z0-9:_-]*\).*/\1/p' | head -1)" # The two intent files should be different paths: each intent gets its own # unique ULID directory, so paths never collide across views. @@ -207,57 +211,66 @@ if [[ -n "$intent_file_1" ]] && [[ -n "$intent_file_2" ]]; then _fail "Intent path collision" "both got: $intent_file_1" fi else - # Fallback: count total intent files — should be at least 2 - total_intents="$(find .vault/intents -name "intent.md" 2>/dev/null | wc -l | tr -d ' ')" || true - if [[ "$total_intents" -ge 2 ]]; then - _pass "Multiple intent files exist ($total_intents)" - else - _fail "Multiple intents" "expected >= 2, got $total_intents" - fi + _fail "Intent files have different paths" "first: $intent_file_1; second: $intent_file_2" fi +if [[ -z "$second_id" || -z "$intent_file_2" || "$second_id" == "$first_id" ]]; then + _fail "second intent identity and path" "create output: $out2" + print_summary || true + exit 1 +fi +if [[ "$intent_file_2" =~ ^intents/[0-9A-HJKMNP-TV-Z]{26}/intent\.md$ ]]; then + _pass "second intent has a ULID-scoped path" +else + _fail "second intent has a ULID-scoped path" "file: $intent_file_2" +fi +assert_file_exists "second intent file exists" ".vault/$intent_file_2" # Each view should only see its own intents -switch_view "agent-session-1" >/dev/null 2>&1 -list1="$(atomic intent list --json 2>/dev/null)" || true -count1="$(echo "$list1" | grep -c '"id"' || true)" +require_success "switch to agent-session-1" switch_view "agent-session-1" +require_capture "list agent-session-1 intents" atomic intent list --json +list1="$COMMAND_OUTPUT" -switch_view "agent-session-2" >/dev/null 2>&1 -list2="$(atomic intent list --json 2>/dev/null)" || true -count2="$(echo "$list2" | grep -c '"id"' || true)" +require_success "switch to agent-session-2" switch_view "agent-session-2" +require_capture "list agent-session-2 intents" atomic intent list --json +list2="$COMMAND_OUTPUT" # Note: the manifest is shared (local redb), so both intents may appear. # The key property is that the file paths don't collide. -_pass "View 1 intents: $count1, View 2 intents: $count2" +assert_success "first intent is listed on its view" python3 -c \ + 'import json,sys; assert any(e["id"] == sys.argv[2] and e["title"] == "Draft intent 1" for e in json.loads(sys.argv[1]))' "$list1" "$first_id" +assert_success "second intent is listed on its view" python3 -c \ + 'import json,sys; assert any(e["id"] == sys.argv[2] and e["title"] == "Draft intent 2" for e in json.loads(sys.argv[1]))' "$list2" "$second_id" # ═══════════════════════════════════════════════════════════════════════════ begin_section "View-Scoped Intents: Intent operations work on scoped paths" # ═══════════════════════════════════════════════════════════════════════════ # Switch back to view 1 and verify show/update work -switch_view "agent-session-1" >/dev/null 2>&1 - -# Get the intent ID -first_id="$(echo "$list1" | grep -oE '"id"\s*:\s*"[^"]+"' | head -1 \ - | sed 's/.*"\([^"]*\)"$/\1/')" || true +require_success "switch back to agent-session-1" switch_view "agent-session-1" if [[ -n "$first_id" ]]; then # Show should work - show_out="$(atomic intent show "$first_id" --json 2>/dev/null)" || true + require_capture "show first draft intent" atomic intent show "$first_id" --json + show_out="$COMMAND_OUTPUT" if echo "$show_out" | grep -qi "draft intent"; then _pass "Intent show works on view-scoped path" else - _pass "Intent show completes for $first_id" + _fail "Intent show returns first intent" "got: $show_out" fi # Update should work - update_out="$(atomic intent update "$first_id" --status in-progress 2>&1)" || true + require_capture "update first draft intent" atomic intent update "$first_id" --status in-progress + update_out="$COMMAND_OUTPUT" if echo "$update_out" | grep -qiE "updated|in-progress"; then _pass "Intent update works on view-scoped path" else - _pass "Intent update completes for $first_id" + _fail "Intent update works on view-scoped path" "got: $update_out" fi + require_capture "show updated first intent" atomic intent show "$first_id" --json + assert_success "updated intent status persists" python3 -c \ + 'import json,sys; data=json.loads(sys.argv[1]); assert data["status"] == "in_progress"' "$COMMAND_OUTPUT" else - _skip "Intent show/update" "could not extract intent ID from list" + _fail "Intent show/update" "could not extract intent ID from creation" fi # ════════════════════════════════════════════════════════════════════════════ diff --git a/tests/harness/17_cross_view_merge.sh b/tests/harness/17_cross_view_merge.sh index aad3bedf..b68b607f 100755 --- a/tests/harness/17_cross_view_merge.sh +++ b/tests/harness/17_cross_view_merge.sh @@ -43,6 +43,58 @@ source "$HARNESS_DIR/helpers.sh" # assert_max_lines, assert_file_contains, assert_file_not_contains). source "$HARNESS_DIR/merge_helpers.sh" +# A refused insert is a failure of this suite's merge expectation, not a +# successful merge. Verify that it preserved the target history and bytes, +# then explicitly mark the post-insert assertions as unexercised. +attempt_merge() { + local desc="$1" from="$2" to="$3"; shift 3 + local before after output file index=0 snapshot_dir + if ! before="$(atomic log --format json --view "$to" --all 2>&1)"; then + _fail "$desc: read target history" "$before" + _skip "$desc: merge postconditions" "target history unavailable" + return 1 + fi + if ! snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-merge-snapshot-XXXXXX")"; then + _fail "$desc: create snapshot directory" "mktemp failed" + _skip "$desc: merge postconditions" "snapshot unavailable" + return 1 + fi + _HARNESS_TMPDIRS+=("$snapshot_dir") + for file in "$@"; do + if ! cp "$file" "$snapshot_dir/$index"; then + _fail "$desc: snapshot $file" "cannot save pre-insert bytes" + _skip "$desc: merge postconditions" "snapshot unavailable" + return 1 + fi + index=$((index + 1)) + done + if output="$(insert_from_view "$from" "$to" 2>&1)"; then + _pass "$desc: insert succeeds" + return 0 + fi + _fail "$desc: insert succeeds" "$output" + if after="$(atomic log --format json --view "$to" --all 2>&1)"; then + if [[ "$before" == "$after" ]]; then + _pass "$desc: refused insert preserves target history" + else + _fail "$desc: refused insert preserves target history" "target history changed" + fi + else + _fail "$desc: read target history after refusal" "$after" + fi + index=0 + for file in "$@"; do + if cmp -s "$snapshot_dir/$index" "$file"; then + _pass "$desc: refused insert preserves $file" + else + _fail "$desc: refused insert preserves $file" "working bytes changed" + fi + index=$((index + 1)) + done + _skip "$desc: merge postconditions" "insert failed" + return 1 +} + # ═══════════════════════════════════════════════════════════════════════════ begin_section "Case 1: Non-overlapping edits merge without duplication" @@ -72,14 +124,13 @@ pool_size = 5 level = "info" format = "json"' -assert_success "add config.toml on dev" atomic add config.toml -record_change "Initial config" >/dev/null 2>&1 +require_success "add config.toml on dev" atomic add config.toml +require_success "record initial config" record_change "Initial config" # Create draft view from dev -new_view "feature-config" --draft --parent dev >/dev/null 2>&1 || \ - new_view "feature-config" >/dev/null 2>&1 -insert_from_view "dev" "feature-config" >/dev/null 2>&1 || true -switch_view "feature-config" >/dev/null 2>&1 +require_success "create feature-config" new_view "feature-config" --draft --parent dev +require_success "insert dev into feature-config" insert_from_view "dev" "feature-config" +require_success "switch to feature-config" switch_view "feature-config" assert_current_view "on feature-config" "feature-config" # Modify section 1 on draft (change host) @@ -95,10 +146,10 @@ pool_size = 5 level = "info" format = "json"' -record_change "Change server host to 0.0.0.0" >/dev/null 2>&1 +require_success "record server host edit" record_change "Change server host to 0.0.0.0" # Switch to dev and modify section 3 (change log level) -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" assert_current_view "back on dev" "dev" overwrite_file "config.toml" '[server] @@ -113,10 +164,10 @@ pool_size = 5 level = "debug" format = "json"' -record_change "Change log level to debug" >/dev/null 2>&1 +require_success "record log level edit" record_change "Change log level to debug" # Insert from draft → dev -insert_out="$(insert_from_view "feature-config" "dev" 2>&1)" || true +if attempt_merge "non-overlapping edits" "feature-config" "dev" config.toml; then # Verify: file exists and has no duplication assert_file_exists "config.toml exists after insert" "config.toml" @@ -136,11 +187,23 @@ assert_max_lines "config.toml has reasonable line count" "config.toml" 20 # Both changes should be present assert_file_contains "draft edit present (host = 0.0.0.0)" "config.toml" '0.0.0.0' assert_file_contains "dev edit present (level = debug)" "config.toml" 'debug' +assert_file_content "non-overlapping merge preserves exact content" "config.toml" '[server] +host = "0.0.0.0" +port = 8080 + +[database] +url = "postgres://localhost/mydb" +pool_size = 5 + +[logging] +level = "debug" +format = "json"' echo "" echo " config.toml after merge:" cat config.toml | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -163,14 +226,13 @@ create_file "version.txt" 'version = "1.0.0" name = "my-app" description = "A sample application"' -assert_success "add version.txt on dev" atomic add version.txt -record_change "Initial version file" >/dev/null 2>&1 +require_success "add version.txt on dev" atomic add version.txt +require_success "record initial version file" record_change "Initial version file" # Create draft view -new_view "bump-major" --draft --parent dev >/dev/null 2>&1 || \ - new_view "bump-major" >/dev/null 2>&1 -insert_from_view "dev" "bump-major" >/dev/null 2>&1 || true -switch_view "bump-major" >/dev/null 2>&1 +require_success "create bump-major" new_view "bump-major" --draft --parent dev +require_success "insert dev into bump-major" insert_from_view "dev" "bump-major" +require_success "switch to bump-major" switch_view "bump-major" assert_current_view "on bump-major" "bump-major" # Draft: bump to 2.0.0 @@ -178,19 +240,19 @@ overwrite_file "version.txt" 'version = "2.0.0" name = "my-app" description = "A sample application"' -record_change "Bump to 2.0.0" >/dev/null 2>&1 +require_success "record version 2.0.0" record_change "Bump to 2.0.0" # Switch to dev: bump to 1.1.0 -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" overwrite_file "version.txt" 'version = "1.1.0" name = "my-app" description = "A sample application"' -record_change "Bump to 1.1.0" >/dev/null 2>&1 +require_success "record version 1.1.0" record_change "Bump to 1.1.0" # Insert from draft → dev (this is the collision) -insert_out="$(insert_from_view "bump-major" "dev" 2>&1)" || true +if attempt_merge "overlapping edits" "bump-major" "dev" version.txt; then assert_file_exists "version.txt exists after overlapping insert" "version.txt" @@ -200,14 +262,14 @@ assert_file_exists "version.txt exists after overlapping insert" "version.txt" # - two "version = …" lines WRAPPED in conflict markers # (`>>>>>>>` / `=======` / `<<<<<<<`), so a human can see both sides. # What we must reject is duplication WITHOUT markers. -version_lines="$(grep -c 'version = ' version.txt)" +version_lines="$(grep -c 'version = ' version.txt || true)" version_lines="$(echo "$version_lines" | tr -d '[:space:]')" has_conflict_markers=0 -if grep -qE '^(>>>>>>>|<<<<<<<|=======)' version.txt; then +if grep -qE '^>>>>>>>' version.txt && grep -qE '^=======' version.txt && grep -qE '^<<<<<<<' version.txt; then has_conflict_markers=1 fi -if [[ "$version_lines" -le 1 || "$has_conflict_markers" -eq 1 ]]; then +if [[ "$version_lines" -eq 1 || ( "$version_lines" -ge 2 && "$has_conflict_markers" -eq 1 ) ]]; then _pass "no silent duplication of version line ($version_lines occurrence(s), markers=$has_conflict_markers)" else _fail "no silent duplication of version line" "found $version_lines 'version = ' lines with no conflict markers" @@ -226,6 +288,7 @@ echo "" echo " version.txt after overlapping merge:" cat version.txt | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -257,15 +320,14 @@ create_file "app.py" 'def main(): if __name__ == "__main__": main()' -assert_success "add readme.md" atomic add readme.md -assert_success "add app.py" atomic add app.py -record_change "Initial files" >/dev/null 2>&1 +require_success "add readme.md" atomic add readme.md +require_success "add app.py" atomic add app.py +require_success "record initial files" record_change "Initial files" # Create draft view -new_view "feature-docs" --draft --parent dev >/dev/null 2>&1 || \ - new_view "feature-docs" >/dev/null 2>&1 -insert_from_view "dev" "feature-docs" >/dev/null 2>&1 || true -switch_view "feature-docs" >/dev/null 2>&1 +require_success "create feature-docs" new_view "feature-docs" --draft --parent dev +require_success "insert dev into feature-docs" insert_from_view "dev" "feature-docs" +require_success "switch to feature-docs" switch_view "feature-docs" assert_current_view "on feature-docs" "feature-docs" # Draft: change readme heading + add helper to app.py @@ -286,10 +348,10 @@ def main(): if __name__ == "__main__": main()' -record_change "Update docs heading and add greet function" >/dev/null 2>&1 +require_success "record draft documentation and function" record_change "Update docs heading and add greet function" # Switch to dev: change readme heading differently -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" overwrite_file "readme.md" '# My Cool Project @@ -299,10 +361,10 @@ This is the project description. Run the app with `python app.py`.' -record_change "Update docs heading on dev" >/dev/null 2>&1 +require_success "record dev documentation" record_change "Update docs heading on dev" # Insert from draft → dev -insert_out="$(insert_from_view "feature-docs" "dev" 2>&1)" || true +if attempt_merge "two-file overlap" "feature-docs" "dev" readme.md app.py; then # Check readme.md — the heading collides assert_file_exists "readme.md exists after insert" "readme.md" @@ -319,7 +381,7 @@ assert_max_lines "readme.md reasonable size" "readme.md" 20 assert_file_exists "app.py exists after insert" "app.py" # main() should appear exactly once as a definition -def_main_count="$(grep -c 'def main' app.py || echo 0)" +def_main_count="$(grep -c 'def main' app.py || true)" if [[ "$def_main_count" -eq 1 ]]; then _pass "def main() appears once in app.py" else @@ -339,6 +401,7 @@ echo "" echo " app.py after merge:" cat app.py | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -371,14 +434,13 @@ create_file "changelog.md" '# Changelog ## v1.2.0 - Performance improvements' -assert_success "add changelog.md" atomic add changelog.md -record_change "Initial changelog" >/dev/null 2>&1 +require_success "add changelog.md" atomic add changelog.md +require_success "record initial changelog" record_change "Initial changelog" # Create draft view -new_view "release-notes" --draft --parent dev >/dev/null 2>&1 || \ - new_view "release-notes" >/dev/null 2>&1 -insert_from_view "dev" "release-notes" >/dev/null 2>&1 || true -switch_view "release-notes" >/dev/null 2>&1 +require_success "create release-notes" new_view "release-notes" --draft --parent dev +require_success "insert dev into release-notes" insert_from_view "dev" "release-notes" +require_success "switch to release-notes" switch_view "release-notes" assert_current_view "on release-notes" "release-notes" # Draft: append v1.3.0 @@ -396,10 +458,10 @@ overwrite_file "changelog.md" '# Changelog ## v1.3.0 - New feature: user profiles' -record_change "Add v1.3.0 release notes" >/dev/null 2>&1 +require_success "record v1.3.0 notes" record_change "Add v1.3.0 release notes" # Switch to dev: append v1.4.0 -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" overwrite_file "changelog.md" '# Changelog @@ -415,10 +477,10 @@ overwrite_file "changelog.md" '# Changelog ## v1.4.0 - Security patches' -record_change "Add v1.4.0 release notes" >/dev/null 2>&1 +require_success "record v1.4.0 notes" record_change "Add v1.4.0 release notes" # Insert from draft → dev -insert_out="$(insert_from_view "release-notes" "dev" 2>&1)" || true +if attempt_merge "same-gap appends" "release-notes" "dev" changelog.md; then assert_file_exists "changelog.md exists after insert" "changelog.md" @@ -443,6 +505,7 @@ echo "" echo " changelog.md after merge:" cat changelog.md | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -470,14 +533,13 @@ def deprecated_multiply(a, b): def subtract(a, b): return a - b' -assert_success "add utils.py" atomic add utils.py -record_change "Initial utils" >/dev/null 2>&1 +require_success "add utils.py" atomic add utils.py +require_success "record initial utils" record_change "Initial utils" # Create draft view -new_view "cleanup" --draft --parent dev >/dev/null 2>&1 || \ - new_view "cleanup" >/dev/null 2>&1 -insert_from_view "dev" "cleanup" >/dev/null 2>&1 || true -switch_view "cleanup" >/dev/null 2>&1 +require_success "create cleanup" new_view "cleanup" --draft --parent dev +require_success "insert dev into cleanup" insert_from_view "dev" "cleanup" +require_success "switch to cleanup" switch_view "cleanup" assert_current_view "on cleanup" "cleanup" # Draft: delete the deprecated function @@ -487,10 +549,10 @@ overwrite_file "utils.py" 'def add(a, b): def subtract(a, b): return a - b' -record_change "Remove deprecated_multiply" >/dev/null 2>&1 +require_success "record removal" record_change "Remove deprecated_multiply" # Switch to dev: modify the deprecated function -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" overwrite_file "utils.py" 'def add(a, b): return a + b @@ -502,10 +564,10 @@ def deprecated_multiply(a, b): def subtract(a, b): return a - b' -record_change "Add deprecation warning" >/dev/null 2>&1 +require_success "record deprecation warning" record_change "Add deprecation warning" # Insert from draft → dev -insert_out="$(insert_from_view "cleanup" "dev" 2>&1)" || true +if attempt_merge "delete versus edit" "cleanup" "dev" utils.py; then assert_file_exists "utils.py exists after insert" "utils.py" @@ -537,6 +599,7 @@ echo "" echo " utils.py after merge:" cat utils.py | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -568,14 +631,13 @@ export function start(): void { // End of service' -assert_success "add service.ts" atomic add service.ts -record_change "Initial service" >/dev/null 2>&1 +require_success "add service.ts" atomic add service.ts +require_success "record initial service" record_change "Initial service" # Create draft view -new_view "feature-service" --draft --parent dev >/dev/null 2>&1 || \ - new_view "feature-service" >/dev/null 2>&1 -insert_from_view "dev" "feature-service" >/dev/null 2>&1 || true -switch_view "feature-service" >/dev/null 2>&1 +require_success "create feature-service" new_view "feature-service" --draft --parent dev +require_success "insert dev into feature-service" insert_from_view "dev" "feature-service" +require_success "switch to feature-service" switch_view "feature-service" assert_current_view "on feature-service" "feature-service" # Draft change 1: modify header comment @@ -588,7 +650,7 @@ export function start(): void { // End of service' -record_change "Update service header to v2" >/dev/null 2>&1 +require_success "record service header" record_change "Update service header to v2" # Draft change 2: add a stop function overwrite_file "service.ts" '// Service v2 — improved @@ -604,7 +666,7 @@ export function stop(): void { // End of service' -record_change "Add stop function" >/dev/null 2>&1 +require_success "record stop function" record_change "Add stop function" # Draft change 3: modify stop function overwrite_file "service.ts" '// Service v2 — improved @@ -624,10 +686,10 @@ export function stop(graceful: boolean = true): void { // End of service' -record_change "Add graceful stop parameter" >/dev/null 2>&1 +require_success "record graceful stop" record_change "Add graceful stop parameter" # Switch to dev and make a non-overlapping change (modify footer) -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" overwrite_file "service.ts" '// Service v1 const SERVICE_NAME = "my-service"; @@ -638,10 +700,10 @@ export function start(): void { // Copyright 2024 — All rights reserved' -record_change "Update footer with copyright" >/dev/null 2>&1 +require_success "record copyright footer" record_change "Update footer with copyright" # Insert all draft changes → dev -insert_out="$(insert_from_view "feature-service" "dev" 2>&1)" || true +if attempt_merge "sequential draft changes" "feature-service" "dev" service.ts; then assert_file_exists "service.ts exists after insert" "service.ts" @@ -653,12 +715,13 @@ assert_occurrence_count "export start() once" "service.ts" "export function star # stop() should appear exactly once (from draft's final state, NOT # duplicated from intermediate states) -stop_count="$(grep -c 'export function stop' service.ts || echo 0)" -if [[ "$stop_count" -le 1 ]]; then - _pass "export stop() not duplicated ($stop_count occurrence(s))" +stop_count="$(grep -c 'export function stop' service.ts || true)" +if [[ "$stop_count" -eq 1 ]]; then + _pass "export stop() appears once ($stop_count occurrence(s))" else - _fail "export stop() not duplicated" "found $stop_count times (expected 0 or 1)" + _fail "export stop() appears once" "found $stop_count times (expected 1)" fi +assert_file_contains "final stop signature is present" "service.ts" 'stop(graceful: boolean = true)' # The header should be the draft's version (v2) assert_file_contains "draft header present (v2)" "service.ts" "v2" @@ -683,6 +746,7 @@ echo "" echo " service.ts after merge:" cat service.ts | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -716,14 +780,13 @@ database: host: localhost port: 5432' -assert_success "add config.yaml" atomic add config.yaml -record_change "Initial config.yaml" >/dev/null 2>&1 +require_success "add config.yaml" atomic add config.yaml +require_success "record initial config.yaml" record_change "Initial config.yaml" # Create draft -new_view "feature-yaml" --draft --parent dev >/dev/null 2>&1 || \ - new_view "feature-yaml" >/dev/null 2>&1 -insert_from_view "dev" "feature-yaml" >/dev/null 2>&1 || true -switch_view "feature-yaml" >/dev/null 2>&1 +require_success "create feature-yaml" new_view "feature-yaml" --draft --parent dev +require_success "insert dev into feature-yaml" insert_from_view "dev" "feature-yaml" +require_success "switch to feature-yaml" switch_view "feature-yaml" assert_current_view "on feature-yaml" "feature-yaml" # Draft: change app name @@ -739,10 +802,10 @@ database: host: localhost port: 5432' -record_change "Rename app to super-app" >/dev/null 2>&1 +require_success "record app name" record_change "Rename app to super-app" # Switch to dev: change database port -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" overwrite_file "config.yaml" 'app: name: myapp @@ -756,15 +819,15 @@ database: host: localhost port: 5433' -record_change "Change db port to 5433" >/dev/null 2>&1 +require_success "record database port" record_change "Change db port to 5433" # Switch to draft and insert FROM dev (reverse direction) -switch_view "feature-yaml" >/dev/null 2>&1 +require_success "switch to feature-yaml" switch_view "feature-yaml" -insert_out="$(insert_from_view "dev" "feature-yaml" 2>&1)" || true +if attempt_merge "reverse non-overlapping edits" "dev" "feature-yaml" config.yaml; then # Materialise the working copy after inserting into current view -atomic_out="$(atomic status 2>&1)" || true +require_success "read status after reverse insert" atomic status assert_file_exists "config.yaml exists after reverse insert" "config.yaml" @@ -778,6 +841,17 @@ assert_file_contains "draft edit present (super-app)" "config.yaml" "super-app" # Dev's change should be present assert_file_contains "dev edit present (5433)" "config.yaml" "5433" +assert_file_content "reverse merge preserves exact content" "config.yaml" 'app: + name: super-app + version: 1.0.0 + +server: + host: localhost + port: 3000 + +database: + host: localhost + port: 5433' assert_max_lines "config.yaml reasonable size" "config.yaml" 18 @@ -785,6 +859,7 @@ echo "" echo " config.yaml after reverse insert:" cat config.yaml | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -807,14 +882,13 @@ create_file "index.html" ' ' -assert_success "add index.html" atomic add index.html -record_change "Initial HTML" >/dev/null 2>&1 +require_success "add index.html" atomic add index.html +require_success "record initial HTML" record_change "Initial HTML" # Create draft, modify, record -new_view "feature-html" --draft --parent dev >/dev/null 2>&1 || \ - new_view "feature-html" >/dev/null 2>&1 -insert_from_view "dev" "feature-html" >/dev/null 2>&1 || true -switch_view "feature-html" >/dev/null 2>&1 +require_success "create feature-html" new_view "feature-html" --draft --parent dev +require_success "insert dev into feature-html" insert_from_view "dev" "feature-html" +require_success "switch to feature-html" switch_view "feature-html" overwrite_file "index.html" ' @@ -825,10 +899,10 @@ overwrite_file "index.html" ' ' -record_change "Update title and paragraph on draft" >/dev/null 2>&1 +require_success "record draft HTML" record_change "Update title and paragraph on draft" # Dev: different edit -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" overwrite_file "index.html" ' @@ -839,10 +913,10 @@ overwrite_file "index.html" ' ' -record_change "Update heading on dev" >/dev/null 2>&1 +require_success "record dev heading" record_change "Update heading on dev" # Insert draft → dev -insert_from_view "feature-html" "dev" >/dev/null 2>&1 || true +if attempt_merge "post-merge record" "feature-html" "dev" index.html; then # Now make another edit on dev AFTER the merge overwrite_file "index.html" ' @@ -855,7 +929,8 @@ overwrite_file "index.html" ' ' # This record should work cleanly — no duplication from the merge -record_out="$(record_change "Post-merge edit" 2>&1)" || true +require_success "record post-merge edit" record_change "Post-merge edit" +assert_clean "clean after post-merge record" # Verify the file has the post-merge content assert_file_content "index.html has final content" "index.html" ' @@ -877,6 +952,7 @@ echo "" echo " index.html after post-merge record:" cat index.html | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -903,31 +979,30 @@ create_file "config.ts" 'export const APP_NAME = "atomic"; export const TIMEOUT_MS = 5000; export const MAX_RETRIES = 3;' -assert_success "add config.ts" atomic add config.ts -record_change "Initial timeout config" >/dev/null 2>&1 +require_success "add config.ts" atomic add config.ts +require_success "record initial timeout config" record_change "Initial timeout config" -new_view "rename-timeout" --draft --parent dev >/dev/null 2>&1 || \ - new_view "rename-timeout" >/dev/null 2>&1 -insert_from_view "dev" "rename-timeout" >/dev/null 2>&1 || true -switch_view "rename-timeout" >/dev/null 2>&1 +require_success "create rename-timeout" new_view "rename-timeout" --draft --parent dev +require_success "insert dev into rename-timeout" insert_from_view "dev" "rename-timeout" +require_success "switch to rename-timeout" switch_view "rename-timeout" # Agent A renames the identifier overwrite_file "config.ts" 'export const APP_NAME = "atomic"; export const TIMEOUT_SEC = 5000; export const MAX_RETRIES = 3;' -record_change "Rename TIMEOUT_MS → TIMEOUT_SEC" >/dev/null 2>&1 +require_success "record timeout rename" record_change "Rename TIMEOUT_MS → TIMEOUT_SEC" -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" # Agent B changes the value (different token, same line) overwrite_file "config.ts" 'export const APP_NAME = "atomic"; export const TIMEOUT_MS = 10000; export const MAX_RETRIES = 3;' -record_change "Tune timeout to 10000" >/dev/null 2>&1 +require_success "record timeout value" record_change "Tune timeout to 10000" -insert_out="$(insert_from_view "rename-timeout" "dev" 2>&1)" || true +if attempt_merge "disjoint token edits" "rename-timeout" "dev" config.ts; then assert_file_exists "config.ts exists after token-merge insert" "config.ts" @@ -946,6 +1021,7 @@ echo "" echo " config.ts after token-disjoint merge:" cat config.ts | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -969,22 +1045,21 @@ init_repo create_file "config.ts" 'export const TIMEOUT_MS = 5000;' -assert_success "add config.ts" atomic add config.ts -record_change "Initial timeout" >/dev/null 2>&1 +require_success "add config.ts" atomic add config.ts +require_success "record initial timeout" record_change "Initial timeout" -new_view "bump-A" --draft --parent dev >/dev/null 2>&1 || \ - new_view "bump-A" >/dev/null 2>&1 -insert_from_view "dev" "bump-A" >/dev/null 2>&1 || true -switch_view "bump-A" >/dev/null 2>&1 +require_success "create bump-A" new_view "bump-A" --draft --parent dev +require_success "insert dev into bump-A" insert_from_view "dev" "bump-A" +require_success "switch to bump-A" switch_view "bump-A" overwrite_file "config.ts" 'export const TIMEOUT_MS = 10000;' -record_change "Bump to 10000" >/dev/null 2>&1 +require_success "record timeout 10000" record_change "Bump to 10000" -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" overwrite_file "config.ts" 'export const TIMEOUT_MS = 30000;' -record_change "Bump to 30000" >/dev/null 2>&1 +require_success "record timeout 30000" record_change "Bump to 30000" -insert_out="$(insert_from_view "bump-A" "dev" 2>&1)" || true +if attempt_merge "overlapping token edits" "bump-A" "dev" config.ts; then assert_file_exists "config.ts exists after overlap insert" "config.ts" @@ -997,6 +1072,7 @@ echo "" echo " config.ts after same-token conflict:" cat config.ts | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -1026,31 +1102,30 @@ create_file "process.ts" 'export function process(data) { return transform(data); }' -assert_success "add process.ts" atomic add process.ts -record_change "Initial process function" >/dev/null 2>&1 +require_success "add process.ts" atomic add process.ts +require_success "record initial process function" record_change "Initial process function" -new_view "add-param" --draft --parent dev >/dev/null 2>&1 || \ - new_view "add-param" >/dev/null 2>&1 -insert_from_view "dev" "add-param" >/dev/null 2>&1 || true -switch_view "add-param" >/dev/null 2>&1 +require_success "create add-param" new_view "add-param" --draft --parent dev +require_success "insert dev into add-param" insert_from_view "dev" "add-param" +require_success "switch to add-param" switch_view "add-param" # Agent A: add an options parameter overwrite_file "process.ts" 'export function process(data, options) { return transform(data); }' -record_change "Add options parameter" >/dev/null 2>&1 +require_success "record options parameter" record_change "Add options parameter" -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" # Agent B: add a return type overwrite_file "process.ts" 'export function process(data): Result { return transform(data); }' -record_change "Add Result return type" >/dev/null 2>&1 +require_success "record return type" record_change "Add Result return type" -insert_out="$(insert_from_view "add-param" "dev" 2>&1)" || true +if attempt_merge "structural token edits" "add-param" "dev" process.ts; then assert_file_exists "process.ts exists after structural merge" "process.ts" @@ -1080,6 +1155,7 @@ echo "" echo " process.ts after structural merge:" cat process.ts | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ @@ -1117,14 +1193,13 @@ create_file "src/lib.ts" 'export function oldFn(x) { export const VERSION = "1.0";' -assert_success "add src/api.ts" atomic add src/api.ts -assert_success "add src/lib.ts" atomic add src/lib.ts -record_change "Initial library" >/dev/null 2>&1 +require_success "add src/api.ts" atomic add src/api.ts +require_success "add src/lib.ts" atomic add src/lib.ts +require_success "record initial library" record_change "Initial library" -new_view "rename-fn" --draft --parent dev >/dev/null 2>&1 || \ - new_view "rename-fn" >/dev/null 2>&1 -insert_from_view "dev" "rename-fn" >/dev/null 2>&1 || true -switch_view "rename-fn" >/dev/null 2>&1 +require_success "create rename-fn" new_view "rename-fn" --draft --parent dev +require_success "insert dev into rename-fn" insert_from_view "dev" "rename-fn" +require_success "switch to rename-fn" switch_view "rename-fn" # Agent A: rename oldFn → newFn across both files. overwrite_file "src/api.ts" 'import { newFn } from "./lib"; @@ -1139,9 +1214,9 @@ overwrite_file "src/lib.ts" 'export function newFn(x) { export const VERSION = "1.0";' -record_change "Rename oldFn → newFn" >/dev/null 2>&1 +require_success "record function rename" record_change "Rename oldFn → newFn" -switch_view "dev" >/dev/null 2>&1 +require_success "switch to dev" switch_view "dev" # Agent B: change the function body (a DIFFERENT line than agent A # touched in lib.ts). api.ts is left alone by agent B. @@ -1151,9 +1226,9 @@ overwrite_file "src/lib.ts" 'export function oldFn(x) { export const VERSION = "1.0";' -record_change "Tighten oldFn body" >/dev/null 2>&1 +require_success "record function body" record_change "Tighten oldFn body" -insert_out="$(insert_from_view "rename-fn" "dev" 2>&1)" || true +if attempt_merge "multi-file refactor" "rename-fn" "dev" src/api.ts src/lib.ts; then assert_file_exists "src/api.ts exists after refactor" "src/api.ts" assert_file_exists "src/lib.ts exists after refactor" "src/lib.ts" @@ -1170,7 +1245,7 @@ assert_occurrence_count "api.ts has one handle()" "src/api.ts" "function handle" # Both edits must be present in their respective lines. assert_file_contains "lib.ts signature renamed" "src/lib.ts" 'export function newFn' assert_file_contains "lib.ts body change present" "src/lib.ts" 'x * 3 + 1' -assert_file_not_contains "lib.ts old body gone" "src/lib.ts" 'return x \* 2;' +assert_file_not_contains "lib.ts old body gone" "src/lib.ts" 'return x * 2;' assert_occurrence_count "lib.ts has one function def" "src/lib.ts" "export function" 1 assert_file_contains "lib.ts VERSION untouched" "src/lib.ts" 'VERSION = "1.0"' @@ -1181,6 +1256,7 @@ echo "" echo " src/lib.ts after multi-file refactor:" cat src/lib.ts | sed 's/^/ /' echo "" +fi # ═══════════════════════════════════════════════════════════════════════════ diff --git a/tests/harness/18_git_import_hot_file.sh b/tests/harness/18_git_import_hot_file.sh index 12cc3504..9fc834e2 100644 --- a/tests/harness/18_git_import_hot_file.sh +++ b/tests/harness/18_git_import_hot_file.sh @@ -14,24 +14,30 @@ HOT_FILE_LINES="${HOT_FILE_LINES:-300}" HOT_FILE_IMPORT_MAX_SECONDS="${HOT_FILE_IMPORT_MAX_SECONDS:-30}" HOT_FILE_MODE="${HOT_FILE_MODE:-linear}" HOT_FILE_BRANCHES="${HOT_FILE_BRANCHES:-8}" -HOT_FILE_ALLOWED_SKIPS="${HOT_FILE_ALLOWED_SKIPS:-3}" HOT_FILE_PATH="terraform/context_apply_test.go" count_imported_git_changes() { - local count=0 - local hash + local count=0 hash log details + if ! log="$(atomic log --format short --no-color --full-hash 2>&1)"; then + printf 'atomic log failed: %s\n' "$log" >&2 + return 1 + fi while IFS= read -r hash; do [[ -z "$hash" ]] && continue - if atomic change "$hash" 2>/dev/null | grep -q 'Commit:'; then + if ! details="$(atomic change "$hash" --full-hash 2>&1)"; then + printf 'atomic change %s failed: %s\n' "$hash" "$details" >&2 + return 1 + fi + if printf '%s\n' "$details" | grep -qF 'Git source revision:'; then count=$((count + 1)) fi - done < <(atomic log --format short --no-color --full-hash 2>/dev/null | awk '/^[A-Z2-7]{20,}/ { print $1 }') + done <<< "$(printf '%s\n' "$log" | awk '$1 ~ /^[A-Z2-7]+$/ && length($1) == 52 { print $1 }')" echo "$count" } git_first_parent_commit_count() { - git rev-list --first-parent --count HEAD 2>/dev/null || echo "0" + git rev-list --first-parent --count HEAD } write_hot_file() { @@ -128,13 +134,15 @@ case "$HOT_FILE_MODE" in ;; esac -expected_commits="$(git_first_parent_commit_count)" +require_capture "count first-parent Git history" git_first_parent_commit_count +expected_commits="$COMMAND_OUTPUT" if [[ "$HOT_FILE_MODE" == "branchy" ]]; then - expected_commits="$(git rev-list --all --count 2>/dev/null || echo "$expected_commits")" + require_capture "count all Git history" git rev-list --all --count + expected_commits="$COMMAND_OUTPUT" fi echo " Synthetic history: mode=${HOT_FILE_MODE}, commits=${expected_commits}, lines=${HOT_FILE_LINES}, path=${HOT_FILE_PATH}" -atomic init >/dev/null 2>&1 +require_success "initialize Atomic repository" atomic init start_time=$(date +%s) import_args=(git import) @@ -142,31 +150,26 @@ if [[ "$HOT_FILE_MODE" == "branchy" ]]; then import_args+=(--all) fi -import_out="$(atomic "${import_args[@]}" 2>&1)" || { - _fail "hot-file import succeeds" "$import_out" - print_summary - exit 1 -} +require_capture "hot-file import succeeds" atomic "${import_args[@]}" end_time=$(date +%s) duration=$((end_time - start_time)) -_pass "hot-file import succeeds" echo " Import took ${duration}s" -actual="$(count_imported_git_changes)" +require_capture "count imported Git changes" count_imported_git_changes +actual="$COMMAND_OUTPUT" if [[ "$HOT_FILE_MODE" == "branchy" ]]; then if [[ "$actual" -ge 2 ]]; then _pass "branchy import produced git changes ($actual imported)" else _fail "branchy import produced git changes" "expected at least 2 imported git changes, got $actual" fi + _skip "verify all branch changes" "branch-wide unique change count is not checked" elif [[ "$actual" -eq "$expected_commits" ]]; then _pass "imported git change count matches ($actual vs $expected_commits)" -elif [[ "$actual" -ge $((expected_commits - HOT_FILE_ALLOWED_SKIPS)) ]]; then - _pass "imported git change count within skip tolerance ($actual vs $expected_commits)" else _fail "imported git change count matches" \ - "expected $expected_commits imported git changes, got $actual (allowed skips: $HOT_FILE_ALLOWED_SKIPS)" + "expected $expected_commits imported git changes, got $actual" fi if [[ "$duration" -le "$HOT_FILE_IMPORT_MAX_SECONDS" ]]; then @@ -176,13 +179,8 @@ else "took ${duration}s; likely hot-file assembly traversal regression" fi -status_out="$(atomic status --short 2>/dev/null || true)" -hot_file_status="$(echo "$status_out" | grep -F "$HOT_FILE_PATH" || true)" -if [[ -z "$hot_file_status" ]]; then - _pass "hot file clean after import" -else - _fail "hot file clean after import" "$hot_file_status" -fi +assert_status_no_entry "hot file clean after import" "$HOT_FILE_PATH" +assert_success "hot file still matches Git HEAD" git diff --exit-code HEAD -- "$HOT_FILE_PATH" expected_marker="hot-$(printf '%04d' "$HOT_FILE_COMMITS")" if [[ "$HOT_FILE_MODE" == "branchy" ]]; then diff --git a/tests/harness/19_unrecord.sh b/tests/harness/19_unrecord.sh index 434c56f0..b2284192 100755 --- a/tests/harness/19_unrecord.sh +++ b/tests/harness/19_unrecord.sh @@ -25,77 +25,35 @@ make_temp_repo "unrecord-basic" init_repo create_file "greeting.txt" "Hello, World!" -assert_success "add greeting.txt" atomic add greeting.txt -record_change "Add greeting.txt" >/dev/null 2>&1 || true +require_success "add greeting.txt" atomic add greeting.txt +require_success "record greeting.txt" record_change "Add greeting.txt" # Verify clean after first record -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*greeting\.txt"; then - _fail "greeting.txt is clean after initial record" "still dirty: $(echo "$out" | head -5)" -else - _pass "greeting.txt is clean after initial record" -fi +assert_status_no_entry "greeting.txt is clean after initial record" "greeting.txt" # Modify and record the modification overwrite_file "greeting.txt" "Hello, Modified World!" assert_status_flag "greeting.txt shows M after modify" "M" "greeting.txt" -record_change "Modify greeting.txt" >/dev/null 2>&1 || true +require_success "record greeting modification" record_change "Modify greeting.txt" -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*greeting\.txt"; then - _fail "greeting.txt is clean after modification record" "still dirty: $(echo "$out" | head -5)" -else - _pass "greeting.txt is clean after modification record" -fi +assert_status_no_entry "greeting.txt is clean after modification record" "greeting.txt" # Unrecord the modification -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord last succeeds" -else - _pass "unrecord last completes without crash" -fi +require_success "unrecord last succeeds" unrecord_last # After unrecord, file should show as modified (disk has new content, graph # has the old content from the first record) -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MA].*greeting\.txt"; then - _pass "greeting.txt shows modified or added after unrecord" -else - _fail "greeting.txt shows modified after unrecord" \ - "expected M or A flag. Status: $(echo "$out" | head -5)" -fi +assert_status_flag "greeting.txt shows modified after unrecord" "M" "greeting.txt" # File should NOT show as untracked — it was recorded in the first change -out="$(get_status_short)" -if echo "$out" | grep -qE "^\?.*greeting\.txt"; then - _fail "greeting.txt is not untracked after unrecord" \ - "file should be modified, not untracked" -else - _pass "greeting.txt is not untracked after unrecord" -fi +assert_file_content "greeting bytes preserved after unrecord" "greeting.txt" "Hello, Modified World!" # Re-record should succeed -rec_out="$(record_change "Re-record greeting.txt" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "re-record after unrecord succeeds" -else - if atomic status >/dev/null 2>&1; then - _pass "re-record after unrecord completes (status still works)" - else - _fail "re-record after unrecord succeeds" \ - "output: $(echo "$rec_out" | head -5)" - fi -fi +require_success "re-record after unrecord succeeds" record_change "Re-record greeting.txt" # Should be clean again -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*greeting\.txt"; then - _fail "greeting.txt is clean after re-record" "still dirty: $(echo "$out" | head -5)" -else - _pass "greeting.txt is clean after re-record" -fi +assert_status_no_entry "greeting.txt is clean after re-record" "greeting.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Unrecord: File-add change" @@ -110,33 +68,17 @@ make_temp_repo "unrecord-file-add" init_repo create_file "newfile.txt" "I am brand new" -assert_success "add newfile.txt" atomic add newfile.txt +require_success "add newfile.txt" atomic add newfile.txt assert_status_flag "newfile.txt shows A after add" "A" "newfile.txt" -rec_out="$(record_change "Add newfile.txt" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "record newfile.txt succeeds" -else - _pass "record newfile.txt completes" -fi +require_success "record newfile.txt succeeds" record_change "Add newfile.txt" # Unrecord the file-add change -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord file-add change succeeds" -else - _pass "unrecord file-add change completes without crash" -fi +require_success "unrecord file-add change succeeds" unrecord_last # File should appear as added (A) or untracked (?) — either is acceptable # as long as the system is internally consistent -out="$(get_status_short)" -if echo "$out" | grep -qE "^[A?].*newfile\.txt"; then - _pass "newfile.txt shows as added or untracked after unrecord" -else - _fail "newfile.txt shows as added or untracked after unrecord" \ - "expected A or ? flag. Status: $(echo "$out" | head -5)" -fi +assert_status_flag "newfile.txt is untracked after unrecord" "?" "newfile.txt" # The file should still exist on disk assert_file_exists "newfile.txt still exists on disk after unrecord" "newfile.txt" @@ -145,9 +87,8 @@ assert_file_content "newfile.txt content preserved after unrecord" \ # If status says untracked (?), add should accept it # If status says added (A), add should either succeed or say already tracked -add_out="$(atomic add newfile.txt 2>&1)" || true -# Should not error fatally -_pass "add after unrecord does not crash" +require_success "add after unrecord succeeds" atomic add newfile.txt +assert_status_flag "newfile.txt is staged after re-add" "A" "newfile.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Unrecord: Full modify cycle" @@ -160,66 +101,32 @@ make_temp_repo "unrecord-full-cycle" init_repo create_file "cycle.txt" "version one" -assert_success "add cycle.txt" atomic add cycle.txt -record_change "Add cycle.txt v1" >/dev/null 2>&1 || true +require_success "add cycle.txt" atomic add cycle.txt +require_success "record cycle.txt v1" record_change "Add cycle.txt v1" # Modify file overwrite_file "cycle.txt" "version two" assert_status_flag "cycle.txt is modified after overwrite" "M" "cycle.txt" # Record the modification -record_change "Modify cycle.txt to v2" >/dev/null 2>&1 || true +require_success "record cycle.txt v2" record_change "Modify cycle.txt to v2" -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*cycle\.txt"; then - _fail "cycle.txt is clean after recording v2" "still dirty: $(echo "$out" | head -5)" -else - _pass "cycle.txt is clean after recording v2" -fi +assert_status_no_entry "cycle.txt is clean after recording v2" "cycle.txt" # Unrecord the modification -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord v2 modification succeeds" -else - _pass "unrecord v2 modification completes" -fi +require_success "unrecord v2 modification succeeds" unrecord_last # Status should show modified — disk has "version two", graph has "version one" -out="$(get_status_short)" -if echo "$out" | grep -qE "^M.*cycle\.txt"; then - _pass "cycle.txt shows modified after unrecording v2" -elif echo "$out" | grep -qE "^[A].*cycle\.txt"; then - # Some implementations might show added if the modify was complex - _pass "cycle.txt shows added after unrecording v2 (acceptable)" -else - _fail "cycle.txt shows modified after unrecording v2" \ - "expected M flag. Status: $(echo "$out" | head -5)" -fi +assert_status_flag "cycle.txt shows modified after unrecording v2" "M" "cycle.txt" # Disk content should still have the new version assert_file_content "disk has version two after unrecord" "cycle.txt" "version two" # Re-record should work -rec_out="$(record_change "Re-record cycle.txt v2" 2>&1)" || true -if echo "$rec_out" | grep -qiE "hash|recorded|created|change"; then - _pass "re-record cycle.txt succeeds" -else - if atomic status >/dev/null 2>&1; then - _pass "re-record cycle.txt completes (status still works)" - else - _fail "re-record cycle.txt succeeds" \ - "output: $(echo "$rec_out" | head -5)" - fi -fi +require_success "re-record cycle.txt succeeds" record_change "Re-record cycle.txt v2" # Clean after re-record -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*cycle\.txt"; then - _fail "cycle.txt is clean after re-record" "still dirty: $(echo "$out" | head -5)" -else - _pass "cycle.txt is clean after re-record" -fi +assert_status_no_entry "cycle.txt is clean after re-record" "cycle.txt" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Unrecord: Multiple files" @@ -235,19 +142,14 @@ create_file "alpha.txt" "alpha content" create_file "beta.txt" "beta content" create_file "gamma.txt" "gamma content" -assert_success "add all three files" atomic add alpha.txt beta.txt gamma.txt +require_success "add all three files" atomic add alpha.txt beta.txt gamma.txt # Record all three in a single change -record_change "Add alpha, beta, gamma" >/dev/null 2>&1 || true +require_success "record three files" record_change "Add alpha, beta, gamma" # Verify all clean -out="$(get_status_short)" for f in alpha.txt beta.txt gamma.txt; do - if echo "$out" | grep -qE "^[MADU?].*${f}"; then - _fail "$f is clean after record" "still dirty" - else - _pass "$f is clean after record" - fi + assert_status_no_entry "$f is clean after record" "$f" done # Modify all three and record @@ -255,35 +157,19 @@ overwrite_file "alpha.txt" "alpha modified" overwrite_file "beta.txt" "beta modified" overwrite_file "gamma.txt" "gamma modified" -record_change "Modify all three files" >/dev/null 2>&1 || true +require_success "record all three modifications" record_change "Modify all three files" # Verify clean again -out="$(get_status_short)" for f in alpha.txt beta.txt gamma.txt; do - if echo "$out" | grep -qE "^[MADU?].*${f}"; then - _fail "$f is clean after modification record" "still dirty" - else - _pass "$f is clean after modification record" - fi + assert_status_no_entry "$f is clean after modification record" "$f" done # Unrecord the modification change -unrec_out="$(unrecord_last)" -if echo "$unrec_out" | grep -qiE "unrecord|removed|hash"; then - _pass "unrecord multi-file change succeeds" -else - _pass "unrecord multi-file change completes" -fi +require_success "unrecord multi-file change succeeds" unrecord_last # All three should show as modified (or added) after unrecord -out="$(get_status_short)" for f in alpha.txt beta.txt gamma.txt; do - if echo "$out" | grep -qE "^[MA].*${f}"; then - _pass "$f shows modified or added after unrecord" - else - _fail "$f shows modified or added after unrecord" \ - "Status: $(echo "$out" | grep "$f" || echo '(not found)')" - fi + assert_status_flag "$f shows modified after unrecord" "M" "$f" done # All files should still exist on disk with modified content @@ -303,9 +189,19 @@ make_temp_repo "unrecord-preserve-change" init_repo create_file "preserve.txt" "will be preserved" -assert_success "add preserve.txt" atomic add preserve.txt - -rec_out="$(record_change "Add preserve.txt" 2>&1)" || true +require_success "add preserve.txt" atomic add preserve.txt + +require_success "record preserve.txt" record_change "Add preserve.txt" +require_capture "read recorded change hash" atomic log --format json --count 1 +if ! recorded_hash="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; entries=json.load(sys.stdin); assert len(entries)==1; print(entries[0]["hash"])')"; then + _fail "extract recorded change hash" "log: $COMMAND_OUTPUT" + print_summary || true + exit 1 +fi +change_file=".atomic/changes/${recorded_hash:0:2}/${recorded_hash}.change" +assert_file_exists "recorded change object exists" "$change_file" +require_capture "hash recorded change object before unrecord" shasum -a 256 "$change_file" +object_digest_before="$COMMAND_OUTPUT" # Count change files before unrecord change_count_before="$(find .atomic/changes/ -type f 2>/dev/null | wc -l | tr -d '[:space:]')" @@ -320,11 +216,11 @@ fi change_files_before="$(find .atomic/changes/ -type f 2>/dev/null | sort)" # Unrecord -unrecord_last >/dev/null 2>&1 +require_success "unrecord preserve.txt" unrecord_last # Count change files after unrecord — should still be the same change_count_after="$(find .atomic/changes/ -type f 2>/dev/null | wc -l | tr -d '[:space:]')" -if [[ "$change_count_after" -ge "$change_count_before" ]]; then +if [[ "$change_count_after" -eq "$change_count_before" ]]; then _pass "change files preserved after unrecord (count: $change_count_after)" else _fail "change files preserved after unrecord" \ @@ -336,7 +232,13 @@ change_files_after="$(find .atomic/changes/ -type f 2>/dev/null | sort)" if [[ "$change_files_before" == "$change_files_after" ]]; then _pass "same change files present before and after unrecord" else - _pass "change files still present after unrecord (set may differ)" + _fail "same change files present before and after unrecord" "before: $change_files_before; after: $change_files_after" +fi +require_capture "hash recorded change object after unrecord" shasum -a 256 "$change_file" +if [[ "$COMMAND_OUTPUT" == "$object_digest_before" ]]; then + _pass "unrecord preserves recorded object bytes" +else + _fail "unrecord preserves recorded object bytes" "before: $object_digest_before; after: $COMMAND_OUTPUT" fi # ═══════════════════════════════════════════════════════════════════════════ @@ -355,69 +257,41 @@ init_repo # -- Test 1: Unrecord a file-add, then check status/add consistency -- create_file "cons_new.txt" "new file for consistency" -assert_success "add cons_new.txt" atomic add cons_new.txt -record_change "Add cons_new.txt" >/dev/null 2>&1 || true +require_success "add cons_new.txt" atomic add cons_new.txt +require_success "record cons_new.txt" record_change "Add cons_new.txt" # Unrecord -unrecord_last >/dev/null 2>&1 - -out="$(get_status_short)" -if echo "$out" | grep -qE "^\?.*cons_new\.txt"; then - # Status says untracked → add should accept it - add_out="$(atomic add cons_new.txt 2>&1)" || true - if echo "$add_out" | grep -qiE "error|fatal|cannot"; then - _fail "untracked file accepted by add" \ - "status says untracked but add refused: $add_out" - else - _pass "untracked file accepted by add (status/add consistent)" - fi -elif echo "$out" | grep -qE "^[AM].*cons_new\.txt"; then - # Status says added or modified → file is tracked, add should say so - add_out="$(atomic add cons_new.txt 2>&1)" || true - # Either succeeds silently (idempotent) or says already tracked — both OK - _pass "tracked file handled by add (status/add consistent)" -else - # No flag at all — file might be clean (shouldn't happen after unrecord - # of the only change, but handle gracefully) - _pass "status shows no flag for cons_new.txt (may be implementation-specific)" -fi +require_success "unrecord cons_new.txt" unrecord_last + +assert_status_flag "unrecorded addition is untracked" "?" "cons_new.txt" +require_success "re-add untracked file" atomic add cons_new.txt +assert_status_flag "re-added file is staged" "A" "cons_new.txt" +assert_file_content "re-add preserves file content" "cons_new.txt" "new file for consistency" # -- Test 2: Unrecord a modification, then check status/add consistency -- make_temp_repo "unrecord-consistency-modify" init_repo create_file "cons_mod.txt" "original content" -assert_success "add cons_mod.txt" atomic add cons_mod.txt -record_change "Add cons_mod.txt" >/dev/null 2>&1 || true +require_success "add cons_mod.txt" atomic add cons_mod.txt +require_success "record cons_mod.txt" record_change "Add cons_mod.txt" overwrite_file "cons_mod.txt" "modified content" -record_change "Modify cons_mod.txt" >/dev/null 2>&1 || true +require_success "record cons_mod modification" record_change "Modify cons_mod.txt" # Unrecord the modification -unrecord_last >/dev/null 2>&1 - -out="$(get_status_short)" -if echo "$out" | grep -qE "^M.*cons_mod\.txt"; then - # Status says modified → add should say already tracked - add_out="$(atomic add cons_mod.txt 2>&1)" || true - if echo "$add_out" | grep -qiE "error|fatal|cannot"; then - _fail "modified file handled by add" \ - "status says modified but add errored: $add_out" - else - _pass "modified file handled by add (status/add consistent)" - fi -elif echo "$out" | grep -qE "^[A?].*cons_mod\.txt"; then - # Added or untracked — add should accept - add_out="$(atomic add cons_mod.txt 2>&1)" || true - _pass "file handled by add after unrecord (status/add consistent)" -else - # Clean or no entry — acceptable after unrecord if impl re-applies - _pass "cons_mod.txt status after unrecord is consistent" -fi +require_success "unrecord cons_mod modification" unrecord_last + +assert_status_flag "unrecorded modification remains tracked and modified" "M" "cons_mod.txt" +require_success "add handles tracked modified file" atomic add cons_mod.txt +assert_status_flag "modified status remains after add" "M" "cons_mod.txt" +assert_file_content "tracked add preserves modified bytes" "cons_mod.txt" "modified content" # -- Test 3: Verify that status is stable (calling it twice yields same result) -- -out1="$(get_status_short)" -out2="$(get_status_short)" +require_capture "read first status" get_status_short +out1="$COMMAND_OUTPUT" +require_capture "read second status" get_status_short +out2="$COMMAND_OUTPUT" if [[ "$out1" == "$out2" ]]; then _pass "status is idempotent after unrecord" else diff --git a/tests/harness/20_sandbox.sh b/tests/harness/20_sandbox.sh index 16a9aa30..9b8771aa 100755 --- a/tests/harness/20_sandbox.sh +++ b/tests/harness/20_sandbox.sh @@ -36,8 +36,8 @@ init_repo create_file "src/main.rs" "fn main() {}" create_file "Cargo.toml" "[package]" -assert_success "add tracked files" atomic add src/main.rs Cargo.toml -record_change "init" >/dev/null 2>&1 || true +require_success "add tracked files" atomic add src/main.rs Cargo.toml +require_success "record initial files" record_change "init" # Untracked build artifact (simulates npm install output) mkdir -p node_modules/lodash @@ -48,12 +48,19 @@ create_file "node_modules/lodash/index.js" "module.exports = {}" SBROOT="$(mktemp -d "${TMPDIR:-/tmp}/atomic-sandboxes-XXXXXX")" _HARNESS_TMPDIRS+=("$SBROOT") SB="$SBROOT/agent-1" -assert_success "sandbox create" atomic sandbox create agent-1 --dest "$SB" +require_success "sandbox create" atomic sandbox create agent-1 --dest "$SB" assert_file_exists "tracked file cloned into sandbox" "$SB/src/main.rs" assert_file_exists "Cargo.toml cloned into sandbox" "$SB/Cargo.toml" assert_file_exists "untracked artifact cloned into sandbox" "$SB/node_modules/lodash/index.js" -assert_dir_not_exists "graph .atomic NOT cloned into sandbox" "$SB/.atomic" +assert_file_exists "sandbox has its own working-copy identity" "$SB/.atomic/working_copy_id" +assert_file_not_exists "graph database NOT cloned into sandbox" "$SB/.atomic/atomic.redb" +assert_dir_not_exists "change store NOT cloned into sandbox" "$SB/.atomic/changes" +if [[ "$(cat "$SB/.atomic/working_copy_id")" != "$(cat "$CANON/.atomic/working_copy_id")" ]]; then + _pass "sandbox and canonical working-copy identities differ" +else + _fail "sandbox and canonical working-copy identities differ" "identities are equal" +fi assert_file_exists "sandbox pointer written" "$SB/.atomic-sandbox" # ═══════════════════════════════════════════════════════════════════════════ @@ -77,7 +84,7 @@ begin_section "Sandbox: record inside lands in the canonical graph" # Edit a file inside the sandbox and record from there. overwrite_file "$SB/src/main.rs" "fn main() { println!(\"from sandbox\"); }" -RECORD_OUT="$(cd "$SB" && "$ATOMIC_BIN" record -m "edit from sandbox" 2>&1)" || true +require_success "record sandbox edit" bash -c 'cd "$1" && "$2" record -m "edit from sandbox"' _ "$SB" "$ATOMIC_BIN" # The change must be visible in the canonical repo's log. assert_output_contains \ @@ -97,7 +104,7 @@ begin_section "Sandbox: two sandboxes have independent artifacts" # ═══════════════════════════════════════════════════════════════════════════ SB2="$SBROOT/agent-2" -assert_success "second sandbox create" atomic sandbox create agent-2 --dest "$SB2" +require_success "second sandbox create" atomic sandbox create agent-2 --dest "$SB2" # Agent 1 installs a dep that agent 2 does not. mkdir -p "$SB/node_modules/only-in-1" @@ -114,18 +121,18 @@ begin_section "Sandbox: --from creates a per-agent draft view" # ═══════════════════════════════════════════════════════════════════════════ SB3="$SBROOT/agent-3" -assert_success "sandbox create --from dev" \ +require_success "sandbox create --from dev" \ bash -c "cd '$CANON' && '$ATOMIC_BIN' sandbox create agent-3 --dest '$SB3' --from dev" # The new draft view exists in the canonical repo. assert_output_contains \ "draft view 'agent-3' created" \ "agent-3" \ - bash -c "cd '$CANON' && '$ATOMIC_BIN' view list" + bash -c "cd '$CANON' && '$ATOMIC_BIN' view list --all" # Recording in the sandbox lands in the agent-3 draft, not in dev. overwrite_file "$SB3/src/main.rs" "fn main() { println!(\"draft work\"); }" -(cd "$SB3" && "$ATOMIC_BIN" record -m "work in draft" >/dev/null 2>&1) || true +require_success "record draft sandbox edit" bash -c 'cd "$1" && "$2" record -m "work in draft"' _ "$SB3" "$ATOMIC_BIN" assert_output_contains \ "draft change visible on agent-3 view" \ @@ -137,7 +144,7 @@ begin_section "Sandbox: seal produces a valid OCI image layout" # ═══════════════════════════════════════════════════════════════════════════ SEAL_OUT="$SBROOT/seal-dev" -assert_success "seal dev view" \ +require_success "seal dev view" \ bash -c "cd '$CANON' && '$ATOMIC_BIN' sandbox seal dev -o '$SEAL_OUT' --entrypoint /app/run --env PORT=8080" assert_file_exists "oci-layout written" "$SEAL_OUT/oci-layout" @@ -147,20 +154,30 @@ assert_output_contains "oci-layout has version" "imageLayoutVersion" cat "$SEAL_ # Every blob filename must equal the sha256 of its contents (content-addressed). BLOB_OK=1 +BLOB_COUNT=0 for blob in "$SEAL_OUT"/blobs/sha256/*; do + if [[ ! -f "$blob" ]]; then + BLOB_OK=0 + break + fi + BLOB_COUNT=$((BLOB_COUNT + 1)) name="$(basename "$blob")" actual="$(shasum -a 256 "$blob" | awk '{print $1}')" [ "$name" = "$actual" ] || BLOB_OK=0 done -if [ "$BLOB_OK" = "1" ]; then - _pass "every blob is content-addressed (filename == sha256)" +if [ "$BLOB_OK" = "1" ] && [ "$BLOB_COUNT" -gt 0 ]; then + _pass "every blob is content-addressed (filename == sha256; $BLOB_COUNT blobs)" else _fail "every blob is content-addressed (filename == sha256)" "a blob filename did not match its sha256" fi # The manifest the index points to must reference exactly one layer (flattened). -INDEX_MANIFEST="$(grep -o 'sha256:[0-9a-f]\{64\}' "$SEAL_OUT/index.json" | head -1 | cut -d: -f2)" +require_capture "read sealed manifest digest" python3 -c \ + 'import json,re,sys; a=json.load(open(sys.argv[1]))["manifests"]; assert len(a)==1; d=a[0]["digest"]; assert re.fullmatch(r"sha256:[0-9a-f]{64}",d); print(d.split(":")[1])' "$SEAL_OUT/index.json" +INDEX_MANIFEST="$COMMAND_OUTPUT" assert_file_exists "index references a manifest blob" "$SEAL_OUT/blobs/sha256/$INDEX_MANIFEST" +assert_success "sealed manifest has exactly one layer" python3 -c \ + 'import json,sys; assert len(json.load(open(sys.argv[1]))["layers"]) == 1' "$SEAL_OUT/blobs/sha256/$INDEX_MANIFEST" assert_output_contains \ "sealed image carries provenance annotation" \ "org.atomic.view" \ @@ -168,10 +185,12 @@ assert_output_contains \ # Seal of the SHARED dev view must contain dev's recorded GRAPH content (not # the working tree on disk). Extract the single layer and inspect it. -SEAL_LAYER="$(python3 -c "import json,sys; m=json.load(open('$SEAL_OUT/blobs/sha256/$INDEX_MANIFEST')); print(m['layers'][0]['digest'].split(':')[1])")" +require_capture "read sealed layer digest" python3 -c \ + 'import json,re,sys; a=json.load(open(sys.argv[1]))["layers"]; assert len(a)==1; d=a[0]["digest"]; assert re.fullmatch(r"sha256:[0-9a-f]{64}",d); print(d.split(":")[1])' "$SEAL_OUT/blobs/sha256/$INDEX_MANIFEST" +SEAL_LAYER="$COMMAND_OUTPUT" SEAL_EXTRACT="$SBROOT/seal-dev-extract" mkdir -p "$SEAL_EXTRACT" -tar -xzf "$SEAL_OUT/blobs/sha256/$SEAL_LAYER" -C "$SEAL_EXTRACT" 2>/dev/null || true +require_success "extract sealed dev layer" tar -xzf "$SEAL_OUT/blobs/sha256/$SEAL_LAYER" -C "$SEAL_EXTRACT" assert_file_exists "sealed dev layer contains src/main.rs" "$SEAL_EXTRACT/src/main.rs" assert_file_exists "sealed dev layer contains Cargo.toml" "$SEAL_EXTRACT/Cargo.toml" @@ -195,19 +214,22 @@ begin_section "Sandbox: stage produces a two-layer (base + delta) image" # agent-3 has the "work in draft" change on top of dev — stage it as base+delta. STAGE_OUT="$SBROOT/stage-agent-3" -assert_success "stage agent-3 over dev" \ +require_success "stage agent-3 over dev" \ bash -c "cd '$CANON' && '$ATOMIC_BIN' sandbox stage agent-3 --base dev -o '$STAGE_OUT'" assert_file_exists "staged oci-layout written" "$STAGE_OUT/oci-layout" # The staged manifest must reference TWO layers (base + delta). -STAGE_MANIFEST="$(grep -o 'sha256:[0-9a-f]\{64\}' "$STAGE_OUT/index.json" | head -1 | cut -d: -f2)" -LAYER_COUNT="$(grep -o 'tar+gzip' "$STAGE_OUT/blobs/sha256/$STAGE_MANIFEST" | wc -l | tr -d ' ')" -if [ "$LAYER_COUNT" = "2" ]; then - _pass "staged image has two layers (base + delta)" -else - _fail "staged image has two layers (base + delta)" "expected 2 tar+gzip layers, got $LAYER_COUNT" -fi +require_capture "read staged manifest digest" python3 -c \ + 'import json,re,sys; a=json.load(open(sys.argv[1]))["manifests"]; assert len(a)==1; d=a[0]["digest"]; assert re.fullmatch(r"sha256:[0-9a-f]{64}",d); print(d.split(":")[1])' "$STAGE_OUT/index.json" +STAGE_MANIFEST="$COMMAND_OUTPUT" +require_capture "staged image has two layers (base + delta)" python3 -c \ + 'import json,re,sys; a=json.load(open(sys.argv[1]))["layers"]; assert len(a)==2; assert all("tar+gzip" in x["mediaType"] for x in a); d=a[1]["digest"]; assert re.fullmatch(r"sha256:[0-9a-f]{64}",d); print(d.split(":")[1])' "$STAGE_OUT/blobs/sha256/$STAGE_MANIFEST" +STAGE_LAYER="$COMMAND_OUTPUT" +STAGE_EXTRACT="$SBROOT/stage-delta-extract" +mkdir -p "$STAGE_EXTRACT" +require_success "extract staged delta layer" tar -xzf "$STAGE_OUT/blobs/sha256/$STAGE_LAYER" -C "$STAGE_EXTRACT" +assert_file_content "staged delta contains draft span content" "$STAGE_EXTRACT/src/main.rs" 'fn main() { println!("draft work"); }' assert_output_contains \ "staged image records base-view provenance" \ diff --git a/tests/harness/21_status_add_asymmetry.sh b/tests/harness/21_status_add_asymmetry.sh index b1acbc87..13676a2c 100755 --- a/tests/harness/21_status_add_asymmetry.sh +++ b/tests/harness/21_status_add_asymmetry.sh @@ -5,19 +5,14 @@ # "I run atomic status and it shows me tons of things that aren't tracked, # then I run atomic add and it says there's nothing to add." # -# Root cause: `status` uses the view's change filter to decide which TREE -# entries are "tracked" (view-aware), while `add` / `is_tracked` checks the -# global TREE table (view-unaware). Files recorded on a child/sibling view -# are in TREE but their introducing change isn't visible on the current -# view. Result: -# - status: file in TREE but filtered out → filesystem walk finds it → "Untracked" -# - add: file in TREE → is_tracked() = true → "already tracked" → skip +# A file recorded only on another view must disappear when switching away. +# If independently recreated on disk, it is untracked on this view and `add` +# must stage it; a global TREE row alone does not grant view ownership. # # Scenarios tested: -# 1. File recorded on child draft view, parent view shows it as untracked -# 2. File recorded on sibling view, other sibling shows it as untracked -# 3. Agent workflow: auto-created draft view records files, parent is confused -# 4. After fix: no file should be simultaneously "Untracked" AND "already tracked" +# 1. Switching away from a child or sibling removes that view's file. +# 2. An independently recreated path is untracked until added on this view. +# 3. `add` stages such paths, and recording preserves their bytes. HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" @@ -32,75 +27,46 @@ begin_section "Status/Add Asymmetry: Child draft view leaks into parent" # 3. Create a draft child view (like the agent would) # 4. Switch to draft, create+add+record a new file # 5. Switch back to dev -# 6. BUG: status shows the file as Untracked (it's on disk from the -# draft view's record, but the change isn't in dev's filter) -# 7. BUG: atomic add says "already tracked" (it's in the global TREE) -# -# Expected after fix: -# - The file should NOT appear as Untracked on dev -# - atomic add should not produce a contradictory state +# 6. Verify the file is absent, then recreate it independently on dev. +# 7. Verify untracked → added without a contradictory "already tracked". make_temp_repo "status-add-child-draft" init_repo # Step 1: Record a base file on dev so the view isn't empty create_file "base.txt" "base content" -assert_success "add base.txt on dev" atomic add base.txt -record_change "Add base.txt on dev" >/dev/null 2>&1 || true +require_success "add base.txt on dev" atomic add base.txt +require_success "record base.txt on dev" record_change "Add base.txt on dev" assert_clean "dev is clean after recording base.txt" # Step 2: Create a draft child view (simulates agent session) -out="$(atomic view create agent-session --draft --parent dev 2>&1)" || true -if echo "$out" | grep -qiE "created|view"; then - _pass "create draft view agent-session" -else - _fail "create draft view agent-session" "output: $out" -fi +require_success "create draft view agent-session" atomic view create agent-session --draft --parent dev # Step 3: Switch to the draft view and create+add+record a file -switch_view "agent-session" >/dev/null 2>&1 || true +require_success "switch to agent-session" switch_view "agent-session" assert_current_view "on agent-session" "agent-session" create_file "agent_file.txt" "created by agent" -assert_success "add agent_file.txt on agent-session" atomic add agent_file.txt -record_change "Agent creates agent_file.txt" >/dev/null 2>&1 || true +require_success "add agent_file.txt on agent-session" atomic add agent_file.txt +require_success "record agent_file.txt" record_change "Agent creates agent_file.txt" # Verify it's clean on the draft view -out="$(get_status_short)" -if echo "$out" | grep -qE "^[MADU?].*agent_file\.txt"; then - _fail "agent_file.txt is clean on agent-session" "still dirty: $out" -else - _pass "agent_file.txt is clean on agent-session after record" -fi +assert_status_no_entry "agent_file.txt is clean on agent-session after record" "agent_file.txt" # Step 4: Switch back to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_current_view "back on dev" "dev" -# Step 5: THE BUG — status should NOT show agent_file.txt as Untracked -out="$(get_status_short)" -if echo "$out" | grep -qE "^\?.*agent_file\.txt"; then - _fail "agent_file.txt NOT shown as Untracked on dev" \ - "BUG: status shows '?' for a file that is in TREE (from draft view). Status: $out" -else - _pass "agent_file.txt NOT shown as Untracked on dev" -fi - -# Step 6: THE BUG — add should not claim "already tracked" for an "Untracked" file -# If the file shows as Untracked, trying to add it should succeed. -# If the file doesn't show at all (correct behavior), add should say "already tracked". -# The BROKEN state is: status=Untracked + add=already-tracked (contradictory). -add_out="$(atomic add agent_file.txt 2>&1)" || true -status_out="$(get_status_short)" -has_untracked=$(echo "$status_out" | grep -cE "^\?.*agent_file\.txt" || true) -has_already_tracked=$(echo "$add_out" | grep -ci "already tracked" || true) - -if [[ "$has_untracked" -gt 0 && "$has_already_tracked" -gt 0 ]]; then - _fail "no contradictory status+add state" \ - "BUG: status says Untracked but add says 'already tracked'. Status: $status_out | Add: $add_out" -else - _pass "no contradictory status+add state" -fi +# Step 5: The draft-only file must be absent on dev. +assert_file_not_exists "agent_file.txt is absent on dev" "agent_file.txt" +assert_status_no_entry "agent_file.txt absent from dev status" "agent_file.txt" + +# Step 6: An independently recreated file must be addable on dev. +create_file "agent_file.txt" "independently recreated" +assert_status_flag "recreated agent_file.txt is untracked on dev" "?" "agent_file.txt" +require_success "add independently recreated agent_file.txt" atomic add agent_file.txt +assert_status_flag "recreated agent_file.txt is staged on dev" "A" "agent_file.txt" +assert_file_content "adding recreated file preserves bytes" "agent_file.txt" "independently recreated" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Status/Add Asymmetry: Sibling views don't leak" @@ -119,44 +85,33 @@ init_repo # Record base on dev create_file "base.txt" "base" -assert_success "add base.txt" atomic add base.txt -record_change "Add base" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base" record_change "Add base" # Create two sibling draft views -atomic view create feature-a --draft --parent dev >/dev/null 2>&1 || true -atomic view create feature-b --draft --parent dev >/dev/null 2>&1 || true +require_success "create feature-a" atomic view create feature-a --draft --parent dev +require_success "create feature-b" atomic view create feature-b --draft --parent dev # Record on feature-a -switch_view "feature-a" >/dev/null 2>&1 || true +require_success "switch to feature-a" switch_view "feature-a" create_file "only_on_a.txt" "feature-a content" -assert_success "add only_on_a.txt on feature-a" atomic add only_on_a.txt -record_change "Add only_on_a.txt on feature-a" >/dev/null 2>&1 || true +require_success "add only_on_a.txt on feature-a" atomic add only_on_a.txt +require_success "record only_on_a.txt" record_change "Add only_on_a.txt on feature-a" # Switch to feature-b -switch_view "feature-b" >/dev/null 2>&1 || true +require_success "switch to feature-b" switch_view "feature-b" assert_current_view "on feature-b" "feature-b" # File from feature-a should NOT appear as Untracked on feature-b -out="$(get_status_short)" -if echo "$out" | grep -qE "^\?.*only_on_a\.txt"; then - _fail "only_on_a.txt NOT shown as Untracked on feature-b" \ - "BUG: sibling view file leaks as Untracked. Status: $out" -else - _pass "only_on_a.txt NOT shown as Untracked on feature-b" -fi - -# No contradictory state -add_out="$(atomic add only_on_a.txt 2>&1)" || true -status_out="$(get_status_short)" -has_untracked=$(echo "$status_out" | grep -cE "^\?.*only_on_a\.txt" || true) -has_already_tracked=$(echo "$add_out" | grep -ci "already tracked" || true) - -if [[ "$has_untracked" -gt 0 && "$has_already_tracked" -gt 0 ]]; then - _fail "no contradictory state on feature-b" \ - "BUG: status=Untracked + add='already tracked'. Status: $status_out | Add: $add_out" -else - _pass "no contradictory state on feature-b" -fi +assert_file_not_exists "only_on_a.txt absent on feature-b" "only_on_a.txt" +assert_status_no_entry "only_on_a.txt absent from feature-b status" "only_on_a.txt" + +# Recreate the name independently and verify status/add agree. +create_file "only_on_a.txt" "independent sibling content" +assert_status_flag "recreated sibling path is untracked" "?" "only_on_a.txt" +require_success "add recreated sibling path" atomic add only_on_a.txt +assert_status_flag "recreated sibling path is staged" "A" "only_on_a.txt" +assert_file_content "sibling re-add preserves bytes" "only_on_a.txt" "independent sibling content" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Status/Add Asymmetry: Agent batch-add workflow" @@ -173,67 +128,37 @@ init_repo # Base content on dev create_file "README.md" "# Project" -assert_success "add README.md" atomic add README.md -record_change "Initial commit" >/dev/null 2>&1 || true +require_success "add README.md" atomic add README.md +require_success "record initial commit" record_change "Initial commit" # Agent creates a draft view and records multiple files -atomic view create agent-turn-1 --draft --parent dev >/dev/null 2>&1 || true -switch_view "agent-turn-1" >/dev/null 2>&1 || true +require_success "create agent-turn-1" atomic view create agent-turn-1 --draft --parent dev +require_success "switch to agent-turn-1" switch_view "agent-turn-1" create_file "src/main.rs" "fn main() {}" create_file "src/lib.rs" "pub mod util;" create_file "src/util.rs" "pub fn helper() {}" -create_file "Cargo.toml" "[package]\nname = \"test\"" +create_file "Cargo.toml" $'[package]\nname = "test"' -assert_success "add src/main.rs" atomic add src/main.rs -assert_success "add src/lib.rs" atomic add src/lib.rs -assert_success "add src/util.rs" atomic add src/util.rs -assert_success "add Cargo.toml" atomic add Cargo.toml -record_change "Agent adds project structure" >/dev/null 2>&1 || true +require_success "add src/main.rs" atomic add src/main.rs +require_success "add src/lib.rs" atomic add src/lib.rs +require_success "add src/util.rs" atomic add src/util.rs +require_success "add Cargo.toml" atomic add Cargo.toml +require_success "record agent project structure" record_change "Agent adds project structure" # Switch back to dev -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_current_view "on dev after agent work" "dev" # NO agent files should appear as Untracked on dev -out="$(get_status_short)" -agent_untracked=0 for f in "src/main.rs" "src/lib.rs" "src/util.rs" "Cargo.toml"; do - if echo "$out" | grep -qE "^\?.*$(echo "$f" | sed 's/[\/\.]/\\&/g')"; then - agent_untracked=$((agent_untracked + 1)) - fi + assert_file_not_exists "$f absent from dev" "$f" + assert_status_no_entry "$f absent from dev status" "$f" done -if [[ "$agent_untracked" -gt 0 ]]; then - _fail "no agent files shown as Untracked on dev (found $agent_untracked)" \ - "BUG: $agent_untracked agent files leak as Untracked on parent. Status: $out" -else - _pass "no agent files shown as Untracked on dev" -fi - # atomic add --all should not find contradictory files -add_all_out="$(atomic add --all 2>&1)" || true -if echo "$add_all_out" | grep -qiE "already tracked"; then - # Check if any of those "already tracked" files were shown as Untracked - status_out="$(get_status_short)" - has_contradiction=false - for f in "src/main.rs" "src/lib.rs" "src/util.rs" "Cargo.toml"; do - escaped_f="$(echo "$f" | sed 's/[\/\.]/\\&/g')" - is_untracked=$(echo "$status_out" | grep -cE "^\?.*$escaped_f" || true) - if [[ "$is_untracked" -gt 0 ]]; then - has_contradiction=true - break - fi - done - if $has_contradiction; then - _fail "add --all not contradictory with status" \ - "BUG: files shown as Untracked but add says 'already tracked'" - else - _pass "add --all not contradictory with status" - fi -else - _pass "add --all not contradictory with status" -fi +require_success "add --all on clean dev" atomic add --all +assert_clean "dev stays clean after add --all" # ═══════════════════════════════════════════════════════════════════════════ begin_section "Status/Add Asymmetry: No-switch agent workflow (THE BUG)" @@ -267,8 +192,8 @@ init_repo # Base content on dev create_file "base.txt" "base" -assert_success "add base.txt" atomic add base.txt -record_change "Initial commit" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base" record_change "Initial commit" assert_clean "dev is clean" # --- Simulate agent hook: create draft, add+record, switch back --- @@ -277,16 +202,16 @@ create_file "agent_created.txt" "agent was here" create_file "src/agent_module.rs" "pub fn agent_fn() {}" # Step 2: Agent hook creates draft view -atomic view create agent-draft --draft --parent dev >/dev/null 2>&1 || true +require_success "create agent-draft" atomic view create agent-draft --draft --parent dev # Step 3: Agent hook switches to draft, adds, records, switches back -switch_view "agent-draft" >/dev/null 2>&1 || true -assert_success "add agent_created.txt on draft" atomic add agent_created.txt -assert_success "add src/agent_module.rs on draft" atomic add "src/agent_module.rs" -record_change "Agent turn: add files" >/dev/null 2>&1 || true +require_success "switch to agent-draft" switch_view "agent-draft" +require_success "add agent_created.txt on draft" atomic add agent_created.txt +require_success "add src/agent_module.rs on draft" atomic add "src/agent_module.rs" +require_success "record agent files" record_change "Agent turn: add files" # Step 4: Switch back to dev (this is what the hook does after recording) -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_current_view "back on dev after agent hook" "dev" # Step 5: User manually re-creates files (or they leaked from switch) @@ -296,101 +221,56 @@ assert_current_view "back on dev after agent hook" "dev" create_file "agent_created.txt" "agent was here" create_file "src/agent_module.rs" "pub fn agent_fn() {}" -# THE BUG: These files are in TREE (agent added them), the change is on -# the draft view. On dev: -# - status: change not in dev's filter → skip from tracked → Untracked -# - add: is_tracked() checks global TREE → already tracked → skip - -out="$(get_status_short)" -echo " DEBUG status output: $out" >&2 - -# Test 1: Files should NOT be shown as Untracked -if echo "$out" | grep -qE "^\?.*agent_created\.txt"; then - _fail "agent_created.txt NOT Untracked on dev" \ - "BUG: file in TREE from draft view shows as Untracked. Status: $out" -else - _pass "agent_created.txt NOT Untracked on dev" -fi - -if echo "$out" | grep -qE "^\?.*agent_module\.rs"; then - _fail "agent_module.rs NOT Untracked on dev" \ - "BUG: file in TREE from draft view shows as Untracked. Status: $out" -else - _pass "agent_module.rs NOT Untracked on dev" -fi - -# Test 2: The contradictory state must not exist -add_out="$(atomic add agent_created.txt 2>&1)" || true -status_out="$(get_status_short)" -has_untracked=$(echo "$status_out" | grep -cE "^\?.*agent_created\.txt" || true) -has_already_tracked=$(echo "$add_out" | grep -ci "already tracked" || true) - -if [[ "$has_untracked" -gt 0 && "$has_already_tracked" -gt 0 ]]; then - _fail "no status+add contradiction for agent_created.txt" \ - "BUG: status=Untracked + add='already tracked'. Status: $status_out | Add: $add_out" -else - _pass "no status+add contradiction for agent_created.txt" -fi - -# Test 3: If on disk + in TREE, the correct status should be Added -# (tracked but not recorded on THIS view) -if echo "$out" | grep -qE "^A.*agent_created\.txt"; then - _pass "agent_created.txt shown as Added (correct: tracked, needs record on dev)" -else - # Also acceptable: not shown at all (file shouldn't be on disk) - if echo "$out" | grep -q "agent_created\.txt"; then - _fail "agent_created.txt shown as Added" \ - "File shown with wrong status. Status: $out" - else - _pass "agent_created.txt shown as Added (or correctly hidden)" - fi -fi +# The draft's recorded change is not visible on dev. The independently +# recreated files must be untracked until explicitly added on dev. + +assert_status_flag "recreated agent_created.txt is untracked" "?" "agent_created.txt" +assert_status_flag "recreated agent_module.rs is untracked" "?" "src/agent_module.rs" + +# Test 2: Adding both untracked paths must stage them. +require_success "add recreated agent files" atomic add agent_created.txt src/agent_module.rs +assert_status_flag "agent_created.txt is staged on dev" "A" "agent_created.txt" +assert_status_flag "agent_module.rs is staged on dev" "A" "src/agent_module.rs" + +# Test 3: Record the independently added files and preserve their bytes. +require_success "record recreated agent files on dev" record_change "Record recreated agent files" +assert_clean "dev is clean after recording recreated files" +assert_file_content "agent_created.txt bytes preserved" "agent_created.txt" "agent was here" +assert_file_content "agent module bytes preserved" "src/agent_module.rs" "pub fn agent_fn() {}" # ═══════════════════════════════════════════════════════════════════════════ -begin_section "Status/Add Asymmetry: File on disk from other view shows as Added" +begin_section "Status/Add Asymmetry: Recreated path stages on the current view" # ═══════════════════════════════════════════════════════════════════════════ # -# If a file IS on disk and IS in TREE (from another view), the correct -# status should be "Added" (tracked, needs recording on this view) — NOT -# "Untracked" (which implies it's unknown to the system). -# -# This test verifies that the file shows as Added when it's on disk but -# its graph content belongs to another view. +# A file independently recreated after switching away is untracked until +# added on this view, regardless of another view's global TREE row. make_temp_repo "status-add-shows-added" init_repo # Record base on dev create_file "base.txt" "base" -assert_success "add base.txt" atomic add base.txt -record_change "Base commit" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base" record_change "Base commit" # Create draft, record a file on it -atomic view create feature --draft --parent dev >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature" atomic view create feature --draft --parent dev +require_success "switch to feature" switch_view "feature" create_file "feature.txt" "feature content" -assert_success "add feature.txt" atomic add feature.txt -record_change "Feature file" >/dev/null 2>&1 || true +require_success "add feature.txt" atomic add feature.txt +require_success "record feature file" record_change "Feature file" # Switch to dev — file should be removed from disk by materialization -switch_view "dev" >/dev/null 2>&1 || true - -# If the file was properly removed by switch_view, it should not appear at all -# If it still exists on disk (materialization bug), it should show as Added, NOT Untracked -if [[ -f "feature.txt" ]]; then - # File leaked onto disk — at minimum it should be Added, not Untracked - out="$(get_status_short)" - if echo "$out" | grep -qE "^\?.*feature\.txt"; then - _fail "leaked file shows as Added not Untracked" \ - "BUG: file on disk from another view shows as '?' instead of 'A'. Status: $out" - elif echo "$out" | grep -qE "^A.*feature\.txt"; then - _pass "leaked file shows as Added not Untracked" - else - _pass "leaked file handled correctly (not shown or shown with other status)" - fi -else - _pass "leaked file shows as Added not Untracked (file correctly removed by switch)" -fi +require_success "switch to dev" switch_view "dev" + +# Switching removes the recorded draft-only file; re-creation is explicit. +assert_file_not_exists "feature.txt removed on switch to dev" "feature.txt" +assert_status_no_entry "removed feature.txt absent from dev status" "feature.txt" +create_file "feature.txt" "recreated on dev" +assert_status_flag "recreated feature.txt is untracked" "?" "feature.txt" +require_success "add recreated feature.txt" atomic add feature.txt +assert_status_flag "recreated feature.txt is added" "A" "feature.txt" +assert_file_content "recreated feature.txt bytes are preserved" "feature.txt" "recreated on dev" # ═══════════════════════════════════════════════════════════════════════════ print_summary diff --git a/tests/harness/22_switch_conflict_markers.sh b/tests/harness/22_switch_conflict_markers.sh index 488bedc8..45f96d91 100755 --- a/tests/harness/22_switch_conflict_markers.sh +++ b/tests/harness/22_switch_conflict_markers.sh @@ -42,29 +42,29 @@ init_repo # Create file on dev create_file "config.txt" "version=1.0" -assert_success "add config.txt" atomic add config.txt -record_change "Add config.txt" >/dev/null 2>&1 || true +require_success "add config.txt" atomic add config.txt +require_success "record config.txt" record_change "Add config.txt" # Create draft from dev -new_view "feature" --draft --parent dev >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature" new_view "feature" --draft --parent dev +require_success "switch to feature" switch_view "feature" # Modify on feature overwrite_file "config.txt" "version=2.0" -record_change "Update config to 2.0 on feature" >/dev/null 2>&1 || true +require_success "record feature config" record_change "Update config to 2.0 on feature" # Switch to dev — should show dev's version, no conflict markers -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_content "dev has original version" "config.txt" "version=1.0" assert_no_conflict_markers "no conflict markers after switch to dev (case 1)" "config.txt" # Switch back to feature — should show feature's version -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch back to feature" switch_view "feature" assert_file_content "feature has v2.0" "config.txt" "version=2.0" assert_no_conflict_markers "no conflict markers after switch to feature" "config.txt" # Round-trip -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_file_content "dev still has v1.0 (round 2)" "config.txt" "version=1.0" assert_no_conflict_markers "no conflict markers (round 2)" "config.txt" @@ -91,30 +91,30 @@ def main(): if __name__ == "__main__": main() EOF -assert_success "add app.py" atomic add app.py -record_change "Add app.py" >/dev/null 2>&1 || true +require_success "add app.py" atomic add app.py +require_success "record app.py" record_change "Add app.py" ORIGINAL_CONTENT="$(cat app.py)" # Create draft from dev -new_view "agent" --draft --parent dev >/dev/null 2>&1 || true -switch_view "agent" >/dev/null 2>&1 || true +require_success "create agent" new_view "agent" --draft --parent dev +require_success "switch to agent" switch_view "agent" # Modify line 5 on agent (change name) sed -i.bak 's/name = "World"/name = "Agent"/' app.py && rm -f app.py.bak -record_change "Change name to Agent" >/dev/null 2>&1 || true +require_success "record agent edit" record_change "Change name to Agent" AGENT_CONTENT="$(cat app.py)" # Switch to dev — should show ORIGINAL, not agent's version, no conflict -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_content "dev has original app.py" "app.py" "$ORIGINAL_CONTENT" assert_no_conflict_markers "no conflict markers on dev after agent edits (case 2)" "app.py" assert_file_contains "dev has World" "app.py" '"World"' assert_file_not_contains "dev does NOT have Agent" "app.py" '"Agent"' # Switch back to agent -switch_view "agent" >/dev/null 2>&1 || true +require_success "switch back to agent" switch_view "agent" assert_file_content "agent has its version" "app.py" "$AGENT_CONTENT" assert_no_conflict_markers "no conflict markers on agent" "app.py" @@ -144,31 +144,31 @@ url = postgres://localhost/mydb pool_size = 10 timeout = 30 EOF -assert_success "add server.conf" atomic add server.conf -record_change "Add server.conf" >/dev/null 2>&1 || true +require_success "add server.conf" atomic add server.conf +require_success "record server.conf" record_change "Add server.conf" # Create draft -new_view "feature" --draft --parent dev >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature" new_view "feature" --draft --parent dev +require_success "switch to feature" switch_view "feature" # Feature edits: change port and pool_size sed -i.bak 's/port = 8080/port = 9090/' server.conf && rm -f server.conf.bak sed -i.bak 's/pool_size = 10/pool_size = 20/' server.conf && rm -f server.conf.bak -record_change "Feature: change port and pool_size" >/dev/null 2>&1 || true +require_success "record feature configuration" record_change "Feature: change port and pool_size" # Switch to dev, edit workers and timeout -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_no_conflict_markers "no markers on dev before dev edits" "server.conf" sed -i.bak 's/workers = 4/workers = 8/' server.conf && rm -f server.conf.bak sed -i.bak 's/timeout = 30/timeout = 60/' server.conf && rm -f server.conf.bak -record_change "Dev: change workers and timeout" >/dev/null 2>&1 || true +require_success "record dev configuration" record_change "Dev: change workers and timeout" DEV_CONF="$(cat server.conf)" # Switch to feature — draft inherits dev's changes (live perspective) # so it sees the merged state of both views' edits -switch_view "feature" >/dev/null 2>&1 || true +require_success "switch to feature with inherited changes" switch_view "feature" assert_no_conflict_markers "no conflict markers on feature (merged, different lines)" "server.conf" # Feature should see BOTH its own edits AND dev's edits (inherited) assert_file_contains "feature has port 9090 (own edit)" "server.conf" "port = 9090" @@ -177,7 +177,7 @@ assert_file_contains "feature has workers 8 (inherited from dev)" "server.conf" assert_file_contains "feature has timeout 60 (inherited from dev)" "server.conf" "timeout = 60" # THE KEY TEST: Switch to dev — must show ONLY dev's state, no merge -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_no_conflict_markers "no conflict markers on dev after round-trip" "server.conf" assert_file_content "dev has its own clean version" "server.conf" "$DEV_CONF" assert_file_contains "dev has workers 8" "server.conf" "workers = 8" @@ -203,23 +203,23 @@ version = 1.0.0 author = original description = A sample app EOF -assert_success "add version.txt" atomic add version.txt -record_change "Add version.txt" >/dev/null 2>&1 || true +require_success "add version.txt" atomic add version.txt +require_success "record version.txt" record_change "Add version.txt" # Create draft -new_view "hotfix" --draft --parent dev >/dev/null 2>&1 || true -switch_view "hotfix" >/dev/null 2>&1 || true +require_success "create hotfix" new_view "hotfix" --draft --parent dev +require_success "switch to hotfix" switch_view "hotfix" # Hotfix changes version to 1.0.1 sed -i.bak 's/version = 1.0.0/version = 1.0.1/' version.txt && rm -f version.txt.bak -record_change "Hotfix: bump to 1.0.1" >/dev/null 2>&1 || true +require_success "record hotfix version" record_change "Hotfix: bump to 1.0.1" # Switch to dev, change version to 2.0.0 -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_no_conflict_markers "no markers before dev edit" "version.txt" sed -i.bak 's/version = 1.0.0/version = 2.0.0/' version.txt && rm -f version.txt.bak -record_change "Dev: bump to 2.0.0" >/dev/null 2>&1 || true +require_success "record dev version" record_change "Dev: bump to 2.0.0" # THE KEY TEST: dev must show ONLY its own version, no merge assert_no_conflict_markers "no conflict markers on dev (case 4)" "version.txt" @@ -227,12 +227,32 @@ assert_file_contains "dev has 2.0.0" "version.txt" "version = 2.0.0" # Switch to hotfix — draft inherits dev's change, CRDT merges # (the exact result depends on token-level merge — may be 2.0.1 or markers) -switch_view "hotfix" >/dev/null 2>&1 || true +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-switch-snapshot-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp version.txt "$snapshot_dir/version.txt" +require_capture "read dev history before conflicting switch" atomic log --format json --view dev --all +history_before="$COMMAND_OUTPUT" +if switch_out="$(switch_view "hotfix" 2>&1)"; then + _pass "switch to conflicting hotfix" +else + _fail "switch to conflicting hotfix" "$switch_out" + assert_current_view "refused switch preserves dev view" "dev" + assert_success "refused switch preserves dev file byte-for-byte" cmp -s "$snapshot_dir/version.txt" version.txt + require_capture "read dev history after refused switch" atomic log --format json --view dev --all + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then + _pass "refused switch preserves dev history" + else + _fail "refused switch preserves dev history" "target history changed" + fi + _skip "remaining case 4 and cases 5–14" "conflicting switch refused" + print_summary || true + exit 1 +fi # Don't assert exact content — just verify no crash and file exists assert_file_exists "version.txt exists on hotfix" "version.txt" # Switch back to dev — MUST be clean, the visit to hotfix must not pollute -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_no_conflict_markers "no conflict markers on dev after visiting hotfix" "version.txt" assert_file_contains "dev still has 2.0.0" "version.txt" "version = 2.0.0" # Verify dev does NOT have hotfix's version mixed in diff --git a/tests/harness/23_shared_view_parent_chain.sh b/tests/harness/23_shared_view_parent_chain.sh index 0429346c..d382c6e5 100755 --- a/tests/harness/23_shared_view_parent_chain.sh +++ b/tests/harness/23_shared_view_parent_chain.sh @@ -1,8 +1,9 @@ #!/usr/bin/env bash # 23_shared_view_parent_chain.sh — Shared view parent chain filter tests. # -# Validates that shared views with parent chains properly include -# all ancestor changes in their change filter during materialization. +# The CLI creates draft children only; promotion produces a shared root. +# The shared-child scenario cannot be constructed through this CLI and must +# not be counted as passed. The draft-chain cases below remain useful coverage. # # Bug: collect_visible_change_ids only walks parent chain for draft views. # Shared views with parents (e.g. staging → dev) only see their own @@ -50,7 +51,7 @@ assert_file_not_contains() { local path="$2" local needle="$3" if [[ ! -f "$path" ]]; then - _pass "$desc" + _fail "$desc" "file does not exist: $path" return fi if grep -qF "$needle" "$path"; then @@ -60,13 +61,23 @@ assert_file_not_contains() { fi } +assert_draft_parent() { + local name="$1" parent="$2" + require_capture "read view metadata for $name" atomic view list --all --json + assert_success "$name is draft parented on $parent" python3 -c \ + 'import json,sys; rows=json.loads(sys.argv[1])["views"]; assert any(x["name"] == sys.argv[2] and x["scope"] == "draft" and x["parent"] == sys.argv[3] for x in rows)' \ + "$COMMAND_OUTPUT" "$name" "$parent" +} + +_skip "shared-scope child parent-chain coverage" "CLI creates draft children; promotion clears the parent" + # ═══════════════════════════════════════════════════════════════════════════ -begin_section "Case 1: Shared chain dev → staging" +begin_section "Case 1: Draft child chain dev → staging" # ═══════════════════════════════════════════════════════════════════════════ # -# Build a two-level shared view hierarchy: +# Exercise the representable two-level view hierarchy: # dev (root shared, created by init) -# └── staging (shared, parent=dev) +# └── staging (draft, parent=dev) # # Record a file on dev, insert to staging, record on staging. # Switch between them. Each view should show only its own content. @@ -76,33 +87,34 @@ init_repo # Record file on dev (root shared) create_file "base.txt" "I am the base file from dev" -assert_success "add base.txt" atomic add base.txt -record_change "Add base.txt on dev" >/dev/null 2>&1 || true +require_success "add base.txt" atomic add base.txt +require_success "record base.txt" record_change "Add base.txt on dev" -# Create staging (shared, parent=dev) -new_view "staging" --parent dev >/dev/null 2>&1 || true -insert_from_view "dev" "staging" >/dev/null 2>&1 || true +# Create staging (draft, parent=dev) +require_success "create staging draft" new_view "staging" --draft --parent dev +assert_draft_parent "staging" "dev" +require_success "insert dev into staging" insert_from_view "dev" "staging" # Switch to staging — must see base.txt -switch_view "staging" >/dev/null 2>&1 || true +require_success "switch to staging" switch_view "staging" assert_file_exists "base.txt visible on staging" "base.txt" assert_file_content "base.txt has dev content on staging" "base.txt" "I am the base file from dev" assert_no_conflict_markers "no markers on staging" "base.txt" # Record something extra on staging create_file "staging-feature.txt" "staging feature" -assert_success "add staging-feature.txt" atomic add staging-feature.txt -record_change "Add staging-feature on staging" >/dev/null 2>&1 || true +require_success "add staging-feature.txt" atomic add staging-feature.txt +require_success "record staging feature" record_change "Add staging-feature on staging" # Switch to dev — should see base.txt but NOT staging-feature.txt -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_exists "base.txt on dev" "base.txt" assert_file_content "base.txt content on dev" "base.txt" "I am the base file from dev" assert_no_conflict_markers "no markers on dev" "base.txt" assert_file_not_exists "staging-feature.txt NOT on dev" "staging-feature.txt" # ═══════════════════════════════════════════════════════════════════════════ -begin_section "Case 2: Shared chain — file edited at each level" +begin_section "Case 2: Draft chain — file edited at each level" # ═══════════════════════════════════════════════════════════════════════════ # # dev has a file, staging adds to it. @@ -117,20 +129,21 @@ cat > config.ini << 'EOF' [dev] setting = base EOF -assert_success "add config.ini" atomic add config.ini -record_change "Add config on dev" >/dev/null 2>&1 || true +require_success "add config.ini" atomic add config.ini +require_success "record config on dev" record_change "Add config on dev" -# Create staging (shared, parent=dev), insert dev's changes, add a section -new_view "staging" --parent dev >/dev/null 2>&1 || true -insert_from_view "dev" "staging" >/dev/null 2>&1 || true -switch_view "staging" >/dev/null 2>&1 || true +# Create staging (draft, parent=dev), insert dev's changes, add a section +require_success "create staging draft" new_view "staging" --draft --parent dev +assert_draft_parent "staging" "dev" +require_success "insert dev into staging" insert_from_view "dev" "staging" +require_success "switch to staging" switch_view "staging" cat >> config.ini << 'EOF' [staging] version = 1.0 EOF -record_change "Add staging section" >/dev/null 2>&1 || true +require_success "record staging section" record_change "Add staging section" # Staging should see both sections assert_no_conflict_markers "no markers on staging" "config.ini" @@ -138,22 +151,22 @@ assert_file_contains "staging has dev section" "config.ini" "[dev]" assert_file_contains "staging has staging section" "config.ini" "[staging]" # Switch to dev — should see ONLY dev section -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_no_conflict_markers "no markers on dev" "config.ini" assert_file_contains "dev has dev section" "config.ini" "[dev]" assert_file_not_contains "dev lacks staging section" "config.ini" "[staging]" # Round-trip: switch staging → dev → staging -switch_view "staging" >/dev/null 2>&1 || true +require_success "switch back to staging" switch_view "staging" assert_no_conflict_markers "staging clean after round-trip" "config.ini" assert_file_contains "staging still has both" "config.ini" "[staging]" -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch back to dev" switch_view "dev" assert_no_conflict_markers "dev clean after round-trip" "config.ini" assert_file_not_contains "dev still lacks staging" "config.ini" "[staging]" # ═══════════════════════════════════════════════════════════════════════════ -begin_section "Case 3: Draft from shared child — inherits full chain" +begin_section "Case 3: Draft from draft child — inherits full chain" # ═══════════════════════════════════════════════════════════════════════════ # # A draft view parented on staging (which is parented on dev) should @@ -165,16 +178,18 @@ init_repo # Dev creates a file create_file "library.rs" 'pub fn core_fn() -> i32 { 42 }' -assert_success "add library.rs" atomic add library.rs -record_change "Add library on dev" >/dev/null 2>&1 || true +require_success "add library.rs" atomic add library.rs +require_success "record library on dev" record_change "Add library on dev" -# Chain: dev → staging (shared) -new_view "staging" --parent dev >/dev/null 2>&1 || true -insert_from_view "dev" "staging" >/dev/null 2>&1 || true +# Chain: dev → staging (draft) +require_success "create staging draft" new_view "staging" --draft --parent dev +assert_draft_parent "staging" "dev" +require_success "insert dev into staging" insert_from_view "dev" "staging" # Draft from staging -new_view "feature" --draft --parent staging >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature draft" new_view "feature" --draft --parent staging +assert_draft_parent "feature" "staging" +require_success "switch to feature" switch_view "feature" # Feature should see the file from dev (via staging → dev chain) assert_file_exists "library.rs on feature" "library.rs" @@ -183,16 +198,16 @@ assert_no_conflict_markers "no markers on feature" "library.rs" # Edit on feature overwrite_file "library.rs" 'pub fn core_fn() -> i32 { 99 }' -record_change "Feature: return 99" >/dev/null 2>&1 || true +require_success "record feature edit" record_change "Feature: return 99" # Switch to staging — must see dev's original -switch_view "staging" >/dev/null 2>&1 || true +require_success "switch to staging" switch_view "staging" assert_file_exists "library.rs on staging" "library.rs" assert_file_contains "staging has 42" "library.rs" "42" assert_no_conflict_markers "no markers on staging" "library.rs" # Switch to dev — must see dev's original -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_file_exists "library.rs on dev" "library.rs" assert_file_contains "dev has 42" "library.rs" "42" assert_no_conflict_markers "no markers on dev" "library.rs" diff --git a/tests/harness/24_concurrent_insert_conflict.sh b/tests/harness/24_concurrent_insert_conflict.sh index 12fa66c1..c238be76 100755 --- a/tests/harness/24_concurrent_insert_conflict.sh +++ b/tests/harness/24_concurrent_insert_conflict.sh @@ -41,15 +41,15 @@ import json def main(): pass PYEOF -assert_success "add imports.py" atomic add imports.py -record_change "Add imports.py on dev" >/dev/null 2>&1 || true +require_success "add imports.py" atomic add imports.py +require_success "record base imports" record_change "Add imports.py on dev" # Create two draft views parented on dev -new_view "alpha" --draft --parent dev >/dev/null 2>&1 || true -new_view "beta" --draft --parent dev >/dev/null 2>&1 || true +require_success "create alpha" new_view "alpha" --draft --parent dev +require_success "create beta" new_view "beta" --draft --parent dev # On alpha: add an empty line after the imports section (after 'import json') -switch_view "alpha" >/dev/null 2>&1 || true +require_success "switch to alpha" switch_view "alpha" cat > imports.py << 'PYEOF' import os import sys @@ -69,10 +69,10 @@ import json def main(): pass PYEOF -record_change "Alpha: add blank line after imports" >/dev/null 2>&1 || true +require_success "record alpha edit" record_change "Alpha: add blank line after imports" # On beta: add the same empty line at the same position -switch_view "beta" >/dev/null 2>&1 || true +require_success "switch to beta" switch_view "beta" cat > imports.py << 'PYEOF' import os import sys @@ -82,16 +82,36 @@ import json def main(): pass PYEOF -record_change "Beta: add blank line after imports" >/dev/null 2>&1 || true +require_success "record beta edit" record_change "Beta: add blank line after imports" # Switch to dev, insert from alpha, then beta -switch_view "dev" >/dev/null 2>&1 || true -insert_from_view "alpha" "dev" >/dev/null 2>&1 || true -insert_from_view "beta" "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" +require_success "insert alpha into dev" insert_from_view "alpha" "dev" +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-concurrent-snapshot-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp imports.py "$snapshot_dir/imports.py" +require_capture "read dev history before beta insert" atomic log --format json --view dev --all +history_before="$COMMAND_OUTPUT" +if insert_out="$(insert_from_view "beta" "dev" 2>&1)"; then + _pass "insert identical beta edit into dev" +else + _fail "insert identical beta edit into dev" "$insert_out" + assert_success "refused insert preserves file bytes" cmp -s "$snapshot_dir/imports.py" imports.py + assert_current_view "refused insert preserves dev view" "dev" + require_capture "read dev history after refusal" atomic log --format json --view dev --all + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then + _pass "refused insert preserves dev history" + else + _fail "refused insert preserves dev history" "history changed" + fi + _skip "remaining case 1 and cases 2–4" "identical concurrent insertion refused" + print_summary || true + exit 1 +fi # Re-materialize: switch away and back -switch_view "alpha" >/dev/null 2>&1 || true -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to alpha" switch_view "alpha" +require_success "switch back to dev" switch_view "dev" assert_file_exists "imports.py exists on dev after inserts" "imports.py" assert_no_conflict_markers "no conflict markers on dev (identical edits)" "imports.py" @@ -101,8 +121,8 @@ assert_file_contains "dev has def main" "imports.py" "def main():" # Verify content is stable across switches DEV_SNAPSHOT_1="$(snapshot_file "imports.py")" -switch_view "alpha" >/dev/null 2>&1 || true -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to alpha again" switch_view "alpha" +require_success "switch back to dev again" switch_view "dev" assert_file_stable "dev content stable after round-trip (case 1)" "imports.py" "$DEV_SNAPSHOT_1" # ═══════════════════════════════════════════════════════════════════════════ diff --git a/tests/harness/25_agent_provenance.sh b/tests/harness/25_agent_provenance.sh index d2061961..72400173 100755 --- a/tests/harness/25_agent_provenance.sh +++ b/tests/harness/25_agent_provenance.sh @@ -19,30 +19,24 @@ # hook carries it (the timing fix), and the agent's response is # derived from the transcript. # -# Prerequisites: sqlite3, python3, zstd. Suite skips (exit 77) if absent. +# Prerequisite: Python with sqlite3. Suite skips (exit 77) if absent. HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" -for tool in sqlite3 python3 zstd; do +for tool in python3; do if ! command -v "$tool" &>/dev/null; then skip_suite "$tool not installed" fi done -# Extract the last zstd frame of a change file (the UNHASHED section is -# written last, before the 32-byte trailer) and print its JSON. -last_section_json() { - local change_file="$1" - python3 - "$change_file" <<'EOF' >/tmp/_prov_last.zst -import sys -data = open(sys.argv[1], "rb").read() -i = data.rfind(b"\x28\xb5\x2f\xfd") -assert i >= 0, "no zstd frame in change file" -sys.stdout.buffer.write(data[i:len(data) - 32]) -EOF - zstd -d -f /tmp/_prov_last.zst -o /tmp/_prov_last.json >/dev/null 2>&1 - cat /tmp/_prov_last.json +if ! python3 -c 'import sqlite3' >/dev/null 2>&1; then + skip_suite "Python sqlite3 module unavailable" +fi + +run_hook() { + local payload="$1"; shift + printf '%s\n' "$payload" | "$@" } # ═══════════════════════════════════════════════════════════════════════════ @@ -57,57 +51,33 @@ REPO_DIR="$(pwd -P)" # Fake OpenCode data dir with a minimal schema — just what the reader # queries: session.directory, message(id, data), part(message_id, data). -OC_HOME="$REPO_DIR/oc-home" -mkdir -p "$OC_HOME" +OC_HOME="$(mktemp -d "${TMPDIR:-/tmp}/atomic-opencode-fixture-XXXXXX")" +_HARNESS_TMPDIRS+=("$OC_HOME") NOW_MS=$(( $(date +%s) * 1000 )) USER_MS=$NOW_MS ASST_MS=$(( NOW_MS + 5000 )) -sqlite3 "$OC_HOME/opencode.db" </dev/null 2>&1 -echo '{"session_id":"ses_harness","prompt":"harness: fix the widget","cwd":"'"$REPO_DIR"'"}' \ - | OPENCODE_HOME="$OC_HOME" atomic agent hooks opencode user-prompt >/dev/null 2>&1 +require_success "start OpenCode session" run_hook '{"session_id":"ses_harness","source":"startup","cwd":"'"$REPO_DIR"'"}' env OPENCODE_HOME="$OC_HOME" "$ATOMIC_BIN" agent hooks opencode session-start +require_success "submit OpenCode prompt" run_hook '{"session_id":"ses_harness","prompt":"harness: fix the widget","cwd":"'"$REPO_DIR"'"}' env OPENCODE_HOME="$OC_HOME" "$ATOMIC_BIN" agent hooks opencode user-prompt create_file "widget.txt" "widget v1" # A tool hook carrying the same call id as the store's tool part — the join # key the enrichment uses to graft commands/outputs onto graph nodes. -echo '{"session_id":"ses_harness","tool_name":"bash","tool_call_id":"call_h1","status":"completed","cwd":"'"$REPO_DIR"'"}' \ - | OPENCODE_HOME="$OC_HOME" atomic agent hooks opencode after-tool >/dev/null 2>&1 +require_success "record OpenCode tool event" run_hook '{"session_id":"ses_harness","tool_name":"bash","tool_call_id":"call_h1","status":"completed","cwd":"'"$REPO_DIR"'"}' env OPENCODE_HOME="$OC_HOME" "$ATOMIC_BIN" agent hooks opencode after-tool -echo '{"session_id":"ses_harness","turn_number":1,"model":"test/model","provider":"testprovider","cwd":"'"$REPO_DIR"'"}' \ - | OPENCODE_HOME="$OC_HOME" atomic agent hooks opencode stop >/dev/null 2>&1 +require_success "finalize OpenCode turn" run_hook '{"session_id":"ses_harness","turn_number":1,"model":"test/model","provider":"testprovider","cwd":"'"$REPO_DIR"'"}' env OPENCODE_HOME="$OC_HOME" "$ATOMIC_BIN" agent hooks opencode stop if [[ -f ".atomic/sessions/ses_harness.json" ]]; then _pass "opencode turn recorded a session" else @@ -125,8 +95,10 @@ else fi # Finalized history remains readable through the immutable session ledger. -HASH="$(atomic log -f json 2>/dev/null | python3 -c "import json,sys; print(json.load(sys.stdin)[0]['hash'])")" -ledger_json="$(atomic session show ses_harness --json 2>/dev/null)" +require_capture "read OpenCode change log" atomic log -f json +HASH="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; entries=json.load(sys.stdin); assert entries; print(entries[0]["hash"])')" +require_capture "read OpenCode session ledger" atomic session show ses_harness --json +ledger_json="$COMMAND_OUTPUT" if echo "$ledger_json" | python3 -c "import json,sys; d=json.load(sys.stdin); assert len(d[1]) == 1; assert d[1][0]['provenance_hash']"; then _pass "finalized provenance is readable from the session ledger" else @@ -136,7 +108,8 @@ assert_file_not_exists "graph.json removed after owner-journal cutover" ".atomic assert_file_not_exists "graph.lock removed after owner-journal cutover" ".atomic/sessions/ses_harness/graph.lock" # #4 (change side) — reasoning_text, tokens, cost, steps on the provenance. -prov_json="$(atomic change "$HASH" -f json 2>/dev/null)" +require_capture "read recorded OpenCode change" atomic change "$HASH" -f json +prov_json="$COMMAND_OUTPUT" if echo "$prov_json" | python3 -c " import json, sys p = json.load(sys.stdin)['provenance'] @@ -155,10 +128,11 @@ fi # #2 — agent_turn persisted inside the change file (unhashed section). -change_file="$(find .atomic/changes -name "$HASH.change" | head -1)" -if last_section_json "$change_file" | python3 -c " +change_file=".atomic/changes/${HASH:0:2}/${HASH}.change" +assert_file_exists "OpenCode change object exists" "$change_file" +if echo "$prov_json" | python3 -c " import json, sys -t = json.load(sys.stdin).get('agent_turn') +t = (json.load(sys.stdin).get('unhashed') or {}).get('agent_turn') assert t, 'no agent_turn key' types = [e['entry_type'] for e in t['condensed_transcript']] assert 'user' in types and 'assistant' in types and 'tool' in types, types @@ -176,21 +150,21 @@ make_temp_repo "agent-prov-cc" init_repo REPO_DIR="$(pwd -P)" -TRANSCRIPT="$REPO_DIR/cc-transcript.jsonl" -cat > "$TRANSCRIPT" <<'EOF' -{"type":"user","uuid":"u1","message":{"role":"user","content":[{"type":"text","text":"patch the parser"}]}} -{"type":"assistant","uuid":"a1","message":{"role":"assistant","content":[{"type":"text","text":"Patching now."},{"type":"tool_use","name":"Edit","input":{"file_path":"parser.rs"}}]}} -{"type":"assistant","uuid":"a2","message":{"role":"assistant","content":[{"type":"text","text":"The parser is patched and verified."}]}} -EOF +CC_FIXTURE_DIR="$(mktemp -d "${TMPDIR:-/tmp}/atomic-claude-fixture-XXXXXX")" +_HARNESS_TMPDIRS+=("$CC_FIXTURE_DIR") +TRANSCRIPT="$CC_FIXTURE_DIR/cc-transcript.jsonl" +printf '%s\n' \ + '{"type":"user","uuid":"u1","message":{"role":"user","content":[{"type":"text","text":"patch the parser"}]}}' \ + '{"type":"assistant","uuid":"a1","message":{"role":"assistant","content":[{"type":"text","text":"Patching now."},{"type":"tool_use","name":"Edit","input":{"file_path":"parser.rs"}}]}}' \ + '{"type":"assistant","uuid":"a2","message":{"role":"assistant","content":[{"type":"text","text":"The parser is patched and verified."}]}}' > "$TRANSCRIPT" # Deliberately NO transcript_path at session start / prompt — the timing # gap. The Stop hook carries it. -echo '{"session_id":"cc-harness","cwd":"'"$REPO_DIR"'"}' \ - | atomic agent hooks claude-code session-start >/dev/null 2>&1 -echo '{"session_id":"cc-harness","prompt":"patch the parser","cwd":"'"$REPO_DIR"'"}' \ - | atomic agent hooks claude-code user-prompt-submit >/dev/null 2>&1 +require_success "start Claude session" run_hook '{"session_id":"cc-harness","cwd":"'"$REPO_DIR"'"}' "$ATOMIC_BIN" agent hooks claude-code session-start +require_success "submit Claude prompt" run_hook '{"session_id":"cc-harness","prompt":"patch the parser","cwd":"'"$REPO_DIR"'"}' "$ATOMIC_BIN" agent hooks claude-code user-prompt-submit -before="$(python3 -c "import json; print(json.load(open('.atomic/sessions/cc-harness.json')).get('transcript_path'))" 2>/dev/null || true)" +require_capture "read Claude session before stop" python3 -c "import json; print(json.load(open('.atomic/sessions/cc-harness.json')).get('transcript_path'))" +before="$COMMAND_OUTPUT" if [[ "$before" == "None" || -z "$before" ]]; then _pass "session has no transcript_path before Stop (the gap scenario)" else @@ -199,8 +173,7 @@ fi create_file "parser.rs" "fn main() {}" -echo '{"session_id":"cc-harness","transcript_path":"'"$TRANSCRIPT"'","cwd":"'"$REPO_DIR"'"}' \ - | atomic agent hooks claude-code stop >/dev/null 2>&1 +require_success "finalize Claude turn" run_hook '{"session_id":"cc-harness","transcript_path":"'"$TRANSCRIPT"'","cwd":"'"$REPO_DIR"'"}' "$ATOMIC_BIN" agent hooks claude-code stop # #2 (timing fix) — transcript_path applied from the Stop event. after="$(python3 -c "import json; print(json.load(open('.atomic/sessions/cc-harness.json')).get('transcript_path') or '')")" @@ -211,8 +184,10 @@ else fi # #1 (transcript fallback) — immutable turn exists; agent_turn below checks text. -CC_HASH="$(atomic log -f json 2>/dev/null | python3 -c "import json,sys; print(json.load(sys.stdin)[0]['hash'])")" -cc_ledger="$(atomic session show cc-harness --json 2>/dev/null)" +require_capture "read Claude change log" atomic log -f json +CC_HASH="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; entries=json.load(sys.stdin); assert entries; print(entries[0]["hash"])')" +require_capture "read Claude session ledger" atomic session show cc-harness --json +cc_ledger="$COMMAND_OUTPUT" if echo "$cc_ledger" | python3 -c "import json,sys; d=json.load(sys.stdin); assert len(d[1]) == 1; assert d[1][0]['provenance_hash']"; then _pass "claude finalized provenance is readable from the session ledger" else @@ -221,10 +196,13 @@ fi assert_file_not_exists "claude graph.json removed after cutover" ".atomic/sessions/cc-harness/graph.json" # #2 — agent_turn persisted for the claude-code change too. -cc_file="$(find .atomic/changes -name "$CC_HASH.change" | head -1)" -if last_section_json "$cc_file" | python3 -c " +cc_file=".atomic/changes/${CC_HASH:0:2}/${CC_HASH}.change" +assert_file_exists "Claude change object exists" "$cc_file" +require_capture "read recorded Claude change" atomic change "$CC_HASH" -f json +cc_change="$COMMAND_OUTPUT" +if echo "$cc_change" | python3 -c " import json, sys -t = json.load(sys.stdin).get('agent_turn') +t = (json.load(sys.stdin).get('unhashed') or {}).get('agent_turn') assert t, 'no agent_turn key' texts = [e.get('content') for e in t['condensed_transcript'] if e.get('content')] assert any('patched and verified' in x for x in texts), texts diff --git a/tests/harness/26_vault_intent_inherit.sh b/tests/harness/26_vault_intent_inherit.sh index c3f4abb4..c62ec0da 100755 --- a/tests/harness/26_vault_intent_inherit.sh +++ b/tests/harness/26_vault_intent_inherit.sh @@ -35,22 +35,26 @@ copy_all_changes() { local src_changes="$1/.atomic/changes" local dst_changes="$2/.atomic/changes" - find "$src_changes" -name "*.change" -type f 2>/dev/null | while read -r f; do + find "$src_changes" -name "*.change" -type f -print0 | while IFS= read -r -d '' f; do local rel="${f#$src_changes/}" local dst_dir="$dst_changes/$(dirname "$rel")" - mkdir -p "$dst_dir" - cp "$f" "$dst_dir/" + mkdir -p "$dst_dir" || exit 1 + cp "$f" "$dst_dir/" || exit 1 + cmp -s "$f" "$dst_dir/$(basename "$f")" || exit 1 done } get_change_hashes() { - find "$1/.atomic/changes" -name "*.change" -type f 2>/dev/null \ - | sort | xargs -I{} basename {} .change -} - -# Extract the first PROJECT::author::seq human key from `intent list` output. -first_human_key() { - atomic intent list 2>/dev/null | grep -oE '[^ ]+::[^ ]+::[0-9]+' | head -1 + local log + if ! log="$(cd "$1" && atomic log --format json --reverse 2>&1)"; then + printf 'cannot read source history: %s\n' "$log" >&2 + return 1 + fi + printf '%s\n' "$log" | python3 -c ' +import json,sys +entries=json.load(sys.stdin) +assert entries and all(isinstance(e.get("hash"),str) and len(e["hash"]) == 52 for e in entries) +print("\n".join(e["hash"] for e in entries))' } # ════════════════════════════════════════════════════════════════════════════ @@ -61,23 +65,33 @@ make_temp_repo "vault-inherit-A" REPO_A="$REPO_DIR" init_repo --vault -atomic intent new "Inherited from A" >/dev/null 2>&1 -A_KEY="$(first_human_key)" +require_capture "User A: create intent" atomic intent new "Inherited from A" +A_KEY="$(printf '%s\n' "$COMMAND_OUTPUT" | sed -n 's/.*Created intent: \([A-Za-z0-9:_-]*\).*/\1/p' | head -1)" +A_INTENT_FILE="$(printf '%s\n' "$COMMAND_OUTPUT" | sed -n 's|.*file: \.vault/||p' | head -1)" if [[ -n "$A_KEY" ]]; then _pass "User A: intent created ($A_KEY)" else _fail "User A: intent created" "no human key in intent list" + print_summary || true + exit 1 fi A_SEQ="${A_KEY##*::}" +if [[ ! "$A_SEQ" =~ ^[0-9]+$ || ! "$A_INTENT_FILE" =~ ^intents/[0-9A-HJKMNP-TV-Z]{26}/intent\.md$ ]]; then + _fail "User A: intent identity and path" "key=$A_KEY file=$A_INTENT_FILE" + print_summary || true + exit 1 +fi +assert_file_exists "User A: intent file exists" ".vault/$A_INTENT_FILE" -atomic memory new --kind lesson --id inherited-note \ - --text "Vault entries must survive inheritance." >/dev/null 2>&1 +require_success "User A: create memory" atomic memory new --kind lesson --id inherited-note \ + --text "Vault entries must survive inheritance." assert_output_contains "User A: memory listed" "inherited-note" \ atomic memory list -record_change "vault: intent + memory" -a >/dev/null 2>&1 || true +require_success "User A: record vault content" record_change "vault: intent + memory" -a -A_HASHES="$(get_change_hashes "$REPO_A")" +require_capture "User A: read ordered change hashes" get_change_hashes "$REPO_A" +A_HASHES="$COMMAND_OUTPUT" if [[ -n "$A_HASHES" ]]; then _pass "User A: recorded vault content" else @@ -92,19 +106,21 @@ make_temp_repo "vault-inherit-B" REPO_B="$REPO_DIR" init_repo --no-vault -copy_all_changes "$REPO_A" "$REPO_B" +require_success "copy changes byte-for-byte" copy_all_changes "$REPO_A" "$REPO_B" cd "$REPO_B" for hash in $A_HASHES; do - atomic insert "$hash" --view dev >/dev/null 2>&1 || true + require_success "User B: insert $hash" atomic insert "$hash" --view dev done -atomic view switch dev >/dev/null 2>&1 || true +require_success "User B: select dev" atomic view switch dev assert_file_exists "User B: inherited memory materialized" \ ".vault/memory/inherited-note.md" +assert_success "User B: memory bytes match source" cmp -s "$REPO_A/.vault/memory/inherited-note.md" "$REPO_B/.vault/memory/inherited-note.md" +assert_success "User B: intent bytes match source" cmp -s "$REPO_A/.vault/$A_INTENT_FILE" "$REPO_B/.vault/$A_INTENT_FILE" # Bootstrap the vault tables from the materialized files (clone does this # automatically; we simulated the transfer with insert), then sync. -atomic vault init >/dev/null 2>&1 || true -atomic vault sync >/dev/null 2>&1 || true +require_success "User B: initialize vault" atomic vault init +require_success "User B: sync vault" atomic vault sync # ════════════════════════════════════════════════════════════════════════════ begin_section "User B: inherited intent + memory are queryable" @@ -126,29 +142,35 @@ assert_output_contains "User B: memory list shows inherited memory" \ begin_section "User B: local create allocates next seq; sync is idempotent" # ════════════════════════════════════════════════════════════════════════════ -atomic intent new "Created on B" >/dev/null 2>&1 +require_capture "User B: create local intent" atomic intent new "Created on B" +B_KEY="$(printf '%s\n' "$COMMAND_OUTPUT" | sed -n 's/.*Created intent: \([A-Za-z0-9:_-]*\).*/\1/p' | head -1)" +if [[ -z "$B_KEY" || "$B_KEY" == "$A_KEY" || ! "${B_KEY##*::}" =~ ^[0-9]+$ ]]; then + _fail "User B: local intent gets a distinct numeric key" "inherited=$A_KEY created=$B_KEY" + print_summary || true + exit 1 +fi -B_LIST="$(atomic intent list 2>/dev/null)" -B_COUNT="$(echo "$B_LIST" | grep -cE '[^ ]+::[^ ]+::[0-9]+' || true)" +require_capture "User B: list intents" atomic intent list --json +B_LIST="$COMMAND_OUTPUT" +B_COUNT="$(printf '%s\n' "$B_LIST" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))')" if [[ "$B_COUNT" == "2" ]]; then _pass "User B: both intents listed after local create" else _fail "User B: both intents listed" "expected 2 keys, got $B_COUNT: $B_LIST" fi -if [[ "$(echo "$B_LIST" | grep -oE '[^ ]+::[^ ]+::[0-9]+' | sort -u | wc -l | tr -d ' ')" == "2" ]]; then - _pass "User B: local intent got a fresh key (no collision with $A_KEY)" +assert_success "User B: both unique intent keys and titles persist" python3 -c \ + 'import json,sys; rows=json.loads(sys.argv[1]); assert len(rows)==2; assert {(r["id"],r["title"]) for r in rows} == {(sys.argv[2],"Inherited from A"),(sys.argv[3],"Created on B")}' "$B_LIST" "$A_KEY" "$B_KEY" +if [[ "${B_KEY##*::}" -eq $((10#$A_SEQ + 1)) ]]; then + _pass "User B: local intent gets next sequence" else - _fail "User B: allocator collision" "duplicate human keys: $B_LIST" + _fail "User B: local intent gets next sequence" "inherited=$A_KEY created=$B_KEY" fi -atomic vault sync >/dev/null 2>&1 || true -B_COUNT_AFTER="$(atomic intent list 2>/dev/null | grep -cE '[^ ]+::[^ ]+::[0-9]+' || true)" -if [[ "$B_COUNT_AFTER" == "2" ]]; then - _pass "User B: second vault sync is idempotent" -else - _fail "User B: second vault sync" "expected 2 keys, got $B_COUNT_AFTER" -fi +require_success "User B: sync vault again" atomic vault sync +require_capture "User B: list intents after second sync" atomic intent list --json +assert_success "User B: second vault sync is idempotent" python3 -c \ + 'import json,sys; before=json.loads(sys.argv[1]); after=json.loads(sys.argv[2]); assert sorted((r["id"],r["title"]) for r in before)==sorted((r["id"],r["title"]) for r in after)' "$B_LIST" "$COMMAND_OUTPUT" # ════════════════════════════════════════════════════════════════════════════ diff --git a/tests/harness/27_merge_ordering_duplication.sh b/tests/harness/27_merge_ordering_duplication.sh index 1ce16833..c8dfe865 100755 --- a/tests/harness/27_merge_ordering_duplication.sh +++ b/tests/harness/27_merge_ordering_duplication.sh @@ -150,11 +150,11 @@ fi begin_section "Post-merge repository state" -assert_success "status runs after a conflicted merge" atomic status +require_success "status runs after a conflicted merge" atomic status assert_occurrences "re-materializing does not add further copies" f.txt "header" 1 -switch_view side-a >/dev/null 2>&1 || true -switch_view "$BASE_VIEW" >/dev/null 2>&1 || true +require_success "switch to side-a" switch_view side-a +require_success "switch back to base view" switch_view "$BASE_VIEW" tail_after=$(grep -cE '^tail[0-9]+$' f.txt 2>/dev/null || true) if [[ "$tail_after" == "30" ]]; then diff --git a/tests/harness/28_merge_rubric.sh b/tests/harness/28_merge_rubric.sh index 70059452..48e0a236 100755 --- a/tests/harness/28_merge_rubric.sh +++ b/tests/harness/28_merge_rubric.sh @@ -45,7 +45,7 @@ make_temp_repo rubric-a1b1 build_case "$BASE_5" \ $'alpha\nbeta-A\ngamma\ndelta\nepsilon\n' \ $'alpha\nbeta\ngamma\ndelta-B\nepsilon\n' -atomic insert "$FEATURE_HASH" >/dev/null 2>&1 +require_success "disjoint single-insert succeeds" atomic insert "$FEATURE_HASH" assert_no_markers "disjoint single-insert: no markers" f.txt assert_present "disjoint single-insert: side A present" f.txt "beta-A" assert_present "disjoint single-insert: side B present" f.txt "delta-B" @@ -58,7 +58,7 @@ make_temp_repo rubric-a2b2 build_case "$BASE_5" \ $'alpha\nbeta-A\ngamma\ndelta\nepsilon\n' \ $'alpha\nbeta\ngamma-B\ndelta\nepsilon\n' -insert_from_view feature "$BASE_VIEW" >/dev/null 2>&1 +require_success "adjacent bulk-insert succeeds" insert_from_view feature "$BASE_VIEW" assert_no_markers "adjacent bulk-insert: no markers" f.txt assert_present "adjacent bulk-insert: side A present" f.txt "beta-A" assert_present "adjacent bulk-insert: side B present" f.txt "gamma-B" @@ -71,7 +71,27 @@ make_temp_repo rubric-a3b2 build_case "$BASE_5" \ $'alpha\nSAME-EDIT\ngamma\ndelta\nepsilon\n' \ $'alpha\nSAME-EDIT\ngamma\ndelta\nepsilon\n' -insert_from_view feature "$BASE_VIEW" >/dev/null 2>&1 +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-rubric-snapshot-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp f.txt "$snapshot_dir/f.txt" +require_capture "read base history before identical insert" atomic log --format json --view "$BASE_VIEW" --all +history_before="$COMMAND_OUTPUT" +if insert_out="$(insert_from_view feature "$BASE_VIEW" 2>&1)"; then + _pass "identical concurrent insert succeeds" +else + _fail "identical concurrent insert succeeds" "$insert_out" + assert_success "refused insert preserves file bytes" cmp -s "$snapshot_dir/f.txt" f.txt + assert_current_view "refused insert preserves base view" "$BASE_VIEW" + require_capture "read base history after refusal" atomic log --format json --view "$BASE_VIEW" --all + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then + _pass "refused insert preserves base history" + else + _fail "refused insert preserves base history" "history changed" + fi + _skip "remaining merge-rubric cases" "identical concurrent edit refused" + print_summary || true + exit 1 +fi assert_no_markers "identical edits: no markers" f.txt assert_occurrences "identical edits: deduped to a single copy" f.txt "SAME-EDIT" 1 assert_occurrences "identical edits: no tail dup" f.txt "epsilon" 1 diff --git a/tests/harness/29_rename.sh b/tests/harness/29_rename.sh index 8043238f..1e078e17 100755 --- a/tests/harness/29_rename.sh +++ b/tests/harness/29_rename.sh @@ -41,10 +41,11 @@ $(echo "$out" | sed 's/^/ /')" # Assert `atomic doctor check` reports the working copy consistent (exit 0). assert_doctor_consistent() { local desc="$1" - if atomic doctor check >/dev/null 2>&1; then + local out + if out="$(atomic doctor check 2>&1)"; then _pass "$desc" else - _fail "$desc" "atomic doctor check reported inconsistency" + _fail "$desc" "atomic doctor check failed: $out" fi } @@ -69,12 +70,17 @@ begin_section "atomic mv stages the original inode at the destination" make_temp_repo rename-mv-shape init_repo printf 'line1\nline2\nline3\n' > old.txt -add_files old.txt >/dev/null -record_change "base" >/dev/null -atomic mv old.txt new.txt >/dev/null 2>&1 +require_success "add original file" atomic add old.txt +require_success "record base file" record_change "base" +require_success "stage pure rename" atomic mv old.txt new.txt # Before record the destination is staged as Added with the original inode; # the graph-backed source claim remains available to the record planner. assert_status_has "mv: destination is staged" '^A[[:space:]]+new\.txt$' +require_success "record pure rename" record_change "pure rename" +assert_file_not_exists "pure rename removes old path" old.txt +assert_file_equals "pure rename preserves exact bytes" new.txt $'line1\nline2\nline3\n' +assert_status_clean "pure rename is clean after recording" +assert_output_contains "pure rename shows move evidence" "authoritative: old.txt" atomic change # ── Destination and unsupported-kind safety ────────────────────────────────── begin_section "atomic mv refuses unsafe destinations and directory moves" @@ -82,28 +88,28 @@ make_temp_repo rename-mv-safety init_repo printf 'source bytes\n' > source.txt printf 'tracked destination bytes\n' > tracked.txt -add_files source.txt tracked.txt >/dev/null -record_change "base" >/dev/null +require_success "add source and tracked destination" atomic add source.txt tracked.txt +require_success "record source and tracked destination" record_change "base" printf 'untracked destination bytes\n' > untracked.txt assert_failure "mv: refuses existing untracked destination without force" \ atomic mv source.txt untracked.txt -assert_file_contains "mv: source survives untracked-destination refusal" \ - source.txt "source bytes" -assert_file_contains "mv: untracked destination survives refusal" \ - untracked.txt "untracked destination bytes" +assert_file_equals "mv: source survives untracked-destination refusal" \ + source.txt $'source bytes\n' +assert_file_equals "mv: untracked destination survives refusal" \ + untracked.txt $'untracked destination bytes\n' assert_failure "mv: refuses tracked destination even with force" \ atomic mv --force source.txt tracked.txt -assert_file_contains "mv: source survives tracked-destination refusal" \ - source.txt "source bytes" -assert_file_contains "mv: tracked destination survives refusal" \ - tracked.txt "tracked destination bytes" -assert_success "mv: force safely replaces an untracked destination" \ +assert_file_equals "mv: source survives tracked-destination refusal" \ + source.txt $'source bytes\n' +assert_file_equals "mv: tracked destination survives refusal" \ + tracked.txt $'tracked destination bytes\n' +require_success "mv: force safely replaces an untracked destination" \ atomic mv --force source.txt untracked.txt assert_file_not_exists "mv: forced source moved" source.txt assert_file_contains "mv: forced destination has source bytes" \ untracked.txt "source bytes" mkdir empty-dir -add_files empty-dir >/dev/null +require_success "add empty directory" atomic add empty-dir assert_failure "mv: directory source is explicitly refused" \ atomic mv empty-dir moved-dir @@ -112,11 +118,31 @@ begin_section "atomic mv + record round-trips (content preserved, doctor clean)" make_temp_repo rename-mv-roundtrip init_repo printf 'line1\nline2\nline3\n' > old.txt -add_files old.txt >/dev/null -record_change "base" >/dev/null -atomic mv old.txt new.txt >/dev/null 2>&1 +require_success "add original file" atomic add old.txt +require_success "record base file" record_change "base" +require_success "stage rename" atomic mv old.txt new.txt printf 'line1\nline2 edited after move\nline3\n' > new.txt -record_change "rename+edit old->new" >/dev/null +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-rename-snapshot-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp new.txt "$snapshot_dir/new.txt" +require_capture "read history before rename+edit" atomic log --format json +history_before="$COMMAND_OUTPUT" +if record_out="$(record_change "rename+edit old->new" 2>&1)"; then + _pass "record rename with edit" +else + _fail "record rename with edit" "$record_out" + assert_success "refused record preserves destination bytes" cmp -s "$snapshot_dir/new.txt" new.txt + assert_file_not_exists "refused record preserves staged source removal" old.txt + require_capture "read history after refused rename+edit" atomic log --format json + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then + _pass "refused rename+edit preserves history" + else + _fail "refused rename+edit preserves history" "history changed" + fi + _skip "rename+edit postconditions and later cases" "record refused" + print_summary || true + exit 1 +fi assert_file_exists "rename: new path exists" new.txt assert_file_not_exists "rename: old path gone" old.txt assert_file_contains "rename: edit preserved" new.txt "line2 edited after move" @@ -133,8 +159,8 @@ assert_occurrences "rename: line3 once" new.txt "line3" 1 # ── Rename back restores the original ──────────────────────────────────────── begin_section "atomic mv back restores the original path" -atomic mv new.txt old.txt >/dev/null 2>&1 -record_change "rename back new->old" >/dev/null +require_success "stage rename back" atomic mv new.txt old.txt +require_success "record rename back" record_change "rename back new->old" assert_file_exists "rename-back: old path restored" old.txt assert_file_not_exists "rename-back: new path gone" new.txt assert_file_contains "rename-back: content intact" old.txt "line2" @@ -146,11 +172,11 @@ begin_section "atomic mv into a subdirectory" make_temp_repo rename-mv-subdir init_repo printf 'hello\nworld\n' > f.txt -add_files f.txt >/dev/null -record_change "base" >/dev/null +require_success "add subdirectory fixture" atomic add f.txt +require_success "record subdirectory fixture" record_change "base" mkdir -p sub -atomic mv f.txt sub/f.txt >/dev/null 2>&1 -record_change "move into sub" >/dev/null +require_success "stage subdirectory move" atomic mv f.txt sub/f.txt +require_success "record subdirectory move" record_change "move into sub" assert_file_exists "subdir move: sub/f.txt exists" sub/f.txt assert_file_not_exists "subdir move: f.txt gone" f.txt assert_file_contains "subdir move: content preserved" sub/f.txt "world" diff --git a/tests/harness/30_record_conflict_refusal.sh b/tests/harness/30_record_conflict_refusal.sh index 6fb99aa3..72d64d66 100755 --- a/tests/harness/30_record_conflict_refusal.sh +++ b/tests/harness/30_record_conflict_refusal.sh @@ -32,25 +32,63 @@ echo "${BOLD}record refuses conflict markers (and reports it as a user error)${R # markers on the base view. (Rubric A4 x B1; same shape as merge_helpers' # build_case, spelled out here so this suite stands alone.) setup_conflict() { - init_repo - printf 'alpha\nbeta\ngamma\n' > f.txt - add_files f.txt >/dev/null - record_change "base" >/dev/null - BASE_VIEW="$(current_view)" - - new_view feature >/dev/null - switch_view feature >/dev/null - printf 'alpha\nONE\nbeta\ngamma\n' > f.txt - record_change "edit A" >/dev/null - FEATURE_HASH="$(tip_hash feature)" - - switch_view "$BASE_VIEW" >/dev/null - printf 'alpha\nTWO\nbeta\ngamma\n' > f.txt - record_change "edit B" >/dev/null - - atomic insert "$FEATURE_HASH" >/dev/null 2>&1 + build_case $'alpha\nbeta\ngamma\n' $'alpha\nONE\nbeta\ngamma\n' $'alpha\nTWO\nbeta\ngamma\n' + local snapshot_dir history_before output + snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-refusal-snapshot-XXXXXX")" + _HARNESS_TMPDIRS+=("$snapshot_dir") + cp f.txt "$snapshot_dir/f.txt" + require_capture "read history before conflict insert" atomic log --format json --view "$BASE_VIEW" --all + history_before="$COMMAND_OUTPUT" + if output="$(atomic insert "$FEATURE_HASH" 2>&1)"; then + _pass "create conflict for record refusal" + else + _fail "create conflict for record refusal" "$output" + assert_success "refused conflict insert preserves bytes" cmp -s "$snapshot_dir/f.txt" f.txt + assert_current_view "refused conflict insert preserves view" "$BASE_VIEW" + require_capture "read history after refused conflict insert" atomic log --format json --view "$BASE_VIEW" --all + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then + _pass "refused conflict insert preserves history" + else + _fail "refused conflict insert preserves history" "history changed" + fi + _skip "merge-generated marker refusal and resolution cases" "conflict insert refused" + print_summary || true + exit 1 + fi } +begin_section "record refuses user-written marker text" +make_temp_repo record-user-markers +init_repo +create_file f.txt $'alpha\nbeta\n' +require_success "add marker fixture" atomic add f.txt +require_success "record marker fixture" record_change "base" +printf 'alpha\n>>>>>>> side A\nONE\n=======\nTWO\n<<<<<<< side B\n' > f.txt +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-marker-snapshot-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp f.txt "$snapshot_dir/f.txt" +require_capture "read history before marker refusal" atomic log --format json +history_before="$COMMAND_OUTPUT" +assert_failed_output_contains "marker refusal names file and line" "f.txt still contains conflict markers at line 2" atomic record -m "should be refused" +assert_failed_output_not_contains "marker refusal is not internal" "Internal error" atomic record -m "should be refused" +assert_failed_output_not_contains "marker refusal has no bug-report hint" "Please report it" atomic record -m "should be refused" +record_status=0 +record_output="$(atomic record -m "should be refused" 2>&1)" || record_status=$? +if [[ "$record_status" -ne 0 && "$record_status" -ne 128 ]]; then + _pass "marker refusal uses a user-error exit code" +else + _fail "marker refusal uses a user-error exit code" "exit=$record_status: $record_output" +fi +assert_success "marker refusal preserves bytes" cmp -s "$snapshot_dir/f.txt" f.txt +require_capture "read history after marker refusal" atomic log --format json +if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then + _pass "marker refusal preserves history" +else + _fail "marker refusal preserves history" "history changed" +fi +require_success "explicit marker override succeeds" atomic record -m "record explicit marker text" --allow-conflict-markers +assert_clean "marker override records the working file" + # ── 1 · the refusal ───────────────────────────────────────────────────────── begin_section "record refuses a file that still carries markers" @@ -60,7 +98,7 @@ setup_conflict assert_markers "conflict markers are present" f.txt assert_failure "record fails while markers remain" atomic record -m "should be refused" -assert_output_contains "names the file and the line" \ +assert_failed_output_contains "names the file and the line" \ "f.txt still contains conflict markers at line 2" \ atomic record -m "should be refused" @@ -73,9 +111,9 @@ assert_markers "markers survive the refused record" f.txt begin_section "the refusal is classified as a user error" -assert_output_not_contains "not reported as an internal error" \ +assert_failed_output_not_contains "not reported as an internal error" \ "Internal error" atomic record -m "should be refused" -assert_output_not_contains "does not tell the user to file a bug" \ +assert_failed_output_not_contains "does not tell the user to file a bug" \ "Please report it" atomic record -m "should be refused" # Exit code: must be a user/data error, never 128 (which error.rs reserves for @@ -94,7 +132,7 @@ fi # The hint should point at the fix, not at the issue tracker. # NB: the leading '--' is dropped from the needle on purpose — assert_output_contains # passes it straight to `grep -F`, which would parse '--allow…' as an option. -assert_output_contains "hint points at the resolution" \ +assert_failed_output_contains "hint points at the resolution" \ "allow-conflict-markers" atomic record -m "should be refused" # ── 3 · the documented override still works ───────────────────────────────── diff --git a/tests/harness/31_init_lazy_untracked.sh b/tests/harness/31_init_lazy_untracked.sh index aa51b7b4..5f01052e 100755 --- a/tests/harness/31_init_lazy_untracked.sh +++ b/tests/harness/31_init_lazy_untracked.sh @@ -19,33 +19,21 @@ fi mkfifo unrelated.pipe -INIT_STATUS=0 -atomic init >init.out 2>&1 & -INIT_PID=$! - -# Keep the harness finite if this behavior regresses. Polling avoids relying on -# GNU `timeout`, which is not installed by default on macOS. -for _ in {1..100}; do - if ! kill -0 "$INIT_PID" 2>/dev/null; then - break - fi - sleep 0.1 -done - -if kill -0 "$INIT_PID" 2>/dev/null; then - kill "$INIT_PID" 2>/dev/null || true - wait "$INIT_PID" 2>/dev/null || true - _fail "atomic init completes without reading the FIFO" \ - "initialization was still running after 10 seconds" -else - wait "$INIT_PID" || INIT_STATUS=$? - if [[ "$INIT_STATUS" -eq 0 ]]; then - _pass "atomic init completes without reading the FIFO" - else - _fail "atomic init completes without reading the FIFO" \ - "atomic init exited $INIT_STATUS: $(head -5 init.out)" - fi -fi +# The timeout kills only the process group created for this fixture, including +# the real CLI beneath any harness wrapper, so no blocked child is orphaned. +require_capture "atomic init completes without reading the FIFO" python3 -c ' +import os,signal,subprocess,sys +p=subprocess.Popen([sys.argv[1],"init"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,start_new_session=True) +try: + out,_=p.communicate(timeout=10) +except subprocess.TimeoutExpired: + os.killpg(p.pid,signal.SIGKILL) + out,_=p.communicate() + sys.stdout.buffer.write(out) + print("initialization was still running after 10 seconds",file=sys.stderr) + sys.exit(1) +sys.stdout.buffer.write(out) +sys.exit(p.returncode)' "$ATOMIC_BIN" assert_dir_exists "repository metadata was initialized" .atomic assert_dir_exists "vault was initialized" .vault diff --git a/tests/harness/31_record_oversized_file.sh b/tests/harness/31_record_oversized_file.sh index f0cf601e..58988131 100755 --- a/tests/harness/31_record_oversized_file.sh +++ b/tests/harness/31_record_oversized_file.sh @@ -36,7 +36,7 @@ make_oversized_file() { local path="$1" mkdir -p "$(dirname "$path")" # Sparse allocation: instant, and no multi-megabyte fixture in the repo. - dd if=/dev/zero of="$path" bs=1 count=0 seek="$OVERSIZED_BYTES" 2>/dev/null + dd if=/dev/zero of="$path" bs=1 count=0 seek="$OVERSIZED_BYTES" } # ── 1 · .gitignore keeps build output out of record ───────────────────────── @@ -49,13 +49,13 @@ init_repo printf '/.next/\n/node_modules\n' > .gitignore create_file src/app/page.tsx 'export default function Page() { return null }' create_file package.json '{"name":"next-app"}' -make_oversized_file .next/dev/cache/turbopack/v16.2.10/00000012.sst +require_success "create oversized build artifact" make_oversized_file .next/dev/cache/turbopack/v16.2.10/00000012.sst assert_status_no_entry "the build cache is not offered as untracked" ".next" assert_status_contains "source files still are" "src/app/page.tsx" -add_files . >/dev/null -assert_success "record succeeds despite the oversized build artifact" \ +require_success "add non-ignored files" atomic add . +require_success "record succeeds despite the oversized build artifact" \ atomic record -m "First change with Atomic" # The whole point: the artifact must not be in history. @@ -89,23 +89,23 @@ make_temp_repo record-oversized-tracked init_repo create_file README.md 'hello' -make_oversized_file dataset.bin -add_files . >/dev/null +require_success "create oversized tracked file" make_oversized_file dataset.bin +require_success "add oversized and small files" atomic add . assert_failure "record fails on the oversized file" \ atomic record -m "should be refused" -assert_output_not_contains "not reported as an internal error" \ +assert_failed_output_not_contains "not reported as an internal error" \ "Internal error" atomic record -m "should be refused" -assert_output_not_contains "does not tell the user to file a bug" \ +assert_failed_output_not_contains "does not tell the user to file a bug" \ "Please report it" atomic record -m "should be refused" # Sizes must be human-readable; a raw byte count says nothing about how far # over the limit the file is. -assert_output_contains "names the file" "dataset.bin" atomic record -m "should be refused" -assert_output_contains "size in human units" "10.8 MiB" atomic record -m "should be refused" -assert_output_contains "limit in human units" "10.0 MiB" atomic record -m "should be refused" -assert_output_not_contains "no raw byte counts" \ +assert_failed_output_contains "names the file" "dataset.bin" atomic record -m "should be refused" +assert_failed_output_contains "size in human units" "10.8 MiB" atomic record -m "should be refused" +assert_failed_output_contains "limit in human units" "10.0 MiB" atomic record -m "should be refused" +assert_failed_output_not_contains "no raw byte counts" \ "$OVERSIZED_BYTES" atomic record -m "should be refused" # Exit code: must be a user/data error, never 128. @@ -127,14 +127,14 @@ fi begin_section "the suggested escape hatches work" -assert_output_contains "hint mentions .atomicignore" \ +assert_failed_output_contains "hint mentions .atomicignore" \ ".atomicignore" atomic record -m "should be refused" -assert_output_contains "hint mentions skip-binary" \ +assert_failed_output_contains "hint mentions skip-binary" \ "skip-binary" atomic record -m "should be refused" -assert_output_contains "hint mentions max-size" \ +assert_failed_output_contains "hint mentions max-size" \ "max-size" atomic record -m "should be refused" -assert_success "--skip-binary records everything else" \ +require_success "--skip-binary records everything else" \ atomic record -m "skip the big one" --skip-binary # README.md went in; dataset.bin did not. @@ -157,13 +157,13 @@ make_temp_repo record-oversized-maxsize init_repo create_file notes.txt 'a few hundred bytes would be plenty here' -add_files . >/dev/null +require_success "add notes.txt" atomic add . assert_failure "a lowered --max-size refuses a small file" \ atomic record -m "should be refused" --max-size 8 -assert_output_contains "and reports it the same way" \ +assert_failed_output_contains "and reports it the same way" \ "notes.txt" atomic record -m "should be refused" --max-size 8 -assert_success "a raised --max-size accepts it" \ +require_success "a raised --max-size accepts it" \ atomic record -m "raised ceiling" --max-size 100000 print_summary diff --git a/tests/harness/32_review_gate_insert.sh b/tests/harness/32_review_gate_insert.sh index ea6d32aa..ca3b62f4 100755 --- a/tests/harness/32_review_gate_insert.sh +++ b/tests/harness/32_review_gate_insert.sh @@ -17,7 +17,7 @@ # # STATUS: the insert path is not implemented yet. Target-behavior checks are # gated on $EXPECT_INSERT: -# - unset/0 → reported as pending (skipped), suite stays green +# - unset/0 → reported as pending (skipped), suite is partial # - 1 → hard assertions, so this file drives implementation red→green # # What already holds today (hard-asserted here, no gate): @@ -63,9 +63,9 @@ assert_target() { # * `main_has_record` matches the `hash` FIELD exactly (whole line), so a # squash record's embedded trailer text can't produce a false positive. main_view_hashes() { - atomic log --view "$MAIN" -f json 2>/dev/null \ - | grep -ao '"hash"[[:space:]]*:[[:space:]]*"[^"]*"' \ - | sed -E 's/.*"([^"]+)"$/\1/' + local out + out="$(atomic log --view "$MAIN" -f json 2>&1)" || return 1 + printf '%s\n' "$out" | python3 -c 'import json,sys; rows=json.load(sys.stdin); assert isinstance(rows,list); print("\n".join(r["hash"] for r in rows))' } main_has_record() { local out; out="$(main_view_hashes)" @@ -78,7 +78,7 @@ tag_show_contains() { # Dry-run router recommendation for the current target (SPEC §4.4). dry_run_recommends_insert() { local out; out="$(atomic git import --incremental --branch "$MAIN" --dry-run 2>&1)" - grep -qaiE "squash|atomic header|insert" <<<"$out" + grep -qaiE '(recommend|route|use|will|would|should).*(insert|original)|(insert|original).*(recommend|route|instead)' <<<"$out" } # The shared view materializes `src/two.rs` after switching to it. shared_view_materializes_two() { @@ -109,10 +109,10 @@ make_temp_repo "review-gate-insert" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import seeds the shared view" atomic git import --no-vault +require_success "git import seeds the shared view" atomic git import --no-vault MAIN="$(git_default_branch)" -_pass "shared view is '$MAIN'" +assert_current_view "shared view is '$MAIN'" "$MAIN" # ════════════════════════════════════════════════════════════════════════ # Section 3: Agent work in a draft view — the original change records @@ -120,27 +120,25 @@ _pass "shared view is '$MAIN'" begin_section "Record originals in a draft view" -assert_success "create + switch to draft" \ +require_success "create + switch to draft" \ atomic view create agent --draft --parent "$MAIN" --switch # Two per-turn changes. Their on-disk content must match what the squash tree # will contain, so inserting them reproduces the squash exactly (SPEC §5/D1). create_file "src/one.rs" "fn one() {}" -add_files "src/one.rs" -record_change "feat: one" >/dev/null +require_success "add one.rs" atomic add "src/one.rs" +require_success "record one.rs" record_change "feat: one" create_file "src/two.rs" "fn two() {}" -add_files "src/two.rs" -record_change "feat: two" >/dev/null +require_success "add two.rs" atomic add "src/two.rs" +require_success "record two.rs" record_change "feat: two" # Capture the two originals' hashes (newest-first). The draft's own changes are # the two most recent, regardless of whether inherited history is shown. +require_capture "read draft history" atomic log --view agent -f json +require_capture "extract original change hashes" python3 -c 'import json,sys; rows=json.loads(sys.argv[1]); assert len(rows)>=2 and rows[0]["message"]=="feat: two" and rows[1]["message"]=="feat: one"; print(rows[0]["hash"]); print(rows[1]["hash"])' "$COMMAND_OUTPUT" ORIG=() -while IFS= read -r h; do - [[ -n "$h" ]] && ORIG+=("$h") -done < <(atomic log --view agent -f json 2>/dev/null \ - | grep -o '"hash"[[:space:]]*:[[:space:]]*"[^"]*"' \ - | sed -E 's/.*"([^"]+)"$/\1/') +while IFS= read -r h; do [[ -n "$h" ]] && ORIG+=("$h"); done <<< "$COMMAND_OUTPUT" HASH_TWO="${ORIG[0]:-}" # newest (feat: two) HASH_ONE="${ORIG[1]:-}" # older (feat: one) @@ -162,7 +160,7 @@ begin_section "Simulate squash-merge commit with multi-block trailers" # Return to the shared view so the working copy is the shared baseline; the # draft-only files disappear, then we recreate them as the squash tree. -switch_view "$MAIN" >/dev/null +require_success "switch to shared view" switch_view "$MAIN" # The squash tree == shared baseline + both originals' content. create_file "src/one.rs" "fn one() {}" @@ -207,12 +205,38 @@ assert_target "dry-run flags an insertable atomic squash" dry_run_recommends_ins begin_section "Incremental import: squash → shared view" -MAIN_BEFORE="$(atomic log --view "$MAIN" 2>/dev/null | grep -c '^#' || true)" - -assert_success "incremental import of squash" \ - atomic git import --incremental --branch "$MAIN" --no-vault +require_capture "count shared history before import" atomic log --view "$MAIN" -f json +MAIN_BEFORE="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))')" + +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-review-gate-snapshot-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp src/one.rs "$snapshot_dir/one.rs" +cp src/two.rs "$snapshot_dir/two.rs" +require_capture "read shared history before squash import" atomic log --view "$MAIN" -f json +history_before="$COMMAND_OUTPUT" +require_capture "read draft history before squash import" atomic log --view agent -f json +draft_before="$COMMAND_OUTPUT" +require_capture "read Git HEAD before squash import" git rev-parse HEAD +head_before="$COMMAND_OUTPUT" +if import_out="$(atomic git import --incremental --branch "$MAIN" --no-vault 2>&1)"; then + _pass "incremental import of squash" +else + _fail "incremental import of squash" "$import_out" + assert_success "refused import preserves one.rs bytes" cmp -s "$snapshot_dir/one.rs" src/one.rs + assert_success "refused import preserves two.rs bytes" cmp -s "$snapshot_dir/two.rs" src/two.rs + assert_current_view "refused import preserves shared view" "$MAIN" + require_capture "read shared history after refusal" atomic log --view "$MAIN" -f json + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then _pass "refusal preserves shared history"; else _fail "refusal preserves shared history" "history changed"; fi + require_capture "read draft history after refusal" atomic log --view agent -f json + if [[ "$COMMAND_OUTPUT" == "$draft_before" ]]; then _pass "refusal preserves draft history"; else _fail "refusal preserves draft history" "history changed"; fi + require_capture "read Git HEAD after refusal" git rev-parse HEAD + if [[ "$COMMAND_OUTPUT" == "$head_before" ]]; then _pass "refusal preserves Git HEAD"; else _fail "refusal preserves Git HEAD" "HEAD changed"; fi + _skip "ReviewGate metadata, insertion and draft-deletion checks" "squash import refused" + print_summary || true + exit 1 +fi -switch_view "$MAIN" >/dev/null +require_success "switch to shared view after import" switch_view "$MAIN" # Always-true guarantees (both current and target behavior satisfy these): assert_file_content "shared view materializes one.rs" "src/one.rs" "fn one() {}" @@ -239,7 +263,8 @@ assert_target "shared view contains original two.rs record" main_has_record "$HA # The squash must NOT add a brand-new change record: with insert, the shared # view grows by exactly the two originals (not by a single squash record). -MAIN_AFTER="$(atomic log --view "$MAIN" 2>/dev/null | grep -c '^#' || true)" +require_capture "count shared history after import" atomic log --view "$MAIN" -f json +MAIN_AFTER="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))')" assert_target "shared view grew by exactly the two originals" \ test "$MAIN_AFTER" -eq "$((MAIN_BEFORE + 2))" @@ -257,13 +282,13 @@ begin_section "Target: draft is disposable, provenance survives (SPEC §1)" # Drafts must be deletable without losing the granular history: the shared view # now references the originals, so they stay alive and materializable. -atomic view delete agent --force >/dev/null 2>&1 || \ - atomic view delete agent >/dev/null 2>&1 || true - -assert_target "originals still in shared view after draft deletion" \ - main_has_record "$HASH_ONE" -assert_target "shared view still materializes after draft deletion" \ - shared_view_materializes_two +if main_has_record "$HASH_ONE" && main_has_record "$HASH_TWO"; then + require_success "delete draft after originals are present" atomic view delete agent --force + assert_target "originals still in shared view after draft deletion" main_has_record "$HASH_ONE" + assert_target "shared view still materializes after draft deletion" shared_view_materializes_two +else + _skip "draft deletion and provenance survival" "originals not both present in shared view" +fi # ── Summary ──────────────────────────────────────────────────────────────── diff --git a/tests/harness/35_git_shim_serialization.sh b/tests/harness/35_git_shim_serialization.sh new file mode 100755 index 00000000..f1b51d79 --- /dev/null +++ b/tests/harness/35_git_shim_serialization.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# Deterministic common-lock exclusion plus repeat-convergence smoke coverage. +HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" +source "$HARNESS_DIR/helpers.sh" +source "$HARNESS_DIR/shim_helpers.sh" +require_git +begin_section "Held cross-process operation lock excludes shim mutation" +shim_fixture shim-serialization +shim_refusal "common lock" 'lock|busy|contention|retry|another' shim_locked_command git shim reconcile +shim_clean "after lock release" + +begin_section "Two reconcile callers converge without double publication" +HEAD_BEFORE="$(git_head_sha_full)" +COUNT_BEFORE="$(git_commit_count)" +evidence="$(mktemp -d "${TMPDIR:-/tmp}/atomic-shim-callers-XXXXXX")" +_HARNESS_TMPDIRS+=("$evidence") +atomic git shim reconcile > "$evidence/one" 2>&1 & first=$! +atomic git shim reconcile > "$evidence/two" 2>&1 & second=$! +for item in "$first:one" "$second:two"; do + pid="${item%%:*}"; name="${item#*:}" + if wait "$pid"; then + _pass "$name: reconcile succeeded" + elif grep -qiE 'lock|busy|contention|retry|another' "$evidence/$name"; then + _pass "$name: typed retry under contention" + else + _fail "$name: succeeds or requests retry" "$(cat "$evidence/$name")" + fi +done +require_success "reconcile converges after callers" atomic git shim reconcile +require_capture "read final commit count" git_commit_count +if [[ "$COMMAND_OUTPUT" == "$COUNT_BEFORE" && "$(git_head_sha_full)" == "$HEAD_BEFORE" ]]; then + _pass "concurrent no-op callers create no commits" +else + _fail "concurrent no-op callers create no commits" "Git history changed" +fi +shim_clean "converged projection" +print_summary diff --git a/tests/harness/39_ambient_inode_views.sh b/tests/harness/39_ambient_inode_views.sh index ff929fd0..8765612d 100755 --- a/tests/harness/39_ambient_inode_views.sh +++ b/tests/harness/39_ambient_inode_views.sh @@ -38,26 +38,26 @@ begin_section "Descendant draft inherits parent file identity" make_temp_repo ambient-inode-descendant init_repo -new_view ab12 --draft --parent dev --switch >/dev/null +require_success "create parent draft" new_view ab12 --draft --parent dev --switch printf 'from-ab12\n' > f.txt -add_files f.txt >/dev/null -record_change "ab12 adds f.txt" >/dev/null +require_success "add parent file" atomic add f.txt +require_success "record parent file" record_change "ab12 adds f.txt" -new_view cd23 --draft --parent ab12 --switch >/dev/null +require_success "create child draft" new_view cd23 --draft --parent ab12 --switch assert_file_content "cd23 materializes ab12's inherited file" f.txt "from-ab12" assert_clean "cd23 starts clean with inherited file" -ADD_OUTPUT="$(atomic add f.txt 2>&1)" +require_success "add inherited file" atomic add f.txt assert_clean "adding inherited file is idempotent" assert_file_content "idempotent add preserves inherited content" f.txt "from-ab12" printf 'from-cd23\n' > f.txt -record_change "cd23 edits inherited f.txt" >/dev/null +require_success "record child edit" record_change "cd23 edits inherited f.txt" assert_file_content "cd23 records against inherited file" f.txt "from-cd23" -switch_view ab12 >/dev/null +require_success "switch to parent" switch_view ab12 assert_file_content "parent remains at its own visible generation" f.txt "from-ab12" -switch_view cd23 >/dev/null +require_success "switch to child" switch_view cd23 assert_file_content "child restores its edit on the same inherited file" f.txt "from-cd23" # ── Case 2: selecting ambient graph data does not rewrite TREE history ────── @@ -65,23 +65,29 @@ begin_section "Cross-view insertion is closure-only and idempotent" make_temp_repo ambient-inode-insert init_repo -new_view feature --draft --parent dev --switch >/dev/null +require_success "create feature draft" new_view feature --draft --parent dev --switch printf 'ambient content\n' > ambient.txt -add_files ambient.txt >/dev/null -record_change "feature adds ambient.txt" >/dev/null +require_success "add ambient file" atomic add ambient.txt +require_success "record ambient file" record_change "feature adds ambient.txt" JOURNAL_BEFORE="$(journal_fingerprint)" -switch_view dev >/dev/null -insert_from_view feature dev >/dev/null +require_success "switch to dev" switch_view dev +require_success "insert ambient file" insert_from_view feature dev JOURNAL_AFTER_FIRST="$(journal_fingerprint)" -assert_equal_value "first insert does not append snapshot-derived TREE ops" \ - "$JOURNAL_BEFORE" "$JOURNAL_AFTER_FIRST" +if [[ "$JOURNAL_BEFORE" == missing ]]; then + _skip "first insert does not append snapshot-derived TREE ops" "legacy deferred TREE journal absent" +else + assert_equal_value "first insert does not append snapshot-derived TREE ops" "$JOURNAL_BEFORE" "$JOURNAL_AFTER_FIRST" +fi assert_file_content "dev materializes selected ambient file" ambient.txt "ambient content" -insert_from_view feature dev >/dev/null +require_success "repeat ambient insertion" insert_from_view feature dev JOURNAL_AFTER_SECOND="$(journal_fingerprint)" -assert_equal_value "repeated insert leaves TREE lifecycle metadata unchanged" \ - "$JOURNAL_AFTER_FIRST" "$JOURNAL_AFTER_SECOND" +if [[ "$JOURNAL_AFTER_FIRST" == missing ]]; then + _skip "repeated insert leaves TREE lifecycle metadata unchanged" "legacy deferred TREE journal absent" +else + assert_equal_value "repeated insert leaves TREE lifecycle metadata unchanged" "$JOURNAL_AFTER_FIRST" "$JOURNAL_AFTER_SECOND" +fi assert_file_content "repeated insert preserves content" ambient.txt "ambient content" assert_clean "repeated ambient insert leaves dev clean" @@ -90,19 +96,35 @@ begin_section "Sibling same-path creates surface an identity conflict" make_temp_repo ambient-inode-siblings init_repo -new_view bill --draft --parent dev --switch >/dev/null +require_success "create Bill draft" new_view bill --draft --parent dev --switch printf 'from-bill\n' > f.txt -add_files f.txt >/dev/null -record_change "bill creates f.txt" >/dev/null +require_success "add Bill file" atomic add f.txt +require_success "record Bill file" record_change "bill creates f.txt" -switch_view dev >/dev/null -new_view sally --draft --parent dev --switch >/dev/null +require_success "switch to dev" switch_view dev +require_success "create Sally draft" new_view sally --draft --parent dev --switch printf 'from-sally\n' > f.txt -add_files f.txt >/dev/null -record_change "sally creates f.txt" >/dev/null +require_success "add Sally file" atomic add f.txt +require_success "record Sally file" record_change "sally creates f.txt" JOURNAL_BEFORE_CONFLICT="$(journal_fingerprint)" -insert_from_view bill sally >/dev/null 2>&1 || true +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-ambient-snapshot-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp f.txt "$snapshot_dir/f.txt" +require_capture "read Sally history before insert" atomic log --view sally -f json +history_before="$COMMAND_OUTPUT" +if insert_out="$(insert_from_view bill sally 2>&1)"; then + _pass "insert Bill's same-path identity" +else + _fail "insert Bill's same-path identity" "$insert_out" + assert_success "refused insertion preserves Sally bytes" cmp -s "$snapshot_dir/f.txt" f.txt + assert_current_view "refused insertion preserves Sally view" sally + require_capture "read Sally history after refusal" atomic log --view sally -f json + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then _pass "refusal preserves Sally history"; else _fail "refusal preserves Sally history" "history changed"; fi + _skip "same-path conflict and remaining cases" "name-conflict insertion refused" + print_summary || true + exit 1 +fi JOURNAL_AFTER_CONFLICT="$(journal_fingerprint)" assert_equal_value "combining siblings does not invent TREE lifecycle ops" \ "$JOURNAL_BEFORE_CONFLICT" "$JOURNAL_AFTER_CONFLICT" diff --git a/tests/harness/39_git_shim_workflow.sh b/tests/harness/39_git_shim_workflow.sh new file mode 100755 index 00000000..2e332a22 --- /dev/null +++ b/tests/harness/39_git_shim_workflow.sh @@ -0,0 +1,107 @@ +#!/usr/bin/env bash +# Current opt-in shim: Git branch adoption, record projection, draft detach, +# explicit shim switching, idempotence and unenrolled-repository isolation. +HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" +source "$HARNESS_DIR/helpers.sh" +source "$HARNESS_DIR/shim_helpers.sh" +require_git + +begin_section "Enrollment, exact native projection and repeat reconciliation" +shim_fixture shim-workflow +MAIN_HEAD="$(git_head_sha_full)" +printf 'native\n' > tracked.txt +require_success "record projects through the enrolled shim" atomic record -am native +require_capture "read projected HEAD" git rev-parse HEAD +if [[ "$COMMAND_OUTPUT" != "$MAIN_HEAD" ]]; then _pass "record advances Git projection"; else _fail "record advances Git projection" "HEAD unchanged"; fi +require_success "native bytes match Git" shim_assert_blob "native blob" tracked.txt $'native\n' +shim_clean "native record" +REPEAT_HEAD="$(git_head_sha_full)" +require_capture "history before repeated reconcile" atomic log --all --format json +HISTORY="$COMMAND_OUTPUT" +require_success "repeated reconcile" atomic git shim reconcile +assert_output_contains "repeat retains HEAD" "$REPEAT_HEAD" git rev-parse HEAD +require_capture "history after repeated reconcile" atomic log --all --format json +if [[ "$COMMAND_OUTPUT" == "$HISTORY" ]]; then _pass "repeat retains Atomic identities"; else _fail "repeat retains Atomic identities" "history changed"; fi + +begin_section "Git-authored topic branch is explicitly adopted" +require_success "create Git topic" git switch -qc topic +printf 'foreign\n' > tracked.txt +printf 'topic\n' > topic.txt +: > topic-empty.txt +mkdir -p 'nested space' +printf '#!/bin/sh\necho topic\n' > 'nested space/run.sh' +chmod +x 'nested space/run.sh' +ln -s tracked.txt topic-link +require_success "delete topic baseline path" git rm -q base-only.txt +require_success "stage topic" git add tracked.txt topic.txt topic-empty.txt 'nested space/run.sh' topic-link +require_success "commit topic" git commit -qm topic +TOPIC_HEAD="$(git_head_sha_full)" +require_success "adopt Git topic" atomic git shim reconcile +assert_current_view "topic view adopted" topic +assert_output_contains "adoption preserves foreign commit" "$TOPIC_HEAD" git rev-parse HEAD +require_success "topic blob exact" shim_assert_blob "topic bytes" tracked.txt $'foreign\n' +shim_clean "adopted topic" +require_success "return through Git" git switch -q "$SHIM_MAIN" +metadata="$(mktemp -d "${TMPDIR:-/tmp}/atomic-shim-adoption-XXXXXX")" +_HARNESS_TMPDIRS+=("$metadata") +require_success "snapshot Git-rendered files before adoption" shim_tracked_file_metadata "$metadata/before" +require_success "adopt branch return" atomic git shim reconcile +require_success "snapshot Git-rendered files after adoption" shim_tracked_file_metadata "$metadata/after" +assert_success "adoption does not rewrite Git-rendered files (bytes, modes, inodes, mtimes)" cmp -s "$metadata/before" "$metadata/after" +assert_current_view "main view restored" "$SHIM_MAIN" +require_success "main blob exact" shim_assert_blob "main bytes" tracked.txt $'native\n' +assert_file_not_exists "topic-only path absent on main" topic.txt +assert_file_not_exists "topic-local empty file absent on main" topic-empty.txt +assert_file_exists "topic-deleted path restored on main" base-only.txt +printf 'private sentinel\n' > .atomic/bridge/sentinel.txt + +begin_section "Native draft projection is detached, not an invented mirror branch" +MAIN_REF="$(git rev-parse "refs/heads/$SHIM_MAIN")" +require_success "create native draft" atomic view create agent --draft --parent "$SHIM_MAIN" +require_success "switch native draft" atomic view switch agent --force +assert_current_view "agent view current" agent +assert_failure "draft projection detaches HEAD" git symbolic-ref -q HEAD +assert_failure "native switch does not invent branch agent" git show-ref --verify --quiet refs/heads/agent +assert_output_contains "main ref retained" "$MAIN_REF" git rev-parse "refs/heads/$SHIM_MAIN" +printf 'agent\n' > tracked.txt +require_success "record on detached draft" atomic record -am agent +require_success "detached draft blob exact" shim_assert_blob "draft bytes" tracked.txt $'agent\n' +shim_clean "detached draft" +require_success "publish draft branch explicitly (local only)" atomic git shim publish agent --branch agent +assert_success "explicit publication creates branch agent" git show-ref --verify --quiet refs/heads/agent +require_capture "read explicit publication blob" git show refs/heads/agent:tracked.txt +if [[ "$COMMAND_OUTPUT" == agent ]]; then _pass "published branch contains the draft bytes"; else _fail "published branch contains the draft bytes" "$COMMAND_OUTPUT"; fi +require_success "native return to main" atomic view switch "$SHIM_MAIN" --force +require_success "main bytes retained" shim_assert_blob "main after draft" tracked.txt $'native\n' +require_success "explicit shim switch to known topic" atomic git shim switch topic +assert_current_view "explicit switch selects topic" topic +assert_output_contains "explicit shim switch attaches topic" refs/heads/topic git symbolic-ref HEAD +require_success "topic materialized exactly" shim_assert_blob "topic after switch" tracked.txt $'foreign\n' +assert_file_exists "topic-only path restored" topic.txt +assert_file_not_exists "topic deletion survives round-trip" base-only.txt +assert_file_exists "topic-local zero-byte file restored" topic-empty.txt +assert_success "topic-local file stays empty" test ! -s topic-empty.txt +assert_success "topic executable mode restored" test -x 'nested space/run.sh' +assert_success "topic symlink kind restored" test -L topic-link +assert_output_contains "topic symlink target exact" tracked.txt readlink topic-link +assert_success "private sentinel survives switches byte-exactly" shim_file_bytes .atomic/bridge/sentinel.txt $'private sentinel\n' +assert_file_exists "zero-byte file survives switching" empty.txt +assert_success "zero-byte file remains empty" test ! -s empty.txt +shim_clean "explicit switch" +require_success "repeat explicit switch to main" atomic git shim switch "$SHIM_MAIN" +require_success "repeat explicit switch to topic" atomic git shim switch topic +require_success "topic bytes survive second round-trip" shim_assert_blob "second topic projection" tracked.txt $'foreign\n' +assert_success "private sentinel survives second round-trip byte-exactly" shim_file_bytes .atomic/bridge/sentinel.txt $'private sentinel\n' +shim_clean "second round-trip" + +begin_section "Unenrolled native repositories do not acquire shim branch behavior" +make_temp_repo shim-unenrolled +init_git_repo +git_commit initial tracked.txt base +BEFORE="$(git_head_sha_full)" +require_success "native init without Git import" atomic init --no-vault +require_success "create unenrolled draft" atomic view create other +require_success "switch unenrolled draft" atomic view switch other --force +assert_output_contains "unenrolled switch preserves Git HEAD" "$BEFORE" git rev-parse HEAD +assert_failure "unenrolled switch creates no mirror branch" git show-ref --verify --quiet refs/heads/other +print_summary diff --git a/tests/harness/40_git_shim_safety.sh b/tests/harness/40_git_shim_safety.sh new file mode 100755 index 00000000..dcf9c351 --- /dev/null +++ b/tests/harness/40_git_shim_safety.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# Current shim safety: exact-content refusal, private paths, stale claims, +# collisions and pre-materialize failure. No legacy shadow log/branch contract. +HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" +source "$HARNESS_DIR/helpers.sh" +source "$HARNESS_DIR/shim_helpers.sh" +require_git + +begin_section "Dirty bytes and conflict markers are never published implicitly" +shim_fixture shim-safety +printf 'unrecorded\n' > tracked.txt +shim_refusal "dirty reconcile" 'tracked.txt|dirty|uncommitted|working|staged or unstaged' --allow-wip-capture atomic git shim reconcile +printf 'base\n' > tracked.txt +marker_gt='>>>>>>>'; marker_eq='======='; marker_lt='<<<<<<<' +printf 'base\n%s 1\nleft\n%s 1 [C2YTBAHQ]\nright\n%s 1\n' "$marker_gt" "$marker_eq" "$marker_lt" > tracked.txt +shim_refusal "marker record" 'marker|conflict|tracked.txt' atomic record -am markers +shim_refusal "marker reconcile" 'marker|conflict|tracked.txt|dirty|working' atomic git shim reconcile +# Legacy --allow-conflict-markers is not a promise to bypass the enrolled +# shim's snapshot/publication policy. The ordinary refusal is tested above; +# the native-only explicit override remains covered in suite 30. +printf 'resolved\n' > tracked.txt +require_success "record resolved content" atomic record -am resolved +require_success "resolved projection exact" shim_assert_blob "resolved blob" tracked.txt $'resolved\n' +shim_clean "after resolution" + +begin_section "Private metadata never enters an ordinary projection" +require_capture "read projected paths" git ls-tree -r --name-only HEAD +if printf '%s\n' "$COMMAND_OUTPUT" | grep -qE '^\.atomic(/|$)|^\.atomicignore$'; then + _fail "repository database and ignore policy excluded" "$COMMAND_OUTPUT" +else + _pass "repository database and ignore policy excluded" +fi +require_success "stage excluded ignore policy deliberately" git add -f .atomicignore +shim_refusal "tracked private-path verification" 'atomicignore|exclu|private|index' atomic git shim verify +require_success "unstage fixture private policy without deleting bytes" git restore --staged .atomicignore +shim_clean "private policy retained locally" +mkdir -p .vault +printf 'private intent evidence\n' > .vault/private-note.md +require_success "stage private vault note deliberately" git add -f .vault/private-note.md +shim_refusal "private vault index verification" 'vault|exclu|private|index' atomic git shim verify +require_success "unstage private vault note without deleting it" git restore --staged .vault/private-note.md +assert_success "private vault bytes survive refusal" shim_file_bytes .vault/private-note.md $'private intent evidence\n' +rm .vault/private-note.md +rmdir .vault + +begin_section "Forged provenance and stale Git baseline refuse without publication" +require_capture "read coherent tip" git rev-parse HEAD +COHERENT="$COMMAND_OUTPUT" +require_success "create untrusted claim fixture" git -c core.hooksPath=/dev/null commit --allow-empty -qm $'Untrusted fixture\n\nAtomic-Changes: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +DRIFT="$(git_head_sha_full)" +shim_refusal "stale-baseline record" 'reconcil|baseline|HeadChanged|unanchored' atomic record -am refused +shim_refusal "forged-claim reconcile" 'unverif|forg|bare Atomic|provenance' atomic git shim reconcile +require_success "restore only fixture ref using expected-old lease" git update-ref "refs/heads/$SHIM_MAIN" "$COHERENT" "$DRIFT" +require_success "reconcile restored fixture" atomic git shim reconcile +shim_clean "after forged claim refusal" + +begin_section "Switch collisions preserve exact path types and private bytes" +require_success "create Git feature branch" git switch -qc feature +printf 'target\n' > collision.txt +mkdir nested +printf 'child\n' > nested/child.txt +printf 'regular target\n' > directory-target +require_success "stage feature paths" git add collision.txt nested/child.txt directory-target +require_success "commit feature" git commit -qm feature +require_success "adopt feature" atomic git shim reconcile +require_success "switch to main" atomic git shim switch "$SHIM_MAIN" +printf 'collision.txt\nnested\ndirectory-target/\n' >> .git/info/exclude +printf 'private user bytes\n' > collision.txt +shim_refusal "untracked regular file" 'collision|untracked|collision.txt' --allow-wip-capture atomic git shim switch feature +rm collision.txt +printf 'private parent\n' > nested +shim_refusal "untracked parent file" 'collision|untracked|nested|parent' --allow-wip-capture atomic git shim switch feature +rm nested +mkdir directory-target +printf 'private child\n' > directory-target/private.txt +shim_refusal "directory versus file" 'collision|untracked|directory-target|directory' --allow-wip-capture atomic git shim switch feature +rm directory-target/private.txt +rmdir directory-target +shim_refusal "before-materialize failpoint" 'before.materializ|ATOMIC_TEST_SHIM_FAIL_BEFORE_MATERIALIZE' --allow-wip-capture env ATOMIC_TEST_SHIM_FAIL_BEFORE_MATERIALIZE=1 "$ATOMIC_BIN" git shim switch feature +require_success "switch retries successfully" atomic git shim switch feature +assert_success "target collision file exact" shim_file_bytes collision.txt $'target\n' +assert_success "target nested file exact" shim_file_bytes nested/child.txt $'child\n' +assert_success "target regular file exact" shim_file_bytes directory-target $'regular target\n' +shim_clean "after successful retry" +require_success "return to main" atomic git shim switch "$SHIM_MAIN" +assert_file_not_exists "feature file removed on return" collision.txt +assert_file_not_exists "feature nested file removed on return" nested/child.txt +shim_clean "after collision round-trip" +print_summary diff --git a/tests/harness/40_switch_transactionality.sh b/tests/harness/40_switch_transactionality.sh index 32bca180..8bda359e 100755 --- a/tests/harness/40_switch_transactionality.sh +++ b/tests/harness/40_switch_transactionality.sh @@ -11,31 +11,45 @@ init_repo printf 'cache/\n' > .atomicignore printf 'source view\n' > source.txt -add_files .atomicignore source.txt >/dev/null -record_change "add source file and ignore rules" >/dev/null +require_success "add source and ignore rules" atomic add .atomicignore source.txt +require_success "record source and ignore rules" record_change "add source file and ignore rules" mkdir -p cache printf 'source cache\n' > cache/state.txt -SOURCE_VIEW="$(current_view)" +require_capture "read source view" current_view +SOURCE_VIEW="$COMMAND_OUTPUT" -new_view target --draft --parent "$SOURCE_VIEW" --switch >/dev/null +require_success "create and switch to target" new_view target --draft --parent "$SOURCE_VIEW" --switch mkdir -p nested printf 'target view\n' > nested/target.txt -add_files nested/target.txt >/dev/null -record_change "add target file" >/dev/null +require_success "add target file" atomic add nested/target.txt +require_success "record target file" record_change "add target file" -switch_view "$SOURCE_VIEW" >/dev/null +require_success "switch to source" switch_view "$SOURCE_VIEW" assert_file_not_exists "target-only file is absent on source" nested/target.txt assert_file_content "source file is intact before failed switch" source.txt "source view" assert_file_content "source ignored workspace is present before failure" cache/state.txt "source cache" -assert_failure "switch fails after publishing but before materialization" \ - env ATOMIC_TEST_FAIL_SWITCH_AFTER_PUBLISH=1 "$ATOMIC_BIN" view switch target --force +if fault_output="$(env ATOMIC_TEST_FAIL_SWITCH_AFTER_PUBLISH=1 "$ATOMIC_BIN" view switch target --force 2>&1)"; then + _fail "switch fails after publishing but before materialization" "failpoint unexpectedly succeeded: $fault_output" + _skip "publication rollback and later recovery checks" "failpoint did not fire" + print_summary || true + exit 1 +else + if [[ "$fault_output" == *"injected switch failure after view publication"* ]]; then + _pass "switch fails after publishing but before materialization" + else + _fail "switch fails after publishing but before materialization" "wrong failure: $fault_output" + _skip "publication rollback and later recovery checks" "requested failpoint did not fire" + print_summary || true + exit 1 + fi +fi # The failed command exits with a durable recovery marker. The next command # opens the repository writable and must roll the whole transition back. assert_file_exists "failed switch leaves durable recovery marker" \ .atomic/deferred-tree-alignment.pending -assert_success "next writable open performs switch recovery" atomic add source.txt +require_success "next writable open performs switch recovery" atomic add source.txt assert_equal_value() { local desc="$1" expected="$2" actual="$3" if [[ "$expected" == "$actual" ]]; then @@ -44,34 +58,44 @@ assert_equal_value() { _fail "$desc" "expected '$expected', got '$actual'" fi } -assert_equal_value "reopen restores source as current view" \ - "$SOURCE_VIEW" "$(cat .atomic/current_view)" +assert_current_view "reopen restores source as current view" "$SOURCE_VIEW" assert_file_not_exists "successful recovery clears pending marker" \ .atomic/deferred-tree-alignment.pending assert_file_content "source tracked content survives rollback" source.txt "source view" assert_file_content "source ignored workspace is restored by rollback" cache/state.txt "source cache" -assert_success "switch succeeds after obstruction is removed" \ +require_success "switch succeeds after obstruction is removed" \ atomic view switch target --force -assert_equal_value "target becomes current after successful switch" \ - target "$(cat .atomic/current_view)" +assert_current_view "target becomes current after successful switch" target assert_file_content "target file materializes after successful retry" nested/target.txt "target view" assert_file_content "inherited source file remains present" source.txt "source view" assert_file_not_exists "successful switch leaves no recovery marker" \ .atomic/deferred-tree-alignment.pending begin_section "Recovery removes a partially materialized target view" -switch_view "$SOURCE_VIEW" >/dev/null +require_success "switch back to source" switch_view "$SOURCE_VIEW" assert_file_not_exists "target-only file is removed before mixed-state test" nested/target.txt -assert_failure "switch fails after target files are materialized" \ - env ATOMIC_TEST_FAIL_SWITCH_AFTER_MATERIALIZE=1 "$ATOMIC_BIN" view switch target --force +if fault_output="$(env ATOMIC_TEST_FAIL_SWITCH_AFTER_MATERIALIZE=1 "$ATOMIC_BIN" view switch target --force 2>&1)"; then + _fail "switch fails after target files are materialized" "failpoint unexpectedly succeeded: $fault_output" + _skip "partial-materialization recovery checks" "failpoint did not fire" + print_summary || true + exit 1 +else + if [[ "$fault_output" == *"injected switch failure after materialization"* ]]; then + _pass "switch fails after target files are materialized" + else + _fail "switch fails after target files are materialized" "wrong failure: $fault_output" + _skip "partial-materialization recovery checks" "requested failpoint did not fire" + print_summary || true + exit 1 + fi +fi assert_file_exists "partially materialized target file exists before recovery" nested/target.txt assert_file_exists "post-materialization failure retains recovery marker" \ .atomic/deferred-tree-alignment.pending -assert_success "writable reopen recovers mixed working copy" atomic add source.txt -assert_equal_value "mixed-state recovery restores source current view" \ - "$SOURCE_VIEW" "$(cat .atomic/current_view)" +require_success "writable reopen recovers mixed working copy" atomic add source.txt +assert_current_view "mixed-state recovery restores source current view" "$SOURCE_VIEW" assert_file_not_exists "mixed-state recovery removes target-only tracked file" nested/target.txt assert_file_content "mixed-state recovery restores source content" source.txt "source view" assert_file_content "mixed-state recovery restores ignored workspace" cache/state.txt "source cache" diff --git a/tests/harness/41_git_shim_hooks.sh b/tests/harness/41_git_shim_hooks.sh index 19b8b509..5b865bfd 100755 --- a/tests/harness/41_git_shim_hooks.sh +++ b/tests/harness/41_git_shim_hooks.sh @@ -12,7 +12,9 @@ begin_section "Prerequisites" require_git atomic_current_view() { - atomic view list 2>/dev/null | awk '/^\*/ { print $2 }' + local output + output="$(atomic view list --all --json 2>&1)" || return 1 + printf '%s\n' "$output" | python3 -c 'import json,sys; print(json.load(sys.stdin)["current_view"])' } wait_for_receipt() { @@ -52,15 +54,16 @@ git commit --quiet -m "feature" FEATURE_HEAD="$(git_head_sha_full)" git switch --quiet "$MAIN" -assert_success "initialize Atomic for shim hooks" atomic init --view "$MAIN" -assert_success "enable advisory shim" atomic git shim enable -HOOK_PATH="$(git rev-parse --git-path hooks/post-checkout)" -if [[ -x "$HOOK_PATH" ]] && grep -q '^# atomic:git-shim-dispatcher:v1$' "$HOOK_PATH"; then +require_success "initialize Atomic for shim hooks" atomic init --view "$MAIN" +require_success "enable advisory shim" atomic git shim enable +require_capture "locate post-checkout hook" git rev-parse --git-path hooks/post-checkout +HOOK_PATH="$COMMAND_OUTPUT" +if [[ -x "$HOOK_PATH" ]] && grep -q '^# atomic:git-bridge-dispatcher:v1$' "$HOOK_PATH"; then _pass "shim enable installs an owned executable dispatcher" else _fail "shim enable installs an owned executable dispatcher" "$HOOK_PATH missing or unowned" fi -if grep -q "${ATOMIC_BIN}" "$HOOK_PATH"; then +if grep -qF -- "${HARNESS_REAL_ATOMIC:-$ATOMIC_BIN}" "$HOOK_PATH"; then _pass "dispatcher embeds the absolute Atomic binary" else _fail "dispatcher embeds the absolute Atomic binary" "$(cat "$HOOK_PATH")" @@ -104,10 +107,12 @@ begin_section "Correctness remains independent of hook execution" rm -f "$HOOK_PATH" "$JOURNAL" git switch --quiet "$MAIN" git switch --quiet feature -if [[ -e "$JOURNAL" ]]; then - _fail "disabled hook produces no evidence" "journal unexpectedly recreated" +# Reference-transaction dispatchers remain installed and may legitimately +# recreate the journal; only post-checkout evidence must be absent. +if [[ -f "$JOURNAL" ]] && grep -q '"record_type":"post-checkout"' "$JOURNAL"; then + _fail "disabled checkout hook produces no checkout evidence" "$(cat "$JOURNAL")" else - _pass "disabled hook produces no evidence" + _pass "disabled checkout hook produces no checkout evidence" fi set +e FORENSIC_OUT="$(atomic status --no-reconcile 2>&1)" @@ -140,4 +145,32 @@ else _fail "custom hook owner receives integration instructions" "$CUSTOM_OUT" fi +begin_section "Owned dispatchers install and uninstall without touching user hooks" +make_temp_repo git-shim-hook-ownership +init_git_repo +git_commit initial tracked.txt baseline +require_success "import hook-ownership fixture" atomic git import --no-vault +printf '#!/bin/sh\necho user-owned\n' > .git/hooks/post-checkout +chmod +x .git/hooks/post-checkout +CUSTOM_BYTES="$(shasum -a 256 .git/hooks/post-checkout)" +require_success "install preserves unmanaged post-checkout" atomic git hooks install +assert_output_not_contains "unmanaged hook has no Atomic dispatcher" atomic:git-bridge-dispatcher:v1 cat .git/hooks/post-checkout +for hook in pre-commit pre-push post-rewrite reference-transaction; do + assert_success "$hook is an executable owned dispatcher" test -x ".git/hooks/$hook" + assert_output_contains "$hook carries ownership marker" atomic:git-bridge-dispatcher:v1 cat ".git/hooks/$hook" +done +assert_file_not_exists "legacy post-commit importer absent" .git/hooks/post-commit +assert_file_not_exists "legacy post-merge importer absent" .git/hooks/post-merge +require_success "second install succeeds" atomic git hooks install +require_success "uninstall owned dispatchers" atomic git hooks uninstall +for hook in pre-commit pre-push post-rewrite reference-transaction; do + assert_file_not_exists "$hook removed by uninstall" ".git/hooks/$hook" +done +require_capture "hash unmanaged hook after install/uninstall" shasum -a 256 .git/hooks/post-checkout +if [[ "$COMMAND_OUTPUT" == "$CUSTOM_BYTES" ]]; then + _pass "unmanaged hook bytes retained exactly" +else + _fail "unmanaged hook bytes retained exactly" "$COMMAND_OUTPUT" +fi +assert_success "unmanaged executable mode retained" test -x .git/hooks/post-checkout print_summary diff --git a/tests/harness/41_view_switch_name_conflict.sh b/tests/harness/41_view_switch_name_conflict.sh index c745f6cd..e63e64bf 100755 --- a/tests/harness/41_view_switch_name_conflict.sh +++ b/tests/harness/41_view_switch_name_conflict.sh @@ -30,26 +30,42 @@ make_temp_repo "view-switch-name-conflict" init_repo create_file "seed.txt" "seed content" -assert_success "add seed.txt" atomic add seed.txt -record_change "seed base" >/dev/null 2>&1 || true +require_success "add seed.txt" atomic add seed.txt +require_success "record seed base" record_change "seed base" # ─────────────────────────────────────────────────────────────────────────── begin_section "Repro: two views claim f.txt independently (name conflict)" # ─────────────────────────────────────────────────────────────────────────── -new_view "feature" >/dev/null 2>&1 || true -switch_view "feature" >/dev/null 2>&1 || true +require_success "create feature view" new_view "feature" +require_success "switch to feature" switch_view "feature" -create_file "f.txt" "from-feature first line\nbody line" -assert_success "add f.txt on feature" atomic add f.txt -record_change "feature creates f.txt" >/dev/null 2>&1 || true +create_file "f.txt" $'from-feature first line\nbody line' +require_success "add f.txt on feature" atomic add f.txt +require_success "record feature file" record_change "feature creates f.txt" -switch_view "dev" >/dev/null 2>&1 || true -create_file "f.txt" "from-dev first line\nbody line" -assert_success "add f.txt on dev" atomic add f.txt -record_change "dev creates f.txt" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" +create_file "f.txt" $'from-dev first line\nbody line' +require_success "add f.txt on dev" atomic add f.txt +require_success "record dev file" record_change "dev creates f.txt" # Insert feature → dev: two inodes claim f.txt ⇒ name conflict. -insert_from_view "feature" "dev" >/dev/null 2>&1 || true +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-name-conflict-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp f.txt "$snapshot_dir/f.txt" +require_capture "read dev history before insertion" atomic log --view dev -f json +history_before="$COMMAND_OUTPUT" +if insert_out="$(insert_from_view "feature" "dev" 2>&1)"; then + _pass "insert feature name conflict" +else + _fail "insert feature name conflict" "$insert_out" + assert_success "refusal preserves dev file bytes" cmp -s "$snapshot_dir/f.txt" f.txt + assert_current_view "refusal preserves dev view" dev + require_capture "read dev history after refusal" atomic log --view dev -f json + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then _pass "refusal preserves dev history"; else _fail "refusal preserves dev history" "history changed"; fi + _skip "conflict-marker and child-switch checks" "name-conflict insertion refused" + print_summary || true + exit 1 +fi # The conflict must actually be materialized: markers on disk. if grep -q ">>>>>>>" f.txt 2>/dev/null; then @@ -63,8 +79,8 @@ fi begin_section "Bug: switching to an inheriting child deletes the file" # ─────────────────────────────────────────────────────────────────────────── # Fork from the current view (dev): the child inherits dev's lineage. -new_view "child" >/dev/null 2>&1 || true -switch_view "child" >/dev/null 2>&1 || true +require_success "create child view" new_view "child" +require_success "switch to child" switch_view "child" assert_current_view "now on child" "child" # THE REGRESSION: the file is in the child's inherited state — it must NOT @@ -83,4 +99,4 @@ fi assert_file_exists "seed.txt survives the switch (inherited state)" "seed.txt" # ─────────────────────────────────────────────────────────────────────────── -print_summary \ No newline at end of file +print_summary diff --git a/tests/harness/42_git_shim_guard.sh b/tests/harness/42_git_shim_guard.sh index 90e184be..48fbcdc0 100644 --- a/tests/harness/42_git_shim_guard.sh +++ b/tests/harness/42_git_shim_guard.sh @@ -50,12 +50,15 @@ start_colocated_repo() { create_file tracked.txt "checkpoint\n" git add tracked.txt git commit --no-gpg-sign --quiet -m "checkpoint" - atomic git import --no-vault >/dev/null - atomic git shim reconcile >/dev/null + require_success "$label: import Git" atomic git import --no-vault + require_success "$label: reconcile Git shim" atomic git shim reconcile if [[ -s .atomic/bridge/workspace.json ]]; then _pass "$label: valid shim checkpoint established" else _fail "$label: valid shim checkpoint established" ".atomic/bridge/workspace.json missing" + _skip "guard matrix and subsequent checks" "fixture checkpoint unavailable" + print_summary || true + exit 1 fi } @@ -89,15 +92,40 @@ snapshot_guard_state() { } > "$destination" } +snapshot_operations() { + local scope="$1" destination="$2" + python3 -c ' +import json, subprocess, sys +binary, scope = sys.argv[1:] +args = [binary, "op", "log", "--json"] + (["--repository"] if scope == "repository" else []) +log = json.loads(subprocess.check_output(args, text=True)) +details = [json.loads(subprocess.check_output([binary, "op", "show", entry["id"], "--json"], text=True)) for entry in log["entries"]] +print(json.dumps({"log": log, "details": details}, sort_keys=True))' "$ATOMIC_BIN" "$scope" > "$destination" +} + assert_guard_refusal() { local label="$1" local operation="$2" shift 2 - local before after output rc lines + local before baseline after before_ops after_ops before_repo_ops after_repo_ops output rc lines before="$(mktemp "${TMPDIR:-/tmp}/atomic-cb0c-before-XXXXXX")" + baseline="$(mktemp "${TMPDIR:-/tmp}/atomic-cb0c-baseline-XXXXXX")" after="$(mktemp "${TMPDIR:-/tmp}/atomic-cb0c-after-XXXXXX")" - _HARNESS_TMPDIRS+=("$before" "$after") + _HARNESS_TMPDIRS+=("$before" "$baseline" "$after") + before_ops="$(mktemp "${TMPDIR:-/tmp}/atomic-cb0c-before-ops-XXXXXX")" + after_ops="$(mktemp "${TMPDIR:-/tmp}/atomic-cb0c-after-ops-XXXXXX")" + before_repo_ops="$(mktemp "${TMPDIR:-/tmp}/atomic-cb0c-before-repo-ops-XXXXXX")" + after_repo_ops="$(mktemp "${TMPDIR:-/tmp}/atomic-cb0c-after-repo-ops-XXXXXX")" + _HARNESS_TMPDIRS+=("$before_ops" "$after_ops" "$before_repo_ops" "$after_repo_ops") + require_success "$label: capture working-copy operations before command" snapshot_operations working-copy "$before_ops" + require_success "$label: capture repository operations before command" snapshot_operations repository "$before_repo_ops" + snapshot_guard_state "$baseline" snapshot_guard_state "$before" + if cmp -s "$baseline" "$before"; then + _pass "$label: consecutive snapshots are stable before command" + else + _fail "$label: consecutive snapshots are stable before command" "$(diff -u "$baseline" "$before" || true)" + fi set +e output="$("$@" 2>&1)" @@ -164,19 +192,26 @@ assert_guard_refusal() { else _fail "$label: refusal is non-mutating" "$(diff -u "$before" "$after" || true)" fi + require_success "$label: capture working-copy operations after command" snapshot_operations working-copy "$after_ops" + require_success "$label: capture repository operations after command" snapshot_operations repository "$after_repo_ops" + assert_success "$label: refusal preserves working-copy operation DAG and receipts" cmp -s "$before_ops" "$after_ops" + assert_success "$label: refusal preserves repository operation DAG and receipts" cmp -s "$before_repo_ops" "$after_repo_ops" } begin_section "Every working-copy boundary shares one early refusal" start_colocated_repo "git-shim-guard-matrix" -atomic view create feature --draft --parent main >/dev/null -CHANGE="$(atomic log -f oneline --full-hash | awk 'NR == 1 { print $1 }')" -HISTORY_BEFORE="$(atomic diff --change "$CHANGE" --name-only --no-color)" +require_success "create feature view" atomic view create feature --draft --parent main +require_capture "read latest change hash" atomic log -f json --count 1 +require_capture "extract latest change hash" python3 -c 'import json,sys; rows=json.loads(sys.argv[1]); assert len(rows)==1 and len(rows[0]["hash"])==52; print(rows[0]["hash"])' "$COMMAND_OUTPUT" +CHANGE="$COMMAND_OUTPUT" +require_capture "read historical diff before drift" atomic diff --change "$CHANGE" --name-only --no-color +HISTORY_BEFORE="$COMMAND_OUTPUT" -atomic git shim enable >/dev/null +require_success "enable shim" atomic git shim enable HOOK_PATH="$(git rev-parse --git-path hooks/post-checkout)" rm -f "$HOOK_PATH" raw_git_drift raw-drift -if [[ ! -e .atomic/bridge/git-events.jsonl ]]; then +if [[ ! -e .atomic/bridge/git-events.jsonl ]] || ! grep -q '"record_type":"post-checkout"' .atomic/bridge/git-events.jsonl; then _pass "removing the advisory hook leaves no checkout journal" else _fail "removing the advisory hook leaves no checkout journal" "$(cat .atomic/bridge/git-events.jsonl)" diff --git a/tests/harness/42_view_create_parent.sh b/tests/harness/42_view_create_parent.sh index 0bc29dbf..d3ebcdae 100755 --- a/tests/harness/42_view_create_parent.sh +++ b/tests/harness/42_view_create_parent.sh @@ -23,33 +23,31 @@ make_temp_repo "view-create-parent" init_repo create_file "seed.txt" "seed content" -assert_success "add seed.txt" atomic add seed.txt -record_change "seed base" >/dev/null 2>&1 || true +require_success "add seed.txt" atomic add seed.txt +require_success "record seed base" record_change "seed base" # ─────────────────────────────────────────────────────────────────────────── begin_section "Bug: --parent child is empty, switch wipes the tree" # ─────────────────────────────────────────────────────────────────────────── -$ATOMIC_BIN view create child --parent dev >/dev/null 2>&1 || true +require_success "create --parent child view" atomic view create child --parent dev +assert_view_exists "--parent child view is created" child +require_capture "read child scope and parent" atomic view list --all --json +assert_success "child is a draft parented on dev" python3 -c \ + 'import json,sys; rows=json.loads(sys.argv[1])["views"]; assert any(r["name"]=="child" and r["scope"]=="draft" and r["parent"]=="dev" for r in rows)' "$COMMAND_OUTPUT" -if $ATOMIC_BIN view list -a 2>/dev/null | grep -q "child"; then - _pass "--parent child view is created" -else - _fail "--parent child view is created" "view create failed" -fi - -switch_view "child" >/dev/null 2>&1 || true +require_success "switch to child" switch_view "child" assert_current_view "now on child" "child" # THE REGRESSION: the child inherited dev's state, so switching must keep # the tracked files on disk. -assert_file_exists "seed.txt survives the switch into --parent child" "seed.txt" +assert_file_content "seed.txt survives the switch into --parent child" "seed.txt" "seed content" # ─────────────────────────────────────────────────────────────────────────── begin_section "Round-trip: dev still holds the file after switching back" # ─────────────────────────────────────────────────────────────────────────── -switch_view "dev" >/dev/null 2>&1 || true +require_success "switch to dev" switch_view "dev" assert_current_view "back on dev" "dev" -assert_file_exists "seed.txt is intact back on dev" "seed.txt" +assert_file_content "seed.txt is intact back on dev" "seed.txt" "seed content" # ─────────────────────────────────────────────────────────────────────────── -print_summary \ No newline at end of file +print_summary diff --git a/tests/harness/44_view_split_delete_confirmation.sh b/tests/harness/44_view_split_delete_confirmation.sh index ee5478c8..095b3d4c 100644 --- a/tests/harness/44_view_split_delete_confirmation.sh +++ b/tests/harness/44_view_split_delete_confirmation.sh @@ -53,9 +53,9 @@ assert_file_content "dry-run preserves working content" work.txt "important work assert_failure "split refuses without a terminal or --confirm" \ noninteractive view split wip --last 1 -assert_output_contains "refusal explains how to acknowledge the risks" \ +assert_failed_output_contains "refusal explains how to acknowledge the risks" \ "Re-run with --confirm" noninteractive view split wip --last 1 -assert_output_contains "split warns about later orphaning" \ +assert_failed_output_contains "split warns about later orphaning" \ "Deleting that draft" noninteractive view split wip --last 1 assert_failure "refused split creates no draft" view_exists wip assert_success "refused split retains the source reference" view_has_change dev "$WORK_HASH" @@ -75,13 +75,13 @@ assert_success "return to source" switch_view dev assert_file_not_exists "extracted file is absent from source" work.txt assert_failure "orphaning deletion refuses without a terminal or --force" \ noninteractive view delete wip -assert_output_contains "deletion reports orphan count" \ +assert_failed_output_contains "deletion reports orphan count" \ "would orphan 1 change(s)" noninteractive view delete wip -assert_output_contains "deletion lists the full orphan hash" \ +assert_failed_output_contains "deletion lists the full orphan hash" \ "$WORK_HASH" noninteractive view delete wip -assert_output_contains "deletion gives the explicit acknowledgment flag" \ +assert_failed_output_contains "deletion gives the explicit acknowledgment flag" \ "Re-run with --force" noninteractive view delete wip -assert_output_not_contains "inherited base is not an orphan candidate" \ +assert_failed_output_not_contains "inherited base is not an orphan candidate" \ "$BASE_HASH" noninteractive view delete wip assert_success "refused deletion preserves draft" view_exists wip assert_success "refused deletion preserves its last reference" view_has_change wip "$WORK_HASH" @@ -135,9 +135,9 @@ assert_success "cascade dry-run does not prompt" \ noninteractive view split cascade "$CREATE_HASH" --cascade --dry-run assert_failure "cascade still requires explicit confirmation" \ noninteractive view split cascade "$CREATE_HASH" --cascade -assert_output_contains "warning includes the full cascaded move count" \ +assert_failed_output_contains "warning includes the full cascaded move count" \ "removes 2 change(s)" noninteractive view split cascade "$CREATE_HASH" --cascade -assert_output_contains "warning distinguishes the additional dependent" \ +assert_failed_output_contains "warning distinguishes the additional dependent" \ "1 dependent change(s) included" noninteractive view split cascade "$CREATE_HASH" --cascade assert_success "refused cascade retains creation" view_has_change dev "$CREATE_HASH" assert_success "refused cascade retains dependent edit" view_has_change dev "$EDIT_HASH" diff --git a/tests/harness/helpers.sh b/tests/harness/helpers.sh index 8ccc7ea0..2e156549 100755 --- a/tests/harness/helpers.sh +++ b/tests/harness/helpers.sh @@ -39,6 +39,7 @@ _HARNESS_SUMMARY_PRINTED=0 # Exit code used when an entire suite cannot run because a prerequisite is # unavailable. The top-level runner recognizes 77 as SKIPPED, not PASSED. HARNESS_SKIP_EXIT="${HARNESS_SKIP_EXIT:-77}" +HARNESS_PARTIAL_EXIT="${HARNESS_PARTIAL_EXIT:-78}" # ── Binary discovery ──────────────────────────────────────────────────────── @@ -373,35 +374,68 @@ assert_file_content() { fi } -# Assert that stdout of a command contains a substring. +# Assert that a successful command's output contains a substring. # Usage: assert_output_contains "desc" "substring" command args... assert_output_contains() { local desc="$1" local needle="$2" shift 2 local out - out="$("$@" 2>&1)" || true - if echo "$out" | grep -qF "$needle"; then + if ! out="$("$@" 2>&1)"; then + _fail "$desc" "command failed: $*${out:+ +$out}" + return + fi + if printf '%s\n' "$out" | grep -qF -- "$needle"; then _pass "$desc" else _fail "$desc" "output did not contain '$needle'. Got: $(echo "$out" | head -5)" fi } -# Assert that stdout of a command does NOT contain a substring. +# Assert that a successful command's output does NOT contain a substring. assert_output_not_contains() { local desc="$1" local needle="$2" shift 2 local out - out="$("$@" 2>&1)" || true - if echo "$out" | grep -qF "$needle"; then + if ! out="$("$@" 2>&1)"; then + _fail "$desc" "command failed: $*${out:+ +$out}" + return + fi + if printf '%s\n' "$out" | grep -qF -- "$needle"; then _fail "$desc" "output should not contain '$needle' but does" else _pass "$desc" fi } +# Assertions on diagnostics from a command that must fail. +assert_failed_output_contains() { + local desc="$1" needle="$2" out; shift 2 + if out="$("$@" 2>&1)"; then + _fail "$desc" "expected command failure: $*${out:+ +$out}" + elif printf '%s\n' "$out" | grep -qF -- "$needle"; then + _pass "$desc" + else + _fail "$desc" "failure output did not contain '$needle': $out" + fi +} + +assert_failed_output_not_contains() { + local desc="$1" needle="$2" out; shift 2 + if out="$("$@" 2>&1)"; then + _fail "$desc" "expected command failure: $*${out:+ +$out}" + elif printf '%s\n' "$out" | grep -qF -- "$needle"; then + _fail "$desc" "failure output unexpectedly contained '$needle': $out" + else + _pass "$desc" + fi +} + # Assert that `atomic status` output contains a substring. assert_status_contains() { local desc="$1" @@ -638,7 +672,7 @@ git_merge_branch() { git_add_submodule() { local url="$1" local path="$2" - git submodule add --quiet "$url" "$path" 2>/dev/null + git submodule add --quiet "$url" "$path" || return $? git commit --quiet -m "Add submodule $path" } @@ -675,7 +709,10 @@ assert_change_author() { local ref="$2" local expected="$3" local out - out="$(atomic change "$ref" 2>/dev/null || true)" + if ! out="$(atomic change "$ref" 2>&1)"; then + _fail "$desc" "change command failed: $out" + return + fi if echo "$out" | grep -qiE "author.*$expected"; then _pass "$desc" else @@ -690,7 +727,10 @@ assert_change_message() { local ref="$2" local expected="$3" local out - out="$(atomic change "$ref" 2>/dev/null || true)" + if ! out="$(atomic change "$ref" 2>&1)"; then + _fail "$desc" "change command failed: $out" + return + fi if echo "$out" | grep -qF "$expected"; then _pass "$desc" else @@ -730,5 +770,8 @@ print_summary() { if [[ $TESTS_FAILED -gt 0 ]]; then return 1 fi + if [[ $TESTS_SKIPPED -gt 0 ]]; then + return "$HARNESS_PARTIAL_EXIT" + fi return 0 } diff --git a/tests/harness/helpers_selftest.sh b/tests/harness/helpers_selftest.sh index b3ab5a4d..ac117fdf 100644 --- a/tests/harness/helpers_selftest.sh +++ b/tests/harness/helpers_selftest.sh @@ -44,6 +44,21 @@ run_expect_failure "failed status is not clean" \ run_expect_failure "failed status is not an absent path" \ 'source "$HARNESS_DIR/helpers.sh"; assert_status_no_entry "absent" file; print_summary' \ 'short status command failed' +run_expect_failure "failed command is not a successful output match" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_output_contains "match" "fixture" atomic record; print_summary' \ + 'command failed' +run_expect_failure "failed command is not a successful negative output match" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_output_not_contains "no match" "absent" atomic record; print_summary' \ + 'command failed' +run_expect_failure "expected-failure output assertion rejects success" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_failed_output_not_contains "must fail" "absent" printf ok; print_summary' \ + 'expected command failure' +run_expect_failure "failed change command is reported for author" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_change_author "author" deadbeef Alice; print_summary' \ + 'change command failed: fixture command failed' +run_expect_failure "failed change command is reported for message" \ + 'source "$HARNESS_DIR/helpers.sh"; assert_change_message "message" deadbeef text; print_summary' \ + 'change command failed: fixture command failed' run_expect_failure "non-current view is not current" \ 'source "$HARNESS_DIR/helpers.sh"; assert_current_view "current" dev; print_summary' \ "expected current view 'dev'" @@ -68,10 +83,48 @@ if [[ "$skip_status" -ne 77 ]]; then fi echo "PASS: explicit suite skip stays skipped" +partial_status=0 +HARNESS_EXPECT_SUMMARY=1 bash -c 'source "$HARNESS_DIR/helpers.sh"; _pass "exercised"; _skip "not exercised" "fixture"; print_summary' >/dev/null 2>&1 || partial_status=$? +if [[ "$partial_status" -ne 78 ]]; then + echo "FAIL: partially exercised suite returned $partial_status instead of 78" >&2 + exit 1 +fi +echo "PASS: skipped assertions make a suite partial" + +mkdir -p "$test_root/runner" +cp "$HARNESS_DIR/helpers.sh" "$HARNESS_DIR/run_all.sh" "$test_root/runner/" +printf '%s\n' 'source "$(dirname "$0")/helpers.sh"' '_pass "exercised"' 'print_summary' > "$test_root/runner/01_full.sh" +printf '%s\n' 'source "$(dirname "$0")/helpers.sh"' '_pass "exercised"' '_skip "missing" "fixture"' 'print_summary' > "$test_root/runner/02_partial.sh" +printf '%s\n' 'source "$(dirname "$0")/helpers.sh"' 'skip_suite "fixture"' > "$test_root/runner/03_skip.sh" +runner_status=0 +runner_out="$(bash "$test_root/runner/run_all.sh" 02 2>&1)" || runner_status=$? +if [[ "$runner_status" -ne 78 || "$runner_out" != *"Suite PARTIAL"* || "$runner_out" == *"Suite PASSED"* ]]; then + echo "FAIL: runner misreported partial suite (exit $runner_status): $runner_out" >&2 + exit 1 +fi +runner_status=0 +runner_out="$(bash "$test_root/runner/run_all.sh" 03 2>&1)" || runner_status=$? +if [[ "$runner_status" -ne 77 || "$runner_out" != *"Suite SKIPPED"* || "$runner_out" == *"Suite PASSED"* ]]; then + echo "FAIL: runner misreported skipped suite (exit $runner_status): $runner_out" >&2 + exit 1 +fi +runner_status=0 +runner_out="$(bash "$test_root/runner/run_all.sh" 01 03 2>&1)" || runner_status=$? +if [[ "$runner_status" -ne 78 || "$runner_out" != *"1 passed"* || "$runner_out" != *"1 skipped"* ]]; then + echo "FAIL: runner misreported mixed coverage (exit $runner_status): $runner_out" >&2 + exit 1 +fi +echo "PASS: runner distinguishes full, partial, and skipped suites" + bash -c 'source "$HARNESS_DIR/helpers.sh"; assert_current_view "current" feature; print_summary' >/dev/null +bash -c 'source "$HARNESS_DIR/helpers.sh"; assert_failed_output_contains "expected error" "fixture" atomic record; assert_failed_output_not_contains "not internal" "absent" atomic record; print_summary' >/dev/null bash -c 'source "$HARNESS_DIR/helpers.sh"; assert_view_exists "empty draft" empty; print_summary' >/dev/null run_expect_failure "view names are matched exactly" \ 'source "$HARNESS_DIR/helpers.sh"; assert_view_exists "missing" empt; print_summary' \ "view 'empt' not found" +bash -c 'source "$HARNESS_DIR/helpers.sh"; source "$HARNESS_DIR/merge_helpers.sh"; printf "present\n" > "$test_root/merge-file"; assert_occurrence_count "absent" "$test_root/merge-file" missing 0; print_summary' >/dev/null +run_expect_failure "missing file cannot pass a negative content assertion" \ + 'source "$HARNESS_DIR/helpers.sh"; source "$HARNESS_DIR/merge_helpers.sh"; assert_file_not_contains "missing file" "$test_root/no-file" text; print_summary' \ + 'file does not exist' bash -c 'source "$HARNESS_DIR/helpers.sh"; cd "$test_root"; create_file empty ""; [[ -f empty && ! -s empty ]]; append_file empty ""; [[ ! -s empty ]]; overwrite_file empty "value"; overwrite_file empty ""; [[ ! -s empty ]]' >/dev/null echo "PASS: current nested view and zero-byte file" diff --git a/tests/harness/19_git_shadow.sh b/tests/harness/legacy-shadow/19_git_shadow.sh old mode 100755 new mode 100644 similarity index 69% rename from tests/harness/19_git_shadow.sh rename to tests/harness/legacy-shadow/19_git_shadow.sh index aceb20bd..af553e77 --- a/tests/harness/19_git_shadow.sh +++ b/tests/harness/legacy-shadow/19_git_shadow.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 19_git_shadow.sh — Test harness for Git Shadow Sync +# HISTORICAL: coverage migrated to ../39_git_shim_workflow.sh and ../41_git_shim_hooks.sh. # # Validates the full Atomic ↔ Git sync workflow: # 1. Initial git import @@ -15,11 +15,6 @@ source "$HARNESS_DIR/helpers.sh" # ── Helper functions ────────────────────────────────────────────────── -# Count tags in current view -count_tags() { - atomic tag list 2>/dev/null | grep -c "^" || echo "0" -} - # Get the git HEAD sha get_git_sha() { git rev-parse HEAD 2>/dev/null @@ -56,15 +51,21 @@ git_commit "Initial commit" "README.md" "# Hello" git_commit "Add feature" "src/main.rs" "fn main() {}" git_commit "Add tests" "tests/test.rs" "fn test() {}" -assert_success "git import succeeds" atomic git import --no-vault +require_success "git import succeeds" atomic git import --no-vault # Count log entries (lines starting with #N) count_log_entries() { - atomic log 2>/dev/null | grep -c '^#' || true + local out + if ! out="$(atomic log --format json "$@" 2>&1)"; then + printf 'log failed: %s\n' "$out" >&2 + return 1 + fi + printf '%s\n' "$out" | python3 -c 'import json,sys; entries=json.load(sys.stdin); assert isinstance(entries,list); print(len(entries))' } # Verify atomic log shows at least 3 changes (git commits + .atomicignore) -INITIAL_COUNT=$(count_log_entries) +require_capture "count initial imported changes" count_log_entries +INITIAL_COUNT="$COMMAND_OUTPUT" if [ "$INITIAL_COUNT" -ge 3 ]; then _pass "changes imported (count=$INITIAL_COUNT, expected >=3)" else @@ -74,10 +75,11 @@ fi # Verify GIT_SHA_INDEX was populated — incremental should find nothing new # (all SHAs already indexed) git_commit "Fourth commit" "src/lib.rs" "pub fn lib() {}" -assert_success "incremental import succeeds" atomic git import --incremental +require_success "incremental import succeeds" atomic git import --incremental # Should now have initial + 1 more change -NEW_COUNT=$(count_log_entries) +require_capture "count incremental changes" count_log_entries +NEW_COUNT="$COMMAND_OUTPUT" EXPECTED=$((INITIAL_COUNT + 1)) if [ "$NEW_COUNT" -ge "$EXPECTED" ]; then _pass "incremental added 1 change (count=$NEW_COUNT, expected >=$EXPECTED)" @@ -95,15 +97,15 @@ make_temp_repo "git-push" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import" atomic git import --no-vault +require_success "git import" atomic git import --no-vault # Make changes in Atomic create_file "src/app.rs" "fn app() { println!(\"hello\"); }" -add_files "src/app.rs" -record_change "feat: add app module" +require_success "add app module" atomic add "src/app.rs" +require_success "record app module" record_change "feat: add app module" # Push to git -assert_success "git push succeeds" atomic git push --no-push -m "Sync: add app module" +require_success "create local Git shadow commit without pushing" atomic git push --no-push -m "Sync: add app module" # Verify git log has the commit with trailers assert_output_contains "commit has Atomic-Changes trailer" "Atomic-Changes:" git log -1 --format=%B @@ -131,25 +133,26 @@ DEFAULT_BRANCH=$(git_default_branch) git_commit "Initial release" "README.md" "# v1.0" # 2. Create dev branch with multiple commits -git checkout -b dev 2>/dev/null +require_success "create Git dev branch" git checkout --quiet -b dev git_commit "feat: auth" "src/auth.rs" "fn login() {}" git_commit "feat: api" "src/api.rs" "fn handler() {}" git_commit "test: auth tests" "tests/auth.rs" "fn test_login() {}" # 3. Switch back and create squash merge (GitHub format) -git checkout "$DEFAULT_BRANCH" 2>/dev/null -git merge --squash dev 2>/dev/null -git commit -m "Add auth and API (#42) +require_success "return to default Git branch" git checkout --quiet "$DEFAULT_BRANCH" +require_success "squash Git dev branch" git merge --quiet --squash dev +require_success "record Git squash commit" git commit --quiet -m "Add auth and API (#42) * feat: auth * feat: api -* test: auth tests" 2>/dev/null +* test: auth tests" # Now import everything at once — main branch has: initial + squash commit -assert_success "import with squash" atomic git import --no-vault +require_success "import with squash" atomic git import --no-vault # Verify changes were imported -LOG_COUNT=$(count_log_entries) +require_capture "count squash history" count_log_entries +LOG_COUNT="$COMMAND_OUTPUT" if [ "$LOG_COUNT" -ge 2 ]; then _pass "squash commit imported (count=$LOG_COUNT)" else @@ -157,12 +160,13 @@ else fi # Check for ReviewGate tag — classification runs during import -TAG_OUTPUT=$(atomic tag list 2>/dev/null || echo "") +require_capture "list squash tags" atomic tag list +TAG_OUTPUT="$COMMAND_OUTPUT" if echo "$TAG_OUTPUT" | grep -qi "pr-42\|review\|squash\|merge"; then _pass "ReviewGate tag created for squash merge" else # Classification only runs in --incremental mode, not on initial import - _pass "squash commit imported (classification runs on --incremental)" + _skip "ReviewGate tag for squash merge" "initial import did not produce a classification tag" fi # ════════════════════════════════════════════════════════════════════════ @@ -177,16 +181,17 @@ init_git_repo # Build entire git history before Atomic import git_commit "Initial" "README.md" "# Project" DEFAULT_BRANCH=$(git_default_branch) -git checkout -b feature 2>/dev/null +require_success "create Git feature branch" git checkout --quiet -b feature git_commit "feat: widget" "src/widget.rs" "fn widget() {}" -git checkout "$DEFAULT_BRANCH" 2>/dev/null -git merge --no-ff feature -m "Merge branch 'feature'" 2>/dev/null +require_success "return to default Git branch" git checkout --quiet "$DEFAULT_BRANCH" +require_success "create Git merge commit" git merge --quiet --no-ff feature -m "Merge branch 'feature'" # Import all at once -assert_success "import with merge" atomic git import --no-vault +require_success "import with merge" atomic git import --no-vault # Verify changes were imported (initial + feature + merge) -LOG_COUNT=$(count_log_entries) +require_capture "count merge history" count_log_entries +LOG_COUNT="$COMMAND_OUTPUT" if [ "$LOG_COUNT" -ge 2 ]; then _pass "merge commit imported (count=$LOG_COUNT)" else @@ -202,31 +207,37 @@ begin_section "Git hooks: install, status, uninstall" make_temp_repo "git-hooks" init_git_repo git_commit "Initial" "README.md" "# Hooks test" -assert_success "import" atomic git import --no-vault +require_success "import" atomic git import --no-vault # Install hooks -assert_success "hooks install" atomic git hooks install +require_success "hooks install" atomic git hooks install -# Verify hooks were created -assert_file_exists "post-commit hook exists" ".git/hooks/post-commit" -assert_file_exists "post-merge hook exists" ".git/hooks/post-merge" +# Verify the supported advisory dispatchers were created. Legacy import hooks +# are intentionally removed rather than automatically importing in a hook. +assert_file_exists "post-checkout hook exists" ".git/hooks/post-checkout" +assert_file_exists "pre-commit hook exists" ".git/hooks/pre-commit" +assert_file_exists "pre-push hook exists" ".git/hooks/pre-push" assert_file_exists "post-rewrite hook exists" ".git/hooks/post-rewrite" +assert_file_exists "reference-transaction hook exists" ".git/hooks/reference-transaction" +assert_file_not_exists "legacy post-commit hook absent" ".git/hooks/post-commit" +assert_file_not_exists "legacy post-merge hook absent" ".git/hooks/post-merge" # Verify hook content has our markers -assert_output_contains "post-commit has marker" "atomic:git:begin" cat .git/hooks/post-commit -assert_output_contains "post-commit calls import" "atomic git import --incremental" cat .git/hooks/post-commit +assert_output_contains "post-checkout has dispatcher marker" "atomic:git-bridge-dispatcher:v1" cat .git/hooks/post-checkout +assert_output_contains "post-checkout calls advisory shim" "git shim hook-post-checkout" cat .git/hooks/post-checkout # Status should show installed assert_output_contains "status shows installed" "installed" atomic git hooks status # Idempotent install -assert_success "second install is idempotent" atomic git hooks install +require_success "second install is idempotent" atomic git hooks install # Uninstall -assert_success "hooks uninstall" atomic git hooks uninstall +require_success "hooks uninstall" atomic git hooks uninstall # Verify hooks were removed -assert_file_not_exists "post-commit hook removed" ".git/hooks/post-commit" +assert_file_not_exists "post-checkout hook removed" ".git/hooks/post-checkout" +assert_file_not_exists "post-rewrite hook removed" ".git/hooks/post-rewrite" # Status should show not installed assert_output_contains "status shows not installed" "not installed" atomic git hooks status @@ -240,23 +251,26 @@ begin_section "Git hooks: preserve existing hook content" make_temp_repo "hooks-preserve" init_git_repo git_commit "Initial" "README.md" "# Test" -assert_success "import" atomic git import --no-vault +require_success "import" atomic git import --no-vault -# Create a pre-existing hook +# Create a pre-existing hook. Atomic must not alter an unmanaged dispatcher. mkdir -p .git/hooks echo '#!/bin/sh -echo "my custom hook"' > .git/hooks/post-commit -chmod +x .git/hooks/post-commit +echo "my custom hook"' > .git/hooks/post-checkout +chmod +x .git/hooks/post-checkout -# Install should append, not overwrite -assert_success "install with existing hook" atomic git hooks install -assert_output_contains "existing content preserved" "my custom hook" cat .git/hooks/post-commit -assert_output_contains "atomic section added" "atomic:git:begin" cat .git/hooks/post-commit +# Install leaves the unmanaged hook intact and gives integration instructions. +require_capture "install with existing hook" atomic git hooks install +assert_output_contains "unmanaged hook reported" "left untouched" printf '%s\n' "$COMMAND_OUTPUT" +assert_file_content "existing hook content preserved exactly" ".git/hooks/post-checkout" '#!/bin/sh +echo "my custom hook"' +assert_output_not_contains "no dispatcher added to unmanaged hook" "atomic:git-bridge-dispatcher:v1" cat .git/hooks/post-checkout # Uninstall should remove only atomic section -assert_success "uninstall preserves custom" atomic git hooks uninstall -assert_output_contains "custom content still there" "my custom hook" cat .git/hooks/post-commit -assert_output_not_contains "atomic section removed" "atomic:git:begin" cat .git/hooks/post-commit +require_success "uninstall preserves custom" atomic git hooks uninstall +assert_file_content "custom hook still preserved exactly" ".git/hooks/post-checkout" '#!/bin/sh +echo "my custom hook"' +assert_output_not_contains "no dispatcher added after uninstall" "atomic:git-bridge-dispatcher:v1" cat .git/hooks/post-checkout # ════════════════════════════════════════════════════════════════════════ # Section 8: Incremental import idempotency @@ -269,8 +283,9 @@ init_git_repo git_commit "First" "a.txt" "alpha" git_commit "Second" "b.txt" "beta" -assert_success "initial import" atomic git import --no-vault -BASE_COUNT=$(count_log_entries) +require_success "initial import" atomic git import --no-vault +require_capture "count initial history" count_log_entries +BASE_COUNT="$COMMAND_OUTPUT" if [ "$BASE_COUNT" -ge 2 ]; then _pass "initial import (count=$BASE_COUNT, expected >=2)" else @@ -278,8 +293,9 @@ else fi # Run incremental with no new commits — should be a no-op -assert_success "incremental no-op" atomic git import --incremental -NOOP_COUNT=$(count_log_entries) +require_success "incremental no-op" atomic git import --incremental +require_capture "count history after no-op" count_log_entries +NOOP_COUNT="$COMMAND_OUTPUT" if [ "$NOOP_COUNT" -eq "$BASE_COUNT" ]; then _pass "no-op: count unchanged ($NOOP_COUNT)" else @@ -288,8 +304,9 @@ fi # Add a commit and import again git_commit "Third" "c.txt" "gamma" -assert_success "incremental with new commit" atomic git import --incremental -NEW_COUNT=$(count_log_entries) +require_success "incremental with new commit" atomic git import --incremental +require_capture "count history after new commit" count_log_entries +NEW_COUNT="$COMMAND_OUTPUT" EXPECTED=$((BASE_COUNT + 1)) if [ "$NEW_COUNT" -eq "$EXPECTED" ]; then _pass "added 1 change ($NEW_COUNT)" @@ -298,8 +315,9 @@ else fi # Run incremental again — should be a no-op again -assert_success "incremental no-op again" atomic git import --incremental -FINAL_COUNT=$(count_log_entries) +require_success "incremental no-op again" atomic git import --incremental +require_capture "count history after second no-op" count_log_entries +FINAL_COUNT="$COMMAND_OUTPUT" if [ "$FINAL_COUNT" -eq "$NEW_COUNT" ]; then _pass "no-op again: count unchanged ($FINAL_COUNT)" else @@ -320,18 +338,20 @@ git_commit "Shared draft baseline" "shared.txt" "parent shared" git_commit "Rename baseline" "rename-me.txt" "rename baseline" PRIMARY_BRANCH=$(git_default_branch) -assert_success "initial import for draft regression" atomic git import --no-vault -PRIMARY_COUNT_BEFORE=$(atomic log --view "$PRIMARY_BRANCH" 2>/dev/null | grep -c '^#' || true) +require_success "initial import for draft regression" atomic git import --no-vault +require_capture "count primary history before import" count_log_entries --view "$PRIMARY_BRANCH" +PRIMARY_COUNT_BEFORE="$COMMAND_OUTPUT" # Draft logs hide inherited parent changes by default. Incremental import must # still deduplicate against the explicit target view, and it must not leave the # user's current draft merely because Git's branch was checked for new commits. -assert_success "create and switch to inherited draft" \ +require_success "create and switch to inherited draft" \ atomic view create agent-draft --draft --parent "$PRIMARY_BRANCH" --switch -assert_success "incremental no-op while draft is current" \ +require_success "incremental no-op while draft is current" \ atomic git import --incremental --branch "$PRIMARY_BRANCH" --no-vault -PRIMARY_COUNT_AFTER=$(atomic log --view "$PRIMARY_BRANCH" 2>/dev/null | grep -c '^#' || true) +require_capture "count primary history after no-op" count_log_entries --view "$PRIMARY_BRANCH" +PRIMARY_COUNT_AFTER="$COMMAND_OUTPUT" if [ "$PRIMARY_COUNT_AFTER" -eq "$PRIMARY_COUNT_BEFORE" ]; then _pass "active draft no-op: target count unchanged ($PRIMARY_COUNT_AFTER)" else @@ -339,13 +359,7 @@ else "expected $PRIMARY_COUNT_BEFORE, got $PRIMARY_COUNT_AFTER" fi -CURRENT_ATOMIC_VIEW=$(tr -d '[:space:]' < .atomic/current_view) -if [ "$CURRENT_ATOMIC_VIEW" = "agent-draft" ]; then - _pass "incremental import preserves current draft" -else - _fail "incremental import preserves current draft" \ - "expected agent-draft, got $CURRENT_ATOMIC_VIEW" -fi +assert_current_view "incremental import preserves current draft" "agent-draft" # A real incremental sync can arrive while the draft has diverged from its # parent. The import target must not be reconciled against that foreign working @@ -354,7 +368,7 @@ fi printf '%s' "draft alpha" > a.txt printf '%s' "draft shared" > shared.txt printf '%s' "draft rename" > rename-me.txt -assert_success "record divergent draft modification" \ +require_success "record divergent draft modification" \ atomic record --all -m "Draft modifies an inherited file" rm b.txt @@ -370,10 +384,11 @@ git commit --quiet -m "Third: add c, delete shared path, and rename a file" mv renamed.txt rename-me.txt rm c.txt printf '%s' "draft rename" > rename-me.txt -assert_success "incremental new commit while draft is current" \ - atomic git import --incremental --branch "$PRIMARY_BRANCH" --no-vault +if import_output="$(atomic git import --incremental --branch "$PRIMARY_BRANCH" --no-vault 2>&1)"; then + _pass "incremental new commit while draft is current" -PRIMARY_COUNT_WITH_NEW_COMMIT=$(atomic log --view "$PRIMARY_BRANCH" 2>/dev/null | grep -c '^#' || true) +require_capture "count primary history after new commit" count_log_entries --view "$PRIMARY_BRANCH" +PRIMARY_COUNT_WITH_NEW_COMMIT="$COMMAND_OUTPUT" EXPECTED_PRIMARY_COUNT=$((PRIMARY_COUNT_BEFORE + 1)) if [ "$PRIMARY_COUNT_WITH_NEW_COMMIT" -eq "$EXPECTED_PRIMARY_COUNT" ]; then _pass "active draft import adds exactly one target change" @@ -382,13 +397,7 @@ else "expected $EXPECTED_PRIMARY_COUNT, got $PRIMARY_COUNT_WITH_NEW_COMMIT" fi -CURRENT_ATOMIC_VIEW=$(tr -d '[:space:]' < .atomic/current_view) -if [ "$CURRENT_ATOMIC_VIEW" = "agent-draft" ]; then - _pass "new-commit import preserves current draft" -else - _fail "new-commit import preserves current draft" \ - "expected agent-draft, got $CURRENT_ATOMIC_VIEW" -fi +assert_current_view "new-commit import preserves current draft" "agent-draft" assert_file_content "draft modification remains materialized" "a.txt" "draft alpha" assert_file_not_exists "draft deletion remains materialized" "b.txt" assert_file_content "draft-owned target deletion remains materialized" \ @@ -397,7 +406,8 @@ assert_file_content "draft-owned target rename remains materialized" \ "rename-me.txt" "draft rename" assert_file_not_exists "target rename does not leak into active draft" "renamed.txt" assert_file_not_exists "target addition does not leak into active draft" "c.txt" -DRAFT_STATUS=$(atomic status --short 2>/dev/null || true) +require_capture "read draft status after import" atomic status --short +DRAFT_STATUS="$COMMAND_OUTPUT" if echo "$DRAFT_STATUS" | grep -qE '^\?+ +shared\.txt$'; then _fail "target deletion keeps draft path tracked" "$DRAFT_STATUS" else @@ -414,7 +424,7 @@ fi # lifecycle so switching can remove one occupant before installing the other. printf '%s' "beta" > b.txt printf '%s' "draft gamma" > c.txt -assert_success "record foreground add at deferred target path" \ +require_success "record foreground add at deferred target path" \ atomic record --all -m "Draft independently adds target path" rm b.txt assert_file_content "foreground same-path add remains materialized" \ @@ -424,16 +434,16 @@ assert_file_content "foreground same-path add remains materialized" \ # view's new path baseline. Switching to the target and back may not resurrect # the stale pre-rename path from the first journal event. printf '%s' "beta" > b.txt -assert_success "foreground draft rename after background import" \ +require_success "foreground draft rename after background import" \ mv rename-me.txt draft-renamed.txt -assert_success "record foreground draft rename" \ +require_success "record foreground draft rename" \ atomic record --all -m "Draft renames after background import" rm b.txt assert_file_content "foreground draft rename is materialized" \ "draft-renamed.txt" "draft rename" assert_file_not_exists "foreground draft rename removes old path" "rename-me.txt" -assert_success "force switch to imported primary after draft sync" \ +require_success "force switch to imported primary after draft sync" \ atomic view switch --force "$PRIMARY_BRANCH" assert_file_content "primary modification is not taken from draft" "a.txt" "alpha" assert_output_contains "primary graph retains the draft-deleted path" "D b.txt" \ @@ -443,7 +453,7 @@ assert_file_not_exists "primary receives the target deletion" "shared.txt" assert_file_not_exists "primary removes the old renamed path" "rename-me.txt" assert_file_content "primary receives the renamed path" "renamed.txt" "rename baseline" -assert_success "switch back to source with same-path add" \ +require_success "switch back to source with same-path add" \ atomic view switch --force agent-draft assert_file_content "source restores its own same-path inode" \ "c.txt" "draft gamma" @@ -453,7 +463,7 @@ assert_file_not_exists "source removes the target rename again" "renamed.txt" # Return TREE to the target state used by the interrupted-switch recovery # fixture below. -assert_success "switch target again before recovery fixture" \ +require_success "switch target again before recovery fixture" \ atomic view switch --force "$PRIMARY_BRANCH" assert_file_content "target restores its own same-path inode" "c.txt" "gamma" @@ -470,7 +480,7 @@ rm -f renamed.txt printf '%s\n' "agent-draft" > .atomic/current_view printf '{"version":1,"source_view":"agent-draft","target_view":"%s"}\n' \ "$PRIMARY_BRANCH" > .atomic/deferred-tree-alignment.pending -assert_success "recover interrupted TREE/view alignment" \ +require_success "recover interrupted TREE/view alignment" \ atomic view switch --force agent-draft assert_file_not_exists "recovery clears the pending alignment marker" \ ".atomic/deferred-tree-alignment.pending" @@ -480,6 +490,22 @@ assert_file_content "recovery restores the source same-path inode" \ "c.txt" "draft gamma" assert_file_not_exists "draft does not resurrect the stale rename baseline" "rename-me.txt" assert_file_not_exists "draft does not retain the target rename" "renamed.txt" +else + _fail "incremental new commit while draft is current" "$import_output" + require_capture "count primary history after refused import" count_log_entries --view "$PRIMARY_BRANCH" + if [[ "$COMMAND_OUTPUT" -eq "$PRIMARY_COUNT_BEFORE" ]]; then + _pass "refused import preserves primary change count" + else + _fail "refused import preserves primary change count" "before=$PRIMARY_COUNT_BEFORE after=$COMMAND_OUTPUT" + fi + assert_current_view "refused import preserves current draft" "agent-draft" + assert_file_content "refused import preserves draft modification" "a.txt" "draft alpha" + assert_file_content "refused import preserves draft shared file" "shared.txt" "draft shared" + assert_file_content "refused import preserves draft rename bytes" "rename-me.txt" "draft rename" + assert_file_not_exists "refused import preserves draft deletion" "b.txt" + assert_file_not_exists "refused import does not materialize target addition" "c.txt" + _skip "active draft's later materialization and recovery checks" "incremental import failed" +fi # ════════════════════════════════════════════════════════════════════════ # Section 9: Git-owned materialization after importing all local branches @@ -492,19 +518,19 @@ init_git_repo git_commit "Main baseline" "shared.txt" "main baseline" PRIMARY_BRANCH=$(git_default_branch) -git checkout -b zz-materialization 2>/dev/null +require_success "create Git materialization branch" git checkout --quiet -b zz-materialization git_commit "Feature content" "feature-only.txt" "feature content" printf '#!/bin/sh\necho feature\n' > feature-tool.sh chmod +x feature-tool.sh ln -s shared.txt feature-link git add feature-tool.sh feature-link git commit --quiet -m "Add executable and symlink" -git checkout "$PRIMARY_BRANCH" 2>/dev/null +require_success "return to primary Git branch" git checkout --quiet "$PRIMARY_BRANCH" # `--all` imports every local Git branch. Git remains checked out on the # primary branch, so Atomic must not leave the worktree materialized as the # final non-HEAD Atomic view. -assert_success "import all local branches" atomic git import --all --no-vault +require_success "import all local branches" atomic git import --all --no-vault assert_file_not_exists "feature file is absent from Git's checked-out branch" "feature-only.txt" assert_file_not_exists "feature executable is absent from Git's checked-out branch" "feature-tool.sh" @@ -520,8 +546,8 @@ fi # Switching branches remains a Git-owned materialization. Incremental import # must index this checkout rather than writing Atomic's version over it. -git checkout zz-materialization 2>/dev/null -assert_success "incremental import of checked-out feature" atomic git import --incremental --branch zz-materialization --no-vault +require_success "checkout Git materialization branch" git checkout --quiet zz-materialization +require_success "incremental import of checked-out feature" atomic git import --incremental --branch zz-materialization --no-vault assert_file_content "feature checkout remains intact" "feature-only.txt" "feature content" if [[ -x feature-tool.sh ]]; then _pass "Git executable bit remains intact" @@ -546,11 +572,13 @@ fi # This supersedes the old "git owns the checkout; view switch must not move git" # model. It is a ref move — git metadata is preserved and files are never # re-rendered; Atomic stays the content authority. -assert_success "Atomic view switch materializes primary view" atomic view switch "$PRIMARY_BRANCH" +if switch_output="$(atomic view switch "$PRIMARY_BRANCH" 2>&1)"; then +_pass "Atomic view switch materializes primary view" assert_dir_exists "Atomic view switch preserves Git metadata" ".git" assert_file_content "primary view content is materialized" "shared.txt" "main baseline" -SWITCHED_GIT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null) +require_capture "read switched Git branch" git symbolic-ref --short HEAD +SWITCHED_GIT_BRANCH="$COMMAND_OUTPUT" if [ "$SWITCHED_GIT_BRANCH" = "$PRIMARY_BRANCH" ]; then _pass "git shadow HEAD follows the view to '$PRIMARY_BRANCH'" else @@ -569,6 +597,29 @@ if [[ -z "$GIT_STATUS_AFTER_CLEAN" ]]; then else _fail "clean worktree once raw-git residue is cleared" "$GIT_STATUS_AFTER_CLEAN" fi +else + _fail "Atomic view switch materializes primary view" "$switch_output" + require_capture "read Git branch after refused switch" git symbolic-ref --short HEAD + if [[ "$COMMAND_OUTPUT" == "zz-materialization" ]]; then + _pass "refused switch preserves Git branch" + else + _fail "refused switch preserves Git branch" "got: $COMMAND_OUTPUT" + fi + assert_file_content "refused switch preserves feature bytes" "feature-only.txt" "feature content" + assert_file_content "refused switch preserves shared bytes" "shared.txt" "main baseline" + if [[ -L feature-link && -x feature-tool.sh ]]; then + _pass "refused switch preserves symlink and executable" + else + _fail "refused switch preserves symlink and executable" "feature-link or feature-tool.sh changed" + fi + require_capture "read Git status after refused switch" git status --short + if [[ -z "$COMMAND_OUTPUT" ]]; then + _pass "refused switch preserves clean Git status" + else + _fail "refused switch preserves clean Git status" "$COMMAND_OUTPUT" + fi + _skip "primary-view materialization checks" "view switch refused" +fi # ════════════════════════════════════════════════════════════════════════ # Section 10: GitLab MR format detection @@ -582,18 +633,19 @@ init_git_repo # Build entire git history before Atomic import git_commit "Initial" "README.md" "# Project" DEFAULT_BRANCH=$(git_default_branch) -git checkout -b feature-login 2>/dev/null +require_success "create Git login branch" git checkout --quiet -b feature-login git_commit "Add login" "src/login.rs" "fn login() {}" -git checkout "$DEFAULT_BRANCH" 2>/dev/null -git merge --squash feature-login 2>/dev/null -git commit -m "Add login +require_success "return to default Git branch" git checkout --quiet "$DEFAULT_BRANCH" +require_success "squash Git login branch" git merge --quiet --squash feature-login +require_success "record GitLab squash commit" git commit --quiet -m "Add login -See merge request mygroup/myproject!55" 2>/dev/null +See merge request mygroup/myproject!55" -assert_success "import with GitLab MR" atomic git import --no-vault +require_success "import with GitLab MR" atomic git import --no-vault # Verify changes were imported -LOG_COUNT=$(count_log_entries) +require_capture "count GitLab history" count_log_entries +LOG_COUNT="$COMMAND_OUTPUT" if [ "$LOG_COUNT" -ge 2 ]; then _pass "GitLab MR squash imported (count=$LOG_COUNT)" else diff --git a/tests/harness/33_shadow_push_conflict_markers.sh b/tests/harness/legacy-shadow/33_shadow_push_conflict_markers.sh old mode 100755 new mode 100644 similarity index 72% rename from tests/harness/33_shadow_push_conflict_markers.sh rename to tests/harness/legacy-shadow/33_shadow_push_conflict_markers.sh index 895e6461..df65de8b --- a/tests/harness/33_shadow_push_conflict_markers.sh +++ b/tests/harness/legacy-shadow/33_shadow_push_conflict_markers.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 33_shadow_push_conflict_markers.sh — Shadow push must never commit markers. +# HISTORICAL: coverage migrated to ../40_git_shim_safety.sh. # # Regression for SPEC-shadow-push-conflict-markers.md: `atomic git push` # (shadow materialization, incl. --no-push) committed working copies that still @@ -34,17 +34,19 @@ begin_section "Clean shadow push (baseline, no regression)" make_temp_repo "shadow-conflict" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import seeds the view" atomic git import --no-vault +require_success "git import seeds the view" atomic git import --no-vault create_file "src/app.ts" "const x = 1;" -add_files "src/app.ts" -record_change "feat: app" >/dev/null +require_success "add app.ts" atomic add "src/app.ts" +require_success "record app.ts" record_change "feat: app" -assert_success "clean working copy shadow-pushes" \ +require_success "clean working copy shadow-pushes locally" \ atomic git push --no-push -m "sync clean" -CLEAN_COUNT="$(git_commit_count)" -_pass "baseline git commit count = $CLEAN_COUNT" +require_capture "read baseline Git commit count" git rev-list --count HEAD +CLEAN_COUNT="$COMMAND_OUTPUT" +require_capture "read baseline Git HEAD" git rev-parse HEAD +CLEAN_HEAD="$COMMAND_OUTPUT" # ════════════════════════════════════════════════════════════════════════ # A materialized conflict must abort the shadow push @@ -68,19 +70,30 @@ $MARK_EQ 1 [C2YTBAHQ] const b = 3; $MARK_LT 1 EOF +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-shadow-marker-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp src/app.ts "$snapshot_dir/app.ts" +require_capture "read history before marker refusal" atomic log --format json +history_before="$COMMAND_OUTPUT" # Capture the aborted push once (output + rc + commit count around it). set +e PUSH_OUT="$(atomic git push --no-push -m "sync conflicted" 2>&1)" PUSH_RC=$? set -e -AFTER_COUNT="$(git_commit_count)" +require_capture "read Git commit count after refusal" git rev-list --count HEAD +AFTER_COUNT="$COMMAND_OUTPUT" if [ "$PUSH_RC" -ne 0 ]; then _pass "shadow push exits non-zero on markers" else _fail "shadow push exits non-zero on markers" "expected non-zero, got 0" fi +assert_success "refused shadow push preserves marker bytes" cmp -s "$snapshot_dir/app.ts" src/app.ts +require_capture "read Git HEAD after refusal" git rev-parse HEAD +if [[ "$COMMAND_OUTPUT" == "$CLEAN_HEAD" ]]; then _pass "refusal preserves Git HEAD"; else _fail "refusal preserves Git HEAD" "HEAD changed"; fi +require_capture "read history after marker refusal" atomic log --format json +if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then _pass "refusal preserves Atomic history"; else _fail "refusal preserves Atomic history" "history changed"; fi if echo "$PUSH_OUT" | grep -qa "src/app.ts"; then _pass "abort names the conflicted file" @@ -128,12 +141,22 @@ begin_section "Explicit --allow-conflict-markers override" # them with record's own override, so the working copy now matches the view # (V2 tree↔view coherence holds). The shadow-push override then bypasses the V1 # marker gate and commits. -assert_success "record accepts markers with its override" \ - atomic record --allow-conflict-markers -m "accept markers as content" -assert_success "override commits a marker-laden tree" \ +if record_out="$(atomic record --allow-conflict-markers -m "accept markers as content" 2>&1)"; then + _pass "record accepts markers with its override" +else + _fail "record accepts markers with its override" "$record_out" + assert_success "refused override preserves marker bytes" cmp -s "$snapshot_dir/app.ts" src/app.ts + require_capture "read history after refused override" atomic log --format json + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then _pass "refused override preserves Atomic history"; else _fail "refused override preserves Atomic history" "history changed"; fi + _skip "shadow override and commit checks" "record override refused" + print_summary || true + exit 1 +fi +require_success "override commits a marker-laden tree locally" \ atomic git push --no-push --allow-conflict-markers -m "override" -OVERRIDE_COUNT="$(git_commit_count)" +require_capture "read Git commit count after override" git rev-list --count HEAD +OVERRIDE_COUNT="$COMMAND_OUTPUT" if [ "$OVERRIDE_COUNT" -gt "$CLEAN_COUNT" ]; then _pass "override created a commit ($OVERRIDE_COUNT)" else diff --git a/tests/harness/34_shadow_provenance_guard.sh b/tests/harness/legacy-shadow/34_shadow_provenance_guard.sh old mode 100755 new mode 100644 similarity index 63% rename from tests/harness/34_shadow_provenance_guard.sh rename to tests/harness/legacy-shadow/34_shadow_provenance_guard.sh index 90c5dc3b..e44c0721 --- a/tests/harness/34_shadow_provenance_guard.sh +++ b/tests/harness/legacy-shadow/34_shadow_provenance_guard.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 34_shadow_provenance_guard.sh — Validator Rule V4 (provenance / excluded paths). +# HISTORICAL: coverage migrated to ../40_git_shim_safety.sh. # # SPEC-single-materializer-validator.md, Phase 1 / Rule V4: a shadow commit MUST # never stage a git-excluded Atomic provenance path (`.atomic/`, `.vault/`, @@ -25,11 +25,6 @@ begin_section "Prerequisites" require_git # Grep the committed tree for any Atomic provenance path. -tree_has_provenance() { - git ls-tree -r --name-only HEAD 2>/dev/null \ - | grep -qE '^\.atomic/|^\.vault/|^\.atomicignore$' -} - # ════════════════════════════════════════════════════════════════════════ # A normal shadow commit never contains provenance paths # ════════════════════════════════════════════════════════════════════════ @@ -39,16 +34,17 @@ begin_section "Normal shadow commit excludes provenance paths" make_temp_repo "v4-clean" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import seeds the view" atomic git import --no-vault +require_success "git import seeds the view" atomic git import --no-vault assert_dir_exists ".atomic exists on disk" ".atomic" create_file "src/app.ts" "const x = 1;" -add_files "src/app.ts" -record_change "feat: app" >/dev/null -assert_success "clean shadow push commits" atomic git push --no-push -m "sync" +require_success "add app.ts" atomic add "src/app.ts" +require_success "record app.ts" record_change "feat: app" +require_success "clean shadow push commits locally" atomic git push --no-push -m "sync" -if tree_has_provenance; then +require_capture "read committed Git tree" git ls-tree -r --name-only HEAD +if printf '%s\n' "$COMMAND_OUTPUT" | grep -qE '^\.atomic/|^\.vault/|^\.atomicignore$'; then _fail "committed tree has no provenance paths" \ "found: $(git ls-tree -r --name-only HEAD | grep -E '^\.atomic/|^\.vault/|^\.atomicignore$' | head -3)" else @@ -65,29 +61,53 @@ begin_section "V4 aborts on an already-tracked provenance path" make_temp_repo "v4-tracked" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import" atomic git import --no-vault +require_success "git import" atomic git import --no-vault # Simulate the dangerous pre-condition: a `.vault/` path that got committed to # git before excludes were in place, so it is TRACKED (exclude rules no longer # protect it and `git add -A`/update_all will keep staging it). mkdir -p .vault printf 'intent: precious' > .vault/note.md -git add -f .vault/note.md -git commit -q -m "Accidentally tracked a provenance path" +require_success "stage fixture provenance path" git add -f .vault/note.md +require_success "commit fixture provenance path" git commit -q -m "Accidentally tracked a provenance path" -VAULT_HASH_BEFORE="$(git hash-object .vault/note.md)" -COUNT_BEFORE="$(git_commit_count)" +require_capture "hash fixture provenance bytes" git hash-object .vault/note.md +VAULT_HASH_BEFORE="$COMMAND_OUTPUT" +require_capture "read fixture Git commit count" git rev-list --count HEAD +COUNT_BEFORE="$COMMAND_OUTPUT" # Record an ordinary change and attempt a shadow push. create_file "src/app.ts" "const y = 2;" -add_files "src/app.ts" -record_change "feat: app2" >/dev/null +if add_out="$(atomic add "src/app.ts" 2>&1)"; then + _pass "add app.ts with tracked provenance fixture" +else + _fail "add app.ts with tracked provenance fixture" "$add_out" + require_capture "hash provenance after refused add" git hash-object .vault/note.md + if [[ "$COMMAND_OUTPUT" == "$VAULT_HASH_BEFORE" ]]; then _pass "refused add preserves provenance bytes"; else _fail "refused add preserves provenance bytes" "bytes changed"; fi + assert_file_content "refused add preserves source bytes" src/app.ts "const y = 2;" + require_capture "read Git commit count after refused add" git rev-list --count HEAD + if [[ "$COMMAND_OUTPUT" == "$COUNT_BEFORE" ]]; then _pass "refused add preserves Git history"; else _fail "refused add preserves Git history" "commit count changed"; fi + _skip "tracked-provenance shadow push checks" "fixture add refused" + print_summary || true + exit 1 +fi +if record_out="$(record_change "feat: app2" 2>&1)"; then + _pass "record app.ts with tracked provenance fixture" +else + _fail "record app.ts with tracked provenance fixture" "$record_out" + require_capture "hash provenance after refused record" git hash-object .vault/note.md + if [[ "$COMMAND_OUTPUT" == "$VAULT_HASH_BEFORE" ]]; then _pass "refused record preserves provenance bytes"; else _fail "refused record preserves provenance bytes" "bytes changed"; fi + _skip "tracked-provenance shadow push checks" "fixture record refused" + print_summary || true + exit 1 +fi set +e PUSH_OUT="$(atomic git push --no-push -m "sync tracked provenance" 2>&1)" PUSH_RC=$? set -e -COUNT_AFTER="$(git_commit_count)" +require_capture "read Git commit count after refusal" git rev-list --count HEAD +COUNT_AFTER="$COMMAND_OUTPUT" if [ "$PUSH_RC" -ne 0 ]; then _pass "shadow push aborts (V4) on a tracked provenance path" @@ -115,7 +135,8 @@ else fi # Provenance byte-identical after the aborted push. -VAULT_HASH_AFTER="$(git hash-object .vault/note.md)" +require_capture "hash provenance after shadow push" git hash-object .vault/note.md +VAULT_HASH_AFTER="$COMMAND_OUTPUT" if [ "$VAULT_HASH_AFTER" = "$VAULT_HASH_BEFORE" ]; then _pass "provenance file left byte-identical on abort" else diff --git a/tests/harness/35_shadow_concurrent_push.sh b/tests/harness/legacy-shadow/35_shadow_concurrent_push.sh old mode 100755 new mode 100644 similarity index 54% rename from tests/harness/35_shadow_concurrent_push.sh rename to tests/harness/legacy-shadow/35_shadow_concurrent_push.sh index b9e41998..b526e0df --- a/tests/harness/35_shadow_concurrent_push.sh +++ b/tests/harness/legacy-shadow/35_shadow_concurrent_push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 35_shadow_concurrent_push.sh — Shadow-commit serialization (Rule / Phase 3). +# HISTORICAL: coverage migrated to ../35_git_shim_serialization.sh. # # SPEC-single-materializer-validator.md §4.3 / Principle 5: only one shadow # materialize/commit may be in flight per repo. Concurrent pushes must serialize @@ -27,33 +27,52 @@ begin_section "Concurrent shadow pushes serialize (no corruption, no double comm make_temp_repo "shadow-concurrent" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import" atomic git import --no-vault +require_success "git import" atomic git import --no-vault # Baseline shadow commit. create_file "src/a.ts" "const a = 1;" -add_files "src/a.ts" -record_change "feat: a" >/dev/null -assert_success "baseline push" atomic git push --no-push -m "baseline" -BASE_COUNT="$(git_commit_count)" +require_success "add src/a.ts" atomic add "src/a.ts" +require_success "record src/a.ts" record_change "feat: a" +require_success "baseline local shadow commit" atomic git push --no-push -m "baseline" +require_capture "read baseline commit count" git rev-list --count HEAD +BASE_COUNT="$COMMAND_OUTPUT" # A new recorded change, then two overlapping pushes of the same state. create_file "src/b.ts" "const b = 2;" -add_files "src/b.ts" -record_change "feat: b" >/dev/null +require_success "add src/b.ts" atomic add "src/b.ts" +require_success "record src/b.ts" record_change "feat: b" -atomic git push --no-push -m "concurrent-1" >/dev/null 2>&1 & +output_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-concurrent-output-XXXXXX")" +_HARNESS_TMPDIRS+=("$output_dir") +atomic git push --no-push -m "concurrent-1" >"$output_dir/concurrent-1.out" 2>&1 & P1=$! -atomic git push --no-push -m "concurrent-2" >/dev/null 2>&1 & +atomic git push --no-push -m "concurrent-2" >"$output_dir/concurrent-2.out" 2>&1 & P2=$! -wait "$P1" || true -wait "$P2" || true +RC1=0 +RC2=0 +wait "$P1" || RC1=$? +wait "$P2" || RC2=$? +# Launching two processes is a smoke test, not proof that their critical +# sections overlapped. The deterministic lock unit test covers exclusivity; +# this harness must not claim that it exercised actual contention. +_skip "confirmed overlapping shadow critical sections" "parallel process launch does not prove contention" +for index in 1 2; do + rc_var="RC$index" + rc="${!rc_var}" + if [[ "$rc" -eq 0 ]]; then + _pass "concurrent local shadow commit $index succeeds" + else + _fail "concurrent local shadow commit $index succeeds" "exit $rc: $(cat "$output_dir/concurrent-$index.out")" + fi +done # The repo must remain valid (no interleaved/corrupt tree). assert_success "git repo still valid after concurrent pushes" git fsck --no-progress assert_success "atomic status still works" atomic status # The burst must add at most one commit — never a double commit. -BURST_COUNT="$(git_commit_count)" +require_capture "read concurrent commit count" git rev-list --count HEAD +BURST_COUNT="$COMMAND_OUTPUT" if [ "$BURST_COUNT" -le "$((BASE_COUNT + 1))" ]; then _pass "concurrent burst added at most one commit ($BURST_COUNT)" else @@ -62,14 +81,12 @@ else fi # Convergence: a final push leaves the new change committed and the tree clean. -atomic git push --no-push -m "converge" >/dev/null 2>&1 || true -if git ls-tree -r --name-only HEAD 2>/dev/null | grep -qx "src/b.ts"; then - _pass "new change is committed after convergence" -else - _fail "new change is committed after convergence" "src/b.ts not in HEAD tree" -fi +require_success "converge local shadow commit" atomic git push --no-push -m "converge" +require_capture "read committed src/b.ts" git show HEAD:src/b.ts +if [[ "$COMMAND_OUTPUT" == 'const b = 2;' ]]; then _pass "new change is committed with exact text"; else _fail "new change is committed with exact text" "got: $COMMAND_OUTPUT"; fi -FINAL_COUNT="$(git_commit_count)" +require_capture "read converged commit count" git rev-list --count HEAD +FINAL_COUNT="$COMMAND_OUTPUT" if [ "$FINAL_COUNT" -eq "$((BASE_COUNT + 1))" ]; then _pass "exactly one commit for the new change ($FINAL_COUNT)" else diff --git a/tests/harness/36_shadow_coherence_v2.sh b/tests/harness/legacy-shadow/36_shadow_coherence_v2.sh old mode 100755 new mode 100644 similarity index 62% rename from tests/harness/36_shadow_coherence_v2.sh rename to tests/harness/legacy-shadow/36_shadow_coherence_v2.sh index fe0c09aa..e4c82845 --- a/tests/harness/36_shadow_coherence_v2.sh +++ b/tests/harness/legacy-shadow/36_shadow_coherence_v2.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 36_shadow_coherence_v2.sh — Validator Rule V2 (tree ↔ view coherence). +# HISTORICAL: coverage migrated to ../40_git_shim_safety.sh. # # SPEC-single-materializer-validator.md §6.2 / Phase 4: the staged tree must # correspond to what the current view materializes. A working copy that has @@ -29,25 +29,39 @@ begin_section "Coherent record → push commits (no false positive)" make_temp_repo "v2-coherence" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import" atomic git import --no-vault +require_success "git import" atomic git import --no-vault create_file "src/a.ts" "const a = 1;" -add_files "src/a.ts" -record_change "feat: a" >/dev/null -assert_success "coherent shadow push commits" atomic git push --no-push -m "baseline" -BASE_COUNT="$(git_commit_count)" +require_success "add src/a.ts" atomic add "src/a.ts" +require_success "record src/a.ts" record_change "feat: a" +require_success "coherent local shadow commit" atomic git push --no-push -m "baseline" +require_capture "read baseline Git commit count" git rev-list --count HEAD +BASE_COUNT="$COMMAND_OUTPUT" +require_capture "read baseline Git HEAD" git rev-parse HEAD +BASE_HEAD="$COMMAND_OUTPUT" begin_section "Out-of-band drift is rejected by V2" # Edit a tracked file on disk WITHOUT recording it: the working copy now # diverges from the view's recorded content. overwrite_file "src/a.ts" "const a = 999; // edited on disk, never recorded" +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-shadow-drift-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp src/a.ts "$snapshot_dir/a.ts" +require_capture "read Atomic history before drift refusal" atomic log --format json +history_before="$COMMAND_OUTPUT" set +e PUSH_OUT="$(atomic git push --no-push -m "drift" 2>&1)" PUSH_RC=$? set -e -AFTER_COUNT="$(git_commit_count)" +require_capture "read Git count after drift refusal" git rev-list --count HEAD +AFTER_COUNT="$COMMAND_OUTPUT" +assert_success "drift refusal preserves working bytes" cmp -s "$snapshot_dir/a.ts" src/a.ts +require_capture "read Git HEAD after drift refusal" git rev-parse HEAD +if [[ "$COMMAND_OUTPUT" == "$BASE_HEAD" ]]; then _pass "drift refusal preserves Git HEAD"; else _fail "drift refusal preserves Git HEAD" "HEAD changed"; fi +require_capture "read Atomic history after drift refusal" atomic log --format json +if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then _pass "drift refusal preserves Atomic history"; else _fail "drift refusal preserves Atomic history" "history changed"; fi if [ "$PUSH_RC" -ne 0 ]; then _pass "shadow push aborts (V2) on out-of-band drift" @@ -76,11 +90,12 @@ fi begin_section "Recording the edit restores coherence" -add_files "src/a.ts" -record_change "feat: a (edited)" >/dev/null -assert_success "push commits once coherent" atomic git push --no-push -m "recorded edit" +require_success "add src/a.ts" atomic add "src/a.ts" +require_success "record edited src/a.ts" record_change "feat: a (edited)" +require_success "local shadow commit succeeds once coherent" atomic git push --no-push -m "recorded edit" -FINAL_COUNT="$(git_commit_count)" +require_capture "read final Git commit count" git rev-list --count HEAD +FINAL_COUNT="$COMMAND_OUTPUT" if [ "$FINAL_COUNT" -eq "$((BASE_COUNT + 1))" ]; then _pass "exactly one commit after recording ($FINAL_COUNT)" else diff --git a/tests/harness/37_shadow_view_branch_follow.sh b/tests/harness/legacy-shadow/37_shadow_view_branch_follow.sh old mode 100755 new mode 100644 similarity index 63% rename from tests/harness/37_shadow_view_branch_follow.sh rename to tests/harness/legacy-shadow/37_shadow_view_branch_follow.sh index 0ae62819..d2fbd98e --- a/tests/harness/37_shadow_view_branch_follow.sh +++ b/tests/harness/legacy-shadow/37_shadow_view_branch_follow.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 37_shadow_view_branch_follow.sh — git shadow HEAD follows `atomic view switch`. +# HISTORICAL: coverage migrated to ../39_git_shim_workflow.sh. # # SPEC-single-materializer-validator.md §5.4, Direction A ("git shadows Atomic"): # Atomic is upstream, so switching the Atomic view points the downstream git @@ -24,7 +24,7 @@ echo "${BOLD}══════════════════════ begin_section "Prerequisites" require_git -git_head_branch() { git symbolic-ref --short HEAD 2>/dev/null; } +git_head_branch() { git symbolic-ref --short HEAD; } # ════════════════════════════════════════════════════════════════════════ # git shadow HEAD follows the Atomic view @@ -35,43 +35,46 @@ begin_section "Shadow HEAD follows view switch" make_temp_repo "view-branch-follow" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import establishes shadow sync" atomic git import --no-vault +require_success "git import establishes shadow sync" atomic git import --no-vault -MAIN="$(git_head_branch)" -_pass "shadow default branch is '$MAIN'" +require_capture "read shadow default branch" git_head_branch +MAIN="$COMMAND_OUTPUT" +assert_current_view "Atomic view matches shadow default branch" "$MAIN" # Record a change and create a draft view (no --switch yet). create_file "src/a.ts" "const a = 1;" -add_files "src/a.ts" -record_change "feat: a" >/dev/null -assert_success "create draft view" atomic view create agent --draft --parent "$MAIN" +require_success "add src/a.ts" atomic add "src/a.ts" +require_success "record src/a.ts" record_change "feat: a" +require_success "create draft view" atomic view create agent --draft --parent "$MAIN" # Switching to the draft moves the git shadow onto a mirror branch (created). -assert_success "switch to draft view" atomic view switch agent --force -if [ "$(git_head_branch)" = "agent" ]; then +require_success "switch to draft view" atomic view switch agent --force +assert_current_view "Atomic view switched to agent" "agent" +if branch="$(git_head_branch 2>&1)" && [ "$branch" = "agent" ]; then _pass "git HEAD followed to branch 'agent'" else - _fail "git HEAD followed to branch 'agent'" "on '$(git_head_branch)'" + _fail "git HEAD followed to branch 'agent'" "on '$branch'" fi assert_success "mirror branch 'agent' exists" git rev-parse --verify --quiet refs/heads/agent # The materialized working copy is intact (ref move, not a checkout). -assert_file_exists "working copy intact after follow" "src/a.ts" +assert_file_content "working copy intact after follow" "src/a.ts" "const a = 1;" # Switching back moves HEAD back to the shared branch. -assert_success "switch back to shared view" atomic view switch "$MAIN" --force -if [ "$(git_head_branch)" = "$MAIN" ]; then +require_success "switch back to shared view" atomic view switch "$MAIN" --force +assert_current_view "Atomic view returned to shared" "$MAIN" +if branch="$(git_head_branch 2>&1)" && [ "$branch" = "$MAIN" ]; then _pass "git HEAD followed back to '$MAIN'" else - _fail "git HEAD followed back to '$MAIN'" "on '$(git_head_branch)'" + _fail "git HEAD followed back to '$MAIN'" "on '$branch'" fi # Idempotent: switching to the view we're already on is a no-op. -assert_success "switch to current view is a no-op" atomic view switch "$MAIN" -if [ "$(git_head_branch)" = "$MAIN" ]; then +require_success "switch to current view is a no-op" atomic view switch "$MAIN" +if branch="$(git_head_branch 2>&1)" && [ "$branch" = "$MAIN" ]; then _pass "idempotent switch leaves HEAD on '$MAIN'" else - _fail "idempotent switch leaves HEAD on '$MAIN'" "on '$(git_head_branch)'" + _fail "idempotent switch leaves HEAD on '$MAIN'" "on '$branch'" fi # ════════════════════════════════════════════════════════════════════════ @@ -83,21 +86,25 @@ begin_section "Non-shadow repo: view switch leaves git alone" make_temp_repo "no-shadow" init_git_repo git_commit "Initial" "README.md" "# Plain" -BEFORE_BRANCH="$(git_head_branch)" +require_capture "read non-shadow Git branch" git_head_branch +BEFORE_BRANCH="$COMMAND_OUTPUT" # Atomic repo WITHOUT git import (no shadow excludes → shadow sync not active). init_repo create_file "src/x.ts" "const x = 1;" -add_files "src/x.ts" -record_change "feat: x" >/dev/null -new_view "other" >/dev/null 2>&1 || atomic view create other >/dev/null 2>&1 -assert_success "switch view in non-shadow repo" atomic view switch other --force - -if [ "$(git_head_branch)" = "$BEFORE_BRANCH" ]; then +require_success "add src/x.ts" atomic add "src/x.ts" +require_success "record src/x.ts" record_change "feat: x" +require_success "create non-shadow draft" new_view "other" +require_success "switch view in non-shadow repo" atomic view switch other --force +assert_current_view "Atomic view switched in non-shadow repo" "other" +assert_file_content "non-shadow switch preserves source bytes" "src/x.ts" "const x = 1;" + +require_capture "read non-shadow Git branch after switch" git_head_branch +if [ "$COMMAND_OUTPUT" = "$BEFORE_BRANCH" ]; then _pass "git HEAD untouched in non-shadow repo ('$BEFORE_BRANCH')" else _fail "git HEAD untouched in non-shadow repo" \ - "expected '$BEFORE_BRANCH', got '$(git_head_branch)'" + "expected '$BEFORE_BRANCH', got '$COMMAND_OUTPUT'" fi assert_failure "no mirror branch created in non-shadow repo" \ git rev-parse --verify --quiet refs/heads/other diff --git a/tests/harness/38_shadow_drift_v3.sh b/tests/harness/legacy-shadow/38_shadow_drift_v3.sh old mode 100755 new mode 100644 similarity index 55% rename from tests/harness/38_shadow_drift_v3.sh rename to tests/harness/legacy-shadow/38_shadow_drift_v3.sh index e9f351c2..6c08bc9a --- a/tests/harness/38_shadow_drift_v3.sh +++ b/tests/harness/legacy-shadow/38_shadow_drift_v3.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 38_shadow_drift_v3.sh — Validator Rule V3 (git ↔ Atomic state lineage). +# HISTORICAL: coverage migrated to ../40_git_shim_safety.sh and ../42_git_shim_guard.sh. # # SPEC-single-materializer-validator.md §6.3 / Phase 5: a shadow push must refuse # when the git branch's last published Atomic-State is NOT in the current view's @@ -27,10 +27,9 @@ require_git # Extract the current view's tip Atomic-State (base32) from the log. view_tip_state() { - atomic log --view "$MAIN" -f json 2>/dev/null \ - | grep -o '"state"[[:space:]]*:[[:space:]]*"[^"]*"' \ - | head -1 \ - | sed -E 's/.*"([^"]+)"$/\1/' + local output + output="$(atomic log --view "$MAIN" -f json 2>&1)" || return 1 + printf '%s\n' "$output" | python3 -c 'import json,sys; rows=json.load(sys.stdin); assert rows and len(rows[0]["state"])==52; print(rows[0]["state"])' } begin_section "Fast-forward push commits (no false positive)" @@ -38,26 +37,31 @@ begin_section "Fast-forward push commits (no false positive)" make_temp_repo "drift-v3" init_git_repo git_commit "Initial" "README.md" "# Project" -assert_success "git import" atomic git import --no-vault -MAIN="$(git symbolic-ref --short HEAD)" +require_success "git import" atomic git import --no-vault +require_capture "read current Git branch" git symbolic-ref --short HEAD +MAIN="$COMMAND_OUTPUT" create_file "src/a.ts" "const a = 1;" -add_files "src/a.ts" -record_change "feat: a" >/dev/null -assert_success "baseline push (fast-forward)" atomic git push --no-push -m "A" -BASE_COUNT="$(git_commit_count)" +require_success "add src/a.ts" atomic add "src/a.ts" +require_success "record src/a.ts" record_change "feat: a" +require_success "baseline local shadow commit" atomic git push --no-push -m "A" +require_capture "read baseline commit count" git rev-list --count HEAD +BASE_COUNT="$COMMAND_OUTPUT" begin_section "Drift (state not in lineage) is rejected by V3" # Record another change (not yet pushed). create_file "src/b.ts" "const b = 2;" -add_files "src/b.ts" -record_change "feat: b" >/dev/null +require_success "add src/b.ts" atomic add "src/b.ts" +require_success "record src/b.ts" record_change "feat: b" +require_capture "read coherent Git HEAD" git rev-parse HEAD +COHERENT_HEAD="$COMMAND_OUTPUT" # Fabricate drift: a git commit on the branch advertising an Atomic-State that # is NOT in the view's history (mutate the tip state's last char). find_last_ # pushed_state will pick this up as the branch's last published state. -TIP_STATE="$(view_tip_state)" +require_capture "read current view state" view_tip_state +TIP_STATE="$COMMAND_OUTPUT" # Mutate a MIDDLE character (full data bits) rather than the last (which carries # base32 padding bits) so the result is still a valid merkle, just a different # one that is not in the view's history. @@ -65,18 +69,32 @@ IDX=10 ORIG_CHAR="${TIP_STATE:$IDX:1}" REPL="Q"; [ "$ORIG_CHAR" = "Q" ] && REPL="Z" FAKE_STATE="${TIP_STATE:0:$IDX}$REPL${TIP_STATE:$((IDX + 1))}" -git commit --allow-empty -q -m "Simulated drift +require_success "create empty drift fixture commit" git commit --allow-empty -q -m "Simulated drift Atomic-View: $MAIN Atomic-State: $FAKE_STATE" -DRIFT_TIP_COUNT="$(git_commit_count)" +require_capture "read drift commit count" git rev-list --count HEAD +DRIFT_TIP_COUNT="$COMMAND_OUTPUT" +require_capture "read drift Git HEAD" git rev-parse HEAD +DRIFT_HEAD="$COMMAND_OUTPUT" +require_capture "read Atomic history before refusal" atomic log --view "$MAIN" -f json +HISTORY_BEFORE="$COMMAND_OUTPUT" +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-shadow-drift-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp src/b.ts "$snapshot_dir/b.ts" set +e PUSH_OUT="$(atomic git push --no-push -m "onto drift" 2>&1)" PUSH_RC=$? set -e -AFTER_COUNT="$(git_commit_count)" +require_capture "read Git count after refusal" git rev-list --count HEAD +AFTER_COUNT="$COMMAND_OUTPUT" +assert_success "drift refusal preserves source bytes" cmp -s "$snapshot_dir/b.ts" src/b.ts +require_capture "read Git HEAD after refusal" git rev-parse HEAD +if [[ "$COMMAND_OUTPUT" == "$DRIFT_HEAD" ]]; then _pass "drift refusal preserves Git HEAD"; else _fail "drift refusal preserves Git HEAD" "HEAD changed"; fi +require_capture "read Atomic history after refusal" atomic log --view "$MAIN" -f json +if [[ "$COMMAND_OUTPUT" == "$HISTORY_BEFORE" ]]; then _pass "drift refusal preserves Atomic history"; else _fail "drift refusal preserves Atomic history" "history changed"; fi if [ "$PUSH_RC" -ne 0 ]; then _pass "shadow push aborts (V3) on drift" @@ -105,11 +123,13 @@ fi begin_section "Reconcile-then-push succeeds" -# Reconcile by dropping the drift commit so the branch tip is a coherent shadow -# state again. -git reset --hard HEAD~1 >/dev/null 2>&1 -assert_success "push succeeds after reconcile" atomic git push --no-push -m "recorded b" -FINAL_COUNT="$(git_commit_count)" +# The drift commit was empty, so compare-and-set the disposable fixture ref +# back to its prior commit without resetting the index or working bytes. +require_success "restore fixture branch with expected-old lease" git update-ref "refs/heads/$MAIN" "$COHERENT_HEAD" "$DRIFT_HEAD" +assert_success "fixture ref restoration preserves source bytes" cmp -s "$snapshot_dir/b.ts" src/b.ts +require_success "local shadow commit succeeds after reconcile" atomic git push --no-push -m "recorded b" +require_capture "read reconciled Git commit count" git rev-list --count HEAD +FINAL_COUNT="$COMMAND_OUTPUT" if [ "$FINAL_COUNT" -gt "$BASE_COUNT" ]; then _pass "reconciled push created a commit ($FINAL_COUNT)" else diff --git a/tests/harness/39_git_shim_mvp.sh b/tests/harness/legacy-shadow/39_git_shim_mvp.sh old mode 100755 new mode 100644 similarity index 96% rename from tests/harness/39_git_shim_mvp.sh rename to tests/harness/legacy-shadow/39_git_shim_mvp.sh index 5648b3a8..2f3b3b67 --- a/tests/harness/39_git_shim_mvp.sh +++ b/tests/harness/legacy-shadow/39_git_shim_mvp.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 39_git_shim_mvp.sh — Offline contract test for `atomic git shim reconcile`. +# HISTORICAL: coverage migrated to ../39_git_shim_workflow.sh and ../40_git_shim_safety.sh. # # The MVP imports the checked-out Git branch, aligns it with an Atomic view, # reconciles later Git commits without disturbing either working tree, is @@ -17,14 +17,16 @@ begin_section "Prerequisites" require_git atomic_current_view() { - atomic view list 2>/dev/null | awk '/^\*/ { print $2 }' + local output + output="$(atomic view list --all --json 2>&1)" || return 1 + printf '%s\n' "$output" | python3 -c 'import json,sys; print(json.load(sys.stdin)["current_view"])' } assert_clean_statuses() { local label="$1" local git_status atomic_status - git_status="$(git status --short)" - atomic_status="$(atomic status --short 2>/dev/null || true)" + if ! git_status="$(git status --short 2>&1)"; then _fail "$label: read Git status" "$git_status"; return; fi + if ! atomic_status="$(atomic status --short 2>&1)"; then _fail "$label: read Atomic status" "$atomic_status"; return; fi if [[ -z "$git_status" ]]; then _pass "$label: git status is clean" else @@ -67,18 +69,22 @@ git add README.md src/nested/base.txt tracked-empty.txt git commit --quiet -m "Initial" MAIN="$(git_current_branch)" -assert_success "initial git import" atomic git import --no-vault +require_success "initial git import" atomic git import --no-vault assert_view_exists "initial branch has matching Atomic view" "$MAIN" assert_clean_statuses "after initial import" -assert_success "create matching Atomic feature view" \ +require_success "create matching Atomic feature view" \ atomic view create feature --draft --parent "$MAIN" -assert_success "switch to aligned Atomic feature view" atomic view switch feature --force -if [[ "$(git_current_branch)" == "feature" ]]; then +require_success "switch to aligned Atomic feature view" atomic view switch feature --force +if branch="$(git symbolic-ref --short HEAD 2>&1)" && [[ "$branch" == "feature" ]]; then _pass "Atomic view switch aligns Git on feature branch" else - _fail "Atomic view switch aligns Git on feature branch" \ - "expected feature, got $(git_current_branch)" + _fail "Atomic view switch aligns Git on feature branch" "expected feature, got $branch" + assert_current_view "switch preserves intended Atomic view" feature + assert_file_content "switch preserves tracked source" src/nested/base.txt 'base\n' + _skip "feature reconciliation and subsequent scenarios" "Git branch not aligned" + print_summary || true + exit 1 fi create_file "src/nested/feature.txt" "feature\n" : > feature-empty.txt diff --git a/tests/harness/40_git_shim_materialize_safety.sh b/tests/harness/legacy-shadow/40_git_shim_materialize_safety.sh old mode 100755 new mode 100644 similarity index 87% rename from tests/harness/40_git_shim_materialize_safety.sh rename to tests/harness/legacy-shadow/40_git_shim_materialize_safety.sh index a198f03c..03ba60dd --- a/tests/harness/40_git_shim_materialize_safety.sh +++ b/tests/harness/legacy-shadow/40_git_shim_materialize_safety.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# 40_git_shim_materialize_safety.sh — Offline shim-switch collision safety. +# HISTORICAL: coverage migrated to ../40_git_shim_safety.sh. # # Exercises pre-materialization refusal for untracked path-shape collisions and # the explicit before-materialize failpoint. Mid-write rollback remains outside @@ -17,7 +17,9 @@ begin_section "Prerequisites" require_git atomic_current_view() { - atomic view list 2>/dev/null | awk '/^\*/ { print $2 }' + local output + output="$(atomic view list --all --json 2>&1)" || return 1 + printf '%s\n' "$output" | python3 -c 'import json,sys; print(json.load(sys.stdin)["current_view"])' } checkpoint_hash() { @@ -46,8 +48,8 @@ snapshot_complete_files() { assert_clean_statuses() { local label="$1" local git_status atomic_status - git_status="$(git status --short)" - atomic_status="$(atomic status --short 2>/dev/null || true)" + if ! git_status="$(git status --short 2>&1)"; then _fail "$label: read Git status" "$git_status"; return; fi + if ! atomic_status="$(atomic status --short 2>&1)"; then _fail "$label: read Atomic status" "$atomic_status"; return; fi if [[ -z "$git_status" ]]; then _pass "$label: Git status is clean" else @@ -148,9 +150,19 @@ git add README.md git commit --quiet -m "Initial main projection" MAIN="$(git_current_branch)" -assert_success "import main into Atomic" atomic git import --no-vault -assert_success "create feature view" atomic view create feature --draft --parent "$MAIN" -assert_success "align feature view and Git branch" atomic view switch feature --force +require_success "import main into Atomic" atomic git import --no-vault +require_success "create feature view" atomic view create feature --draft --parent "$MAIN" +require_success "align feature view and Git branch" atomic view switch feature --force +if branch="$(git symbolic-ref --short HEAD 2>&1)" && [[ "$branch" == feature ]]; then + _pass "feature switch aligns Git branch" +else + _fail "feature switch aligns Git branch" "expected feature, got $branch" + assert_current_view "feature switch preserves Atomic view" feature + assert_file_content "feature switch preserves source bytes" README.md 'shim materialize safety\n' + _skip "collision and failpoint scenarios" "feature Git branch not aligned" + print_summary || true + exit 1 +fi create_file "feature-marker.txt" "feature shim checkpoint\n" git add feature-marker.txt git commit --quiet -m "Establish feature shim checkpoint" diff --git a/tests/harness/legacy-shadow/README.md b/tests/harness/legacy-shadow/README.md new file mode 100644 index 00000000..6b759a5a --- /dev/null +++ b/tests/harness/legacy-shadow/README.md @@ -0,0 +1,10 @@ +# Archived shadow and early-shim scenarios + +These scripts preserve the previous scenarios and assertions for coverage +review. They are **not active suites** and are not intended to run from here +(their original relative helper paths were retained). + +See [the current shim coverage map](../shim.md) for replacement suites, changed +contracts and coverage boundaries. Use `run_all.sh` with the old suite name to +select the current replacements; do not interpret archived results as current +shim evidence. diff --git a/tests/harness/merge_helpers.sh b/tests/harness/merge_helpers.sh index 3267460b..0250a9a9 100644 --- a/tests/harness/merge_helpers.sh +++ b/tests/harness/merge_helpers.sh @@ -103,10 +103,10 @@ assert_file_contains() { fi } -# Assert a file does NOT contain a substring (a missing file trivially passes). +# Assert an existing file does NOT contain a substring. assert_file_not_contains() { local desc="$1" path="$2" needle="$3" - if [[ ! -f "$path" ]]; then _pass "$desc"; return; fi + if [[ ! -f "$path" ]]; then _fail "$desc" "file does not exist: $path"; return; fi if grep -qF "$needle" "$path"; then _fail "$desc" "'$needle' should not be in $path but was found" else @@ -127,7 +127,18 @@ assert_present() { # Count substring (regex) occurrences; prints the count. count_occurrences() { local file="$1" pattern="$2" - grep -c "$pattern" "$file" 2>/dev/null || echo "0" + local count status + if count="$(grep -c -- "$pattern" "$file" 2>&1)"; then + printf '%s\n' "$count" + else + status=$? + if [[ $status -eq 1 ]]; then + printf '%s\n' "$count" + else + printf 'count failed (exit %s): %s\n' "$status" "$count" >&2 + return "$status" + fi + fi } # Assert a regex/substring appears exactly N times (substring count). @@ -135,7 +146,10 @@ assert_occurrence_count() { local desc="$1" file="$2" pattern="$3" expected="$4" if [[ ! -f "$file" ]]; then _fail "$desc" "file does not exist: $file"; return; fi local actual - actual="$(count_occurrences "$file" "$pattern")" + if ! actual="$(count_occurrences "$file" "$pattern" 2>&1)"; then + _fail "$desc" "$actual" + return + fi if [[ "$actual" -eq "$expected" ]]; then _pass "$desc" else @@ -147,9 +161,8 @@ assert_occurrence_count() { assert_occurrences() { local desc="$1" path="$2" needle="$3" expected="$4" local actual=0 - if [[ -f "$path" ]]; then - actual=$(grep -cxF "$needle" "$path" 2>/dev/null || true) - fi + if [[ ! -f "$path" ]]; then _fail "$desc" "file does not exist: $path"; return; fi + actual=$(grep -cxF "$needle" "$path" 2>/dev/null || true) if [[ "$actual" == "$expected" ]]; then _pass "$desc" else @@ -197,8 +210,11 @@ assert_file_equals() { expected_file="$(mktemp "${TMPDIR:-/tmp}/harness-expected-XXXXXX")" printf '%s' "$expected" > "$expected_file" local diff_out diff_exit - diff_out="$(diff -u "$expected_file" "$path" 2>&1)" - diff_exit=$? + if diff_out="$(diff -u "$expected_file" "$path" 2>&1)"; then + diff_exit=0 + else + diff_exit=$? + fi rm -f "$expected_file" case $diff_exit in 0) _pass "$desc" ;; @@ -231,12 +247,22 @@ assert_file_stable() { # The view name a fresh repo starts on (the one marked '*'). current_view() { - atomic view list 2>/dev/null | awk '/^\*/{print $2}' + local output + if ! output="$(atomic view list --all --json 2>&1)"; then + printf 'view list failed: %s\n' "$output" >&2 + return 1 + fi + printf '%s\n' "$output" | python3 -c 'import json,sys; name=json.load(sys.stdin)["current_view"]; assert isinstance(name,str) and name; print(name)' } # Hash of the most recent change on a view. tip_hash() { - atomic log --view "$1" 2>/dev/null | grep -oE '=== [A-Z0-9]{12}' | head -1 | cut -d' ' -f2 + local output + if ! output="$(atomic log --format json --count 1 --view "$1" 2>&1)"; then + printf 'log failed: %s\n' "$output" >&2 + return 1 + fi + printf '%s\n' "$output" | python3 -c 'import json,sys; rows=json.load(sys.stdin); assert len(rows)==1 and len(rows[0]["hash"])==52; print(rows[0]["hash"])' } # Backwards-compatible alias (27 spelling). tip_hash_of_view() { tip_hash "$1"; } @@ -267,23 +293,46 @@ line5 init_repo printf '%s' "$base_content" > f.txt - add_files f.txt >/dev/null - record_change "base" >/dev/null + require_success "add base file" atomic add f.txt + require_success "record base file" record_change "base" - BASE_VIEW="$(current_view)" + require_capture "read base view" current_view + BASE_VIEW="$COMMAND_OUTPUT" - new_view side-a >/dev/null - switch_view side-a >/dev/null + require_success "create side-a" new_view side-a + require_success "switch to side-a" switch_view side-a printf '%s' "$a_content" > f.txt - record_change "edit A" >/dev/null + require_success "record edit A" record_change "edit A" local hash_a - hash_a="$(tip_hash_of_view side-a)" + require_capture "read edit A hash" tip_hash_of_view side-a + hash_a="$COMMAND_OUTPUT" - switch_view "$BASE_VIEW" >/dev/null + require_success "switch back to base view" switch_view "$BASE_VIEW" printf '%s' "$b_content" > f.txt - record_change "edit B" >/dev/null - - atomic insert "$hash_a" >/dev/null 2>&1 + require_success "record edit B" record_change "edit B" + + local snapshot_dir history_before output + snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-merge-builder-XXXXXX")" + _HARNESS_TMPDIRS+=("$snapshot_dir") + cp f.txt "$snapshot_dir/f.txt" + require_capture "read base history before insert" atomic log --format json --view "$BASE_VIEW" --all + history_before="$COMMAND_OUTPUT" + if output="$(atomic insert "$hash_a" 2>&1)"; then + _pass "insert edit A" + else + _fail "insert edit A" "$output" + assert_success "refused insert preserves file bytes" cmp -s "$snapshot_dir/f.txt" f.txt + assert_current_view "refused insert preserves base view" "$BASE_VIEW" + require_capture "read base history after refusal" atomic log --format json --view "$BASE_VIEW" --all + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then + _pass "refused insert preserves base history" + else + _fail "refused insert preserves base history" "history changed" + fi + _skip "merge postconditions and later cases" "insert failed" + print_summary || true + exit 1 + fi } # Build a base repo, a `feature` draft with edit A, and edit B on the base @@ -294,20 +343,22 @@ build_case() { local base="$1" a="$2" b="$3" init_repo printf '%s' "$base" > f.txt - add_files f.txt >/dev/null - record_change "base" >/dev/null - BASE_VIEW="$(current_view)" + require_success "add base file" atomic add f.txt + require_success "record base file" record_change "base" + require_capture "read base view" current_view + BASE_VIEW="$COMMAND_OUTPUT" - new_view feature >/dev/null - switch_view feature >/dev/null + require_success "create feature" new_view feature + require_success "switch to feature" switch_view feature write_or_delete "$a" - record_change "edit A" >/dev/null - FEATURE_HASH="$(tip_hash feature)" + require_success "record edit A" record_change "edit A" + require_capture "read edit A hash" tip_hash feature + FEATURE_HASH="$COMMAND_OUTPUT" - switch_view "$BASE_VIEW" >/dev/null + require_success "switch to base view" switch_view "$BASE_VIEW" if [[ "$b" != "__UNCHANGED__" ]]; then write_or_delete "$b" - record_change "edit B" >/dev/null + require_success "record edit B" record_change "edit B" fi } @@ -351,8 +402,14 @@ assert_honest() { local m s l if [[ -f "$path" ]] && grep -qE '^>>>>>>>' "$path" 2>/dev/null; then m=1; else m=0; fi local sstatus cshort esc - sstatus="$(atomic status --short 2>/dev/null || true)" - cshort="$(atomic conflicts --short 2>/dev/null || true)" + if ! sstatus="$(atomic status --short 2>&1)"; then + _fail "$desc" "status command failed: $sstatus" + return + fi + if ! cshort="$(atomic conflicts --short 2>&1)"; then + _fail "$desc" "conflicts command failed: $cshort" + return + fi esc="$(printf '%s' "$path" | sed 's/[].[^$*+?{}()|\\/]/\\&/g')" if echo "$sstatus" | grep -qE "^C[[:space:]]+${esc}$"; then s=1; else s=0; fi if echo "$cshort" | grep -qE "^${esc}:"; then l=1; else l=0; fi diff --git a/tests/harness/run_all.sh b/tests/harness/run_all.sh index 18e4539a..8db770ed 100755 --- a/tests/harness/run_all.sh +++ b/tests/harness/run_all.sh @@ -6,6 +6,8 @@ # Usage: # ./tests/harness/run_all.sh # run everything # ./tests/harness/run_all.sh 01 03 # run only suites 01 and 03 +# ./tests/harness/run_all.sh shim # current offline shim contract +# ./tests/harness/run_all.sh --list shim # show canonical suites without running # ATOMIC_BIN=/path/to/atomic ./tests/harness/run_all.sh # custom binary # # Environment variables: @@ -18,7 +20,14 @@ set -euo pipefail HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" REPO_ROOT="$(cd "$HARNESS_DIR/../.." && pwd)" HARNESS_SKIP_EXIT=77 -export HARNESS_SKIP_EXIT +HARNESS_PARTIAL_EXIT=78 +export HARNESS_SKIP_EXIT HARNESS_PARTIAL_EXIT +LIST_ONLY=0 +FILTERS=() +for argument in "$@"; do + if [[ "$argument" == --list ]]; then LIST_ONLY=1; else FILTERS+=("$argument"); fi +done +if [[ ${#FILTERS[@]} -gt 0 ]]; then set -- "${FILTERS[@]}"; else set --; fi # ── Colours ───────────────────────────────────────────────────────────────── @@ -36,7 +45,9 @@ fi # ── Build binary if needed ────────────────────────────────────────────────── if [[ -z "${ATOMIC_BIN:-}" ]]; then - if [[ -x "$REPO_ROOT/target/debug/atomic" ]]; then + if [[ "$LIST_ONLY" == 1 ]]; then + ATOMIC_BIN="" + elif [[ -x "$REPO_ROOT/target/debug/atomic" ]]; then ATOMIC_BIN="$REPO_ROOT/target/debug/atomic" elif [[ -x "$REPO_ROOT/target/release/atomic" ]]; then ATOMIC_BIN="$REPO_ROOT/target/release/atomic" @@ -80,12 +91,38 @@ fi # If positional arguments are given, treat them as prefix filters. # ./run_all.sh 01 03 → only run 01_*.sh and 03_*.sh SELECTED_SUITES=() +add_suite_once() { + local candidate="$1" selected + for selected in ${SELECTED_SUITES[@]+"${SELECTED_SUITES[@]}"}; do + [[ "$candidate" == "$selected" ]] && return 0 + done + SELECTED_SUITES+=("$candidate") +} if [[ $# -gt 0 ]]; then for arg in "$@"; do + # Legacy shadow selectors remain usable but run the consolidated + # current contract, never the archived historical expectations. + alias_targets=() + case "${arg%.sh}" in + shim) alias_targets=(35_git_shim_serialization 39_git_shim_workflow 40_git_shim_safety 41_git_shim_hooks 42_git_shim_guard 43_git_shim_operation_recovery) ;; + 19|19_git_shadow) alias_targets=(39_git_shim_workflow 41_git_shim_hooks) ;; + 33|33_shadow_push_conflict_markers|34|34_shadow_provenance_guard|36|36_shadow_coherence_v2|38|38_shadow_drift_v3|40_git_shim_materialize_safety) alias_targets=(40_git_shim_safety) ;; + 35_shadow_concurrent_push) alias_targets=(35_git_shim_serialization) ;; + 37|37_shadow_view_branch_follow|39_git_shim_mvp) alias_targets=(39_git_shim_workflow) ;; + esac + if [[ ${#alias_targets[@]} -gt 0 ]]; then + echo " Selector $arg → ${alias_targets[*]}" + for target in "${alias_targets[@]}"; do + add_suite_once "$HARNESS_DIR/$target.sh" + done + # Numeric 19 also has a native unrecord suite; retain it. + if [[ "$arg" == 19 ]]; then add_suite_once "$HARNESS_DIR/19_unrecord.sh"; fi + continue + fi for suite in "${ALL_SUITES[@]}"; do base="$(basename "$suite")" if [[ "$base" == "${arg}"* ]]; then - SELECTED_SUITES+=("$suite") + add_suite_once "$suite" fi done done @@ -107,13 +144,17 @@ for s in "${SELECTED_SUITES[@]}"; do done echo "" +if [[ "$LIST_ONLY" == 1 ]]; then exit 0; fi + # ── Run suites ────────────────────────────────────────────────────────────── TOTAL_SUITES=0 PASSED_SUITES=0 SKIPPED_SUITES=0 +PARTIAL_SUITES=0 FAILED_SUITES=0 SKIPPED_SUITE_NAMES=() +PARTIAL_SUITE_NAMES=() FAILED_SUITE_NAMES=() TOTAL_TESTS=0 @@ -154,6 +195,10 @@ for suite in "${SELECTED_SUITES[@]}"; do SKIPPED_SUITES=$((SKIPPED_SUITES + 1)) SKIPPED_SUITE_NAMES+=("$suite_name") echo " ${YELLOW}Suite SKIPPED${RESET}" + elif [[ $suite_exit -eq $HARNESS_PARTIAL_EXIT ]]; then + PARTIAL_SUITES=$((PARTIAL_SUITES + 1)) + PARTIAL_SUITE_NAMES+=("$suite_name") + echo " ${YELLOW}Suite PARTIAL (skipped assertions)${RESET}" else FAILED_SUITES=$((FAILED_SUITES + 1)) FAILED_SUITE_NAMES+=("$suite_name") @@ -176,7 +221,7 @@ echo "${BOLD}╔═════════════════════ echo "${BOLD}║ Combined Results ║${RESET}" echo "${BOLD}╠═══════════════════════════════════════════════════════════╣${RESET}" echo "${BOLD}║${RESET} ${BOLD}║${RESET}" -echo "${BOLD}║${RESET} Suites: ${GREEN}${PASSED_SUITES} passed${RESET} ${YELLOW}${SKIPPED_SUITES} skipped${RESET} ${RED}${FAILED_SUITES} failed${RESET} / ${TOTAL_SUITES} total ${BOLD}║${RESET}" +echo "${BOLD}║${RESET} Suites: ${GREEN}${PASSED_SUITES} passed${RESET} ${YELLOW}${PARTIAL_SUITES} partial${RESET} ${YELLOW}${SKIPPED_SUITES} skipped${RESET} ${RED}${FAILED_SUITES} failed${RESET} / ${TOTAL_SUITES} total ${BOLD}║${RESET}" echo "${BOLD}║${RESET} ${BOLD}║${RESET}" if [[ ${#SKIPPED_SUITE_NAMES[@]} -gt 0 ]]; then @@ -187,6 +232,14 @@ if [[ ${#SKIPPED_SUITE_NAMES[@]} -gt 0 ]]; then echo "${BOLD}║${RESET} ${BOLD}║${RESET}" fi +if [[ ${#PARTIAL_SUITE_NAMES[@]} -gt 0 ]]; then + echo "${BOLD}║${RESET} ${YELLOW}Partially exercised suites:${RESET} ${BOLD}║${RESET}" + for name in "${PARTIAL_SUITE_NAMES[@]}"; do + printf "${BOLD}║${RESET} ${YELLOW}• %-51s${RESET} ${BOLD}║${RESET}\n" "$name" + done + echo "${BOLD}║${RESET} ${BOLD}║${RESET}" +fi + if [[ ${#FAILED_SUITE_NAMES[@]} -gt 0 ]]; then echo "${BOLD}║${RESET} ${RED}Failed suites:${RESET} ${BOLD}║${RESET}" for name in "${FAILED_SUITE_NAMES[@]}"; do @@ -203,4 +256,10 @@ echo "" if [[ $FAILED_SUITES -gt 0 ]]; then exit 1 fi +if [[ $PARTIAL_SUITES -gt 0 || ( $SKIPPED_SUITES -gt 0 && $PASSED_SUITES -gt 0 ) ]]; then + exit "$HARNESS_PARTIAL_EXIT" +fi +if [[ $SKIPPED_SUITES -gt 0 ]]; then + exit "$HARNESS_SKIP_EXIT" +fi exit 0 diff --git a/tests/harness/shim.md b/tests/harness/shim.md new file mode 100644 index 00000000..282adc84 --- /dev/null +++ b/tests/harness/shim.md @@ -0,0 +1,102 @@ +# Current offline shim harness + +The old shadow-sync and early shim suites overlapped and assumed different +publication models. The active suites now share signed, opt-in, local-only +fixtures in `shim_helpers.sh`. They never configure a remote or push one. + +| Suite | Contract | +|---|---| +| `35_git_shim_serialization.sh` | Hold the common operation lock in a different process, prove a typed refusal, release it and verify convergence; two reconcile callers must not double-publish. | +| `39_git_shim_workflow.sh` | Enroll; native record projects automatically; adopt Git-authored branches without rewriting Git-rendered files; repeat reconciliation preserves identities; native Draft switching detaches HEAD; explicit local `shim publish` creates a branch; explicit shim switching restores exact files, empty files, deletions, executable modes and symlinks. Unenrolled native repos do not acquire mirror branches. | +| `40_git_shim_safety.sh` | Dirty/marker refusal, resolved retry, excluded metadata in the index, forged provenance, stale baseline, untracked file/parent/directory collisions, pre-materialize injection and successful retry. | +| `41_git_shim_hooks.sh` | Advisory event/receipt evidence, correctness without checkout hooks, owned dispatchers, idempotent installation/uninstallation, and byte/mode preservation of unmanaged hooks. | +| `42_git_shim_guard.sh` | Strict early stale-baseline refusal across command boundaries, diagnostics and raw database-byte non-mutation. Its existing assertions remain unchanged; consolidation does not waive known failures. | +| `43_git_shim_operation_recovery.sh` | Separate interruption/recovery contract. Retained unchanged; missing injection seams are not crash passes. | + +```sh +ATOMIC_BIN=/absolute/path/to/atomic bash tests/harness/run_all.sh --list shim +ATOMIC_BIN=/absolute/path/to/atomic bash tests/harness/run_all.sh shim +bash tests/harness/shim_harness_selftest.sh +``` + +`--list` does not build or invoke Atomic. Canonical suite discovery excludes the +`legacy-shadow/` directory. Old runner selectors are redirected and deduplicated: + +| Historical selector | Active replacement | +|---|---| +| `19_git_shadow` | 39 workflow + 41 hooks | +| `33_shadow_push_conflict_markers` | 40 safety | +| `34_shadow_provenance_guard` | 40 safety | +| `35_shadow_concurrent_push` | 35 serialization | +| `36_shadow_coherence_v2` | 40 safety | +| `37_shadow_view_branch_follow` | 39 workflow | +| `38_shadow_drift_v3` | 40 safety (strict cross-command gate remains in 42) | +| `39_git_shim_mvp` | 39 workflow | +| `40_git_shim_materialize_safety` | 40 safety | + +Numeric selectors retain their normal native-suite matches (notably `19_unrecord`). +Select canonical names if you do not want every suite sharing a numeric prefix. + +## Deliberate contract changes + +- Native Draft view switching does **not** promise to invent an attached Git + mirror branch. Check detached projection; require explicit `shim publish` + when a named branch is wanted. `shim switch` remains an explicit operation + that attaches an already interpreted target branch. +- An enrolled native record already projects to Git. A second manual shadow + push/reconcile is not required to publish that local record, nor should it + manufacture another commit. +- Private-path policy is checked at its real boundary (index/projection), not + by assuming `atomic add` must proceed past a stale baseline. `.atomic/` and + `.atomicignore` must stay private; `.vault/` follows the configured policy, + not the old blanket “all vault paths are always excluded” expectation. +- Foreground refusal can append WIP-capture evidence. The opt-in capture + comparator permits only verified system capture/rebase records and a + verified switch **back to the original view**, with a capture present. It + checks receipts for each new operation. It still requires unchanged user + history/current view, filesystem bytes/kinds/modes, refs, Git HEAD/index, + checkpoint, every existing change object, and all existing operation-log + entries (apart from their derived head flags). This comparator does not + byte-snapshot the redb database or every prior receipt body. + Strict refusal cases permit no operation/object additions at all. Suite 42 + retains the stronger raw-database non-mutation gate separately. +- Legacy `shadow-validate:V1/V2/V3/V4` strings and `hook-errors.log` are not the + current authority. Check actual refusal, causal state, and journal receipts. + Marker override in a native-only repo remains in suite 30; it is not a bypass + of the enrolled shim's publication policy. +- The old concurrency suite explicitly skipped proof of contention. The new + suite holds the actual common lock before launching the other process; + the subsequent two-caller burst is labeled only as convergence smoke coverage. + +## Coverage boundaries, not silent omissions + +The original scripts are preserved verbatim apart from their archival headers +under `legacy-shadow/`; they are reference material, not executable active +contracts. Initial/imported Git history fidelity stays in suites 10/13; squash +classification and review evidence stay in 32 (known failures are not removed). +The old 19 dirty-background-import fixture manually mixed divergent Git index +and draft bytes, and its recovery fixture edited `.atomic/current_view` plus a +legacy pending marker. Those are not valid setup for today's working-copy +authority. Current safety tests require refusal without external data loss; +operation recovery is owned by 43, not counted as passed by a note in suite 40. +The 45 watcher, 47 real-repository and 48 GitHub suites remain separate and +retain their opt-in/network requirements; they are not run by this consolidation. + +## Consolidation validation + +Validated offline against the pinned default-feature debug binary used for the +17/22/28 fixes (`atomic-harness17` in the disposable harness evidence directory): + +| Suite | Observed result | +|---|---| +| 35 serialization | 30 passed, 0 failed, 0 skipped | +| 39 workflow | 99 passed, 0 failed, 0 skipped | +| 40 safety | 115 passed, 0 failed, 0 skipped | +| 41 hooks | 38 passed, 0 failed, 0 skipped | +| 42 strict guard (unchanged) | 104 passed, 12 failed, 0 skipped | +| 43 operation recovery (unchanged) | Not rerun in this consolidation | + +Suite 42 still exposes its existing diagnostic, raw-database refusal and Git +HEAD-preservation failures. Neither the WIP comparator nor consolidation removes +those assertions. The runner/discovery selftest passed seven checks. This is +not a full workspace, network, watcher or crash-injection certification. diff --git a/tests/harness/shim_harness_selftest.sh b/tests/harness/shim_harness_selftest.sh new file mode 100755 index 00000000..53598810 --- /dev/null +++ b/tests/harness/shim_harness_selftest.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Runner/discovery contracts only; no repository mutations or CLI required. +set -euo pipefail +HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" +python3 - "$HARNESS_DIR" <<'PY' +import pathlib, re, subprocess, sys +root = pathlib.Path(sys.argv[1]) +def selected(*args): + output = subprocess.check_output(['bash',str(root/'run_all.sh'),'--list',*args], text=True) + return re.findall(r'• ([^\s]+\.sh)', output) +expected = ['35_git_shim_serialization.sh','39_git_shim_workflow.sh', + '40_git_shim_safety.sh','41_git_shim_hooks.sh','42_git_shim_guard.sh', + '43_git_shim_operation_recovery.sh'] +assert selected('shim') == expected +assert selected('33','34','36','38','40_git_shim_materialize_safety') == ['40_git_shim_safety.sh'] +assert selected('37','39_git_shim_mvp','39_git_shim_workflow') == ['39_git_shim_workflow.sh'] +assert selected('35_shadow_concurrent_push') == ['35_git_shim_serialization.sh'] +assert selected('19') == ['39_git_shim_workflow.sh','41_git_shim_hooks.sh','19_unrecord.sh'] +all_suites = selected() +assert len(all_suites) == len(set(all_suites)) +assert not any('shadow' in name or name in ('39_git_shim_mvp.sh','40_git_shim_materialize_safety.sh') for name in all_suites) +assert set(expected) <= set(all_suites) +print('7 shim harness selector/discovery checks passed') +PY diff --git a/tests/harness/shim_helpers.sh b/tests/harness/shim_helpers.sh new file mode 100644 index 00000000..d572ecb9 --- /dev/null +++ b/tests/harness/shim_helpers.sh @@ -0,0 +1,202 @@ +#!/usr/bin/env bash +# Shared fixtures for the current, opt-in causal shim (not legacy shadow sync). +# Source helpers.sh first. All writes stay in disposable repositories; no remote +# is configured and every Git publication check is local. + +shim_fixture() { + local label="$1" private + make_temp_repo "$label" + private="$(mktemp -d "${TMPDIR:-/tmp}/atomic-shim-private-XXXXXX")" + _HARNESS_TMPDIRS+=("$private") + export HOME="$private/home" ATOMIC_HOME="$private/home/.atomic" + export ATOMIC_SERVICE=local ATOMIC_NONINTERACTIVE=1 + export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 GIT_TERMINAL_PROMPT=0 + unset ATOMIC_RPC ATOMIC_DAEMON_SOCKET + mkdir -p "$HOME" + init_git_repo + git config push.default nothing + printf 'base\n' > tracked.txt + printf 'main-only\n' > base-only.txt + : > empty.txt + require_success "$label: stage baseline" git add tracked.txt empty.txt base-only.txt + require_success "$label: commit baseline" git commit -qm baseline + SHIM_MAIN="$(git_current_branch)" + require_success "$label: import" atomic git import --no-vault + python3 - "$private/binding.key" <<'PY' +import os, secrets, sys +fd = os.open(sys.argv[1], os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) +with os.fdopen(fd, 'w') as file: + file.write(secrets.token_hex(32)) +PY + require_success "$label: enroll signed local shim" atomic git shim enable --no-refspecs --binding-key-file "$private/binding.key" + require_success "$label: anchor" atomic git shim reconcile + shim_clean "$label: baseline" +} + +shim_clean() { + local label="$1" output + require_success "$label: verify projection" atomic git shim verify + require_capture "$label: read Git status" git status --porcelain + output="$COMMAND_OUTPUT" + if [[ -z "$output" ]]; then _pass "$label: Git clean"; else _fail "$label: Git clean" "$output"; fi + require_capture "$label: read Atomic status" atomic status --short + output="$COMMAND_OUTPUT" + if [[ -z "$output" ]]; then _pass "$label: Atomic clean"; else _fail "$label: Atomic clean" "$output"; fi +} + +shim_assert_blob() { + local label="$1" path="$2" expected="$3" + python3 - "$path" "$expected" <<'PY' +import pathlib, subprocess, sys +path, expected = sys.argv[1], sys.argv[2].encode() +assert pathlib.Path(path).read_bytes() == expected, ('working bytes', path) +assert subprocess.check_output(['git', 'show', 'HEAD:' + path]) == expected, ('Git blob', path) +PY +} + +shim_file_bytes() { + python3 - "$1" "$2" <<'PY' +import pathlib, sys +assert pathlib.Path(sys.argv[1]).read_bytes() == sys.argv[2].encode(), sys.argv[1] +PY +} + +# Logical refusal snapshot: refs (including detached HEAD), index, checkpoint, +# exact file bytes/kinds/modes, immutable objects, both operation DAGs, +# and effective history. Advisory event logs are deliberately not authority. +# The dedicated 42 guard suite additionally checks raw database-byte immutability. +shim_snapshot() { + python3 - "$ATOMIC_BIN" "$1" <<'PY' +import hashlib, json, os, pathlib, stat, subprocess, sys +binary, destination = sys.argv[1:] +def call(*args): + return subprocess.check_output(args, stderr=subprocess.PIPE).decode() +def digest(path): + return hashlib.sha256(path.read_bytes()).hexdigest() if path.exists() else None +files = {} +for directory, dirs, names in os.walk('.'): + dirs[:] = sorted(name for name in dirs if name not in ('.git', '.atomic')) + for name in dirs: + p = pathlib.Path(directory, name) + if not p.is_symlink(): + mode = p.lstat().st_mode + files[str(p)] = [stat.S_IFMT(mode), stat.S_IMODE(mode), None] + for name in sorted(names + [d for d in dirs if pathlib.Path(directory, d).is_symlink()]): + p = pathlib.Path(directory, name) + mode = p.lstat().st_mode + files[str(p)] = [stat.S_IFMT(mode), stat.S_IMODE(mode), + os.readlink(p) if p.is_symlink() else digest(p)] + dirs[:] = [d for d in dirs if not pathlib.Path(directory, d).is_symlink()] +result = dict(head=call('git','rev-parse','HEAD'), refs=call('git','show-ref'), + head_file=pathlib.Path('.git/HEAD').read_text(), index=digest(pathlib.Path('.git/index')), + checkpoint=digest(pathlib.Path('.atomic/bridge/workspace.json')), files=files, + changes={str(p):digest(p) for p in sorted(pathlib.Path('.atomic/changes').rglob('*.change'))}, + working_ops=json.loads(call(binary,'op','log','--json')), + repository_ops=json.loads(call(binary,'op','log','--repository','--json')), + current_view=json.loads(call(binary,'view','list','--all','--json'))['current_view'], + history=json.loads(call(binary,'log','--all','--format','json'))) +pathlib.Path(destination).write_text(json.dumps(result, sort_keys=True, indent=2)) +PY +} + +shim_locked_command() { + python3 - "$ATOMIC_BIN" "$@" <<'PY' +import fcntl, subprocess, sys +with open('.atomic/bridge.lock', 'r+b') as lock: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + result = subprocess.run(sys.argv[1:], timeout=30) + sys.exit(result.returncode) +PY +} + +shim_tracked_file_metadata() { + python3 - "$1" <<'PY' +import hashlib, json, os, pathlib, stat, subprocess, sys +result = {} +for name in subprocess.check_output(['git','ls-files','-z']).split(b'\0'): + if not name: + continue + p = pathlib.Path(os.fsdecode(name)) + metadata = p.lstat() + result[str(p)] = [metadata.st_mode, metadata.st_ino, metadata.st_mtime_ns, + os.readlink(p) if stat.S_ISLNK(metadata.st_mode) + else hashlib.sha256(p.read_bytes()).hexdigest()] +pathlib.Path(sys.argv[1]).write_text(json.dumps(result, sort_keys=True)) +PY +} + +shim_refusal() { + local label="$1" diagnostic="$2" evidence output rc capture=0 + shift 2 + if [[ "${1:-}" == --allow-wip-capture ]]; then capture=1; shift; fi + evidence="$(mktemp -d "${TMPDIR:-/tmp}/atomic-shim-refusal-XXXXXX")" + _HARNESS_TMPDIRS+=("$evidence") + require_success "$label: snapshot before" shim_snapshot "$evidence/before.json" + if output="$("$@" 2>&1)"; then rc=0; else rc=$?; fi + if [[ "$rc" != 0 ]]; then _pass "$label: refused"; else _fail "$label: refused" "unexpected success: $output"; fi + if printf '%s\n' "$output" | grep -qiE "$diagnostic"; then + _pass "$label: diagnostic" + else + _fail "$label: diagnostic" "$output" + fi + require_success "$label: snapshot after" shim_snapshot "$evidence/after.json" + if python3 - "$evidence/before.json" "$evidence/after.json" "$capture" "$ATOMIC_BIN" <<'PY' +import json, subprocess, sys +before, after = [json.load(open(path)) for path in sys.argv[1:3]] +if sys.argv[3] == '0': + assert before == after, 'strict refusal snapshot changed' +else: + # Foreground transition capture is permitted, not silently ignored: only + # verified private capture/rebase operations may be appended. Existing objects and + # operation-log entries are immutable; visible history and all external leases + # remain byte-identical. A capture may temporarily switch into wc/; + # the final user view must not change. No publication/recovery is permitted. + for key in before.keys() - {'changes','working_ops','repository_ops'}: + assert before[key] == after[key], key + for path, digest in before['changes'].items(): + assert after['changes'].get(path) == digest, ('changed object',path) + added_ops = set() + capture_seen = False + switch_seen = False + for scope in ('working_ops','repository_ops'): + old = {row['id']:row for row in before[scope]['entries']} + new = {row['id']:row for row in after[scope]['entries']} + assert old.keys() <= new.keys(), ('lost operations',scope) + for identity, entry in old.items(): + assert {k:v for k,v in entry.items() if k != 'is_head'} == { + k:v for k,v in new[identity].items() if k != 'is_head'}, ('changed operation',identity) + for identity in new.keys() - old.keys(): + entry = new[identity] + assert entry['verification'] == 'verified', entry + assert entry['actor'].get('kind') == 'system', entry + actor = entry['actor'].get('name') + assert (entry['kind'], actor) in ( + ('record','repository-record'), ('record','bridge-pre-transition-capture'), + ('record','bridge-snapshot-rebase'), ('switch_view','repository-switch')), entry + detail = json.loads(subprocess.check_output([sys.argv[4],'op','show',identity,'--json'])) + assert detail['verification'] == 'verified' and any( + receipt['kind'] == 'verified' for receipt in detail['receipts']), detail + if actor == 'repository-record': + views = [detail.get('before',{}).get('view'), detail['delta']['after'].get('view')] + assert any(view and view['name'].startswith('wc/') for view in views), detail + if actor == 'repository-switch': + # Capturing WIP can restore the user view after temporarily + # visiting its baseline. It must return to the original view, + # never leave a refused target selected. + assert detail['delta']['after']['view']['name'] == before['current_view'], detail + switch_seen = True + capture_seen |= actor == 'bridge-pre-transition-capture' + added_ops.add(identity) + if before[scope] != after[scope]: + assert new.keys() - old.keys(), ('head changed without append',scope) + if before['changes'] != after['changes']: + assert added_ops, 'unexplained object publication' + assert not switch_seen or capture_seen, 'switch without a verified WIP capture' + print('verified append-only WIP capture operations:', len(added_ops)) +PY + then + _pass "$label: preserves external state and immutable history (WIP capture policy=$capture)" + else + _fail "$label: preserves external state and immutable history (WIP capture policy=$capture)" "$(diff -u "$evidence/before.json" "$evidence/after.json" || true)" + fi +} From afd6e8d8d6897d3b04d53085ce30da1b3872c203 Mon Sep 17 00:00:00 2001 From: Lee Faus Date: Thu, 5 Nov 2026 18:40:43 -0500 Subject: [PATCH 04/14] native atomic harness runs complete --- .../repository_query_messages.proto | 4 +- libatomic/src/daemon/services.rs | 20 +- libatomic/tests/local_service_test.rs | 57 ++++ tests/harness/23_shared_view_parent_chain.sh | 58 +++- tests/harness/32_review_gate_insert.sh | 299 +----------------- tests/harness/40_switch_transactionality.sh | 105 +----- .../legacy-shadow/32_review_gate_insert.sh | 295 +++++++++++++++++ tests/harness/native.md | 83 +++++ tests/harness/native_helpers.sh | 24 ++ tests/harness/native_review_insert_test.py | 183 +++++++++++ tests/harness/native_support.py | 56 ++++ tests/harness/native_switch_test.py | 92 ++++++ tests/harness/run_all.sh | 1 + tests/harness/shim.md | 5 +- tests/harness/shim_harness_selftest.sh | 3 +- 15 files changed, 866 insertions(+), 419 deletions(-) create mode 100644 tests/harness/legacy-shadow/32_review_gate_insert.sh create mode 100644 tests/harness/native.md create mode 100644 tests/harness/native_helpers.sh create mode 100644 tests/harness/native_review_insert_test.py create mode 100644 tests/harness/native_support.py create mode 100644 tests/harness/native_switch_test.py diff --git a/libatomic/proto/atomic/services/repository_query/repository_query_messages.proto b/libatomic/proto/atomic/services/repository_query/repository_query_messages.proto index 7155d442..1dd19c96 100644 --- a/libatomic/proto/atomic/services/repository_query/repository_query_messages.proto +++ b/libatomic/proto/atomic/services/repository_query/repository_query_messages.proto @@ -78,8 +78,8 @@ message FileStatusEntry { message LogRequest { RepositoryRef repository = 1; - bool all_views = 2; // `--all` - optional string view = 3; // required unless all_views; no daemon current-view default + bool all_views = 2; // Legacy field name: include inherited history (`--all`) of the selected view. + optional string view = 3; // Explicit target is honored even with all_views; otherwise use the request's working-copy view. optional PageCursor cursor = 4; optional PageBudget budget = 5; repeated string path_filter = 6; // `--path`: entries whose changes touch it diff --git a/libatomic/src/daemon/services.rs b/libatomic/src/daemon/services.rs index adc52ea8..e7c69040 100644 --- a/libatomic/src/daemon/services.rs +++ b/libatomic/src/daemon/services.rs @@ -399,16 +399,16 @@ impl repository_query_service_server::RepositoryQueryService for QueryImpl { self.state.log_rpc("Log", Some(&handle)); let gate_handle = handle.clone(); let _gate = gate_handle.exclusive().await; - let view = if request.all_views { - None - } else { - Some( - request - .view - .clone() - .unwrap_or_else(|| handle.current_view()), - ) - }; + // `all_views` is the legacy wire name for CLI --all: include the + // selected view's inherited history, not discard its explicit target. + // Dropping `view` here made `log --view dev --all` read the current + // Draft instead, falsely showing unpromoted records on the Shared view. + let view = Some( + request + .view + .clone() + .unwrap_or_else(|| handle.current_view()), + ); let include_inherited = request.all_views; let tags_only = request.tags_only; let path_filter = request.path_filter.clone(); diff --git a/libatomic/tests/local_service_test.rs b/libatomic/tests/local_service_test.rs index ce95ac65..3e021992 100644 --- a/libatomic/tests/local_service_test.rs +++ b/libatomic/tests/local_service_test.rs @@ -240,6 +240,63 @@ async fn local_add_record_log_and_change_use_existing_handlers() { assert!(without.change_bundle.is_none()); } +#[tokio::test] +async fn local_log_all_preserves_explicit_view_and_includes_ancestors() { + use atomic_core::change::ChangeHeader; + use atomic_repository::RecordOptions; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + let record = |repo: &Repository, message: &str| { + repo.record( + wc, + ChangeHeader::new(message), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap(); + }; + std::fs::write(dir.path().join("base.txt"), b"base\n").unwrap(); + repo.add(wc, "base.txt", Default::default()).unwrap(); + record(&repo, "shared baseline"); + // Seed baseline membership so --all retains inherited entries that the + // ordinary Draft-only log hides, while still honoring an explicit view. + repo.create_view_from("agent", "dev").unwrap(); + repo.switch_view(wc, "agent").unwrap(); + std::fs::write(dir.path().join("draft.txt"), b"draft\n").unwrap(); + repo.add(wc, "draft.txt", Default::default()).unwrap(); + record(&repo, "draft only"); + drop(repo); + let service = LocalService::new(); + let repository = resolve(&service, dir.path()).await; + for (view, all, expected) in [ + (Some("dev"), true, vec!["shared baseline"]), + (Some("agent"), true, vec!["draft only", "shared baseline"]), + (Some("agent"), false, vec!["draft only"]), + (None, true, vec!["draft only", "shared baseline"]), + ] { + let response: proto::LogResponse = invoke( + &service, + "/atomic.RepositoryQueryService/Log", + &proto::LogRequest { + repository: Some(repository.clone()), + view: view.map(str::to_string), + all_views: all, + ..Default::default() + }, + ) + .await; + let messages: Vec<_> = response + .entries + .iter() + .map(|entry| entry.message.as_deref().unwrap()) + .collect(); + assert_eq!(messages, expected, "view={view:?}, all={all}"); + } +} + #[tokio::test] async fn local_record_reports_native_span_semantic_statistics() { assert_local_record_reports_semantic_statistics(true).await; diff --git a/tests/harness/23_shared_view_parent_chain.sh b/tests/harness/23_shared_view_parent_chain.sh index d382c6e5..59d43edb 100755 --- a/tests/harness/23_shared_view_parent_chain.sh +++ b/tests/harness/23_shared_view_parent_chain.sh @@ -1,14 +1,7 @@ #!/usr/bin/env bash -# 23_shared_view_parent_chain.sh — Shared view parent chain filter tests. -# -# The CLI creates draft children only; promotion produces a shared root. -# The shared-child scenario cannot be constructed through this CLI and must -# not be counted as passed. The draft-chain cases below remain useful coverage. -# -# Bug: collect_visible_change_ids only walks parent chain for draft views. -# Shared views with parents (e.g. staging → dev) only see their own -# VIEW_CHANGES, missing ancestor changes. This could produce incomplete -# content or conflict markers. +# Native hierarchy contract: Draft chains inherit; Shared views are roots. +# Keep the historical filename for runner compatibility. Shared children are +# not a supported repository identity and are not fabricated in this harness. HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" @@ -69,8 +62,6 @@ assert_draft_parent() { "$COMMAND_OUTPUT" "$name" "$parent" } -_skip "shared-scope child parent-chain coverage" "CLI creates draft children; promotion clears the parent" - # ═══════════════════════════════════════════════════════════════════════════ begin_section "Case 1: Draft child chain dev → staging" # ═══════════════════════════════════════════════════════════════════════════ @@ -212,6 +203,45 @@ assert_file_exists "library.rs on dev" "library.rs" assert_file_contains "dev has 42" "library.rs" "42" assert_no_conflict_markers "no markers on dev" "library.rs" -# ═══════════════════════════════════════════════════════════════════════════ - +begin_section "Case 4: Promotion creates an independent Shared root" +# staging explicitly holds the baseline references inserted above. Promotion +# must preserve them while severing parent inheritance, not invent a Shared +# child that the native repository API deliberately disallows. +require_capture "read staging history before promotion" atomic log --view staging --all --format json +staging_history="$COMMAND_OUTPUT" +require_success "promote staging to Shared root" atomic view promote staging +require_capture "read promoted staging metadata" atomic view list --all --json +assert_success "promoted staging is Shared with no parent" python3 -c \ + 'import json,sys; rows=json.loads(sys.argv[1])["views"]; view=next(x for x in rows if x["name"]=="staging"); assert view["scope"]=="shared" and view["parent"] is None' \ + "$COMMAND_OUTPUT" +require_capture "read staging history after promotion" atomic log --view staging --all --format json +assert_success "promotion preserves original change identities" python3 -c \ + 'import json,sys; before=json.loads(sys.argv[1]); after=json.loads(sys.argv[2]); assert [r["hash"] for r in before]==[r["hash"] for r in after]' \ + "$staging_history" "$COMMAND_OUTPUT" +create_file late-dev.txt 'new dev-only history' +require_success "add later dev path" atomic add late-dev.txt +require_success "record later dev path" record_change 'later dev change' +require_success "switch to promoted Shared root" switch_view staging +assert_file_content "promoted root retains baseline content" library.rs 'pub fn core_fn() -> i32 { 42 }' +assert_file_not_exists "Shared root does not inherit later former-parent change" late-dev.txt +require_success "create child anchored on promoted root" new_view staging-child --draft --parent staging +assert_draft_parent staging-child staging +create_file shared-only.txt 'owned by staging root' +require_success "add Shared root change" atomic add shared-only.txt +require_success "record Shared root change after child creation" record_change 'shared root update' +require_success "switch to child of promoted root" switch_view staging-child +assert_file_content "child inherits later Shared-parent change" shared-only.txt 'owned by staging root' +assert_file_not_exists "child does not inherit former root ancestors" late-dev.txt +assert_file_content "child sees correct baseline" library.rs 'pub fn core_fn() -> i32 { 42 }' +require_capture "child is clean after inherited materialization" atomic status --short --no-untracked +if [[ -z "$COMMAND_OUTPUT" ]]; then _pass "child working copy is clean"; else _fail "child working copy is clean" "$COMMAND_OUTPUT"; fi +require_success "return to original Shared root" switch_view dev +assert_file_content "dev retains its later change" late-dev.txt 'new dev-only history' +assert_file_not_exists "independent Shared-root change does not leak into dev" shared-only.txt +require_capture "snapshot view metadata before missing-parent refusal" atomic view list --all --json +views_before="$COMMAND_OUTPUT" +assert_failure "missing parent cannot create a partial view" atomic view create invalid-child --parent missing-parent +require_capture "read view metadata after missing-parent refusal" atomic view list --all --json +assert_success "missing-parent refusal preserves view identities" python3 -c \ + 'import json,sys; assert json.loads(sys.argv[1])==json.loads(sys.argv[2])' "$views_before" "$COMMAND_OUTPUT" print_summary diff --git a/tests/harness/32_review_gate_insert.sh b/tests/harness/32_review_gate_insert.sh index ca3b62f4..151e44d0 100755 --- a/tests/harness/32_review_gate_insert.sh +++ b/tests/harness/32_review_gate_insert.sh @@ -1,295 +1,12 @@ #!/usr/bin/env bash -# 32_review_gate_insert.sh — Executable spec for squash-import → insert originals -# -# Walks the workflow from SPEC-review-gate-insert-originals.md: -# -# 1. git repo + `atomic git import` → shared view (named after git default) -# 2. Agent work in an Atomic draft → per-turn CHANGE RECORDS (the originals) -# 3. Simulated GitHub squash-merge → a single git commit on the shared -# branch whose body carries the originals' `Atomic-Changes` trailers -# (multiple blocks, mimicking GitHub concatenating each squashed body) -# 4. `atomic git import --incremental --branch ` -# -# TARGET (SPEC §2, §4): the squash must NOT become a new change record. Instead -# the importer inserts the named originals into the shared view (they already -# live in the graph) and represents the squash as an aggregate ReviewGate TAG -# (from…to, count, inserted:true). Deleting the draft must then be safe. -# -# STATUS: the insert path is not implemented yet. Target-behavior checks are -# gated on $EXPECT_INSERT: -# - unset/0 → reported as pending (skipped), suite is partial -# - 1 → hard assertions, so this file drives implementation red→green -# -# What already holds today (hard-asserted here, no gate): -# - the import succeeds and the shared view materializes the squash content -# - a ReviewGate tag is created for the squash -# - its metadata lists EVERY original hash across ALL trailer blocks -# (the BUG-review-gate-original-hashes.md fix, validated end-to-end) - +# Native review-readiness -> exact original-change insertion -> Draft retirement. +# No Git import, fabricated trailer, surrogate squash, or EXPECT_INSERT skip. +# Readiness is a triage verdict checked by this workflow, not a claim that every +# low-level insertion API automatically enforces the triage policy. HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" - -# ── Target-behavior gate ────────────────────────────────────────────────── -# -# assert_target -# pass if the command succeeds (target behavior is present) -# fail if it does not AND EXPECT_INSERT=1 (drives implementation) -# pending otherwise (feature not built yet — keeps the suite green) -assert_target() { - local desc="$1"; shift - if "$@" >/dev/null 2>&1; then - _pass "$desc" - elif [[ "${EXPECT_INSERT:-0}" == "1" ]]; then - _fail "$desc" "target behavior (SPEC) not met" - else - _skip "$desc" "pending: insert-originals (SPEC §4) not implemented" - fi -} - -# Predicate helpers (used as commands for assert_* so they run under the gate). -# -# main_has_record matches the change's `hash` FIELD exactly — not a raw substring -# of the JSON. A squash change record embeds the originals' `Atomic-Changes` -# trailers in its commit message, so a substring grep would spuriously match -# even when the record was never inserted. Exact hash-field membership is the -# real "is this original a change in the shared view?" test. -# These predicates capture command output into a variable and then match with a -# here-string, deliberately avoiding `cmd | grep -q`. Under `set -o pipefail` -# (active via helpers.sh), `grep -q` exits on first match and closes the pipe; -# the still-writing `atomic` process then dies with SIGPIPE, and pipefail -# reports the pipeline as failed even though the text matched. Two more notes: -# * `grep -a` forces text mode — `tag show` / the router emit UTF-8 glyphs -# (…, ✓, —) that make macOS/BSD grep treat the stream as binary. -# * `main_has_record` matches the `hash` FIELD exactly (whole line), so a -# squash record's embedded trailer text can't produce a false positive. -main_view_hashes() { - local out - out="$(atomic log --view "$MAIN" -f json 2>&1)" || return 1 - printf '%s\n' "$out" | python3 -c 'import json,sys; rows=json.load(sys.stdin); assert isinstance(rows,list); print("\n".join(r["hash"] for r in rows))' -} -main_has_record() { - local out; out="$(main_view_hashes)" - grep -qxaF "$1" <<<"$out" -} -tag_show_contains() { - local out; out="$(atomic tag show "$TAG_NAME" 2>/dev/null)" - grep -qaF "$1" <<<"$out" -} -# Dry-run router recommendation for the current target (SPEC §4.4). -dry_run_recommends_insert() { - local out; out="$(atomic git import --incremental --branch "$MAIN" --dry-run 2>&1)" - grep -qaiE '(recommend|route|use|will|would|should).*(insert|original)|(insert|original).*(recommend|route|instead)' <<<"$out" -} -# The shared view materializes `src/two.rs` after switching to it. -shared_view_materializes_two() { - atomic view switch "$MAIN" --force >/dev/null 2>&1 && test -f src/two.rs -} - -# ── Suite banner ────────────────────────────────────────────────────────── - -echo "" -echo "${BOLD}══════════════════════════════════════════════════════════════${RESET}" -echo "${BOLD} Suite: 32_review_gate_insert${RESET}" -echo "${BOLD}══════════════════════════════════════════════════════════════${RESET}" - -# ════════════════════════════════════════════════════════════════════════ -# Section 1: Prerequisites -# ════════════════════════════════════════════════════════════════════════ - -begin_section "Prerequisites" -require_git - -# ════════════════════════════════════════════════════════════════════════ -# Section 2: Seed the shared view from git -# ════════════════════════════════════════════════════════════════════════ - -begin_section "Seed shared view from git import" - -make_temp_repo "review-gate-insert" -init_git_repo -git_commit "Initial" "README.md" "# Project" - -require_success "git import seeds the shared view" atomic git import --no-vault - -MAIN="$(git_default_branch)" -assert_current_view "shared view is '$MAIN'" "$MAIN" - -# ════════════════════════════════════════════════════════════════════════ -# Section 3: Agent work in a draft view — the original change records -# ════════════════════════════════════════════════════════════════════════ - -begin_section "Record originals in a draft view" - -require_success "create + switch to draft" \ - atomic view create agent --draft --parent "$MAIN" --switch - -# Two per-turn changes. Their on-disk content must match what the squash tree -# will contain, so inserting them reproduces the squash exactly (SPEC §5/D1). -create_file "src/one.rs" "fn one() {}" -require_success "add one.rs" atomic add "src/one.rs" -require_success "record one.rs" record_change "feat: one" - -create_file "src/two.rs" "fn two() {}" -require_success "add two.rs" atomic add "src/two.rs" -require_success "record two.rs" record_change "feat: two" - -# Capture the two originals' hashes (newest-first). The draft's own changes are -# the two most recent, regardless of whether inherited history is shown. -require_capture "read draft history" atomic log --view agent -f json -require_capture "extract original change hashes" python3 -c 'import json,sys; rows=json.loads(sys.argv[1]); assert len(rows)>=2 and rows[0]["message"]=="feat: two" and rows[1]["message"]=="feat: one"; print(rows[0]["hash"]); print(rows[1]["hash"])' "$COMMAND_OUTPUT" -ORIG=() -while IFS= read -r h; do [[ -n "$h" ]] && ORIG+=("$h"); done <<< "$COMMAND_OUTPUT" - -HASH_TWO="${ORIG[0]:-}" # newest (feat: two) -HASH_ONE="${ORIG[1]:-}" # older (feat: one) - -if [[ -n "$HASH_ONE" && -n "$HASH_TWO" && "$HASH_ONE" != "$HASH_TWO" ]]; then - _pass "captured two original change hashes" -else - _fail "captured two original change hashes" \ - "one='$HASH_ONE' two='$HASH_TWO' (log parse failed?)" - print_summary - exit 1 -fi - -# ════════════════════════════════════════════════════════════════════════ -# Section 4: Simulate the GitHub squash merge onto the shared branch -# ════════════════════════════════════════════════════════════════════════ - -begin_section "Simulate squash-merge commit with multi-block trailers" - -# Return to the shared view so the working copy is the shared baseline; the -# draft-only files disappear, then we recreate them as the squash tree. -require_success "switch to shared view" switch_view "$MAIN" - -# The squash tree == shared baseline + both originals' content. -create_file "src/one.rs" "fn one() {}" -create_file "src/two.rs" "fn two() {}" -git add src/one.rs src/two.rs - -# GitHub concatenates each squashed commit's body, so the merge commit carries -# ONE Atomic-Changes block per original. A bullet line heads each block so the -# final paragraph is never mistaken for an `atomic git push` self-push trailer. -git commit -q -m "Add feature one and two (#1) - -* feat: one -Atomic-View: agent -Atomic-Changes: $HASH_ONE - -* feat: two -Atomic-View: agent -Atomic-Changes: $HASH_TWO" - -assert_output_contains "squash commit carries both trailers (block 1)" \ - "$HASH_ONE" git log -1 --format=%B -assert_output_contains "squash commit carries both trailers (block 2)" \ - "$HASH_TWO" git log -1 --format=%B - -# ════════════════════════════════════════════════════════════════════════ -# Section 5: Dry-run router (SPEC §4.4) — advisory classification -# ════════════════════════════════════════════════════════════════════════ - -begin_section "Dry-run classifies the incoming squash" - -# Always true today: the forecast reports the one new commit. -assert_output_contains "dry-run forecasts one new commit" \ - "1 commit" atomic git import --incremental --branch "$MAIN" --dry-run - -# TARGET (SPEC §4.4): the forecast should recognise atomic headers and route -# the user to the insert path rather than a plain count. -assert_target "dry-run flags an insertable atomic squash" dry_run_recommends_insert - -# ════════════════════════════════════════════════════════════════════════ -# Section 6: Incremental import of the squash -# ════════════════════════════════════════════════════════════════════════ - -begin_section "Incremental import: squash → shared view" - -require_capture "count shared history before import" atomic log --view "$MAIN" -f json -MAIN_BEFORE="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))')" - -snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-review-gate-snapshot-XXXXXX")" -_HARNESS_TMPDIRS+=("$snapshot_dir") -cp src/one.rs "$snapshot_dir/one.rs" -cp src/two.rs "$snapshot_dir/two.rs" -require_capture "read shared history before squash import" atomic log --view "$MAIN" -f json -history_before="$COMMAND_OUTPUT" -require_capture "read draft history before squash import" atomic log --view agent -f json -draft_before="$COMMAND_OUTPUT" -require_capture "read Git HEAD before squash import" git rev-parse HEAD -head_before="$COMMAND_OUTPUT" -if import_out="$(atomic git import --incremental --branch "$MAIN" --no-vault 2>&1)"; then - _pass "incremental import of squash" -else - _fail "incremental import of squash" "$import_out" - assert_success "refused import preserves one.rs bytes" cmp -s "$snapshot_dir/one.rs" src/one.rs - assert_success "refused import preserves two.rs bytes" cmp -s "$snapshot_dir/two.rs" src/two.rs - assert_current_view "refused import preserves shared view" "$MAIN" - require_capture "read shared history after refusal" atomic log --view "$MAIN" -f json - if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then _pass "refusal preserves shared history"; else _fail "refusal preserves shared history" "history changed"; fi - require_capture "read draft history after refusal" atomic log --view agent -f json - if [[ "$COMMAND_OUTPUT" == "$draft_before" ]]; then _pass "refusal preserves draft history"; else _fail "refusal preserves draft history" "history changed"; fi - require_capture "read Git HEAD after refusal" git rev-parse HEAD - if [[ "$COMMAND_OUTPUT" == "$head_before" ]]; then _pass "refusal preserves Git HEAD"; else _fail "refusal preserves Git HEAD" "HEAD changed"; fi - _skip "ReviewGate metadata, insertion and draft-deletion checks" "squash import refused" - print_summary || true - exit 1 -fi - -require_success "switch to shared view after import" switch_view "$MAIN" - -# Always-true guarantees (both current and target behavior satisfy these): -assert_file_content "shared view materializes one.rs" "src/one.rs" "fn one() {}" -assert_file_content "shared view materializes two.rs" "src/two.rs" "fn two() {}" - -TAG_NAME="pr-1" -assert_output_contains "ReviewGate tag created for the squash" \ - "$TAG_NAME" atomic tag list - -# Prerequisite fix (BUG-review-gate-original-hashes.md), validated end-to-end: -# the ReviewGate metadata must list EVERY original hash, not just the first. -assert_success "ReviewGate records first original hash" tag_show_contains "$HASH_ONE" -assert_success "ReviewGate records second original hash" tag_show_contains "$HASH_TWO" - -# ════════════════════════════════════════════════════════════════════════ -# Section 7: TARGET behavior — originals inserted, squash is a tag -# ════════════════════════════════════════════════════════════════════════ - -begin_section "Target: originals live in the shared view (SPEC §2, §4)" - -# The shared view must reference the ORIGINAL records, not a new squash record. -assert_target "shared view contains original one.rs record" main_has_record "$HASH_ONE" -assert_target "shared view contains original two.rs record" main_has_record "$HASH_TWO" - -# The squash must NOT add a brand-new change record: with insert, the shared -# view grows by exactly the two originals (not by a single squash record). -require_capture "count shared history after import" atomic log --view "$MAIN" -f json -MAIN_AFTER="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))')" -assert_target "shared view grew by exactly the two originals" \ - test "$MAIN_AFTER" -eq "$((MAIN_BEFORE + 2))" - -# The ReviewGate is an aggregate over the inserted records. `tag show` renders -# the metadata (not raw JSON), so assert against the rendered provenance block. -assert_target "ReviewGate marks the aggregate as inserted" tag_show_contains "inserted" -assert_target "ReviewGate shows the aggregate range" tag_show_contains "Aggregate" -assert_target "ReviewGate records the git squash provenance" tag_show_contains "squash" - -# ════════════════════════════════════════════════════════════════════════ -# Section 8: TARGET behavior — deleting the draft is safe -# ════════════════════════════════════════════════════════════════════════ - -begin_section "Target: draft is disposable, provenance survives (SPEC §1)" - -# Drafts must be deletable without losing the granular history: the shared view -# now references the originals, so they stay alive and materializable. -if main_has_record "$HASH_ONE" && main_has_record "$HASH_TWO"; then - require_success "delete draft after originals are present" atomic view delete agent --force - assert_target "originals still in shared view after draft deletion" main_has_record "$HASH_ONE" - assert_target "shared view still materializes after draft deletion" shared_view_materializes_two -else - _skip "draft deletion and provenance survival" "originals not both present in shared view" -fi - -# ── Summary ──────────────────────────────────────────────────────────────── - +source "$HARNESS_DIR/native_helpers.sh" +begin_section "Native signed review, preview and original-change promotion" +make_temp_repo native-review-insert +run_native_checks native_review_insert_test.py print_summary diff --git a/tests/harness/40_switch_transactionality.sh b/tests/harness/40_switch_transactionality.sh index 8bda359e..4323e47b 100755 --- a/tests/harness/40_switch_transactionality.sh +++ b/tests/harness/40_switch_transactionality.sh @@ -1,105 +1,10 @@ #!/usr/bin/env bash -# 40_switch_transactionality.sh — Recover failed view switches through disk materialization. - +# Native switch errors recover synchronously through leased operations/receipts. +# These injected returned errors are not process-kill or crash-window coverage. HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" -source "$HARNESS_DIR/merge_helpers.sh" - -begin_section "Failed materialization rolls back the published view" +source "$HARNESS_DIR/native_helpers.sh" +begin_section "Native switch failures: journaled rollback, shelves and retry" make_temp_repo switch-transaction -init_repo - -printf 'cache/\n' > .atomicignore -printf 'source view\n' > source.txt -require_success "add source and ignore rules" atomic add .atomicignore source.txt -require_success "record source and ignore rules" record_change "add source file and ignore rules" -mkdir -p cache -printf 'source cache\n' > cache/state.txt -require_capture "read source view" current_view -SOURCE_VIEW="$COMMAND_OUTPUT" - -require_success "create and switch to target" new_view target --draft --parent "$SOURCE_VIEW" --switch -mkdir -p nested -printf 'target view\n' > nested/target.txt -require_success "add target file" atomic add nested/target.txt -require_success "record target file" record_change "add target file" - -require_success "switch to source" switch_view "$SOURCE_VIEW" -assert_file_not_exists "target-only file is absent on source" nested/target.txt -assert_file_content "source file is intact before failed switch" source.txt "source view" -assert_file_content "source ignored workspace is present before failure" cache/state.txt "source cache" - -if fault_output="$(env ATOMIC_TEST_FAIL_SWITCH_AFTER_PUBLISH=1 "$ATOMIC_BIN" view switch target --force 2>&1)"; then - _fail "switch fails after publishing but before materialization" "failpoint unexpectedly succeeded: $fault_output" - _skip "publication rollback and later recovery checks" "failpoint did not fire" - print_summary || true - exit 1 -else - if [[ "$fault_output" == *"injected switch failure after view publication"* ]]; then - _pass "switch fails after publishing but before materialization" - else - _fail "switch fails after publishing but before materialization" "wrong failure: $fault_output" - _skip "publication rollback and later recovery checks" "requested failpoint did not fire" - print_summary || true - exit 1 - fi -fi - -# The failed command exits with a durable recovery marker. The next command -# opens the repository writable and must roll the whole transition back. -assert_file_exists "failed switch leaves durable recovery marker" \ - .atomic/deferred-tree-alignment.pending -require_success "next writable open performs switch recovery" atomic add source.txt -assert_equal_value() { - local desc="$1" expected="$2" actual="$3" - if [[ "$expected" == "$actual" ]]; then - _pass "$desc" - else - _fail "$desc" "expected '$expected', got '$actual'" - fi -} -assert_current_view "reopen restores source as current view" "$SOURCE_VIEW" -assert_file_not_exists "successful recovery clears pending marker" \ - .atomic/deferred-tree-alignment.pending -assert_file_content "source tracked content survives rollback" source.txt "source view" -assert_file_content "source ignored workspace is restored by rollback" cache/state.txt "source cache" -require_success "switch succeeds after obstruction is removed" \ - atomic view switch target --force -assert_current_view "target becomes current after successful switch" target -assert_file_content "target file materializes after successful retry" nested/target.txt "target view" -assert_file_content "inherited source file remains present" source.txt "source view" -assert_file_not_exists "successful switch leaves no recovery marker" \ - .atomic/deferred-tree-alignment.pending - -begin_section "Recovery removes a partially materialized target view" -require_success "switch back to source" switch_view "$SOURCE_VIEW" -assert_file_not_exists "target-only file is removed before mixed-state test" nested/target.txt - -if fault_output="$(env ATOMIC_TEST_FAIL_SWITCH_AFTER_MATERIALIZE=1 "$ATOMIC_BIN" view switch target --force 2>&1)"; then - _fail "switch fails after target files are materialized" "failpoint unexpectedly succeeded: $fault_output" - _skip "partial-materialization recovery checks" "failpoint did not fire" - print_summary || true - exit 1 -else - if [[ "$fault_output" == *"injected switch failure after materialization"* ]]; then - _pass "switch fails after target files are materialized" - else - _fail "switch fails after target files are materialized" "wrong failure: $fault_output" - _skip "partial-materialization recovery checks" "requested failpoint did not fire" - print_summary || true - exit 1 - fi -fi -assert_file_exists "partially materialized target file exists before recovery" nested/target.txt -assert_file_exists "post-materialization failure retains recovery marker" \ - .atomic/deferred-tree-alignment.pending - -require_success "writable reopen recovers mixed working copy" atomic add source.txt -assert_current_view "mixed-state recovery restores source current view" "$SOURCE_VIEW" -assert_file_not_exists "mixed-state recovery removes target-only tracked file" nested/target.txt -assert_file_content "mixed-state recovery restores source content" source.txt "source view" -assert_file_content "mixed-state recovery restores ignored workspace" cache/state.txt "source cache" -assert_file_not_exists "mixed-state recovery clears pending marker" \ - .atomic/deferred-tree-alignment.pending - +run_native_checks native_switch_test.py print_summary diff --git a/tests/harness/legacy-shadow/32_review_gate_insert.sh b/tests/harness/legacy-shadow/32_review_gate_insert.sh new file mode 100644 index 00000000..3b947c34 --- /dev/null +++ b/tests/harness/legacy-shadow/32_review_gate_insert.sh @@ -0,0 +1,295 @@ +#!/usr/bin/env bash +# HISTORICAL Git-squash/trailer proposal. Native workflow lives in ../32_review_gate_insert.sh. +# +# Walks the workflow from SPEC-review-gate-insert-originals.md: +# +# 1. git repo + `atomic git import` → shared view (named after git default) +# 2. Agent work in an Atomic draft → per-turn CHANGE RECORDS (the originals) +# 3. Simulated GitHub squash-merge → a single git commit on the shared +# branch whose body carries the originals' `Atomic-Changes` trailers +# (multiple blocks, mimicking GitHub concatenating each squashed body) +# 4. `atomic git import --incremental --branch ` +# +# TARGET (SPEC §2, §4): the squash must NOT become a new change record. Instead +# the importer inserts the named originals into the shared view (they already +# live in the graph) and represents the squash as an aggregate ReviewGate TAG +# (from…to, count, inserted:true). Deleting the draft must then be safe. +# +# STATUS: the insert path is not implemented yet. Target-behavior checks are +# gated on $EXPECT_INSERT: +# - unset/0 → reported as pending (skipped), suite is partial +# - 1 → hard assertions, so this file drives implementation red→green +# +# What already holds today (hard-asserted here, no gate): +# - the import succeeds and the shared view materializes the squash content +# - a ReviewGate tag is created for the squash +# - its metadata lists EVERY original hash across ALL trailer blocks +# (the BUG-review-gate-original-hashes.md fix, validated end-to-end) + +HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" +source "$HARNESS_DIR/helpers.sh" + +# ── Target-behavior gate ────────────────────────────────────────────────── +# +# assert_target +# pass if the command succeeds (target behavior is present) +# fail if it does not AND EXPECT_INSERT=1 (drives implementation) +# pending otherwise (feature not built yet — keeps the suite green) +assert_target() { + local desc="$1"; shift + if "$@" >/dev/null 2>&1; then + _pass "$desc" + elif [[ "${EXPECT_INSERT:-0}" == "1" ]]; then + _fail "$desc" "target behavior (SPEC) not met" + else + _skip "$desc" "pending: insert-originals (SPEC §4) not implemented" + fi +} + +# Predicate helpers (used as commands for assert_* so they run under the gate). +# +# main_has_record matches the change's `hash` FIELD exactly — not a raw substring +# of the JSON. A squash change record embeds the originals' `Atomic-Changes` +# trailers in its commit message, so a substring grep would spuriously match +# even when the record was never inserted. Exact hash-field membership is the +# real "is this original a change in the shared view?" test. +# These predicates capture command output into a variable and then match with a +# here-string, deliberately avoiding `cmd | grep -q`. Under `set -o pipefail` +# (active via helpers.sh), `grep -q` exits on first match and closes the pipe; +# the still-writing `atomic` process then dies with SIGPIPE, and pipefail +# reports the pipeline as failed even though the text matched. Two more notes: +# * `grep -a` forces text mode — `tag show` / the router emit UTF-8 glyphs +# (…, ✓, —) that make macOS/BSD grep treat the stream as binary. +# * `main_has_record` matches the `hash` FIELD exactly (whole line), so a +# squash record's embedded trailer text can't produce a false positive. +main_view_hashes() { + local out + out="$(atomic log --view "$MAIN" -f json 2>&1)" || return 1 + printf '%s\n' "$out" | python3 -c 'import json,sys; rows=json.load(sys.stdin); assert isinstance(rows,list); print("\n".join(r["hash"] for r in rows))' +} +main_has_record() { + local out; out="$(main_view_hashes)" + grep -qxaF "$1" <<<"$out" +} +tag_show_contains() { + local out; out="$(atomic tag show "$TAG_NAME" 2>/dev/null)" + grep -qaF "$1" <<<"$out" +} +# Dry-run router recommendation for the current target (SPEC §4.4). +dry_run_recommends_insert() { + local out; out="$(atomic git import --incremental --branch "$MAIN" --dry-run 2>&1)" + grep -qaiE '(recommend|route|use|will|would|should).*(insert|original)|(insert|original).*(recommend|route|instead)' <<<"$out" +} +# The shared view materializes `src/two.rs` after switching to it. +shared_view_materializes_two() { + atomic view switch "$MAIN" --force >/dev/null 2>&1 && test -f src/two.rs +} + +# ── Suite banner ────────────────────────────────────────────────────────── + +echo "" +echo "${BOLD}══════════════════════════════════════════════════════════════${RESET}" +echo "${BOLD} Suite: 32_review_gate_insert${RESET}" +echo "${BOLD}══════════════════════════════════════════════════════════════${RESET}" + +# ════════════════════════════════════════════════════════════════════════ +# Section 1: Prerequisites +# ════════════════════════════════════════════════════════════════════════ + +begin_section "Prerequisites" +require_git + +# ════════════════════════════════════════════════════════════════════════ +# Section 2: Seed the shared view from git +# ════════════════════════════════════════════════════════════════════════ + +begin_section "Seed shared view from git import" + +make_temp_repo "review-gate-insert" +init_git_repo +git_commit "Initial" "README.md" "# Project" + +require_success "git import seeds the shared view" atomic git import --no-vault + +MAIN="$(git_default_branch)" +assert_current_view "shared view is '$MAIN'" "$MAIN" + +# ════════════════════════════════════════════════════════════════════════ +# Section 3: Agent work in a draft view — the original change records +# ════════════════════════════════════════════════════════════════════════ + +begin_section "Record originals in a draft view" + +require_success "create + switch to draft" \ + atomic view create agent --draft --parent "$MAIN" --switch + +# Two per-turn changes. Their on-disk content must match what the squash tree +# will contain, so inserting them reproduces the squash exactly (SPEC §5/D1). +create_file "src/one.rs" "fn one() {}" +require_success "add one.rs" atomic add "src/one.rs" +require_success "record one.rs" record_change "feat: one" + +create_file "src/two.rs" "fn two() {}" +require_success "add two.rs" atomic add "src/two.rs" +require_success "record two.rs" record_change "feat: two" + +# Capture the two originals' hashes (newest-first). The draft's own changes are +# the two most recent, regardless of whether inherited history is shown. +require_capture "read draft history" atomic log --view agent -f json +require_capture "extract original change hashes" python3 -c 'import json,sys; rows=json.loads(sys.argv[1]); assert len(rows)>=2 and rows[0]["message"]=="feat: two" and rows[1]["message"]=="feat: one"; print(rows[0]["hash"]); print(rows[1]["hash"])' "$COMMAND_OUTPUT" +ORIG=() +while IFS= read -r h; do [[ -n "$h" ]] && ORIG+=("$h"); done <<< "$COMMAND_OUTPUT" + +HASH_TWO="${ORIG[0]:-}" # newest (feat: two) +HASH_ONE="${ORIG[1]:-}" # older (feat: one) + +if [[ -n "$HASH_ONE" && -n "$HASH_TWO" && "$HASH_ONE" != "$HASH_TWO" ]]; then + _pass "captured two original change hashes" +else + _fail "captured two original change hashes" \ + "one='$HASH_ONE' two='$HASH_TWO' (log parse failed?)" + print_summary + exit 1 +fi + +# ════════════════════════════════════════════════════════════════════════ +# Section 4: Simulate the GitHub squash merge onto the shared branch +# ════════════════════════════════════════════════════════════════════════ + +begin_section "Simulate squash-merge commit with multi-block trailers" + +# Return to the shared view so the working copy is the shared baseline; the +# draft-only files disappear, then we recreate them as the squash tree. +require_success "switch to shared view" switch_view "$MAIN" + +# The squash tree == shared baseline + both originals' content. +create_file "src/one.rs" "fn one() {}" +create_file "src/two.rs" "fn two() {}" +git add src/one.rs src/two.rs + +# GitHub concatenates each squashed commit's body, so the merge commit carries +# ONE Atomic-Changes block per original. A bullet line heads each block so the +# final paragraph is never mistaken for an `atomic git push` self-push trailer. +git commit -q -m "Add feature one and two (#1) + +* feat: one +Atomic-View: agent +Atomic-Changes: $HASH_ONE + +* feat: two +Atomic-View: agent +Atomic-Changes: $HASH_TWO" + +assert_output_contains "squash commit carries both trailers (block 1)" \ + "$HASH_ONE" git log -1 --format=%B +assert_output_contains "squash commit carries both trailers (block 2)" \ + "$HASH_TWO" git log -1 --format=%B + +# ════════════════════════════════════════════════════════════════════════ +# Section 5: Dry-run router (SPEC §4.4) — advisory classification +# ════════════════════════════════════════════════════════════════════════ + +begin_section "Dry-run classifies the incoming squash" + +# Always true today: the forecast reports the one new commit. +assert_output_contains "dry-run forecasts one new commit" \ + "1 commit" atomic git import --incremental --branch "$MAIN" --dry-run + +# TARGET (SPEC §4.4): the forecast should recognise atomic headers and route +# the user to the insert path rather than a plain count. +assert_target "dry-run flags an insertable atomic squash" dry_run_recommends_insert + +# ════════════════════════════════════════════════════════════════════════ +# Section 6: Incremental import of the squash +# ════════════════════════════════════════════════════════════════════════ + +begin_section "Incremental import: squash → shared view" + +require_capture "count shared history before import" atomic log --view "$MAIN" -f json +MAIN_BEFORE="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))')" + +snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/atomic-review-gate-snapshot-XXXXXX")" +_HARNESS_TMPDIRS+=("$snapshot_dir") +cp src/one.rs "$snapshot_dir/one.rs" +cp src/two.rs "$snapshot_dir/two.rs" +require_capture "read shared history before squash import" atomic log --view "$MAIN" -f json +history_before="$COMMAND_OUTPUT" +require_capture "read draft history before squash import" atomic log --view agent -f json +draft_before="$COMMAND_OUTPUT" +require_capture "read Git HEAD before squash import" git rev-parse HEAD +head_before="$COMMAND_OUTPUT" +if import_out="$(atomic git import --incremental --branch "$MAIN" --no-vault 2>&1)"; then + _pass "incremental import of squash" +else + _fail "incremental import of squash" "$import_out" + assert_success "refused import preserves one.rs bytes" cmp -s "$snapshot_dir/one.rs" src/one.rs + assert_success "refused import preserves two.rs bytes" cmp -s "$snapshot_dir/two.rs" src/two.rs + assert_current_view "refused import preserves shared view" "$MAIN" + require_capture "read shared history after refusal" atomic log --view "$MAIN" -f json + if [[ "$COMMAND_OUTPUT" == "$history_before" ]]; then _pass "refusal preserves shared history"; else _fail "refusal preserves shared history" "history changed"; fi + require_capture "read draft history after refusal" atomic log --view agent -f json + if [[ "$COMMAND_OUTPUT" == "$draft_before" ]]; then _pass "refusal preserves draft history"; else _fail "refusal preserves draft history" "history changed"; fi + require_capture "read Git HEAD after refusal" git rev-parse HEAD + if [[ "$COMMAND_OUTPUT" == "$head_before" ]]; then _pass "refusal preserves Git HEAD"; else _fail "refusal preserves Git HEAD" "HEAD changed"; fi + _skip "ReviewGate metadata, insertion and draft-deletion checks" "squash import refused" + print_summary || true + exit 1 +fi + +require_success "switch to shared view after import" switch_view "$MAIN" + +# Always-true guarantees (both current and target behavior satisfy these): +assert_file_content "shared view materializes one.rs" "src/one.rs" "fn one() {}" +assert_file_content "shared view materializes two.rs" "src/two.rs" "fn two() {}" + +TAG_NAME="pr-1" +assert_output_contains "ReviewGate tag created for the squash" \ + "$TAG_NAME" atomic tag list + +# Prerequisite fix (BUG-review-gate-original-hashes.md), validated end-to-end: +# the ReviewGate metadata must list EVERY original hash, not just the first. +assert_success "ReviewGate records first original hash" tag_show_contains "$HASH_ONE" +assert_success "ReviewGate records second original hash" tag_show_contains "$HASH_TWO" + +# ════════════════════════════════════════════════════════════════════════ +# Section 7: TARGET behavior — originals inserted, squash is a tag +# ════════════════════════════════════════════════════════════════════════ + +begin_section "Target: originals live in the shared view (SPEC §2, §4)" + +# The shared view must reference the ORIGINAL records, not a new squash record. +assert_target "shared view contains original one.rs record" main_has_record "$HASH_ONE" +assert_target "shared view contains original two.rs record" main_has_record "$HASH_TWO" + +# The squash must NOT add a brand-new change record: with insert, the shared +# view grows by exactly the two originals (not by a single squash record). +require_capture "count shared history after import" atomic log --view "$MAIN" -f json +MAIN_AFTER="$(printf '%s\n' "$COMMAND_OUTPUT" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))')" +assert_target "shared view grew by exactly the two originals" \ + test "$MAIN_AFTER" -eq "$((MAIN_BEFORE + 2))" + +# The ReviewGate is an aggregate over the inserted records. `tag show` renders +# the metadata (not raw JSON), so assert against the rendered provenance block. +assert_target "ReviewGate marks the aggregate as inserted" tag_show_contains "inserted" +assert_target "ReviewGate shows the aggregate range" tag_show_contains "Aggregate" +assert_target "ReviewGate records the git squash provenance" tag_show_contains "squash" + +# ════════════════════════════════════════════════════════════════════════ +# Section 8: TARGET behavior — deleting the draft is safe +# ════════════════════════════════════════════════════════════════════════ + +begin_section "Target: draft is disposable, provenance survives (SPEC §1)" + +# Drafts must be deletable without losing the granular history: the shared view +# now references the originals, so they stay alive and materializable. +if main_has_record "$HASH_ONE" && main_has_record "$HASH_TWO"; then + require_success "delete draft after originals are present" atomic view delete agent --force + assert_target "originals still in shared view after draft deletion" main_has_record "$HASH_ONE" + assert_target "shared view still materializes after draft deletion" shared_view_materializes_two +else + _skip "draft deletion and provenance survival" "originals not both present in shared view" +fi + +# ── Summary ──────────────────────────────────────────────────────────────── + +print_summary diff --git a/tests/harness/native.md b/tests/harness/native.md new file mode 100644 index 00000000..2dc0b084 --- /dev/null +++ b/tests/harness/native.md @@ -0,0 +1,83 @@ +# Native workflow harnesses + +Run the three native workflows without Git, shim enrollment or remotes: + +```sh +ATOMIC_BIN=/absolute/path/to/atomic bash tests/harness/run_all.sh native-workflows +``` + +The selector runs **40 → 32 → 23**. Individual numeric selectors still work. +Python-driven fixtures use a disposable HOME/config/identity store and local +service calls, with remote service overrides removed. Every assertion is +reported to the shell harness; a driver crash or zero assertions cannot pass. + +## 40 — switch transactionality + +The native switch API attempts recovery synchronously when a switch returns +an error. This replaces the old test's expectation that a failed command must +leave `.atomic/deferred-tree-alignment.pending` and partial target files. + +Both returned-error seams (`AFTER_PUBLISH` and `AFTER_MATERIALIZE`) must actually +fire. Before any writable retry the harness inspects the failed switch and its +verified inverse recovery head, parent link, leases and receipts. It checks +exact tracked/untracked bytes and modes, both views' ignored artifacts/shelves, +unchanged histories, clean tracked status, idempotent writable reopen, and a +successful ordinary retry and round trip. These are **returned-error rollback +tests, not process-kill or power-loss coverage**. + +## 32 — signed review-readiness and insertion of originals + +The active test is native-only. It creates two isolated fixture identities, +an attested work intent, two dependent code records, a negative self-review, +and an independently signed completed review. The triage report must change +from blocked to ready before the workflow inserts anything. + +Preview must preserve both histories, working bytes and operation head. Explicit +cross-view insertion must retain the original change hashes, dependency and +immutable object bytes, with no surrogate/squash record. Repeated insertion is +membership-idempotent; the Shared view must render the exact result and retain +both originals and the verifiable review after the Draft is deleted. + +This tests **the review-then-insert workflow**, not a claim that every low-level +insert API enforces triage automatically. The two fixture signing identities +exercise identity independence; this is not an independent review of Atomic's +implementation. The former Git squash/trailer proposal is preserved under +`legacy-shadow/32_review_gate_insert.sh`; it is not run or claimed passing here. +Native tests do not relax the rejection of fabricated Git provenance. + +The fixture uncovered and regression-tests a real service-path bug: +`log --view --all` dropped the explicit view and read the current Draft. +The service now honors the explicit view while retaining the selected log's +inherited-entry option. The local-service regression covers explicit Shared +and seeded Draft histories, with and without inherited entries, plus current +view selection. + +## 23 — hierarchy and Shared-root boundaries + +The historical filename remains for compatibility. The current native creation +contract makes Draft children; promotion makes a Shared **root**, not a Shared +child. There is no skipped assertion for an impossible CLI setup. + +The harness checks multi-level Draft inheritance and isolation, promotion of +explicit baseline membership without changing original identities, isolation +from later changes on the former parent, live inheritance by a child of the +new Shared root, clean materialization and non-mutating missing-parent refusal. +It does **not** claim to exercise a Shared-child hierarchy from the older model. + +## Validation + +The native default-feature debug build passed the focused run with no skips: + +| Suite | Passed | Failed | Skipped | +|---|---:|---:|---:| +| 40 native switch transactionality | 53 | 0 | 0 | +| 32 native review and original insertion | 28 | 0 | 0 | +| 23 native hierarchy | 89 | 0 | 0 | +| 17 cross-view merge (nonregression) | 208 | 0 | 0 | +| 22 switch/conflict markers (nonregression) | 179 | 0 | 0 | +| 28 merge rubric (nonregression) | 207 | 0 | 0 | + +All 15 `libatomic` local-service integration tests, workspace `cargo check` and +workspace Clippy also passed. The native/shim selector selftest passed eight +checks; new Python and shell files passed syntax checks. No full-suite, +power-loss, server or network validation is claimed by this focused run. diff --git a/tests/harness/native_helpers.sh b/tests/harness/native_helpers.sh new file mode 100644 index 00000000..f656d05f --- /dev/null +++ b/tests/harness/native_helpers.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Native-only Python checks report each exercised assertion back to helpers.sh. +run_native_checks() { + local script="$1" evidence private rc=0 line failures_before="$TESTS_FAILED" passed=0 + shift + evidence="$(mktemp "${TMPDIR:-/tmp}/atomic-native-checks-XXXXXX")" + _HARNESS_TMPDIRS+=("$evidence") + private="$(mktemp -d "${TMPDIR:-/tmp}/atomic-native-home-XXXXXX")" + _HARNESS_TMPDIRS+=("$private") + HOME="$private" ATOMIC_HOME="$private/.atomic" ATOMIC_CONFIG_DIR="$private/config" ATOMIC_SERVICE=local PYTHONDONTWRITEBYTECODE=1 \ + python3 "$HARNESS_DIR/$script" "$ATOMIC_BIN" "$@" > "$evidence" 2>&1 || rc=$? + while IFS= read -r line; do + case "$line" in + PASS$'\t'*) _pass "${line#*$'\t'}"; passed=$((passed + 1)) ;; + FAIL$'\t'*) _fail "${line#*$'\t'}" "native workflow assertion failed" ;; + *) printf ' %s\n' "$line" ;; + esac + done < "$evidence" + if [[ "$rc" != 0 && "$TESTS_FAILED" == "$failures_before" ]]; then + _fail "native driver completes" "exit $rc; remaining assertions were not exercised" + elif [[ "$rc" == 0 && "$passed" == 0 ]]; then + _fail "native driver exercises assertions" "driver reported no passes" + fi +} diff --git a/tests/harness/native_review_insert_test.py b/tests/harness/native_review_insert_test.py new file mode 100644 index 00000000..1741e2ff --- /dev/null +++ b/tests/harness/native_review_insert_test.py @@ -0,0 +1,183 @@ +"""Native review-readiness -> insert original records -> retire Draft, without Git.""" +import re +import sys + +from native_support import ROOT, call, check, data, files_under, record, view + + +def history(name): + return data('log', '--view', name, '--all', '--format', 'json') + + +def new_intent(title, target=None): + before = {row['id'] for row in data('intent', 'list', '--json')} + args = ['intent', 'new', title] + if target: + args += ['--review', 'urn:atomic:intent:' + target] + call(*args) + row = next(row for row in data('intent', 'list', '--json') if row['id'] not in before) + for path in (ROOT / '.vault/intents').glob('*/intent.md'): + text = path.read_text() + if re.search(r'^id: ' + re.escape(row['id']) + r'$', text, re.M): + uid = re.search(r'^uid: (.+)$', text, re.M).group(1) + return row['id'], uid + raise AssertionError('created intent has no materialized identity') + + +def work_body(uid, complete): + criterion = 'status=met verifiedBy="native-fixture" evidence="Exact fixture bytes checked before attestation"' if complete else 'status=unmet' + status = 'done' if complete else 'open' + return f''':::why +Preserve the two original native records during promotion. +::: +:::acceptance-criterion{{#{uid}-ac-1 {criterion}}} +The two source files have the expected fixture bytes. +::: +:::task{{#{uid}-1 status={status} criteria={uid}-ac-1}} +Implement the two fixture files. +::file-ref{{path=src/one.rs}} +::file-ref{{path=src/two.rs}} +::: +:::scope-in +src/one.rs and src/two.rs. +::: +:::scope-out +Git imports, remotes and production user data. +::: +:::constraint +Original change hashes and bytes must survive promotion. +::: +''' + + +def review_body(uid, target, verifier): + return f''':::why +Independently check the native fixture's exact bytes and original records. +::: +:::acceptance-criterion{{#{uid}-ac-1 status=met verifiedBy="{verifier}" evidence="Both original records and expected file bytes inspected"}} +The native fixture meets its acceptance criterion. +::: +:::task{{#{uid}-1 status=done criteria={uid}-ac-1}} +Review the work intent and its fixture records. +::: +:::ref{{to=urn:atomic:intent:{target} edge=reviews}} +::: +:::scope-in +The native work intent and its two original code records. +::: +:::scope-out +Git squash import or surrogate change synthesis. +::: +:::constraint +The review signer must differ from the work signer. +::: +''' + + +def complete_intent(identity, body, signer): + call('intent', 'update', identity, '--body', body, '--force') + call('intent', 'attest', identity, '--identity', signer) + call('intent', 'update', identity, '--status', 'done') + call('intent', 'attest', identity, '--identity', signer) + call('intent', 'validate', identity) + call('intent', 'verify', identity, '--identity', signer) + + +def report(): + return data('triage', 'review', 'agent', '--into', 'dev', '--json') + + +def main(): + call('identity', 'new', 'native-author', '--set-default') + call('identity', 'new', 'native-reviewer') + call('init') + check('native review fixture has no Git repository', not (ROOT / '.git').exists()) + work_id, work_uid = new_intent('Native originals fixture') + call('intent', 'update', work_id, '--body', work_body(work_uid, False), '--force') + call('intent', 'attest', work_id, '--identity', 'native-author') + call('add', '.vault') + record('record work explanation on shared baseline') + call('view', 'create', 'agent', '--draft', '--parent', 'dev', '--switch') + (ROOT / 'src').mkdir() + (ROOT / 'src/one.rs').write_bytes(b'fn one() {}\n') + call('add', 'src/one.rs') + record('original one') + first = history('agent')[0]['hash'] + (ROOT / 'src/one.rs').write_bytes(b'fn one() { two(); }\n') + (ROOT / 'src/two.rs').write_bytes(b'fn two() {}\n') + call('add', 'src/two.rs') + record('original two depends on one') + second = history('agent')[0]['hash'] + check('two distinct original code identities recorded', first != second) + original = data('change', second, '--format', 'json') + check('second original retains dependency on first', first in original['dependencies']) + before_review = report() + check('unmet and unreviewed work is blocked in triage', before_review['verdict'] == 'blocked') + check('triage names both original records', {first, second} <= set(before_review['inputs']['candidate_changes'])) + + check('work bytes satisfy the fixture before completion', + (ROOT / 'src/one.rs').read_bytes() == b'fn one() { two(); }\n' and + (ROOT / 'src/two.rs').read_bytes() == b'fn two() {}\n') + complete_intent(work_id, work_body(work_uid, True), 'native-author') + self_id, self_uid = new_intent('Self-review negative fixture', work_uid) + complete_intent(self_id, review_body(self_uid, work_uid, 'native-author'), 'native-author') + self_report = report() + check('self-authored review does not satisfy independent-review readiness', any( + finding['code'] == 'UNREVIEWED_CHANGE' and finding['severity'] == 'block' + for finding in self_report['findings'])) + call('identity', 'default', 'native-reviewer') + independent_id, independent_uid = new_intent('Independent fixture review', work_uid) + complete_intent(independent_id, review_body(independent_uid, work_uid, 'native-reviewer'), 'native-reviewer') + call('add', '.vault') + record('retain signed work and review evidence') + ready = report() + check('independently reviewed work is ready for native insertion', ready['verdict'] == 'ready') + check('independent review clears the review finding', not any(f['code'] == 'UNREVIEWED_CHANGE' for f in ready['findings'])) + check('review report retains both original code identities', {first, second} <= set(ready['inputs']['candidate_changes'])) + source_history = history('agent') + target_before_preview = history('dev') + source_hashes = {row['hash'] for row in source_history} + # Pin both histories immediately before preview; code originals must + # still be absent from the target until explicit insertion. + baseline_hashes = {row['hash'] for row in target_before_preview} + check('code originals remain Draft-only before insertion', not ({first, second} & baseline_hashes)) + candidate_hashes = source_hashes - baseline_hashes + object_bytes = {str(path): path.read_bytes() for path in (ROOT / '.atomic/changes').rglob('*.change')} + before_files = files_under(ROOT) + before_ops = data('op', 'log', '--json') + preview = call('insert', 'from-view', 'agent', '--to-view', 'dev', '--dry-run') + check('native preview names the two originals', all(h[:8] in preview.stdout.decode() for h in (first, second))) + check('preview preserves target history', history('dev') == target_before_preview) + check('preview preserves source history', history('agent') == source_history) + check('preview preserves working bytes', files_under(ROOT) == before_files) + check('preview appends no working-copy operation', data('op', 'log', '--json') == before_ops) + call('insert', 'from-view', 'agent', '--to-view', 'dev') + check('promotion leaves current Draft selected', view() == 'agent') + check('promotion preserves source history', history('agent') == source_history) + promoted = history('dev') + check('target references exactly the originals and evidence, no surrogate change', {r['hash'] for r in promoted} == baseline_hashes | candidate_hashes) + check('both original code records now belong to Shared view', {first, second} <= {r['hash'] for r in promoted}) + check('promotion preserves all immutable change bytes', {str(p): p.read_bytes() for p in (ROOT / '.atomic/changes').rglob('*.change')} == object_bytes) + call('insert', 'from-view', 'agent', '--to-view', 'dev') + check('repeat insertion is membership-idempotent', history('dev') == promoted) + call('view', 'switch', 'dev', '--force') + check('Shared view materializes exact original result', + (ROOT / 'src/one.rs').read_bytes() == b'fn one() { two(); }\n' and + (ROOT / 'src/two.rs').read_bytes() == b'fn two() {}\n') + call('view', 'delete', 'agent', '--force') + check('landed Draft can be retired', not any(row['name'] == 'agent' for row in data('view', 'list', '--all', '--json')['views'])) + check('retiring Draft preserves Shared history', history('dev') == promoted) + check('original change remains inspectable after Draft retirement', data('change', second, '--format', 'json')['hash'] == second) + check('Shared working copy remains clean after Draft retirement', not call('status', '--short', '--no-untracked').stdout.strip()) + call('intent', 'verify', independent_id, '--identity', 'native-reviewer') + check('signed independent review survives Draft retirement', True) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + import traceback + traceback.print_exc() + print('FAIL\t' + str(error), flush=True) + sys.exit(1) diff --git a/tests/harness/native_support.py b/tests/harness/native_support.py new file mode 100644 index 00000000..b6159d68 --- /dev/null +++ b/tests/harness/native_support.py @@ -0,0 +1,56 @@ +"""Shared native-only CLI fixture helpers (no Git or remote operations).""" +import hashlib +import json +import os +from pathlib import Path +import stat +import subprocess +import sys + +BINARY = os.path.abspath(sys.argv[1]) +ROOT = Path.cwd() + + +def call(*args, env=None, success=True): + process_env = dict(os.environ, **(env or {})) + process_env.pop('ATOMIC_RPC', None) + process_env.pop('ATOMIC_DAEMON_SOCKET', None) + result = subprocess.run([BINARY, *args], env=process_env, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=60) + if success and result.returncode: + raise AssertionError((args, result.returncode, result.stdout.decode(), result.stderr.decode())) + return result + + +def data(*args): + return json.loads(call(*args).stdout) + + +def check(label, condition): + if not condition: + raise AssertionError(label) + print('PASS\t' + label, flush=True) + + +def record(message): + call('record', '-am', message) + + +def view(): + return data('view', 'list', '--all', '--json')['current_view'] + + +def files_under(path): + """Compare file artifacts byte-for-byte, including modes and link targets.""" + result = {} + if path.exists(): + for directory, dirs, files in os.walk(path, followlinks=False): + dirs[:] = [d for d in dirs if d not in ('.atomic', '.git')] + for name in files + [d for d in dirs if Path(directory, d).is_symlink()]: + p = Path(directory, name) + mode = p.lstat().st_mode + result[str(p.relative_to(path))] = ( + stat.S_IFMT(mode), stat.S_IMODE(mode), + os.readlink(p) if p.is_symlink() else hashlib.sha256(p.read_bytes()).hexdigest()) + dirs[:] = [d for d in dirs if not Path(directory, d).is_symlink()] + return result diff --git a/tests/harness/native_switch_test.py b/tests/harness/native_switch_test.py new file mode 100644 index 00000000..546e0600 --- /dev/null +++ b/tests/harness/native_switch_test.py @@ -0,0 +1,92 @@ +"""Native switch failure contract: journaled synchronous rollback, not legacy markers.""" +import sys + +from native_support import ROOT, call, check, data, files_under, record, view + + +def main(): + call('init', '--no-vault') + check('fixture is native-only (no Git repository)', not (ROOT / '.git').exists()) + (ROOT / '.atomicignore').write_bytes(b'cache/\n') + (ROOT / 'source.txt').write_bytes(b'source view\n') + (ROOT / 'source.txt').chmod(0o755) + call('add', '.atomicignore', 'source.txt') + record('source') + source = view() + (ROOT / 'cache').mkdir() + (ROOT / 'cache/state.txt').write_bytes(b'source cache\n') + call('view', 'create', 'target', '--draft', '--parent', source, '--switch') + (ROOT / 'nested').mkdir(exist_ok=True) + (ROOT / 'nested/target.txt').write_bytes(b'target view\n') + call('add', 'nested/target.txt') + record('target') + (ROOT / 'cache').mkdir(exist_ok=True) + (ROOT / 'cache/state.txt').write_bytes(b'target cache\n') + call('view', 'switch', source, '--force') + (ROOT / 'untracked.txt').write_bytes(b'undecided user work\n') + working_copy = (ROOT / '.atomic/working_copy_id').read_text().strip() + shelves = ROOT / '.atomic/working-copies' / working_copy / 'workspaces' + check('source ignored artifact is materialized', (ROOT / 'cache/state.txt').read_bytes() == b'source cache\n') + check('target ignored artifact is shelved', any('target' in p and row for p, row in files_under(shelves).items())) + + for phase, diagnostic in [ + ('AFTER_PUBLISH', 'injected switch failure after view publication'), + ('AFTER_MATERIALIZE', 'injected switch failure after materialization'), + ]: + baseline_files = files_under(ROOT) + baseline_shelves = files_under(shelves) + histories = {name: data('log', '--view', name, '--all', '--format', 'json') for name in (source, 'target')} + old_log = data('op', 'log', '--json') + old_ids = {entry['id'] for entry in old_log['entries']} + result = call('view', 'switch', 'target', '--force', + env={'ATOMIC_TEST_FAIL_SWITCH_' + phase: '1'}, success=False) + check(phase + ': requested failure seam actually fires', result.returncode != 0 and diagnostic in result.stderr.decode() + result.stdout.decode()) + + # Inspect BEFORE any writable retry: switch_view_with_operation performs + # its inverse recovery synchronously on a returned error. + log = data('op', 'log', '--json') + added = [entry for entry in log['entries'] if entry['id'] not in old_ids] + failed = [entry for entry in added if entry['kind'] == 'switch_view'] + check(phase + ': failed switch is durably journaled once', len(failed) == 1) + heads = [entry for entry in log['entries'] if entry['is_head']] + check(phase + ': inverse recovery is the verified head before retry', + len(heads) == 1 and heads[0]['kind'] == 'recover' and heads[0]['verification'] == 'verified') + recovery = data('op', 'show', heads[0]['id'], '--json') + check(phase + ': inverse recovery links to failed switch', failed[0]['id'] in recovery['parents']) + check(phase + ': recovery has a verified receipt', any(r['kind'] == 'verified' for r in recovery['receipts'])) + original = data('op', 'show', failed[0]['id'], '--json') + check(phase + ': original plan leases the working copy', any(e['target']['kind'] == 'working_copy' for e in original['delta']['effects'])) + check(phase + ': original plan leases target materialization', any(e['target'].get('path') == 'nested/target.txt' for e in original['delta']['effects'])) + check(phase + ': ignored artifacts have shelf leases', any(e['target']['kind'] == 'shelf_path' for e in original['delta']['effects'])) + check(phase + ': files, links and modes already restored', files_under(ROOT) == baseline_files) + check(phase + ': both views shelves restored', files_under(shelves) == baseline_shelves) + check(phase + ': source view restored', view() == source) + check(phase + ': target-only content absent after rollback', not (ROOT / 'nested/target.txt').exists()) + check(phase + ': tracked status is clean after rollback', not call('status', '--short', '--no-untracked').stdout.strip()) + for name in (source, 'target'): + check(phase + ': immutable history retained for ' + name, + data('log', '--view', name, '--all', '--format', 'json') == histories[name]) + + call('add', 'source.txt') + check(phase + ': writable reopen is recovery-idempotent', data('op', 'log', '--json') == log) + check(phase + ': writable reopen preserves user artifacts', files_under(ROOT) == baseline_files) + call('view', 'switch', 'target', '--force') + check(phase + ': ordinary retry selects target', view() == 'target') + check(phase + ': target content is byte-exact', (ROOT / 'nested/target.txt').read_bytes() == b'target view\n') + check(phase + ': target ignored artifact restored', (ROOT / 'cache/state.txt').read_bytes() == b'target cache\n') + check(phase + ': untracked bytes survive retry', (ROOT / 'untracked.txt').read_bytes() == b'undecided user work\n') + check(phase + ': inherited executable survives retry', (ROOT / 'source.txt').stat().st_mode & 0o111 != 0) + check(phase + ': tracked status is clean after retry', not call('status', '--short', '--no-untracked').stdout.strip()) + check(phase + ': successful retry has a verified switch head', any(e['is_head'] and e['kind'] == 'switch_view' and e['verification'] == 'verified' for e in data('op', 'log', '--json')['entries'])) + call('view', 'switch', source, '--force') + check(phase + ': round-trip restores source artifacts', files_under(ROOT) == baseline_files) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + import traceback + traceback.print_exc() + print('FAIL\t' + str(error), flush=True) + sys.exit(1) diff --git a/tests/harness/run_all.sh b/tests/harness/run_all.sh index 8db770ed..e284bd28 100755 --- a/tests/harness/run_all.sh +++ b/tests/harness/run_all.sh @@ -104,6 +104,7 @@ if [[ $# -gt 0 ]]; then # current contract, never the archived historical expectations. alias_targets=() case "${arg%.sh}" in + native-workflows) alias_targets=(40_switch_transactionality 32_review_gate_insert 23_shared_view_parent_chain) ;; shim) alias_targets=(35_git_shim_serialization 39_git_shim_workflow 40_git_shim_safety 41_git_shim_hooks 42_git_shim_guard 43_git_shim_operation_recovery) ;; 19|19_git_shadow) alias_targets=(39_git_shim_workflow 41_git_shim_hooks) ;; 33|33_shadow_push_conflict_markers|34|34_shadow_provenance_guard|36|36_shadow_coherence_v2|38|38_shadow_drift_v3|40_git_shim_materialize_safety) alias_targets=(40_git_shim_safety) ;; diff --git a/tests/harness/shim.md b/tests/harness/shim.md index 282adc84..869e949b 100644 --- a/tests/harness/shim.md +++ b/tests/harness/shim.md @@ -73,7 +73,10 @@ Select canonical names if you do not want every suite sharing a numeric prefix. The original scripts are preserved verbatim apart from their archival headers under `legacy-shadow/`; they are reference material, not executable active contracts. Initial/imported Git history fidelity stays in suites 10/13; squash -classification and review evidence stay in 32 (known failures are not removed). +classification and review-evidence scenarios from the old 32 are retained in +`legacy-shadow/32_review_gate_insert.sh` as the historical Git-squash proposal; +its bare trailers are not accepted proof. Active suite 32 is now the native +signed-review and original-record insertion workflow described in `native.md`. The old 19 dirty-background-import fixture manually mixed divergent Git index and draft bytes, and its recovery fixture edited `.atomic/current_view` plus a legacy pending marker. Those are not valid setup for today's working-copy diff --git a/tests/harness/shim_harness_selftest.sh b/tests/harness/shim_harness_selftest.sh index 53598810..2da1d460 100755 --- a/tests/harness/shim_harness_selftest.sh +++ b/tests/harness/shim_harness_selftest.sh @@ -12,6 +12,7 @@ expected = ['35_git_shim_serialization.sh','39_git_shim_workflow.sh', '40_git_shim_safety.sh','41_git_shim_hooks.sh','42_git_shim_guard.sh', '43_git_shim_operation_recovery.sh'] assert selected('shim') == expected +assert selected('native-workflows') == ['40_switch_transactionality.sh','32_review_gate_insert.sh','23_shared_view_parent_chain.sh'] assert selected('33','34','36','38','40_git_shim_materialize_safety') == ['40_git_shim_safety.sh'] assert selected('37','39_git_shim_mvp','39_git_shim_workflow') == ['39_git_shim_workflow.sh'] assert selected('35_shadow_concurrent_push') == ['35_git_shim_serialization.sh'] @@ -20,5 +21,5 @@ all_suites = selected() assert len(all_suites) == len(set(all_suites)) assert not any('shadow' in name or name in ('39_git_shim_mvp.sh','40_git_shim_materialize_safety.sh') for name in all_suites) assert set(expected) <= set(all_suites) -print('7 shim harness selector/discovery checks passed') +print('8 native/shim harness selector/discovery checks passed') PY From a2a23cef37d544a1d4a47575f22e4aae1e34b633 Mon Sep 17 00:00:00 2001 From: Lee Faus Date: Thu, 5 Nov 2026 19:52:14 -0500 Subject: [PATCH 05/14] shim fix for spans and doctor bug --- atomic-cli/src/commands/workspace_txn.rs | 15 +- .../tests/it/git_shim_workspace_test.rs | 182 ++++++++++++++++ atomic-core/src/pristine/traits/view.rs | 9 +- .../src/repository/deferred_tree.rs | 5 +- .../src/repository/name_resolution.rs | 100 +++++++++ atomic-repository/src/repository/repair.rs | 23 +- atomic-repository/src/repository/snapshot.rs | 1 + atomic-repository/src/repository/status.rs | 5 +- .../tests/conflict_surface_tests.rs | 8 +- .../tests/native_index_repair_tests.rs | 206 ++++++++++++++++++ atomic-repository/src/repository/views.rs | 9 +- .../src/repository/workspace_txn.rs | 41 ++++ libatomic/src/daemon/state.rs | 23 +- tests/harness/42_git_shim_guard.sh | 50 ++--- .../harness/43_git_shim_operation_recovery.sh | 53 ++--- tests/harness/shim.md | 59 ++++- 16 files changed, 707 insertions(+), 82 deletions(-) diff --git a/atomic-cli/src/commands/workspace_txn.rs b/atomic-cli/src/commands/workspace_txn.rs index 97480a59..7eb88f55 100644 --- a/atomic-cli/src/commands/workspace_txn.rs +++ b/atomic-cli/src/commands/workspace_txn.rs @@ -85,7 +85,7 @@ pub(crate) fn enter_remediation_workspace_budgeted( pub(crate) fn remediation_error(remediation: WorkspaceRemediation) -> CliError { CliError::StaleBaseline { report: format!( - "workspace reconciliation required: {}", + "workspace reconciliation required: {}\nInspect with 'atomic status --no-reconcile'; then run 'atomic git shim reconcile' before retrying.", remediation.describe() ), } @@ -105,6 +105,19 @@ pub(crate) fn prepare_service_query(allow_git_operation: bool) -> CliResult still holds its own immutable + // capture. That inherited visibility change must update its conflict cache + // even though the private view was never materialized. + f.atomic(&["doctor", "check"]); + let repo = atomic_repository::Repository::open(f.root.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + let snapshot_name = atomic_repository::Repository::snapshot_view_name(wc); + let txn = repo.pristine().read_txn().unwrap(); + let snapshot_view = txn.get_view(&snapshot_name).unwrap().unwrap(); + let members: Vec<_> = txn + .iter_changes(&snapshot_view, 0) + .unwrap() + .collect::>() + .unwrap(); + let conflicts = txn.iter_conflicts(snapshot_view.id).unwrap(); + assert!( + !conflicts.is_empty(), + "private snapshot's genuine name conflicts must be cached, not ignored" + ); + assert!(conflicts + .iter() + .flat_map(|(_, records)| records) + .any(|conflict| conflict.path == "shared.txt")); + let bytes: Vec<_> = members + .iter() + .map(|(_, id, _)| { + let hash = txn.get_external(*id).unwrap().unwrap(); + ( + hash, + fs::read(repo.change_store().change_path(&hash)).unwrap(), + ) + }) + .collect(); + drop(txn); + // Prove doctor still detects deliberate missing private-view rows, and the + // explicit repair restores them without modifying the snapshot itself. + let mut txn = repo.pristine().write_txn().unwrap(); + txn.del_conflicts(snapshot_view.id, conflicts[0].0).unwrap(); + txn.commit().unwrap(); + drop(repo); + let output = f.command(env!("CARGO_BIN_EXE_atomic"), &["doctor", "check"]); + assert!(!output.status.success()); + let text = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!(text.contains("CONFLICTS missing row"), "{text}"); + f.atomic(&["doctor", "repair-native-indexes"]); + f.atomic(&["doctor", "check"]); + let repo = atomic_repository::Repository::open(f.root.path()).unwrap(); + let txn = repo.pristine().read_txn().unwrap(); + let current = txn.get_view(&snapshot_name).unwrap().unwrap(); + assert_eq!(current.state, snapshot_view.state); + assert_eq!( + txn.iter_changes(¤t, 0) + .unwrap() + .collect::, _>>() + .unwrap(), + members + ); + drop(txn); + for (hash, expected) in &bytes { + assert_eq!( + fs::read(repo.change_store().change_path(hash)).unwrap(), + *expected + ); + } + drop(repo); + + f.git(&["switch", "-c", "feature"]); + fs::remove_file(f.root.path().join("target-only.txt")).unwrap(); + fs::create_dir_all(f.root.path().join("source-only")).unwrap(); + fs::write(f.root.path().join("source-only/first.txt"), b"first\n").unwrap(); + fs::write(f.root.path().join("source-only/second.txt"), b"second\n").unwrap(); + f.git(&["add", "-A"]); + f.git(&["commit", "-qm", "foreign deletion and additions"]); + f.atomic(&["git", "shim", "reconcile"]); + f.atomic(&["git", "shim", "verify"]); + f.atomic(&["doctor", "check"]); + assert_eq!( + fs::read(f.root.path().join("shared.txt")).unwrap(), + b"shared\n" + ); + assert!(!f.root.path().join("target-only.txt").exists()); + for (hash, expected) in bytes { + assert_eq!( + fs::read( + atomic_repository::Repository::open(f.root.path()) + .unwrap() + .change_store() + .change_path(&hash) + ) + .unwrap(), + expected + ); + } +} + +#[test] +fn unbound_git_drift_refuses_before_writable_database_open() { + let f = Fixture::new(false); + f.atomic(&["view", "create", "feature", "--draft", "--parent", "main"]); + f.git(&["switch", "-c", "raw-drift"]); + fs::write(f.root.path().join("tracked.txt"), b"foreign Git state\n").unwrap(); + f.git(&["add", "tracked.txt"]); + f.git(&["commit", "-qm", "unbound foreign commit"]); + fs::write( + f.root.path().join("candidate.txt"), + b"untracked user data\n", + ) + .unwrap(); + let head = f.git(&["rev-parse", "HEAD"]); + let refs = f.git(&["show-ref"]); + let index = fs::read(f.root.path().join(".git/index")).unwrap(); + let checkpoint = fs::read(f.root.path().join(".atomic/bridge/workspace.json")).unwrap(); + for args in [ + vec!["status", "--short"], + vec!["diff", "--name-only"], + vec!["add", "candidate.txt"], + vec!["record", "--all", "-m", "must refuse"], + vec!["view", "switch", "feature", "--force"], + vec!["restore", "tracked.txt"], + ] { + let database = fs::read(f.root.path().join(".atomic/atomic.redb")).unwrap(); + let output = f.command(env!("CARGO_BIN_EXE_atomic"), &args); + let diagnostic = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!(!output.status.success(), "{args:?}: {diagnostic}"); + assert!( + diagnostic.contains("reconciliation required"), + "{args:?}: {diagnostic}" + ); + assert!( + diagnostic.contains("atomic git shim reconcile"), + "{args:?}: {diagnostic}" + ); + assert_eq!( + fs::read(f.root.path().join(".atomic/atomic.redb")).unwrap(), + database, + "raw database changed: {args:?}" + ); + assert_eq!( + fs::read(f.root.path().join(".git/index")).unwrap(), + index, + "{args:?}" + ); + assert_eq!( + fs::read(f.root.path().join(".atomic/bridge/workspace.json")).unwrap(), + checkpoint, + "{args:?}" + ); + assert_eq!( + fs::read(f.root.path().join("tracked.txt")).unwrap(), + b"foreign Git state\n", + "{args:?}" + ); + assert_eq!( + fs::read(f.root.path().join("candidate.txt")).unwrap(), + b"untracked user data\n", + "{args:?}" + ); + assert_eq!(f.git(&["rev-parse", "HEAD"]), head, "{args:?}"); + assert_eq!(f.git(&["show-ref"]), refs, "{args:?}"); + } +} + #[test] fn incremental_graph_import_preserves_independent_draft_edit() { let f = Fixture { diff --git a/atomic-core/src/pristine/traits/view.rs b/atomic-core/src/pristine/traits/view.rs index 008c8eab..8203c9d8 100644 --- a/atomic-core/src/pristine/traits/view.rs +++ b/atomic-core/src/pristine/traits/view.rs @@ -666,16 +666,17 @@ impl std::fmt::Display for StoredConflictKind { /// A conflict persisted in the `CONFLICTS` table for one file on one view. /// -/// Captured from the materialization result so the repository can report -/// conflicted files (`atomic status`) and refuse to `record` over markers -/// without re-running a full materialize. +/// Name conflicts can be derived before materialization. A known marker line +/// is captured only from a materialization result, so status can distinguish +/// an unmaterialized conflict from marker removal pending a recorded resolution. #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct StoredConflict { /// The kind of conflict. pub kind: StoredConflictKind, /// The file's path at detection time (for display). pub path: String, - /// 1-based line where the conflict region begins, if known. + /// 1-based observed marker line, if known. For Name conflicts, `None` + /// means the namespace conflict is known but no marker rendering is known. pub line: Option, /// Base32 hashes of the changes involved. pub sides: Vec, diff --git a/atomic-repository/src/repository/deferred_tree.rs b/atomic-repository/src/repository/deferred_tree.rs index e4245074..cc2482ae 100644 --- a/atomic-repository/src/repository/deferred_tree.rs +++ b/atomic-repository/src/repository/deferred_tree.rs @@ -989,6 +989,7 @@ impl Repository { } else { self.apply_deferred_tree_ops_in_txn(txn, &journal, &visibility)? }; + super::name_resolution::refresh_name_conflict_caches(txn, view.id)?; if changed { self.persist_deferred_tree_journal(&journal)?; } @@ -1604,7 +1605,9 @@ impl Repository { &*txn, &super::filter::view_membership(txn, &view)?, )?; - self.apply_deferred_tree_ops_in_txn(txn, &journal, &visibility) + let affected = self.apply_deferred_tree_ops_in_txn(txn, &journal, &visibility)?; + super::name_resolution::refresh_name_conflict_caches(txn, view.id)?; + Ok(affected) } fn apply_deferred_tree_ops_in_txn( diff --git a/atomic-repository/src/repository/name_resolution.rs b/atomic-repository/src/repository/name_resolution.rs index 6b237b72..d4840912 100644 --- a/atomic-repository/src/repository/name_resolution.rs +++ b/atomic-repository/src/repository/name_resolution.rs @@ -52,6 +52,106 @@ pub(super) struct ReducedPathClaims { pub(super) name_claims: HashMap, Vec<(String, PathClaimId)>>, } +/// Membership and parent changes alter descendant filters even when those +/// descendants are not materialized. Keep their derived name-conflict rows in +/// the same transaction as the publication. This does not render files, touch +/// snapshot membership, or replace unrelated order-conflict observations. +pub(super) fn refresh_name_conflict_caches( + txn: &mut atomic_core::pristine::WriteTxn<'_>, + changed_view: u64, +) -> Result<(), RepositoryError> { + use atomic_core::pristine::{MutTxnT, StoredConflict, StoredConflictKind, ViewTxnT}; + + let names: Vec<_> = txn + .snapshot_views()? + .into_iter() + .map(|(name, _)| name) + .collect(); + let mut affected = Vec::new(); + for name in names { + let view = txn + .get_view(&name)? + .ok_or_else(|| RepositoryError::ViewNotFound { name: name.clone() })?; + if txn + .resolve_full_view_chain(&view)? + .iter() + .any(|ancestor| ancestor.id == changed_view) + { + affected.push(view); + } + } + let claims = txn.iter_path_claims()?; + for view in affected { + // Match native realignment's compatibility with legacy dependency + // indexes. The membership/filter still bounds every visible claim. + let membership = super::filter::view_membership(txn, &view)?; + let visibility = GraphVisibilityClosure::try_from_membership_lenient(&*txn, &membership)?; + let reduced = reduce_path_claim_entries(&*txn, &visibility, &claims)?; + let old: BTreeMap<_, _> = txn.iter_conflicts(view.id)?.into_iter().collect(); + let mut records: BTreeMap> = old + .iter() + .filter_map(|(inode, conflicts)| { + let retained: Vec<_> = conflicts + .iter() + .filter(|conflict| conflict.kind != StoredConflictKind::Name) + .cloned() + .collect(); + (!retained.is_empty()).then_some((*inode, retained)) + }) + .collect(); + for (path, conflict) in reduced.conflicts.iter().collect::>() { + let mut sides = Vec::new(); + for change in conflict.sides.iter().flat_map(|side| &side.event_changes) { + let hash = txn.get_external(*change)?.ok_or_else(|| { + RepositoryError::Database(format!( + "name-conflict side change {} has no external hash", + change.get() + )) + })?; + sides.push(hash.to_base32()); + } + sides.sort(); + sides.dedup(); + let markers_observed = old.values().flatten().any(|record| { + record.kind == StoredConflictKind::Name + && record.path == *path + && record.sides == sides + && record.line == Some(1) + }); + for side in conflict.sides_at_path(path) { + let record = StoredConflict { + kind: StoredConflictKind::Name, + path: path.clone(), + // Deriving a name conflict is not proof that marker bytes + // have ever been materialized. Preserve only an unchanged, + // valid observation; status uses it for pending resolution. + line: (!side.is_directory() && markers_observed).then_some(1), + sides: sides.clone(), + }; + let rows = records.entry(side.inode.get()).or_default(); + if !rows.contains(&record) { + rows.push(record); + } + } + } + for inode in old + .keys() + .chain(records.keys()) + .copied() + .collect::>() + { + match records.get(&inode) { + Some(conflicts) if old.get(&inode) != Some(conflicts) => { + txn.put_conflicts(view.id, inode, conflicts)?; + } + None => txn.del_conflicts(view.id, inode)?, + _ => {} + } + } + } + Ok(()) +} + pub(super) fn path_claim_events_for_change( txn: &T, change_id: NodeId, diff --git a/atomic-repository/src/repository/repair.rs b/atomic-repository/src/repository/repair.rs index d68a7fb0..448e268e 100644 --- a/atomic-repository/src/repository/repair.rs +++ b/atomic-repository/src/repository/repair.rs @@ -1469,8 +1469,16 @@ fn collect_expected_conflicts( conflicts: &mut BTreeMap<(u64, Inode), Vec>, ) -> Result<(), RepositoryError> where - T: GraphTxnT + TreeTxnT + InodeGraphOps, + T: GraphTxnT + TreeTxnT + ViewTxnT + InodeGraphOps, { + let cached: Vec<_> = match txn.iter_conflicts(view_id) { + Ok(rows) => rows.into_iter().flat_map(|(_, records)| records).collect(), + // An unreadable observation is not authority. Derive the required + // conflict rows anyway; the independent actual-table comparison below + // still reports malformed CONFLICTS bytes (and other damaged indexes). + Err(PristineError::Serialization { .. }) => Vec::new(), + Err(error) => return Err(RepositoryError::Database(error.to_string())), + }; for (path, conflict) in &reduced.conflicts { let mut side_hashes = Vec::new(); for change_id in conflict @@ -1491,6 +1499,17 @@ where } side_hashes.sort(); side_hashes.dedup(); + // A derived Name row can be complete before its view is materialized. + // `line` is an observation, not graph authority: retain a valid marker + // location only for the same conflict. Missing/invalid observations do + // not justify fabricating one during repair. Paths, sides and required + // rows are still independently re-derived from authoritative claims. + let markers_observed = cached.iter().any(|record| { + record.kind == StoredConflictKind::Name + && record.path == *path + && record.sides == side_hashes + && record.line == Some(1) + }); for side in conflict.sides_at_path(path) { conflicts .entry((view_id, side.inode)) @@ -1498,7 +1517,7 @@ where .push(StoredConflict { kind: StoredConflictKind::Name, path: path.clone(), - line: (!side.is_directory()).then_some(1), + line: (!side.is_directory() && markers_observed).then_some(1), sides: side_hashes.clone(), }); } diff --git a/atomic-repository/src/repository/snapshot.rs b/atomic-repository/src/repository/snapshot.rs index 75706836..da2c3c61 100644 --- a/atomic-repository/src/repository/snapshot.rs +++ b/atomic-repository/src/repository/snapshot.rs @@ -252,6 +252,7 @@ impl Repository { }); } } + super::name_resolution::refresh_name_conflict_caches(&mut txn, snapshot_view.id)?; txn.commit() .map_err(|error| RepositoryError::Database(error.to_string()))?; diff --git a/atomic-repository/src/repository/status.rs b/atomic-repository/src/repository/status.rs index 2479ec73..e33514d3 100644 --- a/atomic-repository/src/repository/status.rs +++ b/atomic-repository/src/repository/status.rs @@ -135,7 +135,10 @@ impl Repository { .map_err(|error| RepositoryError::Database(error.to_string()))? .into_iter() .flat_map(|(_, records)| records) - .filter(|record| record.kind == atomic_core::pristine::StoredConflictKind::Name) + .filter(|record| { + record.kind == atomic_core::pristine::StoredConflictKind::Name + && record.line.is_some() + }) .map(|record| record.path) .collect(); diff --git a/atomic-repository/src/repository/tests/conflict_surface_tests.rs b/atomic-repository/src/repository/tests/conflict_surface_tests.rs index 03f796f5..f94b95fa 100644 --- a/atomic-repository/src/repository/tests/conflict_surface_tests.rs +++ b/atomic-repository/src/repository/tests/conflict_surface_tests.rs @@ -641,16 +641,16 @@ fn make_two_incarnation_name_conflict() -> (TempDir, TestRepository, std::path:: } /// A name conflict whose working tree already holds exactly one incarnation's -/// bytes must be resolved by a normal managed record even when no conflict row -/// was ever persisted. "Content-clean" must not mean the two live claims are +/// bytes must be resolved by a normal managed record even when no conflict +/// markers were ever materialized. "Content-clean" must not mean the two live claims are /// silently ignored; the winning side is chosen by byte equality (never by /// timestamp) and the losing claims are superseded, not erased. #[test] fn content_clean_name_conflict_is_resolved_by_record() { let (_temp, repo, new_file) = make_two_incarnation_name_conflict(); - // No materialize ran, so there is no persisted conflict row; the working - // tree simply keeps one side's bytes. + // No materialize ran. Derived Name rows may already exist, but they carry + // no observed marker line; the working tree simply keeps one side's bytes. std::fs::write(&new_file, "from-base\n").unwrap(); let on_disk = std::fs::read_to_string(&new_file).unwrap(); assert!( diff --git a/atomic-repository/src/repository/tests/native_index_repair_tests.rs b/atomic-repository/src/repository/tests/native_index_repair_tests.rs index 769436ad..8de82534 100644 --- a/atomic-repository/src/repository/tests/native_index_repair_tests.rs +++ b/atomic-repository/src/repository/tests/native_index_repair_tests.rs @@ -41,6 +41,204 @@ fn inode_graph_keys(repo: &Repository) -> Vec<(Inode, atomic_core::types::GraphN .unwrap() } +#[test] +fn inherited_name_conflict_cache_tracks_publication_and_unrecord() { + use atomic_core::pristine::{StoredConflict, StoredConflictKind}; + let (temp, mut repo) = create_temp_repo(); + let root = temp.path(); + std::fs::write(root.join("base.txt"), b"base\n").unwrap(); + repo.add("base.txt", TrackingOptions::default()).unwrap(); + record_all(&repo, "base"); + repo.create_draft_view("child", "dev").unwrap(); + repo.switch_view("child").unwrap(); + std::fs::write(root.join("same.txt"), b"child\n").unwrap(); + repo.add("same.txt", TrackingOptions::default()).unwrap(); + let child_change = record_all(&repo, "child claimant"); + repo.switch_view("dev").unwrap(); + std::fs::write(root.join("same.txt"), b"parent\n").unwrap(); + repo.add("same.txt", TrackingOptions::default()).unwrap(); + let parent_change = record_all(&repo, "parent claimant"); + let txn = repo.pristine.read_txn().unwrap(); + let child = txn.get_view("child").unwrap().unwrap(); + let parent = txn.get_view("dev").unwrap().unwrap(); + let rows = txn.iter_conflicts(child.id).unwrap(); + assert_eq!( + rows.len(), + 2, + "both inherited name-conflict sides must be cached before child materialization" + ); + assert!(rows.iter().all(|(_, records)| records + .iter() + .all(|record| record.kind == StoredConflictKind::Name && record.line.is_none()))); + let child_members = txn + .iter_changes(&child, 0) + .unwrap() + .collect::, _>>() + .unwrap(); + drop(txn); + let report = repo.verify_native_derived_indexes().unwrap(); + assert!(report.is_healthy(), "{:?}", report.problems); + + repo.create_draft_view("grandchild", "child").unwrap(); + let grandchild = repo + .pristine + .read_txn() + .unwrap() + .get_view("grandchild") + .unwrap() + .unwrap(); + assert_eq!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(grandchild.id) + .unwrap(), + rows, + "a newly created descendant must inherit the correct name-conflict cache" + ); + + // Refresh only the derived Name rows. An existing Order observation must + // survive byte-for-byte, and an aborted refresh must publish nothing. + let order = StoredConflict { + kind: StoredConflictKind::Order, + path: "same.txt".into(), + line: Some(7), + sides: Vec::new(), + }; + let inode = rows[0].0; + let mut txn = repo.pristine.write_txn().unwrap(); + let mut mixed = rows[0].1.clone(); + mixed.push(order.clone()); + txn.put_conflicts(child.id, inode, &mixed).unwrap(); + super::super::name_resolution::refresh_name_conflict_caches(&mut txn, parent.id).unwrap(); + let refreshed = txn.iter_conflicts(child.id).unwrap(); + assert!(refreshed + .iter() + .any(|(key, records)| *key == inode && records.contains(&order))); + assert_eq!( + txn.iter_changes(&child, 0) + .unwrap() + .collect::, _>>() + .unwrap(), + child_members + ); + txn.abort().unwrap(); + assert_eq!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(child.id) + .unwrap(), + rows + ); + + // Invalid marker observations are corruption, not evidence that a conflict + // was rendered. Doctor must detect them and repair without inventing one. + let mut txn = repo.pristine.write_txn().unwrap(); + let mut invalid = rows[0].1.clone(); + invalid[0].line = Some(99); + txn.put_conflicts(child.id, inode, &invalid).unwrap(); + txn.commit().unwrap(); + let report = repo.verify_native_derived_indexes().unwrap(); + assert!(report + .problems + .iter() + .any(|problem| problem.index == NativeIndex::Conflicts + && problem.kind == NativeIndexProblemKind::Mismatched)); + repo.repair_native_derived_indexes().unwrap(); + assert_eq!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(child.id) + .unwrap(), + rows + ); + + repo.unrecord( + parent_change.hash(), + crate::unrecord::UnrecordOptions::default(), + ) + .unwrap(); + assert!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(child.id) + .unwrap() + .is_empty(), + "retiring a parent reference must clear obsolete inherited Name rows" + ); + assert!(repo + .pristine + .read_txn() + .unwrap() + .iter_conflicts(grandchild.id) + .unwrap() + .is_empty()); + repo.reinsert_change(parent_change.hash(), None).unwrap(); + assert_eq!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(child.id) + .unwrap(), + rows + ); + assert_eq!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(grandchild.id) + .unwrap(), + rows + ); + repo.set_view_scope("child", ViewScope::Shared).unwrap(); + assert!(repo + .pristine + .read_txn() + .unwrap() + .iter_conflicts(child.id) + .unwrap() + .is_empty()); + assert!(repo + .pristine + .read_txn() + .unwrap() + .iter_conflicts(grandchild.id) + .unwrap() + .is_empty()); + repo.set_view_identity("child", ViewScope::Draft, Some("dev")) + .unwrap(); + assert_eq!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(child.id) + .unwrap(), + rows + ); + assert_eq!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(grandchild.id) + .unwrap(), + rows + ); + assert!(repo.load_change(child_change.hash()).is_ok()); + drop(repo); + let repo = Repository::open(root).unwrap(); + assert_eq!( + repo.pristine + .read_txn() + .unwrap() + .iter_conflicts(child.id) + .unwrap(), + rows + ); +} + #[test] fn native_index_verifier_accepts_healthy_nested_projection() { let (temp, repo) = create_temp_repo(); @@ -682,6 +880,14 @@ fn name_conflict_ambiguity_survives_repair_without_tree_winner() { let before = std::fs::read(temp.path().join("same.txt")).unwrap(); assert!(before.windows(7).any(|window| window == b">>>>>>>")); assert_eq!(repo.get_file_inode("same.txt").unwrap(), None); + assert!(repo.verify_native_derived_indexes().unwrap().is_healthy()); + // Publication now maintains the inactive descendant's cache. Deliberately + // corrupt it instead of relying on the old producer bug for a missing row. + let mut txn = repo.pristine.write_txn().unwrap(); + let feature = txn.get_view("feature").unwrap().unwrap(); + assert!(!txn.iter_conflicts(feature.id).unwrap().is_empty()); + txn.del_conflicts_prefix(feature.id).unwrap(); + txn.commit().unwrap(); let report = repo.verify_native_derived_indexes().unwrap(); assert!(report.problems.iter().any(|problem| { problem.index == NativeIndex::Conflicts && problem.kind == NativeIndexProblemKind::Missing diff --git a/atomic-repository/src/repository/views.rs b/atomic-repository/src/repository/views.rs index 2380a303..918847ef 100644 --- a/atomic-repository/src/repository/views.rs +++ b/atomic-repository/src/repository/views.rs @@ -195,6 +195,7 @@ impl Repository { txn.update_view(&new_view) .map_err(|e| RepositoryError::Database(e.to_string()))?; + super::name_resolution::refresh_name_conflict_caches(&mut txn, new_view.id)?; if txn.kg_writes_enabled()? { self.project_view_kg(&mut txn)?; @@ -276,6 +277,8 @@ impl Repository { txn.update_view(&view) .map_err(|e| RepositoryError::Database(e.to_string()))?; + super::name_resolution::refresh_name_conflict_caches(&mut txn, view.id)?; + if txn.kg_writes_enabled()? { self.project_view_kg(&mut txn)?; } @@ -339,6 +342,8 @@ impl Repository { txn.update_view(&view) .map_err(|e| RepositoryError::Database(e.to_string()))?; + super::name_resolution::refresh_name_conflict_caches(&mut txn, view.id)?; + if txn.kg_writes_enabled()? { self.project_view_kg(&mut txn)?; } @@ -812,8 +817,10 @@ impl Repository { None => None, }; - txn.create_view(name, scope, parent_id) + let view = txn + .create_view(name, scope, parent_id) .map_err(|e| RepositoryError::Database(e.to_string()))?; + super::name_resolution::refresh_name_conflict_caches(&mut txn, view.id)?; if txn.kg_writes_enabled()? { self.project_view_kg(&mut txn)?; diff --git a/atomic-repository/src/repository/workspace_txn.rs b/atomic-repository/src/repository/workspace_txn.rs index 1e09e007..055e07cf 100644 --- a/atomic-repository/src/repository/workspace_txn.rs +++ b/atomic-repository/src/repository/workspace_txn.rs @@ -843,6 +843,47 @@ struct CheckpointWire { } impl Repository { + /// Read-only early refusal for an ordinary workspace command, before its + /// writable database open can perform redb maintenance. This is a preflight, + /// not a lease: the real transaction must re-observe under operation locks. + /// Verified bound-HEAD adoption and pending journal recovery remain eligible + /// for that transaction; an unbound Git checkout is never adopted here. + /// + /// # Examples + /// + /// ```no_run + /// use atomic_repository::Repository; + /// use std::path::Path; + /// if let Some(remediation) = Repository::preflight_workspace_boundary(Path::new("."))? { + /// eprintln!("{}", remediation.describe()); + /// } + /// # Ok::<(), atomic_repository::RepositoryError>(()) + /// ``` + pub fn preflight_workspace_boundary( + root: &Path, + ) -> Result, RepositoryError> { + let mut repo = Self::open_readonly_for_operation_inspection(root)?; + if !repo.shim_workspace_active()? { + return Ok(None); + } + let working_copy = repo.require_working_copy_id()?; + if repo.pending_unsafe_recovery_work(working_copy)?.is_some() { + return Ok(None); + } + match repo.begin_workspace_txn(WorkspaceTxnMode::Observe)? { + WorkspaceTxnStart::Ready(_) => Ok(None), + WorkspaceTxnStart::Remediation(remediation) => { + if remediation.is_head_candidate() { + let observed = observe_git_metadata(root).map_err(observation_error)?; + if super::anchor::head_binding_for_observation(&repo, &observed).is_some() { + return Ok(None); + } + } + Ok(Some(remediation)) + } + } + } + /// Enter the shared workspace boundary while retaining its ordered locks. pub fn begin_workspace_txn( &mut self, diff --git a/libatomic/src/daemon/state.rs b/libatomic/src/daemon/state.rs index 340df1ac..cc68e060 100644 --- a/libatomic/src/daemon/state.rs +++ b/libatomic/src/daemon/state.rs @@ -139,6 +139,25 @@ impl RepoHandle { let deadline = std::time::Instant::now() + database_open_wait(); loop { let remaining = deadline.saturating_duration_since(std::time::Instant::now()); + if mode == WorkspaceTxnMode::Reconcile { + if let Some(remediation) = Repository::preflight_workspace_boundary(&self.root) + .map_err(repository_error)? + { + let retry_completed_switch = matches!( + &remediation, + WorkspaceRemediation::Unanchored { + state: UnanchoredWorkspace::AtomicCheckpointDrift { desired_view, .. }, + .. + } if target == Some(desired_view.as_str()) + ); + if !retry_completed_switch { + return Err(domain_status( + ErrorCode::PreconditionFailed, + format!("workspace reconciliation required: {}\nInspect with 'atomic status --no-reconcile'; then run 'atomic git shim reconcile' before retrying.", remediation.describe()), + )); + } + } + } let mut repo = match mode { WorkspaceTxnMode::Observe => Repository::open_readonly_wait(&self.root, remaining), WorkspaceTxnMode::Reconcile => { @@ -169,7 +188,7 @@ impl RepoHandle { return Err(domain_status( ErrorCode::PreconditionFailed, format!( - "workspace reconciliation required: {}", + "workspace reconciliation required: {}\nInspect with 'atomic status --no-reconcile'; then run 'atomic git shim reconcile' before retrying.", remediation.describe() ), )) @@ -255,7 +274,7 @@ pub(super) fn enter_workspace( atomic_repository::WorkspaceTxnStart::Remediation(remediation) => Err(domain_status( ErrorCode::PreconditionFailed, format!( - "workspace reconciliation required: {}", + "workspace reconciliation required: {}\nInspect with 'atomic status --no-reconcile'; then run 'atomic git shim reconcile' before retrying.", remediation.describe() ), )), diff --git a/tests/harness/42_git_shim_guard.sh b/tests/harness/42_git_shim_guard.sh index 48fbcdc0..7129d2c9 100644 --- a/tests/harness/42_git_shim_guard.sh +++ b/tests/harness/42_git_shim_guard.sh @@ -139,34 +139,16 @@ assert_guard_refusal() { fi local evidence_ok=1 + # The current workspace boundary exposes a typed reason and explicit + # remediation. Full checkpoint/manifest evidence belongs to the separate + # read-only forensic command, not the retired shadow guard's prose format. for expected in \ - "Unsafe operation: $operation" \ - "Refusal: Git moved away from the shim checkpoint" \ - "Old checkpoint Atomic view: main" \ - "Old checkpoint Atomic state:" \ - "Old checkpoint Atomic manifest root: atomic-visible-regular-files-blake3-v1:" \ - "Old checkpoint Git HEAD: attached refs/heads/main at" \ - "Old checkpoint Git HEAD tree:" \ - "Old checkpoint Git index tree:" \ - "Old checkpoint Git index digest:" \ - "Old checkpoint Git refs digest:" \ - "Old checkpoint Git refs: digest-only checkpoint evidence" \ - "Old checkpoint Git manifest root: git-tree-oid:" \ - "Current Atomic view: main" \ - "Current Atomic state:" \ - "Current Atomic manifest root: atomic-visible-regular-files-blake3-v1:" \ - "Current Git HEAD: attached refs/heads/raw-drift at" \ - "Current Git HEAD tree:" \ - "Current Git index tree:" \ - "Current Git index digest:" \ - "Current Git refs digest:" \ - "Current Git refs:" \ - "refs/heads/main ->" \ - "refs/heads/raw-drift ->" \ - "Current Git manifest root: git-tree-oid:" \ - "Remediation: Reconcile the observed Git baseline before retrying the refused operation." \ - " atomic status --no-reconcile" \ - " atomic git shim reconcile" + 'workspace reconciliation required:' \ + 'HeadSymrefChanged' \ + 'checkpoint: "refs/heads/main"' \ + 'observed: "refs/heads/raw-drift"' \ + 'atomic status --no-reconcile' \ + 'atomic git shim reconcile' do if ! printf '%s\n' "$output" | grep -qF -- "$expected"; then evidence_ok=0 @@ -174,9 +156,9 @@ assert_guard_refusal() { fi done if [[ "$evidence_ok" -eq 1 ]]; then - _pass "$label: reports old/current Atomic+Git, refs/index/roots, operation, and exact remediation" + _pass "$label: reports typed drift, old/new refs and exact remediation" else - _fail "$label: reports old/current Atomic+Git, refs/index/roots, operation, and exact remediation" "$output" + _fail "$label: reports typed drift, old/new refs and exact remediation" "$output" fi lines="$(printf '%s\n' "$output" | wc -l | tr -d '[:space:]')" @@ -250,7 +232,7 @@ else _fail "diff --change bypasses the guard and remains history-only" "exit $HISTORY_RC: $HISTORY_AFTER" fi -begin_section "Ordinary edits and Atomic-only advancement remain allowed" +begin_section "Ordinary edits and enrolled Atomic projection remain allowed" start_colocated_repo "git-shim-guard-atomic-advance" GIT_HEAD_BEFORE="$(git rev-parse HEAD)" overwrite_file tracked.txt "ordinary edit\n" @@ -265,11 +247,13 @@ else _fail "ordinary edit passes diff" "$DIFF_OUT" fi assert_success "ordinary edit passes record" atomic record -m "Atomic-only advancement" -if [[ "$(git rev-parse HEAD)" == "$GIT_HEAD_BEFORE" ]]; then - _pass "Atomic record does not move Git HEAD" +if [[ "$(git rev-parse HEAD)" != "$GIT_HEAD_BEFORE" ]] && \ + [[ "$(git show HEAD:tracked.txt)" == 'ordinary edit\n' ]]; then + _pass "enrolled Atomic record advances Git with the recorded bytes" else - _fail "Atomic record does not move Git HEAD" "Git HEAD changed" + _fail "enrolled Atomic record advances Git with the recorded bytes" "Git projection missing or incorrect" fi +assert_success "recorded projection passes shim verification" atomic git shim verify assert_success "status passes after Atomic-only advancement" atomic status --short create_file atomic-only.txt "further Atomic work\n" assert_success "add passes after Atomic-only advancement" atomic add atomic-only.txt diff --git a/tests/harness/43_git_shim_operation_recovery.sh b/tests/harness/43_git_shim_operation_recovery.sh index 6474841d..27365cd5 100644 --- a/tests/harness/43_git_shim_operation_recovery.sh +++ b/tests/harness/43_git_shim_operation_recovery.sh @@ -1,11 +1,10 @@ #!/usr/bin/env bash -# Regression contract for interruption-safe shim switching. -# -# The assertions are the original expected-red CB-1B contract, now promoted -# unchanged into the numbered integration harness. +# Returned-error recovery contract for shim switching and publication. +# Named fault seams must fire; this is not SIGKILL or power-loss evidence. HARNESS_DIR="$(cd "$(dirname "$0")" && pwd)" source "$HARNESS_DIR/helpers.sh" +source "$HARNESS_DIR/shim_helpers.sh" echo "" echo "${BOLD}══════════════════════════════════════════════════════════════${RESET}" @@ -62,8 +61,14 @@ UNINSTRUMENTED_FAILPOINTS=0 assert_clean_statuses() { local label="$1" local git_status atomic_status - git_status="$(git status --short)" - atomic_status="$(atomic status --short 2>/dev/null || true)" + if ! git_status="$(git status --short 2>&1)"; then + _fail "$label: read Git status" "$git_status" + return + fi + if ! atomic_status="$(atomic status --short 2>&1)"; then + _fail "$label: read Atomic status" "$atomic_status" + return + fi if [[ -z "$git_status" ]]; then _pass "$label: Git status is clean" else @@ -77,21 +82,16 @@ assert_clean_statuses() { } begin_section "Create offline main and feature projections" -make_temp_repo "red-switch-partial-removal" -init_git_repo -create_file "shared.txt" "shared projection\n" -create_file "target-only.txt" "main target-only content\n" -git add shared.txt target-only.txt -git commit --quiet -m "Main projection" +shim_fixture shim-operation-recovery +# Start from the imported Git baseline, as the original recovery contract did. +# tracked.txt is shared; base-only.txt is the target-only path restored on retry. MAIN="$(git_current_branch)" -assert_success "import main into Atomic without network or vault" atomic git import --no-vault -assert_success "create feature view" atomic view create feature --draft --parent "$MAIN" -assert_success "align feature view and Git branch" atomic view switch feature --force +require_success "create a real Git feature branch" git switch -qc feature # Two lexically sorted source-only tracked paths guarantee that the failpoint # after the first tracked removal observes a genuinely partial materialization. -rm target-only.txt +rm base-only.txt create_file "source-only/01-first.txt" "first source-only tracked file\n" create_file "source-only/02-second.txt" "second source-only tracked file\n" git add -A @@ -116,10 +116,10 @@ set +e FAIL_OUTPUT="$(ATOMIC_FAIL_SWITCH_AFTER_FIRST_TRACKED_REMOVAL=1 atomic git shim switch "$MAIN" 2>&1)" FAIL_RC=$? set -e -if [[ "$FAIL_RC" -ne 0 ]]; then +if [[ "$FAIL_RC" -ne 0 && "$FAIL_OUTPUT" == *"debug failpoint: switch failed after first tracked-path removal"* ]]; then _pass "failpoint makes shim switch fail" else - _fail "failpoint makes shim switch fail" "command unexpectedly succeeded: $FAIL_OUTPUT" + _fail "failpoint makes shim switch fail" "requested seam did not fire (exit $FAIL_RC): $FAIL_OUTPUT" fi ACTUAL_HEAD="$(git_head_sha_full)" @@ -127,8 +127,9 @@ ACTUAL_BRANCH="$(git_current_branch)" ACTUAL_VIEW="$(atomic_current_view)" ACTUAL_CHECKPOINT="$(checkpoint_hash)" ACTUAL_GIT_STATUS="$(git status --short)" -ACTUAL_ATOMIC_STATUS="$(atomic status --short 2>/dev/null || true)" +# Capture paths before a normal status boundary could perform recovery. snapshot_complete_state "$AFTER_FAILURE_STATE" +ACTUAL_ATOMIC_STATUS="$(atomic status --short 2>&1)" begin_section "Rollback contract" assert_equal "source Git HEAD is restored" "$SOURCE_HEAD" "$ACTUAL_HEAD" @@ -157,21 +158,21 @@ assert_equal "retry aligns Atomic view to target" "$MAIN" "$(atomic_current_view assert_clean_statuses "target projection after retry" assert_success "shim verify succeeds after retry" atomic git shim verify -begin_section "CB-8B: journaled projection publication recovers at its crash points" +begin_section "CB-8B: journaled projection publication recovers at returned-error seams" # The record-path projection journals its ref/HEAD publication before -# visibility (RFC §7.2 step 3/4, CB-8B). Each named failpoint crashes the +# visibility (RFC §7.2 step 3/4, CB-8B). Each named failpoint interrupts the # journaled publication at a phase boundary; the retry must complete or roll # back under the same leases — the original switch assertions above stay # unchanged, and these are ADDITIONAL Git-specific crash points. # # CB-13A R4 crash-evidence contract: an instrumented run must PROVE the # expected seam was reached (`debug failpoint: ` in the output) before -# any of this counts as a crash pass. A missing seam or an ordinary command +# any of this counts as an injected-failure pass. A missing seam or an ordinary command # error is never credited as a failpoint success, and an uninstrumented # (shipping) build is reported as an explicit documented limitation — never # as a passing crash check. for FAILPOINT_NAME in ATOMIC_FAIL_PROJECTION_BEFORE_REF ATOMIC_FAIL_PROJECTION_AFTER_EFFECTS; do - begin_section "Projection publication crash point: $FAILPOINT_NAME" + begin_section "Projection publication failure point: $FAILPOINT_NAME" atomic view switch "$MAIN" --force >/dev/null 2>&1 || true # Unique content per crash point: a previous iteration's successful # record (uninstrumented build) must not leave the tree looking clean. @@ -203,7 +204,7 @@ for FAILPOINT_NAME in ATOMIC_FAIL_PROJECTION_BEFORE_REF ATOMIC_FAIL_PROJECTION_A fi else _skip "projection publication failpoint: $FAILPOINT_NAME" \ - "uninstrumented build — the $FAILPOINT_NAME seam never triggered (record succeeded); crash point NOT exercised (documented limitation)" + "uninstrumented build — the $FAILPOINT_NAME seam never triggered (record succeeded); failure point NOT exercised" UNINSTRUMENTED_FAILPOINTS=$((UNINSTRUMENTED_FAILPOINTS + 1)) git reset -q 2>/dev/null || true continue @@ -211,10 +212,10 @@ for FAILPOINT_NAME in ATOMIC_FAIL_PROJECTION_BEFORE_REF ATOMIC_FAIL_PROJECTION_A # The journaled publication must leave the Git repository intact: the # ref exists, HEAD resolves, and the ODB is clean (no force overwrite, # no torn refs). - if git rev-parse --verify --quiet refs/heads/master >/dev/null; then + if git rev-parse --verify --quiet "refs/heads/$MAIN" >/dev/null; then _pass "projection ref survives the interruption: $FAILPOINT_NAME" else - _fail "projection ref survives the interruption: $FAILPOINT_NAME" "refs/heads/master missing" + _fail "projection ref survives the interruption: $FAILPOINT_NAME" "refs/heads/$MAIN missing" fi if git fsck --no-progress >/dev/null 2>&1; then _pass "Git object database intact after interruption: $FAILPOINT_NAME" diff --git a/tests/harness/shim.md b/tests/harness/shim.md index 869e949b..024a35d0 100644 --- a/tests/harness/shim.md +++ b/tests/harness/shim.md @@ -10,8 +10,8 @@ fixtures in `shim_helpers.sh`. They never configure a remote or push one. | `39_git_shim_workflow.sh` | Enroll; native record projects automatically; adopt Git-authored branches without rewriting Git-rendered files; repeat reconciliation preserves identities; native Draft switching detaches HEAD; explicit local `shim publish` creates a branch; explicit shim switching restores exact files, empty files, deletions, executable modes and symlinks. Unenrolled native repos do not acquire mirror branches. | | `40_git_shim_safety.sh` | Dirty/marker refusal, resolved retry, excluded metadata in the index, forged provenance, stale baseline, untracked file/parent/directory collisions, pre-materialize injection and successful retry. | | `41_git_shim_hooks.sh` | Advisory event/receipt evidence, correctness without checkout hooks, owned dispatchers, idempotent installation/uninstallation, and byte/mode preservation of unmanaged hooks. | -| `42_git_shim_guard.sh` | Strict early stale-baseline refusal across command boundaries, diagnostics and raw database-byte non-mutation. Its existing assertions remain unchanged; consolidation does not waive known failures. | -| `43_git_shim_operation_recovery.sh` | Separate interruption/recovery contract. Retained unchanged; missing injection seams are not crash passes. | +| `42_git_shim_guard.sh` | Early stale-baseline refusal across command boundaries, typed drift and actionable remediation, raw database-byte/operation/receipt non-mutation, forensic bypasses and normal enrolled projection. | +| `43_git_shim_operation_recovery.sh` | Local signed/imported baseline, actual Git feature branch and explicit adoption, partial-removal rollback and ordinary retry, plus instrumented publication returned-error seams. Missing seams are not passes. | ```sh ATOMIC_BIN=/absolute/path/to/atomic bash tests/harness/run_all.sh --list shim @@ -19,6 +19,18 @@ ATOMIC_BIN=/absolute/path/to/atomic bash tests/harness/run_all.sh shim bash tests/harness/shim_harness_selftest.sh ``` +For the **full fault-injection test**, build the test feature explicitly: + +```sh +cargo build --locked -p atomic-cli --features adoption-test-injection +ATOMIC_BIN="$PWD/target/debug/atomic" bash tests/harness/run_all.sh shim +``` + +Without that feature, suite 43 is intentionally partial: the two publication +seams are no-ops in a shipping build. These are injected **returned errors**, +not process kills or power-loss tests. Use a normal default-feature build for +ordinary demo/release use, not the fault-injection build. + `--list` does not build or invoke Atomic. Canonical suite discovery excludes the `legacy-shadow/` directory. Old runner selectors are redirected and deduplicated: @@ -59,7 +71,11 @@ Select canonical names if you do not want every suite sharing a numeric prefix. entries (apart from their derived head flags). This comparator does not byte-snapshot the redb database or every prior receipt body. Strict refusal cases permit no operation/object additions at all. Suite 42 - retains the stronger raw-database non-mutation gate separately. + retains the stronger raw-database non-mutation gate separately. Ordinary + command preflight now rejects an already observed unbound checkout before + opening the unified database writable. Verified bound-HEAD adoption and + pending recovery still enter the locked transaction and revalidate there; + preflight is not itself a lease or a race-free Git lock. - Legacy `shadow-validate:V1/V2/V3/V4` strings and `hook-errors.log` are not the current authority. Check actual refusal, causal state, and journal receipts. Marker override in a native-only repo remains in suite 30; it is not a bypass @@ -99,7 +115,36 @@ Validated offline against the pinned default-feature debug binary used for the | 42 strict guard (unchanged) | 104 passed, 12 failed, 0 skipped | | 43 operation recovery (unchanged) | Not rerun in this consolidation | -Suite 42 still exposes its existing diagnostic, raw-database refusal and Git -HEAD-preservation failures. Neither the WIP comparator nor consolidation removes -those assertions. The runner/discovery selftest passed seven checks. This is -not a full workspace, network, watcher or crash-injection certification. +Those are historical consolidation results. Subsequent shim repair preserved +all raw-state and journal comparisons while fixing writable-open-before-refusal. +It replaced the legacy shadow diagnostic prose expectation with typed drift +and exact remediation commands, and the obsolete “record leaves HEAD alone” +expectation with exact enrolled-record projection plus verification. + +| Suite | Fresh instrumented result | +|---|---| +| 35 serialization | 30 passed, 0 failed, 0 skipped | +| 39 workflow | 99 passed, 0 failed, 0 skipped | +| 40 safety | 115 passed, 0 failed, 0 skipped | +| 41 hooks | 38 passed, 0 failed, 0 skipped | +| 42 strict guard | 117 passed, 0 failed, 0 skipped | +| 43 returned-error recovery | 39 passed, 0 failed, 0 skipped | + +Default-feature suite 43 separately reported **29 passed, 0 failed, 2 skipped**. +The six CLI workspace integration tests passed, including new byte-for-byte +database non-mutation checks for status/diff/add/record/switch/restore under an +unbound checkout and existing bound/adoption regressions. Native suites 23/32/40, +all 15 local-service tests, workspace check and workspace Clippy also passed. +This is not full network, watcher, process-kill or power-loss certification. + +### Separate maintenance edge found during setup + +An alternate fixture that adds two native paths (`shared.txt`, `target-only.txt`) +on an enrolled imported baseline, records/projects them, then creates a Git +feature branch which deletes one path and adds two source-only paths, reconciles +and verifies its visible projection. However, `doctor check` reports four missing +`CONFLICTS` cache rows in the private `wc/` snapshot view. Repair populates +them, but that cache-completeness issue is **not fixed** here. The reproduction +was retained in disposable evidence rather than weakening doctor checks. Suite +43 retains its original imported-baseline scope and still runs doctor checks +for its foreign deletion before recovery testing. From 8180bf32e2a6e1faf7b3bc014562b1256b498130 Mon Sep 17 00:00:00 2001 From: Lee Faus Date: Thu, 5 Nov 2026 20:01:12 -0500 Subject: [PATCH 06/14] final docs --- tests/harness/shim.md | 48 +++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/tests/harness/shim.md b/tests/harness/shim.md index 024a35d0..66f56064 100644 --- a/tests/harness/shim.md +++ b/tests/harness/shim.md @@ -137,14 +137,40 @@ unbound checkout and existing bound/adoption regressions. Native suites 23/32/40 all 15 local-service tests, workspace check and workspace Clippy also passed. This is not full network, watcher, process-kill or power-loss certification. -### Separate maintenance edge found during setup - -An alternate fixture that adds two native paths (`shared.txt`, `target-only.txt`) -on an enrolled imported baseline, records/projects them, then creates a Git -feature branch which deletes one path and adds two source-only paths, reconciles -and verifies its visible projection. However, `doctor check` reports four missing -`CONFLICTS` cache rows in the private `wc/` snapshot view. Repair populates -them, but that cache-completeness issue is **not fixed** here. The reproduction -was retained in disposable evidence rather than weakening doctor checks. Suite -43 retains its original imported-baseline scope and still runs doctor checks -for its foreign deletion before recovery testing. +### Doctor conflict-cache maintenance correction + +The retained native-add/foreign-branch fixture reproduced four missing +`CONFLICTS` rows in a private `wc/` view. The failure already existed after +the native record: the parent advanced, so the private view inherited a new +claimant, but its cache had not been refreshed because it was not materialized. + +Name-conflict rows are now refreshed for affected views and descendants in the +same transaction as graph/membership publication and native realignment. +Creation, snapshot-view setup, scope changes and reparenting maintain them too. +This changes derived cache rows only: snapshot membership, immutable change +bytes and unrelated Order observations are retained. + +A derived name conflict is not proof that markers were rendered. New derived +Name rows carry no observed marker line; materialization can supply one, and +an unchanged valid observation is preserved. `status` therefore still reports +unmaterialized competing claims as Conflicted even if one side's marker-free +bytes is on disk. Doctor re-derives required rows/claimant sides, validates +observed locations and still reports missing, mismatched or malformed cache +rows. Explicit repair does not fabricate missing marker observations. + +The new CLI regression failed before the correction and now passes after +native recording, explicit corruption/repair, reopen and Git reconciliation. +It verifies snapshot membership/state and change bytes are preserved. The +repository regression also exercises descendant creation, unrecord/reinsert, +promotion/reparenting, retained Order records and aborted transactions. + +Validation: **1,553 repository library tests passed, 0 failed, 3 ignored**; +all seven CLI workspace tests passed. Native 23/32/40 and shim 35/39/40/41/42 +passed without skips; instrumented recovery 43 passed 39/39. Workspace check, +workspace Clippy and Rust 1.90 core/repository checks passed. No power-loss or +network certification is implied. + +Repositories that already have the missing cache rows may still need explicit +`atomic doctor repair-native-indexes` with the corrected binary, followed by +`atomic doctor check`. Checking remains read-only; upgrading does not silently +repair or discard their private snapshots. No repair was run on the demo checkout. From f4a9d8fc8d62ea0111e3a713dd18265e03da5ef0 Mon Sep 17 00:00:00 2001 From: Lee Faus Date: Fri, 6 Nov 2026 12:52:06 -0500 Subject: [PATCH 07/14] faster materialization with spans --- atomic-core/src/change/mod.rs | 2 + atomic-core/src/change/store.rs | 24 + atomic-core/src/change/verified.rs | 261 +++++++++ .../src/pristine/span_patch_storage.rs | 188 +++++- .../span_patch_storage/build_tests.rs | 425 ++++++++++++++ .../examples/imported_materialize_bench.rs | 422 ++++++++++++++ .../examples/materialize_bench.rs | 551 ++++++++++++++++++ atomic-repository/src/changestore/mod.rs | 32 +- atomic-repository/src/changestore/tests.rs | 66 +++ .../src/changestore/trait_impl.rs | 19 +- .../src/repository/materialize.rs | 226 ++++--- .../repository/materialize/verified_tests.rs | 106 ++++ atomic-repository/src/repository/switch.rs | 67 ++- atomic-repository/src/repository/tests/mod.rs | 1 + .../tests/switch_lease_render_tests.rs | 217 +++++++ .../IMPORTED-COLD-RESULTS.md | 125 ++++ tools/materialize-bench/IMPORTED-COLD.md | 148 +++++ tools/materialize-bench/README.md | 74 +++ tools/materialize-bench/RESULTS.md | 102 ++++ tools/materialize-bench/SPAN-CONTEXT.md | 170 ++++++ .../materialize-bench/SWITCH-OPTIMIZATION.md | 140 +++++ tools/materialize-bench/VERIFIED-LOADING.md | 198 +++++++ tools/materialize-bench/imported_guest.py | 340 +++++++++++ tools/materialize-bench/run.py | 213 +++++++ .../materialize-bench/test_imported_guest.py | 76 +++ tools/materialize-bench/test_runner.py | 54 ++ 26 files changed, 4137 insertions(+), 110 deletions(-) create mode 100644 atomic-core/src/change/verified.rs create mode 100644 atomic-core/src/pristine/span_patch_storage/build_tests.rs create mode 100644 atomic-repository/examples/imported_materialize_bench.rs create mode 100644 atomic-repository/examples/materialize_bench.rs create mode 100644 atomic-repository/src/repository/materialize/verified_tests.rs create mode 100644 atomic-repository/src/repository/tests/switch_lease_render_tests.rs create mode 100644 tools/materialize-bench/IMPORTED-COLD-RESULTS.md create mode 100644 tools/materialize-bench/IMPORTED-COLD.md create mode 100644 tools/materialize-bench/README.md create mode 100644 tools/materialize-bench/RESULTS.md create mode 100644 tools/materialize-bench/SPAN-CONTEXT.md create mode 100644 tools/materialize-bench/SWITCH-OPTIMIZATION.md create mode 100644 tools/materialize-bench/VERIFIED-LOADING.md create mode 100644 tools/materialize-bench/imported_guest.py create mode 100644 tools/materialize-bench/run.py create mode 100644 tools/materialize-bench/test_imported_guest.py create mode 100644 tools/materialize-bench/test_runner.py diff --git a/atomic-core/src/change/mod.rs b/atomic-core/src/change/mod.rs index ffaa8066..e798d7af 100644 --- a/atomic-core/src/change/mod.rs +++ b/atomic-core/src/change/mod.rs @@ -108,6 +108,7 @@ pub mod session; pub mod signing; pub mod span_patch; mod store; +mod verified; // Re-export all public types // Allow deprecated types - these are re-exported for backward compatibility @@ -139,6 +140,7 @@ pub use provenance::{ AITool, AIVendor, AuthorshipParts, Cost, PromptContent, Provenance, SuggestionType, TokenUsage, }; pub use store::{ChangeStore, MemoryChangeStore, MemoryStoreError}; +pub use verified::{VerifiedChange, VerifiedChangeCache, VerifiedLoadError}; #[cfg(test)] mod tests { diff --git a/atomic-core/src/change/store.rs b/atomic-core/src/change/store.rs index 99dcae47..4772f3a0 100644 --- a/atomic-core/src/change/store.rs +++ b/atomic-core/src/change/store.rs @@ -105,6 +105,30 @@ pub trait ChangeStore: Send + Sync { /// Returns an error if the change is not found or cannot be loaded. fn get_change(&self, hash: &Hash) -> Result; + /// Load an immutable identity-bound value. Custom stores keep conservative + /// canonical hash verification unless they provide a checked encoded reader. + fn get_verified_change( + &self, + hash: &Hash, + ) -> Result> { + let change = self + .get_change(hash) + .map_err(super::VerifiedLoadError::Store)?; + let verified = super::VerifiedChange::from_change(change)?; + verified.check_hash(hash)?; + Ok(verified) + } + + /// Revalidate a cache hit against current backing data. The default reloads + /// and verifies; stores must not trust a pathname, mtime or a prior receipt. + fn revalidate_verified_change( + &self, + hash: &Hash, + _cached: &super::VerifiedChange, + ) -> Result> { + self.get_verified_change(hash) + } + /// Check if a change exists. /// /// # Arguments diff --git a/atomic-core/src/change/verified.rs b/atomic-core/src/change/verified.rs new file mode 100644 index 00000000..ec10cb62 --- /dev/null +++ b/atomic-core/src/change/verified.rs @@ -0,0 +1,261 @@ +//! Immutable verification witnesses and operation-local decoded-change reuse. +use super::{Change, ChangeError, ChangeStore}; +use crate::types::Hash; +use std::io::Cursor; +use std::sync::{Arc, RwLock}; + +/// A change bound to an identity by checked decoding or canonical serialization. +/// +/// No mutable access is exposed: modifying a clone of the decoded change cannot +/// change the value covered by this witness. This is not a persistent trust bit. +#[derive(Clone, Debug)] +pub struct VerifiedChange { + hash: Hash, + change: Arc, + source_digest: Option, +} + +impl VerifiedChange { + /// Verify the original V3 bytes, section manifest and decoded semantics. + /// The original encoded identity is authoritative; no recompression is needed. + pub fn from_bytes(bytes: &[u8]) -> Result { + let mut reader = Cursor::new(bytes); + let (change, hash) = Change::deserialize(&mut reader)?; + if reader.position() != bytes.len() as u64 { + return Err(ChangeError::Invalid( + "trailing bytes after verified change".into(), + )); + } + Ok(Self { + hash, + change: Arc::new(change), + source_digest: Some(Hash::of(bytes)), + }) + } + + /// Conservative fallback for stores that only provide decoded values. + pub fn from_change(change: Change) -> Result { + let hash = change.hash()?; + Ok(Self { + hash, + change: Arc::new(change), + source_digest: None, + }) + } + + pub fn hash(&self) -> Hash { + self.hash + } + pub fn change(&self) -> &Change { + &self.change + } + + /// Check the requested identity even when a store returned a valid witness + /// for some other change. + pub fn check_hash(&self, expected: &Hash) -> Result<(), ChangeError> { + if self.hash != *expected { + return Err(ChangeError::Invalid(format!( + "change hash mismatch: expected {expected:?}, computed {:?}", + self.hash + ))); + } + Ok(()) + } + + /// Whether these bytes exactly match the previously verified source, + /// including unhashed notes/signatures. A change in any byte requires a + /// new checked decode, not a timestamp-based cache hit. + pub fn matches_source_bytes(&self, bytes: &[u8]) -> bool { + self.source_digest + .is_some_and(|digest| digest == Hash::of(bytes)) + } +} + +#[derive(Debug, thiserror::Error)] +pub enum VerifiedLoadError { + #[error(transparent)] + Store(E), + #[error(transparent)] + Verification(#[from] ChangeError), +} + +/// A bounded, operation-local cache of verified decoded values, never indexes +/// or causal proofs. Every hit asks the underlying store to revalidate its source. +/// +/// `max_entries` is an entry-count bound, not a decoded-byte memory bound. Once +/// full, further changes are verified without admission. Drop the adapter when +/// the operation finishes; do not install it as a global repository cache. +pub struct VerifiedChangeCache<'a, S: ChangeStore> { + store: &'a S, + entries: RwLock>, + max_entries: usize, +} + +impl<'a, S: ChangeStore> VerifiedChangeCache<'a, S> { + pub fn new(store: &'a S, max_entries: usize) -> Self { + Self { + store, + entries: RwLock::new(Default::default()), + max_entries, + } + } +} + +impl ChangeStore for VerifiedChangeCache<'_, S> { + type Error = S::Error; + + fn get_change(&self, hash: &Hash) -> Result { + self.store.get_change(hash) + } + + fn get_verified_change( + &self, + hash: &Hash, + ) -> Result> { + let cached = self + .entries + .read() + .ok() + .and_then(|entries| entries.get(hash).cloned()); + let verified = match cached { + Some(cached) => self.store.revalidate_verified_change(hash, &cached)?, + None => self.store.get_verified_change(hash)?, + }; + verified.check_hash(hash)?; + if let Ok(mut entries) = self.entries.write() { + if entries.contains_key(hash) || entries.len() < self.max_entries { + entries.insert(*hash, verified.clone()); + } + } + Ok(verified) + } + + fn get_contents( + &self, + hash_fn: F, + node: crate::types::GraphNode, + buf: &mut [u8], + ) -> Result + where + F: Fn(crate::types::NodeId) -> Option, + { + self.store.get_contents(hash_fn, node, buf) + } + + fn get_contents_ext( + &self, + node: crate::types::GraphNode>, + buf: &mut [u8], + ) -> Result { + self.store.get_contents_ext(node, buf) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::change::ChangeHeader; + + #[test] + fn verified_bytes_bind_identity_and_do_not_allow_mutation() { + let change = Change::new( + ChangeHeader::new("original"), + vec![], + b"hello".to_vec(), + vec![], + ); + let mut bytes = Vec::new(); + let hash = change.serialize(&mut bytes).unwrap(); + let verified = VerifiedChange::from_bytes(&bytes).unwrap(); + verified.check_hash(&hash).unwrap(); + assert!(verified.matches_source_bytes(&bytes)); + let mut cloned = verified.change().clone(); + cloned.contents[0] = b'X'; + assert_eq!(verified.change().contents, b"hello"); + assert!(verified.check_hash(&Hash::of(b"wrong identity")).is_err()); + bytes.push(1); + assert!(!verified.matches_source_bytes(&bytes)); + assert!(VerifiedChange::from_bytes(&bytes).is_err()); + } + + #[test] + fn changed_encoded_bytes_and_truncated_sources_are_rejected() { + let change = Change::new(ChangeHeader::new("root"), vec![], b"hello".to_vec(), vec![]); + let mut bytes = Vec::new(); + change.serialize(&mut bytes).unwrap(); + for index in [0, bytes.len() / 2, bytes.len() - 1] { + let mut altered = bytes.clone(); + altered[index] ^= 1; + assert!(VerifiedChange::from_bytes(&altered).is_err()); + } + assert!(VerifiedChange::from_bytes(&bytes[..bytes.len() - 1]).is_err()); + assert!(!VerifiedChange::from_change(change) + .unwrap() + .matches_source_bytes(&bytes)); + } + + #[test] + fn verified_identity_uses_original_encoding_not_a_rechunked_rewrite() { + use crate::change::format_v3::types::envelope::encode_change_header_v2; + use crate::change::format_v3::{ChangeWriter, FileHeader, HashDedupTable, WriterOptions}; + let change = Change::new( + ChangeHeader::new("two original chunks"), + vec![], + b"hello".to_vec(), + vec![], + ); + let table = HashDedupTable::new([0; 32]); + let header = FileHeader::builder() + .hash_table_entries(table.len() as u32) + .contents_chunks(2) + .build(); + let mut bytes = Vec::new(); + let mut writer = ChangeWriter::new(&mut bytes, WriterOptions::default()); + writer.write_file_header(&header).unwrap(); + writer.write_hash_table(&table).unwrap(); + writer + .write_change_header_v2_payload( + &encode_change_header_v2(&change.hashed, &table).unwrap(), + ) + .unwrap(); + writer.write_dependencies(&[]).unwrap(); + writer.write_content_chunk(0, b"he").unwrap(); + writer.write_content_chunk(1, b"llo").unwrap(); + let original = Hash::from_bytes(writer.finalize().unwrap().content_hash); + let verified = VerifiedChange::from_bytes(&bytes).unwrap(); + assert_eq!(verified.change().contents, b"hello"); + verified.check_hash(&original).unwrap(); + assert_ne!(verified.hash(), verified.change().hash().unwrap()); + } + + #[test] + fn custom_store_cache_hits_still_reload_and_verify_and_admission_is_bounded() { + use crate::change::MemoryChangeStore; + let store = MemoryChangeStore::new(); + let first = Change::new(ChangeHeader::new("first"), vec![], b"one".to_vec(), vec![]); + let hash = store.insert_change(first.clone()).unwrap(); + let other = store + .insert_change(Change::new( + ChangeHeader::new("other"), + vec![], + b"two".to_vec(), + vec![], + )) + .unwrap(); + let cache = VerifiedChangeCache::new(&store, 1); + cache.get_verified_change(&hash).unwrap(); + cache.get_verified_change(&other).unwrap(); + assert_eq!(cache.entries.read().unwrap().len(), 1); + let mut tampered = first.clone(); + tampered.contents[0] ^= 1; + store.insert(hash, tampered); + assert!(cache.get_verified_change(&hash).is_err()); + store.insert(hash, first); + cache.get_verified_change(&hash).unwrap(); + store.remove(&hash); + assert!(cache.get_verified_change(&hash).is_err()); + let disabled = VerifiedChangeCache::new(&store, 0); + disabled.get_verified_change(&other).unwrap(); + assert!(disabled.entries.read().unwrap().is_empty()); + } +} diff --git a/atomic-core/src/pristine/span_patch_storage.rs b/atomic-core/src/pristine/span_patch_storage.rs index 3536280d..f5c6abf7 100644 --- a/atomic-core/src/pristine/span_patch_storage.rs +++ b/atomic-core/src/pristine/span_patch_storage.rs @@ -10,13 +10,14 @@ use super::{ }; pub use crate::change::span_patch::SpanProjectionId; use crate::change::span_patch::{ExternalFile, ExternalSpan, SpanOp, SpanPatch}; -use crate::change::{Change, ChangeStore}; +use crate::change::{Change, ChangeStore, VerifiedChange}; use crate::types::{Hash, Inode, NodeId}; use anyhow::{bail, ensure, Context, Result}; use rayon::prelude::*; use redb::{MultimapTableDefinition, ReadableMultimapTable, ReadableTable, TableDefinition}; use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::sync::Arc; +use std::time::{Duration, Instant}; pub(super) const FILES: TableDefinition<&[u8; 36], u64> = TableDefinition::new("span_patch_files_v1"); @@ -462,6 +463,81 @@ struct RenderChange { type ExpectedEdits = HashMap>; const MAX_PROOF_BYTES: usize = 4 * 1024 * 1024; +/// Wall-clock construction phases and transaction-local lookup counts. +/// +/// Loading includes the store's checked identity verification. The separate +/// `verify_hashes` stage binds its immutable witness to the requested hash and +/// extracts render data; it no longer recompresses filesystem-backed changes. +/// Times are bounded parallel-batch wall time, not summed worker CPU time. +/// `total` also includes allocation and loop overhead. +/// These metrics describe one context, not an entire materialization operation. +#[derive(Clone, Debug, Default)] +pub struct SpanContextBuildMetrics { + /// Version 2 uses checked loading plus witness binding; the original phase + /// instrumentation (version 1) reserialized changes in `verify_hashes`. + pub pipeline_version: u8, + pub resolve_changes: Duration, + pub load_changes: Duration, + pub verify_hashes: Duration, + pub validate_dependencies: Duration, + pub assemble_edits: Duration, + pub causal_proof: Duration, + pub total: Duration, + pub changes: usize, + pub edits: usize, + pub dependency_reads: usize, + pub internal_lookups: usize, + pub internal_hits: usize, + pub file_lookups: usize, + pub file_hits: usize, +} + +/// Never seed inverse IDs from EXTERNAL: INTERNAL must still be checked at +/// least once per referenced hash in this exact borrowed transaction. +#[derive(Default)] +struct SpanBuildLookups { + internal: HashMap, + files: HashMap<(Hash, u32), Inode>, +} + +impl SpanBuildLookups { + fn internal( + &mut self, + txn: &T, + hash: &Hash, + missing: &'static str, + metrics: &mut SpanContextBuildMetrics, + ) -> Result { + if let Some(id) = self.internal.get(hash) { + metrics.internal_hits += 1; + return Ok(*id); + } + metrics.internal_lookups += 1; + let id = txn.get_internal(hash)?.context(missing)?; + self.internal.insert(*hash, id); + Ok(id) + } + + fn file( + &mut self, + txn: &T, + file: &ExternalFile, + metrics: &mut SpanContextBuildMetrics, + ) -> Result { + let key = (file.owner, file.ordinal); + if let Some(inode) = self.files.get(&key) { + metrics.file_hits += 1; + return Ok(*inode); + } + metrics.file_lookups += 1; + let inode = txn + .span_file_inode(file)? + .context("span file mapping missing")?; + self.files.insert(key, inode); + Ok(inode) + } +} + // Transaction-local exact reachability; never use local NodeId ordering as // causality. When the bounded proof does not fit, use checked graph walks. struct CausalProof { @@ -471,15 +547,15 @@ struct CausalProof { } impl CausalProof { - fn build( - txn: &T, - visible: &EffectiveProjectionClosure, + fn build( + order: &[NodeId], + dependencies: &HashMap>, budget: usize, ) -> Result> { if budget == 0 { return Ok(None); } - let count = visible.len(); + let count = order.len(); let words = count.div_ceil(64); let Some(cells) = count.checked_mul(words) else { return Ok(None); @@ -494,7 +570,6 @@ impl CausalProof { if bytes > budget.min(MAX_PROOF_BYTES) { return Ok(None); } - let order: Vec<_> = visible.iter_dependency_first().copied().collect(); let mut ranks: Vec<_> = order .iter() .enumerate() @@ -514,10 +589,10 @@ impl CausalProof { let (earlier, rest) = bits.split_at_mut(rank * words); let row = &mut rest[..words]; row[rank / 64] |= 1u64 << (rank % 64); - for dep in txn.get_change_deps(id)? { - let dep_id = txn - .get_internal(&dep)? - .context("span causal proof dependency missing")?; + for dep_id in dependencies + .get(&id) + .context("span causal proof dependencies missing")? + { let index = ranks .binary_search_by_key(&dep_id.get(), |(id, _)| id.get()) .map_err(|_| anyhow::anyhow!("span causal proof closure is incomplete"))?; @@ -562,6 +637,7 @@ pub struct SpanRenderContext<'a, T: SpanPatchReadTxn> { changes: HashMap>, expected: ExpectedEdits, proof: Option, + metrics: SpanContextBuildMetrics, } /// A bounded render never retains more than the requested number of bytes. @@ -965,12 +1041,22 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { visible: &'a EffectiveProjectionClosure, budget: usize, ) -> Result { + let trace = std::env::var_os("ATOMIC_TRACE_SPAN_RENDER").is_some(); + let started = Instant::now(); + let mut metrics = SpanContextBuildMetrics { + pipeline_version: 2, + ..Default::default() + }; let mut changes = HashMap::new(); let mut expected = ExpectedEdits::new(); let ids: Vec<_> = visible.iter_dependency_first().copied().collect(); + metrics.changes = ids.len(); + let mut dependencies = HashMap::with_capacity(ids.len()); + let mut lookups = SpanBuildLookups::default(); // Hashing and reads may run in parallel, but report errors in the // dependency order and limit speculative work after a bad object. for batch in ids.chunks(64) { + let phase = Instant::now(); let hashes: Vec<_> = batch .iter() .map(|id| { @@ -979,25 +1065,39 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { .and_then(|hash| hash.context("visible hash missing")) }) .collect(); - let loaded: Vec)>> = hashes + metrics.resolve_changes += phase.elapsed(); + let phase = Instant::now(); + let loaded: Vec> = hashes .into_par_iter() .map(|hash| { let hash = hash?; - let mut change = store.get_change(&hash)?; - ensure!(change.hash()? == hash, "change hash mismatch"); + Ok((hash, store.get_verified_change(&hash)?)) + }) + .collect(); + metrics.load_changes += phase.elapsed(); + let phase = Instant::now(); + let loaded: Vec)>> = loaded + .into_par_iter() + .map(|loaded| { + let (hash, verified) = loaded?; + verified.check_hash(&hash)?; + let change = verified.change(); Ok(( hash, Arc::new(RenderChange { - contents: std::mem::take(&mut change.contents), - dependencies: std::mem::take(&mut change.hashed.dependencies), - span: change.hashed.span.take(), + contents: change.contents.clone(), + dependencies: change.hashed.dependencies.clone(), + span: change.hashed.span.clone(), }), )) }) .collect(); + metrics.verify_hashes += phase.elapsed(); for (id, loaded) in batch.iter().zip(loaded) { let (hash, change) = loaded?; + let phase = Instant::now(); let mut indexed = txn.get_change_deps(*id)?; + metrics.dependency_reads += 1; let mut declared = change.dependencies.clone(); indexed.sort(); declared.sort(); @@ -1005,7 +1105,26 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { indexed == declared, "dependency index differs from hashed change" ); + let resolved = indexed + .iter() + .map(|dep| { + let dep_id = lookups.internal( + txn, + dep, + "span causal proof dependency missing", + &mut metrics, + )?; + ensure!( + visible.contains(dep_id), + "span causal proof closure is incomplete" + ); + Ok(dep_id) + }) + .collect::>>()?; + dependencies.insert(*id, resolved); + metrics.validate_dependencies += phase.elapsed(); changes.insert(hash, Arc::clone(&change)); + let phase = Instant::now(); if let Some(patch) = &change.span { for op in &patch.ops { if let SpanOp::Edit { @@ -1017,12 +1136,13 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { insert_end, } = op { - let inode = txn - .span_file_inode(file)? - .context("span file mapping missing")?; - let owner = txn - .get_internal(&target.owner)? - .context("span target missing")?; + let inode = lookups.file(txn, file, &mut metrics)?; + let owner = lookups.internal( + txn, + &target.owner, + "span target missing", + &mut metrics, + )?; ensure!(visible.contains(owner), "span target outside view"); let key = span_key(inode, owner, target.start, target.end); let row: [u8; 40] = @@ -1033,21 +1153,40 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { expected.entry(inode.get()).or_default().insert((key, row)), "duplicate hashed span edit" ); + metrics.edits += 1; } } } + metrics.assemble_edits += phase.elapsed(); } } - let proof = CausalProof::build(txn, visible, budget)?; + let phase = Instant::now(); + let proof = CausalProof::build(&ids, &dependencies, budget)?; + metrics.causal_proof = phase.elapsed(); + metrics.total = started.elapsed(); + if trace { + eprintln!( + "[span context] changes={} elapsed={:?} metrics={:?}", + ids.len(), + metrics.total, + metrics, + ); + } Ok(Self { txn, visible, changes, expected, proof, + metrics, }) } + /// Construction costs for this context's exact transaction and visibility. + pub fn build_metrics(&self) -> &SpanContextBuildMetrics { + &self.metrics + } + /// Render an indexed root when its creating change is visible. pub fn render_if_visible(&self, inode: Inode) -> Result>> { let Some(root) = self.txn.span_root(inode)? else { @@ -1412,6 +1551,9 @@ fn append_visible(frame: &RenderFrame, start: usize, end: usize, out: &mut Rende } } +#[cfg(test)] +mod build_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/atomic-core/src/pristine/span_patch_storage/build_tests.rs b/atomic-core/src/pristine/span_patch_storage/build_tests.rs new file mode 100644 index 00000000..004a2485 --- /dev/null +++ b/atomic-core/src/pristine/span_patch_storage/build_tests.rs @@ -0,0 +1,425 @@ +use super::*; +use crate::change::{ChangeHeader, MemoryChangeStore}; +use crate::pristine::{MutTxnT, Pristine, PristineError}; +use crate::types::{EdgeFlags, GraphNode, Position}; +use std::cell::{Cell, RefCell}; + +struct CountedTxn<'a> { + inner: &'a ReadTxn, + dependencies: Cell, + internals: RefCell>, + files: RefCell>, + missing_internal: Option, +} + +impl<'a> CountedTxn<'a> { + fn new(inner: &'a ReadTxn) -> Self { + Self { + inner, + dependencies: Cell::new(0), + internals: RefCell::default(), + files: RefCell::default(), + missing_internal: None, + } + } +} + +impl GraphTxnT for CountedTxn<'_> { + type Adj = ::Adj; + fn get_external(&self, id: NodeId) -> std::result::Result, PristineError> { + self.inner.get_external(id) + } + fn get_internal(&self, hash: &Hash) -> std::result::Result, PristineError> { + *self.internals.borrow_mut().entry(*hash).or_default() += 1; + if self.missing_internal == Some(*hash) { + return Ok(None); + } + self.inner.get_internal(hash) + } + fn iter_adjacent( + &self, + node: GraphNode, + min: EdgeFlags, + max: EdgeFlags, + ) -> std::result::Result { + self.inner.iter_adjacent(node, min, max) + } + fn find_block( + &self, + pos: Position, + ) -> std::result::Result, PristineError> { + self.inner.find_block(pos) + } + fn find_block_end( + &self, + pos: Position, + ) -> std::result::Result, PristineError> { + self.inner.find_block_end(pos) + } + fn has_vertex(&self, node: GraphNode) -> std::result::Result { + self.inner.has_vertex(node) + } + fn get_node_type(&self, id: NodeId) -> std::result::Result, PristineError> { + self.inner.get_node_type(id) + } + fn get_rev_deps(&self, id: NodeId) -> std::result::Result, PristineError> { + self.inner.get_rev_deps(id) + } + fn get_change_deps(&self, id: NodeId) -> std::result::Result, PristineError> { + self.dependencies.set(self.dependencies.get() + 1); + self.inner.get_change_deps(id) + } + fn has_change_in_graph(&self, id: NodeId) -> std::result::Result { + self.inner.has_change_in_graph(id) + } + fn change_deps_indexed_count( + &self, + id: NodeId, + ) -> std::result::Result, PristineError> { + self.inner.change_deps_indexed_count(id) + } +} + +impl SpanPatchReadTxn for CountedTxn<'_> { + fn span_change_indexed(&self, change: NodeId) -> Result { + self.inner.span_change_indexed(change) + } + fn span_root(&self, inode: Inode) -> Result> { + self.inner.span_root(inode) + } + fn span_file_inode(&self, file: &ExternalFile) -> Result> { + *self + .files + .borrow_mut() + .entry((file.owner, file.ordinal)) + .or_default() += 1; + self.inner.span_file_inode(file) + } + fn span_exists(&self, key: &[u8; 32]) -> Result { + self.inner.span_exists(key) + } + fn span_edits_for_inode(&self, inode: Inode) -> Result> { + self.inner.span_edits_for_inode(inode) + } + fn span_edits_at(&self, key: &[u8; 32]) -> Result> { + self.inner.span_edits_at(key) + } +} + +struct Fixture { + _directory: tempfile::TempDir, + pristine: Pristine, + store: MemoryChangeStore, + root: Hash, + edit: Hash, + inode: Inode, +} + +impl Fixture { + fn new() -> Result { + let directory = tempfile::tempdir()?; + let pristine = Pristine::open(directory.path().join("pristine.redb"))?; + let store = MemoryChangeStore::new(); + let inode = Inode::new(42); + { + let mut txn = pristine.write_txn()?; + txn.open_or_create_view("dev")?; + txn.commit()?; + } + let put = |change: Change, roots| -> Result { + let hash = store.insert_change(change.clone())?; + let mut txn = pristine.write_txn()?; + let id = txn.register_change(&hash)?; + txn.put_change_deps(id, change.dependencies())?; + index_change(&mut txn, &store, "dev", hash, &change, &roots)?; + let mut view = txn.get_view("dev")?.context("view missing")?; + txn.put_change(&mut view, id, &hash)?; + txn.update_view(&view)?; + txn.commit()?; + Ok(hash) + }; + let mut root = Change::new( + ChangeHeader::new("root"), + vec![], + b"abcdefgh".to_vec(), + vec![], + ); + root.hashed.span = Some(SpanPatch::new( + vec![], + vec![SpanOp::Root { + path: "file.txt".into(), + ordinal: 0, + start: 0, + end: 8, + }], + )); + let root = put(root, BTreeMap::from([(0, inode)]))?; + let mut edit = Change::new( + ChangeHeader::new("three edits"), + vec![], + b"ABC".to_vec(), + vec![root], + ); + edit.hashed.span = Some(SpanPatch::new( + vec![], + (0..3) + .map(|index| SpanOp::Edit { + file: ExternalFile { + owner: root, + ordinal: 0, + }, + target: ExternalSpan { + owner: root, + start: 0, + end: 8, + }, + start: index * 2, + end: index * 2 + 1, + insert_start: index, + insert_end: index + 1, + }) + .collect(), + )); + let edit = put(edit, BTreeMap::new())?; + Ok(Self { + _directory: directory, + pristine, + store, + root, + edit, + inode, + }) + } + fn visible(&self, txn: &ReadTxn) -> Result { + let view = txn.get_view("dev")?.context("view missing")?; + Ok(EffectiveProjectionClosure::try_from_membership( + txn, + &txn.view_membership_set(&view)?, + )?) + } +} + +#[test] +fn context_reuses_verified_dependencies_and_resolutions_with_exact_counters() -> Result<()> { + let fixture = Fixture::new()?; + let txn = fixture.pristine.read_txn()?; + let visible = fixture.visible(&txn)?; + let counted = CountedTxn::new(&txn); + let context = SpanRenderContext::new(&counted, &fixture.store, &visible)?; + let metrics = context.build_metrics(); + assert_eq!(metrics.changes, 2); + assert_eq!(metrics.edits, 3); + assert_eq!(metrics.dependency_reads, 2); + assert_eq!( + counted.dependencies.get(), + 2, + "causal proof must not read DEPS again" + ); + assert_eq!( + counted + .internals + .borrow() + .values() + .copied() + .collect::>(), + [1] + ); + assert_eq!( + counted.files.borrow().values().copied().collect::>(), + [1] + ); + assert_eq!((metrics.internal_lookups, metrics.internal_hits), (1, 3)); + assert_eq!((metrics.file_lookups, metrics.file_hits), (1, 2)); + let phases = metrics.resolve_changes + + metrics.load_changes + + metrics.verify_hashes + + metrics.validate_dependencies + + metrics.assemble_edits + + metrics.causal_proof; + assert!(phases <= metrics.total); + assert_eq!(context.render(fixture.inode)?, b"AbBdCfgh"); + let without_matrix = + SpanRenderContext::with_dependency_proof_budget(&txn, &fixture.store, &visible, 0)?; + assert!(without_matrix.proof.is_none()); + assert_eq!(without_matrix.render(fixture.inode)?, b"AbBdCfgh"); + Ok(()) +} + +#[test] +fn cached_resolutions_do_not_cross_transaction_snapshots() -> Result<()> { + let fixture = Fixture::new()?; + let txn = fixture.pristine.read_txn()?; + let visible = fixture.visible(&txn)?; + let verified = crate::change::VerifiedChangeCache::new(&fixture.store, 4); + let context = SpanRenderContext::new(&txn, &verified, &visible)?; + let file = ExternalFile { + owner: fixture.root, + ordinal: 0, + }; + { + let write = fixture.pristine.write_txn()?; + { + let mut files = write.txn.open_table(FILES)?; + assert!(files.remove(&file_key(&file))?.is_some()); + } + write.commit()?; + } + assert_eq!( + context.render(fixture.inode)?, + b"AbBdCfgh", + "old snapshot stays consistent" + ); + let next_txn = fixture.pristine.read_txn()?; + let next_visible = fixture.visible(&next_txn)?; + let error = SpanRenderContext::new(&next_txn, &verified, &next_visible) + .err() + .context("new snapshot accepted missing file mapping")?; + assert!( + error.to_string().contains("span file mapping missing"), + "{error:#}" + ); + Ok(()) +} + +#[test] +fn reused_verified_payloads_do_not_hide_changed_span_index_rows() -> Result<()> { + let fixture = Fixture::new()?; + let verified = crate::change::VerifiedChangeCache::new(&fixture.store, 4); + let old_txn = fixture.pristine.read_txn()?; + let old_visible = fixture.visible(&old_txn)?; + let old_context = SpanRenderContext::new(&old_txn, &verified, &old_visible)?; + { + let write = fixture.pristine.write_txn()?; + let rows = write.span_edits_for_inode(fixture.inode)?; + assert_eq!(rows.len(), 3); + { + let mut edits = write.txn.open_multimap_table(EDITS)?; + assert!(edits.remove(&rows[0].0, &rows[0].1)?); + } + write.commit()?; + } + assert_eq!(old_context.render(fixture.inode)?, b"AbBdCfgh"); + let txn = fixture.pristine.read_txn()?; + let visible = fixture.visible(&txn)?; + let context = SpanRenderContext::new(&txn, &verified, &visible)?; + let error = context + .render(fixture.inode) + .err() + .context("changed span index was trusted")?; + assert!( + error.to_string().contains("span index differs"), + "{error:#}" + ); + Ok(()) +} + +#[test] +fn context_still_checks_dependency_index_and_inverse_id_rows() -> Result<()> { + let fixture = Fixture::new()?; + let txn = fixture.pristine.read_txn()?; + let visible = fixture.visible(&txn)?; + let mut counted = CountedTxn::new(&txn); + counted.missing_internal = Some(fixture.root); + let error = SpanRenderContext::new(&counted, &fixture.store, &visible) + .err() + .context("missing inverse ID accepted")?; + assert!( + error.to_string().contains("dependency missing"), + "{error:#}" + ); + { + let mut write = fixture.pristine.write_txn()?; + let edit_id = write.get_internal(&fixture.edit)?.context("edit missing")?; + write.put_change_deps(edit_id, &[])?; + write.commit()?; + } + let next_txn = fixture.pristine.read_txn()?; + let next_visible = fixture.visible(&next_txn)?; + let error = SpanRenderContext::new(&next_txn, &fixture.store, &next_visible) + .err() + .context("dependency mismatch accepted")?; + assert!( + error.to_string().contains("dependency index differs"), + "{error:#}" + ); + Ok(()) +} + +struct TamperedStore<'a> { + inner: &'a MemoryChangeStore, + corrupt: Hash, + missing: Hash, +} +impl ChangeStore for TamperedStore<'_> { + type Error = ::Error; + fn get_change(&self, hash: &Hash) -> std::result::Result { + if *hash == self.missing { + return self.inner.get_change(&Hash::of(b"intentionally absent")); + } + let mut change = self.inner.get_change(hash)?; + if *hash == self.corrupt { + change.hashed.header.message.push_str(" tampered"); + } + Ok(change) + } + fn get_contents( + &self, + hash_fn: F, + node: GraphNode, + bytes: &mut [u8], + ) -> std::result::Result + where + F: Fn(NodeId) -> Option, + { + self.inner.get_contents(hash_fn, node, bytes) + } + fn get_contents_ext( + &self, + node: GraphNode>, + bytes: &mut [u8], + ) -> std::result::Result { + self.inner.get_contents_ext(node, bytes) + } +} + +#[test] +fn separate_load_and_hash_phases_preserve_dependency_ordered_failures() -> Result<()> { + let fixture = Fixture::new()?; + let txn = fixture.pristine.read_txn()?; + let visible = fixture.visible(&txn)?; + let store = TamperedStore { + inner: &fixture.store, + corrupt: fixture.root, + missing: fixture.edit, + }; + let error = SpanRenderContext::new(&txn, &store, &visible) + .err() + .context("corrupt hash accepted")?; + assert!( + error.to_string().contains("change hash mismatch"), + "earlier hash error must precede later load error: {error:#}" + ); + Ok(()) +} + +#[test] +fn reused_adjacency_proof_is_exact_bounded_and_not_node_id_ordered() -> Result<()> { + let (root, child, tip) = (NodeId::new(90), NodeId::new(3), NodeId::new(7)); + let order = [root, child, tip]; + let mut dependencies = HashMap::from([(root, vec![]), (child, vec![root]), (tip, vec![child])]); + let proof = + CausalProof::build(&order, &dependencies, MAX_PROOF_BYTES)?.context("proof missing")?; + assert_eq!(proof.contains(tip, root), Some(true)); + assert_eq!(proof.contains(root, tip), Some(false)); + assert_eq!(proof.contains(tip, tip), Some(false)); + assert!(CausalProof::build(&order, &dependencies, 0)?.is_none()); + assert!(CausalProof::build(&order, &dependencies, 1)?.is_none()); + dependencies.insert(child, vec![tip]); + let error = CausalProof::build(&order, &dependencies, MAX_PROOF_BYTES) + .err() + .context("wrong topological order accepted")?; + assert!(error.to_string().contains("not dependency-first")); + dependencies.insert(child, vec![NodeId::new(999)]); + assert!(CausalProof::build(&order, &dependencies, MAX_PROOF_BYTES).is_err()); + Ok(()) +} diff --git a/atomic-repository/examples/imported_materialize_bench.rs b/atomic-repository/examples/imported_materialize_bench.rs new file mode 100644 index 00000000..de08e27d --- /dev/null +++ b/atomic-repository/examples/imported_materialize_bench.rs @@ -0,0 +1,422 @@ +//! Single-shot, correctness-checked probe of an already imported disposable repo. +//! Cache reset, import, fixture preparation and verification are not API timings. +use std::collections::BTreeMap; +use std::fs; +use std::path::{Component, Path, PathBuf}; +use std::time::Instant; + +use anyhow::{bail, ensure, Context, Result}; +use atomic_core::operation::OperationScope; +use atomic_core::pristine::span_patch_storage::SpanRenderContext; +use atomic_core::pristine::{GraphVisibilityClosure, TreeTxnT, ViewTxnT}; +use atomic_repository::Repository; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +#[derive(Deserialize)] +struct Manifest { + git_tip: String, + files: Vec, +} + +#[derive(Deserialize)] +struct ExpectedFile { + path: String, + mode: String, + sha256: String, +} + +#[derive(Serialize)] +struct Report { + schema: u32, + git_tip: String, + backend: String, + case: String, + debug_build: bool, + total_ns: u128, + phases_ns: BTreeMap, + span_context_phases_ns: BTreeMap, + span_context_counts: BTreeMap, + verified_files: usize, + rendered_bytes: usize, + files_written: Option, + files_skipped: Option, + bytes_written: Option, + head_verified: bool, +} + +fn sha256(bytes: &[u8]) -> String { + format!("{:x}", Sha256::digest(bytes)) +} + +fn verify_files(root: &Path, files: &[ExpectedFile]) -> Result<()> { + for file in files { + let path = root.join(&file.path); + let metadata = fs::symlink_metadata(&path)?; + let bytes = if file.mode == "120000" { + ensure!(metadata.is_symlink(), "expected symlink: {}", file.path); + #[cfg(unix)] + { + use std::os::unix::ffi::OsStrExt; + fs::read_link(path)?.as_os_str().as_bytes().to_vec() + } + #[cfg(not(unix))] + bail!("symlink fixture requires Unix"); + } else { + ensure!(metadata.is_file(), "expected regular file: {}", file.path); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + ensure!( + (metadata.permissions().mode() & 0o111 != 0) == (file.mode == "100755"), + "executable mode differs: {}", + file.path + ); + } + fs::read(path)? + }; + ensure!(sha256(&bytes) == file.sha256, "bytes differ: {}", file.path); + } + Ok(()) +} + +fn run( + repo_path: &Path, + manifest_path: &Path, + output: &Path, + backend: &str, + case: &str, + allow_debug: bool, +) -> Result<()> { + ensure!(matches!(backend, "graph" | "span"), "invalid backend"); + ensure!( + matches!(case, "render" | "materialize_missing"), + "invalid case" + ); + ensure!( + !cfg!(debug_assertions) || allow_debug, + "use a release build" + ); + ensure!(!output.exists(), "refusing to overwrite report"); + let manifest: Manifest = serde_json::from_slice(&fs::read(manifest_path)?)?; + ensure!(!manifest.files.is_empty(), "empty fixture"); + let mut names = std::collections::HashSet::new(); + for file in &manifest.files { + ensure!( + !file.path.is_empty() + && Path::new(&file.path) + .components() + .all(|part| matches!(part, Component::Normal(_))), + "unsafe path" + ); + ensure!(names.insert(file.path.clone()), "duplicate path"); + ensure!( + matches!(file.mode.as_str(), "100644" | "100755" | "120000"), + "unsupported Git mode" + ); + } + let mut report = Report { + schema: 1, + git_tip: manifest.git_tip.clone(), + backend: backend.into(), + case: case.into(), + debug_build: cfg!(debug_assertions), + total_ns: 0, + phases_ns: BTreeMap::new(), + span_context_phases_ns: BTreeMap::new(), + span_context_counts: BTreeMap::new(), + verified_files: manifest.files.len(), + rendered_bytes: 0, + files_written: None, + files_skipped: None, + bytes_written: None, + head_verified: false, + }; + let mut rendered = Vec::new(); + let started = Instant::now(); + let phase = Instant::now(); + let repo = Repository::open(repo_path)?; + let wc = repo.require_working_copy_id()?; + report + .phases_ns + .insert("repository_open".into(), phase.elapsed().as_nanos()); + if case == "render" { + let phase = Instant::now(); + let txn = repo.pristine().read_txn()?; + let view_name = repo.desired_view_name(wc)?; + let view = txn.get_view(&view_name)?.context("missing imported view")?; + let visibility = + GraphVisibilityClosure::try_from_membership(&txn, &txn.view_membership_set(&view)?)?; + let files = manifest + .files + .iter() + .map(|file| { + let inode = txn + .get_inode(&file.path)? + .context("missing imported inode")?; + let position = txn + .inode_position(inode)? + .context("missing imported position")?; + Ok((inode, position)) + }) + .collect::>>()?; + report + .phases_ns + .insert("read_setup".into(), phase.elapsed().as_nanos()); + let phase = Instant::now(); + let context = if backend == "span" { + Some(SpanRenderContext::new( + &txn, + repo.change_store(), + &visibility, + )?) + } else { + None + }; + report + .phases_ns + .insert("span_context".into(), phase.elapsed().as_nanos()); + if let Some(context) = &context { + let metrics = context.build_metrics(); + report.span_context_phases_ns = [ + ("resolve_changes", metrics.resolve_changes), + ("load_changes", metrics.load_changes), + ("verify_hashes", metrics.verify_hashes), + ("validate_dependencies", metrics.validate_dependencies), + ("assemble_edits", metrics.assemble_edits), + ("causal_proof", metrics.causal_proof), + ("total", metrics.total), + ] + .into_iter() + .map(|(name, duration)| (name.to_string(), duration.as_nanos())) + .collect(); + report.span_context_counts = [ + ("pipeline_version", usize::from(metrics.pipeline_version)), + ("changes", metrics.changes), + ("edits", metrics.edits), + ("dependency_reads", metrics.dependency_reads), + ("internal_lookups", metrics.internal_lookups), + ("internal_hits", metrics.internal_hits), + ("file_lookups", metrics.file_lookups), + ("file_hits", metrics.file_hits), + ] + .into_iter() + .map(|(name, count)| (name.to_string(), count)) + .collect(); + } + let phase = Instant::now(); + for (inode, position) in files { + let bytes = if let Some(context) = &context { + // Every selected Git-imported file must have a valid span root; + // never silently fall back if its index is absent or corrupt. + context.render(inode)? + } else { + let (bytes, conflicts, _) = + atomic_core::output::repo::output_file_to_buffer_with_options( + &txn, + repo.change_store(), + position, + atomic_core::output::repo::FileOutputOptions::new(), + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(visibility.clone()), + )?; + ensure!(conflicts.is_empty(), "unexpected fixture conflict"); + bytes + }; + report.rendered_bytes += bytes.len(); + rendered.push(bytes); + } + report + .phases_ns + .insert("render".into(), phase.elapsed().as_nanos()); + } else { + let phase = Instant::now(); + let result = repo.materialize(wc)?; + ensure!( + !result.has_conflicts() && result.files_truncated == 0, + "conflict or truncation" + ); + report.files_written = Some(result.files_written); + report.files_skipped = Some(result.files_skipped); + report.bytes_written = Some(result.bytes_written); + report + .phases_ns + .insert("materialize".into(), phase.elapsed().as_nanos()); + } + let phase = Instant::now(); + drop(repo); + report + .phases_ns + .insert("repository_close".into(), phase.elapsed().as_nanos()); + report.total_ns = started.elapsed().as_nanos(); + + // All equality and journal checks occur after timing and repository close. + if case == "render" { + ensure!( + rendered.len() == manifest.files.len(), + "render count differs" + ); + for (bytes, file) in rendered.iter().zip(&manifest.files) { + ensure!( + sha256(bytes) == file.sha256, + "render differs: {}", + file.path + ); + } + } + verify_files(repo_path, &manifest.files)?; + let repo = Repository::open(repo_path)?; + let log = repo.operation_log(OperationScope::WorkingCopy(wc), Some(1), false)?; + ensure!( + matches!( + log.head_state, + atomic_repository::repository::OperationHeadState::Single(_) + ) && log.entries.iter().filter(|entry| entry.is_head).count() == 1 + && log + .entries + .iter() + .filter(|entry| entry.is_head) + .all(|entry| entry.verification + == atomic_repository::repository::OperationVerificationState::Verified), + "operation head is incomplete or divergent" + ); + report.head_verified = true; + let output = fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(output)?; + serde_json::to_writer_pretty(output, &report)?; + Ok(()) +} + +fn main() -> Result<()> { + let mut args = std::env::args().skip(1); + let mut repo = None; + let mut manifest = None; + let mut output = None; + let mut backend = None; + let mut case = None; + let mut allow_debug = false; + while let Some(arg) = args.next() { + match arg.as_str() { + "--repo" => repo = Some(PathBuf::from(args.next().context("missing repo")?)), + "--manifest" => { + manifest = Some(PathBuf::from(args.next().context("missing manifest")?)) + } + "--output" => output = Some(PathBuf::from(args.next().context("missing output")?)), + "--backend" => backend = args.next(), + "--case" => case = args.next(), + "--allow-debug" => allow_debug = true, + _ => bail!("unknown argument {arg}"), + } + } + run( + &repo.context("--repo required")?, + &manifest.context("--manifest required")?, + &output.context("--output required")?, + &backend.context("--backend required")?, + &case.context("--case required")?, + allow_debug, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use atomic_core::change::ChangeHeader; + use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; + use atomic_repository::RecordOptions; + + #[test] + fn imported_probe_format_and_verification_smoke() { + for backend in ["graph", "span"] { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("repo"); + let repo = Repository::init(&path).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + if backend == "span" { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + for name in ["file.txt", "empty.txt"] { + fs::write( + path.join(name), + if name == "file.txt" { + b"hello\n".as_slice() + } else { + b"" + }, + ) + .unwrap(); + repo.add(wc, name, Default::default()).unwrap(); + } + repo.record( + wc, + ChangeHeader::new("smoke"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + .sync_vault(false) + .enrich_kg(false), + ) + .unwrap(); + drop(repo); + let manifest = dir.path().join("manifest.json"); + fs::write( + &manifest, + serde_json::to_vec( + &serde_json::json!({"git_tip":"synthetic smoke only", "files":[ + {"path":"file.txt","mode":"100644","sha256":sha256(b"hello\n")}, + {"path":"empty.txt","mode":"100644","sha256":sha256(b"")} + ]}), + ) + .unwrap(), + ) + .unwrap(); + run( + &path, + &manifest, + &dir.path().join("render.json"), + backend, + "render", + true, + ) + .unwrap(); + let rendered: serde_json::Value = + serde_json::from_slice(&fs::read(dir.path().join("render.json")).unwrap()).unwrap(); + if backend == "span" { + assert_eq!( + rendered["span_context_phases_ns"] + .as_object() + .unwrap() + .len(), + 7 + ); + assert!(rendered["span_context_counts"]["changes"].as_u64().unwrap() > 0); + } else { + assert!(rendered["span_context_phases_ns"] + .as_object() + .unwrap() + .is_empty()); + } + fs::remove_file(path.join("file.txt")).unwrap(); + fs::remove_file(path.join("empty.txt")).unwrap(); + let output = dir.path().join("materialize.json"); + run( + &path, + &manifest, + &output, + backend, + "materialize_missing", + true, + ) + .unwrap(); + let report: serde_json::Value = + serde_json::from_slice(&fs::read(output).unwrap()).unwrap(); + assert_eq!(report["verified_files"], 2); + assert_eq!(report["head_verified"], true); + } + } +} diff --git a/atomic-repository/examples/materialize_bench.rs b/atomic-repository/examples/materialize_bench.rs new file mode 100644 index 00000000..39253d29 --- /dev/null +++ b/atomic-repository/examples/materialize_bench.rs @@ -0,0 +1,551 @@ +//! Correctness-checked native graph/span materialization baseline. +//! Run optimized builds through tools/materialize-bench/run.py; setup and +//! verification are outside sample timing. This is not a cold-OS-cache test. +use std::collections::BTreeMap; +use std::fs; +use std::path::{Path, PathBuf}; +use std::time::Instant; + +use anyhow::{bail, ensure, Context, Result}; +use atomic_core::change::ChangeHeader; +use atomic_core::operation::OperationScope; +use atomic_core::pristine::span_patch_storage::SpanRenderContext; +use atomic_core::pristine::{ + CapabilityMutTxnT, GraphVisibilityClosure, MutTxnT, TreeTxnT, ViewTxnT, SPAN_RANGE_CAPABILITY, +}; +use atomic_repository::{RecordOptions, Repository}; +use serde::{Deserialize, Serialize}; + +const CASES: &[&str] = &[ + "render_session", + "render_reopened", + "materialize_noop", + "materialize_missing", + "switch_one_forward", + "switch_one_back", + "switch_many_forward", + "switch_many_back", +]; + +#[derive(Clone, Debug, Serialize, Deserialize)] +struct Config { + output: PathBuf, + files: usize, + lines: usize, + edits: usize, + iterations: usize, + warmups: usize, + allow_debug: bool, + keep_fixtures: bool, +} + +impl Config { + fn parse() -> Result { + let mut config = Self { + output: "materialize-bench.json".into(), + files: 24, + lines: 128, + edits: 24, + iterations: 5, + warmups: 1, + allow_debug: false, + keep_fixtures: false, + }; + let mut args = std::env::args().skip(1); + while let Some(arg) = args.next() { + match arg.as_str() { + "--allow-debug" => config.allow_debug = true, + "--keep-fixtures" => config.keep_fixtures = true, + "--output" => config.output = args.next().context("--output needs a path")?.into(), + "--files" => { + config.files = args.next().context("--files needs a value")?.parse()? + } + "--lines" => { + config.lines = args.next().context("--lines needs a value")?.parse()? + } + "--edits" => { + config.edits = args.next().context("--edits needs a value")?.parse()? + } + "--iterations" => { + config.iterations = + args.next().context("--iterations needs a value")?.parse()? + } + "--warmups" => { + config.warmups = args.next().context("--warmups needs a value")?.parse()? + } + _ => bail!("unknown argument: {arg}"), + } + } + config.validate()?; + Ok(config) + } + + fn validate(&self) -> Result<()> { + ensure!( + self.files > 0 && self.lines >= 2 && self.iterations > 0, + "files and iterations must be positive; lines must be at least two" + ); + ensure!( + self.files <= 1024 && self.lines <= 100_000 && self.edits <= 10_000, + "fixture exceeds benchmark safety bounds" + ); + ensure!( + self.iterations <= 1000 + && self.warmups <= 1000 + && self + .files + .checked_mul(self.lines) + .is_some_and(|n| n <= 2_000_000), + "sample count or total fixture lines exceed safety bounds" + ); + ensure!( + !cfg!(debug_assertions) || self.allow_debug, + "use a --release build (or --allow-debug for correctness smoke tests only)" + ); + ensure!( + !self.output.exists(), + "refusing to overwrite an existing output report" + ); + Ok(()) + } +} + +#[derive(Clone)] +struct Golden { + path: String, + content: Vec, + mode: u32, +} + +fn file_mode(path: &Path) -> Result { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + Ok(fs::metadata(path)?.permissions().mode() & 0o7777) + } + #[cfg(not(unix))] + { + Ok(u32::from(fs::metadata(path)?.permissions().readonly())) + } +} + +fn record(repo: &Repository, message: &str, tick: &mut i64) -> Result<()> { + let mut header = ChangeHeader::new(message); + header.timestamp = chrono::DateTime::from_timestamp(1_600_000_000 + *tick, 0).unwrap(); + *tick += 1; + repo.record( + repo.require_working_copy_id()?, + header, + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + .sync_vault(false) + .enrich_kg(false), + )?; + Ok(()) +} + +struct Fixture { + directory: tempfile::TempDir, + repo: Option, + backend: &'static str, + golden: BTreeMap>, + setup_ns: u128, +} + +impl Fixture { + fn new(parent: &Path, config: &Config, span: bool) -> Result { + let started = Instant::now(); + let directory = tempfile::Builder::new() + .prefix(if span { "span-" } else { "graph-" }) + .tempdir_in(parent)?; + let root = directory.path(); + let mut repo = Repository::init(root)?; + if span { + let mut txn = repo.pristine().write_txn()?; + txn.put_required_capability(SPAN_RANGE_CAPABILITY)?; + txn.commit()?; + } + fs::create_dir(root.join("files"))?; + let mut contents: Vec> = (0..config.files) + .map(|file| { + (0..config.lines) + .map(|line| format!("file_{file:04} line_{line:06} value_00000000\n")) + .collect() + }) + .collect(); + for (file, lines) in contents.iter().enumerate() { + fs::write( + root.join(format!("files/file_{file:04}.txt")), + lines.concat(), + )?; + } + fs::write(root.join("empty.txt"), [])?; + let wc = repo.require_working_copy_id()?; + repo.add(wc, "files", Default::default())?; + repo.add(wc, "empty.txt", Default::default())?; + let mut tick = 0; + record(&repo, "synthetic baseline", &mut tick)?; + for edit in 0..config.edits { + let line = (edit * 17) % config.lines; + contents[0][line] = format!("file_0000 line_{line:06} value_{:08}\n", edit + 1); + fs::write(root.join("files/file_0000.txt"), contents[0].concat())?; + record(&repo, "synthetic hot-file edit", &mut tick)?; + } + let mut golden = BTreeMap::new(); + let capture = |content: &[Vec]| -> Result> { + let mut files = Vec::new(); + for (file, lines) in content.iter().enumerate() { + let path = format!("files/file_{file:04}.txt"); + files.push(Golden { + mode: file_mode(&root.join(&path))?, + path, + content: lines.concat().into_bytes(), + }); + } + files.push(Golden { + path: "empty.txt".into(), + content: Vec::new(), + mode: file_mode(&root.join("empty.txt"))?, + }); + Ok(files) + }; + golden.insert("dev".into(), capture(&contents)?); + repo.create_view_from("bench-near", "dev")?; + repo.create_view_from("bench-far", "dev")?; + repo.switch_view(wc, "bench-near")?; + let mut near = contents.clone(); + near[0][0] = "near view: one changed line\n".into(); + fs::write(root.join("files/file_0000.txt"), near[0].concat())?; + record(&repo, "synthetic near view", &mut tick)?; + golden.insert("bench-near".into(), capture(&near)?); + repo.switch_view(wc, "dev")?; + repo.switch_view(wc, "bench-far")?; + let mut far = contents; + for (file, lines) in far.iter_mut().enumerate() { + lines[0] = format!("far view: file {file:04} changed\n"); + fs::write( + root.join(format!("files/file_{file:04}.txt")), + lines.concat(), + )?; + } + record(&repo, "synthetic many-file view", &mut tick)?; + golden.insert("bench-far".into(), capture(&far)?); + repo.switch_view(wc, "dev")?; + repo.materialize(wc)?; + let healthy = repo.verify_native_derived_indexes()?; + ensure!( + healthy.is_healthy(), + "fixture indexes unhealthy: {:?}", + healthy.problems + ); + let fixture = Self { + directory, + repo: Some(repo), + backend: if span { "span" } else { "graph" }, + golden, + setup_ns: started.elapsed().as_nanos(), + }; + fixture.verify("dev")?; + Ok(fixture) + } + + fn verify(&self, view: &str) -> Result<()> { + ensure!( + self.repo.as_ref().unwrap().current_view() == view, + "wrong current view" + ); + for file in &self.golden[view] { + let path = self.directory.path().join(&file.path); + ensure!( + fs::symlink_metadata(&path)?.file_type().is_file(), + "wrong kind: {}", + file.path + ); + ensure!( + fs::read(&path)? == file.content, + "wrong bytes: {}", + file.path + ); + ensure!(file_mode(&path)? == file.mode, "wrong mode: {}", file.path); + } + Ok(()) + } +} + +#[derive(Debug, Serialize)] +struct Sample { + backend: String, + case: String, + iteration: usize, + total_ns: u128, + phases_ns: BTreeMap, + files_written: Option, + files_skipped: Option, + bytes_written: Option, + vertices_processed: Option, + edges_traversed: Option, + verified_files: usize, + output_bytes: usize, +} + +type RenderProbe = (BTreeMap, Vec>); + +fn render_probe(repo: &Repository, golden: &[Golden], span: bool) -> Result { + let mut phases = BTreeMap::new(); + let started = Instant::now(); + let txn = repo.pristine().read_txn()?; + let view = txn.get_view("dev")?.context("missing base view")?; + let visibility = + GraphVisibilityClosure::try_from_membership(&txn, &txn.view_membership_set(&view)?)?; + let files = golden + .iter() + .map(|file| { + let inode = txn + .get_inode(&file.path)? + .context("missing fixture inode")?; + let position = txn + .inode_position(inode)? + .context("missing fixture position")?; + Ok((inode, position)) + }) + .collect::>>()?; + phases.insert("read_setup".into(), started.elapsed().as_nanos()); + let started = Instant::now(); + let context = if span { + Some(SpanRenderContext::new( + &txn, + repo.change_store(), + &visibility, + )?) + } else { + None + }; + phases.insert("span_context".into(), started.elapsed().as_nanos()); + let started = Instant::now(); + let mut rendered = Vec::new(); + for (inode, position) in files { + let bytes = if let Some(context) = &context { + context.render(inode)? + } else { + let (bytes, conflicts, _) = + atomic_core::output::repo::output_file_to_buffer_with_options( + &txn, + repo.change_store(), + position, + atomic_core::output::repo::FileOutputOptions::new(), + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(visibility.clone()), + )?; + ensure!(conflicts.is_empty(), "unexpected synthetic graph conflict"); + bytes + }; + rendered.push(bytes); + } + phases.insert("render".into(), started.elapsed().as_nanos()); + Ok((phases, rendered)) +} + +fn sample(fixture: &mut Fixture, case: &str, iteration: usize) -> Result { + let mut phases = BTreeMap::new(); + let mut written = None; + let mut skipped = None; + let mut bytes_written = None; + let mut vertices_processed = None; + let mut edges_traversed = None; + let mut rendered = None; + if case == "materialize_missing" { + // Remove only the benchmark's own generated regular files, never a + // directory tree or a caller-provided repository. + for file in &fixture.golden["dev"] { + fs::remove_file(fixture.directory.path().join(&file.path))?; + } + } + if case == "render_reopened" { + drop(fixture.repo.take()); + } + let started = Instant::now(); + if case == "render_reopened" { + let opened = Instant::now(); + fixture.repo = Some(Repository::open(fixture.directory.path())?); + phases.insert("repository_open".into(), opened.elapsed().as_nanos()); + } + let repo = fixture.repo.as_mut().unwrap(); + let wc = repo.require_working_copy_id()?; + let target = match case { + "switch_one_forward" => "bench-near", + "switch_many_forward" => "bench-far", + _ => "dev", + }; + if case.starts_with("render_") { + let (measured, content) = + render_probe(repo, &fixture.golden["dev"], fixture.backend == "span")?; + phases.extend(measured); + rendered = Some(content); + } else { + let result = if case.starts_with("switch_") { + repo.switch_view(wc, target)? + } else { + repo.materialize(wc)? + }; + written = Some(result.files_written); + skipped = Some(result.files_skipped); + bytes_written = Some(result.bytes_written); + vertices_processed = Some(result.vertices_processed); + edges_traversed = Some(result.edges_traversed); + ensure!( + !result.has_conflicts() && result.files_truncated == 0, + "unexpected materialization conflict/truncation" + ); + } + let total_ns = started.elapsed().as_nanos(); + // Filesystem correctness and journal verification are never timed. + if let Some(rendered) = rendered { + for (bytes, file) in rendered.iter().zip(&fixture.golden["dev"]) { + ensure!(*bytes == file.content, "render differs: {}", file.path); + } + } + fixture.verify(target)?; + let log = fixture.repo.as_ref().unwrap().operation_log( + OperationScope::WorkingCopy(wc), + Some(1), + false, + )?; + ensure!( + matches!( + log.head_state, + atomic_repository::repository::OperationHeadState::Single(_) + ) && log.entries.iter().filter(|entry| entry.is_head).count() == 1 + && log + .entries + .iter() + .filter(|entry| entry.is_head) + .all(|entry| entry.verification + == atomic_repository::repository::OperationVerificationState::Verified), + "incomplete benchmark operation" + ); + Ok(Sample { + backend: fixture.backend.into(), + case: case.into(), + iteration, + total_ns, + phases_ns: phases, + files_written: written, + files_skipped: skipped, + bytes_written, + vertices_processed, + edges_traversed, + verified_files: fixture.golden[target].len(), + output_bytes: fixture.golden[target] + .iter() + .map(|file| file.content.len()) + .sum(), + }) +} + +fn run(config: Config) -> Result<()> { + config.validate()?; + let parent = config + .output + .parent() + .filter(|p| !p.as_os_str().is_empty()) + .unwrap_or(Path::new(".")); + fs::create_dir_all(parent)?; + let binary = std::env::current_exe()?; + let binary_hash = blake3::hash(&fs::read(&binary)?).to_hex().to_string(); + let mut fixtures = [ + Fixture::new(parent, &config, false)?, + Fixture::new(parent, &config, true)?, + ]; + for view in ["dev", "bench-near", "bench-far"] { + let left = &fixtures[0].golden[view]; + let right = &fixtures[1].golden[view]; + ensure!( + left.iter() + .zip(right) + .all(|(a, b)| a.path == b.path && a.content == b.content && a.mode == b.mode), + "graph/span fixture mismatch for {view}" + ); + } + let mut samples = Vec::new(); + for iteration in 0..config.warmups + config.iterations { + for case in CASES { + for offset in 0..2 { + let index = (iteration + offset) % 2; + let measured = sample( + &mut fixtures[index], + case, + iteration.saturating_sub(config.warmups), + ) + .with_context(|| { + format!("{} {case}, iteration {iteration}", fixtures[index].backend) + })?; + if iteration >= config.warmups { + samples.push(measured); + } + } + } + } + ensure!( + blake3::hash(&fs::read(&binary)?).to_hex().as_str() == binary_hash, + "benchmark binary changed during run" + ); + let setup: Vec<_> = fixtures.iter().map(|f| serde_json::json!({"backend":f.backend,"setup_ns":f.setup_ns, + "database_bytes_after_samples":fs::metadata(f.directory.path().join(".atomic/atomic.redb")).map(|m|m.len()).ok()})).collect(); + let mut retained = Vec::new(); + for fixture in fixtures { + drop(fixture.repo); + if config.keep_fixtures { + retained.push(fixture.directory.keep()); + } + } + let report = serde_json::json!({"schema":1,"config":config,"binary_blake3":binary_hash, + "debug_build":cfg!(debug_assertions),"platform":{"os":std::env::consts::OS,"arch":std::env::consts::ARCH}, + "fixture_kind":"synthetic native histories; graph versus opt-in span capability; no Git/shim/vault enrichment", + "cache_policy":"session and reopened Repository handles; OS caches uncontrolled (not cold-cache claims)", + "setup":setup,"retained_fixtures":retained,"samples":samples}); + let mut output = fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&config.output)?; + serde_json::to_writer_pretty(&mut output, &report)?; + println!( + "verified {} paired samples; report {}", + config.iterations * CASES.len() * 2, + config.output.display() + ); + Ok(()) +} + +fn main() -> Result<()> { + run(Config::parse()?) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn paired_benchmark_smoke_checks_every_case() { + let dir = tempfile::tempdir().unwrap(); + let output = dir.path().join("report.json"); + run(Config { + output: output.clone(), + files: 2, + lines: 8, + edits: 2, + iterations: 1, + warmups: 0, + allow_debug: true, + keep_fixtures: false, + }) + .unwrap(); + let report: serde_json::Value = serde_json::from_slice(&fs::read(output).unwrap()).unwrap(); + assert_eq!(report["samples"].as_array().unwrap().len(), CASES.len() * 2); + assert!(report["samples"] + .as_array() + .unwrap() + .iter() + .all(|s| s["verified_files"] == 3)); + } +} diff --git a/atomic-repository/src/changestore/mod.rs b/atomic-repository/src/changestore/mod.rs index 4f0e677f..b3d412d0 100644 --- a/atomic-repository/src/changestore/mod.rs +++ b/atomic-repository/src/changestore/mod.rs @@ -74,7 +74,7 @@ use std::io::{BufReader, BufWriter}; use std::path::{Path, PathBuf}; use std::sync::RwLock; -use atomic_core::change::Change; +use atomic_core::change::{Change, VerifiedChange}; use atomic_core::types::{Base32, Hash}; // Re-export iterator types used by the public API @@ -445,6 +445,36 @@ impl ChangeStore { Ok(change) } + /// Read the current source even on an operation-local cache hit. Identical + /// bytes reuse the immutable checked decode; changed notes, corruption, + /// replacement and deletion cannot hide behind the ordinary decoded LRU. + pub(crate) fn load_verified_change( + &self, + hash: &Hash, + cached: Option<&VerifiedChange>, + ) -> ChangeStoreResult { + let bytes = fs::read(self.change_path(hash)).map_err(|error| { + if error.kind() == std::io::ErrorKind::NotFound { + ChangeStoreError::NotFound { + hash: hash.to_base32(), + } + } else { + ChangeStoreError::Io(error) + } + })?; + let verified = match cached.filter(|cached| cached.matches_source_bytes(&bytes)) { + Some(cached) => cached.clone(), + None => VerifiedChange::from_bytes(&bytes)?, + }; + if verified.hash() != *hash { + return Err(ChangeStoreError::HashMismatch { + expected: hash.to_base32(), + computed: verified.hash().to_base32(), + }); + } + Ok(verified) + } + /// Hold `bytes` as `hash`'s content at `start..start + bytes.len()`. pub fn hold_span(&self, hash: Hash, start: u64, bytes: Vec) { if let Ok(mut spans) = self.spans.write() { diff --git a/atomic-repository/src/changestore/tests.rs b/atomic-repository/src/changestore/tests.rs index a6a67f75..f65d2c7f 100644 --- a/atomic-repository/src/changestore/tests.rs +++ b/atomic-repository/src/changestore/tests.rs @@ -43,6 +43,72 @@ fn create_test_change_with_content(message: &str, content: &[u8]) -> Change { // LRU Cache Tests +#[test] +fn verified_operation_cache_reuses_decode_but_rechecks_actual_files() { + use atomic_core::change::{ChangeStore as CoreChangeStore, VerifiedChangeCache}; + let (store, _temp) = create_test_store(); + let change = create_test_change_with_content("verified", b"original bytes"); + let hash = store.save_change(&change).unwrap(); + let path = store.change_path(&hash); + let original = fs::read(&path).unwrap(); + let cache = VerifiedChangeCache::new(&store, 4); + let first = cache.get_verified_change(&hash).unwrap(); + let second = cache.get_verified_change(&hash).unwrap(); + assert!( + std::ptr::eq(first.change(), second.change()), + "identical source should reuse its immutable decode" + ); + + let other = store + .save_change(&create_test_change_with_content("other", b"replacement")) + .unwrap(); + fs::write(&path, fs::read(store.change_path(&other)).unwrap()).unwrap(); + assert!( + cache.get_verified_change(&hash).is_err(), + "valid bytes for another identity must refuse" + ); + fs::write(&path, &original).unwrap(); + cache.get_verified_change(&hash).unwrap(); + + let mut corrupt = original.clone(); + let last = corrupt.len() - 1; + corrupt[last] ^= 1; + fs::write(&path, &corrupt).unwrap(); + assert!(cache.get_verified_change(&hash).is_err()); + assert!( + store.load_change(&hash).is_ok(), + "ordinary LRU is deliberately stale in this fixture" + ); + fs::write(&path, &original).unwrap(); + cache.get_verified_change(&hash).unwrap(); + fs::remove_file(&path).unwrap(); + assert!( + cache.get_verified_change(&hash).is_err(), + "deletion must not hide behind a cached witness" + ); +} + +#[test] +fn verified_operation_cache_reloads_changed_unhashed_notes_without_rewriting_identity() { + use atomic_core::change::{ChangeStore as CoreChangeStore, VerifiedChangeCache}; + let (store, _temp) = create_test_store(); + let mut change = create_test_change_with_content("same author", b"authored bytes"); + change.unhashed = Some(serde_json::json!({"note":"old"})); + let hash = store.save_change(&change).unwrap(); + let cache = VerifiedChangeCache::new(&store, 4); + let first = cache.get_verified_change(&hash).unwrap(); + change.unhashed = Some(serde_json::json!({"note":"new"})); + let mut bytes = Vec::new(); + assert_eq!(change.serialize(&mut bytes).unwrap(), hash); + fs::write(store.change_path(&hash), &bytes).unwrap(); + let updated = cache.get_verified_change(&hash).unwrap(); + assert_eq!(updated.hash(), hash); + assert_eq!(updated.change().unhashed, change.unhashed); + assert!(!std::ptr::eq(first.change(), updated.change())); + let reused = cache.get_verified_change(&hash).unwrap(); + assert!(std::ptr::eq(updated.change(), reused.change())); +} + #[test] fn test_lru_cache_basic_operations() { let mut cache: LruCache = LruCache::new(3); diff --git a/atomic-repository/src/changestore/trait_impl.rs b/atomic-repository/src/changestore/trait_impl.rs index 9d2a4778..f7d9e69d 100644 --- a/atomic-repository/src/changestore/trait_impl.rs +++ b/atomic-repository/src/changestore/trait_impl.rs @@ -3,8 +3,8 @@ //! This bridges the repository's filesystem-backed `ChangeStore` with the //! core library's content retrieval functions (like `retrieve_content`). -use atomic_core::change::ChangeHeader; use atomic_core::change::ChangeStore as ChangeStoreTrait; +use atomic_core::change::{ChangeHeader, VerifiedChange, VerifiedLoadError}; use atomic_core::types::{GraphNode, Hash, NodeId}; use super::{ChangeStore, ChangeStoreError}; @@ -16,6 +16,23 @@ impl ChangeStoreTrait for ChangeStore { self.load_change(hash) } + fn get_verified_change( + &self, + hash: &Hash, + ) -> Result> { + self.load_verified_change(hash, None) + .map_err(VerifiedLoadError::Store) + } + + fn revalidate_verified_change( + &self, + hash: &Hash, + cached: &VerifiedChange, + ) -> Result> { + self.load_verified_change(hash, Some(cached)) + .map_err(VerifiedLoadError::Store) + } + fn has_change(&self, hash: &Hash) -> bool { ChangeStore::has_change(self, hash) } diff --git a/atomic-repository/src/repository/materialize.rs b/atomic-repository/src/repository/materialize.rs index b4ca7161..9fb37aa9 100644 --- a/atomic-repository/src/repository/materialize.rs +++ b/atomic-repository/src/repository/materialize.rs @@ -20,6 +20,10 @@ struct MaterializationPlanEntry { expected_new: EffectValue, } +type MaterializationVerifiedChanges<'a> = + atomic_core::change::VerifiedChangeCache<'a, crate::changestore::ChangeStore>; +const MAX_MATERIALIZATION_VERIFIED_CHANGES: usize = 4096; + #[cfg(unix)] fn materialized_directory_mode() -> u32 { 0o755 @@ -627,7 +631,25 @@ impl Repository { path: &str, view_name: &str, ) -> Result>, RepositoryError> { - let normalized = normalize_path(Path::new(path)); + let mut bytes = None; + self.for_each_switch_target_file(&[path.to_string()], view_name, |_, rendered| { + bytes = rendered; + Ok(()) + })?; + Ok(bytes) + } + + /// Stream lease bytes from one read snapshot, sharing verified span history + /// across ordinary files. Nothing survives this call's transaction or gets + /// reused by execution/recovery; the visitor must not mutate repository state. + pub(super) fn for_each_switch_target_file( + &self, + paths: &[String], + view_name: &str, + mut visit: impl FnMut(&str, Option>) -> Result<(), RepositoryError>, + ) -> Result<(), RepositoryError> { + use atomic_core::pristine::span_patch_storage::SpanRenderContext; + let txn = self .pristine .read_txn() @@ -659,74 +681,102 @@ impl Repository { let inode_graph_table = txn .open_inode_graph_table() .map_err(|error| RepositoryError::Database(error.to_string()))?; - if projection.name_conflicts.contains_key(&normalized) { - let selected = std::collections::HashSet::from([normalized.clone()]); - let prepared = prepare_name_conflict_output( + let mut span_context = None; + for path in paths { + let normalized = normalize_path(Path::new(path)); + if projection.name_conflicts.contains_key(&normalized) { + let selected = std::collections::HashSet::from([normalized.clone()]); + let prepared = prepare_name_conflict_output( + &txn, + &self.change_store, + &inode_graph_table, + &visibility, + &external_hashes, + &projection.name_conflicts, + Some(&selected), + )?; + let bytes = match prepared.files.as_slice() { + [entry] => Ok(entry.bytes().map(ToOwned::to_owned)), + [] if prepared.directories == [normalized] => Ok(None), + _ => Err(RepositoryError::InvalidOperation { + message: format!( + "switch conflict renderer produced an ambiguous result for '{path}'" + ), + }), + }?; + visit(path, bytes)?; + continue; + } + + let Some(item) = projection + .present + .get(&normalized) + .filter(|item| !item.is_directory) + else { + visit(path, None)?; + continue; + }; + let materialization = atomic_core::output::project_inode_attributes( + &txn, + item.position, + visibility.attribute_visibility(), + ) + .map_err(|error| RepositoryError::Database(error.to_string()))?; + if materialization.is_conflicted() { + return Err(RepositoryError::Output(format!( + "cannot switch '{}' with conflicting inode attributes", + path + ))); + } + let kind = materialization.materialization.kind; + // The lease must hash the same semantic rendering used by materialize. + // The fast content reader can retain forks resolved by that renderer. + let repository_bytes = if super::content::has_visible_span_root( &txn, &self.change_store, - &inode_graph_table, + item.inode, &visibility, - &external_hashes, - &projection.name_conflicts, - Some(&selected), - )?; - return match prepared.files.as_slice() { - [entry] => Ok(entry.bytes().map(ToOwned::to_owned)), - [] if prepared.directories == [normalized] => Ok(None), - _ => Err(RepositoryError::InvalidOperation { - message: format!( - "switch conflict renderer produced an ambiguous result for '{path}'" - ), - }), + )? { + if span_context.is_none() { + span_context = Some( + SpanRenderContext::new(&txn, &self.change_store, &visibility) + .map_err(|error| RepositoryError::Database(error.to_string()))?, + ); + } + span_context + .as_ref() + .unwrap() + .render(item.inode) + .map_err(|error| RepositoryError::Output(format!("{path}: span: {error}")))? + } else { + render_name_conflict_side( + &txn, + &self.change_store, + &inode_graph_table, + &visibility, + &external_hashes, + path, + item.inode, + item.position, + ) + .map_err(RepositoryError::Output)? }; + if kind != atomic_core::change::InodeKind::Regular + || first_conflict_marker_line(&repository_bytes).is_some() + { + visit(path, Some(repository_bytes))?; + continue; + } + use crate::content_filter::ContentFilter; + let filtered = crate::content_filter::GitAttributesFilter::for_repository(&self.root) + .smudge(Path::new(path), &repository_bytes) + .map_err(|error| RepositoryError::Output(error.to_string()))?; + for warning in &filtered.warnings { + log::warn!("switch: {warning}"); + } + visit(path, Some(filtered.bytes))?; } - - let Some(item) = projection - .present - .get(&normalized) - .filter(|item| !item.is_directory) - else { - return Ok(None); - }; - let materialization = atomic_core::output::project_inode_attributes( - &txn, - item.position, - visibility.attribute_visibility(), - ) - .map_err(|error| RepositoryError::Database(error.to_string()))?; - if materialization.is_conflicted() { - return Err(RepositoryError::Output(format!( - "cannot switch '{}' with conflicting inode attributes", - path - ))); - } - let kind = materialization.materialization.kind; - // The lease must hash the same semantic rendering used by materialize. - // The fast content reader can retain forks resolved by that renderer. - let repository_bytes = render_name_conflict_side( - &txn, - &self.change_store, - &inode_graph_table, - &visibility, - &external_hashes, - path, - item.inode, - item.position, - ) - .map_err(RepositoryError::Output)?; - if kind != atomic_core::change::InodeKind::Regular - || first_conflict_marker_line(&repository_bytes).is_some() - { - return Ok(Some(repository_bytes)); - } - use crate::content_filter::ContentFilter; - let filtered = crate::content_filter::GitAttributesFilter::for_repository(&self.root) - .smudge(Path::new(path), &repository_bytes) - .map_err(|error| RepositoryError::Output(error.to_string()))?; - for warning in &filtered.warnings { - log::warn!("switch: {warning}"); - } - Ok(Some(filtered.bytes)) + Ok(()) } fn remove_absent_entries( @@ -1190,11 +1240,19 @@ impl Repository { })?; (graph_visibility_closure(&txn, &view)?, view.id, view.state) }; + // Only immutable decoded bytes are reused across plan/execution. Each + // new context still checks its own dependency/span indexes, and cache + // hits re-read and fingerprint the actual change files. + let verified_changes = MaterializationVerifiedChanges::new( + &self.change_store, + visibility.len().min(MAX_MATERIALIZATION_VERIFIED_CHANGES), + ); let plan = self.plan_materialization_with_visibility( working_copy, only_paths.clone(), visibility.clone(), view_id, + Some(&verified_changes), )?; let mut effects = Vec::new(); for entry in plan { @@ -1308,12 +1366,15 @@ impl Repository { prepared.operation().clone(), ); executor.complete_preparatory(&view_name)?; - let result = self.materialize_parallel_with_visibility_journaled( + let result = self.materialize_parallel_with_visibility_mode( working_copy, only_paths, visibility, view_id, - &mut executor, + true, + Some(&mut executor), + None, + Some(&verified_changes), )?; executor.complete_remaining(&view_name)?; drop(executor); @@ -1359,6 +1420,7 @@ impl Repository { true, Some(executor), None, + None, ) } @@ -1378,6 +1440,7 @@ impl Repository { false, None, None, + None, ) .map(|_| ()) } @@ -1388,6 +1451,7 @@ impl Repository { only_paths: Option>, visibility: GraphVisibilityClosure, view_id: u64, + verified_changes: Option<&MaterializationVerifiedChanges<'_>>, ) -> Result, RepositoryError> { let mut plan = Vec::new(); self.materialize_parallel_with_visibility_mode( @@ -1398,6 +1462,7 @@ impl Repository { false, None, Some(&mut plan), + verified_changes, )?; Ok(plan) } @@ -1412,6 +1477,7 @@ impl Repository { execute: bool, mut effect_executor: Option<&mut dyn MaterializationEffectExecutor>, plan: Option<&mut Vec>, + verified_changes: Option<&MaterializationVerifiedChanges<'_>>, ) -> Result { self.validate_working_copy(working_copy)?; use atomic_core::output::repo::OutputItem; @@ -1509,8 +1575,11 @@ impl Repository { } let span_context = if has_visible_spans { Some( - SpanRenderContext::new(&txn, &self.change_store, &visibility) - .map_err(|error| RepositoryError::Database(error.to_string()))?, + match verified_changes { + Some(store) => SpanRenderContext::new(&txn, store, &visibility), + None => SpanRenderContext::new(&txn, &self.change_store, &visibility), + } + .map_err(|error| RepositoryError::Database(error.to_string()))?, ) } else { None @@ -1600,12 +1669,18 @@ impl Repository { context )) })?; - store.load_change(&hash).map_err(|error| { - RepositoryError::Output(format!( - "failed to load {} ({}) while pre-warming materialization: {}", - context, hash, error - )) - })?; + // Span construction already verified every visible immutable + // source. Do not decode the same closure again solely to warm + // the graph LRU. Mixed graph files retain checked lazy reads; + // non-visible origins retain the original preload check. + if span_context.is_none() || !visibility.contains(*node_id) { + store.load_change(&hash).map_err(|error| { + RepositoryError::Output(format!( + "failed to load {} ({}) while pre-warming materialization: {}", + context, hash, error + )) + })?; + } hashes.insert(*node_id, hash); } if trace_mat { @@ -2596,6 +2671,9 @@ impl Repository { } } +#[cfg(test)] +mod verified_tests; + #[cfg(test)] mod conflict_marker_tests { use super::first_conflict_marker_line; diff --git a/atomic-repository/src/repository/materialize/verified_tests.rs b/atomic-repository/src/repository/materialize/verified_tests.rs new file mode 100644 index 00000000..f2aa4782 --- /dev/null +++ b/atomic-repository/src/repository/materialize/verified_tests.rs @@ -0,0 +1,106 @@ +use super::*; +use atomic_core::change::ChangeHeader; +use atomic_core::pristine::{CapabilityMutTxnT, SPAN_RANGE_CAPABILITY}; + +#[derive(Default)] +struct NoEffects(usize); +impl MaterializationEffectExecutor for NoEffects { + fn create_directory(&mut self, _: &str) -> Result { + self.0 += 1; + Ok(false) + } + fn write_file(&mut self, _: &str, _: &[u8]) -> Result { + self.0 += 1; + Ok(false) + } + fn remove_path(&mut self, _: &str) -> Result { + self.0 += 1; + Ok(false) + } +} + +#[test] +fn verified_plan_payload_cannot_hide_deleted_or_corrupted_sources_at_execution() { + for delete in [false, true] { + let directory = tempfile::tempdir().unwrap(); + let repo = Repository::init(directory.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let file = directory.path().join("file.txt"); + std::fs::write(&file, b"authored bytes\n").unwrap(); + repo.add(wc, "file.txt", Default::default()).unwrap(); + let recorded = repo + .record( + wc, + ChangeHeader::new("span"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + .sync_vault(false) + .enrich_kg(false), + ) + .unwrap(); + let hash = *recorded.hash(); + std::fs::remove_file(&file).unwrap(); + let (visibility, view_id) = { + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view("dev").unwrap().unwrap(); + (graph_visibility_closure(&txn, &view).unwrap(), view.id) + }; + let cache = MaterializationVerifiedChanges::new(&repo.change_store, 4); + let before = repo.working_copy_record(wc).unwrap(); + let before_head = repo + .operation_log(OperationScope::WorkingCopy(wc), Some(1), false) + .unwrap() + .head_state; + let plan = repo + .plan_materialization_with_visibility( + wc, + None, + visibility.clone(), + view_id, + Some(&cache), + ) + .unwrap(); + assert!(plan.iter().any(|entry| entry.path == "file.txt")); + let source = repo.change_store.change_path(&hash); + if delete { + std::fs::remove_file(source).unwrap(); + } else { + let mut bytes = std::fs::read(&source).unwrap(); + let last = bytes.len() - 1; + bytes[last] ^= 1; + std::fs::write(source, bytes).unwrap(); + } + let mut executor = NoEffects::default(); + assert!(repo + .materialize_parallel_with_visibility_mode( + wc, + None, + visibility, + view_id, + true, + Some(&mut executor), + None, + Some(&cache) + ) + .is_err()); + assert_eq!( + executor.0, 0, + "source rejection must happen before any executor effect" + ); + assert!(!file.exists()); + assert_eq!(repo.working_copy_record(wc).unwrap(), before); + assert_eq!( + repo.operation_log(OperationScope::WorkingCopy(wc), Some(1), false) + .unwrap() + .head_state, + before_head + ); + } +} diff --git a/atomic-repository/src/repository/switch.rs b/atomic-repository/src/repository/switch.rs index 9d9fb6ae..fd2d29bc 100644 --- a/atomic-repository/src/repository/switch.rs +++ b/atomic-repository/src/repository/switch.rs @@ -557,6 +557,8 @@ impl Repository { self.validate_working_copy(working_copy)?; let old_view_name = self.desired_view_name(working_copy)?; let before_record = self.working_copy_record(working_copy)?; + let trace_switch = std::env::var_os("ATOMIC_TRACE_SWITCH").is_some(); + let phase_start = std::time::Instant::now(); // Resolve both views and validate both dependency closures before the // switch publishes a pointer or mutates TREE-derived state. @@ -705,12 +707,26 @@ impl Repository { // shelving ignored files, or removing tracked paths. This does not make // filesystem execution crash-safe, but it guarantees graph/preload/ // content errors refuse the switch before its first external effect. + if trace_switch { + eprintln!( + "[switch timing] namespace elapsed={:?}", + phase_start.elapsed() + ); + } + let phase_start = std::time::Instant::now(); self.validate_materialization_with_visibility( working_copy, None, new_visibility.clone(), new_view_id, )?; + if trace_switch { + eprintln!( + "[switch timing] preflight_render elapsed={:?}", + phase_start.elapsed() + ); + } + let phase_start = std::time::Instant::now(); let mut tracked_paths: HashSet = old_files.union(&new_files).cloned().collect(); tracked_paths.extend(old_directories.iter().cloned()); @@ -833,8 +849,17 @@ impl Repository { let mut files_to_write: Vec = new_files.iter().cloned().collect(); files_to_write.sort(); - for path in files_to_write { - let target = EffectTarget::FilesystemPath { path: path.clone() }; + if trace_switch { + eprintln!( + "[switch timing] nonfile_planning elapsed={:?}", + phase_start.elapsed() + ); + } + let phase_start = std::time::Instant::now(); + self.for_each_switch_target_file(&files_to_write, view, |path, bytes| { + let target = EffectTarget::FilesystemPath { + path: path.to_string(), + }; let mut expected_old = effects .iter() .rev() @@ -853,15 +878,13 @@ impl Repository { }); expected_old = EffectValue::Absent; } - let bytes = self - .switch_target_file_bytes(&path, view)? - .ok_or_else(|| RepositoryError::InvalidOperation { - message: format!( - "target view '{view}' projects '{path}' as a file without materialized content" - ), - })?; + let bytes = bytes.ok_or_else(|| RepositoryError::InvalidOperation { + message: format!( + "target view '{view}' projects '{path}' as a file without materialized content" + ), + })?; let materialization = new_file_materialization - .get(&path) + .get(path) .copied() .unwrap_or_default(); let kind = match materialization.kind { @@ -891,8 +914,17 @@ impl Repository { expected_new, }); } - } + Ok(()) + })?; + if trace_switch { + eprintln!( + "[switch timing] file_leases files={} elapsed={:?}", + new_files.len(), + phase_start.elapsed() + ); + } + let phase_start = std::time::Instant::now(); for path in &restore_paths { self.plan_restore_ignored_paths(working_copy, view, path, &mut effects)?; } @@ -916,6 +948,13 @@ impl Repository { operation_timestamp_ms(), )?; let operation_id = prepared.operation().id(); + if trace_switch { + eprintln!( + "[switch timing] prepare_operation elapsed={:?}", + phase_start.elapsed() + ); + } + let phase_start = std::time::Instant::now(); let initial_effect = prepared .operation() .payload() @@ -1043,6 +1082,12 @@ impl Repository { )?; filesystem_executor.complete_remaining(view)?; drop(filesystem_executor); + if trace_switch { + eprintln!( + "[switch timing] execute elapsed={:?}", + phase_start.elapsed() + ); + } if std::env::var_os("ATOMIC_TEST_FAIL_SWITCH_AFTER_MATERIALIZE").is_some() { return Err(RepositoryError::InvalidOperation { diff --git a/atomic-repository/src/repository/tests/mod.rs b/atomic-repository/src/repository/tests/mod.rs index 4c744e1a..80d3a470 100644 --- a/atomic-repository/src/repository/tests/mod.rs +++ b/atomic-repository/src/repository/tests/mod.rs @@ -50,6 +50,7 @@ mod snapshot_tests; mod stale_conflict_reconcile_tests; mod status_tests; mod switch_file_loss_tests; +mod switch_lease_render_tests; mod synthesis_tests; mod tag_projection_tests; mod tracking_tests; diff --git a/atomic-repository/src/repository/tests/switch_lease_render_tests.rs b/atomic-repository/src/repository/tests/switch_lease_render_tests.rs new file mode 100644 index 00000000..2b0fa159 --- /dev/null +++ b/atomic-repository/src/repository/tests/switch_lease_render_tests.rs @@ -0,0 +1,217 @@ +use super::*; +use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; + +fn record_all(repo: &TestRepository, message: &str) { + repo.record( + ChangeHeader::new(message), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + .sync_vault(false) + .enrich_kg(false), + ) + .unwrap(); +} + +fn enable_spans(repo: &TestRepository) { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); +} + +fn render_batch(repo: &TestRepository, paths: &[String], view: &str) -> Vec>> { + let mut rendered = Vec::new(); + repo.for_each_switch_target_file(paths, view, |path, bytes| { + assert_eq!(path, paths[rendered.len()]); + rendered.push(bytes); + Ok(()) + }) + .unwrap(); + rendered +} + +#[test] +fn switch_lease_batch_matches_filtered_bytes_and_does_not_reuse_other_views() { + for spans in [false, true] { + let (temp, mut repo) = create_temp_repo(); + // Retain a graph-origin file even in the span-capable fixture. + std::fs::write(temp.path().join("legacy.txt"), b"legacy\n").unwrap(); + repo.add("legacy.txt", TrackingOptions::default()).unwrap(); + record_all(&repo, "graph origin"); + if spans { + enable_spans(&repo); + } + std::fs::write(temp.path().join(".gitattributes"), "*.txt text eol=crlf\n").unwrap(); + for (path, bytes) in [ + ("a.txt", b"alpha\n".as_slice()), + ("b.txt", b"beta\n"), + ("empty.txt", b""), + ] { + std::fs::write(temp.path().join(path), bytes).unwrap(); + repo.add(path, TrackingOptions::default()).unwrap(); + } + record_all(&repo, "new files"); + repo.create_view_from("lease-feature", "dev").unwrap(); + repo.switch_view("lease-feature").unwrap(); + std::fs::write(temp.path().join("a.txt"), b"changed\r\n").unwrap(); + record_all(&repo, "feature edit"); + let paths: Vec = ["a.txt", "b.txt", "empty.txt", "legacy.txt", "not-present"] + .map(str::to_string) + .into(); + let expected = vec![ + Some(b"alpha\r\n".to_vec()), + Some(b"beta\r\n".to_vec()), + Some(vec![]), + Some(b"legacy\r\n".to_vec()), + None, + ]; + let before = repo.working_copy_record(repo.working_copy()).unwrap(); + assert_eq!(render_batch(&repo, &paths, "dev"), expected); + let mut feature = expected.clone(); + feature[0] = Some(b"changed\r\n".to_vec()); + assert_eq!(render_batch(&repo, &paths, "lease-feature"), feature); + assert_eq!(render_batch(&repo, &paths, "dev"), expected); + assert_eq!( + repo.working_copy_record(repo.working_copy()).unwrap(), + before + ); + assert_eq!( + std::fs::read(temp.path().join("a.txt")).unwrap(), + b"changed\r\n" + ); + + let mut visited = Vec::new(); + let error = repo + .for_each_switch_target_file(&paths, "dev", |path, _| { + visited.push(path.to_owned()); + Err(RepositoryError::InvalidOperation { + message: "visitor refused".into(), + }) + }) + .unwrap_err(); + assert!(error.to_string().contains("visitor refused")); + assert_eq!(visited, ["a.txt"]); + } +} + +#[test] +fn switch_lease_batch_checks_later_span_indexes_even_after_context_is_built() { + use atomic_core::pristine::span_patch_storage::SpanPatchReadTxn; + use atomic_core::pristine::TreeTxnT; + use redb::{MultimapTableDefinition, TableDefinition}; + + for missing_root in [true, false] { + let (temp, repo) = create_temp_repo(); + enable_spans(&repo); + for name in ["a.txt", "b.txt"] { + std::fs::write(temp.path().join(name), name.as_bytes()).unwrap(); + repo.add(name, TrackingOptions::default()).unwrap(); + } + record_all(&repo, "span roots"); + std::fs::write(temp.path().join("b.txt"), b"edited b").unwrap(); + record_all(&repo, "span edit"); + let paths = ["a.txt".to_string(), "b.txt".to_string()]; + assert_eq!(render_batch(&repo, &paths, "dev").len(), 2); + let inode = repo + .pristine() + .read_txn() + .unwrap() + .get_inode("b.txt") + .unwrap() + .unwrap(); + let edits = repo + .pristine() + .read_txn() + .unwrap() + .span_edits_for_inode(inode) + .unwrap(); + assert!(!edits.is_empty()); + let wc = repo.working_copy(); + let before_record = repo.working_copy_record(wc).unwrap(); + let before_head = repo + .operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + Some(1), + false, + ) + .unwrap() + .head_state; + drop(repo); + let db = redb::Database::open(temp.path().join(".atomic/atomic.redb")).unwrap(); + let txn = db.begin_write().unwrap(); + if missing_root { + let mut roots = txn + .open_table(TableDefinition::::new( + "span_patch_roots_v1", + )) + .unwrap(); + assert!(roots.remove(inode.get()).unwrap().is_some()); + } else { + let mut rows = txn + .open_multimap_table(MultimapTableDefinition::<&[u8; 32], &[u8; 40]>::new( + "span_patch_edits_v1", + )) + .unwrap(); + assert!(rows.remove(&edits[0].0, &edits[0].1).unwrap()); + } + txn.commit().unwrap(); + drop(db); + let repo = Repository::open(temp.path()).unwrap(); + let mut visited = Vec::new(); + let error = repo + .for_each_switch_target_file(&paths, "dev", |path, bytes| { + visited.push(path.to_string()); + assert_eq!(bytes.unwrap(), b"a.txt"); + Ok(()) + }) + .unwrap_err(); + let expected_error = if missing_root { + "span root missing" + } else { + "span index differs" + }; + assert!(error.to_string().contains(expected_error), "{error}"); + assert_eq!(visited, ["a.txt"]); + assert_eq!(repo.working_copy_record(wc).unwrap(), before_record); + assert_eq!( + repo.operation_log( + atomic_core::operation::OperationScope::WorkingCopy(wc), + Some(1), + false + ) + .unwrap() + .head_state, + before_head + ); + for name in ["a.txt", "b.txt"] { + assert_eq!( + std::fs::read(temp.path().join(name)).unwrap(), + if name == "a.txt" { + name.as_bytes() + } else { + b"edited b" + } + ); + } + } +} + +#[cfg(unix)] +#[test] +fn switch_lease_batch_does_not_smudge_span_symlink_targets() { + let (temp, repo) = create_temp_repo(); + enable_spans(&repo); + std::fs::write( + temp.path().join(".gitattributes"), + "link ident text eol=crlf\n", + ) + .unwrap(); + std::os::unix::fs::symlink("target\n$Id$", temp.path().join("link")).unwrap(); + repo.add("link", TrackingOptions::default()).unwrap(); + record_all(&repo, "span symlink"); + assert_eq!( + render_batch(&repo, &["link".into()], "dev"), + vec![Some(b"target\n$Id$".to_vec())] + ); +} diff --git a/tools/materialize-bench/IMPORTED-COLD-RESULTS.md b/tools/materialize-bench/IMPORTED-COLD-RESULTS.md new file mode 100644 index 00000000..763333fc --- /dev/null +++ b/tools/materialize-bench/IMPORTED-COLD-RESULTS.md @@ -0,0 +1,125 @@ +# First imported Hyperfine cold-guest smoke + +Follow-up: [phase breakdown and transaction-local reuse](SPAN-CONTEXT.md). + +**The span speedup is present on the real imported history, but the 100–200 ms +materialization goal is not met.** These are two samples per backend/case, not +a stable performance distribution. The reported p95 for two observations is +just the maximum; do not interpret it as a reliable tail-latency estimate. + +## Measurements + +Fresh-process API totals include repository open and close, with verification +outside timing. Graph and spans restore the same selected Git files. + +| Case | Graph median | Span median | Graph / span | +|---|---:|---:|---:| +| Validated rendering (no file writes) | 2,472.2 ms | 247.8 ms | 9.98× | +| Full journaled materialization | 5,707.9 ms | 926.5 ms | 6.16× | + +Full span-materialization samples: **924.7 and 928.3 ms**. Both median and +maximum exceed 200 ms. Each full-materialization sample writes **75 files / +593,962 bytes**, skips one existing native bootstrap file, verifies the selected +Git bytes/kinds/executable bits and a single completed operation head, and leaves +the Git index unchanged. + +Span render-probe phase medians: + +| Phase | Median | +|---|---:| +| Repository open, including working-copy identification | 8.8 ms | +| Read/visibility setup | 8.3 ms | +| Validated span-context construction | **208.5 ms** | +| Render selected file bytes | **6.1 ms** | +| Repository close | 15.8 ms | + +Graph rendering alone takes about 2,446.4 ms. The span context already consumes +the entire target budget before file writes. This identifies a concrete next +investigation: profile content loading/hashing, declared/indexed dependency +checks, expected-edit construction and causal-proof setup, then measure how +often the real materializer repeats that work during plan and execution. + +Do not attribute the remaining difference between the render probe and full +materialization entirely to filesystem writes: those cases have different +control paths, and materialization also plans, validates, renders and journals. +No further production performance shortcut was introduced for this smoke. + +## Fixture and cache boundary + +- Pinned Hyperfine Git tip: `c5161524980522320c7d3425e714cc0965a869a5`. +- **1,097 first-parent commits**, complete **75-file selected Git tree**. +- Dedicated smolvm: `atomic-hyperfine-cold-20261106`. +- Linux/aarch64, two vCPUs, 8 GiB configured memory, two Rayon threads. +- Guest-local ext4 workspace, not a host-shared source/repository mount. +- Before each fresh process: restore the closed baseline database at its original + canonical working-copy location, prepare selected files, guest `sync`, then + guest `drop_caches=3`. No warmups. Both backend orders are represented once. +- Host disk-image/physical storage caches are uncontrolled. These results are + **guest-page-cache-cold**, not host-cache-cold or physical-disk-cold. +- Import, cache reset, fixture reset, VM startup and post-sample verification + are excluded from API totals. Separate process-wall observations include + verification and are not interchangeable with API latency. + +The source archive is a frozen export of the existing implementation plus the +single-shot measurement example. It excludes host Git/Atomic/OpenCode metadata +and credentials. Both graph and span imports use the same feature-enabled +release CLI. The measured example uses normal optimized release settings, not +a debug build. Failed 4/8 GiB builds are failures, not benchmark samples; the CLI +linked at 16 GiB and the guest returned to 8 GiB before import and measurement. + +## Correctness caveat: explicit setup repair in both backends + +The original fresh graph-import smoke stopped at `doctor check`; it ran **no +timed samples**. A separate fixture retry revealed the same eight stale +`DIRECTORIES` rows in **both graph and span imports** (inodes 9, 15, 22, 30, +40, 50, 144 and 149). + +Only the disposable retry fixtures received explicit +`atomic doctor repair-native-indexes`. Required post-repair checks passed in +both: **806 consistent derived-index rows and 76 clean working-copy files**. +The setup also verified unchanged immutable change files, recorded history, +Git index and all selected Git checkout bytes before saving the baseline. +Pre-repair databases and failed/passing doctor logs are retained. + +Consequently these are **post-repair imported-state measurements**, not a clean +ordinary fresh-import pass. The stale-directory import bug remains unfixed. +Repair was not timed, not silently performed by a read, and not applied to the +demo checkout. No unsupported coverage or initial doctor failure counts as a pass. + +All **eight measured backend samples** passed their byte/mode/head checks. +Host correctness smoke and Clippy for the new Rust example passed; six runner +unit tests passed. Those tests do not substitute for the imported-state evidence. + +## Identities and artifacts + +SHA-256 identities: + +```text +source archive: fd7372f5ebd72fedce3d779a1bb74128f71b722ea3a955355e5fcf0227a696d2 +Git bundle: 0aa93f6a5679412b4250a08f85503e56b4e367b28561a4829dce6461eb3c6758 +Linux CLI: 379211242b58ef0e93ddaf910920f24446046b4a6de3f8bf9f7971eec6ad2cd7 +Linux probe: 46de9a01e69f0182f4741eb37169c498ec45f120c9a2234ab333ac7d3139746d +evidence tar: 797c32e459ce3e0e1459efa07af1aa456d042ac27c3c546a0938222a23fa5f64 +``` + +Host evidence is retained under: + +```text +/private/var/folders/hl/fwbg_jc93tvgz43rj7t25x240000gn/T/opencode/span-harness01-1gFvn7/imported-cold-vm-01/ + source.json + linux-build.json + cold-smoke-run.json + cold-smoke-evidence-01.tar.gz +``` + +The archive includes per-sample JSON/stdout/stderr, the pinned probe, cache-reset +observations, prepared fixture identities, Git manifest, doctor/repair logs and +build logs (including the 8 GiB CLI OOM). Guest fixtures remain at +`/workspace/hyperfine-smoke-repaired-fixtures`; the original failed graph fixture +is separate at `/workspace/hyperfine-smoke-fixtures`. Raw results are at +`/workspace/cold-smoke-repaired-01`. + +Import setup observations were 34.4 s graph and 53.2 s spans; these are single +observations, **not import performance claims**. Baseline databases were +67,375,104 and 12,193,792 bytes respectively, uncompacted. They are not a new +compacted storage benchmark. diff --git a/tools/materialize-bench/IMPORTED-COLD.md b/tools/materialize-bench/IMPORTED-COLD.md new file mode 100644 index 00000000..6e00a265 --- /dev/null +++ b/tools/materialize-bench/IMPORTED-COLD.md @@ -0,0 +1,148 @@ +# Imported Hyperfine materialization: cold guest benchmark + +The [first real Hyperfine smoke results](IMPORTED-COLD-RESULTS.md) are now +available. They are post-repair, guest-page-cache-cold measurements; the 200 ms +materialization target was not met. +The [verified-loading follow-up](VERIFIED-LOADING.md) reduces full materialization +to about 310 ms under the same qualifications; render-only is not the goal case. + +**Performance goal:** approximately 100–200 ms for materialization **after** a Git +import. Importing all history is setup, not part of this latency target. Faster +than 100 ms is acceptable. Report both median and p95 against a 200 ms upper +target; correctness success is not proof the performance goal is met. + +This benchmark reuses `SpanRenderContext`, the graph renderer and the ordinary +journaled `Repository::materialize` path. It does not replace existing span +storage, fake historical semantic authorship, bypass validation, or substitute +plain file copying for materialization. + +## Two cases + +- `render`: fresh-process repository open, visibility setup, validated content + rendering of every selected Git file, and repository close. No working-copy + writes. Useful for identifying the rendering/validation floor, **not** for + satisfying the materialization target by itself. +- `materialize_missing`: fresh-process repository open, ordinary journaled + materialization after removing the selected Git files, and repository close. + Includes the real filesystem writes, leases, journal receipts and durability + work. This is the target case. Native bootstrap files not present in the Git + tree (for example `.atomicignore`) stay in place and are handled by the normal + materializer, rather than silently dropping bootstrap history. + +The example reports `total_ns` and phase times. Manifest parsing, byte/mode +verification and operation-head checks are outside the reported API total. +Process wall time is also retained, but includes those verification steps and +is labeled accordingly; it is not interchangeable with `total_ns`. VM boot, +Git import, fixture reset, `sync`, and cache reset are outside sample timing. + +## Pinning and verification + +The Hyperfine fixture is Git tip +`c5161524980522320c7d3425e714cc0965a869a5`, **1,097 first-parent commits**, with +**75 selected Git files**. Export its public Git history to a bundle and a +manifest generated from `git ls-tree -r -z ` and `git cat-file blob `. +The manifest format is: + +```json +{ + "git_tip": "c5161524980522320c7d3425e714cc0965a869a5", + "first_parent_commits": 1097, + "files": [ + {"path": "src/main.rs", "mode": "100644", "git_blob": "", "sha256": ""} + ] +} +``` + +Use the complete tree, not just the example entry. Preparation rejects unsafe +paths, duplicate entries, unsupported modes, mismatching blobs, incomplete tree +coverage, and the wrong tip/history length. It imports graph and spans using +the same experimental-span-enabled CLI binary and checks the unchanged Git +checkout/index and `doctor check` afterward. Import time and uncompacted database +sizes are retained separately. + +The first full-Hyperfine setup smoke failed on graph import: `doctor check` +reported eight stale `DIRECTORIES` rows (inodes 9, 15, 22, 30, 40, 50, 144 and +149). No cold samples ran in that attempt. The original fixture is retained; +the harness does not silently suppress this fresh-import failure. + +An explicit `prepare --repair-derived-indexes` retry may repair a **new +disposable fixture** before measurement. It preserves the pre-repair database +and failed doctor logs, runs `repair-native-indexes`, requires a passing doctor +check, and verifies unchanged immutable change files, recorded history, Git +index and expected checkout bytes. The report records whether each backend +needed repair. Such results are **post-repair imported-state measurements**, +not proof that ordinary fresh import passes doctor. Setup repair is not timed +and is never run on the demo checkout. Without that flag, doctor failure still +aborts preparation. + +Every sample verifies SHA-256 bytes, file kinds and executable bits for all +selected files, plus a single verified working-copy operation head. Span render +probes require valid span roots; they do not fall back to graph output on a +missing index. The CLI and probe must be optimized Linux/aarch64 binaries built +from frozen source; macOS executables cannot run in the guest. The example's +debug smoke test checks the protocol but is not imported-history or cold-cache +performance evidence. + +## Cache and storage policy + +Use a dedicated named smolvm machine with no host credentials, SSH forwarding, +source-tree writable mount or demo-checkout access. Copy frozen source and the +public fixture bundle into its **guest-local ext4 workspace**. Build before +measurement and retain source/archive/executable identities and build logs. +Registry/dependency access is setup only; measurement does not need network. + +The guest runner restores a closed baseline redb at its **original absolute +working-copy location**, prepares the selected files, calls guest `sync`, writes +`3` to `/proc/sys/vm/drop_caches`, and starts a fresh probe process. Failure to +reset caches fails the run. It refuses non-ext4 backing to avoid accidentally +calling a host-shared filesystem cold. It retains pre/post-reset `/proc/meminfo`. +Eight measured iterations alternate backend order evenly; there are no warmup +samples for this cold protocol. First/second-position distributions stay visible. + +These are **guest-page-cache-cold** observations. Host disk-image and physical +storage caches are uncontrolled unless separately proven otherwise. Do not label +the numbers host-cache-cold or physical-disk-cold. Nothing purges host-wide caches. +Resetting guest caches is narrower and less disruptive than restarting the host. + +Only the closed database is reset between samples. Immutable change files and +any prior recovery artifacts remain at their original location; the restored +database is authoritative for heads/receipts and does not reference later runs. +This is not a disk-image reset or a claim that the entire disk layout is fresh. +All fixture mutation is confined to the runner's newly created, ownership-marked +disposable directory. User files and other VMs are outside scope. + +### smolvm setup lessons + +On this installation, requests smaller than the runtime's 20 GiB storage / 10 +GiB overlay templates triggered missing-`resize2fs` warnings and the disposable +workspace did not survive a restart. Using the matching template sizes passed a +sentinel write → graceful stop → start → read check. Verify persistence before +an expensive import; do not assume it from CLI help alone. No user VM was changed. + +The first 4 GiB, two-job Linux release build was OOM-killed. With 8 GiB and one +Cargo worker the measurement probe built, but the full CLI's release LTO link +was also OOM-killed. The successful CLI link retry used 16 GiB for build only; +the guest returned to 8 GiB before import/measurement, with two vCPUs / two Rayon threads. +Keep the normal optimized release settings and features rather than changing +the measured workload to fit build memory. Preserve failed builds as failures, +not benchmark samples. Resource changes and builds must finish before timing. + +## Commands inside the prepared guest + +```sh +python3 /workspace/imported_guest.py prepare \ + --root /workspace/hyperfine-fixtures \ + --bundle /workspace/hyperfine.bundle \ + --manifest /workspace/hyperfine-manifest.json \ + --cli /workspace/source/target/release/atomic + +python3 /workspace/imported_guest.py measure \ + --root /workspace/hyperfine-fixtures \ + --probe /workspace/source/target/release/examples/imported_materialize_bench \ + --output /workspace/cold-results-01 --iterations 8 +``` + +Both root and output must be new directories. Copy results/build logs back to +approved host scratch storage. Retain raw per-sample JSON and stdout/stderr, not +just a median. No valid imported cold measurements or 200 ms target attainment +is implied merely by adding this harness. diff --git a/tools/materialize-bench/README.md b/tools/materialize-bench/README.md new file mode 100644 index 00000000..d81aba2f --- /dev/null +++ b/tools/materialize-bench/README.md @@ -0,0 +1,74 @@ +# Materialization benchmark + +For the separate pinned Git-import / smolvm cold-guest protocol, see +[IMPORTED-COLD.md](IMPORTED-COLD.md). Native synthetic results below do not +establish its 100–200 ms materialization target. +The [span-context phase/reuse follow-up](SPAN-CONTEXT.md) identified the load/hash +bottleneck; lookup caching alone did not demonstrate a cold speedup. +[Verified loading and repeated-data reuse](VERIFIED-LOADING.md) subsequently +reduced full span materialization to about 310 ms, still above the 200 ms target. + +Compare matched synthetic **native** graph and opt-in span histories. This is +not a Git-import benchmark. No production performance shortcuts are enabled. + +```sh +python3 -B -m unittest discover -s tools/materialize-bench -p 'test_*.py' +cargo test --offline --locked -p atomic-repository --example materialize_bench +python3 -B tools/materialize-bench/run.py --build --profile many-files --output /approved/scratch/run-01 +python3 -B tools/materialize-bench/run.py --binary /approved/scratch/run-01/materialize_bench --profile hot-file --output /approved/scratch/run-02 +``` + +Use a new output directory outside the source tree. The release build is offline +and locked; Cargo must already have its dependencies. The runner fingerprints +source before/after build and measurement, copies the executable, and checks its +identity after running. Do not edit source during a run. A caller-supplied binary +has explicitly **unverified source association**; correlate its SHA-256 with the +original build's `run.json`. Debug runs require `--allow-debug` and are smoke +tests, never performance evidence. + +Defaults: eight measured iterations, one excluded warmup, two Rayon threads. +`many-files` uses 24 files × 128 lines and 24 hot-file edits; `hot-file` uses one +4096-line file and 48 edits; `smoke` uses two files × eight lines and two edits. +Every fixture also includes an empty file. Override with `--files`, `--lines`, +`--edits`, `--iterations`, `--warmups`, and `--threads`. + +## Cases and timing boundaries + +- `render_session`: render base-view content using an existing repository handle. +- `render_reopened`: drop/reopen the repository, then render; includes open cost. +- `materialize_noop`: ordinary journaled materialization of already-correct files. +- `materialize_missing`: same API after removing only generated tracked files. +- `switch_one_forward/back`: switch to/from a view with one changed line. +- `switch_many_forward/back`: switch to/from a view changing one line per file. + +Render probes report read/closure setup, span-context validation, and rendering +separately. End-to-end cases use the real journaled APIs; the probe is not a +substitute for those timings. Setup, deleting fixture files, and post-sample +byte/mode/head verification are outside timing. Render allocation/teardown may +contribute to total time beyond the individually measured phases. + +Backends alternate order per iteration; cases stay in fixed order so forward/ +back switches remain paired. These are sequential observations of **uncontrolled +OS caches**, not cold-cache measurements. Reopened handles are not cold disks. +Fixtures, verification and prior cases warm caches. Eight samples give only a +rough distribution: nearest-rank p95 is the maximum for eight samples. Use an +even iteration count to balance backend ordering. First/second-position timing +distributions are retained separately: first-position one-file switches showed +a sizable systematic penalty in initial runs, and seven samples overweighted +one order in each backend's median. This is not proof of the underlying cause. +Repeat runs, reverse scheduling or use more samples before claiming small +improvements. Odd counts remain available for correctness smoke tests; the +metadata flags their unbalanced backend ordering. + +`raw.json` retains all sample times, phase times, counters, config, executable +Blake3 and setup sizes. `run.json` retains SHA-256 identity, source fingerprint, +commands, outcome, medians/p95 and logs. A graph/span median ratio above one +favors spans. Database sizes are post-sample, uncompacted sizes, not a storage +efficiency measurement. Errors and incomplete/duplicate samples fail the run; +they are not passes. Fixtures are disposable; `--keep-fixtures` retains them. + +The benchmark checks expected bytes and modes after every sample and verifies a +single completed working-copy operation head. It does not certify imported +history, genuine conflicts, cold caches, transport, watchers, crashes or power +loss. Keep provenance, causal validation, leases and receipts intact when +optimizing; cache verified work instead of bypassing checks. diff --git a/tools/materialize-bench/RESULTS.md b/tools/materialize-bench/RESULTS.md new file mode 100644 index 00000000..b5161a42 --- /dev/null +++ b/tools/materialize-bench/RESULTS.md @@ -0,0 +1,102 @@ +# Initial materialization baseline + +Follow-up: [many-file switch optimization and balanced measurements](SWITCH-OPTIMIZATION.md). + +Two sequential runs per profile, each with seven measured iterations and one +excluded warmup, two Rayon threads, on macOS/aarch64. All four runs used the same +optimized executable, SHA-256: + +```text +9e2e606f59404461bfe9377e37055f1be7791e670997bfd416e62ea6d1174402 +``` + +These are matched synthetic native histories, **not imported histories or cold +OS caches**. See [README.md](README.md) for timing boundaries and limitations. +The second hot-file run followed the second many-file run; workloads were not +run concurrently. Unrelated system load was not controlled. + +## Median milliseconds, graph / span + +| Case | Many files, run 1 | Many files, run 2 | Hot file, run 1 | Hot file, run 2 | +|---|---:|---:|---:|---:| +| Render session | 19.879 / 11.479 | 18.618 / 11.991 | 26.615 / 6.087 | 26.567 / 5.689 | +| Reopen + render | 79.108 / 67.086 | 76.854 / 67.346 | 81.716 / 57.147 | 81.074 / 55.913 | +| No-op materialization | 59.488 / 68.418 | 62.469 / 64.301 | 104.701 / 60.666 | 101.865 / 57.205 | +| Missing-file materialization | 515.078 / 491.784 | 509.120 / 488.628 | 140.755 / 97.291 | 138.410 / 94.687 | +| One-file switch forward | 170.279 / 240.252 | 193.557 / 238.088 | 193.673 / 128.816 | 190.012 / 124.809 | +| One-file switch back | 166.407 / 200.402 | 178.411 / 218.465 | 192.236 / 129.753 | 187.829 / 125.484 | +| Many-file switch forward | 593.525 / 595.991 | 589.467 / 621.105 | 194.396 / 130.727 | 190.844 / 124.910 | +| Many-file switch back | 591.356 / 629.987 | 599.503 / 628.057 | 194.247 / 129.663 | 189.254 / 123.764 | + +Many files: 24 × 128 lines, 24 hot-file edits, plus an empty file. +Hot file: one × 4096 lines, 48 edits, plus an empty file. With one nonempty file, +the hot-file profile's one/many switch cases are effectively the same workload, +not independent evidence of multi-file scaling. + +Every run completed all 112 backend samples (56 graph/span pairs), with expected +bytes/modes and a single verified working-copy operation head checked outside +sample timing. Four runs total 448 checked backend samples. No skips count as +passes. Benchmark smoke also passed all eight cases; runner unit tests and +example Clippy passed. + +## Interpretation and next target + +**Subsequent measurement caveat:** per-iteration inspection during optimization +found a strong first-position penalty in one-file forward switching. These +seven-sample runs give one backend four first-position samples and the other +three, so aggregate medians can overstate the backend gap. Preserve these raw +observations, but use even counts and order-stratified summaries for follow-up +comparisons. The runner now defaults to eight iterations and reports first/ +second-position distributions explicitly. This does not establish the cause +of the order effect. + +- Session rendering favors spans in both profiles: 1.55–1.73× for many files and + 4.37–4.67× for the hot file. +- Hot-file end-to-end results consistently favor spans: missing-file restoration + 1.45–1.46×, no-op materialization 1.73–1.78×, switching about 1.48–1.53×. +- Many-file one-file switching is consistently slower with spans. Forward + medians are 238–240 ms versus graph's 170–194 ms. No-op results vary enough + that a precise small regression claim would be premature. +- Many-file missing-file restoration improves only slightly; rendering savings + do not translate proportionally into journaled filesystem operations. + +In first-run session probes, span-context construction takes 2.934 ms versus +0.245 ms rendering for many files; the hot file takes 2.187 ms versus 0.071 ms. +Graph render phases are 12.918 and 23.370 ms respectively. Total probe timings +also include work outside those phases, including teardown. Do not add phase +medians and assume they equal the median total. Reopened probes spend roughly +50–56 ms opening either backend, limiting gains from rendering alone. + +**Next priority: profile the many-file small-switch regression**, including +closure/context construction counts, per-file content retrieval, repeated plan +validation/rendering, and operation preparation/execution. Sweep file count +with total bytes and history held fixed to distinguish file-count overhead from +history/size effects. Context creation, prewarming, and repeated validation are +candidates from code inspection, not established causal explanations. Existing +materialization tracing starts after context construction and therefore cannot +by itself account for that cost. + +No production performance optimization is included in this baseline. Retain +hash/index/causal validation, provenance, leases and receipts; reuse verified +work only within explicit validity boundaries. + +## Raw evidence + +Artifacts were retained outside the repository under: + +```text +/private/var/folders/hl/fwbg_jc93tvgz43rj7t25x240000gn/T/opencode/span-harness01-1gFvn7/ + materialize-many-01/ + materialize-hot-01/ + materialize-many-02/ + materialize-hot-02/ +``` + +Each contains `raw.json`, `run.json`, the pinned executable and stdout/stderr. +The first run also contains Cargo build logs and before/after source fingerprint +checks. Subsequent supplied-binary runs explicitly mark source association as +unverified; their executable SHA-256 matches the first build. A formatting-only +change to the example's function signature occurred before the second runs; +those runs still used the original frozen executable. Scratch paths are local +evidence, not permanent downloadable artifacts; this table preserves the +baseline if temporary storage is reclaimed. diff --git a/tools/materialize-bench/SPAN-CONTEXT.md b/tools/materialize-bench/SPAN-CONTEXT.md new file mode 100644 index 00000000..7e7bed61 --- /dev/null +++ b/tools/materialize-bench/SPAN-CONTEXT.md @@ -0,0 +1,170 @@ +# Span-context phases and transaction-local reuse + +Follow-up: [verified loading and repeated decoded-data reuse](VERIFIED-LOADING.md) +measures a substantial improvement and documents phase pipeline version 2. + +## Implemented + +`SpanRenderContext::build_metrics()` now exposes wall-clock construction phases +and lookup counters. `ATOMIC_TRACE_SPAN_RENDER=1` emits the same metrics once +per context. The imported-history probe retains nested +`span_context_phases_ns` and `span_context_counts` separately from its outer +open/render/close phases; do not sum nested and outer timings together. + +Phases: + +- `resolve_changes`: resolve visible change hashes. +- `load_changes`: load/decode complete changes in bounded parallel batches. +- `verify_hashes`: canonical hash checking and extraction of render-required data. +- `validate_dependencies`: compare indexed and hashed dependencies and resolve IDs. +- `assemble_edits`: construct exact expected per-inode edit sets. +- `causal_proof`: build bounded reachability from the already checked adjacency. +- `total`: construction including allocation and loop overhead, excluding trace + output. It need not equal the sum of phase medians. + +Loading and hashing are separate parallel stages of at most 64 changes, so these +are wall-clock phase times, not sums of worker CPU times. This also temporarily +retains full change metadata for one bounded batch. Failures are still consumed +in dependency order; a later load failure cannot mask an earlier hash failure. + +Construction-local maps reuse: + +- One indexed dependency-list read per visible change, used both for comparison + against hashed dependencies and for causal-proof construction. +- Successful `INTERNAL` lookups per referenced hash. +- Successful portable-file-to-inode lookups per `(owner hash, file ordinal)`. + +The inverse-ID cache is **not** populated merely from `EXTERNAL`: the actual +inverse mapping must still exist. These caches are local to construction and +do not cross transactions or publication. Per-file root/edit/index checks stay +in the renderer. The proof remains bounded by the existing budget, rejects +invalid topological order, and never treats numeric NodeId order as causality. +The existing checked fallback remains when the proof does not fit. + +No change identity, provenance, schema, capability, migration or operation-journal +contract changes. This does not convert existing graph history into span history. +No persistent verification bit or validation bypass was added. + +## Measured outcome: useful diagnosis, no demonstrated cold speedup + +Balanced cold measurements reuse the same explicitly repaired Hyperfine +fixtures as the [first imported smoke](IMPORTED-COLD-RESULTS.md): 1,097 Git +first-parent commits, 75 selected files, 1,098 visible Atomic changes including +native bootstrap. Eight iterations per run, two vCPUs / two Rayon threads, +8 GiB guest, no warmups. Scheduling: before → after → after → before. + +Median milliseconds, showing both separate runs: + +| Case/backend | Before | After | +|---|---:|---:| +| Span render total | 252.749 / 253.190 | 254.085 / 255.686 | +| Span full materialization | 966.453 / 991.761 | 965.450 / 962.131 | +| Graph render total (control) | 2,535.972 / 2,659.396 | 2,508.129 / 2,520.813 | +| Graph full materialization (control) | 5,737.083 / 5,821.720 | 5,691.245 / 5,706.087 | + +Rendering is essentially unchanged (slightly slower in these runs), and the +small materialization differences do not establish a reliable optimization +gain given control-run drift. **The 100–200 ms materialization target remains +unmet.** This pass demonstrates removal of repeated lookups, not a large cold +latency improvement. Instrumentation/staging and reuse changed together; there +is no instrumentation-only ablation here. + +Candidate span-context phase medians across the two untraced runs: + +| Phase | Run 1 | Run 2 | +|---|---:|---:| +| Resolve visible hashes | 0.288 ms | 0.289 ms | +| Load/decode changes | **121.227 ms** | **121.281 ms** | +| Canonical hash verification / render-data extraction | **90.185 ms** | **92.773 ms** | +| Validate dependencies / resolve IDs | 1.510 ms | 1.507 ms | +| Assemble expected edits | 0.831 ms | 0.851 ms | +| Causal proof | 0.102 ms | 0.100 ms | +| Total construction | 214.623 ms | 215.533 ms | + +The load and hash phases dominate. Importantly, `Change::hash()` serializes the +complete V3 change into a temporary buffer; this phase is not just Blake3 on an +already available byte slice. Investigating a verified reader that avoids an +unnecessary decode/re-encode round trip is now higher value than another cache +for dependency or file-ID lookup. It must preserve the actual canonical identity +checks; the measurements do not authorize trusting an unchecked loader. + +Deterministic counters per candidate render context: + +```text +visible changes / dependency-list reads: 1,098 / 1,098 +expected edits: 4,278 +internal-ID database lookups: 1,096 +internal-ID cache hits: 9,058 +portable-file database lookups: 116 +portable-file cache hits: 4,162 +``` + +The original constructor read dependency lists again while building the proof +and performed individual inverse-ID / file-ID lookups. The new counters and +counting-transaction unit test demonstrate reuse without inferring it from +elapsed time. + +## Separate diagnostic run + +`imported_guest.py measure --trace-span-context` explicitly enables tracing and +labels its outcome `diagnostic-guest-cache-cold`. Do not pool it with untraced +baseline timings. Nested render metrics are available without trace output. + +Two diagnostic full-span-materialization samples each constructed **two +contexts**, costing approximately **214–217 ms apiece**. This confirms repeated +setup across the real materialization path; it does not prove that an entire +context can safely cross its transaction boundary. A next implementation should +consider reusable verified immutable data with explicit revalidation of the +relevant new snapshot, rather than blindly carrying a proof across publication. + +Remaining materialization time also includes prewarming, planning, filesystem +observations, durable operation preparation, receipts and execution; it cannot +all be called disk-write overhead by subtracting the render probe. + +## Validation and limitations + +- **3,747 core library tests passed**, none ignored. +- **1,556 repository library tests passed**, 3 existing tests ignored. +- **30 native span integration tests passed**, none ignored. +- Both benchmark example smokes passed. The imported probe smoke checks the new + nested phase/counter fields as well as bytes and operation heads. +- Five new core tests cover exact lookup counts, snapshot isolation, missing + inverse mappings, changed dependency indexes, canonical hash corruption, + deterministic error priority, bounded proofs and non-NodeId causal ordering. +- Strict core-library and imported-example Clippy passed. Broader core-test + Clippy was blocked by two pre-existing lints: the `projection_ids` test helper's + complex return type and `git_closure.rs`'s `vec![hash(1), hash(2)]`. They were not + suppressed or counted as a pass. +- **128 untraced backend samples** and **8 separate diagnostic samples** passed + selected-file byte/kind/mode and completed-head checks. Every materialization + wrote 75 files / 593,962 bytes. Git indexes were unchanged. + +These are guest-page-cache-cold observations after guest `sync` / `drop_caches=3`. +Host disk-image and physical storage caches are uncontrolled. Both backend +fixtures required explicit directory-cache repair during the earlier setup; +the ordinary fresh-import bug is still not fixed. This pass does not change +those qualifications or claim clean fresh-import certification. The demo +checkout was not modified. + +## Identities and evidence + +```text +source archive SHA-256: 55939776748db48f6cecbe10894fec7c45943480b76c4079cb99361781442463 +baseline probe SHA-256: 46de9a01e69f0182f4741eb37169c498ec45f120c9a2234ab333ac7d3139746d +candidate probe SHA-256: eb59a7732289e6fd14346d4b3a8fce02582c7a8d0cc0f57e4547ecaf0547969a +evidence archive SHA-256: 67c501a66b96c033c86a122172cd28777cc0dd22e4d7fe384593076838f1e479 +``` + +Approved host scratch directory: + +```text +/private/var/folders/hl/fwbg_jc93tvgz43rj7t25x240000gn/T/opencode/span-harness01-1gFvn7/span-context-vm-01/ +``` + +It retains source identities/archive, full test logs and +`span-context-evidence-01.tar.gz`. The evidence includes all per-sample reports, +stdout/stderr, cache-reset observations, phase/counter metrics, candidate binary, +build log, runner scripts and prepared fixture identities. The baseline binary +is pinned in the prior smoke evidence. Guest runs are retained as +`/workspace/span-context-{before,after}-0{1,2}` and +`/workspace/span-context-diagnostic-01`. diff --git a/tools/materialize-bench/SWITCH-OPTIMIZATION.md b/tools/materialize-bench/SWITCH-OPTIMIZATION.md new file mode 100644 index 00000000..cbdaf8c7 --- /dev/null +++ b/tools/materialize-bench/SWITCH-OPTIMIZATION.md @@ -0,0 +1,140 @@ +# Many-file small-switch optimization + +## Outcome + +Batching switch lease rendering removes the measured span small-switch penalty +in the synthetic many-file fixture. Two balanced before/after runs show about +**16–19% lower forward span-switch latency** and **17–18% lower backward +latency**. Graph switching stays roughly unchanged. This is not a universal +performance guarantee, and imported-history behavior was not benchmarked. + +Median end-to-end milliseconds; each cell shows the two separate run medians: + +| Case | Graph before | Graph after | Span before | Span after | +|---|---:|---:|---:|---:| +| One-file forward | 184.911 / 189.600 | 192.233 / 189.912 | 226.140 / 223.808 | 183.791 / 187.740 | +| One-file back | 169.239 / 169.110 | 166.837 / 170.054 | 203.644 / 204.697 | 167.846 / 168.150 | +| Many-file forward | 577.404 / 587.715 | 590.208 / 596.941 | 615.565 / 628.793 | 577.510 / 571.247 | +| Many-file back | 598.902 / 600.790 | 597.419 / 598.245 | 628.626 / 636.846 | 597.138 / 592.881 | + +Hot-file control (one balanced run before/after): span forward switching goes +from 126.280 to 120.804 ms and back from 125.843 to 120.651 ms. Afterward it +remains about 1.56–1.59× faster than graph. This preserves the existing hot-file +advantage; the small additional gain needs more repetitions before treating it +as a precise estimate. Materialization outside switching is unchanged by the +optimization; small differences there are controls, not claimed improvements. + +## What changed and what did not + +Previously, lease planning called `switch_target_file_bytes` for every target +file. Each call opened a read transaction, resolved visibility, projected the +namespace, and constructed a validated span history context for a span file. +Diagnostic traces showed **25 contexts for 25 files** in the many-file fixture. + +`Repository::for_each_switch_target_file` now streams target bytes under one +read snapshot and namespace projection. Ordinary span files share one lazily +constructed `SpanRenderContext`. Per-file capability/root checks and indexed +edit validation still run. Graph rendering, semantic conflict resolution, +smudge filtering and symlink handling retain their existing behavior. +Name-conflict sides retain their existing renderer and are not claimed to share +this context; these benchmarks contain no genuine conflicts. + +The visitor hashes one file's exact output into its lease at a time. No extra +full-checkout byte cache is retained. The context dies with the read snapshot; +it is not reused across views, transactions, publication, execution or recovery. +Preflight rendering, operation preparation, effect leases, receipts and +independent execution validation remain intact. Immutable authored changes, +causality and provenance are not rewritten. + +Diagnostic samples (not performance distributions) show lease-planning context +counts dropping **25 → 1**. Span file-lease planning drops from roughly +93–103 ms to 54–68 ms across the four switch directions in those traces. +These logged times include diagnostic overhead and are not substitutes for +the untraced end-to-end measurements above. + +`ATOMIC_TRACE_SWITCH` now emits namespace, preflight render, nonfile planning, +file lease, operation preparation and execution timings. +`ATOMIC_TRACE_SPAN_RENDER` emits context construction count/timing information. +Both are opt-in diagnostics; the benchmark runner strips inherited `ATOMIC_*` +variables, so diagnostic runs invoke the pinned example directly with a new +output path. Execution timing includes initial publication and preparatory +effects but not final working-copy verification; namespace timing starts after +the initial working-copy identity/state reads. The diagnostics do not account +for every part of total switch time. + +## Ordering correction + +The initial seven-sample runs revealed a strong first-position penalty in +many-file forward switches. Seven alternating samples give one backend four +first-position runs and the other three, biasing comparisons of aggregate +medians. No underlying cause of that ordering effect has been established. + +The runner now defaults to **eight** measured iterations and reports separate +first/second-position distributions. Balanced runs above use eight iterations, +one excluded warmup and two Rayon threads: four observations of each order per +backend/case. Many-file run scheduling was before → after → after → before; +hot-file scheduling was before → after. Everything ran sequentially, without +build/test load overlapping measurements. OS caches and unrelated system load +remain uncontrolled. + +The improvement holds within both forward-switch order groups: + +| Backend/version | First-position medians (two runs) | Second-position medians (two runs) | +|---|---:|---:| +| Graph before | 209.834 / 207.440 | 170.168 / 168.343 | +| Graph after | 208.683 / 210.781 | 170.082 / 171.105 | +| Span before | 245.613 / 244.396 | 204.891 / 204.594 | +| Span after | 200.767 / 205.687 | 168.992 / 169.104 | + +## Validation and retained evidence + +- Repository library: **1,556 passed, 0 failed, 3 ignored** (ignored is not pass). +- Native span integration suite: **30 passed, 0 failed, 0 ignored**. +- Benchmark smoke: passed, exercising all eight paired cases. +- Three new unit regressions cover graph/span/mixed histories, filtered bytes, + empty/missing paths, view isolation, visitor error propagation, symlink targets, + and missing roots/edits in a later file after the shared context is constructed. + Corruption failures preserve working bytes, working-copy record and head. +- Example/library Clippy, formatting and three runner unit tests passed. +- All six balanced reports validated: **768 checked backend samples** (384 + graph/span pairs), with four first/second samples per backend/case/run. + +Benchmark artifacts remain under the same approved scratch root as the initial +baseline: + +```text +/private/var/folders/hl/fwbg_jc93tvgz43rj7t25x240000gn/T/opencode/span-harness01-1gFvn7/ + switch-balanced-before-many-01/ + switch-balanced-before-many-02/ + switch-balanced-after-many-01/ + switch-balanced-after-many-02/ + switch-balanced-before-hot-01/ + switch-balanced-after-hot-01/ + switch-profile-before.{json,log} + switch-profile-after.{json,log} + switch-fix-library-tests.log + switch-fix-span-tests.log + switch-fix-smoke.log +``` + +Before executable SHA-256 (diagnostics added, original per-file planning): +`eaacb4276c37bb02bb87d33fe133ebadb30d522730a3d969307d1fde7bba8056`. +After executable SHA-256: +`578762e8faa992934c2a571927d074262efacecb3f4a5ebcc03316c2b9cc6f58`. +The after executable's fingerprinted offline release build is retained in +`switch-after-many-01/`. Balanced runs pin those exact supplied executables; +their source association is explicitly marked unverified by the runner and +correlated here by hash. Runner order-summary/default changes were made after +building the after executable; neither executable changed during these runs. + +The demo checkout was not modified. No remote push, Git-history rewrite, +validation bypass or unsupported-case pass is part of this work. + +## Next investigation + +Keep the balanced ordering and retained baselines for subsequent changes. +Many-file switch time now includes substantial non-rendering work: exact +filesystem observations, durable operation preparation and receipted execution. +Measure those before introducing another cache. The first-position effect also +deserves isolation (for example independently scheduled cases and a fixed-byte, +fixed-history file-count sweep) rather than guessing at its cause. diff --git a/tools/materialize-bench/VERIFIED-LOADING.md b/tools/materialize-bench/VERIFIED-LOADING.md new file mode 100644 index 00000000..273f5fb0 --- /dev/null +++ b/tools/materialize-bench/VERIFIED-LOADING.md @@ -0,0 +1,198 @@ +# Verified loading and operation-local decoded-data reuse + +## Result + +Matched cold-guest runs show **about 3.5× faster full span materialization** than +the preceding phase/reuse build. It is now **310–311 ms**, not yet the 200 ms +upper target. Render-only meets that latency range, but is not the acceptance +case and must not be substituted for full materialization. + +Eight measured iterations per run; cells show the two separate run medians: + +| Case/backend | Before | After | +|---|---:|---:| +| Span validated render API total | 277.436 / 277.013 ms | **150.056 / 150.909 ms** | +| Span full materialization API total | 1,083.927 / 1,083.482 ms | **311.478 / 309.741 ms** | +| Graph render API total (control) | 2,729.901 / 2,798.006 ms | 2,737.330 / 2,742.637 ms | +| Graph materialization API total (control) | 5,977.694 / 6,020.893 ms | 5,947.551 / 5,982.658 ms | + +After-change full-span p95 observations are **317.807 / 316.955 ms**. With eight +samples, nearest-rank p95 is the maximum, not a reliable tail estimate. Both +median and p95 remain above 200 ms. Scheduling was before → after → after → +before, with equal first/second backend counts within each run. No builds or +tests overlapped measurement. These are combined changes, not an isolated +ablation of each component. + +## What changed + +### Checked original-byte identity instead of decode/re-encode identity + +The filesystem store already uses `Change::deserialize`, which verifies hashed +encoded sections against the trailer and performs version-appropriate section +manifest and decoded-data validation. Span construction then called +`Change::hash()`, serializing, chunking and compressing the complete value again. + +`VerifiedChange` is an opaque, immutable witness produced either by: + +- Checked decoding of the actual original bytes, with complete consumption and + no trailing garbage; or +- Conservative canonical serialization of a decoded value for stores that do + not provide a checked encoded reader. + +It exposes no mutable change reference, and is not deserializable as a trusted +cache marker. The filesystem path additionally binds the verified hash to the +requested filename identity; a valid change placed under another hash refuses. +This verifies content identity and structure, **not an author's cryptographic +signature**. Existing signature/provenance handling remains separate. + +The original encoded identity is authoritative. A valid change with different +content chunk boundaries can have an original hash different from a fresh +writer's hash of its decoded value. The new regression verifies that the original +hash is retained, not silently normalized or rewritten. No authored object, +historical provenance, semantic ownership or change reference is replaced. + +`ChangeStore::get_verified_change` defaults to the conservative checked-value +path. Unknown/custom stores therefore do not gain an unchecked fast path. +The existing general `get_change` / filesystem decoded LRU contract is unchanged. + +### Scoped reuse with real source revalidation + +Direct journaled materialization creates a `VerifiedChangeCache` for the +operation. It shares decoded immutable data between planning and execution, +**not** a transaction, namespace projection, dependency index or causal proof. + +Every filesystem cache hit rereads the actual file and computes an exact-byte +fingerprint, including unhashed notes and signature bytes. Only a match to the +previously checked source reuses its immutable decoded value. Changed source +bytes require another complete checked decode; deletion, truncation, invalid +framing, hash corruption and wrong-identity replacement fail closed. No mtime, +size, prior receipt or global decoded LRU is sufficient to authorize reuse. +Changed valid unhashed notes are reread without rewriting the authored identity. + +The cache is limited to the visible count or 4,096 entries, whichever is smaller. +This is an **entry-count limit, not a decoded-byte budget**. Full metadata may +still be large. Further entries are verified without admission; there is no +global cache, persistent verification bit, eviction thrash or across-operation +trust. This benchmark does not certify memory use for arbitrarily large history. + +Each context still rebuilds checked dependency adjacency/causal proof and +compares span roots/edits against its own read snapshot. Filesystem effect leases, +journal preparation, receipts, execution and independent recovery remain intact. +Filtered output is not cached as if filters were immutable. The source checks +are observations, not a new cross-process filesystem lock or a complete +concurrent-mutation certification. + +### Avoid redundant whole-closure prewarming + +When span construction has already checked every visible change, materialization +does not decode the same closure again merely to populate the ordinary graph LRU. +The external-hash map remains complete. Graph-only materialization keeps its +original prewarm path; non-visible origins retain their explicit preload check. +Mixed graph files retain checked lazy content reads. A missing source is not +converted into a successful skip. + +## Phase evidence and interpretation + +Render-context phase medians across the two candidate runs: + +```text +checked load/decode: 102.328 / 102.366 ms +witness binding / render extraction: 17.543 / 17.874 ms +dependency validation: 1.649 / 1.648 ms +expected-edit assembly: 0.978 / 0.972 ms +causal proof: 0.126 / 0.129 ms +total construction: 123.329 / 123.585 ms +``` + +`SpanContextBuildMetrics.pipeline_version` is now **2**. The retained legacy +field `verify_hashes` means witness-to-request binding and render-data extraction, +including release of decoded metadata when no operation cache retains it. +Actual original-byte hashing is part of `load_changes`; generic decoded-only +stores also perform their conservative serialization check there. Do not read +the smaller `verify_hashes` number as proof that hashing was omitted, or compare +individual pipeline-1/2 phase keys as identical workloads. Totals include all +construction work and teardown; repository close also remains timed. + +The separately labeled diagnostic run confirms two contexts in each full +materialization: + +| Diagnostic sample | First context | Second context | +|---|---:|---:| +| 0 | 117.154 ms | **8.223 ms** | +| 1 | 117.037 ms | **8.076 ms** | + +Second-context checked loading is **4.87–4.90 ms**, with source-file rereads and +fingerprints still performed. Dependency/span index checks are not carried over +from the first transaction. This demonstrates reuse of the expensive decode, +not removal of validation. The same physical source can still be rejected after +planning if deleted or corrupted before execution. + +The remaining materialization time includes namespace/attribute work, lease +observations, mode probes, durable preparation, filesystem effects, receipts +and final verification. More detailed execution/journal timings are the next +useful experiment; subtracting render medians does not prove it is all disk-write +overhead. The 100–200 ms goal remains open. + +## Validation + +- **3,752 core library tests passed**, none ignored. +- **1,559 repository library tests passed**, 3 existing tests ignored. +- **30 native-span integration tests passed**, none ignored. +- Both benchmark smokes, strict core/repository library and imported-example + Clippy, and workspace check passed. +- New regressions cover immutable witness access, original non-default chunk + identity, corrupted/truncated/trailing bytes, conservative custom-store cache + revalidation, entry admission bounds, actual filesystem replacement/deletion, + changed notes, changed span indexes, and source rejection before executor + effects after a successful plan. +- **128 untraced backend samples** and **8 separate diagnostic samples** passed + byte/kind/mode and single completed-operation-head checks. Every materialization + wrote 75 files / 593,962 bytes and skipped the existing native bootstrap file. +- The retained original CLI independently passed final doctor checks in both + fixtures. Immutable object-tree digests, recorded history and Git indexes + matched their pre-benchmark identities. + +## Scope and reproducibility + +The same pinned Hyperfine tip, 1,097 first-parent commits / 75 selected files, +two vCPUs, 8 GiB guest memory, two Rayon threads and guest-local ext4 storage are +used throughout. Each fresh process follows baseline restoration, guest `sync` +and `drop_caches=3`. API totals include open/materialize-or-render/close and +exclude setup/verification; separate process-wall numbers include verification. + +Host disk-image and physical storage caches are uncontrolled: these are +**guest-page-cache-cold** results. Both fixtures remain explicitly repaired +imported states; the eight stale-directory fresh-import bug is not fixed or +counted as a pass. This does not benchmark import speed, Git-shim branch switching, +network transport or power loss. No new schema/capability or graph-to-span history +migration was introduced. The demo checkout remains untouched. + +SHA-256 identities: + +```text +source archive: 54373ccc5b0957d1f72409f89003c7f9de67f6447b5636fffeb84d9bced73735 +baseline probe: eb59a7732289e6fd14346d4b3a8fce02582c7a8d0cc0f57e4547ecaf0547969a +candidate probe: 854fe5baf9258ec36a9f03d9be12012e1ed0dc905c745d60ee79eb7d93867b10 +evidence archive: 82abd5ab11f08682fcccaa195c638ea322735172e8779e7c1d56233aa654d25d +``` + +Host scratch artifacts: + +```text +/private/var/folders/hl/fwbg_jc93tvgz43rj7t25x240000gn/T/opencode/span-harness01-1gFvn7/verified-loading-vm-01/ + source.json + source.tar.gz + core-tests.log + repository-tests.log + native-span-tests.log + example-tests.log + workspace-check.log + verified-loading-evidence-01.tar.gz +``` + +The evidence archive includes per-sample reports/stdout/stderr, cache-reset +observations, nested phase/counter metrics, candidate binary, build log/identities, +runner scripts, fixture identities and final doctor output. The pinned baseline +is retained in the preceding span-context evidence. Guest run directories are +`/workspace/verified-loading-{before,after}-0{1,2}` and +`/workspace/verified-loading-diagnostic-01`. diff --git a/tools/materialize-bench/imported_guest.py b/tools/materialize-bench/imported_guest.py new file mode 100644 index 00000000..3ae20cd5 --- /dev/null +++ b/tools/materialize-bench/imported_guest.py @@ -0,0 +1,340 @@ +#!/usr/bin/env python3 +"""Prepare and measure pinned Git imports on a disposable Linux VM disk. + +Only guest page caches are reset. Host block caches and physical storage caches +are uncontrolled. Never run against a user's repository or a host-shared mount. +""" +import argparse +import hashlib +import json +import math +import os +from pathlib import Path, PurePosixPath +import platform +import shutil +import stat +import statistics +import subprocess +import time + +MARKER = 'atomic-imported-cold-benchmark-v1' +BACKENDS = ('graph', 'span') +CASES = ('render', 'materialize_missing') + + +def digest(path): + value = hashlib.sha256() + with path.open('rb') as stream: + for block in iter(lambda: stream.read(1024 * 1024), b''): + value.update(block) + return value.hexdigest() + + +def validate_manifest(manifest): + if not manifest['files']: + raise ValueError('empty manifest') + tip = manifest['git_tip'] + if len(tip) not in (40, 64) or any(char not in '0123456789abcdef' for char in tip): + raise ValueError('invalid pinned Git tip') + paths = set() + for file in manifest['files']: + name = file['path'] + path = PurePosixPath(name) + if not name or not path.parts or path.is_absolute() or '..' in path.parts or path.parts[0] in ('.git', '.atomic') or str(path) != name: + raise ValueError('unsafe or noncanonical fixture path: ' + name) + if name in paths or file['mode'] not in ('100644', '100755', '120000'): + raise ValueError('duplicate path or unsupported mode') + if len(file['sha256']) != 64 or any(char not in '0123456789abcdef' for char in file['sha256']): + raise ValueError('invalid expected content hash') + paths.add(name) + + +def isolated_env(root): + env = {key: value for key, value in os.environ.items() + if not key.startswith(('ATOMIC_', 'GIT_', 'XDG_'))} + home = root / 'home' + home.mkdir(exist_ok=True) + env.update(HOME=str(home), ATOMIC_HOME=str(home / '.atomic'), + ATOMIC_CONFIG_DIR=str(home / 'config'), ATOMIC_SERVICE='local', + ATOMIC_SERVER_URL='http://127.0.0.1:1', RAYON_NUM_THREADS='2', + GIT_CONFIG_GLOBAL='/dev/null', GIT_CONFIG_NOSYSTEM='1', + GIT_TERMINAL_PROMPT='0', GIT_CONFIG_COUNT='1', + GIT_CONFIG_KEY_0='credential.helper', GIT_CONFIG_VALUE_0='') + return env + + +def command(args, env, cwd=None, log=None): + result = subprocess.run([str(arg) for arg in args], env=env, cwd=cwd, capture_output=True) + if log: + log.with_suffix('.stdout').write_bytes(result.stdout) + log.with_suffix('.stderr').write_bytes(result.stderr) + result.check_returncode() + return result.stdout + + +def file_bytes(path, mode): + metadata = path.lstat() + if mode == '120000': + if not stat.S_ISLNK(metadata.st_mode): + raise ValueError('not a symlink: ' + str(path)) + return os.fsencode(os.readlink(path)) + if not stat.S_ISREG(metadata.st_mode) or bool(metadata.st_mode & 0o111) != (mode == '100755'): + raise ValueError('wrong kind or mode: ' + str(path)) + return path.read_bytes() + + +def verify_checkout(repo, manifest): + for file in manifest['files']: + if hashlib.sha256(file_bytes(repo / file['path'], file['mode'])).hexdigest() != file['sha256']: + raise ValueError('checkout bytes differ: ' + file['path']) + + +def immutable_changes_digest(repo): + value = hashlib.sha256() + for path in sorted((repo / '.atomic/changes').rglob('*')): + if path.is_file(): + name = os.fsencode(str(path.relative_to(repo / '.atomic/changes'))) + value.update(len(name).to_bytes(8, 'big')) + value.update(name) + value.update(bytes.fromhex(digest(path))) + return value.hexdigest() + + +def prepare(args): + root = args.root.resolve() + root.mkdir(parents=True, exist_ok=False) + (root / 'owner.json').write_text(json.dumps({'marker': MARKER, 'root': str(root)})) + manifest = json.loads(args.manifest.read_text()) + validate_manifest(manifest) + (root / 'manifest.json').write_text(json.dumps(manifest, indent=2) + '\n') + env = isolated_env(root) + golden = root / 'golden' + golden.mkdir() + fixtures = {} + binary_sha = digest(args.cli) + for backend in BACKENDS: + repo = root / backend + command(['git', '-c', 'core.hooksPath=/dev/null', 'clone', '--no-checkout', args.bundle, repo], env) + command(['git', '-c', 'core.hooksPath=/dev/null', 'checkout', '-b', 'atomic-bench', manifest['git_tip']], env, repo) + tip = command(['git', 'rev-parse', 'HEAD'], env, repo).decode().strip() + count = int(command(['git', 'rev-list', '--first-parent', '--count', 'HEAD'], env, repo)) + if tip != manifest['git_tip'] or count != manifest['first_parent_commits']: + raise ValueError('wrong Git fixture identity') + entries = command(['git', 'ls-tree', '-r', '-z', tip], env, repo) + actual = set() + for row in filter(None, entries.split(b'\0')): + meta, path = row.split(b'\t', 1) + mode, kind, oid = meta.decode().split() + if kind != 'blob': + raise ValueError('unsupported non-blob Git entry') + actual.add((path.decode('utf-8'), mode, oid)) + expected = {(file['path'], file['mode'], file['git_blob']) for file in manifest['files']} + if actual != expected: + raise ValueError('manifest does not cover the exact pinned Git tree') + verify_checkout(repo, manifest) + index_sha = digest(repo / '.git/index') + for index, file in enumerate(manifest['files']): + blob = command(['git', 'cat-file', 'blob', file['git_blob']], env, repo) + if hashlib.sha256(blob).hexdigest() != file['sha256']: + raise ValueError('Git manifest mismatch') + target = golden / str(index) + if target.exists() and target.read_bytes() != blob: + raise ValueError('graph/span Git baseline differs') + target.write_bytes(blob) + args_import = [args.cli, 'git', 'import', '--no-vault'] + if backend == 'span': + args_import.append('--span') + started = time.monotonic() + command(args_import, env, repo, root / (backend + '-import')) + import_seconds = time.monotonic() - started + verify_checkout(repo, manifest) + if digest(repo / '.git/index') != index_sha: + raise ValueError('import changed Git index') + repair = dict(requested=args.repair_derived_indexes, performed=False) + try: + command([args.cli, 'doctor', 'check'], env, repo, root / (backend + '-doctor')) + except subprocess.CalledProcessError as error: + repair['initial_doctor_exit'] = error.returncode + if not args.repair_derived_indexes: + raise + shutil.copyfile(repo / '.atomic/atomic.redb', root / (backend + '-pre-repair.redb')) + immutable_before = immutable_changes_digest(repo) + log_before = command([args.cli, 'log', '--format', 'json'], env, repo) + command([args.cli, 'doctor', 'repair-native-indexes'], env, repo, root / (backend + '-repair')) + command([args.cli, 'doctor', 'check'], env, repo, root / (backend + '-doctor-after-repair')) + if immutable_changes_digest(repo) != immutable_before: + raise ValueError('repair changed immutable authored change files') + if command([args.cli, 'log', '--format', 'json'], env, repo) != log_before: + raise ValueError('repair changed recorded history') + verify_checkout(repo, manifest) + if digest(repo / '.git/index') != index_sha: + raise ValueError('repair changed Git index') + repair.update(performed=True, immutable_changes_sha256=immutable_before, + history_sha256=hashlib.sha256(log_before).hexdigest()) + database = repo / '.atomic/atomic.redb' + baseline = root / (backend + '-baseline.redb') + shutil.copyfile(database, baseline) + # Reset just this closed database at its original canonical location + # before each sample. Immutable changes and recovery artifacts remain. + fixtures[backend] = dict(repo=str(repo), database_sha256=digest(baseline), + database_bytes=baseline.stat().st_size, + git_index_sha256=index_sha, import_seconds=import_seconds, + derived_index_repair=repair) + if digest(args.cli) != binary_sha: + raise ValueError('import binary changed') + metadata = dict(marker=MARKER, git_tip=manifest['git_tip'], + first_parent_commits=manifest['first_parent_commits'], files=len(manifest['files']), + cli_sha256=binary_sha, bundle_sha256=digest(args.bundle), fixtures=fixtures) + (root / 'prepared.json').write_text(json.dumps(metadata, indent=2) + '\n') + print(json.dumps(metadata, indent=2)) + + +def check_owned_root(root): + owner = json.loads((root / 'owner.json').read_text()) + if owner != dict(marker=MARKER, root=str(root)): + raise ValueError('not the original disposable benchmark root') + + +def reset_fixture(root, backend, manifest, case): + repo = root / backend + shutil.copyfile(root / (backend + '-baseline.redb'), repo / '.atomic/atomic.redb') + for index, file in enumerate(manifest['files']): + path = repo / file['path'] + if path.exists() or path.is_symlink(): + if path.is_dir() and not path.is_symlink(): + raise ValueError('refusing to remove directory: ' + str(path)) + path.unlink() + if case == 'render': + path.parent.mkdir(parents=True, exist_ok=True) + content = (root / 'golden' / str(index)).read_bytes() + if file['mode'] == '120000': + path.symlink_to(os.fsdecode(content)) + else: + path.write_bytes(content) + path.chmod(0o755 if file['mode'] == '100755' else 0o644) + + +def meminfo(): + return Path('/proc/meminfo').read_text() + + +def distribution(values): + values = sorted(values) + return dict(n=len(values), median_ms=statistics.median(values), + p95_ms=values[math.ceil(.95 * len(values)) - 1], min_ms=values[0], max_ms=values[-1]) + + +def measure(args): + if platform.system() != 'Linux': + raise ValueError('guest-only Linux cache reset required') + if args.iterations < 2 or args.iterations % 2: + raise ValueError('use an even number of iterations >= 2') + root = args.root.resolve() + check_owned_root(root) + prepared = json.loads((root / 'prepared.json').read_text()) + manifest = json.loads((root / 'manifest.json').read_text()) + validate_manifest(manifest) + env = isolated_env(root) + if args.trace_span_context: + env['ATOMIC_TRACE_SPAN_RENDER'] = '1' + filesystem = command(['findmnt', '-n', '-o', 'FSTYPE,SOURCE,TARGET', '-T', root], env).decode().strip() + if filesystem.split()[0] != 'ext4': + raise ValueError('use a guest-local ext4 disk, not a host-shared mount: ' + filesystem) + output = args.output.resolve() + output.mkdir(parents=True, exist_ok=False) + binary = output / 'imported_materialize_bench' + shutil.copy2(args.probe, binary) + binary.chmod(0o700) + binary_sha = digest(binary) + samples = [] + metadata = dict(outcome='incomplete', cache_policy='sync + guest Linux drop_caches=3 before every fresh process', + host_cache_policy='uncontrolled; NOT physical-disk-cold or host-cache-cold', + filesystem=filesystem, iterations=args.iterations, rayon_threads=2, + binary_sha256=binary_sha, fixture=prepared, platform=platform.platform(), + logical_cpus=os.cpu_count(), guest_meminfo=meminfo(), + diagnostic_trace=args.trace_span_context, + timing='API total includes open, render/materialize and close; verification excluded; process wall includes verification') + try: + for iteration in range(args.iterations): + for case in CASES: + for offset in range(2): + backend = BACKENDS[(iteration + offset) % 2] + repo = root / backend + if digest(root / (backend + '-baseline.redb')) != prepared['fixtures'][backend]['database_sha256']: + raise ValueError('baseline database changed') + reset_fixture(root, backend, manifest, case) + os.sync() + before = meminfo() + Path('/proc/sys/vm/drop_caches').write_text('3\n') + after = meminfo() + stem = output / f'{iteration:02}-{case}-{backend}' + report = stem.with_suffix('.json') + command_line = [binary, '--repo', repo, '--manifest', root / 'manifest.json', + '--output', report, '--backend', backend, '--case', case] + started = time.monotonic() + command(command_line, env, log=stem) + wall = time.monotonic() - started + sample = json.loads(report.read_text()) + if sample['debug_build'] or not sample['head_verified'] or sample['verified_files'] != len(manifest['files']): + raise ValueError('unverified or debug benchmark sample') + if sample['git_tip'] != manifest['git_tip'] or sample['backend'] != backend or sample['case'] != case: + raise ValueError('wrong sample identity') + if digest(repo / '.git/index') != prepared['fixtures'][backend]['git_index_sha256']: + raise ValueError('measurement changed Git index') + sample.update(iteration=iteration, backend_order=offset, + process_wall_including_verification_seconds=wall, + meminfo_before_cache_reset=before, meminfo_after_cache_reset=after) + samples.append(sample) + if len(samples) != args.iterations * len(CASES) * len(BACKENDS) or digest(binary) != binary_sha: + raise ValueError('incomplete samples or replaced executable') + summary = {} + for case in CASES: + summary[case] = {} + for backend in BACKENDS: + selected = [sample for sample in samples if sample['case'] == case and sample['backend'] == backend] + summary[case][backend] = distribution([sample['total_ns'] / 1_000_000 for sample in selected]) + summary[case][backend]['order_ms'] = {str(order): distribution([ + sample['total_ns'] / 1_000_000 for sample in selected if sample['backend_order'] == order]) for order in (0, 1)} + phases = sorted({phase for sample in selected for phase in sample['phases_ns']}) + summary[case][backend]['phases_ms'] = {phase: distribution([ + sample['phases_ns'][phase] / 1_000_000 for sample in selected if phase in sample['phases_ns']]) for phase in phases} + context_phases = sorted({phase for sample in selected for phase in sample.get('span_context_phases_ns', {})}) + summary[case][backend]['span_context_phases_ms'] = {phase: distribution([ + sample['span_context_phases_ns'][phase] / 1_000_000 for sample in selected + if phase in sample.get('span_context_phases_ns', {})]) for phase in context_phases} + target = summary['materialize_missing']['span'] + metadata.update(outcome='diagnostic-guest-cache-cold' if args.trace_span_context else 'measured-guest-cache-cold', summary=summary, + performance_target=dict(case='materialize_missing', backend='span', upper_ms=200, + median_met=target['median_ms'] <= 200, p95_met=target['p95_ms'] <= 200)) + print(json.dumps(summary, indent=2)) + except Exception as error: + metadata.update(outcome='failed-or-invalid', error=str(error)) + raise + finally: + metadata['samples'] = samples + (output / 'run.json').write_text(json.dumps(metadata, indent=2) + '\n') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + commands = parser.add_subparsers(dest='command', required=True) + setup = commands.add_parser('prepare') + setup.add_argument('--root', type=Path, required=True) + setup.add_argument('--bundle', type=Path, required=True) + setup.add_argument('--manifest', type=Path, required=True) + setup.add_argument('--cli', type=Path, required=True) + setup.add_argument('--repair-derived-indexes', action='store_true', + help='explicit setup repair after a failed doctor check; preserve evidence and label the fixture') + run = commands.add_parser('measure') + run.add_argument('--root', type=Path, required=True) + run.add_argument('--probe', type=Path, required=True) + run.add_argument('--output', type=Path, required=True) + run.add_argument('--iterations', type=int, default=8) + run.add_argument('--trace-span-context', action='store_true', + help='emit per-context phase/counter diagnostics; label this run diagnostic, not an untraced timing baseline') + args = parser.parse_args() + prepare(args) if args.command == 'prepare' else measure(args) + + +if __name__ == '__main__': + main() diff --git a/tools/materialize-bench/run.py b/tools/materialize-bench/run.py new file mode 100644 index 00000000..9f424c2b --- /dev/null +++ b/tools/materialize-bench/run.py @@ -0,0 +1,213 @@ +#!/usr/bin/env python3 +"""Frozen, local-only graph/span materialization runs; retain raw samples and identities.""" +import argparse +from collections import defaultdict +import hashlib +import json +import math +import os +from pathlib import Path +import platform +import shutil +import statistics +import subprocess +import time + +ROOT = Path(__file__).resolve().parents[2] +PROFILES = { + 'smoke': dict(files=2, lines=8, edits=2), + 'many-files': dict(files=24, lines=128, edits=24), + 'hot-file': dict(files=1, lines=4096, edits=48), +} +CASES = ('render_session', 'render_reopened', 'materialize_noop', 'materialize_missing', + 'switch_one_forward', 'switch_one_back', 'switch_many_forward', 'switch_many_back') + + +def validate_report(report, iterations): + expected = {(case, backend, iteration) for case in CASES + for backend in ('graph', 'span') for iteration in range(iterations)} + seen = set() + for sample in report['samples']: + key = (sample['case'], sample['backend'], sample['iteration']) + if key not in expected or key in seen: + raise ValueError('unexpected or duplicate sample: ' + repr(key)) + seen.add(key) + if not isinstance(sample['total_ns'], int) or sample['total_ns'] <= 0: + raise ValueError('nonpositive or invalid sample duration') + if seen != expected: + raise ValueError('not all paired cases produced samples') + + +def digest(path): + value = hashlib.sha256() + with path.open('rb') as stream: + for block in iter(lambda: stream.read(1024 * 1024), b''): + value.update(block) + return value.hexdigest() + + +def source_fingerprint(): + """Hash source bytes directly, never Git metadata or a writable Atomic open.""" + names = ['Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', 'libatomic', 'tools'] + names += sorted(p.name for p in ROOT.iterdir() if p.is_dir() and p.name.startswith('atomic-')) + files = [] + for name in names: + path = ROOT / name + if path.is_file(): + files.append(path) + elif path.is_dir(): + for directory, dirs, entries in os.walk(path, followlinks=False): + dirs[:] = sorted(d for d in dirs if d not in ('target', '.git', '.atomic', '__pycache__')) + links = [d for d in dirs if (Path(directory) / d).is_symlink()] + files.extend(Path(directory) / d for d in links) + dirs[:] = [d for d in dirs if d not in links] + files.extend(Path(directory) / entry for entry in entries) + value = hashlib.sha256() + for path in sorted(set(files), key=lambda p: os.fsencode(str(p.relative_to(ROOT)))): + relative = os.fsencode(str(path.relative_to(ROOT))) + value.update(len(relative).to_bytes(8, 'big')) + value.update(relative) + value.update(b'L' if path.is_symlink() else b'F') + value.update(bytes([bool(path.lstat().st_mode & 0o111)])) + content = os.fsencode(os.readlink(path)) if path.is_symlink() else path.read_bytes() + value.update(len(content).to_bytes(8, 'big')) + value.update(content) + return value.hexdigest() + + +def distribution(values): + ordered = sorted(values) + return dict(n=len(values), median=statistics.median(values), mean=statistics.mean(values), + p95=ordered[max(0, math.ceil(0.95 * len(values)) - 1)], + minimum=ordered[0], maximum=ordered[-1], + stdev=statistics.stdev(values) if len(values) > 1 else 0.0) + + +def summarize(report): + groups = defaultdict(list) + phases = defaultdict(lambda: defaultdict(list)) + order_groups = defaultdict(lambda: defaultdict(list)) + for sample in report['samples']: + key = (sample['case'], sample['backend']) + groups[key].append(sample['total_ns'] / 1_000_000) + # The executable alternates backend order per iteration, including + # excluded warmups. Keep this confound visible, especially for odd n. + first = (sample['iteration'] + report['config']['warmups']) % 2 + order = 'first' if first == (0 if sample['backend'] == 'graph' else 1) else 'second' + order_groups[key][order].append(sample['total_ns'] / 1_000_000) + for phase, nanos in sample['phases_ns'].items(): + phases[key][phase].append(nanos / 1_000_000) + summary = [] + for case in sorted({key[0] for key in groups}): + entry = {'case': case} + for backend in ('graph', 'span'): + key = (case, backend) + entry[backend] = distribution(groups[key]) + entry[backend]['phases_ms'] = {name: distribution(values) for name, values in phases[key].items()} + entry[backend]['order_ms'] = {name: distribution(values) for name, values in order_groups[key].items()} + entry['graph_over_span_median'] = entry['graph']['median'] / entry['span']['median'] + summary.append(entry) + return summary + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + selection = parser.add_mutually_exclusive_group(required=True) + selection.add_argument('--build', action='store_true', help='build offline/locked/release from fingerprinted source') + selection.add_argument('--binary', type=Path, help='explicit existing materialize_bench executable (build provenance not assumed)') + parser.add_argument('--output', required=True, type=Path, help='new artifact directory; must not exist') + parser.add_argument('--profile', choices=PROFILES, default='many-files') + for name in ('files', 'lines', 'edits'): + parser.add_argument('--' + name, type=int) + parser.add_argument('--iterations', type=int, default=8) + parser.add_argument('--warmups', type=int, default=1) + parser.add_argument('--threads', type=int, default=2) + parser.add_argument('--keep-fixtures', action='store_true') + parser.add_argument('--allow-debug', action='store_true', help='correctness smoke only, not performance evidence') + args = parser.parse_args() + if args.threads < 1 or args.iterations < 1 or args.warmups < 0: + parser.error('threads/iterations must be positive and warmups nonnegative') + output = args.output.resolve() + output.mkdir(parents=True, exist_ok=False) + output.chmod(0o700) + source_before = source_fingerprint() + metadata = dict(outcome='incomplete', source_sha256=source_before, + source_binding='fingerprinted offline release build' if args.build else 'caller-supplied binary; source/build association unverified', + platform=platform.platform(), logical_cpus=os.cpu_count(), threads=args.threads, + balanced_backend_order=args.iterations % 2 == 0, + concurrency_control='OS cache and unrelated system load are not controlled') + try: + candidate = args.binary.resolve() if args.binary else None + if args.build: + command = ['cargo', 'build', '--release', '--offline', '--locked', '-p', 'atomic-repository', + '--example', 'materialize_bench', '--message-format=json-render-diagnostics'] + build_env = dict(os.environ, CARGO_NET_OFFLINE='true') + build_env.setdefault('CARGO_BUILD_JOBS', '2') + # Avoid platform-specific stripping tools; recorded here so build + # settings are explicit. This changes debug stripping, not safety. + build_env.setdefault('CARGO_PROFILE_RELEASE_STRIP', 'none') + metadata['build_command'] = command + metadata['release_strip'] = build_env['CARGO_PROFILE_RELEASE_STRIP'] + result = subprocess.run(command, cwd=ROOT, env=build_env, capture_output=True) + (output / 'build.stdout.jsonl').write_bytes(result.stdout) + (output / 'build.stderr.log').write_bytes(result.stderr) + result.check_returncode() + for line in result.stdout.splitlines(): + event = json.loads(line) + if event.get('reason') == 'compiler-artifact' and event.get('target', {}).get('name') == 'materialize_bench' and event.get('executable'): + candidate = Path(event['executable']) + if candidate is None: + raise RuntimeError('Cargo did not report the benchmark executable') + if source_fingerprint() != source_before: + raise RuntimeError('source changed during build; discard this candidate') + if candidate is None or not candidate.is_file(): + raise RuntimeError('benchmark executable not found') + binary = output / 'materialize_bench' + shutil.copy2(candidate, binary) + metadata['binary_sha256'] = digest(binary) + config = dict(PROFILES[args.profile]) + config.update({name: getattr(args, name) for name in ('files', 'lines', 'edits') if getattr(args, name) is not None}) + command = [str(binary), '--output', str(output / 'raw.json'), '--iterations', str(args.iterations), + '--warmups', str(args.warmups)] + for name, value in config.items(): + command += ['--' + name, str(value)] + if args.keep_fixtures: + command.append('--keep-fixtures') + if args.allow_debug: + command.append('--allow-debug') + home = output / 'home' + temporary = output / 'tmp' + home.mkdir() + temporary.mkdir() + env = {key: value for key, value in os.environ.items() + if not key.startswith(('ATOMIC_', 'GIT_', 'XDG_'))} + env.update(HOME=str(home), ATOMIC_HOME=str(home / '.atomic'), ATOMIC_CONFIG_DIR=str(home / 'config'), + ATOMIC_SERVICE='local', TMPDIR=str(temporary), RAYON_NUM_THREADS=str(args.threads)) + metadata['command'] = command + started = time.monotonic() + result = subprocess.run(command, cwd=ROOT, env=env, capture_output=True) + metadata['runner_elapsed_seconds'] = time.monotonic() - started + metadata['exit_code'] = result.returncode + (output / 'stdout.log').write_bytes(result.stdout) + (output / 'stderr.log').write_bytes(result.stderr) + result.check_returncode() + if source_fingerprint() != source_before or digest(binary) != metadata['binary_sha256']: + raise RuntimeError('source or pinned binary changed during measurement; results invalid') + report = json.loads((output / 'raw.json').read_text()) + validate_report(report, args.iterations) + summary = summarize(report) + metadata.update(outcome='smoke-only' if report['debug_build'] else 'measured', summary_ms=summary) + print('| Case | Graph median ms | Span median ms | Graph / span |') + print('|---|---:|---:|---:|') + for row in summary: + print(f"| {row['case']} | {row['graph']['median']:.3f} | {row['span']['median']:.3f} | {row['graph_over_span_median']:.2f}× |") + print('Artifacts:', output) + except Exception as error: + metadata.update(outcome='failed-or-invalid', error=str(error)) + raise + finally: + (output / 'run.json').write_text(json.dumps(metadata, indent=2, sort_keys=True) + '\n') + + +if __name__ == '__main__': + main() diff --git a/tools/materialize-bench/test_imported_guest.py b/tools/materialize-bench/test_imported_guest.py new file mode 100644 index 00000000..aecfad19 --- /dev/null +++ b/tools/materialize-bench/test_imported_guest.py @@ -0,0 +1,76 @@ +import copy +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest + +spec = importlib.util.spec_from_file_location('imported_guest', Path(__file__).with_name('imported_guest.py')) +guest = importlib.util.module_from_spec(spec) +spec.loader.exec_module(guest) + + +class ImportedGuestTests(unittest.TestCase): + def manifest(self): + return dict(git_tip='a' * 40, files=[dict(path='src/main.rs', mode='100644', sha256=hashlib.sha256(b'test').hexdigest())]) + + def test_manifest_refuses_unsafe_paths_and_unsupported_modes(self): + guest.validate_manifest(self.manifest()) + for name in ('', '.', '..', '../outside', '/outside', '.atomic/atomic.redb', '.git/index', 'src/../outside', 'src//main.rs'): + manifest = self.manifest() + manifest['files'][0]['path'] = name + with self.assertRaises(ValueError, msg=name): + guest.validate_manifest(manifest) + for field, value in [('mode', '160000'), ('sha256', 'not-a-hash')]: + manifest = self.manifest() + manifest['files'][0][field] = value + with self.assertRaises(ValueError): + guest.validate_manifest(manifest) + manifest = self.manifest() + manifest['files'].append(copy.deepcopy(manifest['files'][0])) + with self.assertRaises(ValueError): + guest.validate_manifest(manifest) + + def test_reset_only_touches_owned_files_and_database(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory).resolve() + with self.assertRaises(FileNotFoundError): + guest.check_owned_root(root) + (root / 'owner.json').write_text(json.dumps(dict(marker=guest.MARKER, root=str(root)))) + guest.check_owned_root(root) + repo = root / 'graph' + (repo / '.atomic').mkdir(parents=True) + (repo / 'src').mkdir() + (repo / '.atomic/atomic.redb').write_bytes(b'changed') + (root / 'graph-baseline.redb').write_bytes(b'original') + (root / 'golden').mkdir() + (root / 'golden/0').write_bytes(b'test') + unrelated = repo / 'keep-user-file' + unrelated.write_bytes(b'untouched') + manifest = self.manifest() + guest.reset_fixture(root, 'graph', manifest, 'render') + guest.verify_checkout(repo, manifest) + guest.reset_fixture(root, 'graph', manifest, 'materialize_missing') + self.assertFalse((repo / 'src/main.rs').exists()) + self.assertEqual((repo / '.atomic/atomic.redb').read_bytes(), b'original') + self.assertEqual(unrelated.read_bytes(), b'untouched') + + def test_immutable_change_digest_detects_content_and_name_changes(self): + with tempfile.TemporaryDirectory() as directory: + repo = Path(directory) + changes = repo / '.atomic/changes' + changes.mkdir(parents=True) + first = changes / 'first.change' + first.write_bytes(b'original authored bytes') + baseline = guest.immutable_changes_digest(repo) + first.write_bytes(b'altered authored bytes') + self.assertNotEqual(guest.immutable_changes_digest(repo), baseline) + first.write_bytes(b'original authored bytes') + self.assertEqual(guest.immutable_changes_digest(repo), baseline) + first.rename(changes / 'renamed.change') + self.assertNotEqual(guest.immutable_changes_digest(repo), baseline) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/materialize-bench/test_runner.py b/tools/materialize-bench/test_runner.py new file mode 100644 index 00000000..abcf58d3 --- /dev/null +++ b/tools/materialize-bench/test_runner.py @@ -0,0 +1,54 @@ +import copy +import importlib.util +from pathlib import Path +import unittest + +spec = importlib.util.spec_from_file_location('runner', Path(__file__).with_name('run.py')) +runner = importlib.util.module_from_spec(spec) +spec.loader.exec_module(runner) + + +class RunnerTests(unittest.TestCase): + def report(self): + return {'config': {'warmups': 1}, 'samples': [dict(case=case, backend=backend, iteration=i, + total_ns=(2 if backend == 'graph' else 1) * 1_000_000, + phases_ns={'render': 500_000}) + for case in runner.CASES for backend in ('graph', 'span') + for i in range(3)]} + + def test_complete_report_and_summary(self): + report = self.report() + runner.validate_report(report, 3) + summary = runner.summarize(report) + self.assertEqual(len(summary), 8) + for row in summary: + self.assertEqual(row['graph_over_span_median'], 2) + self.assertEqual(row['graph']['n'], 3) + self.assertEqual(row['span']['phases_ms']['render']['median'], .5) + self.assertEqual(row['span']['order_ms']['first']['n'], 2) + self.assertEqual(row['graph']['order_ms']['first']['n'], 1) + + def test_missing_duplicate_unknown_and_invalid_samples_fail(self): + report = self.report() + bad = copy.deepcopy(report) + bad['samples'].pop() + with self.assertRaises(ValueError): + runner.validate_report(bad, 3) + for field, value in [('case', 'unknown'), ('iteration', 9), ('total_ns', 0)]: + bad = copy.deepcopy(report) + bad['samples'][0][field] = value + with self.assertRaises(ValueError): + runner.validate_report(bad, 3) + bad = copy.deepcopy(report) + bad['samples'][-1] = bad['samples'][0] + with self.assertRaises(ValueError): + runner.validate_report(bad, 3) + + def test_nearest_rank_p95(self): + result = runner.distribution(list(range(1, 21))) + self.assertEqual(result['median'], 10.5) + self.assertEqual(result['p95'], 19) + + +if __name__ == '__main__': + unittest.main() From 6524c44c124dfde597ee76862014ff3202c5abd7 Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 10 Nov 2026 11:36:32 -0600 Subject: [PATCH 08/14] bench: partial materialization cases and Git working-copy baseline (#323) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: share one span render context per transaction Every caller of retrieve_span_if_present built a fresh SpanRenderContext, which verifies and decodes the whole visible closure, for every file it rendered. On a span import of hyperfine (1,097 commits, 75 files) one `atomic view switch` built the context 312 times, `atomic record -a` 466 times and `atomic restore -f` 157 times, at roughly 55 ms each, while the materialization itself took milliseconds. - Add SharedSpanRender: a lazily built context shared by every file read through one transaction and visibility closure. The per-file root, capability and visibility checks are unchanged; only the closure decode is shared. - Use it in project_tree, project_change_closure_with_txn and plan_file_moves, which render every projected file in one transaction. - Add Repository::get_file_contents_on_view and recorded_presence_on_view so callers that read many paths get one transaction, one projection and one context, with the absent-liveness check scoped to exactly those paths. Use them in the shim checkpoint observation, working-copy verification and the CLI and daemon restore paths, which previously rendered (and discarded) every file one at a time. Batched reads are tested byte for byte against the single-path reads for graph-origin, span-rooted, empty, directory and absent paths on two views. On the hyperfine span import, `atomic view switch` drops from 25-34 s to 4-7 s (12 context builds) and `atomic restore -f` of all 75 files from 15 s to 4.8 s (8 builds); graph-only repositories are unaffected. * fix: keep batched restore and checkpoint reads behaviour-identical Checkpoint observation no longer reads graph content for paths that carry conflict-marker bytes. Name-conflict errors carry the same path and claimant counts as the single-path reader. The daemon and CLI restore paths classify every path up front but still walk and report them in status order, so untracking, failures and output lines happen exactly where they did before. The batch-read test now asserts that the fixture really records span-rooted files. * fix: stream checkpoint and verify file contents * feat: partial materialization cases and Git working-copy baseline Add materialize_paths_noop, materialize_paths_missing and materialize_prefix_missing to the paired graph/span materialization benchmark, with per-sample subset write-count assertions, and a new git_baseline.py harness that times git checkout -- full working-tree restores on the same synthetic history shape. First paired run (macOS/aarch64, many-files, 8 iterations): partial materialize_paths restore of 8 of 25 files takes ~315 ms against ~565 ms full, while the partial noop floor (~187 ms) equals the full noop — the journaled plan/lease/journal path dominates, not content work. Span capability stays within 2% on every materialize case. Git restores the full tree in ~22 ms. * feat: agent-sandbox sync benchmark over the daemon Materialize RPC Add libatomic/examples/agent_sandbox_bench.rs plus a frozen runner: a cold, empty sandbox syncs over the production tonic stack (SandboxImpl handlers on a Unix socket, streaming Materialize with path_prefix, write_sandbox_entry receiver), with git clone over file:// as the wire-protocol comparison on the same synthetic history shape. Partial sandboxes must hold exactly the requested subtree and nothing beyond it. Recorded loopback runs: full-sync parity with git clone at both scales (66 vs 72 ms small, 190 vs 185 ms large). The atomic prefix partial holds a near-constant request phase because the handler walks the whole view closure and filters at stream time — filtering during projection is the measured next optimization. * perf: cache verified change parses in the change store load_verified_change re-read and re-parsed the change file on every call. Change files are content-addressed and write-once, so a parsed, hash-verified change is immutable for the handle's lifetime; cache it. revalidate_verified_change callers keep the old re-read-and-compare behavior, and explicit revalidation requests bypass the cache. Measured on the agent-sandbox benchmark (2,048-file span fixture, 49 changes, one 11 MB baseline): a full view render went from 34.6 s to 1.08 s — every per-file span context was re-parsing the baseline, at roughly 85 ms per render. Span partial materialize dropped from 1.13 s to 634 ms. Graph-origin paths do not consult this cache and are unchanged. * perf: share one span render context per view walk materialize_view_entries rendered each file through a fresh SpanRenderContext, and every context decodes and verifies the whole visible closure: a 2,048-file span view built 4,213 contexts per materialize (traced), ~20.7 s of combined CPU hidden by rayon parallelism. SharedSpanContext builds once per walk, lazily on the first span-rooted file, and is safe to share across the rayon render chunks (OnceLock; single-threaded OnceCell cannot cross them). Graph-origin files still fall back to the graph renderer, and callers without a shared context keep the old per-call path. Measured with the parse cache on the agent-sandbox benchmark (2,048-file span fixture): partial materialize 639 -> 341 ms, full 1,091 -> 794 ms. Span render now matches the graph backend on this fixture. * chore: drop the local git working-copy baseline harness The materialize_bench comparison set is atomic-internal: paired graph/span cases and partial-vs-full ratios. Wire-protocol Git comparison stays in the agent-sandbox benchmark, where both sides start from nothing over their real transports. * feat: COW sandbox provision case for the agent-sandbox benchmark atomic_sandbox_cow times Repository::provision_sandbox on the same synthetic fixture: the local copy-on-write clone an agent gets on a machine that already holds the repository. Measured (2,048 files, 11 MB, reflink on APFS): 276 ms median — per-file reflink syscall overhead dominates at ~0.13 ms/file, so provision time scales with file count, not bytes. Beats the RPC partial (336 ms) and git clone (779 ms); the whole-tree clonefile(2) fast path is the follow-up that would make it near-instant. --------- Co-authored-by: Vincent --- Cargo.lock | 3 + atomic-cli/src/commands/git/checkpoint.rs | 75 +- atomic-cli/src/commands/restore.rs | 152 +++- .../examples/materialize_bench.rs | 77 ++ atomic-repository/src/changestore/mod.rs | 20 + atomic-repository/src/repository/content.rs | 268 ++++++- .../src/repository/deferred_tree.rs | 27 +- .../src/repository/materialize.rs | 27 +- atomic-repository/src/repository/mod.rs | 1 + .../src/repository/project_tree.rs | 22 +- atomic-repository/src/repository/record.rs | 11 +- atomic-repository/src/repository/status.rs | 3 +- .../repository/tests/batch_content_tests.rs | 189 +++++ atomic-repository/src/repository/tests/mod.rs | 1 + atomic-repository/src/repository/verify.rs | 50 +- libatomic/Cargo.toml | 5 + libatomic/examples/agent_sandbox_bench.rs | 750 ++++++++++++++++++ libatomic/src/daemon/services.rs | 50 +- tools/materialize-bench/README.md | 24 + tools/materialize-bench/run.py | 1 + tools/materialize-bench/run_agent_sandbox.py | 170 ++++ .../test_run_agent_sandbox.py | 51 ++ tools/materialize-bench/test_runner.py | 2 +- 23 files changed, 1894 insertions(+), 85 deletions(-) create mode 100644 atomic-repository/src/repository/tests/batch_content_tests.rs create mode 100644 libatomic/examples/agent_sandbox_bench.rs create mode 100644 tools/materialize-bench/run_agent_sandbox.py create mode 100644 tools/materialize-bench/test_run_agent_sandbox.py diff --git a/Cargo.lock b/Cargo.lock index 3e1e2d6d..16cdfc26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2358,6 +2358,7 @@ dependencies = [ "chrono", "data-encoding", "futures-core", + "hyper-util", "napi", "napi-build", "napi-derive", @@ -2366,6 +2367,7 @@ dependencies = [ "protox", "rand 0.8.8", "redb 4.4.0", + "serde", "serde_json", "similar", "tempfile", @@ -2373,6 +2375,7 @@ dependencies = [ "tokio-stream", "tonic", "tonic-build", + "tower 0.4.13", "tracing", "ulid", "url", diff --git a/atomic-cli/src/commands/git/checkpoint.rs b/atomic-cli/src/commands/git/checkpoint.rs index 26b675e4..02d98c25 100644 --- a/atomic-cli/src/commands/git/checkpoint.rs +++ b/atomic-cli/src/commands/git/checkpoint.rs @@ -344,26 +344,71 @@ fn observe_atomic_on_handle( .map_err(|error| CheckpointError::Atomic(error.to_string()))? .map(|(object, markers)| (object.hash().ok(), markers)); + // Paths with marker bytes never consult the graph. Every other path is + // streamed from one read (one projection, one span context) in the same + // sorted order, and hashed as it arrives so only one file is held at a + // time; marker paths are hashed from their marker bytes in between. + let marker_bytes = |path: &str| -> Option<&[u8]> { + conflict_markers + .as_ref() + .and_then(|(_, markers)| markers.get(path)) + .map(Vec::as_slice) + }; + let graph_paths: Vec = paths + .iter() + .filter(|path| marker_bytes(path).is_none()) + .cloned() + .collect(); let mut hasher = blake3::Hasher::new(); hasher.update(ATOMIC_MANIFEST_DOMAIN); - for path in paths { - let content = match &conflict_markers { - Some((_, markers)) if markers.contains_key(&path) => { - markers.get(&path).cloned().expect("marker bytes") - } - _ => repo - .get_file_content_on_view(&path, view) - .map_err(|error| CheckpointError::Atomic(error.to_string()))? - .ok_or_else(|| { - CheckpointError::Atomic(format!( - "visible path '{path}' has no graph-derived content on view '{view}'" - )) - })?, - }; + let hash_entry = |hasher: &mut blake3::Hasher, path: &str, content: &[u8]| { hasher.update(&(path.len() as u64).to_le_bytes()); hasher.update(path.as_bytes()); hasher.update(&(content.len() as u64).to_le_bytes()); - hasher.update(&content); + hasher.update(content); + }; + let mut next = 0usize; + let mut missing: Option = None; + let streamed = repo.for_each_file_content_on_view(&graph_paths, view, |path, bytes| { + while paths.get(next).is_some_and(|pending| pending != path) { + let pending = &paths[next]; + match marker_bytes(pending) { + Some(marker) => hash_entry(&mut hasher, pending, marker), + None => { + missing = Some(pending.clone()); + return Err(atomic_repository::RepositoryError::InvalidOperation { + message: format!("visible path '{pending}' has no content"), + }); + } + } + next += 1; + } + let Some(content) = bytes else { + missing = Some(path.to_string()); + return Err(atomic_repository::RepositoryError::InvalidOperation { + message: format!("visible path '{path}' has no content"), + }); + }; + hash_entry(&mut hasher, path, &content); + next += 1; + Ok(()) + }); + if let Some(path) = missing { + return Err(CheckpointError::Atomic(format!( + "visible path '{path}' has no graph-derived content on view '{view}'" + ))); + } + streamed.map_err(|error| CheckpointError::Atomic(error.to_string()))?; + while let Some(pending) = paths.get(next) { + match marker_bytes(pending) { + Some(marker) => hash_entry(&mut hasher, pending, marker), + None => { + return Err(CheckpointError::Atomic(format!( + "visible path '{pending}' has no graph-derived content on view '{view}'" + ))) + } + } + next += 1; } Ok(CheckpointAtomicState { diff --git a/atomic-cli/src/commands/restore.rs b/atomic-cli/src/commands/restore.rs index 544db13e..97fb0dad 100644 --- a/atomic-cli/src/commands/restore.rs +++ b/atomic-cli/src/commands/restore.rs @@ -55,11 +55,12 @@ //! Would restore: src/lib.rs //! ``` +use std::collections::HashMap; use std::path::{Path, PathBuf}; use atomic_repository::tracking::TrackingOptions; use atomic_repository::{ - FileStatus, Repository, RepositoryStatus, StatusOptions, WorkspaceTxnMode, + FileStatus, RecordedPresence, Repository, RepositoryStatus, StatusOptions, WorkspaceTxnMode, }; use clap::Parser; @@ -247,6 +248,117 @@ impl Restore { } } + /// Classify and restore every Modified/Deleted path in one pass, returning + /// the outcome for each path so the caller can report them in status order. + /// + /// One projection decides which paths the view still records, and one + /// materialization writes all of them, instead of rendering and + /// materializing file by file. Outcomes match the single-file path: a path + /// the view does not project (or projects as a directory) is `Skipped`, + /// and a path with an unresolved name conflict fails with the same error. + /// A projection failure is reported for every path as the single-file read + /// would have reported it; if the batched materialization fails, every + /// path falls back to the single-file restore so outcomes stay per file. + fn restore_contents( + &self, + repo: &Repository, + working_copy: atomic_core::WorkingCopyId, + view: &str, + paths: &[PathBuf], + ) -> HashMap> { + let mut outcomes = HashMap::new(); + if paths.is_empty() { + return outcomes; + } + let names: Vec = paths + .iter() + .map(|path| path.to_string_lossy().into_owned()) + .collect(); + let presence = match repo.recorded_presence_on_view(&names, view) { + Ok(presence) => presence, + Err(error) => { + // A view or projection error would fail every single-path read + // the same way; report it per file without re-projecting. + for path in paths { + outcomes.insert( + path.clone(), + Err(CliError::Internal(anyhow::anyhow!( + "Failed to get file content: {}", + error + ))), + ); + } + return outcomes; + } + }; + let mut selected: Vec<(PathBuf, String)> = Vec::new(); + for ((path, name), presence) in paths.iter().zip(&names).zip(presence) { + match presence { + RecordedPresence::Present => selected.push((path.clone(), name.clone())), + RecordedPresence::Absent => { + outcomes.insert(path.clone(), Ok(RestoreOutcome::Skipped)); + } + RecordedPresence::NameConflict { paths, claimants } => { + let error = atomic_repository::name_conflict_error( + &atomic_repository::normalize_path(path), + paths, + claimants, + ); + outcomes.insert( + path.clone(), + Err(CliError::Internal(anyhow::anyhow!( + "Failed to get file content: {}", + error + ))), + ); + } + } + } + if selected.is_empty() { + return outcomes; + } + let batch = selected.iter().map(|(_, name)| name.clone()).collect(); + match repo.materialize_paths(working_copy, batch) { + Ok(_) => { + for (path, _) in selected { + outcomes.insert(path, Ok(RestoreOutcome::Restored)); + } + } + Err(error) => { + print_warning(&format!( + "Batched restore failed ({}); restoring file by file", + error + )); + let retry: Vec = selected.into_iter().map(|(path, _)| path).collect(); + outcomes.extend(self.restore_contents_one_by_one(repo, working_copy, view, &retry)); + } + } + outcomes + } + + fn restore_contents_one_by_one( + &self, + repo: &Repository, + working_copy: atomic_core::WorkingCopyId, + view: &str, + paths: &[PathBuf], + ) -> HashMap> { + paths + .iter() + .map(|path| { + let outcome = self.restore_file( + repo, + working_copy, + view, + Path::new(""), + path, + FileStatus::Modified, + ); + (path.clone(), outcome) + }) + .collect() + } + /// Restore a single file according to its status. /// /// - `Added` (tracked but not recorded): undo tracking. The file stays @@ -455,17 +567,39 @@ impl Command for Restore { let mut restored_count = 0; let mut error_count = 0; + // Modified/Deleted files are classified and materialized together so + // the view is projected and its span history decoded once, not once + // per file. Added files are untracked one by one. Outcomes are + // reported in status order, as before. + let content_paths: Vec = files_to_restore + .iter() + .filter(|(_, file_status)| *file_status != FileStatus::Added) + .map(|(path, _)| path.clone()) + .collect(); + let mut content_outcomes = + self.restore_contents(&repo, working_copy, &workspace_view, &content_paths); + for (path, file_status) in &files_to_restore { let path_display = path.display(); - match self.restore_file( - &repo, - working_copy, - &workspace_view, - &repo_root, - path, - *file_status, - ) { + let outcome = if *file_status == FileStatus::Added { + self.restore_file( + &repo, + working_copy, + &workspace_view, + &repo_root, + path, + *file_status, + ) + } else { + content_outcomes.remove(path).unwrap_or_else(|| { + Err(CliError::Internal(anyhow::anyhow!( + "no restore outcome recorded for '{}'", + path_display + ))) + }) + }; + match outcome { Ok(RestoreOutcome::Restored) => { println!(" Restored: {}", path_display); restored_count += 1; diff --git a/atomic-repository/examples/materialize_bench.rs b/atomic-repository/examples/materialize_bench.rs index 39253d29..9a311003 100644 --- a/atomic-repository/examples/materialize_bench.rs +++ b/atomic-repository/examples/materialize_bench.rs @@ -21,12 +21,20 @@ const CASES: &[&str] = &[ "render_reopened", "materialize_noop", "materialize_missing", + "materialize_paths_noop", + "materialize_paths_missing", + "materialize_prefix_missing", "switch_one_forward", "switch_one_back", "switch_many_forward", "switch_many_back", ]; +/// Zero-based fixture index of a generated `files/file_NNNN.txt` path. +fn fixture_file_index(path: &str) -> Option { + path.strip_prefix("files/file_")?.get(..4)?.parse().ok() +} + #[derive(Clone, Debug, Serialize, Deserialize)] struct Config { output: PathBuf, @@ -151,6 +159,13 @@ struct Fixture { repo: Option, backend: &'static str, golden: BTreeMap>, + /// Every third generated file (including the edited hot file); the + /// `materialize_paths_*` partial subset. + partial_paths: std::collections::HashSet, + /// Filename prefix selecting the first ten generated files; the + /// `materialize_prefix_missing` subset. + prefix: String, + prefix_paths: std::collections::BTreeSet, setup_ns: u128, } @@ -240,11 +255,39 @@ impl Fixture { "fixture indexes unhealthy: {:?}", healthy.problems ); + let partial_paths: std::collections::HashSet = golden["dev"] + .iter() + .filter_map(|file| { + fixture_file_index(&file.path) + .filter(|index| index % 3 == 0) + .map(|_| file.path.clone()) + }) + .collect(); + ensure!( + !partial_paths.is_empty() && partial_paths.len() < golden["dev"].len(), + "degenerate partial path subset" + ); + let prefix = "files/file_000".to_string(); + let prefix_paths: std::collections::BTreeSet = golden["dev"] + .iter() + .filter_map(|file| { + fixture_file_index(&file.path) + .filter(|index| *index < 10) + .map(|_| file.path.clone()) + }) + .collect(); + ensure!( + !prefix_paths.is_empty() && prefix_paths.len() < golden["dev"].len(), + "degenerate prefix subset" + ); let fixture = Self { directory, repo: Some(repo), backend: if span { "span" } else { "graph" }, golden, + partial_paths, + prefix, + prefix_paths, setup_ns: started.elapsed().as_nanos(), }; fixture.verify("dev")?; @@ -362,9 +405,24 @@ fn sample(fixture: &mut Fixture, case: &str, iteration: usize) -> Result fs::remove_file(fixture.directory.path().join(&file.path))?; } } + if case == "materialize_paths_missing" || case == "materialize_prefix_missing" { + // Partial analog of materialize_missing: remove only the subset the + // partial call is expected to restore. + let subset: Vec<&String> = if case == "materialize_paths_missing" { + fixture.partial_paths.iter().collect() + } else { + fixture.prefix_paths.iter().collect() + }; + for path in subset { + fs::remove_file(fixture.directory.path().join(path))?; + } + } if case == "render_reopened" { drop(fixture.repo.take()); } + // Partial path sets are cloned outside the timed region. + let partial_paths = fixture.partial_paths.clone(); + let prefix = fixture.prefix.clone(); let started = Instant::now(); if case == "render_reopened" { let opened = Instant::now(); @@ -386,9 +444,28 @@ fn sample(fixture: &mut Fixture, case: &str, iteration: usize) -> Result } else { let result = if case.starts_with("switch_") { repo.switch_view(wc, target)? + } else if case == "materialize_paths_noop" || case == "materialize_paths_missing" { + repo.materialize_paths(wc, partial_paths)? + } else if case == "materialize_prefix_missing" { + repo.materialize_prefix(wc, &prefix)? } else { repo.materialize(wc)? }; + // A partial call must actually rewrite its subset, never report the + // whole view as written and never skip deleted files. + if case == "materialize_paths_missing" || case == "materialize_prefix_missing" { + let expected = if case == "materialize_paths_missing" { + fixture.partial_paths.len() + } else { + fixture.prefix_paths.len() + }; + ensure!( + result.files_written >= expected, + "partial materialize wrote {} files, expected at least {}", + result.files_written, + expected + ); + } written = Some(result.files_written); skipped = Some(result.files_skipped); bytes_written = Some(result.bytes_written); diff --git a/atomic-repository/src/changestore/mod.rs b/atomic-repository/src/changestore/mod.rs index b3d412d0..a546053f 100644 --- a/atomic-repository/src/changestore/mod.rs +++ b/atomic-repository/src/changestore/mod.rs @@ -122,6 +122,12 @@ pub struct ChangeStore { /// concurrent read access while ensuring exclusive write access. pub(crate) cache: RwLock>, + /// Parsed, hash-verified changes keyed by hash. Change files are + /// content-addressed and write-once, so a verified parse is immutable + /// for this handle's lifetime; re-parsing an 11 MB baseline per rendered + /// file is the dominant cost of span rendering. + pub(crate) verified_cache: RwLock>, + /// Content spans held without their change files — a remote sandbox's /// cache has the bytes of the vertices it was sent, never whole changes. /// Consulted before the change files. @@ -215,6 +221,7 @@ impl ChangeStore { Ok(Self { changes_dir, cache: RwLock::new(LruCache::new(cache_capacity)), + verified_cache: RwLock::new(LruCache::new(cache_capacity)), spans: RwLock::new(SpanOverlay::default()), }) } @@ -230,6 +237,7 @@ impl ChangeStore { Ok(Self { changes_dir, cache: RwLock::new(LruCache::new(cache_capacity)), + verified_cache: RwLock::new(LruCache::new(cache_capacity)), spans: RwLock::new(SpanOverlay::default()), }) } @@ -453,6 +461,13 @@ impl ChangeStore { hash: &Hash, cached: Option<&VerifiedChange>, ) -> ChangeStoreResult { + if cached.is_none() { + if let Ok(cache) = self.verified_cache.read() { + if let Some(verified) = cache.peek(hash) { + return Ok(verified.clone()); + } + } + } let bytes = fs::read(self.change_path(hash)).map_err(|error| { if error.kind() == std::io::ErrorKind::NotFound { ChangeStoreError::NotFound { @@ -472,6 +487,11 @@ impl ChangeStore { computed: verified.hash().to_base32(), }); } + if cached.is_none() { + if let Ok(mut cache) = self.verified_cache.write() { + cache.insert(*hash, verified.clone()); + } + } Ok(verified) } diff --git a/atomic-repository/src/repository/content.rs b/atomic-repository/src/repository/content.rs index fa03ff60..c3d1b076 100644 --- a/atomic-repository/src/repository/content.rs +++ b/atomic-repository/src/repository/content.rs @@ -23,6 +23,28 @@ fn graph_visibility_at_change( graph_visibility_from_membership(txn, &membership).map(Some) } +/// Whether a view projects a file at a path, without rendering its content. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RecordedPresence { + /// The view projects a non-directory entry at this path. + Present, + /// The path is absent on the view, or projects a directory. + Absent, + /// The path has an unresolved name conflict; no single content applies. + NameConflict { paths: usize, claimants: usize }, +} + +/// The error every content read returns for a path with an unresolved name +/// conflict. Callers that classify paths first must report the same error. +pub fn name_conflict_error(path: &str, paths: usize, claimants: usize) -> RepositoryError { + RepositoryError::InvalidOperation { + message: format!( + "path '{}' has an unresolved name conflict ({} path(s), {} claimant(s)); refusing to choose content", + path, paths, claimants + ), + } +} + impl Repository { /// Get the recorded content for a tracked file. /// @@ -289,6 +311,52 @@ impl Repository { } } + /// Decide, for many paths at once, whether a view projects a file there. + /// + /// One transaction and one tree projection answer every path; nothing is + /// rendered. The rules match [`Self::get_materialized_entry_on_view`]: + /// a visible name conflict is reported instead of choosing content, and a + /// directory or an absent path is `Absent`. + pub fn recorded_presence_on_view( + &self, + paths: &[String], + view_name: &str, + ) -> Result, RepositoryError> { + let txn = self + .pristine + .read_txn() + .map_err(|e| RepositoryError::Database(e.to_string()))?; + let view = txn + .get_view(view_name) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .ok_or_else(|| RepositoryError::ViewNotFound { + name: view_name.to_string(), + })?; + let visibility = graph_visibility_closure(&txn, &view)?; + let wanted: std::collections::HashSet = paths + .iter() + .map(|path| normalize_path(Path::new(path))) + .collect(); + let projection = + self.project_tree_for_visibility_wanted(&txn, &visibility, Some(&wanted))?; + Ok(paths + .iter() + .map(|path| { + let normalized = normalize_path(Path::new(path)); + if let Some(conflict) = projection.name_conflicts.get(&normalized) { + return RecordedPresence::NameConflict { + paths: conflict.paths.len(), + claimants: conflict.sides.len(), + }; + } + match projection.present.get(&normalized) { + Some(item) if !item.is_directory => RecordedPresence::Present, + _ => RecordedPresence::Absent, + } + }) + .collect()) + } + /// Resolve a file as an explicit present/absent materialization entry on a view. /// /// Presence comes from the operation-aware path lifecycle projection. Empty @@ -315,14 +383,11 @@ impl Repository { let projection = self.project_tree_for_visibility_scoped(&txn, &visibility, Some(normalized.as_str()))?; if let Some(conflict) = projection.name_conflicts.get(&normalized) { - return Err(RepositoryError::InvalidOperation { - message: format!( - "path '{}' has an unresolved name conflict ({} path(s), {} claimant(s)); refusing to choose content", - normalized, - conflict.paths.len(), - conflict.sides.len() - ), - }); + return Err(name_conflict_error( + &normalized, + conflict.paths.len(), + conflict.sides.len(), + )); } let Some(item) = projection.present.get(&normalized) else { let inode = projection @@ -335,20 +400,84 @@ impl Repository { if item.is_directory { return Ok(MaterializedEntry::absent(normalized, Some(item.inode))); } - let bytes = - match retrieve_span_if_present(&txn, &self.change_store, item.inode, &visibility)? { - Some(bytes) => bytes, - None => retrieve_content_with_filter_fast( + let spans = SharedSpanContext::new(&txn, &self.change_store, &visibility); + let bytes = render_projected_file(&txn, &self.change_store, &spans, item, &visibility)?; + Ok(MaterializedEntry::present(normalized, item.inode, bytes)) + } + + /// Stream the recorded bytes of many files on a view through one + /// transaction, one tree projection and one shared span context. + /// + /// `visit` receives each path in the order given, with exactly what + /// [`Self::get_file_content_on_view`] returns for it: `None` for an absent + /// path or a directory. Bytes are handed over one file at a time and + /// dropped by the visitor, so peak memory stays at one file, not the whole + /// set. A requested path with an unresolved name conflict fails the call, + /// as the single-path read refuses to choose content for it; a visitor + /// error stops the walk and is returned unchanged. Nothing survives this + /// call's transaction; the visitor must not mutate repository state. + pub fn for_each_file_content_on_view( + &self, + paths: &[String], + view_name: &str, + mut visit: impl FnMut(&str, Option>) -> Result<(), RepositoryError>, + ) -> Result<(), RepositoryError> { + let txn = self + .pristine + .read_txn() + .map_err(|e| RepositoryError::Database(e.to_string()))?; + let view = txn + .get_view(view_name) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .ok_or_else(|| RepositoryError::ViewNotFound { + name: view_name.to_string(), + })?; + let visibility = graph_visibility_closure(&txn, &view)?; + let wanted: std::collections::HashSet = paths + .iter() + .map(|path| normalize_path(Path::new(path))) + .collect(); + let projection = + self.project_tree_for_visibility_wanted(&txn, &visibility, Some(&wanted))?; + let spans = SharedSpanContext::new(&txn, &self.change_store, &visibility); + for path in paths { + let normalized = normalize_path(Path::new(path)); + if let Some(conflict) = projection.name_conflicts.get(&normalized) { + return Err(name_conflict_error( + &normalized, + conflict.paths.len(), + conflict.sides.len(), + )); + } + let bytes = match projection.present.get(&normalized) { + Some(item) if !item.is_directory => Some(render_projected_file( &txn, &self.change_store, - item.inode, - item.position, - atomic_core::output::alive::RetrieveOptions::new() - .with_graph_visibility(visibility), - ) - .map_err(|error| RepositoryError::Database(error.to_string()))?, + &spans, + item, + &visibility, + )?), + _ => None, }; - Ok(MaterializedEntry::present(normalized, item.inode, bytes)) + visit(path, bytes)?; + } + Ok(()) + } + + /// Collected form of [`Self::for_each_file_content_on_view`]: one entry per + /// requested path, in order. This holds every rendered file at once, so + /// callers that only need to consume each file should stream instead. + pub fn get_file_contents_on_view( + &self, + paths: &[String], + view_name: &str, + ) -> Result>>, RepositoryError> { + let mut contents = Vec::with_capacity(paths.len()); + self.for_each_file_content_on_view(paths, view_name, |_, bytes| { + contents.push(bytes); + Ok(()) + })?; + Ok(contents) } /// Read a path through an exact assembly visibility: `view`'s validated @@ -907,6 +1036,37 @@ impl Repository { } } +/// Recorded bytes of one projected file: its span rendering when the file is +/// span-rooted on this view, otherwise the filtered graph rendering. +fn render_projected_file( + txn: &T, + store: &C, + spans: &SharedSpanContext<'_, T, C>, + item: &atomic_core::output::repo::OutputItem, + visibility: &GraphVisibilityClosure, +) -> Result, RepositoryError> +where + T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + + atomic_core::pristine::GraphTxnT + + atomic_core::pristine::InodeGraphOps + + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CapabilityTxnT, + C: atomic_core::change::ChangeStore, +{ + if let Some(bytes) = spans.render_if_present(item.inode)? { + return Ok(bytes); + } + retrieve_content_with_filter_fast( + txn, + store, + item.inode, + item.position, + atomic_core::output::alive::RetrieveOptions::new() + .with_graph_visibility(visibility.clone()), + ) + .map_err(|error| RepositoryError::Database(error.to_string())) +} + pub(super) fn retrieve_span_if_present( txn: &T, store: &C, @@ -928,6 +1088,76 @@ where .map_err(|error| RepositoryError::Database(error.to_string())) } +/// One span render context shared by every file read through a single +/// transaction and visibility closure. `retrieve_span_if_present` builds a +/// fresh context per call — which decodes and verifies the whole visible +/// closure — so a per-file caller on a large view pays the closure decode +/// thousands of times; holders of this struct pay it once. Safe to share +/// across rayon workers: first use builds, the rest reuse. +pub(super) struct SharedSpanContext<'a, T, C> +where + T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CapabilityTxnT, + C: atomic_core::change::ChangeStore, +{ + txn: &'a T, + store: &'a C, + visibility: &'a GraphVisibilityClosure, + context: std::sync::OnceLock< + Result, String>, + >, +} + +impl<'a, T, C> SharedSpanContext<'a, T, C> +where + T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn + + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CapabilityTxnT, + C: atomic_core::change::ChangeStore, +{ + pub(super) fn new( + txn: &'a T, + store: &'a C, + visibility: &'a GraphVisibilityClosure, + ) -> Self { + Self { + txn, + store, + visibility, + context: std::sync::OnceLock::new(), + } + } + + /// Render `inode` through the shared context when its span root is + /// visible; `None` means the file is graph-origin and must be read from + /// the graph. + pub(super) fn render_if_present( + &self, + inode: Inode, + ) -> Result>, RepositoryError> { + if !has_visible_span_root(self.txn, self.store, inode, self.visibility)? { + return Ok(None); + } + let context = self + .context + .get_or_init(|| { + atomic_core::pristine::span_patch_storage::SpanRenderContext::new( + self.txn, + self.store, + self.visibility, + ) + .map_err(|error| error.to_string()) + }) + .as_ref() + .map_err(|error| RepositoryError::Database(error.clone()))?; + context + .render(inode) + .map(Some) + .map_err(|error| RepositoryError::Database(error.to_string())) + } +} + pub(super) fn retrieve_span_bounded_if_present( txn: &T, store: &C, diff --git a/atomic-repository/src/repository/deferred_tree.rs b/atomic-repository/src/repository/deferred_tree.rs index cc2482ae..3b7725e6 100644 --- a/atomic-repository/src/repository/deferred_tree.rs +++ b/atomic-repository/src/repository/deferred_tree.rs @@ -1051,6 +1051,31 @@ impl Repository { visibility: &GraphVisibilityClosure, wanted: Option<&str>, ) -> Result + where + T: GraphTxnT + + TreeTxnT + + PathClaimTxnT + + atomic_core::pristine::InodeGraphOps, + { + match wanted { + Some(path) => self.project_tree_for_visibility_wanted( + txn, + visibility, + Some(&HashSet::from([path.to_string()])), + ), + None => self.project_tree_for_visibility_wanted(txn, visibility, None), + } + } + + /// Set-scoped variant of [`Self::project_tree_for_visibility_scoped`]: + /// the absent-liveness check runs only for the `wanted` paths, so a + /// batch of path reads costs what the same reads cost one by one. + pub(super) fn project_tree_for_visibility_wanted( + &self, + txn: &T, + visibility: &GraphVisibilityClosure, + wanted: Option<&HashSet>, + ) -> Result where T: GraphTxnT + TreeTxnT @@ -1105,7 +1130,7 @@ impl Repository { let mut absent_check_ms = 0u128; let visible_changes = visibility.iter_dependency_first().copied().collect(); for absent in reduced.absent { - let wanted_here = wanted.is_none_or(|path| path == absent.path.as_str()); + let wanted_here = wanted.is_none_or(|paths| paths.contains(absent.path.as_str())); if wanted_here && !absent.directory && !alive_inodes.contains(&absent.inode) diff --git a/atomic-repository/src/repository/materialize.rs b/atomic-repository/src/repository/materialize.rs index 9fb37aa9..034d697b 100644 --- a/atomic-repository/src/repository/materialize.rs +++ b/atomic-repository/src/repository/materialize.rs @@ -435,6 +435,7 @@ pub(super) fn render_name_conflict( inode_graph_table, visibility, external_hashes, + None, path, *inode, *position, @@ -455,13 +456,25 @@ pub(super) fn render_name_conflict_side( inode_graph_table: &redb::ReadOnlyMultimapTable<&'static [u8; 32], &'static [u8; 24]>, visibility: &GraphVisibilityClosure, external_hashes: &std::collections::HashMap, + shared_spans: Option< + &super::content::SharedSpanContext< + '_, + atomic_core::pristine::ReadTxn, + C, + >, + >, path: &str, inode: Inode, position: Position, ) -> Result, String> { - if let Some(bytes) = super::content::retrieve_span_if_present(txn, store, inode, visibility) - .map_err(|error| format!("{path}: span: {error}"))? - { + let span_bytes = match shared_spans { + Some(shared) => shared + .render_if_present(inode) + .map_err(|error| format!("{path}: span: {error}"))?, + None => super::content::retrieve_span_if_present(txn, store, inode, visibility) + .map_err(|error| format!("{path}: span: {error}"))?, + }; + if let Some(bytes) = span_bytes { return Ok(bytes); } use atomic_core::output::repo::{ @@ -597,6 +610,7 @@ fn prepare_name_conflict_output( inode_graph_table, visibility, external_hashes, + None, &path, side.inode, side.position, @@ -755,6 +769,7 @@ impl Repository { &inode_graph_table, &visibility, &external_hashes, + None, path, item.inode, item.position, @@ -2589,6 +2604,11 @@ impl Repository { // Preserve the streaming client's mkdir-before-file contract. paths.sort_by(|a, b| is_directory(b).cmp(&is_directory(a)).then_with(|| a.cmp(b))); let filter = crate::content_filter::GitAttributesFilter::for_repository(&self.root); + // One span context per walk: per-file contexts each decode and + // verify the whole visible closure, which on a large span-backed + // view costs more than the render itself. + let shared_spans = + super::content::SharedSpanContext::new(&txn, &self.change_store, &visibility); for chunk in paths.chunks(256) { let rendered: Vec> = chunk .par_iter() @@ -2625,6 +2645,7 @@ impl Repository { &table, &visibility, &hashes, + Some(&shared_spans), path, item.inode, item.position, diff --git a/atomic-repository/src/repository/mod.rs b/atomic-repository/src/repository/mod.rs index 51f727eb..f5cb2fef 100644 --- a/atomic-repository/src/repository/mod.rs +++ b/atomic-repository/src/repository/mod.rs @@ -44,6 +44,7 @@ use std::sync::Arc; use atomic_core::change::{Change, ChangeHeader, GraphOp}; pub use atomic_core::output::repo::MaterializedEntry; use atomic_core::output::repo::{MaterializeResult, OutputItem}; +pub use content::{name_conflict_error, RecordedPresence}; use atomic_core::pristine::{ GraphTxnT, GraphVisibilityClosure, MutTxnT, Pristine, TreeTxnT, ViewMembershipSet, ViewScope, diff --git a/atomic-repository/src/repository/project_tree.rs b/atomic-repository/src/repository/project_tree.rs index 1a945254..ff188021 100644 --- a/atomic-repository/src/repository/project_tree.rs +++ b/atomic-repository/src/repository/project_tree.rs @@ -1229,6 +1229,7 @@ impl Repository { ))); } let mut entries = Vec::new(); + let spans = super::content::SharedSpanContext::new(&txn, &self.change_store, &closure); for item in projection .present .values() @@ -1261,13 +1262,9 @@ impl Repository { ))); } let materialization = attrs.materialization; - let bytes = match super::content::retrieve_span_if_present( - &txn, - &self.change_store, - item.inode, - &closure, - ) - .map_err(ProjectTreeError::from)? + let bytes = match spans + .render_if_present(item.inode) + .map_err(ProjectTreeError::from)? { Some(content) => content, None => super::content::retrieve_content_with_filter_fast( @@ -1445,6 +1442,7 @@ impl Repository { } let mut entries = Vec::new(); let mut projected_paths = std::collections::BTreeSet::new(); + let spans = super::content::SharedSpanContext::new(txn, &self.change_store, &closure); for item in projection .present .values() @@ -1468,13 +1466,9 @@ impl Repository { let materialization = attrs.materialization; let bytes = match marker_bytes.get(item.path.as_str()) { Some(override_bytes) => override_bytes.clone(), - None => match super::content::retrieve_span_if_present( - txn, - &self.change_store, - item.inode, - &closure, - ) - .map_err(ProjectTreeError::from)? + None => match spans + .render_if_present(item.inode) + .map_err(ProjectTreeError::from)? { Some(content) => content, None => super::content::retrieve_content_with_filter_fast( diff --git a/atomic-repository/src/repository/record.rs b/atomic-repository/src/repository/record.rs index c4e13d0b..f49f1ea8 100644 --- a/atomic-repository/src/repository/record.rs +++ b/atomic-repository/src/repository/record.rs @@ -4694,11 +4694,13 @@ fn plan_file_moves( basis: MoveBasis, } + // One span context serves every deleted candidate in this planning pass. + let spans = super::content::SharedSpanContext::new(cached, store, visibility); let load_old_content = |inode: Inode, position: Position| -> Result, RecordError> { - if let Some(bytes) = - super::content::retrieve_span_if_present(cached, store, inode, visibility) - .map_err(RecordError::Repository)? + if let Some(bytes) = spans + .render_if_present(inode) + .map_err(RecordError::Repository)? { return Ok(bytes); } @@ -5075,7 +5077,8 @@ fn prepare_name_resolution( &inode_graph_table, visibility, &external_hashes, - path, + + None, path, side.inode, side.position, ) diff --git a/atomic-repository/src/repository/status.rs b/atomic-repository/src/repository/status.rs index e33514d3..8e3e49c3 100644 --- a/atomic-repository/src/repository/status.rs +++ b/atomic-repository/src/repository/status.rs @@ -507,7 +507,8 @@ impl Repository { &inode_graph_table, &visibility, &external_hashes, - &item.path, + + None, &item.path, item.inode, item.position, ) diff --git a/atomic-repository/src/repository/tests/batch_content_tests.rs b/atomic-repository/src/repository/tests/batch_content_tests.rs new file mode 100644 index 00000000..18903e9a --- /dev/null +++ b/atomic-repository/src/repository/tests/batch_content_tests.rs @@ -0,0 +1,189 @@ +use super::*; +use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; + +fn record_all(repo: &TestRepository, message: &str) { + repo.record( + ChangeHeader::new(message), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + .sync_vault(false) + .enrich_kg(false), + ) + .unwrap(); +} + +fn enable_spans(repo: &TestRepository) { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); +} + +fn write_and_add(temp: &tempfile::TempDir, repo: &mut TestRepository, path: &str, bytes: &[u8]) { + let full = temp.path().join(path); + if let Some(parent) = full.parent() { + std::fs::create_dir_all(parent).unwrap(); + } + std::fs::write(full, bytes).unwrap(); + repo.add(path, TrackingOptions::default()).unwrap(); +} + +/// Batched reads must be byte-identical to the single-path reads they replace, +/// for graph-origin files, span-rooted files, empty files, directories and +/// absent paths, on every view they are asked about. +#[test] +fn batch_reads_and_presence_match_single_path_reads() { + for spans in [false, true] { + let (temp, mut repo) = create_temp_repo(); + write_and_add(&temp, &mut repo, "legacy.txt", b"legacy\n"); + record_all(&repo, "graph origin"); + if spans { + enable_spans(&repo); + } + write_and_add(&temp, &mut repo, "a.txt", b"alpha\n"); + write_and_add(&temp, &mut repo, "b.txt", b"beta\n"); + write_and_add(&temp, &mut repo, "empty.txt", b""); + write_and_add(&temp, &mut repo, "dir/c.txt", b"gamma\n"); + record_all(&repo, "new files"); + std::fs::write(temp.path().join("b.txt"), b"beta edited\n").unwrap(); + record_all(&repo, "edit b"); + + repo.create_view_from("feature", "dev").unwrap(); + repo.switch_view("feature").unwrap(); + std::fs::write(temp.path().join("a.txt"), b"alpha on feature\n").unwrap(); + record_all(&repo, "feature edit"); + + if spans { + use atomic_core::pristine::span_patch_storage::SpanPatchReadTxn; + use atomic_core::pristine::TreeTxnT; + let txn = repo.pristine().read_txn().unwrap(); + let inode = txn.get_inode("a.txt").unwrap().unwrap(); + assert!( + txn.span_root(inode).unwrap().is_some(), + "fixture must record a.txt as a span-rooted file" + ); + let legacy = txn.get_inode("legacy.txt").unwrap().unwrap(); + assert!( + txn.span_root(legacy).unwrap().is_none(), + "fixture must keep legacy.txt graph-origin" + ); + } + + let paths: Vec = [ + "a.txt", + "b.txt", + "empty.txt", + "legacy.txt", + "dir/c.txt", + "dir", + "missing.txt", + ] + .map(str::to_string) + .into(); + for view in ["dev", "feature"] { + let batch = repo.get_file_contents_on_view(&paths, view).unwrap(); + assert_eq!(batch.len(), paths.len()); + for (path, bytes) in paths.iter().zip(&batch) { + assert_eq!( + bytes, + &repo.get_file_content_on_view(path, view).unwrap(), + "spans={spans} view={view} path={path}" + ); + } + let expected_a: &[u8] = if view == "feature" { + b"alpha on feature\n" + } else { + b"alpha\n" + }; + assert_eq!(batch[0].as_deref(), Some(expected_a)); + assert_eq!(batch[1].as_deref(), Some(b"beta edited\n".as_slice())); + assert_eq!(batch[2].as_deref(), Some(b"".as_slice())); + assert_eq!(batch[3].as_deref(), Some(b"legacy\n".as_slice())); + assert_eq!(batch[4].as_deref(), Some(b"gamma\n".as_slice())); + assert_eq!(batch[5], None, "directories have no content"); + assert_eq!(batch[6], None, "absent paths have no content"); + + let presence = repo.recorded_presence_on_view(&paths, view).unwrap(); + let expected: Vec = batch + .iter() + .map(|bytes| { + if bytes.is_some() { + RecordedPresence::Present + } else { + RecordedPresence::Absent + } + }) + .collect(); + assert_eq!(presence, expected, "spans={spans} view={view}"); + } + } +} + +/// An unknown view fails the batch exactly like the single-path read. +#[test] +fn batch_reads_reject_unknown_views() { + let (temp, mut repo) = create_temp_repo(); + write_and_add(&temp, &mut repo, "a.txt", b"alpha\n"); + record_all(&repo, "one file"); + let paths = vec!["a.txt".to_string()]; + assert!(matches!( + repo.get_file_contents_on_view(&paths, "nope"), + Err(RepositoryError::ViewNotFound { .. }) + )); + assert!(matches!( + repo.recorded_presence_on_view(&paths, "nope"), + Err(RepositoryError::ViewNotFound { .. }) + )); + assert_eq!(repo.get_file_contents_on_view(&[], "dev").unwrap(), vec![]); +} + +/// The streaming read visits every path in the requested order with the same +/// bytes the collected read returns, and a visitor error stops the walk and +/// comes back unchanged. +#[test] +fn streaming_reads_visit_in_order_and_propagate_visitor_errors() { + for spans in [false, true] { + let (temp, mut repo) = create_temp_repo(); + if spans { + enable_spans(&repo); + } + write_and_add(&temp, &mut repo, "b.txt", b"beta\n"); + write_and_add(&temp, &mut repo, "a.txt", b"alpha\n"); + write_and_add(&temp, &mut repo, "dir/c.txt", b"gamma\n"); + record_all(&repo, "files"); + let paths: Vec = ["dir/c.txt", "missing.txt", "a.txt", "dir", "b.txt"] + .map(str::to_string) + .into(); + let collected = repo.get_file_contents_on_view(&paths, "dev").unwrap(); + let mut visited = Vec::new(); + repo.for_each_file_content_on_view(&paths, "dev", |path, bytes| { + visited.push((path.to_string(), bytes)); + Ok(()) + }) + .unwrap(); + let visited_paths: Vec<&str> = visited.iter().map(|(path, _)| path.as_str()).collect(); + assert_eq!( + visited_paths, + paths.iter().map(String::as_str).collect::>() + ); + let visited_bytes: Vec>> = + visited.into_iter().map(|(_, bytes)| bytes).collect(); + assert_eq!(visited_bytes, collected, "spans={spans}"); + + let mut seen = Vec::new(); + let error = repo + .for_each_file_content_on_view(&paths, "dev", |path, _| { + seen.push(path.to_string()); + if path == "a.txt" { + return Err(RepositoryError::InvalidOperation { + message: "visitor refused".into(), + }); + } + Ok(()) + }) + .unwrap_err(); + assert!(error.to_string().contains("visitor refused"), "{error}"); + assert_eq!(seen, ["dir/c.txt", "missing.txt", "a.txt"], "spans={spans}"); + } +} diff --git a/atomic-repository/src/repository/tests/mod.rs b/atomic-repository/src/repository/tests/mod.rs index 80d3a470..0a505b6f 100644 --- a/atomic-repository/src/repository/tests/mod.rs +++ b/atomic-repository/src/repository/tests/mod.rs @@ -9,6 +9,7 @@ use std::path::Path; use tempfile::TempDir; mod attribute_tests; +mod batch_content_tests; mod binding_fetch_tests; mod binding_store_tests; mod binding_verify_tests; diff --git a/atomic-repository/src/repository/verify.rs b/atomic-repository/src/repository/verify.rs index b41df533..67f6c92f 100644 --- a/atomic-repository/src/repository/verify.rs +++ b/atomic-repository/src/repository/verify.rs @@ -138,6 +138,38 @@ impl Repository { // Files visible (tracked + recorded) on the current view. let visible = self.visible_file_paths(&desired_view)?; + // Clean files are compared against the graph. Stream them through one + // transaction so the view's span history is decoded once, comparing + // each file as it arrives; only the mismatching lengths are kept and + // reported below in the visible-path order. + let is_uncommitted_or_conflicted = |path: &String| { + matches!( + status_by_path.get(path), + Some(FileStatus::Modified) + | Some(FileStatus::Added) + | Some(FileStatus::Deleted) + | Some(FileStatus::Conflicted) + ) + }; + let clean_paths: Vec = visible + .iter() + .filter(|path| !is_uncommitted_or_conflicted(path)) + .cloned() + .collect(); + let mut drift: HashMap = HashMap::new(); + let mut clean_files_checked = 0usize; + self.for_each_file_content_on_view(&clean_paths, &desired_view, |path, graph| { + let disk = std::fs::read(self.root.join(path)).ok(); + if let (Some(disk), Some(graph)) = (disk, graph) { + clean_files_checked += 1; + if disk != graph { + drift.insert(path.to_string(), (disk.len(), graph.len())); + } + } + Ok(()) + })?; + report.clean_files_checked += clean_files_checked; + for path in &visible { let st = status_by_path.get(path).copied(); @@ -154,18 +186,12 @@ impl Repository { // Clean (absent from status) or other benign states: the disk // must match the graph exactly. _ => { - let abs = self.root.join(path); - let disk = std::fs::read(&abs).ok(); - let graph = self.get_file_content_on_view(path, &desired_view)?; - if let (Some(disk), Some(graph)) = (disk.as_ref(), graph.as_ref()) { - report.clean_files_checked += 1; - if disk != graph { - report.problems.push(VerifyProblem::MaterializationDrift { - path: path.clone(), - disk_len: disk.len(), - graph_len: graph.len(), - }); - } + if let Some((disk_len, graph_len)) = drift.remove(path.as_str()) { + report.problems.push(VerifyProblem::MaterializationDrift { + path: path.clone(), + disk_len, + graph_len, + }); } } } diff --git a/libatomic/Cargo.toml b/libatomic/Cargo.toml index 0f26e962..a64ca7c8 100644 --- a/libatomic/Cargo.toml +++ b/libatomic/Cargo.toml @@ -57,6 +57,11 @@ url = { workspace = true } # Loopback protocol acceptance harness only; no production listener is added. axum = { version = "0.7", default-features = false, features = ["http1", "tokio"] } tempfile = { workspace = true } +serde = { workspace = true, features = ["derive"] } +# agent_sandbox_bench: the real tonic client stack over a Unix socket. +hyper-util = { workspace = true } +tower = { workspace = true } +tokio-stream = { workspace = true, features = ["net"] } [build-dependencies] napi-build = { version = "2", optional = true } diff --git a/libatomic/examples/agent_sandbox_bench.rs b/libatomic/examples/agent_sandbox_bench.rs new file mode 100644 index 00000000..9aeb3771 --- /dev/null +++ b/libatomic/examples/agent_sandbox_bench.rs @@ -0,0 +1,750 @@ +//! Agent-sandbox sync benchmark: cold sandbox over the daemon RPC versus Git +//! over its wire protocol. +//! +//! The scenario: an agent sandbox starts with nothing and fetches the files +//! it needs. Three matched cases on the same synthetic history (two directory +//! subtrees plus an empty file, one commit per hot-file edit): +//! +//! - `atomic_rpc_full` — a fresh sandbox connects to a Unix-socket tonic +//! server hosting the production `SandboxImpl` handler and streams the +//! whole view via the `Materialize` RPC (no prefix), writing every entry. +//! - `atomic_rpc_partial` — the same `Materialize` RPC with `path_prefix` +//! set to one subtree; only that subtree's entries and their ancestor +//! directories may be streamed. +//! - `git_clone_full` — `git clone` of a bare repository over the `file://` +//! wire transport. +//! - `atomic_sandbox_cow` — `Repository::provision_sandbox`, the local +//! copy-on-write clone an agent gets on a machine that already holds the +//! repository (reflink where the filesystem supports it). No wire, no +//! RPC: the instant-workspace path. +//! +//! The atomic side exercises the real transport end to end: a fresh channel +//! per sample (a sandbox never reuses a connection), the generated client, +//! the production handlers against a real repository, and the real +//! `write_sandbox_entry` receiver. Only the `atomicd` shell is absent; the +//! socket is bound directly. Only the sandbox token/middleware layer is +//! bypassed (no token is presented, so the handlers run as the trusted +//! local caller). +//! +//! Timing boundaries: the timed region starts at the client connect (or the +//! first Git subprocess spawn) and ends when the last entry is written (or +//! Git exits). Fixtures, server startup, and byte verification are outside +//! timing. There is no network RTT: the atomic transport is a loopback Unix +//! socket and Git uses loopback `file://` pack negotiation; treat absolute +//! numbers as transport-plus-work measurements, not WAN estimates. +use std::collections::BTreeMap; +use std::fs; +use std::io::Write; +use std::os::unix::net::UnixListener as StdUnixListener; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::sync::Arc; +use std::time::Instant; + +use anyhow::{bail, ensure, Context, Result}; +use libatomic::atomic::{sandbox_service_client::SandboxServiceClient, MaterializeRequest}; +use libatomic::daemon::services_sandbox::SandboxImpl; +use libatomic::daemon::state::DaemonState; +use serde::{Deserialize, Serialize}; +use tokio_stream::wrappers::UnixListenerStream; +use tonic::transport::{Endpoint, Uri}; +use tower::service_fn; +use hyper_util::rt::TokioIo; + +const CASES: &[&str] = &[ + "atomic_rpc_full", + "atomic_rpc_partial", + "atomic_sandbox_cow", + "git_clone_full", +]; + +/// The partial subtree every partial case fetches: `files/east/`. +const PARTIAL_PREFIX: &str = "files/east"; + +#[derive(Clone, Debug, Serialize, Deserialize)] +struct Config { + output: PathBuf, + files_east: usize, + files_west: usize, + lines: usize, + edits: usize, + iterations: usize, + warmups: usize, + allow_debug: bool, + keep_fixtures: bool, + span: bool, +} + +impl Config { + fn parse() -> Result { + let mut config = Self { + output: "agent-sandbox-bench.json".into(), + files_east: 8, + files_west: 16, + lines: 128, + edits: 24, + iterations: 8, + warmups: 1, + allow_debug: false, + keep_fixtures: false, + span: false, + }; + let mut args = std::env::args().skip(1); + while let Some(arg) = args.next() { + match arg.as_str() { + "--allow-debug" => config.allow_debug = true, + "--span" => config.span = true, + "--keep-fixtures" => config.keep_fixtures = true, + "--output" => config.output = args.next().context("--output needs a path")?.into(), + "--files-east" => { + config.files_east = args.next().context("--files-east needs a value")?.parse()? + } + "--files-west" => { + config.files_west = args.next().context("--files-west needs a value")?.parse()? + } + "--lines" => config.lines = args.next().context("--lines needs a value")?.parse()?, + "--edits" => config.edits = args.next().context("--edits needs a value")?.parse()?, + "--iterations" => { + config.iterations = args.next().context("--iterations needs a value")?.parse()? + } + "--warmups" => { + config.warmups = args.next().context("--warmups needs a value")?.parse()? + } + _ => bail!("unknown argument: {arg}"), + } + } + config.validate()?; + Ok(config) + } + + fn validate(&self) -> Result<()> { + ensure!( + self.files_east > 0 + && self.files_west > 0 + && self.lines >= 2 + && self.iterations > 0, + "file counts and iterations must be positive; lines must be at least two" + ); + ensure!( + self.files_east + self.files_west <= 1024 + && self.lines <= 100_000 + && self.edits <= 10_000, + "fixture exceeds benchmark safety bounds" + ); + ensure!( + self.iterations <= 1000 + && self.warmups <= 1000 + && self + .files_east + .checked_add(self.files_west) + .is_some_and(|n| n.checked_mul(self.lines).is_some_and(|n| n <= 2_000_000)), + "sample count or total fixture lines exceed safety bounds" + ); + ensure!( + !cfg!(debug_assertions) || self.allow_debug, + "use a --release build (or --allow-debug for correctness smoke tests only)" + ); + ensure!( + !self.output.exists(), + "refusing to overwrite an existing output report" + ); + Ok(()) + } +} + +// ————————————————————————————— fixture ————————————————————————————— + +fn generated_line(tree: &str, file: usize, line: usize) -> String { + format!("{tree} file_{file:04} line_{line:06} value_00000000\n") +} + +fn edited_line(line: usize, edit: usize) -> String { + format!("east file_0000 line_{line:06} value_{edit:08}\n") +} + +/// One tracked file's golden bytes. +#[derive(Clone)] +struct Golden { + path: String, + content: Vec, +} + +fn build_golden(config: &Config) -> Vec { + let mut golden = Vec::new(); + for (tree, count) in [ + ("east", config.files_east), + ("west", config.files_west), + ] { + for file in 0..count { + golden.push(Golden { + path: format!("files/{tree}/file_{file:04}.txt"), + content: (0..config.lines) + .map(|line| generated_line(tree, file, line)) + .collect::() + .into_bytes(), + }); + } + } + golden.push(Golden { + path: "empty.txt".into(), + content: Vec::new(), + }); + golden +} + +fn east_paths(config: &Config) -> Vec { + (0..config.files_east) + .map(|file| format!("files/east/file_{file:04}.txt")) + .collect() +} + +fn git(root: &Path, args: &[&str], tick: Option) -> Result<()> { + let output = Command::new("git") + .args(args) + .current_dir(root) + .env("GIT_AUTHOR_NAME", "bench") + .env("GIT_AUTHOR_EMAIL", "bench@example.invalid") + .env("GIT_COMMITTER_NAME", "bench") + .env("GIT_COMMITTER_EMAIL", "bench@example.invalid") + .env( + "GIT_AUTHOR_DATE", + format!("{} +0000", 1_600_000_000 + tick.unwrap_or(0)), + ) + .env( + "GIT_COMMITTER_DATE", + format!("{} +0000", 1_600_000_000 + tick.unwrap_or(0)), + ) + .output() + .with_context(|| format!("running git {}", args.join(" ")))?; + ensure!( + output.status.success(), + "git {} failed: {}", + args.join(" "), + String::from_utf8_lossy(&output.stderr) + ); + Ok(()) +} + +/// Build the atomic host repository and the Git bare repository with the +/// same synthetic content. Returns the atomic golden and the bare path. +fn prepare_fixtures( + parent: &Path, + config: &Config, +) -> Result<(tempfile::TempDir, Vec, PathBuf)> { + let directory = tempfile::Builder::new() + .prefix("agb") + .tempdir_in(parent)?; + let root = directory.path(); + let golden = build_golden(config); + + // Atomic host repository. Writes happen through the ordinary record path + // so the served graph is a real repository, not a canned fixture. + let repo = atomic_repository::Repository::init(root.join("host"))?; + if config.span { + use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; + let mut txn = repo.pristine().write_txn()?; + txn.put_required_capability(SPAN_RANGE_CAPABILITY)?; + txn.commit()?; + } + fs::create_dir_all(root.join("host/files/east"))?; + fs::create_dir_all(root.join("host/files/west"))?; + for file in &golden { + fs::write(root.join("host").join(&file.path), &file.content)?; + } + let wc = repo.require_working_copy_id()?; + repo.add(wc, "files", Default::default())?; + repo.add(wc, "empty.txt", Default::default())?; + record(&repo, "synthetic baseline", 0)?; + // Hot-file edits: every edit rewrites the east hot file and records it, + // exactly one commit per edit to match the Git history's shape. + let hot_path = root.join("host/files/east/file_0000.txt"); + let mut hot: Vec = (0..config.lines) + .map(|l| generated_line("east", 0, l)) + .collect(); + for edit in 0..config.edits { + let line = (edit * 17) % config.lines; + hot[line] = edited_line(line, edit + 1); + let content = hot.concat(); + fs::write(&hot_path, &content)?; + record(&repo, "synthetic hot-file edit", edit + 1)?; + } + let hot = hot.concat(); + // The golden east hot file must reflect the final edit, not the baseline. + let mut golden = golden; + let golden_east_hot = golden + .iter_mut() + .find(|f| f.path == "files/east/file_0000.txt") + .context("missing east hot file in golden")?; + golden_east_hot.content = hot.into_bytes(); + drop(repo); + + // Git bare repository with the same shape: one baseline commit, one + // commit per hot-file edit. The seed must start from the baseline + // content — the golden's hot file already reflects the edits. + let seed = root.join("git-seed"); + fs::create_dir_all(seed.join("files/east"))?; + fs::create_dir_all(seed.join("files/west"))?; + let baseline_hot: Vec = golden + .iter() + .map(|file| { + if file.path == "files/east/file_0000.txt" { + Golden { + path: file.path.clone(), + content: (0..config.lines) + .map(|line| generated_line("east", 0, line)) + .collect::() + .into_bytes(), + } + } else { + file.clone() + } + }) + .collect(); + for file in &baseline_hot { + fs::write(seed.join(&file.path), &file.content)?; + } + git(&seed, &["init", "-q", "-b", "dev"], None)?; + git(&seed, &["add", "-A"], None)?; + git(&seed, &["commit", "-q", "-m", "synthetic baseline"], Some(0))?; + let seed_hot = seed.join("files/east/file_0000.txt"); + for edit in 0..config.edits { + let line = (edit * 17) % config.lines; + let mut lines: Vec = fs::read_to_string(&seed_hot)? + .split_inclusive('\n') + .map(String::from) + .collect(); + lines[line] = edited_line(line, edit + 1); + fs::write(&seed_hot, lines.concat())?; + git(&seed, &["add", "-A"], None)?; + git(&seed, &["commit", "-q", "-m", "synthetic hot-file edit"], Some(edit + 1))?; + } + let bare = root.join("host-bare.git"); + git( + root, + &["clone", "-q", "--bare", "git-seed", "host-bare.git"], + None, + )?; + // Both histories must have replayed to byte-identical trees. + let git_hot = fs::read(&seed_hot)?; + ensure!( + golden + .iter() + .find(|f| f.path == "files/east/file_0000.txt") + .is_some_and(|f| f.content == git_hot), + "atomic and Git fixture hot files diverged" + ); + Ok((directory, golden, bare)) +} + +fn record(repo: &atomic_repository::Repository, message: &str, tick: usize) -> Result<()> { + use atomic_core::change::ChangeHeader; + use atomic_repository::RecordOptions; + let mut header = ChangeHeader::new(message); + header.timestamp = chrono::DateTime::from_timestamp(1_600_000_000 + tick as i64, 0).unwrap(); + repo.record( + repo.require_working_copy_id()?, + header, + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + .sync_vault(false) + .enrich_kg(false), + )?; + Ok(()) +} + +// ————————————————————————————— server ————————————————————————————— + +/// Bind the production sandbox handlers on a private Unix socket. This is +/// the `atomicd` shape minus the shell: one `DaemonState`, one registered +/// repository, the generated tonic server. +fn serve( + state: Arc, + host_root: PathBuf, + socket: PathBuf, +) -> Result>> { + use libatomic::atomic::sandbox_service_server::SandboxServiceServer; + + let std_listener = StdUnixListener::bind(&socket) + .with_context(|| format!("binding {}", socket.display()))?; + std_listener.set_nonblocking(true)?; + // Fail fast if the repository cannot open; the server would otherwise + // serve errors that look like client bugs. The registration also gives + // the caller the repository ref the handlers will resolve. + let handle = state.register(host_root); + handle.repository()?; + Ok(std::thread::spawn(move || { + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .enable_all() + .build() + .map_err(|e| anyhow::anyhow!("server runtime: {e}"))?; + runtime.block_on(async move { + let listener = tokio::net::UnixListener::from_std(std_listener)?; + let incoming = UnixListenerStream::new(listener); + tonic::transport::Server::builder() + .add_service(SandboxServiceServer::new(SandboxImpl { state })) + .serve_with_incoming(incoming) + .await + .map_err(|e| anyhow::anyhow!("tonic server: {e}")) + }) + })) +} + +// ————————————————————————————— client ————————————————————————————— + +async fn connect(socket: &Path) -> Result> { + let socket = socket.to_path_buf(); + let channel = Endpoint::from_static("http://localhost") + .connect_with_connector(service_fn(move |_: Uri| { + let socket = socket.clone(); + async move { + let stream = tokio::net::UnixStream::connect(socket).await?; + Ok::<_, std::io::Error>(TokioIo::new(stream)) + } + })) + .await?; + Ok(SandboxServiceClient::new(channel).max_decoding_message_size(64 * 1024 * 1024)) +} + +/// One cold sandbox materialize: fresh channel, `Materialize` RPC, write +/// every streamed entry. Connect, request and stream run on one runtime — +/// a tonic channel is bound to the runtime that created it. +fn atomic_sandbox( + socket: &Path, + repository: libatomic::atomic::RepositoryRef, + target: libatomic::atomic::ViewRef, + prefix: Option, + sandbox: &Path, +) -> Result<(BTreeMap, usize)> { + use libatomic::daemon::sandbox_wire; + + let mut phases = BTreeMap::new(); + let sandbox = sandbox.to_path_buf(); + let socket = socket.to_path_buf(); + let (written, inner) = std::thread::spawn(move || -> Result<(usize, BTreeMap)> { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .map_err(anyhow::Error::msg)?; + runtime.block_on(async move { + use tokio_stream::StreamExt; + let mut phases = BTreeMap::new(); + let started = Instant::now(); + let mut client = connect(&socket).await?; + phases.insert("connect".into(), started.elapsed().as_nanos()); + let started = Instant::now(); + let mut stream = client + .materialize(MaterializeRequest { + repository: Some(repository), + target: Some(target), + path_prefix: prefix, + }) + .await? + .into_inner(); + phases.insert("request".into(), started.elapsed().as_nanos()); + let started = Instant::now(); + let mut written = 0usize; + loop { + let frame = stream + .next() + .await + .ok_or_else(|| anyhow::anyhow!("materialize ended without its summary"))? + .map_err(anyhow::Error::msg)?; + match frame.frame { + Some(libatomic::atomic::materialize_frame::Frame::Entry(entry)) => { + atomic_repository::write_sandbox_entry( + &sandbox, + &entry.path, + sandbox_wire::tree_entry_kind(&entry).map_err(anyhow::Error::msg)?, + entry.mode.unwrap_or(0o644), + entry.inline.as_deref().unwrap_or_default(), + )?; + written += 1; + } + Some(libatomic::atomic::materialize_frame::Frame::Done(done)) => { + ensure!( + done.entries as usize == written, + "the server sent {written} entries but said {}", + done.entries + ); + break; + } + None => bail!("an empty materialize frame"), + } + } + phases.insert("stream_and_write".into(), started.elapsed().as_nanos()); + Ok((written, phases)) + }) + }) + .join() + .map_err(|_| anyhow::anyhow!("client thread panicked"))??; + phases.extend(inner); + Ok((phases, written)) +} + +// ————————————————————————————— git cases ————————————————————————————— + +fn git_clone_full(bare: &Path, sandbox: &Path) -> Result> { + let mut phases = BTreeMap::new(); + let started = Instant::now(); + git( + sandbox.parent().unwrap_or(Path::new("/")), + &[ + "clone", + "-q", + &format!("file://{}", bare.display()), + &sandbox.display().to_string(), + ], + None, + )?; + phases.insert("clone".into(), started.elapsed().as_nanos()); + Ok(phases) +} + +// ————————————————————————————— verification ————————————————————————————— + +fn file_mode(path: &Path) -> Result { + use std::os::unix::fs::PermissionsExt; + Ok(fs::metadata(path)?.permissions().mode() & 0o7777) +} + +/// Full sandboxes hold every tracked file; partial sandboxes hold exactly +/// the east subtree and nothing else from the west or the root. +fn verify(sandbox: &Path, golden: &[Golden], partial: bool, east: &[String]) -> Result<()> { + if partial { + for path in east { + let content = golden + .iter() + .find(|f| &f.path == path) + .context("east path missing from golden")?; + let target = sandbox.join(path); + ensure!(target.is_file(), "partial sandbox misses {}", path); + ensure!( + fs::read(&target)? == content.content, + "partial sandbox bytes differ: {path}" + ); + } + for file in golden { + if !east.contains(&file.path) { + ensure!( + !sandbox.join(&file.path).exists(), + "partial sandbox fetched beyond its prefix: {}", + file.path + ); + } + } + return Ok(()); + } + for file in golden { + let target = sandbox.join(&file.path); + ensure!(target.is_file(), "sandbox misses {}", file.path); + ensure!( + fs::read(&target)? == file.content, + "sandbox bytes differ: {}", + file.path + ); + ensure!( + file_mode(&target)? == 0o644 || file.content.is_empty(), + "sandbox mode differs: {}", + file.path + ); + } + Ok(()) +} + +// ————————————————————————————— harness ————————————————————————————— + +#[derive(Debug, Serialize)] +struct Sample { + case: String, + iteration: usize, + total_ns: u128, + phases_ns: BTreeMap, + entries: Option, + verified_files: usize, + output_bytes: usize, +} + +fn run(config: Config) -> Result<()> { + config.validate()?; + let parent = config + .output + .parent() + .filter(|p| !p.as_os_str().is_empty()) + .unwrap_or(Path::new(".")); + fs::create_dir_all(parent)?; + ensure!( + Command::new("git") + .arg("--version") + .output() + .map(|o| o.status.success()) + .unwrap_or(false), + "git is required for the Git comparison cases" + ); + let (fixtures, golden, bare) = prepare_fixtures(parent, &config)?; + let host_root = fixtures.path().join("host"); + let socket = fixtures.path().join("s.sock"); + ensure!( + socket.to_string_lossy().len() <= 100, // sun_path limit is 104; leave slack + "socket path too long for a Unix socket: {} — use a shorter --output directory", + socket.display() + ); + let state = Arc::new(DaemonState::new()); + let handle = state.register(host_root.clone()); + let repository = handle.repository_ref().repository_id; + handle.repository()?; // fail fast; the server would serve errors otherwise + let server = serve(state, host_root.clone(), socket.clone())?; + let east = east_paths(&config); + let east_bytes: usize = golden + .iter() + .filter(|f| east.contains(&f.path)) + .map(|f| f.content.len()) + .sum(); + + let mut samples = Vec::new(); + let result = (|| -> Result<()> { + for iteration in 0..config.warmups + config.iterations { + for case in CASES { + let partial = case.contains("partial"); + let sandbox = fixtures.path().join(format!("sandbox-{case}-{iteration}")); + // Store open is setup, not measured: the COW clone is the + // operation under test. + let cow_repo = (*case == "atomic_sandbox_cow").then(|| { + atomic_repository::Repository::open_existing_wait( + &host_root, + std::time::Duration::from_secs(30), + ) + .expect("open host repository") + }); + let started = Instant::now(); + let (phases, entries) = match *case { + "atomic_rpc_full" | "atomic_rpc_partial" => { + let target = libatomic::atomic::ViewRef { + view_id: Vec::new(), + name: Some("dev".into()), + }; + let reference = libatomic::atomic::RepositoryRef { + authority: "local".into(), + repository_id: repository.clone(), + workspace_id: None, + }; + let (phases, entries) = atomic_sandbox( + &socket, + reference, + target, + partial.then(|| PARTIAL_PREFIX.to_string()), + &sandbox, + )?; + (phases, Some(entries)) + } + "atomic_sandbox_cow" => { + let repo = cow_repo.as_ref().unwrap(); + let wc = repo.require_working_copy_id()?; + let written = repo.provision_sandbox(wc, &sandbox, "dev")?; + (BTreeMap::new(), Some(written)) + } + "git_clone_full" => (git_clone_full(&bare, &sandbox)?, None), + _ => bail!("unknown case: {case}"), + }; + let total_ns = started.elapsed().as_nanos(); + // Byte verification is never timed. + verify(&sandbox, &golden, partial, &east) + .with_context(|| format!("{case} iteration {iteration} verification"))?; + if iteration >= config.warmups { + samples.push(Sample { + case: case.to_string(), + iteration: iteration - config.warmups, + total_ns, + phases_ns: phases, + entries, + verified_files: if partial { east.len() } else { golden.len() }, + output_bytes: if partial { + east_bytes + } else { + golden.iter().map(|f| f.content.len()).sum() + }, + }); + } + let _ = fs::remove_dir_all(&sandbox); + } + } + Ok(()) + })(); + // The tonic server never shuts down; detach it. The process (or test) + // exits with the thread still serving, which mirrors atomicd. + drop(server); + if let Err(error) = result { + // Retain the fixtures for inspection; a TempDir would delete them. + let retained = fixtures.keep(); + eprintln!("fixtures retained at {} for inspection", retained.display()); + return Err(error); + } + let retained_fixtures = config.keep_fixtures.then(|| fixtures.keep()); + + let report = serde_json::json!({ + "schema": 1, + "config": config, + "debug_build": cfg!(debug_assertions), + "platform": {"os": std::env::consts::OS, "arch": std::env::consts::ARCH}, + "fixture_kind": "two-subtree synthetic histories; atomic daemon Materialize RPC on a Unix socket versus Git file:// clone", + "cache_policy": "server and bare repository persist across samples; sandboxes are fresh; OS caches uncontrolled", + "retained_fixtures": retained_fixtures, + "samples": samples, + }); + let mut output = fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&config.output)?; + serde_json::to_writer_pretty(&mut output, &report)?; + output.flush()?; + println!( + "verified {} samples; report {}", + config.iterations * CASES.len(), + config.output.display() + ); + Ok(()) +} + +fn main() -> Result<()> { + run(Config::parse()?) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn smoke_checks_every_case() { + let dir = tempfile::tempdir().unwrap(); + let output = dir.path().join("report.json"); + run(Config { + output: output.clone(), + files_east: 2, + files_west: 2, + lines: 8, + edits: 1, + iterations: 1, + warmups: 0, + allow_debug: true, + keep_fixtures: false, + span: true, + }) + .unwrap(); + let report: serde_json::Value = serde_json::from_slice(&fs::read(output).unwrap()).unwrap(); + let samples = report["samples"].as_array().unwrap(); + assert_eq!(samples.len(), CASES.len()); + for sample in samples { + assert!(sample["total_ns"].as_u64().unwrap() > 0); + assert!(sample["verified_files"].as_u64().unwrap() > 0); + } + let partial: Vec<_> = samples + .iter() + .filter(|s| s["case"].as_str().unwrap().contains("partial")) + .collect(); + assert_eq!(partial.len(), 1); + assert_eq!(partial[0]["case"].as_str().unwrap(), "atomic_rpc_partial"); + assert_eq!(partial[0]["verified_files"].as_u64().unwrap(), 2); + } +} diff --git a/libatomic/src/daemon/services.rs b/libatomic/src/daemon/services.rs index e7c69040..ea937f63 100644 --- a/libatomic/src/daemon/services.rs +++ b/libatomic/src/daemon/services.rs @@ -2043,6 +2043,34 @@ impl repository_mutation_service_server::RepositoryMutationService for MutationI name paths explicitly or pass force", )); } + // One projection decides which Modified/Deleted paths the view + // still records; rendering every file here only to discard the + // bytes would decode the view's history once per file. A + // projection error therefore fails the request before any path is + // untracked, and a render error now surfaces from materialization + // (which plans before it writes). The walk below keeps status + // order, so untracking and a name-conflict failure happen at the + // same position as before. + let content_paths: Vec = dirty + .iter() + .filter(|(_, file_status)| { + *file_status != atomic_repository::status::FileStatus::Added + }) + .map(|(path, _)| path.clone()) + .collect(); + let presence: std::collections::HashMap = + if content_paths.is_empty() { + Default::default() + } else { + content_paths + .iter() + .cloned() + .zip( + repo.recorded_presence_on_view(&content_paths, repo.current_view()) + .map_err(repository_error)?, + ) + .collect() + }; let mut restored: Vec = Vec::new(); let mut materialize = std::collections::HashSet::new(); for (path, file_status) in dirty { @@ -2058,16 +2086,26 @@ impl repository_mutation_service_server::RepositoryMutationService for MutationI restored.push(path); } // Modified/Deleted: pristine content back to disk. - _ => { - let content = repo - .get_file_content(std::path::Path::new(&path)) - .map_err(repository_error)?; - if content.is_some() { + _ => match presence.get(&path) { + Some(atomic_repository::RecordedPresence::Present) => { materialize.insert(path.clone()); restored.push(path); } // No pristine content: skip (nothing safe to do). - } + Some(atomic_repository::RecordedPresence::Absent) | None => {} + Some(atomic_repository::RecordedPresence::NameConflict { + paths, + claimants, + }) => { + return Err(repository_error(atomic_repository::name_conflict_error( + &atomic_repository::tracking::normalize_path(std::path::Path::new( + &path, + )), + *paths, + *claimants, + ))); + } + }, } } if !materialize.is_empty() { diff --git a/tools/materialize-bench/README.md b/tools/materialize-bench/README.md index d81aba2f..f4445c8d 100644 --- a/tools/materialize-bench/README.md +++ b/tools/materialize-bench/README.md @@ -38,6 +38,14 @@ Every fixture also includes an empty file. Override with `--files`, `--lines`, - `render_reopened`: drop/reopen the repository, then render; includes open cost. - `materialize_noop`: ordinary journaled materialization of already-correct files. - `materialize_missing`: same API after removing only generated tracked files. +- `materialize_paths_noop`: journaled `materialize_paths` over a fixed subset + (every third generated file, including the edited hot file) with nothing + deleted; measures the partial fast path when every file is already correct. +- `materialize_paths_missing`: the same subset after removing only those files; + the partial analog of `materialize_missing`. +- `materialize_prefix_missing`: remove the first ten generated files, then + journaled `materialize_prefix("files/file_000")`; partial selection by + filename prefix instead of an explicit path set. - `switch_one_forward/back`: switch to/from a view with one changed line. - `switch_many_forward/back`: switch to/from a view changing one line per file. @@ -67,6 +75,22 @@ favors spans. Database sizes are post-sample, uncompacted sizes, not a storage efficiency measurement. Errors and incomplete/duplicate samples fail the run; they are not passes. Fixtures are disposable; `--keep-fixtures` retains them. +## Agent-sandbox sync benchmark + +`run_agent_sandbox.py` builds and drives +`libatomic/examples/agent_sandbox_bench.rs`: a cold, empty sandbox syncing +over the production daemon `Materialize` RPC (tonic, Unix socket, +`SandboxImpl` handlers, `path_prefix` partial) versus Git's wire clone +(`file://`) on matched synthetic histories — the cold-sandbox scenario the +local cases above cannot answer. The benchmark checks expected bytes after +every sample; partial sandboxes must hold exactly the requested subtree and +nothing beyond it. + +```sh +python3 -B tools/materialize-bench/run_agent_sandbox.py --build \ + --profile many-files --output /approved/scratch/asb-01 +``` + The benchmark checks expected bytes and modes after every sample and verifies a single completed working-copy operation head. It does not certify imported history, genuine conflicts, cold caches, transport, watchers, crashes or power diff --git a/tools/materialize-bench/run.py b/tools/materialize-bench/run.py index 9f424c2b..6392a6a0 100644 --- a/tools/materialize-bench/run.py +++ b/tools/materialize-bench/run.py @@ -20,6 +20,7 @@ 'hot-file': dict(files=1, lines=4096, edits=48), } CASES = ('render_session', 'render_reopened', 'materialize_noop', 'materialize_missing', + 'materialize_paths_noop', 'materialize_paths_missing', 'materialize_prefix_missing', 'switch_one_forward', 'switch_one_back', 'switch_many_forward', 'switch_many_back') diff --git a/tools/materialize-bench/run_agent_sandbox.py b/tools/materialize-bench/run_agent_sandbox.py new file mode 100644 index 00000000..0449c61a --- /dev/null +++ b/tools/materialize-bench/run_agent_sandbox.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python3 +"""Frozen, local-only agent-sandbox sync runs: atomic daemon RPC versus Git wire clone. + +Wraps libatomic/examples/agent_sandbox_bench.rs with the same identity +discipline as run.py: the source tree is fingerprinted before and after build +and measurement, the release executable is copied into the artifact directory +and its SHA-256 is verified afterwards. Debug runs are smoke tests, never +performance evidence. +""" +import argparse +import json +from collections import defaultdict +import os +from pathlib import Path +import platform +import shutil +import subprocess +import time + +sys_path = Path(__file__).resolve().parent +import sys # noqa: E402 + +sys.path.insert(0, str(sys_path)) +from run import digest, distribution, source_fingerprint # noqa: E402 + +ROOT = sys_path.parents[1] +PROFILES = { + 'smoke': dict(files_east=2, files_west=2, lines=8, edits=1), + 'many-files': dict(files_east=8, files_west=16, lines=128, edits=24), + 'hot-file': dict(files_east=1, files_west=16, lines=4096, edits=48), +} +CASES = ('atomic_rpc_full', 'atomic_rpc_partial', 'atomic_sandbox_cow', 'git_clone_full') + + +def validate_report(report, iterations): + expected = {(case, iteration) for case in CASES for iteration in range(iterations)} + seen = set() + for sample in report['samples']: + key = (sample['case'], sample['iteration']) + if key not in expected or key in seen: + raise ValueError('unexpected or duplicate sample: ' + repr(key)) + seen.add(key) + if not isinstance(sample['total_ns'], int) or sample['total_ns'] <= 0: + raise ValueError('nonpositive or invalid sample duration') + if seen != expected: + raise ValueError('not all cases produced samples') + + +def summarize(report): + groups = defaultdict(list) + phases = defaultdict(lambda: defaultdict(list)) + for sample in report['samples']: + groups[sample['case']].append(sample['total_ns'] / 1_000_000) + for name, nanos in sample['phases_ns'].items(): + phases[sample['case']][name].append(nanos / 1_000_000) + return {case: dict(distribution(values), + phases_ms={name: distribution(values) for name, values in phases[case].items()}) + for case, values in groups.items()} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + selection = parser.add_mutually_exclusive_group(required=True) + selection.add_argument('--build', action='store_true', + help='build offline/locked/release from fingerprinted source') + selection.add_argument('--binary', type=Path, + help='existing agent_sandbox_bench executable (build provenance not assumed)') + parser.add_argument('--output', required=True, type=Path, + help='new artifact directory; must not exist') + parser.add_argument('--profile', choices=PROFILES, default='many-files') + for name in ('files-east', 'files-west', 'lines', 'edits'): + parser.add_argument('--' + name, type=int) + parser.add_argument('--iterations', type=int, default=8) + parser.add_argument('--warmups', type=int, default=1) + parser.add_argument('--keep-fixtures', action='store_true') + parser.add_argument('--allow-debug', action='store_true', + help='correctness smoke only, not performance evidence') + args = parser.parse_args() + if args.iterations < 1 or args.warmups < 0: + parser.error('iterations must be positive and warmups nonnegative') + output = args.output.resolve() + output.mkdir(parents=True, exist_ok=False) + output.chmod(0o700) + source_before = source_fingerprint() + metadata = dict(outcome='incomplete', source_sha256=source_before, + source_binding='fingerprinted offline release build' if args.build + else 'caller-supplied binary; source/build association unverified', + platform=platform.platform(), logical_cpus=os.cpu_count(), + concurrency_control='OS cache and unrelated system load are not controlled') + try: + candidate = args.binary.resolve() if args.binary else None + if args.build: + command = ['cargo', 'build', '--release', '--offline', '--locked', '-p', 'libatomic', + '--example', 'agent_sandbox_bench', '--message-format=json-render-diagnostics'] + build_env = dict(os.environ, CARGO_NET_OFFLINE='true') + build_env.setdefault('CARGO_BUILD_JOBS', '2') + build_env.setdefault('CARGO_PROFILE_RELEASE_STRIP', 'none') + metadata['build_command'] = command + result = subprocess.run(command, cwd=ROOT, env=build_env, capture_output=True) + (output / 'build.stdout.jsonl').write_bytes(result.stdout) + (output / 'build.stderr.log').write_bytes(result.stderr) + result.check_returncode() + for line in result.stdout.splitlines(): + event = json.loads(line) + if event.get('reason') == 'compiler-artifact' and \ + event.get('target', {}).get('name') == 'agent_sandbox_bench' and \ + event.get('executable'): + candidate = Path(event['executable']) + if candidate is None: + raise RuntimeError('Cargo did not report the benchmark executable') + if source_fingerprint() != source_before: + raise RuntimeError('source changed during build; discard this candidate') + if candidate is None or not candidate.is_file(): + raise RuntimeError('benchmark executable not found') + binary = output / 'agent_sandbox_bench' + shutil.copy2(candidate, binary) + metadata['binary_sha256'] = digest(binary) + config = {name.replace('_', '-'): value + for name, value in PROFILES[args.profile].items()} + for name in ('files-east', 'files-west', 'lines', 'edits'): + value = getattr(args, name.replace('-', '_')) + if value is not None: + config[name] = value + command = [str(binary), '--output', str(output / 'raw.json'), + '--iterations', str(args.iterations), '--warmups', str(args.warmups)] + for name, value in config.items(): + command += ['--' + name, str(value)] + if args.keep_fixtures: + command.append('--keep-fixtures') + if args.allow_debug: + command.append('--allow-debug') + home = output / 'home' + temporary = output / 'tmp' + home.mkdir() + temporary.mkdir() + env = {key: value for key, value in os.environ.items() + if not key.startswith(('ATOMIC_', 'GIT_', 'XDG_'))} + env.update(HOME=str(home), ATOMIC_HOME=str(home / '.atomic'), + ATOMIC_CONFIG_DIR=str(home / 'config'), ATOMIC_SERVICE='local', + TMPDIR=str(temporary)) + metadata['command'] = command + started = time.monotonic() + result = subprocess.run(command, cwd=ROOT, env=env, capture_output=True) + metadata['runner_elapsed_seconds'] = time.monotonic() - started + metadata['exit_code'] = result.returncode + (output / 'stdout.log').write_bytes(result.stdout) + (output / 'stderr.log').write_bytes(result.stderr) + result.check_returncode() + if source_fingerprint() != source_before or digest(binary) != metadata['binary_sha256']: + raise RuntimeError('source or pinned binary changed during measurement; results invalid') + report = json.loads((output / 'raw.json').read_text()) + validate_report(report, args.iterations) + summary = summarize(report) + metadata.update(outcome='smoke-only' if report['debug_build'] else 'measured', + summary_ms=summary) + print('| Case | Median ms | Mean ms | p95 ms |') + print('|---|---:|---:|---:|') + for case in CASES: + row = summary[case] + print(f"| {case} | {row['median']:.3f} | {row['mean']:.3f} | {row['p95']:.3f} |") + print('Artifacts:', output) + except Exception as error: + metadata.update(outcome='failed-or-invalid', error=str(error)) + raise + finally: + (output / 'run.json').write_text(json.dumps(metadata, indent=2, sort_keys=True) + '\n') + + +if __name__ == '__main__': + main() diff --git a/tools/materialize-bench/test_run_agent_sandbox.py b/tools/materialize-bench/test_run_agent_sandbox.py new file mode 100644 index 00000000..aebac217 --- /dev/null +++ b/tools/materialize-bench/test_run_agent_sandbox.py @@ -0,0 +1,51 @@ +import importlib.util +import json +from pathlib import Path +import unittest + +for name in ('run', 'run_agent_sandbox'): + spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(name + '.py')) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + globals()[name] = module + + +def report(): + return {'samples': [dict(case=case, iteration=i, total_ns=(2 if case.startswith('atomic') else 1) * 1_000_000, + phases_ns={'connect': 100_000}) + for case in run_agent_sandbox.CASES for i in range(3)]} + + +class RunnerTests(unittest.TestCase): + def test_complete_report_and_summary(self): + data = report() + run_agent_sandbox.validate_report(data, 3) + summary = run_agent_sandbox.summarize(data) + self.assertEqual(len(summary), len(run_agent_sandbox.CASES)) + self.assertEqual(summary['atomic_rpc_full']['median'], 2) + self.assertEqual(summary['git_clone_full']['median'], 1) + self.assertEqual(summary['atomic_rpc_full']['phases_ms']['connect']['median'], .1) + + def test_missing_duplicate_unknown_and_invalid_samples_fail(self): + import copy + bad = copy.deepcopy(report()) + bad['samples'].pop() + with self.assertRaises(ValueError): + run_agent_sandbox.validate_report(bad, 3) + for field, value in [('case', 'unknown'), ('iteration', 9), ('total_ns', 0)]: + bad = copy.deepcopy(report()) + bad['samples'][0][field] = value + with self.assertRaises(ValueError): + run_agent_sandbox.validate_report(bad, 3) + bad = copy.deepcopy(report()) + bad['samples'][-1] = bad['samples'][0] + with self.assertRaises(ValueError): + run_agent_sandbox.validate_report(bad, 3) + + def test_profiles_shape_matches_example_flags(self): + for profile in run_agent_sandbox.PROFILES.values(): + self.assertEqual(set(profile), {'files_east', 'files_west', 'lines', 'edits'}) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/materialize-bench/test_runner.py b/tools/materialize-bench/test_runner.py index abcf58d3..feeacbd2 100644 --- a/tools/materialize-bench/test_runner.py +++ b/tools/materialize-bench/test_runner.py @@ -20,7 +20,7 @@ def test_complete_report_and_summary(self): report = self.report() runner.validate_report(report, 3) summary = runner.summarize(report) - self.assertEqual(len(summary), 8) + self.assertEqual(len(summary), len(runner.CASES)) for row in summary: self.assertEqual(row['graph_over_span_median'], 2) self.assertEqual(row['graph']['n'], 3) From fbaa4ca85ddbb03d92aeba4a8150ad07a9fbeeaf Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 10 Nov 2026 11:48:53 -0600 Subject: [PATCH 09/14] style: cargo fmt the merged vault handler --- libatomic/src/daemon/services_agent.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libatomic/src/daemon/services_agent.rs b/libatomic/src/daemon/services_agent.rs index d7415d9c..52ff50d9 100644 --- a/libatomic/src/daemon/services_agent.rs +++ b/libatomic/src/daemon/services_agent.rs @@ -1499,7 +1499,8 @@ impl vault_service_server::VaultService for VaultImpl { disk_path.display() ))) } - } let mut frontmatter = Map::new(); + } + let mut frontmatter = Map::new(); frontmatter.insert("uid".into(), Value::String(id)); frontmatter.insert("memoryKind".into(), Value::String(memory_kind)); frontmatter.insert("status".into(), Value::String(memory_status)); From 3b18246db0877e8603780b7c97a9e5d8e782ca21 Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 10 Nov 2026 14:23:51 -0600 Subject: [PATCH 10/14] feat: switch-from-empty case for the materialization benchmark (#324) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit switch_from_empty rests on a view where every tracked file is absent (untimed setup switch) and times the switch to the full view: maximal pure materialize work through the switch entry point — every tracked file rendered and written, no deletions, no revert machinery. Measured many-files: 887 ms graph / 872 ms span, comparable to switch_many — the switch machinery itself, not the write volume, dominates at this fixture size. --- .../examples/materialize_bench.rs | 18 ++++++++++++++++++ tools/materialize-bench/run.py | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/atomic-repository/examples/materialize_bench.rs b/atomic-repository/examples/materialize_bench.rs index 9a311003..27bb3a74 100644 --- a/atomic-repository/examples/materialize_bench.rs +++ b/atomic-repository/examples/materialize_bench.rs @@ -28,6 +28,7 @@ const CASES: &[&str] = &[ "switch_one_back", "switch_many_forward", "switch_many_back", + "switch_from_empty", ]; /// Zero-based fixture index of a generated `files/file_NNNN.txt` path. @@ -229,6 +230,16 @@ impl Fixture { golden.insert("dev".into(), capture(&contents)?); repo.create_view_from("bench-near", "dev")?; repo.create_view_from("bench-far", "dev")?; + // A view where every tracked file is absent: the far end of the + // switch spectrum (the existing near/far views differ by lines). + repo.create_view_from("bench-empty", "dev")?; + repo.switch_view(wc, "bench-empty")?; + for file in &golden["dev"] { + fs::remove_file(root.join(&file.path))?; + } + record(&repo, "synthetic empty view", &mut tick)?; + repo.switch_view(wc, "dev")?; + repo.materialize(wc)?; repo.switch_view(wc, "bench-near")?; let mut near = contents.clone(); near[0][0] = "near view: one changed line\n".into(); @@ -420,6 +431,13 @@ fn sample(fixture: &mut Fixture, case: &str, iteration: usize) -> Result if case == "render_reopened" { drop(fixture.repo.take()); } + if case == "switch_from_empty" { + // Untimed setup: rest on the empty view so the timed switch carries + // the full materialization — every tracked file rendered and written, + // nothing deleted. Pure materialize work, no revert machinery. + let repo = fixture.repo.as_mut().unwrap(); + repo.switch_view(repo.require_working_copy_id()?, "bench-empty")?; + } // Partial path sets are cloned outside the timed region. let partial_paths = fixture.partial_paths.clone(); let prefix = fixture.prefix.clone(); diff --git a/tools/materialize-bench/run.py b/tools/materialize-bench/run.py index 6392a6a0..00fda5c8 100644 --- a/tools/materialize-bench/run.py +++ b/tools/materialize-bench/run.py @@ -21,7 +21,8 @@ } CASES = ('render_session', 'render_reopened', 'materialize_noop', 'materialize_missing', 'materialize_paths_noop', 'materialize_paths_missing', 'materialize_prefix_missing', - 'switch_one_forward', 'switch_one_back', 'switch_many_forward', 'switch_many_back') + 'switch_one_forward', 'switch_one_back', 'switch_many_forward', 'switch_many_back', + 'switch_from_empty') def validate_report(report, iterations): From 906db5ff0f27b8c4f82e84fd9971b15ca21e2385 Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 10 Nov 2026 14:26:27 -0600 Subject: [PATCH 11/14] style: cargo fmt across the merged tree --- atomic-repository/src/repository/content.rs | 6 +- .../src/repository/materialize.rs | 8 +- atomic-repository/src/repository/record.rs | 4 +- atomic-repository/src/repository/status.rs | 4 +- libatomic/examples/agent_sandbox_bench.rs | 159 +++++++++--------- 5 files changed, 90 insertions(+), 91 deletions(-) diff --git a/atomic-repository/src/repository/content.rs b/atomic-repository/src/repository/content.rs index c3d1b076..e5a23ac4 100644 --- a/atomic-repository/src/repository/content.rs +++ b/atomic-repository/src/repository/content.rs @@ -1116,11 +1116,7 @@ where + atomic_core::pristine::CapabilityTxnT, C: atomic_core::change::ChangeStore, { - pub(super) fn new( - txn: &'a T, - store: &'a C, - visibility: &'a GraphVisibilityClosure, - ) -> Self { + pub(super) fn new(txn: &'a T, store: &'a C, visibility: &'a GraphVisibilityClosure) -> Self { Self { txn, store, diff --git a/atomic-repository/src/repository/materialize.rs b/atomic-repository/src/repository/materialize.rs index 034d697b..6fc016c6 100644 --- a/atomic-repository/src/repository/materialize.rs +++ b/atomic-repository/src/repository/materialize.rs @@ -456,13 +456,7 @@ pub(super) fn render_name_conflict_side( inode_graph_table: &redb::ReadOnlyMultimapTable<&'static [u8; 32], &'static [u8; 24]>, visibility: &GraphVisibilityClosure, external_hashes: &std::collections::HashMap, - shared_spans: Option< - &super::content::SharedSpanContext< - '_, - atomic_core::pristine::ReadTxn, - C, - >, - >, + shared_spans: Option<&super::content::SharedSpanContext<'_, atomic_core::pristine::ReadTxn, C>>, path: &str, inode: Inode, position: Position, diff --git a/atomic-repository/src/repository/record.rs b/atomic-repository/src/repository/record.rs index f49f1ea8..ea005ffa 100644 --- a/atomic-repository/src/repository/record.rs +++ b/atomic-repository/src/repository/record.rs @@ -5077,8 +5077,8 @@ fn prepare_name_resolution( &inode_graph_table, visibility, &external_hashes, - - None, path, + None, + path, side.inode, side.position, ) diff --git a/atomic-repository/src/repository/status.rs b/atomic-repository/src/repository/status.rs index 8e3e49c3..7c8f8403 100644 --- a/atomic-repository/src/repository/status.rs +++ b/atomic-repository/src/repository/status.rs @@ -507,8 +507,8 @@ impl Repository { &inode_graph_table, &visibility, &external_hashes, - - None, &item.path, + None, + &item.path, item.inode, item.position, ) diff --git a/libatomic/examples/agent_sandbox_bench.rs b/libatomic/examples/agent_sandbox_bench.rs index 9aeb3771..4894f3ee 100644 --- a/libatomic/examples/agent_sandbox_bench.rs +++ b/libatomic/examples/agent_sandbox_bench.rs @@ -42,6 +42,7 @@ use std::sync::Arc; use std::time::Instant; use anyhow::{bail, ensure, Context, Result}; +use hyper_util::rt::TokioIo; use libatomic::atomic::{sandbox_service_client::SandboxServiceClient, MaterializeRequest}; use libatomic::daemon::services_sandbox::SandboxImpl; use libatomic::daemon::state::DaemonState; @@ -49,7 +50,6 @@ use serde::{Deserialize, Serialize}; use tokio_stream::wrappers::UnixListenerStream; use tonic::transport::{Endpoint, Uri}; use tower::service_fn; -use hyper_util::rt::TokioIo; const CASES: &[&str] = &[ "atomic_rpc_full", @@ -97,15 +97,22 @@ impl Config { "--keep-fixtures" => config.keep_fixtures = true, "--output" => config.output = args.next().context("--output needs a path")?.into(), "--files-east" => { - config.files_east = args.next().context("--files-east needs a value")?.parse()? + config.files_east = + args.next().context("--files-east needs a value")?.parse()? } "--files-west" => { - config.files_west = args.next().context("--files-west needs a value")?.parse()? + config.files_west = + args.next().context("--files-west needs a value")?.parse()? + } + "--lines" => { + config.lines = args.next().context("--lines needs a value")?.parse()? + } + "--edits" => { + config.edits = args.next().context("--edits needs a value")?.parse()? } - "--lines" => config.lines = args.next().context("--lines needs a value")?.parse()?, - "--edits" => config.edits = args.next().context("--edits needs a value")?.parse()?, "--iterations" => { - config.iterations = args.next().context("--iterations needs a value")?.parse()? + config.iterations = + args.next().context("--iterations needs a value")?.parse()? } "--warmups" => { config.warmups = args.next().context("--warmups needs a value")?.parse()? @@ -119,10 +126,7 @@ impl Config { fn validate(&self) -> Result<()> { ensure!( - self.files_east > 0 - && self.files_west > 0 - && self.lines >= 2 - && self.iterations > 0, + self.files_east > 0 && self.files_west > 0 && self.lines >= 2 && self.iterations > 0, "file counts and iterations must be positive; lines must be at least two" ); ensure!( @@ -171,10 +175,7 @@ struct Golden { fn build_golden(config: &Config) -> Vec { let mut golden = Vec::new(); - for (tree, count) in [ - ("east", config.files_east), - ("west", config.files_west), - ] { + for (tree, count) in [("east", config.files_east), ("west", config.files_west)] { for file in 0..count { golden.push(Golden { path: format!("files/{tree}/file_{file:04}.txt"), @@ -231,9 +232,7 @@ fn prepare_fixtures( parent: &Path, config: &Config, ) -> Result<(tempfile::TempDir, Vec, PathBuf)> { - let directory = tempfile::Builder::new() - .prefix("agb") - .tempdir_in(parent)?; + let directory = tempfile::Builder::new().prefix("agb").tempdir_in(parent)?; let root = directory.path(); let golden = build_golden(config); @@ -305,7 +304,11 @@ fn prepare_fixtures( } git(&seed, &["init", "-q", "-b", "dev"], None)?; git(&seed, &["add", "-A"], None)?; - git(&seed, &["commit", "-q", "-m", "synthetic baseline"], Some(0))?; + git( + &seed, + &["commit", "-q", "-m", "synthetic baseline"], + Some(0), + )?; let seed_hot = seed.join("files/east/file_0000.txt"); for edit in 0..config.edits { let line = (edit * 17) % config.lines; @@ -316,7 +319,11 @@ fn prepare_fixtures( lines[line] = edited_line(line, edit + 1); fs::write(&seed_hot, lines.concat())?; git(&seed, &["add", "-A"], None)?; - git(&seed, &["commit", "-q", "-m", "synthetic hot-file edit"], Some(edit + 1))?; + git( + &seed, + &["commit", "-q", "-m", "synthetic hot-file edit"], + Some(edit + 1), + )?; } let bare = root.join("host-bare.git"); git( @@ -366,8 +373,8 @@ fn serve( ) -> Result>> { use libatomic::atomic::sandbox_service_server::SandboxServiceServer; - let std_listener = StdUnixListener::bind(&socket) - .with_context(|| format!("binding {}", socket.display()))?; + let std_listener = + StdUnixListener::bind(&socket).with_context(|| format!("binding {}", socket.display()))?; std_listener.set_nonblocking(true)?; // Fail fast if the repository cannot open; the server would otherwise // serve errors that look like client bugs. The registration also gives @@ -423,63 +430,65 @@ fn atomic_sandbox( let mut phases = BTreeMap::new(); let sandbox = sandbox.to_path_buf(); let socket = socket.to_path_buf(); - let (written, inner) = std::thread::spawn(move || -> Result<(usize, BTreeMap)> { - let runtime = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .map_err(anyhow::Error::msg)?; - runtime.block_on(async move { - use tokio_stream::StreamExt; - let mut phases = BTreeMap::new(); - let started = Instant::now(); - let mut client = connect(&socket).await?; - phases.insert("connect".into(), started.elapsed().as_nanos()); - let started = Instant::now(); - let mut stream = client - .materialize(MaterializeRequest { - repository: Some(repository), - target: Some(target), - path_prefix: prefix, - }) - .await? - .into_inner(); - phases.insert("request".into(), started.elapsed().as_nanos()); - let started = Instant::now(); - let mut written = 0usize; - loop { - let frame = stream - .next() - .await - .ok_or_else(|| anyhow::anyhow!("materialize ended without its summary"))? - .map_err(anyhow::Error::msg)?; - match frame.frame { - Some(libatomic::atomic::materialize_frame::Frame::Entry(entry)) => { - atomic_repository::write_sandbox_entry( - &sandbox, - &entry.path, - sandbox_wire::tree_entry_kind(&entry).map_err(anyhow::Error::msg)?, - entry.mode.unwrap_or(0o644), - entry.inline.as_deref().unwrap_or_default(), - )?; - written += 1; - } - Some(libatomic::atomic::materialize_frame::Frame::Done(done)) => { - ensure!( - done.entries as usize == written, - "the server sent {written} entries but said {}", - done.entries - ); - break; + let (written, inner) = + std::thread::spawn(move || -> Result<(usize, BTreeMap)> { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .map_err(anyhow::Error::msg)?; + runtime.block_on(async move { + use tokio_stream::StreamExt; + let mut phases = BTreeMap::new(); + let started = Instant::now(); + let mut client = connect(&socket).await?; + phases.insert("connect".into(), started.elapsed().as_nanos()); + let started = Instant::now(); + let mut stream = client + .materialize(MaterializeRequest { + repository: Some(repository), + target: Some(target), + path_prefix: prefix, + }) + .await? + .into_inner(); + phases.insert("request".into(), started.elapsed().as_nanos()); + let started = Instant::now(); + let mut written = 0usize; + loop { + let frame = stream + .next() + .await + .ok_or_else(|| anyhow::anyhow!("materialize ended without its summary"))? + .map_err(anyhow::Error::msg)?; + match frame.frame { + Some(libatomic::atomic::materialize_frame::Frame::Entry(entry)) => { + atomic_repository::write_sandbox_entry( + &sandbox, + &entry.path, + sandbox_wire::tree_entry_kind(&entry) + .map_err(anyhow::Error::msg)?, + entry.mode.unwrap_or(0o644), + entry.inline.as_deref().unwrap_or_default(), + )?; + written += 1; + } + Some(libatomic::atomic::materialize_frame::Frame::Done(done)) => { + ensure!( + done.entries as usize == written, + "the server sent {written} entries but said {}", + done.entries + ); + break; + } + None => bail!("an empty materialize frame"), } - None => bail!("an empty materialize frame"), } - } - phases.insert("stream_and_write".into(), started.elapsed().as_nanos()); - Ok((written, phases)) + phases.insert("stream_and_write".into(), started.elapsed().as_nanos()); + Ok((written, phases)) + }) }) - }) - .join() - .map_err(|_| anyhow::anyhow!("client thread panicked"))??; + .join() + .map_err(|_| anyhow::anyhow!("client thread panicked"))??; phases.extend(inner); Ok((phases, written)) } From 7bf1e33dece8ea27747a6b76446a050abea848ab Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 10 Nov 2026 14:40:18 -0600 Subject: [PATCH 12/14] fix: hex-encode the sha256 digest without LowerHex The digest array no longer implements LowerHex after the dependency refresh on dev; encode explicitly with HEXLOWER instead. --- atomic-repository/examples/imported_materialize_bench.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/atomic-repository/examples/imported_materialize_bench.rs b/atomic-repository/examples/imported_materialize_bench.rs index de08e27d..b907a3f3 100644 --- a/atomic-repository/examples/imported_materialize_bench.rs +++ b/atomic-repository/examples/imported_materialize_bench.rs @@ -46,7 +46,8 @@ struct Report { } fn sha256(bytes: &[u8]) -> String { - format!("{:x}", Sha256::digest(bytes)) + use data_encoding::HEXLOWER; + HEXLOWER.encode(&Sha256::digest(bytes)) } fn verify_files(root: &Path, files: &[ExpectedFile]) -> Result<()> { From 830ef9722150a92d83d37ea4b0a3778f15554712 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 11 Nov 2026 11:51:30 -0500 Subject: [PATCH 13/14] fix: get CI green on the span branch (#328) * fix: build agent_sandbox_bench only on Unix The benchmark drives the daemon over a Unix domain socket, so the example did not compile on Windows and the Windows test jobs never ran. Gate its items to Unix and leave a stub main elsewhere. * fix: update span import for the git2 0.21 shorthand API Reference::shorthand returns a Result since git2 0.21, so the experimental-span-import feature no longer compiled. CI does not build that feature. * fix: let the Git import preflight repair a database left by a killed process The preflight opened the database without recovery and refused any database that needed repair, so `git shim reconcile` failed after the watch daemon was killed. Allow redb's own storage-level repair once the common lock is held. It changes no repository state. Dry runs, format upgrades and repository recovery are still refused. Adds a regression test using a copy of a repository that is still open. * fix: stale tests and two small behaviour gaps on the span branch - Span incremental import takes its target from the pinned Git branch instead of Repository::current_view(). The refusal when the current view differs is unchanged; it moves next to the importer's own check. - for_each_switch_target_file returns early when there is nothing to read, so a switch that writes no files no longer needs the target view's dependency closure. - whole_file_diff_test: pure deletions and a missing final newline are no longer stored as whole-file rewrites. Keep the diff assertions and drop the rewrite precondition for those three fixtures. - remove_recorded_path_test: count oneline log entries by line instead of by the default format's "#N" header. - routing_parity_test: the service path now prints the same follow-up hints as the local path. --- atomic-cli/src/commands/git/import.rs | 12 +++-- atomic-cli/src/commands/git/stream.rs | 11 ++++ .../tests/it/remove_recorded_path_test.rs | 2 +- atomic-cli/tests/it/routing_parity_test.rs | 2 +- atomic-core/src/pristine/txn/pristine.rs | 25 +++++++++ .../src/repository/git_stream.rs | 54 +++++++++++++++++++ atomic-repository/src/repository/locks.rs | 35 +++++++++--- .../src/repository/materialize.rs | 5 ++ .../tests/it/whole_file_diff_test.rs | 19 +++++-- libatomic/examples/agent_sandbox_bench.rs | 47 +++++++++++++++- 10 files changed, 193 insertions(+), 19 deletions(-) diff --git a/atomic-cli/src/commands/git/import.rs b/atomic-cli/src/commands/git/import.rs index c86c5b94..8e7d1cf4 100644 --- a/atomic-cli/src/commands/git/import.rs +++ b/atomic-cli/src/commands/git/import.rs @@ -867,6 +867,7 @@ impl SpanIncrementalGitObservation { } let branch = head .shorthand() + .ok() .ok_or_else(|| CliError::GitError { message: "Git branch name is not representable".into(), })? @@ -923,7 +924,7 @@ impl SpanIncrementalGitObservation { message: format!("cannot reobserve Git index: {error}"), })?; if !after_head.is_branch() - || after_head.shorthand() != Some(branch.as_str()) + || after_head.shorthand().ok() != Some(branch.as_str()) || after_head.target() != Some(tip) || after_index != index || git.path().join("index.lock").exists() @@ -974,9 +975,6 @@ impl Import { }); } let mut repo = Repository::open(workdir).map_err(CliError::from)?; - if repo.current_view() != pinned.branch { - return Err(CliError::GitError { message: "span incremental import requires the current Atomic view to match the Git branch".into() }); - } let limits = self.stream_limits()?; let importer = ParallelImporter::new( git, @@ -984,10 +982,14 @@ impl Import { graph_only: true, span_stream: true, mainline_only: true, - target_view: repo.current_view().to_string(), + // The pinned Git branch names the target. The view is never + // taken from the handle's ambient selection; the importer + // refuses unless the two already agree. + target_view: pinned.branch.clone(), ..Default::default() }, ); + importer.require_target_is_current_view(&repo)?; let plan = importer.validate_graph_stream_prospectively(&pinned.branch, pinned.tip, limits)?; let expected_tree = plan.expected_git_tree().ok_or_else(|| CliError::GitError { diff --git a/atomic-cli/src/commands/git/stream.rs b/atomic-cli/src/commands/git/stream.rs index 8b82c8a4..067f6620 100644 --- a/atomic-cli/src/commands/git/stream.rs +++ b/atomic-cli/src/commands/git/stream.rs @@ -699,6 +699,17 @@ impl ParallelImporter { }) } + /// Refuse unless the handle's current view is already this importer's + /// target. Incremental span import never selects or switches a view. + pub(crate) fn require_target_is_current_view(&self, repo: &Repository) -> CliResult<()> { + if self.options.target_view != repo.current_view() { + return Err(git_error( + "span incremental import requires the current Atomic view to match the Git branch", + )); + } + Ok(()) + } + pub(crate) fn import_span_incremental_prevalidated( &self, repo: &mut Repository, diff --git a/atomic-cli/tests/it/remove_recorded_path_test.rs b/atomic-cli/tests/it/remove_recorded_path_test.rs index c18de64d..4af7f01a 100644 --- a/atomic-cli/tests/it/remove_recorded_path_test.rs +++ b/atomic-cli/tests/it/remove_recorded_path_test.rs @@ -74,7 +74,7 @@ fn record_file(root: &Path, name: &str, bytes: &[u8]) { fn change_count(root: &Path) -> usize { cli(root, &["log", "-f", "oneline"]) .lines() - .filter(|line| line.starts_with('#')) + .filter(|line| !line.trim().is_empty()) .count() } diff --git a/atomic-cli/tests/it/routing_parity_test.rs b/atomic-cli/tests/it/routing_parity_test.rs index 56c53319..623b8163 100644 --- a/atomic-cli/tests/it/routing_parity_test.rs +++ b/atomic-cli/tests/it/routing_parity_test.rs @@ -343,7 +343,7 @@ fn memory_new_without_json_preserves_the_human_summary() { assert_eq!( output, format!( - "Created memory: {id}\n file: .vault/memory/{id}.md\n kind: context\n status: active\n" + "Created memory: {id}\n file: .vault/memory/{id}.md\n kind: context\n status: active\n\n atomic memory validate {id}\n atomic memory attest {id}\n" ) ); } diff --git a/atomic-core/src/pristine/txn/pristine.rs b/atomic-core/src/pristine/txn/pristine.rs index d6bedf1a..1aa9a7d8 100644 --- a/atomic-core/src/pristine/txn/pristine.rs +++ b/atomic-core/src/pristine/txn/pristine.rs @@ -493,6 +493,31 @@ impl Pristine { Self::open_readonly_with_schema_check(path, true, false) } + /// Open for inspection after letting redb finish its own crash repair. + /// + /// A process killed mid-write leaves the file needing redb's storage-level + /// repair, which a read-only open cannot perform. This runs only that + /// repair: it never upgrades a legacy format, initializes tables, or runs + /// repository recovery, and it returns the same errors as + /// [`Self::open_readonly_without_recovery`] for everything else. + pub fn open_readonly_after_crash_repair>(path: P) -> PristineResult { + let cache_bytes = 8 * 1024 * 1024 * 1024; // 8 GiB + let mut builder = Builder::new(); + builder.set_cache_size(cache_bytes); + let db = match builder.open_read_only(path.as_ref()) { + Ok(db) => db, + Err(redb::DatabaseError::RepairAborted) => { + // Close the writable handle cleanly to persist the repaired + // allocator state before taking the shared read lock. + let backend = SnapshotBackend::open(path.as_ref(), false)?; + drop(builder.create_with_backend(backend)?); + builder.open_read_only(path.as_ref())? + } + Err(error) => return Err(error.into()), + }; + Self::scan_ids(PristineDatabase::ReadOnly(db), true) + } + /// Open an existing database for read-only repair inspection without /// initializing tables or requiring a completed PATH_CLAIMS schema. /// diff --git a/atomic-repository/src/repository/git_stream.rs b/atomic-repository/src/repository/git_stream.rs index 9d9da274..af4f1a1d 100644 --- a/atomic-repository/src/repository/git_stream.rs +++ b/atomic-repository/src/repository/git_stream.rs @@ -2074,6 +2074,60 @@ mod tests { assert!(!dot.join("bridge.lock").exists()); } + /// Copy a repository while it is still open: the copy is what a killed + /// process leaves behind, a database redb must repair before reading. + fn copy_of_open_repository(source: &std::path::Path) -> tempfile::TempDir { + fn copy_dir(from: &std::path::Path, to: &std::path::Path) { + std::fs::create_dir_all(to).unwrap(); + for entry in std::fs::read_dir(from).unwrap() { + let entry = entry.unwrap(); + let target = to.join(entry.file_name()); + if entry.file_type().unwrap().is_dir() { + copy_dir(&entry.path(), &target); + } else { + std::fs::copy(entry.path(), &target).unwrap(); + } + } + } + let copy = tempfile::tempdir().unwrap(); + copy_dir(source, copy.path()); + copy + } + + #[test] + fn git_import_preflight_repairs_a_database_left_by_a_killed_process() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + let killed = copy_of_open_repository(dir.path()); + let dry_run = copy_of_open_repository(dir.path()); + drop(repo); + let database = + |root: &std::path::Path| root.join(".atomic").join(super::super::DATABASE_FILE); + assert!( + atomic_core::pristine::Pristine::open_readonly_without_recovery(database( + killed.path() + )) + .is_err(), + "fixture must need redb crash repair" + ); + + // A dry run may not write, so it still refuses. + assert!(Repository::try_lock_git_import_without_span(dry_run.path(), true).is_err()); + assert!( + atomic_core::pristine::Pristine::open_readonly_without_recovery(database( + dry_run.path() + )) + .is_err() + ); + + let guard = Repository::try_lock_git_import_without_span(killed.path(), false) + .expect("a database needing only crash repair must pass the preflight"); + assert!(guard.is_some()); + drop(guard); + atomic_core::pristine::Pristine::open_readonly_without_recovery(database(killed.path())) + .expect("the preflight leaves the database readable without recovery"); + } + #[test] fn failed_final_proof_never_promotes_private_stream() { let dir = tempfile::tempdir().unwrap(); diff --git a/atomic-repository/src/repository/locks.rs b/atomic-repository/src/repository/locks.rs index ad4d485b..df9e43ab 100644 --- a/atomic-repository/src/repository/locks.rs +++ b/atomic-repository/src/repository/locks.rs @@ -169,8 +169,9 @@ impl Repository { /// Reject ordinary Git imports into a span repository before any writable /// open or recovery. Hold the returned guard until import completes. - /// Databases requiring repair or migration are refused rather than opened - /// writable to inspect them. + /// Databases requiring migration or repository recovery are refused rather + /// than opened writable to inspect them. The one exception is redb's own + /// repair after a killed process, which runs under the common lock. pub fn try_lock_git_import_without_span>( path: P, dry_run: bool, @@ -204,7 +205,13 @@ impl Repository { if layout.working_root != requested { return Ok(None); } - let inspect = || -> Result { + // A process killed mid-write leaves the database needing redb's + // storage-level repair before it can be read at all. That repair + // changes no repository state, so `repair` allows it once the common + // lock is held and the caller may write; otherwise the database only + // counts as present here and the locked pass decides. Format upgrades + // and repository recovery are still refused. + let inspect = |repair: bool| -> Result { let mut found = false; for name in [super::DATABASE_FILE, super::LEGACY_PRISTINE_FILE] { let database = layout.common_dot_dir.join(name); @@ -222,7 +229,23 @@ impl Repository { Err(error) => return Err(error.into()), } found = true; - let pristine = Pristine::open_readonly_without_recovery(&database)?; + let pristine = match Pristine::open_readonly_without_recovery(&database) { + Ok(pristine) => pristine, + Err(atomic_core::pristine::PristineError::Database(error)) + if matches!(*error, redb::DatabaseError::RepairAborted) => + { + if !repair { + if dry_run { + return Err( + atomic_core::pristine::PristineError::Database(error).into() + ); + } + continue; + } + Pristine::open_readonly_after_crash_repair(&database)? + } + Err(error) => return Err(error.into()), + }; let has_span = pristine .required_repository_capabilities()? @@ -248,7 +271,7 @@ impl Repository { // A refusal for an already-span-backed repository must not create a // lock file. Recheck under the common lock to close the race with a // concurrent span-capability installation. - if !inspect()? { + if !inspect(false)? { return Ok(None); } let lock_path = layout.common_dot_dir.join(COMMON_OPERATION_LOCK); @@ -258,7 +281,7 @@ impl Repository { }); } let common = try_lock_file(&lock_path, RepositoryLockKind::Common, true)?; - if !inspect()? { + if !inspect(!dry_run)? { return Err(RepositoryError::InvalidOperation { message: "repository database disappeared during Git import preflight".into(), }); diff --git a/atomic-repository/src/repository/materialize.rs b/atomic-repository/src/repository/materialize.rs index 6fc016c6..e969b622 100644 --- a/atomic-repository/src/repository/materialize.rs +++ b/atomic-repository/src/repository/materialize.rs @@ -658,6 +658,11 @@ impl Repository { ) -> Result<(), RepositoryError> { use atomic_core::pristine::span_patch_storage::SpanRenderContext; + // Nothing to read means nothing to project: a switch that writes no + // files must not require the target view's dependency closure. + if paths.is_empty() { + return Ok(()); + } let txn = self .pristine .read_txn() diff --git a/atomic-repository/tests/it/whole_file_diff_test.rs b/atomic-repository/tests/it/whole_file_diff_test.rs index 29b091a9..6c58a951 100644 --- a/atomic-repository/tests/it/whole_file_diff_test.rs +++ b/atomic-repository/tests/it/whole_file_diff_test.rs @@ -19,7 +19,8 @@ fn record(repo: &Repository) -> Hash { .hash() } -fn replacement(before: &[u8], after: &[u8]) -> (TempDir, Repository, Hash, Change) { +/// Record `before`, then `after`, and return the second change. +fn edit(before: &[u8], after: &[u8]) -> (TempDir, Repository, Hash, Change) { let dir = TempDir::new().unwrap(); let repo = Repository::init(dir.path()).unwrap(); std::fs::write(dir.path().join("file.txt"), before).unwrap(); @@ -33,6 +34,12 @@ fn replacement(before: &[u8], after: &[u8]) -> (TempDir, Repository, Hash, Chang std::fs::write(dir.path().join("file.txt"), after).unwrap(); let hash = record(&repo); let change = repo.change_store().load_change(&hash).unwrap(); + (dir, repo, hash, change) +} + +/// An edit that must be stored as a whole-file rewrite. +fn replacement(before: &[u8], after: &[u8]) -> (TempDir, Repository, Hash, Change) { + let (dir, repo, hash, change) = edit(before, after); assert_eq!( whole_file_replacement_text(&change.file_ops()[0], &change.contents, || Some( before.to_vec() @@ -89,8 +96,9 @@ fn replacement_diff_uses_historical_text_for_all_output_formats() { #[test] fn replacement_deletion_does_not_show_survivors_as_added() { - let (_dir, repo, hash, change) = - replacement(b"keep\nold one\nold two\ntail\n", b"keep\ntail\n"); + // Pure deletions are recorded as line deletes, not as a rewrite; the + // review delta must read the same either way. + let (_dir, repo, hash, change) = edit(b"keep\nold one\nold two\ntail\n", b"keep\ntail\n"); let (files, _) = change_file_diffs(&repo, &change, &hash, &DiffOutputConfig::new()).unwrap(); assert_eq!(files[0].stats.insertions, 0); assert_eq!(files[0].stats.deletions, 2); @@ -106,7 +114,7 @@ fn replacement_deletion_does_not_show_survivors_as_added() { #[test] fn replacement_truncation_keeps_modified_file_status() { - let (_dir, repo, hash, change) = replacement(b"one\ntwo\n", b""); + let (_dir, repo, hash, change) = edit(b"one\ntwo\n", b""); let (files, _) = change_file_diffs( &repo, &change, @@ -122,7 +130,8 @@ fn replacement_truncation_keeps_modified_file_status() { #[test] fn replacement_diff_preserves_blank_lines_and_unterminated_last_line() { - let (_dir, repo, hash, change) = replacement(b"head\n\nold\n", b"head\n\nnew"); + // A missing final newline alone no longer forces a rewrite. + let (_dir, repo, hash, change) = edit(b"head\n\nold\n", b"head\n\nnew"); let (files, _) = change_file_diffs(&repo, &change, &hash, &DiffOutputConfig::new()).unwrap(); assert_eq!(files[0].stats.insertions, 1); assert_eq!(files[0].stats.deletions, 1); diff --git a/libatomic/examples/agent_sandbox_bench.rs b/libatomic/examples/agent_sandbox_bench.rs index 4894f3ee..91c0f1da 100644 --- a/libatomic/examples/agent_sandbox_bench.rs +++ b/libatomic/examples/agent_sandbox_bench.rs @@ -32,25 +32,43 @@ //! timing. There is no network RTT: the atomic transport is a loopback Unix //! socket and Git uses loopback `file://` pack negotiation; treat absolute //! numbers as transport-plus-work measurements, not WAN estimates. +#[cfg(unix)] use std::collections::BTreeMap; +#[cfg(unix)] use std::fs; +#[cfg(unix)] use std::io::Write; +#[cfg(unix)] use std::os::unix::net::UnixListener as StdUnixListener; +#[cfg(unix)] use std::path::{Path, PathBuf}; +#[cfg(unix)] use std::process::Command; +#[cfg(unix)] use std::sync::Arc; +#[cfg(unix)] use std::time::Instant; +#[cfg(unix)] use anyhow::{bail, ensure, Context, Result}; +#[cfg(unix)] use hyper_util::rt::TokioIo; +#[cfg(unix)] use libatomic::atomic::{sandbox_service_client::SandboxServiceClient, MaterializeRequest}; +#[cfg(unix)] use libatomic::daemon::services_sandbox::SandboxImpl; +#[cfg(unix)] use libatomic::daemon::state::DaemonState; +#[cfg(unix)] use serde::{Deserialize, Serialize}; +#[cfg(unix)] use tokio_stream::wrappers::UnixListenerStream; +#[cfg(unix)] use tonic::transport::{Endpoint, Uri}; +#[cfg(unix)] use tower::service_fn; +#[cfg(unix)] const CASES: &[&str] = &[ "atomic_rpc_full", "atomic_rpc_partial", @@ -59,8 +77,10 @@ const CASES: &[&str] = &[ ]; /// The partial subtree every partial case fetches: `files/east/`. +#[cfg(unix)] const PARTIAL_PREFIX: &str = "files/east"; +#[cfg(unix)] #[derive(Clone, Debug, Serialize, Deserialize)] struct Config { output: PathBuf, @@ -75,6 +95,7 @@ struct Config { span: bool, } +#[cfg(unix)] impl Config { fn parse() -> Result { let mut config = Self { @@ -158,21 +179,25 @@ impl Config { // ————————————————————————————— fixture ————————————————————————————— +#[cfg(unix)] fn generated_line(tree: &str, file: usize, line: usize) -> String { format!("{tree} file_{file:04} line_{line:06} value_00000000\n") } +#[cfg(unix)] fn edited_line(line: usize, edit: usize) -> String { format!("east file_0000 line_{line:06} value_{edit:08}\n") } /// One tracked file's golden bytes. +#[cfg(unix)] #[derive(Clone)] struct Golden { path: String, content: Vec, } +#[cfg(unix)] fn build_golden(config: &Config) -> Vec { let mut golden = Vec::new(); for (tree, count) in [("east", config.files_east), ("west", config.files_west)] { @@ -193,12 +218,14 @@ fn build_golden(config: &Config) -> Vec { golden } +#[cfg(unix)] fn east_paths(config: &Config) -> Vec { (0..config.files_east) .map(|file| format!("files/east/file_{file:04}.txt")) .collect() } +#[cfg(unix)] fn git(root: &Path, args: &[&str], tick: Option) -> Result<()> { let output = Command::new("git") .args(args) @@ -228,6 +255,7 @@ fn git(root: &Path, args: &[&str], tick: Option) -> Result<()> { /// Build the atomic host repository and the Git bare repository with the /// same synthetic content. Returns the atomic golden and the bare path. +#[cfg(unix)] fn prepare_fixtures( parent: &Path, config: &Config, @@ -343,6 +371,7 @@ fn prepare_fixtures( Ok((directory, golden, bare)) } +#[cfg(unix)] fn record(repo: &atomic_repository::Repository, message: &str, tick: usize) -> Result<()> { use atomic_core::change::ChangeHeader; use atomic_repository::RecordOptions; @@ -366,6 +395,7 @@ fn record(repo: &atomic_repository::Repository, message: &str, tick: usize) -> R /// Bind the production sandbox handlers on a private Unix socket. This is /// the `atomicd` shape minus the shell: one `DaemonState`, one registered /// repository, the generated tonic server. +#[cfg(unix)] fn serve( state: Arc, host_root: PathBuf, @@ -401,6 +431,7 @@ fn serve( // ————————————————————————————— client ————————————————————————————— +#[cfg(unix)] async fn connect(socket: &Path) -> Result> { let socket = socket.to_path_buf(); let channel = Endpoint::from_static("http://localhost") @@ -418,6 +449,7 @@ async fn connect(socket: &Path) -> Result Result> { let mut phases = BTreeMap::new(); let started = Instant::now(); @@ -514,6 +547,7 @@ fn git_clone_full(bare: &Path, sandbox: &Path) -> Result> // ————————————————————————————— verification ————————————————————————————— +#[cfg(unix)] fn file_mode(path: &Path) -> Result { use std::os::unix::fs::PermissionsExt; Ok(fs::metadata(path)?.permissions().mode() & 0o7777) @@ -521,6 +555,7 @@ fn file_mode(path: &Path) -> Result { /// Full sandboxes hold every tracked file; partial sandboxes hold exactly /// the east subtree and nothing else from the west or the root. +#[cfg(unix)] fn verify(sandbox: &Path, golden: &[Golden], partial: bool, east: &[String]) -> Result<()> { if partial { for path in east { @@ -565,6 +600,7 @@ fn verify(sandbox: &Path, golden: &[Golden], partial: bool, east: &[String]) -> // ————————————————————————————— harness ————————————————————————————— +#[cfg(unix)] #[derive(Debug, Serialize)] struct Sample { case: String, @@ -576,6 +612,7 @@ struct Sample { output_bytes: usize, } +#[cfg(unix)] fn run(config: Config) -> Result<()> { config.validate()?; let parent = config @@ -716,11 +753,19 @@ fn run(config: Config) -> Result<()> { Ok(()) } +/// The benchmark drives the daemon over a Unix domain socket, so there is +/// nothing to run elsewhere; the target still has to build on every platform. +#[cfg(not(unix))] +fn main() { + eprintln!("agent_sandbox_bench requires a Unix platform"); +} + +#[cfg(unix)] fn main() -> Result<()> { run(Config::parse()?) } -#[cfg(test)] +#[cfg(all(test, unix))] mod tests { use super::*; From 97a659dcf89a57066909c096bdf806587182b0ae Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Wed, 11 Nov 2026 12:05:51 -0600 Subject: [PATCH 14/14] test: unix-gate tests exercising the unix-only projected span pipeline Projected semantic recording is refused off-unix (lossless_unix_paths = cfg!(unix)), so the 16 stream tests and 2 native_span_record tests that exercise it failed on Windows CI. Gate them #[cfg(unix)], matching the existing pattern in both files. span_compiler_replays_imported_prefix_and_matches_fresh_extension gets a note: it raw-reads the open redb database to assert read-only validation, which Windows refuses with a sharing violation (OS error 33) regardless of the span pipeline. --- atomic-cli/src/commands/git/stream.rs | 20 +++++++++++++++++++ atomic-repository/tests/native_span_record.rs | 2 ++ 2 files changed, 22 insertions(+) diff --git a/atomic-cli/src/commands/git/stream.rs b/atomic-cli/src/commands/git/stream.rs index 067f6620..8dfc390c 100644 --- a/atomic-cli/src/commands/git/stream.rs +++ b/atomic-cli/src/commands/git/stream.rs @@ -1876,6 +1876,11 @@ mod tests { ); } + // Raw-reads the open redb database to assert prefix validation is + // read-only. On Windows, redb holds a byte-range lock on the + // memory-mapped file, so a second-handle read fails with a sharing + // violation (OS error 33) before the assertion can run. + #[cfg(unix)] #[test] fn span_compiler_replays_imported_prefix_and_matches_fresh_extension() { use atomic_core::pristine::{GitShaIndexTxnT, GraphTxnT, MutTxnT}; @@ -2007,6 +2012,7 @@ mod tests { ); } + #[cfg(unix)] #[test] fn incremental_span_private_stage_promotes_only_after_final_tree_proof() { use atomic_core::operation::OperationScope; @@ -2442,6 +2448,7 @@ mod tests { assert_eq!(std::fs::read(path).unwrap(), b"native user work\n"); } + #[cfg(unix)] #[test] fn incremental_span_private_stage_accepts_two_successive_updates() { let (_git_dir, git, tip, files) = fixture(7); @@ -2738,6 +2745,7 @@ mod tests { } } + #[cfg(unix)] #[test] fn incremental_span_git_observation_failure_keeps_target_and_resumes() { let (_git_dir, git, tip, files) = fixture(5); @@ -2815,6 +2823,7 @@ mod tests { } } + #[cfg(unix)] #[test] fn span_import_without_semantic_history_records_native_edit() { use atomic_core::operation::OperationScope; @@ -2922,6 +2931,7 @@ mod tests { assert_eq!(std::fs::read(path).unwrap(), b"alpha\nworking edit\nlast\n"); } + #[cfg(unix)] #[test] fn projected_native_span_edits_transfer_and_reopen() { use atomic_core::pristine::MutTxnT; @@ -3251,6 +3261,7 @@ mod tests { } } + #[cfg(unix)] #[test] fn projected_native_span_edits_compose_across_views_and_refuse_overlap() { let (_git_dir, git, tip, _) = fixture(2); @@ -3352,6 +3363,7 @@ mod tests { ); } + #[cfg(unix)] #[test] fn projected_native_record_undo_keeps_semantic_capability() { use atomic_core::pristine::{ @@ -3451,6 +3463,7 @@ mod tests { assert_eq!(std::fs::read(path).unwrap(), b"alpha\nworking edit\nlast\n"); } + #[cfg(unix)] #[test] fn projected_native_edits_preserve_empty_binary_and_line_endings() { let (_git_dir, git, tip, _) = fixture(2); @@ -3509,6 +3522,7 @@ mod tests { } } + #[cfg(unix)] #[test] fn projected_token_edits_roundtrip_common_sequences() { let (_git_dir, git, tip, _) = fixture(2); @@ -3581,6 +3595,7 @@ mod tests { } } + #[cfg(unix)] #[test] fn projected_native_edit_retains_unchanged_token_authorship() { use atomic_core::pristine::span_patch_storage::SpanRenderContext; @@ -3673,6 +3688,7 @@ mod tests { assert_eq!(unicode.lines[0].tokens[1].authors[0].target.owner, beta); } + #[cfg(unix)] #[test] fn projected_token_budget_refuses_before_record_publication() { use atomic_core::operation::OperationScope; @@ -3746,6 +3762,7 @@ mod tests { ); } + #[cfg(unix)] #[test] fn projected_and_native_crdt_files_can_share_one_record() { let (_git_dir, git, tip, _) = fixture(2); @@ -3888,6 +3905,7 @@ mod tests { ); } + #[cfg(unix)] #[test] fn projected_edit_with_wrong_baseline_or_path_refuses_before_publication() { use atomic_core::change::span_patch::SpanProjectionId; @@ -3995,6 +4013,7 @@ mod tests { } } + #[cfg(unix)] #[test] fn projected_record_object_collision_aborts_prepared_operation() { use atomic_core::operation::{OperationKind, OperationScope}; @@ -4092,6 +4111,7 @@ mod tests { assert_eq!(std::fs::read(stored).unwrap(), before_bytes); } + #[cfg(unix)] #[test] fn stale_projected_record_refuses_overlap_before_graph_commit() { use atomic_core::operation::OperationScope; diff --git a/atomic-repository/tests/native_span_record.rs b/atomic-repository/tests/native_span_record.rs index 41d2ba47..8d6a2462 100644 --- a/atomic-repository/tests/native_span_record.rs +++ b/atomic-repository/tests/native_span_record.rs @@ -169,6 +169,7 @@ fn native_span_sandbox_images_use_recorded_view_bytes() { assert_eq!(std::fs::read(path).unwrap(), b"unrecorded\n"); } +#[cfg(unix)] #[test] fn native_span_project_tree_uses_exact_recorded_bytes() { use atomic_core::operation::GitHashAlgorithm; @@ -1828,6 +1829,7 @@ fn native_text_binary_transitions_keep_content_and_semantics() { assert_eq!(std::fs::read(&path).unwrap(), b"new\ntext"); } +#[cfg(unix)] #[test] fn missing_span_root_refuses_without_overwriting_working_bytes() { use atomic_core::pristine::TreeTxnT;