Skip to content
Closed
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
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ secfoo run --skill security-architecture-review --agent claude
```

You'll need an agent CLI already installed and authenticated — Claude
Code, Cursor, Antigravity, or Gemini CLI (`--agent claude|agent|agy|gemini`).
Code, Cursor, Antigravity, or Gemini CLI (`--agent claude|agent|agy|gemini`)
— or just a model API key with the built-in `api` agent (below).
On a real terminal, that first run prompts for a project name and
application ID, then browse every result in the dashboard:

Expand All @@ -51,6 +52,35 @@ secfoo run --skill sast --skill threat-modeling \
secfoo serve
```

### No agent CLI? Use an API key

The built-in `api` agent is a LangGraph workflow that sends the target's
source to OpenAI, Anthropic or Gemini through LiteLLM — no coding-agent
CLI needed, which suits CI. Keys are read from the environment only and
never stored:

```bash
pip install "secfoo[api]"
export OPENAI_API_KEY=... # or ANTHROPIC_API_KEY / GEMINI_API_KEY
export SECFOO_API_MODEL=openai/gpt-4.1-mini # optional; any LiteLLM model id
secfoo run --skill sast --agent api --target https://github.com/org/repo
```

It suits small and medium repositories: the whole target has to fit in one
request, and larger ones are rejected with a message suggesting `--exclude`.

### Tracking AI spend

Every run records tokens and cost where the agent reports them (`api`,
`claude`; `gemini` reports tokens only). See it per run in `secfoo run`,
`secfoo list` and the dashboard, or summarised:

```bash
secfoo cost # by agent
secfoo cost --by skill --since 2026-09-01
secfoo cost --project checkout
```

See [Getting Started](https://secfoo.com/docs/index.html#quickstart) or
the [CLI reference](https://secfoo.com/docs/cli.html) for more.

Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ dependencies = [
]

[project.optional-dependencies]
dev = ["pytest>=8.0", "httpx>=0.27", "ruff>=0.4", "bandit>=1.8", "pip-audit>=2.7"]
dev = ["pytest>=8.0", "httpx>=0.27", "ruff>=0.4", "bandit>=1.8", "pip-audit>=2.7", "langgraph>=0.2"]
build = ["pyinstaller>=6.0"]
# The `api` agent (secfoo/agents/api.py): a LangGraph workflow that calls OpenAI/Anthropic/Gemini
# through LiteLLM with an API key instead of driving an installed agent CLI.
# (dev includes langgraph so the real graph runs in tests; litellm is faked.)
api = ["litellm>=1.40", "langgraph>=0.2"]

[project.scripts]
secfoo = "secfoo.cli:main"
Expand Down
235 changes: 235 additions & 0 deletions src/secfoo/agents/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
"""Agent that calls a model API directly (OpenAI, Anthropic, Gemini via LiteLLM).

Unlike the CLI adapters, the model can't open files itself, so the target's
source files are pasted into the prompt ("context stuffing", Tier A).

The run is a small LangGraph graph:

collect_files -> assess -> (report complete?) -> END
^ | no, first attempt
+- request_fix <+

`request_fix` asks the model to reformat its own draft into the required
report format without resending the source, so a malformed report costs one
cheap extra call rather than a second full assessment.
"""

from __future__ import annotations

import fnmatch
import os
import time
from pathlib import Path
from typing import TypedDict

from secfoo.agents.base import AgentAdapter, AgentResult, Status, Usage
from secfoo.report.severity import extract_overall_risk_rating
from secfoo.skills.renderer import DEFAULT_EXCLUDE_PATHS

KEY_VARS = ("OPENAI_API_KEY", "ANTHROPIC_API_KEY", "GEMINI_API_KEY")
DEFAULT_MODEL = "openai/gpt-4.1-mini"
MAX_FILE_BYTES = 200_000
MAX_TOTAL_CHARS = 2_000_000
MAX_ATTEMPTS = 2 # one assessment + at most one reformat
NUM_RETRIES = 3 # LiteLLM's own backoff for rate limits / transient errors

DIR_PATTERNS = [p.rstrip("/") for p in DEFAULT_EXCLUDE_PATHS if p.endswith("/")]
FILE_PATTERNS = [p for p in DEFAULT_EXCLUDE_PATHS if not p.endswith("/")]

FIX_REQUEST = (
"## Draft report to reformat\n"
"You already reviewed the target source and wrote the draft below, but it does not "
"follow the required output format above (it has no `**Overall risk rating:**` line). "
"Rewrite it into exactly that format. Keep every finding as-is: do not add, drop, or "
"re-rate findings. Output only the report.\n\n"
)


class _State(TypedDict):
prompt: str
workdir: str
model: str
timeout: int
messages: list[dict]
report: str
attempts: int
input_tokens: int
output_tokens: int
cost_usd: float
unpriced: bool


def _matches(name: str, patterns: list[str]) -> bool:
return any(fnmatch.fnmatch(name, p) for p in patterns)


def collect_files(workdir: Path) -> str:
"""Return every readable text file under `workdir` as one labelled string."""
chunks: list[str] = []
total = 0
for root, dirs, files in os.walk(workdir):
dirs[:] = sorted(d for d in dirs if not _matches(d, DIR_PATTERNS))
for name in sorted(files):
path = Path(root) / name
if _matches(name, FILE_PATTERNS) or path.stat().st_size > MAX_FILE_BYTES:
continue
try:
text = path.read_text(encoding="utf-8")
except (UnicodeDecodeError, OSError):
continue # binary file (image, font, ...)
chunk = f"\n===== FILE: {path.relative_to(workdir).as_posix()} =====\n{text}\n"
total += len(chunk)
if total > MAX_TOTAL_CHARS:
raise ValueError(
f"Target is too large for the api agent (over {MAX_TOTAL_CHARS:,} characters). "
"Use --exclude to narrow it, or use a CLI agent."
)
chunks.append(chunk)
return "".join(chunks)


def build_graph(litellm, StateGraph, START, END):
"""The assessment workflow. Takes the libraries as arguments so tests can
run the real graph against a fake LiteLLM."""

def collect(state: _State) -> dict:
code = collect_files(Path(state["workdir"]))
content = (
f"{state['prompt']}\n\n## Source files\nYou cannot open files. "
f"The complete target source is included below.\n{code}"
)
return {"messages": [{"role": "user", "content": content}]}

def assess(state: _State) -> dict:
response = litellm.completion(
model=state["model"],
messages=state["messages"],
timeout=state["timeout"],
num_retries=NUM_RETRIES,
)
usage = _usage(litellm, response)
return {
"report": response.choices[0].message.content or "",
"attempts": state["attempts"] + 1,
"input_tokens": state["input_tokens"] + (usage.input_tokens or 0),
"output_tokens": state["output_tokens"] + (usage.output_tokens or 0),
"cost_usd": state["cost_usd"] + (usage.cost_usd or 0.0),
"unpriced": state["unpriced"] or usage.cost_usd is None,
}

def request_fix(state: _State) -> dict:
# Fresh, short conversation: the instructions (which carry the output
# contract) plus the draft -- not the source files again.
content = f"{state['prompt']}\n\n{FIX_REQUEST}{state['report']}"
return {"messages": [{"role": "user", "content": content}]}

def next_step(state: _State) -> str:
if extract_overall_risk_rating(state["report"]) is not None or state["attempts"] >= MAX_ATTEMPTS:
return END
return "request_fix"

graph = StateGraph(_State)
graph.add_node("collect_files", collect)
graph.add_node("assess", assess)
graph.add_node("request_fix", request_fix)
graph.add_edge(START, "collect_files")
graph.add_edge("collect_files", "assess")
graph.add_conditional_edges("assess", next_step, ["request_fix", END])
graph.add_edge("request_fix", "assess")
return graph.compile()


class ApiAdapter(AgentAdapter):
name = "api"
binary = "API key" # label only: shown in `secfoo agents`

def is_available(self) -> bool:
return any(os.environ.get(k) for k in KEY_VARS)

def build_command(self, prompt: str, *, workdir: Path) -> list[str]:
return [] # required by the base class; there is no program to launch

def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> AgentResult:
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)}")
try:
import litellm
from langgraph.graph import END, START, StateGraph
except ImportError:
return self._result(
"failed", started, stderr='litellm/langgraph are not installed. Run: pip install "secfoo[api]"'
)
try:
final = build_graph(litellm, StateGraph, START, END).invoke({
"prompt": prompt,
"workdir": str(workdir),
"model": os.environ.get("SECFOO_API_MODEL", DEFAULT_MODEL),
"timeout": timeout or self.default_timeout_seconds,
"messages": [],
"report": "",
"attempts": 0,
"input_tokens": 0,
"output_tokens": 0,
"cost_usd": 0.0,
"unpriced": False,
})
except Exception as exc: # noqa: BLE001 -- any failure becomes a failed run with its message
return self._result("failed", started, stderr=str(exc))
usage = Usage(
input_tokens=final["input_tokens"],
output_tokens=final["output_tokens"],
cost_usd=None if final["unpriced"] else final["cost_usd"],
)
report = final["report"]
return self._result("success", started, stdout=report, report=report, usage=usage)

def _result(
self,
status: Status,
started: float,
*,
stdout: str = "",
stderr: str = "",
report: str = "",
usage: Usage | None = None,
) -> AgentResult:
usage = usage or Usage()
return AgentResult(
agent=self.name,
exit_code=0 if status == "success" else None,
stdout=stdout,
stderr=stderr,
duration_seconds=time.monotonic() - started,
timed_out=False,
status=status,
raw_report=report,
input_tokens=usage.input_tokens,
output_tokens=usage.output_tokens,
cost_usd=usage.cost_usd,
)


def _strip_key_whitespace() -> None:
"""A key pasted with a trailing newline makes httpx reject the auth
header, which LiteLLM reports only as a vague "Connection error"."""
for key in KEY_VARS:
value = os.environ.get(key)
if value and value != value.strip():
os.environ[key] = value.strip()


def _usage(litellm, response) -> Usage:
"""Token counts from the response; cost from LiteLLM's price table
(None for a model LiteLLM has no price for)."""
tokens = getattr(response, "usage", None)
try:
cost = litellm.completion_cost(completion_response=response)
except Exception: # noqa: BLE001 -- unknown model pricing shouldn't fail a successful run
cost = None
return Usage(
input_tokens=getattr(tokens, "prompt_tokens", None),
output_tokens=getattr(tokens, "completion_tokens", None),
cost_usd=float(cost) if cost is not None else None,
)
20 changes: 20 additions & 0 deletions src/secfoo/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ class AgentResult:
timed_out: bool
status: Status
raw_report: str
# AI spend for this run, when the agent reports it. None means unknown
# (the CLI doesn't expose it), not zero.
input_tokens: int | None = None
output_tokens: int | None = None
cost_usd: float | None = None


@dataclass(frozen=True)
class Usage:
input_tokens: int | None = None
output_tokens: int | None = None
cost_usd: float | None = None


class AgentAdapter(ABC):
Expand All @@ -78,6 +90,10 @@ def extract_report(self, stdout: str) -> str:
"""Pull the report text out of raw stdout. Default: stdout is the report."""
return stdout

def extract_usage(self, stdout: str) -> Usage:
"""Pull token counts / cost out of raw stdout. Default: unknown."""
return Usage()

def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> AgentResult:
if not self.is_available():
return AgentResult(
Expand Down Expand Up @@ -112,6 +128,7 @@ def run(self, prompt: str, *, workdir: Path, timeout: int | None = None) -> Agen
stdout, stderr = proc.communicate(timeout=effective_timeout)
duration = time.monotonic() - started
status: Status = "success" if proc.returncode == 0 else "failed"
usage = self.extract_usage(stdout)
return AgentResult(
agent=self.name,
exit_code=proc.returncode,
Expand All @@ -121,6 +138,9 @@ 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 "",
input_tokens=usage.input_tokens,
output_tokens=usage.output_tokens,
cost_usd=usage.cost_usd,
)
except subprocess.TimeoutExpired:
_kill_process_tree(proc.pid)
Expand Down
24 changes: 23 additions & 1 deletion src/secfoo/agents/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
from pathlib import Path

from secfoo.agents.base import AgentAdapter
from secfoo.agents.base import AgentAdapter, Usage
from secfoo.mcp import write_claude_mcp_config
from secfoo.settings import load_config

Expand Down Expand Up @@ -55,3 +55,25 @@ def extract_report(self, stdout: str) -> str:
except json.JSONDecodeError:
return stdout
return payload.get("result", stdout)

def extract_usage(self, stdout: str) -> Usage:
# `--output-format json` reports total_cost_usd plus a usage block.
# On a subscription login the cost is claude's API-price estimate,
# not an amount actually billed.
try:
payload = json.loads(stdout)
except json.JSONDecodeError:
return Usage()
if not isinstance(payload, dict):
return Usage()
usage = payload.get("usage") or {}
input_tokens = sum(
usage.get(key) or 0
for key in ("input_tokens", "cache_creation_input_tokens", "cache_read_input_tokens")
)
cost = payload.get("total_cost_usd")
return Usage(
input_tokens=input_tokens if usage else None,
output_tokens=usage.get("output_tokens"),
cost_usd=float(cost) if cost is not None else None,
)
Loading
Loading