fix(core,mcp,store): a phone number is kept and a ZIP+4 is kept; only a tax id's printed shapes are refused in a phone

A telephone is ten digits or more, so the strict reading of a party's fields
(any run of nine digits) refused every phone number: the bill-to snapshot's
check, new with the free-text change, refused `503-555-0100` and failed the
window's invoice test. The party's own phone (since D-55) and the tool's
bill-to phone had always been refused the same way; only 7-digit test
numbers hid it.

A phone field (a party's, a bill-to's, the seller's) is now its own class,
read as a reference is: refuse the printed SSN 3-2-4 and EIN 2-7 and nothing
else, since a nine-digit national number is ordinary. A postal address
keeps the strict reading except a ZIP+4 (`98101-1234`, five and four, nine
digits), a postal code. The formation state's entity number is a
registration number (a Washington UBI is nine digits) and is read as a
reference. The tools' table, the engine, and the audit's keys agree; the
audit reads `phone` and `fax` as a phone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 20:50:23 -07:00
1 parent 1dbca94bd2
commit c206652abe
12 files changed
+725 -52

No files matched your search

+44 -15
View File
@@ -1285,35 +1285,64 @@ pub fn check_document_text(
}
/// The party details a new invoice prints: the customer's (a party's
/// fields: strict, no run of nine digits, [`crate::parties::check_text`])
/// and the seller's (the company's own, free text: no tax id's shape).
/// fields: strict, no run of nine digits, [`crate::parties::check_text`];
/// an address allows a ZIP+4, [`crate::parties::check_address`]; a phone
/// is refused only in a tax id's printed shapes,
/// [`crate::parties::check_phone`]) and the seller's (the company's own,
/// free text: no tax id's shape; its phone as any phone).
pub fn check_snapshots(
bill_to: Option<&PartySnapshot>,
seller: Option<&PartySnapshot>,
) -> Result<(), LedgerError> {
let fields = |s: &'_ PartySnapshot| -> [(&'static str, Option<String>); 8] {
/// How a field of a snapshot is read.
#[derive(Clone, Copy, PartialEq)]
enum Kind {
Plain,
Address,
Phone,
}
let fields = |s: &'_ PartySnapshot| -> [(&'static str, Kind, Option<String>); 8] {
[
("the bill-to name", Some(s.name.clone())),
("the bill-to address", s.address.clone()),
("the bill-to email", s.email.clone()),
("the bill-to phone", s.phone.clone()),
("the bill-to contact", s.contact.clone()),
("the billing address", s.billing_address.clone()),
("the shipping address", s.shipping_address.clone()),
("the billing email", s.billing_email.clone()),
("the bill-to name", Kind::Plain, Some(s.name.clone())),
("the bill-to address", Kind::Address, s.address.clone()),
("the bill-to email", Kind::Plain, s.email.clone()),
("the bill-to phone", Kind::Phone, s.phone.clone()),
("the bill-to contact", Kind::Plain, s.contact.clone()),
(
"the billing address",
Kind::Address,
s.billing_address.clone(),
),
(
"the shipping address",
Kind::Address,
s.shipping_address.clone(),
),
("the billing email", Kind::Plain, s.billing_email.clone()),
]
};
if let Some(s) = bill_to {
for (field, text) in fields(s) {
for (field, kind, text) in fields(s) {
if let Some(t) = text {
crate::parties::check_text(field, &t).map_err(LedgerError::InvalidParty)?;
match kind {
Kind::Plain => crate::parties::check_text(field, &t),
Kind::Address => crate::parties::check_address(field, &t),
Kind::Phone => crate::parties::check_phone(field, &t),
}
.map_err(LedgerError::InvalidParty)?;
}
}
}
if let Some(s) = seller {
for (field, text) in fields(s) {
for (field, kind, text) in fields(s) {
if let Some(t) = text {
LedgerError::free_text(&field.replace("bill-to", "seller"), &t)?;
let field = field.replace("bill-to", "seller");
if kind == Kind::Phone {
crate::settings::check_phone_text(&field, &t)
.map_err(LedgerError::FreeTextHoldsTin)?;
} else {
LedgerError::free_text(&field, &t)?;
}
}
}
}
+17 -6
View File
@@ -5070,6 +5070,16 @@ impl Ledger {
Some(t) => crate::parties::check_text(f, t).map_err(LedgerError::InvalidParty),
None => Ok(()),
};
// A postal address allows a ZIP+4; a phone or fax number is refused
// only in a tax id's printed shapes (WB-partner-15).
let address = |f: &str, v: &Option<String>| match v {
Some(t) => crate::parties::check_address(f, t).map_err(LedgerError::InvalidParty),
None => Ok(()),
};
let phone = |f: &str, v: &Option<String>| match v {
Some(t) => crate::parties::check_phone(f, t).map_err(LedgerError::InvalidParty),
None => Ok(()),
};
match field {
Name(n) => {
if n.trim().is_empty() {
@@ -5087,7 +5097,7 @@ impl Ledger {
}
DisplayName(v) => text("display_name", v)?,
Website(v) => text("website", v)?,
Address(v) => text("address", v)?,
Address(v) => address("address", v)?,
Notes(v) => text("notes", v)?,
Archived(false) => {
if let Some(other) = self.party_by_name(&p.name)
@@ -5105,12 +5115,13 @@ impl Ledger {
crate::parties::check_email("email", e).map_err(LedgerError::InvalidParty)?;
}
}
// The D-55 contact block: two addresses and a phone like any
// other text (no full TIN), the billing email an address.
Phone(v) => text("phone", v)?,
// The D-55 contact block: two addresses (a ZIP+4 allowed) and a
// phone (refused only in a tax id's printed shapes), the billing
// email an address.
Phone(v) => phone("phone", v)?,
Contact(v) => text("contact", v)?,
BillingAddress(v) => text("billing_address", v)?,
ShippingAddress(v) => text("shipping_address", v)?,
BillingAddress(v) => address("billing_address", v)?,
ShippingAddress(v) => address("shipping_address", v)?,
BillingEmail(v) => {
if let Some(e) = v {
crate::parties::check_email("billing_email", e)
+44
View File
@@ -717,6 +717,50 @@ pub fn check_text(field: &str, text: &str) -> Result<(), String> {
Ok(())
}
/// [`has_full_tin`] for a postal address: the same strict reading, except
/// that a ZIP+4 (five digits, a hyphen, four digits, standing alone:
/// `98101-1234`) is a postal code, not a tax id, though it is nine digits.
pub fn has_full_tin_in_address(text: &str) -> bool {
let chars: Vec<char> = text.chars().collect();
let runs = if REPLAYING.with(std::cell::Cell::get) {
journaled_tin_runs(&chars)
} else {
full_tin_runs(&chars)
};
runs.iter().any(|&(first, last)| {
let run = &chars[first..=last];
let zip_plus_4 = run.len() == 10
&& run[5] == '-'
&& run
.iter()
.enumerate()
.all(|(i, c)| i == 5 || c.is_ascii_digit());
!zip_plus_4
})
}
/// [`check_text`] for a postal address: a ZIP+4 is allowed
/// ([`has_full_tin_in_address`]); every other run of nine digits is refused.
pub fn check_address(field: &str, text: &str) -> Result<(), String> {
if has_full_tin_in_address(text) {
return Err(format!(
"{field} looks like it holds a full taxpayer identification number; \
the books keep only its last four digits (the full number belongs in the vault)"
));
}
// The length and control-character bounds are the other text's.
check_note_form(field, text)
}
/// Check a telephone or fax number: not a tax id's printed shapes (an SSN's
/// 3-2-4, an EIN's 2-7: [`crate::settings::check_phone_text`]); every other
/// number is a phone's, whatever its digits or grouping. At most 500
/// characters, no control characters except newline.
pub fn check_phone(field: &str, text: &str) -> Result<(), String> {
crate::settings::check_phone_text(field, text)?;
check_note_form(field, text)
}
/// Check a free text field: no tax id's shape (a nine-digit number standing
/// alone, an SSN's 3-2-4, an EIN's 2-7: [`crate::settings::check_free_text`]),
/// at most 500 characters, no control characters except newline. For a
+31 -1
View File
@@ -759,6 +759,19 @@ pub fn holds_printed_tin_in_reference(text: &str) -> bool {
!printed_tin_spans(text, Reading::Reference).1.is_empty()
}
/// [`holds_printed_tin`] for a telephone or fax number: only the printed
/// SSN (3-2-4) and EIN (2-7) forms count. A phone number is ten digits or
/// more (a country code and an area code), and a national number of nine
/// is ordinary in many countries, so a run of nine or more digits proves
/// nothing there; `206-555-0142`, `(206) 555-0142`, `+1 206 555 0142`,
/// `+44 20 7946 0958` and an extension are all kept (WB-partner-15). It
/// reads as a reference does; it has its own name so that the fields that
/// hold a phone (a party's, a bill-to's, the tools' table, the audit's
/// keys) say so, and one change here moves them all.
pub fn holds_printed_tin_in_phone(text: &str) -> bool {
holds_printed_tin_in_reference(text)
}
/// [`holds_printed_tin`] for a web address: the printed SSN (3-2-4) and
/// EIN (2-7) forms count as there, but a bare run of nine digits joined
/// to a letter or to the URL punctuation `/ = ? & #` does not: it is an
@@ -1012,6 +1025,16 @@ pub fn check_reference_text(field: &str, text: &str) -> Result<(), String> {
Ok(())
}
/// A telephone or fax number that may be kept: not a tax id's printed
/// shapes ([`holds_printed_tin_in_phone`]). Returns why not, in
/// [`tin_refusal`]'s words. Never refuses during a replay (BS-entry-4).
pub fn check_phone_text(field: &str, text: &str) -> Result<(), String> {
if !crate::parties::is_replaying() && holds_printed_tin_in_phone(text) {
return Err(tin_refusal(field));
}
Ok(())
}
fn no_tin(what: &str, s: &str) -> Result<(), String> {
if holds_printed_tin(s) {
return Err(format!(
@@ -1287,7 +1310,14 @@ pub fn check(key: &Key, v: &Value) -> Result<Value, String> {
let name = key.name;
match key.kind {
Kind::Text => {
let text = line_text(name, want_str(name, v)?, MAX_TEXT)?;
// The state's entity number is a registration number (a
// Washington UBI is nine digits): a reference, refused only in
// a tax id's printed shapes (WB-partner-15).
let text = if name == "company.state_entity_number" {
reference_line_text(name, want_str(name, v)?, MAX_TEXT)?
} else {
line_text(name, want_str(name, v)?, MAX_TEXT)?
};
// One key's own bound: an invoice number's prefix is short and
// visible (a space or a control character would break the
// number's reading, and the file's).
+293
View File
@@ -0,0 +1,293 @@
//! A telephone or fax number is kept, and a tax id's printed shapes are not
//! (BS-partner-2, WB-partner-15; docs/spec/claims/partner.md, the CHOICE on
//! BS-partner-2 as amended 2026-11-21).
//!
//! Oracle: what a phone number is. It is ten digits or more with a country
//! and an area code, so the strict reading of a party's fields (any run of
//! nine digits) refused every phone number there was; a phone field
//! refuses only what reads as a Social Security number (3-2-4) or an EIN
//! (2-7). A postal address likewise keeps a ZIP+4, which is nine digits and
//! no tax id. The fake ids are `000-00-0000` and `00-0000000`; no real one
//! appears here.
use ledger_core::arap::{DocumentLine, InvoiceFields, PartySnapshot};
use ledger_core::parties::{check_address, check_phone, has_full_tin, has_full_tin_in_address};
use ledger_core::{Currency, Ledger, LedgerError, PartyField};
use proptest::prelude::*;
use rust_decimal::Decimal;
/// What a phone number looks like in the world.
const PHONES: &[&str] = &[
"2065550142",
"206-555-0142",
"206.555.0142",
"206 555 0142",
"(206) 555-0142",
"(206)555-0142",
"+1 206 555 0142",
"+1 (206) 555-0142",
"+1-206-555-0142",
"1-800-555-0142",
"+44 20 7946 0958",
"+44 (0)20 7946 0958",
"020 7946 0958",
"+49 30 12345678",
"+33 1 23 45 67 89",
"+81 3-1234-5678",
"+91 98765 43210",
"612345678",
"206-555-0142 x1234",
"206-555-0142 ext. 12",
"(206) 555-0142, ext 7",
"503-555-0100",
"555-0142",
"",
];
const SSN: &str = "000-00-0000";
const EIN: &str = "00-0000000";
#[test]
fn an_ordinary_phone_number_is_kept() {
for p in PHONES {
assert_eq!(check_phone("phone", p), Ok(()), "{p:?}");
}
}
#[test]
fn an_ssn_or_an_ein_in_a_phone_field_is_refused_in_words_that_never_say_the_number() {
for t in [
SSN,
EIN,
"000 00 0000",
"000.00.0000",
"ssn 000-00-0000",
"206-555-0142 / 000-00-0000",
"fax 00-0000000",
"\u{ff10}\u{ff10}\u{ff10}-\u{ff10}\u{ff10}-\u{ff10}\u{ff10}\u{ff10}\u{ff10}",
] {
let why = check_phone("the bill-to phone", t).unwrap_err();
assert!(
why.starts_with("the bill-to phone looks like it holds a tax id"),
"{t:?}: {why}"
);
assert!(!why.contains("0000"), "{why}");
}
}
#[test]
fn a_phone_field_keeps_a_phone_a_party_s_other_fields_would_refuse() {
// The strict reading of the other party fields still refuses these.
for p in ["206-555-0142", "+1 206 555 0142", "2065550142", "612345678"] {
assert!(has_full_tin(p), "{p}: strict");
assert!(check_phone("phone", p).is_ok(), "{p}: phone");
}
}
#[test]
fn a_zip_plus_4_is_kept_in_an_address_and_a_tax_id_is_not() {
for a in [
"98101-1234",
"400 Pine St\nSeattle, WA 98101-1234",
"400 Pine St, Seattle WA 98101-1234, USA",
"Suite 100, 84101",
"PO Box 12, Salt Lake City UT 84101-0012",
"98101-1234\n84047-1234",
] {
assert_eq!(check_address("address", a), Ok(()), "{a:?}");
}
for a in [
SSN,
EIN,
"400 Pine St 123456789",
"123 456 789",
"98101-12345", // six digits after: nine, then one more
"198101-1234", // six before
"98101-1234 5678", // a longer run
"98101 1234", // not a ZIP+4's hyphen
"400 Pine St 000-00-0000, Seattle",
] {
assert!(check_address("address", a).is_err(), "{a:?}");
assert!(has_full_tin_in_address(a), "{a:?}");
}
}
/// A phone field and an address on the party, through the engine.
#[test]
fn a_party_keeps_any_phone_and_a_zip_plus_4_and_stays_strict_elsewhere() {
let mut l = Ledger::new(Currency::USD);
let party = l.add_party("Harbor Coffee", &[]).unwrap();
for p in PHONES {
l.update_party(party, PartyField::Phone(Some((*p).to_string())))
.unwrap();
}
l.update_party(
party,
PartyField::BillingAddress(Some("1 Main St, Seattle WA 98101-1234".into())),
)
.unwrap();
l.update_party(
party,
PartyField::ShippingAddress(Some("2 Dock Rd, Seattle WA 98101-1234".into())),
)
.unwrap();
l.update_party(
party,
PartyField::Address(Some("3 Pier St, Seattle WA 98101-1234".into())),
)
.unwrap();
for bad in [SSN, EIN] {
let before = l.clone();
let r = l.update_party(party, PartyField::Phone(Some(bad.into())));
assert!(matches!(r, Err(LedgerError::InvalidParty(_))), "{r:?}");
assert_eq!(l, before, "a refusal changes nothing");
}
// Every other field of a party is still strict.
for f in [
PartyField::Contact(Some("Ana 123456789".into())),
PartyField::Notes(Some("tax id 123456789".into())),
PartyField::Address(Some("1 Main St 123456789".into())),
PartyField::Email(Some("123456789@example.com".into())),
] {
let r = l.update_party(party, f.clone());
assert!(
matches!(r, Err(LedgerError::InvalidParty(_))),
"{f:?}: {r:?}"
);
}
}
fn snap(phone: &str) -> PartySnapshot {
PartySnapshot {
name: "Harbor Coffee Roasters, Inc.".into(),
address: Some("455 Wharf Street\nPortland, OR 97209-1234".into()),
email: None,
phone: Some(phone.into()),
contact: None,
billing_address: None,
shipping_address: None,
billing_email: None,
}
}
fn invoice_with(
l: &mut Ledger,
bill_to: PartySnapshot,
seller: PartySnapshot,
) -> Result<(), LedgerError> {
let c = l.add_customer("Harbor Coffee").unwrap();
let d = chrono::NaiveDate::from_ymd_opt(2026, 3, 1).unwrap();
l.create_invoice_with(
c,
d,
d,
vec![DocumentLine {
description: "Consulting".into(),
amount: Decimal::from(10),
class_id: None,
account: None,
quantity: None,
unit_price: None,
}],
InvoiceFields {
bill_to: Some(bill_to),
seller: Some(seller),
..Default::default()
},
)
.map(|_| ())
}
/// The invoice's bill-to and seller snapshots, through the engine.
#[test]
fn an_invoice_prints_any_phone_and_refuses_a_tax_id_in_one() {
for p in PHONES.iter().filter(|p| !p.is_empty()) {
let mut l = Ledger::new(Currency::USD);
let r = invoice_with(&mut l, snap(p), snap(p));
assert!(r.is_ok(), "{p:?}: {r:?}");
}
for bad in [SSN, EIN] {
let mut l = Ledger::new(Currency::USD);
let r = invoice_with(&mut l, snap(bad), snap("206-555-0142"));
assert!(
matches!(r, Err(LedgerError::InvalidParty(_))),
"{bad}: {r:?}"
);
let mut l = Ledger::new(Currency::USD);
let r = invoice_with(&mut l, snap("206-555-0142"), snap(bad));
assert!(
matches!(r, Err(LedgerError::FreeTextHoldsTin(_))),
"{bad}: {r:?}"
);
}
}
proptest! {
/// Any phone built of a country code, an area code and a subscriber
/// number, in any of the usual groupings, is kept unless it is itself
/// an SSN's 3-2-4 or an EIN's 2-7 (which a phone's groups never are).
#[test]
fn a_phone_of_any_usual_grouping_is_kept(
cc in "[1-9][0-9]{0,2}",
area in "[2-9][0-9]{2}",
ex in "[2-9][0-9]{2}",
sub in "[0-9]{4}",
sep in prop::sample::select(vec!["-", " ", ".", ""]),
shape in 0usize..4,
) {
let p = match shape {
0 => format!("+{cc} {area}{sep}{ex}{sep}{sub}"),
1 => format!("({area}) {ex}{sep}{sub}"),
2 => format!("{area}{sep}{ex}{sep}{sub} x{cc}"),
_ => format!("+{cc}-{area}{ex}{sub}"),
};
prop_assert!(check_phone("phone", &p).is_ok(), "{p}");
}
/// An SSN's 3-2-4 and an EIN's 2-7, with any separator, are refused
/// in a phone field, alone or among words.
#[test]
fn a_printed_ssn_or_ein_is_refused_in_a_phone_field(
a in "[0-9]{3}", b in "[0-9]{2}", c in "[0-9]{4}",
e in "[0-9]{2}", f in "[0-9]{7}",
sep in prop::sample::select(vec!["-", " ", "\u{2013}", " - "]),
pre in prop::sample::select(vec!["", "ssn ", "(", "fax: "]),
post in prop::sample::select(vec!["", " x12", ")", " thanks"]),
) {
let ssn = format!("{pre}{a}{sep}{b}{sep}{c}{post}");
let ein = format!("{pre}{e}{sep}{f}{post}");
prop_assert!(check_phone("phone", &ssn).is_err(), "{ssn}");
prop_assert!(check_phone("phone", &ein).is_err(), "{ein}");
}
/// A ZIP+4 anywhere in an address is kept.
#[test]
fn a_zip_plus_4_is_kept_wherever_it_stands(
z in "[0-9]{5}", p in "[0-9]{4}",
pre in prop::sample::select(vec!["", "Seattle, WA ", "1 Main St\nSeattle WA ", "Unit 4, "]),
post in prop::sample::select(vec!["", ", USA", "\nUSA"]),
) {
let a = format!("{pre}{z}-{p}{post}");
prop_assert!(check_address("address", &a).is_ok(), "{a}");
}
}
/// The formation state's entity number is a registration number: a
/// Washington UBI is nine digits, so a bare nine digits is kept, and a
/// printed SSN or EIN is not (every other text setting is free text).
#[test]
fn the_state_entity_number_keeps_nine_digits_and_refuses_a_printed_tax_id() {
let key = ledger_core::settings::key("company.state_entity_number").unwrap();
for ok in ["603123456", "603 123 456", "14745117-0160", "5012345"] {
assert!(
ledger_core::settings::check(key, &serde_json::json!(ok)).is_ok(),
"{ok}"
);
}
for bad in [SSN, EIN] {
assert!(ledger_core::settings::check(key, &serde_json::json!(bad)).is_err());
}
// Another text setting stays free text: a bare nine digits is refused.
let agent = ledger_core::settings::key("company.registered_agent").unwrap();
assert!(ledger_core::settings::check(agent, &serde_json::json!("123456789")).is_err());
}