Repository navigation
refactor(server): delete the KV read-through cache and its binding - #107
Conversation
The KV layer was the only cache in the system with no invalidation path. SahajCloud purges the Cloudflare edge by `Cache-Tag` on every write, but a tag purge cannot reach another Worker's KV, so an edit to `wm-web-config` or `wm-web-translations` could serve the old value here for a full day (`CacheTTL.SETTINGS` was 86400). A Worker subrequest to `cloud.sydevelopers.com` reads through that zone's cache, and every slug these functions read is covered by the Cache Rule (SahajCloud#555/#526 for the collections, #710 for the globals). So the reads keep a cache — one an editor's save can reach. The accepted cost is a Railway round trip every 600s per colo instead of once per TTL globally. `withCache` also supplied the retry, which nothing in the ticket replaces. `readCms` keeps it: `withRetry` for a public read, a bare call for a preview read, which must fail fast rather than make an editor wait through ~7s of backoff. Two tests pin that split, since the only thing that used to encode it was the `bypassCache` flag. `content-index.ts` needs no retry wrapper — `fetchContentIndexDocs` already degrades to `[]` on every failure, so nothing ever threw past it. Its `preview` option existed only to bypass KV and no caller passed it. Refs #98 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WVj9t7DQ59MW3eqKEJisCC
`server/CACHING.md` was the document that explained the KV layer, so it is the deliverable of #98, not an afterthought. It now leads with the edge cache and its purge-on-write, and names the three reads still on KV — `getAtlasSeo`, `getAtlasSitemapUrls`, `getContentSitemapUrls` — with the warning that nothing invalidates them. `kv-cache.ts` keeps only `CacheTTL.LIST`, the one constant a remaining caller uses, and its examples name atlas keys instead of the page keys it no longer writes. `docs/rules/rest-api.md` showed every new query function a `withCache` wrapper to copy. It now shows `readCms`, and says there is no cache key, TTL or purge step to add. `docs/local-environment.md` told a reader to wait out a 24 h TTL for a globals edit; that window is now 600s and a write purges it. Refs #98 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WVj9t7DQ59MW3eqKEJisCC
`/simplify` findings, applied. `withCache` kept `bypassCache` and `retryConfig` for callers that no longer exist — the three remaining ones pass `cacheKey`, `ttl` and `fetchFn` only. Both options go, along with the local `DEFAULT_RETRY_CONFIG` that duplicated `error-utils.ts`'s identical defaults. `generateCacheKey` loses its array branch for the same reason, which also removes the in-place `value.sort()` that `getRelatedLectures` had to guard against. `CacheTTL` was down to one member used by one caller, so the number moves to `sitemap-routes.ts` as `SITEMAP_DOCS_TTL`. `resolveContentIndex` had become a pass-through repeating a guard `fetchContentIndexDocs` already does, so its two callers now fetch and transform directly. `getWebConfig`'s `preview` flag was vestigial. Its inner client is a bare `createPayloadClient()` — no preview credentials — so the flag never changed the request. It existed to bypass a 24 h KV entry, and with that gone its only remaining effect was silently disabling retry for a preview render. Stale KV claims corrected where the branch's reads used to be cached: `api-routes.ts` said the related-content endpoints were "not cached upstream" — SahajCloud marks them edge-cacheable with `publicReadCacheHeaders` — and `sitemap-routes.ts`, `pages/[slug]/+data.ts` and `pages/lectures/[id]/_lecture.ts` each described a KV entry that is now an edge entry. The "do not add a KV caller" warning is stated once, in `CACHING.md`, with the other three surfaces linking to it. Refs #98 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WVj9t7DQ59MW3eqKEJisCC
Deploying wemeditate-design with
|
| Latest commit: |
afb2a77
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://0dff73e4.wm-design.pages.dev |
| Branch Preview URL: | https://claude-refactor-98-drop-kv-c.wm-design.pages.dev |
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
wemeditate-web | afb2a77 | Commit Preview URL Branch Preview URL |
Sep 18 2026, 12:44 AM |
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
A clean removal, honest about its gap, no refactor smuggled in. The whole benefit rests on two dashboard facts neither repo can assert — the starts_with "/api/globals/" term and the purge credentials. Confirm both before merge. Unconfirmed, this trades a stale read for an uncached one per render.
Findings
should-fix—server/content-index.ts:181— ameditationsblock reads/api/user-choices, which the upstream rule deliberately excludes.should-fix—server/cms-client.ts:625— "purged on write" is best-effort upstream, unconfirmed.consider—server/kv-cache.ts:7— my answer on criterion 2: keep the module, amend the ticket.consider—server/cms-client.ts:75—readCmsiswithRetryat 6 of its 10 call sites.
Checked: the full diff; withRetry's defaults; both surviving withCache callers, neither passing an array, so the narrowed key type is safe; SahajCloud@07e3e40 DEPLOYMENT.md §Edge Cache and §Globals, and its ContentIndexBlock.ts mapping meditations to /api/user-choices.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
The wrapper held two unrelated knobs for one caller each. `retry` was passed at a single call site, and `withRetry`'s own defaults already are the three attempts the other six sites wanted. Dropping it leaves one idea in the name: a preview read skips the retry. `getPageLocaleStatus` now calls `withRetry` directly for its single attempt, which is where that policy is legible anyway. Addresses #107 (comment) Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uvUiGWYGRSfYJ4kJMJ9e1
`CACHING.md` stated the tag purge on write as a fact this repo can rely on. Upstream it is best-effort: SahajCloud's `DEPLOYMENT.md` says an unset `CLOUDFLARE_ZONE_ID` or `CLOUDFLARE_CACHE_PURGE_TOKEN` makes every purge a silent no-op. With them unset the `s-maxage=600` TTL is the only thing bounding a stale read, and `getWebConfig` — which lost its preview bypass here — is the read that notices. Quote 600s as the worst case everywhere, and name the purge as what only shortens it. Addresses #107 (comment) Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uvUiGWYGRSfYJ4kJMJ9e1
|
Addressed the adversarial review, pushed 57e60e0.
Manual verification gained a step: confirm 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
Ardnived
left a comment
There was a problem hiding this comment.
readCms seems like a confusing function name for a method which is supposed to handle retry, plus isn't there already a withRetry function? Basically, readCms is a badly named function
`readCms` read as a policy name for what is only "retry, unless this is a preview read". The name now states that rule, so a reader does not have to open it to learn that `withRetry` is what it delegates to. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WdvLfJrbJQ4v3Bma4md2DD
|
Renamed
🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
|
@sydevs-bot Address the changes in this comment on the original ticket and update the PR accordingly. I am expecting the complete removal of the KV cache. |
Completes #98. The atlas reads and the content sitemap read were carved out when the ticket was written, on the ground that they needed their own argument. @Ardnived decided on 2026-09-17 that they do not: every staleness window shortens to the edge's 600s, none lengthens. `withCache` also supplied the retry, and all three of these reads degrade silently — `getAtlasSeo` to `null`, the two sitemap halves to `[]`. An unported retry would turn one upstream blip into a page missing its server-rendered half, or a sitemap missing a whole section, in a response that still answers 200. Each now calls `withRetry` directly, with the same ladder `withCache` ran on a miss: 3 attempts at 1s base delay. `getAtlasSeo`'s 404 branch returns rather than throws, so a dead route still costs one read instead of three. The two ⚠ notes went with the code they annotated. Both described KV behaviour alone: a stored `null` reading back as a miss, and the `content-sitemap-docs` key prefix guarding against a shape change in `getCachedResponse`. Neither has a referent now. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014ny1hhE4rfwasorqtcYHn3
CACHING.md loses the "what still uses KV" table and its inspection runbook, and gains the rule the removal makes load-bearing: a read that degrades silently is the one that most needs the retry the cache used to supply. The windows the removal shortened are recorded, since #98's argument was that none lengthens. AGENTS.md and server/AGENTS.md drop the binding from the Worker's description and from `pageContext`. rest-api.md says where a read with no preview variant gets its retry. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014ny1hhE4rfwasorqtcYHn3
|
Completed the removal, pushed afb2a77. Criterion 2's
Gate green: lint 0 errors, 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
The only conflict was the file header of `types/vike.d.ts`. Both sides rewrote the same line: #107 dropped "Cloudflare Workers bindings" with the KV binding it described, and this branch replaced "hooks such as onBeforeRoute" because a second hook now writes `locale`. The resolution keeps both edits. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TW8NJxcfXF2WkwXNQfV1cE
Cloudflare Pages is red on 797d9c9, and the build log lives only in the Cloudflare dashboard, which a routine cannot reach. The branch forked at 2af1870, three commits back, so the Pages build was still installing the pre-#111 dependency tree: 88 extra packages and a whole unused Next.js subtree. This merge builds against the tree main installs today. It also brings in the two server changes this PR's memos sit on top of: #107 drops `preview` from `getWebConfig`, which `loadSiteContext` calls, and #113 adds `+onCreatePageContext.server.ts`, the error-page path this PR's fork spec describes. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B9MPJ8NWXhK85k27xRq6Ji
* test: drop leftovers from the KV cache removal and an unused smoke helper - `kv: undefined` in two sahajcloud-context mocks: the context lost its kv field in #107, so the mocks described a shape that no longer exists. - Three retry tests titled "…since the KV layer used to supply that": the KV layer is gone, so the titles now state only the behaviour. - `headTags()` in the smoke preview helper: exported in #28 and never called. The audit found no migration-era test files left; #87, #99 and #107 already removed them with their code. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(server): drop the stale KV binding claim `getSahajCloudContext()` returns `{ apiKey, baseURL }`, and `SahajCloudEnv.Bindings` carries no KV namespace — the binding went with `server/kv-cache.ts` in #98. The docblock still promised a reader that the helper resolves one, which is the same leftover this PR deletes from the file's own test mock. Wording now matches `sitemap-routes.ts`. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjXde9tn99Z82seiuKd * fix(components): call the related fetch edge-cached Nothing in this Worker holds persistent state (server/CACHING.md). The related endpoints are cached by the Cloudflare zone in front of SahajCloud, which sets `s-maxage` and `Cache-Tag` through `publicReadCacheHeaders`. The KV namespace these docblocks named went away in #98, so "KV-cached" pointed a reader at a cache they could neither inspect nor purge. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjXde9tn99Z82seiuKd --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Summary
server/kv-cache.ts, theWEMEDITATE_CACHEbinding,AtlasCacheTTLandCacheTTLare deleted. This Worker now holds no persistent state at all.withCachealso supplied the retry. Every read it wrapped keeps it, so the removal costs no resilience.Changes
server/cms-client.ts,server/content-index.ts— collection and global reads dropwithCache/generateCacheKey/CacheTTL. Public reads go throughwithRetryUnlessPreview, which iswithRetryfor a public read and a bare call for a preview one;getPageLocaleStatuskeeps its single attempt.server/atlas-client.ts—getAtlasSeoandgetAtlasSitemapUrlscallwithRetrydirectly.AtlasCacheTTLis gone with the cache it configured.server/sitemap-routes.ts—getContentSitemapUrlslikewise, andSITEMAP_DOCS_TTLwith it.server/kv-cache.ts,wrangler.toml,server/cms-context.ts,types/vike.d.ts— the module, the binding,CmsContext.kvand thepageContext.cloudflaretype all deleted.server/CACHING.md,AGENTS.md,server/AGENTS.md,docs/rules/rest-api.md— the edge is the only cache, and a read that degrades silently still needswithRetry.Acceptance criteria
server/goes through KVgrep -rn "WEMEDITATE_CACHE" .finds nothing outside the changelogCACHING.md, which records the removal)wm-web-configedit is visible within the edge TTLpnpm typecheckandpnpm test:runpass;CACHING.mddescribes the edge as the only cachePreview
https://claude-refactor-98-drop-kv-cache-wemeditate-web.contact-c66.workers.dev
Test results
pnpm lint(0 errors),tsc --noEmit,pnpm test:run646 passedpnpm build(the binding's removal does not break the Worker bundle)withRetrycall removed, then passing with it restoredManual verification
/sitemap.xmlon the preview, and confirm both still render with their content. These two lose their KV layer last. A 404, or a sitemap with no/map/rows and no page rows, is how that removal fails.starts_with "/api/globals/"term is live on the "Client read edge cache (Vary: Authorization)" Cache Rule. SahajCloud'sDEPLOYMENT.md§"Globals are cacheable reads too (#710)" records that it is needed; only the dashboard proves it is there. Without it the two globals lose all caching rather than gaining a shorter window.CLOUDFLARE_ZONE_IDandCLOUDFLARE_CACHE_PURGE_TOKENare set on SahajCloud's Railway production service. Unset, every tag purge is a silent no-op and 600s is the only bound on a stale read. The docs here say 600s rather than "immediate", so this confirmation improves the guarantee instead of rescuing it.cf-cache-status: HITon the CMS subrequest, then edit that document in SahajCloud and confirm a third request reflects it (acceptance criterion 3).Notes for reviewer
One cost the ticket's table does not name. A KV hit used to skip the retry. Now every request can spend it, so during an upstream outage
/sitemap.xmland an atlas route hold a Worker request for ~7s instead of serving a stale copy, on every request rather than once per TTL. The edge absorbs the repeat once a response is cacheable, but an outage produces no cacheable response. This follows from the decision rather than questioning it — flagging it because it is the one property that got worse, and the accepted-cost table only covers staleness windows./api/user-choicesis not in the Cache Rule, so one content-index block reads uncached. Filed upstream as sydevs/SahajCloud#804, which asks for the oneeqterm or a recorded decision that the path staysDYNAMIC. SahajCloud'sDEPLOYMENT.mdnamesuser-choicesas the deliberate exclusion from the rule's slug list, and ameditationscontent-index block resolves toGET /api/user-choices?…(ContentIndexBlock.ts'sCONTENT_TYPE_CONFIG), so that read loses its 30-minute KV entry and gains nothing. #98's body is corrected to match.Dismissed review findings:
withRetry({ maxAttempts: 1 })so a failed preview read reaches Sentry. Skipped — it adds Sentry events for errors an editor is already looking at, which is a behaviour change this ticket did not ask for.maxAttemptsat 2 for the readers that degrade to[]. Skipped — same ladder as before, only reached more often; changing it is a separate tuning decision.Filed on the way: #108 —
loadSiteContext's per-request memo is keyed on Vike'spageContext, which is a freshProxyper hook, so it misses betweendata()andonBeforeRender(). Pre-existing, and this branch makes each miss a subrequest instead of a KV hit.Detail
Why the edge covers these reads. Verified against
sydevs/SahajCloudat07e3e40, not assumed:src/plugins/cache/policy.ts—CACHE_TTLS.collectionscoverspages,meditations,lectures,songs,albums,images,app-cards,regions,audiences,events;CACHE_TTLS.globalscoverswm-web-configandwm-web-translationsat 600s.DEPLOYMENT.md§"Edge Cache" — the live rule matches/api/<slug>and/api/<slug>/…, which is what covers the nested custom endpoints this repo reads:/api/meditations/:id/songs,/api/lectures/:id/related-meditations,/api/meditations/:id/related-lectures,/api/lectures/for-audience. Each of those handlers emitspublicReadCacheHeadersupstream, so they carry a reals-maxageandCache-Tag.What bounds staleness.
s-maxage=600, and nothing else this repo can assert. SahajCloud purges theCache-Tagon write only whenCLOUDFLARE_ZONE_IDandCLOUDFLARE_CACHE_PURGE_TOKENare set; unset, the purge is a silent no-op (DEPLOYMENT.md§Edge Cache). Every doc here quotes 600s as the worst case and names the purge as what only shortens it.Every window the removal touched, and its direction. A region 3600s → 600s, a class 900s → 600s, the atlas sitemap 3600s → 600s, the content sitemap 1800s → 600s, CMS collections and globals 30min–24h → 600s. None lengthens.
What the retry preserves.
withCacheranwithRetry(fetchFn, { maxAttempts: 3, baseDelayMs: 1000 })on a miss and skipped it entirely underbypassCache.withRetry's own defaults are identical, so each ported read is byte-equivalent to the old miss path.getAtlasSeo's 404 branch returns rather than throws, so a dead atlas route still costs one read instead of three — pinned by a test.Why the retry matters most here. All three ported reads degrade silently:
getAtlasSeotonull, both sitemap halves to[], each inside a response that still answers 200. Dropping the retry with the cache would have turned one upstream blip into an atlas page missing its server-rendered half, or a sitemap missing a whole section, with nothing in the output to say so.The two
⚠notes went with the code they annotated. Both described KV behaviour alone — a storednullreading back as a miss ingetAtlasSeo, and thecontent-sitemap-docskey prefix guarding against a shape change ingetCachedResponse. Neither has a referent now thatkv-cache.tsis deleted.Security review: run (the diff touches
^server/and^wrangler.toml). No findings. The change is purely subtractive on the credential path: cache keys never encoded the API key,getWebConfignever sent preview credentials ordraft, and removing the binding narrows what the Worker can reach rather than widening it. No preview-scoped read was reachable from a cached path.Closes #98
🤖 Written by the sydevs autonomous loop — see what this is