Skip to content

Commit 064efce

Browse files
authored
fix(agents): report node exec timeouts and process kills truthfully (#125466)
Two model-visible outcome bugs in the exec surface: - formatNodeRunToolResult read only stdout/stderr/error, so a node-host timeout rendered as '(no output)' and a nonzero exit with stdout read as success; details carried no timedOut. Render the same timeout marker + retry guidance and exit-code note the local/gateway host produces, and record timedOut in details (the node detached follow-up path already rendered both). - process kill and remove-running returned details.status 'failed' for a successfully performed termination, flagging the tool call as an error and inviting the model to retry a kill that worked. The finished-session branches already return 'completed'.
1 parent 720da74 commit 064efce

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎src/agents/bash-tools.exec-host-node-phases.test.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,36 @@ describe("direct node run", () => {
179179
const result = await invokeNodeSystemRunDirect(createDirectNodeRun());
180180
const visibleText = result.content[0]?.type === "text" ? result.content[0].text : "";
181181

182-
expect(visibleText).toBe(`${stdout}\n${stderr}\n${errorText}`);
182+
expect(visibleText).toBe(`${stdout}\n${stderr}\n${errorText}\n(Command exited with code 1)`);
183183
expect(result.details).toMatchObject({ aggregated: visibleText });
184184
});
185185

186+
it("renders a nonzero exit code in the model-visible text", async () => {
187+
callGatewayToolMock.mockResolvedValueOnce({
188+
payload: { success: false, stdout: "done", stderr: "", error: null, exitCode: 3 },
189+
});
190+
191+
const result = await invokeNodeSystemRunDirect(createDirectNodeRun());
192+
const visibleText = result.content[0]?.type === "text" ? result.content[0].text : "";
193+
194+
// Output alone must not read as success when the command failed.
195+
expect(visibleText).toContain("done");
196+
expect(visibleText).toContain("(Command exited with code 3)");
197+
expect(result.details).toMatchObject({ status: "failed", exitCode: 3 });
198+
});
199+
200+
it("renders a timeout marker and records timedOut in details", async () => {
201+
callGatewayToolMock.mockResolvedValueOnce({
202+
payload: { success: false, stdout: "", stderr: "", error: null, timedOut: true },
203+
});
204+
205+
const result = await invokeNodeSystemRunDirect(createDirectNodeRun());
206+
const visibleText = result.content[0]?.type === "text" ? result.content[0].text : "";
207+
208+
expect(visibleText).toContain("Command timed out.");
209+
expect(result.details).toMatchObject({ status: "failed", timedOut: true });
210+
});
211+
186212
it("never dispatches a direct node run after cancellation", async () => {
187213
const controller = new AbortController();
188214
const reason = new Error("cancelled before direct node dispatch");

‎src/agents/bash-tools.exec-host-node-phases.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
invokeNodeSystemRun,
4343
} from "./bash-tools.exec-host-node-failure.js";
4444
import type { ExecuteNodeHostCommandParams } from "./bash-tools.exec-host-node.types.js";
45-
import { renderExecUpdateText } from "./bash-tools.exec-output.js";
45+
import { appendExecTimeoutRetryGuidance, renderExecUpdateText } from "./bash-tools.exec-output.js";
4646
import type { ExecToolDetails } from "./bash-tools.exec-types.js";
4747
import type { AgentToolResult } from "./runtime/index.js";
4848
import { callGatewayTool } from "./tools/gateway.js";
@@ -253,7 +253,15 @@ export function formatNodeRunToolResult(params: {
253253
const errorText = typeof payloadObj.error === "string" ? payloadObj.error : "";
254254
const success = typeof payloadObj.success === "boolean" ? payloadObj.success : false;
255255
const exitCode = typeof payloadObj.exitCode === "number" ? payloadObj.exitCode : null;
256-
const output = [stdout, stderr, errorText].filter(Boolean).join("\n");
256+
const timedOut = payloadObj.timedOut === true;
257+
// Failure must be visible in the text the model reads, matching the
258+
// local/gateway host rendering — output alone reads as success.
259+
const outcomeNote = timedOut
260+
? appendExecTimeoutRetryGuidance("Command timed out.", "overall-timeout")
261+
: !success && exitCode !== null && exitCode !== 0
262+
? `(Command exited with code ${exitCode})`
263+
: "";
264+
const output = [stdout, stderr, errorText, outcomeNote].filter(Boolean).join("\n");
257265
return {
258266
content: [
259267
{
@@ -269,6 +277,7 @@ export function formatNodeRunToolResult(params: {
269277
exitCode,
270278
durationMs: Date.now() - params.startedAt,
271279
aggregated: output,
280+
...(timedOut ? { timedOut: true } : {}),
272281
cwd: params.cwd,
273282
} satisfies ExecToolDetails,
274283
};

‎src/agents/bash-tools.process.e2e.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ test.skipIf(process.platform === "win32")(
416416
sessionId,
417417
});
418418
expect(textContent(killed)).toBe(`Termination requested for session ${sessionId}.`);
419+
// A performed kill must not read as a failed tool call.
420+
expect(killed.details).toMatchObject({ status: "completed" });
419421

420422
await expect
421423
.poll(

‎src/agents/bash-tools.process.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ export function createProcessTool(
665665
);
666666
}
667667
resetPollRetrySuggestion(params.sessionId);
668+
// The kill was performed; "failed" here would flag a successful
669+
// action as a tool error and invite the model to retry it.
668670
return {
669671
content: [
670672
{
@@ -673,7 +675,7 @@ export function createProcessTool(
673675
},
674676
],
675677
details: {
676-
status: "failed",
678+
status: "completed",
677679
name: scopedSession ? deriveSessionName(scopedSession.command) : undefined,
678680
},
679681
};
@@ -716,6 +718,8 @@ export function createProcessTool(
716718
scopedSession.backgrounded = false;
717719
deleteSession(params.sessionId);
718720
resetPollRetrySuggestion(params.sessionId);
721+
// Removal succeeded (termination requested + registry row dropped);
722+
// match the finished-session remove branch's success shape.
719723
return {
720724
content: [
721725
{
@@ -724,7 +728,7 @@ export function createProcessTool(
724728
},
725729
],
726730
details: {
727-
status: "failed",
731+
status: "completed",
728732
name: scopedSession ? deriveSessionName(scopedSession.command) : undefined,
729733
},
730734
};

0 commit comments

Comments
 (0)