Skip to content

fix(search): retry user-delimited script phrases on empty FTS - #2194

Open
mikemikimike wants to merge 2 commits into
basicmachines-co:mainfrom
mikemikimike:fix/issue-2183-relaxed-script-phrases-20261201-a22cb2
Open

mikemikimike wants to merge 2 commits into
basicmachines-co:mainfrom
mikemikimike:fix/issue-2183-relaxed-script-phrases-20261201-a22cb2

Conversation

@mikemikimike

Copy link
Copy Markdown
Contributor

Summary

  • Keep strict hybrid FTS matching as the first attempt, then retry zero-result searches with OR between user-delimited script phrases.
  • Preserve ordered bigram matching within each phrase and retain the word-channel constraint in mixed-script queries for SQLite and PostgreSQL.

Tests

  • Focused SQLite search tests: 75 passed, 2 skipped.
  • Focused PostgreSQL search tests: 73 passed, 4 skipped.
  • Broader unit and integration failures were reproduced on the unmodified upstream base.

Fixes #2183

Original issue: #2183

Signed-off-by: mikemikimike <13286568797@163.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T00:14:41.786712Z 1d180a2 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 1d180a2ea9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

if not search_text:
return None
script_query = analyze_script_query(search_text.strip())
if len(script_query.gram_phrases) < 2:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Gate script retries with the shared eligibility rules

In hybrid retrieval, a query that the shared guard intentionally deems ineligible can still pass this check: SearchReader.hybrid() always sets allow_relaxed=True, so 適者/生存 (one punctuation-joined token for which relaxed_query_words() returns None) is analyzed as two gram_phrases and retried as 適者 OR 生存 after a strict miss. The equivalent PostgreSQL len(script_conditions) >= 2 branch does the same, causing hybrid results to include notes containing only one half even though FTS mode preserves the query as strict; require the user-delimited/central relaxation eligibility here rather than raw script-run count.

Useful? React with 👍 / 👎.

@Flynn-Kawolsky

Copy link
Copy Markdown

Thanks for picking up #2183 so quickly across both SQLite and Postgres (and for catching the pre-existing Postgres word_text parity bug in run_search as well) — ruff, ty, and the 75 SQLite/Postgres tests all pass cleanly.

Three concrete edge cases I noticed while reviewing and testing 1d180a2:

  1. Eligibility gate + per-whitespace-token grouping & deduplication (extending Codex's P2 note):

    • Currently relaxed_script_fts_text() (sqlite_search_query.py:244-253) and compile_fts_filter() (postgres_search_query.py:634) trigger whenever len(gram_phrases) >= 2 / len(script_conditions) >= 2. Because analyze_script_query() splits script_runs on any non-script character (punctuation/digits), single-token or ineligible queries like "適者/生存", "SPEC 16 顯示器 邊緣", or "適者 適者" relax even though relaxed_query_words(search_text) is None.
    • Importantly, gating on relaxed_query_words(search_text) is not None alone isn't quite enough if all gram_phrases are flattened into a single OR:
      • "OpenAI GPT 適者/生存" returns relaxed_query_words == ['OpenAI', 'GPT'] (non-alnum '適者/生存' is pruned by word.isalnum()), so flat OR still splits the single token "適者/生存" into "適者" OR "生存".
      • "顯示器 邊緣/面板 磁感測" returns relaxed_query_words == ['顯示器', '磁感測'] with word_text=None, so flat OR splits "邊緣/面板" into "邊緣" OR "面板", matching notes that mention only "面板" with no word-channel backstop.
      • "OpenAI 適者 適者" returns relaxed_query_words == ['OpenAI', '適者'], so without deduplicating token-level script groups it runs a redundant "適者" OR "適者" retry with relaxed_fallback_used=True.
    • Gating on relaxed_query_words(search_text) is not None, grouping gram_phrases per whitespace-delimited token (keeping intra-token runs AND-ed together — e.g. SQLite FTS5 script_ngrams: (("邊緣" AND "面板") OR "顯示 示器") and Postgres ((cond_a AND cond_b) OR cond_c)), deduplicating unique token groups, and relaxing only when >= 2 distinct token groups remain handles all of these cleanly.
  2. Postgres is_tsquery_syntax_error futile retry when only relaxed_script is active (postgres_search_query.py:1007):

    • Changing if not (is_tsquery_syntax_error(exc) and relaxed and params.get("text")): to and fallback_available re-runs the exact same malformed params["text"] whenever relaxed is None and relaxed_script is True.
    • For example, mirroring the existing test_postgres_search_repository.py:1692 fixture ("foo<bar baz qux"), "foo<bar 適者 生存" has word_text="foo<bar" (triggers is_tsquery_syntax_error), relaxed=None, and relaxed_query_words("foo<bar 適者 生存") == ['適者', '生存'] — so this still triggers even after adding the relaxed_query_words gate, deterministically failing on the retry.
    • Keeping the except guard scoped to word-channel relaxation (if not (is_tsquery_syntax_error(exc) and relaxed and params.get("text")): raise) avoids the futile retry.
  3. Minor telemetry/debug-log accuracy on script-only retries (sqlite_search_query.py:643-653, postgres_search_query.py:1025-1035):

    • On pure-script or 1–2 word mixed retries (e.g. "顯示器 邊緣 磁感測" or "OpenAI 適者 生存"), relaxed is None, so both backends currently log relaxed='None' and record token_count=0 (len(relaxed_query_words(relaxed_search_text) or ())). Computing token_count from relaxed_query_words(search_text) and including relaxed_script in the debug log keeps telemetry accurate.

Signed-off-by: mikemikimike <13286568797@163.com>
@Flynn-Kawolsky

Copy link
Copy Markdown

Thanks for the thoughtful follow-up in c080c81! Verified all three points from R1 locally on Windows 11:

  1. Eligibility gate & per-token grouping (script_ngrams.py:158-170, sqlite_search_query.py:244-261, postgres_search_query.py:620-672): relaxed_query_words(search_text) is not None + script_query_token_groups cleanly preserves intra-token AND ("顯示器 邊緣/面板 磁感測" -> ("邊緣" AND "面板")), keeps "OpenAI GPT 適者/生存" strict-only on the script half, and deduplicates repeated token groups.
  2. Postgres is_tsquery_syntax_error guard (postgres_search_query.py:1022-1024): Confirmed single-execution behavior when params.get("text") is absent (test_search_relaxed_rendering.py:237-255).
  3. Telemetry & debug logging (sqlite_search_query.py:658-668, postgres_search_query.py:1038-1049): relaxed_script logging and token_count calculation look clean.

ruff check, ruff format --check, ty check, and pytest (test_script_ngrams.py, test_script_relaxed_fallback.py, test_search_relaxed_rendering.py — 93 passed, 2 skipped) all pass locally. LGTM!

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.

[FEATURE] Let hybrid relaxed FTS retry OR user-delimited script phrases when strict AND returns 0 results

2 participants