MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

fix(auto-recall): add timeout + fail-open so slow LLMs cannot stall startup or first turn

Open Sanjays2402 opened this issue 6 months ago • 2 comments

Summary

With auto-recall enabled and an existing memory database, a slow LLM on the recall/filter path could block the gateway critical path for 30-40 seconds — long enough to trip health checks and cause restart loops (#1452).

This PR wraps the recall/filter LLM work in a configurable timeout and ensures any exception in the auto-recall path fails open rather than propagating to the gateway top level.

Changes

  • New withTimeout helper that resolves to null on timeout (clean fail-open semantics).
  • Auto-recall LLM filter now races against recall.autoRecallTimeoutMs (default 8000 ms).
  • Top-level try/catch around the auto-recall block; on any error or timeout we log a warning and return an empty memory set so the prompt build proceeds normally.
  • New config key documented: recall.autoRecallTimeoutMs.

Behavior

  • Healthy LLM: indistinguishable from before — recall + filter happen, memories injected.
  • Slow LLM (timeout): warning logged, prompt builds with no auto-injected memories, gateway proceeds.
  • LLM error: same as timeout — warning logged, fail open.
  • Startup ready is unaffected; auto-recall was never on that path, but the timeout + fail-open guarantees it stays that way.

Fixes #1452

Sanjays2402 avatar May 09 '26 19:05 Sanjays2402

Thanks — both addressed in the latest commit:

  • index.ts:1905 (config not wired): resolveConfig() (in src/config.ts) now passes cfg.recall.autoRecallTimeoutMs through to the resolved recall block. Without this the user-facing config key was effectively dead and the default always won — good catch.
  • with-timeout.test.ts:48 (flaky timers): Switched to vi.useFakeTimers() + vi.advanceTimersByTimeAsync() so the timeout behavior is asserted deterministically. The previous wall-clock elapsed < 500ms check was the obvious flake risk under CI load. Test suite now finishes in ~5ms and the 30s-hang case is verified by advancing fake time past 8001ms instead of measuring real elapsed time.

Sanjays2402 avatar May 09 '26 19:05 Sanjays2402

This PR has been automatically marked as stale due to inactivity.

github-actions[bot] avatar Jun 09 '26 06:06 github-actions[bot]