Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(review): start the AI review when the SQL is submitted or changed (#21695)
h3n4lc086a5c48035
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.goindex f69fd45a24..ea30eabd62 100644--- a/backend/api/v1/issue_service.go+++ b/backend/api/v1/issue_service.go@@ -525,10 +525,11 @@ func (s *IssueService) CreateIssue(ctx context.Context, req *connect.Request[v1p}}- // The review starts before the workflow, whose approval check can create- // the rollout: a slot already in place holds it until the review is done.+ // The reviews start before the workflow, whose approval check can create+ // the rollout: a rule slot already in place holds it until that review is+ // done.if created {- startRuleReview(ctx, s.profile, s.store, s.bus, issue)+ startReviews(ctx, s.profile, s.store, s.bus, issue, store.ReviewRunTypeRule, store.ReviewRunTypeAI)}if created && !issue.Payload.GetDraft() {issue, err = startIssueWorkflow(ctx, s.store, s.webhookManager, s.licenseService, s.bus, project, user.Name, user.Email, issue)diff --git a/backend/api/v1/issue_service_review_run.go b/backend/api/v1/issue_service_review_run.goindex 19f69c150e..61bb328f63 100644--- a/backend/api/v1/issue_service_review_run.go+++ b/backend/api/v1/issue_service_review_run.go@@ -169,31 +169,52 @@ func checkReviewable(ctx context.Context, stores *store.Store, issue *store.Issureturn nil}-// startRuleReview starts the standard rule review of the issue where SQL-// Review V2 is on, at the points the plan checks run. It never fails its-// caller: an issue the review does not apply to is skipped, and a failure is-// logged.-func startRuleReview(ctx context.Context, profile *config.Profile, stores *store.Store, b *bus.Bus, issue *store.IssueMessage) {+// startReviews starts the issue's reviews of the given types where SQL Review+// V2 is on, at the points the plan checks run. The AI review starts only while+// the workspace allows it, and only the rule review's run holds the rollout.+// It never fails its caller: an issue the reviews do not apply to is skipped,+// and a failure is logged.+func startReviews(ctx context.Context, profile *config.Profile, stores *store.Store, b *bus.Bus, issue *store.IssueMessage, reviewTypes ...string) {if !profile.SQLReviewV2 || issue == nil || issue.Type != storepb.Issue_DATABASE_CHANGE {return}if err := checkReviewable(ctx, stores, issue); err != nil {if connect.CodeOf(err) != connect.CodeFailedPrecondition {- slog.Warn("failed to start the standard rule review",+ slog.Warn("failed to start the review",slog.String("project", issue.ProjectID),slog.Int64("issue_id", issue.UID),log.BBError(err))}return}- if _, err := stores.CreateReviewRun(ctx, issue.ProjectID, issue.UID, store.ReviewRunTypeRule); err != nil {- slog.Error("failed to start the standard rule review",- slog.String("project", issue.ProjectID),- slog.Int64("issue_id", issue.UID),- log.BBError(err))- return+ started := false+ for _, reviewType := range reviewTypes {+ if reviewType == store.ReviewRunTypeAI {+ aiSetting, err := stores.GetAISettingUncached(ctx, common.GetWorkspaceIDFromContext(ctx))+ if err != nil {+ slog.Warn("failed to get the AI setting to start the AI review",+ slog.String("project", issue.ProjectID),+ slog.Int64("issue_id", issue.UID),+ log.BBError(err))+ continue+ }+ if aireview.CheckSetting(aiSetting) != nil {+ continue+ }+ }+ if _, err := stores.CreateReviewRun(ctx, issue.ProjectID, issue.UID, reviewType); err != nil {+ slog.Error("failed to start the review",+ slog.String("project", issue.ProjectID),+ slog.Int64("issue_id", issue.UID),+ slog.String("type", reviewType),+ log.BBError(err))+ continue+ }+ started = true+ }+ if started {+ b.ReviewRunTickleChan <- 0}- b.ReviewRunTickleChan <- 0}// requestRolloutCheck asks the rollout creator to look at the issue's plandiff --git a/backend/api/v1/issue_service_review_run_test.go b/backend/api/v1/issue_service_review_run_test.goindex 877dfe0e19..698212554d 100644--- a/backend/api/v1/issue_service_review_run_test.go+++ b/backend/api/v1/issue_service_review_run_test.go@@ -260,33 +260,166 @@ func TestRunReviewRefusesRuleReviewWithoutSQLReviewV2(t *testing.T) {require.Equal(t, v1pb.ReviewRun_AVAILABLE, resp.Msg.Status)}-func TestStartRuleReview(t *testing.T) {+func TestStartReviews(t *testing.T) {t.Parallel()ctx := issueServiceTestContext()stores := setupIssueServiceTestStore(ctx, t)b, err := bus.New()require.NoError(t, err)- claimRuleRuns := func() []*store.ClaimedReviewRun {+ claimedTypes := func() []string {t.Helper()claimed, err := stores.ClaimAvailableReviewRuns(ctx, "replica")require.NoError(t, err)- return claimed+ var types []string+ for _, c := range claimed {+ types = append(types, c.Type)+ }+ return types}+ sqlReviewV2 := &config.Profile{SQLReviewV2: true}+ bothReviews := []string{store.ReviewRunTypeRule, store.ReviewRunTypeAI}issue := createReviewRunIssue(ctx, t, stores, "project-a")- startRuleReview(ctx, &config.Profile{}, stores, b, issue)- require.Empty(t, claimRuleRuns(), "SQL Review V2 is off")+ startReviews(ctx, &config.Profile{}, stores, b, issue, bothReviews...)+ require.Empty(t, claimedTypes(), "SQL Review V2 is off")- startRuleReview(ctx, &config.Profile{SQLReviewV2: true}, stores, b, issue)- claimed := claimRuleRuns()- require.Len(t, claimed, 1)- require.Equal(t, issue.UID, claimed[0].IssueUID)- require.Equal(t, store.ReviewRunTypeRule, claimed[0].Type)+ startReviews(ctx, sqlReviewV2, stores, b, issue, bothReviews...)+ require.Equal(t, []string{store.ReviewRunTypeRule}, claimedTypes(), "the AI review is switched off")require.Len(t, b.ReviewRunTickleChan, 1)+ _, err = stores.UpsertSetting(ctx, &store.SettingMessage{+ Name: storepb.SettingName_AI,+ Workspace: "default",+ Value: &storepb.AISetting{Enabled: true, SqlReviewEnabled: true},+ })+ require.NoError(t, err)+ startReviews(ctx, sqlReviewV2, stores, b, issue, bothReviews...)+ require.ElementsMatch(t, bothReviews, claimedTypes())++ startReviews(ctx, sqlReviewV2, stores, b, issue, store.ReviewRunTypeRule)+ require.Equal(t, []string{store.ReviewRunTypeRule}, claimedTypes(), "only the requested review starts")+issue.Status = storepb.Issue_DONE- startRuleReview(ctx, &config.Profile{SQLReviewV2: true}, stores, b, issue)- require.Empty(t, claimRuleRuns(), "a closed issue is skipped")+ startReviews(ctx, sqlReviewV2, stores, b, issue, bothReviews...)+ require.Empty(t, claimedTypes(), "a closed issue is skipped")+}++// TestAIReviewStartsWhenTheSQLIsSubmittedOrChanged pins the points the AI+// review starts on its own: creating the issue and changing the plan's SQL.+// Rerunning the plan checks leaves it alone.+func TestAIReviewStartsWhenTheSQLIsSubmittedOrChanged(t *testing.T) {+ t.Parallel()+ ctx := issueServiceTestContext()+ stores := setupIssueServiceTestStore(ctx, t)+ projectID := "project-a"+ _, err := stores.CreateInstance(ctx, &store.InstanceMessage{+ ResourceID: "prod",+ Workspace: "default",+ Metadata: &storepb.Instance{+ Engine: storepb.Engine_POSTGRES,+ Activation: true,+ DataSources: []*storepb.DataSource{{Id: "admin", Type: storepb.DataSourceType_ADMIN}},+ },+ })+ require.NoError(t, err)+ _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{+ InstanceID: "prod",+ DatabaseName: "app",+ ProjectID: projectID,+ Metadata: &storepb.DatabaseMetadata{},+ })+ require.NoError(t, err)+ sheets, err := stores.CreateSheets(ctx, projectID,+ &store.SheetMessage{Statement: "ALTER TABLE t ADD COLUMN a INT;"},+ &store.SheetMessage{Statement: "ALTER TABLE t ADD COLUMN b INT;"},+ )+ require.NoError(t, err)+ _, err = stores.UpsertSetting(ctx, &store.SettingMessage{+ Name: storepb.SettingName_AI,+ Workspace: "default",+ Value: &storepb.AISetting{Enabled: true, SqlReviewEnabled: true},+ })+ require.NoError(t, err)+ plan, err := stores.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: projectID,+ Name: "plan",+ Config: &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{+ Id: "spec-1",+ Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{+ Targets: []string{"instances/prod/databases/app"},+ SheetSha256: sheets[0].Sha256,+ },+ },+ }}},+ }, "[email protected]")+ require.NoError(t, err)++ issueService := newIssueServiceForTest(t, stores)+ iamManager, err := iam.NewManager(stores, nil, false)+ require.NoError(t, err)+ planService := NewPlanService(stores, issueService.bus, iamManager, webhook.NewManager(stores, nil), nil, &config.Profile{SQLReviewV2: true})+ getRun := func(issueUID int64, reviewType string) *store.ReviewRunMessage {+ t.Helper()+ run, err := stores.GetReviewRun(ctx, projectID, issueUID, reviewType)+ require.NoError(t, err)+ require.NotNil(t, run, "%s review run", reviewType)+ return run+ }+ claimAll := func() {+ t.Helper()+ _, err := stores.ClaimAvailableReviewRuns(ctx, "replica")+ require.NoError(t, err)+ }++ created, err := issueService.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{+ Parent: "projects/" + projectID,+ Issue: &v1pb.Issue{+ Type: v1pb.Issue_DATABASE_CHANGE,+ Plan: common.FormatPlan(projectID, plan.UID),+ },+ }))+ require.NoError(t, err)+ _, issueUID, err := common.GetProjectIDIssueUID(created.Msg.Name)+ require.NoError(t, err)+ aiRun := getRun(issueUID, store.ReviewRunTypeAI)+ require.Equal(t, storepb.ReviewRun_AVAILABLE, aiRun.Status)+ require.Equal(t, int64(0), aiRun.Attempt)+ claimAll()++ _, err = planService.UpdatePlan(ctx, connect.NewRequest(&v1pb.UpdatePlanRequest{+ Plan: &v1pb.Plan{+ Name: common.FormatPlan(projectID, plan.UID),+ Specs: []*v1pb.Plan_Spec{{+ Id: "spec-1",+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{+ Targets: []string{"instances/prod/databases/app"},+ Sheet: common.FormatSheet(projectID, sheets[1].Sha256),+ },+ },+ }},+ },+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"specs"}},+ }))+ require.NoError(t, err)+ updatedPlan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: projectID, UID: &plan.UID})+ require.NoError(t, err)+ aiRun = getRun(issueUID, store.ReviewRunTypeAI)+ require.Equal(t, storepb.ReviewRun_AVAILABLE, aiRun.Status, "changing the SQL restarts the AI review")+ require.Equal(t, int64(1), aiRun.Attempt)+ require.Equal(t, updatedPlan.Config.GetApprovalInputVersion(), aiRun.Payload.GetApprovalInputVersion(), "the run reviews the new SQL")+ claimAll()++ ruleAttempt := getRun(issueUID, store.ReviewRunTypeRule).Attempt+ _, err = planService.RunPlanChecks(ctx, connect.NewRequest(&v1pb.RunPlanChecksRequest{+ Name: common.FormatPlan(projectID, plan.UID),+ }))+ require.NoError(t, err)+ require.Equal(t, ruleAttempt+1, getRun(issueUID, store.ReviewRunTypeRule).Attempt, "rerunning the checks reruns the rule review")+ aiRun = getRun(issueUID, store.ReviewRunTypeAI)+ require.Equal(t, storepb.ReviewRun_RUNNING, aiRun.Status, "rerunning the checks leaves the AI review alone")+ require.Equal(t, int64(1), aiRun.Attempt)}func TestGetReviewRunReportsAnOutdatedRun(t *testing.T) {diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.goindex 79b03fc5d0..9f70a756c9 100644--- a/backend/api/v1/plan_service.go+++ b/backend/api/v1/plan_service.go@@ -386,11 +386,11 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[v}}- // The review starts before the plan checks and the approval check are- // signaled, as either can create the rollout: a slot already reset holds it- // until the review of the new SQL is done.+ // The reviews start before the plan checks and the approval check are+ // signaled, as either can create the rollout: a rule slot already reset+ // holds it until the rule review of the new SQL is done.if planCheckRunsTrigger {- startRuleReview(ctx, s.profile, s.store, s.bus, updateResult.Issue)+ startReviews(ctx, s.profile, s.store, s.bus, updateResult.Issue, store.ReviewRunTypeRule, store.ReviewRunTypeAI)}if updatedIssue := updateResult.Issue; updateResult.ApprovalReset && updatedIssue != nil {@@ -453,7 +453,8 @@ func (s *PlanService) GetPlanCheckRun(ctx context.Context, request *connect.Requ}// startRuleReviewOfPlan starts the standard rule review of the plan's issue,-// if it has one.+// if it has one. Rerunning the checks leaves the AI review alone: rerunning it+// costs model calls, so it has its own RunReview.func (s *PlanService) startRuleReviewOfPlan(ctx context.Context, plan *store.PlanMessage) {if !s.profile.SQLReviewV2 {return@@ -470,7 +471,7 @@ func (s *PlanService) startRuleReviewOfPlan(ctx context.Context, plan *store.Plalog.BBError(err))return}- startRuleReview(ctx, s.profile, s.store, s.bus, issue)+ startReviews(ctx, s.profile, s.store, s.bus, issue, store.ReviewRunTypeRule)}// RunPlanChecks runs plan checks for a plan.