feat(session): enhance deep search to include session metadata

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Natalie 2026-05-17 06:35:35 -07:00
parent d634acab5e
commit fa872a3718

View file

@ -176,22 +176,47 @@ list_search_on() {
list_sessions_on "$1"
}
# Grep the full content of every Claude session JSONL on <host> for <pat> and
# emit the matching rows in list_sessions_on() format. Used as a fallback when
# the cheap pattern path (first-message snippets + cwd) returns nothing.
# Grep the full content of every Claude session JSONL AND the per-session
# metadata index (~/.claude/sessions/*.json — which holds display names set
# via `claude -n <name>`) on <host> for <pat>, and emit matching rows in
# list_sessions_on() format. Used as a fallback when the cheap pattern path
# (first-message snippets + cwd) returns nothing.
deep_search_on() {
_host=$1; _pat=$2
_q=$(printf %s "$_pat" | sed "s/'/'\\\\''/g")
# Two passes:
# (a) grep every projects/*.jsonl for the pattern → recover uuid via filename
# (b) scan sessions/*.json (per-pid metadata: display name set via `claude -n`)
# via python, extract sessionId of any whose `name` contains the pattern
_py_script='import json, os, sys, glob
pat = os.environ.get("RCLAUDE_PAT", "").lower()
if not pat: sys.exit(0)
for f in glob.glob(os.path.expanduser("~/.claude/sessions/*.json")):
try:
d = json.load(open(f))
except Exception:
continue
name = (d.get("name") or "").lower()
sid = d.get("sessionId") or ""
if pat in name and sid:
print(sid)'
if is_local "$_host"; then
_files=$(grep -l -F -i -- "$_pat" "$HOME/.claude/projects/"*/*.jsonl 2>/dev/null || true)
_uuids_a=$(grep -l -F -i -- "$_pat" "$HOME/.claude/projects/"*/*.jsonl 2>/dev/null \
| awk -F/ '{print $NF}' | sed 's/\.jsonl$//')
_uuids_b=$(RCLAUDE_PAT="$_pat" python3 -c "$_py_script" 2>/dev/null || true)
else
_files=$(ssh -o BatchMode=yes -o ConnectTimeout=5 "$_host" \
"grep -l -F -i -- '$_q' \$HOME/.claude/projects/*/*.jsonl 2>/dev/null" 2>/dev/null || true)
_uuids_a=$(ssh -o BatchMode=yes -o ConnectTimeout=5 "$_host" \
"grep -l -F -i -- '$_q' \$HOME/.claude/projects/*/*.jsonl 2>/dev/null \
| awk -F/ '{print \$NF}' | sed 's/\\.jsonl\$//'" 2>/dev/null || true)
_uuids_b=$(ssh -o BatchMode=yes -o ConnectTimeout=5 "$_host" \
"RCLAUDE_PAT='$_q' python3 -" 2>/dev/null <<PYEOF || true
$_py_script
PYEOF
)
fi
[ -z "$_files" ] && return 0
# Extract bare uuids → alternation regex for awk (single-line, safe).
_re=$(printf '%s\n' "$_files" | awk -F/ '{print $NF}' | sed 's/\.jsonl$//' \
| sort -u | tr '\n' '|' | sed 's/|$//')
_uuids=$(printf '%s\n%s\n' "$_uuids_a" "$_uuids_b" | sort -u | grep -v '^$' || true)
[ -z "$_uuids" ] && return 0
_re=$(printf '%s\n' "$_uuids" | tr '\n' '|' | sed 's/|$//')
[ -z "$_re" ] && return 0
# Bump CLAUDE_PROJECTS_LIMIT for this lookup so older matches aren't lost.
CLAUDE_PROJECTS_LIMIT=5000 list_sessions_on "$_host" \