Skip to content

Commit 53c4755

Browse files
committed
fix: Initialize configuration on constructor to prevent false restart triggers
1 parent bfb41d5 commit 53c4755

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

src/services/code-index/config-manager.ts

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,18 @@ export class CodeIndexConfigManager {
1919
private qdrantApiKey?: string
2020
private searchMinScore?: number
2121

22-
constructor(private readonly contextProxy: ContextProxy) {}
22+
constructor(private readonly contextProxy: ContextProxy) {
23+
// Initialize with current configuration to avoid false restart triggers
24+
this._loadAndSetConfiguration()
25+
}
2326

2427
/**
25-
* Loads persisted configuration from globalState.
28+
* Private method that handles loading configuration from storage and updating instance variables.
29+
* This eliminates code duplication between initializeWithCurrentConfig() and loadConfiguration().
2630
*/
27-
public async loadConfiguration(): Promise<{
28-
configSnapshot: PreviousConfigSnapshot
29-
currentConfig: {
30-
isEnabled: boolean
31-
isConfigured: boolean
32-
embedderProvider: EmbedderProvider
33-
modelId?: string
34-
openAiOptions?: ApiHandlerOptions
35-
ollamaOptions?: ApiHandlerOptions
36-
qdrantUrl?: string
37-
qdrantApiKey?: string
38-
searchMinScore?: number
39-
}
40-
requiresRestart: boolean
41-
}> {
42-
const previousConfigSnapshot: PreviousConfigSnapshot = {
43-
enabled: this.isEnabled,
44-
configured: this.isConfigured(),
45-
embedderProvider: this.embedderProvider,
46-
modelId: this.modelId,
47-
openAiKey: this.openAiOptions?.openAiNativeApiKey,
48-
ollamaBaseUrl: this.ollamaOptions?.ollamaBaseUrl,
49-
qdrantUrl: this.qdrantUrl,
50-
qdrantApiKey: this.qdrantApiKey,
51-
}
52-
53-
let codebaseIndexConfig = this.contextProxy?.getGlobalState("codebaseIndexConfig") ?? {
31+
private _loadAndSetConfiguration(): void {
32+
// Load configuration from storage
33+
const codebaseIndexConfig = this.contextProxy?.getGlobalState("codebaseIndexConfig") ?? {
5434
codebaseIndexEnabled: false,
5535
codebaseIndexQdrantUrl: "http://localhost:6333",
5636
codebaseIndexSearchMinScore: 0.4,
@@ -70,6 +50,7 @@ export class CodeIndexConfigManager {
7050
const openAiKey = this.contextProxy?.getSecret("codeIndexOpenAiKey") ?? ""
7151
const qdrantApiKey = this.contextProxy?.getSecret("codeIndexQdrantApiKey") ?? ""
7252

53+
// Update instance variables with configuration
7354
this.isEnabled = codebaseIndexEnabled || false
7455
this.qdrantUrl = codebaseIndexQdrantUrl
7556
this.qdrantApiKey = qdrantApiKey ?? ""
@@ -82,6 +63,40 @@ export class CodeIndexConfigManager {
8263
this.ollamaOptions = {
8364
ollamaBaseUrl: codebaseIndexEmbedderBaseUrl,
8465
}
66+
}
67+
68+
/**
69+
* Loads persisted configuration from globalState.
70+
*/
71+
public async loadConfiguration(): Promise<{
72+
configSnapshot: PreviousConfigSnapshot
73+
currentConfig: {
74+
isEnabled: boolean
75+
isConfigured: boolean
76+
embedderProvider: EmbedderProvider
77+
modelId?: string
78+
openAiOptions?: ApiHandlerOptions
79+
ollamaOptions?: ApiHandlerOptions
80+
qdrantUrl?: string
81+
qdrantApiKey?: string
82+
searchMinScore?: number
83+
}
84+
requiresRestart: boolean
85+
}> {
86+
// Capture the ACTUAL previous state before loading new configuration
87+
const previousConfigSnapshot: PreviousConfigSnapshot = {
88+
enabled: this.isEnabled,
89+
configured: this.isConfigured(),
90+
embedderProvider: this.embedderProvider,
91+
modelId: this.modelId,
92+
openAiKey: this.openAiOptions?.openAiNativeApiKey ?? "",
93+
ollamaBaseUrl: this.ollamaOptions?.ollamaBaseUrl ?? "",
94+
qdrantUrl: this.qdrantUrl ?? "",
95+
qdrantApiKey: this.qdrantApiKey ?? "",
96+
}
97+
98+
// Load new configuration from storage and update instance variables
99+
this._loadAndSetConfiguration()
85100

86101
const requiresRestart = this.doesConfigChangeRequireRestart(previousConfigSnapshot)
87102

@@ -127,15 +142,15 @@ export class CodeIndexConfigManager {
127142
doesConfigChangeRequireRestart(prev: PreviousConfigSnapshot): boolean {
128143
const nowConfigured = this.isConfigured()
129144

130-
// Handle null/undefined values safely
145+
// Handle null/undefined values safely - use empty strings for consistency with loaded config
131146
const prevEnabled = prev?.enabled ?? false
132147
const prevConfigured = prev?.configured ?? false
133148
const prevProvider = prev?.embedderProvider ?? "openai"
134149
const prevModelId = prev?.modelId ?? undefined
135-
const prevOpenAiKey = prev?.openAiKey ?? undefined
136-
const prevOllamaBaseUrl = prev?.ollamaBaseUrl ?? undefined
137-
const prevQdrantUrl = prev?.qdrantUrl ?? undefined
138-
const prevQdrantApiKey = prev?.qdrantApiKey ?? undefined
150+
const prevOpenAiKey = prev?.openAiKey ?? ""
151+
const prevOllamaBaseUrl = prev?.ollamaBaseUrl ?? ""
152+
const prevQdrantUrl = prev?.qdrantUrl ?? ""
153+
const prevQdrantApiKey = prev?.qdrantApiKey ?? ""
139154

140155
// 1. Transition from disabled/unconfigured to enabled+configured
141156
if ((!prevEnabled || !prevConfigured) && this.isEnabled && nowConfigured) {
@@ -165,22 +180,22 @@ export class CodeIndexConfigManager {
165180

166181
// Authentication changes
167182
if (this.embedderProvider === "openai") {
168-
const currentOpenAiKey = this.openAiOptions?.openAiNativeApiKey ?? undefined
183+
const currentOpenAiKey = this.openAiOptions?.openAiNativeApiKey ?? ""
169184
if (prevOpenAiKey !== currentOpenAiKey) {
170185
return true
171186
}
172187
}
173188

174189
if (this.embedderProvider === "ollama") {
175-
const currentOllamaBaseUrl = this.ollamaOptions?.ollamaBaseUrl ?? undefined
190+
const currentOllamaBaseUrl = this.ollamaOptions?.ollamaBaseUrl ?? ""
176191
if (prevOllamaBaseUrl !== currentOllamaBaseUrl) {
177192
return true
178193
}
179194
}
180195

181196
// Qdrant configuration changes
182-
const currentQdrantUrl = this.qdrantUrl ?? undefined
183-
const currentQdrantApiKey = this.qdrantApiKey ?? undefined
197+
const currentQdrantUrl = this.qdrantUrl ?? ""
198+
const currentQdrantApiKey = this.qdrantApiKey ?? ""
184199

185200
if (prevQdrantUrl !== currentQdrantUrl || prevQdrantApiKey !== currentQdrantApiKey) {
186201
return true

0 commit comments

Comments
 (0)