feat(mcp): every write call is one transaction with a request key (P15 T1, D-88, D-89)

Accounting reasoning: these are financial books reached over the internet,
where a request is retried. A write whose answer is lost and then sent again
must not post twice, a call half done must not be left looking done, and a
failed write must be on the record.

Every tool that can change the books (83 of the 140: three that only read
what is sealed and may answer with a TIN are reads) takes a required
`request_key`, added to its schema in tools/list and taken out of the
arguments before the tool runs, so no tool, preview digest or check changes.
A write without a usable key is refused in words. Write calls take turns,
and each runs as one transaction of the store: opened (or answered from the
books if its key committed: the same tool and arguments return the first
answer and write nothing; other arguments are refused as a misuse of the
key; the access log says `repeat`), then closed with its answer if it
answered ok, or rolled back and closed `failed` if it errored, failed or was
stopped by a close. Books an older version wrote take no write at all until
the owner's go-ahead (D-89): every write is refused with the upgrade remedy;
reads work as ever. `books.import_bank_csv` is one transaction like the rest.

The handbook, docs/mcp-interface.md, the claims (WB-format-8 to 12, and
WB-format-3 amended for D-89) and docs/parity.md say so. Tests that wrote to
old books through the tools now assert the refusal and write after the
upgrade; the shared wire helpers add a fresh key to every tool call (the
journal helpers leave the call frames out, as they left out nothing before),
so no other test changed. The store-level `_waits_for_<feature>` tests still
check each feature's own gate. No figure and no golden moved.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 09:43:52 -07:00
1 parent 2633109d55
commit e542319e63
25 files changed
+1084 -186

No files matched your search

+290
View File
@@ -0,0 +1,290 @@
//! Every write a tool makes is one transaction that happens once (plan
//! P15 2a, D-88, D-89): the request key, the repeat, and the frame around
//! a call. The journal's side is `ledger_store::calls`.
//!
//! ```text
//! GOAL: a write whose answer is lost and which is then retried happens
//! once; a call half done is never left looking done; a failed
//! write is on the record.
//! SOURCE: WB-format-8, WB-format-9, WB-format-10, WB-format-11;
//! plan/p15-app-and-relay.md 2a.
//! INVARIANTS:
//! 1. Every tool that can change anything (no readOnlyHint, but for
//! READS_THAT_SEAL, which write nothing and may answer with a TIN) takes a
//! required `request_key` in its schema; a call that writes without
//! one is refused in words, before it runs. A preview (a confirmed
//! tool called without confirmed: true) writes nothing and needs
//! none. The key is taken out of the arguments before the tool sees
//! them, so a tool's own checks and previews are unchanged.
//! 2. A write call runs alone among writes: they take turns, so a
//! call's lines are one run in the journal, and a repeat that
//! arrives while its original still runs waits for it, then gets its
//! answer.
//! 3. A repeat of a (token, key) whose call committed, with the same
//! tool and arguments, returns that call's answer as it was given and
//! writes nothing; with other arguments (or another tool) it is
//! refused as a misuse of the key. The access log records a repeat
//! as `repeat`.
//! 4. A call that answered ok commits with a closing line holding its
//! answer; one that answered with an error, was stopped by a close or
//! failed by any other means rolls back, is journaled as `failed`
//! when it had written lines, and leaves its key unused.
//! 5. Books that haven't declared call_transactions take no write: every
//! write call is refused with the upgrade remedy, before it runs.
//! Reads are never refused for it.
//! 6. No full TIN is kept in the journal (BS-partner-2): no answer holds
//! one (READS_THAT_SEAL, the three that can, are no transaction), and
//! an error text that holds one is kept masked.
//! SCENARIOS:
//! 1. IF a write is called again with the same key after its answer was
//! lost: SHOULD return the first answer, and the books hold one of
//! its effects.
//! 2. IF the same key comes with a different request: SHOULD refuse,
//! saying so and that nothing was changed.
//! 3. IF the process dies between the call's lines: SHOULD leave none
//! of them in the books (ledger_store::calls).
//! ```
use ledger_store::calls::{CallEnd, CallId, MAX_KEY_LEN, MIN_KEY_LEN};
use rmcp::model::{CallToolResponse, CallToolResult};
use serde_json::{Map, Value, json};
use super::BooksServer;
use crate::scopes::{Needs, TABLE};
/// The argument every write call carries.
pub const KEY_FIELD: &str = "request_key";
/// Why a write was refused before it ran, for its access record.
pub(crate) const MISSING_KEY: &str = "request_key_missing";
/// The key was used for another request.
pub(crate) const KEY_REUSED: &str = "request_key_reused";
/// The books are waiting for the owner's format upgrade (D-89).
pub(crate) const UPGRADE_PENDING: &str = "format_upgrade_pending";
/// The call repeated one that committed: its answer was returned.
pub(crate) const REPEAT: &str = "repeat";
/// The words that tell a model what the key is, whenever it is asked for.
const KEY_WORDS: &str = "request_key: any text you make up, 8 to 128 characters (a UUID is fine), new for each different action and never reused for another. If a call's answer is lost, send the same call again with the same key: the books will not do it twice, and will answer what they answered the first time";
/// The tools that are not read-only by their annotation but write nothing
/// to the books: they read what is sealed or put it into a file, and the
/// answer of each may hold a full TIN, which no journal line may (BS-partner-2).
/// They take no request key and are no transaction; they are read as any
/// read is (INVARIANT 1).
pub(crate) const READS_THAT_SEAL: [&str; 3] = [
"books.vendor_1099",
"books.get_w9",
"books.export_cpa_package",
];
/// The tools whose calls can write: no readOnlyHint (`next_steps::
/// mutating_tools`), except [`READS_THAT_SEAL`] (83 of the 86).
pub(crate) fn writes(tool: &str) -> bool {
super::next_steps::mutating_tools().contains(tool) && !READS_THAT_SEAL.contains(&tool)
}
/// Whether a call to `tool` with `args` writes: a write tool, except a
/// confirmed tool called as a preview (INVARIANT 1).
pub(crate) fn is_write_call(tool: &str, args: Option<&Map<String, Value>>) -> bool {
if !writes(tool) {
return false;
}
let previews = TABLE
.iter()
.any(|(n, needs)| *n == tool && *needs == Needs::PostWhenConfirmed);
!(previews
&& !args
.and_then(|a| a.get("confirmed"))
.and_then(Value::as_bool)
.unwrap_or(false))
}
/// Take the request key out of the arguments (INVARIANT 1).
pub(crate) fn take_key(args: &mut Option<Map<String, Value>>) -> Option<Value> {
args.as_mut().and_then(|a| a.remove(KEY_FIELD))
}
/// What is wrong with a call's key, in words; None when it will do.
pub(crate) fn key_problem(key: Option<&Value>) -> Option<String> {
match key {
None | Some(Value::Null) => Some(format!(
"this call changes the books and carries no request_key, so it was not run and nothing was changed. Send it again with a {KEY_WORDS}."
)),
Some(Value::String(k)) => {
let n = k.chars().count();
if k != k.trim() || k.chars().any(char::is_control) {
Some("the request_key has spaces at its ends or control characters in it, so the call was not run and nothing was changed. Send it again with a key of plain text".into())
} else if !(MIN_KEY_LEN..=MAX_KEY_LEN).contains(&n) {
Some(format!(
"the request_key is {n} characters; it must be {MIN_KEY_LEN} to {MAX_KEY_LEN}, so the call was not run and nothing was changed. Send it again with a {KEY_WORDS}."
))
} else {
None
}
}
Some(_) => Some(format!(
"the request_key must be text, so the call was not run and nothing was changed. Send it again with a {KEY_WORDS}."
)),
}
}
/// The key as text, once it has passed [`key_problem`].
pub(crate) fn key_text(key: &Value) -> String {
key.as_str().unwrap_or_default().to_string()
}
/// The tool list's schema for a write tool: `request_key` is a required
/// property (INVARIANT 1). A read-only tool's is left as it is.
pub(crate) fn add_key_to_schema(tool: &mut rmcp::model::Tool) {
if !writes(&tool.name) {
return;
}
let mut schema: Map<String, Value> = (*tool.input_schema).clone();
let props = schema
.entry("properties")
.or_insert_with(|| Value::Object(Map::new()));
if let Value::Object(props) = props {
props.insert(
KEY_FIELD.into(),
json!({
"type": "string",
"minLength": MIN_KEY_LEN,
"maxLength": MAX_KEY_LEN,
"description": KEY_WORDS,
}),
);
}
let required = schema
.entry("required")
.or_insert_with(|| Value::Array(Vec::new()));
if let Value::Array(required) = required
&& !required.iter().any(|r| r == KEY_FIELD)
{
required.push(json!(KEY_FIELD));
}
tool.input_schema = std::sync::Arc::new(schema);
}
/// What opening a call came to.
pub(crate) enum Begun {
/// The call is open: run the tool.
Run,
/// Not run: this is the answer, and the access record's code.
Answer(Box<CallToolResponse>, &'static str),
}
fn refusal(text: String) -> CallToolResponse {
CallToolResponse::Complete(CallToolResult::error(vec![
rmcp::model::ContentBlock::text(text),
]))
}
/// Open a call (INVARIANTS 3, 5): refuse it if the books take no calls;
/// answer it from the books if its key already committed; otherwise open
/// it. Takes the store's lock for the moment it needs; the caller holds
/// the write turn (`SharedStore::write_turn`).
pub(crate) fn begin(server: &BooksServer, id: CallId) -> Result<Begun, String> {
let mut store = server.lock()?;
if let Err(e) = store.ensure_takes_calls() {
return Ok(Begun::Answer(
Box::new(refusal(e.to_string())),
UPGRADE_PENDING,
));
}
if let Some(kept) = store.recorded_call(&id.token, &id.key) {
if kept.tool == id.tool && kept.args == id.args {
return match serde_json::from_str::<CallToolResult>(&kept.answer) {
Ok(answer) => Ok(Begun::Answer(
Box::new(CallToolResponse::Complete(answer)),
REPEAT,
)),
Err(e) => Err(format!(
"the answer kept for this request_key can't be read back: {e}"
)),
};
}
return Ok(Begun::Answer(
Box::new(refusal(format!(
"this request_key was already used for {} with other arguments, so the call was not run and nothing was changed. A key names one action: send this one again with a key of its own",
if kept.tool == id.tool {
"the same tool".to_string()
} else {
kept.tool.clone()
}
))),
KEY_REUSED,
));
}
store.begin_call(id).map_err(|e| e.to_string())?;
Ok(Begun::Run)
}
/// The text of the first text block, or "".
fn first_text(r: &CallToolResult) -> &str {
r.content
.iter()
.find_map(|c| match c {
rmcp::model::ContentBlock::Text(t) => Some(t.text.as_str()),
_ => None,
})
.unwrap_or("")
}
/// Whether a tool's final answer is one that committed: a result that is
/// not an error, and the call not stopped by a close (INVARIANT 4).
pub(crate) fn answered_ok(
out: &Result<CallToolResponse, rmcp::ErrorData>,
stopped_by_close: bool,
) -> bool {
!stopped_by_close
&& matches!(out, Ok(CallToolResponse::Complete(r)) if r.is_error != Some(true))
}
/// Close the open call with how it came out (INVARIANTS 4, 6). An error
/// here means the call could not be closed: the books server stops, and
/// the caller is told so.
pub(crate) fn close(
server: &BooksServer,
out: &Result<CallToolResponse, rmcp::ErrorData>,
ok: bool,
) -> Result<(), String> {
use ledger_core::parties::{has_full_tin, redact_full_tins};
let mut store = server.lock()?;
if !store.in_call() {
return Ok(());
}
let masked = |text: String| {
if has_full_tin(&text) {
redact_full_tins(&text).into_owned()
} else {
text
}
};
let ended = if ok {
let Ok(CallToolResponse::Complete(r)) = out else {
return Err("a call that committed has no answer".into());
};
// Kept as given: no tool's answer holds a full TIN (the three that
// can are no transaction), and a digest of the books' head can hold
// nine digits in a row, which masking would spoil on a repeat.
let text = serde_json::to_string(r).map_err(|e| e.to_string())?;
store.close_call(CallEnd::Done(&text))
} else {
let why = match out {
Ok(CallToolResponse::Complete(r)) => first_text(r).to_string(),
Ok(_) => "the call did not complete".to_string(),
Err(e) => e.message.to_string(),
};
let why = if why.is_empty() {
"the call failed".to_string()
} else {
why
};
store.close_call(CallEnd::Failed(&masked(why)))
};
ended.map_err(|e| e.to_string())
}
/// What a call is told when its closing line could not be written.
pub(crate) const NOT_CLOSED: &str = "this call could not be completed: its closing line could not be written, so none of it counts, and the books server stopped taking requests to protect the books. Ask the human to restart it, then send the call again with the same request_key";
+5 -1
View File
@@ -4769,8 +4769,12 @@ mod tests {
assert_eq!(added.len(), 1, "one line");
assert_eq!(added[0]["type"], "journal.format_set");
let c = ledger_store::format::current();
// The declaration also gives older books their id (plan P15 6a).
let mut declared = added[0]["payload"].clone();
let books_id = declared.as_object_mut().unwrap().remove("books_id");
assert_eq!(books_id.and_then(|v| v.as_str().map(str::len)), Some(32));
assert_eq!(
added[0]["payload"],
declared,
serde_json::json!({"format": c.format, "features": c.features})
);
assert_eq!(done.head.0, backup.head.0 + 1);
+98 -4
View File
@@ -222,6 +222,11 @@ pub struct SharedStore {
running: std::sync::atomic::AtomicUsize,
/// Woken whenever one ends.
ended: tokio::sync::Notify,
/// The write turn (calls.rs INVARIANT 2): held for a write call's
/// whole run, from its key being looked up to its closing line, and
/// for the owner's format upgrade, so a call's lines are one run in
/// the journal.
writes: tokio::sync::Mutex<()>,
/// The books' base currency, fixed at their creation: read without
/// the lock (the connect text names it).
currency: Currency,
@@ -240,6 +245,13 @@ impl SharedStore {
Ok(f(store))
}
/// Wait for the write turn (calls.rs INVARIANT 2). Blocking, on a
/// blocking thread, as the store's lock is; unlike it, the turn is
/// held across a call's awaits, so its guard is `Send`.
pub(crate) fn write_turn(&self) -> tokio::sync::MutexGuard<'_, ()> {
self.writes.blocking_lock()
}
/// Run `f` on the store under its lock, writing: the owner's format
/// upgrade, after its backup (backup.rs). Blocking. Refused once the
/// books are closing or the server stopped (INVARIANTS 2, 3), counted
@@ -257,6 +269,8 @@ impl SharedStore {
if self.closing() {
return Err("the books are closing".into());
}
// Not inside a tool's call: its lines are a run of their own.
let _turn = self.write_turn();
let mut store = self.store.blocking_lock();
if self.stopped() {
return Err(STOPPED.to_string());
@@ -318,6 +332,7 @@ impl SharedStore {
hurry: Arc::default(),
running: std::sync::atomic::AtomicUsize::new(0),
ended: tokio::sync::Notify::new(),
writes: tokio::sync::Mutex::new(()),
})
}
@@ -1514,6 +1529,8 @@ enum ShownDates {
mod access_tools;
#[path = "tools/backup.rs"]
mod backup_tools;
#[path = "calls.rs"]
mod calls;
#[path = "tools/files.rs"]
mod files;
#[path = "guards.rs"]
@@ -1783,11 +1800,17 @@ impl BooksServer {
impl ServerHandler for BooksServer {
async fn call_tool(
&self,
request: rmcp::model::CallToolRequestParams,
mut request: rmcp::model::CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> Result<rmcp::model::CallToolResponse, rmcp::ErrorData> {
use crate::access::{self, Call};
use ledger_store::access::Outcome;
// The request key is the call's own, not the tool's: out of the
// arguments here, so no tool, preview digest or record sees it as
// an argument (calls.rs INVARIANT 1). A call writes when its tool
// can change the books and it is not a preview.
let request_key = calls::take_key(&mut request.arguments);
let writing = calls::is_write_call(&request.name, request.arguments.as_ref());
let mut call = Call::new(&self.access, &self.board, &context, "tool", &request.name);
let outcome_of = |out: &Result<rmcp::model::CallToolResponse, rmcp::ErrorData>| match out {
Ok(rmcp::model::CallToolResponse::Complete(r)) if r.is_error != Some(true) => {
@@ -1861,7 +1884,16 @@ impl ServerHandler for BooksServer {
Err(rmcp::ErrorData::invalid_params(why, None)),
)
}),
};
}
// A write without a usable request key isn't run (calls.rs
// INVARIANT 1).
.or_else(|| {
if !writing {
return None;
}
calls::key_problem(request_key.as_ref())
.map(|why| (calls::MISSING_KEY, Ok(tool_error(why))))
});
if let Some((code, answer)) = refusal {
call.digested(refused_digest(request.arguments.clone()).await);
return self.refuse(&call, code, answer).await;
@@ -1900,6 +1932,46 @@ impl ServerHandler for BooksServer {
});
// Counted as running by now (run_blocking): hashed here.
let digest = Call::digest_of(request.arguments.as_ref());
// A write call is one transaction (calls.rs): it takes its
// turn among writes, and is opened, or answered from its
// key, before the tool runs.
let call_id =
request_key
.as_ref()
.filter(|_| writing)
.map(|k| ledger_store::calls::CallId {
token: running_call
.caller()
.map(|c| c.token.id.clone())
.unwrap_or_default(),
tool: tool_name.clone(),
key: calls::key_text(k),
args: digest.clone().unwrap_or_default(),
});
let _turn = call_id.as_ref().map(|_| server.store.write_turn());
let early = match &call_id {
None => None,
Some(id) => match calls::begin(&server, id.clone()) {
Ok(calls::Begun::Run) => None,
Ok(calls::Begun::Answer(answer, code)) => Some((answer, code)),
Err(why) => Some((Box::new(tool_error(why)), access::STOPPED)),
},
};
if let Some((answer, code)) = early {
// Not run: refused (with the guards a refusal carries),
// or answered again from the books as it was first
// answered.
let answer = if code == calls::REPEAT {
Ok(*answer)
} else {
next_steps::apply(&server, &tool_name, envelope_args.as_ref(), Ok(*answer))
};
YOU.with(|y| y.borrow_mut().take());
let mut c = running_call;
c.digested(digest);
let recorded = c.record_blocking(Outcome::Refused(code.into()), None);
return (answer, recorded);
}
let tcc =
rmcp::handler::server::tool::ToolCallContext::new(&server, request, context);
let router = Self::tool_router();
@@ -1927,7 +1999,26 @@ impl ServerHandler for BooksServer {
}
};
// The result envelope (next_steps.rs INVARIANT 1).
let out = next_steps::apply(&server, &tool_name, envelope_args.as_ref(), out);
let mut out = next_steps::apply(&server, &tool_name, envelope_args.as_ref(), out);
let stopped = STOPPED_BY_CLOSE.with(|s| s.replace(false));
// The call is over: its closing line, holding the answer
// the caller is given, commits it (or, for a call that
// failed or was stopped, rolls it back). Only then does it
// count as done (calls.rs INVARIANT 4).
let mut wrote = wrote;
if call_id.is_some() {
let ok = calls::answered_ok(&out, stopped);
if let Err(why) = calls::close(&server, &out, ok) {
eprintln!("wanderbooks: a call could not be closed: {why}");
server.store.stop();
out = Ok(tool_error(calls::NOT_CLOSED.to_string()));
}
// The closing line is this call's last write.
wrote = WROTE
.with(|w| w.borrow_mut().take())
.map(|(seq, hash)| ledger_store::chain::Head { seq, hash })
.or(wrote);
}
// Who asked is the request's alone: a later request on this
// thread (a resource read) must not see it.
YOU.with(|y| y.borrow_mut().take());
@@ -1936,7 +2027,7 @@ impl ServerHandler for BooksServer {
c.with_agent(AGENT_NOTE.with(|a| a.borrow_mut().take()));
// A call a close stopped is on record as stopped, not as an
// answer it gave (local.rs INVARIANT 10).
let outcome = if STOPPED_BY_CLOSE.with(|s| s.replace(false)) {
let outcome = if stopped {
Outcome::Refused(access::STOPPED_BY_CLOSE.into())
} else {
outcome_of(&out)
@@ -1977,6 +2068,9 @@ impl ServerHandler for BooksServer {
context: RequestContext<RoleServer>,
) -> Result<rmcp::model::ListToolsResult, rmcp::ErrorData> {
let mut tools = Self::tool_router().list_all();
// Every tool that can change the books takes a request key
// (calls.rs INVARIANT 1).
tools.iter_mut().for_each(calls::add_key_to_schema);
// A token is shown what it can call (scopes.rs); the list then
// differs by caller, so it may be cached only for this one.
let scopes = crate::access::caller(&context)