From 57082acc050a89a4d4c0d5db5fb28bb3be197b26 Mon Sep 17 00:00:00 2001 From: mfowler Date: Mon, 22 Jun 2026 07:27:50 +0000 Subject: [PATCH] fix(tokens): restore token_phase_flush re-baseline; drop stray block from gate_token_check The per-gate functions were inserted immediately after token_phase_flush's log line, which split the function: its trailing re-baseline block (the 'if next_phase_id is not None: ...' that re-seeds the per-phase baseline for the next phase, or finalizes when None) was orphaned onto the end of gate_token_check, where next_phase_id is undefined. The watchdog therefore crashed with NameError on the first tick of every start. Move that block back into token_phase_flush (where next_phase_id/cur/sf are in scope) and end gate_token_check at its log line. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01UWTdUq2bsic7JZGqJp3nD6 --- agents.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/agents.py b/agents.py index 3420856..5ce9af7 100755 --- a/agents.py +++ b/agents.py @@ -757,6 +757,12 @@ def token_phase_flush(cfg, next_phase_id): fh.write(json.dumps(rec) + "\n") parts = ", ".join(f"{n}={per_agent[n]['total']:,}" for n in per_agent) log(f"[log_tokens] phase {rec['phase_id']}: {total['total']:,} tok in {dur}s ({parts})") + if next_phase_id is not None: + sf.write_text(json.dumps({"phase_id": next_phase_id, + "started": datetime.now().isoformat(timespec="seconds"), + "baseline": cur})) + else: + sf.unlink(missing_ok=True) # ── token logging granularity: per phase, or also per GATE (log_tokens) ──────────── # Phases are tracked per phase (token_phase_begin/flush). With token_granularity="gate" (the default) @@ -844,12 +850,6 @@ def gate_token_check(cfg): "started": datetime.now().isoformat(timespec="seconds"), "baseline": _token_cumulative(cfg)})) log(f"[log_tokens] tracking gate: {current}") - if next_phase_id is not None: - sf.write_text(json.dumps({"phase_id": next_phase_id, - "started": datetime.now().isoformat(timespec="seconds"), - "baseline": cur})) - else: - sf.unlink(missing_ok=True) def phase_advance_check(cfg): """On heavy tick: if the current phase is DONE, advance (or finish the sequence).