Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/inferrsi/apps/delivery/benchmark_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
15 changes: 15 additions & 0 deletions tests/test_result_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading