gsd-pi 2.37.1-dev.d3ace49 → 2.38.0-dev.361f5e3

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 (168) hide show
  1. package/dist/app-paths.js +1 -1
  2. package/dist/cli.js +9 -0
  3. package/dist/extension-discovery.d.ts +5 -3
  4. package/dist/extension-discovery.js +14 -9
  5. package/dist/extension-registry.js +2 -2
  6. package/dist/remote-questions-config.js +2 -2
  7. package/dist/resources/extensions/browser-tools/package.json +3 -1
  8. package/dist/resources/extensions/cmux/index.js +55 -1
  9. package/dist/resources/extensions/context7/package.json +1 -1
  10. package/dist/resources/extensions/env-utils.js +29 -0
  11. package/dist/resources/extensions/get-secrets-from-user.js +5 -24
  12. package/dist/resources/extensions/github-sync/cli.js +284 -0
  13. package/dist/resources/extensions/github-sync/index.js +73 -0
  14. package/dist/resources/extensions/github-sync/mapping.js +67 -0
  15. package/dist/resources/extensions/github-sync/sync.js +424 -0
  16. package/dist/resources/extensions/github-sync/templates.js +118 -0
  17. package/dist/resources/extensions/github-sync/types.js +7 -0
  18. package/dist/resources/extensions/google-search/package.json +3 -1
  19. package/dist/resources/extensions/gsd/auto/session.js +6 -23
  20. package/dist/resources/extensions/gsd/auto-dispatch.js +7 -8
  21. package/dist/resources/extensions/gsd/auto-loop.js +149 -170
  22. package/dist/resources/extensions/gsd/auto-post-unit.js +92 -70
  23. package/dist/resources/extensions/gsd/auto-prompts.js +7 -31
  24. package/dist/resources/extensions/gsd/auto-start.js +13 -2
  25. package/dist/resources/extensions/gsd/auto-worktree-sync.js +13 -5
  26. package/dist/resources/extensions/gsd/auto.js +143 -96
  27. package/dist/resources/extensions/gsd/captures.js +9 -1
  28. package/dist/resources/extensions/gsd/commands-extensions.js +3 -2
  29. package/dist/resources/extensions/gsd/commands-handlers.js +16 -3
  30. package/dist/resources/extensions/gsd/commands-prefs-wizard.js +1 -1
  31. package/dist/resources/extensions/gsd/commands.js +22 -2
  32. package/dist/resources/extensions/gsd/context-budget.js +2 -10
  33. package/dist/resources/extensions/gsd/detection.js +1 -2
  34. package/dist/resources/extensions/gsd/docs/preferences-reference.md +0 -2
  35. package/dist/resources/extensions/gsd/doctor-checks.js +82 -0
  36. package/dist/resources/extensions/gsd/doctor-environment.js +78 -0
  37. package/dist/resources/extensions/gsd/doctor-format.js +15 -0
  38. package/dist/resources/extensions/gsd/doctor-providers.js +27 -11
  39. package/dist/resources/extensions/gsd/doctor.js +184 -11
  40. package/dist/resources/extensions/gsd/export.js +1 -1
  41. package/dist/resources/extensions/gsd/files.js +2 -2
  42. package/dist/resources/extensions/gsd/forensics.js +1 -1
  43. package/dist/resources/extensions/gsd/git-service.js +8 -1
  44. package/dist/resources/extensions/gsd/index.js +2 -1
  45. package/dist/resources/extensions/gsd/migrate/parsers.js +1 -1
  46. package/dist/resources/extensions/gsd/package.json +1 -1
  47. package/dist/resources/extensions/gsd/preferences-models.js +0 -12
  48. package/dist/resources/extensions/gsd/preferences-types.js +1 -1
  49. package/dist/resources/extensions/gsd/preferences-validation.js +59 -11
  50. package/dist/resources/extensions/gsd/preferences.js +8 -5
  51. package/dist/resources/extensions/gsd/prompts/discuss.md +11 -14
  52. package/dist/resources/extensions/gsd/prompts/execute-task.md +2 -2
  53. package/dist/resources/extensions/gsd/prompts/guided-discuss-milestone.md +11 -12
  54. package/dist/resources/extensions/gsd/prompts/guided-discuss-slice.md +8 -10
  55. package/dist/resources/extensions/gsd/prompts/guided-resume-task.md +1 -1
  56. package/dist/resources/extensions/gsd/prompts/queue.md +4 -8
  57. package/dist/resources/extensions/gsd/prompts/reactive-execute.md +11 -8
  58. package/dist/resources/extensions/gsd/prompts/run-uat.md +25 -10
  59. package/dist/resources/extensions/gsd/prompts/workflow-start.md +2 -2
  60. package/dist/resources/extensions/gsd/repo-identity.js +21 -4
  61. package/dist/resources/extensions/gsd/resource-version.js +2 -1
  62. package/dist/resources/extensions/gsd/state.js +1 -1
  63. package/dist/resources/extensions/gsd/visualizer-data.js +1 -1
  64. package/dist/resources/extensions/gsd/worktree.js +35 -16
  65. package/dist/resources/extensions/remote-questions/status.js +2 -1
  66. package/dist/resources/extensions/remote-questions/store.js +2 -1
  67. package/dist/resources/extensions/search-the-web/provider.js +2 -1
  68. package/dist/resources/extensions/subagent/index.js +12 -3
  69. package/dist/resources/extensions/subagent/isolation.js +2 -1
  70. package/dist/resources/extensions/ttsr/rule-loader.js +2 -1
  71. package/dist/resources/extensions/universal-config/package.json +1 -1
  72. package/dist/welcome-screen.d.ts +12 -0
  73. package/dist/welcome-screen.js +53 -0
  74. package/package.json +1 -1
  75. package/packages/pi-coding-agent/dist/core/package-manager.d.ts.map +1 -1
  76. package/packages/pi-coding-agent/dist/core/package-manager.js +8 -4
  77. package/packages/pi-coding-agent/dist/core/package-manager.js.map +1 -1
  78. package/packages/pi-coding-agent/package.json +1 -1
  79. package/packages/pi-coding-agent/src/core/package-manager.ts +8 -4
  80. package/pkg/package.json +1 -1
  81. package/src/resources/extensions/cmux/index.ts +57 -1
  82. package/src/resources/extensions/env-utils.ts +31 -0
  83. package/src/resources/extensions/get-secrets-from-user.ts +5 -24
  84. package/src/resources/extensions/github-sync/cli.ts +364 -0
  85. package/src/resources/extensions/github-sync/index.ts +93 -0
  86. package/src/resources/extensions/github-sync/mapping.ts +81 -0
  87. package/src/resources/extensions/github-sync/sync.ts +556 -0
  88. package/src/resources/extensions/github-sync/templates.ts +183 -0
  89. package/src/resources/extensions/github-sync/tests/cli.test.ts +20 -0
  90. package/src/resources/extensions/github-sync/tests/commit-linking.test.ts +39 -0
  91. package/src/resources/extensions/github-sync/tests/mapping.test.ts +104 -0
  92. package/src/resources/extensions/github-sync/tests/templates.test.ts +110 -0
  93. package/src/resources/extensions/github-sync/types.ts +47 -0
  94. package/src/resources/extensions/gsd/auto/session.ts +7 -25
  95. package/src/resources/extensions/gsd/auto-dispatch.ts +6 -8
  96. package/src/resources/extensions/gsd/auto-loop.ts +207 -252
  97. package/src/resources/extensions/gsd/auto-post-unit.ts +69 -41
  98. package/src/resources/extensions/gsd/auto-prompts.ts +7 -33
  99. package/src/resources/extensions/gsd/auto-start.ts +18 -2
  100. package/src/resources/extensions/gsd/auto-worktree-sync.ts +15 -4
  101. package/src/resources/extensions/gsd/auto.ts +139 -101
  102. package/src/resources/extensions/gsd/captures.ts +10 -1
  103. package/src/resources/extensions/gsd/commands-extensions.ts +4 -2
  104. package/src/resources/extensions/gsd/commands-handlers.ts +17 -2
  105. package/src/resources/extensions/gsd/commands-prefs-wizard.ts +1 -1
  106. package/src/resources/extensions/gsd/commands.ts +24 -2
  107. package/src/resources/extensions/gsd/context-budget.ts +2 -12
  108. package/src/resources/extensions/gsd/detection.ts +2 -2
  109. package/src/resources/extensions/gsd/docs/preferences-reference.md +0 -2
  110. package/src/resources/extensions/gsd/doctor-checks.ts +75 -0
  111. package/src/resources/extensions/gsd/doctor-environment.ts +82 -1
  112. package/src/resources/extensions/gsd/doctor-format.ts +20 -0
  113. package/src/resources/extensions/gsd/doctor-providers.ts +26 -9
  114. package/src/resources/extensions/gsd/doctor-types.ts +16 -1
  115. package/src/resources/extensions/gsd/doctor.ts +177 -13
  116. package/src/resources/extensions/gsd/export.ts +1 -1
  117. package/src/resources/extensions/gsd/files.ts +2 -2
  118. package/src/resources/extensions/gsd/forensics.ts +1 -1
  119. package/src/resources/extensions/gsd/git-service.ts +13 -1
  120. package/src/resources/extensions/gsd/index.ts +3 -1
  121. package/src/resources/extensions/gsd/migrate/parsers.ts +1 -1
  122. package/src/resources/extensions/gsd/preferences-models.ts +0 -12
  123. package/src/resources/extensions/gsd/preferences-types.ts +4 -4
  124. package/src/resources/extensions/gsd/preferences-validation.ts +51 -11
  125. package/src/resources/extensions/gsd/preferences.ts +8 -5
  126. package/src/resources/extensions/gsd/prompts/discuss.md +11 -14
  127. package/src/resources/extensions/gsd/prompts/execute-task.md +2 -2
  128. package/src/resources/extensions/gsd/prompts/guided-discuss-milestone.md +11 -12
  129. package/src/resources/extensions/gsd/prompts/guided-discuss-slice.md +8 -10
  130. package/src/resources/extensions/gsd/prompts/guided-resume-task.md +1 -1
  131. package/src/resources/extensions/gsd/prompts/queue.md +4 -8
  132. package/src/resources/extensions/gsd/prompts/reactive-execute.md +11 -8
  133. package/src/resources/extensions/gsd/prompts/run-uat.md +25 -10
  134. package/src/resources/extensions/gsd/prompts/workflow-start.md +2 -2
  135. package/src/resources/extensions/gsd/repo-identity.ts +23 -4
  136. package/src/resources/extensions/gsd/resource-version.ts +3 -1
  137. package/src/resources/extensions/gsd/state.ts +1 -1
  138. package/src/resources/extensions/gsd/tests/agent-end-retry.test.ts +21 -18
  139. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +16 -37
  140. package/src/resources/extensions/gsd/tests/cmux.test.ts +93 -0
  141. package/src/resources/extensions/gsd/tests/doctor-enhancements.test.ts +266 -0
  142. package/src/resources/extensions/gsd/tests/doctor-providers.test.ts +86 -3
  143. package/src/resources/extensions/gsd/tests/preferences.test.ts +2 -7
  144. package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +59 -0
  145. package/src/resources/extensions/gsd/tests/repo-identity-worktree.test.ts +21 -1
  146. package/src/resources/extensions/gsd/tests/run-uat.test.ts +11 -3
  147. package/src/resources/extensions/gsd/tests/worktree.test.ts +47 -0
  148. package/src/resources/extensions/gsd/types.ts +0 -1
  149. package/src/resources/extensions/gsd/visualizer-data.ts +1 -1
  150. package/src/resources/extensions/gsd/worktree.ts +35 -15
  151. package/src/resources/extensions/remote-questions/status.ts +3 -1
  152. package/src/resources/extensions/remote-questions/store.ts +3 -1
  153. package/src/resources/extensions/search-the-web/provider.ts +2 -1
  154. package/src/resources/extensions/subagent/index.ts +12 -3
  155. package/src/resources/extensions/subagent/isolation.ts +3 -1
  156. package/src/resources/extensions/ttsr/rule-loader.ts +3 -1
  157. package/dist/resources/extensions/gsd/prompt-compressor.js +0 -393
  158. package/dist/resources/extensions/gsd/semantic-chunker.js +0 -254
  159. package/dist/resources/extensions/gsd/summary-distiller.js +0 -212
  160. package/src/resources/extensions/gsd/prompt-compressor.ts +0 -508
  161. package/src/resources/extensions/gsd/semantic-chunker.ts +0 -336
  162. package/src/resources/extensions/gsd/summary-distiller.ts +0 -258
  163. package/src/resources/extensions/gsd/tests/context-compression.test.ts +0 -193
  164. package/src/resources/extensions/gsd/tests/prompt-compressor.test.ts +0 -529
  165. package/src/resources/extensions/gsd/tests/semantic-chunker.test.ts +0 -426
  166. package/src/resources/extensions/gsd/tests/summary-distiller.test.ts +0 -323
  167. package/src/resources/extensions/gsd/tests/token-optimization-benchmark.test.ts +0 -1272
  168. package/src/resources/extensions/gsd/tests/token-optimization-prefs.test.ts +0 -164
@@ -289,10 +289,17 @@ export class CmuxClient {
289
289
  }
290
290
 
291
291
  async createSplit(direction: "right" | "down" | "left" | "up"): Promise<string | null> {
292
+ return this.createSplitFrom(this.config.surfaceId, direction);
293
+ }
294
+
295
+ async createSplitFrom(
296
+ sourceSurfaceId: string | undefined,
297
+ direction: "right" | "down" | "left" | "up",
298
+ ): Promise<string | null> {
292
299
  if (!this.config.splits) return null;
293
300
  const before = new Set(await this.listSurfaceIds());
294
301
  const args = ["new-split", direction];
295
- const scopedArgs = this.appendSurface(this.appendWorkspace(args), this.config.surfaceId);
302
+ const scopedArgs = this.appendSurface(this.appendWorkspace(args), sourceSurfaceId);
296
303
  await this.runAsync(scopedArgs);
297
304
  const after = await this.listSurfaceIds();
298
305
  for (const id of after) {
@@ -301,6 +308,55 @@ export class CmuxClient {
301
308
  return null;
302
309
  }
303
310
 
311
+ /**
312
+ * Create a grid of surfaces for parallel agent execution.
313
+ *
314
+ * Layout strategy (gsd stays in the original surface):
315
+ * 1 agent: [gsd | A]
316
+ * 2 agents: [gsd | A]
317
+ * [ | B]
318
+ * 3 agents: [gsd | A]
319
+ * [ C | B]
320
+ * 4 agents: [gsd | A]
321
+ * [ C | B] (D splits from B downward)
322
+ * [ | D]
323
+ *
324
+ * Returns surface IDs in order, or empty array on failure.
325
+ */
326
+ async createGridLayout(count: number): Promise<string[]> {
327
+ if (!this.config.splits || count <= 0) return [];
328
+ const surfaces: string[] = [];
329
+
330
+ // First split: create right column from the gsd surface
331
+ const rightCol = await this.createSplitFrom(this.config.surfaceId, "right");
332
+ if (!rightCol) return [];
333
+ surfaces.push(rightCol);
334
+ if (count === 1) return surfaces;
335
+
336
+ // Second split: split right column down → bottom-right
337
+ const bottomRight = await this.createSplitFrom(rightCol, "down");
338
+ if (!bottomRight) return surfaces;
339
+ surfaces.push(bottomRight);
340
+ if (count === 2) return surfaces;
341
+
342
+ // Third split: split gsd surface down → bottom-left
343
+ const bottomLeft = await this.createSplitFrom(this.config.surfaceId, "down");
344
+ if (!bottomLeft) return surfaces;
345
+ surfaces.push(bottomLeft);
346
+ if (count === 3) return surfaces;
347
+
348
+ // Fourth+: split subsequent surfaces down from the last created
349
+ let lastSurface = bottomRight;
350
+ for (let i = 3; i < count; i++) {
351
+ const next = await this.createSplitFrom(lastSurface, "down");
352
+ if (!next) break;
353
+ surfaces.push(next);
354
+ lastSurface = next;
355
+ }
356
+
357
+ return surfaces;
358
+ }
359
+
304
360
  async sendSurface(surfaceId: string, text: string): Promise<boolean> {
305
361
  const payload = text.endsWith("\n") ? text : `${text}\n`;
306
362
  const stdout = await this.runAsync(["send-surface", "--surface", surfaceId, payload]);
@@ -0,0 +1,31 @@
1
+ // GSD Extension — Environment variable utilities
2
+ // Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
3
+ //
4
+ // Pure utility for checking existing env keys in .env files and process.env.
5
+ // Extracted from get-secrets-from-user.ts to avoid pulling in @gsd/pi-tui
6
+ // when only env-checking is needed (e.g. from files.ts during report generation).
7
+
8
+ import { readFile } from "node:fs/promises";
9
+
10
+ /**
11
+ * Check which keys already exist in a .env file or process.env.
12
+ * Returns the subset of `keys` that are already set.
13
+ */
14
+ export async function checkExistingEnvKeys(keys: string[], envFilePath: string): Promise<string[]> {
15
+ let fileContent = "";
16
+ try {
17
+ fileContent = await readFile(envFilePath, "utf8");
18
+ } catch {
19
+ // ENOENT or other read error — proceed with empty content
20
+ }
21
+
22
+ const existing: string[] = [];
23
+ for (const key of keys) {
24
+ const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
25
+ const regex = new RegExp(`^${escaped}\\s*=`, "m");
26
+ if (regex.test(fileContent) || key in process.env) {
27
+ existing.push(key);
28
+ }
29
+ }
30
+ return existing;
31
+ }
@@ -67,30 +67,11 @@ async function writeEnvKey(filePath: string, key: string, value: string): Promis
67
67
 
68
68
  // ─── Exported utilities ───────────────────────────────────────────────────────
69
69
 
70
- /**
71
- * Check which keys already exist in the .env file or process.env.
72
- * Returns the subset of `keys` that are already set.
73
- * Handles ENOENT gracefully (still checks process.env).
74
- * Empty-string values count as existing.
75
- */
76
- export async function checkExistingEnvKeys(keys: string[], envFilePath: string): Promise<string[]> {
77
- let fileContent = "";
78
- try {
79
- fileContent = await readFile(envFilePath, "utf8");
80
- } catch {
81
- // ENOENT or other read error — proceed with empty content
82
- }
83
-
84
- const existing: string[] = [];
85
- for (const key of keys) {
86
- const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
87
- const regex = new RegExp(`^${escaped}\\s*=`, "m");
88
- if (regex.test(fileContent) || key in process.env) {
89
- existing.push(key);
90
- }
91
- }
92
- return existing;
93
- }
70
+ // Re-export from env-utils.ts so existing consumers still work.
71
+ // The implementation lives in env-utils.ts to avoid pulling @gsd/pi-tui
72
+ // into modules that only need env-checking (e.g. files.ts during reports).
73
+ import { checkExistingEnvKeys } from "./env-utils.js";
74
+ export { checkExistingEnvKeys };
94
75
 
95
76
  /**
96
77
  * Detect the write destination based on project files in basePath.
@@ -0,0 +1,364 @@
1
+ /**
2
+ * Thin wrapper around the `gh` CLI.
3
+ *
4
+ * Every public function returns `GhResult<T>` — never throws.
5
+ * Uses `execFileSync` (not `execSync`) for safety.
6
+ */
7
+
8
+ import { execFileSync } from "node:child_process";
9
+
10
+ // ─── Result Type ────────────────────────────────────────────────────────────
11
+
12
+ export interface GhResult<T> {
13
+ ok: boolean;
14
+ data?: T;
15
+ error?: string;
16
+ }
17
+
18
+ function ok<T>(data: T): GhResult<T> {
19
+ return { ok: true, data };
20
+ }
21
+
22
+ function fail<T>(error: string): GhResult<T> {
23
+ return { ok: false, error };
24
+ }
25
+
26
+ // ─── gh Availability ────────────────────────────────────────────────────────
27
+
28
+ let _ghAvailable: boolean | null = null;
29
+
30
+ export function ghIsAvailable(): boolean {
31
+ if (_ghAvailable !== null) return _ghAvailable;
32
+ try {
33
+ execFileSync("gh", ["--version"], {
34
+ encoding: "utf-8",
35
+ stdio: ["ignore", "pipe", "ignore"],
36
+ timeout: 5_000,
37
+ });
38
+ _ghAvailable = true;
39
+ } catch {
40
+ _ghAvailable = false;
41
+ }
42
+ return _ghAvailable;
43
+ }
44
+
45
+ /** Reset cached availability (for testing). */
46
+ export function _resetGhCache(): void {
47
+ _ghAvailable = null;
48
+ }
49
+
50
+ // ─── Rate Limit Check ───────────────────────────────────────────────────────
51
+
52
+ let _rateLimitCheckedAt = 0;
53
+ let _rateLimitOk = true;
54
+ const RATE_LIMIT_CHECK_INTERVAL_MS = 300_000; // 5 minutes
55
+
56
+ export function ghHasRateLimit(cwd: string): boolean {
57
+ const now = Date.now();
58
+ if (now - _rateLimitCheckedAt < RATE_LIMIT_CHECK_INTERVAL_MS) return _rateLimitOk;
59
+ _rateLimitCheckedAt = now;
60
+ try {
61
+ const raw = execFileSync("gh", ["api", "rate_limit", "--jq", ".rate.remaining"], {
62
+ cwd,
63
+ encoding: "utf-8",
64
+ stdio: ["ignore", "pipe", "ignore"],
65
+ timeout: 10_000,
66
+ }).trim();
67
+ const remaining = parseInt(raw, 10);
68
+ _rateLimitOk = Number.isFinite(remaining) && remaining >= 100;
69
+ } catch {
70
+ // Can't check — assume OK so we don't silently disable sync
71
+ _rateLimitOk = true;
72
+ }
73
+ return _rateLimitOk;
74
+ }
75
+
76
+ // ─── Helpers ────────────────────────────────────────────────────────────────
77
+
78
+ const GH_TIMEOUT = 15_000;
79
+ const MAX_BODY_LENGTH = 65_000;
80
+
81
+ function truncateBody(body: string): string {
82
+ if (body.length <= MAX_BODY_LENGTH) return body;
83
+ return body.slice(0, MAX_BODY_LENGTH) + "\n\n---\n*Body truncated (exceeded 65K characters)*";
84
+ }
85
+
86
+ function runGh(args: string[], cwd: string): GhResult<string> {
87
+ try {
88
+ const stdout = execFileSync("gh", args, {
89
+ cwd,
90
+ encoding: "utf-8",
91
+ stdio: ["ignore", "pipe", "pipe"],
92
+ timeout: GH_TIMEOUT,
93
+ }).trim();
94
+ return ok(stdout);
95
+ } catch (err) {
96
+ const msg = err instanceof Error ? err.message : String(err);
97
+ return fail(msg);
98
+ }
99
+ }
100
+
101
+ function runGhJson<T>(args: string[], cwd: string): GhResult<T> {
102
+ const result = runGh(args, cwd);
103
+ if (!result.ok) return fail(result.error!);
104
+ try {
105
+ return ok(JSON.parse(result.data!) as T);
106
+ } catch {
107
+ return fail(`Failed to parse JSON: ${result.data}`);
108
+ }
109
+ }
110
+
111
+ // ─── Repo Detection ─────────────────────────────────────────────────────────
112
+
113
+ export function ghDetectRepo(cwd: string): GhResult<string> {
114
+ const result = runGh(
115
+ ["repo", "view", "--json", "nameWithOwner", "--jq", ".nameWithOwner"],
116
+ cwd,
117
+ );
118
+ if (!result.ok) return fail(result.error!);
119
+ const repo = result.data!.trim();
120
+ if (!repo || !repo.includes("/")) return fail("Could not detect repo");
121
+ return ok(repo);
122
+ }
123
+
124
+ // ─── Issues ─────────────────────────────────────────────────────────────────
125
+
126
+ export interface CreateIssueOpts {
127
+ repo: string;
128
+ title: string;
129
+ body: string;
130
+ labels?: string[];
131
+ milestone?: number;
132
+ parentIssue?: number;
133
+ }
134
+
135
+ export function ghCreateIssue(cwd: string, opts: CreateIssueOpts): GhResult<number> {
136
+ const args = [
137
+ "issue", "create",
138
+ "--repo", opts.repo,
139
+ "--title", opts.title,
140
+ "--body", truncateBody(opts.body),
141
+ ];
142
+ if (opts.labels?.length) {
143
+ args.push("--label", opts.labels.join(","));
144
+ }
145
+ if (opts.milestone) {
146
+ args.push("--milestone", String(opts.milestone));
147
+ }
148
+
149
+ const result = runGh(args, cwd);
150
+ if (!result.ok) return fail(result.error!);
151
+
152
+ // gh issue create returns the URL; extract issue number
153
+ const match = result.data!.match(/\/issues\/(\d+)/);
154
+ if (!match) return fail(`Could not parse issue number from: ${result.data}`);
155
+ const issueNumber = parseInt(match[1], 10);
156
+
157
+ // If parent specified, add as sub-issue via GraphQL
158
+ if (opts.parentIssue) {
159
+ ghAddSubIssue(cwd, opts.repo, opts.parentIssue, issueNumber);
160
+ }
161
+
162
+ return ok(issueNumber);
163
+ }
164
+
165
+ export function ghCloseIssue(cwd: string, repo: string, issueNumber: number, comment?: string): GhResult<void> {
166
+ if (comment) {
167
+ ghAddComment(cwd, repo, issueNumber, comment);
168
+ }
169
+ const result = runGh(
170
+ ["issue", "close", String(issueNumber), "--repo", repo],
171
+ cwd,
172
+ );
173
+ if (!result.ok) return fail(result.error!);
174
+ return ok(undefined);
175
+ }
176
+
177
+ export function ghAddComment(cwd: string, repo: string, issueNumber: number, body: string): GhResult<void> {
178
+ const result = runGh(
179
+ ["issue", "comment", String(issueNumber), "--repo", repo, "--body", truncateBody(body)],
180
+ cwd,
181
+ );
182
+ if (!result.ok) return fail(result.error!);
183
+ return ok(undefined);
184
+ }
185
+
186
+ // ─── Sub-Issues (GraphQL) ───────────────────────────────────────────────────
187
+
188
+ function ghAddSubIssue(cwd: string, repo: string, parentNumber: number, childNumber: number): GhResult<void> {
189
+ // Get node IDs for both issues
190
+ const parentResult = runGhJson<{ id: string }>(
191
+ ["api", `repos/${repo}/issues/${parentNumber}`, "--jq", "{id: .node_id}"],
192
+ cwd,
193
+ );
194
+ const childResult = runGhJson<{ id: string }>(
195
+ ["api", `repos/${repo}/issues/${childNumber}`, "--jq", "{id: .node_id}"],
196
+ cwd,
197
+ );
198
+
199
+ if (!parentResult.ok || !childResult.ok) {
200
+ return fail("Could not resolve issue node IDs for sub-issue linking");
201
+ }
202
+
203
+ const mutation = `mutation { addSubIssue(input: { issueId: "${parentResult.data!.id}", subIssueId: "${childResult.data!.id}" }) { issue { id } } }`;
204
+ return runGh(["api", "graphql", "-f", `query=${mutation}`], cwd) as GhResult<void>;
205
+ }
206
+
207
+ // ─── Milestones ─────────────────────────────────────────────────────────────
208
+
209
+ export function ghCreateMilestone(cwd: string, repo: string, title: string, description: string): GhResult<number> {
210
+ const result = runGhJson<{ number: number }>(
211
+ [
212
+ "api", `repos/${repo}/milestones`,
213
+ "-X", "POST",
214
+ "-f", `title=${title}`,
215
+ "-f", `description=${truncateBody(description)}`,
216
+ "-f", "state=open",
217
+ "--jq", "{number: .number}",
218
+ ],
219
+ cwd,
220
+ );
221
+ if (!result.ok) return fail(result.error!);
222
+ return ok(result.data!.number);
223
+ }
224
+
225
+ export function ghCloseMilestone(cwd: string, repo: string, milestoneNumber: number): GhResult<void> {
226
+ const result = runGh(
227
+ [
228
+ "api", `repos/${repo}/milestones/${milestoneNumber}`,
229
+ "-X", "PATCH",
230
+ "-f", "state=closed",
231
+ ],
232
+ cwd,
233
+ );
234
+ if (!result.ok) return fail(result.error!);
235
+ return ok(undefined);
236
+ }
237
+
238
+ // ─── Pull Requests ──────────────────────────────────────────────────────────
239
+
240
+ export interface CreatePROpts {
241
+ repo: string;
242
+ base: string;
243
+ head: string;
244
+ title: string;
245
+ body: string;
246
+ draft?: boolean;
247
+ }
248
+
249
+ export function ghCreatePR(cwd: string, opts: CreatePROpts): GhResult<number> {
250
+ const args = [
251
+ "pr", "create",
252
+ "--repo", opts.repo,
253
+ "--base", opts.base,
254
+ "--head", opts.head,
255
+ "--title", opts.title,
256
+ "--body", truncateBody(opts.body),
257
+ ];
258
+ if (opts.draft) args.push("--draft");
259
+
260
+ const result = runGh(args, cwd);
261
+ if (!result.ok) return fail(result.error!);
262
+
263
+ const match = result.data!.match(/\/pull\/(\d+)/);
264
+ if (!match) return fail(`Could not parse PR number from: ${result.data}`);
265
+ return ok(parseInt(match[1], 10));
266
+ }
267
+
268
+ export function ghMarkPRReady(cwd: string, repo: string, prNumber: number): GhResult<void> {
269
+ const result = runGh(
270
+ ["pr", "ready", String(prNumber), "--repo", repo],
271
+ cwd,
272
+ );
273
+ if (!result.ok) return fail(result.error!);
274
+ return ok(undefined);
275
+ }
276
+
277
+ export function ghMergePR(cwd: string, repo: string, prNumber: number, strategy: "squash" | "merge" = "squash"): GhResult<void> {
278
+ const args = [
279
+ "pr", "merge", String(prNumber),
280
+ "--repo", repo,
281
+ strategy === "squash" ? "--squash" : "--merge",
282
+ "--delete-branch",
283
+ ];
284
+ const result = runGh(args, cwd);
285
+ if (!result.ok) return fail(result.error!);
286
+ return ok(undefined);
287
+ }
288
+
289
+ // ─── Projects v2 ────────────────────────────────────────────────────────────
290
+
291
+ export function ghAddToProject(cwd: string, repo: string, projectNumber: number, issueNumber: number): GhResult<void> {
292
+ // Get the issue's node ID first
293
+ const issueResult = runGhJson<{ id: string }>(
294
+ ["api", `repos/${repo}/issues/${issueNumber}`, "--jq", "{id: .node_id}"],
295
+ cwd,
296
+ );
297
+ if (!issueResult.ok) return fail(issueResult.error!);
298
+
299
+ // Get the project's node ID
300
+ const [owner] = repo.split("/");
301
+ const projectResult = runGhJson<{ id: string }>(
302
+ [
303
+ "api", "graphql",
304
+ "-f", `query=query { user(login: "${owner}") { projectV2(number: ${projectNumber}) { id } } }`,
305
+ "--jq", ".data.user.projectV2.id",
306
+ ],
307
+ cwd,
308
+ );
309
+
310
+ // Try org if user fails
311
+ let projectId: string | undefined;
312
+ if (projectResult.ok && projectResult.data?.id) {
313
+ projectId = projectResult.data.id;
314
+ } else {
315
+ const orgResult = runGhJson<{ id: string }>(
316
+ [
317
+ "api", "graphql",
318
+ "-f", `query=query { organization(login: "${owner}") { projectV2(number: ${projectNumber}) { id } } }`,
319
+ "--jq", ".data.organization.projectV2.id",
320
+ ],
321
+ cwd,
322
+ );
323
+ if (orgResult.ok) projectId = orgResult.data?.id;
324
+ }
325
+
326
+ if (!projectId) return fail("Could not find project");
327
+
328
+ const mutation = `mutation { addProjectV2ItemById(input: { projectId: "${projectId}", contentId: "${issueResult.data!.id}" }) { item { id } } }`;
329
+ return runGh(["api", "graphql", "-f", `query=${mutation}`], cwd) as GhResult<void>;
330
+ }
331
+
332
+ // ─── Branch Operations ──────────────────────────────────────────────────────
333
+
334
+ export function ghPushBranch(cwd: string, branch: string, setUpstream = true): GhResult<void> {
335
+ const args = ["git", "push"];
336
+ if (setUpstream) args.push("-u", "origin", branch);
337
+ else args.push("origin", branch);
338
+
339
+ try {
340
+ execFileSync(args[0], args.slice(1), {
341
+ cwd,
342
+ encoding: "utf-8",
343
+ stdio: ["ignore", "pipe", "pipe"],
344
+ timeout: 30_000,
345
+ });
346
+ return ok(undefined);
347
+ } catch (err) {
348
+ return fail(err instanceof Error ? err.message : String(err));
349
+ }
350
+ }
351
+
352
+ export function ghCreateBranch(cwd: string, branch: string, from: string): GhResult<void> {
353
+ try {
354
+ execFileSync("git", ["branch", branch, from], {
355
+ cwd,
356
+ encoding: "utf-8",
357
+ stdio: ["ignore", "pipe", "pipe"],
358
+ timeout: 10_000,
359
+ });
360
+ return ok(undefined);
361
+ } catch (err) {
362
+ return fail(err instanceof Error ? err.message : String(err));
363
+ }
364
+ }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * GitHub Sync extension for GSD.
3
+ *
4
+ * Opt-in extension that syncs GSD lifecycle events to GitHub:
5
+ * milestones → GH Milestones + tracking issues, slices → draft PRs,
6
+ * tasks → sub-issues with auto-close on commit.
7
+ *
8
+ * Integration happens via a single dynamic import in auto-post-unit.ts.
9
+ * This index registers a `/github-sync` command for manual bootstrap
10
+ * and status display.
11
+ */
12
+
13
+ import type { ExtensionAPI } from "@gsd/pi-coding-agent";
14
+ import { bootstrapSync } from "./sync.js";
15
+ import { loadSyncMapping } from "./mapping.js";
16
+ import { ghIsAvailable } from "./cli.js";
17
+
18
+ export default function (pi: ExtensionAPI) {
19
+ pi.registerCommand("github-sync", {
20
+ description: "Bootstrap GitHub sync or show sync status",
21
+ handler: async (args: string, ctx) => {
22
+ const subcommand = args.trim().toLowerCase();
23
+
24
+ if (subcommand === "status") {
25
+ await showStatus(ctx);
26
+ return;
27
+ }
28
+
29
+ if (subcommand === "bootstrap" || subcommand === "") {
30
+ await runBootstrap(ctx);
31
+ return;
32
+ }
33
+
34
+ ctx.ui.notify(
35
+ "Usage: /github-sync [bootstrap|status]",
36
+ "info",
37
+ );
38
+ },
39
+ });
40
+ }
41
+
42
+ async function showStatus(ctx: import("@gsd/pi-coding-agent").ExtensionCommandContext) {
43
+ if (!ghIsAvailable()) {
44
+ ctx.ui.notify("GitHub sync: `gh` CLI not installed or not authenticated.", "warning");
45
+ return;
46
+ }
47
+
48
+ const mapping = loadSyncMapping(ctx.cwd);
49
+ if (!mapping) {
50
+ ctx.ui.notify("GitHub sync: No sync mapping found. Run `/github-sync bootstrap` to initialize.", "info");
51
+ return;
52
+ }
53
+
54
+ const milestoneCount = Object.keys(mapping.milestones).length;
55
+ const sliceCount = Object.keys(mapping.slices).length;
56
+ const taskCount = Object.keys(mapping.tasks).length;
57
+ const openMilestones = Object.values(mapping.milestones).filter(m => m.state === "open").length;
58
+ const openSlices = Object.values(mapping.slices).filter(s => s.state === "open").length;
59
+ const openTasks = Object.values(mapping.tasks).filter(t => t.state === "open").length;
60
+
61
+ ctx.ui.notify(
62
+ [
63
+ `GitHub sync: repo=${mapping.repo}`,
64
+ ` Milestones: ${milestoneCount} (${openMilestones} open)`,
65
+ ` Slices: ${sliceCount} (${openSlices} open)`,
66
+ ` Tasks: ${taskCount} (${openTasks} open)`,
67
+ ].join("\n"),
68
+ "info",
69
+ );
70
+ }
71
+
72
+ async function runBootstrap(ctx: import("@gsd/pi-coding-agent").ExtensionCommandContext) {
73
+ if (!ghIsAvailable()) {
74
+ ctx.ui.notify("GitHub sync: `gh` CLI not installed or not authenticated.", "warning");
75
+ return;
76
+ }
77
+
78
+ ctx.ui.notify("GitHub sync: bootstrapping...", "info");
79
+
80
+ try {
81
+ const counts = await bootstrapSync(ctx.cwd);
82
+ if (counts.milestones === 0 && counts.slices === 0 && counts.tasks === 0) {
83
+ ctx.ui.notify("GitHub sync: everything already synced (or no milestones found).", "info");
84
+ } else {
85
+ ctx.ui.notify(
86
+ `GitHub sync: created ${counts.milestones} milestone(s), ${counts.slices} slice(s), ${counts.tasks} task(s).`,
87
+ "info",
88
+ );
89
+ }
90
+ } catch (err) {
91
+ ctx.ui.notify(`GitHub sync bootstrap failed: ${err}`, "error");
92
+ }
93
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Persistence layer for the GitHub sync mapping.
3
+ *
4
+ * The mapping lives at `.gsd/github-sync.json` and tracks which GSD
5
+ * entities have been synced to which GitHub entities (issues, PRs,
6
+ * milestones) along with their numbers and sync timestamps.
7
+ */
8
+
9
+ import { existsSync, readFileSync } from "node:fs";
10
+ import { join } from "node:path";
11
+ import { atomicWriteSync } from "../gsd/atomic-write.js";
12
+ import type { SyncMapping, MilestoneSyncRecord, SliceSyncRecord, SyncEntityRecord } from "./types.js";
13
+
14
+ const MAPPING_FILENAME = "github-sync.json";
15
+
16
+ function mappingPath(basePath: string): string {
17
+ return join(basePath, ".gsd", MAPPING_FILENAME);
18
+ }
19
+
20
+ // ─── Load / Save ────────────────────────────────────────────────────────────
21
+
22
+ export function loadSyncMapping(basePath: string): SyncMapping | null {
23
+ const path = mappingPath(basePath);
24
+ if (!existsSync(path)) return null;
25
+ try {
26
+ const raw = readFileSync(path, "utf-8");
27
+ const parsed = JSON.parse(raw);
28
+ if (parsed?.version !== 1) return null;
29
+ return parsed as SyncMapping;
30
+ } catch {
31
+ return null;
32
+ }
33
+ }
34
+
35
+ export function saveSyncMapping(basePath: string, mapping: SyncMapping): void {
36
+ const path = mappingPath(basePath);
37
+ atomicWriteSync(path, JSON.stringify(mapping, null, 2) + "\n");
38
+ }
39
+
40
+ export function createEmptyMapping(repo: string): SyncMapping {
41
+ return {
42
+ version: 1,
43
+ repo,
44
+ milestones: {},
45
+ slices: {},
46
+ tasks: {},
47
+ };
48
+ }
49
+
50
+ // ─── Accessors ──────────────────────────────────────────────────────────────
51
+
52
+ export function getMilestoneRecord(mapping: SyncMapping, mid: string): MilestoneSyncRecord | null {
53
+ return mapping.milestones[mid] ?? null;
54
+ }
55
+
56
+ export function getSliceRecord(mapping: SyncMapping, mid: string, sid: string): SliceSyncRecord | null {
57
+ return mapping.slices[`${mid}/${sid}`] ?? null;
58
+ }
59
+
60
+ export function getTaskRecord(mapping: SyncMapping, mid: string, sid: string, tid: string): SyncEntityRecord | null {
61
+ return mapping.tasks[`${mid}/${sid}/${tid}`] ?? null;
62
+ }
63
+
64
+ export function getTaskIssueNumber(mapping: SyncMapping, mid: string, sid: string, tid: string): number | null {
65
+ const record = getTaskRecord(mapping, mid, sid, tid);
66
+ return record?.issueNumber ?? null;
67
+ }
68
+
69
+ // ─── Mutators ───────────────────────────────────────────────────────────────
70
+
71
+ export function setMilestoneRecord(mapping: SyncMapping, mid: string, record: MilestoneSyncRecord): void {
72
+ mapping.milestones[mid] = record;
73
+ }
74
+
75
+ export function setSliceRecord(mapping: SyncMapping, mid: string, sid: string, record: SliceSyncRecord): void {
76
+ mapping.slices[`${mid}/${sid}`] = record;
77
+ }
78
+
79
+ export function setTaskRecord(mapping: SyncMapping, mid: string, sid: string, tid: string, record: SyncEntityRecord): void {
80
+ mapping.tasks[`${mid}/${sid}/${tid}`] = record;
81
+ }