Files
Athena-Deck/docs/maintenance/qwen-asr-protocol-fix.py
T
Mikei386 c0bc5d3a1b Integrate Basalt telemetry and shared GPU speech runtimes
Publish the reviewed accumulated runtime, scheduler, profile, UI, native recipes and historical maintenance evidence. Includes Qwen GPU ASR correction and shared Anna/atelier ownership. Validated with 784 local tests, Python/JSON parsing and eight JavaScript syntax checks. Live GPU acceptance and full fresh-host restore limits remain documented.
2026-11-03 21:16:41 +01:00

61 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""Athena host: narrow ASR-prefix correction, retained explicit maintenance Go."""
import argparse,hashlib,importlib.util,json,shutil,subprocess,time
from pathlib import Path
ROOT=Path('/opt/athena-deck-dev/asr-protocol-fix');BASE=Path('/opt/athena-deck-dev/runtime');STATE=BASE/'state';SOURCE=Path('/opt/athena-deck-dev/source');IMAGE='athena-deck-standalone:qwen-asr-protocol-fix'
FILES=('stt.py','deploy/QWEN_ASR_GPU.md')
SETTINGS=('profiles.json','endpoint.json','audio-policy.json','cpu-partition.json','reasoning-budget.json')
def info():return json.loads(subprocess.check_output(['docker','inspect','athena-deck-dev'],text=True))[0]
def sha(p):return hashlib.sha256(p.read_bytes()).hexdigest()
def save(a):
p=ROOT/'activation.json';p.write_text(json.dumps(a,indent=2));p.chmod(0o600)
assert json.loads(p.read_text())==a
def prepare():
assert not (ROOT/'activation.json').exists(),'Never overwrite an activation record'
d=info();tag=d['Config']['Image'];assert subprocess.check_output(['docker','image','inspect','--format','{{.Id}}',tag],text=True).strip()==d['Image']
live=subprocess.check_output(['docker','exec','athena-deck-dev','python3','-B','-c','from pathlib import Path;import hashlib;print(hashlib.sha256(Path("/app/stt.py").read_bytes()).hexdigest())'],text=True).strip()
assert live==sha(SOURCE/'stt.py'),'Canonical source and current image disagree'
before={n:sha(SOURCE/n) for n in FILES};after={n:sha(ROOT/'app'/n) for n in FILES}
for n in FILES:
target=ROOT/'before'/n;target.parent.mkdir(parents=True,exist_ok=True);shutil.copy2(SOURCE/n,target)
(ROOT/'app'/n).chmod(0o644)
(ROOT/'Dockerfile').write_text('FROM '+tag+'\nUSER root\nCOPY app/ /app/\nUSER 65534:65534\n')
(ROOT/'.dockerignore').write_text('*\n!Dockerfile\n!app/\n!app/**\n')
with (ROOT/'build.log').open('w') as f:subprocess.run(['docker','build','--network','none','--pull=false','-t',IMAGE,ROOT],check=True,stdout=f,stderr=subprocess.STDOUT,timeout=180)
probe="from pathlib import Path;import tempfile;from server import Server;from stt import qwen_asr_request;assert qwen_asr_request(b'fixture','de')['messages'][-1]['content']=='language German<asr_text>';assert qwen_asr_request(b'fixture','en')['messages'][-1]['content']=='language English<asr_text>';t=tempfile.TemporaryDirectory();s=Server(0,state_dir=Path(t.name));s.endpoint.close();s.video.close();s.dashboard.close();s.server_close();t.cleanup();print('Corrected protocol and isolated full Server constructor passed')"
subprocess.run(['docker','run','--rm','--network','none','--read-only','--user','65534:65534','--memory','512m','--pids-limit','256','--cap-drop','ALL','-e','NVIDIA_VISIBLE_DEVICES=void','-e','DECK_REQUIRE_SETUP=0','-e','DECK_NETWORK_MODE=disabled','-e','TMPDIR=/test-tmp','--mount','type=bind,src='+str(STATE/'basalt-runtime/tmp')+',dst=/test-tmp','--entrypoint','python3',IMAGE,'-B','-c',probe],check=True,timeout=45)
assert info()['State']['StartedAt']==d['State']['StartedAt']
save(dict(phase='prepared',image_before=tag,image_id_before=d['Image'],started_before=d['State']['StartedAt'],source_before=before,source_after=after,config=json.loads((BASE/'installation.json').read_text()),settings={n:(STATE/n).read_text() for n in SETTINGS}))
print('Correction image built and full constructor passed; production unchanged',flush=True)
def apply():
a=json.loads((ROOT/'activation.json').read_text());assert a['phase']=='prepared'
d=json.loads((ROOT/'drained.json').read_text());assert 0<=time.time()-d['checked_at']<60 and d['state']=='stopped'
assert type(d['active_requests']) is int and d['active_requests']==0
assert all(type(d['scheduler'][k]) is int and d['scheduler'][k]==0 for k in ('active_requests','waiting_requests','tts_waiting_requests','stt_waiting_requests')) and d['scheduler']['switching'] is False
assert info()['Image']==a['image_id_before'] and all(sha(SOURCE/n)==a['source_before'][n] for n in FILES)
assert all((STATE/n).read_text()==a['settings'][n] for n in SETTINGS if n!='endpoint.json')
subprocess.run(['docker','stop','--timeout','45','athena-deck-dev'],check=True)
assert not subprocess.check_output(['nvidia-smi','--query-compute-apps=pid','--format=csv,noheader,nounits'],text=True).strip()
for n in FILES:shutil.copy2(ROOT/'app'/n,SOURCE/n)
(STATE/'endpoint.json').write_text(a['settings']['endpoint.json'])
subprocess.run(['docker','rm','athena-deck-dev'],check=True)
spec=importlib.util.spec_from_file_location('deck_install',SOURCE/'deploy/install.py');assert spec and spec.loader
module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
config=dict(a['config'],image=IMAGE,previous_image=a['image_before']);module.launch(config);module.ready(config)
deadline=time.monotonic()+90
while info()['State'].get('Health',{}).get('Status')=='starting' and time.monotonic()<deadline:time.sleep(1)
assert info()['State']['Health']['Status']=='healthy';module.write_manifest(BASE,config)
assert all(sha(SOURCE/n)==a['source_after'][n] for n in FILES)
assert all((STATE/n).read_text()==a['settings'][n] for n in SETTINGS)
verify='from pathlib import Path;import hashlib,json;print(json.dumps({n:hashlib.sha256((Path("/app")/n).read_bytes()).hexdigest() for n in '+repr(FILES)+'}))'
image_hashes=json.loads(subprocess.check_output(['docker','exec','athena-deck-dev','python3','-B','-c',verify],text=True))
assert image_hashes==a['source_after']
a.update(phase='activated; actual transcription acceptance pending',activated_at=time.time());save(a)
print('ASR protocol correction activated, healthy, image/source hashes and all protected settings verified',flush=True)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('operation',choices=('prepare','apply'));p.add_argument('--go',action='store_true');args=p.parse_args();assert args.go,'Existing explicit Michael maintenance Go required'
prepare() if args.operation=='prepare' else apply()