sillyspec 3.26.10 → 3.26.11

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 (97) hide show
  1. package/.claude/skills/sillyspec-archive/SKILL.md +85 -85
  2. package/.claude/skills/sillyspec-brainstorm/SKILL.md +114 -114
  3. package/.claude/skills/sillyspec-commit/SKILL.md +109 -109
  4. package/.claude/skills/sillyspec-continue/SKILL.md +45 -45
  5. package/.claude/skills/sillyspec-doctor/SKILL.md +76 -76
  6. package/.claude/skills/sillyspec-execute/SKILL.md +200 -200
  7. package/.claude/skills/sillyspec-explore/SKILL.md +95 -95
  8. package/.claude/skills/sillyspec-knowledge/SKILL.md +269 -269
  9. package/.claude/skills/sillyspec-plan/SKILL.md +148 -148
  10. package/.claude/skills/sillyspec-quick/SKILL.md +111 -111
  11. package/.claude/skills/sillyspec-resume/SKILL.md +68 -68
  12. package/.claude/skills/sillyspec-scan/SKILL.md +79 -79
  13. package/.claude/skills/sillyspec-state/SKILL.md +64 -64
  14. package/.claude/skills/sillyspec-status/SKILL.md +54 -54
  15. package/.claude/skills/sillyspec-verify/SKILL.md +83 -83
  16. package/.claude/skills/sillyspec-workspace/SKILL.md +169 -169
  17. package/.gitattributes +2 -0
  18. package/.husky/pre-push +20 -20
  19. package/.npmrc-publish +2 -0
  20. package/README.md +201 -201
  21. package/SKILL.md +90 -90
  22. package/package.json +39 -39
  23. package/packages/dashboard/dist/assets/index-Bq_Z2hne.js +7446 -7446
  24. package/packages/dashboard/dist/assets/index-O2W5RV4z.css +1 -1
  25. package/packages/dashboard/dist/index.html +16 -16
  26. package/packages/dashboard/package.json +25 -25
  27. package/packages/dashboard/server/executor.js +86 -86
  28. package/packages/dashboard/server/index.js +601 -601
  29. package/packages/dashboard/server/parser.js +526 -526
  30. package/src/change-risk-profile.js +254 -254
  31. package/src/classify-change.js +109 -109
  32. package/src/config-schema.js +310 -310
  33. package/src/contract-matrix.js +379 -379
  34. package/src/db.js +350 -350
  35. package/src/dispatch/backends/local-agent.js +155 -155
  36. package/src/dispatch/backends/sillyhub-mcp.js +245 -245
  37. package/src/dispatch/probe.js +210 -210
  38. package/src/dispatch/strategy.js +121 -121
  39. package/src/docs-check.js +131 -4
  40. package/src/doctor-diagnostics.js +711 -711
  41. package/src/endpoint-extractor.js +401 -401
  42. package/src/fs-atomic.js +69 -69
  43. package/src/git-helper.js +79 -79
  44. package/src/hooks/claude-pre-tool-use.cjs +125 -125
  45. package/src/hooks/worktree-guard.js +848 -848
  46. package/src/index.js +90 -8
  47. package/src/init.js +623 -623
  48. package/src/knowledge-match.js +130 -130
  49. package/src/local-detect.js +120 -120
  50. package/src/machine-interface.js +543 -543
  51. package/src/migrate.js +127 -127
  52. package/src/progress/change-registry.js +297 -260
  53. package/src/progress/consistency-doctor.js +355 -355
  54. package/src/progress/shared.js +36 -36
  55. package/src/progress/stage-machine.js +29 -1
  56. package/src/progress/step-store.js +261 -261
  57. package/src/progress.js +1131 -1127
  58. package/src/quicklog.js +693 -693
  59. package/src/review-tier.js +77 -77
  60. package/src/run/command.js +1385 -1364
  61. package/src/run/complete-handlers.js +1443 -1399
  62. package/src/run/complete.js +225 -24
  63. package/src/run/gates.js +755 -755
  64. package/src/run/prompt.js +761 -761
  65. package/src/run/quick-audit.js +155 -146
  66. package/src/run/shared.js +1268 -1215
  67. package/src/run/stage.js +487 -479
  68. package/src/scan-postcheck.js +371 -371
  69. package/src/setup.js +416 -416
  70. package/src/sillyhub-mcp/client.js +529 -529
  71. package/src/spec-sync.js +323 -319
  72. package/src/stage-contract.js +897 -897
  73. package/src/stage-review.js +549 -549
  74. package/src/stages/archive.js +155 -155
  75. package/src/stages/brainstorm-auto.js +236 -236
  76. package/src/stages/brainstorm.js +596 -597
  77. package/src/stages/cmd-existence.js +145 -145
  78. package/src/stages/doctor.js +371 -371
  79. package/src/stages/execute.js +1003 -1003
  80. package/src/stages/explore.js +34 -34
  81. package/src/stages/index.js +29 -29
  82. package/src/stages/knowledge.js +498 -498
  83. package/src/stages/plan-postcheck.js +1302 -1302
  84. package/src/stages/plan.js +625 -623
  85. package/src/stages/quick.js +151 -151
  86. package/src/stages/scan.js +607 -607
  87. package/src/stages/status.js +64 -64
  88. package/src/stages/verify.js +264 -264
  89. package/src/sync.js +1223 -1135
  90. package/src/task-review.js +1019 -1019
  91. package/src/verify-postcheck.js +1186 -1186
  92. package/src/workflow.js +600 -600
  93. package/src/worktree-apply.js +1213 -1136
  94. package/src/worktree-deps.js +353 -353
  95. package/src/worktree.js +1481 -1481
  96. package/templates/workflows/archive-impact.yaml +79 -79
  97. package/templates/workflows/scan-docs.yaml +132 -132
@@ -1,526 +1,526 @@
1
- import { readFileSync, existsSync, readdirSync, statSync } from 'fs'
2
- import { execSync } from 'child_process'
3
- import { join, relative, sep } from 'path'
4
- import { fileURLToPath } from 'url'
5
- import { dirname } from 'path'
6
-
7
- const __dirname = dirname(fileURLToPath(import.meta.url))
8
-
9
- /**
10
- * Parse docs tree for a project
11
- * @param {string} projectPath - Path to the project directory
12
- * @returns {object} Docs tree grouped by type
13
- */
14
- // Known framework detection keywords in package.json dependencies
15
- const FRAMEWORK_PATTERNS = [
16
- { keys: ['react', 'react-dom'], name: 'React' },
17
- { keys: ['vue'], name: 'Vue' },
18
- { keys: ['next'], name: 'Next.js' },
19
- { keys: ['nuxt'], name: 'Nuxt' },
20
- { keys: ['express'], name: 'Express' },
21
- { keys: ['koa'], name: 'Koa' },
22
- { keys: ['fastify'], name: 'Fastify' },
23
- { keys: ['nestjs', '@nestjs/core'], name: 'NestJS' },
24
- { keys: ['svelte'], name: 'Svelte' },
25
- { keys: ['astro'], name: 'Astro' },
26
- { keys: ['vite'], name: 'Vite' },
27
- { keys: ['webpack'], name: 'Webpack' },
28
- { keys: ['typescript'], name: 'TypeScript' },
29
- { keys: ['tailwindcss'], name: 'Tailwind' },
30
- { keys: ['prisma'], name: 'Prisma' },
31
- { keys: ['drizzle-orm'], name: 'Drizzle' },
32
- ]
33
-
34
- /**
35
- * Parse project overview info
36
- * @param {string} projectPath
37
- * @returns {object} Overview data
38
- */
39
- export function parseProjectOverview(projectPath) {
40
- const result = {
41
- techStack: [],
42
- lastActive: null,
43
- docStats: { design: 0, plan: 0, archive: 0, changes: 0, scan: 0, quicklog: 0, total: 0 },
44
- git: { branch: '', lastCommit: '', dirtyCount: 0 }
45
- }
46
-
47
- // --- Tech stack ---
48
- const pkgPath = join(projectPath, 'package.json')
49
- if (existsSync(pkgPath)) {
50
- try {
51
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
52
- const deps = Object.keys({ ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) })
53
- for (const pattern of FRAMEWORK_PATTERNS) {
54
- if (pattern.keys.some(k => deps.includes(k))) {
55
- result.techStack.push(pattern.name)
56
- }
57
- }
58
- } catch {}
59
- }
60
- if (existsSync(join(projectPath, 'pom.xml'))) {
61
- result.techStack.push('Java')
62
- try {
63
- const content = readFileSync(join(projectPath, 'pom.xml'), 'utf-8')
64
- if (content.includes('spring-boot')) result.techStack.push('Spring Boot')
65
- } catch {}
66
- }
67
- if (existsSync(join(projectPath, 'build.gradle')) || existsSync(join(projectPath, 'build.gradle.kts'))) {
68
- if (!result.techStack.includes('Java')) result.techStack.push('Gradle')
69
- }
70
- if (existsSync(join(projectPath, 'requirements.txt')) || existsSync(join(projectPath, 'pyproject.toml'))) {
71
- result.techStack.push('Python')
72
- }
73
- if (existsSync(join(projectPath, 'go.mod'))) {
74
- result.techStack.push('Go')
75
- }
76
- if (result.techStack.length === 0) result.techStack = []
77
-
78
- // --- Last active ---
79
- const sillyspecDir = join(projectPath, '.sillyspec')
80
- // Progress is stored in SQLite (.sillyspec/.runtime/sillyspec.db), not progress.json
81
- // Use mtime of the DB file as a fallback for lastActive
82
- const dbPath = join(sillyspecDir, '.runtime', 'sillyspec.db')
83
- if (existsSync(dbPath)) {
84
- try {
85
- const s = statSync(dbPath)
86
- result.lastActive = s.mtime.toISOString()
87
- } catch {}
88
- }
89
- if (!result.lastActive) {
90
- // Fallback: most recently modified file in .sillyspec
91
- try {
92
- const findRecent = (dir) => {
93
- let latest = null
94
- if (!existsSync(dir)) return latest
95
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
96
- const p = join(dir, entry.name)
97
- try {
98
- const s = statSync(p)
99
- if (entry.isDirectory()) {
100
- const sub = findRecent(p)
101
- if (sub && (!latest || sub > latest)) latest = sub
102
- } else if (!latest || s.mtimeMs > latest) {
103
- latest = s.mtimeMs
104
- }
105
- } catch {}
106
- }
107
- return latest
108
- }
109
- const ts = findRecent(sillyspecDir)
110
- if (ts) result.lastActive = new Date(ts).toISOString()
111
- } catch {}
112
- }
113
-
114
- // --- Doc stats ---
115
- const docsDir = join(sillyspecDir, 'docs')
116
- if (existsSync(docsDir)) {
117
- const typeMap = { brainstorm: 'design', plan: 'plan', archive: 'archive', changes: 'changes', scan: 'scan', quicklog: 'quicklog' }
118
- try {
119
- for (const projDir of readdirSync(docsDir, { withFileTypes: true }).filter(d => d.isDirectory())) {
120
- for (const typeDir of readdirSync(join(docsDir, projDir.name), { withFileTypes: true }).filter(d => d.isDirectory())) {
121
- const key = typeMap[typeDir.name]
122
- if (!key) continue
123
- const count = countMdFiles(join(docsDir, projDir.name, typeDir.name))
124
- result.docStats[key] += count
125
- result.docStats.total += count
126
- }
127
- }
128
- } catch {}
129
- }
130
-
131
- // --- Git info ---
132
- try {
133
- result.git.lastCommit = execSync('git log -1 --format=%s', { cwd: projectPath, encoding: 'utf-8' }).trim()
134
- } catch {}
135
- try {
136
- result.git.branch = execSync('git branch --show-current', { cwd: projectPath, encoding: 'utf-8' }).trim()
137
- } catch {}
138
- try {
139
- result.git.dirtyCount = parseInt(execSync('git status --porcelain', { cwd: projectPath, encoding: 'utf-8' }).trim().split('\n').filter(Boolean).length, 10) || 0
140
- } catch {}
141
-
142
- return result
143
- }
144
-
145
- export function parseGitDetail(projectPath) {
146
- const result = { branch: '', commits: [], untracked: [] }
147
- try {
148
- result.branch = execSync('git branch --show-current', { cwd: projectPath, encoding: 'utf-8' }).trim()
149
- } catch {}
150
- try {
151
- const log = execSync('git log -5 --format=%h|%s|%an|%aI', { cwd: projectPath, encoding: 'utf-8' }).trim()
152
- result.commits = log.split('\n').filter(Boolean).map(line => {
153
- const [hash, message, author, date] = line.split('|')
154
- return { hash, message, author, date }
155
- })
156
- } catch {}
157
- try {
158
- const status = execSync('git status --porcelain', { cwd: projectPath, encoding: 'utf-8' }).trim()
159
- result.untracked = status.split('\n').filter(Boolean).map(line => ({
160
- status: line.slice(0, 2).trim(),
161
- file: line.slice(3)
162
- }))
163
- } catch {}
164
- return result
165
- }
166
-
167
- export function parseTechStackDetail(projectPath) {
168
- const result = { frameworks: [], dependencies: {}, devDependencies: {} }
169
- const pkgPath = join(projectPath, 'package.json')
170
- if (existsSync(pkgPath)) {
171
- try {
172
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
173
- result.dependencies = pkg.dependencies || {}
174
- result.devDependencies = pkg.devDependencies || {}
175
- const allDeps = Object.keys({ ...result.dependencies, ...result.devDependencies })
176
- for (const pattern of FRAMEWORK_PATTERNS) {
177
- if (pattern.keys.some(k => allDeps.includes(k))) {
178
- result.frameworks.push(pattern.name)
179
- }
180
- }
181
- } catch {}
182
- }
183
- return result
184
- }
185
-
186
- export function parseDocsList(projectPath) {
187
- const sillyspecDir = join(projectPath, '.sillyspec')
188
- const docsDir = join(sillyspecDir, 'docs')
189
- const groups = []
190
- if (!existsSync(docsDir)) return groups
191
- const typeMap = {
192
- brainstorm: { label: '设计文档', icon: '📋' },
193
- plan: { label: '实现计划', icon: '📐' },
194
- archive: { label: '已归档', icon: '📦' },
195
- changes: { label: '当前变更', icon: '⚙️' },
196
- scan: { label: '架构文档', icon: '🔍' },
197
- quicklog: { label: '快速修复', icon: '⚡' }
198
- }
199
- try {
200
- for (const projDir of readdirSync(docsDir, { withFileTypes: true }).filter(d => d.isDirectory())) {
201
- for (const typeDir of readdirSync(join(docsDir, projDir.name), { withFileTypes: true }).filter(d => d.isDirectory())) {
202
- const cfg = typeMap[typeDir.name]
203
- if (!cfg) continue
204
- const files = listFilesRecursive(join(docsDir, projDir.name, typeDir.name))
205
- if (files.length) {
206
- groups.push({ key: typeDir.name, label: cfg.label, icon: cfg.icon, files })
207
- }
208
- }
209
- }
210
- } catch {}
211
- return groups
212
- }
213
-
214
- function listFilesRecursive(dir) {
215
- const files = []
216
- try {
217
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
218
- const p = join(dir, entry.name)
219
- if (entry.isDirectory()) {
220
- const sub = listFilesRecursive(p)
221
- for (const f of sub) f.name = entry.name + '/' + f.name
222
- files.push(...sub)
223
- } else if (entry.name.endsWith('.md')) {
224
- const s = statSync(p)
225
- files.push({ name: entry.name, path: p, size: s.size, mtime: s.mtime.toISOString() })
226
- }
227
- }
228
- } catch {}
229
- return files
230
- }
231
-
232
- function countMdFiles(dir) {
233
- let count = 0
234
- try {
235
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
236
- if (entry.isDirectory()) {
237
- count += countMdFiles(join(dir, entry.name))
238
- } else if (entry.name.endsWith('.md')) {
239
- count++
240
- }
241
- }
242
- } catch {}
243
- return count
244
- }
245
-
246
- export function parseDocsTree(projectPath) {
247
- const sillyspecDir = join(projectPath, '.sillyspec')
248
- const docsDir = join(sillyspecDir, 'docs')
249
-
250
- if (!existsSync(docsDir)) {
251
- return { groups: [] }
252
- }
253
-
254
- const groupConfig = [
255
- { key: 'brainstorm', label: '📋 设计文档', icon: '📋', dir: 'brainstorm' },
256
- { key: 'plan', label: '📐 实现计划', icon: '📐', dir: 'plan' },
257
- { key: 'changes', label: '⚙️ 当前变更', icon: '⚙️', dir: 'changes' },
258
- { key: 'archive', label: '📦 已归档', icon: '📦', dir: 'archive' },
259
- { key: 'scan', label: '🔍 架构文档', icon: '🔍', dir: 'scan' },
260
- { key: 'quicklog', label: '⚡ 快速修复', icon: '⚡', dir: 'quicklog' },
261
- ]
262
-
263
- const groups = []
264
-
265
- // Find project dirs under docs/
266
- const projectDirs = existsSync(docsDir) ? readdirSync(docsDir, { withFileTypes: true })
267
- .filter(d => d.isDirectory()).map(d => d.name) : []
268
-
269
- for (const projName of projectDirs) {
270
- const projDocsDir = join(docsDir, projName)
271
-
272
- for (const group of groupConfig) {
273
- const groupDir = join(projDocsDir, group.dir)
274
- if (!existsSync(groupDir)) continue
275
-
276
- const files = []
277
- try {
278
- const entries = readdirSync(groupDir, { withFileTypes: true })
279
- for (const entry of entries) {
280
- if (entry.isDirectory()) {
281
- // For changes/ and archive/ subdirs
282
- const subDir = join(groupDir, entry.name)
283
- try {
284
- const subFiles = readdirSync(subDir).filter(f => f.endsWith('.md'))
285
- for (const sf of subFiles) {
286
- const filePath = join(subDir, sf)
287
- files.push({
288
- name: `${entry.name}/${sf}`,
289
- path: filePath,
290
- title: sf.replace('.md', '')
291
- })
292
- }
293
- } catch {}
294
- } else if (entry.name.endsWith('.md')) {
295
- const filePath = join(groupDir, entry.name)
296
- let title = entry.name.replace('.md', '')
297
- try {
298
- const content = readFileSync(filePath, 'utf-8')
299
- const titleMatch = content.match(/^#\s+(.+)$/m)
300
- if (titleMatch) title = titleMatch[1]
301
- } catch {}
302
- files.push({ name: entry.name, path: filePath, title })
303
- }
304
- }
305
- } catch {}
306
-
307
- if (files.length > 0) {
308
- groups.push({
309
- key: `${projName}::${group.key}`,
310
- label: group.label,
311
- project: projName,
312
- files
313
- })
314
- }
315
- }
316
- }
317
-
318
- return { groups }
319
- }
320
-
321
- const VIEWABLE_SILLYSPEC_DOC_EXTENSIONS = new Set([
322
- '.md', '.markdown', '.mdx',
323
- '.html', '.htm',
324
- '.txt', '.log',
325
- '.json', '.yaml', '.yml', '.toml',
326
- '.xml', '.csv'
327
- ])
328
-
329
- const SILLYSPEC_DOC_GROUPS = [
330
- { key: 'docs', label: '📚 docs', dir: 'docs' },
331
- { key: 'changes', label: '⚙️ changes', dir: 'changes' },
332
- { key: 'plans', label: '🧾 plans', dir: 'plans' },
333
- { key: 'quicklog', label: '⚡ quicklog', dir: 'quicklog' },
334
- { key: 'knowledge', label: '🧠 knowledge', dir: 'knowledge' },
335
- { key: 'projects', label: '📁 projects', dir: 'projects' },
336
- { key: 'workspace', label: '🗂️ workspace', dir: 'workspace' },
337
- { key: 'shared', label: '🔗 shared', dir: 'shared' },
338
- { key: 'runtime', label: '🧰 .runtime', dir: '.runtime' }
339
- ]
340
-
341
- function sillyspecDocExt(fileName) {
342
- const index = fileName.lastIndexOf('.')
343
- return index === -1 ? '' : fileName.slice(index).toLowerCase()
344
- }
345
-
346
- function isViewableSillyspecDoc(fileName) {
347
- return VIEWABLE_SILLYSPEC_DOC_EXTENSIONS.has(sillyspecDocExt(fileName))
348
- }
349
-
350
- function titleFromSillyspecDoc(filePath, fileName) {
351
- const ext = sillyspecDocExt(fileName)
352
- if (ext === '.md' || ext === '.markdown' || ext === '.mdx') {
353
- try {
354
- const content = readFileSync(filePath, 'utf-8')
355
- const titleMatch = content.match(/^#\s+(.+)$/m)
356
- if (titleMatch) return titleMatch[1]
357
- } catch {}
358
- }
359
- return fileName.replace(/\.(md|markdown|mdx|html?|txt|log|json|ya?ml|toml|xml|csv)$/i, '')
360
- }
361
-
362
- function listSillyspecDocsRecursive(dir, rootDir) {
363
- const files = []
364
- try {
365
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
366
- const filePath = join(dir, entry.name)
367
- if (entry.isDirectory()) {
368
- files.push(...listSillyspecDocsRecursive(filePath, rootDir))
369
- } else if (entry.isFile() && isViewableSillyspecDoc(entry.name)) {
370
- const s = statSync(filePath)
371
- files.push({
372
- name: relative(rootDir, filePath).split(sep).join('/'),
373
- path: filePath,
374
- title: titleFromSillyspecDoc(filePath, entry.name),
375
- extension: sillyspecDocExt(entry.name).slice(1),
376
- size: s.size,
377
- mtime: s.mtime.toISOString()
378
- })
379
- }
380
- }
381
- } catch {}
382
- return files
383
- }
384
-
385
- export function parseSillyspecDocsTree(projectPath) {
386
- const sillyspecDir = join(projectPath, '.sillyspec')
387
- if (!existsSync(sillyspecDir)) return { groups: [] }
388
-
389
- const groups = []
390
- const seen = new Set()
391
-
392
- for (const group of SILLYSPEC_DOC_GROUPS) {
393
- const groupDir = join(sillyspecDir, group.dir)
394
- if (!existsSync(groupDir)) continue
395
-
396
- const files = listSillyspecDocsRecursive(groupDir, groupDir)
397
- .sort((a, b) => a.name.localeCompare(b.name))
398
-
399
- if (files.length === 0) continue
400
- for (const file of files) seen.add(file.path)
401
- groups.push({ key: group.key, label: group.label, project: '.sillyspec', files })
402
- }
403
-
404
- const rootFiles = []
405
- try {
406
- for (const entry of readdirSync(sillyspecDir, { withFileTypes: true })) {
407
- if (!entry.isFile() || !isViewableSillyspecDoc(entry.name)) continue
408
- const filePath = join(sillyspecDir, entry.name)
409
- if (seen.has(filePath)) continue
410
- const s = statSync(filePath)
411
- rootFiles.push({
412
- name: entry.name,
413
- path: filePath,
414
- title: titleFromSillyspecDoc(filePath, entry.name),
415
- extension: sillyspecDocExt(entry.name).slice(1),
416
- size: s.size,
417
- mtime: s.mtime.toISOString()
418
- })
419
- }
420
- } catch {}
421
-
422
- if (rootFiles.length > 0) {
423
- groups.unshift({ key: 'root', label: '📄 .sillyspec', project: '.sillyspec', files: rootFiles })
424
- }
425
-
426
- return { groups }
427
- }
428
-
429
- /**
430
- * Parse project state from .sillyspec SQLite database via CLI.
431
- * Progress data is stored in SQLite (.sillyspec/.runtime/sillyspec.db),
432
- * accessed through `sillyspec progress show`.
433
- * @param {string} projectPath - Path to the project directory
434
- * @returns {object|null} Project state with currentStage, stages, lastActive
435
- */
436
- export function parseProjectState(projectPath) {
437
- const sillyspecDir = join(projectPath, '.sillyspec')
438
- const dbPath = join(sillyspecDir, '.runtime', 'sillyspec.db')
439
-
440
- if (!existsSync(sillyspecDir) || !existsSync(dbPath)) {
441
- return null
442
- }
443
-
444
- let currentStage = ''
445
- let lastActive = null
446
-
447
- // Use DB file mtime as lastActive indicator
448
- try {
449
- const s = statSync(dbPath)
450
- lastActive = s.mtime.toISOString()
451
- } catch {}
452
-
453
- // Use CLI to read current stage from SQLite
454
- try {
455
- const output = execSync('sillyspec progress show 2>/dev/null', {
456
- cwd: projectPath, encoding: 'utf-8', timeout: 5000
457
- })
458
- const stageMatch = output.match(/当前阶段:\s*(\S+)/)
459
- if (stageMatch) currentStage = stageMatch[1]
460
- } catch {
461
- // CLI unavailable or no active change
462
- }
463
-
464
- return {
465
- currentStage,
466
- nextStep: null,
467
- progress: { stages: {} },
468
- stages: [],
469
- specs: [],
470
- lastActive
471
- }
472
- }
473
-
474
- /**
475
- * Parse spec file content
476
- * @param {string} specPath - Path to the spec file
477
- * @returns {object} Parsed spec with title, sections, and metadata
478
- */
479
- export function parseSpecFile(specPath) {
480
- if (!existsSync(specPath)) {
481
- return null
482
- }
483
-
484
- try {
485
- const content = readFileSync(specPath, 'utf-8')
486
- const lines = content.split('\n')
487
-
488
- const titleMatch = content.match(/^#\s+(.+)$/m)
489
- const title = titleMatch ? titleMatch[1] : 'Unknown'
490
-
491
- const sections = []
492
- let currentSection = null
493
- let currentContent = []
494
-
495
- for (let i = 0; i < lines.length; i++) {
496
- const line = lines[i]
497
- const headingMatch = line.match(/^(#{2,4})\s+(.+)$/)
498
-
499
- if (headingMatch) {
500
- if (currentSection) {
501
- sections.push({
502
- level: currentSection.level,
503
- title: currentSection.title,
504
- content: currentContent.join('\n').trim()
505
- })
506
- }
507
- currentSection = { level: headingMatch[1].length, title: headingMatch[2] }
508
- currentContent = []
509
- } else if (currentSection) {
510
- currentContent.push(line)
511
- }
512
- }
513
-
514
- if (currentSection) {
515
- sections.push({
516
- level: currentSection.level,
517
- title: currentSection.title,
518
- content: currentContent.join('\n').trim()
519
- })
520
- }
521
-
522
- return { title, sections, content }
523
- } catch (err) {
524
- return null
525
- }
526
- }
1
+ import { readFileSync, existsSync, readdirSync, statSync } from 'fs'
2
+ import { execSync } from 'child_process'
3
+ import { join, relative, sep } from 'path'
4
+ import { fileURLToPath } from 'url'
5
+ import { dirname } from 'path'
6
+
7
+ const __dirname = dirname(fileURLToPath(import.meta.url))
8
+
9
+ /**
10
+ * Parse docs tree for a project
11
+ * @param {string} projectPath - Path to the project directory
12
+ * @returns {object} Docs tree grouped by type
13
+ */
14
+ // Known framework detection keywords in package.json dependencies
15
+ const FRAMEWORK_PATTERNS = [
16
+ { keys: ['react', 'react-dom'], name: 'React' },
17
+ { keys: ['vue'], name: 'Vue' },
18
+ { keys: ['next'], name: 'Next.js' },
19
+ { keys: ['nuxt'], name: 'Nuxt' },
20
+ { keys: ['express'], name: 'Express' },
21
+ { keys: ['koa'], name: 'Koa' },
22
+ { keys: ['fastify'], name: 'Fastify' },
23
+ { keys: ['nestjs', '@nestjs/core'], name: 'NestJS' },
24
+ { keys: ['svelte'], name: 'Svelte' },
25
+ { keys: ['astro'], name: 'Astro' },
26
+ { keys: ['vite'], name: 'Vite' },
27
+ { keys: ['webpack'], name: 'Webpack' },
28
+ { keys: ['typescript'], name: 'TypeScript' },
29
+ { keys: ['tailwindcss'], name: 'Tailwind' },
30
+ { keys: ['prisma'], name: 'Prisma' },
31
+ { keys: ['drizzle-orm'], name: 'Drizzle' },
32
+ ]
33
+
34
+ /**
35
+ * Parse project overview info
36
+ * @param {string} projectPath
37
+ * @returns {object} Overview data
38
+ */
39
+ export function parseProjectOverview(projectPath) {
40
+ const result = {
41
+ techStack: [],
42
+ lastActive: null,
43
+ docStats: { design: 0, plan: 0, archive: 0, changes: 0, scan: 0, quicklog: 0, total: 0 },
44
+ git: { branch: '', lastCommit: '', dirtyCount: 0 }
45
+ }
46
+
47
+ // --- Tech stack ---
48
+ const pkgPath = join(projectPath, 'package.json')
49
+ if (existsSync(pkgPath)) {
50
+ try {
51
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
52
+ const deps = Object.keys({ ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) })
53
+ for (const pattern of FRAMEWORK_PATTERNS) {
54
+ if (pattern.keys.some(k => deps.includes(k))) {
55
+ result.techStack.push(pattern.name)
56
+ }
57
+ }
58
+ } catch {}
59
+ }
60
+ if (existsSync(join(projectPath, 'pom.xml'))) {
61
+ result.techStack.push('Java')
62
+ try {
63
+ const content = readFileSync(join(projectPath, 'pom.xml'), 'utf-8')
64
+ if (content.includes('spring-boot')) result.techStack.push('Spring Boot')
65
+ } catch {}
66
+ }
67
+ if (existsSync(join(projectPath, 'build.gradle')) || existsSync(join(projectPath, 'build.gradle.kts'))) {
68
+ if (!result.techStack.includes('Java')) result.techStack.push('Gradle')
69
+ }
70
+ if (existsSync(join(projectPath, 'requirements.txt')) || existsSync(join(projectPath, 'pyproject.toml'))) {
71
+ result.techStack.push('Python')
72
+ }
73
+ if (existsSync(join(projectPath, 'go.mod'))) {
74
+ result.techStack.push('Go')
75
+ }
76
+ if (result.techStack.length === 0) result.techStack = []
77
+
78
+ // --- Last active ---
79
+ const sillyspecDir = join(projectPath, '.sillyspec')
80
+ // Progress is stored in SQLite (.sillyspec/.runtime/sillyspec.db), not progress.json
81
+ // Use mtime of the DB file as a fallback for lastActive
82
+ const dbPath = join(sillyspecDir, '.runtime', 'sillyspec.db')
83
+ if (existsSync(dbPath)) {
84
+ try {
85
+ const s = statSync(dbPath)
86
+ result.lastActive = s.mtime.toISOString()
87
+ } catch {}
88
+ }
89
+ if (!result.lastActive) {
90
+ // Fallback: most recently modified file in .sillyspec
91
+ try {
92
+ const findRecent = (dir) => {
93
+ let latest = null
94
+ if (!existsSync(dir)) return latest
95
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
96
+ const p = join(dir, entry.name)
97
+ try {
98
+ const s = statSync(p)
99
+ if (entry.isDirectory()) {
100
+ const sub = findRecent(p)
101
+ if (sub && (!latest || sub > latest)) latest = sub
102
+ } else if (!latest || s.mtimeMs > latest) {
103
+ latest = s.mtimeMs
104
+ }
105
+ } catch {}
106
+ }
107
+ return latest
108
+ }
109
+ const ts = findRecent(sillyspecDir)
110
+ if (ts) result.lastActive = new Date(ts).toISOString()
111
+ } catch {}
112
+ }
113
+
114
+ // --- Doc stats ---
115
+ const docsDir = join(sillyspecDir, 'docs')
116
+ if (existsSync(docsDir)) {
117
+ const typeMap = { brainstorm: 'design', plan: 'plan', archive: 'archive', changes: 'changes', scan: 'scan', quicklog: 'quicklog' }
118
+ try {
119
+ for (const projDir of readdirSync(docsDir, { withFileTypes: true }).filter(d => d.isDirectory())) {
120
+ for (const typeDir of readdirSync(join(docsDir, projDir.name), { withFileTypes: true }).filter(d => d.isDirectory())) {
121
+ const key = typeMap[typeDir.name]
122
+ if (!key) continue
123
+ const count = countMdFiles(join(docsDir, projDir.name, typeDir.name))
124
+ result.docStats[key] += count
125
+ result.docStats.total += count
126
+ }
127
+ }
128
+ } catch {}
129
+ }
130
+
131
+ // --- Git info ---
132
+ try {
133
+ result.git.lastCommit = execSync('git log -1 --format=%s', { cwd: projectPath, encoding: 'utf-8' }).trim()
134
+ } catch {}
135
+ try {
136
+ result.git.branch = execSync('git branch --show-current', { cwd: projectPath, encoding: 'utf-8' }).trim()
137
+ } catch {}
138
+ try {
139
+ result.git.dirtyCount = parseInt(execSync('git status --porcelain', { cwd: projectPath, encoding: 'utf-8' }).trim().split('\n').filter(Boolean).length, 10) || 0
140
+ } catch {}
141
+
142
+ return result
143
+ }
144
+
145
+ export function parseGitDetail(projectPath) {
146
+ const result = { branch: '', commits: [], untracked: [] }
147
+ try {
148
+ result.branch = execSync('git branch --show-current', { cwd: projectPath, encoding: 'utf-8' }).trim()
149
+ } catch {}
150
+ try {
151
+ const log = execSync('git log -5 --format=%h|%s|%an|%aI', { cwd: projectPath, encoding: 'utf-8' }).trim()
152
+ result.commits = log.split('\n').filter(Boolean).map(line => {
153
+ const [hash, message, author, date] = line.split('|')
154
+ return { hash, message, author, date }
155
+ })
156
+ } catch {}
157
+ try {
158
+ const status = execSync('git status --porcelain', { cwd: projectPath, encoding: 'utf-8' }).trim()
159
+ result.untracked = status.split('\n').filter(Boolean).map(line => ({
160
+ status: line.slice(0, 2).trim(),
161
+ file: line.slice(3)
162
+ }))
163
+ } catch {}
164
+ return result
165
+ }
166
+
167
+ export function parseTechStackDetail(projectPath) {
168
+ const result = { frameworks: [], dependencies: {}, devDependencies: {} }
169
+ const pkgPath = join(projectPath, 'package.json')
170
+ if (existsSync(pkgPath)) {
171
+ try {
172
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
173
+ result.dependencies = pkg.dependencies || {}
174
+ result.devDependencies = pkg.devDependencies || {}
175
+ const allDeps = Object.keys({ ...result.dependencies, ...result.devDependencies })
176
+ for (const pattern of FRAMEWORK_PATTERNS) {
177
+ if (pattern.keys.some(k => allDeps.includes(k))) {
178
+ result.frameworks.push(pattern.name)
179
+ }
180
+ }
181
+ } catch {}
182
+ }
183
+ return result
184
+ }
185
+
186
+ export function parseDocsList(projectPath) {
187
+ const sillyspecDir = join(projectPath, '.sillyspec')
188
+ const docsDir = join(sillyspecDir, 'docs')
189
+ const groups = []
190
+ if (!existsSync(docsDir)) return groups
191
+ const typeMap = {
192
+ brainstorm: { label: '设计文档', icon: '📋' },
193
+ plan: { label: '实现计划', icon: '📐' },
194
+ archive: { label: '已归档', icon: '📦' },
195
+ changes: { label: '当前变更', icon: '⚙️' },
196
+ scan: { label: '架构文档', icon: '🔍' },
197
+ quicklog: { label: '快速修复', icon: '⚡' }
198
+ }
199
+ try {
200
+ for (const projDir of readdirSync(docsDir, { withFileTypes: true }).filter(d => d.isDirectory())) {
201
+ for (const typeDir of readdirSync(join(docsDir, projDir.name), { withFileTypes: true }).filter(d => d.isDirectory())) {
202
+ const cfg = typeMap[typeDir.name]
203
+ if (!cfg) continue
204
+ const files = listFilesRecursive(join(docsDir, projDir.name, typeDir.name))
205
+ if (files.length) {
206
+ groups.push({ key: typeDir.name, label: cfg.label, icon: cfg.icon, files })
207
+ }
208
+ }
209
+ }
210
+ } catch {}
211
+ return groups
212
+ }
213
+
214
+ function listFilesRecursive(dir) {
215
+ const files = []
216
+ try {
217
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
218
+ const p = join(dir, entry.name)
219
+ if (entry.isDirectory()) {
220
+ const sub = listFilesRecursive(p)
221
+ for (const f of sub) f.name = entry.name + '/' + f.name
222
+ files.push(...sub)
223
+ } else if (entry.name.endsWith('.md')) {
224
+ const s = statSync(p)
225
+ files.push({ name: entry.name, path: p, size: s.size, mtime: s.mtime.toISOString() })
226
+ }
227
+ }
228
+ } catch {}
229
+ return files
230
+ }
231
+
232
+ function countMdFiles(dir) {
233
+ let count = 0
234
+ try {
235
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
236
+ if (entry.isDirectory()) {
237
+ count += countMdFiles(join(dir, entry.name))
238
+ } else if (entry.name.endsWith('.md')) {
239
+ count++
240
+ }
241
+ }
242
+ } catch {}
243
+ return count
244
+ }
245
+
246
+ export function parseDocsTree(projectPath) {
247
+ const sillyspecDir = join(projectPath, '.sillyspec')
248
+ const docsDir = join(sillyspecDir, 'docs')
249
+
250
+ if (!existsSync(docsDir)) {
251
+ return { groups: [] }
252
+ }
253
+
254
+ const groupConfig = [
255
+ { key: 'brainstorm', label: '📋 设计文档', icon: '📋', dir: 'brainstorm' },
256
+ { key: 'plan', label: '📐 实现计划', icon: '📐', dir: 'plan' },
257
+ { key: 'changes', label: '⚙️ 当前变更', icon: '⚙️', dir: 'changes' },
258
+ { key: 'archive', label: '📦 已归档', icon: '📦', dir: 'archive' },
259
+ { key: 'scan', label: '🔍 架构文档', icon: '🔍', dir: 'scan' },
260
+ { key: 'quicklog', label: '⚡ 快速修复', icon: '⚡', dir: 'quicklog' },
261
+ ]
262
+
263
+ const groups = []
264
+
265
+ // Find project dirs under docs/
266
+ const projectDirs = existsSync(docsDir) ? readdirSync(docsDir, { withFileTypes: true })
267
+ .filter(d => d.isDirectory()).map(d => d.name) : []
268
+
269
+ for (const projName of projectDirs) {
270
+ const projDocsDir = join(docsDir, projName)
271
+
272
+ for (const group of groupConfig) {
273
+ const groupDir = join(projDocsDir, group.dir)
274
+ if (!existsSync(groupDir)) continue
275
+
276
+ const files = []
277
+ try {
278
+ const entries = readdirSync(groupDir, { withFileTypes: true })
279
+ for (const entry of entries) {
280
+ if (entry.isDirectory()) {
281
+ // For changes/ and archive/ subdirs
282
+ const subDir = join(groupDir, entry.name)
283
+ try {
284
+ const subFiles = readdirSync(subDir).filter(f => f.endsWith('.md'))
285
+ for (const sf of subFiles) {
286
+ const filePath = join(subDir, sf)
287
+ files.push({
288
+ name: `${entry.name}/${sf}`,
289
+ path: filePath,
290
+ title: sf.replace('.md', '')
291
+ })
292
+ }
293
+ } catch {}
294
+ } else if (entry.name.endsWith('.md')) {
295
+ const filePath = join(groupDir, entry.name)
296
+ let title = entry.name.replace('.md', '')
297
+ try {
298
+ const content = readFileSync(filePath, 'utf-8')
299
+ const titleMatch = content.match(/^#\s+(.+)$/m)
300
+ if (titleMatch) title = titleMatch[1]
301
+ } catch {}
302
+ files.push({ name: entry.name, path: filePath, title })
303
+ }
304
+ }
305
+ } catch {}
306
+
307
+ if (files.length > 0) {
308
+ groups.push({
309
+ key: `${projName}::${group.key}`,
310
+ label: group.label,
311
+ project: projName,
312
+ files
313
+ })
314
+ }
315
+ }
316
+ }
317
+
318
+ return { groups }
319
+ }
320
+
321
+ const VIEWABLE_SILLYSPEC_DOC_EXTENSIONS = new Set([
322
+ '.md', '.markdown', '.mdx',
323
+ '.html', '.htm',
324
+ '.txt', '.log',
325
+ '.json', '.yaml', '.yml', '.toml',
326
+ '.xml', '.csv'
327
+ ])
328
+
329
+ const SILLYSPEC_DOC_GROUPS = [
330
+ { key: 'docs', label: '📚 docs', dir: 'docs' },
331
+ { key: 'changes', label: '⚙️ changes', dir: 'changes' },
332
+ { key: 'plans', label: '🧾 plans', dir: 'plans' },
333
+ { key: 'quicklog', label: '⚡ quicklog', dir: 'quicklog' },
334
+ { key: 'knowledge', label: '🧠 knowledge', dir: 'knowledge' },
335
+ { key: 'projects', label: '📁 projects', dir: 'projects' },
336
+ { key: 'workspace', label: '🗂️ workspace', dir: 'workspace' },
337
+ { key: 'shared', label: '🔗 shared', dir: 'shared' },
338
+ { key: 'runtime', label: '🧰 .runtime', dir: '.runtime' }
339
+ ]
340
+
341
+ function sillyspecDocExt(fileName) {
342
+ const index = fileName.lastIndexOf('.')
343
+ return index === -1 ? '' : fileName.slice(index).toLowerCase()
344
+ }
345
+
346
+ function isViewableSillyspecDoc(fileName) {
347
+ return VIEWABLE_SILLYSPEC_DOC_EXTENSIONS.has(sillyspecDocExt(fileName))
348
+ }
349
+
350
+ function titleFromSillyspecDoc(filePath, fileName) {
351
+ const ext = sillyspecDocExt(fileName)
352
+ if (ext === '.md' || ext === '.markdown' || ext === '.mdx') {
353
+ try {
354
+ const content = readFileSync(filePath, 'utf-8')
355
+ const titleMatch = content.match(/^#\s+(.+)$/m)
356
+ if (titleMatch) return titleMatch[1]
357
+ } catch {}
358
+ }
359
+ return fileName.replace(/\.(md|markdown|mdx|html?|txt|log|json|ya?ml|toml|xml|csv)$/i, '')
360
+ }
361
+
362
+ function listSillyspecDocsRecursive(dir, rootDir) {
363
+ const files = []
364
+ try {
365
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
366
+ const filePath = join(dir, entry.name)
367
+ if (entry.isDirectory()) {
368
+ files.push(...listSillyspecDocsRecursive(filePath, rootDir))
369
+ } else if (entry.isFile() && isViewableSillyspecDoc(entry.name)) {
370
+ const s = statSync(filePath)
371
+ files.push({
372
+ name: relative(rootDir, filePath).split(sep).join('/'),
373
+ path: filePath,
374
+ title: titleFromSillyspecDoc(filePath, entry.name),
375
+ extension: sillyspecDocExt(entry.name).slice(1),
376
+ size: s.size,
377
+ mtime: s.mtime.toISOString()
378
+ })
379
+ }
380
+ }
381
+ } catch {}
382
+ return files
383
+ }
384
+
385
+ export function parseSillyspecDocsTree(projectPath) {
386
+ const sillyspecDir = join(projectPath, '.sillyspec')
387
+ if (!existsSync(sillyspecDir)) return { groups: [] }
388
+
389
+ const groups = []
390
+ const seen = new Set()
391
+
392
+ for (const group of SILLYSPEC_DOC_GROUPS) {
393
+ const groupDir = join(sillyspecDir, group.dir)
394
+ if (!existsSync(groupDir)) continue
395
+
396
+ const files = listSillyspecDocsRecursive(groupDir, groupDir)
397
+ .sort((a, b) => a.name.localeCompare(b.name))
398
+
399
+ if (files.length === 0) continue
400
+ for (const file of files) seen.add(file.path)
401
+ groups.push({ key: group.key, label: group.label, project: '.sillyspec', files })
402
+ }
403
+
404
+ const rootFiles = []
405
+ try {
406
+ for (const entry of readdirSync(sillyspecDir, { withFileTypes: true })) {
407
+ if (!entry.isFile() || !isViewableSillyspecDoc(entry.name)) continue
408
+ const filePath = join(sillyspecDir, entry.name)
409
+ if (seen.has(filePath)) continue
410
+ const s = statSync(filePath)
411
+ rootFiles.push({
412
+ name: entry.name,
413
+ path: filePath,
414
+ title: titleFromSillyspecDoc(filePath, entry.name),
415
+ extension: sillyspecDocExt(entry.name).slice(1),
416
+ size: s.size,
417
+ mtime: s.mtime.toISOString()
418
+ })
419
+ }
420
+ } catch {}
421
+
422
+ if (rootFiles.length > 0) {
423
+ groups.unshift({ key: 'root', label: '📄 .sillyspec', project: '.sillyspec', files: rootFiles })
424
+ }
425
+
426
+ return { groups }
427
+ }
428
+
429
+ /**
430
+ * Parse project state from .sillyspec SQLite database via CLI.
431
+ * Progress data is stored in SQLite (.sillyspec/.runtime/sillyspec.db),
432
+ * accessed through `sillyspec progress show`.
433
+ * @param {string} projectPath - Path to the project directory
434
+ * @returns {object|null} Project state with currentStage, stages, lastActive
435
+ */
436
+ export function parseProjectState(projectPath) {
437
+ const sillyspecDir = join(projectPath, '.sillyspec')
438
+ const dbPath = join(sillyspecDir, '.runtime', 'sillyspec.db')
439
+
440
+ if (!existsSync(sillyspecDir) || !existsSync(dbPath)) {
441
+ return null
442
+ }
443
+
444
+ let currentStage = ''
445
+ let lastActive = null
446
+
447
+ // Use DB file mtime as lastActive indicator
448
+ try {
449
+ const s = statSync(dbPath)
450
+ lastActive = s.mtime.toISOString()
451
+ } catch {}
452
+
453
+ // Use CLI to read current stage from SQLite
454
+ try {
455
+ const output = execSync('sillyspec progress show 2>/dev/null', {
456
+ cwd: projectPath, encoding: 'utf-8', timeout: 5000
457
+ })
458
+ const stageMatch = output.match(/当前阶段:\s*(\S+)/)
459
+ if (stageMatch) currentStage = stageMatch[1]
460
+ } catch {
461
+ // CLI unavailable or no active change
462
+ }
463
+
464
+ return {
465
+ currentStage,
466
+ nextStep: null,
467
+ progress: { stages: {} },
468
+ stages: [],
469
+ specs: [],
470
+ lastActive
471
+ }
472
+ }
473
+
474
+ /**
475
+ * Parse spec file content
476
+ * @param {string} specPath - Path to the spec file
477
+ * @returns {object} Parsed spec with title, sections, and metadata
478
+ */
479
+ export function parseSpecFile(specPath) {
480
+ if (!existsSync(specPath)) {
481
+ return null
482
+ }
483
+
484
+ try {
485
+ const content = readFileSync(specPath, 'utf-8')
486
+ const lines = content.split('\n')
487
+
488
+ const titleMatch = content.match(/^#\s+(.+)$/m)
489
+ const title = titleMatch ? titleMatch[1] : 'Unknown'
490
+
491
+ const sections = []
492
+ let currentSection = null
493
+ let currentContent = []
494
+
495
+ for (let i = 0; i < lines.length; i++) {
496
+ const line = lines[i]
497
+ const headingMatch = line.match(/^(#{2,4})\s+(.+)$/)
498
+
499
+ if (headingMatch) {
500
+ if (currentSection) {
501
+ sections.push({
502
+ level: currentSection.level,
503
+ title: currentSection.title,
504
+ content: currentContent.join('\n').trim()
505
+ })
506
+ }
507
+ currentSection = { level: headingMatch[1].length, title: headingMatch[2] }
508
+ currentContent = []
509
+ } else if (currentSection) {
510
+ currentContent.push(line)
511
+ }
512
+ }
513
+
514
+ if (currentSection) {
515
+ sections.push({
516
+ level: currentSection.level,
517
+ title: currentSection.title,
518
+ content: currentContent.join('\n').trim()
519
+ })
520
+ }
521
+
522
+ return { title, sections, content }
523
+ } catch (err) {
524
+ return null
525
+ }
526
+ }