fix(reliability): journal fail-safe serialization, per-line parse, review hardening

This commit is contained in:
2026-08-22 12:51:22 -06:00
parent 496f7c58e3
commit 1d17704f72
2 changed files with 31 additions and 10 deletions
@@ -1,5 +1,5 @@
"""Mark VIII reliability event journal. JSONL on /mmc, rotated."""
import json, os, threading
import json, os, threading, time
MK8_DIR = '/mmc/mk8'
EVENTS_PATH = os.path.join(MK8_DIR, 'events.log')
@@ -7,11 +7,10 @@ MAX_BYTES = 5 * 1024 * 1024
KEEP = 4
_LOCK = threading.Lock()
COUNTER_KINDS = ('boot', 'unexpected_boot', 'rollback', 'restart',
'guard_fix')
_COUNTER_KEYS = {'boot': 'boots', 'unexpected_boot': 'unexpected_boots',
'rollback': 'rollbacks', 'restart': 'restarts',
'guard_fix': 'guard_fixes'}
COUNTER_KINDS = tuple(_COUNTER_KEYS)
def _ensure_dir():
@@ -22,11 +21,23 @@ def _ensure_dir():
def log_event(kind, sev='info', msg='', meta=None):
entry = {'ts': int(__import__('time').time()), 'kind': str(kind),
'sev': sev, 'msg': msg[:500]}
if meta:
"""Append one journal entry. Never raises: a reliability journal that
can crash its caller would defeat its purpose. Single-writer per
process is assumed; there is no inter-process lock."""
try:
entry = {'ts': int(time.time()), 'kind': str(kind), 'sev': sev,
'msg': str(msg)[:500]}
if meta is not None:
json.dumps(meta)
entry['meta'] = meta
line = json.dumps(entry) + '\n'
except Exception:
try:
line = json.dumps({'ts': int(time.time()), 'kind': str(kind),
'sev': sev, 'msg': str(msg)[:500],
'meta_repr': repr(meta)[:500]}) + '\n'
except Exception:
return
with _LOCK:
_ensure_dir()
try:
@@ -52,8 +63,16 @@ def read_events(limit=100):
for path in paths:
try:
with open(path) as f:
out.extend(json.loads(l) for l in f if l.strip())
except (OSError, ValueError):
for l in f:
if not l.strip():
continue
try:
row = json.loads(l)
except ValueError:
continue
if isinstance(row, dict):
out.append(row)
except OSError:
continue
out.sort(key=lambda r: r.get('ts', 0))
return out[-limit:][::-1]
+2
View File
@@ -11,6 +11,8 @@ class EventsTest(unittest.TestCase):
def tearDown(self):
mk8_events.MK8_DIR = self.old
mk8_events.EVENTS_PATH = os.path.join(self.old, 'events.log')
mk8_events.MAX_BYTES = 5 * 1024 * 1024
def test_log_and_read_newest_first(self):
mk8_events.log_event('boot', msg='first')