mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-21 04:12:08 +03:00
fix(core): settle an interrupted shell call without waiting for the stop
This commit is contained in:
@@ -152,7 +152,11 @@ export interface Interface {
|
||||
// NotFoundError if the command is unknown or is removed before it terminates.
|
||||
readonly wait: (id: Shell.ID) => Effect.Effect<Shell.Info, NotFoundError>
|
||||
// A known shell's terminal state and bounded tail. Missing capture remains distinct from its exit status.
|
||||
readonly result: (started: Shell.Info) => Effect.Effect<ShellResult.Result>
|
||||
// With `wait`, gives up waiting for the exit after that long and returns the output so far, still running.
|
||||
readonly result: (
|
||||
started: Shell.Info,
|
||||
options?: { readonly wait?: Duration.Input },
|
||||
) => Effect.Effect<ShellResult.Result>
|
||||
// Replaces the running command's timeout from now; zero clears it. Ignored once the command has
|
||||
// ended or timed out, even while its process is still being killed or its output captured.
|
||||
readonly timeout: (id: Shell.ID, duration: number) => Effect.Effect<Shell.Info, NotFoundError>
|
||||
@@ -826,8 +830,19 @@ const layer = () =>
|
||||
}
|
||||
})
|
||||
|
||||
const result = Effect.fn("Shell.result")(function* (started: Shell.Info) {
|
||||
const info = yield* wait(started.id).pipe(
|
||||
const result = Effect.fn("Shell.result")(function* (
|
||||
started: Shell.Info,
|
||||
options?: { readonly wait?: Duration.Input },
|
||||
) {
|
||||
const ended = wait(started.id).pipe(Effect.map(Option.some))
|
||||
const info = yield* (
|
||||
options?.wait === undefined
|
||||
? ended
|
||||
: ended.pipe(
|
||||
Effect.timeoutOrElse({ duration: options.wait, orElse: () => Effect.succeed(Option.none<Info>()) }),
|
||||
)
|
||||
).pipe(
|
||||
Effect.flatMap((info) => (Option.isSome(info) ? Effect.succeed(info.value) : get(started.id))),
|
||||
Effect.catchTag("Shell.NotFoundError", () =>
|
||||
Effect.succeed({ ...started, status: "killed" as const, time: { ...started.time, completed: Date.now() } }),
|
||||
),
|
||||
|
||||
@@ -60,7 +60,9 @@ export function interrupted(result: Result): Output {
|
||||
...captured,
|
||||
output: [
|
||||
lines ? `${text}${savedNotice(lines, result.info.file)}` : captured.output,
|
||||
`Command interrupted by the user (shell ID: ${result.info.id}).`,
|
||||
result.info.status === "running"
|
||||
? `Command interrupted by the user (shell ID: ${result.info.id}); it is still being stopped.`
|
||||
: `Command interrupted by the user (shell ID: ${result.info.id}).`,
|
||||
].join("\n\n"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ import { ToolInterrupted } from "../interrupted.js"
|
||||
|
||||
export const name = "shell"
|
||||
export const DEFAULT_TIMEOUT_MS = 2 * 60 * 1_000
|
||||
// How long an interrupted call waits for its stopped command to exit before settling with the output so far.
|
||||
const INTERRUPT_WAIT = "500 millis"
|
||||
|
||||
const BACKGROUND_INSTRUCTION =
|
||||
"You will be notified automatically when the command finishes. The notification will include the command's output. Unless the user explicitly asks otherwise, DO NOT poll for completion, even if you need the final result to continue. Repeatedly sleeping and reading or searching the output file is polling, not useful work. You may read the current output if it lets you do useful work now, but do not repeatedly check it while waiting for the command to finish. Keep working on anything that does not depend on the result. If you have nothing else to do, end your response; you will be resumed automatically when the command finishes."
|
||||
@@ -317,7 +319,8 @@ export const Plugin = {
|
||||
// Settled as interrupted by the step, as before; the command ends with the job's scope.
|
||||
if (closing()) return yield* Effect.failCause(cause)
|
||||
yield* shell.stop(info.id, "killed").pipe(Effect.ignore)
|
||||
const result = yield* shell.result(info)
|
||||
// The stop escalates to SIGKILL on its own; the session does not wait for that to settle the call.
|
||||
const result = yield* shell.result(info, { wait: INTERRUPT_WAIT })
|
||||
// The stopped job settles from the same exit; this only releases it.
|
||||
yield* jobs.cancel(jobID)
|
||||
// No `shellID`: clients read it as a background call and show the shell's live output instead of this result.
|
||||
|
||||
@@ -1899,6 +1899,37 @@ describe("ShellTool", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
parity.live(
|
||||
"settles an interrupted call without waiting for a command that ignores SIGTERM",
|
||||
() =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
Effect.gen(function* () {
|
||||
const shell = yield* Shell.Service
|
||||
const started = Date.now()
|
||||
const { shellID, result } = yield* interruptCall(
|
||||
registry,
|
||||
"trap '' TERM; printf 'kept\\n'; while :; do sleep 1; done",
|
||||
"call-interrupted-stubborn",
|
||||
)
|
||||
// Bounded by the interrupt's short wait, not by the 3 s grace before SIGKILL.
|
||||
expect(Date.now() - started).toBeLessThan(2_500)
|
||||
const info = yield* shell.get(shellID)
|
||||
expect(result?.content).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: `kept\n\n[showing lines 1-1 of 1; full output saved to ${info.file}]\n\nCommand interrupted by the user (shell ID: ${shellID}); it is still being stopped.`,
|
||||
},
|
||||
])
|
||||
// The stop still ends it.
|
||||
expect((yield* shell.wait(shellID)).status).toBe("killed")
|
||||
}),
|
||||
)
|
||||
}),
|
||||
{ timeout: 15_000 },
|
||||
)
|
||||
|
||||
fakeDaemonIt.live("a prepared handoff moves a daemon-run foreground call to the background at shutdown", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
|
||||
Reference in New Issue
Block a user