@ai-devkit/agent-manager 0.26.2 → 0.26.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/dist/AgentManager.d.ts +5 -2
  2. package/dist/AgentManager.d.ts.map +1 -1
  3. package/dist/AgentManager.js +21 -4
  4. package/dist/AgentManager.js.map +1 -1
  5. package/dist/__tests__/AgentManager.test.js +147 -0
  6. package/dist/__tests__/AgentManager.test.js.map +1 -1
  7. package/dist/__tests__/adapters/ClaudeCodeAdapter.test.js +13 -4
  8. package/dist/__tests__/adapters/ClaudeCodeAdapter.test.js.map +1 -1
  9. package/dist/__tests__/adapters/CodexAdapter.test.js +13 -4
  10. package/dist/__tests__/adapters/CodexAdapter.test.js.map +1 -1
  11. package/dist/__tests__/adapters/CopilotAdapter.test.js +7 -2
  12. package/dist/__tests__/adapters/CopilotAdapter.test.js.map +1 -1
  13. package/dist/__tests__/adapters/GeminiCliAdapter.test.js +18 -5
  14. package/dist/__tests__/adapters/GeminiCliAdapter.test.js.map +1 -1
  15. package/dist/__tests__/adapters/GrokCliAdapter.test.js +7 -2
  16. package/dist/__tests__/adapters/GrokCliAdapter.test.js.map +1 -1
  17. package/dist/__tests__/adapters/OpenCodeAdapter.test.js +13 -4
  18. package/dist/__tests__/adapters/OpenCodeAdapter.test.js.map +1 -1
  19. package/dist/__tests__/adapters/PiAdapter.test.js +32 -4
  20. package/dist/__tests__/adapters/PiAdapter.test.js.map +1 -1
  21. package/dist/__tests__/utils/process.test.js +90 -2
  22. package/dist/__tests__/utils/process.test.js.map +1 -1
  23. package/dist/adapters/AgentAdapter.d.ts +9 -3
  24. package/dist/adapters/AgentAdapter.d.ts.map +1 -1
  25. package/dist/adapters/AgentAdapter.js.map +1 -1
  26. package/dist/adapters/ClaudeCodeAdapter.d.ts +5 -4
  27. package/dist/adapters/ClaudeCodeAdapter.d.ts.map +1 -1
  28. package/dist/adapters/ClaudeCodeAdapter.js +11 -7
  29. package/dist/adapters/ClaudeCodeAdapter.js.map +1 -1
  30. package/dist/adapters/CodexAdapter.d.ts +5 -4
  31. package/dist/adapters/CodexAdapter.d.ts.map +1 -1
  32. package/dist/adapters/CodexAdapter.js +11 -7
  33. package/dist/adapters/CodexAdapter.js.map +1 -1
  34. package/dist/adapters/CopilotAdapter.d.ts +5 -4
  35. package/dist/adapters/CopilotAdapter.d.ts.map +1 -1
  36. package/dist/adapters/CopilotAdapter.js +11 -7
  37. package/dist/adapters/CopilotAdapter.js.map +1 -1
  38. package/dist/adapters/GeminiCliAdapter.d.ts +6 -5
  39. package/dist/adapters/GeminiCliAdapter.d.ts.map +1 -1
  40. package/dist/adapters/GeminiCliAdapter.js +12 -8
  41. package/dist/adapters/GeminiCliAdapter.js.map +1 -1
  42. package/dist/adapters/GrokCliAdapter.d.ts +3 -2
  43. package/dist/adapters/GrokCliAdapter.d.ts.map +1 -1
  44. package/dist/adapters/GrokCliAdapter.js +10 -6
  45. package/dist/adapters/GrokCliAdapter.js.map +1 -1
  46. package/dist/adapters/OpenCodeAdapter.d.ts +5 -4
  47. package/dist/adapters/OpenCodeAdapter.d.ts.map +1 -1
  48. package/dist/adapters/OpenCodeAdapter.js +11 -7
  49. package/dist/adapters/OpenCodeAdapter.js.map +1 -1
  50. package/dist/adapters/PiAdapter.d.ts +3 -2
  51. package/dist/adapters/PiAdapter.d.ts.map +1 -1
  52. package/dist/adapters/PiAdapter.js +12 -9
  53. package/dist/adapters/PiAdapter.js.map +1 -1
  54. package/dist/adapters/index.d.ts +1 -1
  55. package/dist/adapters/index.d.ts.map +1 -1
  56. package/dist/adapters/index.js.map +1 -1
  57. package/dist/index.d.ts +2 -1
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +1 -0
  60. package/dist/index.js.map +1 -1
  61. package/dist/utils/index.d.ts +1 -1
  62. package/dist/utils/index.d.ts.map +1 -1
  63. package/dist/utils/index.js +1 -1
  64. package/dist/utils/index.js.map +1 -1
  65. package/dist/utils/process.d.ts +9 -1
  66. package/dist/utils/process.d.ts.map +1 -1
  67. package/dist/utils/process.js +155 -10
  68. package/dist/utils/process.js.map +1 -1
  69. package/package.json +1 -1
  70. package/src/AgentManager.ts +28 -4
  71. package/src/__tests__/AgentManager.test.ts +89 -0
  72. package/src/__tests__/adapters/ClaudeCodeAdapter.test.ts +16 -5
  73. package/src/__tests__/adapters/CodexAdapter.test.ts +16 -5
  74. package/src/__tests__/adapters/CopilotAdapter.test.ts +8 -1
  75. package/src/__tests__/adapters/GeminiCliAdapter.test.ts +20 -4
  76. package/src/__tests__/adapters/GrokCliAdapter.test.ts +8 -1
  77. package/src/__tests__/adapters/OpenCodeAdapter.test.ts +16 -5
  78. package/src/__tests__/adapters/PiAdapter.test.ts +28 -5
  79. package/src/__tests__/utils/process.test.ts +72 -1
  80. package/src/adapters/AgentAdapter.ts +11 -3
  81. package/src/adapters/ClaudeCodeAdapter.ts +10 -7
  82. package/src/adapters/CodexAdapter.ts +10 -7
  83. package/src/adapters/CopilotAdapter.ts +16 -7
  84. package/src/adapters/GeminiCliAdapter.ts +17 -8
  85. package/src/adapters/GrokCliAdapter.ts +9 -6
  86. package/src/adapters/OpenCodeAdapter.ts +10 -7
  87. package/src/adapters/PiAdapter.ts +10 -9
  88. package/src/adapters/index.ts +1 -1
  89. package/src/index.ts +2 -0
  90. package/src/utils/index.ts +1 -1
  91. package/src/utils/process.ts +160 -10
@@ -11,7 +11,7 @@ import { GeminiCliAdapter } from '../../adapters/GeminiCliAdapter.js';
11
11
  import type { ProcessInfo } from '../../adapters/AgentAdapter.js';
12
12
  import { AgentStatus } from '../../adapters/AgentAdapter.js';
13
13
  import { AgentRegistry, type RegistryEntry } from '../../utils/AgentRegistry.js';
14
- import { listAgentProcesses, enrichProcesses } from '../../utils/process.js';
14
+ import { listAgentProcesses, enrichProcesses, captureProcessSnapshot } from '../../utils/process.js';
15
15
  import { matchProcessesToSessions, generateAgentName } from '../../utils/matching.js';
16
16
  import * as crypto from 'crypto';
17
17
 
@@ -21,6 +21,7 @@ vi.mock('../../utils/process.js', async (importOriginal) => {
21
21
  ...actual,
22
22
  listAgentProcesses: vi.fn(),
23
23
  enrichProcesses: vi.fn(),
24
+ captureProcessSnapshot: vi.fn(),
24
25
  };
25
26
  });
26
27
 
@@ -31,6 +32,7 @@ vi.mock('../../utils/matching.js', () => ({
31
32
 
32
33
  const mockedListAgentProcesses = listAgentProcesses as MockedFunction<typeof listAgentProcesses>;
33
34
  const mockedEnrichProcesses = enrichProcesses as MockedFunction<typeof enrichProcesses>;
35
+ const mockedCaptureProcessSnapshot = captureProcessSnapshot as MockedFunction<typeof captureProcessSnapshot>;
34
36
  const mockedMatchProcessesToSessions = matchProcessesToSessions as MockedFunction<typeof matchProcessesToSessions>;
35
37
  const mockedGenerateAgentName = generateAgentName as MockedFunction<typeof generateAgentName>;
36
38
 
@@ -45,10 +47,15 @@ describe('GeminiCliAdapter', () => {
45
47
  adapter = new GeminiCliAdapter(new AgentRegistry(path.join(tmpHome, 'agents.json')));
46
48
  mockedListAgentProcesses.mockReset();
47
49
  mockedEnrichProcesses.mockReset();
50
+ mockedCaptureProcessSnapshot.mockReset();
48
51
  mockedMatchProcessesToSessions.mockReset();
49
52
  mockedGenerateAgentName.mockReset();
50
53
 
51
54
  mockedEnrichProcesses.mockImplementation((procs) => procs);
55
+ // Compatibility shim for standalone adapter discovery; the manager captures once and slices by name.
56
+ mockedCaptureProcessSnapshot.mockImplementation(async (names) => (
57
+ enrichProcesses(names.flatMap((name) => listAgentProcesses(name)))
58
+ ));
52
59
  mockedMatchProcessesToSessions.mockReturnValue([]);
53
60
  mockedGenerateAgentName.mockImplementation((cwd: string, pid: number) => {
54
61
  const folder = path.basename(cwd) || 'unknown';
@@ -110,6 +117,15 @@ describe('GeminiCliAdapter', () => {
110
117
  tty: 'ttys006',
111
118
  })).toBe(true);
112
119
  });
120
+
121
+ it('should recognize Windows executable and entrypoint paths', () => {
122
+ expect(adapter.canHandle({
123
+ pid: 7,
124
+ command: 'C:\\tools\\node.exe C:\\lib\\gemini.js',
125
+ cwd: 'C:\\repo',
126
+ tty: '',
127
+ })).toBe(true);
128
+ });
113
129
  });
114
130
 
115
131
  describe('detectAgents', () => {
@@ -144,7 +160,7 @@ describe('GeminiCliAdapter', () => {
144
160
  it('should return process-only agents when no session files exist for the process', async () => {
145
161
  const proc: ProcessInfo = {
146
162
  pid: 1234,
147
- command: 'gemini',
163
+ command: 'node /usr/local/bin/gemini',
148
164
  cwd: '/repo',
149
165
  tty: 'ttys001',
150
166
  startTime: new Date('2026-04-18T00:00:00Z'),
@@ -257,7 +273,7 @@ describe('GeminiCliAdapter', () => {
257
273
 
258
274
  const proc: ProcessInfo = {
259
275
  pid: 42,
260
- command: 'gemini',
276
+ command: 'node /usr/local/bin/gemini',
261
277
  cwd,
262
278
  tty: 'ttys001',
263
279
  startTime: new Date('2026-04-18T00:00:00Z'),
@@ -383,7 +399,7 @@ describe('GeminiCliAdapter', () => {
383
399
 
384
400
  const proc: ProcessInfo = {
385
401
  pid: 7,
386
- command: 'gemini',
402
+ command: 'node /usr/local/bin/gemini',
387
403
  cwd: procCwd,
388
404
  tty: 'ttys001',
389
405
  startTime: new Date(),
@@ -13,7 +13,7 @@ import * as path from 'path';
13
13
  import { GrokCliAdapter } from '../../adapters/GrokCliAdapter.js';
14
14
  import type { ProcessInfo } from '../../adapters/AgentAdapter.js';
15
15
  import { AgentStatus } from '../../adapters/AgentAdapter.js';
16
- import { listAgentProcesses, enrichProcesses } from '../../utils/process.js';
16
+ import { listAgentProcesses, enrichProcesses, captureProcessSnapshot } from '../../utils/process.js';
17
17
  import { generateAgentName } from '../../utils/matching.js';
18
18
 
19
19
  vi.mock('../../utils/process.js', async (importOriginal) => {
@@ -22,6 +22,7 @@ vi.mock('../../utils/process.js', async (importOriginal) => {
22
22
  ...actual,
23
23
  listAgentProcesses: vi.fn(),
24
24
  enrichProcesses: vi.fn(),
25
+ captureProcessSnapshot: vi.fn(),
25
26
  };
26
27
  });
27
28
 
@@ -35,6 +36,7 @@ vi.mock('../../utils/matching.js', async (importOriginal) => {
35
36
 
36
37
  const mockedListAgentProcesses = listAgentProcesses as MockedFunction<typeof listAgentProcesses>;
37
38
  const mockedEnrichProcesses = enrichProcesses as MockedFunction<typeof enrichProcesses>;
39
+ const mockedCaptureProcessSnapshot = captureProcessSnapshot as MockedFunction<typeof captureProcessSnapshot>;
38
40
  const mockedGenerateAgentName = generateAgentName as MockedFunction<typeof generateAgentName>;
39
41
 
40
42
  const SESSION_ID = '019f16c3-5d5d-7dc3-85d1-bc629416ca2d';
@@ -64,9 +66,14 @@ describe('GrokCliAdapter', () => {
64
66
 
65
67
  mockedListAgentProcesses.mockReset();
66
68
  mockedEnrichProcesses.mockReset();
69
+ mockedCaptureProcessSnapshot.mockReset();
67
70
  mockedGenerateAgentName.mockReset();
68
71
 
69
72
  mockedEnrichProcesses.mockImplementation((procs) => procs);
73
+ // Compatibility shim for standalone adapter discovery; the manager captures once and slices by name.
74
+ mockedCaptureProcessSnapshot.mockImplementation(async (names) => (
75
+ enrichProcesses(names.flatMap((name) => listAgentProcesses(name)))
76
+ ));
70
77
  mockedGenerateAgentName.mockImplementation((c: string, pid: number) => `${path.basename(c) || 'unknown'}-${pid}`);
71
78
  });
72
79
 
@@ -9,14 +9,19 @@ import * as path from 'path';
9
9
  import { OpenCodeAdapter } from '../../adapters/OpenCodeAdapter.js';
10
10
  import type { ProcessInfo } from '../../adapters/AgentAdapter.js';
11
11
  import { AgentStatus } from '../../adapters/AgentAdapter.js';
12
- import { listAgentProcesses, enrichProcesses } from '../../utils/process.js';
12
+ import { listAgentProcesses, enrichProcesses, captureProcessSnapshot } from '../../utils/process.js';
13
13
  import { generateAgentName } from '../../utils/matching.js';
14
14
  import * as os from 'os';
15
15
 
16
- vi.mock('../../utils/process.js', () => ({
17
- listAgentProcesses: vi.fn(),
18
- enrichProcesses: vi.fn(),
19
- }));
16
+ vi.mock('../../utils/process.js', async (importOriginal) => {
17
+ const actual = await importOriginal() as typeof import('../../utils/process.js');
18
+ return {
19
+ ...actual,
20
+ listAgentProcesses: vi.fn(),
21
+ enrichProcesses: vi.fn(),
22
+ captureProcessSnapshot: vi.fn(),
23
+ };
24
+ });
20
25
 
21
26
  vi.mock('../../utils/matching.js', () => ({
22
27
  generateAgentName: vi.fn(),
@@ -25,6 +30,7 @@ vi.mock('../../utils/matching.js', () => ({
25
30
 
26
31
  const mockedListAgentProcesses = listAgentProcesses as MockedFunction<typeof listAgentProcesses>;
27
32
  const mockedEnrichProcesses = enrichProcesses as MockedFunction<typeof enrichProcesses>;
33
+ const mockedCaptureProcessSnapshot = captureProcessSnapshot as MockedFunction<typeof captureProcessSnapshot>;
28
34
  const mockedGenerateAgentName = generateAgentName as MockedFunction<typeof generateAgentName>;
29
35
 
30
36
  function makeDb(queries: {
@@ -104,9 +110,14 @@ describe('OpenCodeAdapter', () => {
104
110
 
105
111
  mockedListAgentProcesses.mockReset();
106
112
  mockedEnrichProcesses.mockReset();
113
+ mockedCaptureProcessSnapshot.mockReset();
107
114
  mockedGenerateAgentName.mockReset();
108
115
 
109
116
  mockedEnrichProcesses.mockImplementation((procs) => procs);
117
+ // Compatibility shim for standalone adapter discovery; the manager captures once and slices by name.
118
+ mockedCaptureProcessSnapshot.mockImplementation(async (names) => (
119
+ enrichProcesses(names.flatMap((name) => listAgentProcesses(name)))
120
+ ));
110
121
  mockedGenerateAgentName.mockImplementation((cwd, pid) => {
111
122
  const folder = path.basename(cwd) || 'unknown';
112
123
  return `${folder}-${pid}`;
@@ -11,13 +11,18 @@ import { PiAdapter } from '../../adapters/PiAdapter.js';
11
11
  import type { ProcessInfo } from '../../adapters/AgentAdapter.js';
12
12
  import { AgentStatus } from '../../adapters/AgentAdapter.js';
13
13
  import { AgentRegistry } from '../../utils/AgentRegistry.js';
14
- import { listAgentProcesses, enrichProcesses } from '../../utils/process.js';
14
+ import { listAgentProcesses, enrichProcesses, captureProcessSnapshot } from '../../utils/process.js';
15
15
  import { matchProcessesToSessions, generateAgentName } from '../../utils/matching.js';
16
16
 
17
- vi.mock('../../utils/process.js', () => ({
18
- listAgentProcesses: vi.fn(),
19
- enrichProcesses: vi.fn(),
20
- }));
17
+ vi.mock('../../utils/process.js', async (importOriginal) => {
18
+ const actual = await importOriginal() as typeof import('../../utils/process.js');
19
+ return {
20
+ ...actual,
21
+ listAgentProcesses: vi.fn(),
22
+ enrichProcesses: vi.fn(),
23
+ captureProcessSnapshot: vi.fn(),
24
+ };
25
+ });
21
26
 
22
27
  vi.mock('../../utils/matching.js', () => ({
23
28
  matchProcessesToSessions: vi.fn(),
@@ -26,6 +31,7 @@ vi.mock('../../utils/matching.js', () => ({
26
31
 
27
32
  const mockedListAgentProcesses = listAgentProcesses as MockedFunction<typeof listAgentProcesses>;
28
33
  const mockedEnrichProcesses = enrichProcesses as MockedFunction<typeof enrichProcesses>;
34
+ const mockedCaptureProcessSnapshot = captureProcessSnapshot as MockedFunction<typeof captureProcessSnapshot>;
29
35
  const mockedMatchProcessesToSessions = matchProcessesToSessions as MockedFunction<typeof matchProcessesToSessions>;
30
36
  const mockedGenerateAgentName = generateAgentName as MockedFunction<typeof generateAgentName>;
31
37
 
@@ -43,10 +49,15 @@ describe('PiAdapter', () => {
43
49
  adapter = new PiAdapter(new AgentRegistry(path.join(tmpHome, 'agents.json')));
44
50
  mockedListAgentProcesses.mockReset();
45
51
  mockedEnrichProcesses.mockReset();
52
+ mockedCaptureProcessSnapshot.mockReset();
46
53
  mockedMatchProcessesToSessions.mockReset();
47
54
  mockedGenerateAgentName.mockReset();
48
55
 
49
56
  mockedEnrichProcesses.mockImplementation((procs) => procs);
57
+ // Compatibility shim for standalone adapter discovery; the manager captures once and slices by name.
58
+ mockedCaptureProcessSnapshot.mockImplementation(async (names) => (
59
+ enrichProcesses(names.flatMap((name) => listAgentProcesses(name)))
60
+ ));
50
61
  mockedMatchProcessesToSessions.mockReturnValue([]);
51
62
  mockedGenerateAgentName.mockImplementation((cwd: string, pid: number) => {
52
63
  const folder = path.basename(cwd) || 'unknown';
@@ -67,6 +78,18 @@ describe('PiAdapter', () => {
67
78
  expect(adapter.canHandle({ pid: 2, command: '/usr/local/bin/PI --model x', cwd: '/repo', tty: 'ttys002' })).toBe(true);
68
79
  expect(adapter.canHandle({ pid: 3, command: 'node /opt/pi/bin/pi.js', cwd: '/repo', tty: 'ttys003' })).toBe(true);
69
80
  expect(adapter.canHandle({ pid: 4, command: 'node /repo/feature-pi-adapter/script.js', cwd: '/repo', tty: 'ttys004' })).toBe(false);
81
+ expect(adapter.canHandle({ pid: 5, command: 'C:\\tools\\node.exe C:\\lib\\pi.js', cwd: '/repo', tty: 'ttys005' })).toBe(true);
82
+ });
83
+
84
+ it('rejects foreign executables from a hand-built snapshot context', async () => {
85
+ const foreign: ProcessInfo = {
86
+ pid: 6,
87
+ command: 'claude --resume /Users/x/repos/pi/session.jsonl',
88
+ cwd: '/repo',
89
+ tty: 'ttys006',
90
+ };
91
+
92
+ expect(await adapter.detectAgents({ processes: [foreign] })).toEqual([]);
70
93
  });
71
94
 
72
95
  it('maps a running Pi process to the tracker session for its PID', async () => {
@@ -4,7 +4,7 @@
4
4
 
5
5
  import type { MockedFunction } from 'vitest';
6
6
 
7
- import { execFileSync } from 'child_process';
7
+ import { execFile, execFileSync } from 'child_process';
8
8
  import {
9
9
  listAgentProcesses,
10
10
  batchGetProcessCwds,
@@ -12,13 +12,84 @@ import {
12
12
  enrichProcesses,
13
13
  findWrapperProcess,
14
14
  findWrapperProcessPids,
15
+ captureProcessSnapshot,
16
+ filterByProcessNames,
15
17
  } from '../../utils/process.js';
16
18
 
17
19
  vi.mock('child_process', () => ({
20
+ execFile: vi.fn(),
18
21
  execFileSync: vi.fn(),
19
22
  }));
20
23
 
21
24
  const mockedExecFileSync = execFileSync as MockedFunction<typeof execFileSync>;
25
+ const mockedExecFile = execFile as unknown as MockedFunction<(
26
+ file: string,
27
+ args: readonly string[],
28
+ options: object,
29
+ callback: (error: Error | null, stdout: string, stderr: string) => void,
30
+ ) => void>;
31
+
32
+ describe('captureProcessSnapshot', () => {
33
+ beforeEach(() => {
34
+ mockedExecFile.mockReset();
35
+ mockedExecFileSync.mockReset();
36
+ });
37
+
38
+ it('captures and enriches relevant processes without synchronous scans', async () => {
39
+ mockedExecFile.mockImplementation((file, args, _options, callback) => {
40
+ queueMicrotask(() => {
41
+ if (file === 'ps' && args.includes('-axo')) {
42
+ callback(null,
43
+ '100 1 s001 /usr/bin/claude --resume abc\n' +
44
+ '200 1 s002 C:\\\\tools\\\\node.exe C:\\\\bin\\\\pi.js\n' +
45
+ '300 1 s003 /usr/bin/unrelated\n',
46
+ '');
47
+ return;
48
+ }
49
+ if (file === 'lsof') {
50
+ callback(null, 'p100\nn/projects/claude\np200\nn/projects/pi\n', '');
51
+ return;
52
+ }
53
+ if (file === 'ps' && args.some((arg) => arg.includes('lstart='))) {
54
+ callback(null,
55
+ '100 Wed Mar 18 23:18:01 2026\n' +
56
+ '200 Thu Mar 19 10:00:00 2026\n',
57
+ '');
58
+ return;
59
+ }
60
+ callback(new Error(`unexpected command: ${file} ${args.join(' ')}`), '', '');
61
+ });
62
+ });
63
+
64
+ const snapshot = await captureProcessSnapshot(['claude', 'node']);
65
+
66
+ expect(snapshot.map((process) => process.pid)).toEqual([100, 200]);
67
+ expect(snapshot.map((process) => process.cwd)).toEqual(['/projects/claude', '/projects/pi']);
68
+ expect(snapshot.map((process) => process.startTime)).toEqual([
69
+ expect.any(Date),
70
+ expect.any(Date),
71
+ ]);
72
+ expect(mockedExecFileSync).not.toHaveBeenCalled();
73
+ expect(mockedExecFile.mock.calls.filter(([, args]) => args.includes('-axo'))).toHaveLength(1);
74
+ for (const [, , options] of mockedExecFile.mock.calls) {
75
+ expect(options).toMatchObject({ encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 });
76
+ expect(options).not.toHaveProperty('stdio');
77
+ }
78
+ });
79
+ });
80
+
81
+ describe('filterByProcessNames', () => {
82
+ it('matches only argv[0] and normalizes Windows paths and executable suffixes', () => {
83
+ const processes: ProcessInfo[] = [
84
+ { pid: 1, command: 'C:\\tools\\node.exe C:\\bin\\pi.js', cwd: '', tty: '' },
85
+ { pid: 2, command: '/usr/local/bin/node /opt/gemini.js', cwd: '', tty: '' },
86
+ { pid: 3, command: 'codex exec --cd C:\\repos\\node', cwd: '', tty: '' },
87
+ ];
88
+
89
+ expect(filterByProcessNames(processes, ['node'])).toEqual([processes[0], processes[1]]);
90
+ expect(filterByProcessNames(processes, ['node.exe'])).toEqual([processes[0], processes[1]]);
91
+ });
92
+ });
22
93
 
23
94
  describe('listAgentProcesses', () => {
24
95
  beforeEach(() => {
@@ -59,7 +59,7 @@ export interface ProcessInfo {
59
59
  /** Process ID */
60
60
  pid: number;
61
61
 
62
- /** Parent process ID, populated by listAgentProcesses when available */
62
+ /** Parent process ID, populated by process discovery when available */
63
63
  ppid?: number;
64
64
 
65
65
  /** Process command */
@@ -71,7 +71,7 @@ export interface ProcessInfo {
71
71
  /** Terminal TTY (e.g., "ttys030") */
72
72
  tty: string;
73
73
 
74
- /** Process start time, populated by enrichProcesses */
74
+ /** Process start time, populated by process enrichment */
75
75
  startTime?: Date;
76
76
  }
77
77
 
@@ -148,6 +148,11 @@ export interface ListSessionsOptions {
148
148
  type?: AgentType;
149
149
  }
150
150
 
151
+ export interface AgentDetectionContext {
152
+ /** One enriched process snapshot shared across this manager refresh. */
153
+ readonly processes: readonly ProcessInfo[];
154
+ }
155
+
151
156
  /**
152
157
  * Agent Adapter Interface
153
158
  *
@@ -157,11 +162,14 @@ export interface AgentAdapter {
157
162
  /** Type of agent this adapter handles */
158
163
  readonly type: AgentType;
159
164
 
165
+ /** Executable basenames required for shared process discovery. */
166
+ readonly processNames?: readonly string[];
167
+
160
168
  /**
161
169
  * Detect running agents of this type
162
170
  * @returns List of detected agents
163
171
  */
164
- detectAgents(): Promise<AgentInfo[]>;
172
+ detectAgents(context?: AgentDetectionContext): Promise<AgentInfo[]>;
165
173
 
166
174
  /**
167
175
  * Check if this adapter can handle the given process
@@ -7,9 +7,10 @@ import type {
7
7
  ConversationMessage,
8
8
  SessionSummary,
9
9
  ListSessionsOptions,
10
+ AgentDetectionContext,
10
11
  } from './AgentAdapter.js';
11
12
  import { AgentStatus } from './AgentAdapter.js';
12
- import { listAgentProcesses, enrichProcesses } from '../utils/process.js';
13
+ import { captureProcessSnapshot, executableBasename, filterByProcessNames } from '../utils/process.js';
13
14
  import { batchGetSessionFileBirthtimes, isDirectory, listJsonl, safeReaddir, safeStat } from '../utils/session.js';
14
15
  import type { SessionFile } from '../utils/session.js';
15
16
  import { matchProcessesToSessions, generateAgentName } from '../utils/matching.js';
@@ -60,14 +61,15 @@ const PID_FILE_STALENESS_MS = 60000;
60
61
  * Claude Code Adapter
61
62
  *
62
63
  * Detects Claude Code agents by:
63
- * 1. Finding running claude processes via shared listAgentProcesses()
64
- * 2. Enriching with CWD and start times via shared enrichProcesses()
64
+ * 1. Filtering Claude processes from a shared asynchronous process snapshot
65
+ * 2. Using snapshot CWD and start-time enrichment
65
66
  * 3. Attempting authoritative PID-file matching via ~/.claude/sessions/<pid>.json
66
67
  * 4. Falling back to CWD+birthtime heuristic (matchProcessesToSessions) for processes without a PID file
67
68
  * 5. Extracting summary from last user message in session JSONL
68
69
  */
69
70
  export class ClaudeCodeAdapter implements AgentAdapter {
70
71
  readonly type = 'claude' as const;
72
+ readonly processNames = ['claude'] as const;
71
73
 
72
74
  private projectsDir: string;
73
75
  private sessionsDir: string;
@@ -85,13 +87,14 @@ export class ClaudeCodeAdapter implements AgentAdapter {
85
87
  }
86
88
 
87
89
  private isClaudeExecutable(command: string): boolean {
88
- const executable = command.trim().split(/\s+/)[0] || '';
89
- const base = path.basename(executable).toLowerCase();
90
+ const base = executableBasename(command);
90
91
  return base === 'claude' || base === 'claude.exe';
91
92
  }
92
93
 
93
- async detectAgents(): Promise<AgentInfo[]> {
94
- const processes = enrichProcesses(listAgentProcesses('claude'));
94
+ async detectAgents(context?: AgentDetectionContext): Promise<AgentInfo[]> {
95
+ const snapshot = context?.processes ?? await captureProcessSnapshot(this.processNames);
96
+ const relevant = filterByProcessNames(snapshot, this.processNames);
97
+ const processes = relevant.filter((process) => this.canHandle(process));
95
98
  if (processes.length === 0) {
96
99
  return [];
97
100
  }
@@ -2,8 +2,8 @@
2
2
  * Codex Adapter
3
3
  *
4
4
  * Detects running Codex agents by:
5
- * 1. Finding running codex processes via shared listAgentProcesses()
6
- * 2. Enriching with CWD and start times via shared enrichProcesses()
5
+ * 1. Filtering Codex processes from a shared asynchronous process snapshot
6
+ * 2. Using snapshot CWD and start-time enrichment
7
7
  * 3. Matching exact PID-to-session metadata from ~/.codex/ai-devkit/sessions.json
8
8
  * 4. Discovering session files from ~/.codex/sessions/YYYY/MM/DD/ via shared batchGetSessionFileBirthtimes()
9
9
  * 5. Setting resolvedCwd from session_meta first line
@@ -20,9 +20,10 @@ import type {
20
20
  ConversationMessage,
21
21
  SessionSummary,
22
22
  ListSessionsOptions,
23
+ AgentDetectionContext,
23
24
  } from './AgentAdapter.js';
24
25
  import { AgentStatus } from './AgentAdapter.js';
25
- import { listAgentProcesses, enrichProcesses } from '../utils/process.js';
26
+ import { captureProcessSnapshot, executableBasename, filterByProcessNames } from '../utils/process.js';
26
27
  import { batchGetSessionFileBirthtimes, isDirectory, safeReadFile, safeReaddir, safeStat } from '../utils/session.js';
27
28
  import type { SessionFile } from '../utils/session.js';
28
29
  import { matchProcessesToSessions, generateAgentName } from '../utils/matching.js';
@@ -88,6 +89,7 @@ interface MappingMatchResult {
88
89
 
89
90
  export class CodexAdapter implements AgentAdapter {
90
91
  readonly type = 'codex' as const;
92
+ readonly processNames = ['codex'] as const;
91
93
 
92
94
  private static readonly IDLE_THRESHOLD_MINUTES = 5;
93
95
  /** Include session files around process start day to recover long-lived processes. */
@@ -111,8 +113,10 @@ export class CodexAdapter implements AgentAdapter {
111
113
  /**
112
114
  * Detect running Codex agents
113
115
  */
114
- async detectAgents(): Promise<AgentInfo[]> {
115
- const processes = enrichProcesses(listAgentProcesses('codex'));
116
+ async detectAgents(context?: AgentDetectionContext): Promise<AgentInfo[]> {
117
+ const snapshot = context?.processes ?? await captureProcessSnapshot(this.processNames);
118
+ const relevant = filterByProcessNames(snapshot, this.processNames);
119
+ const processes = relevant.filter((process) => this.canHandle(process));
116
120
  if (processes.length === 0) return [];
117
121
 
118
122
  const { cachedAgents, remaining } = this.tryRegistryCache(processes);
@@ -657,8 +661,7 @@ export class CodexAdapter implements AgentAdapter {
657
661
  }
658
662
 
659
663
  private isCodexExecutable(command: string): boolean {
660
- const executable = command.trim().split(/\s+/)[0] || '';
661
- const base = path.basename(executable).toLowerCase();
664
+ const base = executableBasename(command);
662
665
  return base === 'codex' || base === 'codex.exe';
663
666
  }
664
667
 
@@ -2,8 +2,8 @@
2
2
  * Copilot Adapter
3
3
  *
4
4
  * Detects running GitHub Copilot CLI agents by:
5
- * 1. Finding running copilot processes via shared listAgentProcesses()
6
- * 2. Enriching with CWD and start times via shared enrichProcesses()
5
+ * 1. Filtering Copilot processes from a shared asynchronous process snapshot
6
+ * 2. Using snapshot CWD and start-time enrichment
7
7
  * 3. Mapping active ~/.copilot/session-state/{sessionId}/inuse.{pid}.lock files to processes
8
8
  * 4. Reading events.jsonl as the primary session/conversation source
9
9
  * 5. Reading workspace.yaml as a flat fallback metadata source
@@ -17,9 +17,16 @@ import type {
17
17
  ListSessionsOptions,
18
18
  ProcessInfo,
19
19
  SessionSummary,
20
+ AgentDetectionContext,
20
21
  } from './AgentAdapter.js';
21
22
  import { AgentStatus } from './AgentAdapter.js';
22
- import { enrichProcesses, findWrapperProcess, findWrapperProcessPids, listAgentProcesses } from '../utils/process.js';
23
+ import {
24
+ captureProcessSnapshot,
25
+ executableBasename,
26
+ filterByProcessNames,
27
+ findWrapperProcess,
28
+ findWrapperProcessPids,
29
+ } from '../utils/process.js';
23
30
  import { generateAgentName } from '../utils/matching.js';
24
31
  import { isDirectory, safeReadFile, safeReaddir, safeStat } from '../utils/session.js';
25
32
  import { AgentRegistry, type RegistryEntry } from '../utils/AgentRegistry.js';
@@ -89,6 +96,7 @@ interface CopilotEventSummary {
89
96
 
90
97
  export class CopilotAdapter implements AgentAdapter {
91
98
  readonly type = 'copilot' as const;
99
+ readonly processNames = ['copilot'] as const;
92
100
 
93
101
  private static readonly IDLE_THRESHOLD_MINUTES = 5;
94
102
  private static readonly VERBOSE_SYSTEM_EVENTS = new Set([
@@ -120,8 +128,10 @@ export class CopilotAdapter implements AgentAdapter {
120
128
  return this.isCopilotExecutable(processInfo.command);
121
129
  }
122
130
 
123
- async detectAgents(): Promise<AgentInfo[]> {
124
- const processes = enrichProcesses(listAgentProcesses('copilot'));
131
+ async detectAgents(context?: AgentDetectionContext): Promise<AgentInfo[]> {
132
+ const snapshot = context?.processes ?? await captureProcessSnapshot(this.processNames);
133
+ const relevant = filterByProcessNames(snapshot, this.processNames);
134
+ const processes = relevant.filter((process) => this.canHandle(process));
125
135
  if (processes.length === 0) return [];
126
136
 
127
137
  const processByPid = new Map(processes.map((proc) => [proc.pid, proc]));
@@ -457,8 +467,7 @@ export class CopilotAdapter implements AgentAdapter {
457
467
  }
458
468
 
459
469
  private isCopilotExecutable(command: string): boolean {
460
- const executable = command.trim().split(/\s+/)[0] || '';
461
- const base = path.basename(executable).toLowerCase();
470
+ const base = executableBasename(command);
462
471
  return base === 'copilot' || base === 'copilot.exe';
463
472
  }
464
473
  }
@@ -2,8 +2,8 @@
2
2
  * Gemini CLI Adapter
3
3
  *
4
4
  * Detects running Gemini CLI agents by:
5
- * 1. Finding running gemini processes via shared listAgentProcesses()
6
- * 2. Enriching with CWD and start times via shared enrichProcesses()
5
+ * 1. Filtering Gemini Node processes from a shared asynchronous process snapshot
6
+ * 2. Using snapshot CWD and start-time enrichment
7
7
  * 3. Discovering session files from ~/.gemini/tmp/<shortId>/chats/session-*.json
8
8
  * 4. Matching sessions to processes via shared matchProcessesToSessions()
9
9
  * using sha256(cwd) === session.projectHash as the resolvedCwd source
@@ -20,9 +20,16 @@ import type {
20
20
  ConversationMessage,
21
21
  SessionSummary,
22
22
  ListSessionsOptions,
23
+ AgentDetectionContext,
23
24
  } from './AgentAdapter.js';
24
25
  import { AgentStatus } from './AgentAdapter.js';
25
- import { listAgentProcesses, enrichProcesses, findWrapperProcess, findWrapperProcessPids } from '../utils/process.js';
26
+ import {
27
+ captureProcessSnapshot,
28
+ executableBasename,
29
+ filterByProcessNames,
30
+ findWrapperProcess,
31
+ findWrapperProcessPids,
32
+ } from '../utils/process.js';
26
33
  import { isDirectory, safeReadFile, safeReaddir, safeStat } from '../utils/session.js';
27
34
  import type { SessionFile } from '../utils/session.js';
28
35
  import { matchProcessesToSessions, generateAgentName } from '../utils/matching.js';
@@ -81,6 +88,7 @@ interface GeminiSession {
81
88
 
82
89
  export class GeminiCliAdapter implements AgentAdapter {
83
90
  readonly type = 'gemini_cli' as const;
91
+ readonly processNames = ['node'] as const;
84
92
 
85
93
  private static readonly IDLE_THRESHOLD_MINUTES = 5;
86
94
  private static readonly SESSION_FILE_PREFIX = 'session-';
@@ -109,12 +117,13 @@ export class GeminiCliAdapter implements AgentAdapter {
109
117
  * binary). The primary running process is therefore the Node runtime
110
118
  * itself, and `ps aux` lists it as `node /path/to/gemini ...` with
111
119
  * argv[0] = `node`. We scan the Node process pool via the shared
112
- * helper and keep only those whose command line references the gemini
120
+ * snapshot and keep only those whose command line references the gemini
113
121
  * executable or script via isGeminiExecutable().
114
122
  */
115
- async detectAgents(): Promise<AgentInfo[]> {
116
- const nodeProcesses = enrichProcesses(listAgentProcesses('node'));
117
- const processes = nodeProcesses.filter((proc) => this.isGeminiExecutable(proc.command));
123
+ async detectAgents(context?: AgentDetectionContext): Promise<AgentInfo[]> {
124
+ const snapshot = context?.processes ?? await captureProcessSnapshot(this.processNames);
125
+ const relevant = filterByProcessNames(snapshot, this.processNames);
126
+ const processes = relevant.filter((process) => this.canHandle(process));
118
127
  if (processes.length === 0) return [];
119
128
 
120
129
  const wrapperPids = findWrapperProcessPids(processes);
@@ -541,7 +550,7 @@ export class GeminiCliAdapter implements AgentAdapter {
541
550
  // other adapters' argv[0]-only check because the Node-script
542
551
  // distribution puts the real gemini path in argv[1..], not argv[0].
543
552
  for (const token of command.trim().split(/\s+/)) {
544
- const base = path.basename(token).toLowerCase();
553
+ const base = executableBasename(token);
545
554
  if (base === 'gemini' || base === 'gemini.exe' || base === 'gemini.js') {
546
555
  return true;
547
556
  }
@@ -6,9 +6,10 @@ import type {
6
6
  ConversationMessage,
7
7
  SessionSummary,
8
8
  ListSessionsOptions,
9
+ AgentDetectionContext,
9
10
  } from './AgentAdapter.js';
10
11
  import { AgentStatus } from './AgentAdapter.js';
11
- import { listAgentProcesses, enrichProcesses } from '../utils/process.js';
12
+ import { captureProcessSnapshot, executableBasename, filterByProcessNames } from '../utils/process.js';
12
13
  import { isDirectory, safeReadFile, safeReaddir, safeStat } from '../utils/session.js';
13
14
  import { generateAgentName } from '../utils/matching.js';
14
15
 
@@ -16,7 +17,7 @@ import { generateAgentName } from '../utils/matching.js';
16
17
  * Grok Build CLI Adapter
17
18
  *
18
19
  * Detects running Grok Build CLI agents by:
19
- * 1. Finding running `grok` processes via shared listAgentProcesses() — Grok is
20
+ * 1. Filtering `grok` processes from a shared asynchronous snapshot — Grok is
20
21
  * a native binary at ~/.grok/bin/grok, so argv[0] basename is `grok`.
21
22
  * 2. Resolving each live process to its working directory via
22
23
  * ~/.grok/active_sessions.json, which Grok maintains as a list of
@@ -69,6 +70,7 @@ interface GrokSession {
69
70
 
70
71
  export class GrokCliAdapter implements AgentAdapter {
71
72
  readonly type = 'grok_cli' as const;
73
+ readonly processNames = ['grok'] as const;
72
74
 
73
75
  private base: string;
74
76
  private sessionsDir: string;
@@ -87,13 +89,14 @@ export class GrokCliAdapter implements AgentAdapter {
87
89
  }
88
90
 
89
91
  private isGrokExecutable(command: string): boolean {
90
- const executable = command.trim().split(/\s+/)[0] || '';
91
- const base = path.basename(executable).toLowerCase();
92
+ const base = executableBasename(command);
92
93
  return base === 'grok' || base === 'grok.exe';
93
94
  }
94
95
 
95
- async detectAgents(): Promise<AgentInfo[]> {
96
- const processes = enrichProcesses(listAgentProcesses('grok'));
96
+ async detectAgents(context?: AgentDetectionContext): Promise<AgentInfo[]> {
97
+ const snapshot = context?.processes ?? await captureProcessSnapshot(this.processNames);
98
+ const relevant = filterByProcessNames(snapshot, this.processNames);
99
+ const processes = relevant.filter((process) => this.canHandle(process));
97
100
  if (processes.length === 0) {
98
101
  return [];
99
102
  }