Skip to content

fix: preserve monotonic offer redemption across event ordering - #7

Merged
FlyOverCoderKY merged 2 commits into
mainfrom
cursor/preserve-monotonic-offer-redemption
Nov 6, 2026
Merged

FlyOverCoderKY merged 2 commits into
mainfrom
cursor/preserve-monotonic-offer-redemption

Conversation

@FlyOverCoderKY

Copy link
Copy Markdown
Contributor

Summary

  • preserve offer redemption evidence from distinct stale or lower-ranked granting events and explicit redemption evidence
  • retain terminal lifecycle state, provider fields, reservations, host fields, and the lifecycle watermark during evidence-only writes
  • advance snapshot generation so overlapping reconciliation observes the write
  • keep exact event redelivery as a no-op
  • add shared memory and Azurite coverage for ordering, field preservation, reservations, reconciliation, and concurrent updates

Testing

  • CI=1 pnpm run format:check on Node 22 and 24
  • CI=1 pnpm run check on Node 22 and 24
  • CI=1 pnpm test on Node 22 and 24 — 172 tests passed with real Azurite

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

OpenRouter review posted (clean). #7 (review)

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-11-06T14:43:56.278305Z c49acb0 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions 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.

OpenRouter pull-request review

Verdict: issues
Scope: full-pr (full-pr)
Mode: initial
Commit: c49acb08af9b295966a8d1cdc0ee7a464dcf2d17
Judge: openai/gpt-5.6-luna
Cost: $0.2597 (lanes $0.2583 + judge $0.0013)

Lanes

  • meta/muse-spark-1.2: ok (0 finding(s), 73.2s, 251624+7023 tokens (150709 cached), 6 tool round(s), via Meta, $0.18)
  • z-ai/glm-5.3-flash: ok (3 finding(s), 632.1s, 1736545+50630 tokens (1654528 cached), 29 tool round(s), via InferenceNet, $0.0797)

Findings

🟠 Issue 1 — Stale redemption evidence is still dropped on non-terminal rows, so a redeemed one-per-customer offer can be reserved twice

packages/billing-core/src/index.ts:927 · risk · identified by z-ai/glm-5.3-flash

Concrete failure: event E2 (status: "incomplete", eventAt T2, no offer flag) applies onto a reservation-only or otherwise non-terminal row; the redemption event E1 (eventAt T1 < T2, offerRedeemed: true, or a granting status) arrives afterwards. decideLedgerApplication takes the incoming < watermark branch, recordsOfferRedemption returns false because current.status is incomplete rather than canceled/unpaid, and the row keeps offerRedeemed: false. Once the earlier reservation expires or is released, decideReservation (which refuses only on offerRedeemed, subscribed, or a live reservation) mints a fresh reservation, and the host lets the customer redeem the one-per-customer offer a second time — the exact revenue-leak failure mode this package exists to prevent. The equal-rank flavor loses the same way: a same-second, equal-rank evidence event arriving later is dropped without recording.

🔵 Issue 2 — README states offer-redemption monotonicity unconditionally; the implementation only guarantees it for terminal rows

packages/billing-core/README.md:74 · nit · identified by z-ai/glm-5.3-flash

packages/billing-core/README.md line 74 opens with "Offer redemption is monotonic across delivery order." as an unconditional property, but the very next sentences scope the mechanism to "a terminal row". For a non-terminal row (e.g. incomplete) a late event carrying offerRedeemed: true — or a stale granting event — is still dropped with no evidence recorded, so a host reading only the lead sentence will assume reserve() can never mint a reservation after redemption and will not defend against the duplicate-redemption scenario described in the code finding.

🔵 Issue 3 — Spec docs still state the superseded absolute keep/drop rules without the redemption-evidence exception

packages/billing-core/src/index.ts:957 · nit · identified by z-ai/glm-5.3-flash

The PR changes arbitration so that a rejected event can now cause a durable write, but three specification statements still describe the old absolute rule:

  • packages/billing-core/src/index.ts:956-960 — the decideLedgerApplication summary says "Equal rank at the equal second drops the later arrival" and "An existing row with no usable watermark that already carries subscription state is untrusted: keep, never apply because the event arrived", directly above the new paragraph describing an evidence write for exactly those cases.
  • packages/billing-core/src/index.ts:34-35 — the LifecycleStatus doc repeats "Equal rank at the equal second drops the later arrival."
  • docs/PROJECT_PLAN.md:64-71 — "an event applies only if strictly newer than max(eventAt, snapshotAt) ... Equal rank at the equal second drops the later arrival — conservative staleness until a strictly newer event arrives", untouched by this PR.

Cost: AGENTS.md designates the plan doc and these docblocks as the reference a fresh implementer reads; someone reimplementing or reviewing the decider from the summary sentences alone will omit the evidence write, or will be surprised that a "dropped" event returns applied: true with a generation bump.

Workflow run

Comment thread packages/billing-core/src/index.ts Resolved
Comment thread packages/billing-core/README.md Resolved
@FlyOverCoderKY

Copy link
Copy Markdown
Contributor Author

Updated the lifecycle and ordering documentation in 796feb4.

@github-actions github-actions 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.

OpenRouter pull-request review

Verdict: clean
Scope: latest-commit (commit-range)
Mode: verify
Commit: 796feb4e36891e353e8300e2aaae190724096bdc
Judge: openai/gpt-5.6-luna
Cost: $0.1498 (lanes $0.1496 + judge $0.0001)

Lanes

  • meta/muse-spark-1.2: ok (0 finding(s), 47.3s, 97352+2251 tokens (113 cached), 3 tool round(s), via Meta, $0.13)
  • z-ai/glm-5.3-flash: ok (0 finding(s), 95.4s, 394836+12306 tokens (338816 cached), 17 tool round(s), via CoreWeave, $0.0185)

Round 2 resolution

  • ✅ r1-1 fixed correctly — Stale redemption evidence is still dropped on non-terminal rows, so a redeemed one-per-customer offer can be reserved twice: recordsOfferRedemption now only requires current.offerRedeemed===false && (event.offerRedeemed || isGrantingStatus) — the current.status === canceled/unpaid gate was removed, and keepOrRecordOfferRedemption preserves lifecycle watermark, provider/reservation/host fields while bumping snapshotGeneration. Verified in index.ts:923-947 and decideLedgerApplication:971-1005. New coverage confirms incomp…
  • 2 nit finding(s) from earlier rounds retired (severity floor for follow-up rounds is risk).
  • Open findings after this round: 0 (0 bug-severity).

No structured findings from the successful lane(s).

Workflow run

@FlyOverCoderKY
FlyOverCoderKY merged commit cec23b0 into main Nov 6, 2026
3 checks passed
@FlyOverCoderKY
FlyOverCoderKY deleted the cursor/preserve-monotonic-offer-redemption branch November 6, 2026 18:34
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.

1 participant