use crate::ollama;
pub const NOTATION: &str = include_str!("notation.txt");
use std::path::Path;
#[derive(Debug)]
pub struct Step {
pub label: &'static str,
pub prompt_tokens: u64,
pub eval_tokens: u64,
pub ms: u128,
}
pub use crate::spec::rule_depth;
pub use crate::code::{expected_calls, signatures, split_module, test_decls};
fn insert_test(src: &str, test_fn: &str) -> String {
let idx = src.trim_end().rfind('}').unwrap_or(src.len());
format!("{}\n{}\n{}", &src[..idx], test_fn.trim_end(), &src[idx..])
}
fn insert_impl(src: &str, code: &str) -> String {
let (impl_r, tests) = split_module(src);
let (code_impl, _) = split_module(code);
format!("{}\n{}\n\n{}", impl_r.trim_end(), code_impl.trim(), tests)
}
#[must_use]
pub fn red_note(task: &str) -> String {
named_fn(task).map_or(String::new(), |n| {
format!(
"`{n}` does NOT exist yet: this is the RED step, writing it is \
the next one, so a call to it is EXPECTED and is not a reason \
to answer NO. "
)
})
}
fn tick(chunk: &str, n: usize) -> usize {
use std::io::Write;
if ollama::verbose() {
eprint!("{chunk}");
} else if n.wrapping_add(1).is_multiple_of(25) {
eprint!(".");
}
let _ = std::io::stderr().flush();
n.wrapping_add(1)
}
pub struct Caller<'a> {
t: &'a dyn ollama::Transport,
pub log: Vec<Step>,
}
impl<'a> Caller<'a> {
#[must_use]
pub fn new(t: &'a dyn ollama::Transport) -> Self {
Self { t, log: Vec::new() }
}
fn run(
&mut self,
prompt: &str,
label: &'static str,
) -> Result<String, String> {
self.run_sampled(prompt, label, ollama::Sampling::DETERMINISTIC)
}
fn run_sampled(
&mut self,
prompt: &str,
label: &'static str,
sampling: ollama::Sampling,
) -> Result<String, String> {
use std::io::Write;
let local = crate::tokens::count(prompt);
let eta = ollama::predict_for(label, local.tokens);
eprintln!(
" [{label}] -> {} tok ({:.1} KB) - eta {:.0}s cold / {:.0}s if cached (~{} gen)",
local.tokens,
prompt.len() as f64 / 1024.0,
eta.total_s(),
eta.cached_s(),
eta.gen_est
);
eprint!(" ");
let _ = std::io::stderr().flush();
if ollama::verbose() {
eprintln!(
"\n--- prompt [{label}] ---\n{prompt}\n--- end prompt ---"
);
}
let mut n = 0usize;
let r = ollama::generate_via(
self.t,
prompt,
label,
sampling,
eta,
&mut |chunk| n = tick(chunk, n),
)?;
eprintln!();
if self.t.timings_are_real() {
ollama::observe_gen(label, r.eval_tokens);
}
if ollama::verbose() {
if !r.thinking.is_empty() {
eprintln!(
"\n--- reasoning [{label}] ---\n{}\n--- end reasoning ---",
r.thinking
);
}
eprintln!("--- end reply [{label}] ---");
}
let actual = r.ms as f64 / 1000.0;
let basis = if ollama::last_cached() {
eta.cached_s()
} else {
eta.total_s()
};
let delta = (actual - basis) / basis * 100.0;
eprintln!(
" [{label}] <- {} sent · {} gen · {actual:.1}s (eta {:.0}s, {delta:+.0}%){}",
r.prompt_tokens,
r.eval_tokens,
basis,
if ollama::last_cached() {
" [prefix CACHED]"
} else {
""
}
);
if !r.thinking.is_empty() {
eprintln!(
" (+{} reasoning tokens, hidden -- see -v){}",
r.thinking.len() / 4,
if ollama::last_load_ms() > 500 {
format!(
" [endpoint was COLD: {}ms model load]",
ollama::last_load_ms()
)
} else {
String::new()
}
);
}
if r.prompt_tokens.abs_diff(local.tokens) > local.tokens / 10 {
eprintln!(
" [{label}] note: local count {} vs server {} -- >10% apart",
local.tokens, r.prompt_tokens
);
}
self.log.push(Step {
label,
prompt_tokens: r.prompt_tokens,
eval_tokens: r.eval_tokens,
ms: r.ms,
});
Ok(r.text)
}
}
pub fn oneshot(r: &Run) -> Result<Vec<Step>, String> {
let spec_path = r.node.join("SPEC.md");
let mod_path = r.node.join("mod.rs");
let spec_txt =
std::fs::read_to_string(&spec_path).map_err(|e| e.to_string())?;
let original =
std::fs::read_to_string(&mod_path).map_err(|e| e.to_string())?;
let (impl_r, tests_r) = split_module(&original);
let inv = spec_txt
.lines()
.find(|l| l.starts_with(&format!("{}:", r.invariant)))
.ok_or_else(|| format!("{} not declared", r.invariant))?
.to_string();
ollama::prewarm(r.transport);
let mut c = Caller::new(r.transport);
let reply = c.run(
&format!(
"{}\n--- spec (complete) ---\n{spec_txt}\n\n\
--- implementation (complete) ---\n{impl_r}\n\n\
--- existing tests ---\n{tests_r}\n\n\
Prove and implement this invariant:\n {inv}\n\nTask: {}\n\n\
Reply with TWO ```rust fenced blocks: first the new `#[test]` function, \
then the new implementation function(s) to add. The test must fail against \
the current implementation and pass against your new one.",
NOTATION, r.task
),
"monolith",
)?;
let blocks: Vec<&str> = reply
.split("```")
.skip(1)
.step_by(2)
.map(|b| b.strip_prefix("rust").unwrap_or(b).trim())
.collect();
let [test_block, impl_block, ..] = blocks.as_slice() else {
return Err(format!(
"monolith returned {} code blocks, expected 2",
blocks.len()
));
};
std::fs::write(
&mod_path,
insert_impl(&insert_test(&original, test_block), impl_block),
)
.map_err(|e| e.to_string())?;
let (ok, out) = crate::land::gate_with(r.root, &r.cargo)?;
let sent: u64 = c.log.iter().map(|s| s.prompt_tokens).sum();
eprintln!("\n 1 round-trip · {sent} tok sent · max single call {sent}");
if ok {
eprintln!(" VERDICT: MERGEABLE -- gates green");
Ok(c.log)
} else {
eprintln!("{}", crate::land::tail(&out, 1200));
Err("NOT mergeable -- gates red".into())
}
}
pub const DEFAULT_REPAIRS: usize = 3;
pub struct Run<'a> {
pub root: &'a Path,
pub node: &'a Path,
pub owner: &'a Path,
pub invariant: &'a str,
pub task: &'a str,
pub max_repair: usize,
pub cargo: String,
pub transport: &'a dyn ollama::Transport,
}
impl<'a> Run<'a> {
#[must_use]
pub fn new(
root: &'a Path,
node: &'a Path,
invariant: &'a str,
task: &'a str,
) -> Self {
Self {
root,
node,
owner: node,
invariant,
task,
max_repair: DEFAULT_REPAIRS,
cargo: crate::land::cargo_bin(),
transport: &ollama::Http,
}
}
#[must_use]
pub const fn repairs(mut self, n: usize) -> Self {
self.max_repair = n;
self
}
#[must_use]
pub fn with_cargo(mut self, cargo: &str) -> Self {
self.cargo = cargo.to_string();
self
}
#[must_use]
pub const fn owned_by(mut self, owner: &'a Path) -> Self {
self.owner = owner;
self
}
}
pub fn drive(
root: &Path,
node: &Path,
invariant: &str,
task: &str,
max_repair: usize,
) -> Result<Vec<Step>, String> {
drive_run(&Run::new(root, node, invariant, task).repairs(max_repair))
}
struct Restore {
path: std::path::PathBuf,
original: String,
armed: bool,
}
impl Restore {
fn arm(path: &Path, original: &str) -> Self {
Self {
path: path.to_path_buf(),
original: original.to_string(),
armed: true,
}
}
fn keep(&mut self) {
self.armed = false;
}
}
impl Drop for Restore {
fn drop(&mut self) {
if self.armed {
let _ = std::fs::write(&self.path, &self.original);
}
}
}
#[derive(Debug, Clone)]
pub struct Candidate {
pub code: String,
pub green: bool,
pub findings: usize,
}
#[must_use]
pub fn best(cands: &[Candidate]) -> Option<usize> {
cands
.iter()
.enumerate()
.filter(|(_, c)| c.green && c.findings == 0)
.map(|(i, _)| i)
.next()
}
#[derive(Debug, PartialEq, Eq)]
pub enum Pick {
Merit(usize),
Unfinished(usize),
NoWinner,
}
#[must_use]
pub fn select(cands: &[Candidate]) -> Pick {
if let Some(i) = best(cands) {
return Pick::Merit(i);
}
if cands.is_empty() {
return Pick::NoWinner;
}
if cands.iter().any(|c| c.green) {
return Pick::NoWinner;
}
Pick::Unfinished(0)
}
#[must_use]
pub fn candidate_count() -> usize {
std::env::var("SHERD_CANDIDATES")
.ok()
.and_then(|v| v.parse::<usize>().ok())
.unwrap_or(1)
.clamp(1, 5)
}
#[must_use]
pub fn named_fn(task: &str) -> Option<String> {
task.split('`').skip(1).step_by(2).find_map(|seg| {
let name = seg.split('(').next()?.trim();
if seg.contains('(')
&& !name.is_empty()
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
&& !name.starts_with(|c: char| c.is_ascii_digit())
{
Some(name.to_string())
} else {
None
}
})
}
#[must_use]
pub fn blind_prompt(inv: &str, added: &str) -> String {
format!(
"{NOTATION}\nInvariant:\n {inv}\n\n\
Proposed implementation:\n```rust\n{added}\n```\n\n\
You are deliberately NOT shown the test. Judge the code against the \
invariant alone. Answer NO if any of these hold: it returns a constant, \
an empty collection or a default regardless of its input; it names a \
parameter it never reads, with or without a leading underscore; it \
reports a quantity other than the one the invariant is about; or its \
own comments describe it as a stub, a placeholder, or as satisfying \
tests. Answer YES only if code that violated the invariant would \
differ from this. Answer YES or NO on the first line, then one sentence."
)
}
#[must_use]
pub fn is_yes(verdict: &str) -> bool {
verdict
.trim()
.lines()
.next()
.unwrap_or("")
.trim()
.to_uppercase()
.starts_with("YES")
}
pub fn drive_from(
root: &Path,
node: &Path,
owner: &Path,
invariant: &str,
task: &str,
max_repair: usize,
) -> Result<Vec<Step>, String> {
drive_run(
&Run::new(root, node, invariant, task)
.repairs(max_repair)
.owned_by(owner),
)
}
struct Ctx<'a> {
run: &'a Run<'a>,
mod_path: std::path::PathBuf,
inv: String,
spec_rules: String,
surface: String,
tests: String,
in_scope: String,
}
fn red_test(ctx: &Ctx, c: &mut Caller) -> Result<String, String> {
let (inv, surface, task) = (&ctx.inv, &ctx.surface, ctx.run.task);
let base = format!(
"{NOTATION}\n--- spec (rules) ---\n{}\n\n\
--- public surface (signatures only) ---\n{surface}\n\n\
--- existing tests in this module ---\n{}\n\n\
Write ONE new Rust `#[test]` function proving this invariant:\n {inv}\n\n\
Task: {task}\n\n\
It must FAIL against the current implementation, and fail at an assertion -- \
not by failing to compile. It MUST include data that actually violates the \
invariant, and assert that the violation is reported. Use only items that \
already exist, plus the ONE new public function you expect to be written. \
Reply with a single ```rust fenced block containing only the test function.",
ctx.spec_rules, ctx.tests
);
let mut test_fn = String::new();
let mut objection = String::new();
for attempt in 0..3 {
let prompt = if attempt == 0 {
base.clone()
} else {
format!(
"{base}\n\nYour previous attempt was REJECTED by review:\n\
```rust\n{test_fn}\n```\nReason: {objection}\n\
Write a corrected test that answers that objection."
)
};
let label: &'static str = if attempt == 0 {
"1 red-test"
} else {
"1 red-retry"
};
test_fn = ollama::rust_block(&c.run(&prompt, label)?);
if let Some(name) = named_fn(task)
&& !test_fn.contains(&format!("{name}("))
{
objection = contract_objection(&name);
continue;
}
match judge_test(ctx, c, &test_fn, attempt)? {
None => return Ok(test_fn),
Some(no) => objection = no,
}
}
Err(format!(
"judge rejected the test 3 times -- last objection: {objection}"
))
}
fn contract_objection(name: &str) -> String {
eprintln!(" contract: test does not call `{name}` -- rejected locally");
format!(
"the task names `{name}` and this test never calls it. \
Write a test that calls `{name}` directly."
)
}
fn judge_test(
ctx: &Ctx,
c: &mut Caller,
test_fn: &str,
attempt: usize,
) -> Result<Option<String>, String> {
let (surface, in_scope, inv) = (&ctx.surface, &ctx.in_scope, &ctx.inv);
let red_note = red_note(ctx.run.task);
let verdict = c.run(
&format!(
"{NOTATION}\n--- data model ---\n{surface}\n--- already available to a test ---\n{in_scope}\n\nInvariant:\n {inv}\n\n\
Proposed test:\n```rust\n{test_fn}\n```\n\n\
{red_note}Answer YES only if BOTH hold: (a) the test exercises the quantity the \
invariant is actually about -- check the field names against the data model \
above, a test asserting on the wrong field proves nothing; and (b) an \
implementation violating the invariant would fail it. If the function \
DETECTS something, the test MUST include input that should be detected \
and assert it IS -- a test asserting only that nothing was found is \
satisfied by a function that always finds nothing. Exhaustiveness is NOT \
required. Answer YES or NO on the first line, then one sentence."
),
if attempt == 0 { "1b judge" } else { "1b re-judge" },
)?;
let first = verdict.trim().lines().next().unwrap_or("").to_string();
eprintln!(" judge: {}", first.chars().take(78).collect::<String>());
Ok(if is_yes(&verdict) {
None
} else {
Some(verdict.trim().to_string())
})
}
struct Tried {
cand: Candidate,
out: String,
}
fn green_candidates(
ctx: &Ctx,
c: &mut Caller,
test_fn: &str,
red_out: &str,
) -> Result<Vec<Tried>, String> {
let prompt = green_prompt(ctx, test_fn, red_out);
let n = candidate_count();
let mut tried: Vec<Tried> = Vec::new();
let with_test =
std::fs::read_to_string(&ctx.mod_path).map_err(|e| e.to_string())?;
for k in 0..n {
let label: &'static str =
if k == 0 { "2 green" } else { "2 green-alt" };
let code = ollama::rust_block(&c.run_sampled(
&prompt,
label,
ollama::Sampling::candidate(k),
)?);
std::fs::write(&ctx.mod_path, insert_impl(&with_test, &code))
.map_err(|e| e.to_string())?;
let (g, o) = crate::land::gate_with(ctx.run.root, &ctx.run.cargo)?;
let found = candidate_findings(ctx, &code)?;
if n > 1 {
report_candidate(k, g, &found);
}
tried.push(Tried {
cand: Candidate {
code,
green: g,
findings: found.len(),
},
out: o,
});
}
Ok(tried)
}
fn green_prompt(ctx: &Ctx, test_fn: &str, red_out: &str) -> String {
let surface = &ctx.surface;
let wanted = expected_calls(test_fn, surface);
eprintln!(
" contract: {}",
if wanted.is_empty() {
"(none detected)".into()
} else {
wanted.join(", ")
}
);
let contract = if wanted.is_empty() {
String::new()
} else {
format!(
"--- the test calls these; define EXACTLY these names and signatures ---\n{}\n\n",
wanted.join("\n")
)
};
format!(
"--- existing API (signatures; call these, do not reimplement) ---\n{surface}\n\n--- failing test ---\n```rust\n{test_fn}\n```\n\n\
{contract}\
--- failure ---\n{}\n\n\
Write ONLY the new function(s) to ADD to the implementation so this test passes. \
Do not restate existing code. \
Do not modify the test. Reply with a single ```rust fenced block.",
crate::land::tail(red_out, 1500)
)
}
fn candidate_findings(
ctx: &Ctx,
code: &str,
) -> Result<Vec<crate::review::Finding>, String> {
let added = crate::code::public_fns(code);
let cur =
std::fs::read_to_string(&ctx.mod_path).map_err(|e| e.to_string())?;
let (_ci, ct) = split_module(&cur);
let mut found = crate::review::negative_only(code, ct, &added);
found.extend(crate::review::ignored_input(code, &added));
found.extend(crate::review::undocumented(code, &added));
Ok(found)
}
fn report_candidate(k: usize, green: bool, found: &[crate::review::Finding]) {
eprintln!(
" candidate {k}: {} · {}",
if green { "gate GREEN" } else { "gate red" },
if found.is_empty() {
"clean".to_string()
} else {
found
.iter()
.map(|f| f.rule.to_string())
.collect::<Vec<_>>()
.join(", ")
}
);
}
fn repair(
ctx: &Ctx,
c: &mut Caller,
test_fn: &str,
seed: (bool, String, String),
) -> Result<(bool, String, String), String> {
let (mut ok, mut out, mut last_added) = seed;
for i in 0..ctx.run.max_repair {
if ok {
break;
}
eprintln!(" gate: FAIL -- repair {}/{}", i + 1, ctx.run.max_repair);
let cur = std::fs::read_to_string(&ctx.mod_path)
.map_err(|e| e.to_string())?;
let (cur_impl, cur_tests) = split_module(&cur);
let label: &'static str =
if i == 0 { "4 repair-1" } else { "4 repair-n" };
let fixed = ollama::rust_block(&c.run(
&repair_prompt(signatures(cur_impl), &last_added, test_fn, &out),
label,
)?);
let Some(replaced) = cur_impl
.contains(last_added.trim())
.then(|| cur_impl.replace(last_added.trim(), fixed.trim()))
else {
return Err("repair lost track of the previous insertion -- \
refusing to guess where it went"
.into());
};
last_added = fixed;
std::fs::write(
&ctx.mod_path,
format!("{}\n\n{}", replaced.trim_end(), cur_tests),
)
.map_err(|e| e.to_string())?;
let g = crate::land::gate_with(ctx.run.root, &ctx.run.cargo)?;
ok = g.0;
out = g.1;
}
Ok((ok, out, last_added))
}
fn repair_prompt(
surface: String,
last_added: &str,
test_fn: &str,
out: &str,
) -> String {
format!(
"--- existing API (signatures) ---\n{surface}\n\n--- your current attempt ---\n{last_added}\n\n--- test ---\n```rust\n{test_fn}\n```\n\n\
--- failure ---\n{}\n\n\
Reply with ONLY the corrected version of the function(s) you previously \
added, in one ```rust block. Do not restate unrelated code, do not remove \
module documentation, and do not change the behaviour of functions that \
already existed. Do not modify the test.",
crate::land::tail(out, 2000)
)
}
pub fn drive_run(r: &Run) -> Result<Vec<Step>, String> {
let spec_path = r.node.join("SPEC.md");
let mod_path = r.node.join("mod.rs");
let spec_txt = std::fs::read_to_string(&spec_path)
.map_err(|e| format!("{}: {e}", spec_path.display()))?;
let original = std::fs::read_to_string(&mod_path)
.map_err(|e| format!("{}: {e}", mod_path.display()))?;
let (impl_r, tests_r) = split_module(&original);
let mut guard = Restore::arm(&mod_path, &original);
let ctx = Ctx {
run: r,
inv: declared_invariant(r, &spec_txt)?,
spec_rules: rule_depth(&spec_txt),
surface: signatures(impl_r),
in_scope: test_decls(tests_r),
tests: tests_r.to_string(),
mod_path,
};
eprintln!("node {} · {}\n", r.node.display(), ctx.inv.trim());
ollama::prewarm(r.transport);
let mut c = Caller::new(r.transport);
let test_fn = red_test(&ctx, &mut c)?;
std::fs::write(&ctx.mod_path, insert_test(&original, &test_fn))
.map_err(|e| e.to_string())?;
let (red_ok, red_out) = crate::land::gate_with(r.root, &r.cargo)?;
if red_ok {
return Err(
"test passes already -- not a red test, nothing to drive".into()
);
}
eprintln!(" gate: RED as required");
let tried = green_candidates(&ctx, &mut c, &test_fn, &red_out)?;
let pick = pick_candidate(&tried)?;
let Some(won) = tried.get(pick) else {
unreachable!("select returns an index into the slice it was given")
};
let with_test =
std::fs::read_to_string(&ctx.mod_path).map_err(|e| e.to_string())?;
std::fs::write(&ctx.mod_path, insert_impl(&with_test, &won.cand.code))
.map_err(|e| e.to_string())?;
let seed = (won.cand.green, won.out.clone(), won.cand.code.clone());
let (ok, out, last_added) = repair(&ctx, &mut c, &test_fn, seed)?;
if ok {
blind_check(&ctx, &mut c, &last_added)?;
}
report_verdict(c, ok, &out, &mut guard)
}
fn declared_invariant(r: &Run, spec_txt: &str) -> Result<String, String> {
let inv_path = r.owner.join("SPEC.md");
let inv_txt = std::fs::read_to_string(&inv_path)
.unwrap_or_else(|_| spec_txt.to_string());
inv_txt
.lines()
.find(|l| l.starts_with(&format!("{}:", r.invariant)))
.map(str::to_string)
.ok_or_else(|| {
format!(
"{} not declared in {} -- a test for an invariant that does \
not exist encodes an unstated rule",
r.invariant,
inv_path.display()
)
})
}
fn pick_candidate(tried: &[Tried]) -> Result<usize, String> {
let n = tried.len();
let cands: Vec<Candidate> = tried.iter().map(|t| t.cand.clone()).collect();
match select(&cands) {
Pick::Merit(i) => {
if n > 1 {
eprintln!(" merit: candidate {i} of {n} kept");
}
Ok(i)
}
Pick::Unfinished(i) => {
if n > 1 {
eprintln!(" merit: all {n} red -- repairing candidate {i}");
}
Ok(i)
}
Pick::NoWinner => Err(format!(
"{n} candidate(s) green but carrying findings -- reverted. repair \
polishes a stub, it does not fix one"
)),
}
}
fn blind_check(
ctx: &Ctx,
c: &mut Caller,
last_added: &str,
) -> Result<(), String> {
let verdict =
c.run(&blind_prompt(&ctx.inv, last_added), "5 blind judge")?;
let first = verdict.trim().lines().next().unwrap_or("").to_string();
eprintln!(" blind: {}", first.chars().take(78).collect::<String>());
if is_yes(&verdict) {
return Ok(());
}
Err(format!(
"gates green, second lens says NO -- reverted. objection: {}",
verdict.trim()
))
}
fn report_verdict(
c: Caller,
ok: bool,
out: &str,
guard: &mut Restore,
) -> Result<Vec<Step>, String> {
let sent: u64 = c.log.iter().map(|s| s.prompt_tokens).sum();
let max = c.log.iter().map(|s| s.prompt_tokens).max().unwrap_or(0);
eprintln!(
"\n {} round-trips · {sent} tok sent · max single call {max}",
c.log.len()
);
if !ok {
eprintln!("{}", crate::land::tail(out, 2000));
return Err(
"NOT mergeable -- gates red after repair budget, module restored"
.into(),
);
}
eprintln!(" VERDICT: MERGEABLE -- gates green + second lens");
guard.keep();
Ok(c.log)
}
pub fn classify_failure(report: &str) -> bool {
let s = report.trim_start();
s.starts_with("error") || s.contains("error[")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_task_that_names_a_function_names_it_with_parens() {
assert_eq!(
named_fn(
"`post_with_retry(&dyn Transport, url, body)` -- a NEW fn"
),
Some("post_with_retry".into())
);
assert_eq!(named_fn("wire `generate_via` to the retry path"), None);
assert_eq!(named_fn("record per-request template overhead"), None);
assert_eq!(
named_fn("`§F` rows, then `depth(edges)`"),
Some("depth".into())
);
}
#[test]
fn an_unkept_run_restores_the_module() {
let dir = std::env::temp_dir().join("sherd-restore-test");
std::fs::create_dir_all(&dir).unwrap();
let f = dir.join("mod.rs");
std::fs::write(&f, "fn original() {}\n").unwrap();
{
let _g = Restore::arm(&f, "fn original() {}\n");
std::fs::write(&f, "fn generated( { // does not compile\n")
.unwrap();
}
assert_eq!(
std::fs::read_to_string(&f).unwrap(),
"fn original() {}\n",
"a run that keeps nothing must leave nothing behind"
);
{
let mut g = Restore::arm(&f, "fn original() {}\n");
std::fs::write(&f, "fn kept() {}\n").unwrap();
g.keep();
}
assert_eq!(std::fs::read_to_string(&f).unwrap(), "fn kept() {}\n");
let _ = std::fs::remove_dir_all(&dir);
}
fn cand(green: bool, findings: usize) -> Candidate {
Candidate {
code: format!("fn c{findings}() {{}}"),
green,
findings,
}
}
#[test]
fn a_bad_field_has_no_winner() {
assert_eq!(
best(&[cand(false, 0), cand(true, 2), cand(false, 9)]),
None
);
assert_eq!(best(&[]), None);
}
#[test]
fn an_all_red_field_is_unfinished_not_bad() {
assert_eq!(
select(&[cand(false, 1), cand(false, 1), cand(false, 1)]),
Pick::Unfinished(0),
"repair is what answers a red gate"
);
assert_eq!(select(&[cand(false, 9)]), Pick::Unfinished(0));
}
#[test]
fn green_with_a_finding_stays_fatal() {
assert_eq!(select(&[cand(true, 1), cand(false, 0)]), Pick::NoWinner);
assert_eq!(select(&[cand(true, 0), cand(true, 1)]), Pick::Merit(0));
assert_eq!(select(&[]), Pick::NoWinner);
}
#[test]
fn merit_must_be_demonstrated_to_displace_the_deterministic_call() {
assert_eq!(best(&[cand(true, 0), cand(true, 0)]), Some(0));
assert_eq!(best(&[cand(true, 1), cand(true, 0)]), Some(1));
assert_eq!(best(&[cand(false, 0), cand(true, 0)]), Some(1));
}
#[test]
fn candidate_count_is_bounded_at_both_ends() {
assert_eq!(candidate_count().clamp(1, 5), candidate_count());
}
#[test]
fn the_second_lens_is_never_shown_the_test() {
let p = blind_prompt(
"V1: report every dir with no owner",
"pub fn orphans(_d: &[Dir]) -> Vec<Dir> { Vec::new() }",
);
assert!(p.contains("orphans"), "must carry the implementation");
assert!(
p.contains("V1: report every dir"),
"must carry the invariant"
);
assert!(
!p.to_lowercase().contains("#[test]"),
"the test must not reach the second lens: {p}"
);
assert!(!p.contains("assert"), "no test body may leak in: {p}");
}
#[test]
fn a_verdict_is_yes_only_on_the_first_line() {
assert!(is_yes("YES\nit reads its input"));
assert!(is_yes(" yes -- fine "));
assert!(
!is_yes("NO\nreturns YES for everything"),
"a YES in the explanation is not assent"
);
assert!(!is_yes(""));
}
const SRC: &str = "pub fn a() {}\n\n#[cfg(test)]\nmod tests {\n #[test]\n fn t() {}\n}\n";
#[test]
fn a_worker_prompt_carries_no_supervisor_text() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let spec =
std::fs::read_to_string(root.join("src/fed/SPEC.md")).unwrap();
let src = std::fs::read_to_string(root.join("src/fed/mod.rs")).unwrap();
let (impl_r, tests_r) = split_module(&src);
let prompt = format!(
"{NOTATION}{}{}{}",
rule_depth(&spec),
signatures(impl_r),
tests_r
);
for marker in ["/sit", "git revert", "halt(sit)", "maintenance mode"] {
assert!(
!prompt.contains(marker),
"supervisor instruction `{marker}` reached a worker prompt"
);
}
}
#[test]
fn rule_depth_drops_the_archive_sections() {
let s = "## \u{a7}G GOAL\ngoal\n\n## \u{a7}V INVARIANTS\nV1: a\n\n## \u{a7}B BUGS\nB1|x|cause|fix\n";
let r = rule_depth(s);
assert!(r.contains("V1: a"), "rules must survive: {r}");
assert!(!r.contains("B1|"), "§B must be dropped: {r}");
let s2 = "## \u{a7}T TASKS\nT1|.|do the thing|V1\n\n## \u{a7}B BUGS\nB1|x|c|f\n";
assert!(
rule_depth(s2).contains("T1|"),
"§T is the plan and must survive"
);
assert!(!r.contains("BUGS"), "§B header must be dropped: {r}");
}
#[test]
fn insert_impl_never_touches_the_test_region() {
let out = insert_impl(SRC, "pub fn b() {}");
let (_, t) = split_module(&out);
assert_eq!(
t,
split_module(SRC).1,
"test region must be byte-identical"
);
assert!(out.contains("pub fn b"));
}
const MODULE: &str = "pub fn a() {}\n\n#[cfg(test)]\nmod tests {\n #[test]\n fn t() {}\n}\n";
const REPLY_WITH_TEST: &str = "pub fn b() -> u8 { 1 }\n\n#[cfg(test)]\nmod tests {\n #[test]\n fn u() {}\n}";
#[test]
fn a_reply_carrying_its_own_test_module_leaves_one_module() {
let out = insert_impl(MODULE, REPLY_WITH_TEST);
let n = out.matches("\nmod tests {").count();
assert_eq!(n, 1, "exactly one test module survives: {out}");
}
#[test]
fn the_implementation_lands_and_the_replies_test_does_not() {
let out = insert_impl(MODULE, REPLY_WITH_TEST);
assert!(out.contains("pub fn b()"), "the implementation lands");
assert!(!out.contains("fn u()"), "step 2 must not write tests");
assert!(out.contains("fn t()"), "the module's own test is kept");
}
#[test]
fn a_well_behaved_reply_is_unchanged_by_the_guard() {
let out = insert_impl(MODULE, "pub fn b() -> u8 { 1 }");
assert!(out.contains("pub fn b()"));
assert_eq!(out.matches("\nmod tests {").count(), 1);
}
#[test]
fn insert_test_lands_inside_the_tests_module() {
let out = insert_test(SRC, " #[test]\n fn u() {}");
assert!(split_module(&out).1.contains("fn u()"));
}
#[test]
fn classify_failure_works() {
let compile_report =
r#"error[E0425]: cannot find value `foo` in this scope"#;
let assert_report = "thread 'main' panicked at 'assertion failed: x == y', src/main.rs:10:5";
assert!(
classify_failure(compile_report),
"Compile error should be classified as a compile failure"
);
assert!(
!classify_failure(assert_report),
"Assertion failure should not be classified as a compile failure"
);
}
}
#[cfg(test)]
mod loop_tests {
use super::*;
#[test]
fn a_row_naming_a_function_tells_the_judge_it_is_not_written_yet() {
let n = red_note("`post_with_retry(&dyn Transport, url)` -- a NEW fn");
assert!(n.contains("post_with_retry"), "names the target: {n}");
assert!(
n.contains("does NOT exist yet"),
"says why it is absent: {n}"
);
assert!(n.contains("not a reason"), "and that it is not a NO: {n}");
}
#[test]
fn a_row_naming_no_function_says_nothing_to_the_judge() {
assert_eq!(red_note("tighten the ceiling comparison"), "");
assert_eq!(
red_note("post_with_retry(url) with no backticks"),
"",
"an unbackticked name is not a named function"
);
}
use std::cell::Cell;
use std::io::BufRead;
use std::time::Duration;
struct Scripted {
replies: Vec<String>,
at: Cell<usize>,
}
impl Scripted {
fn new(replies: &[&str]) -> Self {
Self {
replies: replies.iter().map(|s| (*s).to_string()).collect(),
at: Cell::new(0),
}
}
fn frames(text: &str) -> String {
let esc = text
.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n");
format!(
"{{\"response\":\"{esc}\",\"done\":false}}\n\
{{\"response\":\"\",\"done\":true,\
\"prompt_eval_count\":10,\"eval_count\":5}}\n"
)
}
}
impl ollama::Transport for Scripted {
fn timings_are_real(&self) -> bool {
false
}
fn post(
&self,
_url: &str,
_body: &str,
_t: Duration,
) -> Result<Box<dyn BufRead + Send>, String> {
let i = self.at.get();
let text = self
.replies
.get(i)
.ok_or_else(|| format!("script exhausted at call {i}"))?;
self.at.set(i.wrapping_add(1));
Ok(Box::new(std::io::Cursor::new(Self::frames(text))))
}
}
fn write_exec(path: &Path, body: &str) -> Result<(), String> {
crate::testrepo::write_script(path, body).map(|_| ())
}
fn scripted_cargo(dir: &Path, red_times: u32) -> Result<String, String> {
let counter = dir.join("gate-count");
let script = dir.join("scripted-cargo");
let body = format!(
"#!/bin/sh\ncase \"$1\" in test) ;; *) exit 0 ;; esac\n\
n=$(cat {c} 2>/dev/null || echo 0)\n\
echo $((n+1)) > {c}\n\
if [ \"$n\" -lt \"{red_times}\" ]; then \
echo 'test result: FAILED'; exit 1; fi\n\
echo 'test result: ok'\nexit 0\n",
c = counter.display()
);
write_exec(&script, &body)?;
Ok(script.display().to_string())
}
fn repo_fixture(root: &Path) -> Result<(), String> {
std::fs::write(
root.join("SPEC.md"),
"# SPEC\n\n## \u{a7}F FEDERATION\n\ndir|owns|\u{22a5}owns|tokens\n\
node|the fixture|everything else|-\n",
)
.map_err(|e| format!("root spec: {e}"))?;
std::fs::write(root.join(".sherd-slices"), "# none\n")
.map_err(|e| format!("slices: {e}"))
}
fn node_fixture(dir: &Path) -> Result<(), String> {
std::fs::create_dir_all(dir).map_err(|e| format!("mkdir: {e}"))?;
std::fs::write(
dir.join("SPEC.md"),
"# SPEC\n\n## \u{a7}V INVARIANTS\n\nV1: a doubling returns twice its input\n",
)
.map_err(|e| format!("spec: {e}"))?;
std::fs::write(
dir.join("mod.rs"),
"pub fn existing() -> u8 {\n 1\n}\n\n#[cfg(test)]\nmod tests {\n\
\x20 #[test]\n fn t() {}\n}\n",
)
.map_err(|e| format!("mod: {e}"))
}
fn script() -> Scripted {
Scripted::new(&[
"```rust\n#[test]\nfn doubles() { assert_eq!(double(2), 4); }\n```",
"YES it exercises the invariant",
"```rust\n/// Twice its input.\npub fn double(n: u8) -> u8 { n * 2 }\n```",
"YES it reads its input",
])
}
#[test]
fn the_loop_runs_end_to_end_with_no_endpoint() {
assert_eq!(drive_a_scripted_run(), Ok(()));
}
fn scratch(
tag: &str,
) -> Result<(std::path::PathBuf, std::path::PathBuf), String> {
use std::sync::atomic::{AtomicUsize, Ordering};
static N: AtomicUsize = AtomicUsize::new(0);
let n = N.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir()
.join(format!("sherd-loop-{tag}-{}-{n}", std::process::id()));
let node = dir.join("node");
node_fixture(&node)?;
repo_fixture(&dir)?;
Ok((dir, node))
}
fn drive_a_scripted_run() -> Result<(), String> {
let (dir, node) = scratch("repair")?;
let cargo = scripted_cargo(&dir, 1)?;
let t = script();
let base = Run::new(&dir, &node, "V1", "add double()")
.with_cargo(&cargo)
.repairs(0);
let out = drive_run(&Run {
transport: &t,
..base
});
let after = std::fs::read_to_string(node.join("mod.rs"))
.map_err(|e| format!("read back: {e}"))?;
let _ = std::fs::remove_dir_all(&dir);
check_outcome(&out, &after)
}
fn monolith_script(blocks: &str) -> Scripted {
Scripted::new(&[blocks])
}
const TWO_BLOCKS: &str = "```rust\n#[test]\nfn doubles() { assert_eq!(double(2), 4); }\n```\n\
and now the implementation:\n\
```rust\npub fn double(n: u8) -> u8 { n * 2 }\n```";
#[test]
fn the_monolith_arm_runs_and_reports_mergeable_on_a_green_gate() {
assert_eq!(monolith_green(), Ok(()));
}
fn monolith_green() -> Result<(), String> {
let (dir, node) = scratch("mono-green")?;
let cargo = scripted_cargo(&dir, 0)?;
let t = monolith_script(TWO_BLOCKS);
let base = scripted_run(&dir, &node, &cargo, 0);
let out = oneshot(&Run {
transport: &t,
..base
});
let after = std::fs::read_to_string(node.join("mod.rs"))
.map_err(|e| format!("read back: {e}"))?;
let _ = std::fs::remove_dir_all(&dir);
assert!(out.is_ok(), "a green gate is MERGEABLE: {out:?}");
assert_both_blocks_landed(&after);
Ok(())
}
fn assert_both_blocks_landed(after: &str) {
assert!(after.contains("n * 2"), "the impl block landed: {after}");
assert!(after.contains("doubles"), "the test block landed too");
}
#[test]
fn the_monolith_arm_reports_red_rather_than_claiming_a_merge() {
assert_eq!(monolith_red(), Ok(()));
}
fn monolith_red() -> Result<(), String> {
let (dir, node) = scratch("mono-red")?;
let cargo = scripted_cargo(&dir, 9)?;
let t = monolith_script(TWO_BLOCKS);
let base = scripted_run(&dir, &node, &cargo, 0);
let out = oneshot(&Run {
transport: &t,
..base
});
let _ = std::fs::remove_dir_all(&dir);
assert!(out.is_err(), "a red gate is NOT mergeable: {out:?}");
Ok(())
}
#[test]
fn a_monolith_reply_short_of_two_blocks_is_an_error() {
assert_eq!(monolith_one_block(), Ok(()));
}
fn monolith_one_block() -> Result<(), String> {
let (dir, node) = scratch("mono-one")?;
let cargo = scripted_cargo(&dir, 0)?;
let t =
monolith_script("```rust\npub fn double(n: u8) -> u8 { n*2 }\n```");
let base = scripted_run(&dir, &node, &cargo, 0);
let out = oneshot(&Run {
transport: &t,
..base
});
let after = std::fs::read_to_string(node.join("mod.rs"))
.map_err(|e| format!("read back: {e}"))?;
let _ = std::fs::remove_dir_all(&dir);
assert_unusable_reply_wrote_nothing(&out, &after)
}
fn assert_unusable_reply_wrote_nothing(
out: &Result<Vec<Step>, String>,
after: &str,
) -> Result<(), String> {
let Err(msg) = out else {
return Err("one block is not two".into());
};
assert!(msg.contains("expected 2"), "say what was wrong: {msg}");
assert!(
!after.contains("double"),
"nothing is written when the reply is unusable: {after}"
);
Ok(())
}
fn repair_script() -> Scripted {
Scripted::new(&[
"```rust\n#[test]\nfn doubles() { assert_eq!(double(2), 4); }\n```",
"YES it exercises the invariant",
"```rust\n/// Twice its input.\npub fn double(n: u8) -> u8 { n + n }\n```",
"```rust\n/// Twice its input.\npub fn double(n: u8) -> u8 { n * 2 }\n```",
"YES it reads its input",
])
}
#[test]
fn a_red_gate_sends_the_loop_through_repair() {
assert_eq!(drive_with_one_repair(), Ok(()));
}
fn scripted_run<'a>(
dir: &'a Path,
node: &'a Path,
cargo: &str,
repairs: usize,
) -> Run<'a> {
Run::new(dir, node, "V1", "add double()")
.with_cargo(cargo)
.repairs(repairs)
}
fn drive_with_one_repair() -> Result<(), String> {
let (dir, node) = scratch("repair")?;
let cargo = scripted_cargo(&dir, 2)?;
let t = repair_script();
let base = scripted_run(&dir, &node, &cargo, 1);
let out = drive_run(&Run {
transport: &t,
..base
});
let after = std::fs::read_to_string(node.join("mod.rs"))
.map_err(|e| format!("read back: {e}"))?;
let _ = std::fs::remove_dir_all(&dir);
assert!(out.is_ok(), "a repaired run that goes green lands: {out:?}");
assert_repaired_body_landed(&after);
Ok(())
}
fn assert_repaired_body_landed(after: &str) {
assert!(
after.contains("n * 2"),
"the REPAIRED body is what lands, not the first attempt: {after}"
);
assert!(
!after.contains("n + n"),
"repair REPLACES its own previous insertion rather than appending \
beside it -- appending would duplicate the definition (B5): {after}"
);
}
#[test]
fn repair_exhaustion_reports_rather_than_landing_a_red_tree() {
assert_eq!(exhausted_repairs_do_not_pass(), Ok(()));
}
fn exhausted_repairs_do_not_pass() -> Result<(), String> {
let (dir, node) = scratch("exhaust")?;
let cargo = scripted_cargo(&dir, 9)?;
let t = repair_script();
let base = scripted_run(&dir, &node, &cargo, 1);
let out = drive_run(&Run {
transport: &t,
..base
});
let _ = std::fs::remove_dir_all(&dir);
assert!(
out.is_err(),
"an exhausted repair reports, never a silent pass: {out:?}"
);
Ok(())
}
fn check_outcome(
out: &Result<Vec<Step>, String>,
after: &str,
) -> Result<(), String> {
let steps = match out {
Ok(s) => s,
Err(e) => return Err(format!("the run must be KEPT, got: {e}")),
};
assert!(!steps.is_empty(), "a kept run records what it cost");
assert!(
after.contains("double"),
"the implementation must be IN the module, not restored away: \
{after}"
);
Ok(())
}
#[test]
fn a_toolchain_that_cannot_run_fails_fmt_rather_than_passing_it() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let (ok, report) = crate::land::fmt_ok(root, "definitely-not-a-cargo");
assert!(!ok, "an unrunnable toolchain is not a PASS");
assert!(report.contains("fmt: FAIL"), "{report}");
}
#[test]
fn the_ratchet_is_silent_when_clippy_cannot_run() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let (ok, report) =
crate::land::lint_debt_ok(root, "definitely-not-a-cargo");
assert!(ok, "no count means nothing to compare");
assert!(report.is_empty(), "{report}");
}
#[test]
fn the_gate_reads_the_recorded_lint_debt() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let was = crate::debt::recorded(root);
assert!(was.is_some(), "this repo records a debt total");
assert!(was.is_some_and(|n| n > 0), "and it is a real count");
}
#[test]
fn a_tree_with_no_lint_debt_file_does_not_fail_the_gate() {
let dir = std::env::temp_dir();
assert_eq!(
crate::debt::recorded(&dir.join("definitely-not-a-repo")),
None
);
}
#[test]
fn a_tree_with_no_rust_yields_no_density() {
let dir = std::env::temp_dir().join("sherd-no-rust-at-all");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).expect("mkdir");
std::fs::write(dir.join(".lint-debt"), "density 5.0\n").expect("write");
let (ok, r) = crate::land::lint_debt_ok(&dir, "false");
assert!(ok, "no denominator is not a refusal: {r}");
assert!(r.is_empty(), "and it says nothing: {r}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_loop_coordinates_and_each_step_is_its_own_function() {
let src = include_str!("mod.rs");
let (impl_r, _tests) = split_module(src);
for step in [
"fn red_test(",
"fn judge_test(",
"fn green_candidates(",
"fn repair(",
"fn blind_check(",
] {
assert!(impl_r.contains(step), "the loop lost {step}");
}
let drive = impl_r
.split_once("pub fn drive_run(")
.and_then(|(_, r)| r.split_once("\n}\n"))
.map(|(body, _)| body)
.unwrap_or_default();
for call in [
"red_test(&ctx",
"green_candidates(&ctx",
"repair(&ctx",
"blind_check(&ctx",
] {
assert!(
drive.contains(call),
"the coordinator stopped calling {call}"
);
}
assert!(
!drive.contains("NOTATION"),
"a prompt body belongs to its step, not to the coordinator"
);
assert!(
impl_r.contains("let mut guard = Restore::arm("),
"the coordinator arms the guard"
);
}
#[test]
fn the_contract_objection_names_the_function_the_row_asked_for() {
let o = contract_objection("post_with_retry");
assert!(o.contains("post_with_retry"), "{o}");
assert!(
o.contains("calls `post_with_retry` directly"),
"the objection has to be actionable, not just a complaint: {o}"
);
}
#[test]
fn a_green_candidate_carrying_findings_is_refused_not_repaired() {
let clean = Candidate {
code: "pub fn f() {}".into(),
green: true,
findings: 0,
};
let flawed = Candidate {
findings: 2,
..clean.clone()
};
let tried = |c: Candidate| Tried {
cand: c,
out: String::new(),
};
assert_eq!(pick_candidate(&[tried(clean.clone())]), Ok(0));
let refused = pick_candidate(&[tried(flawed)]);
assert!(
refused.is_err_and(|e| e.contains("reverted")),
"a green candidate with findings is reverted, not kept"
);
assert!(pick_candidate(&[]).is_err());
}
#[test]
fn the_green_prompt_carries_the_contract_and_the_failure() {
let t = Scripted::new(&[]);
let run = Run {
root: Path::new("."),
node: Path::new("."),
owner: Path::new("."),
invariant: "V1",
task: "add `existing`",
max_repair: 1,
cargo: "false".into(),
transport: &t,
};
let ctx = probe_ctx(&run, "pub fn existing(x: u64) -> bool");
let p = green_prompt(&ctx, "fn t() { assert!(existing(1)); }", "E0425");
assert!(p.contains("existing"), "the surface is shown: {p}");
assert!(p.contains("E0425"), "the failure is shown: {p}");
assert!(
p.contains("Do not modify the test"),
"step 2 may not rewrite the test it was given"
);
}
#[test]
fn the_repair_prompt_shows_what_was_last_added() {
let p = repair_prompt(
"pub fn existing()".into(),
"pub fn mine() -> u8 { 0 }",
"fn t() {}",
"assertion failed",
);
assert!(p.contains("pub fn mine() -> u8 { 0 }"), "{p}");
assert!(p.contains("assertion failed"), "{p}");
assert!(
p.contains("corrected version of the function(s) you previously"),
"repair replaces its own work, it does not add more"
);
}
fn probe_ctx<'a>(run: &'a Run<'a>, surface: &str) -> Ctx<'a> {
Ctx {
run,
mod_path: std::path::PathBuf::from("mod.rs"),
inv: "V1: a ! b".into(),
spec_rules: String::new(),
surface: surface.to_string(),
tests: String::new(),
in_scope: String::new(),
}
}
fn cargo_with_warnings(dir: &Path, n: usize) -> Result<String, String> {
let script = dir.join("noisy-cargo");
let mut emit = String::new();
for i in 0..n {
emit.push_str(&format!(
"echo 'src/x/mod.rs:{i}:1: warning: made up' >&2\n"
));
}
write_exec(&script, &format!("#!/bin/sh\n{emit}exit 0\n"))?;
Ok(script.display().to_string())
}
#[test]
fn the_ratchet_holds_on_density_and_lets_the_tree_grow_clean() {
assert_eq!(ratchet_both_ways(), Ok(()));
}
fn ratchet_both_ways() -> Result<(), String> {
let (dir, _n) = scratch("ratchet")?;
std::fs::create_dir_all(dir.join("src/deeper"))
.map_err(|e| format!("mkdir: {e}"))?;
std::fs::write(dir.join("src/x.rs"), "// line\n".repeat(60))
.map_err(|e| format!("write: {e}"))?;
std::fs::write(dir.join("src/deeper/y.rs"), "// line\n".repeat(40))
.map_err(|e| format!("write: {e}"))?;
std::fs::write(dir.join("src/notes.md"), "// line\n".repeat(500))
.map_err(|e| format!("write: {e}"))?;
std::fs::write(dir.join(".lint-debt"), "density 20.0\n")
.map_err(|e| format!("write: {e}"))?;
let (ok, r) =
crate::land::lint_debt_ok(&dir, &cargo_with_warnings(&dir, 2)?);
assert!(ok, "holding at the recorded density PASSES: {r}");
assert!(r.contains("PASS === 20.0 per KLoC (ceiling 20.0)"), "{r}");
let (rose, rr) =
crate::land::lint_debt_ok(&dir, &cargo_with_warnings(&dir, 3)?);
assert!(!rose, "one more warning on the same lines REFUSES: {rr}");
assert!(rr.contains("ROSE === 30.0 per KLoC (ceiling 20.0)"), "{rr}");
std::fs::write(dir.join("src/x.rs"), "// line\n".repeat(110))
.map_err(|e| format!("write: {e}"))?;
let (grew, gr) =
crate::land::lint_debt_ok(&dir, &cargo_with_warnings(&dir, 3)?);
assert!(grew, "same density over more lines PASSES: {gr}");
let _ = std::fs::remove_dir_all(&dir);
Ok(())
}
#[test]
fn fmt_and_the_ratchet_pass_against_a_scripted_toolchain() {
assert_eq!(scripted_gate_halves(), Ok(()));
}
fn scripted_gate_halves() -> Result<(), String> {
let (dir, _node) = scratch("gatehalves")?;
let cargo = scripted_cargo(&dir, 0)?;
let (fmt, fmt_r) = crate::land::fmt_ok(&dir, &cargo);
assert!(fmt, "a scripted toolchain formats clean: {fmt_r}");
assert!(fmt_r.contains("fmt: PASS"), "{fmt_r}");
let (debt, debt_r) = crate::land::lint_debt_ok(&dir, &cargo);
assert!(debt, "an absent ratchet does not fail the gate");
assert!(debt_r.is_empty(), "and says nothing: {debt_r}");
let _ = std::fs::remove_dir_all(&dir);
Ok(())
}
#[test]
fn a_gate_that_could_not_run_is_an_error_not_a_verdict() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let Err(msg) =
crate::land::gate_with(root, "definitely-not-a-cargo-binary")
else {
return;
};
assert!(
msg.contains("could not RUN"),
"say the gate did not EXECUTE, not that it failed: {msg}"
);
}
}