diff --git a/.github/workflows/desktop.yml b/.github/workflows/desktop.yml index ce203dc..388207d 100644 --- a/.github/workflows/desktop.yml +++ b/.github/workflows/desktop.yml @@ -171,6 +171,52 @@ jobs: working-directory: frontend/src-tauri run: cargo test + # Build the Python runtime that release bundles ship, install it the way the + # app does on first launch, and check that `locus serve` comes up. Release + # builds run the same check on every platform before publishing. + runtime-smoke: + if: github.event_name == 'pull_request' + strategy: + fail-fast: false + matrix: + include: + - platform: ubuntu-22.04 + target: x86_64-unknown-linux-gnu + - platform: windows-latest + target: x86_64-pc-windows-msvc + runs-on: ${{ matrix.platform }} + permissions: + contents: read + steps: + - uses: actions/checkout@v7 + with: + # The Locus wheel's version comes from git tags. + fetch-depth: 0 + + - name: Install Rust stable + uses: dtolnay/rust-toolchain@stable + + - name: Rust cache + uses: swatinem/rust-cache@v2 + with: + workspaces: 'rust -> target' + + - name: Install uv + uses: astral-sh/setup-uv@v10.3.0 + with: + version: '0.13.6' + enable-cache: true + + - name: Build and smoke-test the bundled runtime + shell: bash + run: | + cd frontend/src-tauri/scripts + ./download-uv.sh "${{ matrix.target }}" + ./build-runtime.sh 3.12 + suffix="" + [[ "${{ matrix.target }}" == *windows* ]] && suffix=".exe" + ./runtime-smoke.sh "../binaries/uv-${{ matrix.target }}${suffix}" + # Remove stale artifacts from the desktop-edge rolling pre-release so that # only the current build's files are available for download. (The stable # `desktop-latest` channel the installed app polls is never cleaned here.) @@ -359,12 +405,22 @@ jobs: - name: Install Rust stable uses: dtolnay/rust-toolchain@stable with: - targets: ${{ matrix.platform == 'macos-14' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }} + # macOS builds a universal app; Windows ARM64 also builds the x64 + # locus_rust wheel for its emulated Python backend. + targets: ${{ matrix.platform == 'macos-14' && 'aarch64-apple-darwin,x86_64-apple-darwin' || matrix.platform == 'windows-11-arm' && 'x86_64-pc-windows-msvc' || '' }} - name: Rust cache uses: swatinem/rust-cache@v2 with: - workspaces: 'frontend/src-tauri -> target' + workspaces: | + frontend/src-tauri -> target + rust -> target + + - name: Install uv + uses: astral-sh/setup-uv@v10.3.0 + with: + version: '0.13.6' + enable-cache: true - name: Setup Node.js uses: actions/setup-node@v7 @@ -405,6 +461,41 @@ jobs: ./download-ollama.sh "$OLLAMA_TARGET" fi + # Packaged installs set up their Python backend on first launch from + # these files with the bundled uv, so users need no git, Python or Rust. + # The smoke test installs them the same way and starts `locus serve`. + - name: Bundle uv and the Python runtime + shell: bash + run: | + cd frontend/src-tauri/scripts + case "${{ matrix.platform }}" in + macos-14) + ./download-uv.sh universal-apple-darwin + for arch in aarch64 x86_64; do + cp ../binaries/uv-universal-apple-darwin "../binaries/uv-${arch}-apple-darwin" + done + UV_SIDECAR=../binaries/uv-universal-apple-darwin + ./build-runtime.sh 3.12 universal2-apple-darwin + ;; + windows-11-arm) + ./download-uv.sh aarch64-pc-windows-msvc + UV_SIDECAR=../binaries/uv-aarch64-pc-windows-msvc.exe + # The Python stack runs under x64 emulation on Windows ARM64. + ./build-runtime.sh cpython-3.12-windows-x86_64-none x86_64-pc-windows-msvc + ;; + windows-latest) + ./download-uv.sh x86_64-pc-windows-msvc + UV_SIDECAR=../binaries/uv-x86_64-pc-windows-msvc.exe + ./build-runtime.sh 3.12 + ;; + *) + ./download-uv.sh x86_64-unknown-linux-gnu + UV_SIDECAR=../binaries/uv-x86_64-unknown-linux-gnu + ./build-runtime.sh 3.12 + ;; + esac + ./runtime-smoke.sh "$UV_SIDECAR" + - name: Determine release info id: release-info shell: bash @@ -496,12 +587,21 @@ jobs: version: process.env.TAURI_VERSION, bundle: { createUpdaterArtifacts: true, - externalBin: ['binaries/ollama'], + externalBin: ['binaries/ollama', 'binaries/uv'], }, }; - if (process.env.OLLAMA_TARGET === 'aarch64-pc-windows-msvc') { - config.bundle.resources = { 'binaries/ollama-runtime-aarch64-pc-windows-msvc/': './' }; - } + // download-ollama.sh names the runtime after the target it fetched. + const ollamaTarget = process.env.OLLAMA_TARGET || + { macOS: 'universal-apple-darwin', Linux: 'x86_64-unknown-linux-gnu' }[process.env.RUNNER_OS]; + assert.ok(ollamaTarget, `No Ollama runtime target for ${process.env.RUNNER_OS}`); + config.bundle.resources = { + // The Python runtime the app installs on first launch. + 'binaries/runtime/': 'runtime/', + // Ollama's inference libraries: beside ollama.exe on Windows, and + // under its working directory on macOS and Linux. + [`binaries/ollama-runtime-${ollamaTarget}/`]: + process.env.RUNNER_OS === 'Windows' ? './' : 'ollama-runtime/', + }; writeFileSync('frontend/src-tauri/tauri.release.conf.json', JSON.stringify(config)); NODE diff --git a/README.md b/README.md index c6947ff..78193a3 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ Download the installer for your platform from the [**latest release**](https://g | **macOS (Apple Silicon and Intel)** | `Locus__universal.dmg` | | **Linux** | `Locus__amd64.AppImage`, `.deb`, or `.rpm` | +On first launch, choose **Ollama** to run models on your PC (or point Locus at your own OpenAI-compatible server). Locus then sets itself up: it installs its private Python runtime, starts the bundled Ollama, and downloads a model sized for your RAM. You don't need to install git, Python, Rust, or Ollama yourself. First launch needs an internet connection and takes a few minutes; later launches start in seconds. + The desktop app checks for new releases and offers to install them. > [!NOTE] diff --git a/docs/downloads.md b/docs/downloads.md index c087948..75191d2 100644 --- a/docs/downloads.md +++ b/docs/downloads.md @@ -12,14 +12,14 @@ Locus runs entirely on your hardware. Choose the interface that fits your workfl ## Desktop App The desktop app is a native window for the Locus chat UI. All inference and backend -processing happens on your local machine — the app connects to the backend you start locally. - -!!! info "Backend required" - Start the backend before opening the desktop app. The quickstart script handles everything: - ```bash - git clone https://github.com/aryanjsx/Locus.git && cd Locus - ./scripts/quickstart.sh - ``` +processing happens on your local machine, and the app sets everything up itself. + +!!! info "First launch" + On first launch, choose **Ollama** (runs models on your PC) or your own + OpenAI-compatible server. Locus then installs its private Python runtime, + starts the bundled Ollama, and downloads a model sized for your RAM. This + needs an internet connection and takes a few minutes; later launches start + in seconds. You don't need to install git, Python, Rust, or Ollama yourself. ### Download diff --git a/docs/getting-started/installation.md b/docs/getting-started/installation.md index feeeaba..2710fc6 100644 --- a/docs/getting-started/installation.md +++ b/docs/getting-started/installation.md @@ -78,19 +78,11 @@ Then open [http://localhost:5173](http://localhost:5173). ## Desktop App The desktop app is a native window for the Locus chat UI. All inference and backend -processing happens on your local machine — the app connects to the backend you start locally. +processing happens on your local machine, and the app sets everything up itself. ### Setup -**Step 1.** Start the backend (same as Browser App): - -```bash -git clone https://github.com/aryanjsx/Locus.git -cd Locus -./scripts/quickstart.sh -``` - -**Step 2.** Download and open the desktop app from the +**Step 1.** Download and open the desktop app from the [:material-download: **latest release**](https://github.com/aryanjsx/Locus/releases/latest): | Platform | File | @@ -102,6 +94,24 @@ cd Locus | Linux (RPM) | `Locus--1.x86_64.rpm` | | Linux (AppImage) | `Locus__amd64.AppImage` | +**Step 2.** On first launch, choose **Ollama** to run models on your PC, or +point Locus at your own OpenAI-compatible server. Locus then sets itself up: + +1. Installs its private Python runtime in `~/.locus/desktop-runtime`, including + a Python it downloads for itself. +2. Starts the bundled Ollama. If you already have Ollama installed, Locus uses + that instead, keeping CUDA acceleration if you have it. +3. Downloads a model sized for your RAM and starts the Locus server. + +First launch needs an internet connection and takes a few minutes. Later +launches start in seconds. You don't need to install git, Python, Rust, or +Ollama yourself. If setup fails, relaunch Locus to retry; to start over, +delete `~/.locus/desktop-runtime`. + +The bundled Ollama runs models on your CPU, with Vulkan GPU acceleration on +Windows and Linux and Metal on Apple Silicon Macs. For NVIDIA CUDA acceleration, install +[Ollama](https://ollama.com/download) yourself; Locus will use it. + The app connects to `http://localhost:8000` automatically. !!! warning "macOS: the app isn't signed by Apple yet" diff --git a/docs/index.md b/docs/index.md index bdc7f39..881bf8c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -53,18 +53,11 @@ Locus is that stack. It is a framework for local-first personal AI, built around === "Desktop App" - The desktop app is a native window for the Locus UI. - The backend (Ollama + inference) runs on your machine — start it first, then open the app. + The desktop app is a native window for the Locus UI. It runs everything + on your machine and sets itself up on first launch: its Python runtime, + the bundled Ollama, and a model sized for your RAM. - **Step 1.** Start the backend: - - ```bash - git clone https://github.com/aryanjsx/Locus.git - cd Locus - ./scripts/quickstart.sh - ``` - - **Step 2.** Download and open the desktop app: + Download and open the desktop app: [Download the desktop app](https://github.com/aryanjsx/Locus/releases/latest){ .md-button .md-button--primary } diff --git a/frontend/src-tauri/binaries/.gitignore b/frontend/src-tauri/binaries/.gitignore index 7486442..cda5572 100644 --- a/frontend/src-tauri/binaries/.gitignore +++ b/frontend/src-tauri/binaries/.gitignore @@ -1,3 +1,5 @@ -# Downloaded sidecar binaries — platform-specific, not committed +# Downloaded sidecar binaries and built runtime — platform-specific, not committed ollama-* +uv-* +runtime/ !.gitignore diff --git a/frontend/src-tauri/scripts/build-runtime.sh b/frontend/src-tauri/scripts/build-runtime.sh new file mode 100755 index 0000000..ea01a07 --- /dev/null +++ b/frontend/src-tauri/scripts/build-runtime.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# +# Build the Python runtime bundled with the desktop app into +# frontend/src-tauri/binaries/runtime/. On first launch the app installs it +# with the bundled uv, so end users need no git, Python, or Rust toolchain. +# +# Usage: +# ./build-runtime.sh [cargo-target] +# ./build-runtime.sh 3.12 +# ./build-runtime.sh 3.12 universal2-apple-darwin +# ./build-runtime.sh cpython-3.12-windows-x86_64-none x86_64-pc-windows-msvc +# +# Output: +# requirements.txt third-party dependencies, hash-locked from uv.lock +# locus_assistant-*.whl, locus_rust-*.whl +# manifest.json the Python request and file names the app installs +# +# Requires uv, a Rust toolchain for the target, and the repository's git +# history (the Locus version comes from git tags). + +set -euo pipefail + +PYTHON_REQUEST="${1:?usage: build-runtime.sh [cargo-target]}" +CARGO_TARGET="${2:-}" + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" +OUT_DIR="$SCRIPT_DIR/../binaries/runtime" +rm -rf "$OUT_DIR" +mkdir -p "$OUT_DIR" +OUT_DIR="$(cd "$OUT_DIR" && pwd)" +cd "$REPO_ROOT" + +# The rust/ workspace pins an MSRV toolchain without the cross targets the +# release jobs install on stable. +export RUSTUP_TOOLCHAIN="${RUSTUP_TOOLCHAIN:-stable}" + +echo "Exporting locked requirements..." +uv export --frozen --no-dev --no-header --no-emit-project \ + --no-emit-package locus-rust \ + --extra desktop --extra inference-cloud --extra inference-google \ + --format requirements-txt -o "$OUT_DIR/requirements.txt" + +echo "Building the Locus wheel..." +uv build --wheel --out-dir "$OUT_DIR" + +echo "Building the locus_rust wheel for Python $PYTHON_REQUEST..." +uv python install "$PYTHON_REQUEST" +PYTHON_BIN="$(uv python find --managed-python "$PYTHON_REQUEST")" +maturin_args=( + build --release + --manifest-path rust/crates/locus-python/Cargo.toml + --interpreter "$PYTHON_BIN" + --out "$OUT_DIR" +) +if [ -n "$CARGO_TARGET" ]; then + maturin_args+=(--target "$CARGO_TARGET") +fi +uvx --from 'maturin>=1.12.6,<2' maturin "${maturin_args[@]}" + +"$PYTHON_BIN" - "$OUT_DIR" "$PYTHON_REQUEST" <<'PY' +import json +import sys +from pathlib import Path + +out, python = Path(sys.argv[1]), sys.argv[2] +wheels = sorted(p.name for p in out.glob("*.whl")) +for prefix in ("locus_assistant-", "locus_rust-"): + found = [w for w in wheels if w.startswith(prefix)] + if len(found) != 1: + sys.exit(f"expected one {prefix}*.whl in {out}, found {found}") +manifest = {"python": python, "requirements": "requirements.txt", "wheels": wheels} +(out / "manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8") +print(json.dumps(manifest, indent=2)) +PY + +ls -lh "$OUT_DIR" diff --git a/frontend/src-tauri/scripts/download-ollama.sh b/frontend/src-tauri/scripts/download-ollama.sh index f603e8b..06eef49 100644 --- a/frontend/src-tauri/scripts/download-ollama.sh +++ b/frontend/src-tauri/scripts/download-ollama.sh @@ -63,10 +63,12 @@ case "$TARGET" in *windows*) SUFFIX=".exe" ;; esac OUT_FILE="$BINARIES_DIR/ollama-${TARGET}${SUFFIX}" +# Inference runs in helper binaries and libraries that ship beside the CLI +# (llama-server, libllama, the ggml CPU backends). Without them Ollama starts +# but can't load a model, so every target bundles this runtime directory. RUNTIME_DIR="$BINARIES_DIR/ollama-runtime-${TARGET}" -# ARM64 inference also needs the archive's DLLs and lib/ollama runners. -if [ -f "$OUT_FILE" ] && { [ "$TARGET" != "aarch64-pc-windows-msvc" ] || [ -d "$RUNTIME_DIR/lib/ollama" ]; }; then +if [ -f "$OUT_FILE" ] && [ -d "$RUNTIME_DIR" ]; then echo "Already exists: $OUT_FILE" echo "Delete it first to re-download." exit 0 @@ -151,22 +153,58 @@ if [ "$TARGET" = "universal-apple-darwin" ]; then fi fi -if [ "$TARGET" = "aarch64-pc-windows-msvc" ]; then - if [ ! -d "$OLLAMA_TMP_DIR/lib/ollama" ]; then - echo "Windows Ollama archive is missing its lib/ollama inference runtime" - exit 1 - fi - # Preserve the release layout beside the renamed sidecar. The release - # config maps this directory beside the installed ollama.exe. - rm -rf "$RUNTIME_DIR" - mkdir -p "$RUNTIME_DIR" - for resource in "$OLLAMA_TMP_DIR"/*; do - case "$(basename "$resource")" in - ollama.exe|ollama-archive) continue ;; - esac - cp -R "$resource" "$RUNTIME_DIR/" - done -fi +# CUDA and ROCm add 1-2 GB; CPU and Vulkan cover most machines. Users who want +# CUDA can install Ollama themselves, and the app prefers that install. +drop_heavy_gpu_backends() { + find "$1" -maxdepth 1 -type d \( -name 'cuda*' -o -name 'rocm*' -o -name 'mlx_cuda*' \) \ + -exec rm -rf {} + +} + +rm -rf "$RUNTIME_DIR" +mkdir -p "$RUNTIME_DIR" +case "$TARGET" in + *windows*) + if [ ! -d "$OLLAMA_TMP_DIR/lib/ollama" ]; then + echo "Windows Ollama archive is missing its lib/ollama inference runtime" + exit 1 + fi + # Preserve the release layout beside the renamed sidecar. The release + # config maps this directory beside the installed ollama.exe, where + # Ollama looks for lib/ollama. + for resource in "$OLLAMA_TMP_DIR"/*; do + case "$(basename "$resource")" in + ollama.exe|ollama-archive) continue ;; + esac + cp -R "$resource" "$RUNTIME_DIR/" + done + drop_heavy_gpu_backends "$RUNTIME_DIR/lib/ollama" + ;; + *apple-darwin) + # The macOS archive keeps its helpers beside the CLI. The app starts + # Ollama with this directory as its working directory, and Ollama + # searches dist/darwin there. Copy symlinks as files so the bundle + # doesn't depend on them surviving packaging. + mkdir -p "$RUNTIME_DIR/dist/darwin" + for resource in "$OLLAMA_TMP_DIR"/*; do + case "$(basename "$resource")" in + ollama|ollama-archive|*LICENSE*) continue ;; + esac + cp -RL "$resource" "$RUNTIME_DIR/dist/darwin/" + done + ;; + *linux*) + if [ ! -d "$OLLAMA_TMP_DIR/lib/ollama" ]; then + echo "Linux Ollama archive is missing its lib/ollama inference runtime" + exit 1 + fi + # Ollama searches build/lib/ollama under its working directory, which + # the app sets to this directory. + mkdir -p "$RUNTIME_DIR/build/lib" + cp -RL "$OLLAMA_TMP_DIR/lib/ollama" "$RUNTIME_DIR/build/lib/" + drop_heavy_gpu_backends "$RUNTIME_DIR/build/lib/ollama" + ;; +esac +echo "Bundled runtime: $RUNTIME_DIR ($(du -sh "$RUNTIME_DIR" | cut -f1))" cp "$OLLAMA_BIN" "$OUT_FILE" chmod +x "$OUT_FILE" diff --git a/frontend/src-tauri/scripts/download-uv.sh b/frontend/src-tauri/scripts/download-uv.sh new file mode 100755 index 0000000..faea3fb --- /dev/null +++ b/frontend/src-tauri/scripts/download-uv.sh @@ -0,0 +1,95 @@ +#!/usr/bin/env bash +# +# Download the uv binary for the target platform and place it in the Tauri +# binaries/ directory so it can be bundled as an externalBin sidecar. The +# desktop app uses it on first launch to install its Python runtime. +# +# Usage: +# ./download-uv.sh x86_64-pc-windows-msvc +# ./download-uv.sh aarch64-pc-windows-msvc +# ./download-uv.sh x86_64-unknown-linux-gnu +# ./download-uv.sh universal-apple-darwin +# +# Each archive is checked against the .sha256 file published with the release. + +set -euo pipefail + +# Keep in step with the uv the release workflow builds the runtime with. +UV_VERSION="${UV_VERSION:-0.13.6}" +RELEASE_URL="https://github.com/astral-sh/uv/releases/download/${UV_VERSION}" + +BINARIES_DIR="$(cd "$(dirname "$0")/../binaries" 2>/dev/null && pwd || echo "$(dirname "$0")/../binaries")" +mkdir -p "$BINARIES_DIR" + +TARGET="${1:?usage: download-uv.sh }" +echo "Target triple: $TARGET" + +UV_TMP_DIR="$(mktemp -d)" +trap 'rm -rf "$UV_TMP_DIR"' EXIT + +sha256_of() { + if command -v sha256sum &>/dev/null; then + sha256sum "$1" | awk '{print $1}' + else + shasum -a 256 "$1" | awk '{print $1}' + fi +} + +# Download, verify and extract the uv binary for one target triple. +# Prints the path of the extracted binary. +fetch_uv() { + local triple="$1" ext="tar.gz" exe="" + case "$triple" in + *windows*) ext="zip"; exe=".exe" ;; + esac + local asset="uv-${triple}.${ext}" + local dir="$UV_TMP_DIR/$triple" + mkdir -p "$dir" + + echo "Downloading: $RELEASE_URL/$asset" >&2 + curl -fsSL "$RELEASE_URL/$asset" -o "$dir/$asset" + curl -fsSL "$RELEASE_URL/$asset.sha256" -o "$dir/$asset.sha256" + local expected actual + expected="$(awk '{print $1}' "$dir/$asset.sha256")" + actual="$(sha256_of "$dir/$asset")" + if [ "$expected" != "$actual" ]; then + echo "Checksum mismatch for $asset: expected $expected, got $actual" >&2 + exit 1 + fi + + case "$ext" in + zip) unzip -q "$dir/$asset" -d "$dir" ;; + *) tar xzf "$dir/$asset" -C "$dir" ;; + esac + local bin + bin="$(find "$dir" -type f -name "uv${exe}" | head -1)" + if [ -z "$bin" ]; then + echo "Could not find uv${exe} in $asset" >&2 + exit 1 + fi + echo "$bin" +} + +# Tauri externalBin naming: -[.exe] +case "$TARGET" in + universal-apple-darwin) + # Tauri's universal macOS bundle needs both architectures in one file. + arm="$(fetch_uv aarch64-apple-darwin)" + x64="$(fetch_uv x86_64-apple-darwin)" + OUT_FILE="$BINARIES_DIR/uv-universal-apple-darwin" + lipo -create "$arm" "$x64" -output "$OUT_FILE" + lipo "$OUT_FILE" -verify_arch arm64 x86_64 + ;; + *windows*) + OUT_FILE="$BINARIES_DIR/uv-${TARGET}.exe" + cp "$(fetch_uv "$TARGET")" "$OUT_FILE" + ;; + *) + OUT_FILE="$BINARIES_DIR/uv-${TARGET}" + cp "$(fetch_uv "$TARGET")" "$OUT_FILE" + ;; +esac + +chmod +x "$OUT_FILE" +echo "Saved to: $OUT_FILE" +ls -lh "$OUT_FILE" diff --git a/frontend/src-tauri/scripts/runtime-smoke.sh b/frontend/src-tauri/scripts/runtime-smoke.sh new file mode 100755 index 0000000..7ae370d --- /dev/null +++ b/frontend/src-tauri/scripts/runtime-smoke.sh @@ -0,0 +1,107 @@ +#!/usr/bin/env bash +# +# Install the bundled runtime the way the desktop app does on first launch, +# then start `locus serve` against a stand-in Ollama API and check /health. +# Keep the uv arguments in step with `install_steps` in src/runtime.rs. +# +# Usage: +# ./runtime-smoke.sh [uv-binary] +# +# Expects build-runtime.sh to have filled frontend/src-tauri/binaries/runtime. + +set -euo pipefail + +UV_BIN="${1:-uv}" +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +BUNDLE="$(cd "$SCRIPT_DIR/../binaries/runtime" && pwd)" +HOME_DIR="$(mktemp -d)" +OLLAMA_PORT=11998 +SERVE_PORT=8799 + +cleanup() { + [ -n "${SERVE_PID:-}" ] && kill "$SERVE_PID" 2>/dev/null || true + [ -n "${OLLAMA_PID:-}" ] && kill "$OLLAMA_PID" 2>/dev/null || true + rm -rf "$HOME_DIR" +} +trap cleanup EXIT + +case "$(uname -s)" in + MINGW*|MSYS*|CYGWIN*) BIN_DIR="Scripts"; EXE=".exe" ;; + *) BIN_DIR="bin"; EXE="" ;; +esac +VENV="$HOME_DIR/venv" +PYTHON="$VENV/$BIN_DIR/python$EXE" + +export UV_PYTHON_INSTALL_DIR="$HOME_DIR/python" +export UV_CACHE_DIR="$HOME_DIR/cache" +export UV_NO_CONFIG=1 +export UV_PYTHON_DOWNLOADS=automatic + +read_manifest() { + "$UV_BIN" run --no-project --python 3.12 python -c \ + "import json,sys; m=json.load(open(sys.argv[1])); print(m[sys.argv[2]] if isinstance(m[sys.argv[2]], str) else '\n'.join(m[sys.argv[2]]))" \ + "$BUNDLE/manifest.json" "$1" | tr -d '\r' +} +PYTHON_REQUEST="$(read_manifest python)" +REQUIREMENTS="$BUNDLE/$(read_manifest requirements)" +WHEELS=() +while IFS= read -r wheel; do + WHEELS+=("$BUNDLE/$wheel") +done < <(read_manifest wheels) + +echo "== Installing the bundled runtime (Python $PYTHON_REQUEST)" +"$UV_BIN" venv --python "$PYTHON_REQUEST" --managed-python "$VENV" +"$UV_BIN" pip install --python "$PYTHON" --no-deps --require-hashes -r "$REQUIREMENTS" +"$UV_BIN" pip install --python "$PYTHON" --no-deps "${WHEELS[@]}" +"$PYTHON" -c "import locus, locus_rust" +"$VENV/$BIN_DIR/locus$EXE" --version + +echo "== Starting locus serve against a stand-in Ollama API" +"$PYTHON" - "$OLLAMA_PORT" <<'PY' & +import json, sys +from http.server import BaseHTTPRequestHandler, HTTPServer + +MODEL = {"name": "qwen3:0.6b", "model": "qwen3:0.6b", "size": 1, "details": {"family": "qwen3"}} + +class Handler(BaseHTTPRequestHandler): + def _send(self, body): + data = json.dumps(body).encode() + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def do_GET(self): + self._send({"models": [MODEL]} if self.path.startswith("/api/tags") else {"version": "0.0.0"}) + + def do_POST(self): + self._send({"model": MODEL["name"], "message": {"role": "assistant", "content": "ok"}, "done": True}) + + def log_message(self, *args): + pass + +HTTPServer(("127.0.0.1", int(sys.argv[1])), Handler).serve_forever() +PY +OLLAMA_PID=$! + +LOCUS_HOME="$HOME_DIR/locus-home" OLLAMA_HOST="http://127.0.0.1:$OLLAMA_PORT" \ + "$VENV/$BIN_DIR/locus$EXE" serve --port "$SERVE_PORT" \ + --engine ollama --model qwen3:0.6b --agent simple > "$HOME_DIR/serve.log" 2>&1 & +SERVE_PID=$! + +for _ in $(seq 1 120); do + code="$(curl -s -m 2 -o /dev/null -w '%{http_code}' "http://127.0.0.1:$SERVE_PORT/health" || true)" + if [ "$code" = "200" ]; then + echo "locus serve is healthy" + exit 0 + fi + if ! kill -0 "$SERVE_PID" 2>/dev/null; then + break + fi + sleep 1 +done + +echo "locus serve did not become healthy (last HTTP status: ${code:-none})" +tail -40 "$HOME_DIR/serve.log" +exit 1 diff --git a/frontend/src-tauri/src/lib.rs b/frontend/src-tauri/src/lib.rs index 6cec8f6..5e9a5e5 100644 --- a/frontend/src-tauri/src/lib.rs +++ b/frontend/src-tauri/src/lib.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::sync::{Arc, OnceLock}; use std::time::Duration; use tauri::menu::{MenuBuilder, MenuItemBuilder}; use tauri::tray::TrayIconBuilder; @@ -6,6 +6,13 @@ use tauri::Manager; use tauri_plugin_autostart::MacosLauncher; use tokio::sync::Mutex; +mod runtime; + +/// The app's resource directory, recorded at startup. Release bundles keep +/// the Python runtime (`runtime/`) and Ollama's libraries (`ollama-runtime/`) +/// here. +static RESOURCE_DIR: OnceLock = OnceLock::new(); + const OLLAMA_PORT: u16 = 11434; const JARVIS_PORT: u16 = 8000; #[cfg(not(all(target_os = "windows", target_arch = "aarch64")))] @@ -188,6 +195,19 @@ fn home_dir() -> String { .unwrap_or_default() } +/// A sidecar binary (`ollama`, `uv`) that Tauri installed beside the desktop +/// executable. +fn sidecar_beside_exe(name: &str) -> Option { + let exe = std::env::current_exe().ok()?; + let file = if cfg!(target_os = "windows") { + format!("{name}.exe") + } else { + name.to_string() + }; + let path = exe.parent()?.join(file); + path.is_file().then(|| path.to_string_lossy().into_owned()) +} + /// Resolve full path to a binary by checking common locations. /// macOS .app bundles don't inherit the shell PATH, so we probe manually. fn resolve_bin(name: &str) -> String { @@ -197,13 +217,8 @@ fn resolve_bin(name: &str) -> String { // Prefer the bundled runtime over an unrelated Ollama install on PATH. #[cfg(all(target_os = "windows", target_arch = "aarch64"))] if name == "ollama" { - if let Ok(exe) = std::env::current_exe() { - if let Some(dir) = exe.parent() { - let sidecar = dir.join("ollama.exe"); - if sidecar.is_file() { - return sidecar.to_string_lossy().into_owned(); - } - } + if let Some(sidecar) = sidecar_beside_exe(name) { + return sidecar; } } @@ -247,6 +262,12 @@ fn resolve_bin(name: &str) -> String { } } + // A user-installed copy (above) wins; otherwise use the bundled sidecar, + // which isn't on PATH inside a macOS .app or a Linux package. + if let Some(sidecar) = sidecar_beside_exe(name) { + return sidecar; + } + // Fallback: ask the OS to find it on PATH. // On Windows this uses `where.exe`, on Unix `which`. #[cfg(target_os = "windows")] @@ -391,6 +412,143 @@ fn project_root_near_executable(exe: &std::path::Path) -> Option>, +) -> Result { + if explicit_root || inside_checkout { + return Ok(BackendRuntime::Source); + } + match bundled { + Some(Ok(rt)) => Ok(BackendRuntime::Managed(rt)), + Some(Err(err)) => Err(err), + None => Ok(BackendRuntime::Source), + } +} + +fn select_backend_runtime() -> Result { + let explicit_root = std::env::var("LOCUS_ROOT") + .map(|root| std::path::Path::new(&root).join("pyproject.toml").exists()) + .unwrap_or(false); + let inside_checkout = std::env::current_exe() + .ok() + .and_then(|exe| project_root_near_executable(&exe)) + .is_some(); + let bundled = RESOURCE_DIR.get().and_then(|dir| runtime::bundled_runtime(dir)); + choose_backend_runtime(explicit_root, inside_checkout, bundled) +} + +/// Ollama's bundled CPU/GPU libraries on macOS and Linux. Ollama looks for +/// them under its working directory (`build/lib/ollama`, or `dist/darwin` on +/// macOS); Windows bundles keep them beside the executable instead. +fn bundled_ollama_runtime_dir() -> Option { + let dir = RESOURCE_DIR.get()?.join("ollama-runtime"); + dir.is_dir().then_some(dir) +} + +/// The program, leading arguments and working directory that run `locus`. +struct ServeTarget { + program: String, + prefix: Vec, + cwd: std::path::PathBuf, + managed: bool, +} + +impl ServeTarget { + fn source(uv_bin: &str, root: std::path::PathBuf) -> Self { + Self { + program: uv_bin.to_string(), + prefix: vec!["run".into(), "locus".into()], + cwd: root, + managed: false, + } + } + + fn managed(runtime_home: &std::path::Path) -> Self { + Self { + program: runtime::venv_locus(&runtime::venv_dir(runtime_home)) + .display() + .to_string(), + prefix: Vec::new(), + cwd: runtime_home.to_path_buf(), + managed: true, + } + } + + /// The `locus` invocation to show users, e.g. `uv run locus serve ...`. + fn display_command(&self, args: &[String]) -> String { + let program = if self.managed { self.program.as_str() } else { "uv" }; + std::iter::once(program) + .chain(self.prefix.iter().map(String::as_str)) + .chain(args.iter().map(String::as_str)) + .collect::>() + .join(" ") + } + + /// What to check when the server won't start. + fn checklist(&self) -> String { + if self.managed { + format!( + "1. First-launch setup finished in {}\n\ + 2. If it keeps failing, delete that folder and relaunch Locus to reinstall it", + self.cwd.display(), + ) + } else { + format!( + "1. uv is installed ({})\n\ + 2. The Locus repo is at {}\n\ + 3. 'uv sync' completes in that directory", + self.program, + self.cwd.display(), + ) + } + } +} + +/// Install the bundled runtime on first launch (or after an app update) and +/// return how to run its `locus`. +async fn prepare_managed_runtime( + rt: &runtime::BundledRuntime, + uv_bin: &str, + status: &SharedStatus, +) -> Result { + let home = runtime::runtime_home(&home_dir()); + if !runtime::is_installed(&home, rt) { + runtime::reset_install(&home)?; + let uv = std::path::PathBuf::from(uv_bin); + for step in runtime::install_steps(rt, &home) { + status.lock().await.detail = step.detail.into(); + runtime::run_step(&uv, &step.args, &home, prepare_subprocess_for_appimage) + .await + .map_err(|out| runtime::format_install_failure(&home, step.name, &out))?; + } + status.lock().await.detail = "Checking the installation...".into(); + let python = runtime::venv_python(&runtime::venv_dir(&home)); + runtime::run_step( + &python, + &runtime::verify_args(), + &home, + prepare_subprocess_for_appimage, + ) + .await + .map_err(|out| runtime::format_install_failure(&home, "checking the installation", &out))?; + runtime::mark_installed(&home, rt)?; + } + Ok(ServeTarget::managed(&home)) +} + // --------------------------------------------------------------------------- // BackendManager — owns the Ollama + Locus server child processes // --------------------------------------------------------------------------- @@ -1049,6 +1207,78 @@ fn check_jarvis_port_available() -> Result<(), String> { // Backend boot sequence (runs in background after app launch) // --------------------------------------------------------------------------- +/// Prepare a source checkout for `uv run locus`: check for Rust, run +/// `uv sync`, and confirm the native extension imports. +async fn prepare_source_runtime( + root: &std::path::Path, + uv_bin: &str, + status: &SharedStatus, +) -> Result<(), String> { + let cargo_bin = resolve_bin("cargo"); + if !std::path::Path::new(&cargo_bin).exists() && cargo_bin == "cargo" { + return Err(format_missing_rust_toolchain()); + } + + // Install dependencies automatically (handles fresh clones). + // + // Previously we ran `uv sync` with both stdout AND stderr piped to + // /dev/null and discarded the exit code (`let _ = …`). When `uv sync` + // failed — Windows path issues, network problems, lockfile conflicts — + // the user saw no error, the boot continued, `uv run locus serve` + // then ran in an under-provisioned venv, and the user waited the full + // 600s health-check window before getting "Locus server did not + // become healthy in time" with no actionable detail (issue #331). + // + // Now: capture stderr, check the exit status, surface a useful error + // to the user BEFORE the long server-start wait. The status detail + // message also indicates this can take a couple of minutes on first + // boot so users don't restart the app thinking it's stuck. + { + let mut s = status.lock().await; + s.detail = "Installing dependencies (uv sync — may take 1-2 min on first boot)...".into(); + } + let mut sync_cmd = tokio::process::Command::new(uv_bin); + sync_cmd + .args([ + "sync", + "--extra", + "desktop", + "--extra", + "inference-cloud", + "--extra", + "inference-google", + // locus_rust lives in a uv dependency group (not the published + // `desktop` extra) so pip installs from PyPI don't require it (#584). + "--group", + "desktop-native", + ]) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .current_dir(root); + // Avoid LD_LIBRARY_PATH leak when running inside an AppImage (#455). + prepare_subprocess_for_appimage(&mut sync_cmd); + prepare_backend_python(&mut sync_cmd); + add_cargo_bin_to_path(&mut sync_cmd); + sync_cmd.kill_on_drop(true); + let sync_output = sync_cmd.output().await; + match sync_output { + Ok(out) if !out.status.success() => { + let stderr = String::from_utf8_lossy(&out.stderr); + return Err(format_uv_sync_failure(root, out.status.code(), &stderr)); + } + Err(e) => { + return Err(format_uv_sync_spawn_error(root, uv_bin, &e.to_string())); + } + Ok(_) => {} // success — fall through + } + + { + let mut s = status.lock().await; + s.detail = "Verifying Rust extension (locus_rust)...".into(); + } + verify_locus_rust_extension(root, uv_bin).await +} + async fn boot_backend(backend: SharedBackend, status: SharedStatus) { // Never infer consent from a missing or malformed config. A fresh desktop // install must persist an explicit source choice before this function can @@ -1091,6 +1321,10 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { .env("OLLAMA_HOST", format!("127.0.0.1:{}", OLLAMA_PORT)) .stdout(std::process::Stdio::null()) .stderr(std::process::Stdio::null()); + if let Some(dir) = bundled_ollama_runtime_dir() { + sidecar_cmd.current_dir(dir); + } + runtime::hide_console_window(&mut sidecar_cmd); // Avoid LD_LIBRARY_PATH leak when running inside an AppImage (#455). prepare_subprocess_for_appimage(&mut sidecar_cmd); match spawn_owned_child(&mut sidecar_cmd) { @@ -1246,7 +1480,21 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { s.detail = "Starting API server...".into(); } - let uv_bin = resolve_bin("uv"); + let backend_runtime = match select_backend_runtime() { + Ok(found) => found, + Err(err) => { + let mut s = status.lock().await; + s.error = Some(err); + return; + } + }; + + // Packaged releases install with the uv bundled beside the app so the + // install doesn't depend on whatever uv the user may have. + let uv_bin = match &backend_runtime { + BackendRuntime::Managed(_) => sidecar_beside_exe("uv").unwrap_or_else(|| resolve_bin("uv")), + BackendRuntime::Source => resolve_bin("uv"), + }; // Verify uv is actually installed. Concrete per-OS instructions — // the generic "install it from astral.sh" was the #1 source of @@ -1278,10 +1526,14 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { return; } - let mut project_root = find_project_root(); + let mut project_root = match &backend_runtime { + BackendRuntime::Managed(_) => None, + BackendRuntime::Source => find_project_root(), + }; - if project_root.is_none() { - // Auto-clone on first launch + if project_root.is_none() && backend_runtime == BackendRuntime::Source { + // Auto-clone on first launch (development builds without a bundled + // runtime). let git_bin = resolve_bin("git"); // Check that git is installed @@ -1497,95 +1749,34 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { return; } - let root = project_root.as_ref().unwrap(); - - let cargo_bin = resolve_bin("cargo"); - if !std::path::Path::new(&cargo_bin).exists() && cargo_bin == "cargo" { - let mut s = status.lock().await; - s.error = Some(format_missing_rust_toolchain()); - return; - } - - // Install dependencies automatically (handles fresh clones). - // - // Previously we ran `uv sync` with both stdout AND stderr piped to - // /dev/null and discarded the exit code (`let _ = …`). When `uv sync` - // failed — Windows path issues, network problems, lockfile conflicts — - // the user saw no error, the boot continued, `uv run locus serve` - // then ran in an under-provisioned venv, and the user waited the full - // 600s health-check window before getting "Locus server did not - // become healthy in time" with no actionable detail (issue #331). - // - // Now: capture stderr, check the exit status, surface a useful error - // to the user BEFORE the long server-start wait. The status detail - // message also indicates this can take a couple of minutes on first - // boot so users don't restart the app thinking it's stuck. - { - let mut s = status.lock().await; - s.detail = "Installing dependencies (uv sync — may take 1-2 min on first boot)...".into(); - } - let mut sync_cmd = tokio::process::Command::new(&uv_bin); - sync_cmd - .args([ - "sync", - "--extra", - "desktop", - "--extra", - "inference-cloud", - "--extra", - "inference-google", - // locus_rust lives in a uv dependency group (not the published - // `desktop` extra) so pip installs from PyPI don't require it (#584). - "--group", - "desktop-native", - ]) - .stdout(std::process::Stdio::null()) - .stderr(std::process::Stdio::piped()) - .current_dir(root); - // Avoid LD_LIBRARY_PATH leak when running inside an AppImage (#455). - prepare_subprocess_for_appimage(&mut sync_cmd); - prepare_backend_python(&mut sync_cmd); - add_cargo_bin_to_path(&mut sync_cmd); - sync_cmd.kill_on_drop(true); - let sync_output = sync_cmd.output().await; - match sync_output { - Ok(out) if !out.status.success() => { - let stderr = String::from_utf8_lossy(&out.stderr); - let mut s = status.lock().await; - s.error = Some(format_uv_sync_failure(root, out.status.code(), &stderr)); - return; - } - Err(e) => { - let mut s = status.lock().await; - s.error = Some(format_uv_sync_spawn_error(root, &uv_bin, &e.to_string())); - return; + let serve = match &backend_runtime { + BackendRuntime::Managed(rt) => match prepare_managed_runtime(rt, &uv_bin, &status).await { + Ok(target) => target, + Err(err) => { + let mut s = status.lock().await; + s.error = Some(err); + return; + } + }, + BackendRuntime::Source => { + let root = project_root.clone().expect("source runtime has a project root"); + if let Err(err) = prepare_source_runtime(&root, &uv_bin, &status).await { + let mut s = status.lock().await; + s.error = Some(err); + return; + } + ServeTarget::source(&uv_bin, root) } - Ok(_) => {} // success — fall through - } - - { - let mut s = status.lock().await; - s.detail = "Verifying Rust extension (locus_rust)...".into(); - } - if let Err(err) = verify_locus_rust_extension(root, &uv_bin).await { - let mut s = status.lock().await; - s.error = Some(err); - return; - } + }; { let mut s = status.lock().await; - s.detail = format!("Starting API server from {}...", root.display()); + s.detail = format!("Starting API server from {}...", serve.cwd.display()); } - let mut cmd = tokio::process::Command::new(&uv_bin); - let mut serve_argv: Vec = vec![ - "run".into(), - "locus".into(), - "serve".into(), - "--port".into(), - JARVIS_PORT.to_string(), - ]; + let mut cmd = tokio::process::Command::new(&serve.program); + let mut serve_argv: Vec = serve.prefix.clone(); + serve_argv.extend(["serve".into(), "--port".into(), JARVIS_PORT.to_string()]); serve_argv.extend(plan.serve_args.iter().cloned()); // If the Ollama pull fell back to a different tag than planned, serve the // tag that is actually present. boot_plan always emits `--model` followed @@ -1594,8 +1785,7 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { match serve_argv.iter().position(|a| a == "--model") { Some(i) if i + 1 < serve_argv.len() => serve_argv[i + 1] = m.clone(), _ => eprintln!( - "Warning: resolved model {:?} could not be applied; \ - '--model ' not found in serve args {:?}", + "Warning: resolved model {:?} could not be applied; '--model ' not found in serve args {:?}", m, serve_argv ), } @@ -1603,12 +1793,15 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { cmd.args(&serve_argv) .stdout(std::process::Stdio::null()) .stderr(std::process::Stdio::piped()) - .current_dir(root); + .current_dir(&serve.cwd); // Avoid LD_LIBRARY_PATH leak when running inside an AppImage (#455) — // do this BEFORE cmd.env() calls below so our explicit cloud-key env // additions aren't accidentally stripped. prepare_subprocess_for_appimage(&mut cmd); - prepare_backend_python(&mut cmd); + if !serve.managed { + prepare_backend_python(&mut cmd); + } + runtime::hide_console_window(&mut cmd); // Inject cloud API keys from secure desktop storage. for (key, value) in cloud_keys_for_inference(&cfg) { @@ -1634,10 +1827,10 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { Err(e) => { let mut s = status.lock().await; s.error = Some(format!( - "Could not start locus server: {}. \ - Make sure uv is installed (https://astral.sh/uv) and the Locus repo is cloned at {}", + "Could not start locus server with {}: {}. Check that:\n{}", + serve.program, e, - root.display(), + serve.checklist(), )); return; } @@ -1651,17 +1844,13 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { s.error = Some(format!( "Locus server is running but the inference engine is not available \ (HTTP 503). This usually means the configured model couldn't be loaded.\n\n\ - Check the server logs, or run 'uv run locus serve --port {}{}' \ + Check the server logs, or run '{}' \ from {} to see the engine error.\n\n\ Server response:\n{}", - JARVIS_PORT, - // Show the args actually passed (after `serve --port `), - // including any post-fallback `--model` override. - match serve_argv.get(5..) { - Some(rest) if !rest.is_empty() => format!(" {}", rest.join(" ")), - _ => String::new(), - }, - root.display(), + // Show the args actually passed, including any post-fallback + // `--model` override. + serve.display_command(&serve_argv[serve.prefix.len()..]), + serve.cwd.display(), body.trim(), )); return; @@ -1678,13 +1867,9 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { s.error = Some(if stderr.is_empty() { format!( "Locus server exited (code {}) before becoming ready.\n\n\ - No stderr output. Check that:\n\ - 1. uv is installed ({})\n\ - 2. The Locus repo is at {}\n\ - 3. 'uv sync' completes in that directory", + No stderr output. Check that:\n{}", code_str, - uv_bin, - root.display(), + serve.checklist(), ) } else { format!( @@ -1699,12 +1884,8 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) { let mut s = status.lock().await; s.error = Some(if stderr.is_empty() { format!( - "Locus server did not become ready within 10 minutes. Check that:\n\ - 1. uv is installed ({})\n\ - 2. The Locus repo is at {}\n\ - 3. Run 'uv sync' in that directory", - uv_bin, - root.display(), + "Locus server did not become ready within 10 minutes. Check that:\n{}", + serve.checklist(), ) } else { format!( @@ -1969,21 +2150,32 @@ async fn fetch_models(api_url: String) -> Result { #[tauri::command] async fn run_jarvis_command(args: Vec) -> Result { - let uv_bin = resolve_bin("uv"); - - let mut cmd_args = vec!["run".to_string(), "locus".to_string()]; + let target = match select_backend_runtime() { + Ok(BackendRuntime::Managed(rt)) => { + let home = runtime::runtime_home(&home_dir()); + if !runtime::is_installed(&home, &rt) { + return Err("Locus is still finishing its first-launch setup. Try again in a moment.".into()); + } + Some(ServeTarget::managed(&home)) + } + // Run from the project root so `uv run locus` resolves the Locus + // project regardless of the app's launch cwd. Without this `locus` + // isn't found and the backend never starts — the UI then shows + // "Failed to get response" (see #531). + _ => find_project_root().map(|root| ServeTarget::source(&resolve_bin("uv"), root)), + }; + let (program, mut cmd_args) = match &target { + Some(t) => (t.program.clone(), t.prefix.clone()), + None => (resolve_bin("uv"), vec!["run".to_string(), "locus".to_string()]), + }; cmd_args.extend(args.iter().cloned()); - let mut cmd = tokio::process::Command::new(&uv_bin); + let mut cmd = tokio::process::Command::new(&program); cmd.args(&cmd_args); - // Run from the project root so `uv run locus` resolves the Locus - // project regardless of the app's launch cwd. In a packaged install the - // cwd isn't the checkout, so without this `locus` isn't found and the - // backend never starts — the UI then shows "Failed to get response" - // (see #531). - if let Some(ref root) = find_project_root() { - cmd.current_dir(root); + if let Some(t) = &target { + cmd.current_dir(&t.cwd); } + runtime::hide_console_window(&mut cmd); let is_serve = args.first().map(|a| a.as_str() == "serve").unwrap_or(false); @@ -3373,6 +3565,12 @@ pub fn run() { } })) .setup(move |app| { + // Record where the bundled runtime and Ollama libraries live + // before any boot task needs them. + if let Ok(dir) = app.path().resource_dir() { + let _ = RESOURCE_DIR.set(dir); + } + // System tray let show = MenuItemBuilder::with_id("show", "Show / Hide").build(app)?; let health = MenuItemBuilder::with_id("health", "Health: starting...") @@ -3494,7 +3692,7 @@ pub fn run() { #[cfg(test)] mod tests { use super::{ - boot_plan, default_local_model, discard_pending_inference_setup_at, + boot_plan, choose_backend_runtime, default_local_model, discard_pending_inference_setup_at, format_extension_import_failure, format_missing_rust_toolchain, format_port_unavailable, format_uv_sync_failure, format_uv_sync_spawn_error, managed_cloud_key_names, matching_installed_model, model_names_match, normalize_host, @@ -3503,13 +3701,77 @@ mod tests { project_root_near_executable, reload_cloud_keys_for_owned_backend_at, should_persist_resolved_model, spawn_owned_child, stage_pending_inference_config_at, startup_installed_model, upsert_engine_host, uv_sync_stderr_tail, BackendManager, - InferenceConfig, InferenceCredentialStore, PendingInferenceRollback, SetupStatus, - SourceKind, DESKTOP_UV_SYNC_COMMAND, PENDING_INFERENCE_API_KEY, + BackendRuntime, InferenceConfig, InferenceCredentialStore, PendingInferenceRollback, + ServeTarget, SetupStatus, SourceKind, DESKTOP_UV_SYNC_COMMAND, PENDING_INFERENCE_API_KEY, }; + use crate::runtime::BundledRuntime; use std::collections::HashMap; use std::path::Path; use std::sync::Mutex as StdMutex; + fn sample_bundled_runtime() -> BundledRuntime { + BundledRuntime { + manifest: "{}".into(), + python: "3.12".into(), + requirements: "requirements.txt".into(), + wheels: vec!["locus_assistant-1.0.0-py3-none-any.whl".into()], + } + } + + #[test] + fn packaged_builds_use_the_bundled_runtime() { + let chosen = choose_backend_runtime(false, false, Some(Ok(sample_bundled_runtime()))); + assert_eq!(chosen, Ok(BackendRuntime::Managed(sample_bundled_runtime()))); + } + + #[test] + fn a_checkout_overrides_the_bundled_runtime() { + for (explicit_root, inside_checkout) in [(true, false), (false, true)] { + let chosen = choose_backend_runtime( + explicit_root, + inside_checkout, + Some(Ok(sample_bundled_runtime())), + ); + assert_eq!(chosen, Ok(BackendRuntime::Source)); + } + } + + #[test] + fn builds_without_a_bundle_fall_back_to_source() { + assert_eq!(choose_backend_runtime(false, false, None), Ok(BackendRuntime::Source)); + } + + #[test] + fn a_broken_bundle_is_reported() { + let chosen = choose_backend_runtime(false, false, Some(Err("missing wheel".into()))); + assert_eq!(chosen, Err("missing wheel".to_string())); + } + + #[test] + fn managed_serve_target_runs_locus_from_the_runtime_venv() { + let home = Path::new("rt-home"); + let target = ServeTarget::managed(home); + let locus = crate::runtime::venv_locus(&crate::runtime::venv_dir(home)) + .display() + .to_string(); + assert_eq!(target.program, locus); + assert!(target.prefix.is_empty()); + assert_eq!(target.cwd, home); + let args = vec!["serve".to_string(), "--port".to_string(), "8000".to_string()]; + assert_eq!(target.display_command(&args), format!("{} serve --port 8000", locus)); + assert!(target.checklist().contains("rt-home")); + } + + #[test] + fn source_serve_target_uses_uv_run() { + let target = ServeTarget::source("/opt/uv", Path::new("checkout").to_path_buf()); + assert_eq!(target.prefix, vec!["run", "locus"]); + let args = vec!["serve".to_string()]; + assert_eq!(target.display_command(&args), "uv run locus serve"); + assert!(target.checklist().contains("/opt/uv")); + assert!(target.checklist().contains("checkout")); + } + #[test] fn atlas_cloud_key_is_loaded_with_managed_desktop_keys() { assert!(managed_cloud_key_names().contains(&"ATLASCLOUD_API_KEY".to_string())); diff --git a/frontend/src-tauri/src/runtime.rs b/frontend/src-tauri/src/runtime.rs new file mode 100644 index 0000000..44f97b9 --- /dev/null +++ b/frontend/src-tauri/src/runtime.rs @@ -0,0 +1,428 @@ +//! Managed Python runtime for packaged desktop installs. +//! +//! Release bundles ship a `runtime/` resource directory with the Locus wheel, +//! a prebuilt `locus_rust` wheel, a hash-locked `requirements.txt` exported +//! from `uv.lock`, and a `manifest.json` that names them. On first launch the +//! bundled `uv` creates a private virtual environment, with a uv-managed +//! CPython, under `~/.locus/desktop-runtime` and installs from those files. +//! End users need no git, Python, or Rust toolchain. + +use std::path::{Path, PathBuf}; + +/// Resource sub-directory that holds the bundled runtime files. +pub const RUNTIME_RESOURCE_DIR: &str = "runtime"; +const MANIFEST_FILE: &str = "manifest.json"; +/// Written after a successful install; holds the manifest it was built from. +const STAMP_FILE: &str = "installed.json"; +const STDERR_TAIL_CHARS: usize = 1500; + +/// A runtime bundled with this build of the desktop app. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BundledRuntime { + /// Raw manifest text, also written as the install stamp so a new app + /// version (new manifest) triggers a reinstall. + pub manifest: String, + /// uv Python request, e.g. `3.12` or `cpython-3.12-windows-x86_64-none`. + pub python: String, + pub requirements: PathBuf, + /// Installed after the locked requirements, without dependency resolution. + pub wheels: Vec, +} + +#[derive(serde::Deserialize)] +struct Manifest { + python: String, + requirements: String, + wheels: Vec, +} + +/// Parse `manifest` and check that every file it names exists in `dir`. +pub fn parse_bundled_runtime(dir: &Path, manifest: &str) -> Result { + let parsed: Manifest = serde_json::from_str(manifest) + .map_err(|e| format!("The bundled runtime manifest is invalid: {}", e))?; + if parsed.python.trim().is_empty() || parsed.wheels.is_empty() { + return Err("The bundled runtime manifest lists no Python version or wheels.".into()); + } + let requirements = dir.join(&parsed.requirements); + let wheels: Vec = parsed.wheels.iter().map(|w| dir.join(w)).collect(); + for path in std::iter::once(&requirements).chain(wheels.iter()) { + if !path.is_file() { + return Err(format!( + "A bundled runtime file is missing: {}. Reinstall Locus.", + path.display() + )); + } + } + Ok(BundledRuntime { + manifest: manifest.to_string(), + python: parsed.python, + requirements, + wheels, + }) +} + +/// The runtime bundled under `resource_dir`. `None` means this build ships +/// no runtime (development builds); `Some(Err)` means the bundle is broken. +pub fn bundled_runtime(resource_dir: &Path) -> Option> { + let dir = resource_dir.join(RUNTIME_RESOURCE_DIR); + let manifest = std::fs::read_to_string(dir.join(MANIFEST_FILE)).ok()?; + Some(parse_bundled_runtime(&dir, &manifest)) +} + +pub fn runtime_home(home: &str) -> PathBuf { + PathBuf::from(home).join(".locus").join("desktop-runtime") +} + +pub fn venv_dir(runtime_home: &Path) -> PathBuf { + runtime_home.join("venv") +} + +pub fn venv_python(venv: &Path) -> PathBuf { + if cfg!(target_os = "windows") { + venv.join("Scripts").join("python.exe") + } else { + venv.join("bin").join("python") + } +} + +pub fn venv_locus(venv: &Path) -> PathBuf { + if cfg!(target_os = "windows") { + venv.join("Scripts").join("locus.exe") + } else { + venv.join("bin").join("locus") + } +} + +/// True when `runtime_home` holds a complete install of exactly `runtime`. +pub fn is_installed(runtime_home: &Path, runtime: &BundledRuntime) -> bool { + venv_locus(&venv_dir(runtime_home)).is_file() + && std::fs::read_to_string(runtime_home.join(STAMP_FILE)) + .map(|stamp| stamp == runtime.manifest) + .unwrap_or(false) +} + +/// One `uv` invocation of the first-launch install, with its progress label. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct InstallStep { + /// Progress text shown while the step runs. + pub detail: &'static str, + /// Short name used in failure messages. + pub name: &'static str, + pub args: Vec, +} + +/// The `uv` commands that install `runtime` into `runtime_home`. Pure, so the +/// exact arguments are unit-tested. +pub fn install_steps(runtime: &BundledRuntime, runtime_home: &Path) -> Vec { + let venv = venv_dir(runtime_home); + let python = venv_python(&venv).display().to_string(); + let mut wheel_args: Vec = ["pip", "install", "--python", &python, "--no-deps"] + .iter() + .map(|s| s.to_string()) + .collect(); + wheel_args.extend(runtime.wheels.iter().map(|w| w.display().to_string())); + vec![ + InstallStep { + detail: "Setting up Python (first launch)...", + name: "setting up Python", + args: vec![ + "venv".into(), + "--python".into(), + runtime.python.clone(), + "--managed-python".into(), + venv.display().to_string(), + ], + }, + InstallStep { + detail: "Installing Python packages (first launch, this can take a few minutes)...", + name: "installing Python packages", + args: vec![ + "pip".into(), + "install".into(), + "--python".into(), + python, + "--no-deps".into(), + "--require-hashes".into(), + "-r".into(), + runtime.requirements.display().to_string(), + ], + }, + InstallStep { + detail: "Installing Locus...", + name: "installing Locus", + args: wheel_args, + }, + ] +} + +/// Environment for every `uv` call: keep Python, caches and settings inside +/// the runtime home so a user's own uv setup can't change the install. +pub fn uv_env(runtime_home: &Path) -> Vec<(&'static str, String)> { + vec![ + ( + "UV_PYTHON_INSTALL_DIR", + runtime_home.join("python").display().to_string(), + ), + ( + "UV_CACHE_DIR", + runtime_home.join("cache").display().to_string(), + ), + ("UV_NO_CONFIG", "1".into()), + ("UV_PYTHON_DOWNLOADS", "automatic".into()), + ] +} + +/// Remove a partial or outdated install so the next steps start clean. The +/// downloaded Python and cache are kept to make a retry faster. +pub fn reset_install(runtime_home: &Path) -> Result<(), String> { + let _ = std::fs::remove_file(runtime_home.join(STAMP_FILE)); + let venv = venv_dir(runtime_home); + if venv.exists() { + std::fs::remove_dir_all(&venv).map_err(|e| { + format!( + "Could not remove the old runtime at {}: {}", + venv.display(), + e + ) + })?; + } + std::fs::create_dir_all(runtime_home) + .map_err(|e| format!("Could not create {}: {}", runtime_home.display(), e)) +} + +/// Record a finished install and drop the download cache it no longer needs. +pub fn mark_installed(runtime_home: &Path, runtime: &BundledRuntime) -> Result<(), String> { + std::fs::write(runtime_home.join(STAMP_FILE), &runtime.manifest) + .map_err(|e| format!("Could not record the runtime install: {}", e))?; + let _ = std::fs::remove_dir_all(runtime_home.join("cache")); + Ok(()) +} + +/// Hide the console window that Windows opens for console-subsystem children. +pub fn hide_console_window(cmd: &mut tokio::process::Command) { + #[cfg(target_os = "windows")] + { + const CREATE_NO_WINDOW: u32 = 0x0800_0000; + cmd.creation_flags(CREATE_NO_WINDOW); + } + #[cfg(not(target_os = "windows"))] + let _ = cmd; +} + +fn stderr_tail(stderr: &[u8]) -> String { + let text = String::from_utf8_lossy(stderr); + let total = text.chars().count(); + text.chars() + .skip(total.saturating_sub(STDERR_TAIL_CHARS)) + .collect::() + .trim() + .to_string() +} + +/// User-facing message for a failed first-launch step. +pub fn format_install_failure(runtime_home: &Path, step_name: &str, output: &str) -> String { + format!( + "First-launch setup failed while {}.\n\nLast output:\n{}\n\n\ + Check your internet connection, then relaunch Locus to retry. \ + To start the setup over, delete {} and relaunch.", + step_name, + if output.is_empty() { + "(no output)" + } else { + output + }, + runtime_home.display(), + ) +} + +/// Run one program with the runtime environment and fail with its stderr. +pub async fn run_step( + program: &Path, + args: &[String], + runtime_home: &Path, + prepare: fn(&mut tokio::process::Command), +) -> Result<(), String> { + let mut cmd = tokio::process::Command::new(program); + cmd.args(args) + .envs(uv_env(runtime_home)) + .current_dir(runtime_home) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .kill_on_drop(true); + prepare(&mut cmd); + hide_console_window(&mut cmd); + match cmd.output().await { + Ok(out) if out.status.success() => Ok(()), + Ok(out) => Err(stderr_tail(&out.stderr)), + Err(e) => Err(format!("could not run {}: {}", program.display(), e)), + } +} + +/// Check that the installed environment can import Locus and its extension. +pub fn verify_args() -> Vec { + vec!["-c".into(), "import locus, locus_rust".into()] +} + +#[cfg(test)] +mod tests { + use super::*; + + fn temp_dir(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "locus-runtime-test-{}-{}", + name, + std::process::id() + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + const MANIFEST: &str = r#"{ + "python": "3.12", + "requirements": "requirements.txt", + "wheels": ["locus_rust-1.0.0-cp312-cp312-win_amd64.whl", "locus_assistant-1.0.0-py3-none-any.whl"] + }"#; + + fn bundle(name: &str) -> PathBuf { + let dir = temp_dir(name); + for file in [ + "requirements.txt", + "locus_rust-1.0.0-cp312-cp312-win_amd64.whl", + "locus_assistant-1.0.0-py3-none-any.whl", + ] { + std::fs::write(dir.join(file), "").unwrap(); + } + dir + } + + #[test] + fn parses_manifest_and_resolves_files() { + let dir = bundle("parse"); + let rt = parse_bundled_runtime(&dir, MANIFEST).unwrap(); + assert_eq!(rt.python, "3.12"); + assert_eq!(rt.requirements, dir.join("requirements.txt")); + assert_eq!( + rt.wheels, + vec![ + dir.join("locus_rust-1.0.0-cp312-cp312-win_amd64.whl"), + dir.join("locus_assistant-1.0.0-py3-none-any.whl"), + ] + ); + } + + #[test] + fn rejects_manifest_naming_missing_files() { + let dir = temp_dir("missing"); + std::fs::write(dir.join("requirements.txt"), "").unwrap(); + let err = parse_bundled_runtime(&dir, MANIFEST).unwrap_err(); + assert!(err.contains("missing"), "{err}"); + } + + #[test] + fn rejects_manifest_without_wheels() { + let dir = bundle("empty"); + let err = parse_bundled_runtime( + &dir, + r#"{"python": "3.12", "requirements": "requirements.txt", "wheels": []}"#, + ) + .unwrap_err(); + assert!(err.contains("no Python version or wheels"), "{err}"); + } + + #[test] + fn no_bundle_means_development_build() { + let dir = temp_dir("none"); + assert!(bundled_runtime(&dir).is_none()); + } + + #[test] + fn install_steps_create_venv_then_install_locked_then_wheels() { + let dir = bundle("steps"); + let rt = parse_bundled_runtime(&dir, MANIFEST).unwrap(); + let home = dir.join("home"); + let steps = install_steps(&rt, &home); + let venv = venv_dir(&home).display().to_string(); + let python = venv_python(&venv_dir(&home)).display().to_string(); + + assert_eq!(steps.len(), 3); + assert_eq!( + steps[0].args, + vec![ + "venv", + "--python", + "3.12", + "--managed-python", + venv.as_str() + ] + ); + assert_eq!( + steps[1].args, + vec![ + "pip", + "install", + "--python", + python.as_str(), + "--no-deps", + "--require-hashes", + "-r", + dir.join("requirements.txt").display().to_string().as_str(), + ] + ); + assert_eq!( + &steps[2].args[..5], + &["pip", "install", "--python", python.as_str(), "--no-deps"] + ); + assert_eq!(steps[2].args.len(), 7); + } + + #[test] + fn stamp_must_match_manifest_and_locus_must_exist() { + let dir = bundle("stamp"); + let rt = parse_bundled_runtime(&dir, MANIFEST).unwrap(); + let home = dir.join("home"); + reset_install(&home).unwrap(); + assert!(!is_installed(&home, &rt)); + + let locus = venv_locus(&venv_dir(&home)); + std::fs::create_dir_all(locus.parent().unwrap()).unwrap(); + std::fs::write(&locus, "").unwrap(); + assert!(!is_installed(&home, &rt), "no stamp yet"); + + mark_installed(&home, &rt).unwrap(); + assert!(is_installed(&home, &rt)); + + let newer = BundledRuntime { + manifest: MANIFEST.replace("1.0.0", "1.0.1"), + ..rt.clone() + }; + assert!(!is_installed(&home, &newer), "a new app version reinstalls"); + + reset_install(&home).unwrap(); + assert!(!locus.exists()); + assert!(!is_installed(&home, &rt)); + } + + #[test] + fn uv_env_keeps_python_and_cache_in_runtime_home() { + let home = PathBuf::from("rt-home"); + let env = uv_env(&home); + assert!(env.contains(&("UV_NO_CONFIG", "1".to_string()))); + assert!(env.contains(&( + "UV_PYTHON_INSTALL_DIR", + home.join("python").display().to_string() + ))); + assert!(env.contains(&("UV_CACHE_DIR", home.join("cache").display().to_string()))); + } + + #[test] + fn failure_message_names_step_and_reset_path() { + let msg = format_install_failure(Path::new("rt-home"), "installing Locus", "boom"); + assert!(msg.contains("failed while installing Locus."), "{msg}"); + assert!(msg.contains("boom")); + assert!(msg.contains("rt-home")); + let empty = format_install_failure(Path::new("rt-home"), "installing Locus", ""); + assert!(empty.contains("(no output)")); + } +} diff --git a/src/locus/cli/_banner.py b/src/locus/cli/_banner.py index 339d2ef..624e742 100644 --- a/src/locus/cli/_banner.py +++ b/src/locus/cli/_banner.py @@ -5,16 +5,13 @@ # "Locus" rendered in the figlet "standard" font. Stored as plain text # (no inline Rich markup) so the backslashes in the glyphs don't collide with # Rich's [tag] markup or Python raw-string escaping — colour is applied at -# print time via a style argument. The "standard" font renders a clearly -# readable capital J (the bottom-left \___/ hook), unlike the cramped prior -# art where the J read as an I. +# print time via a style argument. _WORDMARK = ( - " ___ _ _ ", - " / _ \\ _ __ ___ _ __ | | __ _ _ ____ _(_)___ ", - "| | | | '_ \\ / _ \\ '_ \\ _ | |/ _` | '__\\ \\ / / / __|", - "| |_| | |_) | __/ | | | |_| | (_| | | \\ V /| \\__ \\", - " \\___/| .__/ \\___|_| |_|\\___/ \\__,_|_| \\_/ |_|___/", - " |_| ", + " _", + "| | ___ ___ _ _ ___", + "| | / _ \\ / __| | | / __|", + "| |__| (_) | (__| |_| \\__ \\", + "|_____\\___/ \\___|\\__,_|___/", ) _TAGLINE = "Personal AI, On Personal Devices" diff --git a/src/locus/cli/bench_cmd.py b/src/locus/cli/bench_cmd.py index 2fd97e0..0f464a0 100644 --- a/src/locus/cli/bench_cmd.py +++ b/src/locus/cli/bench_cmd.py @@ -24,12 +24,11 @@ logger = logging.getLogger(__name__) _BANNER = r""" - ___ _ _ - / _ \ _ __ ___ _ __ | | __ _ _ ____ _(_)___ -| | | | '_ \ / _ \ '_ \ _ | |/ _` | '__\ \ / / / __| -| |_| | |_) | __/ | | | |_| | (_| | | \ V /| \__ \ - \___/| .__/ \___|_| |_|\___/ \__,_|_| \_/ |_|___/ - |_| + _ +| | ___ ___ _ _ ___ +| | / _ \ / __| | | / __| +| |__| (_) | (__| |_| \__ \ +|_____\___/ \___|\__,_|___/ """ diff --git a/tests/tools/test_web_search.py b/tests/tools/test_web_search.py index 4403e4f..a7a0391 100644 --- a/tests/tools/test_web_search.py +++ b/tests/tools/test_web_search.py @@ -245,6 +245,15 @@ def _mock_import(name, *args, **kwargs): monkeypatch.setattr(builtins, "__import__", _mock_import) + # Stub DuckDuckGo so the fallback doesn't depend on a live search. + mock_ddgs = MagicMock() + mock_ddgs.text.return_value = [ + {"title": "DDG Result", "href": "https://example.com", "body": "Content"}, + ] + mock_ddgs_module = MagicMock() + mock_ddgs_module.DDGS.return_value = mock_ddgs + monkeypatch.setitem(sys.modules, "ddgs", mock_ddgs_module) + tool = WebSearchTool(api_key="test-key") result = tool.execute(query="test query") assert result.success is True