Spaces:
Sleeping
Sleeping
File size: 12,532 Bytes
115b94d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | """Scope admission, immutable iterations and exact-edit deltas; no paid calls."""
import json
import unittest
from pathlib import Path
from types import SimpleNamespace
from fastapi.testclient import TestClient
from src.apps.workbench.business_scope import BusinessScopes,outline
from src.apps.workbench.engine import LocalWorkbench
from src.apps.workbench.web import create_web
from src.modules.skill.domain.models import SkillError,digest
from src.modules.llm_gateway.service import ModelReply
from src.tests.runtime.fixtures import setup,workspace,USER
LOGISTICS='帮我开发一个物流管理的程序,作为物流公司,能够检索到当前订单的状态,并根据实际的物流进行状态调整。'
CLEAR='按订单号查询订单状态;订单号不能为空,未找到显示未找到。'
class BusinessScopeTests(unittest.TestCase):
def setUp(self):
self.folder=self.enterContext(workspace())
self.rt,self.request,self.model,_=setup(self.folder)
self.prior={}
self.engine=SimpleNamespace(store=self.rt.store,spec=lambda *a:None,job=lambda actor,key:self.prior[key])
self.scopes=BusinessScopes(self.engine)
def test_short_explicit_is_ready_but_long_ambiguous_is_not(self):
self.assertTrue(outline(CLEAR)['ready'])
self.assertFalse(outline('帮我做一个优秀的物流系统,'+'界面美观、功能丰富、体验很好。'*40)['ready'])
s=self.scopes.prepare(USER,CLEAR,'clear-scope-01')
self.assertEqual(s['status'],'confirmed');self.assertEqual(s['confirmation'],'explicit-requirement')
self.assertEqual(self.scopes.baseline(USER,CLEAR,s['id'])['original_requirement'],CLEAR)
self.assertEqual(self.rt.store.calls('no-run'),[])
def test_ambiguous_waits_and_explicit_confirm_freezes_minimum(self):
s=self.scopes.prepare(USER,LOGISTICS,'ambiguous-01')
self.assertEqual(s['status'],'waiting_scope');self.assertLessEqual(len(s['card']['questions']),3)
self.assertNotIn('新增',s['card']['functions']);self.assertNotIn('删除',s['card']['functions'])
with self.assertRaises(SkillError):self.scopes.baseline(USER,LOGISTICS,s['id'])
s=self.scopes.confirm(USER,s['id'],s['digest'])
self.assertEqual(s,self.scopes.confirm(USER,s['id'],s['digest']))
self.assertEqual(self.scopes.baseline(USER,LOGISTICS,s['id'])['confirmation'],'explicit-minimal-scope')
def test_essential_missing_information_cannot_be_approved_away(self):
for index,requirement in enumerate(['帮我做一个完整系统','查询客户并自动审批权限']):
s=self.scopes.prepare(USER,requirement,'essential-missing-'+str(index))
self.assertFalse(s['card']['can_start'])
with self.assertRaises(SkillError) as exc:self.scopes.confirm(USER,s['id'],s['digest'])
self.assertEqual(exc.exception.code,'BUSINESS_SCOPE_INCOMPLETE')
self.assertTrue(outline(LOGISTICS+'不连接真实数据库。')['can_start'])
def test_supplement_is_new_version_not_overwrite_and_explicit_features_retained(self):
original='库存管理,包含入库、出库、报表、批量导入和多仓库。'
s=self.scopes.prepare(USER,original,'scope-supplement-01')
self.assertNotIn('报表',s['card']['excluded']);self.assertNotIn('多仓库',s['card']['excluded'])
new=self.scopes.prepare(USER,original,'scope-supplement-02',parent_id=s['id'],supplement='入库数量必须为正,出库不得超过库存;验收显示修改后的数量。')
self.assertEqual(new['version'],2);self.assertEqual(new['original_requirement'],original)
self.assertIn(original,new['requirement']);self.assertEqual(self.scopes.get(USER,s['id']),s)
def test_budget_fit_preserves_all_features_and_clear_request_starts_directly(self):
from src.modules.governance.resource_limits import configured
self.engine.resource_limits=configured()
with self.rt.store.connect() as db:db.execute('CREATE TABLE web_jobs(id TEXT,state TEXT)')
req='库存管理:查询、入库、出库、占用、预采购、报表。数量必须非负。验收:查询结果;入库结果;出库结果;占用结果;采购结果;报表结果。'
p=self.scopes.prepare(USER,req,'scope-phased-01')
self.assertEqual(p['status'],'confirmed');self.assertEqual(len(p['card']['functions']),6)
self.assertEqual(p['card']['all_functions'],['查询','入库','出库','占用','预采购','报表'])
self.assertEqual(len(p['card']['acceptance']),6)
self.assertEqual(p['card']['deferred_acceptance'],[])
self.assertEqual(p['card']['execution_mode'],'original')
self.assertLessEqual(p['card']['budget_estimate']['estimated_tokens'],60000)
self.assertEqual(p['original_requirement'],req);self.assertEqual(self.rt.store.calls('no-run'),[])
simple=self.scopes.prepare(USER,CLEAR,'scope-limited-clear')
self.assertTrue(simple['card']['ready']);self.assertEqual(simple['card']['questions'],[])
self.assertEqual(simple['status'],'confirmed')
def test_insufficient_budget_proposes_adjustment_and_budget_change_recalculates(self):
from src.modules.governance.resource_limits import configured
self.engine.resource_limits=configured({'max_tokens':80000})
with self.rt.store.connect() as db:db.execute('CREATE TABLE web_jobs(id TEXT,state TEXT)')
req='库存管理:查询、入库、出库、占用、预采购、报表。数量必须非负。验收:查询结果;入库结果;出库结果;占用结果;采购结果;报表结果。'
s=self.scopes.prepare(USER,req,'budget-adjusted')
self.assertEqual(s['status'],'waiting_scope')
self.assertEqual(s['card']['execution_mode'],'adjusted')
self.assertEqual(s['card']['functions'],['查询','入库','出库'])
self.assertEqual(s['card']['deferred_acceptance'],['占用结果','采购结果','报表结果。'])
self.assertLessEqual(s['card']['budget_estimate']['estimated_tokens'],40000)
self.engine.resource_limits=configured()
with self.assertRaises(SkillError) as exc:self.scopes.confirm(USER,s['id'],s['digest'])
self.assertEqual(exc.exception.code,'SCOPE_BUDGET_CHANGED')
updated=exc.exception.details['scope']
self.assertEqual(updated['card']['execution_mode'],'original')
self.assertEqual(len(updated['card']['functions']),6)
self.assertEqual(self.scopes.get(USER,s['id']),s)
self.scopes.confirm(USER,updated['id'],updated['digest'])
def test_tiny_budget_blocks_and_explicit_data_count_not_reduced(self):
from src.modules.governance.resource_limits import configured
with self.rt.store.connect() as db:db.execute('CREATE TABLE web_jobs(id TEXT,state TEXT)')
self.engine.resource_limits=configured({'max_tokens':20000})
s=self.scopes.prepare(USER,LOGISTICS,'budget-too-small')
self.assertFalse(s['card']['can_start']);self.assertFalse(s['card']['budget_estimate']['fits'])
with self.assertRaises(SkillError):self.scopes.confirm(USER,s['id'],s['digest'])
self.engine.resource_limits=configured()
s=self.scopes.prepare(USER,LOGISTICS+'表格显示1000条订单,使用合成数据。','data-count-preserved')
self.assertIn('1000条',s['requirement']);self.assertEqual(s['card']['execution_mode'],'original')
self.assertFalse(any('少量' in a for a in s['card']['assumptions']))
self.assertIsNone(s['card']['budget_estimate']['actual_tokens'])
def test_failure_scope_change_keeps_old_evidence_and_requires_new_confirmation(self):
run=self.rt.create(USER,self.request,'failed-scope-original')
self.rt.store.mutate(run['id'],lambda r:r.update(status='failed',error='JAVA_TEST_FAILED',diagnostics={'failures':['original list failure']}))
self.prior[run['id']]={'scenario':'business-app','status':'failed','requirement':LOGISTICS,'model_calls':[]}
original=self.rt.store.get(run['id'])
s=self.scopes.prepare(USER,LOGISTICS,'scope-failed-01',base_run=run['id'])
with self.assertRaises(SkillError) as exc:self.scopes.confirm(USER,s['id'],s['digest'])
self.assertEqual(exc.exception.code,'SCOPE_CHANGE_REQUIRED')
new=self.scopes.prepare(USER,LOGISTICS,'scope-failed-02',parent_id=s['id'],supplement='本轮只做订单号查询,状态调整留到下轮;未找到显示未找到。')
self.assertEqual(new['status'],'waiting_scope');self.assertTrue(new['new_acceptance_baseline'])
self.assertEqual(new['card']['functions'],['查询']);self.assertIn('修改',new['card']['deferred_by_user'])
another=self.scopes.prepare(USER,LOGISTICS,'scope-failed-03',parent_id=new['id'],supplement='查询结果还要显示订单号。')
self.assertEqual(another['card']['functions'],['查询'])
self.scopes.confirm(USER,new['id'],new['digest'])
self.assertEqual(self.rt.store.get(run['id']),original)
self.prior[run['id']]['status']='unknown'
with self.assertRaises(SkillError) as exc:self.scopes.prepare(USER,LOGISTICS,'unknown-scope-01',base_run=run['id'])
self.assertEqual(exc.exception.code,'SCOPE_BASELINE_UNCERTAIN')
def test_incremental_code_uses_exact_edits_and_preserves_unchanged_file(self):
run=self.rt.create(USER,self.request,'scope-incremental-code')
java='src/main/java/demo/App.java';files={java:'class App { int count=1; }','website/index.html':'<html>unchanged</html>'}
scope={'id':'s','digest':'a'*64,'version':2,'confirmation':'explicit-minimal-scope','affected_tests':['数量'],'card':outline(CLEAR)}
def active(r):
r.update(status='running',claim='scope-test')
r['request'].update(baseline={'files':files,'source_digest':digest(files)},scope_baseline=scope)
self.rt.store.mutate(run['id'],active)
def complete(messages,bound,cancelled):
payload=json.loads(messages[-1]['content'])['data']
self.assertIn('confirmed_business_plan',payload)
return ModelReply({'edits':[{'path':java,'old':'count=1','new':'count=2'}]},10,10,self.model.name)
self.model.complete=complete
result=self.rt.llm.generate(run['id'],'code',{'previous':files,'requirement':CLEAR,'repair_scope':{'mode':'exact-edits'}},lambda:False)
self.assertEqual(result['files'][java],'class App { int count=2; }')
self.assertEqual(result['files']['website/index.html'],files['website/index.html'])
class ScopeHttpTests(unittest.TestCase):
def test_original_request_bypasses_retired_scope_gate_and_keeps_queue_idempotency(self):
with workspace() as folder:
_,_,model,_=setup(Path(folder)/'model')
e=LocalWorkbench(Path(folder)/'web',[model],start_worker=False,recover_incomplete=False)
self.assertIsNone(e.resource_limits)
e.identity.seed_demo_users()
with TestClient(create_web(e)) as c:
c.headers['Origin']='http://127.0.0.1:8789'
auth=c.post('/api/session/login',json={'username':'manager','password':'VibeCoding@2026'}).json()
c.headers['X-CSRF-Token']=auth['csrf']
requirement=LOGISTICS+' 保留完整ERP表格、搜索、编辑、分页和1000条数据。'
req={'scenario':'business-app','requirement':requirement,'request_key':'original-http-test-01'}
first=c.post('/api/jobs',json=req);first.raise_for_status()
again=c.post('/api/jobs',json=req);again.raise_for_status()
self.assertEqual(first.json()['id'],again.json()['id'])
self.assertEqual(e.queue.qsize(),1);self.assertEqual(model.count,0)
frozen=e.store.get(first.json()['id'])['request']
self.assertEqual(frozen['requirement'],requirement)
self.assertNotIn('scope_baseline',frozen);self.assertNotIn('resource_limits',frozen)
old=c.post('/api/business-scope',json={'requirement':requirement,'request_key':'retired-scope-01'})
self.assertEqual(old.status_code,410)
self.assertEqual(old.json()['code'],'SCOPE_ADMISSION_REMOVED')
|