Skip to content

[CLI] Add hf spaces ssh#4241

Merged
hanouticelina merged 9 commits into
mainfrom
cli/spaces-ssh
May 21, 2026
Merged

[CLI] Add hf spaces ssh#4241
hanouticelina merged 9 commits into
mainfrom
cli/spaces-ssh

Conversation

@gary149

@gary149 gary149 commented May 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Add hf spaces ssh <space_id> to SSH into a Space's Dev Mode container.

Features:

  • Detects whether Dev Mode is enabled via SpaceRuntime.dev_mode (new field, from API devMode).
  • If Dev Mode is off, prompts the user to enable it interactively (default to True), then waits until it's ready before connecting.
  • --auto flag skips the prompt and enables Dev Mode automatically.
  • --dry-run flag prints the ssh command instead of running it.
  • -i / --identity-file to forward a specific SSH key.

Other changes:

  • Updated hf spaces dev-mode output to mention hf spaces ssh.
  • Added dev_mode: bool attribute to SpaceRuntime.

Example:

✗ hf spaces ssh huggingface/tmp-dev-mode-test                     
Dev Mode is disabled on 'huggingface/tmp-dev-mode-test'. Enable it now? [Y/n]: 
app starting...
Dev mode ready!
Running `ssh huggingface-tmp-dev-mode-test@ssh.hf.space`
root@r-huggingface-tmp-dev-mode-test-2msg752d-86560-vhqp4:/app# ls

Usage:

# Basic usage
$ hf spaces ssh username/my-space

# Print the command without running it
$ hf spaces ssh username/my-space --dry-run
ssh username-my-space@ssh.hf.space

# Auto-enable dev mode + SSH
$ hf spaces ssh username/my-space --auto

# With a specific SSH key
$ hf spaces ssh username/my-space -i ~/.ssh/id_ed25519

🤖 Generated with Claude Code


Note

Low Risk
Low risk: primarily adds a new CLI entrypoint plus small refactoring and a new SpaceRuntime.dev_mode field; behavior changes are limited to Dev Mode workflows and interactive confirmations.

Overview
Adds hf spaces ssh to open an SSH session into a Space’s Dev Mode container, supporting --auto (enable Dev Mode without prompting), --dry-run (print the ssh command), and -i/--identity-file (forward an SSH key).

Extends SpaceRuntime with a dev_mode flag sourced from the API (devMode), refactors hf spaces dev-mode to reuse a shared “wait until Dev Mode is ready” helper, and updates CLI/docs reference pages to document the new SSH workflow. Also generalizes out.confirm() so commands can customize the “skip confirmation” flag text (used to point users to --auto for this command).

Reviewed by Cursor Bugbot for commit db5beec. Bugbot is set up for automated code reviews on this repo. Configure here.

@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

- Fix dev mode detection: use `SpaceRuntime.dev_mode` (from API `devMode`
  field) instead of checking `subdomain` presence.
- Rename `--print` to `--dry-run` (follows gcloud/git/make convention).
- Add `--auto` flag to enable Dev Mode without prompting.
- If dev mode is off, prompt user to enable it (interactive confirm).
- Extract `_wait_for_dev_mode` helper shared by `dev-mode` and `ssh`.
- Remove unnecessary `FileNotFoundError` catch on `ssh` subprocess.
- Mention `hf spaces ssh` in `dev-mode` output.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Wauplin Wauplin changed the title [CLI] Add hf spaces ssh <space_id> [CLI] Add hf spaces ssh May 21, 2026
cmd = ["ssh"]
if identity_file is not None:
cmd += ["-i", str(identity_file)]
cmd.append(f"{info.subdomain}@ssh.hf.space")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SSH attempted without waiting for space to be running

Medium Severity

When dev mode is already enabled (info.runtime.dev_mode is True) but the space is still in an intermediate stage (BUILDING, APP_STARTING, etc.), the code skips the _wait_for_dev_mode call and immediately attempts SSH. This happens if a user enables dev mode through the web UI or another CLI invocation and then quickly runs hf spaces ssh before the container is ready. The SSH connection will fail because the container isn't accepting connections yet. The wait logic only executes in the branch where dev mode was just enabled by this command.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9e49757. Configure here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it's fine

Comment thread src/huggingface_hub/cli/spaces.py

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default mode and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c32ba09. Configure here.

Comment thread src/huggingface_hub/cli/spaces.py Outdated
@Wauplin

Wauplin commented May 21, 2026

Copy link
Copy Markdown
Collaborator

Tested and works well on https://huggingface.co/spaces/huggingface/tmp-dev-mode-test

✗ hf spaces ssh huggingface/tmp-dev-mode-test                     
Dev Mode is disabled on 'huggingface/tmp-dev-mode-test'. Enable it now? [Y/n]: 
app starting...
Dev mode ready!
Running `ssh huggingface-tmp-dev-mode-test@ssh.hf.space`
root@r-huggingface-tmp-dev-mode-test-2msg752d-86560-vhqp4:/app# ls
✗ hf spaces ssh huggingface/tmp-dev-mode-test
Running `ssh huggingface-tmp-dev-mode-test@ssh.hf.space`
root@r-huggingface-tmp-dev-mode-test-2msg752d-86560-vhqp4:/app# pwd
/app
root@r-huggingface-tmp-dev-mode-test-2msg752d-86560-vhqp4:/app# 
exit
Connection to ssh.hf.space closed.

@Wauplin Wauplin marked this pull request as ready for review May 21, 2026 12:30
@Wauplin Wauplin requested a review from hanouticelina May 21, 2026 12:30

@hanouticelina hanouticelina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

@hanouticelina hanouticelina merged commit 20eece8 into main May 21, 2026
21 checks passed
@hanouticelina hanouticelina deleted the cli/spaces-ssh branch May 21, 2026 13:49
@huggingface-hub-bot

Copy link
Copy Markdown
Contributor

This PR has been shipped as part of the v1.17.0 release.

davanstrien added a commit to huggingface/hub-docs that referenced this pull request Jun 4, 2026
Add a CLI shortcut to the Dev Mode SSH section: `hf spaces ssh` resolves the
Space subdomain and opens the session for you, with `--dry-run` and `-i`
examples plus an `--auto` note. Complements the existing manual
`ssh <subdomain>@ssh.hf.space` instructions; the existing "register your SSH
key" line covers the prerequisite for both paths.

Added in huggingface_hub v1.17.0 (huggingface/huggingface_hub#4241).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Wauplin pushed a commit to huggingface/hub-docs that referenced this pull request Jun 4, 2026
Add a CLI shortcut to the Dev Mode SSH section: `hf spaces ssh` resolves the
Space subdomain and opens the session for you, with `--dry-run` and `-i`
examples plus an `--auto` note. Complements the existing manual
`ssh <subdomain>@ssh.hf.space` instructions; the existing "register your SSH
key" line covers the prerequisite for both paths.

Added in huggingface_hub v1.17.0 (huggingface/huggingface_hub#4241).

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

3 participants