Skip to content
Closed
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
10 changes: 5 additions & 5 deletions src/codex-compact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHash, randomUUID } from "node:crypto";
import type { CompressionBlock } from "acp-kernel";
import { statusInputBaseline, type Session } from "./session.js";
import { evidenceInputBaseline, type Session } from "./session.js";
import { codexCompactMode as knobCodexCompactMode } from "./knobs.js";

// #3686: the x-bili-plugin marker name is read DIRECTLY here instead of via
Expand Down Expand Up @@ -232,10 +232,10 @@ export const CODEX_COMPACT_HEALTH_RATIO = 0.9;

export function codexCompactGatePre(session: Session, effectiveLimit: number): boolean {
if (effectiveLimit <= 0) return false;
// Evidence-grade baseline only (#3008/#1492/#1839 discipline): raw
// stats.lastInputTokens can be an uncalibrated estimate-grade value that
// self-disables the valve at phantom pressure while real usage is far below.
if (statusInputBaseline(session) >= effectiveLimit * CODEX_COMPACT_HEALTH_RATIO) return false;
// Evidence-grade baseline only (#3008/#1492/#1839). Status can substitute
// a local estimate after a send without usage. That estimate can cross the
// threshold in either direction while the last measured usage is unchanged.
if (evidenceInputBaseline(session.stats) >= effectiveLimit * CODEX_COMPACT_HEALTH_RATIO) return false;
return session.state.blocks.some((b) => b.active);
}

Expand Down
11 changes: 8 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,12 +957,17 @@ export function usageAnchorCycleStale(stats: Session["stats"]): boolean {
return (stats.localInputEstimate ?? 0) > 0;
}

/** Last measured usage or bounded overflow evidence, before status fallbacks. */
export function evidenceInputBaseline(stats: Session["stats"]): number {
return stats.lastInputTokensSource === "usage" || stats.lastInputTokensSource === "overflow-arm"
? stats.lastInputTokens
: stats.lastUsageGradeTokens ?? 0;
}

export function statusInputBaseline(session: Session): number {
const stats = session.stats;
if (usageAnchorCycleStale(stats)) return stats.localInputEstimate ?? 0;
const baseline = stats.lastInputTokensSource === "usage" || stats.lastInputTokensSource === "overflow-arm"
? stats.lastInputTokens
: stats.lastUsageGradeTokens ?? 0;
const baseline = evidenceInputBaseline(stats);
return staleSmallAnchorFallback(stats, baseline) ?? baseline;
}

Expand Down
27 changes: 27 additions & 0 deletions tests/codex-compact-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SessionStore, _setStoreForTest } from "../src/persist.ts";
import { _setForTest as setRegistryForTest } from "../src/registry.ts";
import { listSessions, _resetSessionsForTest } from "../src/session.ts";
import { buildTriggerForgeBody } from "../src/codex-compact.ts";
import { noteOutboundContextEstimates } from "../src/cache-ledger.ts";

// Issue #321 PR-E2: conditional interception + forgery of codex's native
// compaction requests. When BILI_CODEX_COMPACT=intercept, the client is codex,
Expand Down Expand Up @@ -504,6 +505,32 @@ test("e2e #332 (trigger form): intercept + healthy ACP + oversized payload → f
});
});

test("e2e (trigger form): upper bound from a send without usage cannot veto interception", async () => {
await withHarness({ mode: "intercept", firstTurnTokens: 4_400 }, async (h) => {
const afterSetup = await setupCompressedSession(h);
const session = listSessions().find((s) => s.meta.label === SESSION)!;
assert.equal(session.stats.lastInputTokensSource, "usage");
assert.ok(session.stats.lastInputTokens < 13_500);
noteOutboundContextEstimates(session, {
upperBound: 13_800,
scaledText: 4_600,
imageTokens: 0,
calibrated: true,
});

const response = 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" }] }),
});
const body = await response.text();
assert.equal(response.status, 200);
assert.match(body, /fc_bili_/);
assert.match(body, /response\.completed/);
assert.equal(h.bodies.length, afterSetup, "the oversized trigger never reached upstream");
});
});

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);
Expand Down
29 changes: 29 additions & 0 deletions tests/codex-compact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,35 @@ test("codexCompactGatePre: evidence-grade baseline only (#3008)", () => {
assert.equal(codexCompactGatePre(mockSession(9_500, [block(true)]), limit), true, "no source, no usage anchor → baseline 0 → forge");
});

test("codexCompactGatePre: a high estimate from a send without usage cannot veto interception", () => {
const limit = 475_000;
const lowUsage = mockSession(139_820, [block(true)], { source: "usage", grade: 139_820 });
lowUsage.stats.contextTokensSource = "estimate";
lowUsage.stats.localInputEstimate = 436_702;
assert.equal(codexCompactGatePre(lowUsage, limit), true, "the per-send upper bound cannot veto interception");
});

test("codexCompactGatePre: low estimates cannot erase usage or overflow evidence", () => {
const limit = 475_000;
const threshold = 427_500;
const highUsage = mockSession(threshold, [block(true)], { source: "usage", grade: threshold });
highUsage.stats.contextTokensSource = "estimate";
highUsage.stats.localInputEstimate = 139_820;
assert.equal(codexCompactGatePre(highUsage, limit), false, "a later estimate cannot erase measured pressure");

const overflow = mockSession(threshold, [block(true)], { source: "overflow-arm", grade: 139_820 });
overflow.stats.contextTokensSource = "estimate";
overflow.stats.localInputEstimate = 139_820;
assert.equal(codexCompactGatePre(overflow, limit), false, "bounded overflow evidence takes precedence");
});

test("codexCompactGatePre: the stale-small status fallback cannot replace retained usage", () => {
const retainedUsage = mockSession(500_000, [block(true)], { source: "estimate", grade: 100_000 });
retainedUsage.stats.contextTokensSource = "estimate";
retainedUsage.stats.contextEstimateTokens = 450_000;
assert.equal(codexCompactGatePre(retainedUsage, 475_000), true, "the stale-small status fallback is also an estimate");
});

test("replaceBiliCompactionItems: echo becomes a summary handoff message in place", () => {
const ours = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}x`, encrypted_content: `${CODEX_COMPACT_SENTINEL}summary text` };
const real = { type: "compaction", id: "fc_real", encrypted_content: "opaque" };
Expand Down
Loading