use crate::brain::agent::service::phantom::{asserted_facts, mixed_iteration_facts};
const INCIDENT_TEXT: &str = r#"Gate died on 2× E0603 "this item is private"."#;
const UNRELATED_OUTPUT: &str = "\
src/brain/agent/service/mod: 41 lines
Compiling opencrabs v0.5.3
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3m 44s
";
const REAL_E0603_OUTPUT: &str = "\
error[E0603]: module `service` is private
--> src/brain/agent/mod.rs:1:1
|
1 | mod service;
| ^^^^^^^^^^^^ private module
";
#[test]
fn a_rustc_diagnostic_code_is_extracted_as_an_asserted_fact() {
let facts = asserted_facts(INCIDENT_TEXT);
assert!(
facts.iter().any(|f| f == "E0603"),
"the diagnostic code was not extracted: {facts:?}"
);
}
#[test]
fn a_diagnostic_touching_a_longer_token_is_not_extracted() {
for text in [
"parent 1a2b3E0603ff is on main",
"see src/E0603.rs for the fixture",
"code E06031 is not a rustc id",
"offset 0xE0603 in the dump",
] {
let facts = asserted_facts(text);
assert!(
!facts.iter().any(|f| f == "E0603"),
"{text:?} extracted a diagnostic from a non-diagnostic token: {facts:?}"
);
}
}
#[test]
fn a_bare_and_a_bracketed_code_are_both_extracted() {
for text in [
"Gate died on E0603 again.",
"cargo clippy reported error[E0425] on the call.",
] {
let facts = asserted_facts(text);
assert!(
facts.iter().any(|f| f.starts_with('E') && f.len() == 5),
"{text:?} yielded no diagnostic: {facts:?}"
);
}
}
#[test]
fn the_incident_fires_on_a_mixed_iteration() {
let executed: Vec<String> = vec![
r#"{"command":"cargo fmt --all"}"#.to_string(),
r#"{"command":"arch -arm64 cargo clippy -p opencrabs --all-features"}"#.to_string(),
];
let outputs: Vec<String> = vec![UNRELATED_OUTPUT.to_string()];
let violation = mixed_iteration_facts(INCIDENT_TEXT, &executed, &outputs, UNRELATED_OUTPUT)
.expect("the incident fabrication was not caught on a mixed iteration");
assert!(
violation.branches.contains(&"mixed_unbacked_facts"),
"wrong branch fired: {:?}",
violation.branches
);
assert!(
violation.unbacked_facts.iter().any(|f| f == "E0603"),
"the invented code is not named for the nudge: {:?}",
violation.unbacked_facts
);
}
#[test]
fn the_same_claim_backed_by_real_output_does_not_fire() {
let executed: Vec<String> = vec![r#"{"command":"cargo clippy"}"#.to_string()];
let outputs: Vec<String> = vec![REAL_E0603_OUTPUT.to_string()];
let evidence = format!("{UNRELATED_OUTPUT}{REAL_E0603_OUTPUT}");
assert!(
mixed_iteration_facts(INCIDENT_TEXT, &executed, &outputs, &evidence).is_none(),
"a diagnostic that really was printed must never be called a fabrication"
);
}
#[test]
fn a_recap_of_the_work_its_own_call_did_is_not_flagged() {
let in_flight = r#"{"command":"gh pr list -R opencrabs/opencrabs --state open"}"#;
let text = "Ran `gh pr list -R opencrabs/opencrabs --state open` for the current set.";
assert!(
mixed_iteration_facts(text, &[in_flight.to_string()], &[], "").is_none(),
"the in-flight call must vouch for its own command"
);
}
#[test]
fn the_same_recap_without_the_call_in_input_is_flagged() {
let text = "Ran `gh pr list -R opencrabs/opencrabs --state open` for the current set.";
let prior = r#"{"command":"git status"}"#;
let violation = mixed_iteration_facts(text, &[prior.to_string()], &[], "")
.expect("uncalled command missed");
assert!(
violation.branches.contains(&"mixed_uncalled_commands"),
"wrong branch fired: {:?}",
violation.branches
);
}
#[test]
fn empty_text_yields_no_violation() {
assert!(mixed_iteration_facts(" ", &[], &[], "").is_none());
}
fn zero_tool_block(src: &str) -> (usize, usize) {
let start = src
.find("if tool_uses.is_empty() {")
.expect("the zero-tool gate is gone — this test needs re-pointing");
let open = start + src[start..].find('{').expect("no opening brace");
let mut depth = 0usize;
for (i, ch) in src[open..].char_indices() {
match ch {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
return (start, open + i);
}
}
_ => {}
}
}
panic!("unterminated zero-tool block");
}
#[test]
fn the_mixed_check_runs_outside_the_zero_tool_block() {
let src = include_str!("../brain/agent/service/tool_loop.rs");
let (start, end) = zero_tool_block(src);
let call = src
.find("super::phantom::mixed_iteration_facts(")
.expect("the mixed-iteration check is not wired into the loop");
assert!(
call > end,
"the mixed check sits inside the zero-tool block ({start}..{end}), byte {call}"
);
let guard_window = &src[call.saturating_sub(900)..call];
assert!(
guard_window.contains("tool_uses.is_empty()"),
"the mixed check does not exclude zero-tool iterations"
);
}
#[test]
fn the_shape_tells_stay_inside_the_zero_tool_block() {
let src = include_str!("../brain/agent/service/tool_loop.rs");
let (start, end) = zero_tool_block(src);
for tell in [
"matches_work_announcement(",
"matches_now_gerund(",
"matches_plan_announcement(",
] {
if let Some(at) = src.find(tell) {
assert!(
at > start && at < end,
"{tell} was moved outside the zero-tool block (span {start}..{end})"
);
}
}
}
#[test]
fn the_correction_lands_after_the_tool_results() {
let src = include_str!("../brain/agent/service/tool_loop.rs");
let results = src
.find("context.add_message(tool_result_msg);")
.expect("tool result persistence not found");
let correction = src
.find("if let Some(violation) = mixed_fact_verdict")
.expect("the mixed-iteration correction is not injected");
assert!(
correction > results,
"the correction is injected before the tool results reach the context"
);
}
#[test]
fn the_correction_honours_the_structured_report_carve_out() {
let src = include_str!("../brain/agent/service/tool_loop.rs");
let (_, rest) = src
.split_once("if let Some(violation) = mixed_fact_verdict")
.expect("correction block not found");
let (block, _) = rest
.split_once("context.add_message(Message::user(nudge));")
.expect("correction block never injects its nudge");
assert!(
block.contains("violation.structured_report"),
"the correction ignores the #1506 structured-report carve-out"
);
assert!(
block.contains("is_structured_report") || block.contains("structured_report"),
"no structured-report guard in the correction block"
);
}