#!/usr/bin/env python3
"""Transplant a trained writer bundle into a copy of the K512 GGUF.
`transplant_gguf_tensors` is whitelist-only and byte-level, so this is the whole job: load the bundle,
hand it over with the writers scope, and report what changed.
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from phase3_lora_and_transplant import load_writers_bin, transplant_gguf_tensors # noqa: E402
BASE = Path("models/K512_iq2_xs/K512-iq2_xs-metal.gguf")
BUNDLE = Path("persona/checkpoints/agent_persona_lora_v1/agent_persona_writers.bin")
OUT = Path("models/K512_iq2_xs/K512-iq2_xs-metal-persona-q8_0.gguf")
if __name__ == "__main__":
import sys as _sys
bundle = Path(_sys.argv[1]) if len(_sys.argv) > 1 else BUNDLE
out = Path(_sys.argv[2]) if len(_sys.argv) > 2 else OUT
replacements = load_writers_bin(str(bundle))
print(f"bundle: {len(replacements)} writers from {bundle}", flush=True)
result = transplant_gguf_tensors(str(BASE), str(out), replacements,
scope="swift_writers", allow_type_change=True)
print(f"transplant: {result}", flush=True)
print(f"wrote {out} ({out.stat().st_size:,} bytes)", flush=True)