-
Notifications
You must be signed in to change notification settings - Fork 0
627 lines (604 loc) · 27.5 KB
/
Copy pathguide-draft.yml
File metadata and controls
627 lines (604 loc) · 27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
name: Guide draft
on:
issues:
types: [labeled]
jobs:
draft:
if: github.event.label.name == 'guide:draft'
runs-on: ubuntu-latest
timeout-minutes: 180
concurrency:
group: guide-draft-issue-${{ github.event.issue.number }}
cancel-in-progress: false
permissions:
contents: write
issues: write
pull-requests: write
env:
ISSUE_NUMBER: ${{ github.event.issue.number }}
ISSUE_TITLE: ${{ github.event.issue.title }}
ISSUE_BODY: ${{ github.event.issue.body }}
GH_TOKEN: ${{ secrets.AGENT_PAT || secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
steps:
- name: Ensure factory labels
run: |
set -euo pipefail
# Repo labels must exist before gh issue edit --add-label (missing label = hard fail).
ensure_label() {
local name="$1" color="$2" desc="$3"
if gh label list --limit 100 --json name --jq '.[].name' | grep -Fxq "$name"; then
return 0
fi
gh label create "$name" --color "$color" --description "$desc"
}
ensure_label "guide:draft" "1D76DB" "Trigger guide draft factory"
ensure_label "guide:in-progress" "FBCA04" "Guide draft factory running"
ensure_label "guide:blocked" "D73A4A" "Guide draft factory blocked"
- name: Preflight existing factory PR
id: preflight
run: |
set -euo pipefail
# Resume from an open factory PR for this issue (guide/issue-N-*) instead
# of refusing — clarifications should iterate on prior research/setup.
# If push succeeded but PR create flaked, also resume from an orphan
# remote factory branch (no open PR yet).
existing=$(gh pr list \
--state open \
--search "in:body \"#${ISSUE_NUMBER}\"" \
--json number,url,body,author,headRefName \
--jq "[.[] | select(.body | test(\"(?i)(closes|fixes|resolves)\\\\s+#${ISSUE_NUMBER}\\\\b\"))]")
count=$(echo "$existing" | jq 'length')
resume=false
resume_pr_url=""
resume_pr_number=""
resume_branch=""
refused=false
refused_pr_url=""
for i in $(seq 0 $((count - 1))); do
author=$(echo "$existing" | jq -r ".[$i].author.login")
head=$(echo "$existing" | jq -r ".[$i].headRefName")
url=$(echo "$existing" | jq -r ".[$i].url")
num=$(echo "$existing" | jq -r ".[$i].number")
if ! gh api "repos/${GH_REPO}/collaborators/${author}" --silent 2>/dev/null; then
continue
fi
if [[ "$head" == guide/issue-${ISSUE_NUMBER}-* ]]; then
resume=true
resume_pr_url="$url"
resume_pr_number="$num"
resume_branch="$head"
break
fi
# Non-factory collaborator PR still blocks (avoid stomping human work).
refused=true
refused_pr_url="$url"
break
done
if [ "$resume" != "true" ] && [ "$refused" != "true" ]; then
# matching-refs: prefix search without paginating all branches
refs_json=$(gh api "repos/${GH_REPO}/git/matching-refs/heads/guide/issue-${ISSUE_NUMBER}-" 2>/dev/null || echo "[]")
bcount=$(echo "$refs_json" | jq 'length')
if [ "$bcount" -eq 1 ]; then
resume=true
resume_branch=$(echo "$refs_json" | jq -r '.[0].ref | sub("^refs/heads/"; "")')
echo "Resuming from orphan factory branch ${resume_branch} (no open PR)"
elif [ "$bcount" -gt 1 ]; then
# Prefer the branch with the newest committer date.
best=""
best_date=""
while IFS= read -r row; do
b=$(echo "$row" | jq -r '.ref | sub("^refs/heads/"; "")')
sha=$(echo "$row" | jq -r '.object.sha')
[ -z "$b" ] || [ -z "$sha" ] && continue
date=$(gh api "repos/${GH_REPO}/git/commits/${sha}" --jq '.committer.date' 2>/dev/null || echo "")
if [ -n "$date" ] && { [ -z "$best_date" ] || [[ "$date" > "$best_date" ]]; }; then
best="$b"
best_date="$date"
fi
done < <(echo "$refs_json" | jq -c '.[]')
if [ -n "$best" ]; then
resume=true
resume_branch="$best"
echo "Resuming from newest orphan factory branch ${resume_branch} (${bcount} matches)"
fi
fi
fi
echo "refused=$refused" >> "$GITHUB_OUTPUT"
echo "refused_pr_url=$refused_pr_url" >> "$GITHUB_OUTPUT"
echo "resume=$resume" >> "$GITHUB_OUTPUT"
echo "resume_pr_url=$resume_pr_url" >> "$GITHUB_OUTPUT"
echo "resume_pr_number=$resume_pr_number" >> "$GITHUB_OUTPUT"
echo "resume_branch=$resume_branch" >> "$GITHUB_OUTPUT"
- name: Refuse non-factory PR
if: steps.preflight.outputs.refused == 'true'
env:
REFUSED_PR_URL: ${{ steps.preflight.outputs.refused_pr_url }}
run: |
gh issue edit "$ISSUE_NUMBER" --remove-label "guide:draft" || true
gh issue edit "$ISSUE_NUMBER" --add-label "guide:blocked" || true
gh issue comment "$ISSUE_NUMBER" --body "Refused to run: $REFUSED_PR_URL already targets this issue and is not a factory branch (\`guide/issue-${ISSUE_NUMBER}-*\`). Close it or finish that PR first, then re-add \`guide:draft\`."
- name: Transition labels
if: steps.preflight.outputs.refused != 'true'
run: |
gh issue edit "$ISSUE_NUMBER" --remove-label "guide:draft" || true
gh issue edit "$ISSUE_NUMBER" --remove-label "guide:blocked" || true
gh issue edit "$ISSUE_NUMBER" --add-label "guide:in-progress"
- name: Checkout main (first run)
if: steps.preflight.outputs.refused != 'true' && steps.preflight.outputs.resume != 'true'
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
token: ${{ secrets.AGENT_PAT || secrets.GITHUB_TOKEN }}
- name: Checkout factory branch (resume)
if: steps.preflight.outputs.refused != 'true' && steps.preflight.outputs.resume == 'true'
uses: actions/checkout@v4
with:
ref: ${{ steps.preflight.outputs.resume_branch }}
fetch-depth: 0
token: ${{ secrets.AGENT_PAT || secrets.GITHUB_TOKEN }}
- name: Setup Node.js
if: steps.preflight.outputs.refused != 'true'
uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
cache-dependency-path: pipeline/package-lock.json
- name: Install dependencies
if: steps.preflight.outputs.refused != 'true'
working-directory: pipeline
run: npm ci
- name: Distill issue intent
id: distill
if: steps.preflight.outputs.refused != 'true'
working-directory: pipeline
env:
CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}
run: |
set -euo pipefail
if [ -z "${CURSOR_API_KEY:-}" ]; then
echo "CURSOR_API_KEY secret is not set" > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
fi
# Fold issue comments into distill context so clarifications on the
# ticket (not only the body) reach --notes on retry.
comments=$(gh api "repos/${GH_REPO}/issues/${ISSUE_NUMBER}/comments" \
--jq '[.[].body] | join("\n\n---\n\n")' \
2>/dev/null || echo "")
if [ -n "${comments}" ]; then
# Keep every line of this assignment indented — bare "## ..." at
# column 0 ends the YAML `run: |` block and breaks the workflow.
{
printf '%s\n\n' "${ISSUE_BODY:-}"
echo "## Issue thread (for clarifications)"
printf '%s\n' "${comments}"
} > "${RUNNER_TEMP}/issue-body-with-thread.txt"
export ISSUE_BODY="$(cat "${RUNNER_TEMP}/issue-body-with-thread.txt")"
fi
set +e
npm run resolve-issue -- --output "${RUNNER_TEMP}/resolved.json"
code=$?
set -e
if [ ! -f "${RUNNER_TEMP}/resolved.json" ]; then
echo "resolve-issue produced no resolved.json (exit ${code})" > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
fi
status=$(jq -r '.status' "${RUNNER_TEMP}/resolved.json")
if [ "$status" = "ok" ]; then
slug=$(jq -r '.slug' "${RUNNER_TEMP}/resolved.json")
provider=$(jq -r '.provider' "${RUNNER_TEMP}/resolved.json")
persona=$(jq -r '.persona // "it-admin"' "${RUNNER_TEMP}/resolved.json")
notes=$(jq -r '.notes // ""' "${RUNNER_TEMP}/resolved.json")
echo "slug=$slug" >> "$GITHUB_OUTPUT"
echo "provider=$provider" >> "$GITHUB_OUTPUT"
echo "persona=$persona" >> "$GITHUB_OUTPUT"
{
echo "notes<<EOF"
echo "$notes"
echo "EOF"
} >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "$status" = "needs_clarification" ]; then
reason=$(jq -r '.reason' "${RUNNER_TEMP}/resolved.json")
candidates=$(jq -r '(.candidates // []) | join(", ")' "${RUNNER_TEMP}/resolved.json")
{
echo "Distill needs clarification before drafting."
echo
echo "**Reason:** ${reason}"
if [ -n "$candidates" ]; then
echo
echo "**Candidates:** ${candidates}"
fi
echo
echo "Reply on this issue (or edit the body) clarifying which MCP server, then re-add \`guide:draft\`."
} > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
fi
echo "Unexpected distill status: ${status}" > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
- name: Comment resolved intent
if: steps.preflight.outputs.refused != 'true' && success()
env:
SLUG: ${{ steps.distill.outputs.slug }}
PROVIDER: ${{ steps.distill.outputs.provider }}
PERSONA: ${{ steps.distill.outputs.persona }}
NOTES: ${{ steps.distill.outputs.notes }}
RESUME: ${{ steps.preflight.outputs.resume }}
RESUME_PR_URL: ${{ steps.preflight.outputs.resume_pr_url }}
RESUME_BRANCH: ${{ steps.preflight.outputs.resume_branch }}
run: |
set -euo pipefail
body_file="${RUNNER_TEMP}/resolved-comment.md"
{
if [ "${RESUME}" = "true" ]; then
if [ -n "${RESUME_PR_URL}" ]; then
echo "Resuming on existing factory PR: ${RESUME_PR_URL}"
else
echo "Resuming on factory branch \`${RESUME_BRANCH}\` (no open PR yet — will open one after this run)."
fi
echo
echo "Resolved as \`${SLUG}\` (${PROVIDER}), persona \`${PERSONA}\`."
echo "Prior \`guides/${SLUG}/\` stays on the branch — research/draft will revise from those artifacts (lock skips when inputs match)."
else
echo "Resolved as \`${SLUG}\` (${PROVIDER}), persona \`${PERSONA}\`."
fi
if [ -n "${NOTES}" ]; then
echo
echo "Notes handed to the pipeline:"
echo
echo "> ${NOTES}"
fi
echo
echo "Starting \`draft-guide\`…"
} > "$body_file"
gh issue comment "$ISSUE_NUMBER" --body-file "$body_file"
- name: Create branch
id: branch
if: steps.preflight.outputs.refused != 'true' && success()
env:
SLUG: ${{ steps.distill.outputs.slug }}
RESUME: ${{ steps.preflight.outputs.resume }}
RESUME_BRANCH: ${{ steps.preflight.outputs.resume_branch }}
run: |
set -euo pipefail
git config user.name "guide-factory[bot]"
git config user.email "guide-factory[bot]@users.noreply.github.com"
if [ "${RESUME}" = "true" ]; then
echo "name=${RESUME_BRANCH}" >> "$GITHUB_OUTPUT"
echo "Resuming on ${RESUME_BRANCH}"
exit 0
fi
branch="guide/issue-${ISSUE_NUMBER}-${SLUG}"
echo "name=$branch" >> "$GITHUB_OUTPUT"
git checkout -b "$branch"
- name: Draft guide
id: draft
if: steps.preflight.outputs.refused != 'true' && success()
working-directory: pipeline
env:
CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}
SLUG: ${{ steps.distill.outputs.slug }}
PERSONA: ${{ steps.distill.outputs.persona }}
NOTES: ${{ steps.distill.outputs.notes }}
run: |
set -euo pipefail
# --pause-on-scope: stop after research when material OQs lack Decision N
# replies in notes (exit 3 → research-only PR + Scope check).
args=(--overwrite --pause-on-scope)
if [ -n "${PERSONA}" ] && [ "${PERSONA}" != "it-admin" ]; then
args+=(--persona "${PERSONA}")
fi
if [ -n "${NOTES}" ]; then
args+=(--notes "${NOTES}")
fi
set +e
npm run draft-guide -- "${SLUG}" "${args[@]}"
code=$?
set -e
# Prefer the newest run record for this slug (written even on unconverged).
record=""
if compgen -G "${GITHUB_WORKSPACE}/retro/runs/*-${SLUG}.json" > /dev/null; then
record=$(ls -t "${GITHUB_WORKSPACE}"/retro/runs/*-"${SLUG}".json | head -n1)
echo "record=${record}" >> "$GITHUB_OUTPUT"
cp "$record" "${RUNNER_TEMP}/run-record.json"
fi
# CLI: 0 = converged, 2 = unconverged/blocked/failed, 3 = awaiting_scope,
# 1 = hard failure. Exit 2/3 still wrote files — open a PR for humans.
if [ "$code" -eq 0 ]; then
echo "outcome=converged" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "$code" -eq 3 ]; then
if [ -f "${GITHUB_WORKSPACE}/guides/${SLUG}/research.md" ]; then
echo "outcome=awaiting_scope" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "draft-guide exited 3 (awaiting_scope) but research.md is missing" > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
fi
if [ "$code" -eq 2 ]; then
if [ -d "${GITHUB_WORKSPACE}/guides/${SLUG}" ]; then
echo "outcome=unconverged" >> "$GITHUB_OUTPUT"
echo "draft-guide exited 2 (unconverged/blocked/failed). Opening a draft PR with whatever was written for human review." > "${RUNNER_TEMP}/failure_reason.txt"
exit 0
fi
echo "draft-guide exited 2 and guides/${SLUG}/ is missing" > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
fi
echo "draft-guide exited ${code} (hard failure; see workflow logs)" > "${RUNNER_TEMP}/failure_reason.txt"
exit "$code"
- name: Commit and push
id: push
if: steps.preflight.outputs.refused != 'true' && success()
env:
BRANCH: ${{ steps.branch.outputs.name }}
SLUG: ${{ steps.distill.outputs.slug }}
OUTCOME: ${{ steps.draft.outputs.outcome }}
run: |
set -euo pipefail
git add "guides/${SLUG}/" || true
# Matching run records for this slug (any timestamp prefix).
if compgen -G "retro/runs/*-${SLUG}.json" > /dev/null; then
git add retro/runs/*-"${SLUG}".json
fi
if git diff --cached --quiet; then
# Resume after a PR-create flake (or lock skip): branch already has the tip.
if git ls-remote --exit-code origin "refs/heads/${BRANCH}" >/dev/null 2>&1; then
echo "No new guide changes; keeping existing tip of ${BRANCH}"
echo "pushed=true" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "No guide or run-record changes to commit" > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
fi
msg="Draft guide: ${SLUG} (issue #${ISSUE_NUMBER})"
if [ "${OUTCOME:-}" = "awaiting_scope" ]; then
msg="Research (awaiting scope): ${SLUG} (issue #${ISSUE_NUMBER})"
fi
git commit -m "$msg"
# force-with-lease keeps resume pushes from clobbering unexpected remote edits
git push --force-with-lease origin "$BRANCH"
echo "pushed=true" >> "$GITHUB_OUTPUT"
- name: Open or update PR
id: open_pr
if: steps.preflight.outputs.refused != 'true' && success()
env:
BRANCH: ${{ steps.branch.outputs.name }}
SLUG: ${{ steps.distill.outputs.slug }}
PROVIDER: ${{ steps.distill.outputs.provider }}
OUTCOME: ${{ steps.draft.outputs.outcome }}
RESUME: ${{ steps.preflight.outputs.resume }}
RESUME_PR_NUMBER: ${{ steps.preflight.outputs.resume_pr_number }}
RESUME_PR_URL: ${{ steps.preflight.outputs.resume_pr_url }}
run: |
set -euo pipefail
# Title stays short; status lives in draft vs ready + issue comments.
title="guide: ${PROVIDER}"
title="${title:0:256}"
# Needs a human reply (scope Decisions or unresolved blockers) → stay draft.
# Converged → ready for review.
needs_human=false
if [ "${OUTCOME}" = "awaiting_scope" ] || [ "${OUTCOME}" = "unconverged" ]; then
needs_human=true
fi
body_file="${RUNNER_TEMP}/pr-body.md"
{
echo "Closes #${ISSUE_NUMBER}"
echo
if [ "${OUTCOME}" = "awaiting_scope" ]; then
echo "Factory **research-only** pause for \`guides/${SLUG}/\` — material open questions need Decisions before draft."
echo
echo "**Pipeline status:** awaiting scope. See the issue comment **Scope check**."
echo "Answer with \`Decision N: …\`, then re-add \`guide:draft\` to continue."
echo
else
echo "Factory draft of \`guides/${SLUG}/\` via \`draft-guide\` (Cursor SDK)."
echo
fi
if [ "${OUTCOME}" = "unconverged" ]; then
echo "**Pipeline status:** unconverged (reviewers still had blockers after max rounds)."
echo "See the issue comment **Pipeline review** for unresolved blockers and open questions."
echo "Do not merge until those are settled."
echo
fi
if [ "${RESUME}" = "true" ]; then
echo "_Updated by a factory re-run (resume) — prior research/setup on this branch were reused where the lock allowed._"
echo
fi
if [ "$needs_human" = "true" ]; then
echo "This PR is a **draft** until the issue Decisions / blockers are settled."
else
echo "Pipeline **converged** — ready for human review (agents never commit; this Action did)."
fi
} > "$body_file"
if [ -f "${RUNNER_TEMP}/run-record.json" ]; then
echo >> "$body_file"
if [ "${OUTCOME}" = "awaiting_scope" ]; then
bash "${GITHUB_WORKSPACE}/.github/scripts/format-scope-check.sh" \
"${RUNNER_TEMP}/run-record.json" "" >> "$body_file"
else
bash "${GITHUB_WORKSPACE}/.github/scripts/format-pipeline-review.sh" \
"${RUNNER_TEMP}/run-record.json" "" \
"${GITHUB_WORKSPACE}/guides/${SLUG}" >> "$body_file"
fi
fi
# Retry transient GitHub GraphQL / 5xx flakes (seen on gh pr create).
is_transient() {
local err="$1"
echo "$err" | grep -qiE \
'GraphQL: Something went wrong|HTTP 50[0-9]|timed out|timeout|ECONNRESET|ECONNREFUSED|secondary rate limit|API rate limit'
}
retry_gh() {
local max=5 attempt=1 delay=10
local err_file="${RUNNER_TEMP}/gh-pr.err"
while true; do
if "$@" 2>"$err_file"; then
return 0
fi
local err
err=$(cat "$err_file")
if [ "$attempt" -ge "$max" ] || ! is_transient "$err"; then
echo "$err" >&2
return 1
fi
echo "Transient GitHub error on attempt ${attempt}/${max}; retrying in ${delay}s…" >&2
echo "$err" >&2
sleep "$delay"
attempt=$((attempt + 1))
delay=$((delay * 2))
done
}
if [ "${RESUME}" = "true" ] && [ -n "${RESUME_PR_NUMBER}" ]; then
if ! retry_gh gh pr edit "${RESUME_PR_NUMBER}" --title "$title" --body-file "$body_file"; then
{
echo "Pushed \`${BRANCH}\` but failed to update PR #${RESUME_PR_NUMBER} after retries."
echo "Re-add \`guide:draft\` to resume from that branch/PR."
} > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
fi
if [ "$needs_human" = "true" ]; then
gh pr ready "${RESUME_PR_NUMBER}" --undo || true
else
gh pr ready "${RESUME_PR_NUMBER}" || true
fi
echo "pr_url=${RESUME_PR_URL}" >> "$GITHUB_OUTPUT"
exit 0
fi
create_args=(--base main --head "$BRANCH" --title "$title" --body-file "$body_file")
if [ "$needs_human" = "true" ]; then
create_args+=(--draft)
fi
err_file="${RUNNER_TEMP}/gh-pr.err"
pr_url=""
max=5
attempt=1
delay=10
while true; do
set +e
pr_url=$(gh pr create "${create_args[@]}" 2>"$err_file")
create_code=$?
set -e
if [ "$create_code" -eq 0 ]; then
pr_url=$(printf '%s\n' "$pr_url" | tail -n1)
break
fi
err=$(cat "$err_file")
if [ "$attempt" -ge "$max" ] || ! is_transient "$err"; then
# Maybe create succeeded and only the response flaked — recover by URL.
existing_url=$(gh pr list --head "$BRANCH" --state open --json url --jq '.[0].url // empty' 2>/dev/null || true)
if [ -n "$existing_url" ]; then
echo "gh pr create failed but open PR exists for ${BRANCH}: ${existing_url}"
pr_url="$existing_url"
break
fi
echo "$err" >&2
{
echo "Guide was pushed to \`${BRANCH}\` but opening the PR failed after retries (likely a GitHub API flake)."
echo
echo "Re-add \`guide:draft\` — the next run will resume from that branch and retry PR create (without a blank-tree restart)."
} > "${RUNNER_TEMP}/failure_reason.txt"
exit 1
fi
echo "Transient GitHub error on attempt ${attempt}/${max}; retrying in ${delay}s…" >&2
echo "$err" >&2
sleep "$delay"
attempt=$((attempt + 1))
delay=$((delay * 2))
done
echo "pr_url=$pr_url" >> "$GITHUB_OUTPUT"
- name: Comment pipeline review or scope check on issue
if: steps.preflight.outputs.refused != 'true' && success()
env:
PR_URL: ${{ steps.open_pr.outputs.pr_url }}
OUTCOME: ${{ steps.draft.outputs.outcome }}
SLUG: ${{ steps.distill.outputs.slug }}
run: |
set -euo pipefail
body_file="${RUNNER_TEMP}/pipeline-review-comment.md"
if [ "${OUTCOME}" = "awaiting_scope" ]; then
if [ -f "${RUNNER_TEMP}/run-record.json" ]; then
bash "${GITHUB_WORKSPACE}/.github/scripts/format-scope-check.sh" \
"${RUNNER_TEMP}/run-record.json" "${PR_URL}" > "$body_file"
else
{
echo "## Scope check"
echo
echo "Research paused before draft (awaiting scope). Draft PR: ${PR_URL}"
echo
echo "_No run record found to list Decisions._"
} > "$body_file"
fi
gh issue comment "$ISSUE_NUMBER" --body-file "$body_file"
gh issue edit "$ISSUE_NUMBER" --add-label "guide:blocked" || true
exit 0
fi
if [ -f "${RUNNER_TEMP}/run-record.json" ]; then
bash "${GITHUB_WORKSPACE}/.github/scripts/format-pipeline-review.sh" \
"${RUNNER_TEMP}/run-record.json" "${PR_URL}" \
"${GITHUB_WORKSPACE}/guides/${SLUG}" > "$body_file"
else
{
echo "## Pipeline review"
echo
if [ "${OUTCOME}" = "unconverged" ]; then
echo "Draft PR opened (pipeline **unconverged**): ${PR_URL}"
else
echo "Draft PR opened: ${PR_URL}"
fi
echo
echo "_No run record found to summarize blockers / open questions._"
} > "$body_file"
fi
gh issue comment "$ISSUE_NUMBER" --body-file "$body_file"
- name: Mark blocked on failure
if: steps.preflight.outputs.refused != 'true' && failure()
env:
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
SLUG: ${{ steps.distill.outputs.slug }}
PUSHED: ${{ steps.push.outputs.pushed }}
BRANCH: ${{ steps.branch.outputs.name }}
run: |
reason="(no reason file written; check workflow logs)"
if [ -f "${RUNNER_TEMP}/failure_reason.txt" ]; then
reason=$(cat "${RUNNER_TEMP}/failure_reason.txt")
fi
body_file="${RUNNER_TEMP}/failure-comment.md"
{
if [ "${PUSHED}" = "true" ]; then
echo "\`guide:draft\` run failed after pushing \`${BRANCH:-guide/issue-${ISSUE_NUMBER}-…}\`."
echo
echo "$reason"
echo
echo "The guide branch is on the remote — re-add \`guide:draft\` to resume from it (skipping a blank-tree restart)."
else
echo "\`guide:draft\` run failed (no PR opened)."
echo
echo "$reason"
fi
echo
echo "**Workflow run:** ${RUN_URL}"
echo
} > "$body_file"
# If a run record exists from a partial draft, surface blockers here too.
if [ -f "${RUNNER_TEMP}/run-record.json" ]; then
echo >> "$body_file"
bash "${GITHUB_WORKSPACE}/.github/scripts/format-pipeline-review.sh" \
"${RUNNER_TEMP}/run-record.json" "" \
"${GITHUB_WORKSPACE}/guides/${SLUG}" >> "$body_file" || true
elif [ -n "${SLUG}" ] && compgen -G "${GITHUB_WORKSPACE}/retro/runs/*-${SLUG}.json" > /dev/null; then
record=$(ls -t "${GITHUB_WORKSPACE}"/retro/runs/*-"${SLUG}".json | head -n1)
echo >> "$body_file"
bash "${GITHUB_WORKSPACE}/.github/scripts/format-pipeline-review.sh" \
"$record" "" "${GITHUB_WORKSPACE}/guides/${SLUG}" >> "$body_file" || true
else
{
echo "Reply on this issue with clarifications, then re-add \`guide:draft\`."
} >> "$body_file"
fi
gh issue edit "$ISSUE_NUMBER" --add-label "guide:blocked" || true
gh issue comment "$ISSUE_NUMBER" --body-file "$body_file"
- name: Always remove in-progress
if: always() && steps.preflight.outputs.refused != 'true'
run: gh issue edit "$ISSUE_NUMBER" --remove-label "guide:in-progress" || true