From a472461478b08f9d4c0a0d403a32298bbfed9cd0 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Thu, 10 Jul 2025 19:27:00 +0530 Subject: [PATCH 1/3] introduce indexing lock --- core/core.ts | 6 --- core/index.d.ts | 1 + core/indexing/CodebaseIndexer.ts | 21 ++++++++- core/indexing/refreshIndex.ts | 45 +++++++++++++++++++ .../IndexingProgress/IndexingProgress.tsx | 2 +- .../IndexingProgressIndicator.tsx | 1 + .../IndexingProgressSubtext.tsx | 1 + .../IndexingProgressTitleText.tsx | 1 + 8 files changed, 69 insertions(+), 9 deletions(-) diff --git a/core/core.ts b/core/core.ts index 72931c5e8..915099a3e 100644 --- a/core/core.ts +++ b/core/core.ts @@ -11,7 +11,6 @@ import { ConfigHandler } from "./config/ConfigHandler"; import { SYSTEM_PROMPT_DOT_FILE } from "./config/getWorkspaceContinueRuleDotFiles"; import { addModel, deleteModel } from "./config/util"; import CurrentFileContextProvider from "./context/providers/CurrentFileContextProvider"; -import { ContinueServerClient } from "./continueServer/stubs/client"; import { getAuthUrlForTokenPage } from "./control-plane/auth/index"; import { getControlPlaneEnv } from "./control-plane/env"; import { DevDataSqliteDb } from "./data/devdataSqlite"; @@ -202,11 +201,6 @@ export class Core { dataLogger.ideSettingsPromise = ideSettingsPromise; void ideSettingsPromise.then((ideSettings) => { - const continueServerClient = new ContinueServerClient( - ideSettings.remoteConfigServerUrl, - ideSettings.userToken, - ); - // Index on initialization void this.ide.getWorkspaceDirs().then(async (dirs) => { // Respect pauseCodebaseIndexOnStart user settings diff --git a/core/index.d.ts b/core/index.d.ts index 1ace46141..014dcc6de 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -50,6 +50,7 @@ export interface IndexingProgressUpdate { shouldClearIndexes?: boolean; status: | "loading" + | "waiting" | "indexing" | "done" | "failed" diff --git a/core/indexing/CodebaseIndexer.ts b/core/indexing/CodebaseIndexer.ts index e867399fd..2488313ba 100644 --- a/core/indexing/CodebaseIndexer.ts +++ b/core/indexing/CodebaseIndexer.ts @@ -16,7 +16,7 @@ import { ChunkCodebaseIndex } from "./chunk/ChunkCodebaseIndex.js"; import { CodeSnippetsCodebaseIndex } from "./CodeSnippetsIndex.js"; import { FullTextSearchCodebaseIndex } from "./FullTextSearchCodebaseIndex.js"; import { LanceDbIndex } from "./LanceDbIndex.js"; -import { getComputeDeleteAddRemove } from "./refreshIndex.js"; +import { getComputeDeleteAddRemove, IndexLock } from "./refreshIndex.js"; import { CodebaseIndex, IndexResultType, @@ -643,11 +643,28 @@ export class CodebaseIndexer { ); } + private async *waitForDBIndex(): AsyncGenerator { + let foundLock = await IndexLock.isLocked(); + while (foundLock?.locked) { + console.log(`indexing ${foundLock.dirs}`); + yield { + progress: 0, + desc: "", + status: "waiting", + }; + await new Promise((resolve) => setTimeout(resolve, 1000)); + } + } + public async refreshCodebaseIndex(paths: string[]) { if (this.indexingCancellationController) { this.indexingCancellationController.abort(); } this.indexingCancellationController = new AbortController(); + for await (const update of this.waitForDBIndex()) { + this.updateProgress(update); + } + IndexLock.lock(paths.join(", ")); // acquire the index lock to prevent multiple windows to start indexing try { for await (const update of this.refreshDirs( paths, @@ -663,7 +680,7 @@ export class CodebaseIndexer { console.log(`Failed refreshing codebase index directories: ${e}`); await this.handleIndexingError(e); } - + IndexLock.unlock(); // release the index lock // Directly refresh submenu items if (this.messenger) { this.messenger.send("refreshSubmenuItems", { diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index ec84f631b..0b4b0ac83 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -45,6 +45,16 @@ export class SqliteDb { artifactId STRING NOT NULL )`, ); + + await db.exec( + `CREATE TABLE IF NOT EXISTS indexing_lock ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + locked BOOLEAN NOT NULL, + timestamp INTEGER NOT NULL, + dirs STRING NOT NULL + )`, + ); + // Delete duplicate rows from tag_catalog await db.exec(` DELETE FROM tag_catalog @@ -532,3 +542,38 @@ export function truncateToLastNBytes(input: string, maxBytes: number): string { export function truncateSqliteLikePattern(input: string, safety: number = 100) { return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH - safety); } + +export class IndexLock { + private static async getLockTableName() { + return "indexing_lock"; + } + + static async isLocked(): Promise< + { locked: boolean; dirs: string } | undefined | undefined + > { + const db = await SqliteDb.get(); + const lockTableName = await IndexLock.getLockTableName(); + const row = (await db.get( + `SELECT locked, dirs FROM ${lockTableName} WHERE locked = ?`, + true, + )) as { locked: boolean; dirs: string } | undefined; + return row; + } + + static async lock(dirs: string) { + const db = await SqliteDb.get(); + const lockTableName = await IndexLock.getLockTableName(); + await db.run( + `INSERT INTO ${lockTableName} (locked, timestamp, dirs) VALUES (?, ?, ?)`, + true, + Date.now(), + dirs, + ); + } + + static async unlock() { + const db = await SqliteDb.get(); + const lockTableName = await IndexLock.getLockTableName(); + await db.run(`DELETE FROM ${lockTableName} WHERE locked = ?`, true); + } +} diff --git a/gui/src/pages/config/IndexingProgress/IndexingProgress.tsx b/gui/src/pages/config/IndexingProgress/IndexingProgress.tsx index 6347409c1..e0df05b62 100644 --- a/gui/src/pages/config/IndexingProgress/IndexingProgress.tsx +++ b/gui/src/pages/config/IndexingProgress/IndexingProgress.tsx @@ -111,7 +111,7 @@ function IndexingProgress() {
- {update.status !== "loading" && ( + {["loading", "waiting"].includes(update.status) && ( )}
diff --git a/gui/src/pages/config/IndexingProgress/IndexingProgressIndicator.tsx b/gui/src/pages/config/IndexingProgress/IndexingProgressIndicator.tsx index c7c8602dc..2604d9ead 100644 --- a/gui/src/pages/config/IndexingProgress/IndexingProgressIndicator.tsx +++ b/gui/src/pages/config/IndexingProgress/IndexingProgressIndicator.tsx @@ -13,6 +13,7 @@ export interface IndexingProgressIndicatorProps { const STATUS_TO_ICON: Record = { disabled: null, loading: null, + waiting: null, indexing: ArrowPathIcon, paused: PauseCircleIcon, done: CheckCircleIcon, diff --git a/gui/src/pages/config/IndexingProgress/IndexingProgressSubtext.tsx b/gui/src/pages/config/IndexingProgress/IndexingProgressSubtext.tsx index 02622d7a9..e24eb5564 100644 --- a/gui/src/pages/config/IndexingProgress/IndexingProgressSubtext.tsx +++ b/gui/src/pages/config/IndexingProgress/IndexingProgressSubtext.tsx @@ -11,6 +11,7 @@ const STATUS_TO_SUBTITLE_TEXT: Record< > = { done: "Click to re-index", loading: "", + waiting: "", indexing: "Click to pause", paused: "Click to resume", failed: "Click to retry", diff --git a/gui/src/pages/config/IndexingProgress/IndexingProgressTitleText.tsx b/gui/src/pages/config/IndexingProgress/IndexingProgressTitleText.tsx index e15ba5713..dc0666479 100644 --- a/gui/src/pages/config/IndexingProgress/IndexingProgressTitleText.tsx +++ b/gui/src/pages/config/IndexingProgress/IndexingProgressTitleText.tsx @@ -8,6 +8,7 @@ export interface IndexingProgressTitleTextProps { const STATUS_TO_TEXT: Record = { done: "Indexing complete", loading: "Initializing", + waiting: "Indexing other workspace", indexing: "Indexing in-progress", paused: "Indexing paused", failed: "Indexing failed", From 69f035d263270aa28035f0e7ac00ace8bd7a0177 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Thu, 10 Jul 2025 19:28:18 +0530 Subject: [PATCH 2/3] fix eslint --- core/indexing/CodebaseIndexer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/indexing/CodebaseIndexer.ts b/core/indexing/CodebaseIndexer.ts index 2488313ba..84023749f 100644 --- a/core/indexing/CodebaseIndexer.ts +++ b/core/indexing/CodebaseIndexer.ts @@ -664,7 +664,7 @@ export class CodebaseIndexer { for await (const update of this.waitForDBIndex()) { this.updateProgress(update); } - IndexLock.lock(paths.join(", ")); // acquire the index lock to prevent multiple windows to start indexing + await IndexLock.lock(paths.join(", ")); // acquire the index lock to prevent multiple windows to start indexing try { for await (const update of this.refreshDirs( paths, @@ -680,7 +680,7 @@ export class CodebaseIndexer { console.log(`Failed refreshing codebase index directories: ${e}`); await this.handleIndexingError(e); } - IndexLock.unlock(); // release the index lock + await IndexLock.unlock(); // release the index lock // Directly refresh submenu items if (this.messenger) { this.messenger.send("refreshSubmenuItems", { From a020a49f4b90aab15a85da2b1a1944288a80d577 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Thu, 10 Jul 2025 20:47:44 +0530 Subject: [PATCH 3/3] add timestamp calculation --- core/indexing/CodebaseIndexer.ts | 26 ++++++++++++++++-- core/indexing/refreshIndex.ts | 27 ++++++++++++------- .../IndexingProgress/IndexingProgress.tsx | 2 +- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/core/indexing/CodebaseIndexer.ts b/core/indexing/CodebaseIndexer.ts index 84023749f..0bd9b1328 100644 --- a/core/indexing/CodebaseIndexer.ts +++ b/core/indexing/CodebaseIndexer.ts @@ -643,9 +643,21 @@ export class CodebaseIndexer { ); } + /** + * We want to prevent sqlite concurrent write errors + * when there are 2 indexing happening from different windows. + * We want the other window to wait until the first window's indexing finishes. + * Incase the first window closes before indexing is finished, + * we want to unlock the IndexLock by checking the last timestamp. + */ private async *waitForDBIndex(): AsyncGenerator { let foundLock = await IndexLock.isLocked(); while (foundLock?.locked) { + if ((Date.now() - foundLock.timestamp) / 1000 > 10) { + console.log(`${foundLock.dirs} is not being indexed... unlocking`); + await IndexLock.unlock(); + break; + } console.log(`indexing ${foundLock.dirs}`); yield { progress: 0, @@ -653,6 +665,7 @@ export class CodebaseIndexer { status: "waiting", }; await new Promise((resolve) => setTimeout(resolve, 1000)); + foundLock = await IndexLock.isLocked(); } } @@ -664,7 +677,13 @@ export class CodebaseIndexer { for await (const update of this.waitForDBIndex()) { this.updateProgress(update); } - await IndexLock.lock(paths.join(", ")); // acquire the index lock to prevent multiple windows to start indexing + + await IndexLock.lock(paths.join(", ")); // acquire the index lock to prevent multiple windows to begin indexing + const indexLockTimestampUpdateInterval = setInterval( + () => void IndexLock.updateTimestamp(), + 5_000, + ); + try { for await (const update of this.refreshDirs( paths, @@ -680,7 +699,10 @@ export class CodebaseIndexer { console.log(`Failed refreshing codebase index directories: ${e}`); await this.handleIndexingError(e); } - await IndexLock.unlock(); // release the index lock + + clearInterval(indexLockTimestampUpdateInterval); // interval will also be cleared when window closes before indexing is finished + await IndexLock.unlock(); + // Directly refresh submenu items if (this.messenger) { this.messenger.send("refreshSubmenuItems", { diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index 0b4b0ac83..42c4f0c8d 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -544,36 +544,45 @@ export function truncateSqliteLikePattern(input: string, safety: number = 100) { } export class IndexLock { - private static async getLockTableName() { + private static getLockTableName() { return "indexing_lock"; } static async isLocked(): Promise< - { locked: boolean; dirs: string } | undefined | undefined + { locked: boolean; dirs: string; timestamp: number } | undefined | undefined > { const db = await SqliteDb.get(); - const lockTableName = await IndexLock.getLockTableName(); const row = (await db.get( - `SELECT locked, dirs FROM ${lockTableName} WHERE locked = ?`, + `SELECT locked, dirs, timestamp FROM ${IndexLock.getLockTableName()} WHERE locked = ?`, true, - )) as { locked: boolean; dirs: string } | undefined; + )) as { locked: boolean; dirs: string; timestamp: number } | undefined; return row; } static async lock(dirs: string) { const db = await SqliteDb.get(); - const lockTableName = await IndexLock.getLockTableName(); await db.run( - `INSERT INTO ${lockTableName} (locked, timestamp, dirs) VALUES (?, ?, ?)`, + `INSERT INTO ${IndexLock.getLockTableName()} (locked, timestamp, dirs) VALUES (?, ?, ?)`, true, Date.now(), dirs, ); } + static async updateTimestamp() { + const db = await SqliteDb.get(); + await db.run( + `UPDATE ${IndexLock.getLockTableName()} SET timestamp = ? where locked = ?`, + Date.now(), + true, + ); + } + static async unlock() { const db = await SqliteDb.get(); - const lockTableName = await IndexLock.getLockTableName(); - await db.run(`DELETE FROM ${lockTableName} WHERE locked = ?`, true); + await db.run( + `DELETE FROM ${IndexLock.getLockTableName()} WHERE locked = ?`, + true, + ); } } diff --git a/gui/src/pages/config/IndexingProgress/IndexingProgress.tsx b/gui/src/pages/config/IndexingProgress/IndexingProgress.tsx index e0df05b62..abe4de39d 100644 --- a/gui/src/pages/config/IndexingProgress/IndexingProgress.tsx +++ b/gui/src/pages/config/IndexingProgress/IndexingProgress.tsx @@ -111,7 +111,7 @@ function IndexingProgress() {
- {["loading", "waiting"].includes(update.status) && ( + {!["loading", "waiting"].includes(update.status) && ( )}