Skip to content

Tell the model when a Cursor-managed background shell finishes #150

Open

Description

@oakimov

From discussion #132 (thanks @nkoynov). We'll implement this here rather than port the fork's module, so it can meet the persistence and deduplication requirements below.

Problem

When Cursor starts a shell with block_until_ms: 0 (background_shell_spawn), or a shell runs past its block_until_ms (soft-background), the provider hands the command to a detached supervisor. It then returns only the shell ID and its terminals/<id>.txt record: "Started in the background (…)" or "Still running in the background (…) after N ms" (shell-timeout.ts:588-596).

When the command ends, the supervisor writes the status and the exit_code / elapsed_ms / ended_at footer into the record (terminal-runner.ts:525-527), but nothing reaches the session. The model has two options:

  • hold its turn with AwaitShell or sleep loops. AwaitShell is a Cursor-side wait with no host step boundary, so it also delays steers (host-steer.ts:3).
  • end the turn and lose track of the job.

OpenCode 2's own background: true shells don't have this gap: OpenCode posts a <shell id=… state=…> notice, and the provider forwards it as a host note (language-model.ts:7490).

Cursor CLI

Cursor CLI 2026.10.22-27dcf5f reports these completions:

  • A finished background task queues a BackgroundTaskCompletion (task id, kind, status, title, detail, output path, reason).
  • When the session is idle, queued completions are sent together as one ConversationAction.background_task_completion_action (field 12), which starts a new turn.
  • A completion is requeued while an awaitToolCall for the same task id is in progress, so the model doesn't get both an await result and a notice.
  • After a turn the user aborted, queued completions are dropped rather than sent.

Our ConversationAction schema has no field 12 (protocol/messages.ts:1842).

Scope

In scope: managed terminal records, meaning explicit background spawns and soft-background handoffs, on OpenCode 1.x and 2.x.

Out of scope:

  • OpenCode 2 native background shells. They already have notices.
  • The unmanaged POSIX fallbacks (native Windows, no runtime storage). They have no record to watch, so they stay as they are. The docs should say so.

Requirements

  1. Detect completion from the record, not from the payload PID: completed, failed, cancelled, hard timeout, and supervisor death (the record is reported as aborted with an unknown exit status, README "Explicit and soft-background shells…").
  2. Deliver through explicit host APIs, gated on capability, never on host identity. On 2.x use ctx.session.synthetic (already used for agent switches, plugin-opencode2.ts:230). On 1.x, check whether the classic plugin's session.promptAsync with a synthetic part (as in src/plugin.ts agent switching) delivers the notice without touching the user's draft or agent. If a host has neither, there's no notice and no guidance change.
  3. Shape: one note per delivery, batching every completion that is ready. Each completion gives the shell ID, command, final state, exit code or signal, run time, the record path and a short output tail. It reaches Cursor as a host note (user_context), never system_context. If it arrives while a Run is held, it continues that Run like other host notes. If the session is idle, it starts a turn.
  4. Exactly one report per shell, decided by shell ID, not by timing. Hold a completion while an AwaitShell for that shell ID is running. Drop it once the model has seen the final state (an AwaitShell result, or a read of the record that includes its footer). The fork saw both an AwaitShell result and a notice for the same job in 1 of 9 runs with 10 parallel threads.
  5. Follow Cursor CLI on Stop: completions queued when the user stops a turn are dropped, not sent into the next turn.
  6. Survive restart. Pending completions go into the per-session snapshot. After a restart, records that finished (or whose supervisor died) while the server was down are still reported once.
  7. Prompt cache: the notice is a new message at the end of the conversation. Any shell-guidance line saying the model need not poll belongs in the frozen baseline from the conversation's first Run. It must not be added or removed mid-conversation.
  8. Cost: watching must not add a timer per shell or block startup, and must stop with the session or plugin disposal. No TMPDIR remnants in tests.

Open questions

  • Wire form: should we send a host note (above) or open the Run with background_task_completion_action, as Cursor CLI does? The action is the closer match, but we need independent wire evidence of how Cursor's server treats it before using it: whether it needs a checkpoint and how it relates to ResumeAction. Until then, use the host-note path, which is the same path OpenCode's own <shell> notices take.
  • Relation to the native-jobs redesign (tasks/todo.md): once OpenCode 2 native jobs own Cursor background shells, the 2.x half of this is replaced by OpenCode's own notices. Keep the implementation contained so it's easy to remove; 1.x still needs it.

Done when

  • Unit tests cover each final state, supervisor death, batching, the AwaitShell race (held, then dropped), Stop dropping queued completions, and restart recovery from a snapshot.
  • Live checks on OpenCode 1.x and 2.x: an idle session gets one notice that starts a turn, a held Run gets it at its next step, and parallel jobs each produce exactly one report.
  • README shell section, both host guides and the Unreleased changelog are updated.

Activity

  1. added
    enhancement
    on Nov 4, 2026
  2. nkoynov commented on Nov 4, 2026

    @nkoynov
    Contributor

    Thanks for taking this on. When there's a branch I can run the 2.x live checks from your list here (idle session, held Run, parallel jobs) on OpenCode 2.2.5 with a real key.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions