Pass the checkout root to project, read_premises and checkouts
This commit is contained in:
+24
-23
@@ -94,11 +94,11 @@ def sections(text):
|
||||
return {name: ["\n".join(body) for body in bodies] for name, bodies in found.items()}
|
||||
|
||||
|
||||
def read_premises():
|
||||
def read_premises(root):
|
||||
try:
|
||||
found = sections(open("README.md", encoding="utf-8").read())
|
||||
found = sections(open(os.path.join(root, "README.md"), encoding="utf-8").read())
|
||||
except OSError as failure:
|
||||
sys.exit(f"README.md in {os.getcwd()}: {failure.strerror} - no premises.")
|
||||
sys.exit(f"README.md in {root}: {failure.strerror} - no premises.")
|
||||
for name in ("Goals", "Audience"):
|
||||
bodies = found.get(name, [])
|
||||
if not bodies:
|
||||
@@ -117,12 +117,12 @@ def checkout_root():
|
||||
return found.stdout.strip()
|
||||
|
||||
|
||||
def project():
|
||||
return os.getcwd().removeprefix(os.path.expanduser("~/code/")).lstrip("/")
|
||||
def project(root):
|
||||
return root.removeprefix(os.path.expanduser("~/code/")).lstrip("/")
|
||||
|
||||
|
||||
def session_name():
|
||||
return f"A {project()} loop-claude"
|
||||
def session_name(root):
|
||||
return f"A {project(root)} loop-claude"
|
||||
|
||||
|
||||
def session_file(session_id):
|
||||
@@ -196,23 +196,23 @@ def make_private(folder):
|
||||
os.umask(mask)
|
||||
|
||||
|
||||
def archive(path, note):
|
||||
folder = os.path.join(ARCHIVE, project())
|
||||
def archive(root, path, note):
|
||||
folder = os.path.join(ARCHIVE, project(root))
|
||||
try:
|
||||
stamp = time.strftime("%Y-%m-%dT%H-%M-%SZ", time.gmtime(os.path.getmtime(path)))
|
||||
make_private(folder)
|
||||
dest = os.path.join(folder, f"{stamp}.md")
|
||||
if not os.path.exists(dest):
|
||||
with open(os.open(dest, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w", encoding="utf-8") as copy:
|
||||
copy.write(f"# {stamp} {project()}\n\n{note}\n\n{render(entries(path))}\n")
|
||||
copy.write(f"# {stamp} {project(root)}\n\n{note}\n\n{render(entries(path))}\n")
|
||||
print(f"transcript: {dest}", file=sys.stderr)
|
||||
except OSError as failure:
|
||||
print(f"could not archive the transcript: {failure}", file=sys.stderr)
|
||||
|
||||
|
||||
def checkouts():
|
||||
found = [os.getcwd()]
|
||||
listing = subprocess.run(["git", "worktree", "list", "--porcelain"], capture_output=True, text=True)
|
||||
def checkouts(root):
|
||||
found = [root]
|
||||
listing = subprocess.run(["git", "worktree", "list", "--porcelain"], capture_output=True, cwd=root, text=True)
|
||||
if listing.returncode == 0:
|
||||
found += [line.split(" ", 1)[1] for line in listing.stdout.splitlines() if line.startswith("worktree ")]
|
||||
return sorted(set(found))
|
||||
@@ -323,17 +323,17 @@ def judge(task, premises, transcript):
|
||||
time.sleep(60)
|
||||
|
||||
|
||||
def interrupt(session_id, path, question):
|
||||
def interrupt(root, session_id, path, question):
|
||||
print(f"\n{question}", file=sys.stderr)
|
||||
if not (sys.stdin.isatty() and sys.stdout.isatty()):
|
||||
return "stdin or stdout is not a terminal"
|
||||
trust(checkouts())
|
||||
trust(checkouts(root))
|
||||
before = human_turns(entries(path))
|
||||
subprocess.run(["claude", "--permission-mode", "bypassPermissions", "--resume", str(session_id)])
|
||||
return "" if human_turns(entries(path)) > before else "the interrupt ended without the human typing anything"
|
||||
|
||||
|
||||
def run_cycle(task, premises, name, session_id):
|
||||
def run_cycle(root, task, premises, name, session_id):
|
||||
if failure := run_worker(worker_args(task, name, session_id, resuming=session_open(session_id))):
|
||||
return failure
|
||||
path = session_file(session_id)
|
||||
@@ -341,13 +341,13 @@ def run_cycle(task, premises, name, session_id):
|
||||
sys.exit(f"claude left no transcript for session {session_id}")
|
||||
verdict = judge(task, premises, render(entries(path)))
|
||||
while verdict["ask"]:
|
||||
archive(path, f"Task: {task}\n\nVerdict: needs input\n\n{verdict['question']}")
|
||||
if failure := interrupt(session_id, path, verdict["question"]):
|
||||
archive(root, path, f"Task: {task}\n\nVerdict: needs input\n\n{verdict['question']}")
|
||||
if failure := interrupt(root, session_id, path, verdict["question"]):
|
||||
print(failure, file=sys.stderr)
|
||||
sys.exit(2)
|
||||
verdict = judge(task, premises, render(entries(path)))
|
||||
if verdict["finished"]:
|
||||
archive(path, f"Task: {task}\n\nVerdict: finished")
|
||||
archive(root, path, f"Task: {task}\n\nVerdict: finished")
|
||||
end_session(path)
|
||||
return ""
|
||||
|
||||
@@ -376,14 +376,15 @@ def main():
|
||||
if task in ("", "-h", "--help"):
|
||||
print(__doc__.strip())
|
||||
sys.exit(0 if task else 1)
|
||||
os.chdir(checkout_root())
|
||||
root = checkout_root()
|
||||
os.chdir(root)
|
||||
while True:
|
||||
reload_if_edited(task, cycle_number, failures)
|
||||
cycle_number += 1
|
||||
premises = read_premises()
|
||||
name = session_name()
|
||||
premises = read_premises(root)
|
||||
name = session_name(root)
|
||||
print(f"\n=== cycle {cycle_number} ===", flush=True)
|
||||
failure = run_cycle(task, premises, name, uuid.uuid5(SESSION_NAMESPACE, name))
|
||||
failure = run_cycle(root, task, premises, name, uuid.uuid5(SESSION_NAMESPACE, name))
|
||||
if not failure:
|
||||
failures = 0
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user