use std::collections::BTreeMap;
use crate::db::Db;
use crate::error::Result;
use crate::model::{now_ts, Anchor, Assertion, Standing};
use crate::repo::footing::{assess, paths_in, Anchoring};
use crate::repo::Recalled;
use crate::witness::Witness;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ground {
pub paths: Vec<String>,
pub by: Anchoring,
}
impl Ground {
fn nothing() -> Ground {
Ground {
paths: Vec::new(),
by: Anchoring::Derived,
}
}
}
pub fn anchor(
db: &Db,
witness: Option<&Witness>,
assertion_id: &str,
ground: &Ground,
) -> Result<Vec<Anchor>> {
let (Some(witness), false) = (witness, ground.paths.is_empty()) else {
return Ok(Vec::new());
};
let at = now_ts();
let anchors: Vec<Anchor> = ground
.paths
.iter()
.map(|path| Anchor {
hash: witness.fingerprint(path),
path: path.clone(),
at: at.clone(),
})
.collect();
if !db.footings().anchor(assertion_id, &anchors, ground.by)? {
return Ok(Vec::new());
}
Ok(anchors)
}
pub fn ground(db: &Db, task_seq: Option<i64>, named: Option<&[String]>) -> Ground {
if let Some(paths) = named {
let usable: Vec<String> = paths
.iter()
.filter_map(|p| crate::glob::normalize(p))
.filter(|p| crate::glob::is_literal(p))
.collect();
if !usable.is_empty() {
return Ground {
paths: dedup(usable),
by: Anchoring::Named,
};
}
}
let Some(seq) = task_seq else {
return Ground::nothing();
};
match db.footings().ground_for_task(seq) {
Ok(paths) => Ground {
paths,
by: Anchoring::Derived,
},
Err(_) => Ground::nothing(),
}
}
pub fn standing(db: &Db, witness: Option<&Witness>, project: &str, assertion_id: &str) -> Standing {
let ids = vec![assertion_id.to_string()];
standings(db, witness, project, &ids)
.remove(assertion_id)
.unwrap_or(Standing::Unanchored)
}
pub fn standings(
db: &Db,
witness: Option<&Witness>,
project: &str,
assertion_ids: &[String],
) -> BTreeMap<String, Standing> {
let Some(witness) = witness else {
return BTreeMap::new();
};
let Ok(footings) = db.footings().anchors_for(Some(project), assertion_ids) else {
return BTreeMap::new();
};
let now: BTreeMap<String, String> = paths_in(&footings)
.into_iter()
.map(|path| {
let hash = witness.fingerprint(&path);
(path, hash)
})
.collect();
footings
.into_iter()
.map(|(id, anchors)| {
let standing = assess(&anchors, |path| now.get(path).cloned().unwrap_or_default());
(id, standing)
})
.collect()
}
pub fn settle(db: &Db, witness: Option<&Witness>, seq: i64) -> usize {
let Some(witness) = witness else {
return 0;
};
let Ok(learned) = db.footings().learned_on(seq) else {
return 0;
};
if learned.is_empty() {
return 0;
}
let ground = ground(db, Some(seq), None);
if ground.paths.is_empty() {
return 0;
}
learned
.into_iter()
.filter(|a| {
anchor(db, Some(witness), &a.id, &ground).is_ok_and(|written| !written.is_empty())
})
.count()
}
pub fn decorate(db: &Db, witness: Option<&Witness>, mut recalled: Vec<Recalled>) -> Vec<Recalled> {
if recalled.is_empty() {
return recalled;
}
let ids: Vec<String> = recalled.iter().map(|r| r.assertion.id.clone()).collect();
let project = recalled[0].assertion.project.clone();
let mut standings = standings(db, witness, &project, &ids);
for row in &mut recalled {
row.standing = standings.remove(&row.assertion.id);
row.corroboration = corroboration(db, &row.assertion);
}
recalled.sort_by_key(|r| {
(
r.reason.strength(),
r.standing.as_ref().map_or(1, Standing::rank),
)
});
recalled
}
pub fn corroboration(db: &Db, assertion: &Assertion) -> Option<String> {
db.footings()
.voices(assertion)
.ok()
.and_then(|v| v.describe())
}
fn dedup(paths: Vec<String>) -> Vec<String> {
let mut out: Vec<String> = Vec::new();
for path in paths {
if !out.contains(&path) {
out.push(path);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::repo::{NewAssertion, OnConflict};
struct Sandbox {
dir: tempfile::TempDir,
db: Db,
}
impl Sandbox {
fn new() -> Option<Sandbox> {
let dir = tempfile::tempdir().ok()?;
for args in [
vec!["init", "-q"],
vec!["config", "user.email", "t@example.com"],
vec!["config", "user.name", "t"],
] {
let ok = std::process::Command::new("git")
.args(&args)
.current_dir(dir.path())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.ok()?
.success();
if !ok {
return None;
}
}
Some(Sandbox {
dir,
db: Db::open_in_memory().ok()?,
})
}
fn project(&self) -> String {
self.dir.path().to_string_lossy().to_string()
}
fn witness(&self) -> Witness {
Witness::discover(self.dir.path()).expect("a fresh git checkout is witnessable")
}
fn write(&self, path: &str, contents: &str) {
let full = self.dir.path().join(path);
if let Some(parent) = full.parent() {
std::fs::create_dir_all(parent).unwrap();
}
std::fs::write(full, contents).unwrap();
}
fn remove(&self, path: &str) {
std::fs::remove_file(self.dir.path().join(path)).unwrap();
}
fn store(&self, content: &str, task_seq: Option<i64>) -> Assertion {
self.db
.memory()
.store(NewAssertion {
project: &self.project(),
content,
tags: "",
actor: "codex:9f2c",
task_seq,
})
.unwrap()
}
fn record(&self, content: &str, actor: &str) -> crate::repo::Recorded {
self.db
.memory()
.record(NewAssertion {
project: &self.project(),
content,
tags: "",
actor,
task_seq: None,
})
.unwrap()
}
fn standing(&self, id: &str) -> Standing {
standing(&self.db, Some(&self.witness()), &self.project(), id)
}
fn anchor_to(&self, id: &str, paths: &[&str]) -> Vec<Anchor> {
let named: Vec<String> = paths.iter().map(|p| p.to_string()).collect();
let ground = ground(&self.db, None, Some(&named));
anchor(&self.db, Some(&self.witness()), id, &ground).unwrap()
}
}
fn named(paths: &[&str]) -> Vec<String> {
paths.iter().map(|p| p.to_string()).collect()
}
#[test]
fn a_fact_anchored_to_a_file_goes_shaky_when_the_file_moves() {
let Some(s) = Sandbox::new() else { return };
s.write("src/config.rs", "fn load() { env_first() }");
let a = s.store("the loader reads env before the file", None);
let anchors = s.anchor_to(&a.id, &["src/config.rs"]);
assert_eq!(anchors.len(), 1);
assert!(!anchors[0].hash.is_empty());
assert_eq!(
s.standing(&a.id).as_str(),
"firm",
"nothing has happened yet"
);
s.write("src/config.rs", "fn load() { file_first() }");
let moved = s.standing(&a.id);
assert_eq!(moved.as_str(), "shaky");
assert!(moved.needs_checking());
assert!(moved.describe().unwrap().contains("src/config.rs"));
s.remove("src/config.rs");
assert_eq!(s.standing(&a.id).as_str(), "orphaned");
}
#[test]
fn restating_a_shaky_fact_re_anchors_it_and_records_the_second_voice() {
let Some(s) = Sandbox::new() else { return };
s.write("src/config.rs", "v1");
let first = s.record("the loader reads env before the file", "codex:9f2c");
assert!(!first.was_affirmed());
let id = first.assertion().id.clone();
s.anchor_to(&id, &["src/config.rs"]);
s.write("src/config.rs", "v2");
assert_eq!(s.standing(&id).as_str(), "shaky");
let again = s.record(
" the loader reads env before the file ",
"claude-code:af31",
);
assert!(
again.was_affirmed(),
"an exact restatement is not a new fact"
);
assert_eq!(again.assertion().id, id, "and it keeps its provenance");
s.anchor_to(&id, &["src/config.rs"]);
assert_eq!(s.standing(&id).as_str(), "firm");
let sentence = corroboration(&s.db, again.assertion()).unwrap();
assert!(
sentence.contains("independently across 2 harnesses"),
"{sentence}"
);
}
#[test]
fn a_task_that_keeps_working_after_recording_a_fact_still_leaves_it_firm() {
let Some(s) = Sandbox::new() else { return };
s.write("src/config.rs", "v1");
let project = s.project();
let seq =
s.db.tasks()
.create(&project, "Port the loader", "", 0, "cli")
.unwrap()
.seq;
s.db.scopes()
.declare(seq, &named(&["src/config.rs"]), "cli", OnConflict::Report)
.unwrap();
s.db.tasks()
.claim(seq, "codex:9f2c", std::time::Duration::from_secs(900))
.unwrap();
let learned = s.store("the loader reads env before the file", Some(seq));
let derived = ground(&s.db, Some(seq), None);
assert_eq!(derived.by, Anchoring::Derived);
anchor(&s.db, Some(&s.witness()), &learned.id, &derived).unwrap();
s.write("src/config.rs", "v2 — tidied up");
assert_eq!(
s.standing(&learned.id).as_str(),
"shaky",
"mid-flight, its own edits have moved the ground under it"
);
assert_eq!(settle(&s.db, Some(&s.witness()), seq), 1);
assert_eq!(
s.standing(&learned.id).as_str(),
"firm",
"settling re-reads the tree the task is leaving behind"
);
}
#[test]
fn settling_leaves_a_footing_the_author_named_alone() {
let Some(s) = Sandbox::new() else { return };
s.write("src/config.rs", "v1");
s.write("docs/loader.md", "the loader, explained");
let project = s.project();
let seq =
s.db.tasks()
.create(&project, "Port the loader", "", 0, "cli")
.unwrap()
.seq;
s.db.scopes()
.declare(seq, &named(&["src/config.rs"]), "cli", OnConflict::Report)
.unwrap();
s.db.tasks()
.claim(seq, "codex:9f2c", std::time::Duration::from_secs(900))
.unwrap();
let about_docs = s.store("the loader's docs are hand-written", Some(seq));
let stated = ground(&s.db, Some(seq), Some(&named(&["docs/loader.md"])));
assert_eq!(stated.by, Anchoring::Named);
anchor(&s.db, Some(&s.witness()), &about_docs.id, &stated).unwrap();
s.write("src/config.rs", "v2");
assert_eq!(s.standing(&about_docs.id).as_str(), "firm");
assert_eq!(
settle(&s.db, Some(&s.witness()), seq),
0,
"nothing to settle: the only fact here chose its own ground"
);
assert_eq!(
s.db.footings()
.anchors(&about_docs.id)
.unwrap()
.into_iter()
.map(|a| a.path)
.collect::<Vec<_>>(),
vec!["docs/loader.md"],
"and it kept it"
);
}
#[test]
fn a_fact_from_another_project_is_never_measured_against_this_tree() {
let Some(s) = Sandbox::new() else { return };
s.write("src/config.rs", "v1");
let mine = s.store("this project's fact", None);
s.anchor_to(&mine.id, &["src/config.rs"]);
let theirs =
s.db.memory()
.store(NewAssertion {
project: "/somewhere/else",
content: "another project's fact",
tags: "",
actor: "codex:9f2c",
task_seq: None,
})
.unwrap();
s.db.footings()
.anchor(
&theirs.id,
&[Anchor {
path: "src/config.rs".into(),
hash: "whatever-it-said-over-there".into(),
at: now_ts(),
}],
Anchoring::Named,
)
.unwrap();
let ids = vec![mine.id.clone(), theirs.id.clone()];
let seen = standings(&s.db, Some(&s.witness()), &s.project(), &ids);
assert_eq!(seen.len(), 1);
assert_eq!(seen[&mine.id].as_str(), "firm");
assert!(!seen.contains_key(&theirs.id));
}
#[test]
fn without_a_witness_nothing_is_anchored_and_nothing_is_claimed() {
let db = Db::open_in_memory().unwrap();
let a = db
.memory()
.store(NewAssertion {
project: "/tmp/project",
content: "a fact",
tags: "",
actor: "codex:9f2c",
task_seq: None,
})
.unwrap();
let ground = Ground {
paths: named(&["src/config.rs"]),
by: Anchoring::Named,
};
assert!(anchor(&db, None, &a.id, &ground).unwrap().is_empty());
assert_eq!(
standing(&db, None, "/tmp/project", &a.id),
Standing::Unanchored
);
assert_eq!(settle(&db, None, 1), 0);
}
#[test]
fn named_paths_win_over_the_task_and_globs_are_dropped() {
let db = Db::open_in_memory().unwrap();
let seq = db
.tasks()
.create("/tmp/project", "t", "", 0, "cli")
.unwrap()
.seq;
db.scopes()
.declare(seq, &named(&["src/db.rs"]), "cli", OnConflict::Report)
.unwrap();
let chosen = ground(&db, Some(seq), Some(&named(&["./src/config.rs", "src/**"])));
assert_eq!(
chosen,
Ground {
paths: named(&["src/config.rs"]),
by: Anchoring::Named,
},
"normalized, and the glob is not guessed at"
);
let fallback = ground(&db, Some(seq), Some(&named(&["src/**"])));
assert_eq!(
fallback,
Ground {
paths: named(&["src/db.rs"]),
by: Anchoring::Derived,
}
);
assert!(ground(&db, None, None).paths.is_empty());
}
}