Two blank lines throughout, callers above callees in each group, model with the loop's own
This commit is contained in:
+25
-51
@@ -111,7 +111,6 @@ def main():
|
|||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def run_cycle(root, task, premises, name, session_id):
|
def run_cycle(root, task, premises, name, session_id):
|
||||||
if failure := run_worker(root, worker_args(task, name, session_id, resuming=session_open(session_id))):
|
if failure := run_worker(root, worker_args(task, name, session_id, resuming=session_open(session_id))):
|
||||||
return failure
|
return failure
|
||||||
@@ -131,7 +130,6 @@ def run_cycle(root, task, premises, name, session_id):
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def reload_if_edited(root, task, cycle_number, failures):
|
def reload_if_edited(root, task, cycle_number, failures):
|
||||||
try:
|
try:
|
||||||
source = open(__file__, encoding="utf-8").read()
|
source = open(__file__, encoding="utf-8").read()
|
||||||
@@ -148,7 +146,6 @@ def reload_if_edited(root, task, cycle_number, failures):
|
|||||||
os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, json.dumps(state)])
|
os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, json.dumps(state)])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def reloaded_state(handed):
|
def reloaded_state(handed):
|
||||||
state = parse(handed[0]) if len(handed) == 1 else None
|
state = parse(handed[0]) if len(handed) == 1 else None
|
||||||
if not state or {"cycle_number", "failures", "root"} - state.keys() or not isinstance(state.get("task"), str) or not state["task"]:
|
if not state or {"cycle_number", "failures", "root"} - state.keys() or not isinstance(state.get("task"), str) or not state["task"]:
|
||||||
@@ -156,7 +153,6 @@ def reloaded_state(handed):
|
|||||||
return state
|
return state
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def checkout_root():
|
def checkout_root():
|
||||||
found = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True)
|
found = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True)
|
||||||
if found.returncode != 0:
|
if found.returncode != 0:
|
||||||
@@ -164,7 +160,6 @@ def checkout_root():
|
|||||||
return found.stdout.strip()
|
return found.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def read_premises(root):
|
def read_premises(root):
|
||||||
try:
|
try:
|
||||||
found = sections(open(os.path.join(root, "README.md"), encoding="utf-8").read())
|
found = sections(open(os.path.join(root, "README.md"), encoding="utf-8").read())
|
||||||
@@ -181,7 +176,6 @@ def read_premises(root):
|
|||||||
return f"## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}"
|
return f"## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def sections(text):
|
def sections(text):
|
||||||
found, current, fenced = {}, None, False
|
found, current, fenced = {}, None, False
|
||||||
for line in text.splitlines():
|
for line in text.splitlines():
|
||||||
@@ -196,18 +190,11 @@ def sections(text):
|
|||||||
return {name: ["\n".join(body) for body in bodies] for name, bodies in found.items()}
|
return {name: ["\n".join(body) for body in bodies] for name, bodies in found.items()}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def session_name(root):
|
def session_name(root):
|
||||||
# "A " is the human's cue in the session picker that the session is the loop's.
|
# "A " is the human's cue in the session picker that the session is the loop's.
|
||||||
return f"A {project(root)} loop-claude"
|
return f"A {project(root)} loop-claude"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def project(root):
|
|
||||||
return root.removeprefix(os.path.expanduser("~/code/")).lstrip("/")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def archive(root, path, note):
|
def archive(root, path, note):
|
||||||
folder = os.path.join(ARCHIVE, project(root))
|
folder = os.path.join(ARCHIVE, project(root))
|
||||||
try:
|
try:
|
||||||
@@ -222,7 +209,6 @@ def archive(root, path, note):
|
|||||||
print(f"could not archive the transcript: {failure}", file=sys.stderr)
|
print(f"could not archive the transcript: {failure}", file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def make_private(folder):
|
def make_private(folder):
|
||||||
mask = os.umask(0o077)
|
mask = os.umask(0o077)
|
||||||
try:
|
try:
|
||||||
@@ -231,19 +217,25 @@ def make_private(folder):
|
|||||||
os.umask(mask)
|
os.umask(mask)
|
||||||
|
|
||||||
|
|
||||||
|
def model(variable):
|
||||||
|
return ["--model", os.environ[variable]] if os.environ.get(variable) else []
|
||||||
|
|
||||||
|
|
||||||
|
def project(root):
|
||||||
|
return root.removeprefix(os.path.expanduser("~/code/")).lstrip("/")
|
||||||
|
|
||||||
|
|
||||||
# The transcript file
|
# The transcript file
|
||||||
|
|
||||||
|
|
||||||
def session_file(session_id):
|
|
||||||
hit = glob.glob(os.path.expanduser(f"~/.claude/projects/*/{session_id}.jsonl"))
|
|
||||||
return hit[0] if hit else ""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def session_open(session_id):
|
def session_open(session_id):
|
||||||
return bool(session_file(session_id))
|
return bool(session_file(session_id))
|
||||||
|
|
||||||
|
|
||||||
|
def session_file(session_id):
|
||||||
|
hit = glob.glob(os.path.expanduser(f"~/.claude/projects/*/{session_id}.jsonl"))
|
||||||
|
return hit[0] if hit else ""
|
||||||
|
|
||||||
|
|
||||||
def delete_transcript_files(path):
|
def delete_transcript_files(path):
|
||||||
for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"):
|
for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"):
|
||||||
@@ -253,14 +245,12 @@ def delete_transcript_files(path):
|
|||||||
print(f"could not remove {sibling}: {failure}", file=sys.stderr)
|
print(f"could not remove {sibling}: {failure}", file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def entries(path):
|
def entries(path):
|
||||||
for raw in open(path, encoding="utf-8"):
|
for raw in open(path, encoding="utf-8"):
|
||||||
if entry := parse(raw):
|
if entry := parse(raw):
|
||||||
yield entry
|
yield entry
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse(raw):
|
def parse(raw):
|
||||||
try:
|
try:
|
||||||
entry = json.loads(raw)
|
entry = json.loads(raw)
|
||||||
@@ -272,6 +262,10 @@ def parse(raw):
|
|||||||
# "user" entries
|
# "user" entries
|
||||||
|
|
||||||
|
|
||||||
|
def human_turns(entries):
|
||||||
|
return sum(1 for entry in entries if typed(entry))
|
||||||
|
|
||||||
|
|
||||||
def typed(entry):
|
def typed(entry):
|
||||||
"""A human turn: a "user" entry, not isMeta, not promptSource "sdk" (the loop's own prompts), whose message.content is a string not starting with "<" (<command-name> and its kind) or one or more text parts, none starting with "[Request interrupted"."""
|
"""A human turn: a "user" entry, not isMeta, not promptSource "sdk" (the loop's own prompts), whose message.content is a string not starting with "<" (<command-name> and its kind) or one or more text parts, none starting with "[Request interrupted"."""
|
||||||
if entry.get("type") != "user" or entry.get("isMeta") or entry.get("promptSource") == "sdk":
|
if entry.get("type") != "user" or entry.get("isMeta") or entry.get("promptSource") == "sdk":
|
||||||
@@ -283,11 +277,6 @@ def typed(entry):
|
|||||||
return bool(parts) and all(isinstance(part, dict) and part.get("type") == "text" and not part.get("text", "").startswith("[Request interrupted") for part in parts)
|
return bool(parts) and all(isinstance(part, dict) and part.get("type") == "text" and not part.get("text", "").startswith("[Request interrupted") for part in parts)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def human_turns(entries):
|
|
||||||
return sum(1 for entry in entries if typed(entry))
|
|
||||||
|
|
||||||
|
|
||||||
# "assistant" entries and stream-json
|
# "assistant" entries and stream-json
|
||||||
|
|
||||||
|
|
||||||
@@ -310,7 +299,6 @@ def run_worker(root, args):
|
|||||||
return "" if done else "claude exited 0 without a result event"
|
return "" if done else "claude exited 0 without a result event"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def worker_args(task, name, session_id, resuming):
|
def worker_args(task, name, session_id, resuming):
|
||||||
if resuming:
|
if resuming:
|
||||||
start = ["-p", "Continue the task; the previous run stopped before it was finished.", "--resume", str(session_id)]
|
start = ["-p", "Continue the task; the previous run stopped before it was finished.", "--resume", str(session_id)]
|
||||||
@@ -325,22 +313,6 @@ def worker_args(task, name, session_id, resuming):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def model(variable):
|
|
||||||
return ["--model", os.environ[variable]] if os.environ.get(variable) else []
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def said(part, limit=400):
|
|
||||||
kind = part.get("type") if isinstance(part, dict) else None
|
|
||||||
if kind == "text" and part.get("text", "").strip():
|
|
||||||
return part["text"]
|
|
||||||
if kind == "tool_use":
|
|
||||||
return f"[tool {part.get('name')}] {json.dumps(part.get('input'))[:limit]}"
|
|
||||||
return ""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def render(entries):
|
def render(entries):
|
||||||
lines = []
|
lines = []
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
@@ -352,6 +324,15 @@ def render(entries):
|
|||||||
return "\n\n".join(lines)
|
return "\n\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def said(part, limit=400):
|
||||||
|
kind = part.get("type") if isinstance(part, dict) else None
|
||||||
|
if kind == "text" and part.get("text", "").strip():
|
||||||
|
return part["text"]
|
||||||
|
if kind == "tool_use":
|
||||||
|
return f"[tool {part.get('name')}] {json.dumps(part.get('input'))[:limit]}"
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
# --json-schema verdicts
|
# --json-schema verdicts
|
||||||
|
|
||||||
|
|
||||||
@@ -381,7 +362,6 @@ def judge(root, task, premises, transcript):
|
|||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def judge_prompt(task, premises, transcript):
|
def judge_prompt(task, premises, transcript):
|
||||||
return (
|
return (
|
||||||
f"From README.md:\n{premises}\n\n"
|
f"From README.md:\n{premises}\n\n"
|
||||||
@@ -392,12 +372,10 @@ def judge_prompt(task, premises, transcript):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def complete(verdict):
|
def complete(verdict):
|
||||||
return isinstance(verdict, dict) and set(JUDGE_SCHEMA["required"]) <= verdict.keys()
|
return isinstance(verdict, dict) and set(JUDGE_SCHEMA["required"]) <= verdict.keys()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def grounded(verdict, transcript):
|
def grounded(verdict, transcript):
|
||||||
if not verdict["ask"]:
|
if not verdict["ask"]:
|
||||||
return True
|
return True
|
||||||
@@ -418,7 +396,6 @@ def interrupt(root, session_id, path, question):
|
|||||||
return "" if human_turns(entries(path)) > before else "the interrupt ended without the human typing anything"
|
return "" if human_turns(entries(path)) > before else "the interrupt ended without the human typing anything"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def checkouts(root):
|
def checkouts(root):
|
||||||
found = [root]
|
found = [root]
|
||||||
listing = subprocess.run(["git", "worktree", "list", "--porcelain"], capture_output=True, cwd=root, text=True)
|
listing = subprocess.run(["git", "worktree", "list", "--porcelain"], capture_output=True, cwd=root, text=True)
|
||||||
@@ -427,7 +404,6 @@ def checkouts(root):
|
|||||||
return sorted(set(found))
|
return sorted(set(found))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def trust(paths):
|
def trust(paths):
|
||||||
config = os.path.expanduser("~/.claude.json")
|
config = os.path.expanduser("~/.claude.json")
|
||||||
try:
|
try:
|
||||||
@@ -453,8 +429,6 @@ def trust(paths):
|
|||||||
print("trusted for the interrupt: " + ", ".join(granted), file=sys.stderr)
|
print("trusted for the interrupt: " + ", ".join(granted), file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user