gsd-pi 2.74.0-dev.2b524c3 → 2.74.0-dev.b741afb

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 (159) hide show
  1. package/dist/cli.js +85 -0
  2. package/dist/headless-query.js +4 -1
  3. package/dist/help-text.js +23 -0
  4. package/dist/resources/extensions/gsd/auto/detect-stuck.js +11 -4
  5. package/dist/resources/extensions/gsd/auto/phases.js +45 -1
  6. package/dist/resources/extensions/gsd/auto-post-unit.js +52 -56
  7. package/dist/resources/extensions/gsd/auto-prompts.js +12 -0
  8. package/dist/resources/extensions/gsd/auto.js +8 -2
  9. package/dist/resources/extensions/gsd/bootstrap/register-extension.js +21 -8
  10. package/dist/resources/extensions/gsd/commands/catalog.js +26 -1
  11. package/dist/resources/extensions/gsd/commands/handlers/ops.js +20 -0
  12. package/dist/resources/extensions/gsd/commands/handlers/workflow.js +68 -9
  13. package/dist/resources/extensions/gsd/commands-add-tests.js +111 -0
  14. package/dist/resources/extensions/gsd/commands-backlog.js +140 -0
  15. package/dist/resources/extensions/gsd/commands-do.js +79 -0
  16. package/dist/resources/extensions/gsd/commands-maintenance.js +6 -6
  17. package/dist/resources/extensions/gsd/commands-pr-branch.js +180 -0
  18. package/dist/resources/extensions/gsd/commands-session-report.js +82 -0
  19. package/dist/resources/extensions/gsd/commands-ship.js +187 -0
  20. package/dist/resources/extensions/gsd/db-writer.js +3 -5
  21. package/dist/resources/extensions/gsd/graph-context.js +66 -0
  22. package/dist/resources/extensions/gsd/gsd-db.js +321 -0
  23. package/dist/resources/extensions/gsd/index.js +15 -2
  24. package/dist/resources/extensions/gsd/md-importer.js +3 -4
  25. package/dist/resources/extensions/gsd/memory-store.js +19 -51
  26. package/dist/resources/extensions/gsd/milestone-validation-gates.js +13 -12
  27. package/dist/resources/extensions/gsd/native-git-bridge.js +7 -4
  28. package/dist/resources/extensions/gsd/prompts/add-tests.md +35 -0
  29. package/dist/resources/extensions/gsd/state.js +5 -1
  30. package/dist/resources/extensions/gsd/tools/complete-slice.js +15 -0
  31. package/dist/resources/extensions/gsd/tools/workflow-tool-executors.js +3 -14
  32. package/dist/resources/extensions/gsd/triage-resolution.js +2 -5
  33. package/dist/resources/extensions/gsd/workflow-manifest.js +8 -69
  34. package/dist/resources/extensions/gsd/workflow-migration.js +21 -22
  35. package/dist/resources/extensions/gsd/workflow-projections.js +4 -1
  36. package/dist/resources/extensions/gsd/workflow-reconcile.js +14 -11
  37. package/dist/tsconfig.extensions.tsbuildinfo +1 -0
  38. package/dist/web/standalone/.next/BUILD_ID +1 -1
  39. package/dist/web/standalone/.next/app-path-routes-manifest.json +7 -7
  40. package/dist/web/standalone/.next/build-manifest.json +2 -2
  41. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  42. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  43. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  44. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  45. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  46. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  47. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  48. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  49. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  50. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  51. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  52. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  53. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  54. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  55. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  56. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  57. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  58. package/dist/web/standalone/.next/server/app/index.html +1 -1
  59. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  60. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  61. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  62. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  63. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  64. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  65. package/dist/web/standalone/.next/server/app-paths-manifest.json +7 -7
  66. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  67. package/dist/web/standalone/.next/server/middleware-manifest.json +5 -5
  68. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  69. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  70. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  71. package/package.json +3 -2
  72. package/packages/daemon/package.json +2 -2
  73. package/packages/mcp-server/dist/index.d.ts +3 -0
  74. package/packages/mcp-server/dist/index.d.ts.map +1 -1
  75. package/packages/mcp-server/dist/index.js +3 -0
  76. package/packages/mcp-server/dist/index.js.map +1 -1
  77. package/packages/mcp-server/dist/readers/graph.d.ts +87 -0
  78. package/packages/mcp-server/dist/readers/graph.d.ts.map +1 -0
  79. package/packages/mcp-server/dist/readers/graph.js +548 -0
  80. package/packages/mcp-server/dist/readers/graph.js.map +1 -0
  81. package/packages/mcp-server/dist/readers/index.d.ts +2 -0
  82. package/packages/mcp-server/dist/readers/index.d.ts.map +1 -1
  83. package/packages/mcp-server/dist/readers/index.js +1 -0
  84. package/packages/mcp-server/dist/readers/index.js.map +1 -1
  85. package/packages/mcp-server/dist/server.d.ts.map +1 -1
  86. package/packages/mcp-server/dist/server.js +65 -0
  87. package/packages/mcp-server/dist/server.js.map +1 -1
  88. package/packages/mcp-server/package.json +2 -2
  89. package/packages/mcp-server/src/index.ts +15 -0
  90. package/packages/mcp-server/src/readers/graph.test.ts +426 -0
  91. package/packages/mcp-server/src/readers/graph.ts +708 -0
  92. package/packages/mcp-server/src/readers/index.ts +12 -0
  93. package/packages/mcp-server/src/server.ts +83 -0
  94. package/packages/mcp-server/tsconfig.json +1 -0
  95. package/packages/mcp-server/tsconfig.tsbuildinfo +1 -0
  96. package/packages/native/package.json +2 -2
  97. package/packages/native/tsconfig.tsbuildinfo +1 -0
  98. package/packages/pi-agent-core/package.json +1 -1
  99. package/packages/pi-agent-core/tsconfig.json +1 -0
  100. package/packages/pi-agent-core/tsconfig.tsbuildinfo +1 -0
  101. package/packages/pi-ai/package.json +1 -1
  102. package/packages/pi-ai/tsconfig.json +1 -0
  103. package/packages/pi-ai/tsconfig.tsbuildinfo +1 -0
  104. package/packages/pi-coding-agent/tsconfig.json +1 -0
  105. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -0
  106. package/packages/pi-tui/package.json +1 -1
  107. package/packages/pi-tui/tsconfig.json +1 -0
  108. package/packages/pi-tui/tsconfig.tsbuildinfo +1 -0
  109. package/packages/rpc-client/package.json +1 -1
  110. package/packages/rpc-client/tsconfig.json +1 -0
  111. package/packages/rpc-client/tsconfig.tsbuildinfo +1 -0
  112. package/src/resources/extensions/gsd/auto/detect-stuck.ts +12 -4
  113. package/src/resources/extensions/gsd/auto/loop-deps.ts +6 -0
  114. package/src/resources/extensions/gsd/auto/phases.ts +68 -1
  115. package/src/resources/extensions/gsd/auto-post-unit.ts +60 -57
  116. package/src/resources/extensions/gsd/auto-prompts.ts +13 -0
  117. package/src/resources/extensions/gsd/auto.ts +7 -0
  118. package/src/resources/extensions/gsd/bootstrap/register-extension.ts +24 -8
  119. package/src/resources/extensions/gsd/commands/catalog.ts +26 -1
  120. package/src/resources/extensions/gsd/commands/handlers/ops.ts +20 -0
  121. package/src/resources/extensions/gsd/commands/handlers/workflow.ts +74 -9
  122. package/src/resources/extensions/gsd/commands-add-tests.ts +137 -0
  123. package/src/resources/extensions/gsd/commands-backlog.ts +182 -0
  124. package/src/resources/extensions/gsd/commands-do.ts +109 -0
  125. package/src/resources/extensions/gsd/commands-maintenance.ts +6 -6
  126. package/src/resources/extensions/gsd/commands-pr-branch.ts +234 -0
  127. package/src/resources/extensions/gsd/commands-session-report.ts +101 -0
  128. package/src/resources/extensions/gsd/commands-ship.ts +219 -0
  129. package/src/resources/extensions/gsd/db-writer.ts +3 -5
  130. package/src/resources/extensions/gsd/graph-context.ts +85 -0
  131. package/src/resources/extensions/gsd/gsd-db.ts +467 -0
  132. package/src/resources/extensions/gsd/index.ts +18 -2
  133. package/src/resources/extensions/gsd/md-importer.ts +3 -5
  134. package/src/resources/extensions/gsd/memory-store.ts +31 -62
  135. package/src/resources/extensions/gsd/milestone-validation-gates.ts +13 -14
  136. package/src/resources/extensions/gsd/native-git-bridge.ts +11 -12
  137. package/src/resources/extensions/gsd/prompts/add-tests.md +35 -0
  138. package/src/resources/extensions/gsd/state.ts +9 -2
  139. package/src/resources/extensions/gsd/tests/commands-backlog.test.ts +158 -0
  140. package/src/resources/extensions/gsd/tests/commands-do.test.ts +127 -0
  141. package/src/resources/extensions/gsd/tests/commands-pr-branch.test.ts +68 -0
  142. package/src/resources/extensions/gsd/tests/commands-session-report.test.ts +82 -0
  143. package/src/resources/extensions/gsd/tests/commands-ship.test.ts +71 -0
  144. package/src/resources/extensions/gsd/tests/commands-workflow-custom.test.ts +14 -0
  145. package/src/resources/extensions/gsd/tests/extension-bootstrap-isolation.test.ts +154 -0
  146. package/src/resources/extensions/gsd/tests/graph-context.test.ts +337 -0
  147. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +68 -1
  148. package/src/resources/extensions/gsd/tests/native-git-bridge-exec-fallback.test.ts +140 -0
  149. package/src/resources/extensions/gsd/tests/single-writer-invariant.test.ts +180 -0
  150. package/src/resources/extensions/gsd/tests/workflow-logger-wiring.test.ts +223 -0
  151. package/src/resources/extensions/gsd/tools/complete-slice.ts +19 -0
  152. package/src/resources/extensions/gsd/tools/workflow-tool-executors.ts +3 -11
  153. package/src/resources/extensions/gsd/triage-resolution.ts +2 -7
  154. package/src/resources/extensions/gsd/workflow-manifest.ts +9 -104
  155. package/src/resources/extensions/gsd/workflow-migration.ts +21 -29
  156. package/src/resources/extensions/gsd/workflow-projections.ts +8 -1
  157. package/src/resources/extensions/gsd/workflow-reconcile.ts +15 -15
  158. /package/dist/web/standalone/.next/static/{YzIEI9sxJy4t5xgClF08g → XnHY5eXUsTCFmNodWHetD}/_buildManifest.js +0 -0
  159. /package/dist/web/standalone/.next/static/{YzIEI9sxJy4t5xgClF08g → XnHY5eXUsTCFmNodWHetD}/_ssgManifest.js +0 -0
@@ -0,0 +1,109 @@
1
+ /**
2
+ * GSD Command — /gsd do
3
+ *
4
+ * Routes freeform natural language to the correct /gsd subcommand
5
+ * using keyword matching. Falls back to /gsd quick for task-like input.
6
+ */
7
+
8
+ import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent";
9
+
10
+ interface Route {
11
+ keywords: string[];
12
+ command: string;
13
+ }
14
+
15
+ const ROUTES: Route[] = [
16
+ { keywords: ["progress", "status", "dashboard", "how far", "where are we"], command: "status" },
17
+ { keywords: ["auto", "autonomous", "run all", "keep going", "start auto"], command: "auto" },
18
+ { keywords: ["stop", "halt", "abort"], command: "stop" },
19
+ { keywords: ["pause", "break", "take a break"], command: "pause" },
20
+ { keywords: ["history", "past", "what happened", "previous"], command: "history" },
21
+ { keywords: ["doctor", "health", "diagnose", "check health"], command: "doctor" },
22
+ { keywords: ["clean up", "cleanup", "remove old", "prune", "tidy"], command: "cleanup" },
23
+ { keywords: ["export", "report", "share results"], command: "export" },
24
+ { keywords: ["ship", "pull request", "create pr", "open pr", "merge"], command: "ship" },
25
+ { keywords: ["discuss", "talk about", "architecture", "design"], command: "discuss" },
26
+ { keywords: ["undo", "revert", "rollback", "take back"], command: "undo" },
27
+ { keywords: ["skip", "skip task", "skip this"], command: "skip" },
28
+ { keywords: ["queue", "reorder", "milestone order", "order milestones"], command: "queue" },
29
+ { keywords: ["visualize", "viz", "graph", "chart", "show graph"], command: "visualize" },
30
+ { keywords: ["capture", "note", "idea", "thought", "remember"], command: "capture" },
31
+ { keywords: ["inspect", "database", "sqlite", "db state"], command: "inspect" },
32
+ { keywords: ["knowledge", "rule", "pattern", "lesson"], command: "knowledge" },
33
+ { keywords: ["session report", "session summary", "cost summary", "how much"], command: "session-report" },
34
+ { keywords: ["backlog", "parking lot", "later", "someday"], command: "backlog" },
35
+ { keywords: ["pr branch", "clean branch", "filter commits"], command: "pr-branch" },
36
+ { keywords: ["add tests", "write tests", "generate tests", "test coverage"], command: "add-tests" },
37
+ { keywords: ["next", "step", "next step", "what's next"], command: "next" },
38
+ { keywords: ["migrate", "migration", "convert", "upgrade"], command: "migrate" },
39
+ { keywords: ["steer", "change direction", "pivot", "redirect"], command: "steer" },
40
+ { keywords: ["park", "shelve", "set aside"], command: "park" },
41
+ { keywords: ["widget", "toggle widget"], command: "widget" },
42
+ { keywords: ["logs", "debug logs", "log files"], command: "logs" },
43
+ ];
44
+
45
+ interface MatchResult {
46
+ command: string;
47
+ remainingArgs: string;
48
+ score: number;
49
+ }
50
+
51
+ function matchRoute(input: string): MatchResult | null {
52
+ const lower = input.toLowerCase();
53
+ let bestMatch: MatchResult | null = null;
54
+
55
+ for (const route of ROUTES) {
56
+ for (const keyword of route.keywords) {
57
+ if (lower.includes(keyword)) {
58
+ const score = keyword.length; // Longer match = higher confidence
59
+ if (!bestMatch || score > bestMatch.score) {
60
+ // Strip the matched keyword from input to get remaining args
61
+ const idx = lower.indexOf(keyword);
62
+ const remaining = (input.slice(0, idx) + input.slice(idx + keyword.length)).trim();
63
+ bestMatch = { command: route.command, remainingArgs: remaining, score };
64
+ }
65
+ }
66
+ }
67
+ }
68
+
69
+ return bestMatch;
70
+ }
71
+
72
+ export async function handleDo(
73
+ args: string,
74
+ ctx: ExtensionCommandContext,
75
+ pi: ExtensionAPI,
76
+ ): Promise<void> {
77
+ if (!args.trim()) {
78
+ ctx.ui.notify(
79
+ "Usage: /gsd do <what you want to do>\n\n" +
80
+ "Examples:\n" +
81
+ " /gsd do show me progress\n" +
82
+ " /gsd do run autonomously\n" +
83
+ " /gsd do clean up old branches\n" +
84
+ " /gsd do fix the login bug",
85
+ "warning",
86
+ );
87
+ return;
88
+ }
89
+
90
+ const match = matchRoute(args);
91
+
92
+ if (match) {
93
+ const fullCommand = match.remainingArgs
94
+ ? `${match.command} ${match.remainingArgs}`
95
+ : match.command;
96
+
97
+ ctx.ui.notify(`→ /gsd ${fullCommand}`, "info");
98
+
99
+ // Re-dispatch through the main dispatcher
100
+ const { handleGSDCommand } = await import("./commands/dispatcher.js");
101
+ await handleGSDCommand(fullCommand, ctx, pi);
102
+ return;
103
+ }
104
+
105
+ // No keyword match → treat as quick task
106
+ ctx.ui.notify(`→ /gsd quick ${args}`, "info");
107
+ const { handleQuick } = await import("./quick.js");
108
+ await handleQuick(args, ctx, pi);
109
+ }
@@ -488,7 +488,7 @@ export async function handleCleanupProjects(args: string, ctx: ExtensionCommandC
488
488
  * Prints counts of recovered items and the resulting project phase.
489
489
  */
490
490
  export async function handleRecover(ctx: ExtensionCommandContext, basePath: string): Promise<void> {
491
- const { isDbAvailable: dbAvailable, _getAdapter, transaction: dbTransaction } = await import("./gsd-db.js");
491
+ const { isDbAvailable: dbAvailable, clearEngineHierarchy, transaction: dbTransaction } = await import("./gsd-db.js");
492
492
  const { migrateHierarchyToDb } = await import("./md-importer.js");
493
493
  const { invalidateStateCache } = await import("./state.js");
494
494
 
@@ -498,12 +498,12 @@ export async function handleRecover(ctx: ExtensionCommandContext, basePath: stri
498
498
  }
499
499
 
500
500
  try {
501
- // 1. Delete + re-populate inside a single transaction for atomicity
502
- const db = _getAdapter()!;
501
+ // 1. Delete + re-populate inside a single transaction for atomicity.
502
+ // clearEngineHierarchy() uses transaction() internally but transaction()
503
+ // is re-entrant, so wrapping in dbTransaction() keeps the whole
504
+ // clear+repopulate atomic.
503
505
  const counts = dbTransaction(() => {
504
- db.exec("DELETE FROM tasks");
505
- db.exec("DELETE FROM slices");
506
- db.exec("DELETE FROM milestones");
506
+ clearEngineHierarchy();
507
507
  return migrateHierarchyToDb(basePath);
508
508
  });
509
509
 
@@ -0,0 +1,234 @@
1
+ /**
2
+ * GSD Command — /gsd pr-branch
3
+ *
4
+ * Creates a clean PR branch by cherry-picking commits while stripping
5
+ * any changes to .gsd/, .planning/, and PLAN.md paths. Useful for
6
+ * upstream PRs where planning artifacts should not be included.
7
+ */
8
+
9
+ import type { ExtensionCommandContext } from "@gsd/pi-coding-agent";
10
+
11
+ import { execFileSync } from "node:child_process";
12
+
13
+ import {
14
+ nativeGetCurrentBranch,
15
+ nativeDetectMainBranch,
16
+ nativeBranchExists,
17
+ } from "./native-git-bridge.js";
18
+
19
+ const EXCLUDED_PATHS = [".gsd", ".planning", "PLAN.md"] as const;
20
+
21
+ function git(basePath: string, args: readonly string[]): string {
22
+ return execFileSync("git", args, { cwd: basePath, encoding: "utf-8" }).trim();
23
+ }
24
+
25
+ function gitAllowFail(basePath: string, args: readonly string[]): void {
26
+ try {
27
+ execFileSync("git", args, { cwd: basePath, encoding: "utf-8", stdio: "pipe" });
28
+ } catch {
29
+ // ignored — caller opts into non-fatal behavior
30
+ }
31
+ }
32
+
33
+ function hasStagedChanges(basePath: string): boolean {
34
+ try {
35
+ execFileSync("git", ["diff", "--cached", "--quiet"], {
36
+ cwd: basePath,
37
+ stdio: "pipe",
38
+ });
39
+ return false;
40
+ } catch {
41
+ return true;
42
+ }
43
+ }
44
+
45
+ function isValidBranchName(name: string): boolean {
46
+ try {
47
+ execFileSync("git", ["check-ref-format", "--branch", name], { stdio: "pipe" });
48
+ return true;
49
+ } catch {
50
+ return false;
51
+ }
52
+ }
53
+
54
+ function getCodeOnlyCommits(basePath: string, base: string, head: string): string[] {
55
+ try {
56
+ const allCommits = git(basePath, ["log", "--format=%H", `${base}..${head}`])
57
+ .split("\n")
58
+ .filter(Boolean);
59
+ const codeCommits: string[] = [];
60
+
61
+ for (const sha of allCommits) {
62
+ const files = git(basePath, ["diff-tree", "--no-commit-id", "--name-only", "-r", sha])
63
+ .split("\n")
64
+ .filter(Boolean);
65
+ const hasCodeChanges = files.some(
66
+ (f) => !f.startsWith(".gsd/") && !f.startsWith(".planning/") && f !== "PLAN.md",
67
+ );
68
+ if (hasCodeChanges) {
69
+ codeCommits.push(sha);
70
+ }
71
+ }
72
+
73
+ return codeCommits.reverse(); // chronological for cherry-picking
74
+ } catch {
75
+ return [];
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Cherry-pick a commit while stripping excluded paths from the resulting
81
+ * commit. Returns true if a commit was produced, false if nothing remained
82
+ * after filtering.
83
+ */
84
+ function cherryPickFiltered(basePath: string, sha: string): boolean {
85
+ git(basePath, ["cherry-pick", "--no-commit", "--allow-empty", sha]);
86
+
87
+ // Unstage any excluded paths introduced by the cherry-pick.
88
+ gitAllowFail(basePath, ["reset", "HEAD", "--", ...EXCLUDED_PATHS]);
89
+
90
+ // Restore worktree state for excluded paths from HEAD (if tracked),
91
+ // then remove any newly introduced untracked files under those paths.
92
+ gitAllowFail(basePath, ["checkout", "HEAD", "--", ...EXCLUDED_PATHS]);
93
+ gitAllowFail(basePath, ["clean", "-fdq", "--", ...EXCLUDED_PATHS]);
94
+
95
+ if (!hasStagedChanges(basePath)) {
96
+ // Nothing remained after filtering — discard worktree residue and skip.
97
+ git(basePath, ["reset", "--hard", "HEAD"]);
98
+ return false;
99
+ }
100
+
101
+ git(basePath, ["commit", "-C", sha]);
102
+ return true;
103
+ }
104
+
105
+ function assertNoExcludedPaths(basePath: string, base: string): void {
106
+ const files = git(basePath, [
107
+ "diff",
108
+ "--name-only",
109
+ `${base}..HEAD`,
110
+ ])
111
+ .split("\n")
112
+ .filter(Boolean);
113
+ const leaked = files.filter(
114
+ (f) => f.startsWith(".gsd/") || f.startsWith(".planning/") || f === "PLAN.md",
115
+ );
116
+ if (leaked.length > 0) {
117
+ throw new Error(
118
+ `PR branch still contains excluded paths: ${leaked.slice(0, 5).join(", ")}${
119
+ leaked.length > 5 ? ` (+${leaked.length - 5} more)` : ""
120
+ }`,
121
+ );
122
+ }
123
+ }
124
+
125
+ export async function handlePrBranch(
126
+ args: string,
127
+ ctx: ExtensionCommandContext,
128
+ ): Promise<void> {
129
+ const basePath = process.cwd();
130
+ const dryRun = args.includes("--dry-run");
131
+ const nameMatch = args.match(/--name\s+(\S+)/);
132
+
133
+ const currentBranch = nativeGetCurrentBranch(basePath);
134
+ const mainBranch = nativeDetectMainBranch(basePath);
135
+
136
+ // Determine base ref (prefer upstream/main if available)
137
+ let baseRef: string;
138
+ try {
139
+ git(basePath, ["rev-parse", "--verify", "upstream/main"]);
140
+ baseRef = "upstream/main";
141
+ } catch {
142
+ baseRef = mainBranch;
143
+ }
144
+
145
+ // Find commits with code changes
146
+ const commits = getCodeOnlyCommits(basePath, baseRef, "HEAD");
147
+
148
+ if (commits.length === 0) {
149
+ ctx.ui.notify("No code-only commits found (all commits only touch .gsd/ files).", "info");
150
+ return;
151
+ }
152
+
153
+ if (dryRun) {
154
+ const lines = [`Would create PR branch with ${commits.length} commits (filtering .gsd/ paths):\n`];
155
+ for (const sha of commits) {
156
+ const msg = git(basePath, ["log", "--format=%s", "-1", sha]);
157
+ lines.push(` ${sha.slice(0, 8)} ${msg}`);
158
+ }
159
+ ctx.ui.notify(lines.join("\n"), "info");
160
+ return;
161
+ }
162
+
163
+ const requestedName = nameMatch?.[1];
164
+ if (requestedName && !isValidBranchName(requestedName)) {
165
+ ctx.ui.notify(
166
+ `Invalid branch name: ${requestedName}. Must satisfy git check-ref-format.`,
167
+ "error",
168
+ );
169
+ return;
170
+ }
171
+
172
+ const defaultName = `pr/${currentBranch}`;
173
+ const prBranch = requestedName ?? defaultName;
174
+
175
+ if (!isValidBranchName(prBranch)) {
176
+ ctx.ui.notify(
177
+ `Derived branch name is invalid: ${prBranch}. Use --name to override.`,
178
+ "error",
179
+ );
180
+ return;
181
+ }
182
+
183
+ if (nativeBranchExists(basePath, prBranch)) {
184
+ ctx.ui.notify(
185
+ `Branch ${prBranch} already exists. Use --name to specify a different name, or delete it first.`,
186
+ "warning",
187
+ );
188
+ return;
189
+ }
190
+
191
+ try {
192
+ // Create clean branch from base
193
+ git(basePath, ["checkout", "-b", prBranch, baseRef]);
194
+
195
+ // Cherry-pick with path filter
196
+ let picked = 0;
197
+ let skipped = 0;
198
+ for (const sha of commits) {
199
+ try {
200
+ if (cherryPickFiltered(basePath, sha)) {
201
+ picked++;
202
+ } else {
203
+ skipped++;
204
+ }
205
+ } catch (pickErr) {
206
+ gitAllowFail(basePath, ["cherry-pick", "--abort"]);
207
+ gitAllowFail(basePath, ["reset", "--hard", "HEAD"]);
208
+ const detail = pickErr instanceof Error ? pickErr.message : String(pickErr);
209
+ ctx.ui.notify(
210
+ `Cherry-pick conflict at ${sha.slice(0, 8)}. Picked ${picked}/${commits.length} commits. Resolve manually.\n${detail}`,
211
+ "warning",
212
+ );
213
+ git(basePath, ["checkout", currentBranch]);
214
+ return;
215
+ }
216
+ }
217
+
218
+ // Post-condition: no excluded paths should appear in the PR branch diff.
219
+ assertNoExcludedPaths(basePath, baseRef);
220
+
221
+ const skippedMsg = skipped > 0 ? ` (${skipped} skipped — contained only planning artifacts)` : "";
222
+ ctx.ui.notify(
223
+ `Created ${prBranch} with ${picked} commits${skippedMsg} (no .gsd/ artifacts).\nSwitch back: git checkout ${currentBranch}`,
224
+ "success",
225
+ );
226
+ } catch (err) {
227
+ // Restore original branch on failure
228
+ gitAllowFail(basePath, ["cherry-pick", "--abort"]);
229
+ gitAllowFail(basePath, ["reset", "--hard", "HEAD"]);
230
+ gitAllowFail(basePath, ["checkout", currentBranch]);
231
+ const msg = err instanceof Error ? err.message : String(err);
232
+ ctx.ui.notify(`Failed to create PR branch: ${msg}`, "error");
233
+ }
234
+ }
@@ -0,0 +1,101 @@
1
+ /**
2
+ * GSD Command — /gsd session-report
3
+ *
4
+ * Summarizes the current session: tasks completed, cost, tokens,
5
+ * duration, model usage breakdown.
6
+ */
7
+
8
+ import type { ExtensionCommandContext } from "@gsd/pi-coding-agent";
9
+
10
+ import { mkdirSync, writeFileSync } from "node:fs";
11
+ import { join } from "node:path";
12
+
13
+ import { getLedger, getProjectTotals, aggregateByModel, formatCost, formatTokenCount, loadLedgerFromDisk } from "./metrics.js";
14
+ import type { UnitMetrics } from "./metrics.js";
15
+ import { gsdRoot } from "./paths.js";
16
+ import { formatDuration } from "../shared/format-utils.js";
17
+
18
+ function formatSessionReport(units: UnitMetrics[]): string {
19
+ const totals = getProjectTotals(units);
20
+ const byModel = aggregateByModel(units);
21
+
22
+ const lines: string[] = [];
23
+ lines.push("╭─ Session Report ──────────────────────────────────────╮");
24
+
25
+ if (totals.duration > 0) {
26
+ lines.push(`│ Duration: ${formatDuration(totals.duration).padEnd(40)}│`);
27
+ }
28
+ lines.push(`│ Units: ${String(units.length).padEnd(40)}│`);
29
+ lines.push(`│ Cost: ${formatCost(totals.cost).padEnd(40)}│`);
30
+ lines.push(`│ Tokens: ${`${formatTokenCount(totals.tokens.input)} in / ${formatTokenCount(totals.tokens.output)} out`.padEnd(40)}│`);
31
+ lines.push("│ │");
32
+
33
+ // Work completed
34
+ if (units.length > 0) {
35
+ lines.push("│ Work Completed: │");
36
+ for (const unit of units) {
37
+ const finished = unit.finishedAt > 0;
38
+ const status = finished ? "✓" : "•";
39
+ const label = ` ${status} ${unit.id ?? "unknown"}`;
40
+ lines.push(`│ ${label.padEnd(53)}│`);
41
+ }
42
+ lines.push("│ │");
43
+ }
44
+
45
+ // Model usage
46
+ if (byModel.length > 0) {
47
+ lines.push("│ Model Usage: │");
48
+ for (const m of byModel) {
49
+ const label = ` ${m.model}: ${m.units} units (${formatCost(m.cost)})`;
50
+ lines.push(`│ ${label.padEnd(53)}│`);
51
+ }
52
+ }
53
+
54
+ lines.push("╰───────────────────────────────────────────────────────╯");
55
+ return lines.join("\n");
56
+ }
57
+
58
+ export async function handleSessionReport(
59
+ args: string,
60
+ ctx: ExtensionCommandContext,
61
+ ): Promise<void> {
62
+ const basePath = process.cwd();
63
+
64
+ // Get units from in-memory ledger or disk
65
+ const ledger = getLedger();
66
+ let units: UnitMetrics[];
67
+
68
+ if (ledger && ledger.units.length > 0) {
69
+ units = ledger.units;
70
+ } else {
71
+ const diskLedger = loadLedgerFromDisk(basePath);
72
+ if (!diskLedger || diskLedger.units.length === 0) {
73
+ ctx.ui.notify("No session data — no units have been executed yet.", "info");
74
+ return;
75
+ }
76
+ units = diskLedger.units;
77
+ }
78
+
79
+ // JSON output
80
+ if (args.includes("--json")) {
81
+ const totals = getProjectTotals(units);
82
+ const byModel = aggregateByModel(units);
83
+ ctx.ui.notify(JSON.stringify({ units: units.length, totals, byModel }, null, 2), "info");
84
+ return;
85
+ }
86
+
87
+ // Save to file
88
+ if (args.includes("--save")) {
89
+ const report = formatSessionReport(units);
90
+ const reportsDir = join(gsdRoot(basePath), "reports");
91
+ mkdirSync(reportsDir, { recursive: true });
92
+ const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
93
+ const outPath = join(reportsDir, `session-${timestamp}.md`);
94
+ writeFileSync(outPath, `\`\`\`\n${report}\n\`\`\`\n`, "utf-8");
95
+ ctx.ui.notify(`Report saved: ${outPath}`, "success");
96
+ return;
97
+ }
98
+
99
+ // Display
100
+ ctx.ui.notify(formatSessionReport(units), "info");
101
+ }
@@ -0,0 +1,219 @@
1
+ /**
2
+ * GSD Command — /gsd ship
3
+ *
4
+ * Creates a PR from milestone artifacts: generates title + body from
5
+ * roadmap, slice summaries, and metrics, then opens via `gh pr create`.
6
+ */
7
+
8
+ import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent";
9
+
10
+ import { execFileSync } from "node:child_process";
11
+ import { existsSync, readFileSync, readdirSync } from "node:fs";
12
+
13
+ import { deriveState } from "./state.js";
14
+ import { resolveMilestoneFile, resolveSlicePath, resolveSliceFile } from "./paths.js";
15
+ import { getLedger, getProjectTotals, aggregateByModel, formatCost, formatTokenCount, loadLedgerFromDisk } from "./metrics.js";
16
+ import { nativeGetCurrentBranch, nativeDetectMainBranch } from "./native-git-bridge.js";
17
+ import { formatDuration } from "../shared/format-utils.js";
18
+
19
+ function git(basePath: string, args: readonly string[]): string {
20
+ return execFileSync("git", args, { cwd: basePath, encoding: "utf-8" }).trim();
21
+ }
22
+
23
+ function isValidRefName(name: string): boolean {
24
+ try {
25
+ execFileSync("git", ["check-ref-format", "--branch", name], { stdio: "pipe" });
26
+ return true;
27
+ } catch {
28
+ return false;
29
+ }
30
+ }
31
+
32
+ interface PRContent {
33
+ title: string;
34
+ body: string;
35
+ }
36
+
37
+ function listSliceIds(basePath: string, milestoneId: string): string[] {
38
+ // Slices live at <milestoneDir>/slices/<sliceId>/ with canonical S\d+ IDs.
39
+ // Use resolveSlicePath with a probe to find the real slices directory root.
40
+ const probe = resolveSlicePath(basePath, milestoneId, "S01");
41
+ let slicesDir: string | null = null;
42
+ if (probe) {
43
+ // probe looks like <milestoneDir>/slices/S01 — parent is slices dir.
44
+ slicesDir = probe.replace(/[\\/][^\\/]+$/, "");
45
+ } else {
46
+ // Fall back to scanning the milestones roadmap file's sibling slices dir.
47
+ const roadmap = resolveMilestoneFile(basePath, milestoneId, "ROADMAP");
48
+ if (roadmap) {
49
+ slicesDir = roadmap.replace(/[\\/][^\\/]+$/, "") + "/slices";
50
+ }
51
+ }
52
+ if (!slicesDir || !existsSync(slicesDir)) return [];
53
+
54
+ try {
55
+ return readdirSync(slicesDir, { withFileTypes: true })
56
+ .filter((e) => e.isDirectory() && /^S\d+$/.test(e.name))
57
+ .map((e) => e.name)
58
+ .sort();
59
+ } catch {
60
+ return [];
61
+ }
62
+ }
63
+
64
+ function collectSliceSummaries(basePath: string, milestoneId: string): string[] {
65
+ const summaries: string[] = [];
66
+ for (const sliceId of listSliceIds(basePath, milestoneId)) {
67
+ const summaryPath = resolveSliceFile(basePath, milestoneId, sliceId, "SUMMARY");
68
+ if (!summaryPath || !existsSync(summaryPath)) continue;
69
+ try {
70
+ const content = readFileSync(summaryPath, "utf-8").trim();
71
+ if (content) summaries.push(`### ${sliceId}\n${content}`);
72
+ } catch {
73
+ // non-fatal
74
+ }
75
+ }
76
+ return summaries;
77
+ }
78
+
79
+ function generatePRContent(basePath: string, milestoneId: string, milestoneTitle: string): PRContent {
80
+ const title = `feat: ${milestoneTitle || milestoneId}`;
81
+
82
+ const sections: string[] = [];
83
+
84
+ // TL;DR
85
+ sections.push("## TL;DR\n");
86
+ sections.push(`**What:** Ship milestone ${milestoneId} — ${milestoneTitle || "(untitled)"}`);
87
+ sections.push(`**Why:** Milestone work complete, ready for review.`);
88
+ sections.push(`**How:** See slice summaries below.\n`);
89
+
90
+ // What — slice summaries
91
+ const summaries = collectSliceSummaries(basePath, milestoneId);
92
+ if (summaries.length > 0) {
93
+ sections.push("## What\n");
94
+ sections.push(summaries.join("\n\n"));
95
+ sections.push("");
96
+ }
97
+
98
+ // Roadmap status
99
+ const roadmapPath = resolveMilestoneFile(basePath, milestoneId, "ROADMAP");
100
+ if (roadmapPath && existsSync(roadmapPath)) {
101
+ try {
102
+ const roadmap = readFileSync(roadmapPath, "utf-8");
103
+ const checkboxLines = roadmap.split("\n").filter((l) => /^\s*-\s*\[[ x]\]/.test(l));
104
+ if (checkboxLines.length > 0) {
105
+ sections.push("## Roadmap\n");
106
+ sections.push(checkboxLines.join("\n"));
107
+ sections.push("");
108
+ }
109
+ } catch {
110
+ // non-fatal
111
+ }
112
+ }
113
+
114
+ // Metrics
115
+ const ledger = getLedger();
116
+ const units = ledger?.units ?? loadLedgerFromDisk(basePath)?.units ?? [];
117
+ if (units.length > 0) {
118
+ const totals = getProjectTotals(units);
119
+ const byModel = aggregateByModel(units);
120
+ sections.push("## Metrics\n");
121
+ sections.push(`- **Units executed:** ${units.length}`);
122
+ sections.push(`- **Total cost:** ${formatCost(totals.cost)}`);
123
+ sections.push(`- **Tokens:** ${formatTokenCount(totals.tokens.input)} input / ${formatTokenCount(totals.tokens.output)} output`);
124
+ if (totals.duration > 0) {
125
+ sections.push(`- **Duration:** ${formatDuration(totals.duration)}`);
126
+ }
127
+ if (byModel.length > 0) {
128
+ sections.push(`- **Models:** ${byModel.map((m) => `${m.model} (${m.units} units)`).join(", ")}`);
129
+ }
130
+ sections.push("");
131
+ }
132
+
133
+ // Change type checklist
134
+ sections.push("## Change type\n");
135
+ sections.push("- [x] `feat` — New feature or capability");
136
+ sections.push("- [ ] `fix` — Bug fix");
137
+ sections.push("- [ ] `refactor` — Code restructuring");
138
+ sections.push("- [ ] `test` — Adding or updating tests");
139
+ sections.push("- [ ] `docs` — Documentation only");
140
+ sections.push("- [ ] `chore` — Build, CI, or tooling changes\n");
141
+
142
+ // AI disclosure
143
+ sections.push("---\n");
144
+ sections.push("*This PR was prepared with AI assistance (GSD auto-mode).*");
145
+
146
+ return { title, body: sections.join("\n") };
147
+ }
148
+
149
+ export async function handleShip(
150
+ args: string,
151
+ ctx: ExtensionCommandContext,
152
+ _pi: ExtensionAPI,
153
+ ): Promise<void> {
154
+ const basePath = process.cwd();
155
+ const dryRun = args.includes("--dry-run");
156
+ const draft = args.includes("--draft");
157
+ const force = args.includes("--force");
158
+ const baseMatch = args.match(/--base\s+(\S+)/);
159
+ const base = baseMatch?.[1] ?? nativeDetectMainBranch(basePath);
160
+
161
+ if (!isValidRefName(base)) {
162
+ ctx.ui.notify(`Invalid base branch name: ${base}`, "error");
163
+ return;
164
+ }
165
+
166
+ // 1. Validate milestone state
167
+ const state = await deriveState(basePath);
168
+ if (!state.activeMilestone) {
169
+ ctx.ui.notify("No active milestone to ship. Complete milestone work first.", "warning");
170
+ return;
171
+ }
172
+
173
+ const milestoneId = state.activeMilestone.id;
174
+ const milestoneTitle = state.activeMilestone.title ?? "";
175
+
176
+ // 2. Check for incomplete work (use GSD phase as proxy — no phase field on ActiveRef)
177
+ if (state.phase !== "complete" && !force) {
178
+ ctx.ui.notify(
179
+ `Milestone ${milestoneId} may not be complete (phase: ${state.phase}). Use --force to ship anyway.`,
180
+ "warning",
181
+ );
182
+ return;
183
+ }
184
+
185
+ // 3. Generate PR content
186
+ const { title, body } = generatePRContent(basePath, milestoneId, milestoneTitle);
187
+
188
+ // 4. Dry-run — just show the PR content
189
+ if (dryRun) {
190
+ ctx.ui.notify(`--- PR Preview ---\n\nTitle: ${title}\n\n${body}`, "info");
191
+ return;
192
+ }
193
+
194
+ // 5. Check git state
195
+ const currentBranch = nativeGetCurrentBranch(basePath);
196
+ if (!isValidRefName(currentBranch)) {
197
+ ctx.ui.notify(`Current branch name is invalid for git: ${currentBranch}`, "error");
198
+ return;
199
+ }
200
+ if (currentBranch === base) {
201
+ ctx.ui.notify(`You're on ${base} — create a feature branch first.`, "warning");
202
+ return;
203
+ }
204
+
205
+ // 6. Push and create PR (all argv-safe, no shell interpolation)
206
+ try {
207
+ git(basePath, ["push", "-u", "origin", currentBranch]);
208
+
209
+ const ghArgs = ["pr", "create", "--base", base, "--title", title, "--body", body];
210
+ if (draft) ghArgs.push("--draft");
211
+
212
+ const prUrl = execFileSync("gh", ghArgs, { cwd: basePath, encoding: "utf-8" }).trim();
213
+
214
+ ctx.ui.notify(`PR created: ${prUrl}`, "success");
215
+ } catch (err) {
216
+ const msg = err instanceof Error ? err.message : String(err);
217
+ ctx.ui.notify(`Failed to create PR: ${msg}`, "error");
218
+ }
219
+ }