sillyspec 3.11.8 → 3.11.10

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.
package/.npmrc.tmp ADDED
@@ -0,0 +1 @@
1
+ //registry.npmjs.org/:_authToken=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
@@ -0,0 +1,197 @@
1
+ # Worktree 隔离
2
+
3
+ SillySpec 在 `execute` 和 `quick` 阶段使用 **git worktree** 隔离 AI 子代理的代码修改,确保主工作区始终干净。
4
+
5
+ ## 概述
6
+
7
+ AI 子代理在执行任务时会在 worktree 中修改源码,而非直接在主工作区操作。任务完成后通过 `worktree apply` 将变更合入主工作区。这带来几个好处:
8
+
9
+ - **安全性** — 主工作区不受意外修改影响
10
+ - **可审计** — 变更集中在一个 diff 中,便于 review
11
+ - **可回退** — 不满意直接 cleanup,主工作区零影响
12
+ - **多 Agent 并行** — 多个子代理可以同时工作在不同的 worktree 中
13
+
14
+ ### 架构示意
15
+
16
+ ```
17
+ 主工作区 worktree 隔离区
18
+ ┌──────────────────────┐ ┌──────────────────────────┐
19
+ │ src/ │ │ .sillyspec/.runtime/ │
20
+ │ (hook 禁止写入) │ │ worktrees/ │
21
+ │ .sillyspec/ │ │ <change-name>/ │
22
+ │ changes/ │ │ src/ (完整副本) │
23
+ │ <change-name>/ │ │ node_modules/ │
24
+ │ design.md │ │ ... │
25
+ │ tasks.md │ │ │
26
+ │ .sillyspec/.runtime/ │ │ 分支: sillyspec/<name> │
27
+ │ gate-status.json │ └──────────────────────────┘
28
+ │ worktrees/ │ │
29
+ │ <change-name>/ │ │ sillyspec worktree apply
30
+ │ meta.json │────────────────────┘
31
+ └──────────────────────┘
32
+ ```
33
+
34
+ ## 命令参考
35
+
36
+ ### `sillyspec worktree create <change-name> [--base <branch>]`
37
+
38
+ 创建一个隔离的 worktree。
39
+
40
+ - 基于 `--base` 分支(默认当前 HEAD)创建新分支 `sillyspec/<change-name>`
41
+ - worktree 目录位于 `.sillyspec/.runtime/worktrees/<change-name>/`
42
+ - 在目录内生成 `meta.json` 记录元数据(分支名、base hash、创建时间等)
43
+ - 如果 worktree 已存在则报错,提示先执行 cleanup
44
+
45
+ ### `sillyspec worktree apply <change-name> [--check-only]`
46
+
47
+ 将 worktree 中的变更合入主工作区。
48
+
49
+ - 读取 `meta.json` 中的 base hash,与主工作区比对
50
+ - 从 `design.md` 解析文件变更清单,校验变更范围
51
+ - `--check-only` 只输出检查结果,不实际 apply
52
+ - 校验通过后生成 patch 并 3-way apply 到主工作区
53
+ - apply 成功后自动清理 worktree
54
+
55
+ **文件变更清单解析:** 从 `.sillyspec/changes/<change-name>/design.md` 中的 `## 文件变更清单` 表格提取。
56
+
57
+ ### `sillyspec worktree list`
58
+
59
+ 列出所有活跃的 worktree。
60
+
61
+ 输出表格包含变更名、分支名和创建时间。
62
+
63
+ ### `sillyspec worktree cleanup <change-name>`
64
+
65
+ 清理指定的 worktree。
66
+
67
+ - 强制移除 worktree 目录
68
+ - 删除 `sillyspec/<change-name>` 分支
69
+ - 删除 `meta.json`
70
+
71
+ > ⚠️ cleanup 会丢弃所有未 apply 的变更,请确认后再执行。
72
+
73
+ ## Hook 拦截机制
74
+
75
+ SillySpec 通过 hook 在 AI 工具调用(Write/Edit/MultiEdit/Bash)前拦截非法写入。判断逻辑采用 **三重门禁**:
76
+
77
+ ```
78
+ allowWrite = stageGate && locationGate && fileGate
79
+ ```
80
+
81
+ ### 阶段门禁(stageGate)
82
+
83
+ - 读取 `.sillyspec/.runtime/gate-status.json`(由 CLI 维护)
84
+ - 只有 `execute` 和 `quick` 阶段允许源码写入
85
+ - 其他阶段(brainstorm/plan/verify/archive/explore)→ 禁止
86
+ - 无 `gate-status.json` → 禁止(默认安全)
87
+
88
+ ### 位置门禁(locationGate)
89
+
90
+ - 目标路径必须在 `.sillyspec/.runtime/worktrees/` 下才允许源码写入
91
+ - 主工作区的源码目录一律禁止
92
+
93
+ ### 文件门禁(fileGate)
94
+
95
+ 文档类、配置类文件在所有阶段放行,不受阶段和位置限制:
96
+
97
+ - `.sillyspec/` 开头的路径
98
+ - `.md` 文件
99
+ - `package.json`、`tsconfig.json`、`local.yaml` 等配置文件
100
+ - `.git/` 下的文件
101
+
102
+ ### Bash 命令拦截
103
+
104
+ | 类型 | 示例 |
105
+ |------|------|
106
+ | **只读放行** | `grep`、`cat`、`git diff`、`git status`、`ls`、`find`、`sillyspec worktree apply/create/list/cleanup` |
107
+ | **禁止** | `git add`、`git commit`、`git push`、`git checkout`、`rm -rf`、`sudo` 等 |
108
+ | **不确定** | 启发式判断,放行但警告 |
109
+
110
+ > 在 worktree 目录下执行 Bash 命令时全部放行,不做拦截。
111
+
112
+ ## 降级方案和逃生开关
113
+
114
+ | 场景 | 处理方式 |
115
+ |------|---------|
116
+ | **git < 2.15** | 不支持 worktree,报错停止 |
117
+ | **`--no-worktree` 标志** | 跳过隔离创建,但 hook 仍然拦截源码写入 |
118
+ | **`SILLYSPEC_DISABLE_HOOKS=1`** | 紧急禁用所有 hook,全部放行 |
119
+ | **无 gate-status.json** | stageGate=false,默认禁止源码写入 |
120
+ | **worktree 创建失败** | 报错停止,不进入无隔离状态 |
121
+
122
+ > ⚠️ 不存在"降级到放行"的路径。只有"降级到更严格"或"紧急逃生开关"。设计原则是默认安全。
123
+
124
+ ## 多 Agent 并行使用
125
+
126
+ 不同的 AI 子代理可以同时创建各自的 worktree:
127
+
128
+ ```bash
129
+ # Agent A 处理 task-01
130
+ sillyspec worktree create feature-auth
131
+ # worktree: .sillyspec/.runtime/worktrees/feature-auth/
132
+
133
+ # Agent B 处理 task-02
134
+ sillyspec worktree create feature-ui
135
+ # worktree: .sillyspec/.runtime/worktrees/feature-ui/
136
+ ```
137
+
138
+ 两个 Agent 在各自的 worktree 中独立工作,互不干扰。各自完成后分别 apply 合入主工作区。
139
+
140
+ > 💡 如果两个 Agent 修改了相同的文件,后 apply 的一方可能遇到冲突。建议在 `plan` 阶段通过 Wave 分组避免同一文件被多个 Agent 修改。
141
+
142
+ ## 环境变量
143
+
144
+ | 变量 | 说明 |
145
+ |------|------|
146
+ | `SILLYSPEC_DISABLE_HOOKS` | 设为 `1` 时禁用所有 hook(紧急逃生) |
147
+ | `SILLYSPEC_WORKTREE_DIR` | 自定义 worktree 存储目录(默认 `.sillyspec/.runtime/worktrees/`) |
148
+
149
+ ## 常见问题和故障排除
150
+
151
+ ### worktree 残留无法清理
152
+
153
+ ```bash
154
+ # 查看所有活跃 worktree
155
+ sillyspec worktree list
156
+
157
+ # 强制清理指定 worktree
158
+ sillyspec worktree cleanup <change-name>
159
+
160
+ # 如果 worktree 目录被手动删除但分支残留
161
+ git worktree prune
162
+ git branch -D sillyspec/<change-name>
163
+ ```
164
+
165
+ ### apply 失败:base hash 不一致
166
+
167
+ 说明主工作区在 worktree 创建后又被修改过。处理方式:
168
+
169
+ 1. 检查主工作区的变更:`git diff`
170
+ 2. 如果主工作区变更不重要 → `git stash` 后重试 apply
171
+ 3. 如果重要 → 手动解决冲突
172
+
173
+ ### apply 失败:文件清单校验不通过
174
+
175
+ 说明 worktree 中修改了 `design.md` 清单之外的文件。处理方式:
176
+
177
+ 1. 查看清单外文件:apply 的输出会列出
178
+ 2. 确认是否是合理的新增(如测试文件)
179
+ 3. 更新 `design.md` 中的文件变更清单后重试
180
+ 4. 或用 `--check-only` 排查后再 apply
181
+
182
+ ### Hook 误拦截了合法操作
183
+
184
+ - 临时方案:设置 `SILLYSPEC_DISABLE_HOOKS=1` 环境变量
185
+ - 检查目标文件是否应加入文件白名单(fileGate)
186
+ - 检查当前阶段是否正确(gate-status.json)
187
+
188
+ ### worktree 内 node_modules 问题
189
+
190
+ worktree 创建后需要安装依赖:
191
+
192
+ ```bash
193
+ cd .sillyspec/.runtime/worktrees/<change-name>/
194
+ npm install
195
+ ```
196
+
197
+ 如果项目使用 pnpm 且有 monorepo,可能需要额外配置。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sillyspec",
3
- "version": "3.11.8",
3
+ "version": "3.11.10",
4
4
  "description": "SillySpec CLI — 流程状态机,让 AI 严格按步骤来",
5
5
  "icon": "logo.jpg",
6
6
  "homepage": "https://sillyspec.ppdmq.top/",
@@ -0,0 +1,52 @@
1
+ import { readFileSync, existsSync } from 'fs'
2
+
3
+ /**
4
+ * 从 design.md 解析文件变更清单
5
+ * @param {string} designMdPath - design.md 文件路径
6
+ * @returns {Set<string>} 文件路径集合(相对路径,如 "src/worktree.js")
7
+ */
8
+ export function parseFileChangeList(designMdPath) {
9
+ const result = new Set()
10
+
11
+ if (!designMdPath || !existsSync(designMdPath)) return result
12
+
13
+ const content = readFileSync(designMdPath, 'utf8')
14
+
15
+ // 定位"文件变更清单"标题
16
+ const sectionRegex = /^#{2,3}\s*文件变更清单/m
17
+ const sectionMatch = content.match(sectionRegex)
18
+ if (!sectionMatch) return result
19
+
20
+ // 从标题后开始,截取到下一个 ## 标题或文件末尾
21
+ const afterSection = content.slice(sectionMatch.index + sectionMatch[0].length)
22
+ const nextSectionMatch = afterSection.match(/^##\s/m)
23
+ const relevantContent = nextSectionMatch
24
+ ? afterSection.slice(0, nextSectionMatch.index)
25
+ : afterSection
26
+
27
+ // 解析表格行
28
+ const lines = relevantContent.split('\n')
29
+ let headerSkipped = false
30
+ for (const line of lines) {
31
+ // 跳过分隔行和非表格行
32
+ if (!line.startsWith('|') || /^\|[-:\s|]+\|$/.test(line)) continue
33
+
34
+ const cells = line.split('|').slice(1, -1) // 去掉首尾空元素
35
+ if (cells.length < 2) continue
36
+
37
+ // 跳过 header 行(包含「文件路径」的表头)
38
+ if (!headerSkipped) {
39
+ headerSkipped = true
40
+ continue
41
+ }
42
+
43
+ const filePath = cells[1].trim()
44
+
45
+ // 忽略空路径、注释、.sillyspec/ 内的路径
46
+ if (!filePath || filePath === '—' || filePath === '-' || filePath.startsWith('.sillyspec/')) continue
47
+
48
+ result.add(filePath)
49
+ }
50
+
51
+ return result
52
+ }
@@ -0,0 +1,125 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Claude Code PreToolUse hook 入口
4
+ *
5
+ * 从 stdin 读取 JSON,调用 worktree-guard 判断是否拦截。
6
+ *
7
+ * Claude Code hooks.json 配置:
8
+ * {
9
+ * "hooks": {
10
+ * "PreToolUse": [
11
+ * {
12
+ * "matcher": "Edit|Write|MultiEdit|Bash",
13
+ * "hooks": [
14
+ * {
15
+ * "type": "command",
16
+ * "command": "node /path/to/sillyspec/src/hooks/claude-pre-tool-use.cjs"
17
+ * }
18
+ * ]
19
+ * }
20
+ * ]
21
+ * }
22
+ * }
23
+ */
24
+
25
+ 'use strict'
26
+
27
+ const path = require('path')
28
+ const fs = require('fs')
29
+
30
+ // 动态 import ESM 模块
31
+ async function main() {
32
+ // Claude Code hook 通过 stdin 传入 JSON
33
+ let input = ''
34
+ try {
35
+ input = await readStdin()
36
+ } catch {
37
+ // 无法读取 stdin → 放行(安全优先于阻断)
38
+ process.exit(0)
39
+ }
40
+
41
+ let parsed
42
+ try {
43
+ parsed = JSON.parse(input)
44
+ } catch {
45
+ // 非法 JSON → 放行
46
+ process.exit(0)
47
+ }
48
+
49
+ // 解析 Claude Code hook 输入格式
50
+ // 格式:{ tool_name: string, tool_input: { ... } }
51
+ const toolName = parsed.tool_name || parsed.tool || ''
52
+ const toolInput = parsed.tool_input || {}
53
+
54
+ // 转换为 shouldBlock 的输入格式
55
+ const toolMap = {
56
+ 'Write': 'Write',
57
+ 'Edit': 'Edit',
58
+ 'MultiEdit': 'MultiEdit',
59
+ 'Bash': 'Bash',
60
+ }
61
+
62
+ const mappedTool = toolMap[toolName]
63
+ if (!mappedTool) {
64
+ // 不在拦截范围内的工具 → 放行
65
+ process.exit(0)
66
+ }
67
+
68
+ // 构造 opts
69
+ const opts = { tool: mappedTool }
70
+ if (mappedTool === 'Bash') {
71
+ opts.command = toolInput.command || ''
72
+ } else {
73
+ // Write/Edit/MultiEdit
74
+ const fp = toolInput.file_path || toolInput.filePath
75
+ if (fp) opts.filePath = fp
76
+ // MultiEdit 可能有多个文件
77
+ if (mappedTool === 'MultiEdit') {
78
+ const fps = toolInput.edits
79
+ ? toolInput.edits.map(e => e.file_path || e.filePath).filter(Boolean)
80
+ : []
81
+ if (fps.length > 0) opts.filePaths = fps
82
+ }
83
+ }
84
+
85
+ // 加载 worktree-guard(ESM)
86
+ let shouldBlock
87
+ try {
88
+ const mod = await import('./worktree-guard.js')
89
+ shouldBlock = mod.shouldBlock
90
+ } catch (e) {
91
+ // 模块加载失败 → 放行(不因为 hook 出错阻断工作流)
92
+ // eslint-disable-next-line no-console
93
+ console.error(`[sillyspec-hook] 模块加载失败: ${e.message}`)
94
+ process.exit(0)
95
+ }
96
+
97
+ const result = shouldBlock(opts)
98
+
99
+ if (result.blocked) {
100
+ // 输出错误信息(Claude Code 会显示给 agent)
101
+ // eslint-disable-next-line no-console
102
+ console.error(`[sillyspec] ❌ ${result.reason || 'blocked by worktree guard'}`)
103
+ process.exit(2) // exit code 2 = 阻止工具执行
104
+ }
105
+
106
+ // 放行
107
+ process.exit(0)
108
+ }
109
+
110
+ function readStdin() {
111
+ return new Promise((resolve, reject) => {
112
+ const chunks = []
113
+ process.stdin.setEncoding('utf8')
114
+ process.stdin.on('data', chunk => chunks.push(chunk))
115
+ process.stdin.on('end', () => resolve(chunks.join('')))
116
+ process.stdin.on('error', reject)
117
+ // 超时保护(3秒)
118
+ setTimeout(() => {
119
+ process.stdin.destroy()
120
+ resolve('')
121
+ }, 3000)
122
+ })
123
+ }
124
+
125
+ main().catch(() => process.exit(0))