process-inspector.spec.ts 12 KB

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