profileComputer.live.test.ts 9.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // @vitest-environment node
  2. import {expect,it} from 'vitest'
  3. import {createServer} from 'node:http'
  4. import {existsSync} from 'node:fs'
  5. import {spawn} from 'node:child_process'
  6. import {DatabaseSync} from 'node:sqlite'
  7. import {mkdtemp,mkdir,cp,writeFile,readFile,rm} from 'node:fs/promises'
  8. import {tmpdir} from 'node:os'
  9. import {join,resolve} from 'node:path'
  10. import {randomUUID} from 'node:crypto'
  11. import {ComputerGateway,ComputerRuntime} from '../../src/runner/worker/gateway'
  12. import {UpstreamClient} from '../../src/server/upstream'
  13. import {UpstreamServiceSession} from '../../src/server/localAuth'
  14. const scanner=process.env.YAOYAO_TIRITH_BIN??resolve(process.env.YAOYAO_HERMES_SOURCE??'.','../bin/tirith')
  15. const available=['YAOYAO_HERMES_PYTHON','YAOYAO_HERMES_SOURCE','YAOYAO_COMPUTER_IMAGE'].every(name=>!!process.env[name])&&existsSync(scanner)
  16. it.skipIf(!available).each(['profile','isolated'] as const)('runs a real Hermes %s session with native skills, authorized files, images and a real Docker VM without a Worker',async(mode)=>{
  17. const home=await mkdtemp(join(tmpdir(),'yaoyao-profile-live-')),workspace=join(home,'workspace'),db=new DatabaseSync(':memory:')
  18. await mkdir(workspace);await mkdir(join(home,'plugins'))
  19. await cp(resolve('integrations/hermes-bots-bridge'),join(home,'plugins','yaoyao-bot-bridge'),{recursive:true})
  20. await writeFile(join(workspace,'AGENTS.md'),'PROFILE_CONTEXT_MARKER: use only the fixture files and the assigned test virtual machine.\n')
  21. const skill=join(home,'skills','authorized-fixture');await mkdir(skill,{recursive:true})
  22. await writeFile(join(skill,'SKILL.md'),'---\nname: authorized-fixture\ndescription: Read the authorized fixture using the native Hermes environment.\n---\nUse scripts from this directory through Hermes terminal.\n')
  23. await writeFile(join(workspace,'authorized.txt'),'authorized-profile-file')
  24. await writeFile(join(skill,'check.py'),"from pathlib import Path\nimport os\nassert os.environ.get('YAOYAO_AUTHORIZED_FIXTURE') == 'profile-env-marker'\nassert Path('authorized.txt').read_text() == 'authorized-profile-file'\nprint('native-skill-authorized-proof')\n")
  25. await writeFile(join(home,'.env'),'YAOYAO_AUTHORIZED_FIXTURE=profile-env-marker\n')
  26. const image='iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jFz8AAAAASUVORK5CYII='
  27. const requests:any[]=[],results:any[]=[]
  28. let step=0
  29. const model=createServer(async(req,res)=>{
  30. let body='';for await(const part of req)body+=part
  31. res.setHeader('Content-Type','application/json')
  32. if(req.method==='GET'){res.end(JSON.stringify({data:[{id:'fixture-profile-model',context_length:65536}]}));return}
  33. const input=JSON.parse(body);requests.push(input)
  34. const tools=input.tools??[];let tool:string|undefined,args:any
  35. if(tools.length){
  36. results.push(...input.messages.filter((m:any)=>m.role==='tool'))
  37. const operation=[['skill_view',{name:'authorized-fixture'}],['terminal',{command:`python3 '${skill}/check.py'`,timeout:10}],['yaoyao_computer_copy_file_',{from:'host',source:join(workspace,'authorized.txt'),destination:'authorized.txt'}],['yaoyao_computer_shell_',{command:'cat authorized.txt > proof.txt; printf vm-profile-proof >> proof.txt; uname -s'}],['yaoyao_computer_read_file_',{path:'proof.txt'}],['yaoyao_computer_copy_file_',{from:'vm',source:'proof.txt',destination:join(workspace,'result.txt')}]][step]
  38. if(operation){tool=tools.find((t:any)=>t.function.name===operation[0]||t.function.name.startsWith(operation[0]))?.function.name;args=operation[1];if(tool)step++}
  39. }
  40. const message=tool?{role:'assistant',content:null,tool_calls:[{id:randomUUID(),type:'function',function:{name:tool,arguments:JSON.stringify(args)}}]}:{role:'assistant',content:tools.length?'本机与虚拟机协作完成':'fixture'}
  41. if(input.stream){res.setHeader('Content-Type','text/event-stream');res.end('data: '+JSON.stringify({id:randomUUID(),object:'chat.completion.chunk',model:'fixture-profile-model',choices:[{index:0,delta:message,finish_reason:tool?'tool_calls':'stop'}]})+'\n\ndata: [DONE]\n\n')}
  42. else res.end(JSON.stringify({id:randomUUID(),object:'chat.completion',model:'fixture-profile-model',choices:[{index:0,message,finish_reason:tool?'tool_calls':'stop'}],usage:{prompt_tokens:100,completion_tokens:30,total_tokens:130}}))
  43. })
  44. await new Promise<void>(done=>model.listen(0,'127.0.0.1',done))
  45. const modelURL=`http://127.0.0.1:${(model.address() as {port:number}).port}/v1`
  46. await writeFile(join(home,'config.yaml'),JSON.stringify({model:{default:'fixture-profile-model',provider:'custom',base_url:modelURL,api_key:'fixture',context_length:65536,max_tokens:4096,supports_vision:true},terminal:{cwd:workspace},plugins:{enabled:['yaoyao-bot-bridge']},toolsets:['all'],tools:{tool_search:{enabled:'off'}},dashboard:{turn_isolation:false},security:{tirith_path:scanner,tirith_fail_open:false},compression:{enabled:false},memory:{memory_enabled:false,user_profile_enabled:false}}))
  47. const reservation=createServer();await new Promise<void>(done=>reservation.listen(0,'127.0.0.1',done))
  48. const port=(reservation.address() as {port:number}).port;await new Promise<void>(done=>reservation.close(()=>done()))
  49. const url=new URL(`http://127.0.0.1:${port}`),source=process.env.YAOYAO_HERMES_SOURCE!,python=process.env.YAOYAO_HERMES_PYTHON!
  50. let logs='',completionTimer:ReturnType<typeof setTimeout>|undefined
  51. const child=spawn(python,['-m','hermes_cli.main','serve','--host','127.0.0.1','--port',String(port),'--skip-build','--isolated'],{cwd:workspace,env:{PATH:process.env.PATH,HOME:process.env.HOME,LANG:'en_US.UTF-8',HERMES_HOME:home,PYTHONPATH:source,HERMES_DASHBOARD_SESSION_TOKEN:'fixture-local-profile-token-123456789',HERMES_YOLO_MODE:mode==='profile'?'1':'0',NO_PROXY:'127.0.0.1,localhost'},stdio:['ignore','pipe','pipe']})
  52. child.stdout.on('data',c=>{logs=(logs+String(c)).slice(-16000)});child.stderr.on('data',c=>{logs=(logs+String(c)).slice(-16000)})
  53. const client=new UpstreamClient(url),target={url,client,session:new UpstreamServiceSession(client,()=>undefined)}
  54. const runtime=new ComputerRuntime(db,{protocol:1,runnerId:randomUUID(),serverURL:url.href,hermesURL:url.href,token:'fixture',allowedProfiles:['default'],artifactRoots:[],computers:{runtime:'docker',imageId:process.env.YAOYAO_COMPUTER_IMAGE!,python:'/must-not-launch-worker',hermesSource:'/must-not-read-profile',hermesHome:'/must-not-read-profile',network:'none'}},home,resolve('src/runner/worker/hermes_worker.py'),undefined,target)
  55. const id=randomUUID(),gateway=new ComputerGateway(runtime,{environmentId:id,agentId:id,ownerKey:'profile-live-fixture',hostAccess:mode==='profile',profileSession:mode==='profile',hermesRuntime:true},randomUUID(),async()=>{},async()=>({}),target)
  56. try{
  57. await expect.poll(async()=>{if(child.exitCode!==null)throw new Error(logs);try{return(await fetch(new URL('/api/status',url))).status}catch{return 0}},{timeout:45000}).toBe(200)
  58. const approvals=new Set<string>(),frames:any[]=[],answers:any[]=[]
  59. const complete=new Promise<any>((done,reject)=>{completionTimer=setTimeout(()=>{void readFile(join(home,'logs','agent.log'),'utf8').catch(()=>logs).then(log=>reject(new Error('Hermes fixture completion timed out\n'+JSON.stringify({step,approvals:[...approvals],frames,answers})+'\n'+log.slice(-16000))))},45000);gateway.onEvent=frame=>{
  60. if(!['message.delta','reasoning.delta'].includes(frame.type))frames.push(frame)
  61. if(frame.type==='message.complete')done(frame.payload)
  62. if(['approval.request','approval.requested'].includes(frame.type)){
  63. const id=String(frame.payload?.request_id??frame.payload?.id??'')
  64. if(!approvals.has(id)){approvals.add(id);void gateway.rpc('approval.respond',{session_id:frame.session_id,request_id:id,choice:'once'}).then(result=>{answers.push({id,result})},reject)}
  65. }
  66. };gateway.onDisconnect=()=>reject(new Error('Profile disconnected\n'+logs))})
  67. void complete.catch(()=>{})
  68. await gateway.connect();const opened=await gateway.rpc('session.create',{profile:'default'})
  69. await gateway.rpc('image.attach_bytes',{session_id:opened.session_id,filename:'fixture.png',content_base64:image})
  70. await gateway.rpc('prompt.submit',{session_id:opened.session_id,text:'依次执行本机终端、虚拟机命令、读取虚拟机文件,然后报告完成。'})
  71. const result=await complete;clearTimeout(completionTimer)
  72. expect(result.text,logs).toBe('本机与虚拟机协作完成');expect(step,JSON.stringify(requests.map(r=>r.tools?.map((t:any)=>t.function.name)))).toBe(6)
  73. expect(JSON.stringify(results)).toContain('native-skill-authorized-proof');expect(JSON.stringify(results)).toContain('vm-profile-proof');expect(JSON.stringify(results)).toContain('Linux')
  74. expect(JSON.stringify(requests.filter(r=>r.tools?.length).map(r=>r.messages))).toContain('PROFILE_CONTEXT_MARKER')
  75. expect(JSON.stringify(requests.filter(r=>r.tools?.length).map(r=>r.messages))).toContain(image)
  76. expect(JSON.stringify(results)).toContain('authorized-profile-file')
  77. expect(await readFile(join(workspace,'result.txt'),'utf8')).toBe('authorized-profile-filevm-profile-proof')
  78. expect(await runtime.extractMemory('default','只保留给定事实:测试已完成')).toBe('fixture')
  79. if(mode==='isolated')expect(approvals.size).toBeGreaterThanOrEqual(3)
  80. expect(runtime.workers.size).toBe(0)
  81. }finally{
  82. clearTimeout(completionTimer)
  83. await gateway.close().catch(()=>{});await runtime.controls.close();await runtime.pool.close();db.close();client.close()
  84. child.kill('SIGTERM');await new Promise<void>(done=>{if(child.exitCode!==null){done();return}const timer=setTimeout(()=>child.kill('SIGKILL'),5000);child.once('exit',()=>{clearTimeout(timer);done()})})
  85. await new Promise<void>(done=>{model.close(()=>done());model.closeAllConnections()});await rm(home,{recursive:true,force:true})
  86. }
  87. },120000)