feat(sp27): unify 835 ingest + reconciliation in handle_835 (atomic)

Move 'reconcile.run(s, record.id)' inside CycloneStore.add()'s ingest
session, before s.commit(). The placeholder adjustment_amount set by
_remittance_835_row is overwritten by reconcile's CAS-aggregate pass
in the same transaction — readers never see a half-reconciled
Remittance row. If reconcile raises, the entire 835 ingest rolls back
via the session's __exit__.

The previous flow committed batch + remittance rows in session-1,
then opened session-2 to run reconcile.fail-soft. Two visible
problems closed: a race window where readers fetched the placeholder
adjustment_amount, and a half-reconciled state left visible if
reconcile crashed.

Deviation from the plan (N4 in autoreview): the reconcile call lives
in cycl_store.add() rather than handle_835 calling a new
reconcile.run_now(batch_id) helper after add(). Same end state, one
fewer module surface, handler stays a thin wrapper over the store.
This commit is contained in:
Nora
2026-06-29 12:08:47 -06:00
parent 44a6fb031a
commit d5f95b4f3c
6 changed files with 203 additions and 55 deletions
+14 -2
View File
@@ -330,8 +330,20 @@ def test_run_reversal_flips_paid_to_reversed(fixture_835):
assert reversal_match.prior_claim_state == ClaimState.PAID
def test_run_failed_reconcile_writes_activity_event(fixture_835):
"""If reconcile crashes, the batch + remittances stay; activity event records failure."""
def test_run_reconcile_raise_in_session_leaves_prior_commits_alone(fixture_835):
"""A ``reconcile.run`` raise inside an open session does not damage
previously-committed rows.
The test seeds a batch + remit, commits them in one session, then
calls ``reconcile.run`` in a fresh session with ``match`` monkey-
patched to raise. The pre-existing rows must survive — they're on
disk from the prior commit, separate from the rolling-back
session. This pins the unit-level invariant that ``reconcile.run``
itself never commits and never silently mutates rows outside the
session it was given; it is the *caller's* responsibility (now
``CycloneStore.add`` per SP27 Task 10) to control the transaction
boundary.
"""
with db.SessionLocal()() as s:
_make_batch(s)
_make_remit(s, "CLP-1", "PCN-A", "1", "100.00", "100.00")