From 50153a9a8eb5913fd5e472c49bd424e8e1fb4ea0 Mon Sep 17 00:00:00 2001 From: ziadarafat Date: Wed, 2 Dec 2026 09:20:37 -0700 Subject: [PATCH] Save Pi worker and model configuration --- bin/pi-worker | 94 ++++++++++++++++++++++++++ config/pi/auto-model.json | 6 +- config/pi/settings.json | 5 +- config/systemd/user/pi-workers.service | 15 ++++ 4 files changed, 115 insertions(+), 5 deletions(-) create mode 100755 bin/pi-worker create mode 100644 config/systemd/user/pi-workers.service diff --git a/bin/pi-worker b/bin/pi-worker new file mode 100755 index 0000000..2ecdc93 --- /dev/null +++ b/bin/pi-worker @@ -0,0 +1,94 @@ +#!/usr/bin/env bash +set -euo pipefail +umask 077 +export PATH="$HOME/.local/bin:$HOME/.nix-profile/bin:/usr/bin:/bin" +root="${XDG_STATE_HOME:-$HOME/.local/state}/pi-workers" +self="$(realpath "$0")" +profiles="$(dirname "$self")/../config/free-pi/models.json" +mkdir -p "$root" +case "${2:-}" in + cohere|local) provider=$2 ;; + *) echo 'Usage: pi-worker {start|serve} {cohere|local} | pi-worker submit {cohere|local} REPO TASK.md' >&2; exit 2 ;; +esac +queue="$root/$provider" +mkdir -p "$queue" +case "$1" in + start) + if ! tmux has-session -t "=pi-worker-$provider" 2>/dev/null; then + tmux new-session -d -s "pi-worker-$provider" -- "$self" serve "$provider" + tmux set-option -w -t "pi-worker-$provider:" remain-on-exit on + fi + ;; + stop) + tmux kill-session -t "=pi-worker-$provider" + ;; + submit) + [[ $# == 4 && -s "$4" ]] + task=$(realpath "$4") + job=$(mktemp -d "$queue/job-XXXXXXXX") + git -C "$3" worktree add --detach "$job/work" HEAD >&2 + cp -- "$task" "$job/task.md" + touch "$job/ready" + printf '%s\n' "$job" + ;; + serve) + exec 9>"$queue/worker.lock" + flock -n 9 + shopt -s nullglob + trap 'exit 0' TERM INT + printf 'Waiting for %s jobs in %s\n' "$provider" "$queue" + for marker in "$queue"/job-*/running; do + job=${marker%/running} + printf '125\n' >"$job/exit-code" + mv -- "$marker" "$job/done" + done + while true; do + for ready in "$queue"/job-*/ready; do + job=${ready%/ready} + mv -- "$ready" "$job/running" + printf 'Running %s\n' "${job##*/}" + code=0 + "$self" run "$provider" "$job" >"$job/result.txt" 2>&1 || code=$? + printf '%s\n' "$code" >"$job/exit-code" + mv -- "$job/running" "$job/done" + printf '%s finished with status %s\n' "${job##*/}" "$code" + done + sleep 3 + done + ;; + run) + job=$3 + [[ "$job" == "$queue"/job-* && -f "$job/running" ]] + pi_path=$(mise which pi) + node_path=$(mise which node) + modules=$(dirname "$(dirname "$pi_path")") + mkdir -p "$job/profile" + if [[ "$provider" == cohere ]]; then + model=cohere/command-a-03-2025 + jq '{providers: {cohere: (.providers.cohere | .apiKey = "$COHERE_API_KEY")}}' "$profiles" > "$job/profile/models.json" + else + model=ollama/gemma4-12b-qat-64k + jq '{providers: {ollama: (.providers.ollama | .apiKey = "ollama")}}' "$profiles" > "$job/profile/models.json" + fi + sandbox=(bwrap --die-with-parent --unshare-user --unshare-pid --unshare-ipc + --proc /proc --dev /dev --tmpfs /tmp + --ro-bind /usr /usr --ro-bind /nix/store /nix/store + --symlink usr/bin /bin --symlink usr/lib /lib --symlink usr/lib64 /lib64 + --ro-bind /etc/resolv.conf /etc/resolv.conf --ro-bind /etc/ssl/certs /etc/ssl/certs + --ro-bind "$modules" /runtime/node_modules --ro-bind "$node_path" /runtime/node + --bind "$job/profile" /profile --ro-bind "$job/task.md" /task.md + --bind "$job/work" /work --tmpfs /home --chdir /work) + while IFS= read -r name; do + sandbox+=(--unsetenv "$name") + done < <(compgen -e | sed '/^COHERE_API_KEY$/d') + sandbox+=(--setenv HOME /home --setenv PATH /usr/bin:/bin --setenv PI_CODING_AGENT_DIR /profile) + sandbox+=(/runtime/node /runtime/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js + --no-session --no-extensions --no-mcp --tools read,edit,write,grep,find,ls + --model "$model" --print @/task.md) + if [[ "$provider" == cohere ]]; then + exec timeout -k 10 300 secretctl get COHERE_API_KEY -- "${sandbox[@]}" + fi + exec timeout -k 10 300 "${sandbox[@]}" + ;; + *) exit 2 ;; +esac diff --git a/config/pi/auto-model.json b/config/pi/auto-model.json index 0a18a72..f0337af 100644 --- a/config/pi/auto-model.json +++ b/config/pi/auto-model.json @@ -6,7 +6,7 @@ }, "fallback": { "provider": "ollama", - "model": "gemma4-12b-qat-128k", - "thinking": "low" + "model": "gemma4-26b-a4b-qat-128k", + "thinking": "high" } -} +} \ No newline at end of file diff --git a/config/pi/settings.json b/config/pi/settings.json index 11902da..9a25d10 100644 --- a/config/pi/settings.json +++ b/config/pi/settings.json @@ -5,7 +5,8 @@ "npm:@juicesharp/rpiv-ask-user-question", "npm:pi-background-tasks", "npm:context-mode", - "npm:pi-auto-models" + "npm:pi-auto-models", + "npm:pi-lens" ], "extensionChangelogVersions": { "{\"source\":\"git:github.com/GeneGulanesJr/LaPis\",\"scope\":\"user\"}": "1.4.0" @@ -25,4 +26,4 @@ "ollama/gemma4-26b-a4b-qat:latest", "ollama/gemma4-12b-qat-uncensored-64k:latest" ] -} +} \ No newline at end of file diff --git a/config/systemd/user/pi-workers.service b/config/systemd/user/pi-workers.service new file mode 100644 index 0000000..7c47690 --- /dev/null +++ b/config/systemd/user/pi-workers.service @@ -0,0 +1,15 @@ +[Unit] +Description=Start the Pi task worker tmux sessions +Wants=network-online.target +After=network-online.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=%h/.dotfiles/bin/pi-worker start cohere +ExecStart=%h/.dotfiles/bin/pi-worker start local +ExecStop=-%h/.dotfiles/bin/pi-worker stop cohere +ExecStop=-%h/.dotfiles/bin/pi-worker stop local + +[Install] +WantedBy=default.target