Skip to content

fix: make prefix-affinity chains permanent (#1724) - #1749

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-30_affinity-permanent
Sep 30, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-30_affinity-permanent

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Follow-up to the #1724 LRU review: the 7-day TTL contradicted the month/year-level single-session promise — a chain idle over a week lost recognition and the resumed conversation forked into a fresh session.

  • Chains are now permanent: they leave the table only under LRU capacity pressure (cap raised 256 → 1024, ≈8MB worst case).
  • Dropped the TTL gate in peekChain() and the sweep in resolve()/findResumeParent(); lastSeen stays as the LRU/freshness tie-break.
  • importSnapshot() no longer drops aged entries — a 400-day-old chain still reattaches; malformed-entry defenses unchanged.
  • Note for fix: multi-instance shared-state hardening — activePack round-trip + affinity clobber guard (#1724) #1737: normalizeForWrite should drop its TTL/256 re-application on rebase; the union + deeper-depth-wins merge survives.

Verification: typecheck clean; affinity-persist + prefix-affinity suites 29/29.

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-30_affinity-permanent (82d97ae)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1720-pr.1749.350dcff

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-pr1749.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] 🏷 Picked up. This is a completed-work PR — verifying per protocol: fetching branch 2026-09-30_affinity-permanent @ 303e95f, checking CI status, walking the diff against master, and re-running typecheck + the affinity suites locally. Will report conclusions shortly.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review of PR #1749 (head 303e95f1) — verified, mergeable.

① Issue direction. Follow-up to the #1724 LRU review: the 7-day TTL was the mechanism producing "chain idle >1 week loses recognition → resume forks a fresh session" — it directly contradicted the month/year-level single-session promise. The fix removes that mechanism (constant + sweep + gates) rather than masking the symptom, and keeps lastSeen purely as the LRU/freshness tie-break. Direction is right.

② Regression walk — existing behaviors of the touched paths:

  • Chain validity: old = expired after 7 days idle (TTL gate in peekChain(), sweep in resolve()/findResumeParent(), drop in importSnapshot()); new = permanent, leaves the table only under LRU capacity pressure. Intended change — disclosed below.
  • Table cap: 256 → 1024; worst case ≈2MB → ≈8MB (1024 × ≤128 hashes × 64B). Bounded and documented; strictly more retention than before.
  • Persistence: prefix-affinity.json format unchanged (version:1, same fields incl. lastSeen) — no migration; file size grows proportionally with chain count but stays bounded by the cap; debounced atomic tmp+rename write untouched.
  • Append-only discipline (detectUnannouncedHistoryRewrite 误判 1 条消息的侧边辅助请求(如 WebSearch),导致 Ref 映射表被洗空、编号雪崩耗尽(m99999)及 363 万 Token 溢出 #1075/Claude 上 resume 导致消息错乱 #1486, src/server.ts:2082-2086): peekChain() now returns aged entries too, so a short side-request against a chain idle >7 days is correctly refused instead of clobbering the tracked chain — consistent with permanence, intended semantics.
  • Matching logic in resolve()/findResumeParent(): byte-identical apart from the removed expire() call (deepest-match-wins, lastSeen tie-break preserved).
  • LRU eviction (note() + importSnapshot tail loop): unchanged; note() still stamps lastSeen: Date.now(). importSnapshot count semantics unchanged (well-formed entries counted, LRU trim after).
  • Malformed-entry defenses: all field-type checks in importSnapshot intact.
  • Wire/message shape, config schema, cross-repo deps, src/update.ts, version field: untouched. Both compression modes unaffected (resolution is mode-independent).

③ Code. Minimal and clean: no leftover TTL_MS/expire(/256 references anywhere in the four touched files (grep-verified); comments updated with rationale citing #1724; memory math stated. No as any/@ts-ignore, no console noise.

④ Tests. The two suites have teeth: LRU-cap test drives 1030 distinct conversations and asserts exactly 1024 tracked; import test asserts a 400-day-old entry imports (count=1) while malformed entries (non-string sessionId, non-number depth, null) still yield 0. Deterministic (Date.now() arithmetic with large offsets, no env luck). Minor gap (non-blocking): no direct unit pinning peekChain() returning an aged entry — the server-side append-only consequence rides on the resolver contract tests.

⑤ Diff cleanliness. Single commit, 4 files, every line serves the purpose; no whitespace/lockfile churn. version-guard passed (no version bump on a content branch).

Behavior change disclosure (old → new):

⚠️ Dependency note for #1737 (confirmed): on branch 2026-09-30_multi-instance-store-hardening, normalizeForWrite (src/affinity-persist.ts:77-82) still filters now - e.lastSeen <= TTL_MS and slices to 256 on every write. If #1737 lands without rebasing onto this change and dropping those two lines, every debounced flush would silently strip aged chains from disk — reintroducing exactly the bug this PR fixes, one flush later. Keep the union + deeper-depth-wins merge, drop the TTL/cap re-application.

Verification reproduced locally on the PR head: typecheck clean; tests/prefix-affinity.test.ts + tests/affinity-persist.test.ts 29/29 pass. CI: all 12 check-runs on 303e95f1 completed green (ubuntu/windows 22+24 test matrix, all four real-client e2e lanes, hermetic registry, version-guard, build-artifact, codex overlay).

Verdict: 本 PR 可以合并。


中文摘要:修复了前缀亲和链 7 天 TTL 与「月/年级单会话」产品承诺矛盾的问题(闲置超一周的会话恢复时丢失识别、分叉出新会话)——移除 TTL 门控与清扫、链改为永久(仅在容量压力下按 LRU 淘汰,上限 256→1024),快照格式不变;已本地复验(typecheck 干净、两套件 29/29)并确认 CI 12 项全绿,注意 #1737 rebase 时必须同步去掉 normalizeForWrite 里的 TTL/256 重放,否则会在下次落盘时把本修复静默打回,可以合并。

The 7-day TTL contradicted the month/year-level single-session promise:
a chain idle over a week lost its recognition, forking the resumed
conversation into a fresh session on the next request. Chains are now
permanent — they leave the table only under LRU capacity pressure
(raised 256 -> 1024, ~8MB worst case):

- drop the TTL gate in peekChain() and the sweep in resolve()/
  findResumeParent(); lastSeen stays as the LRU/freshness tie-break
- importSnapshot() no longer drops aged entries (400-day-old chains
  reattach); malformed-entry defenses unchanged
- note for #1737: its normalizeForWrite TTL/256 re-application should
  be dropped on rebase; the union + deeper-depth-wins merge survives
@ranxianglei
ranxianglei force-pushed the 2026-09-30_affinity-permanent branch from 303e95f to 82d97ae Compare September 30, 2026 13:04
@ranxianglei
ranxianglei merged commit 93d5043 into master Sep 30, 2026
12 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-30_affinity-permanent branch October 15, 2026 05:32
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.

1 participant