check / check (push) Canceled after 0s
The owner (2026-10-23): "We should probably just delete the stdio path altogether. THere's no point in that. And it sitting there unused will just lead to bitrot. Nuke it?" Books are served only by the books server (`wanderbooks server`), reached by agents through the bridge (`wanderbooks mcp --book ID`), which stays. Removed: `wanderbooks dev-stdio`, the `ledger-mcp` binary, and `ledger_mcp::run` (stdio and `--http` over a bare data dir), with all that only that mode had: - files the books make coming back inline: `HandedFile.content_base64` is gone, and `download` and `expires_at` are always there; - books with no access log or backups: `books.access_log`, `books.backup` and `books.back_up_now` are always offered, by scope; - the settings file read and reloaded (`legacy_files`), the vault key named by a file or `--vault-key-file`, and the books dir's key-inside-the-books and git checks (with `paths.rs`); - `wire.rs` (the bridge frames an agent's lines itself); - `books.settings`' file fields and `books.start`'s `settings_source`. `BooksServer` is built one way, from `Served` (a book as the books server serves it), in production and in the unit tests. An unknown tool is now a protocol error (mcp-tools.md). Inline file input is unchanged. Then the owner's three decisions (2026-10-23, the question tool): (a) `books.import_settings` is removed entirely (118 tools): old books come in whole by `wanderbooks books adopt`, which reads their settings file once; ledger-config keeps its settings-file parsing only as far as adoption uses it (`load` and the path-reading `effective_receipt_policy` are gone), and `books.chart_template` gives its settings as `books.set_setting` changes. (b) The viewer's loose-books-folder mode is removed (`--data-dir`, `--books-toml`, `--vault-key-file`, the settings file and config.json followed): it opens only books of the data root, reading settings from the journal and the vault from the book's own `vault/` and `keys/`. (c) `wanderbooks dev-stdio` has no stub: it is an unknown command like any other word. Also: an upload's expiry past the calendar is an error, never an empty string. Tests: every suite that drove the stdio server moved to crates/ledger-cli/tests/ and drives a real `wanderbooks server` on a data root of its own (tests/common), handed files by their download; mcp_stdio.rs is mcp_journeys.rs. Tests of what is gone are deleted and listed in docs/testing.md. Docs: plan D-44 (D-18 superseded), STATUS, testing, rules, mcp-interface, the handbook, README; justfile recipes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
200 lines
6.6 KiB
Rust
200 lines
6.6 KiB
Rust
//! Concurrency: several agents at once against one book's store.
|
|
//!
|
|
//! The books server gives every session its own service over the book's
|
|
//! one shared, locked store (ledger-mcp local.rs INVARIANT 4). Oracle (server.rs INVARIANTS 2 and
|
|
//! 5, under contention): clients posting at the same time leave books
|
|
//! that verify, a trial balance that balances, every posted entry with
|
|
//! its receipt (threshold 0.00), and each draft posted exactly once even
|
|
//! when two clients race to post it; a restart replays the same books.
|
|
|
|
use common::{Client, call};
|
|
use serde_json::json;
|
|
|
|
mod common;
|
|
|
|
const CLIENTS: usize = 6;
|
|
const ENTRIES: usize = 12;
|
|
|
|
fn b64(bytes: &[u8]) -> String {
|
|
const T: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
let mut out = String::new();
|
|
for c in bytes.chunks(3) {
|
|
let n = (u32::from(c[0]) << 16)
|
|
| (u32::from(*c.get(1).unwrap_or(&0)) << 8)
|
|
| u32::from(*c.get(2).unwrap_or(&0));
|
|
for (i, shift) in [18, 12, 6, 0].iter().enumerate() {
|
|
if i <= c.len() {
|
|
out.push(T[((n >> shift) & 63) as usize] as char);
|
|
} else {
|
|
out.push('=');
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
async fn draft(c: &Client, tag: &str, cents: u64, with_receipt: bool) -> u64 {
|
|
let e = call(
|
|
c,
|
|
"books.draft_entry",
|
|
json!({"date": "2026-03-01", "description": tag}),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let id = e["entry_id"].as_u64().unwrap();
|
|
let amount = format!("{}.{:02}", cents / 100, cents % 100);
|
|
for (account, side) in [("6000", "debit"), ("1000", "credit")] {
|
|
call(
|
|
c,
|
|
"books.add_line",
|
|
json!({"entry_id": id, "account": account, "side": side, "amount": amount}),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
if with_receipt {
|
|
call(
|
|
c,
|
|
"books.attach_receipt",
|
|
json!({"entry_id": id, "file_name": format!("{tag}.pdf"),
|
|
"mime_type": "application/pdf", "content_base64": b64(tag.as_bytes()),
|
|
"sha256": ledger_store::journal::sha256_hex(tag.as_bytes())}),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
id
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
|
async fn agents_posting_at_once_leave_whole_books() {
|
|
// Made on a thread of its own: the settings go in through a session
|
|
// with a runtime of its own (common.rs), never inside the test's.
|
|
let books = std::thread::spawn(|| {
|
|
common::Books::with(
|
|
"http-concurrency",
|
|
"USD",
|
|
Some("[books]\nreceipt_threshold = \"0.00\"\n"),
|
|
)
|
|
})
|
|
.join()
|
|
.unwrap();
|
|
let (run, token) = (books.home.run(), books.home.token(common::BOOK));
|
|
let connect = |run: std::path::PathBuf, token: String| async move {
|
|
common::connect_at(&run, common::BOOK, Some(token))
|
|
.await
|
|
.unwrap()
|
|
};
|
|
let setup = connect(run.clone(), token.clone()).await;
|
|
for (code, name, ty) in [
|
|
("1000", "Cash", "asset"),
|
|
("3000", "Capital", "equity"),
|
|
("6000", "Supplies", "expense"),
|
|
] {
|
|
call(
|
|
&setup,
|
|
"books.add_account",
|
|
json!({"code": code, "name": name, "account_type": ty}),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
// Drafts every client will race to post.
|
|
let mut contested = Vec::new();
|
|
for k in 0..ENTRIES {
|
|
contested.push(draft(&setup, &format!("contested {k}"), 100 + k as u64, true).await);
|
|
}
|
|
|
|
let mut tasks = Vec::new();
|
|
for n in 0..CLIENTS {
|
|
let (run, token) = (run.clone(), token.clone());
|
|
let contested = contested.clone();
|
|
tasks.push(tokio::spawn(async move {
|
|
let c = connect(run, token).await;
|
|
let (mut posted, mut refused, mut won) = (0usize, 0usize, 0usize);
|
|
for (k, &race) in contested.iter().enumerate() {
|
|
// A receipted entry posts; a bare one is refused.
|
|
let receipted = k % 3 != 0;
|
|
let id = draft(
|
|
&c,
|
|
&format!("c{n} e{k}"),
|
|
(n * 1000 + k + 1) as u64,
|
|
receipted,
|
|
)
|
|
.await;
|
|
match call(
|
|
&c,
|
|
"books.post_entry",
|
|
json!({"entry_id": id, "confirmed": true}),
|
|
)
|
|
.await
|
|
{
|
|
Ok(_) => posted += 1,
|
|
Err(_) => refused += 1,
|
|
}
|
|
assert_eq!(posted + refused, k + 1);
|
|
// Race everyone for the contested draft.
|
|
if call(
|
|
&c,
|
|
"books.post_entry",
|
|
json!({"entry_id": race, "confirmed": true}),
|
|
)
|
|
.await
|
|
.is_ok()
|
|
{
|
|
won += 1;
|
|
}
|
|
}
|
|
let _ = c.cancel().await;
|
|
(posted, refused, won)
|
|
}));
|
|
}
|
|
let (mut posted, mut refused, mut won) = (0, 0, 0);
|
|
for t in tasks {
|
|
let (p, r, w) = t.await.unwrap();
|
|
posted += p;
|
|
refused += r;
|
|
won += w;
|
|
}
|
|
let per_client_bare = (0..ENTRIES).filter(|k| k % 3 == 0).count();
|
|
assert_eq!(
|
|
refused,
|
|
CLIENTS * per_client_bare,
|
|
"every bare entry refused"
|
|
);
|
|
assert_eq!(posted, CLIENTS * (ENTRIES - per_client_bare));
|
|
assert_eq!(won, ENTRIES, "each contested draft posted exactly once");
|
|
|
|
let v = call(&setup, "books.verify", json!({})).await.unwrap();
|
|
assert_eq!(v["status"], "ok", "{v}");
|
|
let tb = call(&setup, "books.trial_balance", json!({}))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(tb["balanced"], true, "{tb}");
|
|
// Every posted entry has its receipt (threshold 0.00).
|
|
for id in 1..=(ENTRIES + CLIENTS * ENTRIES) as u64 {
|
|
let e = call(&setup, "books.get_entry", json!({"entry_id": id}))
|
|
.await
|
|
.unwrap();
|
|
if e["status"] == "posted" {
|
|
assert!(
|
|
e["receipts"].as_array().is_some_and(|r| !r.is_empty()),
|
|
"entry {id} posted without a receipt: {e}"
|
|
);
|
|
}
|
|
}
|
|
let head = v["head"].clone();
|
|
let _ = setup.cancel().await;
|
|
|
|
// A restart replays the same books.
|
|
books.restart();
|
|
let c = connect(run.clone(), token.clone()).await;
|
|
let v2 = call(&c, "books.verify", json!({})).await.unwrap();
|
|
assert_eq!(v2["status"], "ok");
|
|
assert_eq!(
|
|
v2["head"], head,
|
|
"the restarted server sees the same journal"
|
|
);
|
|
let _ = c.cancel().await;
|
|
}
|