Skip to content

fix(entity): close concurrency race + Unicode NFC/NFD split#189

Merged
KailasMahavarkar merged 1 commit intomainfrom
fix/resolver-concurrency-and-unicode
May 2, 2026
Merged

fix(entity): close concurrency race + Unicode NFC/NFD split#189
KailasMahavarkar merged 1 commit intomainfrom
fix/resolver-concurrency-and-unicode

Conversation

@KailasMahavarkar
Copy link
Copy Markdown
Contributor

Adversarial stress smoke (10 scenarios, 22+ assertions) found two production-relevant bugs in the resolver:

1. Concurrency race (HIGH severity for LLM-rate ingest)

resolve_mention() reads candidates, returns "no match" + fresh entity_id; caller issues CREATE NODE. Two threads racing on the same name both observe zero candidates → both mint distinct ids → N entities for one human.

Repro: 8 threads × 50 ingests of name "Bob" leaked 2-4 canonical Bobs per run.

Fix: new resolve_and_create_entity() wraps the read+write under a process-global lock + name→entity_id cache. Cache short-circuits the second concurrent caller without a NODES query, bypassing read-after-write visibility lag.

Result: 5 consecutive 8-thread × 50-mention runs all produce exactly 1 canonical entity.

2. NFC vs NFD Unicode split (MEDIUM)

"Müller" with composed ü (NFC, U+00FC) and decomposed ü (NFD, u + U+0308) normalized to mller vs muller because the regex strips the combining mark only AFTER NFD decomposition.

Fix: normalize_name NFKD-decomposes first; both encodings → muller.

bonsai_ingestor

Synthesizer uses resolve_and_create_entity so the entity write happens under the lock. Tests with gs=None still get a CREATE NODE for the entity inline (the wrapper isn't called).

Test plan

pytest tests/test_entity_resolver.py tests/test_bonsai_ingestor.py
-> 115 passed

pytest --tb=short -q ...
-> 1952 passed, 102 skipped

Stress smoke (/tmp/smoke_entity_stress.py, 10 scenarios):

  • 1000 same-name single thread → 1 entity
  • 500 distinct names → 500 entities, 41 ingests/s
  • 8 threads × 50 same-name → 1 entity (was 2-4)
  • 20 candidates → 2 ms resolver
  • DSL injection survived
  • Unicode/emoji names ingest
  • NFC vs NFD Müller → both "muller"
  • 50KB context OK
  • 50 sessions × 200 mentions, 30 unique people → 30 entities, 216 ingests/s

Outstanding (separate work):

  • Replay of same msg_id raises Node already exists: 'm1'. By-design CREATE semantics. Pipelines must dedupe msg_ids upstream or use UPSERT for idempotent retries.

Stress smoke (8 threads x 50 ingests of same name) leaked 2-4 duplicate
entities per run. Two bugs:

1. Read-decide-write race. resolve_mention() reads candidates, returns
   "no match" + a fresh entity_id; caller then issues CREATE NODE. Two
   threads racing on the same name both observe zero candidates and
   both mint distinct fresh ids - producing N entities for what should
   be 1.

   Fix: new resolve_and_create_entity() wraps the read+write critical
   section in a process-global lock and a name -> entity_id cache.
   Cache short-circuits the second concurrent caller without a NODES
   query, bypassing any read-after-write visibility lag. After the
   fix, 5 consecutive 8-thread x 50-mention runs all produce exactly
   1 canonical entity.

2. NFC vs NFD encodings of the same name normalized to different
   strings. "Muller" written by source A (NFC: U+00FC composed) and
   source B (NFD: u + U+0308 combining) collapsed to "mller" vs
   "muller" because [^a-z0-9] strips the combining mark only after
   NFD decomposition.

   Fix: normalize_name now NFKD-decomposes before lower + strip. Both
   encodings collapse to "muller". Confirmed via stress scenario 7.

bonsai_ingestor: synthesizer uses resolve_and_create_entity instead of
resolve_mention so the entity write happens under the lock. When
gs is None (dry-run / unit tests) the synthesizer still emits the
CREATE NODE for the entity since the wrapper hasn't run.

Test plan:
  pytest tests/test_entity_resolver.py tests/test_bonsai_ingestor.py
    -> 115 passed
  pytest --tb=short -q ...
    -> 1952 passed, 102 skipped

Stress smoke (10 scenarios, 22+ assertions):
  9/10 PASS. Outstanding INVESTIGATE: replay of same msg_id raises
  "Node already exists: 'm1'" - by-design CREATE semantics, not
  this PR's scope. Caller pipelines must dedupe msg_ids upstream
  or use UPSERT NODE for idempotent retries.

Throughput: 216-231 ingests/s on 16-core CPU, model2vec embedder.

reset_resolver_cache_for_tests() exposed for test isolation; pytest
fixture and stress-smoke fresh_store() both clear the cache so prior
runs don't leak entity ids.
@KailasMahavarkar KailasMahavarkar merged commit eb7a963 into main May 2, 2026
5 checks passed
@KailasMahavarkar KailasMahavarkar deleted the fix/resolver-concurrency-and-unicode branch May 2, 2026 13:39
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.

1 participant