#!/usr/bin/env python3 """Fix for the /train page log parser (found during verification 2026-11-20): LeRobot humanizes the step counter in metric lines — "step:1K", "step:10K" from step 1000 on (steps 1000/1010/1020 all print "1K"), so the v1 parser only recovered steps 10..990 (99 of 1000 points on the previous jedi log). The exact step is present in the tqdm fragment glued to the same physical line ("1230/60000 ["). Fix: resolve the step from the tqdm fragment first, fall back to a plain-integer step field, else infer from the previous step plus the observed logging interval. Exact-match asserted; backup at server.py.bak-trainpagefix-20261120. """ import py_compile import shutil from pathlib import Path P = Path.home() / "sera_v4_dashboard" / "server.py" src = P.read_text() orig = src R1_OLD = r'''_L60K_STEP_RE = re.compile(r"\bstep:(\d+)\s+smpl:")''' R1_NEW = r'''_L60K_METRIC_RE = re.compile(r"\bstep:(\S+)\s+smpl:")''' R2_OLD = '''_l60k_state = {"path": None, "offset": 0, "loss": [], "dd": [], "lr": [], "ts": [], "eval": [], "total": None}''' R2_NEW = '''_l60k_state = {"path": None, "offset": 0, "loss": [], "dd": [], "lr": [], "ts": [], "eval": [], "total": None, "interval": 10, "last_step": None}''' R3_OLD = ''' st.update({"path": path, "offset": 0, "loss": [], "dd": [], "lr": [], "ts": [], "eval": [], "total": None})''' R3_NEW = ''' st.update({"path": path, "offset": 0, "loss": [], "dd": [], "lr": [], "ts": [], "eval": [], "total": None, "interval": 10, "last_step": None})''' R4_OLD = '''def _l60k_parse_chunk(st, text): for m in _L60K_TQDM_RE.finditer(text): try: st["total"] = int(m.group(2)) except Exception: pass for line in text.splitlines(): me = _L60K_EVAL_RE.search(line) if me: try: st["eval"].append((int(me.group(1)), float(me.group(2)))) except Exception: pass continue ms = _L60K_STEP_RE.search(line) if not ms or "loss:" not in line: continue try: step = int(ms.group(1)) except Exception: continue loss = _last_float(r"\\bloss:([0-9.eE+-]+)", line) if loss is not None: st["loss"].append((step, loss)) dd = _last_float(r"jedi_dd_task:([0-9.]+)", line) if dd is not None: st["dd"].append((step, dd)) lr = _last_float(r"\\blr:([0-9.eE+-]+)", line) if lr is not None: st["lr"].append((step, lr)) mt = _L60K_TS_RE.search(line) if mt: try: ts = time.mktime(time.strptime(mt.group(1), "%Y-%m-%d %H:%M:%S")) st["ts"].append((step, ts)) except Exception: pass''' R4_NEW = '''def _l60k_parse_chunk(st, text): for m in _L60K_TQDM_RE.finditer(text): try: st["total"] = int(m.group(2)) except Exception: pass for line in text.splitlines(): me = _L60K_EVAL_RE.search(line) if me: try: st["eval"].append((int(me.group(1)), float(me.group(2)))) except Exception: pass continue mm = _L60K_METRIC_RE.search(line) if not mm or "loss:" not in line: continue # Step chinh xac nam o manh tqdm "1230/60000 [" di ngay truoc # dong INFO tren cung dong vat ly; truong step: bi rut gon # dang K (1K, 10K) tu step 1000 nen khong doc truc tiep duoc. # Du phong: step nguyen -> dung ngay; khong co ca hai -> suy # tu step truoc cong interval log da quan sat. step = None mtq = _L60K_TQDM_RE.search(line) if mtq: try: step = int(mtq.group(1)) st["total"] = int(mtq.group(2)) except Exception: step = None if step is None and mm.group(1).isdigit(): step = int(mm.group(1)) if step is not None: prev = st.get("last_step") if prev is not None and 0 < step - prev <= 1000: st["interval"] = step - prev st["last_step"] = step elif st.get("last_step") is not None: step = st["last_step"] + st.get("interval", 10) st["last_step"] = step else: continue loss = _last_float(r"\\bloss:([0-9.eE+-]+)", line) if loss is not None: st["loss"].append((step, loss)) dd = _last_float(r"jedi_dd_task:([0-9.]+)", line) if dd is not None: st["dd"].append((step, dd)) lr = _last_float(r"\\blr:([0-9.eE+-]+)", line) if lr is not None: st["lr"].append((step, lr)) mt = _L60K_TS_RE.search(line) if mt: try: ts = time.mktime(time.strptime(mt.group(1), "%Y-%m-%d %H:%M:%S")) st["ts"].append((step, ts)) except Exception: pass''' REPLACEMENTS = [(R1_OLD, R1_NEW), (R2_OLD, R2_NEW), (R3_OLD, R3_NEW), (R4_OLD, R4_NEW)] for i, (old, new) in enumerate(REPLACEMENTS): n = src.count(old) assert n == 1, f"replacement {i}: anchor found {n} times (expected 1)" src = src.replace(old, new) assert src != orig shutil.copy(P, P.with_name("server.py.bak-trainpagefix-20261120")) P.write_text(src) py_compile.compile(str(P), doraise=True) print(f"dashboard server.py train-page parser fixed OK ({len(REPLACEMENTS)} replacements), backup at server.py.bak-trainpagefix-20261120")