fix(codemode): drop the null result line before logs (#64344)

This commit is contained in:
Aiden Cline
2026-11-16 20:41:54 -06:00
committed by GitHub
Unverified
parent a0777f3182
commit d54fe2628d
3 changed files with 33 additions and 1 deletions
+3 -1
View File
@@ -434,7 +434,9 @@ function formatResult(result: CodeMode.Result) {
? `Warnings:\n${result.warnings.map((item) => `- [${item.kind}] ${item.message}`).join("\n")}`
: undefined
const logs = result.logs && result.logs.length > 0 ? `Logs:\n${result.logs.join("\n")}` : undefined
return [output, warnings, logs].filter((part) => part !== undefined && part !== "").join("\n\n")
// A program that only logs returns undefined (sent as null); a "null" line before its logs says nothing.
const value = result.ok && result.value === null && (warnings || logs) ? undefined : output
return [value, warnings, logs].filter((part) => part !== undefined && part !== "").join("\n\n")
}
// Compact JSON: indentation adds tokens on every later step without helping the model. Clients pretty-print it.
+23
View File
@@ -152,6 +152,29 @@ test("execute rejects filesystem fetches", async () => {
expect(result.output.output).toContain("fetch cannot be used for filesystem access.")
})
describe("execute output", () => {
const run = async (code: string) =>
(await Effect.runPromise(createCodeMode(new Map()).execute({ code }, context))).output.output
test("a program that only logs shows just its logs", async () => {
expect(await run('console.log("found", 12, "files")')).toBe("Logs:\nfound 12 files")
})
test("a program that only warns shows just its warnings", async () => {
expect(await run('const rejected = Promise.reject(new Error("boom"))')).toBe(
"Warnings:\n- [ExecutionFailure] Unhandled rejection from an un-awaited promise: Error: boom",
)
})
test("a null result with nothing else still shows null", async () => {
expect(await run("return null")).toBe("null")
})
test("a returned value keeps its line before the logs", async () => {
expect(await run('console.log("hi"); return { n: 1 }')).toBe('{"n":1}\n\nLogs:\nhi')
})
})
describe("streaming fetch", () => {
const run = (url: URL, code: string, permission = allowAll) =>
createCodeMode(new Map(), permission).execute(
+7
View File
@@ -26,4 +26,11 @@ describe("execute output", () => {
})
expect(formatExecuteOutput("{not json")).toEqual({ json: undefined, rest: undefined, text: "{not json" })
})
test("keeps output without a result line as it is", () => {
expect(formatExecuteOutput("Logs:\nhi").text).toBe("Logs:\nhi")
expect(formatExecuteOutput("Warnings:\n- [unhandled] boom\n\nLogs:\nhi").text).toBe(
"Warnings:\n- [unhandled] boom\n\nLogs:\nhi",
)
})
})