mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-17 06:21:36 +00:00
* feat(codex): add native session supervision * fix(codex): harden supervision integration * fix(codex): preserve locked harness ownership * fix(codex): fence native session archive * fix(codex): revalidate archive binding ownership * feat(codex): integrate supervision runtime * feat(sessions): preserve harness-owned execution * feat(sessions): persist harness ownership invariants * feat(gateway): enforce harness-owned sessions * feat(setup): enable detected Codex supervision * feat(mac): expose supervised Codex sessions * feat(ui): make Codex sessions actionable * docs(codex): document session supervision * test(codex): cover integration ownership * chore(i18n): refresh supervision inventories * fix(setup): finalize Codex activation atomically * test(codex): narrow binding store update * fix(sessions): preserve legacy model locks * test(macos): serialize Codex catalog fixtures * fix(sessions): preserve legacy lock admission * chore(i18n): reconcile supervision metadata * test(sessions): mark legacy lock fixture * fix(macos): drain final Codex catalog frame * docs: leave supervision note to release * style(macos): satisfy Codex catalog type length * chore: record session accessor seam owners * fix(macos): honor configured Codex supervision * fix(codex): preserve harness-owned model locks * fix(codex): satisfy supervision lint gates * chore(i18n): refresh native supervision inventory * fix(codex): align supervision validation contracts * fix(codex): close supervision boundary gaps * fix(codex): preserve supervision activation contracts * fix(codex): dispose standalone supervision runtime * fix(codex): pin supervised source connection * fix(plugins): bind delegated runs to exact session target * fix(codex): scope supervised sessions to configured agents * fix(codex): fingerprint effective supervision home * fix(codex): normalize supervision plugin policy * fix(codex): keep supervised bindings stable across upgrades * fix(codex): guard all supervised binding connections * fix(codex): preserve catalog filters and pending CAS identity * fix(codex): preserve supervision identity for diagnostics * fix(codex): bind uncertain commits to supervision connection * fix(codex): satisfy supervision type boundaries * fix(macos): reconcile current main validation * fix(codex): handle absent runtime config in supervision * fix(doctor): own local audio acceleration check * fix(codex): satisfy integration lint gates * fix(codex): satisfy lifecycle safety guards
666 lines
23 KiB
TypeScript
666 lines
23 KiB
TypeScript
// Codex tests cover startup binding plugin behavior.
|
|
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
readCodexAppServerBinding,
|
|
testCodexAppServerBindingStore,
|
|
writeCodexAppServerBinding,
|
|
} from "./session-binding.test-helpers.js";
|
|
import { rotateOversizedCodexAppServerStartupBinding as rotateStartupBindingImpl } from "./startup-binding.js";
|
|
|
|
function rotateOversizedCodexAppServerStartupBinding(
|
|
params: Omit<Parameters<typeof rotateStartupBindingImpl>[0], "bindingStore" | "identity">,
|
|
) {
|
|
return rotateStartupBindingImpl({
|
|
...params,
|
|
bindingStore: testCodexAppServerBindingStore,
|
|
identity: { kind: "session", agentId: "main", sessionId: params.sessionFile },
|
|
});
|
|
}
|
|
|
|
describe("Codex app-server startup binding", () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(async () => {
|
|
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-startup-binding-"));
|
|
});
|
|
|
|
afterEach(async () => {
|
|
vi.restoreAllMocks();
|
|
await fs.rm(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
async function writeExistingBinding(
|
|
sessionFile: string,
|
|
workspaceDir: string,
|
|
overrides: Partial<Parameters<typeof writeCodexAppServerBinding>[1]> = {},
|
|
) {
|
|
await writeCodexAppServerBinding(sessionFile, {
|
|
threadId: "thread-existing",
|
|
cwd: workspaceDir,
|
|
model: "gpt-5.4-codex",
|
|
modelProvider: "openai",
|
|
...overrides,
|
|
});
|
|
}
|
|
|
|
async function writeSessionRecord(sessionFile: string, record: Record<string, unknown>) {
|
|
await fs.mkdir(path.dirname(sessionFile), { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(path.dirname(sessionFile), "sessions.json"),
|
|
JSON.stringify({
|
|
"agent:main:session-1": {
|
|
sessionFile,
|
|
...record,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
|
|
it("does not use a default byte limit when maxActiveTranscriptBytes is unset", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
"x".repeat(2_000_000),
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding?.threadId).toBe("thread-existing");
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding?.threadId).toBe("thread-existing");
|
|
});
|
|
|
|
it("never rotates a provisional supervision source binding", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, {
|
|
connectionScope: "supervision",
|
|
supervisionSourceThreadId: "thread-existing",
|
|
preserveNativeModel: true,
|
|
conversationSourceTransferComplete: true,
|
|
pendingSupervisionBranch: {
|
|
sourceThreadId: "thread-existing",
|
|
lastTurnId: "turn-terminal",
|
|
},
|
|
});
|
|
await writeSessionRecord(sessionFile, { totalTokens: 999_999 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
"x".repeat(2_000_000),
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: "1k",
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding).toMatchObject({
|
|
threadId: "thread-existing",
|
|
pendingSupervisionBranch: {
|
|
sourceThreadId: "thread-existing",
|
|
lastTurnId: "turn-terminal",
|
|
},
|
|
});
|
|
await expect(readCodexAppServerBinding(sessionFile)).resolves.toMatchObject({
|
|
threadId: "thread-existing",
|
|
pendingSupervisionBranch: { sourceThreadId: "thread-existing" },
|
|
});
|
|
});
|
|
|
|
it("never rotates a materialized supervised native thread", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, {
|
|
connectionScope: "supervision",
|
|
supervisionSourceThreadId: "thread-source",
|
|
preserveNativeModel: true,
|
|
conversationSourceTransferComplete: true,
|
|
});
|
|
await writeSessionRecord(sessionFile, { totalTokens: 999_999 });
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
projectedTurnTokens: 999_999,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: { truncateAfterCompaction: true, maxActiveTranscriptBytes: "1b" },
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding).toMatchObject({
|
|
threadId: "thread-existing",
|
|
connectionScope: "supervision",
|
|
supervisionSourceThreadId: "thread-source",
|
|
});
|
|
await expect(readCodexAppServerBinding(sessionFile)).resolves.toMatchObject({
|
|
threadId: "thread-existing",
|
|
connectionScope: "supervision",
|
|
});
|
|
});
|
|
|
|
it("reuses the session record cache while sessions.json is unchanged", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const sessionsJson = path.join(path.dirname(sessionFile), "sessions.json");
|
|
const readFileSpy = vi.spyOn(fs, "readFile");
|
|
|
|
for (let i = 0; i < 2; i += 1) {
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: undefined,
|
|
});
|
|
expect(binding?.threadId).toBe("thread-existing");
|
|
}
|
|
|
|
const sessionStoreReads = readFileSpy.mock.calls.filter(
|
|
([file]) => typeof file === "string" && file === sessionsJson,
|
|
);
|
|
expect(sessionStoreReads).toHaveLength(1);
|
|
});
|
|
|
|
it("checks native rollout token pressure under default compaction config", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
`${JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
last_token_usage: {
|
|
total_tokens: 241_198,
|
|
},
|
|
model_context_window: 258_400,
|
|
},
|
|
},
|
|
})}\n`,
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: undefined,
|
|
});
|
|
|
|
expect(binding).toBeUndefined();
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding).toBeUndefined();
|
|
});
|
|
|
|
it("caps the default native reserve so small context windows keep prompt budget", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 100 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
`${JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
last_token_usage: {
|
|
total_tokens: 100,
|
|
},
|
|
model_context_window: 16_000,
|
|
},
|
|
},
|
|
})}\n`,
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: undefined,
|
|
});
|
|
|
|
expect(binding?.threadId).toBe("thread-existing");
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding?.threadId).toBe("thread-existing");
|
|
});
|
|
|
|
it("honors shorthand byte units for native rollout limits", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(path.join(rolloutDir, "rollout-thread-existing.jsonl"), "x".repeat(2_000));
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: "1k",
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding).toBeUndefined();
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding).toBeUndefined();
|
|
});
|
|
|
|
it("honors custom Codex home rollout files for native rollout limits", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
const codexHome = path.join(tempDir, "custom-codex-home");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(codexHome, "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(path.join(rolloutDir, "rollout-thread-existing.jsonl"), "x".repeat(2_000));
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
codexHome,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: 1_000,
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding).toBeUndefined();
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding).toBeUndefined();
|
|
});
|
|
|
|
it("uses current rollout token usage before cumulative usage", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
`${JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
total_token_usage: {
|
|
total_tokens: 300_000,
|
|
},
|
|
last_token_usage: {
|
|
total_tokens: 12_000,
|
|
},
|
|
},
|
|
},
|
|
})}\n`,
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: "1mb",
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding?.threadId).toBe("thread-existing");
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding?.threadId).toBe("thread-existing");
|
|
});
|
|
|
|
it("ignores stale session token totals for native rollout rotation", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, {
|
|
totalTokens: 300_000,
|
|
totalTokensFresh: false,
|
|
});
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
`${JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
last_token_usage: {
|
|
total_tokens: 12_000,
|
|
},
|
|
},
|
|
},
|
|
})}\n`,
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: "1mb",
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding?.threadId).toBe("thread-existing");
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding?.threadId).toBe("thread-existing");
|
|
});
|
|
|
|
it("clears native rollouts at Codex's reported model context window", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
const rolloutFile = path.join(rolloutDir, "rollout-thread-existing.jsonl");
|
|
await fs.writeFile(
|
|
rolloutFile,
|
|
[
|
|
JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
last_token_usage: {
|
|
total_tokens: 128_000,
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
model_context_window: 128_000,
|
|
},
|
|
},
|
|
}),
|
|
].join("\n") + "\n",
|
|
);
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: "1mb",
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding).toBeUndefined();
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding).toBeUndefined();
|
|
});
|
|
|
|
it("keeps native rollouts above the old guard when Codex still has context window headroom", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
`${JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
last_token_usage: {
|
|
total_tokens: 86_000,
|
|
},
|
|
model_context_window: 272_000,
|
|
},
|
|
},
|
|
})}\n`,
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: "1mb",
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding?.threadId).toBe("thread-existing");
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding?.threadId).toBe("thread-existing");
|
|
});
|
|
|
|
it("includes projected turn tokens in the native rollout pressure check", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
`${JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
last_token_usage: {
|
|
total_tokens: 220_000,
|
|
},
|
|
model_context_window: 258_400,
|
|
},
|
|
},
|
|
})}\n`,
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: undefined,
|
|
projectedTurnTokens: 30_000,
|
|
});
|
|
|
|
expect(binding).toBeUndefined();
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding).toBeUndefined();
|
|
});
|
|
|
|
it("uses the session context window when the native rollout omits its model window", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000, contextTokens: 258_400 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(rolloutDir, "rollout-thread-existing.jsonl"),
|
|
`${JSON.stringify({
|
|
payload: {
|
|
type: "token_count",
|
|
info: {
|
|
last_token_usage: {
|
|
total_tokens: 241_198,
|
|
},
|
|
},
|
|
},
|
|
})}\n`,
|
|
);
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: undefined,
|
|
});
|
|
|
|
expect(binding).toBeUndefined();
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding).toBeUndefined();
|
|
});
|
|
|
|
it("clears byte-oversized rollouts before reading their contents", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
const rolloutFile = path.join(rolloutDir, "rollout-thread-existing.jsonl");
|
|
await fs.writeFile(rolloutFile, "x".repeat(2_000));
|
|
const openSpy = vi.spyOn(fs, "open");
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: 1_000,
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding).toBeUndefined();
|
|
expect(openSpy.mock.calls.some(([file]) => String(file) === rolloutFile)).toBe(false);
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding).toBeUndefined();
|
|
});
|
|
|
|
it("clears native rollouts at the configured byte limit", async () => {
|
|
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
const workspaceDir = path.join(tempDir, "workspace");
|
|
const agentDir = path.join(tempDir, "agent");
|
|
await writeExistingBinding(sessionFile, workspaceDir, { dynamicToolsFingerprint: "[]" });
|
|
await writeSessionRecord(sessionFile, { totalTokens: 12_000 });
|
|
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
|
await fs.mkdir(rolloutDir, { recursive: true });
|
|
await fs.writeFile(path.join(rolloutDir, "rollout-thread-existing.jsonl"), "x".repeat(1_000));
|
|
|
|
const binding = await rotateOversizedCodexAppServerStartupBinding({
|
|
binding: await readCodexAppServerBinding(sessionFile),
|
|
sessionFile,
|
|
agentDir,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
compaction: {
|
|
truncateAfterCompaction: true,
|
|
maxActiveTranscriptBytes: 1_000,
|
|
},
|
|
},
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
expect(binding).toBeUndefined();
|
|
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
|
expect(savedBinding).toBeUndefined();
|
|
});
|
|
});
|