sillyspec 3.20.3 → 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 (224) hide show
  1. package/.claude/skills/sillyspec-archive/SKILL.md +21 -17
  2. package/.claude/skills/sillyspec-auto/SKILL.md +83 -77
  3. package/.claude/skills/sillyspec-brainstorm/SKILL.md +44 -17
  4. package/.claude/skills/sillyspec-commit/SKILL.md +106 -105
  5. package/.claude/skills/sillyspec-continue/SKILL.md +45 -44
  6. package/.claude/skills/sillyspec-doctor/SKILL.md +31 -22
  7. package/.claude/skills/sillyspec-execute/SKILL.md +30 -17
  8. package/.claude/skills/sillyspec-explore/SKILL.md +109 -96
  9. package/.claude/skills/sillyspec-knowledge/SKILL.md +270 -0
  10. package/.claude/skills/sillyspec-plan/SKILL.md +21 -52
  11. package/.claude/skills/sillyspec-propose/SKILL.md +21 -17
  12. package/.claude/skills/sillyspec-quick/SKILL.md +21 -17
  13. package/.claude/skills/sillyspec-resume/SKILL.md +68 -111
  14. package/.claude/skills/sillyspec-scan/SKILL.md +21 -17
  15. package/.claude/skills/sillyspec-state/SKILL.md +54 -54
  16. package/.claude/skills/sillyspec-status/SKILL.md +21 -17
  17. package/.claude/skills/sillyspec-verify/SKILL.md +21 -17
  18. package/.claude/skills/sillyspec-workspace/SKILL.md +157 -149
  19. package/.husky/pre-push +13 -0
  20. package/CLAUDE.md +18 -0
  21. package/README.md +198 -186
  22. package/SKILL.md +90 -87
  23. package/bin/sillyspec.js +2 -2
  24. package/docs/brainstorm-plan-contract.md +64 -0
  25. package/docs/plan-execute-contract.md +123 -0
  26. package/docs/platform-scan-protocol.md +298 -0
  27. package/docs/revision-mode.md +115 -0
  28. package/docs/sillyspec/file-lifecycle/known-implementation-gaps.md +99 -0
  29. package/docs/sillyspec/file-lifecycle/platform-workflows-sync.md +218 -0
  30. package/docs/sillyspec/file-lifecycle/stage-artifacts.md +167 -0
  31. package/docs/sillyspec/file-lifecycle/storage-and-state.md +148 -0
  32. package/docs/sillyspec/file-lifecycle/worktree-and-guard.md +211 -0
  33. package/docs/sillyspec/file-lifecycle.md +125 -0
  34. package/docs/workflow-contract-regression.md +106 -0
  35. package/docs/worktree-isolation.md +252 -0
  36. package/package.json +40 -34
  37. package/packages/dashboard/dist/assets/{index-D1EVTLmc.js → index-Bq_Z2hne.js} +7446 -7446
  38. package/packages/dashboard/dist/assets/index-O2W5RV4z.css +1 -0
  39. package/packages/dashboard/dist/index.html +16 -16
  40. package/packages/dashboard/dist/prototype-dashboard.html +836 -0
  41. package/packages/dashboard/dist/prototype-overview.html +256 -0
  42. package/packages/dashboard/index.html +15 -15
  43. package/packages/dashboard/package-lock.json +2384 -2384
  44. package/packages/dashboard/package.json +25 -25
  45. package/packages/dashboard/public/prototype-dashboard.html +836 -0
  46. package/packages/dashboard/public/prototype-overview.html +256 -0
  47. package/packages/dashboard/server/executor.js +86 -86
  48. package/packages/dashboard/server/index.js +588 -508
  49. package/packages/dashboard/server/parser.js +526 -458
  50. package/packages/dashboard/server/watcher.js +350 -342
  51. package/packages/dashboard/src/App.vue +558 -325
  52. package/packages/dashboard/src/components/ActionBar.vue +93 -84
  53. package/packages/dashboard/src/components/CommandPalette.vue +96 -92
  54. package/packages/dashboard/src/components/DetailPanel.vue +137 -137
  55. package/packages/dashboard/src/components/DocPreview.vue +105 -8
  56. package/packages/dashboard/src/components/DocTree.vue +75 -19
  57. package/packages/dashboard/src/components/HResizeHandle.vue +48 -0
  58. package/packages/dashboard/src/components/LogStream.vue +65 -65
  59. package/packages/dashboard/src/components/PipelineStage.vue +95 -75
  60. package/packages/dashboard/src/components/PipelineView.vue +156 -129
  61. package/packages/dashboard/src/components/ProjectCard.vue +187 -0
  62. package/packages/dashboard/src/components/ProjectList.vue +210 -210
  63. package/packages/dashboard/src/components/ProjectOverview.vue +113 -139
  64. package/packages/dashboard/src/components/StageBadge.vue +67 -53
  65. package/packages/dashboard/src/components/StepCard.vue +94 -89
  66. package/packages/dashboard/src/components/VResizeHandle.vue +61 -0
  67. package/packages/dashboard/src/components/detail/DocsDetail.vue +48 -48
  68. package/packages/dashboard/src/components/detail/GitDetail.vue +61 -61
  69. package/packages/dashboard/src/components/detail/TechDetail.vue +43 -43
  70. package/packages/dashboard/src/composables/useDashboard.js +192 -164
  71. package/packages/dashboard/src/composables/useKeyboard.js +119 -119
  72. package/packages/dashboard/src/composables/useLayout.js +131 -0
  73. package/packages/dashboard/src/composables/useWebSocket.js +129 -129
  74. package/packages/dashboard/src/main.js +8 -8
  75. package/packages/dashboard/src/style.css +132 -132
  76. package/packages/dashboard/vite.config.js +18 -18
  77. package/src/brainstorm-postcheck.js +158 -0
  78. package/src/change-list.js +52 -0
  79. package/src/change-risk-profile.js +352 -0
  80. package/src/classify-change.js +73 -0
  81. package/src/constants.js +70 -0
  82. package/src/contract-matrix.js +278 -0
  83. package/src/db.js +201 -0
  84. package/src/endpoint-extractor.js +315 -0
  85. package/src/hooks/claude-pre-tool-use.cjs +125 -0
  86. package/src/hooks/worktree-guard.js +761 -0
  87. package/src/index.js +922 -252
  88. package/src/init.js +431 -375
  89. package/src/knowledge-match.js +130 -0
  90. package/src/migrate.js +117 -117
  91. package/src/modules.js +482 -0
  92. package/src/progress.js +1734 -508
  93. package/src/run.js +3465 -652
  94. package/src/scan-postcheck.js +387 -0
  95. package/src/setup.js +398 -460
  96. package/src/stage-contract.js +700 -0
  97. package/src/stages/archive.js +160 -54
  98. package/src/stages/brainstorm-auto.js +229 -0
  99. package/src/stages/brainstorm.js +645 -239
  100. package/src/stages/doctor.js +365 -312
  101. package/src/stages/execute.js +625 -264
  102. package/src/stages/explore.js +34 -0
  103. package/src/stages/index.js +29 -35
  104. package/src/stages/knowledge.js +498 -0
  105. package/src/stages/plan-postcheck.js +511 -0
  106. package/src/stages/plan.js +582 -279
  107. package/src/stages/propose.js +174 -115
  108. package/src/stages/quick.js +102 -63
  109. package/src/stages/scan.js +558 -141
  110. package/src/stages/status.js +65 -65
  111. package/src/stages/verify.js +322 -135
  112. package/src/sync.js +497 -0
  113. package/src/task-review.js +346 -0
  114. package/src/workflow.js +785 -0
  115. package/src/worktree-apply.js +549 -0
  116. package/src/worktree-deps.js +185 -0
  117. package/src/worktree.js +982 -0
  118. package/templates/workflows/archive-impact.yaml +79 -0
  119. package/templates/workflows/scan-docs.yaml +132 -0
  120. package/test/brainstorm-plan-contract.test.mjs +273 -0
  121. package/test/check-syntax.mjs +26 -0
  122. package/test/cli-top-level-aliases.test.mjs +174 -0
  123. package/test/contract-artifacts.test.mjs +323 -0
  124. package/test/decision-supersede.test.mjs +277 -0
  125. package/test/knowledge-match.test.mjs +231 -0
  126. package/test/plan-execute-contract.test.mjs +330 -0
  127. package/test/plan-optimization.test.mjs +572 -0
  128. package/test/platform-artifacts.test.mjs +190 -0
  129. package/test/platform-failure-samples.test.mjs +199 -0
  130. package/test/platform-recovery-chain.test.mjs +179 -0
  131. package/test/platform-recovery.test.mjs +167 -0
  132. package/test/platform-scan-p0.test.mjs +175 -0
  133. package/test/revision-v1.test.mjs +1145 -0
  134. package/test/run-sanitize-project-name.test.mjs +51 -0
  135. package/test/run-scan-postcheck-fail.test.mjs +64 -0
  136. package/test/run-scan-project-parse.test.mjs +200 -0
  137. package/test/run-tests.mjs +48 -0
  138. package/test/scan-docs-yaml-placeholders.test.mjs +84 -0
  139. package/test/scan-knowledge.test.mjs +175 -0
  140. package/test/scan-paths.test.mjs +68 -0
  141. package/test/scan-postcheck-project-priority.test.mjs +85 -0
  142. package/test/scan-postcheck.test.mjs +197 -0
  143. package/test/scan-workflow-anyfailed-block.test.mjs +52 -0
  144. package/test/spec-dir.test.mjs +206 -0
  145. package/test/stage-contract-failed-post-check.test.mjs +102 -0
  146. package/test/stage-contract.test.mjs +299 -0
  147. package/test/stage-definitions.test.mjs +39 -0
  148. package/test/wait-gates.test.mjs +501 -0
  149. package/test/workflow-spec-base.test.mjs +142 -0
  150. package/test/worktree-deps-provision.test.mjs +148 -0
  151. package/test/worktree-guard.test.mjs +136 -0
  152. package/test/worktree-native-overlay.test.mjs +188 -0
  153. package/.sillyspec/changes/archive/2026-04-08-derive-state/design.md +0 -97
  154. package/.sillyspec/changes/archive/2026-04-08-derive-state/plan.md +0 -51
  155. package/.sillyspec/changes/archive/2026-04-08-derive-state/proposal.md +0 -29
  156. package/.sillyspec/changes/archive/2026-04-08-derive-state/requirements.md +0 -34
  157. package/.sillyspec/changes/archive/2026-04-08-derive-state/tasks.md +0 -13
  158. package/.sillyspec/changes/archive/2026-04-08-derive-state/verify-result.md +0 -43
  159. package/.sillyspec/changes/auto-mode/design.md +0 -50
  160. package/.sillyspec/changes/auto-mode/proposal.md +0 -19
  161. package/.sillyspec/changes/auto-mode/requirements.md +0 -21
  162. package/.sillyspec/changes/auto-mode/tasks.md +0 -7
  163. package/.sillyspec/changes/brainstorm-archive/2026-04-05-dashboard-design.md +0 -206
  164. package/.sillyspec/changes/brainstorm-archive/2026-04-05-unified-docs-design.md +0 -199
  165. package/.sillyspec/changes/dashboard/design.md +0 -219
  166. package/.sillyspec/changes/dashboard/design.md.braindraft +0 -206
  167. package/.sillyspec/changes/run-command-design/design.md +0 -1230
  168. package/.sillyspec/changes/unified-docs-design/design.md +0 -199
  169. package/.sillyspec/docs/sillyspec/scan/.gitkeep +0 -0
  170. package/.sillyspec/knowledge/INDEX.md +0 -8
  171. package/.sillyspec/knowledge/uncategorized.md +0 -3
  172. package/.sillyspec/plans/2026-04-05-dashboard.md +0 -737
  173. package/.sillyspec/projects/sillyspec.yaml +0 -3
  174. package/dist/steps/brainstorm/01-load-context.md +0 -30
  175. package/dist/steps/brainstorm/02-reuse-check.md +0 -6
  176. package/dist/steps/brainstorm/03-prototype-analysis.md +0 -11
  177. package/dist/steps/brainstorm/04-module-split.md +0 -23
  178. package/dist/steps/brainstorm/05-dialog-explore.md +0 -8
  179. package/dist/steps/brainstorm/06-propose-approaches.md +0 -3
  180. package/dist/steps/brainstorm/07-present-design.md +0 -3
  181. package/dist/steps/brainstorm/08-write-design.md +0 -21
  182. package/dist/steps/brainstorm/09-self-review.md +0 -15
  183. package/dist/steps/brainstorm/10-user-confirm.md +0 -3
  184. package/dist/steps/brainstorm/11-output-spec.md +0 -7
  185. package/dist/steps/brainstorm/manifest.yaml +0 -26
  186. package/dist/steps/execute/01-load-context.md +0 -41
  187. package/dist/steps/execute/02-scan-conventions.md +0 -47
  188. package/dist/steps/execute/03-skill-mcp.md +0 -19
  189. package/dist/steps/execute/04-assign-task.md +0 -22
  190. package/dist/steps/execute/04b-prompt-template.md +0 -54
  191. package/dist/steps/execute/05-write-test.md +0 -7
  192. package/dist/steps/execute/06-write-code.md +0 -8
  193. package/dist/steps/execute/07-run-test.md +0 -26
  194. package/dist/steps/execute/08-fix-issues.md +0 -28
  195. package/dist/steps/execute/09-next-task.md +0 -33
  196. package/dist/steps/execute/manifest.yaml +0 -28
  197. package/dist/steps/plan/01-load-context.md +0 -22
  198. package/dist/steps/plan/02-anchor-confirm.md +0 -1
  199. package/dist/steps/plan/03-expand-tasks.md +0 -33
  200. package/dist/steps/plan/04-mark-order.md +0 -15
  201. package/dist/steps/plan/05-e2e-planning.md +0 -17
  202. package/dist/steps/plan/06-self-check.md +0 -16
  203. package/dist/steps/plan/07-save.md +0 -1
  204. package/dist/steps/plan/manifest.yaml +0 -18
  205. package/dist/steps/scan/01-env-detect.md +0 -51
  206. package/dist/steps/scan/02-tech-stack.md +0 -16
  207. package/dist/steps/scan/03-conventions.md +0 -16
  208. package/dist/steps/scan/04-structure.md +0 -19
  209. package/dist/steps/scan/05-quality.md +0 -18
  210. package/dist/steps/scan/06-complete.md +0 -49
  211. package/dist/steps/scan/manifest.yaml +0 -16
  212. package/dist/steps/verify/01-load-specs.md +0 -28
  213. package/dist/steps/verify/02-check-tasks.md +0 -1
  214. package/dist/steps/verify/03-check-design.md +0 -6
  215. package/dist/steps/verify/04-run-tests.md +0 -7
  216. package/dist/steps/verify/05-e2e-tests.md +0 -27
  217. package/dist/steps/verify/05b-e2e-fix.md +0 -33
  218. package/dist/steps/verify/06-code-quality.md +0 -25
  219. package/dist/steps/verify/07-lint-check.md +0 -27
  220. package/dist/steps/verify/08-output-report.md +0 -14
  221. package/dist/steps/verify/manifest.yaml +0 -22
  222. package/packages/dashboard/dist/assets/index-DGe8CqeP.css +0 -1
  223. package/src/derive.js +0 -147
  224. package/src/step.js +0 -543
@@ -1,458 +1,526 @@
1
- import { readFileSync, existsSync, readdirSync, statSync } from 'fs'
2
- import { execSync } from 'child_process'
3
- import { join } 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
- const progressPath = join(sillyspecDir, '.runtime', 'progress.json')
81
- if (existsSync(progressPath)) {
82
- try {
83
- const progress = JSON.parse(readFileSync(progressPath, 'utf-8'))
84
- if (progress.stages) {
85
- for (const stageData of Object.values(progress.stages)) {
86
- if (stageData.lastActive && (!result.lastActive || new Date(stageData.lastActive) > new Date(result.lastActive))) {
87
- result.lastActive = stageData.lastActive
88
- }
89
- }
90
- }
91
- if (progress.lastActive) result.lastActive = progress.lastActive
92
- } catch {}
93
- }
94
- if (!result.lastActive) {
95
- // Fallback: most recently modified file in .sillyspec
96
- try {
97
- const findRecent = (dir) => {
98
- let latest = null
99
- if (!existsSync(dir)) return latest
100
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
101
- const p = join(dir, entry.name)
102
- try {
103
- const s = statSync(p)
104
- if (entry.isDirectory()) {
105
- const sub = findRecent(p)
106
- if (sub && (!latest || sub > latest)) latest = sub
107
- } else if (!latest || s.mtimeMs > latest) {
108
- latest = s.mtimeMs
109
- }
110
- } catch {}
111
- }
112
- return latest
113
- }
114
- const ts = findRecent(sillyspecDir)
115
- if (ts) result.lastActive = new Date(ts).toISOString()
116
- } catch {}
117
- }
118
-
119
- // --- Doc stats ---
120
- const docsDir = join(sillyspecDir, 'docs')
121
- if (existsSync(docsDir)) {
122
- const typeMap = { brainstorm: 'design', plan: 'plan', archive: 'archive', changes: 'changes', scan: 'scan', quicklog: 'quicklog' }
123
- try {
124
- for (const projDir of readdirSync(docsDir, { withFileTypes: true }).filter(d => d.isDirectory())) {
125
- for (const typeDir of readdirSync(join(docsDir, projDir.name), { withFileTypes: true }).filter(d => d.isDirectory())) {
126
- const key = typeMap[typeDir.name]
127
- if (!key) continue
128
- const count = countMdFiles(join(docsDir, projDir.name, typeDir.name))
129
- result.docStats[key] += count
130
- result.docStats.total += count
131
- }
132
- }
133
- } catch {}
134
- }
135
-
136
- // --- Git info ---
137
- try {
138
- result.git.lastCommit = execSync('git log -1 --format=%s', { cwd: projectPath, encoding: 'utf-8' }).trim()
139
- } catch {}
140
- try {
141
- result.git.branch = execSync('git branch --show-current', { cwd: projectPath, encoding: 'utf-8' }).trim()
142
- } catch {}
143
- try {
144
- result.git.dirtyCount = parseInt(execSync('git status --porcelain', { cwd: projectPath, encoding: 'utf-8' }).trim().split('\n').filter(Boolean).length, 10) || 0
145
- } catch {}
146
-
147
- return result
148
- }
149
-
150
- export function parseGitDetail(projectPath) {
151
- const result = { branch: '', commits: [], untracked: [] }
152
- try {
153
- result.branch = execSync('git branch --show-current', { cwd: projectPath, encoding: 'utf-8' }).trim()
154
- } catch {}
155
- try {
156
- const log = execSync('git log -5 --format=%h|%s|%an|%aI', { cwd: projectPath, encoding: 'utf-8' }).trim()
157
- result.commits = log.split('\n').filter(Boolean).map(line => {
158
- const [hash, message, author, date] = line.split('|')
159
- return { hash, message, author, date }
160
- })
161
- } catch {}
162
- try {
163
- const status = execSync('git status --porcelain', { cwd: projectPath, encoding: 'utf-8' }).trim()
164
- result.untracked = status.split('\n').filter(Boolean).map(line => ({
165
- status: line.slice(0, 2).trim(),
166
- file: line.slice(3)
167
- }))
168
- } catch {}
169
- return result
170
- }
171
-
172
- export function parseTechStackDetail(projectPath) {
173
- const result = { frameworks: [], dependencies: {}, devDependencies: {} }
174
- const pkgPath = join(projectPath, 'package.json')
175
- if (existsSync(pkgPath)) {
176
- try {
177
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
178
- result.dependencies = pkg.dependencies || {}
179
- result.devDependencies = pkg.devDependencies || {}
180
- const allDeps = Object.keys({ ...result.dependencies, ...result.devDependencies })
181
- for (const pattern of FRAMEWORK_PATTERNS) {
182
- if (pattern.keys.some(k => allDeps.includes(k))) {
183
- result.frameworks.push(pattern.name)
184
- }
185
- }
186
- } catch {}
187
- }
188
- return result
189
- }
190
-
191
- export function parseDocsList(projectPath) {
192
- const sillyspecDir = join(projectPath, '.sillyspec')
193
- const docsDir = join(sillyspecDir, 'docs')
194
- const groups = []
195
- if (!existsSync(docsDir)) return groups
196
- const typeMap = {
197
- brainstorm: { label: '设计文档', icon: '📋' },
198
- plan: { label: '实现计划', icon: '📐' },
199
- archive: { label: '已归档', icon: '📦' },
200
- changes: { label: '当前变更', icon: '⚙️' },
201
- scan: { label: '架构文档', icon: '🔍' },
202
- quicklog: { label: '快速修复', icon: '⚡' }
203
- }
204
- try {
205
- for (const projDir of readdirSync(docsDir, { withFileTypes: true }).filter(d => d.isDirectory())) {
206
- for (const typeDir of readdirSync(join(docsDir, projDir.name), { withFileTypes: true }).filter(d => d.isDirectory())) {
207
- const cfg = typeMap[typeDir.name]
208
- if (!cfg) continue
209
- const files = listFilesRecursive(join(docsDir, projDir.name, typeDir.name))
210
- if (files.length) {
211
- groups.push({ key: typeDir.name, label: cfg.label, icon: cfg.icon, files })
212
- }
213
- }
214
- }
215
- } catch {}
216
- return groups
217
- }
218
-
219
- function listFilesRecursive(dir) {
220
- const files = []
221
- try {
222
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
223
- const p = join(dir, entry.name)
224
- if (entry.isDirectory()) {
225
- const sub = listFilesRecursive(p)
226
- for (const f of sub) f.name = entry.name + '/' + f.name
227
- files.push(...sub)
228
- } else if (entry.name.endsWith('.md')) {
229
- const s = statSync(p)
230
- files.push({ name: entry.name, path: p, size: s.size, mtime: s.mtime.toISOString() })
231
- }
232
- }
233
- } catch {}
234
- return files
235
- }
236
-
237
- function countMdFiles(dir) {
238
- let count = 0
239
- try {
240
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
241
- if (entry.isDirectory()) {
242
- count += countMdFiles(join(dir, entry.name))
243
- } else if (entry.name.endsWith('.md')) {
244
- count++
245
- }
246
- }
247
- } catch {}
248
- return count
249
- }
250
-
251
- export function parseDocsTree(projectPath) {
252
- const sillyspecDir = join(projectPath, '.sillyspec')
253
- const docsDir = join(sillyspecDir, 'docs')
254
-
255
- if (!existsSync(docsDir)) {
256
- return { groups: [] }
257
- }
258
-
259
- const groupConfig = [
260
- { key: 'brainstorm', label: '📋 设计文档', icon: '📋', dir: 'brainstorm' },
261
- { key: 'plan', label: '📐 实现计划', icon: '📐', dir: 'plan' },
262
- { key: 'changes', label: '⚙️ 当前变更', icon: '⚙️', dir: 'changes' },
263
- { key: 'archive', label: '📦 已归档', icon: '📦', dir: 'archive' },
264
- { key: 'scan', label: '🔍 架构文档', icon: '🔍', dir: 'scan' },
265
- { key: 'quicklog', label: '⚡ 快速修复', icon: '⚡', dir: 'quicklog' },
266
- ]
267
-
268
- const groups = []
269
-
270
- // Find project dirs under docs/
271
- const projectDirs = existsSync(docsDir) ? readdirSync(docsDir, { withFileTypes: true })
272
- .filter(d => d.isDirectory()).map(d => d.name) : []
273
-
274
- for (const projName of projectDirs) {
275
- const projDocsDir = join(docsDir, projName)
276
-
277
- for (const group of groupConfig) {
278
- const groupDir = join(projDocsDir, group.dir)
279
- if (!existsSync(groupDir)) continue
280
-
281
- const files = []
282
- try {
283
- const entries = readdirSync(groupDir, { withFileTypes: true })
284
- for (const entry of entries) {
285
- if (entry.isDirectory()) {
286
- // For changes/ and archive/ subdirs
287
- const subDir = join(groupDir, entry.name)
288
- try {
289
- const subFiles = readdirSync(subDir).filter(f => f.endsWith('.md'))
290
- for (const sf of subFiles) {
291
- const filePath = join(subDir, sf)
292
- files.push({
293
- name: `${entry.name}/${sf}`,
294
- path: filePath,
295
- title: sf.replace('.md', '')
296
- })
297
- }
298
- } catch {}
299
- } else if (entry.name.endsWith('.md')) {
300
- const filePath = join(groupDir, entry.name)
301
- let title = entry.name.replace('.md', '')
302
- try {
303
- const content = readFileSync(filePath, 'utf-8')
304
- const titleMatch = content.match(/^#\s+(.+)$/m)
305
- if (titleMatch) title = titleMatch[1]
306
- } catch {}
307
- files.push({ name: entry.name, path: filePath, title })
308
- }
309
- }
310
- } catch {}
311
-
312
- if (files.length > 0) {
313
- groups.push({
314
- key: `${projName}::${group.key}`,
315
- label: group.label,
316
- project: projName,
317
- files
318
- })
319
- }
320
- }
321
- }
322
-
323
- return { groups }
324
- }
325
-
326
- /**
327
- * Parse project state from .sillyspec directory
328
- * @param {string} projectPath - Path to the project directory
329
- * @returns {object} Project state with currentStage, nextStep, progress, stages, specs, lastActive
330
- */
331
- export function parseProjectState(projectPath) {
332
- const sillyspecDir = join(projectPath, '.sillyspec')
333
-
334
- if (!existsSync(sillyspecDir)) {
335
- return null
336
- }
337
-
338
- let currentStage = ''
339
- let nextStep = null
340
- let progress = { stages: {} }
341
- let stages = []
342
- let specs = []
343
- let lastActive = null
344
-
345
- // Read progress.json for current stage
346
- const progressPath = join(sillyspecDir, '.runtime', 'progress.json')
347
- if (existsSync(progressPath)) {
348
- try {
349
- const progressData = JSON.parse(readFileSync(progressPath, 'utf-8'))
350
- progress = progressData
351
- currentStage = progressData.currentStage || ''
352
- stages = Object.keys(progressData.stages || {})
353
-
354
- // Find last active
355
- if (progressData.lastActive) lastActive = progressData.lastActive
356
- if (progressData.stages) {
357
- for (const [stageName, stageData] of Object.entries(progressData.stages)) {
358
- if (stageData.lastActive || stageData.startedAt) {
359
- const t = stageData.lastActive || stageData.startedAt
360
- if (!lastActive || new Date(t) > new Date(lastActive)) lastActive = t
361
- }
362
- }
363
- }
364
- } catch (err) {
365
- // Progress file exists but couldn't be parsed
366
- }
367
- }
368
-
369
- // List all spec files
370
- const specsDir = join(sillyspecDir, 'specs')
371
- if (existsSync(specsDir)) {
372
- try {
373
- const specFiles = readdirSync(specsDir)
374
- .filter(f => f.endsWith('.md'))
375
- .sort()
376
-
377
- specs = specFiles.map(f => {
378
- const specPath = join(specsDir, f)
379
- try {
380
- const content = readFileSync(specPath, 'utf-8')
381
- const titleMatch = content.match(/^#\s+(.+)$/m)
382
- return {
383
- name: f,
384
- title: titleMatch ? titleMatch[1] : f,
385
- path: specPath
386
- }
387
- } catch {
388
- return { name: f, title: f, path: specPath }
389
- }
390
- })
391
- } catch (err) {
392
- // Specs directory couldn't be read
393
- }
394
- }
395
-
396
- return {
397
- currentStage,
398
- nextStep,
399
- progress,
400
- stages,
401
- specs,
402
- lastActive
403
- }
404
- }
405
-
406
- /**
407
- * Parse spec file content
408
- * @param {string} specPath - Path to the spec file
409
- * @returns {object} Parsed spec with title, sections, and metadata
410
- */
411
- export function parseSpecFile(specPath) {
412
- if (!existsSync(specPath)) {
413
- return null
414
- }
415
-
416
- try {
417
- const content = readFileSync(specPath, 'utf-8')
418
- const lines = content.split('\n')
419
-
420
- const titleMatch = content.match(/^#\s+(.+)$/m)
421
- const title = titleMatch ? titleMatch[1] : 'Unknown'
422
-
423
- const sections = []
424
- let currentSection = null
425
- let currentContent = []
426
-
427
- for (let i = 0; i < lines.length; i++) {
428
- const line = lines[i]
429
- const headingMatch = line.match(/^(#{2,4})\s+(.+)$/)
430
-
431
- if (headingMatch) {
432
- if (currentSection) {
433
- sections.push({
434
- level: currentSection.level,
435
- title: currentSection.title,
436
- content: currentContent.join('\n').trim()
437
- })
438
- }
439
- currentSection = { level: headingMatch[1].length, title: headingMatch[2] }
440
- currentContent = []
441
- } else if (currentSection) {
442
- currentContent.push(line)
443
- }
444
- }
445
-
446
- if (currentSection) {
447
- sections.push({
448
- level: currentSection.level,
449
- title: currentSection.title,
450
- content: currentContent.join('\n').trim()
451
- })
452
- }
453
-
454
- return { title, sections, content }
455
- } catch (err) {
456
- return null
457
- }
458
- }
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
+ }