Files
claude-config/bin/touch_predictions/main.py
T

110 lines
5.0 KiB
Python

"""Record what a chunk's items were predicted to touch beside what its branch touched.
usage: touch-predictions record <ID>...
Run it in the chunk's worktree, on the chunk's branch, with every review fix committed and before
`todo-item finish` removes the items; <ID>... are the plan items the chunk takes. It reads each
item's prediction from its detail in the project's plan: the bullets under a `Touches:` line, one
per file, as in
Touches:
- `bin/a.py`: `main`, `Plan.branch` (small)
- `README.md`: `## Goals`
- `bin/run.sh`: lines 10-20
- `docs/new.md`
A path alone is the whole file, and (small) marks a part the item changes only a little. It names
what the branch touched the same way, from the diff between the branch's merge base with the
default branch and HEAD:
- in a .py file, by Python's `ast` module: the innermost function or class a changed line lies in,
named with those around it; a top-level assignment by the name it sets; `(module)` for any other
top-level line. A comment between two statements goes with the one below it;
- in a .md file, by the nearest heading above the line, outside fenced code, spelt as its line;
`(top)` before the first heading;
- in any other file, and in Python that doesn't parse, by the line ranges the diff changed, as they
stand at HEAD;
- a file the branch added or deleted, or changed without text hunks, as the path alone. A rename
reads as a file deleted and one added.
It leaves out the plan and the project's records, which every chunk writes. It appends
`## Chunk <IDs>, <UTC time>, at <commit>` with both lists to the record the project's config names
as `touch-predictions`, in place of an entry the record already holds for the same items. Commit
the record with the chunk. Where the config leaves the setting out, it says so and exits 0.
Exit 1, saying why, on any other arguments, when no config names the checkout, the plan or the
record cannot be read or written, the plan holds no item <ID>, a `Touches:` list cannot be read, or
git cannot name the merge base or the diff.
"""
import sys
import time
from git_cli.git_repo import default_ref, git
from failure import Failure
from plan_store.stores import store as plan_store
from touch_predictions.prediction import predicted, together, Touches
from project_config.main import project
from project_config.settings import ProjectConfig, TOUCH_PREDICTIONS
from touch_predictions import record
from record_store.stores import RECORDS, touch_predictions_store
from touch_predictions.diff_parts import touched
from text_files.utc_stamp import utc
def main(argv: list[str]) -> int:
if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
if argv[:1] != ["record"] or len(argv) < 2 or not all(id_.isascii() and id_.isdigit() for id_ in argv[1:]):
print("usage: touch-predictions record <ID>...", file=sys.stderr)
return 1
config = project()
done = config if isinstance(config, Failure) else recorded(config, [int(id_) for id_ in argv[1:]])
if isinstance(done, Failure):
print(f"touch-predictions: {done.reason}", file=sys.stderr)
return 1
print(done)
return 0
def recorded(config: ProjectConfig, ids: list[int]) -> str | Failure:
"""Appends the entry of the chunk that takes the items ids, and says so; or says that the project keeps no record."""
store = touch_predictions_store(config)
if store is None:
return f"The project's config sets no `{TOUCH_PREDICTIONS}`, so nothing is recorded."
prediction = predictions(config, ids)
if isinstance(prediction, Failure):
return prediction
cwd = str(config.top)
default = default_ref(cwd)
base = default if isinstance(default, Failure) else git("merge-base", default, "HEAD", cwd=cwd)
head = base if isinstance(base, Failure) else git("rev-parse", "--short", "HEAD", cwd=cwd)
if isinstance(base, Failure) or isinstance(head, Failure):
return head if isinstance(head, Failure) else base
# Every chunk writes the plan and the records, so they tell no two chunks apart.
skipped = {path for name in ("plan", *RECORDS, TOUCH_PREDICTIONS) if (path := config.tracked(name))}
touches = touched(base.strip(), "HEAD", cwd, skipped)
if isinstance(touches, Failure):
return touches
failed = record.append(store, ids, record.entry(ids, utc(time.time()), head.strip(), prediction, touches))
return failed or f"Recorded what chunk {', '.join(map(str, ids))} was predicted to touch, and what it touched, in {store.label}."
def predictions(config: ProjectConfig, ids: list[int]) -> Touches | None | Failure:
"""What the items ids predict between them; None where none of them predicts anything."""
plan = plan_store(config).read()
if isinstance(plan, Failure):
return plan
details = {item.id: item.detail for item in plan.items}
found = []
for id_ in ids:
if id_ not in details:
return Failure(f"the plan holds no item {id_}")
prediction = predicted(details[id_])
if isinstance(prediction, Failure):
return Failure(f"item {id_}: {prediction.reason}")
if prediction is not None:
found.append(prediction)
return together(found) if found else None