Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e6af249
fix: mark structurally-exhausted tier-recovery deterministic (#5766)
Nov 5, 2026
35922b3
test: reference GitHub issue #5764 in tier-exhaustion fix (was intern…
Nov 5, 2026
e76f371
fix: reanchor tag-changed shorter replays whose logical tail survived…
Nov 7, 2026
2ea662b
test: pin the #5844 repacked-tail reanchor (positive case, guardrails…
Nov 7, 2026
5718af8
docs: document persona-lane drift recovery — no supported attach comm…
Nov 7, 2026
57d0ca7
fix(#5844): review hardening — distrust a persisted canon column whos…
Nov 7, 2026
30b479b
test(#5844): review fix — guardrail must exercise the canon path, not…
Nov 7, 2026
dc0d5ee
docs(#5844): document the calls-turn prose boundary of the content-ve…
Nov 7, 2026
207a24d
fix: correct stale rung ordinal — #5686 shorter-tail rung is the sixt…
Nov 7, 2026
0be5b73
fix: stop anthropic-wire sessions from sailing past the upstream wall…
Nov 7, 2026
ad7a297
fix: drop dead Record<never, never> from #5880 test session cast
ranxianglei Nov 10, 2026
9d18c77
fix: keep overflow-armed tier exhaustion 502-retryable, not 413 (#6451)
Nov 13, 2026
a8b5fa6
Merge branch 'master' into 2026-11-12_preflight-tier-batch
Nov 13, 2026
c9d9ce7
Merge remote-tracking branch 'origin/ci-fixes' into sweep-6435
Nov 13, 2026
b309b27
Merge remote-tracking branch 'origin/master' into pr6435-conflict-res…
Nov 15, 2026
8ed56be
docs(architecture): add src/responses-system-sections.ts to the modul…
Nov 16, 2026
1a4450b
Merge remote-tracking branch 'upstream/master' into HEAD
Nov 16, 2026
245ecb1
fix(test): recalibrate #2929 probe anchor to nudge positions {3,5,11,14}
Nov 16, 2026
f4e289d
Merge remote-tracking branch 'origin/master' into pr6435-conflict-res…
Nov 16, 2026
7247410
test(#2929): make fold probe schedule-independent + hermetic wire shape
Nov 16, 2026
53fea86
merge origin/ci-fixes: inherit master CI fixes (#2929 fold-probe sche…
Nov 16, 2026
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
45 changes: 45 additions & 0 deletions docs/session-identity.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,51 @@ From then on:
claude/codex/dsh need nothing here: they share one session id across
subagents or have no child-session concept at all.

## Persona-lane drift: when re-anchoring is impossible (#5844)

The order resolver keeps the live conversation on the raw id through persona
text changes whenever it can verify continuation (head continuity,
header-corroborated model switch, positional fragment runs, fragment- or
content-verified tails — `src/session-resolve.ts`). When the host destroys ALL
byte-level evidence in the same turn it rotates its system prompt — e.g. an
upgrade that changes both the system prompt and the message-record layout
(#5844), or a grown+shifted replay (#5714) — the conversation forks onto
`<id>|sub:<fp>` with empty compression state. That fork is by design:
mis-attribution is the most expensive failure in this system (MESSAGE-IDENTITY).

The "content-verified" tail compares the last ≤8 turns in canonical form —
the identity chain's own hashing input (`neutralTurnCanonical`): kind plus
logical text/calls. For an assistant turn carrying tool calls the canonical
form is calls+role only (cross-wire neutrality — OpenAI renders such turns
with `content: null`), so a prose-only difference inside a calls turn is
invisible to this rung exactly as it is to the rest of the chain that shares
the same canonical form. A different conversation must still replay identical
user questions, tool results and tool calls across the whole tail window to be
accepted; record regrouping alone can never do that.

**There is no supported command to re-attach a derived lane to its parent,
and no config knob for it.** The resolution is decided per request from
evidence only; after a drift, the only sanctioned path is surgery on the
persisted skeleton:

1. Stop the proxy first — the running daemon silently rewrites
`<state dir>/session-order.json` from its in-memory skeletons, so any edit
made while it runs is lost (state dir = `$XDG_STATE_HOME/billion-context`,
default `~/.local/state/billion-context/`).
2. In `session-order.json`, set the parent entry's `tagHash` to the sha256 hex
digest of the host's CURRENT full system text (the field stores the full
digest, not a truncated fingerprint), or delete the entry entirely to let
the raw id be claimed fresh on the next request.
3. Restart the proxy; the next request re-anchors onto the raw id and the
fold state stored under it becomes live again.
4. Remove the orphaned derived lane: `bili sessions rm <id>|sub:<fp>`
(its rows are otherwise inert but keep growing).

Do NOT merge lanes when the replay content genuinely diverged (a different
conversation riding the same client id) — that is exactly what the fork
exists to prevent; the procedure above is for "same conversation, evidence
destroyed by the host".

## Future direction

The durable fix is a **client-side identity signal**: a stable
Expand Down
15 changes: 15 additions & 0 deletions docs/session-identity.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@

claude/codex/dsh 在这里不需要任何信号:它们的 subagent 共享同一个会话 id,或根本没有子会话概念。

## 派生车道漂移:无法重新锚定时怎么办(#5844)

顺序解析器在能验证延续性时(头部延续、头部佐证的模型切换、逐位 fragment 连续段、fragment 或内容校验的尾部——`src/session-resolve.ts`)会把活会话留在原始 id 上。但当宿主在轮换 system prompt 的同一轮里销毁了**全部**字节级证据——例如一次升级同时改了 system prompt 和消息记录布局(#5844),或增长+位移的重放(#5714)——会话就会分叉到 `<id>|sub:<fp>`,压缩状态为空。这是有意设计:误归属(mis-attribution)是本系统代价最高的失败(MESSAGE-IDENTITY)。

「内容校验」的尾部比较的是最后 ≤8 轮的**规范化形式**——身份链自有的 hash 输入(`neutralTurnCanonical`):kind 加逻辑文本/调用。对携带工具调用的 assistant 轮,规范化形式只含 calls+role(跨线中性——OpenAI 会把这类轮渲染为 `content: null`),因此 calls 轮内**仅散文不同**对本档不可见,这与共享同一规范化形式的身份链其余部分完全一致。另一个对话要能被接受,仍须在整段尾窗上重放逐字一致的用户提问、工具结果与工具调用;单纯的消息重打包做不到这一点。

**目前没有任何受支持的命令能把派生车道重新挂回父会话,也没有对应的配置项。** 解析结果每轮只由证据决定;漂移发生后,唯一被认可的恢复路径是对持久化骨架做手术:

1. 先停代理——运行中的守护进程会用内存里的骨架悄悄重写 `<state 目录>/session-order.json`,运行期间做的任何编辑都会丢失(state 目录 = `$XDG_STATE_HOME/billion-context`,默认 `~/.local/state/billion-context/`)。
2. 在 `session-order.json` 里,把父条目的 `tagHash` 设为宿主**当前**完整 system 文本的 sha256 hex 摘要(该字段存的是完整摘要,不是截断指纹);或者直接删掉整个条目,让原始 id 在下一次请求时重新认领。
3. 重启代理;下一次请求重新锚定到原始 id,其下已存储的折叠状态随之复活。
4. 删除孤儿派生车道:`bili sessions rm <id>|sub:<fp>`(其行数据虽不再被使用,但会持续增长)。

如果重放内容与记录**确实**分叉了(同一客户端 id 上跑着另一个对话),不要合并车道——这正是分叉机制要防止的事;上面的流程只适用于「同一对话、证据被宿主销毁」的情形。

## 未来方向

持久的修复是**客户端侧身份信号**:由客户端自身提供的稳定 per-conversation
Expand Down
1 change: 1 addition & 0 deletions reference/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ billion-context/
│ ├── remote-notices.ts # Remote announcement feed watcher: fail-open polling, last-known-good snapshot (#5548)
│ ├── request-membership.ts # Settle-side membership probe: is this usage report ours (#4805)
│ ├── resolver-wedge.ts # Persistent local DNS resolver wedge detector + alarm (#5221)
│ ├── responses-system-sections.ts # Single source of truth for the Responses-wire developer-section join: steady prepare + round-2 rebuild share one separator (#6922)
│ ├── responses-tool-output.ts # Responses tool-output image parts: projection + patch helpers
│ ├── rollout.ts # Feature rollout: per-install uuid, basis-point buckets, nested cohorts (#3062)
│ ├── runaway-guard.ts # Intrinsic stream terminator for runaway tag/codepoint enumeration (#2346)
Expand Down
15 changes: 12 additions & 3 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1975,7 +1975,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
const active = deps.session.state.blocks.filter((b) => b.active);
return active.filter((b) => b.tier === 1).length >= 2 || active.filter((b) => b.tier === 2).length >= 2;
};
type TierRecoveryResult = { recovered: boolean; callsUsed: number; netSaved: number; honestDetail: string };
type TierRecoveryResult = { recovered: boolean; callsUsed: number; netSaved: number; honestDetail: string; stopReason: "fit" | "exhausted" | "cap" | "unavailable" | "no-shrink" };
const attemptTierRecovery = async (): Promise<TierRecoveryResult> => {
let callsUsed = 0;
let netSaved = 0;
Expand Down Expand Up @@ -2187,7 +2187,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
`futile round: folding compressible raw messages plus tier distillation (${callsUsed} summary call(s), net ~${netSaved} tokens saved) still leaves the payload ~${overBy} tokens over the window — ${why}. ` +
`These figures are measured on the post-fold wire view; the separately displayed context total additionally floors on the upstream usage baseline, which also bills the system prompt and tool definitions. ` +
`Raise the model context window or restart the session.`;
return { recovered, callsUsed, netSaved, honestDetail };
return { recovered, callsUsed, netSaved, honestDetail, stopReason };
};
for (let round = 0; round < effectiveRoundCap; round++) {
if (deps.signal?.aborted) {
Expand Down Expand Up @@ -3237,7 +3237,16 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
const rec = await attemptTierRecovery();
deps.log("info", `[preflight] tier-distillation recovery finished: ${rec.recovered ? "recovered" : "not recovered"}; ${rec.callsUsed} summary call(s), net ~${rec.netSaved} tok saved (#2952)`);
if (!rec.recovered) {
failure = { kind: "exhausted", detail: rec.honestDetail };
// #5764/#3461: only the structurally-exhausted ending (span-math proved no legal
// two-block span / not enough positionable blocks — e.g. oversized inline media
// blowing every span past TIER_DISTILL_SOURCE_CHAR_BUDGET) is deterministic →
// 413 not-forwarded; cap/unavailable/no-shrink are relay/budget-dependent and MUST
// stay 502-retryable (server.ts fail-fast contract).
// #6451: an overflow-armed baseline is a relay-billing artifact, not a structural
// doom — the upstream proved a smaller window, so the client may still recover by
// switching models or the upstream recovering; keep it 502-retryable.
const overflowArmed = deps.session.stats.lastInputTokensSource === "overflow-arm";
failure = { kind: "exhausted", deterministic: rec.stopReason === "exhausted" && !overflowArmed, detail: rec.honestDetail };
}
}
if (currentTokens >= limit && !failure) {
Expand Down
7 changes: 2 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, applyPostRebuildBaselineAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback } 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 @@ -4374,10 +4374,7 @@ export async function preflightCompressIfNeeded(
const rebuiltImageTokens = imageReserveFor(session, rebuilt.protocol, rebuilt.body, opts, upstreamUrl);
remainingImageTokens = rebuiltImageTokens;
reducedImageWire = result.compressedRanges > 0 && rebuiltImageTokens < imageTokens;
if (rebuiltTextSize > session.stats.lastInputTokens) {
session.stats.lastInputTokens = rebuiltTextSize;
session.stats.lastInputTokensSource = "estimate";
}
applyPostRebuildBaselineAnchor(session, rebuiltTextSize);
// Same calibrated caliber as the trigger above — gate, per-round exit
// and this final fit must judge the payload on one scale (#1933 F1).
// The baseline anchor above stays raw deliberately: it is a floor for
Expand Down
19 changes: 10 additions & 9 deletions src/server/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ export { outputBudgetField, readOutputBudget, writeOutputBudget };

// #453 hard backstop: cap the forwarded output budget so input+output can never
// exceed the window on request-rebuilding upstreams (vLLM rejects an oversized
// total instead of clamping). Wired into the openai/responses/google prepare
// lanes only — canonical Anthropic routes enforce their input limit
// independently of max_tokens, so they get no clamp call site. #4205 boundary:
// wrapper providers on the anthropic wire (completion billed against the same
// window) DO reserve headroom at the band layer but keep no egress clamp here;
// their residual over-requests ride the overflow self-heal (#4093) instead.
// The proxy owns both sides of the sum, so capping output to (window - input -
// margin) makes the overflow impossible even when the agent ignores the
// compress nudge.
// total instead of clamping). Wired into ALL FOUR prepare lanes — openai,
// responses, google, and anthropic (#5880 added the anthropic site: wrapper
// providers on that wire bill completion against the SAME stated window as
// input — DeepSeek's 400 quotes "N in the messages, M in the completion"
// against one wall — which is exactly the shape this clamp exists for; for
// canonical Anthropic, whose input limit is enforced independently of
// max_tokens, the requested budget already rides under the window and the
// clamp is a no-op). The proxy owns both sides of the sum, so capping output
// to (window - input - margin) makes the overflow impossible even when the
// agent ignores the compress nudge.
const OUTPUT_CLAMP_MARGIN_PCT = 0.05;
const OUTPUT_CLAMP_MIN_MARGIN = 2048;
// #5278: safety inflation applied to the planning estimate BEFORE sizing both
Expand Down
2 changes: 1 addition & 1 deletion src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3132,7 +3132,7 @@ async function handleRequest(
return await prepareGoogle(work as GoogleRequestBody, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, nativeWindow, googleModel ?? v1Model, googlePathKind(urlPath) === "stream-generate", visibilityMarkers, upstreamOrigin, req, decide);
}
const preparedOut = protocol === "anthropic"
? await prepareAnthropic(work as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, reasoningCfg, visibilityMarkers, requestModel, decide)
? await prepareAnthropic(work as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, visibilityMarkers, requestModel, decide)
: protocol === "openai"
? await prepareOpenai(work as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, visibilityMarkers, route?.rewrittenUrl, decide, bodyText)
: responsesCompact
Expand Down
14 changes: 12 additions & 2 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ 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, dedupeAdjacentOutputConfig, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js";
import { applyReasoningDrop, dedupeAdjacentOutputConfig, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js";
import { currentCalibrationFactor } from "../util.js";
import { recordNudgeObservation } from "../context-analysis.js";
import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js";
import { emitNudgeInjected, nudgeDebugMeta } from "../nudge-debug.js";
Expand Down Expand Up @@ -40,7 +41,7 @@ import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker
import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js";
import { applyImageRescalePass } from "../image-rescale.js";
import { isAutoModeClassifier } from "../auto-mode-forge.js";
import { countSystemAndToolsTokens, emergencyNudge, emergencyNudgeArmed, projectThinkingMass } from "./budget.js";
import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, projectThinkingMass } from "./budget.js";
import { effectiveAbsorbBlock } from "./prepare-responses.js";
import { effectiveBlockOpsEnabled } from "./prepare-responses.js";
import { injectSystem, injectTool } from "./inject.js";
Expand All @@ -63,6 +64,7 @@ export async function prepareAnthropic(
session: Session,
pluginMode: boolean,
upstreamOrigin: string,
nativeWindow: number,
reasoning: CompressReasoningConfig | undefined,
visibilityMarkers: boolean,
/** The route's resolved model, when the caller already has one. On the
Expand Down Expand Up @@ -693,6 +695,14 @@ export async function prepareAnthropic(
// main-scale tiebreak sees this request's own upper bound.
probeRequestMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0);
if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin;
// #5880: egress output-budget clamp, same call-site family as the openai/
// responses/google lanes (prepare-openai.ts). Wrapper providers on this
// wire bill completion against the SAME stated window as input (DeepSeek's
// 400: "N in the messages, M in the completion" against one wall), so an
// input-only fit judgment must be backed by an output clamp here too; for
// canonical Anthropic (separate input/output limits) the requested
// max_tokens already rides under the window and the clamp is a no-op.
clampOutgoingOutput(rebuilt as Record<string, unknown>, "max_tokens", { systemText: extractSystem(systemOut), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserve, kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: upstreamOrigin, ledgerFloorTokens: dshLedgerFloorTokens(session.metadata), knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record<string, unknown>, opts.routes, upstreamOrigin, opts.sessionHeader, modelIdOf(parsed)) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; });
if (nudgeOutcome === "injected") recordNudgeInject(session, nudge);
return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared;
}
Expand Down
Loading
Loading