github / github/gh-aw

Cross-repo safe-output PRs fail: patch generated as "create new file" when target file already exists

Open
#17,969 7 comments 0 reactions 1 assignee Claimed by @dsyme View on GitHub
ai-generated ai-inspected bug community
Dominant language
Go
Stars
5.1k
Forks
541
Avg merge
5h 46m
Merged PRs (30d)
760

Description

> [!NOTE]
> This issue was AI-generated based on investigation of two failing workflow runs.

## Problem

When a workflow's `target-repo` differs from the source repo (cross-repo safe-output PR), `git am` fails with exit code 128 because the patch is generated as `create mode 100644` (new file) even though the file already exists in the target repo.

## Reproduction

This is reproducible in a cross-repo workflow that:

- runs in a source repo workspace
- reads a file from a *different* target repo via GitHub MCP
- writes/edits that file locally
- emits a patch via `generate_git_patch.sh`
- applies the patch in the target repo via safe-outputs

In this configuration, the patch is generated as `create mode 100644`, but the file already exists in the target repo, so `git am` fails with exit code 128.

## Root Cause

1. The agent runs in the **source repo** workspace
2. It reads a file from the **target repo** via GitHub MCP, writes it locally, and edits it
3. `generate_git_patch.sh` diffs against the source repo's HEAD — since the file doesn't exist in the source repo, the diff is `create mode 100644` (a full new-file patch)
4. The `safe_outputs` job checks out the target repo (where the file already exists) and runs `git am`, which correctly rejects creating a file that already exists

## Workflow config

```yaml
safe-outputs:
create-pull-request:
target-repo: 'microsoft/vscode-docs'
allowed-repos: ['microsoft/vscode-docs']
base-branch: main
```

## Suggested Fix

Either (or both):

1. **Patch generation**: When `target-repo` differs from the source repo, diff against the target repo's tree instead of the source repo's HEAD. This would produce a proper "modify existing file" patch.

2. **Patch application**: Use `git am --3way` (or fall back to `git apply --3way`) when applying cross-repo patches, which can resolve the create-vs-modify mismatch using the target repo's index.

## Environment

- `gh-aw`: v0.46.5 / awf v0.20.1 / awmg v0.1.4
- Copilot CLI: 0.0.411
- Model: claude-opus-4.6

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.