needle / scripts /run_workflow_state_cpu.py
Leon4gr45's picture
Upload folder using huggingface_hub (part 2)
25a2c1c verified
Raw History Blame Contribute Delete
3.53 kB
"""Owned authenticated CPU state/restart probes; no ports or selected tool execution."""
import hashlib
import json
import os
from pathlib import Path
import secrets
import subprocess
import time
def main():
D=['docker','--host=unix:///var/run/docker.sock'];env=dict(os.environ,NEEDLE_API_KEY=secrets.token_hex(24))
for key in ['DOCKER_HOST','DOCKER_CONTEXT','DOCKER_TLS','DOCKER_TLS_VERIFY','DOCKER_CERT_PATH']:env.pop(key,None)
def run(args):return subprocess.run(D+args,env=env,capture_output=True,text=True,check=True).stdout
for label,image in [('before','needle-gateway-v2:coverage'),('after','needle-gateway-v2:workflow')]:
cid=run(['create','--cpus','2','--memory','16g','--network','none','-e','NEEDLE_API_KEY','-e','NEEDLE_AUTH_MODE=required','--entrypoint','uvicorn',image,'scripts.workflow_cpu_app:app','--app-dir','/srv','--host','127.0.0.1','--port','7860']).strip()
try:
for folder in ['scripts','benchmarks']:run(['cp',folder,cid+':/srv/'+folder])
run(['start',cid])
checked={}
for file in ['gateway.py','models.py','registry.py','context_adapter.py','decision_policy.py','intent_guard.py','request_limits.py','laya.py']+(['payload_transport.py','native_schema.py'] if label=='after' else []):
source=subprocess.check_output(['git','show','ef25f06:'+file]) if label=='before' else Path(file).read_bytes()
actual=run(['exec',cid,'sha256sum','/srv/'+file]).split()[0]
assert actual==hashlib.sha256(source).hexdigest()
checked[file]=actual
model=run(['exec','--workdir','/root/.cache/cactus-needle/v3/3.0.1',cid,'sha256sum','--check','/srv/needle-assets.sha256'])
def ready():
for _ in range(50):
try:
run(['exec',cid,'python','-c',"import os,urllib.request;urllib.request.urlopen(urllib.request.Request('http://127.0.0.1:7860/ready',headers={'Authorization':'Bearer '+os.environ['NEEDLE_API_KEY']}))"]);return
except subprocess.CalledProcessError:time.sleep(.2)
raise RuntimeError('Owned test server not ready')
ready();smoke=json.loads(run(['exec',cid,'python','-m','scripts.smoke_registry']))
initialize=json.loads(run(['exec',cid,'python','-m','scripts.qualify_restart','initialize']))
start=time.perf_counter();run(['restart',cid]);ready();restart_ms=(time.perf_counter()-start)*1000
recovered=json.loads(run(['exec',cid,'python','-m','scripts.qualify_restart','after-restart']))
output={'methodology':'Actual authenticated local CPU registry/state/restart smoke, synthetic repeated requests; no returned tool executes. Not HF or held-out accuracy.','resources':{'cpu_quota':2,'memory_gib':16,'network':'none','published_ports':[],'client_in_same_cgroup':True,'sdk_date_fact':'date: 2026-10-17 Sat 00:00','clock_control_sha256':hashlib.sha256(Path('scripts/workflow_cpu_app.py').read_bytes()).hexdigest()},'verified_runtime_source_sha256':checked,'model_asset_check':model.strip().splitlines(),'state_smoke':smoke,'restart_initialize':initialize,'restart_recovery':recovered,'restart_to_authorized_ready_ms':restart_ms}
Path('benchmarks/workflow/results/'+label+'-state-cpu.json').write_text(json.dumps(output,indent=2)+'\n')
print(label,smoke['warm_delta_latency_ms'],restart_ms,flush=True)
finally:run(['rm','-f',cid])
if __name__=='__main__':main()