okami.mom/strata

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

Niko1221Merge f-clean-3 (321794636): docs and history (cleanup step 3, items 3.0-3.10); release manifest and capability table with checks, history/ with pointers, link check, tools/ split, bench index; owner OK'd the wording 11-082b36e8a02

main
4.0 KiB101 linesraw
1#!/usr/bin/env python3
2"""SAVE / RESTORE round trip with parked conversations holding the host RAM (#1572 / #1573).
3
4  python3 research/tools/live-probes-2026-10/session_pressure_test.py CFG OUTFILE
5  STRATA_EXTRA_ARGS="--prompt-cache 4 --conversation-cache-mib 3072 --conversation-cache-slots 4
6                     --conversation-cache-min-free-mib <floor> [--session-save-reclaim]"
7
8Parks three conversations, runs a fourth (D), SAVEs it, runs an unrelated one, RESTOREs, and checks that D's next turn
9gives the same token ids after the restore as it does when continued live.  Prints SAVE: ok|refused (...) and PASS/FAIL.
10"""
11import json, os, sys, threading, time
12from pathlib import Path
13
14REPO = Path(os.environ.get("STRATA_REPO", Path(__file__).resolve().parents[3]))
15sys.path.insert(0, str(REPO / "tools")); sys.path.insert(0, str(REPO))
16import strata_tokenizer as ST
17from serve import server as S
18from serve.server import StrataEngine, child_env
19
20cfg = json.load(open(sys.argv[1]))
21out_file = sys.argv[2]
22tp = Path(cfg["tokenizer"])
23vocab = json.loads((tp / "vocab.json").read_text())
24toks = [None] * len(vocab)
25for t, i in vocab.items():
26    toks[i] = t
27tok = ST.Tokenizer(toks, (tp / "merges.txt").read_text().split("\n"), json.loads((tp / "token_type.json").read_text()))
28args = cfg["args"] + os.environ.get("STRATA_EXTRA_ARGS", "").split()
29eng = StrataEngine(os.environ.get("STRATA_EXE", cfg["exe"]), args, cwd=cfg.get("cwd"),
30                   log=os.environ.get("SMOKE_LOG", cfg["log"]), env=child_env(cfg))
31GEN = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
32
33
34def enc(text):
35    return tok.encode(text, parse_special=True)
36
37
38def run(ids, max_new=60):
39    out = [t for t in eng.generate(ids, max_new, {"temperature": 0}, threading.Event()) if t is not None]
40    return out, int(eng.last.get("reused") or 0)
41
42
43def chat(tag, n):
44    return enc(f"<|im_start|>user\n[{tag}] " + " ".join(f"Entry {i} of {tag}: value {i * 13 % 97}." for i in range(n))
45               + "\nSummarize in one line." + GEN)
46
47
48for tag in ("alpha", "bravo", "charlie"):
49    p = chat(tag, 70)
50    run(p, 20)
51D = chat("delta", 80)
52outD, _ = run(D, 60)
53print(f"D: prompt {len(D)} tokens, {len(outD)} generated", flush=True)
54nxt = enc("<|im_end|>\n<|im_start|>user\nNow say only the word done." + GEN)
55saved = False
56hog = None
57if os.environ.get("HOG_TARGET_MIB"):   # take the RAM so that only HOG_TARGET_MIB stay available: a SAVE needs more
58    import psutil
59    extra = psutil.virtual_memory().available - int(os.environ["HOG_TARGET_MIB"]) * 2**20
60    if extra > 0:
61        hog = bytearray(extra)
62        for i in range(0, len(hog), 4096):
63            hog[i] = 1
64    print("available now: %d MiB" % (psutil.virtual_memory().available >> 20), flush=True)
65try:
66    r = eng.session_file("save", out_file)
67    saved = True
68    print("SAVE: ok", r, flush=True)
69except S.SessionRefused as e:
70    print("SAVE: refused", e.kind, str(e)[:300], flush=True)
71if not saved and "--session-save-reclaim" in args:
72    # the server's bounded retry on the same engine
73    class Svc:
74        engine = eng
75    from serve.session_save_retry import session_file as retry
76    try:
77        r = retry(Svc, "save", out_file, S.SessionRefused)
78        saved = True
79        print("SAVE after the bounded wait: ok", r, flush=True)
80    except S.SessionRefused as e:
81        print("SAVE after the bounded wait: refused", e.kind, str(e)[:300], flush=True)
82hog = None   # the RAM goes back
83if not saved:
84    print("NOSAVE")
85    eng.proc.stdin.close()
86    sys.exit(2)
87live_next, reused_live = run(D + outD + nxt, 20)
88print(f"D live next turn: reused {reused_live}", flush=True)
89run(chat("echo", 70), 20)   # something else takes the session
90r = eng.session_file("restore", out_file)
91print("RESTORE:", r, flush=True)
92again, reused_r = run(D + outD + nxt, 20)
93print(f"D after RESTORE: reused {reused_r}", flush=True)
94eng.proc.stdin.close()
95try:
96    eng.proc.wait(60)
97except Exception:
98    eng.proc.kill()
99ok = again == live_next and reused_r > 0
100print("PASS" if ok else "FAIL (ids %s)" % ("equal" if again == live_next else "differ"))
101sys.exit(0 if ok else 1)