124 lines
5.6 KiB
Python
124 lines
5.6 KiB
Python
"""Print a table of the shell command patterns a release's chunks ran.
|
|
|
|
usage: command-patterns [<version>] [--calls <file>]
|
|
|
|
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<version> and up to v<version>'s own tag, by their tag dates. Without <version>, 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.
|
|
|
|
With --calls <file>, it first writes the file, replacing any it holds, with one line per Bash call:
|
|
a JSON object with `chunk` (the chunk's number in the window, from 1, in the order the archive lists
|
|
the chunks), `pattern` (as above), `seconds` (as in the table), `command_characters`,
|
|
`result_characters` and `command`, the command's text. Lines follow the chunks in that order, and each
|
|
chunk's threads. The file is readable by its owner alone. Commands may carry a secret, so write it
|
|
outside any folder git tracks. Where it cannot be written, no table is printed.
|
|
|
|
Exit 1, saying why, on unknown arguments or --calls without a file; on a version no tag names; when git
|
|
cannot start or finds no checkout; when the archive cannot be read; and when the --calls file cannot
|
|
be written.
|
|
"""
|
|
import json
|
|
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 command_patterns.tally import records, table, tally
|
|
from text_files.utc_stamp import seconds
|
|
|
|
USAGE = "usage: command-patterns [<version>] [--calls <file>]"
|
|
|
|
|
|
def main(argv: list[str]) -> int:
|
|
if argv in (["-h"], ["--help"]):
|
|
print(__doc__.strip())
|
|
return 0
|
|
calls_file = None
|
|
if "--calls" in argv:
|
|
at = argv.index("--calls")
|
|
if at + 1 == len(argv) or argv[at + 1].startswith("-"):
|
|
print(f"command-patterns: --calls needs a file; {USAGE}", file=sys.stderr)
|
|
return 1
|
|
calls_file = pathlib.Path(argv[at + 1])
|
|
argv = argv[:at] + argv[at + 2:]
|
|
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, calls_file)
|
|
if isinstance(printed, Failure):
|
|
print(f"command-patterns: {printed.reason}", file=sys.stderr)
|
|
return 1
|
|
print(printed)
|
|
return 0
|
|
|
|
|
|
def report(version: str | None, calls_file: pathlib.Path | 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)
|
|
if calls_file is not None:
|
|
saved = save_calls(calls_file, "".join(json.dumps(each) + "\n" for each in records(threads)))
|
|
if saved:
|
|
return saved
|
|
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 save_calls(path: pathlib.Path, text: str) -> Failure | None:
|
|
"""Write text to path for its owner alone, since a command may carry a secret; path keeps no wider mode it already had."""
|
|
try:
|
|
with open(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w", encoding="utf-8") as file:
|
|
os.fchmod(file.fileno(), 0o600)
|
|
file.write(text)
|
|
except OSError as failure:
|
|
return Failure(f"{path}: {failure.strerror}")
|
|
return None
|
|
|
|
|
|
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])
|