#!/usr/bin/env bash
set -euo pipefail

# ============================================================
# ORNITH + OPENCODE LAB FINAL
# ============================================================

ROOT="$HOME/bench/ornith/opencode-final"
BASE="$HOME/bench/ornith/opencode-lab/workspace"
RUNS="$ROOT/runs"

IMAGE="lomelidev/llama.cpp:cu130-nccl-sse-faall-10974"
MODEL_DIR="/home/lab/models/ornith15-9b"

MODEL="local-ornith/ornith-local"

PORT=8120

SLOTS=3
CTX_SLOT=262144
CTX_TOTAL=$((SLOTS * CTX_SLOT))

# HARD STOP del modelo.
MAX_OUTPUT=4096

mkdir -p "$ROOT" "$RUNS"

SELF="$(readlink -f "$0")"


die() {
    echo
    echo "ERROR: $*" >&2
    exit 1
}


# ============================================================
# CLEAN OLD LAB
# ============================================================

cleanup_old() {

    echo
    echo "============================================================"
    echo " LIMPIANDO OPENCODE VIEJO"
    echo "============================================================"

    echo
    echo "Procesos antes:"
    pgrep -af 'opencode|ornith-opencode|opencode-parallel' || true


    # Viejos wrappers de nuestros benchmarks.
    pkill -TERM -f \
      '/home/lab/bench/ornith/.*parallel.*\.sh' \
      2>/dev/null || true

    pkill -TERM -f \
      'opencode run.*local-ornith/ornith-local' \
      2>/dev/null || true

    pkill -TERM -f \
      'timeout .*opencode run.*local-ornith/ornith-local' \
      2>/dev/null || true

    sleep 2


    # El shared background service es quien conservó las sesiones huérfanas.
    # Para este laboratorio nuevo NO lo necesitamos: usamos --standalone.
    mapfile -t SERVICE_PIDS < <(
      pgrep -f 'opencode\.exe serve --service' || true
    )

    if ((${#SERVICE_PIDS[@]})); then

        echo
        echo "Deteniendo shared service viejo:"
        printf '  PID %s\n' "${SERVICE_PIDS[@]}"

        kill -TERM "${SERVICE_PIDS[@]}" 2>/dev/null || true

        sleep 3

        for pid in "${SERVICE_PIDS[@]}"; do
            if kill -0 "$pid" 2>/dev/null; then
                echo "Forzando PID $pid"
                kill -KILL "$pid" 2>/dev/null || true
            fi
        done
    fi


    # Standalone servers huérfanos de pruebas anteriores.
    pkill -TERM -f \
      'opencode\.exe serve --stdio --port 0' \
      2>/dev/null || true

    sleep 2

    echo
    echo "Conexiones a llama después de limpiar:"
    ss -tnp | grep ':8120' || echo "  ninguna"

    echo
}


# ============================================================
# RECREATE LLAMA
# ============================================================

start_llama() {

    echo
    echo "============================================================"
    echo " RECREANDO ORNITH"
    echo "============================================================"
    echo
    echo "GPU        : 0"
    echo "Slots      : $SLOTS"
    echo "Ctx/slot   : $CTX_SLOT"
    echo "Ctx total  : $CTX_TOTAL"
    echo "KV         : Q4/Q4"
    echo "FA         : ON"
    echo "DFlash     : OFF"
    echo "Vision     : OFF"
    echo "Hard output: $MAX_OUTPUT"
    echo


    docker rm -f ornith >/dev/null 2>&1 || true


    docker run -d \
      --name ornith \
      --gpus '"device=0"' \
      --ipc=host \
      -p "$PORT:8080" \
      -v "$MODEL_DIR:/models:ro" \
      -e GGML_CUDA_GRAPH_OPT=1 \
      "$IMAGE" \
      -m /models/Ornith-1.5-9B-Q4_K_L.gguf \
      -a ornith-local \
      -ngl 999 \
      -fa on \
      -ctk q4_0 \
      -ctv q4_0 \
      -c "$CTX_TOTAL" \
      -np "$SLOTS" \
      --no-kv-unified \
      --cont-batching \
      --no-mmproj \
      --n-predict "$MAX_OUTPUT" \
      -b 4096 \
      -ub 2048 \
      --cache-ram 32768 \
      --cache-prompt \
      --ctx-checkpoints 4 \
      --jinja \
      --metrics \
      --host 0.0.0.0 \
      --port 8080 \
      --temp 0.6 \
      --top-p 0.95 \
      --top-k 20 \
      --min-p 0 \
      >/dev/null


    READY=0

    for i in $(seq 1 120); do

        if curl -fsS \
          "http://127.0.0.1:$PORT/health" \
          >/dev/null 2>&1
        then
            READY=1
            break
        fi

        if ! docker ps --format '{{.Names}}' |
          grep -qx ornith
        then
            break
        fi

        sleep 1
    done


    [[ "$READY" == 1 ]] ||
      die "ornith no arrancó"


    LOGS="$(docker logs ornith 2>&1)"


    grep -Fq \
      "n_slots = 3, n_ctx_slot = 262144" \
      <<< "$LOGS" \
      || die "no quedó 3 × 256K"


    echo
    echo "Llama OK:"
    grep -F 'n_slots =' <<< "$LOGS" | tail -1

    echo
    echo "Args de Docker:"
    docker inspect ornith \
      --format '{{json .Args}}' |
      jq .

    echo
    echo "VRAM:"
    nvidia-smi \
      --query-gpu=index,memory.used,memory.free \
      --format=csv

    echo
}


# ============================================================
# SLOT STATUS
# ============================================================

slots() {

    curl -s "http://127.0.0.1:$PORT/slots" |
    jq -r '
      .[] |
      "SLOT \(.id)  task=\(.id_task)  busy=\(.is_processing)
ctx=\(.n_ctx)
prompt=\(.n_prompt_tokens)
processed=\(.n_prompt_tokens_processed)
cache=\(.n_prompt_tokens_cache)
decoded=\(.next_token[0].n_decoded // 0)
max_tokens=\(.params.max_tokens)
n_predict=\(.params.n_predict)
---------------------------------------------"
    '
}


# ============================================================
# PREPARE WORKSPACE
# ============================================================

prepare_agent() {

    local RUN="$1"
    local I="$2"
    local DIR="$RUN/workspaces/agent-$I"

    mkdir -p "$DIR"

    cp -a "$BASE/." "$DIR/"


    # Config V2 local a cada workspace.
    cat > "$DIR/opencode.json" <<EOF
{
  "\$schema": "https://opencode.ai/config.json",
  "model": "$MODEL",

  "providers": {
    "local-ornith": {
      "package": "@opencode/ai/providers/openai-compatible",
      "name": "Ornith 1.5 Local",

      "settings": {
        "baseURL": "http://127.0.0.1:$PORT/v1",
        "apiKey": "local-no-auth"
      },

      "models": {
        "ornith-local": {
          "modelID": "ornith-local",
          "name": "Ornith-1.5-9B Local",

          "capabilities": {
            "tools": true,
            "input": ["text"],
            "output": ["text"]
          },

          "compatibility": {
            "reasoningField": "reasoning_content"
          },

          "limit": {
            "context": 262144,
            "output": $MAX_OUTPUT
          }
        }
      }
    }
  }
}
EOF

    python3 -m json.tool \
      "$DIR/opencode.json" >/dev/null
}


# ============================================================
# AGENT PROCESS
# ============================================================

agent() {

    local RUN="$2"
    local I="$3"
    local PANE="$4"

    local DIR="$RUN/workspaces/agent-$I"
    local PROMPT="$RUN/prompts/agent-$I.txt"

    local LOG="$RUN/logs/agent-$I.log"
    local ERR="$RUN/logs/agent-$I.err"
    local STAT="$RUN/status/agent-$I.status"
    local TIMEFILE="$RUN/timing/agent-$I.txt"

    # --------------------------
    # Isolation
    # --------------------------

    export OPENCODE_CONFIG="$DIR/opencode.json"

    export XDG_CONFIG_HOME="$RUN/opencode-state/agent-$I/config"
    export XDG_DATA_HOME="$RUN/opencode-state/agent-$I/data"
    export XDG_CACHE_HOME="$RUN/opencode-state/agent-$I/cache"

    mkdir -p \
      "$XDG_CONFIG_HOME" \
      "$XDG_DATA_HOME" \
      "$XDG_CACHE_HOME"


    export OPENCODE_DISABLE_AUTOUPDATE=1

    # OpenCode-side extra cap.
    export OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX="$MAX_OUTPUT"

    # Go estaba faltando en el run anterior.
    export PATH="/usr/local/go/bin:$PATH"


    cd "$DIR"

    local START_NS
    START_NS=$(date +%s%N)

    local START
    START=$(date '+%H:%M:%S')

    echo "A$I RUNNING start=$START" > "$STAT"

    tmux select-pane \
      -t "$PANE" \
      -T "A$I RUN"


    clear

    echo "============================================================"
    echo " AGENT $I"
    echo "============================================================"
    echo
    echo "DIR:"
    echo "$DIR"
    echo
    echo "MODEL:"
    echo "$MODEL"
    echo
    echo "MAX OUTPUT:"
    echo "$MAX_OUTPUT"
    echo
    echo "PROMPT:"
    cat "$PROMPT"
    echo
    echo "============================================================"
    echo


    set +e


    # IMPORTANT:
    #
    # --standalone = private OpenCode server per agent.
    #
    # timeout creates a process group.
    # SIGINT first; SIGKILL after 15 sec if needed.
    #
    # NO shared service => no zombie sessions.
    #
    timeout \
      --signal=INT \
      --kill-after=15s \
      300s \
      opencode run \
      --standalone \
      --auto \
      --model "$MODEL" \
      "$(cat "$PROMPT")" \
      > >(tee "$LOG") \
      2> >(tee "$ERR" >&2)

    RC=$?


    set -e


    local END_NS
    END_NS=$(date +%s%N)

    local WALL
    WALL=$(
      python3 - "$START_NS" "$END_NS" <<'PY2'
import sys
a, b = map(int, sys.argv[1:])
print(f"{(b-a)/1e9:.3f}")
PY2
    )


    local END
    END=$(date '+%H:%M:%S')


    if [[ "$RC" == 0 ]]; then

        STATE="DONE"

    elif [[ "$RC" == 124 || "$RC" == 137 ]]; then

        STATE="TIMEOUT"

    else

        STATE="FAIL"

    fi


    printf \
      "A%s %s wall=%ss rc=%s\n" \
      "$I" "$STATE" "$WALL" "$RC" \
      > "$STAT"


    printf \
      "agent=%s state=%s rc=%s wall=%ss start=%s end=%s\n" \
      "$I" "$STATE" "$RC" "$WALL" "$START" "$END" \
      > "$TIMEFILE"


    tmux select-pane \
      -t "$PANE" \
      -T "A$I $STATE"


    echo
    echo "============================================================"
    echo " AGENT $I $STATE"
    echo " RC   : $RC"
    echo " WALL : ${WALL}s"
    echo "============================================================"
    echo

    exec bash
}


# ============================================================
# STATUS
# ============================================================

status() {

    LAST="$RUNS/LAST_RUN"

    [[ -f "$LAST" ]] || {
        echo "No hay run."
        exit 0
    }

    RUN="$(cat "$LAST")"

    clear

    echo "ORNITH / OPENCODE"
    echo "============================================================"
    echo "$RUN"
    echo

    for i in {1..6}; do

        F="$RUN/status/agent-$i.status"

        if [[ -f "$F" ]]; then
            cat "$F"
        else
            echo "A$i WAIT"
        fi

    done


    echo
    echo "============================================================"

    DONE=$(
      grep -hE ' DONE ' \
        "$RUN"/status/*.status 2>/dev/null |
      wc -l || true
    )

    TIMEOUT=$(
      grep -hE ' TIMEOUT ' \
        "$RUN"/status/*.status 2>/dev/null |
      wc -l || true
    )

    FAIL=$(
      grep -hE ' FAIL ' \
        "$RUN"/status/*.status 2>/dev/null |
      wc -l || true
    )

    echo "DONE=$DONE  TIMEOUT=$TIMEOUT  FAIL=$FAIL"
    echo

    echo "GPU:"
    nvidia-smi \
      --query-gpu=memory.used,memory.free,utilization.gpu,power.draw,temperature.gpu \
      --format=csv,noheader \
      -i 0

    echo
    echo "OpenCode → llama connections:"
    ss -tnp |
      grep ':8120' |
      grep -c opencode || true
}


# ============================================================
# RESULTS
# ============================================================

results() {

    LAST="$RUNS/LAST_RUN"

    [[ -f "$LAST" ]] ||
      die "No hay LAST_RUN"

    RUN="$(cat "$LAST")"

    echo
    echo "RUN:"
    echo "$RUN"
    echo

    echo "TIMINGS:"
    echo "============================================================"

    for i in {1..6}; do

        F="$RUN/timing/agent-$i.txt"

        if [[ -f "$F" ]]; then
            cat "$F"
        else
            echo "agent=$i sin timing"
        fi

    done

    echo
    echo "LOG SIZES:"
    echo "============================================================"

    du -h "$RUN"/logs/agent-*.log 2>/dev/null || true

    echo
    echo "ACTIVE OPENCODE LAB PROCESSES:"
    echo "============================================================"

    pgrep -af \
      'opencode run.*local-ornith/ornith-local|opencode\.exe serve --stdio' \
      || echo "ninguno"

    echo
    echo "LLAMA SLOTS:"
    echo "============================================================"

    slots
}


# ============================================================
# STOP CURRENT LAB
# ============================================================

stop_lab() {

    echo "Deteniendo agentes actuales..."

    pkill -TERM -f \
      'opencode run.*local-ornith/ornith-local' \
      2>/dev/null || true

    pkill -TERM -f \
      'opencode\.exe serve --stdio --port 0' \
      2>/dev/null || true

    sleep 3


    pkill -KILL -f \
      'opencode run.*local-ornith/ornith-local' \
      2>/dev/null || true

    pkill -KILL -f \
      'opencode\.exe serve --stdio --port 0' \
      2>/dev/null || true


    echo
    echo "Reiniciando ornith para cancelar cualquier SSE huérfano..."

    docker restart ornith >/dev/null

    for i in $(seq 1 120); do
        curl -fsS \
          "http://127.0.0.1:$PORT/health" \
          >/dev/null 2>&1 && break

        sleep 1
    done

    echo
    echo "SLOTS:"
    slots
}


# ============================================================
# RUN
# ============================================================

run() {

    cleanup_old
    start_llama


    RUN="$RUNS/run-$(date -u +%Y%m%dT%H%M%SZ)"

    mkdir -p \
      "$RUN"/{workspaces,prompts,logs,status,timing,opencode-state}

    echo "$RUN" > "$RUNS/LAST_RUN"


    # --------------------------------------------------------
    # Controlled prompts.
    #
    # Do enough tool work to behave like coding agents,
    # but don't ask them to redesign the world.
    # --------------------------------------------------------

    cat > "$RUN/prompts/agent-1.txt" <<'EOF'
Inspect worker.go and worker_test.go.
Fix the worker pool lifecycle and graceful shutdown.
Use tools, edit files, and run the relevant tests.
Keep the change focused.
Stop when the tests pass.
Give a concise final summary.
EOF


    cat > "$RUN/prompts/agent-2.txt" <<'EOF'
Inspect dedupe.go.
Fix the most important concurrency and unbounded-memory problem.
Add focused tests.
Run only the relevant tests.
Stop when they pass.
Keep the final summary concise.
EOF


    cat > "$RUN/prompts/agent-3.txt" <<'EOF'
Review worker.go for goroutine leaks and channel ownership bugs.
Implement one focused production-quality fix.
Run the relevant tests.
Do not over-engineer.
Stop when tests pass.
EOF


    cat > "$RUN/prompts/agent-4.txt" <<'EOF'
Inspect worker_test.go and worker.go.
Add one useful test for graceful cancellation or shutdown.
Make only the code changes required for that test.
Run the relevant tests.
Stop when they pass.
EOF


    cat > "$RUN/prompts/agent-5.txt" <<'EOF'
Inspect this project for backpressure problems.
Implement one focused bounded-concurrency improvement.
Run the relevant tests.
Stop when tests pass.
Give a short final summary.
EOF


    cat > "$RUN/prompts/agent-6.txt" <<'EOF'
Review all Go files as a senior Go reviewer.
Do not modify anything.
Run tests if useful.
Report the five most important correctness or concurrency findings.
Be concise and finish.
EOF


    for i in {1..6}; do
        prepare_agent "$RUN" "$i"
        echo "A$i WAIT" > "$RUN/status/agent-$i.status"
    done


    # --------------------------------------------------------
    # Recreate OpenCode tab
    # --------------------------------------------------------

    tmux kill-window \
      -t llama:OpenCode \
      2>/dev/null || true

    tmux new-window \
      -d \
      -t llama \
      -n OpenCode


    # Start with one pane and split until 6.
    for n in 1 2 3 4 5; do
        tmux split-window \
          -d \
          -t llama:OpenCode
    done

    tmux select-layout \
      -t llama:OpenCode \
      tiled

    tmux set-option \
      -t llama:OpenCode \
      pane-border-status top


    mapfile -t PANES < <(
      tmux list-panes \
        -t llama:OpenCode \
        -F '#{pane_id}'
    )


    [[ "${#PANES[@]}" == 6 ]] ||
      die "No pude crear 6 panes"


    # --------------------------------------------------------
    # Check tab
    # --------------------------------------------------------

    tmux send-keys \
      -t llama:Check \
      C-c

    tmux send-keys \
      -t llama:Check \
      "watch -n 1 '$SELF status'" \
      C-m


    echo
    echo "============================================================"
    echo " LANZANDO 6 AGENTES"
    echo "============================================================"
    echo
    echo "$RUN"
    echo


    for i in {1..6}; do

        P="${PANES[$((i-1))]}"

        tmux select-pane \
          -t "$P" \
          -T "A$i WAIT"

        tmux send-keys \
          -t "$P" \
          "$SELF agent '$RUN' '$i' '$P'" \
          C-m

    done


    echo
    echo "OpenCode : 6 agentes"
    echo "Check    : status"
    echo "Log      : llama-server"
    echo "Monitor  : GPU / VM"
    echo
    echo "Resultados:"
    echo "  $SELF results"
    echo
    echo "Stop:"
    echo "  $SELF stop"
    echo

    tmux select-window \
      -t llama:OpenCode
}


# ============================================================
# ROUTER
# ============================================================

CMD="${1:-run}"

case "$CMD" in

run)
    run
    ;;

agent)
    agent "$@"
    ;;

cleanup)
    cleanup_old
    ;;

slots)
    slots
    ;;

watch)
    watch -n 1 "$SELF slots"
    ;;

status)
    status
    ;;

results)
    results
    ;;

stop)
    stop_lab
    ;;

*)
    echo "Uso:"
    echo
    echo "  $SELF run"
    echo "  $SELF status"
    echo "  $SELF slots"
    echo "  $SELF watch"
    echo "  $SELF results"
    echo "  $SELF stop"
    exit 1
    ;;

esac
