--- name: search description: Search previously exported Cairn sessions, notes, meetings and indexed docs using QMD BM25 or exact ripgrep, or one deep search with --deep. Use when the user says "cairn search", "search my sessions", "search past sessions", "search my meetings", "grep my sessions", or "exact search", or otherwise asks to locate material in personal session history. Do not activate for ordinary web searches, repository code searches, or unrelated uses of find/look up. Requires qmd; rg is optional. argument-hint: "QUERY [-n NUM] [-c COLLECTION] [--full] [--exact] [--deep]" allowed-tools: Bash(qmd:*), Bash(rg:*), Bash(head:*), Bash(cairn recall gaps:*), Bash(cairn recall search:*), Bash(cairn errors:*) --- Read [the harness contract](../../references/harnesses/contract.md) and its relevant Claude or Codex reference from this installed package before host-specific operations. # Search Skill Lightweight mid-conversation search. One query, top results with snippets, done. Unlike `/recall` (which expands queries, fetches full documents, and synthesises a "One Thing"), `/search` is a fast inline lookup - results with snippets, no synthesis. Two engines: - **`qmd search`** (BM25, ranked) - topics and concepts ("webhook processing"). - **`rg`** (ripgrep, exact) - literal strings and regex ("ECONNRESET", `parse_frontmatter`, a file path, a session ID). rg reads the markdown files directly, so it also finds sessions that QMD has not indexed yet. It has no ranking, so it needs the output limits in step 5. ## Usage ``` /search webhook processing # search all collections (BM25) /search analysis pipeline -n 3 # limit results /search onboarding flow -c notes # restrict to one collection /search policy management --full # show full document content /search ECONNRESET --exact # exact match with rg /search "def \w+_frontmatter" --exact # regex with rg /search the thing about vendor terms --deep # one deep search (BM25 + vectors), slower ``` In Codex, type `$search` in place of `/search`; `/recall` below is `$recall` there. ## Workflow 1. **Parse the query and flags** from the user's input: - `-n NUM` - number of results (default: 5) - `-c COLLECTION` - restrict to one collection (see step 2 for valid names) - `--full` - show full document content instead of snippets - `--exact` (alias `--rg`) - use rg instead of BM25 - `--deep` - one deep search (BM25 + vectors) through `cairn recall search`; only when the user passes it - Everything else is the search query. **Choose the engine.** Use rg if `--exact` is given, or if the query is clearly a literal: an identifier, error string, file path, URL, session ID, or regex. Use rg also when the user asks about something from the last hour, which QMD might not have indexed yet. Otherwise use `qmd search`. If rg is not installed (`command not found`), fall back to `qmd search` and say so in one line. Use the deep path only when `--deep` is given; never choose it yourself. `--exact` wins over `--deep`. 2. **Resolve the collection (only if `-c` is given).** Do **not** hardcode or guess collection names - they vary per machine and change as integrations are added. Get the live list: ```bash qmd collection list ``` Use one of the returned names for `-c`. Omit `-c` to search every collection (default). 3. **BM25 path - run the search:** ```bash qmd search "QUERY" -n NUM [-c COLLECTION] ``` Use `qmd search` (BM25), not `qmd query` (hybrid) - BM25 is much faster, and speed is the point for inline lookups. Go to step 6. 4. **Deep path (`--deep` only):** ```bash cairn recall search "QUERY" --deep -n NUM [-c COLLECTION] --json ``` Tell the user in one line that this takes up to about 30 seconds. Present `results` as in step 6, with each hit's `label` (`bm25`, `hybrid` or `bm25+hybrid`) in brackets. If `hybrid.note` is not null, print it as one line under the results. If the command exits non-zero or says `invalid choice`, run the BM25 path of step 3 and say in one line that deep search is not available. Run it at most once per search. 5. **rg path - resolve paths, then search in two passes.** a. **Resolve each collection's directory.** Do **not** hardcode or guess paths. Collections can live outside cairn's `data_root` (for example a user's own notes folder). For each collection to search (all names from `qmd collection list`, or the `-c` one), read the `Path:` line: ```bash qmd collection show COLLECTION ``` Skip a collection whose directory does not exist. Quote each path, because paths can contain spaces. b. **Pass 1 - list matching files, newest first, capped:** ```bash rg -l -i -F -e "QUERY" --type md --sortr modified "PATH1" "PATH2" ... | head -n 20 ``` - `-F` treats the query as literal text. Drop `-F` for a regex query. - `-e` keeps a query that starts with `-` from being read as a flag. Keep it directly before the query. - Drop `-i` if case matters (identifiers, error codes). - `--sortr modified` puts the newest files first. An exported session file changes only when its session changes, so this is a good proxy for recency. - For counts per file instead of names, use `-c` in place of `-l`. c. **Pass 2 - matched lines for the top NUM files only, capped:** ```bash rg -n -i -F -e "QUERY" -M 300 -m 5 -C 1 "FILE1" "FILE2" ... ``` - `-M 300` cuts lines longer than 300 characters. **Never omit it**: a session transcript can hold a single line of several megabytes. - `-m 5` limits matches per file. `-C 1` adds one line of context. **Guardrails.** Never run rg over a whole collection without `-l` or `-c`, and never print matched lines without both `-M` and `-m`. An unbounded rg over a real corpus can return tens of thousands of lines and fill the context window. 6. **Present results** as a compact, scannable list/table. For BM25 hits: title, collection, score, and snippet. For rg hits: file path, collection, date (session files are named `YYYY-MM-DD-.md`), and the matched line. The user wants a quick answer, not a wall of text. If a search returns zero hits, run the gaps report once: ```bash cairn recall gaps --json ``` Use only the gaps in its JSON. If `status` is `degraded`, report: `No match for "QUERY", but coverage is degraded: .` Then add the `fix` of each warn gap. Suggest `/search QUERY --exact` only when `rg.available` is `true` and a gap is `index-lag`, `index-count` or `collection-missing`: rg reads the files QMD has not indexed. If `status` is `clean`, report: `No match for "QUERY" in the indexed collections.` A search with some hits but fewer than `-n` is a normal result: report the hits and do not run the gaps report. If `cairn recall gaps --json` itself exits non-zero, read the `code:` line on stderr and run `cairn errors CODE`; report its next step and stop. A failing `qmd` command has no cairn code: report qmd's own message and suggest `cairn doctor`. 7. **If `--full`** (or the user asks to expand a hit): ```bash qmd get "document-path" -l 100 ``` For an rg hit, use its `qmd://COLLECTION/` form, or read the file directly. Do NOT: expand the query into variants, fetch full documents unless asked, synthesise a "One Thing", run parallel searches across variants, or add `--deep` unless the user passed it - that's `/recall`'s job.