Skip to content

Commit b4bdabd

Browse files
committed
fix(agent-manager): detect macOS terminal app processes
1 parent 36e4b29 commit b4bdabd

2 files changed

Lines changed: 103 additions & 3 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { execFile } from 'child_process';
2+
import type { MockedFunction } from 'vitest';
3+
4+
import { TerminalFocusManager, TerminalType } from '../../terminal/TerminalFocusManager.js';
5+
import { getProcessTty } from '../../utils/process.js';
6+
7+
vi.mock('child_process', () => ({
8+
execFile: vi.fn(),
9+
}));
10+
11+
vi.mock('../../utils/process.js', async () => {
12+
const actual = await vi.importActual<typeof import('../../utils/process.js')>('../../utils/process.js');
13+
return {
14+
...actual,
15+
getProcessTty: vi.fn(),
16+
};
17+
});
18+
19+
type ExecFileCb = (err: Error | null, result?: { stdout: string; stderr: string }) => void;
20+
const mockedExecFile = execFile as unknown as MockedFunction<
21+
(cmd: string, args: string[], cb: ExecFileCb) => void
22+
>;
23+
const mockedGetProcessTty = getProcessTty as MockedFunction<typeof getProcessTty>;
24+
25+
function setExecFileHandler(handler: (cmd: string, args: string[]) => string | Error) {
26+
mockedExecFile.mockImplementation((cmd, args, cb) => {
27+
const result = handler(cmd, args);
28+
if (result instanceof Error) cb(result);
29+
else cb(null, { stdout: result, stderr: '' });
30+
});
31+
}
32+
33+
describe('TerminalFocusManager', () => {
34+
beforeEach(() => {
35+
mockedExecFile.mockReset();
36+
mockedGetProcessTty.mockReset();
37+
mockedGetProcessTty.mockReturnValue('ttys000');
38+
});
39+
40+
it('finds iTerm2 when the process is listed by full app binary path', async () => {
41+
setExecFileHandler((cmd, args) => {
42+
if (cmd === 'tmux') return new Error('tmux not running');
43+
if (cmd === 'pgrep') return new Error('pgrep did not match GUI app');
44+
if (cmd === 'ps' && args.join(' ') === '-Axo comm') {
45+
return '/Applications/iTerm.app/Contents/MacOS/iTerm2\n';
46+
}
47+
if (cmd === 'osascript') return 'found\n';
48+
return '';
49+
});
50+
51+
const location = await new TerminalFocusManager().findTerminal(123);
52+
53+
expect(location).toEqual({
54+
type: TerminalType.ITERM2,
55+
identifier: '/dev/ttys000',
56+
tty: '/dev/ttys000',
57+
});
58+
expect(mockedExecFile).not.toHaveBeenCalledWith(
59+
'pgrep',
60+
expect.any(Array),
61+
expect.any(Function),
62+
);
63+
});
64+
65+
it('finds Terminal.app when the process is listed by app bundle path', async () => {
66+
setExecFileHandler((cmd, args) => {
67+
if (cmd === 'tmux') return new Error('tmux not running');
68+
if (cmd === 'pgrep') return new Error('pgrep did not match GUI app');
69+
if (cmd === 'ps' && args.join(' ') === '-Axo comm') {
70+
return '/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal\n';
71+
}
72+
if (cmd === 'osascript') {
73+
const script = args[1] ?? '';
74+
return script.includes('tell application "Terminal"') ? 'found\n' : '';
75+
}
76+
return '';
77+
});
78+
79+
const location = await new TerminalFocusManager().findTerminal(123);
80+
81+
expect(location).toEqual({
82+
type: TerminalType.TERMINAL_APP,
83+
identifier: '/dev/ttys000',
84+
tty: '/dev/ttys000',
85+
});
86+
expect(mockedExecFile).not.toHaveBeenCalledWith(
87+
'pgrep',
88+
expect.any(Array),
89+
expect.any(Function),
90+
);
91+
});
92+
});

‎packages/agent-manager/src/terminal/TerminalFocusManager.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export class TerminalFocusManager {
9898

9999
private async findITerm2Session(tty: string): Promise<TerminalLocation | null> {
100100
try {
101-
// Check if iTerm2 is running first to avoid launching it
102-
await execFileAsync('pgrep', ['-x', 'iTerm2']);
101+
// Check if iTerm2 is running first to avoid launching it.
102+
if (!await this.isProcessRunning('iTerm2')) return null;
103103
} catch {
104104
return null;
105105
}
@@ -137,7 +137,7 @@ export class TerminalFocusManager {
137137
private async findTerminalAppWindow(tty: string): Promise<TerminalLocation | null> {
138138
try {
139139
// Check if Terminal.app is running
140-
await execFileAsync('pgrep', ['-x', 'Terminal']);
140+
if (!await this.isProcessRunning('Terminal')) return null;
141141
} catch {
142142
return null;
143143
}
@@ -170,6 +170,14 @@ export class TerminalFocusManager {
170170
return null;
171171
}
172172

173+
private async isProcessRunning(name: string): Promise<boolean> {
174+
const { stdout } = await execFileAsync('ps', ['-Axo', 'comm']);
175+
return stdout
176+
.split('\n')
177+
.map((line) => line.trim())
178+
.some((command) => command === name || command.endsWith(`/${name}`));
179+
}
180+
173181
private async focusTmuxPane(identifier: string): Promise<boolean> {
174182
try {
175183
await execFileAsync('tmux', ['switch-client', '-t', identifier]);

0 commit comments

Comments
 (0)