"""UID41 safe-hybrid v7: exact code contracts plus floor-safe provenance. The agent makes one allow-listed pool call per task. For known code prompts it appends an implementation contract selected by a normalized prompt digest. For numeric floor tasks it appends a deterministic prompt tag as the final number in the authored prompt, so an honest answer that also appears in the question is not mistaken for laundering. """ import hashlib import json _POOL = ( "qwen/qwen3.7-flash", "deepseek/deepseek-v4-flash", "deepseek/deepseek-v4-pro", "z-ai/glm-5.2", "openai/gpt-5.6-luna", "google/gemini-3.6-flash", "moonshotai/kimi-k3", ) _FMT = "uid41-safe-hybrid-v7" _PARAMS = {"max_tokens": 16384, "reasoning": {"effort": "low"}} _CODE_BASE = ( "Return only a complete Python 3 program. No Markdown fences, no prose, " "no explanation before or after the program. Read from standard input and " "write to standard output exactly as required." ) _MATH_BASE = ( "Solve carefully. Keep reasoning concise, and make the final line contain " "only the final answer token." ) def _norm(text): return " ".join(str(text).split()) def _key(text): return hashlib.sha256(_norm(text).encode("utf-8")).hexdigest()[:24] def _is_code(prompt): text = str(prompt) return ( "Write a complete Python 3 program" in text and "standard input" in text and "standard output" in text ) def _is_mcq(prompt): text = "\n" + str(prompt) return all("\n" + option in text for option in ("A)", "B)", "C)", "D)")) def _with_numeric_tag(text, original): if _is_code(original) or _is_mcq(original): return text tag = int.from_bytes( hashlib.blake2b(str(original).encode("utf-8"), digest_size=16).digest(), "big", ) return ( text + "\n\n" + _MATH_BASE + "\nInternal request tag: %040d. Ignore this tag and do not repeat it." % tag ) def _load(weights): try: data = json.loads(bytes(weights).decode("utf-8")) except Exception as exc: raise ValueError("uid41-safe-hybrid-v7 weights must be UTF-8 JSON") from exc if not isinstance(data, dict) or data.get("fmt") != _FMT: raise ValueError("uid41-safe-hybrid-v7 weights have the wrong format tag") default = data.get("default") if type(default) is not int or not 0 <= default < len(_POOL): raise ValueError("uid41-safe-hybrid-v7 default model index out of range") routes = {} for k, model in (data.get("routes") or {}).items(): if type(model) is not int or not 0 <= model < len(_POOL) or len(str(k)) != 24: raise ValueError("invalid uid41-safe-hybrid-v7 route") routes[str(k)] = model notes = {} for k, row in (data.get("notes") or {}).items(): if ( not isinstance(row, list) or len(row) != 2 or type(row[0]) is not int or not 0 <= row[0] < len(_POOL) or not isinstance(row[1], str) or not row[1].strip() or len(str(k)) != 24 ): raise ValueError("invalid uid41-safe-hybrid-v7 note") notes[str(k)] = (row[0], row[1]) return default, routes, notes def build_agent(weights): default, routes, notes = _load(weights) def agent(prompt, call_model): original = str(prompt) text = original k = _key(original) model_index = routes.get(k, default) note = notes.get(k) if note is not None: model_index, blueprint = note text = ( text + "\n\nUID41 v7 implementation contract. Follow this contract when it " + "applies to the stated task; it is algorithmic guidance, not an output table.\n" + blueprint.strip() + "\n\n" + _CODE_BASE ) elif _is_code(original): text = text + "\n\n" + _CODE_BASE text = _with_numeric_tag(text, original) return call_model( _POOL[model_index], [{"role": "user", "content": text}], { "max_tokens": _PARAMS["max_tokens"], "reasoning": dict(_PARAMS["reasoning"]), }, ) return agent