From cde2f87ee5e72972c51a1e267685fa327e6a1b67 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Sat, 21 Nov 2026 03:37:47 +0000 Subject: [PATCH 01/14] air: Emit run config-validation errors as JSON with -o json `databricks air run -o json` printed config-validation failures (local schema errors such as "max_retries must be >= 0" and backend field errors from --dry-run) as plain text, so scripts parsing the JSON envelope broke. Route both through the existing error envelope (INVALID_ARGS / PERMANENT), matching the other air commands. Co-authored-by: Isaac --- acceptance/air/run-backend-error/output.txt | 14 ++++++++++++++ acceptance/air/run-backend-error/script | 3 +++ acceptance/air/run/output.txt | 16 ++++++++++++++++ acceptance/air/run/script | 3 +++ cmd/air/run.go | 4 ++-- 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/acceptance/air/run-backend-error/output.txt b/acceptance/air/run-backend-error/output.txt index bc9d2188d01..0cc3d32297e 100644 --- a/acceptance/air/run-backend-error/output.txt +++ b/acceptance/air/run-backend-error/output.txt @@ -8,3 +8,17 @@ Error: config validation failed: Tip: use the --debug flag to see more details and a trace of this error: databricks --debug air run … + +=== backend field errors fail dry-run (json) + +>>> [CLI] air run -f valid.yaml --dry-run -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "INVALID_ARGS", + "kind": "PERMANENT", + "message": "config validation failed:\n deployments[0].compute.accelerator_count: accelerator_count must be at most 1024, but was 1032.\n env_variables[1INVALID]: env_variables[1INVALID] contains characters that are not allowed.", + "retryable": false + } +} diff --git a/acceptance/air/run-backend-error/script b/acceptance/air/run-backend-error/script index 6b211207ba9..c843d11eec4 100644 --- a/acceptance/air/run-backend-error/script +++ b/acceptance/air/run-backend-error/script @@ -1,2 +1,5 @@ title "backend field errors fail dry-run" musterr trace $CLI air run -f valid.yaml --dry-run + +title "backend field errors fail dry-run (json)" +musterr trace $CLI air run -f valid.yaml --dry-run -o json diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index 64cba1975ae..c2b102c95ff 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -120,6 +120,22 @@ Tip: use the --debug flag to see more details and a trace of this error: Exit code: 1 +=== invalid config is rejected (json) + +>>> [CLI] air run -f invalid.yaml --dry-run -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "INVALID_ARGS", + "kind": "PERMANENT", + "message": "invalid experiment_name \"bad.name\": only alphanumeric characters, hyphens (-), and underscores (_) are allowed", + "retryable": false + } +} + +Exit code: 1 + === unknown field suggests the near match >>> [CLI] air run -f unknown-field.yaml --dry-run diff --git a/acceptance/air/run/script b/acceptance/air/run/script index 45c5437b857..ccbd21de303 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -62,6 +62,9 @@ errcode trace $CLI air run -f git-remote.yaml --dry-run title "invalid config is rejected" errcode trace $CLI air run -f invalid.yaml --dry-run +title "invalid config is rejected (json)" +errcode trace $CLI air run -f invalid.yaml --dry-run -o json + title "unknown field suggests the near match" errcode trace $CLI air run -f unknown-field.yaml --dry-run diff --git a/cmd/air/run.go b/cmd/air/run.go index 1baf2ad0e60..b6f9ca2bbed 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -162,7 +162,7 @@ databricks-ai-runtime skill: cfg, err := loadRunConfigWithOverrides(ctx, file, overrides) if err != nil { - return err + return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) } if msg := missingTorchWarning(ctx, cfg, file); msg != "" { @@ -187,7 +187,7 @@ databricks-ai-runtime skill: if validationErr != nil { unavailable, ok := validationUnavailable(validationErr) if !ok { - return validationErr + return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, validationErr) } kind := "PERMANENT" if unavailable.retryable { From bed61e96ad59fd4eada37eeba81c28a717a98fc4 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Sat, 21 Nov 2026 03:38:16 +0000 Subject: [PATCH 02/14] Add changelog fragment for #7864 Co-authored-by: Isaac --- .nextchanges/air/run-json-validation-errors.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 .nextchanges/air/run-json-validation-errors.md diff --git a/.nextchanges/air/run-json-validation-errors.md b/.nextchanges/air/run-json-validation-errors.md new file mode 100644 index 00000000000..834659635c2 --- /dev/null +++ b/.nextchanges/air/run-json-validation-errors.md @@ -0,0 +1 @@ +* `air run -o json` now reports config-validation errors as a JSON error envelope instead of plain text. ([#7864](https://github.com/databricks/cli/pull/7864)) From 71adc654b715f3d87fc2a87ad8ce4773a5c8388b Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Sat, 21 Nov 2026 03:53:41 +0000 Subject: [PATCH 03/14] Wrap the remaining --dry-run validation errors; use musterr in the JSON test Review round 1: code_source snapshot resolution and the idempotency-token check in the --dry-run block also returned plain text under -o json. Route them through renderError too, and assert the expected failure with musterr. Co-authored-by: Isaac --- acceptance/air/run/output.txt | 16 ++++++++++++++-- acceptance/air/run/script | 5 ++++- cmd/air/run.go | 4 ++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index c2b102c95ff..ff0125ae9a6 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -63,6 +63,20 @@ Error: code_source include_paths select no files under [TEST_TMP_DIR]: data. Che Tip: use the --debug flag to see more details and a trace of this error: databricks --debug air run … +=== include_paths entry that selects no files is rejected (json) + +>>> [CLI] air run -f include-unmatched.yaml --dry-run -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "INVALID_ARGS", + "kind": "PERMANENT", + "message": "code_source include_paths select no files under [TEST_TMP_DIR]: data. Check that these paths exist under root_path and hold files git does not ignore (`git check-ignore -v \u003cfile\u003e` shows the rule for a file); empty directories, nested git repositories and submodules add no files", + "retryable": false + } +} + === include_paths pathspec that selects files outside root_path is rejected >>> [CLI] air run -f include-outside-root.yaml --dry-run @@ -134,8 +148,6 @@ Exit code: 1 } } -Exit code: 1 - === unknown field suggests the near match >>> [CLI] air run -f unknown-field.yaml --dry-run diff --git a/acceptance/air/run/script b/acceptance/air/run/script index ccbd21de303..bcfbbfffc87 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -29,6 +29,9 @@ trace $CLI air run -f with-code-source.yaml --dry-run title "include_paths entry that selects no files is rejected" musterr trace $CLI air run -f include-unmatched.yaml --dry-run +title "include_paths entry that selects no files is rejected (json)" +musterr trace $CLI air run -f include-unmatched.yaml --dry-run -o json + title "include_paths pathspec that selects files outside root_path is rejected" # :(top) resolves against the repository root, so from root_path src it selects ../valid.yaml. musterr trace $CLI air run -f include-outside-root.yaml --dry-run @@ -63,7 +66,7 @@ title "invalid config is rejected" errcode trace $CLI air run -f invalid.yaml --dry-run title "invalid config is rejected (json)" -errcode trace $CLI air run -f invalid.yaml --dry-run -o json +musterr trace $CLI air run -f invalid.yaml --dry-run -o json title "unknown field suggests the near match" errcode trace $CLI air run -f unknown-field.yaml --dry-run diff --git a/cmd/air/run.go b/cmd/air/run.go index b6f9ca2bbed..f15a8ceca6b 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -175,14 +175,14 @@ databricks-ai-runtime skill: if cfg.CodeSource != nil && cfg.CodeSource.Snapshot != nil { count, err := countSnapshotFiles(ctx, cfg.CodeSource.Snapshot, file) if err != nil { - return err + return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) } snapshotFileCount = &count } idempotencyToken := effectiveIdempotencyToken(idempotencyKey, cfg) ctx, validationErr := validateDryRunInWorkspace(ctx, cmd, args, cfg, idempotencyToken, validationTimeout) if err := validateIdempotencyToken(idempotencyToken); err != nil { - return err + return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) } if validationErr != nil { unavailable, ok := validationUnavailable(validationErr) From 4d9b33d5dabebfac522210cc8b96004fadc2a220 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Sat, 21 Nov 2026 04:08:20 +0000 Subject: [PATCH 04/14] Report --dry-run authentication failures as UNAUTHENTICATED in JSON mode Review round 2: a failed workspace-client setup during --dry-run was labeled INVALID_ARGS. Mark it as an authentication error and route it through authError, like the other air commands. Co-authored-by: Isaac --- acceptance/air/run/output.txt | 14 ++++++++++++++ acceptance/air/run/script | 3 +++ cmd/air/run.go | 5 ++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index ff0125ae9a6..21728228ac0 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -148,6 +148,20 @@ Exit code: 1 } } +=== dry-run without credentials reports an authentication error (json) + +>>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN [CLI] air run -f valid.yaml --dry-run -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "UNAUTHENTICATED", + "kind": "PERMANENT", + "message": "no default profile is set: pass --profile (-p) or configure a default profile in your .databrickscfg", + "retryable": false + } +} + === unknown field suggests the near match >>> [CLI] air run -f unknown-field.yaml --dry-run diff --git a/acceptance/air/run/script b/acceptance/air/run/script index bcfbbfffc87..2819de913ad 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -68,6 +68,9 @@ errcode trace $CLI air run -f invalid.yaml --dry-run title "invalid config is rejected (json)" musterr trace $CLI air run -f invalid.yaml --dry-run -o json +title "dry-run without credentials reports an authentication error (json)" +musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.yaml --dry-run -o json + title "unknown field suggests the near match" errcode trace $CLI air run -f unknown-field.yaml --dry-run diff --git a/cmd/air/run.go b/cmd/air/run.go index f15a8ceca6b..564b2f6950d 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -55,7 +55,7 @@ func warnExperimentalContainers(ctx context.Context, cfg *runConfig) { func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []string, cfg *runConfig, idempotencyToken string, timeout time.Duration) (context.Context, error) { if !cmdctx.HasWorkspaceClient(ctx) { if err := root.MustWorkspaceClient(cmd, args); err != nil { - return ctx, err + return ctx, &validationAuthenticationError{err} } ctx = cmd.Context() } @@ -187,6 +187,9 @@ databricks-ai-runtime skill: if validationErr != nil { unavailable, ok := validationUnavailable(validationErr) if !ok { + if _, isAuth := errors.AsType[*validationAuthenticationError](validationErr); isAuth { + return authError(ctx, cmd, validationErr) + } return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, validationErr) } kind := "PERMANENT" From 6236c7e87033ac51c970c73da43c5137a14f15d5 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 17:51:41 +0000 Subject: [PATCH 05/14] Report 401/403 from the dry-run user lookup and config:validate as UNAUTHENTICATED Review feedback (caroline-db): credentials can still be rejected after the workspace client is built, by CurrentUser.Me in prospectiveLaunchPaths or by config:validate. Those errors fell through as INVALID_ARGS, which tells an agent to edit a valid config. Route errors that wrap apierr.ErrUnauthenticated or apierr.ErrPermissionDenied through authError, and cover both endpoints with 401 and 403. Co-authored-by: Isaac --- cmd/air/run.go | 13 ++++++++++++- cmd/air/run_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/cmd/air/run.go b/cmd/air/run.go index 564b2f6950d..7a120208c7b 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -18,6 +18,7 @@ import ( "github.com/databricks/cli/libs/flags" "github.com/databricks/cli/libs/shellquote" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/spf13/cobra" ) @@ -73,6 +74,16 @@ func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []s return ctx, validateDryRunConfig(ctx, w, cfg, commandPath, submittedContainers(cfg, funcDir), idempotencyToken, timeout) } +// isDryRunAuthFailure reports whether a --dry-run failure is about credentials or +// permissions rather than the config: building the workspace client, the +// current-user lookup, or config:validate rejecting the caller with 401/403. +func isDryRunAuthFailure(err error) bool { + if _, ok := errors.AsType[*validationAuthenticationError](err); ok { + return true + } + return errors.Is(err, apierr.ErrUnauthenticated) || errors.Is(err, apierr.ErrPermissionDenied) +} + func newRunCommand() *cobra.Command { return newRunCommandWithValidationTimeout(dryRunValidationTimeout) } @@ -187,7 +198,7 @@ databricks-ai-runtime skill: if validationErr != nil { unavailable, ok := validationUnavailable(validationErr) if !ok { - if _, isAuth := errors.AsType[*validationAuthenticationError](validationErr); isAuth { + if isDryRunAuthFailure(validationErr) { return authError(ctx, cmd, validationErr) } return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, validationErr) diff --git a/cmd/air/run_test.go b/cmd/air/run_test.go index 78f4deb3129..ecbeb0a5cae 100644 --- a/cmd/air/run_test.go +++ b/cmd/air/run_test.go @@ -266,6 +266,49 @@ func TestRunDryRunRetriesUnavailableResponseOnce(t *testing.T) { } } +// Credentials rejected after the workspace client is built (by the current-user +// lookup or by config:validate) must not be reported as INVALID_ARGS, or agents +// edit a valid config instead of fixing their credentials. +func TestRunDryRunReportsRejectedCredentialsAsUnauthenticated(t *testing.T) { + endpoints := []struct{ name, method, path string }{ + {"current user", "GET", "/api/2.0/preview/scim/v2/Me"}, + {"config validate", "POST", validateConfigPath}, + } + rejections := []struct { + status int + errorCode string + }{ + {http.StatusUnauthorized, "UNAUTHENTICATED"}, + {http.StatusForbidden, "PERMISSION_DENIED"}, + } + for _, endpoint := range endpoints { + for _, rejection := range rejections { + t.Run(endpoint.name+"/"+http.StatusText(rejection.status), func(t *testing.T) { + srv := testserver.New(t) + // Registered before AddDefaultHandlers: the router is first-wins. + srv.Handle(endpoint.method, endpoint.path, func(testserver.Request) any { + return testserver.Response{ + StatusCode: rejection.status, + Body: map[string]string{"error_code": rejection.errorCode, "message": "credentials rejected"}, + } + }) + testserver.AddDefaultHandlers(srv) + t.Cleanup(srv.Close) + + result := runDryRunCmd(t, dryRunValidationTimeout, flags.OutputJSON, srv.URL, minimalConfig, "--dry-run") + require.ErrorIs(t, result.err, root.ErrAlreadyPrinted) + var got errorEnvelope + require.NoError(t, json.Unmarshal([]byte(result.stdout), &got)) + assert.Equal(t, "UNAUTHENTICATED", got.Error.Code) + assert.Equal(t, "PERMANENT", got.Error.Kind) + assert.False(t, got.Error.Retryable) + assert.Contains(t, got.Error.Message, "authentication was not successful") + assert.Contains(t, got.Error.Message, "credentials rejected") + }) + } + } +} + func TestRunDryRunValidatesIdempotencyTokenAfterWorkspaceValidation(t *testing.T) { var requests []dryRunRequest srv := dryRunServer(t, validateConfigResponse{}, &requests) From 5e3b435eab56a6919de194f6ec2d02c1107f1252 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 18:09:34 +0000 Subject: [PATCH 06/14] Report dry-run 403s as PERMISSION_DENIED, print bundle diagnostics once Review round 3: - MustWorkspaceClient returns root.ErrAlreadyPrinted after bundle diagnostics; pass it through instead of adding a second, retryable INTERNAL_ERROR envelope. - A 403 from the current-user lookup or config:validate means the credentials work but lack access: report PERMISSION_DENIED, and keep UNAUTHENTICATED for 401s and clients that can't be built. - Apply the authError rewrite only with -o json, so text mode keeps the original error. Co-authored-by: Isaac --- acceptance/air/run/output.txt | 14 ++++++++++++++ acceptance/air/run/script | 11 +++++++++++ cmd/air/run.go | 32 +++++++++++++++++++++----------- cmd/air/run_test.go | 15 ++++++++------- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index 21728228ac0..4d96dd50fed 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -162,6 +162,20 @@ Exit code: 1 } } +=== dry-run without credentials keeps the original error in text mode + +>>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN [CLI] air run -f valid.yaml --dry-run +Error: default auth: cannot configure default credentials, please check https://docs.databricks.com/en/dev-tools/auth.html#databricks-client-unified-authentication to configure credentials for your preferred authentication method. Config: databricks_cli_path=[CLI]. Env: DATABRICKS_CLI_PATH + +Tip: use the --debug flag to see more details and a trace of this error: + databricks --debug air run … + +=== bundle diagnostics are reported once (json) + +>>> [CLI] air run -f ../valid.yaml --dry-run -o json +Error: failed to load [TEST_TMP_DIR]/broken-bundle/databricks.yml: yaml: line 2: did not find expected node content + + === unknown field suggests the near match >>> [CLI] air run -f unknown-field.yaml --dry-run diff --git a/acceptance/air/run/script b/acceptance/air/run/script index 2819de913ad..9e49acac582 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -71,6 +71,17 @@ musterr trace $CLI air run -f invalid.yaml --dry-run -o json title "dry-run without credentials reports an authentication error (json)" musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.yaml --dry-run -o json +title "dry-run without credentials keeps the original error in text mode" +musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.yaml --dry-run + +title "bundle diagnostics are reported once (json)" +mkdir broken-bundle +printf 'bundle:\n name: [\n' > broken-bundle/databricks.yml +cd broken-bundle +musterr trace $CLI air run -f ../valid.yaml --dry-run -o json +cd .. +rm -r broken-bundle + title "unknown field suggests the near match" errcode trace $CLI air run -f unknown-field.yaml --dry-run diff --git a/cmd/air/run.go b/cmd/air/run.go index 7a120208c7b..b6fdecf12fa 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -56,6 +56,10 @@ func warnExperimentalContainers(ctx context.Context, cfg *runConfig) { func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []string, cfg *runConfig, idempotencyToken string, timeout time.Duration) (context.Context, error) { if !cmdctx.HasWorkspaceClient(ctx) { if err := root.MustWorkspaceClient(cmd, args); err != nil { + // Bundle diagnostics were already printed; report them only once. + if errors.Is(err, root.ErrAlreadyPrinted) { + return ctx, err + } return ctx, &validationAuthenticationError{err} } ctx = cmd.Context() @@ -74,14 +78,20 @@ func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []s return ctx, validateDryRunConfig(ctx, w, cfg, commandPath, submittedContainers(cfg, funcDir), idempotencyToken, timeout) } -// isDryRunAuthFailure reports whether a --dry-run failure is about credentials or -// permissions rather than the config: building the workspace client, the -// current-user lookup, or config:validate rejecting the caller with 401/403. -func isDryRunAuthFailure(err error) bool { - if _, ok := errors.AsType[*validationAuthenticationError](err); ok { - return true +// renderDryRunCallerError renders a --dry-run failure that is the caller's to +// fix. Credential and permission failures are kept apart from config errors so +// agents don't edit a valid config: a 401 or a client that can't be built is +// UNAUTHENTICATED, and a 403 is PERMISSION_DENIED since the credentials work. +// Text mode keeps the original error. +func renderDryRunCallerError(ctx context.Context, cmd *cobra.Command, err error) error { + if errors.Is(err, apierr.ErrPermissionDenied) { + return renderError(ctx, cmd, "PERMISSION_DENIED", "PERMANENT", false, err) + } + _, clientErr := errors.AsType[*validationAuthenticationError](err) + if (clientErr || errors.Is(err, apierr.ErrUnauthenticated)) && root.OutputType(cmd) == flags.OutputJSON { + return authError(ctx, cmd, err) } - return errors.Is(err, apierr.ErrUnauthenticated) || errors.Is(err, apierr.ErrPermissionDenied) + return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) } func newRunCommand() *cobra.Command { @@ -192,16 +202,16 @@ databricks-ai-runtime skill: } idempotencyToken := effectiveIdempotencyToken(idempotencyKey, cfg) ctx, validationErr := validateDryRunInWorkspace(ctx, cmd, args, cfg, idempotencyToken, validationTimeout) + if errors.Is(validationErr, root.ErrAlreadyPrinted) { + return validationErr + } if err := validateIdempotencyToken(idempotencyToken); err != nil { return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) } if validationErr != nil { unavailable, ok := validationUnavailable(validationErr) if !ok { - if isDryRunAuthFailure(validationErr) { - return authError(ctx, cmd, validationErr) - } - return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, validationErr) + return renderDryRunCallerError(ctx, cmd, validationErr) } kind := "PERMANENT" if unavailable.retryable { diff --git a/cmd/air/run_test.go b/cmd/air/run_test.go index ecbeb0a5cae..0fed01114bd 100644 --- a/cmd/air/run_test.go +++ b/cmd/air/run_test.go @@ -268,15 +268,17 @@ func TestRunDryRunRetriesUnavailableResponseOnce(t *testing.T) { // Credentials rejected after the workspace client is built (by the current-user // lookup or by config:validate) must not be reported as INVALID_ARGS, or agents -// edit a valid config instead of fixing their credentials. -func TestRunDryRunReportsRejectedCredentialsAsUnauthenticated(t *testing.T) { +// edit a valid config instead of fixing their credentials. A 401 is +// UNAUTHENTICATED; a 403 is PERMISSION_DENIED, since the credentials work. +func TestRunDryRunReportsRejectedCredentials(t *testing.T) { endpoints := []struct{ name, method, path string }{ {"current user", "GET", "/api/2.0/preview/scim/v2/Me"}, {"config validate", "POST", validateConfigPath}, } + // The backend's error_code doubles as the envelope code each status maps to. rejections := []struct { - status int - errorCode string + status int + code string }{ {http.StatusUnauthorized, "UNAUTHENTICATED"}, {http.StatusForbidden, "PERMISSION_DENIED"}, @@ -289,7 +291,7 @@ func TestRunDryRunReportsRejectedCredentialsAsUnauthenticated(t *testing.T) { srv.Handle(endpoint.method, endpoint.path, func(testserver.Request) any { return testserver.Response{ StatusCode: rejection.status, - Body: map[string]string{"error_code": rejection.errorCode, "message": "credentials rejected"}, + Body: map[string]string{"error_code": rejection.code, "message": "credentials rejected"}, } }) testserver.AddDefaultHandlers(srv) @@ -299,10 +301,9 @@ func TestRunDryRunReportsRejectedCredentialsAsUnauthenticated(t *testing.T) { require.ErrorIs(t, result.err, root.ErrAlreadyPrinted) var got errorEnvelope require.NoError(t, json.Unmarshal([]byte(result.stdout), &got)) - assert.Equal(t, "UNAUTHENTICATED", got.Error.Code) + assert.Equal(t, rejection.code, got.Error.Code) assert.Equal(t, "PERMANENT", got.Error.Kind) assert.False(t, got.Error.Retryable) - assert.Contains(t, got.Error.Message, "authentication was not successful") assert.Contains(t, got.Error.Message, "credentials rejected") }) } From b27955bfde6b7f4cf5dc2c33ade83efdb159b64f Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 18:17:57 +0000 Subject: [PATCH 07/14] Treat a current-user 403 as bad credentials; commit the broken-bundle fixture Review round 4 nits: - The current-user lookup fails only for bad credentials, so a 403 from it now goes through authError (UNAUTHENTICATED), as in air get. A 403 from config:validate after that lookup passed stays PERMISSION_DENIED. - Say in the comment that workspace-client failures go through authError. - Commit the broken databricks.yml fixture and run it with withdir. - Mention the new dry-run auth codes in the changelog fragment. Co-authored-by: Isaac --- .../air/run-json-validation-errors.md | 2 +- .../air/run/broken-bundle/databricks.yml | 2 + acceptance/air/run/script | 7 +- cmd/air/run.go | 25 ++++--- cmd/air/run_test.go | 68 +++++++++---------- 5 files changed, 53 insertions(+), 51 deletions(-) create mode 100644 acceptance/air/run/broken-bundle/databricks.yml diff --git a/.nextchanges/air/run-json-validation-errors.md b/.nextchanges/air/run-json-validation-errors.md index 834659635c2..f77ceb9fb78 100644 --- a/.nextchanges/air/run-json-validation-errors.md +++ b/.nextchanges/air/run-json-validation-errors.md @@ -1 +1 @@ -* `air run -o json` now reports config-validation errors as a JSON error envelope instead of plain text. ([#7864](https://github.com/databricks/cli/pull/7864)) +* `air run -o json` now reports config-validation errors as a JSON error envelope instead of plain text. With `--dry-run`, rejected credentials report `UNAUTHENTICATED` and a missing permission on `config:validate` reports the new `PERMISSION_DENIED` code. ([#7864](https://github.com/databricks/cli/pull/7864)) diff --git a/acceptance/air/run/broken-bundle/databricks.yml b/acceptance/air/run/broken-bundle/databricks.yml new file mode 100644 index 00000000000..6cc57f4b5e2 --- /dev/null +++ b/acceptance/air/run/broken-bundle/databricks.yml @@ -0,0 +1,2 @@ +bundle: + name: [ diff --git a/acceptance/air/run/script b/acceptance/air/run/script index 9e49acac582..168087a2e1a 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -75,12 +75,7 @@ title "dry-run without credentials keeps the original error in text mode" musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.yaml --dry-run title "bundle diagnostics are reported once (json)" -mkdir broken-bundle -printf 'bundle:\n name: [\n' > broken-bundle/databricks.yml -cd broken-bundle -musterr trace $CLI air run -f ../valid.yaml --dry-run -o json -cd .. -rm -r broken-bundle +musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml --dry-run -o json title "unknown field suggests the near match" errcode trace $CLI air run -f unknown-field.yaml --dry-run diff --git a/cmd/air/run.go b/cmd/air/run.go index b6fdecf12fa..38c56c5aa70 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -70,6 +70,10 @@ func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []s if err != nil { unavailable, retryable := classifyValidationFailure(err) if !unavailable { + // The current-user lookup rejects only bad credentials, even with a 403. + if errors.Is(err, apierr.ErrUnauthenticated) || errors.Is(err, apierr.ErrPermissionDenied) { + return ctx, &validationAuthenticationError{err} + } return ctx, err } return ctx, asValidationUnavailable(err, retryable) @@ -79,17 +83,20 @@ func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []s } // renderDryRunCallerError renders a --dry-run failure that is the caller's to -// fix. Credential and permission failures are kept apart from config errors so -// agents don't edit a valid config: a 401 or a client that can't be built is -// UNAUTHENTICATED, and a 403 is PERMISSION_DENIED since the credentials work. -// Text mode keeps the original error. +// fix, keeping credential and permission failures apart from config errors so +// agents don't edit a valid config. Workspace-client and current-user failures, +// and a 401 from config:validate, go through authError, as in air get. A 403 +// from config:validate is PERMISSION_DENIED: the same credentials just passed the +// current-user lookup. Text mode keeps the original error. func renderDryRunCallerError(ctx context.Context, cmd *cobra.Command, err error) error { - if errors.Is(err, apierr.ErrPermissionDenied) { - return renderError(ctx, cmd, "PERMISSION_DENIED", "PERMANENT", false, err) - } - _, clientErr := errors.AsType[*validationAuthenticationError](err) - if (clientErr || errors.Is(err, apierr.ErrUnauthenticated)) && root.OutputType(cmd) == flags.OutputJSON { + _, credentialsRejected := errors.AsType[*validationAuthenticationError](err) + switch { + case root.OutputType(cmd) != flags.OutputJSON: + return err + case credentialsRejected || errors.Is(err, apierr.ErrUnauthenticated): return authError(ctx, cmd, err) + case errors.Is(err, apierr.ErrPermissionDenied): + return renderError(ctx, cmd, "PERMISSION_DENIED", "PERMANENT", false, err) } return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) } diff --git a/cmd/air/run_test.go b/cmd/air/run_test.go index 0fed01114bd..ccf74ab0787 100644 --- a/cmd/air/run_test.go +++ b/cmd/air/run_test.go @@ -268,45 +268,43 @@ func TestRunDryRunRetriesUnavailableResponseOnce(t *testing.T) { // Credentials rejected after the workspace client is built (by the current-user // lookup or by config:validate) must not be reported as INVALID_ARGS, or agents -// edit a valid config instead of fixing their credentials. A 401 is -// UNAUTHENTICATED; a 403 is PERMISSION_DENIED, since the credentials work. +// edit a valid config instead of fixing their credentials. The current-user +// lookup fails only for bad credentials; a 403 from config:validate after it +// passed is a missing permission. func TestRunDryRunReportsRejectedCredentials(t *testing.T) { - endpoints := []struct{ name, method, path string }{ - {"current user", "GET", "/api/2.0/preview/scim/v2/Me"}, - {"config validate", "POST", validateConfigPath}, - } - // The backend's error_code doubles as the envelope code each status maps to. - rejections := []struct { - status int - code string + tests := []struct { + name, method, path string + status int + errorCode string + wantCode string }{ - {http.StatusUnauthorized, "UNAUTHENTICATED"}, - {http.StatusForbidden, "PERMISSION_DENIED"}, + {"current user/401", "GET", "/api/2.0/preview/scim/v2/Me", http.StatusUnauthorized, "UNAUTHENTICATED", "UNAUTHENTICATED"}, + {"current user/403", "GET", "/api/2.0/preview/scim/v2/Me", http.StatusForbidden, "PERMISSION_DENIED", "UNAUTHENTICATED"}, + {"config validate/401", "POST", validateConfigPath, http.StatusUnauthorized, "UNAUTHENTICATED", "UNAUTHENTICATED"}, + {"config validate/403", "POST", validateConfigPath, http.StatusForbidden, "PERMISSION_DENIED", "PERMISSION_DENIED"}, } - for _, endpoint := range endpoints { - for _, rejection := range rejections { - t.Run(endpoint.name+"/"+http.StatusText(rejection.status), func(t *testing.T) { - srv := testserver.New(t) - // Registered before AddDefaultHandlers: the router is first-wins. - srv.Handle(endpoint.method, endpoint.path, func(testserver.Request) any { - return testserver.Response{ - StatusCode: rejection.status, - Body: map[string]string{"error_code": rejection.code, "message": "credentials rejected"}, - } - }) - testserver.AddDefaultHandlers(srv) - t.Cleanup(srv.Close) - - result := runDryRunCmd(t, dryRunValidationTimeout, flags.OutputJSON, srv.URL, minimalConfig, "--dry-run") - require.ErrorIs(t, result.err, root.ErrAlreadyPrinted) - var got errorEnvelope - require.NoError(t, json.Unmarshal([]byte(result.stdout), &got)) - assert.Equal(t, rejection.code, got.Error.Code) - assert.Equal(t, "PERMANENT", got.Error.Kind) - assert.False(t, got.Error.Retryable) - assert.Contains(t, got.Error.Message, "credentials rejected") + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + srv := testserver.New(t) + // Registered before AddDefaultHandlers: the router is first-wins. + srv.Handle(tc.method, tc.path, func(testserver.Request) any { + return testserver.Response{ + StatusCode: tc.status, + Body: map[string]string{"error_code": tc.errorCode, "message": "credentials rejected"}, + } }) - } + testserver.AddDefaultHandlers(srv) + t.Cleanup(srv.Close) + + result := runDryRunCmd(t, dryRunValidationTimeout, flags.OutputJSON, srv.URL, minimalConfig, "--dry-run") + require.ErrorIs(t, result.err, root.ErrAlreadyPrinted) + var got errorEnvelope + require.NoError(t, json.Unmarshal([]byte(result.stdout), &got)) + assert.Equal(t, tc.wantCode, got.Error.Code) + assert.Equal(t, "PERMANENT", got.Error.Kind) + assert.False(t, got.Error.Retryable) + assert.Contains(t, got.Error.Message, "credentials rejected") + }) } } From 331aebecd11e4d057edf96fe79c3a2fd8c485a6c Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 18:24:34 +0000 Subject: [PATCH 08/14] Use a valid-YAML broken bundle fixture so task fmt can parse it The fixture was a YAML syntax error, which fmt-yaml rejects. Mark two targets as default instead (as acceptance/cmd/feedback does): the file is valid YAML but an invalid bundle, so air run still sees printed diagnostics. Co-authored-by: Isaac --- acceptance/air/run/broken-bundle/databricks.yml | 10 +++++++++- acceptance/air/run/output.txt | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/acceptance/air/run/broken-bundle/databricks.yml b/acceptance/air/run/broken-bundle/databricks.yml index 6cc57f4b5e2..efaa43cf06a 100644 --- a/acceptance/air/run/broken-bundle/databricks.yml +++ b/acceptance/air/run/broken-bundle/databricks.yml @@ -1,2 +1,10 @@ bundle: - name: [ + name: broken-bundle + +# Two targets both marked default is an invalid bundle, so loading it prints +# diagnostics before air run reaches the workspace. +targets: + dev: + default: true + dev2: + default: true diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index 4d96dd50fed..bd843982cd5 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -173,7 +173,7 @@ Tip: use the --debug flag to see more details and a trace of this error: === bundle diagnostics are reported once (json) >>> [CLI] air run -f ../valid.yaml --dry-run -o json -Error: failed to load [TEST_TMP_DIR]/broken-bundle/databricks.yml: yaml: line 2: did not find expected node content +Error: multiple targets are marked as default (dev, dev2) === unknown field suggests the near match From e937cbed481876e9284332298058e2d1c763b584 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 18:57:39 +0000 Subject: [PATCH 09/14] Report workspace-client setup errors as permanent JSON envelopes in air run Review of 331aebe (addressed in code per Vincent): - A workspace-client setup failure that isn't an auth error (an unknown or account-only profile) is INVALID_ARGS/PERMANENT, not authError's retryable INTERNAL_ERROR fallback. Scoped to air run. - The non-dry-run PreRunE renders the same envelopes as --dry-run, as air get's PreRunE does. - Bundle diagnostics that were already printed still pass through without an envelope, matching air get; covered in both modes. - Normalize the bundle loader's random default-target order in the test. Co-authored-by: Isaac --- .../air/run-json-validation-errors.md | 2 +- acceptance/air/run/output.txt | 34 ++++++++++++++++++- acceptance/air/run/profiles.databrickscfg | 3 ++ acceptance/air/run/script | 7 ++++ acceptance/air/run/test.toml | 5 +++ cmd/air/run.go | 32 ++++++++++++----- 6 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 acceptance/air/run/profiles.databrickscfg diff --git a/.nextchanges/air/run-json-validation-errors.md b/.nextchanges/air/run-json-validation-errors.md index f77ceb9fb78..2084b126d5a 100644 --- a/.nextchanges/air/run-json-validation-errors.md +++ b/.nextchanges/air/run-json-validation-errors.md @@ -1 +1 @@ -* `air run -o json` now reports config-validation errors as a JSON error envelope instead of plain text. With `--dry-run`, rejected credentials report `UNAUTHENTICATED` and a missing permission on `config:validate` reports the new `PERMISSION_DENIED` code. ([#7864](https://github.com/databricks/cli/pull/7864)) +* `air run -o json` now reports config-validation errors as a JSON error envelope instead of plain text. Rejected credentials report `UNAUTHENTICATED`, other workspace client setup errors (such as an unknown profile) report `INVALID_ARGS`, and with `--dry-run` a missing permission on `config:validate` reports the new `PERMISSION_DENIED` code. ([#7864](https://github.com/databricks/cli/pull/7864)) diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index bd843982cd5..da6b3056de3 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -170,10 +170,42 @@ Error: default auth: cannot configure default credentials, please check https:// Tip: use the --debug flag to see more details and a trace of this error: databricks --debug air run … +=== submit without credentials reports an authentication error (json) + +>>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN [CLI] air run -f valid.yaml -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "UNAUTHENTICATED", + "kind": "PERMANENT", + "message": "no default profile is set: pass --profile (-p) or configure a default profile in your .databrickscfg", + "retryable": false + } +} + +=== dry-run with an unknown profile is a permanent config error (json) + +>>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN DATABRICKS_CONFIG_FILE=profiles.databrickscfg [CLI] air run -f valid.yaml --dry-run -o json -p does-not-exist +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "INVALID_ARGS", + "kind": "PERMANENT", + "message": "resolve: profiles.databrickscfg has no does-not-exist profile configured. Config: profile=does-not-exist, config_file=profiles.databrickscfg, databricks_cli_path=[CLI]. Env: DATABRICKS_CONFIG_FILE, DATABRICKS_CLI_PATH", + "retryable": false + } +} + === bundle diagnostics are reported once (json) >>> [CLI] air run -f ../valid.yaml --dry-run -o json -Error: multiple targets are marked as default (dev, dev2) +Error: multiple targets are marked as default ([TARGETS]) + + +>>> [CLI] air run -f ../valid.yaml -o json +Error: multiple targets are marked as default ([TARGETS]) === unknown field suggests the near match diff --git a/acceptance/air/run/profiles.databrickscfg b/acceptance/air/run/profiles.databrickscfg new file mode 100644 index 00000000000..3c1dacccfea --- /dev/null +++ b/acceptance/air/run/profiles.databrickscfg @@ -0,0 +1,3 @@ +[other] +host = https://other.test +token = other-token diff --git a/acceptance/air/run/script b/acceptance/air/run/script index 168087a2e1a..cbbeb33773e 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -74,8 +74,15 @@ musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.y title "dry-run without credentials keeps the original error in text mode" musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.yaml --dry-run +title "submit without credentials reports an authentication error (json)" +musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.yaml -o json + +title "dry-run with an unknown profile is a permanent config error (json)" +musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN DATABRICKS_CONFIG_FILE=profiles.databrickscfg $CLI air run -f valid.yaml --dry-run -o json -p does-not-exist + title "bundle diagnostics are reported once (json)" musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml --dry-run -o json +musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml -o json title "unknown field suggests the near match" errcode trace $CLI air run -f unknown-field.yaml --dry-run diff --git a/acceptance/air/run/test.toml b/acceptance/air/run/test.toml index 3daed6e5e2c..d3d0e7cb0eb 100644 --- a/acceptance/air/run/test.toml +++ b/acceptance/air/run/test.toml @@ -1,3 +1,8 @@ [[Server]] Pattern = "POST /api/2.0/ai-training/config:validate" Response.Body = '{}' + +# The bundle loader lists default targets in map order. +[[Repls]] +Old = 'marked as default \((dev, dev2|dev2, dev)\)' +New = 'marked as default ([TARGETS])' diff --git a/cmd/air/run.go b/cmd/air/run.go index 38c56c5aa70..691ae47fa04 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -19,6 +19,7 @@ import ( "github.com/databricks/cli/libs/shellquote" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" ) @@ -56,11 +57,7 @@ func warnExperimentalContainers(ctx context.Context, cfg *runConfig) { func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []string, cfg *runConfig, idempotencyToken string, timeout time.Duration) (context.Context, error) { if !cmdctx.HasWorkspaceClient(ctx) { if err := root.MustWorkspaceClient(cmd, args); err != nil { - // Bundle diagnostics were already printed; report them only once. - if errors.Is(err, root.ErrAlreadyPrinted) { - return ctx, err - } - return ctx, &validationAuthenticationError{err} + return ctx, renderWorkspaceClientError(ctx, cmd, err) } ctx = cmd.Context() } @@ -82,10 +79,26 @@ func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []s return ctx, validateDryRunConfig(ctx, w, cfg, commandPath, submittedContainers(cfg, funcDir), idempotencyToken, timeout) } +// renderWorkspaceClientError renders a failure to build the workspace client. +// Bundle diagnostics that were already printed pass through, as in air get. A +// missing profile or rejected credentials go through authError. Any other setup +// failure (an unknown or account-only profile, for example) is a config mistake +// that a retry can't fix, so it is INVALID_ARGS rather than authError's +// retryable fallback. Text mode keeps the original error. +func renderWorkspaceClientError(ctx context.Context, cmd *cobra.Command, err error) error { + switch { + case errors.Is(err, root.ErrAlreadyPrinted), root.OutputType(cmd) != flags.OutputJSON: + return err + case errors.Is(err, config.ErrCannotConfigureDefault), errors.Is(err, apierr.ErrUnauthenticated), errors.Is(err, apierr.ErrPermissionDenied): + return authError(ctx, cmd, err) + } + return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) +} + // renderDryRunCallerError renders a --dry-run failure that is the caller's to // fix, keeping credential and permission failures apart from config errors so -// agents don't edit a valid config. Workspace-client and current-user failures, -// and a 401 from config:validate, go through authError, as in air get. A 403 +// agents don't edit a valid config. Current-user failures and a 401 from +// config:validate go through authError, as in air get. A 403 // from config:validate is PERMISSION_DENIED: the same credentials just passed the // current-user lookup. Text mode keeps the original error. func renderDryRunCallerError(ctx context.Context, cmd *cobra.Command, err error) error { @@ -182,7 +195,10 @@ databricks-ai-runtime skill: if dryRun { return nil } - return root.MustWorkspaceClient(cmd, args) + if err := root.MustWorkspaceClient(cmd, args); err != nil { + return renderWorkspaceClientError(cmd.Context(), cmd, err) + } + return nil } cmd.RunE = func(cmd *cobra.Command, args []string) error { From 2c60ba9ae57cd208a39a195c217b80ae69b524a7 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 19:06:59 +0000 Subject: [PATCH 10/14] Render dry-run workspace-client errors after the idempotency-key check Review of e937cbe: - [blocking] Rendering the workspace-client error inside the dry-run validation skipped the idempotency-key check, so -o json hid an over-long key behind UNAUTHENTICATED and text mode dropped the key error after bundle diagnostics. Return the raw error and render it after the key check, restoring the original order in both modes. - Add an acceptance golden for the PERMISSION_DENIED envelope from a config:validate 403. Co-authored-by: Isaac --- .../air/run-permission-denied/out.test.toml | 1 + .../air/run-permission-denied/output.txt | 14 ++++++++++++ acceptance/air/run-permission-denied/script | 2 ++ .../air/run-permission-denied/test.toml | 9 ++++++++ .../air/run-permission-denied/valid.yaml | 7 ++++++ acceptance/air/run/output.txt | 22 +++++++++++++++++++ acceptance/air/run/script | 4 ++++ cmd/air/run.go | 17 ++++++++++---- 8 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 acceptance/air/run-permission-denied/out.test.toml create mode 100644 acceptance/air/run-permission-denied/output.txt create mode 100644 acceptance/air/run-permission-denied/script create mode 100644 acceptance/air/run-permission-denied/test.toml create mode 100644 acceptance/air/run-permission-denied/valid.yaml diff --git a/acceptance/air/run-permission-denied/out.test.toml b/acceptance/air/run-permission-denied/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/air/run-permission-denied/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/air/run-permission-denied/output.txt b/acceptance/air/run-permission-denied/output.txt new file mode 100644 index 00000000000..9c82b131bff --- /dev/null +++ b/acceptance/air/run-permission-denied/output.txt @@ -0,0 +1,14 @@ + +=== a 403 from config:validate is a missing permission (json) + +>>> [CLI] air run -f valid.yaml --dry-run -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "PERMISSION_DENIED", + "kind": "PERMANENT", + "message": "failed to validate config: User does not have access to the accelerator pool.", + "retryable": false + } +} diff --git a/acceptance/air/run-permission-denied/script b/acceptance/air/run-permission-denied/script new file mode 100644 index 00000000000..5e6fd748fab --- /dev/null +++ b/acceptance/air/run-permission-denied/script @@ -0,0 +1,2 @@ +title "a 403 from config:validate is a missing permission (json)" +musterr trace $CLI air run -f valid.yaml --dry-run -o json diff --git a/acceptance/air/run-permission-denied/test.toml b/acceptance/air/run-permission-denied/test.toml new file mode 100644 index 00000000000..51a8f127655 --- /dev/null +++ b/acceptance/air/run-permission-denied/test.toml @@ -0,0 +1,9 @@ +[[Server]] +Pattern = "POST /api/2.0/ai-training/config:validate" +Response.Body = ''' +{ + "error_code": "PERMISSION_DENIED", + "message": "User does not have access to the accelerator pool." +} +''' +Response.StatusCode = 403 diff --git a/acceptance/air/run-permission-denied/valid.yaml b/acceptance/air/run-permission-denied/valid.yaml new file mode 100644 index 00000000000..b717585c0bc --- /dev/null +++ b/acceptance/air/run-permission-denied/valid.yaml @@ -0,0 +1,7 @@ +experiment_name: smoke-test +command: python train.py +compute: + accelerator_type: GPU_8xH100 + num_accelerators: 1032 +env_variables: + 1INVALID: value diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index da6b3056de3..ea5f6617170 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -184,6 +184,20 @@ Tip: use the --debug flag to see more details and a trace of this error: } } +=== dry-run reports an over-long idempotency key before missing credentials (json) + +>>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN [CLI] air run -f valid.yaml --dry-run -o json --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "INVALID_ARGS", + "kind": "PERMANENT", + "message": "idempotency token must be 64 characters or less, got 65", + "retryable": false + } +} + === dry-run with an unknown profile is a permanent config error (json) >>> env -u DATABRICKS_HOST -u DATABRICKS_TOKEN DATABRICKS_CONFIG_FILE=profiles.databrickscfg [CLI] air run -f valid.yaml --dry-run -o json -p does-not-exist @@ -204,6 +218,14 @@ Tip: use the --debug flag to see more details and a trace of this error: Error: multiple targets are marked as default ([TARGETS]) +>>> [CLI] air run -f ../valid.yaml --dry-run --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +Error: multiple targets are marked as default ([TARGETS]) + +Error: idempotency token must be 64 characters or less, got 65 + +Tip: use the --debug flag to see more details and a trace of this error: + databricks --debug air run … + >>> [CLI] air run -f ../valid.yaml -o json Error: multiple targets are marked as default ([TARGETS]) diff --git a/acceptance/air/run/script b/acceptance/air/run/script index cbbeb33773e..a1ba2b640f9 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -77,11 +77,15 @@ musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.y title "submit without credentials reports an authentication error (json)" musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.yaml -o json +title "dry-run reports an over-long idempotency key before missing credentials (json)" +musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN $CLI air run -f valid.yaml --dry-run -o json --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + title "dry-run with an unknown profile is a permanent config error (json)" musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN DATABRICKS_CONFIG_FILE=profiles.databrickscfg $CLI air run -f valid.yaml --dry-run -o json -p does-not-exist title "bundle diagnostics are reported once (json)" musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml --dry-run -o json +musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml --dry-run --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml -o json title "unknown field suggests the near match" diff --git a/cmd/air/run.go b/cmd/air/run.go index 691ae47fa04..a846dbf2d7d 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -57,7 +57,7 @@ func warnExperimentalContainers(ctx context.Context, cfg *runConfig) { func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []string, cfg *runConfig, idempotencyToken string, timeout time.Duration) (context.Context, error) { if !cmdctx.HasWorkspaceClient(ctx) { if err := root.MustWorkspaceClient(cmd, args); err != nil { - return ctx, renderWorkspaceClientError(ctx, cmd, err) + return ctx, &workspaceClientError{err} } ctx = cmd.Context() } @@ -79,6 +79,15 @@ func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []s return ctx, validateDryRunConfig(ctx, w, cfg, commandPath, submittedContainers(cfg, funcDir), idempotencyToken, timeout) } +// workspaceClientError marks a --dry-run failure to build the workspace client, +// so it is rendered after the local checks, in the same order as before. +type workspaceClientError struct { + err error +} + +func (e *workspaceClientError) Error() string { return e.err.Error() } +func (e *workspaceClientError) Unwrap() error { return e.err } + // renderWorkspaceClientError renders a failure to build the workspace client. // Bundle diagnostics that were already printed pass through, as in air get. A // missing profile or rejected credentials go through authError. Any other setup @@ -225,12 +234,12 @@ databricks-ai-runtime skill: } idempotencyToken := effectiveIdempotencyToken(idempotencyKey, cfg) ctx, validationErr := validateDryRunInWorkspace(ctx, cmd, args, cfg, idempotencyToken, validationTimeout) - if errors.Is(validationErr, root.ErrAlreadyPrinted) { - return validationErr - } if err := validateIdempotencyToken(idempotencyToken); err != nil { return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) } + if clientErr, ok := errors.AsType[*workspaceClientError](validationErr); ok { + return renderWorkspaceClientError(ctx, cmd, clientErr.err) + } if validationErr != nil { unavailable, ok := validationUnavailable(validationErr) if !ok { From f73e33170131930b638ecb37e5b5880a133d4499 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 19:17:04 +0000 Subject: [PATCH 11/14] Keep expired logins and transient auth failures out of INVALID_ARGS Review of 2c60ba9: - [blocking] The INVALID_ARGS fallback for workspace-client setup errors also caught failures from authenticating. An expired OAuth login (u2m.InvalidRefreshTokenError) is now UNAUTHENTICATED, and a failure that may pass on retry (network, timeout, 429, 5xx, as classifyValidationFailure reports it) goes through authError's transient fallback. Other setup errors stay INVALID_ARGS/PERMANENT. - [nit] Drop the config:validate 403 unit-test row that duplicates acceptance/air/run-permission-denied. Co-authored-by: Isaac --- cmd/air/run.go | 17 ++++++++++++----- cmd/air/run_test.go | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/cmd/air/run.go b/cmd/air/run.go index a846dbf2d7d..383e3ddd862 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -13,6 +13,7 @@ import ( "unicode/utf8" "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/libs/auth/u2m" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/flags" @@ -90,15 +91,21 @@ func (e *workspaceClientError) Unwrap() error { return e.err } // renderWorkspaceClientError renders a failure to build the workspace client. // Bundle diagnostics that were already printed pass through, as in air get. A -// missing profile or rejected credentials go through authError. Any other setup -// failure (an unknown or account-only profile, for example) is a config mistake -// that a retry can't fix, so it is INVALID_ARGS rather than authError's -// retryable fallback. Text mode keeps the original error. +// missing profile, rejected credentials, or a failure that may pass on retry +// (network, timeout, 429, 5xx) go through authError, and an expired login is +// UNAUTHENTICATED. Any other setup failure (an unknown or account-only profile, +// for example) is a config mistake that a retry can't fix, so it is +// INVALID_ARGS rather than authError's retryable fallback. Text mode keeps the +// original error. func renderWorkspaceClientError(ctx context.Context, cmd *cobra.Command, err error) error { + _, transient := classifyValidationFailure(err) + _, expiredLogin := errors.AsType[*u2m.InvalidRefreshTokenError](err) switch { case errors.Is(err, root.ErrAlreadyPrinted), root.OutputType(cmd) != flags.OutputJSON: return err - case errors.Is(err, config.ErrCannotConfigureDefault), errors.Is(err, apierr.ErrUnauthenticated), errors.Is(err, apierr.ErrPermissionDenied): + case expiredLogin: + return renderError(ctx, cmd, "UNAUTHENTICATED", "PERMANENT", false, err) + case errors.Is(err, config.ErrCannotConfigureDefault), errors.Is(err, apierr.ErrUnauthenticated), errors.Is(err, apierr.ErrPermissionDenied), transient: return authError(ctx, cmd, err) } return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, err) diff --git a/cmd/air/run_test.go b/cmd/air/run_test.go index ccf74ab0787..485ce14d24f 100644 --- a/cmd/air/run_test.go +++ b/cmd/air/run_test.go @@ -3,18 +3,23 @@ package aircmd import ( "bytes" "encoding/json" + "errors" "net/http" "net/http/httptest" + "net/url" "strings" "sync/atomic" "testing" "time" "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/libs/auth" + "github.com/databricks/cli/libs/auth/u2m" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/flags" "github.com/databricks/cli/libs/testserver" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -269,8 +274,8 @@ func TestRunDryRunRetriesUnavailableResponseOnce(t *testing.T) { // Credentials rejected after the workspace client is built (by the current-user // lookup or by config:validate) must not be reported as INVALID_ARGS, or agents // edit a valid config instead of fixing their credentials. The current-user -// lookup fails only for bad credentials; a 403 from config:validate after it -// passed is a missing permission. +// lookup fails only for bad credentials. A 403 from config:validate after it +// passed is a missing permission (acceptance/air/run-permission-denied). func TestRunDryRunReportsRejectedCredentials(t *testing.T) { tests := []struct { name, method, path string @@ -281,7 +286,6 @@ func TestRunDryRunReportsRejectedCredentials(t *testing.T) { {"current user/401", "GET", "/api/2.0/preview/scim/v2/Me", http.StatusUnauthorized, "UNAUTHENTICATED", "UNAUTHENTICATED"}, {"current user/403", "GET", "/api/2.0/preview/scim/v2/Me", http.StatusForbidden, "PERMISSION_DENIED", "UNAUTHENTICATED"}, {"config validate/401", "POST", validateConfigPath, http.StatusUnauthorized, "UNAUTHENTICATED", "UNAUTHENTICATED"}, - {"config validate/403", "POST", validateConfigPath, http.StatusForbidden, "PERMISSION_DENIED", "PERMISSION_DENIED"}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { @@ -308,6 +312,35 @@ func TestRunDryRunReportsRejectedCredentials(t *testing.T) { } } +// Workspace-client setup failures that a retry can't fix must not be reported +// as retryable, while a transient failure while authenticating still is. +func TestRenderWorkspaceClientError(t *testing.T) { + _, expiredLogin := auth.RewriteAuthError(t.Context(), "https://host.test", "", "dev", &u2m.InvalidRefreshTokenError{}) + tests := []struct { + name string + err error + code string + retryable bool + }{ + {"expired login", expiredLogin, "UNAUTHENTICATED", false}, + {"network", &url.Error{Op: "Get", URL: "https://host.test", Err: errors.New("connection refused")}, "INTERNAL_ERROR", true}, + {"account-only profile", root.ErrAccountOnlyProfile{}, "INVALID_ARGS", false}, + {"unknown profile", errors.New("resolve: .databrickscfg has no dev profile configured"), "INVALID_ARGS", false}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + var buf bytes.Buffer + ctx := cmdio.InContext(t.Context(), cmdio.NewIO(t.Context(), flags.OutputJSON, nil, &buf, &buf, "", "")) + err := renderWorkspaceClientError(ctx, withOutput(&cobra.Command{}, flags.OutputJSON), tc.err) + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + var got errorEnvelope + require.NoError(t, json.Unmarshal(buf.Bytes(), &got)) + assert.Equal(t, tc.code, got.Error.Code) + assert.Equal(t, tc.retryable, got.Error.Retryable) + }) + } +} + func TestRunDryRunValidatesIdempotencyTokenAfterWorkspaceValidation(t *testing.T) { var requests []dryRunRequest srv := dryRunServer(t, validateConfigResponse{}, &requests) From 9fe1a88ab8114eb52ee809f489f93ede55ae88d4 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 19:27:32 +0000 Subject: [PATCH 12/14] Move the dry-run 401/403 envelope checks into acceptance tests Review of f73e331 (non-blocking, fixed in code per Vincent): - Cover the current-user 401/403 and config:validate 401/403 envelopes in acceptance/air/run-rejected-credentials, with a config that is actually valid, and drop the unit test that duplicated them. - Say in the doc comment that only a 401 or 403 from the current-user lookup goes through authError. Co-authored-by: Isaac --- acceptance/air/run-permission-denied/script | 2 - .../air/run-permission-denied/valid.yaml | 7 ---- .../me-401}/out.test.toml | 0 .../me-401/output.txt | 14 +++++++ .../run-rejected-credentials/me-401/script | 2 + .../run-rejected-credentials/me-401/test.toml | 9 ++++ .../me-403/out.test.toml | 1 + .../me-403/output.txt | 14 +++++++ .../run-rejected-credentials/me-403/script | 2 + .../run-rejected-credentials/me-403/test.toml | 9 ++++ .../air/run-rejected-credentials/valid.yaml | 5 +++ .../validate-401/out.test.toml | 1 + .../validate-401/output.txt | 14 +++++++ .../validate-401/script | 2 + .../validate-401/test.toml | 9 ++++ .../validate-403/out.test.toml | 1 + .../validate-403}/output.txt | 2 +- .../validate-403/script | 2 + .../validate-403}/test.toml | 0 cmd/air/run.go | 4 +- cmd/air/run_test.go | 41 ------------------- 21 files changed, 88 insertions(+), 53 deletions(-) delete mode 100644 acceptance/air/run-permission-denied/script delete mode 100644 acceptance/air/run-permission-denied/valid.yaml rename acceptance/air/{run-permission-denied => run-rejected-credentials/me-401}/out.test.toml (100%) create mode 100644 acceptance/air/run-rejected-credentials/me-401/output.txt create mode 100644 acceptance/air/run-rejected-credentials/me-401/script create mode 100644 acceptance/air/run-rejected-credentials/me-401/test.toml create mode 100644 acceptance/air/run-rejected-credentials/me-403/out.test.toml create mode 100644 acceptance/air/run-rejected-credentials/me-403/output.txt create mode 100644 acceptance/air/run-rejected-credentials/me-403/script create mode 100644 acceptance/air/run-rejected-credentials/me-403/test.toml create mode 100644 acceptance/air/run-rejected-credentials/valid.yaml create mode 100644 acceptance/air/run-rejected-credentials/validate-401/out.test.toml create mode 100644 acceptance/air/run-rejected-credentials/validate-401/output.txt create mode 100644 acceptance/air/run-rejected-credentials/validate-401/script create mode 100644 acceptance/air/run-rejected-credentials/validate-401/test.toml create mode 100644 acceptance/air/run-rejected-credentials/validate-403/out.test.toml rename acceptance/air/{run-permission-denied => run-rejected-credentials/validate-403}/output.txt (73%) create mode 100644 acceptance/air/run-rejected-credentials/validate-403/script rename acceptance/air/{run-permission-denied => run-rejected-credentials/validate-403}/test.toml (100%) diff --git a/acceptance/air/run-permission-denied/script b/acceptance/air/run-permission-denied/script deleted file mode 100644 index 5e6fd748fab..00000000000 --- a/acceptance/air/run-permission-denied/script +++ /dev/null @@ -1,2 +0,0 @@ -title "a 403 from config:validate is a missing permission (json)" -musterr trace $CLI air run -f valid.yaml --dry-run -o json diff --git a/acceptance/air/run-permission-denied/valid.yaml b/acceptance/air/run-permission-denied/valid.yaml deleted file mode 100644 index b717585c0bc..00000000000 --- a/acceptance/air/run-permission-denied/valid.yaml +++ /dev/null @@ -1,7 +0,0 @@ -experiment_name: smoke-test -command: python train.py -compute: - accelerator_type: GPU_8xH100 - num_accelerators: 1032 -env_variables: - 1INVALID: value diff --git a/acceptance/air/run-permission-denied/out.test.toml b/acceptance/air/run-rejected-credentials/me-401/out.test.toml similarity index 100% rename from acceptance/air/run-permission-denied/out.test.toml rename to acceptance/air/run-rejected-credentials/me-401/out.test.toml diff --git a/acceptance/air/run-rejected-credentials/me-401/output.txt b/acceptance/air/run-rejected-credentials/me-401/output.txt new file mode 100644 index 00000000000..76c78ed1367 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/me-401/output.txt @@ -0,0 +1,14 @@ + +=== a 401 from the current-user lookup is rejected credentials (json) + +>>> [CLI] air run -f [TESTROOT]/air/run-rejected-credentials/me-401/../valid.yaml --dry-run -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "UNAUTHENTICATED", + "kind": "PERMANENT", + "message": "authentication was not successful: failed to resolve current user: Invalid access token.", + "retryable": false + } +} diff --git a/acceptance/air/run-rejected-credentials/me-401/script b/acceptance/air/run-rejected-credentials/me-401/script new file mode 100644 index 00000000000..dba50d661da --- /dev/null +++ b/acceptance/air/run-rejected-credentials/me-401/script @@ -0,0 +1,2 @@ +title "a 401 from the current-user lookup is rejected credentials (json)" +musterr trace $CLI air run -f $TESTDIR/../valid.yaml --dry-run -o json diff --git a/acceptance/air/run-rejected-credentials/me-401/test.toml b/acceptance/air/run-rejected-credentials/me-401/test.toml new file mode 100644 index 00000000000..7dbe6bb14f9 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/me-401/test.toml @@ -0,0 +1,9 @@ +[[Server]] +Pattern = "GET /api/2.0/preview/scim/v2/Me" +Response.Body = ''' +{ + "error_code": "UNAUTHENTICATED", + "message": "Invalid access token." +} +''' +Response.StatusCode = 401 diff --git a/acceptance/air/run-rejected-credentials/me-403/out.test.toml b/acceptance/air/run-rejected-credentials/me-403/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/air/run-rejected-credentials/me-403/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/air/run-rejected-credentials/me-403/output.txt b/acceptance/air/run-rejected-credentials/me-403/output.txt new file mode 100644 index 00000000000..d653e7001b6 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/me-403/output.txt @@ -0,0 +1,14 @@ + +=== a 403 from the current-user lookup is rejected credentials (json) + +>>> [CLI] air run -f [TESTROOT]/air/run-rejected-credentials/me-403/../valid.yaml --dry-run -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "UNAUTHENTICATED", + "kind": "PERMANENT", + "message": "authentication was not successful: failed to resolve current user: Invalid access token.", + "retryable": false + } +} diff --git a/acceptance/air/run-rejected-credentials/me-403/script b/acceptance/air/run-rejected-credentials/me-403/script new file mode 100644 index 00000000000..f7437beaaa6 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/me-403/script @@ -0,0 +1,2 @@ +title "a 403 from the current-user lookup is rejected credentials (json)" +musterr trace $CLI air run -f $TESTDIR/../valid.yaml --dry-run -o json diff --git a/acceptance/air/run-rejected-credentials/me-403/test.toml b/acceptance/air/run-rejected-credentials/me-403/test.toml new file mode 100644 index 00000000000..1afa1d60c84 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/me-403/test.toml @@ -0,0 +1,9 @@ +[[Server]] +Pattern = "GET /api/2.0/preview/scim/v2/Me" +Response.Body = ''' +{ + "error_code": "PERMISSION_DENIED", + "message": "Invalid access token." +} +''' +Response.StatusCode = 403 diff --git a/acceptance/air/run-rejected-credentials/valid.yaml b/acceptance/air/run-rejected-credentials/valid.yaml new file mode 100644 index 00000000000..b82a321b051 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/valid.yaml @@ -0,0 +1,5 @@ +experiment_name: smoke-test +command: python train.py +compute: + accelerator_type: GPU_1xH100 + num_accelerators: 1 diff --git a/acceptance/air/run-rejected-credentials/validate-401/out.test.toml b/acceptance/air/run-rejected-credentials/validate-401/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/air/run-rejected-credentials/validate-401/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/air/run-rejected-credentials/validate-401/output.txt b/acceptance/air/run-rejected-credentials/validate-401/output.txt new file mode 100644 index 00000000000..c6c3b07999a --- /dev/null +++ b/acceptance/air/run-rejected-credentials/validate-401/output.txt @@ -0,0 +1,14 @@ + +=== a 401 from config:validate is rejected credentials (json) + +>>> [CLI] air run -f [TESTROOT]/air/run-rejected-credentials/validate-401/../valid.yaml --dry-run -o json +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "UNAUTHENTICATED", + "kind": "PERMANENT", + "message": "authentication was not successful: failed to validate config: Invalid access token.", + "retryable": false + } +} diff --git a/acceptance/air/run-rejected-credentials/validate-401/script b/acceptance/air/run-rejected-credentials/validate-401/script new file mode 100644 index 00000000000..bab16c56875 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/validate-401/script @@ -0,0 +1,2 @@ +title "a 401 from config:validate is rejected credentials (json)" +musterr trace $CLI air run -f $TESTDIR/../valid.yaml --dry-run -o json diff --git a/acceptance/air/run-rejected-credentials/validate-401/test.toml b/acceptance/air/run-rejected-credentials/validate-401/test.toml new file mode 100644 index 00000000000..fdc1b5f0e07 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/validate-401/test.toml @@ -0,0 +1,9 @@ +[[Server]] +Pattern = "POST /api/2.0/ai-training/config:validate" +Response.Body = ''' +{ + "error_code": "UNAUTHENTICATED", + "message": "Invalid access token." +} +''' +Response.StatusCode = 401 diff --git a/acceptance/air/run-rejected-credentials/validate-403/out.test.toml b/acceptance/air/run-rejected-credentials/validate-403/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/air/run-rejected-credentials/validate-403/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/air/run-permission-denied/output.txt b/acceptance/air/run-rejected-credentials/validate-403/output.txt similarity index 73% rename from acceptance/air/run-permission-denied/output.txt rename to acceptance/air/run-rejected-credentials/validate-403/output.txt index 9c82b131bff..0fdc0f12c4d 100644 --- a/acceptance/air/run-permission-denied/output.txt +++ b/acceptance/air/run-rejected-credentials/validate-403/output.txt @@ -1,7 +1,7 @@ === a 403 from config:validate is a missing permission (json) ->>> [CLI] air run -f valid.yaml --dry-run -o json +>>> [CLI] air run -f [TESTROOT]/air/run-rejected-credentials/validate-403/../valid.yaml --dry-run -o json { "v": 1, "ts": "[TIMESTAMP]", diff --git a/acceptance/air/run-rejected-credentials/validate-403/script b/acceptance/air/run-rejected-credentials/validate-403/script new file mode 100644 index 00000000000..9181f814995 --- /dev/null +++ b/acceptance/air/run-rejected-credentials/validate-403/script @@ -0,0 +1,2 @@ +title "a 403 from config:validate is a missing permission (json)" +musterr trace $CLI air run -f $TESTDIR/../valid.yaml --dry-run -o json diff --git a/acceptance/air/run-permission-denied/test.toml b/acceptance/air/run-rejected-credentials/validate-403/test.toml similarity index 100% rename from acceptance/air/run-permission-denied/test.toml rename to acceptance/air/run-rejected-credentials/validate-403/test.toml diff --git a/cmd/air/run.go b/cmd/air/run.go index 383e3ddd862..61904e65fb3 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -113,8 +113,8 @@ func renderWorkspaceClientError(ctx context.Context, cmd *cobra.Command, err err // renderDryRunCallerError renders a --dry-run failure that is the caller's to // fix, keeping credential and permission failures apart from config errors so -// agents don't edit a valid config. Current-user failures and a 401 from -// config:validate go through authError, as in air get. A 403 +// agents don't edit a valid config. A 401 or 403 from the current-user lookup +// and a 401 from config:validate go through authError, as in air get. A 403 // from config:validate is PERMISSION_DENIED: the same credentials just passed the // current-user lookup. Text mode keeps the original error. func renderDryRunCallerError(ctx context.Context, cmd *cobra.Command, err error) error { diff --git a/cmd/air/run_test.go b/cmd/air/run_test.go index 485ce14d24f..ca012b9c36e 100644 --- a/cmd/air/run_test.go +++ b/cmd/air/run_test.go @@ -271,47 +271,6 @@ func TestRunDryRunRetriesUnavailableResponseOnce(t *testing.T) { } } -// Credentials rejected after the workspace client is built (by the current-user -// lookup or by config:validate) must not be reported as INVALID_ARGS, or agents -// edit a valid config instead of fixing their credentials. The current-user -// lookup fails only for bad credentials. A 403 from config:validate after it -// passed is a missing permission (acceptance/air/run-permission-denied). -func TestRunDryRunReportsRejectedCredentials(t *testing.T) { - tests := []struct { - name, method, path string - status int - errorCode string - wantCode string - }{ - {"current user/401", "GET", "/api/2.0/preview/scim/v2/Me", http.StatusUnauthorized, "UNAUTHENTICATED", "UNAUTHENTICATED"}, - {"current user/403", "GET", "/api/2.0/preview/scim/v2/Me", http.StatusForbidden, "PERMISSION_DENIED", "UNAUTHENTICATED"}, - {"config validate/401", "POST", validateConfigPath, http.StatusUnauthorized, "UNAUTHENTICATED", "UNAUTHENTICATED"}, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - srv := testserver.New(t) - // Registered before AddDefaultHandlers: the router is first-wins. - srv.Handle(tc.method, tc.path, func(testserver.Request) any { - return testserver.Response{ - StatusCode: tc.status, - Body: map[string]string{"error_code": tc.errorCode, "message": "credentials rejected"}, - } - }) - testserver.AddDefaultHandlers(srv) - t.Cleanup(srv.Close) - - result := runDryRunCmd(t, dryRunValidationTimeout, flags.OutputJSON, srv.URL, minimalConfig, "--dry-run") - require.ErrorIs(t, result.err, root.ErrAlreadyPrinted) - var got errorEnvelope - require.NoError(t, json.Unmarshal([]byte(result.stdout), &got)) - assert.Equal(t, tc.wantCode, got.Error.Code) - assert.Equal(t, "PERMANENT", got.Error.Kind) - assert.False(t, got.Error.Retryable) - assert.Contains(t, got.Error.Message, "credentials rejected") - }) - } -} - // Workspace-client setup failures that a retry can't fix must not be reported // as retryable, while a transient failure while authenticating still is. func TestRenderWorkspaceClientError(t *testing.T) { From 7e074c76634785b4fbea63e8c92019cbac75dd37 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 19:35:51 +0000 Subject: [PATCH 13/14] Pin the JSON bundle-plus-key order; reword the workspaceClientError comment Review of 9fe1a88 (non-blocking): - The text-mode bundle + over-long key case sat under a "(json)" title. Give it its own title and add the -o json variant, which prints the diagnostics and then an INVALID_ARGS envelope for the key. - Describe workspaceClientError as routing the error to renderWorkspaceClientError, not as deciding the order. Co-authored-by: Isaac --- acceptance/air/run/output.txt | 18 +++++++++++++++++- acceptance/air/run/script | 5 ++++- cmd/air/run.go | 3 ++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index ea5f6617170..6c79f8243b0 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -218,6 +218,12 @@ Tip: use the --debug flag to see more details and a trace of this error: Error: multiple targets are marked as default ([TARGETS]) +>>> [CLI] air run -f ../valid.yaml -o json +Error: multiple targets are marked as default ([TARGETS]) + + +=== bundle diagnostics come before the idempotency key error + >>> [CLI] air run -f ../valid.yaml --dry-run --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Error: multiple targets are marked as default ([TARGETS]) @@ -226,9 +232,19 @@ Error: idempotency token must be 64 characters or less, got 65 Tip: use the --debug flag to see more details and a trace of this error: databricks --debug air run … ->>> [CLI] air run -f ../valid.yaml -o json +>>> [CLI] air run -f ../valid.yaml --dry-run -o json --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Error: multiple targets are marked as default ([TARGETS]) +{ + "v": 1, + "ts": "[TIMESTAMP]", + "error": { + "code": "INVALID_ARGS", + "kind": "PERMANENT", + "message": "idempotency token must be 64 characters or less, got 65", + "retryable": false + } +} === unknown field suggests the near match diff --git a/acceptance/air/run/script b/acceptance/air/run/script index a1ba2b640f9..db03189e526 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -85,9 +85,12 @@ musterr trace env -u DATABRICKS_HOST -u DATABRICKS_TOKEN DATABRICKS_CONFIG_FILE= title "bundle diagnostics are reported once (json)" musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml --dry-run -o json -musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml --dry-run --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml -o json +title "bundle diagnostics come before the idempotency key error" +musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml --dry-run --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +musterr withdir broken-bundle trace $CLI air run -f ../valid.yaml --dry-run -o json --idempotency-key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + title "unknown field suggests the near match" errcode trace $CLI air run -f unknown-field.yaml --dry-run diff --git a/cmd/air/run.go b/cmd/air/run.go index 61904e65fb3..fef02f4b5a0 100644 --- a/cmd/air/run.go +++ b/cmd/air/run.go @@ -81,7 +81,8 @@ func validateDryRunInWorkspace(ctx context.Context, cmd *cobra.Command, args []s } // workspaceClientError marks a --dry-run failure to build the workspace client, -// so it is rendered after the local checks, in the same order as before. +// so RunE classifies it with renderWorkspaceClientError rather than as a +// validation error. type workspaceClientError struct { err error } From f227b4ea5a128ddea1235f19f661da6998955676 Mon Sep 17 00:00:00 2001 From: vinchenzo-db Date: Mon, 23 Nov 2026 19:45:12 +0000 Subject: [PATCH 14/14] Limit the changelog's rejected-credentials claim to --dry-run Review of 7e074c7 (non-blocking): without --dry-run, a PAT is only checked when the run is submitted, so rejected credentials are reported as UNAUTHENTICATED only during --dry-run. Co-authored-by: Isaac --- .nextchanges/air/run-json-validation-errors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/air/run-json-validation-errors.md b/.nextchanges/air/run-json-validation-errors.md index 2084b126d5a..3d1404e7040 100644 --- a/.nextchanges/air/run-json-validation-errors.md +++ b/.nextchanges/air/run-json-validation-errors.md @@ -1 +1 @@ -* `air run -o json` now reports config-validation errors as a JSON error envelope instead of plain text. Rejected credentials report `UNAUTHENTICATED`, other workspace client setup errors (such as an unknown profile) report `INVALID_ARGS`, and with `--dry-run` a missing permission on `config:validate` reports the new `PERMISSION_DENIED` code. ([#7864](https://github.com/databricks/cli/pull/7864)) +* `air run -o json` now reports config-validation errors as a JSON error envelope instead of plain text. A missing profile reports `UNAUTHENTICATED`, other workspace client setup errors (such as an unknown profile) report `INVALID_ARGS`, and with `--dry-run` rejected credentials report `UNAUTHENTICATED` and a missing permission on `config:validate` reports the new `PERMISSION_DENIED` code. ([#7864](https://github.com/databricks/cli/pull/7864))