Skip to content

Commit f80f402

Browse files
opencodeclaude
andcommitted
fix(monitor): give command checks a shell, fix cancel's stale-status race
Copilot review on #38, both confirmed against the code: - MonitorProcess.check built its ChildProcess with no `shell` option, unlike tool/bash.ts's own invocation of the same command string -- quotes, pipes, env expansion and multi-word commands could silently behave differently or not run at all, despite asserting the identical `bash` permission. Reuses bash.ts's own platform-default shell (exported as `BashTool.defaultShell`) rather than re-deriving it; the config-configured shell override is out of scope here since threading Config.Service through would add an ambient dependency run-coordinator.ts's drain must stay free of. - MonitorRuntime.cancel() read the monitor's status BEFORE interrupting the active drain. coordinator.interrupt() blocks until the drain fiber is fully settled, so if the drain reached a terminal status (triggered/ completed/failed) in the window between that read and the interrupt, cancel() would publish Cancelled against the stale snapshot and clobber the real terminal status -- projectStatus's unconditional update (no CAS, justified by "only one sequential drain loop ever writes these") has nothing to catch it. Reordered to interrupt first, then read: the status read after interrupt() returns is never stale. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M1CJmqai46ihahdKMVNB27
1 parent 2343696 commit f80f402

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎packages/core/src/monitor/process.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Monitor } from "@opencode-ai/schema/monitor"
66
import { AppProcess } from "../process"
77
import { LocationMutation } from "../location-mutation"
88
import { PermissionV2 } from "../permission"
9+
import { BashTool } from "../tool/bash"
910

1011
export const MAX_CAPTURE_BYTES = 1024 * 1024
1112
export const CAPABILITY = "monitor"
@@ -49,8 +50,15 @@ export const check = Effect.fn("MonitorProcess.check")(function* (input: {
4950
sessionID: input.sessionID,
5051
})
5152

53+
// Reuses bash.ts's own platform default rather than re-deriving it (Copilot review, #38): without
54+
// a shell, quotes/pipes/multi-word commands can silently parse differently, or not run at all,
55+
// even though this asserts the identical `bash` permission. Config-configured shell overrides
56+
// (bash.ts's own `entries().shell` lookup) are not threaded through here -- that would add
57+
// Config.Service to check()'s ambient requirements, which run-coordinator.ts's drain must stay
58+
// free of; out of scope for closing the "no shell at all" gap this fixes.
5259
const command = ChildProcess.make(input.source.command, [], {
5360
cwd: target.canonical,
61+
shell: BashTool.defaultShell(),
5462
stdin: "ignore",
5563
detached: process.platform !== "win32",
5664
forceKillAfter: Duration.seconds(3),

‎packages/core/src/monitor/runtime.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,14 @@ export const layer = Layer.effect(
229229
start: (monitorID) => coordinator.run(monitorID).pipe(Effect.mapError(() => new UnavailableError())),
230230
cancel: (monitorID) =>
231231
Effect.gen(function* () {
232-
const info = yield* monitor.get(monitorID)
232+
// Interrupt BEFORE reading status (Copilot review, #38): coordinator.interrupt() blocks
233+
// until the drain fiber is fully settled, so a read taken first can go stale if the
234+
// drain reaches a terminal status (triggered/completed/failed) in the window between
235+
// that read and the interrupt -- publishing Cancelled against the stale snapshot would
236+
// then clobber the real terminal status projectStatus's unconditional update has no CAS
237+
// to catch. Reading only after interrupt() returns means the status is never stale.
233238
yield* coordinator.interrupt(monitorID)
239+
const info = yield* monitor.get(monitorID)
234240
if (!info || (info.status !== "starting" && info.status !== "running")) return
235241
const now = yield* DateTime.now
236242
yield* events.publish(MonitorEvent.Cancelled, { sessionID: info.sessionID, monitorID, timestamp: now })

‎packages/core/src/tool/bash.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const Output = Schema.Struct({
4646

4747
type Output = typeof Output.Type
4848

49-
const defaultShell = () => (process.platform === "win32" ? (process.env.COMSPEC ?? "cmd.exe") : "/bin/sh")
49+
export const defaultShell = () => (process.platform === "win32" ? (process.env.COMSPEC ?? "cmd.exe") : "/bin/sh")
5050

5151
const modelOutput = (output: Output) => {
5252
const warnings = output.warnings?.length

0 commit comments

Comments
 (0)