@torka/claude-workflows 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +6 -2
- package/bmad-workflows/bmm/workflows/4-implementation/implement-epic-with-subagents/steps/step-01-init.md +24 -2
- package/bmad-workflows/bmm/workflows/4-implementation/implement-epic-with-subagents/steps/step-01b-continue.md +16 -9
- package/bmad-workflows/bmm/workflows/4-implementation/implement-epic-with-subagents/steps/step-02-orchestrate.md +3 -0
- package/bmad-workflows/bmm/workflows/4-implementation/implement-epic-with-subagents/steps/step-03-complete.md +4 -1
- package/commands/{git-cleanup-and-merge.md → git-local-cleanup-push-pr.md} +15 -116
- package/commands/github-pr-resolve.md +446 -0
- package/package.json +1 -1
|
@@ -12,8 +12,12 @@
|
|
|
12
12
|
"description": "Analyze epic files to identify which epics can run in parallel using Git worktrees"
|
|
13
13
|
},
|
|
14
14
|
{
|
|
15
|
-
"name": "git-cleanup-
|
|
16
|
-
"description": "Analyze
|
|
15
|
+
"name": "git-local-cleanup-push-pr",
|
|
16
|
+
"description": "Analyze local branches, cleanup merged branches, push changes, and create PRs"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"name": "github-pr-resolve",
|
|
20
|
+
"description": "Assess open PRs, handle review comments, run CI, and merge"
|
|
17
21
|
}
|
|
18
22
|
],
|
|
19
23
|
"agents": [
|
|
@@ -67,22 +67,42 @@ current_path: $(pwd)
|
|
|
67
67
|
|
|
68
68
|
### 2. Discover Sidecar Files
|
|
69
69
|
|
|
70
|
-
Scan for existing epic execution state files
|
|
70
|
+
Scan for existing epic execution state files.
|
|
71
71
|
|
|
72
|
+
**IMPORTANT:** When running in a worktree, the sidecar was created in the **main repo** before session restart. You must search the main repo's sidecar folder, not just the local worktree folder.
|
|
73
|
+
|
|
74
|
+
**If running in MAIN REPO (`is_worktree` = false):**
|
|
72
75
|
```bash
|
|
73
76
|
ls -la {sidecarFolder}/epic-*-state.yaml 2>/dev/null
|
|
74
77
|
```
|
|
75
78
|
|
|
79
|
+
**If running in WORKTREE (`is_worktree` = true):**
|
|
80
|
+
```bash
|
|
81
|
+
# Derive main repo path from git
|
|
82
|
+
MAIN_REPO_PATH=$(dirname "$(git rev-parse --git-common-dir)")
|
|
83
|
+
MAIN_SIDECAR_FOLDER="$MAIN_REPO_PATH/_bmad-output/epic-executions"
|
|
84
|
+
|
|
85
|
+
# Search main repo sidecar folder (canonical location where setup created it)
|
|
86
|
+
ls -la "$MAIN_SIDECAR_FOLDER"/epic-*-state.yaml 2>/dev/null
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Store main repo sidecar path for later use:**
|
|
90
|
+
```
|
|
91
|
+
main_sidecar_folder: "$MAIN_SIDECAR_FOLDER" # Only when is_worktree=true
|
|
92
|
+
```
|
|
93
|
+
|
|
76
94
|
**For each sidecar found, extract:**
|
|
77
95
|
- Epic number (from filename pattern)
|
|
78
96
|
- `current_phase` value
|
|
79
97
|
- `worktree_config.worktree_path` (if present)
|
|
80
98
|
- `stories_pending` count
|
|
99
|
+
- `sidecar_path` (full path to the sidecar file)
|
|
81
100
|
|
|
82
101
|
**Build sidecar list:**
|
|
83
102
|
```
|
|
84
103
|
sidecars_found:
|
|
85
104
|
- file: "epic-2-state.yaml"
|
|
105
|
+
sidecar_path: "/main/repo/_bmad-output/epic-executions/epic-2-state.yaml"
|
|
86
106
|
epic_number: 2
|
|
87
107
|
phase: "executing"
|
|
88
108
|
worktree_path: "/path/to/worktree" | null
|
|
@@ -106,10 +126,12 @@ Based on detection results, determine the appropriate path:
|
|
|
106
126
|
```
|
|
107
127
|
Detected: Running in worktree for Epic {N}
|
|
108
128
|
Found matching execution state with {X} stories pending.
|
|
129
|
+
Sidecar location: {sidecar_path}
|
|
109
130
|
|
|
110
131
|
Routing to continuation...
|
|
111
132
|
```
|
|
112
133
|
|
|
134
|
+
→ Pass `sidecar_path` context for subsequent steps to use
|
|
113
135
|
→ Load, read entire file, then execute `{continueFile}`
|
|
114
136
|
|
|
115
137
|
---
|
|
@@ -131,7 +153,7 @@ Setup was completed in previous session.
|
|
|
131
153
|
Ready to begin story execution...
|
|
132
154
|
```
|
|
133
155
|
|
|
134
|
-
→ Update sidecar: `current_phase: "executing"`
|
|
156
|
+
→ Update sidecar at `{sidecar_path}` (the matched sidecar's full path): `current_phase: "executing"`
|
|
135
157
|
→ Load, read entire file, then execute step-02-orchestrate.md
|
|
136
158
|
|
|
137
159
|
---
|
|
@@ -72,9 +72,9 @@ To resume epic execution from a previous session by loading the sidecar state, d
|
|
|
72
72
|
|
|
73
73
|
**Note:** This step is routed from step-01-init which already detected:
|
|
74
74
|
- Whether we're in a worktree or main repo
|
|
75
|
-
- Which sidecar file to use (passed as context)
|
|
75
|
+
- Which sidecar file to use (passed as `sidecar_path` context)
|
|
76
76
|
|
|
77
|
-
If
|
|
77
|
+
If `sidecar_path` was NOT specified by router (edge case):
|
|
78
78
|
|
|
79
79
|
```bash
|
|
80
80
|
# Detect worktree context
|
|
@@ -82,21 +82,28 @@ GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
|
|
|
82
82
|
GIT_COMMON=$(git rev-parse --git-common-dir 2>/dev/null)
|
|
83
83
|
CURRENT_PATH=$(pwd)
|
|
84
84
|
|
|
85
|
-
#
|
|
85
|
+
# Determine sidecar search location
|
|
86
86
|
if [ "$GIT_DIR" != "$GIT_COMMON" ]; then
|
|
87
|
-
#
|
|
88
|
-
|
|
87
|
+
# In worktree - sidecar is in main repo
|
|
88
|
+
MAIN_REPO_PATH=$(dirname "$GIT_COMMON")
|
|
89
|
+
SIDECAR_FOLDER="$MAIN_REPO_PATH/_bmad-output/epic-executions"
|
|
90
|
+
else
|
|
91
|
+
# In main repo - use local folder
|
|
92
|
+
SIDECAR_FOLDER="{sidecarFolder}"
|
|
89
93
|
fi
|
|
94
|
+
|
|
95
|
+
# Find sidecar with matching worktree_path (if in worktree)
|
|
96
|
+
# Parse each epic-*-state.yaml and match worktree_config.worktree_path == CURRENT_PATH
|
|
90
97
|
```
|
|
91
98
|
|
|
92
99
|
**Worktree-specific notes:**
|
|
93
|
-
- Sidecar files are ALWAYS in main repo's `
|
|
94
|
-
- When in worktree,
|
|
95
|
-
- `worktree_config.worktree_path` in sidecar identifies
|
|
100
|
+
- Sidecar files are ALWAYS created in the **main repo's** `_bmad-output/epic-executions/`
|
|
101
|
+
- When in a worktree, you must derive the main repo path to find the sidecar
|
|
102
|
+
- `worktree_config.worktree_path` in sidecar identifies which worktree it belongs to
|
|
96
103
|
|
|
97
104
|
### 1. Load Sidecar State
|
|
98
105
|
|
|
99
|
-
Read the sidecar file at `
|
|
106
|
+
Read the sidecar file at the path determined above (use `sidecar_path` from router context, or derive it as shown in section 0):
|
|
100
107
|
|
|
101
108
|
Extract:
|
|
102
109
|
- `epic_execution_state.epic_file`: Path to epic being executed
|
|
@@ -79,11 +79,14 @@ To autonomously execute all pending stories in the epic by orchestrating special
|
|
|
79
79
|
|
|
80
80
|
## CONTEXT BOUNDARIES:
|
|
81
81
|
|
|
82
|
+
- **Sidecar path:** Use `sidecar_path` from router context (step-01-init). In worktree mode, the sidecar is in the main repo's `_bmad-output/epic-executions/`, NOT the worktree's folder.
|
|
82
83
|
- Sidecar file tracks current story and phase
|
|
83
84
|
- Each agent gets fresh context with specific instructions
|
|
84
85
|
- Sprint-status.yaml is source of truth for story states
|
|
85
86
|
- Handoff messages provide phase completion status
|
|
86
87
|
|
|
88
|
+
**CRITICAL for worktree mode:** All sidecar updates must use the absolute `sidecar_path` passed from step-01-init. Do NOT use `{sidecarFolder}` directly as it resolves to the wrong location in worktrees.
|
|
89
|
+
|
|
87
90
|
---
|
|
88
91
|
|
|
89
92
|
## STORY EXECUTION LOOP
|
|
@@ -74,6 +74,7 @@ To generate the Epic Completion Report, create a Pull Request, handle worktree c
|
|
|
74
74
|
|
|
75
75
|
## CONTEXT BOUNDARIES:
|
|
76
76
|
|
|
77
|
+
- **Sidecar path:** Use `sidecar_path` from router context (step-01-init). In worktree mode, the sidecar is in the main repo's `_bmad-output/epic-executions/`, NOT the worktree's folder.
|
|
77
78
|
- Sidecar file contains complete execution history
|
|
78
79
|
- All story outcomes recorded in execution_log
|
|
79
80
|
- Sprint-status reflects final story states
|
|
@@ -86,7 +87,9 @@ To generate the Epic Completion Report, create a Pull Request, handle worktree c
|
|
|
86
87
|
|
|
87
88
|
### 1. Load Execution Data
|
|
88
89
|
|
|
89
|
-
Read the sidecar file
|
|
90
|
+
Read the sidecar file using `sidecar_path` from router context (passed through step-01-init → step-01b/02):
|
|
91
|
+
|
|
92
|
+
**Note:** In worktree mode, this path points to the main repo's sidecar location, not the worktree's.
|
|
90
93
|
|
|
91
94
|
Gather:
|
|
92
95
|
- `epic_execution_state.epic_file`, `epic_execution_state.epic_name`
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: 'Analyze
|
|
2
|
+
description: 'Analyze local branches, cleanup merged branches, push changes, and create PRs'
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
IT IS CRITICAL THAT YOU FOLLOW THIS WORKFLOW EXACTLY.
|
|
@@ -33,7 +33,7 @@ Before any operations, verify the environment is safe:
|
|
|
33
33
|
- Run `git branch --show-current` to get current branch name
|
|
34
34
|
- **If empty (detached HEAD)**: Run `git rev-parse HEAD` to save the commit SHA instead
|
|
35
35
|
- Run `git rev-parse --show-toplevel` to record the current worktree path
|
|
36
|
-
- Track whether we started in detached HEAD state for Phase
|
|
36
|
+
- Track whether we started in detached HEAD state for Phase 6 restoration
|
|
37
37
|
|
|
38
38
|
8. **Build worktree inventory**: Run `git worktree list --porcelain` to detect all worktrees.
|
|
39
39
|
- Parse the output to build a map: `{ branch_name -> worktree_path }`
|
|
@@ -58,7 +58,7 @@ Before any operations, verify the environment is safe:
|
|
|
58
58
|
11. **Initialize operation log**: Create an in-memory log to track all operations performed.
|
|
59
59
|
- This log will be used for error recovery if something fails mid-workflow
|
|
60
60
|
- Track: operation type, target (branch/worktree), status (success/failed)
|
|
61
|
-
- Log each destructive operation as it completes (worktree removal, branch deletion, push, PR creation
|
|
61
|
+
- Log each destructive operation as it completes (worktree removal, branch deletion, push, PR creation)
|
|
62
62
|
|
|
63
63
|
If any pre-flight check fails, STOP and clearly explain what the user needs to do.
|
|
64
64
|
|
|
@@ -213,11 +213,6 @@ For each branch now eligible for deletion:
|
|
|
213
213
|
|
|
214
214
|
- Run `git branch -vv | grep ': gone]'` and delete them
|
|
215
215
|
|
|
216
|
-
### Step 7: Final cleanup
|
|
217
|
-
|
|
218
|
-
- Run `git fetch --prune` to clean up stale refs
|
|
219
|
-
- Run `git worktree prune` to clean up stale worktree admin entries
|
|
220
|
-
|
|
221
216
|
---
|
|
222
217
|
|
|
223
218
|
## Phase 4: Push Local Changes to Remote
|
|
@@ -307,123 +302,31 @@ For each confirmed branch:
|
|
|
307
302
|
|
|
308
303
|
---
|
|
309
304
|
|
|
310
|
-
## Phase 6:
|
|
311
|
-
|
|
312
|
-
For each open PR (including newly created ones):
|
|
313
|
-
|
|
314
|
-
**Configuration:**
|
|
315
|
-
- Max 3 fix attempts per PR
|
|
316
|
-
- Max 5 minute wait per CI run
|
|
317
|
-
- 30 second polling interval
|
|
318
|
-
|
|
319
|
-
**Process:**
|
|
320
|
-
1. Check CI status: `gh pr checks <pr-number>` or `gh pr view <pr-number> --json statusCheckRollup`
|
|
321
|
-
2. If CI is running:
|
|
322
|
-
- Display: "Waiting for CI on PR #<number>... (attempt X/3, Xs elapsed)"
|
|
323
|
-
- Wait 30 seconds
|
|
324
|
-
- Re-check (up to 5 minutes total)
|
|
325
|
-
3. If CI passes: Mark PR as ready for merge, proceed to Phase 7
|
|
326
|
-
4. If CI fails:
|
|
327
|
-
- Fetch and display the failure output
|
|
328
|
-
- Categorize the failure:
|
|
329
|
-
- **Lint/Formatting errors**: Offer to run `npm run lint -- --fix` automatically
|
|
330
|
-
- **Type errors**: Show errors, ask user: "Skip this PR", "I'll fix manually"
|
|
331
|
-
- **Test failures**: Show test output, ask user: "Skip this PR", "I'll fix manually"
|
|
332
|
-
- **Build errors**: Show error, ask user: "Skip this PR", "I'll fix manually"
|
|
333
|
-
- If auto-fix chosen (lint only):
|
|
334
|
-
- Run the fix command
|
|
335
|
-
- Show the diff to user
|
|
336
|
-
- Ask for confirmation before committing
|
|
337
|
-
- Commit with message: "fix: auto-fix lint errors"
|
|
338
|
-
- Push and decrement retry counter
|
|
339
|
-
- If manual fix chosen:
|
|
340
|
-
- Inform user and pause for this PR
|
|
341
|
-
- Ask: "Continue with other PRs?" or "Wait for manual fix?"
|
|
342
|
-
5. If max retries exceeded:
|
|
343
|
-
- Ask user: "Skip this PR", "Abort workflow", or "I'll fix manually"
|
|
344
|
-
- Handle accordingly
|
|
345
|
-
|
|
346
|
-
**IMPORTANT**: NEVER auto-fix test failures or type errors. Only lint/formatting issues are safe to auto-fix.
|
|
347
|
-
|
|
348
|
-
---
|
|
349
|
-
|
|
350
|
-
## Phase 7: Merge PRs
|
|
351
|
-
|
|
352
|
-
For each PR that passed CI:
|
|
353
|
-
|
|
354
|
-
**Pre-merge checks:**
|
|
355
|
-
1. Re-verify CI status (avoid race condition): `gh pr view <pr-number> --json statusCheckRollup`
|
|
356
|
-
2. Check merge state: `gh pr view <pr-number> --json mergeStateStatus,mergeable`
|
|
357
|
-
|
|
358
|
-
**If branch is behind default branch (`mergeStateStatus` is `BEHIND`):**
|
|
359
|
-
- Ask user: "Update branch via GitHub" or "Skip this PR"
|
|
360
|
-
- If update chosen:
|
|
361
|
-
- Run: `gh api repos/{owner}/{repo}/pulls/{pr-number}/update-branch -X PUT`
|
|
362
|
-
- If the API call fails (e.g., merge conflicts), inform user and offer to skip
|
|
363
|
-
- Wait for CI to re-run after update
|
|
364
|
-
- Re-check merge state
|
|
365
|
-
|
|
366
|
-
**If merge conflicts exist (`mergeable` is `CONFLICTING`):**
|
|
367
|
-
- STOP - cannot auto-merge
|
|
368
|
-
- Show the PR URL
|
|
369
|
-
- Ask user: "Skip this PR" or "I'll resolve conflicts manually"
|
|
370
|
-
- If manual resolution, wait for user confirmation then re-check
|
|
371
|
-
|
|
372
|
-
**Merge:**
|
|
373
|
-
- Run `gh pr merge <pr-number> --delete-branch`
|
|
374
|
-
- This respects the repository's merge strategy settings
|
|
375
|
-
- The `--delete-branch` flag removes the remote branch after merge
|
|
376
|
-
|
|
377
|
-
**If merge fails:**
|
|
378
|
-
- Report the error
|
|
379
|
-
- Ask user how to proceed
|
|
380
|
-
|
|
381
|
-
---
|
|
382
|
-
|
|
383
|
-
## Phase 8: Final Cleanup & Summary
|
|
305
|
+
## Phase 6: Local Cleanup & Summary
|
|
384
306
|
|
|
385
|
-
### Step 1:
|
|
386
|
-
|
|
387
|
-
For any worktrees whose branches were merged in Phase 7:
|
|
388
|
-
- The remote branch is now deleted, but the local worktree may still exist
|
|
389
|
-
- For each such worktree, ask user:
|
|
390
|
-
```
|
|
391
|
-
Worktree at '<path>' is now on merged branch '<branch>'.
|
|
392
|
-
|
|
393
|
-
Options:
|
|
394
|
-
1. Remove worktree (Recommended)
|
|
395
|
-
2. Keep worktree (switch to <default-branch>)
|
|
396
|
-
```
|
|
397
|
-
- If remove: `git worktree remove <path>`
|
|
398
|
-
- If keep: `git -C <path> checkout <default-branch>`
|
|
399
|
-
|
|
400
|
-
### Step 2: Clean up stale refs and worktree entries
|
|
307
|
+
### Step 1: Clean up stale refs and worktree entries
|
|
401
308
|
|
|
402
309
|
1. Run `git fetch --prune` to clean up all stale remote refs
|
|
403
310
|
2. Run `git worktree prune` to clean up stale worktree admin entries
|
|
404
|
-
3. Delete any local branches that were merged (check against PRs merged in Phase 7)
|
|
405
|
-
- Skip branches that are checked out in remaining worktrees
|
|
406
311
|
|
|
407
|
-
### Step
|
|
312
|
+
### Step 2: Return to original context
|
|
408
313
|
|
|
409
314
|
- **If original state was a branch**: Run `git checkout <original-branch>` (if it still exists, otherwise stay on default branch)
|
|
410
315
|
- **If original state was detached HEAD**: Run `git checkout <original-commit-sha>` to restore exact position
|
|
411
|
-
- If the original branch was
|
|
316
|
+
- If the original branch was deleted, inform user: "Your original branch '<branch>' was deleted. You are now on '<default-branch>'."
|
|
412
317
|
|
|
413
|
-
### Step
|
|
318
|
+
### Step 3: Display final summary
|
|
414
319
|
|
|
415
320
|
```
|
|
416
321
|
=====================================================
|
|
417
|
-
Git
|
|
322
|
+
Local Git Cleanup Complete!
|
|
418
323
|
=====================================================
|
|
419
324
|
|
|
420
325
|
BRANCHES:
|
|
421
326
|
✓ Deleted X merged branches (local + remote)
|
|
422
327
|
✓ Pushed X branches to remote
|
|
423
328
|
✓ Created X PRs
|
|
424
|
-
✓ Merged X PRs
|
|
425
329
|
○ Skipped X WIP branches
|
|
426
|
-
✗ X PRs skipped (CI failed / merge conflicts)
|
|
427
330
|
|
|
428
331
|
WORKTREES:
|
|
429
332
|
✓ Removed X worktrees (merged branches)
|
|
@@ -434,6 +337,10 @@ CURRENT STATE:
|
|
|
434
337
|
Branch: <current-branch>
|
|
435
338
|
Worktree: <current-worktree-path>
|
|
436
339
|
|
|
340
|
+
PRs CREATED:
|
|
341
|
+
- PR #42: feature-x
|
|
342
|
+
- PR #43: bugfix-1
|
|
343
|
+
|
|
437
344
|
REMAINING BRANCHES:
|
|
438
345
|
- feature-wip (in worktree: /path/to/feature-wip)
|
|
439
346
|
- bugfix-active (ahead 2)
|
|
@@ -442,9 +349,8 @@ ACTIVE WORKTREES:
|
|
|
442
349
|
/path/to/main main (this worktree)
|
|
443
350
|
/path/to/feature feature-wip clean
|
|
444
351
|
|
|
445
|
-
|
|
446
|
-
-
|
|
447
|
-
- Worktree at /path/to/feature has uncommitted changes
|
|
352
|
+
NEXT STEPS:
|
|
353
|
+
Run `/github-pr-resolve` to handle CI, reviews, and merging.
|
|
448
354
|
=====================================================
|
|
449
355
|
```
|
|
450
356
|
|
|
@@ -481,11 +387,6 @@ If the workflow fails at any point, display:
|
|
|
481
387
|
- Branch is pushed, PR was not created
|
|
482
388
|
- Retry: `gh pr create --head <branch>`
|
|
483
389
|
|
|
484
|
-
**If failed during merge:**
|
|
485
|
-
- PR still exists, not merged
|
|
486
|
-
- Check PR status: `gh pr view <number>`
|
|
487
|
-
- Retry via GitHub UI or: `gh pr merge <number>`
|
|
488
|
-
|
|
489
390
|
**If failed during rebase:**
|
|
490
391
|
- Rebase may be in progress
|
|
491
392
|
- Check: `git status`
|
|
@@ -502,8 +403,6 @@ If the workflow fails at any point, display:
|
|
|
502
403
|
- NEVER use force push (`--force` or `-f`)
|
|
503
404
|
- NEVER use force delete for branches (`-D`) unless confirmed squash-merged
|
|
504
405
|
- ALWAYS confirm with user before destructive operations
|
|
505
|
-
- NEVER merge without CI passing
|
|
506
|
-
- ALWAYS respect retry limits
|
|
507
406
|
- ALWAYS preserve the user's working context (return to original branch)
|
|
508
407
|
|
|
509
408
|
### Worktree Safety Rules
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: 'Assess open PRs, handle review comments, run CI, and merge'
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
IT IS CRITICAL THAT YOU FOLLOW THIS WORKFLOW EXACTLY.
|
|
6
|
+
|
|
7
|
+
<workflow CRITICAL="TRUE">
|
|
8
|
+
|
|
9
|
+
## Phase 0: Pre-flight Checks
|
|
10
|
+
|
|
11
|
+
Before any operations, verify the environment is safe:
|
|
12
|
+
|
|
13
|
+
1. **Check gh authentication**: Run `gh auth status`. If not authenticated, STOP and inform user.
|
|
14
|
+
|
|
15
|
+
2. **Detect remote name**: Run `git remote` and use the first result (usually `origin`).
|
|
16
|
+
|
|
17
|
+
3. **Detect default branch**: Run `gh repo view --json defaultBranchRef -q .defaultBranchRef.name`. Fallback to `main` if this fails.
|
|
18
|
+
|
|
19
|
+
4. **Record current branch and worktree**:
|
|
20
|
+
- Run `git branch --show-current` to get current branch name
|
|
21
|
+
- **If empty (detached HEAD)**: Run `git rev-parse HEAD` to save the commit SHA instead
|
|
22
|
+
- Run `git rev-parse --show-toplevel` to record the current worktree path
|
|
23
|
+
- Track whether we started in detached HEAD state for Phase 5 restoration
|
|
24
|
+
|
|
25
|
+
5. **Build worktree inventory**: Run `git worktree list --porcelain` to detect all worktrees.
|
|
26
|
+
- Parse the output to build a map: `{ branch_name -> worktree_path }`
|
|
27
|
+
- For each branch, record if it's checked out in a worktree and where
|
|
28
|
+
|
|
29
|
+
6. **Check for uncommitted changes**: Run `git status --porcelain`.
|
|
30
|
+
- If output is non-empty, WARN the user (but do NOT block)
|
|
31
|
+
- Note: We only block if we need to checkout branches later for review comment fixes
|
|
32
|
+
|
|
33
|
+
7. **Initialize operation log**: Create an in-memory log to track all operations performed.
|
|
34
|
+
- Track: operation type, target (PR/branch), status (success/failed)
|
|
35
|
+
- Log each operation as it completes
|
|
36
|
+
|
|
37
|
+
If gh authentication fails, STOP and clearly explain what the user needs to do.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Phase 1: PR Discovery & Assessment
|
|
42
|
+
|
|
43
|
+
Gather comprehensive information about all open PRs:
|
|
44
|
+
|
|
45
|
+
1. **Fetch all open PRs**: Run `gh pr list --state open --json number,title,headRefName,baseRefName,statusCheckRollup,reviewDecision,isDraft,createdAt,url`
|
|
46
|
+
|
|
47
|
+
2. **For each PR, gather additional details**:
|
|
48
|
+
- Review status: `gh pr view <number> --json reviews,reviewDecision`
|
|
49
|
+
- CI status: Parse `statusCheckRollup` from the list query
|
|
50
|
+
- Mergeable status: `gh pr view <number> --json mergeStateStatus,mergeable`
|
|
51
|
+
|
|
52
|
+
3. **Categorize PRs**:
|
|
53
|
+
|
|
54
|
+
| Category | Description | Action |
|
|
55
|
+
|----------|-------------|--------|
|
|
56
|
+
| Ready to Merge | CI passed, approved, no conflicts | Phase 4 |
|
|
57
|
+
| Needs Review Comments | Has pending/requested changes | Phase 2 |
|
|
58
|
+
| CI Running | Checks in progress | Phase 3 |
|
|
59
|
+
| CI Failed | Checks failed | Phase 3 |
|
|
60
|
+
| Draft | Marked as draft | Skip unless user requests |
|
|
61
|
+
| Conflicts | Has merge conflicts | Warn user |
|
|
62
|
+
|
|
63
|
+
4. **Present summary table**:
|
|
64
|
+
```
|
|
65
|
+
Open PRs Assessment:
|
|
66
|
+
|
|
67
|
+
| # | Title | Branch | Status | Reviews | CI | Mergeable |
|
|
68
|
+
|---|-------|--------|--------|---------|----|-----------|
|
|
69
|
+
| 42 | Add auth | feature-auth | Ready | Approved | ✓ | Yes |
|
|
70
|
+
| 43 | Fix bug | bugfix-123 | Review | Changes Requested | ✓ | Yes |
|
|
71
|
+
| 44 | New API | api-v2 | CI | - | Running | Yes |
|
|
72
|
+
| 45 | Refactor | refactor | Conflicts | - | ✓ | No |
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
5. **Ask user how to proceed**:
|
|
76
|
+
- "Process all PRs in order"
|
|
77
|
+
- "Let me select which PRs to process"
|
|
78
|
+
- "Skip to specific phase" (if user only wants merging)
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Phase 2: Review Comments Handling
|
|
83
|
+
|
|
84
|
+
For each PR with pending reviews or requested changes:
|
|
85
|
+
|
|
86
|
+
### Step 1: Fetch review data
|
|
87
|
+
|
|
88
|
+
Run `gh pr view <number> --json reviews,reviewDecision,comments` and `gh api repos/{owner}/{repo}/pulls/{number}/comments` to get:
|
|
89
|
+
- Review decision (APPROVED, CHANGES_REQUESTED, REVIEW_REQUIRED)
|
|
90
|
+
- All review comments with file paths and line numbers
|
|
91
|
+
|
|
92
|
+
### Step 2: Categorize comments
|
|
93
|
+
|
|
94
|
+
Analyze each comment and categorize:
|
|
95
|
+
|
|
96
|
+
| Category | Auto-addressable? | Examples |
|
|
97
|
+
|----------|-------------------|----------|
|
|
98
|
+
| Add comment/docstring | Yes | "Please add a comment explaining this", "Add JSDoc" |
|
|
99
|
+
| Rename variable/function | Yes | "Rename `x` to `count`", "Use more descriptive name" |
|
|
100
|
+
| Add type annotation | Yes | "Add type for this parameter" |
|
|
101
|
+
| Fix typo | Yes | "Typo: 'recieve' -> 'receive'" |
|
|
102
|
+
| Logic change | No - guided | "This should handle the null case" |
|
|
103
|
+
| Refactor request | No - guided | "Extract this into a separate function" |
|
|
104
|
+
| Question/clarification | No - guided | "Why is this needed?" |
|
|
105
|
+
| Approval/praise | Skip | "LGTM", "Nice work" |
|
|
106
|
+
|
|
107
|
+
### Step 3: Display summary and ask user
|
|
108
|
+
|
|
109
|
+
For each PR with comments:
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
PR #42: "Add user authentication"
|
|
113
|
+
Review status: Changes Requested
|
|
114
|
+
Reviewer: @reviewer-name
|
|
115
|
+
|
|
116
|
+
Comments: 5 total
|
|
117
|
+
- 3 auto-addressable (add docstring, rename var, fix typo)
|
|
118
|
+
- 2 need guidance (logic change, refactor request)
|
|
119
|
+
|
|
120
|
+
Options:
|
|
121
|
+
1. Auto-address 3 simple comments (Recommended)
|
|
122
|
+
2. Guide me through all 5 comments
|
|
123
|
+
3. View all comments first
|
|
124
|
+
4. Skip this PR
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Step 4: Auto-address simple comments
|
|
128
|
+
|
|
129
|
+
If user chooses auto-address:
|
|
130
|
+
|
|
131
|
+
1. **Checkout the PR branch** (if not already):
|
|
132
|
+
- Check if branch is in a worktree - if so, operate there with `git -C`
|
|
133
|
+
- Otherwise: `git checkout <branch>`
|
|
134
|
+
|
|
135
|
+
2. **For each auto-addressable comment**:
|
|
136
|
+
- Read the file at the specified path
|
|
137
|
+
- Identify the exact location (line number from review comment)
|
|
138
|
+
- Make the change based on the comment type:
|
|
139
|
+
- **Add comment/docstring**: Generate appropriate documentation
|
|
140
|
+
- **Rename**: Use find/replace for the identifier
|
|
141
|
+
- **Add type annotation**: Infer or use suggested type
|
|
142
|
+
- **Fix typo**: Apply the correction
|
|
143
|
+
- Show diff to user: "Change X to Y in file.ts:42"
|
|
144
|
+
- Ask: "Apply this change?" / "Skip" / "Edit manually"
|
|
145
|
+
|
|
146
|
+
3. **After all accepted changes**:
|
|
147
|
+
- Show consolidated diff
|
|
148
|
+
- Ask for confirmation before committing
|
|
149
|
+
- Commit: `git commit -am "fix: address review comments"`
|
|
150
|
+
- Push: `git push`
|
|
151
|
+
|
|
152
|
+
### Step 5: Guided resolution for complex comments
|
|
153
|
+
|
|
154
|
+
If user chooses guided resolution or has complex comments remaining:
|
|
155
|
+
|
|
156
|
+
For each complex comment:
|
|
157
|
+
1. Display the comment with full context:
|
|
158
|
+
```
|
|
159
|
+
File: src/auth/login.ts
|
|
160
|
+
Line: 42-45
|
|
161
|
+
Reviewer: @reviewer-name
|
|
162
|
+
Comment: "This should handle the null case - what happens if user is undefined?"
|
|
163
|
+
|
|
164
|
+
Current code:
|
|
165
|
+
40 | const user = getUser(id);
|
|
166
|
+
41 | return user.name; // <- reviewer's concern
|
|
167
|
+
42 | }
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
2. Ask user with `AskUserQuestion`:
|
|
171
|
+
```
|
|
172
|
+
How would you like to address this?
|
|
173
|
+
1. Show me the file (I'll make the fix)
|
|
174
|
+
2. Suggest a fix (Claude will propose code)
|
|
175
|
+
3. Mark as resolved with reply
|
|
176
|
+
4. Skip for now
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
3. **If "Suggest a fix"**:
|
|
180
|
+
- Analyze the code and comment
|
|
181
|
+
- Propose a code change
|
|
182
|
+
- Show diff and ask for confirmation
|
|
183
|
+
- Apply if accepted
|
|
184
|
+
|
|
185
|
+
4. **If "Mark as resolved with reply"**:
|
|
186
|
+
- Ask user for reply text
|
|
187
|
+
- Post reply: `gh api repos/{owner}/{repo}/pulls/{number}/comments/{comment_id}/replies -f body="<reply>"`
|
|
188
|
+
|
|
189
|
+
### Step 6: Post-comment handling
|
|
190
|
+
|
|
191
|
+
After addressing comments:
|
|
192
|
+
- Ask: "Re-request review from @reviewer?" / "Mark ready for review?" / "Leave as-is"
|
|
193
|
+
- If re-request: `gh pr ready <number>` (if was draft) or comment @-mentioning reviewer
|
|
194
|
+
|
|
195
|
+
### Safety Guardrails
|
|
196
|
+
|
|
197
|
+
- NEVER auto-address comments that involve logic changes
|
|
198
|
+
- ALWAYS show diff before committing auto-addressed changes
|
|
199
|
+
- ALWAYS confirm before pushing
|
|
200
|
+
- Track which comments were addressed vs skipped for summary
|
|
201
|
+
- NEVER modify code that the comment doesn't directly reference
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## Phase 3: CI Loop
|
|
206
|
+
|
|
207
|
+
For each open PR that needs CI verification:
|
|
208
|
+
|
|
209
|
+
**Configuration:**
|
|
210
|
+
- Max 3 fix attempts per PR
|
|
211
|
+
- Max 5 minute wait per CI run
|
|
212
|
+
- 30 second polling interval
|
|
213
|
+
|
|
214
|
+
**Process:**
|
|
215
|
+
|
|
216
|
+
1. **Check CI status**: `gh pr view <pr-number> --json statusCheckRollup`
|
|
217
|
+
|
|
218
|
+
2. **If CI is running**:
|
|
219
|
+
- Display: "Waiting for CI on PR #<number>... (attempt X/3, Xs elapsed)"
|
|
220
|
+
- Wait 30 seconds
|
|
221
|
+
- Re-check (up to 5 minutes total)
|
|
222
|
+
|
|
223
|
+
3. **If CI passes**: Mark PR as ready for merge, proceed to Phase 4
|
|
224
|
+
|
|
225
|
+
4. **If CI fails**:
|
|
226
|
+
- Fetch and display the failure output
|
|
227
|
+
- Categorize the failure:
|
|
228
|
+
- **Lint/Formatting errors**: Offer to run `npm run lint -- --fix` automatically
|
|
229
|
+
- **Type errors**: Show errors, ask user: "Skip this PR", "I'll fix manually"
|
|
230
|
+
- **Test failures**: Show test output, ask user: "Skip this PR", "I'll fix manually"
|
|
231
|
+
- **Build errors**: Show error, ask user: "Skip this PR", "I'll fix manually"
|
|
232
|
+
- If auto-fix chosen (lint only):
|
|
233
|
+
- Checkout branch (handle worktree if needed)
|
|
234
|
+
- Run the fix command
|
|
235
|
+
- Show the diff to user
|
|
236
|
+
- Ask for confirmation before committing
|
|
237
|
+
- Commit with message: "fix: auto-fix lint errors"
|
|
238
|
+
- Push and decrement retry counter
|
|
239
|
+
- If manual fix chosen:
|
|
240
|
+
- Inform user and pause for this PR
|
|
241
|
+
- Ask: "Continue with other PRs?" or "Wait for manual fix?"
|
|
242
|
+
|
|
243
|
+
5. **If max retries exceeded**:
|
|
244
|
+
- Ask user: "Skip this PR", "Abort workflow", or "I'll fix manually"
|
|
245
|
+
- Handle accordingly
|
|
246
|
+
|
|
247
|
+
**IMPORTANT**: NEVER auto-fix test failures or type errors. Only lint/formatting issues are safe to auto-fix.
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
## Phase 4: Merge PRs
|
|
252
|
+
|
|
253
|
+
For each PR that passed CI (and has no pending review comments):
|
|
254
|
+
|
|
255
|
+
### Pre-merge checks
|
|
256
|
+
|
|
257
|
+
1. Re-verify CI status (avoid race condition): `gh pr view <pr-number> --json statusCheckRollup`
|
|
258
|
+
2. Check merge state: `gh pr view <pr-number> --json mergeStateStatus,mergeable`
|
|
259
|
+
|
|
260
|
+
### Handle branch behind default branch
|
|
261
|
+
|
|
262
|
+
**If `mergeStateStatus` is `BEHIND`:**
|
|
263
|
+
- Ask user: "Update branch via GitHub" or "Skip this PR"
|
|
264
|
+
- If update chosen:
|
|
265
|
+
- Run: `gh api repos/{owner}/{repo}/pulls/{pr-number}/update-branch -X PUT`
|
|
266
|
+
- If the API call fails (e.g., merge conflicts), inform user and offer to skip
|
|
267
|
+
- Wait for CI to re-run after update
|
|
268
|
+
- Re-check merge state
|
|
269
|
+
|
|
270
|
+
### Handle merge conflicts
|
|
271
|
+
|
|
272
|
+
**If `mergeable` is `CONFLICTING`:**
|
|
273
|
+
- STOP - cannot auto-merge
|
|
274
|
+
- Show the PR URL
|
|
275
|
+
- Ask user: "Skip this PR" or "I'll resolve conflicts manually"
|
|
276
|
+
- If manual resolution, wait for user confirmation then re-check
|
|
277
|
+
|
|
278
|
+
### Merge
|
|
279
|
+
|
|
280
|
+
- Present merge summary before proceeding:
|
|
281
|
+
```
|
|
282
|
+
Ready to merge:
|
|
283
|
+
PR #42: "Add user authentication" (feature-auth -> main)
|
|
284
|
+
|
|
285
|
+
This will:
|
|
286
|
+
- Merge the PR using repository's merge strategy
|
|
287
|
+
- Delete the remote branch
|
|
288
|
+
|
|
289
|
+
Proceed? [Yes / Skip / Abort]
|
|
290
|
+
```
|
|
291
|
+
- Run `gh pr merge <pr-number> --delete-branch`
|
|
292
|
+
- This respects the repository's merge strategy settings
|
|
293
|
+
- The `--delete-branch` flag removes the remote branch after merge
|
|
294
|
+
|
|
295
|
+
### Handle merge failure
|
|
296
|
+
|
|
297
|
+
- Report the error
|
|
298
|
+
- Ask user how to proceed
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## Phase 5: Final Cleanup & Summary
|
|
303
|
+
|
|
304
|
+
### Step 1: Handle orphaned worktrees from merged PRs
|
|
305
|
+
|
|
306
|
+
For any worktrees whose branches were merged in Phase 4:
|
|
307
|
+
- The remote branch is now deleted, but the local worktree may still exist
|
|
308
|
+
- For each such worktree, ask user:
|
|
309
|
+
```
|
|
310
|
+
Worktree at '<path>' is now on merged branch '<branch>'.
|
|
311
|
+
|
|
312
|
+
Options:
|
|
313
|
+
1. Remove worktree (Recommended)
|
|
314
|
+
2. Keep worktree (switch to <default-branch>)
|
|
315
|
+
```
|
|
316
|
+
- If remove: `git worktree remove <path>`
|
|
317
|
+
- If keep: `git -C <path> checkout <default-branch>`
|
|
318
|
+
|
|
319
|
+
### Step 2: Clean up local branches
|
|
320
|
+
|
|
321
|
+
For each PR merged in Phase 4:
|
|
322
|
+
- Delete local branch if it exists: `git branch -d <branch>`
|
|
323
|
+
- Skip if branch is checked out in a remaining worktree
|
|
324
|
+
|
|
325
|
+
### Step 3: Clean up stale refs
|
|
326
|
+
|
|
327
|
+
1. Run `git fetch --prune` to clean up all stale remote refs
|
|
328
|
+
2. Run `git worktree prune` to clean up stale worktree admin entries
|
|
329
|
+
|
|
330
|
+
### Step 4: Return to original context
|
|
331
|
+
|
|
332
|
+
- **If original state was a branch**: Run `git checkout <original-branch>` (if it still exists, otherwise stay on default branch)
|
|
333
|
+
- **If original state was detached HEAD**: Run `git checkout <original-commit-sha>` to restore exact position
|
|
334
|
+
- If the original branch was merged and deleted, inform user: "Your original branch '<branch>' was merged. You are now on '<default-branch>'."
|
|
335
|
+
|
|
336
|
+
### Step 5: Display final summary
|
|
337
|
+
|
|
338
|
+
```
|
|
339
|
+
=====================================================
|
|
340
|
+
GitHub PR Resolution Complete!
|
|
341
|
+
=====================================================
|
|
342
|
+
|
|
343
|
+
PRs PROCESSED:
|
|
344
|
+
✓ Merged X PRs
|
|
345
|
+
- PR #42: "Add user authentication"
|
|
346
|
+
- PR #43: "Fix login bug"
|
|
347
|
+
○ Skipped X PRs (CI failed / conflicts)
|
|
348
|
+
- PR #45: "Refactor" (merge conflicts)
|
|
349
|
+
|
|
350
|
+
REVIEW COMMENTS:
|
|
351
|
+
✓ Auto-addressed X comments
|
|
352
|
+
✓ Manually resolved X comments
|
|
353
|
+
○ Skipped X comments
|
|
354
|
+
|
|
355
|
+
CI FIXES:
|
|
356
|
+
✓ Auto-fixed X lint issues
|
|
357
|
+
○ X PRs need manual fixes
|
|
358
|
+
|
|
359
|
+
CLEANUP:
|
|
360
|
+
✓ Deleted X remote branches
|
|
361
|
+
✓ Deleted X local branches
|
|
362
|
+
✓ Removed X worktrees
|
|
363
|
+
|
|
364
|
+
CURRENT STATE:
|
|
365
|
+
Branch: <current-branch>
|
|
366
|
+
Worktree: <current-worktree-path>
|
|
367
|
+
|
|
368
|
+
REMAINING OPEN PRs:
|
|
369
|
+
- PR #45: "Refactor" - merge conflicts, needs manual resolution
|
|
370
|
+
- PR #46: "Draft feature" - draft, skipped
|
|
371
|
+
|
|
372
|
+
WARNINGS (if any):
|
|
373
|
+
- PR #45 has merge conflicts at src/auth/login.ts
|
|
374
|
+
=====================================================
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
</workflow>
|
|
378
|
+
|
|
379
|
+
## Error Recovery
|
|
380
|
+
|
|
381
|
+
If the workflow fails at any point, display:
|
|
382
|
+
|
|
383
|
+
1. **Operations completed successfully** (from the operation log)
|
|
384
|
+
2. **The operation that failed** and the error message
|
|
385
|
+
3. **Current repository state**:
|
|
386
|
+
- Current branch: `git branch --show-current`
|
|
387
|
+
- Uncommitted changes: `git status --porcelain`
|
|
388
|
+
- Any in-progress operations: check for rebase/merge/cherry-pick
|
|
389
|
+
|
|
390
|
+
4. **Recovery guidance based on failure point**:
|
|
391
|
+
|
|
392
|
+
**If failed during review comment fix:**
|
|
393
|
+
- Changes may be uncommitted
|
|
394
|
+
- Check: `git status` and `git diff`
|
|
395
|
+
- Either commit or stash the changes
|
|
396
|
+
- PR is still open (safe)
|
|
397
|
+
|
|
398
|
+
**If failed during CI fix commit:**
|
|
399
|
+
- Changes may be uncommitted
|
|
400
|
+
- Check: `git status`
|
|
401
|
+
- Commit manually: `git commit -am "fix: ..."`
|
|
402
|
+
- Push: `git push`
|
|
403
|
+
|
|
404
|
+
**If failed during merge:**
|
|
405
|
+
- PR still exists, not merged
|
|
406
|
+
- Check PR status: `gh pr view <number>`
|
|
407
|
+
- Retry via GitHub UI or: `gh pr merge <number>`
|
|
408
|
+
|
|
409
|
+
**If failed during worktree cleanup:**
|
|
410
|
+
- Worktree may be partially removed
|
|
411
|
+
- Run: `git worktree prune` to clean up
|
|
412
|
+
- Check: `git worktree list`
|
|
413
|
+
|
|
414
|
+
**If failed during branch deletion:**
|
|
415
|
+
- Local branch may be deleted but remote still exists (or vice versa)
|
|
416
|
+
- Check: `git branch -a | grep <branch>`
|
|
417
|
+
- Manual cleanup if needed
|
|
418
|
+
|
|
419
|
+
5. **Always safe to re-run**: The workflow is idempotent - running it again will skip already-completed operations.
|
|
420
|
+
|
|
421
|
+
---
|
|
422
|
+
|
|
423
|
+
## Safety Rules
|
|
424
|
+
|
|
425
|
+
### PR Safety Rules
|
|
426
|
+
- NEVER merge without CI passing
|
|
427
|
+
- NEVER force merge when conflicts exist
|
|
428
|
+
- ALWAYS verify merge state before merging
|
|
429
|
+
- ALWAYS respect retry limits
|
|
430
|
+
|
|
431
|
+
### Review Comment Safety Rules
|
|
432
|
+
- NEVER auto-address comments that involve logic changes
|
|
433
|
+
- ALWAYS show diff before committing changes
|
|
434
|
+
- ALWAYS confirm before pushing
|
|
435
|
+
- NEVER modify code that the comment doesn't directly reference
|
|
436
|
+
- ALWAYS track which comments were addressed for summary
|
|
437
|
+
|
|
438
|
+
### Branch Safety Rules
|
|
439
|
+
- NEVER use force push (`--force` or `-f`)
|
|
440
|
+
- ALWAYS preserve the user's working context (return to original branch)
|
|
441
|
+
|
|
442
|
+
### Worktree Safety Rules
|
|
443
|
+
- NEVER remove a worktree with uncommitted changes without explicit user confirmation
|
|
444
|
+
- ALWAYS check worktree status before operations
|
|
445
|
+
- ALWAYS run `git worktree prune` after removing worktrees
|
|
446
|
+
- ALWAYS use `git -C <path>` for operations in linked worktrees
|