Skip to content

Commit 38a0cf6

Browse files
committed
refactor(code-index): use immutable configuration snapshots
1 parent 1803c01 commit 38a0cf6

3 files changed

Lines changed: 405 additions & 391 deletions

File tree

‎src/services/code-index/__tests__/config-manager.spec.ts‎

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,34 @@ describe("CodeIndexConfigManager", () => {
8181
})
8282
})
8383

84+
describe("model dimension parsing", () => {
85+
it.each([
86+
{ raw: undefined, expected: undefined, warns: false },
87+
{ raw: null, expected: undefined, warns: false },
88+
{ raw: 1536, expected: 1536, warns: false },
89+
{ raw: "768", expected: 768, warns: false },
90+
{ raw: 0, expected: undefined, warns: true },
91+
{ raw: -1, expected: undefined, warns: true },
92+
{ raw: "invalid", expected: undefined, warns: true },
93+
{ raw: NaN, expected: undefined, warns: true },
94+
])("parses $raw with warning=$warns", async ({ raw, expected, warns }) => {
95+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
96+
try {
97+
mockContextProxy.getGlobalState.mockReturnValue({ codebaseIndexEmbedderModelDimension: raw })
98+
const { currentConfig } = await configManager.loadConfiguration()
99+
expect(currentConfig.modelDimension).toBe(expected)
100+
expect(warn).toHaveBeenCalledTimes(warns ? 1 : 0)
101+
if (warns) {
102+
expect(warn).toHaveBeenCalledWith(
103+
`Invalid codebaseIndexEmbedderModelDimension value: ${raw}. Must be a positive number.`,
104+
)
105+
}
106+
} finally {
107+
warn.mockRestore()
108+
}
109+
})
110+
})
111+
84112
describe("isFeatureEnabled", () => {
85113
it("should return false when codebaseIndexEnabled is false", async () => {
86114
mockContextProxy.getGlobalState.mockReturnValue({
@@ -192,6 +220,7 @@ describe("CodeIndexConfigManager", () => {
192220
qdrantUrl: "http://localhost:6333",
193221
qdrantApiKey: "",
194222
searchMinScore: 0.4,
223+
searchMaxResults: 50,
195224
})
196225
expect(result.requiresRestart).toBe(false)
197226
})
@@ -1773,6 +1802,69 @@ describe("CodeIndexConfigManager", () => {
17731802
})
17741803

17751804
describe("getConfig", () => {
1805+
it("publishes frozen configuration and options for every provider", async () => {
1806+
mockContextProxy.getGlobalState.mockReturnValue({
1807+
codebaseIndexEnabled: true,
1808+
codebaseIndexEmbedderProvider: providerIdentifiers.openai,
1809+
codebaseIndexQdrantUrl: "http://localhost:6333",
1810+
codebaseIndexOpenAiCompatibleBaseUrl: "https://example.com/v1",
1811+
codebaseIndexSearchMaxResults: 23,
1812+
})
1813+
mockContextProxy.getSecret.mockReturnValue("test-key")
1814+
1815+
const { currentConfig } = await configManager.loadConfiguration()
1816+
expect(currentConfig).toEqual(configManager.getConfig())
1817+
expect(currentConfig.searchMaxResults).toBe(23)
1818+
expect(Object.isFrozen(currentConfig)).toBe(true)
1819+
expect(Reflect.set(currentConfig, "modelId", "changed")).toBe(false)
1820+
for (const options of [
1821+
currentConfig.openAiOptions,
1822+
currentConfig.ollamaOptions,
1823+
currentConfig.openAiCompatibleOptions,
1824+
currentConfig.geminiOptions,
1825+
currentConfig.mistralOptions,
1826+
currentConfig.vercelAiGatewayOptions,
1827+
currentConfig.bedrockOptions,
1828+
currentConfig.openRouterOptions,
1829+
]) {
1830+
expect(options).toBeDefined()
1831+
if (!options) throw new Error("Expected provider options")
1832+
expect(Object.isFrozen(options)).toBe(true)
1833+
for (const key of Object.keys(options)) {
1834+
expect(Reflect.set(options, key, "changed")).toBe(false)
1835+
}
1836+
}
1837+
expect(configManager.getConfig()).toEqual(currentConfig)
1838+
})
1839+
1840+
it("preserves previously returned snapshots across reloads", async () => {
1841+
mockContextProxy.getSecret.mockReturnValue("old-key")
1842+
const { currentConfig: previous } = await configManager.loadConfiguration()
1843+
mockContextProxy.getSecret.mockReturnValue("new-key")
1844+
const { currentConfig: current, configSnapshot } = await configManager.loadConfiguration()
1845+
1846+
expect(previous.openAiOptions?.openAiNativeApiKey).toBe("old-key")
1847+
expect(current.openAiOptions?.openAiNativeApiKey).toBe("new-key")
1848+
expect(current.openAiOptions).not.toBe(previous.openAiOptions)
1849+
expect(configSnapshot.openAiKey).toBe("old-key")
1850+
})
1851+
1852+
it("keeps the current snapshot when building the next snapshot fails", async () => {
1853+
const previous = configManager.getConfig()
1854+
mockContextProxy.getGlobalState.mockReturnValue({
1855+
codebaseIndexEnabled: true,
1856+
codebaseIndexQdrantUrl: "http://changed:6333",
1857+
get codebaseIndexEmbedderModelDimension(): number {
1858+
throw new Error("Invalid stored dimension")
1859+
},
1860+
})
1861+
1862+
await expect(configManager.loadConfiguration()).rejects.toThrow("Invalid stored dimension")
1863+
expect(configManager.getConfig()).toEqual(previous)
1864+
expect(configManager.isFeatureEnabled).toBe(false)
1865+
expect(configManager.getConfig().openAiOptions).toBe(previous.openAiOptions)
1866+
})
1867+
17761868
it("should return the current configuration", () => {
17771869
mockContextProxy.getGlobalState.mockReturnValue({
17781870
codebaseIndexEnabled: true,
@@ -2292,13 +2384,9 @@ describe("CodeIndexConfigManager", () => {
22922384
mockContextProxy.getSecret.mockReturnValue(undefined)
22932385

22942386
configManager = new CodeIndexConfigManager(mockContextProxy)
2295-
// The defensive `return false` is unreachable through the public API because
2296-
// EmbedderProvider is a closed union, so exercise it by forcing the private field
2297-
// to a value outside the union. `embedderProvider` is TypeScript `private`, not
2298-
// `#`-private, so a runtime property write reaches it. The double assertion is a
2299-
// last resort: the private field is not part of the public type surface, and
2300-
// `as any` is avoided to keep the file's no-explicit-any suppression budget flat.
2301-
;(configManager as unknown as Record<string, unknown>)["embedderProvider"] = "not-a-provider"
2387+
// Replace the snapshot with deliberately invalid runtime data to exercise
2388+
// the defensive branch without mutating the frozen production snapshot.
2389+
Reflect.set(configManager, "config", { ...configManager["config"], embedderProvider: "not-a-provider" })
23022390
expect(configManager.isConfigured()).toBe(false)
23032391
})
23042392
})

0 commit comments

Comments
 (0)