Skip to content

Commit 2f28751

Browse files
committed
fix(vscode): keep a pending permission mode when applying it fails
1 parent c3dbed6 commit 2f28751

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

apps/vscode/src/runtime/pythinker-runtime.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,13 @@ export class PythinkerRuntime {
239239
): Promise<void> {
240240
const pending = this.pendingPermissionByView.get(webviewId);
241241
if (pending === undefined) return;
242-
this.pendingPermissionByView.delete(webviewId);
243242
await runtime.setPermissionMode(pending);
243+
// Dropped only once it landed, and only if it is still the request in hand:
244+
// a failed apply keeps the command for the next attempt, and a command that
245+
// arrived during the await outranks the one just applied.
246+
if (this.pendingPermissionByView.get(webviewId) === pending) {
247+
this.pendingPermissionByView.delete(webviewId);
248+
}
244249
}
245250

246251
private wrapSession(session: Session, permissionMode: PermissionMode): SessionRuntime {

apps/vscode/test/pythinker-runtime.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,22 @@ describe("Pythinker runtime (owns shared SDK sessions for Webviews)", () => {
493493
expect(runtime.pendingPermissionTarget("view-1", "manual").permissionMode).toBe("manual");
494494
});
495495

496+
it("keeps a pending permission mode when applying it to the session fails", async () => {
497+
const { runtime, sdk } = createRuntime();
498+
const boundary = sdk.addSession("saved-1", "/workspace");
499+
(boundary.session as { setPermission: (mode: PermissionMode) => Promise<void> }).setPermission =
500+
async () => {
501+
throw new Error("engine offline");
502+
};
503+
await runtime.pendingPermissionTarget("view-1", "manual").setPermissionMode("yolo");
504+
505+
await expect(
506+
runtime.openSession(openOptions({ webviewId: "view-1", sessionId: "saved-1" })),
507+
).rejects.toThrow("engine offline");
508+
509+
expect(runtime.pendingPermissionTarget("view-1", "manual").permissionMode).toBe("yolo");
510+
});
511+
496512
it("persists a mode change so the next attach restores it", async () => {
497513
const { runtime, sdk } = createRuntime();
498514
const session = sdk.addSession("saved-1", "/workspace", { permission: "manual" });

0 commit comments

Comments
 (0)