Skip to content

Comments

Fix push_to_pull_request_branch generating bad patch on issue_comment follow-up runs#16927

Closed
Copilot wants to merge 17 commits intomainfrom
copilot/fix-patch-generation-bug
Closed

Fix push_to_pull_request_branch generating bad patch on issue_comment follow-up runs#16927
Copilot wants to merge 17 commits intomainfrom
copilot/fix-patch-generation-bug

Conversation

Copy link
Contributor

Copilot AI commented Feb 19, 2026

  • Fix generate_git_patch.cjs: fetch origin/<branch> before falling back to merge-base
  • Refactor generate_git_patch.cjs into smaller functions: resolveBaseRef, writePatch, tryBranchStrategy, tryHeadStrategy
  • Add core.info/core.warning logging
  • generate_git_patch.test.cjs: set up global.core mock before tests
  • safe_outputs_handlers.test.cjs: set up global.core mock before tests
  • Remove unused generate_git_patch.sh
  • Merge main and recompile lock files
Original prompt

This section details on the original issue you should resolve

<issue_title>push_to_pull_request_branch generates a bad patch when triggered via an issue_comment event on a PR</issue_title>
<issue_description>Observed on elastic/ai-github-actions#169 (comment)

Agent added one follow-up commit, but generated patch started with [PATCH 1/2] for a commit already on the PR branch, then [PATCH 2/2] for the new commit.

In safe_outputs, we checked out origin/main-ef3ad89c0dd0de53 and git am failed on patch 1 (already exists in index / patch does not apply), so patch 2 never applied.

Likely cause: when refs/remotes/origin/ is not detected at patch-generation time, generate_git_patch.cjs falls back to merge-base(origin/main, ), which can include already-present branch history.

Expected for push_to_pull_request_branch: always generate incremental patch from origin/..HEAD (fetch that ref explicitly); if unavailable, fail clearly instead of merge-base fallback.

Suggested fix:
In generate_git_patch.cjs, add a strict mode for push_to_pull_request_branch:

  • explicitly fetch origin :refs/remotes/origin/
  • generate patch from origin/..HEAD
  • no merge-base fallback in this mode

Keep current fallback behavior for create_pull_request only</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.


Changeset

  • Type: patch
  • Description: Ensure push_to_pull_request_branch fetches the target branch so generated patches only include new commits

Generated by Changeset Generator



✨ PR Review Safe Output Test - Run 22210660047

💥 [THE END] — Illustrated by Smoke Claude



✨ PR Review Safe Output Test - Run 22226202391

💥 [THE END] — Illustrated by Smoke Claude


Changeset

  • Type: patch
  • Description: Ensure push_to_pull_request_branch fetches the target branch before generating patches so follow-up commits only include new work.

Generated by Changeset Generator

Copilot AI and others added 2 commits February 19, 2026 22:21
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
… commits

When issue_comment triggers a workflow run, gh pr checkout is used to check out the
PR branch. Unlike git fetch + checkout, gh pr checkout does NOT create a
refs/remotes/origin/<branch> remote tracking ref.

Without that ref, generate_git_patch fell back to merge-base with the default branch,
which includes ALL commits on the PR branch (including ones already pushed). On a
follow-up run this produced a [PATCH 1/2] for an already-applied commit, causing
git am to fail.

Fix: before falling back to merge-base, try fetching origin/<branch>. If the fetch
succeeds (branch already on remote), use it as the patch base so only genuinely new
commits are included. Only fall back to merge-base for truly brand-new branches.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix patch generation issue for issue_comment events Fix push_to_pull_request_branch generating bad patch on issue_comment follow-up runs Feb 19, 2026
Copilot AI requested a review from pelikhan February 19, 2026 22:31
@pelikhan
Copy link
Contributor

@copilot merge main, recompile

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 19, 2026

@copilot merge main, recompile

Done — merged main and recompiled all lock files in bdce136.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /usr/bin/gh /usr/bin/gh api graphql -f query=query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { hasDiscussionsEnabled } } -f owner=github -f name=gh-aw (http block)
    • Triggering command: /usr/bin/gh /usr/bin/gh api graphql -f query=query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { hasDiscussionsEnabled } } -f owner=github -f name=gh-aw .github/workflows/ci-doctor.lock.yml .github/workflows/claude-code-user-docs-review.lock.yml .github/workflows/cli-consistency-checker.lock.yml (http block)
  • https://api.github.com/repos/actions/checkout/git/ref/tags/11bd71901bbe5b1630ceea73d27597364c9af683
    • Triggering command: /usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/11bd71901bbe5b1630ceea73d27597364c9af683 --jq .object.sha (http block)
  • https://api.github.com/repos/actions/checkout/git/ref/tags/v6
    • Triggering command: /usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v6 --jq .object.sha (http block)
  • https://api.github.com/repos/actions/github-script/git/ref/tags/v7
    • Triggering command: /usr/bin/gh gh api /repos/actions/github-script/git/ref/tags/v7 --jq .object.sha (http block)
  • https://api.github.com/repos/actions/setup-go/git/ref/tags/4dc6199c7b1a012772edbd06daecab0f50c9053c
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/4dc6199c7b1a012772edbd06daecab0f50c9053c --jq .object.sha (http block)
  • https://api.github.com/repos/actions/setup-go/git/ref/tags/v5
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/v5 --jq .object.sha (http block)
  • https://api.github.com/repos/actions/setup-go/git/ref/tags/v6
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/v6 --jq .object.sha (http block)
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/v6 --jq .object.sha -c=4 -nolocalimports -importcfg /tmp/go-build3694944384/b333/importcfg -embedcfg /tmp/go-build3694944384/b333/embedcfg -pack (http block)
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/v6 --jq .object.sha -c=4 -nolocalimports -importcfg /tmp/go-build3694944384/b001/importcfg -pack /home/REDACTED/work/gh-aw/gh-aw/cmd/gh-aw/main.go (http block)
  • https://api.github.com/repos/actions/setup-node/git/ref/tags/v6
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-node/git/ref/tags/v6 --jq .object.sha (http block)
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-node/git/ref/tags/v6 --jq .object.sha -c=4 -nolocalimports -importcfg /tmp/go-build3694944384/b275/importcfg -pack /home/REDACTED/work/gh-aw/gh-aw/pkg/constants/constants.go (http block)
  • https://api.github.com/repos/anchore/sbom-action/git/ref/tags/v0
    • Triggering command: /usr/bin/gh gh api /repos/anchore/sbom-action/git/ref/tags/v0 --jq .object.sha (http block)
  • https://api.github.com/repos/docker/build-push-action/git/ref/tags/v6
    • Triggering command: /usr/bin/gh gh api /repos/docker/build-push-action/git/ref/tags/v6 --jq .object.sha aw ./cmd/gh-aw (http block)
    • Triggering command: /usr/bin/gh gh api /repos/docker/build-push-action/git/ref/tags/v6 --jq .object.sha (http block)
  • https://api.github.com/repos/docker/login-action/git/ref/tags/v3
    • Triggering command: /usr/bin/gh gh api /repos/docker/login-action/git/ref/tags/v3 --jq .object.sha (http block)
  • https://api.github.com/repos/docker/metadata-action/git/ref/tags/v5
    • Triggering command: /usr/bin/gh gh api /repos/docker/metadata-action/git/ref/tags/v5 --jq .object.sha (http block)
  • https://api.github.com/repos/docker/setup-buildx-action/git/ref/tags/v3
    • Triggering command: /usr/bin/gh gh api /repos/docker/setup-buildx-action/git/ref/tags/v3 --jq .object.sha (http block)
  • https://api.github.com/repos/github/gh-aw/git/ref/tags/a70c5eada06553e3510ac27f2c3bda9d3705bccb
    • Triggering command: /usr/bin/gh gh api /repos/github/gh-aw/git/ref/tags/a70c5eada06553e3510ac27f2c3bda9d3705bccb --jq .object.sha (http block)
  • https://api.github.com/repos/githubnext/agentics/git/ref/tags/-
    • Triggering command: /usr/bin/gh gh api /repos/githubnext/agentics/git/ref/tags/- --jq .object.sha (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Adds logInfo/logWarning helpers (stderr + core.info/core.warning when
in GitHub Actions context) and instruments every decision point:
- Entry: branchName, GITHUB_SHA, defaultBranch, cwd, patchPath
- Strategy 1: local branch check, tracking-ref lookup, fetch attempt,
  merge-base fallback, baseRef chosen, commitCount, commit list, patch
  write result
- Strategy 2: currentHead vs GITHUB_SHA, ancestry check, commitCount,
  commit list, patch write result
- Final: success summary or error reason

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
@pelikhan
Copy link
Contributor

@copilot too much logging, just use core.info.

Merge main and recompile

Copilot AI and others added 2 commits February 19, 2026 23:35
- Replace logInfo/logWarning helpers + stderr writes with direct core.info
  and core.warning calls throughout generate_git_patch.cjs
- Merge origin/main (adds macOS arm64 smoke-test fix)
- Recompile all workflow lock files (154/154)

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 19, 2026

@copilot too much logging, just use core.info.

Merge main and recompile

Done in 5d412ab — replaced the custom helpers/stderr writes with direct core.info/core.warning calls, then merged main and recompiled.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /usr/bin/gh /usr/bin/gh api graphql -f query=query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { hasDiscussionsEnabled } } -f owner=github -f name=gh-aw (http block)
    • Triggering command: /usr/bin/gh /usr/bin/gh api graphql -f query=query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { hasDiscussionsEnabled } } -f owner=github -f name=gh-aw fflib (http block)
  • https://api.github.com/repos/actions/checkout/git/ref/tags/11bd71901bbe5b1630ceea73d27597364c9af683
    • Triggering command: /usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/11bd71901bbe5b1630ceea73d27597364c9af683 --jq .object.sha (http block)
  • https://api.github.com/repos/actions/checkout/git/ref/tags/v6
    • Triggering command: /usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v6 --jq .object.sha (http block)
  • https://api.github.com/repos/actions/github-script/git/ref/tags/v7
    • Triggering command: /usr/bin/gh gh api /repos/actions/github-script/git/ref/tags/v7 --jq .object.sha (http block)
    • Triggering command: /usr/bin/gh gh api /repos/actions/github-script/git/ref/tags/v7 --jq .object.sha patch.cjs (http block)
  • https://api.github.com/repos/actions/setup-go/git/ref/tags/4dc6199c7b1a012772edbd06daecab0f50c9053c
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/4dc6199c7b1a012772edbd06daecab0f50c9053c --jq .object.sha (http block)
  • https://api.github.com/repos/actions/setup-go/git/ref/tags/v5
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/v5 --jq .object.sha (http block)
  • https://api.github.com/repos/actions/setup-go/git/ref/tags/v6
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/v6 --jq .object.sha (http block)
  • https://api.github.com/repos/actions/setup-node/git/ref/tags/v6
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-node/git/ref/tags/v6 --jq .object.sha (http block)
    • Triggering command: /usr/bin/gh gh api /repos/actions/setup-node/git/ref/tags/v6 --jq .object.sha k/_temp/runtime-remote.origin.url .cfg r: $owner, name: $name) { hasDiscussionsEnabled } } (http block)
  • https://api.github.com/repos/anchore/sbom-action/git/ref/tags/v0
    • Triggering command: /usr/bin/gh gh api /repos/anchore/sbom-action/git/ref/tags/v0 --jq .object.sha patch.cjs (http block)
  • https://api.github.com/repos/docker/build-push-action/git/ref/tags/v6
    • Triggering command: /usr/bin/gh gh api /repos/docker/build-push-action/git/ref/tags/v6 --jq .object.sha patch.cjs (http block)
  • https://api.github.com/repos/docker/login-action/git/ref/tags/v3
    • Triggering command: /usr/bin/gh gh api /repos/docker/login-action/git/ref/tags/v3 --jq .object.sha patch.cjs (http block)
  • https://api.github.com/repos/docker/metadata-action/git/ref/tags/v5
    • Triggering command: /usr/bin/gh gh api /repos/docker/metadata-action/git/ref/tags/v5 --jq .object.sha patch.cjs (http block)
  • https://api.github.com/repos/docker/setup-buildx-action/git/ref/tags/v3
    • Triggering command: /usr/bin/gh gh api /repos/docker/setup-buildx-action/git/ref/tags/v3 --jq .object.sha patch.cjs (http block)
  • https://api.github.com/repos/github/gh-aw/git/ref/tags/a70c5eada06553e3510ac27f2c3bda9d3705bccb
    • Triggering command: /usr/bin/gh gh api /repos/github/gh-aw/git/ref/tags/a70c5eada06553e3510ac27f2c3bda9d3705bccb --jq .object.sha (http block)
  • https://api.github.com/repos/githubnext/agentics/git/ref/tags/-
    • Triggering command: /usr/bin/gh gh api /repos/githubnext/agentics/git/ref/tags/- --jq .object.sha (http block)

If you need me to access, download, or install something from one of these locations, you can either:

@pelikhan pelikhan marked this pull request as ready for review February 19, 2026 23:44
Copilot AI review requested due to automatic review settings February 19, 2026 23:44
@@ -73,14 +73,22 @@ if [ -n "$BRANCH_NAME" ]; then
echo "Using origin/${BRANCH_NAME@Q} as base for patch generation"
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot determine if this file is still used in the generated lock.yml file. If not remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Confirmed — generate_git_patch.sh is not referenced in any compiled .lock.yml file. Patch generation at runtime is handled entirely by generate_git_patch.cjs. Removed the script, its test file (git_patch_head_test.go), and the README entry in 174e6f7.

@github-actions github-actions bot removed the smoke label Feb 20, 2026
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Smoke test review of PR #16927. The refactoring of generate_git_patch.cjs looks solid — resolveBaseRef correctly prioritizes remote tracking refs, then fetches, then falls back to merge-base, which directly addresses the bug. The decomposition into resolveBaseRef, writePatch, tryBranchStrategy, and tryHeadStrategy improves readability. Two minor inline comments added.

📰 BREAKING: Report filed by Smoke Copilot for issue #16927

return false;
}

const baseRef = resolveBaseRef(branchName, defaultBranch, cwd);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice refactor! The resolveBaseRef + writePatch separation makes tryBranchStrategy clean and easy to follow. The priority order (remote tracking → fetch → merge-base) correctly handles the issue_comment follow-up case.

const { getErrorMessage } = require("./error_helpers.cjs");
const { execGitSync } = require("./git_helpers.cjs");

const PATCH_PATH = "/tmp/gh-aw/aw.patch";
Copy link
Contributor

Choose a reason for hiding this comment

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

Hardcoding PATCH_PATH as a module-level constant is a good improvement over having it scattered as a string literal. Consider exporting it or making it configurable for testing purposes.

@github-actions
Copy link
Contributor

📰 VERDICT: Smoke Copilot has concluded. All systems operational. This is a developing story. 🎤

@github-actions
Copy link
Contributor

PR titles: Prevent MCP server version bump test failures by replacing hardcoded versions with constants; Stabilize frontmatter hash across LF/CRLF newline conventions
GitHub MCP: ✅
Serena MCP: ✅
Playwright: ✅
File write: ✅
Bash cat: ✅
Build: ✅
Overall: PASS

🔮 The oracle has spoken through Smoke Codex for issue #16927

@github-actions
Copy link
Contributor

Smoke Test Results — Run §22226202391

Overall: PARTIAL (1 failure, 1 skipped)

Core Tests (#1–10): ✅✅✅✅✅✅✅✅✅✅

PR Review Tests (#11–17):

💥 [THE END] — Illustrated by Smoke Claude for issue #16927

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

💥 Automated smoke test review - all systems nominal!

💥 [THE END] — Illustrated by Smoke Claude for issue #16927

@pelikhan
Copy link
Contributor

@copilot merge main and recompile

Copy link
Contributor Author

Copilot AI commented Feb 20, 2026

@copilot merge main and recompile

Done in 35d50c4 — merged main and recompiled 153 workflows.

@github-actions
Copy link
Contributor

🧪 Smoke Temporary ID is now testing temporary ID functionality...

@github-actions
Copy link
Contributor

github-actions bot commented Feb 20, 2026

💫 TO BE CONTINUED... Smoke Claude failed! Our hero faces unexpected challenges...

@github-actions
Copy link
Contributor

github-actions bot commented Feb 20, 2026

✨ The prophecy is fulfilled... Smoke Codex has completed its mystical journey. The stars align. 🌟

@github-actions
Copy link
Contributor

🧪 Smoke Project is now testing project operations...

@github-actions
Copy link
Contributor

📰 BREAKING: Smoke Copilot is now investigating this pull request. Sources say the story is developing...

@github-actions
Copy link
Contributor

Smoke Project completed successfully. All project operations validated.

@github-actions
Copy link
Contributor

Smoke test results
PRs: Delete shared/mood.md workflow and clean up all references; Add base-branch support to assign-to-agent for cross-repo PR creation
GitHub MCP: ✅
Serena MCP: ✅
Playwright: ✅
File write+cat: ✅
Build: ✅
Overall: PASS

🔮 The oracle has spoken through Smoke Codex for issue #16927

@github-actions
Copy link
Contributor

Smoke Test Results for @pelikhan's PR:

Test Status
GitHub MCP
Safe Inputs GH CLI
Serena MCP ❌ (not available)
Playwright
File Write + Bash
Discussion Interaction
Build gh-aw
Discussion Creation
Haiku Dispatch
PR Review

Overall: PASS (1 tool unavailable)

📰 BREAKING: Report filed by Smoke Copilot for issue #16927

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Smoke test review pass. The generate_git_patch.cjs refactor looks solid - good fallback strategy for base ref resolution and clean patch writing logic. generate_git_patch.sh removal is clean.

📰 BREAKING: Report filed by Smoke Copilot for issue #16927

@github-actions
Copy link
Contributor

📰 VERDICT: Smoke Copilot has concluded. All systems operational. This is a developing story. 🎤

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

push_to_pull_request_branch generates a bad patch when triggered via an issue_comment event on a PR

2 participants