Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 42 additions & 33 deletions src/core/Cline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
addLineNumbers,
stripLineNumbers,
everyLineHasLineNumbers,
truncateOutput,
} from "../integrations/misc/extract-text"
import { TerminalManager, ExitCodeDetails } from "../integrations/terminal/TerminalManager"
import { UrlContentFetcher } from "../services/browser/UrlContentFetcher"
Expand Down Expand Up @@ -55,11 +54,12 @@ import { HistoryItem } from "../shared/HistoryItem"
import { ClineAskResponse } from "../shared/WebviewMessage"
import { GlobalFileNames } from "../shared/globalFileNames"
import { defaultModeSlug, getModeBySlug, getFullModeDetails } from "../shared/modes"
import { EXPERIMENT_IDS, experiments as Experiments, ExperimentId } from "../shared/experiments"
import { calculateApiCost } from "../utils/cost"
import { fileExistsAtPath } from "../utils/fs"
import { arePathsEqual, getReadablePath } from "../utils/path"
import { parseMentions } from "./mentions"
import { RooIgnoreController, LOCK_TEXT_SYMBOL } from "./ignore/RooIgnoreController"
import { RooIgnoreController } from "./ignore/RooIgnoreController"
import { AssistantMessageContent, parseAssistantMessage, ToolParamName, ToolUseName } from "./assistant-message"
import { formatResponse } from "./prompts/responses"
import { SYSTEM_PROMPT } from "./prompts/system"
Expand All @@ -70,7 +70,7 @@ import { BrowserSession } from "../services/browser/BrowserSession"
import { McpHub } from "../services/mcp/McpHub"
import crypto from "crypto"
import { insertGroups } from "./diff/insert-groups"
import { EXPERIMENT_IDS, experiments as Experiments, ExperimentId } from "../shared/experiments"
import { OutputBuilder } from "../integrations/terminal/OutputBuilder"

const cwd =
vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution
Expand Down Expand Up @@ -912,44 +912,52 @@ export class Cline {
// Tools

async executeCommandTool(command: string): Promise<[boolean, ToolResponse]> {
const { terminalOutputLimit } = (await this.providerRef.deref()?.getState()) ?? {}

const terminalInfo = await this.terminalManager.getOrCreateTerminal(cwd)
terminalInfo.terminal.show() // weird visual bug when creating new terminals (even manually) where there's an empty space at the top.
const process = this.terminalManager.runCommand(terminalInfo, command)
// Weird visual bug when creating new terminals (even manually) where
// there's an empty space at the top.
terminalInfo.terminal.show()
const process = this.terminalManager.runCommand(terminalInfo, command, terminalOutputLimit)

let userFeedback: { text?: string; images?: string[] } | undefined
let didContinue = false
const sendCommandOutput = async (line: string): Promise<void> => {

const sendCommandOutput = async (line: string) => {
try {
const { response, text, images } = await this.ask("command_output", line)

if (response === "yesButtonClicked") {
// proceed while running
// Proceed while running.
} else {
userFeedback = { text, images }
}

didContinue = true
process.continue() // continue past the await
process.continue() // Continue past the await.
} catch {
// This can only happen if this ask promise was ignored, so ignore this error
// This can only happen if this ask promise was ignored, so ignore this error.
}
}

let lines: string[] = []
let completed = false
let exitDetails: ExitCodeDetails | undefined

let builder = new OutputBuilder({ maxSize: terminalOutputLimit })
let output: string | undefined = undefined

process.on("line", (line) => {
lines.push(line)
builder.append(line)

if (!didContinue) {
sendCommandOutput(line)
} else {
this.say("command_output", line)
}
})

let completed = false
let exitDetails: ExitCodeDetails | undefined
process.once("completed", (output?: string) => {
// Use provided output if available, otherwise keep existing result.
if (output) {
lines = output.split("\n")
}
process.once("completed", (buffer?: string) => {
output = buffer
completed = true
})

Expand All @@ -965,19 +973,17 @@ export class Cline {

await process

// Wait for a short delay to ensure all messages are sent to the webview
// Wait for a short delay to ensure all messages are sent to the webview.
// This delay allows time for non-awaited promises to be created and
// for their associated messages to be sent to the webview, maintaining
// the correct order of messages (although the webview is smart about
// grouping command_output messages despite any gaps anyways)
// grouping command_output messages despite any gaps anyways).
await delay(50)

const { terminalOutputLineLimit } = (await this.providerRef.deref()?.getState()) ?? {}
const output = truncateOutput(lines.join("\n"), terminalOutputLineLimit)
const result = output.trim()
const result = output || builder.content

if (userFeedback) {
await this.say("user_feedback", userFeedback.text, userFeedback.images)

return [
true,
formatResponse.toolResult(
Expand All @@ -991,25 +997,28 @@ export class Cline {

if (completed) {
let exitStatus = "No exit code available"

if (exitDetails !== undefined) {
if (exitDetails.signal) {
exitStatus = `Process terminated by signal ${exitDetails.signal} (${exitDetails.signalName})`

if (exitDetails.coreDumpPossible) {
exitStatus += " - core dump possible"
}
} else {
exitStatus = `Exit code: ${exitDetails.exitCode}`
}
}

return [false, `Command executed. ${exitStatus}${result.length > 0 ? `\nOutput:\n${result}` : ""}`]
} else {
return [
false,
`Command is still running in the user's terminal.${
result.length > 0 ? `\nHere's the output so far:\n${result}` : ""
}\n\nYou will be updated on the terminal status and new output in the future.`,
]
}

return [
false,
`Command is still running in the user's terminal.${
result.length > 0 ? `\nHere's the output so far:\n${result}` : ""
}\n\nYou will be updated on the terminal status and new output in the future.`,
]
}

async *attemptApiRequest(previousApiReqIndex: number, retryAttempt: number = 0): ApiStream {
Expand Down Expand Up @@ -3495,7 +3504,7 @@ export class Cline {
terminalDetails += "\n\n# Actively Running Terminals"
for (const busyTerminal of busyTerminals) {
terminalDetails += `\n## Original command: \`${busyTerminal.lastCommand}\``
const newOutput = this.terminalManager.getUnretrievedOutput(busyTerminal.id)
const newOutput = this.terminalManager.readLine(busyTerminal.id)
if (newOutput) {
terminalDetails += `\n### New Output\n${newOutput}`
} else {
Expand All @@ -3507,7 +3516,7 @@ export class Cline {
if (inactiveTerminals.length > 0) {
const inactiveTerminalOutputs = new Map<number, string>()
for (const inactiveTerminal of inactiveTerminals) {
const newOutput = this.terminalManager.getUnretrievedOutput(inactiveTerminal.id)
const newOutput = this.terminalManager.readLine(inactiveTerminal.id)
if (newOutput) {
inactiveTerminalOutputs.set(inactiveTerminal.id, newOutput)
}
Expand Down
73 changes: 73 additions & 0 deletions src/core/__tests__/contextProxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,77 @@ describe("ContextProxy", () => {
expect(proxy.getGlobalState("unknownKey")).toBe("some-value")
})
})

describe("resetAllState", () => {
it("should clear all in-memory caches", async () => {
// Setup initial state in caches
await proxy.setValues({
apiModelId: "gpt-4", // global state
openAiApiKey: "test-api-key", // secret
unknownKey: "some-value", // unknown
})

// Verify initial state
expect(proxy.getGlobalState("apiModelId")).toBe("gpt-4")
expect(proxy.getSecret("openAiApiKey")).toBe("test-api-key")
expect(proxy.getGlobalState("unknownKey")).toBe("some-value")

// Reset all state
await proxy.resetAllState()

// Caches should be reinitialized with values from the context
// Since our mock globalState.get returns undefined by default,
// the cache should now contain undefined values
expect(proxy.getGlobalState("apiModelId")).toBeUndefined()
expect(proxy.getGlobalState("unknownKey")).toBeUndefined()
})

it("should update all global state keys to undefined", async () => {
// Setup initial state
await proxy.updateGlobalState("apiModelId", "gpt-4")
await proxy.updateGlobalState("apiProvider", "openai")

// Reset all state
await proxy.resetAllState()

// Should have called update with undefined for each key
for (const key of GLOBAL_STATE_KEYS) {
expect(mockGlobalState.update).toHaveBeenCalledWith(key, undefined)
}

// Total calls should include initial setup + reset operations
const expectedUpdateCalls = 2 + GLOBAL_STATE_KEYS.length
expect(mockGlobalState.update).toHaveBeenCalledTimes(expectedUpdateCalls)
})

it("should delete all secrets", async () => {
// Setup initial secrets
await proxy.storeSecret("apiKey", "test-api-key")
await proxy.storeSecret("openAiApiKey", "test-openai-key")

// Reset all state
await proxy.resetAllState()

// Should have called delete for each key
for (const key of SECRET_KEYS) {
expect(mockSecrets.delete).toHaveBeenCalledWith(key)
}

// Total calls should equal the number of secret keys
expect(mockSecrets.delete).toHaveBeenCalledTimes(SECRET_KEYS.length)
})

it("should reinitialize caches after reset", async () => {
// Spy on initialization methods
const initStateCache = jest.spyOn(proxy as any, "initializeStateCache")
const initSecretCache = jest.spyOn(proxy as any, "initializeSecretCache")

// Reset all state
await proxy.resetAllState()

// Should reinitialize caches
expect(initStateCache).toHaveBeenCalledTimes(1)
expect(initSecretCache).toHaveBeenCalledTimes(1)
})
})
})
25 changes: 25 additions & 0 deletions src/core/contextProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,29 @@ export class ContextProxy {

return Promise.all(promises)
}

/**
* Resets all global state, secrets, and in-memory caches.
* This clears all data from both the in-memory caches and the VSCode storage.
* @returns A promise that resolves when all reset operations are complete
*/
async resetAllState(): Promise<void> {
// Clear in-memory caches
this.stateCache.clear()
this.secretCache.clear()

// Reset all global state values to undefined
const stateResetPromises = GLOBAL_STATE_KEYS.map((key) =>
this.originalContext.globalState.update(key, undefined),
)

// Delete all secrets
const secretResetPromises = SECRET_KEYS.map((key) => this.originalContext.secrets.delete(key))

// Wait for all reset operations to complete
await Promise.all([...stateResetPromises, ...secretResetPromises])

this.initializeStateCache()
this.initializeSecretCache()
}
}
4 changes: 2 additions & 2 deletions src/core/mentions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import * as vscode from "vscode"
import * as path from "path"
import { openFile } from "../../integrations/misc/open-file"
import { UrlContentFetcher } from "../../services/browser/UrlContentFetcher"
import { mentionRegexGlobal, formatGitSuggestion, type MentionSuggestion } from "../../shared/context-mentions"
import { mentionRegexGlobal } from "../../shared/context-mentions"
import fs from "fs/promises"
import { extractTextFromFile } from "../../integrations/misc/extract-text"
import { isBinaryFile } from "isbinaryfile"
import { diagnosticsToProblemsString } from "../../integrations/diagnostics"
import { getCommitInfo, getWorkingState } from "../../utils/git"
import { getLatestTerminalOutput } from "../../integrations/terminal/get-latest-output"
import { getLatestTerminalOutput } from "../../integrations/terminal/getLatestTerminalOutput"

export async function openMention(mention?: string): Promise<void> {
if (!mention) {
Expand Down
26 changes: 9 additions & 17 deletions src/core/webview/ClineProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import simpleGit from "simple-git"
import { setPanel } from "../../activate/registerCommands"

import { ApiConfiguration, ApiProvider, ModelInfo, API_CONFIG_KEYS } from "../../shared/api"
import { CheckpointStorage } from "../../shared/checkpoints"
import { findLast } from "../../shared/array"
import { CustomSupportPrompts, supportPrompt } from "../../shared/support-prompt"
import { supportPrompt } from "../../shared/support-prompt"
import { GlobalFileNames } from "../../shared/globalFileNames"
import { SecretKey, GlobalStateKey, SECRET_KEYS, GLOBAL_STATE_KEYS } from "../../shared/globalState"
import { HistoryItem } from "../../shared/HistoryItem"
import { ApiConfigMeta, ExtensionMessage } from "../../shared/ExtensionMessage"
import { checkoutDiffPayloadSchema, checkoutRestorePayloadSchema, WebviewMessage } from "../../shared/WebviewMessage"
import { Mode, CustomModePrompts, PromptComponent, defaultModeSlug, ModeConfig } from "../../shared/modes"
import { Mode, PromptComponent, defaultModeSlug, ModeConfig } from "../../shared/modes"
import { checkExistKey } from "../../shared/checkExistApiConfig"
import { EXPERIMENT_IDS, experiments as Experiments, experimentDefault, ExperimentId } from "../../shared/experiments"
import { TERMINAL_OUTPUT_LIMIT } from "../../shared/terminal"
import { downloadTask } from "../../integrations/misc/export-markdown"
import { openFile, openImage } from "../../integrations/misc/open-file"
import { selectImages } from "../../integrations/misc/process-images"
Expand Down Expand Up @@ -1215,7 +1215,6 @@ export class ClineProvider implements vscode.WebviewViewProvider {
await this.postStateToWebview()
break
case "checkpointStorage":
console.log(`[ClineProvider] checkpointStorage: ${message.text}`)
const checkpointStorage = message.text ?? "task"
await this.updateGlobalState("checkpointStorage", checkpointStorage)
await this.postStateToWebview()
Expand Down Expand Up @@ -1249,8 +1248,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
await this.updateGlobalState("writeDelayMs", message.value)
await this.postStateToWebview()
break
case "terminalOutputLineLimit":
await this.updateGlobalState("terminalOutputLineLimit", message.value)
case "terminalOutputLimit":
await this.updateGlobalState("terminalOutputLimit", message.value)
await this.postStateToWebview()
break
case "mode":
Expand Down Expand Up @@ -2134,7 +2133,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
screenshotQuality,
preferredLanguage,
writeDelayMs,
terminalOutputLineLimit,
terminalOutputLimit,
fuzzyMatchThreshold,
mcpEnabled,
enableMcpServerCreation,
Expand Down Expand Up @@ -2186,7 +2185,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
screenshotQuality: screenshotQuality ?? 75,
preferredLanguage: preferredLanguage ?? "English",
writeDelayMs: writeDelayMs ?? 1000,
terminalOutputLineLimit: terminalOutputLineLimit ?? 500,
terminalOutputLimit: terminalOutputLimit ?? TERMINAL_OUTPUT_LIMIT,
fuzzyMatchThreshold: fuzzyMatchThreshold ?? 1.0,
mcpEnabled: mcpEnabled ?? true,
enableMcpServerCreation: enableMcpServerCreation ?? true,
Expand Down Expand Up @@ -2334,7 +2333,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
screenshotQuality: stateValues.screenshotQuality ?? 75,
fuzzyMatchThreshold: stateValues.fuzzyMatchThreshold ?? 1.0,
writeDelayMs: stateValues.writeDelayMs ?? 1000,
terminalOutputLineLimit: stateValues.terminalOutputLineLimit ?? 500,
terminalOutputLimit: stateValues.terminalOutputLimit ?? TERMINAL_OUTPUT_LIMIT,
mode: stateValues.mode ?? defaultModeSlug,
preferredLanguage:
stateValues.preferredLanguage ??
Expand Down Expand Up @@ -2433,14 +2432,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
return
}

for (const key of this.context.globalState.keys()) {
await this.contextProxy.updateGlobalState(key, undefined)
}

for (const key of SECRET_KEYS) {
await this.storeSecret(key, undefined)
}

await this.contextProxy.resetAllState()
await this.configManager.resetAllConfigs()
await this.customModesManager.resetCustomModes()
await this.removeClineFromStack()
Expand Down
Loading