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

# ============================================================================
# Ornith P2 — Long-context retrieval / needle validation
#
# Profiles:
#   P2-NATIVE-262K : 262144 ctx, native RoPE, 1 slot
#   P2-YARN-512K   : 524288 ctx, YaRN x4, 1 slot
#
# Each profile runs 2 cold rounds with three unique needles placed near:
#   12%, 50%, 88% of the long document.
#
# PASS requires the FINAL assistant content (not reasoning_content) to contain
# all 3 exact codes in the requested A=/B=/C= format.
#
# Output budget is 4096 to avoid invalidating retrieval because Ornith spends
# the whole budget reasoning.
#
# Baseline restored on success OR failure:
#   3 x 256K, Q4/Q4, FA ON, DFlash OFF, cache ON, hard output 4096.
# ============================================================================

ROOT="${HOME}/bench/ornith"
P2ROOT="${ROOT}/p2"

IMAGE="lomelidev/llama.cpp:cu130-nccl-sse-faall-10974"
MODEL_DIR="${HOME}/models/ornith15-9b"
MODEL_FILE="Ornith-1.5-9B-Q4_K_L.gguf"

PORT=8120
MODEL_ALIAS="ornith-local"

KV_K="q4_0"
KV_V="q4_0"

OUTPUT_LIMIT=4096
BASELINE_OUTPUT=4096
CACHE_RAM=32768

ROUNDS=2

# Leave comfortable headroom for chat-template overhead + 4096 output.
TARGET_NATIVE=245000
TARGET_YARN=500000

RUN=""
MON_PID=""
RESTORE_ON_EXIT=0

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

mkdir -p "$P2ROOT"

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

utc_stamp() {
    date -u +%Y%m%dT%H%M%SZ
}

need_cmd() {
    command -v "$1" >/dev/null 2>&1 || die "Falta comando: $1"
}

preflight_tools() {
    local c
    for c in docker curl jq python3 nvidia-smi tmux ss sha256sum awk sed grep column; do
        need_cmd "$c"
    done

    [[ -f "$MODEL_DIR/$MODEL_FILE" ]] || die "No existe modelo: $MODEL_DIR/$MODEL_FILE"
    docker image inspect "$IMAGE" >/dev/null 2>&1 || die "No existe imagen Docker: $IMAGE"
    tmux has-session -t llama 2>/dev/null || die "No existe tmux session 'llama'"
}

# ----------------------------------------------------------------------------
# Clean possible contamination
# ----------------------------------------------------------------------------

clean_old_clients() {
    echo
    echo "== Limpiando clientes que puedan contaminar :8120 =="

    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 1
    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

    mapfile -t PIDS < <(
        ss -tnp 2>/dev/null \
          | grep ':8120' \
          | grep -oE 'pid=[0-9]+' \
          | cut -d= -f2 \
          | sort -u \
          || true
    )

    if ((${#PIDS[@]})); then
        echo "Cortando clientes conectados a :8120: ${PIDS[*]}"
        kill -TERM "${PIDS[@]}" 2>/dev/null || true
        sleep 1
        local pid
        for pid in "${PIDS[@]}"; do
            kill -KILL "$pid" 2>/dev/null || true
        done
    fi

    if ss -tnp 2>/dev/null | grep -q ':8120'; then
        echo "Conexiones restantes:"
        ss -tnp 2>/dev/null | grep ':8120' || true
    else
        echo "Sin conexiones a :8120"
    fi

    return 0
}

# ----------------------------------------------------------------------------
# llama lifecycle
# ----------------------------------------------------------------------------

start_profile() {
    local profile="$1"

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

    local ctx
    local -a rope_args=()

    case "$profile" in
        native)
            ctx=262144
            ;;
        yarn)
            ctx=524288
            rope_args=(
                --rope-scaling yarn
                --rope-scale 4
                --yarn-orig-ctx 262144
            )
            ;;
        *)
            die "Perfil desconocido: $profile"
            ;;
    esac

    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/${MODEL_FILE}" \
      -a "$MODEL_ALIAS" \
      -ngl 999 \
      -fa on \
      -ctk "$KV_K" \
      -ctv "$KV_V" \
      -c "$ctx" \
      -np 1 \
      --no-kv-unified \
      --cont-batching \
      --no-mmproj \
      --n-predict "$OUTPUT_LIMIT" \
      "${rope_args[@]}" \
      -b 4096 \
      -ub 2048 \
      --cache-ram "$CACHE_RAM" \
      --cache-prompt \
      --ctx-checkpoints 4 \
      --jinja \
      --metrics \
      --host 0.0.0.0 \
      --port 8080 \
      --temp 0.0 \
      --top-p 1.0 \
      --top-k 0 \
      --min-p 0 \
      >/dev/null

    local ok=0
    local _
    for _ in $(seq 1 180); do
        if curl -fsS "http://127.0.0.1:${PORT}/health" >/dev/null 2>&1; then
            ok=1
            break
        fi

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

        sleep 1
    done

    if [[ "$ok" != "1" ]]; then
        docker logs ornith 2>&1 | tail -160 || true
        die "llama no arrancó para profile=$profile"
    fi

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

    if grep -q 'no FlashAttention vector kernel' <<<"$logs"; then
        die "FA fallback detectado"
    fi

    if grep -Eqi 'cudaMalloc failed|out of memory|capping' <<<"$logs"; then
        die "OOM/capping detectado"
    fi

    if [[ "$profile" == "native" ]]; then
        grep -Fq 'n_slots = 1, n_ctx_slot = 262144' <<<"$logs" \
          || die "Native no quedó 1×262144"
    else
        grep -Fq 'n_slots = 1, n_ctx_slot = 524288' <<<"$logs" \
          || die "YaRN no quedó 1×524288"

        local args
        args="$(docker inspect ornith --format '{{json .Args}}')"

        grep -q -- '--rope-scaling' <<<"$args" || die "Falta --rope-scaling en YaRN"
        grep -q -- 'yarn' <<<"$args" || die "Falta yarn en args"
        grep -q -- '--rope-scale' <<<"$args" || die "Falta --rope-scale"
        grep -q -- '--yarn-orig-ctx' <<<"$args" || die "Falta --yarn-orig-ctx"
    fi

    return 0
}

restore_baseline() {
    echo
    echo "== Restaurando baseline 3x256K / Q4Q4 / FA / cache ON / output=4096 =="

    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/${MODEL_FILE}" \
      -a "$MODEL_ALIAS" \
      -ngl 999 \
      -fa on \
      -ctk "$KV_K" \
      -ctv "$KV_V" \
      -c 786432 \
      -np 3 \
      --no-kv-unified \
      --cont-batching \
      --no-mmproj \
      --n-predict "$BASELINE_OUTPUT" \
      -b 4096 \
      -ub 2048 \
      --cache-ram "$CACHE_RAM" \
      --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

    local ok=0
    local _
    for _ in $(seq 1 180); do
        if curl -fsS "http://127.0.0.1:${PORT}/health" >/dev/null 2>&1; then
            ok=1
            break
        fi
        sleep 1
    done

    [[ "$ok" == "1" ]] || die "No pude restaurar baseline"

    curl -s "http://127.0.0.1:${PORT}/slots" \
      | jq -r '.[] | "slot=\(.id) ctx=\(.n_ctx) busy=\(.is_processing) max=\(.params.max_tokens)"'

    return 0
}

# ----------------------------------------------------------------------------
# Prompt builder and request client
# ----------------------------------------------------------------------------

write_helpers() {
    cat > "$RUN/build_prompt.py" <<'PY'
#!/usr/bin/env python3
import hashlib
import http.client
import json
import random
import sys
from pathlib import Path

out_prompt = Path(sys.argv[1])
out_expected = Path(sys.argv[2])
target_tokens = int(sys.argv[3])
profile = sys.argv[4]
round_no = int(sys.argv[5])
port = int(sys.argv[6])

seed = 20260917 + round_no * 1000 + (0 if profile == "native" else 500000)
rng = random.Random(seed)

codes = {
    "A": f"A-{profile.upper()}-R{round_no}-{rng.randrange(16**10):010X}",
    "B": f"B-{profile.upper()}-R{round_no}-{rng.randrange(16**10):010X}",
    "C": f"C-{profile.upper()}-R{round_no}-{rng.randrange(16**10):010X}",
}

header = """You are reading a long synthetic technical archive.
Three retrieval needles are embedded in the archive.
At the very end you will receive a question.
Do not summarize the archive.
Do not invent values.
The final answer MUST contain exactly these three lines and no explanation:
A=<exact code>
B=<exact code>
C=<exact code>

BEGIN ARCHIVE
"""

footer = """
END ARCHIVE

RETRIEVAL QUESTION:
Find the three secret codes associated with labels A, B, and C.
Return exactly:
A=<exact code>
B=<exact code>
C=<exact code>
No explanation. No prose. No markdown.
"""

def filler_line(i: int) -> str:
    # Unique deterministic text, deliberately not repetitive boilerplate only.
    h = hashlib.sha256(f"{profile}-{round_no}-{i}".encode()).hexdigest()
    return (
        f"RECORD-{i:07d} subsystem=s{i%97:02d} shard={i%31:02d} "
        f"counter={i*17+13} token={h[:16]} digest={h[16:40]} "
        f"note=synthetic-archive-entry-{(i*7919)%1000003}\n"
    )

def make_text(n_lines: int) -> str:
    p1 = max(1, int(n_lines * 0.12))
    p2 = max(p1 + 1, int(n_lines * 0.50))
    p3 = max(p2 + 1, int(n_lines * 0.88))

    parts = [header]
    for i in range(1, n_lines + 1):
        parts.append(filler_line(i))
        if i == p1:
            parts.append(f"\nNEEDLE LABEL A SECRET CODE: {codes['A']}\n\n")
        if i == p2:
            parts.append(f"\nNEEDLE LABEL B SECRET CODE: {codes['B']}\n\n")
        if i == p3:
            parts.append(f"\nNEEDLE LABEL C SECRET CODE: {codes['C']}\n\n")
    parts.append(footer)
    return "".join(parts)

def tokenize(text: str) -> int:
    body = json.dumps({"content": text}).encode()
    conn = http.client.HTTPConnection("127.0.0.1", port, timeout=120)
    conn.request(
        "POST",
        "/tokenize",
        body=body,
        headers={
            "Content-Type": "application/json",
            "Content-Length": str(len(body)),
        },
    )
    r = conn.getresponse()
    raw = r.read()
    conn.close()
    if r.status < 200 or r.status >= 300:
        raise RuntimeError(f"/tokenize status={r.status}: {raw[:500]!r}")
    obj = json.loads(raw)
    tokens = obj.get("tokens")
    if isinstance(tokens, list):
        return len(tokens)
    if isinstance(tokens, int):
        return tokens
    raise RuntimeError(f"Unexpected /tokenize response keys: {obj.keys()}")

# Estimate tokens/line using a medium sample.
sample_n = 1000
sample_t = tokenize(make_text(sample_n))
per_line = max(1.0, (sample_t - 300) / sample_n)
guess = max(100, int(target_tokens / per_line))

# Iteratively scale line count to land near target. We deliberately target below
# the hard context limit, so +/- ~500 tokens is harmless.
best = None
for _ in range(6):
    text = make_text(guess)
    n = tokenize(text)

    if best is None or abs(n - target_tokens) < abs(best[0] - target_tokens):
        best = (n, guess, text)

    if abs(n - target_tokens) <= 250:
        break

    scale = target_tokens / max(1, n)
    new_guess = max(100, int(guess * scale))

    if new_guess == guess:
        new_guess += 1 if n < target_tokens else -1

    guess = new_guess

actual_tokens, n_lines, final_text = best

out_prompt.write_text(final_text)
out_expected.write_text(
    json.dumps(
        {
            "profile": profile,
            "round": round_no,
            "target_tokens": target_tokens,
            "raw_prompt_tokens": actual_tokens,
            "filler_lines": n_lines,
            "codes": codes,
            "positions": {"A": 0.12, "B": 0.50, "C": 0.88},
        },
        indent=2,
    ) + "\n"
)

print(json.dumps({
    "target_tokens": target_tokens,
    "raw_prompt_tokens": actual_tokens,
    "filler_lines": n_lines,
    "codes": codes,
}))
PY

    cat > "$RUN/chat_stream.py" <<'PY'
#!/usr/bin/env python3
import http.client
import json
import sys
import time
from pathlib import Path

prompt_path = Path(sys.argv[1])
expected_path = Path(sys.argv[2])
sse_path = Path(sys.argv[3])
result_path = Path(sys.argv[4])
port = int(sys.argv[5])
output_limit = int(sys.argv[6])

prompt = prompt_path.read_text()
expected = json.loads(expected_path.read_text())

payload = {
    "model": "ornith-local",
    "messages": [
        {"role": "user", "content": prompt}
    ],
    "stream": True,
    "max_tokens": output_limit,
    "temperature": 0.0,
    "top_p": 1.0,
    "top_k": 0,
    "seed": 1,
}

body = json.dumps(payload).encode()

conn = http.client.HTTPConnection("127.0.0.1", port, timeout=1800)

t0 = time.monotonic()
epoch0 = time.time()

conn.request(
    "POST",
    "/v1/chat/completions",
    body=body,
    headers={
        "Content-Type": "application/json",
        "Content-Length": str(len(body)),
        "Connection": "close",
    },
)

resp = conn.getresponse()

first_delta = None
content_parts = []
reasoning_parts = []
finish_reason = None
last_obj = None

with sse_path.open("wb") as raw:
    while True:
        line = resp.readline()
        if not line:
            break

        raw.write(line)
        raw.flush()

        stripped = line.strip()

        if not stripped.startswith(b"data:"):
            continue

        data = stripped[5:].strip()

        if not data or data == b"[DONE]":
            continue

        try:
            obj = json.loads(data)
        except Exception:
            continue

        last_obj = obj

        choices = obj.get("choices") or []
        if not choices:
            continue

        c = choices[0]
        delta = c.get("delta") or {}

        content = delta.get("content")
        reasoning = delta.get("reasoning_content")

        if content:
            if first_delta is None:
                first_delta = time.monotonic()
            content_parts.append(content)

        if reasoning:
            if first_delta is None:
                first_delta = time.monotonic()
            reasoning_parts.append(reasoning)

        if c.get("finish_reason") is not None:
            finish_reason = c.get("finish_reason")

t1 = time.monotonic()
epoch1 = time.time()

content = "".join(content_parts)
reasoning = "".join(reasoning_parts)

codes = expected["codes"]

expected_lines = {
    "A": f"A={codes['A']}",
    "B": f"B={codes['B']}",
    "C": f"C={codes['C']}",
}

content_hits = {k: (v in content) for k, v in expected_lines.items()}
reasoning_hits = {k: (codes[k] in reasoning) for k in ("A", "B", "C")}

success = all(content_hits.values())

timings = {}
usage = {}

if isinstance(last_obj, dict):
    timings = last_obj.get("timings") or {}
    usage = last_obj.get("usage") or {}

result = {
    "http_status": resp.status,
    "start_epoch": epoch0,
    "end_epoch": epoch1,
    "ttft_ms": None if first_delta is None else (first_delta - t0) * 1000.0,
    "total_ms": (t1 - t0) * 1000.0,
    "finish_reason": finish_reason,
    "content": content,
    "reasoning_content": reasoning,
    "content_length": len(content),
    "reasoning_length": len(reasoning),
    "expected_lines": expected_lines,
    "content_hits": content_hits,
    "reasoning_hits": reasoning_hits,
    "success": success,
    "timings": timings,
    "usage": usage,
    "last_chunk": last_obj,
}

result_path.write_text(json.dumps(result, indent=2) + "\n")

conn.close()

if resp.status < 200 or resp.status >= 300:
    sys.exit(2)
PY

    cat > "$RUN/poll_slot.py" <<'PY'
#!/usr/bin/env python3
import json
import os
import sys
import time
import urllib.request
from pathlib import Path

pid = int(sys.argv[1])
port = int(sys.argv[2])
jsonl_path = Path(sys.argv[3])
summary_path = Path(sys.argv[4])

summary = {
    "actual_slot": None,
    "task_id": None,
    "prompt_tokens": 0,
    "processed_tokens": 0,
    "cached_tokens": 0,
    "decoded_tokens": 0,
    "max_tokens": None,
}

def alive(p):
    try:
        os.kill(p, 0)
        return True
    except OSError:
        return False

def fetch_slots():
    try:
        with urllib.request.urlopen(
            f"http://127.0.0.1:{port}/slots",
            timeout=0.75,
        ) as r:
            return json.loads(r.read())
    except Exception:
        return []

seen_task = None
idle_after = 0

with jsonl_path.open("w") as out:
    while True:
        slots = fetch_slots()
        now = time.time()

        active = [s for s in slots if s.get("is_processing")]

        target = None

        if seen_task is not None:
            for s in active:
                if s.get("id_task") == seen_task:
                    target = s
                    break

        if target is None and active:
            target = active[0]
            seen_task = target.get("id_task")

        if target is not None:
            rec = {
                "ts": now,
                "id": target.get("id"),
                "id_task": target.get("id_task"),
                "n_ctx": target.get("n_ctx"),
                "prompt": target.get("n_prompt_tokens") or 0,
                "processed": target.get("n_prompt_tokens_processed") or 0,
                "cache": target.get("n_prompt_tokens_cache") or 0,
                "decoded": ((target.get("next_token") or [{}])[0].get("n_decoded") or 0),
                "max_tokens": (target.get("params") or {}).get("max_tokens"),
            }

            out.write(json.dumps(rec) + "\n")
            out.flush()

            summary["actual_slot"] = rec["id"]
            summary["task_id"] = rec["id_task"]
            summary["prompt_tokens"] = max(summary["prompt_tokens"], rec["prompt"])
            summary["processed_tokens"] = max(summary["processed_tokens"], rec["processed"])
            summary["cached_tokens"] = max(summary["cached_tokens"], rec["cache"])
            summary["decoded_tokens"] = max(summary["decoded_tokens"], rec["decoded"])
            summary["max_tokens"] = rec["max_tokens"]
            idle_after = 0
        elif not alive(pid):
            idle_after += 1

        if not alive(pid) and idle_after >= 3:
            break

        time.sleep(0.05)

summary_path.write_text(json.dumps(summary, indent=2) + "\n")
PY

    chmod +x "$RUN/build_prompt.py" "$RUN/chat_stream.py" "$RUN/poll_slot.py"
}

# ----------------------------------------------------------------------------
# Monitor / tmux
# ----------------------------------------------------------------------------

start_monitor() {
    (
        echo "timestamp,mem_available_kib,load1,psi_cpu,psi_mem,psi_io,gpu_util,gpu_used_mib,gpu_free_mib,power_w,temp_c"

        while true; do
            local_ts="$(date -u +%Y-%m-%dT%H:%M:%S.%3NZ)"
            mem="$(awk '/MemAvailable:/ {print $2}' /proc/meminfo)"
            load1="$(awk '{print $1}' /proc/loadavg)"
            pcpu="$(awk '/some/ {for(i=1;i<=NF;i++) if($i ~ /^avg10=/){split($i,a,"=");print a[2]}}' /proc/pressure/cpu)"
            pmem="$(awk '/some/ {for(i=1;i<=NF;i++) if($i ~ /^avg10=/){split($i,a,"=");print a[2]}}' /proc/pressure/memory)"
            pio="$(awk '/some/ {for(i=1;i<=NF;i++) if($i ~ /^avg10=/){split($i,a,"=");print a[2]}}' /proc/pressure/io)"
            gpu="$(nvidia-smi -i 0 \
              --query-gpu=utilization.gpu,memory.used,memory.free,power.draw,temperature.gpu \
              --format=csv,noheader,nounits)"
            echo "${local_ts},${mem},${load1},${pcpu},${pmem},${pio},${gpu}"
            sleep 1
        done
    ) > "$RUN/system.csv" &

    MON_PID=$!
    echo "$MON_PID" > "$RUN/monitor.pid"
}

stop_monitor() {
    if [[ -n "${MON_PID:-}" ]]; then
        kill "$MON_PID" 2>/dev/null || true
        wait "$MON_PID" 2>/dev/null || true
        MON_PID=""
    fi
}

ensure_window() {
    local name="$1"
    if ! tmux list-windows -t llama -F '#{window_name}' | grep -qx "$name"; then
        tmux new-window -d -t llama -n "$name"
    fi
}

setup_tmux() {
    local w
    for w in Log Check Test Monitor OpenCode; do
        ensure_window "$w"
    done

    tmux send-keys -t llama:Log C-c
    tmux send-keys -t llama:Log \
      'while true; do clear; docker logs -f ornith 2>&1; sleep 1; done' C-m

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

    tmux send-keys -t llama:OpenCode C-c
    tmux send-keys -t llama:OpenCode \
      "watch -n 1 '$SELF slots'" C-m
}

# ----------------------------------------------------------------------------
# Progress / views
# ----------------------------------------------------------------------------

update_progress() {
    local scenario="$1"
    local round="$2"
    local phase="$3"

    python3 - "$RUN/progress.json" "$scenario" "$round" "$phase" <<'PY'
import json, os, sys, time

path, scenario, rnd, phase = sys.argv[1:]
obj = {
    "scenario": scenario,
    "round": int(rnd),
    "phase": phase,
    "updated_epoch": time.time(),
}
tmp = path + ".tmp"
with open(tmp, "w") as f:
    json.dump(obj, f)
os.replace(tmp, path)
PY
}

dashboard() {
    [[ -f "$ROOT/LAST_P2" ]] || {
        echo "No hay LAST_P2 todavía."
        return
    }

    local d
    d="$(cat "$ROOT/LAST_P2")"

    clear
    echo "ORNITH P2 — LONG-CONTEXT RETRIEVAL"
    echo "============================================================================"
    echo "$d"
    echo

    if [[ -f "$d/progress.json" ]]; then
        jq -r '
          "Current : \(.scenario)
Round   : \(.round)
Phase   : \(.phase)"
        ' "$d/progress.json"
    else
        echo "Esperando..."
    fi

    echo
    echo "RESULTADOS"
    echo "============================================================================"
    if [[ -s "$d/summary.tsv" ]]; then
        tail -n 10 "$d/summary.tsv" | column -t -s $'\t'
    fi

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

slots_view() {
    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)
---------------------------------------------"
      '
}

# ----------------------------------------------------------------------------
# One round
# ----------------------------------------------------------------------------

run_round() {
    local label="$1"      # P2-NATIVE-262K / P2-YARN-512K
    local profile="$2"    # native/yarn
    local target="$3"
    local round="$4"

    local case_dir="$RUN/cases/$label/round-$(printf '%02d' "$round")"
    mkdir -p "$case_dir"

    update_progress "$label" "$round" "startup"

    # Cold server per round: no prompt-cache contamination.
    start_profile "$profile"

    update_progress "$label" "$round" "build-prompt"

    echo
    echo "Building prompt: $label round=$round target=${target} tokens"

    python3 "$RUN/build_prompt.py" \
      "$case_dir/prompt.txt" \
      "$case_dir/expected.json" \
      "$target" \
      "$profile" \
      "$round" \
      "$PORT" \
      > "$case_dir/build.json"

    cat "$case_dir/build.json" | jq .

    update_progress "$label" "$round" "retrieval"

    local log_before
    log_before="$(docker logs ornith 2>&1 | wc -l)"

    python3 "$RUN/chat_stream.py" \
      "$case_dir/prompt.txt" \
      "$case_dir/expected.json" \
      "$case_dir/response.sse" \
      "$case_dir/response.json" \
      "$PORT" \
      "$OUTPUT_LIMIT" &
    local req_pid=$!

    python3 "$RUN/poll_slot.py" \
      "$req_pid" \
      "$PORT" \
      "$case_dir/slots.jsonl" \
      "$case_dir/slots-summary.json" &
    local poll_pid=$!

    local rc=0
    wait "$req_pid" || rc=$?
    wait "$poll_pid" || true

    docker logs ornith 2>&1 \
      | tail -n "+$((log_before + 1))" \
      > "$case_dir/llama.log"

    if [[ "$rc" != "0" ]]; then
        cat "$case_dir/response.json" 2>/dev/null || true
        tail -120 "$case_dir/llama.log" || true
        die "$label round=$round request rc=$rc"
    fi

    local capping fa_warn
    capping="$(grep -ci 'capping' "$case_dir/llama.log" || true)"
    fa_warn="$(grep -c 'no FlashAttention vector kernel' "$case_dir/llama.log" || true)"

    [[ "$capping" == "0" ]] || die "$label round=$round tuvo capping"
    [[ "$fa_warn" == "0" ]] || die "$label round=$round tuvo FA fallback"

    python3 - \
      "$label" "$profile" "$round" "$target" \
      "$case_dir/expected.json" \
      "$case_dir/response.json" \
      "$case_dir/slots-summary.json" \
      "$case_dir/result.json" \
      "$RUN/results.jsonl" \
      "$RUN/summary.tsv" <<'PY'
import json
import sys

(
    label, profile, rnd, target,
    expected_path, response_path, slots_path,
    result_path, jsonl_path, tsv_path,
) = sys.argv[1:]

expected = json.load(open(expected_path))
response = json.load(open(response_path))
slots = json.load(open(slots_path))

last = response.get("last_chunk") or {}
timings = response.get("timings") or {}
usage = response.get("usage") or {}

row = {
    "scenario": label,
    "profile": profile,
    "round": int(rnd),
    "target_raw_tokens": int(target),
    "raw_prompt_tokens": expected.get("raw_prompt_tokens"),
    "server_prompt_tokens": slots.get("prompt_tokens"),
    "server_processed_tokens": slots.get("processed_tokens"),
    "server_cached_tokens": slots.get("cached_tokens"),
    "decoded_tokens": slots.get("decoded_tokens"),
    "max_tokens": slots.get("max_tokens"),
    "ttft_ms": response.get("ttft_ms"),
    "total_ms": response.get("total_ms"),
    "finish_reason": response.get("finish_reason"),
    "content_length": response.get("content_length"),
    "reasoning_length": response.get("reasoning_length"),
    "content_hits": response.get("content_hits"),
    "reasoning_hits": response.get("reasoning_hits"),
    "success": bool(response.get("success")),
    "prompt_tps": timings.get("prompt_per_second"),
    "gen_tps": timings.get("predicted_per_second"),
    "usage_prompt_tokens": usage.get("prompt_tokens"),
    "usage_completion_tokens": usage.get("completion_tokens"),
    "codes": expected.get("codes"),
}

with open(result_path, "w") as f:
    json.dump(row, f, indent=2)
    f.write("\n")

with open(jsonl_path, "a") as f:
    f.write(json.dumps(row) + "\n")

hits = row["content_hits"] or {}
hit_str = "".join(k if hits.get(k) else "-" for k in ("A", "B", "C"))

fields = [
    row["scenario"], row["round"],
    row["raw_prompt_tokens"], row["server_prompt_tokens"],
    row["server_processed_tokens"], row["server_cached_tokens"],
    row["decoded_tokens"],
    f'{row["ttft_ms"]:.2f}' if isinstance(row["ttft_ms"], (int, float)) else "",
    f'{row["total_ms"]:.2f}' if isinstance(row["total_ms"], (int, float)) else "",
    f'{row["prompt_tps"]:.2f}' if isinstance(row["prompt_tps"], (int, float)) else "",
    f'{row["gen_tps"]:.2f}' if isinstance(row["gen_tps"], (int, float)) else "",
    row["finish_reason"] or "",
    row["content_length"],
    row["reasoning_length"],
    hit_str,
    "PASS" if row["success"] else "FAIL",
]

with open(tsv_path, "a") as f:
    f.write("\t".join("" if x is None else str(x) for x in fields) + "\n")
PY

    local result
    result="$(jq -r '.success' "$case_dir/result.json")"

    echo
    echo "RESULT $label round=$round"
    jq '{
      success,
      finish_reason,
      raw_prompt_tokens,
      server_prompt_tokens,
      server_processed_tokens,
      decoded_tokens,
      ttft_ms,
      total_ms,
      prompt_tps,
      gen_tps,
      content_hits,
      reasoning_hits,
      content_length,
      reasoning_length
    }' "$case_dir/result.json"

    # A failed retrieval is a valid benchmark result; do NOT abort the suite.
    if [[ "$result" != "true" ]]; then
        echo "WARNING: retrieval FAIL in $label round=$round"
    fi
}

# ----------------------------------------------------------------------------
# Report
# ----------------------------------------------------------------------------

make_report() {
    python3 - "$RUN" <<'PY'
from pathlib import Path
import json
import statistics
import sys

run = Path(sys.argv[1])
rows = [
    json.loads(x)
    for x in (run / "results.jsonl").read_text().splitlines()
    if x.strip()
]

def med(xs):
    xs = [float(x) for x in xs if isinstance(x, (int, float))]
    return statistics.median(xs) if xs else None

scenarios = sorted({r["scenario"] for r in rows})
summary = {}

for s in scenarios:
    rs = [r for r in rows if r["scenario"] == s]

    summary[s] = {
        "samples": len(rs),
        "passes": sum(1 for r in rs if r.get("success")),
        "pass_rate_pct": (
            100.0 * sum(1 for r in rs if r.get("success")) / len(rs)
            if rs else None
        ),
        "median_raw_prompt_tokens": med([r.get("raw_prompt_tokens") for r in rs]),
        "median_server_prompt_tokens": med([r.get("server_prompt_tokens") for r in rs]),
        "median_ttft_ms": med([r.get("ttft_ms") for r in rs]),
        "median_total_ms": med([r.get("total_ms") for r in rs]),
        "median_prompt_tps": med([r.get("prompt_tps") for r in rs]),
        "median_gen_tps": med([r.get("gen_tps") for r in rs]),
        "median_reasoning_length": med([r.get("reasoning_length") for r in rs]),
        "finish_reasons": [r.get("finish_reason") for r in rs],
    }

(run / "summary.json").write_text(json.dumps(summary, indent=2) + "\n")

def f(v, n=2):
    return "-" if v is None else f"{v:.{n}f}"

md = [
    "# Ornith P2 — Long-context Retrieval",
    "",
    f"Run: `{run}`",
    "",
    "PASS requires all three exact needles in FINAL `content`, not merely in `reasoning_content`.",
    "",
    "| Scenario | n | passes | pass rate | raw prompt med | server prompt med | TTFT med ms | total med s | prompt t/s med | gen t/s med |",
    "|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|",
]

for s in scenarios:
    x = summary[s]
    md.append(
        f"| {s} | {x['samples']} | {x['passes']} | {f(x['pass_rate_pct'])}% "
        f"| {f(x['median_raw_prompt_tokens'],0)} | {f(x['median_server_prompt_tokens'],0)} "
        f"| {f(x['median_ttft_ms'])} | {f((x['median_total_ms']/1000) if x['median_total_ms'] else None)} "
        f"| {f(x['median_prompt_tps'])} | {f(x['median_gen_tps'])} |"
    )

md += [
    "",
    "## Profiles",
    "",
    "- `P2-NATIVE-262K`: 262144 context, native model RoPE.",
    "- `P2-YARN-512K`: 524288 context, YaRN, `--rope-scale 4`, `--yarn-orig-ctx 262144`.",
    "- Q4/Q4 KV, FlashAttention ON, DFlash OFF, vision OFF.",
    "- Output hard cap 4096.",
    "- Every round starts from a fresh llama-server (cold cache).",
    "",
    "## Baseline restored",
    "",
    "- 3 × 256K",
    "- KV Q4/Q4",
    "- FlashAttention ON",
    "- DFlash OFF",
    "- cache-prompt ON",
    "- hard output 4096",
    "",
]

report = "\n".join(md) + "\n"
(run / "report.md").write_text(report)
print(report)
PY
}

# ----------------------------------------------------------------------------
# Manifest
# ----------------------------------------------------------------------------

make_manifest() {
    local image_id model_sha
    image_id="$(docker image inspect "$IMAGE" --format '{{.Id}}')"
    model_sha="$(sha256sum "$MODEL_DIR/$MODEL_FILE" | awk '{print $1}')"

    cat > "$RUN/manifest.json" <<EOF
{
  "created_utc": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "suite": "Ornith P2 long-context retrieval",
  "image": "$IMAGE",
  "image_id": "$image_id",
  "model_file": "$MODEL_FILE",
  "model_sha256": "$model_sha",
  "kv_k": "$KV_K",
  "kv_v": "$KV_V",
  "flash_attention": true,
  "dflash": false,
  "vision": false,
  "output_limit": $OUTPUT_LIMIT,
  "rounds": $ROUNDS,
  "native": {
    "ctx": 262144,
    "target_raw_prompt_tokens": $TARGET_NATIVE
  },
  "yarn": {
    "ctx": 524288,
    "target_raw_prompt_tokens": $TARGET_YARN,
    "rope_scale": 4,
    "yarn_orig_ctx": 262144
  }
}
EOF
}

# ----------------------------------------------------------------------------
# Trap
# ----------------------------------------------------------------------------

cleanup_runtime() {
    stop_monitor || true
}

on_exit() {
    local rc=$?
    trap - EXIT INT TERM

    cleanup_runtime || true

    if [[ "$RESTORE_ON_EXIT" == "1" ]]; then
        restore_baseline || true
    fi

    exit "$rc"
}

# ----------------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------------

run_all() {
    preflight_tools
    clean_old_clients

    RUN="$P2ROOT/p2-$(utc_stamp)"
    mkdir -p "$RUN/cases"

    echo "$RUN" > "$ROOT/LAST_P2"

    printf \
"scenario\tround\traw_prompt\tserver_prompt\tprocessed\tcached\tdecoded\tttft_ms\ttotal_ms\tprompt_tps\tgen_tps\tfinish\tcontent_len\treasoning_len\thits\tresult\n" \
      > "$RUN/summary.tsv"

    : > "$RUN/results.jsonl"

    make_manifest
    write_helpers
    setup_tmux
    start_monitor

    RESTORE_ON_EXIT=1
    trap on_exit EXIT INT TERM

    local r

    for r in $(seq 1 "$ROUNDS"); do
        run_round "P2-NATIVE-262K" native "$TARGET_NATIVE" "$r"
    done

    for r in $(seq 1 "$ROUNDS"); do
        run_round "P2-YARN-512K" yarn "$TARGET_YARN" "$r"
    done

    update_progress "ANALYSIS" "$ROUNDS" "done"

    make_report | tee "$RUN/report-console.txt"

    docker logs ornith > "$RUN/llama-last.log" 2>&1 || true

    RESTORE_ON_EXIT=0
    cleanup_runtime
    restore_baseline

    echo
    echo "============================================================================"
    echo "P2 TERMINADA — BASELINE RESTAURADO"
    echo "============================================================================"
    echo "Run:      $RUN"
    echo "TSV:      $RUN/summary.tsv"
    echo "JSON:     $RUN/summary.json"
    echo "Reporte:  $RUN/report.md"
}

results_view() {
    [[ -f "$ROOT/LAST_P2" ]] || die "No existe $ROOT/LAST_P2"

    local d
    d="$(cat "$ROOT/LAST_P2")"

    echo "$d"
    echo

    if [[ -f "$d/report.md" ]]; then
        cat "$d/report.md"
    else
        echo "P2 aún no termina."
        echo
        tail -n 10 "$d/summary.tsv" | column -t -s $'\t'
    fi
}

case "${1:-run}" in
    run)
        run_all
        ;;
    dashboard)
        dashboard
        ;;
    slots)
        slots_view
        ;;
    watch)
        watch -n 1 "$SELF slots"
        ;;
    results)
        results_view
        ;;
    restore)
        restore_baseline
        ;;
    *)
        echo "Uso:"
        echo "  $SELF run"
        echo "  $SELF dashboard"
        echo "  $SELF slots"
        echo "  $SELF watch"
        echo "  $SELF results"
        echo "  $SELF restore"
        exit 1
        ;;
esac
