"""Explicit tar byte stream, without xattrs or AppleDouble metadata."""
from pathlib import Path
import json, tarfile, io, hashlib
HERE=Path(__file__).resolve().parent
RUN=Path('/tmp/cdx-ai-core-direct002-20261011')
for src in (HERE/'tools').iterdir():
    if src.is_file():(RUN/src.name).write_bytes(src.read_bytes())
for lane in ['B','C']:
    target=Path(f'/tmp/cdx-ai-core-direct002-{lane}.tar')
    entries={}
    for p in RUN.iterdir():
        if p.is_file(): entries[p.name]=p
    for p in (RUN/'source').rglob('*'):
        if p.is_file():entries[p.relative_to(RUN).as_posix()]=p
    for p in (RUN/f'seed-{lane}').iterdir():
        if p.is_file(): entries['private/'+p.name]=p
    directories={'cdx-ai-core-direct002-20261011','cdx-ai-core-direct002-20261011/private'}
    for n in entries:
        pp=Path('cdx-ai-core-direct002-20261011')/n
        directories.update(str(parent) for parent in pp.parents if str(parent)!='.')
    with tarfile.open(target,'w',format=tarfile.USTAR_FORMAT) as tar:
        for n in sorted(directories):
            info=tarfile.TarInfo(n);info.type=tarfile.DIRTYPE;info.mode=0o700;tar.addfile(info)
        for n,p in sorted(entries.items()):
            raw=p.read_bytes();info=tarfile.TarInfo('cdx-ai-core-direct002-20261011/'+n);info.size=len(raw);info.mode=0o600;tar.addfile(info,io.BytesIO(raw))
    target.chmod(0o600)
    print(json.dumps({'lane':lane,'entries':len(entries),'bytes':target.stat().st_size,'sha256':hashlib.sha256(target.read_bytes()).hexdigest(),'xattrs':0,'AppleDouble':0}))
