Skip to content

fix: use stable cache key for SSH remote workspaces #6067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
53 changes: 51 additions & 2 deletions src/services/code-index/__tests__/cache-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as vscode from "vscode"
import { createHash } from "crypto"
import debounce from "lodash.debounce"
import { CacheManager } from "../cache-manager"
import * as path from "path"
import * as os from "os"

// Mock safeWriteJson utility
vitest.mock("../../../utils/safeWriteJson", () => ({
Expand Down Expand Up @@ -38,6 +40,11 @@ vitest.mock("@roo-code/telemetry", () => ({
},
}))

// Mock os module
vitest.mock("os", () => ({
homedir: vitest.fn(() => "/home/user"),
}))

describe("CacheManager", () => {
let mockContext: vscode.ExtensionContext
let mockWorkspacePath: string
Expand All @@ -63,8 +70,20 @@ describe("CacheManager", () => {
})

describe("constructor", () => {
it("should correctly set up cachePath using Uri.joinPath and crypto.createHash", () => {
const expectedHash = createHash("sha256").update(mockWorkspacePath).digest("hex")
it("should correctly set up cachePath using Uri.joinPath with stable cache key", () => {
// The cache key should be based on workspace name and relative path
const workspaceName = path.basename(mockWorkspacePath)
const homedir = os.homedir()
let relativePath = mockWorkspacePath

if (mockWorkspacePath.startsWith(homedir)) {
relativePath = path.relative(homedir, mockWorkspacePath)
}

// Normalize path separators for consistency
const normalizedRelativePath = relativePath.replace(/\\/g, "/")
const compositeKey = `${workspaceName}::${normalizedRelativePath}`
const expectedHash = createHash("sha256").update(compositeKey).digest("hex")

expect(vscode.Uri.joinPath).toHaveBeenCalledWith(
mockContext.globalStorageUri,
Expand All @@ -75,6 +94,36 @@ describe("CacheManager", () => {
it("should set up debounced save function", () => {
expect(debounce).toHaveBeenCalledWith(expect.any(Function), 1500)
})

it("should generate stable cache key for workspace under home directory", () => {
// os.homedir is already mocked to return '/home/user'
const workspaceUnderHome = "/home/user/projects/myproject"
const cacheManagerHome = new CacheManager(mockContext, workspaceUnderHome)

// Expected key should use relative path from home
const expectedKey = `myproject::projects/myproject`
const expectedHash = createHash("sha256").update(expectedKey).digest("hex")

expect(vscode.Uri.joinPath).toHaveBeenCalledWith(
mockContext.globalStorageUri,
`roo-index-cache-${expectedHash}.json`,
)
})

it("should generate stable cache key for workspace outside home directory", () => {
// os.homedir is already mocked to return '/home/user'
const workspaceOutsideHome = "/opt/projects/myproject"
const cacheManagerOutside = new CacheManager(mockContext, workspaceOutsideHome)

// Expected key should use full path since it's outside home
const expectedKey = `myproject::/opt/projects/myproject`
const expectedHash = createHash("sha256").update(expectedKey).digest("hex")

expect(vscode.Uri.joinPath).toHaveBeenCalledWith(
mockContext.globalStorageUri,
`roo-index-cache-${expectedHash}.json`,
)
})
})

describe("initialize", () => {
Expand Down
45 changes: 41 additions & 4 deletions src/services/code-index/cache-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import debounce from "lodash.debounce"
import { safeWriteJson } from "../../utils/safeWriteJson"
import { TelemetryService } from "@roo-code/telemetry"
import { TelemetryEventName } from "@roo-code/types"
import * as path from "path"
import * as os from "os"

/**
* Manages the cache for code indexing
Expand All @@ -23,15 +25,50 @@ export class CacheManager implements ICacheManager {
private context: vscode.ExtensionContext,
private workspacePath: string,
) {
this.cachePath = vscode.Uri.joinPath(
context.globalStorageUri,
`roo-index-cache-${createHash("sha256").update(workspacePath).digest("hex")}.json`,
)
// Generate a stable cache key that persists across SSH sessions
const cacheKey = this.generateStableCacheKey(workspacePath)
this.cachePath = vscode.Uri.joinPath(context.globalStorageUri, `roo-index-cache-${cacheKey}.json`)
this._debouncedSaveCache = debounce(async () => {
await this._performSave()
}, 1500)
}

/**
* Generates a stable cache key for the workspace that persists across SSH sessions
* @param workspacePath The workspace path
* @returns A stable hash key
*/
private generateStableCacheKey(workspacePath: string): string {
// Get the workspace folder name
const workspaceName = path.basename(workspacePath)

// Try to get a relative path from home directory for additional stability
const homedir = os.homedir()
let relativePath = workspacePath

try {
// If the workspace is under the home directory, use the relative path
if (workspacePath.startsWith(homedir)) {
relativePath = path.relative(homedir, workspacePath)
}
} catch (error) {
// If we can't get relative path, just use the full path
console.warn("Failed to get relative path from home directory:", error)
}

// Normalize path separators to forward slashes for consistency across platforms
// This ensures the same cache key is generated regardless of the OS
const normalizedRelativePath = relativePath.replace(/\\/g, "/")

// Create a composite key using workspace name and normalized relative path
// This should be more stable across SSH sessions where the absolute path might change
// but the relative structure remains the same
const compositeKey = `${workspaceName}::${normalizedRelativePath}`

// Generate hash from the composite key
return createHash("sha256").update(compositeKey).digest("hex")
}

/**
* Initializes the cache manager by loading the cache file
*/
Expand Down