From 7600e3f84ce62a7b5bc416e7539243c077abf92c Mon Sep 17 00:00:00 2001 From: karthickmk2001 Date: Mon, 21 Sep 2026 13:34:27 +0100 Subject: [PATCH 1/2] docs: document how to add or change an agent adapter Contributors had no documented path to extend secfoo with a new agent CLI, even though the AgentAdapter base class + registry.py already supported it cleanly. Adds a CONTRIBUTING.md section (mirroring the existing skill-authoring one) and a README pointer to it. Co-Authored-By: Claude Sonnet 5 --- CONTRIBUTING.md | 25 +++++++++++++++++++++++++ README.md | 4 +++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ddc2c8b..5851220 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -34,6 +34,31 @@ dashboard (like SAST's Open/Closed Findings register), look at templates as the reference shape — most skills don't need this and are fine with the generic activity page (`activities/detail.html`). +## Adding or changing an agent adapter + +Agent adapters live under `src/secfoo/agents/*.py`. Each one subclasses +`AgentAdapter` (`src/secfoo/agents/base.py`), which owns the shared +subprocess/timeout/process-tree-kill contract, so a new adapter only +needs to supply: + +- `name` / `binary` class vars, and `default_timeout_seconds` if the + 1800s default doesn't fit +- `build_command(prompt, *, workdir)` — the argv to invoke the CLI + non-interactively +- `extract_report(stdout)` — optional, only for a CLI that wraps its + answer in a JSON envelope rather than printing the report directly + +Then register the class in `ADAPTERS` in +`src/secfoo/agents/registry.py`. `secfoo agents`, `--agent`, and the web +run form are all driven from that dict, so there's nothing else to wire +up. + +The built-in `secfoo` agent (`src/secfoo/agents/secfoo.py`) is the one +exception: it overrides `run()` directly instead of `build_command()`, +since it runs in-process via LangGraph/LiteLLM rather than shelling out +to an external CLI — use it as the reference if your adapter also needs +to skip the subprocess path. + ## Pull requests - Keep PRs focused — one behavior change per PR is easier to review diff --git a/README.md b/README.md index fa5a858..779fbba 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,9 @@ the [CLI reference](https://secfoo.com/docs/cli.html) for more. - **Concurrent by default**: multiple `--skill` flags run at the same time, not one after another. - **Bring your own agent**: Claude Code, Cursor, Antigravity, or Gemini - CLI — pick whichever you already use and trust. + CLI — pick whichever you already use and trust. Want to wire up a + different CLI, or your own LangGraph/LiteLLM pipeline? See + [Adding or changing an agent adapter](CONTRIBUTING.md#adding-or-changing-an-agent-adapter). - **Open source CLI**: MIT licensed. ## Activity catalog From d9de0843f835e81d2b4349c8bc8d0298b332b6b3 Mon Sep 17 00:00:00 2001 From: karthickmk2001 Date: Mon, 21 Sep 2026 13:45:35 +0100 Subject: [PATCH 2/2] feat: track AI spend for Claude Code runs, not just the built-in agent secfoo cost previously only reported spend for the built-in secfoo/ LangGraph agent, silently leaving cost_usd NULL for every run through claude/cursor/antigravity/gemini even though the README implied cost was tracked per run in general. Adds an extract_cost() hook to AgentAdapter, wired into run()'s success path alongside the existing extract_report() hook. Claude Code reports its own total_cost_usd in --output-format json, so ClaudeAdapter now parses and returns it -- a real CLI-reported figure, not an estimate. Cursor, Antigravity, and Gemini CLI don't report a cost figure in their output, so those adapters intentionally leave cost_usd unset (documented inline) rather than guess from token counts against a pricing table we'd have to keep current. README and CONTRIBUTING.md updated to describe the actual coverage. Co-Authored-By: Claude Sonnet 5 --- CONTRIBUTING.md | 6 ++++++ README.md | 7 +++++-- src/secfoo/agents/antigravity.py | 4 ++++ src/secfoo/agents/base.py | 8 ++++++++ src/secfoo/agents/claude.py | 11 +++++++++++ src/secfoo/agents/cursor.py | 4 ++++ src/secfoo/agents/gemini.py | 7 +++++++ tests/test_agents/test_antigravity.py | 9 +++++++++ tests/test_agents/test_claude.py | 28 +++++++++++++++++++++++++++ tests/test_agents/test_cursor.py | 9 +++++++++ tests/test_agents/test_gemini.py | 9 +++++++++ 11 files changed, 100 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5851220..2d21a5c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -47,6 +47,12 @@ needs to supply: non-interactively - `extract_report(stdout)` — optional, only for a CLI that wraps its answer in a JSON envelope rather than printing the report directly +- `extract_cost(stdout)` — optional, only if the CLI itself reports a USD + spend figure (e.g. Claude Code's `total_cost_usd` in `--output-format + json`; see `ClaudeAdapter`). Leave it unset if the CLI doesn't report + cost — don't estimate from token counts, since there's no pricing table + in this repo to keep current. `secfoo cost` shows `-` for runs whose + adapter doesn't provide this. Then register the class in `ADAPTERS` in `src/secfoo/agents/registry.py`. `secfoo agents`, `--agent`, and the web diff --git a/README.md b/README.md index 779fbba..d7204e9 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,11 @@ $env:SECFOO_MODEL = "openai/gpt-4o-mini" secfoo run --skill security-architecture-review --agent secfoo ``` -LiteLLM-reported spend is stored per run and can be reviewed from the -dashboard or queried from the CLI: +Spend is stored per run and can be reviewed from the dashboard or queried +from the CLI, for any agent whose CLI reports its own cost -- currently +the built-in `secfoo` agent (via LiteLLM) and `claude` (via Claude Code's +own `total_cost_usd`); Cursor, Antigravity, and Gemini CLI don't report a +spend figure yet, so those runs show `-`: ```bash secfoo cost diff --git a/src/secfoo/agents/antigravity.py b/src/secfoo/agents/antigravity.py index 51b8e84..9eb6b11 100644 --- a/src/secfoo/agents/antigravity.py +++ b/src/secfoo/agents/antigravity.py @@ -38,6 +38,10 @@ class AntigravityAdapter(AgentAdapter): binary = "agy" default_timeout_seconds = 1800 + # No extract_cost() override: `agy --print` has no JSON output mode (see + # module docstring), so there's no reported spend figure to parse. + # secfoo cost will show `-` for runs through this adapter. + def build_command(self, prompt: str, *, workdir: Path) -> list[str]: effective_timeout = getattr(self, "_effective_timeout", self.default_timeout_seconds) return [self.binary, "--print", prompt, "--print-timeout", f"{effective_timeout}s"] diff --git a/src/secfoo/agents/base.py b/src/secfoo/agents/base.py index 4a6b165..af77aad 100644 --- a/src/secfoo/agents/base.py +++ b/src/secfoo/agents/base.py @@ -84,6 +84,13 @@ def extract_report(self, stdout: str) -> str: """Pull the report text out of raw stdout. Default: stdout is the report.""" return stdout + def extract_cost(self, stdout: str) -> float | None: + """Pull a CLI-reported USD spend figure out of raw stdout, if the CLI + reports one. Default: no cost data available. Only override this + with a value the CLI itself reports -- never a token-count estimate, + since we have no reliable, current pricing table to convert with.""" + return None + def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> AgentResult: if not self.is_available(): return AgentResult( @@ -127,6 +134,7 @@ def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> Agen timed_out=False, status=status, raw_report=self.extract_report(stdout) if status == "success" else "", + cost_usd=self.extract_cost(stdout) if status == "success" else None, ) except subprocess.TimeoutExpired: _kill_process_tree(proc.pid) diff --git a/src/secfoo/agents/claude.py b/src/secfoo/agents/claude.py index 5da60f8..1d9a95f 100644 --- a/src/secfoo/agents/claude.py +++ b/src/secfoo/agents/claude.py @@ -55,3 +55,14 @@ def extract_report(self, stdout: str) -> str: except json.JSONDecodeError: return stdout return payload.get("result", stdout) + + def extract_cost(self, stdout: str) -> float | None: + # `--output-format json` includes `total_cost_usd`, Claude Code's own + # accounting of the spend for this invocation -- a real reported + # figure, not something we derive. + try: + payload = json.loads(stdout) + except json.JSONDecodeError: + return None + value = payload.get("total_cost_usd") + return float(value) if isinstance(value, (int, float)) else None diff --git a/src/secfoo/agents/cursor.py b/src/secfoo/agents/cursor.py index 66ab515..1fa3d72 100644 --- a/src/secfoo/agents/cursor.py +++ b/src/secfoo/agents/cursor.py @@ -24,3 +24,7 @@ class CursorAdapter(AgentAdapter): def build_command(self, prompt: str, *, workdir: Path) -> list[str]: return [self.binary, "-p", prompt] + + # No extract_cost() override: `agent -p` prints plain text, not a JSON + # envelope with a reported spend figure, so there's nothing to parse + # cost out of. secfoo cost will show `-` for runs through this adapter. diff --git a/src/secfoo/agents/gemini.py b/src/secfoo/agents/gemini.py index d4a0604..544b647 100644 --- a/src/secfoo/agents/gemini.py +++ b/src/secfoo/agents/gemini.py @@ -39,3 +39,10 @@ def extract_report(self, stdout: str) -> str: except json.JSONDecodeError: return stdout return payload.get("response", payload.get("result", stdout)) + + # No extract_cost() override: Gemini CLI's JSON output doesn't report a + # USD spend figure (unlike Claude Code's total_cost_usd), and deriving + # one from token counts would need a pricing table we have no reliable + # way to keep current -- see AgentAdapter.extract_cost()'s docstring for + # why that tradeoff isn't worth it. secfoo cost will show `-` for runs + # through this adapter until Gemini CLI reports cost itself. diff --git a/tests/test_agents/test_antigravity.py b/tests/test_agents/test_antigravity.py index 756533a..f93e849 100644 --- a/tests/test_agents/test_antigravity.py +++ b/tests/test_agents/test_antigravity.py @@ -26,3 +26,12 @@ def test_no_dangerously_skip_permissions_flag(tmp_path): adapter = AntigravityAdapter() cmd = adapter.build_command("hi", workdir=tmp_path) assert "--dangerously-skip-permissions" not in cmd + + +def test_run_success_leaves_cost_usd_none(fake_popen, tmp_path): + """`agy --print` has no JSON output mode, so no reported spend figure to + parse -- cost_usd must stay None rather than a guessed/estimated value.""" + fake_popen(returncode=0, stdout="# Report", stderr="") + adapter = AntigravityAdapter() + result = adapter.run("hi", workdir=tmp_path) + assert result.cost_usd is None diff --git a/tests/test_agents/test_claude.py b/tests/test_agents/test_claude.py index 635dbbe..407158f 100644 --- a/tests/test_agents/test_claude.py +++ b/tests/test_agents/test_claude.py @@ -70,3 +70,31 @@ def test_run_success_extracts_report_from_json(fake_popen, tmp_path, monkeypatch result = adapter.run("review this", workdir=tmp_path) assert result.status == "success" assert result.raw_report == "# Threat Report" + + +def test_extract_cost_parses_total_cost_usd(): + adapter = ClaudeAdapter() + stdout = json.dumps({"result": "ok", "total_cost_usd": 0.0412}) + assert adapter.extract_cost(stdout) == 0.0412 + + +def test_extract_cost_returns_none_when_field_missing(): + adapter = ClaudeAdapter() + assert adapter.extract_cost(json.dumps({"result": "ok"})) is None + + +def test_extract_cost_returns_none_on_non_json(): + adapter = ClaudeAdapter() + assert adapter.extract_cost("not json") is None + + +def test_run_success_sets_cost_usd_from_json(fake_popen, tmp_path, monkeypatch): + monkeypatch.setattr("secfoo.agents.claude.load_config", lambda: _empty_config()) + fake_popen( + returncode=0, + stdout=json.dumps({"result": "# Threat Report", "total_cost_usd": 0.0412}), + stderr="", + ) + adapter = ClaudeAdapter() + result = adapter.run("review this", workdir=tmp_path) + assert result.cost_usd == 0.0412 diff --git a/tests/test_agents/test_cursor.py b/tests/test_agents/test_cursor.py index aa545c4..222ad7c 100644 --- a/tests/test_agents/test_cursor.py +++ b/tests/test_agents/test_cursor.py @@ -23,3 +23,12 @@ def test_timeout_status_does_not_raise(fake_popen, tmp_path): assert result.timed_out is True assert result.exit_code is None assert result.raw_report == "" + + +def test_run_success_leaves_cost_usd_none(fake_popen, tmp_path): + """`agent -p` prints plain text, no reported spend figure to parse -- + cost_usd must stay None rather than a guessed/estimated value.""" + fake_popen(returncode=0, stdout="plain text report", stderr="") + adapter = CursorAdapter() + result = adapter.run("hi", workdir=tmp_path) + assert result.cost_usd is None diff --git a/tests/test_agents/test_gemini.py b/tests/test_agents/test_gemini.py index b8aa4ed..9b7a227 100644 --- a/tests/test_agents/test_gemini.py +++ b/tests/test_agents/test_gemini.py @@ -27,3 +27,12 @@ def test_extract_report_result_key_fallback(): def test_extract_report_non_json_passthrough(): adapter = GeminiAdapter() assert adapter.extract_report("plain text") == "plain text" + + +def test_run_success_leaves_cost_usd_none(fake_popen, tmp_path): + """Gemini CLI's JSON output has no reported spend figure -- cost_usd + must stay None rather than a guessed/estimated value.""" + fake_popen(returncode=0, stdout=json.dumps({"response": "R"}), stderr="") + adapter = GeminiAdapter() + result = adapter.run("hi", workdir=tmp_path) + assert result.cost_usd is None