Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: prevent Codex non-zero exit code from overriding completed turns (#30)

Gener9cd8f787fb17
diff --git a/src/cli/daemon/agent/__tests__/codex.test.ts b/src/cli/daemon/agent/__tests__/codex.test.ts
index c9d95f5b0..2612ad315 100644
--- a/src/cli/daemon/agent/__tests__/codex.test.ts
+++ b/src/cli/daemon/agent/__tests__/codex.test.ts
@@ -856,6 +856,58 @@ describe("CodexBackend", () => {
expect(messages).not.toContainEqual({ type: "text", content: "ignored" });
});
+ it("turn completed successfully + non-zero exit code → status completed (not failed)", async () => {
+ const session = backend.execute("hello", { cwd: "/tmp" });
+ const mock = getMock();
+
+ await completeHandshake();
+ sendNotification("turn/completed", { turn: { id: "turn_1", status: "completed" } });
+ mock.proc.emit("close", 1);
+
+ const result = await session.result;
+ expect(result.status).toBe("completed");
+ });
+
+ it("non-zero exit code before turn completes → status failed", async () => {
+ const session = backend.execute("hello", { cwd: "/tmp" });
+ const mock = getMock();
+
+ await completeHandshake();
+ sendNotification("turn/started", { turn: { id: "turn_1" } });
+ // Process exits before turn/completed
+ mock.proc.emit("close", 1);
+
+ const result = await session.result;
+ expect(result.status).toBe("failed");
+ });
+
+ it("turn completed successfully + non-zero exit + turnError → status failed with turnError", async () => {
+ const session = backend.execute("hello", { cwd: "/tmp" });
+ const mock = getMock();
+
+ await completeHandshake();
+ sendNotification("error", { error: { message: "rate limit" }, willRetry: false });
+ sendNotification("turn/completed", { turn: { id: "turn_1", status: "completed" } });
+ mock.proc.emit("close", 1);
+
+ const result = await session.result;
+ expect(result.status).toBe("failed");
+ expect(result.error).toBe("rate limit");
+ });
+
+ it("strips ANSI escape codes from stderr", async () => {
+ const session = backend.execute("hello", { cwd: "/tmp" });
+ const mock = getMock();
+
+ await completeHandshake();
+ mock.stderr.push("\x1b[31mERROR\x1b[0m something broke\n");
+ mock.proc.emit("close", 1);
+
+ const result = await session.result;
+ expect(result.error).toBe("ERROR something broke\n");
+ expect(result.error).not.toContain("\x1b[");
+ });
+
it("handles invalid JSON gracefully", async () => {
const session = backend.execute("hello", { cwd: "/tmp" });
const mock = getMock();
diff --git a/src/cli/daemon/agent/codex.ts b/src/cli/daemon/agent/codex.ts
index 45b6dfa49..f1e557f06 100644
--- a/src/cli/daemon/agent/codex.ts
+++ b/src/cli/daemon/agent/codex.ts
@@ -78,6 +78,7 @@ export class CodexBackend implements AgentBackend {
// Turn lifecycle state
let turnStarted = false;
let turnDoneTriggered = false;
+ let turnCompletedSuccessfully = false;
let lastCompletedTurnId = "";
let turnError = "";
@@ -210,6 +211,7 @@ export class CodexBackend implements AgentBackend {
const status = (turn?.status as string) || (params.status as string) || "";
if (status === "completed" || status === "finished") {
+ turnCompletedSuccessfully = true;
triggerTurnDone(false);
} else if (status === "cancelled" || status === "aborted" || status === "interrupted") {
triggerTurnDone(true);
@@ -504,11 +506,11 @@ export class CodexBackend implements AgentBackend {
if (timedOut) {
resultStatus = "timeout";
- } else if (code !== 0 && resultStatus === "completed") {
+ } else if (code !== 0 && resultStatus === "completed" && !turnCompletedSuccessfully) {
resultStatus = "failed";
}
- const stderr = stderrChunks.join("");
+ const stderr = stderrChunks.join("").replace(/\x1b\[[0-9;]*m/g, "");
if (stderr && !lastError) {
lastError = stderr;
}
diff --git a/src/cli/daemon/session-runner.test.ts b/src/cli/daemon/session-runner.test.ts
index 61fd2137f..f1274cca2 100644
--- a/src/cli/daemon/session-runner.test.ts
+++ b/src/cli/daemon/session-runner.test.ts
@@ -221,6 +221,24 @@ describe("session-runner runSession", () => {
expect(mockClientInstance.completeTask).not.toHaveBeenCalled();
});
+ it("uses 'agent exited unexpectedly' when result.error is empty on failure", async () => {
+ setupBackend([], {
+ status: "failed",
+ output: "",
+ error: "",
+ durationMs: 100,
+ sessionId: "sess-1",
+ });
+
+ await runSession(makeInput());
+
+ expect(mockClientInstance.failTask).toHaveBeenCalledWith(
+ "test_token",
+ "t1",
+ "agent exited unexpectedly",
+ );
+ });
+
it("writes timeline init entry with session runner PID (process.pid)", async () => {
setupBackend([], {
status: "completed",
diff --git a/src/cli/daemon/session-runner.ts b/src/cli/daemon/session-runner.ts
index f3815607c..147749507 100644
--- a/src/cli/daemon/session-runner.ts
+++ b/src/cli/daemon/session-runner.ts
@@ -362,7 +362,7 @@ export async function runSession(input: SessionRunnerInput): Promise<void> {
updateEntry(timelineDir, task.id, (entry) => {
entry.pid = null;
entry.status = "failed";
- entry.errmsg = result.error || "unknown error";
+ entry.errmsg = result.error || "agent exited unexpectedly";
});
}
@@ -381,7 +381,7 @@ export async function runSession(input: SessionRunnerInput): Promise<void> {
const dur = (result.durationMs / 1000).toFixed(1);
log.info(`completed (duration=${dur}s, messages=${seq}, tools=${toolCount})`);
} else {
- const errorMsg = result.error || "unknown error";
+ const errorMsg = result.error || "agent exited unexpectedly";
await reportToServer(
() => client.failTask(token, task.id, errorMsg),
{ taskId: task.id, type: "fail", payload: { error: errorMsg }, token, serverURL, createdAt: new Date().toISOString() },