@ai-devkit/agent-manager 0.22.0 → 0.24.0

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 (34) hide show
  1. package/README.md +3 -0
  2. package/dist/__tests__/AgentManager.test.js +9 -1
  3. package/dist/__tests__/AgentManager.test.js.map +1 -1
  4. package/dist/__tests__/terminal/TerminalFocusManager.test.js +180 -0
  5. package/dist/__tests__/terminal/TerminalFocusManager.test.js.map +1 -1
  6. package/dist/__tests__/terminal/TtyWriter.test.js +232 -8
  7. package/dist/__tests__/terminal/TtyWriter.test.js.map +1 -1
  8. package/dist/__tests__/utils/agent-requests.test.js +90 -0
  9. package/dist/__tests__/utils/agent-requests.test.js.map +1 -0
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -0
  13. package/dist/index.js.map +1 -1
  14. package/dist/terminal/TerminalFocusManager.d.ts +11 -0
  15. package/dist/terminal/TerminalFocusManager.d.ts.map +1 -1
  16. package/dist/terminal/TerminalFocusManager.js +84 -10
  17. package/dist/terminal/TerminalFocusManager.js.map +1 -1
  18. package/dist/terminal/TtyWriter.d.ts +20 -0
  19. package/dist/terminal/TtyWriter.d.ts.map +1 -1
  20. package/dist/terminal/TtyWriter.js +198 -9
  21. package/dist/terminal/TtyWriter.js.map +1 -1
  22. package/dist/utils/agent-requests.d.ts +10 -0
  23. package/dist/utils/agent-requests.d.ts.map +1 -0
  24. package/dist/utils/agent-requests.js +22 -0
  25. package/dist/utils/agent-requests.js.map +1 -0
  26. package/package.json +1 -1
  27. package/src/__tests__/AgentManager.test.ts +7 -1
  28. package/src/__tests__/terminal/TerminalFocusManager.test.ts +187 -0
  29. package/src/__tests__/terminal/TtyWriter.test.ts +255 -6
  30. package/src/__tests__/utils/agent-requests.test.ts +74 -0
  31. package/src/index.ts +3 -0
  32. package/src/terminal/TerminalFocusManager.ts +103 -11
  33. package/src/terminal/TtyWriter.ts +184 -7
  34. package/src/utils/agent-requests.ts +28 -0
@@ -2,6 +2,7 @@ import { execFile } from 'child_process';
2
2
  import type { MockedFunction } from 'vitest';
3
3
 
4
4
  import { TerminalFocusManager, TerminalType } from '../../terminal/TerminalFocusManager.js';
5
+ import type { TerminalLocation } from '../../terminal/TerminalFocusManager.js';
5
6
  import { getProcessTty } from '../../utils/process.js';
6
7
 
7
8
  vi.mock('child_process', () => ({
@@ -62,6 +63,192 @@ describe('TerminalFocusManager', () => {
62
63
  );
63
64
  });
64
65
 
66
+ describe('WezTerm', () => {
67
+ const listArgs = ['cli', 'list', '--format', 'json'];
68
+
69
+ it('finds the WezTerm pane whose tty_name matches the agent tty', async () => {
70
+ setExecFileHandler((cmd, args) => {
71
+ if (cmd === 'tmux') return new Error('tmux not running');
72
+ if (cmd === 'wezterm' && args.join(' ') === listArgs.join(' ')) {
73
+ return JSON.stringify([
74
+ { pane_id: 3, tty_name: '/dev/ttys099', cwd: '/x' },
75
+ { pane_id: 7, tty_name: '/dev/ttys000', cwd: '/y' },
76
+ ]);
77
+ }
78
+ return '';
79
+ });
80
+
81
+ const location = await new TerminalFocusManager().findTerminal(123);
82
+
83
+ expect(location).toEqual({
84
+ type: TerminalType.WEZTERM,
85
+ identifier: '7',
86
+ tty: '/dev/ttys000',
87
+ });
88
+ });
89
+
90
+ it('skips the macOS AppleScript probes when WezTerm matches', async () => {
91
+ setExecFileHandler((cmd) => {
92
+ if (cmd === 'tmux') return new Error('tmux not running');
93
+ if (cmd === 'wezterm') {
94
+ return JSON.stringify([{ pane_id: 7, tty_name: '/dev/ttys000' }]);
95
+ }
96
+ return '';
97
+ });
98
+
99
+ await new TerminalFocusManager().findTerminal(123);
100
+
101
+ expect(mockedExecFile).not.toHaveBeenCalledWith(
102
+ 'osascript',
103
+ expect.any(Array),
104
+ expect.any(Function),
105
+ );
106
+ });
107
+
108
+ it('returns UNKNOWN when WezTerm is installed but no pane matches the tty', async () => {
109
+ setExecFileHandler((cmd) => {
110
+ if (cmd === 'tmux') return new Error('tmux not running');
111
+ if (cmd === 'wezterm') {
112
+ return JSON.stringify([{ pane_id: 7, tty_name: '/dev/ttys099' }]);
113
+ }
114
+ return '';
115
+ });
116
+
117
+ const location = await new TerminalFocusManager().findTerminal(123);
118
+
119
+ expect(location?.type).toBe(TerminalType.UNKNOWN);
120
+ });
121
+
122
+ it('returns UNKNOWN (without throwing) on malformed JSON from wezterm', async () => {
123
+ setExecFileHandler((cmd) => {
124
+ if (cmd === 'tmux') return new Error('tmux not running');
125
+ if (cmd === 'wezterm') return 'not-json{';
126
+ return '';
127
+ });
128
+
129
+ const location = await new TerminalFocusManager().findTerminal(123);
130
+
131
+ expect(location?.type).toBe(TerminalType.UNKNOWN);
132
+ });
133
+
134
+ it('returns UNKNOWN when the wezterm binary is missing', async () => {
135
+ setExecFileHandler((cmd) => {
136
+ if (cmd === 'tmux') return new Error('tmux not running');
137
+ if (cmd === 'wezterm') return new Error('spawn wezterm ENOENT');
138
+ return '';
139
+ });
140
+
141
+ const location = await new TerminalFocusManager().findTerminal(123);
142
+
143
+ expect(location?.type).toBe(TerminalType.UNKNOWN);
144
+ });
145
+
146
+ it('prefers tmux over WezTerm (tmux-inside-WezTerm resolves to tmux)', async () => {
147
+ setExecFileHandler((cmd) => {
148
+ if (cmd === 'tmux') {
149
+ return `/dev/ttys000|my:0.1`;
150
+ }
151
+ if (cmd === 'wezterm') {
152
+ return JSON.stringify([{ pane_id: 7, tty: '/dev/ttys000' }]);
153
+ }
154
+ return '';
155
+ });
156
+
157
+ const location = await new TerminalFocusManager().findTerminal(123);
158
+
159
+ expect(location).toEqual({
160
+ type: TerminalType.TMUX,
161
+ identifier: 'my:0.1',
162
+ tty: '/dev/ttys000',
163
+ });
164
+ // WezTerm must not even be queried when tmux already matched.
165
+ expect(mockedExecFile).not.toHaveBeenCalledWith(
166
+ 'wezterm',
167
+ expect.any(Array),
168
+ expect.any(Function),
169
+ );
170
+ });
171
+ });
172
+
173
+ describe('focusTerminal for WezTerm', () => {
174
+ const location: TerminalLocation = {
175
+ type: TerminalType.WEZTERM,
176
+ identifier: '7',
177
+ tty: '/dev/ttys000',
178
+ };
179
+
180
+ it('focuses the pane via wezterm cli activate-pane --pane-id', async () => {
181
+ setExecFileHandler((cmd, args) => {
182
+ if (cmd === 'wezterm' && args.join(' ') === 'cli activate-pane --pane-id 7') {
183
+ return '';
184
+ }
185
+ return '';
186
+ });
187
+
188
+ const ok = await new TerminalFocusManager().focusTerminal(location);
189
+
190
+ expect(ok).toBe(true);
191
+ expect(mockedExecFile).toHaveBeenCalledWith(
192
+ 'wezterm',
193
+ ['cli', 'activate-pane', '--pane-id', '7'],
194
+ expect.any(Function),
195
+ );
196
+ });
197
+
198
+ it('returns false (without throwing) when focus fails', async () => {
199
+ setExecFileHandler((cmd) => {
200
+ if (cmd === 'wezterm') return new Error('boom');
201
+ return '';
202
+ });
203
+
204
+ const ok = await new TerminalFocusManager().focusTerminal(location);
205
+
206
+ expect(ok).toBe(false);
207
+ });
208
+ });
209
+
210
+ describe('debug tracing', () => {
211
+ it('emits the matching decision path via the debug logger', async () => {
212
+ const debug = vi.fn();
213
+ setExecFileHandler((cmd, args) => {
214
+ if (cmd === 'tmux') return new Error('tmux not running');
215
+ if (cmd === 'wezterm' && args.join(' ').includes('cli list')) {
216
+ return JSON.stringify([{ pane_id: 7, tty_name: '/dev/ttys000' }]);
217
+ }
218
+ return '';
219
+ });
220
+
221
+ const location = await new TerminalFocusManager(debug).findTerminal(123);
222
+
223
+ expect(location?.type).toBe(TerminalType.WEZTERM);
224
+ const messages = debug.mock.calls.map((call) => call[0] as string);
225
+ expect(messages.some((m) => /pid=123/.test(m))).toBe(true);
226
+ expect(messages.some((m) => /tmux.*no match|tmux: no/i.test(m))).toBe(true);
227
+ expect(messages.some((m) => /wezterm/i.test(m))).toBe(true);
228
+ });
229
+
230
+ it('emits the focus decision path via the debug logger', async () => {
231
+ const debug = vi.fn();
232
+ setExecFileHandler((cmd, args) => {
233
+ if (cmd === 'wezterm' && args.join(' ') === 'cli activate-pane --pane-id 7') {
234
+ return '';
235
+ }
236
+ return '';
237
+ });
238
+
239
+ const ok = await new TerminalFocusManager(debug).focusTerminal({
240
+ type: TerminalType.WEZTERM,
241
+ identifier: '7',
242
+ tty: '/dev/ttys000',
243
+ });
244
+
245
+ expect(ok).toBe(true);
246
+ const messages = debug.mock.calls.map((call) => call[0] as string);
247
+ expect(messages.some((m) => /focusing wezterm/i.test(m))).toBe(true);
248
+ expect(messages.some((m) => /succeeded/i.test(m))).toBe(true);
249
+ });
250
+ });
251
+
65
252
  it('finds Terminal.app when the process is listed by app bundle path', async () => {
66
253
  setExecFileHandler((cmd, args) => {
67
254
  if (cmd === 'tmux') return new Error('tmux not running');
@@ -19,6 +19,7 @@ function mockExecFileSuccess(stdout = '') {
19
19
  mockedExecFile.mockImplementation((...args: unknown[]) => {
20
20
  const cb = args[args.length - 1] as (err: Error | null, result: { stdout: string }, stderr: string) => void;
21
21
  cb(null, { stdout }, '');
22
+ return { stdin: { end: vi.fn() } };
22
23
  });
23
24
  }
24
25
 
@@ -41,22 +42,36 @@ describe('TtyWriter', () => {
41
42
  tty: '/dev/ttys030',
42
43
  };
43
44
 
44
- it('sends message and Enter as separate tmux send-keys calls', async () => {
45
+ it('pastes message in bracketed paste mode and sends Enter separately', async () => {
45
46
  mockExecFileSuccess();
47
+ const message = 'line 1\nline 2\n';
46
48
 
47
- await TtyWriter.send(location, 'continue');
49
+ await TtyWriter.send(location, message);
48
50
 
49
- expect(mockedExecFile).toHaveBeenCalledWith(
51
+ const loadArgs = mockedExecFile.mock.calls[0]?.[1] as string[];
52
+ const bufferName = loadArgs[2];
53
+ expect(bufferName).toMatch(/^ai-devkit-send-/);
54
+ expect(mockedExecFile).toHaveBeenNthCalledWith(
55
+ 1,
50
56
  'tmux',
51
- ['send-keys', '-t', 'main:0.1', '-l', 'continue'],
57
+ ['load-buffer', '-b', bufferName, '-'],
52
58
  expect.any(Function),
53
59
  );
54
- expect(mockedExecFile).toHaveBeenCalledWith(
60
+ expect(mockedExecFile.mock.results[0]?.value.stdin.end)
61
+ .toHaveBeenCalledWith(message);
62
+ expect(mockedExecFile).toHaveBeenNthCalledWith(
63
+ 2,
64
+ 'tmux',
65
+ ['paste-buffer', '-t', 'main:0.1', '-b', bufferName, '-p', '-d'],
66
+ expect.any(Function),
67
+ );
68
+ expect(mockedExecFile).toHaveBeenNthCalledWith(
69
+ 3,
55
70
  'tmux',
56
71
  ['send-keys', '-t', 'main:0.1', 'Enter'],
57
72
  expect.any(Function),
58
73
  );
59
- expect(mockedExecFile).toHaveBeenCalledTimes(2);
74
+ expect(mockedExecFile).toHaveBeenCalledTimes(3);
60
75
  });
61
76
 
62
77
  it('throws on tmux failure', async () => {
@@ -186,6 +201,96 @@ describe('TtyWriter', () => {
186
201
  });
187
202
  });
188
203
 
204
+ describe('WezTerm', () => {
205
+ const location: TerminalLocation = {
206
+ type: TerminalType.WEZTERM,
207
+ identifier: '7',
208
+ tty: '/dev/ttys030',
209
+ };
210
+
211
+ it('sends the message via stdin and Enter as a separate send-text call', async () => {
212
+ mockExecFileSuccess();
213
+
214
+ await TtyWriter.send(location, 'continue');
215
+
216
+ // Step 1: message body via stdin, not argv, so prompt contents are
217
+ // not exposed through process listings.
218
+ expect(mockedExecFile).toHaveBeenCalledWith(
219
+ 'wezterm',
220
+ ['cli', 'send-text', '--pane-id', '7'],
221
+ expect.any(Function),
222
+ );
223
+ expect(mockedExecFile.mock.results[0]?.value.stdin.end)
224
+ .toHaveBeenCalledWith('continue');
225
+ // Step 2: Enter as a single carriage return (0x0d) with --no-paste,
226
+ // so the CR is delivered literally (not wrapped in paste brackets).
227
+ // execFile passes the actual CR byte (JS '\x0d'); the equivalent
228
+ // shell command is:
229
+ // wezterm cli send-text --pane-id <id> --no-paste $'\x0d'
230
+ expect(mockedExecFile).toHaveBeenCalledWith(
231
+ 'wezterm',
232
+ ['cli', 'send-text', '--pane-id', '7', '--no-paste', '\x0d'],
233
+ expect.any(Function),
234
+ );
235
+ expect(mockedExecFile).toHaveBeenCalledTimes(2);
236
+ });
237
+
238
+ it('passes the Enter byte as the carriage return (0x0d), not newline', async () => {
239
+ mockExecFileSuccess();
240
+
241
+ await TtyWriter.send(location, 'continue');
242
+
243
+ // The Enter call is the second invocation; its last argv element is
244
+ // a single byte equal to char code 13 (0x0d).
245
+ const enterCall = mockedExecFile.mock.calls[1];
246
+ const enterArgs = enterCall[1] as string[];
247
+ const enterByte = enterArgs[enterArgs.length - 1];
248
+ expect(enterByte).toHaveLength(1);
249
+ expect(enterByte.charCodeAt(0)).toBe(0x0d);
250
+ expect(enterArgs).toContain('--no-paste');
251
+ });
252
+
253
+ it('keeps the whole message out of argv and writes it verbatim to stdin', async () => {
254
+ mockExecFileSuccess();
255
+ const hostile = 'echo pwned; $(rm -rf /) `whoami` | cat\nline2';
256
+
257
+ await TtyWriter.send(location, hostile);
258
+
259
+ // The message is written verbatim to stdin, not built into a shell
260
+ // string or exposed as a process argument.
261
+ const textCall = mockedExecFile.mock.calls[0];
262
+ const textArgs = textCall[1] as string[];
263
+ expect(textArgs).toEqual(
264
+ ['cli', 'send-text', '--pane-id', '7'],
265
+ );
266
+ expect(textCall[2]).toBeTypeOf('function');
267
+ expect(mockedExecFile.mock.results[0]?.value.stdin.end)
268
+ .toHaveBeenCalledWith(hostile);
269
+ });
270
+
271
+ it('uses the pane id from location.identifier', async () => {
272
+ mockExecFileSuccess();
273
+ const pane42 = { ...location, identifier: '42' };
274
+
275
+ await TtyWriter.send(pane42, 'hi');
276
+
277
+ expect(mockedExecFile).toHaveBeenCalledWith(
278
+ 'wezterm',
279
+ ['cli', 'send-text', '--pane-id', '42'],
280
+ expect.any(Function),
281
+ );
282
+ expect(mockedExecFile.mock.results[0]?.value.stdin.end)
283
+ .toHaveBeenCalledWith('hi');
284
+ });
285
+
286
+ it('throws when the text send fails', async () => {
287
+ mockExecFileError('wezterm send-text failed');
288
+
289
+ await expect(TtyWriter.send(location, 'hello'))
290
+ .rejects.toThrow('wezterm send-text failed');
291
+ });
292
+ });
293
+
189
294
  describe('unsupported terminal', () => {
190
295
  it('throws for unknown terminal type', async () => {
191
296
  const location: TerminalLocation = {
@@ -198,4 +303,148 @@ describe('TtyWriter', () => {
198
303
  .rejects.toThrow('Cannot send input: unsupported terminal type');
199
304
  });
200
305
  });
306
+
307
+ describe('sendKey — tmux', () => {
308
+ const location: TerminalLocation = {
309
+ type: TerminalType.TMUX,
310
+ identifier: 'main:0.1',
311
+ tty: '/dev/ttys030',
312
+ };
313
+
314
+ it('sends key via tmux send-keys directly (no paste buffer, no auto-Enter)', async () => {
315
+ mockExecFileSuccess();
316
+
317
+ await TtyWriter.sendKey(location, '1');
318
+
319
+ expect(mockedExecFile).toHaveBeenCalledTimes(1);
320
+ expect(mockedExecFile).toHaveBeenCalledWith(
321
+ 'tmux',
322
+ ['send-keys', '-t', 'main:0.1', '1'],
323
+ expect.any(Function),
324
+ );
325
+ });
326
+
327
+ it('passes through named keys like Enter', async () => {
328
+ mockExecFileSuccess();
329
+ await TtyWriter.sendKey(location, 'Enter');
330
+ expect(mockedExecFile).toHaveBeenCalledWith(
331
+ 'tmux',
332
+ ['send-keys', '-t', 'main:0.1', 'Enter'],
333
+ expect.any(Function),
334
+ );
335
+ });
336
+
337
+ it('translates Esc byte (\\x1b) to the named "Escape" key', async () => {
338
+ mockExecFileSuccess();
339
+ await TtyWriter.sendKey(location, '\x1b');
340
+ expect(mockedExecFile).toHaveBeenCalledWith(
341
+ 'tmux',
342
+ ['send-keys', '-t', 'main:0.1', 'Escape'],
343
+ expect.any(Function),
344
+ );
345
+ });
346
+ });
347
+
348
+ describe('sendKey — WezTerm', () => {
349
+ const location: TerminalLocation = {
350
+ type: TerminalType.WEZTERM,
351
+ identifier: '7',
352
+ tty: '/dev/ttys030',
353
+ };
354
+
355
+ it('uses wezterm cli send-text --no-paste to deliver a raw key', async () => {
356
+ mockExecFileSuccess();
357
+
358
+ await TtyWriter.sendKey(location, '1');
359
+
360
+ expect(mockedExecFile).toHaveBeenCalledTimes(1);
361
+ expect(mockedExecFile).toHaveBeenCalledWith(
362
+ 'wezterm',
363
+ ['cli', 'send-text', '--pane-id', '7', '--no-paste', '1'],
364
+ expect.any(Function),
365
+ );
366
+ });
367
+ });
368
+
369
+ describe('sendKey — iTerm2', () => {
370
+ const location: TerminalLocation = {
371
+ type: TerminalType.ITERM2,
372
+ identifier: '/dev/ttys030',
373
+ tty: '/dev/ttys030',
374
+ };
375
+
376
+ it('uses System Events keystroke after activating the iTerm2 session', async () => {
377
+ mockExecFileSuccess('ok');
378
+
379
+ await TtyWriter.sendKey(location, '2');
380
+
381
+ const args = (mockedExecFile.mock.calls[0] as unknown[])[1] as string[];
382
+ const script = args[1];
383
+ expect(script).toContain('tell application "iTerm"');
384
+ expect(script).toContain('tell application "System Events" to keystroke "2"');
385
+ expect(mockedExecFile).toHaveBeenCalledTimes(1);
386
+ });
387
+
388
+ it('translates Esc byte (\\x1b) to AppleScript `key code 53`', async () => {
389
+ mockExecFileSuccess('ok');
390
+ await TtyWriter.sendKey(location, '\x1b');
391
+ const args = (mockedExecFile.mock.calls[0] as unknown[])[1] as string[];
392
+ const script = args[1];
393
+ expect(script).toContain('tell application "System Events" to key code 53');
394
+ expect(script).not.toContain('keystroke');
395
+ });
396
+
397
+ it('throws when session not found', async () => {
398
+ mockExecFileSuccess('not_found');
399
+ await expect(TtyWriter.sendKey(location, '1'))
400
+ .rejects.toThrow('iTerm2 session not found');
401
+ });
402
+ });
403
+
404
+ describe('sendKey — Terminal.app', () => {
405
+ const location: TerminalLocation = {
406
+ type: TerminalType.TERMINAL_APP,
407
+ identifier: '/dev/ttys030',
408
+ tty: '/dev/ttys030',
409
+ };
410
+
411
+ it('uses System Events keystroke after selecting the Terminal.app tab', async () => {
412
+ mockExecFileSuccess('ok');
413
+
414
+ await TtyWriter.sendKey(location, '3');
415
+
416
+ const args = (mockedExecFile.mock.calls[0] as unknown[])[1] as string[];
417
+ const script = args[1];
418
+ expect(script).toContain('tell application "Terminal"');
419
+ expect(script).toContain('tell application "System Events" to keystroke "3"');
420
+ expect(mockedExecFile).toHaveBeenCalledTimes(1);
421
+ });
422
+
423
+ it('translates Esc byte (\\x1b) to AppleScript `key code 53`', async () => {
424
+ mockExecFileSuccess('ok');
425
+ await TtyWriter.sendKey(location, '\x1b');
426
+ const args = (mockedExecFile.mock.calls[0] as unknown[])[1] as string[];
427
+ const script = args[1];
428
+ expect(script).toContain('tell application "System Events" to key code 53');
429
+ expect(script).not.toContain('keystroke');
430
+ });
431
+
432
+ it('throws when tab not found', async () => {
433
+ mockExecFileSuccess('not_found');
434
+ await expect(TtyWriter.sendKey(location, '1'))
435
+ .rejects.toThrow('Terminal.app tab not found');
436
+ });
437
+ });
438
+
439
+ describe('sendKey — unsupported terminal', () => {
440
+ it('throws for unknown terminal type', async () => {
441
+ const location: TerminalLocation = {
442
+ type: TerminalType.UNKNOWN,
443
+ identifier: '',
444
+ tty: '/dev/ttys030',
445
+ };
446
+ await expect(TtyWriter.sendKey(location, '1'))
447
+ .rejects.toThrow('Cannot send key: unsupported terminal type');
448
+ });
449
+ });
201
450
  });
@@ -0,0 +1,74 @@
1
+ import { mkdtempSync, rmSync, writeFileSync } from 'fs';
2
+ import { tmpdir } from 'os';
3
+ import { join } from 'path';
4
+ import { getAgentRequestPath, readLatestAgentRequest, writeAgentRequest, type AgentRequest } from '../../utils/agent-requests.js';
5
+
6
+ describe('agent-requests', () => {
7
+ let homeDir: string;
8
+
9
+ beforeEach(() => {
10
+ homeDir = mkdtempSync(join(tmpdir(), 'agent-requests-test-'));
11
+ });
12
+
13
+ afterEach(() => {
14
+ rmSync(homeDir, { recursive: true, force: true });
15
+ });
16
+
17
+ describe('getAgentRequestPath', () => {
18
+ it('returns ~/.ai-devkit/agent-requests/<sessionId>.json', () => {
19
+ expect(getAgentRequestPath(homeDir, 'abc-123')).toBe(
20
+ join(homeDir, '.ai-devkit', 'agent-requests', 'abc-123.json'),
21
+ );
22
+ });
23
+ });
24
+
25
+ describe('writeAgentRequest', () => {
26
+ it('creates the directory and file on first write', () => {
27
+ const entry: AgentRequest = {
28
+ sessionId: 'sess-1',
29
+ toolName: 'Bash',
30
+ toolInput: { command: 'ls /tmp' },
31
+ timestamp: '2026-06-29T00:00:00.000Z',
32
+ };
33
+ writeAgentRequest(homeDir, entry);
34
+
35
+ expect(readLatestAgentRequest(homeDir, 'sess-1')).toEqual(entry);
36
+ });
37
+
38
+ it('overwrites an existing entry on subsequent writes', () => {
39
+ const first: AgentRequest = { sessionId: 'sess-2', toolName: 'Bash', toolInput: { command: 'echo first' }, timestamp: '2026-06-29T00:00:01.000Z' };
40
+ const second: AgentRequest = { sessionId: 'sess-2', toolName: 'Bash', toolInput: { command: 'echo second' }, timestamp: '2026-06-29T00:00:02.000Z' };
41
+
42
+ writeAgentRequest(homeDir, first);
43
+ writeAgentRequest(homeDir, second);
44
+
45
+ expect(readLatestAgentRequest(homeDir, 'sess-2')).toEqual(second);
46
+ });
47
+ });
48
+
49
+ describe('readLatestAgentRequest', () => {
50
+ it('returns null when no file exists for the session', () => {
51
+ expect(readLatestAgentRequest(homeDir, 'no-such-session')).toBeNull();
52
+ });
53
+
54
+ it('returns null when the file contains malformed JSON', () => {
55
+ const entry: AgentRequest = { sessionId: 'bad', toolName: 'Bash', toolInput: {}, timestamp: '2026-06-29T00:00:00.000Z' };
56
+ writeAgentRequest(homeDir, entry);
57
+ writeFileSync(getAgentRequestPath(homeDir, 'bad'), 'NOT JSON{{{', 'utf-8');
58
+
59
+ expect(readLatestAgentRequest(homeDir, 'bad')).toBeNull();
60
+ });
61
+
62
+ it('returns the stored entry when the file is valid', () => {
63
+ const entry: AgentRequest = {
64
+ sessionId: 'good',
65
+ toolName: 'AskUserQuestion',
66
+ toolInput: { question: 'Which option?', options: ['A', 'B'] },
67
+ timestamp: '2026-06-29T12:00:00.000Z',
68
+ };
69
+ writeAgentRequest(homeDir, entry);
70
+
71
+ expect(readLatestAgentRequest(homeDir, 'good')).toEqual(entry);
72
+ });
73
+ });
74
+ });
package/src/index.ts CHANGED
@@ -30,3 +30,6 @@ export type { RegistryEntry } from './utils/AgentRegistry.js';
30
30
  export { TmuxManager } from './terminal/TmuxManager.js';
31
31
  export { AGENTS } from './utils/agents.js';
32
32
  export type { AgentConfig, StartableAgentType } from './utils/agents.js';
33
+
34
+ export type { AgentRequest } from './utils/agent-requests.js';
35
+ export { getAgentRequestPath, readLatestAgentRequest, writeAgentRequest } from './utils/agent-requests.js';