Elffuss BraidHub

kikocis/continue public

Merge pull request #6554 from uinstinct/indexing-lock

feat: lock indexing until completion
Changeseta4e52f074c3846c6d0e25181e844c8e881b2cb4f659c3bd321ceab0930f4003c
AuthorDallin Romney <dallinromney@gmail.com> · 2025-07-11 00:07 -07:00
CommitterGitHub <noreply@github.com> · 2025-07-11 00:07 -07:00
Parents275abb44c5e4, d667427437b4

Browse the files at this changeset

8 files changed

modified core/core.ts +0 −6
@@ -11,7 +11,6 @@
1111 import { SYSTEM_PROMPT_DOT_FILE } from "./config/getWorkspaceContinueRuleDotFiles";
1212 import { addModel, deleteModel } from "./config/util";
1313 import CurrentFileContextProvider from "./context/providers/CurrentFileContextProvider";
14-import { ContinueServerClient } from "./continueServer/stubs/client";
1514 import { getAuthUrlForTokenPage } from "./control-plane/auth/index";
1615 import { getControlPlaneEnv } from "./control-plane/env";
1716 import { DevDataSqliteDb } from "./data/devdataSqlite";
@@ -202,11 +201,6 @@
202201 dataLogger.ideSettingsPromise = ideSettingsPromise;
203202
204203 void ideSettingsPromise.then((ideSettings) => {
205- const continueServerClient = new ContinueServerClient(
206- ideSettings.remoteConfigServerUrl,
207- ideSettings.userToken,
208- );
209-
210204 // Index on initialization
211205 void this.ide.getWorkspaceDirs().then(async (dirs) => {
212206 // Respect pauseCodebaseIndexOnStart user settings
modified core/index.d.ts +1 −0
@@ -50,6 +50,7 @@
5050 shouldClearIndexes?: boolean;
5151 status:
5252 | "loading"
53+ | "waiting"
5354 | "indexing"
5455 | "done"
5556 | "failed"
modified core/indexing/CodebaseIndexer.ts +40 −1
@@ -16,7 +16,7 @@
1616 import { CodeSnippetsCodebaseIndex } from "./CodeSnippetsIndex.js";
1717 import { FullTextSearchCodebaseIndex } from "./FullTextSearchCodebaseIndex.js";
1818 import { LanceDbIndex } from "./LanceDbIndex.js";
19-import { getComputeDeleteAddRemove } from "./refreshIndex.js";
19+import { getComputeDeleteAddRemove, IndexLock } from "./refreshIndex.js";
2020 import {
2121 CodebaseIndex,
2222 IndexResultType,
@@ -643,11 +643,47 @@
643643 );
644644 }
645645
646+ /**
647+ * We want to prevent sqlite concurrent write errors
648+ * when there are 2 indexing happening from different windows.
649+ * We want the other window to wait until the first window's indexing finishes.
650+ * Incase the first window closes before indexing is finished,
651+ * we want to unlock the IndexLock by checking the last timestamp.
652+ */
653+ private async *waitForDBIndex(): AsyncGenerator<IndexingProgressUpdate> {
654+ let foundLock = await IndexLock.isLocked();
655+ while (foundLock?.locked) {
656+ if ((Date.now() - foundLock.timestamp) / 1000 > 10) {
657+ console.log(`${foundLock.dirs} is not being indexed... unlocking`);
658+ await IndexLock.unlock();
659+ break;
660+ }
661+ console.log(`indexing ${foundLock.dirs}`);
662+ yield {
663+ progress: 0,
664+ desc: "",
665+ status: "waiting",
666+ };
667+ await new Promise((resolve) => setTimeout(resolve, 1000));
668+ foundLock = await IndexLock.isLocked();
669+ }
670+ }
671+
646672 public async refreshCodebaseIndex(paths: string[]) {
647673 if (this.indexingCancellationController) {
648674 this.indexingCancellationController.abort();
649675 }
650676 this.indexingCancellationController = new AbortController();
677+ for await (const update of this.waitForDBIndex()) {
678+ this.updateProgress(update);
679+ }
680+
681+ await IndexLock.lock(paths.join(", ")); // acquire the index lock to prevent multiple windows to begin indexing
682+ const indexLockTimestampUpdateInterval = setInterval(
683+ () => void IndexLock.updateTimestamp(),
684+ 5_000,
685+ );
686+
651687 try {
652688 for await (const update of this.refreshDirs(
653689 paths,
@@ -664,6 +700,9 @@
664700 await this.handleIndexingError(e);
665701 }
666702
703+ clearInterval(indexLockTimestampUpdateInterval); // interval will also be cleared when window closes before indexing is finished
704+ await IndexLock.unlock();
705+
667706 // Directly refresh submenu items
668707 if (this.messenger) {
669708 this.messenger.send("refreshSubmenuItems", {
modified core/indexing/refreshIndex.ts +54 −0
@@ -45,6 +45,16 @@
4545 artifactId STRING NOT NULL
4646 )`,
4747 );
48+
49+ await db.exec(
50+ `CREATE TABLE IF NOT EXISTS indexing_lock (
51+ id INTEGER PRIMARY KEY AUTOINCREMENT,
52+ locked BOOLEAN NOT NULL,
53+ timestamp INTEGER NOT NULL,
54+ dirs STRING NOT NULL
55+ )`,
56+ );
57+
4858 // Delete duplicate rows from tag_catalog
4959 await db.exec(`
5060 DELETE FROM tag_catalog
@@ -532,3 +542,47 @@
532542 export function truncateSqliteLikePattern(input: string, safety: number = 100) {
533543 return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH - safety);
534544 }
545+
546+export class IndexLock {
547+ private static getLockTableName() {
548+ return "indexing_lock";
549+ }
550+
551+ static async isLocked(): Promise<
552+ { locked: boolean; dirs: string; timestamp: number } | undefined | undefined
553+ > {
554+ const db = await SqliteDb.get();
555+ const row = (await db.get(
556+ `SELECT locked, dirs, timestamp FROM ${IndexLock.getLockTableName()} WHERE locked = ?`,
557+ true,
558+ )) as { locked: boolean; dirs: string; timestamp: number } | undefined;
559+ return row;
560+ }
561+
562+ static async lock(dirs: string) {
563+ const db = await SqliteDb.get();
564+ await db.run(
565+ `INSERT INTO ${IndexLock.getLockTableName()} (locked, timestamp, dirs) VALUES (?, ?, ?)`,
566+ true,
567+ Date.now(),
568+ dirs,
569+ );
570+ }
571+
572+ static async updateTimestamp() {
573+ const db = await SqliteDb.get();
574+ await db.run(
575+ `UPDATE ${IndexLock.getLockTableName()} SET timestamp = ? where locked = ?`,
576+ Date.now(),
577+ true,
578+ );
579+ }
580+
581+ static async unlock() {
582+ const db = await SqliteDb.get();
583+ await db.run(
584+ `DELETE FROM ${IndexLock.getLockTableName()} WHERE locked = ?`,
585+ true,
586+ );
587+ }
588+}
modified gui/src/pages/config/IndexingProgress/IndexingProgress.tsx +1 −1
@@ -111,7 +111,7 @@
111111 <div className="mt-4 flex flex-col">
112112 <div className="mb-0 flex justify-between text-sm">
113113 <IndexingProgressTitleText update={update} />
114- {update.status !== "loading" && (
114+ {!["loading", "waiting"].includes(update.status) && (
115115 <IndexingProgressIndicator update={update} />
116116 )}
117117 </div>
modified gui/src/pages/config/IndexingProgress/IndexingProgressIndicator.tsx +1 −0
@@ -13,6 +13,7 @@
1313 const STATUS_TO_ICON: Record<IndexingProgressUpdate["status"], any> = {
1414 disabled: null,
1515 loading: null,
16+ waiting: null,
1617 indexing: ArrowPathIcon,
1718 paused: PauseCircleIcon,
1819 done: CheckCircleIcon,
modified gui/src/pages/config/IndexingProgress/IndexingProgressSubtext.tsx +1 −0
@@ -11,6 +11,7 @@
1111 > = {
1212 done: "Click to re-index",
1313 loading: "",
14+ waiting: "",
1415 indexing: "Click to pause",
1516 paused: "Click to resume",
1617 failed: "Click to retry",
modified gui/src/pages/config/IndexingProgress/IndexingProgressTitleText.tsx +1 −0
@@ -8,6 +8,7 @@
88 const STATUS_TO_TEXT: Record<IndexingProgressUpdate["status"], string> = {
99 done: "Indexing complete",
1010 loading: "Initializing",
11+ waiting: "Indexing other workspace",
1112 indexing: "Indexing in-progress",
1213 paused: "Indexing paused",
1314 failed: "Indexing failed",