diff --git a/CLAUDE.md b/CLAUDE.md index 148c181..e7582bb 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -428,6 +428,33 @@ dropped the label signal. Fixed by tracking `saw_label` as its own boolean, upda regardless of whether that row's own text is empty, independent of the `buffer` list (which only holds non-empty entries). +**RBC page-4 footer absorbed into the last transaction's description, fixed 2026-10-12.** +The permanent regression case the RBC fixture's ground truth was built to catch (see +"Outstanding before Phase 1" above): RBC prints `Account Fees: $48.75` directly under +"Closing balance" on the statement's last page - a real summary line, not transaction +data, but one `extract_page()` had no reason to recognize as anything special. It isn't +a money token by this module's own definition (`$48.75`, not `48.75` - the `$` fails +`_MONEY_RE`), has no reference-number prefix, and isn't a page-number footer, so it fell +through to plain narrative text; being the very last row on the statement with no +following transaction to lead into, `rows_to_transactions`'s own trailing-leftover-buffer +rule (correctly, by its own logic - nothing here was buggy in isolation) attached it to +the last real transaction above it. The fix treats the closing-balance line the same way +the header row is already treated: a position-based boundary, not a text pattern to +special-case. `extract_page()` now sets `after_closing_balance = True` the moment it +recognizes that line (same spot it already captures the figure for +`extract_statement_facts`) and skips every subsequent line on the page outright, before +it ever reaches the money/narrative classification that swept the footer in originally. +Confirmed safe for HSBC, which never emits a `closing_balance` row at all (its balance +prints outside the table, already excluded by the header-top preamble filter) - the flag +simply never flips there, so this is a pure no-op on that bank's own path. Verified via +`measure.py` (RBC's field accuracy went from 0.9977 to 1.0, HSBC unaffected) and a real +`/api/process` call against the running container (last transaction's description is now +just `"Misc Payment VSA FEE11112299"`, transaction count still 108). RBC's overall +`status` is still "Failed" after this fix - a separate, pre-existing, already-documented +`engine_agreement` disagreement on page 4 (see "Docling spike finding" and the column- +collision note in the Architecture section), not something this fix touched or was ever +meant to resolve. + **Balance-chain and date-ordering direction are auto-detected, not hardcoded per bank** — `checks.py` tries both forward and backward (and both newest-first and oldest-first) and keeps whichever has fewer violations, because RBC and HSBC don't share a convention and a @@ -718,12 +745,11 @@ restructure; the behavior itself predates it and is unchanged: `fixtures/RBC-CAD-Aug26_ground_truth.xlsx`, hand-verified by reading all 4 statement pages visually (via `pdftoppm`/poppler) against the extractor's own output — all 108 transactions, the opening balance ($66,857.70), and the closing balance ($90,569.13) - matched exactly. One real extraction bug was caught in the process and is now a - permanent regression case: the last transaction on page 4 absorbs the page's - trailing `Account Fees: $48.75` footer line into its own description (not yet - fixed in `table_extract.py` — ground truth encodes the correct value, so - `measure.py` reports this row's description as a mismatch every run until it's - fixed). `fixtures/HSBC-USD-Sep2026_ground_truth.xlsx` was built and verified the + matched exactly. One real extraction bug was caught in the process and became a + permanent regression case, since-fixed (2026-10-12, see below): the last + transaction on page 4 absorbed the page's trailing `Account Fees: $48.75` footer + line into its own description. `fixtures/HSBC-USD-Sep2026_ground_truth.xlsx` was + built and verified the same way (all 32 transactions visually confirmed page-by-page); HSBC's own opening/closing ground truth is `None`/`None`, matching `extract_statement_facts` — HSBC genuinely doesn't print these within the table diff --git a/backend/services/extraction/table_extract.py b/backend/services/extraction/table_extract.py index 5b78d8b..3748347 100644 --- a/backend/services/extraction/table_extract.py +++ b/backend/services/extraction/table_extract.py @@ -284,6 +284,7 @@ def extract_page(page, carry_zones=None, page_no=1): rows = [] last_date = "" + after_closing_balance = False for top in sorted(lines.keys()): # Anything at or above this page's own header row - an account- # summary preamble, "Opening/Closing balance" lines, repeated @@ -299,6 +300,25 @@ def extract_page(page, carry_zones=None, page_no=1): if found_zones is not None and top <= header_top: continue + # Anything BELOW the closing-balance line is trailing account + # boilerplate, not transaction data, the same way anything above + # the header row is leading boilerplate. Confirmed for real: RBC + # prints "Account Fees: $48.75" directly under "Closing balance" + # on the statement's last page - no money token this regex + # recognizes (it's "$48.75", not "48.75"), no reference-number + # prefix, so with nothing else to catch it, it fell through to + # plain narrative text and - since it's the very last row on the + # statement, with no following transaction to lead into - landed + # on the LAST real transaction's description via the same + # trailing-leftover-buffer rule a genuine trailing narrative line + # needs (see rows_to_transactions). The closing balance is always + # the true end of a bank's own transaction table (confirmed: it's + # printed once, at the end, same as "4 of 4" already marks the + # end of the page) - once seen, nothing after it on this page is + # worth classifying as either a transaction or narrative text. + if after_closing_balance: + continue + line_words = sorted(lines[top], key=lambda w: w["x0"]) leftmost = line_words[0] text_all = " ".join(w["text"] for w in line_words) @@ -347,6 +367,7 @@ def extract_page(page, carry_zones=None, page_no=1): continue if set(money.keys()) == {"balance"} and row_text_lower in layout_rules.CLOSING_BALANCE_PHRASES: rows.append({"type": "closing_balance", "balance": money["balance"]}) + after_closing_balance = True continue if money: