Skip to content

fix(shell): rebuild past Cursor's own shell traffic and keep background shells detached - #197

Merged
cursor[bot] merged 1 commit into
mainfrom
fix/shell-rebuild-and-detach
Nov 19, 2026
Merged

cursor[bot] merged 1 commit into
mainfrom
fix/shell-rebuild-and-detach

Conversation

@oakimov

@oakimov oakimov commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Fixes #182, fixes #189, fixes #190, fixes #191.

@nkoynov, thanks for the four reports and the repro branches. Your soft-deny-rebuild and await-opaque-activity fixtures are merged into the post-result matrix here, with a few extra refusal controls.

Problem

The four issues come from two causes.

A post-result rebuild refused because of Cursor's own shell traffic

Background shells on OpenCode 2.x

Fix

  • force-background provider answer. It covers only a soft-deny of a request about a call already sent (isExecRequestWithoutNewCall: force_background_shell / force_background_subagent). Other soft-denied execs still need a result, because the model read their refusal text (soft-denied-fetch control). A deny in the cut attempt still refuses through the attempt barrier.

  • cursorWait. It validates the AwaitShell lifecycle bytes against Cursor CLI's agent.v1 descriptors:

    • a start must carry args only;
    • a task wait's completion must be success.still_running with no regex_match or wake_reason;
    • a sleep's completion must be success.complete with only runtime_ms, equal to block_until_ms.

    Waits are tracked like inline catalog lookups (waitCalls): a wait needs its own start, an unfinished wait still blocks the rebuild, and the attempt that delivered the result stays a barrier. A finished task's exit code, an error, a regex match, a wake reason, a shorter sleep and any unknown field all stay unsafe. Waits claimed by named polls keep their current handling.

  • Adapter argv. The adapter wraps [-o NAME | +o NAME]... -c <command> and hands the wrapper the host's shell and those options as arguments. The payload therefore runs the argv the host would have run, with the same zsh semantics as a foreground command. Any other argument form runs the host shell unwrapped, as before. Arguments after the command used to be wrapped and dropped; they now run unwrapped. The payload shell no longer rides in CURSOR_SHELL_EXECUTABLE, which every payload used to inherit; classic OpenCode's injector passes it as an argument too.

  • Own process group plus a handoff pipe. cursor_own_group starts the supervisor, and the unmanaged background spawn, in a process group of their own. It uses bash job control (set -m), which also covers macOS where there is no setsid, and falls back to setsid when bash is missing. Stop and the host timeout used to reach the supervisor only as that group's TERM, so they now travel through a handoff FIFO:

    • the launcher holds the only write end;
    • it writes detached explicitly at the spawn handle or at the end of the foreground window (not from the EXIT trap, because bash runs that trap on TERM);
    • an end of file without detached TERMs the supervisor, which finalizes aborted / 143.
    • if the supervisor dies while starting the command group and the output collector, they exit. The output pipe and the group's lifeline are opened before those children run, so that death is an end of file rather than a blocked open, and a later read of the stale terminal reports aborted with exit_code: unknown.

    The unmanaged soft-background payload stays in the launcher's group, so Stop during its window still stops it.

Compared with the fork branches: macOS is covered, options are passed through as given instead of parsed against an allow/deny list, and the launcher and the unmanaged spawn share one helper.

Verification

  • bun run typecheck and bun run build are clean. bun test ./test on macOS: 3,599 tests across 131 files; the one failure was a stale command-text assertion in test/tools.test.ts, fixed and rerun with the 11 other touched files (876 pass, 0 fail, test/architecture.test.ts included). Linux, where the no-bash setsid path runs, is covered by CI.
  • New regressions that fail on main and pass here:
    • test/run-error-host.test.ts: soft-denied-background, await-sleep, await-timed-out rebuild. soft-denied-background-after-result, soft-denied-fetch, await-finished, await-woken, await-short-sleep and web-fetch still refuse; those controls pass on both.
    • test/replay-safety.test.ts: Cursor-side waits (accepted shapes, 16 rejected shapes, start/duplicate/unfinished, claimed waits) and the force-background coverage case.
    • test/shell-timeout.test.ts, shell create adapter argv: the unmanaged and managed spawns under /bin/sh -o noglob and /bin/zsh +o nomatch run with those options; the unwrapped forms fall through; the unmanaged spawn stays out of the launcher's group.
    • test/terminal-runner.test.ts, managed terminal and the launcher's process group: spawn and soft handoff under sh, dash, bash and zsh survive a SIGKILL of the exited launcher's group. TERM or KILL to the launcher's group, or to the launcher alone, during the window, and a KILL before ready, give an aborted record with no job processes left. Nothing of the job stays in the launcher's group. On main, 11 of these 12 fail; group TERM already worked there.
  • Prompt caching: RequestContext and checkpoint handling are unchanged; only the set of post-result cuts that may rebuild from history grows.
  • Both majors: the recovery and supervisor changes are shared, the adapter belongs to 2.x, and the 1.x injector now passes the shell as an argument.
  • Live check on OpenCode 2.3.x: pending. @nkoynov, could you rerun your VM repro for Background shells run in the foreground when the host shell is zsh (OpenCode 2.2.7+) #190 and Background shells die partway under OpenCode 2.3: the process daemon kills the supervisor's process group #191 on this branch?

@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge; live OpenCode 2.3.x behavior is still unverified.

Summary

This PR broadens safe recovery after completed tool results and keeps background shells separate from the host's launcher process group.

  • History rebuilds can account for more of Cursor’s own shell activity.
  • Shell wrappers keep the options OpenCode passes before the command.
  • Background jobs run outside the launcher’s process group after handoff.
  • Managed terminals record an abort when their supervisor stops during startup.

No actionable issues found. Tests were inspected but not run during this review; the live OpenCode 2.3.x check remains pending in the PR description.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Host approves shell command] --> B[Adapter preserves shell and options]
  B --> C[Launcher starts supervisor in its own process group]
  C --> D[Supervisor starts command and output collector]
  D --> E{Launcher handoff pipe}
  E -->|detached message| F[Job continues independently]
  E -->|EOF without message| G[Supervisor stops job and records abort]
  F --> H[Terminal record keeps output and exit status]
Loading

Reviews (2) · Last reviewed commit: "fix(shell): rebuild past Cursor's own sh..." · Reviewed by Greptile

@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@oakimov
oakimov force-pushed the fix/shell-rebuild-and-detach branch 2 times, most recently from 3da0c7e to e6612b2 Compare November 19, 2026 17:29
@nkoynov

nkoynov commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Tested e6612b2 on Linux (NixOS, Bun 1.4.4) against main 29cdeba. Typecheck and build pass, and the touched test files pass (519 tests, nothing Linux-specific). Our fixtures for #182 and #189, and our tests for zsh's +o nomatch -c (#190) and for a group SIGKILL during or after the foreground window (#191), fail on main and pass here. With a local Cursor stub and OpenCode 2.3.2, a zsh background shell now detaches in about half a second (on main it blocked for the whole command), Stop in the foreground window ends with 143, and a SIGKILL of the launcher's group after the spawn leaves the job running to exit 0.

One thing this doesn't change, and main has it too: a kill -9 of the supervisor in the few ms before group.sh opens the lifeline leaves group.sh and the collector waiting, with the record stuck at running. With the supervisor in its own group only a direct SIGKILL or the OOM killer can hit that now.

…nd shells detached

A post-result history rebuild no longer fails on Cursor shell traffic that
changed nothing history lacks:
- the provider's bare-status refusal of force_background_shell/subagent
  requests is a provider answer (`force-background`) needing no result (#182);
- an AwaitShell Cursor ran that slept its whole length, or timed out with the
  shell still running, is accounted for when its lifecycle bytes strictly
  match that shape (`cursorWait`); a finished task, error, regex match or
  wake reason still refuses (#189).

Background shells launch and survive under OpenCode 2.x:
- the create adapter wraps `[-o|+o NAME]... -c <command>`, so zsh under
  OpenCode 2.2.7+ (`+o nomatch`) detaches again; the payload gets the host's
  shell and options as wrapper arguments instead of the inherited
  CURSOR_SHELL_EXECUTABLE (#190);
- the supervisor and the unmanaged spawn start in a process group of their
  own (bash job control, also on macOS; setsid without bash), out of reach of
  OpenCode 2.3.0's delayed SIGKILL of the launcher's group. Stop and the host
  timeout reach the supervisor through a handoff pipe whose only write end the
  launcher holds; it writes `detached` when it hands the job over (#191).

Fixes #182, #189, #190, #191.

Co-authored-by: nkoynov <[email protected]>
@oakimov
oakimov force-pushed the fix/shell-rebuild-and-detach branch from e6612b2 to 5342743 Compare November 19, 2026 18:22

oakimov commented Nov 19, 2026

Copy link
Copy Markdown
Owner Author

Fixed the startup race from the test report in 5342743. The group and collector now open their pipe ends while they still hold the inherited endpoints. If the supervisor dies before the payload starts, the helpers exit and the payload stays unstarted. Reading the stale terminal record reports aborted with an unknown exit status.

I reproduced the failure on the previous head and tested the fix with SIGKILL before the group fork and again before payload launch. Both cases passed on Linux and macOS, and the startup interruption also passed through native OpenCode 2.3.0 with a local fixture. The full Linux suite (3,619 tests), 201 focused macOS tests, and GitHub checks passed.

@nkoynov

nkoynov commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Confirmed on Linux with 5342743. terminal-runner.test.ts passes (79 tests), and the probe that SIGKILLs the supervisor before the group forks, and again right before the payload starts, now leaves nothing running: the launcher prints the init failure and exits 1. On e6612b2 the same probe left group.sh and the collector waiting. Thanks for the quick fix.

@cursor
cursor Bot merged commit 48bb50e into main Nov 19, 2026
11 checks passed
@oakimov
oakimov deleted the fix/shell-rebuild-and-detach branch November 20, 2026 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment