Skip to content

Land billing-core Phase 3: snapshot reconciliation - #3

Merged
FlyOverCoderKY merged 5 commits into
mainfrom
cursor/billing-core-phase-3-59bc
Aug 15, 2026
Merged

FlyOverCoderKY merged 5 commits into
mainfrom
cursor/billing-core-phase-3-59bc

Conversation

@FlyOverCoderKY

@FlyOverCoderKY FlyOverCoderKY commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

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:
    • observes the domain CAS token (eventAt, eventId) plus snapshotAt / snapshotGeneration
    • samples the freshness bound from the injected Clock before fetching provider truth
    • applies the snapshot through a decider that re-checks the observed watermark and generation against freshly read state
  • An intervening event write drops the snapshot (intervening-write)
  • A newer reconciliation on the same event token drops the slower fetch (superseded)
  • Every successful snapshot write increments snapshotGeneration so two same-bound overlapping sweeps cannot clobber each other's fields
  • snapshotAt never jumps into a later Unix second than the sampled bound, so same-second webhooks still get lifecycle-rank arbitration
  • Reservation-only / eventless rows are skipped so a founding webhook is not gated by an invented bound
  • On a token match the decider always writes — even when no field changed — so freshness advances and a delayed intermediate webhook cannot land after a no-op sweep
  • The watermark identity is never touched: the snapshot repairs field drift without disturbing event dedup
  • Time comes from the injected @pegma/spine Clock. Never Date.now() on a production path
  • Host-declared sticky / firstWins still re-evaluate inside the snapshot decider

Named tests run unchanged over createMemoryStore() and Azure Tables against real Azurite:

  • delayed-intermediate-webhook-after-sweep
  • intervening-write drops snapshot
  • no-op still advances snapshotAt
  • samples snapshotAt before fetch so a later cancel webhook can still apply
  • overlapping sweep does not clobber a newer snapshot
  • overlapping same-bound sweeps do not clobber fresher fields
  • overlapping .999Z reconcile does not stale a same-second webhook
  • does not snapshot a reservation-only row so a founding webhook can still apply

Review follow-up

07fb5c4 addresses the first three review findings: pre-fetch freshness bound, superseded overlapping sweeps, and eventless-row refusal.

d2fb788 made every successful snapshot write strictly advance snapshotAt.

b009977 stops that bump from crossing a Unix second. Same-bound CAS now uses snapshotGeneration; snapshotAt stays at max(current, sampled bound). Pinned by a .999Z overlapping reconcile plus a same-second canceled webhook.

Explicitly not in this PR

  • packages/billing-stripe (Phase 4)
  • RetireGolden host swap (Phase 5)

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.

Open in Web Open in Cursor 

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>
@chatgpt-codex-connector

Copy link
Copy Markdown

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>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread packages/billing-core/src/index.ts Resolved
Comment thread packages/billing-core/src/index.ts Resolved
Comment thread packages/billing-core/src/index.ts Resolved
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>
Comment thread packages/billing-core/src/index.ts Resolved
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>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread packages/billing-core/src/index.ts Resolved
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>
@FlyOverCoderKY
FlyOverCoderKY merged commit 4f9ef14 into main Aug 15, 2026
5 checks passed
@FlyOverCoderKY
FlyOverCoderKY deleted the cursor/billing-core-phase-3-59bc branch August 15, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants