opencode-goal-plugin icon indicating copy to clipboard operation
opencode-goal-plugin copied to clipboard

Auto-continue delivers the continuation prompt while the session is still streaming, and its scheduled wake-up is dropped on busy early-returns

Open ananas-wonders opened this issue 1 month ago • 2 comments

What happened?

With auto_continue: true, the plugin reserves and delivers a continuation prompt while the session is still actively producing assistant output — not after it goes idle. Two deliveries observed while the orchestrator agent was mid-run:

delivery time what happened
05:51:21.027 continuationPrompt delivered 25ms after an assistant message completed, while the same agent run continued streaming until 05:53:08 with no idle event in between
05:35:57.659 continuationPrompt delivered 12ms after an assistant message completed, while the same run kept streaming until 05:39:28 with no idle event in between

Additionally, nothing was delivered for ~2 minutes after the min_continue_interval_seconds gate had already opened: the previously scheduled wake-up had been silently dropped, and the next delivery only happened at the next real idle event (06:38:52, ~404ms after an idle milestone).

Environment: opencode v2.0.28, goal plugin v0.1.60, V2 setup path. Config: auto_continue default, defer_while_tasks_active: true, max_task_block_seconds: 7200, min_continue_interval_seconds: 1800 (also reproduced at 900 and 120).

Why it happens (src/server.ts)

1. The reserve-gate fallback delivers on a timer, not on idleness. When reserveContinuation refuses because the minimum interval has not elapsed, the refused run schedules a timed wake-up at lastContinuationAt + minInterval + 1s (continuationDelayFromSnapshot + scheduleSettledContinuation, ~line 2613 in the V2 path):

// A fast execution can settle before the minimum interval expires.
// There may be no further idle event, so retain a timed wake-up rather
// than dropping the only continuation signal.
scheduleSettledContinuation(sessionID, continuationDelayFromSnapshot(minInterval, waiting.lastContinuationAt), scheduled != null)

That wake-up runs runAutoContinue and delivers at exactly that wall-clock time regardless of session work state, whenever busySessions does not contain the session at that instant. Deliveries at 05:51:21 and 05:35:57 each matched lastContinuationAt + minInterval timing while a run was streaming.

2. busySessions is the only busy signal and goes stale. runAutoContinue guards delivery with an in-memory busySessions set maintained solely from session.status / session.idle events:

async function runAutoContinue(...) {
    if (disposed) return
    if (busySessions.has(sessionID)) return   // ← in-memory only

It is never reconciled with live session state before sendContinuation, and gaps between assistant messages (async tool waits, background subagent results landing mid-run) are not marked busy — so at wake-up time the session can look idle while it is mid-run. This also explains the stale look after a host restart or plugin hot-reload, where the set starts empty.

3. Task deferral unblocks permanently by child age. taskBlockStatus defers continuations while child tasks run, but taskTracker.hasBlockingTasks(sessionID, maxTaskBlockMs) stops deferring once a child's age passes the ceiling. A background subagent child that had been alive for ~32h was ignored by deferral for the whole observation window.

4. Every busy early-return silently drops the scheduled wake-up. All early-returns and the mid-delivery guard — if (busySessions.has(sessionID)) return (~lines 1685, 1727, 1740), !continuationStillReserved(goal, beforeDelivery) || busySessions.has(sessionID) (~1793) — return without re-scheduling anything for the active goal. When the timed wake-up fires during a busy moment, it is dropped permanently; the only remaining trigger is the next idle event, potentially minutes after the gate already opened (observed: gate open at 06:37:56, delivery at 06:38:52 only because a real idle milestone happened to occur then).

The same shape exists in the legacy (V1) path in the same file.

Reproduction

  1. Enable a goal with auto_continue: true and a non-zero min_continue_interval_seconds.
  2. Keep an orchestrator-style agent producing outputs with async tool/subagent gaps so that consecutive assistant messages of one run have no idle milestone between them.
  3. Observe that each scheduled wake-up lands on lastContinuationAt + minInterval + ~1s wall-clock and delivers continuationPrompt mid-run, and that wakes firing on a busy moment are consumed without any retry scheduling.

Suggested fixes

  • Before reserving/delivering, re-check live session work state (e.g. latest assistant message still streaming or OpenCode session status) instead of trusting the event-fed busySessions alone; treat "unknown" as busy.
  • On a busy early-return (and on the !continuationStillReserved guard), re-arm scheduleSettledContinuation for the next opportunity instead of dropping the wake-up.
  • Reconsider the age-based deferral ceiling so long-lived children keep deferring until they report an end-of-life state.

I'm happy to open a PR for any of these; happy to share the raw event/message timelines from my opencode.db capture if useful.

ananas-wonders avatar Oct 14 '26 06:10 ananas-wonders

Thanks for the detailed timing analysis. Could you share the ordered raw event slice for one mid-stream timer delivery, from the last session.execution.started through the first real session.execution.succeeded or session.idle, including session.status, assistant message/step completion events, the session ID, and the continuationPrompt timestamp? Please also include the events around the dropped wake-up at 06:37:56. That will show whether the host emitted a misleading idle signal, missed a busy signal, or only the plugin lost its scheduled retry, so we can gate delivery on the correct execution boundary and add a faithful regression test.

danyel117 avatar Oct 15 '26 06:10 danyel117

Thanks for the precise framing. One important caveat first: the host does not persist the raw SSE event stream — the event table in ~/.local/share/opencode/opencode.db exists but is empty (row count 0), and ~/.local/share/opencode/log/opencode.log only logs lifecycle at INFO (42 event-ish lines in 22 MB, none in the relevant windows). So session.execution.started / session.execution.succeeded / session.status cannot be recovered post-hoc. What is durable, at millisecond precision, is the per-session session_message table. I use these proxies:

  • type='idle' row = the persisted run boundary (the durable form of the terminal idle outcome). Every idle row below sits at a genuine settlement.
  • assistant rows carry time.created / time.streamed / time.completed = message-level (step) completion events.
  • type='synthetic' rows = subagent/shell milestones inside a turn.
  • type='user' row whose text starts with Continue working toward the active session goal = the continuationPrompt.

Session: ses_ed256c7cffferRKFrBI6RTVyKG ("Payload migration plan review and implementation prep"), all times local, 2026-10-14.

Slice 1 — one mid-stream delivery (05:51:21.027), from the previous run boundary

The run began at the idle row 05:45:00.452 and never went idle until 05:53:08.766 — one uninterrupted execution the entire time:

05:50:41.318 assistant msg msg_138f67fea001QG85  created 05:50:41.318  streamed 05:50:55.244  completed 05:50:55.262
05:50:55.684 assistant msg msg_138f6b805001gKFU  created 05:50:55.684  streamed 05:51:20.981  completed 05:51:20.996
05:51:21.027 user      msg msg_138f700f1001MGD8  "Continue working toward the active session goal..."   <- continuationPrompt, 31 ms after a step completion
05:51:21.831 assistant msg msg_138f71ca0001IsD   created 05:51:21.831  streamed 05:51:33.468  completed 05:51:33.485
05:51:34.295 assistant msg msg_138f74d59001nu0r  created 05:51:34.295  streamed 05:52:28.256  completed 05:52:28.272
05:52:29.267 assistant msg msg_138f82378001adfY  created 05:52:29.267  streamed 05:52:44.278  completed 05:52:44.293
05:52:44.708 assistant msg msg_138f861ea001rEMY  created 05:52:44.708  streamed 05:53:08.726  completed 05:53:08.753
05:53:08.766 IDLE (first real run boundary after the delivery)

There is no idle event at any point before the prompt — the last one was 05:45:00.452, before this run even started. The prompt was enqueued 31 ms after msg_138f6b805001gKFU completed, while the execution was still consuming steps 05:51:21.831 → 05:53:08.753. This matches v2autopilot.ts:2438 — "The CLI/native path can ignore a mid-turn prompt enqueued via the inbox. Timed wake-ups must fire anyway." — i.e., the mid-turn inbox enqueue is deliberate, and the data confirms it lands between assistant steps of a live run.

Second occurrence with the same shape, still live today (2026-10-15):

06:42:22.789 assistant created 06:42:22.789  streamed 06:42:35.987  completed 06:42:36.006
06:42:36.023 user      "Continue working toward the active session goal..."   (17 ms later, mid-run)
06:42:37.388 assistant created 06:42:37.388  ... run continues until idle 06:43:46.533

Slice 2 — around the due-times 06:36:16 and 06:37:56

Last delivery before this window was 06:06:15.243 (36.9 s after the genuine idle at 06:05:38.316). With the interval that had been hot-reloaded at 06:29:46, due-times were 06:06:15 + 1801 s = 06:36:16 and + 1901 s = 06:37:56:

06:36:12.784 synthetic (shell completed)
06:36:42.820 assistant created 06:36:42.820  streamed 06:36:59.351  completed 06:36:59.369
06:37:00.307 assistant created 06:37:00.307  streamed 06:37:48.795  completed 06:37:48.816
06:37:49.936 assistant created 06:37:49.936  streamed 06:38:03.188  completed 06:38:03.208   <- spans the 06:37:56 due-time
06:38:04.354 assistant created 06:38:04.354  streamed 06:38:31.862  completed 06:38:31.885
06:38:39.946 assistant created 06:38:39.946  streamed 06:38:51.623  completed 06:38:51.657
06:38:51.665 IDLE (genuine run boundary)
06:38:52.069 user      "Continue working toward the active session goal..."   (404 ms after the idle)
06:38:53.170 assistant created 06:38:53.170  ... run continues until idle 06:39:51.722

No delivery occurred at 06:36:16 or 06:37:56 — both due-times landed inside assistant streaming windows (06:36:12–06:36:59 and 06:37:48–06:38:03). The next delivery came only 404 ms after the genuine idle boundary.

Answers to your three-way discriminator

  1. Misleading idle signal: no. Every persisted idle row sits at a genuine settlement (05:15:01.407, 05:20:56.140, 05:31:43.319, 05:45:00.452, 05:53:08.766, 06:05:38.316, 06:38:51.665, ...). The mid-run injections at 05:51:21.027 / 06:42:36.023 had no idle event before them — the host never said idle.
  2. Missed busy signal: yes, for the mid-run injections. The run was streaming before and after each injection (slice 1), so at delivery time the busy state was not represented wherever the delivery decision was made.
  3. Lost scheduled retry: possible but indistinguishable post-hoc. At 06:36:16/06:37:56 nothing fired during streaming. From persistence alone I cannot tell whether the busy guard blocked-and-silently-dropped, or the scheduled work item was lost — but the next delivery appeared only on a genuine idle event, so no compensating retry ever fired.

For the record, the full sequence of continuation prompts vs. idle boundaries in that morning (Oct 14, 05:00–07:00):

continuationPrompt preceding idle row gap verdict
05:11:14.862 (earlier) — mid-run
05:15:01.748 05:15:01.407 341 ms idle-triggered
05:17:30.129 05:16:47.866 ~2 min mid-run
05:20:56.463 05:20:56.140 323 ms idle-triggered
05:35:57.659 05:31:43.319 ~4 min mid-run
05:51:21.027 05:45:00.452 ~6 min mid-run
06:06:15.243 06:05:38.316 36.9 s idle-triggered
06:38:52.069 06:38:51.665 404 ms idle-triggered

For the regression test

Since the host keeps no event history, the plugin must capture its own trace to make this testable: on every session.execution.started, session.status, idle, watchdog timer fire, reservation and delivery, append a line (session ID, event, ms timestamp) to a small ring buffer / debug file. With that trace plus session_message rows, "delivery while an assistant step is mid-stream" becomes a one-query assertion. Happy to run a capture against my live setup if you want a longer multi-hour trace — or to take a crack at the patch: gate sendContinuation on the run boundary (there exists a live-execution/streaming check), re-arm the scheduled retry when the busy guard blocks instead of dropping it, and stop treating assistant-step completion as a run boundary.

ananas-wonders avatar Oct 15 '26 07:10 ananas-wonders