diff --git a/atomic-core/Cargo.toml b/atomic-core/Cargo.toml index 11778709..c8e8960d 100644 --- a/atomic-core/Cargo.toml +++ b/atomic-core/Cargo.toml @@ -65,5 +65,6 @@ syn = "2" [features] default = [] +prototype-context-completion = [] # Enable additional debug assertions debug-checks = [] diff --git a/atomic-core/src/change/change.rs b/atomic-core/src/change/change.rs index d56eb9a6..169966ab 100644 --- a/atomic-core/src/change/change.rs +++ b/atomic-core/src/change/change.rs @@ -1044,6 +1044,10 @@ impl Change { collect_edge_update_hashes(inode, table)?; } GraphOp::SetAttr { inode, .. } => collect_position_hash(inode, table)?, + // Experimental context endpoints are encoded as full hashes, + // not repository IDs or compact graph-position indices. + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { .. } => {} } } diff --git a/atomic-core/src/change/format_v3/compact/convert.rs b/atomic-core/src/change/format_v3/compact/convert.rs index 5975e67e..47fdc012 100644 --- a/atomic-core/src/change/format_v3/compact/convert.rs +++ b/atomic-core/src/change/format_v3/compact/convert.rs @@ -30,6 +30,10 @@ impl<'t> Compactor<'t> { reason, })?; match op { + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { effect } => Ok(CompactGraphOp::PrototypeContext { + effect: effect.clone(), + }), GraphOp::FileAdd { add_name, add_inode, @@ -235,6 +239,10 @@ impl<'t> Compactor<'t> { reason, })?; let expanded: FormatResult>> = match op { + #[cfg(feature = "prototype-context-completion")] + CompactGraphOp::PrototypeContext { effect } => Ok(GraphOp::PrototypeContext { + effect: effect.clone(), + }), CompactGraphOp::FileAdd { add_name, add_inode, diff --git a/atomic-core/src/change/format_v3/compact/graph_op.rs b/atomic-core/src/change/format_v3/compact/graph_op.rs index 69f48e03..bed89845 100644 --- a/atomic-core/src/change/format_v3/compact/graph_op.rs +++ b/atomic-core/src/change/format_v3/compact/graph_op.rs @@ -195,6 +195,11 @@ pub enum CompactGraphOp { /// Canonical attribute value. value: InodeAttr, }, + /// Private experimental sandbox operation; not a production discriminant. + #[cfg(feature = "prototype-context-completion")] + PrototypeContext { + effect: crate::change::prototype_context::PrototypeContextEffect, + }, } impl CompactGraphOp { @@ -273,6 +278,8 @@ impl CompactGraphOp { | CompactGraphOp::ResurrectZombies { local, .. } => Some(&local.path), CompactGraphOp::SetAttr { path, .. } => Some(path), CompactGraphOp::AddRoot { .. } | CompactGraphOp::DelRoot { .. } => None, + #[cfg(feature = "prototype-context-completion")] + CompactGraphOp::PrototypeContext { .. } => None, } } @@ -296,6 +303,8 @@ impl CompactGraphOp { CompactGraphOp::AddRoot { .. } => "AddRoot", CompactGraphOp::DelRoot { .. } => "DelRoot", CompactGraphOp::SetAttr { .. } => "SetAttr", + #[cfg(feature = "prototype-context-completion")] + CompactGraphOp::PrototypeContext { .. } => "PrototypeContext", } } } diff --git a/atomic-core/src/change/format_v3/sections.rs b/atomic-core/src/change/format_v3/sections.rs index 51c4bacc..75c4e6c4 100644 --- a/atomic-core/src/change/format_v3/sections.rs +++ b/atomic-core/src/change/format_v3/sections.rs @@ -46,10 +46,12 @@ //! modify the repository DAG (vertex insertions, edge updates). This is the //! minimum required to apply a change. A "thin pull" downloads only these. //! -//! - **SEMANTIC section**: The display/analysis layer. Contains [`FileOps`] with +//! - **SEMANTIC section**: The human-readable semantic layer. Contains [`FileOps`] with //! Trunk/Branch/Leaf operations for line-level and token-level diffs, blame, -//! and code review. This is optional — it can be regenerated from graph + content. -//! A "thin review" downloads only these + content chunks. +//! and code review. Span projections retain original authored fragments; +//! graph bytes alone cannot recover authenticated semantic names. +//! Historical graph-only objects remain readable, but must not be presented as +//! a complete semantic interpretation. Selective readers must retain context. //! //! Both sections for the same file share the same `content_range`, which indexes //! into the change's content chunks. This enables the reader to correlate diff --git a/atomic-core/src/change/graph_op.rs b/atomic-core/src/change/graph_op.rs index 602d181c..d6ffe142 100644 --- a/atomic-core/src/change/graph_op.rs +++ b/atomic-core/src/change/graph_op.rs @@ -340,6 +340,11 @@ pub enum GraphOp { /// Canonical attribute value. value: InodeAttr, }, + /// Private experimental context relation; requires the sandbox capability. + #[cfg(feature = "prototype-context-completion")] + PrototypeContext { + effect: super::prototype_context::PrototypeContextEffect, + }, } impl GraphOp { @@ -551,6 +556,8 @@ impl GraphOp { | GraphOp::ResurrectZombies { local, .. } => Some(&local.path), GraphOp::AddRoot { .. } | GraphOp::DelRoot { .. } => None, + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { .. } => None, } } @@ -659,6 +666,8 @@ impl GraphOp { GraphOp::AddRoot { .. } => "AddRoot", GraphOp::DelRoot { .. } => "DelRoot", GraphOp::SetAttr { .. } => "SetAttr", + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { .. } => "PrototypeContext", } } } @@ -691,6 +700,8 @@ impl fmt::Display for GraphOp { GraphOp::AddRoot { .. } => write!(f, "AddRoot"), GraphOp::DelRoot { .. } => write!(f, "DelRoot"), GraphOp::SetAttr { path, value, .. } => write!(f, "SetAttr: {path} {value:?}"), + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { effect } => write!(f, "PrototypeContext: {effect:?}"), } } } @@ -799,6 +810,8 @@ impl<'a, H> Iterator for HunkAtomIter<'a, H> { // Attribute events are applied outside the edge graph. (GraphOp::SetAttr { .. }, _) => None, + #[cfg(feature = "prototype-context-completion")] + (GraphOp::PrototypeContext { .. }, _) => None, }; if result.is_some() { @@ -858,6 +871,8 @@ impl GraphOp { GraphOp::DirDel { .. } => 1, GraphOp::DirUndel { .. } => 1, GraphOp::SetAttr { .. } => 0, + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { .. } => 0, } } } diff --git a/atomic-core/src/change/mod.rs b/atomic-core/src/change/mod.rs index e798d7af..f83e469b 100644 --- a/atomic-core/src/change/mod.rs +++ b/atomic-core/src/change/mod.rs @@ -102,8 +102,11 @@ mod graph_op; mod header; mod local; pub mod ops; +#[cfg(feature = "prototype-context-completion")] +pub mod prototype_context; mod provenance; pub mod provenance_graph; +pub mod semantic_context; pub mod session; pub mod signing; pub mod span_patch; diff --git a/atomic-core/src/change/ops.rs b/atomic-core/src/change/ops.rs index 912e3cc9..52353ce0 100644 --- a/atomic-core/src/change/ops.rs +++ b/atomic-core/src/change/ops.rs @@ -322,10 +322,25 @@ impl FileOps { } /// Translate existing semantic identities while preserving ROOT placeholders. + /// + /// Resolve each distinct external owner once, before changing any operation. + /// If resolution fails, `self` is unchanged. The caller supplies the owner + /// mapping; this does not infer names from paths, application order or local + /// numeric coincidences. pub fn remap_referenced_node_ids( &mut self, mut remap: impl FnMut(crate::types::NodeId) -> Result, ) -> Result<(), E> { + let mapping: std::collections::BTreeMap<_, _> = self + .referenced_node_ids() + .into_iter() + .map(|id| remap(id).map(|resolved| (id, resolved))) + .collect::>()?; + let mut remap = |id| -> Result { + // referenced_node_ids enumerates the same non-ROOT fields walked + // below. All fallible resolution finished before the first write. + Ok(mapping[&id]) + }; fn trunk( id: &mut TrunkId, f: &mut impl FnMut(crate::types::NodeId) -> Result, @@ -1154,6 +1169,211 @@ mod tests { BranchId::new(NodeId::new(1), idx) } + // These are representation laws for transport of patch names, not a claim + // that arbitrary overlapping patches commute or that view removal is an + // inverse patch. ROOT is the self-name, not an external owner to transport. + fn transport_fixtures() -> Vec { + let trunk = TrunkId::new(NodeId::new(2), 17); + let branch = |owner, index| BranchId::new(NodeId::new(owner), index); + let leaf = |owner, index| LeafId::new(NodeId::new(owner), index); + let tokens = vec![ + LeafOp::Insert { + after: Some(leaf(0, 11)), + kind: TokenKind::Word, + content: b"new".to_vec(), + }, + LeafOp::Insert { + after: Some(leaf(5, 12)), + kind: TokenKind::Whitespace, + content: b" ".to_vec(), + }, + LeafOp::Delete { leaf: leaf(6, 13) }, + LeafOp::Replace { + leaf: leaf(7, 14), + new_content: b"replacement".to_vec(), + }, + LeafOp::Restore { leaf: leaf(8, 15) }, + ]; + let lines = vec![ + LineOps::new( + branch(0, 20), + BranchOp::Insert { + after: Some(branch(3, 21)), + content: tokens.clone(), + }, + ) + .with_content_range(ChangePosition::new(100), ChangePosition::new(120)), + LineOps::new( + branch(3, 21), + BranchOp::Delete { + branch: branch(4, 22), + content: tokens.clone(), + }, + ), + LineOps::new( + branch(4, 22), + BranchOp::Modify { + branch: branch(3, 21), + old_content: tokens.clone(), + new_content: tokens, + }, + ), + LineOps::new( + branch(0, 23), + BranchOp::Restore { + branch: branch(4, 22), + }, + ), + LineOps::new( + branch(3, 24), + BranchOp::Reparent { + branch: branch(4, 25), + new_after: Some(branch(0, 26)), + }, + ), + ]; + let variants = vec![ + None, + Some(TrunkOp::Create { + path: "f".into(), + encoding: Some(Encoding::Utf8), + }), + Some(TrunkOp::Delete { trunk }), + Some(TrunkOp::Move { + trunk, + new_path: "renamed".into(), + }), + Some(TrunkOp::Undelete { trunk }), + Some(TrunkOp::SetMode { trunk, mode: 0o644 }), + Some(TrunkOp::SetKind { + trunk, + kind: crate::change::InodeKind::Regular, + }), + ]; + let mut fixtures: Vec<_> = variants + .into_iter() + .map(|trunk_op| FileOps { + trunk_id: trunk, + path: "f".into(), + trunk_op, + line_ops: lines.clone(), + }) + .collect(); + fixtures.push(FileOps::projected_span_edit( + "projected".into(), + crate::change::span_patch::ExternalFile { + owner: crate::types::Hash::of(b"portable owner is not a local ID"), + ordinal: 7, + }, + crate::change::span_patch::SpanProjectionId(crate::types::Hash::of(b"projection")), + )); + fixtures + } + + #[test] + fn semantic_transport_identity_composition_and_inverse() { + for original in transport_fixtures() { + let mut identity = original.clone(); + identity.remap_referenced_node_ids::<()>(Ok).unwrap(); + assert_eq!(identity, original); + + let mut sequential = original.clone(); + sequential + .remap_referenced_node_ids::<()>(|id| Ok(NodeId::new(id.get() + 100))) + .unwrap(); + sequential + .remap_referenced_node_ids::<()>(|id| Ok(NodeId::new(id.get() * 3))) + .unwrap(); + let mut composed = original.clone(); + composed + .remap_referenced_node_ids::<()>(|id| Ok(NodeId::new((id.get() + 100) * 3))) + .unwrap(); + assert_eq!( + sequential, composed, + "transport must compose as name substitution" + ); + + let mut roundtrip = original.clone(); + roundtrip + .remap_referenced_node_ids::<()>(|id| Ok(NodeId::new(id.get() + 100))) + .unwrap(); + roundtrip + .remap_referenced_node_ids::<()>(|id| Ok(NodeId::new(id.get() - 100))) + .unwrap(); + assert_eq!( + roundtrip, original, + "inverse renaming must preserve all non-name fields and ROOT" + ); + } + } + + #[test] + fn semantic_transport_disjoint_renamings_commute() { + let left = |id: NodeId| -> Result { + Ok(if id.get() == 2 { NodeId::new(102) } else { id }) + }; + let right = |id: NodeId| -> Result { + Ok(if id.get() == 3 { NodeId::new(103) } else { id }) + }; + for original in transport_fixtures() { + let mut left_right = original.clone(); + left_right.remap_referenced_node_ids(left).unwrap(); + left_right.remap_referenced_node_ids(right).unwrap(); + let mut right_left = original; + right_left.remap_referenced_node_ids(right).unwrap(); + right_left.remap_referenced_node_ids(left).unwrap(); + assert_eq!(left_right, right_left); + } + } + + #[test] + fn semantic_transport_resolves_each_external_owner_once() { + for original in transport_fixtures() { + let owners = original.referenced_node_ids(); + let mut observed = std::collections::BTreeSet::new(); + let mut transported = original; + transported + .remap_referenced_node_ids::<()>(|id| { + assert!(!id.is_root(), "ROOT must retain its self-relative meaning"); + assert!( + observed.insert(id), + "one owner must not be resolved inconsistently at different occurrences" + ); + Ok(NodeId::new(id.get() + 100)) + }) + .unwrap(); + assert_eq!(observed, owners); + assert_eq!( + transported.referenced_node_ids(), + owners + .iter() + .map(|id| NodeId::new(id.get() + 100)) + .collect() + ); + } + } + + #[test] + fn semantic_transport_failure_leaves_the_patch_unchanged() { + for original in transport_fixtures() { + for missing in original.referenced_node_ids() { + let mut transported = original.clone(); + let result = transported.remap_referenced_node_ids(|id| { + if id == missing { + Err(id) + } else { + Ok(NodeId::new(id.get() + 100)) + } + }); + assert_eq!(result, Err(missing)); + assert_eq!( + transported, original, + "an undefined transport must not leave a partly renamed patch" + ); + } + } + } + // FileOps Tests #[test] diff --git a/atomic-core/src/change/prototype_context.rs b/atomic-core/src/change/prototype_context.rs new file mode 100644 index 00000000..f234e4eb --- /dev/null +++ b/atomic-core/src/change/prototype_context.rs @@ -0,0 +1,30 @@ +//! Experimental sandbox patch operations. No production opcode is finalized. +use crate::types::Hash; +use serde::{Deserialize, Serialize}; + +/// An exact operation identity. External completion carriers use ordinal zero; +/// an own-name declaration is part of its content patch at its actual ordinal. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct ContextEffectRef { + pub change: Hash, + pub ordinal: u32, +} + +/// Add a context-name relation or reverse one exact observed effect. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum PrototypeContextEffect { + Bind { + consumer: Hash, + symbol: u64, + provider: Hash, + }, + Reverse { + observed: ContextEffectRef, + }, + /// Names captured in this newly authored patch's own encoding frame. + /// Unlike Bind, this does not claim to recover an old author's context. + /// The ordered pairs cover every foreign semantic owner exactly once. + DeclareOwnNames { + owners: Vec<(u64, Hash)>, + }, +} diff --git a/atomic-core/src/change/semantic_context.rs b/atomic-core/src/change/semantic_context.rs new file mode 100644 index 00000000..4c01a01e --- /dev/null +++ b/atomic-core/src/change/semantic_context.rs @@ -0,0 +1,354 @@ +//! Change-scoped semantic names and their portable owner identities. +//! +//! A frame describes one author's encoding, never repository-wide aliases. +//! Resolving it proves dependency visibility before transporting a copy of the +//! operations. Capturing new names and reconstructing missing legacy context +//! remain separate responsibilities of the record and completion workflows. + +use super::FileOps; +use crate::pristine::{EffectiveProjectionClosure, GraphTxnT, PristineError, ViewMembershipSet}; +use crate::types::{Hash, NodeId}; +use serde::{Deserialize, Serialize}; +use std::collections::{BTreeMap, BTreeSet}; +use thiserror::Error; + +/// A non-ROOT name in one change's encoding and the change that owns it. +/// +/// Field order preserves the existing span semantic-owner wire representation. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct SemanticOwner { + /// Non-ROOT numeric name used by this change's semantic encoding. + pub authored_id: u64, + /// Immutable identity of the change that introduced the named items. + pub owner: Hash, +} + +/// A declaration or receiver-context failure, before caller operations mutate. +#[derive(Debug, Error)] +pub enum SemanticContextError { + #[error("ROOT is a self-relative placeholder, not a foreign semantic name")] + RootName, + #[error("a semantic owner must identify a real change, not ROOT")] + RootOwner, + #[error("semantic owner names must be strictly ordered without duplicates")] + NonCanonicalNames, + #[error("distinct semantic owner names cannot alias the same change {0}")] + AliasedOwner(Hash), + #[error("semantic identities lack a complete portable owner mapping (missing {missing:?}, unused {unused:?})")] + IncompleteMapping { + missing: Vec, + unused: Vec, + }, + #[error("semantic dependency missing: {0}")] + MissingDependency(Hash), + #[error("semantic owner missing: {0}")] + MissingOwner(Hash), + #[error("semantic owner {0} is outside its dependency closure")] + OutsideDependencyClosure(Hash), + #[error("semantic owner {owner} does not round-trip through receiver id {local_id}")] + InconsistentReceiverIdentity { owner: Hash, local_id: NodeId }, + #[error(transparent)] + Database(#[from] PristineError), +} + +/// Validated declarations for one immutable change's semantic encoding frame. +/// +/// This in-memory contract validates the existing span owner declarations; +/// it does not add another stored representation or duplicate file content. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SemanticNameFrame { + owners: Vec, +} + +impl SemanticNameFrame { + /// Validate strictly ordered, one-to-one foreign-owner declarations. + /// Invalid input is rejected, not normalized into a different declaration. + /// + /// # Examples + /// + /// ``` + /// use atomic_core::change::{FileOps, semantic_context::{SemanticNameFrame, SemanticOwner}}; + /// use atomic_core::{crdt::TrunkId, types::{Hash, NodeId}}; + /// let frame = SemanticNameFrame::new(vec![SemanticOwner { + /// authored_id: 91, + /// owner: Hash::of(b"creating change"), + /// }])?; + /// let ops = vec![FileOps::edit(TrunkId::new(NodeId::new(91), 0), "src/lib.rs".into())]; + /// frame.validate_for(&ops)?; + /// # Ok::<(), atomic_core::change::semantic_context::SemanticContextError>(()) + /// ``` + pub fn new(owners: Vec) -> Result { + if owners.iter().any(|entry| entry.authored_id == 0) { + return Err(SemanticContextError::RootName); + } + if owners.iter().any(|entry| entry.owner == Hash::NONE) { + return Err(SemanticContextError::RootOwner); + } + if owners + .windows(2) + .any(|pair| pair[0].authored_id >= pair[1].authored_id) + { + return Err(SemanticContextError::NonCanonicalNames); + } + let mut hashes = BTreeSet::new(); + for entry in &owners { + if !hashes.insert(entry.owner) { + return Err(SemanticContextError::AliasedOwner(entry.owner)); + } + } + Ok(Self { owners }) + } + + pub fn owners(&self) -> &[SemanticOwner] { + &self.owners + } + + /// Require exactly one declaration for every foreign name, with no unused + /// declarations that could silently expand the operation's context. + pub fn validate_for(&self, ops: &[FileOps]) -> Result<(), SemanticContextError> { + let used: BTreeSet<_> = ops.iter().flat_map(FileOps::referenced_node_ids).collect(); + let declared: BTreeSet<_> = self + .owners + .iter() + .map(|entry| NodeId::new(entry.authored_id)) + .collect(); + let missing = used.difference(&declared).copied().collect::>(); + let unused = declared.difference(&used).copied().collect::>(); + if !missing.is_empty() || !unused.is_empty() { + return Err(SemanticContextError::IncompleteMapping { missing, unused }); + } + Ok(()) + } + + /// Transport a copy into a receiver's numeric encoding frame, using only + /// hash owners visible through the explicit patch dependency closure. + /// ROOT names remain self-relative; the input is unchanged on every error. + pub fn localize( + &self, + txn: &T, + dependencies: &[Hash], + ops: &[FileOps], + ) -> Result, SemanticContextError> { + self.validate_for(ops)?; + let roots = dependencies + .iter() + .map(|hash| { + txn.get_internal(hash)? + .ok_or(SemanticContextError::MissingDependency(*hash)) + }) + .collect::, SemanticContextError>>()?; + let closure = EffectiveProjectionClosure::try_from_membership( + txn, + &ViewMembershipSet::from_ordered(roots), + )?; + let mut mapping = BTreeMap::new(); + for entry in &self.owners { + let id = txn + .get_internal(&entry.owner)? + .ok_or(SemanticContextError::MissingOwner(entry.owner))?; + if id.is_root() || txn.get_external(id)? != Some(entry.owner) { + return Err(SemanticContextError::InconsistentReceiverIdentity { + owner: entry.owner, + local_id: id, + }); + } + if !closure.contains(id) { + return Err(SemanticContextError::OutsideDependencyClosure(entry.owner)); + } + mapping.insert(NodeId::new(entry.authored_id), id); + } + let mut transported = ops.to_vec(); + for file in &mut transported { + file.remap_referenced_node_ids(|id| { + mapping + .get(&id) + .copied() + .ok_or_else(|| SemanticContextError::IncompleteMapping { + missing: vec![id], + unused: vec![], + }) + })?; + } + Ok(transported) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::change::LineOps; + use crate::crdt::{BranchId, BranchOp, TrunkId}; + use crate::pristine::{MutTxnT, Pristine}; + + fn owner(symbol: u64, hash: Hash) -> SemanticOwner { + SemanticOwner { + authored_id: symbol, + owner: hash, + } + } + fn operations(symbol: u64) -> Vec { + let mut file = FileOps::edit(TrunkId::new(NodeId::new(symbol), 2), "f".into()); + file.add_line_op(LineOps::new( + BranchId::new(NodeId::ROOT, 0), + BranchOp::Insert { + after: Some(BranchId::new(NodeId::new(symbol), 4)), + content: vec![], + }, + )); + vec![file] + } + + #[test] + fn declarations_reject_root_unordered_duplicate_and_aliased_names() { + let a = Hash::of(b"a"); + let b = Hash::of(b"b"); + assert!(matches!( + SemanticNameFrame::new(vec![owner(0, a)]), + Err(SemanticContextError::RootName) + )); + assert!(matches!( + SemanticNameFrame::new(vec![owner(1, Hash::NONE)]), + Err(SemanticContextError::RootOwner) + )); + for entries in [ + vec![owner(2, a), owner(1, b)], + vec![owner(1, a), owner(1, b)], + ] { + assert!(matches!( + SemanticNameFrame::new(entries), + Err(SemanticContextError::NonCanonicalNames) + )); + } + assert!(matches!( + SemanticNameFrame::new(vec![owner(1, a), owner(2, a)]), + Err(SemanticContextError::AliasedOwner(_)) + )); + } + + #[test] + fn exact_coverage_reports_missing_and_unused_names() { + let frame = SemanticNameFrame::new(vec![owner(7, Hash::of(b"a"))]).unwrap(); + let input = operations(9); + let before = input.clone(); + assert!( + matches!(frame.validate_for(&input), Err(SemanticContextError::IncompleteMapping {missing, unused}) + if missing == vec![NodeId::new(9)] && unused == vec![NodeId::new(7)]) + ); + assert_eq!(input, before); + } + + #[test] + fn localization_is_change_scoped_transitive_and_allocation_independent() { + let a = Hash::of(b"owner"); + let parent = Hash::of(b"explicit parent"); + let input = operations(91); + let before = input.clone(); + let frame = SemanticNameFrame::new(vec![owner(91, a)]).unwrap(); + for padding in [0, 7] { + let dir = tempfile::tempdir().unwrap(); + let pristine = Pristine::open(dir.path().join("db")).unwrap(); + let mut txn = pristine.write_txn().unwrap(); + for i in 0..padding { + txn.register_change(&Hash::of(format!("padding {i}").as_bytes())) + .unwrap(); + } + let id = txn.register_change(&a).unwrap(); + txn.put_change_deps(id, &[]).unwrap(); + let parent_id = txn.register_change(&parent).unwrap(); + txn.put_change_deps(parent_id, &[a]).unwrap(); + let localized = frame.localize(&txn, &[parent], &input).unwrap(); + assert_eq!(localized[0].trunk_id(), TrunkId::new(id, 2)); + assert_eq!( + localized[0].line_ops()[0].branch_id(), + BranchId::new(NodeId::ROOT, 0) + ); + assert!( + matches!(localized[0].line_ops()[0].operation(), BranchOp::Insert {after:Some(after),..} if *after == BranchId::new(id,4)) + ); + assert_eq!(input, before); + let other = Hash::of(b"other owner"); + let other_id = txn.register_change(&other).unwrap(); + txn.put_change_deps(other_id, &[]).unwrap(); + let other_frame = SemanticNameFrame::new(vec![owner(91, other)]).unwrap(); + assert_eq!( + other_frame.localize(&txn, &[other], &input).unwrap()[0].trunk_id(), + TrunkId::new(other_id, 2) + ); + } + } + + #[test] + fn missing_or_out_of_context_owners_never_mutate_operations() { + let dir = tempfile::tempdir().unwrap(); + let pristine = Pristine::open(dir.path().join("db")).unwrap(); + let mut txn = pristine.write_txn().unwrap(); + let a = Hash::of(b"owner"); + let input = operations(91); + let before = input.clone(); + let frame = SemanticNameFrame::new(vec![owner(91, a)]).unwrap(); + assert!(matches!( + frame.localize(&txn, &[a], &input), + Err(SemanticContextError::MissingDependency(_)) + )); + assert!(matches!( + frame.localize(&txn, &[], &input), + Err(SemanticContextError::MissingOwner(_)) + )); + txn.register_change(&a).unwrap(); + assert!(matches!( + frame.localize(&txn, &[], &input), + Err(SemanticContextError::OutsideDependencyClosure(_)) + )); + assert_eq!(input, before); + } + + #[test] + fn root_only_operations_need_no_foreign_declarations() { + let dir = tempfile::tempdir().unwrap(); + let pristine = Pristine::open(dir.path().join("db")).unwrap(); + let input = vec![FileOps::edit(TrunkId::new(NodeId::ROOT, 2), "f".into())]; + assert_eq!( + SemanticNameFrame::new(vec![]) + .unwrap() + .localize(&pristine.read_txn().unwrap(), &[], &input) + .unwrap(), + input + ); + } + + #[test] + fn later_owner_failure_preserves_every_file_and_unindexed_context_refuses() { + let dir = tempfile::tempdir().unwrap(); + let pristine = Pristine::open(dir.path().join("db")).unwrap(); + let mut txn = pristine.write_txn().unwrap(); + let a = Hash::of(b"a"); + let b = Hash::of(b"missing b"); + let id = txn.register_change(&a).unwrap(); + let mut input = operations(91); + input.extend(operations(92)); + let before = input.clone(); + let frame = SemanticNameFrame::new(vec![owner(91, a), owner(92, b)]).unwrap(); + assert!(matches!( + frame.localize(&txn, &[a], &input), + Err(SemanticContextError::Database(_)) + )); + txn.put_change_deps(id, &[]).unwrap(); + assert!( + matches!(frame.localize(&txn,&[a],&input),Err(SemanticContextError::MissingOwner(hash)) if hash == b) + ); + assert_eq!(input, before); + } + + #[test] + fn existing_span_owner_wire_bytes_and_public_import_path_are_unchanged() { + use super::super::span_patch::{SemanticOwner as ExistingPublicOwner, SpanPatch}; + let mut patch = SpanPatch::new(vec![], vec![]); + let entry: ExistingPublicOwner = owner(300, Hash::from_bytes([0xa5; 32])); + patch.semantic_owners.push(entry); + let mut expected = b"ATSP".to_vec(); + expected.extend_from_slice(&[2, 0, 0, 1, 0xac, 2]); + expected.extend_from_slice(&[0xa5; 32]); + assert_eq!(patch.encode().unwrap(), expected); + assert_eq!(SpanPatch::decode(&expected).unwrap(), patch); + } +} diff --git a/atomic-core/src/change/span_patch.rs b/atomic-core/src/change/span_patch.rs index 46e50ee0..7c405d81 100644 --- a/atomic-core/src/change/span_patch.rs +++ b/atomic-core/src/change/span_patch.rs @@ -8,15 +8,13 @@ use serde::{Deserialize, Serialize}; const MAGIC: &[u8; 4] = b"ATSP"; const VERSION: u8 = 2; +/// Existing span algebra, additionally permitting verified legacy graph ranges +/// as authored sources. Requires the span-legacy-context reader fence. +pub const LEGACY_SOURCE_VERSION: u8 = 3; -/// 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, -} +// Preserve the public import path and encoded field order for existing callers. +use super::semantic_context::SemanticNameFrame; +pub use super::semantic_context::SemanticOwner; /// Portable identity of a file created by a change. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -131,7 +129,7 @@ impl SpanPatch { /// Encode the canonical span payload. pub fn encode(&self) -> Result, SpanPatchError> { - if self.version != 1 && self.version != VERSION { + if self.version != 1 && self.version != VERSION && self.version != LEGACY_SOURCE_VERSION { return Err(SpanPatchError::UnsupportedVersion(self.version)); } let mut bytes = MAGIC.to_vec(); @@ -159,7 +157,7 @@ impl SpanPatch { .strip_prefix(MAGIC) .ok_or(SpanPatchError::WrongMagic)?; let version = *payload.first().ok_or(SpanPatchError::InvalidEncoding)?; - if version != 1 && version != VERSION { + if version != 1 && version != VERSION && version != LEGACY_SOURCE_VERSION { return Err(SpanPatchError::UnsupportedVersion(version)); } let (patch, remaining) = if version == 1 { @@ -180,7 +178,8 @@ impl SpanPatch { if !remaining.is_empty() { return Err(SpanPatchError::InvalidEncoding); } - if patch.version != 1 && patch.version != VERSION { + if patch.version != 1 && patch.version != VERSION && patch.version != LEGACY_SOURCE_VERSION + { return Err(SpanPatchError::UnsupportedVersion(patch.version)); } Ok(patch) @@ -189,31 +188,14 @@ impl SpanPatch { /// 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 { + if self.version != 1 && self.version != VERSION && self.version != LEGACY_SOURCE_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); - } + SemanticNameFrame::new(self.semantic_owners.clone()) + .map_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 => { @@ -256,60 +238,17 @@ 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) + Ok( + SemanticNameFrame::new(patch.semantic_owners.clone())?.localize( + txn, + change.dependencies(), + change.file_ops(), + )?, + ) } #[cfg(test)] diff --git a/atomic-core/src/output/crdt.rs b/atomic-core/src/output/crdt.rs index ea62be02..b4cc3826 100644 --- a/atomic-core/src/output/crdt.rs +++ b/atomic-core/src/output/crdt.rs @@ -463,6 +463,8 @@ pub enum CrdtOutputError { Pristine(crate::pristine::PristineError), /// Change store read failed. Store(E), + /// Immutable span edit metadata could not be verified. + VerifiedStore(String), /// A branch was alive but had no BRANCH_VERTEX mapping — the CRDT layer /// has no way to know which bytes the branch corresponds to. /// @@ -470,6 +472,9 @@ pub enum CrdtOutputError { /// missing its content reference. This is recoverable: callers can fall /// back to the byte-graph walker for the affected file. OrphanBranch(crate::crdt::BranchId), + /// Use canonical view-aware retrieval; the retained CRDT cache is not the + /// interpretation of authored span edits. + SpanContextRequired(crate::crdt::TrunkId), } impl std::fmt::Display for CrdtOutputError { @@ -477,9 +482,13 @@ impl std::fmt::Display for CrdtOutputError { match self { CrdtOutputError::Pristine(e) => write!(f, "pristine error: {}", e), CrdtOutputError::Store(e) => write!(f, "change store error: {}", e), + CrdtOutputError::VerifiedStore(e) => write!(f, "verified change store error: {e}"), CrdtOutputError::OrphanBranch(b) => { write!(f, "branch {} has no BRANCH_VERTEX mapping", b) } + CrdtOutputError::SpanContextRequired(trunk) => { + write!(f, "trunk {trunk} requires explicit view/span context") + } } } } @@ -517,6 +526,8 @@ impl From for CrdtOutputError { /// /// # Caveats /// +/// - Files with span-projected semantics return [`CrdtOutputError::SpanContextRequired`]. Use +/// canonical view-aware retrieval rather than falling back to retained caches. /// - Reads `branch.state` directly — correct for single-view linear /// history, *not* multi-view scenarios where the same branch may be /// alive on one view and deleted on another. Add a `change_filter` @@ -554,6 +565,28 @@ where { use crate::types::Hash; + for author in txn.crdt_span_edit_authors(trunk_id)? { + let hash = + txn.get_external(author)? + .ok_or(crate::pristine::PristineError::Inconsistent { + message: "span edit author has no external identity".into(), + })?; + let change = changes + .get_verified_change(&hash) + .map_err(|error| CrdtOutputError::VerifiedStore(error.to_string()))?; + change + .check_hash(&hash) + .map_err(|error| CrdtOutputError::VerifiedStore(error.to_string()))?; + if change.change().file_ops().iter().any(|ops| { + matches!( + ops.trunk_op(), + Some(crate::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) { + return Err(CrdtOutputError::SpanContextRequired(trunk_id)); + } + } + let mut out: Vec = Vec::new(); for branch_id in iter_trunk_branches_in_file_order(txn, trunk_id)? { diff --git a/atomic-core/src/pristine/capability.rs b/atomic-core/src/pristine/capability.rs index de540820..821b9add 100644 --- a/atomic-core/src/pristine/capability.rs +++ b/atomic-core/src/pristine/capability.rs @@ -35,6 +35,15 @@ pub const SPAN_RANGE_CAPABILITY: RepositoryCapability = pub const SPAN_PROJECTED_SEMANTICS_CAPABILITY: RepositoryCapability = RepositoryCapability::new("span-projected-semantics", 1); +/// Span edits may address original legacy graph ranges without copying them. +pub const SPAN_LEGACY_CONTEXT_CAPABILITY: RepositoryCapability = + RepositoryCapability::new("span-legacy-context", 1); + +/// Private experimental artifacts; ordinary builds must refuse these sandboxes. +#[cfg(feature = "prototype-context-completion")] +pub const PROTOTYPE_CONTEXT_CAPABILITY: RepositoryCapability = + RepositoryCapability::new("prototype-semantic-context-NOT-FOR-PUBLICATION", 1); + /// Capabilities supported by this Atomic build. pub const SUPPORTED_REPOSITORY_CAPABILITIES: &[RepositoryCapability] = &[ CHANGE_FORMAT_VNEXT_CAPABILITY, @@ -43,6 +52,9 @@ pub const SUPPORTED_REPOSITORY_CAPABILITIES: &[RepositoryCapability] = &[ GIT_CLOSURE_SHARING_CAPABILITY, SPAN_RANGE_CAPABILITY, SPAN_PROJECTED_SEMANTICS_CAPABILITY, + SPAN_LEGACY_CONTEXT_CAPABILITY, + #[cfg(feature = "prototype-context-completion")] + PROTOTYPE_CONTEXT_CAPABILITY, ]; /// A repository capability and the minimum version required to use it. diff --git a/atomic-core/src/pristine/mod.rs b/atomic-core/src/pristine/mod.rs index d57cca6c..307ec540 100644 --- a/atomic-core/src/pristine/mod.rs +++ b/atomic-core/src/pristine/mod.rs @@ -177,11 +177,13 @@ pub use attribute::{ INODE_ATTR_EVENT_VERSION, }; pub use bindings::{BindingMutTxnT, BindingStoreOutcome, BindingTxnT}; +#[cfg(feature = "prototype-context-completion")] +pub use capability::PROTOTYPE_CONTEXT_CAPABILITY; pub use capability::{ RepositoryCapability, RequiredRepositoryCapability, UnsupportedRepositoryCapability, CHANGE_FORMAT_VNEXT_CAPABILITY, GIT_CLOSURE_SHARING_CAPABILITY, REQUIRED_CAPABILITY_PREFIX, - SHIM_CUTOVER_CAPABILITY, SPAN_PROJECTED_SEMANTICS_CAPABILITY, SPAN_RANGE_CAPABILITY, - SUPPORTED_REPOSITORY_CAPABILITIES, + SHIM_CUTOVER_CAPABILITY, SPAN_LEGACY_CONTEXT_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/span_patch_storage.rs b/atomic-core/src/pristine/span_patch_storage.rs index f5c6abf7..71b229bd 100644 --- a/atomic-core/src/pristine/span_patch_storage.rs +++ b/atomic-core/src/pristine/span_patch_storage.rs @@ -18,6 +18,8 @@ use redb::{MultimapTableDefinition, ReadableMultimapTable, ReadableTable, TableD use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::sync::Arc; use std::time::{Duration, Instant}; +mod legacy; +pub use legacy::LegacySpanGraph; pub(super) const FILES: TableDefinition<&[u8; 36], u64> = TableDefinition::new("span_patch_files_v1"); @@ -50,6 +52,70 @@ fn span_key(inode: Inode, owner: NodeId, start: u64, end: u64) -> [u8; 32] { .expect("fixed span key size") } +/// Bind only the canonical legacy FileAdd identity needed by a version-3 span +/// edit. This is a small derived index, never content or a numeric-name alias. +/// The caller must abort the transaction if subsequent validation refuses. +pub fn prepare_legacy_files( + txn: &mut WriteTxn<'_>, + store: &S, + change: &Change, +) -> Result<()> { + let Some(patch) = &change.hashed.span else { + return Ok(()); + }; + if patch.version != crate::change::span_patch::LEGACY_SOURCE_VERSION { + return Ok(()); + } + ensure!( + txn.legacy_span_enabled()?, + "legacy span context capability missing" + ); + let ids = change + .dependencies() + .iter() + .map(|hash| { + txn.get_internal(hash)? + .context("legacy span dependency missing") + }) + .collect::>>()?; + let deps = EffectiveProjectionClosure::try_from_membership( + txn, + &ViewMembershipSet::from_ordered(ids), + )?; + for op in &patch.ops { + if let SpanOp::Edit { file, .. } = op { + if txn.span_file_inode(file)?.is_none() { + let source = store.get_verified_change(&file.owner)?; + source.check_hash(&file.owner)?; + let meta = legacy::metadata(file.owner, source.change()); + let pos = meta + .file_position(file.ordinal) + .context("legacy file ordinal has no FileAdd")?; + ensure!( + source.change().hashed.span.is_none(), + "typed span file index is missing; legacy identity cannot replace it" + ); + let owner = txn + .get_internal(&file.owner)? + .context("legacy file owner missing")?; + ensure!( + deps.contains(owner), + "legacy file owner outside dependency closure" + ); + let inode = super::TreeTxnT::position_inode( + txn, + crate::Position::new(owner, crate::ChangePosition::new(pos)), + )? + .context("legacy canonical inode missing")?; + txn.txn + .open_table(FILES)? + .insert(&file_key(file), inode.get())?; + } + } + } + Ok(()) +} + /// 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. @@ -61,7 +127,12 @@ pub fn index_change( change: &Change, root_inodes: &BTreeMap, ) -> Result<()> { - ensure!(change.hash()? == hash, "change/hash mismatch"); + let verified = store.get_verified_change(&hash)?; + verified.check_hash(&hash)?; + ensure!( + verified.change().hashed == change.hashed && verified.change().contents == change.contents, + "change differs from its verified original encoding" + ); let patch = change .hashed .span @@ -88,6 +159,7 @@ pub fn index_change( txn, &ViewMembershipSet::from_ordered(dep_ids), )?; + prepare_legacy_files(txn, store, change)?; validate_projected_semantics(txn, store, change, &dependencies)?; let already = txn.txn.open_table(APPLIED)?.get(id.get())?.is_some(); if already { @@ -121,7 +193,13 @@ pub fn index_change( "file identity already exists" ); } - SpanOp::Edit { file, target, .. } => { + SpanOp::Edit { + file, + target, + start: edit_start, + end: edit_end, + .. + } => { let file_owner = txn .get_internal(&file.owner)? .context("file owner missing")?; @@ -135,18 +213,44 @@ pub fn index_change( let inode = txn .span_file_inode(file)? .context("file identity missing")?; + let verified = store.get_verified_change(&target.owner)?; + verified.check_hash(&target.owner)?; + let source = verified.change(); + source + .contents + .get(usize::try_from(target.start)?..usize::try_from(target.end)?) + .context("authored target is outside original content")?; + if txn.span_root(inode)?.is_none() { + ensure!( + patch.version == crate::change::span_patch::LEGACY_SOURCE_VERSION, + "legacy range requires span payload version 3" + ); + use crate::crdt::TrunkOp; + ensure!(change.file_ops().iter().any(|ops| matches!(ops.trunk_op(),Some(TrunkOp::ProjectedSpanEdit {file: bound,..}) if bound==file)),"legacy span edit requires a checked semantic baseline"); + let context = SpanRenderContext::new(txn, store, &dependencies)?; + let state = context + .legacy_record_state(inode)? + .context("legacy dependency baseline missing")?; + ensure!( + state.file == *file + && legacy::covers(&state, target, *edit_start, *edit_end), + "legacy span edit addresses bytes outside its dependency baseline" + ); + let position = txn + .legacy_span_position(inode)? + .context("legacy inode position missing")?; + let root_hash = txn + .get_external(position.change)? + .context("legacy creator hash missing")?; + let meta = legacy::metadata(target.owner, source); + if meta.contains(root_hash, position.pos.get(), target.start, target.end) { + continue; + } + } ensure!( - txn.txn - .open_table(SPANS)? - .get(&span_key(inode, target_owner, target.start, target.end))? - .is_some(), + txn.span_exists(&span_key(inode, target_owner, target.start, target.end))?, "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 @@ -318,9 +422,13 @@ pub fn validate_projected_semantics state, + None if patch.version == crate::change::span_patch::LEGACY_SOURCE_VERSION => context + .legacy_record_state(inode)? + .context("legacy projected baseline missing")?, + None => bail!("projected semantic file is outside the dependency closure"), + }; ensure!( state.file == *file, "projected semantic file identity differs from span root" @@ -335,6 +443,24 @@ pub fn validate_projected_semantics Result { + Ok(false) + } + fn legacy_span_position(&self, _inode: Inode) -> Result>> { + Ok(None) + } + fn legacy_span_enabled(&self) -> Result { + Ok(false) + } + /// A checked, unambiguous legacy byte graph. Native implementations use + /// canonical inode positions; no CRDT numeric identities are interpreted. + fn legacy_span_graph( + &self, + _inode: Inode, + _visible: &EffectiveProjectionClosure, + ) -> Result> { + Ok(None) + } /// 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. @@ -352,6 +478,39 @@ pub trait SpanPatchReadTxn: GraphTxnT { macro_rules! impl_span_read { ($txn:ty) => { impl SpanPatchReadTxn for $txn { + fn projected_span_enabled(&self) -> Result { + use super::CapabilityTxnT; + match self + .required_capability_version(super::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id())? + { + None => Ok(false), + Some(1) => Ok(true), + Some(_) => bail!("unsupported projected span semantic capability"), + } + } + fn legacy_span_position( + &self, + inode: Inode, + ) -> Result>> { + Ok(super::TreeTxnT::inode_position(self, inode)?) + } + fn legacy_span_enabled(&self) -> Result { + use super::CapabilityTxnT; + match self + .required_capability_version(super::SPAN_LEGACY_CONTEXT_CAPABILITY.id())? + { + None => Ok(false), + Some(1) => Ok(true), + Some(_) => bail!("unsupported legacy span context capability"), + } + } + fn legacy_span_graph( + &self, + inode: Inode, + visible: &EffectiveProjectionClosure, + ) -> Result> { + legacy::retrieve(self, inode, visible) + } fn span_change_indexed(&self, change: NodeId) -> Result { let table = match self.txn.open_table(APPLIED) { Ok(table) => table, @@ -428,6 +587,22 @@ impl_span_read!(ReadTxn); impl_span_read!(WriteTxn<'_>); impl SpanPatchReadTxn for CachedGraphTxn<'_> { + fn projected_span_enabled(&self) -> Result { + self.txn.projected_span_enabled() + } + fn legacy_span_position(&self, inode: Inode) -> Result>> { + self.txn.legacy_span_position(inode) + } + fn legacy_span_enabled(&self) -> Result { + self.txn.legacy_span_enabled() + } + fn legacy_span_graph( + &self, + inode: Inode, + visible: &EffectiveProjectionClosure, + ) -> Result> { + self.txn.legacy_span_graph(inode, visible) + } fn span_change_indexed(&self, change: NodeId) -> Result { self.txn.span_change_indexed(change) } @@ -458,6 +633,7 @@ struct RenderChange { contents: Vec, dependencies: Vec, span: Option, + legacy: legacy::LegacyMetadata, } type ExpectedEdits = HashMap>; @@ -638,6 +814,7 @@ pub struct SpanRenderContext<'a, T: SpanPatchReadTxn> { expected: ExpectedEdits, proof: Option, metrics: SpanContextBuildMetrics, + projected_files: BTreeSet<[u8; 36]>, } /// A bounded render never retains more than the requested number of bytes. @@ -660,6 +837,10 @@ pub struct VisibleSpan { /// A verified recording baseline and its visible authored ranges. pub struct SpanRecordState { + /// The baseline is the original legacy byte graph, not fabricated CRDT rows. + pub legacy_graph: bool, + /// A selected authored change already uses range-derived semantics. + pub projected_semantics: bool, /// Portable file identity. pub file: ExternalFile, /// Original authored span, including for an empty file. @@ -1048,6 +1229,7 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { ..Default::default() }; let mut changes = HashMap::new(); + let mut projected_files = BTreeSet::new(); let mut expected = ExpectedEdits::new(); let ids: Vec<_> = visible.iter_dependency_first().copied().collect(); metrics.changes = ids.len(); @@ -1088,6 +1270,7 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { contents: change.contents.clone(), dependencies: change.hashed.dependencies.clone(), span: change.hashed.span.clone(), + legacy: legacy::metadata(hash, change), }), )) }) @@ -1095,6 +1278,11 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { metrics.verify_hashes += phase.elapsed(); for (id, loaded) in batch.iter().zip(loaded) { let (hash, change) = loaded?; + ensure!( + change.legacy.projected_files.is_empty() || txn.projected_span_enabled()?, + "projected span semantic capability missing" + ); + projected_files.extend(change.legacy.projected_files.iter().map(file_key)); let phase = Instant::now(); let mut indexed = txn.get_change_deps(*id)?; metrics.dependency_reads += 1; @@ -1126,6 +1314,11 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { changes.insert(hash, Arc::clone(&change)); let phase = Instant::now(); if let Some(patch) = &change.span { + ensure!( + patch.version != crate::change::span_patch::LEGACY_SOURCE_VERSION + || txn.legacy_span_enabled()?, + "legacy span context capability missing" + ); for op in &patch.ops { if let SpanOp::Edit { file, @@ -1179,6 +1372,7 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { expected, proof, metrics, + projected_files, }) } @@ -1187,9 +1381,28 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { &self.metrics } + /// Detect an inconsistent old numeric trunk against its actual graph inode. + /// Equality alone is not a provider proof and never supplies a substitution. + pub fn has_unportable_legacy_trunk(&self, inode: Inode) -> Result { + let Some(pos) = self.txn.legacy_span_position(inode)? else { + return Ok(false); + }; + let owner = self + .txn + .get_external(pos.change)? + .context("legacy inode owner missing")?; + Ok(self + .changes + .values() + .any(|c| c.legacy.mismatched_trunk(owner, pos.pos.get(), pos.change))) + } + /// 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 { + if self.txn.legacy_span_enabled()? && self.expected.contains_key(&inode.get()) { + return Ok(self.legacy_record_state(inode)?.map(|state| state.content)); + } return Ok(None); }; if !self.visible.contains(NodeId::new(root[0])) { @@ -1198,9 +1411,21 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { self.render(inode).map(Some) } + /// Whether this closure contains a typed root or range edits over a legacy + /// graph. Unrelated legacy files keep their existing graph interpretation. + pub fn has_visible_content(&self, inode: Inode) -> Result { + if let Some(root) = self.txn.span_root(inode)? { + return Ok(self.visible.contains(NodeId::new(root[0]))); + } + Ok(self.txn.legacy_span_enabled()? && self.expected.contains_key(&inode.get())) + } + /// 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 { + if self.txn.legacy_span_enabled()? && self.expected.contains_key(&inode.get()) { + return self.legacy_record_state(inode); + } return Ok(None); }; if !self.visible.contains(NodeId::new(root[0])) { @@ -1256,7 +1481,10 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { ); } } + let projected_semantics = self.projected_files.contains(&file_key(&file)); Ok(Some(SpanRecordState { + legacy_graph: false, + projected_semantics, file, root: ExternalSpan { owner, @@ -1269,8 +1497,24 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { })) } + /// Read an old graph as authored ranges without trusting sender-local CRDT + /// names or copying its content into another persisted representation. + pub fn legacy_record_state(&self, inode: Inode) -> Result> { + let Some(base) = legacy::record_state(self, inode)? else { + return Ok(None); + }; + self.render_legacy_state(inode, base, usize::MAX) + .map(|(state, _)| Some(state)) + } + /// Render the visible span tree for an inode. pub fn render(&self, inode: Inode) -> Result> { + if self.txn.span_root(inode)?.is_none() && self.txn.legacy_span_enabled()? { + return Ok(self + .legacy_record_state(inode)? + .context("legacy span baseline missing")? + .content); + } let (content, oversized) = self.render_internal(inode, None, usize::MAX)?; ensure!(!oversized, "span render exceeds addressable memory"); Ok(content) @@ -1279,6 +1523,18 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { /// 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 { + if self.txn.span_root(inode)?.is_none() && self.txn.legacy_span_enabled()? { + let base = + legacy::layout(self, inode, false)?.context("legacy bounded baseline missing")?; + let (state, oversized) = self.render_legacy_state(inode, base, max_bytes)?; + return Ok(if oversized { + BoundedSpanContent::Oversized { + retained_bytes: state.content.len(), + } + } else { + BoundedSpanContent::Complete(state.content) + }); + } let (content, oversized) = self.render_internal(inode, None, max_bytes)?; if oversized { Ok(BoundedSpanContent::Oversized { @@ -1349,6 +1605,7 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { limit: max_bytes, oversized: false, segments, + applied: BTreeSet::new(), }; self.render_spans(inode, root, &mut out)?; Ok((out.bytes, out.oversized)) @@ -1380,13 +1637,34 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { 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 indexed = self.txn.span_exists(&key)?; let hash = self.txn.get_external(owner)?.context("span hash missing")?; let change = Arc::clone( self.changes .get(&hash) .context("span change outside closure")?, ); + if !indexed { + ensure!(self.txn.legacy_span_enabled()?, "span index row missing"); + ensure!( + self.txn.span_root(inode)?.is_none(), + "typed span index row missing" + ); + let position = self + .txn + .legacy_span_position(inode)? + .context("legacy span inode missing")?; + let file_owner = self + .txn + .get_external(position.change)? + .context("legacy file owner missing")?; + ensure!( + change + .legacy + .contains(file_owner, position.pos.get(), start, end), + "legacy span has no authored graph declaration" + ); + } let (start, end) = (usize::try_from(start)?, usize::try_from(end)?); change .contents @@ -1438,21 +1716,69 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { cursor: 0, previous: None, covered: None, + limit: end - start, }) } fn render_spans(&self, inode: Inode, root: [u64; 3], out: &mut RenderOutput<'_>) -> Result<()> { + self.render_span_slice(inode, root, None, out) + } + + fn render_span_slice( + &self, + inode: Inode, + root: [u64; 3], + bounds: Option<(usize, usize)>, + out: &mut RenderOutput<'_>, + ) -> Result<()> { let mut active = BTreeSet::new(); active.insert(root); - let mut stack = vec![self.frame(inode, root)?]; + let mut frame = self.frame(inode, root)?; + if let Some((start, end)) = bounds { + ensure!( + start <= end && end <= frame.end - frame.start, + "invalid visible legacy range" + ); + let mut edits = Vec::new(); + for row in frame.edits { + let (lo, hi) = (usize::try_from(row[0])?, usize::try_from(row[1])?); + let relevant = if lo == hi { + start <= lo && lo <= end + } else { + lo < end && start < hi + }; + if relevant { + ensure!( + start <= lo && hi <= end, + "span edit overlaps an unavailable legacy range" + ); + edits.push(row); + } + } + frame.edits = edits; + frame.cursor = start; + frame.limit = end; + } + let mut stack = vec![frame]; while let Some(frame) = stack.last_mut() { if frame.next == frame.edits.len() { - append_visible(frame, frame.cursor, frame.end - frame.start, out); + append_visible(frame, frame.cursor, frame.limit, out); active.remove(&frame.span); stack.pop(); continue; } let [lo, hi, author, insert_start, insert_end] = frame.edits[frame.next]; + out.applied.insert(( + span_key( + inode, + NodeId::new(frame.span[0]), + frame.span[1], + frame.span[2], + ), + words([lo, hi, author, insert_start, insert_end]) + .try_into() + .unwrap(), + )); frame.next += 1; let (lo, hi) = (usize::try_from(lo)?, usize::try_from(hi)?); ensure!( @@ -1505,6 +1831,85 @@ impl<'a, T: SpanPatchReadTxn> SpanRenderContext<'a, T> { } Ok(()) } + + fn render_legacy_state( + &self, + inode: Inode, + mut base: SpanRecordState, + max_bytes: usize, + ) -> Result<(SpanRecordState, bool)> { + let expected = self.expected.get(&inode.get()).cloned().unwrap_or_default(); + let observed = self + .txn + .span_edits_for_inode(inode)? + .into_iter() + .filter(|(_, row)| { + self.visible + .contains(NodeId::new(decode_words::<5>(row)[2])) + }) + .collect::>(); + ensure!( + expected == observed, + "visible legacy span index differs from hashed changes" + ); + if expected.is_empty() + && base.content.len() == base.spans.last().map(|s| s.output.end).unwrap_or(0) + && base.content.len() <= max_bytes + { + return Ok((base, false)); + } + let mut spans = Vec::new(); + let mut out = RenderOutput { + bytes: Vec::new(), + limit: max_bytes, + oversized: false, + segments: Some(&mut spans), + applied: BTreeSet::new(), + }; + let root_owner = self + .txn + .get_internal(&base.root.owner)? + .context("legacy root owner missing")?; + self.render_spans( + inode, + [root_owner.get(), base.root.start, base.root.end], + &mut out, + )?; + for span in &base.spans { + let owner = self + .txn + .get_internal(&span.target.owner)? + .context("legacy range owner missing")?; + let start = usize::try_from(span.target_start)?; + let end = start + span.output.len(); + self.render_span_slice( + inode, + [owner.get(), span.target.start, span.target.end], + Some((start, end)), + &mut out, + )?; + } + ensure!( + out.applied == expected, + "span edit target is no longer reachable in the legacy graph" + ); + let oversized = out.oversized; + let content = std::mem::take(&mut out.bytes); + drop(out); + base.content = content; + base.spans = spans; + for (_, row) in expected { + let author = NodeId::new(decode_words::<5>(&row)[2]); + base.dependencies.push( + self.txn + .get_external(author)? + .context("legacy span edit owner missing")?, + ); + } + base.dependencies.sort(); + base.dependencies.dedup(); + Ok((base, oversized)) + } } struct RenderFrame { @@ -1518,6 +1923,7 @@ struct RenderFrame { cursor: usize, previous: Option<(usize, usize, u64)>, covered: Option<(usize, usize, u64)>, + limit: usize, } struct RenderOutput<'a> { @@ -1525,6 +1931,7 @@ struct RenderOutput<'a> { limit: usize, oversized: bool, segments: Option<&'a mut Vec>, + applied: BTreeSet<([u8; 32], [u8; 40])>, } fn append_visible(frame: &RenderFrame, start: usize, end: usize, out: &mut RenderOutput<'_>) { @@ -1570,6 +1977,8 @@ mod tests { end: 20, }; SpanRecordState { + legacy_graph: false, + projected_semantics: false, file: ExternalFile { owner: first, ordinal: 0, @@ -1702,6 +2111,8 @@ mod tests { end: content.len() as u64, }; let state = SpanRecordState { + legacy_graph: false, + projected_semantics: false, file: ExternalFile { owner, ordinal: 7 }, root: root.clone(), spans: vec![VisibleSpan { @@ -1787,6 +2198,8 @@ mod tests { end: content.len() as u64, }; let state = SpanRecordState { + legacy_graph: false, + projected_semantics: false, file: ExternalFile { owner, ordinal: 0 }, root: root.clone(), spans: (0..content.len()) @@ -1830,6 +2243,8 @@ mod tests { let first = Hash::of(b"first newline author"); let second = Hash::of(b"second newline author"); let mut state = SpanRecordState { + legacy_graph: false, + projected_semantics: false, file: ExternalFile { owner: first, ordinal: 0, @@ -1966,15 +2381,16 @@ mod tests { #[test] fn independent_span_edits_render_in_both_insert_orders() -> Result<()> { + type ProjectionIds = ( + SpanProjectionId, + Vec<(SpanProjectionId, Vec)>, + ); fn projection_ids( repo: &Pristine, store: &MemoryChangeStore, view_name: &str, inode: Inode, - ) -> Result<( - SpanProjectionId, - Vec<(SpanProjectionId, Vec)>, - )> { + ) -> Result { let txn = repo.read_txn()?; let view = txn.get_view(view_name)?.context("view missing")?; let visible = EffectiveProjectionClosure::try_from_membership( diff --git a/atomic-core/src/pristine/span_patch_storage/legacy.rs b/atomic-core/src/pristine/span_patch_storage/legacy.rs new file mode 100644 index 00000000..e5493f32 --- /dev/null +++ b/atomic-core/src/pristine/span_patch_storage/legacy.rs @@ -0,0 +1,291 @@ +use super::*; +use crate::change::{Atom, AtomRef, GraphOp}; +use crate::types::{GraphNode, Position}; + +/// A canonical inode's unambiguous legacy byte graph under one checked closure. +pub struct LegacySpanGraph { + pub(super) position: Position, + pub(super) nodes: Vec>, + pub(super) contributors: BTreeSet, +} + +#[derive(Default)] +pub(super) struct LegacyMetadata { + files: Vec, + ranges: BTreeSet<(Hash, u64, u64, u64)>, + pub(super) projected_files: Vec, + foreign_trunks: Vec<(Hash, u64, u64)>, +} +impl LegacyMetadata { + pub(super) fn mismatched_trunk(&self, owner: Hash, pos: u64, actual: NodeId) -> bool { + self.foreign_trunks + .iter() + .any(|(file, position, raw)| *file == owner && *position == pos && *raw != actual.get()) + } + pub(super) fn file_position(&self, ordinal: u32) -> Option { + self.files.get(ordinal as usize).copied() + } + pub(super) fn contains(&self, owner: Hash, pos: u64, start: u64, end: u64) -> bool { + self.ranges.contains(&(owner, pos, start, end)) + } +} + +pub(super) fn covers(state: &SpanRecordState, target: &ExternalSpan, start: u64, end: u64) -> bool { + if target == &state.root && target.start == target.end { + return state.content.is_empty() && start == 0 && end == 0; + } + let mut ranges = state + .spans + .iter() + .filter(|span| &span.target == target) + .map(|span| { + ( + span.target_start, + span.target_start + span.output.len() as u64, + ) + }) + .collect::>(); + ranges.sort(); + let mut at = start; + for (lo, hi) in ranges { + if start == end && lo <= start && start <= hi { + return true; + } + if hi <= at { + continue; + } + if lo > at { + return false; + } + at = hi; + if at >= end { + return true; + } + } + false +} + +pub(super) fn metadata(hash: Hash, change: &Change) -> LegacyMetadata { + let mut result = LegacyMetadata::default(); + for ops in change.file_ops() { + if let Some(crate::crdt::TrunkOp::ProjectedSpanEdit { file, .. }) = ops.trunk_op() { + result.projected_files.push(file.clone()); + } + if change.hashed.span.is_none() && !ops.trunk_id().change_id().is_root() { + for op in change + .hunks() + .iter() + .filter(|op| op.path() == Some(ops.path())) + { + for atom in op.atoms() { + let inode = match atom { + AtomRef::Insertion(i) | AtomRef::Atom(Atom::Insertion(i)) => i.inode, + AtomRef::EdgeUpdate(e) | AtomRef::Atom(Atom::EdgeUpdate(e)) => e.inode, + }; + result.foreign_trunks.push(( + inode.change.unwrap_or(hash), + inode.pos.get(), + ops.trunk_id().change_id().get(), + )); + } + } + } + } + for op in change.hunks() { + if let GraphOp::FileAdd { add_inode, .. } = op { + let pos = add_inode.start.get(); + result.files.push(pos); + result.ranges.insert((hash, pos, pos, pos)); + } + for atom in op.atoms() { + if let AtomRef::Insertion(insert) | AtomRef::Atom(Atom::Insertion(insert)) = atom { + if !insert.flag.contains(crate::EdgeFlags::FOLDER) && insert.start < insert.end { + result.ranges.insert(( + insert.inode.change.unwrap_or(hash), + insert.inode.pos.get(), + insert.start.get(), + insert.end.get(), + )); + } + } + } + } + result +} + +pub(super) fn retrieve( + txn: &T, + inode: Inode, + visible: &EffectiveProjectionClosure, +) -> Result> { + use crate::output::alive::{compute_order, retrieve_graph, RetrieveOptions}; + let Some(position) = txn.inode_position(inode)? else { + return Ok(None); + }; + if !visible.contains(position.change) { + return Ok(None); + } + let mut retrieved = retrieve_graph( + txn, + position, + RetrieveOptions::new().with_graph_visibility(visible.clone()), + )?; + ensure!(!retrieved.truncated, "legacy span graph is truncated"); + let order = compute_order(&mut retrieved.graph); + ensure!( + !order.has_conflicts(), + "legacy span baseline contains unresolved ordering conflicts" + ); + let mut nodes = Vec::new(); + for scc in order.sccs.iter().rev() { + for id in scc { + let vertex = retrieved + .graph + .try_get_vertex(*id) + .context("legacy graph vertex missing")?; + ensure!( + !vertex.is_zombie(), + "legacy span baseline contains a zombie conflict" + ); + if !vertex.node.change.is_root() && vertex.node.start < vertex.node.end { + nodes.push(vertex.node); + } + } + } + let mut contributors = BTreeSet::from([position.change]); + for row in txn.iter_inode_vertices(inode)? { + let (node, edge) = row?; + for id in [node.change, edge.introduced_by()] { + if !id.is_root() && visible.contains(id) { + contributors.insert(id); + } + } + } + Ok(Some(LegacySpanGraph { + position, + nodes, + contributors, + })) +} + +pub(super) fn record_state( + context: &SpanRenderContext<'_, T>, + inode: Inode, +) -> Result> { + layout(context, inode, true) +} + +pub(super) fn layout( + context: &SpanRenderContext<'_, T>, + inode: Inode, + retain_content: bool, +) -> Result> { + let Some(graph) = context.txn.legacy_span_graph(inode, context.visible)? else { + return Ok(None); + }; + let owner = context + .txn + .get_external(graph.position.change)? + .context("legacy file owner hash missing")?; + let creator = context + .changes + .get(&owner) + .context("legacy file creator outside closure")?; + let ordinal = creator + .legacy + .files + .iter() + .position(|pos| *pos == graph.position.pos.get()) + .context("legacy inode has no canonical FileAdd")?; + let ordinal = u32::try_from(ordinal)?; + ensure!( + creator.span.is_none(), + "typed span root index is missing; cannot fall back to legacy graph" + ); + let mut content = Vec::new(); + let mut spans: Vec = Vec::new(); + let mut output_len = 0usize; + for node in graph.nodes { + let hash = context + .txn + .get_external(node.change)? + .context("legacy content owner hash missing")?; + let change = context + .changes + .get(&hash) + .context("legacy content outside closure")?; + let candidates: Vec<_> = change + .legacy + .ranges + .iter() + .filter(|(file_owner, pos, start, end)| { + *file_owner == owner + && *pos == graph.position.pos.get() + && *start <= node.start.get() + && node.end.get() <= *end + }) + .collect(); + ensure!( + candidates.len() == 1, + "legacy vertex has no unique immutable authored range" + ); + let (_, _, start, end) = *candidates[0]; + ensure!( + end <= change.contents.len() as u64, + "legacy declaration is outside verified content" + ); + let bytes = change + .contents + .get(usize::try_from(node.start.get())?..usize::try_from(node.end.get())?) + .context("legacy authored bytes missing")?; + let lo = output_len; + output_len = output_len + .checked_add(bytes.len()) + .context("legacy output overflow")?; + if retain_content { + content.extend_from_slice(bytes); + } + let target = ExternalSpan { + owner: hash, + start, + end, + }; + let offset = node.start.get() - start; + if let Some(previous) = spans.last_mut() { + if previous.target == target + && previous.target_start + previous.output.len() as u64 == offset + { + previous.output.end = output_len; + continue; + } + } + spans.push(VisibleSpan { + output: lo..output_len, + target, + target_start: offset, + }); + } + let dependencies = graph + .contributors + .into_iter() + .map(|id| { + context + .txn + .get_external(id)? + .context("legacy contributor hash missing") + }) + .collect::>>()?; + Ok(Some(SpanRecordState { + legacy_graph: true, + projected_semantics: true, + file: ExternalFile { owner, ordinal }, + root: ExternalSpan { + owner, + start: graph.position.pos.get(), + end: graph.position.pos.get(), + }, + content, + spans, + dependencies: dependencies.into_iter().collect(), + })) +} diff --git a/atomic-core/src/pristine/traits/crdt_read.rs b/atomic-core/src/pristine/traits/crdt_read.rs index 8d3e28a6..74d71382 100644 --- a/atomic-core/src/pristine/traits/crdt_read.rs +++ b/atomic-core/src/pristine/traits/crdt_read.rs @@ -18,6 +18,16 @@ use crate::pristine::error::PristineError; /// hold the txn behind a shared borrow while iterating CRDT state, which is /// the natural fit for output walkers and record-side lookups. pub trait CrdtTxnT { + /// Authors of indexed span edits for an ambient trunk. Raw output checks + /// their immutable semantic declarations before trusting retained cache rows. + /// Backends without span storage return an empty list. + fn crdt_span_edit_authors( + &self, + _trunk: crate::crdt::TrunkId, + ) -> Result, PristineError> { + Ok(Vec::new()) + } + /// Get a trunk (file) entry from the CRDT tables. fn get_crdt_trunk( &self, diff --git a/atomic-core/src/pristine/txn/read.rs b/atomic-core/src/pristine/txn/read.rs index e375ffb6..f6602d0e 100644 --- a/atomic-core/src/pristine/txn/read.rs +++ b/atomic-core/src/pristine/txn/read.rs @@ -1743,6 +1743,32 @@ macro_rules! crdt_multimap_opt { } impl crate::pristine::traits::CrdtTxnT for ReadTxn { + fn crdt_span_edit_authors(&self, trunk: crate::crdt::TrunkId) -> PristineResult> { + use crate::pristine::span_patch_storage::SpanPatchReadTxn; + let table = match self.txn.open_table(crate::crdt::tables::INODE_TRUNK) { + Ok(table) => table, + Err(redb::TableError::TableDoesNotExist(_)) => return Ok(Vec::new()), + Err(error) => return Err(error.into()), + }; + let mut authors = std::collections::BTreeSet::new(); + for row in table.iter()? { + let (inode, stored) = row?; + if *stored.value() == trunk.to_bytes() { + let inode = crate::Inode::new(inode.value()); + for (_, edit) in + self.span_edits_for_inode(inode) + .map_err(|e| PristineError::Inconsistent { + message: e.to_string(), + })? + { + authors.insert(NodeId::new(u64::from_be_bytes( + edit[16..24].try_into().unwrap(), + ))); + } + } + } + Ok(authors.into_iter().collect()) + } fn get_crdt_trunk( &self, key: &[u8; 12], @@ -2227,6 +2253,9 @@ impl<'txn> TreeTxnT for CachedGraphTxn<'txn> { // ───────────────────────────────────────────────────────────────────────── impl<'txn> crate::pristine::CrdtTxnT for CachedGraphTxn<'txn> { + fn crdt_span_edit_authors(&self, trunk: crate::crdt::TrunkId) -> PristineResult> { + self.txn.crdt_span_edit_authors(trunk) + } fn get_crdt_trunk( &self, key: &[u8; 12], diff --git a/atomic-core/src/pristine/txn/write/mod.rs b/atomic-core/src/pristine/txn/write/mod.rs index 2ec4db4b..1f57825f 100644 --- a/atomic-core/src/pristine/txn/write/mod.rs +++ b/atomic-core/src/pristine/txn/write/mod.rs @@ -2284,6 +2284,28 @@ impl<'a> MutTxnT for WriteTxn<'a> { // txns and avoids needing a write_txn just to inspect CRDT state. impl crate::pristine::traits::CrdtTxnT for WriteTxn<'_> { + fn crdt_span_edit_authors(&self, trunk: crate::crdt::TrunkId) -> PristineResult> { + use crate::pristine::span_patch_storage::SpanPatchReadTxn; + let table = self.txn.open_table(INODE_TRUNK)?; + let mut authors = std::collections::BTreeSet::new(); + for row in table.iter()? { + let (inode, stored) = row?; + if *stored.value() == trunk.to_bytes() { + let inode = crate::Inode::new(inode.value()); + for (_, edit) in + self.span_edits_for_inode(inode) + .map_err(|e| PristineError::Inconsistent { + message: e.to_string(), + })? + { + authors.insert(NodeId::new(u64::from_be_bytes( + edit[16..24].try_into().unwrap(), + ))); + } + } + } + Ok(authors.into_iter().collect()) + } fn get_crdt_trunk(&self, key: &[u8; 12]) -> PristineResult> { let table = self.txn.open_table(TRUNKS)?; let result = match table.get(key)? { diff --git a/atomic-core/src/pristine/view_graph.rs b/atomic-core/src/pristine/view_graph.rs index 6371d39f..50edec9e 100644 --- a/atomic-core/src/pristine/view_graph.rs +++ b/atomic-core/src/pristine/view_graph.rs @@ -415,6 +415,12 @@ impl<'a, T: TreeTxnT> TreeTxnT for ViewGraph<'a, T> { // ───────────────────────────────────────────────────────────────────────── impl<'a, T: crate::pristine::CrdtTxnT> crate::pristine::CrdtTxnT for ViewGraph<'a, T> { + fn crdt_span_edit_authors( + &self, + trunk: crate::crdt::TrunkId, + ) -> Result, PristineError> { + self.inner.crdt_span_edit_authors(trunk) + } fn get_crdt_trunk( &self, key: &[u8; 12], diff --git a/atomic-repository/Cargo.toml b/atomic-repository/Cargo.toml index a8a0384b..ab59c708 100644 --- a/atomic-repository/Cargo.toml +++ b/atomic-repository/Cargo.toml @@ -20,6 +20,11 @@ query-test-instrumentation = [] # Full review policy evaluation needs the existing PurRDF engine (Rust 1.98). # Consumers that keep the repository's lower MSRV do not accept this exception. review-shacl = ["dep:atomic-shacl"] +# Native, deliberately bounded inode/name-transport prototype. No wire-format +# change, no new persisted operation, and no effect in default builds. +prototype-inode-transport = [] +# Replayable sandbox-only context operations. Not a production wire contract. +prototype-context-completion = ["prototype-inode-transport", "atomic-core/prototype-context-completion"] [dependencies] unicode-normalization = "0.1" diff --git a/atomic-repository/examples/reconstruct_semantic_context.rs b/atomic-repository/examples/reconstruct_semantic_context.rs new file mode 100644 index 00000000..1e59e5bf --- /dev/null +++ b/atomic-repository/examples/reconstruct_semantic_context.rs @@ -0,0 +1,11 @@ +//! Read-only context witness; shared with the feature-gated native validator. +use atomic_repository::Repository; +use atomic_repository::{changestore, repository}; +#[cfg(test)] +use atomic_repository::{record, TrackingOptions, UnrecordOptions}; +#[path = "../src/context_witness_prototype.rs"] +mod witness; + +fn main() -> Result<(), Box> { + witness::run_example() +} diff --git a/atomic-repository/examples/replay_context_sandbox.rs b/atomic-repository/examples/replay_context_sandbox.rs new file mode 100644 index 00000000..07ec44ab --- /dev/null +++ b/atomic-repository/examples/replay_context_sandbox.rs @@ -0,0 +1,370 @@ +//! Disposable replay only. Never opens the input repository for writing. +#[cfg(feature = "prototype-context-completion")] +fn main() -> Result<(), Box> { + use atomic_core::change::{ChangeHeader, VerifiedChange}; + use atomic_core::types::{Base32, Hash}; + use atomic_repository::{record::RecordOptions, Repository}; + use std::path::Path; + const VIEW: &str = "experimental-context"; + const SKILL: &str = "skills/triage-review/SKILL.md"; + let args: Vec<_> = std::env::args().collect(); + if args.len() != 3 { + return Err("usage: replay_context_sandbox ".into()); + } + let input = std::fs::canonicalize(&args[1])?; + let output = Path::new(&args[2]); + if output.exists() { + return Err("refuse existing output".into()); + } + let original_edit = std::fs::read(input.join(SKILL))?; + let original_db = std::fs::read(input.join(".atomic/atomic.redb"))?; + let source = Repository::open_readonly(&input)?; + let selected: Vec<_> = source + .get_view_changes(None)? + .into_iter() + .map(|(_, h)| h) + .collect(); + std::fs::create_dir(output)?; + let mut report = serde_json::json!({"experimental":true,"signed":false,"published":false, + "original_count":selected.len(),"source_database":Hash::of(&original_db), + "completion_inserted":false,"record_attempted":false,"second_replay":false}); + let attempt = (|| -> Result<(), Box> { + fn sandbox(path: &Path) -> Result> { + let mut repo = Repository::init(path)?; + repo.create_draft_view(VIEW, "dev")?; + repo.switch_view(repo.require_working_copy_id()?, VIEW)?; + Ok(repo) + } + fn transfer( + from: &Repository, + to: &Repository, + hashes: &[Hash], + ) -> Result<(), Box> { + for hash in hashes { + let bytes = std::fs::read(from.change_store().change_path(hash))?; + if VerifiedChange::from_bytes(&bytes)?.hash() != *hash { + return Err("original hash mismatch".into()); + } + to.import_change_bytes(hash, &bytes)?; + if std::fs::read(to.change_store().change_path(hash))? != bytes { + return Err("changed stored bytes".into()); + } + } + Ok(()) + } + let first = sandbox(&output.join("first"))?; + transfer(&source, &first, &selected)?; + let consumer = Hash::from_base32(b"FWMGY5TBGUV5RJOMN6F4CXR5UT3DX3YNSRAWS2WBZHECZDFU6Y3A") + .ok_or("bad consumer")?; + let completion = first.prototype_create_completion(&selected, consumer, 15)?; + report["completion"] = serde_json::json!(completion); + let mut roots = selected.clone(); + roots.push(completion); + first.prototype_insert_composed(&[completion])?; + report["completion_inserted"] = true.into(); + report["completed_scope_count"] = first.get_view_changes(None)?.len().into(); + report["context"] = first.prototype_context_report()?; + let completed_hashes: Vec<_> = first + .get_view_changes(None)? + .into_iter() + .map(|(_, h)| h) + .collect(); + let completion_replay = sandbox(&output.join("completion-replay"))?; + transfer(&first, &completion_replay, &completed_hashes)?; + completion_replay.prototype_insert_composed(&completed_hashes)?; + if completion_replay.view_set_id(VIEW)? != first.view_set_id(VIEW)? { + return Err("completed scope replay changed its selected patch set".into()); + } + first.materialize_view_to(VIEW, first.root())?; + completion_replay.materialize_view_to(VIEW, completion_replay.root())?; + let mut paths = std::collections::BTreeSet::new(); + for hash in &completed_hashes { + paths.extend( + first + .load_change(hash)? + .hunks() + .iter() + .filter_map(|h| h.path().map(str::to_owned)), + ); + } + for path in paths { + let left = first.root().join(&path); + let right = completion_replay.root().join(&path); + if (left.is_file() || right.is_file()) && std::fs::read(left)? != std::fs::read(right)? + { + return Err("completed scope replay changed materialized bytes".into()); + } + } + report["completion_replayed_without_sidecar"] = true.into(); + let next_consumer = + Hash::from_base32(b"HFAP35CGF5DYCB24NZIQ74IWTC5BG2BH6WJ4UA3HT75GL4IMBIMA") + .ok_or("bad second consumer")?; + let mut completed_symbols = std::collections::BTreeSet::new(); + let mut completions = Vec::new(); + loop { + match first.prototype_insert_composed(&roots) { + Ok(_) => break, + Err(error) => { + // Bounded diagnostic driver, not a production error parser + // or implicit insertion policy. Only this exact consumer's + // reported missing owner may trigger a new stored patch; + // every patch independently requires a complete witness. + let text = error.to_string(); + let marker = format!("while composing {next_consumer}: Invalid change: inode prototype: no typed owner in explicit graph context for symbol "); + let Some((_, symbol)) = text.rsplit_once(&marker) else { + return Err(error.into()); + }; + let symbol: u64 = symbol.parse()?; + if completed_symbols.len() >= 32 || !completed_symbols.insert(symbol) { + return Err(error.into()); + } + let before = first.view_set_id(VIEW)?; + let completion = + first.prototype_create_completion(&selected, next_consumer, symbol)?; + if first.view_set_id(VIEW)? != before { + return Err("completion factory changed membership".into()); + } + roots.push(completion); + completions.push(serde_json::json!({"consumer":next_consumer,"symbol":symbol,"completion":completion})); + report["additional_completions"] = serde_json::json!(completions); + } + } + } + report["completion_inserted"] = true.into(); + report["context"] = first.prototype_context_report()?; + first.materialize_view_to(VIEW, first.root())?; + let original_paths: std::collections::BTreeSet<_> = selected + .iter() + .map(|hash| first.load_change(hash)) + .collect::, _>>()? + .iter() + .flat_map(|change| { + change + .hunks() + .iter() + .filter_map(|h| h.path().map(str::to_owned)) + }) + .collect(); + let mut baseline = std::collections::BTreeMap::new(); + for path in original_paths { + baseline.insert(path.clone(), first.get_file_content_on_view(&path, VIEW)?); + } + std::fs::write(first.root().join(SKILL), &original_edit)?; + report["record_attempted"] = true.into(); + let candidate = first.record( + first.require_working_copy_id()?, + ChangeHeader::new("EXPERIMENTAL SANDBOX: preserved triage-review 1.1.1 edit"), + RecordOptions::new() + .paths(vec![SKILL]) + .save_to_store(true) + .apply_after_record(true), + )?; + report["candidate"] = serde_json::json!(candidate.hash()); + first.materialize_view_to(VIEW, first.root())?; + for (path, bytes) in &baseline { + if path != SKILL && first.get_file_content_on_view(path, VIEW)? != *bytes { + return Err(format!("record altered unrelated file {path}").into()); + } + } + report["unrelated_files_unchanged"] = true.into(); + let replay_hashes: Vec<_> = first + .get_view_changes(None)? + .into_iter() + .map(|(_, h)| h) + .collect(); + let second = sandbox(&output.join("second"))?; + transfer(&first, &second, &replay_hashes)?; + second.prototype_insert_composed(&[*candidate.hash()])?; + second.materialize_view_to(VIEW, second.root())?; + if std::fs::read(second.root().join(SKILL))? != original_edit { + return Err("fresh replay did not preserve the skill edit".into()); + } + transfer(&source, &second, &selected)?; + report["second_replay"] = true.into(); + report["candidate_root_replay"] = true.into(); + second.prototype_insert_composed(&replay_hashes)?; + + // An actual independent graph/content patch, not just an empty marker. + report["stage"] = "independent-producer".into(); + let producer = sandbox(&output.join("independent-producer"))?; + let probe_path = "commutation-probe.txt"; + std::fs::write(producer.root().join(probe_path), b"independent patch\n")?; + producer.add( + producer.require_working_copy_id()?, + probe_path, + atomic_repository::TrackingOptions::default(), + )?; + let probe = producer.record( + producer.require_working_copy_id()?, + ChangeHeader::new("EXPERIMENTAL: independent file patch"), + RecordOptions::new() + .paths(vec![probe_path]) + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + )?; + if !probe.change().dependencies().is_empty() { + return Err("commutation probe must be independent".into()); + } + transfer(&producer, &second, &[*probe.hash()])?; + report["stage"] = "independent-after".into(); + second.prototype_insert_composed(&[*probe.hash()])?; + let reordered = sandbox(&output.join("independent-first"))?; + use atomic_core::pristine::{GraphTxnT, MutTxnT}; + for i in 0..7 { + let padding = reordered.save_change(&atomic_core::change::Change::empty( + ChangeHeader::new(format!("unselected allocation padding {i}")), + ))?; + let mut txn = reordered.pristine().write_txn()?; + txn.register_change(&padding)?; + txn.commit()?; + } + transfer(&producer, &reordered, &[*probe.hash()])?; + report["stage"] = "independent-before".into(); + reordered.prototype_insert_composed(&[*probe.hash()])?; + let mut reverse_transfer = replay_hashes.clone(); + reverse_transfer.reverse(); + transfer(&first, &reordered, &reverse_transfer)?; + reordered.prototype_insert_composed(&reverse_transfer)?; + report["stage"] = "commutation-content-check".into(); + if second.view_set_id(VIEW)? != reordered.view_set_id(VIEW)? { + return Err("independent compositions have different SetIds".into()); + } + second.materialize_view_to(VIEW, second.root())?; + reordered.materialize_view_to(VIEW, reordered.root())?; + for path in baseline + .keys() + .map(String::as_str) + .chain(std::iter::once(probe_path)) + { + // materialize_view_to is additive and may leave stale bytes for a + // historically deleted path. Compare canonical presence/content, + // then verify materialized bytes only for present entries. + let left = second.get_file_content_on_view(path, VIEW)?; + let right = reordered.get_file_content_on_view(path, VIEW)?; + if left != right { + return Err( + format!("independent change failed content commutation for {path}").into(), + ); + } + if let Some(bytes) = left { + if std::fs::read(second.root().join(path))? != bytes + || std::fs::read(reordered.root().join(path))? != bytes + { + return Err(format!( + "materialization disagrees with commuted projection for {path}" + ) + .into()); + } + } + } + let left_id = second + .pristine() + .read_txn()? + .get_internal(candidate.hash())?; + let right_id = reordered + .pristine() + .read_txn()? + .get_internal(candidate.hash())?; + if left_id == right_id { + return Err("allocation control did not shift candidate identity".into()); + } + report["commutation_different_allocations"] = true.into(); + report["commutation_set_id"] = serde_json::json!(second.view_set_id(VIEW)?); + fn semantic_snapshot( + repo: &Repository, + path: &str, + ) -> Result> { + use atomic_core::crdt::tables::{decode_branch_id, decode_leaf_id, encode_trunk_id}; + use atomic_core::pristine::CrdtTxnT; + let txn = repo.pristine().read_txn()?; + let trunk = txn + .get_trunk_by_path(path)? + .ok_or("semantic file is missing")?; + let external = |id| -> Result> { + Ok(txn.get_external(id)?.ok_or("semantic owner hash missing")?) + }; + let mut branches = std::collections::BTreeMap::new(); + for branch_key in txn.iter_trunk_branches(&encode_trunk_id(&trunk))? { + let branch_key = branch_key?; + let id = decode_branch_id(&branch_key); + let row = txn + .get_crdt_branch(&branch_key)? + .ok_or("semantic line missing")?; + if row.trunk_id != trunk { + return Err("line ownership disagrees with semantic file".into()); + } + let after = txn.get_crdt_branch_after(&branch_key)?; + let after = match after { + Some(key) if key != [0; 12] => { + let id = decode_branch_id(&key); + serde_json::json!([external(id.change_id())?, id.branch_idx()]) + } + Some(_) => serde_json::json!("start"), + None => serde_json::Value::Null, + }; + let mut leaves = std::collections::BTreeMap::new(); + for key in txn.iter_branch_leaves(&branch_key)? { + let key = key?; + let leaf = decode_leaf_id(&key); + let row = txn.get_crdt_leaf(&key)?.ok_or("semantic token missing")?; + if row.branch_id != id { + return Err("token ownership disagrees with line".into()); + } + leaves.insert( + (external(leaf.change_id())?, leaf.leaf_idx()), + serde_json::json!({ + "kind":format!("{:?}",row.kind),"state":format!("{:?}",row.state), + "content_range":[row.content_start,row.content_end]}), + ); + } + branches.insert( + (external(id.change_id())?, id.branch_idx()), + serde_json::json!({ + "after":after,"state":format!("{:?}",row.state),"line_hash":row.line_hash, + "leaves":leaves.into_iter().collect::>()}), + ); + } + if branches.is_empty() { + return Err("semantic snapshot unexpectedly empty".into()); + } + Ok( + serde_json::json!({"trunk":[external(trunk.change_id())?,trunk.file_idx()], + "branches":branches.into_iter().collect::>()}), + ) + } + for path in [SKILL, probe_path] { + if semantic_snapshot(&second, path)? != semantic_snapshot(&reordered, path)? { + return Err( + format!("independent change failed semantic commutation for {path}").into(), + ); + } + } + report["commutation_semantic_names_order_tokens"] = true.into(); + Ok(()) + })(); + let preserved = std::fs::read(input.join(SKILL))? == original_edit + && std::fs::read(input.join(".atomic/atomic.redb"))? == original_db; + report["source_preserved"] = preserved.into(); + report["success"] = attempt.is_ok().into(); + if let Err(error) = &attempt { + report["refusal"] = error.to_string().into(); + } + std::fs::write( + output.join("result.json"), + serde_json::to_vec_pretty(&report)?, + )?; + println!("{}", serde_json::to_string_pretty(&report)?); + if !preserved { + return Err("input changed during experiment; no overwrite authorized".into()); + } + attempt +} + +#[cfg(not(feature = "prototype-context-completion"))] +fn main() { + eprintln!( + "requires --features prototype-context-completion; disposable sandbox experiment only" + ); + std::process::exit(2); +} diff --git a/atomic-repository/examples/span_skill_replay.rs b/atomic-repository/examples/span_skill_replay.rs new file mode 100644 index 00000000..5f76ab9f --- /dev/null +++ b/atomic-repository/examples/span_skill_replay.rs @@ -0,0 +1,295 @@ +//! Ordinary product APIs only. Original input is opened read-only; every write +//! is confined to a new disposable output. No prototype feature is required. +use atomic_core::change::{Change, ChangeHeader, VerifiedChange}; +use atomic_core::pristine::span_patch_storage::SpanRenderContext; +use atomic_core::pristine::{CachedGraphTxn, GraphTxnT, MutTxnT, ViewTxnT}; +use atomic_core::types::Hash; +use atomic_repository::{InsertOptions, RecordOptions, Repository, TrackingOptions}; +use std::{collections::BTreeSet, path::Path}; +type Result = std::result::Result>; +const VIEW: &str = "span-skill-regression"; +const SKILL: &str = "skills/triage-review/SKILL.md"; +fn sandbox(path: &Path) -> Result { + let mut r = Repository::init(path)?; + r.create_draft_view(VIEW, "dev")?; + r.switch_view(r.require_working_copy_id()?, VIEW)?; + Ok(r) +} +fn transfer(src: &Repository, dst: &Repository, hashes: &[Hash]) -> Result<()> { + for hash in hashes { + let bytes = std::fs::read(src.change_store().change_path(hash))?; + if VerifiedChange::from_bytes(&bytes)?.hash() != *hash { + return Err("source hash mismatch".into()); + } + dst.import_change_bytes(hash, &bytes)?; + if std::fs::read(dst.change_store().change_path(hash))? != bytes { + return Err("transfer altered original bytes/attachments".into()); + } + } + Ok(()) +} +fn semantic_ids(r: &Repository) -> Result { + let snapshot = span_state(r)?.semantic_snapshot()?; + Ok( + serde_json::json!({"baseline":snapshot.id,"lines":snapshot.lines.iter().map(|line| + serde_json::json!({"id":line.id,"tokens":line.tokens.iter().map(|token|token.id).collect::>()})).collect::>()}), + ) +} +fn span_state( + r: &Repository, +) -> Result { + let (inode, _) = r + .get_inode_and_position(SKILL)? + .ok_or("skill inode missing")?; + let tx = r.pristine().read_txn()?; + let view = tx.get_view(VIEW)?.ok_or("view missing")?; + let visible = atomic_repository::repository::effective_projection_closure(&tx, &view)?; + let cached = CachedGraphTxn::new(&tx)?; + let ctx = SpanRenderContext::new(&cached, r.change_store(), &visible)?; + match ctx.record_state(inode)? { + Some(state) => Ok(state), + None => ctx + .legacy_record_state(inode)? + .ok_or_else(|| "span baseline missing".into()), + } +} +fn graph_rows(r: &Repository) -> Result<(u64, u64)> { + use redb::{ReadableDatabase, ReadableTableMetadata}; + let db = r + .pristine() + .shared_database() + .ok_or("native database required")?; + let txn = db.begin_read()?; + Ok(( + txn.open_multimap_table(atomic_core::pristine::tables::GRAPH)? + .len()?, + txn.open_multimap_table(atomic_core::pristine::tables::INODE_GRAPH)? + .len()?, + )) +} +fn main() -> Result<()> { + let args: Vec<_> = std::env::args().collect(); + if args.len() != 3 { + return Err("usage: span_skill_replay ".into()); + } + let input = std::fs::canonicalize(&args[1])?; + let output = Path::new(&args[2]); + if output.exists() { + return Err("refuse existing output".into()); + } + let edit = std::fs::read(input.join(SKILL))?; + let db = std::fs::read(input.join(".atomic/atomic.redb"))?; + let source = Repository::open_readonly(&input)?; + let originals = source + .get_view_changes(None)? + .into_iter() + .map(|(_, h)| h) + .collect::>(); + std::fs::create_dir(output)?; + let mut report = serde_json::json!({"prototype_features":false,"published":false,"signed":false,"recorded":false,"replayed":false,"commutation":false,"originals":originals.len()}); + let attempt = (|| -> Result<()> { + let sender = sandbox(&output.join("sender"))?; + transfer(&source, &sender, &originals)?; + for hash in &originals { + sender.insert_change_rec(hash, InsertOptions::default().view(VIEW))?; + } + sender.materialize_view_to(VIEW, sender.root())?; + let paths: BTreeSet<_> = originals + .iter() + .map(|hash| sender.load_change(hash)) + .collect::, _>>()? + .iter() + .flat_map(|c| { + c.hunks() + .iter() + .filter_map(|op| op.path().map(str::to_owned)) + }) + .chain(std::iter::once(SKILL.into())) + .collect(); + let before = paths + .iter() + .map(|p| Ok((p.clone(), sender.get_file_content_on_view(p, VIEW)?))) + .collect::>>()?; + let baseline = span_state(&sender)?; + let authored = baseline + .spans + .iter() + .map(|span| (span.target.owner, span.target.start, span.target.end)) + .collect::>(); + let before_rows = graph_rows(&sender)?; + std::fs::write(sender.root().join(SKILL), &edit)?; + report["stage"] = "record".into(); + let recorded = sender.record( + sender.require_working_copy_id()?, + ChangeHeader::new("span: preserved triage-review edit"), + RecordOptions::new() + .paths(vec![SKILL]) + .save_to_store(true) + .apply_after_record(true), + )?; + let patch = recorded + .change() + .hashed + .span + .as_ref() + .ok_or("record did not use span algebra")?; + if patch.version != atomic_core::change::span_patch::LEGACY_SOURCE_VERSION { + return Err("legacy context was not explicitly versioned".into()); + } + if recorded.change().hunks().iter().any(|op| { + matches!( + op, + atomic_core::change::GraphOp::Edit { .. } + | atomic_core::change::GraphOp::Replacement { .. } + ) + }) { + return Err("record fell back to linewise content graph".into()); + } + report["recorded"] = true.into(); + report["candidate"] = serde_json::json!(recorded.hash()); + report["inserted_bytes"] = recorded.change().contents.len().into(); + report["span_ops"] = patch.ops.len().into(); + let after_rows = graph_rows(&sender)?; + if before_rows != after_rows { + return Err("skill edit expanded content graph rows".into()); + } + report["graph_rows_before"] = serde_json::json!(before_rows); + report["graph_rows_after"] = serde_json::json!(after_rows); + let state = span_state(&sender)?; + for span in &state.spans { + if span.target.owner != *recorded.hash() + && !authored.contains(&(span.target.owner, span.target.start, span.target.end)) + { + return Err("unchanged bytes acquired a fabricated original owner".into()); + } + } + report["authored_ownership_preserved"] = true.into(); + for (path, bytes) in &before { + if path != SKILL && sender.get_file_content_on_view(path, VIEW)? != *bytes { + return Err(format!("unrelated file changed: {path}").into()); + } + } + let receiver = sandbox(&output.join("receiver"))?; + let mut all = originals.clone(); + all.push(*recorded.hash()); + transfer(&sender, &receiver, &all)?; + report["stage"] = "hash-root-replay".into(); + receiver.insert_change_rec(recorded.hash(), InsertOptions::default().view(VIEW))?; + if receiver.get_file_content_on_view(SKILL, VIEW)?.as_ref() != Some(&edit) { + return Err("fresh hash-root replay differs".into()); + } + for hash in &originals { + receiver.insert_change_rec(hash, InsertOptions::default().view(VIEW))?; + } + if semantic_ids(&sender)? != semantic_ids(&receiver)? { + return Err("replay changed span semantic identities".into()); + } + report["replayed"] = true.into(); + let probe_path = "commutation-probe.txt"; + std::fs::write(sender.root().join(probe_path), b"independent\n")?; + sender.add( + sender.require_working_copy_id()?, + probe_path, + TrackingOptions::default(), + )?; + let probe = sender.record( + sender.require_working_copy_id()?, + ChangeHeader::new("independent span probe"), + RecordOptions::new() + .paths(vec![probe_path]) + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + )?; + if !probe.change().dependencies().is_empty() { + return Err("probe must be independent".into()); + } + transfer(&sender, &receiver, &[*probe.hash()])?; + receiver.insert_change_rec(probe.hash(), InsertOptions::default().view(VIEW))?; + let reordered = sandbox(&output.join("independent-first"))?; + for i in 0..7 { + let hash = reordered.save_change(&Change::empty(ChangeHeader::new(format!( + "unselected allocation {i}" + ))))?; + let mut tx = reordered.pristine().write_txn()?; + tx.register_change(&hash)?; + tx.commit()?; + } + all.push(*probe.hash()); + all.reverse(); + transfer(&sender, &reordered, &all)?; + report["stage"] = "commutation".into(); + reordered.insert_change_rec(probe.hash(), InsertOptions::default().view(VIEW))?; + reordered.insert_change_rec(recorded.hash(), InsertOptions::default().view(VIEW))?; + for hash in originals.iter().rev() { + reordered.insert_change_rec(hash, InsertOptions::default().view(VIEW))?; + } + if receiver.view_set_id(VIEW)? != reordered.view_set_id(VIEW)? { + return Err("commuted SetIds differ".into()); + } + for path in paths + .iter() + .map(String::as_str) + .chain(std::iter::once(probe_path)) + { + if receiver.get_file_content_on_view(path, VIEW)? + != reordered.get_file_content_on_view(path, VIEW)? + { + return Err(format!("commuted content differs: {path}").into()); + } + } + if semantic_ids(&receiver)? != semantic_ids(&reordered)? { + return Err("commuted line/token identities differ".into()); + } + if receiver + .pristine() + .read_txn()? + .get_internal(recorded.hash())? + == reordered + .pristine() + .read_txn()? + .get_internal(recorded.hash())? + { + return Err("allocation control did not shift IDs".into()); + } + report["commutation"] = true.into(); + report["unchanged_unrelated_entries"] = true.into(); + let identities = semantic_ids(&receiver)?; + let rows = graph_rows(&receiver)?; + if rows != graph_rows(&reordered)? { + return Err("commuted graph row counts differ".into()); + } + report["commuted_graph_rows"] = serde_json::json!(rows); + drop(receiver); + let database_path = output.join("receiver/.atomic/atomic.redb"); + let mut database = redb::Database::open(&database_path)?; + database.compact()?; + drop(database); + let reopened = Repository::open(output.join("receiver"))?; + if semantic_ids(&reopened)? != identities + || graph_rows(&reopened)? != rows + || reopened.get_file_content_on_view(SKILL, VIEW)?.as_ref() != Some(&edit) + { + return Err("reopen/compaction changed authored state".into()); + } + report["reopen_compaction"] = true.into(); + report["receiver_compacted_bytes"] = std::fs::metadata(database_path)?.len().into(); + Ok(()) + })(); + let preserved = std::fs::read(input.join(SKILL))? == edit + && std::fs::read(input.join(".atomic/atomic.redb"))? == db; + report["source_preserved"] = preserved.into(); + report["success"] = attempt.is_ok().into(); + if let Err(e) = &attempt { + report["refusal"] = e.to_string().into(); + } + std::fs::write( + output.join("result.json"), + serde_json::to_vec_pretty(&report)?, + )?; + println!("{}", serde_json::to_string_pretty(&report)?); + if !preserved { + return Err("source changed; no overwrite authorized".into()); + } + attempt +} diff --git a/atomic-repository/src/apply/inode_transport_prototype.rs b/atomic-repository/src/apply/inode_transport_prototype.rs new file mode 100644 index 00000000..7eddc9aa --- /dev/null +++ b/atomic-repository/src/apply/inode_transport_prototype.rs @@ -0,0 +1,395 @@ +//! Bounded native prototype: transport semantic names in an explicit inode and +//! graph-endpoint context. No stored patch bytes or dependency facts are changed. +//! Unsupported/ambiguous contexts refuse; this is not the general legacy repair. + +use atomic_core::change::{Atom, AtomRef, Change, FileOps, GraphOp}; +use atomic_core::crdt::tables::{decode_trunk_id, encode_branch_id, encode_leaf_id}; +use atomic_core::crdt::{BranchId, BranchOp, LeafId, LeafOp, TrunkId, TrunkOp}; +use atomic_core::pristine::{ + CrdtTxnT, EffectiveProjectionClosure, GraphTxnT, TreeTxnT, ViewMembershipSet, +}; +use atomic_core::types::{Hash, NodeId, Position}; +use std::collections::{BTreeMap, BTreeSet}; + +type Result = std::result::Result; +type Domains = BTreeMap>; + +pub(crate) fn localize( + txn: &T, + applying: NodeId, + change: &Change, + #[cfg(feature = "prototype-context-completion")] completion: Option< + &crate::repository::prototype_context::Projection, + >, +) -> Result> { + if change.hashed.span.is_some() { + return Err("inode prototype does not replace the span localizer".into()); + } + if change + .file_ops() + .iter() + .all(|op| op.referenced_node_ids().is_empty()) + { + return Ok(change.file_ops().to_vec()); + } + #[allow(unused_mut)] + let mut roots = change + .dependencies() + .iter() + .map(|h| { + txn.get_internal(h) + .map_err(|e| e.to_string())? + .ok_or_else(|| "missing patch context".into()) + }) + .collect::>>()?; + #[cfg(feature = "prototype-context-completion")] + let explicit = { + let consumer = txn + .get_external(applying) + .map_err(|e| e.to_string())? + .ok_or("inode prototype: missing applying hash")?; + let mut explicit = BTreeMap::new(); + if let Some(completion) = completion { + for (symbol, provider) in completion.bindings(consumer)? { + let id = txn + .get_internal(&provider) + .map_err(|e| e.to_string())? + .ok_or("inode prototype: completion provider not applied")?; + roots.push(id); + explicit.insert(NodeId::new(symbol), id); + } + } + explicit + }; + let context = EffectiveProjectionClosure::try_from_membership( + txn, + &ViewMembershipSet::from_ordered(roots), + ) + .map_err(|e| e.to_string())?; + let mut domains = Domains::new(); + #[cfg(feature = "prototype-context-completion")] + for (raw, owner) in &explicit { + domains.insert(*raw, BTreeSet::from([*owner])); + } + let mut bindings = Vec::new(); + for op in change.file_ops() { + if op.referenced_node_ids().is_empty() { + bindings.push(None); + continue; + } + let mut anchors = BTreeSet::new(); + let mut candidates = BTreeSet::new(); + for hunk in change + .hunks() + .iter() + .filter(|h| h.path() == Some(op.path())) + { + if let GraphOp::SetAttr { inode, .. } = hunk { + anchors.insert(resolve(txn, applying, *inode)?); + } + for atom in hunk.atoms() { + match atom { + AtomRef::Insertion(insert) | AtomRef::Atom(Atom::Insertion(insert)) => { + anchors.insert(resolve(txn, applying, insert.inode)?); + for p in insert.predecessors.iter().chain(&insert.successors) { + candidate(txn, p.change, &mut candidates)?; + } + } + AtomRef::EdgeUpdate(update) | AtomRef::Atom(Atom::EdgeUpdate(update)) => { + anchors.insert(resolve(txn, applying, update.inode)?); + for edge in &update.edges { + candidate(txn, edge.from.change, &mut candidates)?; + candidate(txn, edge.to.change, &mut candidates)?; + candidate(txn, edge.introduced_by, &mut candidates)?; + } + } + } + } + } + if anchors.len() != 1 { + return Err(format!( + "inode prototype requires one explicit graph inode for '{}'", + op.path() + )); + } + let anchor = *anchors.first().unwrap(); + if !context.contains(anchor.change) { + return Err("inode prototype: file owner is outside declared patch context".into()); + } + let inode = txn + .position_inode(anchor) + .map_err(|e| e.to_string())? + .ok_or("inode prototype: canonical graph inode is not indexed")?; + let key = txn + .get_crdt_inode_trunk(inode.get()) + .map_err(|e| e.to_string())? + .ok_or("inode prototype: graph inode has no semantic trunk")?; + let trunk = decode_trunk_id(&key); + if trunk.change_id() != anchor.change || trunk.file_idx() != op.trunk_id().file_idx() { + return Err("inode prototype: canonical inode/trunk identity disagreement".into()); + } + if op.trunk_id().change_id().is_root() { + return Err( + "inode prototype: existing-file operations need an external trunk name".into(), + ); + } + constrain( + &mut domains, + op.trunk_id().change_id(), + BTreeSet::from([trunk.change_id()]), + )?; + candidates.insert(trunk.change_id()); + #[cfg(feature = "prototype-context-completion")] + candidates.extend(explicit.values()); + candidates.retain(|id| context.contains(id)); + match op.trunk_op() { + Some(TrunkOp::Delete { trunk: raw }) + | Some(TrunkOp::Move { trunk: raw, .. }) + | Some(TrunkOp::Undelete { trunk: raw }) + | Some(TrunkOp::SetMode { trunk: raw, .. }) + | Some(TrunkOp::SetKind { trunk: raw, .. }) + if *raw != op.trunk_id() => + { + return Err("inode prototype: conflicting trunk operands".into()); + } + Some(TrunkOp::Create { .. } | TrunkOp::ProjectedSpanEdit { .. }) => { + return Err("inode prototype: unsupported external creation/projection".into()); + } + _ => {} + } + for line in op.line_ops() { + branch_domain(txn, trunk, line.branch_id(), &candidates, &mut domains)?; + match line.operation() { + BranchOp::Insert { after, content } => { + if let Some(id) = after { + branch_domain(txn, trunk, *id, &candidates, &mut domains)?; + } + leaves(txn, trunk, content, &candidates, &mut domains)?; + } + BranchOp::Delete { branch, content } => { + branch_domain(txn, trunk, *branch, &candidates, &mut domains)?; + leaves(txn, trunk, content, &candidates, &mut domains)?; + } + BranchOp::Modify { + branch, + old_content, + new_content, + } => { + branch_domain(txn, trunk, *branch, &candidates, &mut domains)?; + leaves(txn, trunk, old_content, &candidates, &mut domains)?; + leaves(txn, trunk, new_content, &candidates, &mut domains)?; + } + BranchOp::Restore { branch } => { + branch_domain(txn, trunk, *branch, &candidates, &mut domains)? + } + BranchOp::Reparent { branch, new_after } => { + branch_domain(txn, trunk, *branch, &candidates, &mut domains)?; + if let Some(id) = new_after { + branch_domain(txn, trunk, *id, &candidates, &mut domains)?; + } + } + } + } + bindings.push(Some(trunk)); + } + // Resolve only a unique injective substitution; never choose by numeric + // equality, iteration order, path history or a guessed provider. + loop { + let fixed: Vec<_> = domains + .values() + .filter(|d| d.len() == 1) + .map(|d| *d.first().unwrap()) + .collect(); + let used: BTreeSet<_> = fixed.iter().copied().collect(); + if used.len() != fixed.len() { + return Err("inode prototype: aliased foreign owner names".into()); + } + let mut progress = false; + for domain in domains.values_mut().filter(|d| d.len() > 1) { + let old = domain.len(); + domain.retain(|id| !used.contains(id)); + if domain.is_empty() { + return Err("inode prototype: contradictory name substitution".into()); + } + progress |= old != domain.len(); + } + if !progress { + break; + } + } + if domains.values().any(|d| d.len() != 1) { + return Err("inode prototype: ambiguous graph-context owner".into()); + } + let mut bound = change.file_ops().to_vec(); + for (op, binding) in bound.iter_mut().zip(bindings) { + op.remap_referenced_node_ids(|raw| { + domains + .get(&raw) + .and_then(|d| d.first()) + .copied() + .ok_or_else(|| "inode prototype: unbound owner".to_string()) + })?; + if let Some(trunk) = binding { + if op.trunk_id() != trunk { + return Err("inode prototype: transport did not preserve inode binding".into()); + } + validate_tokens(txn, applying, op)?; + } + } + Ok(bound) +} + +fn resolve( + txn: &T, + applying: NodeId, + p: Position>, +) -> Result> { + let id = match p.change { + None => applying, + Some(hash) => txn + .get_internal(&hash) + .map_err(|e| e.to_string())? + .ok_or("missing graph owner")?, + }; + Ok(Position::new(id, p.pos)) +} + +fn candidate(txn: &T, hash: Option, out: &mut BTreeSet) -> Result<()> { + if let Some(hash) = hash.filter(|h| *h != Hash::NONE) { + out.insert( + txn.get_internal(&hash) + .map_err(|e| e.to_string())? + .ok_or("missing graph context owner")?, + ); + } + Ok(()) +} + +fn constrain(domains: &mut Domains, raw: NodeId, candidates: BTreeSet) -> Result<()> { + if raw.is_root() { + return Ok(()); + } + let domain = domains.entry(raw).or_insert_with(|| candidates.clone()); + *domain = domain.intersection(&candidates).copied().collect(); + if domain.is_empty() { + return Err(format!( + "inode prototype: no typed owner in explicit graph context for symbol {}", + raw.get() + )); + } + Ok(()) +} + +fn branch_domain( + txn: &T, + trunk: TrunkId, + raw: BranchId, + candidates: &BTreeSet, + domains: &mut Domains, +) -> Result<()> { + if raw.change_id().is_root() { + return Ok(()); + } + let mut found = BTreeSet::new(); + for owner in candidates { + if txn + .get_crdt_branch(&encode_branch_id(&BranchId::new(*owner, raw.branch_idx()))) + .map_err(|e| e.to_string())? + .is_some_and(|row| row.trunk_id == trunk) + { + found.insert(*owner); + } + } + constrain(domains, raw.change_id(), found) +} + +fn leaves( + txn: &T, + trunk: TrunkId, + tokens: &[LeafOp], + candidates: &BTreeSet, + domains: &mut Domains, +) -> Result<()> { + for token in tokens { + let leaf = match token { + LeafOp::Insert { after, .. } => *after, + LeafOp::Delete { leaf } | LeafOp::Replace { leaf, .. } | LeafOp::Restore { leaf } => { + Some(*leaf) + } + }; + if let Some(raw) = leaf.filter(|id| !id.change_id().is_root()) { + let mut found = BTreeSet::new(); + for owner in candidates { + if let Some(leaf) = txn + .get_crdt_leaf(&encode_leaf_id(&LeafId::new(*owner, raw.leaf_idx()))) + .map_err(|e| e.to_string())? + { + if txn + .get_crdt_branch(&encode_branch_id(&leaf.branch_id)) + .map_err(|e| e.to_string())? + .is_some_and(|row| row.trunk_id == trunk) + { + found.insert(*owner); + } + } + } + constrain(domains, raw.change_id(), found)?; + } + } + Ok(()) +} + +fn validate_tokens(txn: &T, applying: NodeId, op: &FileOps) -> Result<()> { + for line in op.line_ops() { + let raw = line.branch_id(); + let branch = if raw.change_id().is_root() { + BranchId::new(applying, raw.branch_idx()) + } else { + raw + }; + let operand = match line.operation() { + BranchOp::Delete { branch, .. } + | BranchOp::Modify { branch, .. } + | BranchOp::Restore { branch } + | BranchOp::Reparent { branch, .. } => Some(*branch), + BranchOp::Insert { .. } => None, + }; + if let Some(raw) = operand { + let target = if raw.change_id().is_root() { + BranchId::new(applying, raw.branch_idx()) + } else { + raw + }; + if target != branch { + return Err("inode prototype: contradictory branch operands".into()); + } + } + let groups: Vec<&[LeafOp]> = match line.operation() { + BranchOp::Insert { content, .. } | BranchOp::Delete { content, .. } => vec![content], + BranchOp::Modify { + old_content, + new_content, + .. + } => vec![old_content, new_content], + _ => vec![], + }; + for token in groups.into_iter().flatten() { + let leaf = match token { + LeafOp::Insert { after, .. } => *after, + LeafOp::Delete { leaf } + | LeafOp::Replace { leaf, .. } + | LeafOp::Restore { leaf } => Some(*leaf), + }; + if let Some(leaf) = leaf.filter(|id| !id.change_id().is_root()) { + let row = txn + .get_crdt_leaf(&encode_leaf_id(&leaf)) + .map_err(|e| e.to_string())? + .ok_or("missing token context")?; + if row.branch_id != branch { + return Err("inode prototype: token belongs to another line".into()); + } + } + } + } + Ok(()) +} diff --git a/atomic-repository/src/apply/mod.rs b/atomic-repository/src/apply/mod.rs index eb6e478e..0986b1cc 100644 --- a/atomic-repository/src/apply/mod.rs +++ b/atomic-repository/src/apply/mod.rs @@ -96,6 +96,9 @@ mod cross_view; mod queries; mod types; +#[cfg(feature = "prototype-inode-transport")] +pub(crate) mod inode_transport_prototype; + #[cfg(test)] mod tests; @@ -295,6 +298,19 @@ fn write_change_to_graph_inner( .open_or_create_view(view_name) .map_err(|e| InsertError::Database(e.to_string()))?; + #[cfg(feature = "prototype-context-completion")] + if change + .hunks() + .iter() + .any(|op| matches!(op, GraphOp::PrototypeContext { .. })) + && (options.prototype_context.is_none() || !view.kind.is_draft()) + { + return Err(InsertError::InvalidChange { + message: "experimental context carriers require atomic draft sandbox composition" + .into(), + }); + } + // Validate we can apply (unless caller explicitly skipped validation, // e.g. rebuild_change_graph re-applying an existing change with new hunks). let verified_frontier = if options.skip_validation { @@ -429,6 +445,34 @@ fn apply_change_hunks_locked( let trace_record = std::env::var_os("ATOMIC_TRACE_RECORD").is_some() || std::env::var_os("ATOMIC_TRACE_INSERT").is_some(); + #[cfg(feature = "prototype-context-completion")] + for (ordinal, op) in change.hunks().iter().enumerate() { + if let GraphOp::PrototypeContext { effect } = op { + use atomic_core::change::prototype_context::ContextEffectRef; + let hash = txn + .get_external(change_id) + .map_err(|e| InsertError::Database(e.to_string()))? + .ok_or_else(|| InsertError::InvalidChange { + message: "context carrier has no hash".into(), + })?; + let valid = options + .prototype_context + .as_ref() + .is_some_and(|projection| { + projection.effects.get(&ContextEffectRef { + change: hash, + ordinal: ordinal as u32, + }) == Some(effect) + }); + if !valid { + return Err(InsertError::InvalidChange { + message: "context operation requires validated atomic sandbox composition" + .into(), + }); + } + } + } + let hunks = change.hunks(); let total_hunks = hunks.len(); let hunk_phase_start = std::time::Instant::now(); @@ -479,7 +523,13 @@ 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 preserve_legacy_as_ranges = change.hashed.span.is_none() && { + use atomic_core::pristine::{CapabilityTxnT, SPAN_LEGACY_CONTEXT_CAPABILITY}; + txn.required_capability_version(SPAN_LEGACY_CONTEXT_CAPABILITY.id()) + .map_err(|e| InsertError::Database(e.to_string()))? + .is_some() + }; + if change.has_file_ops() && !preserve_legacy_as_ranges { let mut localized = None; if change.hashed.span.is_some() { localized = Some( @@ -490,6 +540,19 @@ fn apply_change_hunks_locked( )?, ); } + #[cfg(feature = "prototype-inode-transport")] + if change.hashed.span.is_none() { + localized = Some( + inode_transport_prototype::localize( + txn, + change_id, + change, + #[cfg(feature = "prototype-context-completion")] + options.prototype_context.as_deref(), + ) + .map_err(|message| InsertError::InvalidChange { message })?, + ); + } let file_ops = localized.as_deref().unwrap_or_else(|| change.file_ops()); if change.hashed.span.is_none() && file_ops.iter().any(|ops| { diff --git a/atomic-repository/src/apply/types.rs b/atomic-repository/src/apply/types.rs index 4766a5be..345e4f89 100644 --- a/atomic-repository/src/apply/types.rs +++ b/atomic-repository/src/apply/types.rs @@ -154,6 +154,9 @@ pub struct InsertOptions { /// inserting its first dependency does not invalidate its own review pin. /// Default: `false` pub(crate) skip_publication_gate: bool, + #[cfg(feature = "prototype-context-completion")] + pub(crate) prototype_context: + Option>, } impl Default for InsertOptions { @@ -166,6 +169,8 @@ impl Default for InsertOptions { track_conflicts: true, skip_validation: false, skip_publication_gate: false, + #[cfg(feature = "prototype-context-completion")] + prototype_context: None, } } } diff --git a/atomic-repository/src/context_witness_prototype.rs b/atomic-repository/src/context_witness_prototype.rs new file mode 100644 index 00000000..a3834d68 --- /dev/null +++ b/atomic-repository/src/context_witness_prototype.rs @@ -0,0 +1,924 @@ +//! Read-only prototype: reconstruct a branch-owner context witness from a +//! selected, hash-verified patch set. This neither applies a repair nor changes +//! dependencies. No chronology or receiving-repository numeric IDs select an owner. +//! +//! cargo run -p atomic-repository --example reconstruct_semantic_context -- \ +//! +use crate::Repository; +use atomic_core::change::{Atom, Change, GraphOp, VerifiedChange}; +use atomic_core::crdt::{BranchOp, LeafOp, TrunkOp}; +use atomic_core::pristine::{GraphTxnT, ViewTxnT}; +use atomic_core::types::{Base32, Hash}; +use serde::{Deserialize, Serialize}; +use std::collections::{BTreeMap, BTreeSet}; +use std::path::Path; + +type Result = std::result::Result>; +pub(crate) type Objects = BTreeMap; + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +struct File { + owner: Hash, + position: u64, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +struct Reference { + file: File, + branch: u32, + role: String, +} + +fn anchor(p: atomic_core::types::Position>, own: Hash) -> File { + File { + owner: p.change.unwrap_or(own), + position: p.pos.get(), + } +} + +fn file(change: &Change, own: Hash, path: &str) -> Result { + let mut files = BTreeSet::new(); + for hunk in change.hunks().iter().filter(|h| h.path() == Some(path)) { + match hunk { + GraphOp::FileAdd { add_inode, .. } => { + files.insert(File { + owner: own, + position: add_inode.start.get(), + }); + } + GraphOp::Edit { change, .. } => { + files.insert(anchor( + match change { + Atom::Insertion(i) => i.inode, + Atom::EdgeUpdate(e) => e.inode, + }, + own, + )); + } + GraphOp::Replacement { + change, + replacement, + .. + } => { + files.insert(anchor(change.inode, own)); + files.insert(anchor(replacement.inode, own)); + } + GraphOp::FileMove { del, add, .. } => { + files.insert(anchor(del.inode, own)); + files.insert(anchor(add.inode, own)); + } + GraphOp::FileDel { del, contents, .. } => { + files.insert(anchor(contents.as_ref().unwrap_or(del).inode, own)); + } + GraphOp::FileUndel { + undel, contents, .. + } => { + files.insert(anchor(contents.as_ref().unwrap_or(undel).inode, own)); + } + GraphOp::SetAttr { inode, .. } => { + files.insert(anchor(*inode, own)); + } + _ => {} + } + } + if files.len() != 1 { + return Err(format!("{} graph inode anchors for {path}", files.len()).into()); + } + Ok(files.into_iter().next().unwrap()) +} + +fn validate_file(objects: &Objects, f: &File, index: u32) -> Result<()> { + let creator = objects + .get(&f.owner) + .ok_or("file creator is outside selected context")?; + let additions: Vec<_> = creator + .hunks() + .iter() + .filter_map(|h| match h { + GraphOp::FileAdd { + add_inode, path, .. + } if add_inode.start.get() == f.position => Some(path), + _ => None, + }) + .collect(); + if additions.len() != 1 { + return Err("file inode has no unique canonical creation".into()); + } + let declarations: Vec<_> = creator + .file_ops() + .iter() + .filter(|o| { + o.path() == additions[0] && matches!(o.trunk_op(), Some(TrunkOp::Create { .. })) + }) + .collect(); + if declarations.len() != 1 + || !declarations[0].trunk_id().change_id().is_root() + || declarations[0].trunk_id().file_idx() != index + { + return Err(format!("semantic file index {index} disagrees with graph inode creation {}:{} ({}, declared trunks: {:?})", + f.owner, f.position, additions[0], declarations.iter().map(|o| o.trunk_id()).collect::>()).into()); + } + Ok(()) +} + +fn requirements( + objects: &Objects, + consumer: Hash, + symbol: u64, +) -> Result<(Vec, BTreeMap)> { + if symbol == 0 { + return Err("ROOT is not a foreign owner symbol".into()); + } + let change = objects + .get(&consumer) + .ok_or("consumer is outside selected context")?; + let mut refs = Vec::new(); + let mut forced = BTreeMap::new(); + for (oi, ops) in change.file_ops().iter().enumerate() { + let relevant = ops + .referenced_node_ids() + .iter() + .any(|id| id.get() == symbol); + let owner = ops.trunk_id().change_id(); + if !relevant && owner.is_root() { + continue; + } + let f = file(change, consumer, ops.path())?; + if !owner.is_root() { + if let Some(previous) = forced.insert(owner.get(), f.owner) { + if previous != f.owner { + return Err("consumer has contradictory inode owner bindings".into()); + } + } + } + // This is a witness for one patch-scoped name, not certification of + // unrelated operations in the entire historical patch. Keep canonical + // owner constraints above, but validate indices/operands only where + // this symbol occurs. In particular, unrelated ROOT attribute ops do + // not supply or consume a foreign branch-owner name. + if !relevant { + continue; + } + validate_file(objects, &f, ops.trunk_id().file_idx())?; + let trunk_operand = match ops.trunk_op() { + Some(TrunkOp::Delete { trunk }) + | Some(TrunkOp::Move { trunk, .. }) + | Some(TrunkOp::Undelete { trunk }) + | Some(TrunkOp::SetMode { trunk, .. }) + | Some(TrunkOp::SetKind { trunk, .. }) => Some(*trunk), + _ => None, + }; + if owner.get() == symbol || trunk_operand.is_some_and(|t| t.change_id().get() == symbol) { + return Err("this bounded witness prototype covers branch-owner references, not trunk completion".into()); + } + for (li, line) in ops.line_ops().iter().enumerate() { + let prefix = format!("file_ops[{oi}].line[{li}]"); + let mut add = |b: atomic_core::crdt::BranchId, role: String| { + if b.change_id().get() == symbol { + refs.push(Reference { + file: f.clone(), + branch: b.branch_idx(), + role, + }); + } + }; + add(line.branch_id(), format!("{prefix}.identity")); + let mut branches = Vec::new(); + let mut groups: Vec<&[LeafOp]> = Vec::new(); + match line.operation() { + BranchOp::Insert { after, content } => { + branches.extend(*after); + groups.push(content); + } + BranchOp::Delete { branch, content } => { + branches.push(*branch); + groups.push(content); + } + BranchOp::Modify { + branch, + old_content, + new_content, + } => { + branches.push(*branch); + groups.push(old_content); + groups.push(new_content); + } + BranchOp::Restore { branch } => branches.push(*branch), + BranchOp::Reparent { branch, new_after } => { + branches.push(*branch); + branches.extend(*new_after); + } + } + if !matches!(line.operation(), BranchOp::Insert { .. }) + && branches[0] != line.branch_id() + { + return Err("contradictory branch identity and operation operand".into()); + } + for (i, b) in branches.into_iter().enumerate() { + add(b, format!("{prefix}.branch_operand[{i}]")); + } + for token in groups.into_iter().flatten() { + let leaf = match token { + LeafOp::Insert { after, .. } => *after, + LeafOp::Delete { leaf } + | LeafOp::Replace { leaf, .. } + | LeafOp::Restore { leaf } => Some(*leaf), + }; + if leaf.is_some_and(|l| l.change_id().get() == symbol) { + return Err("this bounded witness prototype cannot leave token-owner occurrences uncovered".into()); + } + } + } + } + if refs.is_empty() { + return Err("symbol has no branch references".into()); + } + Ok((refs, forced)) +} + +fn insertions<'a>( + change: &'a Change, + own: Hash, + f: &File, +) -> Vec<&'a atomic_core::change::Insertion>> { + let mut insertions = Vec::new(); + for hunk in change.hunks() { + let insertion = match hunk { + GraphOp::FileAdd { + add_inode, + contents, + .. + } if *f + == (File { + owner: own, + position: add_inode.start.get(), + }) => + { + contents.as_ref() + } + GraphOp::Edit { + change: Atom::Insertion(i), + .. + } => Some(i), + GraphOp::Replacement { replacement, .. } => Some(replacement), + _ => None, + }; + if let Some(i) = insertion { + if !i.flag.contains(atomic_core::types::EdgeFlags::FOLDER) && anchor(i.inode, own) == *f + { + insertions.push(i); + } + } + } + insertions +} + +fn spans(change: &Change, own: Hash, f: &File) -> Vec<(u64, u64)> { + let mut spans: Vec<_> = insertions(change, own, f) + .iter() + .map(|i| (i.start.get(), i.end.get())) + .collect(); + spans.sort(); + spans +} + +fn line_start(objects: &Objects, change: &Change, own: Hash, f: &File, lo: u64) -> bool { + insertions(change, own, f).iter().any(|i| { + if i.start.get() < lo && lo < i.end.get() { + return change.contents.get(lo as usize - 1) == Some(&b'\n'); + } + i.start.get() == lo + && !i.predecessors.is_empty() + && i.predecessors.iter().all(|p| { + let position = anchor(*p, own); + if position == *f { + return true; + } // Exact empty file-inode marker. + position + .position + .checked_sub(1) + .and_then(|index| objects.get(&position.owner)?.contents.get(index as usize)) + == Some(&b'\n') + }) + }) +} + +fn covers(spans: &[(u64, u64)], lo: u64, hi: u64) -> bool { + let mut at = lo; + for (start, end) in spans { + if *end <= at { + continue; + } + if *start > at { + break; + } + at = at.max(*end); + if at >= hi { + return true; + } + } + at >= hi +} + +fn declarations(objects: &Objects, own: Hash) -> Result> { + let change = objects + .get(&own) + .ok_or("provider is outside selected context")?; + let mut seen = BTreeSet::new(); + let mut out = BTreeMap::new(); + for (oi, ops) in change.file_ops().iter().enumerate() { + if !ops.line_ops().iter().any(|line| { + line.branch_id().change_id().is_root() + && matches!(line.operation(), BranchOp::Insert { .. }) + }) { + continue; + } + let f = file(change, own, ops.path())?; + validate_file(objects, &f, ops.trunk_id().file_idx())?; + let ranges = spans(change, own, &f); + for (li, line) in ops.line_ops().iter().enumerate() { + let BranchOp::Insert { content, .. } = line.operation() else { + continue; + }; + if !line.branch_id().change_id().is_root() { + continue; + } + if !seen.insert(line.branch_id().branch_idx()) { + return Err("duplicate provider branch declaration".into()); + } + let Some((lo, hi)) = line.content_range() else { + continue; + }; + let (lo, hi) = (lo.get(), hi.get()); + if hi <= lo { + continue; + } + let raw = change + .contents + .get(lo as usize..hi as usize) + .ok_or("semantic range outside verified contents")?; + let mut bytes = Vec::new(); + for token in content { + match token { + LeafOp::Insert { content, .. } => bytes.extend_from_slice(content), + _ => return Err("unsupported provider token edit".into()), + } + } + // Match the actual tokenizer: LF separates branches; no other byte, + // including CR and trailing spaces, may be silently trimmed. + let Some(body) = raw.strip_suffix(b"\n") else { + continue; + }; + if body.contains(&b'\n') + || bytes != body + || !covers(&ranges, lo, hi) + || !line_start(objects, change, own, &f, lo) + { + continue; + } + let relevant: Vec<_> = ranges + .iter() + .filter(|(start, end)| *end > lo && *start < hi) + .copied() + .collect(); + out.insert( + (f.clone(), line.branch_id().branch_idx()), + serde_json::json!({ + "file_ops":oi,"line_op":li,"owner":own,"start":lo,"end":hi, + "bytes_blake3":Hash::of(raw),"graph_inode":f,"graph_spans":relevant + }), + ); + } + } + Ok(out) +} + +fn provider_allowed(provider: Hash, symbol: u64, forced: &BTreeMap) -> bool { + forced.iter().all(|(name, owner)| { + if *name == symbol { + *owner == provider + } else { + *owner != provider + } + }) +} + +fn order_anomalies(change: &Change) -> Vec { + let mut out = Vec::new(); + for (oi, ops) in change.file_ops().iter().enumerate() { + for (li, line) in ops.line_ops().iter().enumerate() { + if let BranchOp::Reparent { + branch, + new_after: Some(after), + } = line.operation() + { + if branch == after { + out.push(serde_json::json!({ + "kind":"self_reparent", "file_ops":oi, "line_op":li, + "foreign_symbol":branch.change_id().get(), "branch":branch.branch_idx() + })); + } + } + } + } + out +} + +fn requires_context(objects: &Objects, provider: Hash, consumer: Hash) -> Result { + let mut pending = vec![provider]; + let mut seen = BTreeSet::new(); + while let Some(hash) = pending.pop() { + if hash == consumer { + return Ok(true); + } + if seen.insert(hash) { + pending.extend( + objects + .get(&hash) + .ok_or("incomplete provider patch context")? + .dependencies(), + ); + } + } + Ok(false) +} + +pub(crate) fn proof( + objects: &Objects, + consumer: Hash, + symbol: u64, + provider: Hash, +) -> Result { + if consumer == provider { + return Err("foreign owner cannot be self".into()); + } + if requires_context(objects, provider, consumer)? { + return Err("completion would make required patch context cyclic".into()); + } + let (needs, forced) = requirements(objects, consumer, symbol)?; + if !provider_allowed(provider, symbol, &forced) { + return Err("provider aliases another inode-proven consumer name".into()); + } + let declared = declarations(objects, provider)?; + let mut proofs = Vec::new(); + for need in &needs { + let declaration = declared + .get(&(need.file.clone(), need.branch)) + .ok_or("provider does not cover every reference")?; + proofs.push(serde_json::json!({"reference":need,"declaration":declaration})); + } + Ok( + serde_json::json!({"consumer":consumer,"foreign_symbol":symbol,"provider":provider,"proofs":proofs, + "consumer_order_anomalies":order_anomalies(&objects[&consumer]),"application_validated":false}), + ) +} + +pub(crate) fn reconstruct( + objects: &Objects, + consumer: Hash, + symbol: u64, +) -> Result { + requirements(objects, consumer, symbol)?; + let mut choices = Vec::new(); + for provider in objects.keys().copied() { + if let Ok(witness) = proof(objects, consumer, symbol, provider) { + choices.push(witness); + } + } + if choices.is_empty() { + return Err("no complete graph-backed provider in the selected scope".into()); + } + if choices.len() != 1 { + return Err(format!("ambiguous context: {} complete providers", choices.len()).into()); + } + let mut witness = choices.pop().unwrap(); + witness["search_scope"] = serde_json::json!(objects.keys().collect::>()); + Ok(witness) +} + +pub(crate) fn verify_positive_witness( + objects: &Objects, + witness: &serde_json::Value, +) -> Result<()> { + let consumer = serde_json::from_value(witness["consumer"].clone())?; + let provider = serde_json::from_value(witness["provider"].clone())?; + let symbol = witness["foreign_symbol"] + .as_u64() + .ok_or("invalid foreign symbol")?; + let expected = proof(objects, consumer, symbol, provider)?; + for key in [ + "consumer", + "provider", + "foreign_symbol", + "proofs", + "consumer_order_anomalies", + "application_validated", + ] { + if witness[key] != expected[key] { + return Err(format!("incomplete or altered context witness: {key}").into()); + } + } + // search_scope records how an automatic choice was obtained. It is not a + // lifetime dependency on a live view: positive endpoint validation does not + // switch providers when another candidate later becomes available. + Ok(()) +} + +// Included by the read-only example as well as the experimental native module. +#[allow(dead_code)] +pub(crate) fn run_example() -> Result<()> { + let args: Vec<_> = std::env::args().collect(); + if args.len() != 5 { + return Err("usage: reconstruct_semantic_context ".into()); + } + let repo = Repository::open_readonly(&args[1])?; + let destination = Path::new(&args[4]); + if destination.exists() { + return Err("refuse existing output".into()); + } + let parent = destination + .parent() + .filter(|p| !p.as_os_str().is_empty()) + .unwrap_or(Path::new(".")); + let output = + std::fs::canonicalize(parent)?.join(destination.file_name().ok_or("invalid output name")?); + if output.starts_with(std::fs::canonicalize(repo.root())?) + || output.starts_with(std::fs::canonicalize(repo.dot_dir())?) + { + return Err("diagnostic output must be outside the read-only repository".into()); + } + let db = repo.dot_dir().join("atomic.redb"); + let before = std::fs::read(&db)?; + let consumer = Hash::from_base32(args[2].as_bytes()).ok_or("invalid consumer hash")?; + let symbol = args[3].parse()?; + let txn = repo.pristine().read_txn()?; + let view = txn + .get_view(repo.current_view())? + .ok_or("missing selected view")?; + let scope = crate::repository::effective_projection_closure(&txn, &view)?; + let store = crate::changestore::ChangeStore::open_existing(repo.changes_dir(), 32)?; + let mut objects = Objects::new(); + for id in scope.iter_dependency_first().filter(|id| !id.is_root()) { + let hash = txn + .get_external(*id)? + .ok_or("unregistered context object")?; + let bytes = std::fs::read(store.change_path(&hash))?; + let original = VerifiedChange::from_bytes(&bytes)?; + if original.hash() != hash { + return Err("stored object identity mismatch".into()); + } + objects.insert(hash, original.change().clone()); + } + drop(txn); + let mut witness = reconstruct(&objects, consumer, symbol)?; + verify_positive_witness(&objects, &witness)?; + witness["meaning"] = serde_json::json!( + "read-only prototype context witness; not an applied repair or original-author attestation" + ); + witness["search_set_id"] = serde_json::json!(repo.view_set_id(repo.current_view())?); + witness["audit_source_database_blake3"] = serde_json::json!(Hash::of(&before)); + if before != std::fs::read(&db)? { + return Err("source database changed during planning; refusing a stale result".into()); + } + let refs = witness["proofs"].as_array().unwrap(); + let distinct: BTreeSet<_> = refs + .iter() + .map(|p| { + ( + p["reference"]["file"].to_string(), + p["reference"]["branch"].to_string(), + ) + }) + .collect(); + let mut output = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(output)?; + std::io::Write::write_all(&mut output, &serde_json::to_vec_pretty(&witness)?)?; + println!( + "Reconstructed {} reference occurrences, {} distinct graph-backed branches; provider {}", + refs.len(), + distinct.len(), + witness["provider"] + ); + println!("Detected {} self-parent ordering operations in the consumer; this endpoint witness does not validate their composition.", witness["consumer_order_anomalies"].as_array().unwrap().len()); + println!( + "Original database unchanged. No patch, dependency, capability or binding was applied." + ); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::record::RecordOptions; + use crate::{TrackingOptions, UnrecordOptions}; + use atomic_core::change::{ChangeHeader, LineOps}; + use atomic_core::crdt::BranchId; + use atomic_core::types::{ChangePosition, NodeId}; + + fn insert(objects: &mut Objects, change: Change) -> Hash { + let mut raw = Vec::new(); + let encoded = change.serialize(&mut raw).unwrap(); + let verified = VerifiedChange::from_bytes(&raw).unwrap(); + assert_eq!(verified.hash(), encoded); + let hash = verified.hash(); + objects.insert(hash, verified.change().clone()); + hash + } + fn record(repo: &Repository, text: &[u8], message: &str) -> Hash { + std::fs::write(repo.root().join("f"), text).unwrap(); + *repo + .record( + repo.require_working_copy_id().unwrap(), + ChangeHeader::new(message), + RecordOptions::new() + .paths(vec!["f"]) + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap() + .hash() + } + fn fixture() -> (Objects, Hash, Hash) { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + std::fs::write(dir.path().join("f"), b"alpha\nbeta\ntheta\n").unwrap(); + repo.add( + repo.require_working_copy_id().unwrap(), + "f", + TrackingOptions::default(), + ) + .unwrap(); + let a = record(&repo, b"alpha\nbeta\ntheta\n", "synthetic base"); + let q = record(&repo, b"alpha\nbeta\ntheta\ngamma\n", "synthetic provider"); + repo.unrecord(&q, UnrecordOptions::default()).unwrap(); + let p = record(&repo, b"ALPHA\nbeta\ntheta\n", "synthetic consumer"); + let mut consumer = repo.load_change(&p).unwrap(); + let foreign = BranchId::new(NodeId::new(15), 0); + consumer.hashed.file_ops[0].add_line_op(LineOps::new( + foreign, + BranchOp::Reparent { + branch: foreign, + new_after: Some(BranchId::new(NodeId::new(1), 2)), + }, + )); + consumer.hashed.header = ChangeHeader::new("synthetic unresolved branch context"); + let mut objects = Objects::new(); + insert(&mut objects, repo.load_change(&a).unwrap()); + let provider = insert(&mut objects, repo.load_change(&q).unwrap()); + let consumer = insert(&mut objects, consumer); + assert!(!objects[&consumer].dependencies().contains(&provider)); + (objects, consumer, provider) + } + fn duplicate(objects: &mut Objects, provider: Hash) -> Hash { + let p = &objects[&provider]; + let copy = Change::with_file_ops( + ChangeHeader::new("synthetic second matching provider"), + p.hunks().to_vec(), + p.file_ops().to_vec(), + p.contents.clone(), + p.dependencies().to_vec(), + ); + insert(objects, copy) + } + + #[test] + fn reconstructs_and_verifies_every_occurrence_without_local_ids() { + let (objects, consumer, provider) = fixture(); + let witness = reconstruct(&objects, consumer, 15).unwrap(); + verify_positive_witness(&objects, &witness).unwrap(); + assert_eq!(witness["provider"], serde_json::json!(provider)); + assert_eq!(witness["proofs"].as_array().unwrap().len(), 2); + let reversed: Objects = objects.iter().rev().map(|(h, c)| (*h, c.clone())).collect(); + assert_eq!(witness, reconstruct(&reversed, consumer, 15).unwrap()); + } + #[test] + fn unrelated_attribute_indices_do_not_block_a_branch_owner_witness() { + use atomic_core::change::FileOps; + use atomic_core::crdt::TrunkId; + let (mut objects, consumer, provider) = fixture(); + let mut change = objects.remove(&consumer).unwrap(); + // Reproduce the unrelated ROOT attribute-operation pattern in HFAP35. + // It contains no occurrence of the requested foreign symbol. + change.hashed.file_ops.push( + FileOps::set_mode(TrunkId::new(NodeId::ROOT, 1), "unrelated.py".into(), 0o644).unwrap(), + ); + let consumer = insert(&mut objects, change); + let witness = reconstruct(&objects, consumer, 15).unwrap(); + assert_eq!(witness["provider"], serde_json::json!(provider)); + assert_eq!(witness["proofs"].as_array().unwrap().len(), 2); + verify_positive_witness(&objects, &witness).unwrap(); + } + #[test] + fn an_index_disagreement_at_the_requested_symbol_still_refuses() { + use atomic_core::change::FileOps; + use atomic_core::crdt::TrunkId; + let (mut objects, consumer, provider) = fixture(); + let mut change = objects.remove(&consumer).unwrap(); + let original = change.hashed.file_ops.remove(0); + let mut wrong = FileOps::edit( + TrunkId::new(original.trunk_id().change_id(), 99), + original.path().into(), + ); + for line in original.line_ops() { + wrong.add_line_op(line.clone()); + } + change.hashed.file_ops.push(wrong); + let consumer = insert(&mut objects, change); + assert!(proof(&objects, consumer, 15, provider) + .unwrap_err() + .to_string() + .contains("semantic file index 99")); + } + #[test] + fn missing_provider_never_falls_back_to_an_inode_owner_number() { + let (mut objects, consumer, provider) = fixture(); + objects.remove(&provider); + assert!(reconstruct(&objects, consumer, 15) + .unwrap_err() + .to_string() + .contains("no complete")); + } + #[test] + fn another_valid_provider_blocks_new_automatic_choice_but_not_a_pinned_witness() { + let (mut objects, consumer, provider) = fixture(); + let witness = reconstruct(&objects, consumer, 15).unwrap(); + duplicate(&mut objects, provider); + assert!(reconstruct(&objects, consumer, 15) + .unwrap_err() + .to_string() + .contains("ambiguous")); + verify_positive_witness(&objects, &witness).unwrap(); + assert_eq!(witness["provider"], serde_json::json!(provider)); + } + #[test] + fn incomplete_or_modified_witness_is_rejected() { + let (objects, consumer, _) = fixture(); + let original = reconstruct(&objects, consumer, 15).unwrap(); + let mut missing = original.clone(); + missing["proofs"].as_array_mut().unwrap().pop(); + assert!(verify_positive_witness(&objects, &missing).is_err()); + let mut changed = original; + changed["proofs"][0]["declaration"]["end"] = serde_json::json!(9999); + assert!(verify_positive_witness(&objects, &changed).is_err()); + } + #[test] + fn complete_context_does_not_certify_a_self_parent_order_operation() { + let (mut objects, consumer, provider) = fixture(); + let mut p = objects.remove(&consumer).unwrap(); + let branch = BranchId::new(NodeId::new(15), 0); + let last = p.hashed.file_ops[0].line_ops_mut().last_mut().unwrap(); + *last = LineOps::new( + branch, + BranchOp::Reparent { + branch, + new_after: Some(branch), + }, + ); + let consumer = insert(&mut objects, p); + let mut witness = reconstruct(&objects, consumer, 15).unwrap(); + verify_positive_witness(&objects, &witness).unwrap(); + assert_eq!(witness["provider"], serde_json::json!(provider)); + assert_eq!(witness["application_validated"], false); + assert_eq!( + witness["consumer_order_anomalies"] + .as_array() + .unwrap() + .len(), + 1 + ); + witness["consumer_order_anomalies"] = serde_json::json!([]); + assert!(verify_positive_witness(&objects, &witness).is_err()); + } + #[test] + fn a_provider_requiring_the_consumer_is_not_a_valid_completion() { + let (mut objects, consumer, provider) = fixture(); + let p = objects.remove(&provider).unwrap(); + let cyclic = Change::with_file_ops( + ChangeHeader::new("synthetic cyclic completion"), + p.hunks().to_vec(), + p.file_ops().to_vec(), + p.contents, + vec![consumer], + ); + insert(&mut objects, cyclic); + assert!(reconstruct(&objects, consumer, 15).is_err()); + } + #[test] + fn a_semantic_range_without_graph_byte_support_cannot_supply_context() { + let (mut objects, consumer, provider) = fixture(); + let mut bad = objects.remove(&provider).unwrap(); + let line = &mut bad.hashed.file_ops[0].line_ops_mut()[0]; + let (lo, hi) = line.content_range().unwrap(); + line.set_content_range(ChangePosition::new(lo.get() + 1), hi); + insert(&mut objects, bad); + assert!(reconstruct(&objects, consumer, 15).is_err()); + } + #[test] + fn matching_path_text_and_indices_do_not_override_a_different_inode() { + let (mut objects, consumer, provider) = fixture(); + let original = &objects[&consumer]; + let f = file(original, consumer, "f").unwrap(); + let base = &objects[&f.owner]; + let other = Change::with_file_ops( + ChangeHeader::new("synthetic different inode at the same path"), + base.hunks().to_vec(), + base.file_ops().to_vec(), + base.contents.clone(), + base.dependencies().to_vec(), + ); + let other = insert(&mut objects, other); + let mut wrong = objects.remove(&provider).unwrap(); + wrong.hashed.header = ChangeHeader::new("synthetic provider on the other inode"); + wrong.hashed.dependencies = vec![other]; + for hunk in &mut wrong.hashed.hunks { + if let GraphOp::Edit { + change: Atom::Insertion(i), + .. + } = hunk + { + i.inode.change = Some(other); + for p in i.predecessors.iter_mut().chain(&mut i.successors) { + p.change = Some(other); + } + } + } + insert(&mut objects, wrong); + assert!(reconstruct(&objects, consumer, 15).is_err()); + } + #[test] + fn token_bytes_alone_do_not_prove_graph_coverage() { + let (mut objects, consumer, provider) = fixture(); + let mut wrong = objects.remove(&provider).unwrap(); + if let GraphOp::Edit { + change: Atom::Insertion(i), + .. + } = &mut wrong.hashed.hunks[0] + { + i.start = ChangePosition::new(i.start.get() + 1); + } + insert(&mut objects, wrong); + assert!(reconstruct(&objects, consumer, 15).is_err()); + } + #[test] + fn matching_tokens_in_the_middle_of_a_line_are_not_a_branch_witness() { + let (mut objects, consumer, provider) = fixture(); + let mut bad = objects.remove(&provider).unwrap(); + let old = bad.file_ops()[0].line_ops()[0].clone(); + let (lo, hi) = old.content_range().unwrap(); + let mut operation = old.operation().clone(); + if let BranchOp::Insert { content, .. } = &mut operation { + *content = vec![LeafOp::Insert { + after: None, + kind: atomic_core::diff::token::TokenKind::Word, + content: bad.contents[lo.get() as usize + 1..hi.get() as usize - 1].to_vec(), + }]; + } + bad.hashed.file_ops[0].line_ops_mut()[0] = LineOps::new(old.branch_id(), operation) + .with_content_range(ChangePosition::new(lo.get() + 1), hi); + insert(&mut objects, bad); + assert!(reconstruct(&objects, consumer, 15).is_err()); + } + #[test] + fn an_uncovered_token_occurrence_prevents_branch_only_certification() { + let (mut objects, consumer, _) = fixture(); + let mut p = objects.remove(&consumer).unwrap(); + let old = p.file_ops()[0].line_ops()[0].clone(); + let mut op = old.operation().clone(); + if let BranchOp::Modify { old_content, .. } = &mut op { + old_content.push(LeafOp::Restore { + leaf: atomic_core::crdt::LeafId::new(NodeId::new(15), 0), + }); + } else { + panic!("fixture must modify the original first line"); + } + let (lo, hi) = old.content_range().unwrap(); + p.hashed.file_ops[0].line_ops_mut()[0] = + LineOps::new(old.branch_id(), op).with_content_range(lo, hi); + let consumer = insert(&mut objects, p); + assert!(reconstruct(&objects, consumer, 15).is_err()); + } + #[test] + fn duplicate_declarations_and_uncovered_references_do_not_certify_partial_context() { + let (mut objects, consumer, provider) = fixture(); + let mut bad = objects.remove(&provider).unwrap(); + let line = bad.file_ops()[0].line_ops()[0].clone(); + bad.hashed.file_ops[0].add_line_op(line); + insert(&mut objects, bad); + assert!(reconstruct(&objects, consumer, 15).is_err()); + let (mut objects, consumer, _) = fixture(); + let mut p = objects.remove(&consumer).unwrap(); + let missing = BranchId::new(NodeId::new(15), 999); + p.hashed.file_ops[0].add_line_op(LineOps::new( + missing, + BranchOp::Reparent { + branch: missing, + new_after: None, + }, + )); + let changed_consumer = insert(&mut objects, p); + assert!(reconstruct(&objects, changed_consumer, 15).is_err()); + } +} diff --git a/atomic-repository/src/lib.rs b/atomic-repository/src/lib.rs index b70c4047..5eb1913a 100644 --- a/atomic-repository/src/lib.rs +++ b/atomic-repository/src/lib.rs @@ -167,6 +167,9 @@ pub use manifest::{ManifestError, ViewManifest}; // Repository exports pub use repository::*; +#[cfg(feature = "prototype-context-completion")] +mod context_witness_prototype; + // Vault name generation pub use repository::generate_goal_name; diff --git a/atomic-repository/src/repository/changes.rs b/atomic-repository/src/repository/changes.rs index a063790e..e9c7befa 100644 --- a/atomic-repository/src/repository/changes.rs +++ b/atomic-repository/src/repository/changes.rs @@ -22,6 +22,24 @@ fn atom_change_file_version(bytes: &[u8]) -> Result { } impl Repository { + pub(super) fn fence_legacy_span_storage(&self, change: &Change) -> Result<(), RepositoryError> { + if change + .hashed + .span + .as_ref() + .is_some_and(|p| p.version == atomic_core::change::span_patch::LEGACY_SOURCE_VERSION) + { + change + .validate_v2() + .map_err(|e| RepositoryError::Serialization(e.to_string()))?; + self.pristine.require_repository_capabilities(&[ + atomic_core::pristine::SPAN_RANGE_CAPABILITY, + atomic_core::pristine::SPAN_LEGACY_CONTEXT_CAPABILITY, + atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY, + ])?; + } + Ok(()) + } fn require_projected_semantic_storage_capability( &self, change: &Change, @@ -163,6 +181,7 @@ impl Repository { /// println!("Saved change: {}", hash.to_base32()); /// ``` pub fn save_change(&self, change: &Change) -> Result { + self.fence_legacy_span_storage(change)?; self.require_projected_semantic_storage_capability(change)?; if change.file_ops().iter().any(|ops| { matches!( @@ -202,18 +221,13 @@ impl Repository { computed.to_base32() ))); } + self.fence_legacy_span_storage(&change)?; 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(), @@ -274,6 +288,24 @@ impl Repository { change: &Change, ) -> Result { let version = atom_change_file_version(change_bytes)?; + if change + .hashed + .span + .as_ref() + .is_some_and(|p| p.version == atomic_core::change::span_patch::LEGACY_SOURCE_VERSION) + { + let verified = atomic_core::change::VerifiedChange::from_bytes(change_bytes) + .map_err(|e| RepositoryError::Serialization(e.to_string()))?; + if verified.hash() != *hash + || verified.change().hashed != change.hashed + || verified.change().contents != change.contents + { + return Err(RepositoryError::Serialization( + "legacy span bytes differ from supplied identity".into(), + )); + } + self.fence_legacy_span_storage(change)?; + } self.require_projected_semantic_storage_capability(change)?; if version == ATOM_CHANGE_FILE_VERSION_SPAN { let (decoded, actual_hash) = @@ -1874,7 +1906,8 @@ impl Repository { } } -#[cfg(unix)] +/// Publish a projected change object exactly once: the complete bytes or +/// nothing, and never over a different object already stored at `path`. fn save_projected_change_without_overwrite( path: &Path, bytes: &[u8], @@ -1890,13 +1923,13 @@ fn save_projected_change_without_overwrite( // 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()?; + sync_projected_object(path)?; let parent = path .parent() .ok_or_else(|| RepositoryError::InvalidOperation { message: "projected change path has no parent".into(), })?; - std::fs::File::open(parent)?.sync_all()?; + sync_projected_directory(parent)?; Ok(()) } match std::fs::symlink_metadata(path) { @@ -1912,7 +1945,7 @@ fn save_projected_change_without_overwrite( 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()?; + sync_projected_directory(ancestor)?; } let mut temporary = tempfile::NamedTempFile::new_in(parent)?; temporary.write_all(bytes)?; @@ -1927,7 +1960,7 @@ fn save_projected_change_without_overwrite( } match temporary.persist_noclobber(path) { Ok(_) => { - std::fs::File::open(parent)?.sync_all()?; + sync_projected_directory(parent)?; Ok(()) } Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => { @@ -1937,24 +1970,61 @@ fn save_projected_change_without_overwrite( } } -#[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(), - }) +/// Flush an already published object's bytes to stable storage. +fn sync_projected_object(path: &Path) -> std::io::Result<()> { + #[cfg(unix)] + let file = std::fs::File::open(path)?; + // Windows only flushes through a handle that was opened for writing. + #[cfg(not(unix))] + let file = std::fs::OpenOptions::new().write(true).open(path)?; + file.sync_all() +} + +/// Make a directory's entries durable. Directory fsync is only portable on +/// Unix; elsewhere the no-clobber rename that publishes the object is still +/// atomic, so a reader never observes a partial object. +fn sync_projected_directory(directory: &Path) -> std::io::Result<()> { + #[cfg(unix)] + std::fs::File::open(directory)?.sync_all()?; + #[cfg(not(unix))] + let _ = directory; + Ok(()) } -#[cfg(all(test, unix))] +#[cfg(test)] mod projected_store_tests { use super::save_projected_change_without_overwrite; - use std::io::BufRead; - use std::os::unix::process::ExitStatusExt; + #[test] + fn projected_publication_is_idempotent_and_refuses_different_bytes() { + let dir = tempfile::tempdir().unwrap(); + // The hash-prefix directory is created by the caller just before + // publication, exactly as the change store lays objects out. + let prefix = dir.path().join("AB"); + std::fs::create_dir_all(&prefix).unwrap(); + let path = prefix.join("object.change"); + 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"); + let leftovers: Vec<_> = std::fs::read_dir(&prefix) + .unwrap() + .map(|entry| entry.unwrap().file_name()) + .filter(|name| name != "object.change") + .collect(); + assert!( + leftovers.is_empty(), + "staging files must not outlive publication: {leftovers:?}" + ); + } + + #[cfg(unix)] #[test] fn staged_projected_change_survives_process_kill_without_partial_publication() { + use std::io::BufRead; + use std::os::unix::process::ExitStatusExt; + 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"); diff --git a/atomic-repository/src/repository/content.rs b/atomic-repository/src/repository/content.rs index d913e7e6..bbf2de30 100644 --- a/atomic-repository/src/repository/content.rs +++ b/atomic-repository/src/repository/content.rs @@ -1083,8 +1083,7 @@ where return Ok(None); } atomic_core::pristine::span_patch_storage::SpanRenderContext::new(txn, store, visibility) - .and_then(|context| context.render(inode)) - .map(Some) + .and_then(|context| context.render_if_visible(inode)) .map_err(|error| RepositoryError::Database(error.to_string())) } @@ -1141,8 +1140,7 @@ where return Ok(None); } self.context()? - .render(inode) - .map(Some) + .render_if_visible(inode) .map_err(|error| RepositoryError::Database(error.to_string())) } @@ -1209,9 +1207,17 @@ where 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()))?; + let context = SpanRenderContext::new(txn, store, visibility) + .map_err(|e| RepositoryError::Database(e.to_string()))?; + if !context + .has_visible_content(inode) + .map_err(|e| RepositoryError::Database(e.to_string()))? + { + return Ok(None); + } + let rendered = context + .render_bounded(inode, max_bytes) + .map_err(|e| RepositoryError::Database(e.to_string()))?; Ok(Some(match rendered { BoundedSpanContent::Complete(bytes) => BoundedContent::Complete(bytes), BoundedSpanContent::Oversized { retained_bytes } => { @@ -1262,7 +1268,28 @@ where } } } - return Ok(false); + use atomic_core::pristine::SPAN_LEGACY_CONTEXT_CAPABILITY; + return match txn + .required_capability_version(SPAN_LEGACY_CONTEXT_CAPABILITY.id()) + .map_err(|e| RepositoryError::Database(e.to_string()))? + { + None => { + if !txn + .span_edits_for_inode(inode) + .map_err(|e| RepositoryError::Database(e.to_string()))? + .is_empty() + { + return Err(RepositoryError::Database( + "legacy span index has no matching repository capability".into(), + )); + } + Ok(false) + } + Some(1) => Ok(true), + Some(_) => Err(RepositoryError::Database( + "unsupported legacy span context capability".into(), + )), + }; }; if !matches!(capability, Some(2) | Some(3)) { return Err(RepositoryError::Database( diff --git a/atomic-repository/src/repository/history.rs b/atomic-repository/src/repository/history.rs index 29b52a5e..ed389b67 100644 --- a/atomic-repository/src/repository/history.rs +++ b/atomic-repository/src/repository/history.rs @@ -9,7 +9,7 @@ use atomic_core::operation::{ }; impl Repository { - fn authoritative_working_copy_view_name(&self) -> Result { + pub(super) fn authoritative_working_copy_view_name(&self) -> Result { let working_copy = self.require_working_copy_id()?; self.desired_view_name(working_copy) } diff --git a/atomic-repository/src/repository/insert.rs b/atomic-repository/src/repository/insert.rs index 93718244..cc838432 100644 --- a/atomic-repository/src/repository/insert.rs +++ b/atomic-repository/src/repository/insert.rs @@ -284,7 +284,7 @@ impl Repository { } } -fn write_recorded_graph_and_spans( +pub(super) fn write_recorded_graph_and_spans( repository: &Repository, txn: &mut atomic_core::pristine::WriteTxn<'_>, view_name: &str, @@ -308,6 +308,12 @@ fn write_recorded_graph_and_spans( } use atomic_core::change::span_patch::SpanOp; use atomic_core::pristine::{span_patch_storage::SpanPatchReadTxn, CrdtTxnT}; + atomic_core::pristine::span_patch_storage::prepare_legacy_files( + txn, + &repository.change_store, + change, + ) + .map_err(|e| RepositoryError::Apply(e.to_string()))?; 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); @@ -334,7 +340,9 @@ fn write_recorded_graph_and_spans( .ok_or_else(|| { RepositoryError::Apply("projected semantic file mapping missing".into()) })?; - if txn.get_crdt_inode_trunk(inode.get())?.is_some() { + if txn.get_crdt_inode_trunk(inode.get())?.is_some() + && patch.version != atomic_core::change::span_patch::LEGACY_SOURCE_VERSION + { return Err(RepositoryError::Apply( "projected semantic edit cannot replace an existing CRDT trunk".into(), )); @@ -2424,6 +2432,7 @@ impl Repository { ) -> Result { let _review_boundary = self.review_evidence_lock()?; let change = self.load_change(hash)?; + self.fence_legacy_span_storage(&change)?; let view_name = options .view .as_deref() @@ -2860,6 +2869,17 @@ impl Repository { mut options: InsertOptions, ) -> Result { let _review_boundary = self.review_evidence_lock()?; + self.fence_legacy_span_storage(&self.load_change(hash)?)?; + #[cfg(feature = "prototype-context-completion")] + if !self + .prototype_projection_for_hashes(&[*hash])? + .effects + .is_empty() + { + return Err(RepositoryError::InvalidOperation { + message: "experimental context closure requires prototype_insert_composed before any membership mutation".into(), + }); + } 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( @@ -2874,7 +2894,7 @@ impl Repository { let trace_insert = std::env::var_os("ATOMIC_TRACE_INSERT").is_some(); let t0 = std::time::Instant::now(); - // Load the target change to get its dependencies + // Load the target change to get its dependencies. let _change = self.load_change(hash)?; // Get the view name diff --git a/atomic-repository/src/repository/materialize.rs b/atomic-repository/src/repository/materialize.rs index 14a03733..b40b42e4 100644 --- a/atomic-repository/src/repository/materialize.rs +++ b/atomic-repository/src/repository/materialize.rs @@ -1577,13 +1577,16 @@ impl Repository { } None => { // Distinguish legacy graph files from a missing span index - // before any materialization operation is prepared. - super::content::retrieve_span_if_present( + // before any materialization operation is prepared. A + // legacy-backed span edit has no typed root, but its bytes + // must still be rendered through the span context below. + has_visible_spans |= super::content::retrieve_span_if_present( &txn, &self.change_store, item.inode, &visibility, - )?; + )? + .is_some(); } } } diff --git a/atomic-repository/src/repository/mod.rs b/atomic-repository/src/repository/mod.rs index f6e2b3ad..598d0c54 100644 --- a/atomic-repository/src/repository/mod.rs +++ b/atomic-repository/src/repository/mod.rs @@ -125,6 +125,8 @@ pub use remote_cache::{ SandboxSlice, SpanBytes, SubmitRejection, Submitted, SubmittedOutcome, SANDBOX_GRAPH_SLICE_SCHEMA, SANDBOX_SKELETON_SCHEMA, }; +#[cfg(feature = "prototype-context-completion")] +pub(crate) mod prototype_context; mod revise; mod sandbox; mod semantic_closure; diff --git a/atomic-repository/src/repository/operation.rs b/atomic-repository/src/repository/operation.rs index 06ef6956..3c2b8b1d 100644 --- a/atomic-repository/src/repository/operation.rs +++ b/atomic-repository/src/repository/operation.rs @@ -970,7 +970,7 @@ impl Repository { .get(&metadata_target) .cloned() .unwrap_or(MetadataValue::Absent); - if is_projected_semantic_capability(&metadata_target) + if is_permanent_span_context_capability(&metadata_target) && matches!(observed, MetadataValue::Sequence(_)) && matches!(expected_new, MetadataValue::Absent) { @@ -1026,7 +1026,7 @@ impl Repository { .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)) + .filter(|transition| !is_permanent_span_context_capability(&transition.target)) .map(|transition| MetadataTransition { target: transition.target.clone(), expected_old: transition.expected_new.clone(), @@ -3520,7 +3520,7 @@ impl Repository { ) { LeaseClassification::Apply => {} LeaseClassification::AlreadyApplied => { - if !is_projected_semantic_capability(&transition.target) { + if !is_permanent_span_context_capability(&transition.target) { metadata.push(MetadataTransition { target: transition.target.clone(), expected_old: transition.expected_new.clone(), @@ -4054,7 +4054,9 @@ fn validate_capability_lease( ), }); } - if is_projected_semantic_capability(target) && requested < supported.minimum_version() { + if is_permanent_span_context_capability(target) + && requested < supported.minimum_version() + { return Err(RepositoryError::InvalidOperation { message: "projected span semantic capability cannot be lowered".into(), }); @@ -4062,7 +4064,7 @@ fn validate_capability_lease( Ok(()) } MetadataValue::Absent => { - if is_projected_semantic_capability(target) { + if is_permanent_span_context_capability(target) { return Err(RepositoryError::InvalidOperation { message: "projected span semantic capability is permanent once installed" .into(), @@ -4085,8 +4087,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 is_permanent_span_context_capability(target: &MetadataTarget) -> bool { + matches!(target, MetadataTarget::Capability { id } + if id == atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY.id() + || id == atomic_core::pristine::SPAN_LEGACY_CONTEXT_CAPABILITY.id()) } fn apply_metadata_value_in_write( diff --git a/atomic-repository/src/repository/prototype_context.rs b/atomic-repository/src/repository/prototype_context.rs new file mode 100644 index 00000000..bd54610c --- /dev/null +++ b/atomic-repository/src/repository/prototype_context.rs @@ -0,0 +1,800 @@ +//! Replayable, sandbox-only context operations. Authority is the immutable +//! GraphOp in selected changes; this projection is only a derived execution plan. +use super::*; +use crate::apply::{InsertOptions, InsertOutcome, InsertStats}; +use crate::context_witness_prototype::{self as witness, Objects}; +use atomic_core::change::prototype_context::{ContextEffectRef, PrototypeContextEffect as Effect}; +use atomic_core::change::VerifiedChange; +use atomic_core::operation::{ + ActorRef, MetadataTarget, MetadataTransition, MetadataValue, OperationKind, OperationScope, + RepoStateRef, ViewStateRef, +}; +use atomic_core::pristine::{CapabilityMutTxnT, CapabilityTxnT, PROTOTYPE_CONTEXT_CAPABILITY}; +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::Arc; + +fn refused(message: impl std::fmt::Display) -> RepositoryError { + RepositoryError::InvalidOperation { + message: format!("prototype context: {message}"), + } +} + +fn supported_line_order_reference(line: &atomic_core::change::LineOps, symbol: u64) -> bool { + use atomic_core::crdt::BranchOp; + match line.operation() { + BranchOp::Reparent { .. } => true, + // The new line is owned by the consumer; only its predecessor needs + // the completed foreign identity. This is line-order context too, not + // an update to foreign content. The witness separately rejects token + // occurrences and validates every predecessor declaration. + BranchOp::Insert { after, .. } => { + line.branch_id().change_id().is_root() + && after.is_some_and(|id| id.change_id().get() == symbol) + } + _ => false, + } +} + +#[cfg(test)] +mod line_order_tests { + use super::*; + use atomic_core::{ + change::LineOps, + crdt::{BranchId, BranchOp}, + }; + + #[test] + fn inserted_line_predecessors_are_order_context_not_foreign_content_updates() { + let foreign = BranchId::new(NodeId::new(15), 2); + let own = BranchId::new(NodeId::ROOT, 3); + assert!(supported_line_order_reference( + &LineOps::new( + own, + BranchOp::Insert { + after: Some(foreign), + content: vec![] + } + ), + 15 + )); + assert!(!supported_line_order_reference( + &LineOps::new( + foreign, + BranchOp::Insert { + after: Some(foreign), + content: vec![] + } + ), + 15 + )); + assert!(!supported_line_order_reference( + &LineOps::new( + foreign, + BranchOp::Delete { + branch: foreign, + content: vec![] + } + ), + 15 + )); + } +} + +#[derive(Debug, Clone)] +pub(crate) struct Projection { + pub(crate) effects: BTreeMap, + claims: BTreeMap<(Hash, u64), BTreeMap>, + cycle: bool, + pub(crate) digest: Hash, +} + +impl Projection { + fn build(objects: &Objects) -> Result { + let mut effects = BTreeMap::new(); + for (hash, change) in objects { + let found: Vec<_> = change + .hunks() + .iter() + .enumerate() + .filter_map(|(ordinal, op)| match op { + GraphOp::PrototypeContext { effect } => Some((ordinal, effect)), + _ => None, + }) + .collect(); + if found.is_empty() { + continue; + } + if found.len() != 1 { + return Err(refused( + "one context effect per experimental patch is supported", + )); + } + let (ordinal, effect) = found[0]; + if !matches!(effect, Effect::DeclareOwnNames { .. }) + && (change.hunks().len() != 1 + || !change.file_ops().is_empty() + || !change.contents.is_empty()) + { + return Err(refused("completion carriers require exactly one context effect and no content/semantic writes")); + } + match effect { + Effect::DeclareOwnNames { owners } => { + use atomic_core::change::semantic_context::{SemanticNameFrame, SemanticOwner}; + let frame = SemanticNameFrame::new( + owners + .iter() + .map(|(symbol, owner)| SemanticOwner { + authored_id: *symbol, + owner: *owner, + }) + .collect(), + ) + .map_err(|e| refused(format!("own-name declaration: {e}")))?; + frame + .validate_for(change.file_ops()) + .map_err(|e| refused(format!("own-name declaration: {e}")))?; + if change.hashed.span.is_some() + || owners.is_empty() + || owners.iter().any(|(_, owner)| { + *owner == *hash + || !change.dependencies().contains(owner) + || !objects.contains_key(owner) + }) + { + return Err(refused("own-name declaration must exactly cover the new patch's foreign names with distinct explicit prerequisites")); + } + } + Effect::Bind { + consumer, + symbol, + provider, + } => { + if !change.dependencies().contains(consumer) + || !change.dependencies().contains(provider) + { + return Err(refused( + "binding must explicitly require its consumer and provider", + )); + } + let proof = + witness::proof(objects, *consumer, *symbol, *provider).map_err(refused)?; + witness::verify_positive_witness(objects, &proof).map_err(refused)?; + if proof["consumer_order_anomalies"] + .as_array() + .is_none_or(|v| !v.is_empty()) + { + return Err(refused( + "completion proves context, not an invalid self-parent order write", + )); + } + // Complete line-order operands, including the predecessor + // of a consumer-owned inserted line. + // Undefined content/token updates cannot be deferred safely. + for ops in objects[consumer].file_ops() { + for line in ops.line_ops() { + let mut container = atomic_core::change::FileOps::edit( + atomic_core::crdt::TrunkId::new(NodeId::ROOT, 0), + String::new(), + ); + container.add_line_op(line.clone()); + if container + .referenced_node_ids() + .iter() + .any(|id| id.get() == *symbol) + && !supported_line_order_reference(line, *symbol) + { + return Err(refused( + "native prototype supports only line-order context completion", + )); + } + } + } + } + Effect::Reverse { observed } => { + if observed.ordinal != 0 || !change.dependencies().contains(&observed.change) { + return Err(refused( + "inverse must require one exact observed context effect", + )); + } + let original = objects + .get(&observed.change) + .ok_or_else(|| refused("missing inverse context"))?; + if original.hunks().len() != 1 + || !matches!( + original.hunks()[0], + GraphOp::PrototypeContext { + effect: Effect::Bind { .. } | Effect::Reverse { .. } + } + ) + { + return Err(refused("inverse target is not a context operation")); + } + } + } + effects.insert( + ContextEffectRef { + change: *hash, + ordinal: ordinal as u32, + }, + effect.clone(), + ); + } + fn active( + id: ContextEffectRef, + effects: &BTreeMap, + visiting: &mut BTreeSet, + ) -> Result { + if !visiting.insert(id) { + return Err(refused("cyclic inverse effect context")); + } + for (candidate, effect) in effects { + if matches!(effect, Effect::Reverse { observed } if *observed == id) + && active(*candidate, effects, visiting)? + { + visiting.remove(&id); + return Ok(false); + } + } + visiting.remove(&id); + Ok(true) + } + let mut claims = BTreeMap::<_, BTreeMap<_, _>>::new(); + for (id, effect) in &effects { + if let Effect::DeclareOwnNames { owners } = effect { + for (symbol, provider) in owners { + claims + .entry((id.change, *symbol)) + .or_default() + .insert(*id, *provider); + } + } + if let Effect::Bind { + consumer, + symbol, + provider, + } = effect + { + let slot = claims.entry((*consumer, *symbol)).or_default(); + if active(*id, &effects, &mut BTreeSet::new())? { + slot.insert(*id, *provider); + } + } + } + let mut edges: BTreeMap> = objects + .iter() + .map(|(h, c)| (*h, c.dependencies().iter().copied().collect())) + .collect(); + for ((consumer, _), values) in &claims { + edges.entry(*consumer).or_default().extend(values.values()); + } + fn cycle( + node: Hash, + edges: &BTreeMap>, + pending: &mut BTreeSet, + done: &mut BTreeSet, + ) -> bool { + if done.contains(&node) { + return false; + } + if !pending.insert(node) { + return true; + } + if edges + .get(&node) + .is_some_and(|next| next.iter().any(|h| cycle(*h, edges, pending, done))) + { + return true; + } + pending.remove(&node); + done.insert(node); + false + } + let mut done = BTreeSet::new(); + let cycle = edges + .keys() + .any(|h| cycle(*h, &edges, &mut BTreeSet::new(), &mut done)); + let rows:Vec<_>=claims.iter().map(|((consumer,symbol),values)|serde_json::json!({"consumer":consumer,"symbol":symbol,"claims":values.iter().collect::>() })).collect(); + let digest = Hash::of(&serde_json::to_vec(&(cycle, rows)).map_err(refused)?); + Ok(Self { + effects, + claims, + cycle, + digest, + }) + } + + pub(crate) fn bindings(&self, consumer: Hash) -> Result, String> { + if self.cycle { + return Err("prototype context: cyclic required context".into()); + } + let mut out = Vec::new(); + for ((patch, symbol), claims) in &self.claims { + if *patch != consumer { + continue; + } + let providers: BTreeSet<_> = claims.values().copied().collect(); + match providers.len() { + 0 => { + return Err(format!( + "prototype context: unresolved name {symbol} after exact reversal" + )) + } + 1 => out.push((*symbol, *providers.first().unwrap())), + _ => { + return Err(format!( + "prototype context: conflicting providers for name {symbol}" + )) + } + } + } + Ok(out) + } + + pub(crate) fn require_complete(&self) -> Result<(), String> { + for (consumer, _) in self.claims.keys() { + self.bindings(*consumer)?; + } + Ok(()) + } + + fn order(&self, objects: &Objects) -> Result, RepositoryError> { + let mut order = Vec::new(); + let mut done = BTreeSet::new(); + let mut pending = BTreeSet::new(); + fn visit( + h: Hash, + p: &Projection, + objects: &Objects, + order: &mut Vec, + done: &mut BTreeSet, + pending: &mut BTreeSet, + ) -> Result<(), RepositoryError> { + if done.contains(&h) { + return Ok(()); + } + if !pending.insert(h) { + return Err(refused("cyclic composed patch prerequisites")); + } + let change = objects + .get(&h) + .ok_or_else(|| refused("missing composed object"))?; + let mut deps: BTreeSet<_> = change.dependencies().iter().copied().collect(); + if !p.cycle { + for ((consumer, _), claims) in &p.claims { + if *consumer == h { + let providers: BTreeSet<_> = claims.values().copied().collect(); + if providers.len() == 1 { + deps.extend(providers); + } + } + } + } + for dep in deps { + visit(dep, p, objects, order, done, pending)?; + } + pending.remove(&h); + done.insert(h); + order.push(h); + Ok(()) + } + for hash in objects.keys() { + visit(*hash, self, objects, &mut order, &mut done, &mut pending)?; + } + Ok(order) + } + + pub(crate) fn report(&self) -> serde_json::Value { + serde_json::json!({"experimental":true,"cycle":self.cycle,"digest":self.digest, + "names":self.claims.iter().map(|((consumer,symbol),claims)| { + let values:BTreeSet<_>=claims.values().copied().collect(); + serde_json::json!({"consumer":consumer,"symbol":symbol,"status":match values.len(){0=>"unbound",1=>"bound",_=>"conflict"},"providers":values,"effects":claims.keys().collect::>()}) + }).collect::>()}) + } + + pub(crate) fn required_effects_for( + &self, + repo: &Repository, + change: &Change, + ) -> Result, RepositoryError> { + fn anchors(change: &Change, paths: Option<&BTreeSet<&str>>) -> BTreeSet<(Hash, u64)> { + let mut anchors = BTreeSet::new(); + for op in change.hunks() { + if paths.is_some_and(|paths| op.path().is_none_or(|path| !paths.contains(path))) { + continue; + } + if let GraphOp::SetAttr { inode, .. } = op { + if let Some(hash) = inode.change { + anchors.insert((hash, inode.pos.get())); + } + } + for atom in op.atoms() { + let inode = match atom { + atomic_core::change::AtomRef::Insertion(i) + | atomic_core::change::AtomRef::Atom( + atomic_core::change::Atom::Insertion(i), + ) => i.inode, + atomic_core::change::AtomRef::EdgeUpdate(e) + | atomic_core::change::AtomRef::Atom( + atomic_core::change::Atom::EdgeUpdate(e), + ) => e.inode, + }; + if let Some(hash) = inode.change { + anchors.insert((hash, inode.pos.get())); + } + } + } + anchors + } + self.require_complete().map_err(refused)?; + let touched = anchors(change, None); + let mut required = BTreeSet::new(); + loop { + let mut roots = change.dependencies().to_vec(); + roots.extend(&required); + let scope = repo.prototype_load_scope(&roots)?; + let before = required.len(); + for ((consumer_hash, symbol), claims) in &self.claims { + let consumer = repo.load_change(consumer_hash)?; + let paths = consumer + .file_ops() + .iter() + .filter(|ops| { + ops.referenced_node_ids() + .iter() + .any(|id| id.get() == *symbol) + }) + .map(|ops| ops.path()) + .collect::>(); + // Native replay executes the entire required patch, even when + // its original author bundled several files. Retain the + // completion context of every required consumer, transitively, + // plus same-inode order context actually used in assembly. + // Other consumers elsewhere in the view remain unrelated. + if scope.contains_key(consumer_hash) + || !anchors(&consumer, Some(&paths)).is_disjoint(&touched) + { + required.extend(claims.keys().map(|effect| effect.change)); + } + } + if required.len() == before { + break; + } + } + Ok(required.into_iter().collect()) + } +} + +impl Repository { + pub(crate) fn prototype_ordered_scope( + &self, + projection: &Projection, + roots: &[Hash], + ) -> Result, RepositoryError> { + projection.order(&self.prototype_load_scope(roots)?) + } + fn prototype_save_validated_context(&self, change: &Change) -> Result { + let mut objects = self.prototype_load_scope(change.dependencies())?; + let mut bytes = Vec::new(); + let hash = change.serialize(&mut bytes).map_err(refused)?; + objects.insert(hash, change.clone()); + Projection::build(&objects)?; + self.save_change_bytes(&hash, &bytes, change) + } + fn prototype_load_scope(&self, roots: &[Hash]) -> Result { + let mut objects = Objects::new(); + let mut pending = roots.to_vec(); + while let Some(hash) = pending.pop() { + if objects.contains_key(&hash) { + continue; + } + if objects.len() > 4096 { + return Err(refused("sandbox context budget exceeded")); + } + let bytes = std::fs::read(self.change_store.change_path(&hash)) + .map_err(|e| refused(format!("missing {hash}: {e}")))?; + let verified = VerifiedChange::from_bytes(&bytes).map_err(refused)?; + if verified.hash() != hash { + return Err(refused("stored object hash mismatch")); + } + let change = verified.change().clone(); + pending.extend(change.dependencies()); + objects.insert(hash, change); + } + Ok(objects) + } + + pub(crate) fn prototype_projection_for_hashes( + &self, + roots: &[Hash], + ) -> Result, RepositoryError> { + Ok(Arc::new(Projection::build( + &self.prototype_load_scope(roots)?, + )?)) + } + + pub(crate) fn prototype_projection_for_txn( + &self, + txn: &T, + view: &str, + additional: &[Hash], + ) -> Result, RepositoryError> { + let state = txn + .get_view(view)? + .ok_or_else(|| refused("missing target view"))?; + let visible = super::effective_projection_closure(txn, &state)?; + let mut hashes = additional.to_vec(); + for id in visible.iter_dependency_first() { + if !id.is_root() { + hashes.push( + txn.get_external(*id)? + .ok_or_else(|| refused("missing visible object"))?, + ); + } + } + self.prototype_projection_for_hashes(&hashes) + } + + /// Create a new experimental completion patch; never edits its consumer. + pub fn prototype_create_completion( + &self, + scope: &[Hash], + consumer: Hash, + symbol: u64, + ) -> Result { + self.prototype_require_draft()?; + let objects = self.prototype_load_scope(scope)?; + let proof = witness::reconstruct(&objects, consumer, symbol).map_err(refused)?; + let provider = serde_json::from_value(proof["provider"].clone()).map_err(refused)?; + let mut change = Change::empty(ChangeHeader::new( + "EXPERIMENTAL SANDBOX: complete a patch context name", + )); + change.hashed.dependencies = vec![consumer, provider]; + change.hashed.dependencies.sort(); + change.hashed.dependencies.dedup(); + change.hashed.hunks.push(GraphOp::PrototypeContext { + effect: Effect::Bind { + consumer, + symbol, + provider, + }, + }); + self.prototype_save_validated_context(&change) + } + + /// Build the exact inverse of an observed experimental context effect. + pub fn prototype_reverse_context(&self, observed: Hash) -> Result { + self.prototype_require_draft()?; + let mut change = Change::empty(ChangeHeader::new( + "EXPERIMENTAL SANDBOX: reverse an exact context effect", + )); + change.hashed.dependencies = vec![observed]; + change.hashed.hunks.push(GraphOp::PrototypeContext { + effect: Effect::Reverse { + observed: ContextEffectRef { + change: observed, + ordinal: 0, + }, + }, + }); + self.prototype_save_validated_context(&change) + } + + fn prototype_require_draft(&self) -> Result { + let name = self.authoritative_working_copy_view_name()?; + if !self.get_view_info(&name)?.scope.is_draft() { + return Err(refused( + "sandbox completion operations require a draft working copy", + )); + } + Ok(name) + } + + /// Inspect the selected ordinary context-operation facts, including conflicts. + pub fn prototype_context_report(&self) -> Result { + let view = self.prototype_require_draft()?; + let txn = self.pristine.read_txn()?; + Ok(self + .prototype_projection_for_txn(&txn, &view, &[])? + .report()) + } + + /// Atomically compose a sandbox patch set with its explicit context effects. + /// No membership is published under a merely prospective completion. + pub fn prototype_insert_composed( + &self, + roots: &[Hash], + ) -> Result { + let view_name = self.prototype_require_draft()?; + let wc = self.require_working_copy_id()?; + let lock = self.try_lock_operation(wc)?; + if let OperationHeadState::Diverged(heads) = + self.consolidate_operation_heads_locked(&lock)? + { + return Err(RepositoryError::OperationHeadsDiverged { + scope: OperationScope::WorkingCopy(wc).to_string(), + heads: heads.iter().map(ToString::to_string).collect(), + }); + } + let (objects, projection, missing, before, after, transitions) = { + let txn = self.pristine.read_txn()?; + let view = txn + .get_view(&view_name)? + .ok_or_else(|| refused("missing sandbox view"))?; + let visible = super::effective_projection_closure(&txn, &view)?; + let mut all = roots.to_vec(); + for id in visible.iter_dependency_first() { + if !id.is_root() { + all.push( + txn.get_external(*id)? + .ok_or_else(|| refused("missing visible hash"))?, + ); + } + } + let objects = self.prototype_load_scope(&all)?; + let projection = Arc::new(Projection::build(&objects)?); + // Until conflict-state projection is implemented, refuse the whole + // composition rather than retaining stale semantic table writes. + projection.require_complete().map_err(refused)?; + let old_projection = self.prototype_projection_for_txn(&txn, &view_name, &[])?; + for (consumer, _) in projection.claims.keys() { + if let Some(id) = txn.get_internal(consumer)? { + if txn.was_change_applied(id)? + && old_projection.bindings(*consumer).ok() + != projection.bindings(*consumer).ok() + { + return Err(refused("cannot reuse ambient semantic writes under a different completed context; use a fresh sandbox")); + } + } + } + let order = projection.order(&objects)?; + let mut missing = Vec::new(); + for hash in order { + if txn + .get_internal(&hash)? + .is_none_or(|id| !visible.contains(id)) + { + missing.push(hash); + } + } + let mut state = view.state; + let mut transitions = Vec::new(); + for (i, hash) in missing.iter().enumerate() { + state = state.next(hash); + transitions.push(MetadataTransition { + target: MetadataTarget::ViewChange { + view: view_name.clone(), + change: *hash, + }, + expected_old: MetadataValue::Absent, + expected_new: MetadataValue::Sequence(view.change_count + i as u64), + }); + } + if objects.values().any(|c| { + c.hunks() + .iter() + .any(|h| matches!(h, GraphOp::PrototypeContext { .. })) + }) && txn + .required_capability_version(PROTOTYPE_CONTEXT_CAPABILITY.id())? + .is_none() + { + transitions.push(MetadataTransition { + target: MetadataTarget::Capability { + id: PROTOTYPE_CONTEXT_CAPABILITY.id().into(), + }, + expected_old: MetadataValue::Absent, + expected_new: MetadataValue::Sequence(1), + }); + } + let old = self.working_copy_record(wc)?; + let mut new = old.clone(); + new.desired_state = state; + let before = RepoStateRef { + view: Some(ViewStateRef { + name: view_name.clone(), + state: view.state, + set_id: None, + }), + working_copy: Some(super::operation::working_copy_state_ref(old)), + git: None, + }; + let after = RepoStateRef { + view: Some(ViewStateRef { + name: view_name.clone(), + state, + set_id: None, + }), + working_copy: Some(super::operation::working_copy_state_ref(new)), + git: None, + }; + (objects, projection, missing, before, after, transitions) + }; + if missing.is_empty() { + let v = self.get_view_info(&view_name)?; + return Ok(InsertOutcome::new( + v.state, + v.change_count, + false, + InsertStats::default(), + )); + } + let op = self.prepare_metadata_operation( + &lock, + OperationKind::Insert, + None, + before, + after, + transitions, + missing.clone(), + ActorRef::System { + name: "prototype-composed-insert".into(), + }, + super::operation::current_operation_timestamp_ms(), + )?; + let result = (|| { + let mut txn = self.pristine.write_txn()?; + let mut stats = InsertStats::default(); + let mut last = None; + if !projection.effects.is_empty() { + txn.put_required_capability(PROTOTYPE_CONTEXT_CAPABILITY)?; + } + let mut options = InsertOptions::default().view(&view_name); + options.prototype_context = Some(projection); + for hash in &missing { + let change = &objects[hash]; + self.ensure_change_allowed_in_view(&txn, &view_name, change)?; + let existing = txn.get_internal(hash)?; + let applied = match existing { + Some(id) => txn.was_change_applied(id)?, + None => false, + }; + let id = txn.register_change(hash)?; + txn.put_change_deps(id, change.dependencies())?; + let tree = if applied { + self.plan_tree_projection( + &mut txn, + id, + *hash, + &Change::empty(ChangeHeader::new("composed reference only")), + &[], + false, + )? + } else { + self.plan_tree_projection(&mut txn, id, *hash, change, &[], false)? + }; + tree.apply_prerequisites(&mut txn)?; + let out = if change.hashed.span.is_some() && !applied { + super::insert::write_recorded_graph_and_spans( + self, &mut txn, &view_name, id, hash, change, &options, + ) + .map_err(|e| refused(format!("while composing {hash}: {e}")))? + } else { + crate::apply::write_change_to_graph( + &mut txn, &view_name, id, hash, change, &options, applied, + ) + .map_err(|e| refused(format!("while composing {hash}: {e}")))? + }; + self.apply_tree_projection(&mut txn, &tree, &view_name, false)?; + stats.merge(out.stats.clone()); + last = Some(out); + } + if txn.kg_writes_enabled()? { + self.project_view_kg(&mut txn)?; + } + txn.commit()?; + let mut out = last.ok_or_else(|| refused("empty composition"))?; + out.stats = stats; + Ok(out) + })(); + match result { + Ok(out) => { + self.apply_operation_metadata_locked(&lock, op.id())?; + self.finalize_operation_verified(&lock, op.id())?; + Ok(out) + } + Err(error) => { + self.abort_prepared_metadata_operation(&lock, &op)?; + Err(error) + } + } + } +} diff --git a/atomic-repository/src/repository/record.rs b/atomic-repository/src/repository/record.rs index ea005ffa..c570e8b7 100644 --- a/atomic-repository/src/repository/record.rs +++ b/atomic-repository/src/repository/record.rs @@ -189,9 +189,6 @@ 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) @@ -609,9 +606,6 @@ 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())?; @@ -1847,7 +1841,11 @@ impl Repository { /// records them (the same fold the foreign record and the staged replay /// use), so positional Delete/Modify ops bind the branches the view's /// lines actually are. - fn view_alive_branches_in_order( + fn view_alive_branches_in_order< + T: atomic_core::pristine::GraphTxnT + + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CrdtTxnT, + >( &self, txn: &T, view_name: &str, @@ -1866,7 +1864,11 @@ impl Repository { /// visible change through the repository's semantic closure memo (see /// `semantic_closure`): every deleted path of one commit, and every /// later commit, reuses the changes folded before. - fn semantic_closure_trunk_state( + fn semantic_closure_trunk_state< + T: atomic_core::pristine::GraphTxnT + + atomic_core::pristine::TreeTxnT + + atomic_core::pristine::CrdtTxnT, + >( &self, txn: &T, view_name: &str, @@ -1874,6 +1876,40 @@ impl Repository { visibility: &atomic_core::pristine::GraphVisibilityClosure, trunk: atomic_core::crdt::TrunkId, ) -> Result { + #[cfg(feature = "prototype-context-completion")] + let (completion, completed_order) = { + let roots = visibility + .iter_dependency_first() + .filter(|id| !id.is_root()) + .map(|id| { + txn.get_external(*id) + .map_err(|e| e.to_string())? + .ok_or_else(|| "visible patch has no hash".to_string()) + }) + .collect::, String>>()?; + let projection = self + .prototype_projection_for_hashes(&roots) + .map_err(|e| e.to_string())?; + projection.require_complete()?; + let hashes = if projection.effects.is_empty() { + roots + } else { + self.prototype_ordered_scope(&projection, &roots) + .map_err(|e| e.to_string())? + }; + let order = hashes + .into_iter() + .map(|hash| { + txn.get_internal(&hash) + .map_err(|e| e.to_string())? + .filter(|id| visibility.contains(id)) + .ok_or_else(|| { + "completed semantic context is outside selected visibility".to_string() + }) + }) + .collect::, String>>()?; + (projection, order) + }; // The memo is a pure cache: a fold that panicked leaves nothing worth // keeping, so a poisoned lock starts over instead of failing every // later record in this process. @@ -1885,12 +1921,17 @@ impl Repository { *memo = Default::default(); memo }); + #[cfg(feature = "prototype-context-completion")] + memo.align_context(completion.digest); memo.align(view_name, excluded, visibility); - for change_id in visibility + #[cfg(not(feature = "prototype-context-completion"))] + let fold_order = visibility .iter_dependency_first() .copied() - .filter(|id| !id.is_root()) - { + .filter(|id| !id.is_root()); + #[cfg(feature = "prototype-context-completion")] + let fold_order = completed_order; + for change_id in fold_order { if memo.is_folded(change_id) { continue; } @@ -1904,6 +1945,16 @@ impl Repository { atomic_core::change::span_patch::localize_file_ops(txn, &change) .map_err(|e| e.to_string())?; } + #[cfg(feature = "prototype-inode-transport")] + if change.hashed.span.is_none() { + change.hashed.file_ops = crate::apply::inode_transport_prototype::localize( + txn, + change_id, + &change, + #[cfg(feature = "prototype-context-completion")] + Some(&completion), + )?; + } memo.fold(change_id, &change); } Ok(memo.trunk(&trunk)) @@ -2204,7 +2255,13 @@ impl Repository { let shared_cached_txn = CachedGraphTxn::new(&shared_txn).map_err(|e| RecordError::Database(e.to_string()))?; - let span_context = if span_recording { + let span_context = if span_recording + || status.entries().iter().any(|entry| { + matches!( + entry.status(), + FileStatus::Modified | FileStatus::Conflicted + ) + }) { Some( atomic_core::pristine::span_patch_storage::SpanRenderContext::new( &shared_cached_txn, @@ -3232,7 +3289,7 @@ 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 span_state = match &span_context { + let mut 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())), @@ -3245,6 +3302,12 @@ impl Repository { Ok(Some(_)) => return ModifiedResult::Fatal(RecordError::Database(format!("span baseline changed while recording '{path}'"))), Err(error) => return ModifiedResult::Fatal(RecordError::Database(error.to_string())), } + if let Some(context)=&span_context { + span_state=match context.legacy_record_state(file_inode) { + Ok(state)=>state, + 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) @@ -3277,27 +3340,58 @@ impl Repository { // and a record indexing its old lines by the global alive rows // bound its deletes to the wrong branches, which the next Git // commit's staged replay then refused. - let existing_trunk_id = match self.canonical_semantic_trunk(&shared_txn, path, file_inode) { - Ok(trunk) => trunk, - Err(error) => return ModifiedResult::Error(path.clone(), error), - }; - let existing_branches = match existing_trunk_id { - Some(trunk_id) => match self.view_alive_branches_in_order( - &shared_txn, - &effective_view, - &shared_graph_visibility, - trunk_id, - ) { - Ok(alive) => alive, - Err(error) => { - return ModifiedResult::Error( - path.clone(), - format!("semantic state of '{path}' on the view: {error}"), - ) + let semantic: Result<_,String> = if span_state.as_ref().is_some_and(|state|state.legacy_graph || state.projected_semantics) { + Ok((None,Vec::new())) + } else { (|| { + let trunk=self.canonical_semantic_trunk(&shared_txn,path,file_inode)?; + let branches=match trunk { + Some(trunk)=>self.view_alive_branches_in_order(&shared_txn,&effective_view,&shared_graph_visibility,trunk)?, + None=>Vec::new(), + }; + Ok((trunk,branches)) + })() }; + let (mut existing_trunk_id,mut existing_branches)=match semantic { + Ok(state)=>state, + // Span recording cannot yet express a name resolution, so a + // path being resolved stays on the graph route below. + Err(error) if span_state.is_none() && resolution.is_none()=>{ + let Some(context)=&span_context else{return ModifiedResult::Error(path.clone(),error);}; + match context.legacy_record_state(file_inode) { + Ok(Some(state))=>{span_state=Some(state);(None,Vec::new())}, + Ok(None)=>return ModifiedResult::Error(path.clone(),error), + Err(proof)=>return ModifiedResult::Error(path.clone(),format!("{error}; legacy range proof: {proof}")), } }, - None => Vec::new(), + // The checked span baseline is authoritative. Retain old + // numeric metadata, but do not invent a historical owner + // or let an unusable CRDT cache prevent a range edit. + Err(_) if span_state.is_some()=>(None,Vec::new()), + Err(error)=>return ModifiedResult::Error(path.clone(),error), + }; + + // A syntactically valid legacy fold can still be bound to the + // wrong receiver-local trunk. Never derive new operations from + // that cache when it disagrees with the actual byte graph. + let unportable_trunk=match &span_context { + Some(context)=>match context.has_unportable_legacy_trunk(file_inode) {Ok(value)=>value,Err(error)=>return ModifiedResult::Error(path.clone(),error.to_string())}, + None=>false, }; + let legacy_mismatch=unportable_trunk + || (!existing_branches.is_empty() && existing_branches.len()!=atomic_core::diff::Line::from_bytes(&old_content).len()); + // The same holds here: moving a path that is being resolved onto + // the span route would only reach the refusal below, so which + // claimant won the name would decide whether the record succeeds. + if span_state.is_none() && resolution.is_none() && !had_fork_structure && legacy_mismatch { + if let Some(context)=&span_context { + match context.legacy_record_state(file_inode) { + Ok(Some(state)) if state.content==old_content=>{ + span_state=Some(state);existing_trunk_id=None;existing_branches.clear(); + }, + Ok(_)=>return ModifiedResult::Error(path.clone(),"legacy semantic cache has no matching authored baseline".into()), + Err(error)=>return ModifiedResult::Error(path.clone(),error.to_string()), + } + } + } // Reuse the CRDT-materialized baseline only when every existing // branch belongs to a change visible on this view. That keeps @@ -3695,6 +3789,17 @@ impl Repository { }) .map(|ops| (ops.path().to_string(), ops.trunk_id())) .collect(); + let projected_paths: std::collections::HashSet = change + .file_ops() + .iter() + .filter(|ops| { + matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ) + }) + .map(|ops| ops.path().to_string()) + .collect(); for pending in pending_attributes { let target = match pending.position { Some(position) => { @@ -3729,9 +3834,17 @@ impl Repository { value: pending.value, }); - let trunk = pending - .trunk - .or_else(|| semantic_trunks.get(&pending.path).copied()); + // A projected span edit is the only semantic operation its path may + // carry. A legacy file still has a CRDT trunk, but the edit does + // not update it, so the attribute is recorded on the graph inode + // alone, exactly as for a file that never had a trunk. + let trunk = if projected_paths.contains(pending.path.as_str()) { + None + } else { + pending + .trunk + .or_else(|| semantic_trunks.get(&pending.path).copied()) + }; let Some(trunk) = trunk else { // Imported graph-only files have no semantic identity to // update. SetAttr above targets the authoritative graph inode. @@ -3768,6 +3881,66 @@ impl Repository { crate::record::merge_move_evidence(&mut change, &move_evidence) .map_err(|error| RecordError::ChangeStore(error.to_string()))?; } + #[cfg(feature = "prototype-context-completion")] + if !span_recording { + use atomic_core::pristine::{CapabilityTxnT, PROTOTYPE_CONTEXT_CAPABILITY}; + // Sandbox-only compatibility experiment; product integration must + // preserve the existing compact span recording path. + if shared_txn + .required_capability_version(PROTOTYPE_CONTEXT_CAPABILITY.id()) + .map_err(|e| RecordError::Database(e.to_string()))? + .is_some() + { + let ids: std::collections::BTreeSet<_> = change + .file_ops() + .iter() + .flat_map(|ops| ops.referenced_node_ids()) + .collect(); + let mut owners = Vec::new(); + for id in ids { + if !shared_graph_visibility.contains(id) { + return Err(RecordError::Database( + "semantic owner is outside recording context".into(), + )); + } + let owner = shared_txn + .get_external(id) + .map_err(|e| RecordError::Database(e.to_string()))? + .ok_or_else(|| { + RecordError::Database("semantic owner has no hash".into()) + })?; + owners.push((id.get(), owner)); + change.hashed.dependencies.push(owner); + } + if !owners.is_empty() { + change.hashed.hunks.push(GraphOp::PrototypeContext { + effect: atomic_core::change::prototype_context::PrototypeContextEffect::DeclareOwnNames { owners }, + }); + change.hashed.dependencies.sort(); + change.hashed.dependencies.dedup(); + } + } + } + #[cfg(feature = "prototype-context-completion")] + { + let roots = shared_graph_visibility + .iter_dependency_first() + .filter(|id| !id.is_root()) + .map(|id| { + shared_txn + .get_external(*id) + .map_err(|e| RecordError::Database(e.to_string()))? + .ok_or_else(|| RecordError::Database("visible context has no hash".into())) + }) + .collect::, RecordError>>()?; + let completion = self.prototype_projection_for_hashes(&roots)?; + change + .hashed + .dependencies + .extend(completion.required_effects_for(self, &change)?); + change.hashed.dependencies.sort(); + change.hashed.dependencies.dedup(); + } // Application metadata (e.g. the CB-7B adoption attribution marker) // is part of the hashed change content: it is set before // serialization so the marker is content-addressed with the change @@ -3776,7 +3949,7 @@ impl Repository { if !application_metadata.is_empty() { change.hashed.metadata = application_metadata.to_vec(); } - if span_recording { + if span_recording || change.hashed.span.is_some() { 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 @@ -3812,6 +3985,31 @@ impl Repository { atomic_core::change::span_patch::SpanPatch::new(Vec::new(), Vec::new()) }); patch.semantic_owners = owners; + for op in &patch.ops { + if let atomic_core::change::span_patch::SpanOp::Edit { file, .. } = op { + let origin = self.load_change(&file.owner)?; + let root_path = origin.hashed.span.as_ref().and_then(|p| { + p.ops.iter().find_map(|op| match op { + atomic_core::change::span_patch::SpanOp::Root { + ordinal, path, .. + } if *ordinal == file.ordinal => Some(path), + _ => None, + }) + }); + let projected=change.hashed.file_ops.iter().any(|ops|matches!(ops.trunk_op(),Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit {file: bound,..}) if bound==file)); + if root_path.is_none() + || (projected + && root_path.is_some_and(|path| { + origin + .file_ops() + .iter() + .any(|ops| ops.path() == path && ops.is_create()) + })) + { + patch.version = atomic_core::change::span_patch::LEGACY_SOURCE_VERSION; + } + } + } } lifecycle.verify_promotion_content(&change, self)?; change = lifecycle.classify(change)?; @@ -3995,6 +4193,36 @@ impl Repository { } let mut transitions = Vec::new(); let mut operation_changes = vec![computed_hash]; + if outcome.change().hashed.span.as_ref().is_some_and(|p| { + p.version == atomic_core::change::span_patch::LEGACY_SOURCE_VERSION + }) { + use atomic_core::pristine::{ + CapabilityTxnT, SPAN_LEGACY_CONTEXT_CAPABILITY, SPAN_RANGE_CAPABILITY, + }; + for cap in [SPAN_RANGE_CAPABILITY, SPAN_LEGACY_CONTEXT_CAPABILITY] { + let old = txn + .required_capability_version(cap.id()) + .map_err(|e| RecordError::Database(e.to_string()))?; + if old != Some(cap.minimum_version()) { + if old.is_some_and(|v| v > cap.minimum_version()) { + return Err(RecordError::Database( + "unsupported span capability".into(), + )); + } + transitions.push(MetadataTransition { + target: MetadataTarget::Capability { + id: cap.id().into(), + }, + expected_old: old + .map(|v| MetadataValue::Sequence(u64::from(v))) + .unwrap_or(MetadataValue::Absent), + expected_new: MetadataValue::Sequence(u64::from( + cap.minimum_version(), + )), + }); + } + } + } if outcome.change().file_ops().iter().any(|ops| { matches!( ops.trunk_op(), @@ -4209,7 +4437,16 @@ impl Repository { // for the file to be recognized as tracked with graph content. let mut apply_error = None; if options.get_apply_after_record() && outcome.was_saved() { - let apply_opts = InsertOptions::default().view(&target_view); + #[allow(unused_mut)] + let mut apply_opts = InsertOptions::default().view(&target_view); + #[cfg(feature = "prototype-context-completion")] + { + apply_opts.prototype_context = Some(self.prototype_projection_for_txn( + &txn, + &target_view, + &[*outcome.hash()], + )?); + } let apply_t0 = std::time::Instant::now(); #[cfg(test)] let inject_apply_failure = RECORD_APPLY_FAULT.with(|fault| fault.replace(false)); diff --git a/atomic-repository/src/repository/remote_cache.rs b/atomic-repository/src/repository/remote_cache.rs index 4baad67a..80c8af2a 100644 --- a/atomic-repository/src/repository/remote_cache.rs +++ b/atomic-repository/src/repository/remote_cache.rs @@ -665,11 +665,6 @@ impl Repository { 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(), diff --git a/atomic-repository/src/repository/semantic_closure.rs b/atomic-repository/src/repository/semantic_closure.rs index 236a2492..9e56cc3e 100644 --- a/atomic-repository/src/repository/semantic_closure.rs +++ b/atomic-repository/src/repository/semantic_closure.rs @@ -44,6 +44,8 @@ pub(super) struct TrunkBranchState { #[derive(Debug, Default)] pub(super) struct SemanticClosureMemo { closures: Vec, + #[cfg(feature = "prototype-context-completion")] + context_digest: Option, } /// The fold of one `(view, exclusions)` closure. @@ -89,6 +91,13 @@ impl MemoKey { } impl SemanticClosureMemo { + #[cfg(feature = "prototype-context-completion")] + pub(super) fn align_context(&mut self, digest: Hash) { + if self.context_digest != Some(digest) { + self.closures.clear(); + self.context_digest = Some(digest); + } + } /// Make the closure of `view` under `excluded` the current one: its /// folded prefix is kept when every folded change is still visible, /// otherwise it starts over. The closures aligned least recently are @@ -203,3 +212,24 @@ fn resolve_branch(change_id: NodeId, raw: BranchId) -> BranchId { raw } } + +#[cfg(all(test, feature = "prototype-context-completion"))] +mod context_tests { + use super::*; + + #[test] + fn changed_completion_digest_discards_previously_successful_folds() { + let a = Hash::of(b"bound"); + let b = Hash::of(b"exact reversal or removal"); + let mut memo = SemanticClosureMemo::default(); + memo.align_context(a); + let mut closure = FoldedClosure::new(MemoKey::new("sandbox", &[])); + closure.folded.insert(NodeId::new(1)); + memo.closures.push(closure); + memo.align_context(a); + assert!(memo.is_folded(NodeId::new(1))); + memo.align_context(b); + assert!(!memo.is_folded(NodeId::new(1))); + assert_eq!(memo.closure_count(), 0); + } +} diff --git a/atomic-repository/src/repository/tests/legacy_history_span_insert_tests.rs b/atomic-repository/src/repository/tests/legacy_history_span_insert_tests.rs index 2c715ffb..388bfd60 100644 --- a/atomic-repository/src/repository/tests/legacy_history_span_insert_tests.rs +++ b/atomic-repository/src/repository/tests/legacy_history_span_insert_tests.rs @@ -175,3 +175,59 @@ fn span_insert_still_refuses_a_dependency_stored_under_the_wrong_hash() { .insert_change_rec(&span_edit, InsertOptions::default()) .is_err()); } + +/// A span edit of a file from pre-0.20 history may land together with a mode +/// change of that file. The file still has a CRDT trunk, but the projected +/// edit must stay the path's only semantic operation, with the mode recorded +/// on the graph inode. Windows always takes this path: it observes a writable +/// file as 0o666, which differs from the default mode of a file whose history +/// carries no mode attribute. +#[cfg(unix)] +#[test] +fn span_edit_of_pre_0_20_file_records_together_with_a_mode_change() { + use atomic_core::change::{GraphOp, InodeAttr}; + use std::os::unix::fs::PermissionsExt; + + let (sender_dir, sender, legacy) = repo_with_legacy_history(); + enable_spans(&sender); + let path = sender_dir.path().join("a.txt"); + std::fs::write(&path, EDITED_CONTENT).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + let span_edit = record_all(&sender, "span edit and mode change"); + + let change = sender.load_change(&span_edit).unwrap(); + assert!( + change.hashed.span.is_some(), + "the edit must be a span change" + ); + assert!(change.dependencies().contains(&legacy)); + assert_eq!( + change + .file_ops() + .iter() + .filter(|ops| ops.path() == "a.txt") + .count(), + 1, + "the projected edit is the path's only semantic operation" + ); + assert!( + change.hunks().iter().any(|hunk| matches!( + hunk, + GraphOp::SetAttr { + value: InodeAttr::Mode(0o755), + .. + } + )), + "the mode change is recorded on the graph inode" + ); + assert_eq!(content(&sender, "dev"), EDITED_CONTENT); + + let (_receiver_dir, receiver, _) = repo_with_legacy_history(); + receiver + .import_change_bytes(&span_edit, &span_edit_bytes(&sender, &span_edit)) + .unwrap(); + receiver + .insert_change_rec(&span_edit, InsertOptions::default()) + .unwrap(); + assert_eq!(content(&receiver, "dev"), EDITED_CONTENT); +} diff --git a/atomic-repository/src/repository/tests/record_duplication_tests.rs b/atomic-repository/src/repository/tests/record_duplication_tests.rs index 1b80e54d..54ba33cf 100644 --- a/atomic-repository/src/repository/tests/record_duplication_tests.rs +++ b/atomic-repository/src/repository/tests/record_duplication_tests.rs @@ -548,3 +548,70 @@ fn test_emptying_file_after_orphan_view_merge_removes_every_copy() { leftover_bytes ); } + +/// `test_emptying_file_after_orphan_view_merge_removes_every_copy` with the +/// working file at the mode Windows reports for every writable file (0o666). +/// +/// That mode differs from the default of a path with no visible mode +/// attribute, so the orphan view's change also records a mode for the file +/// and depends on the change that first added it. Merging then leaves two +/// claimants for the name, and which one wins follows the change hashes, so +/// it varies between runs. Emptying the file has to record whichever wins. +/// The scenario is repeated so that both outcomes are exercised. +#[cfg(unix)] +#[test] +fn test_emptying_file_after_orphan_view_merge_with_windows_file_mode() { + use crate::apply::CrossViewInsertOptions; + use std::os::unix::fs::PermissionsExt; + + fn write(file: &std::path::Path, bytes: &[u8]) { + std::fs::write(file, bytes).unwrap(); + std::fs::set_permissions(file, std::fs::Permissions::from_mode(0o666)).unwrap(); + } + + for round in 0..8 { + let (temp_dir, mut repo) = create_temp_repo(); + let file = temp_dir.path().join("main.go"); + + let initial = build_go_like_source(80); + write(&file, initial.as_bytes()); + repo.add("main.go", TrackingOptions::default()).unwrap(); + record_all(&repo, "Add main.go"); + + create_parentless_shared_view(&repo, "orphan-xyz"); + repo.set_current_view("orphan-xyz").unwrap(); + let edited = initial.replacen("return 40\n", "return 4000\n", 1); + write(&file, edited.as_bytes()); + repo.record( + ChangeHeader::new("orphan edit"), + RecordOptions::new() + .with_all(true) + .view("orphan-xyz") + .apply_after_record(true) + .save_to_store(true), + ) + .unwrap(); + repo.set_current_view("dev").unwrap(); + + repo.insert_from_view(CrossViewInsertOptions::new("orphan-xyz", "dev")) + .unwrap(); + repo.materialize().unwrap(); + + write(&file, b""); + repo.record( + ChangeHeader::new("empty main.go"), + RecordOptions::new() + .with_all(true) + .apply_after_record(true) + .save_to_store(true), + ) + .unwrap_or_else(|error| panic!("round {round}: emptying the file must record: {error}")); + + let content = repo.get_file_content_on_view("main.go", "dev").unwrap(); + assert_eq!( + content.as_ref().map(|c| c.len()).unwrap_or(0), + 0, + "round {round}: a duplicate copy from the orphan-view merge survived" + ); + } +} diff --git a/atomic-repository/src/triage/report.rs b/atomic-repository/src/triage/report.rs index 5d03515e..62ed7452 100644 --- a/atomic-repository/src/triage/report.rs +++ b/atomic-repository/src/triage/report.rs @@ -1203,6 +1203,8 @@ pub fn count_hunk_atoms(hunks: &[GraphOp]) -> (usize, usize) { | GraphOp::ResurrectZombies { .. } | GraphOp::DelRoot { .. } => edges += 1, GraphOp::SetAttr { .. } => {} + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { .. } => {} } } (vertices, edges) @@ -1273,6 +1275,8 @@ fn hunk_symbol_and_path(graph_op: &GraphOp) -> (&'static str, String) { GraphOp::AddRoot { .. } => ("◉", "(root)".to_string()), GraphOp::DelRoot { .. } => ("⊘", "(root)".to_string()), GraphOp::SetAttr { path, .. } => ("@", path.clone()), + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { .. } => ("@", "(experimental context)".into()), } } @@ -1302,6 +1306,8 @@ fn hunk_atom_info(graph_op: &GraphOp) -> String { GraphOp::AddRoot { .. } => "(+1 span: root)".to_string(), GraphOp::DelRoot { .. } => "(~edges: delete root)".to_string(), GraphOp::SetAttr { value, .. } => format!("(attribute: {value:?})"), + #[cfg(feature = "prototype-context-completion")] + GraphOp::PrototypeContext { effect } => format!("(experimental context: {effect:?})"), } } diff --git a/atomic-repository/tests/legacy_span_context.rs b/atomic-repository/tests/legacy_span_context.rs new file mode 100644 index 00000000..f4bef582 --- /dev/null +++ b/atomic-repository/tests/legacy_span_context.rs @@ -0,0 +1,358 @@ +//! Forward legacy edits use verified original ranges, never old numeric CRDT +//! identities. All corruption and capability experiments are disposable. +use atomic_core::change::span_patch::SpanOp; +use atomic_core::change::{Change, ChangeHeader}; +use atomic_core::pristine::{CapabilityMutTxnT, CapabilityTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; +use atomic_repository::{RecordOptions, Repository}; + +fn record(repo: &Repository) -> atomic_core::Hash { + *repo + .record( + repo.require_working_copy_id().unwrap(), + ChangeHeader::new("fixture"), + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap() + .hash() +} + +fn fixture(initial: &[u8]) -> (tempfile::TempDir, Repository, atomic_core::Hash) { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + std::fs::write(dir.path().join("f"), initial).unwrap(); + repo.add( + repo.require_working_copy_id().unwrap(), + "f", + Default::default(), + ) + .unwrap(); + let root = record(&repo); + assert!(repo.load_change(&root).unwrap().hashed.span.is_none()); + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + (dir, repo, root) +} + +#[test] +fn empty_legacy_file_and_repeated_projected_edits_replay_after_reopen() { + let (dir, repo, root) = fixture(b""); + let mut hashes = vec![root]; + for content in [b"first\n".as_slice(), b"second\n", b"", b"again\n"] { + std::fs::write(dir.path().join("f"), content).unwrap(); + hashes.push(record(&repo)); + assert_eq!(repo.get_file_content("f").unwrap().unwrap(), content); + } + let receiver_dir = tempfile::tempdir().unwrap(); + let receiver = Repository::init(receiver_dir.path()).unwrap(); + for hash in &hashes { + let bytes = std::fs::read(repo.change_store().change_path(hash)).unwrap(); + receiver.import_change_bytes(hash, &bytes).unwrap(); + } + receiver + .insert_change_rec(hashes.last().unwrap(), Default::default()) + .unwrap(); + drop(receiver); + assert_eq!( + Repository::open(receiver_dir.path()) + .unwrap() + .get_file_content("f") + .unwrap() + .unwrap(), + b"again\n" + ); +} + +#[test] +fn legacy_projected_binary_edits_and_text_transitions_replay() { + let (dir, source, root) = fixture(b"before\0\xff"); + let mut changes = vec![root]; + for bytes in [ + b"after\0\xfe".as_slice(), + b"plain text\n", + b"binary again\0\xff", + ] { + std::fs::write(dir.path().join("f"), bytes).unwrap(); + let edit = record(&source); + let change = source.load_change(&edit).unwrap(); + assert!(change.file_ops().iter().any(|ops| matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ))); + assert_eq!(source.get_file_content("f").unwrap().unwrap(), bytes); + changes.push(edit); + + // Check each transition independently from immutable objects, without + // inheriting the author's CRDT caches or filesystem state. + let receiver_dir = tempfile::tempdir().unwrap(); + let receiver = Repository::init(receiver_dir.path()).unwrap(); + for hash in &changes { + let original = std::fs::read(source.change_store().change_path(hash)).unwrap(); + receiver.import_change_bytes(hash, &original).unwrap(); + } + receiver + .insert_change_rec(&edit, Default::default()) + .unwrap(); + assert_eq!(receiver.get_file_content("f").unwrap().unwrap(), bytes); + receiver + .materialize(receiver.require_working_copy_id().unwrap()) + .unwrap(); + assert_eq!(std::fs::read(receiver_dir.path().join("f")).unwrap(), bytes); + } +} + +#[test] +fn materialize_legacy_span_edits_without_a_typed_root() { + let (dir, source, root) = fixture(b"before\n"); + std::fs::write(dir.path().join("f"), b"after\n").unwrap(); + let edit = record(&source); + for sequential in [false, true] { + let receiver_dir = tempfile::tempdir().unwrap(); + let receiver = Repository::init(receiver_dir.path()).unwrap(); + for hash in [root, edit] { + let bytes = std::fs::read(source.change_store().change_path(&hash)).unwrap(); + receiver.import_change_bytes(&hash, &bytes).unwrap(); + } + receiver + .insert_change_rec(&edit, Default::default()) + .unwrap(); + assert_eq!(receiver.get_file_content("f").unwrap().unwrap(), b"after\n"); + let wc = receiver.require_working_copy_id().unwrap(); + if sequential { + receiver.materialize_sequential(wc).unwrap(); + } else { + receiver.materialize(wc).unwrap(); + } + assert_eq!( + std::fs::read(receiver_dir.path().join("f")).unwrap(), + b"after\n" + ); + assert_eq!(receiver.materialize(wc).unwrap().files_written, 0); + } +} + +#[test] +fn malformed_legacy_context_refuses_before_publishing_dependencies() { + let (dir, source, root) = fixture(b"one\ntwo\n"); + std::fs::write(dir.path().join("f"), b"ONE\ntwo\n").unwrap(); + let candidate = source.load_change(&record(&source)).unwrap(); + for mode in 0..5 { + let mut bad: Change = candidate.clone(); + bad.hashed.header = ChangeHeader::new(format!("invalid context {mode}")); + if mode == 0 { + for op in &mut bad.hashed.span.as_mut().unwrap().ops { + if let SpanOp::Edit { file, .. } = op { + file.ordinal = 999; + } + } + } else if mode == 1 { + if let SpanOp::Edit { target, .. } = &mut bad.hashed.span.as_mut().unwrap().ops[0] { + target.owner = atomic_core::Hash::of(b"not an authored provider"); + } + } else if mode == 2 { + bad.hashed.span.as_mut().unwrap().version = 2; + } else if mode == 3 { + bad.hashed.file_ops.clear(); + } else { + let ops = &bad.hashed.file_ops[0]; + let Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { file, .. }) = ops.trunk_op() + else { + panic!("fixture must use projected semantics"); + }; + bad.hashed.file_ops[0] = atomic_core::change::FileOps::projected_span_edit( + ops.path().to_owned(), + file.clone(), + atomic_core::change::span_patch::SpanProjectionId(atomic_core::Hash::of( + b"wrong dependency baseline", + )), + ); + } + let receiver_dir = tempfile::tempdir().unwrap(); + let receiver = Repository::init(receiver_dir.path()).unwrap(); + let bytes = std::fs::read(source.change_store().change_path(&root)).unwrap(); + receiver.import_change_bytes(&root, &bytes).unwrap(); + let before = receiver.view_set_id(receiver.current_view()).unwrap(); + match receiver.save_change(&bad) { + Ok(hash) => assert!( + receiver + .insert_change_rec(&hash, Default::default()) + .is_err(), + "accepted mode {mode}" + ), + Err(error) => assert!( + error.to_string().contains("capability"), + "unexpected refusal: {error}" + ), + } + assert_eq!( + receiver.view_set_id(receiver.current_view()).unwrap(), + before + ); + } +} + +#[test] +fn legacy_reader_fence_is_present_after_projected_record_and_reopen() { + let (dir, repo, _) = fixture(b"before\n"); + std::fs::write(dir.path().join("f"), b"after\n").unwrap(); + let hash = record(&repo); + let patch = repo.load_change(&hash).unwrap(); + assert_eq!( + patch.hashed.span.unwrap().version, + atomic_core::change::span_patch::LEGACY_SOURCE_VERSION + ); + drop(repo); + let mut repo = Repository::open(dir.path()).unwrap(); + let txn = repo.pristine().read_txn().unwrap(); + for capability in [ + atomic_core::pristine::SPAN_LEGACY_CONTEXT_CAPABILITY, + atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY, + ] { + assert_eq!( + txn.required_capability_version(capability.id()).unwrap(), + Some(1) + ); + } + assert!(matches!( + atomic_core::output::crdt::output_file_via_crdt(&txn, repo.change_store(), "f"), + Err(atomic_core::output::crdt::CrdtOutputError::SpanContextRequired(_)) + )); + drop(txn); + repo.undo_operation(repo.require_working_copy_id().unwrap(), None) + .unwrap(); + assert_eq!(repo.get_file_content("f").unwrap().unwrap(), b"before\n"); + assert_eq!(std::fs::read(dir.path().join("f")).unwrap(), b"after\n"); + let txn = repo.pristine().read_txn().unwrap(); + assert_eq!( + txn.required_capability_version(atomic_core::pristine::SPAN_LEGACY_CONTEXT_CAPABILITY.id()) + .unwrap(), + Some(1) + ); +} + +#[test] +fn legacy_projection_is_view_scoped_and_disjoint_edits_commute() { + use atomic_repository::apply::CrossViewInsertOptions; + let (dir, mut repo, _) = fixture(b"alpha\nbeta\ngamma\n"); + let wc = repo.require_working_copy_id().unwrap(); + repo.create_draft_view("left", "dev").unwrap(); + repo.create_draft_view("right", "dev").unwrap(); + repo.switch_view(wc, "left").unwrap(); + std::fs::write(dir.path().join("f"), b"ALPHA\nbeta\ngamma\n").unwrap(); + let left = record(&repo); + repo.switch_view(wc, "right").unwrap(); + assert_eq!( + repo.get_file_content("f").unwrap().unwrap(), + b"alpha\nbeta\ngamma\n" + ); + std::fs::write(dir.path().join("f"), b"alpha\nbeta\nGAMMA\n").unwrap(); + let right = record(&repo); + repo.insert_from_view(CrossViewInsertOptions::new("left", "right").only_changes(vec![left])) + .unwrap(); + assert_eq!( + repo.get_file_content_on_view("f", "left").unwrap().unwrap(), + b"ALPHA\nbeta\ngamma\n" + ); + assert_eq!( + repo.get_file_content_on_view("f", "dev").unwrap().unwrap(), + b"alpha\nbeta\ngamma\n" + ); + repo.insert_from_view(CrossViewInsertOptions::new("right", "left").only_changes(vec![right])) + .unwrap(); + assert_eq!( + repo.view_set_id("left").unwrap(), + repo.view_set_id("right").unwrap() + ); + for view in ["left", "right"] { + assert_eq!( + repo.get_file_content_on_view("f", view).unwrap().unwrap(), + b"ALPHA\nbeta\nGAMMA\n" + ); + } + drop(repo); + let repo = Repository::open(dir.path()).unwrap(); + assert_eq!( + repo.get_file_content_on_view("f", "dev").unwrap().unwrap(), + b"alpha\nbeta\ngamma\n" + ); +} + +#[test] +fn missing_legacy_indexes_or_capabilities_never_return_stale_graph_bytes() { + use redb::ReadableMultimapTable; + for mode in 0..3 { + let (dir, repo, _) = fixture(b"before\n"); + std::fs::write(dir.path().join("f"), b"after\n").unwrap(); + record(&repo); + // Deliberate corruption of this isolated fixture only. Supported + // operations are not allowed to remove these immutable-object fences. + let db = repo.pristine().shared_database().unwrap(); + let txn = db.begin_write().unwrap(); + if mode == 0 { + let mut edits = txn + .open_multimap_table(redb::MultimapTableDefinition::<&[u8; 32], &[u8; 40]>::new( + "span_patch_edits_v1", + )) + .unwrap(); + let keys = edits + .iter() + .unwrap() + .map(|row| *row.unwrap().0.value()) + .collect::>(); + for key in keys { + drop(edits.remove_all(&key).unwrap()); + } + } else { + let capability = if mode == 1 { + atomic_core::pristine::SPAN_LEGACY_CONTEXT_CAPABILITY + } else { + atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY + }; + txn.open_table(atomic_core::pristine::tables::PRISTINE_META) + .unwrap() + .remove(format!("required-capability/{}", capability.id()).as_str()) + .unwrap(); + } + txn.commit().unwrap(); + assert!( + repo.get_file_content("f").is_err(), + "returned stale bytes after corruption {mode}" + ); + assert!( + repo.materialize(repo.require_working_copy_id().unwrap()) + .is_err(), + "materialized stale bytes after corruption {mode}" + ); + assert_eq!(std::fs::read(dir.path().join("f")).unwrap(), b"after\n"); + } +} + +#[test] +fn overlapping_legacy_span_edits_refuse_without_publishing_membership() { + use atomic_repository::apply::CrossViewInsertOptions; + let (dir, mut repo, _) = fixture(b"before\n"); + let wc = repo.require_working_copy_id().unwrap(); + repo.create_draft_view("left", "dev").unwrap(); + repo.create_draft_view("right", "dev").unwrap(); + repo.switch_view(wc, "left").unwrap(); + std::fs::write(dir.path().join("f"), b"LEFT\n").unwrap(); + let left = record(&repo); + repo.switch_view(wc, "right").unwrap(); + std::fs::write(dir.path().join("f"), b"RIGHT\n").unwrap(); + record(&repo); + let before = repo.view_set_id("right").unwrap(); + assert!(repo + .insert_from_view(CrossViewInsertOptions::new("left", "right").only_changes(vec![left])) + .is_err()); + assert_eq!(repo.view_set_id("right").unwrap(), before); + assert_eq!( + repo.get_file_content_on_view("f", "right") + .unwrap() + .unwrap(), + b"RIGHT\n" + ); + assert_eq!(std::fs::read(dir.path().join("f")).unwrap(), b"RIGHT\n"); +} diff --git a/atomic-repository/tests/replayable_context_prototype.rs b/atomic-repository/tests/replayable_context_prototype.rs new file mode 100644 index 00000000..b8603022 --- /dev/null +++ b/atomic-repository/tests/replayable_context_prototype.rs @@ -0,0 +1,339 @@ +//! Experimental immutable context operations, never a published repair. +#![cfg(feature = "prototype-context-completion")] +use atomic_core::change::prototype_context::{ContextEffectRef, PrototypeContextEffect as Effect}; +use atomic_core::change::{Change, ChangeHeader, GraphOp, LineOps, VerifiedChange}; +use atomic_core::crdt::{BranchId, BranchOp}; +use atomic_core::pristine::{CapabilityTxnT, PROTOTYPE_CONTEXT_CAPABILITY}; +use atomic_core::types::{Hash, NodeId}; +use atomic_repository::{ + record::RecordOptions, InsertOptions, Repository, TrackingOptions, UnrecordOptions, +}; + +const VIEW: &str = "context-sandbox"; +fn sandbox(path: &std::path::Path) -> Repository { + let mut r = Repository::init(path).unwrap(); + r.create_draft_view(VIEW, "dev").unwrap(); + r.switch_view(r.require_working_copy_id().unwrap(), VIEW) + .unwrap(); + r +} +fn record(r: &Repository, text: &[u8]) -> Hash { + std::fs::write(r.root().join("f"), text).unwrap(); + *r.record( + r.require_working_copy_id().unwrap(), + ChangeHeader::new("synthetic context fixture"), + RecordOptions::new() + .paths(vec!["f"]) + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap() + .hash() +} +fn fixture(r: &Repository) -> (Vec, Hash, Hash) { + std::fs::write(r.root().join("f"), b"alpha\nbeta\ntheta\n").unwrap(); + r.add( + r.require_working_copy_id().unwrap(), + "f", + TrackingOptions::default(), + ) + .unwrap(); + let base = record(r, b"alpha\nbeta\ntheta\n"); + let provider = record(r, b"alpha\nbeta\ntheta\ngamma\n"); + r.unrecord(&provider, UnrecordOptions::default()).unwrap(); + let original = record(r, b"ALPHA\nbeta\ntheta\n"); + let mut consumer = r.load_change(&original).unwrap(); + let foreign = BranchId::new(NodeId::new(15), 0); + consumer.hashed.file_ops[0].add_line_op(LineOps::new( + foreign, + BranchOp::Reparent { + branch: foreign, + new_after: Some(BranchId::new(NodeId::new(1), 2)), + }, + )); + consumer.hashed.header = ChangeHeader::new("synthetic consumer missing an order operand owner"); + consumer.hashed.metadata = b"immutable synthetic agent attachment".to_vec(); + let consumer = r.save_change(&consumer).unwrap(); + (vec![base, provider, consumer], consumer, provider) +} +fn transfer(source: &Repository, target: &Repository, hashes: &[Hash]) { + for hash in hashes { + let bytes = std::fs::read(source.change_store().change_path(hash)).unwrap(); + assert_eq!(VerifiedChange::from_bytes(&bytes).unwrap().hash(), *hash); + target.import_change_bytes(hash, &bytes).unwrap(); + assert_eq!( + std::fs::read(target.change_store().change_path(hash)).unwrap(), + bytes + ); + } +} +fn effect(r: &Repository, message: &str, effect: Effect, dependencies: Vec) -> Hash { + let mut c = Change::empty(ChangeHeader::new(message)); + c.hashed.hunks.push(GraphOp::PrototypeContext { effect }); + c.hashed.dependencies = dependencies; + c.hashed.dependencies.sort(); + c.hashed.dependencies.dedup(); + r.save_change(&c).unwrap() +} + +#[test] +fn saved_completion_replays_records_and_keeps_originals() { + let src_dir = tempfile::tempdir().unwrap(); + let src = sandbox(src_dir.path()); + let (mut scope, consumer, _) = fixture(&src); + let out_dir = tempfile::tempdir().unwrap(); + let out = sandbox(out_dir.path()); + transfer(&src, &out, &scope); + let before = out.view_set_id(VIEW).unwrap(); + assert!(out.prototype_insert_composed(&scope).is_err()); + assert_eq!(out.view_set_id(VIEW).unwrap(), before); + let completion = out + .prototype_create_completion(&scope, consumer, 15) + .unwrap(); + // An ordinary per-patch insertion must not execute half a composition. + assert!(out + .insert_change_rec(&completion, InsertOptions::default().view(VIEW)) + .is_err()); + assert_eq!(out.view_set_id(VIEW).unwrap(), before); + scope.push(completion); + out.prototype_insert_composed(&scope).unwrap(); + assert_eq!( + out.pristine() + .read_txn() + .unwrap() + .required_capability_version(PROTOTYPE_CONTEXT_CAPABILITY.id()) + .unwrap(), + Some(1) + ); + assert_eq!( + out.prototype_context_report().unwrap()["names"][0]["status"], + "bound" + ); + let set = out.view_set_id(VIEW).unwrap(); + out.prototype_insert_composed(&scope).unwrap(); + assert_eq!(out.view_set_id(VIEW).unwrap(), set); + out.materialize_view_to(VIEW, out.root()).unwrap(); + let mut text = std::fs::read(out.root().join("f")).unwrap(); + text.extend_from_slice(b"delta\n"); + let candidate = record(&out, &text); + let authored = out.load_change(&candidate).unwrap(); + let patch = authored + .hashed + .span + .as_ref() + .expect("ordinary recorder uses spans"); + assert_eq!( + patch.version, + atomic_core::change::span_patch::LEGACY_SOURCE_VERSION + ); + assert!(authored.file_ops().iter().any(|ops| matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ))); + assert!(out + .load_change(&candidate) + .unwrap() + .dependencies() + .contains(&completion)); + scope.push(candidate); + let dst_dir = tempfile::tempdir().unwrap(); + let dst = sandbox(dst_dir.path()); + use atomic_core::pristine::{GraphTxnT, MutTxnT}; + for i in 0..7 { + let hash = dst + .save_change(&Change::empty(ChangeHeader::new(format!( + "unselected allocation padding {i}" + )))) + .unwrap(); + let mut txn = dst.pristine().write_txn().unwrap(); + txn.register_change(&hash).unwrap(); + txn.commit().unwrap(); + } + scope.reverse(); + transfer(&out, &dst, &scope); + dst.prototype_insert_composed(&[candidate]).unwrap(); + dst.materialize_view_to(VIEW, dst.root()).unwrap(); + assert_ne!( + out.pristine() + .read_txn() + .unwrap() + .get_internal(&candidate) + .unwrap(), + dst.pristine() + .read_txn() + .unwrap() + .get_internal(&candidate) + .unwrap() + ); + assert_eq!(std::fs::read(dst.root().join("f")).unwrap(), text); + assert_eq!( + dst.load_change(&consumer).unwrap().hashed.metadata, + b"immutable synthetic agent attachment" + ); + transfer( + &src, + &dst, + &scope + .iter() + .copied() + .filter(|h| *h != completion && *h != candidate) + .collect::>(), + ); +} + +#[test] +fn incomplete_own_name_declarations_refuse_before_membership() { + let source_dir = tempfile::tempdir().unwrap(); + let source = sandbox(source_dir.path()); + let (mut scope, consumer, _) = fixture(&source); + let dir = tempfile::tempdir().unwrap(); + let r = sandbox(dir.path()); + transfer(&source, &r, &scope); + scope.push(r.prototype_create_completion(&scope, consumer, 15).unwrap()); + r.prototype_insert_composed(&scope).unwrap(); + r.materialize_view_to(VIEW, r.root()).unwrap(); + let mut bytes = std::fs::read(r.root().join("f")).unwrap(); + bytes.extend_from_slice(b"delta\n"); + let good = record(&r, &bytes); + let mut bad = r.load_change(&good).unwrap(); + for op in &mut bad.hashed.hunks { + if let GraphOp::PrototypeContext { + effect: Effect::DeclareOwnNames { owners }, + } = op + { + owners.pop(); + } + } + let bad = r.save_change(&bad).unwrap(); + let before = r.view_set_id(VIEW).unwrap(); + assert!(r + .prototype_insert_composed(&[bad]) + .unwrap_err() + .to_string() + .contains("own-name declaration")); + assert_eq!(r.view_set_id(VIEW).unwrap(), before); +} + +#[test] +fn exact_reversals_and_equal_claims_never_erase_another_claim() { + let source_dir = tempfile::tempdir().unwrap(); + let source = sandbox(source_dir.path()); + let (mut scope, consumer, provider) = fixture(&source); + let dir = tempfile::tempdir().unwrap(); + let r = sandbox(dir.path()); + transfer(&source, &r, &scope); + let a = r.prototype_create_completion(&scope, consumer, 15).unwrap(); + let b = effect( + &r, + "separate equal binding", + Effect::Bind { + consumer, + symbol: 15, + provider, + }, + vec![consumer, provider], + ); + let reverse_a = r.prototype_reverse_context(a).unwrap(); + scope.extend([a, b, reverse_a]); + r.prototype_insert_composed(&scope).unwrap(); + let report = r.prototype_context_report().unwrap(); + assert_eq!(report["names"][0]["status"], "bound"); + assert_eq!(report["names"][0]["effects"].as_array().unwrap().len(), 1); + let reverse_b = r.prototype_reverse_context(b).unwrap(); + let before = r.view_set_id(VIEW).unwrap(); + assert!(r + .prototype_insert_composed(&[reverse_b]) + .unwrap_err() + .to_string() + .contains("unresolved")); + assert_eq!(r.view_set_id(VIEW).unwrap(), before); + // Reverse-of-reverse reactivates exactly B, not A. Compose both at once. + let restore_b = r.prototype_reverse_context(reverse_b).unwrap(); + r.prototype_insert_composed(&[restore_b]).unwrap(); + assert_eq!( + r.prototype_context_report().unwrap()["names"][0]["effects"] + .as_array() + .unwrap() + .len(), + 1 + ); +} + +#[test] +fn invalid_endpoints_and_reverse_ordinals_abort_without_membership_or_capability() { + let source_dir = tempfile::tempdir().unwrap(); + let source = sandbox(source_dir.path()); + let (scope, consumer, _) = fixture(&source); + let dir = tempfile::tempdir().unwrap(); + let r = sandbox(dir.path()); + transfer(&source, &r, &scope); + let before = r.view_set_id(VIEW).unwrap(); + for hash in [ + effect( + &r, + "wrong provider", + Effect::Bind { + consumer, + symbol: 15, + provider: scope[0], + }, + vec![consumer, scope[0]], + ), + effect( + &r, + "invalid effect ordinal", + Effect::Reverse { + observed: ContextEffectRef { + change: consumer, + ordinal: 9, + }, + }, + vec![consumer], + ), + ] { + assert!(r.prototype_insert_composed(&[hash]).is_err()); + assert_eq!(r.view_set_id(VIEW).unwrap(), before); + assert_eq!( + r.pristine() + .read_txn() + .unwrap() + .required_capability_version(PROTOTYPE_CONTEXT_CAPABILITY.id()) + .unwrap(), + None + ); + } +} + +#[test] +fn competing_valid_providers_are_a_conflict_not_an_arrival_order_choice() { + let source_dir = tempfile::tempdir().unwrap(); + let source = sandbox(source_dir.path()); + let (scope, consumer, provider) = fixture(&source); + let dir = tempfile::tempdir().unwrap(); + let r = sandbox(dir.path()); + transfer(&source, &r, &scope); + let a = r.prototype_create_completion(&scope, consumer, 15).unwrap(); + let mut duplicate = r.load_change(&provider).unwrap(); + duplicate.hashed.header = ChangeHeader::new("synthetic second equally valid provider"); + let other = r.save_change(&duplicate).unwrap(); + let b = effect( + &r, + "conflicting binding", + Effect::Bind { + consumer, + symbol: 15, + provider: other, + }, + vec![consumer, other], + ); + let before = r.view_set_id(VIEW).unwrap(); + for roots in [[a, b], [b, a]] { + assert!(r + .prototype_insert_composed(&roots) + .unwrap_err() + .to_string() + .contains("conflicting providers")); + assert_eq!(r.view_set_id(VIEW).unwrap(), before); + } +} diff --git a/atomic-repository/tests/semantic_context_algebra.rs b/atomic-repository/tests/semantic_context_algebra.rs new file mode 100644 index 00000000..93c89e66 --- /dev/null +++ b/atomic-repository/tests/semantic_context_algebra.rs @@ -0,0 +1,318 @@ +//! Executable specification of the proposed context-relation algebra. +//! +//! This is NOT a native opcode, validator, migration or attachment-lookup test. +//! Fixture declarations stand in for graph endpoints independently verified by +//! a future native validator. See docs/semantic-context-completion.md. +use atomic_core::types::Hash; +use std::collections::{BTreeMap, BTreeSet}; + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +struct Name { + consumer: Hash, + symbol: u64, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +enum Effect { + Bind { name: Name, provider: Hash }, + Reverse { observed: Hash }, +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +struct Model { + required: BTreeMap)>, + provided: BTreeMap)>, + effects: BTreeMap, + attachments: BTreeMap>, +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +struct Projection { + active_claims: BTreeMap>, + cycle: bool, +} + +fn h(s: &str) -> Hash { + Hash::of(s.as_bytes()) +} + +fn name(consumer: &str) -> Name { + Name { + consumer: h(consumer), + symbol: 15, + } +} + +fn fixture() -> Model { + let mut model = Model::default(); + for patch in ["P", "X", "Q", "Y"] { + model + .required + .insert(name(patch), (h("inode"), BTreeSet::from([2, 3]))); + model + .provided + .insert(h(patch), (h("inode"), BTreeSet::from([2, 3, 4]))); + model.attachments.insert( + h(patch), + format!("unchanged synthetic attachment for {patch}").into_bytes(), + ); + } + model.provided.insert( + h("wrong-inode"), + (h("other inode"), BTreeSet::from([2, 3, 4])), + ); + model + .provided + .insert(h("missing-item"), (h("inode"), BTreeSet::from([2]))); + model +} + +impl Model { + fn apply(&mut self, id: Hash, effect: Effect) -> Result<(), &'static str> { + if let Some(existing) = self.effects.get(&id) { + return if existing == &effect { + Ok(()) + } else { + Err("effect identity mismatch") + }; + } + match &effect { + Effect::Bind { name, provider } => { + if name.symbol == 0 { + return Err("ROOT is not a foreign context symbol"); + } + let (inode, items) = self + .required + .get(name) + .ok_or("undeclared consumer symbol")?; + let (provider_inode, provided) = self + .provided + .get(provider) + .ok_or("provider is outside explicit context")?; + if inode != provider_inode { + return Err("inode mismatch"); + } + if !items.is_subset(provided) { + return Err("provider does not declare every referenced item"); + } + } + Effect::Reverse { observed } => { + if !self.effects.contains_key(observed) { + return Err("inverse lacks its observed operation context"); + } + } + } + self.effects.insert(id, effect); + Ok(()) + } + + fn active(&self, id: Hash) -> bool { + // A reverse can reference only an already declared immutable effect, + // so this effect-reference graph cannot contain a cycle. + !self.effects.iter().any(|(candidate, effect)| { + matches!(effect, Effect::Reverse { observed } if *observed == id) + && self.active(*candidate) + }) + } + + fn project(&self) -> Projection { + let mut projection = Projection::default(); + for (id, effect) in &self.effects { + if self.active(*id) { + if let Effect::Bind { name, provider } = effect { + projection + .active_claims + .entry(name.clone()) + .or_default() + .insert(*id, *provider); + } + } + } + let mut edges: BTreeMap> = BTreeMap::new(); + for (name, claims) in &projection.active_claims { + edges + .entry(name.consumer) + .or_default() + .extend(claims.values()); + } + fn cycle( + node: Hash, + edges: &BTreeMap>, + active: &mut BTreeSet, + done: &mut BTreeSet, + ) -> bool { + if done.contains(&node) { + return false; + } + if !active.insert(node) { + return true; + } + if edges.get(&node).is_some_and(|children| { + children + .iter() + .any(|next| cycle(*next, edges, active, done)) + }) { + return true; + } + active.remove(&node); + done.insert(node); + false + } + let mut done = BTreeSet::new(); + projection.cycle = edges + .keys() + .copied() + .any(|node| cycle(node, &edges, &mut BTreeSet::new(), &mut done)); + projection + } +} + +fn bind(consumer: &str, provider: &str) -> Effect { + Effect::Bind { + name: name(consumer), + provider: h(provider), + } +} + +fn reverse(observed: &str) -> Effect { + Effect::Reverse { + observed: h(observed), + } +} + +fn apply_all(model: &Model, effects: &[(&str, Effect)]) -> Model { + let mut model = model.clone(); + for (id, effect) in effects { + model.apply(h(id), effect.clone()).unwrap(); + } + model +} + +#[test] +fn proposed_independent_context_relations_compose_and_commute() { + let base = fixture(); + let a = ("R", bind("P", "Q")); + let b = ("S", bind("X", "Y")); + let c = ("T", bind("Y", "P")); + let ab = apply_all(&base, &[a.clone(), b.clone()]); + assert_eq!( + ab.project(), + apply_all(&base, &[b.clone(), a.clone()]).project() + ); + assert_eq!( + apply_all(&ab, std::slice::from_ref(&c)), + apply_all(&base, &[a.clone(), b.clone(), c.clone()]) + ); + assert_eq!( + apply_all(&apply_all(&base, &[a]), &[b, c]), + apply_all(&ab, &[("T", bind("Y", "P"))]) + ); + assert_ne!(name("P"), name("X")); + assert_eq!( + ab.project().active_claims.len(), + 2, + "encoded symbol 15 is scoped by its exact consumer patch" + ); + assert_eq!( + base.project(), + Projection::default(), + "unselected completing effects do not bind names" + ); + assert_eq!(ab.attachments, base.attachments); +} + +#[test] +fn proposed_exact_inverse_restores_relation_identity_and_preserves_other_claims() { + let base = fixture(); + let effect = ("R", bind("P", "Q")); + let applied = apply_all(&base, std::slice::from_ref(&effect)); + assert_eq!(apply_all(&applied, &[effect]), applied); + let undone = apply_all(&applied, &[("undo-R", reverse("R"))]); + assert_eq!(undone.project(), base.project()); + let restored = apply_all(&undone, &[("undo-undo-R", reverse("undo-R"))]); + assert_eq!(restored.project(), applied.project()); + assert!(restored.project().active_claims[&name("P")].contains_key(&h("R"))); + + let equal = apply_all( + &applied, + &[("equal", bind("P", "Q")), ("undo-R", reverse("R"))], + ); + let claims = &equal.project().active_claims[&name("P")]; + assert_eq!( + claims.values().copied().collect::>(), + BTreeSet::from([h("Q")]) + ); + assert!(claims.contains_key(&h("equal"))); + assert!(!claims.contains_key(&h("R"))); + for model in [&applied, &undone, &restored, &equal] { + assert_eq!(model.attachments, base.attachments); + } +} + +#[test] +fn proposed_competing_and_cyclic_relations_are_order_independent_conflicts() { + let base = fixture(); + let first = ("R", bind("P", "Q")); + let competing = ("competing", bind("P", "Y")); + let conflict = apply_all(&base, &[first.clone(), competing.clone()]); + assert_eq!( + conflict.project(), + apply_all(&base, &[competing, first.clone()]).project() + ); + assert_eq!( + conflict.project().active_claims[&name("P")] + .values() + .copied() + .collect::>() + .len(), + 2 + ); + let resolved = apply_all(&conflict, &[("remove competing", reverse("competing"))]); + assert_eq!(resolved.project(), apply_all(&base, &[first]).project()); + + // Disjoint names alone do not establish independence: these two relations + // form a cycle together. Retain both facts, never choose the first arrival. + let a = ("cycle-a", bind("P", "Q")); + let b = ("cycle-b", bind("Q", "P")); + let cyclic = apply_all(&base, &[a.clone(), b.clone()]); + assert_eq!(cyclic.project(), apply_all(&base, &[b, a]).project()); + assert!(cyclic.project().cycle); + let repaired = apply_all(&cyclic, &[("undo cycle", reverse("cycle-b"))]); + assert!(!repaired.project().cycle); + for model in [&conflict, &resolved, &cyclic, &repaired] { + assert_eq!(model.attachments, base.attachments); + } +} + +#[test] +fn proposed_undefined_context_operations_refuse_without_mutation() { + let base = fixture(); + for (label, effect) in [ + ("missing inverse context", reverse("not-applied")), + ("wrong inode", bind("P", "wrong-inode")), + ("missing item", bind("P", "missing-item")), + ("unknown provider", bind("P", "unknown")), + ("undeclared consumer", bind("unknown", "Q")), + ( + "ROOT symbol", + Effect::Bind { + name: Name { + consumer: h("P"), + symbol: 0, + }, + provider: h("Q"), + }, + ), + ] { + let mut model = base.clone(); + assert!(model.apply(h(label), effect).is_err()); + assert_eq!(model, base); + } + let mut model = apply_all(&base, &[("R", bind("P", "Q"))]); + let before = model.clone(); + assert!(model.apply(h("R"), bind("P", "Y")).is_err()); + assert_eq!( + model, before, + "one immutable operation ID cannot change payload" + ); +} diff --git a/atomic-repository/tests/semantic_inode_transport_prototype.rs b/atomic-repository/tests/semantic_inode_transport_prototype.rs new file mode 100644 index 00000000..e4e5a30c --- /dev/null +++ b/atomic-repository/tests/semantic_inode_transport_prototype.rs @@ -0,0 +1,591 @@ +//! Working native prototype, not a context-completion opcode or legacy repair. +use atomic_core::change::envelope::SessionEnvelope; +#[cfg(feature = "prototype-inode-transport")] +use atomic_core::change::GraphOp; +#[cfg(feature = "prototype-inode-transport")] +use atomic_core::change::LineOps; +use atomic_core::change::{Atom, AtomRef, Change, ChangeHeader, VerifiedChange}; +use atomic_core::crdt::tables::encode_branch_id; +use atomic_core::crdt::BranchId; +#[cfg(feature = "prototype-inode-transport")] +use atomic_core::crdt::{BranchOp, LeafId, LeafOp}; +use atomic_core::pristine::{CrdtTxnT, GraphTxnT, MutTxnT}; +use atomic_core::types::Hash; +#[cfg(feature = "prototype-inode-transport")] +use atomic_core::types::NodeId; +#[cfg(feature = "prototype-inode-transport")] +use atomic_core::types::Position; +use atomic_repository::record::RecordOptions; +#[cfg(feature = "prototype-inode-transport")] +use atomic_repository::UnrecordOptions; +use atomic_repository::{InsertOptions, Repository, TrackingOptions}; +#[cfg(feature = "prototype-inode-transport")] +use std::collections::BTreeSet; +use tempfile::TempDir; + +const VIEW: &str = "native-algebra-prototype"; +const BASE: &[u8] = b"alpha\nbeta\ntheta\n"; +const EDITED: &[u8] = b"alpha\nbeta\ntheta\ngamma\n"; + +struct Fixture { + _dir: TempDir, + source: Repository, + changes: Vec, + hashes: Vec, + envelope: Vec, +} + +fn repo(dir: &std::path::Path) -> Repository { + let mut repo = Repository::init(dir).unwrap(); + repo.create_draft_view(VIEW, "dev").unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + repo.switch_view(wc, VIEW).unwrap(); + repo +} + +fn record(repo: &Repository, path: &str, message: &str, metadata: Vec) -> Hash { + *repo + .record( + repo.require_working_copy_id().unwrap(), + ChangeHeader::new(message), + RecordOptions::new() + .paths(vec![path]) + .with_all(true) + .metadata_bytes(metadata) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap() + .hash() +} + +fn stored(repo: &Repository, hash: Hash) -> Vec { + let store = + atomic_repository::changestore::ChangeStore::open_existing(repo.changes_dir(), 16).unwrap(); + let bytes = std::fs::read(store.change_path(&hash)).unwrap(); + assert_eq!(VerifiedChange::from_bytes(&bytes).unwrap().hash(), hash); + bytes +} + +fn fixture() -> Fixture { + let dir = tempfile::tempdir().unwrap(); + let source = repo(dir.path()); + // Real, unrelated stored objects occupy source IDs 1 and 2 without being + // part of this view. Receiver allocation must not have to reproduce them. + for i in 0..2 { + let hash = source + .save_change(&Change::empty(ChangeHeader::new(format!( + "unrelated fixture {i}" + )))) + .unwrap(); + let mut txn = source.pristine().write_txn().unwrap(); + txn.register_change(&hash).unwrap(); + txn.commit().unwrap(); + } + let mut hashes = Vec::new(); + for (path, bytes) in [ + ("a.txt", BASE), + ("b.txt", b"other\n"), + ("c.txt", b"charlie\n"), + ] { + std::fs::write(dir.path().join(path), bytes).unwrap(); + source + .add( + source.require_working_copy_id().unwrap(), + path, + TrackingOptions::default(), + ) + .unwrap(); + hashes.push(record(&source, path, &format!("create {path}"), vec![])); + } + let envelope = SessionEnvelope::builder("synthetic-native-prototype", "synthetic-agent") + .turn_number(1) + .files_in_turn(vec!["a.txt".into()]) + .build() + .encode() + .unwrap(); + std::fs::write(dir.path().join("a.txt"), EDITED).unwrap(); + hashes.push(record( + &source, + "a.txt", + "E: append gamma", + envelope.clone(), + )); + std::fs::write(dir.path().join("a.txt"), BASE).unwrap(); + hashes.push(record( + &source, + "a.txt", + "R: inverse of gamma insertion", + vec![], + )); + let changes: Vec<_> = hashes + .iter() + .map(|hash| source.load_change(hash).unwrap()) + .collect(); + assert!(changes.iter().all(|c| c.hashed.span.is_none())); + assert!(changes[..3].iter().all(|c| c.dependencies().is_empty())); + assert_eq!(changes[3].dependencies(), [hashes[0]]); + assert!(changes[4].dependencies().contains(&hashes[3])); + assert!(changes[4] + .dependencies() + .iter() + .all(|h| *h == hashes[0] || *h == hashes[3])); + assert!( + changes[4] + .hunks() + .iter() + .flat_map(|h| h.atoms()) + .any(|atom| { + let update = match atom { + AtomRef::EdgeUpdate(update) | AtomRef::Atom(Atom::EdgeUpdate(update)) => { + Some(update) + } + _ => None, + }; + update.is_some_and(|u| u.edges.iter().any(|edge| edge.is_deletion())) + }), + "R must contain a real graph edge reversal, not just remove E from a view" + ); + for hunk in changes[4].hunks() { + for atom in hunk.atoms() { + let update = match atom { + AtomRef::EdgeUpdate(update) | AtomRef::Atom(Atom::EdgeUpdate(update)) => update, + _ => panic!("R must reverse E's edges, not recreate the baseline content"), + }; + assert!(update.edges.iter().all(|edge| edge.is_deletion() + && edge.to.change == Some(hashes[3]) + && edge.introduced_by == Some(hashes[3]))); + } + } + Fixture { + _dir: dir, + source, + changes, + hashes, + envelope, + } +} + +#[cfg(feature = "prototype-inode-transport")] +fn permutations(prefix: Vec, remaining: Vec, out: &mut Vec>) { + if remaining.is_empty() { + out.push(prefix); + return; + } + for (i, index) in remaining.iter().enumerate() { + let mut prefix = prefix.clone(); + prefix.push(*index); + let mut rest = remaining.clone(); + rest.remove(i); + permutations(prefix, rest, out); + } +} + +fn save_fixture(receiver: &Repository, f: &Fixture) { + for (change, hash) in f.changes.iter().zip(&f.hashes) { + assert_eq!(receiver.save_change(change).unwrap(), *hash); + assert_eq!(stored(receiver, *hash), stored(&f.source, *hash)); + } +} + +#[test] +#[cfg(feature = "prototype-inode-transport")] +fn all_twenty_native_orders_preserve_inode_ownership_inverse_and_recordability() { + let f = fixture(); + let root = tempfile::tempdir().unwrap(); + let mut permutations_ = Vec::new(); + permutations(vec![], vec![0, 1, 2, 3, 4], &mut permutations_); + let mut cases = 0; + let mut common_set = None; + for order in permutations_ { + let mut seen = BTreeSet::new(); + if !order.iter().all(|i| { + let valid = f.changes[*i] + .dependencies() + .iter() + .all(|h| seen.contains(h)); + seen.insert(f.hashes[*i]); + valid + }) { + continue; + } + let name: String = order + .iter() + .map(|i| ['A', 'B', 'C', 'E', 'R'][*i]) + .collect(); + let receiver = repo(&root.path().join(&name)); + save_fixture(&receiver, &f); + for index in &order { + receiver + .insert_change_rec(&f.hashes[*index], InsertOptions::default().view(VIEW)) + .unwrap(); + } + assert_eq!( + receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(), + BASE, + "{name}" + ); + assert_eq!( + receiver + .get_file_content_on_view("b.txt", VIEW) + .unwrap() + .unwrap(), + b"other\n" + ); + assert_eq!( + receiver + .get_file_content_on_view("c.txt", VIEW) + .unwrap() + .unwrap(), + b"charlie\n" + ); + let set = receiver.view_set_id(VIEW).unwrap(); + assert_eq!(*common_set.get_or_insert(set), set); + let txn = receiver.pristine().read_txn().unwrap(); + let a = txn.get_internal(&f.hashes[0]).unwrap().unwrap(); + let e = txn.get_internal(&f.hashes[3]).unwrap().unwrap(); + let key = encode_branch_id(&BranchId::new(e, 0)); + let line = txn.get_crdt_branch(&key).unwrap().unwrap(); + assert_eq!( + line.trunk_id.change_id(), + a, + "E attached to a different file in {name}" + ); + assert!( + line.state.is_deleted(), + "the actual inverse must tombstone E's line" + ); + for index in 0..3 { + let original = txn + .get_crdt_branch(&encode_branch_id(&BranchId::new(a, index))) + .unwrap() + .unwrap(); + assert!( + original.state.is_alive(), + "R changed an original line identity" + ); + assert_eq!(original.trunk_id.change_id(), a); + } + drop(txn); + assert_eq!( + receiver.load_change(&f.hashes[3]).unwrap().hashed.metadata, + f.envelope + ); + receiver + .insert_change_rec(&f.hashes[4], InsertOptions::default().view(VIEW)) + .unwrap(); + assert_eq!(receiver.view_set_id(VIEW).unwrap(), set); + assert!( + receiver + .unrecord(&f.hashes[3], UnrecordOptions::default().view(VIEW)) + .is_err(), + "R requires E" + ); + assert_eq!(receiver.view_set_id(VIEW).unwrap(), set); + receiver + .unrecord(&f.hashes[4], UnrecordOptions::default().view(VIEW)) + .unwrap(); + assert_eq!( + receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(), + EDITED + ); + let visible = receiver + .record_foreign_deleted_file_on_view_excluding("a.txt", VIEW, &[]) + .unwrap(); + assert_eq!(visible.crdt_ops().unwrap().line_ops().len(), 4); + // The inverse is absent from this view, even though its rows remain in + // the ambient database. Recording must use the selected patch context. + receiver.materialize_view_to(VIEW, receiver.root()).unwrap(); + std::fs::write( + receiver.root().join("a.txt"), + b"alpha\nbeta\ntheta\ngamma\nintermediate\n", + ) + .unwrap(); + let intermediate = record( + &receiver, + "a.txt", + "record while inverse is unrecorded", + vec![], + ); + assert_eq!( + receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(), + b"alpha\nbeta\ntheta\ngamma\nintermediate\n" + ); + receiver + .unrecord(&intermediate, UnrecordOptions::default().view(VIEW)) + .unwrap(); + assert_eq!( + receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(), + EDITED + ); + receiver + .insert_change_rec(&f.hashes[4], InsertOptions::default().view(VIEW)) + .unwrap(); + assert_eq!( + receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(), + BASE + ); + assert_eq!(receiver.view_set_id(VIEW).unwrap(), set); + // Native materialization followed by native recording: no alternate + // interpreter, semantic-table editing or caller-side identity override. + receiver.materialize_view_to(VIEW, receiver.root()).unwrap(); + std::fs::write(receiver.root().join("a.txt"), b"alpha\nbeta\ntheta\nnext\n").unwrap(); + let next = record( + &receiver, + "a.txt", + "record after commuting patches and inverse", + vec![], + ); + assert_eq!( + receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(), + b"alpha\nbeta\ntheta\nnext\n" + ); + assert_eq!( + receiver + .get_file_content_on_view("c.txt", VIEW) + .unwrap() + .unwrap(), + b"charlie\n" + ); + assert_ne!(next, f.hashes[4]); + for hash in &f.hashes { + assert_eq!(stored(&receiver, *hash), stored(&f.source, *hash)); + } + eprintln!("{name}: native ownership, actual inverse, attachment, replay and subsequent record PASS"); + cases += 1; + } + assert_eq!(cases, 20); +} + +#[test] +#[cfg(feature = "prototype-inode-transport")] +fn invalid_context_refuses_without_publishing_the_change() { + let f = fixture(); + for case in [ + "missing-branch", + "token-from-another-line", + "contradictory-branch", + "undeclared-context", + ] { + let dir = tempfile::tempdir().unwrap(); + let receiver = repo(dir.path()); + save_fixture(&receiver, &f); + for hash in &f.hashes[..3] { + receiver + .insert_change_rec(hash, InsertOptions::default().view(VIEW)) + .unwrap(); + } + let before = receiver.view_set_id(VIEW).unwrap(); + let mut bad = f.changes[3].clone(); + bad.hashed.header.message = format!("invalid prototype fixture: {case}"); + if case == "undeclared-context" { + bad.hashed.dependencies.clear(); + } else { + let line = bad.file_ops()[0].line_ops()[0].clone(); + let mut operation = line.operation().clone(); + if let BranchOp::Insert { after, content } = &mut operation { + if case == "missing-branch" { + *after = Some(BranchId::new(NodeId::new(3), 999_999)); + } + if case == "token-from-another-line" { + if let LeafOp::Insert { after, .. } = &mut content[0] { + *after = Some(LeafId::new(NodeId::new(3), 0)); + } + } + } + if case == "contradictory-branch" { + operation = BranchOp::Delete { + branch: BranchId::new(NodeId::new(3), 2), + content: vec![], + }; + } + let (lo, hi) = line.content_range().unwrap(); + bad.hashed.file_ops[0].line_ops_mut()[0] = + LineOps::new(line.branch_id(), operation).with_content_range(lo, hi); + } + let hash = receiver.save_change(&bad).unwrap(); + let error = receiver + .insert_change(&hash, InsertOptions::default().view(VIEW)) + .unwrap_err(); + assert!( + error.to_string().contains("inode prototype"), + "wrong refusal for {case}: {error}" + ); + assert_eq!(receiver.view_set_id(VIEW).unwrap(), before); + assert_eq!( + receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(), + BASE + ); + } +} + +#[test] +#[cfg(feature = "prototype-inode-transport")] +fn ambiguous_context_does_not_prefer_a_matching_local_number() { + let f = fixture(); + let dir = tempfile::tempdir().unwrap(); + let receiver = repo(dir.path()); + save_fixture(&receiver, &f); + for hash in &f.hashes[..4] { + receiver + .insert_change_rec(hash, InsertOptions::default().view(VIEW)) + .unwrap(); + } + let mut concurrent = f.changes[3].clone(); + concurrent.hashed.header.message = "independent second insertion at the same anchor".into(); + let other = receiver.save_change(&concurrent).unwrap(); + receiver + .insert_change_rec(&other, InsertOptions::default().view(VIEW)) + .unwrap(); + let txn = receiver.pristine().read_txn().unwrap(); + let matching_number = txn.get_internal(&f.hashes[3]).unwrap().unwrap(); + drop(txn); + let before_set = receiver.view_set_id(VIEW).unwrap(); + let before_bytes = receiver.get_file_content_on_view("a.txt", VIEW).unwrap(); + let mut ambiguous = f.changes[3].clone(); + ambiguous.hashed.header.message = + "ambiguous semantic owner in an explicit two-vertex context".into(); + ambiguous.hashed.dependencies = vec![f.hashes[0], f.hashes[3], other]; + ambiguous.hashed.dependencies.sort(); + let end = match &f.changes[3].hunks()[0] { + GraphOp::Edit { + change: Atom::Insertion(insert), + .. + } => insert.end, + _ => panic!("fixture must be a graph insertion"), + }; + if let GraphOp::Edit { + change: Atom::Insertion(insert), + .. + } = &mut ambiguous.hashed.hunks[0] + { + insert.predecessors = vec![ + Position::new(Some(f.hashes[3]), end), + Position::new(Some(other), end), + ]; + } + let line = ambiguous.file_ops()[0].line_ops()[0].clone(); + let mut operation = line.operation().clone(); + if let BranchOp::Insert { after, .. } = &mut operation { + *after = Some(BranchId::new(matching_number, 0)); + } + let (lo, hi) = line.content_range().unwrap(); + ambiguous.hashed.file_ops[0].line_ops_mut()[0] = + LineOps::new(line.branch_id(), operation).with_content_range(lo, hi); + let hash = receiver.save_change(&ambiguous).unwrap(); + let error = receiver + .insert_change(&hash, InsertOptions::default().view(VIEW)) + .unwrap_err(); + assert!( + error.to_string().contains("ambiguous graph-context owner"), + "{error}" + ); + assert_eq!(receiver.view_set_id(VIEW).unwrap(), before_set); + assert_eq!( + receiver.get_file_content_on_view("a.txt", VIEW).unwrap(), + before_bytes + ); +} + +#[test] +#[cfg(feature = "prototype-inode-transport")] +fn overlapping_native_edits_remain_conflicts_in_both_orders() { + let f = fixture(); + let mut edits = Vec::new(); + let producers = tempfile::tempdir().unwrap(); + for label in ["LEFT", "RIGHT"] { + let producer = repo(&producers.path().join(label)); + producer.save_change(&f.changes[0]).unwrap(); + producer + .insert_change_rec(&f.hashes[0], InsertOptions::default().view(VIEW)) + .unwrap(); + producer.materialize_view_to(VIEW, producer.root()).unwrap(); + std::fs::write( + producer.root().join("a.txt"), + format!("{label}\nbeta\ntheta\n"), + ) + .unwrap(); + let hash = record(&producer, "a.txt", label, vec![]); + edits.push((hash, producer.load_change(&hash).unwrap())); + } + for order in [[0, 1], [1, 0]] { + let dir = tempfile::tempdir().unwrap(); + let receiver = repo(dir.path()); + receiver.save_change(&f.changes[0]).unwrap(); + receiver + .insert_change_rec(&f.hashes[0], InsertOptions::default().view(VIEW)) + .unwrap(); + for index in order { + receiver.save_change(&edits[index].1).unwrap(); + receiver + .insert_change_rec(&edits[index].0, InsertOptions::default().view(VIEW)) + .unwrap(); + } + let bytes = receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(); + let text = String::from_utf8(bytes).unwrap(); + assert!( + text.contains("<<<<<<<") && text.contains("LEFT") && text.contains("RIGHT"), + "overlap was silently selected: {text}" + ); + } +} + +/// Historical unframed compatibility control, not the span-backed product path. +#[test] +#[cfg(not(feature = "prototype-inode-transport"))] +fn legacy_unframed_control_reproduces_semantic_misbinding() { + let f = fixture(); + let dir = tempfile::tempdir().unwrap(); + let receiver = repo(dir.path()); + save_fixture(&receiver, &f); + for hash in &f.hashes[..4] { + receiver + .insert_change_rec(hash, InsertOptions::default().view(VIEW)) + .unwrap(); + } + assert_eq!( + receiver + .get_file_content_on_view("a.txt", VIEW) + .unwrap() + .unwrap(), + EDITED + ); + let txn = receiver.pristine().read_txn().unwrap(); + let a = txn.get_internal(&f.hashes[0]).unwrap().unwrap(); + let c = txn.get_internal(&f.hashes[2]).unwrap().unwrap(); + let e = txn.get_internal(&f.hashes[3]).unwrap().unwrap(); + let line = txn + .get_crdt_branch(&encode_branch_id(&BranchId::new(e, 0))) + .unwrap() + .unwrap(); + assert_ne!(line.trunk_id.change_id(), a); + assert_eq!(line.trunk_id.change_id(), c); + assert_eq!( + receiver.load_change(&f.hashes[3]).unwrap().hashed.metadata, + f.envelope + ); +} diff --git a/atomic-repository/tests/span_insert.rs b/atomic-repository/tests/span_insert.rs index a7a886a1..f0bb0e9a 100644 --- a/atomic-repository/tests/span_insert.rs +++ b/atomic-repository/tests/span_insert.rs @@ -472,6 +472,13 @@ fn importing_spans_into_legacy_repository_preserves_legacy_edits() { ("new.txt", b"new span\n"), ] { let txn = target.pristine().read_txn().unwrap(); + if name == "legacy.txt" { + assert!(matches!( + atomic_core::output::crdt::output_file_via_crdt(&txn, target.change_store(), name), + Err(atomic_core::output::crdt::CrdtOutputError::SpanContextRequired(_)) + )); + continue; + } assert_eq!( atomic_core::output::crdt::output_file_via_crdt(&txn, target.change_store(), name) .unwrap(), diff --git a/atomic-repository/tests/span_storage_guard.rs b/atomic-repository/tests/span_storage_guard.rs new file mode 100644 index 00000000..1cbf0c9e --- /dev/null +++ b/atomic-repository/tests/span_storage_guard.rs @@ -0,0 +1,281 @@ +//! Identity/record integration must preserve the compact span graph, not merely +//! render correct bytes while expanding it back to linewise graph operations. +use atomic_core::change::{ChangeHeader, GraphOp}; +use atomic_core::pristine::{CapabilityMutTxnT, MutTxnT, SPAN_RANGE_CAPABILITY}; +use atomic_repository::{RecordOptions, Repository}; +use redb::{ReadableDatabase, ReadableTableMetadata}; +use std::path::Path; + +fn record(repo: &Repository, message: &str) -> atomic_core::Hash { + *repo + .record( + repo.require_working_copy_id().unwrap(), + ChangeHeader::new(message), + RecordOptions::new() + .paths(vec!["large.txt"]) + .with_all(true) + .save_to_store(true) + .apply_after_record(true), + ) + .unwrap() + .hash() +} + +fn graph_rows(repo: &Repository) -> (u64, u64) { + let db = repo.pristine().shared_database().unwrap(); + let txn = db.begin_read().unwrap(); + ( + txn.open_multimap_table(atomic_core::pristine::tables::GRAPH) + .unwrap() + .len() + .unwrap(), + txn.open_multimap_table(atomic_core::pristine::tables::INODE_GRAPH) + .unwrap() + .len() + .unwrap(), + ) +} + +fn semantic_rows(repo: &Repository) -> (u64, u64, u64) { + let db = repo.pristine().shared_database().unwrap(); + let txn = db.begin_read().unwrap(); + ( + txn.open_table(atomic_core::crdt::tables::TRUNKS) + .unwrap() + .len() + .unwrap(), + txn.open_table(atomic_core::crdt::tables::BRANCHES) + .unwrap() + .len() + .unwrap(), + txn.open_table(atomic_core::crdt::tables::LEAVES) + .unwrap() + .len() + .unwrap(), + ) +} + +// Raw compaction is confined to this disposable fixture, after the supported +// repository handle and all transactions have closed. Never a live migration. +fn compacted_bytes(root: &Path) -> u64 { + let path = root.join(".atomic/atomic.redb"); + let mut db = redb::Database::open(&path).unwrap(); + db.compact().unwrap(); + drop(db); + std::fs::metadata(path).unwrap().len() +} + +#[test] +fn small_native_edits_keep_span_graph_rows_constant_and_database_growth_bounded() { + storage_guard(false); +} + +#[test] +fn legacy_forward_edits_keep_authored_ranges_and_bounded_storage() { + storage_guard(true); +} + +fn storage_guard(legacy: bool) { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path()).unwrap(); + if !legacy { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let mut lines: Vec<_> = (0..2048) + .map(|i| format!("line {i:04}: stable payload\n")) + .collect(); + let initial_rows = graph_rows(&repo); + let original = lines.concat().into_bytes(); + std::fs::write(dir.path().join("large.txt"), &original).unwrap(); + repo.add( + repo.require_working_copy_id().unwrap(), + "large.txt", + Default::default(), + ) + .unwrap(); + let root = record(&repo, "span root"); + let root_change = repo.load_change(&root).unwrap(); + if legacy { + assert!(root_change.hashed.span.is_none()); + } else { + assert_eq!(root_change.hashed.span.as_ref().unwrap().ops.len(), 1); + } + let before_rows = graph_rows(&repo); + let before_semantic_rows = semantic_rows(&repo); + assert!( + legacy || (before_rows.0 - initial_rows.0 <= 8 && before_rows.1 - initial_rows.1 <= 8), + "the span root expanded into per-line graph rows" + ); + assert!( + legacy + || !root_change.hunks().iter().any(|op| matches!( + op, + GraphOp::Edit { .. } + | GraphOp::Replacement { .. } + | GraphOp::FileAdd { + contents: Some(_), + .. + } + )), + "a span root must not keep a duplicate linewise content graph" + ); + drop(repo); + let before_bytes = compacted_bytes(dir.path()); + + let repo = Repository::open(dir.path()).unwrap(); + if legacy { + let mut txn = repo.pristine().write_txn().unwrap(); + txn.put_required_capability(SPAN_RANGE_CAPABILITY).unwrap(); + txn.commit().unwrap(); + } + let mut recorded = vec![root]; + for step in 0..8 { + let line = 128 + step * 200; + lines[line] = format!("line {line:04}: changed {step}\n"); + std::fs::write(dir.path().join("large.txt"), lines.concat()).unwrap(); + let hash = record(&repo, "small span edit"); + let change = repo.load_change(&hash).unwrap(); + recorded.push(hash); + let span = change + .hashed + .span + .as_ref() + .expect("must not fall back to a linewise change"); + assert!( + (1..=if legacy { 2 } else { 1 }).contains(&span.ops.len()), + "one edited line must address only its original authored ranges" + ); + assert!( + change.contents.len() < 256, + "record copied the full file into an edit" + ); + assert!( + !change + .hunks() + .iter() + .any(|op| matches!(op, GraphOp::Edit { .. } | GraphOp::Replacement { .. })), + "content edits must stay in the span algebra, not linewise graph hunks" + ); + assert_eq!( + graph_rows(&repo), + before_rows, + "span edits expanded GRAPH/INODE_GRAPH" + ); + if legacy { + assert_eq!( + span.version, + atomic_core::change::span_patch::LEGACY_SOURCE_VERSION + ); + assert!(change.file_ops().iter().any(|ops| matches!( + ops.trunk_op(), + Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) + ))); + assert_eq!( + semantic_rows(&repo), + before_semantic_rows, + "forward projection must not fabricate historical CRDT cache rows" + ); + } + } + let expected = lines.concat().into_bytes(); + // Replay only original immutable encodings, not the source's cache rows. + let destination = tempfile::tempdir().unwrap(); + let receiver = Repository::init(destination.path()).unwrap(); + for hash in &recorded { + let bytes = std::fs::read(repo.change_store().change_path(hash)).unwrap(); + receiver.import_change_bytes(hash, &bytes).unwrap(); + assert_eq!( + std::fs::read(receiver.change_store().change_path(hash)).unwrap(), + bytes + ); + } + receiver + .insert_change_rec(recorded.last().unwrap(), Default::default()) + .unwrap(); + assert_eq!( + receiver.get_file_content("large.txt").unwrap().unwrap(), + expected + ); + assert_eq!(graph_rows(&receiver), before_rows); + if legacy { + use atomic_core::pristine::span_patch_storage::SpanRenderContext; + use atomic_core::pristine::{CachedGraphTxn, ViewTxnT}; + let (inode, _) = repo.get_inode_and_position("large.txt").unwrap().unwrap(); + let txn = repo.pristine().read_txn().unwrap(); + let view = txn.get_view(repo.current_view()).unwrap().unwrap(); + let closure = + atomic_repository::repository::effective_projection_closure(&txn, &view).unwrap(); + let cached = CachedGraphTxn::new(&txn).unwrap(); + let context = SpanRenderContext::new(&cached, repo.change_store(), &closure).unwrap(); + let state = context.record_state(inode).unwrap().unwrap(); + assert_eq!(state.spans.first().unwrap().target.owner, root); + assert!(matches!(context.render_bounded(inode, 31).unwrap(), + atomic_core::pristine::span_patch_storage::BoundedSpanContent::Oversized { retained_bytes } if retained_bytes <= 31)); + } + assert_eq!( + repo.get_file_content_on_view("large.txt", repo.current_view()) + .unwrap() + .unwrap(), + expected + ); + { + let db = repo.pristine().shared_database().unwrap(); + let txn = db.begin_read().unwrap(); + match txn.open_table(atomic_core::pristine::tables::CONTENT_CHUNKS) { + Ok(chunks) => assert_eq!( + chunks.len().unwrap(), + 0, + "ordinary span recording must keep content in change objects" + ), + Err(redb::TableError::TableDoesNotExist(_)) => {} + Err(error) => panic!("could not inspect optional content cache: {error}"), + } + if legacy { + let files = txn + .open_table(redb::TableDefinition::<&[u8; 36], u64>::new( + "span_patch_files_v1", + )) + .unwrap() + .len() + .unwrap(); + let nodes = txn + .open_table(redb::TableDefinition::<&[u8; 32], u8>::new( + "span_patch_nodes_v1", + )) + .unwrap() + .len() + .unwrap(); + let edits = txn + .open_multimap_table(redb::MultimapTableDefinition::<&[u8; 32], &[u8; 40]>::new( + "span_patch_edits_v1", + )) + .unwrap() + .len() + .unwrap(); + assert_eq!(files, 1, "legacy context needs one portable file binding"); + assert!( + nodes <= 16 && edits <= 16, + "small edits expanded span indexes: {nodes} nodes / {edits} edits" + ); + eprintln!("legacy span indexes: {files} files / {nodes} nodes / {edits} edits; retained CRDT rows {before_semantic_rows:?}"); + } + } + drop(repo); + let after_bytes = compacted_bytes(dir.path()); + eprintln!("span storage: compacted db {before_bytes} -> {after_bytes} bytes; GRAPH/INODE_GRAPH rows {before_rows:?}"); + assert!( + after_bytes <= before_bytes + 512 * 1024, + "eight small edits lost the bounded storage benefit" + ); + let reopened = Repository::open(dir.path()).unwrap(); + assert_eq!(graph_rows(&reopened), before_rows); + assert_eq!( + reopened + .get_file_content_on_view("large.txt", reopened.current_view()) + .unwrap() + .unwrap(), + expected + ); +} diff --git a/docs/README.md b/docs/README.md index ead1c9c9..0741d12f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,6 +16,7 @@ Welcome to the Atomic VCS documentation. This directory contains comprehensive d | [Software Supply Chain Checks](./software-supply-chain.md) | The standards behind canonical bytes, proofs and signatures, and the CI jobs that hold them | Developers, Contributors | | [Intent Identity](./intent-identity.md) | ULID + `PROJECT::author::seq` intent identity and reference resolution | Users, Contributors | | [Intent Knowledge Graph](./intent-graph.md) | How intents become a queryable graph and the `atomic vault query` commands | Users, Contributors | +| [Per-change Workflow Knowledge Graph](./per-change-knowledge-graph.md) | Canonical goal → intent → change → review paths, reverse traversal, real per-change results and inspection commands | Users, Reviewers, Contributors | | [ARCHITECTURE.md](./ARCHITECTURE.md) | System architecture, data model, and design decisions | Developers, Contributors | | [THEORY.md](./THEORY.md) | Mathematical foundations of patch theory | Researchers, Core developers | | [IMPLEMENTATION.md](./IMPLEMENTATION.md) | Implementation details and code organization | Contributors | diff --git a/docs/native-patch-algebra-prototype.md b/docs/native-patch-algebra-prototype.md new file mode 100644 index 00000000..eefbc447 --- /dev/null +++ b/docs/native-patch-algebra-prototype.md @@ -0,0 +1,178 @@ +# Native patch algebra prototype + +This is a working, non-default prototype for a bounded name-transport case. It +drives Atomic's actual repository, graph, semantic, insertion, materialization +and record paths. It is not the standalone context-binding design model. + +It does **not** implement the proposed context-completion opcode, fix arbitrary +legacy history, introduce ancestry, change a stored format or flip a repository +capability. Do not enable it on a production repository or publish its output as +a general compatibility fix. + +## Run + +Default-build negative control: + +```sh +cargo test -p atomic-repository \ + --test semantic_inode_transport_prototype --locked -- --nocapture +``` + +This runs an explicitly named diagnostic control which expects the existing +default build to render correct bytes but misbind the inserted line. Its green +result does **not** mean default behavior is fixed. Remove or replace that +expectation when a supported fix graduates. + +Working native prototype: + +```sh +cargo test -p atomic-repository --features prototype-inode-transport \ + --test semantic_inode_transport_prototype --locked -- --nocapture +``` + +Tests create and clean up their own disposable repositories. They do not use a +remote, install a binary, edit the real skill checkout or grant reviewer trust. +Synthetic session attachment data stays in normal draft fixtures; publication +gates are not disabled or represented as satisfied. + +## Native mechanism + +The feature adds a localizer at the native FileOps application boundary and at +the record resolver's semantic-fold boundary. It leaves the original `Change`, +its stored bytes, dependency declarations and hash unchanged. + +For operations with external names, the localizer: + +1. Obtains a single file inode from the operation's explicit graph context, + correlating graph and semantic entries only inside that same immutable patch. + It does not choose the file from an ambient path lookup. +2. Resolves the inode's existing trunk and checks the file index/owner binding. +3. Restricts candidate names to the operation's declared patch context and + canonical graph endpoints. Existing typed branches/tokens must belong to the + same inode; token references must also name the correct line. +4. Accepts only a unique, consistent, injective owner substitution. It never + prefers a candidate because its local number equals the encoded number. +5. Transports a copy of the semantic operands through the existing failure-atomic + `FileOps::remap_referenced_node_ids` primitive. ROOT remains self-relative. + +The graph writer then applies ordinary graph operations and the localized +semantic operands in its normal transaction. A refusal aborts view publication. +The reader's fold uses the same transport, rather than inventing a second +interpretation or a persistent historical override map. + +## Native experiment + +The fixture records actual non-span patches: + +- A, B and C create independent files. +- E appends a line to A's file and requires only A. +- R removes E's inserted line and requires E's context. + +R is checked to contain graph edge deletions of exactly E's vertices introduced +by E. It does not recreate the baseline file or manufacture new baseline line +identities. All three original A line identities remain alive after R. Thus this +is an actual inverse edit in its tested context, not merely unrecording E. + +Source-local unrelated registrations deliberately shift its owner numbers. The +same stored patch bytes are inserted into fresh receivers in all **20** orders +consistent with `A < E < R`; B and C may commute through that sequence. + +Every order must satisfy: + +- Identical final file contents and order-independent membership SetId. +- E's line belongs to A, never B/C or an accidental numeric alias. +- R tombstones E's line while retaining the original A lines and inode. +- Exact session attachment and immutable stored change bytes remain unchanged. +- Repeated insertion is idempotent, and removing E while R needs it refuses. +- Removing/reinserting R changes projected content as expected. +- Native recording works on **A itself** both while R is absent from the view + and after R is reinserted, despite ambient rows from other selected states. +- Independent B/C file content remains unchanged through those records. + +Negative cases must reject a missing branch, a token from another line, +contradictory branch operands, an undeclared context and an ambiguous owner—even +when one ambiguous candidate numerically matches the encoded symbol. The view's +SetId and graph-derived file bytes must remain unchanged after refusal. + +The initial strengthened run passes all four feature-enabled integration tests, +including all 20 legal orders and real recording on the affected file both with +and without the inverse selected. Strict Clippy for this feature/test target +passes. The default-build diagnostic control reproduces the original misbinding. +Logs are retained in the approved diagnostic directory as +`atomic-native-prototype-verified.log`, `atomic-native-prototype-clippy-final.log` +and `atomic-native-prototype-control.log`. + +Additional scoped regression runs pass: + +- The existing seven native `merge_property_tests` with the prototype enabled, + including generated disjoint/boundary edits, same-anchor conflicts, + round-trip content, idempotence and conflict-state honesty. +- Forty existing span insertion/recording tests in both prototype and default + builds. The existing span localizer remains separate and unchanged. +- The four context-operation design-model tests in the default regression run. +- Strict Clippy for this prototype integration-test target in both modes. + +Full workspace/all-feature testing was not performed. These are finite native +experiments, not a general proof of patch-theory laws or a production migration. + +Two independent producers also replace the same base line differently. In both +insertion orders the native graph must surface both alternatives as an explicit +conflict, never silently select the last applied value. + +## Deliberate limits + +### Trial against the actual pending skill edit + +A separate prototype-linked native executable replayed the preserved edited +checkout into a new disposable repository, transferring the original verified +change bytes without re-encoding them. Its selected source scope contained the +14 published release changes plus the local intent/attestation receipt. It also +preserved the pending v1.1.1 skill edit containing the feedback loop and grouped +multi-intent review instructions. + +The native replay accepted the first **11 of 15** changes, then refused +`FWMGY5TBGUV5RJOMN6F4CXR5UT3DX3YNSRAWS2WBZHECZDFU6Y3A` with +`inode prototype: no typed owner in explicit graph context`. Assembly of the new +skill edit was **not reached**. This is consistent with the separately verified +missing-context case; it is not evidence that the original edit is invalid. + +The source skill and `.atomic/atomic.redb` contents were hashed before and after +the trial and were byte-identical. Nothing was signed, published or pushed, and +no capability was changed. Diagnostic artifacts in the approved directory: + +- `atomic-skill-edit-prototype-trial.log` +- `atomic-skill-edit-prototype-trial/result.json` +- `atomic-skill-edit-prototype-trial/preserved-edited-skill.md` + +The executable was only a diagnostic replay/assembly probe. Even on success, it +was configured not to save or apply an unsigned candidate. It is not a new +supported Atomic CLI command or a substitute for the real publication workflow. + +A subsequent read-only [context reconstruction](semantic-context-completion.md#reconstructed-witness-for-this-gap) +found a complete witness for the blocked foreign symbol: 72 occurrences, 24 +distinct graph-backed branch declarations, with VXJVNZ as the unique complete +provider in the preserved scope. That witness has not yet been applied as a +native completion operation, so the replay trial's refusal is still the last +native application result for the actual history. + +### Unsupported cases + +- Context is available only from the existing declared dependencies and graph + endpoints. Missing context such as the real release's FWMGY5/VXJVNZ gap still + refuses. This prototype does not implement a completion operation. +- It relies on a healthy canonical inode/trunk index and typed prerequisite + declarations built by the normal engine. It is not a semantic-index repair + for repositories whose existing caches are already corrupted. +- Multiple inode anchors, ambiguous provider sets, unsupported external + creations and unproved context are rejected. Self-only operations retain the + existing native path; this is not a complete validator for hostile payloads. +- The tests establish the listed finite cases, not general commutation or + inverse theorems. Arbitrary dependent edits to the support of an inverse, + all move/rename cases, all cross-view semantic effects and complete + agent/blame lookup still need broader coverage. +- Candidate enumeration and context construction are prototype code, not a + measured large-repository optimization. + +The normal build remains unchanged. The separate +[context-completion design](semantic-context-completion.md) still requires review +before any new persisted operation or capability is implemented. diff --git a/docs/per-change-knowledge-graph.md b/docs/per-change-knowledge-graph.md new file mode 100644 index 00000000..01b22e45 --- /dev/null +++ b/docs/per-change-knowledge-graph.md @@ -0,0 +1,375 @@ +# Canonical per-change workflow knowledge graph + +**Question:** for one immutable change, which goal motivated it, which intent +governed or explicitly claims it, what was recorded, and which review covers it? +Can we follow the same evidence back from the review to the goal? + +The reading order is **goal → intent → change → review**; the reverse reading +order is **review → change → intent → goal**. Neither is a new stored predicate, +an inverse patch operation, or a Git ancestry rule. The actual edges have the +directions below, and some paths can legitimately be missing. + +This is a documentation view of the **existing** canonical ontology, not a new +schema, inference engine, CLI command, or approval mechanism. Normative vocabulary: + +- [Workflow ontology](../atomic-core/ontology/workflow-1.0.0.ttl): + `https://atomic.dev/ns/workflow/1.0.0`. +- [Review ontology](../atomic-core/ontology/review-1.0.0.ttl): + `https://atomic.dev/ns/review/1.0.0`. +- `atomic:` means `https://atomic.dev/ns/workflow#`, including review predicates. + +## 1. Nodes and identities + +| Node | Meaning | Identity rule | +|---|---|---| +| `atomic:Goal` | A recorded objective or work context | Copy its canonical IRI from graph output. | +| `atomic:WorkIntent` | Requirements, tasks, criteria and constraints | Use its canonical project-qualified IRI; human keys are CLI lookup handles. | +| `atomic:SessionTurn` | A particular captured execution turn | Project/session/turn identity; never select a turn by recency. | +| `atomic:Change` | An immutable recorded patch | `urn:atomic:change:blake3-256:`. | +| `atomic:ReviewIntent` | The reviewer's work intent, criteria and findings | A `WorkIntent` subtype; not the implementation intent or a signature alone. | +| `atomic:PublicationReview` | A present-day signed conclusion over exact scope | A content-addressed review artifact; an `Attestation` subtype. | +| `atomic:ReviewSubject` | Pinned changes, dependency closure, intent revisions and source context | Exact subject identity, not a mutable branch name alone. | +| `atomic:ReviewEvaluation` | Evaluation of a retained review | Keep its evaluation context separate from the immutable reviewed subject. | + +Change hashes are not Git commit hashes, view Merkle states, or SetIds. A Git +rebase does not create an Atomic change or establish an intent/review edge. +Paths and matching authors are not substitutes for any of these identities. + +## 2. Canonical edges: forward facts and reverse traversal + +`^predicate` below means **follow an incoming edge**. It is path notation in this +guide, not another predicate to write into the graph. + +| Asserted relationship | What it establishes | Reverse traversal | +|---|---|---| +| Goal `linkedIntent` Intent | Explicit goal-to-intent link | Intent `^linkedIntent` Goal | +| Intent `linkedGoal` Goal | Explicit intent-to-goal link | Goal `^linkedGoal` Intent | +| Intent `governsTurn` Turn | Captured governing turn | Turn `hasPlan` Intent | +| Turn `generatedChange` Change | Captured generation | Change `generatedBy` Turn | +| Change `implements` Intent | Captured implementation association | Intent `^implements` Change | +| Intent `declaresImplementation` Change | Explicit, evidence-bearing author declaration, possibly retrospective | Change `^declaresImplementation` Intent | +| Change `modifiesFile` File | Affected recorded artifact | File `^modifiesFile` Change | +| ReviewIntent `reviews` Intent | Work-intent scope of a review | Intent `reviewedBy` ReviewIntent | +| PublicationReview `reviewIntent` ReviewIntent | Review artifact's governing review intent | ReviewIntent `^reviewIntent` PublicationReview | +| PublicationReview `reviewsClosure` ReviewSubject | Exact pinned review subject | ReviewSubject `^reviewsClosure` PublicationReview | +| ReviewSubject `reviewsChange` Change | This exact change is in the reviewed subject | Change `^reviewsChange` ReviewSubject | +| ReviewSubject `includesIntent` Intent | Intent revision included in exact review scope | Intent `^includesIntent` ReviewSubject | +| PublicationReview `coversChange` Change | Change coverage; also inspect subject and artifact type | Change `attestedBy` PublicationReview | +| PublicationReview `hasJudgment` CriterionJudgment | Reviewer judgment | Judgment `^hasJudgment` PublicationReview | +| CriterionJudgment `judgesCriterion` AcceptanceCriterion | Criterion being judged | Criterion `^judgesCriterion` Judgment | + +The ontology declares inverses for `governsTurn`/`hasPlan`, +`generatedChange`/`generatedBy`, `reviews`/`reviewedBy` and +`coversChange`/`attestedBy`. It does **not** declare `linkedIntent` and +`linkedGoal` as OWL inverses. Read whichever explicit goal links exist; do not +manufacture the other one. Likewise, do not invent `implementedBy`, `approvedBy` +or `changeReviewedBy` predicates. + +### Forward: goal to intent to change to review + +```text +Goal --linkedIntent--> WorkIntent --governsTurn--> SessionTurn + ^ | + | implements | generatedChange + | v + +------------------------ Change + ^ + | reviewsChange + ReviewSubject + ^ + | reviewsClosure + PublicationReview + | + | reviewIntent + v + ReviewIntent + | + +--reviews--> WorkIntent +``` + +An explicit `WorkIntent --linkedGoal--> Goal` can supply the goal connection +instead of `Goal --linkedIntent--> WorkIntent`. + +Captured implementation has the property-chain meaning: + +```text +Change --generatedBy--> SessionTurn --hasPlan--> WorkIntent +Change --implements--------------------------------> WorkIntent +``` + +An author's `declaresImplementation` is a **different** path. It can make an +implementation claim without a generating turn, but must not be relabeled as +captured provenance or used to fill a missing `hasPlan` edge. + +### Reverse: review to change to intent to goal + +```text +PublicationReview --reviewsClosure--> ReviewSubject --reviewsChange--> Change + | + generatedBy + v + SessionTurn + | + hasPlan + v + WorkIntent + | + linkedGoal + v + Goal +``` + +Where present, `Change --implements--> WorkIntent` is a shortcut for the +captured path. For declarations use incoming `declaresImplementation`; for a +goal with only `linkedIntent`, traverse that edge backwards from the intent. +If no qualifying link exists, the reverse path stops with a reported gap. + +## 3. Keep the kinds of evidence separate + +- **Captured implementation:** the recorded generating-turn/intent association. +- **Declared implementation:** an explicit author claim, not historical capture. +- **Intent recording:** a change modifies the *exact intent Markdown artifact*. + `atomic intent graph` joins stored `vault_path` metadata to `modifiesFile` and + reports `recording_changes` separately. This does not prove implementation. +- **Observed intent:** `Change --hasReviewEvidence--> IntentObservation + --observedIntent--> Intent` retains an observation or receipt, not governance. +- **Dependency:** `Change --directlyDependsOnChange--> Change` supplies patch + prerequisites. Dependency-only work does not inherit the root's intent, + authorship, goal or approval. +- **Explanation:** provenance graphs and memories can explain a decision; + `prov:wasDerivedFrom` does not replace an implementation or review assertion. + +One change can have several roles and implement several intents. Do not discard +that multiplicity to force a single linear chain. Conversely, many changes and +intents may share **one group review**. A per-change report repeats the review +artifact's reference for covered changes; it does not require a new review per +change or count the shared approval as several independent reviews. + +### A link to a review is not an approval + +Inspect the artifact type: an ordinary session attestation can also +`coversChange`. For a publication review, retain its exact subject, work/review +intent revisions, criterion judgments, evidence, signer and policy context. +Check cryptographic `verificationResult`, current `freshnessResult`, +`authorityResult`, required verifications and blocking findings. Schema +`canonicalConformance`, an unsigned preview, a `done` label, or a signature alone +does not establish substantive approval. + +`includesSetAside` describes an independently reviewed terminal disposition; it +is not proof that the historical implementation satisfies its criteria. +An intent-level `reviews` link alone must not be stretched over later changes. +Use the actual triage/publication evaluation for the promotion being considered. + +## 4. One Markdown result per change + +Use the following as a **reporting template**, not as a claim that a new CLI +returns these field names. Keep canonical IRIs and evidence next to every result. + +| Result item | What to retain | +|---|---| +| Change | Full hash/IRI, message and verified local object availability. | +| Snapshot | Project, source view, Merkle/SetId where available, KG generation, ontology version and graph `snapshot_identity`. | +| Goals | Goal/intent pairs and the explicit predicate/direction joining them. | +| Captured intents | Intent IRI and generating turn/path; separate from declarations. | +| Declared intents | Intent IRI, declaration evidence and current signature status. | +| Recording/observation evidence | Exact intent artifact or retained observation; never silently promoted to implementation. | +| Dependencies | Required changes, labeled separately from selected implementation. | +| Reviews | Review intent, artifact and subject IRIs; selected-root/dependency role; pinned scope, judgments, verification, freshness, authority and blockers. | +| Forward result | Proven goal → intent → this change → exact review paths, or the point where each path stops. | +| Inverse result | The same facts traversed review → this change → intent → goal; no additional inferred approval. | +| Limits and gaps | Unavailable objects, missing links, ambiguous claims, stale indexes, truncated exports and unestablished judgments. | + +Use **“not established in this snapshot”**, not “never existed,” for an absent +link. A failed or capped query is not an empty successful result. A two-hop +neighborhood is not complete lineage. Shared file, session, author or view +neighbors can lead to unrelated changes and must not expand review authority. + +## 5. Commands: start at a change + +Run inside the Atomic repository containing the object. These examples use the +published review-skill patch; substitute another full hash for another change. + +```sh +CHANGE_HASH=GYA4QZCV4Z4IPG6SC7TXAMAZHAVPOMS33DVOU3R6DTXSXFCR6MUA + +# Immutable change details; graph hunks can be empty for a span-backed edit. +atomic change "$CHANGE_HASH" --full-hash --show-deps --show-hunks +atomic change "$CHANGE_HASH" --format json + +# Discover the canonical resource rather than guessing a project-qualified ID. +atomic query search "$CHANGE_HASH" --kind 'https://atomic.dev/ns/workflow#Change' --json +``` + +Copy the exact `id` from search. For this example it is: + +```sh +CHANGE_IRI=urn:atomic:change:blake3-256:GYA4QZCV4Z4IPG6SC7TXAMAZHAVPOMS33DVOU3R6DTXSXFCR6MUA + +# Neighbors includes incoming AND outgoing edges, with their original direction. +atomic query neighbors "$CHANGE_IRI" --depth 1 --json +atomic query neighbors "$CHANGE_IRI" --depth 2 --json + +# Exploratory graph export, NOT a complete canonical lineage/approval report. +atomic query graph "$CHANGE_HASH" --limit 1 --depth 2 --changes-per-seed 0 --json > change-neighborhood.json +``` + +For captured explanation, when available: + +```sh +atomic provenance trace "$CHANGE_HASH" +atomic provenance show "$CHANGE_HASH" > change.prov.jsonld +``` + +These commands can report that no provenance graph explains the change. Preserve +that result; do not invent a generating turn. The JSON-LD projection is unsigned +by default. Signing an export would be a new present-day assertion, not an +original execution signature. Current `atomic change` renders recorded +provenance in its default tree; legacy `change -p`/`change -a` flags are not +available in the CLI used for this guide. + +### Directional per-change queries and immutable patch evidence + +`query plan` supports predicate/direction filtering that the simpler `neighbors` +command does not expose. This plan asks only for relevant direct relationships +and bounded original-object evidence: + +```sh +atomic query plan --json < intent-graph.json +atomic intent graph ATOM::continuouslee::1 --details +atomic intent graph ATOM::continuouslee::1 --dot -o intent.dot +atomic intent graph ATOM::continuouslee::1 --html --no-open -o intent.html +``` + +Inspect `captured_changes`, `declared_changes`, `implementing_changes`, +`recording_changes`, `dependency_changes`, `reviewed_changes`, `missing_resources`, +`truncated` and `diagnostics`. This is an **intent-centered** asserted-lineage +export, not an implicit per-change graph command. Its `scope` and snapshot +identity qualify any result you derive from it. Follow an observed or +recording-only intent as context without claiming it governed the change. + +Use `atomic query neighbors '' --json` for a goal and the same +command for copied review intent, subject, artifact or evaluation IRIs. + +```sh +# Current candidate-set evaluation; replace SOURCE_VIEW and TARGET_VIEW. +atomic triage review SOURCE_VIEW --into TARGET_VIEW --json + +# Revalidate registered exact-scope retrospective publication reviews. +atomic triage publication-review inspect + +# Structural workflow checks, not a substitute for inspecting the work. +atomic query validate --profile projection --format json +``` + +The publication validator profile and the promotion policy have additional +requirements; a projection-profile pass alone does not mean “approved.” For +review authoring, signing, registration and renewal see +[Retrospective Publication Review](./retrospective-publication-review.md). + +### Missing or stale derived indexes + +Normal inspection/export commands do not author missing governance or approval. +If a command reports `KgNeedsRebuild` or an unprojected resource, an explicit +maintenance action may be needed: + +```sh +atomic query reindex --full +``` + +This **writes derived indexes** from retained canonical objects and vault data; +it is not a read-only inspection command. Re-export the graph afterward and +record the new snapshot identity. Reindexing does not manufacture provenance, +retroactively bind an intent, or approve a review. Investigate schema/repository +errors rather than treating them as evidence that no links exist. + +## 7. Observed per-change results: the published review-skill update + +Observed on 2026-11-15 using `atomic 0.22.1 (dev 540688f1310d-dirty)` in a fresh +clone of the canonical `atomic-skills` storage **release** view. This is an +explicit historical snapshot, not a claim about future source or approvals. +Derived indexes were explicitly rebuilt in that disposable clone after an +initial `KgNeedsRebuild` response; no historical capture was added. + +| Pin | Value | +|---|---| +| Project | `G4YM66Q3RX5TGKH4YRBYDZXA6PRPCN6JF7AB4LAGYTP2NBMWCEUA` | +| View Merkle | `ER2S7UIQAOAAL3I5QISADZAIJ7E6B2HH6TI47QDQIR4KYBSRLGJQ` | +| KG generation / workflow version | `2` / `https://atomic.dev/ns/workflow/1.0.0` | +| Intent graph snapshot | `NHPD6W5UEZLVBZWX5VSQUZWR6FVHRWA2VQ2PXJPCKTW32HQXZZBQ` | +| Intent | `ATOM::continuouslee::1`, UID `01M7AD39HBA6NSJSMQX7ZKB78T` | +| Captured / declared / implementing / reviewed changes in its intent graph | All empty; `truncated: false`, no missing resources reported. | + +| Change | Recorded result | Goal/intent result | Review/inverse result | +|---|---|---|---| +| `Y7DBQKHTSS7IHTE4BCTEX4FCA2BZL34TK44WJFAYXBCL5B5WCYDA` | Object available/projected; records the exact intent artifact. | Listed in `recording_changes`, **not** `implementing_changes`; no captured goal/intent path established. | No retained publication review returned by inspection; no established review-to-change-to-goal path. | +| `GYA4QZCV4Z4IPG6SC7TXAMAZHAVPOMS33DVOU3R6DTXSXFCR6MUA` | Object available/projected; `modifiesFile` identifies `skills/triage-review/SKILL.md`; five direct patch dependencies. | No direct `generatedBy`, `implements`, incoming `declaresImplementation` or `hasReviewEvidence` returned by the filtered plan. | No incoming `reviewsChange` returned; inspection returns `[]`. Review approval and its inverse chain are **not established**. | + +The projection validator reports `conforms: true` for this snapshot, but its +generated-change shape has **zero applicable/evaluated targets**. That is not +proof of complete execution provenance. The separate `release → dev` candidate +report is `blocked` with eleven findings over the group; it is not eleven +findings attributed to this one skill patch. + +For `GYA4QZCV4Z4I`, bounded patch evidence returns `found`, zero GraphOps, one +FileOps entry and an 8,419-byte inserted blob, with content truncation reported. +`atomic provenance trace` reports that no provenance graph explains the change. +The manual record's authorship/AI declarations do not invent a captured turn. + +The actual proven paths include: + +```text +Y7DBQKHTSS7I --modifiesFile--> .vault/intents/01M7AD39HBA6NSJSMQX7ZKB78T/intent.md +GYA4QZCV4Z4I --modifiesFile--> skills/triage-review/SKILL.md + +Goal ? --explicit link--> Intent ? <--implementation evidence-- GYA4QZCV4Z4I +GYA4QZCV4Z4I <--reviewsChange-- Subject ? <--reviewsClosure-- Review ? +``` + +Question marks mark unestablished links, not placeholder assertions to persist. +The conversation and local intent explain the work to a human, but that is not +permission to fill the canonical graph by matching filenames, guessing a +`plan_id`, creating an adoption intent, or manufacturing an independent review. +Successful recording, publishing and fresh-clone tests establish those outcomes; +they do not establish the missing workflow/review relationships. + +## Further reference + +- [Intent graph semantics and capture/declaration boundaries](./intent-graph.md). +- [Exact review scope and policy](./retrospective-publication-review.md). +- [Typed predicate registry and inverse declarations](../atomic-core/src/kg/ontology.rs). +- [Intent graph selection and result sets](../atomic-repository/src/intent_graph.rs). +- [Review graph construction](../atomic-repository/src/repository/publication_review/graph.rs). +- [Query-plan schema](../atomic-repository/src/query_plan.rs) and + [bounded patch evidence](../atomic-repository/src/patch_evidence.rs). diff --git a/docs/replayable-context-sandbox.md b/docs/replayable-context-sandbox.md new file mode 100644 index 00000000..ac87476a --- /dev/null +++ b/docs/replayable-context-sandbox.md @@ -0,0 +1,165 @@ +# Replayable context-completion sandbox + +**Experimental; not for publication, production upgrade, or original-history repair.** +This is a legacy-compatibility/algebra experiment, **not** the product storage +direction. Product integration must preserve the existing compact span graph; +see [the architectural correction](semantic-context-production.md). +The user explicitly authorized storing a new feature-gated operation only in +disposable repositories. No installed binary, source skill checkout, old patch, +original dependency list, trust policy, remote, or original capability was changed. + +## Implemented subset + +Build `atomic-repository` with `prototype-context-completion`. It also enables +`prototype-inode-transport` and the experimental core codec variant. The persisted +operations are: + +- `Bind { consumer: Hash, symbol: u64, provider: Hash }`. +- `Reverse { observed: { change: Hash, ordinal: u32 } }`. +- `DeclareOwnNames { owners: Vec<(u64, Hash)> }` within a newly recorded patch. + +External Bind/Reverse carriers contain one context operation at ordinal zero +with no content/semantic writes. A binding explicitly requires both immutable +endpoints; a reversal requires the exact observed operation. Endpoint validation +reconstructs the complete graph-backed witness from verified original encodings. +Line-order occurrences are supported: `Reparent` and the foreign predecessor of +a consumer-owned newly inserted line. Foreign content/token-target updates still +refuse. The executable binding is scoped by consumer hash and encoded symbol, +not a global NodeId alias. + +Own-name declarations in this experiment capture its **own current +encoding frame** before hashing/signing, covering every foreign semantic owner +exactly once with an explicit hash prerequisite. This is not reconstructed +old-author evidence. A declaration is a typed operation in its content patch at +its actual ordinal, not a metadata field or sidecar. Its symbols must exactly +match the patch's foreign names and its distinct providers must be declared +prerequisites. Native inode/line/token validation still checks its use. These +intrinsic encoding declarations cannot be independently erased by a completion +reversal. The private writer remains limited to already-marked disposable +experimental repositories; it is not the span-backed product format. The later +schema-4/default non-span detour and full-byte redb copy have been withdrawn. + +`prototype_insert_composed` holds the normal operation lock, prepares a journaled +membership/capability operation, and applies the selected object set in one graph +transaction. An execution-only ordering accounts for explicit provider context; +the original stored dependencies and their index are never rewritten. Carrier +effects must match the sealed, validated projection. A carrier cannot be inserted +via ordinary recursive insertion, which refuses before inserting prerequisites. + +Capability `prototype-semantic-context-NOT-FOR-PUBLICATION` version 1 lands in the +same graph transaction. Ordinary builds do not advertise support. The installed +ordinary binary refused the experimental sandbox with exit 3; a before/after +database digest confirmed no modification. + +Record folds rebuild the context projection from selected immutable operations, +use completed prerequisite order, and invalidate the semantic memo on a changed +projection digest. New records retain active completion carriers for the canonical +inodes they touch and the complete required-consumer context, transitively, rather +than depending on every unrelated completion in a view. A bundled prerequisite's +other operations still need their explicit context for faithful native replay; +that does not mean the new patch modifies those other files. +Reusing already-applied ambient semantic writes under a different binding is +refused; this prototype is intended for fresh sandboxes, not in-place migration. + +## Passing bounded tests + +```sh +cargo test -p atomic-repository --features prototype-context-completion \ + --test replayable_context_prototype --test semantic_inode_transport_prototype \ + --test native_span_record --test span_insert --locked +cargo test -p atomic-repository --features prototype-context-completion \ + --lib context_ --locked +``` + +The completion tests cover saved-operation replay into a fresh repository, a real +subsequent record with retained completion requirements, replay of that record, +unchanged original hashes/attachment bytes, idempotence, equal independently +reversible claims, inverse-of-inverse, invalid endpoints/ordinals, and conflicting +valid providers in both input orders. Own-name coverage tampering refuses before +membership, and the new record replays from its hash with shifted receiver IDs. +The witness controls remain executable. + +## Actual preserved skill trial + +```sh +cargo run -p atomic-repository --features prototype-context-completion \ + --example replay_context_sandbox --locked -- \ + /path/to/preserved-edited-atomic-skills /new/disposable/output +``` + +The input is read-only and the output must not exist. The example verifies exact +object transfer and original source skill/database bytes, then writes `result.json`. +It never signs or publishes the experiment. + +The stored completion for consumer `FWMGY5…`, symbol `15`, provider `VXJVNZ…` +successfully composes that consumer's required scope. This is nine selected +changes including the new carrier, not all fifteen original candidate changes. +That completed scope is also transferred and natively replayed into a second +fresh sandbox without a sidecar. + +**Correction after the user's scope review:** the earlier trial reported an +index disagreement in `.github/scripts/test_validate_skill_metadata.py`, but +that file has no occurrence of the requested symbol and was not modified by the +pending edit. The witness validator was over-scoped: it treated unrelated ROOT +`SetMode`/`SetKind` operations as prerequisites for a foreign branch-name proof. +That report was not evidence of a blocker in the triage-review edit. + +The validator now checks the requested symbol's actual file/branch occurrences, +while retaining canonical owner constraints and strict index checks at those +occurrences. Two new controls distinguish unrelated attributes from a real +target-reference index mismatch; all fourteen witness tests pass. + +Direct audit finds symbol 15 in `HFAP35…`'s historical `atomic-vault` and +`intent-builder` operations, not its Python file or `triage-review` operations. +Scoped reconstruction proves **780 occurrences / 261 distinct branches**, with +`VXJVNZ…` as the sole complete provider. The unchanged source database is verified. + +The only non-`Reparent` use in that gap was an inserted line's predecessor, also +line-order context. The sandbox now supports that narrow case. Four completed +names are needed by `HFAP35…`: symbol 12 → `EHTMCB…`, 14 → `4ASHRO…`, +15 → `VXJVNZ…`, and 16 → `FWMGY5…`. Each is stored in a separate validated Bind +patch. Together with the first FWMGY5 completion, all fifteen original objects +replay without changing their bytes or dependency lists. + +**Trial 15 successfully records and replays the actual preserved v1.1.1 edit.** +The new experimental skill patch is +`KKGEG2DVJUTQFUFVJJYHQK4R3CJJPSFYJMH7DUASGEL5GBFDAJKQ`. +It replays from **its own hash**, with its declared prerequisite closure, in a +fresh sandbox. No sidecar or caller-supplied binding table is needed. Unrelated +recorded paths retain identical canonical presence/content; source skill and +database bytes remain unchanged. The patch is unsigned and unpublished. + +The same example checks concrete commutation: an independently recorded new-file +patch is applied before versus after the real skill patch set. One receiver also +has seven unselected ID-allocation padding objects, and the candidate's local IDs +are verified to differ. The two compositions have identical SetId and canonical +presence/content across the original paths and the independent file. The target +skill and independent file also have identical hash-normalized CRDT line names, +line ownership/state/order, token identities/ownership/state and token ranges. + +Comparison uses canonical view entries, not stale directory leftovers: +`materialize_view_to` is additive and can leave bytes for a historically deleted +path. Present entries are additionally checked against materialized bytes. +This is a passing bounded law test, not a proof of every possible commutation. +The full result is retained as `atomic-replayable-context-trial-15/result.json` +in the approved OpenCode temporary directory. + +Historical trial 17 passed with the subsequently withdrawn schema-4 detour. +Those results establish only that experiment's correctness checks, **not** that +it preserved span storage or was an approved product architecture. Its artifacts +remain isolated; they are not rewritten or promoted. The product direction is +the existing span record/index/projection flow, with database-growth checks as +well as algebra tests. + +## Remaining limits + +- Unresolved/conflicting compositions refuse atomically. They are **not** yet + retained as selected conflict-state operations; reversal of the sole required + claim therefore refuses unless composed with a restoring/remaining claim. +- General trunk/index/token completion is unsupported. Context proof alone is + not a proof of all consumer ordering laws or all legacy operation validity. +- Full crash/recovery qualification, ordinary insertion of completed closures, + cross-view reuse of ambient semantic tables, and production format negotiation + are not complete. The experimental opcode is not a finalized wire contract. +- Passing these bounded tests is not independent review, production promotion, + or authorization to push the skill or experimental artifacts. diff --git a/docs/semantic-context-completion.md b/docs/semantic-context-completion.md new file mode 100644 index 00000000..84ce33d4 --- /dev/null +++ b/docs/semantic-context-completion.md @@ -0,0 +1,285 @@ +# Proposed patch-context completion operation + +**Status: production design for review; separately authorized sandbox experiment.** +The user first selected design-only work, then explicitly authorized an +experimental replayable patch in disposable repositories. The production format +is not finalized. The actual skill edit now records and replays in disposable +sandboxes, including a concrete commutation test; the published source is still +untouched. No ancestry mechanism is proposed. See +[sandbox results](replayable-context-sandbox.md). + +A later [native prototype](native-patch-algebra-prototype.md) tests ordinary +inode-based name transport, commutation and an actual inverse where existing +context suffices. It does not implement the operation proposed here or activate +a capability. The newer replayable experiment is a distinct, separately gated +feature; it is not a production implementation of every obligation below. + +## Why inode transport alone is insufficient + +The inode establishes the file, not every referenced line/token owner. A read-only +name-transport trial resolves the first eleven changes of atomic-skills release +from canonical graph inodes and the operations' declared contexts. It then finds +a context gap in: + +- Consumer P: `FWMGY5TBGUV5RJOMN6F4CXR5UT3DX3YNSRAWS2WBZHECZDFU6Y3A`. +- Candidate provider Q: `VXJVNZNCFTBSMOSCGOXA27CACML6YO62GSGWIH6CEROX23IR3CSQ`. + +P's `skills/atomic-vault/SKILL.md` operations reference encoded foreign owner 15, +including branch 2. Q declares a self-relative branch 2 for that same file, with +a content range. Q is absent from P's native dependency closure; `extra_known` +and its frontier roots are empty. A separate native closure query confirms this, +independently of the trial's context-membership cache. + +This identifies a candidate missing context, not a recovered original-author +attestation. A production validator must still establish every referenced +endpoint against the exact inode, graph ranges and token relationships. Matching +a branch number or text is insufficient. The trial currently refuses the gap; +it does not import Q from arbitrary repository state or rewrite P's dependency +index. The trial is archived outside production source. + +## Reconstructed witness for this gap + +The missing declaration is not missing data. A read-only reconstruction now +accounts for **all 72 occurrences** of foreign symbol 15 in P, covering **24 +distinct branch identities**. Q is the only complete graph-backed provider in +the preserved selected patch set. No chronological ordering or receiving-local +NodeId is used to choose it. + +Reproduce the witness with the version-controlled example: + +```sh +cargo run -p atomic-repository --example reconstruct_semantic_context --locked -- \ + /path/to/atomic-skills \ + FWMGY5TBGUV5RJOMN6F4CXR5UT3DX3YNSRAWS2WBZHECZDFU6Y3A \ + 15 /existing/output-directory/context-witness.json +``` + +The output must be new and outside the repository. The program opens the source +read-only, verifies stored objects using their original encodings, checks the +canonical file creation/index, and checks each self-relative provider branch +against its graph-owned byte ranges, line boundaries and token bytes. Every +consumer occurrence has an exact role and declaration witness. This bounded +implementation supports newline-terminated branch declarations; trunk/token +completion and unsupported occurrences refuse rather than receive partial proof. + +The planner also checks that a proposed provider does not itself require the +consumer, which would make the new prerequisite cyclic. Joint cycles involving +other future completion relations remain an operation-integration obligation. + +Twelve example tests cover complete occurrence coverage, missing providers, +additional equally valid providers, scope reorder, conflicting inode identity +despite equal paths/text/indices, incomplete or altered witnesses, unsupported +token occurrences, cyclic prerequisites, duplicate declarations, mismatched +semantic bytes, missing graph support, mid-line ranges and separation of context +proof from consumer operation validity. Strict Clippy for the +example passes. The actual preserved skill edit and database remain unchanged. + +The witness audits direct self-parent order operations separately and always +sets `application_validated` to false. In the real consumer the direct audit +finds **zero** such self-parent operations. An earlier inference from grouped +reference lists was incorrect: roles with the same indices occurred at different +operation sites. The synthetic self-parent test ensures a complete endpoint +witness cannot conceal a detected order fault or be presented as proof that a +patch is executable. + +An automatic provider choice is unique **within the recorded search scope**, +not a claim about every hypothetical patch or an original author's signature. +Once an explicit positive witness pins Q's hash, adding another candidate does +not silently redirect that witness; the tests distinguish new-planning ambiguity +from validation of an already selected immutable endpoint. `search_scope`, +`search_set_id` and the database digest are audit observations, not dependencies +on a live view or lifetime requirements for an applied relation. + +The witness is not yet a native completion operation. It must not be consumed as +an unchecked sidecar override or used to modify P's hashed dependencies or their +index. The remaining step is to express/consume the completion through ordinary +patch algebra under the reviewed operation and compatibility contract below. + +## Required abstraction: a graph name, not ancestry + +An operation can introduce an explicit relation between a name used by a patch +and a context object that supplies that name. The key is scoped to the exact +immutable patch, not a repository's global numbering: + +```text +ContextName = (consumer change hash, encoded foreign-owner symbol) +Provider = stable provider change hash +EffectRef = (introducing change hash, operation ordinal) +``` + +The encoded symbol is a literal namespace slot in the consumer's payload. It is +never looked up as a receiving repository's NodeId. ROOT is not a foreign symbol. +Names in two different consumers remain distinct even if both contain `15`. + +The relation is part of ordinary typed graph/semantic state and is introduced by +an ordinary patch operation, analogous to a name-binding or attribute operation. +Any physical index is a derived cache of those operations, not another authority, +another version-control graph, a metadata repair database or a branch lineage. +The design must not be implemented by renaming the rejected JSON overlay or +changing a dictionary on reads without normal operation, conflict and inverse +semantics. + +## Proposed operations (names and encoding not finalized) + +```text +BindContext { + consumer: ChangeHash, + symbol: ForeignOwnerSymbol, + provider: ChangeHash, + witnesses: exact typed endpoint proofs +} + +ReverseContextEffect { + observed: EffectRef +} +``` + +`BindContext` adds a new relation claim. Its containing patch R must explicitly +require P, Q and the objects necessary to validate the witnesses. R does not edit +P or Q, rewrite P's hashed dependencies or modify `CHANGE_DEPS` to disagree with +the stored object. The existing dependency closure already brings P and Q into +any selection containing R. The new relation completes a context name; it is +not an assertion that one patch is a chronological parent of another. + +`ReverseContextEffect` reverses exactly the observed relation effect. It requires +that effect in its ordinary patch context. It cannot mean "delete whichever +mapping is current". Reversing a reversal must reactivate the original relation +identity, rather than manufacture a substitute relation and orphan attachments. +Native encoding should reuse established exact edge-transition/inverse machinery +where it genuinely matches this contract; the model below is not a substitute +for that integration proof. + +An inverse restores the relation's projected effect, not an erased audit trail +or an automatic release of all prerequisite references. Ordinary dependency- +closed insertion/removal rules still apply to the recorded operations. + +The physical representation remains a design gate. A `ContextName` is not a +CONTENT byte position: do not encode its symbol or operation ordinal as a +`GraphNode` offset and alias existing vertices. Reuse a properly typed ordinary +operation/event representation and derived indexing, as appropriate to the +native graph algebra, rather than introducing a second patch-history graph. + +A generic patch dependency edge P→Q is not enough: it neither identifies the +foreign name nor proves a file/line/token endpoint. `SolveOrderConflict` is also +not automatically appropriate: the byte graph can already be correctly ordered +while a semantic reference is unresolved. + +## State and algebraic obligations + +Visible binding claims and their exact reversals are immutable operation facts. +Evaluate the active relation claims under the ordinary selected change filter: + +- No valid active claim: the name remains unresolved. Surface a semantic-context + conflict; never fall back to a coincidentally equal local ID. +- One provider value: the name is bound, while retaining every claim identity. +- Multiple distinct providers: explicit context-binding conflict. No timestamp, + insertion order, model identity or "latest" claim wins automatically. + +Equal concurrent claims remain separately addressable. Reversing one does not +erase the other. Inserting the same change twice remains idempotent. + +Required laws, for defined contexts and logical-state equivalence: + +1. Composition is associative. +2. Independent context relations commute. +3. A binding followed by its exact inverse restores the prior projection. +4. Reversing that inverse restores the original binding identity. +5. Independent reversals commute and do not erase unrelated claims. +6. Conflicting claims arrive at the same explicit conflict in either order. +7. Selecting no completing operation has no completing effect. +8. Graph/patch names and attachment identities survive every admissible + commutation and local-ID renaming. + +Disjoint name keys alone are **not** proof of independence. Two otherwise valid +context relations can jointly introduce a requirement cycle. Concurrent facts +must be retained and produce an order-independent cycle conflict, not "accept +the first and discard the second". A local planning policy may refuse a cyclic +candidate before recording, but replication must not use arrival order to choose +which signed operation survives. Cycle detection uses only patch prerequisites +and the actual context relations, never a chronological lineage. + +## Witness validation and authorization + +Before binding is usable, verify: + +- Original consumer/provider objects against their original stored encodings. +- The consumer actually declares the specified non-ROOT foreign symbol. +- Every occurrence being bound is covered, including trunk, line, token and + predecessor references. A partially covered owner symbol is not a repair. +- Canonical inode positions establish the same file; names/paths are display + hints, not identity authority. +- Provider declarations, graph spans and byte/token relationships establish each + typed item. Equal text or matching numeric indices do not establish ownership. +- The new operation's context includes all referenced objects and the observed + effect for an inverse. Validation failures leave the transaction unchanged. +- Current authorization and review policy permit this new operation. A repair + signature records a present-day decision, never backdates original intent, + session capture or provenance. + +An automated planner must refuse when it cannot establish a unique completion. +If a human supplies an explicit resolution, that is a newly authored patch +decision with its own evidence, not a claim that missing historical evidence was +recovered. Impossible or contradictory typed endpoints still refuse. + +## Agent attachment contract + +P, Q and their attachment payloads keep their original hashes and bytes. The +relation is attributable to R; it must not reattribute P/Q's code to R. Attachments +to existing graph positions or operation identities remain addressable before, +during and after inverse operations. A name unresolved without R must be reported +as unresolved—not silently attached to another file or agent. + +Testing unchanged envelope bytes is necessary but insufficient: native tests must +also exercise attachment lookup and blame through the completed relation. The +standalone model cannot establish this native lookup property. + +## Compatibility and native integration gates + +No production opcode, wire version, capability name or CLI command is allocated +by this document. A future implementation must: + +1. First establish whether existing exact graph transitions or a context-valid + commutation/reduction can express the needed relation. For example, a truly + redundant ordering operation may reduce to identity, but that must be proven + in valid context, not assumed because it is inconvenient to resolve. A passing + bind/reverse model is not proof that a new opcode is necessary. +2. If a new operation is necessary, version it and activate an explicit required + capability only after + verified preparation. Unsupported clients must refuse before mutation or + interpretation; they must never ignore a completing operation and write under + the old interpretation. Verify this with actual older binaries. +3. Plan without changing original objects, indexes or working bytes. Use the + existing operation/effect journal, expected-state leases and recovery, not a + second journal or raw database edits. +4. Integrate the relation into ordinary semantic graph application, filtering, + commutation, inversion, conflict display, recording and blame/diff. Merely + storing a new metadata document is not implementation. +5. Preserve content rendering when semantic context is unresolved, while + explicitly surfacing the semantic conflict and refusing operations that need + the missing identity. No unchecked "best effort" semantic writes. +6. Verify the complete real release, not just the first missing relation. Other + missing context or graph/semantic inconsistencies may remain after this one. +7. Keep the original skill edits intact, test native record and fresh-clone replay, + and obtain independent review before any real publication or capability flip. + +## Executable design checks and their limits + +`atomic-repository/tests/semantic_context_algebra.rs` is the reproducible, +version-controlled design model. It models immutable bind facts and exact +reversals over already verified endpoint declarations. Four tests pass with +`cargo test -p atomic-repository --test semantic_context_algebra --locked`. Passing +checks cover independent commutation, inverse identity and inverse-of-inverse, +idempotence, equal concurrent claims, competing-provider conflicts, cycle +conflicts and reversal, missing context, wrong inode, incomplete provider items, +undeclared symbols, ROOT rejection, selected-operation scope and unchanged +attachment payloads. The earlier standalone run is retained as +`atomic-context-completion-model.log` in the approved diagnostic directory; +the repository test run is retained as `atomic-context-completion-design-tests.log`. +Strict Clippy for this test target also passes with +`cargo clippy -p atomic-repository --test semantic_context_algebra --locked -- -D warnings`. + +This is an executable algebra model, not a native patch implementation. It does +not prove witness extraction from arbitrary legacy data, static-plus-dynamic +cycle handling in Atomic's actual dependency engine, native view behavior, +signature authorization, wire compatibility, journaling/recovery or correct +attachment lookup. These remain implementation and review requirements. diff --git a/docs/semantic-context-production.md b/docs/semantic-context-production.md new file mode 100644 index 00000000..d550e734 --- /dev/null +++ b/docs/semantic-context-production.md @@ -0,0 +1,180 @@ +# Semantic context integration into the compact span graph + +**Corrected direction:** use the existing span-backed graph and semantic +projection. The purpose of the span work is to keep persistent graph/database +growth small. Identity correctness must be integrated into that design, not used +to create a parallel default linewise/non-span product path. + +## Architectural correction + +The preceding schema-4 proposal and `change_original_bytes` redb table were an +assistant detour, not an approved extension of the span design. Passing their +record/replay tests did not establish that they preserved compact storage. + +That detour has been removed from source: no schema-4 discriminator, default +writer, standalone context section/capability, HashedChange field, or complete +encoded-object redb table remains. No live database, original change or capability +was rewritten to retract it. Disposable schema-4 trial artifacts are historical +only; do not publish them or reinterpret their hashes as span changes. + +The useful shared `SemanticNameFrame` validator remains in memory and is used by +the existing span owner-map validator/localizer. It changes neither the existing +span encoding nor the graph storage model. Experimental legacy completion facts +and their algebra tests remain separately gated; they are not a replacement for +span-native recording or evidence that product integration is complete. + +## Existing architecture to preserve + +- `atomic-core/src/pristine/span_patch_storage.rs` explicitly keeps content in + change objects. Its tables index file identities, authored span ranges and + range edits—not another complete copy of each change. +- `SpanPatch.semantic_owners` already carries portable owner hashes where CRDT + compatibility operations require them. Reuse that representation and its + capability fence rather than adding a competing default encoding. +- Span semantic projections retain exact original authored fragments and their + hash-derived identities. Do not manufacture a single historical CRDT author + for a line composed from several original changes. +- Preserve original hashes, dependency facts, signatures and agent attachments. + Any completion of missing legacy context must be an explicit validated patch + operation, not ancestry, path-based rebinding or an unchecked sidecar. + +The existing storage work and its release gates are documented in +`docs/git-span-import-handoff.md` and +`.vault/goals/span-backed-storage-integration/work-tree.md`. Historical benchmark +numbers there are not new measurements or permission to bypass unfinished gates. + +## Storage regression guard + +`atomic-repository/tests/span_storage_guard.rs` uses a disposable, span-enabled +repository and the ordinary native record path. It records a 2,048-line root, +then eight small edits, compacts only the closed disposable database and reopens. +The guard requires: + +1. Each edit stays a typed span range edit, with less than 256 inserted bytes. +2. No content `Edit`/`Replacement` graph hunks are emitted as a fallback. +3. GRAPH/INODE_GRAPH row counts remain constant through the edits and reopen. +4. Ordinary recording does not duplicate content into the optional database + content cache. +5. Compacted database growth remains bounded, and reconstructed bytes are exact. + +Observed on the corrected source: compacted `atomic.redb` grows from +**2,015,232 to 2,138,112 bytes**; GRAPH/INODE_GRAPH retain **4 / 2 rows**. This is +a bounded native regression fixture, not a real-project size claim or a proof of +general scalability. The initial native semantic baseline is included; rows were +not deleted to manufacture a saving. Change objects, journals and other required +metadata remain intact. + +## Legacy graph ranges in the ordinary span path + +The default-built implementation now supports forward span edits over verified +legacy byte graphs. It does **not** retrofit the old numeric CRDT names or add +the sandbox's `Bind`/`DeclareOwnNames` operations to the product format. + +- A file is identified by its original creator hash and `GraphOp::FileAdd` + ordinal. Its canonical graph position resolves the receiver-local inode. + Paths and sender CRDT indices are not identity authorities. +- The read-only adapter checks an unambiguous, non-zombie legacy graph under + the explicit dependency closure. Physical vertex splits are normalized to + their immutable original insertion ranges. Every visible byte must have one + original declaration for that file. +- Span payload **version 3** extends the existing `SpanOp::Edit` algebra to those + verified legacy sources; existing version-2 bytes remain unchanged. A + `ProjectedSpanEdit` binds the exact semantic baseline. No original change, + dependency, signature, or attachment is rewritten, and historical content is + not copied into redb or re-attributed to a new author. +- `span-legacy-context` version 1 and the existing projected-semantics fence + prevent older clients from silently ignoring the interpretation. These reader + fences remain while immutable objects remain stored, including after undo. +- Ordinary record, import and recursive insertion use the existing transactional + span index and operation journal. The only extra legacy binding is the small + derived portable-file-to-inode index. Missing typed span indexes cannot be + relabeled as legacy sources. +- Rendering checks stored edit rows against hashed operations and requires + every selected edit to be reachable. Bounded legacy output validates the + fragment layout and edits without concatenating the original file first. + Unavailable, ambiguous or conflicting ranges refuse rather than inventing + semantic ownership. +- Canonical repository content APIs are already view-aware. The low-level + ambient CRDT output API now refuses with `SpanContextRequired` when authored + span projection supersedes its retained cache; it never guesses a latest view. + +### Real candidate trial + +`cargo run -p atomic-repository --example span_skill_replay --locked -- + ` uses only ordinary APIs and default features. +The first successful run retained fifteen original objects, recorded the actual +skill edit as `HTRBETCOS5P7KWQJSK5DQC6HFW3OBEVZZIT65RXQTVBGWWCIOL7Q` +(8,419 inserted bytes, 159 span operations), replayed from the candidate's own +hash and passed independent-file commutation under shifted receiver allocations. +The projected line/token identities and unrelated entries matched; original +encodings/attachments were transferred unchanged and the source database and +skill bytes were preserved. This trial is not a signature, publication, or an +independent review verdict. + +Fresh trial 3, after capability-read hardening, records candidate +`OQRSJCHWXVSXWTQILIUABBB4JCVPGC3NHJPNXCCZFRCVM6S5IE5Q` with the same +8,419 inserted bytes and 159 span operations. GRAPH/INODE_GRAPH stay at +**14,218 / 14,206** through the real skill edit. Both independent-file insertion +orders yield **14,222 / 14,208** rows and equal authored line/token identities. +Reopen after disposable receiver compaction preserves state; the compacted +receiver is **4,435,968 bytes** (not a matched growth benchmark). The installed +older client refuses the sender with exit 3 naming `span-legacy-context`; its +database SHA-256 is unchanged after refusal. Results are retained in +`atomic-span-product-completion-trial-3/result.json` in the approved temporary +directory. + +Six focused `legacy_span_context` tests pass: empty/repeated edits and fresh +replay; malformed ordinal/provider/version/declarations/baseline refusal; +retained fences after record/reopen/undo; disjoint cross-view composition with +an unaffected parent; atomic refusal of overlapping edits; and refusal rather +than stale bytes when an edit index or required capability is missing. These +are bounded examples, not exhaustive conflict/recovery qualification. + +Broad release acceptance remains open for remote/server compatibility, all +producer/consumer audits, cross-view/conflict combinations, and fault-injected +interruption/recovery qualification. Do not infer deployment approval from the +passing disposable trial. + +```sh +cargo test -p atomic-repository --test span_storage_guard --locked -- --nocapture +cargo test -p atomic-repository --test native_span_record --test span_insert --locked +cargo test -p atomic-core --lib change::semantic_context --locked +cargo test -p atomic-repository --test legacy_span_context --locked +``` + +The extended storage guard also covers a legacy 2,048-line root followed by eight +ordinary span edits: GRAPH/INODE_GRAPH remain **4,100 / 4,098**, each edit inserts +less than 256 bytes, and compacted database size grows from **2,539,520 to +2,678,784 bytes** in the final measured run. Its span index contains one file +binding, sixteen authored nodes and sixteen edits; retained CRDT cache rows stay +at **1 trunk / 2,048 lines / 16,384 tokens**, without new fabricated history. +The existing native root guard remains **4 / 2** graph rows, with compacted DB +**2,015,232 to 2,150,400 bytes**. Historical sizes above differ slightly with journal metadata; +neither fixture drops required metadata or rewrites old objects to make a saving. +Exact-byte import and hash-root replay are now part of both storage fixtures. + +Final validation: the complete core/repository run passes **6,198 tests** including +doctests (with the existing ignored cases); the subsequently extended focused +legacy suite passes all **6** tests. The feature-gated completion/inode +compatibility suites pass **9** tests. Workspace/all-target compilation, strict +workspace library Clippy and strict targeted test/example Clippy pass. Strict +workspace/all-target Clippy remains blocked by the pre-existing `useless_vec` +test lint at `atomic-core/src/pristine/git_closure.rs:276`; that unrelated file +was not changed. Logs: `atomic-span-full-tests-final.log`, +`atomic-span-legacy-context-tests-qualified.log`, `atomic-span-storage-qualified.log`, +`atomic-span-compat-qualified.log` and `atomic-span-final-*.log` in the approved +temporary directory. + +## Remaining integration + +The ordinary compact-span record/index/projection implementation and the actual +skill reproduction are now present. Continue qualification at those boundaries, +not by restoring the discarded schema-4 path. Keep normal publication, lock, +lease and recovery policy. Remote/server and all-producer compatibility, detailed +crash-injection cases and independent promotion review are still open. The +experimental composed executor delegates newly recorded span candidates to the +same product span writer; its private completion operations stay feature-gated. + +Intent `ATOM::continuouslee::253` remains in progress. The former schema-4 results +are withdrawn as product acceptance, not relabeled as successful span integration. +No binary was installed and no experiment was pushed. diff --git a/docs/semantic-patch-transport.md b/docs/semantic-patch-transport.md new file mode 100644 index 00000000..6ae28cc0 --- /dev/null +++ b/docs/semantic-patch-transport.md @@ -0,0 +1,153 @@ +# Semantic patch transport and commutation + +## The defect is a violated patch law + +A patch's meaning must not depend on the order in which independent patches +were inserted or the numeric IDs a particular repository allocated to them. +Atomic's graph operations name external objects by hash and resolve those names +locally. Ordinary non-span `FileOps` currently retain non-ROOT numeric owner IDs +without an equivalent portable binding. They can consequently attach a line to +an unrelated file after transfer or independent insertion reordering. + +This is not a missing branch lineage or an instruction to reconstruct ancestry. +Adding dependencies on independent changes to reproduce a producer's allocation +order would mask the defect, constrain valid commutations, and still fail when +unrelated local registrations differ. The rejected historical metadata-overlay +experiment is not the production solution. + +## Laws and state equivalence + +Write `P ; Q` for applying P, then Q, in valid context. Logical state equivalence +includes file content, file/line/token identities and relationships, conflicts, +and stable agent attachments. It does not require equal repository-local numbers, +equal order-sensitive view Merkle chains, or byte-identical operation journals. + +1. **Contextual composition.** `P ; Q` is defined only when Q's required context + is available. Associativity must hold for defined compositions. +2. **Independent commutation.** For independent operations with compatible + context, `P ; Q` and `Q ; P` yield equivalent logical states. More general + contextual commutation may require transported operands; absence of a direct + dependency alone is not proof of independence. +3. **Transport equivariance.** If two repositories use different injective maps + from stable object names to local IDs, transporting a patch and state between + those representations must commute with application. The names change; the + operation and agent attachment do not. +4. **Partial inverse.** An inverse is defined against the appropriate observed + context. A valid `P ; inverse(P)` restores equivalent projected state, not an + erased audit trail. Removing a view reference is not by itself proof of an + inverse-patch construction. +5. **Undefined operations refuse.** Missing, ambiguous or conflicting context + must not be invented. A failed name transport must not leave a partly renamed + patch for a caller to apply accidentally. + +For identity transport `T`, the representation laws are: + +```text +T_identity(P) = P +T_g(T_f(P)) = T_(g composed with f)(P) +T_inverse(f)(T_f(P)) = P for an invertible name map f +T_f(T_g(P)) = T_g(T_f(P)) for disjoint, non-capturing renamings +``` + +These laws for names do not claim that arbitrary content edits commute. ROOT +placeholders retain their self-relative meaning and are not external owners. + +## Native counterexample + +The standalone native experiment in `atomic-semantic-inspect/src/bin/algebra.rs` +in the approved diagnostic directory uses four immutable non-span test patches: + +- A creates `a.txt`. +- B creates `b.txt`. +- C creates `c.txt`. +- E appends a line to `a.txt` and requires only A. + +The creations are independent. E can commute past B and C but cannot be moved +before A. The experiment enumerates all twelve dependency-valid permutations, +using normal repository insertion, not a custom semantic interpreter. The same +four patch objects, including E's explicitly synthetic session attachment, are +used in every repository. Synthetic attachment data is exercised in draft views; +publication policy is not disabled and no fixture is represented as trusted +original-session evidence. + +All twelve orders have equal graph content, SetId and attachment bytes, and +idempotent insertion. Required-context removal refuses. Removing E restores the +three-file base projection, and reinsertion restores membership without changing +stored objects or attachments. Nevertheless, ten orders attach E's line to the +wrong file. Only `B C A E` and `C B A E` happen to allocate A the numeric ID that E +recorded in its producer. Thus byte-only merge properties can pass while semantic +commutation fails. This does not date the introducing commit or establish a +general inverse operator. + +Detailed run: `atomic-semantic-algebra-laws-v2.log`; per-case results are in +`atomic-semantic-algebra-laws-v2/results.json` in the approved temporary directory. + +## Existing operation machinery + +`atomic-core/src/apply/edge.rs` applies graph transitions additively: the old edge +remains and a change introduces the new edge, with visibility controlled by the +normal view filter. `NewEdge::reverse` exchanges the exact prior/new flags and +accepts the appropriate introducing change. Name/order conflict operations also +have explicit solve/unsolve forms. These are useful ordinary patch primitives; +none of them supplies a missing semantic object name automatically. + +`FileOps::remap_referenced_node_ids` is the existing name-transport primitive. +Its implementation now resolves each distinct external owner once before +mutating any operation. Resolver errors leave the original FileOps unchanged. +Tests cover identity, composed and inverse renaming, disjoint-renaming +commutation, ROOT preservation, consistent owner resolution and failure +atomicity across the existing trunk/branch/leaf variants. This is a limited +primitive fix, not a fix for missing portable bindings in ordinary change files. + +## Remaining native fix + +The production boundary to correct is the writer/codec/localizer boundary, not +the ordering of unrelated patches: + +- Persist semantic names using stable change identity and an intra-change index, + with self-relative names represented explicitly. The graph already uses the + analogous external-hash/local-ID distinction. +- Localize those names consistently for every native consumer, not only span + changes. A codec name dictionary is representation data, not a mutable repair + database, an ancestry relation or a permission to change an old patch's meaning. +- Preserve the exact bytes, addresses and attachments of old published changes. + For old payloads without portable bindings, use only uniquely established + operation context or explicit patch operations to resolve a real relationship; + reject insufficient context. Never use matching numbers or chronological order + as an owner resolver. +- Version any wire/capability change explicitly so unsupported readers cannot + silently interpret external names as local numbers. Do not overload session + metadata or label a non-span payload as a span to select a different reader. +- Upgrade the native regression to require semantic ownership and subsequent + recording to succeed in all legal orders, while dependent/conflicting cases + still refuse appropriately and all original attachment bytes remain intact. + +No native codec or old-history recovery implementation is claimed by this note. +The published atomic-skills recording failure remains open. + +There is now a bounded, opt-in [native prototype](native-patch-algebra-prototype.md) +using existing inode and graph-endpoint context. Unlike the original standalone +models, it runs the real insertion and record paths and exercises an actual +inverse edit. It changes neither the stored format nor default behavior and is +not a general solution for the missing-context legacy cases. + +The subsequent inode-context trial found a separate missing declared semantic +context in the real release. The user selected design before new native +operations or compatibility changes. See [the proposed context-completion +operation](semantic-context-completion.md) and its explicitly model-only tests; +it is not an implemented repair or an ancestry-based interpretation layer. + +## Validation of the limited primitive change + +- `cargo test -p atomic-core --lib --locked`: 3,756 tests pass, including four + transport-law tests. Before the implementation change, the exactly-once owner + resolution and failure-atomicity tests fail. +- `cargo test -p atomic-repository --test span_insert --test native_span_record + --locked`: 40 integration tests pass (10 insertion, 30 recording). +- `cargo clippy -p atomic-core --lib --locked -- -D warnings`: passes. +- Strict Clippy including tests remains blocked by existing `type_complexity` + and `useless_vec` findings in `pristine/span_patch_storage.rs` and + `pristine/git_closure.rs`; those unrelated files were not changed. + +The twelve-order diagnostic deliberately records the unresolved semantic law +failures. The green primitive tests must not be mistaken for an end-to-end fix.