#!/usr/bin/env bash set -euo pipefail MODEL="${MODEL:-qwen2.5-coder:7b}" OLLAMA_URL="${OLLAMA_URL:-http://192.168.5.23:11434}" THRESHOLD="${LINAAI_CONFIDENCE_THRESHOLD:-0.75}" FORCE_ESCALATE=0 DRY_RUN=0 usage() { cat >&2 <<'EOF' Usage: Scripts/linaai_task.sh [--threshold 0.75] [--dry-run] [--force-escalate] "task" Routes a task through LinaAI's supervised local workflow: 1. Qwen/Ollama preflight risk and confidence check. 2. Automatic Codex escalation if confidence is too low or task is high risk. 3. Aider local execution for acceptable supervised tasks. 4. Automatic Codex escalation if Aider fails. Use --dry-run to test routing without editing files. EOF } while [[ $# -gt 0 ]]; do case "$1" in --threshold) THRESHOLD="${2:-}" shift 2 ;; --dry-run) DRY_RUN=1 shift ;; --force-escalate) FORCE_ESCALATE=1 shift ;; -h|--help) usage exit 0 ;; --) shift break ;; -*) echo "Unknown option: $1" >&2 usage exit 2 ;; *) break ;; esac done TASK="${*:-}" if [[ -z "$TASK" ]]; then usage exit 2 fi ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" cd "$ROOT" mkdir -p Saved/AiTaskStatus STAMP="$(date -u +%Y%m%dT%H%M%SZ)" PREFLIGHT_JSON="Saved/AiTaskStatus/linaai_preflight_${STAMP}.json" STATUS_JSON="Saved/AiTaskStatus/linaai_status_${STAMP}.json" current_branch="$(git branch --show-current 2>/dev/null || echo unknown)" repo_evidence="$( { echo "cwd: ${ROOT}" echo "branch: ${current_branch}" echo "git_status:" git status --short 2>/dev/null || true echo "top_level:" find . -maxdepth 1 -mindepth 1 -printf "%f\n" 2>/dev/null | sort | head -80 echo "project_markers:" test -f AgrarianGame.uproject && echo "AgrarianGame.uproject" test -d Source && echo "Source/" test -d Config && echo "Config/" test -d Content && echo "Content/" test -d Scripts && echo "Scripts/" test -d Docs && echo "Docs/" echo "script_samples:" find Scripts -maxdepth 1 -type f -printf "%f\n" 2>/dev/null | sort | head -40 echo "doc_samples:" find Docs -maxdepth 2 -type f -printf "%p\n" 2>/dev/null | sort | head -40 } | sed 's/"/'\''/g' )" system_prompt='You are LinaAI, a supervised local coding assistant for Agrarian. You must not pretend certainty. Classify task risk and confidence before any edits. Confidence must be based on concrete evidence. If you lack evidence, confidence must be below 0.65. High-risk areas include Unreal core architecture, save/load, multiplayer, networking/replication, AGR wallet/payments, marketplace/economy transfer logic, auth, security, migrations, deployment secrets, and broad refactors. Return JSON only.' user_prompt=$(cat <