import sqlite3, time, datetime, sys, re, urllib.request, json # compaction-anticipation organ v2 (keeper correction 2026-10-19T03:3xZ: "you've said runway 0 # more than a few times. it's gonna fire soon but your math is off"). Three fixes, each priced # by the census logged in CORPUS same family: # FIX 1 chain attribution: the engine log is ENGINE-WIDE. v1 read the last prompt line and # attributed it to the current session; at seams that line is the DEAD session's peak # (observed 184,572) -> printed runway 0 while the live chain had ~110k spare. # v2 links calls into chains via the reused field (chain continues iff # reused ~= prev prompt+generated, slack 4096) and reads only the LAST chain. # FIX 2 trigger base: harness-0.9-era dense-chain peaks (n=3, post keeper raise): # 183,819 / 184,463 / 184,572 -> median 184,463 = 70.4 pct of n_ctx. # v1 used 65.5 pct (scaled from the chars/4 COMPACT-CEILING census -- wrong surface). # Harness-0.8-era dense-chain peaks (n=7): median 164,466 = 62.5 pct -- matches BREATH-WINDOW band. # FIX 3 seam detection: a chain that starts with reused < 32768 is a checkpoint restore # (post-compaction rebuild); printed as POST-SEAM so a fresh runway is never read as 0. SID = sys.argv[1] if len(sys.argv) > 1 else None DB = r"C:/Users/user/AppData/Local/hermes/state.db" STRATA_LOG = r"C:/code/Strata/strata-swift-iq3_xxs.log" PROPS_URL = "http://127.0.0.1:8080/props" con = sqlite3.connect(DB); cur = con.cursor() if not SID: # newest live session SID = cur.execute("SELECT id FROM sessions ORDER BY started_at DESC LIMIT 1").fetchone()[0] u = cur.execute("SELECT api_call_count, input_tokens, output_tokens FROM session_model_usage WHERE session_id=?", (SID,)).fetchone() calls, in_tok, out_tok = (u[0], u[1], u[2]) if u else (0, 0, 0) started = cur.execute("SELECT started_at FROM sessions WHERE id=?", (SID,)).fetchone()[0] con.close() now = time.time() print(f"session {SID} age={round((now-started)/60,1)} min calls={calls}") props = json.loads(urllib.request.urlopen(PROPS_URL, timeout=10).read().decode()) n_ctx = props["default_generation_settings"]["n_ctx"] pat = re.compile(r"prompt (\d+) tokens = (\d+) reused(?: \+ (\d+)(?: of \d+)? read)?") gen = re.compile(r"(\d+) generated") chains = []; cur_chain = []; prev_end = None; prev_prompt = None for line in open(STRATA_LOG, encoding="utf-8", errors="replace"): m = pat.search(line) if not m: continue p = int(m.group(1)); reused = int(m.group(2)) g = gen.search(line); g = int(g.group(1)) if g else 0 if not cur_chain: cur_chain = [(p, g, reused)]; prev_end = p + g; prev_prompt = p; continue # chain continues iff reused anchors to prev prompt+generated (normal) OR prev prompt # (truncation-replay after a cancelled gen=0 call; keeper anatomy 2026-10-19T03:4xZ). # reused==0 is a cold read (summarizer turn / new session): hard break. if reused > 0 and (abs(reused - prev_end) <= 4096 or abs(reused - prev_prompt) <= 4096): cur_chain.append((p, g, reused)); prev_end = p + g; prev_prompt = p else: chains.append(cur_chain); cur_chain = [(p, g, reused)]; prev_end = p + g; prev_prompt = p if cur_chain: chains.append(cur_chain) chain = chains[-1] start_prompt, start_reused = chain[0][0], chain[0][2] used = chain[-1][0] + chain[-1][1] TRIGGER = round(n_ctx * 0.704) runway = max(TRIGGER - used, 0) print(f"chains in log: {len(chains)} | reading LAST chain: len={len(chain)} start_prompt={start_prompt} start_reused={start_reused}") if start_reused < 32768: print(f"POST-SEAM chain (checkpoint restore, reused={start_reused}): fresh context; runway is real, not 0.") # KEEPER ANATOMY (datum 2026-10-19T03:4xZ): at a fresh seam, n-1 in the log is the # summarizer's turn (cold read: reused=0, gen = summary length) and before that the # previous window's peak. Verified against 03:33Z seam: prev peak 184,572; summarizer # prompt 76,608 = 0 reused + 76,608 read, 24,761 generated (matches state.db # compression row in/out EXACTLY); fresh start 62,709 = 16,384 reused. # Chain entries are (prompt, generated, reused) -- unpack in that order. prev_peak = max(p+g for p,g,_ in chains[-3]) if len(chains) >= 3 else None summ = None for p,g,rw in reversed(chains[-2] if len(chains)>=2 else []): if rw == 0 and g > 512: summ = (p, g); break if prev_peak is not None: print(f"seam anatomy: previous window peak={prev_peak} ({round(100*prev_peak/n_ctx,1)} pct of n_ctx)") if summ: print(f"summarizer turn (n-1): prompt={summ[0]} cold-read, generated={summ[1]} (summary tokens)") # live seam-firing signature: last entry cancelled (gen=0) at a high prompt -> harness is # mid-trigger NOW; next lines are truncation-replay then summarizer. Press-ready NOW. if chain[-1][1] == 0 and chain[-1][0] > TRIGGER * 0.95: print(f"SEAM FIRING NOW: in-flight call cancelled at prompt={chain[-1][0]} (gen=0). Flush press-ready contract.") print(f"n_ctx={n_ctx} chain_used={used} (last call prompt+generated in THIS chain)") print(f"compaction_trigger_estimate={TRIGGER} (70.4 pct of n_ctx = harness threshold 0.9; census n=3 dense chains post-raise: 183,819/184,463/184,572 median 184,463)") print(f"tokens_to_next_compaction_estimate={runway}") # secondary surface (state.db usage): firing-point census of compression calls con = sqlite3.connect(DB); cur = con.cursor() rows = cur.execute("SELECT input_tokens FROM session_model_usage WHERE task='compression' ORDER BY first_seen DESC LIMIT 12").fetchall() con.close() fp = [r[0] for r in rows if r[0]] if fp: import statistics as st print(f"usage-surface firing points (compression-call input, n={len(fp)}): median={round(st.median(fp))} min={min(fp)} max={max(fp)} (summarizer sees the whole conversation; ratio to engine peak ~2.4x)")