Merge pull request #6131 from shssoichiro/refresh-codebase-index

fix: refresh codebase index on config change
This commit is contained in:
Dallin Romney 2025-07-16 20:50:55 +02:00 • committed by GitHub
commit c41be7ee41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 430 additions and 30 deletions

View file

@ -1,3 +1,5 @@
/* eslint-disable max-lines-per-function */
/* lint is not useful for test classes */
import { jest } from "@jest/globals";
import { execSync } from "node:child_process";
import fs from "node:fs";
@ -14,8 +16,11 @@ import {
} from "../test/testDir.js";
import { getIndexSqlitePath } from "../util/paths.js";
import { ConfigResult } from "@continuedev/config-yaml";
import CodebaseContextProvider from "../context/providers/CodebaseContextProvider.js";
import { ContinueConfig } from "../index.js";
import { localPathToUri } from "../util/pathToUri.js";
import { CodebaseIndexer, PauseToken } from "./CodebaseIndexer.js";
import { CodebaseIndexer } from "./CodebaseIndexer.js";
import { getComputeDeleteAddRemove } from "./refreshIndex.js";
import { TestCodebaseIndex } from "./TestCodebaseIndex.js";
import { CodebaseIndex } from "./types.js";
@ -57,6 +62,17 @@ class TestCodebaseIndexer extends CodebaseIndexer {
protected async getIndexesToBuild(): Promise<CodebaseIndex[]> {
return [new TestCodebaseIndex()];
}
// Add public methods to test private methods
public testHasCodebaseContextProvider() {
return (this as any).hasCodebaseContextProvider();
}
public async testHandleConfigUpdate(
configResult: ConfigResult<ContinueConfig>,
) {
return (this as any).handleConfigUpdate({ config: configResult.config });
}
}
// Create a mock messenger type that doesn't require actual protocol imports
@ -71,8 +87,6 @@ type MockMessengerType = {
// These are more like integration tests, whereas we should separately test
// the individual CodebaseIndex classes
describe("CodebaseIndexer", () => {
const pauseToken = new PauseToken(false);
// Replace mockProgressReporter with mockMessenger
const mockMessenger: MockMessengerType = {
send: jest.fn(),
@ -82,13 +96,8 @@ describe("CodebaseIndexer", () => {
onError: jest.fn(),
};
const codebaseIndexer = new TestCodebaseIndexer(
testConfigHandler,
testIde,
mockMessenger as any,
false,
);
const testIndex = new TestCodebaseIndex();
let codebaseIndexer: TestCodebaseIndexer;
let testIndex: TestCodebaseIndex;
beforeAll(async () => {
tearDownTestDir();
@ -99,6 +108,15 @@ describe("CodebaseIndexer", () => {
cwd: TEST_DIR_PATH,
});
execSync('git config user.name "Test"', { cwd: TEST_DIR_PATH });
codebaseIndexer = new TestCodebaseIndexer(
testConfigHandler,
testIde,
mockMessenger as any,
false,
);
await codebaseIndexer.initPromise;
testIndex = new TestCodebaseIndex();
});
afterAll(async () => {
@ -161,6 +179,7 @@ describe("CodebaseIndexer", () => {
}
test("should index test folder without problem", async () => {
walkDirCache.invalidate();
addToTestDir([
["test.ts", TEST_TS],
["py/main.py", TEST_PY],
@ -403,4 +422,270 @@ describe("CodebaseIndexer", () => {
expect(codebaseIndexer.currentIndexingState).toEqual(testState);
});
});
// New describe block for testing handleConfigUpdate functionality
describe("handleConfigUpdate functionality", () => {
let testIndexer: TestCodebaseIndexer;
let mockRefreshCodebaseIndex: jest.MockedFunction<any>;
let mockGetWorkspaceDirs: jest.MockedFunction<any>;
beforeEach(() => {
testIndexer = new TestCodebaseIndexer(
testConfigHandler,
testIde,
mockMessenger as any,
false,
);
// Mock the refreshCodebaseIndex method to avoid actual indexing
mockRefreshCodebaseIndex = jest
.spyOn(testIndexer, "refreshCodebaseIndex")
.mockImplementation(async () => {});
// Mock getWorkspaceDirs to return test directories
mockGetWorkspaceDirs = jest
.spyOn(testIde, "getWorkspaceDirs")
.mockResolvedValue(["/test/workspace"]);
});
afterEach(() => {
jest.clearAllMocks();
});
describe("hasCodebaseContextProvider", () => {
test("should return true when codebase context provider is present", () => {
// Set up config with codebase context provider
(testIndexer as any).config = {
contextProviders: [
{
description: {
title: CodebaseContextProvider.description.title,
},
},
],
};
const result = testIndexer.testHasCodebaseContextProvider();
expect(result).toBe(true);
});
test("should return false when no context providers are configured", () => {
(testIndexer as any).config = {
contextProviders: undefined,
};
const result = testIndexer.testHasCodebaseContextProvider();
expect(result).toBe(false);
});
test("should return false when context providers exist but no codebase provider", () => {
(testIndexer as any).config = {
contextProviders: [
{
description: {
title: "SomeOtherProvider",
},
},
],
};
const result = testIndexer.testHasCodebaseContextProvider();
expect(result).toBe(false);
});
test("should return false when context providers is empty array", () => {
(testIndexer as any).config = {
contextProviders: [],
};
const result = testIndexer.testHasCodebaseContextProvider();
expect(result).toBe(false);
});
});
describe("handleConfigUpdate", () => {
test("should return early when newConfig is null", async () => {
const configResult: ConfigResult<ContinueConfig> = {
config: null as any,
errors: [],
configLoadInterrupted: false,
};
await testIndexer.testHandleConfigUpdate(configResult);
// These get called once on init, so we want them to not get called again
expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(1);
expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(1);
});
test("should return early when newConfig is undefined", async () => {
const configResult: ConfigResult<ContinueConfig> = {
config: undefined as any,
errors: [],
configLoadInterrupted: false,
};
await testIndexer.testHandleConfigUpdate(configResult);
// These get called once on init, so we want them to not get called again
expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(1);
expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(1);
});
test("should return early when no codebase context provider is present", async () => {
const configResult: ConfigResult<ContinueConfig> = {
config: {
contextProviders: [
{
description: {
title: "SomeOtherProvider",
},
},
],
selectedModelByRole: {
embed: {
model: "test-model",
provider: "test-provider",
},
},
} as unknown as ContinueConfig,
errors: [],
configLoadInterrupted: false,
};
await testIndexer.testHandleConfigUpdate(configResult);
// These get called once on init, so we want them to not get called again
expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(1);
expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(1);
});
test("should return early when no embed model is configured", async () => {
const configResult: ConfigResult<ContinueConfig> = {
config: {
contextProviders: [
{
description: {
title: CodebaseContextProvider.description.title,
},
},
],
selectedModelByRole: {
embed: undefined,
},
} as unknown as ContinueConfig,
errors: [],
configLoadInterrupted: false,
};
await testIndexer.testHandleConfigUpdate(configResult);
// These get called once on init, so we want them to not get called again
expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(1);
expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(1);
});
test("should call refreshCodebaseIndex when all conditions are met", async () => {
const configResult: ConfigResult<ContinueConfig> = {
config: {
contextProviders: [
{
description: {
title: CodebaseContextProvider.description.title,
},
},
],
selectedModelByRole: {
embed: {
model: "test-model",
provider: "test-provider",
},
},
} as unknown as ContinueConfig,
errors: [],
configLoadInterrupted: false,
};
await testIndexer.testHandleConfigUpdate(configResult);
// These get called once on init, and we want them to get called again
expect(mockGetWorkspaceDirs).toHaveBeenCalledTimes(2);
expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(2);
expect(mockRefreshCodebaseIndex).toHaveBeenCalledWith([
"/test/workspace",
]);
});
test("should set config property before checking conditions", async () => {
const testConfig = {
contextProviders: [
{
description: {
title: CodebaseContextProvider.description.title,
},
},
],
selectedModelByRole: {
embed: {
model: "test-model",
provider: "test-provider",
},
},
} as unknown as ContinueConfig;
const configResult: ConfigResult<ContinueConfig> = {
config: testConfig,
errors: [],
configLoadInterrupted: false,
};
await testIndexer.testHandleConfigUpdate(configResult);
// Verify that the config was set
expect((testIndexer as any).config).toBe(testConfig);
// These get called once on init, and we want them to get called again
expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(2);
});
test("should handle multiple context providers correctly", async () => {
const configResult: ConfigResult<ContinueConfig> = {
config: {
contextProviders: [
{
description: {
title: "SomeOtherProvider",
},
},
{
description: {
title: CodebaseContextProvider.description.title,
},
},
{
description: {
title: "AnotherProvider",
},
},
],
selectedModelByRole: {
embed: {
model: "test-model",
provider: "test-provider",
},
},
} as unknown as ContinueConfig,
errors: [],
configLoadInterrupted: false,
};
await testIndexer.testHandleConfigUpdate(configResult);
// These get called once on init, and we want them to get called again
expect(mockRefreshCodebaseIndex).toHaveBeenCalledTimes(2);
expect(mockRefreshCodebaseIndex).toHaveBeenCalledWith([
"/test/workspace",
]);
});
});
});
});

View file

@ -1,7 +1,12 @@
import * as fs from "fs/promises";
import { ConfigHandler } from "../config/ConfigHandler.js";
import { IDE, IndexingProgressUpdate, IndexTag } from "../index.js";
import {
ContinueConfig,
IDE,
IndexingProgressUpdate,
IndexTag,
} from "../index.js";
import type { FromCoreProtocol, ToCoreProtocol } from "../protocol";
import type { IMessenger } from "../protocol/messenger";
import { extractMinimalStackTraceInfo } from "../util/extractMinimalStackTraceInfo.js";
@ -9,11 +14,14 @@ import { getIndexSqlitePath, getLanceDbPath } from "../util/paths.js";
import { Telemetry } from "../util/posthog.js";
import { findUriInDirs, getUriPathBasename } from "../util/uri.js";
import { ConfigResult } from "@continuedev/config-yaml";
import CodebaseContextProvider from "../context/providers/CodebaseContextProvider.js";
import { ContinueServerClient } from "../continueServer/stubs/client";
import { LLMError } from "../llm/index.js";
import { getRootCause } from "../util/errors.js";
import { ChunkCodebaseIndex } from "./chunk/ChunkCodebaseIndex.js";
import { CodeSnippetsCodebaseIndex } from "./CodeSnippetsIndex.js";
import { embedModelsAreEqual } from "./docs/DocsService.js";
import { FullTextSearchCodebaseIndex } from "./FullTextSearchCodebaseIndex.js";
import { LanceDbIndex } from "./LanceDbIndex.js";
import { getComputeDeleteAddRemove, IndexLock } from "./refreshIndex.js";
@ -44,6 +52,10 @@ export class CodebaseIndexer {
* - To make as few requests as possible to the embeddings providers
*/
filesPerBatch = 200;
// We normally allow this to run in the background,
// and only need to `await` it for tests.
public initPromise: Promise<void>;
private config!: ContinueConfig;
private indexingCancellationController: AbortController | undefined;
private codebaseIndexingState: IndexingProgressUpdate;
private readonly pauseToken: PauseToken;
@ -83,6 +95,17 @@ export class CodebaseIndexer {
// Initialize pause token
this.pauseToken = new PauseToken(initialPaused);
this.initPromise = this.init(configHandler);
}
// Initialization - load config and attach config listener
private async init(configHandler: ConfigHandler) {
const result = await configHandler.loadConfig();
await this.handleConfigUpdate(result);
configHandler.onConfigUpdate(
this.handleConfigUpdate.bind(this) as (arg: any) => void,
);
}
set paused(value: boolean) {
@ -192,7 +215,7 @@ export class CodebaseIndexer {
workspaceDirs: string[],
): Promise<void> {
if (this.pauseToken.paused) {
// NOTE: by returning here, there is a chance that while paused a file is modified and
// FIXME: by returning here, there is a chance that while paused a file is modified and
// then after unpausing the file is not reindexed
return;
}
@ -763,4 +786,49 @@ export class CodebaseIndexer {
public get currentIndexingState(): IndexingProgressUpdate {
return this.codebaseIndexingState;
}
private hasCodebaseContextProvider() {
return !!this.config.contextProviders?.some(
(provider) =>
provider.description.title ===
CodebaseContextProvider.description.title,
);
}
private isIndexingConfigSame(
config1: ContinueConfig | undefined,
config2: ContinueConfig,
) {
return embedModelsAreEqual(
config1?.selectedModelByRole.embed,
config2.selectedModelByRole.embed,
);
}
private async handleConfigUpdate({
config: newConfig,
}: ConfigResult<ContinueConfig>) {
if (newConfig) {
const needsReindex = !this.isIndexingConfigSame(this.config, newConfig);
this.config = newConfig; // IMPORTANT - need to set up top, other methods below use this without passing it in
// No point in indexing if no codebase context provider
const hasCodebaseContextProvider = this.hasCodebaseContextProvider();
if (!hasCodebaseContextProvider) {
return;
}
// Skip codebase indexing if not supported
// No warning message here because would show on ANY config update
if (!this.config.selectedModelByRole.embed) {
return;
}
if (needsReindex) {
const dirs = await this.ide.getWorkspaceDirs();
await this.refreshCodebaseIndex(dirs);
}
}
}
}

View file

@ -69,7 +69,7 @@ const markFailedInGlobalContext = (siteIndexingConfig: SiteIndexingConfig) => {
const globalContext = new GlobalContext();
const failedDocs = globalContext.get("failedDocs") ?? [];
const newFailedDocs = failedDocs.filter(
(d) => !siteIndexingConfigsAreEqual(siteIndexingConfig, d),
(d) => !docConfigsAreEqual(siteIndexingConfig, d),
);
newFailedDocs.push(siteIndexingConfig);
globalContext.update("failedDocs", newFailedDocs);
@ -81,7 +81,7 @@ const removeFromFailedGlobalContext = (
const globalContext = new GlobalContext();
const failedDocs = globalContext.get("failedDocs") ?? [];
const newFailedDocs = failedDocs.filter(
(d) => !siteIndexingConfigsAreEqual(siteIndexingConfig, d),
(d) => !docConfigsAreEqual(siteIndexingConfig, d),
);
globalContext.update("failedDocs", newFailedDocs);
};
@ -89,32 +89,69 @@ const removeFromFailedGlobalContext = (
const hasIndexingFailed = (siteIndexingConfig: SiteIndexingConfig) => {
const globalContext = new GlobalContext();
const failedDocs = globalContext.get("failedDocs") ?? [];
return failedDocs.find((d) =>
siteIndexingConfigsAreEqual(siteIndexingConfig, d),
return failedDocs.find((d) => docConfigsAreEqual(siteIndexingConfig, d));
};
export function embedModelsAreEqual(
llm1: ILLM | null | undefined,
llm2: ILLM | null | undefined,
): boolean {
return (
llm1?.underlyingProviderName === llm2?.underlyingProviderName &&
llm1?.title === llm2?.title &&
llm1?.maxEmbeddingChunkSize === llm2?.maxEmbeddingChunkSize
);
}
const docConfigsAreEqual = (
siteConfig1: SiteIndexingConfig,
siteConfig2: SiteIndexingConfig,
) => {
return (
siteConfig1.faviconUrl === siteConfig2.faviconUrl &&
siteConfig1.title === siteConfig2.title &&
docConfigsAreEqualExceptTitleAndFavicon(siteConfig1, siteConfig2)
);
};
const docConfigsAreEqualExceptTitleAndFavicon = (
siteConfig1: SiteIndexingConfig,
siteConfig2: SiteIndexingConfig,
) => {
return (
siteConfig1.startUrl === siteConfig2.startUrl &&
siteConfig1.maxDepth === siteConfig2.maxDepth &&
siteConfig1.useLocalCrawling === siteConfig2.useLocalCrawling
);
};
const siteIndexingConfigsAreEqual = (
config1: SiteIndexingConfig,
config2: SiteIndexingConfig,
siteConfig1: SiteIndexingConfig,
siteConfig2: SiteIndexingConfig,
contConfig1: ContinueConfig | undefined,
contConfig2: ContinueConfig,
) => {
return (
config1.startUrl === config2.startUrl &&
config1.faviconUrl === config2.faviconUrl &&
config1.title === config2.title &&
config1.maxDepth === config2.maxDepth &&
config1.useLocalCrawling === config2.useLocalCrawling
docConfigsAreEqual(siteConfig1, siteConfig2) &&
embedModelsAreEqual(
contConfig1?.selectedModelByRole.embed,
contConfig2.selectedModelByRole.embed,
)
);
};
const siteIndexingConfigsAreEqualExceptTitleAndFavicon = (
config1: SiteIndexingConfig,
config2: SiteIndexingConfig,
siteConfig1: SiteIndexingConfig,
siteConfig2: SiteIndexingConfig,
contConfig1: ContinueConfig | undefined,
contConfig2: ContinueConfig,
) => {
return (
config1.startUrl === config2.startUrl &&
config1.maxDepth === config2.maxDepth &&
config1.useLocalCrawling === config2.useLocalCrawling
docConfigsAreEqualExceptTitleAndFavicon(siteConfig1, siteConfig2) &&
embedModelsAreEqual(
contConfig1?.selectedModelByRole.embed,
contConfig2.selectedModelByRole.embed,
)
);
};
@ -1037,12 +1074,22 @@ export default class DocsService {
);
// TODO: Changes to the docs config made while Continue isn't running won't be caught
if (oldConfigDoc && !siteIndexingConfigsAreEqual(oldConfigDoc, doc)) {
if (
oldConfigDoc &&
!siteIndexingConfigsAreEqual(
oldConfigDoc,
doc,
oldConfig,
newConfig,
)
) {
// When only the title or faviconUrl changed, Update the sqlite metadate instead of reindexing
if (
siteIndexingConfigsAreEqualExceptTitleAndFavicon(
oldConfigDoc,
doc,
oldConfig,
newConfig,
)
) {
await this.updateMetadataInSqlite(doc);
@ -1262,7 +1309,7 @@ export default class DocsService {
// Handles the case where a user has manually added the doc to config.json
// so it already exists in the file
const doesEquivalentDocExist = this.config.docs?.some((doc) =>
siteIndexingConfigsAreEqual(doc, siteIndexingConfig),
docConfigsAreEqual(doc, siteIndexingConfig),
);
if (!doesEquivalentDocExist) {