127 lines
4.9 KiB
Python
127 lines
4.9 KiB
Python
import importlib.machinery
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import stat
|
|
import tempfile
|
|
import unittest
|
|
import uuid
|
|
|
|
SESSION_ID = str(getattr(uuid, "uuid7", uuid.uuid4)())
|
|
|
|
LOADER = importlib.machinery.SourceFileLoader("loop_claude", str(pathlib.Path(__file__).with_name("loop-claude")))
|
|
SPEC = importlib.util.spec_from_loader("loop_claude", LOADER)
|
|
loop = importlib.util.module_from_spec(SPEC)
|
|
LOADER.exec_module(loop)
|
|
|
|
|
|
def assistant(*parts):
|
|
return {"type": "assistant", "message": {"content": list(parts)}}
|
|
|
|
|
|
def human(content):
|
|
return {"type": "user", "message": {"content": content}}
|
|
|
|
|
|
class Sections(unittest.TestCase):
|
|
def test_keeps_only_headed_sections_with_a_body(self):
|
|
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n2. two\n\n## Empty\n\n## Audience\nme\n"
|
|
self.assertEqual(loop.sections(text), {"Goals": "1. one\n2. two", "Audience": "me"})
|
|
|
|
|
|
class Render(unittest.TestCase):
|
|
def test_joins_assistant_text_and_tool_calls_and_skips_the_rest(self):
|
|
said = loop.render([
|
|
human("do it"),
|
|
assistant({"type": "text", "text": "Starting."}, {"type": "tool_use", "name": "Bash", "input": {"command": "ls"}}),
|
|
assistant({"type": "text", "text": " "}),
|
|
])
|
|
self.assertEqual(said, 'Starting.\n\n[tool Bash] {"command": "ls"}')
|
|
|
|
|
|
class HumanTurns(unittest.TestCase):
|
|
def test_counts_typed_turns_and_not_tool_results(self):
|
|
self.assertEqual(loop.human_turns([
|
|
human("typed"),
|
|
human([{"type": "text", "text": "typed as a block"}]),
|
|
human([{"type": "tool_result", "content": "not typed"}]),
|
|
human("<command-name>/model</command-name>"),
|
|
{**human("peer message"), "isMeta": True},
|
|
assistant({"type": "text", "text": "said"}),
|
|
]), 2)
|
|
|
|
|
|
class WorkerArgs(unittest.TestCase):
|
|
def test_a_fresh_round_names_the_session_and_asks_to_continue_open_work(self):
|
|
args = loop.worker_args("the task", "A name", SESSION_ID, resuming=False)
|
|
self.assertIn("--session-id", args)
|
|
self.assertEqual(args[args.index("--name") + 1], "A name")
|
|
self.assertTrue(args[1].startswith("the task\n\nContinue any open draft PR"))
|
|
|
|
def test_a_resumed_round_reuses_the_session(self):
|
|
args = loop.worker_args("paint the fence", "A name", SESSION_ID, resuming=True)
|
|
self.assertEqual(args[args.index("--resume") + 1], SESSION_ID)
|
|
self.assertNotIn("--name", args)
|
|
self.assertNotIn("paint the fence", args[1])
|
|
|
|
|
|
class JudgePrompt(unittest.TestCase):
|
|
def test_hands_the_judge_the_tail_of_a_long_transcript(self):
|
|
prompt = loop.judge_prompt("task", "## Goals\n1. g", "early " * 20000 + "LATE")
|
|
self.assertIn("LATE", prompt)
|
|
self.assertLess(prompt.count("early"), 20000)
|
|
self.assertTrue(prompt.endswith(loop.JUDGE_RULES))
|
|
|
|
|
|
class Archive(unittest.TestCase):
|
|
def test_keeps_the_copy_as_private_as_the_transcript(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
transcript = os.path.join(root, "session.jsonl")
|
|
open(transcript, "w").write(json.dumps(assistant({"type": "text", "text": "said"})) + "\n")
|
|
loop.ARCHIVE = os.path.join(root, "archive")
|
|
loop.archive(transcript, "note")
|
|
[copy] = [os.path.join(folder, name) for folder, _, names in os.walk(loop.ARCHIVE) for name in names]
|
|
self.assertEqual(stat.S_IMODE(os.stat(copy).st_mode), 0o600)
|
|
self.assertEqual(stat.S_IMODE(os.stat(os.path.dirname(copy)).st_mode), 0o700)
|
|
self.assertIn("said", open(copy).read())
|
|
|
|
|
|
class Said(unittest.TestCase):
|
|
def test_renders_text_and_tool_calls_and_nothing_else(self):
|
|
self.assertEqual(loop.said({"type": "text", "text": "hi"}), "hi")
|
|
self.assertEqual(loop.said({"type": "text", "text": " \n"}), "")
|
|
self.assertEqual(loop.said({"type": "tool_use", "name": "Bash", "input": {"command": "x" * 500}}, 20), '[tool Bash] {"command": "xxxxxxx')
|
|
self.assertEqual(loop.said("not a part"), "")
|
|
|
|
|
|
class EndSession(unittest.TestCase):
|
|
def test_removes_the_transcript_and_its_sibling_folder(self):
|
|
with tempfile.TemporaryDirectory() as 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")
|
|
loop.end_session(transcript)
|
|
self.assertEqual(os.listdir(root), [])
|
|
|
|
|
|
class Parse(unittest.TestCase):
|
|
def test_yields_only_json_objects(self):
|
|
self.assertEqual(loop.parse('{"type": "user"}'), {"type": "user"})
|
|
self.assertIsNone(loop.parse("not json"))
|
|
self.assertIsNone(loop.parse("[1, 2]"))
|
|
|
|
|
|
class Grounded(unittest.TestCase):
|
|
def test_an_interrupt_must_quote_the_transcript(self):
|
|
self.assertTrue(loop.grounded({"ask": False, "evidence": ""}, "anything"))
|
|
self.assertTrue(loop.grounded({"ask": True, "evidence": "Needs you: x"}, "so.\n\nNeeds you: x\n"))
|
|
self.assertFalse(loop.grounded({"ask": True, "evidence": "invented"}, "the transcript"))
|
|
self.assertFalse(loop.grounded({"ask": True, "evidence": ""}, "the transcript"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|