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
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions src/agent/dsh-compaction-disable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 });
}
Expand Down
38 changes: 38 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2563,6 +2563,44 @@ 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).
// 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
// 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<string, unknown>).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
Expand Down
13 changes: 11 additions & 2 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -336,6 +336,15 @@ 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(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
// re-serialization stay covered (src/fold-reconcile.ts).
Expand Down
16 changes: 13 additions & 3 deletions src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -175,10 +176,19 @@ 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(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
// re-serialization stay covered (src/fold-reconcile.ts).
Expand Down
13 changes: 11 additions & 2 deletions src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -187,6 +187,15 @@ 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(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
// re-serialization stay covered (src/fold-reconcile.ts). The verdict
Expand Down
Loading
Loading