"""Read this turn's bounded, private quality evidence for deterministic charts. This path never sends data to a model or reads original workbooks. In particular it must not use the finalizer's presentation-truncated evidence as a full dataset. """ from __future__ import annotations import json import os from pathlib import Path import re import stat import time FILE_LIMIT = 4 * 1024 * 1024 TOTAL_LIMIT = 16 * 1024 * 1024 DOCUMENT_LIMIT = 100 def load_visual_evidence(directory: Path, release_id: str, *, exclude_files=frozenset(), cancel_event=None, budget_seconds: float = 0.3) -> list[dict]: """Fail closed on bad identity, permissions, excessive size, or a changed file. The caller already owns an isolated actor/session directory. The pre-turn file inventory prevents previous questions from supplying the new answer's charts. Bounds count bytes before parsing any document. """ if directory.is_symlink(): raise ValueError('invalid evidence directory') started = time.monotonic() documents: list[dict] = [] total = 0 for path in sorted(directory.glob('nao-quality-*.json')): if cancel_event is not None and cancel_event.is_set(): return [] if time.monotonic() - started > budget_seconds: raise ValueError('visual evidence deadline') if path.name in exclude_files: continue if not re.fullmatch(r'nao-quality-[a-f0-9]{32}\.json', path.name): continue fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) with os.fdopen(fd, 'rb') as source: before = os.fstat(source.fileno()) if (not stat.S_ISREG(before.st_mode) or before.st_mode & 0o077 or before.st_uid != os.getuid() or before.st_size > FILE_LIMIT): raise ValueError('invalid evidence file') total += before.st_size if total > TOTAL_LIMIT or len(documents) >= DOCUMENT_LIMIT: raise ValueError('visual evidence limit') raw = source.read(FILE_LIMIT + 1) after = os.fstat(source.fileno()) if (len(raw) > FILE_LIMIT or before.st_size != after.st_size or before.st_mtime_ns != after.st_mtime_ns): raise ValueError('evidence changed while reading') document = json.loads(raw) if not isinstance(document, dict): raise ValueError('invalid evidence document') result = document.get('result') if (document.get('tool') != 'quality_data' or document.get('releaseId') != release_id or not isinstance(result, dict) or result.get('releaseId') != release_id or not re.fullmatch(r'Q[A-F0-9]{8}', str(document.get('evidenceRef', '')))): raise ValueError('evidence binding mismatch') if document.get('mode') in ('query', 'compare', 'top', 'changes', 'defects'): documents.append(document) return documents def visual_result(directory: Path, release_id: str, question: str, turn_id: str, *, exclude_files=frozenset(), cancel_event=None) -> dict: """A chart failure is presentation-only and must never change turn status.""" if os.environ.get('QUALITY_ANALYSIS_VISUALS_ENABLED', 'true').lower() not in ('true', '1', 'yes'): return {'visualSchemaVersion': 1, 'visualState': 'not_requested', 'visuals': []} if cancel_event is not None and cancel_event.is_set(): return {'visualSchemaVersion': 1, 'visualState': 'unavailable', 'visuals': []} try: from analysis_visuals import build_visuals evidence = load_visual_evidence(directory, release_id, exclude_files=exclude_files, cancel_event=cancel_event) if cancel_event is not None and cancel_event.is_set(): return {'visualSchemaVersion': 1, 'visualState': 'unavailable', 'visuals': []} return build_visuals(evidence, question, turn_id) except Exception: # Presentation must not turn an otherwise successful business answer into # a failed task, including unexpected adapter bugs. Do not expose paths. return {'visualSchemaVersion': 1, 'visualState': 'failed', 'visuals': []}