This repository has been archived on 2026-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
ollie-9p/tools/memory_recall

47 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# ollie:parallel read
# ollie:tier cold
# description: Search memories by keyword, tag, or title
# Args: <query>
[ -z "$*" ] && echo "usage: memory_recall <query>" && exit 1
mount="${OLLIE:-$HOME/mnt/ollie}"
mem_dir="$mount/m"
[ ! -d "$mem_dir" ] && echo "error: memory directory does not exist: $mem_dir" && exit 1
query="$*"
max_files=10
max_lines=50
stale_days=14
now=$(date +%s)
# Search filenames and bodies; discard I/O errors on stale inodes
matches=$(grep -ril --include='*.md' "$query" "$mem_dir" 2>/dev/null)
[ -z "$matches" ] && echo "no matches for: $query" && exit 0
total=$(echo "$matches" | wc -l)
count=0
while IFS= read -r file; do
count=$((count + 1))
[ $count -gt $max_files ] && echo "(showing $max_files of $total matched files)" && break
fname=$(basename "$file")
# Extract date from filename instead of stat
ts=${fname%%__*}
if [[ $ts =~ ^([0-9]{4})([0-9]{2})([0-9]{2})T ]]; then
fdate=$(date -d "${BASH_REMATCH[1]}-${BASH_REMATCH[2]}-${BASH_REMATCH[3]}" +%s 2>/dev/null)
[ -n "$fdate" ] && age_days=$(( (now - fdate) / 86400 )) || age_days=0
else
age_days=0
fi
[ $age_days -ge $stale_days ] && status="STALE (${age_days}d)" || status="fresh"
echo "=== $fname [$status] ==="
awk -v max="$max_lines" 'NR<=max{print} NR>max{over++} END{if(over) printf "... (%d lines total, showing first %d)\n", NR, max}' "$file" 2>/dev/null || echo "error: could not read $file"
echo
done <<< "$matches"