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
@@ -13,6 +13,7 @@ package audit
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
@@ -22,6 +23,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/sentry/sentry/api/ai/aiapi"
"github.com/sentry/sentry/api/authz"
"github.com/sentry/sentry/api/queryapi"
)
@@ -140,6 +142,74 @@ func TestQueryAPILoggerRefusesWithoutIdentity(t *testing.T) {
}
}
// TestAIInteractionLoggerWritesAttributedToContextIdentity is
// AIInteractionLogger's counterpart to TestQueryAPILoggerWritesAttributedToContextIdentity
// above (Phase 7 task 12) -- same "reads identity from ctx" contract,
// plus a check that Detail actually round-trips the operation/
// confidence/accepted/edited fields that don't have dedicated columns.
func TestAIInteractionLoggerWritesAttributedToContextIdentity(t *testing.T) {
writerPool := testPool(t, "audit_writer", os.Getenv("AUDIT_TEST_POSTGRES_PASSWORD"))
adminPool := testPool(t, "sentry", os.Getenv("AUDIT_TEST_ADMIN_PASSWORD"))
cleanupAuditLog(t, adminPool)
defer cleanupAuditLog(t, adminPool)
logger := NewAIInteractionLogger(NewStore(writerPool), SourceAPI)
ctx := authz.WithIdentity(context.Background(), authz.Identity{TenantID: "acme", UserID: "22222222-2222-2222-2222-222222222222", Role: authz.RoleViewer})
err := logger.LogInteraction(ctx, aiapi.InteractionEntry{
Operation: "translate",
Input: "errors in the last hour",
Output: "earliest=-1h severity=ERROR",
Confidence: "high",
Accepted: true,
Edited: false,
FinalQuery: "earliest=-1h severity=ERROR",
})
if err != nil {
t.Fatalf("LogInteraction: %v", err)
}
var tenantID, userID, eventType, queryText string
var detail []byte
row := adminPool.QueryRow(context.Background(),
`SELECT tenant_id, user_id, event_type, query_text, detail FROM audit_log ORDER BY id DESC LIMIT 1`)
if err := row.Scan(&tenantID, &userID, &eventType, &queryText, &detail); err != nil {
t.Fatalf("reading back the written row: %v", err)
}
if tenantID != "acme" || userID != "22222222-2222-2222-2222-222222222222" {
t.Fatalf("got tenant_id=%q user_id=%q, want acme/22222222-...", tenantID, userID)
}
if eventType != "ai_interaction" {
t.Fatalf("event_type = %q, want ai_interaction", eventType)
}
if queryText != "earliest=-1h severity=ERROR" {
t.Fatalf("query_text = %q, want the final query", queryText)
}
var parsed struct {
Operation string `json:"operation"`
Accepted bool `json:"accepted"`
}
if err := json.Unmarshal(detail, &parsed); err != nil {
t.Fatalf("unmarshaling detail: %v", err)
}
if parsed.Operation != "translate" || !parsed.Accepted {
t.Fatalf("detail = %+v, want operation=translate accepted=true", parsed)
}
}
func TestAIInteractionLoggerRefusesWithoutIdentity(t *testing.T) {
writerPool := testPool(t, "audit_writer", os.Getenv("AUDIT_TEST_POSTGRES_PASSWORD"))
adminPool := testPool(t, "sentry", os.Getenv("AUDIT_TEST_ADMIN_PASSWORD"))
cleanupAuditLog(t, adminPool)
defer cleanupAuditLog(t, adminPool)
logger := NewAIInteractionLogger(NewStore(writerPool), SourceAPI)
err := logger.LogInteraction(context.Background(), aiapi.InteractionEntry{Operation: "fix", Accepted: false})
if err == nil {
t.Fatal("expected LogInteraction to refuse writing an entry with no tenant identity in context")
}
}
// TestVerifyChainDetectsTampering proves the chain actually catches an
// in-place row modification -- not just that VerifyChain runs without
// erroring on untampered data, which a bug returning OK unconditionally