pi-agy-provider 1.1.2 → 1.1.4

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/src/runtime.ts CHANGED
@@ -42,7 +42,12 @@ import {
42
42
  type AgyUsage,
43
43
  } from "../lib/reducer.ts";
44
44
  import { agyToolStepKey, trackActiveToolStep } from "../lib/tool-steps.ts";
45
- import { piSystemInstructionsPrompt, stallContinuationPrompt } from "../lib/prompt.ts";
45
+ import {
46
+ PI_BRIDGE_ROUTING_NOTES,
47
+ piSystemInstructionsPrompt,
48
+ stallContinuationPrompt,
49
+ stripPiToolInventory,
50
+ } from "../lib/prompt.ts";
46
51
  import { AgyTurnController } from "../lib/turn.ts";
47
52
 
48
53
  interface AntigravityRuntimeSettings {
@@ -112,6 +117,7 @@ function waitForRetryBackoff(ms: number, signal: AbortSignal): Promise<boolean>
112
117
  signal.removeEventListener("abort", onAbort);
113
118
  resolve(true);
114
119
  }, ms);
120
+ timer.unref?.();
115
121
  signal.addEventListener("abort", onAbort, { once: true });
116
122
  });
117
123
  }
@@ -171,6 +177,7 @@ export interface AntigravityStateSnapshot {
171
177
  turns: number;
172
178
  conversationUsage: AgyUsage;
173
179
  executor: AgyExecutorSnapshot;
180
+ hasLiveTurn?: boolean;
174
181
  }
175
182
 
176
183
  export interface AntigravityRuntimeShape {
@@ -215,6 +222,8 @@ export interface AntigravityRuntimeShape {
215
222
  readonly timeoutMs?: number;
216
223
  /** Latest observed branch context footprint (`input + cacheRead`), used to scale prefill stall budget. */
217
224
  readonly contextTokens?: number;
225
+ /** Invoked once when the logical agy turn ends (complete, failed, or invalidated). */
226
+ readonly onTurnEnded?: () => void;
218
227
  }) => Effect.Effect<AgyTurnController, AntigravityRuntimeClosedError>;
219
228
  /** Clear the active controller once a provider turn reached a terminal state. */
220
229
  readonly finishTurn: Effect.Effect<void>;
@@ -297,6 +306,7 @@ const makeRuntime = (
297
306
  let turns = 0;
298
307
  let closed = false;
299
308
  let active: AgyTurnController | undefined;
309
+ let activeOnTurnEnded: (() => void) | undefined;
300
310
  // Final output may reach Pi before the executor finishes process cleanup.
301
311
  // New turns must wait for its state/usage commit, not reattach or abort it.
302
312
  let activeTurnCompletion: Promise<void> | undefined;
@@ -312,6 +322,9 @@ const makeRuntime = (
312
322
 
313
323
  const invalidateActiveTurn = () => {
314
324
  generation += 1;
325
+ const endHook = activeOnTurnEnded;
326
+ activeOnTurnEnded = undefined;
327
+ endHook?.();
315
328
  activeTurnAbort?.abort();
316
329
  activeTurnAbort = undefined;
317
330
  activeTurnCompletion = undefined;
@@ -432,6 +445,14 @@ const makeRuntime = (
432
445
  model = request.modelId;
433
446
  const controller = new AgyTurnController(request.prompt, conversationUsage);
434
447
  active = controller;
448
+ let turnEndedFired = false;
449
+ const fireTurnEnded = () => {
450
+ if (turnEndedFired) return;
451
+ turnEndedFired = true;
452
+ if (activeOnTurnEnded === fireTurnEnded) activeOnTurnEnded = undefined;
453
+ request.onTurnEnded?.();
454
+ };
455
+ activeOnTurnEnded = fireTurnEnded;
435
456
  // Compose pi's request signal with our own so close() can kill
436
457
  // the agy child even when pi's signal never fires.
437
458
  const turnAbort = new AbortController();
@@ -454,7 +475,13 @@ const makeRuntime = (
454
475
  !conversationId && restoreHistoryOnNextConversation
455
476
  ? request.historyBootstrap
456
477
  : undefined;
457
- const systemPrompt = request.systemPrompt ?? "";
478
+ const strippedSystemPrompt = stripPiToolInventory(request.systemPrompt ?? "");
479
+ const systemPrompt =
480
+ Boolean(executor.supportsNativeSystemPrompt) &&
481
+ !request.agent?.startsWith("pi-tools") &&
482
+ /<rules>[\s\S]*?<\/rules>/.test(strippedSystemPrompt)
483
+ ? `${PI_BRIDGE_ROUTING_NOTES}\n\n${strippedSystemPrompt}`
484
+ : strippedSystemPrompt;
458
485
  const systemPromptRelay = (
459
486
  !conversationId
460
487
  ? Boolean(systemPrompt)
@@ -793,6 +820,7 @@ const makeRuntime = (
793
820
  // loser; a late rejection does not require a separate swallow catch.
794
821
  activeTurnCompletion = Promise.race([runTurnWithStallRetries(), cancelled])
795
822
  .then((outcome: AgyTurnOutcome) => {
823
+ fireTurnEnded();
796
824
  if (turnGeneration !== generation) {
797
825
  controller.close(
798
826
  "this turn was replaced by a newer request or a driver recycle",
@@ -811,12 +839,14 @@ const makeRuntime = (
811
839
  controller.close();
812
840
  })
813
841
  .catch((cause: unknown) => {
842
+ fireTurnEnded();
814
843
  // A superseded turn still reports its real failure: the
815
844
  // provider reading this controller must never be left with a
816
845
  // bare "ended without a result event".
817
846
  controller.fail(cause instanceof Error ? cause : new Error(String(cause)));
818
847
  })
819
848
  .finally(() => {
849
+ fireTurnEnded();
820
850
  turnDeadline.clear();
821
851
  turnAbort.signal.removeEventListener("abort", onTurnAbort);
822
852
  if (requestAbortHandler) {
@@ -867,6 +897,7 @@ const makeRuntime = (
867
897
  turns,
868
898
  conversationUsage: { ...conversationUsage },
869
899
  executor: executor.snapshot(),
900
+ hasLiveTurn: Boolean(active && !active.isClosed()),
870
901
  })),
871
902
  ),
872
903
  ),