"""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']*>(.*?)',page,re.S|re.I)) if scripts: static=page.split(']+\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))