File size: 2,213 Bytes
dd54b02
 
 
 
 
 
 
 
 
 
 
 
 
 
115b94d
 
 
dd54b02
 
 
 
 
115b94d
dd54b02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Build-time Linux executor pin; runtime never approves or rewrites its image."""
import hashlib
import json
import gzip
import sys
from pathlib import Path
ROOT=Path(__file__).resolve().parents[2];sys.path.insert(0,str(ROOT))
from src.modules.skill.domain.models import digest
from src.adapters.sandbox.qemu import VM_LAUNCH_V8

def prepare_executor(root):
    image=root/'.local/tools/vm';control=root/'src/modules/execution/container'
    baseline=json.loads((root/'deploy/hf/base-vm-manifest.json').read_text())
    if baseline.get('launch')!=VM_LAUNCH_V8:raise RuntimeError('UNAPPROVED_VM_LAUNCH')
    latest=control/'vm_worker_v7.py'
    use_latest=latest.is_file() and hashlib.sha256(latest.read_bytes()).hexdigest()==baseline['worker']
    paths={'kernel':image/'kernel','rootfs':image/'rootfs.cpio.gz','worker':latest if use_latest else control/'vm_worker_v6.py','probe':control/'probe_v3.py'}
    for name,path in paths.items():
        if hashlib.sha256(path.read_bytes()).hexdigest()!=baseline[name]:raise RuntimeError('UNAPPROVED_VM_INPUT')
    paths['qemu']=root/'.local/tools/qemu/qemu-system-x86_64.exe'
    manifest={name:hashlib.sha256(path.read_bytes()).hexdigest() for name,path in paths.items()}
    manifest['launch']=VM_LAUNCH_V8
    (image/('manifest-v9.json' if use_latest else 'manifest-v8.json')).write_text(json.dumps(manifest,indent=2),encoding='utf-8')
    (image/'build-evidence.json').write_text(json.dumps({'backend':'qemu-tcg','platform':'linux-amd64','image':'zkcoding-qemu@sha256:'+digest(manifest),
        'inputs':manifest,'runtime_repin':False},indent=2),encoding='utf-8')
    return manifest

def main():
    database=ROOT/'.local/platform/knowledge.sqlite'
    with gzip.open(database.with_suffix('.sqlite.gz'),'rb') as source,database.open('wb') as target:
        import shutil
        shutil.copyfileobj(source,target)
    expected=json.loads((ROOT/'deploy/hf/knowledge-manifest.json').read_text())
    if hashlib.sha256(database.read_bytes()).hexdigest()!=expected['sha256']:raise RuntimeError('KNOWLEDGE_SNAPSHOT_MISMATCH')
    database.with_suffix('.sqlite.gz').unlink()
    prepare_executor(ROOT)

if __name__=='__main__':main()