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
5 changes: 4 additions & 1 deletion src/secfoo/agents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/secfoo/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions src/secfoo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
}

Expand Down Expand Up @@ -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)

Expand Down
13 changes: 13 additions & 0 deletions src/secfoo/cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 5 additions & 3 deletions src/secfoo/storage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions src/secfoo/storage/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
Expand Down
1 change: 1 addition & 0 deletions src/secfoo/web/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
4 changes: 3 additions & 1 deletion tests/test_agents/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
16 changes: 15 additions & 1 deletion tests/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
Loading