From 992d6105aee978b97d9e3e6f3726f0f42131b6df Mon Sep 17 00:00:00 2001 From: AmirNCI <“x25188984@student.ncirl.ie”> Date: Sat, 17 Oct 2026 11:19:00 +0100 Subject: [PATCH] fix: dashboard overview crashes when an agent has no recorded cost Since cost_summary() stopped collapsing unknown cost to $0 (PR #30), an agent whose runs are all unpriced (devin, droid, cursor, agy) comes back with cost_usd=None. The overview page added every agent's cost with a plain sum(), so one priced run plus one unpriced agent raised TypeError and "/" returned 500. Use sum_optional(), as the `secfoo cost` Total row already does. The "AI spend by agent" table had the same problem with tokens: a run can have a cost but no token counts, and the template added None + None. Show "-" when no token count is known. Co-Authored-By: Claude Opus 5.5 --- src/secfoo/web/routes/overview.py | 11 +++++---- src/secfoo/web/templates/overview.html | 4 +++- tests/test_web/test_routes.py | 31 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/secfoo/web/routes/overview.py b/src/secfoo/web/routes/overview.py index db943fa..92119cc 100644 --- a/src/secfoo/web/routes/overview.py +++ b/src/secfoo/web/routes/overview.py @@ -2,6 +2,7 @@ from fastapi import APIRouter, Request +from secfoo.cost import sum_optional from secfoo.storage.repository import RunRepository from secfoo.web.templating import ordered_skills, severity_rows, templates from secfoo.web.top_findings import build_top_findings @@ -51,11 +52,9 @@ def overview(request: Request): "top_findings": top_findings, "cost_by_agent": cost_by_agent, # None (rendered "-") until at least one run has a known cost, - # so runs that predate tracking don't read as free. - "total_cost": ( - sum(row.cost_usd for row in cost_by_agent) - if any(row.unpriced_runs < row.runs for row in cost_by_agent) - else None - ), + # so runs that predate tracking don't read as free. An agent + # whose runs are all unpriced has cost_usd=None; skip it rather + # than adding it, same as the `secfoo cost` Total row. + "total_cost": sum_optional(row.cost_usd for row in cost_by_agent), }, ) diff --git a/src/secfoo/web/templates/overview.html b/src/secfoo/web/templates/overview.html index 4134c57..df4ded7 100644 --- a/src/secfoo/web/templates/overview.html +++ b/src/secfoo/web/templates/overview.html @@ -108,7 +108,9 @@

AI spend by agent

{{ row.label }} {{ row.runs }} {% set priced = row.unpriced_runs < row.runs %} - {{ ((row.input_tokens + row.output_tokens) | fmt_tokens) if (priced or row.input_tokens or row.output_tokens) else '-' }} + {# A priced run can still have no token counts (e.g. cost-only agents), so don't add None. #} + {% set tokens_known = row.input_tokens is not none or row.output_tokens is not none %} + {{ (((row.input_tokens or 0) + (row.output_tokens or 0)) | fmt_tokens) if tokens_known else '-' }} {{ (row.cost_usd | fmt_cost) if priced else '-' }} {% endfor %} diff --git a/tests/test_web/test_routes.py b/tests/test_web/test_routes.py index 27c7b0e..4aff7cc 100644 --- a/tests/test_web/test_routes.py +++ b/tests/test_web/test_routes.py @@ -75,6 +75,37 @@ def test_overview_lists_recent_assessment(client, tmp_path): assert "org/repo" in resp.text +def test_overview_totals_cost_when_one_agent_is_unpriced(client, tmp_path): + # A priced claude run alongside an agent that never reports cost + # (devin/droid/cursor/agy) makes cost_summary() return cost_usd=None + # for that agent's group; the total must skip it, not crash on it. + repo = RunRepository() + project_id = repo.upsert_project("https://github.com/org/repo", "org/repo", "github") + for agent_id, cost in (("claude", 1.25), ("devin", None)): + run_uuid = repo.create_run( + project_id=project_id, + skill_id="sast", + skill_name="SAST", + agent_id=agent_id, + confluence_urls=[], + ) + repo.complete_run( + run_uuid, + status="success", + exit_code=0, + duration_seconds=1.0, + report_path=None, + prompt_path=None, + stderr_excerpt=None, + cost_usd=cost, + ) + repo.close() + + resp = client.get("/") + assert resp.status_code == 200 + assert "$1.25" in resp.text + + def test_project_detail(client, tmp_path): project_id, _ = _seed_run(tmp_path) resp = client.get(f"/projects/{project_id}")