"""Shared helpers: run schedule, thermal snapshots, time budget, output checks.""" import json import re import subprocess import time MAX_TOKENS = 256 SEED = 42 # Latin square: each context length appears once in every position across the 3 reps, # so thermal drift within a config doesn't favour one length. SCHEDULE = [(1, [512, 16384, 4096]), (2, [4096, 512, 16384]), (3, [16384, 4096, 512])] def thermal(): """Raw `pmset -g therm` text plus parsed CPU_Speed_Limit if present.""" try: out = subprocess.run(["pmset", "-g", "therm"], capture_output=True, text=True, timeout=5).stdout.strip() except Exception as e: # pmset missing / not permitted return {"raw": f"unavailable: {e}", "cpu_speed_limit": None} m = re.search(r"CPU_Speed_Limit\s*=\s*(\d+)", out) return {"raw": out, "cpu_speed_limit": int(m.group(1)) if m else None} def load_prompts(cfg): with open(f"prompts/{cfg}.json") as f: return json.load(f) class Budget: """Skip a run if its estimated duration would overrun this config's deadline.""" def __init__(self, deadline_unix): self.deadline = deadline_unix self.seen = {} # ctx -> last wall seconds def estimate(self, ctx): if ctx in self.seen: return self.seen[ctx] if 4096 in self.seen and ctx == 16384: return self.seen[4096] * 4.5 return 0.0 def allows(self, ctx): return time.time() + self.estimate(ctx) < self.deadline def record(self, ctx, wall): self.seen[ctx] = wall def output_checks(text): """Cheap sanity checks on a completion: bullet count and degeneracy.""" lines = [l.strip() for l in text.splitlines() if l.strip()] bullets = [l for l in lines if re.match(r"^([-*•]|\d+[.)])\s+", l)] words = text.split() distinct = len(set(words)) / len(words) if words else 0.0 # longest run of an identical 8-word window repeating back-to-back grams = [" ".join(words[i:i + 8]) for i in range(max(0, len(words) - 7))] repeated = len(grams) - len(set(grams)) return { "bullet_lines": len(bullets), "has_bullets": len(bullets) >= 3, "distinct_word_ratio": round(distinct, 3), "repeated_8gram_count": repeated, "degenerate": bool(words) and (distinct < 0.2 or repeated > 20), "chars": len(text), }