From 0d731b8efba7fb99859340e7b7484954256cbeb4 Mon Sep 17 00:00:00 2001 From: Inference_ <68734681+Inference1@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:22:57 +0800 Subject: [PATCH] fix(delivery): preserve observed benchmark token usage Only select native usage when its receipt supplies observed counters. Preserve the existing campaign subtotal and source when the attached receipt has no counters. Agent-assisted-by: Codex (author) Signed-off-by: Inference1 <3617237115@qq.com> --- src/inferrsi/apps/delivery/benchmark_usage.py | 6 ++---- tests/test_result_delivery.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/inferrsi/apps/delivery/benchmark_usage.py b/src/inferrsi/apps/delivery/benchmark_usage.py index cd6964f8..38d8d5a4 100644 --- a/src/inferrsi/apps/delivery/benchmark_usage.py +++ b/src/inferrsi/apps/delivery/benchmark_usage.py @@ -80,16 +80,14 @@ def attach_native_usage(snapshot, result, job): coverage = payload.get("benchmark_token_coverage") or {} workspace_bound = snapshot.root.resolve().is_relative_to(Path(request["workspace"]).resolve()) policy = plan.get("report_metadata", {}).get("benchmark_metrics") - usage = result["benchmark_metrics"]["token_usage"] - selected_native_usage = False + usage = None # A standalone job can contain several campaigns: bind the exact frozen plan too. if (supported_policy(policy) and workspace_bound and isinstance(coverage, dict) and coverage.get("campaign_plan_digest") == control["plan_digest"]): usage = terminal_token_usage(host, payload, trial_id=plan.get("condition"), control=control, source=ref) - selected_native_usage = True refs = snapshot.include(native, "agent-job:") - if selected_native_usage: + if usage is not None and usage["observed_tokens"] is not None: usage["source"] = refs[ref] result["usage"]["session_tokens"] = { "value": {key: usage[key] for key in ( diff --git a/tests/test_result_delivery.py b/tests/test_result_delivery.py index 0c3aeed6..1b92fbbe 100644 --- a/tests/test_result_delivery.py +++ b/tests/test_result_delivery.py @@ -922,6 +922,21 @@ def test_single_benchmark_missing_terminal_receipt_preserves_observed_subtotal( assert tokens["total_tokens"] is None and tokens["observed_tokens"] == 1200 +def test_single_benchmark_terminal_receipt_without_counters_preserves_campaign_usage( + standalone_native_usage, +): + root, job, observation = standalone_native_usage + del observation["usage"] + record_native_usage(job, observation) + _, original, _ = delivery.project(root, export_id="campaign-usage") + _, result, _ = delivery.project(root, export_id="native-without-counters", agent_job=job) + tokens = result["benchmark_metrics"]["token_usage"] + assert tokens == original["benchmark_metrics"]["token_usage"] + assert tokens["observed_tokens"] == 1200 and tokens["total_tokens"] is None + assert result["usage"]["session_tokens"] == original["usage"]["session_tokens"] + assert any(source["type"] == "native_agent_usage" for source in result["sources"]) + + @pytest.mark.parametrize( "standalone_native_usage", [{}, {"metrics_schema_version": 2}], indirect=True) def test_native_job_preserves_unsupported_policy_reason(standalone_native_usage):