Skip to content

Commit 9a1e083

Browse files
maksim-zuev-dbIsaac
andauthored
Tell AI agents to pass the device code login on to the user (#7235)
Stacked on #7220. ## Changes When an AI agent is detected (`useragent.AgentProvider()`), device code login prints a note after the user code that tells the agent to give the URL and the code to the user, not to open the URL or approve the login itself, and to ask the user for the confirmation code. In the interactive login, the note also tells the agent to enter the code at the `Confirmation code` prompt. Agent detection does not change the flow. The note uses the new `agent.AgentNote`, which `AgentNotice` now uses too, with its text unchanged. The new `testutil.SetAgent` keeps the agent running the tests out of them. ## Why The login must be approved by the user, but an agent that runs it gets the URL and the code and could try to approve it itself, or not pass it on. Split out of #7220 to keep that PR smaller. ## Tests The `device-code/non-interactive/nominal` acceptance test runs both steps with `AGENT` set. `TestPromptForConfirmationCode_Agent` checks the interactive note, and `libs/agent` tests cover `AgentNote` and `AgentNotice`. This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent df305f2 commit 9a1e083

13 files changed

Lines changed: 175 additions & 9 deletions

File tree

‎acceptance/acceptance_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ func testAccept(t *testing.T, inprocessMode bool, selectedTests []string, skipTo
275275
"KIRO",
276276
"OPENCLAW_SHELL",
277277
"OPENCODE",
278+
"PI_CODING_AGENT",
278279
"VSCODE_AGENT",
279280
"WINDSURF_AGENT",
280281
} {

‎acceptance/cmd/auth/login/device-code/non-interactive/errors/output.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ Error: no pending device code login for host [DATABRICKS_URL]. Run the same comm
6262
>>> musterr [CLI] auth login --host [DATABRICKS_URL] --profile test --use-device-code --user-code WDJB-0001 --confirmation-code AAAA-AAAA
6363
Error: token exchange: Invalid confirmation code (error code: invalid_request). Check the confirmation code and approve the login if you haven't, then rerun this command. Run the same command without --user-code and --confirmation-code to start a new login if it still fails
6464

65+
>>> AGENT=test-agent musterr [CLI] auth login --host [DATABRICKS_URL] --profile test --use-device-code --user-code WDJB-0001 --confirmation-code AAAA-AAAA
66+
Error: token exchange: Invalid confirmation code (error code: invalid_request). Check the confirmation code and approve the login if you haven't, then rerun this command. Run the same command without --user-code and --confirmation-code to start a new login if it still fails
67+
68+
Note for AI agents (test-agent): ask the user to check the confirmation code
69+
and to approve the login if they haven't. Do not approve it yourself
70+
6571
=== Rerun with the right confirmation code
6672

6773
>>> [CLI] auth login --host [DATABRICKS_URL] --profile test --use-device-code --user-code wdjb-0001 --confirmation-code ftfa-qqky
@@ -102,6 +108,7 @@ Error: the login for profile test was started with different scopes. Complete it
102108
>>> print_requests.py --oneline //oidc/v1/device/auth //oidc/v1/token
103109
{"method": "POST", "path": "/oidc/v1/device/auth", "raw_body": "client_id=databricks-cli&scope=offline_access+all-apis"}
104110
{"method": "POST", "path": "/oidc/v1/token", "raw_body": "client_id=databricks-cli&confirmation_code=AAAAAAAA&device_code=device-code-1&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"}
111+
{"method": "POST", "path": "/oidc/v1/token", "raw_body": "client_id=databricks-cli&confirmation_code=AAAAAAAA&device_code=device-code-1&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"}
105112
{"method": "POST", "path": "/oidc/v1/token", "raw_body": "client_id=databricks-cli&confirmation_code=FTFAQQKY&device_code=device-code-1&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"}
106113
{"method": "POST", "path": "/oidc/v1/device/auth", "raw_body": "client_id=databricks-cli&scope=offline_access+all-apis"}
107114
{"method": "POST", "path": "/oidc/v1/token", "raw_body": "client_id=databricks-cli&confirmation_code=FTFAQQKY&device_code=device-code-2&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"}

‎acceptance/cmd/auth/login/device-code/non-interactive/errors/script‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ start_login LOG.wrong-code $CLI auth login --host $DATABRICKS_HOST --profile tes
3939

4040
title "Wrong confirmation code\n"
4141
trace musterr $CLI auth login --host $DATABRICKS_HOST --profile test --use-device-code --user-code WDJB-0001 --confirmation-code AAAA-AAAA
42+
trace AGENT=test-agent musterr $CLI auth login --host $DATABRICKS_HOST --profile test --use-device-code --user-code WDJB-0001 --confirmation-code AAAA-AAAA
4243

4344
# A wrong confirmation code leaves the device code valid, so the pending
4445
# login is kept.

‎acceptance/cmd/auth/login/device-code/non-interactive/nominal/out.databrickscfg‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ auth_type = databricks-cli
88

99
[__settings__]
1010
default_profile = test
11+
12+
[agent]
13+
host = [DATABRICKS_URL]
14+
workspace_id = [WORKSPACE_ID]
15+
auth_type = databricks-cli

‎acceptance/cmd/auth/login/device-code/non-interactive/nominal/output.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,23 @@ Profile test was successfully saved
9191
{"method": "POST", "path": "/oidc/v1/token", "raw_body": "client_id=databricks-cli&confirmation_code=FTFAQQKY&device_code=device-code-2&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"}
9292
{"method": "POST", "path": "/oidc/v1/device/auth", "raw_body": "client_id=databricks-cli&scope=offline_access+all-apis"}
9393
{"method": "POST", "path": "/oidc/v1/token", "raw_body": "client_id=databricks-cli&confirmation_code=FTFAQQKY&device_code=device-code-3&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"}
94+
95+
=== An AI agent gets notes on passing the login on to the user
96+
97+
>>> AGENT=test-agent [CLI] auth login --host [DATABRICKS_URL] --profile agent --use-device-code
98+
Using a browser on another device, visit:
99+
[DATABRICKS_URL]/device/verification
100+
101+
And enter the code:
102+
WDJB-0004
103+
104+
Note for AI agents (test-agent): give the URL and the code above to the user.
105+
Do not open the URL or approve the login yourself: the user must approve it.
106+
Ask the user for the confirmation code their browser shows.
107+
108+
Once finished, complete the login by running the same command with
109+
--user-code WDJB-0004 and --confirmation-code set to the confirmation code
110+
provided in your browser.
111+
112+
>>> AGENT=test-agent [CLI] auth login --host [DATABRICKS_URL] --profile agent --use-device-code --user-code WDJB-0004 --confirmation-code FTFA-QQKY
113+
Profile agent was successfully saved

‎acceptance/cmd/auth/login/device-code/non-interactive/nominal/script‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ trace $CLI auth login --profile test --use-device-code --user-code WDJB-0003 --c
3131
trace cache_keys
3232
trace print_requests.py --oneline //oidc/v1/device/auth //oidc/v1/token
3333

34+
title "An AI agent gets notes on passing the login on to the user\n"
35+
trace AGENT=test-agent $CLI auth login --host $DATABRICKS_HOST --profile agent --use-device-code
36+
trace AGENT=test-agent $CLI auth login --host $DATABRICKS_HOST --profile agent --use-device-code --user-code WDJB-0004 --confirmation-code FTFA-QQKY
37+
3438
# Track the .databrickscfg file that was created to surface changes.
3539
mv "./home/.databrickscfg" "./out.databrickscfg"

‎cmd/auth/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ in two steps instead:
120120
flags exchanges the pending device code instead of requesting a new one,
121121
and the rest of the login runs as usual.
122122

123+
Agent detection (`useragent.AgentProvider()`) does not pick the flow. It only
124+
adds a note after the user code, in both flows, that tells the agent to pass
125+
the login on to the user. In the flow that reads the confirmation code from
126+
stdin, the note also tells the agent to enter the code at the prompt. When the
127+
server rejects the confirmation code of the second step, the error gets a note
128+
that tells the agent to ask the user to approve the login, not to approve it
129+
itself.
130+
123131
The pending login is a token store entry with `PendingLogin` set instead of a
124132
token. Its key is the login's cache key (the profile name, or the host without
125133
a profile) with a `#device-code` suffix, so the target's current token keeps

‎cmd/auth/login_device_code.go‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"slices"
88
"strings"
99

10+
"github.com/databricks/cli/libs/agent"
1011
"github.com/databricks/cli/libs/auth/storage"
1112
"github.com/databricks/cli/libs/auth/u2m"
1213
"github.com/databricks/cli/libs/cmdio"
@@ -30,6 +31,11 @@ const pendingLoginKeySuffix = "#device-code"
3031
// that cannot be completed.
3132
const startNewLoginHint = "Run the same command without --user-code and --confirmation-code to start a new login"
3233

34+
// deviceCodeAgentNote starts the note that tells an AI agent to pass the login
35+
// on to the user.
36+
const deviceCodeAgentNote = "give the URL and the code above to the user.\n" +
37+
"Do not open the URL or approve the login yourself: the user must approve it.\n"
38+
3339
// deviceCodeIncompatibleFlags lists flags that device code login cannot honor.
3440
var deviceCodeIncompatibleFlags = []string{
3541
// Resource indicators are sent on the browser authorization request,
@@ -198,7 +204,8 @@ func (l nonInteractiveDeviceCodeLogin) start(ctx context.Context) error {
198204
if err != nil {
199205
return fmt.Errorf("store pending login: %w", err)
200206
}
201-
cmdio.LogString(ctx, strings.Join(append(approvalInstructions(*da),
207+
agentNote := deviceCodeAgentNote + "Ask the user for the confirmation code their browser shows."
208+
cmdio.LogString(ctx, strings.Join(append(approvalInstructions(*da, agentNote),
202209
"Once finished, complete the login by running the same command with",
203210
"--user-code "+formatCode(da.UserCode)+" and --confirmation-code set to the confirmation code",
204211
"provided in your browser.",
@@ -247,7 +254,14 @@ func (l nonInteractiveDeviceCodeLogin) complete(ctx context.Context, userCode, c
247254
case err == nil:
248255
l.deletePendingLogin(ctx)
249256
case errors.Is(err, u2m.ErrInvalidConfirmationCode):
250-
return nil, fmt.Errorf("%w. Check the confirmation code and approve the login if you haven't, then rerun this command. %s if it still fails", err, startNewLoginHint)
257+
err = fmt.Errorf("%w. Check the confirmation code and approve the login if you haven't, then rerun this command. %s if it still fails", err, startNewLoginHint)
258+
// The agent runs this step, so the error above is addressed to it.
259+
// Only the user may approve the login.
260+
if note := agent.AgentNote("ask the user to check the confirmation code\n" +
261+
"and to approve the login if they haven't. Do not approve it yourself"); note != "" {
262+
err = fmt.Errorf("%w\n\n%s", err, note)
263+
}
264+
return nil, err
251265
case errors.Is(err, u2m.ErrDeviceCodeRateLimited):
252266
return nil, fmt.Errorf("%w. Rerun this command later. %s if it still fails", err, startNewLoginHint)
253267
case errors.Is(err, u2m.ErrInvalidDeviceCode):
@@ -291,7 +305,9 @@ func sameCode(a, b string) bool {
291305
// and asks again.
292306
func promptForConfirmationCode(ctx context.Context, da u2m.DeviceAuthorization, lastErr error, attemptsLeft int) (string, error) {
293307
if lastErr == nil {
294-
cmdio.LogString(ctx, strings.Join(append(approvalInstructions(da),
308+
agentNote := deviceCodeAgentNote + "Ask the user for the confirmation code their browser shows, and enter it\n" +
309+
`at the "Confirmation code" prompt of this command.`
310+
cmdio.LogString(ctx, strings.Join(append(approvalInstructions(da, agentNote),
295311
"Once finished, enter the confirmation code provided in your browser.",
296312
), "\n"))
297313
} else {
@@ -314,15 +330,21 @@ func promptForConfirmationCode(ctx context.Context, da u2m.DeviceAuthorization,
314330

315331
// approvalInstructions returns the lines that show the user where to approve
316332
// the login. Both flows print them before saying how to complete the login.
317-
func approvalInstructions(da u2m.DeviceAuthorization) []string {
318-
return []string{
333+
// An AI agent also gets a note to pass the login on to the user, whether or
334+
// not it runs the command in a terminal.
335+
func approvalInstructions(da u2m.DeviceAuthorization, agentNote string) []string {
336+
lines := []string{
319337
"Using a browser on another device, visit:",
320338
da.VerificationURI,
321339
"",
322340
"And enter the code:",
323341
formatCode(da.UserCode),
324342
"",
325343
}
344+
if note := agent.AgentNote(agentNote); note != "" {
345+
lines = append(lines, note, "")
346+
}
347+
return lines
326348
}
327349

328350
// formatCode displays an 8-character device code as two groups of four

‎cmd/auth/login_device_code_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func TestValidateDeviceCodeLogin(t *testing.T) {
9494
}
9595

9696
func TestPromptForConfirmationCode(t *testing.T) {
97+
testutil.SetAgent(t, "")
9798
tests := []struct {
9899
name string
99100
input string
@@ -134,6 +135,36 @@ func TestPromptForConfirmationCode(t *testing.T) {
134135
}
135136
}
136137

138+
// An AI agent that runs the login in a terminal is told to enter the
139+
// confirmation code at the prompt.
140+
func TestPromptForConfirmationCode_Agent(t *testing.T) {
141+
testutil.SetAgent(t, "test-agent")
142+
var stderr bytes.Buffer
143+
ctx := cmdio.InContext(t.Context(), cmdio.NewIO(t.Context(), flags.OutputText, strings.NewReader("FTFA-QQKY\n"), &bytes.Buffer{}, &stderr, "", ""))
144+
145+
code, err := promptForConfirmationCode(ctx, u2m.DeviceAuthorization{
146+
UserCode: "WDJBMJHT",
147+
VerificationURI: "https://workspace.test/device/verification",
148+
}, nil, 3)
149+
require.NoError(t, err)
150+
assert.Equal(t, "FTFA-QQKY", code)
151+
assert.True(t, strings.HasPrefix(stderr.String(), strings.Join([]string{
152+
"Using a browser on another device, visit:",
153+
"https://workspace.test/device/verification",
154+
"",
155+
"And enter the code:",
156+
"WDJB-MJHT",
157+
"",
158+
"Note for AI agents (test-agent): give the URL and the code above to the user.",
159+
"Do not open the URL or approve the login yourself: the user must approve it.",
160+
"Ask the user for the confirmation code their browser shows, and enter it",
161+
`at the "Confirmation code" prompt of this command.`,
162+
"",
163+
"Once finished, enter the confirmation code provided in your browser.",
164+
"Confirmation code: ",
165+
}, "\n")), stderr.String())
166+
}
167+
137168
func TestPromptForConfirmationCode_Retry(t *testing.T) {
138169
tests := []struct {
139170
attemptsLeft int

‎docs/device-code-login.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ Profile my-profile was successfully saved
8585
confirmation code. It saves the profile and the token. Without
8686
`--profile`, it only stores the token for the host.
8787

88+
When the CLI detects an AI agent, it also prints a note after the code, with
89+
or without a terminal. The note tells the agent to pass the URL and the code
90+
on to you, not to approve the login itself, and to ask you for the
91+
confirmation code:
92+
93+
```console
94+
Note for AI agents (claude-code): give the URL and the code above to the user.
95+
Do not open the URL or approve the login yourself: the user must approve it.
96+
Ask the user for the confirmation code their browser shows.
97+
```
98+
99+
In a terminal, the note also tells the agent to enter the confirmation code
100+
at the `Confirmation code` prompt of the running command.
101+
88102
The pending login stays on your machine, so a confirmation code that ends up
89103
somewhere else, such as an agent's conversation, cannot complete the login on
90104
another machine.

0 commit comments

Comments
 (0)