Answer stability round 1: 1 🔴, 11 🟡 (0 misread); drop keys typed ahead on leaving an answer, wrap every line to the drawn width, list the routing calls, test the paths mutants survived

This commit is contained in:
2026-10-29 01:27:23 +01:00
parent 192b36fada
commit e3cd8ae0cf
15 changed files with 413 additions and 151 deletions
+57 -14
View File
@@ -6,6 +6,7 @@ import json
import os
import pathlib
import shutil
import signal
import stat
import subprocess
import sys
@@ -66,10 +67,8 @@ 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=(), gone=()):
def __init__(self, claude=(), on_terminal_claude=(), git=(), inputs=(), python=(), streams=(), on_terminal=True, free_memory=()):
self.calls = []
# The pids of processes that no longer run.
self.gone = set(gone)
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)}
# Without a script, every read finds all memory available.
self.free_memory = list(free_memory)
@@ -79,7 +78,7 @@ class FakeEffects:
def effects(self):
return Effects(
alive=lambda pid: pid not in self.gone,
alive=lambda pid: True,
claude=lambda args, cwd, stdin: self.answer("claude", (args, stdin)),
claude_on_terminal=self.on_terminal_claude,
discard_typed=lambda: self.calls.append(("discard_typed", None)),
@@ -103,7 +102,7 @@ class FakeEffects:
return self.free_memory.pop(0) if self.free_memory else memory.Memory(available=16, total=16)
def input_or(self, prompt, other):
"""Answers from the input script as input does, but hands a callable in it other, which loop-inbox's answer comes from."""
"""Answers from the input script as input does; a callable in the script gets other, the source of loop-inbox's answer."""
self.calls.append(("input", prompt))
answer = self.scripts["input"].pop(0)
if isinstance(answer, BaseException):
@@ -752,15 +751,26 @@ class Interrupts(unittest.TestCase):
self.assertEqual(self.talk(states.Talk(self.ask(), waiting.OpenInInbox(inbox)), effects), (None, f"{STAMP} claude is open for this question in loop-inbox; waiting for it to end\n"))
self.assertEqual((fake.made("claude_on_terminal"), list(self.paths.waiting.iterdir())), ([], []))
def test_stops_when_loop_inbox_ends_its_claude_or_is_gone_with_nothing_typed(self):
inbox = 4343
def ended(_):
def test_stops_when_loop_inbox_ends_its_claude_with_nothing_typed(self):
inbox, looked, slept = 4343, [], []
def alive(pid):
looked.append(pid)
return pid == inbox
def ended(seconds):
slept.append(seconds)
waiting.send(self.paths.waiting, self.ask(), waiting.ClaudeEnded())
# loop-inbox's claude ends while the loop waits, or loop-inbox is gone before the loop looks.
for gone in ((), (inbox,)):
with self.subTest(gone=gone):
stop, _ = self.talk(states.Talk(self.ask(), waiting.OpenInInbox(inbox)), dataclasses.replace(FakeEffects(gone=gone).effects(), sleep=ended))
self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed"))
stop, _ = self.talk(states.Talk(self.ask(), waiting.OpenInInbox(inbox)), dataclasses.replace(FakeEffects().effects(), alive=alive, sleep=ended))
self.assertEqual((stop, looked, slept), (Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed"), [inbox], [waiting.POLL_SECONDS]))
def test_stops_when_loop_inbox_is_gone_with_nothing_typed(self):
inbox, looked = 4343, []
def alive(pid):
looked.append(pid)
# loop-inbox's process ends while the loop waits for its claude.
return pid == inbox and len(looked) == 1
fake = FakeEffects()
stop, _ = self.talk(states.Talk(self.ask(), waiting.OpenInInbox(inbox)), dataclasses.replace(fake.effects(), alive=alive))
self.assertEqual((stop, looked, fake.made("sleep")), (Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed"), [inbox, inbox], [waiting.POLL_SECONDS]))
def test_lists_the_claude_open_on_its_terminal_in_loop_inbox_while_it_runs(self):
def ran(args):
@@ -805,10 +815,20 @@ class RoutingCheck(unittest.TestCase):
(subagents / "agent-a.meta.json").write_text(json.dumps({"agentType": "Explore", "toolUseId": "a"}))
def check(self, fake):
"""What check_routing returns and says for a loop in /code/log."""
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as said:
stop = routing_check.check_routing(self.root, Rung("m", "high"), self.paths, fake.effects())
stop = routing_check.check_routing(self.root, "/code/log", Rung("m", "high"), self.paths, fake.effects())
return stop, said.getvalue()
def test_lists_the_calls_in_loop_inbox_while_their_claude_is_open(self):
self.chunks(5, 4)
question = f"loop-claude's routing figures call for a routing change:\n- Explore subagents' reports took more of the worker's context than their searches would have.\nThe figures: {self.root / 'routing-figures.md'}"
def ran(args):
self.assertEqual(waiting.waiting(self.paths.waiting, lambda pid: True), [waiting.Ask(ASK_ID, ASK_ID, "/code/log", question, NOW, PID, waiting.OpenOnTerminal())])
return 0
self.check(FakeEffects(on_terminal_claude=[ran], git=[UNREAD, ""]))
self.assertEqual(list(self.paths.waiting.iterdir()), [])
def test_measures_once_20_cycles_have_closed_since_the_last_measurement(self):
self.chunks(5, 3)
self.assertEqual(self.check(FakeEffects()), (None, ""))
@@ -871,6 +891,7 @@ class RoutingCheck(unittest.TestCase):
self.chunks(5, 4)
stop, _ = self.check(FakeEffects(on_terminal_claude=[OSError(2, "No such file or directory")], git=[UNREAD, ""]))
self.assertEqual(stop, Stop(1, "could not start claude for the routing calls: No such file or directory"))
self.assertEqual(list(self.paths.waiting.iterdir()), [])
@unittest.skipIf(os.geteuid() == 0, "root writes into a read-only directory")
def test_says_a_figure_it_cannot_write_and_goes_on(self):
@@ -1243,6 +1264,28 @@ class LocalTime(unittest.TestCase):
self.assertEqual(system_effects().local_time(NOW), "2026-09-21 16:13:20")
class Alive(unittest.TestCase):
def test_a_process_that_runs_is_alive_and_one_that_ended_is_not(self):
ended = subprocess.Popen([sys.executable, "-c", "pass"])
ended.wait()
# Unless this process runs as root, it may not signal pid 1, which still runs.
self.assertEqual([system_effects().alive(pid) for pid in (os.getpid(), ended.pid, 1)], [True, False, True])
class ClaudeOnTerminal(unittest.TestCase):
def test_leaves_ctrl_c_to_claude_while_it_runs_and_returns_its_exit_code(self):
caught = []
self.addCleanup(signal.signal, signal.SIGINT, signal.signal(signal.SIGINT, lambda number, frame: caught.append(number)))
before = signal.getsignal(signal.SIGINT)
with tempfile.TemporaryDirectory() as folder:
claude = pathlib.Path(folder, "claude")
# A Ctrl-C on the terminal reaches every process in its foreground. kill returns once the signal is pending in the test's process, before claude exits and claude_on_terminal returns.
claude.write_text(f"#!{sys.executable}\nimport os, signal, sys\nos.kill(os.getppid(), signal.SIGINT)\nsys.exit(3)\n")
claude.chmod(0o755)
code = system_effects().claude_on_terminal([], folder, {"PATH": folder})
self.assertEqual((code, caught, signal.getsignal(signal.SIGINT)), (3, [], before))
class PythonEffect(unittest.TestCase):
def test_runs_a_script_in_the_given_directory(self):
with tempfile.TemporaryDirectory() as cwd: