fix(backend/copilot): run gh auth setup-git once per sandbox session

Use grep to skip re-running if the credential helper is already
configured in ~/.gitconfig — only pays the cost on first command.
Agent can still call it manually if GH_TOKEN changes mid-session.
This commit is contained in:
Zamil Majdy
2026-03-16 15:42:54 +07:00
parent 81a318de3e
commit 5a2ab65f41
2 changed files with 11 additions and 5 deletions

View File

@@ -70,6 +70,8 @@ _E2B_TOOL_NOTES = """
- If the user has connected their GitHub account, both `gh` and `git` are
pre-authenticated — use them directly without any manual login step.
`git` HTTPS operations (clone, push, pull) work automatically.
- If the token changes mid-session (e.g. user reconnects with a new token),
run `gh auth setup-git` to re-register the credential helper.
- If `gh` or `git` fails with an authentication error (e.g. "authentication
required", "could not read Username", or exit code 128), call
`connect_integration(provider="github")` to surface the GitHub credentials

View File

@@ -151,12 +151,16 @@ class BashExecTool(BaseTool):
integration_env = await get_integration_env_vars(user_id)
envs.update(integration_env)
# When GH_TOKEN is available, lazily configure git to use the gh credential
# helper so that git HTTPS operations (clone/push/pull) are authenticated.
# gh auth setup-git is idempotent (writes ~/.gitconfig) — safe to run every
# time; failure is non-fatal (best-effort, logged at debug level only).
# When GH_TOKEN is available, configure git to use the gh credential helper
# so that git HTTPS operations (clone/push/pull) are authenticated.
# Only runs once per sandbox session (grep skips it if already configured).
# The agent can also call `gh auth setup-git` manually at any time — useful
# if GH_TOKEN changes mid-session (e.g. user reconnects with a new token).
if envs.get("GH_TOKEN"):
command = "gh auth setup-git 2>/dev/null || true; " + command
command = (
"grep -q 'helper.*gh' ~/.gitconfig 2>/dev/null"
" || gh auth setup-git 2>/dev/null || true; " + command
)
try:
result = await sandbox.commands.run(