Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(codex): trail the per-turn clock line so a session keeps a real title

Hugoc5be420bb1cc
diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.ts
index 2f4e8434be..5ab66cb1db 100644
--- a/server/drivers/codex.test.ts
+++ b/server/drivers/codex.test.ts
@@ -26,6 +26,7 @@ import { parseToml } from "../toml-lite.ts";
import * as codexProcess from "./codex-process.ts";
import { CODEX_SIGN_IN_EXPIRED, codexLimitFacts, codexNotSignedIn, codexSignInRefused, codexUserError } from "./codex-errors.ts";
import { CODEX_USAGE_LIMIT_REACHED } from "../../shared/codex-usage-limit.ts";
+import { turnClockLine } from "../turn-clock.ts";
import { removeTempDir } from "../testing/cleanup.ts";
import { TEAM_TOOLS_FAILED } from "./team-tools.ts";
import { unsupportedCallsNotice } from "./codex-unsupported-calls.ts";
@@ -1237,6 +1238,21 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)
expect(recorder.events.some((event) => event.type === "runtime.error" && event.message.includes("cannot safely update bot instructions"))).toBe(true);
});
+ it("trails the per-turn clock line, so a Codex session's first line stays the person's words", async () => {
+ vi.stubEnv("FAKE_CODEX_ECHO_INPUT", "1");
+ await create();
+ const dump = join(scratch, "clock.json");
+ process.env.FAKE_CODEX_DUMP = dump;
+ const clock = turnClockLine(Date.UTC(2026, 9, 13, 9, 30), "UTC");
+
+ await instance.adapter.sendTurn({ threadId: "t-clock-trails", text: `${clock}\n\ncut the middle` });
+ await recorder.until((event) => event.type === "turn.completed");
+
+ const seen = JSON.parse(readFileSync(dump, "utf8"));
+ const turnStart = seen.calls.find((call: { method: string }) => call.method === "turn/start");
+ expect(turnStart.params.input).toEqual([{ type: "text", text: `cut the middle\n\n${clock}` }]);
+ });
+
it("sends current-turn images inline, so the boxed Codex never opens an attachments path", async () => {
vi.stubEnv("FAKE_CODEX_ECHO_INPUT", "1");
await create();
diff --git a/server/drivers/codex.ts b/server/drivers/codex.ts
index 14975332e9..b107c7547d 100644
--- a/server/drivers/codex.ts
+++ b/server/drivers/codex.ts
@@ -2,7 +2,7 @@ import { decodeRoutedCodexConfig, managedCodexArgs, endpointCodexArgs, type Code
export { managedCodexArgs, endpointCodexArgs } from "./codex-endpoint-config.ts";
import { codexActionFailure } from "./codex-action-notice.ts";
import { codexToolSurfaceArgs } from "./codex-tool-surface.ts";
-import { codexShellDisabled, codexTurnConfigArgs } from "./codex-guest.ts"; import { shellOff } from "./shell-off.ts";
+import { codexShellDisabled, codexTurnConfigArgs } from "./codex-guest.ts"; import { shellOff } from "./shell-off.ts"; import { withTrailingTurnClock } from "../turn-clock.ts";
import { TEAM_TOOLS_FAILED, teamToolsFailed } from "./team-tools.ts";
import { enabledMcpMounts, mcpStartupTracker, supportsMcpStartup } from "./codex-mcp-startup.ts";
import { boxAdmittedEnv, boxProxyEnvNames, customServerEnvBox, type McpApprovalMode, mountMcpServer } from "./codex-mcp-mount.ts";
@@ -1330,7 +1330,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {
emit({ ...base(threadId, turnId), type: "session.started", sessionId: codexThreadId, model: startedModel ?? turn.model ?? null, ...(rebuiltFromReplay ? { rebuilt: true } : {}) });
if (rebuiltFromReplay) emit({ ...base(threadId, turnId), type: "runtime.notice", message: droppedSession === "instruction-overflow" ? instructionsOverflowNotice("Codex") : rebuiltSessionNotice("Codex") });
const turnInput = [
- ...(promptText ? [{ type: "text" as const, text: promptText }] : []),
+ ...(promptText ? [{ type: "text" as const, text: withTrailingTurnClock(promptText) }] : []),
...await codexImageInputs(turn.images),
];
const startTurn = () => {
diff --git a/server/testing/turn-clock-text.ts b/server/testing/turn-clock-text.ts
index d7f371e3a2..4b9820b97f 100644
--- a/server/testing/turn-clock-text.ts
+++ b/server/testing/turn-clock-text.ts
@@ -1,8 +1,10 @@
// Exact-text assertions compare a turn without its per-turn clock line, whose minute depends on when the test runs.
+// Most engines lead with the clock; Codex trails it (see withTrailingTurnClock), so both ends are stripped.
import { TURN_CLOCK_LEAD } from "../turn-clock.ts";
-const CLOCK_LINE = new RegExp(`${TURN_CLOCK_LEAD}[^\\n]*\\n\\n`, "g");
+const CLOCK_LEAD = new RegExp(`${TURN_CLOCK_LEAD}[^\\n]*\\n\\n`, "g");
+const CLOCK_TAIL = new RegExp(`\\n\\n${TURN_CLOCK_LEAD}[^\\n]*$`, "g");
export function withoutTurnClock(text: string): string {
- return text.replace(CLOCK_LINE, "");
+ return text.replace(CLOCK_LEAD, "").replace(CLOCK_TAIL, "");
}
diff --git a/server/turn-clock.test.ts b/server/turn-clock.test.ts
index 6b0fcddd34..1067c38bc5 100644
--- a/server/turn-clock.test.ts
+++ b/server/turn-clock.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { hostTimeZone, takesTurnClock, turnClockLine, withTurnClock } from "./turn-clock.ts";
+import { hostTimeZone, takesTurnClock, turnClockLine, withTrailingTurnClock, withTurnClock } from "./turn-clock.ts";
const at = (iso: string) => Date.parse(iso);
@@ -76,3 +76,21 @@ describe("withTurnClock", () => {
expect(second).not.toContain("2026-10-08");
});
});
+
+describe("withTrailingTurnClock", () => {
+ const clock = turnClockLine(at("2026-10-08T16:48:00Z"), "Asia/Baghdad");
+
+ it("moves a leading clock line to the end, so the first line is the person's own words", () => {
+ expect(withTrailingTurnClock(`${clock}\n\ncut the middle`)).toBe(`cut the middle\n\n${clock}`);
+ });
+
+ it("carries a note riding on the clock line along with it", () => {
+ const noted = `${clock}\nnear its limit note\n\ncut the middle`;
+ expect(withTrailingTurnClock(noted)).toBe(`cut the middle\n\n${clock}\nnear its limit note`);
+ });
+
+ it("leaves a text that does not lead with the clock untouched", () => {
+ expect(withTrailingTurnClock("cut the middle")).toBe("cut the middle");
+ expect(withTrailingTurnClock(`${clock}\n\n`)).toBe(`${clock}\n\n`);
+ });
+});
diff --git a/server/turn-clock.ts b/server/turn-clock.ts
index 7dacf6bd41..c965866a47 100644
--- a/server/turn-clock.ts
+++ b/server/turn-clock.ts
@@ -46,3 +46,14 @@ export function withTurnClock(clock: string, text: string): string {
if (!clock || !takesTurnClock(text)) return text;
return `${clock}\n\n${text}`;
}
+
+/** Codex names a ChatGPT-account session from the first line of its first user message, so for that
+ * engine the clock must not lead it. Moves a leading clock line (with any note riding on the same
+ * line) to the end; a text that does not lead with the clock is returned unchanged. */
+export function withTrailingTurnClock(text: string): string {
+ if (!text.startsWith(TURN_CLOCK_LEAD)) return text;
+ const breakAt = text.indexOf("\n\n");
+ if (breakAt === -1) return text;
+ const rest = text.slice(breakAt + 2);
+ return rest ? `${rest}\n\n${text.slice(0, breakAt)}` : text;
+}