Skip to content

fix test: jiti module cache mismatch causes auto-recall tests to silently fail#359

Merged
rwmjhb merged 1 commit intoCortexReach:masterfrom
jlin53882:fix/test-jiti-module-cache-conflict
Mar 27, 2026
Merged

fix test: jiti module cache mismatch causes auto-recall tests to silently fail#359
rwmjhb merged 1 commit intoCortexReach:masterfrom
jlin53882:fix/test-jiti-module-cache-conflict

Conversation

@jlin53882
Copy link
Contributor

Summary

Fixes 5 auto-recall tests in test/recall-text-cleanup.test.mjs that were permanently failing with output === undefined.

Root Cause

index.ts imports from ./src/retriever.js, while the test was mocking jiti("../src/retriever.ts"). jiti separates its module cache by file extension, so these are two different module instances. The mock on the .ts cache never reached the plugin (which uses the .js cache), causing the plugin to call the real embedding API with a fake API key → 401 error → hook silently returned undefined.

Fix

Changed test mock targets from .ts to .js to match index.ts's imports:

  • jiti("../src/retriever.ts")jiti("../src/retriever.js")
  • jiti("../src/embedder.ts")jiti("../src/embedder.js")

Test Results

Before: ✗ 5 tests FAIL (output=undefined)
After: ✔ 15/15 PASS

Related

@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@jlin53882
Copy link
Contributor Author

@jlin53882
Copy link
Contributor Author

Diagnostic Scripts and Test Results

Scripts Used (in order)

1. diagnostic4.mjs — Confirmed 401 error in real retrieve()

// Key finding: retrieve() mock NOT called (0 times), real embedding API called
// Result: [WARN] recall failed: Error: Embedding provider authentication failed (401 invalid_api_key)

2. diagnostic5.mjs — Confirmed jiti cache separation

// Result:
MemoryRetriever (ts) === MemoryRetriever (js): false   // different class
MemoryRetriever prototype (ts) === (js): false         // different prototype
createRetriever (ts) === createRetriever (js): false   // different factory functions

3. diagnostic6.mjs — Confirmed .ts and .js are different caches

// Result:
createRetriever from .ts: retrieve() called 1 times, returned 1 entries  // mock works
createRetriever from .js: retrieve() called 0 times, returned 0 entries  // mock doesn't work

4. fix-verify-a.mjs — Confirmed Plan A works

// Fix: mock createRetriever on .js module BEFORE importing index.ts
// Result:
Output: object (SUCCESS!)    not undefined
prependContext: <relevant-memories>...  ← correct content

5. diag-reassign.mjs — Confirmed ES module live binding works after import

// Result:
After importing index.ts:
reassignCalled: false   import happened, then reassign
  [mockCreateRetriever] called!   mock works
After plugin.register():
reassignCalled: true (should be true if reassign works)

Final Fix Applied

In test/recall-text-cleanup.test.mjs, changed module-level and test-body imports from .ts to .js to match index.ts:

// BEFORE (broken):
const retrieverModuleForMock = jiti("../src/retriever.ts");   // ← wrong cache
const embedderModuleForMock = jiti("../src/embedder.ts");     // ← wrong cache
// ...
const retrieverMod = jiti("../src/retriever.ts");            // ← wrong cache in test body
const embedderMod = jiti("../src/embedder.ts");              // ← wrong cache in test body

// AFTER (fixed):
const retrieverModuleForMock = jiti("../src/retriever.js");  // ← same cache as index.ts
const embedderModuleForMock = jiti("../src/embedder.js");    // ← same cache as index.ts
// ...
const retrieverMod = jiti("../src/retriever.js");             // ← same cache in test body
const embedderMod = jiti("../src/embedder.js");             // ← same cache in test body

Test Results

Before fix:

✖ removes retrieval metadata from auto-recall injected text (444ms)
✖ applies auto-recall item/char budgets before injecting context (101ms)
✖ auto-recall only injects confirmed non-archived memories (70ms)
✖ filters USER.md-exclusive facts from auto-recall injected text (72ms)
✖ filters legacy addressing memories from auto-recall injected text (70ms)

tests 15 | pass 10 | fail 5

After fix:

✔ removes retrieval metadata from auto-recall injected text (124ms)
✔ applies auto-recall item/char budgets before injecting context (40ms)
✔ auto-recall only injects confirmed non-archived memories (36ms)
✔ filters USER.md-exclusive facts from auto-recall injected text (37ms)
✔ filters legacy addressing memories from auto-recall injected text (32ms)

tests 15 | pass 15 | fail 0

@rwmjhb rwmjhb merged commit 4c8bee1 into CortexReach:master Mar 27, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants