Repository navigation
fix(outbound): prevent remote gateway duplicate sends - #128202
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. |
Follow-up to #124310 for #124279. When the agent runtime talks to a remote Gateway it mints no runtime identity, so the gateway keeps a proven-not-sent durable row replayable and also returns the error to the model, which resends. - executor marks proven-not-sent OutboundDeliveryError as recoveryOwnedRetry - message.action maps that marker to additive UNAVAILABLE details { code: "OUTBOUND_DELIVERY_QUEUED" } - message tool returns a non-throwing "not delivered, queued, do not resend" result and keeps the autogenerated idempotency key mapping so a resend of the same content hits the gateway's cached result instead of a second send
664776d to
42884c4
Compare
|
Codex review: found issues before merge. Reviewed August 23, 2026, 8:14 AM ET / 12:14 UTC. ClawSweeper reviewWhat this changesAdds a queued-delivery Gateway error so remote message-tool sends tell the model not to resend while the durable queue retries. Merge readiness⛔ Blocked by patch quality or review findings - 8 items remain Keep open. The PR’s process-local idempotency cache does not prevent a second durable queue row after Gateway restart, expiry, or eviction, so duplicates can still drain; its queued-result parser also accepts the marker on non-UNAVAILABLE errors. Priority: P1 Review scores
Verification
How this fits togetherThe message tool calls a remote Gateway, which sends through a durable outbound queue. The queue’s retry state and the tool result must agree so one failed send has exactly one retry owner. flowchart LR
A[Agent message tool] --> B[Remote Gateway]
B --> C[Gateway idempotency cache]
B --> D[Durable outbound queue]
D --> E{Proven not sent}
E --> F[Queued-delivery result]
F --> G[Model avoids resend]
D --> H[Recovery drain and channel send]
Before merge
Findings
Agent review detailsSecurityNone. PR surfaceSource +48, Tests +139, Other +18. Total +205 across 14 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Copy recommended automerge instructionTechnical reviewBest possible solution: Derive a route-scoped durable delivery intent from the trusted Gateway idempotency request, reuse it at queue admission after cache loss, and accept the queued marker only with the matching UNAVAILABLE contract. Do we have a high-confidence way to reproduce the issue? Yes. Current source shows that Gateway cache loss removes the only reused-key record while the durable queue receives no delivery intent, making the duplicate path source-reproducible. Is this the best way to solve the issue? No. The queue, not a short-lived Gateway cache, must own duplicate prevention across restart and eviction; the structured result also needs the full error-code predicate. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found but not applied because it conflicted with ClawSweeper's review contract. Codex review notes: model internal, reasoning high; reviewed against 8a0ddd33113b. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
When the agent runtime talks to a remote Gateway (gateway.mode "remote" or a
gatewayUrl/gatewayToken override), the message tool withholds the runtime
identity, so a proven-not-sent outbound failure kept its durable retry row
and also surfaced as an error the model would answer by resending — a
duplicate once the queue replayed the row.
- deliver-queue-execute: mark the thrown error recoveryOwnedRetry when the
proven-not-sent row stays replay-eligible (one retry owner per row)
- message.action: additive UNAVAILABLE detail code OUTBOUND_DELIVERY_QUEUED
- message tool: project that error into a non-throwing delivery_queued
result ("queued, will retry automatically, do not resend") and keep the
autogenerated idempotency-key mapping so an identical resend collapses
Validated with unit tests and a real-Telegram remote-gateway E2E
(affected on main: duplicate; fixed: single send, delivery_queued).
Closes openclaw#124279
Co-authored-by: Ayaan Zaidi <[email protected]>
What Problem This Solves
Follow-up to #124310 (issue #124279). That fix dead-letters caller-owned proven-not-sent durable rows so the model's resend is the only retry. It only applies when the gateway can see that the caller is an agent run (
runtimeAgentId). In remote Gateway mode (gateway.mode: "remote"or agatewayUrl/gatewayTokenoverride) the agent runtime deliberately mints no runtime identity, so the gateway keeps the row replay-eligible and returns the error to the model — two retry owners, one duplicate send.Approach
Queue stays the single retry owner; the model is told so.
deliver-queue-execute.ts: the executor already decides which rows stay replay-eligible. When a proven-not-sent failure keeps the row, it marks the thrownOutboundDeliveryErrorwithrecoveryOwnedRetry.gateway/server-methods/send.ts:message.actionmaps that marker to additiveUNAVAILABLEdetails{ code: "OUTBOUND_DELIVERY_QUEUED" }(new closed-union member in@openclaw/gateway-protocol; Swift/Kotlin regenerated). No trust-boundary or method change.message-tool-execution.ts: the message tool turns that error into a non-throwing result —status: "delivery_queued", delivered: false, "not delivered … the gateway queued it and will retry automatically. Do not resend it." — and keeps the autogenerated idempotency-key mapping, so a model that resends the same content anyway hits the gateway's cached result for that key instead of creating a second durable send.Unstructured
UNAVAILABLEerrors still throw; in-process and identity-bearing RPC paths are unchanged (#124310 still dead-letters those rows).Validation
git diff --check,node scripts/check-changed.mjs,pnpm protocol:check,pnpm check:test-types: green.src/infra/outbound,src/gateway/server-methods/send.test.ts,src/agents/tools/message-tool-execution.test.ts,src/agents/tools/message-tool.test.ts,packages/gateway-protocol): 78 files / 1267 tests pass.message.actionemits the details code only for queue-owned retries; message tool returns the queued result, reuses the idempotency key on an identical resend, and still throws for unstructured unavailability; protocol schema accepts the new details member.Telegram E2E (real Telegram, remote-gateway path, fault injection)
$telegram-e2e-userbotoutbound-duplicate-sendrecipe, remote variant: the SUT runs from the checkout withOPENCLAW_GATEWAY_URL/OPENCLAW_GATEWAY_TOKENset, so the in-process agent'smessagetool resolves the loopback gateway as a remote target and mints no runtime identity (the exact gap). A controllable Bot API proxy closes its data listener before the first send (ECONNREFUSED, proven-not-sent) and reopens before the model's next turn. The mock model honors adelivery_queuedtool result like a real model would (no resend); otherwise it retries with a_RETRYmarker. DM to the SUT bot, recorded by the QA user account via TDLib.origin/main@ 93c36f5 (includes #124310)OutboundDeliveryError: Network request for 'sendMessage' failed!(raw error)…_RETRYOPENCLAW_E2E_DUPSEND_PROBE_RETRY, thenOPENCLAW_E2E_DUPSEND_PROBEreplayed byOutbound delivery retry: drained delivery …→ duplicate{"status":"delivery_queued","delivered":false,"message":"Message not delivered: … The gateway queued it and will retry automatically. Do not resend it."}DUPSEND_QUEUED_ACK(no resend)OPENCLAW_E2E_DUPSEND_PROBE, delivered by the drain after the proxy came back → single sendFixed-run gateway log:
⇄ res ✗ message.action 5462ms errorCode=UNAVAILABLE …for the first call, thenOutbound delivery retry: drained delivery 12c6cf0a-… on telegram; proxy transitions up → down (20.8s) → up (26.4s). Affected-run proxy log shows foursendMessagecalls (draft,_RETRY,DUPSEND_TURN_DONE, replayed probe); fixed-run shows three (draft, ack, one probe).Notes
message-tool-execution.tssits at the 700-line lint cap; two single-use helpers were collapsed (normalizeOptionalString(value)?.replace(...), inline Set literal) to make room. Behavior unchanged.