Skip to content

fix(sessions): keep acknowledged inputs queued across a graceful restart - #204848

Merged
obviyus merged 2 commits into
mainfrom
fix/acked-input-restart-handover
Nov 24, 2026
Merged

obviyus merged 2 commits into
mainfrom
fix/acked-input-restart-handover

Conversation

@obviyus

@obviyus obviyus commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A graceful restart can drop a chat message that the Gateway already acknowledged. When the follow-up becomes its own turn (no other run is active) and SIGTERM arrives before that turn writes its user entry, settleUserTurnPendingInput finishes the accepted row with restart-unrecoverable. After restart, the user sees "The Gateway restarted before this message ran. Send it again." The message never runs.

A follow-up that queues behind a running turn survives only because the restart marker sets a preserve flag on live queued owners. An input that is already being admitted is not in that set.

Solution

settleUserTurnPendingInput now hands an accepted, unconsumed input over to restart replay instead of finishing it. The decision comes from a stored fact, not from timing:

  • The transcript append that writes a turn's user entry also consumes the pending row in the same SQLite transaction (it deletes the row, or sets consumed_event_id for collected inputs). The recorder's consumed flag is published by that commit.
  • Accepted and not consumed: the turn has not started. The row stays queued, and boot replay (state = 'queued' AND consumed_event_id IS NULL) runs it once.
  • Consumed: the turn has started and its user entry is in the transcript. Settlement is unchanged; restart resume continues that turn, and the input is not injected again.
  • Not accepted (no ack sent): unchanged; the row ends as restart-unrecoverable and the client got an error.

The restart marker's preserve flag stays. It also protects queued accepted rows that settle at shutdown with a failure that is not classified as a restart.

Impact

  • Acknowledged chat messages run after a graceful restart, exactly once.
  • A turn stopped mid-tool-call resumes from the transcript. Its tool does not run again and its input is not replayed.
  • Production: one condition in the existing owner. No schema, state, hook or export change.

Evidence

Each result below has a red base run (production change reverted, tests kept) and a green head run.

  • Owner unit test, src/sessions/user-turn-transcript.test.ts "hands an unconsumed input to restart replay only once accepted": accepted=true fails on base (the row is cancelled) and passes on head. accepted=false passes on both: a pre-ack input still ends.
  • Real Gateway E2E (test/gateway-restart-resume.e2e.test.ts, isolated state and port):
    • "keeps an acknowledged follow-up admitted as its own turn": base fails with the production shape (row <key>:user cancelled, end_cause = restart-unrecoverable, consumed_event_id NULL, no model request after restart). Head passes. Head passed 6 of 6 runs; the same case lost the input in 10 of 10 runs on main before this change.
    • "keeps an acknowledged follow-up queued behind a running turn": passes on head (regression guard).
    • New "continues a turn stopped mid-tool-call without replaying its input": SIGTERM while exec runs. After restart the turn resumes from the transcript, the exec callback count stays 1, the transcript holds one user entry for the input, no replayed turn is answered, and no pending row remains. Passes on base and head: it guards exactly-once against this change.
  • oxlint and oxfmt --check on the changed files: clean.

Test cost: +228 test lines (under the 400-line record threshold). Each added E2E case takes about 15-20 s; it needs a real Gateway restart, which no cheaper layer can prove.

obviyus and others added 2 commits November 25, 2026 00:02
An accepted input that its transcript append has not consumed has not started. A restart now hands its queued row to boot replay instead of ending it as restart-unrecoverable. A consumed input resumes from the transcript and is not replayed.

Co-authored-by: obviyus <[email protected]>
@clawsweeper

clawsweeper Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@obviyus
obviyus merged commit d40e839 into main Nov 24, 2026
137 checks passed
@obviyus
obviyus deleted the fix/acked-input-restart-handover branch November 24, 2026 19:18
@obviyus

obviyus commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Landed as d40e839. Bug: a graceful restart ended an acknowledged chat message as restart-unrecoverable when its turn had not written its user entry yet. Fix: settleUserTurnPendingInput hands an accepted, unconsumed input to restart replay; a consumed one still resumes from the transcript. Proof: owner unit test red on base and green on head; real Gateway E2E 'admitted as its own turn' passed 6 of 6 on head (lost 10 of 10 on main); the new mid-tool-call case keeps the tool at one run and the input unreplayed.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 25, 2026
…art (openclaw#204848)

## Problem

A graceful restart can drop a chat message that the Gateway already acknowledged. When the follow-up becomes its own turn (no other run is active) and SIGTERM arrives before that turn writes its user entry, `settleUserTurnPendingInput` finishes the accepted row with `restart-unrecoverable`. After restart, the user sees "The Gateway restarted before this message ran. Send it again." The message never runs.

A follow-up that queues behind a running turn survives only because the restart marker sets a preserve flag on live queued owners. An input that is already being admitted is not in that set.

## Solution

`settleUserTurnPendingInput` now hands an accepted, unconsumed input over to restart replay instead of finishing it. The decision comes from a stored fact, not from timing:

- The transcript append that writes a turn's user entry also consumes the pending row in the same SQLite transaction (it deletes the row, or sets `consumed_event_id` for collected inputs). The recorder's `consumed` flag is published by that commit.
- Accepted and not consumed: the turn has not started. The row stays queued, and boot replay (`state = 'queued' AND consumed_event_id IS NULL`) runs it once.
- Consumed: the turn has started and its user entry is in the transcript. Settlement is unchanged; restart resume continues that turn, and the input is not injected again.
- Not accepted (no ack sent): unchanged; the row ends as `restart-unrecoverable` and the client got an error.

The restart marker's preserve flag stays. It also protects queued accepted rows that settle at shutdown with a failure that is not classified as a restart.

## Impact

- Acknowledged chat messages run after a graceful restart, exactly once.
- A turn stopped mid-tool-call resumes from the transcript. Its tool does not run again and its input is not replayed.
- Production: one condition in the existing owner. No schema, state, hook or export change.

## Evidence

Each result below has a red base run (production change reverted, tests kept) and a green head run.

- Owner unit test, `src/sessions/user-turn-transcript.test.ts` "hands an unconsumed input to restart replay only once accepted": `accepted=true` fails on base (the row is `cancelled`) and passes on head. `accepted=false` passes on both: a pre-ack input still ends.
- Real Gateway E2E (`test/gateway-restart-resume.e2e.test.ts`, isolated state and port):
  - "keeps an acknowledged follow-up admitted as its own turn": base fails with the production shape (row `<key>:user` cancelled, `end_cause = restart-unrecoverable`, `consumed_event_id` NULL, no model request after restart). Head passes. Head passed 6 of 6 runs; the same case lost the input in 10 of 10 runs on main before this change.
  - "keeps an acknowledged follow-up queued behind a running turn": passes on head (regression guard).
  - New "continues a turn stopped mid-tool-call without replaying its input": SIGTERM while `exec` runs. After restart the turn resumes from the transcript, the `exec` callback count stays 1, the transcript holds one user entry for the input, no replayed turn is answered, and no pending row remains. Passes on base and head: it guards exactly-once against this change.
- `oxlint` and `oxfmt --check` on the changed files: clean.

Test cost: +228 test lines (under the 400-line record threshold). Each added E2E case takes about 15-20 s; it needs a real Gateway restart, which no cheaper layer can prove.

Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant