Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(composer): keep clipboard child stdout binary
贾湘君ca6cd58ecc54
diff --git a/apps/desktop/electron/main/composer-clipboard-directories.ts b/apps/desktop/electron/main/composer-clipboard-directories.tsindex de6378bd5..821505039 100644--- a/apps/desktop/electron/main/composer-clipboard-directories.ts+++ b/apps/desktop/electron/main/composer-clipboard-directories.ts@@ -10,23 +10,38 @@ type NativeClipboard = {const MAX_ITEMS = 20;const MAX_METADATA_BYTES = 1024 * 1024;-async function execFileAsync(+/** Exported so a regression can exercise a real child process boundary. */+export async function execFileAsync(command: string,args: string[],options: { timeout?: number; maxBuffer?: number; [key: string]: unknown },input?: Buffer,): Promise<Buffer> {- const child = execFile(command, args, options);+ // execFile defaults to utf8 strings; pin the buffer encoding so stdout stays binary+ // no matter which caller reads a clipboard, and normalize anything that still+ // arrives as a string instead of throwing inside the stream handler.+ const child = execFile(command, args, { encoding: "buffer", ...options });return new Promise<Buffer>((resolve, reject) => {- let stdout = Buffer.alloc(0);- child.stdout?.on("data", (chunk: Buffer) => {- stdout = Buffer.concat([stdout, chunk]);+ const chunks: Buffer[] = [];+ let settled = false;+ const fail = (error: unknown) => {+ if (settled) return;+ settled = true;+ reject(error instanceof Error ? error : new Error(String(error)));+ };+ child.stdout?.on("data", (chunk: Buffer | string) => {+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));});+ // Stream errors must reject the promise; an unhandled handler would crash Main.+ child.stdout?.on("error", fail);+ child.stdin?.on("error", fail);child.on("error", (error: ExecFileException) => {- reject(new Error(error.message || "clipboard file list read failed"));+ fail(new Error(error.message || "clipboard file list read failed"));});child.on("close", (code) => {- if (code === 0) resolve(stdout);+ if (settled) return;+ settled = true;+ if (code === 0) resolve(Buffer.concat(chunks));else reject(new Error("clipboard file list read failed"));});child.stdin?.end(input);diff --git a/apps/desktop/test/composer-clipboard-directories.test.mjs b/apps/desktop/test/composer-clipboard-directories.test.mjsindex b58142404..1139a606e 100644--- a/apps/desktop/test/composer-clipboard-directories.test.mjs+++ b/apps/desktop/test/composer-clipboard-directories.test.mjs@@ -79,6 +79,7 @@ test("macOS Finder metadata is piped to plutil asynchronously", async () => {const child = new EventEmitter();child.stdout = new EventEmitter();child.stdin = {+ on() {},end(input) {stdinBytes = Buffer.from(input);setImmediate(() => {@@ -139,6 +140,47 @@ test("Windows PowerShell clipboard adapter rejects a failed child process", asynawait assert.rejects(() => readPaths(silentWindowsClipboard(), "win32"), /clipboard file list read failed/);});+test("the clipboard helper reads a real child process whose stdout is not binary", async () => {+ // execFile defaults to utf8, so a real child hands the stream string chunks. A mock that+ // emits Buffers hides that boundary, which is how the production adapter crashed on macOS.+ const { execFileAsync } = load("../electron/main/composer-clipboard-directories.ts");+ const output = await execFileAsync(process.execPath, ["-e", "process.stdout.write(JSON.stringify(['/tmp/one folder']))"], {+ timeout: 30_000,+ maxBuffer: 1024 * 1024,+ });+ assert.ok(Buffer.isBuffer(output), "stdout must stay binary for JSON.parse");+ assert.deepEqual(JSON.parse(output.toString("utf8")), ["/tmp/one folder"]);+ await assert.rejects(+ () => execFileAsync(process.execPath, ["-e", "process.exit(3)"], { timeout: 30_000 }),+ /clipboard file list read failed/,+ );+});++test("a child process that already decodes stdout to strings is accepted", async () => {+ const paths = ["/tmp/one folder"];+ const child = new EventEmitter();+ child.stdout = new EventEmitter();+ child.stdin = {+ on() {},+ end() {+ setImmediate(() => {+ child.stdout.emit("data", JSON.stringify(paths));+ child.emit("close", 0);+ });+ },+ };+ const { nativeClipboardPaths: readPaths } = loadWithExecFile(() => child);+ assert.deepEqual(await readPaths(silentWindowsClipboard(), "win32"), paths);+});++test("a child stream error rejects instead of escaping the event handler", async () => {+ const child = new EventEmitter();+ child.stdout = new EventEmitter();+ child.stdin = { on() {}, end() { setImmediate(() => child.stdout.emit("error", new Error("spawn ENOENT"))); } };+ const { nativeClipboardPaths: readPaths } = loadWithExecFile(() => child);+ await assert.rejects(() => readPaths(silentWindowsClipboard(), "win32"), /spawn ENOENT/);+});+test("a Windows clipboard probe failure rejects without blocking the main process", async () => {// A hung or killed shell must surface as a rejection the caller can report, never as a// synchronous throw that leaves the main process frozen.