All jobs

Add KV Caching to a Cloudflare Worker

An agent caches a D1 Worker's comment list in KV for 60 seconds, adds the write that clears it, and passes 7 vitest tests and wrangler deploy --dry-run.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads d1-template/wrangler.json, src/index.ts, the migrations and the repository's AGENTS.md, then the test setup of a sibling template, chanfana-openapi-template. d1-template only read comments from D1: it had no write for "invalidate on every write" to hook into, and no test script.

It binds a KV namespace CACHE in wrangler.json. listComments reads key comments:list first, and on a miss queries D1 and stores the result with expirationTtl: 60. A new POST /comments validates {author, content}, inserts, then deletes the key. Seven tests in test/index.test.ts run against the Worker with its D1 and KV bindings and no mocks.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add KV Caching to a Cloudflare Worker. In d1-template, cache the list query's result in a KV namespace for 60 seconds and invalidate it on every write.\" Do the work in this repository, add tests for it, run the template's test, type check and wrangler deploy --dry-run, and open a pull request that links back to the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

Tried on cloudflare/templates at 7ae5750: 11 files changed, 283 insertions(+), 154 deletions(-), including a regenerated pnpm-lock.yaml. On the untouched clone npm test failed with Missing script: "test", while npx tsc and wrangler deploy --dry-run passed. After the change pnpm test ended Tests 7 passed (7), npx tsc exited 0, and the dry run listed env.CACHE (00000000000000000000000000000000) KV Namespace.

For the reviewer: POST /comments was not asked for; it is the smallest write that makes invalidation real. The KV id is a placeholder of zeros until someone runs wrangler kv namespace create CACHE. d1-template/package-lock.json could not be regenerated, because npm 10.9.9 crashed with Cannot read properties of null (reading 'edgesOut'), so it is stale against the new devDependencies.

Run it on every Linear issue labeled agent

An automation with a linear trigger on linear.issue.labeled and the label agent runs the linear-label-to-pr recipe's prompt when someone in your Rush org labels an issue about a slow read. An issue that names the query, the TTL or the routes that write reaches the agent with those words; this run had to add a write because the template had none.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

Caching a read is the easy half. The run found the template had nothing to invalidate on, added the one write that makes the request testable, and tested the 60-second TTL and the clear-on-write path. It also named the race a delete after the insert leaves: a read during a write can cache stale rows again for up to 60 seconds.

Frequently asked questions

What does a real deploy need?

A namespace created with npx wrangler kv namespace create CACHE and its id in place of the zeros in wrangler.json; the README now says so. The run asks whether the template should use automatic provisioning instead.

Why pnpm and not npm?

npm install crashed inside npm 10.9.9, so the agent installed d1-template's dependencies with the monorepo's pnpm (pnpm install --filter d1-template...). The root postinstall failed on an unrelated astro-blog-starter-template#build, but d1-template's own checks ran.

More Cloudflare Workers jobs

More jobs like this: Turn a Linear label into a PR

Keep reading