From e9edeb28942056f6688c8db50ca29d714ab00fac Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Wed, 11 Nov 2026 19:28:28 +0000 Subject: [PATCH] aitools: never prompt a coding agent caller in install Coding agents often run commands in a pseudo-terminal (Cursor, Copilot in the VS Code terminal). Stdin looks like a TTY, so `databricks aitools install` showed its scope select and agent picker, and nobody was there to answer them. The run blocked. Now a caller that `useragent.AgentProvider()` identifies as an agent always gets the non-interactive defaults: global scope and every detected agent. On a terminal, when a prompt would otherwise have run, it prints a note to stderr that names the detected agent by its `--agents` value (or says more than one was detected) and the missing `--scope`/`--agents` flags of `databricks aitools install`, for example `No prompts shown because a coding agent (cursor) was detected. Pass --scope and --agents to databricks aitools install to choose what to install.` An agent env var inherited from an agent session can reach a person's shell, and the note tells them why the picker is gone. The change is scoped to install. These prompts still check only `cmdio.IsPromptSupported` and are left for a follow-up: the `Proceed?` confirm of `uninstall`, the both-scopes prompts of `update` and `uninstall`, and the `apps init` skills prompt. ```diff install RunE + interactive := cmdio.IsPromptSupported(ctx) && useragent.AgentProvider() == "" - resolveScopeWithPrompt(ctx) // prompted on any TTY + resolveScopeWithPrompt(ctx, interactive) - selectAgents(ctx, scope, skillsOnly) // picker on any TTY + selectAgents(ctx, scope, skillsOnly, interactive) - if !explicit && cmdio.IsPromptSupported(ctx) // plan summary + confirm + if !explicit && interactive ``` - The `install` help text now describes the non-interactive defaults. - The install funnel records `interactive` from the same check, so an agent caller's run is reported as non-interactive. - The interactive install tests clear the agent env with `isolateAgents`, so they keep prompting when the test binary runs under an agent (`CLAUDECODE`, `AI_AGENT`, ...). - Changelog fragment: `.nextchanges/cli/aitools-install-no-prompts-for-agents.md`. Co-authored-by: Isaac --- .../aitools-install-no-prompts-for-agents.md | 1 + cmd/aitools/funnel.go | 7 +- cmd/aitools/install.go | 61 ++++++++- cmd/aitools/install_test.go | 125 ++++++++++++++++++ cmd/aitools/scope.go | 4 +- cmd/aitools/uninstall.go | 2 +- libs/telemetry/protos/aitools_events.go | 6 +- 7 files changed, 192 insertions(+), 14 deletions(-) create mode 100644 .nextchanges/cli/aitools-install-no-prompts-for-agents.md diff --git a/.nextchanges/cli/aitools-install-no-prompts-for-agents.md b/.nextchanges/cli/aitools-install-no-prompts-for-agents.md new file mode 100644 index 00000000000..db58d6551c5 --- /dev/null +++ b/.nextchanges/cli/aitools-install-no-prompts-for-agents.md @@ -0,0 +1 @@ +* `databricks aitools install` no longer prompts when a coding agent runs it, even in a pseudo-terminal; it uses the non-interactive defaults instead and prints a note saying so. ([#7477](https://github.com/databricks/cli/pull/7477)) diff --git a/cmd/aitools/funnel.go b/cmd/aitools/funnel.go index ccdf85696b9..8a7dddfe38e 100644 --- a/cmd/aitools/funnel.go +++ b/cmd/aitools/funnel.go @@ -29,10 +29,11 @@ type commandFunnel struct { // startFunnel logs the STARTED event for command and returns the funnel to // report later steps and the outcome on. // installMode is set for install only, so it is on the STARTED event too. -func startFunnel(ctx context.Context, command protos.AitoolsCommand, installMode protos.AitoolsInstallMode) *commandFunnel { +// interactive is whether the command may show prompts. +func startFunnel(ctx context.Context, command protos.AitoolsCommand, installMode protos.AitoolsInstallMode, interactive bool) *commandFunnel { f := &commandFunnel{ command: command, - interactive: cmdio.IsPromptSupported(ctx), + interactive: interactive, installMode: installMode, } f.log(ctx, protos.AitoolsCommandEvent{Stage: protos.AitoolsCommandStageStarted}) @@ -119,7 +120,7 @@ func (f *commandFunnel) log(ctx context.Context, e protos.AitoolsCommandEvent) { // FINISHED (outcome from the returned error). func withCommandFunnel(command protos.AitoolsCommand, runE func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) (err error) { - f := startFunnel(cmd.Context(), command, "") + f := startFunnel(cmd.Context(), command, "", cmdio.IsPromptSupported(cmd.Context())) defer func() { f.finish(cmd.Context(), err) }() cmd.SetContext(context.WithValue(cmd.Context(), funnelKey{}, f)) return runE(cmd, args) diff --git a/cmd/aitools/install.go b/cmd/aitools/install.go index 78775dbf8c3..81cd6311bcd 100644 --- a/cmd/aitools/install.go +++ b/cmd/aitools/install.go @@ -14,6 +14,7 @@ import ( "github.com/databricks/cli/libs/flags" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go/useragent" "github.com/spf13/cobra" ) @@ -32,6 +33,33 @@ var ( // checked. The funnel reports it as CANCELLED: the user chose to act on nothing. var errNoAgentsSelected = errors.New("at least one agent must be selected") +// installPromptsSupported reports whether install may show its prompts. Agents +// often run commands in a pseudo-terminal (Cursor, Copilot in the VS Code +// terminal) where stdin looks like a TTY but nobody can answer a TUI prompt. +// Agent detection is the SDK's list of agent environment variables: +// https://github.com/databricks/databricks-sdk-go/blob/main/useragent/agent.go +// An agent caller always gets the documented non-interactive defaults. +func installPromptsSupported(ctx context.Context) bool { + return cmdio.IsPromptSupported(ctx) && useragent.AgentProvider() == "" +} + +// agentCallerNote is printed when install skips its prompts on a terminal +// because the caller is a coding agent. It names the detected agent by its +// --agents value when it has one, and the flags of `databricks aitools install` +// that would have replaced the skipped prompts. The command is named because +// the deprecated `experimental aitools skills install` alias does not accept +// these flags. +func agentCallerNote(missingFlags []string) string { + provider := useragent.AgentProvider() + detected := "a coding agent was" + if provider == "multiple" { + detected = "more than one coding agent was" + } else if agent := agents.AgentForProvider(provider); agent != nil { + detected = fmt.Sprintf("a coding agent (%s) was", agent.Name) + } + return fmt.Sprintf("No prompts shown because %s detected. Pass %s to databricks aitools install to choose what to install.", detected, strings.Join(missingFlags, " and ")) +} + // delivery is how the databricks tools are delivered to one agent. type delivery int @@ -102,6 +130,10 @@ Agent selection: (unset, interactive) A picker over all known agents, detected ones pre-checked. (unset, non-interactive) Act on every detected agent. +Non-interactive runs (no terminal, or called by a coding agent) never prompt: +scope defaults to global and agents to every detected one. On a terminal, a +coding agent caller gets a note saying so. + Output: --output json Emit a structured result instead of text. Requires --scope and --agents so the command runs without interactive prompts. @@ -118,7 +150,8 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), ctx = cmdio.WithQuiet(ctx) } - funnel := startFunnel(ctx, protos.AitoolsCommandInstall, installModeType(skillsOnly, pathFlag)) + interactive := installPromptsSupported(ctx) + funnel := startFunnel(ctx, protos.AitoolsCommandInstall, installModeType(skillsOnly, pathFlag), interactive) defer func() { funnel.finish(ctx, err) }() if skillsOnly && pathFlag != "" { @@ -206,8 +239,24 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), } funnel.step(ctx, protos.AitoolsCommandStepFlagsValidated) + // A person may be at this terminal with an agent env var inherited + // from an agent session, so say why the prompts they would expect + // were skipped. + if !interactive && cmdio.IsPromptSupported(ctx) { + var missing []string + if scope == "" { + missing = append(missing, "--scope") + } + if !explicit { + missing = append(missing, "--agents") + } + if len(missing) > 0 { + cmdio.LogString(ctx, agentCallerNote(missing)) + } + } + if scope == "" { - scope, err = resolveScopeWithPrompt(ctx) + scope, err = resolveScopeWithPrompt(ctx, interactive) if err != nil { return err } @@ -217,7 +266,7 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), funnel.step(ctx, protos.AitoolsCommandStepScopeResolved) if !explicit { - targetAgents, err = selectAgents(ctx, scope, skillsOnly) + targetAgents, err = selectAgents(ctx, scope, skillsOnly, interactive) if err != nil { return err } @@ -233,7 +282,7 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), funnel.step(ctx, protos.AitoolsCommandStepAgentsSelected) // In the interactive picker path, show a plan summary and confirm. - if !explicit && cmdio.IsPromptSupported(ctx) { + if !explicit && interactive { printPlanSummary(ctx, plan, scope) proceed, err := promptProceed() if err != nil { @@ -340,10 +389,10 @@ func installOutputIsJSON(cmd *cobra.Command) bool { // needs a config dir, so in --skills-only mode an agent is "detected" by its // config dir (PATH-independent); plugin delivery additionally detects agents by // their CLI binary on PATH, which fixes the Codex/Copilot config-dir miss. -func selectAgents(ctx context.Context, scope string, skillsOnly bool) ([]*agents.Agent, error) { +func selectAgents(ctx context.Context, scope string, skillsOnly, interactive bool) ([]*agents.Agent, error) { // Interactive: the picker decides; a prompt error or empty selection is a real // error, not a "nothing detected" no-op. - if cmdio.IsPromptSupported(ctx) { + if interactive { choices := agentChoices(ctx, scope, skillsOnly) if len(choices) == 0 { // Agents were detected but none can be acted on in this scope; the diff --git a/cmd/aitools/install_test.go b/cmd/aitools/install_test.go index 8d8f28f26fb..3795e25dd61 100644 --- a/cmd/aitools/install_test.go +++ b/cmd/aitools/install_test.go @@ -6,6 +6,7 @@ import ( "context" "encoding/json" "errors" + "io" "net/http" "net/http/httptest" "os" @@ -387,6 +388,7 @@ func TestInstallPluginFirstDefault(t *testing.T) { } func TestInstallInteractivePickerAndConfirm(t *testing.T) { + isolateAgents(t) setupTestAgents(t) fakeBinsOnPath(t, "claude") t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") @@ -657,6 +659,7 @@ func TestInstallSkillsRequiresSkillsOnlyOrPath(t *testing.T) { } func TestInstallInteractivePickerErrorPropagates(t *testing.T) { + isolateAgents(t) setupTestAgents(t) setupScopeMock(t, installer.ScopeGlobal) @@ -908,3 +911,125 @@ func TestResolveScopeValidation(t *testing.T) { }) } } + +// setupAgentCallerInstall sets up an install run by the agent caller that +// agentEnv names (key/value pairs), with Claude Code and Cursor detected. Every +// prompt fails the test if shown. isolateAgents clears the environment, so it +// runs first and the HOME, PATH and skills ref set below survive. +func setupAgentCallerInstall(t *testing.T, agentEnv ...string) (plugins *[]pluginCall, skills *[]installCall, scopePrompted *bool) { + t.Helper() + isolateAgents(t, agentEnv...) + setupTestAgents(t) // ~/.claude and ~/.cursor + fakeBinsOnPath(t, "claude") + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + plugins = setupPluginMock(t) + skills = setupInstallMock(t) + scopePrompted = setupScopeMock(t, installer.ScopeProject) + origPrompt := promptAgentSelection + t.Cleanup(func() { promptAgentSelection = origPrompt }) + promptAgentSelection = func(context.Context, []agentChoice) ([]*agents.Agent, error) { + t.Fatal("agent picker shown to an agent caller") + return nil, nil + } + origProceed := promptProceed + t.Cleanup(func() { promptProceed = origProceed }) + promptProceed = func() (bool, error) { + t.Fatal("plan confirm shown to an agent caller") + return false, nil + } + return plugins, skills, scopePrompted +} + +// runInstallWithStderr runs install with args and returns the telemetry +// context and everything written to stderr. +func runInstallWithStderr(t *testing.T, promptSupported bool, args ...string) (context.Context, string) { + t.Helper() + ctx, test := cmdio.SetupTest(t.Context(), cmdio.TestOptions{PromptSupported: promptSupported}) + go drainReader(test.Stdout) + // Buffered so the reader never blocks if the test stops before receiving. + stderr := make(chan []byte, 1) + go func() { + b, _ := io.ReadAll(test.Stderr) + stderr <- b + }() + ctx = telemetry.WithNewLogger(ctx) + cmd := newTestInstallCmd() + cmd.SetContext(ctx) + cmd.SetArgs(args) + + err := cmd.Execute() + test.Done() + out := string(<-stderr) + require.NoError(t, err, "stderr:\n%s", out) + return ctx, out +} + +func TestInstallAgentCallerInPTYNeverPrompts(t *testing.T) { + // Cursor and Copilot run agent commands in a pseudo-terminal, so stdin is a + // TTY, but no human answers the prompts. The run must use the + // non-interactive defaults instead of blocking on the scope select. + plugins, skills, scopePrompted := setupAgentCallerInstall(t, "CURSOR_AGENT", "1") + + ctx, stderr := runInstallWithStderr(t, true) + assert.Contains(t, stderr, "No prompts shown because a coding agent (cursor) was detected. Pass --scope and --agents to databricks aitools install to choose what to install.") + events := funnelEvents(ctx) + require.NotEmpty(t, events) + assert.False(t, events[0].Interactive) + assert.False(t, *scopePrompted) + require.Len(t, *plugins, 1) + assert.Equal(t, agents.NameClaudeCode, (*plugins)[0].agent) + require.Len(t, *skills, 1) + assert.Equal(t, installer.ScopeGlobal, (*skills)[0].opts.Scope) +} + +func TestInstallAgentCallerNoteNamesOnlyMissingFlags(t *testing.T) { + setupAgentCallerInstall(t, "CURSOR_AGENT", "1") + + _, stderr := runInstallWithStderr(t, true, "--scope", "global") + assert.Contains(t, stderr, "No prompts shown because a coding agent (cursor) was detected. Pass --agents to databricks aitools install to choose what to install.") +} + +func TestInstallAgentCallerNoteNamesAgent(t *testing.T) { + cases := []struct { + name string + env []string + want string + }{ + // The SDK reports copilot-cli; the note shows the --agents value. + {name: "registry alias", env: []string{"COPILOT_CLI", "1"}, want: "because a coding agent (copilot) was detected."}, + // vscode-agent has no registry entry, so no name is shown. + {name: "no registry entry", env: []string{"VSCODE_AGENT", "1"}, want: "because a coding agent was detected."}, + {name: "nested agents", env: []string{"CURSOR_AGENT", "1", "CLAUDECODE", "1"}, want: "because more than one coding agent was detected."}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + plugins, _, _ := setupAgentCallerInstall(t, tc.env...) + + _, stderr := runInstallWithStderr(t, true) + assert.Contains(t, stderr, tc.want) + require.Len(t, *plugins, 1) + }) + } +} + +func TestInstallAgentCallerNoteSilent(t *testing.T) { + cases := []struct { + name string + promptSupported bool + args []string + }{ + // Both flags given: no prompt would have run, so there is nothing to explain. + {name: "scope and agents passed", promptSupported: true, args: []string{"--scope", "global", "--agents", "claude-code"}}, + // No terminal: prompts are skipped for every caller, as before. + {name: "no terminal", promptSupported: false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + plugins, _, _ := setupAgentCallerInstall(t, "CURSOR_AGENT", "1") + + _, stderr := runInstallWithStderr(t, tc.promptSupported, tc.args...) + assert.NotContains(t, stderr, "No prompts shown") + require.Len(t, *plugins, 1) + }) + } +} diff --git a/cmd/aitools/scope.go b/cmd/aitools/scope.go index 8cb4dfeadb3..20f5545f542 100644 --- a/cmd/aitools/scope.go +++ b/cmd/aitools/scope.go @@ -62,8 +62,8 @@ func resolveScope(project, global bool) (string, error) { // resolveScopeWithPrompt resolves the scope when neither --project nor // --global is set: interactive mode shows a prompt (default: global), // non-interactive mode uses global. -func resolveScopeWithPrompt(ctx context.Context) (string, error) { - if cmdio.IsPromptSupported(ctx) { +func resolveScopeWithPrompt(ctx context.Context, interactive bool) (string, error) { + if interactive { return promptScopeSelection(ctx) } return installer.ScopeGlobal, nil diff --git a/cmd/aitools/uninstall.go b/cmd/aitools/uninstall.go index 8bbefd9f85a..474c34c9840 100644 --- a/cmd/aitools/uninstall.go +++ b/cmd/aitools/uninstall.go @@ -71,7 +71,7 @@ By default, removes all skills. Use --skills to remove specific skills only.`, // Uninstall is destructive, so confirm interactively before doing // anything. Non-interactive runs (no TTY) proceed unprompted so - // automation is unaffected, matching how install only prompts on a TTY. + // automation is unaffected. dir := globalDir if scope == installer.ScopeProject { dir = projectDir diff --git a/libs/telemetry/protos/aitools_events.go b/libs/telemetry/protos/aitools_events.go index 501a0a93b1b..a2c4a65f222 100644 --- a/libs/telemetry/protos/aitools_events.go +++ b/libs/telemetry/protos/aitools_events.go @@ -135,8 +135,10 @@ type AitoolsCommandEvent struct { Outcome AitoolsCommandOutcome `json:"outcome,omitempty"` ErrorCategory AitoolsErrorCategory `json:"error_category,omitempty"` - // Whether the CLI could show prompts (cmdio.IsPromptSupported). A - // measurable false, so no omitempty. + // Whether the command could prompt. For install: cmdio.IsPromptSupported + // and no coding agent detected, so an agent on a terminal reports false. + // For every other command: cmdio.IsPromptSupported. A measurable false, so + // no omitempty. Interactive bool `json:"interactive"` // Install only: delivery mode. Set on every install event once flags parse.