31 lines
897 B
Python
31 lines
897 B
Python
"""Append-only experiment log utilities."""
|
|
import json
|
|
import os
|
|
from datetime import datetime, timezone
|
|
|
|
LOG_PATH = os.path.expanduser('~/.claude/autoresearch/log.jsonl')
|
|
|
|
|
|
def append_entry(entry: dict, path: str = LOG_PATH) -> None:
|
|
entry = dict(entry) # don't mutate caller's dict
|
|
entry.setdefault('ts', datetime.now(timezone.utc).isoformat())
|
|
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
|
|
with open(path, 'a') as f:
|
|
f.write(json.dumps(entry) + '\n')
|
|
|
|
|
|
def read_entries(path: str = LOG_PATH) -> list:
|
|
if not os.path.exists(path):
|
|
return []
|
|
entries = []
|
|
with open(path) as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if not line:
|
|
continue
|
|
try:
|
|
entries.append(json.loads(line))
|
|
except json.JSONDecodeError:
|
|
pass
|
|
return entries
|