Repository navigation
fix(diagnostics): run the duplicate scan off the event loop (F91) - #195
Merged
Merged
Conversation
_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).
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Defect (audit F91, P1)
memory_diagnosefroze the MCP event loop:_audit_duplicatesran the O(n²) pairwisesmart_similarityscan 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
_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_duplicatesflattens rows to(str(key), str(value), str(kind))tuples (nosqlite3.Rowcrosses the thread boundary) and awaits the scan viaasyncio.to_thread.[: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
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.cmpclean).