Files
Athena-Deck/basalt_tuning.py

39 lines
1.9 KiB
Python

"""Pinned public expert-placement metadata; no model weights or vendor edits."""
import hashlib
import os
from pathlib import Path
import urllib.request
def profile_path(root,recipe):
pin=(recipe.get('tuning') or {}).get('expert_profile')
return Path(root)/'profiles'/pin['file'] if pin else Path(root)/'source/data/profiles/qwen4exp-48x512.prof'
def profile_ready(root,recipe):
pin=(recipe.get('tuning') or {}).get('expert_profile')
if not pin:return True
p=profile_path(root,recipe)
try:return not p.is_symlink() and p.stat().st_size==pin['size'] and hashlib.sha256(p.read_bytes()).hexdigest()==pin['sha256']
except OSError:return False
def install_profile(root,recipe):
pin=(recipe.get('tuning') or {}).get('expert_profile')
if not pin:return profile_path(root,recipe)
p=profile_path(root,recipe)
if p.name!=pin['file'] or Path(pin['file']).name!=pin['file']:raise ValueError('Ungültiger Expertenprofil-Dateiname.')
if profile_ready(root,recipe):return p
if p.parent.is_symlink():raise ValueError('Expertenprofil-Verzeichnis darf kein Symlink sein.')
if p.exists() or p.is_symlink():raise ValueError('Vorhandenes Expertenprofil weicht vom festen Rezept ab; keine Überschreibung.')
if not pin['url'].startswith('https://raw.githubusercontent.com/'):raise ValueError('Feste öffentliche HTTPS-Quelle erforderlich.')
with urllib.request.urlopen(pin['url'],timeout=30) as response:data=response.read(pin['size']+1)
if len(data)!=pin['size'] or hashlib.sha256(data).hexdigest()!=pin['sha256']:raise ValueError('Expertenprofil: Größe oder SHA-256 stimmt nicht.')
p.parent.mkdir(parents=True,exist_ok=True,mode=0o700)
pending=p.with_suffix('.partial')
with pending.open('xb') as stream:
stream.write(data);stream.flush();os.fsync(stream.fileno())
pending.chmod(0o600);pending.replace(p)
if not profile_ready(root,recipe):raise ValueError('Expertenprofil nach Installation nicht verifiziert.')
return p