Skip to content

fix(repositories): clone every branch and authenticate gh in sessions - #372

Merged
artempartos merged 2 commits into
developfrom
fix/session-git-branches-and-gh
Oct 1, 2026
Merged

artempartos merged 2 commits into
developfrom
fix/session-git-branches-and-gh

Conversation

@artempartos

@artempartos artempartos commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Agent sessions could not see branches or reach the GitHub API, and workflows built on "find the branch already in flight" failed on it. Seen on project 28, run 21276: branch discovery reported "no unmerged branches" and "GitHub PRs could not be checked due to missing credentials" and failed the step. The page it was looking for lived on feature/offerings-factory, committed 17 minutes earlier.

Two separate causes:

  1. Branches. The session checkout was git clone --depth=1 --branch=<source>. --depth implies --single-branch, so the refspec tracks only the source branch. git fetch --all never brings in another branch, and git branch -r --no-merged is always empty. This has been the case since February, not since fix!: security and reliability improvements #309. A run that happened to rewrite the refspec by hand found the branch, and one that didn't failed.
  2. GitHub API. Since fix!: security and reliability improvements #309 no token sits in the remote URL, and that was the only place agents found one for gh and REST. gh pr list, gh pr create and POST /merges have failed in every session since the 2026-09-25 deploy, although docs/user-guide/integrations.md still says an agent opens PRs "with gh or the API".

Change

  • Blobless clone for GitHub, GitLab and Azure DevOps session repositories (--filter=blob:none instead of --depth=1). This brings every branch and the full commit history. Blobs of other revisions are fetched on demand through each provider's existing credential helper, which is already configured in the checkout. A full clone was ruled out because it is too heavy for large repositories at session start. Anonymous public clones keep --depth=1.
    • Azure DevOps Services has supported blobless clones since November 2023. We only talk to the cloud service (api_host), not to on-prem Server. I checked this against a public Azure repo: the filter is honoured (blobs are missing after the clone), and git log -p fetches them lazily.
  • gh wrapper (docker/base/git/gh-aixle, installed at runtime as /usr/local/bin/gh, ahead of /usr/bin/gh). Per call, it asks git credential fill in the matching checkout for a token for the call's repository and passes it as GH_TOKEN to that one gh process. The repository comes from -R/--repo, GH_REPO, a gh api repos/<owner>/<repo> path, the checkout the call runs in, or the only GitHub checkout. Nothing goes into the env, .git/config or disk, so fix!: security and reliability improvements #309's guarantee holds. A caller-set GH_TOKEN passes straight through. It is best effort: if the wrapper cannot be written, the clone still succeeds.
  • Clone exec timeout of 120 s. The Kubernetes exec default is 30 s (kubernetes_runtime.rb), and the old clone ran under it implicitly.
  • Session context and user guide now say that every branch is present and that gh authenticates per call.

Measured

Local clone of the 107 MB repository from the incident:

time .git remote branches
--depth=1 (before) 5 s 47 MB 1
--filter=blob:none (after) 6 s 47 MB 77

On the blobless clone, git branch -r --no-merged, git diff --name-only main...<branch> and git merge-base need no network. Checking out another branch made one credential-helper call, and git log -p -5 made two.

Verification

  • Affected backend tests (git_credentials/, azure_devops/, context_builders/resources_test, session_context_service_test, refresh_github_token_test) ran in an isolated container: 209 runs, 0 failures. rubocop and brakeman are clean, and shellcheck is clean on both scripts.
  • gh_wrapper_test.rb runs the script itself against real bash and git, with checkouts configured the way SessionGitSetup configures them.
  • The full suite is left to CI.

Not covered

  • Whether a given GitHub App installation grants Pull requests read/write is a per-installation setting. The wrapper delivers the token, and GitHub decides what it may do.
  • Sessions already running keep their shallow clone and have no wrapper.

🤖 Generated with Claude Code

artempartos and others added 2 commits October 1, 2026 01:20
A session checkout was a --depth=1 single-branch clone, so `git branch -r`
listed only the source branch and no agent could find or diff work already
in flight on another branch without re-deriving a refspec and an unshallow
by hand. Workflows that pick up an existing feature branch reported "no
match" and failed. The clone is now blobless instead: every branch and the
full commit history at about a shallow clone's size, with skipped blobs
fetched on demand through the credential helper. Azure and anonymous public
clones keep their shallow clone.

Since #309 no token sits in the remote URL, which was the only place gh and
REST calls found one, so `gh pr list`, `gh pr create` and the merges API
stopped working in every session. A gh wrapper ahead of the real one on
PATH now asks the same credential helper per call for the repository the
call targets, and hands the token to that gh process only.

The clone exec also gets an explicit timeout: the Kubernetes exec default
of 30 s is shorter than a large repository takes to clone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
Azure DevOps Services has served blobless partial clones since November
2023, and the platform only talks to the cloud service (api_host), never to
an on-prem Azure DevOps Server that might not. So Azure checkouts get every
branch and the full history on the same terms as GitHub and GitLab, with
skipped blobs fetched through the existing Azure credential helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit ceede10 into develop Oct 1, 2026
7 checks passed
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