fix(mcp,vault,store): the tax-id vault is inside the transaction; a read never sees an open call; the commit outcome is told truthfully (P15 T1 review)

The vault wrote a tax id before its journal line, so a call that failed or
was cut left the new tax id (and lost the old) with no line to say so. A
vault write made for a call is now staged under a name no reader opens
(.staged-TAG, .purging-TAG) and is made final, or dropped, only when the
call closes: a purge is set aside and put back if the call fails. A crash
between the closing line and the settle is finished at the next start by
what the journal says of the call. The readers that check a vault record
against the party's committed vault_record hash (the 1099 and CPA package
paths) therefore only ever see committed records; the filing scan reads
every final record on purpose (more tax ids scanned is safer).

close_call(Done) told the caller none of a call counted when its closing
line was on disk and only SQLite failed. It counted: the store now heals
from the journal where it can, and otherwise says the call counts, stops
taking writes and rebuilds at the next open.

A call whose begin line the projection refused left its frame open, so the
next call began inside it (a corrupt journal): the frame is now closed
failed.

A read arriving between a call's return and its closing line waits for the
close (a call that has written no line leaves reads free), so no read shows
what a call did before it committed.

Tests added through the tools: a call that writes lines and fails, a repeat
arriving while its original runs, the same key under two tokens, a crash
with the ok close on disk and SQLite behind, the vault under a failed and a
crashed call, a key reused for another tax id. The helpers that drop frame
lines no longer back the checks that a refusal or a preview wrote nothing.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 10:19:26 -07:00
1 parent d4c0036599
commit 98cbabde8e
22 files changed
+1516 -169

No files matched your search

+27 -1
View File
@@ -612,6 +612,19 @@ pub fn committed(lines: &[JournalLine]) -> Result<Vec<JournalLine>, StoreError>
.collect())
}
/// A call's tag: 16 lowercase hex digits naming it in file names outside
/// the journal (a staged vault record), derived from its token and key,
/// so a later open can ask the journal whether that call committed
/// ([`Store::call_committed`]).
pub fn call_tag(token: &str, key: &str) -> String {
let mut h = Sha256::new();
h.update(b"wanderbooks-call\0");
h.update(token.as_bytes());
h.update(b"\0");
h.update(key.as_bytes());
hex::encode(&h.finalize()[..8])
}
/// HMAC-SHA256 (RFC 2104) of `msg` under `key`, hex.
pub(crate) fn hmac_sha256_hex(key: &[u8], msg: &[u8]) -> String {
const BLOCK: usize = 64;
@@ -773,6 +786,15 @@ impl Store {
}))
}
/// Whether the call with this tag ([`call_tag`]) committed with lines
/// on the journal: its closing line says `ok`.
pub fn call_committed(&self, tag: &str) -> bool {
self.keys
.by
.keys()
.any(|(token, key)| call_tag(token, key) == tag)
}
/// The table's sizes: calls with lines, and the session's calls that
/// wrote none with the bytes of their answers.
pub fn call_table(&self) -> (usize, (usize, usize)) {
@@ -834,10 +856,14 @@ impl Store {
return Ok(());
}
let line = self.journal.append(BEGUN, begun_payload(&call.id))?;
apply_line_to_sqlite(&self.conn, &line)?;
// The frame is open from the moment its line is on disk, whatever
// the projection says next: a call that fails here must close it
// (`failed`), or the next call would begin inside it (a corrupt
// journal).
if let Some(call) = &mut self.call {
call.begun = Some(line.seq);
}
apply_line_to_sqlite(&self.conn, &line)?;
Ok(())
}
+106
View File
@@ -596,3 +596,109 @@ fn a_call_that_rebuilds_the_projection_still_commits() {
let s = Store::open(tmp.path()).unwrap();
s.verify().unwrap();
}
/// The closing line is on disk but the projection can't be brought up to
/// it: the call committed, and is not reported as if it hadn't. The store
/// says so in words, takes no more writes, and the next open rebuilds the
/// projection from the journal with the call's effects in it (SCENARIO 4).
#[test]
fn a_closing_line_on_disk_but_sqlite_failing_is_a_call_that_counts() {
let tmp = tempfile::tempdir().unwrap();
let mut s = books(tmp.path());
// From the closing line on, the projection refuses every write: the
// call writes a begin line, a draft and two lines (four), then closes.
let closing_seq = lines_of(tmp.path()).last().unwrap().seq + 5;
let db_file = tmp.path().join(s.database_file());
let db = rusqlite::Connection::open(&db_file).unwrap();
db.execute_batch(&format!(
"CREATE TRIGGER wb_test_refuses BEFORE UPDATE ON meta \
WHEN NEW.key = 'last_applied_seq' AND CAST(NEW.value AS INTEGER) >= {closing_seq} \
BEGIN SELECT RAISE(ABORT, 'a test refuses this write'); END;"
))
.unwrap();
drop(db);
s.begin_call(id("key-commit-0001")).unwrap();
draft(&mut s, "Counts");
assert_eq!(lines_of(tmp.path()).last().unwrap().seq + 1, closing_seq);
let err = s
.close_call(CallEnd::Done(r#"{"entry_id":1}"#))
.unwrap_err();
let StoreError::CallCommitted(why) = &err else {
panic!("expected CallCommitted, got {err:?}");
};
assert!(why.contains("counts"), "{why}");
assert!(!why.contains("none of it counts"), "{why}");
// The record says ok, and the key is kept: a repeat is the first answer.
let last = lines_of(tmp.path()).into_iter().last().unwrap();
assert_eq!(closed_of(&last).unwrap().outcome, CallOutcome::Ok);
assert_eq!(
answer_of(&kept(&s, "token-1", "key-commit-0001").unwrap()),
r#"{"entry_id":1}"#
);
// No more writes until the books are opened again.
assert!(matches!(
s.begin_call(id("key-commit-0002")),
Err(StoreError::CallRefused(_))
));
drop(s);
rusqlite::Connection::open(&db_file)
.unwrap()
.execute_batch("DROP TRIGGER wb_test_refuses")
.unwrap();
let s = Store::open(tmp.path()).unwrap();
assert_eq!(
s.ledger().get_entry(1).map(|e| e.description()),
Some("Counts")
);
assert_eq!(
answer_of(&kept(&s, "token-1", "key-commit-0001").unwrap()),
r#"{"entry_id":1}"#
);
s.verify().unwrap();
}
/// The projection refuses a call's begin line itself: the line is on disk,
/// so the frame is open and the call must close it (`failed`). Left open,
/// the next call would begin inside it and the journal would be corrupt.
#[test]
fn a_call_whose_begin_line_the_projection_refuses_still_closes_its_frame() {
let tmp = tempfile::tempdir().unwrap();
let mut s = books(tmp.path());
let begin_seq = lines_of(tmp.path()).last().unwrap().seq + 1;
let db_file = tmp.path().join(s.database_file());
let db = rusqlite::Connection::open(&db_file).unwrap();
db.execute_batch(&format!(
"CREATE TRIGGER wb_test_refuses BEFORE UPDATE ON meta \
WHEN NEW.key = 'last_applied_seq' AND CAST(NEW.value AS INTEGER) = {begin_seq} \
BEGIN SELECT RAISE(ABORT, 'a test refuses this write'); END;"
))
.unwrap();
drop(db);
s.begin_call(id("key-begin-0001")).unwrap();
assert!(s.create_entry(d(2026, 1, 5), "Never").is_err());
s.close_call(CallEnd::Failed("the projection refused it"))
.ok();
drop(s);
rusqlite::Connection::open(&db_file)
.unwrap()
.execute_batch("DROP TRIGGER wb_test_refuses")
.unwrap();
let all = lines_of(tmp.path());
assert_eq!(
all.last().unwrap().line_type,
"call.closed",
"{:?}",
types_of(tmp.path())
);
assert_eq!(
closed_of(all.last().unwrap()).unwrap().outcome,
CallOutcome::Failed
);
// The books open, and the next call begins on a closed frame.
let mut s = Store::open(tmp.path()).unwrap();
s.begin_call(id("key-begin-0002")).unwrap();
draft(&mut s, "Fine");
s.close_call(CallEnd::Done("{}")).unwrap();
drop(s);
Store::open(tmp.path()).unwrap().verify().unwrap();
}