Repository navigation
Auto-detect headless environments for device code OAuth - #1
Conversation
- Add is_headless_environment() function that detects SSH, Docker, Codespaces, VS Code Remote, tmux/screen, and missing DISPLAY - Automatically use device code flow when headless environment is detected - Add --browser flag to force browser-based login even in headless environments - Update --device-auth flag description to clarify auto-detection - Add comprehensive tests for environment detection - All existing device code tests pass Co-authored-by: uakbr <[email protected]>
- Add 10 integration tests covering all detection scenarios - Test SSH vars, Codespaces, VS Code Remote, Docker detection - Test Unix DISPLAY detection (with and without) - All 15 tests pass (5 unit + 10 integration) Co-authored-by: uakbr <[email protected]>
- Update authentication.md to explain auto-detection feature - Document the device code flow and when it's triggered - Explain --browser and --device-auth flags - Keep legacy workarounds as reference for older versions Co-authored-by: uakbr <[email protected]>
There was a problem hiding this comment.
Pull Request Overview
This PR implements automatic detection of headless/remote environments to seamlessly enable device code authentication for users running Codex in SSH sessions, Docker containers, VS Code Remote, GitHub Codespaces, and other remote environments. This resolves the previous requirement for manual workarounds when authenticating in headless environments.
- Added automatic headless environment detection with support for SSH, Docker, Codespaces, VS Code Remote, tmux/screen, and Unix systems without DISPLAY
- Introduced
--browserflag to force browser-based login when port forwarding is available - Updated CLI login logic to automatically use device code flow in detected headless environments
Reviewed Changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| codex-rs/login/src/headless_detection.rs | New module implementing environment detection logic for headless/remote scenarios |
| codex-rs/login/tests/suite/headless_detection_integration.rs | Comprehensive integration tests for all headless detection scenarios |
| codex-rs/login/src/lib.rs | Exports new is_headless_environment function for use by CLI |
| codex-rs/cli/src/main.rs | Integrates auto-detection logic into login command flow and adds --browser flag |
| codex-rs/login/Cargo.toml | Adds temp-env dev dependency for environment testing |
| codex-rs/login/tests/suite/mod.rs | Includes new headless detection integration tests |
| docs/authentication.md | Documents automatic headless detection, new flags, and legacy workarounds |
| codex-rs/Cargo.lock | Lock file update for new temp-env dependency |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let _guard = temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_ssh_client() { | ||
| let _guard = temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_ssh_tty() { | ||
| let _guard = temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_codespaces() { | ||
| let _guard = temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_vscode_remote_containers() { | ||
| let _guard = temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { |
There was a problem hiding this comment.
The variable _guard is assigned but never used. The temp_env::with_var function returns (), not a guard. Remove the let _guard = assignment and call the function directly.
| let _guard = temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_client() { | |
| let _guard = temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_tty() { | |
| let _guard = temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_codespaces() { | |
| let _guard = temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_vscode_remote_containers() { | |
| let _guard = temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { | |
| temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_client() { | |
| temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_tty() { | |
| temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_codespaces() { | |
| temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_vscode_remote_containers() { | |
| temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { |
| let _guard = temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_ssh_client() { | ||
| let _guard = temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_ssh_tty() { | ||
| let _guard = temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_codespaces() { | ||
| let _guard = temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_vscode_remote_containers() { | ||
| let _guard = temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { |
There was a problem hiding this comment.
The variable _guard is assigned but never used. The temp_env::with_var function returns (), not a guard. Remove the let _guard = assignment and call the function directly.
| let _guard = temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_client() { | |
| let _guard = temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_tty() { | |
| let _guard = temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_codespaces() { | |
| let _guard = temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_vscode_remote_containers() { | |
| let _guard = temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { | |
| temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_client() { | |
| temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_tty() { | |
| temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_codespaces() { | |
| temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_vscode_remote_containers() { | |
| temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { |
| let _guard = temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_ssh_client() { | ||
| let _guard = temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_ssh_tty() { | ||
| let _guard = temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_codespaces() { | ||
| let _guard = temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_vscode_remote_containers() { | ||
| let _guard = temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { |
There was a problem hiding this comment.
The variable _guard is assigned but never used. The temp_env::with_var function returns (), not a guard. Remove the let _guard = assignment and call the function directly.
| let _guard = temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_client() { | |
| let _guard = temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_tty() { | |
| let _guard = temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_codespaces() { | |
| let _guard = temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_vscode_remote_containers() { | |
| let _guard = temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { | |
| temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_client() { | |
| temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_tty() { | |
| temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_codespaces() { | |
| temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_vscode_remote_containers() { | |
| temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { |
| let _guard = temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_ssh_client() { | ||
| let _guard = temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_ssh_tty() { | ||
| let _guard = temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_codespaces() { | ||
| let _guard = temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | ||
| assert!(is_headless_environment()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_vscode_remote_containers() { | ||
| let _guard = temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { |
There was a problem hiding this comment.
The variable _guard is assigned but never used. The temp_env::with_var function returns (), not a guard. Remove the let _guard = assignment and call the function directly.
| let _guard = temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_client() { | |
| let _guard = temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_tty() { | |
| let _guard = temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_codespaces() { | |
| let _guard = temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_vscode_remote_containers() { | |
| let _guard = temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { | |
| temp_env::with_var("SSH_CONNECTION", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_client() { | |
| temp_env::with_var("SSH_CLIENT", Some("127.0.0.1"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_ssh_tty() { | |
| temp_env::with_var("SSH_TTY", Some("/dev/pts/0"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_codespaces() { | |
| temp_env::with_var("CODESPACE_NAME", Some("my-codespace"), || { | |
| assert!(is_headless_environment()); | |
| }); | |
| } | |
| #[test] | |
| fn detects_vscode_remote_containers() { | |
| temp_env::with_var("REMOTE_CONTAINERS", Some("true"), || { |
|
@copilot address the issues presented. |
- Remove duplicate documentation comment in main.rs (line 197) - Remove unused _guard variable assignments in headless_detection.rs tests - temp_env::with_var returns () not a guard, so assignment is unnecessary Co-authored-by: uakbr <[email protected]>
Fixes openai#2798
Users in SSH, Docker, Codespaces, and other remote environments cannot complete OAuth login because
localhost:1455callback is unreachable. Currently requires manual--device-authflag or copyingauth.jsonfiles.Changes
Automatic headless detection (
codex-rs/login/src/headless_detection.rs)SSH_CONNECTION/SSH_CLIENT/SSH_TTY), Codespaces, VS Code Remote, Docker (/.dockerenv,/proc/self/cgroup), tmux/screen without display, Unix withoutDISPLAYcodex loginroutes to device code flow automatically in detected environmentsCLI flags (
codex-rs/cli/src/main.rs)--browserforces localhost flow (for port forwarding scenarios)--device-authclarified as explicit overrideexperimental_issuerparameterTests
Documentation (
docs/authentication.md)Example
Code Quality
_guardvariables in unit testsNo breaking changes. Existing flags and workflows unchanged.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.