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
349 changes: 128 additions & 221 deletions docs/contributing/architecture/data-storage.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/contributing/architecture/feature-flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ end-to-end (`e2e/admin-feature-flags.spec.ts`).

The retired `mailbox-read-cutover` migration flag is intentionally absent from
the registry. USER email reads are unconditionally owner-Mailbox reads; there is
no D1 fallback or parity-soak gate. The physical D1 graph is a frozen rollback
snapshot, not a selectable serving source.
no D1 fallback or parity-soak gate. Migration 0135 removed the physical shared
D1 graph.

## Success metrics

Expand Down
43 changes: 33 additions & 10 deletions docs/contributing/disaster-recovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,25 @@ Disable ingress / put the app in maintenance before execute. After restore:
reindex Vectorize, re-arm jobs/alarms from D1, recreate queues from Wrangler
config, and expect users to reauthorize OAuth and remote connectors.

### Mailbox authority rollback repair

The frozen shared USER D1 graph is not a rollback or roll-forward source. It
contains only the pre-cutover snapshot and may be reduced by account-deletion or
retention privacy cleanup. A recovery that changes USER email authority must
quiesce ingress and scheduled/effect consumers, restore the authoritative
Mailbox and `EMAIL_BLOBS` snapshots, verify owner inventories and
effect/finalization state, then use reviewed operator tooling to populate any
replacement target from Mailbox before changing code authority. The retired
D1-to-Mailbox parity purge/rebuild path must not be reintroduced.
### Mailbox authority and the pre-drop D1 backup

Migration 0134 installs the canonical approval schema; migration 0135
permanently drops the shared USER/system rollback graph. Production retains the
complete immutable receipt in `email_user_graph_drop_approval`; a newly created,
provably empty preview database may bootstrap without creating one. To inspect
or recover deleted historical rows, download the production receipt's exact SQL
object, verify the signed manifest plus bytes/SHA-256/ETag before import,
restore it into an isolated D1 database, and verify the approved
owner/thread/message/attachment/event manifest plus `PRAGMA foreign_key_check`.
Do not point a live Worker at that isolated pre-0135 schema.

The pre-drop D1 object is forensic/disaster-recovery evidence, not a USER
serving source. A production recovery must quiesce ingress and scheduled/effect
consumers, restore the authoritative Mailbox and `EMAIL_BLOBS` snapshots, and
verify owner inventories and effect/finalization state. A full D1 restore to
0133 state also requires the reviewed migration/conductor procedure to deploy
0134, issue a new approval, and deliberately reapply 0135; the old D1-to-Mailbox
parity/rebuild path must not be reintroduced.

### Mailbox legacy graph destructive prerequisite

Expand All @@ -388,6 +397,20 @@ hours. The caller supplies only `requestId`, `nonce`, and `requestedAt`; source
identity, counts, object keys, hashes, and approval values come from the
allowlisted control plane.

0135 accepts a newer receipt without pinning a particular request, nonce, object
key, SQL SHA-256, ETag, or signature digest. It does pin the deployed trust
configuration: production source account/database/name, signing-key id, Ed25519,
daily retention, restore-baseline id/SHA-256, control-plane issuer, and build
commit. Its monotonic timestamps, canonical manifest/SQL relation, current
marker, and exact counts must also validate at execution time. The receipt
prepared on 2026-08-03 expires at `21:10:38.879Z`; run the same deployed
Workflow again if deployment occurs later.

The only receipt-free case is a universal-chain bootstrap before seeding. The
authority marker and all shared, dedicated, provider-index, repair, due-owner,
and inbound-effect surfaces must be empty. Any legacy row or nonzero marker
requires the ordinary signed approval path.

The production `adhoc/` policy was manually applied and read back on 2026-08-03:
both lock and lifecycle are 35 days. When `DR_BACKUP_ADMIN_TOKEN` is available,
the backup-control-plane deploy job reconciles the managed policy and requires
Expand Down
101 changes: 101 additions & 0 deletions docs/contributing/mailbox-legacy-graph-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Mailbox legacy graph drop

Migration `0135-drop-legacy-email-graph.sql` is destructive and remains
conductor-controlled. Migration 0134 only installs the canonical approval
schema. Direct production SQL is never an approval path.

## Canonical approval process

1. Confirm migration 0134 and the backup control plane are deployed.
2. Generate a UUID request id, a 32-character lowercase hexadecimal nonce, and a
canonical UTC `requestedAt`.
3. Trigger `kody-mailbox-legacy-graph-pre-drop-backup` through the Cloudflare
Workflow API. `params` is an object, not a JSON string.

Example request body:

```json
{
"instance_id": "mailbox-pre-drop-11111111-1111-4111-8111-111111111111",
"params": {
"requestId": "11111111-1111-4111-8111-111111111111",
"nonce": "0123456789abcdef0123456789abcdef",
"requestedAt": "2026-08-03T19:00:00.000Z"
}
}
```

4. Poll that exact instance until `result.status` is `complete`.
5. Retain the non-secret approval receipt. Confirm its `requestId` and `nonce`,
immutable manifest/SQL keys, SQL bytes/SHA-256/R2 ETag, signing key and
signature digest, source identity, export/build/baseline provenance, frozen
marker, exact counts, issuer, verification time, and expiration.
6. Apply 0135 only while that receipt is unexpired. Approval lasts at most two
hours. If conductor merge occurs after expiration, trigger a new Workflow
instance with a new request id, nonce, and timestamp.

The current production receipt expires at `2026-08-03T21:10:38.879Z`. It is not
a value embedded in 0135. A newer receipt may replace it, but it must come from
the same deployed trust configuration: source account
`a99ee2e72728dd52902ef288b7b1447d`, database
`8c1014d1-6b41-4695-a0a2-159071f0f919` / `kody`, key `kody-dr-2026-07`, baseline
`kody-migration-set-2026-07` /
`feb76eb26ed72a55d5fa25d14ef9ee904d0758fc29842c898b584a921ccfd995`, and build
`fe1ca2772de7f369fc06a7d1bd9aeadc3347b2a7`. The migration does not pin the
request id, request nonce, manifest/SQL object keys, SQL SHA-256, R2 ETag, or
manifest-signature digest.

The first attempt that encoded `params` as a string failed and is irrelevant: it
issued no receipt and cannot authorize the migration. Never update or recreate
`email_user_graph_drop_approval` by hand.

## Exact deploy preflight

The canonical preflight is the SQL before the first destructive statement in
`packages/worker/migrations/0135-drop-legacy-email-graph.sql`. It runs in the
same Wrangler D1 transaction as the drop and aborts on any failed CHECK. It
requires:

- all canonical 0134 approval provenance, including source, signing,
restore-baseline, control-plane-build, and object evidence, bound to the
current 0133 marker and a currently valid two-hour verification window;
- exact frozen USER owner/thread/message/attachment/event counts;
- no provider-index repair owner and no `cutover-audit` due-owner backlog;
- a valid dedicated system authority marker, zero system provider links, and
bidirectional full-column legacy/dedicated system parity;
- no unexpected trigger, view, or foreign-key dependency on the shared graph,
and clean foreign-key checks before and after the rebuild/drop.

There is one approval-free bootstrap path for a database that has never held an
email graph. It requires no approval row, `owner_count = 0`, zero total rows
(including `system:email`) in all four shared tables, and empty provider-index,
repair-owner, due-owner, inbound-usage-effect, and dedicated-system graph
surfaces. The dedicated authority marker must also be healthy. A single legacy
row or a nonzero authority marker closes this path and requires a valid
control-plane receipt. This is what lets a newly created preview database apply
the universal migration chain before test-data seeding.

`email_inbound_usage_effects` is intentionally retained. The 0134 signed
contract has no count for it, so 0135 preserves every row and detaches only its
obsolete foreign key. It is an idempotency ledger, expected to age toward zero,
not approved drop data.

Do not copy a reduced status query into deployment automation. The migration
preflight is the exact final authority check coupled atomically to the
destructive statements.

## Final storage and restore state

USER thread/message/attachment/event authority is Mailbox Durable Object SQLite;
USER raw MIME and external attachment bytes remain in `EMAIL_BLOBS`. Dedicated
`system_email_*` tables remain the operator inbox authority. D1 retains
low-write mail configuration, the thin provider reverse index, the detached
inbound usage idempotency ledger, and the cutover marker. Environments that used
the destructive approval path also retain the complete canonical approval
receipt; empty bootstraps have no synthetic receipt.

For inspection, verify the receipt's manifest signature and SQL bytes/SHA-256/
ETag, import into an isolated D1 database, run `PRAGMA foreign_key_check`, and
keep that database disconnected from live Workers. A production recovery to the
pre-drop schema requires maintenance mode and a separately reviewed conductor
plan; it must not reintroduce the retired D1-to-Mailbox mirror.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"primitives:classify": "node .agents/skills/visual-recap/scripts/classify-primitives.mjs",
"docs:check-temporal": "node tools/check-docs-temporal-language.ts",
"migrations:check": "node tools/check-migrations.ts",
"migrate:local": "node --env-file=packages/worker/.env ./wrangler-env.ts d1 migrations apply APP_DB --local && node --env-file=packages/worker/.env ./wrangler-env.ts d1 migrations apply AUDIT_DB --local",
"migrate:e2e": "node --env-file=packages/worker/.env ./wrangler-env.ts d1 migrations apply APP_DB --local --persist-to .wrangler/state/e2e && node --env-file=packages/worker/.env ./wrangler-env.ts d1 migrations apply AUDIT_DB --local --persist-to .wrangler/state/e2e",
"migrate:local": "node tools/apply-local-app-migrations.ts && node --env-file=packages/worker/.env ./wrangler-env.ts d1 migrations apply AUDIT_DB --local",
"migrate:e2e": "node tools/apply-local-app-migrations.ts --persist-to .wrangler/state/e2e && node --env-file=packages/worker/.env ./wrangler-env.ts d1 migrations apply AUDIT_DB --local --persist-to .wrangler/state/e2e",
"preview": "nx run worker:build-client && npm run migrate:local && node --env-file=packages/worker/.env ./wrangler-env.ts dev --local",
"preview:e2e": "node --env-file=packages/worker/.env tools/prepare-e2e-env.ts && nx run worker:build-client && npm run migrate:e2e && node --env-file=packages/worker/.env ./wrangler-env.ts dev --local --persist-to .wrangler/state/e2e",
"generate-types": "node --env-file=packages/worker/.env ./wrangler-env.ts types ./packages/worker/worker-configuration.d.ts",
Expand Down
11 changes: 8 additions & 3 deletions packages/backup-control-plane/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,18 @@ Content-Type: application/json

{
"instance_id": "mailbox-pre-drop-11111111-1111-4111-8111-111111111111",
"params": "{\"requestId\":\"11111111-1111-4111-8111-111111111111\",\"nonce\":\"0123456789abcdef0123456789abcdef\",\"requestedAt\":\"2026-08-03T16:45:00.000Z\"}"
"params": {
"requestId": "11111111-1111-4111-8111-111111111111",
"nonce": "0123456789abcdef0123456789abcdef",
"requestedAt": "2026-08-03T16:45:00.000Z"
}
}
```

The REST API names the custom id field `instance_id` and requires `params` to be
a JSON-encoded string. The Workflow receives the decoded three-key object. Do
not use the binding API's `{ id, params }` shape in this REST request.
the three-key JSON object shown above. Do not encode `params` as a string. The
failed first string-param instance issued no approval and is irrelevant. Do not
use the binding API's `{ id, params }` shape in this REST request.

The authenticated control-plane UI action
`POST /actions/mailbox-pre-drop-backup` generates those three values server-side
Expand Down
Loading
Loading