Read a project's language, title templates and branch template from its config

This commit is contained in:
2026-11-14 01:08:48 +01:00
parent 499d1772be
commit 82f4bbd91b
22 changed files with 316 additions and 43 deletions
+2
View File
@@ -61,6 +61,8 @@ def main(argv: list[str]) -> int:
config = project()
if not given and isinstance(config, Failure):
return failed(config)
if not isinstance(config, Failure):
checkout = dataclasses.replace(checkout, branch=config.branch)
record: RecordStore = FileStore(pathlib.Path(given[0]), None) if given or isinstance(config, Failure) else store(config, "reading-cost")
updated = update(record, archive, checkout, str(home), utc(time.time()) if reset else None)
if isinstance(updated, Failure):
+9 -4
View File
@@ -6,6 +6,7 @@ from claude_code.entries import AssistantTurn, Entry, messages, parse_entry, too
from git_cli.git_command import commits
from reading_cost.shell_reads import Reads, shell_reads
from command_lines.shell_command import resolved, simple_commands
from project_config import templates
@dataclasses.dataclass(frozen=True)
@@ -21,6 +22,8 @@ class Checkout:
current_root: str
main_root: str
tracked: frozenset[str]
# The project's `branch` template, which names each item's worktree.
branch: str = templates.BRANCH
def measure(threads: list[bytes], checkout: Checkout, home: str) -> Reading | None:
@@ -31,7 +34,7 @@ def measure(threads: list[bytes], checkout: Checkout, home: str) -> Reading | No
before = [[entry for entry in thread if isinstance(entry, AssistantTurn) and entry.timestamp < commit] for thread in (worker, *subagents)]
reads = [reads_of(call, turn.cwd or "/", home) for thread in before for turn, call in tool_calls(thread)]
roots = [checkout.main_root, *(added for found in reads for added in found.added)]
files = collections.Counter(file for found in reads for path in found.read if (file := relative(in_main_checkout(path, checkout.main_root), roots)) in checkout.tracked)
files = collections.Counter(file for found in reads for path in found.read if (file := relative(in_main_checkout(path, checkout), roots)) in checkout.tracked)
return Reading(sum(map(tokens_in, before)), files)
@@ -54,9 +57,11 @@ def reads_of(call: ToolCall, cwd: str, home: str) -> Reads:
return shell_reads(str(call.input.get("command", "")), cwd, home) if call.name == "Bash" else Reads([], [])
# chunk-preflight adds each item's worktree beside the checkout as <checkout>-item-<ID>, which no transcript command names.
def in_main_checkout(path: str, main_root: str) -> str:
return re.sub("^" + re.escape(main_root.rstrip("/")) + r"-item-\d+(?=/)", main_root.rstrip("/"), path, count=1)
# chunk-preflight adds each item's worktree beside the checkout as <checkout>-<branch>, each / of the branch a dash, which no transcript command names.
def in_main_checkout(path: str, checkout: Checkout) -> str:
main_root = checkout.main_root.rstrip("/")
worktree = re.escape(f"{main_root}-{checkout.branch.replace('/', '-')}").replace(re.escape(templates.ID), r"\d+")
return re.sub(f"^{worktree}(?=/)", lambda _: main_root, path, count=1)
# Relative to the deepest root holding it, so the checkout and each worktree name a file alike.
+6
View File
@@ -81,6 +81,12 @@ class Measure(unittest.TestCase):
checkout = Checkout("/c/p-item-7", "/c/p", frozenset({"README.md"}))
self.assertEqual(measure.measure([jsonl(worker)], checkout, "/h").files, collections.Counter({"README.md": 1}))
def test_counts_a_file_read_in_a_worktree_named_by_the_configs_branch_template(self):
worker = [said("1", "m1", tokens=10, name="Read", input={"file_path": "/c/p-feature-PAY-7/README.md"}), said("2", "m2", command="git commit -m x")]
checkout = Checkout("/c/p-feature-PAY-7", "/c/p", frozenset({"README.md"}), "feature/PAY-{id}")
self.assertEqual(measure.measure([jsonl(worker)], checkout, "/h").files, collections.Counter({"README.md": 1}))
self.assertEqual(measure.measure([jsonl(worker)], Checkout("/c/p-feature-PAY-7", "/c/p", frozenset({"README.md"})), "/h").files, collections.Counter())
def test_measures_nothing_for_a_worker_that_never_committed(self):
self.assertIsNone(measure.measure([jsonl([said("1", "m1", command="git log --grep commit")])], Checkout("/c/p", "/c/p", frozenset()), "/h"))
self.assertIsNone(measure.measure([jsonl([said("1", "m1", command="echo git commit")])], Checkout("/c/p", "/c/p", frozenset()), "/h"))