Skip to content

Commit b62eb4d

Browse files
committed
fix(qa): retain child output on CLI timeouts
1 parent 76ac472 commit b62eb4d

3 files changed

Lines changed: 87 additions & 7 deletions

File tree

‎extensions/qa-lab/src/suite-runtime-agent-process.integration.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,46 @@ describe("qa suite runtime CLI integration", () => {
7272
status: "ok",
7373
});
7474
});
75+
76+
it("retains real child output when the qa cli times out", async () => {
77+
const repoRoot = await mkdtemp(path.join(os.tmpdir(), "qa-cli-timeout-repo-"));
78+
const tempRoot = await mkdtemp(path.join(os.tmpdir(), "qa-cli-timeout-runtime-"));
79+
cleanups.push(async () => {
80+
await rm(repoRoot, { recursive: true, force: true });
81+
await rm(tempRoot, { recursive: true, force: true });
82+
});
83+
const distDir = path.join(repoRoot, "dist");
84+
await mkdir(distDir, { recursive: true });
85+
await writeFile(
86+
path.join(distDir, "index.js"),
87+
[
88+
'process.stdout.write("timeout stdout marker\\n");',
89+
'process.stderr.write("timeout stderr marker\\n");',
90+
"setInterval(() => {}, 60_000);",
91+
"",
92+
].join("\n"),
93+
"utf8",
94+
);
95+
96+
const error = await runQaCli(
97+
{
98+
repoRoot,
99+
gateway: {
100+
tempRoot,
101+
runtimeEnv: process.env,
102+
},
103+
primaryModel: "openai/gpt-5.6-luna",
104+
alternateModel: "openai/gpt-5.6-luna",
105+
providerMode: "mock-openai",
106+
} as never,
107+
["qa", "suite"],
108+
{ timeoutMs: 1_000 },
109+
).catch((value: unknown) => value);
110+
111+
expect(error).toMatchObject({ code: "qa_cli_timeout" });
112+
const message = error instanceof Error ? error.message : String(error);
113+
expect(message).toContain("qa cli timed out: openclaw qa suite");
114+
expect(message).toContain("stdout:\ntimeout stdout marker");
115+
expect(message).toContain("stderr:\ntimeout stderr marker");
116+
});
75117
});

‎extensions/qa-lab/src/suite-runtime-agent-process.test.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,45 @@ describe("qa suite runtime agent process helpers", () => {
174174

175175
it.runIf(process.platform !== "win32")("kills timed-out qa cli process groups", async () => {
176176
const killSpy = vi.spyOn(process, "kill").mockImplementation(() => true);
177+
vi.useFakeTimers();
177178
try {
178179
const child = createSpawnedProcess({ pid: 12345 });
179180
const { pending } = startMockQaCli({
180181
args: ["qa", "suite"],
181182
child,
182183
options: { timeoutMs: 1 },
183184
});
184-
const timeoutAssertion = expect(pending).rejects.toThrow(
185-
"qa cli timed out: openclaw qa suite",
185+
const errorPromise = pending.catch((value: unknown) => value);
186+
await Promise.resolve();
187+
expect(spawnMock).toHaveBeenCalledTimes(1);
188+
child.stdout.emit(
189+
"data",
190+
Buffer.from(
191+
`stdout-head-marker\n${"x".repeat(QA_CHILD_STDOUT_MAX_BYTES)}\nstdout-tail-marker`,
192+
),
186193
);
187-
188-
await waitForSpawnCount(1);
189-
await timeoutAssertion;
194+
child.stderr.emit(
195+
"data",
196+
Buffer.from(
197+
`stderr-head-marker\n${"x".repeat(QA_CHILD_STDERR_TAIL_BYTES)}\nstderr-tail-marker`,
198+
),
199+
);
200+
await vi.advanceTimersByTimeAsync(1);
201+
202+
const error = await errorPromise;
203+
expect(error).toMatchObject({ code: "qa_cli_timeout" });
204+
const message = error instanceof Error ? error.message : String(error);
205+
expect(message).toContain("qa cli timed out: openclaw qa suite");
206+
expect(message).toContain("stdout:\n[qa cli stdout truncated to last");
207+
expect(message).toContain("stdout-tail-marker");
208+
expect(message).not.toContain("stdout-head-marker");
209+
expect(message).toContain("stderr:\n[qa cli stderr truncated to last");
210+
expect(message).toContain("stderr-tail-marker");
211+
expect(message).not.toContain("stderr-head-marker");
190212
expect(killSpy).toHaveBeenCalledWith(-12345, "SIGKILL");
191213
expect(child.kill).not.toHaveBeenCalled();
192214
} finally {
215+
vi.useRealTimers();
193216
killSpy.mockRestore();
194217
}
195218
});

‎extensions/qa-lab/src/suite-runtime-agent-process.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ async function runQaCli(
265265
opts?: { timeoutMs?: number; json?: boolean; env?: NodeJS.ProcessEnv },
266266
) {
267267
const stdout = createQaChildOutputCapture();
268+
const stdoutTail = createQaChildOutputTail();
268269
const stderr = createQaChildOutputTail();
269270
const distEntryPath = path.join(env.repoRoot, "dist", "index.js");
270271
const nodeExecPath = await resolveQaNodeExecPath();
@@ -281,11 +282,25 @@ async function runQaCli(
281282
const timeoutMs = resolveTimerTimeoutMs(opts?.timeoutMs, 60_000);
282283
const timeout = setTimeout(() => {
283284
signalQaCliProcessTree(child, "SIGKILL");
285+
const stdoutText = formatQaChildOutputTail(stdoutTail, "qa cli stdout");
286+
const stderrText = formatQaChildOutputTail(stderr, "qa cli stderr");
287+
const diagnostics = [
288+
stdoutText ? `stdout:\n${stdoutText}` : "",
289+
stderrText ? `stderr:\n${stderrText}` : "",
290+
]
291+
.filter(Boolean)
292+
.join("\n");
284293
reject(
285-
new QaSuiteInfraError("qa_cli_timeout", `qa cli timed out: openclaw ${args.join(" ")}`),
294+
new QaSuiteInfraError(
295+
"qa_cli_timeout",
296+
`qa cli timed out: openclaw ${args.join(" ")}${diagnostics ? `\n${diagnostics}` : ""}`,
297+
),
286298
);
287299
}, timeoutMs);
288-
child.stdout.on("data", (chunk) => appendQaChildOutput(stdout, chunk));
300+
child.stdout.on("data", (chunk) => {
301+
appendQaChildOutput(stdout, chunk);
302+
appendQaChildOutputTail(stdoutTail, chunk);
303+
});
289304
child.stderr.on("data", (chunk) => appendQaChildOutputTail(stderr, chunk));
290305
child.once("error", (error) => {
291306
clearTimeout(timeout);

0 commit comments

Comments
 (0)