#!/usr/bin/env python3
"""Issue 4295 long-context qualification client (stdlib only, CPU, loopback).

Modes:
  full  : one request filling the engine's advertised max_model_len (prompt + max_tokens == cap).
  conc8 : eight independently salted ~200k-token requests submitted at the same instant.

Serving config: thinking ON, Qwen3.8 recommended thinking-mode sampling (temperature 1.0, top_p 0.95,
top_k 20, min_p 0, presence 0, repetition 1.0), no seed. Every prompt starts with a unique random salt
so no prefix (GPU or host tier) can be reused; usage.cached_tokens must be 0.
"""

import datetime
import hashlib
import json
import secrets
import sys
import threading
import time
import urllib.request

BASE = sys.argv[2] if len(sys.argv) > 2 else "http://127.0.0.1:18741"
TOKEN = open("/home/arbi-admin/ctx4295/token").read().strip()  # throwaway token of this private boot
HDR = {"Content-Type": "application/json", "Authorization": "Bearer " + TOKEN}
SAMPLING = dict(temperature=1.0, top_p=0.95, top_k=20, min_p=0.0, presence_penalty=0.0, repetition_penalty=1.0)
WORDS = ["amber", "silver", "violet", "copper", "emerald", "cobalt", "scarlet", "ivory", "jade", "onyx",
         "saffron", "teal", "crimson", "golden", "indigo", "pearl", "russet", "slate", "topaz", "umber"]
NOUNS = ["lantern", "orchard", "compass", "anchor", "meadow", "bridge", "harbor", "falcon", "glacier", "quarry",
         "violin", "beacon", "canyon", "dynamo", "ember", "fjord", "garnet", "heron", "island", "juniper"]


def now():
    return datetime.datetime.now(datetime.timezone.utc).isoformat()


def post(path, body, timeout=120):
    r = urllib.request.Request(BASE + path, data=json.dumps(body).encode(),
                               headers=HDR)
    return urllib.request.urlopen(r, timeout=timeout)


class Fixture:
    def __init__(self, label):
        self.salt = secrets.token_hex(16)
        rng = hashlib.sha256(self.salt.encode()).digest()
        self.values = [f"{WORDS[(rng[2 * i]) % 20]} {NOUNS[(rng[2 * i + 1] + 7 * i) % 20]}" for i in range(3)]
        self.label = label

    def prompt(self, n):
        rows = []
        for i in range(n):
            d = hashlib.sha256(f"{self.salt}-{i}".encode()).hexdigest()[:16]
            rows.append(f"Record {i:06d}: reference {d}; cargo count {(i * 7919) % 100003}; destination ordinary; status reviewed.\n")
        for frac, key, value in zip((0.05, 0.5, 0.95), ("FIRST", "MIDDLE", "LAST"), self.values):
            rows.insert(int(n * frac), f"IMPORTANT QUALIFICATION ENTRY: {key} secret value is {value}.\n")
        return (f"Session salt {self.salt}. Fixture 4295-{self.label}. Read this synthetic ledger. "
                "Remember the FIRST, MIDDLE and LAST secret values.\n" + "".join(rows) +
                "\nReply with only the FIRST, MIDDLE and LAST secret values, in that order, separated by semicolons.")

    def body(self, n):
        return {"model": "Qwen3.8-27B", "messages": [{"role": "user", "content": self.prompt(n)}],
                "chat_template_kwargs": {"enable_thinking": True}}

    def fit(self, target):
        lo, hi = 0, 12000
        while lo < hi:
            mid = (lo + hi) // 2
            with post("/tokenize", self.body(mid)) as f:
                c = json.load(f)["count"]
            if c < target:
                lo = mid + 1
            else:
                hi = mid
        with post("/tokenize", self.body(lo)) as f:
            d = json.load(f)
        self.records, self.count, self.cap = lo, d["count"], d["max_model_len"]
        return self


def run(fx, max_tokens, rec, t_submit=None):
    req = fx.body(fx.records)
    rec.update(label=fx.label, salt=fx.salt, expected=fx.values, records=fx.records, prompt_tokens_tokenize=fx.count,
               max_model_len=fx.cap, max_tokens=max_tokens, sampling=SAMPLING, thinking=True,
               prompt_sha256=hashlib.sha256(req["messages"][0]["content"].encode()).hexdigest())
    req.update(max_tokens=max_tokens, stream=True, stream_options={"include_usage": True}, **SAMPLING)
    if t_submit is not None:
        while time.monotonic() < t_submit:
            time.sleep(0.001)
    t0 = time.monotonic()
    rec["submitted_at"] = now()
    content, reasoning, usage, first_tok, first_content, finish = [], [], None, None, None, None
    try:
        with post("/v1/chat/completions", req, timeout=10800) as f:
            rec["http_status"] = f.status
            for raw in f:
                if not raw.startswith(b"data:"):
                    continue
                data = raw[5:].strip()
                if data == b"[DONE]":
                    break
                d = json.loads(data)
                if d.get("usage"):
                    usage = d["usage"]
                for c in d.get("choices", []):
                    delta = c.get("delta", {})
                    r = delta.get("reasoning_content") or delta.get("reasoning") or ""
                    t = delta.get("content") or ""
                    if (r or t) and first_tok is None:
                        first_tok = time.monotonic() - t0
                    if t and first_content is None:
                        first_content = time.monotonic() - t0
                    reasoning.append(r)
                    content.append(t)
                    if c.get("finish_reason"):
                        finish = c["finish_reason"]
    except Exception as e:  # recorded, not retried
        rec["error"] = f"{type(e).__name__}: {e}"
        if hasattr(e, "read"):
            rec["error_body"] = e.read()[:2000].decode("utf-8", "replace")
    end = time.monotonic() - t0
    answer = "".join(content)
    pos = [answer.lower().find(v) for v in fx.values]
    comp = (usage or {}).get("completion_tokens") or 0
    rec.update(completed_at=now(), elapsed_s=round(end, 3), ttft_s=first_tok and round(first_tok, 3),
               first_content_s=first_content and round(first_content, 3), finish_reason=finish, usage=usage,
               answer=answer, reasoning_chars=len("".join(reasoning)),
               reasoning_tail="".join(reasoning)[-400:],
               cached_tokens=((usage or {}).get("prompt_tokens_details") or {}).get("cached_tokens"),
               recall_pass=all(p >= 0 for p in pos) and pos == sorted(pos),
               prefill_tok_per_s=first_tok and usage and round(usage["prompt_tokens"] / first_tok, 1),
               decode_tok_per_s=first_tok and comp and end > first_tok and round(comp / (end - first_tok), 2))
    print(json.dumps({k: rec[k] for k in ("label", "elapsed_s", "ttft_s", "recall_pass", "cached_tokens", "answer",
                                         "finish_reason")}), flush=True)
    return rec


def main():
    mode = sys.argv[1]
    out = {"mode": mode, "base": BASE, "started_at": now()}
    with urllib.request.urlopen(urllib.request.Request(BASE + "/v1/models", headers=HDR), timeout=30) as f:
        out["models"] = json.load(f)
    if mode == "full":
        probe = Fixture("full").fit(10)
        cap = probe.cap
        reserve = 3072
        fx = Fixture("full").fit(cap - reserve)
        assert fx.count + 1 <= cap, (fx.count, cap)
        rec = {}
        run(fx, cap - fx.count - 8, rec)  # engine reserves mtp_k+1=8 speculative lookahead tokens
        out["requests"] = [rec]
    elif mode == "conc8":
        fxs = [Fixture(f"c8-{i}").fit(200000) for i in range(8)]
        assert all(f.count >= 200000 for f in fxs)
        recs = [dict() for _ in fxs]
        t_submit = time.monotonic() + 2.0
        wall0 = time.monotonic()
        ths = [threading.Thread(target=run, args=(f, 3072, r, t_submit)) for f, r in zip(fxs, recs)]
        for t in ths:
            t.start()
        for t in ths:
            t.join()
        wall = time.monotonic() - wall0 - 2.0
        out["requests"] = recs
        tot_p = sum((r.get("usage") or {}).get("prompt_tokens", 0) for r in recs)
        tot_c = sum((r.get("usage") or {}).get("completion_tokens", 0) for r in recs)
        out["aggregate"] = dict(wall_s=round(wall, 3), prompt_tokens=tot_p, completion_tokens=tot_c,
                                tokens_per_s=round((tot_p + tot_c) / wall, 1),
                                all_recall=all(r.get("recall_pass") for r in recs),
                                all_uncached=all(r.get("cached_tokens") == 0 for r in recs),
                                distinct_salts=len({r["salt"] for r in recs}))
    out["completed_at"] = now()
    json.dump(out, open(sys.argv[3] if len(sys.argv) > 3 else f"/tmp/ctx4295-{mode}.json", "w"), indent=2)
    print(json.dumps(out.get("aggregate", {})), flush=True)


if __name__ == "__main__":
    main()
