Repository navigation
feat: simhash chain-alignment adoption for anonymous affinity (#2265) - #2266
Conversation
A client-side pervasive decorative rewrite (Trae re-stamps model tags on every assistant message when the user switches models) breaks the exact hash chain on every request: the anonymous resolver mints a fresh session each time, the fold state is orphaned, and the full 1.4M-token history is re-sent and re-folded from zero — repeatedly (#2265). Ladder rung between the exact-prefix match and the fresh mint: per-message 64-bit simhash sketches (bigram shingles, FNV-1a dual-seed). When every aligned position is within Hamming 10 of a tracked chain, the rewrite is recognized as the SAME conversation and the existing session (and its compression state) is kept; note() re-anchors the stored chain on the mutated bytes so the next request resolves by exact prefix again. Guards (each backed by a test): - coverage >= 90% of aligned positions, no position beyond Hamming 32 - pervasive-rewrite gate: >= 20% of positions mutated-but-similar — a POINT edit (edit-and-resend fork) mints its own session (#629 intact) - ownership anchor: >= 1 byte-identical USER message — harness machinery (tool calls, markers, even boilerplate replies) can repeat across DIFFERENT conversations; the user's own words never do - unique candidate only — two passing chains refuse to guess - kill-switch BILI_AFFINITY_SIMHASH=0, 24h sketch TTL, <=64 chains with sketches, identified chains never adopted, defensive snapshot import
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2369-pr.2266.0c2100cThis exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2266.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 Review started. Pulling the branch ( |
PR #2266(simhash 链对齐采纳)审完,含你刚并发推的 3 个后续提交( 七问结论:
两条非阻塞观察(不影响合并):
关于我一开始标记的文档缺口:我开始准备一个 判定:可合并(人工合并——feat 且触配置面,自动合并门本就不适用)。当前 head 与 master 落后的 3 个提交(output-clamp 校准)文件不相交,可干净 rebase/合并。 |
…taches the anonymous session (coverage 100%), compression state preserved, self-heals to the exact-prefix fast path on the next request
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.80-master.ea488cf # this exact build
npm install -g billion-context@master # always the newest merged build
|
Closes #2265. Implements the simhash chain-alignment rung (ladder rung ②, design in the issue's 6016022647 comment) for the anonymous prefix-affinity resolver.
Problem
A client-side pervasive decorative rewrite — Trae re-stamps a model tag on every assistant message when the user switches models — breaks the exact hash chain on every request. Each request mints a fresh
pfa-*session, orphaning the fold state: the full ~1.4M-token history is re-sent raw and re-folded from zero, repeatedly (~900K tokens of wasted folding measured in the wild, #2265).Mechanism
src/affinity-simhash.ts: per-message 64-bit simhash (word bigram shingles, FNV-1a dual-seed features, uniform stride sampling to 256, LRU memo by item hash).src/prefix-affinity.tsresolves in a new rung between the exact-prefix match and the fresh mint: if every aligned position of the incoming list is within Hamming 10 of a tracked chain's sketches, the rewrite is recognized as the SAME conversation — the existing session and its compression state are kept, andnote()re-anchors the stored chain on the mutated bytes so the next request resolves by exact prefix again (self-healing back to the fast path).Guards (each pinned by a test)
BILI_AFFINITY_SIMHASH=0Cache red line
Adoption only affects session selection — the wire bytes forwarded upstream are exactly what the client sent, untouched. The judgement is deterministic per (incoming, tracked) state.
Tests
tests/prefix-affinity-simhash.test.ts(17 cases): simhash math, adoption + self-heal back to fast path, tail-append, reverse rewrite, ambiguity refusal, kill-switch, TTL, shallow chains, persistence roundtrip, malformed sketches degradation, sketch retention window, identified exclusion, point-edit fork non-adoption (#629), shared-boilerplate different-conversation non-adoption. All affinity-family neighbors green (79/79 incl. fork-adoption/resume-ref-inheritance/issue1333/dsh-persona ×2/prefix-affinity/affinity-persist). typecheck clean.Ladder context: ① exact chain (existing fast path) → ② simhash alignment (this PR) → ③ template self-learning (L2, needs the reporter's INCOMING dump) → ④ stable marker echo (L3, Trae already replays 13 ACP marker lines verbatim — foundation exists).