@juanbenjumea/pi-agy 0.7.9 → 0.7.11
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 +10 -0
- package/extensions/command-run.ts +15 -2
- package/extensions/index.ts +5 -7
- package/extensions/lib/bridge-input-budget.ts +37 -0
- package/extensions/lib/cli.ts +4 -12
- package/extensions/lib/execute.ts +11 -13
- package/extensions/lib/provider-driver.ts +93 -67
- package/extensions/lib/provider-mcp.ts +195 -30
- package/extensions/lib/provider.ts +122 -98
- package/extensions/lib/request.ts +25 -0
- package/extensions/lib/spawn.ts +15 -13
- package/extensions/tools.ts +15 -34
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.7.11
|
|
4
|
+
|
|
5
|
+
- Preserve exclusive provider-turn ownership across tool suspension, resumption, and terminal delivery; reject competing turns before they alter bridge tools.
|
|
6
|
+
- Retain terminal responses received while awaiting Pi tools for exactly one continuation, with bounded abandonment and safe process reuse.
|
|
7
|
+
- Reclaim orphaned bridge calls, suspended state, staged images, and unpolled escalation results on completion or abandonment, including races before suspension publication.
|
|
8
|
+
- Add lifecycle and real-loopback/fake-process regression coverage; 455 tests, typecheck, and package checks pass.
|
|
9
|
+
|
|
10
|
+
## 0.7.10
|
|
11
|
+
|
|
12
|
+
- Harden process-tree cancellation, provider tool forwarding, bridge reconnection, terminal response handling, and cancelled resumes.
|
|
13
|
+
- Bound bridge request complexity and refresh the MCP dependency floor and qualified Pi development dependencies.
|
|
14
|
+
- Derive delegation model aliases and tool schemas from one definition; normalize conversation selection while preserving resume behavior.
|
|
15
|
+
- Keep completed background polling compact; retrieve full results with `agy_tasks collect`.
|
|
16
|
+
- Expand regression coverage to 414 tests; typecheck and package checks pass.
|
|
17
|
+
|
|
3
18
|
## 0.7.9
|
|
4
19
|
|
|
5
20
|
- Rewrite the README as a concise overview of agy task delegation and the Antigravity provider, retaining installation, configuration, and development essentials.
|
package/README.md
CHANGED
|
@@ -43,6 +43,14 @@ Provider turns use a persistent agy process per working directory and can stage
|
|
|
43
43
|
|
|
44
44
|
Do not install `@estebanforge/pi-antigravity-bridge` alongside this package: both register the `antigravity` provider and `/agy` command.
|
|
45
45
|
|
|
46
|
+
### Bridge input limits
|
|
47
|
+
|
|
48
|
+
Authenticated bridge POSTs retain a **1 MiB UTF-8 body cap** and allow at most **10,000 object properties plus array elements** across the entire parsed JSON-RPC body, including metadata, with at most **64 container levels** (root container = level 1). Scalar string content counts toward bytes, not structural elements. Overly wide/deep requests return HTTP **413** before SDK validation, session allocation or tool parking; oversized byte streams retain the existing connection-close behavior. Authentication and localhost Host checks happen first, but authentication alone is not a complexity budget.
|
|
49
|
+
|
|
50
|
+
The bridge uses the SDK's low-level `Server`, not the high-level `McpServer` tool validator, so it enforces these local structural limits rather than relying on `maxToolInputElements`. These are defensive input bounds, not a guarantee against all resource exhaustion.
|
|
51
|
+
|
|
52
|
+
The SDK dependency floor is `^1.32.2`; the repository lock qualifies SDK 1.32.2, Zod 4.6.7 and body-parser 2.4.0. A library consumer resolves its own dependency graph: the SDK still permits older Zod through `^3.25 || ^4.0`, and overrides in this package would not enforce a consumer's resolutions. Audit the actual consumer tree; a patched repository lock is not a universal downstream security guarantee.
|
|
53
|
+
|
|
46
54
|
## Configuration
|
|
47
55
|
|
|
48
56
|
Optional file: `$PI_CODING_AGENT_DIR/agy-config.json` (default `~/.pi/agent/agy-config.json`). For example:
|
|
@@ -59,6 +67,8 @@ Optional file: `$PI_CODING_AGENT_DIR/agy-config.json` (default `~/.pi/agent/agy-
|
|
|
59
67
|
|
|
60
68
|
## Development
|
|
61
69
|
|
|
70
|
+
Development tests are qualified against the exact Pi SDK fixture version 1.5.2, which requires Node >=22.19.0; qualification ran on Node 22.23.4. The resolved development graph is qualified; embedded Undici is not independently attested.
|
|
71
|
+
|
|
62
72
|
```bash
|
|
63
73
|
npm test
|
|
64
74
|
npm run typecheck
|
|
@@ -8,6 +8,7 @@ import { describeWhen, truncate } from "./lib/output.js";
|
|
|
8
8
|
import { buildAcceptEditsConfirm } from "./lib/confirm.js";
|
|
9
9
|
import { AGY_RUN_ENTRY_TYPE, type AgyRunReceipt } from "./lib/render.js";
|
|
10
10
|
import { getHistory } from "./lib/sessions.js";
|
|
11
|
+
import { normalizeConversationSelection, type ConversationSelection } from "./lib/request.js";
|
|
11
12
|
|
|
12
13
|
export interface InteractiveRun {
|
|
13
14
|
mode: "plan" | "accept-edits" | "sandbox";
|
|
@@ -83,11 +84,23 @@ export async function executeConfirmedRun(
|
|
|
83
84
|
run: InteractiveRun,
|
|
84
85
|
): Promise<void> {
|
|
85
86
|
const cwd = ctx.cwd;
|
|
87
|
+
let selection: ConversationSelection;
|
|
88
|
+
try {
|
|
89
|
+
selection = normalizeConversationSelection(run);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (ctx.signal?.aborted) {
|
|
92
|
+
ctx.ui.notify("agy: cancelled", "info");
|
|
93
|
+
} else {
|
|
94
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
95
|
+
ctx.ui.notify(`agy failed: ${message}`, "error");
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
86
99
|
await ctx.waitForIdle();
|
|
87
100
|
const requestedContextMode = run.context ?? "none";
|
|
88
101
|
const contextMode = effectiveResumeContextMode(
|
|
89
102
|
requestedContextMode,
|
|
90
|
-
|
|
103
|
+
selection.kind !== "new",
|
|
91
104
|
);
|
|
92
105
|
const contextText =
|
|
93
106
|
contextMode === "none"
|
|
@@ -127,7 +140,7 @@ export async function executeConfirmedRun(
|
|
|
127
140
|
timeout_ms: run.timeout_ms ?? DEFAULT_TIMEOUT_MS,
|
|
128
141
|
conversation_id: run.conversation_id,
|
|
129
142
|
continue: run.continue,
|
|
130
|
-
new_session:
|
|
143
|
+
new_session: selection.kind === "new",
|
|
131
144
|
stream: true,
|
|
132
145
|
context: contextMode,
|
|
133
146
|
context_text: contextText,
|
package/extensions/index.ts
CHANGED
|
@@ -86,13 +86,11 @@ function registerAgyProvider(pi: ExtensionAPI): void {
|
|
|
86
86
|
streamSimple(
|
|
87
87
|
{ id: model.id },
|
|
88
88
|
context.messages as unknown as ProviderTranscriptMessage[],
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
: undefined,
|
|
89
|
+
{
|
|
90
|
+
signal: options?.signal,
|
|
91
|
+
reasoning: options?.reasoning,
|
|
92
|
+
tools: getCurrentTools(context.messages) as unknown as ProviderToolSource[],
|
|
93
|
+
},
|
|
96
94
|
) as unknown as AssistantMessageEventStream,
|
|
97
95
|
});
|
|
98
96
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Limits for the complete parsed bridge request, including JSON-RPC metadata. */
|
|
2
|
+
export const MAX_BRIDGE_INPUT_ELEMENTS = 10_000;
|
|
3
|
+
export const MAX_BRIDGE_INPUT_DEPTH = 64;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Bounds structure before SDK validation. Input must come from JSON.parse:
|
|
7
|
+
* this is not schema validation or a walker for arbitrary JS accessors/proxies.
|
|
8
|
+
* Count object properties and array slots; only containers contribute depth.
|
|
9
|
+
* The root container has depth 1. Strings remain subject to the byte reader.
|
|
10
|
+
*/
|
|
11
|
+
export function isBridgeInputWithinBudget(value: unknown): boolean {
|
|
12
|
+
if (value === null || typeof value !== "object") return true;
|
|
13
|
+
const pending: Array<{ node: object; depth: number }> = [{ node: value, depth: 1 }];
|
|
14
|
+
let elements = 0;
|
|
15
|
+
while (pending.length > 0) {
|
|
16
|
+
const { node, depth } = pending.pop()!;
|
|
17
|
+
const array = Array.isArray(node);
|
|
18
|
+
if (array) {
|
|
19
|
+
// Refuse wide arrays before reading or queuing any of their children.
|
|
20
|
+
elements += node.length;
|
|
21
|
+
if (elements > MAX_BRIDGE_INPUT_ELEMENTS) return false;
|
|
22
|
+
}
|
|
23
|
+
// Do not allocate Object.entries/values or flatten all descendants.
|
|
24
|
+
for (const key in node) {
|
|
25
|
+
if (!Object.hasOwn(node, key)) continue;
|
|
26
|
+
if (!array && ++elements > MAX_BRIDGE_INPUT_ELEMENTS) return false;
|
|
27
|
+
const child = (node as Record<string, unknown>)[key];
|
|
28
|
+
if (child !== null && typeof child === "object") {
|
|
29
|
+
if (depth >= MAX_BRIDGE_INPUT_DEPTH) return false;
|
|
30
|
+
// Every queued child is charged to an already-counted property/slot:
|
|
31
|
+
// the worklist cannot exceed the finite element budget.
|
|
32
|
+
pending.push({ node: child, depth: depth + 1 });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
package/extensions/lib/cli.ts
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
1
|
export { detectVerifyCommand, detectVerifyCommandFromPackageJson } from "./verify.js";
|
|
2
2
|
export { parseJsonResponse } from "./parse.js";
|
|
3
3
|
|
|
4
|
-
export
|
|
5
|
-
| "flash-low"
|
|
6
|
-
| "flash-medium"
|
|
7
|
-
| "flash-high"
|
|
8
|
-
| "pro-low"
|
|
9
|
-
| "pro-high"
|
|
10
|
-
| "sonnet"
|
|
11
|
-
| "opus"
|
|
12
|
-
| "gpt-oss";
|
|
13
|
-
|
|
14
|
-
export const AGY_MODEL_ALIASES: readonly AgyModel[] = [
|
|
4
|
+
export const AGY_MODEL_ALIASES = [
|
|
15
5
|
"flash-low",
|
|
16
6
|
"flash-medium",
|
|
17
7
|
"flash-high",
|
|
@@ -20,7 +10,9 @@ export const AGY_MODEL_ALIASES: readonly AgyModel[] = [
|
|
|
20
10
|
"sonnet",
|
|
21
11
|
"opus",
|
|
22
12
|
"gpt-oss",
|
|
23
|
-
];
|
|
13
|
+
] as const;
|
|
14
|
+
|
|
15
|
+
export type AgyModel = typeof AGY_MODEL_ALIASES[number];
|
|
24
16
|
|
|
25
17
|
export function isAgyModel(value: unknown): value is AgyModel {
|
|
26
18
|
return typeof value === "string" && (AGY_MODEL_ALIASES as readonly string[]).includes(value);
|
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
} from "./postflight.js";
|
|
36
36
|
import { resetPreflightCache, runPreflight } from "./preflight.js";
|
|
37
37
|
import { getHistory, getSession, saveSession, conversationSummary } from "./sessions.js";
|
|
38
|
+
import { normalizeConversationSelection } from "./request.js";
|
|
38
39
|
|
|
39
40
|
export type AgyMode = "plan" | "accept-edits" | "sandbox";
|
|
40
41
|
|
|
@@ -92,6 +93,7 @@ export async function executeAgyTask(
|
|
|
92
93
|
onProgress?: (message: string) => void,
|
|
93
94
|
): Promise<AgyExecutionResult> {
|
|
94
95
|
validateAgyExecutionOptions(options);
|
|
96
|
+
const selection = normalizeConversationSelection(options);
|
|
95
97
|
let agent = normalizeAgyAgentName(options.agent);
|
|
96
98
|
const startedAt = Date.now();
|
|
97
99
|
const budgetController = new AbortController();
|
|
@@ -123,17 +125,18 @@ export async function executeAgyTask(
|
|
|
123
125
|
throw new Error(`Working directory is not a directory: ${options.dir}`);
|
|
124
126
|
}
|
|
125
127
|
|
|
126
|
-
let conversationId =
|
|
127
|
-
|
|
128
|
+
let conversationId = selection.kind === "resume" ? selection.id : undefined;
|
|
129
|
+
// Explicit IDs with new_session=false historically inherit the latest
|
|
130
|
+
// agent too. This lookup policy is separate from the selected CLI ID.
|
|
131
|
+
const resumesLatest = selection.kind === "latest" || options.new_session === false;
|
|
128
132
|
const prior =
|
|
129
133
|
(!conversationId && options.new_session !== true) || (!agent && resumesLatest)
|
|
130
134
|
? await getSession(options.dir)
|
|
131
135
|
: undefined;
|
|
132
136
|
if (
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
prior?.last_conversation_id
|
|
136
|
-
options.new_session === false
|
|
137
|
+
selection.kind === "latest" &&
|
|
138
|
+
selection.source === "recorded" &&
|
|
139
|
+
prior?.last_conversation_id
|
|
137
140
|
) {
|
|
138
141
|
conversationId = prior.last_conversation_id;
|
|
139
142
|
}
|
|
@@ -227,7 +230,7 @@ export async function executeAgyTask(
|
|
|
227
230
|
dir: options.dir,
|
|
228
231
|
timeout_ms: runTimeoutMs,
|
|
229
232
|
conversation_id: conversationId,
|
|
230
|
-
continue:
|
|
233
|
+
continue: selection.kind === "latest" && selection.source === "cli",
|
|
231
234
|
stream: options.stream ?? true,
|
|
232
235
|
skipPermissions,
|
|
233
236
|
},
|
|
@@ -354,12 +357,7 @@ export function validateAgyExecutionOptions(options: AgyExecutionOptions): void
|
|
|
354
357
|
if (!Number.isFinite(options.timeout_ms) || options.timeout_ms <= 0) {
|
|
355
358
|
throw new Error("agy timeout_ms must be a positive finite number");
|
|
356
359
|
}
|
|
357
|
-
|
|
358
|
-
throw new Error("agy cannot use --continue and --conversation together");
|
|
359
|
-
}
|
|
360
|
-
if (options.new_session === true && (options.continue || options.conversation_id)) {
|
|
361
|
-
throw new Error("agy new_session cannot be combined with a conversation to resume");
|
|
362
|
-
}
|
|
360
|
+
normalizeConversationSelection(options);
|
|
363
361
|
}
|
|
364
362
|
|
|
365
363
|
function remainingBudget(startedAt: number, timeoutMs: number): number {
|
|
@@ -126,12 +126,14 @@ interface ActiveTurn {
|
|
|
126
126
|
activity: string[];
|
|
127
127
|
seenActivity: Set<string>;
|
|
128
128
|
runResult: AgyRunResult;
|
|
129
|
-
/**
|
|
129
|
+
/** Process outcome is final; pendingTerminal may still own continuation delivery. */
|
|
130
130
|
settled: boolean;
|
|
131
131
|
/** Suspended mid-turn on a parked bridge call: child alive, timers off. */
|
|
132
132
|
suspended: boolean;
|
|
133
133
|
/** Live waiter for the current phase (turn() or continueTurn()). */
|
|
134
134
|
phase?: TurnPhase;
|
|
135
|
+
/** Terminal envelope received during a park; owned until consumed or abandoned. */
|
|
136
|
+
pendingTerminal?: { kind: "done"; end: DriverTurnEnd } | { kind: "error"; error: Error };
|
|
135
137
|
timer?: NodeJS.Timeout;
|
|
136
138
|
suspendedTimer?: NodeJS.Timeout;
|
|
137
139
|
onAbort?: () => void;
|
|
@@ -203,13 +205,13 @@ export class AgyProviderDriver {
|
|
|
203
205
|
clearTimeout(turn.timer);
|
|
204
206
|
turn.timer = undefined;
|
|
205
207
|
}
|
|
206
|
-
//
|
|
207
|
-
// hold the child forever.
|
|
208
|
+
// Keep this deadline through terminal-awaiting-consumption too: a lost
|
|
209
|
+
// Pi continuation must not hold the child, parks, images or driver forever.
|
|
208
210
|
turn.suspendedTimer = setTimeout(() => {
|
|
211
|
+
if (this.active !== turn || (!turn.pendingTerminal && (turn.settled || !turn.suspended))) return;
|
|
209
212
|
turn.suspendedTimer = undefined;
|
|
210
213
|
this.killChild();
|
|
211
|
-
this.
|
|
212
|
-
this.settleError(turn, new Error("agy provider turn was abandoned while awaiting Pi tools"));
|
|
214
|
+
this.abandonTurn(turn, new Error("agy provider turn was abandoned while awaiting Pi tools"));
|
|
213
215
|
}, SUSPENDED_TURN_MAX_MS);
|
|
214
216
|
(turn.suspendedTimer as unknown as { unref?: () => void }).unref?.();
|
|
215
217
|
const phase = turn.phase;
|
|
@@ -234,6 +236,32 @@ export class AgyProviderDriver {
|
|
|
234
236
|
}
|
|
235
237
|
}
|
|
236
238
|
|
|
239
|
+
/** Abandonment has no future consumer: orphan parks once and release ownership. */
|
|
240
|
+
private abandonTurn(turn: ActiveTurn, error: Error): void {
|
|
241
|
+
if (this.active !== turn) return;
|
|
242
|
+
const parked = turn.suspended || !!turn.pendingTerminal;
|
|
243
|
+
// Teardown must also release a retained terminal, but must never replace it
|
|
244
|
+
// with an abort/transport failure during ordinary terminal settlement.
|
|
245
|
+
turn.pendingTerminal = undefined;
|
|
246
|
+
turn.suspended = false;
|
|
247
|
+
if (parked) this.failParksOrphaned(error.message);
|
|
248
|
+
if (turn.settled) {
|
|
249
|
+
this.active = undefined;
|
|
250
|
+
this.clearSuspendedTimer(turn);
|
|
251
|
+
this.armIdleTimer();
|
|
252
|
+
} else {
|
|
253
|
+
this.settleError(turn, error);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private interruptTurn(turn: ActiveTurn, flag: "aborted" | "timedOut", message: string): void {
|
|
258
|
+
if (turn.settled || this.active !== turn) return;
|
|
259
|
+
this.killChild();
|
|
260
|
+
if (turn.suspended) this.abandonTurn(turn, new Error(message));
|
|
261
|
+
else if (turn.text) this.settleDone(turn, flag === "aborted" ? { aborted: true } : { timedOut: true });
|
|
262
|
+
else this.settleError(turn, new Error(message));
|
|
263
|
+
}
|
|
264
|
+
|
|
237
265
|
/**
|
|
238
266
|
* Resume a suspended turn after its parked calls were completed.
|
|
239
267
|
* Re-arms the full turn timeout (round-trips wait on user approvals) and
|
|
@@ -249,39 +277,45 @@ export class AgyProviderDriver {
|
|
|
249
277
|
}): Promise<DriverTurnEnd> {
|
|
250
278
|
const turn = this.active;
|
|
251
279
|
if (this.closed) throw new Error("agy provider driver was closed");
|
|
280
|
+
if (turn?.pendingTerminal) {
|
|
281
|
+
const terminal = turn.pendingTerminal;
|
|
282
|
+
turn.pendingTerminal = undefined;
|
|
283
|
+
this.clearSuspendedTimer(turn);
|
|
284
|
+
this.active = undefined;
|
|
285
|
+
this.armIdleTimer();
|
|
286
|
+
return terminal.kind === "done" ? Promise.resolve(terminal.end) : Promise.reject(terminal.error);
|
|
287
|
+
}
|
|
252
288
|
if (!turn || turn.settled || !turn.suspended) {
|
|
253
289
|
throw new Error("agy provider driver has no suspended turn");
|
|
254
290
|
}
|
|
255
291
|
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
292
|
return new Promise<DriverTurnEnd>((resolve, reject) => {
|
|
293
|
+
// Install the phase waiter before registering an abort listener: an
|
|
294
|
+
// already-aborted signal may settle synchronously.
|
|
278
295
|
turn.phase = { resolve, reject };
|
|
296
|
+
turn.suspended = false;
|
|
297
|
+
this.clearSuspendedTimer(turn);
|
|
298
|
+
if (options?.onText) turn.onText = options.onText;
|
|
299
|
+
if (options?.onActivity) turn.onActivity = options.onActivity;
|
|
300
|
+
if (options?.timeoutMs !== undefined) turn.timeoutMs = options.timeoutMs;
|
|
301
|
+
if (turn.onAbort) {
|
|
302
|
+
turn.signal.removeEventListener("abort", turn.onAbort);
|
|
303
|
+
turn.onAbort = undefined;
|
|
304
|
+
}
|
|
305
|
+
{
|
|
306
|
+
if (options?.signal) turn.signal = options.signal;
|
|
307
|
+
const onAbort = (): void => {
|
|
308
|
+
this.interruptTurn(turn, "aborted", "agy provider turn was cancelled");
|
|
309
|
+
};
|
|
310
|
+
turn.onAbort = onAbort;
|
|
311
|
+
turn.signal.addEventListener("abort", onAbort, { once: true });
|
|
312
|
+
if (turn.signal.aborted) onAbort();
|
|
313
|
+
}
|
|
314
|
+
if (turn.settled) return;
|
|
279
315
|
turn.timer = setTimeout(() => {
|
|
280
316
|
// A resumed timeout kills the hung child like a fresh one: never
|
|
281
317
|
// leave it running to be reused.
|
|
282
|
-
this.
|
|
283
|
-
if (turn.text) this.settleDone(turn, { timedOut: true });
|
|
284
|
-
else this.settleError(turn, new Error("agy provider turn timed out"));
|
|
318
|
+
this.interruptTurn(turn, "timedOut", "agy provider turn timed out");
|
|
285
319
|
}, Math.max(1, Math.min(PROVIDER_TURN_TIMEOUT_MS, turn.timeoutMs)));
|
|
286
320
|
(turn.timer as unknown as { unref?: () => void }).unref?.();
|
|
287
321
|
});
|
|
@@ -292,8 +326,7 @@ export class AgyProviderDriver {
|
|
|
292
326
|
const turn = this.active;
|
|
293
327
|
if (!turn) return;
|
|
294
328
|
this.killChild();
|
|
295
|
-
this.
|
|
296
|
-
this.settleError(turn, new Error(message));
|
|
329
|
+
this.abandonTurn(turn, new Error(message));
|
|
297
330
|
}
|
|
298
331
|
|
|
299
332
|
snapshot(): ProviderDriverSnapshot {
|
|
@@ -310,9 +343,9 @@ export class AgyProviderDriver {
|
|
|
310
343
|
async close(): Promise<void> {
|
|
311
344
|
this.closed = true;
|
|
312
345
|
this.clearIdleTimer();
|
|
313
|
-
if (this.active
|
|
314
|
-
// Never strand a caller
|
|
315
|
-
this.
|
|
346
|
+
if (this.active) {
|
|
347
|
+
// Never strand a caller or an outstanding park on shutdown.
|
|
348
|
+
this.abandonTurn(this.active, new Error("agy provider driver was closed"));
|
|
316
349
|
}
|
|
317
350
|
this.killChild();
|
|
318
351
|
}
|
|
@@ -562,7 +595,11 @@ export class AgyProviderDriver {
|
|
|
562
595
|
}
|
|
563
596
|
// The terminal response is authoritative when present (identical final
|
|
564
597
|
// text to the per-turn runner); streamed deltas already reached the UI.
|
|
565
|
-
|
|
598
|
+
let text = result.response_complete && result.response ? result.response : turn.text;
|
|
599
|
+
if (text.length > MAX_TURN_TEXT_CHARS) {
|
|
600
|
+
text = text.slice(0, MAX_TURN_TEXT_CHARS);
|
|
601
|
+
turn.truncated = true;
|
|
602
|
+
}
|
|
566
603
|
this.settleDone(turn, { text });
|
|
567
604
|
}
|
|
568
605
|
|
|
@@ -571,15 +608,16 @@ export class AgyProviderDriver {
|
|
|
571
608
|
overrides?: { text?: string; aborted?: boolean; timedOut?: boolean },
|
|
572
609
|
): void {
|
|
573
610
|
if (turn.settled) return;
|
|
611
|
+
const retain = turn.suspended && !turn.phase;
|
|
574
612
|
turn.settled = true;
|
|
575
613
|
turn.suspended = false;
|
|
576
|
-
this.clearSuspendedTimer(turn);
|
|
614
|
+
if (!retain) this.clearSuspendedTimer(turn);
|
|
577
615
|
if (turn.timer) {
|
|
578
616
|
clearTimeout(turn.timer);
|
|
579
617
|
turn.timer = undefined;
|
|
580
618
|
}
|
|
581
619
|
if (turn.onAbort) turn.signal.removeEventListener("abort", turn.onAbort);
|
|
582
|
-
if (this.active === turn) this.active = undefined;
|
|
620
|
+
if (!retain && this.active === turn) this.active = undefined;
|
|
583
621
|
// Bind the conversation the turn actually produced: the next turn
|
|
584
622
|
// reuses only when it asks for this exact conversation (or a fresh
|
|
585
623
|
// process when none is bound). Unterminated line remnants belong to no
|
|
@@ -590,7 +628,7 @@ export class AgyProviderDriver {
|
|
|
590
628
|
this.lineBuffer = "";
|
|
591
629
|
const phase = turn.phase;
|
|
592
630
|
turn.phase = undefined;
|
|
593
|
-
|
|
631
|
+
const end: DriverTurnEnd = {
|
|
594
632
|
kind: "done",
|
|
595
633
|
outcome: {
|
|
596
634
|
text: overrides?.text ?? turn.text,
|
|
@@ -601,31 +639,35 @@ export class AgyProviderDriver {
|
|
|
601
639
|
activity: turn.activity,
|
|
602
640
|
deniedActions: turn.runResult.denied_actions,
|
|
603
641
|
},
|
|
604
|
-
}
|
|
605
|
-
|
|
642
|
+
};
|
|
643
|
+
if (retain) turn.pendingTerminal = { kind: "done", end };
|
|
644
|
+
else phase?.resolve(end);
|
|
645
|
+
if (!retain) this.armIdleTimer();
|
|
606
646
|
}
|
|
607
647
|
|
|
608
648
|
private settleError(turn: ActiveTurn, error: Error): void {
|
|
609
649
|
if (turn.settled) return;
|
|
650
|
+
const retain = turn.suspended && !turn.phase;
|
|
610
651
|
turn.settled = true;
|
|
611
652
|
turn.suspended = false;
|
|
612
|
-
this.clearSuspendedTimer(turn);
|
|
653
|
+
if (!retain) this.clearSuspendedTimer(turn);
|
|
613
654
|
if (turn.timer) {
|
|
614
655
|
clearTimeout(turn.timer);
|
|
615
656
|
turn.timer = undefined;
|
|
616
657
|
}
|
|
617
658
|
if (turn.onAbort) turn.signal.removeEventListener("abort", turn.onAbort);
|
|
618
|
-
if (this.active === turn) this.active = undefined;
|
|
659
|
+
if (!retain && this.active === turn) this.active = undefined;
|
|
619
660
|
const phase = turn.phase;
|
|
620
661
|
turn.phase = undefined;
|
|
621
|
-
|
|
622
|
-
|
|
662
|
+
if (retain) turn.pendingTerminal = { kind: "error", error };
|
|
663
|
+
else phase?.reject(error);
|
|
664
|
+
if (!retain) this.armIdleTimer();
|
|
623
665
|
}
|
|
624
666
|
|
|
625
667
|
private failActive(message: string): void {
|
|
626
668
|
const turn = this.active;
|
|
627
669
|
if (!turn || turn.settled) return;
|
|
628
|
-
this.
|
|
670
|
+
this.abandonTurn(turn, new Error(message));
|
|
629
671
|
}
|
|
630
672
|
|
|
631
673
|
private async runTurn(
|
|
@@ -640,12 +682,12 @@ export class AgyProviderDriver {
|
|
|
640
682
|
return;
|
|
641
683
|
}
|
|
642
684
|
this.clearIdleTimer();
|
|
643
|
-
// A
|
|
644
|
-
//
|
|
645
|
-
//
|
|
646
|
-
if (this.active
|
|
685
|
+
// A parked turn retains ownership through resumed running and terminal
|
|
686
|
+
// delivery. The queue covers only its first phase; never steal its child
|
|
687
|
+
// or its pending terminal result after that phase releases the queue.
|
|
688
|
+
if (this.active) {
|
|
647
689
|
throw new Error(
|
|
648
|
-
"another agy turn
|
|
690
|
+
"another agy turn owns this driver (running or suspended awaiting Pi tools)",
|
|
649
691
|
);
|
|
650
692
|
}
|
|
651
693
|
this.ensureProcess(profile, options.resumeConversationId);
|
|
@@ -682,23 +724,7 @@ export class AgyProviderDriver {
|
|
|
682
724
|
}
|
|
683
725
|
|
|
684
726
|
const killFor = (flag: "aborted" | "timedOut", message: string): void => {
|
|
685
|
-
|
|
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
|
-
}
|
|
727
|
+
this.interruptTurn(turn, flag, message);
|
|
702
728
|
};
|
|
703
729
|
|
|
704
730
|
const onAbort = (): void => killFor("aborted", "agy provider turn was cancelled");
|