#!/usr/bin/env python3 """Enumerate Claude Code sessions in a window and dump their human turns. Usage: scan-sessions.py [--days N] [--since YYYY-MM-DD] [--out DIR] Writes two files to --out (default: cwd): sessions.json one record per top-level session, oldest first userturns.txt every human turn, grouped by session, for reading A "top-level" session is one with no isSidechain marker and at least one real human turn, which drops subagent transcripts. Swarm runs still show up as many sessions sharing one cwd and timestamp — the report should collapse those. """ import argparse import datetime as dt import glob import json import os import sys SKIP_PREFIXES = ( " 20} print(f"{len(sessions)} top-level sessions, " f"{sum(len(s['turns']) for s in sessions)} human turns, " f"{sum(s['mb'] for s in sessions):.0f}MB") if swarms: print("likely swarm runs (collapse these to one line in the report):") for k, v in sorted(swarms.items(), key=lambda x: -x[1]): print(f" {v:4} sessions {k}") print(f"wrote {a.out}/sessions.json and {a.out}/userturns.txt") if __name__ == "__main__": sys.exit(main())