Skip to content

Auto-detect the client/host role from an existing API key - #463

Closed
sammy-vastai wants to merge 1 commit into
cln-3582-client-host-clifrom
cln-3582-role-auto-detect
Closed

Auto-detect the client/host role from an existing API key#463
sammy-vastai wants to merge 1 commit into
cln-3582-client-host-clifrom
cln-3582-role-auto-detect

Conversation

@sammy-vastai

Copy link
Copy Markdown
Contributor

Summary

Stacked on #462. Adds automatic role detection so most users never have to run vastai set role manually — the key insight is that for most existing users an API key already exists, so there's no "enter your key" moment to hook into; detection just needs to use it as soon as possible.

  • ensure_host_role_detected() (vastai/cli/util.py): one best-effort API call (GET /machines?owner=me) that caches the role permanently either way ('client' as much as 'host') on success; a failed check (network/auth error) leaves it undetected for retry rather than guessing.
  • Wired into three trigger points that already have a valid key in hand:
    • get_client() (vastai/cli/utils.py) — fires lazily on a user's next real command. Deliberately does not run on --help/tab completion, which never build a client.
    • vastai set api-key — detects immediately using the newly-saved key and announces the result.
    • The bash installer (scripts/install.sh) — if a key already exists on disk (a pre-existing user re-running the installer to upgrade), fires a single detached (nohup ... & disown) background call so it can't add install latency.
  • pip installs still can't be reached at install time (no post-install hook exists in the wheel/PEP 517 standard) — they fall back to the get_client() lazy path, an accepted, unavoidable gap.
  • Once resolved, the role is sticky — vastai set role host|client remains the deliberate override for e.g. a client account that starts hosting later.

Test plan

  • poetry run pytest tests/cli — 407 passed
  • bash tests/installer/run_tests.sh — 18 passed (3 new scenarios covering the installer's background role-detection)
  • Manually verified set api-key auto-detection and announcement against the real vastai binary

CLN-3582

@sammy-vastai

Copy link
Copy Markdown
Contributor Author

Superseded by #484, which combines this and #462 into a single branch (rebased onto current master, conflicts resolved, tests passing).

sammy-vastai added a commit that referenced this pull request Jul 31, 2026
warm_role_cache() reads the API key from $XDG_CONFIG_HOME/vastai; run_install
only overrode HOME, so an ambient XDG_CONFIG_HOME in the CI runner's own
environment leaked through and pointed the check outside the sandbox. This is
why warm_role_cache_fires_when_key_exists failed in every real GitHub Actions
run (including all 3 runs of the original #463) despite passing locally every
time — confirmed by reproducing the exact CI failure locally by injecting an
ambient XDG_CONFIG_HOME.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant