From 7f3568acf27a34e806bab0f26027ed33c02cbffd Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:55:40 +0000 Subject: [PATCH 1/2] aitools: look the auto-install gate up once per version, and not without agents A machine that cannot reach the compatibility manifest waited for the gate lookup after each of the first 10 commands of a CLI version, and a machine whose home directory keeps no state waited on every run. - A failed lookup is not repeated for the version. - A machine with no coding agent does not look the gate up. - The lookup is bounded by 1 s instead of 3 s. - Ctrl+C or SIGTERM during the lookup ends the lookup, not the command. Co-authored-by: Isaac --- .../aitools/auto-install/skipped/output.txt | 1 - cmd/aitools/autoinstall_run.go | 47 ++++++++++++----- cmd/aitools/autoinstall_run_test.go | 52 ++++++++++++++++++- 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/acceptance/cmd/aitools/auto-install/skipped/output.txt b/acceptance/cmd/aitools/auto-install/skipped/output.txt index 55321b2a457..75c41d79442 100644 --- a/acceptance/cmd/aitools/auto-install/skipped/output.txt +++ b/acceptance/cmd/aitools/auto-install/skipped/output.txt @@ -93,4 +93,3 @@ Automatic installation of Databricks AI tools is enabled. >>> [CLI] jobs list >>> print_telemetry -{"command":"jobs_list","exit_code":0,"aitools_auto_install_event":{"outcome":"SKIPPED_NO_AGENTS","holdout_arm":"TREATMENT","interactive":false}} diff --git a/cmd/aitools/autoinstall_run.go b/cmd/aitools/autoinstall_run.go index 288e295e2a8..bc2ac6f308c 100644 --- a/cmd/aitools/autoinstall_run.go +++ b/cmd/aitools/autoinstall_run.go @@ -12,6 +12,7 @@ import ( "runtime" "slices" "strings" + "syscall" "time" "github.com/databricks/cli/libs/aitools/agents" @@ -30,9 +31,11 @@ import ( const ( // gateTimeout bounds the gate lookup, one compatibility manifest fetch. - // Every machine pays it once per version while the gate is off, so it is - // kept short. A lookup that fails counts as a failed attempt. - gateTimeout = 3 * time.Second + // Every machine pays it once per version while the gate is off, and a + // machine that cannot reach the manifest waits this long after its + // command finished, so it is kept short. A version gets one lookup: one + // that fails is not repeated (see autoInstall). + gateTimeout = time.Second // installTimeout bounds the install or update (its downloads and the // agent CLIs it runs). One that runs out of time counts as a failed @@ -43,6 +46,9 @@ const ( promptTimeout = time.Minute ) +// gateOn is autoinstall.GateOn; tests replace it. +var gateOn = autoinstall.GateOn + // autoInstallDisable is the opt-out command named in the notice. const autoInstallDisable = "databricks aitools auto-install disable" @@ -115,8 +121,19 @@ func autoInstall(ctx context.Context, skippedCommand bool) { // 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 { + // + // A machine with no coding agent skips there as well: the gate changes + // nothing for it, and such machines (containers, job pods) often have no + // route to the manifest and a home directory that keeps no state, so they + // would wait for the lookup on every run. The version is not settled, so + // an agent set up later is still found. + if reason == autoinstall.ReasonGateOff { + tools, err := planTools(quietContext(ctx), in.State) + if err == nil && tools.skip == protos.AitoolsAutoInstallOutcomeSkippedNoAgents { + 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 { skipUnaskable(ctx, in.State.ID) return } @@ -151,16 +168,20 @@ func autoInstall(ctx context.Context, skippedCommand bool) { reason = autoinstall.Decide(in) if reason == autoinstall.ReasonGateOff { gateCtx, cancel := context.WithTimeout(ctx, gateTimeout) - in.GateOn, err = autoinstall.GateOn(gateCtx, version) + // Ctrl+C or a request to terminate ends the lookup instead of killing + // a command that succeeded. + gateCtx, stop := signal.NotifyContext(gateCtx, os.Interrupt, syscall.SIGTERM) + in.GateOn, err = gateOn(gateCtx, version) + stop() cancel() if err != nil { - // The gate is unknown, so the version is not settled: a later run - // looks it up again. Like a failed install, the lookup uses up an - // attempt, so a machine that is offline stops paying for it. The - // feature may be off, so nothing is reported. - attempt := autoinstall.NextAttempt(in.State, version) - log.Debugf(ctx, "auto-install: attempt %d of %d: could not resolve the gate: %v", attempt, autoinstall.MaxAttempts, err) - if err := autoinstall.Record(ctx, version, attempt, id); err != nil { + // The gate is unknown. A machine that cannot reach the manifest + // (no route to GitHub) would wait for the lookup after every + // command, so the version gets no other attempt; the next version + // looks the gate up again. The feature may be off, so nothing is + // reported. + log.Debugf(ctx, "auto-install: could not resolve the gate, not trying again for this version: %v", err) + if err := autoinstall.Record(ctx, version, autoinstall.MaxAttempts, id); err != nil { log.Debugf(ctx, "auto-install: could not save the state: %v", err) } return diff --git a/cmd/aitools/autoinstall_run_test.go b/cmd/aitools/autoinstall_run_test.go index 4f484d39833..a5b3c61f71f 100644 --- a/cmd/aitools/autoinstall_run_test.go +++ b/cmd/aitools/autoinstall_run_test.go @@ -3,6 +3,7 @@ package aitools import ( "bytes" "context" + "errors" "io" "os" "path/filepath" @@ -170,7 +171,7 @@ func TestAutoInstallPrompt(t *testing.T) { wantOutcome: protos.AitoolsAutoInstallOutcomeInstalled, }, { - // A failed gate lookup uses up an attempt without asking. + // A failed plan 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, @@ -283,6 +284,55 @@ func TestAutoInstallKeepsTheIDWhenItCannotAsk(t *testing.T) { assert.NotContains(t, stderr.String(), "Install Databricks AI tools") } +// A machine that cannot reach the compatibility manifest waits for the lookup +// after one command of a version, not after each of them. +func TestAutoInstallLooksTheGateUpOncePerVersion(t *testing.T) { + setupAutoInstallHome(t, "") + lookups := 0 + t.Cleanup(func() { gateOn = autoinstall.GateOn }) + gateOn = func(context.Context, string) (bool, error) { + lookups++ + return false, errors.New("the compatibility manifest could not be fetched") + } + run := func() autoinstall.State { + ctx := cmdio.MockDiscard(t.Context()) + ctx = telemetry.WithNewLogger(ctx) + runAutoInstallHook(ctx) + assert.Empty(t, telemetry.BufferedEvents(ctx)) + state, err := autoinstall.Load(ctx) + require.NoError(t, err) + return state + } + + want := autoinstall.State{LastVersion: "1.30.0", Attempts: autoinstall.MaxAttempts, ID: "treatment-machine"} + assert.Equal(t, want, run()) + assert.Equal(t, want, run()) + assert.Equal(t, 1, lookups) + + t.Setenv("DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION", "1.31.0") + run() + assert.Equal(t, 2, lookups, "a new version looks the gate up again") +} + +// A machine with no coding agent has nothing to install, so it never looks the +// gate up. +func TestAutoInstallSkipsTheGateLookupWithoutAgents(t *testing.T) { + home := setupAutoInstallHome(t, "") + require.NoError(t, os.RemoveAll(filepath.Join(home, ".cursor"))) + t.Cleanup(func() { gateOn = autoinstall.GateOn }) + gateOn = func(context.Context, string) (bool, error) { + t.Error("the gate was looked up") + return false, nil + } + ctx := cmdio.MockDiscard(t.Context()) + ctx = telemetry.WithNewLogger(ctx) + runAutoInstallHook(ctx) + assert.Empty(t, telemetry.BufferedEvents(ctx)) + 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"} From 9a8c9d819e970d8c9462ad6b57a3454707e390dc Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:31:29 +0000 Subject: [PATCH 2/2] aitools: stop auto-install when the gate lookup is interrupted An interrupted lookup could still resolve the gate from a stale cached manifest and go on to prompt or install. Co-authored-by: Isaac --- cmd/aitools/autoinstall_run.go | 18 +++++++++++++--- cmd/aitools/autoinstall_run_test.go | 32 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/cmd/aitools/autoinstall_run.go b/cmd/aitools/autoinstall_run.go index bc2ac6f308c..c071ab29b96 100644 --- a/cmd/aitools/autoinstall_run.go +++ b/cmd/aitools/autoinstall_run.go @@ -49,6 +49,12 @@ const ( // gateOn is autoinstall.GateOn; tests replace it. var gateOn = autoinstall.GateOn +// notifyInterrupt returns a context that is canceled when the CLI receives +// Ctrl+C or a request to terminate; tests replace it. +var notifyInterrupt = func(ctx context.Context) (context.Context, context.CancelFunc) { + return signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) +} + // autoInstallDisable is the opt-out command named in the notice. const autoInstallDisable = "databricks aitools auto-install disable" @@ -167,13 +173,19 @@ func autoInstall(ctx context.Context, skippedCommand bool) { // what decides the run. reason = autoinstall.Decide(in) if reason == autoinstall.ReasonGateOff { - gateCtx, cancel := context.WithTimeout(ctx, gateTimeout) // Ctrl+C or a request to terminate ends the lookup instead of killing // a command that succeeded. - gateCtx, stop := signal.NotifyContext(gateCtx, os.Interrupt, syscall.SIGTERM) + sigCtx, stop := notifyInterrupt(ctx) + gateCtx, cancel := context.WithTimeout(sigCtx, gateTimeout) in.GateOn, err = gateOn(gateCtx, version) - stop() + if err == nil && sigCtx.Err() != nil { + // An interrupted lookup can still answer, from a manifest cached + // earlier. The user asked the CLI to stop, so the run ends here + // like one whose lookup failed. + err = errors.New("interrupted") + } cancel() + stop() if err != nil { // The gate is unknown. A machine that cannot reach the manifest // (no route to GitHub) would wait for the lookup after every diff --git a/cmd/aitools/autoinstall_run_test.go b/cmd/aitools/autoinstall_run_test.go index a5b3c61f71f..f0393e4c23f 100644 --- a/cmd/aitools/autoinstall_run_test.go +++ b/cmd/aitools/autoinstall_run_test.go @@ -6,8 +6,10 @@ import ( "errors" "io" "os" + "os/signal" "path/filepath" "strings" + "syscall" "testing" "github.com/databricks/cli/libs/aitools/agents" @@ -314,6 +316,36 @@ func TestAutoInstallLooksTheGateUpOncePerVersion(t *testing.T) { assert.Equal(t, 2, lookups, "a new version looks the gate up again") } +// An interrupted lookup can still answer from a stale cached manifest that has +// the gate on. The run stops instead of installing. +func TestAutoInstallStopsWhenTheGateLookupIsInterrupted(t *testing.T) { + home := setupAutoInstallHome(t, "") + var interrupt context.CancelFunc + t.Cleanup(func() { + gateOn = autoinstall.GateOn + notifyInterrupt = func(ctx context.Context) (context.Context, context.CancelFunc) { + return signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) + } + }) + notifyInterrupt = func(ctx context.Context) (context.Context, context.CancelFunc) { + ctx, interrupt = context.WithCancel(ctx) + return ctx, interrupt + } + gateOn = func(context.Context, string) (bool, error) { + interrupt() + 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{LastVersion: "1.30.0", Attempts: autoinstall.MaxAttempts, ID: "treatment-machine"}, state) +} + // A machine with no coding agent has nothing to install, so it never looks the // gate up. func TestAutoInstallSkipsTheGateLookupWithoutAgents(t *testing.T) {