Close stdin on the worker, pipe the judge its prompt, ground empty evidence, and skip meta turns in the handoff count
This commit is contained in:
+30
-23
@@ -113,17 +113,24 @@ def session_file(session_id):
|
||||
|
||||
|
||||
def end_session(path):
|
||||
os.remove(path)
|
||||
shutil.rmtree(path.removesuffix(".jsonl"), ignore_errors=True)
|
||||
for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"):
|
||||
try:
|
||||
shutil.rmtree(sibling) if os.path.isdir(sibling) else os.remove(sibling)
|
||||
except OSError as failure:
|
||||
print(f"could not remove {sibling}: {failure}", file=sys.stderr)
|
||||
|
||||
|
||||
def parse(raw):
|
||||
try:
|
||||
entry = json.loads(raw)
|
||||
except ValueError:
|
||||
return None
|
||||
return entry if isinstance(entry, dict) else None
|
||||
|
||||
|
||||
def entries(path):
|
||||
for raw in open(path, encoding="utf-8"):
|
||||
try:
|
||||
entry = json.loads(raw)
|
||||
except ValueError:
|
||||
continue
|
||||
if isinstance(entry, dict):
|
||||
if entry := parse(raw):
|
||||
yield entry
|
||||
|
||||
|
||||
@@ -147,10 +154,10 @@ def render(entries):
|
||||
def human_turns(entries):
|
||||
spoken = 0
|
||||
for entry in entries:
|
||||
if entry.get("type") != "user":
|
||||
if entry.get("type") != "user" or entry.get("isMeta"):
|
||||
continue
|
||||
content = entry.get("message", {}).get("content")
|
||||
if isinstance(content, str):
|
||||
if isinstance(content, str) and not content.startswith("<"):
|
||||
spoken += 1
|
||||
elif isinstance(content, list) and not any(part.get("type") == "tool_result" for part in content if isinstance(part, dict)):
|
||||
spoken += 1
|
||||
@@ -219,17 +226,14 @@ def worker_args(task, name, session_id, resuming):
|
||||
|
||||
|
||||
def run_worker(args):
|
||||
proc = subprocess.Popen(["claude", *args], stdout=subprocess.PIPE, text=True)
|
||||
proc = subprocess.Popen(["claude", *args], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True)
|
||||
done = {}
|
||||
for raw in proc.stdout:
|
||||
try:
|
||||
event = json.loads(raw)
|
||||
except ValueError:
|
||||
continue
|
||||
event = parse(raw) or {}
|
||||
if event.get("type") == "result":
|
||||
done = event
|
||||
elif event.get("type") == "assistant":
|
||||
for part in event["message"]["content"]:
|
||||
for part in event.get("message", {}).get("content", []):
|
||||
if line := said(part, 160):
|
||||
print(line, flush=True)
|
||||
return done if proc.wait() == 0 else {}
|
||||
@@ -245,12 +249,12 @@ def judge_prompt(task, premises, transcript):
|
||||
|
||||
|
||||
def grounded(verdict, transcript):
|
||||
return not verdict["ask"] or verdict["evidence"] in transcript
|
||||
return not verdict["ask"] or bool(verdict["evidence"]) and verdict["evidence"] in transcript
|
||||
|
||||
|
||||
def judge(task, premises, transcript):
|
||||
args = [
|
||||
"-p", judge_prompt(task, premises, transcript),
|
||||
"-p",
|
||||
"--json-schema", json.dumps(JUDGE_SCHEMA),
|
||||
"--model", JUDGE_MODEL,
|
||||
"--no-session-persistence",
|
||||
@@ -259,11 +263,11 @@ def judge(task, premises, transcript):
|
||||
"--tools", "",
|
||||
]
|
||||
for attempt in range(2):
|
||||
proc = subprocess.run(["claude", *args], capture_output=True, text=True)
|
||||
if proc.returncode != 0:
|
||||
print(proc.stderr.strip() or "the judge failed", file=sys.stderr)
|
||||
proc = subprocess.run(["claude", *args], capture_output=True, input=judge_prompt(task, premises, transcript), text=True)
|
||||
verdict = (parse(proc.stdout) or {}).get("structured_output") if proc.returncode == 0 else None
|
||||
if not isinstance(verdict, dict) or "ask" not in verdict:
|
||||
print(proc.stderr.strip() or "the judge returned no verdict", file=sys.stderr)
|
||||
return {}
|
||||
verdict = json.loads(proc.stdout)["structured_output"]
|
||||
if grounded(verdict, transcript):
|
||||
return verdict
|
||||
print("the judge quoted nothing in the transcript twice - interrupting anyway", file=sys.stderr)
|
||||
@@ -272,7 +276,7 @@ def judge(task, premises, transcript):
|
||||
|
||||
def handoff(session_id, path, question):
|
||||
print(f"\n{question}", file=sys.stderr)
|
||||
if not sys.stdin.isatty():
|
||||
if not (sys.stdin.isatty() and sys.stdout.isatty()):
|
||||
return False
|
||||
trust(checkouts())
|
||||
before = human_turns(entries(path))
|
||||
@@ -338,4 +342,7 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(130)
|
||||
|
||||
Reference in New Issue
Block a user