clawt 2.20.0 → 3.0.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 (71) hide show
  1. package/README.md +10 -0
  2. package/dist/index.js +395 -133
  3. package/dist/postinstall.js +35 -6
  4. package/docs/alias.md +108 -0
  5. package/docs/completion.md +55 -0
  6. package/docs/config-file.md +43 -0
  7. package/docs/config.md +91 -0
  8. package/docs/create.md +85 -0
  9. package/docs/init.md +65 -0
  10. package/docs/list.md +67 -0
  11. package/docs/log.md +67 -0
  12. package/docs/merge.md +137 -0
  13. package/docs/notification.md +94 -0
  14. package/docs/projects.md +135 -0
  15. package/docs/remove.md +79 -0
  16. package/docs/reset.md +35 -0
  17. package/docs/resume.md +99 -0
  18. package/docs/run.md +146 -0
  19. package/docs/spec.md +156 -1905
  20. package/docs/status.md +155 -0
  21. package/docs/sync.md +114 -0
  22. package/docs/update-check.md +95 -0
  23. package/docs/validate.md +368 -0
  24. package/package.json +1 -1
  25. package/src/commands/alias.ts +1 -1
  26. package/src/commands/create.ts +10 -5
  27. package/src/commands/init.ts +75 -0
  28. package/src/commands/list.ts +1 -1
  29. package/src/commands/merge.ts +11 -4
  30. package/src/commands/remove.ts +10 -3
  31. package/src/commands/reset.ts +3 -0
  32. package/src/commands/resume.ts +1 -1
  33. package/src/commands/run.ts +9 -3
  34. package/src/commands/status.ts +1 -1
  35. package/src/commands/sync.ts +18 -6
  36. package/src/commands/validate.ts +46 -52
  37. package/src/constants/branch.ts +3 -0
  38. package/src/constants/config.ts +1 -1
  39. package/src/constants/index.ts +2 -2
  40. package/src/constants/messages/completion.ts +1 -1
  41. package/src/constants/messages/create.ts +3 -0
  42. package/src/constants/messages/index.ts +2 -0
  43. package/src/constants/messages/init.ts +18 -0
  44. package/src/constants/messages/remove.ts +2 -0
  45. package/src/constants/messages/sync.ts +3 -0
  46. package/src/constants/messages/validate.ts +6 -0
  47. package/src/constants/paths.ts +3 -0
  48. package/src/index.ts +2 -0
  49. package/src/types/command.ts +7 -1
  50. package/src/types/index.ts +2 -1
  51. package/src/types/projectConfig.ts +5 -0
  52. package/src/utils/config.ts +2 -1
  53. package/src/utils/git.ts +18 -0
  54. package/src/utils/index.ts +5 -0
  55. package/src/utils/json.ts +67 -0
  56. package/src/utils/project-config.ts +77 -0
  57. package/src/utils/validate-branch.ts +166 -0
  58. package/src/utils/worktree.ts +6 -2
  59. package/tests/unit/commands/create.test.ts +20 -16
  60. package/tests/unit/commands/init.test.ts +146 -0
  61. package/tests/unit/commands/merge.test.ts +7 -1
  62. package/tests/unit/commands/remove.test.ts +4 -0
  63. package/tests/unit/commands/reset.test.ts +2 -0
  64. package/tests/unit/commands/run.test.ts +2 -0
  65. package/tests/unit/commands/sync.test.ts +6 -0
  66. package/tests/unit/commands/validate.test.ts +13 -0
  67. package/tests/unit/utils/config.test.ts +2 -2
  68. package/tests/unit/utils/project-config.test.ts +136 -0
  69. package/tests/unit/utils/update-checker.test.ts +28 -7
  70. package/tests/unit/utils/validate-branch.test.ts +272 -0
  71. package/tests/unit/utils/worktree.test.ts +6 -0
package/docs/run.md ADDED
@@ -0,0 +1,146 @@
1
+ ### 5.2 批量创建 Worktree + 执行 Claude Code 任务
2
+
3
+ > **注意:** run 命令内部调用 `createWorktrees` 或 `createWorktreesByBranches`,因此验证分支的创建和主工作分支检测逻辑(包括工作区脏状态处理)**自动继承** create 命令的变更,无需额外修改 run 命令本身。
4
+
5
+ **命令:**
6
+
7
+ ```bash
8
+ # 方式一:通过 --tasks 参数直接指定任务(多任务并行,支持 variadic 语法)
9
+ clawt run -b <branchName> --tasks <task1> <task2> <task3>
10
+
11
+ # 方式二:通过 -f 从任务文件读取任务列表
12
+ clawt run -f <path>
13
+
14
+ # 方式三:不传 --tasks 也不传 -f,在 worktree 中打开 Claude Code 交互式界面
15
+ clawt run -b <branchName>
16
+ ```
17
+
18
+ **参数:**
19
+
20
+ | 参数 | 必填 | 说明 |
21
+ | --------- | ---- | ----------------------------------------------------------- |
22
+ | `-b` | 否 | 分支名(使用 `-f` 时可选,否则必填) |
23
+ | `--tasks` | 否 | 任务描述(可多次指定,每个 --tasks 对应一个任务,任务数量即 worktree 数量)。不传则在 worktree 中打开 Claude Code 交互式界面 |
24
+ | `-f` | 否 | 从任务文件读取任务列表(与 `--tasks` 互斥) |
25
+ | `-c` | 否 | 最大并发数,`0` 表示不限制 |
26
+ | `--dry-run` | 否 | 试运行模式,仅输出预览信息不实际执行 |
27
+
28
+ **互斥约束:**
29
+
30
+ - `--file` 和 `--tasks` **不能同时使用**
31
+ - 非 `-f` 模式必须指定 `-b`
32
+
33
+ **交互式 Claude Code 界面模式:**
34
+
35
+ 当不传 `--tasks` 也不传 `-f` 时,会创建单个 worktree,然后通过 `spawnSync` + `inherit stdio` 在该 worktree 中直接启动 Claude Code CLI 交互式界面,让用户与 Claude Code 直接交互。
36
+
37
+ 启动命令通过配置项 `claudeCodeCommand`(默认值 `claude`)指定,支持自定义命令及参数。
38
+
39
+ #### 任务文件格式
40
+
41
+ 任务文件使用嵌入 HTML 注释标签的自定义格式,不限制文件类型,标签外的任何文本都不会被解析。
42
+
43
+ ```markdown
44
+ 这里可以写任何说明文字,会被忽略
45
+
46
+ <!-- CLAWT-TASKS:START -->
47
+ # branch: feat-login
48
+ 实现用户登录功能
49
+ <!-- CLAWT-TASKS:END -->
50
+
51
+ <!-- CLAWT-TASKS:START -->
52
+ # branch: fix-bug
53
+ 修复内存泄漏问题
54
+ 这是多行任务描述
55
+ 可以写很多行
56
+ <!-- CLAWT-TASKS:END -->
57
+ ```
58
+
59
+ **格式规则:**
60
+
61
+ 1. **任务块界定**:每个任务用 `<!-- CLAWT-TASKS:START -->` 和 `<!-- CLAWT-TASKS:END -->` 包裹
62
+ 2. **分支名声明**:块内必须有一行 `# branch: <分支名>`(冒号前后的空格可灵活)
63
+ 3. **任务描述**:块内除分支名行以外的所有行,合并为任务描述(支持多行)
64
+ 4. **块外内容忽略**:标签外的任何文本都不会被解析
65
+ 5. **必填校验**:每个块必须包含任务描述;分支名默认必填,但使用 `-b` 参数时分支名为可选(会被忽略,用 `-b` 值自动编号)
66
+
67
+ **解析实现:** `src/utils/task-file.ts` 中的 `parseTaskFile()`、`loadTaskFile()` 和 `parseTasksFromOptions()` 函数,类型定义为 `TaskFileEntry`(`src/types/taskFile.ts`)。
68
+
69
+ #### 任务文件模式运行流程
70
+
71
+ 使用 `-f` 时的执行路径(`handleRun` → `handleRunFromFile`):
72
+
73
+ 1. 调用 `loadTaskFile(options.file)` 读取解析文件
74
+ 2. **有 `-b` 参数**:忽略文件中的分支名,用 `-b` 值自动编号创建 worktree(`createWorktrees(branch, count)`)
75
+ 3. **无 `-b` 参数**:使用文件中每个任务的独立分支名,先 `sanitizeBranchName` 清理后调用 `createWorktreesByBranches(branches)`
76
+ 4. 调用 `executeBatchTasks(worktrees, tasks, concurrency)` 执行
77
+
78
+ #### --tasks 模式运行流程
79
+
80
+ 1. 若传了 `--tasks`,解析得到任务数组 `tasks[]`;若未传,先检测分支是否已存在(已存在则提示使用 `clawt resume -b <branchName>` 恢复会话),然后创建单个 worktree 并启动 Claude Code 交互式界面(流程结束,不进入后续并行执行阶段)
81
+ 2. `n = tasks.length`
82
+ 3. 按照 **5.1** 的流程创建 `n` 个 worktree
83
+ 4. 通过公共函数 `executeBatchTasks`(`src/utils/task-executor.ts`)启动批量任务执行,该函数负责进度面板渲染、SIGINT 中断处理、并发控制和汇总输出。对每个 worktree 并行启动 Claude Code CLI:
84
+ ```bash
85
+ cd ~/.clawt/worktrees/<project>/<branchName>-<i>
86
+ claude -p "<tasks[i]>" --output-format stream-json --verbose --permission-mode bypassPermissions
87
+ ```
88
+ 使用 `stream-json` 格式可实时获取 Claude Code 的流式事件(工具调用、文本输出、最终结果),用于在进度面板中显示每个任务的实时活动描述和结果预览。流式事件解析由 `src/utils/stream-parser.ts` 负责。
89
+ 5. 进入**事件监听通知**阶段(见 [5.3](#53-任务完成通知机制))
90
+ 6. **中断处理(Ctrl+C / SIGINT)**
91
+ - 监听 `SIGINT` 信号,用户按下 Ctrl+C 时触发
92
+ - 向所有正在运行的 Claude Code 子进程发送 `SIGTERM` 终止信号
93
+ - 等待所有子进程退出后,进入清理流程:
94
+ - 如果 `autoDeleteBranch` 为 `true`:自动清理本次创建的所有 worktree 和对应分支
95
+ - 否则:交互式询问用户是否移除刚刚创建的 worktree 和对应分支
96
+ - 用户选择保留时,提示可使用 `clawt remove` 手动清理
97
+ - 清理完成后以退出码 `1` 退出
98
+
99
+ **注意:** 当 `n = 1` 时(只有一个任务),worktree 目录命名规则同 **5.1**(不加 `-1` 后缀)。
100
+
101
+ #### `--dry-run` 预览模式
102
+
103
+ 传入 `--dry-run` 时不实际创建 worktree 和执行任务,仅输出预览信息供用户确认。预览由 `printDryRunPreview()`(`src/utils/dry-run.ts`)负责渲染。
104
+
105
+ **输出格式:**
106
+
107
+ ```
108
+ ════════════════════════════════════════
109
+ Dry Run 预览
110
+ ════════════════════════════════════════
111
+ 任务数: 3 │ 并发数: 不限制 │ Worktree: ~/.clawt/worktrees/project
112
+ ────────────────────────────────────────
113
+ ✓ [1/3] feat-login
114
+ 路径: ~/.clawt/worktrees/project/feat-login
115
+ 任务: 实现登录功能
116
+
117
+ ⚠ [2/3] feat-signup — 分支 feat-signup 已存在
118
+ 路径: ~/.clawt/worktrees/project/feat-signup
119
+ 任务: 实现注册功能
120
+
121
+ ✓ [3/3] fix-bug
122
+ 路径: ~/.clawt/worktrees/project/fix-bug
123
+ 任务: 修复内存泄漏
124
+
125
+ ════════════════════════════════════════
126
+ ✓ 预览完成,无冲突。移除 --dry-run 即可正式执行。
127
+ ```
128
+
129
+ **格式规则:**
130
+
131
+ 1. **标题区**:双线分隔符包裹标题 `Dry Run 预览`
132
+ 2. **摘要行**:任务数、并发数、Worktree 目录路径合并为一行,用灰色 `│` 分隔;交互式模式(无 `--tasks`)会额外追加模式信息
133
+ 3. **分支列表**:
134
+ - 正常分支:行首绿色 `✓` + 序号 + 青色分支名
135
+ - 冲突分支:行首黄色 `⚠` + 序号 + 黄色分支名 + 灰色 `—` + 黄色警告文本(如 `分支 xxx 已存在`),警告合并在序号行
136
+ 4. **路径/任务行**:2 空格缩进,灰色标签前缀(`路径:` / `任务:`)
137
+ 5. **任务描述截断**:超过 80 字符时末尾加 `...`,多行合并为单行
138
+ 6. **结尾**:双线分隔符后根据冲突情况输出结论——无冲突时绿色 `✓` 提示,有冲突时黄色 `⚠` 警告
139
+
140
+ **实现要点:**
141
+
142
+ - 常量定义在 `src/constants/messages/run.ts`(`DRY_RUN_*` 系列)
143
+ - `DRY_RUN_WORKTREE_DIR` 前缀为 `Worktree:`(简短形式)
144
+ - `truncateTaskDesc()` 负责截断任务描述(最大长度 80 字符,定义在 `src/utils/dry-run.ts`)
145
+
146
+ ---