Skip to content

feat: simhash chain-alignment adoption for anonymous affinity (#2265) - #2266

Merged
ranxianglei merged 6 commits into
masterfrom
2026-10-06_affinity-simhash-recovery
Oct 6, 2026
Merged

ranxianglei merged 6 commits into
masterfrom
2026-10-06_affinity-simhash-recovery

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

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.ts resolves 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, and note() 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)

Guard Rejects
coverage ≥90% of aligned positions, none beyond Hamming 32 different/divergent conversations
pervasive gate: ≥20% positions mutated-but-similar POINT edits (edit-and-resend fork mints its own session — #629 contract intact)
ownership anchor: ≥1 byte-identical user message different conversations sharing harness boilerplate (found via tests/fork-adoption.test.ts: mock scenarios byte-share assistant replies; user words never repeat)
unique candidate only ambiguity — refuse to guess (#1692 discipline)
identified chains never adopted; 24h sketch TTL; ≤64 chains carry sketches; kill-switch BILI_AFFINITY_SIMHASH=0 —

Cache 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).

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
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-06_affinity-simhash-recovery (e6acedb)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2369-pr.2266.0c2100c

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2266.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review started. Pulling the branch (2026-10-06_affinity-simhash-recovery, head 11794a4) onto current master and walking the four-piece set (src/affinity-simhash.ts, src/prefix-affinity.ts, tests + docs) through the review gates. Will report back with the structured verdict.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

PR #2266(simhash 链对齐采纳)审完,含你刚并发推的 3 个后续提交(41d5b293c config 键、d3ddb0e69/51b351c6a 文档)。核心机制逐行读过(src/affinity-simhash.ts、src/prefix-affinity.ts、server.ts 接线,以及与下游 fold-reconcile(#1921)的组合关系),并在当前 live head 51b351c6a 复验:typecheck 干净 · 亲和族 89/89 + 配置优先级 33/33 · build 成功。

七问结论:

  1. 是什么问题 — Trae 切模型时给每条助手消息重打模型标签,精确哈希链在首个被改写消息处断裂,每个请求新开 pfa-* 会话、折叠态孤立、~1.4M token 历史反复从零重折(实测 ~900K 浪费)。本 PR 在身份阶梯补第②级(simhash 链对齐)治它。
  2. 什么原因导致的 — 根因:匿名前缀亲和只认「逐字节精确前缀」,对装饰性字节改写零容忍,一处改写即断链即开新会话。修法在会话选择层引入受控近似匹配(逐条 64-bit simhash + 汉明门限),把「同一会话被装饰性改写」与「真分叉/异会话」分开。
  3. 怎么修复的 — resolve() 在精确命中与全新开会话之间插入 simhashAdopt:来消息草图与某已跟踪链逐位对齐且过四道门(覆盖≥90%、无位置汉明>32、≥20% 位置「改而相似」、≥1 条字节级一致 user 锚点、候选唯一)才判同会话→保留既有会话及压缩态;note() 把已存链重锚到被改写字节,使下次请求回到精确快路径(自愈)。风险不对称:任何疑义一律回退开新会话,绝不并两个会话。折叠块级状态存活由既有 Fold-state reconciliation: classify resent-history divergence (edit/fork/new/system-only) and make compression blocks re-anchorable chain units #1921 fold-reconcile 承担,本 PR 只负责选对会话。
  4. 是否有回归问题 — 逐项:精确快路径不变(itemHashes 仍是同样 sha256);identified 链从不带草图/不参与采纳;截断(incomingDepth<entry.depth)跳过→照旧 fork;两种压缩模式皆安全(仅匿名路径,转发上游 wire 字节原封不动,缓存红线守住);Design: tree-shaped prefix affinity — forked branches inherit the shared prefix's compression state #629 单点编辑 fork 契约完整保留(单点编辑在所有深度都被 pervasive 门拒)。未发现回归。
  5. 是否破坏缓存 — 不破坏。采纳只改会话选择、不改转发内容;判定对给定(来消息,已跟踪态)是确定的。唯一时间依赖是草图 24h TTL,属保留策略而非正确性约束。
  6. 是否改了设计方向 — 架构未转向,仍是身份阶梯,只是补齐第②级;数据流不变。新增配置面 affinitySimhash(file 布尔)+ BILI_AFFINITY_SIMHASH(env),按 彻底整治环境变了滥用,除非必要都走配置文件 #2030 走 loadOptions(env>file>默认 ON)、登记进 KNOWN_TOP_LEVEL_KEYS、en+zh+website 文档齐备——比我最初读到的 env-only 版本更正确。
  7. 是否引入了新的配置 — 是:affinitySimhash(默认 true/ON)+ BILI_AFFINITY_SIMHASH(0 关该级)。默认 ON 保行为连续、无迁移成本;0/false 回退到 Trae http改URL模式:切模型改写历史 → 匿名身份链每请求断裂 re-mint → 1.4M 全量入站 + 重复折叠死循环 #2265 之前的「断链即开新会话」。

两条非阻塞观察(不影响合并):

关于我一开始标记的文档缺口:我开始准备一个 CONFIGURATION.md 补丁时,看到你并发的 3 个提交已把它更好解决(升级成 config 键 + 规范再生文档),故我那个补丁作废丢弃,无需再动。

判定:可合并(人工合并——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
@ranxianglei
ranxianglei merged commit ea488cf into master Oct 6, 2026
25 checks passed
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.80-master.ea488cf (rolling master tag).

npm install -g billion-context@0.0.80-master.ea488cf  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trae http改URL模式:切模型改写历史 → 匿名身份链每请求断裂 re-mint → 1.4M 全量入站 + 重复折叠死循环

1 participant