Show an item with todo-item show, and save each call with command-patterns --calls

This commit is contained in:
2026-11-12 08:06:53 +01:00
parent 5ccb51f763
commit 34689413ac
9 changed files with 156 additions and 17 deletions
+25 -6
View File
@@ -1,6 +1,6 @@
"""Print a table of the shell command patterns a release's chunks ran.
usage: command-patterns [<version>]
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
@@ -13,13 +13,19 @@ tool's name, then up to two words that read as subcommands, as in `tea pr create
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.
With --calls <file>, it also writes the file, one JSON object per Bash call in the order the chunks and
threads give them: `chunk` (the chunk's number in the window, from 1), `pattern`, `seconds`,
`command_characters`, `result_characters` and `command`, the command's text. The file is
overwritten, and written before the table is printed.
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.
no checkout, or the archive cannot be read, or the --calls file cannot be written.
"""
import json
import os
import pathlib
import sys
@@ -28,23 +34,31 @@ 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 records.utc_stamp import seconds
from command_patterns.tally import table, tally
USAGE = "usage: command-patterns [<version>]"
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)
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
@@ -52,7 +66,7 @@ def main(argv: list[str]) -> int:
return 0
def report(version: str | None) -> str | Failure:
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
@@ -72,6 +86,11 @@ def report(version: str | None) -> str | Failure:
if isinstance(read, Failure):
return read
threads.append(read)
if calls_file is not None:
try:
calls_file.write_text("".join(json.dumps(each) + "\n" for each in records(threads)), encoding="utf-8")
except OSError as failure:
return Failure(f"{calls_file}: {failure.strerror}")
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))}"