"""스냅샷의 파일 재료 — 스텝이 읽을 자원(`resources`)과 프롬프트 파일(`prompt_assets`). 둘 다 **내용**만 본다: 디렉토리·파일 이름은 값에 들어가지 않으므로(프롬프트는 디렉토리 기준 상대경로가 키) 이름만 바꾼 사본은 같은 값이다 (계획 2026-09-24-phase1a D6). """ from __future__ import annotations from dataclasses import dataclass from pathlib import Path from typing import Any from poc_bundle.canonical import content_sha256 from solar_eval.core.fingerprint import iter_prompt_asset_files from solar_eval.pipelines.pipeline import ResourceRef #: 단일 YAML 프롬프트 파일의 `prompt_assets` 키 — 파일 이름이 지문에 들어가지 않게. SINGLE_FILE_KEY = "@file" @dataclass(frozen=True) class CollectedResources: """`resources` 값과 그 해석 결과. Attributes: hashes: `{라벨: sha256_64 | None}`(라벨 정렬). None = 해석 못 함(`hf:` 선언 등). refs: 라벨 → 원래 `ResourceRef`(재현 판정이 종류를 본다). """ hashes: dict[str, str | None] refs: dict[str, ResourceRef] @property def unresolved(self) -> list[str]: """값이 None 인 라벨 — 하나라도 있으면 `snapshot.missing` 에 `resources` 가 오른다.""" return [label for label, value in self.hashes.items() if value is None] def collect_resources(pipeline: Any) -> CollectedResources: """`Pipeline` 인스턴스의 스텝마다 `resource_refs()` 를 모아 파일 sha256 을 뜬다. 같은 라벨(같은 사전을 두 스텝이 쓰는 prod 파이프라인)은 한 번만 적는다. `hf:` 자원은 네트워크를 타지 않고 null 이다. 선언했는데 로컬에 없는 자원은 스텝의 `resource_refs()` 가 `KnowledgeAssetError` 로 죽인다 — run 시작 전에. """ refs: dict[str, ResourceRef] = {} for step in pipeline.steps: for ref in step.resource_refs(): refs.setdefault(ref.label, ref) hashes: dict[str, str | None] = {} for label in sorted(refs): path = refs[label].path hashes[label] = content_sha256(path) if path is not None else None return CollectedResources(hashes, {label: refs[label] for label in sorted(refs)}) def prompt_assets(prompt_path: Path) -> dict[str, str]: """프롬프트의 `{상대경로: sha256_64}` — 디렉토리면 legacy `prompt_sha` 와 같은 파일 집합. 단일 파일(YAML 프롬프트)은 `{"@file": sha256_64}`. Raises: FileNotFoundError: 경로가 없을 때. ValueError: 디렉토리에 지문 대상 파일이 하나도 없을 때(빈 프롬프트로 돈 run 을 막는다). """ path = Path(prompt_path) if path.is_dir(): assets = { file.relative_to(path).as_posix(): content_sha256(file) for file in iter_prompt_asset_files(path) } if not assets: raise ValueError(f"prompt directory has no prompt asset files: {path}") return assets if path.is_file(): return {SINGLE_FILE_KEY: content_sha256(path)} raise FileNotFoundError(f"prompt not found: {path}")