Skip to content

Commit 743ffa8

Browse files
committed
fix: clear pending chat ID when open flow is abandoned
If commands.open() returns without actually opening a window (e.g. the user cancels a workspace, agent, or folder prompt), clear the pending chat ID from memento so it does not leak into a future, unrelated reload. - commands.open() and openWorkspace() now return boolean indicating whether a window was actually opened. - handleOpen() clears the pending chat ID when open() returns false. - Add clearPendingChatId() to MementoManager.
1 parent 3448126 commit 743ffa8

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/commands.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ export class Commands {
430430
431431
* Throw if not logged into a deployment.
432432
*/
433-
public async openFromSidebar(item: OpenableTreeItem) {
433+
public async openFromSidebar(item: OpenableTreeItem): Promise<void> {
434434
if (item) {
435435
const baseUrl = this.extensionClient.getAxiosInstance().defaults.baseURL;
436436
if (!baseUrl) {
@@ -464,7 +464,7 @@ export class Commands {
464464
} else {
465465
// If there is no tree item, then the user manually ran this command.
466466
// Default to the regular open instead.
467-
return this.open();
467+
await this.open();
468468
}
469469
}
470470

@@ -529,7 +529,7 @@ export class Commands {
529529
agentName?: string,
530530
folderPath?: string,
531531
openRecent?: boolean,
532-
): Promise<void> {
532+
): Promise<boolean> {
533533
const baseUrl = this.extensionClient.getAxiosInstance().defaults.baseURL;
534534
if (!baseUrl) {
535535
throw new Error("You are not logged in");
@@ -545,18 +545,24 @@ export class Commands {
545545
workspace = await this.pickWorkspace();
546546
if (!workspace) {
547547
// User declined to pick a workspace.
548-
return;
548+
return false;
549549
}
550550
}
551551

552552
const agents = await this.extractAgentsWithFallback(workspace);
553553
const agent = await maybeAskAgent(agents, agentName);
554554
if (!agent) {
555555
// User declined to pick an agent.
556-
return;
556+
return false;
557557
}
558558

559-
await this.openWorkspace(baseUrl, workspace, agent, folderPath, openRecent);
559+
return this.openWorkspace(
560+
baseUrl,
561+
workspace,
562+
agent,
563+
folderPath,
564+
openRecent,
565+
);
560566
}
561567

562568
/**
@@ -745,7 +751,7 @@ export class Commands {
745751
agent: WorkspaceAgent,
746752
folderPath: string | undefined,
747753
openRecent = false,
748-
) {
754+
): Promise<boolean> {
749755
const remoteAuthority = toRemoteAuthority(
750756
baseUrl,
751757
workspace.owner_name,
@@ -788,7 +794,7 @@ export class Commands {
788794
});
789795
if (!folderPath) {
790796
// User aborted.
791-
return;
797+
return false;
792798
}
793799
}
794800
}
@@ -806,14 +812,15 @@ export class Commands {
806812
// Open this in a new window!
807813
newWindow,
808814
);
809-
return;
815+
return true;
810816
}
811817

812818
// This opens the workspace without an active folder opened.
813819
await vscode.commands.executeCommand("vscode.newWindow", {
814820
remoteAuthority: remoteAuthority,
815821
reuseWindow: !newWindow,
816822
});
823+
return true;
817824
}
818825
}
819826

src/core/mementoManager.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ export class MementoManager {
8181
}
8282
return chatId;
8383
}
84+
85+
/**
86+
* Clear the pending chat ID without reading it. Used when
87+
* the open flow is abandoned (e.g. user cancels a prompt)
88+
* so the stale ID does not leak into a future reload.
89+
*/
90+
public async clearPendingChatId(): Promise<void> {
91+
await this.memento.update("pendingChatId", undefined);
92+
}
8493
}

src/uri/uriHandler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,27 @@ async function handleOpen(ctx: UriRouteContext): Promise<void> {
9393
// a remote-authority reload that wipes in-memory state.
9494
// The extension picks this up after the reload in activate().
9595
const chatId = params.get("chatId");
96+
const mementoManager = serviceContainer.getMementoManager();
9697
if (chatId) {
97-
const mementoManager = serviceContainer.getMementoManager();
9898
await mementoManager.setPendingChatId(chatId);
9999
}
100100

101101
await setupDeployment(params, serviceContainer, deploymentManager);
102102

103-
await commands.open(
103+
const opened = await commands.open(
104104
owner,
105105
workspace,
106106
agent ?? undefined,
107107
folder ?? undefined,
108108
openRecent,
109109
);
110+
111+
// If commands.open() returned without opening a window (e.g. the
112+
// user cancelled a prompt), clear the pending chat ID so it does
113+
// not leak into a future, unrelated reload.
114+
if (!opened && chatId) {
115+
await mementoManager.clearPendingChatId();
116+
}
110117
}
111118

112119
async function handleOpenDevContainer(ctx: UriRouteContext): Promise<void> {

0 commit comments

Comments
 (0)