[PR #1503] [CLOSED] [Engine] conversation cache: never park one conversation with another's retained K/V #3495

Closed
opened 2026-11-07 14:41:52 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1503
Author: @sanastasiou
Created: 10/8/2026
Status: ❌ Closed

Base: main ← Head: fix/retained-kv-leak


📝 Commits (1)

  • cef12c8 conversation cache: never park one conversation with another's retained K/V

📊 Changes

4 files changed (+77 additions, -8 deletions)

View changed files

📝 include/strata/core/conversation_cache.hpp (+24 -4)
📝 src/core/conversation_cache_test.cpp (+35 -0)
📝 src/program/generate.cpp (+9 -2)
📝 sycl/src/program/generate.cpp (+9 -2)

📄 Description

Problem and result

With --batch and conversation parking, one conversation can be parked with another conversation's K/V in its snapshot. Restoring it later serves the other conversation's content.

A restore keeps the restored K/V (ConversationCache::retain) so the next park of the main session copies only what changed. The reuse tracked a token count (unchanged_tokens), not whose tokens. copy_from_slot (a batch slot giving its conversation back to the main session) replaces the main session's K/V but did not drop the retained image. The next park then reused the earlier conversation's K/V for the new one's prefix.

Seen in production on a 2-GPU layer split, --batch 2, --conversation-cache-mib 24576, 4 Claude Code clients:

conversation cache: restored 114705 tokens (live)          <- conversation P; its K/V retained
batch: slot 0 gave back 37564 tokens of this conversation   <- main session now holds Q
conversation cache: parked 37663 tokens ... reused_kv_bytes=573372672   <- Q parked with P's K/V
conversation cache: restored 37663 tokens (live)           <- Q's next turn

Before: Q's reply named a file path that existed only in P's context, and Q answered from P's history from then on. In five 4-conversation runs, 29 parks reused a retained image after a slot had replaced the main session.
After: a park reuses retained K/V only for the prefix the live conversation shares with the conversation it came from; otherwise it copies in full.

none (focused bug fix)

Approach

  • retain() records the ids of the conversation the K/V belongs to.
  • Parks call take_reuse_for(live). It reuses only the prefix that live shares with those ids. With no shared prefix, or no recorded ids, nothing is reused.
  • copy_from_slot drops the retained image before it replaces the main session's K/V.
  • One stderr line is printed whenever a reuse is cut, so the guard is visible.
  • The same edits are made in sycl/src/program/generate.cpp.

Compatibility: the reuse only ever skipped a copy. Cutting it costs at most one full copy of the parked conversation's K/V; the output is otherwise identical. No flag, no default change.

Validation

Passed:

  • Unit tests on this branch's change applied to main e88ee39d (0.2.0-beta + 12 commits; the branch merges cleanly), Ubuntu 24.04, gcc 13.3:
    cmake -G Ninja -S . -B b -DSTRATA_BUILD_CONVERSATION_TESTS=ON -DSTRATA_ENABLE_CUDA=OFF && cmake --build b && (cd b && ctest -R "conversation|session_save")
    → 5/5 tests passed; conversation_cache_test: 4778 checks passed. The new cases: another conversation never gets the retained K/V; a diverging conversation reuses only the shared prefix; a rewrite's limit still holds on every stage; an image of unknown identity is never reused. The first case fails if the ids comparison is removed (checked by mutation on the original revision).
  • CUDA build of v0.2.0-beta (3d1fb480) with this change (plus four unrelated local patches), Linux, CMAKE_CUDA_ARCHITECTURES=86, -DSTRATA_ENABLE_CUDA=ON: builds. It boots and serves Qwen3.8-Flash-Next IQ3_S on 2x RTX 3090 (24 GB each), layer split, --batch 2, conversation cache on, Debian 13, 256 GB RAM.
  • On 0.1.43 with this fix: a 4 × 400K-token compaction run (4 parallel Claude Code conversations, each compacting at about 430K) on the same 2-GPU setup gave recall 5/5 in all four, and none of the transcripts mentions another conversation's working directory.

Failed:
none observed

Not run:

  • SYCL build: no oneAPI environment here. The SYCL edit mirrors the CUDA generate.cpp edit line for line but is not compiled.
  • HIP: no HIP file is touched.
  • The 4-client leak reproduction has not been repeated on 0.2.0-beta; only boot and serving were run there.

Limits

The guard only cuts reuse; it does not add any. A conversation restored, handed to a slot and given back re-copies its full K/V on its next park.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1503 **Author:** [@sanastasiou](https://github.com/sanastasiou) **Created:** 10/8/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/retained-kv-leak` --- ### 📝 Commits (1) - [`cef12c8`](https://github.com/Niko1221/Strata/commit/cef12c872b5d84adc1db2373f99ed7854e3d0689) conversation cache: never park one conversation with another's retained K/V ### 📊 Changes **4 files changed** (+77 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `include/strata/core/conversation_cache.hpp` (+24 -4) 📝 `src/core/conversation_cache_test.cpp` (+35 -0) 📝 `src/program/generate.cpp` (+9 -2) 📝 `sycl/src/program/generate.cpp` (+9 -2) </details> ### 📄 Description ## Problem and result With `--batch` and conversation parking, one conversation can be parked with **another conversation's K/V** in its snapshot. Restoring it later serves the other conversation's content. A restore keeps the restored K/V (`ConversationCache::retain`) so the next park of the main session copies only what changed. The reuse tracked a token *count* (`unchanged_tokens`), not *whose* tokens. `copy_from_slot` (a batch slot giving its conversation back to the main session) replaces the main session's K/V but did not drop the retained image. The next park then reused the earlier conversation's K/V for the new one's prefix. Seen in production on a 2-GPU layer split, `--batch 2`, `--conversation-cache-mib 24576`, 4 Claude Code clients: ``` conversation cache: restored 114705 tokens (live) <- conversation P; its K/V retained batch: slot 0 gave back 37564 tokens of this conversation <- main session now holds Q conversation cache: parked 37663 tokens ... reused_kv_bytes=573372672 <- Q parked with P's K/V conversation cache: restored 37663 tokens (live) <- Q's next turn ``` Before: Q's reply named a file path that existed only in P's context, and Q answered from P's history from then on. In five 4-conversation runs, 29 parks reused a retained image after a slot had replaced the main session. After: a park reuses retained K/V only for the prefix the live conversation shares with the conversation it came from; otherwise it copies in full. ## Related discussion none (focused bug fix) ## Approach - `retain()` records the ids of the conversation the K/V belongs to. - Parks call `take_reuse_for(live)`. It reuses only the prefix that `live` shares with those ids. With no shared prefix, or no recorded ids, nothing is reused. - `copy_from_slot` drops the retained image before it replaces the main session's K/V. - One stderr line is printed whenever a reuse is cut, so the guard is visible. - The same edits are made in `sycl/src/program/generate.cpp`. Compatibility: the reuse only ever skipped a copy. Cutting it costs at most one full copy of the parked conversation's K/V; the output is otherwise identical. No flag, no default change. ## Validation **Passed:** - Unit tests on this branch's change applied to `main` e88ee39d (0.2.0-beta + 12 commits; the branch merges cleanly), Ubuntu 24.04, gcc 13.3: `cmake -G Ninja -S . -B b -DSTRATA_BUILD_CONVERSATION_TESTS=ON -DSTRATA_ENABLE_CUDA=OFF && cmake --build b && (cd b && ctest -R "conversation|session_save")` → 5/5 tests passed; `conversation_cache_test: 4778 checks passed`. The new cases: another conversation never gets the retained K/V; a diverging conversation reuses only the shared prefix; a rewrite's limit still holds on every stage; an image of unknown identity is never reused. The first case fails if the ids comparison is removed (checked by mutation on the original revision). - CUDA build of v0.2.0-beta (3d1fb480) with this change (plus four unrelated local patches), Linux, `CMAKE_CUDA_ARCHITECTURES=86`, `-DSTRATA_ENABLE_CUDA=ON`: builds. It boots and serves Qwen3.8-Flash-Next IQ3_S on 2x RTX 3090 (24 GB each), layer split, `--batch 2`, conversation cache on, Debian 13, 256 GB RAM. - On 0.1.43 with this fix: a 4 × 400K-token compaction run (4 parallel Claude Code conversations, each compacting at about 430K) on the same 2-GPU setup gave recall 5/5 in all four, and none of the transcripts mentions another conversation's working directory. **Failed:** none observed **Not run:** - SYCL build: no oneAPI environment here. The SYCL edit mirrors the CUDA `generate.cpp` edit line for line but is not compiled. - HIP: no HIP file is touched. - The 4-client leak reproduction has not been repeated on 0.2.0-beta; only boot and serving were run there. ## Limits The guard only cuts reuse; it does not add any. A conversation restored, handed to a slot and given back re-copies its full K/V on its next park. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 14:41:52 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3495
No description provided.