Spaces:
Sleeping
Sleeping
Download solar_eval/core/snapshot/impl_hash.py from dev-strender/proofread-demo: direct link, hf CLI and curl.
- Browser
- Download file 15.4 kB
-
https://huggingface.co/spaces/dev-strender/proofread-demo/resolve/main/solar_eval/core/snapshot/impl_hash.py
- Command line
-
hf download hf://spaces/dev-strender/proofread-demo/solar_eval/core/snapshot/impl_hash.py
-
curl -L -o impl_hash.py https://huggingface.co/spaces/dev-strender/proofread-demo/resolve/main/solar_eval/core/snapshot/impl_hash.py
15.4 kB
| """ꡬν ν΄μ β λͺ¨λμ AST import νν¬ Γ νμΌ λ°μ΄νΈ (κ³ν 2026-09-24-phase1a D7). | |
| μΈ κ°λκ° κ°μ μ¬λ£λ₯Ό μ΄λ€: | |
| - **μ€ν **(`step_impl_hashes`): μ€ν ν΄λμ€ λͺ¨λμμ μμν import νν¬. | |
| - **νκ°κΈ°**(`evaluator_impl_hash`): νκ°κΈ° ν΄λμ€(ν©μ±μ΄λ©΄ νμ μ λΆ)μ λͺ¨λμμ μμν νν¬. | |
| - **λ°νμ**(`runtime_impl_hash`): `RUNTIME_MODULES` + μ€μ λ‘ μ΄ νλ‘λ°μ΄λ λͺ¨λ β νν¬λ₯Ό λμ§ μκ³ | |
| λͺ©λ‘μ νμΌ λ°μ΄νΈλ§ λ³Έλ€(λͺ©λ‘μ΄ κ³§ μ μ). | |
| λͺ¨λ β νμΌμ **μ΄λ¦μΌλ‘** νΌλ€(`ImplRoots`). μ΄λ―Έ import λ λͺ¨λμ `__file__` μ΄λ importlib spec μ | |
| μ°μ§ μμΌλ―λ‘, ν μ€νΈκ° κΈ°λ³Έ 루νΈλ₯Ό tmp μ¬λ³ΈμΌλ‘ λ°κΏ λΌμ°λ©΄ μ¬import μμ΄ "ꡬνλ§ λ°κΎΌ" μν©μ΄ | |
| λ§λ€μ΄μ§λ€. ν΄μλ κ³μ°νλ μκ° λμ€ν¬μ νμΌμ μ½λλ€. | |
| """ | |
| from __future__ import annotations | |
| import ast | |
| import os | |
| import sys | |
| import sysconfig | |
| from collections.abc import Iterable, Sequence | |
| from dataclasses import dataclass, field | |
| from pathlib import Path | |
| from typing import Any | |
| from poc_bundle.canonical import content_sha256, json_fingerprint | |
| import solar_eval | |
| #: μμ§ ν¨ν€μ§ μ΄λ¦ β ν¨ν€μ§ 루νΈμμλ μ΄ μ΄λ¦ μλ λͺ¨λλ§ νΌλ€(μλνν° μ°¨λ¨). | |
| ENGINE_PACKAGE = "solar_eval" | |
| #: λ¬λΒ·νμ΄νλΌμΈ μ€νκΈ°Β·ν둬ννΈ μ‘°λ¦½ β λͺ¨λ μ€ν μ λκ°μ΄ μν₯μ μ£Όλ νλ μμν¬ λͺ¨λ. | |
| #: μ¬κΈ°μ μ΄λ² run μ΄ μ°λ νλ‘λ°μ΄λ λͺ¨λμ λν΄ `runtime_impl_hash` λ₯Ό λ¬λ€. ν μ€νΈκ° μ§ν©μ κ³ μ νλ€. | |
| #: λ¬λμμ λΌμ΄ λΈ λμ°λ―Έ(μ λ ₯Β·μ λ΅ μΆμΆ `run_records`, ν μ ν `dataset_prep`Β·`row_filter`, νμ΄νλΌμΈ | |
| #: μμ± `pipeline_factory`)λ λ¬λ λ‘μ§μ΄λΌ ν¨κ» λ³Έλ€ β `solar_eval.core.*` λ μ€ν Β·νκ°κΈ° νν¬μμ λΉ μ§λ―λ‘ | |
| #: μ¬κΈ° μμΌλ©΄ μ΄λ€ ν΄μλ μ΄ νμΌλ€μ λ³΄μ§ μλλ€. ν μ€νΈκ° λ¬λμ core import κ° μ λΆ μ¬κΈ° μλμ§ μ§ν¨λ€. | |
| RUNTIME_MODULES: tuple[str, ...] = ( | |
| "solar_eval.core.runner", | |
| "solar_eval.core.dataset_loader", | |
| "solar_eval.core.dataset_prep", | |
| "solar_eval.core.row_filter", | |
| "solar_eval.core.pipeline_factory", | |
| "solar_eval.core.run_records", | |
| "solar_eval.pipelines.pipeline", | |
| "solar_eval.pipelines.registry", | |
| "solar_eval.pipelines.base", | |
| "solar_eval.evaluators.base", | |
| "solar_eval.evaluators.registry", | |
| "solar_eval.providers.base", | |
| "solar_eval.models.prompt_version", | |
| "solar_eval.models.sample", | |
| ) | |
| #: μ€ν Β·νκ°κΈ° νν¬μμ λΉΌλ κ³΅μ© κΈ°λ° β λ°νμ ν΄μκ° ν λ² λ³΄κ±°λ μΆλ ₯κ³Ό 무κ΄ν κ². | |
| _EXCLUDED_EXACT = frozenset( | |
| {*RUNTIME_MODULES, "solar_eval.pipelines.errors", "solar_eval.config", "solar_eval._version"} | |
| ) | |
| _EXCLUDED_PREFIXES = ( | |
| "solar_eval.providers.", | |
| "solar_eval.cli.", | |
| "solar_eval.stores.", | |
| "solar_eval.core.", | |
| ) | |
| #: ꡬν ν΄μ λΌλ²¨ μ λμ΄ β `pkg:<λ£¨νΈ κΈ°μ€ POSIX κ²½λ‘>`. | |
| LABEL_PREFIX = "pkg:" | |
| #: 루νΈμμ ν μ μλ νλ‘λ°μ΄λ λͺ¨λ(ν μ€νΈ mock λ±)μ λΌλ²¨ μ λμ΄. κ°μ null λ‘ λ€μ΄κ°λ€. | |
| UNRESOLVED_PREFIX = "unresolved:" | |
| class ImplRootError(ValueError): | |
| """ꡬν ν΄μ 루νΈλ‘ μΈ μ μλ λλ ν 리(μλνν°Β·νμ€ λΌμ΄λΈλ¬λ¦¬ μμ).""" | |
| def _forbidden_bases() -> list[Path]: | |
| """λ±λ‘ 루νΈλ‘ κ±°λΆνλ μμ β site-packagesΒ·νμ€ λΌμ΄λΈλ¬λ¦¬Β·μΈν°νλ¦¬ν° prefix.""" | |
| paths = sysconfig.get_paths() | |
| bases = [paths.get(k) for k in ("purelib", "platlib", "stdlib")] | |
| bases += [sys.prefix, sys.base_prefix] | |
| return [Path(b).resolve() for b in bases if b] | |
| def _is_within(path: Path, base: Path) -> bool: | |
| try: | |
| path.relative_to(base) | |
| except ValueError: | |
| return False | |
| return True | |
| class ResolvedModule: | |
| """μ΄λ¦μΌλ‘ νΌ λͺ¨λ νλ. `label` μ μ§λ¬Έ ν€(`pkg:solar_eval/pipelines/rules.py`).""" | |
| name: str | |
| path: Path | |
| label: str | |
| class ImplRoots: | |
| """λͺ¨λ μ΄λ¦ β νμΌ ν΄μ 루νΈ. | |
| - `package_parent`: `solar_eval` ν¨ν€μ§μ λΆλͺ¨ λλ ν 리. μ¬κΈ°μλ `solar_eval.*` μ΄λ¦λ§ νΌλ€ β | |
| μμ§μ΄ site-packages μ μ€μΉλΌ μμ΄λ μλνν°κ° νν¬μ λ€μ΄μ€μ§ μλλ€. | |
| - `extra`: ν μ€νΈΒ·νμ₯μ΄ λͺ μ λ±λ‘ν λλ ν 리. μ¬κΈ°μλ μ΄λ€ μ΄λ¦μ΄λ νΌλ€. μλνν°Β·νμ€ | |
| λΌμ΄λΈλ¬λ¦¬ μμ(`sysconfig` purelib/platlib/stdlib, `sys.prefix`, `sys.base_prefix`)μ κ±°λΆνλ€ | |
| (uv μν¬μ€νμ΄μ€ venv κ° λ ν¬ μμ μμ΄ "λ ν¬ μ" κ·μΉμ΄λ©΄ httpx κ° νν¬μ λ€μ΄μ¨λ€ β κ²ν F3). | |
| """ | |
| package_parent: Path | |
| extra: tuple[Path, ...] = field(default=()) | |
| def __post_init__(self) -> None: | |
| forbidden = _forbidden_bases() | |
| for root in self.extra: | |
| resolved = Path(root).resolve() | |
| if any(_is_within(resolved, base) for base in forbidden): | |
| raise ImplRootError( | |
| f"{root} is inside the interpreter/site-packages area β third-party code is " | |
| "covered by dependency_lock, not by implementation hashes" | |
| ) | |
| def for_engine(cls, extra: Iterable[Path] = ()) -> ImplRoots: | |
| """μ§κΈ import λ `solar_eval` ν¨ν€μ§μ λΆλͺ¨ + λ±λ‘ λλ ν 리.""" | |
| parent = Path(solar_eval.__file__).resolve().parent.parent | |
| return cls(parent, tuple(Path(p) for p in extra)) | |
| def with_extra(self, *dirs: Path) -> ImplRoots: | |
| return ImplRoots(self.package_parent, (*self.extra, *(Path(d) for d in dirs))) | |
| def resolve(self, name: str) -> ResolvedModule | None: | |
| """λͺ¨λ μ΄λ¦μ νμΌλ‘ νΌλ€: `R/<a>/<b>.py` λλ `R/<a>/<b>/__init__.py`. μμΌλ©΄ None.""" | |
| parts = name.split(".") | |
| if not all(p.isidentifier() for p in parts): | |
| return None | |
| roots: list[Path] = [] | |
| if parts[0] == ENGINE_PACKAGE: | |
| roots.append(self.package_parent) | |
| roots.extend(self.extra) | |
| for root in roots: | |
| base = Path(root).joinpath(*parts) | |
| for candidate in (base.with_suffix(".py"), base / "__init__.py"): | |
| if candidate.is_file() and _exact_case(Path(root), candidate): | |
| rel = candidate.relative_to(root).as_posix() | |
| return ResolvedModule(name, candidate, LABEL_PREFIX + rel) | |
| return None | |
| def _exact_case(root: Path, candidate: Path) -> bool: | |
| """`candidate` μ κ²½λ‘ μ±λΆμ΄ λλ ν 리 λͺ©λ‘μ μ΄λ¦κ³Ό **λμλ¬ΈμκΉμ§** κ°μκ°. | |
| macOS APFS μ `is_file()` μ λμλ¬Έμλ₯Ό 무μνλ€ β `from pkg import Helper`(ν΄λμ€)κ° | |
| νμ λͺ¨λ `pkg/helper.py` λ‘ μλͺ» νλ €, κ°μ μ½λκ° Linux μ λ€λ₯Έ νν¬Β·λΌλ²¨μ λΈλ€. | |
| νμ΄μ¬ import λ λμλ¬Έμλ₯Ό ꡬλΆνλ―λ‘ μ¬κΈ°μλ λͺ©λ‘κ³Ό μ νν λμ‘°ν΄ Linux μ κ°κ² νλ€. | |
| """ | |
| current = root | |
| for part in candidate.relative_to(root).parts: | |
| try: | |
| names = os.listdir(current) | |
| except OSError: | |
| return False | |
| if part not in names: | |
| return False | |
| current = current / part | |
| return True | |
| # κΈ°λ³Έ λ£¨νΈ β None μ΄λ©΄ `ImplRoots.for_engine()`. ν μ€νΈλ `set_default_roots` λ‘ μ¬λ³Έμ λΌμ΄λ€. | |
| _DEFAULT_ROOTS: ImplRoots | None = None | |
| def default_roots() -> ImplRoots: | |
| """κΈ°λ³Έ ν΄μ 루νΈ. `set_default_roots` λ‘ λ°κΎΌ κ°μ΄ μμΌλ©΄ κ·Έκ².""" | |
| return _DEFAULT_ROOTS if _DEFAULT_ROOTS is not None else ImplRoots.for_engine() | |
| def set_default_roots(roots: ImplRoots | None) -> ImplRoots | None: | |
| """κΈ°λ³Έ 루νΈλ₯Ό λ°κΎΈκ³ μ΄μ κ°μ λλ €μ€λ€(볡μμ©). None μ΄λ©΄ μμ§ κΈ°λ³ΈμΌλ‘ λλλ¦°λ€.""" | |
| global _DEFAULT_ROOTS | |
| previous = _DEFAULT_ROOTS | |
| _DEFAULT_ROOTS = roots | |
| return previous | |
| def is_excluded(module: ResolvedModule) -> bool: | |
| """μ€ν Β·νκ°κΈ° νν¬μμ λΉΌλκ° β λ°νμ λͺ©λ‘Β·κ³΅μ© κΈ°λ°Β·λͺ¨λ ν¨ν€μ§ `__init__`.""" | |
| if module.path.name == "__init__.py": | |
| return True | |
| if module.name in _EXCLUDED_EXACT: | |
| return True | |
| return module.name.startswith(_EXCLUDED_PREFIXES) | |
| def _package_of(module: ResolvedModule) -> str: | |
| if module.path.name == "__init__.py": | |
| return module.name | |
| return module.name.rpartition(".")[0] | |
| def _absolute_from(module: ResolvedModule, node: ast.ImportFrom) -> str | None: | |
| """`from ..x import y` μ κΈ°μ€ λͺ¨λ μ΄λ¦. μ΅μμ λ°μΌλ‘ λκ°λ©΄ None.""" | |
| if node.level == 0: | |
| return node.module | |
| package = _package_of(module).split(".") if _package_of(module) else [] | |
| keep = len(package) - (node.level - 1) | |
| if keep < 0 or (keep == 0 and not node.module): | |
| return None | |
| base = package[:keep] | |
| if node.module: | |
| base += node.module.split(".") | |
| return ".".join(base) if base else None | |
| def imported_names(module: ResolvedModule, roots: ImplRoots) -> list[str]: | |
| """λͺ¨λ μμ€μ **λͺ¨λ ** import(ν¨μ μΒ·TYPE_CHECKING λΈλ‘ ν¬ν¨)κ° κ°λ¦¬ν€λ λͺ¨λ μ΄λ¦. | |
| `from P import X` λ `P.X` κ° λͺ¨λμ΄λ©΄ κ·Έ λͺ¨λ, μλλ©΄ `P`. | |
| """ | |
| tree = ast.parse(module.path.read_bytes(), filename=str(module.path)) | |
| names: list[str] = [] | |
| for node in ast.walk(tree): | |
| if isinstance(node, ast.Import): | |
| names.extend(alias.name for alias in node.names) | |
| elif isinstance(node, ast.ImportFrom): | |
| base = _absolute_from(module, node) | |
| if base is None: | |
| continue | |
| for alias in node.names: | |
| candidate = f"{base}.{alias.name}" | |
| is_module = alias.name != "*" and roots.resolve(candidate) is not None | |
| names.append(candidate if is_module else base) | |
| return names | |
| def import_closure( | |
| start: Iterable[str], roots: ImplRoots, *, exclude: bool = True | |
| ) -> dict[str, ResolvedModule]: | |
| """μμ λͺ¨λλ€μμ λ£¨νΈ μμΌλ‘ λΏλ import νν¬. μμ λͺ¨λμ μ μΈ κ·μΉκ³Ό 무κ΄νκ² λ€μ΄κ°λ€. | |
| Raises: | |
| ValueError: μμ λͺ¨λμ 루νΈμμ ν μ μμ λ(ꡬνμ λ³Ό μ μλ μ€ν Β·νκ°κΈ°). | |
| """ | |
| closure: dict[str, ResolvedModule] = {} | |
| queue: list[ResolvedModule] = [] | |
| for name in start: | |
| resolved = roots.resolve(name) | |
| if resolved is None: | |
| raise ValueError( | |
| f"module {name!r} is not under the implementation roots " | |
| f"({roots.package_parent}, extra={list(roots.extra)}) β register its directory" | |
| ) | |
| queue.append(resolved) | |
| while queue: | |
| module = queue.pop() | |
| if module.name in closure: | |
| continue | |
| closure[module.name] = module | |
| for name in imported_names(module, roots): | |
| found = roots.resolve(name) | |
| if found is None or found.name in closure: | |
| continue | |
| if exclude and is_excluded(found): | |
| continue | |
| queue.append(found) | |
| return closure | |
| def impl_hash(modules: Iterable[ResolvedModule]) -> str: | |
| """`json_fingerprint({label: content_sha256(νμΌ λ°μ΄νΈ)})` β μ£Όμ λ³κ²½λ ν΄μλ₯Ό λ°κΎΌλ€(μλ).""" | |
| return json_fingerprint({m.label: content_sha256(m.path) for m in modules}) | |
| def closure_hash(start: Iterable[str], roots: ImplRoots) -> tuple[str, list[str]]: | |
| """(νν¬ ν΄μ, μ λ ¬λ λΌλ²¨ λͺ©λ‘).""" | |
| closure = import_closure(start, roots) | |
| return impl_hash(closure.values()), sorted(m.label for m in closure.values()) | |
| def step_impl_hashes( | |
| pipeline: Any, roots: ImplRoots | None = None | |
| ) -> tuple[dict[str, str], dict[str, list[str]]]: | |
| """μμ±λ `Pipeline` μ μ€ν λ§λ€ (ν€ β ν΄μ, ν€ β νν¬ λΌλ²¨). ν€λ λ μ§μ€νΈλ¦¬ μ‘°ν μ΄λ¦. | |
| single_step μ μ묡 `llm_completion` λ νμ΄νλΌμΈ κ°μ²΄μ μμΌλ―λ‘ λ€μ΄κ°λ€(κ²ν F10). | |
| """ | |
| from solar_eval.pipelines.registry import step_key | |
| roots = roots or default_roots() | |
| hashes: dict[str, str] = {} | |
| modules: dict[str, list[str]] = {} | |
| for step in pipeline.steps: | |
| key = step_key(step.config) | |
| if key in hashes: | |
| continue | |
| hashes[key], modules[key] = closure_hash([type(step).__module__], roots) | |
| return dict(sorted(hashes.items())), dict(sorted(modules.items())) | |
| def _evaluator_modules(evaluator: Any) -> list[str]: | |
| names = [type(evaluator).__module__] | |
| for child in evaluator.sub_evaluators(): | |
| names.extend(_evaluator_modules(child)) | |
| return names | |
| def evaluator_impl_hash(evaluator: Any, roots: ImplRoots | None = None) -> tuple[str, list[str]]: | |
| """νκ°κΈ°(ν©μ±μ΄λ©΄ νμ μ λΆ) λͺ¨λμμ μμν νν¬μ (ν΄μ, λΌλ²¨).""" | |
| return closure_hash(_evaluator_modules(evaluator), roots or default_roots()) | |
| def provider_module(provider: Any) -> str: | |
| """νλ‘λ°μ΄λ(μΈμ€ν΄μ€λ ν΄λμ€)κ° μ μλ λͺ¨λ μ΄λ¦.""" | |
| return provider.__module__ if isinstance(provider, type) else type(provider).__module__ | |
| def runtime_impl_hash( | |
| providers: Sequence[Any], roots: ImplRoots | None = None | |
| ) -> tuple[str, list[str]]: | |
| """`RUNTIME_MODULES` + νλ‘λ°μ΄λ λͺ¨λμ νμΌ λ°μ΄νΈ μ§λ¬Έ (νν¬ μμ). | |
| νλ‘λ°μ΄λλ μΈμ€ν΄μ€λ ν΄λμ€λ₯Ό λ°λλ€. 루νΈμμ ν μ μλ νλ‘λ°μ΄λ λͺ¨λ(ν μ€νΈ mock λ±)μ | |
| `unresolved:<λͺ¨λ>` ν€μ null λ‘ λ€μ΄κ°λ€ β λͺ¨λ₯΄λ μ½λλ₯Ό μ‘°μ©ν λΉΌμ§ μλλ€. | |
| Raises: | |
| ValueError: `RUNTIME_MODULES` μ€ νλλ₯Ό 루νΈμμ ν μ μμ λ. | |
| """ | |
| return runtime_impl_hash_for_modules([provider_module(p) for p in providers], roots) | |
| def runtime_impl_hash_for_modules( | |
| provider_modules: Iterable[str], roots: ImplRoots | None = None | |
| ) -> tuple[str, list[str]]: | |
| """`runtime_impl_hash` μ λͺ¨λ μ΄λ¦ν β μ¬μ±μ (`with_evaluator`)μ΄ κΈ°λ‘λ λͺ¨λ μ΄λ¦μΌλ‘ λ€μ λ¬λ€.""" | |
| roots = roots or default_roots() | |
| entries: dict[str, str | None] = {} | |
| for name in RUNTIME_MODULES: | |
| resolved = roots.resolve(name) | |
| if resolved is None: | |
| raise ValueError(f"runtime module {name!r} not found under {roots.package_parent}") | |
| entries[resolved.label] = content_sha256(resolved.path) | |
| for name in sorted(set(provider_modules)): | |
| resolved = roots.resolve(name) | |
| if resolved is None: | |
| entries[UNRESOLVED_PREFIX + name] = None | |
| else: | |
| entries[resolved.label] = content_sha256(resolved.path) | |
| return json_fingerprint(entries), sorted(entries) | |
| def engine_tree_sha256(roots: ImplRoots | None = None) -> str: | |
| """μμ§ ν¨ν€μ§ νΈλ¦¬(`solar_eval/**/*.py`, `__pycache__` μ μΈ)μ νμΌ λ°μ΄νΈ μ§λ¬Έ β provenance μ©. | |
| git μμ΄λ κ³μ°λλ€. ν€λ ν¨ν€μ§ λΆλͺ¨ κΈ°μ€ POSIX κ²½λ‘(`solar_eval/core/runner.py`). | |
| """ | |
| parent = (roots or default_roots()).package_parent | |
| package = Path(parent) / ENGINE_PACKAGE | |
| entries = { | |
| path.relative_to(parent).as_posix(): content_sha256(path) | |
| for path in sorted(package.rglob("*.py")) | |
| if "__pycache__" not in path.relative_to(parent).parts | |
| } | |
| return json_fingerprint(entries) | |