Skip to content

fix(infra): guard delivery queue inflate against corrupted entry_json - #98354

Merged
vincentkoc merged 2 commits into
openclaw:mainfrom
Pick-cat:fix/delivery-queue-inflate-json-parse
Jul 1, 2026
Merged

vincentkoc merged 2 commits into
openclaw:mainfrom
Pick-cat:fix/delivery-queue-inflate-json-parse

Conversation

@Pick-cat

@Pick-cat Pick-cat commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where a single corrupted entry_json row in the SQLite delivery queue (session or outbound) crashes the entire pending-entry load, preventing delivery recovery from making progress. The inflate() helper calls JSON.parse(row.entry_json) without a try-catch — a corrupt row throws an uncaught SyntaxError that propagates through loadDeliveryQueueEntry (single load), loadDeliveryQueueEntries (batch load — one bad row kills all pending entries), updateDeliveryQueueEntry, and moveDeliveryQueueEntryToFailed. Callers in session-delivery recovery, outbound recovery, and the gateway restart sentinel do not catch this error, so a single corrupt SQLite row can block all queued delivery retries.

Why This Change Was Made

Guard inflate() itself — the shared chokepoint through which all queue reads flow. On parse failure it returns null. loadDeliveryQueueEntry already returns null for a missing row, so corrupt rows are treated the same way (unrecoverable → not found). loadDeliveryQueueEntries filters out null inflate results, so one corrupt row no longer discards every other pending entry in the same queue. updateDeliveryQueueEntry and moveDeliveryQueueEntryToFailed now throw a clean ENOENT (via loadDeliveryQueueEntry) instead of a raw SyntaxError, consistent with how they behave when an entry is genuinely absent. No API surface changes.

User Impact

No user-visible impact under normal operation. When entry_json is somehow corrupted (disk error, manual edit, partial write), delivery recovery now skips the bad entry instead of crashing. Recovery and gateway restart sentinel paths remain operational for the remaining valid entries.

Real behavior proof

  • Behavior addressed: a corrupt entry_json row in the SQLite delivery_queue_entries table must not crash delivery queue loads; the corrupt row should be treated as unrecoverable (null/skipped) while valid rows continue to load.
  • Real environment tested: Linux, Node v22.22.0, local source checkout on branch fix/delivery-queue-inflate-json-parse, driving the real exported loadDeliveryQueueEntry, loadDeliveryQueueEntries, updateDeliveryQueueEntry, and moveDeliveryQueueEntryToFailed from src/infra/delivery-queue-sqlite.ts against a real on-disk SQLite database (OPENCLAW_STATE_DIR). Corrupt rows are inserted directly via raw SQL (same write path as upsertDeliveryQueueEntry uses internally) and read back through the real exported loaders across the SQLite disk boundary. 18 assertions covering 6 scenarios.
  • Evidence after fix:
=== Real behavior proof: delivery queue inflate corrupt JSON guards ===

[case 1] loadDeliveryQueueEntry: corrupt row returns null
  ok: valid row returns entry with correct id
  ok: valid row has retryCount
  ok: corrupt row returns null (not SyntaxError)
  ok: nonexistent row returns null

[case 2] loadDeliveryQueueEntries: skips corrupt, returns only valid
  ok: returns 2 valid entries (corrupt skipped)
  ok: valid ids: s-ok-1, s-ok-2 (s-bad excluded)
  ok: outbound: 1 valid entry (corrupt skipped)
  ok: outbound valid id: o-ok

[case 3] all-corrupt queue
  ok: all-corrupt → empty array (not crash)

[case 4] updateDeliveryQueueEntry: corrupt → ENOENT (not SyntaxError)
  ok: throws ENOENT for corrupt row
  ok: error code is ENOENT

[case 5] moveDeliveryQueueEntryToFailed: corrupt → ENOENT (not SyntaxError)
  ok: throws ENOENT for corrupt row

[case 6] SQLite disk readback: round-trip across real DB boundary
  ok: round-trip: enqueuedAt preserved
  ok: round-trip: retryCount preserved
  ok: round-trip: lastError preserved
  ok: round-trip: lastAttemptAt preserved
  ok: round-trip: platformSendStartedAt preserved
  ok: round-trip: recoveryState preserved

=== Summary ===
ALL PROOF ASSERTIONS: 18 passed, 0 failed
  • Negative control (pre-fix behavior reproduced): reverting src/infra/delivery-queue-sqlite.ts to main (4ac5cf8636) and re-running the same proof harness crashes on case 1 with the uncaught SyntaxError — the corrupt row that returns null after the fix instead throws and aborts the entire batch:
=== NEGATIVE CONTROL: main version of delivery-queue-sqlite.ts ===
[case 1] loadDeliveryQueueEntry: corrupt row returns null
  ok: valid row returns entry with correct id
  ok: valid row has retryCount

SyntaxError: Expected property name or '}' in JSON at position 1 (line 1 column 2)
    at JSON.parse (<anonymous>)
    at inflate (src/infra/delivery-queue-sqlite.ts:62:14)
    at loadDeliveryQueueEntry (src/infra/delivery-queue-sqlite.ts:179:16)

exit=1

The negative control is load-bearing: the same proof harness, same corrupt rows, same disk boundary crashes on main and passes 18/18 after the fix. Restoring the fix from the branch (git checkout HEAD -- src/infra/delivery-queue-sqlite.ts) and re-running returns to 18/18 green.

  • What was not tested: a live gateway with real recovery loops processing corrupt entries end-to-end — no gateway was running. The proof drives the exact same exported SQLite loader functions that recovery uses (loadDeliveryQueueEntry ← loadPendingDelivery, loadDeliveryQueueEntries ← loadPendingDeliveries / loadPendingSessionDeliveries), against a real SQLite database across the disk boundary, which exercises the shared chokepoint.

Evidence

Regression tests (11 tests, all pass):

$ node scripts/run-vitest.mjs src/infra/delivery-queue-sqlite.test.ts --run
 Test Files  1 passed (1)
      Tests  11 passed (11)

Covers: corrupt single-row → null, corrupt-in-batch → skipped, all-corrupt-batch → empty array, update-with-corrupt → clean ENOENT, move-to-failed-with-corrupt → clean ENOENT, plus valid-entry round-trips (upsert/load, update, delete).

Existing tests — no regressions:

$ node scripts/run-vitest.mjs src/infra/outbound/delivery-queue.storage.test.ts --run
 Test Files  1 passed (1)
      Tests  12 passed (12)

$ node scripts/run-vitest.mjs src/infra/session-delivery-queue.storage.test.ts --run
 Test Files  1 passed (1)
      Tests  3 passed (3)

Lint: oxlint clean on both changed files.

AI-assisted.

@clawsweeper

clawsweeper Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 1, 2026, 12:02 AM ET / 04:02 UTC.

Summary
The PR changes the shared SQLite delivery queue inflater to return null on parse failures, filters corrupt rows from batch loads, and adds corrupt-row regression tests.

PR surface: Source +6, Tests +162. Total +168 across 2 files.

Reproducibility: yes. Current main and v2026.6.11 have an unguarded JSON.parse in the shared SQLite queue inflater, and the PR body includes a negative-control run showing the same corrupt row crashes on main; I did not execute tests in this read-only review.

Review metrics: 1 noteworthy metric.

  • Persistent Queue Read Path: 1 behavior changed. Malformed delivery_queue_entries.entry_json rows are now treated as unrecoverable per-row reads instead of aborting delivery recovery, so maintainers should notice the recovery semantics.

Stored data model
Persistent data-model change detected: serialized state: src/infra/delivery-queue-sqlite.ts. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Next step before merge

  • No automated repair lane is needed; the PR has no actionable review findings and is ready for normal maintainer merge gating.

Security
Cleared: The diff changes SQLite read handling and tests only; it adds no dependencies, workflows, scripts, permissions, secrets handling, or new code execution paths.

Review details

Best possible solution:

Land the shared inflate-boundary guard and regression tests through normal maintainer merge gating.

Do we have a high-confidence way to reproduce the issue?

Yes. Current main and v2026.6.11 have an unguarded JSON.parse in the shared SQLite queue inflater, and the PR body includes a negative-control run showing the same corrupt row crashes on main; I did not execute tests in this read-only review.

Is this the best way to solve the issue?

Yes. Guarding the shared SQLite inflate chokepoint is the narrowest maintainable fix; catching this separately in session and outbound recovery callers would duplicate policy and leave single-load/update paths exposed.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 63dc9201c6b6.

Label changes

Label justifications:

  • P2: The PR fixes a bounded durable delivery recovery bug where one malformed SQLite queue row can block queued retry recovery.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides after-fix copied terminal output from real exported queue loaders against an on-disk SQLite database, plus a negative-control crash on main.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides after-fix copied terminal output from real exported queue loaders against an on-disk SQLite database, plus a negative-control crash on main.
Evidence reviewed

PR surface:

Source +6, Tests +162. Total +168 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 9 3 +6
Tests 1 162 0 +162
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 171 3 +168

What I checked:

Likely related people:

  • steipete: Peter Steinberger authored and merged the PR that moved outbound and session delivery queues into the shared SQLite delivery_queue_entries table and added the original inflate helper. (role: SQLite delivery queue migration author and merger; confidence: high; commits: 1af4c035e4bc, 5a2fecb4fbe7, 756de077d9ef; files: src/infra/delivery-queue-sqlite.ts, src/infra/outbound/delivery-queue-storage.ts, src/infra/session-delivery-queue-storage.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jul 1, 2026
@Pick-cat

Pick-cat commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 1, 2026
@vincentkoc
vincentkoc merged commit 302524b into openclaw:main Jul 1, 2026
125 of 129 checks passed
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 1, 2026
…openclaw#98354)

* fix(infra): guard delivery queue inflate against corrupted entry_json

* fix(infra): use toSorted in delivery queue test
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 2, 2026
…openclaw#98354)

* fix(infra): guard delivery queue inflate against corrupted entry_json

* fix(infra): use toSorted in delivery queue test
Rorqualx pushed a commit to Rorqualx/cortex that referenced this pull request Jul 8, 2026
…openclaw#98354)

* fix(infra): guard delivery queue inflate against corrupted entry_json

* fix(infra): use toSorted in delivery queue test

(cherry picked from commit 302524b)
Rorqualx pushed a commit to Rorqualx/cortex that referenced this pull request Jul 15, 2026
…openclaw#98354)

* fix(infra): guard delivery queue inflate against corrupted entry_json

* fix(infra): use toSorted in delivery queue test

(cherry picked from commit 302524b)
wheakerd pushed a commit to wheakerd/clawdbot that referenced this pull request Jul 15, 2026
…openclaw#98354)

* fix(infra): guard delivery queue inflate against corrupted entry_json

* fix(infra): use toSorted in delivery queue test

(cherry picked from commit 302524b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants