@michaelxwb/code-flow 0.5.1 → 0.6.0

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 (76) hide show
  1. package/docs/migrations/spec-workflow-0.6.0.md +42 -0
  2. package/package.json +4 -2
  3. package/src/adapters/claude/CLAUDE.md +11 -17
  4. package/src/adapters/claude/commands/cf-init.md +5 -4
  5. package/src/adapters/claude/commands/cf-spec.md +50 -0
  6. package/src/adapters/claude/commands/cf-task/align.md +29 -3
  7. package/src/adapters/claude/commands/cf-task/archive.md +32 -4
  8. package/src/adapters/claude/commands/cf-task/plan.md +54 -1
  9. package/src/adapters/claude/commands/cf-task/prd.md +24 -0
  10. package/src/adapters/claude/commands/cf-task/start.md +56 -14
  11. package/src/adapters/claude/settings.local.json +1 -11
  12. package/src/adapters/codex/AGENTS.md +11 -17
  13. package/src/adapters/codex/hooks.json +6 -4
  14. package/src/adapters/codex/skills/cf-init/SKILL.md +5 -4
  15. package/src/adapters/codex/skills/cf-spec/SKILL.md +55 -0
  16. package/src/adapters/codex/skills/cf-task-align/SKILL.md +29 -3
  17. package/src/adapters/codex/skills/cf-task-archive/SKILL.md +32 -4
  18. package/src/adapters/codex/skills/cf-task-plan/SKILL.md +54 -1
  19. package/src/adapters/codex/skills/cf-task-prd/SKILL.md +24 -0
  20. package/src/adapters/codex/skills/cf-task-start/SKILL.md +56 -14
  21. package/src/adapters/costrict/CLAUDE.md +11 -17
  22. package/src/adapters/costrict/commands/cf-init.md +5 -4
  23. package/src/adapters/costrict/commands/cf-spec.md +50 -0
  24. package/src/adapters/costrict/commands/cf-task/align.md +29 -3
  25. package/src/adapters/costrict/commands/cf-task/archive.md +32 -4
  26. package/src/adapters/costrict/commands/cf-task/plan.md +54 -1
  27. package/src/adapters/costrict/commands/cf-task/prd.md +24 -0
  28. package/src/adapters/costrict/commands/cf-task/start.md +56 -14
  29. package/src/adapters/costrict/settings.local.json +1 -11
  30. package/src/adapters/opencode/AGENTS.md +11 -17
  31. package/src/adapters/opencode/commands/cf-init.md +4 -3
  32. package/src/adapters/opencode/commands/cf-spec.md +54 -0
  33. package/src/adapters/opencode/commands/cf-task/align.md +30 -3
  34. package/src/adapters/opencode/commands/cf-task/archive.md +33 -4
  35. package/src/adapters/opencode/commands/cf-task/plan.md +55 -1
  36. package/src/adapters/opencode/commands/cf-task/prd.md +25 -0
  37. package/src/adapters/opencode/commands/cf-task/start.md +57 -14
  38. package/src/adapters/opencode/plugins/code-flow/index.js +0 -1
  39. package/src/cli.js +51 -2
  40. package/src/core/code-flow/config.yml +13 -16
  41. package/src/core/code-flow/scripts/cf_checks.py +30 -0
  42. package/src/core/code-flow/scripts/cf_core.py +12 -255
  43. package/src/core/code-flow/scripts/cf_log.py +8 -0
  44. package/src/core/code-flow/scripts/cf_post_hook.py +33 -9
  45. package/src/core/code-flow/scripts/cf_pre_tool_hook.py +65 -0
  46. package/src/core/code-flow/scripts/cf_spec_context.py +1233 -0
  47. package/src/core/code-flow/scripts/cf_spec_gate.py +224 -0
  48. package/src/core/code-flow/scripts/cf_spec_metadata.py +304 -0
  49. package/src/core/code-flow/scripts/cf_spec_migrate.py +512 -0
  50. package/src/core/code-flow/scripts/cf_spec_resolver.py +271 -0
  51. package/src/core/code-flow/scripts/cf_spec_router.py +145 -0
  52. package/src/core/code-flow/scripts/cf_spec_session.py +122 -0
  53. package/src/core/code-flow/scripts/cf_spec_verify.py +293 -0
  54. package/src/core/code-flow/scripts/cf_stats.py +40 -5
  55. package/src/core/code-flow/scripts/cf_stop_hook.py +107 -7
  56. package/src/core/code-flow/scripts/cf_task_runtime.py +142 -0
  57. package/src/core/code-flow/scripts/cf_user_prompt_hook.py +34 -360
  58. package/src/core/code-flow/specs/backend/code-quality-performance.md +13 -1
  59. package/src/core/code-flow/specs/backend/database.md +13 -1
  60. package/src/core/code-flow/specs/backend/directory-structure.md +13 -1
  61. package/src/core/code-flow/specs/backend/logging.md +13 -1
  62. package/src/core/code-flow/specs/backend/platform-rules.md +13 -1
  63. package/src/core/code-flow/specs/frontend/component-specs.md +13 -1
  64. package/src/core/code-flow/specs/frontend/directory-structure.md +13 -1
  65. package/src/core/code-flow/specs/frontend/quality-standards.md +13 -1
  66. package/src/core/code-flow/specs/shared/design/design-frontend.md +20 -10
  67. package/src/core/code-flow/specs/shared/design/design-full.md +36 -24
  68. package/src/core/code-flow/specs/shared/design/design-lite.md +21 -9
  69. package/src/core/code-flow/specs/shared/prd-template.md +14 -3
  70. package/src/migrate/spec-workflow.js +238 -0
  71. package/src/adapters/claude/commands/cf-inject.md +0 -55
  72. package/src/adapters/codex/skills/cf-inject/SKILL.md +0 -56
  73. package/src/adapters/costrict/commands/cf-inject.md +0 -55
  74. package/src/adapters/opencode/commands/cf-inject.md +0 -59
  75. package/src/core/code-flow/scripts/cf_inject_hook.py +0 -178
  76. package/src/core/code-flow/scripts/cf_session_hook.py +0 -42
@@ -1,5 +1,14 @@
1
1
  ---
2
+ id: frontend-quality-standards
2
3
  description: 写前端代码时适用:类型、lint、错误处理、测试、状态管理质量约束
4
+ stages: [design, plan, code, review]
5
+ enforcement: required
6
+ verifiers:
7
+ - rule: RULE-frontend-quality-001
8
+ type: manual
9
+ config:
10
+ checklist: Confirm all Guidance and Avoid items for this Spec.
11
+ owner: project-owner
3
12
  ---
4
13
 
5
14
  # Frontend Quality Standards
@@ -27,6 +36,9 @@ render(data);
27
36
  ```
28
37
 
29
38
  ## Rules
39
+ - [RULE-frontend-quality-001] The implementation must satisfy every applicable item in Guidance and avoid every item in Avoid.
40
+
41
+ ## Guidance
30
42
  - TypeScript 项目禁止使用 `any`,未知类型用 `unknown` 并显式收敛
31
43
  - 组件统一使用函数组件(React)/ 组合式 API(Vue 3),禁止类组件新增
32
44
  - 关键交互(提交 / 删除 / 支付)必须有错误提示与 loading 状态
@@ -39,7 +51,7 @@ render(data);
39
51
  - 网络请求统一处理 401 / 403 / 5xx,避免每个调用方重复
40
52
  - 关键路径补端到端测试或组件交互测试
41
53
 
42
- ## Anti-Patterns
54
+ ## Avoid
43
55
  - 禁止在 render / setup 中发起未受控的副作用(用 `useEffect` / `onMounted`)
44
56
  - 禁止把后端错误直接抛给用户(如 SQL / stack trace)
45
57
  - 禁止用 `// @ts-ignore` 绕过类型错误,必须修复或改为 `@ts-expect-error` 加注释
@@ -90,19 +90,19 @@
90
90
 
91
91
  ### 2.4 验收条件 [必填]
92
92
 
93
- > 交互层面的可验证场景,写到可转 e2e / 组件交互测试断言的粒度(明确操作与可观测 UI 结果)。场景 ID 即测试用例来源。
93
+ > 交互层面的可验证场景,写到可转 E2E / 组件交互测试断言的粒度(明确操作与可观测 UI 结果)。场景 ID 即测试用例来源。测试层级只能填 `unit` / `integration` / `E2E` / `manual`;跨路由、服务请求、状态更新和最终 UI 的用户流程必须标为 `E2E`。关键真实边界列出不得 mock 的浏览器、路由、状态或服务边界。
94
94
 
95
95
  **正常场景**
96
96
 
97
- | 场景ID | 功能ID | 优先级 | 操作步骤 | 预期 UI 结果 |
98
- |--------|--------|--------|---------|-------------|
99
- | S-01 | FEAT-01 | P0 | 1. <操作> | <可见结果> |
97
+ | 场景ID | 功能ID | 优先级 | 测试层级 | 关键真实边界 | 操作步骤 | 预期 UI 结果 |
98
+ |--------|--------|--------|---------|-------------|---------|-------------|
99
+ | S-01 | FEAT-01 | P0 | E2E | Browser → Router → Service → UI | 1. <操作> | <可见结果> |
100
100
 
101
101
  **异常场景**
102
102
 
103
- | 场景ID | 功能ID | 触发条件 | UI 表现 |
104
- |--------|--------|---------|---------|
105
- | E-01 | FEAT-01 | (接口失败 / 空数据 / 校验不通过) | (错误提示 / 空态 / 字段定位) |
103
+ | 场景ID | 功能ID | 测试层级 | 关键真实边界 | 触发条件 | UI 表现 |
104
+ |--------|--------|---------|-------------|---------|---------|
105
+ | E-01 | FEAT-01 | integration | Service → UI | (接口失败 / 空数据 / 校验不通过) | (错误提示 / 空态 / 字段定位) |
106
106
 
107
107
  **非功能指标** [按需]
108
108
 
@@ -227,9 +227,19 @@
227
227
 
228
228
  ## 4. 风险与依赖 [按需]
229
229
 
230
- | 风险ID | 描述 | 影响 | 应对 |
231
- |--------|------|------|------|
232
- | | | | |
230
+ | 风险ID | 描述 | 影响 | 应对 | 验证场景 |
231
+ |--------|------|------|------|---------|
232
+ | | | | | S-/E-/B- 或手动验证理由 |
233
+
234
+ ---
235
+
236
+ ## Spec Compliance Matrix
237
+
238
+ > 从需求目录 `spec-context.yml` 继承并逐 Rule 回填。required Rule 必须有具体设计落点和 verifier/验收场景;N/A 只接受逐项用户确认。
239
+
240
+ | Spec/Rule | enforcement | 设计影响 | 设计落点 | 验证场景 | 状态/N/A 理由 |
241
+ |-----------|-------------|---------|---------|---------|----------------|
242
+ | `{spec-id}#RULE-{domain}-001` | required / advisory | <对方案/交互的影响> | §<heading> / <item-id> | S-/E-/B- + verifier | applied / confirmed N/A |
233
243
 
234
244
  ---
235
245
 
@@ -133,32 +133,34 @@
133
133
 
134
134
  #### 2.5.1 业务规则与约束
135
135
 
136
- | ID | 类型 | 描述 |
137
- |----|------|------|
138
- | RULE-01 | 业务规则 | |
139
- | RULE-02 | 系统约束 | 单次批量导入上限≤10000条 |
136
+ | ID | 类型 | 描述 | 验证场景 |
137
+ |----|------|------|---------|
138
+ | RULE-01 | 业务规则 | | S-01 |
139
+ | RULE-02 | 系统约束 | 单次批量导入上限≤10000条 | B-01 |
140
140
 
141
141
  #### 2.5.2 功能验收场景
142
142
 
143
- > 场景写到可转自动化测试断言的粒度(明确前置、输入、可观测输出)。场景 ID(S-/E-/B-)即测试用例来源,§6 追溯矩阵的 TC-XXX 直接引用这些场景 ID。
143
+ > 场景写到可转自动化测试断言的粒度(明确前置、输入、可观测输出)。场景 ID(S-/E-/B-)即测试用例来源,§6 追溯矩阵直接引用这些场景 ID。
144
+ >
145
+ > **测试层级**只能填 `unit` / `integration` / `E2E` / `manual`。跨 API、存储、运行时生成、渲染或用户可见结果等多个边界的场景必须标为 `E2E`;`manual` 仅限无法自动化的外部条件,并记录原因。**关键真实边界**列出测试中不得 mock 的组件或最终可观测面,编码阶段不得自行降级。
144
146
 
145
147
  **正常场景**
146
148
 
147
- | 场景ID | 功能ID | 优先级 | 前置条件 | 操作步骤 | 预期结果 |
148
- |--------|--------|--------|---------|---------|---------|
149
- | S-01 | FEAT-01 | P0 | | | |
149
+ | 场景ID | 功能ID | 优先级 | 测试层级 | 关键真实边界 | 前置条件 | 操作步骤 | 预期结果 |
150
+ |--------|--------|--------|---------|-------------|---------|---------|---------|
151
+ | S-01 | FEAT-01 | P0 | E2E | API → Store → 最终输出 | | | |
150
152
 
151
153
  **异常场景**
152
154
 
153
- | 场景ID | 功能ID | 触发条件 | 系统行为 | 用户感知 |
154
- |--------|--------|---------|---------|---------|
155
- | E-01 | FEAT-01 | | | |
155
+ | 场景ID | 功能ID | 测试层级 | 关键真实边界 | 触发条件 | 系统行为 | 用户感知 |
156
+ |--------|--------|---------|-------------|---------|---------|---------|
157
+ | E-01 | FEAT-01 | integration | Service → Store | | | |
156
158
 
157
159
  **边界场景** [按需]
158
160
 
159
- | 场景ID | 字段/条件 | 边界值 | 预期行为 |
160
- |--------|----------|--------|---------|
161
- | B-01 | | | |
161
+ | 场景ID | 测试层级 | 关键真实边界 | 字段/条件 | 边界值 | 预期行为 |
162
+ |--------|---------|-------------|----------|--------|---------|
163
+ | B-01 | unit | 校验函数 | | | |
162
164
 
163
165
  #### 2.5.3 非功能指标 [按需]
164
166
 
@@ -386,9 +388,9 @@ flowchart TD
386
388
 
387
389
  **风险 / 失效应对**(默认轻量表)
388
390
 
389
- | 风险ID | 失效模式 | 影响 | 应对措施 |
390
- |--------|---------|------|---------|
391
- | RISK-01 | | | |
391
+ | 风险ID | 失效模式 | 影响 | 应对措施 | 验证场景 |
392
+ |--------|---------|------|---------|---------|
393
+ | RISK-01 | | | | E-01 |
392
394
 
393
395
  > 仅安全关键 / 高并发 / 强一致系统才升级为 FMEA:追加 S/O/D(1-10)与 RPN=S×O×D 三列,RPN≥200 上线前必须解决。普通功能用上表即可,不要为打分而打分。
394
396
 
@@ -457,19 +459,29 @@ flowchart TD
457
459
 
458
460
  ### 5.2 风险识别
459
461
 
460
- | 风险ID | 类型 | 描述 | 概率 | 影响 | 应对措施 |
461
- |--------|------|------|------|------|---------|
462
- | | | | | | |
462
+ | 风险ID | 类型 | 描述 | 概率 | 影响 | 应对措施 | 验证场景 |
463
+ |--------|------|------|------|------|---------|---------|
464
+ | | | | | | | S-/E-/B- 或手动验证理由 |
463
465
 
464
466
  ---
465
467
 
466
468
  ## 6. 需求追溯矩阵
467
469
 
468
- | 用户故事 | 功能ID | 接口ID | 测试用例ID | 状态 |
469
- |---------|--------|--------|-----------|------|
470
- | US-01 | FEAT-01 | API-01 | TC-001 | ✅ |
470
+ | 用户故事 | 功能ID | 接口ID | 测试用例ID | 测试层级 | 状态 |
471
+ |---------|--------|--------|-----------|---------|------|
472
+ | US-01 | FEAT-01 | API-01 | S-01 | E2E | 待实现 |
471
473
 
472
- > 用户故事一列引用 PRD 中的 US-XX(无 PRD 时填"需求描述");测试用例 ID 列引用 §2.5.2 的场景 ID(S-/E-/B-),不要另造编号。
474
+ > 用户故事一列引用 PRD 中的 US-XX(无 PRD 时填"需求描述");测试用例 ID 列引用 §2.5.2 的场景 ID(S-/E-/B-),不要另造编号。RULE 与高影响 RISK 必须映射到场景;无法自动化时写明手动验证原因,不得留空。
475
+
476
+ ---
477
+
478
+ ## Spec Compliance Matrix
479
+
480
+ > 从需求目录 `spec-context.yml` 继承并逐 Rule 回填。required Rule 必须有具体设计落点和 verifier/验收场景;N/A 只接受逐项用户确认。
481
+
482
+ | Spec/Rule | enforcement | 设计影响 | 设计落点 | 验证场景 | 状态/N/A 理由 |
483
+ |-----------|-------------|---------|---------|---------|----------------|
484
+ | `{spec-id}#RULE-{domain}-001` | required / advisory | <对方案的影响> | §<heading> / <item-id> | S-/E-/B- + verifier | applied / confirmed N/A |
473
485
 
474
486
  ---
475
487
 
@@ -109,18 +109,20 @@
109
109
  #### 2.4.1 功能验收场景
110
110
 
111
111
  > 每个 P0/P1 FEAT 至少 1 条正常 + 1 条异常场景;"操作步骤/预期结果"写到可直接转为自动化测试断言的粒度(明确输入与可观测输出)。场景 ID(S-/E-/B-)即测试用例编号,供 plan 拆解引用。
112
+ >
113
+ > **测试层级**只能填 `unit` / `integration` / `E2E` / `manual`。跨 API、存储、运行时生成、渲染或用户可见结果等多个边界的场景必须标为 `E2E`;`manual` 仅限无法自动化的外部条件,并记录原因。**关键真实边界**列出测试中不得 mock 的组件或最终可观测面,编码阶段不得自行降级。
112
114
 
113
115
  **正常场景**
114
116
 
115
- | 场景ID | 功能ID | 优先级 | 操作步骤 | 预期结果 |
116
- |--------|--------|--------|---------|---------|
117
- | S-01 | FEAT-01 | P0 | 1. <步骤><br>2. <步骤> | <结果> |
117
+ | 场景ID | 功能ID | 优先级 | 测试层级 | 关键真实边界 | 操作步骤 | 预期结果 |
118
+ |--------|--------|--------|---------|-------------|---------|---------|
119
+ | S-01 | FEAT-01 | P0 | E2E | API → Store → 最终输出 | 1. <步骤><br>2. <步骤> | <结果> |
118
120
 
119
121
  **异常场景**
120
122
 
121
- | 场景ID | 功能ID | 触发条件 | 系统行为 |
122
- |--------|--------|---------|---------|
123
- | E-01 | FEAT-01 | | |
123
+ | 场景ID | 功能ID | 测试层级 | 关键真实边界 | 触发条件 | 系统行为 |
124
+ |--------|--------|---------|-------------|---------|---------|
125
+ | E-01 | FEAT-01 | integration | Service → Store | | |
124
126
 
125
127
  #### 2.4.2 非功能指标 [按需]
126
128
 
@@ -274,9 +276,19 @@ flowchart TD
274
276
 
275
277
  ### 4.2 风险识别
276
278
 
277
- | 风险ID | 描述 | 影响 | 应对措施 |
278
- |--------|------|------|---------|
279
- | | | | |
279
+ | 风险ID | 描述 | 影响 | 应对措施 | 验证场景 |
280
+ |--------|------|------|---------|---------|
281
+ | | | | | S-/E-/B- 或手动验证理由 |
282
+
283
+ ---
284
+
285
+ ## Spec Compliance Matrix
286
+
287
+ > 从需求目录 `spec-context.yml` 继承并逐 Rule 回填。required Rule 必须有具体设计落点和 verifier/验收场景;N/A 只接受逐项用户确认。
288
+
289
+ | Spec/Rule | enforcement | 设计影响 | 设计落点 | 验证场景 | 状态/N/A 理由 |
290
+ |-----------|-------------|---------|---------|---------|----------------|
291
+ | `{spec-id}#RULE-{domain}-001` | required / advisory | <对方案的影响> | §<heading> / <item-id> | S-/E-/B- + verifier | applied / confirmed N/A |
280
292
 
281
293
  ---
282
294
 
@@ -7,10 +7,10 @@
7
7
  > **状态**: 草稿 / 评审中 / 已批准 / 开发中 / 已上线
8
8
 
9
9
  **评审边界说明**:
10
- - **需求基线**: 第 2-6 章(背景/用户/功能/非功能/范围)→ 通过后锁定
11
- - **设计输入**: 第 3-6 章 → 为 `cf-task:align` 提供 US / FEAT / NFR / 范围
10
+ - **需求基线**: 第 2-8 章(背景/用户/功能/非功能/范围/依赖/已有规范约束)→ 通过后锁定
11
+ - **设计输入**: 第 3-8 章 → 为 `cf-task:align` 提供 US / FEAT / NFR / 范围 / Spec refs
12
12
 
13
- **ID 体系**: US(用户故事)、FEAT(功能)、NFR(非功能指标)
13
+ **ID 体系**: US(用户故事)、FEAT(功能)、NFR(非功能指标)、Spec/Rule(已有规范引用)
14
14
 
15
15
  **适用场景**: 需求早期阶段,在设计之前
16
16
  **不适用**: 已有明确技术方案(请直接用 cf-task:align)
@@ -34,6 +34,7 @@
34
34
  - [5. 非功能需求](#5-非功能需求-可选)
35
35
  - [6. 范围与边界](#6-范围与边界-必填)
36
36
  - [7. 依赖与风险](#7-依赖与风险-可选)
37
+ - [8. Existing Spec Constraints](#8-existing-spec-constraints)
37
38
  - [附录:术语表](#附录术语表)
38
39
 
39
40
  ---
@@ -172,6 +173,16 @@
172
173
 
173
174
  ---
174
175
 
176
+ ## 8. Existing Spec Constraints
177
+
178
+ > 仅列出 `stages` 含 `prd` 且已绑定到 `spec-context.yml` 的 Rule。required Rule 必须明确落到范围、FEAT 验收或 NFR;不适用必须逐条记录用户确认,不能由 Agent 自行填写 N/A。
179
+
180
+ | Spec/Rule | 约束 | 对范围/验收的影响 | 状态 |
181
+ |-----------|------|------------------|------|
182
+ | `{spec-id}#RULE-{domain}-001` | <规则摘要> | <In/Out Scope、FEAT-XX 验收或 NFR-XX> | applied / confirmed N/A |
183
+
184
+ ---
185
+
175
186
  ## 附录:术语表
176
187
 
177
188
  | 术语 | 定义 |
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+
3
+ const childProcess = require("node:child_process");
4
+ const crypto = require("node:crypto");
5
+ const fs = require("node:fs");
6
+ const path = require("node:path");
7
+
8
+ const PREFLIGHT = path.join(__dirname, "../core/code-flow/scripts/cf_spec_migrate.py");
9
+
10
+ function walkFiles(root, directory = root) {
11
+ const files = [];
12
+ if (!fs.existsSync(directory)) return files;
13
+ for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
14
+ const full = path.join(directory, entry.name);
15
+ const relative = path.relative(root, full);
16
+ if (entry.isDirectory() && relative === path.join(".code-flow", "migrations")) continue;
17
+ if (entry.isDirectory()) files.push(...walkFiles(root, full));
18
+ else files.push(relative);
19
+ }
20
+ return files.sort();
21
+ }
22
+
23
+ function migrationFiles(root) {
24
+ const entries = [".code-flow", ".claude", ".codex", ".costrict", ".opencode", ".agents", "AGENTS.md", "CLAUDE.md"];
25
+ const files = [];
26
+ for (const entry of entries) {
27
+ const target = path.join(root, entry);
28
+ if (!fs.existsSync(target)) continue;
29
+ if (fs.statSync(target).isDirectory()) files.push(...walkFiles(root, target));
30
+ else files.push(entry);
31
+ }
32
+ return [...new Set(files)].sort();
33
+ }
34
+
35
+ function hash(data) {
36
+ return crypto.createHash("sha256").update(data).digest("hex");
37
+ }
38
+
39
+ function sourceHashes(root) {
40
+ const result = {};
41
+ for (const relative of migrationFiles(root)) {
42
+ result[relative] = hash(fs.readFileSync(path.join(root, relative)));
43
+ }
44
+ return result;
45
+ }
46
+
47
+ function pythonCommand() {
48
+ return process.env.PYTHON || "python3";
49
+ }
50
+
51
+ function preview(root) {
52
+ const project = fs.realpathSync(root);
53
+ const result = childProcess.spawnSync(
54
+ pythonCommand(), [PREFLIGHT, "preflight", "--root", project, "--json"],
55
+ { encoding: "utf8", shell: false }
56
+ );
57
+ if (result.status !== 0) throw new Error(result.stderr || result.stdout || "preflight failed");
58
+ return JSON.parse(result.stdout);
59
+ }
60
+
61
+ function migrationId(root, hashes) {
62
+ const seed = `${root}\0${JSON.stringify(hashes)}\0${Date.now()}`;
63
+ return `spec-workflow-${hash(seed).slice(0, 12)}`;
64
+ }
65
+
66
+ function copyBackup(root, backup, hashes) {
67
+ for (const relative of Object.keys(hashes)) {
68
+ const target = path.join(backup, relative);
69
+ fs.mkdirSync(path.dirname(target), { recursive: true });
70
+ fs.copyFileSync(path.join(root, relative), target);
71
+ }
72
+ }
73
+
74
+ function writeJson(target, value) {
75
+ fs.writeFileSync(target, `${JSON.stringify(value, null, 2)}\n`, "utf8");
76
+ }
77
+
78
+ function prepare(root) {
79
+ const project = fs.realpathSync(root);
80
+ const plan = preview(project);
81
+ const hashes = sourceHashes(project);
82
+ const id = migrationId(project, hashes);
83
+ const workspace = path.join(project, ".code-flow", "migrations", id);
84
+ const backup = path.join(workspace, "backup");
85
+ const staging = path.join(workspace, "staging");
86
+ fs.mkdirSync(backup, { recursive: true });
87
+ fs.mkdirSync(staging, { recursive: true });
88
+ copyBackup(project, backup, hashes);
89
+ const preparedPlan = { ...plan, migration_id: id, project_root: project, source_hashes: hashes };
90
+ const status = plan.unresolved.length ? "prepared_blocked" : "prepared";
91
+ writeJson(path.join(workspace, "migration-plan.json"), preparedPlan);
92
+ writeJson(path.join(workspace, "journal.json"), { version: 1, migration_id: id, status, operations: [] });
93
+ return { migration_id: id, status, workspace, plan: path.join(workspace, "migration-plan.json") };
94
+ }
95
+
96
+ function readJson(target) {
97
+ return JSON.parse(fs.readFileSync(target, "utf8"));
98
+ }
99
+
100
+ function journalPaths(planPath) {
101
+ const workspace = path.dirname(planPath);
102
+ return { workspace, journal: path.join(workspace, "journal.json"), staging: path.join(workspace, "staging"), backup: path.join(workspace, "backup") };
103
+ }
104
+
105
+ function sameHashes(first, second) {
106
+ return JSON.stringify(first) === JSON.stringify(second);
107
+ }
108
+
109
+ function runStaging(planPath, locations) {
110
+ const result = childProcess.spawnSync(
111
+ pythonCommand(), [PREFLIGHT, "stage", "--plan", planPath, "--staging", locations.staging, "--json"],
112
+ { encoding: "utf8", shell: false }
113
+ );
114
+ if (result.status !== 0) throw new Error(result.stdout || result.stderr || "staging failed");
115
+ return JSON.parse(result.stdout);
116
+ }
117
+
118
+ function verifyStaging(staging, manifest) {
119
+ for (const item of manifest.targets) {
120
+ const target = path.join(staging, item.path);
121
+ if (!fs.existsSync(target) || hash(fs.readFileSync(target)) !== item.sha256) {
122
+ throw new Error(`staging hash mismatch: ${item.path}`);
123
+ }
124
+ }
125
+ }
126
+
127
+ function persistJournal(target, journal, status) {
128
+ journal.status = status;
129
+ writeJson(target, journal);
130
+ }
131
+
132
+ function targetEntries(manifest) {
133
+ return manifest.targets.filter((item) => item.path !== ".code-flow/.version");
134
+ }
135
+
136
+ function commitEntry(project, staging, item, journal, journalPath) {
137
+ const source = path.join(staging, item.path);
138
+ const target = path.join(project, item.path);
139
+ journal.operations.push({ path: item.path, status: "renaming" });
140
+ writeJson(journalPath, journal);
141
+ fs.mkdirSync(path.dirname(target), { recursive: true });
142
+ if (fs.existsSync(target)) fs.rmSync(target, { recursive: true, force: true });
143
+ fs.renameSync(source, target);
144
+ journal.operations[journal.operations.length - 1].status = "committed";
145
+ writeJson(journalPath, journal);
146
+ }
147
+
148
+ function obsoletePaths(plan, manifest) {
149
+ const targets = new Set(manifest.targets.map((item) => item.path));
150
+ return Object.keys(plan.source_hashes).filter((item) => item.startsWith(".code-flow/") && item !== ".code-flow/.version" && !targets.has(item));
151
+ }
152
+
153
+ function removeObsolete(project, paths) {
154
+ for (const relative of paths) fs.rmSync(path.join(project, relative), { recursive: true, force: true });
155
+ }
156
+
157
+ function restoreBackup(plan, locations, manifest) {
158
+ for (const item of manifest.targets || []) {
159
+ if (item.path !== ".code-flow/.version") fs.rmSync(path.join(plan.project_root, item.path), { recursive: true, force: true });
160
+ }
161
+ for (const relative of Object.keys(plan.source_hashes)) {
162
+ const source = path.join(locations.backup, relative);
163
+ const target = path.join(plan.project_root, relative);
164
+ fs.mkdirSync(path.dirname(target), { recursive: true });
165
+ fs.copyFileSync(source, target);
166
+ }
167
+ }
168
+
169
+ function rollback(planPath, error = "requested") {
170
+ const plan = readJson(planPath);
171
+ const locations = journalPaths(planPath);
172
+ const journal = readJson(locations.journal);
173
+ try {
174
+ restoreBackup(plan, locations, journal.manifest || { targets: [] });
175
+ journal.error = String(error);
176
+ persistJournal(locations.journal, journal, "rolled_back");
177
+ return { status: "rolled_back", migration_id: plan.migration_id };
178
+ } catch (restoreError) {
179
+ journal.error = String(restoreError);
180
+ persistJournal(locations.journal, journal, "recovery_required");
181
+ return { status: "recovery_required", migration_id: plan.migration_id };
182
+ }
183
+ }
184
+
185
+ function targetsMatch(project, manifest) {
186
+ return targetEntries(manifest).every((item) => {
187
+ const target = path.join(project, item.path);
188
+ return fs.existsSync(target) && hash(fs.readFileSync(target)) === item.sha256;
189
+ });
190
+ }
191
+
192
+ function finishVersion(plan, journal, journalPath) {
193
+ fs.writeFileSync(path.join(plan.project_root, ".code-flow/.version"), "0.6.0", "utf8");
194
+ persistJournal(journalPath, journal, "committed");
195
+ return { status: "committed", migration_id: plan.migration_id };
196
+ }
197
+
198
+ function recoverCommitting(plan, journal, locations, options) {
199
+ if (!targetsMatch(plan.project_root, journal.manifest)) return rollback(path.join(locations.workspace, "migration-plan.json"), "target hash mismatch");
200
+ if (options.interruptBeforeVersion) return { status: "committing", migration_id: plan.migration_id };
201
+ return finishVersion(plan, journal, locations.journal);
202
+ }
203
+
204
+ function apply(planPath, options = {}) {
205
+ const plan = readJson(planPath);
206
+ const locations = journalPaths(planPath);
207
+ const versionPath = path.join(plan.project_root, ".code-flow/.version");
208
+ if (fs.existsSync(versionPath) && fs.readFileSync(versionPath, "utf8").trim() === "0.6.0") return { status: "already_migrated", migration_id: plan.migration_id };
209
+ const journal = readJson(locations.journal);
210
+ if (journal.status === "committing") return recoverCommitting(plan, journal, locations, options);
211
+ if (plan.unresolved.length) throw new Error("migration plan has unresolved items");
212
+ if (!sameHashes(sourceHashes(plan.project_root), plan.source_hashes)) throw new Error("source hash drift");
213
+ try {
214
+ const manifest = runStaging(planPath, locations);
215
+ verifyStaging(locations.staging, manifest);
216
+ journal.manifest = manifest;
217
+ persistJournal(locations.journal, journal, "committing");
218
+ let completed = 0;
219
+ for (const item of targetEntries(manifest)) {
220
+ if (options.failAfter === completed) throw new Error(`injected rename failure ${completed}`);
221
+ if (options.exdevAfter === completed) {
222
+ const exdev = new Error("injected EXDEV");
223
+ exdev.code = "EXDEV";
224
+ throw exdev;
225
+ }
226
+ commitEntry(plan.project_root, locations.staging, item, journal, locations.journal);
227
+ completed += 1;
228
+ }
229
+ removeObsolete(plan.project_root, obsoletePaths(plan, manifest));
230
+ removeObsolete(plan.project_root, manifest.deletions || []);
231
+ if (options.interruptBeforeVersion) return { status: "committing", migration_id: plan.migration_id };
232
+ return finishVersion(plan, journal, locations.journal);
233
+ } catch (error) {
234
+ return rollback(planPath, error);
235
+ }
236
+ }
237
+
238
+ module.exports = { apply, preview, prepare, rollback, sourceHashes, walkFiles };
@@ -1,55 +0,0 @@
1
- # cf-inject
2
-
3
- 强制重新注入指定领域的编码规范(通常不需要手动调用)。
4
-
5
- ## 自动注入机制
6
-
7
- 本项目的规范注入是**全自动**的,通过两层机制保障:
8
-
9
- 1. **CLAUDE.md 指令**:Claude 根据问题自动判断领域,读取 `_map.md` 导航地图理解项目结构
10
- 2. **PreToolUse Hook**:编辑代码文件时根据文件路径标签自动注入匹配的约束 specs
11
-
12
- 两层职责分离:CLAUDE.md 负责导航(Tier 0),Hook 负责约束注入(Tier 1)。
13
-
14
- 正常情况下**无需手动调用**此命令。
15
-
16
- ## 何时使用
17
-
18
- - 需要强制刷新已注入的规范(如 spec 文件刚被修改)
19
- - 想要预览某个领域的完整规范内容
20
- - 自动注入未生效时的排查手段
21
- - Hook 标签匹配遗漏了某些 spec,需要手动补充
22
-
23
- ## 输入
24
-
25
- - `/project:cf-inject frontend` — 强制加载前端全部 specs
26
- - `/project:cf-inject backend` — 强制加载后端全部 specs
27
-
28
- ## 执行步骤
29
-
30
- 1. 用 Read 读取 `.code-flow/config.yml`,获取指定领域的 `specs` 列表
31
- 2. 提取所有 spec 的 `path` 字段(兼容新旧格式)
32
- 3. 按 tier 排序:tier 0(`_map.md`)在前,tier 1 按列表顺序
33
- 4. 用 Read 逐个读取 `.code-flow/specs/` 下的匹配文件
34
- 5. 将规范内容直接输出到对话中,格式:
35
-
36
- ```
37
- ## Active Specs (manual inject)
38
-
39
- ### Navigation (Retrieval Map)
40
-
41
- #### [domain]/_map.md
42
- [导航地图内容]
43
-
44
- ### Constraints (all specs for domain)
45
-
46
- #### [spec-path]
47
- [spec 内容]
48
-
49
- ---
50
- 以上规范是本次开发的约束条件,生成代码必须遵循。
51
- ```
52
-
53
- 6. 用 Write 更新 `.code-flow/.inject-state`,将该域所有 spec 路径标记为已注入,防止 Hook 重复注入
54
-
55
- > 注:手动 inject 会加载该域的**全部** spec,不做标签过滤。这是与 Hook 自动注入的区别——Hook 按标签精准匹配,手动注入用于全量预览或强制刷新。
@@ -1,56 +0,0 @@
1
- ---
2
- name: cf-inject
3
- description: Manually force-load all specs for a domain. Use when auto-injection hasn't triggered, you want to preview a domain's complete spec content, spec files were recently edited and need refresh, or hook tag matching missed some specs.
4
- ---
5
-
6
- ## 自动注入机制
7
-
8
- 本项目的规范注入是**全自动**的,通过两层机制保障:
9
-
10
- 1. **AGENTS.md 指令**:根据问题自动判断领域,读取 `_map.md` 导航地图理解项目结构
11
- 2. **UserPromptSubmit Hook**:每轮对话开始时,根据 prompt 中引用的文件路径标签自动注入匹配的约束 specs
12
-
13
- 两层职责分离:AGENTS.md 负责导航(Tier 0),Hook 负责约束注入(Tier 1)。
14
-
15
- 正常情况下**无需手动调用**此命令。
16
-
17
- ## 何时使用
18
-
19
- - 需要强制刷新已注入的规范(如 spec 文件刚被修改)
20
- - 想要预览某个领域的完整规范内容
21
- - 自动注入未生效时的排查手段(例如 prompt 中未明确引用文件路径)
22
- - Hook 标签匹配遗漏了某些 spec,需要手动补充
23
-
24
- ## 输入
25
-
26
- - `cf-inject frontend` — 强制加载前端全部 specs
27
- - `cf-inject backend` — 强制加载后端全部 specs
28
-
29
- ## 执行步骤
30
-
31
- 1. 读取 `.code-flow/config.yml`,获取指定领域的 `specs` 列表
32
- 2. 提取所有 spec 的 `path` 字段(兼容新旧格式)
33
- 3. 按 tier 排序:tier 0(`_map.md`)在前,tier 1 按列表顺序
34
- 4. 逐个读取 `.code-flow/specs/` 下的匹配文件
35
- 5. 将规范内容直接输出到对话中,格式:
36
-
37
- ```
38
- ## Active Specs (manual inject)
39
-
40
- ### Navigation (Retrieval Map)
41
-
42
- #### [domain]/_map.md
43
- [导航地图内容]
44
-
45
- ### Constraints (all specs for domain)
46
-
47
- #### [spec-path]
48
- [spec 内容]
49
-
50
- ---
51
- 以上规范是本次开发的约束条件,生成代码必须遵循。
52
- ```
53
-
54
- 6. 用 apply_patch 更新 `.code-flow/.inject-state`,将该域所有 spec 路径标记为已注入,防止 Hook 重复注入
55
-
56
- > 注:手动 inject 会加载该域的**全部** spec,不做标签过滤。这是与 Hook 自动注入的区别——UserPromptSubmit Hook 按 prompt 中的文件路径推断标签精准匹配,手动注入用于全量预览或强制刷新。