#!/usr/bin/env python3
"""Patch Bosun GGUF: mark decision tokens as USER_DEFINED (type 4).

The official GGUF ships <|decision_000|>..<|decision_255|> (vocab ids
151669..151924) with token_type=3 (CONTROL). llama.cpp-based runtimes then
detokenize them as empty text and hide their logprobs, which breaks the
decision readout. This script flips those 256 int32 entries to 4
(USER_DEFINED) — metadata only, weights untouched.

Usage:
    python3 patch_gguf.py bosun-v3.1-0.6b-Q8_0.gguf [output.gguf]

Without output the file is patched in place. Idempotent.
"""

import shutil
import struct
import sys

TOKEN_TYPE_KEY = "tokenizer.ggml.token_type"
DECISION_FIRST, DECISION_COUNT = 151669, 256
CONTROL, USER_DEFINED = 3, 4

TYPE_SIZES = {0: 1, 1: 1, 2: 2, 3: 2, 4: 4, 5: 4, 6: 4, 7: 1, 10: 8, 11: 8, 12: 8}


def main() -> int:
    if len(sys.argv) < 2:
        print(__doc__)
        return 2
    src = sys.argv[1]
    dst = sys.argv[2] if len(sys.argv) > 2 else src
    inplace = dst == src
    if inplace:
        dst = src + ".patched"

    with open(src, "rb") as f:
        if f.read(4) != b"GGUF":
            print("not a GGUF file:", src)
            return 1
        version = struct.unpack("<I", f.read(4))[0]
        if version < 2:
            print("unsupported GGUF version:", version)
            return 1
        f.read(8 + 8)  # tensor_count, kv_count
        payload_off = None
        count = 0
        while True:
            key_len = struct.unpack("<Q", f.read(8))[0]
            key = f.read(key_len).decode()
            kv_type = struct.unpack("<I", f.read(4))[0]
            if kv_type == 9:  # ARRAY
                elem_type = struct.unpack("<I", f.read(4))[0]
                n = struct.unpack("<Q", f.read(8))[0]
                if key == TOKEN_TYPE_KEY:
                    if elem_type != 5:
                        print("token_type elem type %d, expected INT32(5)" % elem_type)
                        return 1
                    payload_off, count = f.tell(), n
                    f.seek(n * 4, 1)
                    break
                if elem_type in TYPE_SIZES:
                    f.seek(n * TYPE_SIZES[elem_type], 1)
                else:  # array of strings
                    for _ in range(n):
                        slen = struct.unpack("<Q", f.read(8))[0]
                        f.seek(slen, 1)
                continue
            if kv_type == 8:  # STRING
                slen = struct.unpack("<Q", f.read(8))[0]
                f.seek(slen, 1)
                continue
            size = TYPE_SIZES.get(kv_type)
            if size is None:
                print("unknown kv type", kv_type, "at key", key)
                return 1
            f.seek(size, 1)

    if payload_off is None:
        print("key not found:", TOKEN_TYPE_KEY)
        return 1
    if count < DECISION_FIRST + DECISION_COUNT:
        print("vocab too small:", count)
        return 1

    work = dst
    if inplace:
        work = src + ".patched"
    shutil.copyfile(src, work)
    with open(work, "r+b") as f:
        f.seek(payload_off + 4 * DECISION_FIRST)
        current = struct.unpack("<%di" % DECISION_COUNT, f.read(4 * DECISION_COUNT))
        changed = sum(1 for v in current if v != USER_DEFINED)
        if changed == 0:
            print("already patched, nothing to do")
        else:
            f.seek(payload_off + 4 * DECISION_FIRST)
            f.write(struct.pack("<%di" % DECISION_COUNT, *([USER_DEFINED] * DECISION_COUNT)))
        f.seek(payload_off + 4 * DECISION_FIRST)
        verify = struct.unpack("<%di" % DECISION_COUNT, f.read(4 * DECISION_COUNT))
        assert all(v == USER_DEFINED for v in verify)
    if inplace:
        shutil.move(work, src)
        dst = src
    print("patched %d decision tokens -> %s" % (changed, dst))
    return 0


if __name__ == "__main__":
    sys.exit(main())
