Skip to content
Open
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
9 changes: 4 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -4497,10 +4497,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 correctiveMeter = wireMeter ? responsesPreflightMeter(probe, limit) : undefined;
const probeCalibrated = Math.max(
applyEstimateCalibration(probeTextSize, kFactor, kOrigin, currentOrigin) + imageTokens,
Expand Down
17 changes: 17 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,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 {
Expand Down
98 changes: 98 additions & 0 deletions tests/issue6865-probe-arm-guard.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
Loading