Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.4 KB
Raw Blame
#!/bin/bash
# One-command LOCAL runner for the legacy plaintext-.pkl RAG migration E2E test.
#
# Boot ORDER is load-bearing: phase-1 (migrate_legacy_docstores) runs only at
# server STARTUP (there is no on-demand endpoint), so the legacy `.pkl` fixtures
# MUST exist on disk BEFORE the server boots. This wrapper encapsulates that:
#
#   1. export a disposable LDR_DATA_DIR (default /tmp/ldr-migtest) and WIPE it,
#   2. run the Python seed (auth user + encrypted DB rows + legacy .faiss/.pkl),
#   3. boot a THROWAWAY server on a spare port (default 5099) — its startup
#      runs phase-1,
#   4. run the Puppeteer test (login drives phase-2; then searches),
#   5. tear the server down by the PID this script launched. (The server is
#      non-dumpable, so a same-user `fuser -k PORT/tcp` cannot find it.)
#
# NEVER touches the user's real dev server on 5001 or their real data dir.
#
# Usage:
#   bash tests/ui_tests/run_legacy_pkl_migration.sh [PORT]
#
# Env overrides:
#   LDR_DATA_DIR   disposable data dir (default /tmp/ldr-migtest)
#   PORT (arg 1)   throwaway server port (default 5099)

set -u
# No job control (see scripts/dev/restart_server.sh): with it, the
# background server would lead its own group, setsid would fork, and $!
# would not be the group cleanup() signals.
set +m

PORT="${1:-5099}"
export LDR_DATA_DIR="${LDR_DATA_DIR:-/tmp/ldr-migtest}"
export HF_HUB_CACHE="${HF_HUB_CACHE:-${LDR_DATA_DIR}/cache/huggingface/hub}"

# Resolve repo root from this script's location.
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
cd "${REPO_ROOT}" || exit 1

if [ "${PORT}" = "5001" ]; then
    echo "Refusing to run on port 5001 (the user's real dev server)." >&2
    exit 2
fi

LOG_FILE="/tmp/ldr_server_${PORT}.log"
SERVER_PID=""
# Set when the server runs in its own process group (setsid available).
SERVER_GROUP=0

# shellcheck disable=SC2329  # invoked indirectly via `trap cleanup EXIT` below
cleanup() {
    [ -n "${SERVER_PID}" ] || return 0
    echo ""
    echo "Tearing down throwaway server on port ${PORT} (PID ${SERVER_PID})..."
    if [ "${SERVER_GROUP}" -eq 1 ]; then
        kill -TERM -- "-${SERVER_PID}" 2>/dev/null
    else
        pkill -TERM -P "${SERVER_PID}" 2>/dev/null
        kill -TERM "${SERVER_PID}" 2>/dev/null
    fi
    for _ in $(seq 1 15); do
        kill -0 "${SERVER_PID}" 2>/dev/null || return 0
        sleep 1
    done
    if [ "${SERVER_GROUP}" -eq 1 ]; then
        kill -KILL -- "-${SERVER_PID}" 2>/dev/null
    else
        pkill -KILL -P "${SERVER_PID}" 2>/dev/null
        kill -KILL "${SERVER_PID}" 2>/dev/null
    fi
    return 0
}
trap cleanup EXIT

echo "=== 1/4  Wipe disposable LDR_DATA_DIR=${LDR_DATA_DIR} ==="
rm -rf "${LDR_DATA_DIR}"
mkdir -p "${LDR_DATA_DIR}"

echo "=== 2/4  Seed legacy .pkl fixtures + encrypted-DB rows (before boot) ==="
if ! pdm run python scripts/ci/prepare_library_embeddings.py; then
    echo "Embedding model preparation FAILED" >&2
    exit 1
fi
if ! pdm run python tests/ui_tests/seed_legacy_pkl_migration.py; then
    echo "Seed FAILED" >&2
    exit 1
fi

echo "=== 3/4  Boot throwaway server on port ${PORT} (runs phase-1 at startup) ==="
if (exec 3<>"/dev/tcp/127.0.0.1/${PORT}") 2>/dev/null; then
    echo "Port ${PORT} is already in use; refusing to start a second server." >&2
    exit 1
fi
launcher=(nohup)
if command -v setsid >/dev/null 2>&1; then
    # Own process group, so cleanup stops `pdm run` and the Python server
    # it starts together. With job control off (`set +m` above) setsid
    # exec's in place, so the group's ID is $!.
    launcher+=(setsid)
    SERVER_GROUP=1
fi
LDR_WEB_PORT="${PORT}" "${launcher[@]}" \
    pdm run python -m local_deep_research.web.app >"${LOG_FILE}" 2>&1 &
SERVER_PID=$!

# Wait for readiness.
ready=0
for i in $(seq 1 60); do
    if curl -fsS --connect-timeout 2 --max-time 5 \
        "http://127.0.0.1:${PORT}/api/v1/health" >/dev/null 2>&1; then
        echo "Server ready after ${i}s"
        ready=1
        break
    fi
    sleep 1
done
if [ "${ready}" -ne 1 ]; then
    echo "Server failed to become ready on port ${PORT}" >&2
    echo "--- last 40 log lines ---"
    tail -40 "${LOG_FILE}" 2>/dev/null || true
    exit 1
fi

echo "=== 4/4  Run Puppeteer migration test (login drives phase-2) ==="
LDR_TEST_URL="http://127.0.0.1:${PORT}" \
    LDR_DATA_DIR="${LDR_DATA_DIR}" \
    node tests/ui_tests/test_legacy_pkl_migration_ci.js
rc=$?

exit "${rc}"