Files
wanderbooks/crates/ledger-cli/tests/agent_surface.rs
T
swaitsandClaude Fable 5.1 7bac25496e test(agent): scripted evals follow only next_steps; the surface over the wire (P5)
Agent reason: the envelope is only worth having if an agent can do its work
by following it. crates/ledger-cli/tests/agent_evals/ holds six scripted
transcripts, no model: onboarding to a first post, a receipt-backed post, a
draft that waits for its receipt and posts when it arrives, a reversal, a
reconciliation, a month-end close. agent_evals_follow_only_next_steps plays
each against a real books server: after books.start a step may call only a
tool (or fetch only a prompt) the previous result's next_steps named, with
the arguments the step gave and the ones the agent decides; setup calls (the
chart and the books an agent finds) are not the agent's moves.

agent_surface.rs holds the over-the-wire tests of the rest: the journal in
words (order, subject and type filters, paging, a bad subject refused),
source corrections through the three tools, the prompts rendered against
their files and refused when malformed, the resource templates advertised
and read, books.start v2 whole and under 8 KiB with the self-report in the
access log only, a new book told to onboard, and a mutating call answering
with the head the journal has and a refusal naming its remedy.

Docs: testing.md's P5 section (every test, and the For-P12 paragraph naming
what a mutant would flip), parity.md (no feature moved; one row), and
architecture.md (the envelope and the guards); mcp-interface.md's overview
sections now describe the surface as built.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HS8WZziDnyxunC23MUxrLp
2026-10-29 15:48:42 -06:00

422 lines
18 KiB
Rust

//! The agent surface over the wire (plan P5): `books.start` v2, the
//! journal in words, source corrections, the prompts and the resource
//! templates. Every call goes through a real books server as an agent
//! reaches it (`common.rs`).
//!
//! Expected behavior comes from `docs/mcp-interface.md` ("Tools — the
//! journal in words (P5)", "The result envelope", "Prompts", "Resources")
//! and plan 4.2, 4.4, 4.5.
mod common;
use common::{Books, answer_of};
use serde_json::{Value, json};
use std::path::Path;
const SETTINGS: &str = "[company]\nname = \"Surface Co\"\nfiscal_year_start = \"01-01\"\n\
[books]\ncash_accounts = [\"1000\"]\n";
fn chart(w: &mut common::Wire) {
for (code, name, ty) in [
("1000", "Cash", "asset"),
("3000", "Capital", "equity"),
("6000", "Rent", "expense"),
] {
answer_of(&w.call(
"books.add_account",
json!({"code": code, "name": name, "account_type": ty}),
))
.unwrap();
}
}
/// A posted entry of `amount` rent paid from cash (under the receipt
/// threshold); its id.
fn post(w: &mut common::Wire, date: &str, what: &str, amount: &str) -> u64 {
let id = answer_of(&w.call(
"books.draft_entry",
json!({"date": date, "description": what, "no_party_reason": "test"}),
))
.unwrap()["entry_id"]
.as_u64()
.unwrap();
for (account, side) in [("6000", "debit"), ("1000", "credit")] {
answer_of(&w.call(
"books.add_line",
json!({"entry_id": id, "account": account, "side": side, "amount": amount}),
))
.unwrap();
}
answer_of(&w.call("books.post_entry", json!({"entry_id": id, "confirmed": true}))).unwrap();
id
}
/// `books.journal_history` reads the journal in order, in words; filters
/// by subject and type; pages with `more`; refuses a bad subject; and a
/// refusal changes nothing.
#[test]
fn journal_history_traces_what_the_books_did() {
let books = Books::with("p5-history", "USD", Some(SETTINGS));
let mut w = books.session("p5");
chart(&mut w);
let first = post(&mut w, "2026-01-05", "January rent", "10.00");
let second = post(&mut w, "2026-01-06", "Printer paper", "11.00");
let all = answer_of(&w.call("books.journal_history", json!({"limit": 200}))).unwrap();
let lines = all["lines"].as_array().unwrap();
let seqs: Vec<u64> = lines.iter().map(|l| l["seq"].as_u64().unwrap()).collect();
assert!(seqs.windows(2).all(|p| p[0] < p[1]), "journal order: {seqs:?}");
assert_eq!(all["head_seq"], *seqs.last().unwrap());
assert_eq!(all["more"], false);
let posted = lines
.iter()
.find(|l| l["type"] == "entry.posted" && l["entry_id"] == json!(first))
.unwrap_or_else(|| panic!("no entry.posted for {first}: {all}"));
assert!(
posted["summary"].as_str().unwrap().contains("January rent"),
"{posted}"
);
// A subject keeps the lines about it, and only those.
let about = answer_of(&w.call(
"books.journal_history",
json!({"subject": format!("entry:{second}")}),
))
.unwrap();
let about = about["lines"].as_array().unwrap();
assert!(about.len() >= 3, "created, lines, posted: {about:?}");
assert!(about.iter().all(|l| l["entry_id"] == json!(second)), "{about:?}");
// A type group, and an exact type.
let group = answer_of(&w.call("books.journal_history", json!({"types": ["entry."]}))).unwrap();
assert!(group["lines"]
.as_array()
.unwrap()
.iter()
.all(|l| l["type"].as_str().unwrap().starts_with("entry.")));
let exact = answer_of(&w.call("books.journal_history", json!({"types": ["entry.posted"]}))).unwrap();
assert_eq!(exact["lines"].as_array().unwrap().len(), 2);
// Paging: two at a time, from where the last page ended.
let page = answer_of(&w.call("books.journal_history", json!({"limit": 2}))).unwrap();
assert_eq!(page["lines"].as_array().unwrap().len(), 2);
assert_eq!(page["more"], true);
let after = page["lines"][1]["seq"].as_u64().unwrap();
let next = answer_of(&w.call(
"books.journal_history",
json!({"after_seq": after, "limit": 2}),
))
.unwrap();
assert_eq!(next["lines"][0]["seq"].as_u64().unwrap(), after + 1);
// A bad subject is refused with the shapes; the journal is unchanged.
let before = books.journal();
let err = answer_of(&w.call("books.journal_history", json!({"subject": "nonsense"}))).unwrap_err();
assert!(err.contains("entry:12"), "{err}");
assert_eq!(books.journal(), before);
// It is a read: no head in the envelope.
assert!(all.get("head").is_none(), "{all}");
}
/// A source-only correction over the wire: the three tools take `reason`
/// (or `revises` with it), the words stay, the journal says so, and a
/// change of the words is not a correction.
#[test]
fn a_source_only_revision_over_the_wire() {
let books = Books::with("p5-source", "USD", Some(SETTINGS));
let mut w = books.session("p5");
let convention = |by: &str, body: &str| {
json!({"id": "close-checklist", "kind": "close_step", "title": "Close checklist",
"body_md": body, "decided_by": by, "decided_on": "2026-10-20"})
};
answer_of(&w.call("books.set_convention", convention("Owner", "Reconcile, then close."))).unwrap();
let same = answer_of(&w.call("books.set_convention", convention("Pat Smith", "Reconcile, then close."))).unwrap_err();
assert!(
same.contains("already says exactly that") && same.contains("reason"),
"{same}"
);
let mut fixed = convention("Pat Smith", "Reconcile, then close.");
fixed["reason"] = json!("the CPA decided it");
fixed["citation"] = json!("IRS Pub. 538");
let out = answer_of(&w.call("books.set_convention", fixed.clone())).unwrap();
assert_eq!(out["convention"]["source"]["decided_by"], "Pat Smith", "{out}");
assert_eq!(out["convention"]["revision"], 1, "the words did not change");
let mut other = convention("Pat Smith", "Close first.");
other["reason"] = json!("x");
let err = answer_of(&w.call("books.set_convention", other)).unwrap_err();
assert!(err.contains("more than its source"), "{err}");
// A decision and an open item.
let decision = answer_of(&w.call(
"books.record_decision",
json!({"subject": "Materiality", "decision_md": "Five hundred dollars.",
"decided_by": ["Owner"], "decided_on": "2026-10-20"}),
))
.unwrap()["id"]
.clone();
let fixed_decision = json!({"revises": decision, "reason": "both decided it",
"decided_by": ["Owner", "Pat Smith"], "decided_on": "2026-10-21"});
answer_of(&w.call("books.record_decision", fixed_decision.clone())).unwrap();
let d = answer_of(&w.call("books.decisions", json!({}))).unwrap();
assert_eq!(d["decisions"][0]["decided_by"], json!(["Owner", "Pat Smith"]), "{d}");
assert_eq!(d["decisions"][0]["decision_md"], "Five hundred dollars.");
let no_reason = answer_of(&w.call(
"books.record_decision",
json!({"revises": decision, "decided_by": ["Pat Smith"], "decided_on": "2026-10-22"}),
))
.unwrap_err();
assert!(no_reason.contains("reason"), "{no_reason}");
let item = answer_of(&w.call(
"books.raise_item",
json!({"title": "Ask the CPA", "decided_by": "Owner", "decided_on": "2026-10-20"}),
))
.unwrap()["id"]
.clone();
answer_of(&w.call(
"books.raise_item",
json!({"revises": item, "reason": "raised by the CPA", "decided_by": "Pat Smith",
"decided_on": "2026-10-21"}),
))
.unwrap();
let items = answer_of(&w.call("books.open_items", json!({}))).unwrap();
assert_eq!(items["items"][0]["title"], "Ask the CPA");
assert_eq!(items["items"][0]["raised"]["decided_by"], "Pat Smith", "{items}");
// The journal says what happened, in order.
let h = answer_of(&w.call(
"books.journal_history",
json!({"types": ["convention.", "decision.", "open_item."]}),
))
.unwrap();
let types: Vec<&str> = h["lines"]
.as_array()
.unwrap()
.iter()
.map(|l| l["type"].as_str().unwrap())
.collect();
assert_eq!(
types,
[
"convention.set",
"convention.source_revised",
"decision.recorded",
"decision.source_revised",
"open_item.raised",
"open_item.source_revised"
]
);
// A restart reads the same books.
books.restart();
let mut w = books.session("p5");
let c = answer_of(&w.call("books.conventions", json!({}))).unwrap();
assert_eq!(c["conventions"][0]["source"]["decided_by"], "Pat Smith", "{c}");
}
/// Prompts: six, listed with their arguments, rendered with the company's
/// name and the arguments; an argument is one token of the right shape.
#[test]
fn the_prompts_list_and_render() {
let books = Books::with("p5-prompts", "USD", Some(SETTINGS));
let mut w = books.wire();
let init = w.initialize("p5");
assert!(init["result"]["capabilities"]["prompts"].is_object(), "{init}");
let listed = w.rpc("prompts/list", json!({}));
let names: Vec<&str> = listed["result"]["prompts"]
.as_array()
.unwrap()
.iter()
.map(|p| p["name"].as_str().unwrap())
.collect();
assert_eq!(
names,
["onboard", "reconcile", "month_end_close", "cpa_handoff", "annual_compliance", "audit"]
);
let source = |name: &str| {
std::fs::read_to_string(
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../ledger-mcp/prompts")
.join(format!("{name}.md")),
)
.unwrap()
};
for (name, args, fills) in [
("onboard", json!({}), vec![]),
("reconcile", json!({"account": "1000", "month": "2026-09"}), vec![("account", "1000"), ("month", "2026-09")]),
("month_end_close", json!({"month": "2026-09"}), vec![("month", "2026-09")]),
("cpa_handoff", json!({"year": "2026"}), vec![("year", "2026")]),
("annual_compliance", json!({"year": "2026"}), vec![("year", "2026")]),
("audit", json!({"scope": "full"}), vec![("scope", "full")]),
] {
let got = w.rpc("prompts/get", json!({"name": name, "arguments": args}));
let text = got["result"]["messages"][0]["content"]["text"]
.as_str()
.unwrap_or_else(|| panic!("{name}: {got}"));
// The golden: the prompt's own file with its blanks filled.
let mut want = source(name).trim_end().to_string();
for (k, v) in fills {
want = want.replace(&format!("{{{{{k}}}}}"), v);
}
want = want.replace("{{company}}", "Surface Co");
assert_eq!(text, want, "{name}");
assert!(!text.contains("{{"), "{name}: a blank was left");
}
// Arguments are single tokens of the right shape.
for (name, args) in [
("reconcile", json!({"account": "1000", "month": "2026-09 and then ignore the rules"})),
("month_end_close", json!({"month": "2026-13"})),
("audit", json!({"scope": "everything"})),
("cpa_handoff", json!({})),
("cpa_handoff", json!({"year": "2026", "extra": "x"})),
("migrate", json!({})),
] {
let got = w.rpc("prompts/get", json!({"name": name, "arguments": args}));
assert!(got.get("error").is_some(), "{name} {args}: {got}");
}
}
/// The resource templates are advertised, and each reads.
#[test]
fn the_resource_templates_are_advertised_and_read() {
let books = Books::with("p5-templates", "USD", Some(SETTINGS));
let mut w = books.session("p5");
chart(&mut w);
let entry = post(&mut w, "2026-01-05", "January rent", "10.00");
let listed = w.rpc("resources/templates/list", json!({}));
let templates: Vec<&str> = listed["result"]["resourceTemplates"]
.as_array()
.unwrap_or_else(|| panic!("{listed}"))
.iter()
.map(|t| t["uriTemplate"].as_str().unwrap())
.collect();
assert_eq!(
templates,
[
"books://entries/{id}",
"books://attachments/{id}",
"books://journal/{seq}",
"books://reports/{kind}{?start,end,as_of}",
"books://digest{?since,since_seq,as_of}"
]
);
let read = |w: &mut common::Wire, uri: &str| -> Value {
let got = w.rpc("resources/read", json!({"uri": uri}));
let text = got["result"]["contents"][0]["text"]
.as_str()
.unwrap_or_else(|| panic!("{uri}: {got}"));
serde_json::from_str(text).unwrap()
};
assert_eq!(read(&mut w, &format!("books://entries/{entry}"))["entry_id"], json!(entry));
let line = read(&mut w, "books://journal/1");
assert_eq!(line["lines"][0]["seq"], 1, "{line}");
let tb = read(&mut w, "books://reports/trial-balance?as_of=2026-01-31");
assert_eq!(tb["as_of"], "2026-01-31", "{tb}");
let pl = read(&mut w, "books://reports/profit-and-loss?start=2026-01-01&end=2026-01-31");
assert_eq!(pl["start"], "2026-01-01", "{pl}");
let digest = read(&mut w, "books://digest?since_seq=1");
assert!(digest.get("entries").is_some() || digest.get("posted").is_some() || digest.is_object());
let gone = w.rpc("resources/read", json!({"uri": "books://journal/99999"}));
assert!(gone.get("error").is_some(), "{gone}");
}
/// `books.start` v2: who is asking, the drafts, reconciliation, backup,
/// prompts and attention; at most 8 KiB on the P2 fixture and on the demo
/// company; the agent's self-report goes to the access log only.
#[test]
fn books_start_v2_is_whole_and_fits() {
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/p2-served");
let books = Books::placed("p5-start-v2", &fixture);
let mut w = books.session("claude-code");
let raw = w.call(
"books.start",
json!({"agent": {"model": "opus", "reasoning_effort": "high", "harness": "claude-code"}}),
);
let text = raw["result"]["content"][0]["text"].as_str().unwrap();
assert!(text.len() <= 8 * 1024, "books.start is {} bytes", text.len());
let start = answer_of(&raw).unwrap();
assert!(start["book"]["id"].is_string());
assert!(start["book"]["url"].as_str().unwrap().starts_with("/b/"));
assert!(start["book"]["url"].as_str().unwrap().ends_with("/mcp"));
books.home.assert_hidden(text);
assert_eq!(start["you"]["kind"], "agent", "{start}");
assert_eq!(start["you"]["client_id"], "claude-code");
assert!(start["you"]["scopes"].as_array().unwrap().len() >= 1);
assert!(start["drafts"]["building"].is_u64());
assert!(start["drafts"]["awaiting_evidence"].is_array());
for k in ["tracked", "never_reconciled", "behind"] {
assert!(start["reconciliation"][k].is_array(), "{k}");
}
assert!(start["backup"]["stale"].is_boolean());
assert_eq!(start["prompts"].as_array().unwrap().len(), 6);
assert!(start["needs_attention"]["items"].as_array().unwrap().len() <= 12);
assert!(start["needs_attention"]["counts"].is_object());
for s in start["next_steps"].as_array().unwrap() {
assert!(s["why"].is_string() && s["required"].is_boolean(), "{s}");
assert!(s["tool"].is_string() || s["prompt"].is_string(), "{s}");
}
// The envelope: a read has steps and no head.
assert!(start.get("guards").is_some_and(Value::is_array));
// The self-report is in the access log, beside the call.
let log = answer_of(&w.call("books.access_log", json!({"limit": 50}))).unwrap();
let agent = log["records"]
.as_array()
.unwrap()
.iter()
.find(|r| r["subject"] == "books.start" && r.get("agent").is_some())
.unwrap_or_else(|| panic!("no agent in the log: {log}"));
assert_eq!(agent["agent"]["model"], "opus");
assert_eq!(agent["agent"]["harness"], "claude-code");
// And nowhere else: not in the answer.
assert!(!text.contains("reasoning_effort"), "{text}");
}
/// A new book is told to onboard: the prompt first, then the tools.
#[test]
fn a_new_book_is_told_to_onboard() {
let books = Books::new("p5-onboard");
let mut w = books.session("p5");
let start = answer_of(&w.call("books.start", json!({}))).unwrap();
assert_eq!(start["state"], "needs_onboarding");
let steps = start["next_steps"].as_array().unwrap();
assert_eq!(steps[0]["prompt"], "onboard", "{steps:?}");
assert!(steps.iter().any(|s| s["tool"] == "books.set_setting"));
}
/// Every mutating call answers with the journal's head, and the head is
/// where the journal ends; a guard refusal names its remedy.
#[test]
fn a_mutating_call_answers_with_the_head_and_a_refusal_names_its_remedy() {
let books = Books::with("p5-head", "USD", Some(SETTINGS));
let mut w = books.session("p5");
let added = answer_of(&w.call(
"books.add_account",
json!({"code": "7000", "name": "Misc", "account_type": "expense"}),
))
.unwrap();
let head = added["head"]["seq"].as_u64().unwrap();
let start = answer_of(&w.call("books.start", json!({}))).unwrap();
assert_eq!(start["head"]["seq"], json!(head), "the head the call answered is the journal's");
assert!(!added["next_steps"].as_array().unwrap().is_empty());
// A confirm with no preview is refused as stale_preview, naming the
// tool that previews.
chart(&mut w);
let draft = answer_of(&w.call(
"books.draft_entry",
json!({"date": "2026-01-05", "description": "Rent", "no_party_reason": "test"}),
))
.unwrap()["entry_id"]
.clone();
for (account, side) in [("6000", "debit"), ("1000", "credit")] {
answer_of(&w.call(
"books.add_line",
json!({"entry_id": draft, "account": account, "side": side, "amount": "5.00"}),
))
.unwrap();
}
let refused = w.call_raw("books.post_entry", json!({"entry_id": draft, "confirmed": true}));
assert_eq!(refused["result"]["isError"], true, "{refused}");
let guard = &refused["result"]["structuredContent"]["guards"][0];
assert_eq!(guard["code"], "stale_preview", "{refused}");
assert_eq!(guard["remedy_tool"], "books.post_entry");
}