"""Manifest-bounded factual artifact observation with no acceptance authority.""" from __future__ import annotations import hashlib from dataclasses import dataclass from pathlib import Path @dataclass(frozen=True) class ArtifactObservation: path: str sha256: str byte_count: int media_type: str image_dimensions: tuple[int, int] | None = None def to_record(self) -> dict[str, object]: return { "path": self.path, "sha256": self.sha256, "byte_count": self.byte_count, "media_type": self.media_type, "image_dimensions": list(self.image_dimensions) if self.image_dimensions else None, } class ObservationError(ValueError): """Raised when an artifact manifest escapes or cannot be observed safely.""" def observe_artifacts( project_dir: Path | str, artifact_paths: list[str] ) -> list[ArtifactObservation]: """Read only declared regular files and return deterministic factual metadata.""" root = Path(project_dir).resolve() observations: list[ArtifactObservation] = [] for raw_path in artifact_paths: candidate = (root / raw_path).resolve() if not candidate.is_relative_to(root) or not candidate.is_file() or candidate.is_symlink(): raise ObservationError(f"Declared artifact is unavailable or unsafe: {raw_path}") content = candidate.read_bytes() image_type = _image_type(content) dimensions = _image_dimensions(content, image_type) observations.append( ArtifactObservation( path=raw_path, sha256=hashlib.sha256(content).hexdigest(), byte_count=len(content), media_type=(f"image/{image_type}" if image_type else "application/octet-stream"), image_dimensions=dimensions, ) ) return observations def _image_dimensions(content: bytes, image_type: str | None) -> tuple[int, int] | None: if image_type == "png" and len(content) >= 24: return int.from_bytes(content[16:20], "big"), int.from_bytes(content[20:24], "big") if image_type == "gif" and len(content) >= 10: return int.from_bytes(content[6:8], "little"), int.from_bytes(content[8:10], "little") return None def _image_type(content: bytes) -> str | None: if content.startswith(b"\x89PNG\r\n\x1a\n"): return "png" if content.startswith((b"GIF87a", b"GIF89a")): return "gif" return None