Repository navigation
fix: strip cache_control from prefix-affinity identity hashing (#2188) - #2191
Conversation
Claude Code stamps a cache_control breakpoint on its last message + last assistant message and moves them as the conversation grows. The prefix-affinity identity chain hashed those bytes via stableStringify, so a --resume/--fork replay of an earlier history diverged from the parent's stored chain exactly at the breakpoint position and findResumeParent returned null deterministically — every Claude Code resume/fork lost all compression inheritance (#1486/#1834/#1850). Add canonicalForHash: sorted-key JSON with every cache_control field dropped, used by both chainHashes and perItemHashes. The identity chain is now invariant to breakpoint placement while still requiring actual content to match byte-for-byte. Behavior change (one-time): pre-upgrade chains persisted in prefix-affinity.json were hashed WITH cache_control, so they no longer match new-format incoming hashes and fork fresh once; each session converges after its next live request re-notes its chain (self-healing, no persistence-format change).
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2259-pr.2191.c0c1c35This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2191.tgz
npm install -g packageInstall instructions are refreshed on each push. |
#2191 review 完成。结论先行:可合并,需人工合并——改动触及 identity/session-binding 逻辑,按 §7.4 必须 human merge;质量上无拦点。 查重:open+closed issue/PR 检索 cache_control / prefix-affinity / resume-inheritance 关键词,无重复项;#1486/#1834/#1850 是 resume-inheritance 功能本身的工单,#2189 是独立的 preflight 问题,均与本修复不重叠。 审核证据(不只读 diff):
七问:
顺手小修(独立标注 commit,非本 PR 引入): diff 整洁度:2 文件 +87/-2 单一线性提交,无 lockfile/golden/生成物漂移,每行都与目的相关(外加上面 1 行注释小修)。干净。 判定:可合并,请人工 merge(identity-logic 变更按 §7.4 留给人)。合并后建议观察一次真实 CC resume/fork,确认日志出现 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.55-master.39c4958 # this exact build
npm install -g billion-context@master # always the newest merged build
|
What
findResumeParentrequires the child request's chain hash to equal the parent's storedtailHashat the parent's depth. That chain was hashed withstableStringify(message)— which includes each content block'scache_control. Claude Code stamps acache_controlbreakpoint on its last message and last assistant message and moves them as the conversation grows, so a--resume <parent> --fork-session --session-id <child>replay of the parent's history differs from the parent's final live request only by where those hints sit. The shared prefix was therefore never byte-stable across a breakpoint shift, andfindResumeParentreturnednulldeterministically — every Claude Code resume/fork started at zero compression blocks (#1486 / #1834 / #1850 never fired).Root cause & fix
src/prefix-affinity.ts: addcanonicalForHash— sorted-key JSON with everycache_controlfield dropped — used by bothchainHashesandperItemHashes. The identity chain is now invariant to breakpoint placement while still requiring actual content to match byte-for-byte. All three consumers route through these two functions, so anonymous resolution (#309), identified-session tracking (#1486), and resume-fork detection all inherit the fix; no other code path re-implements this hashing (fork-adoption.tsadopts kernelCoreMessages and relies on the resolver's match).stableStringifyis unchanged and stays exported (still used bysrc/tool-ring.tsfor tool-call argument canonicalization).Behavior change (flagged per policy)
One-time, self-healing: chains persisted in
~/.local/state/billion-context/prefix-affinity.jsonby pre-upgrade bili were hashed withcache_control; after this change they no longer match new-format incoming hashes, so such sessions fork fresh once and re-converge after their next live request re-notes the chain. No false positives are possible (a stale entry would need a sha256 collision to wrongly match). No persistence-format / schema change — I chose "accept one-time invalidation" over adding a hash-version field (the latter is a human-gated persistence change); if versioning is preferred it can be a follow-up.Tests
tests/prefix-affinity.test.ts— two new regression tests pinning the mechanism:cache_controlplacement (same transcript, breakpoint on different trailing blocks → identicaltailHash+itemHashes).cache_controlbreakpoint shift (parent tail carries breakpoints on idx 8/9; child replay clears them and extends →findResumeParentstill resolves the parent at full depth).prefix-affinity,affinity-persist, andresume-ref-inheritancesuites pass; typecheck clean. Full suite left to CI.Closes #2188