process-inspector.spec.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. createProcessInspector,
  4. linuxProcessGroupHasLiveMembers,
  5. parseProcStat,
  6. } from '@deepseek-ai/dsh-subprocess-local/src/process-inspector.ts'
  7. import type { ProcessInspectorInternals } from '@deepseek-ai/dsh-subprocess-local/src/process-inspector.ts'
  8. import { WindowsProcessInspector } from '@deepseek-ai/dsh-subprocess-local/src/windows-inspector.ts'
  9. function stat(
  10. pid: number,
  11. pgrp: number,
  12. session: number,
  13. tpgid: number,
  14. started: string,
  15. parentPid = 1,
  16. state = 'S',
  17. ttyDevice = 99,
  18. ): string {
  19. const rest = [state, String(parentPid), String(pgrp), String(session), String(ttyDevice), String(tpgid)]
  20. while (rest.length < 19) rest.push('0')
  21. rest.push(started)
  22. return `${pid} (command with space) ${rest.join(' ')}`
  23. }
  24. function syscall(number: number, ...args: number[]): string {
  25. const six = [...args]
  26. while (six.length < 6) six.push(0)
  27. return `${number} ${six.slice(0, 6).map(value => `0x${value.toString(16)}`).join(' ')}`
  28. }
  29. function fakeInternals() {
  30. const files = new Map<string, string>()
  31. const dirs = new Map<string, string[]>()
  32. const links = new Map<string, string>()
  33. const devices = new Map<string, { character: boolean; rdev: number }>()
  34. const memories = new Map<string, Buffer>()
  35. const fds = new Map<number, string>()
  36. const kills: Array<[number, NodeJS.Signals]> = []
  37. let nextFd = 10
  38. let ps = ''
  39. let tpgid = '0'
  40. const internals: ProcessInspectorInternals = {
  41. readFile(path) {
  42. const value = files.get(path)
  43. if (value === undefined) throw new Error(`missing ${path}`)
  44. return value
  45. },
  46. readDir(path) {
  47. const value = dirs.get(path)
  48. if (value === undefined) throw new Error(`missing ${path}`)
  49. return value
  50. },
  51. readLink(path) {
  52. const value = links.get(path)
  53. if (value === undefined) throw new Error(`missing ${path}`)
  54. return value
  55. },
  56. stat(path) {
  57. const value = devices.get(path)
  58. if (value === undefined) throw new Error(`missing ${path}`)
  59. return { rdev: value.rdev, isCharacterDevice: () => value.character }
  60. },
  61. open(path) {
  62. if (!memories.has(path)) throw new Error(`missing ${path}`)
  63. const fd = nextFd++
  64. fds.set(fd, path)
  65. return fd
  66. },
  67. read(fd, buffer, length, position) {
  68. const path = fds.get(fd)
  69. if (path === undefined) throw new Error('bad fd')
  70. const source = memories.get(path)
  71. if (source === undefined) throw new Error('missing memory')
  72. return source.copy(buffer, 0, position, Math.min(source.length, position + length))
  73. },
  74. close(fd) { fds.delete(fd) },
  75. exec(_file, args) {
  76. if (args.includes('tpgid=')) return tpgid
  77. return ps
  78. },
  79. kill(pid, signal) { kills.push([pid, signal]) },
  80. }
  81. return {
  82. internals, files, dirs, links, devices, memories, kills,
  83. setPs(value: string) { ps = value },
  84. setTpgid(value: string) { tpgid = value },
  85. }
  86. }
  87. describe('Linux process inspector', () => {
  88. it('treats zombie-only process groups as quiescent and fails closed when unobservable', () => {
  89. const fake = fakeInternals()
  90. expect(linuxProcessGroupHasLiveMembers(77, fake.internals)).toBeUndefined()
  91. fake.dirs.set('/proc', ['self', '10', '11', '12'])
  92. fake.files.set('/proc/10/stat', stat(10, 77, 10, -1, '500', 1, 'Z'))
  93. fake.files.set('/proc/11/stat', stat(11, 77, 10, -1, '501', 1, 'X'))
  94. fake.files.set('/proc/12/stat', stat(12, 88, 12, -1, '502'))
  95. expect(linuxProcessGroupHasLiveMembers(77, fake.internals)).toBe(false)
  96. expect(linuxProcessGroupHasLiveMembers(99, fake.internals)).toBeUndefined()
  97. fake.files.set('/proc/11/stat', stat(11, 77, 10, -1, '501'))
  98. expect(linuxProcessGroupHasLiveMembers(77, fake.internals)).toBe(true)
  99. })
  100. it('parses stat safely, captures only the rooted process tree, and signals identities', () => {
  101. expect(parseProcStat('bad')).toBeUndefined()
  102. expect(parseProcStat('1 () ')).toBeUndefined()
  103. expect(parseProcStat('1 () S')).toBeUndefined()
  104. expect(parseProcStat(stat(10, 20, 30, 40, '500', 1, 'SS'))).toBeUndefined()
  105. expect(parseProcStat(stat(10, 20, 30, 40, '500'))).toEqual({ pid: 10, parentPid: 1, pgrp: 20, session: 30, state: 'S', ttyDevice: 99, tpgid: 40, started: '500' })
  106. const fake = fakeInternals()
  107. fake.dirs.set('/proc', ['x', '10', '11', '12', '13', '14'])
  108. fake.files.set('/proc/10/stat', stat(10, 20, 30, 40, '500'))
  109. fake.files.set('/proc/11/stat', stat(11, 21, 30, -1, '501'))
  110. fake.files.set('/proc/12/stat', stat(12, 22, 30, -1, '502', 10))
  111. fake.files.set('/proc/13/stat', stat(13, 23, 30, -1, '503', 12))
  112. const inspector = createProcessInspector('linux', 'x64', fake.internals)
  113. expect(inspector.foregroundPgid(10)).toBe(40)
  114. expect(inspector.foregroundPgid(11)).toBeUndefined()
  115. expect(inspector.foregroundPgid(99)).toBeUndefined()
  116. expect(inspector.processTree(10)).toEqual([
  117. { pid: 13, started: '503' },
  118. { pid: 12, started: '502' },
  119. { pid: 10, started: '500' },
  120. ])
  121. expect(inspector.processTree(99)).toEqual([])
  122. expect(inspector.processSession(30)).toEqual([
  123. { pid: 10, started: '500' },
  124. { pid: 11, started: '501' },
  125. { pid: 12, started: '502' },
  126. { pid: 13, started: '503' },
  127. ])
  128. expect(inspector.processSession(99)).toEqual([])
  129. expect(inspector.isAlive({ pid: 10, started: '500' })).toBe(true)
  130. expect(inspector.isAlive({ pid: 10, started: 'old' })).toBe(false)
  131. inspector.signalGroup(40, 'SIGINT')
  132. inspector.signalProcess({ pid: 10, started: '500' }, 'SIGTERM')
  133. inspector.signalProcess({ pid: 10, started: 'old' }, 'SIGKILL')
  134. expect(fake.kills).toEqual([[-40, 'SIGINT'], [10, 'SIGTERM']])
  135. fake.files.set('/proc/10/stat', stat(10, 20, 30, 40, '500', 1, 'Z'))
  136. expect(inspector.isAlive({ pid: 10, started: '500' })).toBe(false)
  137. inspector.signalProcess({ pid: 10, started: '500' }, 'SIGKILL')
  138. expect(fake.kills).toEqual([[-40, 'SIGINT'], [10, 'SIGTERM']])
  139. })
  140. it('detects supported kernel ABI waits across non-leader threads', () => {
  141. const fake = fakeInternals()
  142. fake.dirs.set('/proc', ['100', '101'])
  143. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  144. fake.files.set('/proc/101/stat', stat(101, 77, 100, 77, '2'))
  145. fake.dirs.set('/proc/100/task', ['100'])
  146. fake.dirs.set('/proc/101/task', ['101', '102'])
  147. fake.links.set('/proc/100/fd/0', '/dev/pts/1')
  148. fake.devices.set('/proc/100/fd/0', { character: true, rdev: 99 })
  149. fake.links.set('/proc/101/task/102/fd/0', '/dev/pts/1')
  150. fake.devices.set('/proc/101/task/102/fd/0', { character: true, rdev: 99 })
  151. const inspector = createProcessInspector('linux', 'x64', fake.internals)
  152. fake.files.set('/proc/100/task/100/syscall', 'running')
  153. fake.files.set('/proc/101/task/101/syscall', '-1 0x0')
  154. fake.files.set('/proc/101/task/102/syscall', syscall(0, 0))
  155. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  156. fake.files.set('/proc/101/task/102/syscall', syscall(63, 0))
  157. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  158. fake.files.set('/proc/101/task/102/syscall', syscall(270, 1, 0x10))
  159. const fdSet = Buffer.alloc(0x11)
  160. fdSet[0x10] = 1
  161. fake.memories.set('/proc/101/mem', fdSet)
  162. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  163. const poll = Buffer.alloc(8)
  164. poll.writeInt32LE(0, 0)
  165. poll.writeInt16LE(1, 4)
  166. fake.files.set('/proc/101/task/102/syscall', syscall(7, 0x20, 1))
  167. fake.memories.set('/proc/101/mem', Buffer.concat([Buffer.alloc(0x20), poll]))
  168. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  169. fake.files.set('/proc/101/task/102/syscall', syscall(232, 5, 0, 1))
  170. fake.files.set('/proc/101/task/102/fdinfo/5', 'pos: 0\ntfd: 0 events: 19\n')
  171. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  172. })
  173. it('uses the waiting thread fd table and recognizes the controlling-terminal alias', () => {
  174. const fake = fakeInternals()
  175. fake.dirs.set('/proc', ['100'])
  176. fake.files.set('/proc/99/stat', stat(99, 99, 99, 77, '0'))
  177. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  178. fake.dirs.set('/proc/100/task', ['100'])
  179. fake.files.set('/proc/100/task/100/syscall', syscall(0, 0))
  180. fake.links.set('/proc/99/fd/0', '/dev/pts/1')
  181. fake.devices.set('/proc/99/fd/0', { character: true, rdev: 99 })
  182. fake.links.set('/proc/100/fd/0', '/dev/pts/1')
  183. fake.devices.set('/proc/100/fd/0', { character: true, rdev: 99 })
  184. fake.links.set('/proc/100/task/100/fd/0', 'pipe:[123]')
  185. fake.devices.set('/proc/100/task/100/fd/0', { character: false, rdev: 0 })
  186. const inspector = createProcessInspector('linux', 'x64', fake.internals)
  187. expect(inspector.isStdinWaiting(77, 99)).toBe(false)
  188. fake.links.delete('/proc/100/task/100/fd/0')
  189. expect(inspector.isStdinWaiting(77, 99)).toBe(false)
  190. fake.links.set('/proc/100/task/100/fd/0', '/dev/tty')
  191. expect(inspector.isStdinWaiting(77, 99)).toBe(true)
  192. fake.links.set('/proc/100/task/100/fd/0', '/dev/pts/2')
  193. fake.devices.set('/proc/100/task/100/fd/0', { character: true, rdev: 100 })
  194. expect(inspector.isStdinWaiting(77, 99)).toBe(false)
  195. })
  196. it('fails closed on unsupported, malformed, unreadable, or non-stdin waits', () => {
  197. const fake = fakeInternals()
  198. fake.dirs.set('/proc', ['100'])
  199. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  200. fake.dirs.set('/proc/100/task', ['100'])
  201. fake.links.set('/proc/100/fd/0', '/dev/pts/1')
  202. fake.devices.set('/proc/100/fd/0', { character: true, rdev: 99 })
  203. fake.links.set('/proc/100/task/100/fd/0', '/dev/pts/1')
  204. fake.devices.set('/proc/100/task/100/fd/0', { character: true, rdev: 99 })
  205. fake.files.set('/proc/100/task/100/syscall', syscall(0, 2))
  206. expect(createProcessInspector('linux', 'mips', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  207. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  208. fake.files.set('/proc/100/task/100/syscall', syscall(270, 1, 0))
  209. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  210. fake.files.set('/proc/100/task/100/syscall', syscall(7, 0, 0))
  211. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  212. fake.files.set('/proc/100/task/100/syscall', syscall(7, 0, 1))
  213. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  214. fake.files.set('/proc/100/task/100/syscall', syscall(7, 0x20, 1))
  215. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  216. fake.files.set('/proc/100/task/100/syscall', syscall(232, 9, 0, 1))
  217. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  218. fake.files.set('/proc/100/task/100/syscall', syscall(999))
  219. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  220. fake.files.set('/proc/100/task/100/syscall', 'not-a-number 0x0')
  221. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  222. fake.dirs.delete('/proc/100/task')
  223. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  224. fake.dirs.set('/proc', ['100', '200'])
  225. fake.files.set('/proc/200/stat', stat(200, 88, 200, 88, '2'))
  226. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  227. })
  228. it('contains unreadable syscall, memory, and fdinfo boundaries', () => {
  229. const fake = fakeInternals()
  230. fake.dirs.set('/proc', ['100'])
  231. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  232. fake.dirs.set('/proc/100/task', ['100'])
  233. const inspector = createProcessInspector('linux', 'x64', fake.internals)
  234. fake.files.delete('/proc/100/stat')
  235. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  236. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1', 1, 'S', 0))
  237. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  238. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  239. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  240. fake.links.set('/proc/100/fd/0', '/dev/pts/1')
  241. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  242. fake.devices.set('/proc/100/fd/0', { character: true, rdev: 99 })
  243. fake.links.set('/proc/100/task/100/fd/0', '/dev/pts/1')
  244. fake.devices.set('/proc/100/task/100/fd/0', { character: true, rdev: 99 })
  245. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  246. fake.files.set('/proc/100/task/100/syscall', syscall(270, 1, 0x10))
  247. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  248. fake.files.set('/proc/100/task/100/syscall', syscall(232, 5, 0, 1))
  249. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  250. const noStdinPoll = Buffer.alloc(0x28)
  251. noStdinPoll.writeInt32LE(2, 0x20)
  252. noStdinPoll.writeInt16LE(1, 0x24)
  253. fake.memories.set('/proc/100/mem', noStdinPoll)
  254. fake.files.set('/proc/100/task/100/syscall', syscall(7, 0x20, 1))
  255. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  256. })
  257. })
  258. describe('macOS process inspector', () => {
  259. it('reads tpgid and process trees, contains cycles, and identity-fences signals', () => {
  260. const fake = fakeInternals()
  261. fake.setTpgid('55\n')
  262. fake.setPs(' 10 1 Mon Jul 21 10:00:00 2026\n 11 10 Mon Jul 21 10:00:01 2026\n 12 11 Mon Jul 21 10:00:02 2026\n 13 99 Mon Jul 21 10:00:03 2026\nmalformed\n')
  263. const inspector = createProcessInspector('darwin', 'arm64', fake.internals)
  264. expect(inspector.foregroundPgid(10)).toBe(55)
  265. expect(inspector.isStdinWaiting(55, 10)).toBe(false)
  266. expect(inspector.processTree(10)).toEqual([
  267. { pid: 12, started: 'Mon Jul 21 10:00:02 2026' },
  268. { pid: 11, started: 'Mon Jul 21 10:00:01 2026' },
  269. { pid: 10, started: 'Mon Jul 21 10:00:00 2026' },
  270. ])
  271. expect(inspector.processTree(99)).toEqual([])
  272. expect(inspector.processSession(10)).toEqual([])
  273. expect(inspector.isAlive({ pid: 11, started: 'Mon Jul 21 10:00:01 2026' })).toBe(true)
  274. inspector.signalGroup(55, 'SIGTSTP')
  275. inspector.signalProcess({ pid: 11, started: 'Mon Jul 21 10:00:01 2026' }, 'SIGKILL')
  276. inspector.signalProcess({ pid: 12, started: 'missing' }, 'SIGTERM')
  277. expect(fake.kills).toEqual([[-55, 'SIGTSTP'], [11, 'SIGKILL']])
  278. fake.setPs(' 10 11 Mon Jul 21 10:00:00 2026\n 11 10 Mon Jul 21 10:00:01 2026\n')
  279. expect(inspector.processTree(10)).toEqual([
  280. { pid: 11, started: 'Mon Jul 21 10:00:01 2026' },
  281. { pid: 10, started: 'Mon Jul 21 10:00:00 2026' },
  282. ])
  283. })
  284. it('returns undefined for missing or invalid foreground groups and dispatches platform inspectors', () => {
  285. const fake = fakeInternals()
  286. fake.setTpgid('-1')
  287. expect(createProcessInspector('darwin', 'arm64', fake.internals).foregroundPgid(1)).toBeUndefined()
  288. fake.internals.exec = () => { throw new Error('gone') }
  289. expect(createProcessInspector('darwin', 'arm64', fake.internals).foregroundPgid(1)).toBeUndefined()
  290. expect(createProcessInspector('win32', 'x64', fake.internals)).toBeInstanceOf(WindowsProcessInspector)
  291. expect(() => createProcessInspector('freebsd', 'x64', fake.internals)).toThrow('unsupported on platform freebsd')
  292. })
  293. })