From d13de92e61bec82233a956068fcd4e7c43a6ffd7 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Wed, 11 Nov 2026 13:52:56 +0000 Subject: [PATCH] aitools: bound best-effort telemetry auth MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `aitools install` resolved auth for telemetry in PreRunE, before doing any work, so an unreachable workspace blocked the install for as long as the SDK retried. It now resolves auth in the background while the install runs and waits for it at most 3 s after the command started. Nothing is resolved when `DATABRICKS_CLI_DISABLE_TELEMETRY` is set (new `telemetry.Disabled`), and a run that logged no event does not wait. ```diff aitools install - PreRunE: tryConfigureAuth - MustAnyClient(cmd) # blocks while the SDK retries host metadata - RunE: install skills + PreRunE: startTelemetryAuth # returns at once + if telemetry.Disabled → skip + go MustAnyClient(shadow cmd) # shares cmd's flags (--profile), not its context + # SkipTokenRefresh: never refreshes an OAuth token + RunE wrapper + runE: install skills # runs while auth resolves + pending.apply(cmd) + no event logged → return + wait until start + 3 s, at most + succeeded → SetConfigUsed(cmd ctx) + failed or not finished → no config, no upload root: telemetry.Upload ``` The background resolution never refreshes an OAuth token. The process may exit before it finishes, and the server rotates the single-use refresh token, so an abandoned refresh could lose the new one and log the user out. The new `root.SkipTokenRefresh` makes `MustWorkspaceClient` and `MustAccountClient` use `auth.CredentialsWithoutRefresh`, whose databricks-cli strategy passes the new `u2m.WithoutRefresh`: the cached token is used only while it is valid, and an expired one fails auth, so nothing is uploaded. The shadow command logs through `debugOnlyHandler`, so a background warning such as `[hostmetadata] failed to fetch host metadata ...` shows only with `--debug` and does not interrupt the install's output or prompts. This relies on root's host-metadata resolver logging through the command's logger (#7390). A panic in the background resolution is recovered and logged at debug level; the install does not fail. Changelog fragment: `.nextchanges/cli/aitools-install-telemetry-auth.md`. Co-authored-by: Isaac --- .../cli/aitools-install-telemetry-auth.md | 1 + .../home/.claude/.gitkeep | 0 .../install-host-metadata/out.test.toml | 1 + .../aitools/install-host-metadata/output.txt | 10 + .../cmd/aitools/install-host-metadata/script | 17 ++ .../aitools/install-host-metadata/test.toml | 36 +++ .../telemetry/install/home/.claude/.gitkeep | 0 .../aitools/telemetry/install/out.test.toml | 1 + .../cmd/aitools/telemetry/install/output.txt | 35 +++ .../cmd/aitools/telemetry/install/script | 42 ++++ .../cmd/aitools/telemetry/install/test.toml | 29 +++ cmd/aitools/install.go | 8 +- cmd/aitools/legacy_skills.go | 6 +- cmd/aitools/legacy_skills_test.go | 25 ++ cmd/aitools/telemetry.go | 150 +++++++++++- cmd/aitools/telemetry_auth_test.go | 221 ++++++++++++++++++ cmd/root/auth.go | 6 + cmd/root/auth_options.go | 21 ++ cmd/root/auth_options_test.go | 8 + libs/auth/credentials.go | 35 +++ libs/auth/credentials_test.go | 48 ++++ libs/auth/u2m/error.go | 4 + libs/auth/u2m/persistent_auth.go | 21 +- libs/auth/u2m/persistent_auth_test.go | 61 +++++ libs/telemetry/logger.go | 11 +- 25 files changed, 781 insertions(+), 16 deletions(-) create mode 100644 .nextchanges/cli/aitools-install-telemetry-auth.md create mode 100644 acceptance/cmd/aitools/install-host-metadata/home/.claude/.gitkeep create mode 100644 acceptance/cmd/aitools/install-host-metadata/out.test.toml create mode 100644 acceptance/cmd/aitools/install-host-metadata/output.txt create mode 100644 acceptance/cmd/aitools/install-host-metadata/script create mode 100644 acceptance/cmd/aitools/install-host-metadata/test.toml create mode 100644 acceptance/cmd/aitools/telemetry/install/home/.claude/.gitkeep create mode 100644 acceptance/cmd/aitools/telemetry/install/out.test.toml create mode 100644 acceptance/cmd/aitools/telemetry/install/output.txt create mode 100644 acceptance/cmd/aitools/telemetry/install/script create mode 100644 acceptance/cmd/aitools/telemetry/install/test.toml create mode 100644 cmd/aitools/telemetry_auth_test.go diff --git a/.nextchanges/cli/aitools-install-telemetry-auth.md b/.nextchanges/cli/aitools-install-telemetry-auth.md new file mode 100644 index 00000000000..6917342d9f7 --- /dev/null +++ b/.nextchanges/cli/aitools-install-telemetry-auth.md @@ -0,0 +1 @@ +* Fix `databricks aitools install` waiting a long time on an unreachable or slow workspace while it resolves authentication for telemetry; that wait is now limited to a few seconds. ([#7468](https://github.com/databricks/cli/pull/7468)) diff --git a/acceptance/cmd/aitools/install-host-metadata/home/.claude/.gitkeep b/acceptance/cmd/aitools/install-host-metadata/home/.claude/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/acceptance/cmd/aitools/install-host-metadata/out.test.toml b/acceptance/cmd/aitools/install-host-metadata/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/install-host-metadata/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/install-host-metadata/output.txt b/acceptance/cmd/aitools/install-host-metadata/output.txt new file mode 100644 index 00000000000..6bf0804fd1e --- /dev/null +++ b/acceptance/cmd/aitools/install-host-metadata/output.txt @@ -0,0 +1,10 @@ + +=== workspace unreachable: the background auth resolution prints nothing + +>>> [CLI] aitools install --skills-only --scope=global --agents=claude-code +Installing Databricks skills for Claude Code... +Using skills version test-ref +Fetching skills manifest... +Installed 1 skill. + +=== --debug still shows the hostmetadata line, at debug level diff --git a/acceptance/cmd/aitools/install-host-metadata/script b/acceptance/cmd/aitools/install-host-metadata/script new file mode 100644 index 00000000000..55a631f1722 --- /dev/null +++ b/acceptance/cmd/aitools/install-host-metadata/script @@ -0,0 +1,17 @@ +# Isolate HOME so the real machine's agents and skills are not seen. The +# committed home/.claude is what agent detection finds. +sethome home + +# The workspace host does not resolve, so the background telemetry auth +# resolution fails to fetch host metadata and logs hostmetadata's warning. +# The skills still come from the mock server (DATABRICKS_SKILLS_BASE_URL). +export DATABRICKS_HOST=https://unreachable.test + +title "workspace unreachable: the background auth resolution prints nothing" +trace $CLI aitools install --skills-only --scope=global --agents=claude-code + +title "--debug still shows the hostmetadata line, at debug level" +# A fresh cache, so the first run's negative-cache entry does not skip the fetch. +export DATABRICKS_CACHE_DIR="$PWD/debug-cache" +trace $CLI aitools install --skills-only --scope=global --agents=claude-code --debug &> LOG.debug +contains.py 'Debug: [hostmetadata] failed to fetch host metadata for https://unreachable.test' '!Warn: [hostmetadata]' < LOG.debug > /dev/null diff --git a/acceptance/cmd/aitools/install-host-metadata/test.toml b/acceptance/cmd/aitools/install-host-metadata/test.toml new file mode 100644 index 00000000000..ddb4180d056 --- /dev/null +++ b/acceptance/cmd/aitools/install-host-metadata/test.toml @@ -0,0 +1,36 @@ +# Mock server replaces raw.githubusercontent.com for the skills manifest and files. +Env.DATABRICKS_SKILLS_BASE_URL = "$DATABRICKS_HOST" +Env.DATABRICKS_SKILLS_REF = "test-ref" +# The case needs the telemetry auth resolution to run, whatever the shell sets. +Env.DATABRICKS_CLI_DISABLE_TELEMETRY = "" + +Ignore = [ + "home", + "debug-cache", +] + +[[Server]] +Pattern = "GET /test-ref/manifest.json" +Response.Body = ''' +{ + "version": "2", + "updated_at": "2026-01-01T00:00:00Z", + "skills": { + "test-stable": { + "version": "1.0.0", + "description": "Stable test skill", + "files": ["SKILL.md"], + "repo_dir": "skills" + } + } +} +''' + +[[Server]] +Pattern = "GET /test-ref/skills/test-stable/SKILL.md" +Response.Body = '''--- +name: test-stable +--- + +# Test stable skill +''' diff --git a/acceptance/cmd/aitools/telemetry/install/home/.claude/.gitkeep b/acceptance/cmd/aitools/telemetry/install/home/.claude/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/acceptance/cmd/aitools/telemetry/install/out.test.toml b/acceptance/cmd/aitools/telemetry/install/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/telemetry/install/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/telemetry/install/output.txt b/acceptance/cmd/aitools/telemetry/install/output.txt new file mode 100644 index 00000000000..2aea215a26c --- /dev/null +++ b/acceptance/cmd/aitools/telemetry/install/output.txt @@ -0,0 +1,35 @@ + +=== install resolves auth in the background: its install event is uploaded + +>>> [CLI] aitools install --skills-only --scope=global --agents=claude-code +Installing Databricks skills for Claude Code... +Using skills version test-ref +Fetching skills manifest... +Installed 1 skill. + +>>> print_telemetry +{"command":"aitools_install","exit_code":0,"aitools_install_event":{"agents":["CLAUDE_CODE"],"scope":"GLOBAL","error_category":"TYPE_UNSPECIFIED"}} + +=== databricks-cli auth with a valid cached token: the event is uploaded + +>>> [CLI] aitools install --skills-only --scope=global --agents=claude-code --profile test-profile +Installing Databricks skills for Claude Code... +Using skills version test-ref +Fetching skills manifest... +Installed 1 skill. + +>>> print_telemetry +{"command":"aitools_install","exit_code":0,"aitools_install_event":{"agents":["CLAUDE_CODE"],"scope":"GLOBAL","error_category":"TYPE_UNSPECIFIED"}} + +=== databricks-cli auth with an expired cached token: no refresh, nothing is uploaded + +>>> [CLI] aitools install --skills-only --scope=global --agents=claude-code --profile test-profile +Installing Databricks skills for Claude Code... +Using skills version test-ref +Fetching skills manifest... +Installed 1 skill. + +>>> print_requests.py //oidc/v1/token //telemetry-ext + +>>> gron.py [TEST_TMP_DIR]/home/.databricks/token-cache.json +json.tokens.test-profile.access_token = "expired-access-token"; diff --git a/acceptance/cmd/aitools/telemetry/install/script b/acceptance/cmd/aitools/telemetry/install/script new file mode 100644 index 00000000000..25e724968df --- /dev/null +++ b/acceptance/cmd/aitools/telemetry/install/script @@ -0,0 +1,42 @@ +# Isolate HOME so the real machine's agents and skills are not seen. The +# committed home/.claude is what agent detection finds. +sethome home + +title "install resolves auth in the background: its install event is uploaded" +trace $CLI aitools install --skills-only --scope=global --agents=claude-code +trace print_telemetry + +# A databricks-cli profile whose token comes from the CLI's token cache. +cat > "$HOME/.databrickscfg" < "$HOME/.databricks/token-cache.json" <