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:
@@ -0,0 +1,79 @@
|
||||
// Adapts *Store to api/ai/aiapi.InteractionLogger -- same shape as
|
||||
// queryapi_adapter.go's QueryAPILogger, wired in by
|
||||
// enterprise/cmd/enterprise-api alongside it (Phase 7 task 12).
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/sentry/sentry/api/ai/aiapi"
|
||||
"github.com/sentry/sentry/api/authz"
|
||||
)
|
||||
|
||||
// AIInteractionLogger implements aiapi.InteractionLogger by translating
|
||||
// its InteractionEntry into this package's Entry, reading tenant/user
|
||||
// identity from ctx -- same "read identity from ctx rather than the
|
||||
// interface growing tenant-awareness" shape as QueryAPILogger.
|
||||
type AIInteractionLogger struct {
|
||||
store *Store
|
||||
source Source
|
||||
}
|
||||
|
||||
func NewAIInteractionLogger(store *Store, source Source) *AIInteractionLogger {
|
||||
return &AIInteractionLogger{store: store, source: source}
|
||||
}
|
||||
|
||||
// aiInteractionDetail is what Detail carries -- Operation/Confidence/
|
||||
// Accepted/Edited don't have dedicated audit_log columns (same reasoning
|
||||
// as role_change/grant_change already using Detail instead of
|
||||
// query_text/row_count/duration_ms), only QueryText (FinalQuery) does.
|
||||
type aiInteractionDetail struct {
|
||||
Operation string `json:"operation"`
|
||||
Input string `json:"input"`
|
||||
Output string `json:"output"`
|
||||
Confidence string `json:"confidence,omitempty"`
|
||||
Accepted bool `json:"accepted"`
|
||||
Edited bool `json:"edited"`
|
||||
}
|
||||
|
||||
func (l *AIInteractionLogger) LogInteraction(ctx context.Context, entry aiapi.InteractionEntry) error {
|
||||
identity, ok := authz.IdentityFromContext(ctx)
|
||||
if !ok || identity.TenantID == "" {
|
||||
return fmt.Errorf("audit: no tenant identity in context, refusing to write an unattributable audit entry")
|
||||
}
|
||||
|
||||
var userID *string
|
||||
if identity.UserID != "" {
|
||||
userID = &identity.UserID
|
||||
}
|
||||
|
||||
detail, err := json.Marshal(aiInteractionDetail{
|
||||
Operation: entry.Operation,
|
||||
Input: entry.Input,
|
||||
Output: entry.Output,
|
||||
Confidence: entry.Confidence,
|
||||
Accepted: entry.Accepted,
|
||||
Edited: entry.Edited,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("audit: marshaling ai interaction detail: %w", err)
|
||||
}
|
||||
|
||||
var queryText *string
|
||||
if entry.FinalQuery != "" {
|
||||
queryText = &entry.FinalQuery
|
||||
}
|
||||
|
||||
_, err = l.store.Append(ctx, Entry{
|
||||
TenantID: identity.TenantID,
|
||||
UserID: userID,
|
||||
Source: l.source,
|
||||
EventType: EventAIInteraction,
|
||||
QueryText: queryText,
|
||||
Status: StatusSuccess,
|
||||
Detail: detail,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -47,6 +47,12 @@ const (
|
||||
EventGrantChange EventType = "grant_change"
|
||||
EventSSOConfigChange EventType = "sso_config_change"
|
||||
EventSecretReveal EventType = "secret_reveal"
|
||||
// EventAIInteraction (Phase 7 task 12): a translate/fix/optimize
|
||||
// suggestion's accept-or-dismiss outcome. QueryText carries the
|
||||
// resulting query (if any); Detail carries the operation, the
|
||||
// original input, confidence, and whether the user edited the
|
||||
// suggestion before using it -- see ai_interaction_adapter.go.
|
||||
EventAIInteraction EventType = "ai_interaction"
|
||||
)
|
||||
|
||||
type Status string
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user