Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ protected signed annotated `vX.Y.Z` tag already on `origin/main`, followed by

## Where things stand

Phase 2: `@pegma/billing-core` (ledger collection, `(eventAt, eventId)`
Phase 3: `@pegma/billing-core` (ledger collection, `(eventAt, eventId)`
watermark, effective-watermark guard, lifecycle-rank tie-break, declared
`sticky` / `firstWins` combinators, checkout reservation). Nothing is
published. Snapshot reconciliation is Phase 3; `@pegma/billing-stripe` is
Phase 4 — do not create those packages here yet.
`sticky` / `firstWins` combinators, checkout reservation, snapshot
reconciliation). Nothing is published. `@pegma/billing-stripe` is Phase 4
— do not create that package here yet.

Siblings: [spine](https://github.com/pegma-dev/spine),
[storage-core](https://github.com/pegma-dev/storage-core),
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ hosts: the durable record of what a customer's subscription **is**,
maintained correctly under out-of-order webhook delivery.

> [!IMPORTANT]
> Pegma is in early `0.x` development. `@pegma/billing-core` Phase 2 is
> Pegma is in early `0.x` development. `@pegma/billing-core` Phase 3 is
> in-tree and unpublished. No public API is stable.

## The part everyone gets wrong
Expand All @@ -29,10 +29,10 @@ makes that impossible:
- periodic **snapshot reconciliation** that repairs field drift without
disturbing the dedup identity of the events themselves.

Phase 2 ships the watermark, the effective-watermark guard,
Phase 3 ships the watermark, the effective-watermark guard,
lifecycle-rank arbitration, declared ledger invariants (`sticky`,
`firstWins`), and the checkout reservation. Snapshot reconciliation and
the Stripe adapter are later phases. The core knows lifecycle states, not
`firstWins`), the checkout reservation, and snapshot reconciliation.
The Stripe adapter is a later phase. The core knows lifecycle states, not
Stripe's vocabulary. And the data boundary is absolute: the ledger stores
provider identifiers and derived state — never a card number, a raw
payload, or an amount.
Expand All @@ -54,10 +54,10 @@ application.

## Packages

| Package | Role | Phase |
| ----------------------- | ------------------------------------------------ | ----- |
| `@pegma/billing-core` | Ledger, watermark, rank, invariants, reservation | 2 |
| `@pegma/billing-stripe` | Event and snapshot translation | later |
| Package | Role | Phase |
| ----------------------- | ------------------------------------------------------- | ----- |
| `@pegma/billing-core` | Ledger, watermark, rank, invariants, reservation, sweep | 3 |
| `@pegma/billing-stripe` | Event and snapshot translation | later |

The Stripe adapter package is not created until Phase 4.

Expand Down
15 changes: 7 additions & 8 deletions docs/PROJECT_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Status

**Stage:** Phase 2 — combinators and reservation in-tree. Nothing is
**Stage:** Phase 3 — snapshot reconciliation in-tree. Nothing is
published. (`0.1.0`, unpublished.)

**Extraction source:** the RetireGolden account API's subscription ledger —
Expand Down Expand Up @@ -166,12 +166,12 @@ Ledger collection, watermark, effective-watermark guard, lifecycle rank,
equal-second tie-breaking. The reference implementation's race tests are
the conformance bar; suite runs over memory and real Azurite.

### Phase 2 — invariant combinators and the reservation (this tree)
### Phase 2 — invariant combinators and the reservation

`sticky` and `firstWins` as declared rules; the single-opportunity
reservation with its read-winner-back semantics and TTL expiry.

### Phase 3 — snapshot reconciliation
### Phase 3 — snapshot reconciliation (this tree)

The sweep, the domain CAS token, the always-write freshness rule — with
the delayed-intermediate-webhook scenario pinned as a named test.
Expand All @@ -191,11 +191,10 @@ acceptance bar.

## Timing

Phase 2 is implemented in-tree. Later phases remain gated: snapshot
reconciliation (Phase 3) and the Stripe adapter (Phase 4) wait until a
named consumer needs them. Phase 5 (first consumer cutover) stays gated
on that application's operational calendar — never swap code under a soak
or mid-migration.
Phase 3 is implemented in-tree. Later phases remain gated: the Stripe
adapter (Phase 4) waits until a named consumer needs it. Phase 5 (first
consumer cutover) stays gated on that application's operational calendar
— never swap code under a soak or mid-migration.

## Open questions

Expand Down
40 changes: 33 additions & 7 deletions packages/billing-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

Provider-agnostic subscription ledger for Pegma hosts: a per-account
watermark, an effective-watermark guard, lifecycle-rank tie-breaking at
the equal second, declared write-path invariants, and a single-opportunity
checkout reservation.
the equal second, declared write-path invariants, a single-opportunity
checkout reservation, and snapshot reconciliation.

> [!IMPORTANT]
> Pegma is in early `0.x` development. This package's public API is unstable.
Expand Down Expand Up @@ -47,12 +47,26 @@ const reserved = await ledger.reserve("acct_456");
if (reserved.reserved) {
await ledger.release("acct_456", reserved.reservationId);
}

await ledger.reconcile("acct_123", async (observed) => ({
status: observed.status,
providerCustomerId: observed.providerCustomerId,
providerSubscriptionId: observed.providerSubscriptionId,
providerPriceId: observed.providerPriceId,
plan: observed.plan,
periodStartAt: observed.periodStartAt,
periodEndAt: observed.periodEndAt,
trialStartAt: observed.trialStartAt,
trialEndAt: observed.trialEndAt,
cancelAtPeriodEnd: observed.cancelAtPeriodEnd,
}));
```

Hosts inject a `@pegma/storage-core` `Store` and, optionally, a Spine
`Clock` and `Logger`. Reservation TTL is taken from the injected clock —
never from `Date.now()`. This package never creates a network client and
never stores card data, raw payloads, line items, or amounts.
`Clock` and `Logger`. Reservation TTL and snapshot freshness (`snapshotAt`)
are taken from the injected clock — never from `Date.now()`. This package
never creates a network client and never stores card data, raw payloads,
line items, or amounts.

Arrival order proves nothing. Every apply goes through the effective-watermark
guard and lifecycle rank. Exact redelivery is an idempotent no-op.
Expand All @@ -62,6 +76,18 @@ enforced inside the update decider, so they re-evaluate against fresh state
on every conflict. A reservation id is minted inside the decider and read
back from storage; the caller's id is whatever the stored record says.

Phase 2 is the arbitration core plus combinators and reservation. Snapshot
reconciliation and the Stripe adapter are later phases — do not import them
Snapshot reconciliation observes the domain CAS token `(eventAt, eventId)`
and the current `snapshotAt` / `snapshotGeneration`, samples the freshness
bound from the injected clock, then fetches provider truth. The decider
re-checks the watermark and the observed generation against fresh state.
An intervening event or a newer reconciliation drops the snapshot. A
reservation-only row is skipped so a founding webhook is not gated by an
invented bound. A token match always writes — even when no field changed —
and increments `snapshotGeneration` so overlapping same-bound sweeps cannot
clobber fresher fields. `snapshotAt` never jumps into a later Unix second
than the sampled bound, so same-second webhooks still get lifecycle-rank
arbitration. The watermark identity is never touched.

Phase 3 is the arbitration core, combinators, reservation, and snapshot
reconciliation. The Stripe adapter is a later phase — do not import it
from this package.
2 changes: 1 addition & 1 deletion packages/billing-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@pegma/billing-core",
"version": "0.1.0",
"description": "Provider-agnostic subscription ledger: event watermark, lifecycle-rank arbitration, declared invariants, and checkout reservation.",
"description": "Provider-agnostic subscription ledger: event watermark, lifecycle-rank arbitration, declared invariants, checkout reservation, and snapshot reconciliation.",
"license": "MIT",
"type": "module",
"sideEffects": false,
Expand Down
Loading
Loading