use std::fs;
use std::path::PathBuf;
use contextgraph_conformance::sample_query;
use contextgraph_host::{Envelope, decode_line};
use contextgraph_types::{
ALGORITHM_ED25519, Capabilities, ContentFidelity, ContentRef, ContextFrame, ContextQuery,
ContextQueryResult, DataFlow, EgressScope, ErrorCode, FrameAttestation, FrameEmbedding,
FrameId, FrameKind, FrameVerdict, InclusionProof, InclusionStep, InlineContentRequirement,
PROTOCOL_VERSION, Provenance, ProvenanceAttestation, ProviderInfo, QueryCapability, Relation,
Representation, Transform, Verdict, VerifyRequest, VerifyResponse, budget_tokens,
};
const REGENERATE_ENV: &str = "REGENERATE_VECTORS";
const VECTOR_FILE: &str = "schema/reference-vectors.ndjson";
const DIGEST_A: &str = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
const DIGEST_B: &str = "sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03";
const DIGEST_C: &str = "sha256:6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b";
const ATTESTING_PROVIDER: &str = "repo-indexer";
mod attested {
pub const ROOT: &str =
"sha256:e7c84f0a6b2a7f7a6f14b12baa276af1d9347d82561fa1a797b47a7334660b4f";
pub const ROOT_SIGNATURE: &str = "78439a36f599aeecaa4e36c653b647ec1c02395a4563dfe89efbeff375f21a8ca58de0b7ce6779f3cc93f445fb18a91f6ef8ad0ca651853a17056b3da4633602";
pub const MINIMAL_SIBLING: &str =
"sha256:de4b8191386772610af0fcdb0c9908d38ae1feb5dc65aa0098f82bc18ebfa56e";
pub const MAXIMAL_COMMITMENT: &str =
"sha256:78df51a8d632df287957f71a792aca4628542e9e2ef8ef677e1dea2b67e7c499";
pub const MAXIMAL_SIGNATURE: &str = "1d9bd851829a0c6c3ce5eb4933a1852db7ca09034f092ebd2937c111d48e0f992a28882a6c22b1c8beea92073914e5ffe141b820f2a153bdbb0134ed305c0a09";
pub const MAXIMAL_SIBLING: &str =
"sha256:6b1d8bc0f9f73a6ab9e84ccfaf35af23e83aa32441c75450783c699f9d8b2eae";
pub const ISSUED_AT: &str = "2026-08-29T12:00:00Z";
pub const KEY_ID: &str = "repo-indexer-2026-08";
}
fn attestation(signed_commitment: &str, signature: &str) -> ProvenanceAttestation {
ProvenanceAttestation::new(
signed_commitment,
attested::KEY_ID,
ALGORITHM_ED25519,
ATTESTING_PROVIDER,
signature,
attested::ISSUED_AT,
)
}
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("crate dir has a parent")
.to_path_buf()
}
fn maximal_frame() -> ContextFrame {
let content = "Retry with exponential backoff, at most 3 times.";
let mut frame = ContextFrame::full(
"frame:policy:retry",
FrameKind::Doc,
"Retry policy",
content,
0.875,
budget_tokens(content),
);
frame.content_digest = Some(DIGEST_A.into());
frame.uri = Some("file:///repo/docs/retry-policy.md".into());
frame.content_fidelity = Some(ContentFidelity::Exact);
frame.minimum_content_fidelity = Some(ContentFidelity::Normalized);
frame.inline_content_requirement = Some(InlineContentRequirement::Required);
frame.canonical_token_cost = Some(budget_tokens(content));
frame.tokenizer_ref = Some("openai:o200k_base".into());
frame.valid_from = Some("2026-01-01T00:00:00Z".into());
frame.valid_to = Some("2026-12-31T23:59:59Z".into());
frame.recorded_at = Some("2026-07-21T12:34:56Z".into());
frame.citation_label = Some("retry-policy.md L10-18".into());
frame.provenance = vec![
Provenance {
kind: "file".into(),
uri: Some("file:///repo/docs/retry-policy.md".into()),
range: Some("L10-18".into()),
digest: Some(DIGEST_A.into()),
method: Some("file-read".into()),
by: Some("repo-indexer".into()),
},
Provenance {
kind: "derivation".into(),
uri: Some("contextgraph://repo-indexer/retry-policy".into()),
range: None,
digest: None,
method: Some("summary".into()),
by: Some("repo-indexer".into()),
},
];
frame.embedding = Some(FrameEmbedding {
fingerprint: "bge-small-en-v1.5/384/l2".into(),
vector: Some(vec![0.125, -0.5, 1.0]),
});
frame.relations = vec![Relation {
rel: "doc.documents".into(),
target_uri: "symbol:///repo/src/retry.rs#RetryPolicy".into(),
display_name: Some("RetryPolicy".into()),
}];
frame
}
fn minimal_frame() -> ContextFrame {
let content = "Default arrays are omitted on the wire.";
let mut frame = ContextFrame::full(
"frame:minimal",
FrameKind::Fact,
"Minimal conforming frame",
content,
1.0,
budget_tokens(content),
);
frame.citation_label = Some("minimal vector".into());
frame
}
fn compact_frame() -> ContextFrame {
let distilled = "Retry: exponential backoff, max 3.";
let mut frame = ContextFrame::full(
"frame:policy:retry#compact",
FrameKind::Doc,
"Retry policy (distilled)",
distilled,
0.8,
budget_tokens(distilled),
);
frame.representation = Representation::Compact;
frame.content_digest = Some(DIGEST_B.into());
frame.canonical_content_hash = Some(DIGEST_A.into());
frame.content_fidelity = Some(ContentFidelity::Summarized);
frame.content_ref = Some(ContentRef {
provider_id: "repo-indexer".into(),
uri: "contextgraph-ref://repo-indexer/frame:policy:retry".into(),
expires_at: Some("2026-12-31T23:59:59Z".into()),
});
frame.transform = Some(Transform {
method: "extractive-summary".into(),
implementation: "repo-indexer/distill".into(),
version: "1.0.0".into(),
});
frame.canonical_token_cost = Some(budget_tokens(
"Retry with exponential backoff, at most 3 times.",
));
frame.citation_label = Some("retry-policy.md (distilled)".into());
frame
}
fn reference_frame() -> ContextFrame {
let mut frame = ContextFrame::reference(
"frame:policy:retry#ref",
FrameKind::Doc,
"Retry policy (reference)",
ContentRef {
provider_id: "repo-indexer".into(),
uri: "contextgraph-ref://repo-indexer/frame:policy:retry".into(),
expires_at: None,
},
DIGEST_A,
0.7,
);
frame.content_fidelity = Some(ContentFidelity::Omitted);
frame.inline_content_requirement = Some(InlineContentRequirement::ResolvableReferenceAllowed);
frame.canonical_token_cost = Some(budget_tokens(
"Retry with exponential backoff, at most 3 times.",
));
frame.citation_label = Some("retry-policy.md (reference)".into());
frame
}
fn maximal_capabilities() -> Capabilities {
Capabilities {
query: QueryCapability {
kinds: vec!["doc".into(), "snippet".into()],
},
correlation: true,
graph: true,
embeddings_fingerprint: Some("bge-small-en-v1.5/384/l2".into()),
verify: true,
representations: vec![
Representation::Full,
Representation::Compact,
Representation::Reference,
],
resolve: true,
}
}
fn vectors() -> Vec<(&'static str, Envelope)> {
vec![
(
"handshake",
Envelope::Handshake {
protocol_version: PROTOCOL_VERSION.into(),
},
),
(
"handshake_ack/minimal",
Envelope::HandshakeAck {
attester_keys: vec![],
protocol_version: PROTOCOL_VERSION.into(),
provider: ProviderInfo {
name: "minimal-provider".into(),
version: "0.1.0".into(),
data_flow: DataFlow::default(),
},
capabilities: Capabilities::default(),
},
),
(
"handshake_ack/maximal",
Envelope::HandshakeAck {
attester_keys: vec![],
protocol_version: PROTOCOL_VERSION.into(),
provider: ProviderInfo {
name: "example-docs".into(),
version: "1.0.0".into(),
data_flow: DataFlow {
reads: true,
writes: true,
egress: true,
egress_scopes: vec![
EgressScope::OrgTenant,
EgressScope::ThirdPartyModel,
EgressScope::Custom("acme:vector-store".into()),
],
},
},
capabilities: maximal_capabilities(),
},
),
(
"query/minimal-unfiltered",
Envelope::Query {
id: None,
query: sample_query(),
},
),
(
"query/maximal",
Envelope::Query {
id: Some("req-1".into()),
query: ContextQuery {
goal: "explain the retry policy".into(),
query_text: Some("retry backoff".into()),
embedding: Some(vec![0.1, -0.25, 0.5]),
kinds: vec![FrameKind::Doc, FrameKind::Snippet],
anchors: vec!["file:///repo/src/retry.rs".into()],
max_frames: 8,
max_tokens: 4096,
as_of: Some("2026-07-21T12:34:56Z".into()),
representation_preferences: vec![Representation::Compact, Representation::Full],
},
},
),
(
"frames/empty",
Envelope::Frames {
id: None,
result: ContextQueryResult {
frames: vec![],
truncated: false,
dropped_estimate: None,
..Default::default()
},
},
),
(
"frames/all-representations",
Envelope::Frames {
id: Some("req-1".into()),
result: ContextQueryResult {
frames: vec![
maximal_frame(),
minimal_frame(),
compact_frame(),
reference_frame(),
],
truncated: true,
dropped_estimate: Some(12),
..Default::default()
},
},
),
(
"frames/attested",
Envelope::Frames {
id: Some("req-2".into()),
result: ContextQueryResult {
frames: vec![maximal_frame(), minimal_frame()],
truncated: false,
dropped_estimate: None,
frame_attestations: vec![
FrameAttestation::signed(
FrameId::new(
ATTESTING_PROVIDER,
"frame:policy:retry",
Some(DIGEST_A.into()),
),
attestation(attested::MAXIMAL_COMMITMENT, attested::MAXIMAL_SIGNATURE),
)
.with_inclusion_proof(InclusionProof {
leaf_index: 1,
leaf_count: 2,
path: vec![InclusionStep {
sibling: attested::MAXIMAL_SIBLING.into(),
sibling_is_left: true,
}],
}),
FrameAttestation::proven(
FrameId::new(ATTESTING_PROVIDER, "frame:minimal", None),
InclusionProof {
leaf_index: 0,
leaf_count: 2,
path: vec![InclusionStep {
sibling: attested::MINIMAL_SIBLING.into(),
sibling_is_left: false,
}],
},
),
],
result_attestation: Some(attestation(attested::ROOT, attested::ROOT_SIGNATURE)),
},
},
),
(
"verify",
Envelope::Verify {
request: VerifyRequest::new(vec![
FrameId::new("repo-indexer", "frame:policy:retry", Some(DIGEST_A.into())),
FrameId::new("repo-indexer", "frame:minimal", None),
]),
},
),
(
"verified",
Envelope::Verified {
response: VerifyResponse::new(vec![
FrameVerdict::new(
FrameId::new("repo-indexer", "frame:policy:retry", Some(DIGEST_A.into())),
Verdict::Valid,
),
FrameVerdict::new(
FrameId::new("repo-indexer", "frame:changed", Some(DIGEST_B.into())),
Verdict::Stale {
replacement_digest: Some(DIGEST_C.into()),
},
),
FrameVerdict::new(
FrameId::new("repo-indexer", "frame:changed-unknown-to", None),
Verdict::Stale {
replacement_digest: None,
},
),
FrameVerdict::new(
FrameId::new("repo-indexer", "frame:deleted", None),
Verdict::Gone,
),
FrameVerdict::new(
FrameId::new("repo-indexer", "frame:never-served", None),
Verdict::Unknown,
),
]),
},
),
("shutdown", Envelope::Shutdown),
(
"error/uncoded",
Envelope::Error {
id: None,
code: None,
message: "malformed line".into(),
},
),
(
"error/coded",
Envelope::Error {
id: Some("req-1".into()),
code: Some(ErrorCode::BadRequest),
message: "query.max_tokens must be positive".into(),
},
),
(
"error/unknown-code",
Envelope::Error {
id: None,
code: Some(ErrorCode::Unknown("quota_exhausted".into())),
message: "a code this revision does not define".into(),
},
),
]
}
fn render() -> String {
let mut out = String::new();
for (label, envelope) in vectors() {
let line = serde_json::to_string(&envelope)
.unwrap_or_else(|e| panic!("vector {label} must serialize: {e}"));
out.push_str(&line);
out.push('\n');
}
out
}
#[test]
fn reference_vectors_match_the_committed_file() {
let path = repo_root().join(VECTOR_FILE);
let rendered = render();
if std::env::var_os(REGENERATE_ENV).is_some() {
fs::write(&path, &rendered).expect("write reference vectors");
return;
}
let committed = fs::read_to_string(&path).unwrap_or_else(|e| {
panic!("{VECTOR_FILE} is missing ({e}). Regenerate: {REGENERATE_ENV}=1 cargo test -p contextgraph-conformance --test reference_vectors")
});
if committed == rendered {
return;
}
let labels: Vec<_> = vectors().into_iter().map(|(l, _)| l).collect();
let committed_lines: Vec<_> = committed.lines().collect();
let rendered_lines: Vec<_> = rendered.lines().collect();
for (i, label) in labels.iter().enumerate() {
let old = committed_lines.get(i);
let new = rendered_lines.get(i);
if old != new {
panic!(
"reference vector `{label}` (line {}) drifted from {VECTOR_FILE}.\n committed: {}\n serialized: {}\n\nIf the wire change is intended, regenerate and re-validate:\n {REGENERATE_ENV}=1 cargo test -p contextgraph-conformance --test reference_vectors\n python3 schema/validate-examples.py",
i + 1,
old.unwrap_or(&"<missing>"),
new.unwrap_or(&"<missing>"),
);
}
}
assert_eq!(
committed_lines.len(),
rendered_lines.len(),
"{VECTOR_FILE} has a different number of vectors than the generator; regenerate with {REGENERATE_ENV}=1"
);
}
#[test]
fn every_vector_round_trips_through_the_wire_decoder() {
for (label, envelope) in vectors() {
let line = serde_json::to_string(&envelope).expect("serialize");
let decoded = decode_line(&line)
.unwrap_or_else(|e| panic!("vector {label} must decode back into an envelope: {e}"));
assert_eq!(decoded, envelope, "vector {label} must round-trip exactly");
}
}
#[test]
fn vectors_are_conformant() {
for (label, envelope) in vectors() {
let Envelope::Frames { result, .. } = &envelope else {
continue;
};
for frame in &result.frames {
let id = &frame.id;
assert!(
frame.representation_invariants().is_ok(),
"vector {label} frame {id} must satisfy its representation invariants: {:?}",
frame.representation_invariants()
);
assert!(
frame.declares_honest_token_cost(),
"vector {label} frame {id} must declare an honest B3 token_cost \
(declared {}, canonical {})",
frame.token_cost,
frame.expected_inline_token_cost(),
);
assert!(
frame.has_valid_score(),
"vector {label} frame {id} must have a score in [0, 1]"
);
for provenance in &frame.provenance {
if provenance.is_file_provenance() {
assert!(
provenance.has_well_formed_digest(),
"vector {label} frame {id} carries file provenance without an \
F5 digest — the vectors must model the rule, not dodge it"
);
}
}
}
}
}