New /layouts route (LayoutsView.vue, linked from AppHeader.vue): List (own + shared
mappings with usage/failure stats), Inspect (source document with each column drawn
as a labeled vertical strip), Test (an unsaved dry run against any of the user's own
documents), Version history + Roll back (append-only - a rollback creates a new
version rather than editing history), and Retire. The plan's own "Edit" (reopening
ManualMappingView pre-populated) was deliberately dropped - columnMap never persists
page/vertical position, so there's no real data to re-populate a visual editor with.
Drift detection now runs on every recheck, not just the original extraction -
useRowEditing.js and TransactionRow.vue both call the same recordUsage() useBatchExtraction.js
already used, via a newly shared layoutMappingsService.checksPassed(). Required
threading layoutMappingUsed/layoutMappingVersionUsed onto the in-memory document
object too (extractionStore/useDocuments/useBatchExtraction), not just the PocketBase
record.
Found and fixed two real bugs via this stage's own end-to-end testing, both in the
same "computed correctly this session, never persisted" category as L.6's zone-padding
fix: startPage itself was never saved, so every future auto-apply of a saved mapping
would have silently reset to page 1, reintroducing the cover-page bug L.2 already
fixed once. Worse: startPage alone only skips whole pages - it does nothing about the
real header row text still sitting on the start page itself, which would have
corrupted essentially every real manually-mapped statement. Fixed with two new
persisted fields (startPage, headerBottom) and a new header_bottom parameter in
table_extract.py, using the same zone-padding margin as a safety buffer.
Verified against the real rebuilt container: a 2-page synthetic statement confirmed
both bugs real before their fixes and confirmed both fixed together; a second,
independent upload auto-applies the saved mapping with both fields carried through;
sourceDocument backfill, version history/rollback, retire, and real three-user
promotion all confirmed against the real PocketBase; a separate test confirmed the
recheck-triggered drift detection itself, flipping a real mapping to stale after a
deliberately broken running-balance edit. measure.py byte-identical throughout; npm
run build passes.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
New /map-layout route (ManualMappingView.vue): draw a box over each column on the
source document, assign it a role, set date format/sign convention/ignored rows,
Preview (required before Save) runs the real forced-mapping extraction and shows
the first ten rows plus the balance check outcome, Save Mapping persists the
mapping + an initial version + the document. A real File can't survive a router
navigation, so a new layoutMapperStore holds the one pending file + fingerprint in
memory; ExtractorView.vue shows a banner linking to the mapper while one is set.
Two real bugs found by actually wiring this up and testing it end to end:
useBatchExtraction.js was reading fingerprintHash from the wrong (post-reassignment)
response, which never carries that field - would have silently left
documents.layoutFingerprintHash unset for every mapping-applied extraction. More
consequential: forced-mapping column zones were completely unpadded, so a word
landing a fraction of a point past a raw boundary (or a human's own imprecise drag)
could silently exclude a whole transaction with no error - fixed with a new
_FORCED_ZONE_PADDING in table_extract.py, the same discipline the auto-detected
path's own zones already use.
Verified end to end against a hand-built synthetic statement (unmatchable gibberish
headers, 3 real transaction rows at known coordinates) through the real rebuilt
container: Preview correctly extracts all 3 transactions with a passing balance
check; Save persists correctly (confirmed on the re-fetched document record, not
the create response); a second, independent upload of the identical file then
silently auto-applies the saved mapping with zero further input, producing
byte-identical results to the manual Preview - the actual proof this feature
exists for. measure.py byte-identical throughout; a regression re-run of the
earlier Equals Money forced-mapping test confirmed the zone-padding fix changed
nothing there. npm run build passes.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Lays the backend groundwork for the layout mapper (no UI/PocketBase yet): when both
the keyword detector and the layout_quirks fallback fail on every page,
extraction_service.run() now raises LayoutMappingRequired with a computed fingerprint
instead of a dead-end error; routes/extraction.py returns this as a 422 with
{needs_mapping, fingerprint, fingerprintHash}. run()/pipeline_service.run() gained a
forced_mapping parameter that extracts directly from given column positions, bypassing
auto-detection entirely - the entry point a future manual mapping screen will use.
Two real gaps surfaced by testing against the actual problem statement, not assumed:
header_candidate_geometry() scans every page for the best candidate rather than just
page 1 (this statement's own page 1 is a pure cover page); a forced mapping had no
page-boundary concept, so a cover page's own letterhead merged into the first real
transaction - fixed with a new start_page field.
Verified: a synthetic unrecognizable PDF correctly triggers the 422 contract through
the real running container; the real problem statement, forced through a hand-built
mapping, now extracts 36/36 transactions with every check passing, matching the
auto-detected L.1 result exactly; measure.py byte-identical on HSBC/RBC throughout.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
L.1 of the layout-mapper plan (docs at /Users/jason/.claude/plans/witty-inventing-walrus.md):
a new backend/services/extraction/layout_quirks.py runs as a zero-friction fallback after
the keyword/geometry header detector fails, fixing mechanical geometric oddities (here, a
header label wrapped across physical lines) without requiring a manual mapping step. Also
generalizes money-token recognition to accept a leading "$" and date recognition to accept
an unambiguous YYYY/MM/DD shape - both real gaps surfaced by the same new statement once its
header was fixed. Relocates table_extract.py's private header-matching helpers into
layout_rules.py as public functions to avoid a circular import with the new module.
Verified: measure.py byte-identical on HSBC/RBC (every change is a provable no-op there);
the real statement that prompted this now extracts 36/36 transactions through the live
container with statement_balance/running_balance/date_sanity/structure_sanity all passing,
every row spot-checked against the source PDF; the existing scanned-statement OCR fixture
re-verified with zero regression.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
R.1 (backend half) of the staged review-screen plan (Phase 2). Every
transaction now carries source_page/source_bbox - purely additive, so
the frontend's upcoming pdf.js viewer can box the exact printed row a
flagged transaction came from.
Traced extraction_service.py first: the canonical transactions list for
a normal text PDF always comes from pdfplumber (table_extract.py);
Docling's own extract_transactions() only feeds the cross-check
confidence signal, never the returned data. So this only needed to
touch table_extract.py and raw_ocr_extract.py (the OCR pixel-position
fallback), zero changes to engine-selection/cross-check logic.
One shared helper, table_extract._line_bbox(), used by both files
(mirrors how raw_ocr_extract.py already reuses _classify_money/_REF_RE
rather than re-implementing them) instead of two separate
implementations that could drift apart. A new _union_bbox() grows a
transaction's box as narrative lines merge into it, both the trailing
direction (HSBC) and leading direction (RBC).
Docling's own OCR-table-structure path doesn't get a real bbox (a
synthetic row-index, not a pixel position) - those transactions get
source_bbox: null, a graceful no-highlight rather than an error.
Verified: measure.py run before/after, byte-identical pass rate and
field accuracy on all three fixtures. A real /api/process call against
both HSBC and RBC fixtures confirmed every existing field unchanged.
Then the check measure.py can't make at all: rendered both fixtures'
pages for real and drew the reported source_bbox on the actual page
image - every box landed on exactly the right printed row, including
HSBC's trailing-narrative merge correctly widening the box.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Three small UI fixes from earlier testing, never committed:
- Pencil edit icon now uses the same blue accent token the Save button
already does (.row-btn.edit), instead of the default muted gray.
- Description textarea boxes left stale blank space after hiding the
Source Document panel - the drag-resizer already re-measures box
height on resize (autoGrowAll()), but the Hide/Show toggle never did,
so widening the transactions column left every description box at its
old height until something else happened to trigger a re-measure.
Added the missing call.
- Images shown in the Source Document panel had no zoom controls at all
(unlike PDFs, which get the browser's native viewer toolbar for free).
Added a dedicated zoomable/scrollable image panel with -/+/Fit
controls, width-based zoom (not CSS transform, so the panel's
overflow:auto actually produces real scrollbars when zoomed in),
opening at fit-width by default.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>