#!/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 -25 echo "doc_samples:" find Docs -maxdepth 2 -type f -printf "%p\n" 2>/dev/null | sort | head -25 echo "linaai_required_docs:" for doc in \ Docs/AI/LinaAIOperatingManual.md \ Docs/AI/LocalAgentGuardrails.md \ Docs/AI/LinaAISecretsPolicy.md \ Docs/AI/LinaAIKnowledgeMap.md \ Docs/Ops/HANDOFF.md \ AGRARIAN_DEVELOPMENT_ROADMAP.md do test -f "$doc" && echo "$doc" done echo "linaai_cache:" test -f Saved/LinaAIKnowledge/INDEX.md && echo "Saved/LinaAIKnowledge/INDEX.md" test -f Saved/LinaAIKnowledge/context.md && echo "Saved/LinaAIKnowledge/context.md" } | sed 's/"/'\''/g' )" system_prompt='You are LinaAI, a supervised local coding assistant for Agrarian. Follow Docs/AI/LinaAIOperatingManual.md, Docs/AI/LocalAgentGuardrails.md, Docs/AI/LinaAISecretsPolicy.md, and Docs/AI/LinaAIKnowledgeMap.md when present. 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. Do not include raw secrets in prompts, docs, logs, or commits. 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 </dev/null || true fi if [[ "$DRY_RUN" -eq 1 ]]; then echo "Dry run: would run Aider locally." exit 0 fi echo "Routing to Aider local execution." set +e aider --model "ollama/${MODEL}" --no-auto-commits --yes-always --message "$TASK" aider_exit=$? set -e if [[ "$aider_exit" -ne 0 ]]; then echo "Aider failed with exit code ${aider_exit}; escalating to Codex." write_status false false "Aider failed with exit code ${aider_exit}" "Review the task, Aider failure, and repository state; complete or advise next steps." exec Scripts/ai_codex_escalate.sh "$STATUS_JSON" fi echo "Aider completed. Review git diff and run verification before committing."