Phase 7: AI-assisted query authoring (autocomplete, explain, fix, optimize, NL translation)

Adds a self-hosted (Ollama, qwen2.5-coder) model provider abstraction
with a pluggable opt-in cloud adapter, schema grounding, and a shared
cost/safety guard every AI-suggested query is assessed against --
compiling to and executing through the same unchanged Phase 2 IR/
compiler and Phase 4 tenant scoping as a hand-written query, no
parallel execution path.

Track A (built into the query bar): inline ghost-text autocomplete,
"Explain this query", "Fix this query" with a diff view, and a
rule-based "Optimize" suggestion. Track B: natural-language-to-query
translation, always a separate review step from execution, with
`sentryctl query --nl` requiring explicit confirmation to run.
Every accepted/dismissed translate-fix-optimize interaction is logged
into the same append-only audit_log table Phase 4 built.

Two real product bugs were found and fixed via live browser
verification (a Svelte effect re-running on every keystroke that
silently cancelled the ghost-text debounce; a ghost-text widget
positioned at document offset 0 instead of the cursor), and a real
costguard logic bug (unbounded-aggregation vs. raw-row) was caught by
its own test suite. New integration tests wire a real Ollama client
through the real HTTP handler against a mock server matching Ollama's
wire contract (hack/mock-ollama), keeping model-quality verification
out of CI as a disclosed, periodic human-run check instead.

See /docs/phase-7-ai-design.md and /docs/phase-7-runbook.md.
This commit is contained in:
2026-08-16 18:06:27 -07:00
parent 661568085e
commit 7d316f92db
37 changed files with 5230 additions and 20 deletions
+20 -1
View File
@@ -19,6 +19,7 @@ import (
"strings"
"time"
"github.com/sentry/sentry/api/ai/costguard"
"github.com/sentry/sentry/api/authz"
"github.com/sentry/sentry/api/internal/querylang/planner"
"github.com/sentry/sentry/api/querylang/executor"
@@ -99,6 +100,20 @@ type queryRequest struct {
type queryResponse struct {
Columns []string `json:"columns"`
Rows [][]any `json:"rows"`
// Warnings surfaces costguard's assessment (Phase 7 task 4) for
// every query, hand-written or AI-suggested alike -- the same
// guard, never a hard block here. AI-suggested queries get a
// stricter treatment (a Reject-level assessment withholds the
// suggestion entirely, see the ai package) before a query ever
// reaches this handler; a hand-written query submitted directly
// always runs regardless of what this says, matching every prior
// phase's behavior -- this field is informational, not new
// enforcement, a deliberate choice recorded in
// /docs/phase-7-ai-design.md rather than a retrofit nobody decided
// on. Omitted (not an empty array) when there's nothing to say, so
// existing callers that don't look for this field see no shape
// change at all.
Warnings []string `json:"warnings,omitempty"`
}
type errorResponse struct {
@@ -149,7 +164,11 @@ func (h *Handler) handleQuery(w http.ResponseWriter, r *http.Request) {
}
h.logAudit(r.Context(), req, len(result.Rows), duration, nil)
writeJSON(w, queryResponse{Columns: result.Columns, Rows: result.Rows})
resp := queryResponse{Columns: result.Columns, Rows: result.Rows}
if assessment := costguard.Assess(plan); assessment.Level != costguard.LevelOK {
resp.Warnings = []string{costguard.Summary(assessment)}
}
writeJSON(w, resp)
}
// logAudit is fail-open by design (see AuditLogger's doc comment): a