Skip to content
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
3c21148
perf(entitlements): cache enforcement plan lookups
cursoragent Jul 31, 2026
7d5d777
style(docs): format entitlement cache notes
cursoragent Jul 31, 2026
0cd468d
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Jul 31, 2026
59a8238
feat(entitlements): add per-user quota meter
cursoragent Aug 1, 2026
8ebf663
feat(account): inventory UserMeter lifecycle
cursoragent Aug 1, 2026
4a60dd9
test(entitlements): provide UserMeter fixtures
cursoragent Aug 1, 2026
73a8e37
test(entitlements): clean meter test imports
cursoragent Aug 1, 2026
8b001bf
style(mcp): format meter gateway tests
cursoragent Aug 1, 2026
df32168
fix(email): read daily usage from UserMeter
cursoragent Aug 1, 2026
ae07b85
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
e6622a4
docs(entitlements): clarify meter cutover semantics
cursoragent Aug 1, 2026
7628aaf
fix(entitlements): address UserMeter review feedback
cursoragent Aug 1, 2026
468ee51
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
a8cba2b
test(entitlements): add RunLog admin fixture
cursoragent Aug 1, 2026
edfd973
test(run-log): isolate usage counts per user
cursoragent Aug 1, 2026
2307d37
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
b18246f
feat(entitlements): shadow storage bytes in UserMeter
cursoragent Aug 1, 2026
eaaa509
docs(entitlements): inventory storage shadow
cursoragent Aug 1, 2026
3c13b53
fix(entitlements): make storage reserves fail closed
cursoragent Aug 1, 2026
68256ea
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
e454e76
feat(services): shadow liveness in UserMeter
cursoragent Aug 1, 2026
9f7be03
docs(services): inventory liveness shadow
cursoragent Aug 1, 2026
581b896
perf(services): defer liveness shadow writes
cursoragent Aug 1, 2026
393b2a4
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
12e7c02
fix(services): serialize liveness shadows
cursoragent Aug 1, 2026
3a0d41d
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
194d5b5
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
5e8d1b9
feat(account): shadow deletion leases in UserMeter
cursoragent Aug 1, 2026
2e92f03
docs(account): inventory deletion shadow
cursoragent Aug 1, 2026
c566c54
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
62f6078
fix(account): reconcile and sanitize deletion shadows
cursoragent Aug 1, 2026
1116e07
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
d20696d
feat(account): move write leases into UserMeter
cursoragent Aug 1, 2026
6c2effd
docs(account): describe UserMeter lease authority
cursoragent Aug 1, 2026
afe0bb2
test(account): provide authoritative meter fixtures
cursoragent Aug 1, 2026
29dc257
test(account): wire meter into workers fixtures
cursoragent Aug 1, 2026
11814af
fix(account): clean partial lease mirrors
cursoragent Aug 1, 2026
7e88800
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
8d368eb
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
4c0aa23
feat(admin): add UserMeter parity report
cursoragent Aug 1, 2026
c44a518
docs(entitlements): define production parity gates
cursoragent Aug 1, 2026
a6180fc
fix(admin): bound parity page walks
cursoragent Aug 1, 2026
5af8263
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
177a471
refactor(entitlements): stop D1 daily counter mirror
cursoragent Aug 1, 2026
b955401
docs(entitlements): stage daily mirror retirement
cursoragent Aug 1, 2026
164b275
fix(entitlements): preserve pre-drop schema guards
cursoragent Aug 1, 2026
275d23b
fix(entitlements): clarify mirror-stop fallbacks
cursoragent Aug 1, 2026
436c272
docs(entitlements): clarify pre-drop inventory reads
cursoragent Aug 1, 2026
a279b9e
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
fc3bf7a
refactor(account): detach pending daily counter table
cursoragent Aug 1, 2026
fe1766b
docs(storage): point pending-drop guardrail correctly
cursoragent Aug 1, 2026
05bc952
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
659033c
refactor(entitlements): drop retired daily counter table
cursoragent Aug 1, 2026
65b2828
docs(entitlements): format retirement references
cursoragent Aug 1, 2026
3f1f3da
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
698258e
feat(admin): add storage reconciliation maintenance
cursoragent Aug 1, 2026
55b61bb
test(admin): expose reconciliation batch constant
cursoragent Aug 1, 2026
1218bfb
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
3419840
feat(entitlements): make UserMeter storage authority
cursoragent Aug 1, 2026
b1a62a2
fix(entitlements): preserve missing-user storage semantics
cursoragent Aug 1, 2026
70a4b07
style(entitlements): format missing-user test
cursoragent Aug 1, 2026
69dc243
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 1, 2026
1909a01
feat(entitlements): make UserMeter service authority
cursoragent Aug 2, 2026
9dff125
style(entitlements): format service authority tests
cursoragent Aug 2, 2026
83bfcf4
fix(services): await authoritative running projection
cursoragent Aug 2, 2026
be06e56
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 2, 2026
73537b2
fix(services): serialize pending start confirmation
cursoragent Aug 2, 2026
1182cb3
Merge remote-tracking branch 'origin/main' into cursor/meter-do-38c8
cursoragent Aug 2, 2026
d9bc9ea
refactor(account): retire D1 write lease mirror
cursoragent Aug 2, 2026
42e6e3b
style(account): format lease retirement changes
cursoragent Aug 2, 2026
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
25 changes: 19 additions & 6 deletions docs/contributing/account-write-lease-repair.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@ Account write leases do not expire. A crashed writer intentionally blocks
account deletion until an administrator verifies that the process is gone and
releases its exact token with an audit reason.

Active leases may live in D1 (email / no-`env` writers) and/or UserMeter
(`authority='do'` when callers supply `USER_METER`). Admin list unions both,
Active leases live in the UserMeter DO (`authority='do'`) and/or in D1
(`account_write_leases`) as legacy email leases or historical stale rows from
before the temporary D1 mirror was retired (2026-08-01). Admin list unions both,
deduped by token.

**Temporary D1 mirror retired (2026-08-01):** DO-authority callers (those that
supply `USER_METER`) no longer insert a D1 row on acquire. D1
`account_write_leases` rows now represent:

- **Legacy email leases** — callers that omit `env` still take the exact D1 path.
- **Historical stale rows** — pre-retirement mirrors that were not cleaned up
before the deploy. Use `admin_account_write_lease_repair` to clear these via
the audited repair path; do not delete rows directly from D1.

D1 columns and the `account_write_leases` table are **retained** — no schema
drop is planned yet.

First inspect active leases:

```javascript
Expand All @@ -29,11 +42,11 @@ await kody.admin_account_write_lease_repair({
```

The repair writes `account_write_lease_repairs` before releasing the token. D1
leases use an atomic audit-before-delete batch. DO-authority leases use prepare
(stable `repairId`, lease stays held) → audit insert/verify → finalize DO
deletion → clear the temporary D1 rollout mirror. The D1 mirror is never cleared
leases (legacy email) use an atomic audit-before-delete batch. DO-authority
leases use prepare (stable `repairId`, lease stays held) → audit insert/verify →
finalize DO deletion → clear any stale D1 row. A stale D1 row is never cleared
while the DO lease remains held. Retries after a lost finalize response succeed
when the matching audit exists and the DO lease is already gone (idempotently
clearing any stale D1 mirror). Wrong user, stale timestamp, or short reason
clearing any stale D1 row). Wrong user, stale timestamp, or short reason
requests fail closed. Retry account deletion only after inspection shows no
active leases.
100 changes: 50 additions & 50 deletions docs/contributing/architecture/entitlements.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ parity checks, reconcile tooling, and cold bootstrap read it.
for discovery, account export, and deletion, and is the parity mirror — see
[Package service liveness — UserMeter authority](#package-service-liveness--usermeter-authority-cutover-2026-08-01).

**Account-deletion write fencing** uses a split authority model after slice 5
Phase B: D1 `users.deleting_at` remains the permanent point gate; callers that
supply `USER_METER` treat UserMeter (`authority='do'`) as authoritative for
lease acquire/held/release/drain count, while email/transition callers that omit
`env` keep the exact D1 `account_write_leases` path. See
**Account-deletion write fencing** uses a split authority model after the
temporary D1 mirror was retired (2026-08-01): D1 `users.deleting_at` remains
the permanent point gate; callers that supply `USER_METER` treat UserMeter
(`authority='do'`) as authoritative for lease acquire/held/release/drain count
without any D1 row, while email/transition callers that omit `env` keep the
exact D1 `account_write_leases` path. D1 `account_write_leases` rows are now
legacy email leases and historical stale pre-retirement rows. See
[Account-deletion write fencing — UserMeter authority](#account-deletion-write-fencing--usermeter-authority-expand-phase-slice-5-phase-b).

StorageRunner bucket `estimatedBytes` and the per-bucket inventory in
Expand Down Expand Up @@ -339,7 +341,7 @@ pages return `null`. The field reflects the authoritative DO inventory.
**Account purge:** `UserMeter.purge()` clears package-service shadow rows via
`deleteAll` then schema re-init.

### Account-deletion write fencing — UserMeter authority (expand phase slice 5, Phase B)
### Account-deletion write fencing — UserMeter authority (expand phase slice 5, Phase B; mirror retired 2026-08-01)

UserMeter schema **v7** stores `account_write_leases.authority` (`do` |
`legacy`; rows created under v6 default to `legacy`) and `pending_repair_id` for
Expand All @@ -350,45 +352,44 @@ audit-safe DO repair.
- D1 `users.deleting_at` remains the **permanent point gate** (auth projection /
purge failures fail closed). Assert-only readers (`assertAccountWritable*`)
stay D1-only. D1 `account_write_leases` / `account_write_lease_repairs` tables
are **not** retired.
- When `env.USER_METER` is supplied (all non-email Phase-A-wired callers),
UserMeter is **authoritative** for lease acquire / held / release via
`acquireWriteLease` / `assertWriteLeaseHeld` / `releaseWriteLease`
(`authority='do'`). Missing binding or DO failures **fail closed**.
- When `env` is omitted (email transition), retain the **exact D1 lease path**
are **not** retired; the columns and table are kept for legacy email leases,
historical stale rows, and audit repair records.
- When `env.USER_METER` is supplied (all non-email callers), UserMeter is
**authoritative** for lease acquire / held / release via `acquireWriteLease` /
`assertWriteLeaseHeld` / `releaseWriteLease` (`authority='do'`). Missing
binding or DO failures **fail closed**. No D1 row is written on acquire.
- When `env` is omitted (email/legacy), retain the **exact D1 lease path**
(including `active_write_count`, ALS nested reuse, and `waitUntil` release).

**Temporary rolling-version D1 mirror (not the final hot path):** while older
isolates may still run Phase-A mark (D1-only lease counts), every DO-authority
acquire also inserts the **same** `token` / `holder` / `acquiredAt` into D1
`account_write_leases` and bumps `active_write_count` with the existing atomic
batch + lost-response reconcile. If that D1 mirror cannot be confirmed, the DO
lease is released and acquire fails closed. Authoritative release clears the DO
lease first, then the D1 mirror/`active_write_count` (same `waitUntil`
detachment semantics) so a stale D1 row may overblock old marks but never
underblock. After old isolates drain, remove this mirror (and later the D1 lease
inventory) from the hot path.
**Temporary D1 mirror retired (2026-08-01):** the same-token dual-write of DO
leases into D1 `account_write_leases` ended after old-isolate drain. D1 rows
for DO-authority leases are now historical stale rows from before retirement;
use `admin_account_write_lease_repair` to clear them via the audited path.
`admin_user_meter_parity` reports `deletion.temporaryMirrorRetired: true`; the
`doOnly` category is expected and does not fail `mirrorLeaseParity`. Only
`legacyWithoutD1` (legacy-authority meter leases without a D1 row) and
truncated inventory fail the gate.

**`markAccountDeleting`:** always `COALESCE`s D1 `deleting_at` first, then loads
live D1 leases (including temporary DO mirrors). With `env`, calls authoritative
`markDeleting` which sets/preserves the DO tombstone, replaces **only legacy**
rows with that exact D1 snapshot, preserves DO-authority rows, and returns the
deduped-by-token union count used for drain waits. Without `env`, returns the D1
lease count unchanged (so Phase-A marks observe DO acquires via the mirror).
live D1 leases (legacy email rows only after mirror retirement). With `env`,
calls authoritative `markDeleting` which sets/preserves the DO tombstone,
replaces **only legacy** rows with that exact D1 snapshot, preserves DO-authority
rows, and returns the deduped-by-token union count used for drain waits. Without
`env`, returns the D1 lease count unchanged.

**Admin list / repair:** `listActiveAccountWriteLeases(db, userId, env?)` with
`env` unions live D1 leases + DO-authority leases (dedupe by token, same
`acquired_at, token` order); without `env` exact D1 behavior. DO repair is
audit-first and idempotent: prepare (stable `repairId`, lease stays held) →
insert/verify D1 audit → finalize exact pending DO repair → then idempotently
clear matching D1 mirror/count. The D1 mirror is never cleared while the DO
lease remains held. Finalize failure without commit leaves DO + D1 mirror held
and fails closed; retry resumes the pending repair. Retry after finalize commit
/ lost response returns success when the matching audit exists and the DO lease
is absent (clears any stale D1 mirror; never falls through to a mismatched D1
atomic batch). Post-write held checks treat pending repair as held until
finalize, then surface `AccountWriteLeaseLostError`. Pure D1 leases keep the
existing atomic audit-before-release batch.
clear any stale D1 row. A stale D1 row is never cleared while the DO lease
remains held. Finalize failure without commit leaves DO held and the stale D1
row (if any) in place, and fails closed; retry resumes the pending repair. Retry
after finalize commit / lost response returns success when the matching audit
exists and the DO lease is absent (clears any stale D1 row; never falls through
to a mismatched D1 atomic batch). Post-write held checks treat pending repair as
held until finalize, then surface `AccountWriteLeaseLostError`. Pure D1 leases
keep the existing atomic audit-before-release batch.

**Account export / purge:** first-page sanitized `deletionShadow` still omits
raw token/holder. `purge()` clears leases/counters/shadows via `deleteAll` then
Expand Down Expand Up @@ -425,9 +426,9 @@ parity sign-off).
**Do not drop the D1 columns yet.** They serve as the async mirror, cold
bootstrap source, and reconcile cursor during the dual-write window.

**Current UserMeter cutover:** slice 5 Phase B moves non-email write leases into
UserMeter authority while retaining the D1 `deleting_at` gate and temporary
same-token rollout mirrors; email keeps its D1 lease path. Package-service
**Current UserMeter cutover:** slice 5 Phase B moved non-email write leases into
UserMeter authority; the temporary same-token D1 rollout mirror was retired
2026-08-01 after old-isolate drain. Email keeps its D1 lease path. Package-service
authority flip remains a separate high-risk contract follow-up after soak/parity
review. Storage authority flip is complete (see above).

Expand Down Expand Up @@ -462,22 +463,21 @@ Interpret the structured report as independent gates:
| Daily counters (current UTC day) | Final post-drop semantics (`daily.mirrorRetired: true`): report meter counts only; `d1Count`/`delta` are null, each of the four daily resources has `parity: true`, and `mismatchCount === 0` (no D1 comparison). Pre-drop Workers that still see the table compare `d1Count === meterCount` with `mirrorRetired: false`. |
| Storage bytes | `storage.parity` — D1 `users.d1_storage_bytes` equals UserMeter `readStorageBytes` (not `needsBootstrap`). |
| Package services | `packageServices.parity` — inventory mismatch category counts are all zero (`d1Only` / `meterOnly` / `statusMismatch` / `startedAtMismatch` / `sourceUpdatedAtMismatch`), fresh-running counts match under the shared 24h stale window, and the meter page walk is not `truncated`. |
| Deletion tombstone | `deletion.deletingAtParity` — D1 `users.deleting_at` matches the meter tombstone. |
| Temporary D1 lease mirror | `deletion.mirrorLeaseParity` — `doOnly === 0`, `legacyWithoutD1 === 0`, inventory not truncated, and `d1ActiveLeaseCount >= doAuthorityLeaseCount` (same-token mirror coverage). `tokenSetMismatches.d1Only` is reported but does **not** fail this gate. |
| Deletion tombstone | `deletion.deletingAtParity` — D1 `users.deleting_at` matches the meter tombstone. |
| Lease readiness (post-retirement) | `deletion.mirrorLeaseParity` — `legacyWithoutD1 === 0` and inventory not truncated. `doOnly` is expected after mirror retirement and does **not** fail this gate. `d1Only` reflects legacy email leases and historical stale pre-retirement rows. `deletion.temporaryMirrorRetired` is always `true`. |

**D1-only leases:** email and other transition paths that omit `env` still take
exact D1 leases, so `d1Only > 0` is expected until that handoff. Mirror-removal
readiness therefore ignores `d1Only`. Operators separately confirm those rows
are known email/transition holders via `admin_account_write_lease_list` and
holder classification before retiring the temporary D1 mirror inventory.
**D1-only (legacy) leases:** email paths that omit `env` still take exact D1
leases, so `d1Only > 0` is expected and normal. Stale pre-retirement D1 mirrors
also contribute to `d1Only` until cleared via `admin_account_write_lease_repair`.
Operators confirm legacy holders via `admin_account_write_lease_list`.

**Threshold:** treat unexplained mismatches as blocking for the corresponding
cutover (daily mirror retirement before the drop migration, storage authority
flip, package-service authority flip, or temporary D1 lease-mirror removal).
Expected cold accounts may report `needsBootstrap` until live traffic seeds the
DO; that is a bootstrap gap, not a silent pass. Truncated inventories fail
closed (`parity` / `mirrorLeaseParity` false) so operators re-run or raise the
bounded page cap rather than approve a partial compare.
flip, or package-service authority flip). Expected cold accounts may report
`needsBootstrap` until live traffic seeds the DO; that is a bootstrap gap, not
a silent pass. Truncated inventories fail closed (`parity` / `mirrorLeaseParity`
false) so operators re-run or raise the bounded page cap rather than approve a
partial compare.

### Post-flip storage reconciliation (`admin_user_meter_storage_reconcile`)

Expand Down
Loading
Loading