Skip to content

feat(query): expose structural grammar completion metadata (#1844) - #2025

Merged
Sinity merged 1 commit into
masterfrom
feature/feat/query-completion-grammar-metadata-1844
Jun 17, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/feat/query-completion-grammar-metadata-1844

Conversation

@Sinity

@Sinity Sinity commented Jun 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

Expose structural query-unit metadata from the Lark-backed query grammar and use it for shell completion. Completion now suggests block( after exists and structural fields such as type: / text: inside exists block(...), all sourced from the expression module's grammar metadata rather than a parallel list.

Problem

The query grammar now supports structural predicates over messages, actions, and blocks, but completion only projected session field metadata from EXPRESSION_FIELD_REGISTRY. That would make #1844 drift from #2006. There was also a concrete parser-path bug: _is_boolean_expression() recognized exists message(...) and exists action(...), but not exists block(...), so single-child block predicates could miss the Boolean grammar path.

Solution

Added a typed StructuralQueryUnitInfo registry in polylogue/archive/query/expression.py, exported structural_query_units() / structural_query_fields(), and wired polylogue/cli/shell_completion_values.py to produce structural unit and structural field candidates from that registry. The completion callback now recognizes both full partial text (exists block(t) and normal shell-token contexts (exists + b, exists block( + t). The block detector now routes exists block(...) through the same Boolean grammar as message/action predicates.

The durable issue text for #1844 and #2006 was also updated to remove the misleading "floor grammar" framing: there is one Lark grammar/AST/lowerer path.

Verification

  • nix develop --command devtools test tests/unit/cli/test_command_aux_runtime.py tests/unit/cli/test_completion_matrix.py tests/unit/cli/test_query_expression.py -k 'query_structural or block_exists or structural_exists or query_field_completion or query_field_value_completion' -> 17 passed, 261 deselected
  • nix develop --command devtools verify --quick -> exit_code: 0
  • nix develop --command devtools verify reached pytest testmon after static/generated checks passed and was interrupted after about 12 minutes with no failure output; this PR uses the focused slice plus quick gate as the local evidence.

Refs #1844
Refs #2006

Summary by CodeRabbit

  • New Features

    • Added structural query support enabling users to filter conversation elements with field-based conditions.
    • Enhanced shell completion to intelligently suggest available query units and their supported fields during query composition.
  • Tests

    • Added test coverage for structural query parsing and shell completion functionality across different environments.

Problem: query completion only projected session field metadata, while the grammar now supports structural exists predicates over messages, actions, and blocks. The parser also had a stale boolean-expression detector that recognized exists message/action but not exists block, so single-child block predicates could miss the Boolean grammar path.\n\nWhat changed: add typed structural query-unit metadata to the expression module, expose structural unit/field completion candidates from that metadata, and route completion contexts after exists or inside exists <unit>(...) through the shared registry. Fix the block detector so compile_expression uses the same grammar path for exists block(...) as parse_expression_ast.\n\nVerification: devtools test tests/unit/cli/test_command_aux_runtime.py tests/unit/cli/test_completion_matrix.py tests/unit/cli/test_query_expression.py -k 'query_structural or block_exists or structural_exists or query_field_completion or query_field_value_completion' -> 17 passed. devtools verify --quick -> exit_code 0. devtools verify reached pytest testmon and was interrupted after ~12m without failure output.
@coderabbitai

coderabbitai Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Adds a StructuralQueryUnitInfo dataclass and STRUCTURAL_QUERY_UNIT_REGISTRY to the query DSL module, plus structural_query_units() and structural_query_fields(unit) helpers. Broadens _is_boolean_expression to recognize exists block(...). Wires these registries into shell completion via new candidate generators and COMP_WORDS-based context detection. Tests cover candidate metadata, context switching, per-shell matrix behavior, and DSL compilation.

Changes

Structural Query Unit Registry and Shell Completion

Layer / File(s) Summary
Structural unit registry, helpers, and boolean detection fix
polylogue/archive/query/expression.py
Introduces StructuralQueryUnitInfo dataclass and STRUCTURAL_QUERY_UNIT_REGISTRY mapping action, block, message to their accepted fields and examples. Adds structural_query_units() and structural_query_fields(unit) introspection helpers. Broadens _is_boolean_expression regex to include exists block(...). Updates module docstring and __all__.
Shell completion candidates and context detection
polylogue/cli/shell_completion_values.py
Imports structural registry utilities; implements query_structural_unit_candidates and query_structural_field_candidates producing QueryCompletionCandidate objects; adds COMP_WORDS-based helpers to detect unit-vs-field completion context; wires structural completions as an early-return path in complete_query_expression_fields; exports new functions via __all__.
Tests: DSL compilation, candidate metadata, and per-shell matrix
tests/unit/cli/test_command_aux_runtime.py, tests/unit/cli/test_completion_matrix.py, tests/unit/cli/test_query_expression.py
Unit tests assert structural unit/field candidate metadata (insertion text, kind, source, description), context-switching behavior between unit and field completion levels, per-shell matrix assertions that block(, text:, and type: appear in completions, and DSL compile verification that exists block(type:code) lowers to the correct QueryExistsPredicate tree.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • Build completion on shared query grammar #1844: This PR directly implements the structural query completion infrastructure prescribed by this issue, adding STRUCTURAL_QUERY_UNIT_REGISTRY, structural_query_units(), and structural_query_fields() as the source-of-truth registries and consuming them in shell_completion_values.py to generate structural unit and field candidates.

Possibly related PRs

  • Sinity/polylogue#1861: Extends the same polylogue/archive/query/expression.py DSL surface; this PR builds structural exists <unit>(...) registry and completion on top of that query-expression/compiler infrastructure.
  • Sinity/polylogue#2011: Both PRs extend exists structural predicates and boolean-expression recognition in expression.py; the retrieved PR introduces QueryExistsPredicate execution and this PR adds structural unit registry, exists block(...) boolean detection, and completion wiring.

Poem

🐇 Hoppity-hop, I sniff the shell,
exists block( — oh, what a smell!
I typed exists b and quick as a wink,
The registry said "block(" in a blink!
Now text: and type: appear with grace —
Structural queries, all in their place! 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.46% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: exposing structural grammar metadata for completion functionality.
Description check ✅ Passed The description follows the template with all required sections (Summary, Problem, Solution, Verification, Changelog, Risks) properly completed with concrete details.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/feat/query-completion-grammar-metadata-1844

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.

@Sinity
Sinity merged commit 2d41b05 into master Jun 17, 2026
12 of 13 checks passed
@Sinity
Sinity deleted the feature/feat/query-completion-grammar-metadata-1844 branch June 17, 2026 05:12
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