feat(receipt-hunt): convert a foreign receipt instead of refusing to compare it (#1497)

* feat(receipt-hunt): compare a foreign receipt by converting it, not by refusing

A Swedish bank posts a converted figure for a purchase abroad while the
receipt states the original: Anthropic bills 180,00 EUR and the statement
reads -2 014,32 kr. Neither number appears in the other document, so the
matcher refused the pair rather than guess. On a SaaS-heavy ledger that is
not an edge case: of 25 receipts fetched from a real company's mailboxes,
14 were in USD or EUR and none could ever pair.

The receipt's total is now resolved into kronor with Riksbanken's rate for
its own date, and handed to the same matcher, which still wants the
merchant and the date to agree. The seam already existed: the scorer
passed null where a SEK value would go, with a comment explaining that
cross-currency pairs were deliberately incomparable. Surfaces that do not
resolve a rate still pass nothing and behave exactly as before.

Rates are fetched once per currency and day. Riksbanken answers 429 to a
caller that asks per document, and a run holds a dozen receipts from one
vendor in one month. A rate that cannot be resolved leaves the receipt
exactly as incomparable as it was.

Two calibration faults surfaced once the amounts became comparable:

A converted total is judged at 9% rather than 5%. Riksbanken publishes a
mid rate and a card issuer charges its own, so the two carry a known
spread on top of any disagreement about the sum: measured against real
statements, 1.2% to 3%. Holding both to one bar treats a rate spread as
if it were a discrepancy.

The date tolerance moves from 3 days to 10. A card settles days after the
purchase, an international one routinely a week later, and a forwarded
receipt carries the purchase date while the statement carries the posting.
At three days the signal scored zero for ordinary correct pairs and took a
quarter of the weight with it: a receipt agreeing to within 1%, from a
merchant the matcher recognised, still capped at 0.62. The 27 human-
confirmed pairs and the 7 near-misses that must not match all still hold.

Measured on that ledger: purchases with any candidate at all go 9 -> 13,
and the Anthropic pair proposes at 0.82 where it was previously
unscoreable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(receipt-hunt): an undated receipt is not converted at today's rate

Raised in review. A foreign receipt with no invoiceDate fell back to the
current date, which put a receipt of unknown age into amount matching on
the strength of a guess: a rate two years out is how something
incomparable acquires confidence it has not earned. No date, no
conversion, and the receipt stays exactly as it was.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-08-10 17:54:22 +02:00
committed by GitHub
co-authored by Claude Opus 5 Jakob Wennberg
parent 1d635b0d25
commit 4c479f84f4
6 changed files with 292 additions and 6 deletions
+18 -4
View File
@@ -23,6 +23,7 @@
*/
import type { SupabaseClient } from '@supabase/supabase-js'
import {
CONVERTED_AMOUNT_TOLERANCE_PERCENT,
amountVarianceForMatch,
calculateMatchConfidence,
calculateMerchantSimilarity,
@@ -84,6 +85,14 @@ interface ScorableItem {
document_id: string | null
extracted_data: InvoiceExtractionResult | null
channel_context: InboxChannelContext | null
/**
* The receipt's total in kronor, when a caller has resolved a rate for it.
*
* Left undefined by every surface that has not, which keeps the old
* behaviour exactly: a cross-currency pair stays incomparable rather than
* being scored on date and merchant alone.
*/
sek_total?: number | null
}
/** Pull the fields the matcher needs out of an extraction blob. */
@@ -126,10 +135,10 @@ export function scoreUnderlagCandidates(
const amountVariance = amountVarianceForMatch(
sig.total,
sig.currency,
// No stored SEK value on the inbox item, so cross-currency pairs are
// deliberately not comparable and the matcher drops the amount signal
// rather than matching 750 EUR to 750 SEK.
null,
// A SEK value only when someone resolved a rate for this receipt.
// Without one the pair stays incomparable, rather than matching 750 EUR
// to 750 SEK.
item.sek_total ?? null,
tx.amount,
txCurrency,
txSek,
@@ -150,10 +159,15 @@ export function scoreUnderlagCandidates(
: Number.POSITIVE_INFINITY
const similarity = sig.supplier ? calculateMerchantSimilarity(sig.supplier, txMerchant) : 0
// A converted total is judged against the wider bar, because the rate
// spread is a known error rather than a disagreement about the sum.
const converted = sig.currency !== txCurrency && item.sek_total != null
const { confidence, matchReasons } = calculateMatchConfidence(
dateVariance,
amountVariance,
similarity,
undefined,
converted ? CONVERTED_AMOUNT_TOLERANCE_PERCENT : undefined,
)
if (confidence < CANDIDATE_MIN_CONFIDENCE) continue