Skip to content

fix(retrieval): use token hashes for map-unit frequency lookup - #370

Merged
suguanYang merged 1 commit into
mainfrom
fix/wangbinqi/remove-namespace-stat-readers
Sep 1, 2026
Merged

fix(retrieval): use token hashes for map-unit frequency lookup#370
suguanYang merged 1 commit into
mainfrom
fix/wangbinqi/remove-namespace-stat-readers

Conversation

@suguanYang

Copy link
Copy Markdown
Contributor

Summary

Production classic retrieval still queried document_map_unit_tokens.token with ANY(...), bypassing the existing (channel, token_hash, map_unit_id) index. The affected production request hit PostgreSQL statement timeout after about 42 seconds.

This change hashes the same normalized query tokens with SHA-256 and uses token_hash = ANY(...) for the frequency lookup. The response still selects the original token and frequency, so scoring behavior is unchanged; only the lookup predicate changes.

Validation

  • uv run pytest apps/api/tests/contract/test_retrieval_classic_map_unit_contract.py (3 passed)
  • uv run ruff check packages/shared-python/shared/services/retrieval/search/map_unit_discovery.py apps/api/tests/contract/test_retrieval_classic_map_unit_contract.py
  • uv run pyright packages/shared-python/shared/services/retrieval/search/map_unit_discovery.py apps/api/tests/contract/test_retrieval_classic_map_unit_contract.py
  • git diff --check

Deployment follow-up

After deployment, rerun the production classic request and verify HTTP 200, no statement timeout, and a Logfire SQL span containing token_hash = ANY(...) rather than token = ANY(...). No backfill rerun is required for this code-only lookup change.

@suguanYang
suguanYang merged commit 088fc1f into main Sep 1, 2026
6 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.

1 participant