diff --git a/agent_cost_estimator/hooks/mistral-vibe-cli/post_turn.py b/agent_cost_estimator/hooks/mistral-vibe-cli/post_turn.py index 93cbd30..3fc5d96 100644 --- a/agent_cost_estimator/hooks/mistral-vibe-cli/post_turn.py +++ b/agent_cost_estimator/hooks/mistral-vibe-cli/post_turn.py @@ -23,7 +23,13 @@ usage data from: ├── journal/*.jsonl └── generations/*/projection-state.json -Output format: provider:model | session:$XXX | turn:$YYY +Output format (system_message, two lines): + -- provider:model | session:$XXX | turn:$YYY + -- I: O: C: +Token counts are session totals, formatted with automatic suffixes +(k, M, B, T) — the same shape as the CLI's default output. The token +line is omitted when the JSON report carries no token fields (older +harness reports), so the hook falls back to the cost line alone. Model Detection: - Primary: session meta.json config.model_name / config.active_model @@ -97,10 +103,37 @@ def get_session_dir() -> Path: return default_dir +def _format_token_count(count) -> str: + """Format a token count with magnitude suffixes (k, M, B, T). + + Stdlib-only copy of agent_cost_estimator.utils.formatting + .format_token_count: the hook must not import the package + (it may run where only the script is installed), and the two + implementations must stay in sync. + """ + try: + count = int(count or 0) + except (TypeError, ValueError): + return "0" + if count < 1000: + return str(count) + elif count < 1_000_000: + return f"{count / 1000:.2f}k" + elif count < 1_000_000_000: + return f"{count / 1_000_000:.2f}M" + elif count < 1_000_000_000_000: + return f"{count / 1_000_000_000:.2f}B" + else: + return f"{count / 1_000_000_000_000:.2f}T" + + def format_cost_output(stdout: str) -> str: """Format the agent-cost-estimator JSON output into hook message format. - - Expected format: provider:model | session:$XXX | turn:$YYY + + Expected format (system_message gets a '-- ' prefix on the first + line from the caller): + provider:model | session:$XXX | turn:$YYY + -- I: O: C: """ try: data = json.loads(stdout) @@ -111,14 +144,33 @@ def format_cost_output(stdout: str) -> str: model_str = f"{provider}:{model}" else: model_str = f"unknown:{model_key}" - + # Extract costs session_cost = data.get("cost_usd", {}).get("total_usd", 0) last_turn = data.get("last_turn") turn_cost = last_turn.get("cost_usd", {}).get("total_usd", 0) if last_turn else 0 - - # Format: provider:model | session:$XXX | turn:$YYY - return f"{model_str} | session:${session_cost:.2f} | turn:${turn_cost:.2f}" + + # Cost line: provider:model | session:$XXX | turn:$YYY + lines = [ + f"{model_str} | session:${session_cost:.2f} | turn:${turn_cost:.2f}" + ] + + # Token line from session totals (per-turn token counts are not + # part of the harness report). Only present when the report has + # the token fields; the cost line never depends on them. + has_tokens = any( + data.get(field) is not None + for field in ("input_tokens", "cached_input_tokens", "output_tokens") + ) + if has_tokens: + token_line = ( + f"-- I:{_format_token_count(data.get('input_tokens'))} " + f"O:{_format_token_count(data.get('output_tokens'))} " + f"C:{_format_token_count(data.get('cached_input_tokens'))}" + ) + lines.append(token_line) + + return "\n".join(lines) except (json.JSONDecodeError, KeyError, TypeError) as e: LOG.warning(f"Failed to parse output: {e}") return stdout.strip() diff --git a/agent_cost_estimator/hooks/mistral-vibe-code/post_turn.py b/agent_cost_estimator/hooks/mistral-vibe-code/post_turn.py index 013b78a..50e4bee 100644 --- a/agent_cost_estimator/hooks/mistral-vibe-code/post_turn.py +++ b/agent_cost_estimator/hooks/mistral-vibe-code/post_turn.py @@ -24,7 +24,13 @@ usage data from: ├── journal/*.jsonl └── generations/*/projection-state.json -Output format: provider:model | session:$XXX | turn:$YYY +Output format (system_message, two lines): + -- provider:model | session:$XXX | turn:$YYY + -- I: O: C: +Token counts are session totals, formatted with automatic suffixes +(k, M, B, T) — the same shape as the CLI's default output. The token +line is omitted when the JSON report carries no token fields (older +harness reports), so the hook falls back to the cost line alone. Model Detection (VSCodium-specific): - Primary: session meta.json config.model_name / config.active_model @@ -104,10 +110,37 @@ def get_session_dir() -> Path: return default_dir +def _format_token_count(count) -> str: + """Format a token count with magnitude suffixes (k, M, B, T). + + Stdlib-only copy of agent_cost_estimator.utils.formatting + .format_token_count: the hook must not import the package + (it may run where only the script is installed), and the two + implementations must stay in sync. + """ + try: + count = int(count or 0) + except (TypeError, ValueError): + return "0" + if count < 1000: + return str(count) + elif count < 1_000_000: + return f"{count / 1000:.2f}k" + elif count < 1_000_000_000: + return f"{count / 1_000_000:.2f}M" + elif count < 1_000_000_000_000: + return f"{count / 1_000_000_000:.2f}B" + else: + return f"{count / 1_000_000_000_000:.2f}T" + + def format_cost_output(stdout: str) -> str: """Format the agent-cost-estimator JSON output into hook message format. - - Expected format: provider:model | session:$XXX | turn:$YYY + + Expected format (system_message gets a '-- ' prefix on the first + line from the caller): + provider:model | session:$XXX | turn:$YYY + -- I: O: C: """ try: data = json.loads(stdout) @@ -118,14 +151,33 @@ def format_cost_output(stdout: str) -> str: model_str = f"{provider}:{model}" else: model_str = f"unknown:{model_key}" - + # Extract costs session_cost = data.get("cost_usd", {}).get("total_usd", 0) last_turn = data.get("last_turn") turn_cost = last_turn.get("cost_usd", {}).get("total_usd", 0) if last_turn else 0 - - # Format: provider:model | session:$XXX | turn:$YYY - return f"{model_str} | session:${session_cost:.2f} | turn:${turn_cost:.2f}" + + # Cost line: provider:model | session:$XXX | turn:$YYY + lines = [ + f"{model_str} | session:${session_cost:.2f} | turn:${turn_cost:.2f}" + ] + + # Token line from session totals (per-turn token counts are not + # part of the harness report). Only present when the report has + # the token fields; the cost line never depends on them. + has_tokens = any( + data.get(field) is not None + for field in ("input_tokens", "cached_input_tokens", "output_tokens") + ) + if has_tokens: + token_line = ( + f"-- I:{_format_token_count(data.get('input_tokens'))} " + f"O:{_format_token_count(data.get('output_tokens'))} " + f"C:{_format_token_count(data.get('cached_input_tokens'))}" + ) + lines.append(token_line) + + return "\n".join(lines) except (json.JSONDecodeError, KeyError, TypeError) as e: LOG.warning(f"Failed to parse output: {e}") return stdout.strip() diff --git a/tests/test_hooks.py b/tests/test_hooks.py new file mode 100644 index 0000000..e00e612 --- /dev/null +++ b/tests/test_hooks.py @@ -0,0 +1,96 @@ +"""Tests for the Vibe hook scripts (post_turn.py) display formatting. + +The hooks are standalone stdlib-only scripts (they must not import the +package), so they are loaded directly from their files and their +formatting must be tested in isolation. +""" + +import importlib.util +import json +import pathlib +import pytest + +from agent_cost_estimator.utils.formatting import format_token_count + +HOOKS_DIR = pathlib.Path(__file__).parent.parent / "agent_cost_estimator" / "hooks" +HOOK_FILES = { + "mistral-vibe-cli": HOOKS_DIR / "mistral-vibe-cli" / "post_turn.py", + "mistral-vibe-code": HOOKS_DIR / "mistral-vibe-code" / "post_turn.py", +} + + +def _load_hook(name): + """Import a hook script by file path (module-name-safe).""" + path = HOOK_FILES[name] + spec = importlib.util.spec_from_file_location(f"hook_{name.replace('-', '_')}", path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def _report_json(tokens=True): + """A realistic `agent-cost-estimator --json` report.""" + data = { + "pricing": {"model_key": "zai:zai-glm-5-3"}, + "cost_usd": {"total_usd": 0.0136}, + "last_turn": {"cost_usd": {"total_usd": 0.0031}}, + } + if tokens: + data.update({ + "input_tokens": 42116859, + "cached_input_tokens": 41355264, + "output_tokens": 95073, + }) + return json.dumps(data) + + +@pytest.mark.parametrize("hook_name", sorted(HOOK_FILES)) +class TestFormatCostOutput: + def test_two_lines_with_tokens(self, hook_name): + hook = _load_hook(hook_name) + result = hook.format_cost_output(_report_json(tokens=True)) + assert result == ( + "zai:zai-glm-5-3 | session:$0.01 | turn:$0.00\n" + "-- I:42.12M O:95.07k C:41.36M" + ) + + def test_cost_line_only_without_token_fields(self, hook_name): + hook = _load_hook(hook_name) + result = hook.format_cost_output(_report_json(tokens=False)) + assert result == "zai:zai-glm-5-3 | session:$0.01 | turn:$0.00" + assert "\n" not in result + + def test_unknown_provider_falls_back(self, hook_name): + hook = _load_hook(hook_name) + report = json.dumps({ + "pricing": {"model_key": "noprovider"}, + "cost_usd": {"total_usd": 1.0}, + "last_turn": None, + "input_tokens": 0, + "cached_input_tokens": 0, + "output_tokens": 0, + }) + result = hook.format_cost_output(report) + assert result.startswith("unknown:noprovider | session:$1.00 | turn:$0.00") + assert result.endswith("-- I:0 O:0 C:0") + + def test_non_json_stdout_returned_stripped(self, hook_name): + hook = _load_hook(hook_name) + assert hook.format_cost_output(" plain text \n") == "plain text" + + +@pytest.mark.parametrize("hook_name", sorted(HOOK_FILES)) +class TestTokenFormatterParity: + """The hook's inlined formatter must match the package formatter.""" + + @pytest.mark.parametrize("count", [ + 0, 500, 999, 1000, 1500, 9999, 100000, 999999, 1000000, + 1234567, 1000000000, 1500000000000, + ]) + def test_matches_package_formatting(self, hook_name, count): + hook = _load_hook(hook_name) + assert hook._format_token_count(count) == format_token_count(count) + + def test_none_input_is_zero(self, hook_name): + hook = _load_hook(hook_name) + assert hook._format_token_count(None) == "0"