mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4031] [CLOSED] [Engine] Conversation cache: Stop evicting what this freed RAM can never re-use; Auto-shrinking RAM conversation cache slots under RAM pressure #4886
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4886
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4031
Author: @dsenchankau
Created: 10/25/2026
Status: ❌ Closed
Base:
main← Head:f-conv-cache-eviction📝 Commits (6)
d31e523conversation cache: stop evicting what this RAM can never re-admit5b43788Merge branch 'main' into f-conv-cache-eviction209403cconversation cache: the RAM-gate shrink stops ratcheting: regrow puts the configured slots and budget back at every park, restore and borrowed resume; the cut keeps its one-per-event pace because the learned ceiling says further cuts would only evict what the RAM could never hold; regrow leaves the ceiling in place so the prediction still guards the regrown cache; docs match the behavior; testsb3ff220Merge branch 'main' into f-conv-cache-eviction912dc02Reverting the 'evict smallest first' policy68bb6c1Merge branch 'main' into f-conv-cache-eviction📊 Changes
7 files changed (+551 additions, -26 deletions)
View changed files
📝
docs/DETAILS.md(+18 -1)📝
include/strata/core/conversation_cache.hpp(+110 -7)📝
include/strata/core/conversation_memory.hpp(+16 -0)📝
src/core/conversation_cache_test.cpp(+207 -2)📝
src/core/conversation_memory_test.cpp(+15 -0)📝
src/program/generate.cpp(+184 -15)📝
sycl/src/program/generate.cpp(+1 -1)📄 Description
The problem
In a nutshell, when the total system RAM is getting close to the physical limit, RAM conversation cache simply stops working at all. The mechanism fails to park and/or restore cache for sessions -> tries to mitigate it by removing old cache entries -> still not enough RAM -> prompt reprocessing -> rinse and repeat. Cache entries are being nuked, cache still not working.
The problematic setup is one instance of Strata (no parallel requests, only 1 at a time), 2 OpenCode sessions taking turns. Each of these main sessions is also launching subagent sessions.
One of the main agent conversation (~199k tokens, ~3.7 GiB snapshot) plus short subagent turns,
--conversation-cache-mib 20480, five slots. The log shows the cache killing itself twice over.make_room()evicts on the slot count, oldest first. The idle main conversation is always the oldest, so it was the one evicted, every burst. Its return was a full cold read every time:prompt 199177 tokens = 0 reused + 199177 read in 69420 ms, again at 65.2-75.3 s.skip parking (physical RAM admission; ... 5285 MiB available ...; evicted 4, 0 still parked). Cache destroyed, park refused, nothing gained. The run never recovered - every later conversation cold-read too.The headline numbers say why this is not a "raise the floor" bug: with the cache completely empty the box measured 5285 MiB free against a 6262 MiB need. Emptying the cache physically cannot admit that snapshot on this machine, so a loop that destroys it "to make room" is just vandalism with good intentions.
What this changes
Three changes, all in the RAM cache, and each of them answers what the log shows rather than a model of it:
skip parking (physical RAM admission; need N MiB plus M MiB floor, ...; measured empty-cache ceiling C MiB would not admit it: kept). Small snapshots that do fit keep parking beside it. Unknown telemetry, exchange parks (the incoming image still holds RAM) and warm parks (retained reuse buffers hold RAM) still run the old code path - a ceiling read beside memory the cache cannot see would lie, and I'd rather have no reading than a wrong one. Free RAM climbing back to the recorded ceiling makes it stale, it is cleared and the attempt runs again.free RAM (measured empty-cache ceiling C MiB) cannot admit a N MiB snapshot plus the M MiB floor - dropping 1 of S slots and shrinking the budget to B MiB so the rest can park again (freed F MiB; --conversation-cache-mib sets it). It tries admission once with the freed RAM and keeps whatever is still parked otherwise. No cutting loop inside one event: the prediction reduces toneed <= measured ceiling, the ceiling says this box could never host that park even with an empty cache, and on a byte-full cache every further cut really does evict - conversations the RAM could not hold anyway, for a refusal the ceiling already predicts. It refuses to shrink when pinned entries would leave no room at the new caps (killing the park-ability of a pinned prefix is not "making room"). Shrinks stop at one slot.shrunk()/regrow()); at every park, every restore and every borrowed resume, when free RAM stands above the learned ceiling AND leaves room for one full-context conversation at its real per-slot cost, the cut caps come back:free RAM fits a full conversation again: back to S slots and B MiB (--conversation-cache-mib sets it). Regrow leaves the learned ceiling alone: clearing it would disarm the prediction and let the next hopeless park empty the freshly regrown cache just to relearn the number. Without this half the cut caps ratchet for the life of the process, and the reporter's follow-up log shows the cost: on a 20480 MiB / 5-slot box the hopeless parks walked the cache down to 2 slots / 4096 MiB, one cut per event, and after the RAM burst passed the self-made cap kept refusing parks that did fit -skip parking (snapshot 4512 MiB exceeds available budget).There is also one new knob and it is off by default:
STRATA_CONV_CACHE_CLAMP=1clamps the startup budget toslots × one full-context conversationcomputed from the loaded model, so a user who over-estimates their RAM never starts with a cache they cannot use. The runtime shrink above needs no flag - it only ever acts where free RAM already proved the configured size unusable. I kept the startup clamp behind the env on purpose: the per-slot arithmetic is unit-tested, but it decides real users' budgets and deserves box time before it fires unasked. (The same computation already runs unconditionally for the shrink cap and for a startup log line when the budget exceeds it.)The whole-model math behind "one full-context conversation" lives in two pure, saturated helpers (
conversation_slot_ceiling_bytes,conversation_cache_budget_bytes); a PROPERTY test builds a realSavedConversationat that bound and asserts its ownbytes()never exceeds the ceiling, including a 13-buffer K/V case. It over-counts every field (ids and image keys sized at ctx, the whole running state charged in every checkpoint), so it cannot cut capacity a park could use.Compatibility
session_save_reclaimkeeps its documented oldest-first eviction.conversation cache: skip parking (physical RAM admission;is intact on every refusal line (tools/conversation_cache_parity.pyneeded no change); the shrink line carries no prefix because it is not a skip.shrink,regrow). A SYCL startup clamp is not wired: its sizes need a SYCL box to verify. The ceiling gate, the shrink and the regrow are main-path only, as before this PR.Validation
Environment: Linux home-pc 7.0.0-38-generic #38~24.04.4-Ubuntu SMP PREEMPT_DYNAMIC x86_64, RTX 4080 SUPER (driver 615.71.10), CMake 3.28.3, Ninja 1.11.1, CUDA 13.4, Python 3.12.3.
Passed:
All tests pass except the known flakes and the failures that come from main (e.g. different hardware). (The full-
buildrun above predates the merge of main and the oldest-first revert; after them the 23-testbuild-convsuite and the serve tests were re-run green,conversation_cache_testat 4886 checks.)The new checks cover: the oldest-first victim rules and their accounting, the predictor's overflow and fail-closed cases (with the reporter's own numbers as real inputs), the per-slot ceiling incl. saturation, monotonicity and the PROPERTY test above,
shrink()scaling, pinned refusals, freed accounting,evictable/all_pinnedceiling-record rules, the retain over-budget guard, and the cuts not staying cut:shrunk()/regrow()back to the configured slots and budget, and the one-slot-per-event march ending at one slot.Failed: nothing on this host.
Not run:
buildctest and the serve discovery run after the merge and the oldest-first revert (the gate and regrow live ingenerate.cpp, which the dev box used for the revert has no CUDA toolchain for; box re-run pending).Known limits
try_regrow().shrunk()and the regrow line are the observable state instead.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.