-
Notifications
You must be signed in to change notification settings - Fork 2
fix: preserve global config during transient init misses #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -583,6 +583,21 @@ function buildConfig(fileConfig: OpenCodeMemConfig) { | |
| } | ||
|
|
||
| const _globalFileConfig = loadConfigFromPaths(CONFIG_FILES); | ||
| let lastKnownGlobalConfig = _globalFileConfig; | ||
|
|
||
| function isCurrentConfigFromLastKnownGlobal(): boolean { | ||
| if (Object.keys(lastKnownGlobalConfig).length === 0) return false; | ||
| const globalConfig = buildConfig(lastKnownGlobalConfig); | ||
| return ( | ||
| CONFIG.embeddingModel === globalConfig.embeddingModel && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| CONFIG.embeddingDimensions === globalConfig.embeddingDimensions && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| CONFIG.embeddingApiUrl === globalConfig.embeddingApiUrl && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| CONFIG.embeddingApiKey === globalConfig.embeddingApiKey && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| CONFIG.opencodeProvider === globalConfig.opencodeProvider && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| CONFIG.opencodeModel === globalConfig.opencodeModel && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| CONFIG.autoCaptureEnabled === globalConfig.autoCaptureEnabled | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ); | ||
| } | ||
|
Comment on lines
+588
to
+600
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| /** | ||
| * Global CONFIG singleton — resolves from opencode-mem0.json and env. | ||
|
|
@@ -617,6 +632,17 @@ export function initConfig(directory: string): void { | |
| ]; | ||
| const globalConfig = loadConfigFromPaths(CONFIG_FILES); | ||
| const projectConfig = loadConfigFromPaths(projectPaths); | ||
| if (Object.keys(globalConfig).length > 0) { | ||
| lastKnownGlobalConfig = globalConfig; | ||
| } | ||
| if (Object.keys(globalConfig).length === 0 && Object.keys(projectConfig).length === 0) { | ||
| if (isCurrentConfigFromLastKnownGlobal()) { | ||
| Object.assign(CONFIG, buildConfig(lastKnownGlobalConfig)); | ||
| } else { | ||
| Object.assign(CONFIG, buildConfig({})); | ||
| } | ||
| return; | ||
| } | ||
| const merged = deepMerge(globalConfig, projectConfig); | ||
| Object.assign(CONFIG, buildConfig(merged)); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,4 +81,24 @@ describe("project-scoped config resolution", () => { | |
| expect(CONFIG.autoCaptureEnabled).toBe(true); // default value | ||
| expect(CONFIG.opencodeProvider).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("uses the last known global config when config paths are temporarily unavailable", () => { | ||
| (globalThis as any).__mockFs.existsSync = (p: unknown) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const path = String(p); | ||
| return path.includes(".config/opencode/opencode-mem0"); | ||
| }; | ||
| (globalThis as any).__mockFs.readFileSync = () => | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| JSON.stringify({ | ||
| embeddingModel: "text-embedding-3-small", | ||
| embeddingDimensions: 1536, | ||
| }); | ||
| initConfig("/some/project"); | ||
| expect(CONFIG.embeddingModel).toBe("text-embedding-3-small"); | ||
| expect(CONFIG.embeddingDimensions).toBe(1536); | ||
|
|
||
| (globalThis as any).__mockFs.existsSync = () => false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| initConfig("/some/project"); | ||
| expect(CONFIG.embeddingModel).toBe("text-embedding-3-small"); | ||
| expect(CONFIG.embeddingDimensions).toBe(1536); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function with high cyclomatic complexity can be hard to understand and
maintain. Cyclomatic complexity is a software metric that measures the number of
independent paths through a function. A higher cyclomatic complexity indicates
that the function has more decision points and is more complex.