gh-aw icon indicating copy to clipboard operation
gh-aw copied to clipboard

apply_samples.cjs ignores target-repo when fetching PR for push_to_pull_request_branch samples in siderepo workflows

Open dsyme opened this issue 5 months ago • 5 comments

Summary

Two siderepo push-to-PR-branch workflow-dispatch tests in githubnext/gh-aw-test fail persistently on every matrix run (failing since run 27847637501):

  • test-copilot-siderepo-push-to-pull-request-branch-using-dispatch
  • test-copilot-siderepo-sparse-push-to-pull-request-branch-using-dispatch

Failing in all four matrix entries (main/samples=true, main/samples=false, pre-release, release). Previous issues #37546 and #37545 were closed but the bug persists.


Root cause — non-sparse variant

Run: 28080402724

The test creates a PR in githubnext/gh-aw-side-repo (not the host repo githubnext/gh-aw-test). The e2e harness dispatches the workflow with pull_request_number=447 as an input. The lockfile's GH_AW_SAMPLES env var correctly resolves to:

[{
  "tool": "push_to_pull_request_branch",
  "arguments": {
    "message": "Multi-commit test push from Copilot in side repo",
    "pull_request_number": "447"
  },
  "sidecars": { "patch": "..." }
}]

But apply_samples.cjs then fetches:

[warning] apply_samples: GET https://api.github.com/repos/githubnext/gh-aw-test/pulls/447 returned HTTP 404
[error] Error: apply_samples: cannot derive pull-request head branch for sample[0] (tool=push_to_pull_request_branch).
  Trigger the workflow from a pull_request event, or set arguments.pull_request_number on the sample entry,
  or provide GITHUB_TOKEN so the PR can be fetched.
    at preStagePatch (apply_samples.cjs:361:13)
    at async main (apply_samples.cjs:542:7)

It fetches PR #447 from GITHUB_REPOSITORY (githubnext/gh-aw-test) instead of the side-repo (githubnext/gh-aw-side-repo).

The GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG (set in the safe_outputs job) does correctly contain "target": "447", "target-repo": "githubnext/gh-aw-side-repo". The config file at GH_AW_SAFE_OUTPUTS_CONFIG_PATH (which the agent job reads) also has "target": "${GH_AW_INPUT_PULL_REQUEST_NUMBER}", "target-repo": "githubnext/gh-aw-side-repo" — but apply_samples.cjs does not use this information when deriving the PR branch.

Note: GITHUB_TOKEN is present, and GH_AW_REPO_TOKENS includes {"githubnext/gh-aw-side-repo": "***"}. The issue is purely that apply_samples.cjs constructs the PR fetch URL from GITHUB_REPOSITORY only, ignoring target-repo.


Root cause — sparse variant

Run: 28080402587

Different failure mode: apply_samples.cjs succeeds ("1 sample(s) replayed successfully"), but the safe_outputs job fails with:

##[error] Cannot push to pull request branch: patch modifies protected files (README.md).
Add them to the allowed-files configuration field or set protected-files: fallback-to-issue
to create a review issue instead.

The sparse sample's patch only touches src/sparse-test.py, not README.md. This is unexpected. Possible causes:

  • The test-PR branch in the side repo has accumulated a README.md diff from a stale previous run, and the protected-files check evaluates the full PR diff (not just the incoming patch).
  • Or apply_samples.cjs is emitting a different patch than intended.

Suggested fix (non-sparse)

When apply_samples.cjs processes a push_to_pull_request_branch sample and needs to fetch the PR head branch, it should use target-repo from the safe-outputs config (available in the config file at GH_AW_SAFE_OUTPUTS_CONFIG_PATH) rather than always using GITHUB_REPOSITORY. As a fallback, it could also try repos listed in GH_AW_REPO_TOKENS when the primary fetch returns 404.

dsyme avatar Jun 24 '26 19:06 dsyme

Still reproducing on the 2026-06-26 nightly: run 28217875678, job 83592772682 — same 404 on githubnext/gh-aw-test/pulls/<n> instead of githubnext/gh-aw-side-repo.

dsyme avatar Jun 26 '26 12:06 dsyme

Still failing in run 28278834853 (2026-06-27) — not fixed in current main.

Exact root cause pinpointed in actions/setup/js/apply_samples.cjs:

readConfiguredTargetRepo() already exists (line ~250) and is correct — it reads target-repo from the config file at GH_AW_SAFE_OUTPUTS_CONFIG_PATH. That config file is written before the apply_samples step runs, and already contains "target-repo": "githubnext/gh-aw-side-repo". The token lookup in selectTokenForRepo() is also correct — it checks GH_AW_REPO_TOKENS first.

The only bug is that derivePrHeadRef() (line ~217) never calls readConfiguredTargetRepo():

// Current (broken):
const repoSlug = (typeof entry.arguments.repo === "string" && entry.arguments.repo.trim())
    || process.env.GITHUB_REPOSITORY || "";

// Fix — insert readConfiguredTargetRepo() before the GITHUB_REPOSITORY fallback:
const repoSlug = (typeof entry.arguments.repo === "string" && entry.arguments.repo.trim())
    || readConfiguredTargetRepo(entry.tool)
    || process.env.GITHUB_REPOSITORY || "";

That single-line change makes derivePrHeadRef use the configured target-repo (githubnext/gh-aw-side-repo) and then fetchPullRequestHeadRef already picks up the correct per-repo token from GH_AW_REPO_TOKENS via selectTokenForRepo.

dsyme avatar Jun 27 '26 10:06 dsyme

Still failing in runs 28349548237 and 28349741075 (2026-06-29, gh-aw commit d6d91600fd9a, --use-samples), but the error message has changed — suggesting the apply_samples.cjs 404 was fixed in this commit.

New failure:

✗ Message 1 (push_to_pull_request_branch) failed: Repository 'githubnext/gh-aw-side-repo' not found in workspace.
Check out the target repo with actions/checkout and set its 'path' input so the checkout can be located.
If checking out multiple repositories, ensure each actions/checkout step uses the appropriate 'path' input.

This error comes from the safe_output_handler_manager (not apply_samples.cjs). The push handler looks up the side repo's on-disk location via the checkout manifest env var GH_AW_CHECKOUT_PATH_0, which the compiler still emits as "" because no path: is declared on the side-repo actions/checkout step.

Root cause: The compiler generates the actions/checkout step for githubnext/gh-aw-side-repo without a path: input, leaving GH_AW_CHECKOUT_PATH_0: "". The push handler then cannot locate the repo on disk.

Workaround applied in githubnext/gh-aw-test: Added path: gh-aw-side-repo to the checkout: blocks in both test .md files and recompiled. This produces path: gh-aw-side-repo on the checkout step and GH_AW_CHECKOUT_PATH_0: "gh-aw-side-repo" in the manifest, which the push handler can resolve.

A related issue has been filed to make path: mandatory in the compiler for cross-repo checkouts — see #XXXXX (will update once filed).

dsyme avatar Jun 29 '26 14:06 dsyme

New issue filed for the compiler fix: #42256 (require/auto-generate path: for cross-repo checkout: entries).

dsyme avatar Jun 29 '26 14:06 dsyme

Triaged on current main: non-sparse siderepo workflow_dispatch path fixed in #41295 — derivePrHeadRef now uses readConfiguredTargetRepo(entry.tool) before GITHUB_REPOSITORY. Covered by apply_samples.test.cjs (#41292 siderepo workflow_dispatch).

Sparse variant failures may be a separate protected-files/checkout-path issue (#42256). Recommend closing the original target-repo lookup bug; track sparse separately if still failing in gh-aw-test matrix.

Tarekchehahde avatar Jul 11 '26 15:07 Tarekchehahde