gsd-pi 2.3.11 → 2.5.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 (27) hide show
  1. package/README.md +5 -4
  2. package/dist/cli.js +4 -2
  3. package/dist/pi-migration.d.ts +14 -0
  4. package/dist/pi-migration.js +57 -0
  5. package/package.json +2 -2
  6. package/src/resources/GSD-WORKFLOW.md +7 -7
  7. package/src/resources/extensions/gsd/auto.ts +78 -23
  8. package/src/resources/extensions/gsd/docs/preferences-reference.md +27 -0
  9. package/src/resources/extensions/gsd/git-service.ts +588 -0
  10. package/src/resources/extensions/gsd/index.ts +11 -6
  11. package/src/resources/extensions/gsd/preferences.ts +51 -0
  12. package/src/resources/extensions/gsd/prompts/complete-milestone.md +1 -1
  13. package/src/resources/extensions/gsd/prompts/complete-slice.md +1 -1
  14. package/src/resources/extensions/gsd/prompts/execute-task.md +2 -1
  15. package/src/resources/extensions/gsd/prompts/plan-milestone.md +2 -0
  16. package/src/resources/extensions/gsd/prompts/plan-slice.md +2 -0
  17. package/src/resources/extensions/gsd/prompts/replan-slice.md +1 -1
  18. package/src/resources/extensions/gsd/prompts/research-milestone.md +1 -1
  19. package/src/resources/extensions/gsd/prompts/research-slice.md +1 -1
  20. package/src/resources/extensions/gsd/prompts/system.md +62 -216
  21. package/src/resources/extensions/gsd/templates/preferences.md +7 -0
  22. package/src/resources/extensions/gsd/tests/git-service.test.ts +1250 -0
  23. package/src/resources/extensions/gsd/tests/replan-slice.test.ts +0 -2
  24. package/src/resources/extensions/gsd/worktree-command.ts +48 -6
  25. package/src/resources/extensions/gsd/worktree.ts +40 -147
  26. package/src/resources/extensions/search-the-web/index.ts +16 -25
  27. package/src/resources/extensions/search-the-web/native-search.ts +157 -0
@@ -0,0 +1,588 @@
1
+ /**
2
+ * GSD Git Service
3
+ *
4
+ * Core git operations for GSD: types, constants, and pure helpers.
5
+ * Higher-level operations (commit, staging, branching) build on these.
6
+ *
7
+ * This module centralizes the GitPreferences interface, runtime exclusion
8
+ * paths, commit type inference, and the runGit shell helper.
9
+ */
10
+
11
+ import { execSync } from "node:child_process";
12
+ import { existsSync, readFileSync } from "node:fs";
13
+ import { join, sep } from "node:path";
14
+
15
+ import {
16
+ detectWorktreeName,
17
+ getSliceBranchName,
18
+ SLICE_BRANCH_RE,
19
+ } from "./worktree.ts";
20
+
21
+ // ─── Types ─────────────────────────────────────────────────────────────────
22
+
23
+ export interface GitPreferences {
24
+ auto_push?: boolean;
25
+ push_branches?: boolean;
26
+ remote?: string;
27
+ snapshots?: boolean;
28
+ pre_merge_check?: boolean | string;
29
+ commit_type?: string;
30
+ }
31
+
32
+ export interface CommitOptions {
33
+ message: string;
34
+ allowEmpty?: boolean;
35
+ }
36
+
37
+ export interface MergeSliceResult {
38
+ branch: string;
39
+ mergedCommitMessage: string;
40
+ deletedBranch: boolean;
41
+ }
42
+
43
+ export interface PreMergeCheckResult {
44
+ passed: boolean;
45
+ skipped?: boolean;
46
+ command?: string;
47
+ error?: string;
48
+ }
49
+
50
+ // ─── Constants ─────────────────────────────────────────────────────────────
51
+
52
+ /**
53
+ * GSD runtime paths that should be excluded from smart staging.
54
+ * These are transient/generated artifacts that should never be committed.
55
+ * Matches the union of SKIP_PATHS + SKIP_EXACT in worktree-manager.ts
56
+ * and the first 6 entries in gitignore.ts BASELINE_PATTERNS.
57
+ */
58
+ export const RUNTIME_EXCLUSION_PATHS: readonly string[] = [
59
+ ".gsd/activity/",
60
+ ".gsd/runtime/",
61
+ ".gsd/worktrees/",
62
+ ".gsd/auto.lock",
63
+ ".gsd/metrics.json",
64
+ ".gsd/STATE.md",
65
+ ];
66
+
67
+ // ─── Git Helper ────────────────────────────────────────────────────────────
68
+
69
+ /**
70
+ * Run a git command in the given directory.
71
+ * Returns trimmed stdout. Throws on non-zero exit unless allowFailure is set.
72
+ * When `input` is provided, it is piped to stdin.
73
+ */
74
+ export function runGit(basePath: string, args: string[], options: { allowFailure?: boolean; input?: string } = {}): string {
75
+ try {
76
+ return execSync(`git ${args.join(" ")}`, {
77
+ cwd: basePath,
78
+ stdio: [options.input != null ? "pipe" : "ignore", "pipe", "pipe"],
79
+ encoding: "utf-8",
80
+ ...(options.input != null ? { input: options.input } : {}),
81
+ }).trim();
82
+ } catch (error) {
83
+ if (options.allowFailure) return "";
84
+ const message = error instanceof Error ? error.message : String(error);
85
+ throw new Error(`git ${args.join(" ")} failed in ${basePath}: ${message}`);
86
+ }
87
+ }
88
+
89
+ // ─── Commit Type Inference ─────────────────────────────────────────────────
90
+
91
+ /**
92
+ * Keyword-to-commit-type mapping. Order matters — first match wins.
93
+ * Each entry: [keywords[], commitType]
94
+ */
95
+ const COMMIT_TYPE_RULES: [string[], string][] = [
96
+ [["fix", "bug", "patch", "hotfix"], "fix"],
97
+ [["refactor", "restructure", "reorganize"], "refactor"],
98
+ [["doc", "docs", "documentation"], "docs"],
99
+ [["test", "tests", "testing"], "test"],
100
+ [["chore", "cleanup", "clean up", "archive", "remove", "delete"], "chore"],
101
+ ];
102
+
103
+ /**
104
+ * Infer a conventional commit type from a slice title.
105
+ * Uses case-insensitive word-boundary matching against known keywords.
106
+ * Returns "feat" when no keywords match.
107
+ */
108
+ // ─── GitServiceImpl ────────────────────────────────────────────────────
109
+
110
+ export class GitServiceImpl {
111
+ readonly basePath: string;
112
+ readonly prefs: GitPreferences;
113
+
114
+ constructor(basePath: string, prefs: GitPreferences = {}) {
115
+ this.basePath = basePath;
116
+ this.prefs = prefs;
117
+ }
118
+
119
+ /** Convenience wrapper: run git in this repo's basePath. */
120
+ private git(args: string[], options: { allowFailure?: boolean; input?: string } = {}): string {
121
+ return runGit(this.basePath, args, options);
122
+ }
123
+
124
+ /**
125
+ * Smart staging: `git add -A` excluding GSD runtime paths via pathspec.
126
+ * Falls back to plain `git add -A` if the exclusion pathspec fails.
127
+ */
128
+ private smartStage(): void {
129
+ const excludes = RUNTIME_EXCLUSION_PATHS.map(p => `':(exclude)${p}'`);
130
+ const args = ["add", "-A", "--", ".", ...excludes];
131
+ try {
132
+ this.git(args);
133
+ } catch {
134
+ console.error("GitService: smart staging failed, falling back to git add -A");
135
+ this.git(["add", "-A"]);
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Stage files (smart staging) and commit.
141
+ * Returns the commit message string on success, or null if nothing to commit.
142
+ * Uses `git commit -F -` with stdin pipe for safe multi-line message handling.
143
+ */
144
+ commit(opts: CommitOptions): string | null {
145
+ this.smartStage();
146
+
147
+ // Check if anything was actually staged
148
+ const staged = this.git(["diff", "--cached", "--stat"], { allowFailure: true });
149
+ if (!staged && !opts.allowEmpty) return null;
150
+
151
+ this.git(
152
+ ["commit", "-F", "-", ...(opts.allowEmpty ? ["--allow-empty"] : [])],
153
+ { input: opts.message },
154
+ );
155
+ return opts.message;
156
+ }
157
+
158
+ /**
159
+ * Auto-commit dirty working tree with a conventional chore message.
160
+ * Returns the commit message on success, or null if nothing to commit.
161
+ */
162
+ autoCommit(unitType: string, unitId: string): string | null {
163
+ // Quick check: is there anything dirty at all?
164
+ const status = this.git(["status", "--short"], { allowFailure: true });
165
+ if (!status) return null;
166
+
167
+ this.smartStage();
168
+
169
+ // After smart staging, check if anything was actually staged
170
+ // (all changes might have been runtime files that got excluded)
171
+ const staged = this.git(["diff", "--cached", "--stat"], { allowFailure: true });
172
+ if (!staged) return null;
173
+
174
+ const message = `chore(${unitId}): auto-commit after ${unitType}`;
175
+ this.git(["commit", "-F", "-"], { input: message });
176
+ return message;
177
+ }
178
+
179
+ // ─── Branch Queries ────────────────────────────────────────────────────
180
+
181
+ /**
182
+ * Get the "main" branch for this repo.
183
+ * In a worktree: returns worktree/<name> (the worktree's base branch).
184
+ * In the main tree: origin/HEAD symbolic-ref → main/master fallback → current branch.
185
+ */
186
+ getMainBranch(): string {
187
+ const wtName = detectWorktreeName(this.basePath);
188
+ if (wtName) {
189
+ const wtBranch = `worktree/${wtName}`;
190
+ const exists = this.git(["show-ref", "--verify", `refs/heads/${wtBranch}`], { allowFailure: true });
191
+ if (exists) return wtBranch;
192
+ return this.git(["branch", "--show-current"]);
193
+ }
194
+
195
+ const symbolic = this.git(["symbolic-ref", "refs/remotes/origin/HEAD"], { allowFailure: true });
196
+ if (symbolic) {
197
+ const match = symbolic.match(/refs\/remotes\/origin\/(.+)$/);
198
+ if (match) return match[1]!;
199
+ }
200
+
201
+ const mainExists = this.git(["show-ref", "--verify", "refs/heads/main"], { allowFailure: true });
202
+ if (mainExists) return "main";
203
+
204
+ const masterExists = this.git(["show-ref", "--verify", "refs/heads/master"], { allowFailure: true });
205
+ if (masterExists) return "master";
206
+
207
+ return this.git(["branch", "--show-current"]);
208
+ }
209
+
210
+ /** Get the current branch name. */
211
+ getCurrentBranch(): string {
212
+ return this.git(["branch", "--show-current"]);
213
+ }
214
+
215
+ /** True if currently on a GSD slice branch. */
216
+ isOnSliceBranch(): boolean {
217
+ const current = this.getCurrentBranch();
218
+ return SLICE_BRANCH_RE.test(current);
219
+ }
220
+
221
+ /** Returns the slice branch name if on one, null otherwise. */
222
+ getActiveSliceBranch(): string | null {
223
+ try {
224
+ const current = this.getCurrentBranch();
225
+ return SLICE_BRANCH_RE.test(current) ? current : null;
226
+ } catch {
227
+ return null;
228
+ }
229
+ }
230
+
231
+ // ─── Branch Lifecycle ──────────────────────────────────────────────────
232
+
233
+ /**
234
+ * Check if a local branch exists.
235
+ */
236
+ private branchExists(branch: string): boolean {
237
+ try {
238
+ this.git(["show-ref", "--verify", "--quiet", `refs/heads/${branch}`]);
239
+ return true;
240
+ } catch {
241
+ return false;
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Ensure the slice branch exists and is checked out.
247
+ *
248
+ * Creates the branch from the current working branch if it's not a slice
249
+ * branch (preserves planning artifacts). Falls back to main when on another
250
+ * slice branch (avoids chaining slice branches).
251
+ *
252
+ * When creating a new branch, fetches from remote first (best-effort) to
253
+ * ensure the local main is up-to-date.
254
+ *
255
+ * Auto-commits dirty state via smart staging before checkout so runtime
256
+ * files are never accidentally committed during branch switches.
257
+ *
258
+ * Returns true if the branch was newly created.
259
+ */
260
+ ensureSliceBranch(milestoneId: string, sliceId: string): boolean {
261
+ const wtName = detectWorktreeName(this.basePath);
262
+ const branch = getSliceBranchName(milestoneId, sliceId, wtName);
263
+ const current = this.getCurrentBranch();
264
+
265
+ if (current === branch) return false;
266
+
267
+ let created = false;
268
+
269
+ if (!this.branchExists(branch)) {
270
+ // Fetch from remote before creating a new branch (best-effort).
271
+ const remotes = this.git(["remote"], { allowFailure: true });
272
+ if (remotes) {
273
+ const remote = this.prefs.remote ?? "origin";
274
+ const fetchResult = this.git(["fetch", "--prune", remote], { allowFailure: true });
275
+ // fetchResult is empty string on both success and allowFailure-caught error.
276
+ // Check if local is behind upstream (informational only).
277
+ if (remotes.split("\n").includes(remote)) {
278
+ const behind = this.git(
279
+ ["rev-list", "--count", "HEAD..@{upstream}"],
280
+ { allowFailure: true },
281
+ );
282
+ if (behind && parseInt(behind, 10) > 0) {
283
+ console.error(`GitService: local branch is ${behind} commit(s) behind upstream`);
284
+ }
285
+ }
286
+ }
287
+
288
+ // Branch from current when it's a normal working branch (not a slice).
289
+ // If already on a slice branch, fall back to main to avoid chaining.
290
+ const mainBranch = this.getMainBranch();
291
+ const base = SLICE_BRANCH_RE.test(current) ? mainBranch : current;
292
+ this.git(["branch", branch, base]);
293
+ created = true;
294
+ } else {
295
+ // Branch exists — check it's not checked out in another worktree
296
+ const worktreeList = this.git(["worktree", "list", "--porcelain"]);
297
+ if (worktreeList.includes(`branch refs/heads/${branch}`)) {
298
+ throw new Error(
299
+ `Branch "${branch}" is already in use by another worktree. ` +
300
+ `Remove that worktree first, or switch it to a different branch.`,
301
+ );
302
+ }
303
+ }
304
+
305
+ // Auto-commit dirty state via smart staging before checkout
306
+ this.autoCommit("pre-switch", current);
307
+
308
+ this.git(["checkout", branch]);
309
+ return created;
310
+ }
311
+
312
+ /**
313
+ * Switch to main, auto-committing dirty state via smart staging first.
314
+ */
315
+ switchToMain(): void {
316
+ const mainBranch = this.getMainBranch();
317
+ const current = this.getCurrentBranch();
318
+ if (current === mainBranch) return;
319
+
320
+ this.autoCommit("pre-switch", current);
321
+
322
+ this.git(["checkout", mainBranch]);
323
+ }
324
+
325
+ // ─── S05 Features ─────────────────────────────────────────────────────
326
+
327
+ /**
328
+ * Create a snapshot ref for the given label (typically a slice branch name).
329
+ * Gated on prefs.snapshots === true. Ref path: refs/gsd/snapshots/<label>/<timestamp>
330
+ * The ref points at HEAD, capturing the current commit before destructive operations.
331
+ */
332
+ createSnapshot(label: string): void {
333
+ if (this.prefs.snapshots !== true) return;
334
+
335
+ const now = new Date();
336
+ const ts = now.getFullYear().toString()
337
+ + String(now.getMonth() + 1).padStart(2, "0")
338
+ + String(now.getDate()).padStart(2, "0")
339
+ + "-"
340
+ + String(now.getHours()).padStart(2, "0")
341
+ + String(now.getMinutes()).padStart(2, "0")
342
+ + String(now.getSeconds()).padStart(2, "0");
343
+
344
+ const refPath = `refs/gsd/snapshots/${label}/${ts}`;
345
+ this.git(["update-ref", refPath, "HEAD"]);
346
+ }
347
+
348
+ /**
349
+ * Run pre-merge verification check. Auto-detects test runner from project
350
+ * files, or uses custom command from prefs.pre_merge_check.
351
+ *
352
+ * Gating:
353
+ * - `false` → skip (return passed:true, skipped:true)
354
+ * - non-empty string (not "auto") → use as custom command
355
+ * - `true`, `"auto"`, or `undefined` → auto-detect from project files
356
+ *
357
+ * Auto-detection order:
358
+ * package.json scripts.test → npm test
359
+ * package.json scripts.build (only if no test) → npm run build
360
+ * Cargo.toml → cargo test
361
+ * Makefile with test: target → make test
362
+ * pyproject.toml → python -m pytest
363
+ *
364
+ * If no runner detected in auto mode, returns passed:true (don't block).
365
+ */
366
+ runPreMergeCheck(): PreMergeCheckResult {
367
+ const pref = this.prefs.pre_merge_check;
368
+
369
+ // Explicitly disabled
370
+ if (pref === false) {
371
+ return { passed: true, skipped: true };
372
+ }
373
+
374
+ let command: string | null = null;
375
+
376
+ // Custom string command (not "auto")
377
+ if (typeof pref === "string" && pref !== "auto" && pref.trim() !== "") {
378
+ command = pref.trim();
379
+ }
380
+
381
+ // Auto-detect (true, "auto", or undefined)
382
+ if (command === null) {
383
+ command = this.detectTestRunner();
384
+ }
385
+
386
+ if (command === null) {
387
+ return { passed: true, command: "none", error: "no test runner detected" };
388
+ }
389
+
390
+ // Execute the command
391
+ try {
392
+ execSync(command, {
393
+ cwd: this.basePath,
394
+ timeout: 300_000,
395
+ stdio: ["ignore", "pipe", "pipe"],
396
+ encoding: "utf-8",
397
+ });
398
+ return { passed: true, command };
399
+ } catch (err) {
400
+ const stderr = err instanceof Error && "stderr" in err
401
+ ? String((err as { stderr: unknown }).stderr).slice(0, 2000)
402
+ : String(err).slice(0, 2000);
403
+ return { passed: false, command, error: stderr };
404
+ }
405
+ }
406
+
407
+ /**
408
+ * Detect a test/build runner from project files in basePath.
409
+ * Returns the command string or null if nothing detected.
410
+ */
411
+ private detectTestRunner(): string | null {
412
+ const pkgPath = join(this.basePath, "package.json");
413
+ if (existsSync(pkgPath)) {
414
+ try {
415
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
416
+ if (pkg?.scripts?.test) return "npm test";
417
+ if (pkg?.scripts?.build) return "npm run build";
418
+ } catch { /* invalid JSON — skip */ }
419
+ }
420
+
421
+ if (existsSync(join(this.basePath, "Cargo.toml"))) {
422
+ return "cargo test";
423
+ }
424
+
425
+ const makefilePath = join(this.basePath, "Makefile");
426
+ if (existsSync(makefilePath)) {
427
+ try {
428
+ const content = readFileSync(makefilePath, "utf-8");
429
+ if (/^test\s*:/m.test(content)) return "make test";
430
+ } catch { /* skip */ }
431
+ }
432
+
433
+ if (existsSync(join(this.basePath, "pyproject.toml"))) {
434
+ return "python -m pytest";
435
+ }
436
+
437
+ return null;
438
+ }
439
+
440
+ // ─── Merge ─────────────────────────────────────────────────────────────
441
+
442
+ /**
443
+ * Build a rich squash-commit message with a task list from branch commits.
444
+ *
445
+ * Format:
446
+ * type(scope): title
447
+ *
448
+ * Tasks:
449
+ * - commit subject 1
450
+ * - commit subject 2
451
+ *
452
+ * Branch: gsd/M001/S01
453
+ */
454
+ private buildRichCommitMessage(
455
+ commitType: string,
456
+ milestoneId: string,
457
+ sliceId: string,
458
+ sliceTitle: string,
459
+ mainBranch: string,
460
+ branch: string,
461
+ ): string {
462
+ const subject = `${commitType}(${milestoneId}/${sliceId}): ${sliceTitle}`;
463
+
464
+ // Collect branch commit subjects
465
+ const logOutput = this.git(
466
+ ["log", "--oneline", "--format=%s", `${mainBranch}..${branch}`],
467
+ { allowFailure: true },
468
+ );
469
+
470
+ if (!logOutput) return subject;
471
+
472
+ const subjects = logOutput.split("\n").filter(Boolean);
473
+ const MAX_ENTRIES = 20;
474
+ const truncated = subjects.length > MAX_ENTRIES;
475
+ const displayed = truncated ? subjects.slice(0, MAX_ENTRIES) : subjects;
476
+
477
+ const taskLines = displayed.map(s => `- ${s}`).join("\n");
478
+ const truncationLine = truncated ? `\n- ... and ${subjects.length - MAX_ENTRIES} more` : "";
479
+
480
+ return `${subject}\n\nTasks:\n${taskLines}${truncationLine}\n\nBranch: ${branch}`;
481
+ }
482
+
483
+ /**
484
+ * Squash-merge a slice branch into main and delete it.
485
+ *
486
+ * Flow: snapshot branch HEAD → squash merge → rich commit via stdin →
487
+ * auto-push (if enabled) → delete branch.
488
+ *
489
+ * Must be called from the main branch. Uses `inferCommitType(sliceTitle)`
490
+ * for the conventional commit type instead of hardcoding `feat`.
491
+ *
492
+ * Throws when:
493
+ * - Not currently on the main branch
494
+ * - The slice branch does not exist
495
+ * - The slice branch has no commits ahead of main
496
+ */
497
+ mergeSliceToMain(milestoneId: string, sliceId: string, sliceTitle: string): MergeSliceResult {
498
+ const mainBranch = this.getMainBranch();
499
+ const current = this.getCurrentBranch();
500
+
501
+ if (current !== mainBranch) {
502
+ throw new Error(
503
+ `mergeSliceToMain must be called from the main branch ("${mainBranch}"), ` +
504
+ `but currently on "${current}"`,
505
+ );
506
+ }
507
+
508
+ const wtName = detectWorktreeName(this.basePath);
509
+ const branch = getSliceBranchName(milestoneId, sliceId, wtName);
510
+
511
+ if (!this.branchExists(branch)) {
512
+ throw new Error(
513
+ `Slice branch "${branch}" does not exist. Nothing to merge.`,
514
+ );
515
+ }
516
+
517
+ // Check commits ahead
518
+ const aheadCount = this.git(["rev-list", "--count", `${mainBranch}..${branch}`]);
519
+ if (aheadCount === "0") {
520
+ throw new Error(
521
+ `Slice branch "${branch}" has no commits ahead of "${mainBranch}". Nothing to merge.`,
522
+ );
523
+ }
524
+
525
+ // Snapshot the branch HEAD before merge (gated on prefs.snapshots)
526
+ this.createSnapshot(branch);
527
+
528
+ // Build rich commit message before squash (needs branch history)
529
+ const commitType = inferCommitType(sliceTitle);
530
+ const message = this.buildRichCommitMessage(
531
+ commitType, milestoneId, sliceId, sliceTitle, mainBranch, branch,
532
+ );
533
+
534
+ // Squash merge
535
+ this.git(["merge", "--squash", branch]);
536
+
537
+ // Pre-merge check: run after squash (tests merged result), reset on failure
538
+ const checkResult = this.runPreMergeCheck();
539
+ if (!checkResult.passed && !checkResult.skipped) {
540
+ // Undo the squash merge — nothing committed yet, reset staging area
541
+ this.git(["reset", "--hard", "HEAD"]);
542
+ const cmdInfo = checkResult.command ? ` (command: ${checkResult.command})` : "";
543
+ const errInfo = checkResult.error ? `\n${checkResult.error}` : "";
544
+ throw new Error(
545
+ `Pre-merge check failed${cmdInfo}. Merge aborted.${errInfo}`,
546
+ );
547
+ }
548
+
549
+ // Commit with rich message via stdin pipe
550
+ this.git(["commit", "-F", "-"], { input: message });
551
+
552
+ // Delete the merged branch
553
+ this.git(["branch", "-D", branch]);
554
+
555
+ // Auto-push to remote if enabled
556
+ if (this.prefs.auto_push === true) {
557
+ const remote = this.prefs.remote ?? "origin";
558
+ this.git(["push", remote, mainBranch], { allowFailure: true });
559
+ }
560
+
561
+ return {
562
+ branch,
563
+ mergedCommitMessage: `${commitType}(${milestoneId}/${sliceId}): ${sliceTitle}`,
564
+ deletedBranch: true,
565
+ };
566
+ }
567
+ }
568
+
569
+ // ─── Commit Type Inference ─────────────────────────────────────────────────
570
+
571
+ export function inferCommitType(sliceTitle: string): string {
572
+ const lower = sliceTitle.toLowerCase();
573
+
574
+ for (const [keywords, commitType] of COMMIT_TYPE_RULES) {
575
+ for (const keyword of keywords) {
576
+ // "clean up" is multi-word — use indexOf for it
577
+ if (keyword.includes(" ")) {
578
+ if (lower.includes(keyword)) return commitType;
579
+ } else {
580
+ // Word boundary match: keyword must not be surrounded by word chars
581
+ const re = new RegExp(`\\b${keyword}\\b`, "i");
582
+ if (re.test(lower)) return commitType;
583
+ }
584
+ }
585
+ }
586
+
587
+ return "feat";
588
+ }
@@ -158,14 +158,19 @@ export default function (pi: ExtensionAPI) {
158
158
 
159
159
  // ── session_start: render branded GSD header + remote channel status ──
160
160
  pi.on("session_start", async (_event, ctx) => {
161
- const theme = ctx.ui.theme;
162
- const version = process.env.GSD_VERSION || "0.0.0";
161
+ // Theme access throws in RPC mode (no TUI) — header is decorative, skip it
162
+ try {
163
+ const theme = ctx.ui.theme;
164
+ const version = process.env.GSD_VERSION || "0.0.0";
163
165
 
164
- const logoText = GSD_LOGO_LINES.map((line) => theme.fg("accent", line)).join("\n");
165
- const titleLine = ` ${theme.bold("Get Shit Done")} ${theme.fg("dim", `v${version}`)}`;
166
+ const logoText = GSD_LOGO_LINES.map((line) => theme.fg("accent", line)).join("\n");
167
+ const titleLine = ` ${theme.bold("Get Shit Done")} ${theme.fg("dim", `v${version}`)}`;
166
168
 
167
- const headerContent = `${logoText}\n${titleLine}`;
168
- ctx.ui.setHeader((_ui, _theme) => new Text(headerContent, 1, 0));
169
+ const headerContent = `${logoText}\n${titleLine}`;
170
+ ctx.ui.setHeader((_ui, _theme) => new Text(headerContent, 1, 0));
171
+ } catch {
172
+ // RPC mode — no TUI, skip header rendering
173
+ }
169
174
 
170
175
  // Notify remote questions status if configured
171
176
  try {
@@ -2,6 +2,7 @@ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
2
2
  import { homedir } from "node:os";
3
3
  import { isAbsolute, join } from "node:path";
4
4
  import { getAgentDir } from "@mariozechner/pi-coding-agent";
5
+ import type { GitPreferences } from "./git-service.ts";
5
6
 
6
7
  const GLOBAL_PREFERENCES_PATH = join(homedir(), ".gsd", "preferences.md");
7
8
  const LEGACY_GLOBAL_PREFERENCES_PATH = join(homedir(), ".pi", "agent", "gsd-preferences.md");
@@ -51,6 +52,7 @@ export interface GSDPreferences {
51
52
  uat_dispatch?: boolean;
52
53
  budget_ceiling?: number;
53
54
  remote_questions?: RemoteQuestionsConfig;
55
+ git?: GitPreferences;
54
56
  }
55
57
 
56
58
  export interface LoadedGSDPreferences {
@@ -511,6 +513,9 @@ function mergePreferences(base: GSDPreferences, override: GSDPreferences): GSDPr
511
513
  remote_questions: override.remote_questions
512
514
  ? { ...(base.remote_questions ?? {}), ...override.remote_questions }
513
515
  : base.remote_questions,
516
+ git: (base.git || override.git)
517
+ ? { ...(base.git ?? {}), ...(override.git ?? {}) }
518
+ : undefined,
514
519
  };
515
520
  }
516
521
 
@@ -594,6 +599,52 @@ function validatePreferences(preferences: GSDPreferences): {
594
599
  }
595
600
  }
596
601
 
602
+ // ─── Git Preferences ───────────────────────────────────────────────────
603
+ if (preferences.git && typeof preferences.git === "object") {
604
+ const git: Record<string, unknown> = {};
605
+ const g = preferences.git as Record<string, unknown>;
606
+
607
+ if (g.auto_push !== undefined) {
608
+ if (typeof g.auto_push === "boolean") git.auto_push = g.auto_push;
609
+ else errors.push("git.auto_push must be a boolean");
610
+ }
611
+ if (g.push_branches !== undefined) {
612
+ if (typeof g.push_branches === "boolean") git.push_branches = g.push_branches;
613
+ else errors.push("git.push_branches must be a boolean");
614
+ }
615
+ if (g.remote !== undefined) {
616
+ if (typeof g.remote === "string" && g.remote.trim() !== "") git.remote = g.remote.trim();
617
+ else errors.push("git.remote must be a non-empty string");
618
+ }
619
+ if (g.snapshots !== undefined) {
620
+ if (typeof g.snapshots === "boolean") git.snapshots = g.snapshots;
621
+ else errors.push("git.snapshots must be a boolean");
622
+ }
623
+ if (g.pre_merge_check !== undefined) {
624
+ if (typeof g.pre_merge_check === "boolean") {
625
+ git.pre_merge_check = g.pre_merge_check;
626
+ } else if (typeof g.pre_merge_check === "string" && g.pre_merge_check.trim() !== "") {
627
+ git.pre_merge_check = g.pre_merge_check.trim();
628
+ } else {
629
+ errors.push("git.pre_merge_check must be a boolean or a non-empty string command");
630
+ }
631
+ }
632
+ if (g.commit_type !== undefined) {
633
+ const validCommitTypes = new Set([
634
+ "feat", "fix", "refactor", "docs", "test", "chore", "perf", "ci", "build", "style",
635
+ ]);
636
+ if (typeof g.commit_type === "string" && validCommitTypes.has(g.commit_type)) {
637
+ git.commit_type = g.commit_type;
638
+ } else {
639
+ errors.push(`git.commit_type must be one of: feat, fix, refactor, docs, test, chore, perf, ci, build, style`);
640
+ }
641
+ }
642
+
643
+ if (Object.keys(git).length > 0) {
644
+ validated.git = git as GitPreferences;
645
+ }
646
+ }
647
+
597
648
  return { preferences: validated, errors };
598
649
  }
599
650