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(mcp): offer to file a possible write as an issue (#21719)
Vincent Huangad04e189a33b
diff --git a/backend/api/mcp/tool_change.go b/backend/api/mcp/tool_change.goindex f68df7cc9f..1f220e0075 100644--- a/backend/api/mcp/tool_change.go+++ b/backend/api/mcp/tool_change.go@@ -201,32 +201,12 @@ func (s *Server) handleChange(ctx context.Context, req *mcp.CallToolRequest, inp// When input.Project is set, the resolver already filtered by it.project := resolved.project- // Step 4: Create sheet.- sheetName, err := s.createSheet(ctx, project, input.SQL)- if err != nil {- return formatChangeStepError(err, "SHEET_CREATE_FAILED", nil), nil, nil- }-- // Step 5: Create plan.- planName, err := s.createPlan(ctx, project, input.Title, resolved.resourceName, sheetName)- if err != nil {- return formatChangeStepError(err, "PLAN_CREATE_FAILED", map[string]string{"sheet": sheetName}), nil, nil- }-- // Step 6: Run plan checks (hybrid async, 10s budget).- planChecks := s.runPlanChecks(ctx, planName)-- // Step 7: Create issue.- issueName, approvalStatus, err := s.createIssue(ctx, project, input.Title, planName, reason)- if err != nil {- partialRefs := map[string]string{"sheet": sheetName, "plan": planName}- // If plan checks had errors, enforceSqlReview may have blocked issue creation.- suggestion := ""- if planChecks != nil && planChecks.Status == planCheckDone && planChecks.Summary != nil && planChecks.Summary.Error > 0 {- suggestion = "plan checks must pass before issue creation; fix SQL and retry"- }- return formatChangeStepErrorWithHint(err, "ISSUE_CREATE_FAILED", partialRefs, suggestion), nil, nil+ // Steps 4-7: Create the sheet, the plan and the issue.+ filed, failed := s.fileChange(ctx, project, resolved.resourceName, input.SQL, input.Title, reason)+ if failed != nil {+ return failed, nil, nil}+ sheetName, planName, planChecks, issueName, approvalStatus := filed.sheet, filed.plan, filed.planChecks, filed.issue, filed.approvalStatus// Step 8: Determine nextAction from approvalStatus.nextAction := deriveNextAction(approvalStatus)@@ -236,6 +216,7 @@ func (s *Server) handleChange(ctx context.Context, req *mcp.CallToolRequest, inprolloutName := ""rolloutDeferredReason := ""+ var err errorswitch {case !input.CreateRollout:rolloutDeferredReason = rolloutNotRequested@@ -293,6 +274,34 @@ func (s *Server) handleChange(ctx context.Context, req *mcp.CallToolRequest, inp}, output, nil}+type filedChange struct {+ sheet string+ plan string+ planChecks *PlanCheckInfo+ issue string+ approvalStatus string+}++// fileChange files sql against database as a change issue: a sheet, a plan+// whose checks run within the poll budget, and the issue. A step that fails+// returns its error result, which names what the steps before it created.+func (s *Server) fileChange(ctx context.Context, project, database, sql, title, reason string) (*filedChange, *mcp.CallToolResult) {+ sheetName, err := s.createSheet(ctx, project, sql)+ if err != nil {+ return nil, formatChangeStepError(err, "SHEET_CREATE_FAILED", nil)+ }+ planName, err := s.createPlan(ctx, project, title, database, sheetName)+ if err != nil {+ return nil, formatChangeStepError(err, "PLAN_CREATE_FAILED", map[string]string{"sheet": sheetName})+ }+ planChecks := s.runPlanChecks(ctx, planName)+ issueName, approvalStatus, err := s.createIssue(ctx, project, title, planName, reason)+ if err != nil {+ return nil, formatChangeStepError(err, "ISSUE_CREATE_FAILED", map[string]string{"sheet": sheetName, "plan": planName})+ }+ return &filedChange{sheet: sheetName, plan: planName, planChecks: planChecks, issue: issueName, approvalStatus: approvalStatus}, nil+}+// createSheet creates a sheet with base64-encoded SQL content.func (s *Server) createSheet(ctx context.Context, project, sql string) (string, error) {return s.callAPI(ctx, "/bytebase.v1.SheetService/CreateSheet", "create sheets", "bb.sheets.create", map[string]any{@@ -578,30 +587,18 @@ func formatChangeError(err *changeError) *mcp.CallToolResult {// formatChangeStepError formats a step failure into an MCP error result.// It detects permission errors and wraps them appropriately.func formatChangeStepError(err error, defaultCode string, partialRefs map[string]string) *mcp.CallToolResult {- return formatChangeStepErrorWithHint(err, defaultCode, partialRefs, "")-}--// formatChangeStepErrorWithHint is like formatChangeStepError but replaces the-// default suggestion with hint when non-empty, keeping the JSON shape intact.-func formatChangeStepErrorWithHint(err error, defaultCode string, partialRefs map[string]string, hint string) *mcp.CallToolResult {var te *toolErrorif errors.As(err, &te) && te.Code == "PERMISSION_DENIED" {- // A permission denial keeps its code and the server's words; the step's- // hint is about something else.return formatChangeError(&changeError{Code: "PERMISSION_DENIED",Message: te.Message,PartialRefs: partialRefs,})}- suggestion := "check the error and retry"- if hint != "" {- suggestion = hint- }return formatChangeError(&changeError{Code: defaultCode,Message: err.Error(),- Suggestion: suggestion,+ Suggestion: "check the error and retry",PartialRefs: partialRefs,})}diff --git a/backend/api/mcp/tool_change_test.go b/backend/api/mcp/tool_change_test.goindex f278739736..15914c259c 100644--- a/backend/api/mcp/tool_change_test.go+++ b/backend/api/mcp/tool_change_test.go@@ -913,7 +913,7 @@ func TestChange_IssueCreateFailed(t *testing.T) {require.Contains(t, text, "plans/2002") // partialRefs}-func TestChange_IssueCreateFailed_SqlReview(t *testing.T) {+func TestChange_IssueCreateFailed_LabelsRequired(t *testing.T) {mock := newChangeMock(employeeDB())mock.planCheckRunResponse = map[string]any{"status": "DONE",@@ -922,7 +922,7 @@ func TestChange_IssueCreateFailed_SqlReview(t *testing.T) {},}mock.issueStatus = http.StatusBadRequest- mock.issueResponse = map[string]any{"message": "enforceSqlReview: checks failed"}+ mock.issueResponse = map[string]any{"message": "require issue labels"}s := newChangeTestServer(t, mock)result, _, err := s.handleChange(testContext(), nil, ChangeInput{@@ -933,14 +933,11 @@ func TestChange_IssueCreateFailed_SqlReview(t *testing.T) {require.NoError(t, err)require.True(t, result.IsError)- text := result.Content[0].(*mcpsdk.TextContent).Text- require.Contains(t, text, "ISSUE_CREATE_FAILED")- require.Contains(t, text, "plan checks must pass")- // Verify the output is valid JSON (guidance folded into suggestion, not appended as raw text).var parsed changeError- require.NoError(t, json.Unmarshal([]byte(text), &parsed))+ require.NoError(t, json.Unmarshal([]byte(result.Content[0].(*mcpsdk.TextContent).Text), &parsed))require.Equal(t, "ISSUE_CREATE_FAILED", parsed.Code)- require.Contains(t, parsed.Suggestion, "plan checks must pass")+ require.Contains(t, parsed.Message, "require issue labels")+ require.Equal(t, "check the error and retry", parsed.Suggestion, "CreateIssue refuses no issue for its plan checks")}// --- Permission tests ---diff --git a/backend/api/mcp/tool_query.go b/backend/api/mcp/tool_query.goindex eaeaf74a81..05aca5bd42 100644--- a/backend/api/mcp/tool_query.go+++ b/backend/api/mcp/tool_query.go@@ -37,6 +37,8 @@ type QueryInput struct {Schema string `json:"schema,omitempty"`// Container is the CosmosDB container to query.Container string `json:"container,omitempty"`+ // IssueTitle titles the issue when the user files a possible write as one.+ IssueTitle string `json:"issueTitle,omitempty"`// Limit is the maximum number of rows to return (default: 100, max: 1000).Limit int `json:"limit,omitempty"`}@@ -78,14 +80,17 @@ const queryDatabaseDescription = `Execute a SQL query against a Bytebase databas| schema | No | Default schema for unqualified names (Postgres search path). An access grant applies only when this equals the grant's schema || container | No | CosmosDB container (required for CosmosDB). An access grant applies only when this equals the grant's container || limit | No | Max rows (default: 100, max: 1000) |+| issueTitle | No | For a statement that may write: the issue's title if the user chooses Create an issue. The form shows it in that choice. Say what the change does, without data values. Without it, the form does not offer Create an issue |**Examples:**query_database(database="employee_db", statement="SELECT * FROM users LIMIT 10")query_database(database="employee", instance="prod-pg", statement="SELECT count(*) FROM orders")query_database(database="employee", instance="projects/hr/instances/prod-pg", statement="SELECT count(*) FROM orders")+query_database(database="employee_db", statement="UPDATE employee SET active = false WHERE id = 42", issueTitle="Deactivate an employee")**Notes:**- Show the user the statement each answer came from, so they can check it read what they asked about.+- When the user asks for an issue or a review of a change, call propose_database_change instead. It takes no schema, so when the request named one, qualify each name with it.- Masked data: masking replaces a value fully (by default "******"), partly (e.g. "**rn**"), or with a hash. maskedColumns in the result names each masked column and why (not reported for MongoDB, CosmosDB or Elasticsearch). A masked value is a placeholder, not a value anything holds, so never write it back and never filter on it. Any statement containing "******" is refused. When showing a partly masked value, wrap it in backticks: "**rn**" renders as bold Markdown.- Seeing masked values: the user can ask a project owner for a masking exemption, or, if the project allows just-in-time access, request an access grant with Unmask for this exact statement, schema and container. In a Read-write session, file the request with request_access_grant; otherwise the user requests it in the SQL Editor. Once it is approved, find the grant among the user's access grants, then rerun the granted statement unchanged with the grant's schema and container. appliedAccessGrant in the result names the grant that applied.`@@ -114,23 +119,24 @@ func (s *Server) handleQueryDatabase(ctx context.Context, req *mcp.CallToolRequelimit = maxQueryLimit}- // An answer that runs nothing is reported before the database is resolved- // again, which may have changed while the user was asked.- answeredRun, answerState := false, ""+ // An answer that acts on nothing is reported before the database is+ // resolved again, which may have changed while the user was asked.+ choice, answerState := "", ""if canShowConfirmation(req) {if response, state, answered := confirmationAnswer(req); answered {- if result := unrunAnswer(response); result != nil {+ var result *mcp.CallToolResult+ if choice, result = answeredChoice(response); result != nil {return result, nil, nil}- answeredRun, answerState = true, state+ answerState = state}}// Resolve database.resolved, resolveResult := s.resolveTarget(ctx, req, input.Database, input.Instance, input.Project)if resolveResult != nil {- if answeredRun && len(resolveResult.InputRequests) > 0 {- return staleRun("the databases that match changed after the user was asked"), nil, nil+ if choice != "" && len(resolveResult.InputRequests) > 0 {+ return staleAnswer("the databases that match changed after the user was asked"), nil, nil}return resolveResult, nil, nil}@@ -139,17 +145,32 @@ func (s *Server) handleQueryDatabase(ctx context.Context, req *mcp.CallToolReque// now, or at once when the client cannot show the form.confirmation := runConfirmation{database: resolved.resourceName}digest := auth.MCPStatementDigest(input.Statement, resolved.resourceName, input.Schema, input.Container)+ // Create an issue files under the agent's title: one Bytebase generated would+ // pass in a project that requires a manual title, since the server refuses+ // only an empty one. A plan carries no schema, so an issue's rollout would+ // run with the database's default one.+ var issueTitle string+ if input.Schema == "" {+ issueTitle = strings.TrimSpace(input.IssueTitle)+ }+ asked := askedDigest(digest, issueTitle)var confirmed []byteif !canShowConfirmation(req) {confirmed = digest- } else if reply, answered := s.signedReply(ctx, req, confirmation, digest); answered {+ } else if reply, answered := s.signedReply(ctx, req, confirmation, asked); answered {if why := reply.staleReason("the request changed after the user was asked"); why != "" {- return staleRun(why), nil, nil+ return staleAnswer(why), nil, nil+ }+ if choice == runChoiceIssue {+ if issueTitle == "" {+ return noOfferedChoice(), nil, nil+ }+ return s.fileAsIssue(ctx, resolved, input.Statement, issueTitle), nil, nil}confirmed = digest- } else if answeredRun && answerState != "" {+ } else if choice != "" && answerState != "" {// The answer was signed for another database.- return staleRun("the database that matches changed after the user was asked"), nil, nil+ return staleAnswer("the database that matches changed after the user was asked"), nil, nil}// Execute query.@@ -161,7 +182,7 @@ func (s *Server) handleQueryDatabase(ctx context.Context, req *mcp.CallToolReque// A confirmed call that Query still refuses reports the refusal: asking// again in the same call would send a second form.if errors.As(err, &required) && confirmed == nil {- return s.askToRun(ctx, req, confirmation, digest, input.Statement, input.Container, required), nil, nil+ return s.askToRun(ctx, req, confirmation, asked, input.Statement, input.Container, required, issueTitle), nil, nil}if err != nil {var qe *queryErrordiff --git a/backend/api/mcp/tool_query_confirm.go b/backend/api/mcp/tool_query_confirm.goindex 4713e36046..2bb301c952 100644--- a/backend/api/mcp/tool_query_confirm.go+++ b/backend/api/mcp/tool_query_confirm.go@@ -2,6 +2,7 @@ package mcpimport ("context"+ "crypto/sha256""encoding/json""fmt""strings"@@ -24,6 +25,7 @@ import (const (runChoiceField = "choice"runChoiceRun = "run"+ runChoiceIssue = "issue"runChoiceChange = "change")@@ -83,10 +85,22 @@ func confirmationRequiredIn(body json.RawMessage) *confirmationRequired {return nil}-// askToRun returns the form that asks the user to run statement. A call that+// askedDigest is what the form's state binds: the request's digest and the+// issue title, which decides whether the form offers Create an issue and is+// what that choice shows and files. Query checks the request's digest alone,+// since it never sees the title.+func askedDigest(digest []byte, issueTitle string) []byte {+ h := sha256.New()+ _, _ = h.Write(digest)+ _, _ = h.Write([]byte(issueTitle))+ return h.Sum(nil)+}++// askToRun returns the form that asks the user to run statement, and offers to+// file it as an issue titled issueTitle unless issueTitle is empty. A call that// already asked which database to use gets a result that has the agent call// again instead: an older client answers one form per call.-func (s *Server) askToRun(ctx context.Context, req *mcp.CallToolRequest, confirmation runConfirmation, digest []byte, statement, container string, required *confirmationRequired) *mcp.CallToolResult {+func (s *Server) askToRun(ctx context.Context, req *mcp.CallToolRequest, confirmation runConfirmation, digest []byte, statement, container string, required *confirmationRequired, issueTitle string) *mcp.CallToolResult {project, instance, database, err := common.GetDatabaseResourceName(confirmation.database)if err != nil {return formatToolError(err)@@ -113,6 +127,11 @@ func (s *Server) askToRun(ctx context.Context, req *mcp.CallToolRequest, confirm}}message := strings.Join(append(header, request...), "\n")+ choices := []any{map[string]any{"const": runChoiceRun, "title": "Run now"}}+ if issueTitle != "" {+ choices = append(choices, map[string]any{"const": runChoiceIssue, "title": fmt.Sprintf("Create an issue titled %q", issueTitle)})+ }+ choices = append(choices, map[string]any{"const": runChoiceChange, "title": "Change it in chat"})return s.askWithForm(ctx, confirmation, digest, message, map[string]any{"type": "object","properties": map[string]any{@@ -120,11 +139,8 @@ func (s *Server) askToRun(ctx context.Context, req *mcp.CallToolRequest, confirm"type": "string","title": "Run this request?","description": description,- "oneOf": []any{- map[string]any{"const": runChoiceRun, "title": "Run now"},- map[string]any{"const": runChoiceChange, "title": "Change it in chat"},- },- "default": runChoiceRun,+ "oneOf": choices,+ "default": runChoiceRun,},},"required": []string{runChoiceField},@@ -197,34 +213,76 @@ func lineColumns(line string) int {return columns}-// unrunAnswer returns the result for an answer that runs nothing, or nil when-// the user chose Run now. It reads no target or signed state, so a request-// that changed while the user was asked cannot turn a decline into a form.-func unrunAnswer(response mcp.InputResponse) *mcp.CallToolResult {+// answeredChoice returns the choice an answer acts on, Run now or Create an+// issue, or else the result for an answer that acts on nothing. It reads no+// target or signed state, so a request that changed while the user was asked+// cannot turn a decline into a form.+func answeredChoice(response mcp.InputResponse) (string, *mcp.CallToolResult) {content, accepted, why := acceptedForm(response)if !accepted {- return textResult(fmt.Sprintf("Not run: %s, so nothing changed. Do not retry unless the user asks.", why))+ return "", textResult(fmt.Sprintf("Not run: %s, so nothing changed. Do not retry unless the user asks.", why))}switch content[runChoiceField] {case runChoiceRun:- return nil+ return runChoiceRun, nil+ case runChoiceIssue:+ return runChoiceIssue, nilcase runChoiceChange:- return textResult("Not run: the user wants to change this request. Ask them what to change, then call query_database with the revised request.")+ return "", textResult("Not run: the user wants to change this request. Ask them what to change, then call query_database with the revised request.")default:- return textResult("Not run: the answer chose neither option, so nothing changed.")+ return "", noOfferedChoice()}}-// staleRun reports a Run now answer that no longer fits the request. It asks-// nothing: an older client answers one form per call.-func staleRun(why string) *mcp.CallToolResult {+// noOfferedChoice reports an answer that chose none of the form's choices.+func noOfferedChoice() *mcp.CallToolResult {+ return textResult("Not run: the answer chose none of the form's choices, so nothing changed.")+}++// staleAnswer reports a Run now or Create an issue answer that no longer fits+// the request. It asks nothing: an older client answers one form per call.+func staleAnswer(why string) *mcp.CallToolResult {return formatToolError(&toolError{Code: "STALE_CONFIRMATION",- Message: fmt.Sprintf("%s, so nothing ran", why),+ Message: fmt.Sprintf("%s, so nothing ran and no issue was filed", why),Suggestion: "call query_database again to ask the user about the request as it is now",})}+// fileAsIssue files statement against the database as a change issue titled+// title, with propose_database_change's steps but no rollout: Bytebase creates+// the rollout once the issue is approved and its plan checks pass.+func (s *Server) fileAsIssue(ctx context.Context, resolved *resolvedDatabase, statement, title string) *mcp.CallToolResult {+ filed, failed := s.fileChange(ctx, resolved.project, resolved.resourceName, statement, title, "")+ if failed != nil {+ return failed+ }+ return textResult(fmt.Sprintf("Not run: the user chose Create an issue, so the request is filed as issue %s (%s). The issue page shows its approval status.%s",+ filed.issue, s.consoleLink(ctx, filed.issue), planCheckNote(filed.planChecks)))+}++// planCheckNote reports plan checks that may hold the rollout: Bytebase creates+// it on its own only once they are done with no error.+func planCheckNote(checks *PlanCheckInfo) string {+ if checks != nil && checks.Status == planCheckRunning {+ return " Its plan checks are still running; the issue page shows their result."+ }+ if !planChecksHaveErrors(checks) {+ return ""+ }+ var errs []string+ for _, result := range checks.Results {+ if result.Type == "ERROR" {+ errs = append(errs, result.Message)+ }+ }+ note := " Its plan checks did not pass, so Bytebase will not create its rollout on its own"+ if len(errs) > 0 {+ note += ": " + strings.Join(errs, "; ")+ }+ return note + "."+}+// textResult carries text alone, which a client shows the model whole.func textResult(text string) *mcp.CallToolResult {return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: text}}}diff --git a/backend/api/mcp/tool_query_confirm_test.go b/backend/api/mcp/tool_query_confirm_test.goindex 9f377be6dd..96085a07bf 100644--- a/backend/api/mcp/tool_query_confirm_test.go+++ b/backend/api/mcp/tool_query_confirm_test.go@@ -3,6 +3,7 @@ package mcpimport ("bytes""context"+ "encoding/base64""encoding/json""fmt""net/http"@@ -39,8 +40,10 @@ type confirmingQueryMock struct {environment string// refusesConfirmed has Query refuse an INSERT even when it is confirmed.refusesConfirmed bool- mu sync.Mutex- queries []queryCall+ // changes, when set, serves the calls that file a change.+ changes *changeMock+ mu sync.Mutex+ queries []queryCall}func newConfirmingQueryMock(databases []map[string]any) *confirmingQueryMock {@@ -48,6 +51,10 @@ func newConfirmingQueryMock(databases []map[string]any) *confirmingQueryMock {}func (m *confirmingQueryMock) ServeHTTP(w http.ResponseWriter, r *http.Request) {+ if m.changes != nil && changeCall(r.URL.Path) {+ m.changes.handler().ServeHTTP(w, r)+ return+ }if !strings.HasSuffix(r.URL.Path, "SQLService/Query") {m.mu.Lock()databases := m.databases@@ -75,6 +82,12 @@ func (m *confirmingQueryMock) ServeHTTP(w http.ResponseWriter, r *http.Request)_ = json.NewEncoder(w).Encode(makeQueryResponse([]string{"id"}, []string{"int4"}, [][]any{{"1"}}, "0.001s"))}+// changeCall reports whether path is a call that files a change.+func changeCall(path string) bool {+ return strings.Contains(path, "SheetService/") || strings.Contains(path, "PlanService/") ||+ strings.Contains(path, "IssueService/") || strings.Contains(path, "RolloutService/")+}+// setDatabases changes the databases the list calls return.func (m *confirmingQueryMock) setDatabases(databases []map[string]any) {m.mu.Lock()@@ -124,8 +137,12 @@ func runAnswer(choice any) *mcp.ElicitResult {return &mcp.ElicitResult{Action: "accept", Content: map[string]any{runChoiceField: choice}}}+// agentIssueTitle is the title the agent sends with a request it may file as+// an issue.+const agentIssueTitle = "Add an employee"+func queryParams(statement string) *mcp.CallToolParams {- return &mcp.CallToolParams{Name: "query_database", Arguments: map[string]any{"database": "employee_db", "statement": statement}}+ return &mcp.CallToolParams{Name: "query_database", Arguments: map[string]any{"database": "employee_db", "statement": statement, "issueTitle": agentIssueTitle}}}func callQuery(t *testing.T, session *mcp.ClientSession, params *mcp.CallToolParams) *mcp.CallToolResult {@@ -138,6 +155,10 @@ func callQuery(t *testing.T, session *mcp.ClientSession, params *mcp.CallToolPar}func TestQueryDatabaseAsksBeforeAWrite(t *testing.T) {+ const (+ withIssue = `"oneOf":[{"const":"run","title":"Run now"},{"const":"issue","title":"Create an issue titled \"Add an employee\""},{"const":"change","title":"Change it in chat"}]`+ withoutIssue = `"oneOf":[{"const":"run","title":"Run now"},{"const":"change","title":"Change it in chat"}]`+ )for _, row := range []struct {name stringdatabases []map[string]any@@ -145,18 +166,19 @@ func TestQueryDatabaseAsksBeforeAWrite(t *testing.T) {schema stringcontainer stringheader string+ choices string}{{name: "the database's default schema", databases: employeeDatabase(), environment: "prod",- header: `Run in environment "prod" on database "employee_db",` + "\n" + `instance "prod-pg":`},+ header: `Run in environment "prod" on database "employee_db",` + "\n" + `instance "prod-pg":`, choices: withIssue},{name: "a named schema", databases: employeeDatabase(), environment: "prod", schema: "app",- header: `Run in environment "prod" on database "employee_db",` + "\n" + `instance "prod-pg", schema "app":`},+ header: `Run in environment "prod" on database "employee_db",` + "\n" + `instance "prod-pg", schema "app":`, choices: withoutIssue},{name: "a project instance", databases: []map[string]any{makeDatabase("projects/hr/instances/prod-pg/databases/employee_db", "projects/hr/instances/prod-pg", "projects/hr", "POSTGRES", "ds-1"),- }, environment: "prod", header: `Run in environment "prod" on database "employee_db",` + "\n" + `instance "prod-pg":`},+ }, environment: "prod", header: `Run in environment "prod" on database "employee_db",` + "\n" + `instance "prod-pg":`, choices: withIssue},{name: "no environment", databases: employeeDatabase(),- header: `Run on database "employee_db",` + "\n" + `instance "prod-pg":`},- {name: "a CosmosDB container", databases: employeeDatabase(), environment: "prod", container: "orders",- header: `Run in environment "prod" on database "employee_db",` + "\n" + `instance "prod-pg", container "orders":`},+ header: `Run on database "employee_db",` + "\n" + `instance "prod-pg":`, choices: withIssue},+ {name: "a container", databases: employeeDatabase(), environment: "prod", container: "orders",+ header: `Run in environment "prod" on database "employee_db",` + "\n" + `instance "prod-pg", container "orders":`, choices: withIssue},} {t.Run(row.name, func(t *testing.T) {mock := newConfirmingQueryMock(row.databases)@@ -166,7 +188,7 @@ func TestQueryDatabaseAsksBeforeAWrite(t *testing.T) {session := connectLegacy(t, endpoint, token, &mcp.ClientOptions{ElicitationHandler: user.handle})const insert = "INSERT INTO employee VALUES (1)"- arguments := map[string]any{"database": "employee_db", "statement": insert}+ arguments := map[string]any{"database": "employee_db", "statement": insert, "issueTitle": agentIssueTitle}if row.schema != "" {arguments["schema"] = row.schema}@@ -181,7 +203,7 @@ func TestQueryDatabaseAsksBeforeAWrite(t *testing.T) {schema, err := json.Marshal(asked.RequestedSchema)require.NoError(t, err)require.Contains(t, string(schema), `"default":"run"`)- require.Contains(t, string(schema), `"oneOf":[{"const":"run","title":"Run now"},{"const":"change","title":"Change it in chat"}]`)+ require.Contains(t, string(schema), row.choices)require.Contains(t, string(schema), `"description":"Run now runs it at once, with no issue or approval."`)require.Equal(t, []queryCall{{Statement: insert}, {Statement: insert, Confirmed: true}}, mock.calls())})@@ -412,9 +434,12 @@ func TestQueryDatabaseReportsATargetThatChangedWhileAsked(t *testing.T) {{name: "another match, cancel", databases: another, answer: &mcp.ElicitResult{Action: "cancel"}, text: "Not run: the user cancelled"},{name: "a different database, Run now", databases: moved, answer: runAnswer(runChoiceRun), text: "STALE_CONFIRMATION"},{name: "a different database, cancel", databases: moved, answer: &mcp.ElicitResult{Action: "cancel"}, text: "Not run: the user cancelled"},+ {name: "another match, Create an issue", databases: another, answer: runAnswer(runChoiceIssue), text: "STALE_CONFIRMATION"},+ {name: "a different database, Create an issue", databases: moved, answer: runAnswer(runChoiceIssue), text: "STALE_CONFIRMATION"},} {t.Run(row.name, func(t *testing.T) {mock := newConfirmingQueryMock(employeeDatabase())+ mock.changes = newChangeMock(nil)_, endpoint, token := newQueryConfirmServer(t, mock)user := &asker{answer: row.answer, whileAsked: func() { mock.setDatabases(row.databases) }}session := connectLegacy(t, endpoint, token, &mcp.ClientOptions{ElicitationHandler: user.handle})@@ -426,6 +451,37 @@ func TestQueryDatabaseReportsATargetThatChangedWhileAsked(t *testing.T) {for _, query := range mock.calls() {require.False(t, query.Confirmed)}+ require.Nil(t, mock.changes.getCapturedSheet(), "a stale answer files nothing")+ })+ }+}++func TestQueryDatabaseReportsATitleThatChangedWhileAsked(t *testing.T) {+ for _, row := range []struct {+ name string+ asked, answer string+ }{+ {name: "a title added after a form without Create an issue", answer: agentIssueTitle},+ {name: "another title", asked: agentIssueTitle, answer: "Remove an employee"},+ } {+ t.Run(row.name, func(t *testing.T) {+ mock := newConfirmingQueryMock(employeeDatabase())+ mock.changes = newChangeMock(nil)+ s, _, token := newQueryConfirmServer(t, mock)+ session := connectMRTR(t, mrtrEndpoint(t, s), token)+ const insert = "INSERT INTO employee VALUES (1)"+ params := &mcp.CallToolParams{Name: "query_database", Arguments: map[string]any{"database": "employee_db", "statement": insert, "issueTitle": row.asked}}++ first := callQuery(t, session, params)+ require.True(t, first.NeedsInput())+ params.Arguments = map[string]any{"database": "employee_db", "statement": insert, "issueTitle": row.answer}+ params.InputResponses = mcp.InputResponseMap{confirmDecisionRequestID: runAnswer(runChoiceIssue)}+ params.RequestState = first.RequestState+ res := callQuery(t, session, params)+ require.True(t, res.IsError)+ require.Contains(t, resultText(t, res), "STALE_CONFIRMATION")+ require.Nil(t, mock.changes.getCapturedSheet())+ require.Equal(t, []queryCall{{Statement: insert}}, mock.calls())})}}@@ -454,13 +510,13 @@ func TestQueryDatabaseConfirmationUnderMRTRClient(t *testing.T) {require.True(t, changed.IsError)require.Contains(t, resultText(t, changed), "STALE_CONFIRMATION")- moved := &mcp.CallToolParams{Name: "query_database", Arguments: map[string]any{"database": "employee_db", "statement": insert, "container": "orders"}}+ moved := &mcp.CallToolParams{Name: "query_database", Arguments: map[string]any{"database": "employee_db", "statement": insert, "issueTitle": agentIssueTitle, "container": "orders"}}moved.InputResponses = runmoved.RequestState = first.RequestStaterequire.Contains(t, resultText(t, callQuery(t, session, moved)), "STALE_CONFIRMATION", "a confirmation is for one container")unknown := call(insert, mcp.InputResponseMap{confirmDecisionRequestID: runAnswer("skip")}, first.RequestState)- require.Equal(t, "Not run: the answer chose neither option, so nothing changed.", resultText(t, unknown))+ require.Equal(t, "Not run: the answer chose none of the form's choices, so nothing changed.", resultText(t, unknown))ran := call(insert, run, first.RequestState)require.False(t, ran.IsError, resultText(t, ran))@@ -473,12 +529,13 @@ func TestQueryDatabaseConfirmationUnderMRTRClient(t *testing.T) {func TestQueryDatabaseReportsAnAnswerThatRunsNothingBeforeAStaleOne(t *testing.T) {mock := newConfirmingQueryMock(employeeDatabase())+ mock.changes = newChangeMock(nil)s, _, token := newQueryConfirmServer(t, mock)session := connectMRTR(t, mrtrEndpoint(t, s), token)const insert = "INSERT INTO employee VALUES (1)"const database = "instances/prod-pg/databases/employee_db"alice := withOAuth2ClientID(withUserEmail(withWorkspaceID(context.Background(), "ws-test"), "[email protected]"), "client-A")- old := s.signConfirmationState(alice, runConfirmation{database: database}, auth.MCPStatementDigest(insert, database, "", ""), time.Now().Add(-11*time.Minute))+ old := s.signConfirmationState(alice, runConfirmation{database: database}, askedDigest(auth.MCPStatementDigest(insert, database, "", ""), agentIssueTitle), time.Now().Add(-11*time.Minute))call := func(answer *mcp.ElicitResult) *mcp.CallToolResult {params := queryParams(insert)params.InputResponses = mcp.InputResponseMap{confirmDecisionRequestID: answer}@@ -496,7 +553,146 @@ func TestQueryDatabaseReportsAnAnswerThatRunsNothingBeforeAStaleOne(t *testing.Trequire.True(t, ran.IsError)require.Contains(t, resultText(t, ran), "STALE_CONFIRMATION")require.Contains(t, resultText(t, ran), "the user was asked more than 10 minutes ago")+ filed := call(runAnswer(runChoiceIssue))+ require.True(t, filed.IsError)+ require.Contains(t, resultText(t, filed), "the user was asked more than 10 minutes ago, so nothing ran and no issue was filed")require.Empty(t, mock.calls())+ require.Nil(t, mock.changes.getCapturedSheet())+}++func TestQueryDatabaseFilesAnIssue(t *testing.T) {+ mock := newConfirmingQueryMock(employeeDatabase())+ mock.changes = newChangeMock(nil)+ mock.changes.issueResponse = defaultIssueResponse("CHECKING")+ s, endpoint, token := newQueryConfirmServer(t, mock)+ s.planCheckPollBudgetOverride = 100 * time.Millisecond+ user := &asker{answer: runAnswer(runChoiceIssue)}+ session := connectLegacy(t, endpoint, token, &mcp.ClientOptions{ElicitationHandler: user.handle})++ const insert = "INSERT INTO employee VALUES (1)"+ res := callQuery(t, session, queryParams(insert))+ require.False(t, res.IsError, resultText(t, res))+ require.Equal(t, "Not run: the user chose Create an issue, so the request is filed as issue projects/hr-system/issues/3003 "++ "(https://bb.example.com/projects/hr-system/issues/3003). The issue page shows its approval status.", resultText(t, res))+ require.Nil(t, res.StructuredContent, "a client shows text alone to the model whole")+ require.Equal(t, []queryCall{{Statement: insert}}, mock.calls(), "filing an issue runs nothing")++ sheet, ok := mock.changes.getCapturedSheet()["sheet"].(map[string]any)+ require.True(t, ok)+ require.Equal(t, base64.StdEncoding.EncodeToString([]byte(insert)), sheet["content"])+ plan := mock.changes.getCapturedPlan()+ require.Equal(t, "projects/hr", plan["parent"])+ planObj, ok := plan["plan"].(map[string]any)+ require.True(t, ok)+ specs, ok := planObj["specs"].([]any)+ require.True(t, ok)+ spec, ok := specs[0].(map[string]any)+ require.True(t, ok)+ config, ok := spec["changeDatabaseConfig"].(map[string]any)+ require.True(t, ok)+ require.Equal(t, []any{"instances/prod-pg/databases/employee_db"}, config["targets"])+ issue, ok := mock.changes.getCapturedIssue()["issue"].(map[string]any)+ require.True(t, ok)+ require.Equal(t, agentIssueTitle, issue["title"])+ require.Equal(t, planObj["title"], issue["title"])+ require.NotContains(t, issue, "description")+ require.Nil(t, mock.changes.getCapturedRollout(), "Bytebase creates the rollout once the approval flow is done")+}++func TestQueryDatabaseReportsPlanChecksThatHoldTheRollout(t *testing.T) {+ const filed = "Not run: the user chose Create an issue, so the request is filed as issue projects/hr-system/issues/3003 " ++ "(https://bb.example.com/projects/hr-system/issues/3003). The issue page shows its approval status."+ warning := map[string]any{"status": "WARNING", "title": "The statement affects many rows"}+ for _, row := range []struct {+ name string+ run map[string]any+ text string+ }{+ {name: "an error", run: map[string]any{"status": "DONE", "results": []any{+ map[string]any{"status": "ERROR", "title": "UPDATE requires a WHERE clause"}, warning,+ }}, text: filed + " Its plan checks did not pass, so Bytebase will not create its rollout on its own: UPDATE requires a WHERE clause."},+ {name: "a failed run", run: map[string]any{"status": "FAILED"},+ text: filed + " Its plan checks did not pass, so Bytebase will not create its rollout on its own."},+ {name: "a warning", run: map[string]any{"status": "DONE", "results": []any{warning}}, text: filed},+ {name: "checks still running", run: map[string]any{"status": "RUNNING"},+ text: filed + " Its plan checks are still running; the issue page shows their result."},+ } {+ t.Run(row.name, func(t *testing.T) {+ mock := newConfirmingQueryMock(employeeDatabase())+ mock.changes = newChangeMock(nil)+ mock.changes.planCheckRunResponse = row.run+ mock.changes.issueResponse = defaultIssueResponse("CHECKING")+ s, endpoint, token := newQueryConfirmServer(t, mock)+ s.planCheckPollBudgetOverride = 100 * time.Millisecond+ user := &asker{answer: runAnswer(runChoiceIssue)}+ session := connectLegacy(t, endpoint, token, &mcp.ClientOptions{ElicitationHandler: user.handle})++ res := callQuery(t, session, queryParams("INSERT INTO employee VALUES (1)"))+ require.False(t, res.IsError, resultText(t, res))+ require.Equal(t, row.text, resultText(t, res))+ })+ }+}++func TestQueryDatabaseOffersAnIssueOnlyWithATitleAndNoSchema(t *testing.T) {+ for _, row := range []struct {+ name string+ arguments map[string]any+ offered bool+ }{+ {name: "a title and the database's default schema", arguments: map[string]any{"issueTitle": agentIssueTitle}, offered: true},+ {name: "a named schema", arguments: map[string]any{"issueTitle": agentIssueTitle, "schema": "app"}},+ {name: "no title", arguments: map[string]any{}},+ {name: "a blank title", arguments: map[string]any{"issueTitle": " "}},+ } {+ t.Run(row.name, func(t *testing.T) {+ mock := newConfirmingQueryMock(employeeDatabase())+ mock.changes = newChangeMock(nil)+ s, _, token := newQueryConfirmServer(t, mock)+ s.planCheckPollBudgetOverride = 100 * time.Millisecond+ session := connectMRTR(t, mrtrEndpoint(t, s), token)+ params := &mcp.CallToolParams{Name: "query_database", Arguments: row.arguments}+ row.arguments["database"] = "employee_db"+ row.arguments["statement"] = "INSERT INTO employee VALUES (1)"++ first := callQuery(t, session, params)+ require.True(t, first.NeedsInput())+ form, err := json.Marshal(first.InputRequests[confirmDecisionRequestID])+ require.NoError(t, err)+ require.Equal(t, row.offered, strings.Contains(string(form), `"const":"issue"`))++ params.InputResponses = mcp.InputResponseMap{confirmDecisionRequestID: runAnswer(runChoiceIssue)}+ params.RequestState = first.RequestState+ res := callQuery(t, session, params)+ if row.offered {+ require.Contains(t, resultText(t, res), "Not run: the user chose Create an issue")+ require.NotNil(t, mock.changes.getCapturedIssue())+ return+ }+ require.Equal(t, "Not run: the answer chose none of the form's choices, so nothing changed.", resultText(t, res))+ require.Nil(t, mock.changes.getCapturedSheet())+ })+ }+}++func TestQueryDatabaseReportsAnIssueThatCouldNotBeFiled(t *testing.T) {+ mock := newConfirmingQueryMock(employeeDatabase())+ mock.changes = newChangeMock(nil)+ mock.changes.issueStatus = http.StatusForbidden+ mock.changes.issueResponse = map[string]any{"message": "permission denied: bb.issues.create"}+ s, endpoint, token := newQueryConfirmServer(t, mock)+ s.planCheckPollBudgetOverride = 100 * time.Millisecond+ user := &asker{answer: runAnswer(runChoiceIssue)}+ session := connectLegacy(t, endpoint, token, &mcp.ClientOptions{ElicitationHandler: user.handle})++ const insert = "INSERT INTO employee VALUES (1)"+ res := callQuery(t, session, queryParams(insert))+ require.True(t, res.IsError)+ text := resultText(t, res)+ require.Contains(t, text, `"code": "PERMISSION_DENIED"`)+ require.Contains(t, text, `"plan": "projects/hr-system/plans/2002"`, "the result names what the earlier steps created")+ require.Equal(t, []queryCall{{Statement: insert}}, mock.calls())+ require.Nil(t, mock.changes.getCapturedRollout())}func TestConfirmationRequiredIn(t *testing.T) {