File size: 3,934 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
"""Fixed logistics requirement, synthetic budget arithmetic and atomic approval API."""
import unittest
from types import SimpleNamespace
from src.modules.orchestrator.budget_approval import propose

REQUIREMENT='帮我开发一个物流管理的程序,作为物流公司,能够检索到当前订单的状态,并根据实际的物流进行状态调整。'


class ProposalTests(unittest.TestCase):
    def test_stage_shortage_requests_one_fixed_increment(self):
        run,check,calls=self.fixture()
        check.update(input_bound=35295,model_context_limit=None,model='deepseek-v4-flash-0731')
        p,error=propose(run,check,calls,1000)
        self.assertIsNone(error);self.assertEqual(p['stage_limit'],44000)
        self.assertEqual(p['stage_delta'],12000);self.assertEqual(p['task_limit'],132000)
        self.assertEqual(p['task_remaining'],92148);self.assertFalse(p['model_context_limit_verified'])
    def fixture(self):
        run={'id':'logistics','request':{'requirement':REQUIREMENT},'snapshot':{'snapshot_digest':'fixed'},
             'cursor':8,'repairs':1,'repair_pending':True,'outputs':{},'budget':{'max_tokens':120000,'max_model_calls':6}}
        calls=[{'id':i,'state':'settled','charged':n,'reservation':n} for i,n in enumerate((10000,10000,7852))]
        check={'stage':'repair','model':'simulated-logistics-model','stage_limit':32000,'input_bound':42000,'output_target':12288,
               'minimum_output':128,'estimator':'utf8-byte-upper-bound-v1','safety_tokens':512,
               'model_context_limit':200000,'rejection':['STAGE_CONTEXT_BUDGET_EXCEEDED','stage only']}
        return run,check,calls

    def test_stage_shortage_is_not_total_shortage_or_account_balance(self):
        run,check,calls=self.fixture();p,error=propose(run,check,calls,1000)
        self.assertIsNone(error);self.assertEqual(p['stage_limit'],44000);self.assertEqual(p['stage_delta'],12000)
        self.assertEqual(p['task_limit'],132000);self.assertEqual(p['task_delta'],12000)
        self.assertEqual(p['task_remaining'],92148);self.assertEqual(p['adjustment'],'stage_and_task')
        self.assertEqual(p['model'],'simulated-logistics-model');self.assertIsNone(p['currency_estimate'])
        self.assertEqual(p['provider_balance'],'not_verified')

    def test_total_budget_adjustment_is_explicit_and_accounted(self):
        run,check,calls=self.fixture();run['budget']['max_tokens']=30000
        p,error=propose(run,check,calls,1000)
        self.assertIsNone(error);self.assertEqual(p['task_limit'],42000);self.assertEqual(p['task_delta'],12000)
        self.assertEqual(p['output_reservation'],0) # Approval never promises the next call fits.
        self.assertEqual(p['adjustment'],'stage_and_task')

    def test_hard_window_rejects_or_caps_output_without_raising_window(self):
        run,check,calls=self.fixture()
        p,error=propose(run,check,calls,1000,SimpleNamespace(model='model',context_window=40000))
        self.assertIsNone(p);self.assertEqual(error,'MODEL_CONTEXT_LIMIT_EXCEEDED')
        p,error=propose(run,check,calls,1000,SimpleNamespace(model='model',context_window=45000))
        self.assertIsNone(error);self.assertEqual(p['output_reservation'],2000)
        self.assertEqual(p['stage_limit'],44000)

    def test_unknown_call_and_nonbudget_error_are_not_approvable(self):
        run,check,calls=self.fixture();calls[-1].update(state='unknown',charged=None)
        self.assertEqual(propose(run,check,calls,1000)[1],'MODEL_RESULT_UNKNOWN')
        calls[-1].update(state='settled',charged=7852)
        for code in ['MODEL_CONTEXT_LIMIT_EXCEEDED','MODEL_QUOTA_REJECTED','JAVA_TEST_FAILED','SANDBOX_CAPACITY_EXCEEDED']:
            check['rejection']=[code,'not a budget adjustment']
            self.assertEqual(propose(run,check,calls,1000)[1],'BUDGET_NOT_ADJUSTABLE')


if __name__=='__main__':unittest.main()