From e92da1235bf75800b9ac5c77a9ef12420845681d Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 03:08:44 +0800 Subject: [PATCH 1/2] fix: guard the #5974 probe baseline write against overflow-arm demotion (#6865) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #5974 corrective-pass probe (preflightCompressIfNeeded) measured the fresh rebuild and wrote a local estimate onto the session baseline without an overflow-arm guard. In the caliber-delta case (post-fold local probe slightly above the armed wall, but the payload actually fits) the probe demoted the ACTIVE overflow-arm evidence tag to "estimate" before the post-rebuild anchor write ran — silently dropping the evidence floor that keeps an armed session folding instead of trusting optimistic estimates. The next turn then fit-forwarded on a stale estimate until the upstream 400 re-armed it: the exact fold → forward → 400 livelock #5880 fixed, re-opened on the probe path. Extract the probe's baseline write into applyProbeBaselineAnchor (session.ts) with the same overflow-arm guard as armFailureShrink (relay.ts #2484). Unarmed and usage-grade behavior is unchanged (#1987 anchor, #604 usage→estimate). Regression test pins both directions (armed survives, unarmed anchors). Fixes #6865 --- src/server.ts | 9 +-- src/session.ts | 17 +++++ tests/issue6865-probe-arm-guard.test.ts | 98 +++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 tests/issue6865-probe-arm-guard.test.ts diff --git a/src/server.ts b/src/server.ts index 7255880683..d939eae194 100644 --- a/src/server.ts +++ b/src/server.ts @@ -55,7 +55,7 @@ import { prepareResponses, prepareResponsesCompact } from "./server/prepare-resp import { FORCE_TEXT_PROTOCOL } from "./server/inject.js"; export { isChatGptCodexUpstream, isCodexResponsesLite, resolvePromptCacheKey, shouldInjectPromptCacheKey } from "./server/prepare-responses.js"; import { biliToolsDeclaredOnWire, countBiliToolUses, evaluateSelfHealRound, nudgeSuppressed, pluginLaneDegraded, pluginLaneRestore } from "./session-self-heal.js"; -import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, type NudgeStamps, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback } from "./session.js"; +import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, type NudgeStamps, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback, applyProbeBaselineAnchor } from "./session.js"; import type { SeparatorManifest } from "./server/responses-ordering.js"; import { detectStaleInstall, isVersionNewer } from "./update.js"; import { decideEaddrinuse, eaddrinuseExitCode, isPortBusyError, postBindCanLadder, UPGRADE_REPLACEMENT_ENV, type FailFastReason } from "./listen-policy.js"; @@ -4297,10 +4297,9 @@ export async function preflightCompressIfNeeded( pendingRebuild = probe; const probeMsgs = probe.processedMessages.length > 0 ? probe.processedMessages : probe.originalMessages; const probeTextSize = estimateCoreMessages(probeMsgs) + overheadEstimate; - if (probeTextSize > session.stats.lastInputTokens) { - session.stats.lastInputTokens = probeTextSize; - session.stats.lastInputTokensSource = "estimate"; - } + // #6865: the probe's baseline write is guarded by applyProbeBaselineAnchor + // (session.ts) so it never demotes an active overflow-arm tag. + applyProbeBaselineAnchor(session, probeTextSize); const probeCalibrated = applyEstimateCalibration(probeTextSize, kFactor, kOrigin, currentOrigin) + imageTokens; const divergence = Math.round(probeCalibrated - result.payloadEstimate); if (probeCalibrated >= limit && divergence >= Math.max(10_000, Math.round(limit * 0.02))) { diff --git a/src/session.ts b/src/session.ts index f518aa9ca0..d548ca3bb1 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1192,6 +1192,23 @@ export function setPostRebuildAnchor(session: Session, tokens: number): void { markDirty(session); } +// #5974 probe baseline write, hardened by the #5880 doctrine: the #5974 +// corrective-pass probe measures the fresh rebuild and writes a local estimate +// onto the session baseline. An upstream-proven overflow-arm tag must NEVER be +// demoted to estimate-grade by that probe — demoting drops the evidence floor +// that keeps an armed session folding instead of trusting optimistic estimates, +// re-opening the #5880 fold → forward → 400 livelock on the probe path (same +// doctrine as armFailureShrink, relay.ts #2484). Unarmed and usage-grade +// behavior is unchanged: a larger local measurement still anchors the baseline +// (#1987), and a newer larger estimate still retires an older usage sample. +export function applyProbeBaselineAnchor(session: Session, probeTextSize: number): void { + if (session.stats.lastInputTokensSource === "overflow-arm") return; + if (probeTextSize > session.stats.lastInputTokens) { + session.stats.lastInputTokens = probeTextSize; + session.stats.lastInputTokensSource = "estimate"; + } +} + /** Metadata is persisted user-editable JSON — re-validate on read; a corrupt * stamp degrades to "no anchor" (legacy sizing) instead of poisoning the meter. */ export function postRebuildAnchorTokens(session: Session | undefined): number { diff --git a/tests/issue6865-probe-arm-guard.test.ts b/tests/issue6865-probe-arm-guard.test.ts new file mode 100644 index 0000000000..5aab232176 --- /dev/null +++ b/tests/issue6865-probe-arm-guard.test.ts @@ -0,0 +1,98 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +// #6865: the #5974 corrective-pass probe (server.ts preflightCompressIfNeeded) +// measured the fresh rebuild and wrote a local estimate onto the session +// baseline WITHOUT an overflow-arm guard. In the caliber-delta case (post-fold +// local probe slightly above the armed wall, but the payload actually fits) the +// probe demoted the ACTIVE overflow-arm evidence tag to "estimate" BEFORE the +// post-rebuild anchor write ran — silently disarming the evidence floor that +// keeps an armed session folding instead of trusting optimistic estimates. The +// next turn then fit-forwarded on a stale optimistic estimate until the upstream +// 400 re-armed it: the exact fold → forward → 400 livelock #5880 fixed, re-opened +// on the probe path. +// +// The fix extracts the probe's baseline write into applyProbeBaselineAnchor +// (session.ts) with the same overflow-arm guard as armFailureShrink (relay.ts +// #2484). These unit pins lock BOTH directions (#3617 gate discipline): +// armed → the overflow-arm tag AND its value survive the probe write; +// unarmed → the #1987 anchor behavior is unchanged (larger probe raises the +// floor, smaller probe never lowers it); +// usage → a newer larger local estimate still retires an older usage sample +// (#604 behavior preserved). + +import { createInitialState } from "acp-kernel"; +import { applyProbeBaselineAnchor } from "../src/session.ts"; +import type { Session } from "../src/session.ts"; + +function makeSession(lastInputTokens: number, source: Session["stats"]["lastInputTokensSource"]): Session { + const now = Date.now(); + return { + id: "s6865-unit", + meta: {}, + metadata: {}, + state: createInitialState(), + stats: { + requests: 1, + tokensSaved: 0, + inputTokens: 0, + cachedTokens: 0, + outputTokens: 0, + cacheSamples: 0, + lastInputTokens, + lastInputTokensSource: source, + compressCreditTokens: 0, + contextTokens: lastInputTokens, + retrieveCalls: 0, + retrieveHits: 0, + retrieveMisses: 0, + storedBytes: 0, + storeBytesSaved: 0, + rangeRestores: 0, + }, + pendingRetrievals: [], + createdAt: now, + lastSeen: now, + blockContents: new Map(), + inFlight: 0, + persisted: false, + } as unknown as Session; +} + +test("#6865 armed: an active overflow-arm tag survives the probe baseline write", () => { + // Incident shape: the arm holds the netted measured value; the #5974 probe's + // fresh rebuilt-view estimate measures slightly HIGHER (caliber delta). The + // pre-fix inequality-based write demoted the tag to "estimate" here. + const s = makeSession(783_000, "overflow-arm"); + applyProbeBaselineAnchor(s, 783_353); + assert.equal(s.stats.lastInputTokensSource, "overflow-arm", "armed tag must not be demoted by the probe"); + assert.equal(s.stats.lastInputTokens, 783_000, "arm value must not be overwritten by the probe estimate"); +}); + +test("#6865 unarmed: estimate-grade sessions keep the #1987 anchor behavior (both directions)", () => { + const up = makeSession(100_000, "estimate"); + applyProbeBaselineAnchor(up, 120_000); + assert.equal(up.stats.lastInputTokens, 120_000, "larger probe must still anchor the baseline (#1987)"); + assert.equal(up.stats.lastInputTokensSource, "estimate"); + + const down = makeSession(100_000, "estimate"); + applyProbeBaselineAnchor(down, 80_000); + assert.equal(down.stats.lastInputTokens, 100_000, "smaller probe must not lower the floor"); + assert.equal(down.stats.lastInputTokensSource, "estimate"); +}); + +test("#6865 usage: a newer larger probe estimate still retires an older usage sample (#604)", () => { + const u = makeSession(100_000, "usage"); + applyProbeBaselineAnchor(u, 120_000); + assert.equal(u.stats.lastInputTokens, 120_000); + assert.equal(u.stats.lastInputTokensSource, "estimate", "usage→estimate overwrite stays #604 behavior"); +}); + +test("#6865 unarmed-fresh: a session with no source tag anchors on a larger probe", () => { + const f = makeSession(0, undefined); + applyProbeBaselineAnchor(f, 55_000); + assert.equal(f.stats.lastInputTokens, 55_000, "a fresh session must anchor on the probe estimate"); + assert.equal(f.stats.lastInputTokensSource, "estimate"); +}); From cb03a71f99bbf5e47b7ce2af76c983c950fbd04e Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 03:35:28 +0800 Subject: [PATCH 2/2] ci: re-trigger test lane (previous run cancelled by labeled-event skip run)