Skip to content

fix(stores): guard KV JSON parse and DO complete() write failures - #163

Merged
paveg merged 2 commits into
mainfrom
fix/store-corruption-guards
Jul 18, 2026
Merged

fix(stores): guard KV JSON parse and DO complete() write failures#163
paveg merged 2 commits into
mainfrom
fix/store-corruption-guards

Conversation

@paveg

@paveg paveg commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Summary

The adversarial review that corrected ADR 0005 (#162) surfaced two real unguarded failure paths in store adapters. This PR closes both, TDD-first (4 new tests written and confirmed failing before the fix).

  • kvStore: kv.get(key, { type: "json" }) parses inside the Cloudflare runtime and throws on corrupt stored data, so a corrupt record produced an uncaught exception (HTTP 500) instead of the documented degrade-to-miss semantics. All four read sites (get, lock initial read, lock read-back, complete) now go through a safeGetJson helper that treats corrupt data as absent; lock() overwrites the corrupt entry with a fresh lock.
  • durableObjectStore.complete(): storage.put() failure (non-cloneable value, quota error) propagated uncaught — middleware.ts calls store.complete() without a catch, so this crashed the request after the handler had already succeeded. Now a no-op: the response is served uncached and a retry with the same key re-executes the handler, matching the degradation policy of the serializing adapters.

Root-cause fix, not a workaround: the guard lives at the store's serialization boundary (KV's parse happens inside kv.get; DO's serialization happens inside storage.put), which is exactly where Redis/D1 already guard their JSON calls.

Includes a patch changeset and updates ADR 0005's known-gaps bullet to record the closure.

Test plan

  • New tests (Red confirmed before implementation): corrupt-JSON cases for KV get/lock/complete, and a throwing storage.put() in DO complete().
  • pnpm vitest run --coverage — 219/219 pass, 100% statements/branches/functions/lines maintained.
  • pnpm lint / pnpm typecheck — clean.

🤖 Generated with Claude Code

An adversarial review of ADR 0005 found two unguarded failure paths:

- kvStore: kv.get(key, { type: "json" }) parses inside the runtime and
  throws on corrupt data, surfacing as a 500 instead of the documented
  miss semantics. All four read sites now go through a guarded helper
  that treats corrupt data as absent (lock() overwrites it).
- durableObjectStore.complete(): storage.put() failure propagated
  uncaught through the middleware. Now a no-op — the response is served
  uncached so a retry with the same key re-executes the handler.

Both paths follow the existing degradation policy (ADR 0005): every
failure converges to "the request is processed again".

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 406f131

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
hono-idempotency Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@paveg, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 23 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 90828587-3582-4fe8-9aef-4ea22954547f

📥 Commits

Reviewing files that changed from the base of the PR and between d81f0bd and 406f131.

📒 Files selected for processing (6)
  • .changeset/store-corruption-guards.md
  • docs/adr/0005-graceful-json-guards-in-stores.md
  • src/stores/cloudflare-kv.ts
  • src/stores/durable-objects.ts
  • tests/stores/cloudflare-kv.test.ts
  • tests/stores/durable-objects.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/store-corruption-guards

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@paveg
paveg enabled auto-merge (squash) July 18, 2026 21:28
@paveg
paveg merged commit c1a83b9 into main Jul 18, 2026
10 checks passed
@paveg
paveg deleted the fix/store-corruption-guards branch July 18, 2026 21:29
@github-actions github-actions Bot mentioned this pull request Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant