From 94129cadcd686e15a018b93de3a9d27256c66d4f Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:00:37 -0500 Subject: [PATCH] fix(desktop): preserve path identity in build file listings (#12190) --- .../desktop/DesktopBuildCapture.ts | 15 ++--- .../desktop/DesktopBuildCommands.ts | 18 ++---- src/desktop/BuildComputerController.test.ts | 59 ++++++++++++++++++- src/desktop/BuildComputerController.ts | 17 +++++- 4 files changed, 83 insertions(+), 26 deletions(-) diff --git a/src/application/desktop/DesktopBuildCapture.ts b/src/application/desktop/DesktopBuildCapture.ts index 369b4d395b..79c4ce5571 100644 --- a/src/application/desktop/DesktopBuildCapture.ts +++ b/src/application/desktop/DesktopBuildCapture.ts @@ -28,7 +28,10 @@ import { ProviderAdapterError } from "../../domain/providerAdapterError.js"; import { err, ok, type Result } from "../../domain/result.js"; import { AGENTENV_PROVIDER } from "../../desktop/AgentEnvProviderIdentity.js"; import type { AgentEnvComputer } from "../../desktop/AgentEnvComputer.js"; -import { controller } from "../../desktop/BuildComputerController.js"; +import { + controller, + parseBuildComputerJson, +} from "../../desktop/BuildComputerController.js"; import type { BuildControllerAnswer, ControlledComputer, @@ -39,13 +42,7 @@ import { notAnswered, type CaptureStep, } from "./DesktopBuildCaptureFailure.js"; -import { - adapterError, - parseBuildComputerJson, - refused, - runRea, - WORK, -} from "./DesktopBuildCommands.js"; +import { adapterError, refused, runRea, WORK } from "./DesktopBuildCommands.js"; import { exportBuild, type BuildExportRequest, @@ -183,7 +180,7 @@ const checkExportRuntime = async ( ); let rawSchema: unknown; try { - rawSchema = parseBuildComputerJson(content); + rawSchema = parseBuildComputerJson(content, "REA export schema"); } catch { return err( adapterError( diff --git a/src/application/desktop/DesktopBuildCommands.ts b/src/application/desktop/DesktopBuildCommands.ts index 22fb150f2a..bc4df40dbe 100644 --- a/src/application/desktop/DesktopBuildCommands.ts +++ b/src/application/desktop/DesktopBuildCommands.ts @@ -12,13 +12,15 @@ import { type JsonValue, } from "../../domain/jsonValue.js"; import { ProviderAdapterError } from "../../domain/providerAdapterError.js"; -import { decodeStrictUtf8 } from "../../domain/strictUtf8.js"; import { guestFileAnswerCeiling, guestFileAnswerRefusal, } from "../../desktop/AgentEnvComputer.js"; import { AGENTENV_PROVIDER } from "../../desktop/AgentEnvProviderIdentity.js"; -import { runRea as runControllerRea } from "../../desktop/BuildComputerController.js"; +import { + parseBuildComputerJson, + runRea as runControllerRea, +} from "../../desktop/BuildComputerController.js"; import type { ControlledComputer } from "../../ports/BuildComputerControl.js"; /** The work folder REA keeps under the computer user's home; every capture leaves it out. */ @@ -139,16 +141,6 @@ const redactedJson = ( ); }; -/** Decode structured REA output without forwarding parser excerpts of guest data. */ -export const parseBuildComputerJson = (bytes: Uint8Array): unknown => { - try { - return JSON.parse(decodeStrictUtf8(bytes, "preserve")) as unknown; - } catch { - // Parser excerpts may retain only part of a control token or typed secret. - throw new Error("The build computer's REA output is not valid UTF-8 JSON."); - } -}; - /** * The Evidence result, or the reported failure, a REA command wrote to a file * of `bytes` in the computer, with the control token replaced in every @@ -165,7 +157,7 @@ export const readOutput = async ( if (refusal !== undefined) throw refusal; const content = (await computer.homeFiles([path], bytes, signal)).get(path); if (content === undefined) throw new Error(`The computer has no ${path}.`); - const value = parseBuildComputerJson(content); + const value = parseBuildComputerJson(content, "REA output"); const redact = (text: string) => computer.redacted(text); if (cliFailureSchema.safeParse(value).success) { const reported = jsonObjectSchema.parse( diff --git a/src/desktop/BuildComputerController.test.ts b/src/desktop/BuildComputerController.test.ts index 024536de23..7b4ec5eb4f 100644 --- a/src/desktop/BuildComputerController.test.ts +++ b/src/desktop/BuildComputerController.test.ts @@ -22,8 +22,10 @@ const sha256 = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex"); /** A computer whose controller lists `files` into a file beside the folder and whose data/get answers them, noting each fetch of a file. */ -const computer = (files: ReadonlyMap, steps: string[]) => { - const listing = Buffer.from( +const computer = ( + files: ReadonlyMap, + steps: string[], + listing = Buffer.from( JSON.stringify({ files: [...files].map(([path, bytes]) => ({ path, @@ -32,7 +34,8 @@ const computer = (files: ReadonlyMap, steps: string[]) => { })), skipped: [], }), - ); + ), +) => { return { exec: () => Promise.resolve({ @@ -57,6 +60,56 @@ const computer = (files: ReadonlyMap, steps: string[]) => { }; describe("fetchHomeFolder", () => { + it.each([ + { + caseName: "a literal replacement character", + nameBytes: Buffer.from("\uFFFD"), + malformed: false, + }, + { + caseName: "malformed filename bytes", + nameBytes: Buffer.from([0xff]), + malformed: true, + }, + ])("keeps file identity with $caseName", async ({ nameBytes, malformed }) => { + const content = Buffer.from("seed"); + const path = ".rea-build/b/\uFFFD"; + const files = new Map([[path, content]]); + const steps: string[] = []; + const listing = Buffer.concat([ + Buffer.from('{"files":[{"path":".rea-build/b/'), + nameBytes, + Buffer.from( + `","size":${String(content.length)},"sha256":"${sha256(content)}"}],"skipped":[]}`, + ), + ]); + const fetched = fetchHomeFolder( + computer(files, steps, listing), + ".rea-build/b", + { + file: (file) => { + steps.push(`write ${file.path}`); + return Promise.resolve(); + }, + }, + ); + if (malformed) { + await expect(fetched).rejects.toThrow( + "The build computer's file listing is not valid UTF-8 JSON.", + ); + expect(steps).toEqual([]); + } else { + await expect(fetched).resolves.toEqual({ + files: 1, + bytes: content.length, + }); + expect(steps).toEqual([ + `get ${path} within ${String(content.length)}`, + "write \uFFFD", + ]); + } + }); + it("splits tiny files before their answer envelopes exceed the live budget", async () => { const workingBytes = Math.floor(getHeapStatistics().heap_size_limit / 12); const count = Math.floor(workingBytes / (64 * 1024)) + 2; diff --git a/src/desktop/BuildComputerController.ts b/src/desktop/BuildComputerController.ts index 58238498ba..c14848620c 100644 --- a/src/desktop/BuildComputerController.ts +++ b/src/desktop/BuildComputerController.ts @@ -6,6 +6,7 @@ import { z } from "zod"; import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js"; import type { JsonValue } from "../domain/jsonValue.js"; +import { decodeStrictUtf8 } from "../domain/strictUtf8.js"; import { guestFileAnswerBytes, guestFileAnswerCeiling, @@ -24,6 +25,20 @@ const CONTROLLER = fileURLToPath( const FETCH_BATCH_BYTES = 8 * 1024 * 1024; /** Default time allowed for one REA command in the build computer. */ export const BUILD_REA_TIMEOUT_S = 1800; + +/** Read a structured build-computer file without exposing parser excerpts. */ +export const parseBuildComputerJson = ( + bytes: Uint8Array, + subject: string, +): unknown => { + try { + return JSON.parse(decodeStrictUtf8(bytes, "preserve")) as unknown; + } catch { + // A parser excerpt can expose part of a value that exact redaction misses. + throw new Error(`The build computer's ${subject} is not valid UTF-8 JSON.`); + } +}; + /** * Linux `MAX_ARG_STRLEN` on a 4 KiB page, excluding the NUL the kernel counts. * One argument past this fails with E2BIG before the process starts. @@ -271,7 +286,7 @@ const listFolder = async ( output, ); if (content === undefined) throw new Error(`The computer has no ${output}.`); - return listingSchema.parse(JSON.parse(content.toString("utf8"))); + return listingSchema.parse(parseBuildComputerJson(content, "file listing")); }; /** One listed file and the answer budget a batch must fit. */