@juanbenjumea/pi-agy 0.6.11 → 0.7.1
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/CHANGELOG.md +15 -0
- package/README.md +59 -2
- package/extensions/commands.ts +66 -105
- package/extensions/index.ts +164 -47
- package/extensions/lib/args.ts +125 -0
- package/extensions/lib/cli.ts +31 -1223
- package/extensions/lib/command-args.ts +101 -0
- package/extensions/lib/config.ts +36 -1
- package/extensions/lib/confirm.ts +43 -0
- package/extensions/lib/doctor.ts +37 -1
- package/extensions/lib/inspect.ts +254 -0
- package/extensions/lib/lock.ts +1 -1
- package/extensions/lib/model-sort.ts +46 -0
- package/extensions/lib/provider-driver.ts +728 -0
- package/extensions/lib/provider-mcp.ts +580 -0
- package/extensions/lib/provider-models.ts +251 -0
- package/extensions/lib/provider.ts +1148 -0
- package/extensions/lib/retry.ts +34 -0
- package/extensions/lib/spawn.ts +411 -0
- package/extensions/lib/usage.ts +398 -0
- package/extensions/lib/verify.ts +11 -0
- package/package.json +6 -1
|
@@ -0,0 +1,728 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistent agy driver for provider turns.
|
|
3
|
+
*
|
|
4
|
+
* One long-lived `agy --input-format stream-json --output-format stream-json`
|
|
5
|
+
* child per profile (working directory + model + effort + permissions).
|
|
6
|
+
* Turns serialize through a per-driver queue; each turn is one
|
|
7
|
+
* `{"event":"user",...}` stdin line, and stdout NDJSON (`init` / `step_update`
|
|
8
|
+
* / `result`) is routed to the active turn.
|
|
9
|
+
*
|
|
10
|
+
* Reuse rules: a turn reuses the live child when the profile matches and the
|
|
11
|
+
* requested conversation matches the bound one (including both unset — agy
|
|
12
|
+
* keeps the conversation natively, so follow-ups skip cold start and OAuth
|
|
13
|
+
* refresh). Anything else recycles the child (kill + respawn) with a recorded
|
|
14
|
+
* reason. Abort/timeout kills the child — a single turn cannot be cancelled
|
|
15
|
+
* reliably headless — and partial text settles with an explicit flag, never
|
|
16
|
+
* as a clean stop. Idle children are reaped; `close()` disposes everything
|
|
17
|
+
* (wire to `session_shutdown`).
|
|
18
|
+
*
|
|
19
|
+
* Terminal envelopes fail closed, mirroring the per-turn runner this
|
|
20
|
+
* replaces: only SUCCESS/OK statuses are accepted, and a denied run with no
|
|
21
|
+
* response rejects instead of answering empty.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { StringDecoder } from "node:string_decoder";
|
|
25
|
+
|
|
26
|
+
import type { ChildProcess } from "node:child_process";
|
|
27
|
+
|
|
28
|
+
import { getSpawn, killProcessTree } from "./spawn.js";
|
|
29
|
+
import {
|
|
30
|
+
accumulateRunResult,
|
|
31
|
+
appendStreamChunk,
|
|
32
|
+
formatStepProgress,
|
|
33
|
+
parseStreamLine,
|
|
34
|
+
type AgyRunResult,
|
|
35
|
+
} from "./stream.js";
|
|
36
|
+
import type {
|
|
37
|
+
AgyProviderEffort,
|
|
38
|
+
} from "./provider-models.js";
|
|
39
|
+
|
|
40
|
+
export interface ProviderDriverProfile {
|
|
41
|
+
dir: string;
|
|
42
|
+
fullModel: string;
|
|
43
|
+
effort?: AgyProviderEffort;
|
|
44
|
+
skipPermissions: boolean;
|
|
45
|
+
/** Bridge config dir for an extra `--add-dir` (MCP tool discovery). */
|
|
46
|
+
bridgeDir?: string;
|
|
47
|
+
/** Image staging root for an extra `--add-dir` (sandboxed file reads). */
|
|
48
|
+
imageRoot?: string;
|
|
49
|
+
/** Fingerprint of the bridged Pi-tool catalog; changes recycle (fresh ListTools). */
|
|
50
|
+
bridgeTools?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ProviderDriverTurn {
|
|
54
|
+
prompt: string;
|
|
55
|
+
resumeConversationId?: string;
|
|
56
|
+
signal: AbortSignal;
|
|
57
|
+
timeoutMs: number;
|
|
58
|
+
onText?: (delta: string) => void;
|
|
59
|
+
onActivity?: (label: string) => void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface ProviderDriverOutcome {
|
|
63
|
+
text: string;
|
|
64
|
+
truncated: boolean;
|
|
65
|
+
aborted?: boolean;
|
|
66
|
+
timedOut?: boolean;
|
|
67
|
+
conversationId?: string;
|
|
68
|
+
activity: string[];
|
|
69
|
+
deniedActions?: Array<{ action?: string; display_name?: string }>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A bridged Pi-tool call parked mid-turn: agy waits on the MCP response. */
|
|
73
|
+
export interface DriverPark {
|
|
74
|
+
callId: string;
|
|
75
|
+
toolName: string;
|
|
76
|
+
args: Record<string, unknown>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* One phase of a driver turn ends parked (resume with continueTurn after
|
|
81
|
+
* delivering tool results) or done (terminal outcome).
|
|
82
|
+
*/
|
|
83
|
+
export type DriverTurnEnd =
|
|
84
|
+
| { kind: "done"; outcome: ProviderDriverOutcome }
|
|
85
|
+
| { kind: "parked"; park: DriverPark };
|
|
86
|
+
|
|
87
|
+
export interface ProviderDriverStats {
|
|
88
|
+
spawns: number;
|
|
89
|
+
turns: number;
|
|
90
|
+
reuses: number;
|
|
91
|
+
recycles: number;
|
|
92
|
+
lastRecycleReason?: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface ProviderDriverSnapshot {
|
|
96
|
+
state: "idle" | "running" | "dead";
|
|
97
|
+
pid?: number;
|
|
98
|
+
boundConversationId?: string;
|
|
99
|
+
stats: ProviderDriverStats;
|
|
100
|
+
lifecycle: string[];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Idle children are reaped so stale auth/processes never linger. */
|
|
104
|
+
export const PROVIDER_DRIVER_IDLE_MS = 5 * 60_000;
|
|
105
|
+
/** Cap streamed response text per turn. */
|
|
106
|
+
const MAX_TURN_TEXT_CHARS = 512_000;
|
|
107
|
+
const MAX_ACTIVITY_ENTRIES = 32;
|
|
108
|
+
const MAX_STDERR_TAIL_CHARS = 8_000;
|
|
109
|
+
const LIFECYCLE_LIMIT = 24;
|
|
110
|
+
|
|
111
|
+
interface TurnPhase {
|
|
112
|
+
resolve: (end: DriverTurnEnd) => void;
|
|
113
|
+
reject: (error: Error) => void;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Wall-clock cap for a suspended (parked) turn without Pi progress. */
|
|
117
|
+
export const SUSPENDED_TURN_MAX_MS = 30 * 60_000;
|
|
118
|
+
|
|
119
|
+
interface ActiveTurn {
|
|
120
|
+
signal: AbortSignal;
|
|
121
|
+
timeoutMs: number;
|
|
122
|
+
onText?: (delta: string) => void;
|
|
123
|
+
onActivity?: (label: string) => void;
|
|
124
|
+
text: string;
|
|
125
|
+
truncated: boolean;
|
|
126
|
+
activity: string[];
|
|
127
|
+
seenActivity: Set<string>;
|
|
128
|
+
runResult: AgyRunResult;
|
|
129
|
+
/** Terminally settled (done/error/abort/timeout): the record is over. */
|
|
130
|
+
settled: boolean;
|
|
131
|
+
/** Suspended mid-turn on a parked bridge call: child alive, timers off. */
|
|
132
|
+
suspended: boolean;
|
|
133
|
+
/** Live waiter for the current phase (turn() or continueTurn()). */
|
|
134
|
+
phase?: TurnPhase;
|
|
135
|
+
timer?: NodeJS.Timeout;
|
|
136
|
+
suspendedTimer?: NodeJS.Timeout;
|
|
137
|
+
onAbort?: () => void;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function profileKey(profile: ProviderDriverProfile): string {
|
|
141
|
+
return `${profile.dir}::${profile.fullModel}::${profile.effort ?? ""}::${profile.skipPermissions ? "skip" : "ask"}::${profile.bridgeDir ?? ""}::${profile.bridgeTools ?? ""}::${profile.imageRoot ?? ""}`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export class AgyProviderDriver {
|
|
145
|
+
private child?: ChildProcess;
|
|
146
|
+
private generation = 0;
|
|
147
|
+
private profile?: ProviderDriverProfile;
|
|
148
|
+
private boundConversationId?: string;
|
|
149
|
+
private active?: ActiveTurn;
|
|
150
|
+
private queue: Promise<void> = Promise.resolve();
|
|
151
|
+
private idleTimer?: NodeJS.Timeout;
|
|
152
|
+
private readonly idleMs: number;
|
|
153
|
+
private lineBuffer = "";
|
|
154
|
+
private readonly decoder = new StringDecoder("utf8");
|
|
155
|
+
/**
|
|
156
|
+
* Stdout lines that arrived with no turn to route to (startup chatter,
|
|
157
|
+
* eager single-shot processes). Replayed into the first turn on this
|
|
158
|
+
* process incarnation, then dropped — late lines between settled turns
|
|
159
|
+
* belong to no turn and must never leak into the next one.
|
|
160
|
+
*/
|
|
161
|
+
private pendingLines: string[] = [];
|
|
162
|
+
private primed = false;
|
|
163
|
+
private static readonly MAX_PENDING_LINES = 64;
|
|
164
|
+
private stderrTail = "";
|
|
165
|
+
private stats: ProviderDriverStats = { spawns: 0, turns: 0, reuses: 0, recycles: 0 };
|
|
166
|
+
private lifecycle: string[] = [];
|
|
167
|
+
private closed = false;
|
|
168
|
+
|
|
169
|
+
constructor(options?: { idleMs?: number }) {
|
|
170
|
+
this.idleMs = options?.idleMs ?? PROVIDER_DRIVER_IDLE_MS;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Submit a turn; concurrent turns queue behind the active one. Resolves
|
|
175
|
+
* with the first phase end: terminal `done`, or `parked` when agy calls a
|
|
176
|
+
* bridged Pi tool (resume with continueTurn after delivering results).
|
|
177
|
+
*/
|
|
178
|
+
turn(profile: ProviderDriverProfile, turn: ProviderDriverTurn): Promise<DriverTurnEnd> {
|
|
179
|
+
if (this.closed) return Promise.reject(new Error("agy provider driver is closed"));
|
|
180
|
+
if (turn.signal.aborted) return Promise.reject(new Error("agy provider turn was cancelled"));
|
|
181
|
+
const run = this.queue.then(() => this.runTurn(profile, turn));
|
|
182
|
+
// A rejection must not break the chain for later turns.
|
|
183
|
+
this.queue = run.then(
|
|
184
|
+
() => undefined,
|
|
185
|
+
() => undefined,
|
|
186
|
+
);
|
|
187
|
+
return run;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Called when parked bridge calls are orphaned (never completed). */
|
|
191
|
+
onParksOrphaned?: (message: string) => void;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Accept a parked bridge call for the active turn. Suspends the turn
|
|
195
|
+
* (timers off, child alive) and ends the current phase as parked.
|
|
196
|
+
* Returns false when no turn can take the park — the caller must fail it.
|
|
197
|
+
*/
|
|
198
|
+
notifyPark(park: DriverPark): boolean {
|
|
199
|
+
const turn = this.active;
|
|
200
|
+
if (!turn || turn.settled || turn.suspended || !turn.phase) return false;
|
|
201
|
+
turn.suspended = true;
|
|
202
|
+
if (turn.timer) {
|
|
203
|
+
clearTimeout(turn.timer);
|
|
204
|
+
turn.timer = undefined;
|
|
205
|
+
}
|
|
206
|
+
// Abandoned suspensions (Pi session died without shutdown) must not
|
|
207
|
+
// hold the child forever.
|
|
208
|
+
turn.suspendedTimer = setTimeout(() => {
|
|
209
|
+
turn.suspendedTimer = undefined;
|
|
210
|
+
this.killChild();
|
|
211
|
+
this.failParksOrphaned("agy provider turn was abandoned while awaiting Pi tools");
|
|
212
|
+
this.settleError(turn, new Error("agy provider turn was abandoned while awaiting Pi tools"));
|
|
213
|
+
}, SUSPENDED_TURN_MAX_MS);
|
|
214
|
+
(turn.suspendedTimer as unknown as { unref?: () => void }).unref?.();
|
|
215
|
+
const phase = turn.phase;
|
|
216
|
+
turn.phase = undefined;
|
|
217
|
+
this.log(`park:${park.toolName}`);
|
|
218
|
+
phase.resolve({ kind: "parked", park });
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private clearSuspendedTimer(turn: ActiveTurn): void {
|
|
223
|
+
if (turn.suspendedTimer) {
|
|
224
|
+
clearTimeout(turn.suspendedTimer);
|
|
225
|
+
turn.suspendedTimer = undefined;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private failParksOrphaned(message: string): void {
|
|
230
|
+
try {
|
|
231
|
+
this.onParksOrphaned?.(message);
|
|
232
|
+
} catch {
|
|
233
|
+
// A failing orphan hook must never break turn teardown.
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Resume a suspended turn after its parked calls were completed.
|
|
239
|
+
* Re-arms the full turn timeout (round-trips wait on user approvals) and
|
|
240
|
+
* waits for the next phase end. Signal, timeout, and callbacks rebind to
|
|
241
|
+
* the current Pi turn: the previous turn's stream — and its abort signal
|
|
242
|
+
* — are over, and a stale abort must never kill the resumed child.
|
|
243
|
+
*/
|
|
244
|
+
continueTurn(options?: {
|
|
245
|
+
signal?: AbortSignal;
|
|
246
|
+
timeoutMs?: number;
|
|
247
|
+
onText?: (delta: string) => void;
|
|
248
|
+
onActivity?: (label: string) => void;
|
|
249
|
+
}): Promise<DriverTurnEnd> {
|
|
250
|
+
const turn = this.active;
|
|
251
|
+
if (this.closed) throw new Error("agy provider driver was closed");
|
|
252
|
+
if (!turn || turn.settled || !turn.suspended) {
|
|
253
|
+
throw new Error("agy provider driver has no suspended turn");
|
|
254
|
+
}
|
|
255
|
+
if (!this.child) throw new Error("agy provider turn lost its process while suspended");
|
|
256
|
+
turn.suspended = false;
|
|
257
|
+
this.clearSuspendedTimer(turn);
|
|
258
|
+
if (options?.onText) turn.onText = options.onText;
|
|
259
|
+
if (options?.onActivity) turn.onActivity = options.onActivity;
|
|
260
|
+
if (options?.timeoutMs !== undefined) turn.timeoutMs = options.timeoutMs;
|
|
261
|
+
if (turn.onAbort) {
|
|
262
|
+
turn.signal.removeEventListener("abort", turn.onAbort);
|
|
263
|
+
turn.onAbort = undefined;
|
|
264
|
+
}
|
|
265
|
+
if (options?.signal) {
|
|
266
|
+
turn.signal = options.signal;
|
|
267
|
+
const onAbort = (): void => {
|
|
268
|
+
if (turn.settled) return;
|
|
269
|
+
this.killChild();
|
|
270
|
+
if (turn.text) this.settleDone(turn, { aborted: true });
|
|
271
|
+
else this.settleError(turn, new Error("agy provider turn was cancelled"));
|
|
272
|
+
};
|
|
273
|
+
turn.onAbort = onAbort;
|
|
274
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
275
|
+
if (options.signal.aborted) onAbort();
|
|
276
|
+
}
|
|
277
|
+
return new Promise<DriverTurnEnd>((resolve, reject) => {
|
|
278
|
+
turn.phase = { resolve, reject };
|
|
279
|
+
turn.timer = setTimeout(() => {
|
|
280
|
+
// A resumed timeout kills the hung child like a fresh one: never
|
|
281
|
+
// leave it running to be reused.
|
|
282
|
+
this.killChild();
|
|
283
|
+
if (turn.text) this.settleDone(turn, { timedOut: true });
|
|
284
|
+
else this.settleError(turn, new Error("agy provider turn timed out"));
|
|
285
|
+
}, Math.max(1, Math.min(PROVIDER_TURN_TIMEOUT_MS, turn.timeoutMs)));
|
|
286
|
+
(turn.timer as unknown as { unref?: () => void }).unref?.();
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Abandon the active turn (suspended or not): kill the child and fail loudly. */
|
|
291
|
+
cancelSuspended(message: string): void {
|
|
292
|
+
const turn = this.active;
|
|
293
|
+
if (!turn) return;
|
|
294
|
+
this.killChild();
|
|
295
|
+
this.failParksOrphaned(message);
|
|
296
|
+
this.settleError(turn, new Error(message));
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
snapshot(): ProviderDriverSnapshot {
|
|
300
|
+
return {
|
|
301
|
+
state: this.child ? (this.active ? "running" : "idle") : "dead",
|
|
302
|
+
pid: this.child?.pid,
|
|
303
|
+
boundConversationId: this.boundConversationId,
|
|
304
|
+
stats: { ...this.stats },
|
|
305
|
+
lifecycle: [...this.lifecycle],
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Kill the child and reset; the next turn respawns lazily. */
|
|
310
|
+
async close(): Promise<void> {
|
|
311
|
+
this.closed = true;
|
|
312
|
+
this.clearIdleTimer();
|
|
313
|
+
if (this.active && !this.active.settled) {
|
|
314
|
+
// Never strand a caller: a closed driver fails its turns loudly.
|
|
315
|
+
this.settleError(this.active, new Error("agy provider driver was closed"));
|
|
316
|
+
}
|
|
317
|
+
this.killChild();
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private log(event: string): void {
|
|
321
|
+
this.lifecycle.push(`${new Date().toISOString()} ${event}`);
|
|
322
|
+
if (this.lifecycle.length > LIFECYCLE_LIMIT) {
|
|
323
|
+
this.lifecycle.splice(0, this.lifecycle.length - LIFECYCLE_LIMIT);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private clearIdleTimer(): void {
|
|
328
|
+
if (this.idleTimer) {
|
|
329
|
+
clearTimeout(this.idleTimer);
|
|
330
|
+
this.idleTimer = undefined;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private armIdleTimer(): void {
|
|
335
|
+
this.clearIdleTimer();
|
|
336
|
+
if (this.closed) return;
|
|
337
|
+
this.idleTimer = setTimeout(() => {
|
|
338
|
+
this.idleTimer = undefined;
|
|
339
|
+
if (!this.active && this.child) {
|
|
340
|
+
this.log("idle-reap");
|
|
341
|
+
this.killChild();
|
|
342
|
+
this.child = undefined;
|
|
343
|
+
this.profile = undefined;
|
|
344
|
+
this.boundConversationId = undefined;
|
|
345
|
+
}
|
|
346
|
+
}, this.idleMs);
|
|
347
|
+
(this.idleTimer as unknown as { unref?: () => void }).unref?.();
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private recycleReason(profile: ProviderDriverProfile, resume?: string): string | undefined {
|
|
351
|
+
if (!this.child || !this.profile) return "no-process";
|
|
352
|
+
if (profileKey(this.profile) !== profileKey(profile)) {
|
|
353
|
+
if (this.profile.dir !== profile.dir) return "dir-changed";
|
|
354
|
+
if (this.profile.fullModel !== profile.fullModel) return "model-changed";
|
|
355
|
+
if ((this.profile.effort ?? "") !== (profile.effort ?? "")) return "effort-changed";
|
|
356
|
+
if ((this.profile.bridgeDir ?? "") !== (profile.bridgeDir ?? "")) return "bridge-changed";
|
|
357
|
+
if ((this.profile.imageRoot ?? "") !== (profile.imageRoot ?? "")) return "images-changed";
|
|
358
|
+
if ((this.profile.bridgeTools ?? "") !== (profile.bridgeTools ?? "")) return "tools-changed";
|
|
359
|
+
return "permissions-changed";
|
|
360
|
+
}
|
|
361
|
+
if ((resume ?? undefined) !== this.boundConversationId) return "conversation-changed";
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
private killChild(): void {
|
|
366
|
+
this.generation += 1;
|
|
367
|
+
const child = this.child;
|
|
368
|
+
this.child = undefined;
|
|
369
|
+
this.profile = undefined;
|
|
370
|
+
this.boundConversationId = undefined;
|
|
371
|
+
if (child) killProcessTree(child);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private spawn(profile: ProviderDriverProfile, resume?: string): void {
|
|
375
|
+
const spawn = getSpawn();
|
|
376
|
+
const args = ["--add-dir", profile.dir, "--model", profile.fullModel];
|
|
377
|
+
if (profile.effort) args.push("--effort", profile.effort);
|
|
378
|
+
if (resume) args.push("--conversation", resume);
|
|
379
|
+
if (profile.skipPermissions) args.push("--dangerously-skip-permissions");
|
|
380
|
+
if (profile.bridgeDir) args.push("--add-dir", profile.bridgeDir);
|
|
381
|
+
if (profile.imageRoot) args.push("--add-dir", profile.imageRoot);
|
|
382
|
+
// Same flags as the former per-turn spawn, minus `-p`: turns arrive on stdin.
|
|
383
|
+
args.push("--input-format", "stream-json", "--output-format", "stream-json");
|
|
384
|
+
|
|
385
|
+
const generation = this.generation;
|
|
386
|
+
const child = spawn("agy", args, {
|
|
387
|
+
cwd: profile.dir,
|
|
388
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
389
|
+
detached: process.platform !== "win32",
|
|
390
|
+
});
|
|
391
|
+
this.child = child;
|
|
392
|
+
this.profile = profile;
|
|
393
|
+
this.boundConversationId = resume ?? undefined;
|
|
394
|
+
this.lineBuffer = "";
|
|
395
|
+
this.pendingLines = [];
|
|
396
|
+
this.primed = false;
|
|
397
|
+
this.stderrTail = "";
|
|
398
|
+
this.stats.spawns += 1;
|
|
399
|
+
this.log(`spawn:${child.pid ?? "?"}:${resume ? "resume" : "fresh"}`);
|
|
400
|
+
|
|
401
|
+
// EPIPE surfaces async; without this listener a write to a dead child
|
|
402
|
+
// throws uncaught and kills Pi. Deliberately no fail-fast here: the
|
|
403
|
+
// child's stdout close/exit settles the turn (a terminal envelope
|
|
404
|
+
// already in the pipe still wins over the transport error), and the
|
|
405
|
+
// turn timeout bounds a child that stays alive with dead stdin.
|
|
406
|
+
child.stdin?.on("error", (error: Error) => {
|
|
407
|
+
if (generation !== this.generation) return;
|
|
408
|
+
this.log(`stdin-error:${error.message}`);
|
|
409
|
+
});
|
|
410
|
+
child.stdout?.on("data", (data: Buffer) => {
|
|
411
|
+
if (generation !== this.generation) return;
|
|
412
|
+
this.onStdout(data);
|
|
413
|
+
});
|
|
414
|
+
child.stderr?.on("data", (data: Buffer) => {
|
|
415
|
+
if (generation !== this.generation) return;
|
|
416
|
+
this.stderrTail = (this.stderrTail + data.toString("utf8")).slice(-MAX_STDERR_TAIL_CHARS);
|
|
417
|
+
});
|
|
418
|
+
child.on("error", (error: Error) => {
|
|
419
|
+
if (generation !== this.generation) return;
|
|
420
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
421
|
+
this.failActive(
|
|
422
|
+
"Antigravity CLI is not installed. Install agy: curl -fsSL https://antigravity.google/cli/install.sh | bash",
|
|
423
|
+
);
|
|
424
|
+
} else {
|
|
425
|
+
this.failActive(`agy driver failed: ${error.message}`);
|
|
426
|
+
}
|
|
427
|
+
this.killChild();
|
|
428
|
+
});
|
|
429
|
+
child.on("close", (code: number | null) => {
|
|
430
|
+
if (generation !== this.generation) return;
|
|
431
|
+
const wasActive = this.active && !this.active.settled;
|
|
432
|
+
this.child = undefined;
|
|
433
|
+
this.profile = undefined;
|
|
434
|
+
this.boundConversationId = undefined;
|
|
435
|
+
this.log(`exit:${code ?? "signal"}`);
|
|
436
|
+
// Abort/timeout paths settle the turn themselves before killing; an
|
|
437
|
+
// unsettled turn here means the child died mid-turn: fail closed.
|
|
438
|
+
if (wasActive) {
|
|
439
|
+
this.failActive(
|
|
440
|
+
`agy driver exited mid-turn (code ${code ?? "signal"})${this.stderrTail.trim() ? `: ${this.stderrTail.trim().slice(0, 300)}` : ""}`,
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
private ensureProcess(profile: ProviderDriverProfile, resume?: string): void {
|
|
447
|
+
const reason = this.recycleReason(profile, resume);
|
|
448
|
+
if (reason === undefined && this.child) {
|
|
449
|
+
this.stats.reuses += 1;
|
|
450
|
+
this.log("reuse");
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (this.child) {
|
|
454
|
+
this.stats.recycles += 1;
|
|
455
|
+
this.stats.lastRecycleReason = reason ?? "unknown";
|
|
456
|
+
this.log(`recycle:${reason ?? "unknown"}`);
|
|
457
|
+
this.killChild();
|
|
458
|
+
}
|
|
459
|
+
this.spawn(profile, resume);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private pushText(turn: ActiveTurn, delta: string): void {
|
|
463
|
+
if (!delta || turn.settled) return;
|
|
464
|
+
const remaining = MAX_TURN_TEXT_CHARS - turn.text.length;
|
|
465
|
+
if (remaining <= 0) {
|
|
466
|
+
turn.truncated = true;
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
const slice = delta.slice(0, remaining);
|
|
470
|
+
turn.text += slice;
|
|
471
|
+
if (slice.length < delta.length) turn.truncated = true;
|
|
472
|
+
try {
|
|
473
|
+
turn.onText?.(slice);
|
|
474
|
+
} catch {
|
|
475
|
+
// Output for a suspended turn whose Pi stream already ended must
|
|
476
|
+
// never crash the driver; the text is still recorded.
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
private pushActivity(turn: ActiveTurn, label: string): void {
|
|
481
|
+
if (turn.settled || turn.seenActivity.has(label) || turn.activity.length >= MAX_ACTIVITY_ENTRIES) return;
|
|
482
|
+
turn.seenActivity.add(label);
|
|
483
|
+
turn.activity.push(label);
|
|
484
|
+
try {
|
|
485
|
+
turn.onActivity?.(label);
|
|
486
|
+
} catch {
|
|
487
|
+
// Same dead-stream tolerance as text deltas.
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/** Append live agent_response text, tolerating delta and cumulative shapes. */
|
|
492
|
+
private pushLiveText(turn: ActiveTurn, textDelta?: string, responseText?: string): void {
|
|
493
|
+
if (typeof textDelta === "string" && textDelta) {
|
|
494
|
+
this.pushText(turn, textDelta);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
if (typeof responseText === "string" && responseText.length > turn.text.length) {
|
|
498
|
+
this.pushText(turn, responseText.slice(turn.text.length));
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
private onStdout(data: Buffer): void {
|
|
503
|
+
const chunk = appendStreamChunk(this.lineBuffer, this.decoder.write(data), undefined);
|
|
504
|
+
this.lineBuffer = chunk.lineBuffer;
|
|
505
|
+
const turn = this.active;
|
|
506
|
+
if (!turn || turn.settled) {
|
|
507
|
+
if (!this.primed) {
|
|
508
|
+
this.pendingLines.push(...chunk.lines);
|
|
509
|
+
if (this.pendingLines.length > AgyProviderDriver.MAX_PENDING_LINES) {
|
|
510
|
+
this.pendingLines.splice(0, this.pendingLines.length - AgyProviderDriver.MAX_PENDING_LINES);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
for (const line of chunk.lines) this.handleLine(turn, line);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
private handleLine(turn: ActiveTurn, line: string): void {
|
|
519
|
+
if (turn.settled) return;
|
|
520
|
+
const parsed = parseStreamLine(line);
|
|
521
|
+
if (!parsed) return;
|
|
522
|
+
const step = parsed.step_update;
|
|
523
|
+
if (step?.step_type === "agent_response") {
|
|
524
|
+
this.pushLiveText(turn, step.text_delta, undefined);
|
|
525
|
+
// response_text may arrive on the same record family in other builds.
|
|
526
|
+
const record = step as { response_text?: string };
|
|
527
|
+
if (typeof record.response_text === "string") {
|
|
528
|
+
this.pushLiveText(turn, undefined, record.response_text);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const progress = formatStepProgress(parsed);
|
|
532
|
+
if (progress && !parsed.result) this.pushActivity(turn, progress);
|
|
533
|
+
// Shared accumulator: terminal status, usage, denied actions, and
|
|
534
|
+
// conversation ids track exactly like the former per-turn runner.
|
|
535
|
+
turn.runResult = accumulateRunResult(parsed, turn.runResult);
|
|
536
|
+
if (parsed.event === "result" || parsed.result) {
|
|
537
|
+
this.settleFromResult(turn);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
private settleFromResult(turn: ActiveTurn): void {
|
|
542
|
+
if (turn.settled) return;
|
|
543
|
+
const result = turn.runResult;
|
|
544
|
+
const status = result.terminal_status;
|
|
545
|
+
if (status && status !== "SUCCESS" && status !== "OK") {
|
|
546
|
+
this.settleError(
|
|
547
|
+
turn,
|
|
548
|
+
new Error(
|
|
549
|
+
`agy provider turn failed (${status})${result.terminal_error ? `: ${result.terminal_error}` : ""}`,
|
|
550
|
+
),
|
|
551
|
+
);
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
if ((result.denied_actions?.length ?? 0) > 0 && !result.response_complete && !turn.text) {
|
|
555
|
+
this.settleError(
|
|
556
|
+
turn,
|
|
557
|
+
new Error(
|
|
558
|
+
"agy denied all actions for this turn with no response; retry with a narrower request or enable permissions",
|
|
559
|
+
),
|
|
560
|
+
);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
// The terminal response is authoritative when present (identical final
|
|
564
|
+
// text to the per-turn runner); streamed deltas already reached the UI.
|
|
565
|
+
const text = result.response_complete && result.response ? result.response : turn.text;
|
|
566
|
+
this.settleDone(turn, { text });
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
private settleDone(
|
|
570
|
+
turn: ActiveTurn,
|
|
571
|
+
overrides?: { text?: string; aborted?: boolean; timedOut?: boolean },
|
|
572
|
+
): void {
|
|
573
|
+
if (turn.settled) return;
|
|
574
|
+
turn.settled = true;
|
|
575
|
+
turn.suspended = false;
|
|
576
|
+
this.clearSuspendedTimer(turn);
|
|
577
|
+
if (turn.timer) {
|
|
578
|
+
clearTimeout(turn.timer);
|
|
579
|
+
turn.timer = undefined;
|
|
580
|
+
}
|
|
581
|
+
if (turn.onAbort) turn.signal.removeEventListener("abort", turn.onAbort);
|
|
582
|
+
if (this.active === turn) this.active = undefined;
|
|
583
|
+
// Bind the conversation the turn actually produced: the next turn
|
|
584
|
+
// reuses only when it asks for this exact conversation (or a fresh
|
|
585
|
+
// process when none is bound). Unterminated line remnants belong to no
|
|
586
|
+
// turn and must never bleed into the next one.
|
|
587
|
+
if (turn.runResult.conversation_id) {
|
|
588
|
+
this.boundConversationId = turn.runResult.conversation_id;
|
|
589
|
+
}
|
|
590
|
+
this.lineBuffer = "";
|
|
591
|
+
const phase = turn.phase;
|
|
592
|
+
turn.phase = undefined;
|
|
593
|
+
phase?.resolve({
|
|
594
|
+
kind: "done",
|
|
595
|
+
outcome: {
|
|
596
|
+
text: overrides?.text ?? turn.text,
|
|
597
|
+
truncated: turn.truncated,
|
|
598
|
+
aborted: overrides?.aborted,
|
|
599
|
+
timedOut: overrides?.timedOut,
|
|
600
|
+
conversationId: turn.runResult.conversation_id,
|
|
601
|
+
activity: turn.activity,
|
|
602
|
+
deniedActions: turn.runResult.denied_actions,
|
|
603
|
+
},
|
|
604
|
+
});
|
|
605
|
+
this.armIdleTimer();
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
private settleError(turn: ActiveTurn, error: Error): void {
|
|
609
|
+
if (turn.settled) return;
|
|
610
|
+
turn.settled = true;
|
|
611
|
+
turn.suspended = false;
|
|
612
|
+
this.clearSuspendedTimer(turn);
|
|
613
|
+
if (turn.timer) {
|
|
614
|
+
clearTimeout(turn.timer);
|
|
615
|
+
turn.timer = undefined;
|
|
616
|
+
}
|
|
617
|
+
if (turn.onAbort) turn.signal.removeEventListener("abort", turn.onAbort);
|
|
618
|
+
if (this.active === turn) this.active = undefined;
|
|
619
|
+
const phase = turn.phase;
|
|
620
|
+
turn.phase = undefined;
|
|
621
|
+
phase?.reject(error);
|
|
622
|
+
this.armIdleTimer();
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
private failActive(message: string): void {
|
|
626
|
+
const turn = this.active;
|
|
627
|
+
if (!turn || turn.settled) return;
|
|
628
|
+
this.settleError(turn, new Error(message));
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
private async runTurn(
|
|
632
|
+
profile: ProviderDriverProfile,
|
|
633
|
+
options: ProviderDriverTurn,
|
|
634
|
+
): Promise<DriverTurnEnd> {
|
|
635
|
+
// Queued turns may run after close(): fail closed instead of spawning.
|
|
636
|
+
if (this.closed) throw new Error("agy provider driver was closed");
|
|
637
|
+
return new Promise<DriverTurnEnd>((resolve, reject) => {
|
|
638
|
+
if (options.signal.aborted) {
|
|
639
|
+
reject(new Error("agy provider turn was cancelled"));
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
this.clearIdleTimer();
|
|
643
|
+
// A suspended turn belongs to another Pi turn in flight: concurrent
|
|
644
|
+
// bridged sessions in one directory are unsupported — fail fast
|
|
645
|
+
// instead of stealing (or killing) the parked child.
|
|
646
|
+
if (this.active && this.active.suspended && !this.active.settled) {
|
|
647
|
+
throw new Error(
|
|
648
|
+
"another agy turn is suspended awaiting Pi tools in this directory",
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
this.ensureProcess(profile, options.resumeConversationId);
|
|
652
|
+
const child = this.child;
|
|
653
|
+
if (!child) {
|
|
654
|
+
reject(new Error("agy provider driver has no process"));
|
|
655
|
+
this.armIdleTimer();
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
// Replay anything the process emitted before this turn existed.
|
|
659
|
+
const turn: ActiveTurn = {
|
|
660
|
+
signal: options.signal,
|
|
661
|
+
timeoutMs: options.timeoutMs,
|
|
662
|
+
onText: options.onText,
|
|
663
|
+
onActivity: options.onActivity,
|
|
664
|
+
text: "",
|
|
665
|
+
truncated: false,
|
|
666
|
+
activity: [],
|
|
667
|
+
seenActivity: new Set<string>(),
|
|
668
|
+
runResult: { response: "" },
|
|
669
|
+
settled: false,
|
|
670
|
+
suspended: false,
|
|
671
|
+
phase: { resolve, reject },
|
|
672
|
+
};
|
|
673
|
+
this.active = turn;
|
|
674
|
+
this.stats.turns += 1;
|
|
675
|
+
this.log(`turn:${profile.fullModel}:${options.resumeConversationId ? "resume" : "fresh"}`);
|
|
676
|
+
if (!this.primed) {
|
|
677
|
+
this.primed = true;
|
|
678
|
+
const pending = this.pendingLines;
|
|
679
|
+
this.pendingLines = [];
|
|
680
|
+
for (const line of pending) this.handleLine(turn, line);
|
|
681
|
+
if (turn.settled) return;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const killFor = (flag: "aborted" | "timedOut", message: string): void => {
|
|
685
|
+
if (turn.settled) return;
|
|
686
|
+
// A turn cannot be cancelled headless without killing the child; the
|
|
687
|
+
// next turn respawns lazily. Partial text settles with a flag, never
|
|
688
|
+
// as a clean result. A stale signal during suspension abandons the
|
|
689
|
+
// turn: parks are orphaned loudly instead of stranding agy.
|
|
690
|
+
if (turn.suspended) {
|
|
691
|
+
this.killChild();
|
|
692
|
+
this.failParksOrphaned(message);
|
|
693
|
+
this.settleError(turn, new Error(message));
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
this.killChild();
|
|
697
|
+
if (turn.text) {
|
|
698
|
+
this.settleDone(turn, flag === "aborted" ? { aborted: true } : { timedOut: true });
|
|
699
|
+
} else {
|
|
700
|
+
this.settleError(turn, new Error(message));
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
const onAbort = (): void => killFor("aborted", "agy provider turn was cancelled");
|
|
705
|
+
turn.onAbort = onAbort;
|
|
706
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
707
|
+
turn.timer = setTimeout(() => {
|
|
708
|
+
killFor("timedOut", "agy provider turn timed out");
|
|
709
|
+
}, Math.max(1, Math.min(PROVIDER_TURN_TIMEOUT_MS, options.timeoutMs)));
|
|
710
|
+
(turn.timer as unknown as { unref?: () => void }).unref?.();
|
|
711
|
+
|
|
712
|
+
const line = `${JSON.stringify({
|
|
713
|
+
event: "user",
|
|
714
|
+
message: { role: "user", content: options.prompt },
|
|
715
|
+
})}\n`;
|
|
716
|
+
try {
|
|
717
|
+
const stdin = child.stdin;
|
|
718
|
+
if (!stdin) throw new Error("agy driver stdin unavailable");
|
|
719
|
+
stdin.write(line);
|
|
720
|
+
} catch (error) {
|
|
721
|
+
this.settleError(turn, new Error(`failed to write to agy driver: ${error instanceof Error ? error.message : String(error)}`));
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/** Parent-side turn deadline shared with the per-turn runner. */
|
|
728
|
+
export const PROVIDER_TURN_TIMEOUT_MS = 600_000;
|