159 lines
8.9 KiB
Python
159 lines
8.9 KiB
Python
import collections
|
|
import json
|
|
import pathlib
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from routing_figures import archive
|
|
from routing_figures.measure import calls, Chunk, ChunkFigures, measured, render, Reports, Subagent, totals
|
|
from failure import Failure
|
|
from transcript_archive import outcomes
|
|
from model_routing.ladder import Rung
|
|
|
|
LOW = Rung("claude-opus-5-5", "medium")
|
|
HIGH = Rung("claude-opus-5-5", "high")
|
|
LOW_ROUTE = f"{LOW}, routed by item 7's R + C of 9"
|
|
|
|
|
|
def message(id_, at, *parts, model="claude-opus-5-5", tokens=(1, 10, 100)):
|
|
return {"type": "assistant", "timestamp": at, "cwd": "/c", "message": {"id": id_, "model": model, "content": list(parts), "usage": {"input_tokens": tokens[0], "cache_creation_input_tokens": tokens[1], "output_tokens": tokens[2], "cache_read_input_tokens": 5000}}}
|
|
|
|
|
|
def call(name, id_="t1", **arguments):
|
|
return {"type": "tool_use", "id": id_, "name": name, "input": arguments}
|
|
|
|
|
|
def result(id_, text):
|
|
return {"type": "user", "message": {"content": [{"type": "tool_result", "tool_use_id": id_, "content": [{"type": "text", "text": text}]}]}}
|
|
|
|
|
|
def stamped(at, outcome):
|
|
return f"{at} {outcome}\n"
|
|
|
|
|
|
def figures(start=LOW, rounds=0, unfinished=0, reports=None):
|
|
return ChunkFigures({}, start, rounds, unfinished, collections.Counter(), reports or {})
|
|
|
|
|
|
class Measured(unittest.TestCase):
|
|
def test_counts_each_messages_tokens_once_by_project_type_rung_and_the_tool_it_followed(self):
|
|
worker = [
|
|
message("m1", "2026-10-14T10:00:00.100Z", {"type": "text", "text": "reading"}),
|
|
message("m1", "2026-10-14T10:00:00.200Z", call("Read", file_path="/c/a")),
|
|
message("m2", "2026-10-14T10:05:00.000Z", {"type": "text", "text": "done"}),
|
|
message("m3", "2026-10-14T11:00:00.000Z", {"type": "text", "text": "on"}),
|
|
]
|
|
kept = stamped("2026-10-14T10:30:00Z", outcomes.unfinished(LOW_ROUTE, "e")) + stamped("2026-10-14T10:30:00Z", outcomes.stepped_up(str(HIGH))) + stamped("2026-10-14T12:00:00Z", outcomes.finished(str(HIGH), "merged"))
|
|
found = measured(Chunk("log", kept, worker, []), "/h")
|
|
self.assertEqual(found.tokens["project"], {"log": 333})
|
|
self.assertEqual(found.tokens["subagent type"], {"worker": 333})
|
|
self.assertEqual(found.tokens["rung"], {str(LOW): 222, str(HIGH): 111})
|
|
self.assertEqual(found.tokens["tool"], {"prompt": 222, "Read": 111})
|
|
self.assertEqual((found.start, found.unfinished), (LOW, 1))
|
|
|
|
def test_reads_a_subagents_rung_from_its_model_and_the_effort_its_call_set_or_else_the_workers(self):
|
|
worker = [
|
|
message("m1", "2026-10-14T10:00:00Z", call("Agent", "a1", subagent_type="Explore", model="sonnet", effort="high", prompt="find"), call("Agent", "a2", subagent_type="Explore", prompt="find")),
|
|
result("a1", "x" * 30),
|
|
result("a2", "y" * 5),
|
|
]
|
|
subagents = [
|
|
Subagent({"agentType": "Explore", "toolUseId": "a1"}, [message("s1", "2026-10-14T10:00:01Z", model="claude-sonnet-5-5"), result("r", "z" * 20)]),
|
|
Subagent({"agentType": "Explore", "toolUseId": "a2"}, [message("s2", "2026-10-14T10:00:01Z")]),
|
|
]
|
|
found = measured(Chunk("log", stamped("2026-10-14T11:00:00Z", outcomes.finished(LOW_ROUTE, "m")), worker, subagents), "/h")
|
|
self.assertEqual(found.delegated, {"claude-sonnet-5-5 at high effort": 1, str(LOW): 1})
|
|
self.assertEqual(found.tokens["subagent type"], {"worker": 111, "Explore": 222})
|
|
self.assertEqual(found.reports, {"Explore": Reports(2, 35, 20)})
|
|
|
|
def test_counts_review_rounds_past_each_passs_first_once_per_title(self):
|
|
commit = lambda id_, title: message(id_, "2026-10-14T10:00:00Z", call("Bash", command=f"git commit -m '{title}'"))
|
|
worker = [commit("m1", "Answer stability round 1: 1 🔴, 0 🟡"), commit("m2", "Answer stability round 2: 0 🔴, 0 🟡"), commit("m3", "Answer stability round 2: 0 🔴, 0 🟡"), commit("m4", "Answer prose round 1: 0 🔴, 1 🟡")]
|
|
self.assertEqual(measured(Chunk("log", "", worker, []), "/h").rounds_past_first, 1)
|
|
|
|
def test_starts_no_route_for_a_chunk_no_item_routed(self):
|
|
kept = stamped("2026-10-14T10:00:00Z", outcomes.finished(str(LOW), "m"))
|
|
found = measured(Chunk("log", kept, [message("m1", "2026-10-14T09:00:00Z")], []), "/h")
|
|
self.assertIsNone(found.start)
|
|
self.assertEqual(found.tokens["rung"], {str(LOW): 111})
|
|
|
|
|
|
class Calls(unittest.TestCase):
|
|
def test_calls_when_chunks_on_a_rung_took_more_rounds_or_unfinished_verdicts_than_the_rung_above(self):
|
|
chunks = [figures(LOW, rounds=2)] * 5 + [figures(HIGH, unfinished=1)] * 5
|
|
self.assertEqual(calls(totals(chunks)), [
|
|
f"Chunks routed onto {LOW} took more review rounds past each pass's first than chunks routed onto {HIGH}.",
|
|
])
|
|
self.assertEqual(calls(totals([figures(LOW, unfinished=1)] * 5 + [figures(HIGH)] * 5)), [
|
|
f"Chunks routed onto {LOW} took more unfinished verdicts than chunks routed onto {HIGH}.",
|
|
])
|
|
|
|
def test_compares_only_rungs_with_five_chunks_each(self):
|
|
self.assertEqual(calls(totals([figures(LOW, rounds=2)] * 5 + [figures(HIGH)] * 4)), [])
|
|
|
|
def test_calls_when_a_subagent_types_reports_outgrow_the_searches_they_ran(self):
|
|
self.assertEqual(calls(totals([figures(reports={"Explore": Reports(1, 30, 20)})] * 5)), [
|
|
"Explore subagents' reports took more of the worker's context than their searches would have.",
|
|
])
|
|
self.assertEqual(calls(totals([figures(reports={"Explore": Reports(1, 30, 20)})] * 4)), [])
|
|
self.assertEqual(calls(totals([figures(reports={"Explore": Reports(1, 20, 30)})] * 5)), [])
|
|
|
|
def test_renders_each_dimension_largest_first_and_the_calls(self):
|
|
chunk = ChunkFigures({"project": collections.Counter({"a": 2, "b": 20})}, LOW, 1, 0, collections.Counter({str(HIGH): 2}), {"Explore": Reports(2, 3, 4)})
|
|
text = render(totals([chunk]), ["Call."], "2026-10-15T00:00:00Z", 40)
|
|
self.assertIn("Measured 2026-10-15T00:00:00Z, at cycle 40 across loop-claude's archive, from 1 finished chunk.", text)
|
|
self.assertIn("## Calls for a routing change\n\n- Call.\n", text)
|
|
self.assertIn("| b | 20 |\n| a | 2 |", text)
|
|
self.assertIn(f"| {LOW} | 1 | 1.0 | 0.0 | {HIGH} 2 |", text)
|
|
self.assertIn("| Explore | 2 | 3 | 4 |", text)
|
|
self.assertIn("## Calls for a routing change\n\nNone.\n", render(totals([]), [], "2026-10-15T00:00:00Z", 20))
|
|
|
|
|
|
class Archive(unittest.TestCase):
|
|
def setUp(self):
|
|
self.root = pathlib.Path(tempfile.mkdtemp())
|
|
self.addCleanup(lambda: __import__("shutil").rmtree(self.root))
|
|
|
|
def chunk(self, key, name, kept, worker, subagent=None):
|
|
folder = self.root / key / name
|
|
(folder / "session" / "s" / "subagents").mkdir(parents=True)
|
|
(folder / "copy.outcomes.md").write_text(f"Task: t\n\n{kept}")
|
|
(folder / "copy.jsonl").write_text("".join(json.dumps(entry) + "\n" for entry in worker))
|
|
if subagent:
|
|
meta, entries = subagent
|
|
(folder / "session" / "s" / "subagents" / "agent-a.jsonl").write_text("".join(json.dumps(entry) + "\n" for entry in entries))
|
|
(folder / "session" / "s" / "subagents" / "agent-a.meta.json").write_text(json.dumps(meta))
|
|
|
|
def test_counts_the_verdicts_across_every_projects_chunks(self):
|
|
self.chunk("home/x", "c1", stamped("2026-10-14T10:00:00Z", outcomes.unfinished("w", "e")) + stamped("2026-10-14T10:01:00Z", outcomes.stopped("w", "Ctrl-C")) + stamped("2026-10-14T10:02:00Z", outcomes.finished("w", "e")), [])
|
|
self.chunk("log", "c2", stamped("2026-10-14T10:00:00Z", outcomes.interrupted("q", "e")), [])
|
|
self.assertEqual(archive.cycles(self.root), 2)
|
|
self.assertEqual(archive.cycles(self.root / "missing"), 0)
|
|
|
|
def test_measures_each_finished_chunk_keyed_by_its_project(self):
|
|
self.chunk("home/x", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m")), [message("m1", "2026-10-14T09:00:00Z", call("Agent", "a1", subagent_type="Explore"))], ({"agentType": "Explore", "toolUseId": "a1"}, [message("s1", "2026-10-14T09:00:01Z")]))
|
|
self.chunk("log", "c2", stamped("2026-10-14T10:00:00Z", outcomes.unfinished(LOW_ROUTE, "e")), [message("m1", "2026-10-14T09:00:00Z")])
|
|
found = archive.measure_archive(self.root, "/h")
|
|
self.assertEqual([chunk.tokens["project"] for chunk in found], [{"home/x": 222}])
|
|
self.assertEqual(found[0].tokens["subagent type"], {"worker": 111, "Explore": 111})
|
|
|
|
def test_keeps_the_figures_and_says_which_calls_it_raises_for_the_first_time(self):
|
|
self.assertIsNone(archive.write_figures(self.root, "# Routing figures\n"))
|
|
self.assertEqual((self.root / archive.FIGURES).read_text(), "# Routing figures\n")
|
|
self.assertEqual(archive.unraised(self.root, ["A.", "B."]), ["A.", "B."])
|
|
self.assertIsNone(archive.mark_raised(self.root, ["A."], "2026-10-15T00:00:00Z"))
|
|
self.assertEqual(archive.unraised(self.root, ["A.", "B."]), ["B."])
|
|
self.assertIsNone(archive.mark_raised(self.root, ["B."], "2026-10-15T01:00:00Z"))
|
|
self.assertEqual((self.root / archive.RAISED).read_text(), "# Routing calls raised\n\n- 2026-10-15T00:00:00Z A.\n- 2026-10-15T01:00:00Z B.\n")
|
|
|
|
def test_fails_on_an_archive_it_cannot_read(self):
|
|
(self.root / archive.RAISED).mkdir()
|
|
self.assertIsInstance(archive.unraised(self.root, ["A."]), Failure)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|