kimaki 0.26.0 → 0.27.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 (214) hide show
  1. package/dist/agent-model.e2e.test.js +25 -122
  2. package/dist/anthropic-account-identity.js +1 -0
  3. package/dist/anthropic-auth-plugin.js +1 -0
  4. package/dist/anthropic-auth-state.js +1 -0
  5. package/dist/bin.js +73 -19
  6. package/dist/cli-commands/maintenance.js +46 -7
  7. package/dist/cli-commands/send.js +24 -0
  8. package/dist/cli-commands/session.js +183 -8
  9. package/dist/cli-commands/task.js +13 -1
  10. package/dist/cli-parsing.test.js +59 -3
  11. package/dist/cli-runner.js +28 -4
  12. package/dist/cli-runner.test.js +27 -0
  13. package/dist/cli-send-thread.e2e.test.js +1 -1
  14. package/dist/cli.js +32 -0
  15. package/dist/commands/abort.js +5 -1
  16. package/dist/commands/agent.js +6 -8
  17. package/dist/commands/ask-question.js +21 -32
  18. package/dist/commands/ask-question.test.js +43 -1
  19. package/dist/commands/login.js +6 -0
  20. package/dist/commands/merge-worktree.js +36 -10
  21. package/dist/commands/model-variant.js +7 -1
  22. package/dist/commands/model.js +14 -8
  23. package/dist/commands/multioauth.js +1 -0
  24. package/dist/commands/new-worktree.js +170 -102
  25. package/dist/commands/user-command.js +6 -8
  26. package/dist/commands/worktrees.js +5 -13
  27. package/dist/config.js +16 -1
  28. package/dist/database.js +124 -3
  29. package/dist/db.js +22 -0
  30. package/dist/db.test.js +167 -1
  31. package/dist/discord-bot.js +34 -32
  32. package/dist/discord-command-registration.js +9 -1
  33. package/dist/discord-utils.js +13 -5
  34. package/dist/discord-utils.test.js +17 -1
  35. package/dist/errors.js +5 -0
  36. package/dist/exec-async.js +17 -5
  37. package/dist/external-directory-permissions.e2e.test.js +2 -2
  38. package/dist/file-edit-log.js +197 -0
  39. package/dist/file-edit-log.test.js +258 -0
  40. package/dist/format-tables.js +52 -0
  41. package/dist/format-tables.test.js +93 -0
  42. package/dist/gateway-proxy-reconnect.e2e.test.js +28 -8
  43. package/dist/gateway-proxy.e2e.test.js +5 -5
  44. package/dist/git-worktree-core.js +182 -17
  45. package/dist/ipc-tools-plugin.js +63 -3
  46. package/dist/kimaki-opencode-plugin-loading.e2e.test.js +122 -58
  47. package/dist/kimaki-opencode-plugin.js +7 -3
  48. package/dist/kimaki-workspace-adaptor.js +33 -10
  49. package/dist/logger.js +10 -1
  50. package/dist/markdown.js +3 -1
  51. package/dist/markdown.test.js +11 -3
  52. package/dist/message-formatting.js +18 -7
  53. package/dist/message-formatting.test.js +56 -9
  54. package/dist/oauth-rotation-shared.js +10 -0
  55. package/dist/openai-auth-plugin.js +1 -0
  56. package/dist/openai-auth-state.js +1 -0
  57. package/dist/opencode-command.test.js +60 -0
  58. package/dist/opencode.js +53 -12
  59. package/dist/question-abort-clears-dropdown.e2e.test.js +89 -0
  60. package/dist/question-text-before-dropdown.e2e.test.js +52 -0
  61. package/dist/queue-advanced-abort.e2e.test.js +1 -1
  62. package/dist/queue-advanced-action-buttons.e2e.test.js +3 -3
  63. package/dist/queue-advanced-e2e-setup.js +78 -5
  64. package/dist/queue-advanced-footer.e2e.test.js +40 -246
  65. package/dist/queue-advanced-model-switch.e2e.test.js +2 -2
  66. package/dist/queue-advanced-permissions-typing.e2e.test.js +7 -9
  67. package/dist/queue-advanced-question.e2e.test.js +31 -26
  68. package/dist/queue-advanced-typing-interrupt.e2e.test.js +2 -2
  69. package/dist/queue-delete-message.e2e.test.js +1 -1
  70. package/dist/queue-drain-after-interactive-ui.e2e.test.js +2 -2
  71. package/dist/queue-interrupt-drain.e2e.test.js +112 -2
  72. package/dist/queue-question-select-drain.e2e.test.js +30 -128
  73. package/dist/runtime-lifecycle.e2e.test.js +10 -37
  74. package/dist/schema.js +34 -0
  75. package/dist/session-handler/event-stream-state.js +267 -50
  76. package/dist/session-handler/event-stream-state.test.js +415 -8
  77. package/dist/session-handler/model-utils.js +159 -5
  78. package/dist/session-handler/model-utils.test.js +345 -0
  79. package/dist/session-handler/thread-session-runtime.js +252 -182
  80. package/dist/session-sleep.test.js +224 -0
  81. package/dist/session-title-rename.test.js +7 -39
  82. package/dist/sleep.e2e.test.js +491 -0
  83. package/dist/startup-time.e2e.test.js +2 -1
  84. package/dist/store.js +3 -0
  85. package/dist/system-message.js +59 -6
  86. package/dist/system-message.test.js +76 -6
  87. package/dist/task-runner.js +94 -4
  88. package/dist/task-schedule.js +56 -0
  89. package/dist/task-schedule.test.js +71 -1
  90. package/dist/thread-message-queue.e2e.test.js +10 -169
  91. package/dist/undo-redo.e2e.test.js +2 -2
  92. package/dist/voice-attachment.js +23 -1
  93. package/dist/voice-message.e2e.test.js +10 -10
  94. package/dist/voice.test.js +55 -29
  95. package/dist/worktree-clone-isolation.e2e.test.js +188 -0
  96. package/dist/worktree-lifecycle.e2e.test.js +29 -10
  97. package/dist/worktrees.js +226 -443
  98. package/dist/worktrees.test.js +435 -58
  99. package/dist/xai-auth-plugin.js +1 -0
  100. package/dist/xai-auth-state.js +1 -0
  101. package/package.json +8 -6
  102. package/skills/errore/SKILL.md +18 -0
  103. package/skills/goke/SKILL.md +150 -10
  104. package/skills/holocron/SKILL.md +37 -0
  105. package/skills/npm-package/SKILL.md +55 -35
  106. package/skills/profano/SKILL.md +17 -2
  107. package/skills/spiceflow/SKILL.md +39 -3
  108. package/skills/strada/SKILL.md +39 -0
  109. package/skills/zele/SKILL.md +19 -0
  110. package/src/agent-model.e2e.test.ts +25 -151
  111. package/src/anthropic-account-identity.ts +1 -0
  112. package/src/anthropic-auth-plugin.ts +1 -0
  113. package/src/anthropic-auth-state.ts +1 -0
  114. package/src/bin.ts +78 -26
  115. package/src/cli-commands/maintenance.ts +59 -13
  116. package/src/cli-commands/send.ts +25 -0
  117. package/src/cli-commands/session.ts +203 -9
  118. package/src/cli-commands/task.ts +13 -1
  119. package/src/cli-parsing.test.ts +69 -3
  120. package/src/cli-runner.test.ts +49 -0
  121. package/src/cli-runner.ts +28 -4
  122. package/src/cli-send-thread.e2e.test.ts +1 -1
  123. package/src/cli.ts +53 -0
  124. package/src/commands/abort.ts +6 -1
  125. package/src/commands/agent.ts +6 -6
  126. package/src/commands/ask-question.test.ts +48 -1
  127. package/src/commands/ask-question.ts +21 -32
  128. package/src/commands/login.ts +6 -0
  129. package/src/commands/merge-worktree.ts +43 -12
  130. package/src/commands/model-variant.ts +8 -1
  131. package/src/commands/model.ts +18 -8
  132. package/src/commands/multioauth.ts +1 -0
  133. package/src/commands/new-worktree.ts +138 -55
  134. package/src/commands/user-command.ts +6 -6
  135. package/src/commands/worktrees.ts +5 -12
  136. package/src/config.ts +18 -1
  137. package/src/database.ts +195 -3
  138. package/src/db.test.ts +188 -0
  139. package/src/db.ts +26 -0
  140. package/src/discord-bot.ts +35 -29
  141. package/src/discord-command-registration.ts +15 -1
  142. package/src/discord-utils.test.ts +19 -0
  143. package/src/discord-utils.ts +21 -6
  144. package/src/errors.ts +5 -0
  145. package/src/exec-async.ts +20 -11
  146. package/src/external-directory-permissions.e2e.test.ts +2 -2
  147. package/src/file-edit-log.test.ts +287 -0
  148. package/src/file-edit-log.ts +283 -0
  149. package/src/format-tables.test.ts +101 -0
  150. package/src/format-tables.ts +75 -0
  151. package/src/gateway-proxy-reconnect.e2e.test.ts +38 -8
  152. package/src/gateway-proxy.e2e.test.ts +5 -5
  153. package/src/git-worktree-core.ts +247 -20
  154. package/src/ipc-tools-plugin.ts +75 -5
  155. package/src/kimaki-opencode-plugin-loading.e2e.test.ts +188 -97
  156. package/src/kimaki-opencode-plugin.ts +7 -3
  157. package/src/kimaki-workspace-adaptor.ts +40 -11
  158. package/src/logger.ts +13 -4
  159. package/src/markdown.test.ts +13 -3
  160. package/src/markdown.ts +4 -1
  161. package/src/message-formatting.test.ts +86 -12
  162. package/src/message-formatting.ts +19 -7
  163. package/src/oauth-rotation-shared.ts +10 -0
  164. package/src/openai-auth-plugin.ts +1 -0
  165. package/src/openai-auth-state.ts +1 -0
  166. package/src/opencode-command.test.ts +81 -0
  167. package/src/opencode.ts +71 -11
  168. package/src/question-abort-clears-dropdown.e2e.test.ts +109 -0
  169. package/src/question-text-before-dropdown.e2e.test.ts +67 -0
  170. package/src/queue-advanced-abort.e2e.test.ts +1 -1
  171. package/src/queue-advanced-action-buttons.e2e.test.ts +3 -3
  172. package/src/queue-advanced-e2e-setup.ts +83 -4
  173. package/src/queue-advanced-footer.e2e.test.ts +47 -294
  174. package/src/queue-advanced-model-switch.e2e.test.ts +2 -2
  175. package/src/queue-advanced-permissions-typing.e2e.test.ts +7 -9
  176. package/src/queue-advanced-question.e2e.test.ts +35 -29
  177. package/src/queue-advanced-typing-interrupt.e2e.test.ts +2 -2
  178. package/src/queue-delete-message.e2e.test.ts +1 -1
  179. package/src/queue-drain-after-interactive-ui.e2e.test.ts +2 -2
  180. package/src/queue-interrupt-drain.e2e.test.ts +123 -2
  181. package/src/queue-question-select-drain.e2e.test.ts +32 -157
  182. package/src/runtime-lifecycle.e2e.test.ts +10 -41
  183. package/src/schema.sql +14 -1
  184. package/src/schema.ts +36 -0
  185. package/src/session-handler/event-stream-state.test.ts +437 -7
  186. package/src/session-handler/event-stream-state.ts +389 -53
  187. package/src/session-handler/model-utils.test.ts +415 -0
  188. package/src/session-handler/model-utils.ts +249 -5
  189. package/src/session-handler/thread-session-runtime.ts +300 -215
  190. package/src/session-sleep.test.ts +294 -0
  191. package/src/session-title-rename.test.ts +9 -48
  192. package/src/sleep.e2e.test.ts +562 -0
  193. package/src/startup-time.e2e.test.ts +5 -1
  194. package/src/store.ts +21 -0
  195. package/src/system-message.test.ts +86 -6
  196. package/src/system-message.ts +66 -7
  197. package/src/task-runner.ts +136 -2
  198. package/src/task-schedule.test.ts +76 -0
  199. package/src/task-schedule.ts +95 -0
  200. package/src/thread-message-queue.e2e.test.ts +10 -202
  201. package/src/undo-redo.e2e.test.ts +2 -2
  202. package/src/voice-attachment.ts +29 -1
  203. package/src/voice-message.e2e.test.ts +10 -10
  204. package/src/voice.test.ts +63 -29
  205. package/src/worktree-clone-isolation.e2e.test.ts +213 -0
  206. package/src/worktree-lifecycle.e2e.test.ts +29 -9
  207. package/src/worktrees.test.ts +505 -64
  208. package/src/worktrees.ts +257 -603
  209. package/src/xai-auth-plugin.ts +1 -0
  210. package/src/xai-auth-state.ts +1 -0
  211. package/dist/message-finish-field.e2e.test.js +0 -169
  212. package/dist/subagent-rate-limit-plugin.js +0 -184
  213. package/src/message-finish-field.e2e.test.ts +0 -196
  214. package/src/subagent-rate-limit-plugin.ts +0 -228
@@ -5,17 +5,29 @@ import fs from 'node:fs'
5
5
  import path from 'node:path'
6
6
  import { describe, expect, test } from 'vitest'
7
7
  import {
8
- buildSubmoduleReferencePlan,
9
- createWorktreeWithSubmodules,
10
8
  execAsync,
11
9
  getManagedWorktreeDirectory,
10
+ formatMergeWorktreeError,
12
11
  mergeWorktree,
13
- parseGitmodulesFileContent,
14
12
  parseGitWorktreeListPorcelain,
15
13
  resolveSessionWorkingDirectory,
14
+ validateBranchRef,
16
15
  } from './worktrees.js'
17
- import { parseGitmodulesFileContent as parseCoreGitmodulesFileContent } from './git-worktree-core.js'
18
- import { TargetDirtyWorktreeError } from './errors.js'
16
+ import {
17
+ createWorktreeCore,
18
+ parseGitmodulesFileContent as parseCoreGitmodulesFileContent,
19
+ removeWorktreeCore,
20
+ removeWorktreeFromOwnRepository,
21
+ resolveGitCommit,
22
+ resolveGitCommonDirectory,
23
+ validateWorktreeIdentity,
24
+ } from './git-worktree-core.js'
25
+ import {
26
+ GitCommandError,
27
+ PushError,
28
+ RebaseConflictError,
29
+ TargetDirtyWorktreeError,
30
+ } from './errors.js'
19
31
  import {
20
32
  formatAutoWorktreeName,
21
33
  formatWorktreeName,
@@ -51,33 +63,24 @@ function createTestRoot(): string {
51
63
  return fs.mkdtempSync(path.join(tmpRoot, 'worktrees-test-'))
52
64
  }
53
65
 
54
- describe('worktrees', () => {
55
- test('parseGitmodulesFileContent parses paths and urls', () => {
56
- const parsed = parseGitmodulesFileContent(`
57
- [submodule "errore"]
58
- path = errore
59
- url = https://github.com/remorses/errore.git
60
- [submodule "gateway-proxy"]
61
- path = gateway-proxy
62
- url = https://github.com/remorses/gateway-proxy.git
63
- `)
64
-
65
- expect(parsed).toMatchInlineSnapshot(`
66
- [
67
- {
68
- "name": "errore",
69
- "path": "errore",
70
- "url": "https://github.com/remorses/errore.git",
71
- },
72
- {
73
- "name": "gateway-proxy",
74
- "path": "gateway-proxy",
75
- "url": "https://github.com/remorses/gateway-proxy.git",
76
- },
77
- ]
78
- `)
66
+ async function createTestWorktree({
67
+ directory,
68
+ name,
69
+ }: {
70
+ directory: string
71
+ name: string
72
+ }) {
73
+ const baseCommit = await resolveGitCommit({ directory, ref: 'HEAD' })
74
+ if (baseCommit instanceof Error) return baseCommit
75
+ return createWorktreeCore({
76
+ projectDirectory: directory,
77
+ targetDirectory: getManagedWorktreeDirectory({ directory, name }),
78
+ branchName: name,
79
+ baseCommit,
79
80
  })
81
+ }
80
82
 
83
+ describe('worktrees', () => {
81
84
  test('plugin-safe gitmodules parser handles standard indented entries', () => {
82
85
  const parsed = parseCoreGitmodulesFileContent(`
83
86
  [submodule "errore"]
@@ -96,36 +99,7 @@ describe('worktrees', () => {
96
99
  `)
97
100
  })
98
101
 
99
- test('buildSubmoduleReferencePlan uses local references when available', () => {
100
- const sourceDirectory = '/repo'
101
- const plan = buildSubmoduleReferencePlan({
102
- sourceDirectory,
103
- submodulePaths: ['errore', 'gateway-proxy', 'traforo'],
104
- existingSourceSubmoduleDirectories: new Set([
105
- '/repo/errore',
106
- '/repo/gateway-proxy',
107
- ]),
108
- })
109
-
110
- expect(plan).toMatchInlineSnapshot(`
111
- [
112
- {
113
- "path": "errore",
114
- "referenceDirectory": "/repo/errore",
115
- },
116
- {
117
- "path": "gateway-proxy",
118
- "referenceDirectory": "/repo/gateway-proxy",
119
- },
120
- {
121
- "path": "traforo",
122
- "referenceDirectory": null,
123
- },
124
- ]
125
- `)
126
- })
127
-
128
- test('createWorktreeWithSubmodules resolves local-only submodule commits from local source checkout', async () => {
102
+ test('worktree creation resolves local-only submodule commits from local source checkout', async () => {
129
103
  const sandbox = createTestRoot()
130
104
  const submoduleRemote = path.join(sandbox, 'errore-remote.git')
131
105
  const submoduleLocal = path.join(sandbox, 'errore-local')
@@ -210,7 +184,7 @@ describe('worktrees', () => {
210
184
  args: ['commit', '-m', 'pin local-only submodule commit'],
211
185
  })
212
186
 
213
- const worktreeResult = await createWorktreeWithSubmodules({
187
+ const worktreeResult = await createTestWorktree({
214
188
  directory: parentRepo,
215
189
  name: worktreeName,
216
190
  })
@@ -224,13 +198,28 @@ describe('worktrees', () => {
224
198
  cwd: path.join(worktreeResult.directory, 'errore'),
225
199
  args: ['rev-parse', 'HEAD'],
226
200
  })
227
-
201
+ const parentCommonDirectory = await resolveGitCommonDirectory({
202
+ directory: parentRepo,
203
+ })
204
+ if (parentCommonDirectory instanceof Error) {
205
+ throw parentCommonDirectory
206
+ }
207
+ const submoduleCommonDirectory = await resolveGitCommonDirectory({
208
+ directory: path.join(worktreeResult.directory, 'errore'),
209
+ })
210
+ if (submoduleCommonDirectory instanceof Error) {
211
+ throw submoduleCommonDirectory
212
+ }
228
213
  expect({
229
214
  localOnlyShaLength: localOnlySha.length,
230
215
  worktreeSubmoduleShaLength: worktreeSubmoduleSha.length,
231
216
  sameCommit: localOnlySha === worktreeSubmoduleSha,
217
+ belongsToParentClone: !path
218
+ .relative(parentCommonDirectory, submoduleCommonDirectory)
219
+ .startsWith(`..${path.sep}`),
232
220
  }).toMatchInlineSnapshot(`
233
221
  {
222
+ "belongsToParentClone": true,
234
223
  "localOnlyShaLength": 40,
235
224
  "sameCommit": true,
236
225
  "worktreeSubmoduleShaLength": 40,
@@ -249,7 +238,7 @@ describe('worktrees', () => {
249
238
  }
250
239
  })
251
240
 
252
- test('createWorktreeWithSubmodules uses current HEAD even when origin does not have the commit', async () => {
241
+ test('worktree creation uses current HEAD even when origin does not have the commit', async () => {
253
242
  const sandbox = createTestRoot()
254
243
  const parentRemote = path.join(sandbox, 'parent-remote.git')
255
244
  const parentLocal = path.join(sandbox, 'parent-local')
@@ -287,7 +276,7 @@ describe('worktrees', () => {
287
276
  args: ['rev-parse', 'origin/main'],
288
277
  })
289
278
 
290
- const worktreeResult = await createWorktreeWithSubmodules({
279
+ const worktreeResult = await createTestWorktree({
291
280
  directory: parentLocal,
292
281
  name: worktreeName,
293
282
  })
@@ -330,6 +319,184 @@ describe('worktrees', () => {
330
319
  }
331
320
  })
332
321
 
322
+ test('worktree creation survives an unavailable submodule remote', async () => {
323
+ const sandbox = createTestRoot()
324
+ const submoduleRemote = path.join(sandbox, 'missing-remote.git')
325
+ const submoduleSource = path.join(sandbox, 'submodule-source')
326
+ const parentRepo = path.join(sandbox, 'parent')
327
+ const worktreeName = `opencode/kimaki-missing-submodule-${Date.now()}`
328
+
329
+ try {
330
+ await git({ cwd: sandbox, args: ['init', '--bare', '-b', 'main', submoduleRemote] })
331
+ await git({ cwd: sandbox, args: ['clone', submoduleRemote, submoduleSource] })
332
+ await git({ cwd: submoduleSource, args: ['config', 'user.email', 'kimaki-tests@example.com'] })
333
+ await git({ cwd: submoduleSource, args: ['config', 'user.name', 'Kimaki Tests'] })
334
+ fs.writeFileSync(path.join(submoduleSource, 'README.md'), 'submodule\n')
335
+ await git({ cwd: submoduleSource, args: ['add', 'README.md'] })
336
+ await git({ cwd: submoduleSource, args: ['commit', '-m', 'submodule'] })
337
+ await git({ cwd: submoduleSource, args: ['push', 'origin', 'HEAD:main'] })
338
+
339
+ await git({ cwd: sandbox, args: ['init', '-b', 'main', parentRepo] })
340
+ await git({ cwd: parentRepo, args: ['config', 'user.email', 'kimaki-tests@example.com'] })
341
+ await git({ cwd: parentRepo, args: ['config', 'user.name', 'Kimaki Tests'] })
342
+ fs.writeFileSync(path.join(parentRepo, 'README.md'), 'parent\n')
343
+ await git({ cwd: parentRepo, args: ['add', 'README.md'] })
344
+ await git({ cwd: parentRepo, args: ['commit', '-m', 'parent'] })
345
+ await git({
346
+ cwd: parentRepo,
347
+ args: [
348
+ '-c',
349
+ 'protocol.file.allow=always',
350
+ 'submodule',
351
+ 'add',
352
+ submoduleRemote,
353
+ 'missing',
354
+ ],
355
+ })
356
+ await git({ cwd: parentRepo, args: ['commit', '-am', 'add submodule'] })
357
+ await git({ cwd: parentRepo, args: ['submodule', 'deinit', '-f', '--all'] })
358
+ fs.rmSync(path.join(parentRepo, '.git', 'modules', 'missing'), {
359
+ recursive: true,
360
+ force: true,
361
+ })
362
+ fs.rmSync(submoduleRemote, { recursive: true, force: true })
363
+
364
+ const result = await createTestWorktree({
365
+ directory: parentRepo,
366
+ name: worktreeName,
367
+ })
368
+ if (result instanceof Error) throw result
369
+
370
+ expect({
371
+ directoryExists: fs.existsSync(result.directory),
372
+ submoduleUnavailable: !fs.existsSync(path.join(result.directory, 'missing', '.git')),
373
+ }).toMatchInlineSnapshot(`
374
+ {
375
+ "directoryExists": true,
376
+ "submoduleUnavailable": true,
377
+ }
378
+ `)
379
+ } finally {
380
+ const managedDirectory = getManagedWorktreeDirectory({
381
+ directory: parentRepo,
382
+ name: worktreeName,
383
+ })
384
+ if (fs.existsSync(parentRepo)) {
385
+ await git({
386
+ cwd: parentRepo,
387
+ args: ['worktree', 'remove', '--force', managedDirectory],
388
+ }).catch(() => '')
389
+ }
390
+ fs.rmSync(sandbox, { recursive: true, force: true })
391
+ }
392
+ })
393
+
394
+ test('worktree removal succeeds when its branch was already deleted', async () => {
395
+ const sandbox = createTestRoot()
396
+ const parentRepo = path.join(sandbox, 'parent')
397
+ const worktreeDirectory = path.join(sandbox, 'worktree')
398
+ const branchName = 'opencode/kimaki-removed-branch'
399
+
400
+ try {
401
+ await git({ cwd: sandbox, args: ['init', '-b', 'main', parentRepo] })
402
+ await git({ cwd: parentRepo, args: ['config', 'user.email', 'kimaki-tests@example.com'] })
403
+ await git({ cwd: parentRepo, args: ['config', 'user.name', 'Kimaki Tests'] })
404
+ fs.writeFileSync(path.join(parentRepo, 'README.md'), 'parent\n')
405
+ await git({ cwd: parentRepo, args: ['add', 'README.md'] })
406
+ await git({ cwd: parentRepo, args: ['commit', '-m', 'parent'] })
407
+ await git({
408
+ cwd: parentRepo,
409
+ args: ['worktree', 'add', '-b', branchName, worktreeDirectory],
410
+ })
411
+ await git({ cwd: worktreeDirectory, args: ['switch', '--detach'] })
412
+ await git({ cwd: parentRepo, args: ['branch', '-D', branchName] })
413
+
414
+ const result = await removeWorktreeCore({
415
+ projectDirectory: parentRepo,
416
+ worktreeDirectory,
417
+ branchName,
418
+ })
419
+ if (result instanceof Error) throw result
420
+
421
+ expect(fs.existsSync(worktreeDirectory)).toBe(false)
422
+ } finally {
423
+ fs.rmSync(sandbox, { recursive: true, force: true })
424
+ }
425
+ })
426
+
427
+ test('worktree identity mismatch is detected and cleaned from its actual clone', async () => {
428
+ const sandbox = createTestRoot()
429
+ const remote = path.join(sandbox, 'remote.git')
430
+ const requestedClone = path.join(sandbox, 'requested')
431
+ const otherClone = path.join(sandbox, 'other')
432
+ const worktreeDirectory = path.join(sandbox, 'wrong-worktree')
433
+ const branchName = 'opencode/kimaki-wrong-clone'
434
+
435
+ try {
436
+ await git({ cwd: sandbox, args: ['init', '--bare', '-b', 'main', remote] })
437
+ await git({ cwd: sandbox, args: ['clone', remote, otherClone] })
438
+ await git({ cwd: otherClone, args: ['config', 'user.email', 'kimaki-tests@example.com'] })
439
+ await git({ cwd: otherClone, args: ['config', 'user.name', 'Kimaki Tests'] })
440
+ fs.writeFileSync(path.join(otherClone, 'README.md'), 'old\n')
441
+ await git({ cwd: otherClone, args: ['add', 'README.md'] })
442
+ await git({ cwd: otherClone, args: ['commit', '-m', 'old'] })
443
+ await git({ cwd: otherClone, args: ['push', 'origin', 'HEAD:main'] })
444
+
445
+ await git({ cwd: sandbox, args: ['clone', remote, requestedClone] })
446
+ await git({ cwd: requestedClone, args: ['config', 'user.email', 'kimaki-tests@example.com'] })
447
+ await git({ cwd: requestedClone, args: ['config', 'user.name', 'Kimaki Tests'] })
448
+ fs.writeFileSync(path.join(requestedClone, 'requested.txt'), 'new\n')
449
+ await git({ cwd: requestedClone, args: ['add', 'requested.txt'] })
450
+ await git({ cwd: requestedClone, args: ['commit', '-m', 'requested'] })
451
+
452
+ const baseCommit = await git({
453
+ cwd: requestedClone,
454
+ args: ['rev-parse', 'HEAD'],
455
+ })
456
+ await git({
457
+ cwd: otherClone,
458
+ args: ['worktree', 'add', '-b', branchName, worktreeDirectory, 'HEAD'],
459
+ })
460
+
461
+ const validation = await validateWorktreeIdentity({
462
+ projectDirectory: requestedClone,
463
+ worktreeDirectory,
464
+ baseCommit,
465
+ })
466
+ expect(validation).toBeInstanceOf(Error)
467
+
468
+ const cleanup = await removeWorktreeFromOwnRepository({
469
+ worktreeDirectory,
470
+ branchName,
471
+ })
472
+ if (cleanup instanceof Error) throw cleanup
473
+ const otherWorktrees = await git({
474
+ cwd: otherClone,
475
+ args: ['worktree', 'list', '--porcelain'],
476
+ })
477
+ const branchExists = await execAsync(
478
+ `git show-ref --verify --quiet ${JSON.stringify(`refs/heads/${branchName}`)}`,
479
+ { cwd: otherClone },
480
+ )
481
+ .then(() => true)
482
+ .catch(() => false)
483
+
484
+ expect({
485
+ directoryRemoved: !fs.existsSync(worktreeDirectory),
486
+ registrationRemoved: !otherWorktrees.includes(worktreeDirectory),
487
+ branchRemoved: !branchExists,
488
+ }).toMatchInlineSnapshot(`
489
+ {
490
+ "branchRemoved": true,
491
+ "directoryRemoved": true,
492
+ "registrationRemoved": true,
493
+ }
494
+ `)
495
+ } finally {
496
+ fs.rmSync(sandbox, { recursive: true, force: true })
497
+ }
498
+ })
499
+
333
500
  test('mergeWorktree rejects dirty checked-out target before local push', async () => {
334
501
  const sandbox = createTestRoot()
335
502
  const parentRepo = path.join(sandbox, 'parent')
@@ -368,6 +535,280 @@ describe('worktrees', () => {
368
535
  }
369
536
  })
370
537
 
538
+ test('formatMergeWorktreeError includes git command, exit code, and stderr', () => {
539
+ const execErr = Object.assign(new Error('Command failed: git push'), {
540
+ stderr:
541
+ 'fatal: refusing to update checked out branch: refs/heads/main\nhint: set receive.denyCurrentBranch to updateInstead\n',
542
+ stdout: '',
543
+ code: 1,
544
+ })
545
+ const gitErr = new GitCommandError({
546
+ command:
547
+ 'git -C /wt push --receive-pack=git -c receive.denyCurrentBranch=updateInstead receive-pack /repo/.git HEAD:main',
548
+ cause: execErr,
549
+ })
550
+ const err = new PushError({ target: 'main', cause: gitErr })
551
+ expect(formatMergeWorktreeError(err)).toMatchInlineSnapshot(`
552
+ "Merge failed: Push to main failed
553
+ Worktree rebase succeeded. Local branch was not updated. This is not a push to origin.
554
+
555
+ \`\`\`
556
+ git -C /wt push --receive-pack=git -c receive.denyCurrentBranch=updateInstead receive-pack /repo/.git HEAD:main
557
+ exit 1
558
+
559
+ fatal: refusing to update checked out branch: refs/heads/main
560
+ hint: set receive.denyCurrentBranch to updateInstead
561
+ \`\`\`"
562
+ `)
563
+ })
564
+
565
+ test('formatMergeWorktreeError keeps timeout cause when git has no stderr', () => {
566
+ const timeoutErr = new Error(
567
+ 'Command timed out after 30000ms: git -C /wt push /repo/.git HEAD:main',
568
+ )
569
+ const gitErr = new GitCommandError({
570
+ command: 'git -C /wt push /repo/.git HEAD:main',
571
+ cause: timeoutErr,
572
+ })
573
+ const err = new PushError({ target: 'main', cause: gitErr })
574
+ expect(formatMergeWorktreeError(err)).toMatchInlineSnapshot(`
575
+ "Merge failed: Push to main failed
576
+ Worktree rebase succeeded. Local branch was not updated. This is not a push to origin.
577
+
578
+ \`\`\`
579
+ git -C /wt push /repo/.git HEAD:main
580
+
581
+ Command timed out after 30000ms: git -C /wt push /repo/.git HEAD:main
582
+ \`\`\`"
583
+ `)
584
+ })
585
+
586
+ test('formatMergeWorktreeError truncates to maxLength', () => {
587
+ const execErr = Object.assign(new Error('Command failed: git push'), {
588
+ stderr: 'fatal: ' + 'x'.repeat(200),
589
+ code: 1,
590
+ })
591
+ const gitErr = new GitCommandError({
592
+ command: 'git -C /wt push /repo/.git HEAD:main',
593
+ cause: execErr,
594
+ })
595
+ const formatted = formatMergeWorktreeError(
596
+ new PushError({ target: 'main', cause: gitErr }),
597
+ { maxLength: 80 },
598
+ )
599
+ expect(formatted.length).toBeLessThanOrEqual(80)
600
+ expect(formatted.endsWith('… [truncated]')).toBe(true)
601
+ })
602
+
603
+ test('mergeWorktree push failure includes git stderr in formatted error', async () => {
604
+ const sandbox = createTestRoot()
605
+ const parentRepo = path.join(sandbox, 'parent')
606
+ const worktreeDir = path.join(sandbox, 'feature-worktree')
607
+
608
+ try {
609
+ fs.mkdirSync(parentRepo, { recursive: true })
610
+ await git({ cwd: parentRepo, args: ['init', '-b', 'main'] })
611
+ await git({ cwd: parentRepo, args: ['config', 'user.email', 'kimaki-tests@example.com'] })
612
+ await git({ cwd: parentRepo, args: ['config', 'user.name', 'Kimaki Tests'] })
613
+ await git({
614
+ cwd: parentRepo,
615
+ args: ['config', 'core.hooksPath', path.join(parentRepo, '.git', 'hooks')],
616
+ })
617
+
618
+ fs.writeFileSync(path.join(parentRepo, 'README.md'), 'v1\n', 'utf-8')
619
+ await git({ cwd: parentRepo, args: ['add', 'README.md'] })
620
+ await git({ cwd: parentRepo, args: ['commit', '-m', 'init'] })
621
+
622
+ await git({ cwd: parentRepo, args: ['worktree', 'add', '-b', 'feature', worktreeDir] })
623
+ fs.writeFileSync(path.join(worktreeDir, 'feature.md'), 'feature\n', 'utf-8')
624
+ await git({ cwd: worktreeDir, args: ['add', 'feature.md'] })
625
+ await git({ cwd: worktreeDir, args: ['commit', '-m', 'feature'] })
626
+
627
+ const hookPath = path.join(parentRepo, '.git', 'hooks', 'pre-receive')
628
+ fs.writeFileSync(
629
+ hookPath,
630
+ '#!/bin/sh\ncat >/dev/null\necho "hook rejected the push" >&2\nexit 1\n',
631
+ 'utf-8',
632
+ )
633
+ fs.chmodSync(hookPath, 0o755)
634
+
635
+ const result = await mergeWorktree({
636
+ worktreeDir,
637
+ mainRepoDir: parentRepo,
638
+ worktreeName: 'feature',
639
+ targetBranch: 'main',
640
+ })
641
+
642
+ expect(result).toBeInstanceOf(PushError)
643
+ if (!(result instanceof PushError)) return
644
+ const formatted = formatMergeWorktreeError(result)
645
+ expect(formatted.startsWith('Merge failed: Push to main failed')).toBe(true)
646
+ expect(formatted).toContain('hook rejected the push')
647
+ expect(formatted).toContain('exit 1')
648
+ expect(formatted).toContain('git -C ')
649
+ } finally {
650
+ fs.rmSync(sandbox, { recursive: true, force: true })
651
+ }
652
+ })
653
+
654
+ test('mergeWorktree squash creates one target commit with the complete tree', async () => {
655
+ const sandbox = createTestRoot()
656
+ const parentRepo = path.join(sandbox, 'parent')
657
+ const worktreeDir = path.join(sandbox, 'feature-worktree')
658
+
659
+ try {
660
+ fs.mkdirSync(parentRepo, { recursive: true })
661
+ await git({ cwd: parentRepo, args: ['init', '-b', 'main'] })
662
+ await git({ cwd: parentRepo, args: ['config', 'user.email', 'kimaki-tests@example.com'] })
663
+ await git({ cwd: parentRepo, args: ['config', 'user.name', 'Kimaki Tests'] })
664
+
665
+ fs.writeFileSync(path.join(parentRepo, 'README.md'), 'base\n', 'utf-8')
666
+ await git({ cwd: parentRepo, args: ['add', 'README.md'] })
667
+ await git({ cwd: parentRepo, args: ['commit', '-m', 'init'] })
668
+
669
+ await git({ cwd: parentRepo, args: ['worktree', 'add', '-b', 'feature', worktreeDir] })
670
+ fs.writeFileSync(path.join(worktreeDir, 'first.md'), 'first\n', 'utf-8')
671
+ await git({ cwd: worktreeDir, args: ['add', 'first.md'] })
672
+ await git({ cwd: worktreeDir, args: ['commit', '-m', 'add first file'] })
673
+ fs.writeFileSync(path.join(worktreeDir, 'second.md'), 'second\n', 'utf-8')
674
+ await git({ cwd: worktreeDir, args: ['add', 'second.md'] })
675
+ await git({ cwd: worktreeDir, args: ['commit', '-m', 'add second file'] })
676
+
677
+ fs.writeFileSync(path.join(parentRepo, 'target.md'), 'target\n', 'utf-8')
678
+ await git({ cwd: parentRepo, args: ['add', 'target.md'] })
679
+ await git({ cwd: parentRepo, args: ['commit', '-m', 'advance target'] })
680
+ const targetBeforeMerge = await git({
681
+ cwd: parentRepo,
682
+ args: ['rev-parse', 'main'],
683
+ })
684
+
685
+ const result = await mergeWorktree({
686
+ worktreeDir,
687
+ mainRepoDir: parentRepo,
688
+ worktreeName: 'feature',
689
+ targetBranch: 'main',
690
+ strategy: 'squash',
691
+ })
692
+ if (result instanceof Error) throw result
693
+
694
+ const parent = await git({ cwd: parentRepo, args: ['rev-parse', 'main^'] })
695
+ expect({
696
+ sourceCommitCount: result.commitCount,
697
+ targetCommitCount: Number(
698
+ await git({
699
+ cwd: parentRepo,
700
+ args: ['rev-list', '--count', `${targetBeforeMerge}..main`],
701
+ }),
702
+ ),
703
+ parentMatchesTarget: parent === targetBeforeMerge,
704
+ subject: await git({ cwd: parentRepo, args: ['log', '-1', '--format=%s', 'main'] }),
705
+ files: (await git({ cwd: parentRepo, args: ['ls-tree', '-r', '--name-only', 'main'] })).split('\n'),
706
+ worktreeHead: await git({ cwd: worktreeDir, args: ['rev-parse', '--abbrev-ref', 'HEAD'] }),
707
+ featureBranchExists: await execAsync(
708
+ 'git show-ref --verify --quiet refs/heads/feature',
709
+ { cwd: parentRepo },
710
+ ).then(
711
+ () => true,
712
+ () => false,
713
+ ),
714
+ }).toMatchInlineSnapshot(`
715
+ {
716
+ "featureBranchExists": false,
717
+ "files": [
718
+ "README.md",
719
+ "first.md",
720
+ "second.md",
721
+ "target.md",
722
+ ],
723
+ "parentMatchesTarget": true,
724
+ "sourceCommitCount": 2,
725
+ "subject": "Merge worktree feature",
726
+ "targetCommitCount": 1,
727
+ "worktreeHead": "HEAD",
728
+ }
729
+ `)
730
+ } finally {
731
+ fs.rmSync(sandbox, { recursive: true, force: true })
732
+ }
733
+ })
734
+
735
+ test('mergeWorktree keeps reporting a paused rebase conflict on retry', async () => {
736
+ const sandbox = createTestRoot()
737
+ const parentRepo = path.join(sandbox, 'parent')
738
+ const worktreeDir = path.join(sandbox, 'feature-worktree')
739
+
740
+ try {
741
+ fs.mkdirSync(parentRepo, { recursive: true })
742
+ await git({ cwd: parentRepo, args: ['init', '-b', 'main'] })
743
+ await git({ cwd: parentRepo, args: ['config', 'user.email', 'kimaki-tests@example.com'] })
744
+ await git({ cwd: parentRepo, args: ['config', 'user.name', 'Kimaki Tests'] })
745
+ fs.writeFileSync(path.join(parentRepo, 'conflict.txt'), 'base\n', 'utf-8')
746
+ await git({ cwd: parentRepo, args: ['add', 'conflict.txt'] })
747
+ await git({ cwd: parentRepo, args: ['commit', '-m', 'init'] })
748
+
749
+ await git({ cwd: parentRepo, args: ['worktree', 'add', '-b', 'feature', worktreeDir] })
750
+ fs.writeFileSync(path.join(worktreeDir, 'conflict.txt'), 'feature\n', 'utf-8')
751
+ await git({ cwd: worktreeDir, args: ['commit', '-am', 'feature change'] })
752
+ fs.writeFileSync(path.join(parentRepo, 'conflict.txt'), 'target\n', 'utf-8')
753
+ await git({ cwd: parentRepo, args: ['commit', '-am', 'target change'] })
754
+
755
+ const first = await mergeWorktree({
756
+ worktreeDir,
757
+ mainRepoDir: parentRepo,
758
+ worktreeName: 'feature',
759
+ targetBranch: 'main',
760
+ strategy: 'squash',
761
+ })
762
+ const retry = await mergeWorktree({
763
+ worktreeDir,
764
+ mainRepoDir: parentRepo,
765
+ worktreeName: 'feature',
766
+ targetBranch: 'main',
767
+ strategy: 'squash',
768
+ })
769
+
770
+ expect({
771
+ first: first instanceof Error ? first.name : 'success',
772
+ retry: retry instanceof Error ? retry.name : 'success',
773
+ }).toMatchInlineSnapshot(`
774
+ {
775
+ "first": "RebaseConflictError",
776
+ "retry": "RebaseConflictError",
777
+ }
778
+ `)
779
+ expect(first).toBeInstanceOf(RebaseConflictError)
780
+ expect(retry).toBeInstanceOf(RebaseConflictError)
781
+ } finally {
782
+ await execAsync('git rebase --abort', { cwd: worktreeDir }).catch(() => undefined)
783
+ fs.rmSync(sandbox, { recursive: true, force: true })
784
+ }
785
+ })
786
+
787
+ test('validateBranchRef does not execute shell syntax from a branch value', async () => {
788
+ const sandbox = createTestRoot()
789
+ const marker = path.join(sandbox, 'injected')
790
+
791
+ try {
792
+ await git({ cwd: sandbox, args: ['init', '-b', 'main'] })
793
+ const result = await validateBranchRef({
794
+ directory: sandbox,
795
+ ref: `feature/$(touch ${marker})`,
796
+ })
797
+
798
+ expect({
799
+ rejected: result instanceof Error,
800
+ commandExecuted: fs.existsSync(marker),
801
+ }).toMatchInlineSnapshot(`
802
+ {
803
+ "commandExecuted": false,
804
+ "rejected": true,
805
+ }
806
+ `)
807
+ } finally {
808
+ fs.rmSync(sandbox, { recursive: true, force: true })
809
+ }
810
+ })
811
+
371
812
  test('shortenWorktreeSlug leaves short slugs alone', () => {
372
813
  expect(shortenWorktreeSlug('short-name')).toMatchInlineSnapshot(
373
814
  `"short-name"`,