← Context/ plans
Plan C1 — A D1 Index With the Laptop's Tables
Give the sync Worker a database: the same messages and attachments tables the laptop has, in Cloudflare D1, ready for the Worker to fill.
Spec: Attachments-as-First-Class-Mail, section Cloud: the same index on Cloudflare, step C1. Next: Plan-C2-Worker-Indexes-New-Mail.
Goal
The sync Worker (sync-worker/) gets a D1 database bound as INDEX, with the
same tables as the laptop's index.sqlite, created by a migration file. Nothing
writes to it yet; that's C2.
Before handing off (parent session)
Create the database and check that D1 runs FTS5:
BASHcd sync-worker ./cf.sh d1 create kay-os-mail-index # note the database_id it prints ./cf.sh d1 execute kay-os-mail-index --remote \ --command "CREATE VIRTUAL TABLE fts_probe USING fts5(x); DROP TABLE fts_probe;"Give the builder the
database_id. If the FTS5 probe fails, stop: the spec needs a different keyword-search plan before C1 goes ahead.
Read first
mail/kaymail.py:SCHEMA(the source of truth for table shapes).sync-worker/wrangler.jsonc,src/index.ts,src/sync.ts,test/sync.test.ts(thememoryBucketpattern: small interfaces, in-memory fakes, no Cloudflare in tests).sync-worker/src/cloudflare-workers.d.ts(hand-written runtime types; extend it rather than adding@cloudflare/workers-types).
Build
sync-worker/migrations/0001_index.sql. The five tables and their indexes, copied fromkaymail.SCHEMA, with exactly two differences, each commented in the file:messages.uidvalidity,messages.uidandmessages.mailboxmay be NULL. Mail the laptop fetched has no Gmail UID in R2, and C3 indexes it from headers alone. Uniqueness for those rows comes from(account, sha256), checked by the code.A comment that
messages.bodyholds at most 20,000 characters (enforced by the writer in C2, not by SQL).
wrangler.jsonc. Add"d1_databases": [{ "binding": "INDEX", "database_name": "kay-os-mail-index", "database_id": "<from parent>", "migrations_dir": "migrations" }]. AddINDEXtoEnvinsrc/index.ts, unused for now.src/db.ts. A minimalDbinterface matching the D1 calls C2 and C3 need (prepare(sql).bind(...).run()/first()/all()andbatch([...])), plus the D1 type declarations incloudflare-workers.d.ts.test/sqlite-db.ts. ADbbuilt onnode:sqlite(Node 24 has it), that applies every file inmigrations/in order. C2 and C3 tests use it.test/db.test.ts. Applies the migrations to an in-memory database; inserts a message, an FTS row, an attachment, and a link; runs an FTS5MATCH; runs abatchand checks it's atomic (a failing statement rolls back the others).Schema parity test, in Python (
mail/test_kaymail.py): loadsync-worker/migrations/*.sqlinto an in-memory SQLite and comparePRAGMA table_infofor the five tables against a database built fromkaymail.SCHEMA. Columns and types must match; only the documented nullable columns may differ. This is what stops the two indexes drifting.
Rules for whoever builds this
Work on a branch
cloud-c1-d1-indexoffdevelopmentinai-labs/kay-os. Commit with the repo's conventions (seegit log), ending with theCo-Authored-Byline. Don't push, don't merge.Never touch Cloudflare: no
cf.sh deploy, no--remote, nod1 create. The parent session does every remote step.Never read, print, or copy secrets (
.env, Doppler,accounts.toml).No new npm dependencies in this step.
kaymail.pybehaviour must not change. If the schema parity test exposes a real mismatch, report it instead of editingSCHEMA.
Done when
cd sync-worker && pnpm testpasses, includingdb.test.ts.npx -y -p typescript@5.9.3 tsc --noEmitpasses insync-worker/.python3 -m unittest mail/test_kaymail.py mail/test_kaymcp.pypasses, including the parity test../cf.sh d1 migrations apply kay-os-mail-index --localworks (local only).
Hand back
The branch name and commit, the test output summary, and anything in
kaymail.SCHEMA that didn't translate cleanly. Then the parent session applies
the migration remotely (./cf.sh d1 migrations apply kay-os-mail-index
--remote), checks SELECT name FROM sqlite_master, and merges.