"""Run: python AgentRuntime/tests/test_runtime.py (isolated PC acceptance database).""" import copy,json,os,sys,tempfile,time,uuid from pathlib import Path root=Path(__file__).resolve().parents[2] sys.path.insert(0,str(root/'APIGateway')) os.environ['WORKOS_WORKSPACE_DATA_DIR']=tempfile.mkdtemp(prefix='runtime-acceptance-',dir='D:/codex/cache' if Path('D:/codex/cache').exists() else None) from fastapi.testclient import TestClient from app.main import create_app from app.stubs import Providers from AgentRuntime.models import Run checks=[] def check(ok,name): assert ok,name checks.append(name) def req(method,path,token=None,body=None,status=200,key=None): h={'Authorization':'Bearer '+token} if token else {} if key:h['Idempotency-Key']=key r=c.request(method,'/api/v1'+path,headers=h,json=body) assert r.status_code==status,(method,path,r.status_code,r.text) return r.json() if r.content else None def login(name):return req('POST','/auth/login',body={'username':name,'password':'admin123' if name=='admin' else 'demo123'})['access_token'] def space(token):return req('POST','/workspaces',token,{'title':'Runtime acceptance'},201,str(uuid.uuid4()))['workspace_id'] def wait(rid,token,status): for _ in range(150): r=req('GET','/runs/'+rid,token) if r['status']==status:return r if r['status']=='failed':raise AssertionError(r) time.sleep(.04) raise AssertionError((status,r)) def payload(f):return {k:copy.deepcopy(f[k]) for k in ('name','description','graph','base_ids')} p=Providers() with TestClient(create_app(p)) as c: a=login('admin');e=login('lin');z=login('zhou') req('GET','/admin/operations',status=401);req('GET','/admin/operations',e,status=403);check(True,'admin role enforced') op=req('POST','/admin/operations',a,{'name':'Prefix','kind':'transform','config':{'template':'SNAPSHOT_MARK {{request}}'}},201) oid=op['operation_id'];op=req('PUT','/admin/operations/'+oid,a,{'name':'Prefix updated','kind':'transform','config':op['config'],'expected_version':1}) req('PUT','/admin/operations/'+oid,a,{'name':'stale','kind':'transform','expected_version':1},409);check(op['version']==2,'operation CRUD optimistic revision') spare=req('POST','/admin/operations',a,{'name':'Delete me','kind':'transform'},201) req('DELETE','/admin/operations/'+spare['operation_id']+'?expected_version=1',a);check(True,'unused operation deletion') f=req('GET','/admin/workflows/official-document',a);data=payload(f);data['name']='Configurable test';g=data['graph'] g['nodes'].append({'id':'prefix','operation_id':oid,'label':'Injected prefix','x':10,'y':20,'config':{}}) g['edges']=[x for x in g['edges'] if x!={'source':'retrieve','target':'draft'}]+[{'source':'retrieve','target':'prefix'},{'source':'prefix','target':'draft'}] data['base_ids']=['kb-rules'];f=req('POST','/admin/workflows',a,data,201);fid=f['workflow_id'];w=space(a) req('POST','/admin/workflows/'+fid+'/runs',a,{'workspace_id':w,'content':'hello'},409,'unpublished-001');check(True,'unpublished workflow rejected') req('DELETE','/admin/operations/'+oid+'?expected_version=2',a,status=409);check(True,'referenced operation deletion protected') for label,mutate in [('cycle',lambda d:d['graph']['edges'].append({'source':'end','target':'start'})),('orphan',lambda d:d['graph']['edges'].pop(0)),('approval bypass',lambda d:d['graph']['edges'].append({'source':'start','target':'save'}))]: invalid=payload(f);mutate(invalid);bad=req('POST','/admin/workflows',a,invalid,201) v=req('POST','/admin/workflows/'+bad['workflow_id']+'/validate',a) req('POST','/admin/workflows/'+bad['workflow_id']+'/publish',a,{'expected_version':1},422);check(not v['valid'],label+' cannot publish') f=req('POST','/admin/workflows/'+fid+'/publish',a,{'expected_version':f['version']},201) req('PUT','/admin/operations/'+oid,a,{'name':'Changed after publish','kind':'transform','config':{'template':'NEW_MARK {{request}}'},'expected_version':2}) check(p.runtime.published({'tenant_id':'demo-tenant'},fid)['operations'][oid]['config']['template'].startswith('SNAPSHOT'),'published operation snapshot frozen') ew=space(e) req('PUT','/admin/workspaces/'+ew+'/runtime',a,{'workflow_id':fid,'workflow_version':1,'base_ids':[],'expected_version':0},422) binding={'workflow_id':fid,'workflow_version':1,'base_ids':['kb-rules'],'expected_version':0} req('PUT','/admin/workspaces/'+ew+'/runtime',a,binding);req('PUT','/admin/workspaces/'+ew+'/runtime',a,binding,409) check(True,'workspace knowledge scope and revision checked') msg=req('POST','/workspaces/'+ew+'/messages',e,{'content':'资料归档通知'},201,'employee-msg-001') er=req('POST','/workspaces/'+ew+'/runs',e,{'employee_id':'official-document-writer','input_message_id':msg['message_id']},202,'employee-run-001') req('GET','/runs/'+er['run_id'],z,status=404);req('GET','/admin/runs/'+er['run_id'],e,status=403) er=wait(er['run_id'],e,'awaiting_confirmation');check(er['workflow_id']==fid,'employee uses Workspace configured workflow') req('POST','/runs/'+er['run_id']+'/cancel',e,status=202,key='cancel-employee-001');check(True,'owner cancellation and isolation') trial={'workspace_id':w,'content':'请起草资料归档自查通知'} r=req('POST','/admin/workflows/'+fid+'/runs',a,trial,202,'trial-persist-001');rid=r['run_id'] replay=req('POST','/admin/workflows/'+fid+'/runs',a,trial,202,'trial-persist-001');check(replay==r,'admin trial durable idempotency') req('POST','/admin/workflows/'+fid+'/runs',a,{**trial,'content':'different'},409,'trial-persist-001') r=wait(rid,a,'awaiting_confirmation');artifact=req('GET','/artifacts/'+r['artifact_id'],a) check('SNAPSHOT_MARK' in artifact['content'] and 'NEW_MARK' not in artifact['content'],'configured transform really executes frozen version') check(bool(artifact['citation_ids']) and all(p.knowledge.citation({'tenant_id':'demo-tenant','user_id':'u-admin'},cid)['base_id']=='kb-rules' for cid in artifact['citation_ids']),'configured knowledge base used') check(not [x for x in p.tool.history({'tenant_id':'demo-tenant','user_id':'u-admin'},w)['items'] if x['tool_id']=='save-demo-document'],'no tool effect before approval') events=req('GET','/admin/runs/'+rid+'/events',a)['items'];check([x['sequence'] for x in events]==list(range(1,len(events)+1)),'durable event ordering') # Fresh providers model process restart; login again since Identity remains a stub. p2=Providers() with TestClient(create_app(p2)) as c: a=login('admin');r=req('GET','/runs/'+rid,a);check(r['status']=='awaiting_confirmation','human approval wait survives restart') check(req('GET','/admin/workflows/'+fid,a)['published_version']==1,'workflow and operations survive restart') check(req('POST','/admin/workflows/'+fid+'/runs',a,trial,202,'trial-persist-001')['run_id']==rid,'trial replay survives restart') changed=req('PATCH','/artifacts/'+artifact['artifact_id'],a,{'content':artifact['content']+'\nHuman checked','expected_version':artifact['version']}) req('POST','/runs/'+rid+'/approval',a,{'artifact_id':artifact['artifact_id'],'expected_version':artifact['version'],'content_hash':artifact['content_hash']},409,'stale-approval-001') approval={'artifact_id':changed['artifact_id'],'expected_version':changed['version'],'content_hash':changed['content_hash']} req('POST','/runs/'+rid+'/approval',a,approval,202,'approve-current-001');r=wait(rid,a,'succeeded') check(r['completed_count']==10 and len([x for x in p2.tool.history({'tenant_id':'demo-tenant','user_id':'u-admin'},w)['items'] if x['tool_id']=='save-demo-document' and x['status']=='succeeded'])==1,'approval resumes remaining graph and saves once') req('POST','/runs/'+rid+'/approval',a,approval,202,'approve-current-001');check(len([x for x in p2.tool.history({'tenant_id':'demo-tenant','user_id':'u-admin'},w)['items'] if x['tool_id']=='save-demo-document' and x['status']=='succeeded'])==1,'approval idempotency') check(all(s['status']=='succeeded' for s in r['steps']),'all configured nodes completed') check(req('GET','/admin/audit',a)['items'],'administrator changes audited') w2=space(a);queued=req('POST','/admin/workflows/'+fid+'/runs',a,{'workspace_id':w2,'content':'recover normal node'},202,'recover-run-001') # Shutdown while a normal node is in progress, recover from checkpoint. p3=Providers() with TestClient(create_app(p3)) as c: a=login('admin');q=wait(queued['run_id'],a,'awaiting_confirmation');check(True,'normal node recovers after shutdown') req('POST','/runs/'+q['run_id']+'/cancel',a,status=202,key='cancel-recover-001') # Simulate a crash precisely while an external side effect was inflight. with p3.runtime.Session.begin() as db: row=db.get(Run,q['run_id']);body=copy.deepcopy(row.body);body['status']='running';body['inflight']='save';row.status='running';row.body=body p3.runtime.recover();check(req('GET','/runs/'+q['run_id'],a)['error']['code']=='SIDE_EFFECT_UNCERTAIN','uncertain external effect fails safely without replay') check(not [x for x in p3.tool.history({'tenant_id':'demo-tenant','user_id':'u-admin'},w2)['items'] if x['tool_id']=='save-demo-document'],'recovery does not repeat uncertain tool call') print(json.dumps({'status':'passed','checks':len(checks),'items':checks},ensure_ascii=True))