103 lines
5.7 KiB
Python
103 lines
5.7 KiB
Python
import contextlib
|
|
import functools
|
|
import io
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import sys
|
|
import tempfile
|
|
import types
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from chunk_driver.doubles import a_loop
|
|
from chunk_driver import cycle_start
|
|
from failure import Failure
|
|
from loop_effects.doubles import FakeEffects, NOW, STAMP
|
|
from git_cli import git_repo
|
|
from transcript_archive import outcomes, write
|
|
from model_routing.ladder import ProjectRung, Rung
|
|
from model_routing.ladder_file import read_ladder, SHIPPED
|
|
from text_files.file_writes import replace_file
|
|
from model_routing.route import Route, WorkerLine
|
|
from claude_code import session
|
|
|
|
OPUS_PROJECT = ProjectRung(read_ladder(SHIPPED), Rung("claude-opus-5-5", "medium"))
|
|
|
|
|
|
class SetupVersion(unittest.TestCase):
|
|
def test_names_the_release_and_the_commit_or_why_git_cannot_tell(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
git = functools.partial(git_repo.git, env={**os.environ, "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", "GIT_CEILING_DIRECTORIES": root})
|
|
gone = cycle_start.setup_version(pathlib.Path(root), git)
|
|
self.assertRegex(gone, r"^unknown: [^\n]*not a git repository")
|
|
for step in (["init", "-q"], ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "one"]):
|
|
self.assertIsInstance(git(*step, cwd=root), str)
|
|
head = git("rev-parse", "HEAD", cwd=root).strip()
|
|
self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"{head[:7]} ({head})")
|
|
git("tag", "v0.1.0", cwd=root)
|
|
self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0 ({head})")
|
|
git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "two", cwd=root)
|
|
head = git("rev-parse", "HEAD", cwd=root).strip()
|
|
self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0-1-g{head[:7]} ({head})")
|
|
|
|
def test_names_why_git_cannot_name_the_commit_on_one_line(self):
|
|
def fake_git(*args, cwd=None):
|
|
return Failure("fatal: bad HEAD\nhint: repair it") if args[0] == "rev-parse" else "v0.1.0\n"
|
|
|
|
self.assertEqual(cycle_start.setup_version(pathlib.Path("/r"), fake_git), "unknown: fatal: bad HEAD hint: repair it")
|
|
|
|
|
|
class Routes(unittest.TestCase):
|
|
OPUS = Rung("claude-opus-5-5", "medium")
|
|
SONNET = Rung("claude-sonnet-5-5", "high")
|
|
|
|
def test_keeps_a_chunks_route_or_its_archived_one_at_or_above_the_rung_its_item_picks_and_else_routes_afresh(self):
|
|
item = types.SimpleNamespace(id=3, likelihood=4, escape_and_impact=5)
|
|
archived = Route(Rung("claude-opus-5-5", "xhigh"), 3, 4, 5)
|
|
def never():
|
|
raise AssertionError("not asked")
|
|
self.assertEqual(cycle_start.routed(archived, None, "worker-model", never, never), Route(Rung("worker-model")))
|
|
self.assertEqual(cycle_start.routed(archived, OPUS_PROJECT, "claude-opus-5-5", never, never), archived)
|
|
self.assertEqual(cycle_start.routed(None, OPUS_PROJECT, "claude-opus-5-5", lambda: archived, never), archived)
|
|
self.assertEqual(cycle_start.routed(None, OPUS_PROJECT, "claude-opus-5-5", lambda: None, lambda: item), Route(Rung("claude-fable-5-2", "medium"), 3, 4, 5))
|
|
self.assertEqual(cycle_start.routed(Route(self.SONNET), OPUS_PROJECT, "claude-opus-5-5", never, lambda: None), Route(self.OPUS))
|
|
cheap = Route(self.SONNET, 4, 2, 3)
|
|
self.assertEqual(cycle_start.routed(cheap, OPUS_PROJECT, "claude-opus-5-5", never, never), cheap)
|
|
self.assertEqual(cycle_start.routed(Route(Rung("claude-haiku-5-5", "max"), 4, 2, 3), OPUS_PROJECT, "claude-opus-5-5", never, lambda: None), Route(self.OPUS))
|
|
|
|
def test_resumes_on_the_archived_route_and_says_why_it_routes_afresh(self):
|
|
with tempfile.TemporaryDirectory() as home:
|
|
opened = session.session_named("A proj a-driver", pathlib.Path(home, "projects"))
|
|
archive = pathlib.Path(home, "archive")
|
|
transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl")
|
|
route = Route(Rung("claude-fable-5-2", "low"), 3, 4, 5)
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)))
|
|
transcript.parent.mkdir(parents=True)
|
|
transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n")
|
|
self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)))
|
|
with contextlib.redirect_stderr(io.StringIO()):
|
|
self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(route.worker_line(), "working", "j", ""), NOW, replace=replace_file))
|
|
with contextlib.redirect_stderr(stderr):
|
|
self.assertEqual(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)), route)
|
|
self.assertEqual(stderr.getvalue(), "")
|
|
with contextlib.redirect_stderr(io.StringIO()):
|
|
self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.stepped_up(WorkerLine('{"model": "claude-opus-5-5", "rung": 3}')), NOW + 1, replace=replace_file))
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)))
|
|
self.assertEqual(stderr.getvalue(), f'{STAMP} routing the chunk afresh: the archive\'s latest Worker: line names no route that can be read: {{"model": "claude-opus-5-5", "rung": 3}}\n')
|
|
(chunk,) = archive.iterdir()
|
|
chunk.chmod(0)
|
|
try:
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)))
|
|
finally:
|
|
chunk.chmod(0o700)
|
|
self.assertTrue(stderr.getvalue().startswith(f"{STAMP} routing the chunk afresh: could not read the archive"), stderr.getvalue())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|