#![cfg(test)]
#[test]
fn scaffolded_scope_is_medium_shaped_and_never_decorative() {
let scope_of = |medium: MediumType| {
scaffold_binding(ScaffoldParams {
destination_mem: "m",
source_name: "s",
pointer: "p",
medium_type: medium,
intent: None,
additional_deny_paths: Vec::new(),
})
.binding
.sources[0]
.scope
.iter()
.map(|r| r.path.clone())
.collect::<Vec<_>>()
};
assert_eq!(scope_of(MediumType::Codebase), vec!["**/*".to_string()]);
assert_eq!(scope_of(MediumType::Filesystem), vec!["**/*".to_string()]);
assert_eq!(scope_of(MediumType::Git), vec!["**/*".to_string()]);
assert_eq!(scope_of(MediumType::Graph), vec!["*".to_string()]);
assert!(
crate::source_scope::parse_entity_selector("*").is_some(),
"the graph scaffold writes a selector the parser accepts"
);
assert!(
scope_of(MediumType::Web).is_empty(),
"a web facet carries no scope rather than one nothing interprets"
);
}
use super::*;
use crate::pipeline::PatternMode;
fn build_op() -> BuildOperation {
BuildOperation {
mode: BuildMode::Discovery,
trigger: IngestTrigger::Loop,
batch_size: 20,
post_actions: None,
}
}
fn allow(path: &str) -> PatternEntry {
PatternEntry {
path: path.to_string(),
mode: PatternMode::Allow,
}
}
fn source(
name: &str,
medium_type: MediumType,
pointer: &str,
scope: Vec<PatternEntry>,
preparation: Option<&str>,
change_detection: Option<&str>,
) -> Source {
Source {
name: name.to_string(),
medium_type,
pointer: pointer.to_string(),
change_detection: change_detection.map(str::to_string),
scope,
engagement: None,
preparation: preparation.map(str::to_string),
}
}
fn codebase_source() -> Source {
source(
"source-tree",
MediumType::Codebase,
"../public",
vec![allow("../public/**/*.rs")],
None,
None,
)
}
fn binding() -> Binding {
Binding {
version: BINDING_VERSION,
intent: Some("prose for the agent".to_string()),
sources: vec![codebase_source()],
reference_mems: vec!["engine".to_string()],
destination_mem: "plugin".to_string(),
deny_paths: vec!["VISION.md".to_string(), "dev/**".to_string()],
coverage_semantics: None,
rules: Some(serde_json::json!({ "routing": "…" })),
prune: None,
operations: Operations {
build: Some(build_op()),
sync: Some(SyncOperation {
trigger: IngestTrigger::Manual,
batch_size: 20,
}),
verify: Some(VerifyOperation {
trigger: IngestTrigger::Manual,
batch_size: 20,
adjudication_cap: DEFAULT_ADJUDICATION_CAP,
full_resync_every: DEFAULT_FULL_RESYNC_EVERY,
}),
},
}
}
#[test]
fn binding_round_trips() {
let b = binding();
let json = serde_json::to_string(&b).unwrap();
let back: Binding = serde_json::from_str(&json).unwrap();
assert_eq!(back, b);
}
#[test]
fn plan_shaped_v2_json_deserializes() {
let src = r#"{
"version": 2,
"intent": "prose the building agent reads before every run",
"sources": [
{
"name": "source-tree",
"type": "codebase",
"pointer": "../public",
"change_detection": "auto",
"scope": [
{ "path": "../public/**/*.rs", "mode": "allow" },
{ "path": "../public/target/**", "mode": "deny" }
]
}
],
"reference_mems": ["engineering"],
"destination_mem": "engine",
"deny_paths": ["../dev/**"],
"coverage_semantics": "exhaustive",
"operations": {
"build": { "mode": "discovery", "trigger": "loop", "batch_size": 20 },
"sync": { "trigger": "loop", "batch_size": 20 },
"verify": { "trigger": "loop", "batch_size": 20,
"adjudication_cap": 50, "full_resync_every": 20 }
}
}"#;
let b: Binding = serde_json::from_str(src).unwrap();
assert_eq!(b.version, 2);
assert_eq!(b.destination_mem, "engine");
assert_eq!(b.sources.len(), 1);
let s = &b.sources[0];
assert_eq!(s.name, "source-tree");
assert_eq!(s.medium_type, MediumType::Codebase);
assert_eq!(s.pointer, "../public");
assert_eq!(s.change_detection.as_deref(), Some("auto"));
assert_eq!(s.scope.len(), 2);
assert_eq!(b.reference_mems, vec!["engineering".to_string()]);
assert_eq!(b.coverage_semantics, Some(CoverageSemantics::Exhaustive));
assert_eq!(
b.operations.build.as_ref().unwrap().mode,
BuildMode::Discovery
);
assert!(b.operations.sync.is_some());
assert_eq!(b.operations.verify.as_ref().unwrap().adjudication_cap, 50);
}
#[test]
fn coverage_defaults_and_one_shot_wire_form() {
let src = r#"{
"version": 2,
"destination_mem": "m",
"operations": { "build": { "mode": "one-shot", "trigger": "manual", "batch_size": 5 } }
}"#;
let b: Binding = serde_json::from_str(src).unwrap();
assert_eq!(b.coverage_semantics, None, "absent = not stated");
assert_eq!(
b.operations.build.as_ref().unwrap().mode,
BuildMode::OneShot
);
assert!(b.operations.sync.is_none());
assert!(b.operations.verify.is_none());
assert_eq!(
serde_json::to_string(&BuildMode::OneShot).unwrap(),
r#""one-shot""#
);
}
#[test]
fn verify_tier3_knobs_default_and_round_trip() {
let src = r#"{
"version": 2,
"destination_mem": "m",
"operations": {
"build": { "mode": "discovery", "trigger": "loop", "batch_size": 20 },
"verify": { "trigger": "manual", "batch_size": 20 }
}
}"#;
let b: Binding = serde_json::from_str(src).unwrap();
let v = b.operations.verify.as_ref().unwrap();
assert_eq!(v.adjudication_cap, DEFAULT_ADJUDICATION_CAP);
assert_eq!(v.full_resync_every, DEFAULT_FULL_RESYNC_EVERY);
let explicit = VerifyOperation {
trigger: IngestTrigger::Manual,
batch_size: 10,
adjudication_cap: 7,
full_resync_every: 3,
};
let json = serde_json::to_string(&explicit).unwrap();
let back: VerifyOperation = serde_json::from_str(&json).unwrap();
assert_eq!(back, explicit);
assert!(json.contains("adjudication_cap"));
assert!(json.contains("full_resync_every"));
}
#[test]
fn tier3_knobs_do_not_change_the_hash() {
let base = hash_binding(&binding());
let mut tuned = binding();
let v = tuned.operations.verify.as_mut().unwrap();
v.adjudication_cap = 999;
v.full_resync_every = 1;
assert_eq!(
base,
hash_binding(&tuned),
"tier-3 verify knobs are excluded from hash(D)"
);
}
#[test]
fn refinement_mode_is_rejected() {
let src = r#"{
"version": 2,
"destination_mem": "m",
"operations": { "build": { "mode": "refinement", "trigger": "loop", "batch_size": 20 } }
}"#;
let err = serde_json::from_str::<Binding>(src).unwrap_err();
assert!(
err.to_string().contains("refinement") || err.to_string().contains("unknown variant"),
"unexpected error: {err}"
);
}
#[test]
fn version_is_required() {
let src = r#"{
"destination_mem": "m",
"operations": { "build": { "mode": "discovery", "trigger": "loop", "batch_size": 20 } }
}"#;
assert!(serde_json::from_str::<Binding>(src).is_err());
}
#[test]
fn hash_is_stable_and_recomputable() {
let b = binding();
let h1 = hash_binding(&b);
let h2 = hash_binding(&b);
assert_eq!(h1, h2);
assert_eq!(h1.len(), 64);
assert!(
h1.chars()
.all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase())
);
}
#[test]
fn changing_a_source_pattern_changes_the_hash() {
let base = hash_binding(&binding());
let mut changed = binding();
changed.sources[0].scope = vec![allow("../public/**/*.md")];
assert_ne!(base, hash_binding(&changed));
}
#[test]
fn changing_a_source_pointer_changes_the_hash() {
let base = hash_binding(&binding());
let mut changed = binding();
changed.sources[0].pointer = "../elsewhere".to_string();
assert_ne!(base, hash_binding(&changed));
}
#[test]
fn scheduling_knobs_do_not_change_the_hash() {
let base = hash_binding(&binding());
let mut b_trigger = binding();
b_trigger.operations.build.as_mut().unwrap().trigger = IngestTrigger::Manual;
assert_eq!(base, hash_binding(&b_trigger), "trigger is excluded");
let mut b_batch = binding();
b_batch.operations.build.as_mut().unwrap().batch_size = 999;
assert_eq!(base, hash_binding(&b_batch), "batch_size is excluded");
let mut b_post = binding();
b_post.operations.build.as_mut().unwrap().post_actions =
Some(serde_json::json!({ "archive_source": false }));
assert_eq!(base, hash_binding(&b_post), "post_actions is excluded");
let mut b_sync = binding();
b_sync.operations.sync = None;
assert_eq!(base, hash_binding(&b_sync), "sync block is excluded");
let mut b_engage = binding();
b_engage.sources[0].engagement = Some(serde_json::json!({ "readVerb": "Study" }));
assert_eq!(base, hash_binding(&b_engage), "engagement is excluded");
}
#[test]
fn changing_build_mode_changes_the_hash() {
let base = hash_binding(&binding());
let mut b = binding();
b.operations.build.as_mut().unwrap().mode = BuildMode::OneShot;
assert_ne!(base, hash_binding(&b));
}
#[test]
fn absent_build_deserializes_and_hashes() {
let src = r#"{
"version": 2,
"destination_mem": "m",
"operations": { "verify": { "trigger": "manual", "batch_size": 5 } }
}"#;
let b: Binding = serde_json::from_str(src).unwrap();
assert!(b.operations.build.is_none(), "absent build parses to None");
let h = hash_binding(&b);
assert_eq!(h.len(), 64);
}
#[test]
fn capability_matrix_rows() {
let web = medium_capabilities(MediumType::Web);
assert!(!web.enumerable && !web.change_signal && !web.base_version_retrievable);
assert!(!web.glob_deny_legal);
assert_eq!(web.anchor_namespace, "url");
let graph = medium_capabilities(MediumType::Graph);
assert!(graph.enumerable && graph.change_signal && graph.base_version_retrievable);
assert!(!graph.glob_deny_legal, "graph namespace is not path-shaped");
assert_eq!(graph.anchor_namespace, "entity");
for ty in [
MediumType::Codebase,
MediumType::Filesystem,
MediumType::Git,
] {
let c = medium_capabilities(ty);
assert!(c.enumerable && c.change_signal && c.base_version_retrievable);
assert!(c.glob_deny_legal, "{ty:?} allows glob deny_paths");
}
assert_eq!(
medium_capabilities(MediumType::Git).anchor_namespace,
"path+commit"
);
}
#[test]
fn empty_and_duplicate_source_names_refuse() {
let mut b = binding();
b.deny_paths.clear();
b.sources = vec![
source("", MediumType::Codebase, "../a", vec![], None, None),
source("dup", MediumType::Codebase, "../b", vec![], None, None),
source("dup", MediumType::Codebase, "../c", vec![], None, None),
];
let errs = validate_binding(&b).unwrap_err();
assert!(
errs.iter()
.any(|e| matches!(e, CapabilityError::EmptySourceName)),
"expected EmptySourceName, got {errs:?}"
);
assert!(
errs.iter().any(|e| matches!(
e,
CapabilityError::DuplicateSourceName { name } if name == "dup"
)),
"expected DuplicateSourceName, got {errs:?}"
);
}
#[test]
fn sync_and_verify_over_web_refuse() {
let mut b = binding();
b.deny_paths.clear();
b.sources = vec![source(
"web-source",
MediumType::Web,
"https://example.com",
vec![],
None,
None,
)];
let errs = validate_binding(&b).unwrap_err();
let ops: Vec<&str> = errs
.iter()
.filter_map(|e| match e {
CapabilityError::OperationOutOfScope { operation, .. } => Some(*operation),
_ => None,
})
.collect();
assert!(ops.contains(&"sync"), "sync refused: {errs:?}");
assert!(ops.contains(&"verify"), "verify refused: {errs:?}");
}
#[test]
fn glob_deny_over_graph_refuses() {
let mut b = binding();
b.operations.sync = None;
b.operations.verify = None;
b.deny_paths = vec!["some/**".to_string()];
b.sources = vec![source(
"graph-source",
MediumType::Graph,
"home",
vec![],
None,
None,
)];
let errs = validate_binding(&b).unwrap_err();
assert!(
errs.iter()
.any(|e| matches!(e, CapabilityError::GlobDenyIllegal { .. })),
"expected GlobDenyIllegal, got {errs:?}"
);
}
#[test]
fn unregistered_preparation_refuses() {
let mut b = binding();
b.operations.sync = None;
b.operations.verify = None;
b.deny_paths.clear();
b.sources = vec![source(
"manual-pages",
MediumType::Filesystem,
"../docs",
vec![],
Some("pdf-to-markdown"),
None,
)];
let errs = validate_binding(&b).unwrap_err();
let refusal = errs
.iter()
.find(|e| {
matches!(
e,
CapabilityError::PreparationUnsupported { preparation, impl_version, .. }
if preparation == "pdf-to-markdown" && *impl_version == PREPARATION_IMPL_VERSION
)
})
.unwrap_or_else(|| panic!("expected PreparationUnsupported, got {errs:?}"));
let msg = refusal.to_string();
assert!(
msg.contains("not in this engine's preparation registry"),
"{msg}"
);
assert!(
msg.contains("entity-load-bearing"),
"names the registered set: {msg}"
);
assert!(
!msg.contains("facet"),
"the retired noun stays retired: {msg}"
);
}
#[test]
fn registered_preparation_validates_over_its_namespace_only() {
let mut ok = binding();
ok.deny_paths.clear();
ok.sources = vec![source(
"claims",
MediumType::Graph,
"home",
vec![allow("*")],
Some(crate::preparation::ENTITY_LOAD_BEARING),
None,
)];
assert!(
validate_binding(&ok).is_ok(),
"registered preparation over its namespace validates clean: {:?}",
validate_binding(&ok)
);
let mut mismatch = binding();
mismatch.sources = vec![source(
"source-tree",
MediumType::Codebase,
"../public",
vec![allow("**/*.rs")],
Some(crate::preparation::ENTITY_LOAD_BEARING),
None,
)];
let errs = validate_binding(&mismatch).unwrap_err();
assert!(
errs.iter().any(|e| matches!(
e,
CapabilityError::PreparationGrainMismatch { preparation, anchor_namespace, .. }
if preparation == crate::preparation::ENTITY_LOAD_BEARING && *anchor_namespace == "path"
)),
"expected PreparationGrainMismatch, got {errs:?}"
);
assert!(
!errs
.iter()
.any(|e| matches!(e, CapabilityError::PreparationUnsupported { .. })),
"a registered identifier is never reported as unregistered"
);
}
#[test]
fn impl_version_is_hashed_into_every_binding() {
let plain = binding();
assert!(plain.sources.iter().all(|s| s.preparation.is_none()));
let live = hash_binding(&plain);
assert_eq!(
live,
hash_binding_at_impl_version(&plain, PREPARATION_IMPL_VERSION)
);
assert_ne!(
live,
hash_binding_at_impl_version(&plain, 0),
"the pre-registry generation's hash differs from the live one"
);
assert_ne!(
live,
hash_binding_at_impl_version(&plain, PREPARATION_IMPL_VERSION + 1)
);
let mut prepared = plain.clone();
prepared.sources[0].preparation = Some(crate::preparation::ENTITY_LOAD_BEARING.to_string());
assert_ne!(
hash_binding(&prepared),
live,
"the identifier is hashed too"
);
}
#[test]
fn legal_combinations_validate_clean() {
for ty in [
MediumType::Codebase,
MediumType::Filesystem,
MediumType::Git,
] {
let mut b = binding();
b.sources = vec![source(
"f",
ty,
"../src",
vec![allow("../src/**")],
None,
None,
)];
assert!(
validate_binding(&b).is_ok(),
"{ty:?} build+sync+verify should validate clean"
);
}
let mut graph_binding = binding();
graph_binding.deny_paths.clear();
graph_binding.sources = vec![source("g", MediumType::Graph, "home", vec![], None, None)];
assert!(
validate_binding(&graph_binding).is_ok(),
"graph build+sync+verify with no glob deny should validate clean"
);
}
#[test]
fn prune_block_is_additive_and_round_trips() {
let src = r#"{
"version": 2,
"destination_mem": "m",
"operations": { "build": { "mode": "discovery", "trigger": "loop", "batch_size": 20 } }
}"#;
let b: Binding = serde_json::from_str(src).unwrap();
assert!(b.prune.is_none(), "absent prune parses to None");
let enabled = r#"{
"version": 2,
"destination_mem": "m",
"prune": {},
"operations": { "build": { "mode": "discovery", "trigger": "loop", "batch_size": 20 } }
}"#;
let b: Binding = serde_json::from_str(enabled).unwrap();
assert_eq!(b.prune, Some(PruneConfig::default()));
let json = serde_json::to_string(&PruneConfig::default()).unwrap();
assert_eq!(json, "{}");
let legacy = r#"{
"version": 2,
"destination_mem": "m",
"prune": { "guarantee": "never-clobber" },
"operations": { "build": { "mode": "discovery", "trigger": "loop", "batch_size": 20 } }
}"#;
let b: Binding = serde_json::from_str(legacy).unwrap();
assert_eq!(b.prune, Some(PruneConfig::default()));
}
#[test]
fn prune_does_not_change_the_hash() {
let base = hash_binding(&binding());
let mut pruned = binding();
pruned.prune = Some(PruneConfig::default());
assert_eq!(
base,
hash_binding(&pruned),
"prune policy is excluded from hash(D)"
);
}
#[test]
fn prune_validates_clean_over_every_medium() {
let mut nc = binding();
nc.prune = Some(PruneConfig::default());
assert!(validate_binding(&nc).is_ok());
let mut cf = binding();
cf.operations.sync = None;
cf.operations.verify = None;
cf.deny_paths.clear();
cf.prune = Some(PruneConfig::default());
cf.sources = vec![source(
"web-source",
MediumType::Web,
"https://example.com",
vec![],
None,
None,
)];
assert!(validate_binding(&cf).is_ok());
}
#[test]
fn web_build_only_validates_clean() {
let mut b = binding();
b.operations.sync = None;
b.operations.verify = None;
b.deny_paths.clear();
b.sources = vec![source(
"web-source",
MediumType::Web,
"https://example.com",
vec![],
None,
None,
)];
assert!(validate_binding(&b).is_ok());
}
fn web_source(name: &str) -> Source {
source(
name,
MediumType::Web,
"https://example.test",
vec![],
None,
None,
)
}
#[test]
fn coverage_resolves_per_medium_when_undeclared() {
let enumerable = binding();
assert_eq!(enumerable.coverage_semantics, None);
let eff = effective_coverage_semantics(&enumerable);
assert_eq!(eff.value, CoverageSemantics::Exhaustive);
assert!(!eff.declared, "resolved, not declared");
validate_binding(&enumerable).expect("undeclared over enumerable validates");
let mut mixed = binding();
mixed.sources.push(web_source("front"));
mixed.operations.sync = None;
mixed.operations.verify = None;
mixed.deny_paths.clear();
let eff = effective_coverage_semantics(&mixed);
assert_eq!(eff.value, CoverageSemantics::Curated);
assert!(!eff.declared);
validate_binding(&mixed).expect("undeclared over web validates (resolves, never refuses)");
let mut curated = mixed.clone();
curated.coverage_semantics = Some(CoverageSemantics::Curated);
validate_binding(&curated).expect("explicit curated validates over any medium");
let eff = effective_coverage_semantics(&curated);
assert_eq!(eff.value, CoverageSemantics::Curated);
assert!(eff.declared);
}
#[test]
fn explicit_exhaustive_over_non_enumerable_refuses() {
let mut only = binding();
only.sources = vec![web_source("front")];
only.operations.sync = None;
only.operations.verify = None;
only.deny_paths.clear();
only.coverage_semantics = Some(CoverageSemantics::Exhaustive);
let errs = validate_binding(&only).expect_err("must refuse");
assert_eq!(errs.len(), 1, "only this refusal: {errs:?}");
match &errs[0] {
CapabilityError::CoverageExhaustiveUnsupported {
source_name,
medium_type,
} => {
assert_eq!(source_name, "front");
assert_eq!(medium_type, "web");
}
other => panic!("expected CoverageExhaustiveUnsupported, got {other:?}"),
}
let msg = errs[0].to_string();
assert!(
msg.contains("'front'") && msg.contains("'web'") && msg.contains("curated"),
"refusal names source, medium, and the curated remedy: {msg}"
);
let mut multi = binding();
multi.sources = vec![web_source("front")];
multi.operations.verify = None;
multi.deny_paths.clear();
multi.coverage_semantics = Some(CoverageSemantics::Exhaustive);
assert!(multi.operations.sync.is_some(), "fixture declares sync");
let errs = validate_binding(&multi).expect_err("must refuse");
assert!(
errs.iter()
.any(|e| matches!(e, CapabilityError::CoverageExhaustiveUnsupported { .. })),
"coverage refusal present: {errs:?}"
);
assert!(
errs.iter()
.any(|e| matches!(e, CapabilityError::OperationOutOfScope { .. })),
"reported alongside the sync refusal, not replacing it: {errs:?}"
);
let mut ok = binding();
ok.coverage_semantics = Some(CoverageSemantics::Exhaustive);
validate_binding(&ok).expect("explicit exhaustive over enumerable validates");
}
#[test]
fn hash_serialises_the_resolved_coverage_value() {
let undeclared = binding();
let mut declared = binding();
declared.coverage_semantics = Some(CoverageSemantics::Exhaustive);
assert_eq!(
hash_binding(&undeclared),
hash_binding(&declared),
"undeclared over enumerable keeps the pre-optionality hash"
);
let mut curated = binding();
curated.coverage_semantics = Some(CoverageSemantics::Curated);
assert_ne!(hash_binding(&undeclared), hash_binding(&curated));
let mut web_undeclared = binding();
web_undeclared.sources = vec![web_source("front")];
let mut web_curated = web_undeclared.clone();
web_curated.coverage_semantics = Some(CoverageSemantics::Curated);
assert_eq!(
hash_binding(&web_undeclared),
hash_binding(&web_curated),
"undeclared over web resolves (and hashes) as curated"
);
}