Skip to content

fix(core): fix handoff rejection, failed-handoff settlement, and cancelled job cleanup - #68400

Open
jlongster wants to merge 2 commits into
devfrom
codemode-fix-tear-bugs
Open

jlongster wants to merge 2 commits into
devfrom
codemode-fix-tear-bugs

Conversation

@jlongster

@jlongster jlongster commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Related to #66478

Type of change

  • Bug fix

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.

  1. Background script starts during a handoff. execute({ code, background: true }) during an active planned handoff returned a background receipt and wrote a job row before the forked script reached its restart check. Tool.snapshot now refuses mode.full && handoff.active() on the call, before CodeModeJob.Current/background(), with the same restart message. The check in CodeModeTool stays for direct phases.start callers.
  2. Failed handoff leaves runs unsettled. The failed-handoff/shutdown reset discarded saved runs before clearing the handoff signal, so a parked run woke while handingOff() was still true and its foreground part stayed running. The reset now clears the signal first. A saved background job released by that discard now fails with "Interrupted by server restart.", so it settles error and its notice and completeBackground run instead of being left for a later boot.
  3. Caller cancel leaks the outbox. CodeModeJob.deliver skipped every cancelled job, including non-quiet Job.cancel(id), which persists a cancelled outbox row for its notice. It now delivers that notice and completes the row. A quiet cancel still maps its info to undefined, and a teardown cancel (cancelSession without quiet, used by opencode.session.interrupt and Session.remove) is skipped through the same isCancelled distinction the shell notifier uses, so it never wakes the session it interrupted. A caller cancel racing a removed session still hits Session.NotFoundError and 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/core focused tests: test/codemode-execute-options.test.ts and test/durable-tool-restart.test.ts (42 passed)
  • packages/server focused test: test/persistent-pty-handoff.test.ts (11 passed)
  • New tests confirmed to fail with the fixes reverted: background start during handoff refused with no job row, reset ordering cleared-before-discard, failed-handoff background job settling error with its notice, caller-cancel notice/outbox completion, and a teardown session cancel staying silent.
  • bunx prettier --check and bunx oxlint on every changed file (pass)

Screenshots / recordings

N/A (no UI change)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #66478

…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.

@opencodejr opencodejr 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.

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

Comment thread packages/core/src/codemode/job.ts Outdated
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

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.

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)))) return

A 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.

@opencodejr opencodejr 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.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant