Repository navigation
Conversation
|
Codex review: needs maintainer review before merge. Reviewed May 29, 2026, 10:38 AM ET / 14:38 UTC. Summary PR surface: Source +190, Tests +221. Total +411 across 6 files. Reproducibility: yes. there is a high-confidence source reproduction path: current main delivers then acks without any recovery marker, and the linked report/PR body show a two-pass probe where the same entry is delivered twice before the patch. I did not rerun tests in this read-only review. Review metrics: 1 noteworthy metric.
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:
Next step before merge
Security Review detailsBest possible solution: Merge only after maintainers explicitly accept duplicate suppression over at-least-once retry for uncertain session deliveries, or extend the session path with reconciliation before merge. Do we have a high-confidence way to reproduce the issue? Yes, there is a high-confidence source reproduction path: current main delivers then acks without any recovery marker, and the linked report/PR body show a two-pass probe where the same entry is delivered twice before the patch. I did not rerun tests in this read-only review. Is this the best way to solve the issue? Mostly yes: the marker-based repair covers the duplicate replay and partial-send edges, including the latest marker-write failure case. The remaining question is whether fail-safe AGENTS.md: found and applied where relevant. Codex review notes: model gpt-5.5, reasoning high; reviewed against e95fbc05aa61. Label changesLabel changes:
Label justifications:
Evidence reviewedPR surface: Source +190, Tests +221. Total +411 across 6 files. View PR surface stats
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
|
|
Thanks for catching the [P1] - it is a real gap. Pushed 43eb4a1809 to address it. The recovery catch was clearing the recovery marker for every thrown delivery failure. As you noted, Fix:
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
Fixed the test-mock [P2] in e58648cf94:
The two remaining P1s are disclosed maintainer-acceptance items rather than code defects: the fail-safe @clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
… replay on restart The session-delivery recovery path drainQueuedEntry (src/infra/session-delivery-queue-recovery.ts) delivers an entry then acks it (at-least-once). If a restart-continuation agentTurn delivery succeeds (turn re-run + platform send via dispatchAssembledChannelTurn / sendDurableMessageBatch) but the process is killed before ackSessionDelivery, the queue file stays pending and the next recovery blindly re-delivers it. The same user turn runs twice (non-idempotent LLM/tool side-effects) and the reply is sent twice. The parallel outbound queue already guards exactly this: it persists send_attempt_started / unknown_after_send markers (src/infra/outbound/deliver.ts) and on recovery refuses a blind replay for those states without adapter reconciliation (src/infra/outbound/delivery-queue-recovery.ts). The session queue's QueuedSessionDelivery type had no recoveryState field at all (session-delivery-queue-storage.ts), so the two queues' guarantees diverged. Port the outbound marker pattern to the session queue: - add platformSendStartedAt / recoveryState to QueuedSessionDelivery and add markSessionDeliveryPlatformSendAttemptStarted / markSessionDeliveryPlatformOutcomeUnknown / clearSessionDeliveryRecoveryState (storage) - drainQueuedEntry persists the send marker around the deliver seam (the production platform-send seam), and on a recovered send_attempt_started / unknown_after_send marker refuses a blind replay and moves to failed/ (fail-safe). An in-process thrown failure clears the marker so transient failures stay replayable; only a crash leaves the marker durable. Adds a regression test modelling deliver-success-then-crash-before-ack: the second recovery pass re-delivers (deliverCount=2) without the fix and refuses (deliverCount=1) with it. AI-assisted: drafted with claude code (claude-opus-4-8). Co-Authored-By: Claude Opus 4.8 <[email protected]>
…rtial_failed) Address a clawsweeper [P1] on the first revision: the recovery catch cleared the recovery marker for every thrown delivery failure, so a partial_failed (sent-before-error) throw - where sendDurableMessageBatch already delivered a reply chunk before failing - left the entry replayable and could still duplicate the reply / re-run the turn on the next recovery. deliverQueuedSessionDelivery now persists the unknown_after_send marker on a partial_failed send before throwing, and drainQueuedEntry clears the marker only for a proven pre-send failure (nothing delivered). A sent-before-error failure keeps the marker and is moved to failed/ (refuse blind replay), matching the outbound queue, which never clears its marker once the platform send has started. A genuine pre-send transient failure still clears the marker and stays retryable, so at-least-once is not broadly weakened. Adds a regression test for the partial-sent window (RED before / GREEN after). AI-assisted: drafted with claude code (claude-opus-4-8). Co-Authored-By: Claude Opus 4.8 <[email protected]>
…nel session-delivery mock The prior commit imported markSessionDeliveryPlatformOutcomeUnknown into server-restart-sentinel.ts from the session-delivery-queue barrel, but server-restart-sentinel.test.ts's vi.mock of that barrel did not provide the new export, so the test module failed to load and the test-types lane went red. Add the missing export to the mock (mocks object + vi.mock factory). AI-assisted: drafted with claude code (claude-opus-4-8). Co-Authored-By: Claude Opus 4.8 <[email protected]>
…e post-send marker write fails Address a clawsweeper [P2]: drainQueuedEntry only refused replay when the marker read back as unknown_after_send. If deliver(entry) returned (turn ran + reply may have been sent) but markSessionDeliveryPlatformOutcomeUnknown then failed, the entry stayed at send_attempt_started and the catch fell through to clearSessionDeliveryRecoveryState + failSessionDelivery, making the already-sent entry replayable on the next recovery. Track that deliver(entry) returned with a local `delivered` flag; once set, a later failure (marker write or ack) moves the entry to failed/ instead of clearing the marker and requeuing. A pre-send throw (nothing delivered) still clears the marker and stays retryable. Adds a regression test for an outcome-marker write failure after a successful delivery (RED before / GREEN after). AI-assisted: drafted with claude code (claude-opus-4-8). Co-Authored-By: Claude Opus 4.8 <[email protected]>
e58648c to
86711f9
Compare
|
Pushed 86711f9 (rebased onto latest This revision also closes the post-delivery marker-write edge from the last review: The two remaining items are disclosed maintainer-acceptance calls rather than code defects: the fail-safe @clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
…efused even if the marker write fails Address a clawsweeper [P1]: on partial_failed the sentinel persisted unknown_after_send before throwing, but if that marker write itself failed, deliver threw before drainQueuedEntry set `delivered`, the entry stayed at send_attempt_started, and the recovery catch cleared the marker and requeued an already-sent reply. Carry the sent-before-error signal with the thrown error instead of depending on a durable write that can fail: the delivery seam throws SessionDeliverySendUncertainError on partial_failed (marker write is now best-effort, kept for crash durability), and the recovery catch refuses a blind replay when delivered || isSessionDeliverySendUncertainError(err) (with the unknown_after_send marker re-read kept as a secondary guard). A pre-send throw still clears the marker and stays retryable. Adds a regression test for partial_failed where the marker write also fails; updates the partial_failed test to throw the typed signal; adds the new export to the sentinel test mock. AI-assisted: drafted with claude code (claude-opus-4-8). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed 7c4961b for the marker-write-failure edge. Root issue was that the sent-before-error signal depended on a durable write that can itself fail: on Fix: the delivery seam now throws a typed Added a regression test for The remaining two items are disclosed maintainer-acceptance calls, not code defects: the fail-safe @clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
Closing this PR. Upstream #88665 ("refactor: move delivery queues to SQLite") rewrote the session-delivery queue storage layer this PR targets, moving it from the file-based queue to the shared SQLite delivery-queue backing ( The underlying issue is not resolved by that refactor: I will open a fresh PR against the SQLite-backed queue, building the recovery guard on the existing |
fix(infra): reconcile unacked session deliveries to prevent duplicate replay on restart
Summary
agentTurndelivery succeeds (the turn re-runs and the platform reply is sent) but the process dies before the entry is acked, the next recovery re-delivers the same entry unconditionally - re-running a non-idempotent turn and re-sending its reply.send_attempt_started/unknown_after_sendrecovery marker around the delivery, and on recovery refuse a blind replay of an entry whose marker shows the platform send may already have happened (fail-safe: move tofailed/), aligning the session queue with the outbound queue's reconciliation guarantee. Critically, the delivery seam (deliverQueuedSessionDelivery) now persists theunknown_after_sendmarker whensendDurableMessageBatchreturnspartial_failed(some payloads already sent,sentBeforeError === true) before it throws, and the recovery catch only clears the marker for a proven pre-send failure (nothing sent) - a sent-before-error failure keeps the marker and is moved tofailed/instead of being blind-replayed. This mirrors the outbound queue, which never clears its marker once the platform send has started. The recovery catch also tracks whetherdeliver(entry)returned: once it has, a later failure in the marker write or ack does not clear the marker or requeue the entry (the turn/reply may already have gone out), so a storage hiccup after a successful delivery cannot reintroduce a blind replay. The sent-before-error signal travels with a typedSessionDeliverySendUncertainErrorthrown by the delivery seam onpartial_failed(theunknown_after_sendmarker write is best-effort, kept for crash durability), so recovery refuses the replay even if that marker write itself fails - the signal does not depend on a durable write that can fail.Change Type
Scope
src/infra/(session-delivery durable queue: storage + recovery)server-restart-sentinel.tsdelivery seam: markunknown_after_sendonpartial_failedbefore throwing)Touched files (4 source + 2 tests):
src/infra/session-delivery-queue-storage.ts(marker fields +mark*/clear*helpers)src/infra/session-delivery-queue-recovery.ts(refuse-blind-replay; clear marker only for pre-send failures)src/infra/session-delivery-queue.ts(re-exportmarkSessionDeliveryPlatformOutcomeUnknown)src/gateway/server-restart-sentinel.ts(markunknown_after_sendonpartial_failedbefore throwing)src/infra/session-delivery-queue.recovery.test.ts(regression tests)src/gateway/server-restart-sentinel.test.ts(add the new marker helper to thevi.mockof the session-delivery barrel)Linked Issue
Closes #88015
Root Cause
drainQueuedEntry(src/infra/session-delivery-queue-recovery.ts) is an at-least-once drain: it runsawait deliver(entry)thenawait ackSessionDelivery(entry.id). The result is classified into onlyrecovered/failed. There is no durable record of whether the platform send already happened, becauseQueuedSessionDelivery(src/infra/session-delivery-queue-storage.ts) had norecoveryStatefield.So if the process is killed in the window after
deliversucceeds (turn re-run + platform send viadispatchAssembledChannelTurn/sendDurableMessageBatch) but beforeackSessionDeliverycompletes its atomic rename, the queue file stays pending with no marker. The nextrecoverPendingSessionDeliveriespass treats it as a fresh pending entry and re-delivers it.The missing guardrail is the reconciliation marker. The parallel outbound queue already guards exactly this scenario: it persists
send_attempt_started/unknown_after_sendmarkers (src/infra/outbound/deliver.ts) and on recovery refuses a blind replay for those states when it cannot confirm via adapter reconciliation (src/infra/outbound/delivery-queue-recovery.ts, "refusing blind replay without adapter reconciliation"). The session queue replicated none of this, so the two queues' durability guarantees diverged.There are two windows where a queued entry's platform send may already have happened: (1) a crash after the send but before the ack, and (2) an in-process failure thrown after a partial send -
deliverQueuedSessionDeliverycallssendDurableMessageBatch, which returnspartial_failedwithsentBeforeError === true(a reply chunk already delivered) and then throws (src/gateway/server-restart-sentinel.ts). Window (2) is the one the recovery catch must not treat as a fresh, replayable failure. The outbound queue handles window (2) by never clearing the marker once the platform send has started (it has no clear path;failDeliveryruns only when the send had not started, i.e.!platformResultsReturned). The fix gives the session queue the same property: the delivery seam marksunknown_after_sendonpartial_failedbefore throwing, and the recovery catch clears the marker only for a proven pre-send failure.Regression Test Plan
Added four regression tests to the existing recovery suite (
src/infra/session-delivery-queue.recovery.test.ts, profileinfra,test/vitest/vitest.infra.config.ts):"does not re-deliver a session entry whose first delivery succeeded but was left unacked by a crash"- models crash-before-ack by lettingdeliversucceed in PASS 1 while skipping the ack (the entry stays pending with theunknown_after_sendmarker - byte-identical on-disk state to a real crash before ack's atomic rename), then runs a second real recovery pass (PASS 2) and asserts the same entry is not re-delivered."does not re-deliver a session entry whose delivery partially sent before throwing"- models thepartial_failed/ sent-before-error window: PASS 1'sdeliverpersistsunknown_after_sendand throwsSessionDeliverySendUncertainError; the entry is moved tofailed/rather than requeued, and PASS 2 (withbypassBackoff) does not re-deliver it."does not re-deliver a partially sent entry even if persisting the unknown marker fails"- the marker-write-failure edge: PASS 1'sdeliverthrowsSessionDeliverySendUncertainErrorwithout having persisted the marker (entry left atsend_attempt_started); the thrown signal alone makes it non-replayable, so it is moved tofailed/and PASS 2 does not re-deliver it."does not re-deliver when the outcome-marker write fails after a successful delivery"- models a storage failure after a successfuldeliver: PASS 1'sdeliversucceeds butmarkSessionDeliveryPlatformOutcomeUnknownthrows (entry left atsend_attempt_started); thedeliveredflag still makes it non-replayable, so it is moved tofailed/and PASS 2 does not re-deliver it.deliverCount === 2(blind replay) -> test FAILS (verified RED on the working tree by reverting the catch logic).deliverCount === 1(recovery refuses the blind replay) -> tests PASS.Run:
The pre-existing recovery cases (success->ack, transient throw->requeue, retry budget, startup cutoff, backoff tier) all stay green. The transient-throw->requeue case is a pre-send failure: the marker set before deliver is cleared in the catch, so the entry stays retryable exactly as before. The storage round-trip tests (
session-delivery-queue.storage.test.ts) also stay green - the new fields are optional and are not serialized when undefined.Security Impact
.github/CODEOWNERSplaces no@openclaw/secopsconstraint onsrc/infra/session-delivery-queue-*.Repro + Verification
OPENCLAW_HOME+ explicitOPENCLAW_STATE_DIR.agentTurnsession delivery.deliversucceeds, but the ack does not complete (crash-before-ack).recoverPendingSessionDeliveriesagain (restart).deliverinvocations on the same entry.Evidence
Failing before, passing after, on this branch (profile
infra):Type checks clean on this branch:
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 queue file stays pending with no send-attempt marker - QueuedSessionDelivery has no recoveryState field. The next recovery re-delivers the same entry unconditionally, re-running the turn and re-sending the reply. The parallel outbound queue (outbound/delivery-queue-recovery.ts:370) persists send_attempt_started / unknown_after_send markers and reconciles actual send status before replay, refusing blind replay when it cannot confirm (:417); the session queue replicates none of this. With this patch, the session queue records an equivalent marker so recovery refuses blind replay of an already-sent turn.Live Node.js measurement of deliver() invocation count for one unacked agentTurn entry across a crash-before-ack then restart-recovery sequence:
partial_failed/ sent-before-error window (delivery seam marksunknown_after_sendbefore throwing, recovery refuses replay) is covered by the added"...partially sent before throwing"regression test (RED before / GREEN after on this branch) rather than by this live probe.Human Verification
recoverPendingSessionDeliveriesis invoked fromserver-restart-sentinel.tswithdeliver: (entry) => deliverQueuedSessionDelivery(...), so the markers indrainQueuedEntrywrap the real turn-re-run + platform-send seam.send_attempt_started/unknown_after_send+ refuse-blind-replay). Verified the outbound queue has no marker-clear path and only callsfailDeliverywhen the platform send had not started (!platformResultsReturned); the session queue now matches by clearing the marker only for a proven pre-send failure.partial_failed/ sent-before-error failure (marker advanced tounknown_after_sendby the delivery seam before it throws) keeps the marker durable, so recovery refuses a blind replay and fail-safes tofailed/. This closes the gap where clearing the marker on every thrown failure could still blind-replay an already partially sent reply.tsgo:coreandtsgo:core:test(both exit 0).I have not exercised a real separate-process SIGKILL between deliver and ack (the crash is modelled as ack-skip + a real restart-recovery pass; the on-disk state is byte-identical). Calling that out explicitly under What was not tested.
Review Conversations
@clawsweeperraised a [P1] on the first revision: the recovery catch cleared the marker for every thrown delivery failure, so apartial_failed(sent-before-error) throw could still leave the entry replayable and duplicate an already-sent reply. This revision addresses it directly: the delivery seam now persistsunknown_after_sendonpartial_failedbefore throwing, and the recovery catch clears the marker only for a proven pre-send failure. Added a regression test for the partial-sent path. Thanks for catching it. Will address further@clawsweeper/ Greptile / reviewer comments as they arrive.Compatibility / Migration
platformSendStartedAtandrecoveryStateare optional fields onQueuedSessionDelivery; existing on-disk queue entries without them deserialize unchanged and take the normal (non-refuse) path.failed/).Risks and Mitigations
partial_failed/ sent-before-error failure - the entry is moved tofailed/rather than replayed, so in that window a message that was not actually delivered would not be retried.reconcileUnknownSendto recover full at-least-once for the confirmable case (out of scope here to keep this PR small and adapter-contract-free).server-restart-sentinel.tsgets one added line - amarkSessionDeliveryPlatformOutcomeUnknown(entry.id)call on the existingpartial_failedbranch before the existingthrow. No control-flow or behavior change beyond persisting the marker; this is required because only the delivery seam knows the send waspartial_failed(sent-before-error). The rest of the change stays in theinfraqueue files plus the test.