"""Seal an authored OpenAgentWorks bag. Does not validate business semantics or run packaged code.""" from pathlib import Path import json,hashlib,sys def seal(root): root=Path(root);data=root/'data';pkg=json.loads((data/'package.json').read_text()) files=sorted(p for p in data.rglob('*') if p.is_file());resources=[] for p in files: n=p.relative_to(data).as_posix();ext=p.suffix;schema_path=None if n=='package.json':role='manifest';mode='json';schema_path='schemas/package.schema.json' elif n.startswith('schemas/'):role='schema';mode='json_schema' elif n.startswith('catalogs/'):role='canonical_catalog';mode='json';schema_path='schemas/catalogs/'+p.stem+'.schema.json' elif n.startswith('projections/'):role='projection';mode='json';schema_path='schemas/projection.schema.json' elif n.startswith('assets/'): raise ValueError('Example profile stores asset bytes in catalog records; use declared resource support for native asset files') else:raise ValueError('Unclassified authored file '+n) resources.append({'path':n,'media_type':'application/json','role':role,'validation':mode,'schema_path':schema_path,'required':True,'source_paths':[]}) pkg['resources']=resources;pkg['canonical_catalogs']=[r['path'] for r in resources if r['role']=='canonical_catalog'];(data/'package.json').write_text(json.dumps(pkg,indent=2)+'\n') (root/'bagit.txt').write_text('BagIt-Version: 1.0\nTag-File-Character-Encoding: UTF-8\n') files=sorted(p for p in data.rglob('*') if p.is_file());(root/'bag-info.txt').write_text('Bagging-Date: 2026-11-20\nBag-Software-Agent: OpenAgentWorks-reference-1.0\nExternal-Identifier: '+pkg['package_id']+'\nPayload-Oxum: '+str(sum(p.stat().st_size for p in files))+'.'+str(len(files))+'\n') digest=lambda p:hashlib.sha256(p.read_bytes()).hexdigest() (root/'manifest-sha256.txt').write_text(''.join(digest(p)+' '+p.relative_to(root).as_posix()+'\n' for p in files)) (root/'tagmanifest-sha256.txt').write_text(''.join(digest(root/n)+' '+n+'\n' for n in ['bagit.txt','bag-info.txt','manifest-sha256.txt'])) return root if __name__=='__main__':print(seal(sys.argv[1]))