Merge branch 't3/transactional-mcp-writes': every write through the tools is one transaction, exactly once (P15 T1, D-88, D-89)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 10:40:34 -07:00
commit 38647e2143
61 files changed
+5801 -339

No files matched your search

+63
View File
@@ -611,6 +611,69 @@ with generated URIs, checks after each call that the server still
serves, restarts it after them to check the books reopen, and drives
every tool over every book in ledger-store's `tests/fixtures/old-books`.
## Writes: one transaction, one request key (plan P15 2a, D-88)
Every tool that can change the books (no `readOnlyHint`; 83 of the 140: the
three that read what is sealed and put it in a file, `books.vendor_1099`,
`books.get_w9` and `books.export_cpa_package`, are annotated so but write
nothing and may answer with a full TIN, so they are read like any read)
takes one more argument than its own, `request_key`, and its schema says it
is required: 8 to 128 characters of plain text the agent makes up, new for
each different action and never reused for another. The agent sends the same
call again with the same key when its answer is lost; the books do not do it
twice, and answer what they answered the first time (a repeat is recorded in
the access log as `repeat`, and writes nothing, even after the books were
closed and opened). The same key with other arguments, or another tool, is
refused in words and changes nothing. A key is the token's own. A write
without a usable key is refused in words before it runs. A preview (a tool
with `confirmed`, called without `confirmed: true`) writes nothing and needs
no key. A failed write changes nothing and leaves its key free: the corrected
call may use it; where the call got as far as writing journal lines, the
journal says it failed (`call.closed`, outcome `failed`) and none of them
count. Every write call is one transaction: its lines commit together with
its closing line or not at all, and after a crash a call without its closing
line is not treated as done (WB-format-8 to WB-format-10).
What the books keep of a call, and what a repeat is told:
- A call that wrote journal lines is framed by `call.begun` and
`call.closed`; its closing line keeps the answer (at most 4096 bytes). A
repeat of it returns that answer byte for byte. If the answer was longer,
the closing line keeps its SHA-256 and length instead, and a repeat is
told in words that the call was done, with the journal lines it wrote
(`books.journal_history` with `after_seq` lists them), and nothing is
done again.
- A call that wrote no journal line leaves no line at all. Its key and
answer are kept in memory for the life of the server: a repeat within the
session gets the same answer, and after a restart a repeat simply runs
again, which is harmless because it wrote nothing (`books.back_up_now`,
`books.begin_upload`).
- "The same arguments" is a keyed digest of them, tax ids included, under a
secret of the books' own that is never journaled: two requests that differ
only in a tax id are two requests, and the same key with the other tax id
is refused.
- A write that fails after it wrote lines is on the record
(`books.journal_history` lists its lines and says of each that it did not
count); ids it took are handed out again to the next call, so a reader
that takes the journal raw would pin its dead lines on whatever takes the
id next: every reader applies the frame rule, and the journal in words
(`books.journal_history`) and the viewer's journal page show every line and
say of the ones that did not count that they did not.
- A write to the tax-id vault is part of the call: it is final only when the
call commits, so a failed or cut call never replaces or destroys a tax id.
- A request that arrives while a call that has written a line is still
open waits for its closing line: no read shows what a call did before the
call committed.
- If a call's closing line is saved but the books' working copy cannot be
brought up to it, the call is told so in words: it was completed and
counts, the books server stopped to protect the books, and sending the call
again with the same key answers what it answered and changes nothing.
Books an older version wrote take no write at all until the owner gives the
format upgrade its go-ahead (D-89, WB-format-11): every write is refused,
before it runs, with `format_upgrade_pending` and the words "nothing can be
written"; every read works as ever. No tool can give the go-ahead.
## Tool design principles
- **Narrow tools, explicit arguments.** No "do what I mean" tools. The agent
+8
View File
@@ -257,3 +257,11 @@ Three new rows, none above changed status, and the floor stands.
| Agent dock: Claude Code or Codex in a terminal along the bottom of the viewer, started in the books home; never required | QBO's assistant panel is a cross-check only | partial | `dock_runs_the_chosen_agent`, `agent_missing_offers_the_installer` (`crates/ledger-gui/tests/dock.rs`); Linux checked; macOS by the CI run; Windows not offered (ConPTY unchecked) |
| Assistants in the window: each company's state (none, connected, working) as a shape and a word, "Ask your assistant" where a button that edits would be, the Connect page (a row per local tool, the keys with who is on each, Revoke), and a warning before closing the window or a tab with an assistant connected (plan P15 3.3, D-79) | QBO's "Accountant" invitations and Xero's "Advisor" access are a cross-check only | partial | WB-assist-1 to WB-assist-4; `crates/ledger-gui/tests/assistants.rs`; local assistants only: the web half is plan units B1 and B3, the Claude desktop extension C1 |
| First run with no terminal: the viewer sets up the private folder, the owner passphrase, the first books and an agent | QBO's "set up your company" flow | partial | `first_run_onboards_with_no_terminal` (`crates/ledger-gui/tests/welcome.rs`); the AppImage's whole path is the EM's to try on a clean machine |
## P15 T1: every write is one transaction, exactly once (2026-11-21, D-88, D-89)
| Area | Counterpart (a cross-check, not the spec) | Status | Notes |
|---|---|---|---|
| A tool call's lines commit together with a closing line or not at all; a call that never closed, or failed, builds nothing in any replay; a failed call is on the record | A database transaction; QBO and Xero make a write all or nothing and say so on failure | partial | WB-format-8, WB-format-10; `ledger-store` `tests/calls.rs` (`a_crash_at_each_point_of_a_call_leaves_the_whole_call_or_none_of_it`, `a_replay_of_any_prefix_inside_a_call_is_the_replay_of_the_prefix_before_it`, `a_failed_call_is_on_the_record_and_builds_nothing`); partial until the Lean law is in bookspec and a `done` needs a golden |
| Every write carries a request key; a repeat returns the first answer and writes nothing, across a restart; the same key for another request is refused | An idempotency key (a payments API's `Idempotency-Key`); QBO's `requestid` | partial | WB-format-9; `ledger-cli` `tests/call_transactions.rs` (`a_repeat_of_a_key_returns_the_first_answer_and_writes_nothing`, `the_same_key_for_another_request_is_refused_in_words`, `every_tool_that_can_write_asks_for_a_key_and_no_other_does`); `books.import_bank_csv` is one transaction (`the_import_is_one_transaction`) |
| Books that haven't declared the feature take no write and still read; every set of books has a unique id | No counterpart; the owner's rule (D-89) | partial | WB-format-11, WB-format-12; `ledger-store` `tests/calls.rs` (`books_that_have_not_declared_the_feature_take_no_call_and_still_read`, `new_books_are_born_with_an_id_and_no_two_share_one`); `ledger-cli` `tests/format_upgrade.rs` (`books_p2_served_cut_over_whole`) |
+24
View File
@@ -196,6 +196,30 @@ a backup is then proven where it lands, by a drill (docs/mcp-interface.md,
`method` ({model?, reasoning_effort?, adversarial?, verifier_agents?}) is
that client's own word. Whether a claim counts is never journaled: it is
derived against `policy.attestation_bar` when asked.
- **A tool call is a frame, not a record** (plan P15 2a, D-88, feature
`call_transactions`): a call that writes lines is framed by
`call.begun {tool, token, key, args}` before its first line and
`call.closed {tool, token, key, args, outcome, first_seq, last_seq,
answer | answer_sha256 + answer_bytes | error}` after its last. `args`
is an HMAC-SHA256 of the call's arguments under the books' own secret
(`keys/calls.key`), never a plain hash (a tax id would be guessable). The
lines between count only when `outcome` is `ok` (then `answer` is the
text of the answer given, kept so a repeat of `key` returns it, when at
most 4096 bytes; above that `answer_sha256` and `answer_bytes` stand in
its place); `failed` and `unfinished` (written by the open that finds a
call that never closed) say the lines in the frame never count, in any
replay. A call that wrote no line has no frame and no closing line (its
key and answer are kept in memory for the life of the server only). A
line outside a frame applies as ever, so every journal written before
the feature replays as it was. **Every reader that derives a fact reads
the committed journal** (`calls::committed`: the lines outside a frame and
those of `ok` frames), because ids are handed out again after a failed
call and a raw reader would pin the dead lines on whatever takes the id
next; a reader that shows history shows every line and says of the dead
ones that they did not count (`calls::standings`). `store.created` carries
`books_id` (the books' own unique id) in books made since the feature;
older books are given one in the `journal.format_set` of their upgrade.
Neither frame line moves the audit's figure head.
- The store is policy-free: it records attachments faithfully. Whether a
posting *requires* a receipt is decided by the MCP layer's receipt policy
(`docs/mcp-interface.md`), which reads the `attachments` table.
+153 -3
View File
@@ -171,9 +171,10 @@ in their second line, so they wait for nothing. Books an older version wrote
declare less (format 1, written before the gate, declares nothing): they *wait
for a format upgrade*, which is a fact of their journal alone
(`JournalFormat::upgrade`). They open and replay as they are, byte for byte;
everything that needs nothing new is written as ever; a write that would need
the newer format or a feature they haven't declared is refused before anything
is journaled (`StoreError::FormatUpgradePending`, guard
everything that needs nothing new is written as ever, except that since D-88
and D-89 they take no tool write at all (WB-format-11); a write that would
need the newer format or a feature they haven't declared is refused before
anything is journaled (`StoreError::FormatUpgradePending`, guard
`format_upgrade_pending`), never declared on the way. Every setting key and
tax category an earlier version didn't read needs a feature
(`format::setting_feature`, `format::tax_category_feature`: the P3 data-safety
@@ -219,6 +220,155 @@ others are there; none is deleted for being stale.
---
**WB-format-8 Every write a tool makes is one transaction in the journal.**
(The owner, 2026-11-20, D-88: "Writes have to be TRANSACTIONAL ALWAYS! You
can record a failed transaction.") A tool call that writes journal lines
frames them: `call.begun { tool, token, key, args }` just before its first
line, and `call.closed { tool, token, key, args, outcome, first_seq,
last_seq, answer | answer_sha256 + answer_bytes | error }` when it is over,
`outcome` being `ok` (the answer given is `answer`, kept as text so a repeat
returns it byte for byte, when it is at most 4096 bytes; above that the line
keeps `answer_sha256`, the SHA-256 of the answer, and `answer_bytes`, its
length, so every closing line is bounded), `failed` (`error` says how), or
`unfinished` (written by the next open of books whose last call never
closed). `args` is a keyed digest of the call's arguments (WB-format-9). A
call that wrote no journal line has no frame and no closing line: there is
nothing to frame, and nothing it did can be half done. The lines between a
begin and its close count only when the close says `ok`: that is how a reader
tells a complete call from an incomplete one. Every replay (opening the
books, the viewer, verify, rebuild, the projection a restart catches up) goes
through one rule (`CallGate`): a line outside a frame applies at once (every
line before the books declared `call_transactions`, and the owner's own acts:
the format declaration, a repaired tail); a line inside a frame waits, and is
applied with its closing line when that says `ok`, and never when it says
`failed` or `unfinished` or when no closing line follows. So no state is
built from half a call, and a replay of any prefix that ends inside a call
equals the replay of the prefix before it. A close must name the frame it
ends (its `first_seq` is the begin line's, its `last_seq` the line before
it); a frame that begins inside another, a close with no frame, or a close
that does not match is a corrupt journal. The projection keeps a call's
changes in one SQLite transaction, released only after the `ok` closing line
is on disk. When the books are opened and the journal's last call never
closed, its lines are not applied, it is closed `unfinished` before anything
else is written, and a projection that holds its lines is rebuilt. The
envelope `{seq, ts, type, payload, prev}` is untouched: the feature adds two
line types (`call_transactions`, WB-format-2) and nothing else. Calls take
turns: the server runs one write call at a time, so a call's lines are one
run in the journal. `books.import_bank_csv` is one transaction like every
other write (plan P15 6a).
**Every reader of the journal applies the frame rule, because ids are reused
after a failed call.** A failed call's entry, account, party and document ids
were never taken (nothing it wrote counted), so the next call hands them out
again; a reader that took the raw lines would pin the dead lines of a failed
call on whatever took the id next (a failed `books.create_bill` for entry 42,
then a manual entry 42, would read as a "Bill"). So a reader that derives a
fact (an entry's origin and events, an asset's trail, a party's events, an
attention item, a package, what adopting books would keep) reads the
committed journal (`committed`: the lines outside any call and those of calls
that closed `ok`, the frame lines left out) and never the raw one; a reader
that shows history (the viewer's journal page, `books.journal_history`)
shows every line and says in words of each line of a call that failed, never
finished or is still running that it did not count (`Standing`, `standings`).
Backups and restores copy the journal's bytes whole and read none of its
facts. The reader's view of a running call: a viewer reads a call's lines only
once its closing line is read; a request on the server that arrives between a
call's tool finishing and its closing line, when the call has written a line,
waits for the close, so no read shows what a call did before the call
committed.
*Enforced:* `ledger_store::calls` (`CallGate`, `gate_all`, `standings`, `committed`, `Standing`, `closed_of`, `begun_of`, `ANSWER_CAP`, `Store::begin_call`, `Store::close_call`), `Store::open`, `Store::verify`, `Store::rebuild`, `Snapshot::refresh`, `Snapshot::committed_journal`, `Snapshot::journal_standings`, `Store::journal_history`, `history_standing`, ledger-package `PackageBooks`, ledger-gui `Books::build`, `adopt`, `format::CALL_TRANSACTIONS`, ledger-mcp `SharedStore::write_turn`, `BooksServer::lock`.
*Checked:* `ledger-store` `tests/calls.rs` (`a_call_is_framed_and_its_answer_kept_by_key`, `a_call_that_wrote_nothing_leaves_no_line_and_is_remembered_for_the_session`, `a_long_answer_is_kept_as_its_hash_and_a_repeat_is_told_in_words_what_was_done`, `the_committed_journal_leaves_out_the_lines_of_a_call_that_failed`, `a_crash_at_each_point_of_a_call_leaves_the_whole_call_or_none_of_it`, `a_replay_of_any_prefix_inside_a_call_is_the_replay_of_the_prefix_before_it`, `a_viewer_applies_a_call_only_when_it_closes`, `a_frame_that_does_not_close_what_it_follows_is_corrupt`, `a_call_cannot_begin_inside_another_or_after_a_failed_close`, `a_call_whose_begin_line_the_projection_refuses_still_closes_its_frame`, `books_the_version_before_call_transactions_wrote_open_and_report_the_same_figures`), `tests/store.rs` (`books_written_under_older_rules_still_open`, `books_an_earlier_version_wrote_open_to_its_figures`), the unit test `call_transactions_is_its_own_feature` in `format.rs`, `tests/format.rs` (`every_event_the_store_replays_passes_the_gate`); `ledger-gui` `tests/view_model.rs` (`a_failed_calls_lines_are_not_facts_when_its_ids_are_used_again`: the viewer's books, the feed attention reads, and the history after a failed call and an id handed out again); `ledger-cli` `tests/call_transactions.rs` (`a_call_is_one_frame_in_the_journal_closed_with_its_answer`, `the_import_is_one_transaction`, `a_call_that_wrote_lines_and_then_failed_leaves_the_books_and_its_key_as_they_were`).
**WB-format-9 Every write call carries a request key, and a repeat of it
writes nothing.** Every tool that can change the books (no `readOnlyHint`;
83 of the 140: the three that only read what is sealed, `books.vendor_1099`,
`books.get_w9` and `books.export_cpa_package`, write nothing and may answer with
a full TIN, so they are reads) takes a required `request_key`, 8 to 128 characters of plain
text, which the agent makes up, new for each action. A write without a usable
key 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. A repeat of a (token, key) whose
call committed, with the same tool and arguments, returns that call's answer
as it was given, writes nothing, and is recorded in the access log as
`repeat`; with other arguments or another tool it is refused in words as a
misuse of the key, and nothing is changed. A key is the token's own: two
tokens with one key are two keys. "The same arguments" is a keyed digest
(`args_mac`): HMAC-SHA256 over the arguments' canonical JSON, unmasked, under
a secret of the books' own (`keys/calls.key`, 32 random bytes, made on first
need, mode 0600, never journaled, logged or answered), so two requests that
differ only in a tax id are two requests, and nothing journaled lets anyone
recover or test a guess of a tax id (a plain hash of nine digits would be
guessable). The key file is part of the books' keys folder and travels with
its backups; books restored without it refuse a repeat of an older key as a
misuse of the key (nothing is changed), never as a repeat. The key table is
rebuilt at every open from the `ok` closing lines alone and holds one small
entry per committed call, no answers: a repeat reads its answer from the
closing line. A call that answered `ok` but wrote no journal line leaves no
line, so its key and answer are kept in memory for the life of the server
only, in a table bounded at 512 calls and 2 MiB of answers, oldest first out
(an answer above 4096 bytes is not kept); a repeat within the session gets
the same answer, and after a restart a repeat simply runs again, which is
harmless because it wrote nothing (`books.back_up_now` and `books.begin_upload`
are such calls: a second backup or a second empty upload slot is no harm).
A call that wrote lines and whose answer was above 4096 bytes is repeated in
words: it says the call was done, with its journal lines, the answer's
length and SHA-256, that the answer was too long to keep, and writes
nothing. A repeat that arrives while its original is still running waits for
it (calls take turns) and gets its answer. An answer is kept as given: no
tool answer holds a full TIN, and the three tools that can are no
transaction; an error text that holds one is kept masked (BS-partner-2: no
journal line holds one).
*Enforced:* ledger-mcp `calls::is_write_call`, `calls::key_problem`, `calls::begin`, `calls::close`, `calls::add_key_to_schema`, `Store::recorded_call`, `Keys`, `Replay`, `args_mac`, `Store::args_mac`, `hmac_sha256_hex`, `ANSWER_CAP`, `SESSION_KEYS_MAX`.
*Checked:* `ledger-cli` `tests/call_transactions.rs` (`a_repeat_of_a_key_returns_the_first_answer_and_writes_nothing`, `the_same_key_for_another_request_is_refused_in_words`, `a_write_without_a_usable_key_is_not_run`, `every_tool_that_can_write_asks_for_a_key_and_no_other_does`, `the_same_key_under_two_tokens_is_two_calls`, `a_repeat_arriving_while_its_original_runs_gets_its_answer_and_changes_nothing`, `a_repeat_of_a_call_whose_answer_was_too_long_is_told_it_was_done`, `a_call_that_wrote_nothing_leaves_no_line_and_is_answered_again_only_within_the_session`, `a_key_reused_for_another_tax_id_is_refused_not_repeated`); `ledger-store` `tests/calls.rs` (`a_call_is_framed_and_its_answer_kept_by_key`, `the_sessions_key_table_is_bounded_in_count_and_in_bytes`, `a_long_answer_is_kept_as_its_hash_and_a_repeat_is_told_in_words_what_was_done`) and the unit tests `hmac_sha256_matches_rfc_4231` and `args_mac_tells_tax_ids_apart_and_holds_none` in `calls.rs`.
**WB-format-10 A failed write is on the record, and builds nothing.** A call
that answers with an error, is stopped by a close, or fails any other way
rolls back: if it had written lines, its closing line says `failed` with how,
its lines are never applied, the projection's changes are rolled back and the
live ledger is rebuilt from the journal without them; if it had written none,
the access log has it (as every call has) and the journal has nothing. A
failed call leaves its key unused: nothing happened under it, so the corrected
call may use it. Nothing a call writes outside the journal is final before
the call commits: the tax-id vault's writes (`books.set_tin`, `books.set_w9`,
`books.purge_tin`) are staged under names no reader opens and made final when
the call's closing line is written, or dropped (and a purge put back) when
the call fails; a crash between the closing line and that settling is
finished at the next start by what the journal says of the call, so a failed
or cut call never replaces or destroys a record (`ledger_vault::settle`,
`pending_tags`). A call whose closing line cannot be written has not
committed; the store takes no further write, and the books server stops, as
it does when an access record cannot be written. A call whose closing line
*is* on disk but whose projection cannot be brought up to it has committed:
the store heals from the journal where it can, and where it cannot says so
(`StoreError::CallCommitted`: the call was completed and counts, do not do
it again), stops taking writes, and is rebuilt at the next open.
*Enforced:* `Store::close_call`, `StoreError::CallCommitted`, ledger-mcp `calls::answered_ok`, `calls::close`, `calls::recover_vault`, `calls::put_tin`, `calls::purge_records`, `calls::settle_vault`, `ledger_vault::stage_tin`, `ledger_vault::stage_w9`, `ledger_vault::stage_purge`, `ledger_vault::settle`, `ledger_vault::pending_tags`.
*Checked:* `ledger-store` `tests/calls.rs` (`a_failed_call_is_on_the_record_and_builds_nothing`, `a_call_that_wrote_nothing_leaves_no_line_and_is_remembered_for_the_session`, `a_closing_line_on_disk_but_sqlite_failing_is_a_call_that_counts`); `ledger-vault` `tests/vault.rs` (`a_staged_record_is_read_by_no_one_until_its_call_commits`, `a_call_that_did_not_commit_leaves_the_old_record_whole`, `a_purge_is_final_only_when_its_call_commits`); `ledger-cli` `tests/call_transactions.rs` (`a_failed_write_leaves_the_books_and_its_key_as_they_were`, `a_call_that_wrote_lines_and_then_failed_leaves_the_books_and_its_key_as_they_were`, `a_crash_with_the_ok_close_on_disk_but_sqlite_not_committed_leaves_the_call_done_once`, `a_set_tin_that_fails_leaves_the_vault_as_it_was`, `a_crash_between_the_commit_and_the_vault_settling_is_finished_at_the_next_start`).
**WB-format-11 Books that have not declared `call_transactions` take no
write.** (The owner, 2026-11-20, D-89.) This replaces WB-format-3's "everything
that needs nothing new is written as ever" for writes: from the release that
carries the feature, books an older version wrote are opened and read as
ever, and every write call is refused, before it runs, with the upgrade remedy
and the words "nothing can be written", until the owner's go-ahead (the format
upgrade, WB-format-3). So there is one way a write can happen, the one that is
proved. The Store's own commands, which the owner's acts and the tests call
directly outside a call, are unchanged; `Store::begin_call` refuses.
*Enforced:* `Store::ensure_takes_calls`, `Store::begin_call`, `StoreError::FormatUpgradePending`, ledger-mcp `calls::begin`.
*Checked:* `ledger-store` `tests/calls.rs` (`books_that_have_not_declared_the_feature_take_no_call_and_still_read`), the unit test `call_transactions_is_its_own_feature` in `format.rs`; `ledger-cli` `tests/format_upgrade.rs` (`books_p2_served_cut_over_whole`, `format_upgrade_waits_for_consent`, `documents_v2_refused_until_consent`).
**WB-format-12 Every set of books has a unique id.** (Plan P15 6a: the hash of
the journal's first line, which carries a currency and a time to the second,
told two sets of books apart badly.) Books made since `call_transactions` are
born with `books_id`, 128 random bits in hex, in their `store.created` line.
Older books get one with the format upgrade: the `journal.format_set` that
declares `call_transactions` carries `books_id` when the books have none. The
id is read from the journal alone (`books_id_of`), so it is the same after a
restart and for the viewer, and a restored backup keeps it. Books that have
had neither yet have no id (`Store::books_id` is None).
*Enforced:* `Store::create`, `Store::declare_journal_format`, `Store::books_id`, `Snapshot::books_id`, `books_id_of`.
*Checked:* `ledger-store` `tests/calls.rs` (`new_books_are_born_with_an_id_and_no_two_share_one`, `books_that_have_not_declared_the_feature_take_no_call_and_still_read`), `tests/format.rs` and `tests/format_upgrade.rs` (the upgrade's declaration holds the id).
## B-34 The QuickBooks Desktop file (IIF): plain ASCII, every entry through as_of, closes left out, classes carried
**bookspec:** `BS-format-2`