run.spec.ts 20 KB

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