run.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import { mkdtempSync, readFileSync, statSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { dirname, join } from 'node:path'
  4. import { describe, expect, it, vi } from 'vitest'
  5. import { killGroup, OutputCollector, runBash } from '@deepseek-ai/dsh-bash-local'
  6. import type { RunningBash } from '@deepseek-ai/dsh-bash-local'
  7. const { failNextClose } = vi.hoisted(() => ({ failNextClose: { value: false } }))
  8. vi.mock('node:fs', async (importOriginal) => {
  9. const actual = await importOriginal<typeof import('node:fs')>()
  10. return {
  11. ...actual,
  12. closeSync(fd: number): void {
  13. if (failNextClose.value) {
  14. failNextClose.value = false
  15. throw Object.assign(new Error('simulated EIO on close'), { code: 'EIO' })
  16. }
  17. actual.closeSync(fd)
  18. },
  19. }
  20. })
  21. const spillDir = mkdtempSync(join(tmpdir(), 'dsh-bash-spec-'))
  22. function spec(command: string, overrides: Partial<Parameters<typeof runBash>[0]> = {}) {
  23. return {
  24. command,
  25. cwd: process.cwd(),
  26. maxOutputBytes: 64_000,
  27. graceMs: 3_000,
  28. ...overrides,
  29. }
  30. }
  31. /** Poll until a pid no longer exists (kill(pid, 0) throws ESRCH). */
  32. async function waitGone(pid: number, timeoutMs = 5_000): Promise<void> {
  33. const deadline = Date.now() + timeoutMs
  34. while (Date.now() < deadline) {
  35. try {
  36. process.kill(pid, 0)
  37. } catch {
  38. return
  39. }
  40. await new Promise(resolve => setTimeout(resolve, 20))
  41. }
  42. throw new Error(`pid ${pid} still alive after ${timeoutMs}ms`)
  43. }
  44. async function waitForStdout(running: RunningBash, expected: string, timeoutMs = 5_000): Promise<void> {
  45. const deadline = Date.now() + timeoutMs
  46. while (Date.now() < deadline) {
  47. if (running.stdout.snapshot().text.includes(expected)) return
  48. await new Promise(resolve => setTimeout(resolve, 20))
  49. }
  50. throw new Error(`stdout did not include ${JSON.stringify(expected)} after ${timeoutMs}ms`)
  51. }
  52. async function waitForPidFile(path: string, timeoutMs = 5_000): Promise<number> {
  53. const deadline = Date.now() + timeoutMs
  54. while (Date.now() < deadline) {
  55. try {
  56. const pid = Number(readFileSync(path, 'utf8').trim())
  57. if (Number.isSafeInteger(pid) && pid > 0) return pid
  58. } catch {
  59. // The child shell has not written the pid file yet.
  60. }
  61. await new Promise(resolve => setTimeout(resolve, 20))
  62. }
  63. throw new Error(`pid file ${path} was not written after ${timeoutMs}ms`)
  64. }
  65. describe('runBash', () => {
  66. it('captures stdout on success', async () => {
  67. const result = await runBash(spec('echo hello')).done
  68. expect(result.exitCode).toBe(0)
  69. expect(result.signal).toBeNull()
  70. expect(result.stdout.text).toBe('hello\n')
  71. expect(result.stdout.truncated).toBe(false)
  72. expect(result.stderr.text).toBe('')
  73. })
  74. it('captures stderr separately', async () => {
  75. const result = await runBash(spec('echo oops >&2')).done
  76. expect(result.exitCode).toBe(0)
  77. expect(result.stdout.text).toBe('')
  78. expect(result.stderr.text).toBe('oops\n')
  79. })
  80. it('captures both streams', async () => {
  81. const result = await runBash(spec('echo out; echo err >&2')).done
  82. expect(result.stdout.text).toBe('out\n')
  83. expect(result.stderr.text).toBe('err\n')
  84. })
  85. it('reports non-zero exit codes', async () => {
  86. const result = await runBash(spec('exit 42')).done
  87. expect(result.exitCode).toBe(42)
  88. expect(result.signal).toBeNull()
  89. })
  90. it('applies model-friendly env overrides', async () => {
  91. const result = await runBash(spec('echo "$NO_COLOR/$TERM/$PAGER"')).done
  92. expect(result.stdout.text).toBe('1/dumb/cat\n')
  93. })
  94. it('runs in the requested cwd', async () => {
  95. const result = await runBash(spec('pwd', { cwd: '/tmp' })).done
  96. expect(result.stdout.text.trim()).toMatch(/\/tmp$/)
  97. })
  98. it('kills the process group with SIGTERM when the signal fires', async () => {
  99. // runBash owns no timer: it kills on abort. The executor drives the timeout
  100. // by firing this signal via a deadline (see executor.spec.ts); here we
  101. // assert the kill itself lands as SIGTERM.
  102. const controller = new AbortController()
  103. const start = Date.now()
  104. const running = runBash(spec('sleep 60', { signal: controller.signal }))
  105. setTimeout(() => { controller.abort('deadline') }, 100)
  106. const result = await running.done
  107. expect(Date.now() - start).toBeLessThan(5_000)
  108. expect(result.signal).toBe('SIGTERM')
  109. expect(result.exitCode).toBeNull()
  110. })
  111. it('escalates to SIGKILL when SIGTERM is trapped', async () => {
  112. const running = runBash(spec('trap \'\' TERM; echo ready; while :; do sleep 60 & wait $!; done', { graceMs: 200 }))
  113. await waitForStdout(running, 'ready\n')
  114. running.kill()
  115. const result = await running.done
  116. expect(result.signal).toBe('SIGKILL')
  117. })
  118. it('kills the whole process group (grandchildren die too)', async () => {
  119. // The subshell writes the sleep's pid then waits on it; killing the
  120. // group must take the sleep down with bash.
  121. const pidFile = join(spillDir, `grandchild-${Date.now()}.pid`)
  122. const running = runBash(spec(`sleep 60 & echo $! > ${pidFile}; wait`))
  123. const grandchild = await waitForPidFile(pidFile)
  124. expect(grandchild).toBeGreaterThan(0)
  125. running.kill()
  126. const result = await running.done
  127. expect(result.signal).toBe('SIGTERM')
  128. await waitGone(grandchild)
  129. })
  130. it('aborts via AbortSignal mid-run', async () => {
  131. const controller = new AbortController()
  132. const running = runBash(spec('sleep 60', { signal: controller.signal }))
  133. setTimeout(() => { controller.abort('user cancelled') }, 50)
  134. const result = await running.done
  135. expect(result.signal).toBe('SIGTERM')
  136. })
  137. it('throws when the signal is already aborted before spawn', () => {
  138. const controller = new AbortController()
  139. controller.abort('too late')
  140. expect(() => runBash(spec('echo hi', { signal: controller.signal })))
  141. .toThrow(/aborted before spawn: too late/)
  142. })
  143. it('rejects with a spawn error for a nonexistent cwd', async () => {
  144. await expect(runBash(spec('echo hi', { cwd: '/nonexistent-dir-dsh-test' })).done)
  145. .rejects.toThrow(/ENOENT/)
  146. })
  147. it('kill() is idempotent (second call does not restart escalation)', async () => {
  148. const running = runBash(spec('sleep 60'))
  149. running.kill()
  150. running.kill()
  151. const result = await running.done
  152. expect(result.signal).toBe('SIGTERM')
  153. })
  154. })
  155. describe('stdin and extra env (set by in-process plugins)', () => {
  156. it('writes stdin to the command and closes it', async () => {
  157. const result = await runBash(spec('cat', { stdin: 'hello from stdin\n' })).done
  158. expect(result.exitCode).toBe(0)
  159. expect(result.stdout.text).toBe('hello from stdin\n')
  160. })
  161. it('a command that reads stdin sees EOF when none is supplied', async () => {
  162. // No stdin → fd 0 is /dev/null, so `cat` reads EOF and exits 0 with no
  163. // output (it does NOT block).
  164. const result = await runBash(spec('cat')).done
  165. expect(result.exitCode).toBe(0)
  166. expect(result.stdout.text).toBe('')
  167. })
  168. it('gives fd 0 the exact pre-seam type: /dev/null when no stdin, a pipe when supplied', async () => {
  169. // With no bytes, fd 0 remains the pre-seam `ignore` default (/dev/null, a character device).
  170. // Supplied bytes use Node's spawn pipe, which is an AF_UNIX socket rather than a FIFO.
  171. const none = await runBash(spec('test -c /dev/stdin && echo char || echo other')).done
  172. expect(none.stdout.text).toBe('char\n')
  173. const piped = await runBash(spec('test -S /dev/stdin && echo socket || echo other', { stdin: 'x' })).done
  174. expect(piped.stdout.text).toBe('socket\n')
  175. })
  176. it('merges extra env entries onto the scrubbed environment', async () => {
  177. const result = await runBash(spec('echo "$DSH_EXTRA_ONE/$DSH_EXTRA_TWO"', {
  178. env: { DSH_EXTRA_ONE: 'alpha', DSH_EXTRA_TWO: 'beta' },
  179. })).done
  180. expect(result.stdout.text).toBe('alpha/beta\n')
  181. })
  182. it('an explicit extra env entry overrides the model-friendly override and the scrub', async () => {
  183. // TERM is a model-friendly OVERRIDE (dumb); an explicit extra entry wins.
  184. // DSH_OVERRIDE_KEY matches the credential scrub pattern, yet an explicit
  185. // entry is still honored — the scrub only drops AMBIENT process.env creds.
  186. const result = await runBash(spec('echo "$TERM/$DSH_OVERRIDE_KEY"', {
  187. env: { TERM: 'xterm-256color', DSH_OVERRIDE_KEY: 'explicit-wins' },
  188. })).done
  189. expect(result.stdout.text).toBe('xterm-256color/explicit-wins\n')
  190. })
  191. it('does not crash or reject when the child ignores a large stdin (EPIPE)', async () => {
  192. // The child exits without reading, so closing a stdin pipe holding ~1 MiB triggers EPIPE.
  193. // The handler swallows that write error and `done` reports the child's real exit.
  194. const big = 'x'.repeat(1024 * 1024)
  195. const result = await runBash(spec('exit 7', { stdin: big })).done
  196. expect(result.exitCode).toBe(7)
  197. })
  198. })
  199. describe('output truncation and spill', () => {
  200. it('keeps the tail and spills the full stream to disk', async () => {
  201. // 200 numbered lines of ~10 bytes; cap at 500 bytes keeps a late tail.
  202. const result = await runBash(
  203. spec('for i in $(seq 1 200); do printf "line-%04d\\n" $i; done', { maxOutputBytes: 500 }),
  204. { spillDir },
  205. ).done
  206. expect(result.stdout.truncated).toBe(true)
  207. expect(result.stdout.text.length).toBeLessThanOrEqual(500)
  208. expect(result.stdout.text).toContain('line-0200')
  209. expect(result.stdout.text).not.toContain('line-0001')
  210. expect(result.stdout.spillPath).toBeDefined()
  211. const full = readFileSync(result.stdout.spillPath!, 'utf8')
  212. expect(full).toContain('line-0001')
  213. expect(full).toContain('line-0200')
  214. })
  215. it('does not truncate output exactly at the cap', async () => {
  216. const result = await runBash(
  217. spec('printf "%.0sx" $(seq 1 500)', { maxOutputBytes: 500 }),
  218. { spillDir },
  219. ).done
  220. expect(result.stdout.truncated).toBe(false)
  221. expect(result.stdout.text.length).toBe(500)
  222. expect(result.stdout.spillPath).toBeUndefined()
  223. })
  224. it('settles with the tail and no spill path when final spill close fails', async () => {
  225. failNextClose.value = true
  226. const result = await runBash(
  227. spec('for i in $(seq 1 200); do printf "line-%04d\\n" $i; done', { maxOutputBytes: 500 }),
  228. { spillDir },
  229. ).done
  230. expect(failNextClose.value).toBe(false)
  231. expect(result.exitCode).toBe(0)
  232. expect(result.stdout.truncated).toBe(true)
  233. expect(result.stdout.text).toContain('line-0200')
  234. expect(result.stdout.spillPath).toBeUndefined()
  235. })
  236. })
  237. describe('OutputCollector', () => {
  238. it('keeps the tail of a single oversized chunk', () => {
  239. const collector = new OutputCollector(10, 'test', spillDir)
  240. collector.push(Buffer.from('0123456789abcdef'))
  241. const out = collector.finalize()
  242. expect(out.text).toBe('6789abcdef')
  243. expect(out.truncated).toBe(true)
  244. expect(readFileSync(out.spillPath!, 'utf8')).toBe('0123456789abcdef')
  245. })
  246. it('readFrom returns increments and flags lossy reads', () => {
  247. const collector = new OutputCollector(10, 'test', spillDir)
  248. collector.push(Buffer.from('aaaaa'))
  249. const first = collector.readFrom(0)
  250. expect(first.text).toBe('aaaaa')
  251. expect(first.lossy).toBe(false)
  252. expect(first.nextOffset).toBe(5)
  253. collector.push(Buffer.from('bbbbb'))
  254. const second = collector.readFrom(first.nextOffset)
  255. expect(second.text).toBe('bbbbb')
  256. expect(second.lossy).toBe(false)
  257. // Push enough to slide the window past the last offset.
  258. collector.push(Buffer.from('c'.repeat(20)))
  259. const third = collector.readFrom(second.nextOffset)
  260. expect(third.lossy).toBe(true)
  261. expect(third.text).toBe('c'.repeat(10))
  262. expect(third.spillPath).toBeDefined()
  263. })
  264. it('tracks totalBytes across drops', () => {
  265. const collector = new OutputCollector(4, 'test', spillDir)
  266. collector.push(Buffer.from('aaaa'))
  267. collector.push(Buffer.from('bbbb'))
  268. expect(collector.totalBytes).toBe(8)
  269. expect(collector.finalize().text).toBe('bbbb')
  270. })
  271. it('contains close failures and drops the spill path', () => {
  272. const collector = new OutputCollector(4, 'closefail', spillDir)
  273. collector.push(Buffer.from('aaaa'))
  274. collector.push(Buffer.from('bbbb'))
  275. expect(collector.snapshot().spillPath).toBeDefined()
  276. failNextClose.value = true
  277. let out: ReturnType<typeof collector.finalize>
  278. expect(() => { out = collector.finalize() }).not.toThrow()
  279. expect(failNextClose.value).toBe(false)
  280. expect(out!.text).toBe('bbbb')
  281. expect(out!.truncated).toBe(true)
  282. expect(out!.spillPath).toBeUndefined()
  283. })
  284. })
  285. describe('killGroup', () => {
  286. it('ignores non-positive pids', () => {
  287. expect(() => { killGroup(-1, 'SIGTERM') }).not.toThrow()
  288. expect(() => { killGroup(0, 'SIGTERM') }).not.toThrow()
  289. })
  290. it('swallows ESRCH for vanished groups', async () => {
  291. const running = runBash(spec('true'))
  292. await running.done
  293. expect(() => { killGroup(running.pid, 'SIGTERM') }).not.toThrow()
  294. })
  295. })
  296. describe('abort edge cases', () => {
  297. it('reports a fallback reason for reason-less pre-aborted signals', () => {
  298. // Real AbortControllers always set a DOMException reason; signal-like
  299. // objects from other libraries may not — the fallback covers them.
  300. const bare = {
  301. aborted: true,
  302. reason: undefined,
  303. addEventListener() {},
  304. removeEventListener() {},
  305. } as unknown as AbortSignal
  306. expect(() => runBash(spec('echo hi', { signal: bare })))
  307. .toThrow(/aborted before spawn: aborted/)
  308. })
  309. it('reports the terminating signal of an externally self-killed command', async () => {
  310. // runBash reports the raw signal; whether it counts as timeout/cancel is the
  311. // executor's classification (a self-kill is neither) — see executor.spec.ts.
  312. const result = await runBash(spec('kill -TERM $$')).done
  313. expect(result.signal).toBe('SIGTERM')
  314. })
  315. })
  316. describe('environment and spill-file hardening', () => {
  317. it('scrubs credential-shaped env vars from child processes', async () => {
  318. process.env.DSH_TEST_API_KEY = 'super-secret'
  319. process.env.DSH_TEST_TOKEN = 'also-secret'
  320. process.env.DSH_TEST_PLAIN = 'visible'
  321. try {
  322. const result = await runBash(spec('echo "[${DSH_TEST_API_KEY:-absent}|${DSH_TEST_TOKEN:-absent}|${DSH_TEST_PLAIN:-absent}]"')).done
  323. expect(result.stdout.text.trim()).toBe('[absent|absent|visible]')
  324. } finally {
  325. delete process.env.DSH_TEST_API_KEY
  326. delete process.env.DSH_TEST_TOKEN
  327. delete process.env.DSH_TEST_PLAIN
  328. }
  329. })
  330. it('creates spill files with owner-only permissions and random names', async () => {
  331. const result = await runBash(
  332. spec('for i in $(seq 1 200); do printf "line-%04d\\n" $i; done', { maxOutputBytes: 500 }),
  333. { spillDir },
  334. ).done
  335. const path = result.stdout.spillPath!
  336. expect(path).toMatch(/dsh-bash-\d+-\d+-[0-9a-f]{12}-stdout\.log$/)
  337. const mode = statSync(path).mode & 0o777
  338. expect(mode).toBe(0o600)
  339. })
  340. it('defaults spills into a private per-process directory', async () => {
  341. const result = await runBash(
  342. spec('for i in $(seq 1 200); do printf "line-%04d\\n" $i; done', { maxOutputBytes: 500 }),
  343. ).done
  344. const dir = dirname(result.stdout.spillPath!)
  345. expect(dir).toMatch(/dsh-bash-/)
  346. const mode = statSync(dir).mode & 0o777
  347. expect(mode).toBe(0o700)
  348. })
  349. it('killGroup never throws, even for EPERM-style failures', () => {
  350. const spy = vi.spyOn(process, 'kill').mockImplementation(() => {
  351. throw Object.assign(new Error('EPERM'), { code: 'EPERM' })
  352. })
  353. try {
  354. expect(() => { killGroup(12345, 'SIGTERM') }).not.toThrow()
  355. } finally {
  356. spy.mockRestore()
  357. }
  358. })
  359. it('honors AbortSignal on background-style runs (no timeout)', async () => {
  360. const controller = new AbortController()
  361. const running = runBash(spec('sleep 60', { signal: controller.signal }))
  362. setTimeout(() => { controller.abort() }, 50)
  363. const result = await running.done
  364. expect(result.signal).toBe('SIGTERM')
  365. })
  366. })