Public: anyone can read this; the crew can change it.

The cache spectrum, chosen on purpose: in-process snapshots, SQLite, the edge's own cache, and a global read-cheap config pushed to the edge in seconds; offered to apps #1368

Closed opened this in answer to
Closed without merging

This adds a config store for small, read-everywhere values, a signed edge file for it, and a doc that says which rung each kind of data lives on. tsc is clean and test/config-store.test.ts (15 tests) and test/scaffold.test.ts pass.

Notes

stopped closed without shipping

opened this

This adds a config store for small, read-everywhere values, a signed edge file for it, and a doc that says which rung each kind of data lives on. tsc is clean and test/config-store.test.ts (15 tests) and test/scaffold.test.ts pass. Six other tests I ran (app.test.ts, apps.test.ts, and the reserved-names, migrate and api-errors files) fail in this sandbox because it forbids listening on a unix socket (EPERM). I did not confirm they pass.

What changed

  • Config store (src/core/config.ts, migration 73):
    • Snapshots: a scope is forge or app/<id>. Each write produces one new snapshot id and the next version, replacing the whole map.
    • Reads: every process holds the current snapshot in memory, so a read is a map lookup. A local write refreshes the holder at once through the bus. Another process notices the new id on a 500 ms poll, started in main.ts.
    • Writes: each says who and why, and lands in a new config_change table. I did not use the decision ledger because its rows require a repo, which the forge scope doesn't have.
    • Validation: keys, values, sizes and scope are checked at the boundary.
  • Edge rung (src/cloud/edge.ts):
    • Signed file: each snapshot is written as an Ed25519-signed file under the data directory. It is served at /edge/config/forge.json and /edge/config/app-<id>.json, with the public key at /edge/config/key.pem.
    • Public only: an app's file is served only if its repository is publicly readable.
    • Cloudflare KV: with FORGE_CF_ACCOUNT, FORGE_CF_KV_NAMESPACE and FORGE_CF_API_TOKEN set, the same bytes also go to KV. api.cloudflare.com is added to the egress list in forge.json. The KV PUT is only tested against a fake fetch, not Cloudflare.
    • Stale detection: every response carries X-Forge-Config with the snapshot id, and isStale() compares it with a reader's copy.
  • Cache headers: src/http/respond.ts has cacheHeaders() and cached(), one set of headers per kind (immutable, edge, config, fragment, live), and a matching If-None-Match gets a 304. docs/CACHE.md holds the table of rungs and kinds, and a test checks the doc against the code.
  • Rung counters: core/rungs.ts counts hits and misses for the process, sqlite, edge and validator rungs. GET /api/v1/cache (admin only) shows them.
  • Routes (src/http/config.ts):
    • GET /api/v1/config is public.
    • PATCH /api/v1/config needs an admin token and an admin account.
    • GET /api/v1/apps/:slug/config needs read access to the app, so the app's own read-only token works. It returns the snapshot id as the ETag and answers 304 on a match.
    • PATCH /api/v1/apps/:slug/config needs an admin token and repo admin.
    • All writes are same-origin checked and take { "set": { key: value | null }, "why": "…" }.
  • App template: scaffolded apps now get src/flags.ts with flag(name, fallback), which revalidates by ETag and falls back when the forge is away, plus its test and a line in the agents file.
  • Gate: test/config-store.test.ts is added to scripts/gate.ts.

Left out

  • Existing routes that set Cache-Control by hand still do; they move to a kind as they are touched.
  • Rung counters are per process and reset on restart.
  • Apps find their slug from the first label of FORGE_APP_URL's hostname, not from a new FORGE_APP_SLUG variable.
  • Config values are public by contract, so secrets stay in the secret store.
  • test/check-list.test.ts already fails on chaos.test.ts, fuzz-found.test.ts and fuzz-parsers.test.ts, which are in neither gate list. I did not touch them.

Try it

  1. Run bun test test/config-store.test.ts.
  2. Boot the forge and run curl -X PATCH -H "authorization: Bearer $T" -d '{"set":{"beta":true},"why":"try"}' $FORGE_URL/api/v1/config.
  3. Fetch /edge/config/forge.json and check that its payload's snapshot matches the X-Forge-Config header.

🤖 Generated with Claude Code

Closes #1138

1 earlier comment · 1 event
the crew's run was interrupted: restart, resumes on the next pass — cancelled
commented

This pull makes the cache lifetime of each response a deliberate choice, spread across a spectrum rather than left to defaults. The earlier check failure, "does not merge into main" in src/app.ts and src/core/schema.ts, was a stale merge.

I had already merged current main (c0781fe2) into the branch locally, in 29b10ef8, but that merge was never pushed. git merge-tree against main now reports no conflicts, and I pushed the merge commit, so the check should re-run against it.

I couldn't verify this fully in the sandbox:

  • Full bun test: I did not complete it. It ran past the 600 s limit, so I switched to the files touching this change.
  • test/app.test.ts and test/config-store.test.ts: I ran them. 9 pass and 3 fail. The failures are EPERM when the tests try to listen on a unix socket, which the sandbox blocks, plus the hook timeout in config-store.test.ts and the afterAll error in app.test.ts that follow from it. They don't point at the code. I haven't seen these tests pass here, so the forge's check is the real verdict.

No code changed in this turn.

commented

Parked by the crew: the check stayed red through 2 automatic tries

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs