use khive_types::EdgeRelation;
use crate::harness::{run_certificate, Fixture};
enum CertDisposition {
SurvivesAll,
FailsEliminator {
family: &'static str,
kept_because: &'static str,
},
}
struct CertifiedRelation {
relation: &'static str,
fixtures: &'static [Fixture],
}
struct SystemRoleException {
relation: &'static str,
adr: &'static str,
role: &'static str,
disposition: CertDisposition,
}
const CERTIFIED_RELATIONS: &[CertifiedRelation] = &[];
const SYSTEM_ROLE_EXCEPTIONS: &[SystemRoleException] = &[
SystemRoleException {
relation: "contains",
adr: "ADR-002",
role: "structural containment role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "part_of",
adr: "ADR-002",
role: "structural composition role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "instance_of",
adr: "ADR-002",
role: "taxonomic instantiation role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "extends",
adr: "ADR-002",
role: "derivation / refinement role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "variant_of",
adr: "ADR-002",
role: "derivation / alternative role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "introduced_by",
adr: "ADR-002",
role: "provenance attribution role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "supersedes",
adr: "ADR-002",
role: "versioning / replacement role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "derived_from",
adr: "ADR-002",
role: "artifact provenance role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "precedes",
adr: "ADR-002",
role: "temporal ordering role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "depends_on",
adr: "ADR-002",
role: "dependency role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "enables",
adr: "ADR-002",
role: "enablement role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "implements",
adr: "ADR-002",
role: "implementation role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "competes_with",
adr: "ADR-002",
role: "competitive lateral role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "composed_with",
adr: "ADR-002",
role: "compositional lateral role",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "annotates",
adr: "ADR-002",
role: "cross-substrate annotation role (only note-to-entity/note)",
disposition: CertDisposition::SurvivesAll,
},
SystemRoleException {
relation: "supports",
adr: "ADR-055",
role: "epistemic role (kept with refutes as a symmetric pair)",
disposition: CertDisposition::FailsEliminator {
family: "Po",
kept_because: "the epistemic layer requires polarity to be a \
first-class, relation-level distinction that \
planners, indexes, federation, and the public API \
can branch on directly, not a value buried in an \
open metadata blob that 3.3% of edges populate \
(ADR-055; ADR-076 §D3)",
},
},
SystemRoleException {
relation: "refutes",
adr: "ADR-055",
role: "epistemic role (kept with supports as a symmetric pair)",
disposition: CertDisposition::FailsEliminator {
family: "Po",
kept_because: "the epistemic layer requires polarity to be a \
first-class, relation-level distinction that \
planners, indexes, federation, and the public API \
can branch on directly, not a value buried in an \
open metadata blob that 3.3% of edges populate \
(ADR-055; ADR-076 §D3)",
},
},
];
#[test]
fn system_role_exceptions_have_non_empty_adr_and_role() {
for entry in SYSTEM_ROLE_EXCEPTIONS {
assert!(
!entry.adr.is_empty(),
"SYSTEM_ROLE_EXCEPTIONS entry '{}' has an empty `adr` field; \
every exception must cite the ADR that justifies it",
entry.relation,
);
assert!(
!entry.role.is_empty(),
"SYSTEM_ROLE_EXCEPTIONS entry '{}' has an empty `role` field; \
every exception must declare a system role",
entry.relation,
);
}
}
#[test]
fn fail_eliminator_dispositions_have_valid_family_and_non_empty_justification() {
const VALID_CODES: &[&str] = &["Cv", "Er", "At", "Po", "Ch", "Mv", "Sr"];
for entry in SYSTEM_ROLE_EXCEPTIONS {
if let CertDisposition::FailsEliminator {
family,
kept_because,
} = &entry.disposition
{
assert!(
VALID_CODES.contains(family),
"SYSTEM_ROLE_EXCEPTIONS entry '{}' has FailsEliminator with unknown \
family '{}'; family must be one of {:?} (ADR-076 §D2 eliminator codes)",
entry.relation,
family,
VALID_CODES,
);
assert!(
!kept_because.is_empty(),
"SYSTEM_ROLE_EXCEPTIONS entry '{}' has FailsEliminator with an empty \
`kept_because`; a failing eliminator MUST carry a system-role \
justification (ADR-076 §D3: the declaration is the arbiter)",
entry.relation,
);
}
}
}
#[test]
fn certified_relations_pass_full_certificate() {
for entry in CERTIFIED_RELATIONS {
run_certificate(entry.relation, entry.fixtures);
}
}
#[test]
fn survives_all_disposition_is_exactly_the_15_grandfathered_base_relations() {
const EXPECTED: &[&str] = &[
"annotates",
"competes_with",
"composed_with",
"contains",
"depends_on",
"derived_from",
"enables",
"extends",
"implements",
"instance_of",
"introduced_by",
"part_of",
"precedes",
"supersedes",
"variant_of",
];
let mut actual: Vec<&str> = SYSTEM_ROLE_EXCEPTIONS
.iter()
.filter(|e| matches!(e.disposition, CertDisposition::SurvivesAll))
.map(|e| e.relation)
.collect();
actual.sort_unstable();
assert_eq!(
actual, EXPECTED,
"the set of relations with SurvivesAll disposition must equal exactly the 15 \
grandfathered ADR-002 base relations; adding a new SurvivesAll entry requires \
updating this closed list — non-grandfathered relations must go through the \
certificate admission path (CERTIFIED_RELATIONS) or declare FailsEliminator \
with a valid family code and a non-empty kept_because justification"
);
}
#[test]
fn every_edge_relation_has_cert_entry_or_system_role_exception() {
for rel in EdgeRelation::ALL {
let name = rel.as_str();
let certified = CERTIFIED_RELATIONS.iter().any(|e| e.relation == name);
let excepted = SYSTEM_ROLE_EXCEPTIONS.iter().any(|e| e.relation == name);
assert!(
certified || excepted,
"EdgeRelation '{name}' is in EdgeRelation::ALL but has neither a \
non-redundancy certificate in CERTIFIED_RELATIONS nor a system-role \
exception in SYSTEM_ROLE_EXCEPTIONS (ADR-076 §D2). \
To add a new relation via the certificate path: run its certificate \
module to pass all seven eliminators and add it to CERTIFIED_RELATIONS. \
To add via the system-role path (ADR-076 §D1/§D3): add a \
SystemRoleException with non-empty `adr`, `role`, and a \
`disposition` — either SurvivesAll or FailsEliminator {{ family, \
kept_because }} for any eliminator the relation cannot defeat.",
);
}
}