Name the session invariant run_cycle resumes on, and say which outcome ended an interrupt

This commit is contained in:
2026-09-24 23:27:31 +02:00
parent 8301e387e1
commit 67451b418a
4 changed files with 45 additions and 12 deletions
+34 -7
View File
@@ -124,15 +124,37 @@ class Said(unittest.TestCase):
class EndSession(unittest.TestCase):
def test_removes_the_transcript_and_its_sibling_folder(self):
with tempfile.TemporaryDirectory() as root:
def test_removes_the_transcript_and_its_siblings_and_closes_the_session(self):
with tempfile.TemporaryDirectory() as home, unittest.mock.patch.dict(os.environ, {"HOME": home}):
root = os.path.join(home, ".claude", "projects", "p")
os.makedirs(root)
transcript = os.path.join(root, f"{SESSION_ID}.jsonl")
open(transcript, "w").write("{}\n")
os.makedirs(os.path.join(root, SESSION_ID, "subagents"))
os.makedirs(os.path.join(root, f"{SESSION_ID}.superseded-1", "tool-results"))
open(os.path.join(root, f"{SESSION_ID}.orphaned-1-ab.jsonl"), "w").write("{}\n")
self.assertTrue(loop.session_open(SESSION_ID))
loop.end_session(transcript)
self.assertEqual(os.listdir(root), [])
self.assertFalse(loop.session_open(SESSION_ID))
class Interrupt(unittest.TestCase):
def test_says_whether_no_terminal_or_nothing_typed_ended_it(self):
with tempfile.TemporaryDirectory() as root:
transcript = os.path.join(root, "session.jsonl")
open(transcript, "w").write("{}\n")
with unittest.mock.patch("sys.stdin.isatty", return_value=False):
self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "the human is needed and no terminal is attached")
with (
unittest.mock.patch("sys.stdin.isatty", return_value=True),
unittest.mock.patch("sys.stdout.isatty", return_value=True),
unittest.mock.patch.multiple(loop, checkouts=unittest.mock.DEFAULT, trust=unittest.mock.DEFAULT),
unittest.mock.patch("subprocess.run") as run,
):
self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "the interrupt ended without the human typing anything")
run.side_effect = lambda *args, **kwargs: open(transcript, "a").write(json.dumps(human("typed")) + "\n")
self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "")
class Parse(unittest.TestCase):
@@ -166,8 +188,8 @@ if "--json-schema" in args:
open(os.environ["STUB_VERDICTS"], "w").write(" ".join(plan[1:]))
if plan[0] == "fail":
sys.exit(1)
unquoted = plan[0] == "unquoted"
print(json.dumps({"structured_output": {"ask": unquoted, "evidence": "invented" if unquoted else "", "finished": plan[0] == "finished", "question": "q?" if unquoted else ""}}))
ask = plan[0] in ("ask", "unquoted")
print(json.dumps({"structured_output": {"ask": ask, "evidence": {"ask": "worked", "unquoted": "invented"}.get(plan[0], ""), "finished": plan[0] == "finished", "question": "q?" if ask else ""}}))
else:
flag = "--session-id" if "--session-id" in args else "--resume"
folder = os.path.expanduser("~/.claude/projects/p")
@@ -187,9 +209,14 @@ class RunCycle(unittest.TestCase):
open(verdicts, "w").write("fail continue finished unquoted continue")
loop.ARCHIVE = os.path.join(home, "archive")
environment = {"HOME": home, "PATH": f"{home}:{os.environ['PATH']}", "STUB_CALLS": calls, "STUB_VERDICTS": verdicts}
with unittest.mock.patch.dict(os.environ, environment), unittest.mock.patch("time.sleep") as sleep:
with unittest.mock.patch.dict(os.environ, environment), unittest.mock.patch("time.sleep") as sleep, unittest.mock.patch("sys.stdin.isatty", return_value=False):
for _ in range(3):
self.assertTrue(loop.run_cycle("task", "premises", "A name", SESSION_ID))
open(verdicts, "w").write("ask")
with self.assertRaises(SystemExit) as stopped, unittest.mock.patch("sys.stderr") as stderr:
loop.run_cycle("task", "premises", "A name", SESSION_ID)
self.assertEqual(stopped.exception.code, 2)
self.assertIn("no terminal is attached", "".join(str(call.args[0]) for call in stderr.write.call_args_list))
for plan in ("fail fail fail", "unquoted unquoted unquoted"):
open(verdicts, "w").write(plan)
with self.assertRaises(SystemExit) as stopped:
@@ -199,10 +226,10 @@ class RunCycle(unittest.TestCase):
self.assertIn("asking: q?", stopped.exception.code)
self.assertEqual(open(verdicts).read(), "")
starts = [args[args.index("-p") + 1:][:3] for args in map(json.loads, open(calls)) if "--json-schema" not in args]
self.assertEqual([start[1] for start in starts], ["--name", "--resume", "--name", "--resume", "--resume"])
self.assertEqual([start[1] for start in starts], ["--name", "--resume", "--name", "--resume", "--resume", "--resume"])
self.assertEqual(starts[1][2], SESSION_ID)
self.assertEqual(sleep.call_count, 6)
[copy] = [name for _, _, names in os.walk(loop.ARCHIVE) for name in names]
self.assertTrue([name for _, _, names in os.walk(loop.ARCHIVE) for name in names])
class Reload(unittest.TestCase):