Skip to content

Commit ce669bd

Browse files
committed
fix(agents): avoid unused shell snapshot stdout pipe
1 parent 6b39aa9 commit ce669bd

4 files changed

Lines changed: 74 additions & 97 deletions

File tree

‎scripts/proof/patch-spawn.cjs‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎scripts/proof/shell-snapshot-stream-errors.mts‎

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import type { ChildProcess, SpawnOptions } from "node:child_process";
2+
import { EventEmitter } from "node:events";
3+
import os from "node:os";
4+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
6+
import { captureEnv, setTestEnvValue } from "../test-utils/env.js";
7+
import {
8+
maybeWrapCommandWithShellSnapshot,
9+
resetShellSnapshotCacheForTests,
10+
} from "./shell-snapshot.js";
11+
12+
const { killProcessTreeMock, spawnMock } = vi.hoisted(() => ({
13+
killProcessTreeMock: vi.fn(),
14+
spawnMock: vi.fn(),
15+
}));
16+
17+
vi.mock("node:child_process", async () => {
18+
const { mockNodeBuiltinModule } = await import("openclaw/plugin-sdk/test-node-mocks");
19+
return mockNodeBuiltinModule(
20+
() => vi.importActual<typeof import("node:child_process")>("node:child_process"),
21+
{ spawn: spawnMock },
22+
);
23+
});
24+
25+
vi.mock("../process/kill-tree.js", () => ({
26+
killProcessTree: killProcessTreeMock,
27+
}));
28+
29+
describe.skipIf(process.platform === "win32")("shell snapshot subprocesses", () => {
30+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
31+
let envSnapshot: ReturnType<typeof captureEnv>;
32+
33+
beforeEach(() => {
34+
envSnapshot = captureEnv(["HOME", "OPENCLAW_EXEC_SHELL_SNAPSHOT", "OPENCLAW_STATE_DIR"]);
35+
spawnMock.mockReset();
36+
killProcessTreeMock.mockReset();
37+
});
38+
39+
afterEach(() => {
40+
resetShellSnapshotCacheForTests();
41+
envSnapshot.restore();
42+
});
43+
44+
it("does not create output pipes for status-only shell commands", async () => {
45+
const child = Object.assign(new EventEmitter(), { pid: 4242 }) as ChildProcess;
46+
spawnMock.mockImplementation(() => {
47+
queueMicrotask(() => child.emit("close", 1));
48+
return child;
49+
});
50+
51+
const home = tempDirs.make("openclaw-snapshot-spawn-home-");
52+
const stateDir = tempDirs.make("openclaw-snapshot-spawn-state-");
53+
setTestEnvValue("HOME", home);
54+
setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
55+
56+
const command = "echo unchanged";
57+
await expect(
58+
maybeWrapCommandWithShellSnapshot({
59+
command,
60+
shell: "/bin/bash",
61+
shellArgs: ["-c"],
62+
cwd: os.tmpdir(),
63+
env: { ...process.env },
64+
}),
65+
).resolves.toBe(command);
66+
67+
const options = spawnMock.mock.calls[0]?.[2] as SpawnOptions | undefined;
68+
expect(options?.stdio).toBe("ignore");
69+
expect(killProcessTreeMock).toHaveBeenCalledWith(4242, { graceMs: 0 });
70+
});
71+
});

‎src/agents/shell-snapshot.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,16 +440,15 @@ async function runShell(opts: {
440440
cwd: string;
441441
env: Record<string, string | undefined>;
442442
timeoutMs: number;
443-
}): Promise<{ status: number | null; stdout: string }> {
443+
}): Promise<{ status: number | null }> {
444444
return await new Promise((resolve) => {
445445
const child = spawn(opts.shell, [...opts.shellArgs, opts.command], {
446446
cwd: opts.cwd,
447447
detached: process.platform !== "win32",
448448
env: opts.env,
449-
stdio: ["ignore", "pipe", "ignore"],
449+
stdio: "ignore",
450450
windowsHide: true,
451451
});
452-
let stdout = "";
453452
let settled = false;
454453
const finish = (status: number | null) => {
455454
if (settled) {
@@ -458,18 +457,12 @@ async function runShell(opts: {
458457
settled = true;
459458
clearTimeout(timeout);
460459
killProcessTree(child.pid ?? 0, { graceMs: 0 });
461-
child.stdout.destroy();
462-
resolve({ status, stdout });
460+
resolve({ status });
463461
};
464462
const timeout = setTimeout(() => {
465463
killProcessTree(child.pid ?? 0, { graceMs: 250 });
466464
finish(null);
467465
}, opts.timeoutMs);
468-
child.stdout.setEncoding("utf8");
469-
child.stdout.on("data", (chunk) => {
470-
stdout += chunk;
471-
});
472-
child.stdout.on("error", () => {});
473466
child.on("error", () => {
474467
finish(null);
475468
});

0 commit comments

Comments
 (0)