gsd-pi 2.61.0-dev.7aed0bf → 2.62.0-dev.a987556

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 (119) hide show
  1. package/dist/resources/extensions/ask-user-questions.js +47 -3
  2. package/dist/resources/extensions/gsd/auto-start.js +11 -6
  3. package/dist/resources/extensions/gsd/auto-timers.js +8 -2
  4. package/dist/resources/extensions/gsd/auto-worktree.js +19 -0
  5. package/dist/resources/extensions/gsd/auto.js +24 -0
  6. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +4 -0
  7. package/dist/resources/extensions/gsd/bootstrap/tool-call-loop-guard.js +11 -1
  8. package/dist/resources/extensions/gsd/commands-handlers.js +18 -7
  9. package/dist/resources/extensions/gsd/db-writer.js +64 -28
  10. package/dist/resources/extensions/gsd/preferences-models.js +74 -0
  11. package/dist/resources/extensions/gsd/preferences-skills.js +6 -1
  12. package/dist/resources/extensions/gsd/prompts/guided-discuss-milestone.md +1 -1
  13. package/dist/resources/extensions/gsd/prompts/guided-discuss-slice.md +1 -1
  14. package/dist/resources/extensions/gsd/skill-catalog.js +6 -4
  15. package/dist/resources/extensions/gsd/skill-discovery.js +24 -6
  16. package/dist/resources/extensions/gsd/skill-health.js +7 -3
  17. package/dist/resources/extensions/gsd/skill-telemetry.js +5 -2
  18. package/dist/web/standalone/.next/BUILD_ID +1 -1
  19. package/dist/web/standalone/.next/app-path-routes-manifest.json +16 -16
  20. package/dist/web/standalone/.next/build-manifest.json +2 -2
  21. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  22. package/dist/web/standalone/.next/required-server-files.json +1 -1
  23. package/dist/web/standalone/.next/server/app/_global-error.html +2 -2
  24. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  25. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  26. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  27. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  28. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  29. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  30. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  31. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  32. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  33. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  34. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  35. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  36. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  37. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  38. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  39. package/dist/web/standalone/.next/server/app/index.html +1 -1
  40. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  41. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  42. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  43. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  44. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  45. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  46. package/dist/web/standalone/.next/server/app-paths-manifest.json +16 -16
  47. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  48. package/dist/web/standalone/.next/server/pages/500.html +2 -2
  49. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  50. package/dist/web/standalone/server.js +1 -1
  51. package/package.json +1 -1
  52. package/packages/mcp-server/src/cli.ts +1 -1
  53. package/packages/mcp-server/src/index.ts +15 -1
  54. package/packages/mcp-server/src/readers/captures.ts +119 -0
  55. package/packages/mcp-server/src/readers/doctor-lite.ts +225 -0
  56. package/packages/mcp-server/src/readers/index.ts +16 -0
  57. package/packages/mcp-server/src/readers/knowledge.ts +111 -0
  58. package/packages/mcp-server/src/readers/metrics.ts +118 -0
  59. package/packages/mcp-server/src/readers/paths.ts +217 -0
  60. package/packages/mcp-server/src/readers/readers.test.ts +509 -0
  61. package/packages/mcp-server/src/readers/roadmap.ts +263 -0
  62. package/packages/mcp-server/src/readers/state.ts +223 -0
  63. package/packages/mcp-server/src/server.ts +134 -3
  64. package/packages/pi-ai/dist/utils/repair-tool-json.d.ts +26 -6
  65. package/packages/pi-ai/dist/utils/repair-tool-json.d.ts.map +1 -1
  66. package/packages/pi-ai/dist/utils/repair-tool-json.js +67 -9
  67. package/packages/pi-ai/dist/utils/repair-tool-json.js.map +1 -1
  68. package/packages/pi-ai/dist/utils/tests/repair-tool-json.test.js +73 -1
  69. package/packages/pi-ai/dist/utils/tests/repair-tool-json.test.js.map +1 -1
  70. package/packages/pi-ai/src/utils/repair-tool-json.ts +74 -10
  71. package/packages/pi-ai/src/utils/tests/repair-tool-json.test.ts +94 -1
  72. package/packages/pi-coding-agent/dist/core/agent-session-model-switch.test.d.ts +2 -0
  73. package/packages/pi-coding-agent/dist/core/agent-session-model-switch.test.d.ts.map +1 -0
  74. package/packages/pi-coding-agent/dist/core/agent-session-model-switch.test.js +16 -0
  75. package/packages/pi-coding-agent/dist/core/agent-session-model-switch.test.js.map +1 -0
  76. package/packages/pi-coding-agent/dist/core/agent-session.d.ts.map +1 -1
  77. package/packages/pi-coding-agent/dist/core/agent-session.js +4 -0
  78. package/packages/pi-coding-agent/dist/core/agent-session.js.map +1 -1
  79. package/packages/pi-coding-agent/dist/core/retry-handler.d.ts +3 -0
  80. package/packages/pi-coding-agent/dist/core/retry-handler.d.ts.map +1 -1
  81. package/packages/pi-coding-agent/dist/core/retry-handler.js +48 -16
  82. package/packages/pi-coding-agent/dist/core/retry-handler.js.map +1 -1
  83. package/packages/pi-coding-agent/dist/core/retry-handler.test.js +20 -3
  84. package/packages/pi-coding-agent/dist/core/retry-handler.test.js.map +1 -1
  85. package/packages/pi-coding-agent/package.json +1 -1
  86. package/packages/pi-coding-agent/src/core/agent-session-model-switch.test.ts +21 -0
  87. package/packages/pi-coding-agent/src/core/agent-session.ts +4 -0
  88. package/packages/pi-coding-agent/src/core/retry-handler.test.ts +30 -3
  89. package/packages/pi-coding-agent/src/core/retry-handler.ts +49 -16
  90. package/pkg/package.json +1 -1
  91. package/src/resources/extensions/ask-user-questions.ts +60 -4
  92. package/src/resources/extensions/gsd/auto-start.ts +11 -6
  93. package/src/resources/extensions/gsd/auto-timers.ts +8 -2
  94. package/src/resources/extensions/gsd/auto-worktree.ts +18 -0
  95. package/src/resources/extensions/gsd/auto.ts +25 -0
  96. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +4 -0
  97. package/src/resources/extensions/gsd/bootstrap/tool-call-loop-guard.ts +13 -1
  98. package/src/resources/extensions/gsd/commands-handlers.ts +20 -7
  99. package/src/resources/extensions/gsd/db-writer.ts +67 -30
  100. package/src/resources/extensions/gsd/preferences-models.ts +78 -0
  101. package/src/resources/extensions/gsd/preferences-skills.ts +6 -1
  102. package/src/resources/extensions/gsd/prompts/guided-discuss-milestone.md +1 -1
  103. package/src/resources/extensions/gsd/prompts/guided-discuss-slice.md +1 -1
  104. package/src/resources/extensions/gsd/skill-catalog.ts +6 -3
  105. package/src/resources/extensions/gsd/skill-discovery.ts +23 -6
  106. package/src/resources/extensions/gsd/skill-health.ts +7 -3
  107. package/src/resources/extensions/gsd/skill-telemetry.ts +5 -2
  108. package/src/resources/extensions/gsd/tests/ask-user-questions-dedup.test.ts +120 -0
  109. package/src/resources/extensions/gsd/tests/auto-start-model-capture.test.ts +22 -2
  110. package/src/resources/extensions/gsd/tests/auto-wrapup-inflight-guard.test.ts +107 -0
  111. package/src/resources/extensions/gsd/tests/claude-skill-dirs.test.ts +51 -0
  112. package/src/resources/extensions/gsd/tests/db-writer.test.ts +41 -0
  113. package/src/resources/extensions/gsd/tests/model-isolation.test.ts +75 -1
  114. package/src/resources/extensions/gsd/tests/steer-worktree-path.test.ts +108 -0
  115. package/src/resources/extensions/gsd/tests/tool-call-loop-guard.test.ts +17 -4
  116. package/src/resources/extensions/gsd/tests/worktree-db-respawn-truncation.test.ts +81 -2
  117. package/src/resources/extensions/shared/tests/ask-user-freetext.test.ts +6 -1
  118. /package/dist/web/standalone/.next/static/{b7FOoMHaUb3FPoLNbxar4 → AsCFY1___4XTI6GkTQkFb}/_buildManifest.js +0 -0
  119. /package/dist/web/standalone/.next/static/{b7FOoMHaUb3FPoLNbxar4 → AsCFY1___4XTI6GkTQkFb}/_ssgManifest.js +0 -0
@@ -0,0 +1,225 @@
1
+ // GSD MCP Server — lightweight structural health checks
2
+ // Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
3
+
4
+ import { existsSync, readFileSync } from 'node:fs';
5
+ import {
6
+ resolveGsdRoot,
7
+ resolveRootFile,
8
+ findMilestoneIds,
9
+ resolveMilestoneFile,
10
+ resolveMilestoneDir,
11
+ findSliceIds,
12
+ resolveSliceFile,
13
+ findTaskFiles,
14
+ } from './paths.js';
15
+
16
+ // ---------------------------------------------------------------------------
17
+ // Types
18
+ // ---------------------------------------------------------------------------
19
+
20
+ export type Severity = 'info' | 'warning' | 'error';
21
+
22
+ export interface DoctorIssue {
23
+ severity: Severity;
24
+ code: string;
25
+ scope: 'project' | 'milestone' | 'slice' | 'task';
26
+ unitId: string;
27
+ message: string;
28
+ file?: string;
29
+ }
30
+
31
+ export interface DoctorResult {
32
+ ok: boolean;
33
+ issues: DoctorIssue[];
34
+ counts: { error: number; warning: number; info: number };
35
+ }
36
+
37
+ // ---------------------------------------------------------------------------
38
+ // Check implementations
39
+ // ---------------------------------------------------------------------------
40
+
41
+ function checkProjectLevel(gsdRoot: string, issues: DoctorIssue[]): void {
42
+ // PROJECT.md should exist
43
+ const projectPath = resolveRootFile(gsdRoot, 'PROJECT.md');
44
+ if (!existsSync(projectPath)) {
45
+ issues.push({
46
+ severity: 'warning',
47
+ code: 'missing_project_md',
48
+ scope: 'project',
49
+ unitId: '',
50
+ message: 'PROJECT.md is missing — project lacks a description',
51
+ file: projectPath,
52
+ });
53
+ }
54
+
55
+ // STATE.md should exist if milestones exist
56
+ const milestones = findMilestoneIds(gsdRoot);
57
+ if (milestones.length > 0) {
58
+ const statePath = resolveRootFile(gsdRoot, 'STATE.md');
59
+ if (!existsSync(statePath)) {
60
+ issues.push({
61
+ severity: 'warning',
62
+ code: 'missing_state_md',
63
+ scope: 'project',
64
+ unitId: '',
65
+ message: 'STATE.md is missing — run /gsd status to regenerate',
66
+ file: statePath,
67
+ });
68
+ }
69
+ }
70
+ }
71
+
72
+ function checkMilestoneLevel(gsdRoot: string, mid: string, issues: DoctorIssue[]): void {
73
+ const mDir = resolveMilestoneDir(gsdRoot, mid);
74
+ if (!mDir) {
75
+ issues.push({
76
+ severity: 'error',
77
+ code: 'missing_milestone_dir',
78
+ scope: 'milestone',
79
+ unitId: mid,
80
+ message: `Milestone directory for ${mid} not found`,
81
+ });
82
+ return;
83
+ }
84
+
85
+ // CONTEXT.md should exist
86
+ const ctxPath = resolveMilestoneFile(gsdRoot, mid, 'CONTEXT');
87
+ if (!ctxPath || !existsSync(ctxPath)) {
88
+ // Check for draft
89
+ const draftPath = resolveMilestoneFile(gsdRoot, mid, 'CONTEXT-DRAFT');
90
+ if (!draftPath || !existsSync(draftPath)) {
91
+ issues.push({
92
+ severity: 'warning',
93
+ code: 'missing_context',
94
+ scope: 'milestone',
95
+ unitId: mid,
96
+ message: `${mid} has no CONTEXT.md — milestone lacks defined scope`,
97
+ });
98
+ }
99
+ }
100
+
101
+ // ROADMAP.md should exist if slices exist
102
+ const sliceIds = findSliceIds(gsdRoot, mid);
103
+ if (sliceIds.length > 0) {
104
+ const roadmapPath = resolveMilestoneFile(gsdRoot, mid, 'ROADMAP');
105
+ if (!roadmapPath || !existsSync(roadmapPath)) {
106
+ issues.push({
107
+ severity: 'warning',
108
+ code: 'missing_roadmap',
109
+ scope: 'milestone',
110
+ unitId: mid,
111
+ message: `${mid} has ${sliceIds.length} slices but no ROADMAP.md`,
112
+ });
113
+ }
114
+ }
115
+
116
+ // Check if all slices done but no SUMMARY
117
+ if (sliceIds.length > 0) {
118
+ const allDone = sliceIds.every((sid) => {
119
+ const tasks = findTaskFiles(gsdRoot, mid, sid);
120
+ return tasks.length > 0 && tasks.every((t) => t.hasSummary);
121
+ });
122
+ const summaryPath = resolveMilestoneFile(gsdRoot, mid, 'SUMMARY');
123
+ if (allDone && (!summaryPath || !existsSync(summaryPath))) {
124
+ issues.push({
125
+ severity: 'error',
126
+ code: 'all_slices_done_missing_summary',
127
+ scope: 'milestone',
128
+ unitId: mid,
129
+ message: `${mid} has all slices completed but no SUMMARY.md`,
130
+ });
131
+ }
132
+ }
133
+ }
134
+
135
+ function checkSliceLevel(
136
+ gsdRoot: string, mid: string, sid: string, issues: DoctorIssue[],
137
+ ): void {
138
+ const unitId = `${mid}/${sid}`;
139
+
140
+ // PLAN.md should exist
141
+ const planPath = resolveSliceFile(gsdRoot, mid, sid, 'PLAN');
142
+ if (!planPath || !existsSync(planPath)) {
143
+ issues.push({
144
+ severity: 'error',
145
+ code: 'missing_slice_plan',
146
+ scope: 'slice',
147
+ unitId,
148
+ message: `${unitId} has no PLAN.md`,
149
+ });
150
+ }
151
+
152
+ // Tasks should have plans
153
+ const tasks = findTaskFiles(gsdRoot, mid, sid);
154
+ for (const task of tasks) {
155
+ const taskUnitId = `${unitId}/${task.id}`;
156
+ if (!task.hasPlan) {
157
+ issues.push({
158
+ severity: 'warning',
159
+ code: 'missing_task_plan',
160
+ scope: 'task',
161
+ unitId: taskUnitId,
162
+ message: `${taskUnitId} has a summary but no plan file`,
163
+ });
164
+ }
165
+ }
166
+
167
+ // Check for empty slice (directory exists but no tasks or plan)
168
+ if (tasks.length === 0 && (!planPath || !existsSync(planPath))) {
169
+ issues.push({
170
+ severity: 'warning',
171
+ code: 'empty_slice',
172
+ scope: 'slice',
173
+ unitId,
174
+ message: `${unitId} has no plan and no tasks — may be abandoned`,
175
+ });
176
+ }
177
+ }
178
+
179
+ // ---------------------------------------------------------------------------
180
+ // Public API
181
+ // ---------------------------------------------------------------------------
182
+
183
+ export function runDoctorLite(projectDir: string, scope?: string): DoctorResult {
184
+ const gsdRoot = resolveGsdRoot(projectDir);
185
+ const issues: DoctorIssue[] = [];
186
+
187
+ if (!existsSync(gsdRoot)) {
188
+ return {
189
+ ok: true,
190
+ issues: [{
191
+ severity: 'info',
192
+ code: 'no_gsd_directory',
193
+ scope: 'project',
194
+ unitId: '',
195
+ message: 'No .gsd/ directory found — project not initialized',
196
+ }],
197
+ counts: { error: 0, warning: 0, info: 1 },
198
+ };
199
+ }
200
+
201
+ // Project-level checks
202
+ checkProjectLevel(gsdRoot, issues);
203
+
204
+ // Milestone + slice checks
205
+ const milestoneIds = scope
206
+ ? findMilestoneIds(gsdRoot).filter((id) => id === scope)
207
+ : findMilestoneIds(gsdRoot);
208
+
209
+ for (const mid of milestoneIds) {
210
+ checkMilestoneLevel(gsdRoot, mid, issues);
211
+
212
+ const sliceIds = findSliceIds(gsdRoot, mid);
213
+ for (const sid of sliceIds) {
214
+ checkSliceLevel(gsdRoot, mid, sid, issues);
215
+ }
216
+ }
217
+
218
+ const counts = {
219
+ error: issues.filter((i) => i.severity === 'error').length,
220
+ warning: issues.filter((i) => i.severity === 'warning').length,
221
+ info: issues.filter((i) => i.severity === 'info').length,
222
+ };
223
+
224
+ return { ok: counts.error === 0, issues, counts };
225
+ }
@@ -0,0 +1,16 @@
1
+ // GSD MCP Server — readers barrel export
2
+ // Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
3
+
4
+ export { resolveGsdRoot, resolveRootFile } from './paths.js';
5
+ export { readProgress } from './state.js';
6
+ export type { ProgressResult } from './state.js';
7
+ export { readRoadmap } from './roadmap.js';
8
+ export type { RoadmapResult, MilestoneInfo, SliceInfo, TaskInfo } from './roadmap.js';
9
+ export { readHistory } from './metrics.js';
10
+ export type { HistoryResult, MetricsUnit } from './metrics.js';
11
+ export { readCaptures } from './captures.js';
12
+ export type { CapturesResult, CaptureEntry } from './captures.js';
13
+ export { readKnowledge } from './knowledge.js';
14
+ export type { KnowledgeResult, KnowledgeEntry } from './knowledge.js';
15
+ export { runDoctorLite } from './doctor-lite.js';
16
+ export type { DoctorResult, DoctorIssue } from './doctor-lite.js';
@@ -0,0 +1,111 @@
1
+ // GSD MCP Server — knowledge base reader
2
+ // Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
3
+
4
+ import { readFileSync, existsSync } from 'node:fs';
5
+ import { resolveGsdRoot, resolveRootFile } from './paths.js';
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // Types
9
+ // ---------------------------------------------------------------------------
10
+
11
+ export type KnowledgeType = 'rule' | 'pattern' | 'lesson';
12
+
13
+ export interface KnowledgeEntry {
14
+ id: string;
15
+ type: KnowledgeType;
16
+ scope: string;
17
+ content: string;
18
+ addedAt: string;
19
+ }
20
+
21
+ export interface KnowledgeResult {
22
+ entries: KnowledgeEntry[];
23
+ counts: { rules: number; patterns: number; lessons: number };
24
+ }
25
+
26
+ // ---------------------------------------------------------------------------
27
+ // Parser
28
+ // ---------------------------------------------------------------------------
29
+
30
+ function parseTableRows(section: string, type: KnowledgeType): KnowledgeEntry[] {
31
+ const entries: KnowledgeEntry[] = [];
32
+ const lines = section.split('\n');
33
+
34
+ for (const line of lines) {
35
+ if (!line.includes('|')) continue;
36
+ const cells = line.split('|').map((c) => c.trim()).filter(Boolean);
37
+ if (cells.length < 3) continue;
38
+ // Skip header/separator
39
+ if (cells[0].startsWith('#') || cells[0].startsWith('-')) continue;
40
+
41
+ const id = cells[0];
42
+ if (!/^[KPL]\d+$/i.test(id)) continue;
43
+
44
+ if (type === 'rule' && cells.length >= 5) {
45
+ entries.push({
46
+ id, type, scope: cells[1], content: cells[2], addedAt: cells[4] ?? '',
47
+ });
48
+ } else if (type === 'pattern' && cells.length >= 4) {
49
+ entries.push({
50
+ id, type, scope: cells[2] ?? '', content: cells[1], addedAt: cells[3] ?? '',
51
+ });
52
+ } else if (type === 'lesson' && cells.length >= 5) {
53
+ entries.push({
54
+ id, type, scope: cells[4] ?? '',
55
+ content: `${cells[1]} — Root cause: ${cells[2]} — Fix: ${cells[3]}`,
56
+ addedAt: '',
57
+ });
58
+ }
59
+ }
60
+
61
+ return entries;
62
+ }
63
+
64
+ function parseKnowledgeMarkdown(content: string): KnowledgeEntry[] {
65
+ const entries: KnowledgeEntry[] = [];
66
+
67
+ // Find ## Rules section
68
+ const rulesMatch = content.match(/## Rules\s*\n([\s\S]*?)(?=\n## |$)/i);
69
+ if (rulesMatch) {
70
+ entries.push(...parseTableRows(rulesMatch[1], 'rule'));
71
+ }
72
+
73
+ // Find ## Patterns section
74
+ const patternsMatch = content.match(/## Patterns\s*\n([\s\S]*?)(?=\n## |$)/i);
75
+ if (patternsMatch) {
76
+ entries.push(...parseTableRows(patternsMatch[1], 'pattern'));
77
+ }
78
+
79
+ // Find ## Lessons Learned section
80
+ const lessonsMatch = content.match(/## Lessons Learned\s*\n([\s\S]*?)(?=\n## |$)/i);
81
+ if (lessonsMatch) {
82
+ entries.push(...parseTableRows(lessonsMatch[1], 'lesson'));
83
+ }
84
+
85
+ return entries;
86
+ }
87
+
88
+ // ---------------------------------------------------------------------------
89
+ // Public API
90
+ // ---------------------------------------------------------------------------
91
+
92
+ export function readKnowledge(projectDir: string): KnowledgeResult {
93
+ const gsd = resolveGsdRoot(projectDir);
94
+ const knowledgePath = resolveRootFile(gsd, 'KNOWLEDGE.md');
95
+
96
+ if (!existsSync(knowledgePath)) {
97
+ return { entries: [], counts: { rules: 0, patterns: 0, lessons: 0 } };
98
+ }
99
+
100
+ const content = readFileSync(knowledgePath, 'utf-8');
101
+ const entries = parseKnowledgeMarkdown(content);
102
+
103
+ return {
104
+ entries,
105
+ counts: {
106
+ rules: entries.filter((e) => e.type === 'rule').length,
107
+ patterns: entries.filter((e) => e.type === 'pattern').length,
108
+ lessons: entries.filter((e) => e.type === 'lesson').length,
109
+ },
110
+ };
111
+ }
@@ -0,0 +1,118 @@
1
+ // GSD MCP Server — metrics/history reader
2
+ // Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
3
+
4
+ import { readFileSync, existsSync } from 'node:fs';
5
+ import { resolveGsdRoot, resolveRootFile } from './paths.js';
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // Types
9
+ // ---------------------------------------------------------------------------
10
+
11
+ export interface MetricsUnit {
12
+ type: string;
13
+ id: string;
14
+ model: string;
15
+ startedAt: number;
16
+ finishedAt: number;
17
+ tokens: {
18
+ input: number;
19
+ output: number;
20
+ cacheRead: number;
21
+ cacheWrite: number;
22
+ total: number;
23
+ };
24
+ cost: number;
25
+ toolCalls: number;
26
+ apiRequests: number;
27
+ }
28
+
29
+ export interface HistoryResult {
30
+ entries: MetricsUnit[];
31
+ totals: {
32
+ cost: number;
33
+ tokens: { input: number; output: number; total: number };
34
+ units: number;
35
+ durationMs: number;
36
+ };
37
+ }
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Parser
41
+ // ---------------------------------------------------------------------------
42
+
43
+ function parseMetricsJson(content: string): MetricsUnit[] {
44
+ try {
45
+ const data = JSON.parse(content);
46
+ if (!data.units || !Array.isArray(data.units)) return [];
47
+
48
+ return data.units.map((u: Record<string, unknown>) => ({
49
+ type: String(u.type ?? 'unknown'),
50
+ id: String(u.id ?? ''),
51
+ model: String(u.model ?? 'unknown'),
52
+ startedAt: Number(u.startedAt ?? 0),
53
+ finishedAt: Number(u.finishedAt ?? 0),
54
+ tokens: {
55
+ input: Number((u.tokens as Record<string, unknown>)?.input ?? 0),
56
+ output: Number((u.tokens as Record<string, unknown>)?.output ?? 0),
57
+ cacheRead: Number((u.tokens as Record<string, unknown>)?.cacheRead ?? 0),
58
+ cacheWrite: Number((u.tokens as Record<string, unknown>)?.cacheWrite ?? 0),
59
+ total: Number((u.tokens as Record<string, unknown>)?.total ?? 0),
60
+ },
61
+ cost: Number(u.cost ?? 0),
62
+ toolCalls: Number(u.toolCalls ?? 0),
63
+ apiRequests: Number(u.apiRequests ?? 0),
64
+ }));
65
+ } catch {
66
+ return [];
67
+ }
68
+ }
69
+
70
+ // ---------------------------------------------------------------------------
71
+ // Public API
72
+ // ---------------------------------------------------------------------------
73
+
74
+ export function readHistory(projectDir: string, limit?: number): HistoryResult {
75
+ const gsd = resolveGsdRoot(projectDir);
76
+
77
+ // metrics.json (primary)
78
+ const metricsPath = resolveRootFile(gsd, 'metrics.json');
79
+ let units: MetricsUnit[] = [];
80
+
81
+ if (existsSync(metricsPath)) {
82
+ const content = readFileSync(metricsPath, 'utf-8');
83
+ units = parseMetricsJson(content);
84
+ }
85
+
86
+ // Sort by startedAt descending (most recent first)
87
+ units.sort((a, b) => b.startedAt - a.startedAt);
88
+
89
+ // Apply limit
90
+ if (limit && limit > 0) {
91
+ units = units.slice(0, limit);
92
+ }
93
+
94
+ // Compute totals from ALL units (not just limited set)
95
+ const allUnits = existsSync(metricsPath)
96
+ ? parseMetricsJson(readFileSync(metricsPath, 'utf-8'))
97
+ : [];
98
+
99
+ const totals = {
100
+ cost: 0,
101
+ tokens: { input: 0, output: 0, total: 0 },
102
+ units: allUnits.length,
103
+ durationMs: 0,
104
+ };
105
+
106
+ for (const u of allUnits) {
107
+ totals.cost += u.cost;
108
+ totals.tokens.input += u.tokens.input;
109
+ totals.tokens.output += u.tokens.output;
110
+ totals.tokens.total += u.tokens.total;
111
+ totals.durationMs += (u.finishedAt - u.startedAt);
112
+ }
113
+
114
+ // Round cost to 4 decimal places
115
+ totals.cost = Math.round(totals.cost * 10000) / 10000;
116
+
117
+ return { entries: units, totals };
118
+ }
@@ -0,0 +1,217 @@
1
+ // GSD MCP Server — .gsd/ directory resolution
2
+ // Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
3
+
4
+ import { existsSync, statSync, readdirSync } from 'node:fs';
5
+ import { join, resolve, dirname, basename } from 'node:path';
6
+ import { execFileSync } from 'node:child_process';
7
+
8
+ /**
9
+ * Resolve the .gsd/ root directory for a project.
10
+ *
11
+ * Probes in order:
12
+ * 1. projectDir/.gsd (fast path)
13
+ * 2. git repo root/.gsd
14
+ * 3. Walk up from projectDir
15
+ * 4. Fallback: projectDir/.gsd (even if missing — for init)
16
+ */
17
+ export function resolveGsdRoot(projectDir: string): string {
18
+ const resolved = resolve(projectDir);
19
+
20
+ // Fast path: .gsd/ in the given directory
21
+ const direct = join(resolved, '.gsd');
22
+ if (existsSync(direct) && statSync(direct).isDirectory()) {
23
+ return direct;
24
+ }
25
+
26
+ // Try git repo root
27
+ try {
28
+ const gitRoot = execFileSync('git', ['rev-parse', '--show-toplevel'], {
29
+ cwd: resolved,
30
+ encoding: 'utf-8',
31
+ stdio: ['pipe', 'pipe', 'pipe'],
32
+ }).trim();
33
+ const gitGsd = join(gitRoot, '.gsd');
34
+ if (existsSync(gitGsd) && statSync(gitGsd).isDirectory()) {
35
+ return gitGsd;
36
+ }
37
+ } catch {
38
+ // Not a git repo or git not available
39
+ }
40
+
41
+ // Walk up from projectDir
42
+ let dir = resolved;
43
+ while (dir !== dirname(dir)) {
44
+ const candidate = join(dir, '.gsd');
45
+ if (existsSync(candidate) && statSync(candidate).isDirectory()) {
46
+ return candidate;
47
+ }
48
+ dir = dirname(dir);
49
+ }
50
+
51
+ // Fallback
52
+ return direct;
53
+ }
54
+
55
+ /** Resolve path to a .gsd/ root file (STATE.md, KNOWLEDGE.md, etc.) */
56
+ export function resolveRootFile(gsdRoot: string, name: string): string {
57
+ return join(gsdRoot, name);
58
+ }
59
+
60
+ /** Resolve path to milestones directory */
61
+ export function milestonesDir(gsdRoot: string): string {
62
+ return join(gsdRoot, 'milestones');
63
+ }
64
+
65
+ /**
66
+ * Find all milestone directory IDs (M001, M002, etc.).
67
+ * Handles both bare (M001/) and descriptor (M001-FLIGHT-SIM/) naming.
68
+ */
69
+ export function findMilestoneIds(gsdRoot: string): string[] {
70
+ const dir = milestonesDir(gsdRoot);
71
+ if (!existsSync(dir)) return [];
72
+
73
+ const entries = readdirSync(dir, { withFileTypes: true });
74
+ const ids: string[] = [];
75
+
76
+ for (const entry of entries) {
77
+ if (!entry.isDirectory()) continue;
78
+ const match = entry.name.match(/^(M\d+)/);
79
+ if (match) ids.push(match[1]);
80
+ }
81
+
82
+ return ids.sort();
83
+ }
84
+
85
+ /**
86
+ * Resolve the actual directory name for a milestone ID.
87
+ * M001 might live in M001/ or M001-SOME-DESCRIPTOR/.
88
+ */
89
+ export function resolveMilestoneDir(gsdRoot: string, milestoneId: string): string | null {
90
+ const dir = milestonesDir(gsdRoot);
91
+ if (!existsSync(dir)) return null;
92
+
93
+ // Fast path: exact match
94
+ const exact = join(dir, milestoneId);
95
+ if (existsSync(exact) && statSync(exact).isDirectory()) return exact;
96
+
97
+ // Prefix match
98
+ const entries = readdirSync(dir, { withFileTypes: true });
99
+ for (const entry of entries) {
100
+ if (entry.isDirectory() && entry.name.startsWith(milestoneId)) {
101
+ return join(dir, entry.name);
102
+ }
103
+ }
104
+
105
+ return null;
106
+ }
107
+
108
+ /**
109
+ * Resolve a milestone-level file (M001-ROADMAP.md, M001-CONTEXT.md, etc.).
110
+ * Handles various naming conventions.
111
+ */
112
+ export function resolveMilestoneFile(gsdRoot: string, milestoneId: string, suffix: string): string | null {
113
+ const mDir = resolveMilestoneDir(gsdRoot, milestoneId);
114
+ if (!mDir) return null;
115
+
116
+ const dirName = basename(mDir);
117
+
118
+ // Try: M001-ROADMAP.md, then DIRNAME-ROADMAP.md
119
+ const candidates = [
120
+ join(mDir, `${milestoneId}-${suffix}.md`),
121
+ join(mDir, `${dirName}-${suffix}.md`),
122
+ join(mDir, `${suffix}.md`),
123
+ ];
124
+
125
+ for (const c of candidates) {
126
+ if (existsSync(c)) return c;
127
+ }
128
+ return null;
129
+ }
130
+
131
+ /** Find all slice IDs within a milestone (S01, S02, etc.) */
132
+ export function findSliceIds(gsdRoot: string, milestoneId: string): string[] {
133
+ const mDir = resolveMilestoneDir(gsdRoot, milestoneId);
134
+ if (!mDir) return [];
135
+
136
+ const slicesDir = join(mDir, 'slices');
137
+ if (!existsSync(slicesDir)) return [];
138
+
139
+ const entries = readdirSync(slicesDir, { withFileTypes: true });
140
+ const ids: string[] = [];
141
+
142
+ for (const entry of entries) {
143
+ if (!entry.isDirectory()) continue;
144
+ const match = entry.name.match(/^(S\d+)/);
145
+ if (match) ids.push(match[1]);
146
+ }
147
+
148
+ return ids.sort();
149
+ }
150
+
151
+ /** Resolve the actual directory for a slice */
152
+ export function resolveSliceDir(gsdRoot: string, milestoneId: string, sliceId: string): string | null {
153
+ const mDir = resolveMilestoneDir(gsdRoot, milestoneId);
154
+ if (!mDir) return null;
155
+
156
+ const slicesDir = join(mDir, 'slices');
157
+ if (!existsSync(slicesDir)) return null;
158
+
159
+ const exact = join(slicesDir, sliceId);
160
+ if (existsSync(exact) && statSync(exact).isDirectory()) return exact;
161
+
162
+ const entries = readdirSync(slicesDir, { withFileTypes: true });
163
+ for (const entry of entries) {
164
+ if (entry.isDirectory() && entry.name.startsWith(sliceId)) {
165
+ return join(slicesDir, entry.name);
166
+ }
167
+ }
168
+ return null;
169
+ }
170
+
171
+ /** Resolve a slice-level file (S01-PLAN.md, etc.) */
172
+ export function resolveSliceFile(
173
+ gsdRoot: string, milestoneId: string, sliceId: string, suffix: string,
174
+ ): string | null {
175
+ const sDir = resolveSliceDir(gsdRoot, milestoneId, sliceId);
176
+ if (!sDir) return null;
177
+
178
+ const dirName = basename(sDir);
179
+ const candidates = [
180
+ join(sDir, `${sliceId}-${suffix}.md`),
181
+ join(sDir, `${dirName}-${suffix}.md`),
182
+ join(sDir, `${suffix}.md`),
183
+ ];
184
+
185
+ for (const c of candidates) {
186
+ if (existsSync(c)) return c;
187
+ }
188
+ return null;
189
+ }
190
+
191
+ /** Find all task files in a slice's tasks/ directory */
192
+ export function findTaskFiles(
193
+ gsdRoot: string, milestoneId: string, sliceId: string,
194
+ ): Array<{ id: string; hasPlan: boolean; hasSummary: boolean }> {
195
+ const sDir = resolveSliceDir(gsdRoot, milestoneId, sliceId);
196
+ if (!sDir) return [];
197
+
198
+ const tasksDir = join(sDir, 'tasks');
199
+ if (!existsSync(tasksDir)) return [];
200
+
201
+ const files = readdirSync(tasksDir);
202
+ const taskMap = new Map<string, { hasPlan: boolean; hasSummary: boolean }>();
203
+
204
+ for (const f of files) {
205
+ const match = f.match(/^(T\d+).*-(PLAN|SUMMARY)\.md$/i);
206
+ if (!match) continue;
207
+ const [, id, type] = match;
208
+ const existing = taskMap.get(id) ?? { hasPlan: false, hasSummary: false };
209
+ if (type.toUpperCase() === 'PLAN') existing.hasPlan = true;
210
+ if (type.toUpperCase() === 'SUMMARY') existing.hasSummary = true;
211
+ taskMap.set(id, existing);
212
+ }
213
+
214
+ return Array.from(taskMap.entries())
215
+ .map(([id, info]) => ({ id, ...info }))
216
+ .sort((a, b) => a.id.localeCompare(b.id));
217
+ }