sillyspec 3.20.2 → 3.20.4

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 (133) hide show
  1. package/.claude/skills/sillyspec-archive/SKILL.md +21 -21
  2. package/.claude/skills/sillyspec-auto/SKILL.md +83 -83
  3. package/.claude/skills/sillyspec-brainstorm/SKILL.md +44 -44
  4. package/.claude/skills/sillyspec-commit/SKILL.md +106 -106
  5. package/.claude/skills/sillyspec-continue/SKILL.md +45 -45
  6. package/.claude/skills/sillyspec-doctor/SKILL.md +31 -31
  7. package/.claude/skills/sillyspec-execute/SKILL.md +30 -30
  8. package/.claude/skills/sillyspec-explore/SKILL.md +109 -109
  9. package/.claude/skills/sillyspec-knowledge/SKILL.md +269 -269
  10. package/.claude/skills/sillyspec-plan/SKILL.md +21 -21
  11. package/.claude/skills/sillyspec-propose/SKILL.md +21 -21
  12. package/.claude/skills/sillyspec-quick/SKILL.md +21 -21
  13. package/.claude/skills/sillyspec-resume/SKILL.md +68 -68
  14. package/.claude/skills/sillyspec-scan/SKILL.md +21 -21
  15. package/.claude/skills/sillyspec-state/SKILL.md +54 -54
  16. package/.claude/skills/sillyspec-status/SKILL.md +21 -21
  17. package/.claude/skills/sillyspec-verify/SKILL.md +21 -21
  18. package/.claude/skills/sillyspec-workspace/SKILL.md +157 -157
  19. package/.husky/pre-push +13 -13
  20. package/CLAUDE.md +18 -18
  21. package/README.md +198 -188
  22. package/SKILL.md +90 -91
  23. package/bin/sillyspec.js +2 -2
  24. package/docs/brainstorm-plan-contract.md +64 -64
  25. package/docs/plan-execute-contract.md +123 -123
  26. package/docs/platform-scan-protocol.md +298 -298
  27. package/docs/revision-mode.md +115 -115
  28. package/docs/sillyspec/file-lifecycle/known-implementation-gaps.md +99 -99
  29. package/docs/sillyspec/file-lifecycle/platform-workflows-sync.md +218 -218
  30. package/docs/sillyspec/file-lifecycle/stage-artifacts.md +167 -167
  31. package/docs/sillyspec/file-lifecycle/storage-and-state.md +148 -148
  32. package/docs/sillyspec/file-lifecycle/worktree-and-guard.md +211 -193
  33. package/docs/sillyspec/file-lifecycle.md +125 -125
  34. package/docs/workflow-contract-regression.md +106 -106
  35. package/docs/worktree-isolation.md +252 -252
  36. package/package.json +40 -40
  37. package/packages/dashboard/dist/assets/index-Bq_Z2hne.js +7446 -7446
  38. package/packages/dashboard/dist/assets/index-O2W5RV4z.css +1 -1
  39. package/packages/dashboard/dist/index.html +16 -16
  40. package/packages/dashboard/index.html +15 -15
  41. package/packages/dashboard/package-lock.json +2384 -2384
  42. package/packages/dashboard/package.json +25 -25
  43. package/packages/dashboard/server/executor.js +86 -86
  44. package/packages/dashboard/server/index.js +588 -588
  45. package/packages/dashboard/server/parser.js +526 -526
  46. package/packages/dashboard/server/watcher.js +344 -344
  47. package/packages/dashboard/src/App.vue +558 -558
  48. package/packages/dashboard/src/components/ActionBar.vue +93 -93
  49. package/packages/dashboard/src/components/CommandPalette.vue +96 -96
  50. package/packages/dashboard/src/components/DetailPanel.vue +137 -137
  51. package/packages/dashboard/src/components/LogStream.vue +65 -65
  52. package/packages/dashboard/src/components/PipelineStage.vue +95 -95
  53. package/packages/dashboard/src/components/PipelineView.vue +156 -156
  54. package/packages/dashboard/src/components/ProjectList.vue +210 -210
  55. package/packages/dashboard/src/components/StageBadge.vue +67 -67
  56. package/packages/dashboard/src/components/StepCard.vue +94 -94
  57. package/packages/dashboard/src/components/detail/DocsDetail.vue +48 -48
  58. package/packages/dashboard/src/components/detail/GitDetail.vue +61 -61
  59. package/packages/dashboard/src/components/detail/TechDetail.vue +43 -43
  60. package/packages/dashboard/src/composables/useDashboard.js +170 -170
  61. package/packages/dashboard/src/composables/useKeyboard.js +119 -119
  62. package/packages/dashboard/src/composables/useWebSocket.js +129 -129
  63. package/packages/dashboard/src/main.js +8 -8
  64. package/packages/dashboard/src/style.css +132 -132
  65. package/packages/dashboard/vite.config.js +18 -18
  66. package/src/brainstorm-postcheck.js +158 -158
  67. package/src/change-list.js +52 -52
  68. package/src/change-risk-profile.js +352 -352
  69. package/src/classify-change.js +73 -73
  70. package/src/constants.js +70 -70
  71. package/src/contract-matrix.js +278 -278
  72. package/src/db.js +201 -201
  73. package/src/endpoint-extractor.js +315 -315
  74. package/src/hooks/claude-pre-tool-use.cjs +125 -125
  75. package/src/hooks/worktree-guard.js +653 -653
  76. package/src/index.js +922 -900
  77. package/src/init.js +431 -431
  78. package/src/knowledge-match.js +130 -130
  79. package/src/migrate.js +117 -117
  80. package/src/modules.js +482 -482
  81. package/src/progress.js +1734 -1734
  82. package/src/run.js +3465 -3358
  83. package/src/scan-postcheck.js +387 -383
  84. package/src/setup.js +398 -398
  85. package/src/stage-contract.js +700 -700
  86. package/src/stages/archive.js +160 -160
  87. package/src/stages/brainstorm-auto.js +229 -229
  88. package/src/stages/brainstorm.js +645 -645
  89. package/src/stages/doctor.js +365 -365
  90. package/src/stages/execute.js +625 -625
  91. package/src/stages/explore.js +34 -34
  92. package/src/stages/index.js +29 -29
  93. package/src/stages/knowledge.js +498 -498
  94. package/src/stages/plan-postcheck.js +511 -513
  95. package/src/stages/plan.js +582 -582
  96. package/src/stages/propose.js +174 -174
  97. package/src/stages/quick.js +82 -82
  98. package/src/stages/scan.js +558 -558
  99. package/src/stages/status.js +65 -65
  100. package/src/stages/verify.js +322 -322
  101. package/src/sync.js +497 -497
  102. package/src/task-review.js +346 -346
  103. package/src/workflow.js +785 -785
  104. package/src/worktree-apply.js +549 -549
  105. package/src/worktree-deps.js +185 -0
  106. package/src/worktree.js +982 -932
  107. package/templates/workflows/archive-impact.yaml +79 -79
  108. package/templates/workflows/scan-docs.yaml +132 -132
  109. package/test/brainstorm-plan-contract.test.mjs +273 -273
  110. package/test/check-syntax.mjs +26 -26
  111. package/test/contract-artifacts.test.mjs +323 -323
  112. package/test/decision-supersede.test.mjs +277 -277
  113. package/test/knowledge-match.test.mjs +231 -231
  114. package/test/plan-execute-contract.test.mjs +330 -330
  115. package/test/plan-optimization.test.mjs +572 -572
  116. package/test/platform-artifacts.test.mjs +166 -166
  117. package/test/platform-failure-samples.test.mjs +199 -199
  118. package/test/platform-recovery-chain.test.mjs +167 -167
  119. package/test/platform-recovery.test.mjs +136 -136
  120. package/test/platform-scan-p0.test.mjs +168 -168
  121. package/test/revision-v1.test.mjs +1145 -1145
  122. package/test/run-scan-project-parse.test.mjs +200 -200
  123. package/test/run-tests.mjs +48 -48
  124. package/test/scan-knowledge.test.mjs +175 -175
  125. package/test/scan-paths.test.mjs +68 -68
  126. package/test/scan-postcheck.test.mjs +197 -197
  127. package/test/spec-dir.test.mjs +206 -206
  128. package/test/stage-contract.test.mjs +299 -299
  129. package/test/stage-definitions.test.mjs +39 -39
  130. package/test/wait-gates.test.mjs +496 -496
  131. package/test/worktree-deps-provision.test.mjs +148 -0
  132. package/test/worktree-guard.test.mjs +71 -71
  133. package/test/worktree-native-overlay.test.mjs +188 -188
@@ -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
+ }