File size: 5,170 Bytes
caf8059
 
 
 
 
 
8f4045a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115b94d
 
 
 
 
 
8f4045a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc92530
 
 
 
 
 
 
 
 
 
 
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
"""Bounded immutable context; references carry no execution authority."""
import json
from src.modules.skill.domain.models import canonical, digest, SkillError


class ContextService:
    @staticmethod
    def repair_inputs(files, failure_code, diagnostics):
        """Scope compiler repairs and disclose views for list-only API failures.

        Complete sources and logs remain in the immutable run. No clipping of
        requirements, source files or contracts is used to fit a budget.
        """
        java = 'src/main/java/demo/App.java'
        text = diagnostics.get('diagnostics', '') if isinstance(diagnostics, dict) else ''
        import re
        locations = re.findall(r'(?:/input/)?([\w/.-]+\.java):\d+: error:', text)
        scoped = (failure_code == 'JAVA_BUILD_FAILED' and locations
                  and all(path == java for path in locations) and java in files)
        selected = {java: files[java]} if scoped else dict(files)
        failures=diagnostics.get('failures',[]) if isinstance(diagnostics,dict) else []
        if (failure_code=='JAVA_TEST_FAILED' and failures and java in files
                and all(isinstance(f,dict) and f.get('path') in {'/health','/api/app/meta','/unknown'} for f in failures)):
            return {'previous':{java:files[java]},'repair_scope':{'mode':'exact-edits','files':[java],
                'retained_full_sources':{path:digest(content) for path,content in files.items()},
                'unchanged_files':{path:digest(content) for path,content in files.items() if path!=java},
                'instruction':'Correct only the disclosed platform protocol failure with exact edits. Preserve the website and every business operation.'}}
        list_failure=(failure_code=='JAVA_TEST_FAILED' and bool(failures)
                      and all(isinstance(f,dict) and f.get('path')=='/api/app/list' for f in failures))
        if list_failure and 'website/index.html' in files and java in files:
            # A successful compilation plus a disclosed list failure: retain
            # routing, handlers, serialization and all browser logic. Static
            # styling and unrelated initialization/persistence bodies remain in
            # the saved patch base, never regenerated or silently clipped.
            omitted=[]
            def keep_signature(match):
                omitted.append({'method':match.group(1),'digest':digest(match.group(0))})
                return '    // Unchanged '+match.group(1)+' body retained in saved source.\n'
            java_view=re.sub(r'(?ms)^    static void (generateInitialData|persist|handleAction)\([^\n]*\).*?(?=^    (?:static |class )|\Z)',keep_signature,files[java])
            page=files['website/index.html']
            scripts='\n'.join(re.findall(r'<script\b[^>]*>(.*?)</script>',page,re.S|re.I))
            if scripts:
                static=page.split('<script',1)[0]
                ids=re.findall(r'<[^>]+\bid=["\'][^"\']+["\'][^>]*>',static)
                return {'previous':{java:java_view,'website/index.html':scripts},
                    'page_elements':ids,'repair_scope':{'mode':'exact-edits','files':list(files),
                        'retained_full_sources':{path:digest(content) for path,content in files.items()},
                        'omitted_compiled_methods':omitted,'page_view':'complete inline scripts and static element IDs; CSS/layout retained unchanged',
                        'instruction':'Repair the failing list API and its browser consumer together; apply exact snippets to saved full files. Preserve all other behavior.'}}
        return {'previous': selected,
                'repair_scope': {'files': list(selected), 'compiler_only': bool(scoped),
                    'unchanged_files': {path: digest(content) for path, content in files.items() if path not in selected},
                    'instruction': 'Correct only the reported compilation errors; preserve APIs, dependencies and business behavior.'}}

    @staticmethod
    def model_payload(inputs, requirement, runtime_contract):
        # AO04 already carries the immutable complete requirement inside its
        # context snapshot. Do not pay/reserve for a second identical copy.
        payload = dict(inputs, runtime_contract=runtime_contract)
        snapshot = payload.get('snapshot')
        document = snapshot.get('document') if isinstance(snapshot, dict) else None
        if not isinstance(document, dict) or document.get('requirement') != requirement:
            payload['requirement'] = requirement
        return payload

    def snapshot(self, document, citations, *, project_id, source_digest, release_digest):
        body = {'document': document, 'citations': citations,
                'boundary': {'project_id': project_id, 'source_digest': source_digest,
                             'release_digest': release_digest},
                'reference_trust': 'external_reference_not_instruction'}
        encoded = canonical(body)
        if len(encoded.encode()) > 18000:
            raise SkillError('CONTEXT_BUDGET_EXCEEDED', '上下文快照超出上限。', 409)
        return dict(json.loads(encoded), digest=digest(body))