From 697be9538a8574d343e64930cf7adf201954ffa1 Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 22:07:11 +0800 Subject: [PATCH 1/4] fix: re-measure turn sizing after a native-compaction rebase so the nudge meter isn't stranded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A native-compaction rebase (#2432/#2373) calls resetSessionCompression, which wipes every sizing anchor effectiveTokenCount reads, and the pre-rebase value describes the pre-compaction payload. Feeding either into the immediate decideNudge strands firstSightMassReady: a stale-low usage gates off the mass trigger while the real ready mass sits undrained until unrelated growth crosses the band — the #3712 permanent-degradation deadlock (blocked: T1 (cadence) forever). Re-measure the current compacted view with the tokenizer-consistent fit-gate estimator (preflight's defaultCountTokens basis, not the char-count upper bound that would fire a phantom EMERGENCY on code/JSON payloads, #1839) across all four prepare lanes, stamp how the rebased turn priced its context onto the native-compaction boundary, and make the dsh compaction-disable overlay's silent-miss path visible in bili.log. Closes #3712 --- src/agent/dsh-compaction-disable.ts | 30 ++- src/server.ts | 29 +++ src/server/prepare-anthropic.ts | 12 +- src/server/prepare-google.ts | 15 +- src/server/prepare-openai.ts | 12 +- src/server/prepare-responses.ts | 19 +- .../issue3712-post-rebase-nudge-meter.test.ts | 190 ++++++++++++++++++ 7 files changed, 290 insertions(+), 17 deletions(-) create mode 100644 tests/issue3712-post-rebase-nudge-meter.test.ts diff --git a/src/agent/dsh-compaction-disable.ts b/src/agent/dsh-compaction-disable.ts index 4970e3d159..ab86d5048d 100644 --- a/src/agent/dsh-compaction-disable.ts +++ b/src/agent/dsh-compaction-disable.ts @@ -33,6 +33,8 @@ // bundle-patch gate rows may target ids a profile does not ship — their miss // is otherwise visible only in the host loader log). +import { logHostEvent } from "./host-log.js"; + /** The two dsh packages whose backends must not instantiate while bili owns the lane. */ const DSH_COMPACTION_BASIC_PACKAGE = "@deepseek-ai/dsh-compaction-basic"; const DSH_COMMAND_COMPACT_PACKAGE = "@deepseek-ai/dsh-command-compact"; @@ -188,18 +190,34 @@ export function registerCompactionDisable(ctx: CompactionDisableHost): void { // #3662: honor the process verdict — without an active declaration // from bili-native every row passes through stock. if (!isDshCompactionActive()) return resolved; + const rawName = this?.entry?.options?.name; + const entryName = typeof rawName === "string" ? rawName : "unnamed-entry"; + // #3712: this middleware is global and fires for EVERY package's config + // resolution — anything other than the factory preset is an expected + // pass-through (logging it would spam), so only the preset's outcomes + // below are observable. The genuine silent-miss there: we ARE the preset + // (gate satisfied) but its resolved shape changed so the overlay cannot + // reach the nested compaction rows — dsh native compaction would run + // while bili owns compression, and pre-fix that was indistinguishable + // from success using only bili.log. + const isFactoryPreset = entryName === DSH_AGENT_PRESET_PACKAGE; + const config = (resolved ?? {}) as { plugins?: unknown }; + if (!Array.isArray(config.plugins)) { + if (isFactoryPreset) logHostEvent("warn", "[dsh-client]", `compaction-disable overlay MISSED on ${DSH_AGENT_PRESET_PACKAGE}: resolved config has no plugins array — nested compaction rows were NOT stood down (#3354)`); + return resolved; + } // #4147: match STRUCTURALLY, not by entry name — custom presets run // their compaction inside nested rows under their own entry, and a // name filter leaves exactly those armed. Rewrite only when the // tree actually nests a compaction row, so unrelated entries keep // their resolved config object identity. - const config = (resolved ?? {}) as { plugins?: unknown }; - if (!Array.isArray(config.plugins)) return resolved; const compactionRows = countCompactionRows(config.plugins); - if (compactionRows === 0) return resolved; - const rawName = this?.entry?.options?.name; - const entryName = typeof rawName === "string" ? rawName : "unnamed-entry"; - noteGateCoverage(entryName.slice(0, 120), compactionRows, entryName === DSH_AGENT_PRESET_PACKAGE); + if (compactionRows === 0) { + if (isFactoryPreset) logHostEvent("warn", "[dsh-client]", `compaction-disable overlay MISSED on ${DSH_AGENT_PRESET_PACKAGE}: resolved plugins tree has no compaction rows — dsh native compaction NOT stood down (#3354)`); + return resolved; + } + noteGateCoverage(entryName.slice(0, 120), compactionRows, isFactoryPreset); + if (isFactoryPreset) logHostEvent("info", "[dsh-client]", `compaction-disable overlay applied to ${DSH_AGENT_PRESET_PACKAGE}: nested compaction rows stood down (${compactionRows} rows, #3354)`); return { ...config, plugins: disableBasicCompaction(config.plugins) }; }, { global: true }); } diff --git a/src/server.ts b/src/server.ts index 2dd74daee8..43a6e2699a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2563,6 +2563,35 @@ export function usageGradeInputBaseline(session: Session): number { return session.stats.lastInputTokensSource === "usage" ? session.stats.lastInputTokens : 0; } +// #3712: a native-compaction rebase (#2432/#2373) calls resetSessionCompression, +// which wipes every sizing anchor effectiveTokenCount reads (lastInputTokens → 0, +// lastUsageGradeTokens deleted, localInputEstimate cleared) — and the value +// computed BEFORE the rebase describes the PRE-compaction payload. Feeding either +// into the immediate decideNudge strands the meter: a stale-low usage gates off +// firstSightMassReady (usage >= minContextLimitPct) while the real ready mass sits +// undrained until unrelated growth crosses the band (the #3712 permanent-degradation +// deadlock, blocked: T1 (cadence) forever); a naive post-rebase read is just 0. +// Re-measure the CURRENT compacted view with the tokenizer-consistent fit-gate +// estimator preflight uses (defaultCountTokens, same basis as the kernel's own +// count) — truthful, so a genuinely over-window payload still hits its band while +// code/JSON payloads do NOT fire a phantom EMERGENCY the way the char-count upper +// bound would (#1839 ghost class). +export function postRebaseTokenCount(msgs: CoreMessage[], inboundImageTokens = 0): { tokens: number; source: "estimate" } { + return { tokens: estimateCoreMessages(msgs) + inboundImageTokens, source: "estimate" }; +} + +// #3712: stamp how the rebased turn priced its context onto the native-compaction +// boundary itself. stats.contextTokens is refreshed by the turn's usage report into +// the freshest upstream figure, so this event-scoped record is the only place that +// keeps the compaction landing's OWN pricing visible afterwards (panel/status) — the +// stale pre-compaction sample that stranded the meter is exactly what must NOT be the +// surviving number. +export function noteRebasePricing(session: Session, priced: { tokens: number; source: "usage" | "estimate" }): void { + const boundary = session.metadata.nativeCompactionBoundary; + if (!boundary || typeof boundary !== "object") return; + (boundary as Record).priced = priced; +} + // #1403: top-level prompt_cache_key is NOT part of the Anthropic Messages API. // It is the omp plugin's session id stamped for the proxy's identity chain // (#268); the fully-processed path strips it (prepareAnthropic), but every diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index fa57f1e68e..e21caf9d61 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -7,7 +7,7 @@ import { captureThreadsTurn, estimateThreadsTokens, isThreadsBody, threadsReforg import type { CompressReasoningConfig } from "../reasoning-drop.js"; import type { ProxyOptions } from "../config.js"; import { pluginAgentHeader } from "../plugin.js"; -import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js"; +import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, noteRebasePricing, planReasoningDrop, postRebaseTokenCount, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; @@ -283,7 +283,7 @@ export async function prepareAnthropic( // zero-baseline forks replay their FULL raw history with no measurement, // so feeding 0 blinds the nudge (usage 0%, growth ref 0) and no // compression trigger fires until overflow. See effectiveTokenCount. - const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, inboundImageTokens); + let { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, inboundImageTokens); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); // Absorb markers are injected by the kernel's processTurn from // config.absorb. With no channel to call the tool (injection off), @@ -336,6 +336,14 @@ export async function prepareAnthropic( } } } + if (dshRebased) { + // #3712: the rebase just wiped the sizing anchors tokenCount was derived + // from (and tokenCount itself described the pre-compaction payload). + // Re-measure the current compacted view so this turn's nudge decision + // prices reality instead of a stale-low value that strands firstSightMassReady. + ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(msgs, inboundImageTokens)); + noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + } // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 0fa7957240..246c4f7ac2 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -5,7 +5,7 @@ import { coreToGoogle, googleToCore, normalizeGoogleUserPartOrder, type BiliMess import type { ProxyOptions } from "../config.js"; import { pluginAgentHeader } from "../plugin.js"; import { currentCalibrationFactor } from "../util.js"; -import { diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, type Prepared } from "../server.js"; +import { diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, noteRebasePricing, postRebaseTokenCount, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; @@ -143,7 +143,7 @@ export async function prepareGoogle( // #1001: pre-turn snapshot — processTurn below assigns fresh refs to every // previously-unknown id, which would make rewrite detection read 1.0. const knownRefsBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); - const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, imageReserveFor(session, "google", parsed, opts, upstreamOrigin)); + let { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, imageReserveFor(session, "google", parsed, opts, upstreamOrigin)); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); @@ -163,6 +163,7 @@ export async function prepareGoogle( // checkpoint framing in resent history AND decimated fold coverage; // detection runs BEFORE reconcileFoldCoverage/processTurn. No known // dsh+google traffic yet — this closes the same hole on the fourth wire. + let googleRebased = false; if (!isTitleGen && laneDecisions.profile.compactsClientSide && session.state.blocks.some((b) => b.active)) { const coveredBeforeDshCompact = coveredRealHistoryIds(session.state.blocks); // #2627: same real-history caliber const dshGap = foldCoverage(coveredBeforeDshCompact, msgs.map((m) => m.id)); @@ -175,10 +176,18 @@ export async function prepareGoogle( recordConflict(session, "native-compaction", `dsh native compaction: ${missing}/${dshGap.expected} covered id(s) replaced by the compacted history; ACP state rebased (#2432)`); log(authored ? "info" : "warn", `[${sessionId}] dsh ${authored ? "AUTHORED checkpoint landed" : "native compaction detected"} (${dshGap.matched}/${dshGap.expected} covered id(s) retained, checkpoint framing in resent history) — ${authored ? "rebasing ACP state onto the authored bili-snapshot checkpoint (#3733)" : "rebasing ACP state onto the compacted history (#2432)"}`); markNativeCompactionBoundary(session); - reconcileNativeCompactionBoundary(session); + googleRebased = reconcileNativeCompactionBoundary(session); } } } + if (googleRebased) { + // #3712: the rebase just wiped the sizing anchors tokenCount was derived + // from (and tokenCount itself described the pre-compaction payload). + // Re-measure the current compacted view so this turn's nudge decision + // prices reality instead of a stale-low value that strands firstSightMassReady. + ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(msgs, imageReserveFor(session, "google", parsed, opts, upstreamOrigin))); + noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + } // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 0767039e0b..e060e1212e 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -7,7 +7,7 @@ import type { ProxyOptions } from "../config.js"; import { currentCalibrationFactor, hardenOpenaiAssistantContent, systemToUser } from "../util.js"; import { conversationHeaderSource, resolveRelayAffinityPck } from "../session-id.js"; import { pluginAgentHeader } from "../plugin.js"; -import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, warnDroppedOpenaiParts, warnReasoningPairs, type Prepared } from "../server.js"; +import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, noteRebasePricing, planReasoningDrop, postRebaseTokenCount, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, warnDroppedOpenaiParts, warnReasoningPairs, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; @@ -136,7 +136,7 @@ export async function prepareOpenai( // tokenCount = upstream's real input_tokens from the previous turn // tokenCount = upstream's real input_tokens from the previous turn // (see anthropic branch comment + its #553-follow-up exception). - const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, imageReserveFor(session, "openai", parsed, opts, billingUpstream ?? upstreamOrigin)); + let { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, imageReserveFor(session, "openai", parsed, opts, billingUpstream ?? upstreamOrigin)); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); // Absorb markers ride in the kernel's processTurn output (gated by @@ -187,6 +187,14 @@ export async function prepareOpenai( } } } + if (dshRebased) { + // #3712: the rebase just wiped the sizing anchors tokenCount was derived + // from (and tokenCount itself described the pre-compaction payload). + // Re-measure the current compacted view so this turn's nudge decision + // prices reality instead of a stale-low value that strands firstSightMassReady. + ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(msgs, imageReserveFor(session, "openai", parsed, opts, billingUpstream ?? upstreamOrigin))); + noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + } // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). The verdict diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index b69820843e..e42fb2edce 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -6,7 +6,7 @@ import type { CompressReasoningConfig } from "../reasoning-drop.js"; import { resolveAbsorbSettings } from "../compress-settings.js"; import { resolveCompressProtocol, type CompressSettings, type ProxyOptions } from "../config.js"; import { currentCalibrationFactor, strippedResponseIdWarning } from "../util.js"; -import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, repairResponsesAssistantOrdering, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, warnResponsesReasoningPairs, type Prepared } from "../server.js"; +import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, noteRebasePricing, planReasoningDrop, postRebaseTokenCount, repairResponsesAssistantOrdering, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, warnResponsesReasoningPairs, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { mergeAdjacentConfigurationUpdates, patchResponsesInputWithToolImages as patchResponsesInput, responsesToCoreWithToolImages as responsesToCore } from "../responses-tool-output.js"; @@ -70,7 +70,8 @@ export async function prepareResponses( // #3222: #651's reasoning-drop plan, computed on the INBOUND history before // core.processTurn — see planReasoningDrop for why the post-turn view is too late. let reasoningPlan = new Set(); - if (reconcileNativeCompactionBoundary(session)) { + const earlyRebased = reconcileNativeCompactionBoundary(session); + if (earlyRebased) { log("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`); } @@ -226,7 +227,13 @@ export async function prepareResponses( if (opts.debug) { log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); } - const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, imageReserveFor(session, "responses", parsed, opts, billingUpstream ?? upstreamOrigin)); + const imageReserve = imageReserveFor(session, "responses", parsed, opts, billingUpstream ?? upstreamOrigin); + // #3712: an early native-compaction rebase (a boundary marked by a previous + // request's forged-compact handoff) already wiped the sizing anchors this + // request's tokenCount would otherwise derive from — re-measure the current + // view instead of feeding a stale/zero value into decideNudge. + let { tokens: tokenCount, source: tokenCountSource } = earlyRebased ? postRebaseTokenCount(msgs, imageReserve) : effectiveTokenCount(session, renderMsgs, imageReserve); + if (earlyRebased) noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); // Absorb markers ride in the kernel's processTurn output (gated by // config.absorb). The marker/text protocol has no native tool channel, // so strip absorb from the loop config there (both modes). @@ -279,7 +286,11 @@ export async function prepareResponses( log("warn", `[${sessionId}] codex local auto-compaction detected (${localGap.matched}/${localGap.expected} covered ids retained) — rebasing ACP state onto the compacted history (#2372)`); } markNativeCompactionBoundary(session); - reconcileNativeCompactionBoundary(session); + if (reconcileNativeCompactionBoundary(session)) { + // #3712: same post-rebase re-measure as the early site above. + ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(msgs, imageReserve)); + noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + } } } } diff --git a/tests/issue3712-post-rebase-nudge-meter.test.ts b/tests/issue3712-post-rebase-nudge-meter.test.ts new file mode 100644 index 0000000000..f4082de51b --- /dev/null +++ b/tests/issue3712-post-rebase-nudge-meter.test.ts @@ -0,0 +1,190 @@ +// #3712: a native-compaction rebase (dsh desktop's checkpoint landing outside +// bili, #2432/#2373) calls resetSessionCompression, which wipes every sizing +// anchor effectiveTokenCount reads (lastInputTokens → 0, lastUsageGradeTokens +// deleted, localInputEstimate cleared) — and the tokenCount computed BEFORE the +// rebase describes the PRE-compaction payload. Feeding either into the immediate +// core.processTurn strands the nudge meter: a stale-low usage gates off +// firstSightMassReady (usage >= minContextLimitPct) while the real ready mass sits +// undrained until unrelated growth crosses the band — the permanent degradation +// reported in #3712 (out/in 62.5% vs 16.8%, blocked: T1 (cadence) forever). +// +// The fix re-measures the CURRENT compacted view after the rebase. This pins that +// the per-turn sizing input handed to core.processTurn → decideNudge (recorded on +// session.stats.contextTokens / …Source) is a FRESH measure of the current view +// (source "estimate") instead of the stale usage-grade number from before the +// compaction (source "usage"). DSH lanes themselves receive no nudge (#3177 +// host-scope tools), so the input is the correct place to observe the unblocking. +// The seed/fold/replay fixture mirrors issue2432-dsh-local-compaction.test.ts. + +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST = "0"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { getSession, _resetSessionsForTest } from "../src/session.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { DSH_CHECKPOINT_OPEN_TAG, DSH_CHECKPOINT_PREAMBLE_PREFIX } from "../src/server/dsh-compaction-guard.ts"; + +const MODEL = "deepseek-flash"; + +type Rig = { proxyPort: number; upstreamPort: number; bodies: string[]; close: () => Promise }; + +async function startRig(): Promise { + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + bodies.push(raw); + // Report prompt tokens proportional to the payload so the session + // accumulates a real usage-grade baseline between requests. + const promptTokens = Math.max(1, Math.round(raw.length / 4)); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "chatcmpl-1", + object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: promptTokens, completion_tokens: 3, total_tokens: promptTokens + 3 }, + })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetSessionsForTest(); + _resetPluginStateForTest(); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: { context: 30_000 } } } }, + modelContextLimit: 30_000, + kernelConfig: defaultConfig(30_000), + compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return { + proxyPort: (proxy.address() as { port: number }).port, + upstreamPort, + bodies, + close: async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + }, + }; +} + +const chatUrl = (rig: Rig) => `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; + +function dshHeaders(conv: string): Record { + return { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": conv }; +} + +// 14 messages sized like the codex #2372 fixture: the kernel protects the +// last 5 messages, so folding m00001–m00009 covers 9 ids (> the 8-id floor). +function seedInput() { + return Array.from({ length: 14 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: `Message ${i} of the folded conversation. ` + `FILLER_${i}_content_`.repeat(230), + })); +} + +function checkpointMessage() { + return { + role: "user", + content: `${DSH_CHECKPOINT_PREAMBLE_PREFIX} of the conversation so far, as context for continuing. The work established the harness, drove one fold, and then dsh compacted natively. Continue the task directly from the messages that follow, without acknowledging this checkpoint.\n\n${DSH_CHECKPOINT_OPEN_TAG}\n## Primary Request and Intent\n- drive a fold, then replay the compacted view`, + }; +} + +const FOLD_SUMMARY = "MAIN-SUMMARY-SETUP-CONTEXT-FOLDED-BY-COMPRESSION-LONG-ENOUGH-FOR-KERNEL-MIN-LENGTH-CHECK"; + +async function seedAndFold(rig: Rig, conv: string): Promise { + const r1 = await fetch(chatUrl(rig), { + method: "POST", + headers: dshHeaders(conv), + body: JSON.stringify({ model: MODEL, messages: seedInput() }), + }); + assert.equal(r1.status, 200, "seed request succeeds"); + await r1.text(); + const s = getSession(conv); + assert.ok(s, "seed session exists under the conversation key"); + assert.ok(Object.keys(s!.state.messageRefs.byRaw).length >= 14, "refs assigned to the seeded history"); + const r2 = await fetch(`http://127.0.0.1:${rig.proxyPort}/__bili/plugin/tool`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ conversationId: conv, tool: "compress", args: { content: [{ startId: "m00001", endId: "m00009", summary: FOLD_SUMMARY }] } }), + }); + const j = (await r2.json()) as { ok: boolean; result?: string }; + assert.ok(j.ok, `fold accepted (${r2.status})`); + assert.ok(!j.result?.includes("[Compression FAILED"), `fold succeeded: ${j.result?.slice(0, 200)}`); + const covered = new Set((s!.state.blocks ?? []).flatMap((b) => (b.active ? b.effectiveMessageIds : []))); + assert.ok(covered.size >= 8, `fold covers >=8 ids (got ${covered.size})`); +} + +test("#3712: a dsh native-compaction rebase re-measures the turn's sizing input off the CURRENT compacted view, not the stale pre-compaction measurement", async () => { + const rig = await startRig(); + const conv = "dshc-3712-meter-b"; + try { + await seedAndFold(rig, conv); + const s0 = getSession(conv)!; + // Precondition: the seed left a usage-grade input measurement behind — the + // STALE value a pre-fix rebase would reuse for the post-rebase decision. + const staleInput = s0.stats.lastInputTokens; + assert.ok(staleInput > 0, `precondition: the seed produced a usage-grade input measurement (${staleInput})`); + + const r = await fetch(chatUrl(rig), { + method: "POST", + headers: dshHeaders(conv), + body: JSON.stringify({ model: MODEL, messages: [checkpointMessage(), seedInput()[12], seedInput()[13]] }), + }); + assert.equal(r.status, 200, "compacted replay request succeeds"); + await r.text(); + + const s = getSession(conv)!; + const boundary = s.metadata.nativeCompactionBoundary as { pendingRebase?: boolean; priced?: { tokens: number; source: string } } | undefined; + assert.ok(boundary, "native-compaction boundary recorded"); + assert.equal(boundary?.pendingRebase, false, "rebase consumed within the same request"); + assert.equal((s.state.blocks ?? []).length, 0, "block list reset by the rebase"); + // THE FIX: the rebased turn must price its context off a FRESH measure of the + // current compacted view (source "estimate"), not the stale usage-grade baseline + // from before the compaction (source "usage") — which is what gated off + // firstSightMassReady and stranded the meter in #3712. The pricing is stamped on + // the boundary itself because stats.contextTokens is refreshed to the freshest + // upstream figure by this same turn's usage report and would otherwise lose it. + assert.ok(boundary?.priced, "the rebased turn's pricing was recorded on the boundary"); + assert.equal(boundary?.priced?.source, "estimate", "post-rebase sizing input is a fresh measure, not the stale usage-grade baseline"); + assert.ok((boundary?.priced?.tokens ?? 0) > 0, `post-rebase sizing input measured a real current-view size (${boundary?.priced?.tokens})`); + } finally { + await rig.close(); + } +}); From 2ea1b330f367d7fbe47152e7e2cbbb3883c2d614 Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 23:25:53 +0800 Subject: [PATCH 2/4] fix: price the rebased turn off the full outbound view and log the post-rebase pricing (#3712) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review of the #3712 re-measure found the messages-only estimate underprices the rebased turn by exactly the mass the usage-grade baseline it replaces also charged: tool schemas + system frame (incident payload: 97 tool schemas ~23.6K tok + largest system frame ~8.9K tok vs billed 483,998). Switch postRebaseTokenCount to preflight's estimateRawBodyTokens (raw-body string-leaf walk, defaultCountTokens basis, binary image fields excluded — inboundImageTokens handling unchanged), so the meter keeps the usage-grade caliber across the rebase instead of dropping that delta and sitting near-gate payloads below minContextLimitPct again. Also print the rebased turn's pricing at every rebase site ('post-rebase pricing: N tok (estimate)'): priced was previously visible only in the session store, so the estimator gap could not be checked on a live session from bili.log alone (#3712 observability ask). Zero behaviour change beyond the sizing input itself. The regression test now pins the caliber: the replay request carries a 20K-char tool schema and the recorded price must cover it — a messages-only count fails the floor. --- src/server.ts | 13 +++++++++++-- src/server/prepare-anthropic.ts | 3 ++- src/server/prepare-google.ts | 3 ++- src/server/prepare-openai.ts | 3 ++- src/server/prepare-responses.ts | 10 +++++++--- tests/issue3712-post-rebase-nudge-meter.test.ts | 15 ++++++++++++++- 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/server.ts b/src/server.ts index 43a6e2699a..427c06d43f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2576,8 +2576,17 @@ export function usageGradeInputBaseline(session: Session): number { // count) — truthful, so a genuinely over-window payload still hits its band while // code/JSON payloads do NOT fire a phantom EMERGENCY the way the char-count upper // bound would (#1839 ghost class). -export function postRebaseTokenCount(msgs: CoreMessage[], inboundImageTokens = 0): { tokens: number; source: "estimate" } { - return { tokens: estimateCoreMessages(msgs) + inboundImageTokens, source: "estimate" }; +// The measure is taken over the RAW outbound body, not just the message array: +// the usage-grade baseline this replaces (upstream prompt_tokens) prices the FULL +// request — tool schemas and system frame included. A messages-only count drops +// exactly that delta (measured on the #3712 incident payload: 97 tool schemas ≈ +// 23.6K tok + largest system frame ≈ 8.9K tok), re-introducing an underprice that +// can sit a near-gate payload below minContextLimitPct. estimateRawBodyTokens walks +// every string leaf of the parsed body through defaultCountTokens (binary image +// fields excluded — inboundImageTokens charges those separately); its residual +// overcount is structural strings only (ids/roles), a conservative bias of ~1%. +export function postRebaseTokenCount(parsed: unknown, inboundImageTokens = 0): { tokens: number; source: "estimate" } { + return { tokens: estimateRawBodyTokens(parsed) + inboundImageTokens, source: "estimate" }; } // #3712: stamp how the rebased turn priced its context onto the native-compaction diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index e21caf9d61..2604f3a400 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -341,8 +341,9 @@ export async function prepareAnthropic( // from (and tokenCount itself described the pre-compaction payload). // Re-measure the current compacted view so this turn's nudge decision // prices reality instead of a stale-low value that strands firstSightMassReady. - ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(msgs, inboundImageTokens)); + ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(parsed, inboundImageTokens)); noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + log("info", `[${sessionId}] post-rebase pricing: ${tokenCount} tok (${tokenCountSource}) — nudge meter re-based off the current compacted view (#3712)`); } // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 246c4f7ac2..e3411bcd94 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -185,8 +185,9 @@ export async function prepareGoogle( // from (and tokenCount itself described the pre-compaction payload). // Re-measure the current compacted view so this turn's nudge decision // prices reality instead of a stale-low value that strands firstSightMassReady. - ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(msgs, imageReserveFor(session, "google", parsed, opts, upstreamOrigin))); + ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(parsed, imageReserveFor(session, "google", parsed, opts, upstreamOrigin))); noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + log("info", `[${sessionId}] post-rebase pricing: ${tokenCount} tok (${tokenCountSource}) — nudge meter re-based off the current compacted view (#3712)`); } // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index e060e1212e..d82b4b6624 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -192,8 +192,9 @@ export async function prepareOpenai( // from (and tokenCount itself described the pre-compaction payload). // Re-measure the current compacted view so this turn's nudge decision // prices reality instead of a stale-low value that strands firstSightMassReady. - ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(msgs, imageReserveFor(session, "openai", parsed, opts, billingUpstream ?? upstreamOrigin))); + ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(parsed, imageReserveFor(session, "openai", parsed, opts, billingUpstream ?? upstreamOrigin))); noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + log("info", `[${sessionId}] post-rebase pricing: ${tokenCount} tok (${tokenCountSource}) — nudge meter re-based off the current compacted view (#3712)`); } // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index e42fb2edce..7875439975 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -232,8 +232,11 @@ export async function prepareResponses( // request's forged-compact handoff) already wiped the sizing anchors this // request's tokenCount would otherwise derive from — re-measure the current // view instead of feeding a stale/zero value into decideNudge. - let { tokens: tokenCount, source: tokenCountSource } = earlyRebased ? postRebaseTokenCount(msgs, imageReserve) : effectiveTokenCount(session, renderMsgs, imageReserve); - if (earlyRebased) noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + let { tokens: tokenCount, source: tokenCountSource } = earlyRebased ? postRebaseTokenCount(parsed, imageReserve) : effectiveTokenCount(session, renderMsgs, imageReserve); + if (earlyRebased) { + noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + log("info", `[${sessionId}] post-rebase pricing: ${tokenCount} tok (${tokenCountSource}) — nudge meter re-based off the current compacted view (#3712)`); + } // Absorb markers ride in the kernel's processTurn output (gated by // config.absorb). The marker/text protocol has no native tool channel, // so strip absorb from the loop config there (both modes). @@ -288,8 +291,9 @@ export async function prepareResponses( markNativeCompactionBoundary(session); if (reconcileNativeCompactionBoundary(session)) { // #3712: same post-rebase re-measure as the early site above. - ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(msgs, imageReserve)); + ({ tokens: tokenCount, source: tokenCountSource } = postRebaseTokenCount(parsed, imageReserve)); noteRebasePricing(session, { tokens: tokenCount, source: tokenCountSource }); + log("info", `[${sessionId}] post-rebase pricing: ${tokenCount} tok (${tokenCountSource}) — nudge meter re-based off the current compacted view (#3712)`); } } } diff --git a/tests/issue3712-post-rebase-nudge-meter.test.ts b/tests/issue3712-post-rebase-nudge-meter.test.ts index f4082de51b..0ad2c92160 100644 --- a/tests/issue3712-post-rebase-nudge-meter.test.ts +++ b/tests/issue3712-post-rebase-nudge-meter.test.ts @@ -128,6 +128,11 @@ function checkpointMessage() { const FOLD_SUMMARY = "MAIN-SUMMARY-SETUP-CONTEXT-FOLDED-BY-COMPRESSION-LONG-ENOUGH-FOR-KERNEL-MIN-LENGTH-CHECK"; +// #3712 review: the re-measure must price the FULL outbound view — tool schemas +// included, matching the usage-grade caliber (upstream prompt_tokens) it replaces. +// A messages-only count drops this block entirely; the floor below discriminates. +const TOOLS_DESC = "T".repeat(20_000); + async function seedAndFold(rig: Rig, conv: string): Promise { const r1 = await fetch(chatUrl(rig), { method: "POST", @@ -165,7 +170,11 @@ test("#3712: a dsh native-compaction rebase re-measures the turn's sizing input const r = await fetch(chatUrl(rig), { method: "POST", headers: dshHeaders(conv), - body: JSON.stringify({ model: MODEL, messages: [checkpointMessage(), seedInput()[12], seedInput()[13]] }), + body: JSON.stringify({ + model: MODEL, + messages: [checkpointMessage(), seedInput()[12], seedInput()[13]], + tools: [{ type: "function", function: { name: "probe", description: TOOLS_DESC, parameters: { type: "object", properties: {} } } }], + }), }); assert.equal(r.status, 200, "compacted replay request succeeds"); await r.text(); @@ -184,6 +193,10 @@ test("#3712: a dsh native-compaction rebase re-measures the turn's sizing input assert.ok(boundary?.priced, "the rebased turn's pricing was recorded on the boundary"); assert.equal(boundary?.priced?.source, "estimate", "post-rebase sizing input is a fresh measure, not the stale usage-grade baseline"); assert.ok((boundary?.priced?.tokens ?? 0) > 0, `post-rebase sizing input measured a real current-view size (${boundary?.priced?.tokens})`); + // Caliber pin: the tool schema (TOOLS_DESC, ~5K tok at 4 chars/token) must be + // in the measure — a messages-only count would land near the core mass only + // (~1.8K tok here) and fail this floor. + assert.ok((boundary?.priced?.tokens ?? 0) >= TOOLS_DESC.length / 4 + 500, `post-rebase pricing covers the full outbound view incl. tools (${boundary?.priced?.tokens} >= ${TOOLS_DESC.length / 4 + 500})`); } finally { await rig.close(); } From 7d84a6b159be8dec72ec799fcf47f226fe25a4b1 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:20:15 +0800 Subject: [PATCH 3/4] chore: sync root lockfile for kernel 0.0.165 (lock-sync #4599) --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": { From dfadc21b339bfa45c6ec03b2f13e723ce6bf0507 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:22:50 +0800 Subject: [PATCH 4/4] fix(ci): sync lockfile recorded versions with acp-kernel 0.0.165 (#4599) master window commits 293ae6542/de5e03659 bumped kernel/package.json to 0.0.165 without regenerating the lockfiles; lock-sync re-resolution then rewrites the stale recorded versions (root 0.0.163, kernel 0.0.164) and fails. Pure recorded-version sync via npm install --package-lock-only; no dependency or semantic change. --- kernel/package-lock.json | 4 ++-- package-lock.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index da011d94e1..4c5e2d79e9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": {