Skip to content

[PR] dsh: negotiated session recall for unattributed retries (#4082) - #4094

Merged
ranxianglei merged 7 commits into
masterfrom
2026-10-21_conversation-recall
Oct 21, 2026
Merged

ranxianglei merged 7 commits into
masterfrom
2026-10-21_conversation-recall

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #4082 (the bili-side half; the host-side ALS gap itself stays tracked in #2975).

What this does

dsh retries and model switches re-send model traffic from execution chains that never entered an agent turn, so our AsyncLocalStorage attribution reads none and the interceptor falls to directSendOrRefuse — the session loses compression, guardrails, and fold state (#3918, #4068, #2975). dsh cannot accept PRs for the host-side fix, so this adds a negotiated identity recall that needs zero host changes:

  1. Learn (attribution healthy): every attributed dispatch records (lane key → session id, boundary fingerprint) in a small LRU/TTL table. The lane key is origin+path+authorization-hash — deliberately without model, because a model switch on the same history is exactly the shape we must recognize. The boundary fingerprint is the sha of the last message of that dispatch.
  2. Recall (attribution lost): when the takeover gate is about to refuse an unattributed dispatch, the body's own history is checked against the table. A claim requires the stored boundary to be present in the resent history and uniquely matching (two live sessions on one lane → ambiguous → refuse, keep old behavior). A claimed request is routed onto the plugin lane with the negotiated conversation id stamped by the existing beforeSend seam.
  3. Fork safety: a forked history (common prefix then divergence) does not contain the parent's latest boundary in the required position, so forks keep refusing — they are new sessions. Verified by test.
  4. Honest health signal: a negotiated claim deliberately does not clear the attribution-loss streak — the initiator scope is still broken; the request is rescued, not the attribution. The panel keeps showing the host-side problem until dsh fixes it.
  5. Protocol by-product: every 2xx response now carries x-bili-session: <sid>. Cooperating clients can carry the id themselves (fetch response → later fetches), adopting stable identity without any bridge. The dsh recall does not depend on this header.

Limits

  • Recall evidence is per-lane and boundary-based: same-lane parallel forks that share the exact latest message are indistinguishable → ambiguous → refuse (safe direction).
  • A wrong claim is contained by the existing reconcile drift isolation (blocks snooze, nothing is deleted).
  • 8 sessions per lane / 10-minute TTL / 64 lanes; no new env knobs.

Verification

  • tests/conversation-recall.test.ts 5/5 — continuation claim, model-switch + fork shapes, ambiguity rejection, TTL expiry, LRU eviction.
  • tests/dsh-conversation-recall.test.ts 2/2 — end-to-end wiring: attributed turn learns; unattributed retry with the same history is claimed and stamped; fork history never stamps.
  • Regression: native-intercept 64/64, fix-1117 8/8, issue1158, cache-friendly 5/5, issue1839 3/3, incremental-lane 24/24 (response header breaks no goldens).
  • tests/dsh-native.test.ts cannot run in this sandbox (attach-registration timeouts — reproducible on clean master; CI runs it green).
  • tsc clean; unused-exports / lifecycle-ledger / config-surface gates clean.

Host-driven retries, model switches and post-resume resends run outside
the agent execution chain, so the AsyncLocalStorage initiator is missing
and the takeover gate refused them into raw DIRECT sends — compression
off, guards off, permanently for the session (#3918, #2975).

While attribution works, the shim learns lane -> (sid, boundary
fingerprint of the request's last message). When attribution is later
missing on the same lane, a dispatch whose history CONTAINS a remembered
boundary (uniquely) is provably that session's continuation: the gate
claims it and beforeSend stamps the negotiated conversation header
atomically with the body (#3370 discipline). Fork divergence lacks the
boundary — never claimed; no evidence or ambiguity keeps today's direct
behavior byte-for-byte. Every proxied 2xx now carries x-bili-session
(protocol surface for cooperating clients, PLUGIN.md).
@ranxianglei ranxianglei changed the title dsh: negotiated session recall for unattributed retries (#4082) [PR] dsh: negotiated session recall for unattributed retries (#4082) Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-21_conversation-recall (bd3ca62)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4893-pr.4094.8d371f5

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-pr4094.tgz
npm install -g package

Install instructions are refreshed on each push.

Tokens rotate (bearer refresh, key swap) — an auth-keyed lane orphans the
table on the exact retry it exists to rescue. The lane is only the search
namespace; identity evidence stays the boundary fingerprint + uniqueness,
so widening the lane trades a rare cross-account ambiguity (refused, safe)
for a bridge that survives credential rotation.
The LlmRuntime stamps options.sessionId on every dispatch and documents
the llm/stream waterfall as the interception point. Wrap each stream's
iteration in an AsyncLocalStorage keyed by that sid so adapter fetches
read attribution that follows the request, not the async chain — retry
chains, agentless side-calls (compaction, session-title), disposed
initiator scopes, and host runtime gaps all stay attributed (#2975,
#3918, #4082). Attribution order: llm/stream sid first, ambient
currentInitiator fallback, the negotiated recall bridge (#4094) stays
as the last resort. Agentless dispatches without a sid pass through
untouched.
awork and others added 3 commits October 21, 2026 12:52
…ite, stamp rebuild (#4082)

Four non-behavioral fixes from the independent #4082 review of this PR:

- recallLaneKey JSDoc still described the pre-c8960a0e auth-keyed lane;
  rewritten to match the implemented origin+pathname-only key.
- maybeNegotiatedClaim now probes lane existence before parsing/hashing the
  body: agentless sidecars (title-gen etc.) hit the none path constantly,
  and multi-MB quota-retry bodies must not pay full JSON.parse plus
  per-message sha256 when no session was ever learned on that lane.
- learn+stamp moved before fork-adopt/incremental in beforeSend: the
  boundary fingerprint must come from the ORIGINAL full history, not from
  bodies those two steps rewrite (the incremental delta happens to end with
  the newest message today; learning must not rest on that shape).
- The negotiated re-stamp rebuilds init.headers via new Headers(...)
  instead of mutating the host-provided headers object in place (#3370
  discipline): an in-place stamp could leak the negotiated identity into
  later unclaimed requests if the host reuses its headers across retries.

Observable behavior unchanged on every path; wiring test assertions read
through a Headers view to match the rebuilt stamp.
…bution

[PR] dsh: session identity rides the request object (llm/stream attribution, #4082)
@ranxianglei
ranxianglei merged commit 496f05e into master Oct 21, 2026
65 of 66 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.709-master.496f05e (rolling master tag).

npm install -g billion-context@0.0.709-master.496f05e  # 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.

This was referenced Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant