@aibridge/driver-agy 0.9.2 → 0.10.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.
- package/README.md +2 -2
- package/dist/index.d.mts +4 -1
- package/dist/index.mjs +269 -62
- package/package.json +2 -2
- package/src/agy.test.ts +23 -1
- package/src/agy.ts +16 -0
- package/src/generateImage.test.ts +3 -1
- package/src/generateImage.ts +1 -1
- package/src/index.ts +6 -1
- package/src/run.test.ts +595 -92
- package/src/run.ts +362 -72
package/src/run.test.ts
CHANGED
|
@@ -1,22 +1,111 @@
|
|
|
1
|
-
import { writeFileSync } from 'node:fs';
|
|
2
1
|
import type { RunOptions, RunResult } from '@aibridge/proc';
|
|
3
2
|
import { describe, expect, it } from 'vitest';
|
|
4
3
|
import { run } from './run.ts';
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
interface FakeAgyConfig {
|
|
6
|
+
readonly version?: string | null;
|
|
7
|
+
readonly versionError?: Error;
|
|
8
|
+
readonly execError?: Error;
|
|
9
|
+
readonly stdout?: string;
|
|
10
|
+
readonly stderr?: string;
|
|
11
|
+
readonly code?: number | null;
|
|
12
|
+
readonly timedOut?: boolean;
|
|
13
|
+
readonly streamChunks?: readonly string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function fakeAgy(config: FakeAgyConfig = {}): {
|
|
17
|
+
readonly exec: (cmd: string, args: readonly string[], opts?: RunOptions) => Promise<RunResult>;
|
|
18
|
+
readonly calls: Array<{ cmd: string; args: readonly string[]; opts: RunOptions }>;
|
|
19
|
+
} {
|
|
20
|
+
const calls: Array<{ cmd: string; args: readonly string[]; opts: RunOptions }> = [];
|
|
21
|
+
|
|
22
|
+
const exec = async (
|
|
23
|
+
cmd: string,
|
|
24
|
+
args: readonly string[],
|
|
25
|
+
opts: RunOptions = {},
|
|
26
|
+
): Promise<RunResult> => {
|
|
27
|
+
calls.push({ cmd, args, opts });
|
|
28
|
+
|
|
29
|
+
if (config.execError) {
|
|
30
|
+
throw config.execError;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (args.length === 1 && args[0] === '--version') {
|
|
34
|
+
if (config.versionError) {
|
|
35
|
+
throw config.versionError;
|
|
36
|
+
}
|
|
37
|
+
if (config.version === null) {
|
|
38
|
+
return { code: 0, signal: null, stdout: '', stderr: '', timedOut: false };
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
code: 0,
|
|
42
|
+
signal: null,
|
|
43
|
+
stdout: `${config.version ?? 'agy 1.2.3'}\n`,
|
|
44
|
+
stderr: '',
|
|
45
|
+
timedOut: false,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (config.streamChunks) {
|
|
50
|
+
for (const chunk of config.streamChunks) {
|
|
51
|
+
opts.onStdout?.(chunk);
|
|
52
|
+
}
|
|
53
|
+
} else if (config.stdout !== undefined) {
|
|
54
|
+
if (opts.captureStdout === false) {
|
|
55
|
+
opts.onStdout?.(config.stdout);
|
|
56
|
+
} else {
|
|
57
|
+
opts.onStdout?.(config.stdout);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
code: config.code !== undefined ? config.code : 0,
|
|
63
|
+
signal: null,
|
|
64
|
+
stdout: opts.captureStdout === false ? '' : (config.stdout ?? ''),
|
|
65
|
+
stderr: config.stderr ?? '',
|
|
66
|
+
timedOut: config.timedOut ?? false,
|
|
19
67
|
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return { exec, calls };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe('agy driver argv / version handling', () => {
|
|
74
|
+
it('passes stream-json and sets captureStdout: false on version >= 1.2.3 (no-tools)', async () => {
|
|
75
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: '' });
|
|
76
|
+
|
|
77
|
+
await run(
|
|
78
|
+
{
|
|
79
|
+
prompt: 'do something',
|
|
80
|
+
tools: false,
|
|
81
|
+
timeoutSec: 60,
|
|
82
|
+
cwd: '/test/cwd',
|
|
83
|
+
backendModel: 'gemini-3.7-flash-high',
|
|
84
|
+
},
|
|
85
|
+
fake.exec,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
expect(fake.calls.length).toBe(2);
|
|
89
|
+
const runCall = fake.calls[1];
|
|
90
|
+
expect(runCall).toBeDefined();
|
|
91
|
+
if (runCall) {
|
|
92
|
+
expect(runCall.cmd).toBe('agy');
|
|
93
|
+
expect(runCall.args).toEqual([
|
|
94
|
+
'-p',
|
|
95
|
+
'do something',
|
|
96
|
+
'--model',
|
|
97
|
+
'gemini-3.7-flash-high',
|
|
98
|
+
'--print-timeout',
|
|
99
|
+
'60s',
|
|
100
|
+
'--output-format',
|
|
101
|
+
'stream-json',
|
|
102
|
+
]);
|
|
103
|
+
expect(runCall.opts.captureStdout).toBe(false);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('keeps plain text args and captures stdout on version < 1.2.3 (1.2.2)', async () => {
|
|
108
|
+
const fake = fakeAgy({ version: 'agy 1.2.2', stdout: 'Hello world' });
|
|
20
109
|
|
|
21
110
|
const res = await run(
|
|
22
111
|
{
|
|
@@ -26,44 +115,52 @@ describe('agy driver run()', () => {
|
|
|
26
115
|
cwd: '/test/cwd',
|
|
27
116
|
backendModel: 'gemini-3.7-flash-high',
|
|
28
117
|
},
|
|
29
|
-
|
|
118
|
+
fake.exec,
|
|
30
119
|
);
|
|
31
120
|
|
|
32
|
-
|
|
33
|
-
expect(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
121
|
+
const runCall = fake.calls[1];
|
|
122
|
+
expect(runCall).toBeDefined();
|
|
123
|
+
if (runCall) {
|
|
124
|
+
expect(runCall.args).toEqual([
|
|
125
|
+
'-p',
|
|
126
|
+
'do something',
|
|
127
|
+
'--model',
|
|
128
|
+
'gemini-3.7-flash-high',
|
|
129
|
+
'--print-timeout',
|
|
130
|
+
'60s',
|
|
131
|
+
]);
|
|
132
|
+
expect(runCall.opts.captureStdout).toBeUndefined();
|
|
133
|
+
}
|
|
41
134
|
expect(res).toEqual({ ok: true, response: 'Hello world', exitCode: 0 });
|
|
42
135
|
});
|
|
43
136
|
|
|
44
|
-
it('
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
137
|
+
it('falls back to text mode when version is unparseable', async () => {
|
|
138
|
+
const fake = fakeAgy({ version: 'not-a-version', stdout: 'Text fallback' });
|
|
139
|
+
|
|
140
|
+
const res = await run(
|
|
141
|
+
{
|
|
142
|
+
prompt: 'do something',
|
|
143
|
+
tools: false,
|
|
144
|
+
timeoutSec: 60,
|
|
145
|
+
cwd: '/test/cwd',
|
|
146
|
+
backendModel: 'gemini-3.7-flash-high',
|
|
147
|
+
},
|
|
148
|
+
fake.exec,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const runCall = fake.calls[1];
|
|
152
|
+
expect(runCall).toBeDefined();
|
|
153
|
+
if (runCall) {
|
|
154
|
+
expect(runCall.args).not.toContain('--output-format');
|
|
155
|
+
}
|
|
156
|
+
expect(res).toEqual({ ok: true, response: 'Text fallback', exitCode: 0 });
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('passes the prompt verbatim with --dangerously-skip-permissions, cwd as the only add-dir, and stream-json in tools mode', async () => {
|
|
160
|
+
const fake = fakeAgy({
|
|
161
|
+
version: 'agy 1.2.3',
|
|
162
|
+
stdout: `${JSON.stringify({ event: 'result', result: { status: 'SUCCESS', response: 'from stream' } })}\n`,
|
|
163
|
+
});
|
|
67
164
|
|
|
68
165
|
const res = await run(
|
|
69
166
|
{
|
|
@@ -73,82 +170,488 @@ describe('agy driver run()', () => {
|
|
|
73
170
|
cwd: '/repo/root',
|
|
74
171
|
backendModel: 'gemini-3.7-flash-high',
|
|
75
172
|
},
|
|
76
|
-
|
|
173
|
+
fake.exec,
|
|
77
174
|
);
|
|
78
175
|
|
|
79
|
-
|
|
80
|
-
expect(
|
|
81
|
-
|
|
176
|
+
const runCall = fake.calls[1];
|
|
177
|
+
expect(runCall).toBeDefined();
|
|
178
|
+
if (runCall) {
|
|
179
|
+
expect(runCall.args.slice(0, 2)).toEqual(['-p', 'do tool task']);
|
|
180
|
+
expect(runCall.args).toContain('--dangerously-skip-permissions');
|
|
181
|
+
const addDirs = runCall.args.flatMap((a, i) =>
|
|
182
|
+
a === '--add-dir' ? [runCall.args[i + 1]] : [],
|
|
183
|
+
);
|
|
184
|
+
expect(addDirs).toEqual(['/repo/root']);
|
|
185
|
+
expect(runCall.args.slice(-2)).toEqual(['--output-format', 'stream-json']);
|
|
186
|
+
expect(runCall.opts.captureStdout).toBe(false);
|
|
187
|
+
}
|
|
188
|
+
expect(res).toEqual({ ok: true, response: 'from stream', exitCode: 0 });
|
|
82
189
|
});
|
|
190
|
+
});
|
|
83
191
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
192
|
+
describe('agy no-tools stream-json parsing (1.2.3)', () => {
|
|
193
|
+
const task = {
|
|
194
|
+
prompt: 'test prompt',
|
|
195
|
+
tools: false,
|
|
196
|
+
timeoutSec: 30,
|
|
197
|
+
cwd: '/work',
|
|
198
|
+
backendModel: 'gemini-3.7-flash-high',
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const stepUpdate = (
|
|
202
|
+
stepType: string,
|
|
203
|
+
delta?: string,
|
|
204
|
+
state = 'ACTIVE',
|
|
205
|
+
extra: Record<string, unknown> = {},
|
|
206
|
+
) =>
|
|
207
|
+
JSON.stringify({
|
|
208
|
+
event: 'step_update',
|
|
209
|
+
step_update: {
|
|
210
|
+
state,
|
|
211
|
+
step_type: stepType,
|
|
212
|
+
...(delta !== undefined ? { text_delta: delta } : {}),
|
|
213
|
+
...extra,
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const resultFrame = (status: string, response?: string, error?: string) =>
|
|
218
|
+
JSON.stringify({
|
|
219
|
+
event: 'result',
|
|
220
|
+
result: {
|
|
221
|
+
status,
|
|
222
|
+
...(response !== undefined ? { response } : {}),
|
|
223
|
+
...(error !== undefined ? { error } : {}),
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('parses init -> step_update deltas -> result SUCCESS', async () => {
|
|
228
|
+
const log: string[] = [];
|
|
229
|
+
const stream = [
|
|
230
|
+
JSON.stringify({ event: 'init' }),
|
|
231
|
+
stepUpdate('agent_response', 'Hel'),
|
|
232
|
+
stepUpdate('agent_response', 'lo', 'DONE'),
|
|
233
|
+
resultFrame('SUCCESS', 'Hello'),
|
|
234
|
+
'',
|
|
235
|
+
].join('\n');
|
|
236
|
+
|
|
237
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: stream });
|
|
238
|
+
const res = await run({ ...task, onStdout: c => log.push(c) }, fake.exec);
|
|
239
|
+
|
|
240
|
+
expect(res).toEqual({ ok: true, response: 'Hello', exitCode: 0 });
|
|
241
|
+
expect(log.join('')).toBe('Hello\n');
|
|
242
|
+
expect(log.join('')).not.toContain('event');
|
|
243
|
+
expect(log.join('')).not.toContain('init');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('triggers onActivity on mid-line chunks', async () => {
|
|
247
|
+
let activityCount = 0;
|
|
248
|
+
const stream = [
|
|
249
|
+
JSON.stringify({ event: 'init' }),
|
|
250
|
+
stepUpdate('agent_response', 'Hello', 'DONE'),
|
|
251
|
+
resultFrame('SUCCESS', 'Hello'),
|
|
252
|
+
'',
|
|
253
|
+
].join('\n');
|
|
254
|
+
|
|
255
|
+
const fake = fakeAgy({
|
|
256
|
+
version: 'agy 1.2.3',
|
|
257
|
+
streamChunks: [stream.slice(0, 10), stream.slice(10, 25), stream.slice(25)],
|
|
258
|
+
});
|
|
90
259
|
|
|
91
260
|
const res = await run(
|
|
92
261
|
{
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
backendModel: 'gemini-3.7-flash-high',
|
|
262
|
+
...task,
|
|
263
|
+
onActivity: () => {
|
|
264
|
+
activityCount++;
|
|
265
|
+
},
|
|
98
266
|
},
|
|
99
|
-
|
|
267
|
+
fake.exec,
|
|
100
268
|
);
|
|
101
269
|
|
|
270
|
+
expect(res).toEqual({ ok: true, response: 'Hello', exitCode: 0 });
|
|
271
|
+
expect(activityCount).toBeGreaterThan(1);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('maps protocol with empty SUCCESS response and no deltas to no-answer', async () => {
|
|
275
|
+
const stream = [JSON.stringify({ event: 'init' }), resultFrame('SUCCESS', ''), ''].join('\n');
|
|
276
|
+
|
|
277
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: stream });
|
|
278
|
+
const res = await run(task, fake.exec);
|
|
279
|
+
|
|
102
280
|
expect(res.ok).toBe(false);
|
|
103
281
|
if (!res.ok) {
|
|
104
|
-
expect(res.kind).toBe('
|
|
105
|
-
expect(res
|
|
282
|
+
expect(res.kind).toBe('no-answer');
|
|
283
|
+
expect(JSON.stringify(res)).not.toContain('event');
|
|
284
|
+
expect(JSON.stringify(res)).not.toContain('step_update');
|
|
106
285
|
}
|
|
107
286
|
});
|
|
108
287
|
|
|
109
|
-
it('
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
288
|
+
it('prefers result.response over logged deltas when they differ', async () => {
|
|
289
|
+
const log: string[] = [];
|
|
290
|
+
const stream = [
|
|
291
|
+
JSON.stringify({ event: 'init' }),
|
|
292
|
+
stepUpdate('agent_response', 'draft\n', 'DONE'),
|
|
293
|
+
resultFrame('SUCCESS', 'PASS'),
|
|
294
|
+
'',
|
|
295
|
+
].join('\n');
|
|
113
296
|
|
|
114
|
-
const
|
|
297
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: stream });
|
|
298
|
+
const res = await run({ ...task, onStdout: c => log.push(c) }, fake.exec);
|
|
299
|
+
|
|
300
|
+
expect(res).toEqual({ ok: true, response: 'PASS', exitCode: 0 });
|
|
301
|
+
expect(log.join('')).toContain('draft\n');
|
|
302
|
+
expect(log.join('')).toContain('PASS\n');
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('drops reasoning / thinking step_type from log and answer', async () => {
|
|
306
|
+
const log: string[] = [];
|
|
307
|
+
const stream = [
|
|
308
|
+
JSON.stringify({ event: 'init' }),
|
|
309
|
+
stepUpdate('reasoning', 'secret thinking'),
|
|
310
|
+
stepUpdate('agent_response', 'PASS', 'DONE'),
|
|
311
|
+
resultFrame('SUCCESS', 'PASS'),
|
|
312
|
+
'',
|
|
313
|
+
].join('\n');
|
|
314
|
+
|
|
315
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: stream });
|
|
316
|
+
const res = await run({ ...task, onStdout: c => log.push(c) }, fake.exec);
|
|
317
|
+
|
|
318
|
+
expect(res).toEqual({ ok: true, response: 'PASS', exitCode: 0 });
|
|
319
|
+
expect(log.join('')).not.toContain('secret thinking');
|
|
320
|
+
expect(log.join('')).toBe('PASS\n');
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('batches many tiny agent-response deltas (bounded onStdout calls)', async () => {
|
|
324
|
+
let stdoutCallCount = 0;
|
|
325
|
+
const chunks: string[] = [`${JSON.stringify({ event: 'init' })}\n`];
|
|
326
|
+
for (let i = 0; i < 50; i++) {
|
|
327
|
+
chunks.push(`${stepUpdate('agent_response', `part${i}`)}\n`);
|
|
328
|
+
}
|
|
329
|
+
chunks.push(`${stepUpdate('agent_response', '\n', 'DONE')}\n`);
|
|
330
|
+
chunks.push(`${resultFrame('SUCCESS', 'all parts')}\n`);
|
|
331
|
+
|
|
332
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: chunks.join('') });
|
|
333
|
+
await run(
|
|
115
334
|
{
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
backendModel: 'gemini-3.7-flash-high',
|
|
335
|
+
...task,
|
|
336
|
+
onStdout: () => {
|
|
337
|
+
stdoutCallCount++;
|
|
338
|
+
},
|
|
121
339
|
},
|
|
122
|
-
|
|
340
|
+
fake.exec,
|
|
123
341
|
);
|
|
124
342
|
|
|
343
|
+
// Should flush only when newline/DONE/result hit, far fewer than 50 calls
|
|
344
|
+
expect(stdoutCallCount).toBeLessThan(10);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it('flushes an unterminated last NDJSON line', async () => {
|
|
348
|
+
const log: string[] = [];
|
|
349
|
+
const stream = `${JSON.stringify({ event: 'init' })}\n${resultFrame('SUCCESS', 'Done.')}`;
|
|
350
|
+
|
|
351
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: stream });
|
|
352
|
+
const res = await run({ ...task, onStdout: c => log.push(c) }, fake.exec);
|
|
353
|
+
|
|
354
|
+
expect(res).toEqual({ ok: true, response: 'Done.', exitCode: 0 });
|
|
355
|
+
expect(log.join('')).toBe('Done.\n');
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('passes non-protocol output through to log and uses as raw fallback', async () => {
|
|
359
|
+
const log: string[] = [];
|
|
360
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: 'not logged in\n', code: 1 });
|
|
361
|
+
const res = await run({ ...task, onStdout: c => log.push(c) }, fake.exec);
|
|
362
|
+
|
|
363
|
+
expect(res.ok).toBe(false);
|
|
364
|
+
expect(log.join('')).toBe('not logged in\n');
|
|
365
|
+
if (!res.ok) {
|
|
366
|
+
expect(res.kind).toBe('no-answer');
|
|
367
|
+
expect(JSON.stringify(res)).not.toContain('event');
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('strips ANSI prefixes before classifying result frame', async () => {
|
|
372
|
+
const stream = `\x1b[33m${resultFrame('SUCCESS', 'ANSI_OK')}\x1b[0m\n`;
|
|
373
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: stream });
|
|
374
|
+
const res = await run(task, fake.exec);
|
|
375
|
+
|
|
376
|
+
expect(res).toEqual({ ok: true, response: 'ANSI_OK', exitCode: 0 });
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it('returns explicit no-answer when pre-protocol raw output exceeds 8192 chars', async () => {
|
|
380
|
+
const longRaw = `${'A'.repeat(5000)}\n${'B'.repeat(4000)}\n`;
|
|
381
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: longRaw });
|
|
382
|
+
const res = await run(task, fake.exec);
|
|
383
|
+
|
|
384
|
+
expect(res.ok).toBe(false);
|
|
385
|
+
if (!res.ok) {
|
|
386
|
+
expect(res.kind).toBe('no-answer');
|
|
387
|
+
expect(res.message).toContain(
|
|
388
|
+
'returned more than 8192 characters without a recognized stream protocol',
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it('valid result wins even after a long raw preamble > 8192 chars', async () => {
|
|
394
|
+
const longRawWithResult = [
|
|
395
|
+
'A'.repeat(5000),
|
|
396
|
+
'B'.repeat(4000),
|
|
397
|
+
resultFrame('SUCCESS', 'VALID_RESULT'),
|
|
398
|
+
'',
|
|
399
|
+
].join('\n');
|
|
400
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: longRawWithResult });
|
|
401
|
+
const res = await run(task, fake.exec);
|
|
402
|
+
|
|
403
|
+
expect(res).toEqual({ ok: true, response: 'VALID_RESULT', exitCode: 0 });
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
describe('agy tools stream-json parsing', () => {
|
|
408
|
+
const task = {
|
|
409
|
+
prompt: 'tool task',
|
|
410
|
+
tools: true,
|
|
411
|
+
timeoutSec: 30,
|
|
412
|
+
cwd: '/work',
|
|
413
|
+
backendModel: 'gemini-3.7-flash-high',
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
it('logs tool ACTIVE and DONE concisely without dumping heavy payloads', async () => {
|
|
417
|
+
const log: string[] = [];
|
|
418
|
+
const stream = [
|
|
419
|
+
JSON.stringify({ event: 'init' }),
|
|
420
|
+
JSON.stringify({
|
|
421
|
+
event: 'step_update',
|
|
422
|
+
step_update: {
|
|
423
|
+
state: 'ACTIVE',
|
|
424
|
+
tool_name: 'read_file',
|
|
425
|
+
tool_info: { output: '10k'.repeat(5000) },
|
|
426
|
+
},
|
|
427
|
+
}),
|
|
428
|
+
JSON.stringify({
|
|
429
|
+
event: 'step_update',
|
|
430
|
+
step_update: {
|
|
431
|
+
state: 'DONE',
|
|
432
|
+
tool_name: 'read_file',
|
|
433
|
+
tool_info: { summary: 'read 100 lines from foo.ts', output: 'huge payload' },
|
|
434
|
+
},
|
|
435
|
+
}),
|
|
436
|
+
JSON.stringify({ event: 'result', result: { status: 'SUCCESS', response: 'done' } }),
|
|
437
|
+
'',
|
|
438
|
+
].join('\n');
|
|
439
|
+
|
|
440
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stdout: stream });
|
|
441
|
+
const res = await run({ ...task, onStdout: c => log.push(c) }, fake.exec);
|
|
442
|
+
|
|
443
|
+
expect(res).toEqual({ ok: true, response: 'done', exitCode: 0 });
|
|
444
|
+
const logStr = log.join('');
|
|
445
|
+
expect(logStr).toContain('tool read_file\n');
|
|
446
|
+
expect(logStr).toContain('tool read_file done: read 100 lines from foo.ts\n');
|
|
447
|
+
expect(logStr).not.toContain('huge payload');
|
|
448
|
+
expect(logStr).not.toContain('10k10k');
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
describe('agy timeout and error handling', () => {
|
|
453
|
+
const task = {
|
|
454
|
+
prompt: 'test prompt',
|
|
455
|
+
tools: false,
|
|
456
|
+
timeoutSec: 1,
|
|
457
|
+
cwd: '/work',
|
|
458
|
+
backendModel: 'gemini-3.7-flash-high',
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
it('maps soft print-timeout stderr on stream path to timeout with ~1s message', async () => {
|
|
462
|
+
const stderr = '[agy] print timeout after 1s with turn in progress; returning partial output\n';
|
|
463
|
+
const stream = [
|
|
464
|
+
JSON.stringify({ event: 'init' }),
|
|
465
|
+
JSON.stringify({ event: 'result', result: { status: 'SUCCESS', response: 'partial' } }),
|
|
466
|
+
'',
|
|
467
|
+
].join('\n');
|
|
468
|
+
|
|
469
|
+
const fake = fakeAgy({
|
|
470
|
+
version: 'agy 1.2.3',
|
|
471
|
+
stdout: stream,
|
|
472
|
+
stderr,
|
|
473
|
+
code: 0,
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
const res = await run(task, fake.exec);
|
|
477
|
+
|
|
125
478
|
expect(res.ok).toBe(false);
|
|
126
479
|
if (!res.ok) {
|
|
127
480
|
expect(res.kind).toBe('timeout');
|
|
128
|
-
expect(res.
|
|
481
|
+
expect(res.message).toBe('aibridge: agy timed out after ~1s; raise --timeout.');
|
|
129
482
|
}
|
|
130
483
|
});
|
|
131
484
|
|
|
132
|
-
it('maps
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
485
|
+
it('maps soft print-timeout stderr on 1.2.2 text path to timeout', async () => {
|
|
486
|
+
const stderr = '[agy] print timeout after 1s with turn in progress; returning partial output\n';
|
|
487
|
+
const fake = fakeAgy({
|
|
488
|
+
version: 'agy 1.2.2',
|
|
489
|
+
stdout: 'partial',
|
|
490
|
+
stderr,
|
|
491
|
+
code: 0,
|
|
492
|
+
});
|
|
136
493
|
|
|
137
|
-
const res = await run(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
494
|
+
const res = await run(task, fake.exec);
|
|
495
|
+
|
|
496
|
+
expect(res.ok).toBe(false);
|
|
497
|
+
if (!res.ok) {
|
|
498
|
+
expect(res.kind).toBe('timeout');
|
|
499
|
+
expect(res.message).toBe('aibridge: agy timed out after ~1s; raise --timeout.');
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it('maps soft print-timeout stderr to timeout in tools mode too', async () => {
|
|
504
|
+
const stderr = '[agy] print timeout after 1s with turn in progress; returning partial output\n';
|
|
505
|
+
const fake = fakeAgy({ version: 'agy 1.2.3', stderr, code: 0 });
|
|
506
|
+
|
|
507
|
+
const res = await run({ ...task, tools: true }, fake.exec);
|
|
508
|
+
|
|
509
|
+
expect(res.ok).toBe(false);
|
|
510
|
+
if (!res.ok) expect(res.kind).toBe('timeout');
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
it('ERROR result frame is no-answer even with exit 0', async () => {
|
|
514
|
+
const stream = [
|
|
515
|
+
JSON.stringify({ event: 'init' }),
|
|
516
|
+
JSON.stringify({
|
|
517
|
+
event: 'result',
|
|
518
|
+
result: { status: 'ERROR', response: '', error: 'provider failed after writing file' },
|
|
519
|
+
}),
|
|
520
|
+
'',
|
|
521
|
+
].join('\n');
|
|
522
|
+
const fake = fakeAgy({
|
|
523
|
+
version: 'agy 1.2.3',
|
|
524
|
+
stdout: stream,
|
|
525
|
+
code: 0,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const res = await run({ ...task, tools: true }, fake.exec);
|
|
529
|
+
|
|
530
|
+
expect(res.ok).toBe(false);
|
|
531
|
+
if (!res.ok) {
|
|
532
|
+
expect(res.kind).toBe('no-answer');
|
|
533
|
+
expect(res.message).toContain('provider failed after writing file');
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
it('does not trigger soft timeout if stderr does not match exact terminal [agy] line or code != 0', async () => {
|
|
538
|
+
const stderr =
|
|
539
|
+
'mid-stream [agy] print timeout after 1s with turn in progress; returning partial output\nsome other error\n';
|
|
540
|
+
const stream = [
|
|
541
|
+
JSON.stringify({ event: 'init' }),
|
|
542
|
+
JSON.stringify({ event: 'result', result: { status: 'SUCCESS', response: 'ok answer' } }),
|
|
543
|
+
'',
|
|
544
|
+
].join('\n');
|
|
545
|
+
|
|
546
|
+
const fake = fakeAgy({
|
|
547
|
+
version: 'agy 1.2.3',
|
|
548
|
+
stdout: stream,
|
|
549
|
+
stderr,
|
|
550
|
+
code: 0,
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
const res = await run(task, fake.exec);
|
|
554
|
+
expect(res).toEqual({ ok: true, response: 'ok answer', exitCode: 0 });
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it('maps hard process timedOut to timeout with ~21s message', async () => {
|
|
558
|
+
const fake = fakeAgy({
|
|
559
|
+
version: 'agy 1.2.3',
|
|
560
|
+
timedOut: true,
|
|
561
|
+
code: null,
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
const res = await run(task, fake.exec);
|
|
565
|
+
|
|
566
|
+
expect(res.ok).toBe(false);
|
|
567
|
+
if (!res.ok) {
|
|
568
|
+
expect(res.kind).toBe('timeout');
|
|
569
|
+
expect(res.message).toBe('aibridge: agy timed out after ~21s; raise --timeout.');
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it('maps exit 1, error stderr, and ERROR frame to no-answer with detail', async () => {
|
|
574
|
+
const stderr = 'error: unknown model foo\n';
|
|
575
|
+
const stream = [
|
|
576
|
+
JSON.stringify({ event: 'init' }),
|
|
577
|
+
JSON.stringify({ event: 'result', result: { status: 'ERROR', error: 'unknown model foo' } }),
|
|
578
|
+
'',
|
|
579
|
+
].join('\n');
|
|
580
|
+
|
|
581
|
+
const fake = fakeAgy({
|
|
582
|
+
version: 'agy 1.2.3',
|
|
583
|
+
stdout: stream,
|
|
584
|
+
stderr,
|
|
585
|
+
code: 1,
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
const res = await run(task, fake.exec);
|
|
147
589
|
|
|
148
590
|
expect(res.ok).toBe(false);
|
|
149
591
|
if (!res.ok) {
|
|
150
592
|
expect(res.kind).toBe('no-answer');
|
|
151
|
-
expect(res.
|
|
593
|
+
expect(res.message).toContain('unknown model foo');
|
|
594
|
+
expect(JSON.stringify(res)).not.toContain('event');
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it('maps empty SUCCESS with exit 0 and no print-timeout stderr to no-answer (quota death)', async () => {
|
|
599
|
+
const stream = [
|
|
600
|
+
JSON.stringify({ event: 'init' }),
|
|
601
|
+
JSON.stringify({ event: 'result', result: { status: 'SUCCESS', response: '' } }),
|
|
602
|
+
'',
|
|
603
|
+
].join('\n');
|
|
604
|
+
|
|
605
|
+
const fake = fakeAgy({
|
|
606
|
+
version: 'agy 1.2.3',
|
|
607
|
+
stdout: stream,
|
|
608
|
+
code: 0,
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
const res = await run(task, fake.exec);
|
|
612
|
+
|
|
613
|
+
expect(res.ok).toBe(false);
|
|
614
|
+
if (!res.ok) {
|
|
615
|
+
expect(res.kind).toBe('no-answer');
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
describe('agy spawn failure handling', () => {
|
|
621
|
+
const task = {
|
|
622
|
+
prompt: 'test',
|
|
623
|
+
tools: false,
|
|
624
|
+
timeoutSec: 10,
|
|
625
|
+
cwd: '/work',
|
|
626
|
+
backendModel: 'gemini-3.7-flash-high',
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
it('maps ENOENT spawn error to not-found', async () => {
|
|
630
|
+
const err = new Error('spawn agy ENOENT') as Error & { code?: string };
|
|
631
|
+
err.code = 'ENOENT';
|
|
632
|
+
const fake = fakeAgy({ execError: err });
|
|
633
|
+
|
|
634
|
+
const res = await run(task, fake.exec);
|
|
635
|
+
|
|
636
|
+
expect(res.ok).toBe(false);
|
|
637
|
+
if (!res.ok) {
|
|
638
|
+
expect(res.kind).toBe('not-found');
|
|
639
|
+
expect(res.exitCode).toBeNull();
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
it('maps version probe EACCES rejection to kind: spawn without escaping', async () => {
|
|
644
|
+
const err = new Error('spawn agy EACCES') as Error & { code?: string };
|
|
645
|
+
err.code = 'EACCES';
|
|
646
|
+
const fake = fakeAgy({ versionError: err });
|
|
647
|
+
|
|
648
|
+
const res = await run(task, fake.exec);
|
|
649
|
+
|
|
650
|
+
expect(res.ok).toBe(false);
|
|
651
|
+
if (!res.ok) {
|
|
652
|
+
expect(res.kind).toBe('spawn');
|
|
653
|
+
expect(res.message).toContain('EACCES');
|
|
654
|
+
expect(res.exitCode).toBeNull();
|
|
152
655
|
}
|
|
153
656
|
});
|
|
154
657
|
});
|