@ikunin/sprintpilot 2.2.19 → 2.2.20

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.
@@ -523,6 +523,27 @@ function buildPrBody(state, profile, projectRoot, warnings) {
523
523
  // Both paths honor profile.squash_on_merge. The PR-merge path also
524
524
  // requires profile.platform_provider; non-github platforms fall through
525
525
  // to the local-merge sequence with a description note.
526
+ // Build the worktree-cleanup steps appended to every merge_epic plan
527
+ // when profile.worktree_cleanup_on_merge is true. Documented in
528
+ // modules/git/config.yaml ("false = keep worktrees after epic
529
+ // completion for inspection"). Falls back silently when the helper
530
+ // script isn't available (partial install).
531
+ function buildCleanupSteps(profile) {
532
+ if (!profile.worktree_cleanup_on_merge) return [];
533
+ if (!profile.worktree_enabled) return [];
534
+ return [
535
+ {
536
+ args: ['node', '_Sprintpilot/scripts/cleanup-worktrees.js'],
537
+ description: 'remove orphan worktrees whose branches were merged + deleted',
538
+ // SKIPPED (no .worktrees/ dir) returns 0; real failures here should
539
+ // not halt the merge — the epic is already merged, cleanup is
540
+ // best-effort housekeeping.
541
+ tolerate_exit_codes: [0, 1, 2],
542
+ optional: true,
543
+ },
544
+ ];
545
+ }
546
+
526
547
  function planMergeEpic(state, profile, _action, branch) {
527
548
  const baseBranch = profile.base_branch || 'main';
528
549
  const squash = !!profile.squash_on_merge;
@@ -610,6 +631,7 @@ function planMergeEpic(state, profile, _action, branch) {
610
631
  description: `merge epic PR for ${branch} via gh (${squash ? 'squash' : 'merge'}, delete branch)`,
611
632
  env: Object.keys(env).length ? env : undefined,
612
633
  },
634
+ ...buildCleanupSteps(profile),
613
635
  ],
614
636
  };
615
637
  }
@@ -635,6 +657,7 @@ function planMergeEpic(state, profile, _action, branch) {
635
657
  description: `merge epic MR for ${branch} via glab${squash ? ' (squash)' : ''}`,
636
658
  env: Object.keys(env).length ? env : undefined,
637
659
  },
660
+ ...buildCleanupSteps(profile),
638
661
  ],
639
662
  };
640
663
  }
@@ -712,6 +735,7 @@ function planMergeEpic(state, profile, _action, branch) {
712
735
  retry: { attempts: 4, backoff_ms: [2000, 4000, 8000, 16000], on: 'network' },
713
736
  });
714
737
  }
738
+ for (const s of buildCleanupSteps(profile)) steps.push(s);
715
739
  return { branch, steps };
716
740
  }
717
741
 
@@ -105,6 +105,15 @@ function flatToProfile(resolved, profileName) {
105
105
  get(resolved, 'git.worktree.health_check_on_boot'),
106
106
  true,
107
107
  ),
108
+ // git.worktree.cleanup_on_merge — when true, planMergeEpic appends
109
+ // `git worktree prune` + per-directory cleanup steps so .worktrees/
110
+ // doesn't accumulate orphans after an epic merges. Documented in
111
+ // modules/git/config.yaml ("false = keep worktrees after epic
112
+ // completion for inspection").
113
+ worktree_cleanup_on_merge: coerceBool(
114
+ get(resolved, 'git.worktree.cleanup_on_merge'),
115
+ true,
116
+ ),
108
117
  squash_on_merge: coerceBool(get(resolved, 'git.squash_on_merge'), false),
109
118
  reuse_user_branch: coerceBool(get(resolved, 'git.reuse_user_branch'), false),
110
119
  merge_strategy: coerceEnum(
@@ -1,6 +1,6 @@
1
1
  addon:
2
2
  name: sprintpilot
3
- version: 2.2.19
3
+ version: 2.2.20
4
4
  description: Sprintpilot — autopilot and multi-agent addon for BMad Method (git workflow, parallel agents, autonomous story execution)
5
5
  bmad_compatibility: ">=6.2.0"
6
6
  modules:
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env node
2
+
3
+ // cleanup-worktrees.js — remove orphan worktrees under .worktrees/.
4
+ //
5
+ // After `gh pr merge --delete-branch` (or any branch deletion), the
6
+ // branch is gone but the `.worktrees/<name>/` directory remains. Git's
7
+ // `git worktree prune` removes the `.git/worktrees/<name>/` metadata
8
+ // but NOT the actual directory. Without explicit cleanup, .worktrees/
9
+ // accumulates orphans every epic merge.
10
+ //
11
+ // This script:
12
+ // 1. Runs `git worktree prune --expire now` to clear metadata.
13
+ // 2. Walks `.worktrees/*` and removes directories whose branches no
14
+ // longer resolve (locally and on origin).
15
+ //
16
+ // Honors `git.worktree.cleanup_on_merge` via the orchestrator's plan —
17
+ // this script is only invoked when that flag is true. Standalone use:
18
+ //
19
+ // node _Sprintpilot/scripts/cleanup-worktrees.js \
20
+ // [--worktrees-dir .worktrees] [--project-root <path>] [--dry-run]
21
+
22
+ 'use strict';
23
+
24
+ const fs = require('node:fs');
25
+ const path = require('node:path');
26
+ const cp = require('node:child_process');
27
+
28
+ const { parseArgs } = require('../lib/runtime/args');
29
+ const log = require('../lib/runtime/log');
30
+
31
+ function git(cwd, args, opts) {
32
+ return cp.spawnSync('git', ['-C', cwd, ...args], Object.assign({
33
+ encoding: 'utf8',
34
+ timeout: 10000,
35
+ }, opts || {}));
36
+ }
37
+
38
+ function localBranchExists(projectRoot, branch) {
39
+ const r = git(projectRoot, ['show-ref', '--verify', '--quiet', 'refs/heads/' + branch], {
40
+ stdio: 'ignore',
41
+ });
42
+ return r.status === 0;
43
+ }
44
+
45
+ function remoteBranchExists(projectRoot, branch) {
46
+ const r = git(
47
+ projectRoot,
48
+ ['show-ref', '--verify', '--quiet', 'refs/remotes/origin/' + branch],
49
+ { stdio: 'ignore' },
50
+ );
51
+ return r.status === 0;
52
+ }
53
+
54
+ function detectBranchFromGitfile(worktreeDir) {
55
+ const gitfile = path.join(worktreeDir, '.git');
56
+ let raw;
57
+ try {
58
+ raw = fs.readFileSync(gitfile, 'utf8');
59
+ } catch (_e) {
60
+ return { kind: 'unknown', branch: null };
61
+ }
62
+ const m = /^gitdir:\s*(.+)$/m.exec(raw);
63
+ if (!m) return { kind: 'unknown', branch: null };
64
+ const gitdir = m[1].trim();
65
+ if (!fs.existsSync(gitdir)) {
66
+ return { kind: 'orphan', branch: null };
67
+ }
68
+ const headPath = path.join(gitdir, 'HEAD');
69
+ let head;
70
+ try {
71
+ head = fs.readFileSync(headPath, 'utf8').trim();
72
+ } catch (_e) {
73
+ return { kind: 'orphan', branch: null };
74
+ }
75
+ const refMatch = /^ref:\s*refs\/heads\/(.+)$/m.exec(head);
76
+ if (!refMatch) return { kind: 'detached', branch: null };
77
+ return { kind: 'attached', branch: refMatch[1] };
78
+ }
79
+
80
+ function main() {
81
+ const { opts } = parseArgs(process.argv.slice(2));
82
+ if (opts.help) {
83
+ log.out(
84
+ 'Usage: cleanup-worktrees.js [--worktrees-dir .worktrees] [--project-root <path>] [--dry-run]',
85
+ );
86
+ process.exit(0);
87
+ }
88
+ const projectRoot = opts['project-root'] || process.cwd();
89
+ const worktreesDir = opts['worktrees-dir']
90
+ ? path.resolve(opts['worktrees-dir'])
91
+ : path.join(projectRoot, '.worktrees');
92
+ const dryRun = !!opts['dry-run'];
93
+
94
+ const prune = git(projectRoot, ['worktree', 'prune', '--expire', 'now']);
95
+ if (prune.status !== 0 && prune.error) {
96
+ log.error('git worktree prune failed: ' + prune.error.message);
97
+ process.exit(1);
98
+ }
99
+
100
+ if (!fs.existsSync(worktreesDir)) {
101
+ log.out('SUMMARY:0:0:0');
102
+ return;
103
+ }
104
+ let entries;
105
+ try {
106
+ entries = fs.readdirSync(worktreesDir, { withFileTypes: true });
107
+ } catch (e) {
108
+ log.error('cannot read ' + worktreesDir + ': ' + e.message);
109
+ process.exit(1);
110
+ }
111
+
112
+ let inspected = 0;
113
+ let removed = 0;
114
+ let kept = 0;
115
+ for (const ent of entries) {
116
+ if (!ent.isDirectory()) continue;
117
+ inspected += 1;
118
+ const wt = path.join(worktreesDir, ent.name);
119
+ const info = detectBranchFromGitfile(wt);
120
+
121
+ let orphan = false;
122
+ if (info.kind === 'orphan') {
123
+ orphan = true;
124
+ } else if (info.kind === 'attached' && info.branch) {
125
+ const localOk = localBranchExists(projectRoot, info.branch);
126
+ const remoteOk = remoteBranchExists(projectRoot, info.branch);
127
+ if (!localOk && !remoteOk) orphan = true;
128
+ } else {
129
+ kept += 1;
130
+ continue;
131
+ }
132
+
133
+ if (!orphan) {
134
+ kept += 1;
135
+ continue;
136
+ }
137
+
138
+ log.out('ORPHAN:' + ent.name);
139
+ if (dryRun) {
140
+ removed += 1;
141
+ continue;
142
+ }
143
+ const r = git(projectRoot, ['worktree', 'remove', '--force', wt], { stdio: 'ignore' });
144
+ if (r.status !== 0) {
145
+ try {
146
+ fs.rmSync(wt, { recursive: true, force: true });
147
+ } catch (e) {
148
+ log.err('WARN: cannot remove ' + wt + ': ' + e.message);
149
+ continue;
150
+ }
151
+ }
152
+ removed += 1;
153
+ }
154
+
155
+ log.out('SUMMARY:' + inspected + ':' + removed + ':' + kept);
156
+ }
157
+
158
+ main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ikunin/sprintpilot",
3
- "version": "2.2.19",
3
+ "version": "2.2.20",
4
4
  "description": "Sprintpilot — autopilot and multi-agent addon for BMad Method v6: git workflow, parallel agents, autonomous story execution",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {