Skip to content

feat(agent sessions): attribute stored KV cache blocks to sessions - #37482

Merged
ishandhanani merged 27 commits into
mainfrom
idhanani/sglang-session-kv-events-sidecar
Sep 15, 2026
Merged

ishandhanani merged 27 commits into
mainfrom
idhanani/sglang-session-kv-events-sidecar

Conversation

@ishandhanani

@ishandhanani ishandhanani commented Sep 1, 2026 •

Copy link
Copy Markdown
Collaborator

This PR adds optional request session_id metadata to SGLang BlockStored KV events while preserving the existing seven-element base wire payload when no metadata is present. It gives external KV routers enough engine-side evidence to associate stored block lineage with the triggering session without treating shared radix-cache blocks as session-owned.

How This Was Implemented

  • Extend BlockStoredMetadata with an optional session_id and include metadata in the wire payload only when at least one field is populated.
  • Thread Req.session_id through finished and unfinished unified-cache inserts and request-driven unevict events.
  • Keep session_id compatible with cache_salt; event metadata does not participate in block hashing or cache identity.
  • Cover wire compatibility, metadata-aware coalescing, quiet shared-prefix hits, divergent-tail attribution, cache salt, and unevict behavior.
Walkthrough

Mental model

The event identifies the request that caused a physical store transition. It is not an owner list for a content-addressed block that several sessions may share.

flowchart LR
    A["Req.session_id"] --> B["InsertParams"]
    B --> C["UnifiedTreeCore insert or unevict"]
    C --> D["KVCacheEventRecorder"]
    D --> E["BlockStored metadata"]
    E --> F["External KV event consumer"]
Loading

Request lifecycle

The request's session identifier follows the same insertion context as the token IDs and optional cache salt. New pages and request-driven unevicts attach that identifier to BlockStored; a resident shared-prefix hit emits no duplicate store event, while a divergent tail links back to the shared parent hash and carries the session that created the tail.

Boundaries and limitations

Background HiCache transitions without request context remain unattributed, BlockRemoved remains unchanged, and events with different metadata do not coalesce. This change does not add vLLM-style opt-in full cache-hit reporting or change session-radix-cache ownership and lifecycle tracking.

Validation

  • .venv/bin/python -m pytest -q test/registered/unit/disaggregation/test_kv_events.py::TestBlockStoredWireFormat test/registered/unit/mem_cache/test_radix_cache_unit.py::TestKVCacheEventQueue test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py -k 'TestBlockStoredWireFormat or TestKVCacheEventQueue or session_id_is_attributed or session_id_and_cache_salt or shared_prefix_hit_is_quiet_and_divergent_tails_are_attributed' — 11 passed, 2436 deselected.
  • uvx --from 'black==26.1.0' black test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py and uvx --from 'ruff==0.15.1' ruff check --select=F401,F821,UP037 test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py — passed.
  • Complementary Dynamo SessionPrefixIndexer regression combines two route-time shared-prefix matches with two session-attributed divergent tails — all 18 session-prefix-index tests passed.
  • Live SGLang + Dynamo sidecar smoke test with Qwen/Qwen3-0.6B: raw ZMQ emitted four stored block hashes with session_id: session-e2e-001; replay reported #cached-token: 64 and Dynamo reported overlap_blocks=4; the request completed with HTTP 200.

Related Dynamo work: ai-dynamo/dynamo#13806 and ai-dynamo/dynamo#13807


CI States

Latest PR Test (Base): 🚫 Run #34811471297
Latest PR Test (Extra): ❌ Run #34811471181
Latest PR Test (AMD ROCm 10): ❌ Run #34811471338

Wire encoding change (added after review)

KV events are now tagged msgpack maps, not positional arrays. KVCacheEvent drops array_like=True and keeps tag=True, omit_defaults=True, so each event is {"type": "<ClassName>", "<field>": ...} and optional fields left at None are omitted. cache_salt and session_id are plain optional fields on BlockStored. BlockStoredMetadata and BlockStoredWithMetadata are removed. EventBatch stays a positional array [ts, events, attn_dp_rank].

Why: positional arrays cannot grow. msgspec does not trim a trailing None in array_like structs (tested on 0.21.1), so salt metadata had to ride as an extra trailing element (#30827). That element landed in the lora_name slot of consumers that share a decoder with vLLM, and Dynamo dropped every salted batch (ai-dynamo/dynamo#14530 has the reproduction). Exported typed consumers could not read the salt either (#38561). vLLM moved its KV events to this exact map encoding in v0.24, so consumers now decode both engines with one code path.

This is a wire-format change for positional consumers. It will be called out in the release notes of the next SGLang release. The in-repo consumers (experimental/sgl-router and sgl-kv-indexer) decode the map shape only; the old array shape is rejected.

Size: a 16-token, 8-block BlockStored grows from about 666 to 755 bytes (keys); block hashes and token ids still dominate.

Validation of the encoding change

  • test/registered/unit/disaggregation/test_kv_events.py: events encode as tagged maps, unset optionals are omitted, one KVEventBatch decoder reads a mixed stream (unsalted, salted, session-attributed, removed, cleared), batch stays an array.
  • test_radix_cache_unit.py, test_unified_radix_cache_unittest.py, test_rust_tree_core_integration.py updated to the plain fields and run on GPU.
  • sgl-router wire decoder: 27 tests including msgspec golden bytes of a mixed map batch; sgl-kv-indexer bridge: 42 tests including map-form report/revoke/clear, ignored attribution keys, component-aware maps, and skipped undecodable maps.
  • Dynamo main decodes the exact bytes this branch produces (cache namespace from cache_salt; session_id read once feat(router): retrieve optional session_id field from vLLM engine KV events ai-dynamo/dynamo#13806 lands).

@ishandhanani

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@ishandhanani ishandhanani changed the title [Feature] Attribute stored KV cache blocks to sessions feat(agent sessions): attribute stored KV cache blocks to sessions Sep 1, 2026
@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Sep 1, 2026
@ishandhanani

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@ishandhanani

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/e2e/models/test_inkling_unified.py

@github-actions

github-actions Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/e2e/models/test_inkling_unified.py:

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_inkling_unified.py

@ishandhanani

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/e2e/models/test_glm53_flash_b200.py

@github-actions

github-actions Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/e2e/models/test_glm53_flash_b200.py:

🚀 4-gpu-b200 (1 test): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_glm53_flash_b200.py

@ishandhanani

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/unit/disaggregation/test_kv_events.py test/registered/unit/mem_cache/test_radix_cache_unit.py test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py test/registered/unit/mem_cache/test_rust_unified_radix_cache_unittest.py test/registered/rust/test_run_sglang_radix_tree_rust_tests.py

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/unit/disaggregation/test_kv_events.py test/registered/unit/mem_cache/test_radix_cache_unit.py test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py test/registered/unit/mem_cache/test_rust_unified_radix_cache_unittest.py test/registered/rust/test_run_sglang_radix_tree_rust_tests.py:

🚀 ubuntu-latest (2 tests): ✅ View workflow run

cd test/ && python3 registered/unit/disaggregation/test_kv_events.py
cd test/ && python3 registered/rust/test_run_sglang_radix_tree_rust_tests.py

🚀 1-gpu-5090 (3 tests): ❌ View workflow run

cd test/ && python3 registered/unit/mem_cache/test_radix_cache_unit.py
cd test/ && python3 registered/unit/mem_cache/test_unified_radix_cache_unittest.py
cd test/ && python3 registered/unit/mem_cache/test_rust_unified_radix_cache_unittest.py

@Jiminator Jiminator closed this Sep 14, 2026
@Jiminator
Jiminator deleted the idhanani/sglang-session-kv-events-sidecar branch September 14, 2026 04:44
@alexnails
alexnails restored the idhanani/sglang-session-kv-events-sidecar branch September 14, 2026 05:44
@ishandhanani ishandhanani reopened this Sep 14, 2026
@ishandhanani

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/unit/disaggregation/test_kv_events.py test/registered/unit/mem_cache/test_radix_cache_unit.py test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py test/registered/unit/mem_cache/test_rust_unified_radix_cache_unittest.py test/registered/rust/test_run_sglang_radix_tree_rust_tests.py

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/unit/disaggregation/test_kv_events.py test/registered/unit/mem_cache/test_radix_cache_unit.py test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py test/registered/unit/mem_cache/test_rust_unified_radix_cache_unittest.py test/registered/rust/test_run_sglang_radix_tree_rust_tests.py:

🚀 ubuntu-latest (2 tests): ✅ View workflow run

cd test/ && python3 registered/unit/disaggregation/test_kv_events.py
cd test/ && python3 registered/rust/test_run_sglang_radix_tree_rust_tests.py

🚀 1-gpu-5090 (3 tests): ✅ View workflow run

cd test/ && python3 registered/unit/mem_cache/test_radix_cache_unit.py
cd test/ && python3 registered/unit/mem_cache/test_unified_radix_cache_unittest.py
cd test/ && python3 registered/unit/mem_cache/test_rust_unified_radix_cache_unittest.py

@ishandhanani

Copy link
Copy Markdown
Collaborator Author

all relevant CI passed. only 1 flaky base c failed.

@ishandhanani
ishandhanani merged commit 3f871a2 into main Sep 15, 2026
302 of 355 checks passed
@ishandhanani
ishandhanani deleted the idhanani/sglang-session-kv-events-sidecar branch September 15, 2026 04:34
ishandhanani added a commit that referenced this pull request Sep 16, 2026
Brings in the segment lock protocol with receipts (#36848), SWA branching-point
caching (#37584), the external cache linker (#37306), session-attributed KV
events (#37482), aux LRU preservation on split (#38482), and the buffer-mode
prefetch rework (#39283). Every conflicting signature keeps main's shape with V
in place of Tensor; main's new Tensor-only code paths (external-linker offload
transfers, the multi-node SWA backup commit) use the RadixValue operations; the
duplicated read-only FULL-device walk behind full_kv_prefix_len and
match_full_device_prefix is shared through one private helper.
Kangyan-Zhou pushed a commit that referenced this pull request Oct 7, 2026
…maps

Cherry-picked from #37482. Engines now emit each
KVCacheEvent as a msgspec tagged map ({"type": ..., <field>: ...}) rather
than a positional array; the decoder accepts both, so the ZMQ component
tests now exercise the shape live engines send.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JqRozm7pWUVqLM9Uppzypk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants