mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(core): default a zero shell timeout and stop timed-out background commands (#63703)
This commit is contained in:
committed by
GitHub
Unverified
parent
fbdb81d726
commit
73abe63ac2
@@ -51,20 +51,25 @@ const description = (shell?: string, autoBackground = false) =>
|
||||
"Background commands return immediately, and you will be notified when they complete.",
|
||||
].join(" ")
|
||||
|
||||
export const Input = Schema.Struct({
|
||||
command: Schema.String.annotate({ description: "Shell command string to execute" }),
|
||||
workdir: Schema.optionalKey(Schema.String).annotate({
|
||||
description:
|
||||
"Working directory to execute the command in. Defaults to the current working directory. When possible, avoid changing directories in the command and set the working directory here instead.",
|
||||
}),
|
||||
timeout: Schema.optionalKey(NonNegativeInt).annotate({
|
||||
description: `Timeout in milliseconds. Set to 0 to disable the timeout. Defaults to ${DEFAULT_TIMEOUT_MS} for foreground commands. Background commands have no timeout by default.`,
|
||||
}),
|
||||
background: Schema.optionalKey(Schema.Boolean).annotate({
|
||||
description:
|
||||
"Run the command in the background and return immediately (useful for dev servers and long-running builds). You do not need to use '&' at the end of the command when using this parameter. You will be notified when it completes. DO NOT poll for completion.",
|
||||
}),
|
||||
})
|
||||
const input = (autoBackground: boolean) =>
|
||||
Schema.Struct({
|
||||
command: Schema.String.annotate({ description: "Shell command string to execute" }),
|
||||
workdir: Schema.optionalKey(Schema.String).annotate({
|
||||
description:
|
||||
"Working directory to execute the command in. Defaults to the current working directory. When possible, avoid changing directories in the command and set the working directory here instead.",
|
||||
}),
|
||||
timeout: Schema.optionalKey(NonNegativeInt).annotate({
|
||||
description: autoBackground
|
||||
? `Optional timeout in milliseconds (default ${DEFAULT_TIMEOUT_MS}). When it expires, a foreground command moves to the background and a background command is stopped.`
|
||||
: `Timeout in milliseconds. Set to 0 to disable the timeout. Defaults to ${DEFAULT_TIMEOUT_MS} for foreground commands. Background commands have no timeout by default.`,
|
||||
}),
|
||||
background: Schema.optionalKey(Schema.Boolean).annotate({
|
||||
description:
|
||||
"Run the command in the background and return immediately (useful for dev servers and long-running builds). You do not need to use '&' at the end of the command when using this parameter. You will be notified when it completes. DO NOT poll for completion.",
|
||||
}),
|
||||
})
|
||||
|
||||
export const Input = input(false)
|
||||
|
||||
const StructuredOutput = Schema.Struct({
|
||||
output: Schema.String,
|
||||
@@ -351,10 +356,15 @@ export const Plugin = {
|
||||
name,
|
||||
options: { codemode: false },
|
||||
description: description(undefined, autoBackground),
|
||||
input: Input,
|
||||
input: input(autoBackground),
|
||||
output: StructuredOutput,
|
||||
execute: (input, context) => {
|
||||
const timeout = input.background === true ? (input.timeout ?? 0) : (input.timeout ?? DEFAULT_TIMEOUT_MS)
|
||||
// With timeout backgrounding, a foreground timeout of 0 falls back to the default so a call can't block
|
||||
// until the command exits.
|
||||
const foreground = autoBackground
|
||||
? input.timeout || DEFAULT_TIMEOUT_MS
|
||||
: (input.timeout ?? DEFAULT_TIMEOUT_MS)
|
||||
const timeout = input.background === true ? (input.timeout ?? 0) : foreground
|
||||
let finalTimeout = timeout
|
||||
// Uninterruptible from spawning to blocking, so an interrupt cannot orphan a started command.
|
||||
return Effect.uninterruptibleMask((restore) =>
|
||||
@@ -403,7 +413,8 @@ export const Plugin = {
|
||||
),
|
||||
),
|
||||
)
|
||||
if (autoBackground) invocation.timeout = 0
|
||||
// A foreground timeout moves the command to the background instead of stopping it.
|
||||
if (autoBackground && input.background !== true) invocation.timeout = 0
|
||||
}),
|
||||
)
|
||||
yield* context.progress({ shellID: info.id })
|
||||
|
||||
@@ -1544,22 +1544,32 @@ describe("ShellTool", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.live("keeps timeout zero as an unbounded foreground wait", () =>
|
||||
it.effect("treats a foreground timeout of zero as the default timeout with timeout backgrounding enabled", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(
|
||||
tmp.path,
|
||||
(registry) =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* executeTool(registry, call({ command: bodyExitCommand, timeout: 0 }))
|
||||
expect(result.metadata).toMatchObject({ status: "completed", exit: 7 })
|
||||
expect(result.metadata).not.toHaveProperty("timeout")
|
||||
expect(result.content).toEqual([
|
||||
{ type: "text", text: "body" },
|
||||
{ type: "text", text: "Exit code: 7" },
|
||||
])
|
||||
const jobs = yield* Job.Service
|
||||
expect(yield* jobs.runningBackground).toEqual([])
|
||||
const running = yield* executeTool(
|
||||
registry,
|
||||
call({ command: progressOverflowCommand(0, "release"), timeout: 0 }, "call-zero-timeout"),
|
||||
).pipe(Effect.forkScoped({ startImmediately: true }))
|
||||
// The wait starts only after the command spawns, so keep advancing the clock until the call returns.
|
||||
const result = yield* Fiber.join(running).pipe(
|
||||
Effect.raceFirst(
|
||||
Effect.forever(Effect.promise(() => Bun.sleep(20)).pipe(Effect.andThen(TestClock.adjust("2 minutes")))),
|
||||
),
|
||||
)
|
||||
expect(result.content?.[0]).toMatchObject({
|
||||
type: "text",
|
||||
text: expect.stringContaining("did not finish within its timeout of 120 seconds"),
|
||||
})
|
||||
const id = ID.make(String(result.metadata?.shellID))
|
||||
const shell = yield* Shell.Service
|
||||
expect((yield* shell.get(id)).status).toBe("running")
|
||||
yield* Effect.promise(() => Bun.write(path.join(tmp.path, "release"), ""))
|
||||
expect(yield* shell.wait(id)).toMatchObject({ status: "exited", exit: 0 })
|
||||
}),
|
||||
true,
|
||||
)
|
||||
@@ -1608,29 +1618,17 @@ describe("ShellTool", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("ignores explicit background timeouts with timeout backgrounding enabled", () =>
|
||||
it.live("stops a background command when its explicit timeout expires with timeout backgrounding enabled", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(
|
||||
tmp.path,
|
||||
(registry) =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* executeTool(
|
||||
registry,
|
||||
call({ command: progressOverflowCommand(0, "release"), background: true, timeout: 10 }),
|
||||
)
|
||||
const result = yield* executeTool(registry, call({ command: idleCommand, background: true, timeout: 50 }))
|
||||
const id = ID.make(String(result.metadata?.shellID))
|
||||
const shell = yield* Shell.Service
|
||||
const info = yield* shell.get(id)
|
||||
expect(result.output).toMatchObject({
|
||||
exitCode: null,
|
||||
outputPath: info.file,
|
||||
wallTimeSeconds: expect.any(Number),
|
||||
})
|
||||
yield* TestClock.adjust("1 second")
|
||||
expect((yield* shell.get(id)).status).toBe("running")
|
||||
yield* Effect.promise(() => Bun.write(path.join(tmp.path, "release"), ""))
|
||||
expect(yield* shell.wait(id)).toMatchObject({ status: "exited", exit: 0 })
|
||||
expect((yield* shell.wait(id)).status).toBe("timeout")
|
||||
}),
|
||||
true,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user