Save a session record to disk, pruning oldest files if over MAX_SESSIONS.
(record: SessionRecord)
| 36 | |
| 37 | |
| 38 | def save_session(record: SessionRecord) -> Path: |
| 39 | """Save a session record to disk, pruning oldest files if over MAX_SESSIONS.""" |
| 40 | _ensure_dir() |
| 41 | safe_ts = record.timestamp.replace(":", "-") |
| 42 | path = HISTORY_DIR / f"{safe_ts}_session.json" |
| 43 | path.write_text(json.dumps(asdict(record), indent=2), encoding="utf-8") |
| 44 | |
| 45 | all_files = sorted(HISTORY_DIR.glob("*_session.json")) |
| 46 | while len(all_files) > MAX_SESSIONS: |
| 47 | try: |
| 48 | all_files[0].unlink() |
| 49 | except Exception: |
| 50 | logger.debug("Failed to prune old session file.", exc_info=True) |
| 51 | all_files = all_files[1:] |
| 52 | |
| 53 | return path |
| 54 | |
| 55 | |
| 56 | def load_sessions(limit: int | None = None) -> list[dict[str, Any]]: |