Skip to content

fix: channel turns recover safely after gateway restart - #108283

Merged
steipete merged 35 commits into
mainfrom
codex/fix-channel-restart-recovery
Jul 16, 2026
Merged

steipete merged 35 commits into
mainfrom
codex/fix-channel-restart-recovery

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Related: #95609

What Problem This Solves

Fixes an issue where users could receive a resend-request notice after a gateway restart even though OpenClaw had enough durable evidence to finish or safely resume the interrupted channel turn. A restart could also lose a newly accepted channel request before its user turn reached the transcript.

Why This Change Was Made

Channel ingress now records the user turn and its restart-recovery claim atomically before hooks or model execution. Same-conversation terminal message-tool sends receive an exact source-turn receipt, and recovery reconciles that receipt instead of replaying the turn. Recovered message authority remains bound to the original session, account, sender, channel, target, and thread.

Ambiguous side effects still fail closed. Before a terminal source send reaches the provider, the exact session records a pending delivery intent. Confirmed success resolves it to delivered; confirmed non-delivery clears it. A crash or timeout with an unknown outcome keeps the pending marker, so recovery asks for a clean retry instead of replaying remaining effects. No Gateway protocol, public plugin SDK, or configuration surface changes.

User Impact

After a gateway restart, safely resumable Discord and other channel turns continue automatically. Already delivered terminal replies are not duplicated, and turns accepted before hooks or model work no longer disappear from recovery state. Truly ambiguous tool effects retain the existing one-time resend notice.

Release-note context: channel turns recover more reliably after gateway restarts, without duplicate terminal replies or unnecessary resend prompts when durable evidence proves the safe outcome.

Evidence

  • Focused restart-recovery matrix: 779 tests passed across the relevant gateway, runtime, agents, unit, and end-to-end shards.
  • Post-rebase smoke: 161 tests passed across the restart recovery and gateway send suites.
  • Coverage includes atomic channel admission, hook checkpoints, source-ID isolation, pre-send terminal intent, exact receipt reconciliation, session rotation, requester and route authority, bounded bearer lifetime, diverted and unknown delivery outcomes, and duplicate prevention.
  • Blacksmith changed gate: branch-relevant format, lint, type, dependency, database-first, SDK-surface, and generated-surface checks passed (run).
  • Fresh structured review was rerun after each accepted finding and required a clean result before commit.
  • Direct Codex runtime contract checked: resume rebuilds from persisted rollout/history, while cancellation drains active tool tasks before abort. OpenClaw therefore owns durable external-delivery correlation and replay policy.

Live source-blind Discord validation was not run against the unmerged branch because it would require replacing a production gateway or provisioning a separate disposable bot/channel. The integration matrix exercises the exact restart boundaries without mutating a live deployment.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime agents Agent runtime and tooling size: XL maintainer Maintainer-authored PR labels Jul 15, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5108eab23a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/tools/message-tool.ts Outdated
Comment on lines +1683 to +1686
sourceReplyFinal:
action === "send" && sourceReplySinkDeliveryMode === "message_tool_only"
? (requestedSourceReplyFinal ?? true)
: undefined,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve final markers in delivery evidence

When a message_tool_only turn sends a progress update with final:false and then produces a private final answer or terminal failure, this marker is only passed into runMessageActionForTool; it is stripped from the model-facing params and never added to the tool result or the messagingToolSentTargets/messagingToolSourceReplyPayloads evidence that hasCompletedSourceReplyDeliveryEvidence reads. Real runs therefore fall back to the legacy “any committed source reply completes the turn” path and treat a progress send as terminal, so the final answer can be kept private/dropped instead of warning or enqueueing the retry that the new tests only cover by manually seeding sourceReplyFinal: false in fake run results.

Useful? React with 👍 / 👎.

@steipete
steipete force-pushed the codex/fix-channel-restart-recovery branch from 0e712c3 to 2a88121 Compare July 15, 2026 14:02

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a88121165

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +939 to +940
if (params.toolCallId && (!sourceTurnId || sourceTurnRange === undefined)) {
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scan beyond the recent tail for delivered receipts

When a delivered-terminal receipt is recovered after a long turn, recoverStore has only loaded the recent 20 transcript messages, so the original user message with restartRecoveryDeliverySourceRunId can easily be outside messages. In that case this check returns false before the durable receipt can mark the session done, leaving the aborted session skipped on every startup even though delivery was already confirmed. The receipt path should either load/scan the source turn by id or complete without requiring the source user message to be present in the tail.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 648624968a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +749 to +750
sourceReplyFinal: params.input.sourceReplyFinal,
sourceReplyToolCallId: params.input.sourceReplyToolCallId,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Route terminal receipts through direct sends

When a message_tool_only turn uses a direct-outbound channel (for example Slack declares deliveryMode: "direct" in extensions/slack/src/channel.ts:468), these new terminal-source fields only reach the gateway-plugin branch here; the local executeSendAction path at src/infra/outbound/message-action-runner.ts:1312 still sends and mirrors without arming beginTerminalSourceReplyDelivery or writing the message-tool-source-reply marker. If the gateway/process restarts after such a direct send succeeds, restart recovery cannot see a delivered-terminal receipt and treats the mirrored assistant message as an unresumable tail, so users can get a failure notice or duplicate/drop recovery behavior despite the final reply already being sent.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e2a3e69b29

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +535 to +538
if (params.sourceTurnRange.endIndex !== params.messages.length) {
return false;
}
return params.messages.slice(params.toolCallIndex + 1).every((message) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Ignore abort tails when reconciling delivered replies

When a terminal message-tool send has already been recorded as delivered but the aborted run also persisted the standard empty assistant abort artifact, this check rejects completion because the abort artifact is not an exact delivery mirror. The rest of this recovery code explicitly strips those restart-abort tail artifacts before deciding resumability, so this delivered-receipt path can still mark the session failed and send the retry notice even though the terminal reply reached the user; allow the known abort artifact after the mirror when deciding whether the recovered tool result can be appended.

Useful? React with 👍 / 👎.

…tart-recovery

# Conflicts:
#	src/agents/main-session-restart-dispatch.ts
#	src/agents/main-session-restart-recovery.test.ts
#	src/agents/main-session-restart-recovery.ts
@steipete
steipete merged commit d8b723f into main Jul 16, 2026
107 of 112 checks passed
@steipete
steipete deleted the codex/fix-channel-restart-recovery branch July 16, 2026 06:01
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 16, 2026
)

* fix(gateway): recover channel turns safely after restart

* refactor(gateway): keep recovery identity internal

* refactor(gateway): keep recovery metadata private

* fix(gateway): preserve recovery action authority

* fix(gateway): close recovery lifecycle gaps

* fix(gateway): fail closed on missing delivery receipts

* fix(gateway): fail closed on unmirrorable replies

* fix(gateway): persist terminal delivery intent

* fix(gateway): align recovery lifecycle types

* fix(gateway): require exact terminal thread receipt

* fix(gateway): close recovered terminal tool calls

* test(gateway): type terminal tool correlation

* fix(gateway): satisfy restart recovery CI contracts

* fix(gateway): require terminal tool correlation

* fix(agents): scope recovery receipts to source turns

* fix(gateway): fail closed without restart correlation

* fix(gateway): dedupe terminal source redelivery

* fix(agents): keep recovery tool results causal

* test(auto-reply): clean recovery fixture directories

* fix(gateway): allow live terminal sends without recovery claims

* fix(agents): reconcile delivered sends through restart aborts

* fix(agents): keep recovery continuations in source turns

* fix(agents): keep source-less sends non-terminal

* fix(auto-reply): dedupe active source redelivery

* fix(agents): harden recovery receipt contracts

* fix(agents): admit terminal recovery successors

* docs(recovery): document canonical receipt ownership

* fix(agents): fail closed on stale turn authority

* test(auto-reply): assert rebound session state

* fix(plugins): keep restart recovery hook list internal
steipete added a commit that referenced this pull request Jul 16, 2026
…igin turns (#109201)

PR #108283 started deriving a durable channel source-turn id for gateway
chat.send turns carrying originatingChannel/originatingTo overrides, while
the gateway's pre-created user-turn recorder keys the persisted turn by run
id. The same PR's admission guard then throws 'channel restart recovery
requires source-keyed user-turn admission' for every such turn — breaking
qa-lab suite runs, ACP-bridge flows, and relay integrations (reproduced
live on the codex harness chat-image probe; text-only fails identically).

Internal-origin turns (gateway chat.send stamps the internal channel as the
ingress provider) carry run ids, not provider message ids; gateway turns
already own restart via fingerprint admission and client-side idempotent
retries. Skip the mint for internal ingress at both derivation sites so
admission takes the source-less persist path, while real channel ingress
keeps the new source-keyed recovery. Also surface the full agent.wait
result in the codex harness live test error (how this was found).

Live proof: gateway-codex-harness.live.test.ts chat-image probe on a
Blacksmith Testbox with a real OpenAI key failed with the admission error
twice on main and passes admission with this fix.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 17, 2026
…igin turns (openclaw#109201)

PR openclaw#108283 started deriving a durable channel source-turn id for gateway
chat.send turns carrying originatingChannel/originatingTo overrides, while
the gateway's pre-created user-turn recorder keys the persisted turn by run
id. The same PR's admission guard then throws 'channel restart recovery
requires source-keyed user-turn admission' for every such turn — breaking
qa-lab suite runs, ACP-bridge flows, and relay integrations (reproduced
live on the codex harness chat-image probe; text-only fails identically).

Internal-origin turns (gateway chat.send stamps the internal channel as the
ingress provider) carry run ids, not provider message ids; gateway turns
already own restart via fingerprint admission and client-side idempotent
retries. Skip the mint for internal ingress at both derivation sites so
admission takes the source-less persist path, while real channel ingress
keeps the new source-keyed recovery. Also surface the full agent.wait
result in the codex harness live test error (how this was found).

Live proof: gateway-codex-harness.live.test.ts chat-image probe on a
Blacksmith Testbox with a real OpenAI key failed with the admission error
twice on main and passes admission with this fix.
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
)

* fix(gateway): recover channel turns safely after restart

* refactor(gateway): keep recovery identity internal

* refactor(gateway): keep recovery metadata private

* fix(gateway): preserve recovery action authority

* fix(gateway): close recovery lifecycle gaps

* fix(gateway): fail closed on missing delivery receipts

* fix(gateway): fail closed on unmirrorable replies

* fix(gateway): persist terminal delivery intent

* fix(gateway): align recovery lifecycle types

* fix(gateway): require exact terminal thread receipt

* fix(gateway): close recovered terminal tool calls

* test(gateway): type terminal tool correlation

* fix(gateway): satisfy restart recovery CI contracts

* fix(gateway): require terminal tool correlation

* fix(agents): scope recovery receipts to source turns

* fix(gateway): fail closed without restart correlation

* fix(gateway): dedupe terminal source redelivery

* fix(agents): keep recovery tool results causal

* test(auto-reply): clean recovery fixture directories

* fix(gateway): allow live terminal sends without recovery claims

* fix(agents): reconcile delivered sends through restart aborts

* fix(agents): keep recovery continuations in source turns

* fix(agents): keep source-less sends non-terminal

* fix(auto-reply): dedupe active source redelivery

* fix(agents): harden recovery receipt contracts

* fix(agents): admit terminal recovery successors

* docs(recovery): document canonical receipt ownership

* fix(agents): fail closed on stale turn authority

* test(auto-reply): assert rebound session state

* fix(plugins): keep restart recovery hook list internal
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
…igin turns (openclaw#109201)

PR openclaw#108283 started deriving a durable channel source-turn id for gateway
chat.send turns carrying originatingChannel/originatingTo overrides, while
the gateway's pre-created user-turn recorder keys the persisted turn by run
id. The same PR's admission guard then throws 'channel restart recovery
requires source-keyed user-turn admission' for every such turn — breaking
qa-lab suite runs, ACP-bridge flows, and relay integrations (reproduced
live on the codex harness chat-image probe; text-only fails identically).

Internal-origin turns (gateway chat.send stamps the internal channel as the
ingress provider) carry run ids, not provider message ids; gateway turns
already own restart via fingerprint admission and client-side idempotent
retries. Skip the mint for internal ingress at both derivation sites so
admission takes the source-less persist path, while real channel ingress
keeps the new source-keyed recovery. Also surface the full agent.wait
result in the codex harness live test error (how this was found).

Live proof: gateway-codex-harness.live.test.ts chat-image probe on a
Blacksmith Testbox with a real OpenAI key failed with the admission error
twice on main and passes admission with this fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant