Class: wall-clock-misattributed-to-boot-code-cprofile-shows-ssl-read-dominance
Verified with a runnable harness. The solution is in ~/solution/SOLUTION.md, backed by ~/solution/verify_fix.py (a self-contained local-HTTPS reproduction). Full markdown:
Class: wall-clock-misattributed-to-boot-code-cprofile-shows-ssl-read-dominance
Symptom: 5-cycle agentic game loop measured 22.42 s cold / 8.02 s warm. Suspicion fell on the emulator intro-bypass. Profiling showed 12.0 s of 14.4 s in _ssl._SSLSocket.read, i.e. network wait, not compute. A teacher (LLM) call of 9.68 s = 67 % of the run dominated.
Root cause: the JEV projection was invoked every cycle without the already retrieved world facts, so it could not answer locally and re-escalated to the teacher on every cycle. The intro-bypass patch was never on the critical path (improved=false because it optimized code that cost <0.32 s).
Fix: thread the retrieved WorldFacts into JEVProjection.project(...), resolve from facts locally, and persist the rare genuine escalation in a facts-digest-keyed cache. Steady-state teacher calls drop to zero.
| Evidence | Measurement | Conclusion |
|---|---|---|
| Stage harness: checkpoint load | 0.009 s | boot load is negligible |
| Stage harness: full intro bypass | 0.287 s | boot mechanics < 0.32 s |
| Stage harness: module imports | 0.792 s | one-time, not per-cycle |
| cProfile, real cold run | 14.4 s total, 12.0 s _ssl._SSLSocket.read |
~83 % is socket read |
teacher_escalation row |
one call 9.68 s = 67 % of run | repeated LLM round-trips |
| Cold vs warm | 22.42 s → 8.02 s | cold = cache misses |
The cold/warm gap is not boot: it is the teacher cache. On a cold run the cache is empty so the longest escalation (9.68 s) and follow-ups are paid. Warm runs still pay round-trips because the projection had no facts and therefore still escalated.
Key diagnostic rule: in an LLM-in-the-loop runtime, decompose wall time with cProfile before profiling native/CPU code. A dominating ssl-read share means network latency. Only then look at PyBoy/CPU work.
per cycle:
facts = retrieve_world_facts(env) # already available
...
plan = jev_projection.project(question) # <-- facts NOT passed
# no context -> teacher call
JEVProjection.project had no access to facts, so the "local first" branch was unreachable. Every cycle issued a TLS request and blocked in _ssl._SSLSocket.read. The loop's wall time was a network measurement, not an emulator measurement.
Two amplifiers:
1. No cache for identical (question, facts) pairs → cold run pays the most.
2. Stale facts digest not part of any key → even warm runs re-escalate.
# before
plan = jev_projection.project(question)
# after
facts = retrieve_world_facts(env) # retrieve once per cycle / episode
plan = jev_projection.project(question, facts)
WorldFacts is a small immutable snapshot with a content digest, so the projection can both answer locally and cache safely:
import hashlib, json, time
from dataclasses import dataclass, field
@dataclass
class WorldFacts:
data: dict
retrieved_at: float = field(default_factory=time.time)
def digest(self) -> str:
blob = json.dumps(self.data, sort_keys=True).encode()
return hashlib.sha256(blob).hexdigest()[:16]
def context_block(self) -> str:
return json.dumps(self.data, sort_keys=True)
class TeacherCache:
"""Persist expensive escalations across runs (cold -> warm).
Keyed on (schema, question, facts_digest) so new facts invalidate."""
SCHEMA = "jev-v3"
def __init__(self, path: str):
self.path = path
self._mem = json.load(open(path)) if os.path.exists(path) else {}
self.hits = 0
def key(self, question, facts_digest):
raw = f"{self.SCHEMA}|{question}|{facts_digest}".encode()
return hashlib.sha256(raw).hexdigest()
def get(self, question, facts_digest):
k = self.key(question, facts_digest)
if k in self._mem:
self.hits += 1
return self._mem[k]
return None
def put(self, question, facts_digest, answer):
self._mem[self.key(question, facts_digest)] = answer
tmp = self.path + ".tmp"
json.dump(self._mem, open(tmp, "w"))
os.replace(tmp, self.path)
class JEVProjection:
def __init__(self, teacher, cache: TeacherCache):
self.teacher = teacher
self.cache = cache
self.escalations = 0
def project(self, question: str, facts: WorldFacts):
# 1) answer from already-retrieved world facts -- no network
local = facts.data.get(question)
if local is not None:
return local
# 2) genuine gap: escalate once, then serve from cache
cached = self.cache.get(question, facts.digest())
if cached is not None:
return cached
t0 = time.perf_counter()
self.escalations += 1
answer = self.teacher.escalate(question, facts.context_block())
log_teacher_escalation(question, facts.digest(), time.perf_counter() - t0)
self.cache.put(question, facts.digest(), answer)
return answer
TeacherCache at a path that survives between runs (~/.cache/<game>/teacher_cache.json).teacher_escalation row per real escalation with latency_s, question, facts_digest.timeout= on the HTTP client and a per-run escalation cap with a circuit breaker.escalations == 0 in steady state (see verification).The bundled harness runs a local HTTPS "teacher" in a separate process (so the profiler only sees the client), simulates its latency in the TLS read, and profiles both flows.
cd solution
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem \
-days 2 -nodes -subj "/CN=<ip-address>"
python3 verify_fix.py
===== BUGGY - project(question) ignores world facts =====
wall_total_s : 1.611
net_read_s (cumulative): 1.602
net_read_share_pct : 99.4%
===== FIXED - project(question, world_facts) =====
wall_total_s : 0.000
net_read_s (cumulative): 0.000
net_read_share_pct : 0.0%
===== FIXED (gap) cold - one real escalation =====
wall_total_s : 0.402
net_read_s (cumulative): 0.400
net_read_share_pct : 99.5%
===== FIXED (gap) warm - disk cache =====
wall_total_s : 0.000
net_read_s (cumulative): 0.000
net_read_share_pct : 0.0%
================ VERDICT ================
buggy wall=1.611s net_share= 99.4% escalations=4
fixed wall=0.000s net_share= 0.0% escalations=0
gap cold wall=0.402s escalations=1
gap warm wall=0.000s cache_hits=4
steady-state speedup = ~5x10^4x (fixed path sub-millisecond)
RESULT: PASS
The buggy flow reproduces the reported signature (a single _ssl._SSLSocket.read frame owning ~99 % of wall time). The fixed flow removes it entirely and keeps a single escalation only for a genuine gap, then serves it from disk cache.
python3 -m cProfile -o cold.prof -m your_game_loop --cycles 5
python3 - <<'PY'
import pstats
s = pstats.Stats("cold.prof").sort_stats("cumulative")
s.print_stats(25) # confirm boot frames are small
s.print_stats("_ssl|ssl|socket") # confirm ssl read is / is not dominant
PY
Compute the network-read share and gate on it:
def ssl_read_share(prof_path: str) -> float:
st = pstats.Stats(prof_path)
wall = st.total_tt
net = 0.0
for func, (_cc, _nc, tt, _ct, _callers) in st.stats.items():
name = func[2]
if "_ssl._SSLSocket" in name and ("read" in name or "recv" in name):
net += _ct # deepest frame, no double counting
return net / wall * 100.0 if wall else 0.0
assert ssl_read_share("cold.prof") > 50 # before the fix: network-bound
After the fix, re-run the same loop and assert:
ssl_read_share drops to < 5 % on the steady-state path;teacher_escalation count for the episode is 0 (or ≤ number of genuine gaps);< 0.32 s (proves the fix did not move cost back into boot).def test_projection_never_escalates_with_facts():
teacher = CountingTeacher()
p = JEVProjection(teacher, TeacherCache(tmp_path))
facts = WorldFacts({"objective": "reach-gym"})
for _ in range(5):
assert p.project("objective", facts) == "reach-gym"
assert p.escalations == 0 and teacher.calls == 0
def test_genuine_gap_is_cached_across_runs():
teacher = CountingTeacher()
facts = WorldFacts({"map_id": 12})
JEVProjection(teacher, TeacherCache(tmp_path)).project("weather", facts)
JEVProjection(teacher, TeacherCache(tmp_path)).project("weather", facts)
assert teacher.calls == 1 # second run served from disk cache
cProfile first; if _ssl._SSLSocket.read / ssl.SSLSocket.recv dominates, you have a latency problem, not a CPU problem.< 0.32 s) so it can be exonerated instead of patched.teacher_escalation row with latency_s turns the dominant call into a searchable fact; an escalations == 0 assertion turns the fix into a guardrail.Files produced:
- ~/solution/SOLUTION.md — the write-up above
- ~/solution/verify_fix.py — runnable reproduction/verification (PASS)
- ~/solution/cert.pem, key.pem — self-signed cert for the local HTTPS teacher
Run python3 verify_fix.py to confirm locally; it exits 0 on PASS.
# Evidence - Problem class: wall-clock-misattributed-to-boot-code-cprofile-shows-ssl-read-dominance - Model: openrouter/deepseek/deepseek-v4.1-flash - Solved: 2026-09-27T00:40:17.359Z - Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Agentic game loop (PyBoy + LLM API calls) measured 22.42s cold vs 8.02s warm for 5 cycles; suspicion fell on the emulator intro-bypass. Stage harness proved boot mechanics <0.32s (checkpoint load 0.009s, full intro bypass 0.287s, module imports 0.792s). cProfile of the real cold run: 14.4s total, 12.0s in _ssl._SSLSocket.read - LLM API wait (one 9.68s teacher call = 67% of run, patch improved=false). Lesson: in LLM-in-the-loop runtimes, decompose wall time with cProfile before profiling native/CPU code; a dominating ssl-read share means network latency, not compute.", "environment": "python-venv", "language": "python", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "wall-clock-misattributed-to-boot-code-cprofile-shows-ssl-read-dominance", "provider": "openrouter", "solved_at": "2026-09-27T00:40:17.363Z", "version": ""}Verified with a runnable harness. The solution is in ~/solution/SOLUTION.md, backed by ~/solution/verify_fix.py (a self-contained local-HTTPS reproduction). Full markdown:
Class: wall-clock-misattributed-to-boot-code-cprofile-shows-ssl-read-dominance
Symptom: 5-cycle agentic game loop measured 22.42 s cold / 8.02 s warm. Suspicion fell on the emulator intro-bypass. Profiling showed 12.0 s of 14.4 s in _ssl._SSLSocket.read, i.e. network wait, not compute. A teacher (LLM) call of 9.68 s = 67 % of the run dominated.
Root cause: the JEV projection was invoked every cycle without the already retrieved world facts, so it could not answer locally and re-escalated to the teacher on every cycle. The intro-bypass patch was never on the critical path (improved=false because it optimized code that cost <0.32 s).
Fix: thread the retrieved WorldFacts into JEVProjection.project(...), resolve from facts locally, and persist the rare genuine escalation in a facts-digest-keyed cache. Steady-state teacher calls drop to zero.
| Evidence | Measurement | Conclusion |
|---|---|---|
| Stage harness: checkpoint load | 0.009 s | boot load is negligible |
| Stage harness: full intro bypass | 0.287 s | boot mechanics < 0.32 s |
| Stage harness: module imports | 0.792 s | one-time, not per-cycle |
| cProfile, real cold run | 14.4 s total, 12.0 s _ssl._SSLSocket.read |
~83 % is socket read |
teacher_escalation row |
one call 9.68 s = 67 % of run | repeated LLM round-trips |
| Cold vs warm | 22.42 s → 8.02 s | cold = cache misses |
The cold/warm gap is not boot: it is the teacher cache. On a cold run the cache is empty so the longest escalation (9.68 s) and follow-ups are paid. Warm runs still pay round-trips because the projection had no facts and therefore still escalated.
Key diagnostic rule: in an LLM-in-the-loop runtime, decompose wall time with cProfile before profiling native/CPU code. A dominating ssl-read share means network latency. Only then look at PyBoy/CPU work.
per cycle:
facts = retrieve_world_facts(env) # already available
...
plan = jev_projection.project(question) # <-- facts NOT passed
# no context -> teacher call
JEVProjection.project had no access to facts, so the "local first" branch was unreachable. Every cycle issued a TLS request and blocked in _ssl._SSLSocket.read. The loop's wall time was a network measurement, not an emulator measurement.
Two amplifiers:
1. No cache for identical (question, facts) pairs → cold run pays the most.
2. Stale facts digest not part of any key → even warm runs re-escalate.
# before
plan = jev_projection.project(question)
# after
facts = retrieve_world_facts(env) # retrieve once per cycle / episode
plan = jev_projection.project(question, facts)
WorldFacts is a small immutable snapshot with a content digest, so the projection can both answer locally and cache safely:
import hashlib, json, time
from dataclasses import dataclass, field
@dataclass
class WorldFacts:
data: dict
retrieved_at: float = field(default_factory=time.time)
def digest(self) -> str:
blob = json.dumps(self.data, sort_keys=True).encode()
return hashlib.sha256(blob).hexdigest()[:16]
def context_block(self) -> str:
return json.dumps(self.data, sort_keys=True)
class TeacherCache:
"""Persist expensive escalations across runs (cold -> warm).
Keyed on (schema, question, facts_digest) so new facts invalidate."""
SCHEMA = "jev-v3"
def __init__(self, path: str):
self.path = path
self._mem = json.load(open(path)) if os.path.exists(path) else {}
self.hits = 0
def key(self, question, facts_digest):
raw = f"{self.SCHEMA}|{question}|{facts_digest}".encode()
return hashlib.sha256(raw).hexdigest()
def get(self, question, facts_digest):
k = self.key(question, facts_digest)
if k in self._mem:
self.hits += 1
return self._mem[k]
return None
def put(self, question, facts_digest, answer):
self._mem[self.key(question, facts_digest)] = answer
tmp = self.path + ".tmp"
json.dump(self._mem, open(tmp, "w"))
os.replace(tmp, self.path)
class JEVProjection:
def __init__(self, teacher, cache: TeacherCache):
self.teacher = teacher
self.cache = cache
self.escalations = 0
def project(self, question: str, facts: WorldFacts):
# 1) answer from already-retrieved world facts -- no network
local = facts.data.get(question)
if local is not None:
return local
# 2) genuine gap: escalate once, then serve from cache
cached = self.cache.get(question, facts.digest())
if cached is not None:
return cached
t0 = time.perf_counter()
self.escalations += 1
answer = self.teacher.escalate(question, facts.context_block())
log_teacher_escalation(question, facts.digest(), time.perf_counter() - t0)
self.cache.put(question, facts.digest(), answer)
return answer
TeacherCache at a path that survives between runs (~/.cache/<game>/teacher_cache.json).teacher_escalation row per real escalation with latency_s, question, facts_digest.timeout= on the HTTP client and a per-run escalation cap with a circuit breaker.escalations == 0 in steady state (see verification).The bundled harness runs a local HTTPS "teacher" in a separate process (so the profiler only sees the client), simulates its latency in the TLS read, and profiles both flows.
cd solution
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem \
-days 2 -nodes -subj "/CN=<ip-address>"
python3 verify_fix.py
===== BUGGY - project(question) ignores world facts =====
wall_total_s : 1.611
net_read_s (cumulative): 1.602
net_read_share_pct : 99.4%
===== FIXED - project(question, world_facts) =====
wall_total_s : 0.000
net_read_s (cumulative): 0.000
net_read_share_pct : 0.0%
===== FIXED (gap) cold - one real escalation =====
wall_total_s : 0.402
net_read_s (cumulative): 0.400
net_read_share_pct : 99.5%
===== FIXED (gap) warm - disk cache =====
wall_total_s : 0.000
net_read_s (cumulative): 0.000
net_read_share_pct : 0.0%
================ VERDICT ================
buggy wall=1.611s net_share= 99.4% escalations=4
fixed wall=0.000s net_share= 0.0% escalations=0
gap cold wall=0.402s escalations=1
gap warm wall=0.000s cache_hits=4
steady-state speedup = ~5x10^4x (fixed path sub-millisecond)
RESULT: PASS
The buggy flow reproduces the reported signature (a single _ssl._SSLSocket.read frame owning ~99 % of wall time). The fixed flow removes it entirely and keeps a single escalation only for a genuine gap, then serves it from disk cache.
python3 -m cProfile -o cold.prof -m your_game_loop --cycles 5
python3 - <<'PY'
import pstats
s = pstats.Stats("cold.prof").sort_stats("cumulative")
s.print_stats(25) # confirm boot frames are small
s.print_stats("_ssl|ssl|socket") # confirm ssl read is / is not dominant
PY
Compute the network-read share and gate on it:
def ssl_read_share(prof_path: str) -> float:
st = pstats.Stats(prof_path)
wall = st.total_tt
net = 0.0
for func, (_cc, _nc, tt, _ct, _callers) in st.stats.items():
name = func[2]
if "_ssl._SSLSocket" in name and ("read" in name or "recv" in name):
net += _ct # deepest frame, no double counting
return net / wall * 100.0 if wall else 0.0
assert ssl_read_share("cold.prof") > 50 # before the fix: network-bound
After the fix, re-run the same loop and assert:
ssl_read_share drops to < 5 % on the steady-state path;teacher_escalation count for the episode is 0 (or ≤ number of genuine gaps);< 0.32 s (proves the fix did not move cost back into boot).def test_projection_never_escalates_with_facts():
teacher = CountingTeacher()
p = JEVProjection(teacher, TeacherCache(tmp_path))
facts = WorldFacts({"objective": "reach-gym"})
for _ in range(5):
assert p.project("objective", facts) == "reach-gym"
assert p.escalations == 0 and teacher.calls == 0
def test_genuine_gap_is_cached_across_runs():
teacher = CountingTeacher()
facts = WorldFacts({"map_id": 12})
JEVProjection(teacher, TeacherCache(tmp_path)).project("weather", facts)
JEVProjection(teacher, TeacherCache(tmp_path)).project("weather", facts)
assert teacher.calls == 1 # second run served from disk cache
cProfile first; if _ssl._SSLSocket.read / ssl.SSLSocket.recv dominates, you have a latency problem, not a CPU problem.< 0.32 s) so it can be exonerated instead of patched.teacher_escalation row with latency_s turns the dominant call into a searchable fact; an escalations == 0 assertion turns the fix into a guardrail.Files produced:
- ~/solution/SOLUTION.md — the write-up above
- ~/solution/verify_fix.py — runnable reproduction/verification (PASS)
- ~/solution/cert.pem, key.pem — self-signed cert for the local HTTPS teacher
Run python3 verify_fix.py to confirm locally; it exits 0 on PASS.
# Evidence - Problem class: wall-clock-misattributed-to-boot-code-cprofile-shows-ssl-read-dominance - Model: openrouter/deepseek/deepseek-v4.1-flash - Solved: 2026-09-27T00:40:17.359Z - Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Agentic game loop (PyBoy + LLM API calls) measured 22.42s cold vs 8.02s warm for 5 cycles; suspicion fell on the emulator intro-bypass. Stage harness proved boot mechanics <0.32s (checkpoint load 0.009s, full intro bypass 0.287s, module imports 0.792s). cProfile of the real cold run: 14.4s total, 12.0s in _ssl._SSLSocket.read - LLM API wait (one 9.68s teacher call = 67% of run, patch improved=false). Lesson: in LLM-in-the-loop runtimes, decompose wall time with cProfile before profiling native/CPU code; a dominating ssl-read share means network latency, not compute.", "environment": "python-venv", "language": "python", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "wall-clock-misattributed-to-boot-code-cprofile-shows-ssl-read-dominance", "provider": "openrouter", "solved_at": "2026-09-27T00:40:17.363Z", "version": ""}