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
7 changes: 4 additions & 3 deletions cmd/aitools/autoinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
173 changes: 167 additions & 6 deletions cmd/aitools/autoinstall_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"log/slog"
"maps"
"os"
"os/signal"
"runtime"
"slices"
"strings"
Expand All @@ -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"
)

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

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] ExecutionTimeMs in cmd/root/root.go is measured around ExecuteContextC, which includes the post-run hook. So the first command of each version can now report up to this 1-minute wait as command time. Before this PR it already included up to about 13s of install. Consider excluding the hook's time, or noting it where command latency is analyzed.

)

// autoInstallDisable is the opt-out command named in the notice.
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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 {

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] agents.IsSelfExec is checked only in canAsk, which runs only when r.interactive is true. A self-exec child with no terminal therefore installs without asking. Scenario: databricks air images push runs auth docker configure as a self-exec child (configureImageDocker in cmd/air/image.go), with stdout and stderr in a buffer and stdin unset. That child has no terminal, so it installs and settles the version before the parent's hook runs. The user at the terminal is never asked, and the parent then prints the child's "Installed Databricks AI tools…" line. This contradicts the earlier answer that a self-exec child doesn't ask and doesn't install. Skip auto-install in a self-exec child before anything else, without settling the version, for example next to the state-free skips in autoInstall. (in reply to #7703 (comment))

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] tools.newAgents() is built here and again on line 213. The condition interactive && AcceptedVersion != version && len(newAgents()) > 0 is also written out again at line 118, and the two copies must stay in sync by hand. Compute the names once, and consider one helper for the "would ask" predicate used by both places.

// autoInstall skips this case before the lock; the state it decided
// on may have changed since.
if !canAsk(ctx) {

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] No test reaches this second canAsk check, because the early skip catches every current case. The timeout and Ctrl+C paths of ask and the DiscardInput failure path are also untested, and promptTimeout is a const, so a test can't shorten it. The new libs/cmdio/terminal_*.go files have no tests either (.agents/rules/testing.md), and every test uses FakeTTYReader, so discardInput and inForeground never run. An os.Pipe test would check that inForeground is false and discardInput fails on a pipe. Consider making the timeout a package variable and adding cases for a cancelled question and a failed discard.

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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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() == ""
}
Comment on lines +274 to +276

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] autoInstallAsks does not skip CLI processes that the CLI starts itself, unlike MaybeHint, which checks DATABRICKS_CLI_SELF_EXEC. Scenario: on the first run of a version, databricks apps init --deploy runs in flags mode (or in a NO_COLOR or Git Bash terminal, where RecommendSkillsInstall only prints the tip). It runs the child apps deploy on the same terminal. When the deploy succeeds, the child asks "Install Databricks AI tools for …? [Y/n]" and waits up to a minute, before the parent prints "Deploy complete" and moves on to the run step. Skip auto-install, or at least the question, in a self-exec child, and leave it to the parent's own run. (On this PR's base, autoinstall.ChildEnv() also overwrites SelfExecEnv at those apps init call sites; the current base tip fixes that.)

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.

Fixed in 957cbf0, and it covers the question as well as the install: a self-exec child doesn't ask and doesn't install. canAsk now requires !agents.IsSelfExec(ctx), a new exported check beside SelfExecEnv, which MaybeHint now uses too. The child takes the cannot-ask skip: ID saved, version not settled, nothing installed. The parent then asks after its own command. New case: "a CLI process started by the CLI does not ask". The base's ChildEnv/SelfExecEnv fix comes in with the merge.


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

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 install command is written out in the messages at lines 253, 302 and 364, while the sibling command already has autoInstallDisable at the top of the file. .agents/rules/style-guide-go.md asks for a package-level constant at the top of the file and says to follow an existing related constant. Add e.g. autoInstallManual = "databricks aitools install" next to autoInstallDisable, and move skippedNotice up there too.


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

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] In Git Bash's mintty, stdin is a Cygwin pipe that isTTY counts as a terminal. So canAsk is true and the early skip at line 118 does not fire. On every command, a treatment machine takes the lock, resolves the gate (a manifest fetch of up to 3s once the hourly cache is stale), plans, and only then fails here in DiscardInput and takes skipUnaskable. The version is never settled and nothing is reported. A holdout machine on the same setup settles as SKIPPED_HOLDOUT after the gate lookup, so the arms are not counted alike, against the intent of the early skip. Make IsLinePromptSupported false on Windows when stdin is not a real console (for example, isatty.IsTerminal is false but IsCygwinTerminal is true, or GetConsoleMode fails), so the pre-lock skip catches it. Keep this error path as a fallback.

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] Pending input is discarded before Record, which can wait up to 1s for the state lock and then rewrites the file, and only then is the question printed. An Enter pressed in that window (e.g. tapping Enter while a deploy finishes) is read as the default "yes" before the question appears. Keep the discard here as the can-ask check, and discard again right before AskCancellable.

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 {

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] This Record with 0 attempts settles the version before the question, and it also clears Installing. Scenario: a first install that ran without asking (no terminal, e.g. an agent) times out after writing some skills, leaving Installing=true and no install state. A later run in a terminal plans a fresh install, because planTools skips the untracked-skills check while Installing is set, and asks. The user presses Ctrl+C, waits out the minute, or types something else. With Installing cleared, every later version finds untracked skills and settles as KEPT. The machine is never asked again and the partial install is never finished, contrary to UNANSWERED's "the next CLI version asks again". Keep Installing when settling before the question and for UNANSWERED/DECLINED, e.g. with a settle variant that leaves Installing alone.

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]", "")

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] The prompt doesn't handle job control or Ctrl+C, so two common terminal actions now end badly after a command that succeeded. (1) databricks jobs list & from an interactive shell has the terminal on all three streams, so it prompts. A background process group that reads the terminal gets SIGTTIN and stops ("Stopped (tty input)"), and promptTimeout can't fire while it is stopped. (2) The CLI installs no SIGINT handler, so pressing Ctrl+C to dismiss the question kills the process. The shell sees exit status 130 for a command that succeeded, so a make target or && chain stops. The command's own telemetry upload in root.Execute also never runs. Ask only when the process is in the terminal's foreground process group (tcgetpgrp on stdin equals getpgrp). Consider treating SIGINT during the prompt as no answer, e.g. with signal.NotifyContext around AskCancellable, so the command keeps its exit status.

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.

Fixed in cdf01c0. (1) cmdio.IsLinePromptSupported now also requires the process to be in the terminal's foreground process group (TIOCGPGRP == getpgrp; always true on Windows). A background run takes the skip path, which saves the ID and doesn't settle the version. In tmux, databricks auth profiles & now ends as Done, with no prompt and no Stopped (tty input). (2) The prompt runs under signal.NotifyContext(..., os.Interrupt), so Ctrl+C counts as no answer (UNANSWERED, not opted out). In tmux, Ctrl+C at the prompt prints ^C and a newline, and the command exits 0.

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] When the question times out or gets Ctrl+C, AskCancellable leaves its read blocked on the terminal until the process exits, and root.Execute then uploads telemetry for up to 3s. A command the user types right after Ctrl+C is read by the abandoned goroutine and lost. Consider a read that stops on ctx.Done() (muesli/cancelreader is already in the module graph through bubbletea). At least have AskCancellable call MarkStdinReadInBackground when it returns on cancellation, so later prompts in the process know about the leftover read.

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.

Fixed in 957cbf0, with the minimum: when AskCancellable returns on cancellation, it calls MarkStdinReadInBackground, so no later line prompt in the process can lose its answer to the abandoned read. TestAskCancellable_Cancelled checks this. I didn't switch to cancelreader. It would mean changing cmdio's unbuffered stdin handling, which other prompts share, and that's more than this PR should take on. The line typed right after Ctrl+C is still lost until the process exits; I can follow up if you think it's worth it.

if err != nil {
log.Debugf(ctx, "auto-install: skipped: the prompt was not answered: %v", err)
// End the question's line.
cmdio.LogString(ctx, "")

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] On a timeout, Ctrl+C or EOF, the user sees the question followed by an empty line, with no hint that it was skipped or how to install later. The "neither yes nor no" path prints skippedNotice, and this version won't ask again. Print skippedNotice here too.

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,
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading