Skip to content
Open
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
1 change: 1 addition & 0 deletions .nextchanges/cli/aitools-auto-install-no-terminal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Automatic installation of Databricks AI tools no longer installs for the first time without asking when the CLI runs with no terminal (ssh without a terminal, nohup, cron, an IDE extension); the first install waits for a run that can ask. Updates of an existing installation still run without a terminal, and coding agents still get the tools without a question. ([#7834](https://github.com/databricks/cli/pull/7834))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
30 changes: 30 additions & 0 deletions acceptance/cmd/aitools/auto-install/no-terminal/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

=== no terminal and no coding agent: cannot ask, so nothing is installed or reported, and the version is not settled

>>> [CLI] jobs list

>>> print_telemetry

>>> cursor_skills
(no skills)

>>> auto_install_state
{"id": "treatment-machine"}

=== the version is still due: a coding agent's call installs without asking

>>> AGENT=test-agent [CLI] jobs list
Installed Databricks AI tools for Cursor. Disable: databricks aitools auto-install disable

>>> print_telemetry
{"command":"jobs_list","exit_code":0,"aitools_install_event":{"agents":["CURSOR"],"scope":"GLOBAL","error_category":"TYPE_UNSPECIFIED","trigger":"AUTO_FIRST_RUN"}}
{"command":"jobs_list","exit_code":0,"aitools_auto_install_event":{"outcome":"INSTALLED","holdout_arm":"TREATMENT","interactive":false}}

>>> cursor_skills
databricks-test

>>> auto_install_state
{
"last_version": "1.30.0",
"id": "treatment-machine"
}
15 changes: 15 additions & 0 deletions acceptance/cmd/aitools/auto-install/no-terminal/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use_home home
gate true
unset AGENT

title "no terminal and no coding agent: cannot ask, so nothing is installed or reported, and the version is not settled"
trace $CLI jobs list
trace print_telemetry
trace cursor_skills
trace auto_install_state

title "the version is still due: a coding agent's call installs without asking"
trace AGENT=test-agent $CLI jobs list
trace print_telemetry
trace cursor_skills
trace auto_install_state
6 changes: 6 additions & 0 deletions acceptance/cmd/aitools/auto-install/script.prepare
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ unset CI DATABRICKS_AITOOLS_AUTO_INSTALL GITHUB_ACTIONS GITLAB_CI JENKINS_URL TF
# Telemetry is printed by print_telemetry.
unset DATABRICKS_CLI_DISABLE_TELEMETRY DATABRICKS_CLI_TELEMETRY_TIMEOUT

# Without a terminal, only a coding agent's call installs: any other run cannot
# ask first, so it skips (see no-terminal). The cases have no terminal, so they
# run as a coding agent's call. AGENT is the generic marker; this name gets no
# agent hint.
export AGENT=test-agent
Comment thread
anton-107 marked this conversation as resolved.
Resolved

# Test binaries are development builds, which never auto-install. Each case
# names the released version it simulates.
export DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION=1.30.0
Expand Down
2 changes: 2 additions & 0 deletions acceptance/cmd/aitools/auto-install/upgrade/script
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ trace cursor_skills
echo "my notes" >> "$skills_dir/databricks-dropped/SKILL.md"
echo "my notes" >> "$skills_dir/databricks-edited/SKILL.md"

# An update does not ask, so it runs with no terminal and no coding agent too.
unset AGENT
title "upgrade to 1.31.0 with release 0.4.0: updates like aitools update, keeping the edited skills"
trace DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION=1.31.0 DATABRICKS_CLI_GITHUB_API_URL=$DATABRICKS_HOST/newer $CLI jobs list
trace print_telemetry
Expand Down
26 changes: 11 additions & 15 deletions cmd/aitools/autoinstall_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func autoInstall(ctx context.Context, skippedCommand bool) {
log.Debugf(ctx, "auto-install: skipped: %s", tools.skipWhy)
return
}
if err == nil && autoInstallAsks(ctx) && in.State.AcceptedVersion != version && !canAsk(ctx) && len(tools.newAgents()) > 0 {
if err == nil && autoInstallAsks() && in.State.AcceptedVersion != version && !canAsk(ctx) && len(tools.newAgents()) > 0 {
skipUnaskable(ctx, in.State.ID)
return
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func autoInstall(ctx context.Context, skippedCommand bool) {
version: version,
id: id,
arm: holdoutArm(in.Bucket),
interactive: autoInstallAsks(ctx),
interactive: cmdio.HasTerminal(ctx) && autoInstallAsks(),
}
if !reason.Due() {
// Another process settled the version while this one waited.
Expand Down Expand Up @@ -241,11 +241,11 @@ func (r autoInstallRun) attempt(ctx context.Context, prior autoinstall.State) {
attempt := autoinstall.NextAttempt(prior, r.version)
qctx := quietContext(ctx)
tools, err := planTools(qctx, prior)
// A person at the terminal is asked before a first install, once per
// version: a "yes" covers that version's retries. A planning failure is
// a failed attempt, and its retry asks.
// A caller that is not a coding agent is asked before a first install,
// once per version: a "yes" covers that version's retries. A planning
// failure is a failed attempt, and its retry asks.
asked := false
if err == nil && r.interactive && prior.AcceptedVersion != r.version && len(tools.newAgents()) > 0 {
if err == nil && autoInstallAsks() && prior.AcceptedVersion != r.version && len(tools.newAgents()) > 0 {
// autoInstall skips this case before the lock; the state it decided
// on may have changed since.
if !canAsk(ctx) {
Expand Down Expand Up @@ -306,15 +306,11 @@ func (r autoInstallRun) attempt(ctx context.Context, prior autoinstall.State) {
}
}

// autoInstallAsks reports whether a person may be at the terminal, so
// auto-install asks before installing: stdin, stdout or stderr is a terminal
// and no coding agent is detected (see installPromptsSupported). It asks only
// when the question can be seen and answered (cmdio.IsLinePromptSupported)
// and skips otherwise; the install without asking is left to runs with no
// terminal at all. The question is a plain-text line, so TERM=dumb and an
// MSYS2 pty pipe, which turn off the TUI prompts of install, do not skip it.
func autoInstallAsks(ctx context.Context) bool {
return cmdio.HasTerminal(ctx) && useragent.AgentProvider() == ""
// autoInstallAsks reports whether auto-install asks before a first install:
// no coding agent is detected. A run that cannot ask (see canAsk) skips;
// coding agents install without a question.
func autoInstallAsks() bool {
return useragent.AgentProvider() == ""
}

// canAsk reports whether the question can be seen and answered: see
Expand Down
61 changes: 59 additions & 2 deletions cmd/aitools/autoinstall_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/telemetry"
"github.com/databricks/cli/libs/telemetry/protos"
"github.com/databricks/databricks-sdk-go/useragent"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -49,6 +50,9 @@ func TestAutoInstallPrompt(t *testing.T) {
installState string
// env is set on top of a terminal with color.
env map[string]string
// agent is a coding agent's env var, set to 1: the run is that
// agent's call.
agent string
// stdinPiped, stdoutRedirected and stderrRedirected make that
// stream not a terminal.
stdinPiped bool
Expand Down Expand Up @@ -154,7 +158,16 @@ func TestAutoInstallPrompt(t *testing.T) {
wantState: autoinstall.State{ID: "treatment-machine"},
},
{
name: "no terminal at all installs without asking",
// ssh, nohup, cron or an IDE extension: nobody sees the question.
name: "no terminal at all skips without installing",
stdinPiped: true,
stdoutRedirected: true,
stderrRedirected: true,
wantState: autoinstall.State{ID: "treatment-machine"},
},
{
name: "a coding agent with no terminal installs without asking",
agent: "CLAUDECODE",
stdinPiped: true,
stdoutRedirected: true,
stderrRedirected: true,
Expand All @@ -163,6 +176,15 @@ func TestAutoInstallPrompt(t *testing.T) {
wantState: autoinstall.State{LastVersion: "1.30.0", ID: "treatment-machine"},
wantOutcome: protos.AitoolsAutoInstallOutcomeInstalled,
},
{
name: "a coding agent in a terminal installs without asking",
agent: "CURSOR_AGENT",
input: "n\n",
wantStderr: []string{"Installed Databricks AI tools for Cursor."},
wantSkills: true,
wantState: autoinstall.State{LastVersion: "1.30.0", ID: "treatment-machine"},
wantOutcome: protos.AitoolsAutoInstallOutcomeInstalled,
},
{
name: "a retry after yes is not asked again",

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.

[nice to have] After this PR, the only first install a run with no terminal and no coding agent still makes is a retry of a version the user already accepted (accepted_version equals the version, so the checks at autoinstall_run.go:151 and :248 pass). No test covers it. This case runs with a TTY stdin, and the new headless cases have no accepted version. A later change that made the accepted-version check depend on canAsk would stop headless retries after a "yes" without failing any test. Consider adding a table case with stdinPiped, stdoutRedirected and stderrRedirected and the accepted state, expecting INSTALLED with interactive=false.

state: `{"id": "treatment-machine", "last_version": "1.30.0", "attempts": 1, "accepted_version": "1.30.0"}`,
Expand Down Expand Up @@ -217,6 +239,9 @@ func TestAutoInstallPrompt(t *testing.T) {
for k, v := range tc.env {
t.Setenv(k, v)
}
if tc.agent != "" {
setCodingAgent(t, tc.agent)
}
var stdout, stderr bytes.Buffer
outW, errW := cmdio.FakeTTY(&stdout), cmdio.FakeTTY(&stderr)
if tc.stdoutRedirected {
Expand Down Expand Up @@ -250,7 +275,7 @@ func TestAutoInstallPrompt(t *testing.T) {
for _, e := range telemetry.BufferedEvents(ctx) {
if e := e.AitoolsAutoInstallEvent; e != nil {
outcomes = append(outcomes, e.Outcome)
assert.Equal(t, !tc.stdinPiped || !tc.stdoutRedirected || !tc.stderrRedirected, e.Interactive)
assert.Equal(t, tc.agent == "" && (!tc.stdinPiped || !tc.stdoutRedirected || !tc.stderrRedirected), e.Interactive)
assert.Equal(t, protos.AitoolsHoldoutArmTreatment, e.HoldoutArm)
}
}
Expand Down Expand Up @@ -290,6 +315,7 @@ func TestAutoInstallKeepsTheIDWhenItCannotAsk(t *testing.T) {
// after one command of a version, not after each of them.
func TestAutoInstallLooksTheGateUpOncePerVersion(t *testing.T) {
setupAutoInstallHome(t, "")
setCodingAgent(t, "CLAUDECODE")
lookups := 0
t.Cleanup(func() { gateOn = autoinstall.GateOn })
gateOn = func(context.Context, string) (bool, error) {
Expand Down Expand Up @@ -320,6 +346,7 @@ func TestAutoInstallLooksTheGateUpOncePerVersion(t *testing.T) {
// the gate on. The run stops instead of installing.
func TestAutoInstallStopsWhenTheGateLookupIsInterrupted(t *testing.T) {
home := setupAutoInstallHome(t, "")
setCodingAgent(t, "CLAUDECODE")
var interrupt context.CancelFunc
t.Cleanup(func() {
gateOn = autoinstall.GateOn
Expand Down Expand Up @@ -350,6 +377,7 @@ func TestAutoInstallStopsWhenTheGateLookupIsInterrupted(t *testing.T) {
// gate up.
func TestAutoInstallSkipsTheGateLookupWithoutAgents(t *testing.T) {
home := setupAutoInstallHome(t, "")
setCodingAgent(t, "CLAUDECODE")
require.NoError(t, os.RemoveAll(filepath.Join(home, ".cursor")))
t.Cleanup(func() { gateOn = autoinstall.GateOn })
gateOn = func(context.Context, string) (bool, error) {
Expand Down Expand Up @@ -391,6 +419,9 @@ func TestAutoInstallSkipsRootInAUserHome(t *testing.T) {
// when rootInHome is set, and counts the gate lookups.
func runRootInUserHome(t *testing.T, rootInHome bool) (ctx context.Context, home string, lookups *int) {
home = setupAutoInstallHome(t, "")
// A coding agent's call installs without a question, so the user's run
// needs no terminal.
setCodingAgent(t, "CLAUDECODE")
if rootInHome {
// The state the setup seeds would hide a state file or lock file
// the run creates.
Expand All @@ -410,6 +441,25 @@ func runRootInUserHome(t *testing.T, rootInHome bool) (ctx context.Context, home
return ctx, home, lookups
}

// A run with no terminal and no coding agent cannot ask, so it skips before the
// gate lookup: a cron job or a job pod does not wait for it on every run.
func TestAutoInstallSkipsTheGateLookupWhenItCannotAsk(t *testing.T) {

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] TestAutoInstallSkipsTheGateLookupWhenItCannotAsk repeats TestAutoInstallSkipsTheGateLookupWithoutAgents almost line for line: same setup, same failing gateOn fake, same assertions. The only differences are agent versus no agent and whether .cursor exists. .agents/rules/testing.md prefers a table for similar cases, and a table would make that difference explicit. It would also be a place to say why setCodingAgent is now needed in ...WithoutAgents: it keeps that test on the no-agents skip instead of the cannot-ask skip.

home := setupAutoInstallHome(t, "")
t.Cleanup(func() { gateOn = autoinstall.GateOn })
gateOn = func(context.Context, string) (bool, error) {
t.Error("the gate was looked up")
return true, nil
}
ctx := cmdio.MockDiscard(t.Context())
ctx = telemetry.WithNewLogger(ctx)
runAutoInstallHook(ctx)
assert.Empty(t, telemetry.BufferedEvents(ctx))
assert.NoDirExists(t, filepath.Join(home, ".cursor", "skills"))
state, err := autoinstall.Load(ctx)
require.NoError(t, err)
assert.Equal(t, autoinstall.State{ID: "treatment-machine"}, state, "the version is not settled")
}

func TestToolsPlanNewAgents(t *testing.T) {
claude := &agents.Agent{DisplayName: "Claude Code"}
cursor := &agents.Agent{DisplayName: "Cursor"}
Expand Down Expand Up @@ -438,6 +488,13 @@ func runAutoInstallHook(ctx context.Context) {
AutoInstall(list)
}

// setCodingAgent makes the run the call of the coding agent whose env var is
// name. setupAutoInstallHome resets the SDK's cached agent after the test.
func setCodingAgent(t *testing.T, name string) {

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] setCodingAgent sets the env var with t.Setenv and clears the SDK cache, but it depends on running after setupAutoInstallHome. That call's isolateAgents clears the whole environment, and its cleanup resets the cache. Called earlier, the variable is wiped. Called without it, a cached agent can leak into later tests. All current callers run it in the right order. The existing pattern, passing the agent pair to isolateAgents as setupAgentCallerInstall in install_test.go does, would avoid the ordering dependency: for example, let setupAutoInstallHome accept extra env pairs.

t.Setenv(name, "1")
useragent.ClearCache()
}

// setupAutoInstallHome gives the run a clean environment with Cursor's config
// dir, a skills server, the gate on and a released CLI version. state seeds
// the auto-install state (a treatment-arm machine id by default).
Expand Down
Loading