--- name: claude-code:session description: Introspect on your own Claude Code usage and history (session ID, duration, tokens consumed, tool usage patterns, time per project, recent activity summaries, or searching past conversations). Use whenever the user asks about their Claude Code activity ("what's my session ID?", "how many tokens today?", "what did I work on this week?", "find that conversation where I set up X", "am I overusing Bash?"). Do NOT use for general codebase search, git log queries, or arbitrary databases. allowed-tools: - Bash - Read --- # Session Search and analyze Claude Code conversation history via a DuckDB index over JSONL session files. **Current Session ID**: `${CLAUDE_SESSION_ID}` ## Database The session index is a DuckDB database at `$CLAUDE_PLUGIN_DATA/session.duckdb`. The refresh script ensures it is current before querying. ### Refresh Run `refresh.ts` to scan `~/.claude/projects/**/*.jsonl` and update the index. Pass `--refresh` to force a rescan when the user asks for the latest data. The script prints the resolved DB path to stdout. ```bash ${CLAUDE_SKILL_DIR}/scripts/refresh.ts ${CLAUDE_SKILL_DIR}/scripts/refresh.ts --refresh ``` ### Querying After refresh, query the DB with the `duckdb` CLI or any DuckDB client. Named SQL files in `resources/queries/` provide common queries. Use `SET VARIABLE` for parameterization and `getvariable('key')` in SQL. ```bash DB_PATH=$(${CLAUDE_SKILL_DIR}/scripts/refresh.ts) duckdb "$DB_PATH" "SELECT model, SUM(output_tokens) FROM messages WHERE type = 'assistant' GROUP BY model" duckdb "$DB_PATH" < ${CLAUDE_SKILL_DIR}/resources/queries/stats.sql ``` ## Named Queries Built-in queries in `resources/queries/` run by name with `SET VARIABLE` params. Prefer these over writing SQL from scratch. The `project` param matches against the directory name (last path component) using glob syntax: `project=myapp` matches exactly, `project=myapp*` matches the repo and its worktrees. Every query also takes an optional `host` param. Omit it to span every machine (including `local`); pass `host=work` to scope to one imported machine. See [Cross-Machine History](#cross-machine-history). - `search`: find sessions by keyword (ILIKE on `content_text` and `summary`). Params: `query`, `limit`, `after_date`, `before_date`, `project` - `stats`: tool usage breakdown with error rates and aggregate totals. Params: `after_date`, `before_date`, `project` - `errors`: recent tool errors with type filtering. Params: `error_type` (`rejection` or `failure`), `limit`, `after_date`, `before_date`, `project` - `permissions`: tool calls the user rejected. Params: `limit`, `after_date`, `before_date`, `project` - `sandbox`: Bash calls that bypassed the sandbox (`dangerouslyDisableSandbox`), with back-links to prior failed sandboxed calls of the same command. Params: `limit`, `after_date`, `before_date`, `project` - `skills`: skill invocation counts by name. Params: `skill`, `after_date`, `before_date`, `project` - `text-export`: dump cleaned prose from `text_content` (TSV-friendly). Params: `role`, `model` (glob), `after_date`, `before_date`, `project`, `min_chars`. - `phrase-lift`: count a phrase per role/model with per-1M-char rate and assistant-vs-user lift. Params: `phrase`, `after_date`, `before_date`. - `model-summary`: assistant text item/message/char counts per model. Params: `after_date`, `before_date`, `project`. - `schema`: list every column in every table/view. Use this first when you don't know what's available. - `keys`: sample which top-level JSON keys appear in `raw.data` (the unstructured part), with occurrence counts. ## Cross-Machine History Session history copied from another machine is queryable alongside this machine's. Each machine is a `host`: this one is always `local`, and every imported machine gets a label you choose. With nothing imported, the index behaves exactly as the single-machine case. ### Listing hosts ```bash ${CLAUDE_SKILL_DIR}/scripts/hosts.ts ``` Shows each host with its import time, egress policy, last index, rsync source, and a ready-to-run re-sync command. ### Importing a machine Copy the source machine's `~/.claude/projects/` into the import root, then register it. The `!` prefix runs the commands in your own shell, so SSH host-key trust and any 2FA stay in your hands. ```bash mkdir -p ~/.claude/session-imports/