fix(skillopt-sleep): surface codex auth/model/version failures instead of silently scoring 0

A nightly sleep cycle could run for weeks emitting held-out 0.0 -> 0.0 (gate reject, zero
edits), indistinguishable from "nothing to learn", when the real cause was the codex backend
returning an error (expired auth / model unsupported on the account / outdated CLI) that got
scored as a failed rollout.

backend (CodexCliBackend):
- split _call into _call_once + a retry wrapper: transient empties/timeouts are retried
  instead of silently returning "" (mirrors AzureOpenAIBackend's guard);
- on a non-zero exit, surface the reason via last_call_error and return "" rather than
  leaking the CLI error text as if it were a model response;
- fail fast (no retries) on fatal auth/model/version errors (401, refresh_token_reused,
  token_expired, "not supported when using Codex with a ChatGPT account",
  "requires a newer version of Codex").
backend (CliBackend.reflect): retain last_reflect_raw so a no-edits night is diagnosable.
consolidate: ConsolidationResult now carries per-task held-out detail (response, hard/soft,
  fail_reason) + reflect_raw + call_error.
cycle: write diagnostics.json per cycle so a 0.0 night self-explains instead of being a black box.
tests: 4 new (retry-not-silent-zero, auth-error-surfaced-not-scored, holdout-detail, reflect-raw).

Also gitignore the .skillopt-sleep/ runtime dir.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Daniel Martinez
2026-06-27 22:23:19 -05:00
parent 9969a8f393
commit 9fcf5868c3
5 changed files with 196 additions and 3 deletions
+90
View File
@@ -486,6 +486,18 @@ class TestConsolidateGate(unittest.TestCase):
self.assertTrue(res.accepted)
self.assertGreater(res.candidate_score, res.baseline_score)
def test_consolidate_records_holdout_detail(self):
# observability: a 0.0 night must carry per-task evidence (was empty
# response vs failing checks?) so it is diagnosable, not a black box.
be = MockBackend()
tasks = assign_splits(researcher_persona(), holdout_fraction=0.34, seed=42)
res = consolidate(be, tasks, set_learned("", []), "", edit_budget=4,
gate_metric="mixed", night=1)
self.assertTrue(res.holdout_detail) # non-empty per-task rows
row = res.holdout_detail[0]
for k in ("id", "hard", "soft", "response_len", "why"):
self.assertIn(k, row)
def test_no_op_when_already_optimal(self):
be = MockBackend()
tasks = assign_splits(programmer_persona(), holdout_fraction=0.34, seed=1)
@@ -612,6 +624,24 @@ class TestMultiObjectiveAndPrefs(unittest.TestCase):
[], "skill", "", edit_budget=2, evolve_skill=True, evolve_memory=False)
self.assertIn("British English", captured["prompt"])
def test_reflect_records_last_raw(self):
# the optimizer's raw reply must be retained so a no-edits night is
# diagnosable (empty/non-JSON reflect vs genuinely no failures).
from skillopt_sleep.backend import CliBackend
from skillopt_sleep.types import ReplayResult
class CapBackend(CliBackend):
name = "cap"
def _call(self, prompt, *, max_tokens=1024):
return '[{"op":"add","content":"a learned rule","rationale":"x"}]'
be = CapBackend()
t = TaskRecord(id="t", project="/p", intent="x", reference_kind="rule",
judge={"checks": [{"op": "contains", "arg": "z"}]})
be.reflect([(t, ReplayResult(id="t", hard=0.0, fail_reason="failed: contains=z"))],
[], "skill", "", edit_budget=2, evolve_skill=True, evolve_memory=False)
self.assertIn("a learned rule", be.last_reflect_raw)
def test_replay_records_cost(self):
from skillopt_sleep.backend import MockBackend
from skillopt_sleep.replay import replay_one
@@ -654,6 +684,66 @@ class TestCodexBackend(unittest.TestCase):
self.assertIn("-C", cmd)
self.assertEqual(cmd[cmd.index("-C") + 1], expected_project)
def test_codex_call_retries_transient_failure_not_silent_zero(self):
"""A transient timeout must be RETRIED, not silently returned as "" — an
empty reply scores 0 on every judge and zeroes the held-out baseline,
making a flaky backend look identical to 'nothing to learn'."""
import subprocess as _sp
from skillopt_sleep.backend import CodexCliBackend
calls = {"n": 0}
def fake_run(cmd, **kwargs):
calls["n"] += 1
if calls["n"] == 1:
raise _sp.TimeoutExpired(cmd, kwargs.get("timeout", 1))
out_path = cmd[cmd.index("-o") + 1]
with open(out_path, "w", encoding="utf-8") as f:
f.write("real answer")
class Proc:
returncode = 0
stdout = ""
stderr = ""
return Proc()
backend = CodexCliBackend(codex_path="codex")
with mock.patch("skillopt_sleep.backend.subprocess.run", side_effect=fake_run), \
mock.patch("time.sleep", lambda *_a, **_k: None):
out = backend._call("hello")
self.assertEqual(out, "real answer") # recovered on retry
self.assertGreaterEqual(calls["n"], 2) # proves it did not silently return "" once
def test_codex_auth_error_surfaces_not_scored_as_response(self):
"""An auth 401 must become a clear last_call_error + EMPTY response (not the
9k-char error text scored as a 0 'answer'), and must NOT be retried — the
exact failure that silently stalled learning (refresh_token_reused)."""
from skillopt_sleep.backend import CodexCliBackend
calls = {"n": 0}
def fake_run(cmd, **kwargs):
calls["n"] += 1
out_path = cmd[cmd.index("-o") + 1]
open(out_path, "w").close() # empty output file (codex wrote nothing)
class Proc:
returncode = 1
stdout = ""
stderr = "ERROR codex_core::auth: 401 Unauthorized: refresh_token_reused"
return Proc()
be = CodexCliBackend(codex_path="codex")
with mock.patch("skillopt_sleep.backend.subprocess.run", side_effect=fake_run), \
mock.patch("time.sleep", lambda *_a, **_k: None):
out = be._call("hi")
self.assertEqual(out, "") # NOT the error text
self.assertIn("refresh_token_reused", be.last_call_error) # surfaced for the operator
self.assertEqual(calls["n"], 1) # failed fast, no wasted retries
class TestMultiRolloutAndBudget(unittest.TestCase):
def test_rolloutset_stats(self):