Skip to content

fix(bundle): include refs/heads/<branch> in bundle when agent HEAD is on target branch (non-main dispatch)#37929

Merged
pelikhan merged 6 commits into
mainfrom
copilot/safe-outputs-fix-worker-branch
Jun 8, 2026
Merged

fix(bundle): include refs/heads/<branch> in bundle when agent HEAD is on target branch (non-main dispatch)#37929
pelikhan merged 6 commits into
mainfrom
copilot/safe-outputs-fix-worker-branch

Conversation

Copilot AI commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

When a worker is dispatched from a non-main scanner run, safe_outputs fails to apply the git bundle with couldn't find remote ref refs/heads/<worker-branch>. The bundle contains only a bare HEAD ref instead of refs/heads/<branchName>, so applyBundleToBranch can't locate it.

Root cause

In generate_git_bundle.cjs Strategy 2, the code creates a named local branch so the bundle embeds refs/heads/<branchName>:

execGitSync(["branch", "-f", "--", branchName, "HEAD"], { cwd });

This fails with fatal: cannot force update the branch '<branchName>' used by worktree when the agent has already checked out branchName (the normal worker flow: checkout fix-branch → commit → call create_pull_request). The catch silently falls back to rangeEnd = "HEAD", producing a HEAD-only bundle.

Changes

  • generate_git_bundle.cjs (Strategy 2): Before attempting git branch -f, resolve the current branch via rev-parse --abbrev-ref HEAD. If it already equals branchName, use it as rangeEnd directly — no force-update needed. The git branch -f path is kept for the case where HEAD is on a different branch.

  • create_pull_request.cjs (defensive fallback): In the branchRefs.length === 0 HEAD-only bundle path, switch from a bare exec() to getExecOutput(ignoreReturnCode: true) and apply the same prerequisite-recovery pattern used by the initial fetch. When the bundle's prerequisite is a non-main branch tip (not reachable from the main-only shallow safe_outputs checkout), the missing commits are fetched from origin and the bundle apply is retried.

  • Tests: New test in generate_git_bundle.test.cjs covering Strategy 2 with a shallow feature-branch clone where GITHUB_SHA is the feature-branch tip and HEAD is on the new fix branch. New test in create_pull_request.test.cjs covering HEAD-only bundle prerequisite recovery. Updated existing HEAD-fallback test to reflect that the HEAD fetch is now via getExecOutput (not exec).

…ch during non-main dispatch

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix dispatched worker for safe_outputs bundle application fix(bundle): include refs/heads/<branch> in bundle when agent HEAD is on target branch (non-main dispatch) Jun 8, 2026
Copilot AI requested a review from pelikhan June 8, 2026 21:10
@pelikhan pelikhan marked this pull request as ready for review June 8, 2026 21:12
Copilot AI review requested due to automatic review settings June 8, 2026 21:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a failure mode in safe_outputs when applying git bundles produced by workers dispatched from non-main refs, ensuring bundles include the expected branch ref (or can recover prerequisites when they do not).

Changes:

  • Update bundle generation Strategy 2 to avoid git branch -f when HEAD is already on the target branch, so the bundle includes refs/heads/<branch>.
  • Make create_pull_request’s HEAD-only bundle fallback use getExecOutput(ignoreReturnCode: true) and add prerequisite-commit recovery + retry.
  • Add/adjust tests to cover the non-main dispatch + HEAD-only prerequisite recovery scenarios.
Show a summary per file
File Description
actions/setup/js/generate_git_bundle.cjs Ensures Strategy 2 preserves a named refs/heads/<branch> ref when HEAD is already on the target branch.
actions/setup/js/create_pull_request.cjs Adds prerequisite-recovery logic to the HEAD-only bundle fetch fallback using stderr-aware execution.
actions/setup/js/generate_git_bundle.test.cjs Adds a regression test for non-main dispatch behavior (shallow clone + HEAD on fix branch).
actions/setup/js/create_pull_request.test.cjs Updates HEAD-only fallback expectations and adds a test for prerequisite recovery in the HEAD-only path.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 4/4 changed files
  • Comments generated: 2

Comment on lines +181 to +183
// Simulate Actions checkout: shallow clone of feature-branch (depth=1)
execGit(["clone", "--depth=1", "--branch=feature-branch", remoteDir, workDir]);
execGit(["config", "user.name", "Test User"], { cwd: workDir });
Comment on lines +283 to +286
await execApi.exec("git", headPrereqFetchArgs);
core.info("Fetched HEAD bundle prerequisite commits from origin successfully");
await execApi.exec("git", ["fetch", bundleFilePath, `HEAD:${bundleTempRef}`]);
} else {
@pelikhan

pelikhan commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

@copilot apply pr-finisher skill

Copilot AI and others added 2 commits June 8, 2026 21:37
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>

Copilot AI commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

@copilot apply pr-finisher skill

Applied. I ran a PR-finisher pass, addressed the actionable review feedback, and pushed updates in beebe9d and 0bdb1d3.

@pelikhan pelikhan merged commit 13b2c04 into main Jun 8, 2026
12 checks passed
@pelikhan pelikhan deleted the copilot/safe-outputs-fix-worker-branch branch June 8, 2026 21:49
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.

safe_outputs: dispatched worker fails to apply bundle with "couldn't find remote ref" when dispatched from a non-main scanner run

3 participants