Skip to content

Commit 72de407

Browse files
fix(pi): suppress grants across overlapping lifecycle requests
1 parent d78976e commit 72de407

2 files changed

Lines changed: 68 additions & 21 deletions

File tree

‎packages/provider-pi/src/server/adapter.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,6 +3266,50 @@ describe("PiAdapterV2", () => {
32663266
}).pipe(Effect.scoped, Effect.provide(layerTest)),
32673267
);
32683268

3269+
it.effect("suppresses session grants until all overlapping lifecycle requests finish", () =>
3270+
Effect.gen(function* () {
3271+
const { fake, runtime, providerThread, takeApproval, grant, askAgain } = yield* setup;
3272+
fake.deferNextLifecycle("switch_session");
3273+
const first = yield* runtime.resumeThread({ providerThread }).pipe(Effect.forkScoped);
3274+
const firstRequest = yield* fake.takeRequest("switch_session");
3275+
fake.deferNextLifecycle("switch_session");
3276+
const second = yield* runtime.resumeThread({ providerThread }).pipe(Effect.forkScoped);
3277+
const secondRequest = yield* fake.takeRequest("switch_session");
3278+
yield* fake.emit({
3279+
type: "response",
3280+
id: firstRequest["id"],
3281+
command: "switch_session",
3282+
success: true,
3283+
data: { cancelled: false },
3284+
});
3285+
yield* Fiber.join(first);
3286+
yield* grant;
3287+
yield* fake.emit({
3288+
type: "response",
3289+
id: secondRequest["id"],
3290+
command: "switch_session",
3291+
success: true,
3292+
data: { cancelled: false },
3293+
});
3294+
yield* Fiber.join(second);
3295+
yield* fake.emit(prompt("after-lifecycle"));
3296+
const pending = yield* Effect.race(
3297+
takeApproval,
3298+
fake.takeRequest("extension_ui_response").pipe(Effect.as(undefined)),
3299+
);
3300+
assert.isDefined(pending);
3301+
if (pending === undefined) throw new Error("Expected approval after lifecycle requests");
3302+
assert.equal(pending.status, "pending");
3303+
yield* runtime.respondToRuntimeRequest({
3304+
requestId: pending.id,
3305+
decision: "acceptForSession",
3306+
});
3307+
assert.equal((yield* fake.takeRequest("extension_ui_response"))["confirmed"], true);
3308+
assert.equal((yield* takeApproval).status, "resolved");
3309+
assert.equal(yield* askAgain, "automatic");
3310+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
3311+
);
3312+
32693313
it.effect(
32703314
"does not resolve a cancelled dialog or restore its grant after an in-flight answer",
32713315
() =>

‎packages/provider-pi/src/server/adapter.ts‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
516516
>();
517517
const pendingPrompts = new Map<string, PendingPiPrompt>();
518518
const sessionApprovals = new Set<string>();
519-
let lifecycleInFlight = false;
519+
let lifecycleRequestsInFlight = 0;
520520
// Answering a dialog and terminalizing a turn both publish lifecycle
521521
// events. Pi can settle immediately after `extension_ui_response`, so
522522
// serialize the two paths to stop `turn.terminal` from overtaking the
@@ -641,26 +641,29 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
641641
};
642642

643643
const lifecycleRequest = (record: PiRpcRecord) =>
644-
Effect.suspend(() => {
645-
sessionApprovals.clear();
646-
lifecycleInFlight = true;
647-
return request(record, PI_SESSION_TIMEOUT_MS);
648-
}).pipe(
649-
// A local timeout does not cancel Pi's lifecycle hook. Retire the
650-
// process before fallback can race its eventual switch/new-session.
651-
Effect.tapError((error) =>
652-
Effect.logWarning("Pi session lifecycle request failed", {
653-
providerSessionId: input.providerSessionId,
654-
operation: record["type"],
655-
errorTag: error._tag,
656-
}),
657-
),
658-
Effect.catchTags({
659-
PiRpcTimeoutError: (error) =>
660-
connection.terminate.pipe(Effect.andThen(Effect.fail(error))),
644+
Effect.acquireUseRelease(
645+
Effect.sync(() => {
646+
sessionApprovals.clear();
647+
lifecycleRequestsInFlight++;
661648
}),
662-
Effect.onInterrupt(() => connection.terminate),
663-
Effect.ensuring(Effect.sync(() => (lifecycleInFlight = false))),
649+
() =>
650+
request(record, PI_SESSION_TIMEOUT_MS).pipe(
651+
// A local timeout does not cancel Pi's lifecycle hook. Retire the
652+
// process before fallback can race its eventual switch/new-session.
653+
Effect.tapError((error) =>
654+
Effect.logWarning("Pi session lifecycle request failed", {
655+
providerSessionId: input.providerSessionId,
656+
operation: record["type"],
657+
errorTag: error._tag,
658+
}),
659+
),
660+
Effect.catchTags({
661+
PiRpcTimeoutError: (error) =>
662+
connection.terminate.pipe(Effect.andThen(Effect.fail(error))),
663+
}),
664+
Effect.onInterrupt(() => connection.terminate),
665+
),
666+
() => Effect.sync(() => lifecycleRequestsInFlight--),
664667
);
665668

666669
const tokenUsageFromStats = (
@@ -2763,7 +2766,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
27632766
if (
27642767
pending.method === "confirm" &&
27652768
requestInput.decision === "acceptForSession" &&
2766-
!lifecycleInFlight
2769+
lifecycleRequestsInFlight === 0
27672770
) {
27682771
sessionApprovals.add(pending.approvalKey);
27692772
}

0 commit comments

Comments
 (0)