From 36a493c49e792c3e9e583e3d7e2dcc5e3a2f917d Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:44:16 +0000 Subject: [PATCH 01/20] Add `databricks feedback` command Adds a CLI-wide command that sends feedback as a fast_feedback_log event to /telemetry-ext of the authenticated workspace. --command names the command the feedback is about and selects the owning component; the agent and run ID ride in the standard user agent. Feedback written by a detected AI agent requires the user's confirmation. The upload is a single, bounded attempt so ambiguous failures never file the same feedback twice. Commands can opt into building the full command tree so that --command resolves nested commands. Co-authored-by: Isaac --- .nextchanges/cli/feedback.md | 1 + .../cmd/feedback/broken-bundle/databricks.yml | 10 + acceptance/cmd/feedback/out.test.toml | 1 + acceptance/cmd/feedback/output.txt | 174 +++++++++++++++ acceptance/cmd/feedback/script | 40 ++++ acceptance/cmd/feedback/test.toml | 1 + acceptance/help/output.txt | 1 + cmd/cmd.go | 2 + cmd/feedback/feedback.go | 201 ++++++++++++++++++ cmd/feedback/feedback_test.go | 136 ++++++++++++ cmd/root/command_spec.go | 4 + cmd/root/command_spec_test.go | 6 +- cmd/root/root.go | 2 +- cmd/root/tree.go | 8 +- cmd/root/tree_test.go | 22 ++ cmd/root/user_agent_command.go | 7 +- cmd/root/user_agent_command_test.go | 6 +- libs/aitools/agents/hint.go | 1 + libs/aitools/agents/hint_test.go | 1 + libs/telemetry/logger.go | 64 +++++- libs/telemetry/logger_test.go | 97 +++++++++ libs/telemetry/protos/README.md | 6 + libs/telemetry/protos/fast_feedback_log.go | 26 +++ libs/telemetry/protos/frontend_log.go | 5 +- 24 files changed, 802 insertions(+), 20 deletions(-) create mode 100644 .nextchanges/cli/feedback.md create mode 100644 acceptance/cmd/feedback/broken-bundle/databricks.yml create mode 100644 acceptance/cmd/feedback/out.test.toml create mode 100644 acceptance/cmd/feedback/output.txt create mode 100644 acceptance/cmd/feedback/script create mode 100644 acceptance/cmd/feedback/test.toml create mode 100644 cmd/feedback/feedback.go create mode 100644 cmd/feedback/feedback_test.go create mode 100644 libs/telemetry/protos/fast_feedback_log.go diff --git a/.nextchanges/cli/feedback.md b/.nextchanges/cli/feedback.md new file mode 100644 index 00000000000..293ae5f1821 --- /dev/null +++ b/.nextchanges/cli/feedback.md @@ -0,0 +1 @@ +* Add the `databricks feedback` command to send feedback about the CLI to Databricks. ([#7567](https://github.com/databricks/cli/pull/7567)) diff --git a/acceptance/cmd/feedback/broken-bundle/databricks.yml b/acceptance/cmd/feedback/broken-bundle/databricks.yml new file mode 100644 index 00000000000..c8138b9f3cb --- /dev/null +++ b/acceptance/cmd/feedback/broken-bundle/databricks.yml @@ -0,0 +1,10 @@ +bundle: + name: broken-bundle + +# Two targets both marked default is an invalid bundle. Feedback does not use +# the bundle, so this error must not stop it. +targets: + dev: + default: true + dev2: + default: true diff --git a/acceptance/cmd/feedback/out.test.toml b/acceptance/cmd/feedback/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/feedback/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt new file mode 100644 index 00000000000..4a86f4c5eeb --- /dev/null +++ b/acceptance/cmd/feedback/output.txt @@ -0,0 +1,174 @@ + +=== Feedback about a product command is routed to its team + +>>> [CLI] feedback --command air run --run-id 123 --can-be-contacted logs omitted the failure reason +Thank you! Your feedback was sent. Reference: [UUID][0] + +>>> print_requests.py //telemetry-ext +{ + "frontend_log_event_id": "[UUID][0]", + "entry": { + "fast_feedback_log": { + "reference_event_id": "[UUID][1]", + "parent_ui": "ModelTraining.AIRuntime", + "origin": "air_run", + "can_be_contacted": true, + "ui_versions": "cli:[CLI_VERSION]", + "user_agent": "cli/[CLI_VERSION] cmd/feedback run-id/123", + "text": "logs omitted the failure reason" + } + } +} + +=== A command group, with a JSON receipt + +>>> [CLI] feedback --command bundle deploy should print which resources changed -o json +{ + "received": true, + "feedback_id": "[UUID][2]" +} + +>>> print_requests.py //telemetry-ext +{ + "frontend_log_event_id": "[UUID][2]", + "entry": { + "fast_feedback_log": { + "reference_event_id": "[UUID][3]", + "parent_ui": "DeveloperEcosystem.CoreDabs", + "origin": "bundle", + "can_be_contacted": false, + "ui_versions": "cli:[CLI_VERSION]", + "user_agent": "cli/[CLI_VERSION] cmd/feedback", + "text": "deploy should print which resources changed" + } + } +} + +=== Feedback can name a nested command + +>>> [CLI] feedback --command bundle deploy deploy is slow +Thank you! Your feedback was sent. Reference: [UUID][4] + +>>> print_requests.py //telemetry-ext +{ + "frontend_log_event_id": "[UUID][4]", + "entry": { + "fast_feedback_log": { + "reference_event_id": "[UUID][5]", + "parent_ui": "DeveloperEcosystem.CoreDabs", + "origin": "bundle_deploy", + "can_be_contacted": false, + "ui_versions": "cli:[CLI_VERSION]", + "user_agent": "cli/[CLI_VERSION] cmd/feedback", + "text": "deploy is slow" + } + } +} + +=== Validation errors send nothing + +>>> [CLI] feedback +Error: feedback message must contain 1 to 10000 characters + +>>> [CLI] feedback +Error: accepts 1 arg(s), received 0 + +Usage: + databricks feedback MESSAGE [flags] + +Examples: + databricks feedback --command "bundle deploy" "deploy should print which resources changed" + databricks feedback --command "air run" --run-id 123 "logs omitted the failure reason" + +Flags: + --can-be-contacted[=true|false] Allow Databricks to contact you about this feedback + --command string CLI command the feedback is about, for example "bundle deploy" + -h, --help[=true|false] help for feedback + --run-id string ID of the job run the feedback is about + -y, --yes[=true|false] Confirm sending feedback written by an AI agent + +Global Flags: + --debug[=true|false] enable debug logging + -o, --output type output type: text or json (default text) + -p, --profile string ~/.databrickscfg profile + -t, --target string bundle target to use (if applicable) + + +>>> [CLI] feedback --command no-such-command hello +Error: unknown command "no-such-command" + +>>> [CLI] feedback --command bundle no-such-command hello +Error: unknown command "bundle no-such-command" + +>>> [CLI] feedback --run-id abc hello +Error: --run-id must be a positive job run ID + +=== General CLI feedback + +>>> [CLI] feedback the CLI should support ... +Thank you! Your feedback was sent. Reference: [UUID][6] + +>>> print_requests.py //telemetry-ext +{ + "frontend_log_event_id": "[UUID][6]", + "entry": { + "fast_feedback_log": { + "reference_event_id": "[UUID][7]", + "parent_ui": "DeveloperEcosystem.DatabricksCLI", + "origin": "cli", + "can_be_contacted": false, + "ui_versions": "cli:[CLI_VERSION]", + "user_agent": "cli/[CLI_VERSION] cmd/feedback", + "text": "the CLI should support ..." + } + } +} + +=== An AI agent needs the user's confirmation + +>>> AGENT=test-agent [CLI] feedback hello +Error: feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message + +Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it + +=== An AI agent with --yes + +>>> AGENT=test-agent [CLI] feedback --yes hello +Thank you! Your feedback was sent. Reference: [UUID][8] + +>>> print_requests.py //telemetry-ext +{ + "frontend_log_event_id": "[UUID][8]", + "entry": { + "fast_feedback_log": { + "reference_event_id": "[UUID][9]", + "parent_ui": "DeveloperEcosystem.DatabricksCLI", + "origin": "cli", + "can_be_contacted": false, + "ui_versions": "cli:[CLI_VERSION]", + "user_agent": "cli/[CLI_VERSION] cmd/feedback agent/test-agent", + "text": "hello" + } + } +} + +=== A broken bundle does not block feedback + +>>> withdir broken-bundle [CLI] feedback bundle config does not block feedback +Thank you! Your feedback was sent. Reference: [UUID][10] + +>>> print_requests.py //telemetry-ext +{ + "frontend_log_event_id": "[UUID][10]", + "entry": { + "fast_feedback_log": { + "reference_event_id": "[UUID][11]", + "parent_ui": "DeveloperEcosystem.DatabricksCLI", + "origin": "cli", + "can_be_contacted": false, + "ui_versions": "cli:[CLI_VERSION]", + "user_agent": "cli/[CLI_VERSION] cmd/feedback", + "text": "bundle config does not block feedback" + } + } +} diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script new file mode 100644 index 00000000000..937631d9ea4 --- /dev/null +++ b/acceptance/cmd/feedback/script @@ -0,0 +1,40 @@ +# Print the feedback event, keeping only user agent entries that carry feedback +# context; the rest depend on the runner. +print_feedback() { + trace print_requests.py //telemetry-ext | jq '.body.protoLogs[] | fromjson + | .entry.fast_feedback_log.user_agent |= (split(" ") | map(select(test("^(cli|cmd|run-id|agent)/"))) | join(" "))' +} + +title "Feedback about a product command is routed to its team" +trace $CLI feedback --command "air run" --run-id 123 --can-be-contacted " logs omitted the failure reason " +print_feedback + +title "A command group, with a JSON receipt" +trace $CLI feedback --command bundle "deploy should print which resources changed" -o json +print_feedback + +title "Feedback can name a nested command" +trace $CLI feedback --command "bundle deploy" "deploy is slow" +print_feedback + +title "Validation errors send nothing" +musterr trace $CLI feedback " " +musterr trace $CLI feedback +musterr trace $CLI feedback --command "no-such-command" "hello" +musterr trace $CLI feedback --command "bundle no-such-command" "hello" +musterr trace $CLI feedback --run-id abc "hello" + +title "General CLI feedback" +trace $CLI feedback "the CLI should support ..." +print_feedback + +title "An AI agent needs the user's confirmation" +musterr trace AGENT=test-agent $CLI feedback "hello" + +title "An AI agent with --yes" +trace AGENT=test-agent $CLI feedback --yes "hello" +print_feedback + +title "A broken bundle does not block feedback" +trace withdir broken-bundle $CLI feedback "bundle config does not block feedback" +print_feedback diff --git a/acceptance/cmd/feedback/test.toml b/acceptance/cmd/feedback/test.toml new file mode 100644 index 00000000000..159efe02696 --- /dev/null +++ b/acceptance/cmd/feedback/test.toml @@ -0,0 +1 @@ +RecordRequests = true diff --git a/acceptance/help/output.txt b/acceptance/help/output.txt index 9a63360e10c..ae4f221437f 100644 --- a/acceptance/help/output.txt +++ b/acceptance/help/output.txt @@ -185,6 +185,7 @@ Domains Developer Tools air Run and manage AI Runtime workloads on serverless GPU compute bundle Declarative Automation Bundles let you express data/AI/analytics projects as code. + feedback Send feedback about the Databricks CLI to Databricks sandbox *Beta* Manage Databricks Sandbox environments sync Synchronize a local directory to a workspace directory diff --git a/cmd/cmd.go b/cmd/cmd.go index 251058b0127..538a2fb9a26 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -17,6 +17,7 @@ import ( "github.com/databricks/cli/cmd/completion" "github.com/databricks/cli/cmd/configure" "github.com/databricks/cli/cmd/experimental" + "github.com/databricks/cli/cmd/feedback" "github.com/databricks/cli/cmd/fs" "github.com/databricks/cli/cmd/labs" "github.com/databricks/cli/cmd/pipelines" @@ -102,6 +103,7 @@ func topLevelSpecs() []root.CommandSpec { bundle.Spec, cache.Spec, experimental.Spec, + feedback.Spec, psql.Spec, configure.Spec, fs.Spec, diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go new file mode 100644 index 00000000000..c6ae07709fb --- /dev/null +++ b/cmd/feedback/feedback.go @@ -0,0 +1,201 @@ +package feedback + +import ( + "context" + "errors" + "fmt" + "strconv" + "strings" + "time" + "unicode/utf8" + "uuid" + + "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/internal/build" + "github.com/databricks/cli/libs/agent" + "github.com/databricks/cli/libs/cmdctx" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/telemetry" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go/useragent" + "github.com/spf13/cobra" +) + +const ( + messageLimit = 10000 + sendTimeout = 30 * time.Second + + defaultComponent = "DeveloperEcosystem.DatabricksCLI" + runIDKey = "run-id" +) + +var productComponents = map[string]string{ + "air": "ModelTraining.AIRuntime", + "bundle": "DeveloperEcosystem.CoreDabs", +} + +const receiptTemplate = `Thank you! Your feedback was sent. Reference: {{.FeedbackID}} +` + +type receipt struct { + Received bool `json:"received"` + FeedbackID string `json:"feedback_id"` +} + +type options struct { + command string + runID string + canBeContacted bool + yes bool +} + +// Spec declares the feedback command. +var Spec = root.CommandSpec{ + Use: "feedback MESSAGE", + Short: "Send feedback about the Databricks CLI to Databricks", + GroupID: "development", + NeedsFullTree: true, + Build: buildCommand, +} + +func buildCommand(ctx context.Context, cmd *cobra.Command) { + cmd.Long = `Send feedback about the Databricks CLI to Databricks. + +Describe the command you ran, what happened, what you expected, and any +workaround. Use --command to name the command the feedback is about so it +reaches the team that owns it. + +The message is sent with the workspace and user you are authenticated as and +the CLI user agent, which includes the CLI version, operating system and, if +detected, the AI agent running the CLI. No logs, files or transcripts are +collected. Do not include credentials or sensitive data. Follow-up contact is +opt-in with --can-be-contacted. Feedback is sent even when +DATABRICKS_CLI_DISABLE_TELEMETRY is set, because you asked to send it. + +AI agents: submit feedback only with the user's approval. When an agent is +detected, the command asks for confirmation, or requires --yes when no terminal +is attached. Show the user the exact message and pass --yes only after the user +approves it.` + cmd.Example = ` databricks feedback --command "bundle deploy" "deploy should print which resources changed" + databricks feedback --command "air run" --run-id 123 "logs omitted the failure reason"` + cmd.Args = root.ExactArgs(1) + + var opts options + cmd.Flags().StringVar(&opts.command, "command", "", `CLI command the feedback is about, for example "bundle deploy"`) + cmd.Flags().StringVar(&opts.runID, "run-id", "", "ID of the job run the feedback is about") + cmd.Flags().BoolVar(&opts.canBeContacted, "can-be-contacted", false, "Allow Databricks to contact you about this feedback") + cmd.Flags().BoolVarP(&opts.yes, "yes", "y", false, "Confirm sending feedback written by an AI agent") + + cmd.PreRunE = func(cmd *cobra.Command, args []string) error { + cmd.SetContext(root.SkipLoadBundle(cmd.Context())) + return root.MustWorkspaceClient(cmd, args) + } + cmd.RunE = func(cmd *cobra.Command, args []string) error { + return run(cmd, args[0], &opts) + } +} + +func run(cmd *cobra.Command, message string, opts *options) error { + ctx := cmd.Context() + text, err := validateMessage(message) + if err != nil { + return err + } + + component, origin, err := resolveCommand(cmd, opts.command) + if err != nil { + return err + } + if opts.runID != "" { + id, err := strconv.ParseInt(opts.runID, 10, 64) + if err != nil || id <= 0 { + return errors.New("--run-id must be a positive job run ID") + } + ctx = useragent.InContext(ctx, runIDKey, opts.runID) + } + + if provider := useragent.AgentProvider(); provider != "" { + // The SDK adds the agent to HTTP request headers, but the feedback record + // needs it too so triage can distinguish agent reports. + ctx = useragent.InContext(ctx, useragent.AgentKey, provider) + if !opts.yes { + confirmed, err := confirm(ctx, text) + if err != nil || !confirmed { + return err + } + } + } + + id := uuid.New().String() + sendCtx, cancel := context.WithTimeout(ctx, sendTimeout) + defer cancel() + err = telemetry.Send(sendCtx, cmdctx.WorkspaceClient(ctx).Config, []protos.FrontendLog{{ + FrontendLogEventID: id, + Entry: protos.FrontendLogEntry{ + FastFeedbackLog: &protos.FastFeedbackLog{ + ReferenceEventID: cmdctx.ExecId(ctx), + ParentUI: component, + Origin: origin, + CanBeContacted: opts.canBeContacted, + UIVersions: "cli:" + build.GetInfo().Version, + UserAgent: useragent.FromContext(ctx), + Text: text, + }, + }, + }}) + if err != nil { + // The upload may have been accepted before the error, so retrying can + // file the same feedback twice. + return fmt.Errorf("feedback %s could not be confirmed as sent: %w", id, err) + } + + return cmdio.RenderWithTemplate(ctx, receipt{Received: true, FeedbackID: id}, "", receiptTemplate) +} + +func validateMessage(message string) (string, error) { + text := strings.TrimSpace(message) + if text == "" || utf8.RuneCountInString(text) > messageLimit { + return "", fmt.Errorf("feedback message must contain 1 to %d characters", messageLimit) + } + return text, nil +} + +func resolveCommand(cmd *cobra.Command, command string) (string, string, error) { + if command == "" { + return defaultComponent, "cli", nil + } + target, rest, err := cmd.Root().Find(strings.Fields(command)) + if err != nil || len(rest) > 0 || target == cmd.Root() { + return "", "", fmt.Errorf("unknown command %q", command) + } + component := defaultComponent + if productComponent, ok := productComponents[topLevelName(target)]; ok { + component = productComponent + } + return component, root.CommandString(target), nil +} + +func topLevelName(cmd *cobra.Command) string { + for cmd.HasParent() && cmd.Parent().HasParent() { + cmd = cmd.Parent() + } + return cmd.Name() +} + +// confirm asks the user to approve feedback an AI agent is about to send. It +// returns false without error if the user declines. +func confirm(ctx context.Context, text string) (bool, error) { + if !cmdio.IsInputTTY(ctx) { + return false, errors.New("feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + + agent.AgentNote("show the user the exact feedback message and pass --yes only after the user approves sending it")) + } + cmdio.LogString(ctx, "The following feedback will be sent to Databricks:\n\n"+text+"\n") + confirmed, err := cmdio.AskYesOrNo(ctx, "Send this feedback?") + if err != nil { + return false, err + } + if !confirmed { + cmdio.LogString(ctx, "Feedback not sent.") + } + return confirmed, nil +} diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go new file mode 100644 index 00000000000..f51af74ee0a --- /dev/null +++ b/cmd/feedback/feedback_test.go @@ -0,0 +1,136 @@ +package feedback + +import ( + "bytes" + "io" + "strings" + "testing" + + "github.com/databricks/cli/internal/testutil" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/flags" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestValidateMessage(t *testing.T) { + tests := []struct { + name string + message string + want string + wantErr bool + }{ + {name: "trimmed", message: " feedback\n", want: "feedback"}, + {name: "maximum runes", message: strings.Repeat("é", messageLimit), want: strings.Repeat("é", messageLimit)}, + {name: "blank", message: " \n", wantErr: true}, + {name: "too long", message: strings.Repeat("x", messageLimit+1), wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := validateMessage(tt.message) + if tt.wantErr { + assert.EqualError(t, err, "feedback message must contain 1 to 10000 characters") + return + } + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} + +func TestResolveCommand(t *testing.T) { + rootCmd := &cobra.Command{Use: "databricks"} + feedbackCmd := &cobra.Command{Use: "feedback"} + rootCmd.AddCommand(feedbackCmd) + for _, path := range []string{"air run", "bundle deploy", "version"} { + parent := rootCmd + for name := range strings.FieldsSeq(path) { + child := &cobra.Command{Use: name} + parent.AddCommand(child) + parent = child + } + } + + tests := []struct { + name string + command string + wantComponent string + wantOrigin string + wantErr string + }{ + {name: "general", wantComponent: defaultComponent, wantOrigin: "cli"}, + {name: "air subcommand", command: "air run", wantComponent: "ModelTraining.AIRuntime", wantOrigin: "air_run"}, + {name: "bundle group", command: "bundle", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle"}, + {name: "bundle subcommand", command: "bundle deploy", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_deploy"}, + {name: "other command", command: "version", wantComponent: defaultComponent, wantOrigin: "version"}, + {name: "unknown top level", command: "unknown", wantErr: `unknown command "unknown"`}, + {name: "unknown subcommand", command: "bundle unknown", wantErr: `unknown command "bundle unknown"`}, + {name: "blank", command: " ", wantErr: `unknown command " "`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + component, origin, err := resolveCommand(feedbackCmd, tt.command) + if tt.wantErr != "" { + assert.EqualError(t, err, tt.wantErr) + return + } + require.NoError(t, err) + assert.Equal(t, tt.wantComponent, component) + assert.Equal(t, tt.wantOrigin, origin) + }) + } +} + +func TestConfirm(t *testing.T) { + t.Run("accepted", func(t *testing.T) { + var stderr bytes.Buffer + ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader("y\n"), io.Discard, &stderr)) + confirmed, err := confirm(ctx, "exact feedback") + require.NoError(t, err) + assert.True(t, confirmed) + assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\nexact feedback\n") + assert.Contains(t, stderr.String(), "Send this feedback? [y/N]: ") + }) + + t.Run("declined", func(t *testing.T) { + var stderr bytes.Buffer + ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader("\n"), io.Discard, &stderr)) + confirmed, err := confirm(ctx, "exact feedback") + require.NoError(t, err) + assert.False(t, confirmed) + assert.Contains(t, stderr.String(), "Feedback not sent.") + }) + + t.Run("no terminal", func(t *testing.T) { + testutil.SetAgent(t, "test-agent") + ctx := cmdio.InContext(t.Context(), cmdio.NewIO(t.Context(), flags.OutputText, strings.NewReader(""), io.Discard, io.Discard, "", "")) + confirmed, err := confirm(ctx, "exact feedback") + assert.False(t, confirmed) + assert.EqualError(t, err, "feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n"+ + "Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it") + }) +} + +func TestConfirmWithoutColorSupport(t *testing.T) { + for _, tc := range []struct { + name string + key string + value string + }{ + {name: "no color", key: "NO_COLOR", value: "1"}, + {name: "dumb terminal", key: "TERM", value: "dumb"}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx := env.Set(t.Context(), tc.key, tc.value) + var stderr bytes.Buffer + in := cmdio.FakeTTYReader(strings.NewReader("y\n")) + ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputText, in, io.Discard, cmdio.FakeTTY(&stderr), "", "")) + assert.False(t, cmdio.IsPromptSupported(ctx)) + confirmed, err := confirm(ctx, "exact feedback") + require.NoError(t, err) + assert.True(t, confirmed) + }) + } +} diff --git a/cmd/root/command_spec.go b/cmd/root/command_spec.go index 028a0e4af2d..4f8f42bb910 100644 --- a/cmd/root/command_spec.go +++ b/cmd/root/command_spec.go @@ -26,6 +26,10 @@ type CommandSpec struct { Hidden bool Deprecated string + // NeedsFullTree builds every top-level command before running this command, + // for commands that resolve other command paths while they run. + NeedsFullTree bool + Build func(ctx context.Context, cmd *cobra.Command) } diff --git a/cmd/root/command_spec_test.go b/cmd/root/command_spec_test.go index cbbb1d1519d..c95da2749f6 100644 --- a/cmd/root/command_spec_test.go +++ b/cmd/root/command_spec_test.go @@ -10,13 +10,13 @@ import ( "github.com/stretchr/testify/assert" ) -// Every spec field must reach the command, and changing it while building -// must panic. This fails if a field is added to Spec but not handled. +// Every command-valued spec field must reach the command, and changing it while +// building must panic. This fails if such a field is added but not handled. func TestSpecFields(t *testing.T) { specType := reflect.TypeFor[root.CommandSpec]() for i := range specType.NumField() { field := specType.Field(i) - if field.Name == "Build" { + if field.Name == "Build" || field.Name == "NeedsFullTree" { continue } t.Run(field.Name, func(t *testing.T) { diff --git a/cmd/root/root.go b/cmd/root/root.go index a988c22d285..2f8943f6b60 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -204,7 +204,7 @@ Stack Trace: exitCode = 1 } - commandStr := commandString(cmd) + commandStr := CommandString(cmd) ctx = cmd.Context() telemetryErr := telemetry.Upload(cmd.Context(), protos.ExecutionContext{ diff --git a/cmd/root/tree.go b/cmd/root/tree.go index a1e3324b7e8..c70fa124d08 100644 --- a/cmd/root/tree.go +++ b/cmd/root/tree.go @@ -30,8 +30,10 @@ func (t *Tree) Add(specs ...CommandSpec) { // Copy the fields so that the caller changing them later can't make // the placeholder and the built command differ. build := spec.Build + needsFullTree := spec.NeedsFullTree spec = specFields(spec.command()) spec.Build = build + spec.NeedsFullTree = needsFullTree placeholder := spec.command() // Cobra only lists a command in help and completion if it is // runnable or has subcommands; a placeholder has no subcommands. @@ -123,7 +125,11 @@ func (t *Tree) buildForArgs(ctx context.Context, args []string) { // way cobra parses them. Building everything avoids parsing them here. t.buildAll(ctx) case pending: - t.replace(ctx, cmd) + if t.pending[cmd].NeedsFullTree { + t.buildAll(ctx) + } else { + t.replace(ctx, cmd) + } } } diff --git a/cmd/root/tree_test.go b/cmd/root/tree_test.go index f3042b59ffc..33692a6c368 100644 --- a/cmd/root/tree_test.go +++ b/cmd/root/tree_test.go @@ -109,3 +109,25 @@ func TestAddCopiesSpecFields(t *testing.T) { aliases[0] = "new" require.NoError(t, r.Execute(t.Context(), []string{"old", "--help"})) } + +func TestRunningCommandCanResolveNestedCommand(t *testing.T) { + var built []string + r := newTestRoot(t, &built) + var resolved string + var remaining []string + r.Add(root.CommandSpec{ + Use: "inspect", + NeedsFullTree: true, + Build: func(ctx context.Context, cmd *cobra.Command) { + cmd.RunE = func(cmd *cobra.Command, args []string) error { + target, rest, err := cmd.Root().Find([]string{"one", "child"}) + resolved = target.CommandPath() + remaining = rest + return err + } + }, + }) + require.NoError(t, r.Execute(t.Context(), []string{"inspect"})) + assert.Equal(t, "databricks one child", resolved) + assert.Empty(t, remaining) +} diff --git a/cmd/root/user_agent_command.go b/cmd/root/user_agent_command.go index c1a9adf27cd..e0e38994303 100644 --- a/cmd/root/user_agent_command.go +++ b/cmd/root/user_agent_command.go @@ -14,9 +14,8 @@ import ( // See unit test [main.TestCommandsDontUseUnderscoreInName]. const commandSeparator = "_" -// commandString walks up the command hierarchy of the specified -// command to build a string representing this hierarchy. -func commandString(cmd *cobra.Command) string { +// CommandString returns the command hierarchy joined with underscores. +func CommandString(cmd *cobra.Command) string { reversed := []string{cmd.Name()} cmd.VisitParents(func(p *cobra.Command) { if !p.HasParent() { @@ -30,5 +29,5 @@ func commandString(cmd *cobra.Command) string { } func withCommandInUserAgent(ctx context.Context, cmd *cobra.Command) context.Context { - return useragent.InContext(ctx, "cmd", commandString(cmd)) + return useragent.InContext(ctx, "cmd", CommandString(cmd)) } diff --git a/cmd/root/user_agent_command_test.go b/cmd/root/user_agent_command_test.go index 9d76d2011cb..7b6907e2dc2 100644 --- a/cmd/root/user_agent_command_test.go +++ b/cmd/root/user_agent_command_test.go @@ -24,9 +24,9 @@ func TestWithCommandInUserAgent(t *testing.T) { root.AddCommand(hello) hello.AddCommand(world) - assert.Equal(t, "root", commandString(root)) - assert.Equal(t, "hello", commandString(hello)) - assert.Equal(t, "hello_world", commandString(world)) + assert.Equal(t, "root", CommandString(root)) + assert.Equal(t, "hello", CommandString(hello)) + assert.Equal(t, "hello_world", CommandString(world)) ctx := withCommandInUserAgent(t.Context(), world) diff --git a/libs/aitools/agents/hint.go b/libs/aitools/agents/hint.go index bee5ed0afaa..ad15d129c48 100644 --- a/libs/aitools/agents/hint.go +++ b/libs/aitools/agents/hint.go @@ -284,6 +284,7 @@ func isCredentialHelperCommand(cmd *cobra.Command) bool { var hintExemptCommands = []string{ "aitools", "experimental aitools", + "feedback", "auth", "configure", "current-user me", diff --git a/libs/aitools/agents/hint_test.go b/libs/aitools/agents/hint_test.go index 558ae25500c..b896e484af6 100644 --- a/libs/aitools/agents/hint_test.go +++ b/libs/aitools/agents/hint_test.go @@ -259,6 +259,7 @@ func TestIsHintExemptCommand(t *testing.T) { }{ {"aitools install", true}, {"experimental aitools skills install", true}, + {"feedback", true}, {"auth login", true}, {"auth token", true}, {"configure", true}, diff --git a/libs/telemetry/logger.go b/libs/telemetry/logger.go index aac2063fbe5..fa3785c2b23 100644 --- a/libs/telemetry/logger.go +++ b/libs/telemetry/logger.go @@ -114,13 +114,9 @@ func Upload(ctx context.Context, ec protos.ExecutionContext) error { l.logs[i].Entry.DatabricksCliLog.ExecutionContext = &ec } - protoLogs := make([]string, len(l.logs)) - for i, log := range l.logs { - b, err := json.Marshal(log) - if err != nil { - return fmt.Errorf("failed to marshal log: %w", err) - } - protoLogs[i] = string(b) + protoLogs, err := marshalLogs(l.logs) + if err != nil { + return err } if !cmdctx.HasConfigUsed(ctx) { @@ -290,6 +286,14 @@ func within[T any](ctx context.Context, running *atomic.Int32, f func() (T, erro // newAPIClient is client.New with authenticate as the auth visitor. The SDK // runs the auth visitor on every request, including its own retries. func newAPIClient(cfg *config.Config, running *atomic.Int32) (*client.DatabricksClient, error) { + return newAPIClientWithRetries(cfg, running, true) +} + +func newSingleAttemptAPIClient(cfg *config.Config, running *atomic.Int32) (*client.DatabricksClient, error) { + return newAPIClientWithRetries(cfg, running, false) +} + +func newAPIClientWithRetries(cfg *config.Config, running *atomic.Int32, allowRetries bool) (*client.DatabricksClient, error) { clientCfg, err := config.HTTPClientConfigFromConfig(cfg) if err != nil { return nil, err @@ -297,6 +301,13 @@ func newAPIClient(cfg *config.Config, running *atomic.Int32) (*client.Databricks clientCfg.AuthVisitor = func(r *http.Request) error { return authenticate(r, cfg, running) } + if !allowRetries { + // The SDK retries some connection errors regardless of ErrorRetriable, + // so a minimal retry window is needed to stop after the first attempt. + clientCfg.ErrorRetriable = func(context.Context, error) bool { return false } + clientCfg.TransientErrors = nil + clientCfg.RetryTimeout = time.Nanosecond + } return client.NewWithClient(cfg, httpclient.NewApiClient(clientCfg)) } @@ -315,6 +326,45 @@ func authenticate(r *http.Request, cfg *config.Config, running *atomic.Int32) er return nil } +// Send uploads logs in a single request and returns an error unless all of them +// were accepted. It is for events the user explicitly asked to send, so unlike +// Upload it ignores DATABRICKS_CLI_DISABLE_TELEMETRY and never retries. +func Send(ctx context.Context, cfg *config.Config, logs []protos.FrontendLog) error { + protoLogs, err := marshalLogs(logs) + if err != nil { + return err + } + + var running atomic.Int32 + apiClient, err := within(ctx, &running, func() (*client.DatabricksClient, error) { + return newSingleAttemptAPIClient(cfg, &running) + }) + if err != nil { + return fmt.Errorf("failed to create API client: %w", err) + } + + resp, err := attempt(ctx, apiClient, protoLogs) + if err != nil { + return err + } + if resp.NumProtoSuccess < int64(len(protoLogs)) { + return fmt.Errorf("only %d of %d logs were accepted", resp.NumProtoSuccess, len(protoLogs)) + } + return nil +} + +func marshalLogs(logs []protos.FrontendLog) ([]string, error) { + protoLogs := make([]string, len(logs)) + for i, log := range logs { + b, err := json.Marshal(log) + if err != nil { + return nil, fmt.Errorf("failed to marshal log: %w", err) + } + protoLogs[i] = string(b) + } + return protoLogs, nil +} + func attempt(ctx context.Context, apiClient *client.DatabricksClient, protoLogs []string) (*ResponseBody, error) { // Without the workspace routing header, telemetry on unified hosts is // recorded in a central shard instead of the user's workspace. diff --git a/libs/telemetry/logger_test.go b/libs/telemetry/logger_test.go index 94673a9dd1a..345314bd7ff 100644 --- a/libs/telemetry/logger_test.go +++ b/libs/telemetry/logger_test.go @@ -2,7 +2,9 @@ package telemetry import ( "context" + "encoding/json" "errors" + "net/http" "sync/atomic" "testing" "time" @@ -465,3 +467,98 @@ func TestWithinReturnsPanicAsError(t *testing.T) { assert.EqualError(t, err, "panic: boom") assert.Eventually(t, func() bool { return running.Load() == 0 }, time.Second, time.Millisecond) } + +func testFeedbackLog() []protos.FrontendLog { + return []protos.FrontendLog{{ + FrontendLogEventID: "id", + Entry: protos.FrontendLogEntry{ + FastFeedbackLog: &protos.FastFeedbackLog{Text: "hello"}, + }, + }} +} + +func TestSendMakesOneAttempt(t *testing.T) { + tests := []struct { + name string + numProtoSuccess int64 + statusCode int + reset bool + wantErr bool + errContains string + }{ + {name: "all accepted", numProtoSuccess: 1}, + {name: "none accepted", wantErr: true, errContains: "only 0 of 1 logs were accepted"}, + {name: "server error", statusCode: 500, wantErr: true, errContains: "server error"}, + {name: "SDK retryable error", statusCode: 503, wantErr: true, errContains: "server error"}, + {name: "connection reset", reset: true, wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + server := testserver.New(t) + var count atomic.Int32 + var body RequestBody + if tt.reset { + server.HandleRaw(http.MethodPost, "/telemetry-ext", func(w http.ResponseWriter, r *http.Request) { + count.Add(1) + conn, _, err := w.(http.Hijacker).Hijack() + if !assert.NoError(t, err) { + return + } + assert.NoError(t, conn.Close()) + }) + } else { + server.Handle(http.MethodPost, "/telemetry-ext", func(req testserver.Request) any { + count.Add(1) + assert.NoError(t, json.Unmarshal(req.Body, &body)) + if tt.statusCode != 0 { + return testserver.Response{ + StatusCode: tt.statusCode, + Body: apierr.APIError{StatusCode: tt.statusCode, Message: "server error"}, + } + } + return ResponseBody{NumProtoSuccess: tt.numProtoSuccess} + }) + } + + err := Send(t.Context(), &config.Config{Host: server.URL, Token: "token"}, testFeedbackLog()) + if !tt.wantErr { + require.NoError(t, err) + } else { + require.Error(t, err) + if tt.errContains != "" { + assert.ErrorContains(t, err, tt.errContains) + } + } + assert.Equal(t, int32(1), count.Load()) + if !tt.reset { + assert.Equal(t, []string{`{"frontend_log_event_id":"id","entry":{"fast_feedback_log":{"can_be_contacted":false,"text":"hello"}}}`}, body.ProtoLogs) + } + }) + } +} + +func TestSendTimeoutBoundsTokenAcquisition(t *testing.T) { + server := testserver.New(t) + release := make(chan struct{}) + t.Cleanup(func() { close(release) }) + ts := auth.TokenSourceFn(func(context.Context) (*oauth2.Token, error) { + <-release + return nil, errors.New("token endpoint unavailable") + }) + ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond) + defer cancel() + + errc := make(chan error, 1) + go func() { + errc <- Send(ctx, &config.Config{ + Host: server.URL, + Credentials: config.NewTokenSourceStrategy("blocking", ts), + }, testFeedbackLog()) + }() + select { + case err := <-errc: + assert.ErrorIs(t, err, context.DeadlineExceeded) + case <-time.After(10 * time.Second): + t.Fatal("Send did not return after its context deadline") + } +} diff --git a/libs/telemetry/protos/README.md b/libs/telemetry/protos/README.md index aacdcca2e50..64a78b0598d 100644 --- a/libs/telemetry/protos/README.md +++ b/libs/telemetry/protos/README.md @@ -18,6 +18,12 @@ machine, for agents without a session id), so when the command that printed it r (e.g. `databricks bundle schema`), no install hint event is logged for that hour even though the agent saw the hint. +`databricks feedback` sends one `fast_feedback_log` entry, the event the in-product feedback widget +records, instead of a `databricks_cli_log`. The user asked for it to be sent, so it is uploaded +immediately in a single attempt even when `DATABRICKS_CLI_DISABLE_TELEMETRY` is set. When an AI +agent is detected, the user must first confirm the message. Agent and run context ride in the +entry's `user_agent`. + `databricks aitools` install, update, list, version and uninstall log an `aitools_command_event` funnel per run: STARTED, one STEP_COMPLETED per install step reached (install only), and FINISHED with the outcome and error category. The CLI sets no calling-agent field on it: the upload's diff --git a/libs/telemetry/protos/fast_feedback_log.go b/libs/telemetry/protos/fast_feedback_log.go new file mode 100644 index 00000000000..ff51d491ae5 --- /dev/null +++ b/libs/telemetry/protos/fast_feedback_log.go @@ -0,0 +1,26 @@ +package protos + +// FastFeedbackLog corresponds to the FastFeedbackLog lumberjack proto in universe +// (proto/logs/frontend/docs-platform/fast_feedback_log.proto). It is the event the +// in-product feedback widget records, so CLI feedback is triaged the same way. +type FastFeedbackLog struct { + // ID of an event the feedback relates to. The CLI sets the command execution ID so + // triagers can find the matching requests in http_access_log. + ReferenceEventID string `json:"reference_event_id,omitempty"` + + // ES component the feedback is filed under (e.g. "DeveloperEcosystem.DatabricksCLI"). + ParentUI string `json:"parent_ui,omitempty"` + + // Feature within the parent UI the feedback is about. + Origin string `json:"origin,omitempty"` + + CanBeContacted bool `json:"can_be_contacted"` + + // Comma-separated "key:value" pairs of component versions. + UIVersions string `json:"ui_versions,omitempty"` + + UserAgent string `json:"user_agent,omitempty"` + + // Feedback text written by the user. + Text string `json:"text,omitempty"` +} diff --git a/libs/telemetry/protos/frontend_log.go b/libs/telemetry/protos/frontend_log.go index f3c37736cc1..93aad455d1d 100644 --- a/libs/telemetry/protos/frontend_log.go +++ b/libs/telemetry/protos/frontend_log.go @@ -9,8 +9,11 @@ type FrontendLog struct { Entry FrontendLogEntry `json:"entry,omitempty"` } +// FrontendLogEntry mirrors a oneof in the proto, so set only one field. omitzero +// keeps an unset DatabricksCliLog, a value field, out of the JSON. type FrontendLogEntry struct { - DatabricksCliLog DatabricksCliLog `json:"databricks_cli_log,omitempty"` + DatabricksCliLog DatabricksCliLog `json:"databricks_cli_log,omitzero"` + FastFeedbackLog *FastFeedbackLog `json:"fast_feedback_log,omitempty"` } type DatabricksCliLog struct { From 18318ee7fc937858e762f736056fbfe427462e59 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:56:50 +0000 Subject: [PATCH 02/20] Link the feedback changelog entry to its PR Co-authored-by: Isaac --- .nextchanges/cli/feedback.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/cli/feedback.md b/.nextchanges/cli/feedback.md index 293ae5f1821..b402f08a736 100644 --- a/.nextchanges/cli/feedback.md +++ b/.nextchanges/cli/feedback.md @@ -1 +1 @@ -* Add the `databricks feedback` command to send feedback about the CLI to Databricks. ([#7567](https://github.com/databricks/cli/pull/7567)) +* Add the `databricks feedback` command to send feedback about the CLI to Databricks. ([#7724](https://github.com/databricks/cli/pull/7724)) From d6cb2af6ebd47a5e276462d4c46fb74c1552d360 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:30:18 +0000 Subject: [PATCH 03/20] Exclude `feedback` from the count fuzz of generated commands The fuzz calls RunE without the exec ID that cmd/root always sets, and is meant for auto-generated commands; hand-written ones are listed in manualRoots. Co-authored-by: Isaac --- cmd/root/fuzz_panic_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/root/fuzz_panic_test.go b/cmd/root/fuzz_panic_test.go index 1fc8fd9c17d..a52815ffb63 100644 --- a/cmd/root/fuzz_panic_test.go +++ b/cmd/root/fuzz_panic_test.go @@ -209,6 +209,7 @@ func isAutoGenerated(leaf leafCommand) bool { "completion": true, "configure": true, "experimental": true, + "feedback": true, "labs": true, "sandbox": true, "pipelines": true, From abccd592226944f634ec3431d300724e846d3bda Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:15:00 +0000 Subject: [PATCH 04/20] Check feedback input before authenticating and show all sent fields Validate the message, --command and --run-id, and reject --target, before the workspace client is created, so a mistake is not hidden behind a login prompt or an auth error. --target is rejected because feedback never loads the bundle. The confirmation now shows every field the user approves: the message, with control characters escaped so none can hide part of it, and the command, run ID and contact opt-in. --run-id is sent in canonical form. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 12 ++- acceptance/cmd/feedback/script | 4 + cmd/feedback/feedback.go | 106 +++++++++++++++++---- cmd/feedback/feedback_test.go | 105 ++++++++++++++++++-- cmd/root/user_agent_command.go | 3 +- libs/aitools/agents/hint.go | 2 +- libs/telemetry/protos/fast_feedback_log.go | 9 +- 7 files changed, 207 insertions(+), 34 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index 4a86f4c5eeb..17179c73de2 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -95,14 +95,22 @@ Global Flags: >>> [CLI] feedback --command no-such-command hello -Error: unknown command "no-such-command" +Error: unknown command "no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments >>> [CLI] feedback --command bundle no-such-command hello -Error: unknown command "bundle no-such-command" +Error: unknown command "bundle no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments >>> [CLI] feedback --run-id abc hello Error: --run-id must be a positive job run ID +>>> [CLI] feedback --target dev hello +Error: --target selects a bundle target, which feedback does not use; use --profile to choose the workspace + +=== Input errors are reported before authenticating + +>>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN [CLI] feedback +Error: feedback message must contain 1 to 10000 characters + === General CLI feedback >>> [CLI] feedback the CLI should support ... diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script index 937631d9ea4..a3819a176d3 100644 --- a/acceptance/cmd/feedback/script +++ b/acceptance/cmd/feedback/script @@ -23,6 +23,10 @@ musterr trace $CLI feedback musterr trace $CLI feedback --command "no-such-command" "hello" musterr trace $CLI feedback --command "bundle no-such-command" "hello" musterr trace $CLI feedback --run-id abc "hello" +musterr trace $CLI feedback --target dev "hello" + +title "Input errors are reported before authenticating" +musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI feedback " " title "General CLI feedback" trace $CLI feedback "the CLI should support ..." diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index c6ae07709fb..7fa50183f37 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" "time" + "unicode" "unicode/utf8" "uuid" @@ -49,6 +50,17 @@ type options struct { yes bool } +// submission is the validated input, checked before authenticating so that a +// mistake is reported without a login prompt or a network call. +type submission struct { + text string + command string + component string + origin string + runID string + canBeContacted bool +} + // Spec declares the feedback command. var Spec = root.CommandSpec{ Use: "feedback MESSAGE", @@ -86,40 +98,63 @@ approves it.` cmd.Flags().BoolVar(&opts.canBeContacted, "can-be-contacted", false, "Allow Databricks to contact you about this feedback") cmd.Flags().BoolVarP(&opts.yes, "yes", "y", false, "Confirm sending feedback written by an AI agent") + var s submission cmd.PreRunE = func(cmd *cobra.Command, args []string) error { + var err error + s, err = newSubmission(cmd, args[0], &opts) + if err != nil { + return err + } cmd.SetContext(root.SkipLoadBundle(cmd.Context())) return root.MustWorkspaceClient(cmd, args) } cmd.RunE = func(cmd *cobra.Command, args []string) error { - return run(cmd, args[0], &opts) + return send(cmd.Context(), s, opts.yes) } } -func run(cmd *cobra.Command, message string, opts *options) error { - ctx := cmd.Context() +func newSubmission(cmd *cobra.Command, message string, opts *options) (submission, error) { + // Feedback never loads the bundle, so a target would be ignored. + for _, name := range []string{"target", "environment"} { + if f := cmd.Flag(name); f != nil && f.Changed { + return submission{}, fmt.Errorf("--%s selects a bundle target, which feedback does not use; use --profile to choose the workspace", name) + } + } text, err := validateMessage(message) if err != nil { - return err + return submission{}, err } - component, origin, err := resolveCommand(cmd, opts.command) if err != nil { - return err + return submission{}, err + } + s := submission{ + text: text, + command: strings.Join(strings.Fields(opts.command), " "), + component: component, + origin: origin, + canBeContacted: opts.canBeContacted, } if opts.runID != "" { id, err := strconv.ParseInt(opts.runID, 10, 64) if err != nil || id <= 0 { - return errors.New("--run-id must be a positive job run ID") + return submission{}, errors.New("--run-id must be a positive job run ID") } - ctx = useragent.InContext(ctx, runIDKey, opts.runID) + s.runID = strconv.FormatInt(id, 10) } + return s, nil +} +func send(ctx context.Context, s submission, yes bool) error { + if s.runID != "" { + ctx = useragent.InContext(ctx, runIDKey, s.runID) + } if provider := useragent.AgentProvider(); provider != "" { // The SDK adds the agent to HTTP request headers, but the feedback record // needs it too so triage can distinguish agent reports. ctx = useragent.InContext(ctx, useragent.AgentKey, provider) - if !opts.yes { - confirmed, err := confirm(ctx, text) + if !yes { + confirmed, err := confirm(ctx, s) if err != nil || !confirmed { return err } @@ -129,17 +164,17 @@ func run(cmd *cobra.Command, message string, opts *options) error { id := uuid.New().String() sendCtx, cancel := context.WithTimeout(ctx, sendTimeout) defer cancel() - err = telemetry.Send(sendCtx, cmdctx.WorkspaceClient(ctx).Config, []protos.FrontendLog{{ + err := telemetry.Send(sendCtx, cmdctx.WorkspaceClient(ctx).Config, []protos.FrontendLog{{ FrontendLogEventID: id, Entry: protos.FrontendLogEntry{ FastFeedbackLog: &protos.FastFeedbackLog{ ReferenceEventID: cmdctx.ExecId(ctx), - ParentUI: component, - Origin: origin, - CanBeContacted: opts.canBeContacted, + ParentUI: s.component, + Origin: s.origin, + CanBeContacted: s.canBeContacted, UIVersions: "cli:" + build.GetInfo().Version, UserAgent: useragent.FromContext(ctx), - Text: text, + Text: s.text, }, }, }}) @@ -166,7 +201,7 @@ func resolveCommand(cmd *cobra.Command, command string) (string, string, error) } target, rest, err := cmd.Root().Find(strings.Fields(command)) if err != nil || len(rest) > 0 || target == cmd.Root() { - return "", "", fmt.Errorf("unknown command %q", command) + return "", "", fmt.Errorf(`unknown command %q; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, command) } component := defaultComponent if productComponent, ok := productComponents[topLevelName(target)]; ok { @@ -184,12 +219,12 @@ func topLevelName(cmd *cobra.Command) string { // confirm asks the user to approve feedback an AI agent is about to send. It // returns false without error if the user declines. -func confirm(ctx context.Context, text string) (bool, error) { +func confirm(ctx context.Context, s submission) (bool, error) { if !cmdio.IsInputTTY(ctx) { return false, errors.New("feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + agent.AgentNote("show the user the exact feedback message and pass --yes only after the user approves sending it")) } - cmdio.LogString(ctx, "The following feedback will be sent to Databricks:\n\n"+text+"\n") + cmdio.LogString(ctx, "The following feedback will be sent to Databricks:\n\n"+summary(s)) confirmed, err := cmdio.AskYesOrNo(ctx, "Send this feedback?") if err != nil { return false, err @@ -199,3 +234,38 @@ func confirm(ctx context.Context, text string) (bool, error) { } return confirmed, nil } + +// summary returns everything the user approves: the message and the fields +// sent with it. +func summary(s submission) string { + var b strings.Builder + b.WriteString(escapeControl(s.text)) + b.WriteString("\n\n") + if s.command != "" { + fmt.Fprintf(&b, "Command: %s\n", s.command) + } + if s.runID != "" { + fmt.Fprintf(&b, "Run ID: %s\n", s.runID) + } + contact := "no" + if s.canBeContacted { + contact = "yes" + } + fmt.Fprintf(&b, "Databricks may contact you: %s\n", contact) + return b.String() +} + +// escapeControl escapes control characters other than newlines and tabs, so +// that none of them can hide or overwrite part of the message in the terminal. +func escapeControl(text string) string { + var b strings.Builder + for _, r := range text { + if unicode.IsControl(r) && r != '\n' && r != '\t' { + quoted := strconv.QuoteRune(r) + b.WriteString(quoted[1 : len(quoted)-1]) + continue + } + b.WriteRune(r) + } + return b.String() +} diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index f51af74ee0a..921d81de1af 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -40,8 +40,14 @@ func TestValidateMessage(t *testing.T) { } } -func TestResolveCommand(t *testing.T) { +const commandHint = `; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments` + +// newTestFeedbackCommand returns the feedback command under a root with the +// root's bundle target flags and a few commands --command can name. +func newTestFeedbackCommand() *cobra.Command { rootCmd := &cobra.Command{Use: "databricks"} + rootCmd.PersistentFlags().String("target", "", "") + rootCmd.PersistentFlags().String("environment", "", "") feedbackCmd := &cobra.Command{Use: "feedback"} rootCmd.AddCommand(feedbackCmd) for _, path := range []string{"air run", "bundle deploy", "version"} { @@ -52,6 +58,11 @@ func TestResolveCommand(t *testing.T) { parent = child } } + return feedbackCmd +} + +func TestResolveCommand(t *testing.T) { + feedbackCmd := newTestFeedbackCommand() tests := []struct { name string @@ -65,9 +76,10 @@ func TestResolveCommand(t *testing.T) { {name: "bundle group", command: "bundle", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle"}, {name: "bundle subcommand", command: "bundle deploy", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_deploy"}, {name: "other command", command: "version", wantComponent: defaultComponent, wantOrigin: "version"}, - {name: "unknown top level", command: "unknown", wantErr: `unknown command "unknown"`}, - {name: "unknown subcommand", command: "bundle unknown", wantErr: `unknown command "bundle unknown"`}, - {name: "blank", command: " ", wantErr: `unknown command " "`}, + {name: "unknown top level", command: "unknown", wantErr: `unknown command "unknown"` + commandHint}, + {name: "unknown subcommand", command: "bundle unknown", wantErr: `unknown command "bundle unknown"` + commandHint}, + {name: "with arguments", command: "bundle deploy -t dev", wantErr: `unknown command "bundle deploy -t dev"` + commandHint}, + {name: "blank", command: " ", wantErr: `unknown command " "` + commandHint}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -83,21 +95,96 @@ func TestResolveCommand(t *testing.T) { } } +func TestNewSubmission(t *testing.T) { + tests := []struct { + name string + flags []string + opts options + message string + want submission + wantErr string + }{ + { + name: "all fields", + opts: options{command: "bundle deploy", runID: "+00123", canBeContacted: true}, + message: " slow ", + want: submission{ + text: "slow", command: "bundle deploy", component: "DeveloperEcosystem.CoreDabs", + origin: "bundle_deploy", runID: "123", canBeContacted: true, + }, + }, + {name: "zero run ID", opts: options{runID: "0"}, message: "x", wantErr: "--run-id must be a positive job run ID"}, + {name: "non-numeric run ID", opts: options{runID: "abc"}, message: "x", wantErr: "--run-id must be a positive job run ID"}, + {name: "blank message", message: " ", wantErr: "feedback message must contain 1 to 10000 characters"}, + {name: "unknown command", opts: options{command: "nope"}, message: "x", wantErr: `unknown command "nope"` + commandHint}, + { + name: "target", flags: []string{"--target", "dev"}, message: "x", + wantErr: "--target selects a bundle target, which feedback does not use; use --profile to choose the workspace", + }, + { + name: "deprecated environment", flags: []string{"--environment", "dev"}, message: "x", + wantErr: "--environment selects a bundle target, which feedback does not use; use --profile to choose the workspace", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + feedbackCmd := newTestFeedbackCommand() + require.NoError(t, feedbackCmd.ParseFlags(tt.flags)) + got, err := newSubmission(feedbackCmd, tt.message, &tt.opts) + if tt.wantErr != "" { + assert.EqualError(t, err, tt.wantErr) + return + } + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} + +func TestSummary(t *testing.T) { + tests := []struct { + name string + s submission + want string + }{ + { + name: "message only", + s: submission{text: "line one\n\tline two"}, + want: "line one\n\tline two\n\nDatabricks may contact you: no\n", + }, + { + name: "all fields", + s: submission{text: "slow", command: "air run", runID: "42", canBeContacted: true}, + want: "slow\n\nCommand: air run\nRun ID: 42\nDatabricks may contact you: yes\n", + }, + { + name: "control characters cannot hide text", + s: submission{text: "great\x1b[8m hidden\x1b[0m\r\u0085"}, + want: `great\x1b[8m hidden\x1b[0m\r\u0085` + "\n\nDatabricks may contact you: no\n", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, summary(tt.s)) + }) + } +} + func TestConfirm(t *testing.T) { t.Run("accepted", func(t *testing.T) { var stderr bytes.Buffer ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader("y\n"), io.Discard, &stderr)) - confirmed, err := confirm(ctx, "exact feedback") + confirmed, err := confirm(ctx, submission{text: "exact feedback"}) require.NoError(t, err) assert.True(t, confirmed) - assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\nexact feedback\n") + assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\nexact feedback\n\nDatabricks may contact you: no\n\n") assert.Contains(t, stderr.String(), "Send this feedback? [y/N]: ") }) t.Run("declined", func(t *testing.T) { var stderr bytes.Buffer ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader("\n"), io.Discard, &stderr)) - confirmed, err := confirm(ctx, "exact feedback") + confirmed, err := confirm(ctx, submission{text: "exact feedback"}) require.NoError(t, err) assert.False(t, confirmed) assert.Contains(t, stderr.String(), "Feedback not sent.") @@ -106,7 +193,7 @@ func TestConfirm(t *testing.T) { t.Run("no terminal", func(t *testing.T) { testutil.SetAgent(t, "test-agent") ctx := cmdio.InContext(t.Context(), cmdio.NewIO(t.Context(), flags.OutputText, strings.NewReader(""), io.Discard, io.Discard, "", "")) - confirmed, err := confirm(ctx, "exact feedback") + confirmed, err := confirm(ctx, submission{text: "exact feedback"}) assert.False(t, confirmed) assert.EqualError(t, err, "feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n"+ "Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it") @@ -128,7 +215,7 @@ func TestConfirmWithoutColorSupport(t *testing.T) { in := cmdio.FakeTTYReader(strings.NewReader("y\n")) ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputText, in, io.Discard, cmdio.FakeTTY(&stderr), "", "")) assert.False(t, cmdio.IsPromptSupported(ctx)) - confirmed, err := confirm(ctx, "exact feedback") + confirmed, err := confirm(ctx, submission{text: "exact feedback"}) require.NoError(t, err) assert.True(t, confirmed) }) diff --git a/cmd/root/user_agent_command.go b/cmd/root/user_agent_command.go index e0e38994303..cd5f3316cb8 100644 --- a/cmd/root/user_agent_command.go +++ b/cmd/root/user_agent_command.go @@ -14,7 +14,8 @@ import ( // See unit test [main.TestCommandsDontUseUnderscoreInName]. const commandSeparator = "_" -// CommandString returns the command hierarchy joined with underscores. +// CommandString walks up the command hierarchy of the specified +// command to build a string representing this hierarchy. func CommandString(cmd *cobra.Command) string { reversed := []string{cmd.Name()} cmd.VisitParents(func(p *cobra.Command) { diff --git a/libs/aitools/agents/hint.go b/libs/aitools/agents/hint.go index ad15d129c48..fd08fbb7ab9 100644 --- a/libs/aitools/agents/hint.go +++ b/libs/aitools/agents/hint.go @@ -284,11 +284,11 @@ func isCredentialHelperCommand(cmd *cobra.Command) bool { var hintExemptCommands = []string{ "aitools", "experimental aitools", - "feedback", "auth", "configure", "current-user me", "version", + "feedback", "completion", "help", cobra.ShellCompRequestCmd, diff --git a/libs/telemetry/protos/fast_feedback_log.go b/libs/telemetry/protos/fast_feedback_log.go index ff51d491ae5..98ead0b5abe 100644 --- a/libs/telemetry/protos/fast_feedback_log.go +++ b/libs/telemetry/protos/fast_feedback_log.go @@ -4,8 +4,9 @@ package protos // (proto/logs/frontend/docs-platform/fast_feedback_log.proto). It is the event the // in-product feedback widget records, so CLI feedback is triaged the same way. type FastFeedbackLog struct { - // ID of an event the feedback relates to. The CLI sets the command execution ID so - // triagers can find the matching requests in http_access_log. + // ID of an event the feedback relates to. The CLI sets the execution ID of the + // feedback command itself, which its requests also carry as cmd-exec-id in the + // user agent. ReferenceEventID string `json:"reference_event_id,omitempty"` // ES component the feedback is filed under (e.g. "DeveloperEcosystem.DatabricksCLI"). @@ -21,6 +22,8 @@ type FastFeedbackLog struct { UserAgent string `json:"user_agent,omitempty"` - // Feedback text written by the user. + // Feedback text written by the user. Unlike other telemetry it is user-authored + // by design: the user asked to send it, and confirms it first when an AI agent + // wrote it. Text string `json:"text,omitempty"` } From 3d978f7224b59e8af2623f7c9a49062445c759b7 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:38:40 +0000 Subject: [PATCH 05/20] Refuse unapproved agent feedback before authenticating An AI agent without --yes and without a terminal is now refused with the other input checks, before the workspace client is created. The prompt escapes every non-graphic character except newlines and tabs, so bidi overrides and zero-width characters cannot reorder or hide the message. Since main made prompts independent of NO_COLOR, the approval test no longer asserts how cmdio decides that; it checks that the approval works with NO_COLOR and TERM=dumb. Tests now force a real TCP reset for the single-attempt case, and cover sending with DATABRICKS_CLI_DISABLE_TELEMETRY set. The API client takes a retries argument instead of two wrapper constructors. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 25 ++++++++++- acceptance/cmd/feedback/script | 4 ++ cmd/feedback/feedback.go | 52 +++++++++++++++-------- cmd/feedback/feedback_test.go | 67 ++++++++++++++++++------------ cmd/root/command_spec.go | 3 +- cmd/root/command_spec_test.go | 6 ++- libs/telemetry/logger.go | 19 +++------ libs/telemetry/logger_test.go | 4 ++ 8 files changed, 119 insertions(+), 61 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index 17179c73de2..03b1198cb83 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -160,9 +160,9 @@ Thank you! Your feedback was sent. Reference: [UUID][8] } } -=== A broken bundle does not block feedback +=== Feedback is sent even with telemetry disabled ->>> withdir broken-bundle [CLI] feedback bundle config does not block feedback +>>> DATABRICKS_CLI_DISABLE_TELEMETRY=1 [CLI] feedback telemetry is disabled Thank you! Your feedback was sent. Reference: [UUID][10] >>> print_requests.py //telemetry-ext @@ -176,6 +176,27 @@ Thank you! Your feedback was sent. Reference: [UUID][10] "can_be_contacted": false, "ui_versions": "cli:[CLI_VERSION]", "user_agent": "cli/[CLI_VERSION] cmd/feedback", + "text": "telemetry is disabled" + } + } +} + +=== A broken bundle does not block feedback + +>>> withdir broken-bundle [CLI] feedback bundle config does not block feedback +Thank you! Your feedback was sent. Reference: [UUID][12] + +>>> print_requests.py //telemetry-ext +{ + "frontend_log_event_id": "[UUID][12]", + "entry": { + "fast_feedback_log": { + "reference_event_id": "[UUID][13]", + "parent_ui": "DeveloperEcosystem.DatabricksCLI", + "origin": "cli", + "can_be_contacted": false, + "ui_versions": "cli:[CLI_VERSION]", + "user_agent": "cli/[CLI_VERSION] cmd/feedback", "text": "bundle config does not block feedback" } } diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script index a3819a176d3..c8a208f97aa 100644 --- a/acceptance/cmd/feedback/script +++ b/acceptance/cmd/feedback/script @@ -39,6 +39,10 @@ title "An AI agent with --yes" trace AGENT=test-agent $CLI feedback --yes "hello" print_feedback +title "Feedback is sent even with telemetry disabled" +trace DATABRICKS_CLI_DISABLE_TELEMETRY=1 $CLI feedback "telemetry is disabled" +print_feedback + title "A broken bundle does not block feedback" trace withdir broken-bundle $CLI feedback "bundle config does not block feedback" print_feedback diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 7fa50183f37..1eb2a561542 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -105,6 +105,9 @@ approves it.` if err != nil { return err } + if err := checkApproval(cmd.Context(), opts.yes); err != nil { + return err + } cmd.SetContext(root.SkipLoadBundle(cmd.Context())) return root.MustWorkspaceClient(cmd, args) } @@ -116,7 +119,7 @@ approves it.` func newSubmission(cmd *cobra.Command, message string, opts *options) (submission, error) { // Feedback never loads the bundle, so a target would be ignored. for _, name := range []string{"target", "environment"} { - if f := cmd.Flag(name); f != nil && f.Changed { + if cmd.Flag(name).Changed { return submission{}, fmt.Errorf("--%s selects a bundle target, which feedback does not use; use --profile to choose the workspace", name) } } @@ -153,11 +156,11 @@ func send(ctx context.Context, s submission, yes bool) error { // The SDK adds the agent to HTTP request headers, but the feedback record // needs it too so triage can distinguish agent reports. ctx = useragent.InContext(ctx, useragent.AgentKey, provider) - if !yes { - confirmed, err := confirm(ctx, s) - if err != nil || !confirmed { - return err - } + } + if needsApproval(yes) { + confirmed, err := confirm(ctx, s) + if err != nil || !confirmed { + return err } } @@ -217,13 +220,26 @@ func topLevelName(cmd *cobra.Command) string { return cmd.Name() } +// needsApproval reports whether the user must approve the feedback before it is +// sent: an AI agent is running the CLI and did not pass --yes. +func needsApproval(yes bool) bool { + return useragent.AgentProvider() != "" && !yes +} + +// checkApproval returns an error when the user must approve the feedback but +// there is no terminal to ask in. It needs only the input, so it runs before +// authenticating. +func checkApproval(ctx context.Context, yes bool) error { + if !needsApproval(yes) || cmdio.IsInputTTY(ctx) { + return nil + } + return errors.New("feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + + agent.AgentNote("show the user the exact feedback message and pass --yes only after the user approves sending it")) +} + // confirm asks the user to approve feedback an AI agent is about to send. It // returns false without error if the user declines. func confirm(ctx context.Context, s submission) (bool, error) { - if !cmdio.IsInputTTY(ctx) { - return false, errors.New("feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + - agent.AgentNote("show the user the exact feedback message and pass --yes only after the user approves sending it")) - } cmdio.LogString(ctx, "The following feedback will be sent to Databricks:\n\n"+summary(s)) confirmed, err := cmdio.AskYesOrNo(ctx, "Send this feedback?") if err != nil { @@ -235,11 +251,11 @@ func confirm(ctx context.Context, s submission) (bool, error) { return confirmed, nil } -// summary returns everything the user approves: the message and the fields -// sent with it. +// summary returns the message and the choices sent with it, for the user to +// approve. func summary(s submission) string { var b strings.Builder - b.WriteString(escapeControl(s.text)) + b.WriteString(escapeNonGraphic(s.text)) b.WriteString("\n\n") if s.command != "" { fmt.Fprintf(&b, "Command: %s\n", s.command) @@ -255,12 +271,14 @@ func summary(s submission) string { return b.String() } -// escapeControl escapes control characters other than newlines and tabs, so -// that none of them can hide or overwrite part of the message in the terminal. -func escapeControl(text string) string { +// escapeNonGraphic escapes the characters that are not graphic, other than +// newlines and tabs: control and format characters such as escape sequences, +// bidi overrides and zero-width characters, which could hide, reorder or +// overwrite part of the message in the terminal. +func escapeNonGraphic(text string) string { var b strings.Builder for _, r := range text { - if unicode.IsControl(r) && r != '\n' && r != '\t' { + if !unicode.IsGraphic(r) && r != '\n' && r != '\t' { quoted := strconv.QuoteRune(r) b.WriteString(quoted[1 : len(quoted)-1]) continue diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index 921d81de1af..99acb621f99 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -162,6 +162,11 @@ func TestSummary(t *testing.T) { s: submission{text: "great\x1b[8m hidden\x1b[0m\r\u0085"}, want: `great\x1b[8m hidden\x1b[0m\r\u0085` + "\n\nDatabricks may contact you: no\n", }, + { + name: "format characters cannot reorder or hide text", + s: submission{text: "a\u202eb\u200bc\U000E0041 d\u00a0é"}, + want: `a\u202eb\u200bc\U000e0041 d` + "\u00a0é\n\nDatabricks may contact you: no\n", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -189,35 +194,45 @@ func TestConfirm(t *testing.T) { assert.False(t, confirmed) assert.Contains(t, stderr.String(), "Feedback not sent.") }) - - t.Run("no terminal", func(t *testing.T) { - testutil.SetAgent(t, "test-agent") - ctx := cmdio.InContext(t.Context(), cmdio.NewIO(t.Context(), flags.OutputText, strings.NewReader(""), io.Discard, io.Discard, "", "")) - confirmed, err := confirm(ctx, submission{text: "exact feedback"}) - assert.False(t, confirmed) - assert.EqualError(t, err, "feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n"+ - "Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it") - }) } -func TestConfirmWithoutColorSupport(t *testing.T) { - for _, tc := range []struct { - name string - key string - value string +func TestCheckApproval(t *testing.T) { + const refusal = "feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + + "Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it" + tests := []struct { + name string + agent string + yes bool + terminal bool + envKey string + envValue string + wantErr string }{ - {name: "no color", key: "NO_COLOR", value: "1"}, - {name: "dumb terminal", key: "TERM", value: "dumb"}, - } { - t.Run(tc.name, func(t *testing.T) { - ctx := env.Set(t.Context(), tc.key, tc.value) - var stderr bytes.Buffer - in := cmdio.FakeTTYReader(strings.NewReader("y\n")) - ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputText, in, io.Discard, cmdio.FakeTTY(&stderr), "", "")) - assert.False(t, cmdio.IsPromptSupported(ctx)) - confirmed, err := confirm(ctx, submission{text: "exact feedback"}) - require.NoError(t, err) - assert.True(t, confirmed) + {name: "human without a terminal"}, + {name: "agent with --yes", agent: "test-agent", yes: true}, + {name: "agent with a terminal", agent: "test-agent", terminal: true}, + {name: "agent with a terminal and NO_COLOR", agent: "test-agent", terminal: true, envKey: "NO_COLOR", envValue: "1"}, + {name: "agent with a dumb terminal", agent: "test-agent", terminal: true, envKey: "TERM", envValue: "dumb"}, + {name: "agent without a terminal", agent: "test-agent", wantErr: refusal}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + testutil.SetAgent(t, tt.agent) + ctx := t.Context() + if tt.envKey != "" { + ctx = env.Set(ctx, tt.envKey, tt.envValue) + } + var in io.Reader = strings.NewReader("") + if tt.terminal { + in = cmdio.FakeTTYReader(in) + } + ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputText, in, io.Discard, cmdio.FakeTTY(io.Discard), "", "")) + err := checkApproval(ctx, tt.yes) + if tt.wantErr != "" { + assert.EqualError(t, err, tt.wantErr) + return + } + assert.NoError(t, err) }) } } diff --git a/cmd/root/command_spec.go b/cmd/root/command_spec.go index 4f8f42bb910..8839cc1b503 100644 --- a/cmd/root/command_spec.go +++ b/cmd/root/command_spec.go @@ -27,7 +27,8 @@ type CommandSpec struct { Deprecated string // NeedsFullTree builds every top-level command before running this command, - // for commands that resolve other command paths while they run. + // for commands that resolve other command paths while they run. Only + // top-level specs added to a [Tree] use it. NeedsFullTree bool Build func(ctx context.Context, cmd *cobra.Command) diff --git a/cmd/root/command_spec_test.go b/cmd/root/command_spec_test.go index c95da2749f6..353579df968 100644 --- a/cmd/root/command_spec_test.go +++ b/cmd/root/command_spec_test.go @@ -10,12 +10,14 @@ import ( "github.com/stretchr/testify/assert" ) -// Every command-valued spec field must reach the command, and changing it while -// building must panic. This fails if such a field is added but not handled. +// Every spec field must reach the command, and changing it while building +// must panic. This fails if a field is added to Spec but not handled. func TestSpecFields(t *testing.T) { specType := reflect.TypeFor[root.CommandSpec]() for i := range specType.NumField() { field := specType.Field(i) + // NeedsFullTree tells the tree how to build the command; it is not a + // command field. if field.Name == "Build" || field.Name == "NeedsFullTree" { continue } diff --git a/libs/telemetry/logger.go b/libs/telemetry/logger.go index fa3785c2b23..d4d6cae403b 100644 --- a/libs/telemetry/logger.go +++ b/libs/telemetry/logger.go @@ -146,7 +146,7 @@ func Upload(ctx context.Context, ec protos.ExecutionContext) error { } apiClient, err := within(ctx, &l.running, func() (*client.DatabricksClient, error) { - return newAPIClient(cmdctx.ConfigUsed(ctx), &l.running) + return newAPIClient(cmdctx.ConfigUsed(ctx), &l.running, true) }) if err != nil { return fmt.Errorf("failed to create API client: %w", err) @@ -284,16 +284,9 @@ func within[T any](ctx context.Context, running *atomic.Int32, f func() (T, erro } // newAPIClient is client.New with authenticate as the auth visitor. The SDK -// runs the auth visitor on every request, including its own retries. -func newAPIClient(cfg *config.Config, running *atomic.Int32) (*client.DatabricksClient, error) { - return newAPIClientWithRetries(cfg, running, true) -} - -func newSingleAttemptAPIClient(cfg *config.Config, running *atomic.Int32) (*client.DatabricksClient, error) { - return newAPIClientWithRetries(cfg, running, false) -} - -func newAPIClientWithRetries(cfg *config.Config, running *atomic.Int32, allowRetries bool) (*client.DatabricksClient, error) { +// runs the auth visitor on every request, including its own retries. Without +// retries, the client makes a single attempt. +func newAPIClient(cfg *config.Config, running *atomic.Int32, retries bool) (*client.DatabricksClient, error) { clientCfg, err := config.HTTPClientConfigFromConfig(cfg) if err != nil { return nil, err @@ -301,7 +294,7 @@ func newAPIClientWithRetries(cfg *config.Config, running *atomic.Int32, allowRet clientCfg.AuthVisitor = func(r *http.Request) error { return authenticate(r, cfg, running) } - if !allowRetries { + if !retries { // The SDK retries some connection errors regardless of ErrorRetriable, // so a minimal retry window is needed to stop after the first attempt. clientCfg.ErrorRetriable = func(context.Context, error) bool { return false } @@ -337,7 +330,7 @@ func Send(ctx context.Context, cfg *config.Config, logs []protos.FrontendLog) er var running atomic.Int32 apiClient, err := within(ctx, &running, func() (*client.DatabricksClient, error) { - return newSingleAttemptAPIClient(cfg, &running) + return newAPIClient(cfg, &running, false) }) if err != nil { return fmt.Errorf("failed to create API client: %w", err) diff --git a/libs/telemetry/logger_test.go b/libs/telemetry/logger_test.go index 345314bd7ff..ece61d05f53 100644 --- a/libs/telemetry/logger_test.go +++ b/libs/telemetry/logger_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "net" "net/http" "sync/atomic" "testing" @@ -504,6 +505,9 @@ func TestSendMakesOneAttempt(t *testing.T) { if !assert.NoError(t, err) { return } + // A zero linger makes Close send a TCP reset, which the SDK + // retries unless retries are off. + assert.NoError(t, conn.(*net.TCPConn).SetLinger(0)) assert.NoError(t, conn.Close()) }) } else { From b2fe9800801ab7538b93710d714633f36e92f3a3 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:55:00 +0000 Subject: [PATCH 06/20] Escape hidden Unicode in the feedback prompt and require visible text The prompt also escapes variation selectors and the other code points Unicode marks as default ignorable, which terminals draw as nothing and which can carry hidden text. A message made only of such characters is rejected, so blank-looking feedback cannot be filed. An acceptance case now checks that an agent without --yes is refused before authenticating. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 9 ++++++-- acceptance/cmd/feedback/script | 1 + cmd/feedback/feedback.go | 34 +++++++++++++++++++++--------- cmd/feedback/feedback_test.go | 11 ++++++++-- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index 03b1198cb83..f9f3824e9a6 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -68,7 +68,7 @@ Thank you! Your feedback was sent. Reference: [UUID][4] === Validation errors send nothing >>> [CLI] feedback -Error: feedback message must contain 1 to 10000 characters +Error: feedback message must contain 1 to 10000 characters, including a visible one >>> [CLI] feedback Error: accepts 1 arg(s), received 0 @@ -109,7 +109,12 @@ Error: --target selects a bundle target, which feedback does not use; use --prof === Input errors are reported before authenticating >>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN [CLI] feedback -Error: feedback message must contain 1 to 10000 characters +Error: feedback message must contain 1 to 10000 characters, including a visible one + +>>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN AGENT=test-agent [CLI] feedback hello +Error: feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message + +Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it === General CLI feedback diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script index c8a208f97aa..4b4d5fd6e62 100644 --- a/acceptance/cmd/feedback/script +++ b/acceptance/cmd/feedback/script @@ -27,6 +27,7 @@ musterr trace $CLI feedback --target dev "hello" title "Input errors are reported before authenticating" musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI feedback " " +musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN AGENT=test-agent $CLI feedback "hello" title "General CLI feedback" trace $CLI feedback "the CLI should support ..." diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 1eb2a561542..6b49c249116 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -192,8 +192,8 @@ func send(ctx context.Context, s submission, yes bool) error { func validateMessage(message string) (string, error) { text := strings.TrimSpace(message) - if text == "" || utf8.RuneCountInString(text) > messageLimit { - return "", fmt.Errorf("feedback message must contain 1 to %d characters", messageLimit) + if utf8.RuneCountInString(text) > messageLimit || !strings.ContainsFunc(text, visible) { + return "", fmt.Errorf("feedback message must contain 1 to %d characters, including a visible one", messageLimit) } return text, nil } @@ -255,7 +255,7 @@ func confirm(ctx context.Context, s submission) (bool, error) { // approve. func summary(s submission) string { var b strings.Builder - b.WriteString(escapeNonGraphic(s.text)) + b.WriteString(escapeInvisible(s.text)) b.WriteString("\n\n") if s.command != "" { fmt.Fprintf(&b, "Command: %s\n", s.command) @@ -271,15 +271,13 @@ func summary(s submission) string { return b.String() } -// escapeNonGraphic escapes the characters that are not graphic, other than -// newlines and tabs: control and format characters such as escape sequences, -// bidi overrides and zero-width characters, which could hide, reorder or -// overwrite part of the message in the terminal. -func escapeNonGraphic(text string) string { +// escapeInvisible escapes the runes that invisible reports, so none of them can +// hide, reorder or overwrite part of the message in the terminal. +func escapeInvisible(text string) string { var b strings.Builder for _, r := range text { - if !unicode.IsGraphic(r) && r != '\n' && r != '\t' { - quoted := strconv.QuoteRune(r) + if invisible(r) { + quoted := strconv.QuoteRuneToASCII(r) b.WriteString(quoted[1 : len(quoted)-1]) continue } @@ -287,3 +285,19 @@ func escapeNonGraphic(text string) string { } return b.String() } + +// invisible reports whether a terminal would not show r as written, other than +// a newline or tab: characters that are not graphic, such as escape sequences, +// bidi overrides and zero-width characters, and the variation selectors and +// fillers that Unicode marks as default ignorable, which can carry hidden text. +func invisible(r rune) bool { + if r == '\n' || r == '\t' { + return false + } + return !unicode.IsGraphic(r) || unicode.In(r, unicode.Variation_Selector, unicode.Other_Default_Ignorable_Code_Point) +} + +// visible reports whether r shows up as a mark in the terminal. +func visible(r rune) bool { + return !unicode.IsSpace(r) && !invisible(r) +} diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index 99acb621f99..99f566f8f83 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -25,13 +25,15 @@ func TestValidateMessage(t *testing.T) { {name: "trimmed", message: " feedback\n", want: "feedback"}, {name: "maximum runes", message: strings.Repeat("é", messageLimit), want: strings.Repeat("é", messageLimit)}, {name: "blank", message: " \n", wantErr: true}, + {name: "only invisible characters", message: "\u200b\ufe0f\u3164", wantErr: true}, + {name: "visible after invisible", message: "\u200bx", want: "\u200bx"}, {name: "too long", message: strings.Repeat("x", messageLimit+1), wantErr: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := validateMessage(tt.message) if tt.wantErr { - assert.EqualError(t, err, "feedback message must contain 1 to 10000 characters") + assert.EqualError(t, err, "feedback message must contain 1 to 10000 characters, including a visible one") return } require.NoError(t, err) @@ -115,7 +117,7 @@ func TestNewSubmission(t *testing.T) { }, {name: "zero run ID", opts: options{runID: "0"}, message: "x", wantErr: "--run-id must be a positive job run ID"}, {name: "non-numeric run ID", opts: options{runID: "abc"}, message: "x", wantErr: "--run-id must be a positive job run ID"}, - {name: "blank message", message: " ", wantErr: "feedback message must contain 1 to 10000 characters"}, + {name: "blank message", message: " ", wantErr: "feedback message must contain 1 to 10000 characters, including a visible one"}, {name: "unknown command", opts: options{command: "nope"}, message: "x", wantErr: `unknown command "nope"` + commandHint}, { name: "target", flags: []string{"--target", "dev"}, message: "x", @@ -167,6 +169,11 @@ func TestSummary(t *testing.T) { s: submission{text: "a\u202eb\u200bc\U000E0041 d\u00a0é"}, want: `a\u202eb\u200bc\U000e0041 d` + "\u00a0é\n\nDatabricks may contact you: no\n", }, + { + name: "variation selectors and fillers cannot carry hidden text", + s: submission{text: "ok👍\ufe0f\U000E0100\u3164\u034f e\u0301"}, + want: `ok👍\ufe0f\U000e0100\u3164\u034f ` + "e\u0301\n\nDatabricks may contact you: no\n", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From e5a2f404486d1a6eae8206ac75067169169f279a Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:11:39 +0000 Subject: [PATCH 07/20] Exit non-zero when the user declines agent feedback Declining the approval prompt now returns ErrAlreadyPrinted after "Feedback not sent.", so an agent that ran the command can tell a decline from a send by its exit code. The message is indented in the prompt so its lines cannot pass for the fields listed below it. A test drives send through the prompt: an approval sends exactly the message shown, a decline sends nothing. Co-authored-by: Isaac --- cmd/feedback/feedback.go | 11 ++++- cmd/feedback/feedback_test.go | 84 ++++++++++++++++++++++++++++++----- libs/aitools/agents/hint.go | 2 +- 3 files changed, 84 insertions(+), 13 deletions(-) diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 6b49c249116..7810b680f5a 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -159,9 +159,14 @@ func send(ctx context.Context, s submission, yes bool) error { } if needsApproval(yes) { confirmed, err := confirm(ctx, s) - if err != nil || !confirmed { + if err != nil { return err } + if !confirmed { + // confirm told the user; the exit code tells the agent that ran the + // command that nothing was sent. + return root.ErrAlreadyPrinted + } } id := uuid.New().String() @@ -255,7 +260,9 @@ func confirm(ctx context.Context, s submission) (bool, error) { // approve. func summary(s submission) string { var b strings.Builder - b.WriteString(escapeInvisible(s.text)) + // Indent the message so that none of its lines can pass for one of the + // fields below it. + b.WriteString(" " + strings.ReplaceAll(escapeInvisible(s.text), "\n", "\n ")) b.WriteString("\n\n") if s.command != "" { fmt.Fprintf(&b, "Command: %s\n", s.command) diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index 99f566f8f83..cf423ef35a7 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -2,14 +2,22 @@ package feedback import ( "bytes" + "encoding/json" "io" + "net/http" "strings" "testing" + "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/internal/testutil" + "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/telemetry" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -33,7 +41,7 @@ func TestValidateMessage(t *testing.T) { t.Run(tt.name, func(t *testing.T) { got, err := validateMessage(tt.message) if tt.wantErr { - assert.EqualError(t, err, "feedback message must contain 1 to 10000 characters, including a visible one") + assert.EqualError(t, err, messageError) return } require.NoError(t, err) @@ -42,6 +50,11 @@ func TestValidateMessage(t *testing.T) { } } +const ( + messageError = "feedback message must contain 1 to 10000 characters, including a visible one" + runIDError = "--run-id must be a positive job run ID" +) + const commandHint = `; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments` // newTestFeedbackCommand returns the feedback command under a root with the @@ -115,9 +128,9 @@ func TestNewSubmission(t *testing.T) { origin: "bundle_deploy", runID: "123", canBeContacted: true, }, }, - {name: "zero run ID", opts: options{runID: "0"}, message: "x", wantErr: "--run-id must be a positive job run ID"}, - {name: "non-numeric run ID", opts: options{runID: "abc"}, message: "x", wantErr: "--run-id must be a positive job run ID"}, - {name: "blank message", message: " ", wantErr: "feedback message must contain 1 to 10000 characters, including a visible one"}, + {name: "zero run ID", opts: options{runID: "0"}, message: "x", wantErr: runIDError}, + {name: "non-numeric run ID", opts: options{runID: "abc"}, message: "x", wantErr: runIDError}, + {name: "blank message", message: " ", wantErr: messageError}, {name: "unknown command", opts: options{command: "nope"}, message: "x", wantErr: `unknown command "nope"` + commandHint}, { name: "target", flags: []string{"--target", "dev"}, message: "x", @@ -152,27 +165,32 @@ func TestSummary(t *testing.T) { { name: "message only", s: submission{text: "line one\n\tline two"}, - want: "line one\n\tline two\n\nDatabricks may contact you: no\n", + want: " line one\n \tline two\n\nDatabricks may contact you: no\n", }, { name: "all fields", s: submission{text: "slow", command: "air run", runID: "42", canBeContacted: true}, - want: "slow\n\nCommand: air run\nRun ID: 42\nDatabricks may contact you: yes\n", + want: " slow\n\nCommand: air run\nRun ID: 42\nDatabricks may contact you: yes\n", + }, + { + name: "message lines cannot pass for fields", + s: submission{text: "slow\nDatabricks may contact you: no", canBeContacted: true}, + want: " slow\n Databricks may contact you: no\n\nDatabricks may contact you: yes\n", }, { name: "control characters cannot hide text", s: submission{text: "great\x1b[8m hidden\x1b[0m\r\u0085"}, - want: `great\x1b[8m hidden\x1b[0m\r\u0085` + "\n\nDatabricks may contact you: no\n", + want: ` great\x1b[8m hidden\x1b[0m\r\u0085` + "\n\nDatabricks may contact you: no\n", }, { name: "format characters cannot reorder or hide text", s: submission{text: "a\u202eb\u200bc\U000E0041 d\u00a0é"}, - want: `a\u202eb\u200bc\U000e0041 d` + "\u00a0é\n\nDatabricks may contact you: no\n", + want: ` a\u202eb\u200bc\U000e0041 d` + "\u00a0é\n\nDatabricks may contact you: no\n", }, { name: "variation selectors and fillers cannot carry hidden text", s: submission{text: "ok👍\ufe0f\U000E0100\u3164\u034f e\u0301"}, - want: `ok👍\ufe0f\U000e0100\u3164\u034f ` + "e\u0301\n\nDatabricks may contact you: no\n", + want: ` ok👍\ufe0f\U000e0100\u3164\u034f ` + "e\u0301\n\nDatabricks may contact you: no\n", }, } for _, tt := range tests { @@ -189,7 +207,7 @@ func TestConfirm(t *testing.T) { confirmed, err := confirm(ctx, submission{text: "exact feedback"}) require.NoError(t, err) assert.True(t, confirmed) - assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\nexact feedback\n\nDatabricks may contact you: no\n\n") + assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\n exact feedback\n\nDatabricks may contact you: no\n\n") assert.Contains(t, stderr.String(), "Send this feedback? [y/N]: ") }) @@ -243,3 +261,49 @@ func TestCheckApproval(t *testing.T) { }) } } + +func TestSendAsksBeforeSending(t *testing.T) { + tests := []struct { + name string + answer string + wantSent []string + wantErr error + }{ + {name: "approved", answer: "y\n", wantSent: []string{"exact feedback"}}, + {name: "declined", answer: "n\n", wantErr: root.ErrAlreadyPrinted}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + testutil.SetAgent(t, "test-agent") + server := testserver.New(t) + var sent []string + server.Handle(http.MethodPost, "/telemetry-ext", func(req testserver.Request) any { + var body telemetry.RequestBody + require.NoError(t, json.Unmarshal(req.Body, &body)) + for _, raw := range body.ProtoLogs { + var entry protos.FrontendLog + require.NoError(t, json.Unmarshal([]byte(raw), &entry)) + sent = append(sent, entry.Entry.FastFeedbackLog.Text) + } + return telemetry.ResponseBody{NumProtoSuccess: int64(len(body.ProtoLogs))} + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + var stdout, stderr bytes.Buffer + ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader(tt.answer), &stdout, &stderr)) + ctx = cmdctx.SetWorkspaceClient(ctx, w) + ctx = cmdctx.GenerateExecId(ctx) + err = send(ctx, submission{text: "exact feedback", component: defaultComponent, origin: "cli"}, false) + + assert.ErrorIs(t, err, tt.wantErr) + assert.Equal(t, tt.wantSent, sent) + assert.Contains(t, stderr.String(), " exact feedback\n") + if tt.wantErr == nil { + assert.Contains(t, stdout.String(), "Thank you! Your feedback was sent.") + } else { + assert.Empty(t, stdout.String()) + } + }) + } +} diff --git a/libs/aitools/agents/hint.go b/libs/aitools/agents/hint.go index fd08fbb7ab9..059f5da6eab 100644 --- a/libs/aitools/agents/hint.go +++ b/libs/aitools/agents/hint.go @@ -288,7 +288,7 @@ var hintExemptCommands = []string{ "configure", "current-user me", "version", - "feedback", + "feedback", // A one-shot report: the agent has no task to apply the skills to. "completion", "help", cobra.ShellCompRequestCmd, From e9c636615546e5528ba2115ff370e83e3c0f99ef Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:48:28 +0000 Subject: [PATCH 08/20] Fix the feedback test on Windows and explain an unquoted message TestSendAsksBeforeSending cleared the environment before starting its test server; on Windows, sockets then fail to initialize without SystemRoot. The server and client are now created first, and its handler uses assert, since it runs on the server's goroutine. An unquoted message arrives as several arguments; the error now says to quote it and shows the command. Send's authentication error no longer mentions telemetry, which confused users who disabled it. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 3 +++ acceptance/cmd/feedback/script | 1 + cmd/feedback/feedback.go | 8 +++++++- cmd/feedback/feedback_test.go | 13 +++++++++---- libs/telemetry/logger.go | 2 +- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index f9f3824e9a6..96773e98337 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -94,6 +94,9 @@ Global Flags: -t, --target string bundle target to use (if applicable) +>>> [CLI] feedback deploy is slow +Error: the message must be one argument, but received 3; quote it, for example: databricks feedback "deploy is slow" + >>> [CLI] feedback --command no-such-command hello Error: unknown command "no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script index 4b4d5fd6e62..7345e508735 100644 --- a/acceptance/cmd/feedback/script +++ b/acceptance/cmd/feedback/script @@ -20,6 +20,7 @@ print_feedback title "Validation errors send nothing" musterr trace $CLI feedback " " musterr trace $CLI feedback +musterr trace $CLI feedback deploy is slow musterr trace $CLI feedback --command "no-such-command" "hello" musterr trace $CLI feedback --command "bundle no-such-command" "hello" musterr trace $CLI feedback --run-id abc "hello" diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 7810b680f5a..58aeba52a31 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -90,7 +90,13 @@ is attached. Show the user the exact message and pass --yes only after the user approves it.` cmd.Example = ` databricks feedback --command "bundle deploy" "deploy should print which resources changed" databricks feedback --command "air run" --run-id 123 "logs omitted the failure reason"` - cmd.Args = root.ExactArgs(1) + cmd.Args = func(cmd *cobra.Command, args []string) error { + // An unquoted message arrives as several arguments. + if len(args) > 1 { + return fmt.Errorf("the message must be one argument, but received %d; quote it, for example: databricks feedback %q", len(args), strings.Join(args, " ")) + } + return root.ExactArgs(1)(cmd, args) + } var opts options cmd.Flags().StringVar(&opts.command, "command", "", `CLI command the feedback is about, for example "bundle deploy"`) diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index cf423ef35a7..8e9b79558da 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -274,21 +274,26 @@ func TestSendAsksBeforeSending(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - testutil.SetAgent(t, "test-agent") + // Start the server and the client before SetAgent clears the + // environment: on Windows, sockets need SystemRoot. server := testserver.New(t) var sent []string server.Handle(http.MethodPost, "/telemetry-ext", func(req testserver.Request) any { + // The handler runs on the server's goroutine, so it must not + // call require. var body telemetry.RequestBody - require.NoError(t, json.Unmarshal(req.Body, &body)) + assert.NoError(t, json.Unmarshal(req.Body, &body)) for _, raw := range body.ProtoLogs { var entry protos.FrontendLog - require.NoError(t, json.Unmarshal([]byte(raw), &entry)) - sent = append(sent, entry.Entry.FastFeedbackLog.Text) + if assert.NoError(t, json.Unmarshal([]byte(raw), &entry)) { + sent = append(sent, entry.Entry.FastFeedbackLog.Text) + } } return telemetry.ResponseBody{NumProtoSuccess: int64(len(body.ProtoLogs))} }) w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) require.NoError(t, err) + testutil.SetAgent(t, "test-agent") var stdout, stderr bytes.Buffer ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader(tt.answer), &stdout, &stderr)) diff --git a/libs/telemetry/logger.go b/libs/telemetry/logger.go index d4d6cae403b..e2feb320a9c 100644 --- a/libs/telemetry/logger.go +++ b/libs/telemetry/logger.go @@ -313,7 +313,7 @@ func authenticate(r *http.Request, cfg *config.Config, running *atomic.Int32) er return struct{}{}, cfg.Authenticate(authReq) }) if err != nil { - return fmt.Errorf("authenticating telemetry upload: %w", err) + return fmt.Errorf("authenticating upload: %w", err) } r.Header = authReq.Header return nil From 73781ba327d9dca9cb5b5dd6302d0237f635c55f Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:07:46 +0000 Subject: [PATCH 09/20] Point at an unquoted --command and show the workspace in the prompt When --command is given without quotes, its second word arrives as an argument. If --command plus that word name a real command, the error now says to quote --command, instead of suggesting a message that folds the subcommand in. Suggested commands are quoted with shellquote.BashArg, so pasting one cannot expand $ or backticks. The approval prompt also shows the workspace host the feedback is filed in, so the user sees where an agent's --profile sends it. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 5 ++- acceptance/cmd/feedback/script | 1 + cmd/feedback/feedback.go | 46 +++++++++++++++------- cmd/feedback/feedback_test.go | 61 +++++++++++++++++++++++++----- 4 files changed, 88 insertions(+), 25 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index 96773e98337..2cd937be5bf 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -95,7 +95,10 @@ Global Flags: >>> [CLI] feedback deploy is slow -Error: the message must be one argument, but received 3; quote it, for example: databricks feedback "deploy is slow" +Error: the message must be one argument, but received 3; quote it, for example: databricks feedback 'deploy is slow' + +>>> [CLI] feedback --command bundle deploy deploy is slow +Error: --command takes the command as one argument; quote it, for example: databricks feedback --command 'bundle deploy' 'deploy is slow' >>> [CLI] feedback --command no-such-command hello Error: unknown command "no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script index 7345e508735..a524dedbbaf 100644 --- a/acceptance/cmd/feedback/script +++ b/acceptance/cmd/feedback/script @@ -21,6 +21,7 @@ title "Validation errors send nothing" musterr trace $CLI feedback " " musterr trace $CLI feedback musterr trace $CLI feedback deploy is slow +musterr trace $CLI feedback --command bundle deploy "deploy is slow" musterr trace $CLI feedback --command "no-such-command" "hello" musterr trace $CLI feedback --command "bundle no-such-command" "hello" musterr trace $CLI feedback --run-id abc "hello" diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 58aeba52a31..928d53a1598 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -16,6 +16,7 @@ import ( "github.com/databricks/cli/libs/agent" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go/useragent" @@ -90,13 +91,6 @@ is attached. Show the user the exact message and pass --yes only after the user approves it.` cmd.Example = ` databricks feedback --command "bundle deploy" "deploy should print which resources changed" databricks feedback --command "air run" --run-id 123 "logs omitted the failure reason"` - cmd.Args = func(cmd *cobra.Command, args []string) error { - // An unquoted message arrives as several arguments. - if len(args) > 1 { - return fmt.Errorf("the message must be one argument, but received %d; quote it, for example: databricks feedback %q", len(args), strings.Join(args, " ")) - } - return root.ExactArgs(1)(cmd, args) - } var opts options cmd.Flags().StringVar(&opts.command, "command", "", `CLI command the feedback is about, for example "bundle deploy"`) @@ -104,6 +98,10 @@ approves it.` cmd.Flags().BoolVar(&opts.canBeContacted, "can-be-contacted", false, "Allow Databricks to contact you about this feedback") cmd.Flags().BoolVarP(&opts.yes, "yes", "y", false, "Confirm sending feedback written by an AI agent") + cmd.Args = func(cmd *cobra.Command, args []string) error { + return checkArgs(cmd, opts.command, args) + } + var s submission cmd.PreRunE = func(cmd *cobra.Command, args []string) error { var err error @@ -122,6 +120,24 @@ approves it.` } } +// checkArgs requires the message as one argument. Several arguments mean that +// the message, or a --command with several words, was not quoted; the error +// shows the command with the quotes. +func checkArgs(cmd *cobra.Command, command string, args []string) error { + if len(args) <= 1 { + return root.ExactArgs(1)(cmd, args) + } + if command != "" { + path := append(strings.Fields(command), args[0]) + if target, rest, err := cmd.Root().Find(path); err == nil && len(rest) == 0 && target != cmd.Root() { + return fmt.Errorf("--command takes the command as one argument; quote it, for example: databricks feedback --command %s %s", + shellquote.BashArg(strings.Join(path, " ")), shellquote.BashArg(strings.Join(args[1:], " "))) + } + } + return fmt.Errorf("the message must be one argument, but received %d; quote it, for example: databricks feedback %s", + len(args), shellquote.BashArg(strings.Join(args, " "))) +} + func newSubmission(cmd *cobra.Command, message string, opts *options) (submission, error) { // Feedback never loads the bundle, so a target would be ignored. for _, name := range []string{"target", "environment"} { @@ -163,8 +179,9 @@ func send(ctx context.Context, s submission, yes bool) error { // needs it too so triage can distinguish agent reports. ctx = useragent.InContext(ctx, useragent.AgentKey, provider) } + cfg := cmdctx.WorkspaceClient(ctx).Config if needsApproval(yes) { - confirmed, err := confirm(ctx, s) + confirmed, err := confirm(ctx, s, cfg.Host) if err != nil { return err } @@ -178,7 +195,7 @@ func send(ctx context.Context, s submission, yes bool) error { id := uuid.New().String() sendCtx, cancel := context.WithTimeout(ctx, sendTimeout) defer cancel() - err := telemetry.Send(sendCtx, cmdctx.WorkspaceClient(ctx).Config, []protos.FrontendLog{{ + err := telemetry.Send(sendCtx, cfg, []protos.FrontendLog{{ FrontendLogEventID: id, Entry: protos.FrontendLogEntry{ FastFeedbackLog: &protos.FastFeedbackLog{ @@ -250,8 +267,8 @@ func checkApproval(ctx context.Context, yes bool) error { // confirm asks the user to approve feedback an AI agent is about to send. It // returns false without error if the user declines. -func confirm(ctx context.Context, s submission) (bool, error) { - cmdio.LogString(ctx, "The following feedback will be sent to Databricks:\n\n"+summary(s)) +func confirm(ctx context.Context, s submission, workspace string) (bool, error) { + cmdio.LogString(ctx, "The following feedback will be sent to Databricks:\n\n"+summary(s, workspace)) confirmed, err := cmdio.AskYesOrNo(ctx, "Send this feedback?") if err != nil { return false, err @@ -262,14 +279,15 @@ func confirm(ctx context.Context, s submission) (bool, error) { return confirmed, nil } -// summary returns the message and the choices sent with it, for the user to -// approve. -func summary(s submission) string { +// summary returns the message, the workspace it is filed in and the choices +// sent with it, for the user to approve. +func summary(s submission, workspace string) string { var b strings.Builder // Indent the message so that none of its lines can pass for one of the // fields below it. b.WriteString(" " + strings.ReplaceAll(escapeInvisible(s.text), "\n", "\n ")) b.WriteString("\n\n") + fmt.Fprintf(&b, "Workspace: %s\n", workspace) if s.command != "" { fmt.Fprintf(&b, "Command: %s\n", s.command) } diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index 8e9b79558da..facf19751e1 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -55,6 +55,8 @@ const ( runIDError = "--run-id must be a positive job run ID" ) +const testWorkspace = "https://workspace.test" + const commandHint = `; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments` // newTestFeedbackCommand returns the feedback command under a root with the @@ -76,6 +78,45 @@ func newTestFeedbackCommand() *cobra.Command { return feedbackCmd } +func TestCheckArgs(t *testing.T) { + tests := []struct { + name string + command string + args []string + wantErr string + }{ + {name: "one message", args: []string{"deploy is slow"}}, + {name: "no message", wantErr: "accepts 1 arg(s), received 0"}, + { + name: "unquoted message", + args: []string{"costs", "$HOME", "or", "`whoami`"}, + wantErr: "the message must be one argument, but received 4; quote it, for example: databricks feedback 'costs $HOME or `whoami`'", + }, + { + name: "unquoted --command", + command: "bundle", + args: []string{"deploy", "deploy is slow"}, + wantErr: "--command takes the command as one argument; quote it, for example: databricks feedback --command 'bundle deploy' 'deploy is slow'", + }, + { + name: "words that are not a subcommand", + command: "bundle", + args: []string{"nope", "slow"}, + wantErr: "the message must be one argument, but received 2; quote it, for example: databricks feedback 'nope slow'", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := checkArgs(newTestFeedbackCommand(), tt.command, tt.args) + if tt.wantErr == "" { + assert.NoError(t, err) + return + } + assert.ErrorContains(t, err, tt.wantErr) + }) + } +} + func TestResolveCommand(t *testing.T) { feedbackCmd := newTestFeedbackCommand() @@ -165,37 +206,37 @@ func TestSummary(t *testing.T) { { name: "message only", s: submission{text: "line one\n\tline two"}, - want: " line one\n \tline two\n\nDatabricks may contact you: no\n", + want: " line one\n \tline two\n\nWorkspace: https://workspace.test\nDatabricks may contact you: no\n", }, { name: "all fields", s: submission{text: "slow", command: "air run", runID: "42", canBeContacted: true}, - want: " slow\n\nCommand: air run\nRun ID: 42\nDatabricks may contact you: yes\n", + want: " slow\n\nWorkspace: https://workspace.test\nCommand: air run\nRun ID: 42\nDatabricks may contact you: yes\n", }, { name: "message lines cannot pass for fields", s: submission{text: "slow\nDatabricks may contact you: no", canBeContacted: true}, - want: " slow\n Databricks may contact you: no\n\nDatabricks may contact you: yes\n", + want: " slow\n Databricks may contact you: no\n\nWorkspace: https://workspace.test\nDatabricks may contact you: yes\n", }, { name: "control characters cannot hide text", s: submission{text: "great\x1b[8m hidden\x1b[0m\r\u0085"}, - want: ` great\x1b[8m hidden\x1b[0m\r\u0085` + "\n\nDatabricks may contact you: no\n", + want: ` great\x1b[8m hidden\x1b[0m\r\u0085` + "\n\nWorkspace: https://workspace.test\nDatabricks may contact you: no\n", }, { name: "format characters cannot reorder or hide text", s: submission{text: "a\u202eb\u200bc\U000E0041 d\u00a0é"}, - want: ` a\u202eb\u200bc\U000e0041 d` + "\u00a0é\n\nDatabricks may contact you: no\n", + want: ` a\u202eb\u200bc\U000e0041 d` + "\u00a0é\n\nWorkspace: https://workspace.test\nDatabricks may contact you: no\n", }, { name: "variation selectors and fillers cannot carry hidden text", s: submission{text: "ok👍\ufe0f\U000E0100\u3164\u034f e\u0301"}, - want: ` ok👍\ufe0f\U000e0100\u3164\u034f ` + "e\u0301\n\nDatabricks may contact you: no\n", + want: ` ok👍\ufe0f\U000e0100\u3164\u034f ` + "e\u0301\n\nWorkspace: https://workspace.test\nDatabricks may contact you: no\n", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.want, summary(tt.s)) + assert.Equal(t, tt.want, summary(tt.s, testWorkspace)) }) } } @@ -204,17 +245,17 @@ func TestConfirm(t *testing.T) { t.Run("accepted", func(t *testing.T) { var stderr bytes.Buffer ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader("y\n"), io.Discard, &stderr)) - confirmed, err := confirm(ctx, submission{text: "exact feedback"}) + confirmed, err := confirm(ctx, submission{text: "exact feedback"}, testWorkspace) require.NoError(t, err) assert.True(t, confirmed) - assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\n exact feedback\n\nDatabricks may contact you: no\n\n") + assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\n exact feedback\n\nWorkspace: https://workspace.test\nDatabricks may contact you: no\n\n") assert.Contains(t, stderr.String(), "Send this feedback? [y/N]: ") }) t.Run("declined", func(t *testing.T) { var stderr bytes.Buffer ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader("\n"), io.Discard, &stderr)) - confirmed, err := confirm(ctx, submission{text: "exact feedback"}) + confirmed, err := confirm(ctx, submission{text: "exact feedback"}, testWorkspace) require.NoError(t, err) assert.False(t, confirmed) assert.Contains(t, stderr.String(), "Feedback not sent.") From 0fca192b5d9a3af0c7046ab02a4360d7d80205a1 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:25:25 +0000 Subject: [PATCH 10/20] Say what to quote instead of printing a command to paste The argument errors now say what to put quotes around. A printed command dropped the user's other flags (a pasted one could file feedback under the default profile) and its quoting only worked in POSIX shells. The prompt names the workspace with its ID on a unified host, where the host alone does not identify it. checkArgs and resolveCommand share findCommand. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 4 ++-- cmd/feedback/feedback.go | 38 +++++++++++++++++++++--------- cmd/feedback/feedback_test.go | 24 ++++++++++++++++--- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index 2cd937be5bf..bbb321959fd 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -95,10 +95,10 @@ Global Flags: >>> [CLI] feedback deploy is slow -Error: the message must be one argument, but received 3; quote it, for example: databricks feedback 'deploy is slow' +Error: the message must be one argument, but received 3; put quotes around the whole message >>> [CLI] feedback --command bundle deploy deploy is slow -Error: --command takes the command as one argument; quote it, for example: databricks feedback --command 'bundle deploy' 'deploy is slow' +Error: --command takes the command as one argument; put quotes around "bundle deploy" >>> [CLI] feedback --command no-such-command hello Error: unknown command "no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 928d53a1598..97d9efbef68 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -16,9 +16,10 @@ import ( "github.com/databricks/cli/libs/agent" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" - "github.com/databricks/cli/libs/shellquote" + "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/useragent" "github.com/spf13/cobra" ) @@ -121,21 +122,20 @@ approves it.` } // checkArgs requires the message as one argument. Several arguments mean that -// the message, or a --command with several words, was not quoted; the error -// shows the command with the quotes. +// the message, or a --command with several words, was not quoted. The error +// says what to quote rather than printing a command to paste, which would drop +// the user's other flags and depend on their shell's quoting. func checkArgs(cmd *cobra.Command, command string, args []string) error { if len(args) <= 1 { return root.ExactArgs(1)(cmd, args) } if command != "" { path := append(strings.Fields(command), args[0]) - if target, rest, err := cmd.Root().Find(path); err == nil && len(rest) == 0 && target != cmd.Root() { - return fmt.Errorf("--command takes the command as one argument; quote it, for example: databricks feedback --command %s %s", - shellquote.BashArg(strings.Join(path, " ")), shellquote.BashArg(strings.Join(args[1:], " "))) + if _, ok := findCommand(cmd, path); ok { + return fmt.Errorf("--command takes the command as one argument; put quotes around %q", strings.Join(path, " ")) } } - return fmt.Errorf("the message must be one argument, but received %d; quote it, for example: databricks feedback %s", - len(args), shellquote.BashArg(strings.Join(args, " "))) + return fmt.Errorf("the message must be one argument, but received %d; put quotes around the whole message", len(args)) } func newSubmission(cmd *cobra.Command, message string, opts *options) (submission, error) { @@ -181,7 +181,7 @@ func send(ctx context.Context, s submission, yes bool) error { } cfg := cmdctx.WorkspaceClient(ctx).Config if needsApproval(yes) { - confirmed, err := confirm(ctx, s, cfg.Host) + confirmed, err := confirm(ctx, s, workspaceLabel(cfg)) if err != nil { return err } @@ -230,8 +230,8 @@ func resolveCommand(cmd *cobra.Command, command string) (string, string, error) if command == "" { return defaultComponent, "cli", nil } - target, rest, err := cmd.Root().Find(strings.Fields(command)) - if err != nil || len(rest) > 0 || target == cmd.Root() { + target, ok := findCommand(cmd, strings.Fields(command)) + if !ok { return "", "", fmt.Errorf(`unknown command %q; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, command) } component := defaultComponent @@ -241,6 +241,13 @@ func resolveCommand(cmd *cobra.Command, command string) (string, string, error) return component, root.CommandString(target), nil } +// findCommand returns the command at path, and false if path does not name a +// command, has words left over, or is empty. +func findCommand(cmd *cobra.Command, path []string) (*cobra.Command, bool) { + target, rest, err := cmd.Root().Find(path) + return target, err == nil && len(rest) == 0 && target != cmd.Root() +} + func topLevelName(cmd *cobra.Command) string { for cmd.HasParent() && cmd.Parent().HasParent() { cmd = cmd.Parent() @@ -279,6 +286,15 @@ func confirm(ctx context.Context, s submission, workspace string) (bool, error) return confirmed, nil } +// workspaceLabel names the workspace feedback is filed in. A unified host +// serves several workspaces, so there the host alone does not identify it. +func workspaceLabel(cfg *config.Config) string { + if id := cfg.WorkspaceID; id != "" && id != profile.WorkspaceIDNone { + return fmt.Sprintf("%s (workspace ID %s)", cfg.Host, id) + } + return cfg.Host +} + // summary returns the message, the workspace it is filed in and the choices // sent with it, for the user to approve. func summary(s submission, workspace string) string { diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index facf19751e1..d5adf031f2a 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -18,6 +18,7 @@ import ( "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/cli/libs/testserver" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -90,19 +91,19 @@ func TestCheckArgs(t *testing.T) { { name: "unquoted message", args: []string{"costs", "$HOME", "or", "`whoami`"}, - wantErr: "the message must be one argument, but received 4; quote it, for example: databricks feedback 'costs $HOME or `whoami`'", + wantErr: "the message must be one argument, but received 4; put quotes around the whole message", }, { name: "unquoted --command", command: "bundle", args: []string{"deploy", "deploy is slow"}, - wantErr: "--command takes the command as one argument; quote it, for example: databricks feedback --command 'bundle deploy' 'deploy is slow'", + wantErr: `--command takes the command as one argument; put quotes around "bundle deploy"`, }, { name: "words that are not a subcommand", command: "bundle", args: []string{"nope", "slow"}, - wantErr: "the message must be one argument, but received 2; quote it, for example: databricks feedback 'nope slow'", + wantErr: "the message must be one argument, but received 2; put quotes around the whole message", }, } for _, tt := range tests { @@ -117,6 +118,23 @@ func TestCheckArgs(t *testing.T) { } } +func TestWorkspaceLabel(t *testing.T) { + tests := []struct { + name string + workspaceID string + want string + }{ + {name: "workspace host", want: "https://workspace.test"}, + {name: "unified host", workspaceID: "123", want: "https://workspace.test (workspace ID 123)"}, + {name: "no workspace sentinel", workspaceID: "none", want: "https://workspace.test"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, workspaceLabel(&config.Config{Host: "https://workspace.test", WorkspaceID: tt.workspaceID})) + }) + } +} + func TestResolveCommand(t *testing.T) { feedbackCmd := newTestFeedbackCommand() From 128e8fb8607ac45cb38ea3d2d2e45829b8d8c60b Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:45:35 +0000 Subject: [PATCH 11/20] Say when feedback was definitely not sent A 4xx from the server, or a response that accepted none of the logs, means nothing was stored, so the error now says the feedback was not sent instead of that it could not be confirmed. telemetry.Send returns a NotAcceptedError for the second case. The unquoted --command hint follows the arguments for as long as they still name a command, so it names the whole path, for example "bundle deployment bind". The tree looks the spec up once. Co-authored-by: Isaac --- cmd/feedback/feedback.go | 29 +++++++++++++++++++++++-- cmd/feedback/feedback_test.go | 41 ++++++++++++++++++++++++++++++++--- cmd/root/tree.go | 4 ++-- libs/telemetry/logger.go | 12 +++++++++- 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 97d9efbef68..622c996429b 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "slices" "strconv" "strings" "time" @@ -19,6 +20,7 @@ import ( "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/useragent" "github.com/spf13/cobra" @@ -130,8 +132,18 @@ func checkArgs(cmd *cobra.Command, command string, args []string) error { return root.ExactArgs(1)(cmd, args) } if command != "" { - path := append(strings.Fields(command), args[0]) - if _, ok := findCommand(cmd, path); ok { + // Extend --command with the arguments for as long as it still names a + // command, so the error names the whole command. + path := strings.Fields(command) + extended := false + for _, arg := range args { + next := append(slices.Clone(path), arg) + if _, ok := findCommand(cmd, next); !ok { + break + } + path, extended = next, true + } + if extended { return fmt.Errorf("--command takes the command as one argument; put quotes around %q", strings.Join(path, " ")) } } @@ -210,6 +222,9 @@ func send(ctx context.Context, s submission, yes bool) error { }, }}) if err != nil { + if notSent(err) { + return fmt.Errorf("feedback %s was not sent: %w", id, err) + } // The upload may have been accepted before the error, so retrying can // file the same feedback twice. return fmt.Errorf("feedback %s could not be confirmed as sent: %w", id, err) @@ -218,6 +233,16 @@ func send(ctx context.Context, s submission, yes bool) error { return cmdio.RenderWithTemplate(ctx, receipt{Received: true, FeedbackID: id}, "", receiptTemplate) } +// notSent reports whether err shows that the server stored nothing: it rejected +// the request, or accepted none of its logs. +func notSent(err error) bool { + if _, ok := errors.AsType[*telemetry.NotAcceptedError](err); ok { + return true + } + apiErr, ok := errors.AsType[*apierr.APIError](err) + return ok && apiErr.StatusCode >= 400 && apiErr.StatusCode < 500 +} + func validateMessage(message string) (string, error) { text := strings.TrimSpace(message) if utf8.RuneCountInString(text) > messageLimit || !strings.ContainsFunc(text, visible) { diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index d5adf031f2a..3cbc29942d1 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -3,6 +3,8 @@ package feedback import ( "bytes" "encoding/json" + "errors" + "fmt" "io" "net/http" "strings" @@ -18,6 +20,7 @@ import ( "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/cli/libs/testserver" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" @@ -68,11 +71,19 @@ func newTestFeedbackCommand() *cobra.Command { rootCmd.PersistentFlags().String("environment", "", "") feedbackCmd := &cobra.Command{Use: "feedback"} rootCmd.AddCommand(feedbackCmd) - for _, path := range []string{"air run", "bundle deploy", "version"} { + for _, path := range []string{"air run", "bundle deploy", "bundle deployment bind", "version"} { parent := rootCmd for name := range strings.FieldsSeq(path) { - child := &cobra.Command{Use: name} - parent.AddCommand(child) + var child *cobra.Command + for _, c := range parent.Commands() { + if c.Name() == name { + child = c + } + } + if child == nil { + child = &cobra.Command{Use: name} + parent.AddCommand(child) + } parent = child } } @@ -99,6 +110,12 @@ func TestCheckArgs(t *testing.T) { args: []string{"deploy", "deploy is slow"}, wantErr: `--command takes the command as one argument; put quotes around "bundle deploy"`, }, + { + name: "unquoted nested --command", + command: "bundle", + args: []string{"deployment", "bind", "bind fails"}, + wantErr: `--command takes the command as one argument; put quotes around "bundle deployment bind"`, + }, { name: "words that are not a subcommand", command: "bundle", @@ -118,6 +135,24 @@ func TestCheckArgs(t *testing.T) { } } +func TestNotSent(t *testing.T) { + tests := []struct { + name string + err error + want bool + }{ + {name: "nothing accepted", err: fmt.Errorf("wrapped: %w", &telemetry.NotAcceptedError{Accepted: 0, Sent: 1}), want: true}, + {name: "rejected", err: &apierr.APIError{StatusCode: 403}, want: true}, + {name: "server error", err: &apierr.APIError{StatusCode: 500}}, + {name: "connection reset", err: errors.New("connection reset by peer")}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, notSent(tt.err)) + }) + } +} + func TestWorkspaceLabel(t *testing.T) { tests := []struct { name string diff --git a/cmd/root/tree.go b/cmd/root/tree.go index c70fa124d08..8a9e5cfe95e 100644 --- a/cmd/root/tree.go +++ b/cmd/root/tree.go @@ -118,14 +118,14 @@ func (t *Tree) buildForArgs(ctx context.Context, args []string) { if err != nil { return } - _, pending := t.pending[cmd] + spec, pending := t.pending[cmd] switch { case cmd.Parent() == t.cmd && (cmd.Name() == "help" || isCompletionRequest(cmd)): // Help and completion resolve commands from their own arguments, the // way cobra parses them. Building everything avoids parsing them here. t.buildAll(ctx) case pending: - if t.pending[cmd].NeedsFullTree { + if spec.NeedsFullTree { t.buildAll(ctx) } else { t.replace(ctx, cmd) diff --git a/libs/telemetry/logger.go b/libs/telemetry/logger.go index e2feb320a9c..9242546ad84 100644 --- a/libs/telemetry/logger.go +++ b/libs/telemetry/logger.go @@ -341,11 +341,21 @@ func Send(ctx context.Context, cfg *config.Config, logs []protos.FrontendLog) er return err } if resp.NumProtoSuccess < int64(len(protoLogs)) { - return fmt.Errorf("only %d of %d logs were accepted", resp.NumProtoSuccess, len(protoLogs)) + return &NotAcceptedError{Accepted: resp.NumProtoSuccess, Sent: int64(len(protoLogs))} } return nil } +// NotAcceptedError is returned by Send when the server accepted fewer logs than +// were sent, so the others were not stored. +type NotAcceptedError struct { + Accepted, Sent int64 +} + +func (e *NotAcceptedError) Error() string { + return fmt.Sprintf("only %d of %d logs were accepted", e.Accepted, e.Sent) +} + func marshalLogs(logs []protos.FrontendLog) ([]string, error) { protoLogs := make([]string, len(logs)) for i, log := range logs { From bfe01ad1da3922e8d7634d90233af4c6adf6e517 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:03:26 +0000 Subject: [PATCH 12/20] Route --command by its first word instead of building the command tree --command is now plain text: its first word picks the owning team (bundle, air), and the origin joins its words with underscores. Resolving a nested command path needed the full tree, so CommandSpec.NeedsFullTree, its tree tests and the CommandString export are removed and cmd/root matches main again, apart from the fuzz test's list of hand-written commands. An unquoted --command now gets the same hint as an unquoted message. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 10 +--- acceptance/cmd/feedback/script | 2 - cmd/feedback/feedback.go | 76 ++++++++--------------------- cmd/feedback/feedback_test.go | 66 ++++--------------------- cmd/root/command_spec.go | 5 -- cmd/root/command_spec_test.go | 4 +- cmd/root/root.go | 2 +- cmd/root/tree.go | 10 +--- cmd/root/tree_test.go | 22 --------- cmd/root/user_agent_command.go | 6 +-- cmd/root/user_agent_command_test.go | 6 +-- 11 files changed, 41 insertions(+), 168 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index bbb321959fd..bb46c39086d 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -95,16 +95,10 @@ Global Flags: >>> [CLI] feedback deploy is slow -Error: the message must be one argument, but received 3; put quotes around the whole message +Error: the message must be one argument, but received 3; put quotes around the whole message, and around --command if it has several words >>> [CLI] feedback --command bundle deploy deploy is slow -Error: --command takes the command as one argument; put quotes around "bundle deploy" - ->>> [CLI] feedback --command no-such-command hello -Error: unknown command "no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments - ->>> [CLI] feedback --command bundle no-such-command hello -Error: unknown command "bundle no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments +Error: the message must be one argument, but received 2; put quotes around the whole message, and around --command if it has several words >>> [CLI] feedback --run-id abc hello Error: --run-id must be a positive job run ID diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script index a524dedbbaf..24e0996c89b 100644 --- a/acceptance/cmd/feedback/script +++ b/acceptance/cmd/feedback/script @@ -22,8 +22,6 @@ musterr trace $CLI feedback " " musterr trace $CLI feedback musterr trace $CLI feedback deploy is slow musterr trace $CLI feedback --command bundle deploy "deploy is slow" -musterr trace $CLI feedback --command "no-such-command" "hello" -musterr trace $CLI feedback --command "bundle no-such-command" "hello" musterr trace $CLI feedback --run-id abc "hello" musterr trace $CLI feedback --target dev "hello" diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 622c996429b..4e1c6e0af96 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "slices" "strconv" "strings" "time" @@ -67,11 +66,10 @@ type submission struct { // Spec declares the feedback command. var Spec = root.CommandSpec{ - Use: "feedback MESSAGE", - Short: "Send feedback about the Databricks CLI to Databricks", - GroupID: "development", - NeedsFullTree: true, - Build: buildCommand, + Use: "feedback MESSAGE", + Short: "Send feedback about the Databricks CLI to Databricks", + GroupID: "development", + Build: buildCommand, } func buildCommand(ctx context.Context, cmd *cobra.Command) { @@ -101,9 +99,7 @@ approves it.` cmd.Flags().BoolVar(&opts.canBeContacted, "can-be-contacted", false, "Allow Databricks to contact you about this feedback") cmd.Flags().BoolVarP(&opts.yes, "yes", "y", false, "Confirm sending feedback written by an AI agent") - cmd.Args = func(cmd *cobra.Command, args []string) error { - return checkArgs(cmd, opts.command, args) - } + cmd.Args = checkArgs var s submission cmd.PreRunE = func(cmd *cobra.Command, args []string) error { @@ -127,27 +123,11 @@ approves it.` // the message, or a --command with several words, was not quoted. The error // says what to quote rather than printing a command to paste, which would drop // the user's other flags and depend on their shell's quoting. -func checkArgs(cmd *cobra.Command, command string, args []string) error { - if len(args) <= 1 { - return root.ExactArgs(1)(cmd, args) - } - if command != "" { - // Extend --command with the arguments for as long as it still names a - // command, so the error names the whole command. - path := strings.Fields(command) - extended := false - for _, arg := range args { - next := append(slices.Clone(path), arg) - if _, ok := findCommand(cmd, next); !ok { - break - } - path, extended = next, true - } - if extended { - return fmt.Errorf("--command takes the command as one argument; put quotes around %q", strings.Join(path, " ")) - } +func checkArgs(cmd *cobra.Command, args []string) error { + if len(args) > 1 { + return fmt.Errorf("the message must be one argument, but received %d; put quotes around the whole message, and around --command if it has several words", len(args)) } - return fmt.Errorf("the message must be one argument, but received %d; put quotes around the whole message", len(args)) + return root.ExactArgs(1)(cmd, args) } func newSubmission(cmd *cobra.Command, message string, opts *options) (submission, error) { @@ -161,10 +141,7 @@ func newSubmission(cmd *cobra.Command, message string, opts *options) (submissio if err != nil { return submission{}, err } - component, origin, err := resolveCommand(cmd, opts.command) - if err != nil { - return submission{}, err - } + component, origin := resolveCommand(opts.command) s := submission{ text: text, command: strings.Join(strings.Fields(opts.command), " "), @@ -251,33 +228,20 @@ func validateMessage(message string) (string, error) { return text, nil } -func resolveCommand(cmd *cobra.Command, command string) (string, string, error) { - if command == "" { - return defaultComponent, "cli", nil - } - target, ok := findCommand(cmd, strings.Fields(command)) - if !ok { - return "", "", fmt.Errorf(`unknown command %q; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, command) +// resolveCommand returns the component and origin for --command. Its first word +// picks the owning team, so "bundle deploy" reaches bundles without the CLI +// building every command to check the path. The origin joins the words with +// underscores, as the CLI names commands elsewhere (bundle_deploy). +func resolveCommand(command string) (string, string) { + words := strings.Fields(command) + if len(words) == 0 { + return defaultComponent, "cli" } component := defaultComponent - if productComponent, ok := productComponents[topLevelName(target)]; ok { + if productComponent, ok := productComponents[words[0]]; ok { component = productComponent } - return component, root.CommandString(target), nil -} - -// findCommand returns the command at path, and false if path does not name a -// command, has words left over, or is empty. -func findCommand(cmd *cobra.Command, path []string) (*cobra.Command, bool) { - target, rest, err := cmd.Root().Find(path) - return target, err == nil && len(rest) == 0 && target != cmd.Root() -} - -func topLevelName(cmd *cobra.Command) string { - for cmd.HasParent() && cmd.Parent().HasParent() { - cmd = cmd.Parent() - } - return cmd.Name() + return component, strings.Join(words, "_") } // needsApproval reports whether the user must approve the feedback before it is diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index 3cbc29942d1..5abc82f57b0 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -61,71 +61,34 @@ const ( const testWorkspace = "https://workspace.test" -const commandHint = `; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments` - // newTestFeedbackCommand returns the feedback command under a root with the -// root's bundle target flags and a few commands --command can name. +// root's bundle target flags. func newTestFeedbackCommand() *cobra.Command { rootCmd := &cobra.Command{Use: "databricks"} rootCmd.PersistentFlags().String("target", "", "") rootCmd.PersistentFlags().String("environment", "", "") feedbackCmd := &cobra.Command{Use: "feedback"} rootCmd.AddCommand(feedbackCmd) - for _, path := range []string{"air run", "bundle deploy", "bundle deployment bind", "version"} { - parent := rootCmd - for name := range strings.FieldsSeq(path) { - var child *cobra.Command - for _, c := range parent.Commands() { - if c.Name() == name { - child = c - } - } - if child == nil { - child = &cobra.Command{Use: name} - parent.AddCommand(child) - } - parent = child - } - } return feedbackCmd } func TestCheckArgs(t *testing.T) { tests := []struct { name string - command string args []string wantErr string }{ {name: "one message", args: []string{"deploy is slow"}}, {name: "no message", wantErr: "accepts 1 arg(s), received 0"}, { - name: "unquoted message", - args: []string{"costs", "$HOME", "or", "`whoami`"}, - wantErr: "the message must be one argument, but received 4; put quotes around the whole message", - }, - { - name: "unquoted --command", - command: "bundle", - args: []string{"deploy", "deploy is slow"}, - wantErr: `--command takes the command as one argument; put quotes around "bundle deploy"`, - }, - { - name: "unquoted nested --command", - command: "bundle", - args: []string{"deployment", "bind", "bind fails"}, - wantErr: `--command takes the command as one argument; put quotes around "bundle deployment bind"`, - }, - { - name: "words that are not a subcommand", - command: "bundle", - args: []string{"nope", "slow"}, - wantErr: "the message must be one argument, but received 2; put quotes around the whole message", + name: "unquoted message or --command", + args: []string{"deploy", "is", "slow"}, + wantErr: "the message must be one argument, but received 3; put quotes around the whole message, and around --command if it has several words", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := checkArgs(newTestFeedbackCommand(), tt.command, tt.args) + err := checkArgs(newTestFeedbackCommand(), tt.args) if tt.wantErr == "" { assert.NoError(t, err) return @@ -171,33 +134,23 @@ func TestWorkspaceLabel(t *testing.T) { } func TestResolveCommand(t *testing.T) { - feedbackCmd := newTestFeedbackCommand() - tests := []struct { name string command string wantComponent string wantOrigin string - wantErr string }{ {name: "general", wantComponent: defaultComponent, wantOrigin: "cli"}, + {name: "blank", command: " ", wantComponent: defaultComponent, wantOrigin: "cli"}, {name: "air subcommand", command: "air run", wantComponent: "ModelTraining.AIRuntime", wantOrigin: "air_run"}, {name: "bundle group", command: "bundle", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle"}, {name: "bundle subcommand", command: "bundle deploy", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_deploy"}, - {name: "other command", command: "version", wantComponent: defaultComponent, wantOrigin: "version"}, - {name: "unknown top level", command: "unknown", wantErr: `unknown command "unknown"` + commandHint}, - {name: "unknown subcommand", command: "bundle unknown", wantErr: `unknown command "bundle unknown"` + commandHint}, - {name: "with arguments", command: "bundle deploy -t dev", wantErr: `unknown command "bundle deploy -t dev"` + commandHint}, - {name: "blank", command: " ", wantErr: `unknown command " "` + commandHint}, + {name: "any text after a product's name", command: "bundle not-a-command", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_not-a-command"}, + {name: "other command", command: "jobs list", wantComponent: defaultComponent, wantOrigin: "jobs_list"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - component, origin, err := resolveCommand(feedbackCmd, tt.command) - if tt.wantErr != "" { - assert.EqualError(t, err, tt.wantErr) - return - } - require.NoError(t, err) + component, origin := resolveCommand(tt.command) assert.Equal(t, tt.wantComponent, component) assert.Equal(t, tt.wantOrigin, origin) }) @@ -225,7 +178,6 @@ func TestNewSubmission(t *testing.T) { {name: "zero run ID", opts: options{runID: "0"}, message: "x", wantErr: runIDError}, {name: "non-numeric run ID", opts: options{runID: "abc"}, message: "x", wantErr: runIDError}, {name: "blank message", message: " ", wantErr: messageError}, - {name: "unknown command", opts: options{command: "nope"}, message: "x", wantErr: `unknown command "nope"` + commandHint}, { name: "target", flags: []string{"--target", "dev"}, message: "x", wantErr: "--target selects a bundle target, which feedback does not use; use --profile to choose the workspace", diff --git a/cmd/root/command_spec.go b/cmd/root/command_spec.go index 8839cc1b503..028a0e4af2d 100644 --- a/cmd/root/command_spec.go +++ b/cmd/root/command_spec.go @@ -26,11 +26,6 @@ type CommandSpec struct { Hidden bool Deprecated string - // NeedsFullTree builds every top-level command before running this command, - // for commands that resolve other command paths while they run. Only - // top-level specs added to a [Tree] use it. - NeedsFullTree bool - Build func(ctx context.Context, cmd *cobra.Command) } diff --git a/cmd/root/command_spec_test.go b/cmd/root/command_spec_test.go index 353579df968..cbbb1d1519d 100644 --- a/cmd/root/command_spec_test.go +++ b/cmd/root/command_spec_test.go @@ -16,9 +16,7 @@ func TestSpecFields(t *testing.T) { specType := reflect.TypeFor[root.CommandSpec]() for i := range specType.NumField() { field := specType.Field(i) - // NeedsFullTree tells the tree how to build the command; it is not a - // command field. - if field.Name == "Build" || field.Name == "NeedsFullTree" { + if field.Name == "Build" { continue } t.Run(field.Name, func(t *testing.T) { diff --git a/cmd/root/root.go b/cmd/root/root.go index 2f8943f6b60..a988c22d285 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -204,7 +204,7 @@ Stack Trace: exitCode = 1 } - commandStr := CommandString(cmd) + commandStr := commandString(cmd) ctx = cmd.Context() telemetryErr := telemetry.Upload(cmd.Context(), protos.ExecutionContext{ diff --git a/cmd/root/tree.go b/cmd/root/tree.go index 8a9e5cfe95e..a1e3324b7e8 100644 --- a/cmd/root/tree.go +++ b/cmd/root/tree.go @@ -30,10 +30,8 @@ func (t *Tree) Add(specs ...CommandSpec) { // Copy the fields so that the caller changing them later can't make // the placeholder and the built command differ. build := spec.Build - needsFullTree := spec.NeedsFullTree spec = specFields(spec.command()) spec.Build = build - spec.NeedsFullTree = needsFullTree placeholder := spec.command() // Cobra only lists a command in help and completion if it is // runnable or has subcommands; a placeholder has no subcommands. @@ -118,18 +116,14 @@ func (t *Tree) buildForArgs(ctx context.Context, args []string) { if err != nil { return } - spec, pending := t.pending[cmd] + _, pending := t.pending[cmd] switch { case cmd.Parent() == t.cmd && (cmd.Name() == "help" || isCompletionRequest(cmd)): // Help and completion resolve commands from their own arguments, the // way cobra parses them. Building everything avoids parsing them here. t.buildAll(ctx) case pending: - if spec.NeedsFullTree { - t.buildAll(ctx) - } else { - t.replace(ctx, cmd) - } + t.replace(ctx, cmd) } } diff --git a/cmd/root/tree_test.go b/cmd/root/tree_test.go index 33692a6c368..f3042b59ffc 100644 --- a/cmd/root/tree_test.go +++ b/cmd/root/tree_test.go @@ -109,25 +109,3 @@ func TestAddCopiesSpecFields(t *testing.T) { aliases[0] = "new" require.NoError(t, r.Execute(t.Context(), []string{"old", "--help"})) } - -func TestRunningCommandCanResolveNestedCommand(t *testing.T) { - var built []string - r := newTestRoot(t, &built) - var resolved string - var remaining []string - r.Add(root.CommandSpec{ - Use: "inspect", - NeedsFullTree: true, - Build: func(ctx context.Context, cmd *cobra.Command) { - cmd.RunE = func(cmd *cobra.Command, args []string) error { - target, rest, err := cmd.Root().Find([]string{"one", "child"}) - resolved = target.CommandPath() - remaining = rest - return err - } - }, - }) - require.NoError(t, r.Execute(t.Context(), []string{"inspect"})) - assert.Equal(t, "databricks one child", resolved) - assert.Empty(t, remaining) -} diff --git a/cmd/root/user_agent_command.go b/cmd/root/user_agent_command.go index cd5f3316cb8..c1a9adf27cd 100644 --- a/cmd/root/user_agent_command.go +++ b/cmd/root/user_agent_command.go @@ -14,9 +14,9 @@ import ( // See unit test [main.TestCommandsDontUseUnderscoreInName]. const commandSeparator = "_" -// CommandString walks up the command hierarchy of the specified +// commandString walks up the command hierarchy of the specified // command to build a string representing this hierarchy. -func CommandString(cmd *cobra.Command) string { +func commandString(cmd *cobra.Command) string { reversed := []string{cmd.Name()} cmd.VisitParents(func(p *cobra.Command) { if !p.HasParent() { @@ -30,5 +30,5 @@ func CommandString(cmd *cobra.Command) string { } func withCommandInUserAgent(ctx context.Context, cmd *cobra.Command) context.Context { - return useragent.InContext(ctx, "cmd", CommandString(cmd)) + return useragent.InContext(ctx, "cmd", commandString(cmd)) } diff --git a/cmd/root/user_agent_command_test.go b/cmd/root/user_agent_command_test.go index 7b6907e2dc2..9d76d2011cb 100644 --- a/cmd/root/user_agent_command_test.go +++ b/cmd/root/user_agent_command_test.go @@ -24,9 +24,9 @@ func TestWithCommandInUserAgent(t *testing.T) { root.AddCommand(hello) hello.AddCommand(world) - assert.Equal(t, "root", CommandString(root)) - assert.Equal(t, "hello", CommandString(hello)) - assert.Equal(t, "hello_world", CommandString(world)) + assert.Equal(t, "root", commandString(root)) + assert.Equal(t, "hello", commandString(hello)) + assert.Equal(t, "hello_world", commandString(world)) ctx := withCommandInUserAgent(t.Context(), world) From 4c0c9fd2896024c0671a38716bf55ef021ed5df8 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:19:06 +0000 Subject: [PATCH 13/20] Check --command against the top-level commands and command-name characters Plain-text --command let flags, paths and argument values reach origin, let a "databricks" prefix or a typo route feedback to the wrong team, and let terminal control sequences rewrite the approval prompt. Its first word must now be a top-level command, found on the root without building the tree, and every word may only use lowercase letters, digits and dashes. An alias resolves to the command's name. The note for agents, and the help, also ask to show the user the flags that go with the message, such as --can-be-contacted. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 13 ++++++-- acceptance/cmd/feedback/script | 3 ++ cmd/feedback/feedback.go | 51 ++++++++++++++++++++++++------ cmd/feedback/feedback_test.go | 37 ++++++++++++++++------ 4 files changed, 83 insertions(+), 21 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index bb46c39086d..966ab25d867 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -100,6 +100,15 @@ Error: the message must be one argument, but received 3; put quotes around the w >>> [CLI] feedback --command bundle deploy deploy is slow Error: the message must be one argument, but received 2; put quotes around the whole message, and around --command if it has several words +>>> [CLI] feedback --command no-such-command hello +Error: unknown command "no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments + +>>> [CLI] feedback --command databricks bundle deploy hello +Error: unknown command "databricks bundle deploy"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments + +>>> [CLI] feedback --command bundle deploy -t dev hello +Error: unknown command "bundle deploy -t dev"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments + >>> [CLI] feedback --run-id abc hello Error: --run-id must be a positive job run ID @@ -114,7 +123,7 @@ Error: feedback message must contain 1 to 10000 characters, including a visible >>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN AGENT=test-agent [CLI] feedback hello Error: feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message -Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it +Note for AI agents (test-agent): show the user the exact feedback message and the flags you pass with it, such as --can-be-contacted, and pass --yes only after the user approves sending it === General CLI feedback @@ -142,7 +151,7 @@ Thank you! Your feedback was sent. Reference: [UUID][6] >>> AGENT=test-agent [CLI] feedback hello Error: feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message -Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it +Note for AI agents (test-agent): show the user the exact feedback message and the flags you pass with it, such as --can-be-contacted, and pass --yes only after the user approves sending it === An AI agent with --yes diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script index 24e0996c89b..c789287d920 100644 --- a/acceptance/cmd/feedback/script +++ b/acceptance/cmd/feedback/script @@ -22,6 +22,9 @@ musterr trace $CLI feedback " " musterr trace $CLI feedback musterr trace $CLI feedback deploy is slow musterr trace $CLI feedback --command bundle deploy "deploy is slow" +musterr trace $CLI feedback --command "no-such-command" "hello" +musterr trace $CLI feedback --command "databricks bundle deploy" "hello" +musterr trace $CLI feedback --command "bundle deploy -t dev" "hello" musterr trace $CLI feedback --run-id abc "hello" musterr trace $CLI feedback --target dev "hello" diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 4e1c6e0af96..4599cec0844 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -88,8 +88,8 @@ DATABRICKS_CLI_DISABLE_TELEMETRY is set, because you asked to send it. AI agents: submit feedback only with the user's approval. When an agent is detected, the command asks for confirmation, or requires --yes when no terminal -is attached. Show the user the exact message and pass --yes only after the user -approves it.` +is attached. Show the user the exact message and the flags you pass with it, +such as --can-be-contacted, and pass --yes only after the user approves it.` cmd.Example = ` databricks feedback --command "bundle deploy" "deploy should print which resources changed" databricks feedback --command "air run" --run-id 123 "logs omitted the failure reason"` @@ -141,7 +141,10 @@ func newSubmission(cmd *cobra.Command, message string, opts *options) (submissio if err != nil { return submission{}, err } - component, origin := resolveCommand(opts.command) + component, origin, err := resolveCommand(cmd, opts.command) + if err != nil { + return submission{}, err + } s := submission{ text: text, command: strings.Join(strings.Fields(opts.command), " "), @@ -211,7 +214,7 @@ func send(ctx context.Context, s submission, yes bool) error { } // notSent reports whether err shows that the server stored nothing: it rejected -// the request, or accepted none of its logs. +// the request, or did not accept the feedback. func notSent(err error) bool { if _, ok := errors.AsType[*telemetry.NotAcceptedError](err); ok { return true @@ -229,19 +232,47 @@ func validateMessage(message string) (string, error) { } // resolveCommand returns the component and origin for --command. Its first word -// picks the owning team, so "bundle deploy" reaches bundles without the CLI -// building every command to check the path. The origin joins the words with +// must be a top-level command and picks the owning team, so "bundle deploy" +// reaches bundles. The rest is not checked against the command tree, which would +// mean building every command; instead every word is limited to the characters +// of command names, so flags, paths, arguments and terminal control sequences +// cannot reach origin or the approval prompt. The origin joins the words with // underscores, as the CLI names commands elsewhere (bundle_deploy). -func resolveCommand(command string) (string, string) { +func resolveCommand(cmd *cobra.Command, command string) (string, string, error) { words := strings.Fields(command) if len(words) == 0 { - return defaultComponent, "cli" + return defaultComponent, "cli", nil } + // Top-level commands are registered on the root before they are built. + top, rest, err := cmd.Root().Find(words[:1]) + valid := err == nil && len(rest) == 0 && top != cmd.Root() + for _, word := range words { + valid = valid && isCommandWord(word) + } + if !valid { + return "", "", fmt.Errorf(`unknown command %q; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, command) + } + // An alias resolves to the command's name. + words[0] = top.Name() component := defaultComponent if productComponent, ok := productComponents[words[0]]; ok { component = productComponent } - return component, strings.Join(words, "_") + return component, strings.Join(words, "_"), nil +} + +// isCommandWord reports whether word can be part of a command path: lowercase +// letters, digits and dashes, not starting with a dash. +func isCommandWord(word string) bool { + if word == "" || word[0] == '-' { + return false + } + for _, r := range word { + if (r < 'a' || r > 'z') && (r < '0' || r > '9') && r != '-' { + return false + } + } + return true } // needsApproval reports whether the user must approve the feedback before it is @@ -258,7 +289,7 @@ func checkApproval(ctx context.Context, yes bool) error { return nil } return errors.New("feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + - agent.AgentNote("show the user the exact feedback message and pass --yes only after the user approves sending it")) + agent.AgentNote("show the user the exact feedback message and the flags you pass with it, such as --can-be-contacted, and pass --yes only after the user approves sending it")) } // confirm asks the user to approve feedback an AI agent is about to send. It diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index 5abc82f57b0..4ca3408ea53 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -62,13 +62,19 @@ const ( const testWorkspace = "https://workspace.test" // newTestFeedbackCommand returns the feedback command under a root with the -// root's bundle target flags. +// root's bundle target flags and a few top-level commands, without subcommands +// like the CLI's unbuilt placeholders. func newTestFeedbackCommand() *cobra.Command { rootCmd := &cobra.Command{Use: "databricks"} rootCmd.PersistentFlags().String("target", "", "") rootCmd.PersistentFlags().String("environment", "", "") feedbackCmd := &cobra.Command{Use: "feedback"} - rootCmd.AddCommand(feedbackCmd) + rootCmd.AddCommand( + feedbackCmd, + &cobra.Command{Use: "air"}, + &cobra.Command{Use: "bundle"}, + &cobra.Command{Use: "jobs", Aliases: []string{"job"}}, + ) return feedbackCmd } @@ -122,13 +128,13 @@ func TestWorkspaceLabel(t *testing.T) { workspaceID string want string }{ - {name: "workspace host", want: "https://workspace.test"}, - {name: "unified host", workspaceID: "123", want: "https://workspace.test (workspace ID 123)"}, - {name: "no workspace sentinel", workspaceID: "none", want: "https://workspace.test"}, + {name: "workspace host", want: testWorkspace}, + {name: "unified host", workspaceID: "123", want: testWorkspace + " (workspace ID 123)"}, + {name: "no workspace sentinel", workspaceID: "none", want: testWorkspace}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.want, workspaceLabel(&config.Config{Host: "https://workspace.test", WorkspaceID: tt.workspaceID})) + assert.Equal(t, tt.want, workspaceLabel(&config.Config{Host: testWorkspace, WorkspaceID: tt.workspaceID})) }) } } @@ -139,18 +145,31 @@ func TestResolveCommand(t *testing.T) { command string wantComponent string wantOrigin string + wantErr bool }{ {name: "general", wantComponent: defaultComponent, wantOrigin: "cli"}, {name: "blank", command: " ", wantComponent: defaultComponent, wantOrigin: "cli"}, {name: "air subcommand", command: "air run", wantComponent: "ModelTraining.AIRuntime", wantOrigin: "air_run"}, {name: "bundle group", command: "bundle", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle"}, {name: "bundle subcommand", command: "bundle deploy", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_deploy"}, - {name: "any text after a product's name", command: "bundle not-a-command", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_not-a-command"}, + {name: "unchecked subcommand", command: "bundle not-a-command", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_not-a-command"}, {name: "other command", command: "jobs list", wantComponent: defaultComponent, wantOrigin: "jobs_list"}, + {name: "alias", command: "job list", wantComponent: defaultComponent, wantOrigin: "jobs_list"}, + {name: "unknown top level", command: "bundel deploy", wantErr: true}, + {name: "databricks prefix", command: "databricks bundle deploy", wantErr: true}, + {name: "flag", command: "bundle deploy -t dev", wantErr: true}, + {name: "path argument", command: "bundle sync ./src", wantErr: true}, + {name: "uppercase", command: "Bundle deploy", wantErr: true}, + {name: "control characters", command: "bundle \x1b[8mdeploy", wantErr: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - component, origin := resolveCommand(tt.command) + component, origin, err := resolveCommand(newTestFeedbackCommand(), tt.command) + if tt.wantErr { + assert.EqualError(t, err, fmt.Sprintf(`unknown command %q; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, tt.command)) + return + } + require.NoError(t, err) assert.Equal(t, tt.wantComponent, component) assert.Equal(t, tt.wantOrigin, origin) }) @@ -269,7 +288,7 @@ func TestConfirm(t *testing.T) { func TestCheckApproval(t *testing.T) { const refusal = "feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + - "Note for AI agents (test-agent): show the user the exact feedback message and pass --yes only after the user approves sending it" + "Note for AI agents (test-agent): show the user the exact feedback message and the flags you pass with it, such as --can-be-contacted, and pass --yes only after the user approves sending it" tests := []struct { name string agent string From d0b1d4313bc9f809d88e12e5af0344238fd0c165 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:37:00 +0000 Subject: [PATCH 14/20] Report feedback that never left as not sent telemetry.Send marks failures that happen before the request is sent (the payload, the API client, authentication, including its timeout), and the new telemetry.NotSent reports those, a rejected request, and a response that accepted none of the logs. feedback uses it, so a failed login no longer reads as "could not be confirmed as sent". Comments now say what --command validation does not cover (a plain word after the first), why the prompt needs only stdin to be a terminal, and why the minimal retry window does not cancel the request. TestConfirm is table-driven. Co-authored-by: Isaac --- cmd/feedback/feedback.go | 24 +++++---------- cmd/feedback/feedback_test.go | 58 ++++++++++++----------------------- libs/telemetry/logger.go | 33 ++++++++++++++++++-- libs/telemetry/logger_test.go | 28 ++++++++++++++++- 4 files changed, 85 insertions(+), 58 deletions(-) diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index 4599cec0844..a0178b9af17 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -19,7 +19,6 @@ import ( "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" - "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/useragent" "github.com/spf13/cobra" @@ -202,7 +201,7 @@ func send(ctx context.Context, s submission, yes bool) error { }, }}) if err != nil { - if notSent(err) { + if telemetry.NotSent(err) { return fmt.Errorf("feedback %s was not sent: %w", id, err) } // The upload may have been accepted before the error, so retrying can @@ -213,16 +212,6 @@ func send(ctx context.Context, s submission, yes bool) error { return cmdio.RenderWithTemplate(ctx, receipt{Received: true, FeedbackID: id}, "", receiptTemplate) } -// notSent reports whether err shows that the server stored nothing: it rejected -// the request, or did not accept the feedback. -func notSent(err error) bool { - if _, ok := errors.AsType[*telemetry.NotAcceptedError](err); ok { - return true - } - apiErr, ok := errors.AsType[*apierr.APIError](err) - return ok && apiErr.StatusCode >= 400 && apiErr.StatusCode < 500 -} - func validateMessage(message string) (string, error) { text := strings.TrimSpace(message) if utf8.RuneCountInString(text) > messageLimit || !strings.ContainsFunc(text, visible) { @@ -235,9 +224,11 @@ func validateMessage(message string) (string, error) { // must be a top-level command and picks the owning team, so "bundle deploy" // reaches bundles. The rest is not checked against the command tree, which would // mean building every command; instead every word is limited to the characters -// of command names, so flags, paths, arguments and terminal control sequences -// cannot reach origin or the approval prompt. The origin joins the words with -// underscores, as the CLI names commands elsewhere (bundle_deploy). +// of command names, so flags, paths and terminal control sequences cannot reach +// origin or the approval prompt. A plain word after the first, such as an +// argument or a subcommand typo, is not told apart from a subcommand. The +// origin joins the words with underscores, as the CLI names commands elsewhere +// (bundle_deploy). func resolveCommand(cmd *cobra.Command, command string) (string, string, error) { words := strings.Fields(command) if len(words) == 0 { @@ -283,7 +274,8 @@ func needsApproval(yes bool) bool { // checkApproval returns an error when the user must approve the feedback but // there is no terminal to ask in. It needs only the input, so it runs before -// authenticating. +// authenticating. Only stdin has to be a terminal: confirm reads a plain line, +// which works where cmdio.IsPromptSupported says no (TERM=dumb, MSYS2 pipes). func checkApproval(ctx context.Context, yes bool) error { if !needsApproval(yes) || cmdio.IsInputTTY(ctx) { return nil diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index 4ca3408ea53..0418c98841f 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -3,7 +3,6 @@ package feedback import ( "bytes" "encoding/json" - "errors" "fmt" "io" "net/http" @@ -20,7 +19,6 @@ import ( "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/cli/libs/testserver" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" @@ -104,24 +102,6 @@ func TestCheckArgs(t *testing.T) { } } -func TestNotSent(t *testing.T) { - tests := []struct { - name string - err error - want bool - }{ - {name: "nothing accepted", err: fmt.Errorf("wrapped: %w", &telemetry.NotAcceptedError{Accepted: 0, Sent: 1}), want: true}, - {name: "rejected", err: &apierr.APIError{StatusCode: 403}, want: true}, - {name: "server error", err: &apierr.APIError{StatusCode: 500}}, - {name: "connection reset", err: errors.New("connection reset by peer")}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.want, notSent(tt.err)) - }) - } -} - func TestWorkspaceLabel(t *testing.T) { tests := []struct { name string @@ -266,24 +246,26 @@ func TestSummary(t *testing.T) { } func TestConfirm(t *testing.T) { - t.Run("accepted", func(t *testing.T) { - var stderr bytes.Buffer - ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader("y\n"), io.Discard, &stderr)) - confirmed, err := confirm(ctx, submission{text: "exact feedback"}, testWorkspace) - require.NoError(t, err) - assert.True(t, confirmed) - assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\n exact feedback\n\nWorkspace: https://workspace.test\nDatabricks may contact you: no\n\n") - assert.Contains(t, stderr.String(), "Send this feedback? [y/N]: ") - }) - - t.Run("declined", func(t *testing.T) { - var stderr bytes.Buffer - ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader("\n"), io.Discard, &stderr)) - confirmed, err := confirm(ctx, submission{text: "exact feedback"}, testWorkspace) - require.NoError(t, err) - assert.False(t, confirmed) - assert.Contains(t, stderr.String(), "Feedback not sent.") - }) + tests := []struct { + name string + answer string + wantConfirmed bool + wantStderr string + }{ + {name: "accepted", answer: "y\n", wantConfirmed: true, wantStderr: "Send this feedback? [y/N]: "}, + {name: "declined", answer: "\n", wantStderr: "Feedback not sent."}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var stderr bytes.Buffer + ctx := cmdio.InContext(t.Context(), cmdio.NewTestIO(strings.NewReader(tt.answer), io.Discard, &stderr)) + confirmed, err := confirm(ctx, submission{text: "exact feedback"}, testWorkspace) + require.NoError(t, err) + assert.Equal(t, tt.wantConfirmed, confirmed) + assert.Contains(t, stderr.String(), "The following feedback will be sent to Databricks:\n\n exact feedback\n\nWorkspace: "+testWorkspace+"\nDatabricks may contact you: no\n\n") + assert.Contains(t, stderr.String(), tt.wantStderr) + }) + } } func TestCheckApproval(t *testing.T) { diff --git a/libs/telemetry/logger.go b/libs/telemetry/logger.go index 9242546ad84..6a08acc9f5c 100644 --- a/libs/telemetry/logger.go +++ b/libs/telemetry/logger.go @@ -297,6 +297,8 @@ func newAPIClient(cfg *config.Config, running *atomic.Int32, retries bool) (*cli if !retries { // The SDK retries some connection errors regardless of ErrorRetriable, // so a minimal retry window is needed to stop after the first attempt. + // The attempt itself runs on the caller's context, so the window only + // ends the retrying; TestSendMakesOneAttempt fails if that changes. clientCfg.ErrorRetriable = func(context.Context, error) bool { return false } clientCfg.TransientErrors = nil clientCfg.RetryTimeout = time.Nanosecond @@ -313,7 +315,8 @@ func authenticate(r *http.Request, cfg *config.Config, running *atomic.Int32) er return struct{}{}, cfg.Authenticate(authReq) }) if err != nil { - return fmt.Errorf("authenticating upload: %w", err) + // Authentication runs before the request is sent. + return ¬SentError{fmt.Errorf("authenticating upload: %w", err)} } r.Header = authReq.Header return nil @@ -325,7 +328,7 @@ func authenticate(r *http.Request, cfg *config.Config, running *atomic.Int32) er func Send(ctx context.Context, cfg *config.Config, logs []protos.FrontendLog) error { protoLogs, err := marshalLogs(logs) if err != nil { - return err + return ¬SentError{err} } var running atomic.Int32 @@ -333,7 +336,7 @@ func Send(ctx context.Context, cfg *config.Config, logs []protos.FrontendLog) er return newAPIClient(cfg, &running, false) }) if err != nil { - return fmt.Errorf("failed to create API client: %w", err) + return ¬SentError{fmt.Errorf("failed to create API client: %w", err)} } resp, err := attempt(ctx, apiClient, protoLogs) @@ -346,6 +349,30 @@ func Send(ctx context.Context, cfg *config.Config, logs []protos.FrontendLog) er return nil } +// notSentError marks a Send failure that happened before the request was sent, +// so the server cannot have stored the logs. +type notSentError struct { + err error +} + +func (e *notSentError) Error() string { return e.err.Error() } +func (e *notSentError) Unwrap() error { return e.err } + +// NotSent reports whether err, returned by Send, shows that the server stored +// none of the logs: the request was never sent, the server rejected it, or it +// accepted none of the logs. Any other error is ambiguous, because the server +// may have stored the logs before the error. +func NotSent(err error) bool { + if _, ok := errors.AsType[*notSentError](err); ok { + return true + } + if e, ok := errors.AsType[*NotAcceptedError](err); ok { + return e.Accepted == 0 + } + apiErr, ok := errors.AsType[*apierr.APIError](err) + return ok && apiErr.StatusCode >= 400 && apiErr.StatusCode < 500 +} + // NotAcceptedError is returned by Send when the server accepted fewer logs than // were sent, so the others were not stored. type NotAcceptedError struct { diff --git a/libs/telemetry/logger_test.go b/libs/telemetry/logger_test.go index ece61d05f53..219b2103c44 100644 --- a/libs/telemetry/logger_test.go +++ b/libs/telemetry/logger_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "net" "net/http" "sync/atomic" @@ -469,6 +470,26 @@ func TestWithinReturnsPanicAsError(t *testing.T) { assert.Eventually(t, func() bool { return running.Load() == 0 }, time.Second, time.Millisecond) } +func TestNotSent(t *testing.T) { + tests := []struct { + name string + err error + want bool + }{ + {name: "never sent", err: fmt.Errorf("wrapped: %w", ¬SentError{errors.New("no client")}), want: true}, + {name: "nothing accepted", err: &NotAcceptedError{Accepted: 0, Sent: 1}, want: true}, + {name: "some accepted", err: &NotAcceptedError{Accepted: 1, Sent: 2}}, + {name: "rejected", err: &apierr.APIError{StatusCode: 400}, want: true}, + {name: "server error", err: &apierr.APIError{StatusCode: 500}}, + {name: "connection reset", err: errors.New("connection reset by peer")}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, NotSent(tt.err)) + }) + } +} + func testFeedbackLog() []protos.FrontendLog { return []protos.FrontendLog{{ FrontendLogEventID: "id", @@ -486,11 +507,13 @@ func TestSendMakesOneAttempt(t *testing.T) { reset bool wantErr bool errContains string + wantNotSent bool }{ {name: "all accepted", numProtoSuccess: 1}, - {name: "none accepted", wantErr: true, errContains: "only 0 of 1 logs were accepted"}, + {name: "none accepted", wantErr: true, errContains: "only 0 of 1 logs were accepted", wantNotSent: true}, {name: "server error", statusCode: 500, wantErr: true, errContains: "server error"}, {name: "SDK retryable error", statusCode: 503, wantErr: true, errContains: "server error"}, + {name: "rejected", statusCode: 403, wantErr: true, errContains: "server error", wantNotSent: true}, {name: "connection reset", reset: true, wantErr: true}, } for _, tt := range tests { @@ -532,6 +555,7 @@ func TestSendMakesOneAttempt(t *testing.T) { if tt.errContains != "" { assert.ErrorContains(t, err, tt.errContains) } + assert.Equal(t, tt.wantNotSent, NotSent(err)) } assert.Equal(t, int32(1), count.Load()) if !tt.reset { @@ -562,6 +586,8 @@ func TestSendTimeoutBoundsTokenAcquisition(t *testing.T) { select { case err := <-errc: assert.ErrorIs(t, err, context.DeadlineExceeded) + // The token was never fetched, so nothing was sent. + assert.True(t, NotSent(err)) case <-time.After(10 * time.Second): t.Fatal("Send did not return after its context deadline") } From 2092b57d2be862d0d12de4cac507ae0bc15091c2 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:02:42 +0000 Subject: [PATCH 15/20] Ask for approval only when stderr shows the prompt The summary and the question are written to stderr. With stderr redirected the user cannot see what they approve, so an agent then needs --yes. Co-authored-by: Isaac --- cmd/feedback/feedback.go | 24 +++++++++++------------- cmd/feedback/feedback_test.go | 22 ++++++++++++++-------- libs/cmdio/io.go | 7 +++++++ libs/cmdio/io_test.go | 8 ++++++++ 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index a0178b9af17..ce572a2a2fa 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -220,15 +220,12 @@ func validateMessage(message string) (string, error) { return text, nil } -// resolveCommand returns the component and origin for --command. Its first word -// must be a top-level command and picks the owning team, so "bundle deploy" -// reaches bundles. The rest is not checked against the command tree, which would -// mean building every command; instead every word is limited to the characters -// of command names, so flags, paths and terminal control sequences cannot reach -// origin or the approval prompt. A plain word after the first, such as an -// argument or a subcommand typo, is not told apart from a subcommand. The -// origin joins the words with underscores, as the CLI names commands elsewhere -// (bundle_deploy). +// resolveCommand returns the component and origin for --command. The first word +// must be a top-level command and picks the owning team. Checking later words +// would mean building every command, so they are only limited to command-name +// characters, which keeps flags, paths and control sequences out of origin and +// the approval prompt. The origin joins the words with underscores, as in +// bundle_deploy. func resolveCommand(cmd *cobra.Command, command string) (string, string, error) { words := strings.Fields(command) if len(words) == 0 { @@ -273,11 +270,12 @@ func needsApproval(yes bool) bool { } // checkApproval returns an error when the user must approve the feedback but -// there is no terminal to ask in. It needs only the input, so it runs before -// authenticating. Only stdin has to be a terminal: confirm reads a plain line, -// which works where cmdio.IsPromptSupported says no (TERM=dumb, MSYS2 pipes). +// there is no terminal to ask in. It needs only the terminal, so it runs before +// authenticating. confirm shows the summary on stderr and reads a plain line +// from stdin, so it works where cmdio.IsPromptSupported says no (TERM=dumb, +// MSYS2 pipes). func checkApproval(ctx context.Context, yes bool) error { - if !needsApproval(yes) || cmdio.IsInputTTY(ctx) { + if !needsApproval(yes) || (cmdio.IsInputTTY(ctx) && cmdio.IsErrorTTY(ctx)) { return nil } return errors.New("feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index 0418c98841f..ca9dd12632d 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -272,19 +272,21 @@ func TestCheckApproval(t *testing.T) { const refusal = "feedback from an AI agent must be confirmed by the user; run with --yes once the user approves the message\n\n" + "Note for AI agents (test-agent): show the user the exact feedback message and the flags you pass with it, such as --can-be-contacted, and pass --yes only after the user approves sending it" tests := []struct { - name string - agent string - yes bool - terminal bool - envKey string - envValue string - wantErr string + name string + agent string + yes bool + terminal bool + redirectStderr bool + envKey string + envValue string + wantErr string }{ {name: "human without a terminal"}, {name: "agent with --yes", agent: "test-agent", yes: true}, {name: "agent with a terminal", agent: "test-agent", terminal: true}, {name: "agent with a terminal and NO_COLOR", agent: "test-agent", terminal: true, envKey: "NO_COLOR", envValue: "1"}, {name: "agent with a dumb terminal", agent: "test-agent", terminal: true, envKey: "TERM", envValue: "dumb"}, + {name: "agent with stderr redirected", agent: "test-agent", terminal: true, redirectStderr: true, wantErr: refusal}, {name: "agent without a terminal", agent: "test-agent", wantErr: refusal}, } for _, tt := range tests { @@ -298,7 +300,11 @@ func TestCheckApproval(t *testing.T) { if tt.terminal { in = cmdio.FakeTTYReader(in) } - ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputText, in, io.Discard, cmdio.FakeTTY(io.Discard), "", "")) + stderr := io.Discard + if !tt.redirectStderr { + stderr = cmdio.FakeTTY(stderr) + } + ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputText, in, io.Discard, stderr, "", "")) err := checkApproval(ctx, tt.yes) if tt.wantErr != "" { assert.EqualError(t, err, tt.wantErr) diff --git a/libs/cmdio/io.go b/libs/cmdio/io.go index 5a0385d664a..7ca1efb3cb6 100644 --- a/libs/cmdio/io.go +++ b/libs/cmdio/io.go @@ -92,6 +92,13 @@ func IsInputTTY(ctx context.Context) bool { return c.capabilities.stdinIsTTY } +// IsErrorTTY reports whether stderr is a terminal, so that the user sees the +// messages and questions written there. Like IsInputTTY, it ignores TERM=dumb. +func IsErrorTTY(ctx context.Context) bool { + c := fromContext(ctx) + return c.capabilities.stderrIsTTY +} + // IsPagerSupported reports whether stdin, stdout, and stderr are all interactive // terminals. This is the requirement for a full-screen or navigable output // program: unlike IsPromptSupported it also checks stdout, so it returns false diff --git a/libs/cmdio/io_test.go b/libs/cmdio/io_test.go index b926dec3cd7..0dbd0462cbf 100644 --- a/libs/cmdio/io_test.go +++ b/libs/cmdio/io_test.go @@ -52,6 +52,14 @@ func TestIsInputTTY(t *testing.T) { assert.False(t, IsInputTTY(ctx)) } +func TestIsErrorTTY(t *testing.T) { + ctx := InContext(t.Context(), &cmdIO{capabilities: Capabilities{stderrIsTTY: true, dumb: true}}) + assert.True(t, IsErrorTTY(ctx)) + + ctx = InContext(t.Context(), &cmdIO{capabilities: Capabilities{stdinIsTTY: true}}) + assert.False(t, IsErrorTTY(ctx)) +} + func TestHasTerminal(t *testing.T) { for _, tc := range []struct { name string From f396f8551df281e835e9772c99f083a8a7889a98 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:02:43 +0000 Subject: [PATCH 16/20] Report feedback that failed before connecting as not sent A request is written only once its connection is set up, so a DNS, dial or TLS failure before then means the server cannot have stored the feedback. Co-authored-by: Isaac --- libs/telemetry/logger.go | 17 +++++++++++++---- libs/telemetry/logger_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/libs/telemetry/logger.go b/libs/telemetry/logger.go index 6a08acc9f5c..eea825c56b5 100644 --- a/libs/telemetry/logger.go +++ b/libs/telemetry/logger.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net/http" + "net/http/httptrace" "strconv" "sync/atomic" "time" @@ -339,11 +340,19 @@ func Send(ctx context.Context, cfg *config.Config, logs []protos.FrontendLog) er return ¬SentError{fmt.Errorf("failed to create API client: %w", err)} } - resp, err := attempt(ctx, apiClient, protoLogs) - if err != nil { + // A request is written only once its connection is set up, so a failure + // before then, such as a DNS, dial or TLS error, means nothing was sent. + var connected atomic.Bool + traceCtx := httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{ + GotConn: func(httptrace.GotConnInfo) { connected.Store(true) }, + }) + resp, err := attempt(traceCtx, apiClient, protoLogs) + switch { + case err != nil && !connected.Load(): + return ¬SentError{err} + case err != nil: return err - } - if resp.NumProtoSuccess < int64(len(protoLogs)) { + case resp.NumProtoSuccess < int64(len(protoLogs)): return &NotAcceptedError{Accepted: resp.NumProtoSuccess, Sent: int64(len(protoLogs))} } return nil diff --git a/libs/telemetry/logger_test.go b/libs/telemetry/logger_test.go index 219b2103c44..37ba7471e96 100644 --- a/libs/telemetry/logger_test.go +++ b/libs/telemetry/logger_test.go @@ -7,6 +7,7 @@ import ( "fmt" "net" "net/http" + "net/http/httptest" "sync/atomic" "testing" "time" @@ -565,6 +566,34 @@ func TestSendMakesOneAttempt(t *testing.T) { } } +func TestSendFailingBeforeConnectingIsNotSent(t *testing.T) { + // Resolve the config while the server is up, because the SDK retries its + // host metadata lookup for minutes; then close it to refuse the upload. + closed := httptest.NewServer(http.NotFoundHandler()) + refused := &config.Config{Host: closed.URL, Token: "token"} + require.NoError(t, refused.EnsureResolved()) + closed.Close() + + // The client does not trust the certificate of this server. + untrusted := httptest.NewTLSServer(http.NotFoundHandler()) + t.Cleanup(untrusted.Close) + + tests := []struct { + name string + cfg *config.Config + }{ + {name: "connection refused", cfg: refused}, + {name: "untrusted certificate", cfg: &config.Config{Host: untrusted.URL, Token: "token"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Send(t.Context(), tt.cfg, testFeedbackLog()) + require.Error(t, err) + assert.True(t, NotSent(err), "error: %v", err) + }) + } +} + func TestSendTimeoutBoundsTokenAcquisition(t *testing.T) { server := testserver.New(t) release := make(chan struct{}) From 61214bf07ea326ea4b9d136ea3d1109d642737d0 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:15:24 +0000 Subject: [PATCH 17/20] Limit --command to 200 characters Later words of --command are not checked against the command tree, so without a limit it could fill the approval prompt and the origin field. Co-authored-by: Isaac --- cmd/feedback/feedback.go | 5 +++++ cmd/feedback/feedback_test.go | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index ce572a2a2fa..da728b4e24c 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -26,6 +26,8 @@ import ( const ( messageLimit = 10000 + // commandLimit is well above the longest command path, about 80 characters. + commandLimit = 200 sendTimeout = 30 * time.Second defaultComponent = "DeveloperEcosystem.DatabricksCLI" @@ -140,6 +142,9 @@ func newSubmission(cmd *cobra.Command, message string, opts *options) (submissio if err != nil { return submission{}, err } + if utf8.RuneCountInString(opts.command) > commandLimit { + return submission{}, fmt.Errorf("--command must be at most %d characters", commandLimit) + } component, origin, err := resolveCommand(cmd, opts.command) if err != nil { return submission{}, err diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index ca9dd12632d..aff4a17f9ae 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -177,6 +177,10 @@ func TestNewSubmission(t *testing.T) { {name: "zero run ID", opts: options{runID: "0"}, message: "x", wantErr: runIDError}, {name: "non-numeric run ID", opts: options{runID: "abc"}, message: "x", wantErr: runIDError}, {name: "blank message", message: " ", wantErr: messageError}, + { + name: "long command", opts: options{command: "bundle " + strings.Repeat("a", commandLimit)}, message: "x", + wantErr: fmt.Sprintf("--command must be at most %d characters", commandLimit), + }, { name: "target", flags: []string{"--target", "dev"}, message: "x", wantErr: "--target selects a bundle target, which feedback does not use; use --profile to choose the workspace", From f9899f032fea5650ea0d4e9355c95272c0f21a93 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:15:25 +0000 Subject: [PATCH 18/20] Keep the not-accepted error internal to telemetry Callers check NotSent; nothing outside the package uses the error type. Co-authored-by: Isaac --- libs/telemetry/logger.go | 10 +++++----- libs/telemetry/logger_test.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libs/telemetry/logger.go b/libs/telemetry/logger.go index eea825c56b5..3a4f6d7edf6 100644 --- a/libs/telemetry/logger.go +++ b/libs/telemetry/logger.go @@ -353,7 +353,7 @@ func Send(ctx context.Context, cfg *config.Config, logs []protos.FrontendLog) er case err != nil: return err case resp.NumProtoSuccess < int64(len(protoLogs)): - return &NotAcceptedError{Accepted: resp.NumProtoSuccess, Sent: int64(len(protoLogs))} + return ¬AcceptedError{Accepted: resp.NumProtoSuccess, Sent: int64(len(protoLogs))} } return nil } @@ -375,20 +375,20 @@ func NotSent(err error) bool { if _, ok := errors.AsType[*notSentError](err); ok { return true } - if e, ok := errors.AsType[*NotAcceptedError](err); ok { + if e, ok := errors.AsType[*notAcceptedError](err); ok { return e.Accepted == 0 } apiErr, ok := errors.AsType[*apierr.APIError](err) return ok && apiErr.StatusCode >= 400 && apiErr.StatusCode < 500 } -// NotAcceptedError is returned by Send when the server accepted fewer logs than +// notAcceptedError is returned by Send when the server accepted fewer logs than // were sent, so the others were not stored. -type NotAcceptedError struct { +type notAcceptedError struct { Accepted, Sent int64 } -func (e *NotAcceptedError) Error() string { +func (e *notAcceptedError) Error() string { return fmt.Sprintf("only %d of %d logs were accepted", e.Accepted, e.Sent) } diff --git a/libs/telemetry/logger_test.go b/libs/telemetry/logger_test.go index 37ba7471e96..f03a580c954 100644 --- a/libs/telemetry/logger_test.go +++ b/libs/telemetry/logger_test.go @@ -478,8 +478,8 @@ func TestNotSent(t *testing.T) { want bool }{ {name: "never sent", err: fmt.Errorf("wrapped: %w", ¬SentError{errors.New("no client")}), want: true}, - {name: "nothing accepted", err: &NotAcceptedError{Accepted: 0, Sent: 1}, want: true}, - {name: "some accepted", err: &NotAcceptedError{Accepted: 1, Sent: 2}}, + {name: "nothing accepted", err: ¬AcceptedError{Accepted: 0, Sent: 1}, want: true}, + {name: "some accepted", err: ¬AcceptedError{Accepted: 1, Sent: 2}}, {name: "rejected", err: &apierr.APIError{StatusCode: 400}, want: true}, {name: "server error", err: &apierr.APIError{StatusCode: 500}}, {name: "connection reset", err: errors.New("connection reset by peer")}, From 19591b36b19e1163666a4d05a5636f388c04e77c Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:33:53 +0000 Subject: [PATCH 19/20] Keep the not-sent test off the host metadata lookup The SDK retries a failed host metadata lookup for minutes, which stalled the untrusted-certificate case now and then. Stubbing the lookup also removes the pooled connection the refused case could reuse. Co-authored-by: Isaac --- libs/telemetry/logger_test.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libs/telemetry/logger_test.go b/libs/telemetry/logger_test.go index f03a580c954..4dbba27c012 100644 --- a/libs/telemetry/logger_test.go +++ b/libs/telemetry/logger_test.go @@ -567,27 +567,32 @@ func TestSendMakesOneAttempt(t *testing.T) { } func TestSendFailingBeforeConnectingIsNotSent(t *testing.T) { - // Resolve the config while the server is up, because the SDK retries its - // host metadata lookup for minutes; then close it to refuse the upload. - closed := httptest.NewServer(http.NotFoundHandler()) - refused := &config.Config{Host: closed.URL, Token: "token"} - require.NoError(t, refused.EnsureResolved()) - closed.Close() - - // The client does not trust the certificate of this server. + // The client does not trust the certificate of this server. It starts + // first so that it cannot take the port of the closed listener below. untrusted := httptest.NewTLSServer(http.NotFoundHandler()) t.Cleanup(untrusted.Close) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + refused := "http://" + listener.Addr().String() + require.NoError(t, listener.Close()) + + // The SDK retries its host metadata lookup for minutes when it fails. + noMetadata := func(context.Context, string) (*config.HostMetadata, error) { + return nil, nil + } + tests := []struct { name string - cfg *config.Config + host string }{ - {name: "connection refused", cfg: refused}, - {name: "untrusted certificate", cfg: &config.Config{Host: untrusted.URL, Token: "token"}}, + {name: "connection refused", host: refused}, + {name: "untrusted certificate", host: untrusted.URL}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := Send(t.Context(), tt.cfg, testFeedbackLog()) + cfg := &config.Config{Host: tt.host, Token: "token", HostMetadataResolver: noMetadata} + err := Send(t.Context(), cfg, testFeedbackLog()) require.Error(t, err) assert.True(t, NotSent(err), "error: %v", err) }) From 66f0fa62502ce4b5b403b9144b89b57e78398626 Mon Sep 17 00:00:00 2001 From: simon <4305831+simonfaltum@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:33:54 +0000 Subject: [PATCH 20/20] Stop checking --command against the top-level commands --command is a free string again: its first word picks the team, and its words are still limited to command-name characters. Ctrl-D at the approval prompt now declines instead of printing a bare EOF error. Co-authored-by: Isaac --- acceptance/cmd/feedback/output.txt | 53 +++++++++++++++++++----------- acceptance/cmd/feedback/script | 6 ++-- cmd/feedback/feedback.go | 31 +++++++++-------- cmd/feedback/feedback_test.go | 19 ++++------- 4 files changed, 59 insertions(+), 50 deletions(-) diff --git a/acceptance/cmd/feedback/output.txt b/acceptance/cmd/feedback/output.txt index 966ab25d867..eff9b579f70 100644 --- a/acceptance/cmd/feedback/output.txt +++ b/acceptance/cmd/feedback/output.txt @@ -65,6 +65,27 @@ Thank you! Your feedback was sent. Reference: [UUID][4] } } +=== A command without a product team goes to the CLI team + +>>> [CLI] feedback --command no-such-command hello +Thank you! Your feedback was sent. Reference: [UUID][6] + +>>> print_requests.py //telemetry-ext +{ + "frontend_log_event_id": "[UUID][6]", + "entry": { + "fast_feedback_log": { + "reference_event_id": "[UUID][7]", + "parent_ui": "DeveloperEcosystem.DatabricksCLI", + "origin": "no-such-command", + "can_be_contacted": false, + "ui_versions": "cli:[CLI_VERSION]", + "user_agent": "cli/[CLI_VERSION] cmd/feedback", + "text": "hello" + } + } +} + === Validation errors send nothing >>> [CLI] feedback @@ -100,14 +121,8 @@ Error: the message must be one argument, but received 3; put quotes around the w >>> [CLI] feedback --command bundle deploy deploy is slow Error: the message must be one argument, but received 2; put quotes around the whole message, and around --command if it has several words ->>> [CLI] feedback --command no-such-command hello -Error: unknown command "no-such-command"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments - ->>> [CLI] feedback --command databricks bundle deploy hello -Error: unknown command "databricks bundle deploy"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments - >>> [CLI] feedback --command bundle deploy -t dev hello -Error: unknown command "bundle deploy -t dev"; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments +Error: invalid --command "bundle deploy -t dev"; it takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments >>> [CLI] feedback --run-id abc hello Error: --run-id must be a positive job run ID @@ -128,14 +143,14 @@ Note for AI agents (test-agent): show the user the exact feedback message and th === General CLI feedback >>> [CLI] feedback the CLI should support ... -Thank you! Your feedback was sent. Reference: [UUID][6] +Thank you! Your feedback was sent. Reference: [UUID][8] >>> print_requests.py //telemetry-ext { - "frontend_log_event_id": "[UUID][6]", + "frontend_log_event_id": "[UUID][8]", "entry": { "fast_feedback_log": { - "reference_event_id": "[UUID][7]", + "reference_event_id": "[UUID][9]", "parent_ui": "DeveloperEcosystem.DatabricksCLI", "origin": "cli", "can_be_contacted": false, @@ -156,14 +171,14 @@ Note for AI agents (test-agent): show the user the exact feedback message and th === An AI agent with --yes >>> AGENT=test-agent [CLI] feedback --yes hello -Thank you! Your feedback was sent. Reference: [UUID][8] +Thank you! Your feedback was sent. Reference: [UUID][10] >>> print_requests.py //telemetry-ext { - "frontend_log_event_id": "[UUID][8]", + "frontend_log_event_id": "[UUID][10]", "entry": { "fast_feedback_log": { - "reference_event_id": "[UUID][9]", + "reference_event_id": "[UUID][11]", "parent_ui": "DeveloperEcosystem.DatabricksCLI", "origin": "cli", "can_be_contacted": false, @@ -177,14 +192,14 @@ Thank you! Your feedback was sent. Reference: [UUID][8] === Feedback is sent even with telemetry disabled >>> DATABRICKS_CLI_DISABLE_TELEMETRY=1 [CLI] feedback telemetry is disabled -Thank you! Your feedback was sent. Reference: [UUID][10] +Thank you! Your feedback was sent. Reference: [UUID][12] >>> print_requests.py //telemetry-ext { - "frontend_log_event_id": "[UUID][10]", + "frontend_log_event_id": "[UUID][12]", "entry": { "fast_feedback_log": { - "reference_event_id": "[UUID][11]", + "reference_event_id": "[UUID][13]", "parent_ui": "DeveloperEcosystem.DatabricksCLI", "origin": "cli", "can_be_contacted": false, @@ -198,14 +213,14 @@ Thank you! Your feedback was sent. Reference: [UUID][10] === A broken bundle does not block feedback >>> withdir broken-bundle [CLI] feedback bundle config does not block feedback -Thank you! Your feedback was sent. Reference: [UUID][12] +Thank you! Your feedback was sent. Reference: [UUID][14] >>> print_requests.py //telemetry-ext { - "frontend_log_event_id": "[UUID][12]", + "frontend_log_event_id": "[UUID][14]", "entry": { "fast_feedback_log": { - "reference_event_id": "[UUID][13]", + "reference_event_id": "[UUID][15]", "parent_ui": "DeveloperEcosystem.DatabricksCLI", "origin": "cli", "can_be_contacted": false, diff --git a/acceptance/cmd/feedback/script b/acceptance/cmd/feedback/script index c789287d920..deed5673fc3 100644 --- a/acceptance/cmd/feedback/script +++ b/acceptance/cmd/feedback/script @@ -17,13 +17,15 @@ title "Feedback can name a nested command" trace $CLI feedback --command "bundle deploy" "deploy is slow" print_feedback +title "A command without a product team goes to the CLI team" +trace $CLI feedback --command "no-such-command" "hello" +print_feedback + title "Validation errors send nothing" musterr trace $CLI feedback " " musterr trace $CLI feedback musterr trace $CLI feedback deploy is slow musterr trace $CLI feedback --command bundle deploy "deploy is slow" -musterr trace $CLI feedback --command "no-such-command" "hello" -musterr trace $CLI feedback --command "databricks bundle deploy" "hello" musterr trace $CLI feedback --command "bundle deploy -t dev" "hello" musterr trace $CLI feedback --run-id abc "hello" musterr trace $CLI feedback --target dev "hello" diff --git a/cmd/feedback/feedback.go b/cmd/feedback/feedback.go index da728b4e24c..31cbfdc3b52 100644 --- a/cmd/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "strconv" "strings" "time" @@ -145,7 +146,7 @@ func newSubmission(cmd *cobra.Command, message string, opts *options) (submissio if utf8.RuneCountInString(opts.command) > commandLimit { return submission{}, fmt.Errorf("--command must be at most %d characters", commandLimit) } - component, origin, err := resolveCommand(cmd, opts.command) + component, origin, err := resolveCommand(opts.command) if err != nil { return submission{}, err } @@ -226,27 +227,21 @@ func validateMessage(message string) (string, error) { } // resolveCommand returns the component and origin for --command. The first word -// must be a top-level command and picks the owning team. Checking later words -// would mean building every command, so they are only limited to command-name +// picks the owning team. The command is not looked up, which would mean +// building the command tree; its words are only limited to command-name // characters, which keeps flags, paths and control sequences out of origin and // the approval prompt. The origin joins the words with underscores, as in // bundle_deploy. -func resolveCommand(cmd *cobra.Command, command string) (string, string, error) { +func resolveCommand(command string) (string, string, error) { words := strings.Fields(command) if len(words) == 0 { return defaultComponent, "cli", nil } - // Top-level commands are registered on the root before they are built. - top, rest, err := cmd.Root().Find(words[:1]) - valid := err == nil && len(rest) == 0 && top != cmd.Root() for _, word := range words { - valid = valid && isCommandWord(word) - } - if !valid { - return "", "", fmt.Errorf(`unknown command %q; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, command) + if !isCommandWord(word) { + return "", "", fmt.Errorf(`invalid --command %q; it takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, command) + } } - // An alias resolves to the command's name. - words[0] = top.Name() component := defaultComponent if productComponent, ok := productComponents[words[0]]; ok { component = productComponent @@ -292,10 +287,14 @@ func checkApproval(ctx context.Context, yes bool) error { func confirm(ctx context.Context, s submission, workspace string) (bool, error) { cmdio.LogString(ctx, "The following feedback will be sent to Databricks:\n\n"+summary(s, workspace)) confirmed, err := cmdio.AskYesOrNo(ctx, "Send this feedback?") - if err != nil { + switch { + case errors.Is(err, io.EOF): + // Ctrl-D ends the input without an answer or a newline. + cmdio.LogString(ctx, "\nFeedback not sent.") + return false, nil + case err != nil: return false, err - } - if !confirmed { + case !confirmed: cmdio.LogString(ctx, "Feedback not sent.") } return confirmed, nil diff --git a/cmd/feedback/feedback_test.go b/cmd/feedback/feedback_test.go index aff4a17f9ae..e9ff919ff9d 100644 --- a/cmd/feedback/feedback_test.go +++ b/cmd/feedback/feedback_test.go @@ -60,19 +60,13 @@ const ( const testWorkspace = "https://workspace.test" // newTestFeedbackCommand returns the feedback command under a root with the -// root's bundle target flags and a few top-level commands, without subcommands -// like the CLI's unbuilt placeholders. +// root's bundle target flags. func newTestFeedbackCommand() *cobra.Command { rootCmd := &cobra.Command{Use: "databricks"} rootCmd.PersistentFlags().String("target", "", "") rootCmd.PersistentFlags().String("environment", "", "") feedbackCmd := &cobra.Command{Use: "feedback"} - rootCmd.AddCommand( - feedbackCmd, - &cobra.Command{Use: "air"}, - &cobra.Command{Use: "bundle"}, - &cobra.Command{Use: "jobs", Aliases: []string{"job"}}, - ) + rootCmd.AddCommand(feedbackCmd) return feedbackCmd } @@ -134,9 +128,7 @@ func TestResolveCommand(t *testing.T) { {name: "bundle subcommand", command: "bundle deploy", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_deploy"}, {name: "unchecked subcommand", command: "bundle not-a-command", wantComponent: "DeveloperEcosystem.CoreDabs", wantOrigin: "bundle_not-a-command"}, {name: "other command", command: "jobs list", wantComponent: defaultComponent, wantOrigin: "jobs_list"}, - {name: "alias", command: "job list", wantComponent: defaultComponent, wantOrigin: "jobs_list"}, - {name: "unknown top level", command: "bundel deploy", wantErr: true}, - {name: "databricks prefix", command: "databricks bundle deploy", wantErr: true}, + {name: "unchecked top level", command: "bundel deploy", wantComponent: defaultComponent, wantOrigin: "bundel_deploy"}, {name: "flag", command: "bundle deploy -t dev", wantErr: true}, {name: "path argument", command: "bundle sync ./src", wantErr: true}, {name: "uppercase", command: "Bundle deploy", wantErr: true}, @@ -144,9 +136,9 @@ func TestResolveCommand(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - component, origin, err := resolveCommand(newTestFeedbackCommand(), tt.command) + component, origin, err := resolveCommand(tt.command) if tt.wantErr { - assert.EqualError(t, err, fmt.Sprintf(`unknown command %q; --command takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, tt.command)) + assert.EqualError(t, err, fmt.Sprintf(`invalid --command %q; it takes a command path such as "bundle deploy", without the databricks prefix, flags or arguments`, tt.command)) return } require.NoError(t, err) @@ -258,6 +250,7 @@ func TestConfirm(t *testing.T) { }{ {name: "accepted", answer: "y\n", wantConfirmed: true, wantStderr: "Send this feedback? [y/N]: "}, {name: "declined", answer: "\n", wantStderr: "Feedback not sent."}, + {name: "input ended", answer: "", wantStderr: "Send this feedback? [y/N]: \nFeedback not sent."}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {