feat: sessions search --source/--limit/--after filters (Task 2.6.2 completion) #19

Open
opened 2026-04-24 14:21:14 +02:00 by product-owner · 0 comments

Problem Statement

Task 2.6.2 of terraphim-agent-session-search-tasks.md requires /sessions search to support query + source filter + result display, but the current SessionsSubcommand::Search in crates/terraphim_agent/src/repl/commands.rs only accepts a single query: String with no way to narrow results by connector (Claude / Cursor / Aider / Cline), cap the number of results for an LLM-friendly response, or filter by recency. This forces callers to pipe through external tools or read-all-and-filter, defeating the purpose of an indexed session store for agent workflows.

This complements #14 (/sessions import --source/--since for ingestion) by delivering the equivalent read-path filters needed for --robot mode consumers.

Acceptance Criteria

  • /sessions search "<query>" accepts --source <claude|cursor|aider|cline> (repeatable or comma-separated) to filter by connector source_id
  • --limit <N> caps the number of returned sessions (default 20; hard max 200)
  • --after <ISO-date|duration> filters to sessions with activity after that point (--after 7d or --after 2026-01-01)
  • --before <ISO-date|duration> symmetric upper bound
  • When --robot or --format json is active, output flows through the existing RobotOutput envelope and includes a filters_applied field so agents can echo the effective query
  • Unknown source names produce a helpful error listing available sources (do not silently return empty)
  • Existing bare /sessions search <query> behaviour unchanged (flags are additive)
  • Unit tests cover: no-filter baseline, single-source filter, multi-source union, limit clamp, invalid source, date-range filter, duration-shortcut parsing
  • cargo test -p terraphim_agent sessions passes
  • cargo test -p terraphim_sessions passes
  • cargo clippy -p terraphim_agent -p terraphim_sessions -- -D warnings passes

Proposed Approach

  1. Extend SessionsSubcommand::Search in crates/terraphim_agent/src/repl/commands.rs from Search { query: String } to Search { query: String, sources: Vec<String>, limit: Option<usize>, after: Option<String>, before: Option<String> }. Teach ForgivingParser to accept the new flags (aliases already handle --source).
  2. In crates/terraphim_sessions/src/search.rs, add a SearchOptions { sources, limit, after, before } struct and an overload (or extra method) on SessionService::search_sessions that applies the filters after the Tantivy query (or push down into the Tantivy BooleanQuery if index is active — otherwise post-filter on the cached Session list).
  3. Wire the new flags through the REPL handler in crates/terraphim_agent/src/repl/handler.rs including --robot JSON output that includes filters_applied.
  4. Add a tiny shared parse_relative_or_iso(&str) -> Result<DateTime<Utc>> helper in terraphim_sessions::search (reuses chrono; supports Nd/Nw/Nm/Ny and ISO-8601).

Affected crates: terraphim_agent, terraphim_sessions. No changes to connectors. No index schema changes (filters operate on already-indexed metadata).

Dependencies

None strictly blocking. Related but independent from #14 (import filters) and #5 (Tantivy index build). Works against the current in-memory cache even without Tantivy, and will transparently use Tantivy once #5 merges.

Verification

cargo test -p terraphim_sessions search::options
cargo test -p terraphim_agent --features repl-sessions repl::sessions::search
cargo clippy -p terraphim_agent -p terraphim_sessions --features repl-sessions -- -D warnings

# Manual smoke
cargo run -p terraphim_agent --features repl-sessions -- sessions search "tauri" --source claude --limit 5
cargo run -p terraphim_agent --features repl-sessions -- sessions search "tauri" --after 14d --robot

Scope

~250 lines across the two crates (enum widening + options struct + handler wiring + parser updates + ~7 tests). Single PR. Additive; preserves existing command ergonomics.

## Problem Statement Task 2.6.2 of `terraphim-agent-session-search-tasks.md` requires `/sessions search` to support query + source filter + result display, but the current `SessionsSubcommand::Search` in `crates/terraphim_agent/src/repl/commands.rs` only accepts a single `query: String` with no way to narrow results by connector (Claude / Cursor / Aider / Cline), cap the number of results for an LLM-friendly response, or filter by recency. This forces callers to pipe through external tools or read-all-and-filter, defeating the purpose of an indexed session store for agent workflows. This complements #14 (`/sessions import --source/--since` for ingestion) by delivering the equivalent read-path filters needed for `--robot` mode consumers. ## Acceptance Criteria - [ ] `/sessions search "<query>"` accepts `--source <claude|cursor|aider|cline>` (repeatable or comma-separated) to filter by connector source_id - [ ] `--limit <N>` caps the number of returned sessions (default 20; hard max 200) - [ ] `--after <ISO-date|duration>` filters to sessions with activity after that point (`--after 7d` or `--after 2026-01-01`) - [ ] `--before <ISO-date|duration>` symmetric upper bound - [ ] When `--robot` or `--format json` is active, output flows through the existing RobotOutput envelope and includes a `filters_applied` field so agents can echo the effective query - [ ] Unknown source names produce a helpful error listing available sources (do not silently return empty) - [ ] Existing bare `/sessions search <query>` behaviour unchanged (flags are additive) - [ ] Unit tests cover: no-filter baseline, single-source filter, multi-source union, limit clamp, invalid source, date-range filter, duration-shortcut parsing - [ ] `cargo test -p terraphim_agent sessions` passes - [ ] `cargo test -p terraphim_sessions` passes - [ ] `cargo clippy -p terraphim_agent -p terraphim_sessions -- -D warnings` passes ## Proposed Approach 1. Extend `SessionsSubcommand::Search` in `crates/terraphim_agent/src/repl/commands.rs` from `Search { query: String }` to `Search { query: String, sources: Vec<String>, limit: Option<usize>, after: Option<String>, before: Option<String> }`. Teach `ForgivingParser` to accept the new flags (aliases already handle `--source`). 2. In `crates/terraphim_sessions/src/search.rs`, add a `SearchOptions { sources, limit, after, before }` struct and an overload (or extra method) on `SessionService::search_sessions` that applies the filters after the Tantivy query (or push down into the Tantivy `BooleanQuery` if index is active — otherwise post-filter on the cached `Session` list). 3. Wire the new flags through the REPL handler in `crates/terraphim_agent/src/repl/handler.rs` including `--robot` JSON output that includes `filters_applied`. 4. Add a tiny shared `parse_relative_or_iso(&str) -> Result<DateTime<Utc>>` helper in `terraphim_sessions::search` (reuses chrono; supports `Nd/Nw/Nm/Ny` and ISO-8601). Affected crates: `terraphim_agent`, `terraphim_sessions`. No changes to connectors. No index schema changes (filters operate on already-indexed metadata). ## Dependencies None strictly blocking. Related but independent from #14 (import filters) and #5 (Tantivy index build). Works against the current in-memory cache even without Tantivy, and will transparently use Tantivy once #5 merges. ## Verification ``` cargo test -p terraphim_sessions search::options cargo test -p terraphim_agent --features repl-sessions repl::sessions::search cargo clippy -p terraphim_agent -p terraphim_sessions --features repl-sessions -- -D warnings # Manual smoke cargo run -p terraphim_agent --features repl-sessions -- sessions search "tauri" --source claude --limit 5 cargo run -p terraphim_agent --features repl-sessions -- sessions search "tauri" --after 14d --robot ``` ## Scope ~250 lines across the two crates (enum widening + options struct + handler wiring + parser updates + ~7 tests). Single PR. Additive; preserves existing command ergonomics.
Sign in to join this conversation.