Repository navigation
Conversation
…elled job cleanup Refuse a background execute on its call during a planned handoff, so no job row is written before its script would start. Clear the handoff signal before discarding saved runs, so a failed handoff settles them instead of leaving them running. Settle a released saved background job as failed, and deliver and complete the outbox row of a caller-cancelled job.
There was a problem hiding this comment.
The three fixes work. I copied the new core and server tests onto the base branch: 6 of them fail there (handoff background start, failed-handoff settlement, caller-cancel notice and the resumed-job cancel test, both reset-ordering tests) and all of them pass with this PR (41/41 core, 11/11 server). Both type-checks pass.
Public API: unchanged. Everything here is internal and behind full_codemode.
One regression to fix before merging. The relaxed check in CodeModeJob.deliver now also lets teardown cancels through (Job.cancelSession without quiet, which opencode.session.interrupt and Session.remove use). Those have info, so they reach synthetic(), which wakes the session by default. As a result, interrupting a session that has a background Code Mode script now posts a state="cancelled" notice and wakes the session again. That goes against the interrupt tool's "nothing wakes these sessions again". I confirmed this with a small test (background script, then jobs.cancelSession(sessionID), then expect no notice): it passes on the base and fails here. Details are in the inline comment.
Related: #66478
| if (!info || info.status === "cancelled") return | ||
| // Session removal or worker teardown cancels the job and removes its outbox row. A quiet cancel maps its info to | ||
| // none; a caller cancel is reported here like any other outcome. | ||
| if (!info) return |
There was a problem hiding this comment.
This now lets teardown cancels through too. cancelSession without quiet (used by opencode.session.interrupt and Session.remove) sets status: "cancelled" with info present, so deliver goes on to synthetic(), which wakes the session by default. Interrupting a session with a background script now posts a cancelled notice and restarts it. I reproduced this: background script, then h.jobs.cancelSession(sessionID), gives one state="cancelled" notice with this PR and none on the base.
The shell tool separates caller cancels from teardown with jobs.isCancelled. Something like this should keep the caller-cancel fix without notifying on teardown:
if (!info || (info.status === "cancelled" && !(yield* input.jobs.isCancelled(input.id)))) returnA non-quiet session cancel (opencode.session.interrupt and Session.remove) also reports status cancelled, so the caller-cancel fix made it wake the interrupted session with a cancelled notice. Distinguish caller cancels with Job.isCancelled, as the shell notifier does, and keep teardown cancels silent.
There was a problem hiding this comment.
My earlier point is fixed: teardown cancels from cancelSession no longer deliver a notice or wake the interrupted session, and caller cancels still deliver theirs. The focused core tests (42/42) and the core type check pass. Public API: unchanged. No new concerns.
Issue for this PR
Related to #66478
Type of change
What does this PR do?
Fixes the three bugs the Code Mode restart Tear models found. It is behind
full_codemode; with the flag off, Code Mode never gets a full mode and every change here is inert, so flag-off behavior is unchanged.execute({ code, background: true })during an active planned handoff returned a background receipt and wrote ajobrow before the forked script reached its restart check.Tool.snapshotnow refusesmode.full && handoff.active()on the call, beforeCodeModeJob.Current/background(), with the same restart message. The check inCodeModeToolstays for directphases.startcallers.shutdownreset discarded saved runs before clearing the handoff signal, so a parked run woke whilehandingOff()was still true and its foreground part stayedrunning. The reset now clears the signal first. A saved background job released by that discard now fails with"Interrupted by server restart.", so it settleserrorand its notice andcompleteBackgroundrun instead of being left for a later boot.CodeModeJob.deliverskipped everycancelledjob, including non-quietJob.cancel(id), which persists acancelledoutbox row for its notice. It now delivers that notice and completes the row. A quiet cancel still maps its info toundefined, and a teardown cancel (cancelSessionwithoutquiet, used byopencode.session.interruptandSession.remove) is skipped through the sameisCancelleddistinction the shell notifier uses, so it never wakes the session it interrupted. A caller cancel racing a removed session still hitsSession.NotFoundErrorand completes the row.This PR is not stacked on another PR; its base is
dev.How did you verify your code works?
packages/core:nice -n 19 bunx tsc -b tsconfig.json tsconfig.tests.json(pass)packages/server:nice -n 19 bunx tsc -b tsconfig.json(pass)packages/corefocused tests:test/codemode-execute-options.test.tsandtest/durable-tool-restart.test.ts(42 passed)packages/serverfocused test:test/persistent-pty-handoff.test.ts(11 passed)errorwith its notice, caller-cancel notice/outbox completion, and a teardown session cancel staying silent.bunx prettier --checkandbunx oxlinton every changed file (pass)Screenshots / recordings
N/A (no UI change)
Checklist
Added by the opencode bot (AI-generated).
Related: #66478