"""Print a table of the shell command patterns a release's chunks ran. usage: command-patterns [] Run it inside a project checkout or one of its worktrees. It reads loop-claude's archive of the project, as reading-cost does, and takes each chunk that finished in the release's window: after the tag before v and up to v's own tag, by their tag dates. Without , it takes the chunks finished since the highest v* tag, the release not yet cut. It reads the worker's transcript and each subagent's. Each Bash call counts once, under the pattern of its first simple command other than `cd`: the tool's name, then up to two words that read as subcommands, as in `tea pr create`. A subcommand starts with a lowercase letter and holds only lowercase letters, digits and dashes; any other word, such as an option, a path or a number, ends the pattern. The table has one row per pattern, most calls first. Its columns are the chunks that ran the pattern, its calls, and the tokens of its commands and of their results. Tokens are estimated at four characters each. The last column sums the seconds from each call to its result. Exit 1, saying why, on unknown arguments, a version no tag names, or when git cannot start or finds no checkout, or the archive cannot be read. """ import os import pathlib import sys from failure import Failure from transcript_archive.read import finished_chunks, read_threads from git_cli.git_repo import git from transcript_archive.location import project_archive from records.utc_stamp import seconds from command_patterns.tally import table, tally USAGE = "usage: command-patterns []" def main(argv: list[str]) -> int: if argv in (["-h"], ["--help"]): print(__doc__.strip()) return 0 if any(word.startswith("-") for word in argv): print(f"command-patterns: unknown option in {' '.join(argv)}; {USAGE}", file=sys.stderr) return 1 if len(argv) > 1: print(f"command-patterns: takes one version at most, not {' '.join(argv)}; {USAGE}", file=sys.stderr) return 1 printed = report(argv[0] if argv else None) if isinstance(printed, Failure): print(f"command-patterns: {printed.reason}", file=sys.stderr) return 1 print(printed) return 0 def report(version: str | None) -> str | Failure: common = git("rev-parse", "--path-format=absolute", "--git-common-dir") if isinstance(common, Failure): return common # The common dir is the main checkout's .git, whichever worktree runs this, and the main checkout's path keys the archive. main_root = str(pathlib.Path(common.strip()).parent) bounds = window(version, main_root) if isinstance(bounds, Failure): return bounds after, until = bounds chunks = finished_chunks(project_archive(main_root, pathlib.Path.home(), os.environ)) if isinstance(chunks, Failure): return chunks threads = [] for chunk in chunks: if (after is None or seconds(chunk.finished) > after) and (until is None or seconds(chunk.finished) <= until): read = read_threads(chunk) if isinstance(read, Failure): return read threads.append(read) named = f"v{version}" if version else "the release not yet cut" return f"Shell command patterns of {len(threads)} chunk{'' if len(threads) == 1 else 's'} in {named}:\n\n{table(tally(threads))}" def window(version: str | None, cwd: str) -> tuple[int | None, int | None] | Failure: """The release's window as tag dates in seconds since the epoch: after the tag before it, up to its own; None where it is open.""" listed = git("-c", "versionsort.suffix=-", "tag", "--list", "v*", "--sort=-v:refname", "--format=%(refname:short) %(creatordate:unix)", cwd=cwd) if isinstance(listed, Failure): return listed tags = [(name, int(stamp)) for name, stamp in (line.split() for line in listed.splitlines())] if version is None: return (tags[0][1] if tags else None, None) names = [name for name, _ in tags] if f"v{version}" not in names: return Failure(f"no tag v{version}; name a cut release, or give no version for the release not yet cut") at = names.index(f"v{version}") return (tags[at + 1][1] if at + 1 < len(tags) else None, tags[at][1])