24e5dda022
Keep Q&A in a separate persistent conversation with scoped evidence reads, no production tools and immutable delivery files. Add the completed-board UI, recovery handling and regression coverage.
87 lines
6.4 KiB
Python
87 lines
6.4 KiB
Python
import asyncio
|
||
import copy
|
||
import json
|
||
from pathlib import Path
|
||
import tempfile
|
||
import unittest
|
||
from unittest.mock import AsyncMock,patch
|
||
from fastapi import HTTPException
|
||
from codex_pcb import aftercare,app,rpc
|
||
from codex_pcb.config import runtime_for
|
||
from codex_pcb.storage import digest
|
||
|
||
class Aftercare(unittest.IsolatedAsyncioTestCase):
|
||
def setUp(self):
|
||
self.tmp=tempfile.TemporaryDirectory(dir=Path(__file__).parent);self.addCleanup(self.tmp.cleanup)
|
||
self.root=Path(self.tmp.name);(self.root/'delivery').mkdir();(self.root/'workspace/artifacts').mkdir(parents=True)
|
||
report=self.root/'delivery/report.txt';report.write_text('J1 pin 1: VIN24; pin 2: GND. Hardware not bench tested.')
|
||
self.t={'id':'pcb-'+'a'*24,'state':'completed','busy':False,'model':'gpt-6.1-sol','effort':'high','frozen_source':'historical-source','proposal':{'title':'FOC','markdown':'24V demo'},'delivery':{'notes':'24V demo only','files':[{'name':'report.txt','kind':'report','sha256':digest(report.read_bytes())}]}}
|
||
for target,value in [('task_dir',self.root),('workspace',self.root/'workspace')]:
|
||
p=patch.object(aftercare,target,return_value=value);p.start();self.addCleanup(p.stop)
|
||
async def test_evidence_reader_is_scoped_and_checks_delivery_hash(self):
|
||
async def read(path):return await aftercare.handle(self.t,'item/tool/call',{'tool':'pcb_read_delivery','arguments':{'path':path}})
|
||
self.assertTrue((await read('delivery/report.txt'))['success'])
|
||
for path in ['../../state.json','/etc/passwd','workspace/scripts/fix.py']:
|
||
self.assertFalse((await read(path))['success'])
|
||
(self.root/'delivery/report.txt').write_text('changed')
|
||
self.assertIn('checksum mismatch',(await read('delivery/report.txt'))['contentItems'][0]['text'])
|
||
async def test_every_production_tool_is_rejected_in_both_handlers(self):
|
||
for tool in ['pcb_eda','pcb_prepare_delivery','pcb_deliver','pcb_blocked','pcb_proposal']:
|
||
request={'tool':tool,'callId':'x','arguments':{}}
|
||
self.assertFalse((await aftercare.handle(self.t,'item/tool/call',request))['success'])
|
||
with patch.object(app,'event'),patch.object(app,'frozen') as frozen,patch.object(app,'execute',new_callable=AsyncMock) as execute:
|
||
self.assertFalse((await app.handle(self.t,'item/tool/call',request))['success']);frozen.assert_not_called();execute.assert_not_called()
|
||
self.assertEqual(self.t['state'],'completed')
|
||
async def test_api_accepts_old_completed_design_without_eda_readiness_or_source_migration(self):
|
||
before=copy.deepcopy(self.t)
|
||
with patch.object(app,'get',return_value=self.t),patch.object(app,'provider_status',return_value={'configured':True}),patch.object(app,'require_runtime_ready',new_callable=AsyncMock) as readiness,patch.object(aftercare,'schedule',return_value={'busy':True,'messages':[]}) as schedule:
|
||
result=await app.ask_question(self.t['id'],app.Question(text='怎么接线?'))
|
||
self.assertTrue(result['busy']);readiness.assert_not_called();schedule.assert_called_once()
|
||
self.assertEqual(before,self.t)
|
||
async def test_unfinished_and_blank_questions_are_rejected(self):
|
||
with patch.object(app,'get',return_value=self.t):
|
||
with self.assertRaises(HTTPException):await app.ask_question(self.t['id'],app.Question(text=' '))
|
||
self.t['state']='running'
|
||
with self.assertRaises(HTTPException):await app.ask_question(self.t['id'],app.Question(text='怎么接线?'))
|
||
async def test_answer_uses_a_separate_read_only_thread_and_preserves_design(self):
|
||
calls=[];before=copy.deepcopy(self.t);original=(self.root/'delivery/report.txt').read_bytes()
|
||
class Fake:
|
||
def __init__(self,*a,**kw):
|
||
assert kw['read_only_tools'] is True and a[1] is False
|
||
self.events=asyncio.Queue();self.requests=set()
|
||
async def __aenter__(self):return self
|
||
async def __aexit__(self,*args):pass
|
||
async def call(self,method,args):
|
||
calls.append((method,args))
|
||
if method=='thread/start':return {'thread':{'id':'qa-only'},'model':'gpt-6.1-sol'}
|
||
if method=='thread/resume':return {'thread':{'id':'qa-only'},'model':'gpt-6.1-sol'}
|
||
if method=='turn/start':
|
||
await self.events.put({'method':'item/completed','params':{'threadId':'qa-only','item':{'type':'agentMessage','id':str(len(calls)),'text':'依据 report.txt:J1.1 接 VIN24,J1.2 接 GND。','phase':'final_answer'}}})
|
||
await self.events.put({'method':'turn/completed','params':{'threadId':'qa-only','turn':{'id':'turn','status':'completed'}}})
|
||
return {'turn':{'id':'turn'}}
|
||
q={'busy':True,'messages':[]}
|
||
with patch.object(aftercare,'AppServer',Fake):
|
||
await aftercare.answer(self.t,q,'怎么接线?',runtime_for('openai'))
|
||
await aftercare.answer(self.t,q,'还要注意什么?',runtime_for('openai'))
|
||
self.assertEqual([x[0] for x in calls],['thread/start','turn/start','thread/resume','turn/start'])
|
||
self.assertEqual([x['name'] for x in calls[0][1]['dynamicTools']],['pcb_read_delivery'])
|
||
self.assertEqual(calls[1][1]['sandboxPolicy'],{'type':'readOnly','networkAccess':False})
|
||
self.assertEqual(before,self.t);self.assertEqual(original,(self.root/'delivery/report.txt').read_bytes())
|
||
self.assertFalse(q['busy']);self.assertEqual(len(q['messages']),2)
|
||
async def test_duplicate_requests_are_rejected_and_restart_preserves_history(self):
|
||
q={'busy':True,'messages':[{'role':'user','text':'怎么用?'}]};aftercare.save(self.t,q)
|
||
with self.assertRaisesRegex(ValueError,'already'):aftercare.schedule(self.t,'再问',runtime_for())
|
||
aftercare.recover(self.t);restored=aftercare.state(self.t)
|
||
self.assertFalse(restored['busy']);self.assertEqual(restored['messages'],q['messages']);self.assertIn('中断',restored['error'])
|
||
async def test_failed_question_does_not_fail_or_unlock_delivered_design(self):
|
||
before=copy.deepcopy(self.t);q={'busy':True,'messages':[]}
|
||
with patch.object(aftercare,'AppServer',side_effect=RuntimeError('model unavailable')):
|
||
await aftercare.answer(self.t,q,'怎么用?',runtime_for())
|
||
self.assertEqual(q['error'],'model unavailable');self.assertFalse(q['busy']);self.assertEqual(before,self.t)
|
||
def test_read_only_process_disables_shell_web_and_inherited_mcp(self):
|
||
(self.root/'config.toml').write_text('[mcp_servers.eda]\ncommand="eda-tool"\n')
|
||
with patch.object(rpc,'CODEX_HOME',self.root):
|
||
args,_=rpc.process_config(False,runtime_for('openai'),read_only_tools=True)
|
||
self.assertIn('sandbox_mode="read-only"',args);self.assertIn('features.shell_tool=false',args)
|
||
self.assertIn('web_search="disabled"',args);self.assertIn('mcp_servers={}',args);self.assertIn('mcp_servers.eda.enabled=false',args)
|