Close every file loop-claude and its tests open

This commit is contained in:
2026-09-28 00:53:28 +02:00
parent d36953dbd8
commit 2cab114cc3
3 changed files with 51 additions and 44 deletions
+19 -17
View File
@@ -48,6 +48,7 @@ import glob
import itertools
import json
import os
import pathlib
import shutil
import subprocess
import sys
@@ -55,7 +56,7 @@ import time
import uuid
ARCHIVE = os.path.expanduser(os.environ.get("LOOP_CLAUDE_TRANSCRIPTS", "~/.claude/loop-claude-transcripts"))
RELOAD_BASELINE = open(__file__, encoding="utf-8").read()
RELOAD_BASELINE = pathlib.Path(__file__).read_text(encoding="utf-8")
RELOAD_FLAG = "--reloaded"
SESSION_NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec")
@@ -168,7 +169,7 @@ def checkout_root():
def read_premises(root):
try:
found = sections(open(os.path.join(root, "README.md"), encoding="utf-8").read())
found = sections(pathlib.Path(root, "README.md").read_text(encoding="utf-8"))
except OSError as failure:
return Stop(1, f"README.md in {root}: {failure.strerror} - no premises.")
except UnicodeDecodeError as failure:
@@ -277,7 +278,7 @@ def loop_state(argv):
def reload_if_edited(state):
try:
source = open(__file__, encoding="utf-8").read()
source = pathlib.Path(__file__).read_text(encoding="utf-8")
except OSError as failure:
print(f"keeping the running script: {failure}", file=sys.stderr)
return
@@ -390,9 +391,10 @@ def delete_transcript_files(path):
def entries(path):
for raw in open(path, encoding="utf-8"):
if entry := parse(raw):
yield entry
with open(path, encoding="utf-8") as handle:
for raw in handle:
if entry := parse(raw):
yield entry
def parse(raw):
@@ -428,17 +430,17 @@ def typed(entry):
def run_worker(root, args):
proc = subprocess.Popen(["claude", *args], cwd=root, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True)
done = False
for raw in proc.stdout:
event = parse(raw) or {}
if event.get("type") == "result":
done = True
elif event.get("type") == "assistant":
for part in event.get("message", {}).get("content", []):
if line := said(part, 160):
print(line, flush=True)
code = proc.wait()
with subprocess.Popen(["claude", *args], cwd=root, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True) as proc:
done = False
for raw in proc.stdout:
event = parse(raw) or {}
if event.get("type") == "result":
done = True
elif event.get("type") == "assistant":
for part in event.get("message", {}).get("content", []):
if line := said(part, 160):
print(line, flush=True)
code = proc.wait()
if code < 0:
return f"claude killed by signal {-code}"
if code: