VibeCoding / src /modules /llm_gateway /review_contract.py
dellweb3's picture
Deploy verified conversational workbench and isolated Java application previews
5ca3c16 verified
Raw History Blame Contribute Delete
2.31 kB
"""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)}