From 6f68835c3f2c0f2605f14d497db062074739f6c4 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 21 Nov 2026 16:13:06 -0700 Subject: [PATCH] fix(mcp,import): a reference class in the table, the refusal names the table's field, bank rules match the bank's words The tools' table gains a third class, reference, for fields that are identifiers by nature (18 strings: tags and serials, vendor and customer references, payment and send references, account codes, file names, an instrument's label, where a W-9 or W-4 is kept); free text falls from 127 to 109. The refusal names the table's own field and describes a key it does not know as an unrecognised field, never quoting a key the caller chose. A bank import matches its rules against the description as the bank wrote it and journals the masked one, so a rule keyed on a nine-digit reference still matches. A chart file's account code is a reference. Co-Authored-By: Claude Fable 5.2 --- crates/ledger-cli/tests/tin_in_text.rs | 79 +++++++ crates/ledger-import/src/bank_csv.rs | 24 ++ crates/ledger-import/src/chart_csv.rs | 29 +++ .../ledger-mcp/handbook/parties-and-tins.md | 2 +- crates/ledger-mcp/src/text_fields.rs | 216 +++++++++++++++--- crates/ledger-mcp/src/tools/documents.rs | 2 +- crates/ledger-mcp/src/tools/entries.rs | 11 +- 7 files changed, 324 insertions(+), 39 deletions(-) diff --git a/crates/ledger-cli/tests/tin_in_text.rs b/crates/ledger-cli/tests/tin_in_text.rs index 021c0473..bef43fd6 100644 --- a/crates/ledger-cli/tests/tin_in_text.rs +++ b/crates/ledger-cli/tests/tin_in_text.rs @@ -112,6 +112,85 @@ fn a_reference_is_no_tax_id() { assert!(out["entry_id"].is_u64(), "{out}"); } +/// A reference field (an account code, an instrument's label) keeps a bare +/// nine digits and refuses a printed tax id; free text refuses both. +#[test] +fn a_reference_field_keeps_nine_digits_and_refuses_a_printed_tax_id() { + let books = Books::new("tin-reference-field"); + let mut w = books.session("test"); + setup(&mut w); + let ok = answer_of(&w.call( + "books.add_account", + json!({"code": "123456789", "name": "Nine", "account_type": "asset"}), + )) + .unwrap(); + assert!(ok["account_id"].is_u64(), "{ok}"); + answer_of(&w.call( + "books.add_instrument", + json!({"label": "Card 000000000", "kind": "business_card", "account": "1000"}), + )) + .unwrap(); + let before = books.journal_raw(); + for (tool, args, field) in [ + ( + "books.add_account", + json!({"code": SSN, "name": "Odd", "account_type": "asset"}), + "the code of books.add_account", + ), + ( + "books.add_instrument", + json!({"label": format!("Card {EIN}"), "kind": "business_card", "account": "1000"}), + "the label of books.add_instrument", + ), + ( + "books.add_class", + json!({"name": "123456789"}), + "the name of books.add_class", + ), + ] { + let why = answer_of(&w.call(tool, args)).unwrap_err(); + assert!( + why.starts_with(&format!("{field} looks like it holds a tax id")), + "{tool}: {why}" + ); + assert_eq!(books.journal_raw(), before, "{tool}: nothing was written"); + } +} + +/// A rule keyed on a bank's nine-digit reference matches the bank's own +/// words, and the description that is kept is the masked one. +#[test] +fn a_bank_rule_keyed_on_a_nine_digit_reference_still_matches() { + let books = Books::new("tin-bank-rule"); + let mut w = books.session("test"); + setup(&mut w); + answer_of(&w.call( + "books.add_account", + json!({"code": "6100", "name": "Payroll costs", "account_type": "expense"}), + )) + .unwrap(); + let csv = "Date,Description,Amount\n2026-01-02,ACH 000000000 PAYROLL,-12.50\n2026-01-03,CHECK 1234567890,-3.25\n"; + let up = w.upload(csv.as_bytes(), "bank.csv", "text/csv", "import"); + let out = answer_of(&w.call( + "books.import_bank_csv", + json!({"upload_id": up, "bank_account_code": "1000", "default_offset_code": "6000", + "rules": {"000000000": "6100"}}), + )) + .unwrap(); + let imported = out["imported"].as_array().unwrap(); + assert_eq!(imported[0]["offset_account"], "6100", "{out}"); + assert_eq!(imported[1]["offset_account"], "6000", "{out}"); + assert_eq!( + imported[0]["description"], + "ACH \u{2022}\u{2022}\u{2022}\u{2022}\u{2022}0000 PAYROLL" + ); + let journal = String::from_utf8(books.journal_raw()).unwrap(); + assert!( + !journal.contains("ACH 000000000"), + "the journal holds the masked description only" + ); +} + /// A bank file is kept whole with a nine-digit reference masked to its last /// four digits; the answer says how many and where; the journal never holds /// the number. diff --git a/crates/ledger-import/src/bank_csv.rs b/crates/ledger-import/src/bank_csv.rs index 401618c1..3bfc77e7 100644 --- a/crates/ledger-import/src/bank_csv.rs +++ b/crates/ledger-import/src/bank_csv.rs @@ -104,6 +104,11 @@ pub struct MaskedBankImport { pub transactions: Vec, /// Which cells were masked, in file order. pub masked: Vec, + /// Each transaction's description exactly as the bank wrote it, in the + /// same order as `transactions`: for matching a categorizing rule + /// against (a rule keyed on a reference of nine digits still matches), + /// never to be kept or shown. + pub as_written: Vec, } /// [`parse_bank_csv_with`] for a file that is to be kept: a description a @@ -134,9 +139,11 @@ pub fn parse_bank_csv_masked( order: Option, ) -> Result { let mut masked = Vec::new(); + let mut as_written = Vec::new(); let transactions = parse_rows(text, order)? .into_iter() .map(|(line, mut txn)| { + as_written.push(txn.description.clone()); let numbers = ledger_core::settings::count_printed_tins(&txn.description); if numbers > 0 { txn.description = @@ -153,6 +160,7 @@ pub fn parse_bank_csv_masked( Ok(MaskedBankImport { transactions, masked, + as_written, }) } @@ -400,6 +408,22 @@ mod tests { // A file with none masks none. let clean = parse_bank_csv_masked("2026-01-02,Rent,-1.00\n", None).unwrap(); assert!(clean.masked.is_empty()); + assert_eq!(clean.as_written, ["Rent"]); + } + + /// `as_written` is each description as the bank wrote it, in order, + /// for matching a rule against: a rule keyed on a nine-digit reference + /// still finds it, while the kept description is masked. + #[test] + fn the_bank_s_own_words_are_kept_for_matching_rules() { + let csv = "2026-01-02,ACH 000000000 payroll,-1.00\n2026-01-03,Rent,-2.00\n"; + let got = parse_bank_csv_masked(csv, None).unwrap(); + assert_eq!(got.as_written, ["ACH 000000000 payroll", "Rent"]); + assert_eq!( + got.transactions[0].description, + "ACH \u{2022}\u{2022}\u{2022}\u{2022}\u{2022}0000 payroll" + ); + assert_eq!(got.transactions.len(), got.as_written.len()); } #[test] diff --git a/crates/ledger-import/src/chart_csv.rs b/crates/ledger-import/src/chart_csv.rs index 8c18b5d6..096e28c4 100644 --- a/crates/ledger-import/src/chart_csv.rs +++ b/crates/ledger-import/src/chart_csv.rs @@ -126,6 +126,11 @@ pub fn parse_chart_csv(text: &str) -> Result, ChartCsvError> { for (n, value) in fields.iter().enumerate() { let looks = if n == 1 { ledger_core::parties::has_full_tin(value) + } else if n == 0 || n == 3 { + // An account code (and its parent's) is a reference: a + // bare nine digits is a code, only the printed shapes + // read as a tax id (WB-partner-15). + ledger_core::settings::holds_printed_tin_in_reference(value) } else { ledger_core::settings::holds_printed_tin(value) }; @@ -177,6 +182,30 @@ mod tests { assert_eq!(rows[2].account_type, "revenue"); } + /// An account code is a reference: a bare nine digits is a code, a + /// printed SSN or EIN is refused, naming its row and column. + #[test] + fn a_code_may_be_nine_digits_but_not_a_printed_tax_id() { + let ok = + "code,name,type,parent_code\n123456789,Cash,asset,\n123456790,Bank,asset,123456789\n"; + assert_eq!(parse_chart_csv(ok).unwrap().len(), 2); + for csv in [ + "code,name,type,parent_code\n000-00-0000,Cash,asset,\n", + "code,name,type,parent_code\n1000,Cash,asset,\n1001,Bank,asset,00-0000000\n", + ] { + assert!( + matches!(parse_chart_csv(csv), Err(ChartCsvError::LooksLikeTin(_))), + "{csv}" + ); + } + // A name is a party-style field: any run of nine digits. + let name = "code,name,type,parent_code\n1000,Acct 123456789,asset,\n"; + assert!(matches!( + parse_chart_csv(name), + Err(ChartCsvError::LooksLikeTin(_)) + )); + } + #[test] fn parses_optional_tax_category_column() { let csv = "code,name,type,parent_code,tax_category\n4000,Sales,revenue,,gross_receipts\n1000,Cash,asset,,\n"; diff --git a/crates/ledger-mcp/handbook/parties-and-tins.md b/crates/ledger-mcp/handbook/parties-and-tins.md index c67bba34..46fd02e8 100644 --- a/crates/ledger-mcp/handbook/parties-and-tins.md +++ b/crates/ledger-mcp/handbook/parties-and-tins.md @@ -11,7 +11,7 @@ - Every post_entry preview says where the entry stands (party_policy, party_missing, party_suggestions): under the `[books] parties` setting "suggest" (the default), when party_missing, name the likely party to the human and tag it (or add it) before confirming; under "require", posting is refused (party_required) until the entry names a party or is marked as having none (books.set_entry_party with party null and no_party_reason, or draft_entry no_party_reason: only for an entry with truly no counterparty, like a transfer between the company's own accounts); under "off", don't raise it. - Record W-9 facts (books.set_w9) for contractors and landlords before paying them, and W-4 facts (books.set_w4) for employees before their first payroll; name the employee party on every books.run_payroll line. - Never put a full TIN or SSN anywhere in the books, only the last four digits: the engine refuses any nine-digit number in a party field. The full TIN goes only to the encrypted TIN vault, and every member's and every 1099 payee's belongs there: books.set_tin (party, tin, tin_type: ssn, ein, itin; any role, so each member's SSN for their K-1) and books.set_w9 with tin and tin_type for a payee's W-9, and the signed form with w9_upload_id (books.begin_upload purpose w9; never books.file_document or books.attach_receipt: the form shows the TIN). -- A full TIN lives in the vault: keep it out of descriptions, notes, file names and version-history messages. Every tool refuses a string that looks like a tax id (nine digits standing alone, or an SSN's 3-2-4 or an EIN's 2-7 grouping), whichever field it is in, before anything is written, and says which field; a party's own fields refuse any run of nine digits. An invoice number, a phone number or a reference of ten digits or more is no tax id. If a bank or vendor reference is nine digits, write its last four. books.get_w9 hands the signed form over (run its download command to save it for the human); books.purge_tin (preview, then confirmed, with a reason) deletes a TIN no longer needed. +- A full TIN lives in the vault: keep it out of descriptions, notes, file names and version-history messages. Every tool refuses a string that looks like a tax id (nine digits standing alone, or an SSN's 3-2-4 or an EIN's 2-7 grouping), whichever field it is in, before anything is written, and says which field; a party's own fields refuse any run of nine digits. A field that is an identifier by nature (an asset's tag or serial number, a vendor's or customer's reference, a payment's reference, an account code, a file name, an instrument's label, where a W-9 is kept) takes nine digits as a number and refuses only the grouped forms. An invoice number, a phone number, a coordinate or a reference of ten digits or more is no tax id. If a bank or vendor reference is nine digits and goes into a memo or a description, write its last four. books.get_w9 hands the signed form over (run its download command to save it for the human); books.purge_tin (preview, then confirmed, with a reason) deletes a TIN no longer needed. - books.parties lists the directory (each party's TIN last four from its TIN or W-9 facts), books.party shows one party with the year's totals, and books.show party opens it in the viewer. Archive (books.update_party archived) parties no longer dealt with; never delete history. ## Tagging an entry with several parties (books.set_entry_party) diff --git a/crates/ledger-mcp/src/text_fields.rs b/crates/ledger-mcp/src/text_fields.rs index 8cec79e6..5f2efc02 100644 --- a/crates/ledger-mcp/src/text_fields.rs +++ b/crates/ledger-mcp/src/text_fields.rs @@ -91,6 +91,12 @@ pub(crate) enum Why { pub(crate) enum Class { /// Free text: refused when it looks like a tax id. Free, + /// An identifier by nature (an asset tag, a serial number, an invoice or + /// bill number, a payment reference, an account code, a file name, a + /// label, where a paper is kept): refused only in a tax id's printed + /// shapes (an SSN's 3-2-4, an EIN's 2-7); a bare run of nine digits is + /// a legitimate number there (WB-partner-15). + Reference, /// A party's own field (or a name that becomes one): refused at any /// run of nine digits. Strict, @@ -99,6 +105,7 @@ pub(crate) enum Class { } const FREE: Class = Class::Free; +const REFERENCE: Class = Class::Reference; const STRICT: Class = Class::Strict; const VAULT: Class = Class::Exempt(Why::Vault); const LOOKUP: Class = Class::Exempt(Why::Lookup); @@ -119,7 +126,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ "books.add_account", &[ ("account_type", ENUM), - ("code", FREE), + ("code", REFERENCE), ("deductible_percent", AMOUNT), ("name", STRICT), ("tax_category", ENUM), @@ -132,7 +139,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ &[ ("account", LOOKUP), ("kind", ENUM), - ("label", FREE), + ("label", REFERENCE), ("last4", DIGITS), ("owner_party", LOOKUP), ], @@ -190,7 +197,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ "books.attach_receipt", &[ ("description", FREE), - ("file_name", FREE), + ("file_name", REFERENCE), ("link_to[].id", LOOKUP), ("link_to[].kind", ENUM), ("mime_type", FREE), @@ -223,7 +230,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ( "books.begin_upload", &[ - ("file_name", FREE), + ("file_name", REFERENCE), ("mime_type", FREE), ("purpose", ENUM), ("sha256", ID), @@ -260,7 +267,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ("terms", FREE), ("upload_id", ID), ("vendor", LOOKUP), - ("vendor_ref", FREE), + ("vendor_ref", REFERENCE), ], ), ( @@ -275,7 +282,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ("bill_to.phone", STRICT), ("bill_to.shipping_address", STRICT), ("customer", LOOKUP), - ("customer_ref", FREE), + ("customer_ref", REFERENCE), ("date", DATE), ("due_date", DATE), ("lines[].account", LOOKUP), @@ -362,7 +369,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ &[ ("category", ENUM), ("description", FREE), - ("file_name", FREE), + ("file_name", REFERENCE), ("link_to[].id", LOOKUP), ("link_to[].kind", ENUM), ("mime_type", FREE), @@ -386,8 +393,8 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ &[ ("asset", LOOKUP), ("description", FREE), - ("serial", FREE), - ("tag", FREE), + ("serial", REFERENCE), + ("tag", REFERENCE), ], ), ( @@ -403,7 +410,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ("note", FREE), ("on", DATE), ("recipients[]", STRICT), - ("reference", FREE), + ("reference", REFERENCE), ("via", FREE), ], ), @@ -422,7 +429,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ("instrument", LOOKUP), ("paid_from", LOOKUP), ("preview", ID), - ("reference", FREE), + ("reference", REFERENCE), ], ), ( @@ -461,7 +468,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ("instrument", LOOKUP), ("paid_from", LOOKUP), ("preview", ID), - ("reference", FREE), + ("reference", REFERENCE), ], ), ( @@ -534,8 +541,8 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ("purchase_date", DATE), ("salvage_value", AMOUNT), ("sec704c_method", ENUM), - ("serial", FREE), - ("tag", FREE), + ("serial", REFERENCE), + ("tag", REFERENCE), ("tax_basis_rule", ENUM), ], ), @@ -549,7 +556,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ), ( "books.renumber_account", - &[("account", LOOKUP), ("code", FREE)], + &[("account", LOOKUP), ("code", REFERENCE)], ), ( "books.resolve_item", @@ -695,7 +702,7 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ( "books.set_w4", &[ - ("kept_at", FREE), + ("kept_at", REFERENCE), ("party", LOOKUP), ("received_on", DATE), ("state", FREE), @@ -707,13 +714,13 @@ pub(crate) const TABLE: &[(&str, &[(&str, Class)])] = &[ ("entity_type", FREE), ("exempt_payee_code", FREE), ("fatca_code", FREE), - ("kept_at", FREE), + ("kept_at", REFERENCE), ("party", LOOKUP), ("received_on", DATE), ("tin", VAULT), ("tin_last4", DIGITS), ("tin_type", ENUM), - ("w9_file_name", FREE), + ("w9_file_name", REFERENCE), ("w9_upload_id", ID), ], ), @@ -805,6 +812,7 @@ fn class_of(tool: &str, path: &str) -> Class { fn looks_like_a_tax_id(class: Class, text: &str) -> bool { match class { Class::Free => ledger_core::settings::holds_printed_tin(text), + Class::Reference => ledger_core::settings::holds_printed_tin_in_reference(text), Class::Strict => ledger_core::parties::has_full_tin(text), Class::Exempt(_) => false, } @@ -825,11 +833,15 @@ pub(crate) fn refusal(tool: &str, args: Option<&Map>) -> Option String { + if self.unknown { + format!("an unrecognised field of {tool}") + } else { + format!("the {} of {tool}", self.shown) } } } @@ -869,10 +906,7 @@ fn walk(tool: &str, v: &Value, at: &Path, found: &mut Option) { match v { Value::String(s) => { if looks_like_a_tax_id(class_of(tool, &at.table), s) { - *found = Some(ledger_core::settings::tin_refusal(&format!( - "the {} of {tool}", - at.shown - ))); + *found = Some(ledger_core::settings::tin_refusal(&at.named(tool))); } } Value::Array(items) => { @@ -880,6 +914,7 @@ fn walk(tool: &str, v: &Value, at: &Path, found: &mut Option) { let child = Path { table: format!("{}[]", at.table), shown: format!("{}[{i}]", at.shown), + unknown: at.unknown, }; walk(tool, item, &child, found); } @@ -897,8 +932,8 @@ fn walk(tool: &str, v: &Value, at: &Path, found: &mut Option) { // A map's key is text too. if keyed && looks_like_a_tax_id(class_of(tool, &keys), k) { *found = Some(ledger_core::settings::tin_refusal(&format!( - "a key of the {} of {tool}", - at.shown + "a key of {}", + at.named(tool) ))); return; } @@ -1085,9 +1120,10 @@ mod tests { ("books.draft_entry", "description"), ("books.reverse_entry", "description"), ("books.create_invoice", "memo"), - ("books.create_invoice", "customer_ref"), ("books.create_invoice", "lines[].description"), - ("books.create_bill", "vendor_ref"), + ("books.create_bill", "duplicate_reason"), + ("books.pay_bill", "applications[].discount_reason"), + ("books.receive_payment", "applications[].discount_reason"), ("books.attach_receipt", "description"), ("books.record_decision", "decision_md"), ("books.set_convention", "body_md"), @@ -1109,6 +1145,29 @@ mod tests { ] { assert_eq!(class(tool, path), Class::Strict, "{tool} {path}"); } + // Identifiers by nature: a bare nine digits is a number there. + for (tool, path) in [ + ("books.add_account", "code"), + ("books.renumber_account", "code"), + ("books.add_instrument", "label"), + ("books.attach_receipt", "file_name"), + ("books.file_document", "file_name"), + ("books.begin_upload", "file_name"), + ("books.set_w9", "w9_file_name"), + ("books.set_w9", "kept_at"), + ("books.set_w4", "kept_at"), + ("books.create_bill", "vendor_ref"), + ("books.create_invoice", "customer_ref"), + ("books.pay_bill", "reference"), + ("books.receive_payment", "reference"), + ("books.mark_sent", "reference"), + ("books.register_asset", "tag"), + ("books.register_asset", "serial"), + ("books.label_asset", "tag"), + ("books.label_asset", "serial"), + ] { + assert_eq!(class(tool, path), Class::Reference, "{tool} {path}"); + } let vault: Vec<_> = TABLE .iter() .flat_map(|(t, rows)| rows.iter().map(move |(p, c)| (*t, *p, *c))) @@ -1206,6 +1265,97 @@ mod tests { ); } + /// A reference field (a tag, a serial, an invoice or payment reference, + /// a code, a file name) takes a bare nine digits and refuses the printed + /// shapes; a free text field refuses both (WB-partner-15). + #[test] + fn a_reference_takes_nine_digits_and_refuses_a_printed_tax_id() { + for (tool, field) in [ + ("books.register_asset", "serial"), + ("books.label_asset", "tag"), + ("books.create_bill", "vendor_ref"), + ("books.create_invoice", "customer_ref"), + ("books.pay_bill", "reference"), + ("books.receive_payment", "reference"), + ("books.mark_sent", "reference"), + ("books.add_account", "code"), + ("books.attach_receipt", "file_name"), + ("books.add_instrument", "label"), + ("books.set_w9", "kept_at"), + ] { + let call = |v: &str| refusal(tool, Some(&args(json!({ field: v })))); + assert_eq!(call("123456789"), None, "{tool} {field}"); + assert_eq!(call("SN 000000000.pdf"), None, "{tool} {field}"); + for shaped in ["000-00-0000", "ein 00-0000000", "000 00 0000"] { + let why = call(shaped).unwrap_or_else(|| panic!("{tool} {field} {shaped}")); + assert!( + why.starts_with(&format!( + "the {field} of {tool} looks like it holds a tax id" + )), + "{why}" + ); + } + } + // The same run in free text is refused. + assert!( + refusal( + "books.create_invoice", + Some(&args(json!({"memo": "123456789"}))) + ) + .is_some() + ); + assert!( + refusal( + "books.pay_bill", + Some(&args( + json!({"applications": [{"discount_reason": "123456789"}]}) + )) + ) + .is_some() + ); + } + + /// A refusal names the table's own field, never a key the caller chose + /// (an object's key is the caller's to pick, and could be the number). + #[test] + fn a_refusal_never_quotes_a_key_the_caller_chose() { + let key = "key-from-the-caller"; + // An unknown field, an unknown key deep under a known one, and one + // under a field the table takes as a map. + for (tool, a) in [ + ("books.add_class", json!({key: "000-00-0000"})), + ("books.add_class", json!({key: {"deeper": ["00-0000000"]}})), + ( + "books.create_invoice", + json!({"bill_to": {key: "000-00-0000"}}), + ), + ( + "books.create_invoice", + json!({"lines": [{"description": "ok"}, {key: "00-0000000"}]}), + ), + ("books.no_such_tool", json!({key: "000-00-0000"})), + ] { + let why = refusal(tool, Some(&args(a.clone()))).unwrap_or_else(|| panic!("{a}")); + assert!(!why.contains(key), "{tool}: {why}"); + assert!( + why.starts_with(&format!( + "an unrecognised field of {tool} looks like it holds a tax id" + )), + "{why}" + ); + } + // A known field is named as the table names it, with an item's index. + let why = refusal( + "books.create_invoice", + Some(&args(json!({"lines": [{"description": "00-0000000"}]}))), + ) + .unwrap(); + assert!( + why.starts_with("the lines[0].description of books.create_invoice"), + "{why}" + ); + } + /// A nested string is found with its index, whichever depth. #[test] fn a_nested_string_is_found_where_it_is() { diff --git a/crates/ledger-mcp/src/tools/documents.rs b/crates/ledger-mcp/src/tools/documents.rs index f31b9561..daa98659 100644 --- a/crates/ledger-mcp/src/tools/documents.rs +++ b/crates/ledger-mcp/src/tools/documents.rs @@ -474,7 +474,7 @@ impl BooksServer { "due_date, or terms, or the customer's default terms, or invoicing.default_terms", )?; if let Some(r) = &p.customer_ref { - ledger_core::parties::check_note("the customer reference", r)?; + ledger_core::parties::check_reference_note("the customer reference", r)?; } if let Some(m) = &p.memo { ledger_core::parties::check_note("the memo", m)?; diff --git a/crates/ledger-mcp/src/tools/entries.rs b/crates/ledger-mcp/src/tools/entries.rs index 3650e113..284ba9a3 100644 --- a/crates/ledger-mcp/src/tools/entries.rs +++ b/crates/ledger-mcp/src/tools/entries.rs @@ -738,6 +738,7 @@ impl BooksServer { let ledger_import::MaskedBankImport { transactions: txns, masked, + as_written, } = ledger_import::parse_bank_csv_masked(&csv, order).map_err(|e| e.to_string())?; let mut store = self.lock()?; let bank = store @@ -794,8 +795,10 @@ impl BooksServer { // can't become a draft (a date in a closed period, say) refuses the // file instead of leaving the ones before it behind. let mut scratch = store.ledger().clone(); - for txn in &txns { - let (offset_id, _) = offset_for(&txn.description); + for (txn, written) in txns.iter().zip(&as_written) { + // A rule reads the bank's own words, a nine-digit reference + // in them included; what is kept is the masked text. + let (offset_id, _) = offset_for(written); let amount = Money::new(txn.amount.abs(), base); let (bank_side, offset_side) = if txn.amount > Decimal::ZERO { (Side::Debit, Side::Credit) @@ -812,8 +815,8 @@ impl BooksServer { // Rehearsed whole: the upload is used up as the writing begins. files::filed(claim); let mut imported = Vec::new(); - for txn in txns { - let (offset_id, offset_code) = offset_for(&txn.description); + for (txn, written) in txns.into_iter().zip(&as_written) { + let (offset_id, offset_code) = offset_for(written); let amount = Money::new(txn.amount.abs(), store.ledger().base_currency()); let entry_id = store .create_entry(txn.date, &format!("Bank import — {}", txn.description))