import json import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from claude_code import entries from loop_effects.doubles import FakeEffects, judged, ran from agent_cycle import judge, spend from agent_cycle.verdicts import Judged, Unfinished from transcript_archive import outcomes from claude_code.tokens import Tokens, tokens_of def turn(message_id, model, at, **usage): return entries.AssistantTurn(message_id=message_id, model=model, timestamp=at, usage=usage) class Spend(unittest.TestCase): def test_reads_the_cache_writes_by_lifetime_and_counts_one_without_the_split_as_five_minutes(self): self.assertEqual(tokens_of({"input_tokens": 1, "cache_creation_input_tokens": 9, "cache_creation": {"ephemeral_5m_input_tokens": 2, "ephemeral_1h_input_tokens": 3}, "cache_read_input_tokens": 4, "output_tokens": 5}), Tokens(1, 2, 3, 4, 5)) self.assertEqual(tokens_of({"input_tokens": 1, "cache_creation_input_tokens": 9}), Tokens(1, 9, 0, 0, 0)) self.assertEqual(tokens_of(None), Tokens()) def test_counts_a_messages_context_as_every_prompt_token_and_no_output(self): self.assertEqual(Tokens(1, 2, 3, 4, 5).context(), 10) def test_counts_a_message_written_in_the_stamped_second_or_without_a_readable_time_as_before_since(self): thread = [turn("m1", "a", "2026-11-01T10:00:01Z", output_tokens=7), turn("m2", "a", "not a time", output_tokens=3), turn("m3", "a", "2026-11-01T10:00:02Z", output_tokens=1)] self.assertEqual(spend.spent("2026-11-01T10:00:01Z", thread, [], "j", Tokens()), {("worker", "a"): Tokens(output=1)}) def test_sums_two_subagents_of_one_name_on_one_model(self): helpers = [spend.Subagent("Explore", [turn("s1", "b", "2026-11-01T10:00:00Z", input_tokens=3)]), spend.Subagent("Explore", [turn("s2", "b", "2026-11-01T10:00:00Z", input_tokens=4)])] self.assertEqual(spend.spent("", [], helpers, "j", Tokens()), {("Explore", "b"): Tokens(input=7)}) def test_counts_each_message_once_by_its_id_and_only_after_since(self): thread = [ turn("m1", "a", "2026-11-01T10:00:00Z", output_tokens=7), turn("m2", "a", "2026-11-01T10:00:02Z", input_tokens=1, output_tokens=2), turn("m2", "a", "2026-11-01T10:00:02Z", input_tokens=1, output_tokens=2), turn("m3", "b", "2026-11-01T10:00:03Z", cache_read_input_tokens=5), ] self.assertEqual(spend.thread_spend(thread, "2026-11-01T10:00:01Z"), {"a": Tokens(1, 0, 0, 0, 2), "b": Tokens(0, 0, 0, 5, 0)}) self.assertEqual(spend.thread_spend(thread, ""), {"a": Tokens(1, 0, 0, 0, 9), "b": Tokens(0, 0, 0, 5, 0)}) def test_names_each_spender_and_leaves_out_one_that_spent_nothing(self): worker = [turn("m1", "a", "2026-11-01T10:00:00Z", output_tokens=7)] helper = spend.Subagent('Explore "Search"', [turn("s1", "b", "2026-11-01T10:00:00Z", input_tokens=3)]) self.assertEqual(spend.spent("", worker, [helper], "j", Tokens()), {("worker", "a"): Tokens(output=7), ('Explore "Search"', "b"): Tokens(input=3)}) self.assertEqual(spend.spent("", [], [], "j", Tokens(input=1)), {("judge", "j"): Tokens(input=1)}) def test_names_a_subagent_by_its_agent_type_and_description(self): with tempfile.TemporaryDirectory() as root: folder = pathlib.Path(root, "s", "subagents") folder.mkdir(parents=True) for name, meta in [("agent-a", {"agentType": "Explore", "description": "Search the hooks"}), ("agent-b", None), ("agent-c", ["Explore"]), ("agent-d", {"description": "Läs"}), ("agent-e", {"agentType": "Ex\nConfig: x"})]: (folder / f"{name}.jsonl").write_text(json.dumps({"type": "assistant", "message": {"id": name, "model": "m", "content": [], "usage": {"output_tokens": 1}}, "timestamp": "2026-11-01T10:00:00Z"}) + "\n") if meta: (folder / f"{name}.meta.json").write_text(json.dumps(meta)) found = spend.subagents(str(pathlib.Path(root, "s.jsonl"))) self.assertEqual([subagent.name for subagent in found], ['Explore "Search the hooks"', "subagent", "subagent", 'subagent "Läs"', "Ex Config: x"]) self.assertEqual(spend.spent("", [], found[:2], "j", Tokens()), {('Explore "Search the hooks"', "m"): Tokens(output=1), ("subagent", "m"): Tokens(output=1)}) self.assertEqual(spend.subagents(str(pathlib.Path(root, "none.jsonl"))), []) def test_renders_the_spend_then_the_list_prices_of_each_model_it_knows(self): found = {("worker", "claude-opus-5-5"): Tokens(1, 2, 3, 4, 5), ("judge", "unpriced"): Tokens(input=6)} self.assertEqual(outcomes.spent_block("2026-11-01T10:00:00Z", found), """Spent since 2026-11-01T10:00:00Z, in tokens of input, cache write 5m, cache write 1h, cache read, output: - worker, claude-opus-5-5: 1, 2, 3, 4, 5 - judge, unpriced: 6, 0, 0, 0, 0 List prices in dollars per million tokens of input, cache write 5m, cache write 1h, cache read, output: - claude-opus-5-5: 4, 5, 8, 0.2, 20""") self.assertEqual(outcomes.spent_block("", {("judge", "unpriced"): Tokens(input=6)}), "Spent since the chunk began, in tokens of input, cache write 5m, cache write 1h, cache read, output:\n- judge, unpriced: 6, 0, 0, 0, 0") self.assertEqual(outcomes.spent_block("", {}), "") def test_keeps_the_whole_typed_answer_that_holds_the_judges_quote_and_its_tokens(self): shown = "[human] Use A.\n[human] No, keep the old name. Use X.\nworking: keep the new name" typed = ("Use A.", "No, keep the old name. Use X.") for quoted, kept in [("No, keep the old name. Use X.", "No, keep the old name. Use X."), ("keep the old name", "No, keep the old name. Use X."), ("Use", "No, keep the old name. Use X."), ("keep the new name", ""), ("No, keep the name.", "")]: with self.subTest(quoted=quoted): answer = json.loads(judged("unfinished", reversed=quoted).stdout) fake = FakeEffects(claude=[ran(json.dumps({**answer, "usage": {"input_tokens": 2, "output_tokens": 3}}))]) self.assertEqual(judge.run_judge("/r", [], "", judge.Grounds(shown, shown, "", typed), fake.effects()), Unfinished("working", judged=Judged(tokens=Tokens(input=2, output=3), reversed=kept))) if __name__ == "__main__": unittest.main()