Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/codex-compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,21 @@ export function replaceBiliCompactionItems<T>(input: T[]): { items: T[]; replace
return { items, replaced, dropped };
}

// Safety valve: forge ONLY when the transform succeeded, the steady-state
// context is below the 90% threshold (codex's own auto-compact point — above
// it, bili's ACP has failed to keep up and native compaction must backstop),
// and there is at least one active block (a real summary to hand off).
export function codexCompactGate(session: Session, effectiveLimit: number, transformOk: boolean): boolean {
if (!transformOk || effectiveLimit <= 0) return false;
// Safety valve preconditions (#332): evaluable BEFORE the kernel transform so
// the pipeline can dispatch a compaction request (forge vs verbatim
// passthrough) before prepare/preflight touch payload or state. 90% is codex's
// own auto-compact point — above it, native compaction must backstop.
export function codexCompactGatePre(session: Session, effectiveLimit: number): boolean {
if (effectiveLimit <= 0) return false;
if (session.stats.lastInputTokens >= effectiveLimit * 0.9) return false;
return session.state.blocks.some((b) => b.active);
}

// Full safety valve: forge only when the kernel transform also succeeded.
export function codexCompactGate(session: Session, effectiveLimit: number, transformOk: boolean): boolean {
return transformOk && codexCompactGatePre(session, effectiveLimit);
}

// Minimal legal forged reply for the trigger form. Streaming: exactly one
// compaction output item + response.completed (codex's parser,
// codex-api/src/sse/responses.rs, ignores every other event kind and requires
Expand Down
34 changes: 32 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { runCompressLoop, pickAdapter } from "./loop/index.js";
import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js";
import { isFakeCompletion, injectFakeCompletionHint, maxFakeCompletionRetries, fakeBufCap } from "./fake-completion.js";
import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js";
import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js";
import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, codexCompactGatePre, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js";
import { rewriteOpenaiJsonResponse } from "./stream-openai.js";
import { rewriteResponsesJsonResponse } from "./stream-responses.js";
import { observeResponsesTerminalState } from "./stream-terminal.js";
Expand Down Expand Up @@ -1288,6 +1288,32 @@ async function handle(
: responsesCompact
? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session, req, core, reqConfig, log)
: prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, responsesIdentity!, pluginMode, upstreamOrigin, nativeWindow);
// #332: codex's native remote-compaction request (trigger form)
// is dispatched BEFORE prepare/preflight. When it is not
// intercepted, the upstream must receive exactly what codex
// sent: a preflight-compressed/rebuilt payload diverges from
// codex's local history, non-OpenAI backends 400 the
// compaction_trigger item, and folding bili's state as a side
// effect of handling codex's own compaction is wrong.
const isCodexCompactTrigger =
protocol === "responses" &&
!responsesCompact &&
isCodexClient(req.headers) &&
hasCompactionTrigger((parsed as ResponsesRequestBody).input);
if (isCodexCompactTrigger) {
const mode = codexCompactMode();
const gatePre = codexCompactGatePre(session, reqConfig.modelContextLimit);
if (mode === "intercept" && gatePre) prepared = runPrepare();
if (prepared?.codexForge) {
await forward(req, res, opts, prepared.body, prepared, core, reqConfig, log, route, instanceId, affinity);
rememberPluginMessages(sessionId, prepared.processedMessages, prepared.originalMessages, prepared.nudge);
return;
}
const why = mode !== "intercept" ? "BILI_CODEX_COMPACT=pass" : !gatePre ? "gate preconditions not met" : "transform/forge failed";
log("info", `[${session.id}] codex compaction_trigger request not intercepted (${why}) — forwarding verbatim (no preflight, no rebuild, no window clamp)`);
await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log, route, instanceId, affinity);
return;
}
prepared = runPrepare();
if (!countTokens && !responsesCompact) {
const outcome = await preflightCompressIfNeeded(
Expand Down Expand Up @@ -2020,7 +2046,11 @@ function prepareResponsesCompact(
compressInjected: false,
resetAfterSuccess: true,
};
if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input)) {
// #332: gate preconditions BEFORE the transform — when they fail the
// request passes through verbatim without running processTurn (no state
// mutation as a side effect of a compact that will not be intercepted).
if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input)
|| !codexCompactGatePre(session, config.modelContextLimit)) {
return base;
}
// The state commit below is all-or-nothing: every non-forge path restores
Expand Down
100 changes: 100 additions & 0 deletions tests/codex-compact-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ function conversation() {
}
return input;
}
// 14 messages ≈ 15.4k tokens — OVER the 10k window, so preflight WOULD fire
// if this payload went through the normal pipeline (#332 regression input).
function bigConversation() {
const input: { type: string; role: string; content: string }[] = [];
for (let i = 0; i < 14; i++) {
input.push({ type: "message", role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i} of the working session. ` + `WORK_${i}_content_`.repeat(290) });
}
return input;
}

type Harness = {
proxy: http.Server;
Expand Down Expand Up @@ -288,3 +297,94 @@ test("e2e E2 (endpoint form): intercept + healthy ACP → forged JSON {output},
assert.equal(h.bodies.length, afterSetup, "upstream NOT contacted for the intercepted endpoint compact");
});
});

// Issue #332: a compaction_trigger request that is NOT intercepted must reach
// upstream byte-identical — no preflight overflow-compress, no payload
// rebuild, no window clamp, no session-state folding as a side effect. The
// oversized payload (14 msgs ≈ 15.4k > 10k window) is what made the old
// ordering fire preflight before the compact detection ran.

test("e2e #332 (trigger form): pass mode + oversized payload → forwarded byte-identical, no state mutation", async () => {
await withHarness({ mode: "pass", firstTurnTokens: 1000 }, async (h) => {
const afterSetup = await setupCompressedSession(h);
const before = listSessions().find((x) => x.meta.label === SESSION)!;
const stateBefore = JSON.stringify(before.state);

const rawBody = JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...bigConversation(), { type: "compaction_trigger" }] });
const r2 = await fetch(h.url, {
method: "POST",
headers: { "content-type": "application/json", "user-agent": CODEX_UA },
body: rawBody,
});
assert.equal(r2.status, 200, "verbatim passthrough returns the upstream status");
await r2.text();
assert.equal(h.bodies.length, afterSetup + 1, "compact request forwarded to upstream");
assert.equal(h.bodies[h.bodies.length - 1], rawBody, "upstream received the EXACT bytes codex sent (no preflight, no rebuild)");
const after = listSessions().find((x) => x.meta.label === SESSION)!;
assert.equal(JSON.stringify(after.state), stateBefore, "session state untouched (no folding as a side effect)");
});
});

test("e2e #332 (trigger form): intercept + gate preconditions fail (≥90%) + oversized payload → forwarded byte-identical", async () => {
await withHarness({ mode: "intercept", firstTurnTokens: 15000 }, async (h) => {
const afterSetup = await setupCompressedSession(h);
const before = listSessions().find((x) => x.meta.label === SESSION)!;
assert.ok(before.stats.lastInputTokens >= 9000, "high steady-state usage → gate preconditions fail");
const stateBefore = JSON.stringify(before.state);

const rawBody = JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...bigConversation(), { type: "compaction_trigger" }] });
const r2 = await fetch(h.url, {
method: "POST",
headers: { "content-type": "application/json", "user-agent": CODEX_UA },
body: rawBody,
});
assert.equal(r2.status, 200);
await r2.text();
assert.equal(h.bodies.length, afterSetup + 1, "compact request forwarded to upstream");
assert.equal(h.bodies[h.bodies.length - 1], rawBody, "unhealthy ACP → verbatim passthrough (issue log scenario: no more 400 loop on rebuilt payload)");
const after = listSessions().find((x) => x.meta.label === SESSION)!;
assert.equal(JSON.stringify(after.state), stateBefore, "session state untouched");
});
});

test("e2e #332 (trigger form): intercept + healthy ACP + oversized payload → forged, upstream untouched", async () => {
await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => {
const afterSetup = await setupCompressedSession(h);
const s = listSessions().find((x) => x.meta.label === SESSION)!;
assert.ok(s.stats.lastInputTokens < 9000, "low steady-state usage → gate preconditions pass");

const r2 = await fetch(h.url, {
method: "POST",
headers: { "content-type": "application/json", "user-agent": CODEX_UA },
body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...bigConversation(), { type: "compaction_trigger" }] }),
});
assert.equal(r2.status, 200);
assert.equal(r2.headers.get("content-type"), "text/event-stream", "forged SSE");
const text = await r2.text();
assert.ok(text.includes("fc_bili_"), "forged compaction item");
assert.ok(text.includes("resp_bili_"), "forged response id");
assert.equal(h.bodies.length, afterSetup, "upstream NOT contacted (preflight no longer interferes with the forge)");
});
});

test("e2e #332 (endpoint form): intercept + gate preconditions fail → raw body forwarded, no state mutation", async () => {
await withHarness({ mode: "intercept", firstTurnTokens: 15000 }, async (h) => {
const afterSetup = await setupCompressedSession(h);
const before = listSessions().find((x) => x.meta.label === SESSION)!;
assert.ok(before.stats.lastInputTokens >= 9000, "high steady-state usage → gate preconditions fail");
const stateBefore = JSON.stringify(before.state);

const rawBody = JSON.stringify({ model: "gpt-resp", stream: false, session_id: SESSION, instructions: "You are the test coding agent.", input: conversation() });
const r2 = await fetch(h.compactUrl, {
method: "POST",
headers: { "content-type": "application/json", "user-agent": CODEX_UA },
body: rawBody,
});
assert.equal(r2.status, 200);
await r2.text();
assert.equal(h.bodies.length, afterSetup + 1, "endpoint compact forwarded to upstream");
assert.equal(h.bodies[h.bodies.length - 1], rawBody, "upstream received the exact bytes sent");
const after = listSessions().find((x) => x.meta.label === SESSION)!;
assert.equal(JSON.stringify(after.state), stateBefore, "processTurn skipped when the gate cannot pass");
});
});
Loading