Skip to content

Commit 8f96d89

Browse files
rclareyIsaac
andauthored
[SSH] Enable agent shims on dedicated clusters (#7190)
## Changes Install agent shims on dedicated clusters too ## Why To allow customers to use agents on dedicated clusters ## Tests updated/added tests --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 236d5fd commit 8f96d89

8 files changed

Lines changed: 98 additions & 27 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `databricks ssh connect` now makes `claude` and `codex` available on dedicated clusters. ([#7190](https://github.com/databricks/cli/pull/7190))

‎acceptance/ssh/agent-launchers/output.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ exec [CLI] ssh agent-shim claude "$@"
3333
/usr/bin:/bin:[TEST_TMP_DIR]/server-home/.agent-shim/bin
3434
[TEST_TMP_DIR]/server-home/.agent-shim/bin/claude
3535
[TEST_TMP_DIR]/server-home/.agent-shim/bin/codex
36+
37+
=== Dedicated server
38+
39+
=== SetEnv appends the launcher directory to PATH
40+
PATH="/usr/bin:/bin:[TEST_TMP_DIR]/server-home/.agent-shim/bin"
41+
42+
>>> ls server-home/.agent-shim/bin
43+
claude
44+
codex
45+
46+
=== A non-interactive session finds the launchers on PATH
47+
[TEST_TMP_DIR]/server-home/.agent-shim/bin/claude
48+
[TEST_TMP_DIR]/server-home/.agent-shim/bin/codex

‎acceptance/ssh/agent-launchers/script‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,22 @@ ssh -F /dev/null -i client_key -l "$(id -un)" \
4040
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
4141
-o ProxyCommand="$SSHD -i -f $SERVER_HOME/.ssh-tunnel/sshd_config -o StrictModes=no" \
4242
localhost 'echo "$PATH"; command -v claude codex'
43+
44+
# Dedicated clusters get the same launchers and PATH entry as serverless.
45+
rm -rf "$SERVER_HOME"
46+
mkdir -p "$SERVER_HOME"
47+
title "Dedicated server\n"
48+
env -i HOME="$SERVER_HOME" PATH=/usr/bin:/bin DATABRICKS_HOST="$DATABRICKS_HOST" DATABRICKS_TOKEN="$DATABRICKS_TOKEN" \
49+
$CLI ssh server --cluster=test-cluster --session-id=test-session --secret-scope-name=ssh-scope \
50+
--authorized-key-secret-name=client-public-key --shutdown-delay=1s 2>LOG.dedicated-server
51+
52+
title "SetEnv appends the launcher directory to PATH\n"
53+
grep '^SetEnv ' server-home/.ssh-tunnel/sshd_config | tr ' ' '\n' | grep '^PATH='
54+
55+
trace ls server-home/.agent-shim/bin
56+
57+
title "A non-interactive session finds the launchers on PATH\n"
58+
ssh -F /dev/null -i client_key -l "$(id -un)" \
59+
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
60+
-o ProxyCommand="$SSHD -i -f $SERVER_HOME/.ssh-tunnel/sshd_config -o StrictModes=no" \
61+
localhost 'command -v claude codex'

‎experimental/ssh/internal/client/agentshim.go‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,8 @@ func prepareAgentLaunch(ctx context.Context, home string, agent agentlauncher.Ag
695695

696696
// put the Databricks context where the agent reads it, returning any extra argv.
697697
func injectAgentContext(ctx context.Context, home string, agent agentlauncher.AgentSpec) ([]string, error) {
698-
systemContext := agentSystemContext(env.Get(ctx, workspaceHomeEnv))
698+
serverless := dbr.RuntimeVersion(ctx).Type == dbr.ClusterTypeServerless
699+
systemContext := agentSystemContext(env.Get(ctx, workspaceHomeEnv), serverless)
699700
switch {
700701
case agent.ContextFlag != "":
701702
// Keep the scratch context file under the shim's own directory so it never
@@ -823,14 +824,20 @@ func removePath(ctx context.Context, dir string) {
823824
_ = os.Setenv("PATH", strings.Join(dirs, string(os.PathListSeparator)))
824825
}
825826

826-
func agentSystemContext(wsHome string) string {
827+
func agentSystemContext(wsHome string, serverless bool) string {
827828
userHomePart := ""
828829
if wsHome != "" {
829830
userHomePart = " The user's Databricks workspace home directory is " + wsHome + "."
830831
}
832+
computePart := "dedicated cluster"
833+
persistencePart := "Local files persist across sessions until the cluster restarts or terminates; only paths under /Workspace and /Volumes persist beyond that."
834+
if serverless {
835+
computePart = "serverless cluster"
836+
persistencePart = "This container is ephemeral; only paths under /Workspace and /Volumes persist across sessions."
837+
}
831838
return fmt.Sprintf(`You are running inside a "databricks ssh connect" session on the driver node of a
832-
Databricks serverless cluster.
839+
Databricks %s.
833840
- The "databricks" CLI is installed and already authenticated: DATABRICKS_HOST and DATABRICKS_TOKEN are set in the environment, so "databricks ..." commands work with no "databricks auth login". The same token governs Unity Catalog and serving-endpoint access.
834-
- This container is ephemeral; only paths under /Workspace and /Volumes persist across sessions.%s
835-
- You can run shell commands and use the "databricks" CLI to explore the workspace.`, userHomePart)
841+
- %s%s
842+
- You can run shell commands and use the "databricks" CLI to explore the workspace.`, computePart, persistencePart, userHomePart)
836843
}

‎experimental/ssh/internal/client/agentshim_test.go‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ import (
2323
"github.com/databricks/cli/experimental/ssh/internal/agentlauncher"
2424

2525
"github.com/databricks/cli/libs/cmdio"
26+
"github.com/databricks/cli/libs/dbr"
2627
"github.com/databricks/cli/libs/telemetry/protos"
2728
"github.com/databricks/databricks-sdk-go"
2829
"github.com/databricks/databricks-sdk-go/config"
2930
"github.com/stretchr/testify/assert"
3031
"github.com/stretchr/testify/require"
3132
)
3233

34+
var (
35+
serverlessRuntime = dbr.Environment{IsDbr: true, Version: "client.4.9"}
36+
dedicatedRuntime = dbr.Environment{IsDbr: true, Version: "16.4"}
37+
)
38+
3339
func TestPrependPath(t *testing.T) {
3440
t.Setenv("PATH", strings.Join([]string{"/a", "/b"}, string(os.PathListSeparator)))
3541
prependPath(t.Context(), "/b") // existing entry moves to the front (deduped)
@@ -358,7 +364,7 @@ func TestInjectAgentContext(t *testing.T) {
358364

359365
t.Run("flag agent writes a scratch file and returns the flag", func(t *testing.T) {
360366
home := t.TempDir()
361-
args, err := injectAgentContext(t.Context(), home, claude)
367+
args, err := injectAgentContext(dbr.MockRuntime(t.Context(), serverlessRuntime), home, claude)
362368
require.NoError(t, err)
363369
require.Len(t, args, 2)
364370
assert.Equal(t, "--append-system-prompt-file", args[0])
@@ -369,7 +375,7 @@ func TestInjectAgentContext(t *testing.T) {
369375

370376
t.Run("home-file agent writes the global instructions file, no args", func(t *testing.T) {
371377
home := t.TempDir()
372-
args, err := injectAgentContext(t.Context(), home, codex)
378+
args, err := injectAgentContext(dbr.MockRuntime(t.Context(), serverlessRuntime), home, codex)
373379
require.NoError(t, err)
374380
assert.Nil(t, args)
375381
data, err := os.ReadFile(filepath.Join(home, ".codex", "AGENTS.md"))
@@ -382,17 +388,35 @@ func TestInjectAgentContext(t *testing.T) {
382388
target := filepath.Join(home, ".codex", "AGENTS.md")
383389
require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755))
384390
require.NoError(t, os.WriteFile(target, []byte("user's own instructions"), 0o644))
385-
args, err := injectAgentContext(t.Context(), home, codex)
391+
args, err := injectAgentContext(dbr.MockRuntime(t.Context(), serverlessRuntime), home, codex)
386392
require.NoError(t, err)
387393
assert.Nil(t, args)
388394
data, err := os.ReadFile(target)
389395
require.NoError(t, err)
390396
assert.Equal(t, "user's own instructions", string(data))
391397
})
392398

399+
t.Run("context describes the compute the shim runs on", func(t *testing.T) {
400+
for _, c := range []struct {
401+
runtime dbr.Environment
402+
want string
403+
}{
404+
{serverlessRuntime, "Databricks serverless cluster."},
405+
{dedicatedRuntime, "Databricks dedicated cluster."},
406+
} {
407+
home := t.TempDir()
408+
args, err := injectAgentContext(dbr.MockRuntime(t.Context(), c.runtime), home, claude)
409+
require.NoError(t, err)
410+
require.Len(t, args, 2)
411+
data, err := os.ReadFile(args[1])
412+
require.NoError(t, err)
413+
assert.Contains(t, string(data), c.want)
414+
}
415+
})
416+
393417
t.Run("agent without a mechanism writes nothing", func(t *testing.T) {
394418
home := t.TempDir()
395-
args, err := injectAgentContext(t.Context(), home, noContext)
419+
args, err := injectAgentContext(dbr.MockRuntime(t.Context(), serverlessRuntime), home, noContext)
396420
require.NoError(t, err)
397421
assert.Nil(t, args)
398422
entries, err := os.ReadDir(home)
@@ -403,8 +427,20 @@ func TestInjectAgentContext(t *testing.T) {
403427

404428
func TestAgentSystemContext(t *testing.T) {
405429
wsHome := "/Workspace/Users/me@example.com"
406-
assert.Contains(t, agentSystemContext(wsHome), "user's Databricks workspace home directory is "+wsHome)
407-
assert.NotContains(t, agentSystemContext(""), "user's Databricks workspace home directory")
430+
for _, serverless := range []bool{true, false} {
431+
assert.Contains(t, agentSystemContext(wsHome, serverless), "user's Databricks workspace home directory is "+wsHome)
432+
assert.NotContains(t, agentSystemContext("", serverless), "user's Databricks workspace home directory")
433+
}
434+
435+
serverless := agentSystemContext(wsHome, true)
436+
assert.Contains(t, serverless, "Databricks serverless cluster.")
437+
assert.Contains(t, serverless, "This container is ephemeral")
438+
439+
dedicated := agentSystemContext(wsHome, false)
440+
assert.Contains(t, dedicated, "Databricks dedicated cluster.")
441+
assert.Contains(t, dedicated, "until the cluster restarts or terminates")
442+
assert.NotContains(t, dedicated, "serverless")
443+
assert.NotContains(t, dedicated, "ephemeral")
408444
}
409445

410446
func newProbeClient(t *testing.T, host string) *databricks.WorkspaceClient {
@@ -762,7 +798,7 @@ func TestPrepareAgentLaunch(t *testing.T) {
762798

763799
t.Run("passes the context args before the user's agent args", func(t *testing.T) {
764800
home := t.TempDir()
765-
argv, environ, err := prepareAgentLaunch(t.Context(), home, agent, []string{"--resume"})
801+
argv, environ, err := prepareAgentLaunch(dbr.MockRuntime(t.Context(), serverlessRuntime), home, agent, []string{"--resume"})
766802
require.NoError(t, err)
767803
require.NotEmpty(t, argv)
768804
assert.Equal(t, []string{"claude", "--", "--append-system-prompt-file", filepath.Join(home, agentRootDir, contextFile), "--resume"}, argv[1:])
@@ -775,7 +811,7 @@ func TestPrepareAgentLaunch(t *testing.T) {
775811
home := filepath.Join(t.TempDir(), "home")
776812
require.NoError(t, os.WriteFile(home, nil, 0o644))
777813

778-
_, _, err := prepareAgentLaunch(t.Context(), home, agent, nil)
814+
_, _, err := prepareAgentLaunch(dbr.MockRuntime(t.Context(), serverlessRuntime), home, agent, nil)
779815
require.Error(t, err)
780816
assert.Equal(t, protos.SshAgentShimErrorCategoryAgentContextSetupFailed, agentShimOutcome{err: err}.category())
781817
})

‎experimental/ssh/internal/server/server.go‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,9 @@ func Run(ctx context.Context, client *databricks.WorkspaceClient, opts ServerOpt
117117
log.Warnf(ctx, "Failed to seed environment activation, bare python/pip may resolve to the wrong interpreter in interactive sessions: %v", err)
118118
}
119119

120-
// The agent launchers are serverless-only for now: dedicated clusters have no
121-
// story for persisting the shims or respecting existing installs. Best-effort
122-
// for the same reason as seedEnvActivation: the tunnel works without them.
123-
if opts.Serverless {
124-
if err := installAgentLaunchers(ctx, wsHome); err != nil {
125-
log.Warnf(ctx, "Failed to install agent launchers, the affected agents won't be available in SSH sessions: %v", err)
126-
}
120+
// Best-effort for the same reason as seedEnvActivation: the tunnel works without them.
121+
if err := installAgentLaunchers(ctx, wsHome); err != nil {
122+
log.Warnf(ctx, "Failed to install agent launchers, the affected agents won't be available in SSH sessions: %v", err)
127123
}
128124

129125
createServerCommand := func(ctx context.Context) *exec.Cmd {

‎experimental/ssh/internal/server/sshd.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func sshdSetEnv(environ []string, homeDir string, opts ServerOptions) string {
109109
// interactive or not, IDE or terminal, bash or sh, with no startup file
110110
// involved. Appended (not prepended) so the launchers never shadow a real
111111
// binary of the same name earlier on PATH.
112-
if opts.Serverless && parts[0] == "PATH" {
112+
if parts[0] == "PATH" {
113113
value += ":" + path.Join(homeDir, agentlauncher.BinDir)
114114
}
115115
fmt.Fprintf(&setEnvBuf, ` %s="%s"`, parts[0], escapeEnvValue(value))

‎experimental/ssh/internal/server/sshd_test.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ func TestSSHDConfigSetsClientAliveInterval(t *testing.T) {
9191
func TestSSHDSetEnvAppendsAgentLaunchersToPath(t *testing.T) {
9292
environ := []string{"PATH=/usr/bin:/bin", "FOO=bar"}
9393

94-
serverless := sshdSetEnv(environ, "/home/spark", ServerOptions{Serverless: true})
95-
assert.Contains(t, serverless, ` PATH="/usr/bin:/bin:/home/spark/.agent-shim/bin"`)
96-
assert.Contains(t, serverless, ` FOO="bar"`)
97-
98-
dedicated := sshdSetEnv(environ, "/home/spark", ServerOptions{})
99-
assert.Contains(t, dedicated, ` PATH="/usr/bin:/bin"`)
94+
for _, opts := range []ServerOptions{{Serverless: true}, {}} {
95+
setEnv := sshdSetEnv(environ, "/home/spark", opts)
96+
assert.Contains(t, setEnv, ` PATH="/usr/bin:/bin:/home/spark/.agent-shim/bin"`, "serverless=%v", opts.Serverless)
97+
assert.Contains(t, setEnv, ` FOO="bar"`, "serverless=%v", opts.Serverless)
98+
}
10099
}

0 commit comments

Comments
 (0)