Skip to content

The agent that grows with you

Merge remote-tracking branch 'origin/main' into ethie/pm-clean

ethernetd70feca03d2c
diff --cc scripts/run_tests_parallel.py
index b78e7466f91,81424bb7640..6a3e3f351f6
mode 100644,100755..100644
--- a/scripts/run_tests_parallel.py
+++ b/scripts/run_tests_parallel.py
diff --cc tests/plugins/image_gen/test_openrouter_compat_provider.py
index e5c51af09eb,09ad4ffb359..414980b1c82
--- a/tests/plugins/image_gen/test_openrouter_compat_provider.py
+++ b/tests/plugins/image_gen/test_openrouter_compat_provider.py
@@@ -701,8 -701,56 +701,56 @@@ class TestImageApiSurface
assert result["cost_usd"] == 0.0336
assert result["total_tokens"] == 1128
assert result["exact_aspect_ratio"] == "9:16"
- assert result["image"] == "/tmp/i.png"
+ assert result["image"] == str(Path("/tmp/i.png"))
+ _USAGE = {"prompt_tokens": 1000, "completion_tokens": 128, "total_tokens": 1128}
+
+ @pytest.mark.parametrize("surface, model, usage, images", [
+ ("chat", "openai/gpt-5.4-image-2", _USAGE, True), # default chain: token-billed via /chat/completions
+ ("images", "krea/krea-2-medium", _USAGE, True), # curated Image API model
+ ("images", "krea/krea-2-medium", None, True), # flat-fee body without usage: no write
+ ("chat", "openai/gpt-5.4-image-2", _USAGE, False), # billed HTTP 200 with text but no image
+ ("images", "krea/krea-2-medium", _USAGE, False), # billed HTTP 200 with empty ``data``
+ ])
+ def test_token_usage_reaches_session_accounting(self, surface, model, usage, images):
+ """A response carrying token usage records one ``image_generation`` row on the ambient
+ session — also when it carries no image (the provider billed the tokens anyway); a body
+ without usage records nothing."""
+ from agent import aux_accounting
+
+ recorded = []
+
+ class _DB:
+ def record_auxiliary_usage(self, *args, **kwargs):
+ recorded.append((args, kwargs))
+
+ if surface == "chat":
+ response = _mock_chat_response([_PNG_DATA_URI] if images else [])
+ response.json.return_value["usage"] = dict(usage)
+ else:
+ response = _mock_image_api_response([] if not images else None, usage=usage)
+ token = aux_accounting.set_accounting_context(_DB(), "sess-1")
+ try:
+ with patch(_RUNTIME, return_value=_runtime_ok()), \
+ patch("requests.post", return_value=response), \
+ patch("plugins.image_gen.openrouter.save_b64_image", return_value=Path("/tmp/i.png")):
+ result = _openrouter_image_api().generate(prompt="p", aspect_ratio="portrait", model=model)
+ finally:
+ aux_accounting.reset_accounting_context(token)
+
+ assert result["success"] is images
+ if not images:
+ assert result["error_type"] == "empty_response"
+ if usage is None:
+ assert recorded == []
+ return
+ ((session_id, task), kwargs), = recorded
+ assert (session_id, task) == ("sess-1", "image_generation")
+ assert kwargs["model"] == model
+ assert kwargs["billing_provider"] == "openrouter"
+ assert (kwargs["input_tokens"], kwargs["output_tokens"]) == (1000, 128)
+
+
def test_multiple_images_land_in_additional_images(self):
entries = [
{"b64_json": "AA==", "media_type": "image/png"},