use std::path::{Path, PathBuf};
const THIS_FILE: &str = "src/invariants.rs";
fn rust_sources() -> Vec<(String, String)> {
let mut out = raw_rust_sources();
let hidden = test_only_files(&out);
out.retain(|(path, _)| !hidden.contains(path));
out
}
fn raw_rust_sources() -> Vec<(String, String)> {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let mut out = Vec::new();
let mut stack: Vec<PathBuf> = vec![root.join("src")];
while let Some(dir) = stack.pop() {
for entry in std::fs::read_dir(&dir).expect("src/ subdirectory is readable") {
let path = entry.expect("readable directory entry").path();
if path.is_dir() {
stack.push(path);
continue;
}
if path.extension().and_then(|e| e.to_str()) != Some("rs") {
continue;
}
let rel = path
.strip_prefix(root)
.expect("path is under CARGO_MANIFEST_DIR")
.to_string_lossy()
.replace('\\', "/");
if rel == THIS_FILE {
continue;
}
let text = std::fs::read_to_string(&path).expect("source file is valid UTF-8");
out.push((rel, text));
}
}
out.sort();
out
}
fn test_only_files(all: &[(String, String)]) -> std::collections::BTreeSet<String> {
let mut hidden = std::collections::BTreeSet::new();
for (file, src) in all {
let lines: Vec<&str> = src.lines().collect();
for i in 0..lines.len().saturating_sub(1) {
if lines[i].trim() != "#[cfg(test)]" {
continue;
}
let Some(name) = lines[i + 1]
.trim()
.strip_prefix("mod ")
.and_then(|s| s.strip_suffix(';'))
else {
continue;
};
let name = name.trim();
if name.is_empty() || !name.chars().all(|c| c.is_alphanumeric() || c == '_') {
continue;
}
for candidate in module_file_candidates(file, name) {
if all.iter().any(|(p, _)| *p == candidate) {
hidden.insert(candidate);
}
}
}
}
hidden
}
fn module_file_candidates(file: &str, name: &str) -> Vec<String> {
let dir = if file == "src/lib.rs" || file == "src/main.rs" {
"src".to_string()
} else if let Some(d) = file.strip_suffix("/mod.rs") {
d.to_string()
} else {
file.strip_suffix(".rs").unwrap_or(file).to_string()
};
vec![format!("{dir}/{name}.rs"), format!("{dir}/{name}/mod.rs")]
}
#[cfg(test)]
mod test_only_files_checks {
use super::{module_file_candidates, raw_rust_sources, test_only_files};
#[test]
fn every_cfg_test_mod_declaration_is_the_two_line_form() {
let mut split_forms = Vec::new();
for (file, src) in raw_rust_sources() {
let lines: Vec<&str> = src.lines().collect();
for i in 0..lines.len() {
if lines[i].trim() != "#[cfg(test)]" {
continue;
}
let is_mod_decl = |l: &str| {
let l = l.trim();
l.strip_prefix("mod ")
.and_then(|s| s.strip_suffix(';'))
.is_some()
};
let adjacent = lines.get(i + 1).is_some_and(|l| is_mod_decl(l));
if adjacent {
continue;
}
for l in lines.iter().skip(i + 2).take(4) {
if is_mod_decl(l) {
split_forms.push(format!("{file}:{}", i + 1));
break;
}
}
}
}
assert!(
split_forms.is_empty(),
"SPLIT #[cfg(test)] mod DECLARATION(S) — test_only_files() only \
matches the immediate two-line form and would silently miss \
these, scanning the file they gate as production code:\n\
{split_forms:#?}"
);
}
#[test]
fn no_hidden_file_declares_a_further_submodule() {
let all = raw_rust_sources();
let hidden = test_only_files(&all);
let mut nested = Vec::new();
for (file, src) in &all {
if !hidden.contains(file) {
continue;
}
for (lineno, line) in src.lines().enumerate() {
let l = line.trim();
let Some(rest) = l.strip_prefix("mod ") else {
continue;
};
if rest.trim_end().ends_with(';') {
nested.push(format!("{file}:{}: {l}", lineno + 1));
}
}
}
assert!(
nested.is_empty(),
"HIDDEN FILE DECLARES A FURTHER FILE — test_only_files() does not \
recurse, so a submodule of a hidden file is scanned as \
production code unless it carries its own #[cfg(test)] gate:\n\
{nested:#?}"
);
}
#[test]
fn at_least_the_known_hidden_files_are_detected() {
let all = raw_rust_sources();
let hidden = test_only_files(&all);
for expected in [
"src/mcp/write_path_equivalence.rs",
"src/hooks/compliance.rs",
"src/store/db/tests.rs",
"src/mcp/dispatch_v2/tests.rs",
] {
assert!(
hidden.contains(expected),
"expected {expected} to be detected as test-only; \
test_only_files() returned {hidden:#?}"
);
}
}
#[test]
fn module_file_candidates_resolves_both_shapes() {
assert_eq!(
module_file_candidates("src/lib.rs", "invariants"),
vec!["src/invariants.rs", "src/invariants/mod.rs"]
);
assert_eq!(
module_file_candidates("src/store/db/mod.rs", "tests"),
vec!["src/store/db/tests.rs", "src/store/db/tests/mod.rs"]
);
assert_eq!(
module_file_candidates("src/mcp/dispatch_v2/mod.rs", "tests"),
vec![
"src/mcp/dispatch_v2/tests.rs",
"src/mcp/dispatch_v2/tests/mod.rs"
]
);
}
}
fn production_lines(src: &str) -> Vec<(usize, &str)> {
let lines: Vec<&str> = src.lines().collect();
let mut out = Vec::with_capacity(lines.len());
let mut i = 0;
while i < lines.len() {
let trimmed = lines[i].trim_start();
if trimmed.starts_with("#[cfg(test)]") {
let mut depth: i32 = 0;
let mut opened = false;
while i < lines.len() {
depth += lines[i].matches('{').count() as i32;
depth -= lines[i].matches('}').count() as i32;
if lines[i].contains('{') {
opened = true;
}
if opened && depth <= 0 {
break;
}
i += 1;
}
i += 1;
continue;
}
out.push((
i + 1,
if trimmed.starts_with("//") {
""
} else {
lines[i]
},
));
i += 1;
}
out
}
fn declared_fn_name(line: &str) -> Option<&str> {
let mut rest = line.trim_start();
if let Some(after_pub) = rest.strip_prefix("pub") {
let after_pub = after_pub.trim_start();
rest = match after_pub.strip_prefix('(') {
Some(vis) => vis.split_once(')')?.1.trim_start(),
None => after_pub,
};
}
loop {
let before = rest;
for kw in ["const ", "async ", "unsafe "] {
if let Some(r) = rest.strip_prefix(kw) {
rest = r.trim_start();
}
}
if rest.len() == before.len() {
break;
}
}
let rest = rest.strip_prefix("fn ")?;
let name = rest.trim_start();
let end = name.find(|c: char| !c.is_alphanumeric() && c != '_')?;
if end == 0 {
return None;
}
Some(&name[..end])
}
struct FnBody<'a> {
name: &'a str,
line: usize,
lines: Vec<(usize, &'a str)>,
}
fn fn_bodies(src: &str) -> Vec<FnBody<'_>> {
let lines = production_lines(src);
let mut out = Vec::new();
let mut i = 0;
while i < lines.len() {
let Some(name) = declared_fn_name(lines[i].1) else {
i += 1;
continue;
};
let start = i;
let mut depth: i32 = 0;
let mut opened = false;
let mut body = Vec::new();
while i < lines.len() {
let text = lines[i].1;
if !opened {
let semi = text.find(';');
let brace = text.find('{');
if matches!((semi, brace), (Some(s), None) | (Some(s), Some(_)) if Some(s) < brace.or(Some(usize::MAX)))
{
break;
}
}
body.push(lines[i]);
depth += text.matches('{').count() as i32;
depth -= text.matches('}').count() as i32;
if text.contains('{') {
opened = true;
}
if opened && depth <= 0 {
break;
}
i += 1;
}
out.push(FnBody {
name,
line: lines[start].0,
lines: body,
});
i += 1;
}
out
}
#[cfg(test)]
mod gotcha_write_sites {
use super::{fn_bodies, rust_sources};
use std::collections::{BTreeMap, BTreeSet};
const WRITE_MARKERS: [&str; 4] = [
".put(",
".put_batch(",
".put_batch_kv_only(",
"KnowledgeWriteOp::PutRecord",
];
const PROVEN_NON_GOTCHA_PREFIXES: [&str; 18] = [
"file:",
"decision:",
"dev_note:",
"policy:",
"stage:",
"dep:",
"session:",
"analytics:",
"hook_event:",
"compliance:",
"graph:edge:",
"health:",
"parse:",
"audit:",
"enforcement:",
"system:",
"cluster:",
"schema:",
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Class {
Sanctioned,
Bypass,
NonGotcha,
}
const INVENTORY: &[(&str, &str, Class, &str)] = &[
(
"src/cli/init/run.rs",
"run",
Class::Bypass,
"Layer 0 batch write: gotcha:cochange:*, gotcha:revert:*, \
gotcha:ownership:*, gotcha:codeowners:* stubs plus CLAUDE.md \
imports keyed gotcha:claude-md-<slug>",
),
(
"src/cli/proxy.rs",
"put",
Class::Bypass,
"StoreProxy transport: the Direct arm forwards the caller's key \
verbatim, and the sibling socket arm branches on \
key.starts_with(\"gotcha:\")",
),
(
"src/cli/proxy.rs",
"import_records",
Class::Bypass,
"direct-mode import allowlist starts with \"gotcha:\"",
),
(
"src/cli/repair.rs",
"run",
Class::NonGotcha,
"blast-radius and propagation passes rewrite only \
scan_prefix(\"file:\") results, keyed by record.key",
),
(
"src/cli/review.rs",
"edit_candidate",
Class::Bypass,
"re-serializes a GotchaRecord into a key taken from the review \
candidate list (scan_prefix(\"gotcha:\"))",
),
(
"src/cli/sandbox.rs",
"run_protect",
Class::Bypass,
"re-puts scan_prefix(\"gotcha:\") matches with deny-read/deny-write \
tags",
),
(
"src/cli/suggest.rs",
"write_candidates",
Class::Bypass,
"onboarding::build_candidates emits gotcha:codeowners:* and \
gotcha:marker:* keys with concrete affected file paths",
),
(
"src/health/staleness/reparse.rs",
"cascade_staleness_to_gotchas",
Class::Bypass,
"writes gotcha_record under gotcha_key to cascade file staleness \
onto the linked gotchas",
),
(
"src/health/staleness/analyzer.rs",
"analyze_until",
Class::Bypass,
"batch-rescores every namespace in STALENESS_PREFIXES, which \
includes \"gotcha:\"",
),
(
"src/main.rs",
"run_improve",
Class::Bypass,
"`mati improve <key>` rewrites whatever key the user passes, with \
no prefix guard",
),
(
"src/mcp/dispatch_v2/session.rs",
"dispatch_session_side",
Class::Bypass,
"ConsultationHit bumps access_count on input.key and re-puts the \
record; gotchas are the primary consultation target",
),
(
"src/mcp/handlers/gotcha.rs",
"upsert_commit_once",
Class::Bypass,
"v2 gotcha upsert — needs record + file links + audit in one \
transact_knowledge, which gotcha_ops does not offer",
),
(
"src/mcp/handlers/gotcha.rs",
"confirm_commit_once",
Class::Bypass,
"v2 gotcha confirm — same single-transaction requirement",
),
(
"src/mcp/handlers/gotcha.rs",
"tombstone_commit_once",
Class::Bypass,
"v2 gotcha tombstone — same single-transaction requirement",
),
(
"src/mcp/handlers/file.rs",
"handle_file_reparse",
Class::NonGotcha,
"writes the file_key returned by reparse_staged, built as \
format!(\"file:{rel_path}\")",
),
(
"src/mcp/handlers/decision_devnote.rs",
"handle_dev_note_upsert",
Class::NonGotcha,
"rejects any key that is not dev_note:-prefixed before writing",
),
(
"src/mcp/handlers/reads.rs",
"handle_mem_get",
Class::Bypass,
"read path: the spawned task re-puts the fetched record with an \
incremented access_count, and the fetched key is usually a gotcha",
),
(
"src/mcp/handlers/reads.rs",
"handle_mem_bootstrap",
Class::NonGotcha,
"writes file:<path> records assembled for the bootstrap packet",
),
(
"src/mcp/handlers/record_import.rs",
"handle_record_import",
Class::Bypass,
"import prefix allowlist includes \"gotcha:\"; accepted records are \
emitted as PutRecord { key: r.key }",
),
(
"src/mcp/server/dispatch.rs",
"socket_dispatch",
Class::Bypass,
"the `put` command writes req.args[\"key\"] with no namespace \
validation; the `confirm` command writes a confirmed GotchaRecord",
),
(
"src/store/db/crud.rs",
"transact_knowledge",
Class::NonGotcha,
"the write primitive itself — it dispatches KnowledgeWriteOp, it \
does not originate a key",
),
(
"src/store/extraction.rs",
"write_on_extraction",
Class::NonGotcha,
"key_for() strips the gotcha: prefix and returns \
analytics:extraction:<slug>",
),
(
"src/store/extraction.rs",
"mark_outcome",
Class::NonGotcha,
"same key_for() -> analytics:extraction:<slug>",
),
(
"src/store/gotcha_ops/mutation.rs",
"apply_gotcha_write",
Class::Sanctioned,
"the centralized create/edit path",
),
(
"src/store/gotcha_ops/mutation.rs",
"apply_gotcha_tombstone",
Class::Sanctioned,
"the centralized tombstone path",
),
(
"src/store/gotcha_ops/mutation.rs",
"apply_gotcha_confirm",
Class::Sanctioned,
"the centralized confirm path",
),
(
"src/store/migrations.rs",
"commit_bootstrap",
Class::NonGotcha,
"writes only system:schema_version and \
system:migration:applied:<n>",
),
(
"src/store/migrations.rs",
"commit_migration",
Class::NonGotcha,
"the two literal ops are system:schema_version and the history \
key; migration bodies arrive via extra_ops / as_write_op",
),
(
"src/store/migrations.rs",
"as_write_op",
Class::Bypass,
"its producers, apply_v2_unconfirm_auto_derived_gotchas and \
apply_v3_repair_codeowners_gotchas, stage canonical gotcha writes \
inside the migration transaction",
),
(
"src/store/negative_exemplar.rs",
"write_on_tombstone",
Class::NonGotcha,
"make_key() returns analytics:negative_exemplar:<dir>:<slug>; the \
gotcha key survives only as a payload field",
),
(
"src/store/policy_ops.rs",
"create",
Class::NonGotcha,
"ensure_policy_key() rejects anything not policy:-prefixed",
),
(
"src/store/policy_ops.rs",
"edit",
Class::NonGotcha,
"ensure_policy_key() rejects anything not policy:-prefixed",
),
(
"src/store/policy_ops.rs",
"set_stage",
Class::NonGotcha,
"ensure_policy_key() rejects anything not policy:-prefixed",
),
(
"src/store/policy_ops.rs",
"delete",
Class::NonGotcha,
"ensure_policy_key() rejects anything not policy:-prefixed",
),
(
"src/store/repair.rs",
"repair_unnormalized_paths",
Class::Bypass,
"scans gotcha:*, rewrites affected_files in the payload and puts \
back at record.key",
),
(
"src/store/repair.rs",
"repair_fast",
Class::NonGotcha,
"both writes target file records; the gotcha key only moves in and \
out of the gotcha_keys array",
),
(
"src/store/repair.rs",
"purge_orphaned_files",
Class::NonGotcha,
"tombstones orphans found by find_orphaned_files, which only ever \
yields scan_prefix(\"file:\") keys",
),
(
"src/store/session.rs",
"record_shadow_observation",
Class::NonGotcha,
"shadow_observation_key() -> analytics:policy_shadow_<date>",
),
(
"src/store/session.rs",
"upsert_daily_agg",
Class::NonGotcha,
"agg_key is always an analytics:/compliance: daily key; the gotcha \
key appears only as target_key inside the payload",
),
(
"src/store/session.rs",
"log_hit",
Class::Bypass,
"the second write bumps access_count/last_accessed on the consulted \
target record, which is routinely gotcha:<slug>",
),
(
"src/store/session.rs",
"promote_gotcha_candidates",
Class::Bypass,
"scans gotcha:*, sets confirmed = true and increments \
confirmation_count",
),
];
fn proven_non_gotcha(expr: &str, body: &str, consts: &[(String, String)]) -> bool {
let expr = expr.trim_start().trim_start_matches('&').trim_start();
if let Some(literal) = string_literal_at(expr) {
return PROVEN_NON_GOTCHA_PREFIXES
.iter()
.any(|p| literal.starts_with(p));
}
let ident: String = expr
.chars()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect();
if ident.is_empty() {
return false;
}
let after = expr[ident.len()..].chars().next();
if !matches!(after, None | Some(',') | Some(')') | Some(' ')) {
return false;
}
if let Some((_, value)) = consts.iter().find(|(name, _)| *name == ident) {
return PROVEN_NON_GOTCHA_PREFIXES
.iter()
.any(|p| value.starts_with(p));
}
for pattern in [format!("let {ident} ="), format!("let mut {ident} =")] {
let Some(pos) = body.find(&pattern) else {
continue;
};
let rhs = &body[pos + pattern.len()..];
let rhs = rhs.trim_start().trim_start_matches('&').trim_start();
let rhs = rhs.strip_prefix("format!(").unwrap_or(rhs).trim_start();
if let Some(literal) = string_literal_at(rhs) {
return PROVEN_NON_GOTCHA_PREFIXES
.iter()
.any(|p| literal.starts_with(p));
}
}
false
}
fn string_literal_at(expr: &str) -> Option<&str> {
let rest = expr.strip_prefix('"')?;
let end = rest.find('"')?;
Some(&rest[..end])
}
fn str_consts(src: &str) -> Vec<(String, String)> {
let mut out = Vec::new();
for line in src.lines() {
let line = line.trim_start();
let line = line.strip_prefix("pub ").unwrap_or(line);
let Some(rest) = line.strip_prefix("const ") else {
continue;
};
let Some((name, tail)) = rest.split_once(':') else {
continue;
};
let Some((_, value)) = tail.split_once('=') else {
continue;
};
if let Some(literal) = string_literal_at(value.trim_start()) {
out.push((name.trim().to_string(), literal.to_string()));
}
}
out
}
fn scan() -> BTreeMap<(String, String), usize> {
let mut found = BTreeMap::new();
for (file, src) in rust_sources() {
let consts = str_consts(&src);
for body in fn_bodies(&src) {
let body_text: String = body
.lines
.iter()
.map(|(_, l)| *l)
.collect::<Vec<_>>()
.join("\n");
for (idx, (_, line)) in body.lines.iter().enumerate() {
for marker in WRITE_MARKERS {
let Some(pos) = line.find(marker) else {
continue;
};
let tail = &line[pos + marker.len()..];
let key_expr = if marker == "KnowledgeWriteOp::PutRecord" {
let window: String = std::iter::once(tail)
.chain(body.lines[idx + 1..].iter().take(2).map(|(_, l)| *l))
.collect::<Vec<_>>()
.join("\n");
match window.split_once("key:") {
Some((_, after)) => after.trim_start().to_string(),
None => String::new(),
}
} else if tail.trim().is_empty() || tail.trim_start().starts_with("&[") {
body.lines
.get(idx + 1)
.map(|(_, l)| l.to_string())
.unwrap_or_default()
} else {
tail.to_string()
};
if proven_non_gotcha(&key_expr, &body_text, &consts) {
continue;
}
found.insert((file.clone(), body.name.to_string()), body.line);
}
}
}
}
found
}
#[test]
fn every_possible_gotcha_write_site_is_in_the_audited_inventory() {
let found = scan();
let found_sites: BTreeSet<(String, String)> = found.keys().cloned().collect();
let listed: BTreeSet<(String, String)> = INVENTORY
.iter()
.map(|(f, n, _, _)| (f.to_string(), n.to_string()))
.collect();
let unlisted: Vec<String> = found_sites
.difference(&listed)
.map(|site| format!("{}:{} fn {}", site.0, found[site], site.1))
.collect();
let stale: Vec<_> = listed.difference(&found_sites).collect();
assert!(
unlisted.is_empty(),
"NEW GOTCHA WRITE SITE(S) — a record-level store write reachable \
with a `gotcha:*` key appeared outside the audited inventory:\n\
{unlisted:#?}\n\n\
CLAUDE.md says every create/edit/tombstone path goes through \
`store::gotcha_ops`. If this site really needs its own write, add \
it to INVENTORY in src/invariants.rs with a Class and a reason. \
Do not delete this assertion."
);
assert!(
stale.is_empty(),
"STALE INVENTORY ENTRIES — these are listed in \
src/invariants.rs but no longer write records (renamed, deleted, \
or now routed through gotcha_ops):\n{stale:#?}\n\n\
Remove them so the inventory keeps describing the code."
);
}
#[test]
fn the_sanctioned_module_still_owns_the_centralized_paths() {
let sanctioned: Vec<_> = INVENTORY
.iter()
.filter(|(_, _, class, _)| *class == Class::Sanctioned)
.collect();
assert_eq!(
sanctioned.len(),
3,
"store::gotcha_ops must keep exactly its three centralized \
mutations (write / tombstone / confirm); got {sanctioned:#?}"
);
assert!(
sanctioned
.iter()
.all(|(file, _, _, _)| file.starts_with("src/store/gotcha_ops/")),
"only store::gotcha_ops may be classified Sanctioned"
);
}
}
#[cfg(test)]
mod zero_network {
use super::{production_lines, rust_sources};
const BANNED_SOURCE_TOKENS: &[&str] = &[
"std::net::",
"TcpStream",
"TcpListener",
"UdpSocket",
"to_socket_addrs",
"tokio::net::TcpStream",
"tokio::net::TcpListener",
"tokio::net::UdpSocket",
"reqwest::",
"hyper::",
"ureq::",
"isahc::",
"surf::",
"attohttpc::",
"curl::",
"tokio_tungstenite::",
"Command::new(\"curl\")",
"Command::new(\"wget\")",
"Command::new(\"nc\")",
"Command::new(\"ping\")",
];
const BANNED_CRATES: &[&str] = &[
"reqwest",
"hyper",
"hyper-util",
"hyper-rustls",
"hyper-tls",
"h2",
"ureq",
"ureq-proto",
"isahc",
"surf",
"attohttpc",
"curl",
"curl-sys",
"tokio-tungstenite",
"tungstenite",
"tonic",
"hf-hub",
"native-tls",
"rustls",
"tokio-rustls",
"tokio-native-tls",
"sentry",
"opentelemetry",
"posthog-rs",
"segment",
];
#[test]
fn no_source_file_opens_an_ip_socket_or_speaks_http() {
let mut hits = Vec::new();
for (file, src) in rust_sources() {
for (lineno, line) in production_lines(&src) {
for token in BANNED_SOURCE_TOKENS {
if line.contains(token) {
hits.push(format!("{file}:{lineno} [{token}] {}", line.trim()));
}
}
}
}
assert!(
hits.is_empty(),
"NETWORK API IN THE OSS BINARY — CLAUDE.md: \"The OSS binary never \
phones home. Period.\" and \"Any network call in the enforcement \
path\" is forbidden.\n{}\n\n\
Unix domain sockets (tokio::net::UnixStream / UnixListener) are \
the daemon transport and are allowed; nothing that can reach \
another host is.",
hits.join("\n")
);
}
#[test]
fn the_daemon_transport_is_a_unix_domain_socket() {
let uses_unix_sockets = rust_sources().iter().any(|(_, src)| {
production_lines(src)
.iter()
.any(|(_, l)| l.contains("UnixListener") || l.contains("UnixStream"))
});
assert!(
uses_unix_sockets,
"expected the daemon to bind a Unix domain socket; if the IPC \
transport changed, re-derive what BANNED_SOURCE_TOKENS must allow"
);
}
#[test]
fn no_http_client_is_a_non_optional_direct_dependency() {
let manifest = std::fs::read_to_string(
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"),
)
.expect("Cargo.toml is readable");
let doc: toml_edit::DocumentMut = manifest.parse().expect("Cargo.toml parses");
let default_features = doc["features"]["default"]
.as_array()
.expect("[features] default is an array");
assert!(
default_features.is_empty(),
"`default` must stay empty — every zero-network claim below is \
about the default build; got {default_features}"
);
let deps = doc["dependencies"]
.as_table()
.expect("[dependencies] is a table");
let mut offenders = Vec::new();
for (name, item) in deps.iter() {
if !BANNED_CRATES.contains(&name) {
continue;
}
let optional = item
.get("optional")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if !optional {
offenders.push(name.to_string());
}
}
assert!(
offenders.is_empty(),
"network-capable crate(s) are non-optional direct dependencies, so \
they ship in the default binary: {offenders:?}"
);
}
#[test]
fn the_default_build_links_no_http_client_crate() {
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let output = std::process::Command::new(&cargo)
.current_dir(env!("CARGO_MANIFEST_DIR"))
.args([
"tree",
"--locked",
"--offline",
"--no-default-features",
"--edges",
"normal",
"--prefix",
"none",
"--format",
"{p}",
])
.output()
.expect("`cargo tree` runs; the zero-network attestation cannot be skipped");
assert!(
output.status.success(),
"`cargo tree` failed, so the zero-network attestation could not be \
established:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
let linked: Vec<&str> = stdout
.lines()
.filter_map(|l| l.split_whitespace().next())
.collect();
let offenders: Vec<&&str> = BANNED_CRATES
.iter()
.filter(|c| linked.contains(*c))
.collect();
assert!(
offenders.is_empty(),
"THE DEFAULT BUILD LINKS A NETWORK CLIENT — CLAUDE.md: \"The OSS \
binary never phones home. Period.\"\nOffending crate(s): \
{offenders:?}\n\n\
The `semantic` feature is the one sanctioned exception and is \
excluded here via --no-default-features. If a new dependency \
pulled this in transitively, that dependency does not belong in \
the default build."
);
assert!(
linked.len() > 50,
"sanity check: `cargo tree` returned only {} crates, so the scan \
above proved nothing",
linked.len()
);
}
}
#[cfg(test)]
mod documented_caller_claims {
use super::{declared_fn_name, fn_bodies, production_lines, rust_sources};
use std::collections::BTreeSet;
const CLAIM_PHRASES: &[&str] = &[
"called by",
"called from",
"invoked by",
"invoked from",
"used by",
"used from",
"consumed by",
"triggered by",
"driven by",
"executed by",
"fired by",
"recomputed by",
"called at",
"called during",
"called on",
"called once",
"called whenever",
"invoked during",
"invoked on",
"recomputed on",
"runs during",
"runs whenever",
"fires on",
"fires when",
"fires after",
"fires during",
"startup",
];
const ALLOWLIST: &[(&str, &str, &str)] = &[];
struct Claim {
file: String,
line: usize,
name: String,
phrase: &'static str,
}
fn contains_word(hay: &str, needle: &str) -> bool {
let bytes = hay.as_bytes();
let ident = |c: u8| c.is_ascii_alphanumeric() || c == b'_';
let mut from = 0;
while let Some(pos) = hay[from..].find(needle) {
let start = from + pos;
let end = start + needle.len();
if (start == 0 || !ident(bytes[start - 1]))
&& (end == bytes.len() || !ident(bytes[end]))
{
return true;
}
from = end;
}
false
}
fn claim_phrase(doc: &str) -> Option<&'static str> {
let lower = doc.to_ascii_lowercase();
CLAIM_PHRASES
.iter()
.copied()
.find(|p| contains_word(&lower, p))
}
fn claims(file: &str, src: &str) -> Vec<Claim> {
let production: BTreeSet<usize> = production_lines(src).iter().map(|(n, _)| *n).collect();
let mut out = Vec::new();
let mut doc = String::new();
for (idx, line) in src.lines().enumerate() {
let lineno = idx + 1;
if !production.contains(&lineno) {
doc.clear();
continue;
}
let trimmed = line.trim_start();
if let Some(text) = trimmed.strip_prefix("///") {
doc.push_str(text);
doc.push('\n');
continue;
}
if doc.is_empty() {
continue;
}
if trimmed.starts_with("#[") {
continue;
}
if let (Some(name), Some(phrase)) = (declared_fn_name(line), claim_phrase(&doc)) {
out.push(Claim {
file: file.to_string(),
line: lineno,
name: name.to_string(),
phrase,
});
}
doc.clear();
}
out
}
fn own_lines(src: &str, name: &str, decl_line: usize) -> BTreeSet<usize> {
fn_bodies(src)
.into_iter()
.find(|b| b.name == name && b.line == decl_line)
.map(|b| b.lines.iter().map(|(n, _)| *n).collect())
.unwrap_or_else(|| BTreeSet::from([decl_line]))
}
fn production_corpus(sources: &[(String, String)]) -> Vec<(&str, usize, &str)> {
sources
.iter()
.flat_map(|(file, src)| {
production_lines(src)
.into_iter()
.map(move |(n, l)| (file.as_str(), n, l))
})
.collect()
}
fn has_call_site(claim: &Claim, own: &BTreeSet<usize>, corpus: &[(&str, usize, &str)]) -> bool {
corpus.iter().any(|(file, lineno, line)| {
if *file == claim.file && own.contains(lineno) {
return false;
}
if declared_fn_name(line) == Some(claim.name.as_str()) {
return false;
}
call_site(line, &claim.name)
})
}
fn call_site(line: &str, name: &str) -> bool {
let bytes = line.as_bytes();
let ident = |c: u8| c.is_ascii_alphanumeric() || c == b'_';
let mut from = 0;
while let Some(pos) = line[from..].find(name) {
let start = from + pos;
let end = start + name.len();
let bounded = start == 0 || !ident(bytes[start - 1]);
if bounded && line[end..].starts_with('(') {
return true;
}
from = end;
}
false
}
#[test]
fn every_documented_caller_claim_has_a_caller() {
let sources = rust_sources();
let corpus = production_corpus(&sources);
let mut claim_count = 0;
let mut uncalled = Vec::new();
for (file, src) in &sources {
for claim in claims(file, src) {
claim_count += 1;
if ALLOWLIST
.iter()
.any(|(f, n, _)| *f == claim.file && *n == claim.name)
{
continue;
}
let own = own_lines(src, &claim.name, claim.line);
if !has_call_site(&claim, &own, &corpus) {
uncalled.push(format!(
"{}:{} fn {} — doc says \"{}\", nothing calls it",
claim.file, claim.line, claim.name, claim.phrase
));
}
}
}
assert!(
uncalled.is_empty(),
"DOC CLAIMS A CALLER THAT DOES NOT EXIST:\n{}\n\n\
Each of these functions is documented as being called and has no \
call site in src/. The default fix is to correct the doc to say \
what is true. Wiring up a caller is a behaviour change and needs \
its own review — do not do it to silence the test. If the call \
site is real but invisible to a text scan (trait object, macro, \
FFI), add it to ALLOWLIST in src/invariants.rs with the reason.",
uncalled.join("\n")
);
assert!(
claim_count > 30,
"sanity check: only {claim_count} caller claims found in src/, so \
the doc-block parser is broken and the assertion above proved \
nothing"
);
}
#[test]
fn the_allowlist_has_no_stale_entries() {
let sources = rust_sources();
let live: BTreeSet<(String, String)> = sources
.iter()
.flat_map(|(file, src)| claims(file, src))
.map(|c| (c.file, c.name))
.collect();
let stale: Vec<_> = ALLOWLIST
.iter()
.filter(|(f, n, _)| !live.contains(&(f.to_string(), n.to_string())))
.collect();
assert!(
stale.is_empty(),
"STALE ALLOWLIST ENTRIES — renamed, deleted, or the doc no longer \
claims a caller:\n{stale:#?}"
);
let unexplained: Vec<_> = ALLOWLIST
.iter()
.filter(|(_, _, why)| why.trim().is_empty())
.collect();
assert!(
unexplained.is_empty(),
"ALLOWLIST ENTRIES WITHOUT A REASON — an entry that does not say \
why the scan is wrong is a suppression:\n{unexplained:#?}"
);
}
}