← Context/ plans
Plan C3 — The Worker Backfills the Index From R2
Mail already in R2, from the laptop or from earlier Worker runs, gets indexed by the Worker itself, a batch at a time, with no laptop involved.
Spec: Attachments-as-First-Class-Mail, Cloud, step C3. Needs Plan-C1-D1-Cloud-Index and Plan-C2-Worker-Indexes-New-Mail merged.
Goal
Everything already in R2 gets into D1 without the laptop:
humain's 714 messages, pushed from the laptop on 2026-11-26;
the ~1,570 Gmail messages the Worker fetched before C2 existed;
later, the two large archives.
Read first
C2's
src/indexer.tsand its tests.src/sync.ts(store()shows the R2 layout:raw/<account>/<sha[:2]>/<sha>.emlandmeta/<account>/<encodeURIComponent(mailbox)>/<uidvalidity>/<uid>.json, whose JSON includessha256,uid,gmailLabels,gmailThreadId,internalDate,flags).src/index.ts(MailboxSync, Durable Object storage).The R2 blueprint
context-v/blueprints/Mail-Archive-in-Cloudflare-R2.md(bucket layout table).
Build
Extend the
Bucketinterface withlist({ prefix, cursor, limit })returning keys and a continuation cursor (R2'slist()shape), andget(key)returning bytes (arrayBuffer()), not only text. Update the in-memory test bucket.src/backfill.ts:backfillIndex(account, bucket, db, state, budget).stateis{ cursor?: string, done?: boolean, metaIndexed?: boolean }, kept in Durable Object storage underbackfill.First, once per account: list
meta/<account>/. For each JSON with asha256, keepsha → {mailbox, uidvalidity, uid, labels, threadId, internalDate, flags}in D1, in a small table added by a new migration0002_backfill_meta.sql(r2_meta (account, sha256, ...), primary key(account, sha256)). Then setmetaIndexed. (Listing every meta file each run would be slow for Gmail.)Then walk
raw/<account>/fromcursor. For each key: skip if D1 has amessagesrow for(account, sha256). Otherwise get the bytes and callindexMessagewith metadata fromr2_metawhen present, else nulls (mailbox, uid and uidvalidity stay NULL, as C1 allows).Stop after
budgetmessages indexed (default 500) or when the listing ends (done: true). Save the cursor after every page, so a killed run resumes cleanly.Run
refresh_noise's equivalent once at the end.
Wire it in. In
MailboxSync.#run, aftersyncAccount, ifINDEXis bound and the backfill isn't done, runbackfillIndex. Addbackfill: {indexed, done, lastError}tostatus.json. A backfill error never fails the sync.CPU and time. Measure with a test over the fixture set how long parsing takes per message. If 500 messages could exceed a Durable Object's default CPU limit, add
"limits": { "cpu_ms": 300000 }towrangler.jsonc(paid plan) and say so, or lower the default budget. Report the numbers either way.Tests (
test/backfill.test.ts, node Db, in-memory bucket):30 fixture messages in
raw/, 10 withmeta/JSON. One call with budget 12 indexes 12, a second continues, a third finishes, with no duplicates. The 10 with meta have uid, labels and thread id; the rest have NULLs.A message already indexed by C2's live path is skipped.
A corrupt
.emlis recorded aserrorand the walk continues.A thrown error mid-page leaves a cursor that resumes without gaps.
Rules for whoever builds this
Branch
cloud-c3-backfilloffdevelopment(after C2 is merged). Commit with the repo's conventions, ending with theCo-Authored-Byline. Don't push, don't merge.Never touch Cloudflare: no deploy, no
--remote. Tests run locally.Never read, print, or copy secrets. Never log subjects, bodies or filenames.
Don't write to
raw/. The backfill only reads R2 and writes D1 and Durable Object storage.No new dependencies.
Done when
pnpm testand the pinnedtsc --noEmitpass insync-worker/; the Python suite still passes.A local dry run: copy humain's 714
.emlfiles into a temp folder, point the in-memory bucket at it, runbackfillIndexwith budget 1000 against a node D1 stand-in, and compare the counts by kind withpython3 mail/kaymail.py attachments --backfill --account michael@humain.vc's report. Report the time it took.
Hand back
Branch and commits, test summary, the dry-run comparison and timing, and
whether cpu_ms needs raising. The parent session applies migration 0002
remotely, deploys, and after the next scheduled run checks status.json and
D1 counts for humain against the laptop (714 plus new arrivals).