Fix RBC's last transaction absorbing the Account Fees footer line

RBC prints "Account Fees: $48.75" directly under "Closing balance" on
the statement's last page - not a money token this module recognizes
("$48.75", not "48.75"), no reference-number prefix, no page-number
pattern, so it fell through to plain narrative text and landed on the
last real transaction's description via the trailing-leftover-buffer
rule (correct behavior for a GENUINE trailing narrative line, wrong
for this one).

Fixed by treating the closing-balance line as a position-based
boundary, the same way the header row already is: once extract_page()
recognizes it, every subsequent line on that page is skipped outright
rather than reaching money/narrative classification at all. A no-op
for HSBC, which never emits a closing_balance row in the first place.

measure.py: RBC field accuracy 0.9977 -> 1.0, HSBC unaffected. RBC's
overall status is still "Failed" after this - a separate, pre-existing
engine_agreement disagreement on page 4, not touched by this fix.
This commit is contained in:
JasonFraser 2026-10-12 19:31:54 -04:00
parent 16da683551
commit da1b8b325e
2 changed files with 53 additions and 6 deletions

View File

@ -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

View File

@ -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: