Skip to content

fix: drop mid-chain adoption from anonymous prefix affinity (#1115) - #1163

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-22_drop-tail-window-reattach
Sep 23, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-22_drop-tail-window-reattach

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #1115

What changed

The anonymous prefix-affinity resolver's second matching path (tail-window reattach, #316) slid the incoming's leading 8 per-item hashes against any mid-chain offset of every tracked chain and adopted the stored session on a contiguous hit. A mid-chain contiguous match is symmetric evidence, not an ownership proof: it cannot distinguish "same conversation, truncated replay" from "a different conversation quoting shared content" (templated tool outputs, standard files, stock errors — parallel agents on one codebase produce identical mid-chain slices with no attacker involved). Adoption was bidirectionally harmful either way: the adopter's rebuilt wire received the victim session's folded summary blocks (leak), and the adopter's new turns were recorded into the victim's session (pollution). The #1072 interim mitigation (full-8 window instead of 3) closed sub-8 crafted windows but did not break the symmetry.

Per the issue's direction 3 (owner-approved in-thread), mid-chain adoption is removed entirely:

  • src/prefix-affinity.ts: step-2 scan deleted; via narrows to "prefix" | "new"; TAIL_WINDOW → TRUNCATION_LINEAGE_WINDOW = 8 used for lineage attribution only (leading run strictly inside a tracked chain at offset ≥ 1 → lineage.reason = "truncated" with all hit parents; shared leading prefix LCP ≥ MIN_FORK_PREFIX = 3 records "forked" and takes precedence). Lineage never affects matching.
  • src/server.ts: tail-window log branch deleted.
  • Tests: tests/tail-window-reconcile.test.ts → tests/truncation-fork.test.ts, rewritten (reattach assertions → fork assertions; ambiguous multi-parent case now asserts attribution with both parents; append-only / brand-new / templated-head regressions kept; kernel reconcile test kept). The 发现一些bug #1064 fix: cache health — remove condense, fix isTitleGen, preserve cache_control #13 crafted-window regression passes unchanged — mid-chain adoption no longer exists at any window size.
  • New design record SESSION-IDENTITY.md (+ .zh-CN) (requested in-thread): identity principles (会话身份不稳定:credential/upstream 维度导致长会话状态孤儿化(sessions 缺失) #286), why full-prefix matching stays sound, threat model, measurement data, rejected alternatives (suffix-anchored / depth-scaled / kernel-verified / tail-anchored-run+floor), three-layer identity framework, future client-side identity signal.
  • CHANGELOG.md entry.

Behavior change (disclosure)

Before After
Anonymous request whose replayed history is a truncated/rolling suffix of a tracked chain Reattached the stored session (via: "tail-window"; compression ladder continuous) Forks a fresh session (via: "new", lineage.reason = "truncated" recorded for UI/debug); ladder restarts on that truncation

Why: symmetric evidence ≠ ownership (threat model above), and three days of production logs on this daemon's deployment (~11.5k anonymous resolutions, 2026-09-19 → 2026-09-22) showed 0 tail-window reattaches and 0 near-misses — the removed path had no observed users. Cost accepted: one raw resend + ladder restart per truncation, the same cost class instruction-drift forks already accept (#1106); opt-in block adoption (#629, raw-id SET semantics) still carries blocks whose sources remain fully present in the replay, and kernel reconciliation of truncated replays stays sound (deactivate/revive pinned in tests).

Full-prefix matching (the sound path) is untouched. Client-provided ids are untouched. No config schema, persistence format, or cross-repo dependency changes.

Coordination

Pre-flight (on rebased head e28fdfa)

  • npm run typecheck — clean
  • npm test — 2245 pass / 0 fail / 2 skipped (E2E-gated)
  • npm run build — success

E2E (tests/e2e/e2e-codex.test.ts) not run locally: it needs a live upstream reachable from this environment (E2E_UPSTREAM_URL); the change touches only the anonymous-resolution branch of server.ts and the resolver, both covered by the unit suites above. Happy to trigger the manual-dispatch CI E2E if desired.

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-22_drop-tail-window-reattach (efb52fa)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1163

Each push to this PR publishes a new version under the pr-1163 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1163.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 complete — I pulled branch 2026-09-22_drop-tail-window-reattach (single commit 60eedc7), verified the code line-by-line against pre-change master (e28fdfa, which is current tip — merge-base = master, no stale base), re-ran the full pre-flight locally, and checked CI + coordination threads. Verdict: mergeable (human merge — session-binding logic stays human per §7.4 regardless of scope). No fixes were needed; nothing to commit.

Diff cleanliness

7 files, every one purpose-related: resolver, one log branch in server.ts, test rename+rewrite, a 1-line comment update in tests/prefix-affinity.test.ts, CHANGELOG entry, two new design docs. No whitespace churn, no lockfile/generated-file drift, no off-topic files. Clean.

Regression review — existing behaviors on touched paths

I read the pre-change code (not just the diff) and walked each behavior the touched paths carry:

Existing behavior Verdict
Anonymous full-prefix match (step 1, head-anchored) Preserved — byte-identical code path; pinned by the kept append-only regression test (via:"prefix", matchedDepth unchanged).
Client-provided identity (headers / body session_id / prompt_cache_key) Preserved — diff never enters that branch (if (!clientProvided) gate untouched).
Deterministic new-session id derivation (pfa-<sha256(tailHash\0depth)>) → same id after proxy restart Preserved — untouched.
Truncated replay resolution Intentional change, disclosed (PR body table + CHANGELOG): old = tail-window adoption, ladder continuous → new = fork + lineage.reason="truncated", ladder restarts once. Matches the owner-approved direction 3 in #1115 floor 10.
Fork lineage attribution Preserved with one disclosed nuance: old code recorded "truncated" only in the ambiguous multi-candidate case and gave it precedence over the fork scan; new code gives "forked" (LCP ≥ 3) precedence over "truncated". UI/debug-only metadata, never affects matching — and arguably more accurate (a request sharing a ≥3 leading prefix is a fork, not a truncation). Disclosed in the PR body ("takes precedence").
#629 fork block-adoption boundary Preserved, correctly bounded: still fires only on via==="new" && reason==="forked" (src/server.ts:1516). Truncated replays did NOT trigger it before (they adopted via tail-window with no lineage) and do NOT trigger it now (reason==="truncated") — the explicit non-goal from #1115 (no suffix-evidence block handover) is honored.
Kernel reconcile on truncated replays (block deactivate/revive) Preserved — kernel test carried over verbatim into tests/truncation-fork.test.ts.
note() / TTL / eviction / exportSnapshot() persistence Preserved — untouched.
Persisted session.metadata.anonymousPrefixAffinity No schema break: pre-upgrade sessions may still carry via:"tail-window"; I grepped all consumers — src/server.ts:2195 is a truthiness check only, no web-UI reference, nothing branches on the value. Inert legacy data.
Production log string reattached via tail-window Gone after upgrade (expected). The #1115 measurement recipe becomes moot post-upgrade — fine, the capability is gone by design.
Complexity on the anonymous hot path Not worsened: the new lineage scans (LCP + containsRun) run on the same unresolved-request set where the old step-2 window scan ran, same O(chains × depth × w) class (≤256×128×8 hash compares worst case).

Code-level checks: containsRun enforces offset ≥ 1 correctly (haystack.length < w+1 → false, loop starts at j=1); w >= MIN_FORK_PREFIX gate keeps sub-3 inputs out of truncated attribution (same threshold semantics as old MIN_TAIL_MATCH); zero dangling tail-window/TAIL_WINDOW references in src/ or tests/ (comments only); header invariant comment corrected to "byte-identical HEAD" (the old wording was exactly what #1115 called out as overclaiming).

Pre-flight (independently re-run on 60eedc7)

  • npm run typecheck — clean
  • npm test — 2245 pass / 0 fail / 2 skipped (E2E-gated) — matches the PR's claim exactly
  • npm run build — success

CI & coordination

One flag — merge ordering (not a blocker)

Open PR #1150 (the #1148 fix, strip leading system/developer from identity) also touches src/prefix-affinity.ts — its hunk lands in the same header-comment block this PR rewrites, plus adds stripLeadingSystem near hasUserMessage. Expect a textual conflict on whichever lands second; I read both diffs and there is no semantic collision (step-1 input normalization vs step-2 deletion are orthogonal; #1150's tests assert step-1 behavior this PR doesn't touch). Suggestion: merge this PR first (already fully green), then rebase #1150 onto the new master. Several other open PRs touch src/server.ts, but this PR's server hunk is a 3-line log deletion — trivially rebasable either way.

中文摘要:核对了分支代码(单提交 60eedc7,基于最新 master e28fdfa)、逐项回归审查了受影响路径的既有行为(全前缀匹配/客户端身份/确定性会话 id/#629 边界/kernel reconcile/持久化全部保持,截断重放改 fork 为已披露的有意变更,lineage 优先级微调仅影响调试元数据)、本地独立重跑 typecheck/test(2245 pass)/build 全绿、CI 全绿且含零 token E2E 预检,#1072 协调注释已确认在位;唯一提醒是 #1150 与本 PR 在同一文件有文本冲突(无语义冲突),建议先合并本 PR 再 rebase #1150——本 PR 可以合并。

Tail-window reattach (#316) adopted a stored session when the incoming's
leading 8 per-item hashes hit ANY mid-chain offset of a tracked chain. A
mid-chain contiguous match is symmetric evidence, not an ownership proof: it
cannot distinguish 'same conversation, truncated replay' from 'a different
conversation quoting shared content' (templated tool outputs, standard files,
stock errors - parallel agents on one codebase produce these naturally), and
adoption was bidirectionally harmful (folded-summary leak to the adopter,
context pollution of the victim). The #1072 interim mitigation (full-8 window
instead of 3) closed sub-8 crafted windows but not the symmetry.

Three days of production logs (~11.5k anonymous resolutions) showed zero
tail-window reattaches and zero near-misses, so mid-chain adoption is removed
entirely (direction 3 of #1115): truncated replays now fork into a fresh
session. The truncated-run scan survives as lineage attribution only (never
matching): leading TRUNCATION_LINEAGE_WINDOW=8 run strictly inside a tracked
chain records lineage.reason='truncated' with all hit parents; a shared
leading prefix (LCP >= MIN_FORK_PREFIX=3) records 'forked' and takes
precedence. via narrows to 'prefix' | 'new'; the server tail-window log
branch is deleted.

Cost accepted: one raw resend + ladder restart per truncation (same cost class
as instruction-drift forks, #1106); opt-in block adoption (#629) still carries
fully-present blocks, kernel reconcile stays sound (deactivate/revive pinned).

Tests: tail-window-reconcile.test.ts renamed truncation-fork.test.ts and
rewritten (reattach assertions -> fork assertions; ambiguous multi-parent case
asserts attribution with both parents). New design record SESSION-IDENTITY.md
(+ .zh-CN): identity principles, threat model, measurement, rejected
alternatives, three-layer framework, future client-side identity signal.
@ranxianglei
ranxianglei force-pushed the 2026-09-22_drop-tail-window-reattach branch from 60eedc7 to efb52fa Compare September 23, 2026 04:06
@ranxianglei
ranxianglei merged commit 41c7f6a into master Sep 23, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-22_drop-tail-window-reattach 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.

Design: prefix-affinity tail-window reattach adopts sessions on mid-chain evidence — ownership proof needs a redesign, not a bigger threshold

1 participant