Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag

### Changes

- **快照渲染:proxy 车道的出站前缀改由服务端正史渲染,存量会话对内核升级免疫(#3756,根治 #3752/#3751)。** 折叠覆盖此前锚定在"客户端本轮重发的字节"上——消息 id 由内容哈希现算,身份函数一变(协议切换 #3640、#3642 的一次性 re-key、dsh 图片 offload #3588、宿主自查点)覆盖名单就与历史失联:轻则整段历史因 MAX_ANCHORS=16384 封顶失锚(#3752),重则图片族 `_N` 后缀全体位移重铸、每轮 fold 断缓存(#3751)。新引擎 `src/server/snapshot-render.ts`(灰度开关 `BILI_ROLLOUT_SNAPSHOT_RENDER`,默认 0%):当会话有活跃折叠块且统一存储可读时,prepare 层的出站消息序列 = 服务端快照行前缀(覆盖区由块的 seq/signature 覆盖集自拥抑制 + `acp_summary_*` 锚原位重渲,身份跟随块而非客户端字节)+ 客户端尾巴(分歧点 join 之上逐字保留,rewind 裁剪不复活)。链回溯用 id 快路径 + 内容签名兜底(#3658 canonical 种子同构,升级后同内容仍可链上);任何环节失步整轮降级回 legacy 路径,零行为面风险。验收:身份函数原地翻盐后出站前缀逐字节不变(老病当场钉死)、协议中途切换会话不分叉、客户端任意删/checkpoint 覆盖区不复活(一轮采用滞后,模块头有注释)、稳态轮轮前缀字节稳。观测闩 `metadata.snapshotRenderTurns`;kernel 加 `__setMessageIdSaltForTests` 测试缝(kernel 0.0.134,仅测试可达,生产零调用)。

- **Automatic upgrades now stagger across a window instead of landing within minutes; a public policy file controls the window fleet-wide (#3619).** Every bili instance that discovers a new version draws ONE random moment inside a delay window (built-in default **15 min – 48 h**), persists it, and upgrades when it arrives — restarts never re-roll. Before this, a release hit every auto-updating install within ~15–30 minutes, leaving no time to stop a bad one mid-rollout (#3401/#3600/#3394 were all shipped-too-fast incidents). The window and the emergency controls live in `auto-update-policy.json` in this repo, fetched anonymously (plain GET + ETag, zero telemetry, fail-open — an unreachable or malformed file falls back to local defaults): `hold` freezes automatic upgrades fleet-wide, `blocked` skips listed versions, `pinned` sets an upgrade ceiling, and per-version `overrides` tune the window (`{ "range": ">=0.1.200", "maxDelay": "1h" }`). Delay fields take human-readable durations — `"15m"`, `"2d"`, `"1d12h"` (units s/m/h/d/w, combinable; plain numbers still mean raw ms); malformed values are ignored with a warning, never guessed. The file steers timing/skip/pin only — it can never control what gets installed (no URLs, no commands; target versions must exist in the npm packument). Exits: manual `bili update` bypasses `hold`/stagger (still respects `blocked`/`pinned`), `update.staggerMinDelayMs: 0` + `update.staggerMaxDelayMs: 0` (or the `BILI_UPDATE_STAGGER_*_MS` twins) restore legacy immediate upgrades on one machine, `ACP_AUTO_UPDATE=0` disables auto-updates entirely. The update-driven e2e suites pin stagger 0/0 + an unroutable policy URL — they test periodic-update mechanics, not rollout timing.
- **openai wire: non-array content tolerance (kernel 0.0.133, #3658 follow-up)** — the #3642 per-part rewrite iterated `messages[].content` and threw `TypeError: m.content is not iterable` on clients that emit a lone content-part object instead of an array (#2824 corpus caught it). Non-array non-string content now degrades to the legacy empty-text core (byte-compatible ids and bytes with the pre-#3642 codec) instead of throwing. Also regenerates the storage-render chat goldens for the documented one-time canonical re-key (tool-pair ids), and inverts the retired reconcile-off id-churn control into an absorption canary: with reconcile OFF, tool_call id rewrites + argument re-serialization must not disturb fold coverage (canonical ids absorb them by construction).
- **protocol-stable core identity: the same logical conversation now yields byte-identical core message ids in all four dialects (#3642).** Wire codecs previously baked dialect accidents into message identity: anthropic used `"[image]"` as image text while openai deliberately kept `""` ("switching the seed would move every pre-existing shape"), tool-call ids embedded the provider's `toolCallId` bytes, raw JSON tool arguments hashed verbatim (key order = new id), and responses' empty-text reasoning cells seeded from the provider's volatile `item.id`. Switch protocols mid-session (opencode #3640: `/chat/completions` → `/v1/messages`) and ~20-68% of ids churned, tripping the fold-destroyed misdiagnosis #3641 just class-fixed. Identity now derives from a canonical form shared by all four codecs: a new `kernel/src/wire/identity.ts` (canonicalToolArgs: JSON args parsed → recursive key sort → compact stringify, verbatim for non-JSON; canonicalOpaqueSeed: provider `item.id`/`status` stripped; image marker `[image]` everywhere), the `toolCallId` slot retired from the seed (cluster suffixes disambiguate same-shape calls, deleting one sibling is local damage), and granularity normalized to one core per content part (anthropic's existing shape — its lanes take zero granularity re-keying, though tool-call/tool-result cores still re-seed since the `toolCallId` slot is retired; openai/responses/google split runs, with accumulators re-merging on rebuild so the wire stays byte-stable). The kernel first-user pin now spans the opening message's whole core run, so a multimodal opening message keeps its image bytes through folds (#781/#2607). Cross-protocol golden: text/tool/bash conversation → identical id arrays in all four dialects; canonical key-sort makes `{"file","line"}` == `{"line","file"}`. Image cells are now stamped `imagePart: true` at parse time (never text-compared — tag injection rewrites markers mid-render, which previously made image cores render as text and lose the real bytes, #767 billing fallout). Upgrade cost is one re-key of stored ids for openai/responses/google lanes; the #3641 reconcile tiers (word-bag claims, k-th image family pairing, toolCallId channel, epoch gate) absorb it without re-folding.
Expand Down
1 change: 1 addition & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes
| `BILI_PERSIST_EPERM_ALERT_REPEAT_MS` | Re-alert window for the persist EPERM alert, in ms. `0` (default) = alert once then stay silent; `>0` = re-alert at most every that many ms while the failures continue. |
| `BILI_TUNNEL_ALLOWED_HOSTS` | `/bili/<absolute-url>` tunnel admission for **remote clients** (#409): comma-separated `host` or `host:port` entries that unlock loopback/private destinations (e.g. a LAN relay or the machine's own sglang) for non-loopback clients. The proxy itself and link-local/metadata addresses are always denied; local (loopback) clients always pass. |
| `BILI_MAX_SESSIONS` | Max sessions held in memory (default `256`; LRU eviction — disk is the source of truth). |
| `BILI_ROLLOUT_SNAPSHOT_RENDER` | Snapshot render (#3756): set `1` to render the outbound history prefix from the server-side unified-storage transcript instead of the client-resent bytes. Active only when the session has live fold blocks AND unified storage is readable; otherwise every request falls back to the legacy path unchanged. Kills the #3752/#3751 id-churn class: anchor coverage follows block seqs + content signatures, so codec/identity upgrades, client checkpoints and host compactions can no longer drop or duplicate folded history. Per-feature form of `BILI_ROLLOUT_<FEATURE>` (default 0% rollout). |
| `BILI_SESSION_WARM_MAX` | Bounded startup (#2803): max most-recently-saved sessions hydrated at boot before listening (default `0` = warm none, capped by `sessions.max`); older state reloads on demand. Set e.g. `8` to restore eager warming of the recent set. File twin: `sessions.warmMax`. |
| `BILI_SESSION_WARM_MAX_BYTES` | Decoded-byte budget for the boot warm set (#2803; default `67108864` = 64 MiB). Counts DECODED size, not compressed on-disk bytes, so zstd archives cannot sneak past the RAM bound. A single file over the budget is never warmed. File twin: `sessions.warmMaxBytes`. |
| `BILI_SESSION_RESIDENT_MAX_BYTES` | Soft target for estimated resident session bytes (#2803; default `536870912` = 512 MiB). Active/in-flight state stays pinned above it (the byte budget limits the idle cache, not active workload); when over target, idle state discharges back to disk as requests complete. File twin: `sessions.residentMaxBytes`. |
Expand Down
2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.134",
"version": "0.0.135",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
11 changes: 10 additions & 1 deletion kernel/src/wire/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,22 @@ function canonicalize(v: unknown): unknown {
* pinned empty, so plain-text messages keep their pre-#3642 ids verbatim
* (zero migration churn for text-only sessions).
*/
let idSalt: string | null = null;

/** TEST ONLY (#3756): flips the id seed to simulate a codec/identity upgrade
* mid-session — every subsequent id changes although content is byte-identical.
* Production code never sets this. */
export function __setMessageIdSaltForTests(salt: string | null): void {
idSalt = salt;
}

export function deriveCanonicalMessageId(
role: string,
contentType: string,
text: string,
options: { toolName?: string } = {},
): string {
const seed = `${role}|${contentType}||${options.toolName ?? ""}|${text}`;
const seed = `${idSalt ? idSalt + "|" : ""}${role}|${contentType}||${options.toolName ?? ""}|${text}`;
return "h_" + hashId(seed);
}

Expand Down
1 change: 1 addition & 0 deletions kernel/src/wire/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./demoted-thinking.js";
export * from "./mirror.js";
export * from "./util.js";
export * from "./compress-detect.js";
export * from "./identity.js";
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.

24 changes: 24 additions & 0 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,30 @@ export class SessionStore {
}
}

/** #3756 snapshot render: the logical transcript view's seq pointers
* (tail_json.tailSeqs). Null when the unified store is unavailable. */
unifiedTailSeqs(sessionId: string): number[] | null {
if (!this.shadow || this.shadow.stats.disabled) return null;
try {
return this.shadow.tailSeqs(sessionId);
} catch {
return null;
}
}

/** #3756 snapshot render: full-identity row view of the unified
* transcript (msgId/ref/refNum/contentHash included) in seq order.
* Null when the unified store is unavailable — the render lane must
* degrade to the legacy path, never guess. */
unifiedTailViewRows(sessionId: string): import("./storage/store.js").MessageRow[] | null {
if (!this.shadow || this.shadow.stats.disabled) return null;
try {
return this.shadow.tailViewRows(sessionId);
} catch {
return null; // sidecar degraded mid-call: treat as unavailable
}
}

/** #2960 R2.1: summary rows for list surfaces (CLI export, web exposure
* tick). Full mode only — in shadow the JSON tree is the truth base and
* rows may lag files never yet recorded, so list surfaces must walk.
Expand Down
8 changes: 8 additions & 0 deletions src/rollout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ export const ROLLOUT_FEATURES: Record<string, RolloutPlan> = {
// `rollout.features.anthropicThreadsReforge: true` or
// `BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE=1`.
anthropicThreadsReforge: { percent: 0 },
// #3756 snapshot render: assemble the outbound prefix from the server-side
// canonical transcript (unified storage) instead of re-deriving message
// identity from client-resent bytes, making folded sessions immune to
// codec/identity upgrades (#3752) and to client-side head
// deletion/checkpointing (#3723). Degrades to the legacy path whenever the
// rollout is off or the unified transcript cannot resolve. Default 0% —
// force on with `BILI_ROLLOUT_SNAPSHOT_RENDER=1`.
snapshotRender: { percent: 0 },
};

export const BP_MAX = 10_000;
Expand Down
12 changes: 9 additions & 3 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTo
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";
import { maybeSnapshotView } from "./snapshot-render.js";
import { nudgeSuppressed } from "../session-self-heal.js";
import { compressBreakerArmed } from "../stream.js";
import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js";
Expand Down Expand Up @@ -196,6 +197,11 @@ export async function prepareAnthropic(
try {
const { msgs, cacheControls } = anthropicToCore(parsed);
originalMessages = msgs;
// #3756 snapshot render: outbound prefix from the unified transcript
// (fold-time ids, immune to identity re-mints); live tail keeps the
// client's bytes. No-op when the lane is inactive/degraded; the
// reconcile call below still observes the CLIENT view.
const renderMsgs = maybeSnapshotView(session, msgs, { sessionId, log });
// #1320: signature-only thinking blocks bill restored thinking tokens upstream
// but project as empty text locally — attribute the provider-vs-local residual
// to them so every meter sees the billed context (metering-only, wire untouched).
Expand Down Expand Up @@ -224,7 +230,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, msgs, inboundImageTokens);
const { 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 @@ -287,8 +293,8 @@ export async function prepareAnthropic(
// #3222: BEFORE processTurn — its hide-compress-calls node removes
// inactive call+result pairs inside, which would strip the anchors
// this pass needs (the leak applies to the bare name too).
reasoningPlan = planReasoningDrop(msgs, reasoning, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)));
const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) });
reasoningPlan = planReasoningDrop(renderMsgs, reasoning, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)));
const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) });
session.state = turn.state;
adoptContentStore(session, turn.contentStore);
// The fold from last turn's compress has now materialized in state —
Expand Down
13 changes: 10 additions & 3 deletions src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTo
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";
import { maybeSnapshotView } from "./snapshot-render.js";
import { nudgeSuppressed } from "../session-self-heal.js";
import { compressBreakerArmed } from "../stream.js";
import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js";
Expand Down Expand Up @@ -112,13 +113,19 @@ export async function prepareOpenai(
openaiSystemText = outcome.outbound;
}
originalMessages = msgs;
// #3756 snapshot render: the outbound prefix renders from the unified
// transcript (server-authoritative, fold-time ids — immune to identity
// re-mints #3752); only the live tail keeps the client's bytes.
// No-op returns `msgs` when the lane is inactive/degraded.
// reconcileFoldCoverage below still observes the CLIENT view.
const renderMsgs = maybeSnapshotView(session, msgs, { sessionId, log });
// #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));
// 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, msgs, imageReserveFor(session, "openai", parsed, opts, billingUpstream ?? upstreamOrigin));
const { 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 @@ -177,8 +184,8 @@ export async function prepareOpenai(
// #3222: BEFORE processTurn — its hide-compress-calls node removes
// inactive call+result pairs inside, which would strip the anchors
// this pass needs (the leak applies to the bare name too).
reasoningPlan = planReasoningDrop(msgs, reasoning, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)));
const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) });
reasoningPlan = planReasoningDrop(renderMsgs, reasoning, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)));
const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) });
session.state = turn.state;
adoptContentStore(session, turn.contentStore);
// The fold from last turn's compress has now materialized in state —
Expand Down
Loading
Loading