Repository navigation
aitools: skip auto-install when the CLI runs as root in another user's home - #7828
Conversation
…s home ## Changes The automatic installation of Databricks AI tools now skips a run where the CLI is root in a home directory that another user owns. - `libs/aitools/autoinstall`: new `RootInUserHome(ctx)`. On Unix it returns true when the effective UID is 0 and the home directory (from `env.UserHomeDir`) belongs to a non-root UID. If the home can't be stat'ed, it returns false. On Windows it always returns false, because an elevated process keeps the user's own profile. - `Decide` gets a new input, `RootInUserHome`, and a new reason, `root_in_user_home`. The check runs right after the development-build check and before anything that reads the state. The reason is not due, so it doesn't settle the version. - `cmd/aitools`: the auto-install hook passes `RootInUserHome` into `Decide` and adds the new reason to the state-free skips. Such a run returns before it takes the lock, reads or writes the state, looks up the gate, installs anything or sends telemetry. ```diff Decide(in) skipped command / on runtime / cache disabled / development build + root in a user's home -> root_in_user_home (no lock, no state, no telemetry) retries exhausted / already ran / CI / opt-out / gate / holdout / run ``` ## Why `sudo databricks ...` keeps `HOME` pointing at the invoking user's home by default on macOS, and on Linux with `sudo -E`. The auto-install hook then ran as root in that home. It created a root-owned `~/.databricks` (the state and lock files) and could install the agent skills as root. After that, `databricks auth login`, `databricks aitools install` and `databricks aitools auto-install disable` failed for the user, because they could no longer write to that directory. Skipping the run without touching state leaves the home as it was, so the user's next normal run installs as usual. Root in its own home (for example `sudo -H`, or a root login) still installs as before. ## Tests - `TestRootInUserHome` (Unix): returns false for a non-root UID, for root in a root-owned home, for a home that doesn't exist and for an empty home. `TestRootInUserHomeOwnedByAnotherUser` returns true for root in a directory the test user owns. It is skipped when the tests themselves run as root. - `TestDecide`: the new reason, and its precedence over the persisted state and over CI. - `TestReasonDue`: the new reason is not due. - `TestAutoInstallSkipsRootInAUserHome`: with the check forced on, the hook creates no `~/.databricks`, installs no skills, does no gate lookup and buffers no telemetry. With the check off, the same setup installs and records the state. Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
simonfaltum
left a comment
There was a problem hiding this comment.
🤖 Automated review from simonfaltum's review agent (pr-review skill) at a06d719. Each finding was verified against the code; simonfaltum may follow up.
No blocking issues found, approving. 5 non-blocking comment(s) and 1 question(s) below.
This PR skips the automatic AI tools install when the CLI runs as root in a home directory another user owns. It does so with a new not-due root_in_user_home reason that returns before any lock, state, gate lookup or telemetry. The core logic is correct and the placement in Decide is right; tests pass and CI is green. One gap: agent config dirs set through env vars like CLAUDE_CONFIG_DIR bypass the HOME check under sudo -E -H. The other comments are test and debug-log nits, plus a non-blocking scope question about other root-owned writers under sudo.
Questions
- Under
sudowithHOMEkept, other code that runs on every command still creates root-owned directories in the user's home. Examples:libs/cache, used by the version-check notice and host metadata underos.UserCacheDir(), and the agent hint throttle markers. They don't break~/.databricks, but the user's later runs of that version fall back to a no-op cache. ShouldRootInUserHomebecome a shared helper that these writers also check, or is this PR meant to cover auto-install only, with the rest left for a follow-up? (non-blocking)
Comments outside the changed lines
- [nit]
cmd/aitools/autoinstall_run.go:117: State-free skips return without a log line because "these are most runs", butroot_in_user_homeis rare. A user who always runs the CLI under sudo and wonders why the tools never install sees nothing with--debug. Logauto-install: skipped: root_in_user_homeat debug level for this reason, for example by checking it before the silentstateFreeSkipsreturn.
Log the root_in_user_home skip at debug level, note in the doc comment that agent directories set through environment variables are out of scope, skip the root-owned home test where / is not owned by UID 0, make the Decide precedence case start from a state that skips on its own, and turn the test closure into a named helper. Co-authored-by: Isaac <no-reply@databricks.com>
Changes
The automatic installation of Databricks AI tools now skips a run where the CLI is root in a home directory that another user owns.
libs/aitools/autoinstall: newRootInUserHome(ctx). On Unix it returns true when the effective UID is 0 and the home directory (fromenv.UserHomeDir) belongs to a non-root UID. If the home can't be stat'ed, it returns false. On Windows it always returns false, because an elevated process keeps the user's own profile.Decidegets a new input,RootInUserHome, and a new reason,root_in_user_home. The check runs right after the development-build check and before anything that reads the state. The reason is not due, so it doesn't settle the version.cmd/aitools: the auto-install hook passesRootInUserHomeintoDecideand adds the new reason to the state-free skips. Such a run returns before it takes the lock, reads or writes the state, looks up the gate, installs anything or sends telemetry.Decide(in) skipped command / on runtime / cache disabled / development build + root in a user's home -> root_in_user_home (no lock, no state, no telemetry) retries exhausted / already ran / CI / opt-out / gate / holdout / runWhy
sudo databricks ...keepsHOMEpointing at the invoking user's home by default on macOS, and on Linux withsudo -E. The auto-install hook then ran as root in that home. It created a root-owned~/.databricks(the state and lock files) and could install the agent skills as root. After that,databricks auth login,databricks aitools installanddatabricks aitools auto-install disablefailed for the user, because they could no longer write to that directory.Skipping the run without touching state leaves the home as it was, so the user's next normal run installs as usual. Root in its own home (for example
sudo -H, or a root login) still installs as before.Tests
TestRootInUserHome(Unix): returns false for a non-root UID, for root in a root-owned home, for a home that doesn't exist and for an empty home.TestRootInUserHomeOwnedByAnotherUserreturns true for root in a directory the test user owns. It is skipped when the tests themselves run as root.TestDecide: the new reason, and its precedence over the persisted state and over CI.TestReasonDue: the new reason is not due.TestAutoInstallSkipsRootInAUserHome: with the check forced on, the hook creates no~/.databricks, installs no skills, does no gate lookup and buffers no telemetry. With the check off, the same setup installs and records the state.