File size: 8,567 Bytes
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ca3c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import json
import threading
import time
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
from src.apps.workbench.development import progress
from src.apps.workbench.project_files import materialize, execution_record, project_path
from src.modules.skill.domain.models import SkillError, digest
from src.tests.runtime.fixtures import workspace, setup, USER, OTHER


class ExecutionProgressTests(unittest.TestCase):
    def test_short_feedback_uses_recorded_stage_and_stops_at_terminal_state(self):
        from src.modules.orchestrator.feedback import MESSAGES
        self.assertTrue(all(0<len(text)<=20 for text in MESSAGES.values()))
        run={'created_at':0,'project_id':'test-project'}
        events=[{'stage':'code_review','status':'started','timestamp':10,'detail':''}]
        self.assertEqual(progress(run,'working',events,99)['short_message'],'')
        self.assertEqual(progress(run,'working',events,100)['short_message'],'正在审查代码')
        for state in ('verified','failed','cancelled','unknown'):
            self.assertEqual(progress(run,state,events,200)['short_message'],'')

    def test_checkpoint_audits_actual_state_once_without_model_calls(self):
        clock=[1000.0]
        with workspace() as folder:
            runtime,request,model,_=setup(folder,clock=lambda:clock[0])
            run=runtime.create(USER,request,'checkpoint-test')
            runtime.store.mutate(run['id'],lambda r:r.update(status='running',claim='c',lease_until=1400))
            runtime.activity_interval=.01
            with runtime.activity(run['id'],'c','build'):
                clock[0]=1101
                deadline=time.monotonic()+1
                while not any(e['status']=='audit' for e in runtime.store.stages(run['id'])) and time.monotonic()<deadline:time.sleep(.01)
                time.sleep(.04)
            records=runtime.store.stages(run['id'])
            audits=[e for e in records if e['status']=='audit']
            self.assertEqual(len(audits),1)
            data=json.loads(audits[0]['detail'])
            self.assertTrue(data['lease_valid'])
            self.assertEqual(data['model_pending'],0)
            self.assertFalse(data['process']['observed'])
            self.assertEqual(len([e for e in records if e['status']=='checkpoint']),1)
            self.assertEqual(runtime.store.calls(run['id']),[])

    def test_elapsed_and_terminal_result_are_derived_from_actual_records(self):
        run={'created_at':100}
        events=[{'stage':'build','status':'started','timestamp':110,'detail':''},
                {'stage':'build','status':'active','timestamp':125,'detail':'lease heartbeat'}]
        live=progress(run,'working',events,130)
        self.assertEqual(live['elapsed_seconds'],30)
        self.assertEqual(live['stage_elapsed_seconds'],20)
        self.assertEqual(live['last_heartbeat_at'],125)
        self.assertIn('正在',live['headline'])
        self.assertNotIn('通过',live['headline'])
        events.append({'stage':'acceptance','status':'waiting','timestamp':140,'detail':''})
        a=progress(run,'verified',events,150);b=progress(run,'verified',events,999)
        self.assertEqual(a['elapsed_seconds'],b['elapsed_seconds'])
        self.assertEqual(a['elapsed_seconds'],40)
        self.assertEqual(progress(run,'unknown',events,999)['headline'],'本次结果尚未确认')

    def test_live_lease_heartbeat_stops_after_work_and_cancel(self):
        with workspace() as folder:
            runtime,request,_,_=setup(folder);run=runtime.create(USER,request,'heartbeat-test')
            runtime.store.mutate(run['id'],lambda r:r.update(status='running',claim='c',lease_until=time.time()+20))
            runtime.activity_interval=.02
            with runtime.activity(run['id'],'c','build'):
                deadline=time.monotonic()+1
                while not runtime.store.stages(run['id']) and time.monotonic()<deadline:time.sleep(.01)
                self.assertEqual(runtime.store.stages(run['id'])[-1]['status'],'active')
            count=len(runtime.store.stages(run['id']));time.sleep(.05)
            self.assertEqual(len(runtime.store.stages(run['id'])),count)
            runtime.store.mutate(run['id'],lambda r:r.update(cancel_requested=True))
            with runtime.activity(run['id'],'c','build'):time.sleep(.05)
            self.assertEqual(len(runtime.store.stages(run['id'])),count)

    def engine(self, folder):
        run_id='run-'+'a'*32
        source='class App {}\n'
        delivery={'verified':True,'scenario':'erp','source_digest':digest({'src/main/java/demo/App.java':source}),
                  'files':[{'path':'src/main/java/demo/App.java','content':source,'change':'modified','additions':2,'deletions':1,'origin':'model-generated'}]}
        engine=SimpleNamespace(root=Path(folder),lock=threading.RLock())
        def job(actor,key):
            if actor!=USER:raise SkillError('RUN_NOT_FOUND','not found',404)
            return {'id':run_id,'source':{'source_digest':delivery['source_digest']},'evidence':{'digest':'test'},'model_calls':[],'timeline':[]}
        engine.job=job
        return engine,delivery,run_id

    def test_materialized_project_is_exact_idempotent_and_never_overwrites_edits(self):
        with workspace() as folder:
            engine,delivery,key=self.engine(folder)
            with patch('src.apps.workbench.deliverables.get_delivery',return_value=delivery):
                first=materialize(engine,USER,key);second=materialize(engine,USER,key)
                self.assertEqual(first,second)
                root=Path(first['directory']);self.assertTrue(root.is_relative_to(Path(folder)/'projects'))
                for name,expected in first['sha256'].items():self.assertEqual(hashlib.sha256((root/name).read_bytes()).hexdigest(),expected)
                self.assertIn('/preview-launch/',first['preview_url'])
                java=root/'src/main/java/demo/App.java';java.write_text('user changed',encoding='utf-8')
                with self.assertRaises(SkillError) as error:materialize(engine,USER,key)
                self.assertEqual(error.exception.code,'PROJECT_SNAPSHOT_MODIFIED')
                self.assertEqual(java.read_text(),'user changed')

    def test_unverified_and_unsafe_project_paths_cannot_write(self):
        with workspace() as folder:
            engine,delivery,key=self.engine(folder)
            for name in ('../escape','C:/escape','src/file:stream','src\\escape'):
                bad={**delivery,'files':[{**delivery['files'][0],'path':name}]}
                with patch('src.apps.workbench.deliverables.get_delivery',return_value=bad):
                    with self.assertRaises(SkillError):materialize(engine,USER,key)
            with patch('src.apps.workbench.deliverables.get_delivery',return_value={**delivery,'verified':False}):
                with self.assertRaises(SkillError):materialize(engine,USER,key)
            self.assertFalse(project_path(engine,USER,key).exists())

    def test_presentation_upgrade_creates_a_revision_and_preserves_previous_user_edits(self):
        with workspace() as folder:
            engine,delivery,key=self.engine(folder)
            with patch('src.apps.workbench.deliverables.get_delivery',return_value=delivery):
                with patch('src.apps.workbench.project_files.digest',return_value='a'*64):
                    first=materialize(engine,USER,key)
                old=Path(first['directory'])/'src/main/java/demo/App.java'
                old.write_text('user edit',encoding='utf-8')
                with patch('src.apps.workbench.project_files.digest',return_value='b'*64):
                    second=materialize(engine,USER,key)
                self.assertNotEqual(first['directory'],second['directory'])
                self.assertEqual(old.read_text(encoding='utf-8'),'user edit')
                self.assertEqual((Path(second['directory'])/'src/main/java/demo/App.java').read_text(),delivery['files'][0]['content'])

    def test_execution_record_is_read_only_hash_bound_and_access_checked(self):
        with workspace() as folder:
            engine,_,key=self.engine(folder)
            record=execution_record(engine,USER,key)
            self.assertEqual(digest(record['record']),record['digest'])
            self.assertFalse(record['human_acceptance'])
            self.assertFalse((Path(folder)/'projects').exists())
            with self.assertRaises(SkillError):execution_record(engine,OTHER,key)


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