fix: agent manager terminal follows context when switching to local mode (#6360)

* fix: agent manager terminal follows context when switching to local mode

When switching from a worktree to local mode with a pending tab,
currentSessionID() was undefined causing terminal triggers (Cmd+/ and
button click) to silently do nothing. Added a showLocalTerminal fallback
that opens a terminal at workspace root, and showExistingLocalTerminal
to auto-reveal the local terminal when switching contexts.

* fix: show warning when local terminal has no workspace folder

* refactor: centralize local terminal key in SessionTerminalManager
This commit is contained in:
Marius
2026-02-26 14:54:12 +01:00
committed by GitHub
parent 644579304a
commit dab4ae78fb
5 changed files with 52 additions and 0 deletions
@@ -176,6 +176,14 @@ export class AgentManagerProvider implements vscode.Disposable {
this.terminalManager.showTerminal(msg.sessionId, this.state)
return null
}
if (type === "agentManager.showLocalTerminal") {
this.terminalManager.showLocalTerminal()
return null
}
if (type === "agentManager.showExistingLocalTerminal") {
this.terminalManager.showExistingLocal()
return null
}
if (type === "agentManager.requestRepoInfo") {
void this.sendRepoInfo()
return null
@@ -53,6 +53,32 @@ export class SessionTerminalManager {
this.showOrCreate(sessionId, cwd, name)
}
/**
* Show (or create) a terminal for the local workspace (no session required).
* Used when the user triggers a terminal in local mode without an active session.
*/
private static readonly LOCAL_KEY = "__local__"
showLocalTerminal(): void {
if (this.showExisting(SessionTerminalManager.LOCAL_KEY, false)) return
const cwd = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath
if (!cwd) {
this.log("showLocalTerminal: no workspace folder open")
vscode.window.showWarningMessage("Open a folder to use the local terminal")
return
}
this.showOrCreate(SessionTerminalManager.LOCAL_KEY, cwd, "Agent: local")
}
/**
* Show the existing local terminal if one was previously created (used on context switch).
*/
showExistingLocal(): boolean {
return this.showExisting(SessionTerminalManager.LOCAL_KEY)
}
/**
* Show the terminal for a session if it already exists (used when switching sessions).
* Returns true if the terminal was shown, false if no terminal exists for the session.
@@ -158,6 +158,8 @@ describe("Agent Manager Provider — onMessage routing", () => {
"agentManager.closeSession",
"agentManager.configureSetupScript",
"agentManager.showTerminal",
"agentManager.showLocalTerminal",
"agentManager.showExistingLocalTerminal",
"agentManager.requestRepoInfo",
"agentManager.requestState",
"agentManager.setTabOrder",
@@ -579,9 +579,11 @@ const AgentManagerContent: Component = () => {
} else if (fallback && isPending(fallback.id)) {
setActivePendingId(fallback.id)
session.clearCurrentSession()
vscode.postMessage({ type: "agentManager.showExistingLocalTerminal" })
} else {
setActivePendingId(undefined)
session.clearCurrentSession()
vscode.postMessage({ type: "agentManager.showExistingLocalTerminal" })
}
}
@@ -612,6 +614,7 @@ const AgentManagerContent: Component = () => {
else if (msg.action === "showTerminal") {
const id = session.currentSessionID()
if (id) vscode.postMessage({ type: "agentManager.showTerminal", sessionId: id })
else if (selection() === LOCAL) vscode.postMessage({ type: "agentManager.showLocalTerminal" })
} else if (msg.action === "toggleDiff") {
setDiffOpen((prev) => !prev)
} else if (msg.action === "newTab") handleNewTabForCurrentSelection()
@@ -1713,6 +1716,7 @@ const AgentManagerContent: Component = () => {
onClick={() => {
const id = session.currentSessionID()
if (id) vscode.postMessage({ type: "agentManager.showTerminal", sessionId: id })
else if (selection() === LOCAL) vscode.postMessage({ type: "agentManager.showLocalTerminal" })
}}
/>
</TooltipKeybind>
@@ -1112,6 +1112,16 @@ export interface ShowTerminalRequest {
sessionId: string
}
// Show terminal for the local workspace (when no session is active)
export interface ShowLocalTerminalRequest {
type: "agentManager.showLocalTerminal"
}
// Show existing local terminal when switching to local context (no-op if none exists)
export interface ShowExistingLocalTerminalRequest {
type: "agentManager.showExistingLocalTerminal"
}
/**
* Maximum number of parallel worktree versions for multi-version mode.
* Keep in sync with MAX_MULTI_VERSIONS in src/agent-manager/constants.ts.
@@ -1264,6 +1274,8 @@ export type WebviewMessage =
| RequestStateMessage
| ConfigureSetupScriptRequest
| ShowTerminalRequest
| ShowLocalTerminalRequest
| ShowExistingLocalTerminalRequest
| CreateMultiVersionRequest
| SetTabOrderRequest
| SetSessionsCollapsedRequest