Skip to content

fix: initConfig() silently resets CONFIG to defaults when global config file is transiently inaccessible #35

Description

@bob56621517

This issues is from AI agent !

Bug Description

initConfig() in src/config.ts:613-622 can silently reset the entire CONFIG to hardcoded defaults when loadConfigFromPaths(CONFIG_FILES) returns an empty object {}. This occurs when the global config file is transiently inaccessible (I/O contention, file lock, race condition at process startup).

Impact

When triggered, all configuration values are reset to hardcoded defaults:

  • embeddingModel"Xenova/nomic-embed-text-v1" (user had configured Ollama qwen3-embedding:0.6b)
  • embeddingDimensions768 (user had configured 1024)
  • embeddingApiUrlundefined (user had configured http://localhost:11434/v1)
  • embeddingApiKeyundefined (user had configured "ollama")

This causes the Web UI migration detection (/api/migration/detect) to report a false dimension mismatch, because it compares the (now-default) CONFIG values against shard metadata that was written with the correct user-configured values.

Root Cause

In initConfig() (line 613-622):

export function initConfig(directory: string): void {
  const globalConfig = loadConfigFromPaths(CONFIG_FILES);
  const projectConfig = loadConfigFromPaths(projectPaths);
  const merged = deepMerge(globalConfig, projectConfig);
  Object.assign(CONFIG, buildConfig(merged));  // ← BUG: buildConfig({}) fills ALL defaults
}

When globalConfig is {} and projectConfig is {}, buildConfig({}) rebuilds the entire config with every hardcoded default, then Object.assign overwrites the existing CORRECT CONFIG.

Reproduction

  1. Configure opencode-mem0 with Ollama embedding API (non-default model/dimensions)
  2. Start opencode — CONFIG initializes correctly at module load time (line 593)
  3. Open a new session in a project without .opencode/opencode-mem0.jsonc
  4. initConfig(projectDir) is called
  5. If loadConfigFromPaths(CONFIG_FILES) returns {} (transient I/O failure), CONFIG is reset to defaults
  6. Web UI at http://localhost:4747 shows: 模型不匹配:配置使用 768D (Xenova/nomic-embed-text-v1),但2 个分片具有不同的维度。

Proposed Fix

Add a guard in initConfig() — when both global and project configs are empty, skip the CONFIG rebuild entirely:

export function initConfig(directory: string): void {
  const projectPaths = [...];
  const globalConfig = loadConfigFromPaths(CONFIG_FILES);
  const projectConfig = loadConfigFromPaths(projectPaths);

  // Guard: if neither config file was found, preserve existing CONFIG values.
  // Rebuilding from defaults here would silently break user-configured
  // embedding model, dimensions, and API endpoint.
  if (Object.keys(globalConfig).length === 0 && Object.keys(projectConfig).length === 0) {
    return;
  }

  const merged = deepMerge(globalConfig, projectConfig);
  Object.assign(CONFIG, buildConfig(merged));
}

Environment

  • opencode-mem0 v2.16.2
  • Node.js v18+
  • Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions