"""Append-only trial records; legacy CSVs are read without discarding wide rows.""" import csv import json from datetime import datetime, timezone from pathlib import Path import fcntl def append_trials(path, rows, protocol): path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) created = datetime.now(timezone.utc).isoformat() with path.open('a') as fh: fcntl.flock(fh, fcntl.LOCK_EX) for row in rows: record = {'schema': 1, 'created_at': created, 'protocol': protocol, 'result': row} fh.write(json.dumps(record, allow_nan=False, default=float) + '\n') def count_trials(directory, market): """Observed trials are a lower bound; unrecorded searches remain unknown.""" total = 0 paths = [Path(directory) / f'trials_{market}.csv', Path(directory) / f'trials_{market}.jsonl'] if market == 'us': paths += [Path(directory) / 'trials_us_sweep.csv', Path(directory) / 'trials_us_sweep.jsonl'] for path in paths: if not path.exists(): continue with path.open() as fh: if path.suffix == '.jsonl': for line in fh: if line.strip(): record = json.loads(line) if record.get('schema') != 1: raise ValueError(f'Unknown trial schema: {path}') total += 1 else: rows = csv.reader(fh) next(rows, None) for row in rows: if row: if len(row) < 2: raise ValueError(f'Incomplete trial row: {path}') total += 1 return total