feat(search): orchestrator-led agent swarm to search the vault + Mini App button

Add `npm run search` (mnemazine-kb-search.mjs): a recon → fan-out → fan-in
swarm over the live vault that writes a Russian "Справка" Markdown report.
Recon keyword-scores notes (0 tokens); --deep/MNEMAZINE_DEEP engages a
bounded-pool LLM swarm (one agent per shard, untrusted notes fenced), then
the orchestrator dedups + synthesizes. Local fallback keeps it 0-token by
default, mirroring the rest of the deep pipeline. Self-test included.

Wire it into the Telegram Mini App: the vault is Mac-only and the server is
on the VPS, so the button only enqueues. Server gains POST /api/search
(queue), GET /api/searches (list reports), GET /api/search?name= (read, with
path-escape guard). Mini App gets a search card + report list + text viewer
(rendered as text — no markdown XSS sink). The Mac poller drains the queue,
runs the swarm locally, and rsyncs reports back to the VPS.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Зарубин Филипп
2026-06-19 18:28:37 +03:00
co-authored by Claude Opus 4.8
parent 3b7c85bae5
commit fbe205f3f1
5 changed files with 387 additions and 3 deletions
+15
View File
@@ -11,6 +11,7 @@ REPO="${MNEMAZINE_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
VPS="${MNEMAZINE_VPS:-root@YOUR_VPS_HOST}"
KEY="${MNEMAZINE_VPS_KEY:-$HOME/.ssh/id_rsa}"
REMOTE_INBOX="${MNEMAZINE_REMOTE_INBOX:-/var/www/mnemazine-inbox}"
REMOTE_REPORTS="${MNEMAZINE_REMOTE_REPORTS:-/var/www/mnemazine-reports}"
# ponytail: fail fast if still on the placeholder — beats a confusing ssh error.
if [ "$VPS" = "root@YOUR_VPS_HOST" ]; then
@@ -28,6 +29,20 @@ LOCK="$REPO/.mnemazine/poll.lock"
if ! mkdir "$LOCK" 2>/dev/null; then exit 0; fi # previous tick still running
trap 'rmdir "$LOCK" 2>/dev/null' EXIT
# Search queue (vault is Mac-only, so searches run here). Drain atomically:
# cat + truncate in one ssh. Each line is a JSON {topic}; parse safely in node.
queue="$($SSH "$VPS" "f=$REMOTE_INBOX/.search-queue; if [ -f \"\$f\" ]; then cat \"\$f\"; : > \"\$f\"; fi" 2>/dev/null || true)"
if [ -n "$queue" ]; then
topics="$(printf '%s' "$queue" | node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>{for(const l of d.split("\n")){if(!l.trim())continue;try{const t=JSON.parse(l).topic;if(t)process.stdout.write(t.replace(/\n/g," ")+"\n")}catch{}}})' 2>/dev/null || true)"
cd "$REPO"
while IFS= read -r topic; do
[ -z "$topic" ] && continue
MNEMAZINE_DEEP=1 npm run --silent search -- --topic "$topic" || echo "search failed: $topic" >&2
done <<< "$topics"
# Push reports back so the mini app can read them (reverse channel Mac->VPS).
[ -d "$REPO/reports" ] && rsync -az -e "$SSH" "$REPO/reports/" "$VPS:$REMOTE_REPORTS/" 2>/dev/null || true
fi
run=0; manual=0
# Consume run-now flag atomically: print + delete in one ssh.
flag="$($SSH "$VPS" "f=$REMOTE_INBOX/.run-now; if [ -f \"\$f\" ]; then cat \"\$f\"; rm -f \"\$f\"; fi" 2>/dev/null || true)"