opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Restart recovery must decide whether a session is stopped at the turn, not in the sweep

Open cevr opened this issue 2 weeks ago • 1 comments

Problem

At boot, the managed server forks the restart sweep (SessionRestart.resumeSuspendedSessions, packages/server/src/process.ts) and then starts serving routes. While the sweep runs, clients can already reach the server. So a stop, or a new task for a subagent, can arrive between the sweep reading a session's state and the sweep acting on it:

  1. A stop lands after the sweep reads the session as not stopped, and before the recovered turn runs. The recovery saw no stop, and the stop finds no active turn to interrupt. The turn's claim is never released. For a background subagent, its job ends with no settlement and the parent gets no report.
  2. A new task lands after the sweep reads the session as stopped, and before the sweep settles it. The sweep then publishes a user interruption for the new task's running turn and releases that turn's claim while it is still running.

Proposal

The sweep never settles a stopped session itself. It calls SessionExecution.recover (continue a claimed turn, without ending a stop) for every claimed session, and the stopped check moves to the recovered turn's first drain, inside the run coordinator's execution:

  • If the session is stopped there, the execution interrupts itself with reason user. The settle hook then does what a live stop does: it releases the claim, cancels the subagent job, and gives the one cancelled report. The turn is never drained.
  • A stop that lands after that read finds an active execution and interrupts it as usual.
  • A new task (a resume, which clears the stop mark first) either starts the turn itself or is joined by the recover. Either way it runs.
  • A stopped session gets no "continuing after restart" notice and uses no restart attempt.

Tests

In packages/core/test/session-execution.test.ts, a KV wrapper runs a stop, or a new task, right after the sweep's read of the stop mark:

  • the direct child settles: no drain, claim released, mark kept, parent silent;
  • the background child reports cancelled once: claim released, no background row left;
  • the new task runs and holds its claim: the sweep leaves it alone.

All three fail without the change.

cevr avatar Nov 13 '26 14:11 cevr

Thanks for the detailed write-up. I couldn't reproduce this on the latest dev (ba77ff90d), and the code it describes doesn't seem to be there yet:

  • On dev, the restart sweep (resumeSuspendedSessions in packages/core/src/session/execution/restart.ts) doesn't check whether a session was stopped. It counts the restart attempt and posts the "Continuing after restart" notice, then resumes the session or starts the subagent job.
  • A stop goes through SessionExecution.interrupt, which does nothing when no turn is running.
  • I found no stop mark, no read of one in the sweep, and no SessionExecution.recover on dev.

So both cases seem to depend on a change that hasn't landed. Hitting the window between the sweep's read and its action at boot through the released CLI would need injected timing, as in your KV-wrapper test, and I didn't try to rig that.

If this targets a branch or PR, a link to it would help us reproduce it there. So would a way to show the problem on current dev without that test harness.

opencodejr[bot] avatar Nov 13 '26 14:11 opencodejr[bot]