Skip to content

feat(query): parse query expressions with Lark (#2006) - #2009

Merged
Sinity merged 5 commits into
masterfrom
feature/feat/query-dsl-parser-2006
Jun 17, 2026
Merged

Sinity merged 5 commits into
masterfrom
feature/feat/query-dsl-parser-2006

Conversation

@Sinity

@Sinity Sinity commented Jun 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

Introduce Lark as the canonical query-expression grammar substrate, expose a typed parse envelope for current query strings, and add a query-expression explanation payload. Current compile_expression() callers keep working because the new Lark AST lowers into SessionQuerySpec; that is a migration bridge, not a separate floor grammar.

Ref #2006.

Problem

The query DSL was still rooted in a hand-rolled whitespace scanner plus regex token splitting. That works for the old flat conjunction language, but it is the wrong foundation for the full query AST/pipeline grammar in #2006: precedence, grouping, quoted strings, literals, and diagnostics need to belong to one real grammar. The first implementation layer also needs an inspectable parse/lower/plan envelope so later CLI/MCP/API surfaces do not invent their own explanation formats.

CI also surfaced a dependency-audit failure on cryptography==48.0.0 (GHSA-537c-gmf6-5ccf / CVE-2026-39892) after the lockfile refresh.

Solution

  • Add lark>=1.3.1,<2.0 as a runtime dependency and update uv.lock.
  • Replace the manual scanner in polylogue/archive/query/expression.py with a Lark grammar for current query clauses.
  • Add QueryExpressionAST and parse_expression_ast() as the typed parse boundary.
  • Route _lex() through the Lark AST only as a projection into the current clause lowerer; the durable direction is one grammar and one AST/lowering pipeline.
  • Add QueryExpressionExplanation, QueryExpressionExplainClause, and explain_expression() covering source text, clauses, lowerer name, lowered spec, plan descriptions, and unsupported-node placeholders.
  • Preserve existing unquoted code-like FTS searches such as foo(bar), array[0], and dict{key} while still rejecting leading Boolean/group syntax.
  • Reject malformed count filters such as messages:>=10x instead of splitting them into a valid count plus broad FTS term.
  • Tighten feat(query): implement the full query DSL substrate #2006 and the execution-control notes so future work treats old flat-expression behavior as parity evidence and replacement target, not a parallel grammar.
  • Raise the cryptography floor to >=48.0.1; the refreshed lock resolves to 49.0.0.
  • Add AST/explain tests for mixed clauses, escaped quoted strings, JSON-spec mode, review-regressed code-like terms, and malformed count filters.

This is the first working replacement layer, not the full DSL: existing query strings now enter through the Lark grammar and still lower to today's executable SessionQuerySpec path while the next PRs add Boolean/structural lowerers.

Verification

  • uv run pip-audit --strict --vulnerability-service osv --disable-pip -r .cache/requirements-audit-local.txt — No known vulnerabilities found.
  • devtools test tests/unit/cli/test_query_expression.py — 171 passed in 9.56s.
  • devtools verify --quick — exit_code: 0.
  • Pre-push hook reran devtools verify --quick on 3d330d3e — exit_code: 0; the push of the review-fix commit will rerun the same quick gate.

Problem: the query DSL still relied on a hand-rolled whitespace scanner and regex token splitter, which is the wrong substrate for the full AST/pipeline grammar tracked in #2006.

What changed: add Lark as a runtime dependency, define a Lark grammar for the current query-expression clauses, expose parse_expression_ast(), keep _lex() as a compatibility projection, and preserve existing SessionQuerySpec lowering behavior.

Verification: devtools test tests/unit/cli/test_query_expression.py (166 passed); devtools verify --quick (exit_code 0).
@coderabbitai

coderabbitai Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

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

More reviews will be available in 37 minutes and 48 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

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.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 050d755b-4e8f-4693-a70d-d40103b5b3e8

📥 Commits

Reviewing files that changed from the base of the PR and between f18a6b8 and 31f0f41.

⛔ Files ignored due to path filters (1)
  • uv.lock is excluded by !**/*.lock, !**/*.lock
📒 Files selected for processing (4)
  • docs/plans/current-issue-execution-control-plane.md
  • polylogue/archive/query/expression.py
  • pyproject.toml
  • tests/unit/cli/test_query_expression.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/feat/query-dsl-parser-2006

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 and usage tips.

Problem: #2006's first implementation layer needs a concrete way to inspect source text, parsed clauses, lowering path, and execution plan details, not only an internal parser swap.

What changed: add QueryExpressionExplanation / QueryExpressionExplainClause plus explain_expression(), covering both Lark query expressions and direct JSON specs, with payload conversion for future CLI/API exposure.

Verification: devtools test tests/unit/cli/test_query_expression.py (168 passed); devtools verify --quick (exit_code 0).

@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: 02c99ca528

ℹ️ 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".

Comment thread polylogue/archive/query/expression.py Resolved
Comment thread polylogue/archive/query/expression.py Resolved
Sinity added 2 commits June 17, 2026 02:59
Clarify that the Lark grammar and AST are the query language for #2006. The current flat-expression behavior is parity evidence and a temporary lowering target, not a separate floor grammar to preserve.
pip-audit flagged cryptography 48.0.0 via GHSA-537c-gmf6-5ccf / CVE-2026-39892. Raise the declared floor to 48.0.1 and refresh uv.lock, which resolves to 49.0.0.
github-actions Bot pushed a commit that referenced this pull request Jun 17, 2026
Address review findings on the query grammar bridge. Malformed count filters now reject as count/filter errors instead of tokenizing into a valid count plus broad FTS term, and unquoted code-like search tokens such as foo(bar), array[0], and dict{key} continue to compile as FTS terms.
@Sinity
Sinity merged commit 0a4fbfd into master Jun 17, 2026
15 checks passed
@Sinity
Sinity deleted the feature/feat/query-dsl-parser-2006 branch June 17, 2026 01:06
Sinity added a commit that referenced this pull request Jun 17, 2026
## Summary
Add the next #2006 query-DSL layer: explicit Boolean expressions now
compile into a typed predicate tree and execute against archive SQL for
supported session-level fields. Cross-field `OR`, explicit `AND`, `NOT`,
grouping, and `sessions where ...` forms no longer reject or broaden
into FTS when their leaves are SQL-backed session predicates.

Ref #2006.

## Problem
After #2009, current query strings entered through Lark, but cross-field
Boolean syntax was still a typed rejection. That left the new grammar
unable to execute one of the most basic full-DSL requirements: `(repo:x
OR origin:y)` style predicates over session fields. Flattening those
forms into `SessionQuerySpec` tuple fields would silently turn OR into
AND, and treating them as free text would broaden results.

## Solution
- Add `polylogue.archive.query.predicate` with typed
`QueryFieldPredicate`, `QueryBoolPredicate`, and `QueryNotPredicate`
nodes.
- Extend the expression parser so explicit Boolean syntax (`OR`, `AND`,
`NOT`, leading grouping, or `sessions where`) emits a predicate tree on
`QueryExpressionAST`.
- Keep ordinary prose such as `error or timeout not retry` on the FTS
path unless the expression contains structured query syntax.
- Carry the predicate through `SessionQuerySpec`, `SessionQueryPlan`,
and archive execution kwargs.
- Lower supported leaves through the existing `_session_filter_clause`,
preserving the same SQL semantics for origin, repo, tag, title, path,
cwd, tool, action, has-flags/types, message/word counts, since/until,
and exact session ids.
- Compose ordinary filters with Boolean predicates as AND at the archive
store boundary.
- Keep unsupported/non-SQL leaves typed: this PR does not pretend
FTS/vector/pipeline terms are Boolean-lowerable yet.
- Regenerate topology surfaces for the new archive-query module.

## Verification
- `devtools test tests/unit/cli/test_query_expression.py` — `177 passed
in 13.85s`.
- `devtools verify --quick` — `exit_code: 0`.
- Pre-push hook reran `devtools verify --quick` on `9d21ff04` —
`exit_code: 0`.
@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.

  • 59.F050 / P2: fixed-already at the checked revision: polylogue/archive/query/expression.py:1305 requires a token boundary after the numeric count. A suffixed value such as messages:>=10x cannot be consumed as a valid count followed by a separate text term.
  • 59.F051 / P2: fixed-already at the checked revision: polylogue/archive/query/expression.py:1310 permits parentheses, brackets and braces inside bare text, and tests/unit/cli/test_query_expression.py:245 exercises foo(bar), array[0] and dict{key} together. Those non-leading code-like tokens are no longer excluded by the compact lexer.

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