Files
claude-config/bin/do_epic/dry_run.py
T

69 lines
3.0 KiB
Python

"""What `do-epic <EPIC-KEY> --dry-run` runs planning and the scheduler on, in place of Jira and tmux: an in-memory copy of the epic that records each write, and a tmux that starts no window. Reads of the epic and of tmux's windows are real, so the run sees the epic and the chunks as they are now."""
import dataclasses
import subprocess
from agent_cycle.tmux import Tmux
from do_epic.backend.fake import MemoryEpic
from do_epic.backend.protocol import EpicStore
from failure import Failure
from git_cli.git_repo import Git
# The tmux commands that only read. Each other command makes or changes a session or a window.
READS = ("list-windows", "show-options")
# The commands that start a window, which a later listing shows.
STARTS = ("new-session", "new-window")
@dataclasses.dataclass
class DryEpic(MemoryEpic):
"""The epic as `store` holds it. A sub-task it files gets a key no Jira issue has, such as ABC-new1, since Jira alone gives the next number."""
filed: int = 0
def next_key(self) -> str:
self.filed += 1
return f"{self.epic_key.rpartition('-')[0]}-new{self.filed}"
def seeded(store: EpicStore) -> DryEpic | Failure:
"""A DryEpic with the issues and the closed state that store holds now, or why they cannot be read."""
issues = store.read()
done = store.closed()
if isinstance(issues, Failure):
return issues
return done if isinstance(done, Failure) else DryEpic(store.epic_key, list(issues), done=done)
def recording(real: Tmux) -> Tmux:
"""real for each read; a command that starts a window is answered as if it had, and the window shows in every later listing, as a window that runs. Any other command, which would change a window, is answered as done."""
started: list[tuple[str, str, str]] = []
def run(args: list[str]) -> subprocess.CompletedProcess[str]:
if args[0] in STARTS:
named = "-s" if "-s" in args else "-t"
session = args[args.index(named) + 1].strip("=:")
started.append((session, args[args.index("-n") + 1], args[args.index("-c") + 1]))
return subprocess.CompletedProcess(args, 0, f"@dry{len(started)}\n", "")
if args[0] not in READS:
return subprocess.CompletedProcess(args, 0, "", "")
done = real(args)
if args[0] != "list-windows":
return done
rows = done.stdout if done.returncode == 0 else ""
if "-a" in args:
rows += "".join(f"{session}\t{name}\t0\t@dry{at}\t{cwd}\n" for at, (session, name, cwd) in enumerate(started, 1))
else:
rows += "".join(f"{name}\t0\t@dry{at}\n" for at, (session, name, _) in enumerate(started, 1) if f"={session}" == args[args.index("-t") + 1])
return subprocess.CompletedProcess(args, 0, rows, "")
return run
def without_clones(real: Git) -> Git:
"""real, except that a command with `clone` among its words fails, so that no repo is cloned."""
def run(*args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure:
if "clone" in args:
return Failure("a dry run clones nothing")
return real(*args, cwd=cwd, timeout=timeout, guarded=guarded, errors=errors)
return run