"""Decode-path fidelity between two LIVE Flint engines with the admin console's own scorer
(arbi_serve.admin_console.decode_fidelity: arm A's continuation teacher-forced through decode steps on
both arms, served sampler, null and seam controls), for an A/B the console's build axis cannot stage:
two builds of a TP2 engine that no committed compose recipe boots.
usage: flint_decode_fid.py URL_A URL_B OUT.json [prompts repeats depth bands]"""
import asyncio, json, sys
from types import SimpleNamespace
from arbi_serve.admin_console import decode_fidelity as df
from arbi_serve.admin_console import decode_fidelity_prompts as dp
from arbi_serve.admin_console import targets
from arbi_serve.admin_console.ab_probe import CANONICAL_SEED, canonical_prompt
from arbi_serve.admin_console.sweep import AB_PROMPT_SHAPE, frame
url_a, url_b, out = sys.argv[1:4]
n_prompts, repeats, depth = (int(v) for v in (sys.argv[4:7] or (8, 2, 1024)))
bands = [int(v) for v in (sys.argv[7] if len(sys.argv) > 7 else "1,4").split(",")]
A = SimpleNamespace(base_url=url_a, token=None, model="Flint")
B = SimpleNamespace(base_url=url_b, token=None, model="Flint")
sampler = {"temperature": 1.0, "top_p": 0.95, "top_k": 20}
def emit(_f): print(".", end="", flush=True)
async def main():
    async def count(text): return await targets._count_prompt_tokens(A.base_url, None, A.model, text)
    first = await canonical_prompt(count, 0, seed=CANONICAL_SEED, target_tokens=depth, kind_idx=AB_PROMPT_SHAPE[0], lead_override=AB_PROMPT_SHAPE[1])
    spec = dp.parse_score_cells(n_prompts, repeats, "chat")
    cells, manifest = await dp.score_cells(A, {(depth, 1): [first]}, spec, seed=CANONICAL_SEED, shape=AB_PROMPT_SHAPE)
    by_cell = df.cell_prompts(cells)
    ref = await df.capture_reference(A, by_cell, bands, sampler, emit, frame)
    arm = await df.capture_arm(B, by_cell, ref, bands, sampler, emit, frame)
    fold = df.fold_decode(ref, arm, bands, sampler=sampler)
    json.dump({"A": url_a, "B": url_b, "spec": spec, "depth": depth, "bands": bands, "manifest": manifest, "fidelity_decode": fold}, open(out, "w"), indent=1, default=str)
    print(); print(json.dumps(fold, default=str)[:6000])
asyncio.run(main())
