From 19cc8bc9f8206b771f05845e14dac995310270c5 Mon Sep 17 00:00:00 2001 From: Evan <58194240+celestial-vault@users.noreply.github.com> Date: Wed, 30 Apr 2025 22:40:58 -0500 Subject: [PATCH] Add terminal connection timeout (#3218) * add terminal connection timeout * changeset --------- Co-authored-by: Elephant Lumps --- .changeset/nervous-dolphins-greet.md | 5 ++ src/core/controller/index.ts | 28 ++++++- src/core/storage/state-keys.ts | 1 + src/core/storage/state.ts | 3 + src/core/task/index.ts | 2 + src/integrations/terminal/TerminalManager.ts | 36 +++++++-- src/shared/ExtensionMessage.ts | 1 + src/shared/WebviewMessage.ts | 2 + .../src/components/settings/SettingsView.tsx | 4 + .../settings/TerminalSettingsSection.tsx | 76 +++++++++++++++++++ .../src/context/ExtensionStateContext.tsx | 7 ++ 11 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 .changeset/nervous-dolphins-greet.md create mode 100644 webview-ui/src/components/settings/TerminalSettingsSection.tsx diff --git a/.changeset/nervous-dolphins-greet.md b/.changeset/nervous-dolphins-greet.md new file mode 100644 index 00000000..95de9eed --- /dev/null +++ b/.changeset/nervous-dolphins-greet.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +Add a timeout setting for the terminal connection, allowing users to adjust this if they are having timeout issues diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index 066ce245..83183cbb 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -136,8 +136,14 @@ export class Controller { async initTask(task?: string, images?: string[], historyItem?: HistoryItem) { await this.clearTask() // ensures that an existing task doesn't exist before starting a new one, although this shouldn't be possible since user must clear task before starting a new one - const { apiConfiguration, customInstructions, autoApprovalSettings, browserSettings, chatSettings } = - await getAllExtensionState(this.context) + const { + apiConfiguration, + customInstructions, + autoApprovalSettings, + browserSettings, + chatSettings, + shellIntegrationTimeout, + } = await getAllExtensionState(this.context) if (autoApprovalSettings) { const updatedAutoApprovalSettings = { @@ -159,6 +165,7 @@ export class Controller { autoApprovalSettings, browserSettings, chatSettings, + shellIntegrationTimeout, customInstructions, task, images, @@ -784,6 +791,21 @@ export class Controller { } break } + case "updateTerminalConnectionTimeout": { + if (message.shellIntegrationTimeout !== undefined) { + const timeout = message.shellIntegrationTimeout + + if (typeof timeout === "number" && !isNaN(timeout) && timeout > 0) { + await updateGlobalState(this.context, "shellIntegrationTimeout", timeout) + await this.postStateToWebview() + } else { + console.warn( + `Invalid shell integration timeout value received: ${timeout}. ` + `Expected a positive number.`, + ) + } + } + break + } // Add more switch case statements here as more webview message commands // are created within the webview context (i.e. inside media/main.js) } @@ -1769,6 +1791,7 @@ Here is the project's README to help you get started:\n\n${mcpDetails.readmeCont telemetrySetting, planActSeparateModelsSetting, globalClineRulesToggles, + shellIntegrationTimeout, } = await getAllExtensionState(this.context) const localClineRulesToggles = @@ -1798,6 +1821,7 @@ Here is the project's README to help you get started:\n\n${mcpDetails.readmeCont vscMachineId: vscode.env.machineId, globalClineRulesToggles: globalClineRulesToggles || {}, localClineRulesToggles: localClineRulesToggles || {}, + shellIntegrationTimeout, } } diff --git a/src/core/storage/state-keys.ts b/src/core/storage/state-keys.ts index 4bfa6ad8..b0be5f8f 100644 --- a/src/core/storage/state-keys.ts +++ b/src/core/storage/state-keys.ts @@ -76,5 +76,6 @@ export type GlobalStateKey = | "planActSeparateModelsSetting" | "favoritedModelIds" | "requestTimeoutMs" + | "shellIntegrationTimeout" export type LocalStateKey = "localClineRulesToggles" diff --git a/src/core/storage/state.ts b/src/core/storage/state.ts index 0603aacc..857ba780 100644 --- a/src/core/storage/state.ts +++ b/src/core/storage/state.ts @@ -126,6 +126,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { favoritedModelIds, globalClineRulesToggles, requestTimeoutMs, + shellIntegrationTimeout, ] = await Promise.all([ getGlobalState(context, "apiProvider") as Promise, getGlobalState(context, "apiModelId") as Promise, @@ -200,6 +201,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { getGlobalState(context, "favoritedModelIds") as Promise, getGlobalState(context, "globalClineRulesToggles") as Promise, getGlobalState(context, "requestTimeoutMs") as Promise, + getGlobalState(context, "shellIntegrationTimeout") as Promise, ]) let apiProvider: ApiProvider @@ -319,6 +321,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { mcpMarketplaceEnabled, telemetrySetting: telemetrySetting || "unset", planActSeparateModelsSetting, + shellIntegrationTimeout: shellIntegrationTimeout || 4000, } } diff --git a/src/core/task/index.ts b/src/core/task/index.ts index f7c99c83..7559ea7a 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -173,6 +173,7 @@ export class Task { autoApprovalSettings: AutoApprovalSettings, browserSettings: BrowserSettings, chatSettings: ChatSettings, + shellIntegrationTimeout: number, customInstructions?: string, task?: string, images?: string[], @@ -191,6 +192,7 @@ export class Task { console.error("Failed to initialize ClineIgnoreController:", error) }) this.terminalManager = new TerminalManager() + this.terminalManager.setShellIntegrationTimeout(shellIntegrationTimeout) this.urlContentFetcher = new UrlContentFetcher(context) this.browserSession = new BrowserSession(context, browserSettings) this.contextManager = new ContextManager() diff --git a/src/integrations/terminal/TerminalManager.ts b/src/integrations/terminal/TerminalManager.ts index 30344ecb..cfc20b01 100644 --- a/src/integrations/terminal/TerminalManager.ts +++ b/src/integrations/terminal/TerminalManager.ts @@ -39,7 +39,7 @@ const terminalManager = new TerminalManager(context); const process = terminalManager.runCommand('npm install', '/path/to/project'); process.on('line', (line) => { - console.log(line); + console.log(line); }); // To wait for the process to complete naturally: @@ -93,6 +93,7 @@ export class TerminalManager { private terminalIds: Set = new Set() private processes: Map = new Map() private disposables: vscode.Disposable[] = [] + private shellIntegrationTimeout: number = 4000 constructor() { let disposable: vscode.Disposable | undefined @@ -144,13 +145,30 @@ export class TerminalManager { process.run(terminalInfo.terminal, command) } else { // docs recommend waiting 3s for shell integration to activate - pWaitFor(() => terminalInfo.terminal.shellIntegration !== undefined, { timeout: 4000 }).finally(() => { - const existingProcess = this.processes.get(terminalInfo.id) - if (existingProcess && existingProcess.waitForShellIntegration) { - existingProcess.waitForShellIntegration = false - existingProcess.run(terminalInfo.terminal, command) - } + console.log( + `[TerminalManager Test] Waiting for shell integration for terminal ${terminalInfo.id} with timeout ${this.shellIntegrationTimeout}ms`, + ) + pWaitFor(() => terminalInfo.terminal.shellIntegration !== undefined, { + timeout: this.shellIntegrationTimeout, }) + .then(() => { + console.log( + `[TerminalManager Test] Shell integration activated for terminal ${terminalInfo.id} within timeout.`, + ) + }) + .catch((err) => { + console.warn( + `[TerminalManager Test] Shell integration timed out or failed for terminal ${terminalInfo.id}: ${err.message}`, + ) + }) + .finally(() => { + console.log(`[TerminalManager Test] Proceeding with command execution for terminal ${terminalInfo.id}.`) + const existingProcess = this.processes.get(terminalInfo.id) + if (existingProcess && existingProcess.waitForShellIntegration) { + existingProcess.waitForShellIntegration = false + existingProcess.run(terminalInfo.terminal, command) + } + }) } return mergePromise(process, promise) @@ -219,4 +237,8 @@ export class TerminalManager { this.disposables.forEach((disposable) => disposable.dispose()) this.disposables = [] } + + setShellIntegrationTimeout(timeout: number): void { + this.shellIntegrationTimeout = timeout + } } diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index d39cf0b2..e5d60b4f 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -132,6 +132,7 @@ export interface ExtensionState { shouldShowAnnouncement: boolean taskHistory: HistoryItem[] telemetrySetting: TelemetrySetting + shellIntegrationTimeout: number uriScheme?: string userInfo?: { displayName: string | null diff --git a/src/shared/WebviewMessage.ts b/src/shared/WebviewMessage.ts index a3ad3a9a..91fb3c6f 100644 --- a/src/shared/WebviewMessage.ts +++ b/src/shared/WebviewMessage.ts @@ -71,6 +71,7 @@ export interface WebviewMessage { | "toggleClineRule" | "deleteClineRule" | "copyToClipboard" + | "updateTerminalConnectionTimeout" // | "relaunchChromeDebugMode" text?: string @@ -121,6 +122,7 @@ export interface WebviewMessage { filename?: string offset?: number + shellIntegrationTimeout?: number } export type ClineAskResponse = "yesButtonClicked" | "noButtonClicked" | "messageResponse" diff --git a/webview-ui/src/components/settings/SettingsView.tsx b/webview-ui/src/components/settings/SettingsView.tsx index 38700135..bdc1e31a 100644 --- a/webview-ui/src/components/settings/SettingsView.tsx +++ b/webview-ui/src/components/settings/SettingsView.tsx @@ -9,6 +9,7 @@ import { TabButton } from "../mcp/configuration/McpConfigurationView" import { useEvent } from "react-use" import { ExtensionMessage } from "@shared/ExtensionMessage" import BrowserSettingsSection from "./BrowserSettingsSection" +import TerminalSettingsSection from "./TerminalSettingsSection" const { IS_DEV } = process.env @@ -240,6 +241,9 @@ const SettingsView = ({ onDone }: SettingsViewProps) => { {/* Browser Settings Section */} + {/* Terminal Settings Section */} + +
vscode.postMessage({ type: "openExtensionSettings" })} diff --git a/webview-ui/src/components/settings/TerminalSettingsSection.tsx b/webview-ui/src/components/settings/TerminalSettingsSection.tsx new file mode 100644 index 00000000..f55f83da --- /dev/null +++ b/webview-ui/src/components/settings/TerminalSettingsSection.tsx @@ -0,0 +1,76 @@ +import React, { useState } from "react" +import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react" +import { useExtensionState } from "@/context/ExtensionStateContext" +import { vscode } from "@/utils/vscode" + +export const TerminalSettingsSection: React.FC = () => { + const { shellIntegrationTimeout, setShellIntegrationTimeout } = useExtensionState() + const [inputValue, setInputValue] = useState((shellIntegrationTimeout / 1000).toString()) + const [inputError, setInputError] = useState(null) + + const handleTimeoutChange = (event: Event) => { + const target = event.target as HTMLInputElement + const value = target.value + + setInputValue(value) + + const seconds = parseFloat(value) + if (isNaN(seconds) || seconds <= 0) { + setInputError("Please enter a positive number") + return + } + + setInputError(null) + const timeout = Math.round(seconds * 1000) // Convert to milliseconds + + // Update local state + setShellIntegrationTimeout(timeout) + + // Send to extension + vscode.postMessage({ + type: "updateTerminalConnectionTimeout", + shellIntegrationTimeout: timeout, + }) + } + + const handleInputBlur = () => { + // If there was an error, reset the input to the current valid value + if (inputError) { + setInputValue((shellIntegrationTimeout / 1000).toString()) + setInputError(null) + } + } + + return ( +
+

Terminal Settings

+
+
+ +
+ handleTimeoutChange(event as Event)} + onBlur={handleInputBlur} + /> +
+ {inputError && ( +
{inputError}
+ )} +
+

+ Set how long Cline waits for shell integration to activate before executing commands. Increase this value if + you experience terminal connection timeouts. +

+
+
+ ) +} + +export default TerminalSettingsSection diff --git a/webview-ui/src/context/ExtensionStateContext.tsx b/webview-ui/src/context/ExtensionStateContext.tsx index 5592d1d1..d2b13a87 100644 --- a/webview-ui/src/context/ExtensionStateContext.tsx +++ b/webview-ui/src/context/ExtensionStateContext.tsx @@ -39,6 +39,7 @@ interface ExtensionStateContextType extends ExtensionState { setTelemetrySetting: (value: TelemetrySetting) => void setShowAnnouncement: (value: boolean) => void setPlanActSeparateModelsSetting: (value: boolean) => void + setShellIntegrationTimeout: (value: number) => void setMcpServers: (value: McpServer[]) => void // Navigation @@ -69,6 +70,7 @@ export const ExtensionStateContextProvider: React.FC<{ planActSeparateModelsSetting: true, globalClineRulesToggles: {}, localClineRulesToggles: {}, + shellIntegrationTimeout: 4000, // default timeout for shell integration }) const [didHydrateState, setDidHydrateState] = useState(false) const [showWelcome, setShowWelcome] = useState(false) @@ -242,6 +244,11 @@ export const ExtensionStateContextProvider: React.FC<{ ...prevState, shouldShowAnnouncement: value, })), + setShellIntegrationTimeout: (value) => + setState((prevState) => ({ + ...prevState, + shellIntegrationTimeout: value, + })), setMcpServers: (mcpServers: McpServer[]) => setMcpServers(mcpServers), setShowMcp, setMcpTab,