Answer architecture round 2: 0 🔴, 3 🟡 (0 misread); the cycle's and the chunk run's suites sit beside their modules, and the claim rule and the model override have one home each
CI / tests (pull_request) Successful in 3m23s
CI / tests (pull_request) Successful in 3m23s
This commit is contained in:
@@ -2,20 +2,25 @@
|
||||
import json
|
||||
import pathlib
|
||||
import subprocess
|
||||
import typing
|
||||
import uuid
|
||||
|
||||
from loop_effects.effects import Effects
|
||||
from claude_code import entries
|
||||
from failure import Failure
|
||||
from machine_load import memory
|
||||
from loop_effects.child_shells import Shell
|
||||
|
||||
|
||||
CWD = pathlib.Path("/c")
|
||||
NOW = 1790000000
|
||||
def assistant(*parts):
|
||||
|
||||
|
||||
def assistant(*parts: typing.Any) -> dict[str, typing.Any]:
|
||||
return {"type": "assistant", "message": {"content": list(parts)}}
|
||||
|
||||
|
||||
def human(content):
|
||||
def human(content: typing.Any) -> dict[str, typing.Any]:
|
||||
return {"type": "user", "message": {"content": content}}
|
||||
|
||||
|
||||
@@ -23,31 +28,31 @@ def human(content):
|
||||
class FakeEffects:
|
||||
"""Effects that record each call and answer from a script, one answer per call."""
|
||||
|
||||
def __init__(self, claude=(), on_terminal_claude=(), git=(), inputs=(), python=(), streams=(), on_terminal=True, free_memory=(), dm_failures=(), tmux_version="tmux 3.2", windows=None, owner="/r"):
|
||||
self.calls = []
|
||||
def __init__(self, claude: typing.Iterable[typing.Any] = (), on_terminal_claude: typing.Iterable[typing.Any] = (), git: typing.Iterable[typing.Any] = (), inputs: typing.Iterable[typing.Any] = (), python: typing.Iterable[typing.Any] = (), streams: typing.Iterable[typing.Any] = (), on_terminal: bool = True, free_memory: typing.Iterable[memory.Memory | None] = (), dm_failures: typing.Iterable[Failure | None] = (), tmux_version: str = "tmux 3.2", windows: str | None = None, owner: str = "/r") -> None:
|
||||
self.calls: list[tuple[str, typing.Any]] = []
|
||||
# Each tmux command run. They stay out of self.calls, as the DMs do. tmux prints `windows` for list-windows, or without them finds no session.
|
||||
self.tmux_calls = []
|
||||
self.tmux_calls: list[list[str]] = []
|
||||
self.tmux_version = tmux_version
|
||||
self.windows = windows
|
||||
# The checkout the session's @loop-root option names.
|
||||
self.owner = owner
|
||||
# Each direct message sent. They stay out of self.calls, so a test that lists every call doesn't list them. Without a script, each is sent.
|
||||
self.dms = []
|
||||
self.dms: list[str] = []
|
||||
# The directory each DM named, in the order of self.dms; None where it named none.
|
||||
self.dm_directories = []
|
||||
self.dm_directories: list[str | None] = []
|
||||
self.dm_failures = list(dm_failures)
|
||||
self.scripts = {"claude": list(claude), "claude_on_terminal": list(on_terminal_claude), "git": list(git), "input": list(inputs), "python": list(python), "stream_claude": list(streams)}
|
||||
self.scripts: dict[str, list[typing.Any]] = {"claude": list(claude), "claude_on_terminal": list(on_terminal_claude), "git": list(git), "input": list(inputs), "python": list(python), "stream_claude": list(streams)}
|
||||
# Without a script, every read finds all memory available.
|
||||
self.free_memory = list(free_memory)
|
||||
self.terminal = on_terminal
|
||||
self.worker_environments = []
|
||||
self.worker_folders = []
|
||||
self.terminal_environments = []
|
||||
self.terminal_folders = []
|
||||
self.worker_environments: list[dict[str, str]] = []
|
||||
self.worker_folders: list[str] = []
|
||||
self.terminal_environments: list[dict[str, str]] = []
|
||||
self.terminal_folders: list[str] = []
|
||||
# Each timer started, as its delay and action, which a test runs where the timer fires.
|
||||
self.timers = []
|
||||
self.timers: list[tuple[float, typing.Callable[[], None]]] = []
|
||||
|
||||
def effects(self):
|
||||
def effects(self) -> Effects:
|
||||
return Effects(
|
||||
claude=lambda args, cwd, stdin: self.answer("claude", (args, stdin)),
|
||||
claude_on_terminal=self.on_terminal_claude,
|
||||
@@ -68,55 +73,55 @@ class FakeEffects:
|
||||
tmux_on_terminal=lambda args: self.tmux(args).returncode,
|
||||
)
|
||||
|
||||
def timer(self, delay, action):
|
||||
def timer(self, delay: float, action: typing.Callable[[], None]) -> typing.Callable[[], None]:
|
||||
self.timers.append((delay, action))
|
||||
return lambda: self.calls.append(("stop_timer", delay))
|
||||
|
||||
def tmux(self, args):
|
||||
def tmux(self, args: list[str]) -> subprocess.CompletedProcess[str]:
|
||||
self.tmux_calls.append(args)
|
||||
if args[0] == "list-windows":
|
||||
return subprocess.CompletedProcess(args, 1 if self.windows is None else 0, self.windows or "", "no server")
|
||||
return subprocess.CompletedProcess(args, 0, {"-V": self.tmux_version + "\n", "show-options": self.owner + "\n"}.get(args[0], "@9\n"), "")
|
||||
|
||||
def dm(self, text, directory):
|
||||
def dm(self, text: str, directory: str | None) -> Failure | None:
|
||||
self.dms.append(text)
|
||||
self.dm_directories.append(directory)
|
||||
return self.dm_failures.pop(0) if self.dm_failures else None
|
||||
|
||||
def memory(self):
|
||||
def memory(self) -> memory.Memory | None:
|
||||
self.calls.append(("memory", None))
|
||||
return self.free_memory.pop(0) if self.free_memory else memory.Memory(available=16, total=16)
|
||||
|
||||
def on_terminal_claude(self, args, cwd, env):
|
||||
def on_terminal_claude(self, args: list[str], cwd: str, env: dict[str, str]) -> typing.Any:
|
||||
self.terminal_environments.append(env)
|
||||
self.terminal_folders.append(cwd)
|
||||
return self.answer("claude_on_terminal", args)
|
||||
|
||||
def stream(self, args, cwd, env):
|
||||
def stream(self, args: list[str], cwd: str, env: dict[str, str]) -> typing.Any:
|
||||
self.worker_environments.append(env)
|
||||
self.worker_folders.append(cwd)
|
||||
return self.answer("stream_claude", args)
|
||||
|
||||
def answer(self, seam, what):
|
||||
def answer(self, seam: str, what: typing.Any) -> typing.Any:
|
||||
self.calls.append((seam, what))
|
||||
answer = self.scripts[seam].pop(0)
|
||||
if isinstance(answer, BaseException):
|
||||
raise answer
|
||||
return answer(what) if callable(answer) else answer
|
||||
|
||||
def execv(self, path, args):
|
||||
def execv(self, path: str, args: list[str]) -> None:
|
||||
self.calls.append(("execv", args))
|
||||
raise OSError(2, "No such file or directory")
|
||||
|
||||
def made(self, seam):
|
||||
def made(self, seam: str) -> list[typing.Any]:
|
||||
return [what for called, what in self.calls if called == seam]
|
||||
|
||||
|
||||
def ran(stdout="", code=0, stderr=""):
|
||||
def ran(stdout: str = "", code: int = 0, stderr: str = "") -> subprocess.CompletedProcess[str]:
|
||||
return subprocess.CompletedProcess([], code, stdout, stderr)
|
||||
|
||||
|
||||
def judged(verdict, evidence="working", answer="", escalation="", release_ready=False, cost=None, reversed=""):
|
||||
def judged(verdict: str, evidence: str = "working", answer: str = "", escalation: str = "", release_ready: bool = False, cost: float | None = None, reversed: str = "") -> subprocess.CompletedProcess[str]:
|
||||
reply = {"answer": answer, "escalation": escalation, "evidence": evidence, "finished": verdict == "finished", "interrupt": verdict == "interrupt", "question": "Which one?" if verdict in ("interrupt", "quoted") else "", "answered_before": "", "left_open": "", "release_ready": release_ready, "reversed": reversed}
|
||||
return ran(json.dumps({"structured_output": reply, **({} if cost is None else {"total_cost_usd": cost})}))
|
||||
|
||||
@@ -124,7 +129,7 @@ def judged(verdict, evidence="working", answer="", escalation="", release_ready=
|
||||
class Worker:
|
||||
"""A worker claude: writes a line to the session's transcript, then streams a result."""
|
||||
|
||||
def __init__(self, home, prompted=False, text="working", usage=None, sent=None):
|
||||
def __init__(self, home: pathlib.Path, prompted: bool = False, text: str = "working", usage: dict[str, typing.Any] | None = None, sent: str | None = None) -> None:
|
||||
self.home = home
|
||||
# Whether it records its -p prompt in the transcript first, as claude does.
|
||||
self.prompted = prompted
|
||||
@@ -134,7 +139,7 @@ class Worker:
|
||||
# The time its message records, as utc() writes it; none where None.
|
||||
self.sent = sent
|
||||
|
||||
def __call__(self, args):
|
||||
def __call__(self, args: list[str]) -> "Streamed":
|
||||
flag = "--session-id" if "--session-id" in args else "--resume"
|
||||
path = self.home / "projects" / "-fake" / f"{args[args.index(flag) + 1]}.jsonl"
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
@@ -155,26 +160,27 @@ class Worker:
|
||||
class Streamed:
|
||||
"""A started claude. An event of None is an idle stretch of CHECK_EVERY seconds, which advances `clock` by that; `held` are the shells it has, and `stopped` collects the ones stopped."""
|
||||
|
||||
def __init__(self, code, *events, clock=None, held=()):
|
||||
def __init__(self, code: int, *events: dict[str, typing.Any] | None, clock: list[float] | None = None, held: typing.Iterable[Shell] = ()) -> None:
|
||||
self.code = code
|
||||
self.events = events or ({"type": "result"},)
|
||||
self.clock = clock
|
||||
self.held = list(held)
|
||||
self.stopped = []
|
||||
self.stopped: list[Shell] = []
|
||||
|
||||
def lines(self, idle):
|
||||
def lines(self, idle: float) -> typing.Iterator[str | None]:
|
||||
for event in self.events:
|
||||
if event is None:
|
||||
assert self.clock is not None, "an idle stretch advances the clock the test passes"
|
||||
self.clock[0] += idle
|
||||
yield None if event is None else json.dumps(event) + "\n"
|
||||
|
||||
def shells(self):
|
||||
def shells(self) -> list[Shell]:
|
||||
return self.held
|
||||
|
||||
def stop(self, shell):
|
||||
def stop(self, shell: Shell) -> None:
|
||||
self.stopped.append(shell)
|
||||
|
||||
def wait(self):
|
||||
def wait(self) -> int:
|
||||
return self.code
|
||||
|
||||
|
||||
@@ -182,9 +188,9 @@ class Streamed:
|
||||
STAMP = f"<{NOW}>"
|
||||
|
||||
|
||||
def kinds(*found):
|
||||
def kinds(*found: dict[str, typing.Any]) -> list[typing.Any]:
|
||||
return [entries.entry_from(entry) for entry in found]
|
||||
|
||||
|
||||
def said(*texts):
|
||||
def said(*texts: str) -> entries.AssistantTurn:
|
||||
return entries.AssistantTurn(parts=tuple(entries.Text(text) for text in texts))
|
||||
Reference in New Issue
Block a user