Skip to content

fix(runner): update GITHUB_TOKEN for gh CLI after credential refresh#1185

Merged
Gkrumbach07 merged 1 commit intomainfrom
fix/gh-token-refresh-1135
Apr 8, 2026
Merged

fix(runner): update GITHUB_TOKEN for gh CLI after credential refresh#1185
Gkrumbach07 merged 1 commit intomainfrom
fix/gh-token-refresh-1135

Conversation

@ambient-code
Copy link
Copy Markdown
Contributor

@ambient-code ambient-code Bot commented Apr 3, 2026

Summary

  • Fixes GITHUB_TOKEN env var not updated after credential refresh, breaking all gh CLI operations #1135gh CLI operations fail with 401 after mid-session credential refresh because the subprocess's GITHUB_TOKEN env var is stale
  • Adds a gh CLI wrapper script (/tmp/bin/gh) that reads the fresh token from /tmp/.ambient_github_token and exports GH_TOKEN before exec-ing the real gh binary
  • Follows the same pattern as the existing git credential helper — token files are already updated on every refresh, so the wrapper just reads them at invocation time
  • The wrapper is installed by populate_runtime_credentials() and prepended to PATH so it intercepts all gh invocations in the CLI subprocess

How it works

The CLI subprocess environment is fixed at spawn time. When refresh_credentials runs mid-session, os.environ["GITHUB_TOKEN"] is updated in the runner process but NOT in the subprocess. The git credential helper already solved this for git by reading from a token file. This PR applies the same solution for gh:

  1. install_gh_wrapper() writes a shell script to /tmp/bin/gh
  2. The script reads /tmp/.ambient_github_token (already written on every refresh)
  3. If a fresh token is found, it exports GH_TOKEN (which takes precedence over GITHUB_TOKEN)
  4. It then finds and execs the real gh binary, skipping its own directory

Test plan

  • test_install_creates_executable_wrapper — verifies the wrapper is created and executable
  • test_install_prepends_to_path — verifies /tmp/bin is prepended to PATH
  • test_install_is_idempotent — verifies repeated calls don't duplicate PATH entries
  • test_populate_installs_gh_wrapper — verifies populate_runtime_credentials() installs the wrapper
  • All 32 existing credential tests continue to pass

🤖 Generated with Claude Code

…1135)

When GITHUB_TOKEN expires mid-session, the refresh_credentials MCP tool
refreshes backend credentials and writes the fresh token to a file that
the git credential helper reads. However, the gh CLI prioritises the
GITHUB_TOKEN env var (stale in the subprocess) over all other credential
sources, causing 401 errors.

Add a gh CLI wrapper script (same pattern as the existing git credential
helper) that reads the fresh token from /tmp/.ambient_github_token and
exports GH_TOKEN before exec-ing the real gh binary. The wrapper is
installed by populate_runtime_credentials() and prepended to PATH so it
intercepts all gh invocations in the subprocess.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ambient-code ambient-code Bot added this to the Review Queue milestone Apr 3, 2026
@ambient-code ambient-code Bot removed this from the Review Queue milestone Apr 6, 2026
@Gkrumbach07 Gkrumbach07 added the ambient-code:managed PR managed by AI automation label Apr 6, 2026
@ambient-code ambient-code Bot added this to the Review Queue milestone Apr 7, 2026
@Gkrumbach07 Gkrumbach07 merged commit 865dd9d into main Apr 8, 2026
48 of 49 checks passed
@Gkrumbach07 Gkrumbach07 deleted the fix/gh-token-refresh-1135 branch April 8, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ambient-code:managed PR managed by AI automation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GITHUB_TOKEN env var not updated after credential refresh, breaking all gh CLI operations

1 participant