2026-04-18 22:10:23 +00:00
|
|
|
from datetime import date
|
|
|
|
|
from decimal import Decimal
|
v2: regex parser for Meta UK template + accurate RSU tax attribution
## Context
v1 shipped a Claude Haiku-based extractor that validated only 10/71
backfilled rows. Haiku fumbles the arithmetic on pension salary-sacrifice,
conflates RSU vest with regular earnings, and occasionally misreads YTD
vs this-period columns — so 86% of rows land with validated=false and the
downstream dashboards under-report take-home.
Meta UK uses a stable two-variant template (pre/post 2022-01-31 boundary),
so a regex parser is both faster (ms vs. 30-90s + $0.01-0.05/call) and
more accurate. v2 introduces that parser as the primary path, keeps
Claude as the fallback for non-Meta payslips, and surfaces new fields
the dashboard needs to attribute PAYE between cash salary and RSU vests
correctly.
## This change
### Parser (new)
`payslip_ingest/parsers/meta_uk.py` detects the layout variant by header
presence:
- **Variant A** (pre-2022): vertical Description/This Period/This Year.
`AE Pension EE` is a positive deduction against a pre-sacrifice gross —
maps to `pension_employee` for the existing validation formula to hold.
- **Variant B** (post-2022): side-by-side Payments | Deductions | Year to
Date. `AE Pension EE` is NEGATIVE in Payments (salary sacrifice) — maps
to `pension_sacrifice` and is already netted into Total Payment.
`rsu_vest = RSU Tax Offset + RSU Excs Refund` (Meta's template inflates
Taxable Pay without using a matching offset deduction).
Column boundaries come from the header row's anchor positions; each data
row slices into 3 cells and the last numeric token per cell is the amount.
Anchor misses raise ParserError so the caller falls back to Claude rather
than silently returning bad data.
### New fields
Schema + DB + Claude prompt gain:
- `salary`, `bonus`, `pension_sacrifice` — earnings decomposition for the
dashboard's bonus-sacrifice visibility and earnings-breakdown chart
- `taxable_pay`, `ytd_tax_paid`, `ytd_taxable_pay`, `ytd_gross` — powers
the YTD-effective-rate method of attributing cash tax vs RSU tax, which
is the only method that's accurate month-to-month
All new columns default to 0 / null so v1 rows continue to round-trip.
### Orchestration
processor.py tries `parse_meta_uk(pdftotext(pdf))` first. On success the
result goes straight to the DB — zero Claude tokens spent, extraction in
milliseconds. On ParserError it falls through to ClaudeExtractor as before.
ProcessResult gains an `extractor` field ("meta_uk_regex" | "claude") so
backfill logs show the hit rate.
## Tests
- `test_meta_uk_parser.py` — 11 tests covering variant A, variant B
(standard + bonus month + bonus-sacrificed month), malformed inputs,
and end-to-end totals validation for all 4 golden fixtures.
- `test_processor.py` — 2 new tests proving the regex-first short-circuit
and the Claude fallback on non-Meta inputs.
Fixtures under `tests/fixtures/` are hand-crafted `pdftotext -layout`
emulations — real Meta numbers from the plan's sample payslips for
variant B, synthesized realistic variant A and bonus-sacrificed samples.
0001_initial.py reformat is yapf cleanup touched during the session's
format pass; not a behavior change.
## Test Plan
### Automated
```
$ poetry run pytest
============================= test session starts ==============================
collected 53 items
tests/test_extractor.py ..... [ 9%]
tests/test_meta_uk_parser.py ........... [ 30%]
tests/test_paperless.py ...... [ 41%]
tests/test_processor.py .............. [ 67%]
tests/test_schema.py .... [ 75%]
tests/test_tax_year.py ........ [ 90%]
tests/test_webhook.py ..... [100%]
============================== 53 passed in 1.66s ==============================
$ poetry run ruff check .
All checks passed!
$ poetry run mypy .
Success: no issues found in 24 source files
$ poetry run yapf --style pyproject.toml --diff --recursive payslip_ingest tests
(no output — all files are yapf-clean)
```
### Manual Verification
Smoke-test the parser against a real Meta payslip PDF on the deploy host:
```
# After 0003 migration applied to prod DB
$ poetry run python -c "
from payslip_ingest.parsers import parse_meta_uk
import subprocess
text = subprocess.check_output(['pdftotext', '-layout', '/path/to/real.pdf', '-']).decode()
p = parse_meta_uk(text)
print(p.model_dump_json(indent=2))
"
```
Expected: JSON with salary/bonus/rsu_vest/pension_sacrifice populated and
`validate_totals(p)` returning True.
## Reproduce locally
1. `cd payslip-ingest && poetry install`
2. `poetry run pytest tests/test_meta_uk_parser.py -v`
3. Expected: 11 tests pass, each fixture validates totals within 2p.
Closes: code-un1
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 10:53:52 +00:00
|
|
|
from pathlib import Path
|
2026-04-18 22:10:23 +00:00
|
|
|
from typing import Any
|
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
v2: regex parser for Meta UK template + accurate RSU tax attribution
## Context
v1 shipped a Claude Haiku-based extractor that validated only 10/71
backfilled rows. Haiku fumbles the arithmetic on pension salary-sacrifice,
conflates RSU vest with regular earnings, and occasionally misreads YTD
vs this-period columns — so 86% of rows land with validated=false and the
downstream dashboards under-report take-home.
Meta UK uses a stable two-variant template (pre/post 2022-01-31 boundary),
so a regex parser is both faster (ms vs. 30-90s + $0.01-0.05/call) and
more accurate. v2 introduces that parser as the primary path, keeps
Claude as the fallback for non-Meta payslips, and surfaces new fields
the dashboard needs to attribute PAYE between cash salary and RSU vests
correctly.
## This change
### Parser (new)
`payslip_ingest/parsers/meta_uk.py` detects the layout variant by header
presence:
- **Variant A** (pre-2022): vertical Description/This Period/This Year.
`AE Pension EE` is a positive deduction against a pre-sacrifice gross —
maps to `pension_employee` for the existing validation formula to hold.
- **Variant B** (post-2022): side-by-side Payments | Deductions | Year to
Date. `AE Pension EE` is NEGATIVE in Payments (salary sacrifice) — maps
to `pension_sacrifice` and is already netted into Total Payment.
`rsu_vest = RSU Tax Offset + RSU Excs Refund` (Meta's template inflates
Taxable Pay without using a matching offset deduction).
Column boundaries come from the header row's anchor positions; each data
row slices into 3 cells and the last numeric token per cell is the amount.
Anchor misses raise ParserError so the caller falls back to Claude rather
than silently returning bad data.
### New fields
Schema + DB + Claude prompt gain:
- `salary`, `bonus`, `pension_sacrifice` — earnings decomposition for the
dashboard's bonus-sacrifice visibility and earnings-breakdown chart
- `taxable_pay`, `ytd_tax_paid`, `ytd_taxable_pay`, `ytd_gross` — powers
the YTD-effective-rate method of attributing cash tax vs RSU tax, which
is the only method that's accurate month-to-month
All new columns default to 0 / null so v1 rows continue to round-trip.
### Orchestration
processor.py tries `parse_meta_uk(pdftotext(pdf))` first. On success the
result goes straight to the DB — zero Claude tokens spent, extraction in
milliseconds. On ParserError it falls through to ClaudeExtractor as before.
ProcessResult gains an `extractor` field ("meta_uk_regex" | "claude") so
backfill logs show the hit rate.
## Tests
- `test_meta_uk_parser.py` — 11 tests covering variant A, variant B
(standard + bonus month + bonus-sacrificed month), malformed inputs,
and end-to-end totals validation for all 4 golden fixtures.
- `test_processor.py` — 2 new tests proving the regex-first short-circuit
and the Claude fallback on non-Meta inputs.
Fixtures under `tests/fixtures/` are hand-crafted `pdftotext -layout`
emulations — real Meta numbers from the plan's sample payslips for
variant B, synthesized realistic variant A and bonus-sacrificed samples.
0001_initial.py reformat is yapf cleanup touched during the session's
format pass; not a behavior change.
## Test Plan
### Automated
```
$ poetry run pytest
============================= test session starts ==============================
collected 53 items
tests/test_extractor.py ..... [ 9%]
tests/test_meta_uk_parser.py ........... [ 30%]
tests/test_paperless.py ...... [ 41%]
tests/test_processor.py .............. [ 67%]
tests/test_schema.py .... [ 75%]
tests/test_tax_year.py ........ [ 90%]
tests/test_webhook.py ..... [100%]
============================== 53 passed in 1.66s ==============================
$ poetry run ruff check .
All checks passed!
$ poetry run mypy .
Success: no issues found in 24 source files
$ poetry run yapf --style pyproject.toml --diff --recursive payslip_ingest tests
(no output — all files are yapf-clean)
```
### Manual Verification
Smoke-test the parser against a real Meta payslip PDF on the deploy host:
```
# After 0003 migration applied to prod DB
$ poetry run python -c "
from payslip_ingest.parsers import parse_meta_uk
import subprocess
text = subprocess.check_output(['pdftotext', '-layout', '/path/to/real.pdf', '-']).decode()
p = parse_meta_uk(text)
print(p.model_dump_json(indent=2))
"
```
Expected: JSON with salary/bonus/rsu_vest/pension_sacrifice populated and
`validate_totals(p)` returning True.
## Reproduce locally
1. `cd payslip-ingest && poetry install`
2. `poetry run pytest tests/test_meta_uk_parser.py -v`
3. Expected: 11 tests pass, each fixture validates totals within 2p.
Closes: code-un1
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 10:53:52 +00:00
|
|
|
from payslip_ingest import processor
|
2026-04-18 22:10:23 +00:00
|
|
|
from payslip_ingest.processor import process_document
|
|
|
|
|
from payslip_ingest.schema import ExtractedPayslip
|
|
|
|
|
|
v2: regex parser for Meta UK template + accurate RSU tax attribution
## Context
v1 shipped a Claude Haiku-based extractor that validated only 10/71
backfilled rows. Haiku fumbles the arithmetic on pension salary-sacrifice,
conflates RSU vest with regular earnings, and occasionally misreads YTD
vs this-period columns — so 86% of rows land with validated=false and the
downstream dashboards under-report take-home.
Meta UK uses a stable two-variant template (pre/post 2022-01-31 boundary),
so a regex parser is both faster (ms vs. 30-90s + $0.01-0.05/call) and
more accurate. v2 introduces that parser as the primary path, keeps
Claude as the fallback for non-Meta payslips, and surfaces new fields
the dashboard needs to attribute PAYE between cash salary and RSU vests
correctly.
## This change
### Parser (new)
`payslip_ingest/parsers/meta_uk.py` detects the layout variant by header
presence:
- **Variant A** (pre-2022): vertical Description/This Period/This Year.
`AE Pension EE` is a positive deduction against a pre-sacrifice gross —
maps to `pension_employee` for the existing validation formula to hold.
- **Variant B** (post-2022): side-by-side Payments | Deductions | Year to
Date. `AE Pension EE` is NEGATIVE in Payments (salary sacrifice) — maps
to `pension_sacrifice` and is already netted into Total Payment.
`rsu_vest = RSU Tax Offset + RSU Excs Refund` (Meta's template inflates
Taxable Pay without using a matching offset deduction).
Column boundaries come from the header row's anchor positions; each data
row slices into 3 cells and the last numeric token per cell is the amount.
Anchor misses raise ParserError so the caller falls back to Claude rather
than silently returning bad data.
### New fields
Schema + DB + Claude prompt gain:
- `salary`, `bonus`, `pension_sacrifice` — earnings decomposition for the
dashboard's bonus-sacrifice visibility and earnings-breakdown chart
- `taxable_pay`, `ytd_tax_paid`, `ytd_taxable_pay`, `ytd_gross` — powers
the YTD-effective-rate method of attributing cash tax vs RSU tax, which
is the only method that's accurate month-to-month
All new columns default to 0 / null so v1 rows continue to round-trip.
### Orchestration
processor.py tries `parse_meta_uk(pdftotext(pdf))` first. On success the
result goes straight to the DB — zero Claude tokens spent, extraction in
milliseconds. On ParserError it falls through to ClaudeExtractor as before.
ProcessResult gains an `extractor` field ("meta_uk_regex" | "claude") so
backfill logs show the hit rate.
## Tests
- `test_meta_uk_parser.py` — 11 tests covering variant A, variant B
(standard + bonus month + bonus-sacrificed month), malformed inputs,
and end-to-end totals validation for all 4 golden fixtures.
- `test_processor.py` — 2 new tests proving the regex-first short-circuit
and the Claude fallback on non-Meta inputs.
Fixtures under `tests/fixtures/` are hand-crafted `pdftotext -layout`
emulations — real Meta numbers from the plan's sample payslips for
variant B, synthesized realistic variant A and bonus-sacrificed samples.
0001_initial.py reformat is yapf cleanup touched during the session's
format pass; not a behavior change.
## Test Plan
### Automated
```
$ poetry run pytest
============================= test session starts ==============================
collected 53 items
tests/test_extractor.py ..... [ 9%]
tests/test_meta_uk_parser.py ........... [ 30%]
tests/test_paperless.py ...... [ 41%]
tests/test_processor.py .............. [ 67%]
tests/test_schema.py .... [ 75%]
tests/test_tax_year.py ........ [ 90%]
tests/test_webhook.py ..... [100%]
============================== 53 passed in 1.66s ==============================
$ poetry run ruff check .
All checks passed!
$ poetry run mypy .
Success: no issues found in 24 source files
$ poetry run yapf --style pyproject.toml --diff --recursive payslip_ingest tests
(no output — all files are yapf-clean)
```
### Manual Verification
Smoke-test the parser against a real Meta payslip PDF on the deploy host:
```
# After 0003 migration applied to prod DB
$ poetry run python -c "
from payslip_ingest.parsers import parse_meta_uk
import subprocess
text = subprocess.check_output(['pdftotext', '-layout', '/path/to/real.pdf', '-']).decode()
p = parse_meta_uk(text)
print(p.model_dump_json(indent=2))
"
```
Expected: JSON with salary/bonus/rsu_vest/pension_sacrifice populated and
`validate_totals(p)` returning True.
## Reproduce locally
1. `cd payslip-ingest && poetry install`
2. `poetry run pytest tests/test_meta_uk_parser.py -v`
3. Expected: 11 tests pass, each fixture validates totals within 2p.
Closes: code-un1
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 10:53:52 +00:00
|
|
|
FIXTURES = Path(__file__).parent / "fixtures"
|
|
|
|
|
|
2026-04-18 22:10:23 +00:00
|
|
|
|
|
|
|
|
def _sample_extraction() -> ExtractedPayslip:
|
|
|
|
|
return ExtractedPayslip(
|
|
|
|
|
pay_date=date(2026, 3, 28),
|
|
|
|
|
pay_period_start=date(2026, 3, 1),
|
|
|
|
|
pay_period_end=date(2026, 3, 31),
|
|
|
|
|
employer="Acme Ltd",
|
|
|
|
|
currency="GBP",
|
|
|
|
|
gross_pay=Decimal("5000.00"),
|
|
|
|
|
income_tax=Decimal("800.00"),
|
|
|
|
|
national_insurance=Decimal("350.00"),
|
|
|
|
|
pension_employee=Decimal("250.00"),
|
|
|
|
|
pension_employer=Decimal("150.00"),
|
|
|
|
|
student_loan=Decimal("100.00"),
|
2026-04-18 23:37:25 +00:00
|
|
|
rsu_vest=Decimal("0.00"),
|
|
|
|
|
rsu_offset=Decimal("0.00"),
|
2026-04-18 22:10:23 +00:00
|
|
|
other_deductions={"cycle_to_work": Decimal("50.00")},
|
|
|
|
|
net_pay=Decimal("3450.00"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _FakeSession:
|
|
|
|
|
"""Minimal AsyncSession stand-in that records flushes and execute calls."""
|
|
|
|
|
|
2026-04-19 15:33:07 +00:00
|
|
|
def __init__(self, existing_ids: list[int | None]):
|
2026-04-18 22:10:23 +00:00
|
|
|
self._existing_ids = existing_ids
|
|
|
|
|
self.added: list[Any] = []
|
|
|
|
|
self.begin_calls = 0
|
|
|
|
|
|
|
|
|
|
async def __aenter__(self) -> "_FakeSession":
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
async def __aexit__(self, *exc: object) -> None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def begin(self) -> "_FakeSession":
|
|
|
|
|
self.begin_calls += 1
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
async def execute(self, stmt: Any) -> Any:
|
|
|
|
|
result = MagicMock()
|
|
|
|
|
# scalar() returns None when we treat the row as missing.
|
|
|
|
|
result.scalar.return_value = self._existing_ids.pop(0) if self._existing_ids else None
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def add(self, row: Any) -> None:
|
|
|
|
|
row.id = 1
|
|
|
|
|
self.added.append(row)
|
|
|
|
|
|
|
|
|
|
async def flush(self) -> None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _SessionFactory:
|
|
|
|
|
|
|
|
|
|
def __init__(self, sessions: list[_FakeSession]):
|
|
|
|
|
self._sessions = list(sessions)
|
|
|
|
|
self.used: list[_FakeSession] = []
|
|
|
|
|
|
|
|
|
|
def __call__(self) -> _FakeSession:
|
|
|
|
|
session = self._sessions.pop(0)
|
|
|
|
|
self.used.append(session)
|
|
|
|
|
return session
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def paperless() -> AsyncMock:
|
|
|
|
|
mock = AsyncMock()
|
|
|
|
|
mock.get_document.return_value = {"id": 42, "title": "Payslip"}
|
|
|
|
|
mock.download_document.return_value = b"PDFDATA"
|
|
|
|
|
return mock
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def extractor() -> AsyncMock:
|
|
|
|
|
mock = AsyncMock()
|
|
|
|
|
mock.extract.return_value = _sample_extraction()
|
|
|
|
|
return mock
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_process_document_inserts_new(paperless: AsyncMock, extractor: AsyncMock) -> None:
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[]), _FakeSession(existing_ids=[])])
|
|
|
|
|
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert result.validated is True
|
|
|
|
|
paperless.get_document.assert_awaited_once_with(42)
|
|
|
|
|
paperless.download_document.assert_awaited_once_with(42)
|
|
|
|
|
extractor.extract.assert_awaited_once()
|
|
|
|
|
inserted_row = factory.used[1].added[0]
|
|
|
|
|
assert inserted_row.paperless_doc_id == 42
|
|
|
|
|
assert inserted_row.tax_year == "2025/26"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_process_document_skips_existing(paperless: AsyncMock, extractor: AsyncMock) -> None:
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[99])])
|
|
|
|
|
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
assert result.status == "skipped"
|
|
|
|
|
paperless.get_document.assert_not_called()
|
|
|
|
|
extractor.extract.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
processor: skip non-payslip docs by title pattern
The Paperless 'payslip' tag has been applied over the years to P60 annual
summaries, performance/year-end letters, Compensation_EMEA/PSC letters,
comp-review letters, and RSU grant agreements. These are legitimate
financial docs but not monthly payslips, and including them pollutes
the dashboards (a P60 amount is ~12x a single month).
Filter by title regex before hitting Claude so we skip cheaply and
don't burn extraction credit on them. Status returned is
'skipped_non_payslip' to distinguish from the 'already-ingested' skip.
Covers: P60*, *performance*(letter|year-end)*, compensation_emea,
*psc*, comp-letter, rsu grant*. New parameterized tests cover both
the exclude list and representative real payslip titles.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 23:32:17 +00:00
|
|
|
@pytest.mark.parametrize("title", [
|
|
|
|
|
"p60-meta-2025",
|
|
|
|
|
"20001_Tax_254680_P60_2021_To_2022",
|
|
|
|
|
"2024_Performance@_Year-end Letter_Viktor Barzin_1",
|
|
|
|
|
"254680_Viktor_Barzin_18 Compensation_EMEA_20230311_2022 YE PSC",
|
|
|
|
|
"2024-comp-letter",
|
|
|
|
|
"RSU Grant Agreement 2024",
|
|
|
|
|
])
|
|
|
|
|
async def test_process_document_skips_non_payslip_by_title(paperless: AsyncMock,
|
|
|
|
|
extractor: AsyncMock,
|
|
|
|
|
title: str) -> None:
|
|
|
|
|
paperless.get_document.return_value = {"id": 42, "title": title}
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[])])
|
|
|
|
|
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
assert result.status == "skipped_non_payslip"
|
|
|
|
|
paperless.download_document.assert_not_called()
|
|
|
|
|
extractor.extract.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("title", [
|
|
|
|
|
"Payslip_2026-02-27",
|
|
|
|
|
"20001_PY_254680_Jan_2022",
|
|
|
|
|
"UKPY_254680_31_Jul_2019",
|
|
|
|
|
])
|
|
|
|
|
async def test_process_document_keeps_real_payslips(paperless: AsyncMock, extractor: AsyncMock,
|
|
|
|
|
title: str) -> None:
|
|
|
|
|
paperless.get_document.return_value = {"id": 42, "title": title}
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[]), _FakeSession(existing_ids=[])])
|
|
|
|
|
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
extractor.extract.assert_awaited_once()
|
|
|
|
|
|
|
|
|
|
|
2026-04-18 22:10:23 +00:00
|
|
|
async def test_process_document_flags_validation_failure(paperless: AsyncMock,
|
|
|
|
|
extractor: AsyncMock) -> None:
|
|
|
|
|
bad = _sample_extraction()
|
|
|
|
|
bad_dict = bad.model_dump()
|
|
|
|
|
bad_dict["net_pay"] = Decimal("9999.00")
|
|
|
|
|
extractor.extract.return_value = ExtractedPayslip.model_validate(bad_dict)
|
|
|
|
|
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[]), _FakeSession(existing_ids=[])])
|
|
|
|
|
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert result.validated is False
|
|
|
|
|
assert factory.used[1].added[0].validated is False
|
v2: regex parser for Meta UK template + accurate RSU tax attribution
## Context
v1 shipped a Claude Haiku-based extractor that validated only 10/71
backfilled rows. Haiku fumbles the arithmetic on pension salary-sacrifice,
conflates RSU vest with regular earnings, and occasionally misreads YTD
vs this-period columns — so 86% of rows land with validated=false and the
downstream dashboards under-report take-home.
Meta UK uses a stable two-variant template (pre/post 2022-01-31 boundary),
so a regex parser is both faster (ms vs. 30-90s + $0.01-0.05/call) and
more accurate. v2 introduces that parser as the primary path, keeps
Claude as the fallback for non-Meta payslips, and surfaces new fields
the dashboard needs to attribute PAYE between cash salary and RSU vests
correctly.
## This change
### Parser (new)
`payslip_ingest/parsers/meta_uk.py` detects the layout variant by header
presence:
- **Variant A** (pre-2022): vertical Description/This Period/This Year.
`AE Pension EE` is a positive deduction against a pre-sacrifice gross —
maps to `pension_employee` for the existing validation formula to hold.
- **Variant B** (post-2022): side-by-side Payments | Deductions | Year to
Date. `AE Pension EE` is NEGATIVE in Payments (salary sacrifice) — maps
to `pension_sacrifice` and is already netted into Total Payment.
`rsu_vest = RSU Tax Offset + RSU Excs Refund` (Meta's template inflates
Taxable Pay without using a matching offset deduction).
Column boundaries come from the header row's anchor positions; each data
row slices into 3 cells and the last numeric token per cell is the amount.
Anchor misses raise ParserError so the caller falls back to Claude rather
than silently returning bad data.
### New fields
Schema + DB + Claude prompt gain:
- `salary`, `bonus`, `pension_sacrifice` — earnings decomposition for the
dashboard's bonus-sacrifice visibility and earnings-breakdown chart
- `taxable_pay`, `ytd_tax_paid`, `ytd_taxable_pay`, `ytd_gross` — powers
the YTD-effective-rate method of attributing cash tax vs RSU tax, which
is the only method that's accurate month-to-month
All new columns default to 0 / null so v1 rows continue to round-trip.
### Orchestration
processor.py tries `parse_meta_uk(pdftotext(pdf))` first. On success the
result goes straight to the DB — zero Claude tokens spent, extraction in
milliseconds. On ParserError it falls through to ClaudeExtractor as before.
ProcessResult gains an `extractor` field ("meta_uk_regex" | "claude") so
backfill logs show the hit rate.
## Tests
- `test_meta_uk_parser.py` — 11 tests covering variant A, variant B
(standard + bonus month + bonus-sacrificed month), malformed inputs,
and end-to-end totals validation for all 4 golden fixtures.
- `test_processor.py` — 2 new tests proving the regex-first short-circuit
and the Claude fallback on non-Meta inputs.
Fixtures under `tests/fixtures/` are hand-crafted `pdftotext -layout`
emulations — real Meta numbers from the plan's sample payslips for
variant B, synthesized realistic variant A and bonus-sacrificed samples.
0001_initial.py reformat is yapf cleanup touched during the session's
format pass; not a behavior change.
## Test Plan
### Automated
```
$ poetry run pytest
============================= test session starts ==============================
collected 53 items
tests/test_extractor.py ..... [ 9%]
tests/test_meta_uk_parser.py ........... [ 30%]
tests/test_paperless.py ...... [ 41%]
tests/test_processor.py .............. [ 67%]
tests/test_schema.py .... [ 75%]
tests/test_tax_year.py ........ [ 90%]
tests/test_webhook.py ..... [100%]
============================== 53 passed in 1.66s ==============================
$ poetry run ruff check .
All checks passed!
$ poetry run mypy .
Success: no issues found in 24 source files
$ poetry run yapf --style pyproject.toml --diff --recursive payslip_ingest tests
(no output — all files are yapf-clean)
```
### Manual Verification
Smoke-test the parser against a real Meta payslip PDF on the deploy host:
```
# After 0003 migration applied to prod DB
$ poetry run python -c "
from payslip_ingest.parsers import parse_meta_uk
import subprocess
text = subprocess.check_output(['pdftotext', '-layout', '/path/to/real.pdf', '-']).decode()
p = parse_meta_uk(text)
print(p.model_dump_json(indent=2))
"
```
Expected: JSON with salary/bonus/rsu_vest/pension_sacrifice populated and
`validate_totals(p)` returning True.
## Reproduce locally
1. `cd payslip-ingest && poetry install`
2. `poetry run pytest tests/test_meta_uk_parser.py -v`
3. Expected: 11 tests pass, each fixture validates totals within 2p.
Closes: code-un1
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 10:53:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_regex_parser_short_circuits_claude(paperless: AsyncMock, extractor: AsyncMock,
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
"""When pdftotext output matches the Meta template, Claude must not run."""
|
|
|
|
|
meta_text = (FIXTURES / "meta_uk_2026_02.txt").read_text(encoding="utf-8")
|
|
|
|
|
monkeypatch.setattr(processor, "_pdftotext", lambda _: meta_text)
|
|
|
|
|
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[]), _FakeSession(existing_ids=[])])
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert result.validated is True
|
|
|
|
|
assert result.extractor == "meta_uk_regex"
|
|
|
|
|
extractor.extract.assert_not_called()
|
|
|
|
|
# Salary / bonus / pension_sacrifice from the regex parser should land on the row.
|
|
|
|
|
row = factory.used[1].added[0]
|
|
|
|
|
assert row.salary == Decimal("10003.33")
|
|
|
|
|
assert row.pension_sacrifice == Decimal("600.20")
|
|
|
|
|
assert row.rsu_vest == Decimal("30479.76")
|
|
|
|
|
assert row.taxable_pay == Decimal("72096.92")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_regex_miss_falls_back_to_claude(paperless: AsyncMock, extractor: AsyncMock,
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
"""When pdftotext output doesn't match Meta, Claude is invoked."""
|
|
|
|
|
monkeypatch.setattr(processor, "_pdftotext", lambda _: "Some other employer's payslip\n")
|
|
|
|
|
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[]), _FakeSession(existing_ids=[])])
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert result.extractor == "claude"
|
|
|
|
|
extractor.extract.assert_awaited_once()
|
2026-04-19 12:00:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_rejects_implausible_pay_date(paperless: AsyncMock, extractor: AsyncMock) -> None:
|
|
|
|
|
"""Reject 1900-01-01-style hallucinations before they poison the DB."""
|
|
|
|
|
bad = _sample_extraction()
|
|
|
|
|
bad_dict = bad.model_dump()
|
|
|
|
|
bad_dict["pay_date"] = date(1900, 1, 1)
|
|
|
|
|
extractor.extract.return_value = ExtractedPayslip.model_validate(bad_dict)
|
|
|
|
|
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[])])
|
|
|
|
|
with pytest.raises(ValueError, match="implausible pay_date"):
|
|
|
|
|
await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_skips_p60_by_content_when_title_is_null(paperless: AsyncMock, extractor: AsyncMock,
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
"""P60s get the `payslip` tag sometimes, and some have no title in Paperless.
|
|
|
|
|
|
|
|
|
|
The title filter can't catch them, so we also check the pdftotext output
|
|
|
|
|
for the `P60 End of Year Certificate` signature before hitting the
|
|
|
|
|
extractor.
|
|
|
|
|
"""
|
|
|
|
|
paperless.get_document.return_value = {"id": 42, "title": None}
|
|
|
|
|
monkeypatch.setattr(processor, "_pdftotext",
|
|
|
|
|
lambda _: "P60 End of Year Certificate\nTax year to 5 April 2021\n")
|
|
|
|
|
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[])])
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
|
|
|
|
|
assert result.status == "skipped_non_payslip"
|
|
|
|
|
extractor.extract.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_rejects_zero_gross_zero_net(paperless: AsyncMock, extractor: AsyncMock) -> None:
|
|
|
|
|
"""Reject the other common hallucination: all zeros on a non-payslip."""
|
|
|
|
|
bad = _sample_extraction()
|
|
|
|
|
bad_dict = bad.model_dump()
|
|
|
|
|
bad_dict["gross_pay"] = Decimal("0")
|
|
|
|
|
bad_dict["net_pay"] = Decimal("0")
|
|
|
|
|
extractor.extract.return_value = ExtractedPayslip.model_validate(bad_dict)
|
|
|
|
|
|
|
|
|
|
factory = _SessionFactory([_FakeSession(existing_ids=[])])
|
|
|
|
|
with pytest.raises(ValueError, match="zero gross and net"):
|
|
|
|
|
await process_document(42, factory, paperless, extractor)
|
2026-04-19 15:23:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_p60_tag_routes_to_p60_handler(paperless: AsyncMock, extractor: AsyncMock,
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
"""A doc carrying the P60 tag id goes to _handle_p60 (not the payslip path)."""
|
|
|
|
|
p60_text = (FIXTURES / "meta_uk_p60_2024_25.txt").read_text(encoding="utf-8")
|
|
|
|
|
monkeypatch.setattr(processor, "_pdftotext", lambda _: p60_text)
|
|
|
|
|
paperless.get_document.return_value = {"id": 42, "title": "P60 2024-25", "tags": [7]}
|
|
|
|
|
|
|
|
|
|
# Two sessions: one for combined dedup, one for the P60 insert.
|
|
|
|
|
factory = _SessionFactory([
|
|
|
|
|
_FakeSession(existing_ids=[]),
|
|
|
|
|
_FakeSession(existing_ids=[]),
|
|
|
|
|
])
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor, p60_tag_id=7)
|
|
|
|
|
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert result.extractor == "p60_regex"
|
|
|
|
|
assert result.p60_id == 1
|
|
|
|
|
# Extractor (Claude) must not be called for a P60.
|
|
|
|
|
extractor.extract.assert_not_called()
|
|
|
|
|
inserted_row = factory.used[1].added[0]
|
|
|
|
|
assert inserted_row.tax_year == "2024/25"
|
|
|
|
|
assert inserted_row.gross_pay == Decimal("232630.34")
|
|
|
|
|
assert inserted_row.income_tax == Decimal("95820.11")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_p60_tag_absent_follows_payslip_path(paperless: AsyncMock, extractor: AsyncMock,
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
"""A regular payslip (no P60 tag) still goes through the payslip path."""
|
|
|
|
|
meta_text = (FIXTURES / "meta_uk_2026_02.txt").read_text(encoding="utf-8")
|
|
|
|
|
monkeypatch.setattr(processor, "_pdftotext", lambda _: meta_text)
|
|
|
|
|
paperless.get_document.return_value = {"id": 42, "title": "Payslip", "tags": [3]}
|
|
|
|
|
|
|
|
|
|
factory = _SessionFactory([
|
|
|
|
|
_FakeSession(existing_ids=[]),
|
|
|
|
|
_FakeSession(existing_ids=[]),
|
|
|
|
|
])
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor, p60_tag_id=7)
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert result.extractor == "meta_uk_regex"
|
|
|
|
|
assert result.p60_id is None
|
2026-04-19 15:33:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_bonus_dedup_zeros_repeat_within_tax_year(paperless: AsyncMock,
|
|
|
|
|
extractor: AsyncMock) -> None:
|
|
|
|
|
"""Same bonus amount already seen in tax_year → insert as 0.
|
|
|
|
|
|
|
|
|
|
Meta pays one perf bonus per tax year (2 historically). A duplicate
|
|
|
|
|
amount usually means the extractor misread the YTD figure as current
|
|
|
|
|
period — we keep the first occurrence and suppress subsequent matches.
|
|
|
|
|
"""
|
|
|
|
|
sample = _sample_extraction().model_dump()
|
|
|
|
|
sample["bonus"] = Decimal("25000.00")
|
|
|
|
|
extractor.extract.return_value = ExtractedPayslip.model_validate(sample)
|
|
|
|
|
|
|
|
|
|
# Insert session has 2 execute calls: paperless_doc_id dedup, bonus dedup.
|
|
|
|
|
# existing_ids=[None, 1] → paperless_doc_id not found; bonus already seen (id=1).
|
|
|
|
|
factory = _SessionFactory([
|
|
|
|
|
_FakeSession(existing_ids=[]),
|
|
|
|
|
_FakeSession(existing_ids=[None, 1]),
|
|
|
|
|
])
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert factory.used[1].added[0].bonus == Decimal("0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_bonus_dedup_keeps_first_occurrence(paperless: AsyncMock,
|
|
|
|
|
extractor: AsyncMock) -> None:
|
|
|
|
|
"""First occurrence of a bonus in a tax_year is preserved."""
|
|
|
|
|
sample = _sample_extraction().model_dump()
|
|
|
|
|
sample["bonus"] = Decimal("7777.77")
|
|
|
|
|
extractor.extract.return_value = ExtractedPayslip.model_validate(sample)
|
|
|
|
|
|
|
|
|
|
factory = _SessionFactory([
|
|
|
|
|
_FakeSession(existing_ids=[]),
|
|
|
|
|
_FakeSession(existing_ids=[None, None]),
|
|
|
|
|
])
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert factory.used[1].added[0].bonus == Decimal("7777.77")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_bonus_dedup_skips_when_bonus_zero(paperless: AsyncMock,
|
|
|
|
|
extractor: AsyncMock) -> None:
|
|
|
|
|
"""Bonus=0 rows skip the dedup query — avoids a pointless DB hit."""
|
|
|
|
|
sample = _sample_extraction().model_dump()
|
|
|
|
|
sample["bonus"] = Decimal("0")
|
|
|
|
|
extractor.extract.return_value = ExtractedPayslip.model_validate(sample)
|
|
|
|
|
|
|
|
|
|
# existing_ids only contains 1 entry — if dedup ran we'd exhaust it.
|
|
|
|
|
factory = _SessionFactory([
|
|
|
|
|
_FakeSession(existing_ids=[]),
|
|
|
|
|
_FakeSession(existing_ids=[None]),
|
|
|
|
|
])
|
|
|
|
|
result = await process_document(42, factory, paperless, extractor)
|
|
|
|
|
assert result.status == "inserted"
|
|
|
|
|
assert factory.used[1].added[0].bonus == Decimal("0")
|