File size: 2,305 Bytes
5ca3c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""One fail-closed review merge contract, shared by gateway and orchestrator."""
import re
from src.modules.harness.policy import Harness
from src.modules.skill.domain.models import SkillError


class ReviewOutputError(ValueError):
    def __init__(self,category,reason,**evidence):
        self.details={'failure_category':category,'reason_code':reason,**evidence}
        super().__init__(reason)


def checked_review_files(submitted,original,framework):
    if not isinstance(original,dict) or not original:
        raise ReviewOutputError('output_unknown','REVIEW_INPUT_CANDIDATE_MISSING')
    policy=Harness(enabled=True)
    # The saved input is checked too; a compatibility fill cannot introduce an
    # unchecked file, expand permissions, or borrow from another task.
    policy.files({'files':original},framework)
    unknown=set(submitted)-set(original)
    if unknown:
        raise ReviewOutputError('output_paths','REVIEW_PATH_SET_UNEXPECTED',
            unexpected_path_count=len(unknown))
    omitted=sorted(set(original)-set(submitted))
    merged={path:submitted.get(path,text) for path,text in original.items()}
    for content in merged.values():
        if not isinstance(content,str):raise ReviewOutputError('output_schema','REVIEW_FILE_CONTENT_TYPE')
        if '\0' in content:raise ReviewOutputError('output_security','REVIEW_NUL_BYTE')
    java=merged.get('src/main/java/demo/App.java','')
    if framework['profile_id']=='java17-requirement-business' and re.search(r'<\s*(?:!doctype\s+html|html)\b',java,re.I):
        raise ReviewOutputError('output_security','JAVA_EMBEDS_HTML')
    try:source=policy.files({'files':merged},framework)
    except SkillError as error:
        category='output_size' if error.code in {'FILE_TOO_LARGE','SOURCE_LIMIT','FILE_LIMIT'} else 'output_paths' if error.code=='PATH_NOT_ALLOWED' else 'output_security'
        raise ReviewOutputError(category,'REVIEW_'+error.code) from None
    if set(source['files'])!=set(original):
        raise ReviewOutputError('output_paths','REVIEW_CANDIDATE_INCOMPLETE')
    return source,{'contract':'review-complete-candidate-v1','compatibility_mode':'preserve_omitted' if omitted else 'complete',
                   'omitted_paths':omitted,'input_file_count':len(original),'output_file_count':len(submitted)}