memorix icon indicating copy to clipboard operation
memorix copied to clipboard

reports "No API key" even when MEMORIX_EMBEDDING_API_KEY is configured in `.env`

Open FurryWolfX opened this issue 8 months ago • 5 comments

Description

When MEMORIX_EMBEDDING_API_KEY is set in ~/.memorix/.env, running memorix status incorrectly reports:

Failed to init API embedding: No API key for embedding.

The API key is valid and the configuration is correct — the embedding provider works after restarting the background service. However, memorix status always fails to detect the key.

Expected Behavior

memorix status should correctly detect and display the embedding provider status when API key is configured in .env.

Actual Behavior

memorix status reports "No API key" and falls back to BM25 fulltext, even though:

  • The API key is valid (verified with direct curl to SiliconFlow API)
  • The key is set in ~/.memorix/.env as MEMORIX_EMBEDDING_API_KEY
  • The background service starts correctly with the same configuration

Root Cause

In src/cli/commands/status.ts, the execution order is incorrect:

// Line 62: getEmbeddingProvider() is called FIRST
const provider = await getEmbeddingProvider();
if (provider) { ... }

// ... 35 lines later ...

// Line 97: loadDotenv() is called AFTER getEmbeddingProvider()
loadDotenv(project.rootPath);

getEmbeddingProvider() (line 62) is called before loadDotenv() (line 97), so when the embedding provider tries to read MEMORIX_EMBEDDING_API_KEY from process.env, the .env file hasn't been loaded yet.

Steps to Reproduce

  1. Add MEMORIX_EMBEDDING_API_KEY=your-key to ~/.memorix/.env
  2. Ensure embedding.provider: api is set in ~/.memorix/memorix.yml
  3. Run memorix status
  4. Observe: "Failed to init API embedding: No API key"

Environment

  • OS: macOS
  • Node.js: v24.12.0 (via nvm)
  • Memorix version: latest (global npm install)

Fix

Move loadDotenv(project.rootPath) to before the getEmbeddingProvider() call in src/cli/commands/status.ts:

// Load dotenv BEFORE checking embedding provider
const { loadDotenv } = await import('../../config/dotenv-loader.js');
loadDotenv(project.rootPath);

// Then check embedding status
const provider = await getEmbeddingProvider();

FurryWolfX avatar Apr 04 '26 11:04 FurryWolfX

Thanks for the clear report — the root cause was exactly the dotenv load order.

Fixed in #47:

  • memorix status now loads .env before embedding provider initialization
  • the same ordering issue was also fixed in doctor and the TUI health path
  • added regression tests for project .env, user .env, and precedence behavior

This is now merged into main. If you update and still see the issue, feel free to reopen with your exact config path / command output.


感谢你把根因写得这么清楚,问题确实就是 dotenv 的加载顺序。

已经通过 #47 修复:

  • memorix status 现在会在初始化 embedding provider 之前先加载 .env
  • 同类顺序问题也一起修到了 doctor 和 TUI health path
  • 还补了 project .env、user .env 以及优先级行为的回归测试

修复已经合入 main。如果你更新后仍然能复现,欢迎继续贴你的配置路径和命令输出。

AVIDS2 avatar Apr 04 '26 15:04 AVIDS2

Closing the loop here with an English-only summary: this was fixed in #47 and is already merged into main.

The fix loads .env before embedding-provider initialization in the affected CLI status/diagnostic paths, and also adds regression coverage for dotenv ordering so this does not drift back.

Thanks again for the precise report.

AVIDS2 avatar Apr 04 '26 16:04 AVIDS2

memorix dashboard is spitting out the same error @AVIDS2

anhchu-katalon avatar May 18 '26 06:05 anhchu-katalon

Thanks for the follow-up. I checked the dashboard path and this looks like a remaining standalone-dashboard gap, not the original memorix status fix failing.

The original #46 fix moved dotenv loading before embedding-provider initialization in status, doctor, and the TUI health path. However, the standalone memorix dashboard /api/stats path can still ask the embedding provider for status before the dashboard config route has loaded .env, so it may still surface the same "No API key" symptom.

I will carry this into the next patch so standalone dashboard loads the project/user dotenv before reading embedding status, matching the CLI diagnostics behavior.

Temporary workarounds:

  • restart any long-running Memorix/background process after changing .env
  • or export the embedding key in the shell that starts memorix dashboard
  • or use the control-plane dashboard after memorix background start if that path is already loading your project config correctly

Thanks for catching the dashboard-specific path.

AVIDS2 avatar Jun 03 '26 14:06 AVIDS2

Thanks again for the dashboard-specific follow-up. I tracked this separately from the original \memorix status\ fix and fixed the remaining standalone dashboard path for the upcoming 1.0.10 patch.

The dashboard now prepares project-scoped config before /api/stats\ reads embedding status:

  • loads project .env\ before embedding provider initialization
  • initializes project \memorix.yml\ for dashboard routes
  • clears project config when switching to a project whose root is unknown, so config does not bleed from the previous project
  • adds regression coverage for the standalone dashboard stats path

Validation on my side:

  • focused config/dashboard/LLM/TUI tests passed

  • pm run lint\ passed

  • pm run build\ passed
  • full
    pm test\ exited successfully

I will update the release notes when 1.0.10 is published.

AVIDS2 avatar Jun 07 '26 15:06 AVIDS2