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

## [Unreleased]

## [0.1.207] — 2026-10-24

### Storage — 行身份与史官化(#4390,storage review 四个 p2 + 三个顺路 p3)

- **行身份去污染(D4-1)**:统一存储 messages 行新增 `identity` 列——跨保存 JOIN 键改为**位置无关**序列化(剥离 `id`/`wireIndex` 后哈希)。此前身份掺入 wire 位置元数据,客户端头部截断一条消息就会让后续全部行换血(旧行被清扫、seq 重铸、#4335 的区间账记随之抖动)。`content_hash` 保持字节忠实 CAS 键不变。存量库 v8 迁移:回填 identity 并合并 wireIndex 分裂世代(保最低 seq,重复行退还 CAS 引用)。MySQL 同款(information_schema 探测幂等 ALTER + 回填)。
- **权威集清扫改墓碑(D4-2)**:proxy 会话每轮全量保存不再物理 DELETE 缺席行,改为 `retired_at` 墓碑——rewind/undo 的历史在统一存储里**耐久保留**;同一身份回来时原地复活(保原 seq,含 completeLog-less 保存路径 remap 复活)。读面(getMessages/统一重放/视图)默认过滤 retired;CAS 引用不释放。deleteSession/archive/GC 继续拥有物理生命周期。
- **渲染接缝图片真指纹(D4-3)**:snapshot-render 的 identitySig 图片段从 `长度` 改为 `sha256(base64) 前 24 hex`——同尺寸截图不再可能连击假接缝造成静默截断。
- **fork 谱系入正史(D3-2)**:sessions 行的 `parent_session_id` 现在从信封 `metadata.derivedFromSessionId` 落库(此前恒 null,谱系只活在信封里,信封丢失=家族失忆)。
- **顺路**:gc full vacuum 走 `vacuum('full')`(带 TRUNCATE checkpoint,文件真正缩小,D5-1);loadLegacyView 尾指针缺行从静默缩水改为计数 + 三处加载路径 warn(D4-5);fold-reconcile 的 Pass 0 位置副本失步(存储 ids 与顺序骨架不再 1:1)从静默降级改为响亮 warn(此前无任何日志痕迹)。
- **审查修复(review)**:identity JOIN 迁移后,全量 ingest 对「identity 相同、字节不同」的行原本靠 INSERT OR IGNORE 静默保留旧世代——头部截断后存储世代带着旧 wireIndex 与活视图永久失配(采样校验漂移→水位反复清零→#4323 信封冻结的数据丢失向量),外部 CAS 损坏也永远无法自愈。现在全量路径在**同一 seq** 上采纳更新的字节(seq 稳定性不变,delta 追加路径仍只增不改);`tailGaps` 诊断键从 parity 摘要面剔除(存在性差异不再误报漂移)。
- 测试:`tests/storage-review-p2s.test.ts` 7 桩(截断保号/墓碑+复活/接缝指纹/谱系/迁移去重/gc checkpoint/缺行响亮);storage 全家族 + golden/snapshot/fold-reconcile/incremental/web 回归绿。

### Fixed

- **master CI 全红修复:#4390 historian 的媒体引用释放 + #3250 WAL cap 断言预算(#4506)。** bisect 定位 f161c518..02f544da 窗口 first bad = #4390(row-identity-historian),两个破口:① 权威集清扫改墓碑后,retired 行的 message_media 映射与媒体 CAS 引用永远不释放(默认要等 archive 365 天)——图片重度会话里即 #3762/#3901 的字节泄漏,`media-cas` 家族在 windows 分片稳定红。现在 retire 时释放媒体映射+引用(文本字节照旧史官保留;图片体积大、历史价值低),rewind-redo 复活时由重摄取路径幂等重建映射与引用(sqlite+mysql 双后端同款)。② `#3250: configured cap` 断言的 CAP_SLACK=4KiB(一页余量)预算不住 #4390 新增的每行 WAL 体积(identity 列 64hex + idx_messages_identity 索引项,~192 行超 12 页/47,376B)——截断不变量本身完好,坏的是页级余量的算术;custom-cap 用例改用 64KiB 的 inter-reset 累积预算(注释引用 #4390 行体积),close-converges 等其余断言维持一页余量不变。
- **增量车道:拉取回执修复 ack 传输盲区 + 漂移回退取富侧(#4352/#4323)**。2026-10-22 事故双根因修复:① pi 的 after_provider_response 只在部分 provider 传输下暴露响应头,x-bili-applied-seq 丢失时 noteIncrementalAck 永不执行,后续每轮都整包 re-seed(sinceSeq=0 重发 21K 条历史)。现在 GET /__bili/plugin/turn?conversation=<id> 返回热 canonical 的 {appliedSeq, boundary}(boundary=末条 wire 消息哈希,冷重建诚实返回 null),pi 扩展在规划下一轮前拉取回执,仅在 boundary 证明 canonical 吸收了我方末条消息时采纳——绝不按 seq 前进盲目采纳(外来的全量重发也会推进 canonical);② #4323 多实例写同一目录时,漂移回退无条件吃旧信封,把索引里更新的折叠世代(26,971 refs/16 active)换成旧世代(11,849/12),覆盖与 canonical 失配→0 可压区间→preflight 8.29M 假性死局 502。现在漂移两侧按 mapped ref 数取富侧(平局信封赢,平局语义=原子整文件)。新增 tests/incremental-ack-pull.test.ts 3 例(响应头 ack 回归钉/拉取采纳/冷 boundary null 拒采纳)+ storage-full-flip 4b(陈旧信封让位新索引世代)。

### Changes
Expand Down
4 changes: 2 additions & 2 deletions 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.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "billion-context",
"version": "0.1.206",
"version": "0.1.207",
"description": "A context-compression plugin \u2014 small context windows (a 100K context is enough), 5x fewer tokens, month-long single sessions (billions of tokens), and compression quality \u2014 for all agents: pi, OpenCode, Codex, Claude Code, and more. billion-context is all you need.",
"type": "module",
"main": "dist/index.js",
Expand Down
8 changes: 7 additions & 1 deletion release-notes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
},
"billionContextReleaseNotes": {
"schema": 1,
"updated": "2026-10-21",
"updated": "2026-10-24",
"releases": [
{
"version": "0.1.207",
"date": "2026-10-24",
"tier": "recommended",
"summary": "存储可靠性修复(0.1.203–0.1.204 用户请直接升到本版):折叠上下文不再因模型/窗口切换悄悄膨胀直至会话超窗;修复图片会话媒体引用泄漏与存储校验误报(#4390/#4506/#4444)。EN: storage reliability fixes — folded context no longer silently regrows after model/window switches; media-ref leak and false digest drift fixed. v0.1.203–204 users upgrade immediately."
},
{
"version": "0.1.203",
"date": "2026-10-21",
Expand Down
153 changes: 131 additions & 22 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,22 +1202,99 @@ const dshForkAdopter = createForkAdopter((line) => {
persistClientEvent(`bili-native-dsh: ${line}`);
});

/** A seeded fork child of the current initiator, or undefined. The gate is
* header.isSeeded === true (set only by dsh's fork seed path): subagent
* sessions carry parentSession WITHOUT isSeeded — they run fresh task
* contexts, never replay the parent history, and must NOT inherit. */
function forkChildOf(ctx: PluginContext): { sid: string; parent: string } | undefined {
let session: AgentLike["session"];
// #4215/C10a: structured diagnostics for every skipped fork-adoption entry
// gate. The #4215 incident (seeded child with the correct parentSession, ZERO
// adoption trace in the whole log) showed that silent early returns make the
// fired predicate undiagnosable after the fact: this path had eight ways out
// and none of them wrote anything, so "not a fork" vs "a fork we could not
// see" was unanswerable from the log. The verdict names every shape; the
// caller warns once per session for seeded candidates blocked at a gate and
// notes the ambient-blind shapes ONCE per process (they fire on every
// agentless dispatch — title lanes, disposed scopes — so per-request lines
// would be pure noise). Plain non-fork traffic stays completely silent.
export type ForkChildSkipReason =
| "initiator-threw"
| "initiator-missing"
| "session-missing"
| "sid-missing"
| "header-missing"
| "not-seeded"
| "parent-missing"
| "parent-is-self";

export type ForkChildVerdict =
| { ok: true; sid: string; parent: string }
| { ok: false; reason: ForkChildSkipReason; sid?: string; detail?: string };

/** A seeded fork child of the current initiator, or the named reason it is
* not (or cannot be determined). The fork gate is header.isSeeded === true
* (set only by dsh's fork seed path): subagent sessions carry parentSession
* WITHOUT isSeeded — they run fresh task contexts, never replay the parent
* history, and must NOT inherit ("not-seeded" is intentional and stays
* silent — it is the documented majority of all traffic). */
export function forkChildVerdict(ctx: PluginContext): ForkChildVerdict {
let init: AgentLike | undefined;
try {
session = ctx.agents?.currentInitiator?.()?.session;
} catch {
return undefined;
init = ctx.agents?.currentInitiator?.();
} catch (err) {
return { ok: false, reason: "initiator-threw", detail: err instanceof Error ? err.message : String(err) };
}
const sid = typeof session?.id === "string" && session.id.length > 0 ? session.id : undefined;
if (sid === undefined || session?.header === undefined) return undefined;
if (init === undefined || init === null) return { ok: false, reason: "initiator-missing" };
const session = init.session;
if (session === undefined || session === null) return { ok: false, reason: "session-missing" };
const sid = typeof session.id === "string" && session.id.length > 0 ? session.id : undefined;
if (sid === undefined) return { ok: false, reason: "sid-missing" };
if (session.header === undefined) return { ok: false, reason: "header-missing", sid };
const parent = session.header.parentSession;
if (session.header.isSeeded !== true || typeof parent !== "string" || parent.length === 0 || parent === sid) return undefined;
return { sid, parent };
if (session.header.isSeeded !== true) return { ok: false, reason: "not-seeded", sid };
if (typeof parent !== "string" || parent.length === 0) return { ok: false, reason: "parent-missing", sid };
if (parent === sid) return { ok: false, reason: "parent-is-self", sid };
return { ok: true, sid, parent };
}

const forkGateNoted = new Set<string>();
let noticedForkBlindSpot = false;

function noteForkGateOnce(key: string, level: "info" | "warn", line: string): void {
if (forkGateNoted.has(key)) return;
forkGateNoted.add(key);
logHostEvent(level, "[dsh-client]", line);
}

function noteForkVerdict(verdict: Extract<ForkChildVerdict, { ok: false }>): void {
switch (verdict.reason) {
case "not-seeded":
return; // intentional non-inheritance — see forkChildVerdict
case "parent-missing":
noteForkGateOnce(`lineage:${verdict.sid}`, "warn",
`bili-native-dsh: fork adoption for ${verdict.sid} skipped — the session header says isSeeded but carries no usable parentSession; the child starts without inherited compression state (#4215)`);
return;
case "parent-is-self":
noteForkGateOnce(`lineage:${verdict.sid}`, "warn",
`bili-native-dsh: fork adoption for ${verdict.sid} skipped — the session header carries its own id as parentSession; the child starts without inherited compression state (#4215)`);
return;
default: {
// Blind shapes: the ambient read could not determine whether this
// dispatch was a seeded fork child at all. Once per process — these
// fire on EVERY agentless dispatch, so per-request lines would be
// noise. The scoped-attribution state is part of the message: with
// #4082 active the sid rides the request object while fork
// detection still reads only the ambient initiator — that split is
// exactly what #4215 needs named in the log (C10b).
if (noticedForkBlindSpot) return;
noticedForkBlindSpot = true;
const scoped = sidScope.getStore();
const scopedActive = typeof scoped === "string" && scoped.length > 0;
const shape =
verdict.reason === "initiator-threw" ? `threw (${verdict.detail ?? "unknown"})`
: verdict.reason === "initiator-missing" ? "returned no agent"
: verdict.reason === "session-missing" ? "the agent has no session"
: verdict.reason === "sid-missing" ? "the session has no usable id"
: "the session has no epoch header";
logHostEvent("info", "[dsh-client]",
`bili-native-dsh: fork discovery blind on this dispatch — ambient currentInitiator ${shape}${scopedActive ? "; scoped llm/stream attribution WAS active but fork detection does not use it yet (#4082)" : ""}; a seeded fork child dispatched this way leaves NO adoption trace (#4215)`);
}
}
}

function bodyJsonOf(init: RequestInit | undefined): unknown {
Expand All @@ -1241,14 +1318,33 @@ function bodyJsonOf(init: RequestInit | undefined): unknown {


async function maybeForkAdoptBeforeSend(ctx: PluginContext, init: RequestInit | undefined): Promise<void> {
if (!register.toolsReady) return;
const child = forkChildOf(ctx);
if (child === undefined) return;
const child = forkChildVerdict(ctx);
if (!child.ok) {
noteForkVerdict(child);
return;
}
if (!register.toolsReady) {
noteForkGateOnce(`gate:tools-ready:${child.sid}`, "warn",
`bili-native-dsh: fork adoption for ${child.sid} skipped at the tools-ready gate — this request arrived before bili tool registration completed; later requests can still adopt (#4215)`);
return;
}
const body = bodyJsonOf(init);
if (body === undefined) return;
if (sideShapedBody(body)) return;
if (body === undefined) {
noteForkGateOnce(`gate:body-parse:${child.sid}`, "warn",
`bili-native-dsh: fork adoption for ${child.sid} skipped at the body-parse gate — the request body was not parseable JSON, so the replay cannot be projected (#4215)`);
return;
}
if (sideShapedBody(body)) {
noteForkGateOnce(`gate:side-shaped:${child.sid}`, "info",
`bili-native-dsh: fork adoption for ${child.sid} deferred for a side-shaped request (no tools, budget <= 200) — the adoption window stays open for the main request (#4215)`);
return;
}
const base = register.base;
if (base === undefined) return;
if (base === undefined) {
noteForkGateOnce(`gate:proxy-base:${child.sid}`, "warn",
`bili-native-dsh: fork adoption for ${child.sid} skipped at the proxy-base gate — the proxy origin is not known yet, so the fork protocol cannot be reached (#4215)`);
return;
}
await dshForkAdopter.maybeAdopt({ base, parent: child.parent, child: child.sid, body });
}

Expand Down Expand Up @@ -2417,8 +2513,9 @@ export function apply(ctx: PluginContext): void {
}

/** Test hook: reset the module-level registration lifecycle so suites can
* drive apply() repeatedly with a fresh mock ctx. */
export function _resetRegisterForTest(base: string | undefined): void {
* drive apply() repeatedly with a fresh mock ctx. (#4215: optional
* toolsReady so gate scenarios can arm the registration state directly.) */
export function _resetRegisterForTest(base: string | undefined, toolsReady = false): void {
// #3616: invalidate every in-flight recovery chain from earlier scenarios —
// their late landings must not write this scenario's register/state.
originGen += 1;
Expand All @@ -2433,7 +2530,7 @@ export function _resetRegisterForTest(base: string | undefined): void {
state.respawn = undefined;
state.onGiveUp = undefined;
register.base = base;
register.toolsReady = false;
register.toolsReady = toolsReady;
register.dead = false;
register.retryAt = 0;
register.pending = undefined;
Expand All @@ -2454,6 +2551,18 @@ export function maybeRetryForTest(ctx: PluginContext): void {
maybeRetry(ctx);
}

/** Test hook (#4215): drive the fork-adoption entry gate directly without
* arming apply() (the fetch patch is not installed under NODE_TEST_CONTEXT). */
export function _maybeForkAdoptBeforeSendForTest(ctx: PluginContext, init: RequestInit | undefined): Promise<void> {
return maybeForkAdoptBeforeSend(ctx, init);
}

/** Test hook (#4215): reset the fork-gate diagnostics throttle state. */
export function _resetForkGateDiagnosticsForTest(): void {
forkGateNoted.clear();
noticedForkBlindSpot = false;
}

/** Test hook (#2604): reset the host-title channel (dedup map + resolved
* services) so scenarios start un-armed. */
export function _resetTitlesForTest(): void {
Expand Down
Loading
Loading