Repository navigation
feat(query): cwd_prefix filter for session-recall - #543
Conversation
Adds `cwd_prefix` as a first-class filter primitive across the spec / plan / record-query / SQL pushdown chain, plus CLI `--cwd-prefix` and MCP `cwd_prefix` parameter on `list_conversations` and `search`. Pushdown uses `json_extract(conversations.provider_meta, '$.working_directories')` + `json_each` so it works against the existing schema with no migration. Claude Code records carry `cwd` per record; the parser already aggregates these into `provider_meta.working_directories`. Codex records do not carry `cwd` directly (only `git.repository_url`), so cwd_prefix only matches Claude Code conversations until a separate codex-side enrichment lands. Why this exists: the SessionStart hook (sinnix) currently uses `--path` which matches conversations that referenced files under a path. That's ~95% accurate for typical sessions but misses pure-text sessions. `--cwd-prefix` is the semantically correct filter. Tests: 11 new tests covering spec → plan → record-query → SQL pushdown → filter chain → CLI plumbing → MCP request shape → SQL escape behaviour. Ref #540 Co-Authored-By: Claude <[email protected]>
📝 WalkthroughWalkthroughThis PR introduces a new Changes
Sequence DiagramsequenceDiagram
actor User
participant CLI
participant QuerySpec
participant QueryPlan
participant StorageLayer
participant SQLFilter
participant Database
User->>CLI: polylogue --cwd-prefix "/home/user" search
CLI->>QuerySpec: from_params(cwd_prefix="/home/user")
QuerySpec->>QueryPlan: to_plan() with cwd_prefix field
QueryPlan->>StorageLayer: list_conversations(cwd_prefix="/home/user")
StorageLayer->>SQLFilter: _build_conversation_filters(cwd_prefix="/home/user")
SQLFilter->>SQLFilter: Extract working_directories from provider_meta<br/>Build LIKE pattern with escape handling
SQLFilter->>Database: Execute WHERE EXISTS (SELECT...)<br/>WHERE working_dir LIKE '%/home/user%'
Database-->>SQLFilter: Filtered conversation records
SQLFilter-->>StorageLayer: Result set
StorageLayer-->>CLI: Matched conversations
CLI-->>User: Display results
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bbe51f0596
ℹ️ 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".
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@tests/unit/core/test_cwd_prefix_filter.py`:
- Around line 34-39: The test test_cwd_prefix_escapes_sql_wildcards should also
assert that the backslash escape character itself is escaped in the LIKE
pattern; modify the assertion for params from checking only r"100\%cool\_dir%"
to also check that any returned param string contains an escaped backslash (e.g.
that the pattern includes "\\\\" as the escaped backslash) for the result of
calling _build_conversation_filters(cwd_prefix="/path/100%cool_dir\\"); update
or add an assertion referencing the same params returned by
_build_conversation_filters to verify the escape character is present and
doubled in the SQL LIKE pattern.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 4855fb61-7dd8-478b-8f22-1e09cfd279cb
📒 Files selected for processing (18)
docs/cli-reference.mdpolylogue/cli/click_app.pypolylogue/cli/click_option_groups.pypolylogue/lib/filter/builder.pypolylogue/lib/query/fields.pypolylogue/lib/query/plan.pypolylogue/lib/query/spec.pypolylogue/mcp/query_contracts.pypolylogue/mcp/server_tools.pypolylogue/protocols.pypolylogue/storage/backends/queries/conversations_reads.pypolylogue/storage/backends/queries/filter_builder.pypolylogue/storage/query_models.pypolylogue/storage/repository/archive/queries.pytests/baselines/showcase/help-main.txttests/data/witnesses/mcp-tool-schemas.jsontests/unit/cli/__snapshots__/test_terminal_snapshots.ambrtests/unit/core/test_cwd_prefix_filter.py
| def test_cwd_prefix_escapes_sql_wildcards() -> None: | ||
| where, params = _build_conversation_filters(cwd_prefix="/path/100%cool_dir") | ||
| assert "ESCAPE" in where | ||
| # The escaped prefix has the wildcards escaped | ||
| assert any(r"100\%cool\_dir%" in str(p) for p in params) | ||
|
|
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
Add a backslash-escape assertion for LIKE patterns.
Current wildcard coverage validates % and _, but not \ (the declared escape character). Adding a \ case would better protect the escaping contract.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@tests/unit/core/test_cwd_prefix_filter.py` around lines 34 - 39, The test
test_cwd_prefix_escapes_sql_wildcards should also assert that the backslash
escape character itself is escaped in the LIKE pattern; modify the assertion for
params from checking only r"100\%cool\_dir%" to also check that any returned
param string contains an escaped backslash (e.g. that the pattern includes
"\\\\" as the escaped backslash) for the result of calling
_build_conversation_filters(cwd_prefix="/path/100%cool_dir\\"); update or add an
assertion referencing the same params returned by _build_conversation_filters to
verify the escape character is present and doubled in the SQL LIKE pattern.
## Summary First slice of #540. Adds the \`cwd_prefix\` filter primitive across the full query stack: \`ConversationQuerySpec\`, \`ConversationQueryPlan\`, \`ConversationRecordQuery\`, SQL pushdown, CLI \`--cwd-prefix\`, MCP \`cwd_prefix\` param on \`list_conversations\` and \`search\`. ## Problem The session-recall workflow needs to filter conversations by the working directory they ran in. Claude Code records carry \`cwd\` per record; the parser already aggregates these into \`provider_meta.working_directories\`. But there was no filter primitive to query against it — \`--path\` filters file paths *referenced* via tool_use, which is a different question. The deployed sinnix SessionStart hook currently uses \`--path\` as a workaround. ## Solution - \`ConversationQuerySpec.cwd_prefix\`, \`ConversationQueryPlan.cwd_prefix\`, \`ConversationRecordQuery.cwd_prefix\` (plumbed via the existing QueryFieldDescriptor system). - \`ConversationFilterBuilderMixin.cwd_prefix(prefix)\` builder method. - SQL pushdown in \`_build_conversation_filters\` using \`EXISTS (SELECT 1 FROM json_each(json_extract(provider_meta, '$.working_directories'))) cwd WHERE cwd.value LIKE ? ESCAPE '\\\\\\'\`. Works against the existing schema; no migration. - CLI flag \`--cwd-prefix\` registered in shared \`click_option_groups\`, surfaces on every query verb. - MCP \`cwd_prefix\` param on \`list_conversations\` + \`search\`. - \`MCPConversationQueryRequest.cwd_prefix\` threaded into \`build_spec\`. - Witness \`tests/data/witnesses/mcp-tool-schemas.json\` regenerated for the new MCP shape. Codex records carry \`git.repository_url\` rather than \`cwd\`, so \`cwd_prefix\` will only match Claude Code conversations until a codex-side enrichment lands. Documented in code comments. ## Verification \`\`\`bash nix develop -c devtools verify # full baseline (mypy + render-all + 5642 tests + topology lints) — green nix develop -c pytest -q tests/unit/core/test_cwd_prefix_filter.py # 11 new tests pass nix develop -c polylogue --plain --cwd-prefix /realm/project/polylogue --sort date --limit 3 list # returns 3 sessions in this repo \`\`\` ## Follow-ups (rest of #540) - \`path\` → \`referenced_path\` rename (clean break, no compat shim). - \`message_type\` filter primitive (recaps surface via \`get_messages(id, message_type=summary)\`). - \`since_session(id)\` filter. - New paginated \`get_messages\` and \`raw_records\` MCP tools. - MCP resources (\`polylogue://conversation/{id}\` etc.). - Pagination consistency across read tools. - CLI mirror of all new params (already started here with \`--cwd-prefix\`). Closes the \`cwd_prefix\` portion of #540. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added `--cwd-prefix` filter option to the CLI, enabling users to narrow conversation results by filtering based on the recorded working directory prefix * **Documentation** * Updated CLI help text and reference documentation to include details about the new working directory prefix filtering option * **Tests** * Added comprehensive test coverage for the new working directory prefix filtering functionality <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Claude <[email protected]>
|
Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.
Only the rows listed above are dispositioned. Held findings remain open. |
Summary
First slice of #540. Adds the `cwd_prefix` filter primitive across the full query stack: `ConversationQuerySpec`, `ConversationQueryPlan`, `ConversationRecordQuery`, SQL pushdown, CLI `--cwd-prefix`, MCP `cwd_prefix` param on `list_conversations` and `search`.
Problem
The session-recall workflow needs to filter conversations by the working directory they ran in. Claude Code records carry `cwd` per record; the parser already aggregates these into `provider_meta.working_directories`. But there was no filter primitive to query against it — `--path` filters file paths referenced via tool_use, which is a different question. The deployed sinnix SessionStart hook currently uses `--path` as a workaround.
Solution
Codex records carry `git.repository_url` rather than `cwd`, so `cwd_prefix` will only match Claude Code conversations until a codex-side enrichment lands. Documented in code comments.
Verification
```bash
nix develop -c devtools verify # full baseline (mypy + render-all + 5642 tests + topology lints) — green
nix develop -c pytest -q tests/unit/core/test_cwd_prefix_filter.py # 11 new tests pass
nix develop -c polylogue --plain --cwd-prefix /realm/project/polylogue --sort date --limit 3 list # returns 3 sessions in this repo
```
Follow-ups (rest of #540)
Closes the `cwd_prefix` portion of #540.
Summary by CodeRabbit
New Features
--cwd-prefixfilter option to the CLI, enabling users to narrow conversation results by filtering based on the recorded working directory prefixDocumentation
Tests