Repository navigation
Land billing-core Phase 3: snapshot reconciliation - #3
Conversation
Phase 3: observe the (eventAt, eventId) watermark, fetch provider truth afterward, and apply it through a decider that re-checks that domain token against fresh state. An intervening write drops the snapshot. A token match always writes — even when no field changed — so snapshotAt advances and a delayed intermediate webhook cannot land after a no-op sweep. Co-authored-by: Fly Over Coder <FlyOverCoder@gmail.com>
|
You have reached your Codex usage limits for security reviews. Please try again later. |
Narrow the reconcile union before reading written records, and type the observe-then-fetch store wrapper so tsc accepts the update decider. Co-authored-by: Fly Over Coder <FlyOverCoder@gmail.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5bffe464c2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Sample snapshotAt from the injected clock before fetchSnapshot so a cancel that lands during the fetch is not gated by a later bound. Re-check the observed snapshotAt so a slower stale fetch cannot clobber a newer reconciliation on the same event token. Refuse eventless and (null, null) rows so a reservation-only account is not given invented provider state or a freshness bound that would drop the founding webhook. Co-authored-by: Fly Over Coder <FlyOverCoder@gmail.com>
laterTimestamp left the bound unchanged when two reconciles sampled the same instant, so the superseded CAS still matched and a slower fetch could clobber fresher fields. Every snapshot write now persists a strictly later snapshotAt (incoming if newer, otherwise current + 1ms). Pinned by overlapping same-bound sweep tests. Co-authored-by: Fly Over Coder <FlyOverCoder@gmail.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit d2fb788. Configure here.
A 1ms snapshotAt bump from .999Z crossed into the next Unix second and raised effectiveWatermarkSecond, so same-second webhooks were dropped as stale instead of going through lifecycle rank. Snapshot writes now keep snapshotAt at max(current, sampled bound) and increment snapshotGeneration for the same-bound CAS. Pinned by a .999Z overlapping reconcile plus a same-second canceled webhook. Co-authored-by: Fly Over Coder <FlyOverCoder@gmail.com>

Phase 3 of Billing Core: snapshot reconciliation, in-tree and unpublished.
What landed
@pegma/billing-core(0.1.0, unpublished) now includes a periodic sweep that:(eventAt, eventId)plussnapshotAt/snapshotGenerationintervening-write)superseded)snapshotGenerationso two same-bound overlapping sweeps cannot clobber each other's fieldssnapshotAtnever jumps into a later Unix second than the sampled bound, so same-second webhooks still get lifecycle-rank arbitration@pegma/spineClock. NeverDate.now()on a production pathsticky/firstWinsstill re-evaluate inside the snapshot deciderNamed tests run unchanged over
createMemoryStore()and Azure Tables against real Azurite:delayed-intermediate-webhook-after-sweepsnapshotAtsnapshotAtbefore fetch so a later cancel webhook can still apply.999Zreconcile does not stale a same-second webhookReview follow-up
07fb5c4addresses the first three review findings: pre-fetch freshness bound, superseded overlapping sweeps, and eventless-row refusal.d2fb788made every successful snapshot write strictly advancesnapshotAt.b009977stops that bump from crossing a Unix second. Same-bound CAS now usessnapshotGeneration;snapshotAtstays atmax(current, sampled bound). Pinned by a.999Zoverlapping reconcile plus a same-second canceled webhook.Explicitly not in this PR
packages/billing-stripe(Phase 4)Plan status is Phase 3 in-tree, unpublished. Do not merge from this PR unless a reviewer asks. Do not publish or tag.
Local gate:
pnpm run format:check,pnpm run check,pnpm test— 129 tests passed.