diff --git a/packages/core/src/tool/plugin/shell.ts b/packages/core/src/tool/plugin/shell.ts index c5dc7c665c1..a0c9d0858a2 100644 --- a/packages/core/src/tool/plugin/shell.ts +++ b/packages/core/src/tool/plugin/shell.ts @@ -507,10 +507,11 @@ export const Plugin = { if (Option.isNone(found) || found.value.metadata.sessionID !== context.sessionID) return yield* new ToolFailure({ message: `${input.shellID} is not a shell command of this session.` }) const wait = Math.min(input.wait_ms ?? READ_WAIT_MS, READ_MAX_WAIT_MS) - // Waits for the command's job when it has one, so the exit it sees is the job's: holding the job's notification - // meanwhile, and taking its result once finished, so a pushed command read here isn't notified again. + // Waits for the command's job too when it has one, holding the job's notification meanwhile and taking its result + // once finished, so a pushed command read here isn't notified again. A cancelled job ends before its command. const job = yield* jobs.get(id) - const until: Effect.Effect = job?.status === "running" ? jobs.wait({ id }) : shell.wait(id) + const until: Effect.Effect = + job?.status === "running" ? jobs.wait({ id }).pipe(Effect.andThen(shell.wait(id))) : shell.wait(id) // The calling script keeps blocking while this waits, instead of moving to the background. yield* jobs .collect( diff --git a/packages/core/src/tool/plugin/stop.ts b/packages/core/src/tool/plugin/stop.ts index a9143973e1b..f9afccefd30 100644 --- a/packages/core/src/tool/plugin/stop.ts +++ b/packages/core/src/tool/plugin/stop.ts @@ -63,9 +63,9 @@ export const Plugin = { yield* Effect.forEach( stopped, Effect.fnUntraced(function* (id) { - // The model asked for this, so no notification follows. - yield* jobs.deliver(id) - const info = yield* jobs.cancel(id) + // The model asked for this, so no notification follows: the cancel's result is taken. This doesn't + // wait for other holds, such as a `shell_read` waiting on the same shell, which the cancel ends. + const [info] = yield* jobs.collect([id], jobs.cancel(id).pipe(Effect.asVoid)) if (info?.notificationID) yield* jobs.completeBackground(info.notificationID) }), { discard: true }, diff --git a/packages/core/test/tool-shell.test.ts b/packages/core/test/tool-shell.test.ts index 691c0bd3dc3..a50ce01ca02 100644 --- a/packages/core/test/tool-shell.test.ts +++ b/packages/core/test/tool-shell.test.ts @@ -1310,6 +1310,24 @@ describe("ShellTool", () => { expect((yield* shell.wait(shellID)).status).toBe("killed") yield* outboxCleared expect((yield* stop([shellID], "call-stop-again")).output).toEqual({ stopped: [], notRunning: [shellID] }) + + // A shell_read waiting on the command doesn't hold the stop up, and sees the command end. + const other = yield* executeTool(registry, codeModeCall({ command: idleCommand, yield_ms: 50 }, "call-other")) + const otherID = String(other.output.shellID) + const reading = yield* executeTool( + registry, + codeModeCall({ shellID: otherID, wait_ms: 60_000 }, "call-read", ShellTool.readName), + ).pipe(Effect.forkScoped({ startImmediately: true })) + yield* Effect.promise(() => Bun.sleep(100)) + expect((yield* stop([otherID], "call-stop-read").pipe(Effect.timeout("3 seconds"))).output).toEqual({ + stopped: [otherID], + notRunning: [], + }) + expect((yield* Fiber.join(reading).pipe(Effect.timeout("3 seconds"))).output).toMatchObject({ + running: false, + exitCode: null, + }) + yield* outboxCleared expect(yield* Queue.size(notices)).toBe(0) }), )