Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(codex): avoid replaying turn activity after start RPC failures
Sebastian Bdc1f02aa3da8
diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.tsindex f614311c6a..9c743059f3 100644--- a/server/drivers/codex.test.ts+++ b/server/drivers/codex.test.ts@@ -14,6 +14,7 @@ import { ChatGptPlanAuthController } from "./chatgpt-plan-auth.ts";import { recordEvents, type EventRecorder } from "../testing/events.ts";import { CodexDriver, codexNativeIncomingLogMessage, codexUpdateCommand } from "./codex.ts";import { codexCliOverrides } from "./codex-process.ts";+import * as codexProcess from "./codex-process.ts";import { CODEX_SIGN_IN_EXPIRED, codexNotSignedIn, codexSignInRefused, codexUserError } from "./codex-errors.ts";import { CODEX_USAGE_LIMIT_REACHED } from "../../shared/codex-usage-limit.ts";import { removeTempDir } from "../testing/cleanup.ts";@@ -3386,6 +3387,22 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)expect(recorder.events.some((e) => e.type === "turn.retrying")).toBe(false);}, 20_000);+ it.each(["timeout", "error"])("does not replay a tool completed before a turn/start %s", async (reply) => {+ const actions = join(scratch, "early-tool-actions.jsonl");+ const rpcTimeout = codexProcess.codexRpcTimeoutMs;+ vi.spyOn(codexProcess, "codexRpcTimeoutMs").mockImplementation((method) => method === "turn/start" ? 300 : rpcTimeout(method));+ vi.stubEnv("FAKE_CODEX_ACTION_LOG", actions);+ vi.stubEnv("FAKE_CODEX_RETRY_SCALE", "0.001");+ await create({ mode: `early-tool-${reply}`, environment: { HOME: scratch, CODEX_HOME: join(scratch, ".codex") } });+ await instance.adapter.sendTurn({ threadId: `t-early-tool-${reply}`, text: "record one action" });+ const done = await recorder.until((event) => event.type === "turn.completed");++ expect(readFileSync(actions, "utf8").trim().split("\n")).toHaveLength(1);+ expect(done).toMatchObject({ ok: false, stopReason: "rpc_error" });+ expect(recorder.events.filter((event) => event.type === "turn.retrying")).toHaveLength(0);+ expect(recorder.events.filter((event) => event.type === "turn.completed")).toHaveLength(1);+ });+it("retries a transient app-server crash before the turn starts", async () => {process.env.FAKE_CODEX_LAUNCH_CRASHES = "1";process.env.FAKE_CODEX_STATE = join(scratch, "codex-launch-crash");diff --git a/server/drivers/codex.ts b/server/drivers/codex.tsindex 572f15c660..5a4187e70d 100644--- a/server/drivers/codex.ts+++ b/server/drivers/codex.ts@@ -1641,7 +1641,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {const message = refused ? codexSignInExpired(raw) : codexUserError(raw, route);const needsAuth = refused || /(?:\b401\b|unauthorized|missing bearer|authentication required)/i.test(raw);const verdict = classifyError(failure);- if (!state.settled && !abandoned && !recoveredMissingSession && !stopRequested && !needsAuth && verdict.transient && attempt < RETRY_MAX_ATTEMPTS - 1 && state.sawStreamDelta === false) {+ if (!state.settled && !abandoned && !recoveredMissingSession && !stopRequested && !needsAuth && codexTurnId === null && earlyNotifications.length === 0 && verdict.transient && attempt < RETRY_MAX_ATTEMPTS - 1 && state.sawStreamDelta === false) {return relaunch(verdict.reason, message, "rpc_error");}if (!state.settled && !abandoned) {diff --git a/server/testing/fake-codex-app-server.ts b/server/testing/fake-codex-app-server.tsindex cad78803c3..a435e09613 100755--- a/server/testing/fake-codex-app-server.ts+++ b/server/testing/fake-codex-app-server.ts@@ -18,6 +18,7 @@// FAKE_CODEX_ASYNC_QUESTIONS async-question mode: JSON replacing the message's `questions`// FAKE_CODEX_ASYNC_QUESTION_GATE async-question mode: the turn keeps running until this file exists (default 300 ms)// FAKE_CODEX_DUMP path for {pid, argv, env, calls, decision}+// FAKE_CODEX_ACTION_LOG early-tool-timeout/error: append an action before turn/start times out or fails// FAKE_CODEX_ECHO_INPUT "1": echo turn/start's input back as a userMessage item, as codex-cli 0.164.0 does// FAKE_CODEX_PATCH_BYTES each turn first writes web/index.html by apply_patch, streamed as two// item/fileChange/patchUpdated notifications (codex-cli 0.160.1's schema) of half and all this many bytes@@ -608,6 +609,12 @@ process.stdin.on("data", (chunk) => {dump();logStartup("turn/start");nativeThreadId = msg.params?.threadId ?? nativeThreadId;+ if (mode === "early-tool-timeout" || mode === "early-tool-error") {+ if (process.env.FAKE_CODEX_ACTION_LOG) appendFileSync(process.env.FAKE_CODEX_ACTION_LOG, `${JSON.stringify({ action: "record", pid: process.pid })}\n`);+ notify("item/completed", { item: { id: "early-tool", type: "commandExecution", command: "record", status: "completed", aggregatedOutput: "recorded" } });+ if (mode === "early-tool-error") out({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: "connection reset by peer" } });+ break;+ }// Counts turn launches only, not the catalog spawn during create().if (process.env.FAKE_CODEX_LAUNCH_CRASHES && process.env.FAKE_CODEX_STATE) {let launched = 0;