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..c071ab29b96 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,15 @@ const ( promptTimeout = time.Minute ) +// 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" @@ -115,8 +127,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 } @@ -150,17 +173,27 @@ 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) - in.GateOn, err = autoinstall.GateOn(gateCtx, version) + // Ctrl+C or a request to terminate ends the lookup instead of killing + // a command that succeeded. + sigCtx, stop := notifyInterrupt(ctx) + gateCtx, cancel := context.WithTimeout(sigCtx, gateTimeout) + in.GateOn, err = gateOn(gateCtx, version) + 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, 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..f0393e4c23f 100644 --- a/cmd/aitools/autoinstall_run_test.go +++ b/cmd/aitools/autoinstall_run_test.go @@ -3,10 +3,13 @@ package aitools import ( "bytes" "context" + "errors" "io" "os" + "os/signal" "path/filepath" "strings" + "syscall" "testing" "github.com/databricks/cli/libs/aitools/agents" @@ -170,7 +173,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 +286,85 @@ 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") +} + +// 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) { + 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"}