Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -5,10 +5,11 @@ import json
|
||||
import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from loop_effects.effects import Effects
|
||||
from failure import Failure
|
||||
|
||||
# The type of the first line of a run's file, which holds what the stream lacks: claude's arguments, working directory, prompt, exit code and stderr. No event claude writes has this type.
|
||||
HEADER = "replay_run"
|
||||
@@ -21,8 +22,8 @@ def replay_folder(root: pathlib.Path, tool: str, argv: list[str], now: float) ->
|
||||
return root / "replays" / re.sub(r"[^A-Za-z0-9._-]", "_", name)
|
||||
|
||||
|
||||
def archiving(effects: Effects, folder: pathlib.Path) -> Effects:
|
||||
"""effects whose claude also writes each run into folder: a header line, then the stream claude printed. A run is kept whether it failed or not, and a run that repeats another word for word is kept once. A folder that can't be written is reported on stderr, and the replay goes on."""
|
||||
def archiving(effects: Effects, folder: pathlib.Path, say: typing.Callable[[str], None]) -> Effects:
|
||||
"""effects whose claude also writes each run into folder: a header line, then the stream claude printed. A run is kept whether it failed or not, and a run that repeats another word for word is kept once. A folder that can't be written is reported to say, and the replay goes on."""
|
||||
claude = effects.claude
|
||||
|
||||
def run(args: list[str], cwd: str, prompt: str) -> subprocess.CompletedProcess[str]:
|
||||
@@ -32,17 +33,17 @@ def archiving(effects: Effects, folder: pathlib.Path) -> Effects:
|
||||
folder.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
(folder / f"{hashlib.sha256(content.encode()).hexdigest()}.jsonl").write_text(content, encoding="utf-8")
|
||||
except OSError as failure:
|
||||
print(f"could not keep the run in {folder}: {failure}", file=sys.stderr, flush=True)
|
||||
say(f"could not keep the run in {folder}: {failure}")
|
||||
return proc
|
||||
|
||||
return dataclasses.replace(effects, claude=run)
|
||||
|
||||
|
||||
def print_report(folder: pathlib.Path, report: str) -> None:
|
||||
"""Prints the report a replay ends with, and keeps it in folder, beside the runs."""
|
||||
print(report)
|
||||
def kept_report(folder: pathlib.Path, report: str) -> Failure | None:
|
||||
"""Keeps the report a replay ends with in folder, beside the runs; the Failure where it can't."""
|
||||
try:
|
||||
folder.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
(folder / RESULT).write_text(report + "\n", encoding="utf-8")
|
||||
except OSError as failure:
|
||||
print(f"could not keep the result in {folder}: {failure}", file=sys.stderr, flush=True)
|
||||
return Failure(f"could not keep the result in {folder}: {failure}")
|
||||
return None
|
||||
|
||||
@@ -4,6 +4,7 @@ import sys
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
from rung_replay.archive import kept_report
|
||||
from model_routing.ladder import Ladder, Rung
|
||||
from claude_code.reply import Limited
|
||||
from loop_state.paths import paths_from
|
||||
@@ -44,3 +45,15 @@ def limited_exit(tool: str, limited: Limited) -> int:
|
||||
def failed(tool: str, reason: str) -> int:
|
||||
print(f"{tool}: {reason}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
def say(text: str) -> None:
|
||||
"""Writes text to stderr, as each replay's progress and each round it leaves out are told."""
|
||||
print(text, file=sys.stderr, flush=True)
|
||||
|
||||
|
||||
def print_report(folder: pathlib.Path, report: str) -> None:
|
||||
"""Prints the report a replay ends with, and keeps it in folder, beside the runs."""
|
||||
print(report)
|
||||
if kept := kept_report(folder, report):
|
||||
say(kept.reason)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
"""Replay archived cases on a rung, as judge-replay and review-replay both do; and judge-replay's rung search, which holds each candidate rung to what a reference rung reproduced."""
|
||||
import concurrent.futures
|
||||
import dataclasses
|
||||
import sys
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
@@ -49,6 +48,8 @@ def replayed(
|
||||
replay_one: typing.Callable[[Case, Rung], Answer | Limited],
|
||||
reproduced: typing.Callable[[Case, Answer], list[Aim]],
|
||||
what: str,
|
||||
say: typing.Callable[[str], None],
|
||||
at_once: int,
|
||||
climbs: typing.Sequence[Rung] = (),
|
||||
) -> Replay[Case, Aim, Answer] | Limited:
|
||||
"""Replays the cases on the reference rung, then on each climb and each candidate, on the cases where the reference reproduced anything.
|
||||
@@ -57,7 +58,7 @@ def replayed(
|
||||
|
||||
The candidates are tried highest first, until one misses an item the reference reproduced. Going down, every candidate at or above the one named was tried and didn't miss anything, so the result needs no assumption that a higher rung does at least as well; and a miss high up spares the cheaper rungs their replays.
|
||||
"""
|
||||
on_reference = replayed_on(cases, reference, replay_one, what)
|
||||
on_reference = replayed_on(cases, reference, replay_one, what, say, at_once)
|
||||
if isinstance(on_reference, Limited):
|
||||
return on_reference
|
||||
held = [(case, aim) for case, reply in zip(cases, on_reference) if (aim := reproduced(case, reply))]
|
||||
@@ -66,14 +67,14 @@ def replayed(
|
||||
return Replay(on_reference, [], None)
|
||||
climbed: list[Tried[Case, Aim, Answer]] = []
|
||||
for rung in climbs:
|
||||
climb = tried_on(held, rung, replay_one, reproduced, what)
|
||||
climb = tried_on(held, rung, replay_one, reproduced, what, say, at_once)
|
||||
if isinstance(climb, Limited):
|
||||
return climb
|
||||
climbed.append(climb)
|
||||
tried: list[Tried[Case, Aim, Answer]] = []
|
||||
lowest = None
|
||||
for rung in candidates:
|
||||
candidate = tried_on(held, rung, replay_one, reproduced, what)
|
||||
candidate = tried_on(held, rung, replay_one, reproduced, what, say, at_once)
|
||||
if isinstance(candidate, Limited):
|
||||
return candidate
|
||||
tried.append(candidate)
|
||||
@@ -89,9 +90,11 @@ def tried_on(
|
||||
replay_one: typing.Callable[[Case, Rung], Answer | Limited],
|
||||
reproduced: typing.Callable[[Case, Answer], list[Aim]],
|
||||
what: str,
|
||||
say: typing.Callable[[str], None],
|
||||
at_once: int,
|
||||
) -> Tried[Case, Aim, Answer] | Limited:
|
||||
"""rung and each item, of those the reference reproduced in the held cases, that its replies missed."""
|
||||
replies = replayed_on([case for case, _ in held], rung, replay_one, what)
|
||||
replies = replayed_on([case for case, _ in held], rung, replay_one, what, say, at_once)
|
||||
if isinstance(replies, Limited):
|
||||
return replies
|
||||
misses: list[Miss[Case, Aim, Answer]] = []
|
||||
@@ -101,10 +104,10 @@ def tried_on(
|
||||
return Tried(rung, misses)
|
||||
|
||||
|
||||
def replayed_on(cases: list[Case], rung: Rung, replay_one: typing.Callable[[Case, Rung], Answer | Limited], what: str) -> list[Answer] | Limited:
|
||||
"""Each case's reply on rung, AT_ONCE at a time; a usage limit cancels the rest."""
|
||||
print(f"replaying {len(cases)} {what} on {rung}", file=sys.stderr, flush=True)
|
||||
with concurrent.futures.ThreadPoolExecutor(AT_ONCE) as pool:
|
||||
def replayed_on(cases: list[Case], rung: Rung, replay_one: typing.Callable[[Case, Rung], Answer | Limited], what: str, say: typing.Callable[[str], None], at_once: int) -> list[Answer] | Limited:
|
||||
"""Each case's reply on rung, at_once at a time, after telling say what it replays; a usage limit cancels the rest."""
|
||||
say(f"replaying {len(cases)} {what} on {rung}")
|
||||
with concurrent.futures.ThreadPoolExecutor(at_once) as pool:
|
||||
running = [pool.submit(replay_one, case, rung) for case in cases]
|
||||
replies: list[Answer] = []
|
||||
for future in running:
|
||||
|
||||
@@ -9,11 +9,11 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from rung_replay.archive import archiving, print_report, replay_folder
|
||||
from rung_replay.command import down_from
|
||||
from rung_replay.archive import archiving, replay_folder
|
||||
from rung_replay.search import AT_ONCE, first_reply, Miss, Replay, replayed, Tried
|
||||
from rung_replay.command import down_from, print_report
|
||||
from loop_effects.effects import Effects
|
||||
from failure import Failure
|
||||
from rung_replay.search import first_reply, Miss, Replay, replayed, Tried
|
||||
from claude_code.reply import Limited
|
||||
from model_routing.ladder_file import read_ladder, SHIPPED
|
||||
from model_routing.ladder import Rung
|
||||
@@ -34,8 +34,7 @@ def search(cases, candidates, replies, climbs=()):
|
||||
reply = replies[rung]
|
||||
return reply if isinstance(reply, Limited) else reply[case]
|
||||
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
return replayed(cases, REFERENCE, candidates, replay_one, lambda _, reply: sorted(reply), "cases", climbs), asked
|
||||
return replayed(cases, REFERENCE, candidates, replay_one, lambda _, reply: sorted(reply), "cases", lambda text: None, AT_ONCE, climbs), asked
|
||||
|
||||
|
||||
class Replayed(unittest.TestCase):
|
||||
@@ -96,8 +95,8 @@ class ArchivedRuns(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.folder = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "replays" / "tool-1"
|
||||
|
||||
def run_archived(self, replies):
|
||||
return archiving(effects(lambda args, cwd, prompt: replies.pop(0)), self.folder).claude
|
||||
def run_archived(self, replies, say=lambda text: None):
|
||||
return archiving(effects(lambda args, cwd, prompt: replies.pop(0)), self.folder, say).claude
|
||||
|
||||
def kept(self):
|
||||
return sorted(self.folder.glob("*.jsonl"))
|
||||
@@ -134,16 +133,15 @@ class ArchivedRuns(unittest.TestCase):
|
||||
raise FileNotFoundError(2, "No such file or directory")
|
||||
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
archiving(effects(missing), self.folder).claude([], "/r", "p")
|
||||
archiving(effects(missing), self.folder, self.fail).claude([], "/r", "p")
|
||||
self.assertFalse(self.folder.exists())
|
||||
# A file where the folder's parent should be.
|
||||
self.folder.parent.parent.mkdir(exist_ok=True)
|
||||
self.folder.parent.write_text("")
|
||||
proc = subprocess.CompletedProcess(["claude"], 0, "{}", "")
|
||||
err = io.StringIO()
|
||||
with contextlib.redirect_stderr(err):
|
||||
self.assertIs(self.run_archived([proc])([], "/r", "p"), proc)
|
||||
self.assertIn(f"could not keep the run in {self.folder}: ", err.getvalue())
|
||||
said = []
|
||||
self.assertIs(self.run_archived([proc], said.append)([], "/r", "p"), proc)
|
||||
self.assertEqual([text.startswith(f"could not keep the run in {self.folder}: ") for text in said], [True])
|
||||
|
||||
def test_prints_the_report_and_keeps_it_beside_the_runs(self):
|
||||
out = io.StringIO()
|
||||
|
||||
Reference in New Issue
Block a user