@juanbenjumea/pi-agy 0.7.9 → 0.7.12
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 +22 -0
- package/README.md +11 -1
- package/extensions/command-run.ts +15 -2
- package/extensions/index.ts +72 -55
- 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-models.ts +14 -4
- package/extensions/lib/provider.ts +123 -99
- 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
|
@@ -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");
|
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
35
35
|
|
|
36
36
|
import { getDefaultConfigPath } from "./config.js";
|
|
37
|
+
import { isBridgeInputWithinBudget } from "./bridge-input-budget.js";
|
|
37
38
|
import { checkAgyUsage, formatAgyUsage } from "./cli.js";
|
|
38
39
|
import {
|
|
39
40
|
FALLBACK_PROVIDER_MODELS,
|
|
@@ -173,8 +174,10 @@ export interface McpBridgeHandle {
|
|
|
173
174
|
completePark: (callId: string, result: BridgeToolResult) => boolean;
|
|
174
175
|
/** Fail every parked call (teardown, cancelled turns). */
|
|
175
176
|
failAllParks: (message: string) => void;
|
|
177
|
+
/** Release call IDs whose owning provider operation can no longer poll them. */
|
|
178
|
+
discardParks: (callIds: Iterable<string>) => void;
|
|
176
179
|
/** Point-in-time description for status surfaces. */
|
|
177
|
-
describe: () => { configDir: string; url: string; toolCount: number };
|
|
180
|
+
describe: () => { configDir: string; url: string; toolCount: number; pendingResults: number };
|
|
178
181
|
}
|
|
179
182
|
|
|
180
183
|
/** A Pi tool projected for agy: MCP-safe advertised name, real Pi identity. */
|
|
@@ -217,6 +220,8 @@ interface CatalogCache {
|
|
|
217
220
|
|
|
218
221
|
export interface StartMcpBridgeOptions {
|
|
219
222
|
escalateAfterMs?: number;
|
|
223
|
+
sessionCap?: number;
|
|
224
|
+
sessionIdleMs?: number;
|
|
220
225
|
}
|
|
221
226
|
|
|
222
227
|
/**
|
|
@@ -230,12 +235,6 @@ export async function startMcpBridge(
|
|
|
230
235
|
const token = randomBytes(32).toString("hex");
|
|
231
236
|
const escalateAfterMs = options?.escalateAfterMs ?? DEFAULT_PARK_ESCALATE_MS;
|
|
232
237
|
let catalogCache: CatalogCache | undefined;
|
|
233
|
-
|
|
234
|
-
const mcp = new McpServer(
|
|
235
|
-
{ name: "pi-agy-bridge", version: "1" },
|
|
236
|
-
{ capabilities: { tools: {} } },
|
|
237
|
-
);
|
|
238
|
-
|
|
239
238
|
let bridgedTools: BridgeToolDef[] = [];
|
|
240
239
|
let parkHandler: BridgeParkHandler | undefined;
|
|
241
240
|
const parks = new Map<string, (result: BridgeToolResult) => void>();
|
|
@@ -244,7 +243,25 @@ export async function startMcpBridge(
|
|
|
244
243
|
{ name: string; state: "running" | "done" | "failed"; result?: BridgeToolResult; reason?: string }
|
|
245
244
|
>();
|
|
246
245
|
|
|
247
|
-
|
|
246
|
+
interface BridgeSession {
|
|
247
|
+
transport: StreamableHTTPServerTransport;
|
|
248
|
+
mcp: McpServer;
|
|
249
|
+
last: number;
|
|
250
|
+
active: number;
|
|
251
|
+
parks: number;
|
|
252
|
+
closing: boolean;
|
|
253
|
+
}
|
|
254
|
+
const sessions = new Map<string, BridgeSession>();
|
|
255
|
+
const pendingSessions = new Set<BridgeSession>();
|
|
256
|
+
const parkOwners = new Map<string, BridgeSession>();
|
|
257
|
+
|
|
258
|
+
const createMcpServer = (session: BridgeSession): McpServer => {
|
|
259
|
+
const mcp = new McpServer(
|
|
260
|
+
{ name: "pi-agy-bridge", version: "1" },
|
|
261
|
+
{ capabilities: { tools: {} } },
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
mcp.setRequestHandler(ListToolsRequestSchema, () => {
|
|
248
265
|
const skills = scanSkills(dir);
|
|
249
266
|
return {
|
|
250
267
|
tools: [
|
|
@@ -410,8 +427,10 @@ export async function startMcpBridge(
|
|
|
410
427
|
}
|
|
411
428
|
let escalateTimer: NodeJS.Timeout | undefined;
|
|
412
429
|
let didEscalate = false;
|
|
430
|
+
let settled = false;
|
|
413
431
|
const parked = await new Promise<BridgeToolResult | undefined>((resolve) => {
|
|
414
432
|
parks.set(callId, (res) => {
|
|
433
|
+
settled = true;
|
|
415
434
|
if (escalateTimer) clearTimeout(escalateTimer);
|
|
416
435
|
if (didEscalate) {
|
|
417
436
|
const record = escalations.get(callId);
|
|
@@ -423,6 +442,8 @@ export async function startMcpBridge(
|
|
|
423
442
|
resolve(res);
|
|
424
443
|
}
|
|
425
444
|
});
|
|
445
|
+
session.parks += 1;
|
|
446
|
+
parkOwners.set(callId, session);
|
|
426
447
|
let accepted = false;
|
|
427
448
|
try {
|
|
428
449
|
accepted = handler({ callId, toolName: bridged.piName, args: callArgs });
|
|
@@ -431,9 +452,14 @@ export async function startMcpBridge(
|
|
|
431
452
|
}
|
|
432
453
|
if (!accepted) {
|
|
433
454
|
parks.delete(callId);
|
|
455
|
+
if (parkOwners.get(callId) === session) {
|
|
456
|
+
parkOwners.delete(callId);
|
|
457
|
+
session.parks = Math.max(0, session.parks - 1);
|
|
458
|
+
}
|
|
434
459
|
resolve(undefined);
|
|
435
460
|
return;
|
|
436
461
|
}
|
|
462
|
+
if (settled) return;
|
|
437
463
|
// Early-ack escalation: if the tool runs longer than escalateAfterMs,
|
|
438
464
|
// settle the MCP request with STILL RUNNING so agy avoids its 180s timeout.
|
|
439
465
|
escalateTimer = setTimeout(() => {
|
|
@@ -467,18 +493,100 @@ export async function startMcpBridge(
|
|
|
467
493
|
}
|
|
468
494
|
});
|
|
469
495
|
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
496
|
+
return mcp;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
let stopping = false;
|
|
500
|
+
const sessionCap = Number.isFinite(options?.sessionCap) && (options?.sessionCap ?? 16) > 0 ? Math.floor(options?.sessionCap ?? 16) : 16;
|
|
501
|
+
const sessionIdleMs = Number.isFinite(options?.sessionIdleMs) && (options?.sessionIdleMs ?? 300_000) > 0 ? options?.sessionIdleMs ?? 300_000 : 300_000;
|
|
502
|
+
const closeSession = async (id: string, session: BridgeSession) => {
|
|
503
|
+
if (sessions.get(id) !== session) return;
|
|
504
|
+
sessions.delete(id);
|
|
505
|
+
session.closing = true;
|
|
506
|
+
for (const [callId, owner] of parkOwners) {
|
|
507
|
+
if (owner !== session) continue;
|
|
508
|
+
parkOwners.delete(callId);
|
|
509
|
+
session.parks = Math.max(0, session.parks - 1);
|
|
510
|
+
const resolve = parks.get(callId);
|
|
511
|
+
parks.delete(callId);
|
|
512
|
+
resolve?.({ text: "MCP session closed.", isError: true });
|
|
513
|
+
const escalation = escalations.get(callId);
|
|
514
|
+
if (escalation?.state === "running") {
|
|
515
|
+
escalation.state = "failed";
|
|
516
|
+
escalation.reason = "MCP session closed.";
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
await session.transport.close().catch(() => undefined);
|
|
520
|
+
await session.mcp.close().catch(() => undefined);
|
|
521
|
+
};
|
|
522
|
+
const sweep = setInterval(() => {
|
|
523
|
+
const now = Date.now();
|
|
524
|
+
for (const [id, session] of sessions) {
|
|
525
|
+
if (session.active === 0 && session.parks === 0 && now - session.last >= sessionIdleMs) void closeSession(id, session);
|
|
526
|
+
}
|
|
527
|
+
}, Math.max(50, Math.min(sessionIdleMs, 60_000)));
|
|
528
|
+
sweep.unref();
|
|
529
|
+
|
|
476
530
|
const configDir = allocateBridgeConfigDir();
|
|
477
531
|
// Filled after bind; the request handler validates Host against it
|
|
478
532
|
// (defense-in-depth against DNS rebinding past the loopback bind).
|
|
479
533
|
const bound = { port: 0 };
|
|
480
534
|
const http = createServer((req, res) => {
|
|
481
|
-
void handleBridgeRequest(req, res,
|
|
535
|
+
void handleBridgeRequest(req, res, token, bound.port, async (request, body) => {
|
|
536
|
+
const requested = request.headers["mcp-session-id"];
|
|
537
|
+
if (typeof requested === "string") {
|
|
538
|
+
const session = sessions.get(requested);
|
|
539
|
+
if (!session) return undefined;
|
|
540
|
+
session.last = Date.now();
|
|
541
|
+
session.active += 1;
|
|
542
|
+
return { transport: session.transport, session };
|
|
543
|
+
}
|
|
544
|
+
if (!body || typeof body !== "object" || (body as { method?: string }).method !== "initialize") return undefined;
|
|
545
|
+
if (stopping || sessions.size + pendingSessions.size >= sessionCap) return undefined;
|
|
546
|
+
let removeSession = (): void => undefined;
|
|
547
|
+
let session!: BridgeSession;
|
|
548
|
+
const transport = new StreamableHTTPServerTransport({
|
|
549
|
+
sessionIdGenerator: () => randomUUID(),
|
|
550
|
+
onsessioninitialized: (id) => {
|
|
551
|
+
pendingSessions.delete(session);
|
|
552
|
+
if (!stopping && !session.closing) sessions.set(id, session);
|
|
553
|
+
},
|
|
554
|
+
onsessionclosed: () => removeSession(),
|
|
555
|
+
});
|
|
556
|
+
session = { transport, mcp: undefined as never, last: Date.now(), active: 1, parks: 0, closing: false };
|
|
557
|
+
session.mcp = createMcpServer(session);
|
|
558
|
+
pendingSessions.add(session);
|
|
559
|
+
removeSession = () => {
|
|
560
|
+
const id = session.transport.sessionId;
|
|
561
|
+
if (id) void closeSession(id, session);
|
|
562
|
+
else {
|
|
563
|
+
pendingSessions.delete(session);
|
|
564
|
+
session.closing = true;
|
|
565
|
+
void session.transport.close().catch(() => undefined);
|
|
566
|
+
void session.mcp.close().catch(() => undefined);
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
session.transport.onclose = removeSession;
|
|
570
|
+
try {
|
|
571
|
+
await session.mcp.connect(session.transport);
|
|
572
|
+
return { transport: session.transport, session };
|
|
573
|
+
} catch {
|
|
574
|
+
await session.transport.close().catch(() => undefined);
|
|
575
|
+
await session.mcp.close().catch(() => undefined);
|
|
576
|
+
return undefined;
|
|
577
|
+
}
|
|
578
|
+
}, (session) => {
|
|
579
|
+
const id = session.transport.sessionId;
|
|
580
|
+
if (id && sessions.get(id) === session) return;
|
|
581
|
+
pendingSessions.delete(session);
|
|
582
|
+
if (id && !session.closing && !stopping) {
|
|
583
|
+
sessions.set(id, session);
|
|
584
|
+
} else if (!session.closing) {
|
|
585
|
+
session.closing = true;
|
|
586
|
+
void session.transport.close().catch(() => undefined);
|
|
587
|
+
void session.mcp.close().catch(() => undefined);
|
|
588
|
+
}
|
|
589
|
+
}).catch(() => {
|
|
482
590
|
// handleBridgeRequest guards every response path; this is unreachable
|
|
483
591
|
// belt-and-braces so a client error can never crash Pi.
|
|
484
592
|
if (!res.headersSent) {
|
|
@@ -512,9 +620,14 @@ export async function startMcpBridge(
|
|
|
512
620
|
}
|
|
513
621
|
}
|
|
514
622
|
if (parks.size === 0) return;
|
|
515
|
-
const pending = [...parks.
|
|
623
|
+
const pending = [...parks.entries()];
|
|
516
624
|
parks.clear();
|
|
517
|
-
for (const resolve of pending) {
|
|
625
|
+
for (const [callId, resolve] of pending) {
|
|
626
|
+
const owner = parkOwners.get(callId);
|
|
627
|
+
if (owner) {
|
|
628
|
+
parkOwners.delete(callId);
|
|
629
|
+
owner.parks = Math.max(0, owner.parks - 1);
|
|
630
|
+
}
|
|
518
631
|
try {
|
|
519
632
|
resolve({ text: message, isError: true });
|
|
520
633
|
} catch {
|
|
@@ -524,6 +637,12 @@ export async function startMcpBridge(
|
|
|
524
637
|
};
|
|
525
638
|
|
|
526
639
|
const completePark = (callId: string, result: BridgeToolResult): boolean => {
|
|
640
|
+
const owner = parkOwners.get(callId);
|
|
641
|
+
if (owner) {
|
|
642
|
+
parkOwners.delete(callId);
|
|
643
|
+
owner.parks = Math.max(0, owner.parks - 1);
|
|
644
|
+
owner.last = Date.now();
|
|
645
|
+
}
|
|
527
646
|
const esc = escalations.get(callId);
|
|
528
647
|
if (esc && esc.state === "running") {
|
|
529
648
|
esc.state = "done";
|
|
@@ -542,18 +661,32 @@ export async function startMcpBridge(
|
|
|
542
661
|
return true;
|
|
543
662
|
};
|
|
544
663
|
|
|
664
|
+
const discardParks = (callIds: Iterable<string>): void => {
|
|
665
|
+
for (const callId of callIds) {
|
|
666
|
+
const resolve = parks.get(callId);
|
|
667
|
+
parks.delete(callId);
|
|
668
|
+
const owner = parkOwners.get(callId);
|
|
669
|
+
if (owner) {
|
|
670
|
+
parkOwners.delete(callId);
|
|
671
|
+
owner.parks = Math.max(0, owner.parks - 1);
|
|
672
|
+
}
|
|
673
|
+
// Settle any outstanding HTTP request, then drop its poll payload too.
|
|
674
|
+
resolve?.({ text: "Pi provider operation completed.", isError: true });
|
|
675
|
+
escalations.delete(callId);
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
|
|
545
679
|
const stop = async (): Promise<void> => {
|
|
680
|
+
stopping = true;
|
|
546
681
|
failAllParks("Pi tool bridge stopped.");
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
await mcp.close();
|
|
554
|
-
} catch {
|
|
555
|
-
// Best effort.
|
|
682
|
+
clearInterval(sweep);
|
|
683
|
+
await Promise.all([...sessions].map(([id, session]) => closeSession(id, session)));
|
|
684
|
+
for (const session of pendingSessions) {
|
|
685
|
+
session.closing = true;
|
|
686
|
+
await session.transport.close().catch(() => undefined);
|
|
687
|
+
await session.mcp.close().catch(() => undefined);
|
|
556
688
|
}
|
|
689
|
+
pendingSessions.clear();
|
|
557
690
|
// close() alone waits out keep-alive/SSE sockets; destroy them and
|
|
558
691
|
// bound the wait so session shutdown can never hang on a lingerer.
|
|
559
692
|
try {
|
|
@@ -584,7 +717,6 @@ export async function startMcpBridge(
|
|
|
584
717
|
const address = http.address();
|
|
585
718
|
const port = typeof address === "object" && address ? address.port : 0;
|
|
586
719
|
if (!port) throw new Error("bridge bound no port");
|
|
587
|
-
await mcp.connect(transport);
|
|
588
720
|
await writeBridgeConfig(configDir, port, token);
|
|
589
721
|
bound.port = port;
|
|
590
722
|
return {
|
|
@@ -595,6 +727,7 @@ export async function startMcpBridge(
|
|
|
595
727
|
configDir,
|
|
596
728
|
url: `http://127.0.0.1:${port}${BRIDGE_MCP_PATH}`,
|
|
597
729
|
toolCount: bridgedTools.length + BUILTIN_TOOL_COUNT,
|
|
730
|
+
pendingResults: escalations.size,
|
|
598
731
|
}),
|
|
599
732
|
setTools: (tools: BridgeToolDef[]) => {
|
|
600
733
|
bridgedTools = tools;
|
|
@@ -604,6 +737,7 @@ export async function startMcpBridge(
|
|
|
604
737
|
},
|
|
605
738
|
completePark,
|
|
606
739
|
failAllParks,
|
|
740
|
+
discardParks,
|
|
607
741
|
};
|
|
608
742
|
} catch {
|
|
609
743
|
try {
|
|
@@ -654,9 +788,10 @@ async function readBody(req: IncomingMessage): Promise<{ body?: unknown; tooLarg
|
|
|
654
788
|
async function handleBridgeRequest(
|
|
655
789
|
req: IncomingMessage,
|
|
656
790
|
res: ServerResponse,
|
|
657
|
-
transport: StreamableHTTPServerTransport,
|
|
658
791
|
token: string,
|
|
659
792
|
port: number,
|
|
793
|
+
resolveTransport: (req: IncomingMessage, body?: unknown) => Promise<{ transport: StreamableHTTPServerTransport; session: { transport: StreamableHTTPServerTransport; mcp: McpServer; last: number; active: number; parks: number; closing: boolean } } | undefined>,
|
|
794
|
+
registerSession: (session: { transport: StreamableHTTPServerTransport; mcp: McpServer; last: number; active: number; parks: number; closing: boolean }) => void,
|
|
660
795
|
): Promise<void> {
|
|
661
796
|
try {
|
|
662
797
|
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
@@ -702,10 +837,40 @@ async function handleBridgeRequest(
|
|
|
702
837
|
res.writeHead(400).end();
|
|
703
838
|
return;
|
|
704
839
|
}
|
|
705
|
-
|
|
840
|
+
// The SDK receives a pre-parsed body, bypassing its own body reader.
|
|
841
|
+
// Bound structure before even resolving/allocating an MCP session.
|
|
842
|
+
if (!isBridgeInputWithinBudget(body)) {
|
|
843
|
+
res.writeHead(413, { "Content-Type": "application/json" }).end(JSON.stringify({
|
|
844
|
+
error: "Request structure exceeds bridge limits",
|
|
845
|
+
}));
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
const resolved = await resolveTransport(req, body);
|
|
849
|
+
if (!resolved) {
|
|
850
|
+
res.writeHead(req.headers["mcp-session-id"] ? 404 : 503).end();
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
try {
|
|
854
|
+
await resolved.transport.handleRequest(req, res, body);
|
|
855
|
+
} finally {
|
|
856
|
+
resolved.session.active -= 1;
|
|
857
|
+
resolved.session.last = Date.now();
|
|
858
|
+
const id = resolved.transport.sessionId;
|
|
859
|
+
registerSession(resolved.session as { transport: StreamableHTTPServerTransport; mcp: McpServer; last: number; active: number; parks: number; closing: boolean });
|
|
860
|
+
}
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
const resolved = await resolveTransport(req);
|
|
864
|
+
if (!resolved) {
|
|
865
|
+
res.writeHead(req.headers["mcp-session-id"] ? 404 : 400).end();
|
|
706
866
|
return;
|
|
707
867
|
}
|
|
708
|
-
|
|
868
|
+
try {
|
|
869
|
+
await resolved.transport.handleRequest(req, res);
|
|
870
|
+
} finally {
|
|
871
|
+
resolved.session.active -= 1;
|
|
872
|
+
resolved.session.last = Date.now();
|
|
873
|
+
}
|
|
709
874
|
} catch {
|
|
710
875
|
if (!res.headersSent) {
|
|
711
876
|
try {
|
|
@@ -36,12 +36,19 @@ const EFFORT_ORDER: readonly AgyProviderEffort[] = ["low", "medium", "high"];
|
|
|
36
36
|
const TIER_RE = /^(.+)-(high|medium|low)$/;
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* model, and a leading-dash token cannot reach agy's flag parser as `--model`.
|
|
39
|
+
* Preserve the established hyphenated first-column format. A leading-dash
|
|
40
|
+
* token must never reach agy's flag parser as `--model`.
|
|
42
41
|
*/
|
|
43
42
|
const MODEL_LINE_RE = /^[A-Za-z0-9][A-Za-z0-9._]*-[A-Za-z0-9._-]*$/;
|
|
44
43
|
|
|
44
|
+
// Nonhyphen ids also occur (e.g. `argon`). Be conservative about ambiguous
|
|
45
|
+
// human text: require lowercase slug syntax with no trailing dot. Common
|
|
46
|
+
// header and diagnostic first tokens are not models. Other lowercase words
|
|
47
|
+
// (bare or followed by whitespace) are inherently indistinguishable from ids
|
|
48
|
+
// in this unstructured CLI format.
|
|
49
|
+
const NONHYPHEN_MODEL_ID_RE = /^[a-z0-9](?:[a-z0-9._]*[a-z0-9_])?$/;
|
|
50
|
+
const NON_MODEL_TOKEN_RE = /^(?:id|model|models|name|label|status|available|fetching|loading|error|warning|info|auth|authentication|unauthorized|forbidden|login|success)$/;
|
|
51
|
+
|
|
45
52
|
/**
|
|
46
53
|
* Families verified to accept base-slug + `--effort`. Only these collapse to
|
|
47
54
|
* a base entry with a thinking toggle; every other family keeps agy's exact
|
|
@@ -141,7 +148,10 @@ export function parseProviderCatalog(raw: string): AgyProviderModelEntry[] {
|
|
|
141
148
|
const groups = new Map<string, { lines: string[]; tiers: Set<AgyProviderEffort> }>();
|
|
142
149
|
for (const line of raw.split("\n")) {
|
|
143
150
|
const slug = line.trim().split(/\s+/)[0] ?? "";
|
|
144
|
-
if (
|
|
151
|
+
if (
|
|
152
|
+
!MODEL_LINE_RE.test(slug) &&
|
|
153
|
+
(!NONHYPHEN_MODEL_ID_RE.test(slug) || NON_MODEL_TOKEN_RE.test(slug))
|
|
154
|
+
) continue;
|
|
145
155
|
const match = TIER_RE.exec(slug);
|
|
146
156
|
const base = match ? match[1] : slug;
|
|
147
157
|
const tier = match ? (match[2] as AgyProviderEffort) : null;
|