diff --git a/cmd/aitools/autoinstall.go b/cmd/aitools/autoinstall.go index bdd174deffe..ccd5aa70577 100644 --- a/cmd/aitools/autoinstall.go +++ b/cmd/aitools/autoinstall.go @@ -20,11 +20,12 @@ On the first run of a new CLI version, the CLI can install the Databricks AI tools (skills and plugins) for the coding agents already on this machine, as 'databricks aitools install --scope=global' does, or update an existing installation, as 'databricks aitools update' does. It never runs in CI or on -Databricks Runtime, and only touches the global scope. +Databricks Runtime, and only touches the global scope. In a terminal it asks +before the first install. It stays off after 'databricks aitools auto-install disable', a global-scope -'databricks aitools uninstall', or a "no" to the skills prompt of -'databricks apps init', until 'databricks aitools auto-install enable'. +'databricks aitools uninstall', a "no" at that prompt, or a "no" to the skills +prompt of 'databricks apps init', until 'databricks aitools auto-install enable'. Setting ` + autoinstall.EnvVar + ` to anything but a true value (true, 1, yes, on) turns it off in that environment.`, RunE: root.ReportUnknownSubcommand, diff --git a/cmd/aitools/autoinstall_run.go b/cmd/aitools/autoinstall_run.go index 88cb1444bd0..288e295e2a8 100644 --- a/cmd/aitools/autoinstall_run.go +++ b/cmd/aitools/autoinstall_run.go @@ -7,6 +7,8 @@ import ( "io" "log/slog" "maps" + "os" + "os/signal" "runtime" "slices" "strings" @@ -22,6 +24,7 @@ import ( "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/cli/libs/versioncheck" + "github.com/databricks/databricks-sdk-go/useragent" "github.com/spf13/cobra" ) @@ -35,6 +38,9 @@ const ( // agent CLIs it runs). One that runs out of time counts as a failed // attempt and is retried on a later run. installTimeout = 10 * time.Second + + // promptTimeout bounds the wait for an answer at the install prompt. + promptTimeout = time.Minute ) // autoInstallDisable is the opt-out command named in the notice. @@ -43,8 +49,9 @@ const autoInstallDisable = "databricks aitools auto-install disable" // AutoInstall installs Databricks AI tools for the coding agents on this // machine, or updates an existing installation, on the first run of a new CLI // version. The root command runs it after a command succeeded. It never fails -// the command and never writes to stdout: it prints at most one line to -// stderr, and why it skipped or failed only to the debug log. +// the command and never writes to stdout. On stderr it prints the install +// prompt (in a terminal) and one line on what it did; why it skipped or failed +// goes only to the debug log. func AutoInstall(cmd *cobra.Command) { ctx := cmd.Context() defer func() { @@ -98,10 +105,22 @@ func autoInstall(ctx context.Context, skippedCommand bool) { log.Debugf(ctx, "auto-install: skipped: %v", err) return } - if reason := autoinstall.Decide(in); !reason.Due() { + reason := autoinstall.Decide(in) + if !reason.Due() { log.Debugf(ctx, "auto-install: skipped: %s", reason) return } + // A run that would ask before installing but cannot skips before the lock + // and the gate lookup, since it repeats on every such run until a run that + // can ask. A holdout machine or a gate-off version would otherwise settle + // after the lookup; they wait for that run too, so holdout and treatment + // machines are counted alike. + if reason == autoinstall.ReasonGateOff && autoInstallAsks(ctx) && in.State.AcceptedVersion != version && !canAsk(ctx) { + if tools, err := planTools(quietContext(ctx), in.State); err == nil && len(tools.newAgents()) > 0 { + skipUnaskable(ctx, in.State.ID) + return + } + } unlock, ok := autoinstall.TryLock(ctx) if !ok { @@ -117,13 +136,19 @@ func autoInstall(ctx context.Context, skippedCommand bool) { } id := in.State.ID if id == "" { - id = autoinstall.NewID() + // Saved before it decides anything, so this run and one that cannot + // ask (skipUnaskable, which does not hold this lock) use the same ID. + id, err = autoinstall.SaveID(ctx, autoinstall.NewID()) + if err != nil { + log.Debugf(ctx, "auto-install: skipped: could not save the state: %v", err) + return + } } in.Bucket = autoinstall.Bucket(id) // The gate costs a manifest lookup, so it is resolved only when it is // what decides the run. - reason := autoinstall.Decide(in) + reason = autoinstall.Decide(in) if reason == autoinstall.ReasonGateOff { gateCtx, cancel := context.WithTimeout(ctx, gateTimeout) in.GateOn, err = autoinstall.GateOn(gateCtx, version) @@ -146,7 +171,7 @@ func autoInstall(ctx context.Context, skippedCommand bool) { version: version, id: id, arm: holdoutArm(in.Bucket), - interactive: installPromptsSupported(ctx), + interactive: autoInstallAsks(ctx), } if !reason.Due() { // Another process settled the version while this one waited. @@ -174,6 +199,22 @@ 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. + asked := false + if err == nil && r.interactive && 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) { + skipUnaskable(ctx, r.id) + return + } + if !r.ask(ctx, tools.newAgents()) { + return + } + asked = true + } // Count the attempt before making it: an attempt that never finishes (the // process is killed) still counts, and a state that cannot be saved stops // auto-install instead of retrying it on every run. @@ -207,6 +248,10 @@ func (r autoInstallRun) attempt(ctx context.Context, prior autoinstall.State) { } log.Debugf(ctx, "auto-install: attempt %d of %d failed: %v", attempt, autoinstall.MaxAttempts, err) r.report(ctx, outcome, classifyInstallError(err)) + if asked { + // The user is waiting for the answer to take effect. + cmdio.LogString(ctx, "Could not install Databricks AI tools. To install them, run: databricks aitools install") + } default: r.settle(ctx, result.outcome) if result.outcome == protos.AitoolsAutoInstallOutcomeInstalled && len(result.agents) > 0 { @@ -219,6 +264,106 @@ 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() == "" +} + +// canAsk reports whether the question can be seen and answered: see +// cmdio.IsLinePromptSupported. A CLI process that the CLI started itself does +// not ask either; the parent asks after its own command. +func canAsk(ctx context.Context) bool { + return cmdio.IsLinePromptSupported(ctx) && !agents.IsSelfExec(ctx) +} + +// skipUnaskable skips a run that would ask before installing but cannot: the +// question could not be seen or answered safely, and installing without it +// would skip the consent. The version is not settled, so a later run that can +// ask does. A machine with no stored ID gets one, so that such runs do not +// draw a new holdout bucket each time. +func skipUnaskable(ctx context.Context, storedID string) { + log.Debugf(ctx, "auto-install: skipped: cannot ask (a stream is not a terminal, the CLI runs in the background or for another CLI process, or stdin is in use)") + if storedID != "" { + return + } + if _, err := autoinstall.SaveID(ctx, autoinstall.NewID()); err != nil { + log.Debugf(ctx, "auto-install: could not save the state: %v", err) + } +} + +// skippedNotice is printed when the question was answered with neither yes nor +// no, or the "no" could not be saved. +const skippedNotice = "Skipped. To install Databricks AI tools later, run: databricks aitools install" + +// ask asks whether to install for the agents names and reports whether to go +// ahead. What was typed before the question, such as the next command typed +// while this one ran, is discarded unread; where it cannot be (a pipe that +// Git Bash's mintty counts as a terminal), the question is not asked. The +// version is settled before asking, so the question is asked once per +// version even when it is never answered: the process is killed, stdin is +// closed, Ctrl+C is pressed, nobody answers within promptTimeout (an +// unrecognized agent in a pseudo-terminal), or the answer is neither yes nor +// no. "Yes" is recorded so that the version's retries do not ask again; only +// "no" stores the opt-out. +func (r autoInstallRun) ask(ctx context.Context, names []string) bool { + if err := cmdio.DiscardInput(ctx); err != nil { + log.Debugf(ctx, "auto-install: could not discard the pending input: %v", err) + skipUnaskable(ctx, r.id) + return false + } + if err := autoinstall.Record(ctx, r.version, 0, r.id); err != nil { + log.Debugf(ctx, "auto-install: skipped: could not save the state: %v", err) + return false + } + actx, cancel := context.WithTimeout(ctx, promptTimeout) + defer cancel() + // Ctrl+C dismisses the question instead of killing a command that + // succeeded. + actx, stop := signal.NotifyContext(actx, os.Interrupt) + defer stop() + ans, err := cmdio.AskCancellable(actx, "Install Databricks AI tools for "+strings.Join(names, ", ")+"? [Y/n]", "") + if err != nil { + log.Debugf(ctx, "auto-install: skipped: the prompt was not answered: %v", err) + // End the question's line. + cmdio.LogString(ctx, "") + r.settle(ctx, protos.AitoolsAutoInstallOutcomeUnanswered) + return false + } + switch strings.ToLower(strings.TrimSpace(ans)) { + case "", "y", "yes": + if err := autoinstall.Accept(ctx, r.version); err != nil { + // The install goes ahead; a retry of it asks again. + log.Debugf(ctx, "auto-install: could not save the answer: %v", err) + } + return true + case "n", "no": + r.decline(ctx) + return false + } + log.Debugf(ctx, "auto-install: skipped: the answer was neither yes nor no") + r.settle(ctx, protos.AitoolsAutoInstallOutcomeUnanswered) + cmdio.LogString(ctx, skippedNotice) + return false +} + +// decline stores a "no" at the prompt as the opt-out, so no later version +// asks again. +func (r autoInstallRun) decline(ctx context.Context) { + r.settle(ctx, protos.AitoolsAutoInstallOutcomeDeclined) + if err := autoinstall.Decline(ctx); err != nil { + log.Warnf(ctx, "Could not save the answer, so a later CLI version asks again: %v", err) + cmdio.LogString(ctx, skippedNotice) + return + } + cmdio.LogString(ctx, "Skipped. You won't be asked again; to install Databricks AI tools later, run: databricks aitools install") +} + // stateFreeSkips are the reasons Decide finds without the state. var stateFreeSkips = []autoinstall.Reason{ autoinstall.ReasonSkippedCommand, @@ -344,6 +489,22 @@ func planTools(ctx context.Context, prior autoinstall.State) (toolsPlan, error) return toolsPlan{globalDir: globalDir, plan: plan}, nil } +// newAgents names the agents a fresh install gives tools to: skills, or a +// plugin whose CLI is on PATH. An agent that keeps the plugin it has gets +// nothing. It is empty unless the plan is a first install (see fresh). +func (t toolsPlan) newAgents() []string { + if !t.fresh() { + return nil + } + var names []string + for _, it := range t.plan { + if it.delivery == deliverySkills || (it.delivery == deliveryPlugin && !it.cliMissing) { + names = append(names, it.agent.DisplayName) + } + } + return names +} + // agentCLIsStoppable reports whether a timeout stops an agent CLI together // with its descendants. On Windows it stops only the CLI, and a descendant // left running could still be installing when the next run takes the lock, diff --git a/cmd/aitools/autoinstall_run_test.go b/cmd/aitools/autoinstall_run_test.go new file mode 100644 index 00000000000..4f484d39833 --- /dev/null +++ b/cmd/aitools/autoinstall_run_test.go @@ -0,0 +1,350 @@ +package aitools + +import ( + "bytes" + "context" + "io" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/autoinstall" + "github.com/databricks/cli/libs/aitools/installer" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/dbr" + "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/telemetry" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const autoInstallTestManifest = `{ + "version": "2", + "updated_at": "2026-01-01T00:00:00Z", + "skills": { + "databricks-test": {"version": "1.0.0", "description": "Test skill", "files": ["SKILL.md"], "repo_dir": "skills"} + } +}` + +// The interactive prompt needs a terminal, which acceptance tests do not +// have, so these cases drive the hook the root command runs with stdin as a +// test terminal; the rest of the terminal is detected as in a real run. Like +// the acceptance tests in acceptance/cmd/aitools/auto-install, they check +// only what the user and the backend see: the prompt and notice on stderr, +// the skills in Cursor's directory, the stored state and the telemetry +// event. +func TestAutoInstallPrompt(t *testing.T) { + tests := []struct { + name string + state string + input string + // installState replaces the global install state file. + installState string + // env is set on top of a terminal with color. + env map[string]string + // stdinPiped, stdoutRedirected and stderrRedirected make that + // stream not a terminal. + stdinPiped bool + stdoutRedirected bool + stderrRedirected bool + + wantPrompt bool + wantStderr []string + wantSkills bool + wantState autoinstall.State + wantOutcome protos.AitoolsAutoInstallOutcome + }{ + { + name: "enter accepts", + wantPrompt: true, + input: "\n", + wantStderr: []string{"Installed Databricks AI tools for Cursor. Disable: databricks aitools auto-install disable"}, + wantSkills: true, + wantState: autoinstall.State{LastVersion: "1.30.0", AcceptedVersion: "1.30.0", ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeInstalled, + }, + { + name: "yes accepts", + wantPrompt: true, + input: "Y\n", + wantStderr: []string{"Installed Databricks AI tools for Cursor."}, + wantSkills: true, + wantState: autoinstall.State{LastVersion: "1.30.0", AcceptedVersion: "1.30.0", ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeInstalled, + }, + { + name: "no declines and opts out", + wantPrompt: true, + input: "n\n", + wantStderr: []string{"Skipped. You won't be asked again; to install Databricks AI tools later, run: databricks aitools install"}, + wantState: autoinstall.State{LastVersion: "1.30.0", OptedOut: true, ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeDeclined, + }, + { + name: "no answer settles the version without opting out", + input: "", + wantPrompt: true, + wantState: autoinstall.State{LastVersion: "1.30.0", ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeUnanswered, + }, + { + name: "another answer settles the version without opting out", + input: "git status\n", + wantPrompt: true, + wantStderr: []string{"Skipped. To install Databricks AI tools later, run: databricks aitools install"}, + wantState: autoinstall.State{LastVersion: "1.30.0", ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeUnanswered, + }, + { + name: "a planning failure is a failed attempt, retried later", + installState: "not json", + wantState: autoinstall.State{LastVersion: "1.30.0", Attempts: 1, ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeFailed, + }, + { + name: "a failed install after yes says so", + env: map[string]string{"DATABRICKS_SKILLS_BASE_URL": "http://127.0.0.1:1"}, + wantPrompt: true, + input: "\n", + wantStderr: []string{"Could not install Databricks AI tools. To install them, run: databricks aitools install"}, + wantState: autoinstall.State{LastVersion: "1.30.0", Attempts: 1, AcceptedVersion: "1.30.0", Installing: true, ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeFailed, + }, + { + name: "NO_COLOR still asks", + env: map[string]string{"NO_COLOR": "1"}, + wantPrompt: true, + input: "\n", + wantStderr: []string{"Installed Databricks AI tools for Cursor."}, + wantSkills: true, + wantState: autoinstall.State{LastVersion: "1.30.0", AcceptedVersion: "1.30.0", ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeInstalled, + }, + { + name: "TERM=dumb still asks", + env: map[string]string{"TERM": "dumb"}, + wantPrompt: true, + input: "n\n", + wantState: autoinstall.State{LastVersion: "1.30.0", OptedOut: true, ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeDeclined, + }, + { + name: "redirected stderr skips without installing", + stderrRedirected: true, + input: "\n", + wantState: autoinstall.State{ID: "treatment-machine"}, + }, + { + name: "piped stdin skips without installing", + stdinPiped: true, + input: "\n", + wantState: autoinstall.State{ID: "treatment-machine"}, + }, + { + name: "stdout to a pager skips without installing", + stdoutRedirected: true, + input: "\n", + wantState: autoinstall.State{ID: "treatment-machine"}, + }, + { + name: "no terminal at all installs without asking", + stdinPiped: true, + stdoutRedirected: true, + stderrRedirected: true, + 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", + state: `{"id": "treatment-machine", "last_version": "1.30.0", "attempts": 1, "accepted_version": "1.30.0"}`, + input: "", + wantStderr: []string{"Installed Databricks AI tools for Cursor."}, + wantSkills: true, + wantState: autoinstall.State{LastVersion: "1.30.0", AcceptedVersion: "1.30.0", ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeInstalled, + }, + { + // A failed gate lookup uses up an attempt without asking. + name: "a retry that was never asked asks", + state: `{"id": "treatment-machine", "last_version": "1.30.0", "attempts": 1}`, + wantPrompt: true, + input: "\n", + wantSkills: true, + wantState: autoinstall.State{LastVersion: "1.30.0", AcceptedVersion: "1.30.0", ID: "treatment-machine"}, + wantOutcome: protos.AitoolsAutoInstallOutcomeInstalled, + }, + { + name: "a retry that was never asked skips when it cannot ask", + state: `{"id": "treatment-machine", "last_version": "1.30.0", "attempts": 1}`, + stdoutRedirected: true, + input: "\n", + wantState: autoinstall.State{LastVersion: "1.30.0", Attempts: 1, ID: "treatment-machine"}, + }, + { + name: "a CLI process started by the CLI does not ask", + env: map[string]string{"DATABRICKS_CLI_SELF_EXEC": "1"}, + input: "\n", + wantState: autoinstall.State{ID: "treatment-machine"}, + }, + { + // Holdout and treatment machines both wait for a run that can ask. + name: "a holdout machine that cannot ask waits too", + state: `{"id": "holdout-machine"}`, + stderrRedirected: true, + input: "\n", + wantState: autoinstall.State{ID: "holdout-machine"}, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + home := setupAutoInstallHome(t, tc.state) + t.Cleanup(autoinstall.ResetDeclined) + if tc.installState != "" { + dir := filepath.Join(home, ".databricks", "aitools", "skills") + require.NoError(t, os.MkdirAll(dir, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, ".state.json"), []byte(tc.installState), 0o600)) + } + t.Setenv("TERM", "xterm") + for k, v := range tc.env { + t.Setenv(k, v) + } + var stdout, stderr bytes.Buffer + outW, errW := cmdio.FakeTTY(&stdout), cmdio.FakeTTY(&stderr) + if tc.stdoutRedirected { + outW = &stdout + } + if tc.stderrRedirected { + errW = &stderr + } + var in io.Reader = strings.NewReader(tc.input) + if !tc.stdinPiped { + in = cmdio.FakeTTYReader(in) + } + ctx := cmdio.InContext(t.Context(), cmdio.NewIO(t.Context(), flags.OutputText, in, outW, errW, "", "")) + ctx = telemetry.WithNewLogger(ctx) + runAutoInstallHook(ctx) + + assert.Empty(t, stdout.String()) + assert.Equal(t, tc.wantPrompt, strings.Contains(stderr.String(), "Install Databricks AI tools for Cursor? [Y/n]: "), "prompted") + for _, want := range tc.wantStderr { + assert.Contains(t, stderr.String(), want) + } + + _, err := os.Stat(filepath.Join(home, ".cursor", "skills", "databricks-test", "SKILL.md")) + assert.Equal(t, tc.wantSkills, err == nil, "Cursor has the skill") + + state, err := autoinstall.Load(ctx) + require.NoError(t, err) + assert.Equal(t, tc.wantState, state) + + var outcomes []protos.AitoolsAutoInstallOutcome + 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, protos.AitoolsHoldoutArmTreatment, e.HoldoutArm) + } + } + if tc.wantOutcome == "" { + assert.Empty(t, outcomes) + } else { + assert.Equal(t, []protos.AitoolsAutoInstallOutcome{tc.wantOutcome}, outcomes) + } + }) + } +} + +// A run that cannot ask on a machine with no stored ID saves the ID it drew, +// so that later runs keep the machine's holdout bucket. +func TestAutoInstallKeepsTheIDWhenItCannotAsk(t *testing.T) { + setupAutoInstallHome(t, "{}") + t.Setenv("TERM", "xterm") + var stderr bytes.Buffer + run := func() autoinstall.State { + ctx := cmdio.InContext(t.Context(), cmdio.NewIO(t.Context(), flags.OutputText, strings.NewReader("\n"), io.Discard, cmdio.FakeTTY(&stderr), "", "")) + ctx = telemetry.WithNewLogger(ctx) + runAutoInstallHook(ctx) + assert.Empty(t, telemetry.BufferedEvents(ctx)) + state, err := autoinstall.Load(ctx) + require.NoError(t, err) + return state + } + + first := run() + assert.NotEmpty(t, first.ID) + assert.Empty(t, first.LastVersion, "the version is not settled") + assert.Equal(t, first, run()) + assert.NotContains(t, stderr.String(), "Install Databricks AI tools") +} + +func TestToolsPlanNewAgents(t *testing.T) { + claude := &agents.Agent{DisplayName: "Claude Code"} + cursor := &agents.Agent{DisplayName: "Cursor"} + codex := &agents.Agent{DisplayName: "Codex CLI"} + plan := []agentPlanItem{ + {agent: claude, delivery: deliveryPlugin}, + {agent: cursor, delivery: deliverySkills}, + {agent: codex, delivery: deliveryPlugin, cliMissing: true}, + } + + assert.Equal(t, []string{"Claude Code", "Cursor"}, toolsPlan{plan: plan}.newAgents()) + assert.Empty(t, toolsPlan{plan: plan[2:]}.newAgents(), "an agent that keeps its plugin gets nothing") + assert.Empty(t, toolsPlan{existing: &installer.InstallState{}, plan: plan}.newAgents(), "an update does not ask") +} + +// runAutoInstallHook runs the hook as the root command does after +// `databricks jobs list` succeeded. +func runAutoInstallHook(ctx context.Context) { + ctx = dbr.MockRuntime(ctx, dbr.Environment{}) + root := &cobra.Command{Use: "databricks"} + jobs := &cobra.Command{Use: "jobs"} + list := &cobra.Command{Use: "list"} + root.AddCommand(jobs) + jobs.AddCommand(list) + list.SetContext(ctx) + AutoInstall(list) +} + +// 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). +func setupAutoInstallHome(t *testing.T, state string) string { + t.Helper() + home := t.TempDir() + cacheDir := t.TempDir() + url := serveSkills(t, autoInstallTestManifest, map[string]string{ + "/test-ref/skills/databricks-test/SKILL.md": "---\nname: databricks-test\n---\n", + }) + kv := []string{ + "HOME", home, + "USERPROFILE", home, + "XDG_CONFIG_HOME", filepath.Join(home, ".config"), + "APPDATA", filepath.Join(home, "AppData", "Roaming"), + "DATABRICKS_CACHE_DIR", cacheDir, + "DATABRICKS_SKILLS_REF", "test-ref", + "DATABRICKS_SKILLS_BASE_URL", url, + "DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION", "1.30.0", + } + // Windows sockets need SystemRoot. + if v, ok := os.LookupEnv("SystemRoot"); ok { + kv = append(kv, "SystemRoot", v) + } + isolateAgents(t, kv...) + + require.NoError(t, os.MkdirAll(filepath.Join(home, ".cursor"), 0o755)) + if state == "" { + state = `{"id": "treatment-machine"}` + } + stateDir := filepath.Join(home, ".databricks", "aitools") + require.NoError(t, os.MkdirAll(stateDir, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(stateDir, "auto-install.json"), []byte(state), 0o600)) + gate := `{"0.0.0": {"appkit": "0.84.0", "skills": "0.2.30", "auto_install": true}}` + require.NoError(t, os.WriteFile(filepath.Join(cacheDir, "compat-manifest.json"), []byte(gate), 0o600)) + return home +} diff --git a/libs/aitools/agents/hint.go b/libs/aitools/agents/hint.go index 9add2a607d4..c32dd2d5a23 100644 --- a/libs/aitools/agents/hint.go +++ b/libs/aitools/agents/hint.go @@ -216,7 +216,7 @@ func MaybeHint(ctx context.Context, cmd *cobra.Command, pluginRecorded PluginRec if IsCredentialHelperCommand(cmd) { return } - if env.Get(ctx, selfExecEnv) != "" { + if IsSelfExec(ctx) { return } if !cmdio.HasIO(ctx) { @@ -426,6 +426,12 @@ func sessionID(ctx context.Context, sessionEnv string) string { return env.Get(ctx, sessionEnv) } +// IsSelfExec reports whether this CLI process was started by the CLI itself +// (see SelfExecEnv). +func IsSelfExec(ctx context.Context) bool { + return env.Get(ctx, selfExecEnv) != "" +} + // SelfExecEnv returns environ plus the marker that keeps a CLI process the // CLI starts of itself from hinting. Use it as the child's exec.Cmd.Env. func SelfExecEnv(environ []string) []string { diff --git a/libs/aitools/autoinstall/run.go b/libs/aitools/autoinstall/run.go index 38856ce660a..78a7298085e 100644 --- a/libs/aitools/autoinstall/run.go +++ b/libs/aitools/autoinstall/run.go @@ -109,6 +109,26 @@ func MarkInstalling(ctx context.Context) error { return updateState(ctx, func(s *State) { s.Installing = true }) } +// SaveID stores id as the machine identifier unless one is stored already, +// without settling a version, and returns the stored identifier. Runs that +// draw an ID save it before using it, so concurrent runs agree on the +// machine's holdout bucket. +func SaveID(ctx context.Context, id string) (string, error) { + err := updateState(ctx, func(s *State) { + if s.ID == "" { + s.ID = id + } + id = s.ID + }) + return id, err +} + +// Accept records that the user answered "yes" at the install prompt of +// version (State.AcceptedVersion). +func Accept(ctx context.Context, version string) error { + return updateState(ctx, func(s *State) { s.AcceptedVersion = version }) +} + // TryLock claims the machine-wide auto-install lock so that concurrent CLI // processes (agents often run several at once) do not install at the same // time. ok is false when another process holds it or it cannot be taken; that diff --git a/libs/aitools/autoinstall/run_test.go b/libs/aitools/autoinstall/run_test.go index b73b51d6eb5..33e7b48332f 100644 --- a/libs/aitools/autoinstall/run_test.go +++ b/libs/aitools/autoinstall/run_test.go @@ -44,6 +44,21 @@ func TestRecordKeepsConcurrentOptOut(t *testing.T) { assert.Equal(t, "1.30.0", s.LastVersion) } +func TestSaveIDKeepsTheFirstID(t *testing.T) { + ctx := env.WithUserHomeDir(t.Context(), t.TempDir()) + + id, err := SaveID(ctx, "first") + require.NoError(t, err) + assert.Equal(t, "first", id) + id, err = SaveID(ctx, "second") + require.NoError(t, err) + assert.Equal(t, "first", id, "the stored ID is returned") + + s, err := Load(ctx) + require.NoError(t, err) + assert.Equal(t, State{ID: "first"}, s, "the version is not settled") +} + func TestLoadWaitsForStateLock(t *testing.T) { ctx := env.WithUserHomeDir(t.Context(), t.TempDir()) diff --git a/libs/aitools/autoinstall/state.go b/libs/aitools/autoinstall/state.go index c13982fa129..485f8520ac7 100644 --- a/libs/aitools/autoinstall/state.go +++ b/libs/aitools/autoinstall/state.go @@ -37,10 +37,15 @@ type State struct { // OptedOut turns auto-install off for every later version. It is set by // `databricks aitools auto-install disable`, by a global-scope - // `databricks aitools uninstall` and by a "no" to the skills prompt of - // `databricks apps init`. + // `databricks aitools uninstall`, by declining the install prompt and by a + // "no" to the skills prompt of `databricks apps init`. OptedOut bool `json:"opted_out,omitempty"` + // AcceptedVersion is the CLI version whose install prompt the user + // answered "yes". Retries of that version install without asking again; + // any other version asks. + AcceptedVersion string `json:"accepted_version,omitempty"` + // Installing is set while auto-install makes a first install, and stays // set when the install did not finish. The skills such an install left // have no install state, like the ones the user put there, so a later run diff --git a/libs/apps/vite/bridge.go b/libs/apps/vite/bridge.go index 7b658e83c7d..8964d69d228 100644 --- a/libs/apps/vite/bridge.go +++ b/libs/apps/vite/bridge.go @@ -981,6 +981,8 @@ func (vb *Bridge) Start() error { }) if !vb.autoApprove { + // Stop does not end the read, so it can outlive the command. + cmdio.MarkStdinReadInBackground() go vb.readStdinLines(os.Stdin) } diff --git a/libs/cmdio/ask.go b/libs/cmdio/ask.go index 82772c6d38d..c3765d790b7 100644 --- a/libs/cmdio/ask.go +++ b/libs/cmdio/ask.go @@ -69,7 +69,8 @@ func Ask(ctx context.Context, question, defaultVal string) (string, error) { // done and returns ctx.Err(). Reading the input cannot be interrupted, so the // read keeps running in the background and its result is discarded. The // caller must not read the input again after a cancelled call, because the -// abandoned read would consume the next line. +// abandoned read would consume the next line; a cancelled call turns off line +// prompts for the rest of the process (see MarkStdinReadInBackground). func AskCancellable(ctx context.Context, question, defaultVal string) (string, error) { type answer struct { ans string @@ -83,6 +84,9 @@ func AskCancellable(ctx context.Context, question, defaultVal string) (string, e select { case <-ctx.Done(): + // The abandoned read takes the next line, so later prompts in this + // process must not ask. + MarkStdinReadInBackground() return "", ctx.Err() case a := <-answers: return a.ans, a.err diff --git a/libs/cmdio/ask_test.go b/libs/cmdio/ask_test.go index 75fc64d7c54..a4292dde3a0 100644 --- a/libs/cmdio/ask_test.go +++ b/libs/cmdio/ask_test.go @@ -201,6 +201,8 @@ func TestAskCancellable_Cancelled(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) ctx = InContext(ctx, NewIO(ctx, flags.OutputText, stdin, io.Discard, io.Discard, "", "")) cancel() + t.Cleanup(func() { stdinReadInBackground.Store(false) }) _, err := AskCancellable(ctx, "Question", "") assert.ErrorIs(t, err, context.Canceled) + assert.True(t, stdinReadInBackground.Load(), "the abandoned read turns off later line prompts") } diff --git a/libs/cmdio/capabilities.go b/libs/cmdio/capabilities.go index f48c72a006a..2349081462a 100644 --- a/libs/cmdio/capabilities.go +++ b/libs/cmdio/capabilities.go @@ -57,6 +57,16 @@ func (c Capabilities) SupportsPrompt() bool { return c.SupportsInteractive() && c.stdinIsTTY && !c.stdinIsMSYSPipe } +// SupportsLinePrompt returns true if the terminal supports a plain-text line +// prompt (see [Ask]): it writes the question to stderr and reads the answer +// from stdin. stdout must be a terminal too: when it is piped to a pager, the +// pager reads the same terminal, so its keystrokes could answer the prompt. +// Unlike SupportsPrompt it does not depend on TERM=dumb or an MSYS2 pty pipe +// (see conpty.go), which only matter for the TUI prompts. +func (c Capabilities) SupportsLinePrompt() bool { + return c.stdinIsTTY && c.stdoutIsTTY && c.stderrIsTTY +} + // SupportsColor returns true if the given writer supports colored output. // This checks both TTY status and environment variables (NO_COLOR, TERM=dumb). func (c Capabilities) SupportsColor(w io.Writer) bool { diff --git a/libs/cmdio/capabilities_test.go b/libs/cmdio/capabilities_test.go index 63a8226d86e..ca70f46ddda 100644 --- a/libs/cmdio/capabilities_test.go +++ b/libs/cmdio/capabilities_test.go @@ -66,6 +66,22 @@ func TestCapabilities_SupportsPrompt(t *testing.T) { } } +func TestCapabilities_SupportsLinePrompt(t *testing.T) { + full := Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true} + assert.True(t, full.SupportsLinePrompt()) + + // A plain-text line needs no redraw, so TERM=dumb, which turns off the + // TUI prompts, still gets it. + dumb := full + dumb.dumb = true + assert.False(t, dumb.SupportsPrompt()) + assert.True(t, dumb.SupportsLinePrompt()) + + assert.False(t, Capabilities{stdinIsTTY: false, stdoutIsTTY: true, stderrIsTTY: true}.SupportsLinePrompt(), "stdin not TTY") + assert.False(t, Capabilities{stdinIsTTY: true, stdoutIsTTY: false, stderrIsTTY: true}.SupportsLinePrompt(), "stdout not TTY") + assert.False(t, Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: false}.SupportsLinePrompt(), "stderr not TTY") +} + func TestCapabilities_SupportsPager(t *testing.T) { tests := []struct { name string diff --git a/libs/cmdio/io.go b/libs/cmdio/io.go index 5a0385d664a..4c369b49ab8 100644 --- a/libs/cmdio/io.go +++ b/libs/cmdio/io.go @@ -4,8 +4,10 @@ import ( "context" "errors" "io" + "os" "strings" "sync" + "sync/atomic" tea "github.com/charmbracelet/bubbletea" "github.com/databricks/cli/libs/flags" @@ -83,6 +85,44 @@ func IsPromptSupported(ctx context.Context) bool { return c.capabilities.SupportsPrompt() } +// IsLinePromptSupported reports whether a plain-text line prompt ([Ask]) can +// be shown and answered: stdin, stdout and stderr are terminals, this process +// is in the terminal's foreground process group (a background job that reads +// it is stopped), and no goroutine reads stdin in the background (see +// [MarkStdinReadInBackground]). Unlike IsPromptSupported, it does not depend +// on TERM=dumb or an MSYS2 pty pipe, which only matter for the TUI prompts. +func IsLinePromptSupported(ctx context.Context) bool { + c := fromContext(ctx) + if !c.capabilities.SupportsLinePrompt() || stdinReadInBackground.Load() { + return false + } + f, ok := c.in.(*os.File) + return !ok || inForeground(f) +} + +// stdinReadInBackground is set once a goroutine reads stdin for the rest of +// the process. +var stdinReadInBackground atomic.Bool + +// MarkStdinReadInBackground records that a goroutine reads os.Stdin and may +// outlive the command, so later line prompts in this process are not asked: +// that goroutine could take their answer. +func MarkStdinReadInBackground() { + stdinReadInBackground.Store(true) +} + +// DiscardInput discards what was typed at the stdin terminal but not read +// yet, so that a prompt asked after a long-running command reads only what is +// typed after the question. +func DiscardInput(ctx context.Context) error { + c := fromContext(ctx) + f, ok := c.in.(*os.File) + if !ok || !c.capabilities.stdinIsTTY { + return nil + } + return discardInput(f) +} + // IsInputTTY reports whether stdin is a terminal, so that a user can type // input into the command. Unlike IsPromptSupported, it does not depend on // stderr or TERM=dumb, which only matter for rendering prompts, and it accepts diff --git a/libs/cmdio/io_test.go b/libs/cmdio/io_test.go index b926dec3cd7..6c69f1923d5 100644 --- a/libs/cmdio/io_test.go +++ b/libs/cmdio/io_test.go @@ -3,6 +3,7 @@ package cmdio import ( "bytes" "io" + "strings" "testing" "time" @@ -77,3 +78,16 @@ func TestHasTerminal(t *testing.T) { }) } } + +func TestIsLinePromptSupported(t *testing.T) { + ctx := t.Context() + terminal := InContext(ctx, NewIO(ctx, flags.OutputText, FakeTTYReader(strings.NewReader("")), FakeTTY(io.Discard), FakeTTY(io.Discard), "", "")) + assert.True(t, IsLinePromptSupported(terminal)) + + piped := InContext(ctx, NewIO(ctx, flags.OutputText, strings.NewReader(""), FakeTTY(io.Discard), FakeTTY(io.Discard), "", "")) + assert.False(t, IsLinePromptSupported(piped)) + + t.Cleanup(func() { stdinReadInBackground.Store(false) }) + MarkStdinReadInBackground() + assert.False(t, IsLinePromptSupported(terminal), "another goroutine reads stdin") +} diff --git a/libs/cmdio/terminal_darwin.go b/libs/cmdio/terminal_darwin.go new file mode 100644 index 00000000000..b0f2109b1d1 --- /dev/null +++ b/libs/cmdio/terminal_darwin.go @@ -0,0 +1,14 @@ +package cmdio + +import ( + "os" + + "golang.org/x/sys/unix" +) + +// discardInput discards what was typed at the terminal f but not read yet. +// It is tcflush with TCIFLUSH, which libc implements as TIOCFLUSH with FREAD. +func discardInput(f *os.File) error { + const fread = 1 + return unix.IoctlSetPointerInt(int(f.Fd()), unix.TIOCFLUSH, fread) +} diff --git a/libs/cmdio/terminal_foreground.go b/libs/cmdio/terminal_foreground.go new file mode 100644 index 00000000000..d1022bbe75e --- /dev/null +++ b/libs/cmdio/terminal_foreground.go @@ -0,0 +1,17 @@ +//go:build linux || darwin + +package cmdio + +import ( + "os" + + "golang.org/x/sys/unix" +) + +// inForeground reports whether this process is in the foreground process +// group of the terminal f. A background job that reads its terminal is +// stopped (SIGTTIN) until the user brings it to the foreground. +func inForeground(f *os.File) bool { + pgrp, err := unix.IoctlGetInt(int(f.Fd()), unix.TIOCGPGRP) + return err == nil && pgrp == unix.Getpgrp() +} diff --git a/libs/cmdio/terminal_linux.go b/libs/cmdio/terminal_linux.go new file mode 100644 index 00000000000..64367ee2d3f --- /dev/null +++ b/libs/cmdio/terminal_linux.go @@ -0,0 +1,13 @@ +package cmdio + +import ( + "os" + + "golang.org/x/sys/unix" +) + +// discardInput discards what was typed at the terminal f but not read yet +// (tcflush with TCIFLUSH). +func discardInput(f *os.File) error { + return unix.IoctlSetInt(int(f.Fd()), unix.TCFLSH, unix.TCIFLUSH) +} diff --git a/libs/cmdio/terminal_other.go b/libs/cmdio/terminal_other.go new file mode 100644 index 00000000000..1e42d7819ca --- /dev/null +++ b/libs/cmdio/terminal_other.go @@ -0,0 +1,17 @@ +//go:build !linux && !darwin && !windows + +package cmdio + +import "os" + +// inForeground reports whether this process may read the terminal f. The +// release builds' platforms check it; others assume it may. +func inForeground(*os.File) bool { + return true +} + +// discardInput discards what was typed at the terminal f but not read yet. +// Only the release builds' platforms implement it. +func discardInput(*os.File) error { + return nil +} diff --git a/libs/cmdio/terminal_windows.go b/libs/cmdio/terminal_windows.go new file mode 100644 index 00000000000..2462ff5e829 --- /dev/null +++ b/libs/cmdio/terminal_windows.go @@ -0,0 +1,18 @@ +package cmdio + +import ( + "os" + + "golang.org/x/sys/windows" +) + +// inForeground reports whether this process may read the console f. Windows +// consoles have no background jobs, so it always may. +func inForeground(*os.File) bool { + return true +} + +// discardInput discards what was typed at the console f but not read yet. +func discardInput(f *os.File) error { + return windows.FlushConsoleInputBuffer(windows.Handle(f.Fd())) +} diff --git a/libs/telemetry/protos/README.md b/libs/telemetry/protos/README.md index 75091a8146d..5d5140880fd 100644 --- a/libs/telemetry/protos/README.md +++ b/libs/telemetry/protos/README.md @@ -34,12 +34,15 @@ within the upload timeout, when auth has already resolved. On the first run of a new CLI version, the CLI can install Databricks AI tools for the coding agents on the machine (see `databricks aitools auto-install --help`). It logs an `aitools_auto_install_event` with the outcome (`INSTALLED`, `UP_TO_DATE`, `KEPT`, `SKIPPED_CI`, `SKIPPED_OPTOUT`, -`SKIPPED_HOLDOUT`, `SKIPPED_NO_AGENTS`, `SKIPPED_PLATFORM`, `FAILED`, `RETRIES_EXHAUSTED`), the -machine's holdout arm and, for a failure, its error category: once per version and machine, plus -once per retry of a failed attempt. Nothing is logged for a version whose outcome is settled, on -Databricks Runtime, or while the feature is turned off remotely (the CI and opt-out checks come -first and are logged either way). An install with no tools installed yet also logs an -`aitools_install_event` with `trigger` `AUTO_FIRST_RUN`; installs by `databricks aitools install` -leave `trigger` unset. The holdout arm comes from a random machine id kept in +`SKIPPED_HOLDOUT`, `SKIPPED_NO_AGENTS`, `SKIPPED_PLATFORM`, `DECLINED`, `UNANSWERED`, `FAILED`, +`RETRIES_EXHAUSTED`), the machine's holdout arm and, for a failure, its error category: once per +version and machine, plus once per retry of a failed attempt. Nothing is logged for a version whose +outcome is settled, on Databricks Runtime, or while the feature is turned off remotely (the CI and +opt-out checks come first and are logged either way). A run that has to ask before installing but +cannot (a stream is not a terminal, or the CLI runs in the background) logs nothing and leaves the +version to a later run that can ask; a process killed at the prompt logs nothing either. An install +with no tools installed yet also logs an `aitools_install_event` with `trigger` `AUTO_FIRST_RUN`; +installs by `databricks aitools install` leave `trigger` unset. The holdout arm comes from a random +machine id kept in `~/.databricks/aitools/auto-install.json`; the id itself is never sent. Like the other aitools events, these upload at exit only when the command resolved auth. diff --git a/libs/telemetry/protos/aitools_events.go b/libs/telemetry/protos/aitools_events.go index eb67e1cce93..926874606d5 100644 --- a/libs/telemetry/protos/aitools_events.go +++ b/libs/telemetry/protos/aitools_events.go @@ -185,14 +185,23 @@ const ( // The run was in CI (the CI env var, or a CI provider's own). AitoolsAutoInstallOutcomeSkippedCI AitoolsAutoInstallOutcome = "SKIPPED_CI" // The user turned auto-install off: DATABRICKS_AITOOLS_AUTO_INSTALL=false, - // `aitools auto-install disable`, a global `aitools uninstall`, or "no" - // to the skills prompt of `apps init`. + // `aitools auto-install disable`, a global `aitools uninstall`, "no" at + // the prompt of an earlier version, or "no" to the skills prompt of + // `apps init`. AitoolsAutoInstallOutcomeSkippedOptOut AitoolsAutoInstallOutcome = "SKIPPED_OPTOUT" // The machine is in the holdout cohort. AitoolsAutoInstallOutcomeSkippedHoldout AitoolsAutoInstallOutcome = "SKIPPED_HOLDOUT" // No supported coding agent has a config directory on the machine. AitoolsAutoInstallOutcomeSkippedNoAgents AitoolsAutoInstallOutcome = "SKIPPED_NO_AGENTS" - // The attempt failed or timed out; a later run of the same version retries. + // The user answered "no" at the install prompt, which also turns + // auto-install off. + AitoolsAutoInstallOutcomeDeclined AitoolsAutoInstallOutcome = "DECLINED" + // The install prompt got no answer: stdin was closed, Ctrl+C was pressed, + // it timed out, or the answer was neither yes nor no. Nothing is installed + // and auto-install stays on, so the next CLI version asks again. + AitoolsAutoInstallOutcomeUnanswered AitoolsAutoInstallOutcome = "UNANSWERED" + // The attempt failed or timed out; a later run of the same version + // retries. AitoolsAutoInstallOutcomeFailed AitoolsAutoInstallOutcome = "FAILED" // The attempt failed and was the last one this CLI version gets. AitoolsAutoInstallOutcomeRetriesExhausted AitoolsAutoInstallOutcome = "RETRIES_EXHAUSTED" @@ -215,14 +224,18 @@ const ( // machine, plus once per retry after a failed attempt. A version whose outcome // is settled emits nothing, and so does a run that the remote gate turns off; // the CI and opt-out checks come before the gate, so they are emitted either -// way. An install with no tools installed yet also emits an -// AitoolsInstallEvent with Trigger AUTO_FIRST_RUN. +// way. A run that has to ask before installing but cannot emits nothing and +// leaves the version to a later run. An install with no tools installed yet +// also emits an AitoolsInstallEvent with Trigger AUTO_FIRST_RUN. type AitoolsAutoInstallEvent struct { Outcome AitoolsAutoInstallOutcome `json:"outcome"` HoldoutArm AitoolsHoldoutArm `json:"holdout_arm"` - // Whether the run could prompt (cmdio.IsPromptSupported and no coding - // agent detected). A measurable false, so no omitempty. + // Whether a person may be at the terminal (cmdio.HasTerminal and no + // coding agent detected). Such a run asks before the first install of a + // version, so DECLINED and UNANSWERED come only with true; INSTALLED with + // true may still have run without asking (an update, or a retry after an + // accepted attempt). A measurable false, so no omitempty. Interactive bool `json:"interactive"` // Set for FAILED and RETRIES_EXHAUSTED.