MCP: short connect text, a handbook, tool annotations, safe local imports
- The 13K-character instructions (clients cut them short) become a
connect text of at most 1,400 characters whose first 512 stand alone,
plus books.handbook and books://handbook/{section}: the old guidance by
section, with no helper commands, home paths, pull requests or
file-reading suggestions (tool_text_lint).
- Every tool has a title and read-only/destructive hints; no
requiresUserInteraction anywhere (owner decision 2026-10-16).
- books.import_local_file reads only regular files inside the import
roots (the downloads folder by default, or --import-root), never the
data dir, books dir, vault or the key's folder, no symlink escapes or
hard links, and the file opened must be the file checked.
- The store lock is a tokio mutex taken off the runtime threads.
- get_info refreshes settings first; a bare books://digest reads.
Review fixes (P0 review, 2026-10-16):
- Fail closed: a tokio mutex never poisons and spawn_blocking caught the
panic, so after a panic mid-mutation the server kept serving a Store
behind its journal. A panic (the task, or while holding the store)
now stops the shared store; every later request on every session is
refused with a plain message; a restart re-verifies the journal. The
join error goes through redact_refusal and never carries the panic's
text.
- books.set_w9 w9_file_path read any path (a link named w9.pdf to the
vault key was stored and get_w9 wrote it out): it now goes through the
import roots' lockdown, its type judged on the canonical path.
- Sealed even inside a root: the key file in force, where a linked one
leads, both folders, and the default key folder whatever the settings
say. Refusals check the roots first, so outside them every path gets
one message (no probing what exists); inside, other books-dir files say
to copy them out first.
- The open is O_NOFOLLOW | O_NONBLOCK (a pipe swapped in after the check
can't hang a thread) and must match the checked file's device and
inode. New dependency: libc (unix only), for those two open flags,
which std doesn't name.
- Default import roots are the downloads folder only (the plan); files
with full TINs (get_w9, vendor_1099 include_tins, the CPA package with
TINs) are never written into an import root, and the server refuses to
import any file it wrote with TINs (by SHA-256).
- set_tin and set_w9 overwrite vault records: destructive_hint = true;
annotations_complete pins the key tools' hints. The read-only hint is
checked in behaviour on every spec_mcp call (journal and data-dir files
unchanged).
- Text: the connect text names the settings file as the one local file
to edit, says every entry balances on real accounts and names its
party, says invoice/bill/payment/payroll tools post unchecked, rewords
the exemptions, and shows the company name as one plain line; the
handbook takes section "all", lists its ids in the schema and says to
read contract first; books.settings drops the config.json path and no
error names the data dir or the key file. tool_text_lint covers the
whole tool JSON and resources and bans git/commit/FL-0001; tool
descriptions over 700 characters are pinned to today's eight (P5).
- digest: an empty since= is bare; the default date is pinned on every
day of a month.
Round-2 review fixes (2026-10-16):
- TIN files (B-26): the in-memory, per-session set of TIN-file hashes
became one registry beside the vault (vault/tin-files.sha256, hashes
only), shared by every session and every later server. It holds the
W-9 set_w9 reads (recorded as read), get_w9 copies, 1099 worksheets
with TINs, CPA packages with TINs and each file in one that holds a
TIN; a file is recorded before it is written, and a failure refuses.
import_local_file, attach_receipt and file_document (base64 was never
checked) refuse a recorded file, and a text file (by type, name, or
UTF-8/UTF-16 decoding) holding a full TIN. Not caught: an altered
binary copy; the docs say so (P2/P7 remove the path tools).
- The text check is narrow (owner/engineer decision, 2026-10-16: bank
exports are filed for reconciliation, and has_full_tin's "nine digits
however separated" refused their routing numbers, long references and
dates run into amounts). Text is refused when it holds (a) a TIN the
vault holds for a party, anywhere, as nine digits, 3-2-4 joined by
hyphens, spaces or dots, or 2-7 joined by a hyphen or space, compared
on the server against the decrypted vault and never returned or
logged (a vault that can't be opened leaves (b)); or (b) a stand-alone
printed SSN (123-45-6789) or EIN (12-3456789) shape: no digit, and no
hyphen or dot followed by a digit, beside it. Full-width digits and
Unicode hyphens and spaces read as ASCII. has_full_tin still guards
refusals and results (redact_refusal). Known limit, for P7: a refusal
under (a) tells the agent a text it chose holds a vault TIN.
New dependency: zeroize in ledger-mcp (already in the workspace
through ledger-vault, same major version), so the vault's TINs decoded
for the scan are wiped from memory on drop as the vault's own are.
- Mutation testing of the final P0 diff (docs/testing.md): tests for
each law a survivor broke (handbook titles, a dropped default root, a
.jpg W-9, the waker, the walk's folder flags against a link and a
pipe, the 40-link limit, save paths refused as such, the package
marking only its TIN files, a registry that can't be read, text types,
the short receipt rule's counts and a 12-character code). Simpler
code where only a race could tell: inspect places an existing path by
canonicalize itself (no second comparison), read_checked's read limit
is its one size check, openat fails on -1 as specified. The 25 MiB
upload test removes its 50 MB of books however it ends.
- The GUI's stale-rule prompt no longer says to open a pull request: it
asks the agent to find the figure from the rule's official authority,
confirm it with the human, pass it explicitly, and tell the human so it
can be reported upstream (as the handbook says); a lib test pins it and
bans files, git, GitHub and pull requests from every attention prompt.
- Import roots (local_files.rs INVARIANT 0): XDG_DOWNLOAD_DIR="$HOME/"
made $HOME (or /) the root. No root may be /, the home folder or hold
it, or hold the books, data dir, vault or a key: a named one stops the
server at startup, a default one is dropped; the default falls back to
~/Downloads only if that is a real folder other than $HOME; with no
root, an import says so.
- The read is a component walk from / with openat(O_NOFOLLOW |
O_DIRECTORY) per folder and O_NOFOLLOW | O_NONBLOCK for the file, so a
folder swapped for a link after the check fails; no step may land on a
protected inode (one renamed into a root); dev/ino and nlink stay. No
/proc. Portable to macOS. (libc's openat, two small unsafe blocks.)
- One refusal for missing, dangling and outside paths: resolved()
follows every link, dangling ones too, so a link's target existing or
not no longer changes the answer.
- Sealing: the key files (in force, default) and their targets always;
their folders only when not $HOME, not a root, not holding a root; and
~/.config/wanderbooks whatever XDG_CONFIG_HOME says.
- Agent text (I-12): RuleNotKnown names the authority (IRS, with its
address) and says to tell the human, never a repo path, PR or GitHub;
the digest's stale_rules drop the repo catalog and path for it; vault,
outside-books, STOPPED and local-file refusals name no git, path or
flag. tool_text_lint bans docs/, .md, /home, data dir, --data-dir,
--import-root, vault.key, .jsonl, sqlite, .gitignore, github, pull
request, and also lints every error template (scanned from source),
built error texts, and the server's refusal constants.
- The receipt rule told (ReceiptPolicy::rule) is the one enforced: the
exact threshold in the base currency (no "$", no rounding), required
and exempt accounts, reversals exempt; the connect text fits 1,400
characters by dropping reasons, then codes, never the rule.
- The connect text follows the parties setting; "offer books.digest"
everywhere. shown_name drops Unicode format characters (bidi, zero
width).
- read_only_tools_change_nothing compares file contents, the database
by every row, the vault and the settings file (raw database bytes did
not change either; rows are the stable comparison).
- import_local_file takes the name and type from the canonical path;
the digest's journal repair says when the tail was set aside, not the
journal's file name; "version-history messages" back in the TIN rule;
openWorldHint false on every tool, pinned in annotations_complete.
- Outside the MCP crate, for the same texts: ledger-core's DatedRule
gains `authority` (the statute or publication with its address) and
RuleNotKnown carries it; ledger-store's "sqlite error" reads "database
error"; ledger-config's ReceiptPolicy::rule takes the base currency.
New dev-dependency: rusqlite in ledger-mcp (already in the workspace,
same version and features as ledger-store), to read the database's
rows in read_only_tools_change_nothing.
- The filing scan is no TIN oracle (tin_files.rs INVARIANT 4): text
holding more than two TIN-shaped numbers (any rendering rule (a)
reads) that end in a known TIN's last four (the books' TIN and W-9
facts, via known_tin_last4, before the vault is opened; then the
vault's own) is refused with TIN_PROBE, the same words whether or not
one is the vault's TIN; only one or two candidates are compared
against the vault. Tests: unit
text_probing_for_a_tin_is_refused_alike_whether_or_not_it_holds_it,
one_or_two_candidates_are_compared_against_the_vault; over HTTP
text_probing_for_a_tin_is_refused_alike_by_every_filing_tool.
Plan: plan/served.md P0, section 6 "Immediate hole (P0)", 4.1, 4.3, 4.9.
Review round 3. S1: a panic stops the server for every session, so no
input may reach one. Dates are four-digit years (new ledger_core::
calendar, 0001-01-01..9999-12-31): the MCP's parse_date is strict
YYYY-MM-DD, periods are bounded, last_day_of_month returns a refusal
(server and store); schedules, assets, recurring entries, periods,
documents, disposals and entries refuse dates outside the calendar, and
replay refuses such journal dates. Every amount a command takes is at
most a line's (draft lines by size, document lines, payouts, payroll,
statements, shares, tax basis, proceeds, recurring templates), on
replay too, so sums are exact; the 1099 vendor part and the K-1
property share use checked products (TooLargeToCompute); a recurring
run makes at most 1,000 drafts. tests/hostile_args.rs drives every tool
with schema-derived hostile arguments and every resource with generated
URIs, asserting the server never stops; the audit's panics are pinned.
S2: a `.` or `..` path name is refused as NO_SUCH_FILE up front. S3:
NUL-interleaved UTF-16 without a byte-order mark is scanned both ways.
S4: resource-read refusals are TIN-redacted. S5: set_w9 records its W-9
only once the call passes every check, just before the vault write.
S8: idempotentHint only on verify, run_depreciation, run_schedules,
run_recurring, set_party_roles (a repeat leaves journal and vault
alone; spec_vault checks every candidate twice); every tool's hints
pinned in one table. C1: books.settings lists receipt_required_accounts
and receipt_exempt_accounts uncut. C2: the default company name reads
"a new company". C3: the parties rule names no-party reasons. C4/C5:
the handbook names the settings file only as books.settings' books_toml,
and restores "check books.digest and offer it". C6: an import root
inside the books, data dir or vault is refused at startup. The lint
catches `just` however it ends and where the books are;
digest_resource_bare_reads reads the server's month.
Review round 4 (2026-10-17): round 3's bounds ran in replay too, so
journals main wrote and reopened (an entry dated 0000-06-01 or
+10000-01-01, a draft line of 2e15, an asset ending past 9999, a basis
or share past the line limit, sub-cent document and payroll lines) no
longer opened. Posted is forever: the bounds are the commands' only.
- Replay applies main's rule exactly: parse_date takes any date chrono
reads; replay_add_line(_fx) has no size bound (add_line/add_line_fx
do); replay_register_asset/correct_asset (chrono overflow only),
replay_set_asset_tax_basis (check_journaled), replay_set_entry_parties
(check_shares_journaled), replay_register_recurring
(validate_recurring_journaled), replay_register_schedule (months 1 to
600), replay_create_period (order only), replay_dispose_asset, and no
line, date, payment or payroll range checks in replay_invoice/bill,
their payments and replay_payroll_run.
- Derived figures over such books refuse (TooLargeToCompute,
DateOutOfRange), never panic: amortization plans, depreciation into
chrono's last month (and the true-up for lives past 9999), intervals,
document totals and aging, net pay and payroll plans, money flows and
share resolution, the asset register, receipt-policy debits; the MCP
tools and the viewer show what can't be computed.
- tests/fixtures/old-books: the review's 13 journals, its control and 15
more edges, written by main's Store API with main's figures
(expected.json); every one opens in the writer and the viewer to those
figures, every derived figure computes or refuses, every MCP tool and
GUI page answers; hostile_args restarts the server after each
property; each bound has a command/replay test pair.
Final review: a schedule's plan refuses a total of 10^26 or more
(TooLargeToCompute) instead of letting Decimal round its cents silently,
and a null `remaining` in the schedule output is always explained by
plan_error. Replay is unaffected: no journaled schedule month can belong
to a total that large (the line limit bounds every posted month).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
This commit is contained in:
1 parent
9f9f00ed9a
commit
8693d5a3f5
161 files changed
+18938
-1110
No files matched your search
@@ -27,11 +27,20 @@ impl Mcp {
|
||||
}
|
||||
|
||||
fn spawn_with(data_dir: &std::path::Path, extra: &[&std::ffi::OsStr]) -> Self {
|
||||
Self::spawn_env(data_dir, extra, &[])
|
||||
}
|
||||
|
||||
fn spawn_env(
|
||||
data_dir: &std::path::Path,
|
||||
extra: &[&std::ffi::OsStr],
|
||||
env: &[(&str, &std::path::Path)],
|
||||
) -> Self {
|
||||
let bin = env!("CARGO_BIN_EXE_ledger-mcp");
|
||||
let mut child = Command::new(bin)
|
||||
.arg("--data-dir")
|
||||
.arg(data_dir)
|
||||
.args(extra)
|
||||
.envs(env.iter().map(|(k, v)| (*k, v.as_os_str())))
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
@@ -147,7 +156,7 @@ fn mcp_end_to_end_over_stdio() {
|
||||
// Tool listing: the narrow tools; chart is read-only.
|
||||
let list = mcp.request("tools/list", json!({}));
|
||||
let tools = list["result"]["tools"].as_array().unwrap();
|
||||
assert_eq!(tools.len(), 93, "expected 93 tools, got {}", tools.len());
|
||||
assert_eq!(tools.len(), 94, "expected 94 tools, got {}", tools.len());
|
||||
let chart = tools.iter().find(|t| t["name"] == "books.chart").unwrap();
|
||||
assert_eq!(chart["annotations"]["readOnlyHint"], true);
|
||||
assert!(
|
||||
@@ -745,6 +754,541 @@ fn mcp_resources_over_stdio() {
|
||||
assert!(resp.get("error").is_some());
|
||||
}
|
||||
|
||||
/// `--import-root` replaces the default import root (the downloads
|
||||
/// folder): a file in the named folder is filed; one beside it is not.
|
||||
#[test]
|
||||
fn import_root_flag_replaces_the_defaults() {
|
||||
let dir = test_dir("import-root");
|
||||
let inbox = dir.with_extension("inbox");
|
||||
std::fs::create_dir_all(&inbox).unwrap();
|
||||
let inside = inbox.join("receipt.txt");
|
||||
let temp = dir.with_extension("elsewhere.txt");
|
||||
for p in [&inside, &temp] {
|
||||
std::fs::write(p, b"a receipt").unwrap();
|
||||
}
|
||||
let mut mcp = Mcp::spawn_with(
|
||||
&dir.join("books-data"),
|
||||
&[std::ffi::OsStr::new("--import-root"), inbox.as_os_str()],
|
||||
);
|
||||
mcp.request(
|
||||
"initialize",
|
||||
json!({
|
||||
"protocolVersion": "2025-06-18",
|
||||
"capabilities": {},
|
||||
"clientInfo": {"name": "smoke-test", "version": "0.1.0"},
|
||||
}),
|
||||
);
|
||||
mcp.notify("notifications/initialized");
|
||||
let ok = mcp.call(
|
||||
"books.import_local_file",
|
||||
json!({"path": inside.to_str().unwrap(), "category": "receipt"}),
|
||||
);
|
||||
assert_eq!(ok["file_name"], "receipt.txt");
|
||||
let why = mcp.call_err(
|
||||
"books.import_local_file",
|
||||
json!({"path": temp.to_str().unwrap(), "category": "receipt"}),
|
||||
);
|
||||
assert!(
|
||||
why.contains("no such file in the server's import folders"),
|
||||
"{why}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Start a session: initialize, then initialized.
|
||||
fn init(mcp: &mut Mcp) -> Value {
|
||||
let init = mcp.request(
|
||||
"initialize",
|
||||
json!({
|
||||
"protocolVersion": "2025-06-18",
|
||||
"capabilities": {},
|
||||
"clientInfo": {"name": "smoke-test", "version": "0.1.0"},
|
||||
}),
|
||||
);
|
||||
mcp.notify("notifications/initialized");
|
||||
init
|
||||
}
|
||||
|
||||
/// Through the real tools, inside a root (local_files.rs INVARIANTS 1-3,
|
||||
/// Protected::of; S1 and S5 of the P0 review, I of round 2): the key
|
||||
/// file in force, moved into the root after the server started (a root
|
||||
/// holding the books or a key is refused at startup: startup.rs), is
|
||||
/// sealed with what its link leads to and their folders, but never the
|
||||
/// root itself; links in the root to the journal or a vault record lead
|
||||
/// outside it, and get the one refusal a missing file gets (INVARIANT 2).
|
||||
/// books.import_local_file and books.set_w9's w9_file_path agree.
|
||||
#[test]
|
||||
fn import_local_file_and_set_w9_refuse_the_key_vault_and_journal_inside_a_root() {
|
||||
use std::os::unix::fs::symlink;
|
||||
let dir = test_dir("sealed-in-root");
|
||||
let root = dir.join("root");
|
||||
let books = dir.join("books");
|
||||
let data = books.join("books-data");
|
||||
let tray = root.join("drop");
|
||||
let config = dir.join("config");
|
||||
for d in [
|
||||
&data,
|
||||
&tray,
|
||||
&root.join("alias"),
|
||||
&root.join("hidden"),
|
||||
&config.join("wanderbooks"),
|
||||
] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
std::fs::write(root.join("hidden/real.key"), "ab".repeat(32)).unwrap();
|
||||
std::fs::write(root.join("hidden/notes.pdf"), b"%PDF beside the key").unwrap();
|
||||
std::fs::write(root.join("loose.key"), "ef".repeat(32)).unwrap();
|
||||
symlink(root.join("hidden/real.key"), root.join("alias/vault.key")).unwrap();
|
||||
std::fs::write(config.join("wanderbooks/vault.key"), "cd".repeat(32)).unwrap();
|
||||
let settings = |key: &std::path::Path| {
|
||||
std::fs::write(
|
||||
books.join("wanderbooks.toml"),
|
||||
format!(
|
||||
"[company]\nname = \"Sealed Co\"\n[vault]\nkey_file = \"{}\"\n",
|
||||
key.display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
};
|
||||
// Started with the default key (outside the root), so it starts.
|
||||
settings(&config.join("wanderbooks/vault.key"));
|
||||
std::fs::write(tray.join("receipt.pdf"), b"%PDF a receipt").unwrap();
|
||||
std::fs::write(tray.join("w9.pdf"), b"%PDF\xff W-9 scan").unwrap();
|
||||
let root_arg = [std::ffi::OsStr::new("--import-root"), root.as_os_str()];
|
||||
let mut mcp = Mcp::spawn_env(&data, &root_arg, &[("XDG_CONFIG_HOME", &config)]);
|
||||
init(&mut mcp);
|
||||
mcp.call(
|
||||
"books.add_party",
|
||||
json!({"name": "Acme", "roles": ["contractor"]}),
|
||||
);
|
||||
mcp.call(
|
||||
"books.set_tin",
|
||||
json!({"party": "Acme", "tin": "291-12-2846", "tin_type": "ssn"}),
|
||||
);
|
||||
let journal = data.join(ledger_store::JOURNAL_FILE);
|
||||
let vault_record = books
|
||||
.join("vault")
|
||||
.read_dir()
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().path())
|
||||
.find(|p| p.extension().is_some_and(|e| e == "bin"))
|
||||
.unwrap();
|
||||
for (name, to) in [
|
||||
("journal-link.pdf", journal.clone()),
|
||||
("record-link.pdf", vault_record.clone()),
|
||||
("default-key-link.pdf", config.join("wanderbooks/vault.key")),
|
||||
("dangling-link.pdf", config.join("wanderbooks/missing.key")),
|
||||
] {
|
||||
symlink(&to, tray.join(name)).unwrap();
|
||||
}
|
||||
// The key in force moves into the root (a link, alias/vault.key ->
|
||||
// hidden/real.key); the settings reload.
|
||||
settings(&root.join("alias/vault.key"));
|
||||
let refuse = |mcp: &mut Mcp, path: &std::path::Path, why: &str| {
|
||||
let path = path.to_str().unwrap();
|
||||
let err = mcp.call_err(
|
||||
"books.import_local_file",
|
||||
json!({"path": path, "category": "document"}),
|
||||
);
|
||||
assert!(err.contains(why), "{path}: {err}");
|
||||
assert!(!err.contains(dir.to_str().unwrap()), "names a path: {err}");
|
||||
let err = mcp.call_err(
|
||||
"books.set_w9",
|
||||
json!({"party": "Acme", "received_on": "2026-01-03", "entity_type": "Individual",
|
||||
"tin": "291-12-2846", "tin_type": "ssn", "w9_file_path": path}),
|
||||
);
|
||||
assert!(err.contains(why), "{path}: {err}");
|
||||
};
|
||||
for p in [
|
||||
root.join("hidden/real.key"),
|
||||
root.join("hidden/notes.pdf"),
|
||||
root.join("alias/vault.key"),
|
||||
] {
|
||||
refuse(&mut mcp, &p, "belongs to the books or the TIN vault");
|
||||
}
|
||||
for p in [
|
||||
journal.clone(),
|
||||
vault_record,
|
||||
config.join("wanderbooks/vault.key"),
|
||||
tray.join("journal-link.pdf"),
|
||||
tray.join("record-link.pdf"),
|
||||
tray.join("default-key-link.pdf"),
|
||||
tray.join("dangling-link.pdf"),
|
||||
tray.join("missing.pdf"),
|
||||
] {
|
||||
refuse(
|
||||
&mut mcp,
|
||||
&p,
|
||||
"no such file in the server's import folders (by default your downloads folder)",
|
||||
);
|
||||
}
|
||||
// A key kept loose in the root itself seals the key, not the root.
|
||||
settings(&root.join("loose.key"));
|
||||
refuse(
|
||||
&mut mcp,
|
||||
&root.join("loose.key"),
|
||||
"belongs to the books or the TIN vault",
|
||||
);
|
||||
// Ordinary files in the root are filed (with the key the vault was
|
||||
// made with back in force).
|
||||
mcp.call(
|
||||
"books.import_local_file",
|
||||
json!({"path": tray.join("receipt.pdf").to_str().unwrap(), "category": "receipt"}),
|
||||
);
|
||||
// A link is filed under the name and type of the file it leads to.
|
||||
std::fs::write(tray.join("photo.png"), b"\x89PNG a photo").unwrap();
|
||||
symlink(tray.join("photo.png"), tray.join("named-like.pdf")).unwrap();
|
||||
let filed = mcp.call(
|
||||
"books.import_local_file",
|
||||
json!({"path": tray.join("named-like.pdf").to_str().unwrap(), "category": "receipt"}),
|
||||
);
|
||||
assert_eq!(filed["file_name"], "photo.png");
|
||||
let back = mcp.call(
|
||||
"books.get_receipt",
|
||||
json!({"attachment_id": filed["attachment_id"]}),
|
||||
);
|
||||
assert_eq!(back["mime_type"], "image/png", "{back}");
|
||||
settings(&config.join("wanderbooks/vault.key"));
|
||||
mcp.call(
|
||||
"books.set_w9",
|
||||
json!({"party": "Acme", "received_on": "2026-01-03", "entity_type": "Individual",
|
||||
"tin": "291-12-2846", "tin_type": "ssn",
|
||||
"w9_file_path": tray.join("w9.pdf").to_str().unwrap()}),
|
||||
);
|
||||
}
|
||||
|
||||
/// Outputs that carry full TINs (books.get_w9, books.vendor_1099 with
|
||||
/// include_tins, books.export_cpa_package with TINs) are never written
|
||||
/// into an import root, and a copy of one moved into a root is refused by
|
||||
/// its bytes: a TIN-bearing file can't be filed back in as a receipt and
|
||||
/// read out through books.get_receipt (S7 of the P0 review, 2026-10-16).
|
||||
#[test]
|
||||
fn tin_bearing_files_never_land_where_they_could_be_filed_back_in() {
|
||||
let dir = test_dir("tin-files");
|
||||
let root = dir.join("inbox");
|
||||
let out = dir.join("out");
|
||||
let config = dir.join("config");
|
||||
for d in [&root, &out, &config] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
std::fs::write(root.join("w9.pdf"), b"%PDF W-9 TIN 291-12-2846").unwrap();
|
||||
let root_arg = [std::ffi::OsStr::new("--import-root"), root.as_os_str()];
|
||||
let mut mcp = Mcp::spawn_env(
|
||||
&dir.join("books").join("books-data"),
|
||||
&root_arg,
|
||||
&[("XDG_CONFIG_HOME", &config)],
|
||||
);
|
||||
init(&mut mcp);
|
||||
mcp.call(
|
||||
"books.add_account",
|
||||
json!({"code": "1000", "name": "Cash", "account_type": "asset"}),
|
||||
);
|
||||
mcp.call(
|
||||
"books.add_party",
|
||||
json!({"name": "Acme", "roles": ["contractor"]}),
|
||||
);
|
||||
mcp.call(
|
||||
"books.set_w9",
|
||||
json!({"party": "Acme", "received_on": "2026-01-03", "entity_type": "Individual",
|
||||
"tin": "291-12-2846", "tin_type": "ssn",
|
||||
"w9_file_path": root.join("w9.pdf").to_str().unwrap()}),
|
||||
);
|
||||
let in_root = |name: &str| root.join(name).to_str().unwrap().to_string();
|
||||
for (tool, args) in [
|
||||
(
|
||||
"books.get_w9",
|
||||
json!({"party": "Acme", "save_to": in_root("copy.pdf")}),
|
||||
),
|
||||
(
|
||||
"books.vendor_1099",
|
||||
json!({"year": 2026, "include_tins": true, "save_csv_to": in_root("1099.csv")}),
|
||||
),
|
||||
(
|
||||
"books.export_cpa_package",
|
||||
json!({"cash_accounts": ["1000"], "save_to": in_root("cpa.zip")}),
|
||||
),
|
||||
(
|
||||
"books.export_cpa_package",
|
||||
json!({"cash_accounts": ["1000"], "save_to": root.to_str().unwrap()}),
|
||||
),
|
||||
] {
|
||||
let why = mcp.call_err(tool, args);
|
||||
assert!(
|
||||
why.contains("never saved in the server's import folders"),
|
||||
"{tool}: {why}"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
std::fs::read_dir(&root).unwrap().count(),
|
||||
1,
|
||||
"wrote into the root"
|
||||
);
|
||||
// Saved elsewhere, then moved into the root: refused by its bytes.
|
||||
let w9 = out.join("copy.pdf");
|
||||
mcp.call(
|
||||
"books.get_w9",
|
||||
json!({"party": "Acme", "save_to": w9.to_str().unwrap()}),
|
||||
);
|
||||
let csv = out.join("1099.csv");
|
||||
mcp.call(
|
||||
"books.vendor_1099",
|
||||
json!({"year": 2026, "include_tins": true, "save_csv_to": csv.to_str().unwrap()}),
|
||||
);
|
||||
for (from, name) in [(&w9, "receipt.pdf"), (&csv, "statement.csv")] {
|
||||
std::fs::copy(from, root.join(name)).unwrap();
|
||||
let why = mcp.call_err(
|
||||
"books.import_local_file",
|
||||
json!({"path": in_root(name), "category": "receipt"}),
|
||||
);
|
||||
assert!(why.contains("wrote with full TINs"), "{why}");
|
||||
}
|
||||
// Without TINs, the 1099 worksheet may go anywhere a file may.
|
||||
mcp.call(
|
||||
"books.vendor_1099",
|
||||
json!({"year": 2026, "save_csv_to": in_root("plain.csv")}),
|
||||
);
|
||||
}
|
||||
|
||||
/// The connect text and the receipt rule show a few account codes of each
|
||||
/// kind, shortened, and say books.settings lists them: it does, every one,
|
||||
/// uncut, so no account the rule names is out of the agent's reach.
|
||||
#[test]
|
||||
fn every_receipt_account_left_out_of_the_connect_text_is_in_books_settings() {
|
||||
let dir = test_dir("receipt-accounts");
|
||||
let long = "6000-A-CODE-LONGER-THAN-ANY-RULE-SHOWS-IT";
|
||||
let required: Vec<String> = std::iter::once(long.to_string())
|
||||
.chain((1..20).map(|i| format!("{}", 6000 + i)))
|
||||
.collect();
|
||||
let exempt: Vec<String> = (0..12).map(|i| format!("{}", 1100 + i)).collect();
|
||||
std::fs::write(
|
||||
dir.join("config.json"),
|
||||
json!({"receipt_policy": {"require_receipts_above": "0.00",
|
||||
"receipt_required_accounts": required, "receipt_exempt_accounts": exempt}})
|
||||
.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut mcp = Mcp::spawn(&dir);
|
||||
let init = mcp.request(
|
||||
"initialize",
|
||||
json!({
|
||||
"protocolVersion": "2025-06-18",
|
||||
"capabilities": {},
|
||||
"clientInfo": {"name": "smoke-test", "version": "0.1.0"},
|
||||
}),
|
||||
);
|
||||
mcp.notify("notifications/initialized");
|
||||
let connect = init["result"]["instructions"].as_str().unwrap().to_string();
|
||||
assert!(connect.contains("books.settings lists"), "{connect}");
|
||||
let settings = mcp.call("books.settings", json!({}));
|
||||
let rule = settings["receipt_rule"].as_str().unwrap();
|
||||
assert!(rule.contains("books.settings lists them"), "{rule}");
|
||||
let listed = |key: &str| -> Vec<String> {
|
||||
settings[key]
|
||||
.as_array()
|
||||
.unwrap_or_else(|| panic!("books.settings has no {key}: {settings}"))
|
||||
.iter()
|
||||
.map(|c| c.as_str().unwrap().to_string())
|
||||
.collect()
|
||||
};
|
||||
assert_eq!(listed("receipt_required_accounts"), required);
|
||||
assert_eq!(listed("receipt_exempt_accounts"), exempt);
|
||||
// Some codes really are left out of the connect text and the rule,
|
||||
// and the long one is shown cut there, whole here.
|
||||
let left_out: Vec<&String> = required
|
||||
.iter()
|
||||
.chain(&exempt)
|
||||
.filter(|c| !connect.contains(c.as_str()))
|
||||
.collect();
|
||||
assert!(left_out.len() > 20, "{connect}");
|
||||
assert!(!rule.contains(long), "{rule}");
|
||||
for code in left_out {
|
||||
assert!(
|
||||
listed("receipt_required_accounts").contains(code)
|
||||
|| listed("receipt_exempt_accounts").contains(code),
|
||||
"{code} is in neither the connect text nor books.settings"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A bare `books://digest` (no `since`) reads this month so far: an entry
|
||||
/// dated this month is in it, one dated last month is not (plan P0).
|
||||
/// "This month" is the server's: read from its own bare digest, never this
|
||||
/// process's clock, and the run starts over on fresh books if the month
|
||||
/// turns while it runs (midnight on the 1st).
|
||||
#[test]
|
||||
fn digest_resource_bare_reads() {
|
||||
for attempt in 0..3 {
|
||||
if digest_bare_reads_once(attempt) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
panic!("the month kept turning while the test ran");
|
||||
}
|
||||
|
||||
/// One run of `digest_resource_bare_reads`: false if the server's month
|
||||
/// turned during it (nothing is checked then).
|
||||
fn digest_bare_reads_once(attempt: u32) -> bool {
|
||||
let dir = test_dir(&format!("digest-bare-{attempt}"));
|
||||
let mut mcp = Mcp::spawn(&dir.join("books-data"));
|
||||
mcp.request(
|
||||
"initialize",
|
||||
json!({
|
||||
"protocolVersion": "2025-06-18",
|
||||
"capabilities": {},
|
||||
"clientInfo": {"name": "smoke-test", "version": "0.1.0"},
|
||||
}),
|
||||
);
|
||||
mcp.notify("notifications/initialized");
|
||||
let since = |mcp: &mut Mcp, uri: &str| -> Value {
|
||||
let read = mcp.request("resources/read", json!({"uri": uri}));
|
||||
let text = read["result"]["contents"][0]["text"].as_str().unwrap();
|
||||
serde_json::from_str(text).unwrap()
|
||||
};
|
||||
// The server's first day of this month.
|
||||
let first: chrono::NaiveDate = since(&mut mcp, "books://digest")["since"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
assert_eq!(chrono::Datelike::day(&first), 1, "{first}");
|
||||
let before = first.pred_opt().unwrap();
|
||||
for (code, name, typ) in [("1000", "Cash", "asset"), ("6000", "Fees", "expense")] {
|
||||
mcp.call(
|
||||
"books.add_account",
|
||||
json!({"code": code, "name": name, "account_type": typ}),
|
||||
);
|
||||
}
|
||||
for (date, what) in [(first, "this month"), (before, "last month")] {
|
||||
let id = mcp.call(
|
||||
"books.draft_entry",
|
||||
json!({"date": date.to_string(), "description": what}),
|
||||
)["entry_id"]
|
||||
.as_u64()
|
||||
.unwrap();
|
||||
for (side, code) in [("debit", "6000"), ("credit", "1000")] {
|
||||
mcp.call(
|
||||
"books.add_line",
|
||||
json!({"entry_id": id, "account": code, "side": side, "amount": "5.00"}),
|
||||
);
|
||||
}
|
||||
mcp.call(
|
||||
"books.post_entry",
|
||||
json!({"entry_id": id, "confirmed": true}),
|
||||
);
|
||||
}
|
||||
// Bare, and with an empty since=, alike (the default date itself is
|
||||
// pinned on every day of a month in server.rs's unit test).
|
||||
for uri in ["books://digest", "books://digest?since="] {
|
||||
let digest = since(&mut mcp, uri);
|
||||
if digest["since"] != first.to_string() {
|
||||
return false;
|
||||
}
|
||||
let seen: Vec<&str> = digest["entries"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|e| e["description"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(seen, ["this month"], "{uri}");
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// The handbook is one tool and one resource per section, with the
|
||||
/// receipt rule in force rendered in; the connect text points to it.
|
||||
#[test]
|
||||
fn handbook_tool_and_resources_over_stdio() {
|
||||
let dir = test_dir("handbook");
|
||||
let mut mcp = Mcp::spawn(&dir.join("books-data"));
|
||||
let init = mcp.request(
|
||||
"initialize",
|
||||
json!({
|
||||
"protocolVersion": "2025-06-18",
|
||||
"capabilities": {},
|
||||
"clientInfo": {"name": "smoke-test", "version": "0.1.0"},
|
||||
}),
|
||||
);
|
||||
let instructions = init["result"]["instructions"].as_str().unwrap();
|
||||
assert!(instructions.chars().count() <= 1400);
|
||||
// The first 512 characters (not bytes: a name may be multibyte).
|
||||
let lead: String = instructions.chars().take(512).collect();
|
||||
assert!(lead.contains("books.handbook"));
|
||||
mcp.notify("notifications/initialized");
|
||||
let index = mcp.call("books.handbook", json!({}));
|
||||
let sections = index["sections"].as_array().unwrap();
|
||||
assert_eq!(sections[0]["id"], "contract");
|
||||
assert!(index["read_first"].as_str().unwrap().contains("contract"));
|
||||
assert_eq!(index["section"], Value::Null);
|
||||
// The schema names every section, and "all".
|
||||
let list = mcp.request("tools/list", json!({}));
|
||||
let tool = list["result"]["tools"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|t| t["name"] == "books.handbook")
|
||||
.unwrap()
|
||||
.clone();
|
||||
let said = tool["inputSchema"]["properties"]["section"]["description"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
for s in sections {
|
||||
let id = s["id"].as_str().unwrap();
|
||||
assert!(said.contains(id), "the schema doesn't name {id}: {said}");
|
||||
}
|
||||
assert!(said.contains("\"all\""), "{said}");
|
||||
// Section "all": every section's text, in order, under its own title.
|
||||
let all = mcp.call("books.handbook", json!({"section": "all"}));
|
||||
assert_eq!(all["section"]["title"], "The whole handbook");
|
||||
let text = all["section"]["text"].as_str().unwrap();
|
||||
let mut at = 0;
|
||||
for s in sections {
|
||||
let one = mcp.call("books.handbook", json!({"section": s["id"]}));
|
||||
// A section is titled by its heading, as the index lists it.
|
||||
assert_eq!(one["section"]["title"], s["title"]);
|
||||
let part = one["section"]["text"].as_str().unwrap();
|
||||
assert_eq!(
|
||||
part.lines().next().unwrap(),
|
||||
format!("# {}", s["title"].as_str().unwrap())
|
||||
);
|
||||
at += text[at..].find(part).expect("a section missing from all") + part.len();
|
||||
}
|
||||
let one = mcp.call(
|
||||
"books.handbook",
|
||||
json!({"section": "evidence-and-receipts"}),
|
||||
);
|
||||
let text = one["section"]["text"].as_str().unwrap();
|
||||
assert!(text.contains("needs at least one receipt"), "{text}");
|
||||
assert!(!text.contains("{evidence_rule}"));
|
||||
let why = mcp.call_err("books.handbook", json!({"section": "nope"}));
|
||||
assert!(why.contains("contract"), "{why}");
|
||||
|
||||
let list = mcp.request("resources/list", json!({}));
|
||||
let uris: Vec<&str> = list["result"]["resources"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|r| r["uri"].as_str().unwrap())
|
||||
.collect();
|
||||
assert!(uris.contains(&"books://handbook"));
|
||||
for s in sections {
|
||||
let uri = s["uri"].as_str().unwrap();
|
||||
assert!(uris.contains(&uri), "{uri} not listed");
|
||||
let read = mcp.request("resources/read", json!({"uri": uri}));
|
||||
let c = &read["result"]["contents"][0];
|
||||
assert_eq!(c["mimeType"], "text/markdown");
|
||||
assert_eq!(
|
||||
c["text"].as_str().unwrap().chars().count() as u64,
|
||||
s["chars"].as_u64().unwrap()
|
||||
);
|
||||
}
|
||||
let read = mcp.request("resources/read", json!({"uri": "books://handbook"}));
|
||||
let idx: Value =
|
||||
serde_json::from_str(read["result"]["contents"][0]["text"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(idx["sections"].as_array().unwrap().len(), sections.len());
|
||||
let resp = mcp.request_raw("resources/read", json!({"uri": "books://handbook/nope"}));
|
||||
assert!(resp.get("error").is_some());
|
||||
}
|
||||
|
||||
fn base64_like(bytes: &[u8]) -> String {
|
||||
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
let mut out = String::new();
|
||||
@@ -1603,7 +2147,8 @@ fn mcp_vendor_1099_over_stdio() {
|
||||
for needle in [
|
||||
"only through 2026",
|
||||
"official source",
|
||||
"github.com/swaits/wanderbooks",
|
||||
"irs.gov",
|
||||
"tell the human",
|
||||
] {
|
||||
assert!(why.contains(needle), "missing {needle:?} in: {why}");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user