fix(enable-banking): stop reading every ASPSP_ERROR as a too-wide window (#2202) (#2247)

* fix(enable-banking): stop reading every ASPSP_ERROR as a too-wide window (#2202)

ASPSP_ERROR is Enable Banking's generic wrapper for any upstream bank
failure, so "history window beyond the PSD2 limit" and "the bank is
refusing right now" arrived as the same string, and every rejection
walked the whole 90/60/30 narrowing ladder: one user click cost up to
five upstream calls against a bank that was already saying no, the
failure surfaced with "förnya anslutningen" advice that fixes nothing,
and a sync that did narrow was reported as complete.

What the account has accepted before is the signal that tells the two
apart. sync.ts now records the widest window (days before date_to) each
account's bank has answered, on accounts_data as accepted_history_days
(no migration; persisted by the same write-back as dedup_scope). On a
rejected window: no wider than that = the bank is unavailable, stop after
one call; wider = one retry straight at the accepted width, then stop.
Without a record (first sync, legacy rows) the ladder runs as before, but
its exhaustion is now AspspUnavailableError too. The web sync route maps
that to 503 BANK_UNAVAILABLE with copy that says the connection does not
need renewing and leaves the row alone; the agent path keeps the contract
code BANK_SYNC_FAILED but no longer persists renewal advice.

getAllTransactionsWithRaw returns the requested and the effective
date_from plus a narrowed flag; the sync result and the /sync response
carry them (history_from), and the settings toast says from which date
the history is complete when the bank cut the window.

Not done: a per-account backoff for the user-triggered route (the agent
path already has the 15-minute lease from #2165), and using the
envelope's `detail` field (one sample, identical to a width rejection).

Closes #2202

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VnConrmMCxJRQ5kfiPPWyy

* docs(decisions): carry the batch's decision lines (#2237, #2203, #2214) here

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-09-03 18:43:01 +02:00
committed by GitHub
co-authored by Claude Fable 5.1 Jakob Wennberg
parent 0c82db88da
commit 5eac2a492c
10 changed files with 547 additions and 25 deletions
@@ -1,5 +1,6 @@
import type { SupabaseClient } from '@supabase/supabase-js'
import { getAllTransactionsWithRaw, convertTransaction, getAccountBalance, SessionExpiredError } from './api-client'
import { historyWindowDays } from './history-window'
import { bankSyncResponseSchema, connectorErrorSchema } from '@accounted/connect-contract'
import { bankConnectorMode, CONNECTOR_COMPANY_HEADER } from '@/lib/connect/instance/upstreams'
import { uploadDocument } from '@/lib/core/documents/document-service'
@@ -46,6 +47,17 @@ export interface SyncResult {
returnedMinBookingDate?: string
/** Latest booking date the ASPSP returned. Undefined when no transactions came back. */
returnedMaxBookingDate?: string
/** The date_from asked of the bank. */
requestedFromDate: string
/**
* The date_from the bank actually answered. Equal to requestedFromDate
* unless the bank refused the window and a narrower one was used; the sync
* is then complete only from this date on (#2202). Undefined through the
* hosted connector, which does not report it.
*/
effectiveFromDate?: string
/** True when the bank refused the requested window and the history was cut. */
historyNarrowed: boolean
}
/** The subset of a converted bank transaction that the ingest mapping below reads. */
@@ -185,6 +197,8 @@ export async function syncAccountTransactions(
let rawPages: string[]
let bookedEntries: Array<{ tx: BookedTransactionFields; bookingDate: string }>
let totalFetched: number
let effectiveFromDate: string | undefined
let historyNarrowed = false
if (connector) {
const remote = await fetchBookedViaConnector(connector, {
supabase,
@@ -217,9 +231,31 @@ export async function syncAccountTransactions(
fromDate,
toDate,
syncOptions?.strategy,
{ acceptedHistoryDays: account.accepted_history_days },
)
rawPages = fetched.rawPages
totalFetched = fetched.transactions.length
effectiveFromDate = fetched.effectiveDateFrom
historyNarrowed = fetched.narrowed === true
// Remember the widest window this bank has ANSWERED for the account, so a
// later rejection of a window no wider than it is read as the bank being
// unavailable rather than as a too-wide window (#2202). Never shrinks:
// an incremental 7-day sync must not forget that 90 days once worked.
// Stamped on the account object; the caller's accounts_data write-back
// persists it, exactly like dedup_scope and the balance fields.
const acceptedDays = historyWindowDays(fetched.effectiveDateFrom, toDate)
if (acceptedDays !== undefined && acceptedDays > (account.accepted_history_days ?? -1)) {
account.accepted_history_days = acceptedDays
}
if (historyNarrowed) {
console.warn('[enable-banking] Bank refused the requested history window; synced a narrower one', {
connectionId,
accountUid: account.uid,
requestedFromDate: fromDate,
effectiveFromDate,
toDate,
})
}
const bankTransactions = fetched.transactions.map(tx => convertTransaction(tx, account.currency))
// Only ingest BOOKED transactions: those the ASPSP returned with a real
// booking_date. Pending entries are intentionally skipped: a pending row is
@@ -257,6 +293,7 @@ export async function syncAccountTransactions(
transactionCount: totalFetched,
rawPageCount: rawPages.length,
requestedFromDate: fromDate,
effectiveFromDate,
requestedToDate: toDate,
returnedMinBookingDate: minBookingDate,
returnedMaxBookingDate: maxBookingDate,
@@ -407,5 +444,8 @@ export async function syncAccountTransactions(
errors: ingestResult.errors,
returnedMinBookingDate: minBookingDate,
returnedMaxBookingDate: maxBookingDate,
requestedFromDate: fromDate,
effectiveFromDate,
historyNarrowed,
}
}