Repository navigation
fix(shell): rebuild past Cursor's own shell traffic and keep background shells detached - #197
Conversation
[Medium impact] The reviewed changes appear safe to merge; live OpenCode 2.3.x behavior is still unverified. SummaryThis PR broadens safe recovery after completed tool results and keeps background shells separate from the host's launcher process group.
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.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
3da0c7e to
e6612b2
Compare
|
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 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]>
e6612b2 to
5342743
Compare
|
Fixed the startup race from the test report in 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. |
|
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. |
Fixes #182, fixes #189, fixes #190, fixes #191.
@nkoynov, thanks for the four reports and the repro branches. Your
soft-deny-rebuildandawait-opaque-activityfixtures 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
force_background_shell_argsto deliver a queued host note, and the provider soft-denies it with a bareNOT_FOUND. That deny was recorded as a host exec without a result, so a later cut right after a tool result ended the turn withmissing-host-results.opaque-activity.Background shells on OpenCode 2.x
zsh +o nomatch -c <command>(ShellSelect.args). The create adapter matched only-c <command>, so zsh background shells ran in the foreground.Fix
force-backgroundprovider 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-fetchcontrol). A deny in the cut attempt still refuses through the attempt barrier.cursorWait. It validates the AwaitShell lifecycle bytes against Cursor CLI'sagent.v1descriptors:success.still_runningwith noregex_matchorwake_reason;success.completewith onlyruntime_ms, equal toblock_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 inCURSOR_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_groupstarts 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 nosetsid, and falls back tosetsidwhen bash is missing. Stop and the host timeout used to reach the supervisor only as that group's TERM, so they now travel through ahandoffFIFO:detachedexplicitly at the spawn handle or at the end of the foreground window (not from the EXIT trap, because bash runs that trap on TERM);detachedTERMs the supervisor, which finalizesaborted/ 143.abortedwithexit_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 typecheckandbun run buildare clean.bun test ./teston macOS: 3,599 tests across 131 files; the one failure was a stale command-text assertion intest/tools.test.ts, fixed and rerun with the 11 other touched files (876 pass, 0 fail,test/architecture.test.tsincluded). Linux, where the no-bashsetsidpath runs, is covered by CI.mainand pass here:test/run-error-host.test.ts:soft-denied-background,await-sleep,await-timed-outrebuild.soft-denied-background-after-result,soft-denied-fetch,await-finished,await-woken,await-short-sleepandweb-fetchstill 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 theforce-backgroundcoverage case.test/shell-timeout.test.ts,shell create adapter argv: the unmanaged and managed spawns under/bin/sh -o nogloband/bin/zsh +o nomatchrun 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. Onmain, 11 of these 12 fail; group TERM already worked there.