Skip to content

Feat(Agent): Add built-in LangGraph agent with LiteLLM routing and AI spend tracking - #10

Merged
vsn411 merged 1 commit into
secfoo-com:mainfrom
karthickmk2001:feature/secfoo-agent-cost-tracking
Sep 21, 2026
Merged

vsn411 merged 1 commit into
secfoo-com:mainfrom
karthickmk2001:feature/secfoo-agent-cost-tracking

Conversation

@karthickmk2001

Copy link
Copy Markdown
Collaborator

Summary

  • Adds a new secfoo agent adapter (src/secfoo/agents/secfoo.py) that runs skills in-process via LangGraph, routing every model call through LiteLLM so any provider/model LiteLLM supports works through one interface (SECFOO_MODEL, default openai/gpt-4o-mini). Registered alongside the existing claude/agent/agy/gemini adapters.
  • Adds AI spend tracking: AgentResult.cost_usd (from LiteLLM's completion_cost) is threaded through the runner into a new runs.cost_usd SQLite column, and surfaced via:
    • secfoo cost / secfoo cost --project <name> CLI command
    • a Cost column in secfoo list
    • the web dashboard (project page, run detail page, and a new "Recorded AI spend" stat card on the program overview)
    • the cloud-sync payload (push_run)
  • No credentials are stored anywhere; provider keys are read from the environment only (OPENAI_API_KEY / ANTHROPIC_API_KEY / GEMINI_API_KEY), matching this repo's existing convention.

Test plan

  • pytest tests — 640 passed. The only failures (6) are pre-existing on main and untouched by this diff (a Windows os.chmod POSIX-permissions test, and pre-existing mermaid-diagram/depth tests unrelated to any file this PR touches).
  • New unit tests: tests/test_agents/test_secfoo.py (LangGraph/LiteLLM wiring, cost capture, availability check), secfoo cost CLI tests, storage round-trip tests for cost_usd/total_cost_usd, cloud payload test.
  • Manually ran secfoo run --skill secret-scanning --agent secfoo --depth quick end-to-end against a real OpenAI key, then verified secfoo cost, secfoo list, and the dashboard (overview/project/run-detail pages) all show the recorded spend correctly.
  • bandit -r src/secfoo/agents/secfoo.py src/secfoo/cli.py — no issues.

🤖 Generated with Claude Code

Adds a `secfoo` agent adapter that runs skills in-process via LangGraph,
routing every model call through LiteLLM so any provider/model works
through one interface. Per-run cost is captured from LiteLLM and stored
alongside each run, surfaced via `secfoo cost`, the `list`/run-detail
CLI output, the web dashboard, and the cloud-sync payload.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@karthickmk2001 karthickmk2001 changed the title feat: built-in LangGraph/LiteLLM agent with AI spend tracking Feat(Agent): Add built-in LangGraph agent with LiteLLM routing and AI spend tracking Sep 21, 2026
geet038 added a commit that referenced this pull request Sep 21, 2026
Rebuild the api agent's run as a LangGraph graph (collect_files ->
assess -> report check), with LiteLLM still the only model gateway. If
a report comes back without the `**Overall risk rating:**` line every
output contract requires, the graph asks the model once to reformat its
own draft -- without resending the source, so the retry stays cheap.
Tokens and cost add up across both calls. LiteLLM now retries rate
limits and transient errors itself (num_retries=3); running several
skills at once previously failed on OpenAI's rate limit.

Fold in the useful parts of PR #10:
- tokens and cost in the cloud-sync payload and create_completed_run
- `secfoo cost --project`, a README section, config.example.toml hint
- UTF-8 for every text read/write, not just runner's report writes.
  Report reads use errors="replace" so reports written by older
  versions in the Windows code page still render instead of crashing.
  The suite now gives the same result with or without PYTHONUTF8.

langgraph joins litellm in the `api` extra, and dev (litellm stays
faked in tests, so CI doesn't need it).

Co-Authored-By: Claude Opus 5 <[email protected]>
@vsn411
vsn411 merged commit 1b98c09 into secfoo-com:main Sep 21, 2026
2 checks passed
geet038 added a commit that referenced this pull request Sep 21, 2026
The conflict resolution kept both sides in several places:
- repository.py create_completed_run: two INSERT statements spliced
  together (unclosed paren -> SyntaxError, every test failed) and a
  duplicate cost_usd parameter
- repository.py complete_run: a leftover SQL line from #10 after the
  UPDATE string, so execute() received three arguments
- schema.sql: cost_usd declared twice (duplicate column name)
- cli.py: two `cost` commands and two Cost columns in `list`
- overview/project/run_detail templates: both cost renderings; README:
  interleaved intro sentence and an unclosed code fence

Keep one version of each (the one with tokens and "-" for unknown
cost), drop the now-unused total_cost_usd from the overview route, and
point #10's two `cost` CLI tests at the current command. Both the
`secfoo` and `codex` agents from main are kept.

Co-Authored-By: Claude Opus 5 <[email protected]>
AdithKB added a commit to AdithKB/secfoo that referenced this pull request Oct 3, 2026
…odel)

src/secfoo/agents/secfoo.py was the original Initiative-2 draft (PR secfoo-com#10).
PR secfoo-com#11 fixed the exact bug it has -- the agent never reads the target's
files, so the model reviews nothing and fabricates a report -- but fixed
it in a new, separate file (api.py) and never removed this one. Both
have been registered and independently selectable via --agent ever
since, with no indication either is broken.

Reproduced twice against we45/Vulnerable-Flask-App (SAST and Secret
Scanning): --agent secfoo returns "Confirmed" findings at file paths
that don't exist anywhere in the target (app/db.py, app/upload.py,
deploy/ci.yml), because build_graph's _State/run() never wires the
resolved workdir into the model call at all -- collect_files() (the
function that actually reads source) is never called from this file.
The adapter's own test asserts this as correct behavior
(messages == [{"role": "user", "content": "review this target"}]),
so CI can't catch it.

--agent api is the real, fixed implementation and needs no changes.
This just removes the dead duplicate and its now-stale references:
registry, AgentId enum, __init__ exports, its test, and two docs spots
(README's duplicate "built-in LangGraph agent" section, and
config.example.toml's three conflicting `agent =` example lines).

Full suite: 700 passed (down from 702 -- exactly the 2 tests removed
with the dead file). bandit clean on the touched agent files.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants