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
1 change: 0 additions & 1 deletion acceptance/cmd/aitools/auto-install/skipped/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
61 changes: 47 additions & 14 deletions cmd/aitools/autoinstall_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"runtime"
"slices"
"strings"
"syscall"
"time"

"github.com/databricks/cli/libs/aitools/agents"
Expand All @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

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.

With a stale cached manifest containing auto_install: true, Ctrl+C/SIGTERM cancels gateCtx, but clicompat.fetchManifest falls back to the stale cache and returns success. gateOn then returns (true, nil), so this proceeds to a prompt or installation using the uncanceled parent ctx after the user asked it to stop. Please check cancellation before stop()/cancel() and exit auto-install when interrupted, and add a regression test for the stale-cache case.

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 9a8c9d8. The signal context is now separate from the timeout, and a lookup that answers after an interrupt is treated like a failed one: the run ends before any prompt or install, and the version gets no other lookup. TestAutoInstallStopsWhenTheGateLookupIsInterrupted covers the case where the lookup returns the gate on after the interrupt, and fails without the fix.

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
Expand Down
84 changes: 83 additions & 1 deletion cmd/aitools/autoinstall_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"}
Expand Down
Loading