diff --git a/src/secfoo/agents/api.py b/src/secfoo/agents/api.py index 595c376..8c60e32 100644 --- a/src/secfoo/agents/api.py +++ b/src/secfoo/agents/api.py @@ -153,7 +153,10 @@ def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> Agen started = time.monotonic() _strip_key_whitespace() if not self.is_available(): - return self._result("binary_not_found", started, stderr=f"No API key set. Set one of: {', '.join(KEY_VARS)}") + # Not "binary_not_found": this adapter has no CLI to find on PATH + # in the first place -- the thing actually missing is a key. + # Reusing the CLI-adapters' status here was SECFOO-35. + return self._result("not_configured", started, stderr=f"No API key set. Set one of: {', '.join(KEY_VARS)}") try: import litellm from langgraph.graph import END, START, StateGraph diff --git a/src/secfoo/agents/base.py b/src/secfoo/agents/base.py index 4dff466..00bd3ba 100644 --- a/src/secfoo/agents/base.py +++ b/src/secfoo/agents/base.py @@ -18,7 +18,7 @@ from pathlib import Path from typing import ClassVar, Literal -Status = Literal["success", "failed", "timeout", "binary_not_found"] +Status = Literal["success", "failed", "timeout", "binary_not_found", "not_configured"] def _kill_process_tree(pid: int, *, posix: bool = os.name == "posix") -> None: diff --git a/src/secfoo/cli.py b/src/secfoo/cli.py index 8bc49ef..1a483f7 100644 --- a/src/secfoo/cli.py +++ b/src/secfoo/cli.py @@ -22,7 +22,7 @@ from secfoo.attachments import infer_attachment_kind from secfoo.cloud import CloudConfig, CloudError from secfoo.config import CLOUD_CONFIG_PATH, attachment_dir -from secfoo.cost import format_cost, format_tokens +from secfoo.cost import format_cost, format_tokens, sum_optional from secfoo.runner import _project_display_name, execute_runs from secfoo.settings import CONFIG_PATH, ConfigError, load_config from secfoo.skills.loader import load_all_skills @@ -178,6 +178,7 @@ def _resolve_project_id(repo: RunRepository, target: str | None) -> int: "failed": "red", "timeout": "yellow", "binary_not_found": "red", + "not_configured": "red", "running": "cyan", } @@ -627,15 +628,15 @@ def cost( str(row.runs), format_tokens(row.input_tokens), format_tokens(row.output_tokens), - format_cost(row.cost_usd) if row.unpriced_runs < row.runs else "-", + format_cost(row.cost_usd), ) table.add_section() table.add_row( "[bold]Total[/]", str(sum(r.runs for r in rows)), - format_tokens(sum(r.input_tokens for r in rows)), - format_tokens(sum(r.output_tokens for r in rows)), - f"[bold]{format_cost(sum(r.cost_usd for r in rows))}[/]", + format_tokens(sum_optional(r.input_tokens for r in rows)), + format_tokens(sum_optional(r.output_tokens for r in rows)), + f"[bold]{format_cost(sum_optional(r.cost_usd for r in rows))}[/]", ) console.print(table) diff --git a/src/secfoo/cost.py b/src/secfoo/cost.py index 12a6807..6838ba3 100644 --- a/src/secfoo/cost.py +++ b/src/secfoo/cost.py @@ -19,3 +19,16 @@ def format_tokens(value: int | None) -> str: if value is None: return "-" return f"{value:,}" + + +def sum_optional(values) -> float | None: + """Sum a column of per-group totals that can themselves be None (a + group where every run is unpriced/unknown). None only when every value + is None, so a genuine 0 among them still sums -- same "unknown" vs + "zero" distinction `format_cost`/`format_tokens` render, just rolled up + across groups (e.g. the `secfoo cost` command's Total row -- SECFOO-34). + """ + known = [v for v in values if v is not None] + if not known: + return None + return sum(known) diff --git a/src/secfoo/storage/models.py b/src/secfoo/storage/models.py index 497a209..77273d9 100644 --- a/src/secfoo/storage/models.py +++ b/src/secfoo/storage/models.py @@ -227,7 +227,9 @@ class SecretFindingRecord: class CostRow: label: str runs: int - input_tokens: int - output_tokens: int - cost_usd: float + # None means every run in this group is unpriced (no agent in it reports + # the figure), not that the group used zero -- SECFOO-34. + input_tokens: int | None + output_tokens: int | None + cost_usd: float | None unpriced_runs: int diff --git a/src/secfoo/storage/repository.py b/src/secfoo/storage/repository.py index f43118d..e2fa673 100644 --- a/src/secfoo/storage/repository.py +++ b/src/secfoo/storage/repository.py @@ -446,9 +446,12 @@ def cost_summary(self, *, group_by: str, project_id: int | None = None, since: s }[group_by] query = ( f"SELECT {group_sql} AS label, COUNT(*) AS runs, " - "COALESCE(SUM(runs.input_tokens),0) AS input_tokens, " - "COALESCE(SUM(runs.output_tokens),0) AS output_tokens, " - "COALESCE(SUM(runs.cost_usd),0) AS cost_usd, " + # No COALESCE here: SQLite's SUM() already returns NULL (not 0) + # when every value in the group is NULL, and that distinction is + # exactly "unknown" vs "zero" -- collapsing it to 0 is SECFOO-34. + "SUM(runs.input_tokens) AS input_tokens, " + "SUM(runs.output_tokens) AS output_tokens, " + "SUM(runs.cost_usd) AS cost_usd, " "SUM(CASE WHEN runs.cost_usd IS NULL THEN 1 ELSE 0 END) AS unpriced_runs " "FROM runs JOIN projects ON projects.id = runs.project_id " "WHERE runs.status NOT IN ('pending','running')" diff --git a/src/secfoo/web/templating.py b/src/secfoo/web/templating.py index 9cf9e29..298b0b3 100644 --- a/src/secfoo/web/templating.py +++ b/src/secfoo/web/templating.py @@ -69,6 +69,7 @@ def ordered_skills() -> list: "failed": "badge--error", "timeout": "badge--warning", "binary_not_found": "badge--error", + "not_configured": "badge--error", "running": "badge--primary", "pending": "badge--neutral", } diff --git a/tests/test_agents/test_api.py b/tests/test_agents/test_api.py index c587ed8..ba33fd6 100644 --- a/tests/test_agents/test_api.py +++ b/tests/test_agents/test_api.py @@ -77,7 +77,9 @@ def test_is_available_depends_on_an_api_key(no_keys, monkeypatch): def test_run_without_a_key_explains_what_to_set(no_keys, tmp_path): result = ApiAdapter().run("prompt", workdir=tmp_path) - assert result.status == "binary_not_found" + # Not "binary_not_found" -- this adapter has no CLI binary to miss in + # the first place; what's missing is a key (SECFOO-35). + assert result.status == "not_configured" assert "OPENAI_API_KEY" in result.stderr diff --git a/tests/test_cost.py b/tests/test_cost.py index 696041e..8f3322c 100644 --- a/tests/test_cost.py +++ b/tests/test_cost.py @@ -79,7 +79,9 @@ def test_cost_summary_groups_and_counts_unpriced_runs(db): assert rows["api"].cost_usd == pytest.approx(1.0) assert rows["api"].input_tokens == 4000 assert rows["api"].unpriced_runs == 0 - assert rows["agent"].cost_usd == 0 + # Fully-unpriced group: None (unknown), not 0 -- SECFOO-34. + assert rows["agent"].cost_usd is None + assert rows["agent"].input_tokens is None assert rows["agent"].unpriced_runs == 1 @@ -121,6 +123,18 @@ def test_cost_command_shows_totals_and_unpriced_note(db): assert "1 run(s) have no cost recorded" in result.stdout +def test_cost_command_total_is_dash_when_every_run_is_unpriced(db): + """SECFOO-34: an all-unknown total should read "-", not "$0.00".""" + _seed(agent_id="agent") + _seed(agent_id="other-agent") + + result = runner.invoke(app, ["cost"]) + + assert result.exit_code == 0 + assert "$0.00" not in result.stdout + assert "-" in result.stdout + + def test_cost_command_rejects_bad_since_date(db): result = runner.invoke(app, ["cost", "--since", "last week"]) assert result.exit_code != 0