Skip to content

Commit 72eea53

Browse files
LeeCampbellclaude
andauthored
feat: defer workflow file changes when PAT lacks workflow scope (HdrHistogram#151)
Fine-grained PATs cannot push .github/workflows/ changes. Add a push_or_defer_workflows() helper that detects the rejection, strips workflow changes from the commit, retries the push, and posts the deferred diff as a PR comment for manual application. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a3a6af0 commit 72eea53

3 files changed

Lines changed: 71 additions & 3 deletions

File tree

autonomous/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ CLAUDE_CODE_OAUTH_TOKEN=your-oauth-token
99
# Required — upstream PR permissions
1010
GH_TOKEN_UPSTREAM=ghp_xxxxxxxxxxxxxxxxxxxx
1111

12+
# Note: Fine-grained PATs cannot push .github/workflows/ changes (the
13+
# `workflow` scope only exists on classic PATs). The agent will automatically
14+
# defer workflow file changes to a PR comment for manual application.
15+
1216
# Optional
1317
MAX_ITERATIONS=10
1418
COOLDOWN_SECONDS=30

autonomous/agent-loop.sh

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,53 @@ UPSTREAM_REPO="${UPSTREAM_REPO}"
77
UPSTREAM_BASE_BRANCH="${UPSTREAM_BASE_BRANCH:-main}"
88
PROMPT_DIR="${PROMPT_DIR:-/usr/local/share/agent-prompts}"
99

10+
push_or_defer_workflows() {
11+
local branch="$1"
12+
local push_stderr
13+
14+
# Attempt the push, capturing stderr
15+
if push_stderr=$(git push -u origin "$branch" 2>&1); then
16+
return 0
17+
fi
18+
19+
# Check if the failure is workflow-related
20+
if ! echo "$push_stderr" | grep -qi "workflow"; then
21+
echo "ERROR: git push failed: $push_stderr" >&2
22+
return 1
23+
fi
24+
25+
echo "WARNING: push rejected due to workflow file permissions, deferring workflow changes" >&2
26+
27+
# Save the workflow diff against upstream base
28+
git diff "upstream/$UPSTREAM_BASE_BRANCH" -- .github/workflows/ > /tmp/workflow_changes.diff
29+
30+
if [ ! -s /tmp/workflow_changes.diff ]; then
31+
echo "ERROR: workflow rejection detected but no workflow diff found" >&2
32+
return 1
33+
fi
34+
35+
# Restore .github/workflows/ to upstream state
36+
rm -rf .github/workflows
37+
git checkout "upstream/$UPSTREAM_BASE_BRANCH" -- .github/workflows/ 2>/dev/null || true
38+
git add -A
39+
git commit --amend --no-edit
40+
41+
# Retry the push
42+
if ! push_stderr=$(git push -u origin "$branch" 2>&1); then
43+
echo "ERROR: git push failed after stripping workflow changes: $push_stderr" >&2
44+
return 1
45+
fi
46+
47+
echo "WARNING: workflow changes deferred to /tmp/workflow_changes.diff" >&2
48+
return 0
49+
}
50+
1051
sync_state() {
1152
local msg="${1:-agent: update plan state}"
1253
git add -A
1354
if ! git diff --cached --quiet; then
1455
git commit -m "$msg"
15-
git push -u origin "$(git branch --show-current)"
56+
push_or_defer_workflows "$(git branch --show-current)"
1657
fi
1758
}
1859

@@ -70,7 +111,7 @@ case "$STATE" in
70111
git add -A
71112
git diff --cached --quiet || git commit -m "feat(#${ISSUE_NUM}): complete implementation"
72113

73-
if ! git push origin "$BRANCH"; then
114+
if ! push_or_defer_workflows "$BRANCH"; then
74115
echo "ERROR: git push failed, restoring plan state" >&2
75116
cp -r /tmp/plan-backup ./plan 2>/dev/null || true
76117
exit 1
@@ -115,6 +156,27 @@ Closes #${ISSUE_NUM}"
115156
--repo "$UPSTREAM_REPO" --body "PR created: $PR_URL"
116157
fi
117158

159+
# Post deferred workflow changes as a PR comment
160+
if [ -s /tmp/workflow_changes.diff ]; then
161+
DIFF_CONTENT=$(cat /tmp/workflow_changes.diff)
162+
WORKFLOW_COMMENT=$(cat <<EOF
163+
:warning: **Workflow file changes require manual application**
164+
165+
The agent's PAT lacks the \`workflow\` scope needed to push changes under \`.github/workflows/\`.
166+
Please apply the following diff manually:
167+
168+
\`\`\`diff
169+
${DIFF_CONTENT}
170+
\`\`\`
171+
EOF
172+
)
173+
PR_NUMBER=$(echo "$PR_URL" | grep -oP '\d+$')
174+
GH_TOKEN="$GH_TOKEN_UPSTREAM" gh pr comment "$PR_NUMBER" \
175+
--repo "$UPSTREAM_REPO" --body "$WORKFLOW_COMMENT"
176+
rm -f /tmp/workflow_changes.diff
177+
echo "WARNING: workflow changes posted as PR comment"
178+
fi
179+
118180
# Clean up backup after successful PR creation
119181
rm -rf /tmp/plan-backup
120182

autonomous/prompts/create-tasks.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ Include tasks for:
1919
- Updating XML doc comments if public API changes
2020

2121
Use `[ ]` for each task.
22-
Validate the task list is complete by cross-referencing every acceptance criterion in the brief — each criterion must be covered by at least one task.
22+
Validate the task list is complete by cross-referencing every acceptance criterion in the brief — each criterion must be covered by at least one task.
23+
24+
Any task that attempts to alter the ./.github folder will likely fail due to permissions restrictions. These changes should accompany the PR as an attached file with clear direction on the manual intervention required to complete the work.

0 commit comments

Comments
 (0)