Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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))
7 changes: 4 additions & 3 deletions cmd/aitools/funnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand Down
61 changes: 55 additions & 6 deletions cmd/aitools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] .agents/rules/comments.md asks for Go doc comments that end with a period. This one ends with the SDK URL. Moving the "Agent detection is the SDK's list…" lines above the last sentence keeps the link and fixes it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in a475e72. I moved the SDK link lines above the last sentence, so the doc comment now ends with a period.

// 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

Expand Down Expand Up @@ -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.
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The deprecated databricks experimental aitools skills install alias also runs NewInstallCmd (cmd/aitools/legacy_skills.go:45-58), but it accepts only [skill-name] and --experimental. An agent on a terminal that runs the alias now sees "Pass --scope and --agents to choose what to install.", and retrying the alias with --scope global fails with unknown flag: --scope. The deprecation warning does point at databricks aitools install, so this is minor. Naming the command in the note (for example "Pass --scope and --agents to databricks aitools install ...") makes the advice correct from both entry points.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d6c9440. The note now names the command: "Pass --scope and --agents to databricks aitools install to choose what to install." That advice is correct from both databricks aitools install and the deprecated experimental aitools skills install alias.

}
}

if scope == "" {
scope, err = resolveScopeWithPrompt(ctx)
scope, err = resolveScopeWithPrompt(ctx, interactive)
if err != nil {
return err
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
125 changes: 125 additions & 0 deletions cmd/aitools/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -657,6 +659,7 @@ func TestInstallSkillsRequiresSkillsOnlyOrPath(t *testing.T) {
}

func TestInstallInteractivePickerErrorPropagates(t *testing.T) {
isolateAgents(t)
setupTestAgents(t)
setupScopeMock(t, installer.ScopeGlobal)

Expand Down Expand Up @@ -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
Comment on lines +928 to +932

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The test fails fast if the scope prompt or the agent picker runs, but promptProceed stays the real huh.NewConfirm().Run(), which reads the real terminal. If the confirm gate at install.go:241 regressed alone, the test would hit the real widget and fail with an opaque prompt error (or block) instead of naming the regression. Stub promptProceed to t.Fatal("plan confirm shown to an agent caller") too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d0c5cc6. The test now stubs promptProceed to t.Fatal("plan confirm shown to an agent caller").

}
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)
})
}
}
4 changes: 2 additions & 2 deletions cmd/aitools/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/aitools/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions libs/telemetry/protos/aitools_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading