proofread-demo / solar_eval /core /snapshot /environment.py
dev-strender's picture
feat: switch demo to FT + Opus article review (pipeline_exp_261021_ft_hybrid)
3dfc93b verified
Raw History Blame Contribute Delete
7.85 kB
"""์‹คํ–‰ ํ™˜๊ฒฝ ์žฌ๋ฃŒ โ€” ์˜์กด ๋ฒ„์ „ยท์—”์ง„ ๋ฒ„์ „ยท์—”์ง„ ๋ ˆํฌ ์ƒํƒœ (๊ณ„ํš 2026-09-24-phase1a D8ยทD9).
config ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ(์ง€๋ฌธ ๋Œ€์ƒ)์€ ์ถœ๋ ฅ ๊ฒฝ๋กœ์— ์žˆ๋Š” **์ง์ ‘** ๋Ÿฐํƒ€์ž„ ์˜์กด๊ณผ ์—”์ง„ ๋ฒ„์ „ยทํŒŒ์ด์ฌ ๋ถ€๋ฒ„์ „๋ฟ์ด๋‹ค.
์ „์ด ์„ค์น˜ ํํฌยทuv.lockยท์—”์ง„ ์ปค๋ฐ‹ยทํŠธ๋ฆฌ ํ•ด์‹œยทdirty ๋Š” provenance(์ง€๋ฌธ ๋ฐ–)๋กœ ๊ฐ„๋‹ค โ€” ๋ฌด๊ด€ํ•œ ์›Œํฌ์ŠคํŽ˜์ด์Šค
๋ฉค๋ฒ„์˜ ๋ณ€ํ™”๊ฐ€ ์„ค์ • ์ง€๋ฌธ์„ ํ”๋“ค์ง€ ์•Š๊ฒŒ.
"""
from __future__ import annotations
import logging
import subprocess
import sys
from collections.abc import Callable
from importlib import metadata
from pathlib import Path
from typing import Any
from packaging.requirements import InvalidRequirement, Requirement
from packaging.utils import canonicalize_name
from poc_bundle.canonical import content_sha256
import solar_eval
from solar_eval._version import __version__
from solar_eval.core.snapshot.impl_hash import ImplRoots, engine_tree_sha256
logger = logging.getLogger(__name__)
#: ์—”์ง„ ๋ฐฐํฌ ์ด๋ฆ„.
ENGINE_DISTRIBUTION = "solar-eval"
#: ๋ชจ๋ธ ์ž…์ถœ๋ ฅ์— ๋‹ฟ์ง€ ์•Š๋Š” ์ง์ ‘ ์˜์กด โ€” CLI ํ‘œ์‹œยท์„ค์ • ๋กœ๋“œยท๋ฒˆ๋“ค ๋„๊ตฌ. `dependency_lock` ์—์„œ ๋บ€๋‹ค.
#: ์ƒˆ ์ง์ ‘ ์˜์กด์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์ด ๋ชฉ๋ก์ด๋‚˜ ์ถœ๋ ฅ ์˜์กด ์ค‘ ํ•˜๋‚˜๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ „๊นŒ์ง€ ํ…Œ์ŠคํŠธ๊ฐ€ ๊นจ์ง„๋‹ค(์˜๋„).
NON_OUTPUT_DEPENDENCIES = frozenset(
{"click", "tabulate", "rich", "pydantic-settings", "poc-bundle", "packaging"}
)
DEPENDENCY_LOCK_SCHEME = "direct-runtime/v1"
#: config_dir ์•ˆ์—์„œ ์ถœ๋ ฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒฝ๋กœ(dirty ๋ฒ”์œ„). scripts/ยทinsights/ยทREADME ๋Š” ๋บ€๋‹ค(๊ฒ€ํ†  F12ยทm3).
CONFIG_DIRTY_ENTRIES = (
"project.yaml",
"prompts",
"pipelines",
"resources",
"dataset/datasets.lock.yaml",
)
GitRun = Callable[..., Any]
def _marker_env() -> dict[str, str]:
return {"extra": ""}
def direct_requirements(distribution: str = ENGINE_DISTRIBUTION) -> list[str]:
"""๋ฐฐํฌ์˜ ์ง์ ‘ `Requires-Dist` ์ค‘ ํ˜„์žฌ ํ™˜๊ฒฝ์—์„œ ์ผœ์ง€๋Š” ๊ฒƒ(extra ์—†์Œ)์˜ ์ •๊ทœํ™” ์ด๋ฆ„(์ •๋ ฌ)."""
names: set[str] = set()
for raw in metadata.requires(distribution) or []:
try:
req = Requirement(raw)
except InvalidRequirement as exc:
raise ValueError(f"{distribution}: unparsable requirement {raw!r}: {exc}") from exc
if req.marker is not None and not req.marker.evaluate(_marker_env()):
continue
names.add(canonicalize_name(req.name))
return sorted(names)
def direct_runtime_dependencies() -> dict[str, str]:
"""`dependency_lock.packages` โ€” ์ถœ๋ ฅ ๊ฒฝ๋กœ์— ์žˆ๋Š” ์ง์ ‘ ์˜์กด์˜ ์„ค์น˜ ๋ฒ„์ „."""
return {
name: metadata.version(name)
for name in direct_requirements()
if name not in NON_OUTPUT_DEPENDENCIES
}
def dependency_lock() -> dict[str, Any]:
return {
"scheme": DEPENDENCY_LOCK_SCHEME,
"root": ENGINE_DISTRIBUTION,
"packages": direct_runtime_dependencies(),
}
def installed_closure(distribution: str = ENGINE_DISTRIBUTION) -> dict[str, str | None]:
"""์—”์ง„์—์„œ ๋‹ฟ๋Š” ์„ค์น˜ ํํฌ `{์ด๋ฆ„: ๋ฒ„์ „}` โ€” provenance ์ „์šฉ(ํ™˜๊ฒฝ ์žฌ๊ตฌ์„ฑยท๋””๋ฒ„๊ทธ).
์ผœ์ง„ ์š”๊ตฌ์ธ๋ฐ ์„ค์น˜๋ผ ์žˆ์ง€ ์•Š์œผ๋ฉด ๋ฒ„์ „ None ์œผ๋กœ ๋‚จ๊ธด๋‹ค(์ˆจ๊ธฐ์ง€ ์•Š๋Š”๋‹ค).
"""
seen: dict[str, str | None] = {}
queue: list[str] = [str(canonicalize_name(distribution))]
while queue:
name = queue.pop()
if name in seen:
continue
try:
seen[name] = metadata.version(name)
except metadata.PackageNotFoundError:
seen[name] = None
continue
queue.extend(n for n in direct_requirements(name) if n not in seen)
return dict(sorted(seen.items()))
def runner_version() -> dict[str, str]:
"""`{"package", "version", "python": "<major>.<minor>"}` โ€” ํŒจ์น˜ ๋ฒ„์ „์€ ๋„ฃ์ง€ ์•Š๋Š”๋‹ค."""
return {
"package": ENGINE_DISTRIBUTION,
"version": __version__,
"python": f"{sys.version_info.major}.{sys.version_info.minor}",
}
def engine_src_dir() -> Path:
"""์—”์ง„ ์†Œ์Šค ๋ฃจํŠธ(`packages/solar-eval-python/src`) โ€” `solar_eval.__file__` ๊ธฐ์ค€."""
return Path(solar_eval.__file__).resolve().parent.parent
def engine_repo(start: Path | None = None) -> Path | None:
"""์—”์ง„ ์ฝ”๋“œ๊ฐ€ ๋“  git ๋ ˆํฌ โ€” `start`(๊ธฐ๋ณธ ์—”์ง„ ์†Œ์Šค)์—์„œ ์œ„๋กœ `.git` ์„ ์ฐพ๋Š”๋‹ค.
`cfg.repo_root` ๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค: `--projects-dir` ๋ฉด None ์ด ๋œ๋‹ค(๊ฒ€ํ†  F13). ์›ŒํฌํŠธ๋ฆฌ์˜ `.git` ์€
ํŒŒ์ผ์ด๋ผ ์กด์žฌ๋งŒ ๋ณธ๋‹ค.
"""
here = (start or engine_src_dir()).resolve()
for candidate in (here, *here.parents):
if (candidate / ".git").exists():
return candidate
return None
def dirty_scope(
repo: Path, config_dir: Path | None, repo_managed: bool, src_dir: Path | None = None
) -> list[str]:
"""dirty ํŒ์ • ๋ฒ”์œ„(๋ ˆํฌ ์ƒ๋Œ€ POSIX, ์ •๋ ฌ). ๋ ˆํฌ ๋ฐ– ๊ฒฝ๋กœ๋Š” ๋„ฃ์ง€ ์•Š๋Š”๋‹ค."""
src = (src_dir or engine_src_dir()).resolve()
candidates = [src, src.parent / "pyproject.toml", repo / "uv.lock"]
if repo_managed and config_dir is not None:
candidates += [Path(config_dir).resolve() / entry for entry in CONFIG_DIRTY_ENTRIES]
root = repo.resolve()
scope = set()
for path in candidates:
try:
scope.add(path.resolve().relative_to(root).as_posix())
except ValueError:
logger.warning("dirty scope path outside the engine repo, skipped: %s", path)
return sorted(scope)
def _git(run: GitRun, repo: Path, *args: str) -> str | None:
"""git ํ•œ ๋ฒˆ. ์‹คํŒจ(git ์—†์Œยท๋ ˆํฌ ์•„๋‹˜ยท๋น„์ •์ƒ ์ข…๋ฃŒ)๋Š” None โ€” ํ˜ธ์ถœ์ž๊ฐ€ null ๋กœ ๊ธฐ๋กํ•œ๋‹ค."""
try:
result = run(["git", "-C", str(repo), *args], capture_output=True, text=True, check=True)
except (OSError, subprocess.CalledProcessError) as exc:
logger.warning("git %s failed in %s: %s", " ".join(args[:2]), repo, exc)
return None
return result.stdout
def engine_state(
config_dir: Path | None,
repo_managed: bool,
*,
repo: Path | None = None,
src_dir: Path | None = None,
roots: ImplRoots | None = None,
run: GitRun = subprocess.run,
) -> dict[str, Any]:
"""`provenance.engine = {commit, tree_sha256, dirty, dirty_scope}` (D8).
`repo` ๋ฅผ ์•ˆ ์ฃผ๋ฉด `engine_repo()`. ๋ ˆํฌ๋ฅผ ๋ชป ์ฐพ๊ฑฐ๋‚˜ git ์ด ์‹คํŒจํ•˜๋ฉด `commit`ยท`dirty` ๋Š” null,
`tree_sha256` ์€ git ์—†์ด๋„ ๊ณ„์‚ฐ๋œ๋‹ค.
"""
repo = repo if repo is not None else engine_repo(src_dir)
tree = engine_tree_sha256(roots)
if repo is None:
return {"commit": None, "tree_sha256": tree, "dirty": None, "dirty_scope": []}
scope = dirty_scope(repo, config_dir, repo_managed, src_dir)
head = _git(run, repo, "rev-parse", "HEAD")
# ๋นˆ ๋ฒ”์œ„๋กœ status ๋ฅผ ๋ถ€๋ฅด๋ฉด ๋ ˆํฌ ์ „์ฒด๋ฅผ ๋ณธ๋‹ค โ€” ๋ฒ”์œ„๊ฐ€ ์—†์œผ๋ฉด ํŒ์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค(null).
# `--literal-pathspecs`: config_dir ์ด๋ฆ„์˜ `[`ยท`*`ยท`?` ๋ฅผ glob ์œผ๋กœ ์ฝ์ง€ ์•Š๊ฒŒ(๋ฒ”์œ„ ๋ฐ– ๋ณ€๊ฒฝ์ด dirty ๋กœ).
status = (
_git(
run,
repo,
"--literal-pathspecs",
"status",
"--porcelain",
"--untracked-files=normal",
"--",
*scope,
)
if scope
else None
)
return {
"commit": head.strip() if head else None,
"tree_sha256": tree,
"dirty": None if status is None else bool(status.strip()),
"dirty_scope": scope,
}
def uv_lock_sha256(repo: Path | None) -> str | None:
"""๋ ˆํฌ `uv.lock` ์˜ sha256 โ€” provenance ์ „์šฉ(๋ชจ๋…ธ๋ ˆํฌ lock ์€ ๋ฌด๊ด€ํ•œ ๋ฉค๋ฒ„๋กœ๋„ ๋ฐ”๋€๋‹ค)."""
if repo is None:
return None
lock = repo / "uv.lock"
return content_sha256(lock) if lock.is_file() else None