|
|
|
@@ -19,6 +19,7 @@ from branch_chunks.cut import BranchChunk
|
|
|
|
|
from routing_figures.calls import Call, calls, totals
|
|
|
|
|
from routing_figures.chunk import Chunk, chunk_figures, ChunkFigures, Reports, Subagent
|
|
|
|
|
from routing_figures.state import due
|
|
|
|
|
from claude_code.entries import entry_from
|
|
|
|
|
from failure import Failure
|
|
|
|
|
from git_cli import git_repo
|
|
|
|
|
from test_env import isolated_env
|
|
|
|
@@ -42,6 +43,14 @@ def cost_state(**costs):
|
|
|
|
|
return {"type": "cost-state", "totalCostUSD": sum(costs.values()), "modelUsage": {model.replace("_", "-"): {"costUSD": cost} for model, cost in costs.items()}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chunk_of(project, outcomes, worker, subagents):
|
|
|
|
|
return Chunk(project, outcomes, [entry_from(entry) for entry in worker], subagents)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def subagent_of(kind, launched_by, entries):
|
|
|
|
|
return Subagent(kind, launched_by, [entry_from(entry) for entry in entries])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def call(name, id_="t1", **arguments):
|
|
|
|
|
return {"type": "tool_use", "id": id_, "name": name, "input": arguments}
|
|
|
|
|
|
|
|
|
@@ -80,7 +89,7 @@ class Measured(unittest.TestCase):
|
|
|
|
|
message("m3", "2026-10-14T11:00:00.000Z", {"type": "text", "text": "on"}),
|
|
|
|
|
]
|
|
|
|
|
kept = stamped("2026-10-14T10:30:00Z", outcomes.unfinished(LOW_ROUTE, "e", "j", "")) + stamped("2026-10-14T10:30:00Z", outcomes.stepped_up(Route(HIGH).worker_line())) + stamped("2026-10-14T12:00:00Z", outcomes.finished(Route(HIGH).worker_line(), "merged", "j"))
|
|
|
|
|
found = chunk_figures(Chunk("log", [kept], worker, []), "/h")
|
|
|
|
|
found = chunk_figures(chunk_of("log", [kept], worker, []), "/h")
|
|
|
|
|
self.assertEqual(found.dollars["project"], {"log": 6.0})
|
|
|
|
|
self.assertEqual(found.dollars["subagent type"], {"worker": 6.0})
|
|
|
|
|
self.assertEqual(found.dollars["rung"], {str(LOW): 4.0, str(HIGH): 2.0})
|
|
|
|
@@ -89,7 +98,7 @@ class Measured(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def test_counts_the_judges_recorded_cost_under_the_judge_and_the_rung_it_ran_on(self):
|
|
|
|
|
kept = stamped("2026-10-14T10:30:00Z", outcomes.unfinished(LOW_ROUTE, "e", "judge-rung", "", judge_cost=0.5)) + stamped("2026-10-14T12:00:00Z", outcomes.finished(LOW_ROUTE, "m", "judge-rung", judge_cost=0.25)) + stamped("2026-10-14T12:00:00Z", outcomes.rejected("why", "{}", "other-rung", judge_cost=1.0))
|
|
|
|
|
found = chunk_figures(Chunk("log", [kept], [message("m1", "2026-10-14T10:00:00Z")], []), "/h")
|
|
|
|
|
found = chunk_figures(chunk_of("log", [kept], [message("m1", "2026-10-14T10:00:00Z")], []), "/h")
|
|
|
|
|
self.assertEqual(found.dollars["project"], {"log": 3.75})
|
|
|
|
|
self.assertEqual(found.dollars["subagent type"], {"worker": 2.0, "judge": 1.75})
|
|
|
|
|
self.assertEqual(found.dollars["rung"], {str(LOW): 2.0, "judge-rung": 0.75, "other-rung": 1.0})
|
|
|
|
@@ -102,10 +111,10 @@ class Measured(unittest.TestCase):
|
|
|
|
|
result("a2", "y" * 5),
|
|
|
|
|
]
|
|
|
|
|
subagents = [
|
|
|
|
|
Subagent("Explore", "a1", [message("s1", "2026-10-14T10:00:01Z", model="claude-sonnet-5-5"), result("r", "z" * 20)]),
|
|
|
|
|
Subagent("", "a2", [message("s2", "2026-10-14T10:00:01Z")]),
|
|
|
|
|
subagent_of("Explore", "a1", [message("s1", "2026-10-14T10:00:01Z", model="claude-sonnet-5-5"), result("r", "z" * 20)]),
|
|
|
|
|
subagent_of("", "a2", [message("s2", "2026-10-14T10:00:01Z")]),
|
|
|
|
|
]
|
|
|
|
|
found = chunk_figures(Chunk("log", [stamped("2026-10-14T11:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j"))], worker, subagents), "/h")
|
|
|
|
|
found = chunk_figures(chunk_of("log", [stamped("2026-10-14T11:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j"))], worker, subagents), "/h")
|
|
|
|
|
self.assertEqual(found.delegated, {"claude-sonnet-5-5 at high effort": 1, str(LOW): 1})
|
|
|
|
|
self.assertEqual(found.dollars["subagent type"], {"worker": 2.0, "Explore": 3.0})
|
|
|
|
|
self.assertEqual(found.reports, {"Explore": Reports(2, 35, 20)})
|
|
|
|
@@ -115,16 +124,16 @@ class Measured(unittest.TestCase):
|
|
|
|
|
message("m1", "2026-10-14T10:00:00Z", call("Agent", "a1", prompt="Escalation trigger 2: the test failed twice"), call("Agent", "a2", prompt="Escalation trigger 2: again"), call("Agent", "a3", prompt="Escalation trigger 3: a 🔴 in round 2")),
|
|
|
|
|
message("m2", "2026-10-14T10:01:00Z", call("Agent", "a4", prompt="Find the callers. Escalation trigger 1: not at the start"), call("Agent", "a5", prompt="Escalation trigger x: no number")),
|
|
|
|
|
]
|
|
|
|
|
self.assertEqual(chunk_figures(Chunk("log", [], worker, []), "/h").escalated, {2: 2, 3: 1})
|
|
|
|
|
self.assertEqual(chunk_figures(chunk_of("log", [], worker, []), "/h").escalated, {2: 2, 3: 1})
|
|
|
|
|
|
|
|
|
|
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(chunk_figures(Chunk("log", [], worker, []), "/h").rounds_past_first, 1)
|
|
|
|
|
self.assertEqual(chunk_figures(chunk_of("log", [], worker, []), "/h").rounds_past_first, 1)
|
|
|
|
|
|
|
|
|
|
def fixed_later(self, history, route=LOW_ROUTE):
|
|
|
|
|
"""The later fixes measured counts for a chunk that route started, which LOW_ROUTE does by item 7, on a project whose default branch holds history, None where it could not be read."""
|
|
|
|
|
return chunk_figures(Chunk("log", [stamped("2026-10-14T10:00:00Z", outcomes.finished(route, "m", "j"))], [], []), "/h", history).fixed_later
|
|
|
|
|
return chunk_figures(chunk_of("log", [stamped("2026-10-14T10:00:00Z", outcomes.finished(route, "m", "j"))], [], []), "/h", history).fixed_later
|
|
|
|
|
|
|
|
|
|
def test_counts_the_later_fixes_of_the_chunk_that_shipped_the_item_its_route_names(self):
|
|
|
|
|
self.assertEqual(self.fixed_later([shipping(6, fixed_by=("c", "d", "e")), shipping(5, 7, fixed_by=("c", "d"))]), 2)
|
|
|
|
@@ -144,7 +153,7 @@ class Measured(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def test_starts_no_route_for_a_chunk_no_item_routed(self):
|
|
|
|
|
kept = stamped("2026-10-14T10:00:00Z", outcomes.finished(Route(LOW).worker_line(), "m", "j"))
|
|
|
|
|
found = chunk_figures(Chunk("log", [kept], [message("m1", "2026-10-14T09:00:00Z")], []), "/h")
|
|
|
|
|
found = chunk_figures(chunk_of("log", [kept], [message("m1", "2026-10-14T09:00:00Z")], []), "/h")
|
|
|
|
|
self.assertIsNone(found.start)
|
|
|
|
|
self.assertEqual(found.dollars["rung"], {str(LOW): 2.0})
|
|
|
|
|
|
|
|
|
@@ -155,15 +164,15 @@ class Measured(unittest.TestCase):
|
|
|
|
|
message("m2", "2026-10-14T10:30:00Z", call("Agent", "a1", subagent_type="Explore", model="sonnet")),
|
|
|
|
|
message("m3", "2026-10-14T10:31:00Z", call("Agent", "a2", subagent_type="Explore")),
|
|
|
|
|
]
|
|
|
|
|
subagents = [Subagent("Explore", "a1", [result("r", "x")]), Subagent("Explore", "a2", [message("s2", "2026-10-14T10:31:01Z", model="claude-opus-5-5")])]
|
|
|
|
|
found = chunk_figures(Chunk("log", [kept], worker, subagents), "/h")
|
|
|
|
|
subagents = [subagent_of("Explore", "a1", [result("r", "x")]), subagent_of("Explore", "a2", [message("s2", "2026-10-14T10:31:01Z", model="claude-opus-5-5")])]
|
|
|
|
|
found = chunk_figures(chunk_of("log", [kept], worker, subagents), "/h")
|
|
|
|
|
self.assertEqual(found.delegated, {"sonnet at high effort": 1, str(HIGH): 1})
|
|
|
|
|
self.assertEqual(found.dollars["rung"], {str(LOW): 2.0, str(HIGH): 6.0})
|
|
|
|
|
|
|
|
|
|
def test_names_a_subagent_by_its_meta_or_else_its_call_or_else_unknown(self):
|
|
|
|
|
worker = [message("m1", "2026-10-14T10:00:00Z", call("Agent", "a1", subagent_type="Explore"), call("Agent", "a2", subagent_type="Plan"))]
|
|
|
|
|
subagents = [Subagent("stability-reviewer", "a1", []), Subagent("", "a2", []), Subagent("", "", [])]
|
|
|
|
|
self.assertEqual(chunk_figures(Chunk("log", [], worker, subagents), "/h").reports, {"stability-reviewer": Reports(1, 0, 0), "Plan": Reports(1, 0, 0), "unknown": Reports(1, 0, 0)})
|
|
|
|
|
subagents = [subagent_of("stability-reviewer", "a1", []), subagent_of("", "a2", []), subagent_of("", "", [])]
|
|
|
|
|
self.assertEqual(chunk_figures(chunk_of("log", [], worker, subagents), "/h").reports, {"stability-reviewer": Reports(1, 0, 0), "Plan": Reports(1, 0, 0), "unknown": Reports(1, 0, 0)})
|
|
|
|
|
|
|
|
|
|
def test_counts_a_message_without_usage_or_a_priced_model_as_nothing_and_splits_a_cost_evenly_across_tools(self):
|
|
|
|
|
worker = [
|
|
|
|
@@ -173,20 +182,20 @@ class Measured(unittest.TestCase):
|
|
|
|
|
{"type": "assistant", "uuid": "u3", "message": {"model": "claude-opus-5-5", "content": []}},
|
|
|
|
|
{"type": "assistant", "uuid": "u4", "message": {"model": "claude-unheard-of", "content": [], "usage": {"input_tokens": 9_000_000}}},
|
|
|
|
|
]
|
|
|
|
|
found = chunk_figures(Chunk("log", [], worker, []), "/h")
|
|
|
|
|
found = chunk_figures(chunk_of("log", [], worker, []), "/h")
|
|
|
|
|
self.assertEqual(found.dollars["tool"], {"prompt": 4.0, "Read": 4.0, "Grep": 4.0})
|
|
|
|
|
self.assertEqual(found.dollars["project"], {"log": 12.0})
|
|
|
|
|
|
|
|
|
|
def test_prices_input_cache_writes_by_their_lifetime_cache_reads_and_output_at_the_models_list_prices(self):
|
|
|
|
|
usage = {"input_tokens": 1_000_000, "cache_creation_input_tokens": 2_000_000, "cache_creation": {"ephemeral_5m_input_tokens": 1_000_000, "ephemeral_1h_input_tokens": 1_000_000}, "cache_read_input_tokens": 1_000_000, "output_tokens": 1_000_000}
|
|
|
|
|
opus = chunk_figures(Chunk("log", [], [message("m1", "2026-10-14T10:00:00Z", usage=usage)], []), "/h")
|
|
|
|
|
opus = chunk_figures(chunk_of("log", [], [message("m1", "2026-10-14T10:00:00Z", usage=usage)], []), "/h")
|
|
|
|
|
self.assertAlmostEqual(opus.dollars["project"]["log"], 4 + 5 + 8 + 0.2 + 20)
|
|
|
|
|
sonnet = chunk_figures(Chunk("log", [], [message("m1", "2026-10-14T10:00:00Z", model="claude-sonnet-5-5", usage=usage)], []), "/h")
|
|
|
|
|
sonnet = chunk_figures(chunk_of("log", [], [message("m1", "2026-10-14T10:00:00Z", model="claude-sonnet-5-5", usage=usage)], []), "/h")
|
|
|
|
|
self.assertAlmostEqual(sonnet.dollars["project"]["log"], 2 + 2.5 + 4 + 0.2 + 10)
|
|
|
|
|
|
|
|
|
|
def test_counts_a_cache_write_without_a_lifetime_as_a_five_minute_one(self):
|
|
|
|
|
usage = {"cache_creation_input_tokens": 1_000_000}
|
|
|
|
|
found = chunk_figures(Chunk("log", [], [message("m1", "2026-10-14T10:00:00Z", usage=usage)], []), "/h")
|
|
|
|
|
found = chunk_figures(chunk_of("log", [], [message("m1", "2026-10-14T10:00:00Z", usage=usage)], []), "/h")
|
|
|
|
|
self.assertAlmostEqual(found.dollars["project"]["log"], 5)
|
|
|
|
|
|
|
|
|
|
def test_splits_the_cost_a_chunks_last_cost_state_records_for_each_model_across_its_messages_by_list_price(self):
|
|
|
|
@@ -196,32 +205,32 @@ class Measured(unittest.TestCase):
|
|
|
|
|
cost_state(claude_opus_5_5=1.0),
|
|
|
|
|
cost_state(claude_opus_5_5=9.0, claude_haiku_5_5=0.5),
|
|
|
|
|
]
|
|
|
|
|
subagents = [Subagent("Explore", "a1", [message("s1", "2026-10-14T10:00:01Z")])]
|
|
|
|
|
found = chunk_figures(Chunk("log", [], worker, subagents), "/h")
|
|
|
|
|
subagents = [subagent_of("Explore", "a1", [message("s1", "2026-10-14T10:00:01Z")])]
|
|
|
|
|
found = chunk_figures(chunk_of("log", [], worker, subagents), "/h")
|
|
|
|
|
self.assertEqual(found.dollars["subagent type"], {"worker": 6.0, "Explore": 3.0})
|
|
|
|
|
self.assertEqual(found.dollars["project"], {"log": 9.0})
|
|
|
|
|
self.assertEqual(found.unattributed, 0.5)
|
|
|
|
|
|
|
|
|
|
def test_counts_the_subagents_the_worker_launched_that_have_no_transcript(self):
|
|
|
|
|
worker = [message("m1", "2026-10-14T10:00:00Z", call("Agent", "a1", subagent_type="Explore"), call("Agent", "a2", subagent_type="Explore"), call("Read", "r1", file_path="/c/a"))]
|
|
|
|
|
found = chunk_figures(Chunk("log", [], worker, [Subagent("Explore", "a1", [message("s1", "2026-10-14T10:00:01Z")])]), "/h")
|
|
|
|
|
found = chunk_figures(chunk_of("log", [], worker, [subagent_of("Explore", "a1", [message("s1", "2026-10-14T10:00:01Z")])]), "/h")
|
|
|
|
|
self.assertEqual(found.lacking, 1)
|
|
|
|
|
unnamed = chunk_figures(Chunk("log", [], worker, [Subagent("", "", [message("s1", "2026-10-14T10:00:01Z")]), Subagent("", "", [message("s2", "2026-10-14T10:00:02Z")])]), "/h")
|
|
|
|
|
unnamed = chunk_figures(chunk_of("log", [], worker, [subagent_of("", "", [message("s1", "2026-10-14T10:00:01Z")]), subagent_of("", "", [message("s2", "2026-10-14T10:00:02Z")])]), "/h")
|
|
|
|
|
self.assertEqual(unnamed.lacking, 0)
|
|
|
|
|
nested = [message("s1", "2026-10-14T10:00:01Z", call("Agent", "n1", subagent_type="Explore"))]
|
|
|
|
|
launched = chunk_figures(Chunk("log", [], worker, [Subagent("Explore", "a1", nested), Subagent("Explore", "a2", [message("s2", "2026-10-14T10:00:02Z")]), Subagent("Explore", "n1", [message("s3", "2026-10-14T10:00:03Z")])]), "/h")
|
|
|
|
|
launched = chunk_figures(chunk_of("log", [], worker, [subagent_of("Explore", "a1", nested), subagent_of("Explore", "a2", [message("s2", "2026-10-14T10:00:02Z")]), subagent_of("Explore", "n1", [message("s3", "2026-10-14T10:00:03Z")])]), "/h")
|
|
|
|
|
self.assertEqual(launched.lacking, 0)
|
|
|
|
|
self.assertEqual(chunk_figures(Chunk("log", [], worker, [Subagent("Explore", "a1", nested), Subagent("Explore", "a2", [])]), "/h").lacking, 1)
|
|
|
|
|
self.assertEqual(chunk_figures(chunk_of("log", [], worker, [subagent_of("Explore", "a1", nested), subagent_of("Explore", "a2", [])]), "/h").lacking, 1)
|
|
|
|
|
|
|
|
|
|
def test_charges_a_message_without_a_timestamp_to_the_rung_of_the_last_outcome(self):
|
|
|
|
|
undated = message("m1", "")
|
|
|
|
|
del undated["timestamp"]
|
|
|
|
|
kept = stamped("2026-10-14T10:30:00Z", outcomes.unfinished(LOW_ROUTE, "e", "j", "")) + stamped("2026-10-14T12:00:00Z", outcomes.finished(Route(HIGH).worker_line(), "merged", "j"))
|
|
|
|
|
self.assertEqual(chunk_figures(Chunk("log", [kept], [undated], []), "/h").dollars["rung"], {str(HIGH): 2.0})
|
|
|
|
|
self.assertEqual(chunk_figures(chunk_of("log", [kept], [undated], []), "/h").dollars["rung"], {str(HIGH): 2.0})
|
|
|
|
|
|
|
|
|
|
def test_keeps_list_prices_for_a_model_the_cost_state_does_not_name(self):
|
|
|
|
|
worker = [message("m1", "2026-10-14T10:00:00Z"), message("m2", "2026-10-14T10:01:00Z", model="claude-sonnet-5-5"), cost_state(claude_opus_5_5=3.0)]
|
|
|
|
|
found = chunk_figures(Chunk("log", [], worker, []), "/h")
|
|
|
|
|
found = chunk_figures(chunk_of("log", [], worker, []), "/h")
|
|
|
|
|
self.assertEqual(found.dollars["project"], {"log": 4.0})
|
|
|
|
|
self.assertEqual(found.unattributed, 0.0)
|
|
|
|
|
|
|
|
|
|