Skip to content

Auto-detect headless environments for device code OAuth - #1

Merged
uakbr merged 5 commits into
mainfrom
copilot/resolve-issue-2798
Nov 21, 2025
Merged

uakbr merged 5 commits into
mainfrom
copilot/resolve-issue-2798

Conversation

Copilot AI commented Nov 19, 2025 •

Copy link
Copy Markdown

Fixes openai#2798

Users in SSH, Docker, Codespaces, and other remote environments cannot complete OAuth login because localhost:1455 callback is unreachable. Currently requires manual --device-auth flag or copying auth.json files.

Changes

Automatic headless detection (codex-rs/login/src/headless_detection.rs)

  • Detects SSH (SSH_CONNECTION/SSH_CLIENT/SSH_TTY), Codespaces, VS Code Remote, Docker (/.dockerenv, /proc/self/cgroup), tmux/screen without display, Unix without DISPLAY
  • codex login routes to device code flow automatically in detected environments

CLI flags (codex-rs/cli/src/main.rs)

  • --browser forces localhost flow (for port forwarding scenarios)
  • --device-auth clarified as explicit override
  • Removed duplicate documentation comment for experimental_issuer parameter

Tests

  • 5 unit tests for detection logic (fixed unused variable warnings)
  • 10 integration tests covering all environment types
  • All existing login tests pass (19 total)

Documentation (docs/authentication.md)

  • Documents auto-detection behavior and override flags
  • Preserves legacy workarounds for older versions

Example

# Before: stuck in SSH session
$ codex login
Starting local login server on http://localhost:1455
# localhost unreachable

# After: auto-detects and switches
$ codex login
To authenticate:
  1. Open: https://auth.openai.com/codex/device
  2. Enter code: ABCD-EFGH

# With port forwarding
$ codex login --browser
Starting local login server on http://localhost:1455

Code Quality

  • ✅ Addressed PR review feedback (commit 41741d2)
  • ✅ Removed duplicate documentation comment
  • ✅ Fixed unused _guard variables in unit tests
  • ✅ All tests pass, clippy warnings resolved

No breaking changes. Existing flags and workflows unchanged.

Original prompt

openai#2798 (comment)

Resolve this issue. Think step by step. Create an outline. Then output the full solution.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 19, 2025 19:26
- 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]>
@uakbr
uakbr marked this pull request as ready for review November 19, 2025 19:37
Copilot AI review requested due to automatic review settings November 19, 2025 19:37
Copilot AI changed the title [WIP] Resolve issue related to error handling Auto-detect headless environments for device code OAuth Nov 19, 2025
Copilot AI requested a review from uakbr November 19, 2025 19:39

Copilot AI 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.

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 --browser flag 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.

Comment thread codex-rs/cli/src/main.rs Resolved
Comment on lines +68 to +96
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"), || {

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
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 uses AI. Check for mistakes.
Comment on lines +68 to +96
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"), || {

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
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 uses AI. Check for mistakes.
Comment on lines +68 to +96
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"), || {

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
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 uses AI. Check for mistakes.
Comment on lines +68 to +96
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"), || {

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
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 uses AI. Check for mistakes.
@uakbr

uakbr commented Nov 19, 2025

Copy link
Copy Markdown
Owner

@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]>
@uakbr
uakbr merged commit 13e7d23 into main Nov 21, 2025
@uakbr
uakbr deleted the copilot/resolve-issue-2798 branch November 21, 2025 09:00
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.

Support remote / headless OAuth sign-in

3 participants