Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/opencode/src/tool/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ function tail(text: string, maxLines: number, maxBytes: number) {
}
}

const FAIL_TAIL_LINES = 50
const FAIL_TAIL_BYTES = 8 * 1024

const parse = Effect.fn("ShellTool.parse")(function* (command: string, ps: boolean) {
const tree = yield* Effect.promise(() => parser().then((p) => (ps ? p.ps : p.bash).parse(command)))
if (!tree) throw new Error("Failed to parse command")
Expand Down Expand Up @@ -566,7 +569,10 @@ export const ShellTool = Tool.define(
}
if (aborted) meta.push("User aborted the command")
const raw = list.map((item) => item.text).join("")
const end = tail(raw, limits.maxLines, limits.maxBytes)
const failed = typeof code === "number" && code !== 0
const end = failed
? tail(raw, FAIL_TAIL_LINES, FAIL_TAIL_BYTES)
: tail(raw, limits.maxLines, limits.maxBytes)
if (end.cut) cut = true
if (!file && end.cut) {
file = yield* trunc.write(raw)
Expand All @@ -575,8 +581,12 @@ export const ShellTool = Tool.define(
let output = end.text
if (!output) output = "(no output)"

const status = failed ? `Command failed with exit code ${code}.\n\n` : ""
if (cut && file) {
output = `...output truncated...\n\nFull output saved to: ${file}\n\n` + output
output =
`...output truncated...\n\n${status}Full output saved to: ${file}\n\n` + output
} else if (status) {
output = status + output
}
Comment on lines 585 to 590

if (meta.length > 0) {
Expand Down
44 changes: 44 additions & 0 deletions packages/opencode/test/tool/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ const fill = (mode: "lines" | "bytes", n: number) => {
if (PS.has(sh())) return `& ${text}`
return text
}
const failFill = (mode: "lines" | "bytes", n: number) => {
const text = fill(mode, n)
return sh() === "cmd" ? `${text} & exit 1` : `${text}; exit 1`
}
const glob = (p: string) =>
process.platform === "win32" ? Filesystem.normalizePathPattern(p) : p.replaceAll("\\", "/")

Expand Down Expand Up @@ -1196,4 +1200,44 @@ describe("tool.shell truncation", () => {
}),
),
)
it.live("truncates failed command output to a short tail and saves full output to file", () =>
runIn(
projectRoot,
Effect.gen(function* () {
const lineCount = Truncate.MAX_LINES + 500
const result = yield* run({
command: failFill("lines", lineCount),
})
expect(result.metadata.exit).not.toBe(0)
expect(result.metadata.truncated).toBe(true)
expect(result.output).toMatch(/\.\.\.output truncated\.\.\./)
expect(result.output).toMatch(/Command failed with exit code 1/)
expect(result.output).toMatch(/Full output saved to:\s+\S+/)
expect(result.output.split("\n").length).toBeLessThan(Truncate.MAX_LINES)

const filepath = (result.metadata as { outputPath?: string }).outputPath
expect(filepath).toBeTruthy()
const saved = yield* (yield* FSUtil.Service).readFileString(filepath!)
const lines = saved.trim().split(/\r?\n/)
expect(lines.length).toBe(lineCount)
}),
),
30_000,
)

it.live("does not truncate failed command output when it is small", () =>
runIn(
projectRoot,
Effect.gen(function* () {
const result = yield* run({
command: `exit 3`,
})
expect(result.metadata.exit).toBe(3)
expect(result.metadata.truncated).toBe(false)
expect(result.output).toContain("Command failed with exit code 3")
expect(result.output).toContain("(no output)")
}),
),
30_000,
)
})
Loading