fix(core): keep the truncated preview when tool output can't be saved (#63334)

This commit is contained in:
Aiden Cline
2026-11-11 11:02:10 -06:00
committed by GitHub
Unverified
parent 72616be4db
commit 51fcace74a
2 changed files with 38 additions and 4 deletions
+19 -4
View File
@@ -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 }) },
}
})
+19
View 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) =>