Skip to content

fix(diagnostics): run the duplicate scan off the event loop (F91) - #195

Merged
Cipher208 merged 1 commit into
masterfrom
fix/f91-diagnose-loop-freeze
Nov 4, 2026
Merged

Cipher208 merged 1 commit into
masterfrom
fix/f91-diagnose-loop-freeze

Conversation

@Cipher208

Copy link
Copy Markdown
Owner

Defect (audit F91, P1)

memory_diagnose froze the MCP event loop: _audit_duplicates ran the O(n²) pairwise smart_similarity scan as pure CPU work inside the loop. Measured on a copy of the live hermes base (794 same-kind facts → 314 821 pairs): wall 39.99s, loop freeze 39.93s — no other tool could answer while it ran. All other diagnose checks combined: <0.5s.

Fix

  • New private sync _pairwise_duplicates(groups) holds the exact old loop (skip equal keys, >0.85, group/pair order, element schema {a_key, b_key, kind, similarity: round(sim, 3)}).
  • _audit_duplicates flattens rows to (str(key), str(value), str(kind)) tuples (no sqlite3.Row crosses the thread boundary) and awaits the scan via asyncio.to_thread.
  • Threshold, SQL, [:100] cap, suggestion text and every other check untouched.

The quadratic wall time remains by design — a prefilter would change which pairs are reported and needs its own spec. The P1 defect is the freeze: the loop now stays responsive.

Evidence

  • Regression test test_audit_duplicates_does_not_freeze_event_loop: 160 near-identical same-kind facts (~12.7k pairs ≈ 1s naive scan) + heartbeat task; asserts max loop gap < 0.5s and the first seeded pair is in items. RED on base (gap 1.08–1.16s), GREEN with the fix.
  • Live-base copy: pre-fix freeze 1.115s → post-fix 0.031s; duplicate findings byte-identical (2 pairs, cmp clean).
  • Gates: 2868 passed · ruff clean · format clean · mypy 42 = baseline.

_audit_duplicates ran the O(n^2) pairwise smart_similarity scan as pure
CPU work inside the event loop: 794 same-kind facts on the live base
(314 821 pairs) froze the whole MCP server for ~40s. Delegate the scan
to a worker thread via asyncio.to_thread; rows are flattened to plain
tuples before crossing the boundary, and the scan result is unchanged
(byte-identical on a live-base copy).
@coderabbitai

coderabbitai Bot commented Nov 4, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions github-actions Bot added the fix label Nov 4, 2026
@Cipher208
Cipher208 merged commit e38f57b into master Nov 4, 2026
19 checks passed
@Cipher208
Cipher208 deleted the fix/f91-diagnose-loop-freeze branch November 4, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant