HAKO upload: hako/telemetry.py
Browse files- hako/telemetry.py +53 -0
hako/telemetry.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""JSONL telemetry for HAKO's five mandated streams.
|
| 2 |
+
|
| 3 |
+
Streams (one append-only JSONL file each):
|
| 4 |
+
learning -- losses, effective learning rate, Lyapunov value
|
| 5 |
+
routing -- router action probs/entropy, MoE gates, load balance
|
| 6 |
+
orchestration -- cyclic-thinking potential P, cycles, GHSOM growth, N
|
| 7 |
+
diffusion -- game level, difficulty, reward, denoiser loss
|
| 8 |
+
thinking -- request decomposition/feedback/completeness trace
|
| 9 |
+
|
| 10 |
+
Proposition P-TEL (lossless auditability). Each record is a self-contained
|
| 11 |
+
JSON object with monotone field 't' inside its stream; because the writer is
|
| 12 |
+
append-only and atomic per line, any prefix of the file is a valid partial
|
| 13 |
+
trace of the run (crash-consistent audit).
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import json
|
| 18 |
+
import os
|
| 19 |
+
import time
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
from typing import Any, Dict
|
| 22 |
+
|
| 23 |
+
_STREAMS = ("learning", "routing", "orchestration", "diffusion", "thinking")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class Telemetry:
|
| 27 |
+
def __init__(self, cfg) -> None:
|
| 28 |
+
self.dir = Path(cfg.telemetry_dir)
|
| 29 |
+
self.dir.mkdir(parents=True, exist_ok=True)
|
| 30 |
+
self.paths = {s: self.dir / f"{s}.jsonl" for s in _STREAMS}
|
| 31 |
+
self.counters = {s: 0 for s in _STREAMS}
|
| 32 |
+
self.enabled = True
|
| 33 |
+
|
| 34 |
+
def log(self, stream: str, record: Dict[str, Any]) -> None:
|
| 35 |
+
if not self.enabled or stream not in self.paths:
|
| 36 |
+
return
|
| 37 |
+
rec = {"t": self.counters[stream], "ts": time.time(), **record}
|
| 38 |
+
self.counters[stream] += 1
|
| 39 |
+
line = json.dumps(rec, ensure_ascii=True, default=float)
|
| 40 |
+
with open(self.paths[stream], "a", encoding="utf-8") as fh:
|
| 41 |
+
fh.write(line + "\n")
|
| 42 |
+
fh.flush()
|
| 43 |
+
os.fsync(fh.fileno())
|
| 44 |
+
|
| 45 |
+
# convenience wrappers -------------------------------------------------
|
| 46 |
+
def learning(self, **kw): self.log("learning", kw)
|
| 47 |
+
def routing(self, **kw): self.log("routing", kw)
|
| 48 |
+
def orchestration(self, **kw): self.log("orchestration", kw)
|
| 49 |
+
def diffusion(self, **kw): self.log("diffusion", kw)
|
| 50 |
+
def thinking(self, **kw): self.log("thinking", kw)
|
| 51 |
+
|
| 52 |
+
def summary(self) -> Dict[str, int]:
|
| 53 |
+
return {s: self.counters[s] for s in _STREAMS}
|