@tuan_son.dinh/gsd 2.6.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.
Files changed (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +453 -0
  3. package/dist/app-paths.d.ts +4 -0
  4. package/dist/app-paths.js +6 -0
  5. package/dist/cli.d.ts +1 -0
  6. package/dist/cli.js +269 -0
  7. package/dist/loader.d.ts +2 -0
  8. package/dist/loader.js +70 -0
  9. package/dist/logo.d.ts +16 -0
  10. package/dist/logo.js +25 -0
  11. package/dist/onboarding.d.ts +43 -0
  12. package/dist/onboarding.js +418 -0
  13. package/dist/pi-migration.d.ts +14 -0
  14. package/dist/pi-migration.js +57 -0
  15. package/dist/resource-loader.d.ts +22 -0
  16. package/dist/resource-loader.js +60 -0
  17. package/dist/tool-bootstrap.d.ts +4 -0
  18. package/dist/tool-bootstrap.js +74 -0
  19. package/dist/wizard.d.ts +7 -0
  20. package/dist/wizard.js +25 -0
  21. package/package.json +60 -0
  22. package/patches/@mariozechner+pi-coding-agent+0.57.1.patch +108 -0
  23. package/patches/@mariozechner+pi-tui+0.57.1.patch +47 -0
  24. package/pkg/dist/modes/interactive/theme/dark.json +85 -0
  25. package/pkg/dist/modes/interactive/theme/light.json +84 -0
  26. package/pkg/dist/modes/interactive/theme/theme-schema.json +335 -0
  27. package/pkg/dist/modes/interactive/theme/theme.d.ts +78 -0
  28. package/pkg/dist/modes/interactive/theme/theme.d.ts.map +1 -0
  29. package/pkg/dist/modes/interactive/theme/theme.js +949 -0
  30. package/pkg/dist/modes/interactive/theme/theme.js.map +1 -0
  31. package/pkg/package.json +8 -0
  32. package/scripts/postinstall.js +127 -0
  33. package/src/resources/GSD-WORKFLOW.md +661 -0
  34. package/src/resources/agents/researcher.md +29 -0
  35. package/src/resources/agents/scout.md +56 -0
  36. package/src/resources/agents/worker.md +31 -0
  37. package/src/resources/extensions/ask-user-questions.ts +249 -0
  38. package/src/resources/extensions/bg-shell/index.ts +2808 -0
  39. package/src/resources/extensions/browser-tools/BROWSER-TOOLS-V2-PROPOSAL.md +1277 -0
  40. package/src/resources/extensions/browser-tools/core.js +1057 -0
  41. package/src/resources/extensions/browser-tools/index.ts +4989 -0
  42. package/src/resources/extensions/browser-tools/package.json +20 -0
  43. package/src/resources/extensions/context7/index.ts +428 -0
  44. package/src/resources/extensions/context7/package.json +11 -0
  45. package/src/resources/extensions/get-secrets-from-user.ts +352 -0
  46. package/src/resources/extensions/google-search/index.ts +323 -0
  47. package/src/resources/extensions/google-search/package.json +9 -0
  48. package/src/resources/extensions/gsd/activity-log.ts +69 -0
  49. package/src/resources/extensions/gsd/auto.ts +2744 -0
  50. package/src/resources/extensions/gsd/commands.ts +313 -0
  51. package/src/resources/extensions/gsd/crash-recovery.ts +85 -0
  52. package/src/resources/extensions/gsd/dashboard-overlay.ts +521 -0
  53. package/src/resources/extensions/gsd/docs/preferences-reference.md +176 -0
  54. package/src/resources/extensions/gsd/doctor.ts +690 -0
  55. package/src/resources/extensions/gsd/files.ts +732 -0
  56. package/src/resources/extensions/gsd/git-service.ts +597 -0
  57. package/src/resources/extensions/gsd/gitignore.ts +168 -0
  58. package/src/resources/extensions/gsd/guided-flow.ts +817 -0
  59. package/src/resources/extensions/gsd/index.ts +558 -0
  60. package/src/resources/extensions/gsd/metrics.ts +374 -0
  61. package/src/resources/extensions/gsd/migrate/command.ts +218 -0
  62. package/src/resources/extensions/gsd/migrate/index.ts +42 -0
  63. package/src/resources/extensions/gsd/migrate/parser.ts +323 -0
  64. package/src/resources/extensions/gsd/migrate/parsers.ts +624 -0
  65. package/src/resources/extensions/gsd/migrate/preview.ts +48 -0
  66. package/src/resources/extensions/gsd/migrate/transformer.ts +346 -0
  67. package/src/resources/extensions/gsd/migrate/types.ts +370 -0
  68. package/src/resources/extensions/gsd/migrate/validator.ts +55 -0
  69. package/src/resources/extensions/gsd/migrate/writer.ts +539 -0
  70. package/src/resources/extensions/gsd/observability-validator.ts +408 -0
  71. package/src/resources/extensions/gsd/package.json +11 -0
  72. package/src/resources/extensions/gsd/paths.ts +308 -0
  73. package/src/resources/extensions/gsd/preferences.ts +757 -0
  74. package/src/resources/extensions/gsd/prompt-loader.ts +50 -0
  75. package/src/resources/extensions/gsd/prompts/complete-milestone.md +25 -0
  76. package/src/resources/extensions/gsd/prompts/complete-slice.md +29 -0
  77. package/src/resources/extensions/gsd/prompts/discuss.md +189 -0
  78. package/src/resources/extensions/gsd/prompts/doctor-heal.md +29 -0
  79. package/src/resources/extensions/gsd/prompts/execute-task.md +61 -0
  80. package/src/resources/extensions/gsd/prompts/guided-complete-slice.md +1 -0
  81. package/src/resources/extensions/gsd/prompts/guided-discuss-milestone.md +3 -0
  82. package/src/resources/extensions/gsd/prompts/guided-discuss-slice.md +59 -0
  83. package/src/resources/extensions/gsd/prompts/guided-execute-task.md +1 -0
  84. package/src/resources/extensions/gsd/prompts/guided-plan-milestone.md +23 -0
  85. package/src/resources/extensions/gsd/prompts/guided-plan-slice.md +1 -0
  86. package/src/resources/extensions/gsd/prompts/guided-research-slice.md +11 -0
  87. package/src/resources/extensions/gsd/prompts/guided-resume-task.md +1 -0
  88. package/src/resources/extensions/gsd/prompts/plan-milestone.md +65 -0
  89. package/src/resources/extensions/gsd/prompts/plan-slice.md +51 -0
  90. package/src/resources/extensions/gsd/prompts/queue.md +85 -0
  91. package/src/resources/extensions/gsd/prompts/reassess-roadmap.md +48 -0
  92. package/src/resources/extensions/gsd/prompts/replan-slice.md +39 -0
  93. package/src/resources/extensions/gsd/prompts/research-milestone.md +37 -0
  94. package/src/resources/extensions/gsd/prompts/research-slice.md +28 -0
  95. package/src/resources/extensions/gsd/prompts/review-migration.md +66 -0
  96. package/src/resources/extensions/gsd/prompts/run-uat.md +109 -0
  97. package/src/resources/extensions/gsd/prompts/system.md +187 -0
  98. package/src/resources/extensions/gsd/prompts/worktree-merge.md +123 -0
  99. package/src/resources/extensions/gsd/session-forensics.ts +487 -0
  100. package/src/resources/extensions/gsd/skill-discovery.ts +137 -0
  101. package/src/resources/extensions/gsd/state.ts +460 -0
  102. package/src/resources/extensions/gsd/templates/context.md +76 -0
  103. package/src/resources/extensions/gsd/templates/decisions.md +8 -0
  104. package/src/resources/extensions/gsd/templates/milestone-summary.md +73 -0
  105. package/src/resources/extensions/gsd/templates/plan.md +131 -0
  106. package/src/resources/extensions/gsd/templates/preferences.md +24 -0
  107. package/src/resources/extensions/gsd/templates/project.md +31 -0
  108. package/src/resources/extensions/gsd/templates/reassessment.md +28 -0
  109. package/src/resources/extensions/gsd/templates/requirements.md +81 -0
  110. package/src/resources/extensions/gsd/templates/research.md +46 -0
  111. package/src/resources/extensions/gsd/templates/roadmap.md +118 -0
  112. package/src/resources/extensions/gsd/templates/slice-context.md +58 -0
  113. package/src/resources/extensions/gsd/templates/slice-summary.md +99 -0
  114. package/src/resources/extensions/gsd/templates/state.md +19 -0
  115. package/src/resources/extensions/gsd/templates/task-plan.md +52 -0
  116. package/src/resources/extensions/gsd/templates/task-summary.md +57 -0
  117. package/src/resources/extensions/gsd/templates/uat.md +54 -0
  118. package/src/resources/extensions/gsd/tests/activity-log-prune.test.ts +327 -0
  119. package/src/resources/extensions/gsd/tests/auto-preflight.test.ts +56 -0
  120. package/src/resources/extensions/gsd/tests/auto-supervisor.test.mjs +53 -0
  121. package/src/resources/extensions/gsd/tests/complete-milestone.test.ts +225 -0
  122. package/src/resources/extensions/gsd/tests/cost-projection.test.ts +160 -0
  123. package/src/resources/extensions/gsd/tests/derive-state-deps.test.ts +341 -0
  124. package/src/resources/extensions/gsd/tests/derive-state.test.ts +689 -0
  125. package/src/resources/extensions/gsd/tests/discuss-prompt.test.ts +38 -0
  126. package/src/resources/extensions/gsd/tests/doctor.test.ts +505 -0
  127. package/src/resources/extensions/gsd/tests/git-service.test.ts +1313 -0
  128. package/src/resources/extensions/gsd/tests/idle-recovery.test.ts +308 -0
  129. package/src/resources/extensions/gsd/tests/metrics-io.test.ts +201 -0
  130. package/src/resources/extensions/gsd/tests/metrics.test.ts +217 -0
  131. package/src/resources/extensions/gsd/tests/migrate-command.test.ts +390 -0
  132. package/src/resources/extensions/gsd/tests/migrate-parser.test.ts +786 -0
  133. package/src/resources/extensions/gsd/tests/migrate-transformer.test.ts +657 -0
  134. package/src/resources/extensions/gsd/tests/migrate-validator-parsers.test.ts +443 -0
  135. package/src/resources/extensions/gsd/tests/migrate-writer-integration.test.ts +318 -0
  136. package/src/resources/extensions/gsd/tests/migrate-writer.test.ts +420 -0
  137. package/src/resources/extensions/gsd/tests/must-have-parser.test.ts +309 -0
  138. package/src/resources/extensions/gsd/tests/parsers.test.ts +1351 -0
  139. package/src/resources/extensions/gsd/tests/plan-milestone.test.ts +163 -0
  140. package/src/resources/extensions/gsd/tests/plan-quality-validator.test.ts +386 -0
  141. package/src/resources/extensions/gsd/tests/reassess-prompt.test.ts +171 -0
  142. package/src/resources/extensions/gsd/tests/remote-questions.test.ts +155 -0
  143. package/src/resources/extensions/gsd/tests/remote-status.test.ts +99 -0
  144. package/src/resources/extensions/gsd/tests/replan-slice.test.ts +521 -0
  145. package/src/resources/extensions/gsd/tests/requirements.test.ts +125 -0
  146. package/src/resources/extensions/gsd/tests/resolve-ts-hooks.mjs +34 -0
  147. package/src/resources/extensions/gsd/tests/resolve-ts.mjs +11 -0
  148. package/src/resources/extensions/gsd/tests/run-uat.test.ts +348 -0
  149. package/src/resources/extensions/gsd/tests/unit-runtime.test.ts +247 -0
  150. package/src/resources/extensions/gsd/tests/workflow-config.test.mjs +53 -0
  151. package/src/resources/extensions/gsd/tests/workspace-index.test.ts +94 -0
  152. package/src/resources/extensions/gsd/tests/worktree-integration.test.ts +253 -0
  153. package/src/resources/extensions/gsd/tests/worktree-manager.test.ts +160 -0
  154. package/src/resources/extensions/gsd/tests/worktree.test.ts +264 -0
  155. package/src/resources/extensions/gsd/types.ts +159 -0
  156. package/src/resources/extensions/gsd/unit-runtime.ts +184 -0
  157. package/src/resources/extensions/gsd/workspace-index.ts +203 -0
  158. package/src/resources/extensions/gsd/worktree-command.ts +845 -0
  159. package/src/resources/extensions/gsd/worktree-manager.ts +392 -0
  160. package/src/resources/extensions/gsd/worktree.ts +183 -0
  161. package/src/resources/extensions/mac-tools/index.ts +852 -0
  162. package/src/resources/extensions/mac-tools/swift-cli/Package.swift +22 -0
  163. package/src/resources/extensions/mac-tools/swift-cli/Sources/main.swift +1318 -0
  164. package/src/resources/extensions/mcporter/index.ts +429 -0
  165. package/src/resources/extensions/remote-questions/config.ts +81 -0
  166. package/src/resources/extensions/remote-questions/discord-adapter.ts +128 -0
  167. package/src/resources/extensions/remote-questions/format.ts +163 -0
  168. package/src/resources/extensions/remote-questions/manager.ts +192 -0
  169. package/src/resources/extensions/remote-questions/remote-command.ts +307 -0
  170. package/src/resources/extensions/remote-questions/slack-adapter.ts +92 -0
  171. package/src/resources/extensions/remote-questions/status.ts +31 -0
  172. package/src/resources/extensions/remote-questions/store.ts +77 -0
  173. package/src/resources/extensions/remote-questions/types.ts +75 -0
  174. package/src/resources/extensions/search-the-web/cache.ts +78 -0
  175. package/src/resources/extensions/search-the-web/command-search-provider.ts +95 -0
  176. package/src/resources/extensions/search-the-web/format.ts +258 -0
  177. package/src/resources/extensions/search-the-web/http.ts +238 -0
  178. package/src/resources/extensions/search-the-web/index.ts +65 -0
  179. package/src/resources/extensions/search-the-web/native-search.ts +157 -0
  180. package/src/resources/extensions/search-the-web/provider.ts +118 -0
  181. package/src/resources/extensions/search-the-web/tavily.ts +116 -0
  182. package/src/resources/extensions/search-the-web/tool-fetch-page.ts +519 -0
  183. package/src/resources/extensions/search-the-web/tool-llm-context.ts +561 -0
  184. package/src/resources/extensions/search-the-web/tool-search.ts +576 -0
  185. package/src/resources/extensions/search-the-web/url-utils.ts +91 -0
  186. package/src/resources/extensions/shared/confirm-ui.ts +126 -0
  187. package/src/resources/extensions/shared/interview-ui.ts +613 -0
  188. package/src/resources/extensions/shared/next-action-ui.ts +197 -0
  189. package/src/resources/extensions/shared/progress-widget.ts +282 -0
  190. package/src/resources/extensions/shared/terminal.ts +23 -0
  191. package/src/resources/extensions/shared/thinking-widget.ts +107 -0
  192. package/src/resources/extensions/shared/ui.ts +400 -0
  193. package/src/resources/extensions/shared/wizard-ui.ts +551 -0
  194. package/src/resources/extensions/slash-commands/audit.ts +88 -0
  195. package/src/resources/extensions/slash-commands/clear.ts +10 -0
  196. package/src/resources/extensions/slash-commands/create-extension.ts +297 -0
  197. package/src/resources/extensions/slash-commands/create-slash-command.ts +234 -0
  198. package/src/resources/extensions/slash-commands/index.ts +12 -0
  199. package/src/resources/extensions/subagent/agents.ts +126 -0
  200. package/src/resources/extensions/subagent/index.ts +1020 -0
  201. package/src/resources/extensions/voice/index.ts +195 -0
  202. package/src/resources/extensions/voice/speech-recognizer.swift +154 -0
  203. package/src/resources/skills/debug-like-expert/SKILL.md +231 -0
  204. package/src/resources/skills/debug-like-expert/references/debugging-mindset.md +253 -0
  205. package/src/resources/skills/debug-like-expert/references/hypothesis-testing.md +373 -0
  206. package/src/resources/skills/debug-like-expert/references/investigation-techniques.md +337 -0
  207. package/src/resources/skills/debug-like-expert/references/verification-patterns.md +425 -0
  208. package/src/resources/skills/debug-like-expert/references/when-to-research.md +361 -0
  209. package/src/resources/skills/frontend-design/SKILL.md +45 -0
  210. package/src/resources/skills/swiftui/SKILL.md +208 -0
  211. package/src/resources/skills/swiftui/references/animations.md +921 -0
  212. package/src/resources/skills/swiftui/references/architecture.md +1561 -0
  213. package/src/resources/skills/swiftui/references/layout-system.md +1186 -0
  214. package/src/resources/skills/swiftui/references/navigation.md +1492 -0
  215. package/src/resources/skills/swiftui/references/networking-async.md +214 -0
  216. package/src/resources/skills/swiftui/references/performance.md +1706 -0
  217. package/src/resources/skills/swiftui/references/platform-integration.md +204 -0
  218. package/src/resources/skills/swiftui/references/state-management.md +1443 -0
  219. package/src/resources/skills/swiftui/references/swiftdata.md +297 -0
  220. package/src/resources/skills/swiftui/references/testing-debugging.md +247 -0
  221. package/src/resources/skills/swiftui/references/uikit-appkit-interop.md +218 -0
  222. package/src/resources/skills/swiftui/workflows/add-feature.md +191 -0
  223. package/src/resources/skills/swiftui/workflows/build-new-app.md +311 -0
  224. package/src/resources/skills/swiftui/workflows/debug-swiftui.md +192 -0
  225. package/src/resources/skills/swiftui/workflows/optimize-performance.md +197 -0
  226. package/src/resources/skills/swiftui/workflows/ship-app.md +203 -0
  227. package/src/resources/skills/swiftui/workflows/write-tests.md +235 -0
@@ -0,0 +1,392 @@
1
+ /**
2
+ * GSD Worktree Manager
3
+ *
4
+ * Creates and manages git worktrees under .gsd/worktrees/<name>/.
5
+ * Each worktree gets its own branch (worktree/<name>) and a full
6
+ * working copy of the project, enabling parallel work streams.
7
+ *
8
+ * The merge helper compares .gsd/ artifacts between a worktree and
9
+ * the main branch, then dispatches an LLM-guided merge flow.
10
+ *
11
+ * Flow:
12
+ * 1. create() — git worktree add .gsd/worktrees/<name> -b worktree/<name>
13
+ * 2. user works in the worktree (new plans, milestones, etc.)
14
+ * 3. merge() — LLM-guided reconciliation of .gsd/ artifacts back to main
15
+ * 4. remove() — git worktree remove + branch cleanup
16
+ */
17
+
18
+ import { existsSync, mkdirSync, realpathSync } from "node:fs";
19
+ import { execSync } from "node:child_process";
20
+ import { join, relative, resolve } from "node:path";
21
+
22
+ // ─── Types ─────────────────────────────────────────────────────────────────
23
+
24
+ export interface WorktreeInfo {
25
+ name: string;
26
+ path: string;
27
+ branch: string;
28
+ exists: boolean;
29
+ }
30
+
31
+ /** Per-file line change stats from git diff --numstat. */
32
+ export interface FileLineStat {
33
+ file: string;
34
+ added: number;
35
+ removed: number;
36
+ }
37
+
38
+ export interface WorktreeDiffSummary {
39
+ /** Files only in the worktree .gsd/ (new artifacts) */
40
+ added: string[];
41
+ /** Files in both but with different content */
42
+ modified: string[];
43
+ /** Files only in main .gsd/ (deleted in worktree) */
44
+ removed: string[];
45
+ }
46
+
47
+ // ─── Git Helpers ───────────────────────────────────────────────────────────
48
+
49
+ function runGit(cwd: string, args: string[], opts: { allowFailure?: boolean } = {}): string {
50
+ try {
51
+ return execSync(`git ${args.join(" ")}`, {
52
+ cwd,
53
+ stdio: ["ignore", "pipe", "pipe"],
54
+ encoding: "utf-8",
55
+ }).trim();
56
+ } catch (error) {
57
+ if (opts.allowFailure) return "";
58
+ const message = error instanceof Error ? error.message : String(error);
59
+ throw new Error(`git ${args.join(" ")} failed in ${cwd}: ${message}`);
60
+ }
61
+ }
62
+
63
+ export function getMainBranch(basePath: string): string {
64
+ const symbolic = runGit(basePath, ["symbolic-ref", "refs/remotes/origin/HEAD"], { allowFailure: true });
65
+ if (symbolic) {
66
+ const match = symbolic.match(/refs\/remotes\/origin\/(.+)$/);
67
+ if (match) return match[1]!;
68
+ }
69
+ if (runGit(basePath, ["show-ref", "--verify", "refs/heads/main"], { allowFailure: true })) return "main";
70
+ if (runGit(basePath, ["show-ref", "--verify", "refs/heads/master"], { allowFailure: true })) return "master";
71
+ return runGit(basePath, ["branch", "--show-current"]);
72
+ }
73
+
74
+ // ─── Path Helpers ──────────────────────────────────────────────────────────
75
+
76
+ export function worktreesDir(basePath: string): string {
77
+ return join(basePath, ".gsd", "worktrees");
78
+ }
79
+
80
+ export function worktreePath(basePath: string, name: string): string {
81
+ return join(worktreesDir(basePath), name);
82
+ }
83
+
84
+ export function worktreeBranchName(name: string): string {
85
+ return `worktree/${name}`;
86
+ }
87
+
88
+ // ─── Core Operations ───────────────────────────────────────────────────────
89
+
90
+ /**
91
+ * Create a new git worktree under .gsd/worktrees/<name>/ with branch worktree/<name>.
92
+ * The branch is created from the current HEAD of the main branch.
93
+ */
94
+ export function createWorktree(basePath: string, name: string): WorktreeInfo {
95
+ // Validate name: alphanumeric, hyphens, underscores only
96
+ if (!/^[a-zA-Z0-9_-]+$/.test(name)) {
97
+ throw new Error(`Invalid worktree name "${name}". Use only letters, numbers, hyphens, and underscores.`);
98
+ }
99
+
100
+ const wtPath = worktreePath(basePath, name);
101
+ const branch = worktreeBranchName(name);
102
+
103
+ if (existsSync(wtPath)) {
104
+ throw new Error(`Worktree "${name}" already exists at ${wtPath}`);
105
+ }
106
+
107
+ // Ensure the .gsd/worktrees/ directory exists
108
+ const wtDir = worktreesDir(basePath);
109
+ mkdirSync(wtDir, { recursive: true });
110
+
111
+ // Prune any stale worktree entries from a previous removal
112
+ runGit(basePath, ["worktree", "prune"], { allowFailure: true });
113
+
114
+ // Check if the branch already exists (leftover from a previous worktree)
115
+ const branchExists = runGit(basePath, ["show-ref", "--verify", `refs/heads/${branch}`], { allowFailure: true });
116
+ const mainBranch = getMainBranch(basePath);
117
+
118
+ if (branchExists) {
119
+ // Check if the branch is actively used by an existing worktree.
120
+ // `git branch -f` will fail if the branch is checked out somewhere.
121
+ const worktreeUsing = runGit(basePath, ["worktree", "list", "--porcelain"], { allowFailure: true });
122
+ const branchInUse = worktreeUsing.includes(`branch refs/heads/${branch}`);
123
+
124
+ if (branchInUse) {
125
+ throw new Error(
126
+ `Branch "${branch}" is already in use by another worktree. ` +
127
+ `Remove the existing worktree first with /worktree remove ${name}.`,
128
+ );
129
+ }
130
+
131
+ // Reset the stale branch to current main, then attach worktree to it
132
+ runGit(basePath, ["branch", "-f", branch, mainBranch]);
133
+ runGit(basePath, ["worktree", "add", wtPath, branch]);
134
+ } else {
135
+ runGit(basePath, ["worktree", "add", "-b", branch, wtPath, mainBranch]);
136
+ }
137
+
138
+ return {
139
+ name,
140
+ path: wtPath,
141
+ branch,
142
+ exists: true,
143
+ };
144
+ }
145
+
146
+ /**
147
+ * List all GSD-managed worktrees.
148
+ * Parses `git worktree list` and filters to those under .gsd/worktrees/.
149
+ */
150
+ export function listWorktrees(basePath: string): WorktreeInfo[] {
151
+ // Resolve real paths to handle symlinks (e.g. /tmp → /private/tmp on macOS)
152
+ const resolvedBase = existsSync(basePath) ? realpathSync(basePath) : resolve(basePath);
153
+ const wtDir = join(resolvedBase, ".gsd", "worktrees");
154
+ const rawList = runGit(basePath, ["worktree", "list", "--porcelain"]);
155
+
156
+ if (!rawList.trim()) return [];
157
+
158
+ const worktrees: WorktreeInfo[] = [];
159
+ const entries = rawList.split("\n\n").filter(Boolean);
160
+
161
+ for (const entry of entries) {
162
+ const lines = entry.split("\n");
163
+ const wtLine = lines.find(l => l.startsWith("worktree "));
164
+ const branchLine = lines.find(l => l.startsWith("branch "));
165
+
166
+ if (!wtLine || !branchLine) continue;
167
+
168
+ const entryPath = wtLine.replace("worktree ", "");
169
+ const branch = branchLine.replace("branch refs/heads/", "");
170
+
171
+ // Only include worktrees under .gsd/worktrees/
172
+ if (!entryPath.startsWith(wtDir)) continue;
173
+
174
+ const name = relative(wtDir, entryPath);
175
+ // Skip nested paths — only direct children
176
+ if (name.includes("/") || name.includes("\\")) continue;
177
+
178
+ worktrees.push({
179
+ name,
180
+ path: entryPath,
181
+ branch,
182
+ exists: existsSync(entryPath),
183
+ });
184
+ }
185
+
186
+ return worktrees;
187
+ }
188
+
189
+ /**
190
+ * Remove a worktree and optionally delete its branch.
191
+ * If the process is currently inside the worktree, chdir out first.
192
+ */
193
+ export function removeWorktree(
194
+ basePath: string,
195
+ name: string,
196
+ opts: { deleteBranch?: boolean; force?: boolean } = {},
197
+ ): void {
198
+ const wtPath = worktreePath(basePath, name);
199
+ const resolvedWtPath = existsSync(wtPath) ? realpathSync(wtPath) : wtPath;
200
+ const branch = worktreeBranchName(name);
201
+ const { deleteBranch = true, force = false } = opts;
202
+
203
+ // If we're inside the worktree, move out first — git can't remove an in-use directory
204
+ const cwd = process.cwd();
205
+ const resolvedCwd = existsSync(cwd) ? realpathSync(cwd) : cwd;
206
+ if (resolvedCwd === resolvedWtPath || resolvedCwd.startsWith(resolvedWtPath + "/")) {
207
+ process.chdir(basePath);
208
+ }
209
+
210
+ if (!existsSync(wtPath)) {
211
+ runGit(basePath, ["worktree", "prune"], { allowFailure: true });
212
+ if (deleteBranch) {
213
+ runGit(basePath, ["branch", "-D", branch], { allowFailure: true });
214
+ }
215
+ return;
216
+ }
217
+
218
+ // Force-remove to handle dirty worktrees
219
+ runGit(basePath, ["worktree", "remove", "--force", wtPath], { allowFailure: true });
220
+
221
+ // If the directory is still there (e.g. locked), try harder
222
+ if (existsSync(wtPath)) {
223
+ runGit(basePath, ["worktree", "remove", "--force", "--force", wtPath], { allowFailure: true });
224
+ }
225
+
226
+ // Prune stale entries so git knows the worktree is gone
227
+ runGit(basePath, ["worktree", "prune"], { allowFailure: true });
228
+
229
+ if (deleteBranch) {
230
+ runGit(basePath, ["branch", "-D", branch], { allowFailure: true });
231
+ }
232
+ }
233
+
234
+ /** Paths to skip in all worktree diffs (internal/runtime artifacts). */
235
+ const SKIP_PATHS = [".gsd/worktrees/", ".gsd/runtime/", ".gsd/activity/"];
236
+ const SKIP_EXACT = [".gsd/STATE.md", ".gsd/auto.lock", ".gsd/metrics.json"];
237
+
238
+ function shouldSkipPath(filePath: string): boolean {
239
+ if (SKIP_PATHS.some(p => filePath.startsWith(p))) return true;
240
+ if (SKIP_EXACT.includes(filePath)) return true;
241
+ return false;
242
+ }
243
+
244
+ function parseDiffNameStatus(diffOutput: string): WorktreeDiffSummary {
245
+ const added: string[] = [];
246
+ const modified: string[] = [];
247
+ const removed: string[] = [];
248
+
249
+ if (!diffOutput.trim()) return { added, modified, removed };
250
+
251
+ for (const line of diffOutput.split("\n").filter(Boolean)) {
252
+ const [status, ...pathParts] = line.split("\t");
253
+ const filePath = pathParts.join("\t");
254
+
255
+ if (shouldSkipPath(filePath)) continue;
256
+
257
+ switch (status) {
258
+ case "A": added.push(filePath); break;
259
+ case "M": modified.push(filePath); break;
260
+ case "D": removed.push(filePath); break;
261
+ default:
262
+ // Renames, copies — treat as modified
263
+ if (status?.startsWith("R") || status?.startsWith("C")) {
264
+ modified.push(filePath);
265
+ }
266
+ }
267
+ }
268
+
269
+ return { added, modified, removed };
270
+ }
271
+
272
+ /**
273
+ * Diff the .gsd/ directory between the worktree branch and main branch.
274
+ * Returns a summary of added, modified, and removed GSD artifacts.
275
+ */
276
+ export function diffWorktreeGSD(basePath: string, name: string): WorktreeDiffSummary {
277
+ const branch = worktreeBranchName(name);
278
+ const mainBranch = getMainBranch(basePath);
279
+
280
+ const diffOutput = runGit(basePath, [
281
+ "diff", "--name-status", `${mainBranch}...${branch}`, "--", ".gsd/",
282
+ ], { allowFailure: true });
283
+
284
+ return parseDiffNameStatus(diffOutput);
285
+ }
286
+
287
+ /**
288
+ * Diff ALL files between the worktree branch and main branch.
289
+ * Returns a summary of added, modified, and removed files across the entire repo.
290
+ */
291
+ /**
292
+ * Diff ALL files between the worktree branch and main branch.
293
+ * Uses direct diff (no merge-base) to show what will actually change
294
+ * on main when the merge is applied. If both branches have identical
295
+ * content, this correctly returns an empty diff.
296
+ */
297
+ export function diffWorktreeAll(basePath: string, name: string): WorktreeDiffSummary {
298
+ const branch = worktreeBranchName(name);
299
+ const mainBranch = getMainBranch(basePath);
300
+
301
+ const diffOutput = runGit(basePath, [
302
+ "diff", "--name-status", mainBranch, branch,
303
+ ], { allowFailure: true });
304
+
305
+ return parseDiffNameStatus(diffOutput);
306
+ }
307
+
308
+ /**
309
+ * Get per-file line addition/deletion stats for what will change on main.
310
+ * Uses direct diff (not merge-base) so the preview matches the actual merge outcome.
311
+ */
312
+ export function diffWorktreeNumstat(basePath: string, name: string): FileLineStat[] {
313
+ const branch = worktreeBranchName(name);
314
+ const mainBranch = getMainBranch(basePath);
315
+
316
+ const raw = runGit(basePath, [
317
+ "diff", "--numstat", mainBranch, branch,
318
+ ], { allowFailure: true });
319
+
320
+ if (!raw.trim()) return [];
321
+
322
+ const stats: FileLineStat[] = [];
323
+ for (const line of raw.split("\n").filter(Boolean)) {
324
+ const [a, r, ...pathParts] = line.split("\t");
325
+ const file = pathParts.join("\t");
326
+ if (shouldSkipPath(file)) continue;
327
+ const added = a === "-" ? 0 : parseInt(a ?? "0", 10);
328
+ const removed = r === "-" ? 0 : parseInt(r ?? "0", 10);
329
+ stats.push({ file, added, removed });
330
+ }
331
+ return stats;
332
+ }
333
+
334
+ /**
335
+ * Get the full diff content for .gsd/ between the worktree branch and main.
336
+ * Returns the raw unified diff for LLM consumption.
337
+ */
338
+ export function getWorktreeGSDDiff(basePath: string, name: string): string {
339
+ const branch = worktreeBranchName(name);
340
+ const mainBranch = getMainBranch(basePath);
341
+
342
+ return runGit(basePath, [
343
+ "diff", `${mainBranch}...${branch}`, "--", ".gsd/",
344
+ ], { allowFailure: true });
345
+ }
346
+
347
+ /**
348
+ * Get the full diff content for non-.gsd/ files between the worktree branch and main.
349
+ * Returns the raw unified diff for LLM consumption.
350
+ */
351
+ export function getWorktreeCodeDiff(basePath: string, name: string): string {
352
+ const branch = worktreeBranchName(name);
353
+ const mainBranch = getMainBranch(basePath);
354
+
355
+ // Get full diff, then exclude .gsd/ paths
356
+ // We use pathspec magic to exclude .gsd/
357
+ return runGit(basePath, [
358
+ "diff", `${mainBranch}...${branch}`, "--", ".", ":(exclude).gsd/",
359
+ ], { allowFailure: true });
360
+ }
361
+
362
+ /**
363
+ * Get commit log for the worktree branch since it diverged from main.
364
+ */
365
+ export function getWorktreeLog(basePath: string, name: string): string {
366
+ const branch = worktreeBranchName(name);
367
+ const mainBranch = getMainBranch(basePath);
368
+
369
+ return runGit(basePath, [
370
+ "log", "--oneline", `${mainBranch}..${branch}`,
371
+ ], { allowFailure: true });
372
+ }
373
+
374
+ /**
375
+ * Merge the worktree branch into main using squash merge.
376
+ * Must be called from the main working tree (not the worktree itself).
377
+ * Returns the merge commit message.
378
+ */
379
+ export function mergeWorktreeToMain(basePath: string, name: string, commitMessage: string): string {
380
+ const branch = worktreeBranchName(name);
381
+ const mainBranch = getMainBranch(basePath);
382
+ const current = runGit(basePath, ["branch", "--show-current"]);
383
+
384
+ if (current !== mainBranch) {
385
+ throw new Error(`Must be on ${mainBranch} to merge. Currently on ${current}.`);
386
+ }
387
+
388
+ runGit(basePath, ["merge", "--squash", branch]);
389
+ runGit(basePath, ["commit", "-m", commitMessage]);
390
+
391
+ return commitMessage;
392
+ }
@@ -0,0 +1,183 @@
1
+ /**
2
+ * GSD Slice Branch Management — Thin Facade
3
+ *
4
+ * Simple branch-per-slice workflow. No worktrees, no registry.
5
+ * Runtime state (metrics, activity, lock, STATE.md) is gitignored
6
+ * so branch switches are clean.
7
+ *
8
+ * All git-mutation functions delegate to GitServiceImpl from git-service.ts.
9
+ * Pure utility functions (detectWorktreeName, getSliceBranchName, parseSliceBranch,
10
+ * SLICE_BRANCH_RE) remain standalone.
11
+ *
12
+ * Flow:
13
+ * 1. ensureSliceBranch() — create + checkout slice branch
14
+ * 2. agent does work, commits
15
+ * 3. mergeSliceToMain() — checkout main, squash-merge, delete branch
16
+ */
17
+
18
+ import { sep } from "node:path";
19
+
20
+ import { GitServiceImpl } from "./git-service.ts";
21
+ import { loadEffectiveGSDPreferences } from "./preferences.ts";
22
+
23
+ // Re-export MergeSliceResult from the canonical source (D014 — type-only re-export)
24
+ export type { MergeSliceResult } from "./git-service.ts";
25
+
26
+ // ─── Lazy GitServiceImpl Cache ─────────────────────────────────────────────
27
+
28
+ let cachedService: GitServiceImpl | null = null;
29
+ let cachedBasePath: string | null = null;
30
+
31
+ /**
32
+ * Get or create a GitServiceImpl for the given basePath.
33
+ * Resets the cache if basePath changes between calls.
34
+ * Lazy construction: only instantiated at call-time, never at module-evaluation.
35
+ */
36
+ function getService(basePath: string): GitServiceImpl {
37
+ if (cachedService === null || cachedBasePath !== basePath) {
38
+ const loaded = loadEffectiveGSDPreferences();
39
+ const gitPrefs = loaded?.preferences?.git ?? {};
40
+ cachedService = new GitServiceImpl(basePath, gitPrefs);
41
+ cachedBasePath = basePath;
42
+ }
43
+ return cachedService;
44
+ }
45
+
46
+ // ─── Pure Utility Functions (unchanged) ────────────────────────────────────
47
+
48
+ /**
49
+ * Detect the active worktree name from the current working directory.
50
+ * Returns null if not inside a GSD worktree (.gsd/worktrees/<name>/).
51
+ */
52
+ export function detectWorktreeName(basePath: string): string | null {
53
+ const marker = `${sep}.gsd${sep}worktrees${sep}`;
54
+ const idx = basePath.indexOf(marker);
55
+ if (idx === -1) return null;
56
+ const afterMarker = basePath.slice(idx + marker.length);
57
+ const name = afterMarker.split(sep)[0] ?? afterMarker.split("/")[0];
58
+ return name || null;
59
+ }
60
+
61
+ /**
62
+ * Get the slice branch name, namespaced by worktree when inside one.
63
+ *
64
+ * In the main tree: gsd/<milestoneId>/<sliceId>
65
+ * In a worktree: gsd/<worktreeName>/<milestoneId>/<sliceId>
66
+ *
67
+ * This prevents branch conflicts when multiple worktrees work on the
68
+ * same milestone/slice IDs — git doesn't allow a branch to be checked
69
+ * out in more than one worktree simultaneously.
70
+ */
71
+ export function getSliceBranchName(milestoneId: string, sliceId: string, worktreeName?: string | null): string {
72
+ if (worktreeName) {
73
+ return `gsd/${worktreeName}/${milestoneId}/${sliceId}`;
74
+ }
75
+ return `gsd/${milestoneId}/${sliceId}`;
76
+ }
77
+
78
+ /** Regex that matches both plain and worktree-namespaced slice branches. */
79
+ export const SLICE_BRANCH_RE = /^gsd\/(?:([a-zA-Z0-9_-]+)\/)?(M\d+)\/(S\d+)$/;
80
+
81
+ /**
82
+ * Parse a slice branch name into its components.
83
+ * Handles both `gsd/M001/S01` and `gsd/myworktree/M001/S01`.
84
+ */
85
+ export function parseSliceBranch(branchName: string): {
86
+ worktreeName: string | null;
87
+ milestoneId: string;
88
+ sliceId: string;
89
+ } | null {
90
+ const match = branchName.match(SLICE_BRANCH_RE);
91
+ if (!match) return null;
92
+ return {
93
+ worktreeName: match[1] ?? null,
94
+ milestoneId: match[2]!,
95
+ sliceId: match[3]!,
96
+ };
97
+ }
98
+
99
+ // ─── Git-Mutation Functions (delegate to GitServiceImpl) ───────────────────
100
+
101
+ /**
102
+ * Get the "main" branch for GSD slice operations.
103
+ *
104
+ * In the main working tree: returns main/master (the repo's default branch).
105
+ * In a worktree: returns worktree/<name> — the worktree's own base branch.
106
+ *
107
+ * This is critical because git doesn't allow a branch to be checked out
108
+ * in more than one worktree. Slice branches merge into the worktree's base
109
+ * branch, and the worktree branch later merges into the real main via
110
+ * /worktree merge.
111
+ */
112
+ export function getMainBranch(basePath: string): string {
113
+ return getService(basePath).getMainBranch();
114
+ }
115
+
116
+ export function getCurrentBranch(basePath: string): string {
117
+ return getService(basePath).getCurrentBranch();
118
+ }
119
+
120
+ /**
121
+ * Ensure the slice branch exists and is checked out.
122
+ * Creates the branch from the current branch if it's not a slice branch,
123
+ * otherwise from main. This preserves planning artifacts (CONTEXT, ROADMAP,
124
+ * etc.) that were committed on the working branch — which may differ from
125
+ * the repo's default branch (e.g. `developer` vs `main`).
126
+ * When inside a worktree, the branch is namespaced to avoid conflicts.
127
+ * Returns true if the branch was newly created.
128
+ */
129
+ export function ensureSliceBranch(basePath: string, milestoneId: string, sliceId: string): boolean {
130
+ return getService(basePath).ensureSliceBranch(milestoneId, sliceId);
131
+ }
132
+
133
+ /**
134
+ * Auto-commit any dirty files in the current working tree.
135
+ * Returns the commit message used, or null if already clean.
136
+ */
137
+ export function autoCommitCurrentBranch(
138
+ basePath: string, unitType: string, unitId: string,
139
+ ): string | null {
140
+ return getService(basePath).autoCommit(unitType, unitId);
141
+ }
142
+
143
+ /**
144
+ * Switch to main, auto-committing any dirty files on the current branch first.
145
+ */
146
+ export function switchToMain(basePath: string): void {
147
+ getService(basePath).switchToMain();
148
+ }
149
+
150
+ /**
151
+ * Squash-merge a completed slice branch to main.
152
+ * Expects to already be on main (call switchToMain first).
153
+ * Deletes the branch after merge.
154
+ */
155
+ export function mergeSliceToMain(
156
+ basePath: string, milestoneId: string, sliceId: string, sliceTitle: string,
157
+ ): import("./git-service.ts").MergeSliceResult {
158
+ return getService(basePath).mergeSliceToMain(milestoneId, sliceId, sliceTitle);
159
+ }
160
+
161
+ // ─── Query Functions (delegate to GitServiceImpl) ──────────────────────────
162
+
163
+ /**
164
+ * Check if we're currently on a slice branch (not main).
165
+ * Handles both plain (gsd/M001/S01) and worktree-namespaced (gsd/wt/M001/S01) branches.
166
+ */
167
+ export function isOnSliceBranch(basePath: string): boolean {
168
+ const current = getCurrentBranch(basePath);
169
+ return SLICE_BRANCH_RE.test(current);
170
+ }
171
+
172
+ /**
173
+ * Get the active slice branch name, or null if on main.
174
+ * Handles both plain and worktree-namespaced branch patterns.
175
+ */
176
+ export function getActiveSliceBranch(basePath: string): string | null {
177
+ try {
178
+ const current = getCurrentBranch(basePath);
179
+ return SLICE_BRANCH_RE.test(current) ? current : null;
180
+ } catch {
181
+ return null;
182
+ }
183
+ }