feat(ai): provider-neutral AI layer and AI mitigation advice on findings #67

Merged
james merged 3 commits from james/ai-mitigation into main 2026-11-19 21:01:50 +00:00 AGit
Owner

Adds AI provider settings (Anthropic API, OpenAI API, custom OpenAI-compatible/local endpoint) and per-finding AI mitigation advice. Keys encrypted and never returned; SSRF-guarded; monthly token limit; usage log. Advice runs in the background, is stored separately from the finding's mitigation, and is applied only on request. See commit message for details.

Adds AI provider settings (Anthropic API, OpenAI API, custom OpenAI-compatible/local endpoint) and per-finding AI mitigation advice. Keys encrypted and never returned; SSRF-guarded; monthly token limit; usage log. Advice runs in the background, is stored separately from the finding's mitigation, and is applied only on request. See commit message for details.
feat(ai): provider-neutral AI layer and AI mitigation advice on findings
Some checks failed
security-scan / security-scan (pull_request) Successful in 40s
claude-review / review (pull_request) Failing after 1m30s
test / web (pull_request) Successful in 2m28s
test / coverage-badge (pull_request) Has been skipped
test / go (pull_request) Successful in 4m22s
d4bd896e05
Adds internal/ai with one Provider interface and three backends, configured
per organization under System Settings -> AI:

- Anthropic API via the official Go SDK (streamed, structured output,
  claude-opus-5-5 by default, server-side refusal fallbacks on models that
  support them)
- OpenAI API (Chat Completions with strict json_schema)
- any OpenAI-compatible endpoint (Ollama, vLLM, llama.cpp, LM Studio,
  LiteLLM); falls back to describing the schema in the prompt when a local
  server rejects response_format

The service enforces an optional monthly token limit and a small
concurrency cap, and records every request (feature, user, model, tokens,
outcome) in ai_usages. API keys are SecretBox-encrypted and never returned;
changing the provider or base URL drops a stored key unless a new one is
sent, so a key can't be pointed at a new endpoint without re-entering it.
All calls go through the outbound SSRF guard, so a local model on a private
address needs BALAM_OUTBOUND_ALLOW.

First feature: per-finding mitigation advice. Writers generate it from the
finding, its product, scan type, endpoints and matching SBOM components; the
model returns urgency, steps, fixed version, workarounds, verification,
references and a false-positive hint. Generation runs in the background
(models can exceed the 60s write timeout) and the UI polls. Advice is kept
separate from Finding.Mitigation, marked stale when the finding changes, and
copied over only on request. Scanner text is passed as delimited data with
instructions not to follow it, and reference links are filtered to http(s).

Endpoints: GET /ai/status, GET/PUT /ai/settings, POST /ai/settings/test,
GET /ai/usage (admin); GET/POST /findings/{id}/advice,
POST /findings/{id}/advice/apply.

🔎 ojo scan results

No findings.

<!-- ojo-scan-summary --> ### 🔎 ojo scan results No findings.

Codecov Results 📊

✅ Patch coverage is 100.00%. Project has 5221 uncovered lines.


Generated by Codecov Action

<!-- codecov-action-results --> ## Codecov Results 📊 :white_check_mark: Patch coverage is **100.00%**. Project has **5221** uncovered lines. --- *Generated by [Codecov Action](https://github.com/getsentry/codecov-action)*
feat(ai): tiered model routing, token savings, and review fixes
Some checks failed
security-scan / security-scan (pull_request) Successful in 39s
test / coverage-badge (pull_request) Has been cancelled
test / web (pull_request) Has been cancelled
test / go (pull_request) Has been cancelled
claude-review / review (pull_request) Has been cancelled
4d09164ae2
Model routing: every AI request declares a tier and runs on that tier's
model and effort. Defaults: fast = claude-haiku-5-5 / gpt-6-luna (low),
standard = claude-sonnet-5-5 / gpt-6.2-sol (medium), deep = claude-opus-5-5
/ gpt-6.2-astra (high). Each tier can be overridden per org; the settings
page suggests alternatives per tier from ai.ModelCatalog(). OpenAI
reasoning models get reasoning_effort; OpenAI-compatible endpoints have no
defaults and fall back to the standard model. Usage rows record the tier.

Token savings: advice for an unchanged finding is reused instead of
regenerated (?force=true to override), the finding is sent as compact
JSON with 8,000-character field caps, advice output is capped at 8K
tokens, and the connection test hits each distinct model once at low
effort. The monthly token limit is checked before a job is queued (429).

Review fixes (GPT, AGY):
- advice writes are tied to a job id, so a superseded run can't overwrite
  a newer result
- the finding row is locked before advice is created, so concurrent first
  requests don't hit the unique index
- generating and applying advice checks writer on the finding's product
  (EffectiveRole) instead of the org role only
- docs: README AI providers section (tiers, local models, what is sent,
  limits), BALAM_OUTBOUND_ALLOW_HTTP covers AI providers, OpenAPI fields,
  status codes and pagination, AISettings.APIKeyEnc in the encrypted
  column lists
fix(deps): bump OpenTelemetry to v1.47.0 (CVE-2026-128085, CVE-2026-41178)
Some checks failed
claude-review / review (pull_request) Failing after 1m22s
security-scan / security-scan (pull_request) Successful in 1m47s
test / web (pull_request) Successful in 1m55s
test / coverage-badge (pull_request) Has been skipped
test / go (pull_request) Successful in 4m51s
b5b26c2b84
go.opentelemetry.io/otel, otel/trace and otel/metric come in indirectly via
the Anthropic SDK. v1.43.0 is affected by CVE-2026-128085 (attribute value
length limit bypass) and CVE-2026-41178 (unbounded baggage header parsing).
ojo reports no vulnerabilities after the bump.
james merged commit 9ed8481c6a into main 2026-11-19 21:01:50 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ColibriSec/balam!67
No description provided.