The readers put the neutral name in a tool_call's `tool` and the record's own in `native` (readers/toolnames.py): Bash → shell, Read → read, Agent/Task → agent, the five task tools → todo, CronCreate → claude:CronCreate; a converter-written legacy file namespaces its own tools under its entrypoint (agy:manage_task, opencode:execute), an unstamped one's lowercase names are pi's. MCP names are untouched. The fold, the cost buckets (bash → shell, task → todo; TaskOutput / TaskStop now count as shell, TaskGet / TaskList as todo), taskevents, the resource miner, the lifecycle / deploy / worktree detection, tree.py's agent cards, spawned children, the scaffold and the bgtasks route key on the neutral names only. The `todo` calls are told apart by their input (tools.todo_op). A tool card carries `native` when it differs. PARSER_VERSION 45 (a full re-index). docs/transcript-format.md gets the "Tool names" table with the agy column filled from sidecar/agy_tools.py; the v1 fixtures use the neutral names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
6.6 KiB
Python
147 lines
6.6 KiB
Python
"""A local (EVOX2) call is priced from its duration: the box's calibrated wall
|
||
power × the time it worked, at the electricity tariff of the call's date, plus
|
||
the box's amortisation per busy hour — on a pi transcript (one record per call)
|
||
and on an opencode step whose usage lands after its tools ran.
|
||
``python3 -m unittest test_local_energy``"""
|
||
|
||
import json
|
||
import pathlib
|
||
import tempfile
|
||
import unittest
|
||
|
||
from conversations import pricing
|
||
from conversations.parse import parse_conversation
|
||
|
||
TMP = pathlib.Path(tempfile.mkdtemp())
|
||
SID = "22222222-3333-4444-5555-666666666666"
|
||
USAGE = {"input_tokens": 4000, "output_tokens": 300,
|
||
"cache_read_input_tokens": 0, "cache_creation_input_tokens": 0}
|
||
|
||
|
||
def _at(s: int) -> str:
|
||
return f"2026-10-18T10:{s // 60:02d}:{s % 60:02d}.000Z"
|
||
|
||
|
||
def _write(recs, entrypoint=None) -> pathlib.Path:
|
||
p = TMP / f"{SID}.jsonl"
|
||
extra = {"entrypoint": entrypoint} if entrypoint else {}
|
||
p.write_text("".join(json.dumps({"sessionId": SID, "cwd": "/home/u/repo", **extra, **r}) + "\n"
|
||
for r in recs))
|
||
return p
|
||
|
||
|
||
def _user(s, content):
|
||
return {"type": "user", "timestamp": _at(s), "message": {"role": "user", "content": content}}
|
||
|
||
|
||
def _asst(s, mid, model, content, usage=USAGE, **kw):
|
||
msg = {"id": mid, "role": "assistant", "model": model, "content": content}
|
||
if usage is not None:
|
||
msg["usage"] = usage
|
||
return {"type": "assistant", "timestamp": _at(s), "requestId": mid, "message": msg, **kw}
|
||
|
||
|
||
def _cost(model, seconds, ts=_at(0)):
|
||
c = pricing.local_call(model, seconds * 1000, ts)
|
||
return c["electricity"] + c["hardware"]
|
||
|
||
|
||
class Pricing(unittest.TestCase):
|
||
def test_a_call_costs_the_boxs_time(self):
|
||
c = pricing.local_call("qwen3.6-35b-a3b", 3_600_000, "2026-10-18T10:00:00Z")
|
||
self.assertAlmostEqual(c["seconds"], 3600)
|
||
self.assertAlmostEqual(c["wh"], 111.0)
|
||
self.assertAlmostEqual(c["electricity"], 0.111 * 0.2001 * pricing.USD_PER_EUR)
|
||
self.assertAlmostEqual(c["hardware"], 2049 / (5 * 613) * pricing.USD_PER_EUR)
|
||
|
||
def test_watts_per_model(self):
|
||
self.assertEqual(pricing.local_watts("evox2/ggml-org/qwen3.8-27b"), 116.0) # opencode twin
|
||
self.assertEqual(pricing.local_watts("some-new-local"), pricing.LOCAL_WATTS_DEFAULT)
|
||
self.assertEqual(pricing.local_watts("evox2-inkling/inkling-2-mini"),
|
||
pricing.local_watts("inkling-2-mini"))
|
||
|
||
def test_tariff_of_the_calls_date(self):
|
||
self.assertEqual(pricing.electricity_eur_per_kwh("2026-07-31T23:59:00Z"), 0.1940)
|
||
self.assertEqual(pricing.electricity_eur_per_kwh("2026-08-01T00:00:00Z"), 0.2001)
|
||
self.assertEqual(pricing.electricity_eur_per_kwh("2025-01-01T00:00:00Z"), 0.1940)
|
||
self.assertEqual(pricing.electricity_eur_per_kwh(None), 0.2001)
|
||
|
||
def test_stalls_are_capped_and_unknown_durations_free(self):
|
||
self.assertEqual(pricing.local_call("x", 10 * 3_600_000, None)["seconds"],
|
||
pricing.LOCAL_CALL_CAP_S)
|
||
self.assertEqual(pricing.local_call("x", None, None)["wh"], 0)
|
||
|
||
def test_tokens_stay_free(self):
|
||
u = pricing.norm_usage(USAGE)
|
||
self.assertEqual(pricing.usage_cost(u, "qwen3.6-35b-a3b"), 0)
|
||
self.assertEqual(pricing.plan_units(u, "qwen3.6-35b-a3b"), 0)
|
||
|
||
|
||
class Parse(unittest.TestCase):
|
||
def test_pi_calls_priced_by_duration_over_the_runners_zero(self):
|
||
m = "qwen3.6-35b-a3b"
|
||
p = _write([
|
||
_user(0, "list files"),
|
||
_asst(30, "m1", m, [{"type": "tool_use", "id": "t1", "name": "Bash",
|
||
"input": {"command": "ls"}}], costUSD=0),
|
||
_user(32, [{"type": "tool_result", "tool_use_id": "t1", "content": "a.py"}]),
|
||
_asst(122, "m2", m, [{"type": "text", "text": "a.py. DONE"}], costUSD=0),
|
||
])
|
||
s = parse_conversation(p, full=False)
|
||
self.assertAlmostEqual(s["local"]["seconds"], 120) # 30 s + 90 s, not the tool's 2 s
|
||
self.assertAlmostEqual(s["local"]["wh"], 111 * 120 / 3600)
|
||
self.assertAlmostEqual(s["cost"], _cost(m, 30) + _cost(m, 90))
|
||
self.assertAlmostEqual(s["cost"], s["local"]["electricity"] + s["local"]["hardware"])
|
||
self.assertAlmostEqual(s["byModel"][m]["cost"], s["cost"])
|
||
self.assertEqual(s["planUnits"], 0)
|
||
self.assertEqual(s["meterBins"], {}) # no plan's $
|
||
t = parse_conversation(p, full=True)
|
||
card = next(i for i in t["thread"] if i.get("name") == "shell")
|
||
self.assertAlmostEqual(card["turnCost"], _cost(m, 30))
|
||
|
||
def test_opencode_step_takes_its_streams_duration(self):
|
||
# The step's content is written when its stream ends (40 s after the
|
||
# prompt); its usage once its tool has run (5 min later) — the box only
|
||
# worked the 40 s.
|
||
m = "evox2/qwen3.6-35b-a3b"
|
||
p = _write([
|
||
_user(0, "list files"),
|
||
_asst(40, "s1", m, [{"type": "tool_use", "id": "c1", "name": "Bash",
|
||
"input": {"command": "sleep 300"}}], usage=None),
|
||
_user(340, [{"type": "tool_result", "tool_use_id": "c1", "content": "ok"}]),
|
||
_asst(341, "s1", m, [], usage=USAGE),
|
||
], entrypoint="opencode")
|
||
s = parse_conversation(p, full=False)
|
||
self.assertAlmostEqual(s["local"]["seconds"], 40)
|
||
self.assertAlmostEqual(s["cost"], _cost(m, 40))
|
||
self.assertAlmostEqual(s["byTool"]["shell"]["cost"] + s["byTool"]["context"]["cost"],
|
||
s["cost"])
|
||
|
||
def test_a_claude_conversation_has_no_local_share(self):
|
||
p = _write([
|
||
_user(0, "hi"),
|
||
_asst(5, "c1", "claude-opus-5-5", [{"type": "text", "text": "hello"}]),
|
||
])
|
||
s = parse_conversation(p, full=False)
|
||
self.assertIsNone(s["local"])
|
||
self.assertAlmostEqual(s["cost"], pricing.usage_cost(pricing.norm_usage(USAGE),
|
||
"claude-opus-5-5"))
|
||
|
||
|
||
class ProjectRollup(unittest.TestCase):
|
||
def test_a_local_runs_cost_splits_by_the_boxs_time(self):
|
||
from projects.costs import _aggregate
|
||
u = pricing.norm_usage(USAGE)
|
||
s = {"model": "qwen3.6-35b-a3b", "cost": 0.17, "tokens": 4300, "usage": u,
|
||
"byModel": {"qwen3.6-35b-a3b": {"usage": u, "cost": 0.17}}}
|
||
agg = _aggregate([s])
|
||
by = agg["byType"]
|
||
self.assertAlmostEqual(sum(v["cost"] for v in by.values()), 0.17)
|
||
# 300 output tokens weigh 300 × 16 = 4800 against 4000 input
|
||
self.assertAlmostEqual(by["output"]["cost"] / by["input"]["cost"], 4800 / 4000)
|
||
self.assertEqual(by["cacheRead"]["cost"], 0)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|