diff --git a/core/autocomplete/cache.ts b/core/autocomplete/cache.ts index 4886feb23..c04770a4c 100644 --- a/core/autocomplete/cache.ts +++ b/core/autocomplete/cache.ts @@ -1,14 +1,14 @@ import { Mutex } from "async-mutex"; import { open } from "sqlite"; import sqlite3 from "sqlite3"; -import { DatabaseConnection } from "../indexing/refreshIndex.js"; +import { DatabaseConnection, truncateSqliteLikePattern } from "../indexing/refreshIndex.js"; import { getTabAutocompleteCacheSqlitePath } from "../util/paths.js"; export class AutocompleteLruCache { private static capacity = 1000; private mutex = new Mutex(); - constructor(private db: DatabaseConnection) {} + constructor(private db: DatabaseConnection) { } static async get(): Promise { const db = await open({ @@ -37,7 +37,7 @@ export class AutocompleteLruCache { // Have to make sure we take the key with shortest length const result = await this.db.get( "SELECT key, value FROM cache WHERE ? LIKE key || '%' ORDER BY LENGTH(key) DESC LIMIT 1", - prefix, + truncateSqliteLikePattern(prefix), ); // Validate that the cached compeltion is a valid completion for the prefix diff --git a/core/indexing/CodeSnippetsIndex.ts b/core/indexing/CodeSnippetsIndex.ts index a8b41cafe..70872aff9 100644 --- a/core/indexing/CodeSnippetsIndex.ts +++ b/core/indexing/CodeSnippetsIndex.ts @@ -14,7 +14,7 @@ import { getParserForFile, getQueryForFile, } from "../util/treeSitter"; -import { DatabaseConnection, SqliteDb, tagToString } from "./refreshIndex"; +import { DatabaseConnection, SqliteDb, tagToString, truncateSqliteLikePattern } from "./refreshIndex"; import { IndexResultType, MarkCompleteCallback, @@ -28,7 +28,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { relativeExpectedTime: number = 1; artifactId = "codeSnippets"; - constructor(private readonly ide: IDE) {} + constructor(private readonly ide: IDE) { } private static async _createTables(db: DatabaseConnection) { await db.exec(`CREATE TABLE IF NOT EXISTS code_snippets ( @@ -383,7 +383,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { const db = await SqliteDb.get(); await CodeSnippetsCodebaseIndex._createTables(db); - const likePatterns = workspaceDirs.map((dir) => `${dir}%`); + const likePatterns = workspaceDirs.map((dir) => truncateSqliteLikePattern(`${dir}%`)); const placeholders = likePatterns.map(() => "?").join(" OR path LIKE "); const query = ` diff --git a/core/indexing/refreshIndex.test.ts b/core/indexing/refreshIndex.test.ts new file mode 100644 index 000000000..7eeb2c5d8 --- /dev/null +++ b/core/indexing/refreshIndex.test.ts @@ -0,0 +1,34 @@ +import { truncateToLastNBytes } from "./refreshIndex"; + +describe("truncateToLastNBytes", () => { + it("should return full string if maxBytes greater than string byte length", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 100); + expect(result).toBe("Hello World"); + }); + + it("should truncate ASCII string correctly", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 5); + expect(result).toBe("World"); + }); + + it("should handle empty string", () => { + const input = ""; + const result = truncateToLastNBytes(input, 5); + expect(result).toBe(""); + }); + + it("should handle UTF-8 characters correctly", () => { + const input = "👋 Hello"; + // 👋 is 4 bytes, space is 1 byte + const result = truncateToLastNBytes(input, 5); + expect(result).toBe("Hello"); + }); + + it("should handle maxBytes of 0", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 0); + expect(result).toBe(""); + }); +}); diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index cf59899f1..ba2dbd820 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -469,3 +469,24 @@ export class GlobalCacheCodeBaseIndex implements CodebaseIndex { ); } } + +const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000; + +export function truncateToLastNBytes(input: string, maxBytes: number): string { + let bytes = 0; + let startIndex = 0; + + for (let i = input.length - 1; i >= 0; i--) { + bytes += new TextEncoder().encode(input[i]).length; + if (bytes > maxBytes) { + startIndex = i + 1; + break; + } + } + + return input.substring(startIndex, input.length); +} + +export function truncateSqliteLikePattern(input: string) { + return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); +}