Skip to content

fix(cli): exit cleanly when no ChatGPT token is present - #33

Open
robotlearning123 wants to merge 1 commit into
mainfrom
fix/clean-token-startup-error
Open

fix(cli): exit cleanly when no ChatGPT token is present#33
robotlearning123 wants to merge 1 commit into
mainfrom
fix/clean-token-startup-error

Conversation

@robotlearning123

Copy link
Copy Markdown
Owner

Problem

The most common first-run state for a new user is installed + registered with the MCP client, but codex login / gpt2agent setup not run yet — so no token exists. The install flow does not require a token (it prints "No auth.json yet, run codex login"), so this state is normal after a one-line install + client restart.

In that state, the MCP client spawns gpt2agent run --stdio, which raised a bare RuntimeError out of BackendClient() inside build_server() and dumped a backend.py traceback into the client's MCP server logs:

Traceback (most recent call last):
  File ".../bin/gpt2agent", line 6, in <module>
    sys.exit(main())
  ...
  File ".../gpt2agent/backend.py", line 76, in _load_token_with_source
    raise RuntimeError(...)
RuntimeError: No ChatGPT token found — run `codex login` or `gpt2agent setup` (...)

The unauthenticated-HTTP-bind refusal path right below it already exits cleanly via SystemExit; the far more common missing-token path did not. This is a launch-quality inconsistency.

Fix

  • Introduce TokenNotFoundError(RuntimeError) in backend.py and raise it (instead of bare RuntimeError) from _load_token_with_source(). It's a RuntimeError subclass, so every existing except RuntimeError handler and pytest.raises(RuntimeError, ...) assertion keeps working unchanged.
  • Catch it in server.main() and exit 1 with the actionable one-line message via SystemExit — matching the HTTP-bind refusal path — instead of a traceback.

Before / after (real output)

# before:  HOME=<empty> gpt2agent run --stdio
Traceback (most recent call last): ... RuntimeError: No ChatGPT token found ...

# after:   HOME=<empty> gpt2agent run --stdio
No ChatGPT token found — run `codex login` or `gpt2agent setup` (checked .../.codex/auth.json and .../.gpt2agent/token.json)
$ echo $?
1

Server still boots normally when a token is present (verified: exit 0 on stdin close).

Tests

Adds tests/test_audit_2026_07_13_startup.py:

  • TokenNotFoundError is a RuntimeError subclass and is raised on missing token.
  • main() with run --stdio and no token raises SystemExit carrying the actionable message (no traceback).

Full suite: 325 passed, 9 skipped; ruff check clean.

🤖 Generated with Claude Code

The most common first-run state is "installed + registered with the MCP
client, but `codex login` / `gpt2agent setup` not run yet" — no token
exists. Spawning `gpt2agent run [--stdio]` in that state raised a bare
RuntimeError out of BackendClient() inside build_server(), dumping a
backend.py traceback into the MCP client's server logs.

The unauthenticated-HTTP-bind refusal already exits cleanly via
SystemExit; the far more common missing-token path did not. Make them
consistent: introduce TokenNotFoundError (a RuntimeError subclass, so
existing `except RuntimeError` handlers and tests keep working) and catch
it in main() to exit 1 with the actionable one-line message instead of a
traceback.

Verified: `HOME=<empty> gpt2agent run --stdio` now prints
"No ChatGPT token found — run `codex login` or `gpt2agent setup` (...)"
and exits 1 with no traceback; server still boots normally when a token
is present. Adds regression tests; full suite green (325 passed), ruff clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013CXqaBdbZMKH99Tv78AhGX
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@robotlearning123, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 54966cab-e083-42a3-b6d6-f540eaf47160

📥 Commits

Reviewing files that changed from the base of the PR and between 04e3d93 and f85f435.

📒 Files selected for processing (3)
  • gpt2agent/backend.py
  • gpt2agent/server.py
  • tests/test_audit_2026_07_13_startup.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/clean-token-startup-error

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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