[PR #3517] [Engine] conversation cache: the retention pin follows the system-prompt root, not chain item 0 #4664

Open
opened 2026-11-07 15:38:10 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3517
Author: @chaog992
Created: 10/20/2026
Status: 🔄 Open

Base: main ← Head: conv-cache-root-pin


📝 Commits (1)

  • d2a3adf conversation cache: the retention pin follows the system-prompt root, not chain item 0 (#2228, #3090)

📊 Changes

5 files changed (+352 additions, -11 deletions)

View changed files

📝 docs/DETAILS.md (+6 -3)
📝 include/strata/program/conv_cache.hpp (+77 -0)
📝 src/program/conv_cache_test.cpp (+246 -5)
📝 src/program/generate.cpp (+12 -2)
📝 sycl/src/program/generate.cpp (+11 -1)

📄 Description

Problem and result

The retention policy keeps "the root" by pinning chain item 0 (conv_cache.hpp). On a read from token 0,
periodic checkpoints (--prompt-cache-every, taken at chunk ends) are saved during the read, before the root
checkpoint at the first turn boundary (--prompt-cache-root). So when the system prompt and tools end past
the first such chunk end (16K at the defaults), item 0 is a grid checkpoint: the pin protects that grid point
forever, and the real root rotates out at the (cap−1)-th checkpoint after its last use. From then on every new
chat and every compaction re-reads root − 16K tokens at prefill speed, and the root is never saved again (only
a read from token 0 saves it).

Result (policy tests, and an engine A/B below): the pin follows the root, so a new chat after checkpoint
churn mounts the end of the system prompt again, as docs/DETAILS.md promises ("kept for good").

#2228 (open): the reporter's probe of 2026-10-13 20:04 - the 159,184-token root was gone after roughly twelve
requests at --prompt-cache 12 (a policy simulation gives the 10th append), the compaction then mounted at
32,768, and the root survived at --prompt-cache 48. The within-thread grid fallbacks of that issue's
original report, where the 20,765-token root was item 0 all along, are a different case and are not changed
here. #3090 (closed in the 10-19 tracker reset) reached the same diagnosis from the 0.1.45 sources and asked
to pin the root explicitly; its turn-start resumes from 16,384 were from the pinned grid checkpoint.
Merge-adjacent: #1887 (reopened 10-20) and #2240 on top of it move this eviction loop into a helper - whichever
lands second passes the root index at both of its call sites; #2526 appends slot checkpoints to the chain,
which the length-based lookup does not depend on.

Approach

eviction_victim and spread_victim gain overloads that take the root's index and pin it instead of item 0,
which then rotates like any other item; with root 0 (or out of range) they delegate to the existing overloads.
The serve loop names the root through two new pure helpers in the same header: root_length reads the
conversation itself - the first turn boundary, when it lies at or past --prompt-cache-root and a later
boundary exists, the position root_at picks (a single-boundary conversation has no system prompt ending
there) - and
root_index finds the chain item of that length. #3090 suggested marking the root when it is saved, as
pinned marks a pin=N checkpoint; a lookup adds no state that the checkpoint pool, parking and restores would
have to carry and reset, and it runs once per eviction round, only when something must leave (~55-60 µs worst
case at 262,144 tokens, gcc 13.3 -O2 on an i7-13700K). The SYCL mirror gets the same lookup. +355/−13 over 5
files; no new flag.

Validation

Tested: 70781f4b on main 9ae93f7b (v0.2.1.1), WSL2 Ubuntu 24.04, gcc 13.3; the commit also merges cleanly
onto current main.

Passed:

  • g++ -O2 -std=c++20 -Wall -Wextra -Wconversion -I include src/program/conv_cache_test.cpp (also the ctest
    build): all 42 checks pass in 1.5 s. They include: the 40K-root and #2228-shaped chains keep their root for
    30+ turns, driven through the production root_length/root_index lookups, and the 40K chain loses it
    with no root named (LRU and spread controls); the lookup also drives a grids-ahead-of-root chain down to two
    slots, keeping {root, newest}; the policies match references written independently from the doc comments on
    every chain of up to 5 items over three length shapes (even, convex, one huge gap), every root in [0, n),
    stamps with ties, caps 0..n+1 and every tail/pin subset (and absent arrays); a named-root spread case on an
    uneven chain pins the expected victim; root 0 or out of range equals the old overloads the same way
    (delegation guard); root_length / root_index unit cases.

  • The same test against the base policy (the base header plus the new lookups and overloads that ignore the
    root argument, i.e. today's behaviour): 8 of the 42 checks fail - the named-root scenarios and the reference
    comparison - and the other 34 pass. That is the failing-before receipt. Command: the same g++ line with
    -I <dir> holding the shim as strata/program/conv_cache.hpp; both outputs and the shim are below.

    test output, this branch (42 ok)
    conv_cache_test
      an edit 6K before the end: least recent use reuses 9525, spread 201525 of 209525
      least recent use: an edit 6K back falls through to the root        ok
      spread: an edit 6K back reads less than 24K again                  ok
      spread: an edit 42K back reads less than 168K again                ok
      spread: the turn before is still kept                              ok
      spread: every candidate pinned - the plain policy                  ok
      spread: two items, one slot - the plain policy                     ok
      over budget with fresh stamps: the oldest leaf leaves (the FIFO it was) ok
      the root's stale stamp never makes it the victim                   ok
      the root - the shared prefix - is never the victim                 ok
      after a mount, the rotation takes the next least recently used leaf ok
      a two-slot budget keeps root + newest and drops the leaf in between ok
      a one-slot budget has no pin: the oldest leaves, the newest point stays ok
      ties among equally stale leaves: the earlier index, deterministically ok
      a stale tail checkpoint leaves before the oldest leaf              ok
      the newest item, even a tail, is not the victim                    ok
      the root is never the victim, tail flag or not                     ok
      no tail flags: the plain policy                                    ok
      the oldest checkpoint is pinned: the next least recently used leaves ok
      no pin flags: the plain policy                                     ok
      two pins: the third oldest leaves                                  ok
      a pinned item is not taken as the tail either                      ok
      everything pinned: the oldest pin leaves, never the root           ok
      a one-slot budget keeps no pin: the oldest leaves                  ok
      a root behind two grid checkpoints survives 30 turns when the caller names it ok
      control: with no root named (root 0) item 0 stays the pin, and that root rotates out ok
      #2228's chain: the 159K root survives 30 turns at --prompt-cache 12 ok
      the same under --prompt-cache-spread: the named root is never dropped ok
      control: spread with no root named loses that root too             ok
      evicting to two slots through the real lookup keeps {root, newest} ok
      a two-slot budget keeps the named root + newest; the grid point at item 0 leaves ok
      a tail checkpoint at item 0 (ahead of the root) is the victim once the root is named ok
      everything else pinned: the oldest pin leaves, never the named root ok
      root_length: the first turn boundary at or past --prompt-cache-root ok
      root_length: no turn token - no root                               ok
      root_length: a first boundary below --prompt-cache-root is not a root ok
      root_length: a single boundary is the conversation's last turn, not a system prompt's end ok
      root_index: the chain item at the root's length                    ok
      root_index: no item at that length - item 0, as before             ok
      root_index: no root - item 0, as before                            ok
      spread with a named root picks the smallest relative gap           ok
      eviction and spread match the doc-comment references on every small chain, shape and root ok
      root 0 or out of range: the root overloads equal the plain policy on every small chain ok
    PASS
    
    test output against the shimmed base policy (8 FAIL)
    conv_cache_test
      an edit 6K before the end: least recent use reuses 9525, spread 201525 of 209525
      least recent use: an edit 6K back falls through to the root        ok
      spread: an edit 6K back reads less than 24K again                  ok
      spread: an edit 42K back reads less than 168K again                ok
      spread: the turn before is still kept                              ok
      spread: every candidate pinned - the plain policy                  ok
      spread: two items, one slot - the plain policy                     ok
      over budget with fresh stamps: the oldest leaf leaves (the FIFO it was) ok
      the root's stale stamp never makes it the victim                   ok
      the root - the shared prefix - is never the victim                 ok
      after a mount, the rotation takes the next least recently used leaf ok
      a two-slot budget keeps root + newest and drops the leaf in between ok
      a one-slot budget has no pin: the oldest leaves, the newest point stays ok
      ties among equally stale leaves: the earlier index, deterministically ok
      a stale tail checkpoint leaves before the oldest leaf              ok
      the newest item, even a tail, is not the victim                    ok
      the root is never the victim, tail flag or not                     ok
      no tail flags: the plain policy                                    ok
      the oldest checkpoint is pinned: the next least recently used leaves ok
      no pin flags: the plain policy                                     ok
      two pins: the third oldest leaves                                  ok
      a pinned item is not taken as the tail either                      ok
      everything pinned: the oldest pin leaves, never the root           ok
      a one-slot budget keeps no pin: the oldest leaves                  ok
      a root behind two grid checkpoints survives 30 turns when the caller names it FAIL
      control: with no root named (root 0) item 0 stays the pin, and that root rotates out ok
      #2228's chain: the 159K root survives 30 turns at --prompt-cache 12 FAIL
      the same under --prompt-cache-spread: the named root is never dropped FAIL
      control: spread with no root named loses that root too             ok
      evicting to two slots through the real lookup keeps {root, newest} FAIL
      a two-slot budget keeps the named root + newest; the grid point at item 0 leaves FAIL
      a tail checkpoint at item 0 (ahead of the root) is the victim once the root is named FAIL
      everything else pinned: the oldest pin leaves, never the named root FAIL
      root_length: the first turn boundary at or past --prompt-cache-root ok
      root_length: no turn token - no root                               ok
      root_length: a first boundary below --prompt-cache-root is not a root ok
      root_length: a single boundary is the conversation's last turn, not a system prompt's end ok
      root_index: the chain item at the root's length                    ok
      root_index: no item at that length - item 0, as before             ok
      root_index: no root - item 0, as before                            ok
      spread with a named root picks the smallest relative gap           ok
      eviction and spread match the doc-comment references on every small chain, shape and root FAIL
      root 0 or out of range: the root overloads equal the plain policy on every small chain ok
    FAIL
    
    the shim block appended to the base conv_cache.hpp
    
    }  // namespace strata::program::conv_cache
    
    // receipt shim: the serve loop's behaviour at the base - the root argument exists but the policies ignore it
    namespace strata::program::conv_cache {
    inline int64_t root_length(const int64_t* ids, size_t n_ids, int64_t turn_token, int64_t min_root) {
        if (turn_token < 0 || min_root <= 0) return 0;
        for (size_t i = 1; i < n_ids; ++i)
            if (ids[i] == turn_token) {
                if ((int64_t) i < min_root) return 0;
                for (size_t j = i + 1; j < n_ids; ++j)
                    if (ids[j] == turn_token) return (int64_t) i;
                return 0;
            }
        return 0;
    }
    inline size_t root_index(const int64_t* lens, size_t n, int64_t root_len) {
        if (root_len > 0)
            for (size_t i = 0; i < n; ++i)
                if (lens[i] == root_len) return i;
        return 0;
    }
    inline size_t eviction_victim(const uint64_t* s, size_t n, int64_t c, const bool* t, const bool* p, size_t) {
        return eviction_victim(s, n, c, t, p);
    }
    inline size_t spread_victim(const int64_t* l, const uint64_t* s, size_t n, int64_t c, const bool* t, const bool* p,
                                size_t) {
        return spread_victim(l, s, n, c, t, p);
    }
    }
    
  • Full CPU ctest (cmake -DCMAKE_BUILD_TYPE=Release -DSTRATA_BUILD_TESTS=ON -DSTRATA_GGML_DIR=<llama.cpp@pin>
    then ctest -j8): 49 tests - 44 passed (conv_cache_test among them), 1 skipped, 4 failed; see Failed.

  • CUDA (sm_120, nvcc 13.0.88), HIP (gfx1201, ROCm clang 22) and SYCL (icpx 2026.1.1): the full strata target
    configures and links from this branch (compile and link on the dev box, which has no GPU).

  • Engine A/B on a GPU machine: RTX 5080 16 GB (driver 595.84), i9-14900K, Linux, Docker
    (nvidia/cuda:13.0.0-devel, sm_120 build), qwen Q2_0, --context 32768, stock settings (tuning skipped,
    vram_reserve_mib 4096; another service held ~3.5 GB of VRAM). A 23,378-token synthetic system prompt,
    seven appended turns, then a new chat with the same system prompt - base engine (9ae93f7b) vs this branch:
    base mounts a grid checkpoint, prompt 23377 tokens = 19456 reused + 3921 read (1,691 ms); this branch
    mounts the root, prompt 23377 tokens = 23346 reused + 31 read (262 ms). The first reads and every turn's
    draft-acceptance numbers are identical in both runs; the four receipt lines are quoted verbatim from the
    engine log, and the full log and replay script are available on request.

Failed: the four pre-existing Gyro test failures (isa_guard_check, tq_cpu_parity, tq_cpu_parity_avx2,
native_hadamard_test): the default pinned ggml has no trellis types and the guard lacks the tq entries. They
are identical at the unpatched base and untouched by this change; #3511 addresses them.

Not run: #2228's exact request sequence on the reporter's machine - the A/B above is a synthetic
conversation (note the recipe: the new chat must come after at least --prompt-cache − 1 checkpoints since
the root's last mount, or both builds still show the root; about 11 appends at --prompt-cache 12). The
reporter offered to run diagnostic builds, and the replay script is ready. Windows (MSVC) and Metal (macOS
only; the shared generate.cpp compiles into it unchanged) not built. expert_multi_test skipped (no AVX-512
on the dev box).

Limits

  • Default-path behaviour changes exactly when a system prompt ends past the first periodic chunk end and the
    cache goes over budget - that is the fix. Where a mount point changes, the read's chunk boundaries move, so
    answers after such a mount can differ in last-place rounding (as with any different resume point).
  • Opt-in paths change even below 16K: a --prompt-cache-tail or pin=N checkpoint sitting ahead of the
    system-prompt root no longer holds the root's pin (the root stays instead). Both changes are in the root's
    favour.
  • A conversation with no system turn but two or more boundaries pins the end of its first message - the same
    position root_at picks on a full re-read of such a conversation. A new chat that shares only part of that
    first message then reads from token 0 where it could previously mount the 16K grid point.
  • The old grid point at item 0 can rotate out, so a client whose next prompt diverges before the root (for
    example a system prompt whose tail carries a date) reads from token 0 once per distinct system prompt and
    then gets a proper root for it. Keeping both pinned was worse in simulation: at --prompt-cache 2 every new
    save evicts itself, and one rotating slot is lost at every cap.
  • Under --prompt-cache-spread the first grid point stays (the gap rule never takes item 0), costing one
    slot; documented in DETAILS.md.
  • Only reads from token 0 save a root, so a session that lost its root before this change keeps its grid-point
    chain until the next full read. --batch slot hand-back still clears the main chain, as before.
  • The mechanism analysis is from reading the sources (unconfirmed static analysis) plus policy simulations
    that reproduce both reporters' numbers; the engine A/B confirms the new-chat case on a synthetic
    conversation, not the reporters' own sequences.

🔄 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/3517 **Author:** [@chaog992](https://github.com/chaog992) **Created:** 10/20/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `conv-cache-root-pin` --- ### 📝 Commits (1) - [`d2a3adf`](https://github.com/Niko1221/Strata/commit/d2a3adf36963941e56de6397a39ba03b32ed945f) conversation cache: the retention pin follows the system-prompt root, not chain item 0 (#2228, #3090) ### 📊 Changes **5 files changed** (+352 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+6 -3) 📝 `include/strata/program/conv_cache.hpp` (+77 -0) 📝 `src/program/conv_cache_test.cpp` (+246 -5) 📝 `src/program/generate.cpp` (+12 -2) 📝 `sycl/src/program/generate.cpp` (+11 -1) </details> ### 📄 Description ## Problem and result The retention policy keeps "the root" by pinning chain item 0 (`conv_cache.hpp`). On a read from token 0, periodic checkpoints (`--prompt-cache-every`, taken at chunk ends) are saved during the read, before the root checkpoint at the first turn boundary (`--prompt-cache-root`). So when the system prompt and tools end past the first such chunk end (16K at the defaults), item 0 is a grid checkpoint: the pin protects that grid point forever, and the real root rotates out at the (cap−1)-th checkpoint after its last use. From then on every new chat and every compaction re-reads root − 16K tokens at prefill speed, and the root is never saved again (only a read from token 0 saves it). Result (policy tests, and an engine A/B below): the pin follows the root, so a new chat after checkpoint churn mounts the end of the system prompt again, as docs/DETAILS.md promises ("kept for good"). ## Related discussion #2228 (open): the reporter's probe of 2026-10-13 20:04 - the 159,184-token root was gone after roughly twelve requests at `--prompt-cache 12` (a policy simulation gives the 10th append), the compaction then mounted at 32,768, and the root survived at `--prompt-cache 48`. The within-thread grid fallbacks of that issue's original report, where the 20,765-token root was item 0 all along, are a different case and are not changed here. #3090 (closed in the 10-19 tracker reset) reached the same diagnosis from the 0.1.45 sources and asked to pin the root explicitly; its turn-start resumes from 16,384 were from the pinned grid checkpoint. Merge-adjacent: #1887 (reopened 10-20) and #2240 on top of it move this eviction loop into a helper - whichever lands second passes the root index at both of its call sites; #2526 appends slot checkpoints to the chain, which the length-based lookup does not depend on. ## Approach `eviction_victim` and `spread_victim` gain overloads that take the root's index and pin it instead of item 0, which then rotates like any other item; with root 0 (or out of range) they delegate to the existing overloads. The serve loop names the root through two new pure helpers in the same header: `root_length` reads the conversation itself - the first turn boundary, when it lies at or past `--prompt-cache-root` and a later boundary exists, the position `root_at` picks (a single-boundary conversation has no system prompt ending there) - and `root_index` finds the chain item of that length. #3090 suggested marking the root when it is saved, as `pinned` marks a pin=N checkpoint; a lookup adds no state that the checkpoint pool, parking and restores would have to carry and reset, and it runs once per eviction round, only when something must leave (~55-60 µs worst case at 262,144 tokens, gcc 13.3 -O2 on an i7-13700K). The SYCL mirror gets the same lookup. +355/−13 over 5 files; no new flag. ## Validation Tested: 70781f4b on main 9ae93f7b (v0.2.1.1), WSL2 Ubuntu 24.04, gcc 13.3; the commit also merges cleanly onto current main. Passed: - `g++ -O2 -std=c++20 -Wall -Wextra -Wconversion -I include src/program/conv_cache_test.cpp` (also the ctest build): all 42 checks pass in 1.5 s. They include: the 40K-root and #2228-shaped chains keep their root for 30+ turns, driven through the production `root_length`/`root_index` lookups, and the 40K chain loses it with no root named (LRU and spread controls); the lookup also drives a grids-ahead-of-root chain down to two slots, keeping {root, newest}; the policies match references written independently from the doc comments on every chain of up to 5 items over three length shapes (even, convex, one huge gap), every root in [0, n), stamps with ties, caps 0..n+1 and every tail/pin subset (and absent arrays); a named-root spread case on an uneven chain pins the expected victim; root 0 or out of range equals the old overloads the same way (delegation guard); `root_length` / `root_index` unit cases. - The same test against the base policy (the base header plus the new lookups and overloads that ignore the root argument, i.e. today's behaviour): 8 of the 42 checks fail - the named-root scenarios and the reference comparison - and the other 34 pass. That is the failing-before receipt. Command: the same g++ line with `-I <dir>` holding the shim as `strata/program/conv_cache.hpp`; both outputs and the shim are below. <details><summary>test output, this branch (42 ok)</summary> ``` conv_cache_test an edit 6K before the end: least recent use reuses 9525, spread 201525 of 209525 least recent use: an edit 6K back falls through to the root ok spread: an edit 6K back reads less than 24K again ok spread: an edit 42K back reads less than 168K again ok spread: the turn before is still kept ok spread: every candidate pinned - the plain policy ok spread: two items, one slot - the plain policy ok over budget with fresh stamps: the oldest leaf leaves (the FIFO it was) ok the root's stale stamp never makes it the victim ok the root - the shared prefix - is never the victim ok after a mount, the rotation takes the next least recently used leaf ok a two-slot budget keeps root + newest and drops the leaf in between ok a one-slot budget has no pin: the oldest leaves, the newest point stays ok ties among equally stale leaves: the earlier index, deterministically ok a stale tail checkpoint leaves before the oldest leaf ok the newest item, even a tail, is not the victim ok the root is never the victim, tail flag or not ok no tail flags: the plain policy ok the oldest checkpoint is pinned: the next least recently used leaves ok no pin flags: the plain policy ok two pins: the third oldest leaves ok a pinned item is not taken as the tail either ok everything pinned: the oldest pin leaves, never the root ok a one-slot budget keeps no pin: the oldest leaves ok a root behind two grid checkpoints survives 30 turns when the caller names it ok control: with no root named (root 0) item 0 stays the pin, and that root rotates out ok #2228's chain: the 159K root survives 30 turns at --prompt-cache 12 ok the same under --prompt-cache-spread: the named root is never dropped ok control: spread with no root named loses that root too ok evicting to two slots through the real lookup keeps {root, newest} ok a two-slot budget keeps the named root + newest; the grid point at item 0 leaves ok a tail checkpoint at item 0 (ahead of the root) is the victim once the root is named ok everything else pinned: the oldest pin leaves, never the named root ok root_length: the first turn boundary at or past --prompt-cache-root ok root_length: no turn token - no root ok root_length: a first boundary below --prompt-cache-root is not a root ok root_length: a single boundary is the conversation's last turn, not a system prompt's end ok root_index: the chain item at the root's length ok root_index: no item at that length - item 0, as before ok root_index: no root - item 0, as before ok spread with a named root picks the smallest relative gap ok eviction and spread match the doc-comment references on every small chain, shape and root ok root 0 or out of range: the root overloads equal the plain policy on every small chain ok PASS ``` </details> <details><summary>test output against the shimmed base policy (8 FAIL)</summary> ``` conv_cache_test an edit 6K before the end: least recent use reuses 9525, spread 201525 of 209525 least recent use: an edit 6K back falls through to the root ok spread: an edit 6K back reads less than 24K again ok spread: an edit 42K back reads less than 168K again ok spread: the turn before is still kept ok spread: every candidate pinned - the plain policy ok spread: two items, one slot - the plain policy ok over budget with fresh stamps: the oldest leaf leaves (the FIFO it was) ok the root's stale stamp never makes it the victim ok the root - the shared prefix - is never the victim ok after a mount, the rotation takes the next least recently used leaf ok a two-slot budget keeps root + newest and drops the leaf in between ok a one-slot budget has no pin: the oldest leaves, the newest point stays ok ties among equally stale leaves: the earlier index, deterministically ok a stale tail checkpoint leaves before the oldest leaf ok the newest item, even a tail, is not the victim ok the root is never the victim, tail flag or not ok no tail flags: the plain policy ok the oldest checkpoint is pinned: the next least recently used leaves ok no pin flags: the plain policy ok two pins: the third oldest leaves ok a pinned item is not taken as the tail either ok everything pinned: the oldest pin leaves, never the root ok a one-slot budget keeps no pin: the oldest leaves ok a root behind two grid checkpoints survives 30 turns when the caller names it FAIL control: with no root named (root 0) item 0 stays the pin, and that root rotates out ok #2228's chain: the 159K root survives 30 turns at --prompt-cache 12 FAIL the same under --prompt-cache-spread: the named root is never dropped FAIL control: spread with no root named loses that root too ok evicting to two slots through the real lookup keeps {root, newest} FAIL a two-slot budget keeps the named root + newest; the grid point at item 0 leaves FAIL a tail checkpoint at item 0 (ahead of the root) is the victim once the root is named FAIL everything else pinned: the oldest pin leaves, never the named root FAIL root_length: the first turn boundary at or past --prompt-cache-root ok root_length: no turn token - no root ok root_length: a first boundary below --prompt-cache-root is not a root ok root_length: a single boundary is the conversation's last turn, not a system prompt's end ok root_index: the chain item at the root's length ok root_index: no item at that length - item 0, as before ok root_index: no root - item 0, as before ok spread with a named root picks the smallest relative gap ok eviction and spread match the doc-comment references on every small chain, shape and root FAIL root 0 or out of range: the root overloads equal the plain policy on every small chain ok FAIL ``` </details> <details><summary>the shim block appended to the base conv_cache.hpp</summary> ```cpp } // namespace strata::program::conv_cache // receipt shim: the serve loop's behaviour at the base - the root argument exists but the policies ignore it namespace strata::program::conv_cache { inline int64_t root_length(const int64_t* ids, size_t n_ids, int64_t turn_token, int64_t min_root) { if (turn_token < 0 || min_root <= 0) return 0; for (size_t i = 1; i < n_ids; ++i) if (ids[i] == turn_token) { if ((int64_t) i < min_root) return 0; for (size_t j = i + 1; j < n_ids; ++j) if (ids[j] == turn_token) return (int64_t) i; return 0; } return 0; } inline size_t root_index(const int64_t* lens, size_t n, int64_t root_len) { if (root_len > 0) for (size_t i = 0; i < n; ++i) if (lens[i] == root_len) return i; return 0; } inline size_t eviction_victim(const uint64_t* s, size_t n, int64_t c, const bool* t, const bool* p, size_t) { return eviction_victim(s, n, c, t, p); } inline size_t spread_victim(const int64_t* l, const uint64_t* s, size_t n, int64_t c, const bool* t, const bool* p, size_t) { return spread_victim(l, s, n, c, t, p); } } ``` </details> - Full CPU ctest (`cmake -DCMAKE_BUILD_TYPE=Release -DSTRATA_BUILD_TESTS=ON -DSTRATA_GGML_DIR=<llama.cpp@pin>` then `ctest -j8`): 49 tests - 44 passed (conv_cache_test among them), 1 skipped, 4 failed; see Failed. - CUDA (sm_120, nvcc 13.0.88), HIP (gfx1201, ROCm clang 22) and SYCL (icpx 2026.1.1): the full `strata` target configures and links from this branch (compile and link on the dev box, which has no GPU). - Engine A/B on a GPU machine: RTX 5080 16 GB (driver 595.84), i9-14900K, Linux, Docker (nvidia/cuda:13.0.0-devel, sm_120 build), qwen Q2_0, `--context 32768`, stock settings (tuning skipped, `vram_reserve_mib 4096`; another service held ~3.5 GB of VRAM). A 23,378-token synthetic system prompt, seven appended turns, then a new chat with the same system prompt - base engine (9ae93f7b) vs this branch: base mounts a grid checkpoint, `prompt 23377 tokens = 19456 reused + 3921 read` (1,691 ms); this branch mounts the root, `prompt 23377 tokens = 23346 reused + 31 read` (262 ms). The first reads and every turn's draft-acceptance numbers are identical in both runs; the four receipt lines are quoted verbatim from the engine log, and the full log and replay script are available on request. Failed: the four pre-existing Gyro test failures (isa_guard_check, tq_cpu_parity, tq_cpu_parity_avx2, native_hadamard_test): the default pinned ggml has no trellis types and the guard lacks the tq entries. They are identical at the unpatched base and untouched by this change; #3511 addresses them. Not run: #2228's exact request sequence on the reporter's machine - the A/B above is a synthetic conversation (note the recipe: the new chat must come after at least `--prompt-cache` − 1 checkpoints since the root's last mount, or both builds still show the root; about 11 appends at `--prompt-cache 12`). The reporter offered to run diagnostic builds, and the replay script is ready. Windows (MSVC) and Metal (macOS only; the shared `generate.cpp` compiles into it unchanged) not built. expert_multi_test skipped (no AVX-512 on the dev box). ## Limits - Default-path behaviour changes exactly when a system prompt ends past the first periodic chunk end and the cache goes over budget - that is the fix. Where a mount point changes, the read's chunk boundaries move, so answers after such a mount can differ in last-place rounding (as with any different resume point). - Opt-in paths change even below 16K: a `--prompt-cache-tail` or pin=N checkpoint sitting ahead of the system-prompt root no longer holds the root's pin (the root stays instead). Both changes are in the root's favour. - A conversation with no system turn but two or more boundaries pins the end of its first message - the same position `root_at` picks on a full re-read of such a conversation. A new chat that shares only part of that first message then reads from token 0 where it could previously mount the 16K grid point. - The old grid point at item 0 can rotate out, so a client whose next prompt diverges before the root (for example a system prompt whose tail carries a date) reads from token 0 once per distinct system prompt and then gets a proper root for it. Keeping both pinned was worse in simulation: at `--prompt-cache 2` every new save evicts itself, and one rotating slot is lost at every cap. - Under `--prompt-cache-spread` the first grid point stays (the gap rule never takes item 0), costing one slot; documented in DETAILS.md. - Only reads from token 0 save a root, so a session that lost its root before this change keeps its grid-point chain until the next full read. `--batch` slot hand-back still clears the main chain, as before. - The mechanism analysis is from reading the sources (unconfirmed static analysis) plus policy simulations that reproduce both reporters' numbers; the engine A/B confirms the new-chat case on a synthetic conversation, not the reporters' own sequences. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#4664
No description provided.