From 414002a042b95e15e396c516caaa51a164656756 Mon Sep 17 00:00:00 2001 From: ReineLan <105932053+ReineLan@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:42:57 +0800 Subject: [PATCH] fix: use measured evidence for Codex compaction gating --- src/codex-compact.ts | 10 +++++----- src/session.ts | 11 ++++++++--- tests/codex-compact-e2e.test.ts | 27 +++++++++++++++++++++++++++ tests/codex-compact.test.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/codex-compact.ts b/src/codex-compact.ts index 9b93fe67e8..4c85dbdcc1 100644 --- a/src/codex-compact.ts +++ b/src/codex-compact.ts @@ -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 @@ -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); } diff --git a/src/session.ts b/src/session.ts index f518aa9ca0..ca5e7cddf3 100644 --- a/src/session.ts +++ b/src/session.ts @@ -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; } diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts index c5762929b0..bb0d17df9b 100644 --- a/tests/codex-compact-e2e.test.ts +++ b/tests/codex-compact-e2e.test.ts @@ -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, @@ -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); diff --git a/tests/codex-compact.test.ts b/tests/codex-compact.test.ts index ddf350218d..63babfc5c1 100644 --- a/tests/codex-compact.test.ts +++ b/tests/codex-compact.test.ts @@ -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" };