Skip to content

[bug] Copilot ignores valid apps.json token: github.com:<client-id> key fails normalization #641

Closed

Description

@almai

Summary

normalize_github_host_key() strips a URL scheme and splits the host on /, but never on :. GitHub's apps.json keys carry a client-id suffix (github.com:Iv1.b507a08c87ecfe98), so the comparison against "github.com" fails, the function returns None, and select_preferred_token() drops the entry via ?.

// crates/jcode-base/src/auth/copilot.rs:596
let host = host.split('/').next().unwrap_or_default().trim();   // never splits on ':'
let host = host.to_ascii_lowercase();

if host == "github.com" || host == "api.github.com" || host.ends_with(".github.com") {
// crates/jcode-base/src/auth/copilot.rs:553
let normalized_host = normalize_github_host_key(host)?;   // valid token silently dropped

This matters more now that GitHub writes credentials to apps.json rather than hosts.json.

Why it's more than cosmetic

Per the load_github_token() doc comment, apps.json is source 6, ahead of the trusted OpenCode/pi auth.json entries at source 7. When apps.json is discarded, jcode continues down the chain and can pick up a stale token from ~/.pi/agent/auth.json.

In my case that token had been revoked — GitHub returns 401 Bad credentials on the exchange — and the resulting failure surfaces to the user as:

The requested model is not supported.  code: model_not_supported

A model-shaped error for an auth-shaped problem. jcode auth-test --provider copilot reported not_configured the whole time, while a perfectly valid token sat unread in apps.json.

This is made considerably harder to diagnose by #640, which emits a byte-identical model_not_supported message from an entirely unrelated cause. Fixing one changes nothing observable while the other is present.

Reproduce

  1. Authenticate GitHub Copilot such that credentials land in ~/.config/github-copilot/apps.json (current GitHub behaviour; hosts.json is no longer written)
  2. Ensure ~/.pi/agent/auth.json holds an old or revoked github-copilot token
  3. jcode --provider copilot → HTTP 400, and auth reports not_configured

Setting COPILOT_GITHUB_TOKEN to the exact value from apps.json makes all four auth-test probes pass, confirming the token was valid throughout and only the file parser rejected it.

Suggested fix

Split the host on : before comparing, mirroring the existing / handling:

let host = host.split('/').next().unwrap_or_default().trim();
let host = host.split(':').next().unwrap_or_default().trim();

github_host_priority() receives raw_host separately, so relative ordering between multiple entries is unaffected.

Two things worth addressing alongside it:

  • Don't fail silently. A discarded credential file should log at WARN, naming the file and the rejected key. Nothing in the logs indicated apps.json had been read and thrown away.
  • Surface auth failures as auth failures. A failed token exchange currently reaches the user as model_not_supported, which sends people debugging the wrong subsystem entirely.

A unit case for the key github.com:Iv1.b507a08c87ecfe98 would fit alongside the existing cases in copilot_auth_tests.rs.

Environment

jcode v0.61.1 (438789e), macOS, Copilot plus_monthly_subscriber_quota

Activity

  1. changed the title [-][bug] Copilot apps.json credentials silently discarded: host key `github.com:<client-id>` fails normalization, falls through to stale token[/-] [+][bug] Copilot ignores valid apps.json token: `github.com:<client-id>` key fails normalization[/+] on Jul 28, 2026
  2. almai commented on Jul 28, 2026

    @almai
    Author

    Workaround

    load_github_token() checks environment variables at steps 1–3, before any file source, so supplying the token directly bypasses the broken parser at step 6.

    Extract the valid token from apps.json:

    python3 -c "import json;d=json.load(open('$HOME/.config/github-copilot/apps.json'));print(next(v['oauth_token'] for v in d.values() if isinstance(v,dict) and v.get('oauth_token')))"

    Then export it:

    export COPILOT_GITHUB_TOKEN=ghu_…

    With that set, all four jcode auth-test --provider copilot probes pass.

    Two things that cost me time, worth flagging for anyone else hitting this:

    1. The daemon holds the environment, not your shell. jcode clients attach to a background server, and the server makes the API calls. Sourcing the export into an existing shell does nothing for an already-running daemon. It has to be:

      jcode server stop --force
      export COPILOT_GITHUB_TOKEN=ghu_…
      jcode --provider copilot

      Putting the export in a shell rc file is the durable version, since every daemon spawned from a login shell then inherits it.

    2. Deleting the stale ~/.pi/agent/auth.json entry does not help. apps.json is still rejected by the normalization bug regardless, so jcode ends up with no credential at all rather than a bad one. The environment variable is the only route that actually works while this is open.

  3. added
    bug
    priority: high
    triage: reproducible
    autonomous: clear
    on Jul 28, 2026
  4. 1jehuang commented on Jul 28, 2026

    @1jehuang
    Owner

    Confirmed and fixed in #643, using your suggested fix: the host is now split on : as well as /, so github.com:Iv1.<client-id> keys from apps.json normalize correctly instead of being silently dropped. Added a unit case for that exact key alongside the existing ones in copilot_auth_tests.rs.

    Your framing of why this is more than cosmetic was the useful part: the silent drop is what let a stale, revoked token from a lower-priority source win. #640 is fixed in the same PR, so the byte-identical model_not_supported confusion should be gone from both directions.

    The two follow-ups you raised are both fair and not addressed here:

    1. A discarded credential file should log at WARN, naming the file and the rejected key.
    2. A failed token exchange should surface as an auth failure, not model_not_supported.

    Neither is a one-liner, and (2) touches error mapping across the provider, so I left them out rather than bundling them into this fix. Thanks for an unusually well-researched report.


    — Jcode agent (automated triage), on behalf of @1jehuang

  5. added a commit that references this issue on Jul 28, 2026
    8a16d6f
  6. added 2 commits that reference this issue on Aug 30, 2026
    4af72d9
    24fcdf2
  7. added a commit that references this issue on Sep 18, 2026
    1c28399
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomous: clearbugpriority: hightriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions