diff --git a/kody/headless-api/AGENTS.md b/kody/headless-api/AGENTS.md index 157c246b..5c82718c 100644 --- a/kody/headless-api/AGENTS.md +++ b/kody/headless-api/AGENTS.md @@ -45,11 +45,16 @@ write; `GET` reads it back after approval; `POST /api/mcp` lists tools. `viewer` is denied (403). API-key/token issuance (`createApiKey`) is a separate path. - **Writes stage into governance** — they are invisible until `approve()`. -- **`kody.jobs` field schema is best-effort** (`entry` + cron `schedule`). - Confirm exact field names with `repoRunChecks` / the jobs guide on go-day; - the handlers themselves are verified. -- **`pg` over workerd TCP is unproven (U2).** If egress fails on Kody, swap the - pool for a Neon serverless driver — config-side only, because the store types - `PgPoolLike` as a structural interface (no package change). +- **`kody.jobs` schema is confirmed** (validated by `kody dev`): `entry: + "./src/jobs.ts#"`, `schedule: { "type": "interval", "every": "5m" }` (or + `{ "type": "cron", "expression": "<5-field>" }`). Bare cron strings and + `type: "once"` are rejected. +- **`pg` does NOT load under Kody workerd (U2, confirmed via `kody dev`):** + a DB request fails with `No such module "pg"`. Swap the pool for Neon's + serverless driver behind the store's `PgPoolLike` interface — config-side + only, no store-postgres change. Everything else bundles + runs under workerd. +- **Run `kody dev` from a standalone checkout, not nested in the monorepo** — + nesting fails the snapshot with a misleading `bundle target(s) missing` error. + See GO-DAY.md. - `node_modules/` here is local symlink scaffolding for the smoke only; the real deps resolve from the pkg-pr-new URLs in `package.json` on Kody. diff --git a/kody/headless-api/GO-DAY.md b/kody/headless-api/GO-DAY.md index 7a56619d..903d0a9f 100644 --- a/kody/headless-api/GO-DAY.md +++ b/kody/headless-api/GO-DAY.md @@ -1,40 +1,58 @@ # Go-day deltas — headless-api -Everything in this package is built and **locally validated** (real Postgres, -`scripts/local-smoke.mts` → SMOKE PASS) against the workspace packages. What -remains is only what requires the external blockers to clear. +Built and validated two ways: (1) node + real Postgres via +`scripts/local-smoke.mts` → SMOKE PASS (CMS logic, auth, governance, jobs); +(2) **`kody dev` (local workerd)** — see "What kody dev proved" below. + +## What `kody dev` already proved (local workerd, no publish) + +- ✅ **The CMS graph bundles and boots under workerd** (same esbuild path as + publish). The `{ fetch }` entry loads and the CMS API handler serves — `GET /` + returns the CMS's own `404 {"code":"NOT_FOUND"}`. +- ✅ **`kody.jobs` manifest schema confirmed** (kody dev validated it): + `"schedule": { "type": "interval", "every": "5m" }` (or + `{ "type": "cron", "expression": "<5-field>" }`), `entry: + "./src/jobs.ts#"`. A bare cron string and `schedule.type: "once"` are + rejected. +- ⚠️ **U2 answer (local):** the first DB-touching request fails under workerd + with **`Error: No such module "pg"`** during `migrateTo`. The stock `pg` + driver does not load under Kody workerd. Everything upstream of the DB works. + +## Must run `kody dev` from a STANDALONE checkout + +`kody dev` snapshots the package via its git repo. Run inside this monorepo it +fails with `Package bundle target(s) missing from the repo session snapshot: +src/jobs.ts#...` — a **nesting artifact, not a real error**. In a standalone +git repo (how the go-day git lane works) the same files build cleanly. For local +iteration, copy the package to its own dir + `git init` (see the scratch at +`/tmp/kody-standalone` used during the spike), with `node_modules/@tanstack/*` +symlinked to the workspace packages and `node_modules/pg` to the pnpm store. ## Blocked until the Preview builds land 1. **Pin dependency URLs.** Replace `@REF` in `package.json` for the four - `@tanstack/cms-*` deps with the pkg-pr-new refs the Preview CI job reports - (spec §7-1). `pg` stays an npm dep. + `@tanstack/cms-*` deps with the pkg-pr-new refs. `pg` stays declared. ## Blocked on Kody (App install + `package:write` in the tanstack org) -2. **Publish via the git lane** (D3): `packageGetGitRemote({ create: true, - name: '@tanstack/cms-starter-headless-api' })` → clone → copy this package in - → push → `packagePublishExternalPush`. If it returns `locked`, open the - `approval_url`. -3. **Set the `CMS_DATABASE_URL` package secret** to the prod Postgres string. -4. **Resolve U1 (secret accessor).** Fill `src/secrets.ts#getDatabaseUrl` with - the synchronous `kody:runtime` package-secret read. Confirm whether the value - is available synchronously at module scope; if only request-scoped, capture - it into the module cache on the first request (the resolver is sync by - design — store types.ts:85). -5. **Confirm `kody.jobs` field schema** via `repoRunChecks` (the manifest - validator). Current best-effort shape: `{ entry: "./src/jobs.ts#", - schedule: "*/5 * * * *" }`. -6. **Run U2 test** (spec §7-5): the first REST call on the published app is the - pg-over-workerd-TCP test. If egress fails, swap the pool for Neon's serverless - driver — config-side only (`PgPoolLike` is a structural interface). +2. **Resolve U2 — swap `pg` for a workerd-native driver.** Confirmed required + (above). Use Neon's serverless driver behind the store's `PgPoolLike` + interface — config-side only, no store-postgres change (the store types the + pool structurally). Then re-run `kody dev` against a reachable Postgres and + confirm a DB-backed request + a job succeed. +3. **Resolve U1 — secret accessor.** Fill `src/secrets.ts#getDatabaseUrl` with a + synchronous `kody:runtime` package-secret read. NB under `kody dev --target + mock` package secrets are **placeholders** by design, so a real DB URL can't + flow through them locally — test U1 against the published app or with a + `.kody/mocks.ts`-style stand-in. +4. **Publish via the git lane** (D3) and set the `CMS_DATABASE_URL` package + secret. +5. **Public read for §8.** Grant anonymous read on `contentItem` so the seeded + doc is readable over REST with no `x-cms-role` header (the `seed` itself is + in place and validated). ## Post-publish acceptance (spec §8) `appFetch` (`synthetic: true`): seeded doc readable over REST with no auth; `x-cms-role` write succeeds and is attributed; MCP tool list answers; a second package importing `@tanstack/cms-client` reads the same document. - -> Note: the §8 "seeded document readable with **no auth**" check needs a seed + -> public read access — not yet added here (the smoke uses authenticated writes). -> Add a small `seed` and confirm anonymous read on go-day. diff --git a/kody/headless-api/package.json b/kody/headless-api/package.json index 0135b714..a230aece 100644 --- a/kody/headless-api/package.json +++ b/kody/headless-api/package.json @@ -29,11 +29,17 @@ "jobs": { "cms-outbox-drain": { "entry": "./src/jobs.ts#outboxDrain", - "schedule": "*/5 * * * *" + "schedule": { + "type": "interval", + "every": "5m" + } }, "cms-scheduler-tick": { "entry": "./src/jobs.ts#schedulerTick", - "schedule": "*/5 * * * *" + "schedule": { + "type": "interval", + "every": "5m" + } } } } diff --git a/kody/headless-api/scripts/local-smoke.mts b/kody/headless-api/scripts/local-smoke.mts index 27e684af..8bd6288c 100644 --- a/kody/headless-api/scripts/local-smoke.mts +++ b/kody/headless-api/scripts/local-smoke.mts @@ -35,6 +35,23 @@ async function post(op: string, role: string, body: unknown) { ) } +// 0. Seed: a fresh store is seeded on first registration (spec §8). +await entry.fetch(new Request(`${ops}/noop`, { method: 'GET' })).catch(() => {}) +const seeded = + ( + await getAs(systemContext('smoke'), { + collection: 'contentItem', + id: 'welcome', + staged: false, + populate: false, + }) + )?.doc ?? null +check( + 'seed document present on fresh store', + seeded?.title === 'Welcome', + seeded, +) + // 1. Editor stages a write over REST (header-role auth) through the fetch entry. const writeRes = await post('save_document', 'editor', { collection: 'contentItem', diff --git a/kody/headless-api/src/cms.config.ts b/kody/headless-api/src/cms.config.ts index fe8024ef..d941388a 100644 --- a/kody/headless-api/src/cms.config.ts +++ b/kody/headless-api/src/cms.config.ts @@ -3,6 +3,7 @@ import type { CmsConfig } from '@tanstack/cms-core' import { createPostgresStorage } from '@tanstack/cms-store-postgres' import { definition } from './cms.definition' import { getDatabaseUrl } from './secrets' +import { seed } from './seed' // Kody variant of starters/headless-api. Differences from the monorepo starter // (spec §3.1): no `dataDir`/embedded storage — external Postgres via the @@ -22,6 +23,7 @@ export function registerHeadlessCms() { return defineCms(definition, { blockTypes: [], auth, + seed, // `connectionString` accepts a resolver, so the secret stays out of the // build and is read at connect time (store-postgres types.ts:85). storage: createPostgresStorage({ diff --git a/kody/headless-api/src/seed.ts b/kody/headless-api/src/seed.ts new file mode 100644 index 00000000..d745e5a7 --- /dev/null +++ b/kody/headless-api/src/seed.ts @@ -0,0 +1,19 @@ +import type { SeedStore } from '@tanstack/cms-core' + +// Content written on first run against an empty store, so a freshly-published +// app isn't empty (spec §8). Published so it is live immediately. Public (no +// auth) read access is still a go-day item — see GO-DAY.md. +export const seed: SeedStore = { + documents: { + contentItem: { + welcome: { + title: 'Welcome', + slug: 'welcome', + channel: 'web', + priority: 1, + body: 'This headless CMS serves one schema over REST, a typed client, and MCP.', + published: true, + }, + }, + }, +}