mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(core): keep the truncated preview when tool output can't be saved (#63334)
This commit is contained in:
committed by
GitHub
Unverified
parent
72616be4db
commit
51fcace74a
@@ -49,6 +49,12 @@ const cleanup = Effect.fn("ToolOutput.cleanup")(function* (fs: FSUtil.Interface,
|
||||
)
|
||||
})
|
||||
|
||||
// The system error message ("ENOSPC: no space left on device, write") without the stack or wrapper text.
|
||||
const describe = (error: FSUtil.Error) => {
|
||||
const cause = error._tag === "PlatformError" ? error.reason.cause : error.cause
|
||||
return cause instanceof Error ? cause.message.split("\n")[0] : error.message
|
||||
}
|
||||
|
||||
const layer = Layer.effect(
|
||||
Service,
|
||||
Effect.gen(function* () {
|
||||
@@ -86,10 +92,19 @@ const layer = Layer.effect(
|
||||
bytes += size
|
||||
}
|
||||
const file = path.join(directory, fileName())
|
||||
yield* fs.ensureDir(directory).pipe(Effect.orDie)
|
||||
yield* fs.writeFileString(file, text).pipe(Effect.orDie)
|
||||
const failure = yield* fs.ensureDir(directory).pipe(
|
||||
Effect.andThen(fs.writeFileString(file, text)),
|
||||
Effect.as(undefined),
|
||||
Effect.catch((error) =>
|
||||
Effect.logWarning("failed to save full tool output", { file, cause: error }).pipe(
|
||||
Effect.as(describe(error)),
|
||||
),
|
||||
),
|
||||
)
|
||||
const shown = kept.length > 0 ? `lines 1-${kept.length}` : "0 lines"
|
||||
const marker = `[showing ${shown} of ${lines.length}; full output saved to ${file}]`
|
||||
const marker = `[showing ${shown} of ${lines.length}; ${
|
||||
failure === undefined ? `full output saved to ${file}` : `full output could not be saved: ${failure}`
|
||||
}]`
|
||||
const bounded: Tool.Content[] = []
|
||||
let remaining = kept.join("\n").length
|
||||
let seenText = false
|
||||
@@ -115,7 +130,7 @@ const layer = Layer.effect(
|
||||
return {
|
||||
...result,
|
||||
content: bounded,
|
||||
metadata: { ...result.metadata, truncated: true, outputPath: file },
|
||||
metadata: { ...result.metadata, truncated: true, ...(failure === undefined && { outputPath: file }) },
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -48,6 +48,25 @@ describe("ToolOutput", () => {
|
||||
),
|
||||
)
|
||||
|
||||
it.live("keeps the preview when the full output cannot be saved", () =>
|
||||
withStore(
|
||||
(service, fs, root) =>
|
||||
Effect.gen(function* () {
|
||||
yield* fs.writeFileString(path.join(root, ToolOutput.DIRECTORY), "not a directory")
|
||||
const result = yield* service.truncate({ content: [{ type: "text", text: "one\ntwo\nthree" }] })
|
||||
expect(result.metadata).toEqual({ truncated: true })
|
||||
expect(result.content).toEqual([
|
||||
{ type: "text", text: "one\ntwo" },
|
||||
{
|
||||
type: "text",
|
||||
text: expect.stringMatching(/^\[showing lines 1-2 of 3; full output could not be saved: E[A-Z]+: [^\n]+\]$/),
|
||||
},
|
||||
])
|
||||
}),
|
||||
{ maxLines: 2, maxBytes: 1_000 },
|
||||
),
|
||||
)
|
||||
|
||||
it.live("reports lines shown under the byte limit", () =>
|
||||
withStore(
|
||||
(output) =>
|
||||
|
||||
Reference in New Issue
Block a user