run.spec.ts 17 KB

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