process-inspector.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. const observed = inspector.snapshot()
  117. expect(observed.tree(10)).toEqual([
  118. { pid: 13, started: '503' },
  119. { pid: 12, started: '502' },
  120. { pid: 10, started: '500' },
  121. ])
  122. expect(observed.tree(99)).toEqual([])
  123. expect(observed.session(30)).toEqual([
  124. { pid: 10, started: '500' },
  125. { pid: 11, started: '501' },
  126. { pid: 12, started: '502' },
  127. { pid: 13, started: '503' },
  128. ])
  129. expect(observed.session(99)).toEqual([])
  130. expect(observed.alive({ pid: 10, started: '500' })).toBe(true)
  131. expect(observed.alive({ pid: 10, started: 'old' })).toBe(false)
  132. inspector.signalGroup(40, 'SIGINT')
  133. inspector.signalProcess({ pid: 10, started: '500' }, 'SIGTERM')
  134. inspector.signalProcess({ pid: 10, started: 'old' }, 'SIGKILL')
  135. expect(fake.kills).toEqual([[-40, 'SIGINT'], [10, 'SIGTERM']])
  136. fake.files.set('/proc/10/stat', stat(10, 20, 30, 40, '500', 1, 'Z'))
  137. // A zombie is present in the table but never signallable; both the batch
  138. // view and the signal fence report it quiescent once the state changes.
  139. expect(inspector.snapshot().alive({ pid: 10, started: '500' })).toBe(false)
  140. expect(inspector.isAlive({ pid: 10, started: '500' })).toBe(false)
  141. inspector.signalProcess({ pid: 10, started: '500' }, 'SIGKILL')
  142. expect(fake.kills).toEqual([[-40, 'SIGINT'], [10, 'SIGTERM']])
  143. })
  144. it('detects supported kernel ABI waits across non-leader threads', () => {
  145. const fake = fakeInternals()
  146. fake.dirs.set('/proc', ['100', '101'])
  147. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  148. fake.files.set('/proc/101/stat', stat(101, 77, 100, 77, '2'))
  149. fake.dirs.set('/proc/100/task', ['100'])
  150. fake.dirs.set('/proc/101/task', ['101', '102'])
  151. fake.links.set('/proc/100/fd/0', '/dev/pts/1')
  152. fake.devices.set('/proc/100/fd/0', { character: true, rdev: 99 })
  153. fake.links.set('/proc/101/task/102/fd/0', '/dev/pts/1')
  154. fake.devices.set('/proc/101/task/102/fd/0', { character: true, rdev: 99 })
  155. const inspector = createProcessInspector('linux', 'x64', fake.internals)
  156. fake.files.set('/proc/100/task/100/syscall', 'running')
  157. fake.files.set('/proc/101/task/101/syscall', '-1 0x0')
  158. fake.files.set('/proc/101/task/102/syscall', syscall(0, 0))
  159. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  160. fake.files.set('/proc/101/task/102/syscall', syscall(63, 0))
  161. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  162. fake.files.set('/proc/101/task/102/syscall', syscall(270, 1, 0x10))
  163. const fdSet = Buffer.alloc(0x11)
  164. fdSet[0x10] = 1
  165. fake.memories.set('/proc/101/mem', fdSet)
  166. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  167. const poll = Buffer.alloc(8)
  168. poll.writeInt32LE(0, 0)
  169. poll.writeInt16LE(1, 4)
  170. fake.files.set('/proc/101/task/102/syscall', syscall(7, 0x20, 1))
  171. fake.memories.set('/proc/101/mem', Buffer.concat([Buffer.alloc(0x20), poll]))
  172. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  173. fake.files.set('/proc/101/task/102/syscall', syscall(232, 5, 0, 1))
  174. fake.files.set('/proc/101/task/102/fdinfo/5', 'pos: 0\ntfd: 0 events: 19\n')
  175. expect(inspector.isStdinWaiting(77, 100)).toBe(true)
  176. })
  177. it('uses the waiting thread fd table and recognizes the controlling-terminal alias', () => {
  178. const fake = fakeInternals()
  179. fake.dirs.set('/proc', ['100'])
  180. fake.files.set('/proc/99/stat', stat(99, 99, 99, 77, '0'))
  181. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  182. fake.dirs.set('/proc/100/task', ['100'])
  183. fake.files.set('/proc/100/task/100/syscall', syscall(0, 0))
  184. fake.links.set('/proc/99/fd/0', '/dev/pts/1')
  185. fake.devices.set('/proc/99/fd/0', { character: true, rdev: 99 })
  186. fake.links.set('/proc/100/fd/0', '/dev/pts/1')
  187. fake.devices.set('/proc/100/fd/0', { character: true, rdev: 99 })
  188. fake.links.set('/proc/100/task/100/fd/0', 'pipe:[123]')
  189. fake.devices.set('/proc/100/task/100/fd/0', { character: false, rdev: 0 })
  190. const inspector = createProcessInspector('linux', 'x64', fake.internals)
  191. expect(inspector.isStdinWaiting(77, 99)).toBe(false)
  192. fake.links.delete('/proc/100/task/100/fd/0')
  193. expect(inspector.isStdinWaiting(77, 99)).toBe(false)
  194. fake.links.set('/proc/100/task/100/fd/0', '/dev/tty')
  195. expect(inspector.isStdinWaiting(77, 99)).toBe(true)
  196. fake.links.set('/proc/100/task/100/fd/0', '/dev/pts/2')
  197. fake.devices.set('/proc/100/task/100/fd/0', { character: true, rdev: 100 })
  198. expect(inspector.isStdinWaiting(77, 99)).toBe(false)
  199. })
  200. it('fails closed on unsupported, malformed, unreadable, or non-stdin waits', () => {
  201. const fake = fakeInternals()
  202. fake.dirs.set('/proc', ['100'])
  203. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  204. fake.dirs.set('/proc/100/task', ['100'])
  205. fake.links.set('/proc/100/fd/0', '/dev/pts/1')
  206. fake.devices.set('/proc/100/fd/0', { character: true, rdev: 99 })
  207. fake.links.set('/proc/100/task/100/fd/0', '/dev/pts/1')
  208. fake.devices.set('/proc/100/task/100/fd/0', { character: true, rdev: 99 })
  209. fake.files.set('/proc/100/task/100/syscall', syscall(0, 2))
  210. expect(createProcessInspector('linux', 'mips', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  211. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  212. fake.files.set('/proc/100/task/100/syscall', syscall(270, 1, 0))
  213. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  214. fake.files.set('/proc/100/task/100/syscall', syscall(7, 0, 0))
  215. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  216. fake.files.set('/proc/100/task/100/syscall', syscall(7, 0, 1))
  217. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  218. fake.files.set('/proc/100/task/100/syscall', syscall(7, 0x20, 1))
  219. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  220. fake.files.set('/proc/100/task/100/syscall', syscall(232, 9, 0, 1))
  221. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  222. fake.files.set('/proc/100/task/100/syscall', syscall(999))
  223. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  224. fake.files.set('/proc/100/task/100/syscall', 'not-a-number 0x0')
  225. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  226. fake.dirs.delete('/proc/100/task')
  227. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  228. fake.dirs.set('/proc', ['100', '200'])
  229. fake.files.set('/proc/200/stat', stat(200, 88, 200, 88, '2'))
  230. expect(createProcessInspector('linux', 'x64', fake.internals).isStdinWaiting(77, 100)).toBe(false)
  231. })
  232. it('contains unreadable syscall, memory, and fdinfo boundaries', () => {
  233. const fake = fakeInternals()
  234. fake.dirs.set('/proc', ['100'])
  235. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  236. fake.dirs.set('/proc/100/task', ['100'])
  237. const inspector = createProcessInspector('linux', 'x64', fake.internals)
  238. fake.files.delete('/proc/100/stat')
  239. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  240. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1', 1, 'S', 0))
  241. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  242. fake.files.set('/proc/100/stat', stat(100, 77, 100, 77, '1'))
  243. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  244. fake.links.set('/proc/100/fd/0', '/dev/pts/1')
  245. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  246. fake.devices.set('/proc/100/fd/0', { character: true, rdev: 99 })
  247. fake.links.set('/proc/100/task/100/fd/0', '/dev/pts/1')
  248. fake.devices.set('/proc/100/task/100/fd/0', { character: true, rdev: 99 })
  249. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  250. fake.files.set('/proc/100/task/100/syscall', syscall(270, 1, 0x10))
  251. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  252. fake.files.set('/proc/100/task/100/syscall', syscall(232, 5, 0, 1))
  253. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  254. const noStdinPoll = Buffer.alloc(0x28)
  255. noStdinPoll.writeInt32LE(2, 0x20)
  256. noStdinPoll.writeInt16LE(1, 0x24)
  257. fake.memories.set('/proc/100/mem', noStdinPoll)
  258. fake.files.set('/proc/100/task/100/syscall', syscall(7, 0x20, 1))
  259. expect(inspector.isStdinWaiting(77, 100)).toBe(false)
  260. })
  261. })
  262. describe('macOS process inspector', () => {
  263. it('reads tpgid and process trees, contains cycles, and identity-fences signals', () => {
  264. const fake = fakeInternals()
  265. fake.setTpgid('55\n')
  266. 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')
  267. const inspector = createProcessInspector('darwin', 'arm64', fake.internals)
  268. expect(inspector.foregroundPgid(10)).toBe(55)
  269. expect(inspector.isStdinWaiting(55, 10)).toBe(false)
  270. const observed = inspector.snapshot()
  271. expect(observed.tree(10)).toEqual([
  272. { pid: 12, started: 'Mon Jul 21 10:00:02 2026' },
  273. { pid: 11, started: 'Mon Jul 21 10:00:01 2026' },
  274. { pid: 10, started: 'Mon Jul 21 10:00:00 2026' },
  275. ])
  276. expect(observed.tree(99)).toEqual([])
  277. expect(observed.session(10)).toEqual([])
  278. expect(observed.alive({ pid: 11, started: 'Mon Jul 21 10:00:01 2026' })).toBe(true)
  279. inspector.signalGroup(55, 'SIGTSTP')
  280. inspector.signalProcess({ pid: 11, started: 'Mon Jul 21 10:00:01 2026' }, 'SIGKILL')
  281. inspector.signalProcess({ pid: 12, started: 'missing' }, 'SIGTERM')
  282. expect(fake.kills).toEqual([[-55, 'SIGTSTP'], [11, 'SIGKILL']])
  283. fake.setPs(' 10 11 Mon Jul 21 10:00:00 2026\n 11 10 Mon Jul 21 10:00:01 2026\n')
  284. expect(inspector.snapshot().tree(10)).toEqual([
  285. { pid: 11, started: 'Mon Jul 21 10:00:01 2026' },
  286. { pid: 10, started: 'Mon Jul 21 10:00:00 2026' },
  287. ])
  288. })
  289. it('re-reads the process table before signalling instead of trusting an earlier observation', () => {
  290. const fake = fakeInternals()
  291. fake.setPs(' 11 10 Mon Jul 21 10:00:01 2026\n')
  292. const inspector = createProcessInspector('darwin', 'arm64', fake.internals)
  293. inspector.snapshot()
  294. // The member exits after that observation; a recycled pid would otherwise
  295. // inherit the observed identity and take the signal meant for the original.
  296. fake.setPs('')
  297. inspector.signalProcess({ pid: 11, started: 'Mon Jul 21 10:00:01 2026' }, 'SIGKILL')
  298. expect(fake.kills).toEqual([])
  299. })
  300. it('returns undefined for missing or invalid foreground groups and dispatches platform inspectors', () => {
  301. const fake = fakeInternals()
  302. fake.setTpgid('-1')
  303. expect(createProcessInspector('darwin', 'arm64', fake.internals).foregroundPgid(1)).toBeUndefined()
  304. fake.internals.exec = () => { throw new Error('gone') }
  305. expect(createProcessInspector('darwin', 'arm64', fake.internals).foregroundPgid(1)).toBeUndefined()
  306. expect(createProcessInspector('win32', 'x64', fake.internals)).toBeInstanceOf(WindowsProcessInspector)
  307. expect(() => createProcessInspector('freebsd', 'x64', fake.internals)).toThrow('unsupported on platform freebsd')
  308. })
  309. })