"""Portable source, Harbor tasks, four-slot evidence, and independent restoration.""" import argparse import json from pathlib import Path import shutil import tarfile import tempfile from .evaluate import review_evaluation from .pool import verify_pool, build, sha from .model import save ROOT = Path(__file__).resolve().parent.parent IGNORE = shutil.ignore_patterns('__pycache__', '*.pyc', '.pytest_cache', '*.egg-info') def verify_release(root, rebuild=False): root = Path(root).resolve() hashes = json.loads((root/'MANIFEST.json').read_text())['files_sha256'] actual = {str(p.relative_to(root)) for p in root.rglob('*') if p.is_file() and '__pycache__' not in p.parts and p.name != 'MANIFEST.json'} if actual != set(hashes): raise ValueError('Release membership mismatch') for name, expected in hashes.items(): path = root/name if path.is_symlink() or not path.resolve().is_relative_to(root) or sha(path) != expected: raise ValueError('Release hash mismatch: ' + name) summary, _ = review_evaluation(root/'tasks', root/'evidence') if summary != json.loads((root/'evidence/summary.json').read_text()): raise ValueError('Release aggregate mismatch') if rebuild: with tempfile.TemporaryDirectory(prefix='agentgridworld-rebuild-') as tmp: rebuilt = build(root/'agentgridworld/scenarios', Path(tmp)/'tasks') if (rebuilt/'pool.json').read_bytes() != (root/'tasks/pool.json').read_bytes(): raise ValueError('Source regeneration differs') return {'files_verified':len(hashes), 'episodes_replayed':summary['episodes_verified'], 'request_chains_verified':summary['episodes_verified'], 'regenerated_identically':rebuild} def build_release(pool, evaluation, destination): pool = Path(pool).resolve(); evaluation = Path(evaluation).resolve() destination = Path(destination).resolve() archive = destination.with_name(destination.name+'.tar.gz') if destination.exists() or archive.exists(): raise FileExistsError('Release destinations are immutable') summary, _ = review_evaluation(pool, evaluation) if summary != json.loads((evaluation/'summary.json').read_text()): raise ValueError('Independent summary differs') destination.parent.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(prefix='.release-', dir=destination.parent) as temp: stage = Path(temp)/destination.name; stage.mkdir() for name in ('agentgridworld', 'tests', 'docs'): shutil.copytree(ROOT/name, stage/name, ignore=IGNORE) for name in ('README.md', 'LICENSE', 'NOTICE', 'pyproject.toml'): shutil.copyfile(ROOT/name, stage/name) shutil.copytree(pool, stage/'tasks', ignore=IGNORE) evidence = stage/'evidence'; evidence.mkdir() for name in ('protocol.json', 'summary.json'): shutil.copyfile(evaluation/name, evidence/name) jobs = evidence/'jobs'; jobs.mkdir() for slot in summary['protocol']['slots']: job = evaluation/'jobs'/slot['id']; target = jobs/slot['id']; target.mkdir() shutil.copyfile(job/'result.json', target/'result.json') for suffix in ('.pool-binding.json', '.model-access.json'): shutil.copyfile(job.parent/(job.name+suffix), jobs/(job.name+suffix)) for source in sorted(job.glob('*/verifier/episode.json')): trial = source.parent.parent; out = target/trial.name; out.mkdir() for folder in ('verifier','agent'): shutil.copytree(trial/folder, out/folder, ignore=shutil.ignore_patterns('*.log','*.txt','__pycache__')) raw = json.loads((trial/'result.json').read_text()) save(out/'result.json', {k:raw.get(k) for k in ('id','started_at','finished_at','exception_info','verifier_result')}) # Host launch plans/logs and credentials never enter the public package. for file in stage.rglob('*'): if file.is_file() and str(Path.home()).encode() in file.read_bytes(): raise ValueError('Host path in release: '+str(file.relative_to(stage))) save(stage/'release.json', {'schema':'agentgridworld.release.v1','version':'0.1.0', 'tasks':len(summary['tasks']), 'model_episodes':summary['episodes_verified'], 'publication':'local archive; not uploaded', 'environments':'digest-pinned Docker contexts; built Modal IDs in attempt receipts'}) save(stage/'MANIFEST.json', {'files_sha256':{str(p.relative_to(stage)):sha(p) for p in sorted(stage.rglob('*')) if p.is_file()}}) result = verify_release(stage) stage.rename(destination) with tarfile.open(archive, 'w:gz') as stream: stream.add(destination, arcname=destination.name, filter=lambda i:None if '__pycache__' in i.name.split('/') else i) return {'archive':str(archive), 'archive_sha256':sha(archive), **result} def main(): parser=argparse.ArgumentParser();sub=parser.add_subparsers(dest='command', required=True) b=sub.add_parser('build');b.add_argument('--pool',type=Path,required=True) b.add_argument('--evaluation',type=Path,required=True);b.add_argument('--output',type=Path,required=True) v=sub.add_parser('verify');v.add_argument('root',type=Path);v.add_argument('--rebuild',action='store_true') a=parser.parse_args() result=build_release(a.pool,a.evaluation,a.output) if a.command=='build' else verify_release(a.root,a.rebuild) print(json.dumps(result,indent=2)) if __name__=='__main__':main()