Repository navigation
fix(infra): wire session-delivery drain recovery guard onto the shared SQLite recovery_state column - #88885
fix(infra): wire session-delivery drain recovery guard onto the shared SQLite recovery_state column#88885Feelw00 wants to merge 1 commit into
Conversation
|
Codex review: needs changes before merge. Reviewed June 29, 2026, 6:02 PM ET / 22:02 UTC. Summary PR surface: Source +138, Tests +187. Total +325 across 4 files. Reproducibility: yes. Source inspection and the supplied terminal proof give a high-confidence path: enqueue an agentTurn, let delivery succeed without completing ackSessionDelivery, then run recovery again and observe current main replay the pending entry. Review metrics: 1 noteworthy metric.
Stored data model Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Risk before merge
Maintainer options:
Copy recommended automerge instructionNext step before merge
Security Review findings
Review detailsBest possible solution: Use the existing afterRecord boundary after inbound recording succeeds and before dispatch starts, add a regression for record/pre-dispatch failure, then have maintainers explicitly accept fail-safe refusal or pause for adapter reconciliation design. Do we have a high-confidence way to reproduce the issue? Yes. Source inspection and the supplied terminal proof give a high-confidence path: enqueue an agentTurn, let delivery succeed without completing ackSessionDelivery, then run recovery again and observe current main replay the pending entry. Is this the best way to solve the issue? No, not yet. Reusing the shared SQLite recovery_state column is the right bounded direction, but this patch sets the marker before the kernel’s inbound-recording boundary, so the marker must move to afterRecord before the solution is the best fix. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 843ad143647e. Label changesLabel justifications:
Evidence reviewedPR surface: Source +138, Tests +187. Total +325 across 4 files. View PR surface stats
Acceptance criteria:
What I checked:
Likely related people:
What the crustacean ranks mean
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
Review history (1 earlier review cycle)
|
|
This fail-closed direction makes sense to me for the duplicate-send risk. I do think one extra regression would strengthen it: a case where |
ce765f0 to
501944c
Compare
|
Thanks for the review. Addressed the [P1] marker-clearing finding in 501944c50a. [P1] Keep the send marker when delivery may have thrown after sending — fixed. The marker is no longer set up front and cleared on every thrown deliver. Instead the deliver seam ( [P1] At-most-once tradeoff for the crash window — agreed, and intentional. It is documented under Risks and Mitigations and is now scoped to the narrow window after the turn starts but before ack; pre-send / busy / no-op cases stay retryable. A precise adapter-side On the two red checks ( @clawsweeper review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
501944c to
4a7a072
Compare
|
Thanks @byungskers — added in 4a7a072873. Two regressions now cover the retryable-on-throw paths: Note: since your review the clearing was moved out of the generic thrown-deliver catch (which would have replayed a sent-before-error turn) — the marker is now set at the turn-execution boundary, cleared only on a busy-deferred attempt, and a turn that began running is refused. @clawsweeper review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
…its turn has run The session-delivery recovery path drainQueuedEntry (src/infra/session-delivery-queue-recovery.ts) delivers an entry then acks it (at-least-once). If the agent turn re-runs (dispatchAssembledChannelTurn, which can send via the message tool) but the process dies before ack, the entry stays pending and the next recovery blind-replays it: the same turn runs twice (non-idempotent LLM/tool side effects) and its reply is sent twice. openclaw#88665 moved both delivery queues onto a shared SQLite backing with a recovery_state column; the parallel outbound queue uses it to refuse a blind replay without adapter reconciliation, but the session-delivery drain neither set nor read it. Wire recovery_state into the session drain at the turn-execution boundary so the refuse decision is precise: - The deliver seam (server-restart-sentinel.ts) reports onSendAttemptStart right before the agent turn runs, and onSendDeferred if that attempt was merely deferred because the session was busy (the busy turn produces a busy-retry response, no durable work). The no-op deliver paths (systemEvent / changed-session / no-route) never run the turn and report neither. - drainQueuedEntry persists the send marker on onSendAttemptStart (and clears it on onSendDeferred). A recovered entry still carrying the marker is refused (fail-safe moveToFailed). On a thrown deliver, a turn that began running (sendAttempted) is refused even if it threw after sending, while a pre-send / busy-deferred / no-op failure stays retryable (at-least-once preserved for genuine pre-send failures). This matches the outbound queue's no-reconcile contract and addresses the sent-before-error case (a turn that sends then throws is no longer cleared and replayed). Tradeoff: a crash in the narrow window after the turn starts but before ack moves the entry to failed/ rather than replaying it (fail-safe over at-least-once), the same product choice the outbound queue makes without a reconcile capability. Adds regression tests: crash-before-ack, recovered-marker refuse, sent-before-error throw refuse, and pre-send failure stays retryable. AI-assisted: drafted with claude code (claude-opus-4-8). Co-Authored-By: Claude Opus 4.8 <[email protected]>
4a7a072 to
a8e7e8c
Compare
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Summary
drainQueuedEntry(src/infra/session-delivery-queue-recovery.ts) runsawait deliver(entry)thenackSessionDelivery. If the agent turn re-runs (dispatchAssembledChannelTurn, which can send via the message tool) but the process dies before the ack, the entry stays pending and the next recovery re-delivers it.agentTurnruns twice (non-idempotent LLM/tool side effects) and its reply is sent twice. Restart continuations are, by definition, on the crash/restart path.recovery_statecolumn (#88665already added it) into the session drain, at the turn-execution boundary. The deliver seam (server-restart-sentinel.ts) reportsonSendAttemptStartright before the agent turn runs, andonSendDeferredif that attempt is merely deferred because the session is busy.drainQueuedEntrypersists the marker ononSendAttemptStartand refuses a recovered marked entry (fail-safe move tofailed/). On a thrown deliver, a turn that began running is refused even if it threw after sending; a pre-send / busy-deferred / no-op failure stays retryable.recovery_statecolumn itself already exists (#88665). Genuine pre-send failures still retry (at-least-once preserved); the no-op deliver paths (systemEvent/ changed-session / no-route) never mark and stay retryable.Change Type
Bug fix (reliability / crash recovery).
Scope
src/infra/(session-delivery queue recovery + storage) andsrc/gateway/server-restart-sentinel.ts(the deliver seam reports the send boundary). No owner-gated paths touched.Linked Issue
Closes #88015
Root Cause
The session-delivery queue is at-least-once:
drainQueuedEntrydelivers then acks. There was no durable marker recording that a delivery's turn had begun, so recovery could not distinguish "never ran" from "ran but not yet acked." The parallel outbound queue already guards exactly this with arecovery_statemarker and refuses a blind replay on recovery without adapter reconciliation.#88665moved both queues onto a shared SQLite backing carrying arecovery_statecolumn, but the session-delivery drain neither set nor read it.Regression Test Plan
Four tests in
src/infra/session-delivery-queue.recovery.test.ts(profile: infra), RED before / GREEN after:does not re-deliver a session entry whose delivery succeeded but ack was interrupted by a crash— the turn runs (signalsonSendAttemptStart); the ack throws once; a second recovery pass must not replay it.refuses to replay an entry recovered while still carrying the send marker— durablesend_attempt_startedon a pending entry (the cross-restart scenario); recovery refuses (nodeliver, moved tofailed/).refuses to replay a turn that sent before throwing (sent-before-error)— the turn signalsonSendAttemptStartthen throws; recovery must not treat it as pre-send and replay it.retries a pre-send failure that never started the turn—deliverthrows beforeonSendAttemptStart; the entry stays pending and retryable with no marker (at-least-once preserved).These exercise the SQLite-backed queue and require Node 24+ (
node:sqliteStatementSync.columns), matching the repo CI test lane.Security Impact
None. No new permission, secret, or network call; no change to data scope or the adapter surface. The only behavioral change is fail-safe refusal of a blind replay on recovery.
Repro + Verification
OPENCLAW_HOME/OPENCLAW_STATE_DIR.agentTurnsession delivery, let the turn run (mark) without acking (crash-before-ack), then runrecoverPendingSessionDeliveriesagain.failed/.Evidence
Regression tests RED before / GREEN after (see Regression Test Plan). Local gates on this branch:
pnpm buildexit 0, typecheck clean,pnpm checkexit 0; target testssession-delivery-queue.recovery+.storage+server-restart-sentinelpass on Node 24. The Real behavior proof below measuresdeliver()invocation count across a crash-before-ack + restart sequence on the SQLite-backed queue.Human Verification
Compared the rewritten recovery flow against the outbound-queue sibling (
src/infra/outbound/delivery-queue-recovery.ts) to confirm the marker/refuse contract matches; confirmed the busy continuation path (which produces a busy-retry response, no durable work) clears the marker so it stays retryable; traced the production caller (recoverPendingSessionDeliveries←server-restart-sentinel.ts,deliverQueuedSessionDelivery→dispatchAssembledChannelTurn) to place theonSendAttemptStartboundary at the actual turn execution.Review Conversations
New PR — earlier internal review flagged that clearing the marker on every thrown deliver would let a sent-before-error turn replay; this revision moves the marker to the turn-execution boundary so a turn that began running is refused while genuine pre-send failures stay retryable.
Compatibility / Migration
Backward compatible, no migration. The
recovery_statecolumn already exists in the shared delivery-queue schema (#88665); pre-change entries read back with no marker and follow the normal path. A single marker state (send_attempt_started) is used.Risks and Mitigations
failed/rather than replaying it. This favors no-duplicate over at-least-once for that window — the same product choice the outbound queue makes when it has no reconcile capability. Pre-send failures, busy-deferred attempts, and no-op deliver paths are unaffected (they stay retryable). Mitigation / follow-up: a precise adapter-sidereconcileUnknownSend(as the outbound queue has) would let recovery confirm actual send status and replay safely; that is a larger, multi-adapter change kept out of this one-thing-per-PR fix.Real behavior proof
await deliver(entry)thenackSessionDelivery. If the process is killed after deliver succeeds (the agent turn re-runs via dispatchAssembledChannelTurn and the platform reply is sent) but before ack, the entry stays pending. The next recovery re-delivers it unconditionally, re-running the turn and re-sending the reply. refactor: move delivery queues to SQLite #88665 moved the delivery queues onto a shared SQLite backing (delivery-queue-sqlite.ts) that already carries a recovery_state column, but the session-delivery drain does not set or read it; the parallel outbound queue (outbound/delivery-queue-recovery.ts) does, reconciling actual send status before replay and refusing blind replay when it cannot confirm. With this patch the session drain wires the existing recovery_state column so it refuses blind replay of an already-sent turn, matching the outbound queue.Live Node.js measurement of deliver() invocation count for one unacked agentTurn entry across a crash-before-ack then restart-recovery sequence (SQLite-backed session queue):
AI-assisted: drafted with Claude Code (claude-opus-4-8); fully tested (build + typecheck + Node 24 target tests + real-behavior-proof). Author reviewed and understands the change.