#!/bin/bash
# Create local derived tags that pin num_ctx, then prove each pin the way the bench does:
# /api/show must list the PARAMETER, and a load with NO num_ctx must report that window on /api/ps.
#
#   ./create-cg-tags.sh            create (or re-create) every tag whose parent is installed
#   ./create-cg-tags.sh --check    verify only, create nothing
#
# Local only: pulls nothing, pushes nothing, edits no harness config, and never touches the parents.
# Re-running replaces a derived tag with the same content (ollama create is idempotent for these).
# OLLAMA_CONTEXT_LENGTH is irrelevant here: a Modelfile num_ctx wins over it (docs/MODELS.md).
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
host=${OLLAMA_HOST:-127.0.0.1:11434}
case "$host" in http*) api="$host" ;; *) api="http://$host" ;; esac
check_only=0
[ "${1:-}" = "--check" ] && check_only=1

# parent | derived tag | Modelfile | expected window
TAGS=(
  "hf.co/unsloth/gemma-4-26B-A4B-it-GGUF:UD-Q4_K_XL|gemma4-26b-a4b:ud-q4_k_xl-cg|gemma4-26b-a4b-ud-q4_k_xl-cg.Modelfile|32768"
  "hf.co/unsloth/gemma-4-26B-A4B-it-GGUF:Q8_0|gemma4-26b-a4b:q8_0-cg|gemma4-26b-a4b-q8_0-cg.Modelfile|32768"
  "hf.co/unsloth/Qwen3.5-9B-GGUF:Q6_K|qwen3.5-9b:q6_k-cg|qwen3.5-9b-q6_k-cg.Modelfile|32768"
  "hf.co/unsloth/Qwen3.5-9B-GGUF:Q8_0|qwen3.5-9b:q8_0-cg|qwen3.5-9b-q8_0-cg.Modelfile|32768"
  "hf.co/Jackrong/Qwen3.5-9B-DeepSeek-V4-Flash-GGUF:Q8_0|qwen3.5-9b-dsv4-flash:q8_0-cg|qwen3.5-9b-dsv4-flash-q8_0-cg.Modelfile|32768"
  "huggingface.co/orcarouter/OrcaSAQ-2-Cyber-27B-Uncensored-GGUF:latest|orcasaq2-cyber-27b:cg|orcasaq2-cyber-27b-cg.Modelfile|32768"
  "hf.co/jialinyyzz/humanizer:Q8_0|humanizer:q8_0-cg|humanizer-q8_0-cg.Modelfile|8192"
)

command -v ollama >/dev/null || { echo "ollama CLI not found" >&2; exit 3; }
curl -fsS --noproxy '*' -m 5 "$api/api/version" >/dev/null || { echo "Ollama not reachable at $api" >&2; exit 3; }

post() { curl -fsS --noproxy '*' -m 1800 "$api$1" -H 'Content-Type: application/json' -d "$2"; }
unload() { post /api/generate "{\"model\":\"$1\",\"prompt\":\"\",\"stream\":false,\"keep_alive\":0}" >/dev/null 2>&1 || true; }

ok=0; bad=0; skipped=0
for row in "${TAGS[@]}"; do
  IFS='|' read -r parent child file want <<<"$row"
  if ! ollama show "$parent" >/dev/null 2>&1; then
    echo "skip   $child  (parent not installed: $parent)"; skipped=$((skipped + 1)); continue
  fi
  if [ "$check_only" = 0 ]; then
    ollama create "$child" -f "$here/modelfiles/$file" >/dev/null
  fi
  pinned=$(ollama show "$child" --parameters 2>/dev/null | awk '$1 == "num_ctx" {print $2; exit}')
  unload "$child"
  # The contract load: no num_ctx in the request.
  post /api/generate "{\"model\":\"$child\",\"prompt\":\"hi\",\"stream\":false,\"options\":{\"num_predict\":1}}" >/dev/null
  loaded=$(curl -fsS --noproxy '*' "$api/api/ps" | python3 -I -c '
import json, sys
name = sys.argv[1]
for m in json.load(sys.stdin).get("models") or []:
    if name in (m.get("name"), m.get("model")):
        print(m.get("context_length", "")); break
' "$child")
  unload "$child"
  if [ "$pinned" = "$want" ] && [ "$loaded" = "$want" ]; then
    echo "PASS   $child  pin=$pinned loaded=$loaded"; ok=$((ok + 1))
  else
    echo "FAIL   $child  pin=${pinned:-none} loaded=${loaded:-?} want=$want"; bad=$((bad + 1))
  fi
done
echo "pinned tags: $ok pass, $bad fail, $skipped skipped (parent missing)"
[ "$bad" = 0 ]
