use super::*;
const DRIFTED_MD: &str = "---\ntype: spec\ncreated_date: 2026-01-15\nlast_modified: 2026-04-12\nzzz_bogus_field: x\n---\n# Drifted\n\n## Identity\n\nNon-conformant fixture.\n\n## Purpose\n\nRepair-gate tests.\n\n## Relationships\n\n- **USES**: [[anchor]]\n";
fn repair_engine() -> (TempDir, Engine) {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
std::fs::write(
mem_dir.join("anchor.md"),
"---\ntype: spec\ncreated_date: 2026-01-15\nlast_modified: 2026-04-12\n---\n# Anchor\n\n## Identity\n\nTarget.\n\n## Purpose\n\nRelation target.\n",
)
.unwrap();
std::fs::write(mem_dir.join("drifted.md"), DRIFTED_MD).unwrap();
let writer = FilesystemBackend::new(mem_dir.clone());
let engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
(tmp, engine)
}
fn repair_args(id: EntityId, hash: Option<String>) -> UpdateEntityArgs {
UpdateEntityArgs {
anchors: Vec::new(),
id,
expected_hash: crate::ExpectedHash::from_token(hash),
sections: IndexMap::new(),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: vec![crate::ops::RelationUnsetArg {
rel_type: "USES".to_string(),
target: EntityId::new("specs", "anchor"),
}],
anchors_unset: Vec::new(),
}
}
#[test]
fn relations_unset_on_conformant_entity_refuses_repair_not_needed() {
let (_tmp, mut engine) = repair_engine();
let anchor = EntityId::new("specs", "anchor");
let drifted = EntityId::new("specs", "drifted");
engine
.relate_entity(
RelateEntityArgs {
source: anchor.clone(),
expected_hash: None,
rel_type: "USES".to_string(),
target: drifted.clone(),
remove: false,
description: None,
dry_run: false,
},
Actor::Cli,
None,
None,
)
.expect("relate on conformant entity works");
let mut args = repair_args(
anchor.clone(),
engine.get_entity(&anchor).map(|e| e.content_hash.clone()),
);
args.relations_unset[0].target = drifted.clone();
let err = engine
.update_entity(args, Actor::Cli, None, None)
.unwrap_err();
match err {
EngineError::RepairNotNeeded { id, recovery } => {
assert_eq!(id, anchor.to_string());
assert!(
recovery.contains("memstead_relate"),
"recovery must point at the focused tool; got {recovery}"
);
}
other => panic!("expected RepairNotNeeded, got {other:?}"),
}
let entity = engine.store().get(&anchor).unwrap();
assert!(
entity.relationships.iter().any(|r| r.target == drifted),
"gate must not modify the entity"
);
}
#[test]
fn relations_unset_repairs_non_conformant_entity_atomically() {
let (_tmp, mut engine) = repair_engine();
let drifted = EntityId::new("specs", "drifted");
let pre = engine.conformance_findings("specs", None).unwrap();
assert!(
pre.iter().any(|f| f.id == drifted.to_string()),
"fixture must lint non-conformant; got {pre:?}"
);
let mut args = repair_args(
drifted.clone(),
engine.get_entity(&drifted).map(|e| e.content_hash.clone()),
);
args.metadata_unset = vec!["zzz_bogus_field".to_string()];
engine
.update_entity(args, Actor::Cli, None, None)
.expect("repair update lands");
let entity = engine.store().get(&drifted).unwrap();
assert!(
entity.relationships.is_empty(),
"relation must be removed; got {:?}",
entity.relationships
);
assert!(
!entity.metadata.contains_key("zzz_bogus_field"),
"conformance break must be repaired in the same update"
);
let post = engine.conformance_findings("specs", None).unwrap();
assert!(
post.iter().all(|f| f.id != drifted.to_string()),
"post-repair entity must be conformant; got {post:?}"
);
}
#[test]
fn relations_unset_post_state_must_still_validate() {
let (_tmp, mut engine) = repair_engine();
let drifted = EntityId::new("specs", "drifted");
let mut args = repair_args(
drifted.clone(),
engine.get_entity(&drifted).map(|e| e.content_hash.clone()),
);
args.sections = IndexMap::from_iter([("nonexistent_section".to_string(), "x".to_string())]);
let err = engine
.update_entity(args, Actor::Cli, None, None)
.unwrap_err();
assert_eq!(
err.code(),
"UNKNOWN_SECTION",
"strict-write post-condition must hold during repair; got {err:?}"
);
let entity = engine.store().get(&drifted).unwrap();
assert!(
!entity.relationships.is_empty(),
"refused repair must not partially apply"
);
}
#[test]
fn relations_unset_absent_pair_is_silent_noop() {
let (_tmp, mut engine) = repair_engine();
let drifted = EntityId::new("specs", "drifted");
let mut args = repair_args(
drifted.clone(),
engine.get_entity(&drifted).map(|e| e.content_hash.clone()),
);
args.relations_unset[0].rel_type = "NEVER_DECLARED".to_string();
args.metadata_unset = vec!["zzz_bogus_field".to_string()];
engine
.update_entity(args, Actor::Cli, None, None)
.expect("absent pair no-ops, update lands");
let entity = engine.store().get(&drifted).unwrap();
assert_eq!(
entity.relationships.len(),
1,
"the USES relation must survive an unmatched unset"
);
}
fn anchor_input(artifact: &str, hash: &str) -> crate::anchor::AnchorInput {
crate::anchor::AnchorInput {
artifact: Some(artifact.to_string()),
grain: Some("file".to_string()),
class: Some("anchored".to_string()),
hash: Some(hash.to_string()),
hash_stability: Some("stable".to_string()),
..Default::default()
}
}
fn anchor_unset(artifact: &str) -> crate::anchor::AnchorUnsetInput {
crate::anchor::AnchorUnsetInput {
artifact: Some(artifact.to_string()),
grain: None,
class: None,
span: None,
}
}
fn anchor_args(id: EntityId, hash: Option<String>) -> UpdateEntityArgs {
UpdateEntityArgs {
anchors: Vec::new(),
anchors_unset: Vec::new(),
id,
expected_hash: crate::ExpectedHash::from_token(hash),
sections: IndexMap::new(),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
}
}
fn anchored_engine() -> (Engine, TempDir, EntityId, String) {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
let (actor, client) = cli_actor();
let mut args = empty_create_args("specs", "Anchored");
args.anchors = vec![anchor_input("a.rs", "h-a"), anchor_input("b.rs", "h-b")];
let created = engine
.create_entity(args, actor, Some(&client), None)
.unwrap();
let id = EntityId::new("specs", "anchored");
assert_eq!(engine.entity_anchors(&id).len(), 2);
(engine, tmp, id, created.content_hash)
}
#[test]
fn update_anchors_merge_appends_and_replaces_by_triple() {
let (mut engine, _tmp, id, hash) = anchored_engine();
let (actor, client) = cli_actor();
let mut args = anchor_args(id.clone(), Some(hash));
args.anchors = vec![anchor_input("c.rs", "h-c")];
let out = engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
let anchors = engine.entity_anchors(&id);
assert_eq!(anchors.len(), 3, "N existing + 1 new = N+1");
assert_eq!(anchors[0].artifact, "a.rs");
assert_eq!(anchors[0].hash.as_deref(), Some("h-a"));
assert_eq!(anchors[1].artifact, "b.rs");
assert_eq!(anchors[2].artifact, "c.rs");
assert!(!engine.anchors_referencing_artifact("a.rs").is_empty());
assert!(!engine.anchors_referencing_artifact("c.rs").is_empty());
let mut args = anchor_args(id.clone(), Some(out.content_hash));
args.anchors = vec![anchor_input("a.rs", "h-a2")];
engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
let anchors = engine.entity_anchors(&id);
assert_eq!(anchors.len(), 3);
assert_eq!(anchors[0].artifact, "a.rs");
assert_eq!(anchors[0].hash.as_deref(), Some("h-a2"));
assert_eq!(anchors[1].hash.as_deref(), Some("h-b"), "b untouched");
assert_eq!(anchors[2].hash.as_deref(), Some("h-c"), "c untouched");
}
#[test]
fn update_anchors_full_resend_and_absent_are_noops_on_stored_set() {
let (mut engine, tmp, id, hash) = anchored_engine();
let (actor, client) = cli_actor();
let sidecar_path = tmp.path().join(crate::anchor::ANCHOR_SIDECAR_PATH);
let before = std::fs::read(&sidecar_path).unwrap();
let mut args = anchor_args(id.clone(), Some(hash));
args.anchors = vec![anchor_input("a.rs", "h-a"), anchor_input("b.rs", "h-b")];
let out = engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
assert_eq!(
std::fs::read(&sidecar_path).unwrap(),
before,
"full re-send keeps the stored bytes"
);
let mut args = anchor_args(id.clone(), Some(out.content_hash));
args.sections
.insert("identity".to_string(), "changed body".to_string());
engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
assert_eq!(
std::fs::read(&sidecar_path).unwrap(),
before,
"an anchorless update never touches the stored set"
);
}
#[test]
fn update_anchors_unset_bare_narrowed_idempotent_and_unset_first() {
let (mut engine, _tmp, id, hash) = anchored_engine();
let (actor, client) = cli_actor();
let mut span = anchor_input("a.rs", "h-span");
span.grain = Some("span".to_string());
let mut args = anchor_args(id.clone(), Some(hash));
args.anchors = vec![span];
let out = engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
assert_eq!(engine.entity_anchors(&id).len(), 3);
let mut narrowed = anchor_unset("a.rs");
narrowed.grain = Some("span".to_string());
let mut args = anchor_args(id.clone(), Some(out.content_hash));
args.anchors_unset = vec![narrowed];
let out = engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
let anchors = engine.entity_anchors(&id);
assert_eq!(anchors.len(), 2);
assert!(
anchors
.iter()
.all(|a| a.grain == crate::anchor::AnchorGrain::File)
);
let mut args = anchor_args(id.clone(), Some(out.content_hash.clone()));
args.anchors_unset = vec![anchor_unset("never-there.rs")];
engine
.update_entity(args, actor, Some(&client), None)
.expect("unset of a nonexistent target is a no-op, not an error");
assert_eq!(engine.entity_anchors(&id).len(), 2);
let mut args = anchor_args(id.clone(), Some(out.content_hash));
args.anchors_unset = vec![anchor_unset("a.rs")];
args.anchors = vec![anchor_input("a.rs", "h-a-fresh")];
engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
let anchors = engine.entity_anchors(&id);
assert_eq!(anchors.len(), 2);
assert_eq!(anchors[0].artifact, "b.rs", "b.rs untouched throughout");
assert_eq!(anchors[1].hash.as_deref(), Some("h-a-fresh"));
}
#[test]
fn a_payload_naming_one_triple_twice_is_refused() {
let (mut engine, _tmp, id, hash) = anchored_engine();
let (actor, client) = cli_actor();
let mut args = anchor_args(id.clone(), Some(hash.clone()));
args.anchors = vec![
anchor_input("a.rs", "h-first"),
anchor_input("a.rs", "h-second"),
];
let err = engine
.update_entity(args, actor, Some(&client), None)
.expect_err("the repeated triple must refuse");
assert_eq!(err.code(), crate::anchor::INVALID_ANCHOR_CODE);
assert!(
format!("{err}").contains("more than once"),
"the refusal names the collapse: {err}"
);
assert_eq!(engine.entity_anchors(&id).len(), 2);
let mut span = anchor_input("a.rs", "h-span");
span.grain = Some("span".to_string());
let mut args = anchor_args(id.clone(), Some(hash));
args.anchors = vec![anchor_input("a.rs", "h-file"), span];
engine
.update_entity(args, actor, Some(&client), None)
.expect("two grains on one artifact are two rows");
assert_eq!(engine.entity_anchors(&id).len(), 3);
}
#[test]
fn a_re_pin_without_a_hash_keeps_the_stored_baseline() {
let (mut engine, _tmp, id, hash) = anchored_engine();
let (actor, client) = cli_actor();
let mut hashless = anchor_input("a.rs", "");
hashless.hash = None;
let mut args = anchor_args(id.clone(), Some(hash));
args.anchors = vec![hashless];
engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
let kept = engine
.entity_anchors(&id)
.into_iter()
.find(|a| a.artifact == "a.rs")
.expect("the row is still there");
assert_eq!(
kept.hash.as_deref(),
Some("h-a"),
"the baseline the re-pin did not mention survives it"
);
}
#[test]
fn update_anchor_only_and_unset_only_commit_without_hash_movement() {
let (mut engine, _tmp, id, hash) = anchored_engine();
let (actor, client) = cli_actor();
let mut args = anchor_args(id.clone(), Some(hash.clone()));
args.anchors_unset = vec![anchor_unset("b.rs")];
let out = engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
assert!(
!out.write_id.is_empty(),
"unset-only update commits the sidecar"
);
assert_eq!(out.content_hash, hash, "anchors never move `_hash`");
assert_eq!(engine.entity_anchors(&id).len(), 1);
let err = engine
.update_entity(
anchor_args(id.clone(), Some(hash)),
actor,
Some(&client),
None,
)
.unwrap_err();
assert!(matches!(err, EngineError::EmptyUpdate { .. }));
}
#[test]
fn anchor_only_update_across_second_boundary_never_moves_hash() {
let (mut engine, _tmp, id, hash) = anchored_engine();
let (actor, client) = cli_actor();
let t0 = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_778_243_696);
engine.set_mutation_clock(std::sync::Arc::new(move || t0));
let mut args = anchor_args(id.clone(), Some(hash));
args.metadata = [("level".to_string(), "M1".to_string())]
.into_iter()
.collect();
let restamped = engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
let t1 = t0 + std::time::Duration::from_secs(1);
engine.set_mutation_clock(std::sync::Arc::new(move || t1));
let mut args = anchor_args(id.clone(), Some(restamped.content_hash.clone()));
args.anchors = vec![anchor_input("c.rs", "h-c")];
let out = engine
.update_entity(args, actor, Some(&client), None)
.unwrap();
assert!(!out.write_id.is_empty(), "anchor-only update commits");
assert_eq!(
out.content_hash, restamped.content_hash,
"anchors never move `_hash`, even across a second boundary"
);
let entity = engine.store().get(&id).unwrap();
assert_eq!(
entity
.metadata
.get("last_modified")
.and_then(|v| v.as_str()),
Some("2026-05-08T12:34:56Z"),
"anchor-only update must not restamp last_modified"
);
}
#[test]
fn malformed_anchor_unset_refuses_and_nothing_is_written() {
let (mut engine, tmp, id, hash) = anchored_engine();
let (actor, client) = cli_actor();
let sidecar_path = tmp.path().join(crate::anchor::ANCHOR_SIDECAR_PATH);
let before = std::fs::read(&sidecar_path).unwrap();
let mut bad = anchor_unset("a.rs");
bad.grain = Some("paragraph".to_string()); let mut args = anchor_args(id.clone(), Some(hash));
args.anchors_unset = vec![bad];
args.anchors = vec![anchor_input("c.rs", "h-c")];
let err = engine
.update_entity(args, actor, Some(&client), None)
.unwrap_err();
assert_eq!(err.code(), crate::anchor::INVALID_ANCHOR_CODE);
assert_eq!(engine.entity_anchors(&id).len(), 2, "no partial apply");
assert_eq!(std::fs::read(&sidecar_path).unwrap(), before);
}
fn bare_args(id: EntityId, hash: Option<String>) -> UpdateEntityArgs {
UpdateEntityArgs {
anchors: Vec::new(),
anchors_unset: Vec::new(),
id,
expected_hash: crate::ExpectedHash::from_token(hash),
sections: IndexMap::new(),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
}
}
#[test]
fn reserved_key_unset_repairs_smuggled_entity_and_set_stays_refused() {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
std::fs::write(
mem_dir.join("smuggled.md"),
"---\ntype: spec\nmem: wrong-mem\nid: bogus-id\n---\n# Smuggled\n\n## Identity\n\nsmuggled identity.\n\n## Purpose\n\nsmuggled purpose.\n",
)
.unwrap();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir.clone()),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
let (actor, client) = cli_actor();
let id = EntityId::new("specs", "smuggled");
let entity = engine.get_entity(&id).expect("fixture boots");
assert!(
entity.metadata.contains_key("mem") && entity.metadata.contains_key("id"),
"fixture must carry the smuggled keys after boot"
);
let hash = entity.content_hash.clone();
for reserved in ["type", "mem", "id"] {
let mut args = bare_args(id.clone(), Some(hash.clone()));
args.metadata
.insert(reserved.to_string(), "resmuggled".to_string());
let err = engine
.update_entity(args, actor, Some(&client), None)
.expect_err("reserved-key set must refuse on update");
assert_eq!(err.code(), "READ_ONLY_FIELD", "key '{reserved}': {err:?}");
}
let mut batch_item = bare_args(id.clone(), Some(hash.clone()));
batch_item
.metadata
.insert("id".to_string(), "resmuggled".to_string());
let batch = engine
.batch_update(vec![(batch_item, None)], actor, Some(&client), false)
.expect("batch returns a result envelope");
assert!(
!batch.applied,
"batch with a reserved-key set must not apply"
);
assert_eq!(batch.failed, 1);
let mut args = bare_args(id.clone(), Some(hash));
args.metadata_unset = vec!["mem".to_string(), "id".to_string()];
let out = engine
.update_entity(args, actor, Some(&client), None)
.expect("reserved-key unset is the sanctioned repair");
assert!(!out.write_id.is_empty(), "repair is a real commit");
assert_eq!(
out.modified_metadata.unset,
vec!["mem".to_string(), "id".to_string()]
);
let entity = engine.get_entity(&id).expect("entity survives repair");
assert!(
!entity.metadata.contains_key("mem") && !entity.metadata.contains_key("id"),
"smuggled keys must be gone from the store"
);
let on_disk = std::fs::read_to_string(mem_dir.join("smuggled.md")).unwrap();
assert!(
!on_disk.contains("wrong-mem") && !on_disk.contains("bogus-id"),
"smuggled keys must be gone from the file: {on_disk}"
);
let mut args = bare_args(id.clone(), Some(entity.content_hash.clone()));
args.sections
.insert("identity".to_string(), "repaired identity".to_string());
engine
.update_entity(args, actor, Some(&client), None)
.expect("post-repair entity round-trips cleanly");
}
#[test]
fn reserved_type_unset_reseeds_and_is_a_noop_on_healthy_entities() {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir.clone()),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
let (actor, client) = cli_actor();
let created = engine
.create_entity(
empty_create_args("specs", "Healthy"),
actor,
Some(&client),
None,
)
.unwrap();
let id = EntityId::new("specs", "healthy");
for key in ["type", "mem", "id"] {
let mut args = bare_args(id.clone(), Some(created.content_hash.clone()));
args.metadata_unset = vec![key.to_string()];
let out = engine
.update_entity(args, actor, Some(&client), None)
.unwrap_or_else(|e| panic!("unset '{key}' on a healthy entity must no-op: {e:?}"));
assert!(
out.write_id.is_empty(),
"unset '{key}' on a healthy entity is a no-op, not a commit"
);
assert!(
out.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
"no-op must carry the UPDATE_NOOP warning for '{key}'"
);
}
let entity = engine.get_entity(&id).unwrap();
assert_eq!(entity.entity_type, "spec");
assert_eq!(
entity.metadata.get("type").and_then(|v| v.as_str()),
Some("spec"),
"the discriminator survives a type unset"
);
}
#[test]
fn declare_relations_refuses_cycle_and_self_loop_like_relate() {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
let (actor, client) = cli_actor();
let alpha = engine
.create_entity(
empty_create_args("specs", "Alpha"),
actor,
Some(&client),
None,
)
.unwrap();
let beta = engine
.create_entity(
empty_create_args("specs", "Beta"),
actor,
Some(&client),
None,
)
.unwrap();
engine
.relate_entity(
crate::engine::RelateEntityArgs {
source: alpha.id.clone(),
target: beta.id.clone(),
rel_type: "PART_OF".to_string(),
remove: false,
expected_hash: None,
description: None,
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let declare = |rel_type: &str, from: &EntityId, to: &EntityId, hash: String| {
let mut args = bare_args(from.clone(), Some(hash));
args.declare_relations = vec![crate::ops::RelateArg {
target: to.clone(),
rel_type: rel_type.to_string(),
description: None,
}];
args
};
let err = engine
.update_entity(
declare("PART_OF", &beta.id, &alpha.id, beta.content_hash.clone()),
actor,
Some(&client),
None,
)
.expect_err("cycle-closing declare_relations must refuse");
assert_eq!(err.code(), "RELATIONSHIP_CYCLE", "{err:?}");
let details = err.details();
assert_eq!(details["rel_type"], "PART_OF");
assert!(details["existing_path"].is_array());
assert!(
engine
.get_entity(&beta.id)
.unwrap()
.relationships
.is_empty(),
"the refused edge must not land"
);
let alpha_hash = engine.get_entity(&alpha.id).unwrap().content_hash.clone();
let err = engine
.update_entity(
declare("USES", &alpha.id, &alpha.id, alpha_hash),
actor,
Some(&client),
None,
)
.expect_err("self-loop declare_relations must refuse");
assert_eq!(err.code(), "RELATIONSHIP_CYCLE", "{err:?}");
engine
.update_entity(
declare(
"PART_OF",
&beta.id,
&EntityId::new("specs", "gamma"),
beta.content_hash.clone(),
),
actor,
Some(&client),
None,
)
.expect("a non-cycle PART_OF declare must land as today");
}