Skip to content

feat(search): fold Polish l-stroke and remove_diacritics for FTS recall - #2751

Closed
Sinity wants to merge 1 commit into
masterfrom
feat/search-recall-pl-fold
Closed

Sinity wants to merge 1 commit into
masterfrom
feat/search-recall-pl-fold

Conversation

@Sinity

@Sinity Sinity commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Summary

Closes the specific Polish-search recall hole named in polylogue-9jsi: unicode61 FTS5 tokenization alone cannot fold ł/Ł (Latin L with stroke) because Unicode assigns it no canonical decomposition, so a plain-ASCII query like latwo never found seeded łatwo content. This PR adds a narrow, deterministic pl_fold layer around FTS5, expressed once and used identically on the write and query sides, plus enables unicode61 remove_diacritics 2 (which handles the other Polish diacritics — ó, ż, ą, ... — on its own).

Problem

Verified empirically before implementing (see tests/unit/storage/test_pl_fold.py and the PR discussion below): with tokenize='unicode61 remove_diacritics 2', indexing różowy and querying rozowy already matches — the tokenizer folds ordinary combining-mark diacritics correctly and symmetrically on both sides. But żółty tokenizes to zołty (ż and ó folded, ł left untouched), because ł/Ł has no NFD/NFKD decomposition for remove_diacritics or Unicode normalization to reach. So even with the tokenizer bump alone, latwo would still miss łatwo, and remove_diacritics was not even enabled to begin with — both problems compound.

Solution

  • polylogue/storage/fts/pl_fold.py: a single PL_FOLD_TABLE (ł→l, Ł→L) expressed three ways so write-side and query-side folding cannot drift apart:
    • pl_fold() — Python implementation, used for query-side normalization.
    • pl_fold_sql_expr() — an inline SQL REPLACE-chain, embedded directly into trigger/insert DDL text. This is deliberately not a call to a registered custom function, because SQLite trigger bodies execute on whatever connection performs the write — including raw test/tooling connections that reuse the shared trigger DDL (fts_lifecycle.py's _BLOCKS_FTS_TRIGGER_DDL etc.) without ever registering a UDF. An inline REPLACE chain has no such dependency.
    • register_pl_fold() — registers the identical transform as a SQL scalar function pl_fold(x), wired into both sync (storage/sqlite/connection.py) and async (storage/sqlite/async_sqlite.py) read/write connection setup, for ad-hoc queries, diagnostics, and the Python/SQL agreement test.
  • All three unicode61 contentless FTS surfaces — messages_fts, threads_fts, session_work_events_fts — move to tokenize='unicode61 remove_diacritics 2' and fold search_text through pl_fold_sql_expr before insertion, at every DDL/trigger/bulk-insert site (archive_tiers/index.py, fts/fts_lifecycle.py, fts/sql.py). threads_fts/session_work_events_fts were included alongside messages_fts (beyond the bead's literal example) because query-side folding via escape_fts5_query is already global across all FTS surfaces — leaving those two write-side unfolded would have reintroduced exactly the "silent recall loss" drift the bead calls out as the top risk.
  • escape_fts5_query — the single chokepoint every production MATCH builder in the archive routes through (8+ call sites across archive_tiers/archive.py, search/query_builders.py, search_providers/fts5.py, sqlite/queries/session_insight_*_queries.py, insights/resume.py) — applies the identical Python pl_fold to query text before quoting.
  • INDEX_SCHEMA_VERSION 34 → 35 (derived-tier, no migration chain — existing archives rebuild via polylogue ops reset --index && polylogued run). docs/internals.md and docs/search.md updated.

Deliberately out of scope (per the bead's own AC, which requires trigram to stay off "until benchmarked with size+precision report" and never silently default): no trigram fallback lane is added here. Tracked as follow-up polylogue-xul7.

Also discovered, filed, left unfixed (confirmed pre-existing and unrelated via git log): tests/unit/storage/test_archive_tiers_assertions.py::test_fresh_user_tier_has_no_legacy_overlay_tables fails on any fresh checkout since #2703 added context_deliveries to user.db without updating this assertion. Filed as polylogue-gxly.

Verification

  • devtools test tests/unit/storage/test_pl_fold.py — 12 passed. Covers: ł/Ł-only folding, idempotency, Python/registered-UDF/inline-REPLACE-chain 3-way agreement, the two-canonical-DDL-site tokenizer drift lock, and end-to-end recall (latwo/zrobilem finding seeded łatwo/zrobiłem, plus a literal ł query, plus remove_diacritics alone finding różowy/zrobię) against the real trigger-backed schema via the test_conn fixture.
  • devtools test tests/unit/storage/test_fts5.py tests/unit/storage/test_fts_repair_sql.py tests/unit/storage/test_fts_bloat_invariants.py tests/unit/storage/test_dangling_fts_derived_surfaces.py tests/unit/storage/test_index.py tests/unit/storage/test_fts5_query_correctness.py tests/unit/storage/test_fts_escape_in_insights.py tests/unit/storage/test_blocks_command_trigram.py — 211 passed.
  • devtools test tests/unit/storage/test_archive_tiers_assertions.py tests/unit/storage/test_schema_policy_contracts.py tests/unit/cli/test_status_diagnostics.py tests/unit/cli/test_tutorial.py — 63/64 passed (1 pre-existing unrelated failure, polylogue-gxly).
  • devtools test -k "test_polylogued_status_json_reports_schema_mismatch_not_ready or test_polylogued_status_plain_reports_schema_mismatch" tests/unit/daemon/test_daemon_cli.py — 2 passed (the schema-version-sensitive daemon status tests; the full test_daemon_cli.py file was not run in full, it contains an unrelated slow periodic-loop test).
  • mypy --strict on every touched file — clean.
  • devtools render all --check — clean (topology projection regenerated for the new module).
  • Pre-push devtools verify --quick (format + lint + mypy + render-all-check) — passed.

Ref polylogue-9jsi

Problem: unicode61's remove_diacritics option folds ordinary combining-mark
diacritics (o-acute, z-dot, a-ogonek, ...) symmetrically for indexed and
MATCH query text, but Unicode assigns l/L-stroke no canonical decomposition,
so neither NFD normalization nor remove_diacritics can fold it. A plain-ASCII
query like "latwo" never found seeded "latwo-with-stroke" content, and
remove_diacritics wasn't even enabled. Verified empirically with a live
sqlite3 fts5vocab probe before implementing (unicode61 remove_diacritics 2
folds z-dot/o-acute correctly on its own; only the stroke letter needs an
explicit fold).

What changed:
- polylogue/storage/fts/pl_fold.py: one canonical PL_FOLD_TABLE (l/L-stroke
  only) expressed three ways -- pl_fold() (Python, query-side), the
  registered SQL scalar function (ad-hoc/diagnostics), and
  pl_fold_sql_expr() (an inline REPLACE-chain embedded directly in
  trigger/insert DDL text so write-side folding does not depend on every
  connection that fires a blocks/threads/session_work_events trigger having
  registered a custom function first).
- All three unicode61 contentless FTS surfaces (messages_fts, threads_fts,
  session_work_events_fts) move to tokenize='unicode61 remove_diacritics 2'
  and fold search_text through pl_fold_sql_expr before insertion, in both
  canonical DDL sites (archive_tiers/index.py, fts/fts_lifecycle.py,
  fts/sql.py bulk-insert helpers).
- escape_fts5_query -- the single chokepoint every production MATCH builder
  routes through (8+ call sites) -- applies the identical Python pl_fold to
  query text before quoting, so write/query folding cannot drift apart.
- register_pl_fold wired into both sync (storage/sqlite/connection.py) and
  async (storage/sqlite/async_sqlite.py) read/write connection setup.
- INDEX_SCHEMA_VERSION 34 -> 35 (derived-tier rebuild: polylogue ops reset
  --index && polylogued run). docs/internals.md + docs/search.md updated.

Explicitly out of scope (per the bead's own AC): a trigram fallback lane
stays off pending a measured size/precision report -- tracked as
polylogue-xul7, not built here.

Verification: devtools test tests/unit/storage/test_pl_fold.py (12 passed,
covers idempotency, Python/SQL/REPLACE-chain agreement, the DDL-site
drift-lock, and end-to-end recall against the real trigger-backed schema)
plus the surrounding FTS suite (211 passed) and schema-version-sensitive
tests (test_archive_tiers_assertions.py, test_schema_policy_contracts.py,
test_status_diagnostics.py, test_tutorial.py, 2 targeted daemon_cli tests --
all passed except one pre-existing unrelated failure filed as
polylogue-gxly). mypy --strict clean on touched files. devtools render all
--check clean.

Ref polylogue-9jsi

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 13 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 24504286-9170-47f6-8dfd-845bb2445b66

📥 Commits

Reviewing files that changed from the base of the PR and between 2ceff52 and 8200633.

📒 Files selected for processing (14)
  • .beads/interactions.jsonl
  • .beads/issues.jsonl
  • docs/internals.md
  • docs/plans/topology-target.yaml
  • docs/search.md
  • docs/topology-status.md
  • polylogue/storage/fts/fts_lifecycle.py
  • polylogue/storage/fts/pl_fold.py
  • polylogue/storage/fts/sql.py
  • polylogue/storage/search/query_support.py
  • polylogue/storage/sqlite/archive_tiers/index.py
  • polylogue/storage/sqlite/async_sqlite.py
  • polylogue/storage/sqlite/connection.py
  • tests/unit/storage/test_pl_fold.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/search-recall-pl-fold

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

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8200633d7e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Sinity

Sinity commented Jul 12, 2026

Copy link
Copy Markdown
Owner Author

Superseded by merged PR #2754. The replacement includes the original Polish fold plus the audited ArchiveStore rebuild and dangling-repair coverage.

@Sinity Sinity closed this Jul 12, 2026
@Sinity
Sinity deleted the feat/search-recall-pl-fold branch July 12, 2026 16:54
@Sinity

Sinity commented Oct 1, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

  • 75.F026 / P2: fixed-already at the checked revision: The public rebuild route calls rebuild_fts_index_sync at polylogue/storage/sqlite/archive_tiers/archive.py:1318, whose content rebuild uses the canonical insertion SQL. The all-message insertion folds search_text at polylogue/storage/fts/sql.py:266, matching query-side folding rather than restoring raw l-stroke text into FTS.

Only the rows listed above are dispositioned. Held findings remain open.

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