Skip to content

Commit cbc4d94

Browse files
feat(planner): port the prose-OR widen helper (S.1 green)
query_planner.prose_or_query is the archived feat/knowledge-proof branch's plan_prose_query, copied as logic rather than imported: every whitespace token of the raw question with Unicode Cc/Cs characters stripped, tokens carrying no alphanumeric dropped, each survivor double-quoted with embedded quotes doubled, joined with OR. No stop list, no length filter -- that is the §5 hypothesis (council D-12), and the reason the helper is only ever a candidate for the OR-widen step behind the shipped AND arm. Nothing on the serving path calls it yet. It exists so the eval harness can run it as an arm (next commit) and so the S.1 tests go green: the RED-only pyright ignore on the test's import is removed here, as the RED commit said it would be.
1 parent 8ebdeb4 commit cbc4d94

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎packages/agent-session-tools/src/agent_session_tools/query_planner.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import re
6+
import unicodedata
67
from dataclasses import dataclass
78

89
# Pinned verbatim from SessionWeaver v0.2.0. Keep this string form so changes
@@ -16,6 +17,12 @@
1617

1718
_TERM = re.compile(r"[a-zA-Z0-9_./-]+")
1819

20+
# Unicode general categories dropped from a raw token before it is quoted:
21+
# control characters (Cc) and surrogates (Cs). Everything else -- punctuation,
22+
# symbols, other scripts -- is left for the FTS5 tokenizer, which is what makes
23+
# the quoted form parse-safe without a whitelist of characters.
24+
_UNSAFE_CATEGORIES = frozenset({"Cc", "Cs"})
25+
1926

2027
def _terms(question: str) -> tuple[str, ...]:
2128
return tuple(
@@ -30,6 +37,40 @@ def _quote_term(term: str) -> str:
3037
return f'"{term}"'
3138

3239

40+
def prose_tokens(question: str) -> tuple[str, ...]:
41+
"""Every whitespace-separated token of ``question`` that carries an alphanumeric.
42+
43+
The §5 candidate's tokenisation (council D-12), ported from the archived
44+
``feat/knowledge-proof`` branch's ``plan_prose_query``: no stop list, no
45+
length filter, case preserved. Control and surrogate characters are
46+
stripped from each token first; a token left with no alphanumeric at all
47+
(``---``, ``???``) is dropped because FTS5 could match nothing in it.
48+
"""
49+
tokens: list[str] = []
50+
for raw in question.split():
51+
token = "".join(
52+
char for char in raw if unicodedata.category(char) not in _UNSAFE_CATEGORIES
53+
)
54+
if any(char.isalnum() for char in token):
55+
tokens.append(token)
56+
return tuple(tokens)
57+
58+
59+
def _quote_prose_token(token: str) -> str:
60+
"""Quote a raw token as one FTS5 string; an embedded ``"`` is doubled, per FTS5."""
61+
return '"' + token.replace('"', '""') + '"'
62+
63+
64+
def prose_or_query(question: str) -> str:
65+
"""The §5 candidate widen string: every raw token quoted and joined with ``OR``.
66+
67+
Nothing this returns can fail to parse: each token is a double-quoted FTS5
68+
string, so operators, columns, prefixes and punctuation inside it are
69+
plain text for the tokenizer. Returns ``""`` when no token survives.
70+
"""
71+
return " OR ".join(_quote_prose_token(token) for token in prose_tokens(question))
72+
73+
3374
@dataclass(frozen=True)
3475
class QueryPlan:
3576
"""The pure AND-to-OR plan for one question."""

‎packages/agent-session-tools/tests/test_query_planner_or_fallback.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@
2828

2929
from agent_session_tools import retrieval
3030
from agent_session_tools.migrations import migrate
31-
32-
# RED (S.1): the helper does not exist yet, so this module fails to import until
33-
# S.3 lands it; the ignore keeps the commit-time pyright hook honest about that.
34-
from agent_session_tools.query_planner import (
35-
prose_or_query, # pyright: ignore[reportAttributeAccessIssue]
36-
)
31+
from agent_session_tools.query_planner import prose_or_query
3732
from agent_session_tools.retrieval import plan_natural_language, plan_query, search
3833

3934
_GOLDEN = Path(__file__).parent / "golden" / "session_search_pre_planner.json"

0 commit comments

Comments
 (0)