import sys,json,sqlite3,urllib.request,subprocess,argparse from pathlib import Path from datetime import timedelta parser=argparse.ArgumentParser() parser.add_argument('--db',default='/app/backend/data/webui.db') parser.add_argument('--backend',default='/app/backend') parser.add_argument('--base-url',default='http://127.0.0.1:8080') args=parser.parse_args() sys.path.insert(0,args.backend) from open_webui.utils.auth import create_token payload=json.load(sys.stdin);root=Path('/tmp/athena-tool-context-guard-test');root.mkdir(exist_ok=True) (root/'filter.py').write_text(payload['content']);(root/'test_filter.py').write_text(payload['test']) r=subprocess.run([sys.executable,str(root/'test_filter.py')],capture_output=True,text=True) print(r.stderr);assert r.returncode==0,'Guard validation failed' c=sqlite3.connect('file:'+args.db+'?mode=ro',uri=True) uid=c.execute("select id from user where role='admin' limit 1").fetchone()[0];token=create_token({'id':uid},timedelta(minutes=10)) def api(path,body=None): q=urllib.request.Request(args.base_url.rstrip('/')+path,data=json.dumps(body).encode() if body is not None else None,headers={'Authorization':'Bearer '+token,'Content-Type':'application/json'}) return json.load(urllib.request.urlopen(q,timeout=60)) assert not api('/api/tasks').get('tasks'),'Active user task; do not install during run' ident='athena_tool_context_guard' existing=c.execute('select content,is_active,is_global from function where id=?',(ident,)).fetchone() if existing: assert existing[0]==payload['content'],'Different installed version; back up and inspect before update' if existing[1] and existing[2]: pass # Still verify/apply supplied valves below. f=api('/api/v1/functions/id/'+ident) if existing else api('/api/v1/functions/create',{'id':ident,'name':'Athena Tool Context Guard','content':payload['content'],'meta':{'description':'Bounds native tool output before every model request.'}}) if not f.get('is_active'):f=api('/api/v1/functions/id/'+ident+'/toggle',{}) if not f.get('is_global'):f=api('/api/v1/functions/id/'+ident+'/toggle/global',{}) print(json.dumps({'id':f['id'],'active':f['is_active'],'global':f['is_global']})) assert f['is_active'] and f['is_global'] # The API accepts the valve dictionary directly, not {"valves": ...}. if 'valves' in payload: desired=dict(payload['valves']);desired.pop('audit_chat_ids',None) result=api('/api/v1/functions/id/'+ident+'/valves/update',desired) assert result==desired,'API silently ignored or altered valves' stored=json.loads(c.execute('select valves from function where id=?',(ident,)).fetchone()[0] or '{}') assert stored==desired,'Persisted valves differ' print(json.dumps({'valves_verified':True,'audit_enabled':False}))