#!/usr/bin/env python3
"""Lossless tensor-container rewrite: omit vision, retain MTP separately.
No model code, GPU, dequantization, or tensor arithmetic is involved.
"""
import argparse, hashlib, json, os, shutil, struct
from pathlib import Path

def read_header(path):
    with path.open('rb') as f:
        n=struct.unpack('<Q',f.read(8))[0]
        return json.loads(f.read(n)),8+n

def transfer(items, dest):
    header={'__metadata__':{'format':'pt'}};offset=0
    for name,path,start,meta in items:
        size=meta['data_offsets'][1]-meta['data_offsets'][0]
        header[name]={'dtype':meta['dtype'],'shape':meta['shape'],'data_offsets':[offset,offset+size]};offset+=size
    encoded=json.dumps(header,separators=(',',':')).encode();encoded+=b' '*((-len(encoded))%8)
    hashes={}
    with dest.open('xb') as output:
        output.write(struct.pack('<Q',len(encoded)));output.write(encoded)
        for name,path,start,meta in items:
            h=hashlib.sha256();size=meta['data_offsets'][1]-meta['data_offsets'][0]
            with path.open('rb') as source:
                source.seek(start+meta['data_offsets'][0]);remaining=size
                while remaining:
                    block=source.read(min(8*1024*1024,remaining))
                    if not block:raise IOError('truncated source')
                    h.update(block);output.write(block);remaining-=len(block)
            hashes[name]=h.hexdigest()
    # Independent readback of every retained tensor.
    verify,base=read_header(dest)
    with dest.open('rb') as f:
        for name,expected in hashes.items():
            a,b=verify[name]['data_offsets'];f.seek(base+a);left=b-a;h=hashlib.sha256()
            while left:
                block=f.read(min(8*1024*1024,left));assert block;h.update(block);left-=len(block)
            assert h.hexdigest()==expected,name
    return hashes,offset

def main():
    p=argparse.ArgumentParser();p.add_argument('source');p.add_argument('destination');args=p.parse_args()
    src=Path(args.source).resolve();dest=Path(args.destination).resolve();assert src!=dest
    assert not dest.exists(),f'Refusing to overwrite {dest}'
    stage=dest.with_name(dest.name+'.building');stage.mkdir(parents=True,exist_ok=False)
    manifest={'source':str(src),'kept_sha256':{},'omitted_vision':{},'mtp_tensor_count':0};index={};total=0;mtp=[]
    for path in sorted(src.glob('*.safetensors')):
        header,start=read_header(path);keep=[]
        for name,meta in header.items():
            if name=='__metadata__':continue
            if name.startswith('model.visual.') or name.startswith('visual.'):
                manifest['omitted_vision'][name]=meta['data_offsets'][1]-meta['data_offsets'][0];continue
            item=(name,path,start,meta)
            if name.startswith('mtp.'):mtp.append(item)
            else:keep.append(item)
        if keep:
            hashes,size=transfer(keep,stage/path.name);total+=size;manifest['kept_sha256'].update(hashes)
            index.update({name:path.name for name in hashes})
    if mtp:
        hashes,size=transfer(mtp,stage/'mtp.safetensors');total+=size;manifest['kept_sha256'].update(hashes)
        index.update({name:'mtp.safetensors' for name in hashes});manifest['mtp_tensor_count']=len(mtp)
    assert manifest['omitted_vision'],'No vision tensors detected; refusing to mislabel model'
    for path in src.iterdir():
        if path.is_file() and path.suffix in ('.json','.jinja','.txt','.md') and path.name not in ('config.json','model.safetensors.index.json'):
            shutil.copy2(path,stage/path.name)
    config=json.loads((src/'config.json').read_text());config['language_model_only']=True
    (stage/'config.json').write_text(json.dumps(config,indent=2)+'\n')
    (stage/'model.safetensors.index.json').write_text(json.dumps({'metadata':{'total_size':total},'weight_map':index},indent=2)+'\n')
    manifest.update({'retained_bytes':total,'omitted_vision_bytes':sum(manifest['omitted_vision'].values()),'all_retained_tensors_verified':True,'vision_restore_source':str(src)})
    (stage/'component-manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
    os.replace(stage,dest)
    print(json.dumps({'destination':str(dest),'kept':len(index),'mtp':len(mtp),'omitted_vision_bytes':manifest['omitted_vision_bytes'],'retained_bytes':total,'verified':True}),flush=True)
if __name__=='__main__':main()
