Skip to content

feat(sglang): support session-attributed KV events - #14144

Closed
ishandhanani wants to merge 12 commits into
mainfrom
idhanani/dyn-session-kv-events-sidecar
Closed

ishandhanani wants to merge 12 commits into
mainfrom
idhanani/dyn-session-kv-events-sidecar

Conversation

@ishandhanani

Copy link
Copy Markdown
Contributor

This PR completes SGLang support for session-attributed KV events in Dynamo by forwarding canonical agent session IDs through the native sidecar and decoding SGLang's typed BlockStored metadata. This lets the session prefix indexer join route-time shared-prefix matches with newly stored SGLang tails while preserving legacy vLLM positional events.

Stacked on #13807, which is stacked on #13806. The engine-side event metadata is provided by sgl-project/sglang#37482; retarget this PR to main after the stack lands.

How This Was Implemented

  • Lower PreprocessedRequest.agent_context.session_id into SGLang's top-level session_id for native gRPC and native HTTP requests.
  • Distinguish SGLang's position-seven metadata map from legacy vLLM lora_name, then map cache_salt and session_id into canonical Dynamo event fields.
  • Add byte-level positional fixtures and a two-session lineage regression covering one shared prefix with two attributed divergent tails.
Walkthrough

Mental model

The request path supplies the triggering session to SGLang, and the event path returns that session with newly stored blocks. Dynamo keeps physical residency in its existing KV indexers and uses the optional logical session index only to associate the shared route-time prefix with the attributed tail.

flowchart LR
    A["agent_context.session_id"] --> B["SGLang sidecar request"]
    B --> C["SGLang BlockStored metadata"]
    C --> D["RawKvEvent"]
    D --> E["RouterEvent.session_id"]
    E --> F["SessionPrefixIndexer"]
Loading

State and precedence

For native HTTP, a canonical agent_context.session_id overwrites an opaque extra_args.sglang_tito.session_id; when canonical context is absent, the opaque native field remains untouched. Native gRPC writes the canonical value directly into GenerateRequest.session_id.

SGLang extends its positional BlockStored tuple at position seven with a typed metadata map. The deserializer uses an untagged enum to distinguish that map from the legacy positional vLLM string, preserving the existing vLLM trailing-field parser while mapping SGLang cache_salt to cache_namespace and retaining session_id through the canonical event conversion.

Request lifecycle

A route-time match records the deepest shared G1/G2 block for the session in the base PR. A resident SGLang cache hit emits no duplicate store event; when that request creates a tail, SGLang emits the shared parent hash plus new blocks with the triggering session, and Dynamo advances only that session's logical frontier.

Boundaries and limitations

This PR does not change block hashing, physical residency, BlockRemoved, remote-indexer behavior, or SGLang session ownership. It does not bump Dynamo's SGLang pin; that follows an upstream SGLang release containing sgl-project/sglang#37482.

Validation

  • cargo test -p dynamo-kv-router sglang_ --lib — 5 passed.
  • cargo test -p dynamo-sglang-sidecar session_id — 2 passed.
  • cargo test -p dynamo-llm stored_events_update_session_lineage --lib — 1 passed.
  • cargo test -p dynamo-kv-router session_prefix_index --lib — 18 passed.
  • cargo check -p dynamo-kv-router --all-targets --features standalone-indexer and cargo check -p kvbm-consolidator --all-targets — passed with pre-existing dead-code warnings only.
  • Live one-L4 smoke test with Qwen/Qwen3-0.6B: raw SGLang ZMQ emitted four block hashes with session_id=session-e2e-001; replay produced #cached-token: 64, Dynamo reported overlap_blocks=4, and both requests returned HTTP 200.

karen-sy and others added 12 commits September 1, 2026 14:52
Add a logical, per-session prefix index that records the block-hash
lineage each session has matched. The physical indexers drop blocks when
the engine evicts them, so session prefix history disappears with the
cache; this arena keeps the logical chain independently of the physical
cache state.

The index is off by default behind --enable-session-prefix-index. When
enabled, the router derives it inside the existing constructor from the
KvRouterConfig it already receives and feeds it once per lookup with the
deepest block that lookup matched.

update_session_from_stored_blocks is implemented and unit-tested but not
yet wired: the KV event schema carries no session id, so there is no
honest producer for it today.

Signed-off-by: [email protected] <[email protected]>
Review of the session prefix indexer surfaced two defects and three
smaller gaps. This addresses all five.

Unbounded retention. `remove_session` had no production caller, so the
index grew for the life of the process. Both halves of the fix are
needed because both signals are optional: the router now releases a
session when a request carries `session_final` or the `evict_session`
KV hint, and the indexer caps how many sessions it tracks
(`DEFAULT_MAX_SESSIONS`, overridable via `with_max_sessions`), evicting
the least recently used one by exactly the path `remove_session` takes.

Cyclic parent grafts. `validate_chain` only compared a block against its
recorded parent, so a chain grafting an ancestor under its own
descendant was accepted and closed a parent cycle -- every later walk of
the forest then looped forever holding the write lock. Validation now
carries the dominator set of the graft point, which also catches a chain
that revisits a node it created earlier in the same update. Rejection
still happens before the arena is touched, so no chain half-applies. The
two parent walks are additionally bounded by the arena size so a
bookkeeping bug degrades rather than hangs.

Also: correct the `deepest_matched_hash` doc comment, which claimed the
match spanned every cache tier when only device details are ever passed;
move the binding import in the router-args test to module scope behind
the documented `pytest.importorskip` guard; and state the index's actual
scope in the `enable_session_prefix_index` docstring -- lineage is fed
from routing-lookup matches only, since stored-block KV events carry no
session ID.

Validation: cargo test -p dynamo-kv-router --lib session_prefix_index
(17 pass, 6 new); cargo check --workspace --all-targets; cargo check in
lib/bindings/python (out of workspace); cargo clippy on both changed
crates; cargo fmt --all; gen_python_api.py regenerated and --check
clean; pre-commit on the changed files.

Signed-off-by: [email protected] <[email protected]>
Signed-off-by: Karen Chung <[email protected]>
Signed-off-by: Karen Chung <[email protected]>
@github-actions github-actions Bot added the feat label Sep 1, 2026
@karen-sy
karen-sy force-pushed the karenc/session-prefix-indexer-rebase branch from 98ae8d9 to 35e4da4 Compare September 11, 2026 18:56
@ishandhanani
ishandhanani force-pushed the karenc/session-prefix-indexer-rebase branch from 30e3ae8 to 74a6e8e Compare September 14, 2026 06:00
@karen-sy
karen-sy removed this pull request from stack #14784 September 17, 2026 00:32
Base automatically changed from karenc/session-prefix-indexer-rebase to main September 17, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants