diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index f7656094..f5c05ce8 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -88,6 +88,7 @@ class FakeEffects: self.terminal = on_terminal self.worker_environments = [] self.terminal_environments = [] + self.terminal_folders = [] def effects(self): return Effects( @@ -124,6 +125,7 @@ class FakeEffects: def on_terminal_claude(self, args, cwd, env): self.terminal_environments.append(env) + self.terminal_folders.append(cwd) return self.answer("claude_on_terminal", args) def stream(self, args, cwd, env): @@ -301,7 +303,7 @@ class Cycles(unittest.TestCase): with open(session.transcript_file(self.session), "a") as handle: handle.write(json.dumps(human("the second")) + "\n") return 0 - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("finished")], inputs=["/talk"], on_terminal_claude=[answer], git=[""]) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("finished")], inputs=["/talk"], on_terminal_claude=[answer]) cycle = dataclasses.replace(self.cycle(fake), project=Rung("claude-opus-5-5", "medium"), route=Route(Rung("claude-haiku-5-5", "max"), 163, 1, 5)) with contextlib.redirect_stderr(io.StringIO()): self.assertEqual(run.run_cycle(cycle).verdict, Finished("working")) @@ -397,7 +399,7 @@ class Cycles(unittest.TestCase): with open(session.transcript_file(self.session), "a") as handle: handle.write(json.dumps(human("the second")) + "\n") return 0 - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("unfinished")], inputs=["/talk"], on_terminal_claude=[answer], git=[""]) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("unfinished")], inputs=["/talk"], on_terminal_claude=[answer]) with contextlib.redirect_stderr(io.StringIO()): self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Unfinished("working", entries_read=4)) self.assertEqual(self.archived_outcomes(), [f"Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: interrupt\n\nWhich one?\n\nEvidence:\n> working\n\nJudge: judge-model\nWorker: {OFF_LADDER}\n", f"Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: unfinished\n\nEvidence:\n> working\n\nJudge: judge-model\nWorker: {OFF_LADDER}\n"]) @@ -521,7 +523,7 @@ class Cycles(unittest.TestCase): with open(session.transcript_file(self.session), "a") as handle: handle.write(json.dumps(human("cut")) + "\n") return 0 - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt", release_ready=True), judged("finished")], on_terminal_claude=[answer], git=[""]) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt", release_ready=True), judged("finished")], on_terminal_claude=[answer]) with contextlib.redirect_stderr(io.StringIO()): self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Finished("working")) self.assertEqual(fake.made("input"), []) @@ -725,7 +727,7 @@ class Interrupts(unittest.TestCase): def test_talks_on_talk_or_at_once_where_the_answer_is_not_asked_for_here(self): for typed, answer_here in (([" /talk "], True), ([], False)): with self.subTest(typed=typed): - fake = FakeEffects(inputs=typed, on_terminal_claude=[self.typed_there], git=[""]) + fake = FakeEffects(inputs=typed, on_terminal_claude=[self.typed_there]) self.assertEqual(self.put(fake, answer_here)[0], interrupt.Talked()) self.assertEqual(len(fake.made("input")), len(typed)) self.assertEqual(fake.calls[0], ("discard_typed", None)) @@ -761,7 +763,7 @@ class Interrupts(unittest.TestCase): answer, said = self.put(FakeEffects(inputs=["Use B."])) self.assertEqual(answer, "Use B.") self.assertIn(f"loop-inbox cannot show this question: cannot make {self.paths.waiting}: File exists", said) - stop, _ = self.put(FakeEffects(inputs=["/talk"], on_terminal_claude=[0], git=[""])) + stop, _ = self.put(FakeEffects(inputs=["/talk"], on_terminal_claude=[0])) self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed")) def test_makes_way_only_warns_where_loop_inbox_cannot_list_the_open_claude(self): @@ -810,19 +812,19 @@ class Interrupts(unittest.TestCase): def ran(args): self.assertEqual(self.asked(), [self.ask("Cut 0.4.0?", OpenOnTerminal())]) return 0 - fake = FakeEffects(inputs=typed, on_terminal_claude=[ran], git=[""]) + fake = FakeEffects(inputs=typed, on_terminal_claude=[ran]) self.put(fake, answer_here, question="Cut 0.4.0?") self.assertEqual(len(fake.made("claude_on_terminal")), 1) self.assertEqual(asks_and_answers(self.paths.waiting), []) def test_stops_when_nothing_was_typed_in_the_session(self): - fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[0], git=[""]) + fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[0]) stop, _ = self.put(fake) self.assertEqual(fake.made("claude_on_terminal")[0][:4], ["--model", "m", "--effort", "high"]) self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed")) def test_hands_the_claude_that_takes_the_terminal_the_workers_environment(self): - fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[0], git=[""]) + fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[0]) self.put(fake) self.assertEqual(fake.terminal_environments, [{"LOOP_CLAUDE_WORKER_RUNG": "m at high effort", "CLAUDE_CODE_SANDBOXED": "1"}]) @@ -861,7 +863,7 @@ class RoutingCheck(unittest.TestCase): def ran(args): self.assertEqual(waiting(self.paths.waiting), [Ask(ASK_ID, "s1", "/code/log", question, NOW, OpenOnTerminal())]) return 0 - self.check(FakeEffects(on_terminal_claude=[ran], git=[UNREAD, ""])) + self.check(FakeEffects(on_terminal_claude=[ran], git=[UNREAD])) self.assertEqual(asks_and_answers(self.paths.waiting), []) def test_measures_once_20_cycles_have_closed_since_the_last_measurement(self): @@ -881,7 +883,7 @@ class RoutingCheck(unittest.TestCase): def test_hands_the_terminal_over_once_for_each_new_call(self): self.chunks(5, 4) - fake = FakeEffects(on_terminal_claude=[0], git=[UNREAD, ""]) + fake = FakeEffects(on_terminal_claude=[0], git=[UNREAD]) stop, said = self.check(fake) self.assertIsNone(stop) call = "Explore subagents' reports took more of the worker's context than their searches would have." @@ -891,6 +893,7 @@ class RoutingCheck(unittest.TestCase): self.assertEqual(args[:6], ["--model", "m", "--effort", "high", "--permission-mode", "bypassPermissions"]) self.assertIn(f"change nothing until they answer:\n- {call}", args[6]) self.assertEqual(fake.terminal_environments, [{"CLAUDE_CODE_SANDBOXED": "1"}]) + self.assertEqual(fake.terminal_folders, [str(self.home / ".claude")]) (self.root / "routing-figures.md").write_text("Measured x, at cycle 0 across") self.assertEqual(self.check(FakeEffects(git=[UNREAD]))[0], None) self.assertNotIn(call, self.check(FakeEffects())[1]) @@ -898,7 +901,7 @@ class RoutingCheck(unittest.TestCase): def test_stamps_the_figures_and_the_calls_raised_when_it_measures(self): self.chunks(5, 4) (self.root / "routing-calls-raised.md").write_text("# Routing calls raised\n\n- 2026-10-14T10:02:00Z Reset\n") - self.check(FakeEffects(on_terminal_claude=[0], git=[UNREAD, ""])) + self.check(FakeEffects(on_terminal_claude=[0], git=[UNREAD])) figures = (self.root / "routing-figures.md").read_text() self.assertIn(f"Measured {utc(NOW)}, at cycle 20 across", figures) self.assertIn("that finished after the reset of 2026-10-14T10:02:00Z", figures) @@ -924,7 +927,7 @@ class RoutingCheck(unittest.TestCase): def test_stops_when_claude_cannot_start_for_the_calls(self): self.chunks(5, 4) - stop, _ = self.check(FakeEffects(on_terminal_claude=[OSError(2, "No such file or directory")], git=[UNREAD, ""])) + 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(asks_and_answers(self.paths.waiting), []) diff --git a/bin/test_env.py b/bin/test_env.py index 92b56e93..528ac255 100644 --- a/bin/test_env.py +++ b/bin/test_env.py @@ -1,6 +1,7 @@ import os -# A git hook's GIT_DIR, the host's LOOP_CLAUDE_* or PYTHONPATH would point the program under test at the host's own state. +# A git hook's GIT_DIR, the host's LOOP_CLAUDE_* or PYTHONPATH would point the program under test at the host's own state, +# and the host's CLAUDE_CODE_SANDBOXED would hide whether loop-claude sets it. def isolated_env() -> dict[str, str]: - return {name: value for name, value in os.environ.items() if not name.startswith(("GIT_", "LOOP_CLAUDE_", "PYTHON"))} + return {name: value for name, value in os.environ.items() if not name.startswith(("GIT_", "LOOP_CLAUDE_", "PYTHON")) and name != "CLAUDE_CODE_SANDBOXED"}