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
10 changes: 5 additions & 5 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 1: `@pegma/billing-core` (ledger collection, `(eventAt, eventId)`
watermark, effective-watermark guard, lifecycle-rank tie-break). Nothing is
published. Invariant combinators and reservation are Phase 2; snapshot
reconciliation is Phase 3; `@pegma/billing-stripe` is Phase 4 — do not
create those packages here yet.
Phase 2: `@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.

Siblings: [spine](https://github.com/pegma-dev/spine),
[storage-core](https://github.com/pegma-dev/storage-core),
Expand Down
24 changes: 12 additions & 12 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 1 is
> Pegma is in early `0.x` development. `@pegma/billing-core` Phase 2 is
> in-tree and unpublished. No public API is stable.

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

Phase 1 ships the watermark, the effective-watermark guard, and
lifecycle-rank arbitration. Snapshot reconciliation, declared ledger
invariants (`sticky`, `firstWins`), and the checkout reservation are later
phases. Ships later with a Stripe adapter; 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.
Phase 2 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
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.

Not here, on purpose: payment processing and checkout flows, entitlement
resolution ([Authorization Core](https://github.com/pegma-dev/authorization-core)'s
Expand All @@ -54,10 +54,10 @@ application.

## Packages

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

The Stripe adapter package is not created until Phase 4.

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

## Status

**Stage:** Phase 1 — arbitration core in-tree. Nothing is published.
(`0.1.0`, unpublished.)
**Stage:** Phase 2 — combinators and reservation in-tree. Nothing is
published. (`0.1.0`, unpublished.)

**Extraction source:** the RetireGolden account API's subscription ledger —
`api/src/lib/repo.js` (`shouldApplyEvent`, `applySubscriptionSnapshot`,
Expand Down Expand Up @@ -160,13 +160,13 @@ posture.

## Delivery phases

### Phase 1 — the arbitration core (this tree)
### Phase 1 — the arbitration core

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
### Phase 2 — invariant combinators and the reservation (this tree)

`sticky` and `firstWins` as declared rules; the single-opportunity
reservation with its read-winner-back semantics and TTL expiry.
Expand All @@ -191,11 +191,11 @@ acceptance bar.

## Timing

Phase 1 is implemented in-tree. Later phases remain gated: combinators and
reservation (Phase 2), 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 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.

## Open questions

Expand Down
50 changes: 37 additions & 13 deletions packages/billing-core/README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,67 @@
# `@pegma/billing-core`

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

> [!IMPORTANT]
> Pegma is in early `0.x` development. This package's public API is unstable.

```ts
import { createBillingLedger } from "@pegma/billing-core";
import { createBillingLedger, firstWins, sticky } from "@pegma/billing-core";
import { createMemoryStore } from "@pegma/storage-core";
import { fixedClock } from "@pegma/spine";

const ledger = createBillingLedger({
store: createMemoryStore(),
clock: fixedClock("2026-08-15T16:00:00.000Z"),
fields: {
foundingMember: sticky(),
consentAt: firstWins("consent"),
consentVersion: firstWins("consent"),
},
});

const result = await ledger.apply("acct_123", {
eventId: "evt_canceled",
await ledger.apply("acct_123", {
eventId: "evt_founding",
eventAt: "2026-08-15T16:00:00.000Z",
status: "canceled",
status: "active",
providerCustomerId: "cus_123",
providerSubscriptionId: "sub_123",
providerPriceId: "price_pro",
plan: "pro",
providerPriceId: "price_founding",
plan: "founding",
periodStartAt: "2026-08-01T00:00:00.000Z",
periodEndAt: "2026-09-01T00:00:00.000Z",
trialStartAt: null,
trialEndAt: null,
cancelAtPeriodEnd: false,
fields: {
foundingMember: true,
consentAt: "2026-08-15T16:00:00.000Z",
consentVersion: "v1",
},
});

const reserved = await ledger.reserve("acct_456");
if (reserved.reserved) {
await ledger.release("acct_456", reserved.reservationId);
}
```

Hosts inject a `@pegma/storage-core` `Store` and, optionally, a Spine
`Clock` and `Logger`. This package never creates a network client and never
stores card data, raw payloads, line items, or amounts.
`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.

Arrival order proves nothing. Every apply goes through the effective-watermark
guard and lifecycle rank. Exact redelivery is an idempotent no-op.

Phase 1 is the arbitration core. Invariant combinators, checkout reservation,
snapshot reconciliation, and the Stripe adapter are later phases — do not
import them from this package.
`sticky` and `firstWins` are declared on the host's ledger definition and
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
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, effective-watermark guard, and lifecycle-rank arbitration.",
"description": "Provider-agnostic subscription ledger: event watermark, lifecycle-rank arbitration, declared invariants, and checkout reservation.",
"license": "MIT",
"type": "module",
"sideEffects": false,
Expand Down
Loading
Loading