okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-142-engine2' into f-142e85f56209

main
7.0 KiB115 linesraw
1// CPU only: pressure is injected through telemetry, not by exhausting the host.
2#include "strata/core/session_save_reclaim.hpp"
3#include <cstdio>
4#include <cstdlib>
5#include <new>
6
7static bool deny_alloc = false;
8void* operator new(std::size_t n) {
9    if (deny_alloc) throw std::bad_alloc();
10    if (void* p = std::malloc(n ? n : 1)) return p;
11    throw std::bad_alloc();
12}
13void operator delete(void* p) noexcept { std::free(p); }
14void operator delete(void* p, std::size_t) noexcept { std::free(p); }
15
16using namespace strata::core;
17static int checks_run = 0;
18static void check(bool ok, const char* label) {
19    ++checks_run;
20    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
21}
22static ConversationCheckpoint checkpoint(size_t n, bool pinned = false) {
23    ConversationCheckpoint c;
24    c.ids.assign(n, 42); c.imgs = {{1, 99}};
25    c.gdn.assign(4096, 1); c.ple = {2}; c.tails = {3}; c.dead = {4}; c.block_pos = {5};
26    c.used = n; c.pinned = pinned;
27    return c;
28}
29static bool same(const ConversationCheckpoint& a, const ConversationCheckpoint& b) {
30    return a.ids == b.ids && a.imgs == b.imgs && a.gdn == b.gdn && a.ple == b.ple &&
31           a.tails == b.tails && a.dead == b.dead && a.block_pos == b.block_pos &&
32           a.used == b.used && a.pinned == b.pinned && a.stage_parts.size() == b.stage_parts.size();
33}
34static void park(ConversationCache& cache, int tokens, bool pin = false) {
35    SavedConversation c; c.live = checkpoint(tokens);
36    c.checkpoints.push_back(checkpoint(tokens - 1, pin));
37    // Distinct histories, so put() does not remove superseded entries.
38    c.live.ids[0] = tokens; c.checkpoints[0].ids[0] = tokens;
39    check(cache.put(std::move(c)), "fixture parked");
40}
41int main() {
42    SessionSaveLive live; live.state_bytes = 1000; live.tokens = 100; live.images = 1; live.kv_layers = 2;
43    const uint64_t floor = 1u << 20;
44    // Every selected position, first-tie preference, and explicit pins (a pin can be shallower).
45    for (size_t selected = 0; selected < 4; ++selected) for (bool pin : {false, true}) {
46        std::vector<ConversationCheckpoint> chain;
47        for (size_t i = 0; i < 4; ++i) chain.push_back(checkpoint(i == selected ? 300 : 100, pin && i == selected));
48        if (pin) chain[(selected + 1) % 4].ids.resize(500); // deeper, unpinned
49        auto before = *session_deepest_checkpoint(chain);
50        const auto* storage = session_deepest_checkpoint(chain)->gdn.data();
51        ConversationCache cache(1u << 20, 10);
52        park(cache, 10); park(cache, 20, true); park(cache, 30);
53        std::vector<ConversationKv> kv(1); kv[0].k.resize(16384); cache.retain(std::move(kv), 100);
54        const size_t bytes_before = cache.bytes();
55        size_t chain_bytes = 0; for (const auto& c : chain) chain_bytes += c.bytes();
56        deny_alloc = true;
57        auto freed = session_save_reclaim(chain, cache, live, floor, [] { return std::optional<uint64_t>(0); });
58        deny_alloc = false;
59        check(chain.size() == 1 && same(chain[0], before), "selected checkpoint unchanged under pressure");
60        check(chain[0].gdn.data() == storage, "checkpoint moved without copying storage");
61        check(freed.checkpoints == 3 && freed.parked == 2 && cache.size() == 1, "pins survive, other entries reclaimed");
62        check(cache.evictions() == 2 && cache.retained_bytes() == 0, "cache accounting and retained buffers");
63        check(freed.bytes == bytes_before - cache.bytes() + chain_bytes - chain[0].bytes(), "released byte accounting");
64        check(session_deepest_checkpoint(chain)->ids == before.ids, "same file selection after reclaim");
65        auto again = session_save_reclaim(chain, cache, live, floor, [] { return std::optional<uint64_t>(0); });
66        check(again.bytes == 0 && again.checkpoints == 0 && again.parked == 0, "exhausted reclaim is idempotent");
67    }
68    // No-pressure path, unknown telemetry, overflowed estimate: no cache mutation.
69    for (int mode = 0; mode < 4; ++mode) {
70        std::vector<ConversationCheckpoint> chain{checkpoint(100), checkpoint(300)};
71        ConversationCache cache(1u << 20, 10); park(cache, 10);
72        const auto before = chain;
73        const auto bytes = cache.bytes(); auto sl = live;
74        if (mode == 2) sl.state_bytes = UINT64_MAX;
75        const uint64_t need = session_save_peak_bytes(session_deepest_checkpoint(chain), sl);
76        auto r = session_save_reclaim(chain, cache, sl, floor, [&]() -> std::optional<uint64_t> {
77            if (mode == 1) return std::nullopt;
78            return mode == 2 ? 0 : need + floor + (mode == 3 ? 1 : 0);
79        });
80        check(!r.bytes && !r.parked && !r.checkpoints && cache.bytes() == bytes, "no eviction without known pressure");
81        check(chain.size() == 2 && same(chain[0], before[0]) && same(chain[1], before[1]), "unchanged order/content");
82    }
83    // Admit after any individual step; never clear the whole cache when one release suffices.
84    for (int sufficient_after = 1; sufficient_after <= 4; ++sufficient_after) {
85        std::vector<ConversationCheckpoint> chain{checkpoint(100), checkpoint(300), checkpoint(300)};
86        auto* selected_storage = chain[1].gdn.data();
87        ConversationCache cache(1u << 20, 10); park(cache, 10); park(cache, 20);
88        std::vector<ConversationKv> kv(1); kv[0].v.resize(100); cache.retain(std::move(kv), 100);
89        const auto need = session_save_peak_bytes(session_deepest_checkpoint(chain), live);
90        int probes = 0;
91        auto r = session_save_reclaim(chain, cache, live, floor, [&]() -> std::optional<uint64_t> {
92            return probes++ >= sufficient_after ? need + floor : 0;
93        });
94        check(probes == sufficient_after + 1, "stop immediately once RAM meets unchanged floor");
95        check(r.parked == size_t(std::min(sufficient_after - 1, 2)), "one-at-a-time parked eviction");
96        check(r.checkpoints == size_t(sufficient_after == 4), "checkpoint eviction only after parked cache");
97        check(session_deepest_checkpoint(chain)->gdn.data() == selected_storage, "first deepest tie retained");
98    }
99    {
100        std::vector<ConversationCheckpoint> chain{checkpoint(100, true), checkpoint(300, true), checkpoint(500)};
101        ConversationCache cache(0, 0);
102        auto r = session_save_reclaim(chain, cache, live, floor, [] { return std::optional<uint64_t>(0); });
103        check(r.checkpoints == 1 && chain.size() == 2 && chain[0].pinned && chain[1].pinned, "all pins survive in original order");
104        std::vector<ConversationCheckpoint> out; std::string why;
105        check(!session_save_checkpoints(chain, live, [](uint64_t, std::string& w) { w = "low RAM"; return false; }, out, why)
106              && out.empty() && why == "low RAM", "still refused before copying when reclamation is insufficient");
107    }
108    {
109        std::vector<ConversationCheckpoint> chain;
110        ConversationCache cache(0, 0);
111        auto r = session_save_reclaim(chain, cache, live, floor, [] { return std::optional<uint64_t>(0); });
112        check(!r.bytes && !r.checkpoints && !r.parked, "empty chain and disabled cache");
113    }
114    std::printf("session_save_reclaim: %d checks passed\n", checks_run);
115}