Skip to content

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

Train E2: Codex starts a fresh session for instruction blocks only at an edit, and only with history to replay

milind-soni5b740a190ae3
diff --git a/server/drivers/codex-instructions.test.ts b/server/drivers/codex-instructions.test.ts
index 9fe98ef9cd..546ebf52c5 100644
--- a/server/drivers/codex-instructions.test.ts
+++ b/server/drivers/codex-instructions.test.ts
@@ -191,16 +191,30 @@ describe("Codex instruction receipts", () => {
expect(request).toHaveBeenCalledTimes(1);
});
- it("asks for a fresh session even without a new edit once the accumulated blocks pass the provider cap", async () => {
+ it("keeps a thread past the provider cap while its block is unchanged, and asks for a fresh session at the next edit", async () => {
const key = randomUUID();
const request = vi.fn().mockResolvedValue({});
await syncCodexInstructions(key, "native", "rules", "", false, request);
const path = receiptPath(key, "native");
const receipt = JSON.parse(readFileSync(path, "utf8"));
writeFileSync(path, JSON.stringify({ ...receipt, injected: CODEX_INJECTED_CHAR_LIMIT + 1 }), { mode: 0o600 });
- const result = await syncCodexInstructions(key, "native", "rules", "", true, request);
- expect(result).toEqual({ deliverVolatile: false, hadVolatile: false, commitVolatile: null, resetSession: true });
+ const unchanged = await syncCodexInstructions(key, "native", "rules", "", true, request);
+ expect(unchanged.resetSession).toBe(false);
+ const edited = await syncCodexInstructions(key, "native", "rules v2", "", true, request);
+ expect(edited).toEqual({ deliverVolatile: false, hadVolatile: false, commitVolatile: null, resetSession: true });
expect(request).not.toHaveBeenCalled();
expect(JSON.parse(readFileSync(path, "utf8")).injected).toBe(CODEX_INJECTED_CHAR_LIMIT + 1);
});
+
+ it("injects the edit as before when the conversation has no history to start a fresh session from", async () => {
+ const key = randomUUID();
+ const request = vi.fn().mockResolvedValue({});
+ await syncCodexInstructions(key, "native", "rules", "", false, request);
+ const path = receiptPath(key, "native");
+ const receipt = JSON.parse(readFileSync(path, "utf8"));
+ writeFileSync(path, JSON.stringify({ ...receipt, injected: CODEX_INJECTED_CHAR_LIMIT + 1 }), { mode: 0o600 });
+ const result = await syncCodexInstructions(key, "native", "rules v2", "", true, request, false, false);
+ expect(result.resetSession).toBe(false);
+ expect(request).toHaveBeenCalledTimes(1);
+ });
});
diff --git a/server/drivers/codex-instructions.ts b/server/drivers/codex-instructions.ts
index 9647496f5c..83fbb42c6f 100644
--- a/server/drivers/codex-instructions.ts
+++ b/server/drivers/codex-instructions.ts
@@ -35,7 +35,7 @@ export interface CodexInstructionReceipt {
injected?: number;
}
-// Providers merge developer content into one system prompt with a hard character cap (issue #5317: GLM rejects above 100,000); 85,000 leaves room for Codex's own developer blocks.
+// Providers merge developer content into one system prompt with a hard character cap (GLM rejects above 100,000); 85,000 leaves room for Codex's own developer blocks.
export const CODEX_INJECTED_CHAR_LIMIT = 85_000;
const isFingerprint = (value: string) => /^[0-9a-f]{64}$/.test(value);
@@ -84,6 +84,7 @@ export async function syncCodexInstructions(
resumed: boolean,
request: (method: string, params: Record<string, unknown>) => Promise<unknown>,
mentionTurn = false,
+ canReset = true,
): Promise<{ deliverVolatile: boolean; hadVolatile: boolean; commitVolatile: (() => void) | null; resetSession: boolean }> {
const directory = join(DATA_DIR, "codex-instructions");
const path = join(directory, `${digest(JSON.stringify([key, nativeThreadId]))}.sha256`);
@@ -100,9 +101,10 @@ export async function syncCodexInstructions(
const hadVolatile = typeof previous?.volatile === "string" && previous.volatile !== digest("");
const replacement = "The following replaces the previous developer instruction block supplied by OpenMausBot, including its native configured rules and bot-specific instructions. Other Codex instructions and permissions still apply.\n\n"
+ (instructions || "No OpenMausBot bot-specific instructions remain.");
- if (resumed && injected + (previous?.instructions === fingerprint ? 0 : replacement.length) > CODEX_INJECTED_CHAR_LIMIT) {
+ if (canReset && resumed && previous?.instructions !== fingerprint && injected + replacement.length > CODEX_INJECTED_CHAR_LIMIT) {
// Superseded blocks stay in the thread until codex compacts, so another injection would only push the
- // provider's system prompt past its cap; the driver's fresh-session recovery is the only cleanup.
+ // provider's system prompt past its cap; the driver's fresh-session recovery is the only cleanup. Only a changed
+ // block is injected, so a thread whose first block alone is that long is not reset on every turn.
return { deliverVolatile: false, hadVolatile, commitVolatile: null, resetSession: true };
}
if (resumed && previous?.instructions !== fingerprint) {
diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.ts
index db7428a9b9..2f4e8434be 100644
--- a/server/drivers/codex.test.ts
+++ b/server/drivers/codex.test.ts
@@ -882,7 +882,8 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)
const instructions = `Stable rules.${codexSandboxSuffix}`;
const receiptPath = join(DATA_DIR, "codex-instructions", `${createHash("sha256").update(JSON.stringify(["t-overflow", "codex-thread-1"])).digest("hex")}.sha256`);
mkdirSync(dirname(receiptPath), { recursive: true });
- writeFileSync(receiptPath, JSON.stringify({ instructions: createHash("sha256").update(instructions).digest("hex"), injected: 90_000 }), { mode: 0o600 });
+ // An earlier block, with the thread's blocks already near the cap: injecting this changed one would cross it.
+ writeFileSync(receiptPath, JSON.stringify({ instructions: createHash("sha256").update("Older rules.").digest("hex"), injected: 84_900 }), { mode: 0o600 });
const dump = join(scratch, "instruction-overflow.json");
process.env.FAKE_CODEX_DUMP = dump;
const recoveryText = "History\nUser: go on";
diff --git a/server/drivers/codex.ts b/server/drivers/codex.ts
index d6929a6076..14975332e9 100644
--- a/server/drivers/codex.ts
+++ b/server/drivers/codex.ts
@@ -1319,7 +1319,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {
}
if (!codexThreadId) throw new Error("Codex did not return a native thread id");
const { deliverVolatile, hadVolatile, commitVolatile, resetSession } = await syncCodexInstructions(threadId, codexThreadId, instructionsFor(approvalParams),
- promptSplit ? turn.systemVolatile ?? "" : "", resumedNativeThread, request, Boolean(turn.mentionTurn));
+ promptSplit ? turn.systemVolatile ?? "" : "", resumedNativeThread, request, Boolean(turn.mentionTurn), Boolean(turn.recoveryText?.trim()));
if (resetSession) { droppedSession = "instruction-overflow"; return relaunch("instruction_overflow", "Codex did not shut down while moving this conversation to a fresh session", "rpc_error"); }
if (deliverVolatile) {
promptText = withContextNote(