executor.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { mkdtempSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { describe, expect, it, vi } from 'vitest'
  5. import { Context } from 'cordis'
  6. import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
  7. import { BashTaskId } from '@deepseek-ai/dsh-bash'
  8. const spillDir = mkdtempSync(join(tmpdir(), 'dsh-bash-exec-spec-'))
  9. async function setup(config: ConstructorParameters<typeof LocalBashExecutor>[1] = {}) {
  10. const ctx = new Context()
  11. await ctx.plugin(LocalBashExecutor, config)
  12. const bash = ctx.bash as LocalBashExecutor
  13. bash.internals = { spillDir, graceMs: 200 }
  14. return { ctx, bash }
  15. }
  16. /** Poll until a pid no longer exists. */
  17. async function waitGone(pid: number, timeoutMs = 5_000): Promise<void> {
  18. const deadline = Date.now() + timeoutMs
  19. while (Date.now() < deadline) {
  20. try {
  21. process.kill(pid, 0)
  22. } catch {
  23. return
  24. }
  25. await new Promise(resolve => setTimeout(resolve, 20))
  26. }
  27. throw new Error(`pid ${pid} still alive after ${timeoutMs}ms`)
  28. }
  29. describe('LocalBashExecutor.run', () => {
  30. it('resolves with output and the effective timeout', async () => {
  31. const { bash } = await setup({ timeoutMs: 5_000 })
  32. const result = await bash.run(bash.resolve({ command: 'echo hi' }))
  33. expect(result.exitCode).toBe(0)
  34. expect(result.stdout.text).toBe('hi\n')
  35. expect(result.timeoutMs).toBe(5_000)
  36. })
  37. it('uses config cwd, overridable per call', async () => {
  38. const { bash } = await setup({ cwd: '/tmp' })
  39. const fromConfig = await bash.run(bash.resolve({ command: 'pwd' }))
  40. expect(fromConfig.stdout.text.trim()).toMatch(/\/tmp$/)
  41. const fromCall = await bash.run(bash.resolve({ command: 'pwd', workdir: '/' }))
  42. expect(fromCall.stdout.text.trim()).toBe('/')
  43. })
  44. it('defaults cwd to process.cwd()', async () => {
  45. const { bash } = await setup()
  46. const result = await bash.run(bash.resolve({ command: 'pwd' }))
  47. expect(result.stdout.text.trim()).toBe(process.cwd())
  48. })
  49. it('caps per-call timeouts at maxTimeoutMs', async () => {
  50. const { bash } = await setup({ timeoutMs: 1_000, maxTimeoutMs: 2_000 })
  51. const result = await bash.run(bash.resolve({ command: 'true', timeoutMs: 99_999 }))
  52. expect(result.timeoutMs).toBe(2_000)
  53. })
  54. it('rejects invalid numeric config and timeout overrides', async () => {
  55. await expect(setup({ timeoutMs: Number.NaN })).rejects.toThrow(/timeoutMs/)
  56. await expect(setup({ maxTimeoutMs: 0 })).rejects.toThrow(/maxTimeoutMs/)
  57. await expect(setup({ maxOutputBytes: -1 })).rejects.toThrow(/maxOutputBytes/)
  58. const { bash } = await setup()
  59. expect(() => bash.resolve({ command: 'true', timeoutMs: Number.NaN })).toThrow(/request\.timeoutMs/)
  60. expect(() => bash.resolve({ command: 'true', timeoutMs: -1 })).toThrow(/request\.timeoutMs/)
  61. })
  62. it('per-call timeout takes precedence under the cap and kills on expiry', async () => {
  63. const { bash } = await setup({ timeoutMs: 60_000 })
  64. const result = await bash.run(bash.resolve({ command: 'sleep 60', timeoutMs: 100 }))
  65. expect(result.timedOut).toBe(true)
  66. expect(result.timeoutMs).toBe(100)
  67. })
  68. it('propagates abort signals', async () => {
  69. const { bash } = await setup()
  70. const controller = new AbortController()
  71. const pending = bash.run(bash.resolve({ command: 'sleep 60', signal: controller.signal }))
  72. setTimeout(() => { controller.abort() }, 50)
  73. const result = await pending
  74. expect(result.aborted).toBe(true)
  75. })
  76. it('rejects on spawn failure (bad workdir)', async () => {
  77. const { bash } = await setup()
  78. await expect(bash.run(bash.resolve({ command: 'true', workdir: '/nonexistent-dsh' }))).rejects.toThrow(/ENOENT/)
  79. })
  80. })
  81. describe('LocalBashExecutor background tasks', () => {
  82. it('start returns immediately with a registered running task', async () => {
  83. const { bash } = await setup()
  84. const before = Date.now()
  85. const task = bash.start(bash.resolve({ command: 'sleep 0.2; echo done' }))
  86. expect(Date.now() - before).toBeLessThan(150)
  87. expect(task.status).toBe('running')
  88. expect(bash.get(task.id)).toBe(task)
  89. expect(bash.list()).toContain(task)
  90. await task.done
  91. expect(task.status).toBe('completed')
  92. expect(task.exitCode).toBe(0)
  93. })
  94. it('assigns sequential ids', async () => {
  95. const { bash } = await setup()
  96. const first = bash.start(bash.resolve({ command: 'true' }))
  97. const second = bash.start(bash.resolve({ command: 'true' }))
  98. expect(first.id).toBe('bash-1')
  99. expect(second.id).toBe('bash-2')
  100. await Promise.all([first.done, second.done])
  101. })
  102. it('readOutput returns increments without re-delivery', async () => {
  103. const { bash } = await setup()
  104. const task = bash.start(bash.resolve({ command: 'echo first; sleep 0.3; echo second' }))
  105. await new Promise(resolve => setTimeout(resolve, 150))
  106. const first = bash.readOutput(task.id)
  107. expect(first.delta).toBe('first\n')
  108. expect(first.lossy).toBe(false)
  109. await task.done
  110. const second = bash.readOutput(task.id)
  111. expect(second.delta).toBe('second\n')
  112. const third = bash.readOutput(task.id)
  113. expect(third.delta).toBe('')
  114. })
  115. it('readOutput marks stderr sections', async () => {
  116. const { bash } = await setup()
  117. const task = bash.start(bash.resolve({ command: 'echo out; echo err >&2' }))
  118. await task.done
  119. const read = bash.readOutput(task.id)
  120. expect(read.delta).toBe('out\n[stderr]\nerr\n')
  121. })
  122. it('readOutput reports stderr-only deltas without a leading newline', async () => {
  123. const { bash } = await setup()
  124. const task = bash.start(bash.resolve({ command: 'echo err >&2' }))
  125. await task.done
  126. expect(bash.readOutput(task.id).delta).toBe('[stderr]\nerr\n')
  127. })
  128. it('readOutput flags lossy reads and reports spill paths', async () => {
  129. const { bash } = await setup({ maxOutputBytes: 100 })
  130. const task = bash.start(bash.resolve({ command: 'for i in $(seq 1 100); do printf "line-%04d\\n" $i; done' }))
  131. await task.done
  132. const read = bash.readOutput(task.id)
  133. // Window slid past offset 0 → lossy, spill path points at the full stream.
  134. expect(read.lossy).toBe(true)
  135. expect(read.stdoutSpillPath).toBeDefined()
  136. })
  137. it('readOutput throws for unknown ids', async () => {
  138. const { bash } = await setup()
  139. expect(() => bash.readOutput(BashTaskId('nope'))).toThrow(/unknown bash task "nope"/)
  140. })
  141. it('kill terminates the process group and reports status killed', async () => {
  142. const { bash } = await setup()
  143. const task = bash.start(bash.resolve({ command: 'sleep 60' }))
  144. expect(bash.kill(task.id)).toBe(true)
  145. await task.done
  146. expect(task.status).toBe('killed')
  147. expect(task.signal).toBe('SIGTERM')
  148. })
  149. it('kill returns false for finished tasks and throws for unknown ids', async () => {
  150. const { bash } = await setup()
  151. const task = bash.start(bash.resolve({ command: 'true' }))
  152. await task.done
  153. expect(bash.kill(task.id)).toBe(false)
  154. expect(() => bash.kill(BashTaskId('nope'))).toThrow(/unknown bash task "nope"/)
  155. })
  156. it('notifies onTaskDone listeners on completion', async () => {
  157. const { bash } = await setup()
  158. const seen: [string, string][] = []
  159. bash.onTaskDone(task => void seen.push([task.id, task.status]))
  160. const task = bash.start(bash.resolve({ command: 'true' }))
  161. await task.done
  162. expect(seen).toEqual([[task.id, 'completed']])
  163. })
  164. it('notifies onTaskDone for killed tasks too', async () => {
  165. const { bash } = await setup()
  166. const listener = vi.fn()
  167. bash.onTaskDone(listener)
  168. const task = bash.start(bash.resolve({ command: 'sleep 60' }))
  169. bash.kill(task.id)
  170. await task.done
  171. expect(listener).toHaveBeenCalledWith(task)
  172. expect(task.status).toBe('killed')
  173. })
  174. it('marks tasks killed when the background spawn itself fails', async () => {
  175. const { bash } = await setup()
  176. const listener = vi.fn()
  177. bash.onTaskDone(listener)
  178. const task = bash.start(bash.resolve({ command: 'true', workdir: '/nonexistent-dsh' }))
  179. await task.done
  180. expect(task.status).toBe('killed')
  181. expect(listener).toHaveBeenCalledWith(task)
  182. expect(bash.readOutput(task.id).delta).toContain('spawn failed')
  183. })
  184. it('readOutput adds a separator only when stdout lacks a trailing newline', async () => {
  185. const { bash } = await setup()
  186. const task = bash.start(bash.resolve({ command: 'printf out; echo err >&2' }))
  187. await task.done
  188. expect(bash.readOutput(task.id).delta).toBe('out\n[stderr]\nerr\n')
  189. })
  190. it('readOutput reports stderr spill paths', async () => {
  191. const { bash } = await setup({ maxOutputBytes: 100 })
  192. const task = bash.start(bash.resolve({ command: 'for i in $(seq 1 100); do printf "line-%04d\\n" $i >&2; done' }))
  193. await task.done
  194. const read = bash.readOutput(task.id)
  195. expect(read.lossy).toBe(true)
  196. expect(read.stderrSpillPath).toBeDefined()
  197. expect(read.delta).toContain('[stderr]')
  198. })
  199. it('disposing with already-finished tasks only kills the running ones', async () => {
  200. const ctx = new Context()
  201. const fiber = await ctx.plugin(LocalBashExecutor, {})
  202. const bash = ctx.bash as LocalBashExecutor
  203. bash.internals = { spillDir, graceMs: 200 }
  204. const finished = bash.start(bash.resolve({ command: 'true' }))
  205. await finished.done
  206. const running = bash.start(bash.resolve({ command: 'sleep 60' }))
  207. await fiber.dispose()
  208. await running.done
  209. expect(finished.status).toBe('completed')
  210. expect(running.signal).toBe('SIGTERM')
  211. expect(bash.list()).toEqual([])
  212. })
  213. it('disposing the executor fiber kills running tasks (no orphans)', async () => {
  214. const ctx = new Context()
  215. const fiber = await ctx.plugin(LocalBashExecutor, {})
  216. const bash = ctx.bash as LocalBashExecutor
  217. bash.internals = { spillDir, graceMs: 200 }
  218. const listener = vi.fn()
  219. bash.onTaskDone(listener)
  220. const task = bash.start(bash.resolve({ command: 'sleep 60' }))
  221. const running = bash.get(task.id)!
  222. await new Promise(resolve => setTimeout(resolve, 50))
  223. // Grab the pid before dispose clears the registry.
  224. const pid = (running as unknown as { running: { pid: number } }).running.pid
  225. await fiber.dispose()
  226. await waitGone(pid)
  227. expect(bash.list()).toEqual([])
  228. // Listener silenced by base-class teardown — no late notifications.
  229. expect(listener).not.toHaveBeenCalled()
  230. })
  231. })
  232. describe('review fixes: lifecycle hardening', () => {
  233. it('start honors a pre-aborted or later-aborted AbortSignal', async () => {
  234. const { bash } = await setup()
  235. const controller = new AbortController()
  236. const task = bash.start(bash.resolve({ command: 'sleep 60', signal: controller.signal }))
  237. controller.abort()
  238. await task.done
  239. expect(task.status).toBe('killed')
  240. expect(task.signal).toBe('SIGTERM')
  241. })
  242. it('a throwing onTaskDone listener does not reject task.done or starve later listeners', async () => {
  243. const { bash } = await setup()
  244. const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined)
  245. const second = vi.fn()
  246. try {
  247. bash.onTaskDone(() => { throw new Error('listener bug') })
  248. bash.onTaskDone(second)
  249. const task = bash.start(bash.resolve({ command: 'true' }))
  250. await expect(task.done).resolves.toBeUndefined()
  251. expect(second).toHaveBeenCalledWith(task)
  252. expect(errorSpy).toHaveBeenCalled()
  253. } finally {
  254. errorSpy.mockRestore()
  255. }
  256. })
  257. it('dispose AWAITS a TERM-trapping process (SIGKILL escalation included)', async () => {
  258. const ctx = new Context()
  259. const fiber = await ctx.plugin(LocalBashExecutor, {})
  260. const bash = ctx.bash as LocalBashExecutor
  261. bash.internals = { spillDir, graceMs: 200 }
  262. const task = bash.start(bash.resolve({ command: 'trap \'\' TERM; sleep 60' }))
  263. await new Promise(resolve => setTimeout(resolve, 100))
  264. const pid = (task as unknown as { running: { pid: number } }).running.pid
  265. await fiber.dispose()
  266. // Disposal itself waited: the pid must already be gone, no grace left.
  267. expect(() => process.kill(pid, 0)).toThrow()
  268. expect(task.status).toBe('killed')
  269. })
  270. })