From e713776921bfa083d2508829330dbafd3c45d1e7 Mon Sep 17 00:00:00 2001 From: Rahul Iyer Date: Mon, 30 Nov 2026 21:16:35 +0000 Subject: [PATCH] query create: agree to a public query at the team's private limit Amp-Thread-ID: https://ampcode.com/threads/T-01a2147b-3c2e-76b3-aeaf-0aafbe53d879 Co-authored-by: Amp --- cmd/query/create.go | 19 ++++++++++++++++++- cmd/query/create_and_run.go | 2 +- cmd/query/mcp_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/cmd/query/create.go b/cmd/query/create.go index 122f644..030ca1b 100644 --- a/cmd/query/create.go +++ b/cmd/query/create.go @@ -1,7 +1,9 @@ package query import ( + "errors" "fmt" + "strings" "github.com/duneanalytics/cli/cmdutil" "github.com/duneanalytics/cli/output" @@ -48,6 +50,9 @@ func addCreateFlags(cmd *cobra.Command) { cmd.Flags().String("folder-name", "", "save the query into the folder with this name; fails if several folders share it") cmd.Flags().String("parameters", "", parametersHelp) + cmd.Flags().Bool("public-if-private-limit-reached", false, + "if --private is not set, the team's queries are private by default, and its private query limit is used up, "+ + "create the query public instead of failing") cmd.MarkFlagsMutuallyExclusive("folder-id", "folder-name") _ = cmd.MarkFlagRequired("name") _ = cmd.MarkFlagRequired("sql") @@ -58,6 +63,16 @@ const parametersHelp = `query parameters as a JSON array, @path, or @-: ` + `[{"key":"days","type":"number","value":"30"}]; ` + `type is text, number, date, or enum (with "enumOptions"); reference them in SQL as {{key}}` +// privateLimitError replaces the MCP's error at a team's private query limit, +// which tells an AI assistant to ask the user, with the CLI flag that agrees. +func privateLimitError(err error) error { + if err != nil && strings.Contains(err.Error(), "public_if_private_limit_reached") { + return errors.New("the team has used all of its private queries, so this query would be public; " + + "nothing was created. To create it public, run the command again with --public-if-private-limit-reached") + } + return err +} + // createArgs returns the createDuneQuery arguments for the flags of query create // and create-and-run. func createArgs(cmd *cobra.Command) (map[string]any, error) { @@ -67,6 +82,8 @@ func createArgs(cmd *cobra.Command) (map[string]any, error) { "private": "is_private", "folder-id": "folder_id", "folder-name": "folder_name", + + "public-if-private-limit-reached": "public_if_private_limit_reached", }) sql, err := cmdutil.FileFlag(cmd, "sql") if err != nil { @@ -92,7 +109,7 @@ func runCreate(cmd *cobra.Command, _ []string) error { } var resp models.CreateQueryResponse if _, err := cmdutil.CallTool(cmd, "createDuneQuery", args, &resp); err != nil { - return err + return privateLimitError(err) } w := cmd.OutOrStdout() diff --git a/cmd/query/create_and_run.go b/cmd/query/create_and_run.go index aeacc35..8a03009 100644 --- a/cmd/query/create_and_run.go +++ b/cmd/query/create_and_run.go @@ -95,7 +95,7 @@ func runCreateAndRun(cmd *cobra.Command, _ []string) error { } raw, err := cmdutil.CallTool(cmd, "createAndExecuteQuery", args, &res) if err != nil { - return err + return privateLimitError(err) } var errs []error diff --git a/cmd/query/mcp_test.go b/cmd/query/mcp_test.go index 082296e..fc53727 100644 --- a/cmd/query/mcp_test.go +++ b/cmd/query/mcp_test.go @@ -86,6 +86,35 @@ func TestCreateToolError(t *testing.T) { assert.EqualError(t, root.Execute(), "folder_ambiguous: pick a folder_id") } +func TestCreateAtThePrivateLimit(t *testing.T) { + limit := errors.New("The team has used all of its private queries, so this query would be public. Nothing was " + + "created. Tell the user it will be public and ask whether that is OK; only if they agree, create it again " + + "with public_if_private_limit_reached: true.") + for _, tool := range []string{"createDuneQuery", "createAndExecuteQuery"} { + sub := map[string]string{"createDuneQuery": "create", "createAndExecuteQuery": "create-and-run"}[tool] + t.Run(sub, func(t *testing.T) { + // Without the flag, the error names the CLI flag instead of the MCP argument. + fake := &dunemcptest.Fake{Errors: map[string]error{tool: limit}} + root, _ := newMCPRoot(fake) + root.SetArgs([]string{"query", sub, "--name", "T", "--sql", "S"}) + err := root.Execute() + require.Error(t, err) + assert.Contains(t, err.Error(), "run the command again with --public-if-private-limit-reached") + assert.NotContains(t, err.Error(), "public_if_private_limit_reached") + assert.NotContains(t, fake.Last().Args, "public_if_private_limit_reached") + + // With it, the consent is passed on. + fake = &dunemcptest.Fake{Results: map[string]string{tool: createAndRunResult, "createDuneQuery": `{"query_id":1}`}} + root, _ = newMCPRoot(fake) + root.SetArgs([]string{"query", sub, "--name", "T", "--sql", "S", "--public-if-private-limit-reached"}) + require.NoError(t, root.Execute()) + var args map[string]any + require.NoError(t, json.Unmarshal([]byte(fake.Last().Args), &args)) + assert.Equal(t, true, args["public_if_private_limit_reached"]) + }) + } +} + const createAndRunResult = `{ "query":{"query_id":11,"url":"https://dune.com/queries/11"}, "execution":{"execution_id":"01EXEC","state":"COMPLETED","engine_used":"small"},