File size: 6,765 Bytes
3dfc93b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""hub λŒ€μƒ νŒμ • β€” ν•œ κ³³ (κ³„νš 2026-09-27 L6 Β§2.3 Β· D-L6-1 Β· D-L6-10 Β· C-L6-8 Β· C-L6-9).

`resolve_target(cfg, project)` 은 `Target` μ΄λ‚˜ `Skip(code)` λ₯Ό λŒλ €μ€€λ‹€. 쑰건(μˆœμ„œλŒ€λ‘œ):

1. `cfg.repo_root` μ—†μŒ β†’ `is_repo_managed` 도 거짓이면 `standalone`(쑰용히), 참이면 `no_repo_root`.
2. `is_repo_managed(project)` κ±°μ§“ β†’ `not_repo_managed`(λ²ˆμ—­Β·μš”μ•½ λ“±).
3. `project_contexts(cfg.projects_dir, cfg.repo_root, only=[project])` κ°€ case λ₯Ό λͺ» μ€€λ‹€ β†’ `case_unmapped`.
4. resolve ν•œ `cfg.projects_dir` β‰  resolve ν•œ `data_home()/solar-eval-projects` β†’ `data_home_mismatch`(κ²€ν†  F4 β€”
   `cfg.projects_dir` λŠ” resolve λ˜μ§€ μ•Šμ€ κ²½λ‘œλ‹€. 심볼릭 링크 ν™ˆμ€ μ–‘μͺ½μ„ ν’€μ–΄ κ°™λ‹€).
5. 훅이 **직접** μ“°λŠ” 루트(`hub_reports_dir()` β€” μž κΈˆΒ·μƒνƒœ νŒŒμΌΒ·μƒ‰μΈ ν‚€)κ°€ 레포(링크된 μ›Œν¬νŠΈλ¦¬λ©΄ 메인 μž‘μ—… νŠΈλ¦¬λ„) μ•ˆ β†’
   `data_home_inside_repo`(κ²€ν†  F10 β€” PB λ―ΈλŸ¬Β·μ‚¬μ΄λ“œμΉ΄ μ“°κΈ°λŠ” PB κ°€ λ§‰μ§€λ§Œ 이 셋은 PB 검사λ₯Ό κ±°μΉ˜μ§€ μ•ŠλŠ”λ‹€).

`poc_bundle` 은 ν•¨μˆ˜ μ•ˆμ—μ„œλ§Œ import ν•œλ‹€(D-L6-14 β€” `solar-eval --help` κ°€ λ°±ν•„Β·λ―ΈλŸ¬λ₯Ό λ‘œλ”©ν•˜μ§€ μ•Šκ²Œ).
"""

from __future__ import annotations

from collections.abc import Mapping
from pathlib import Path
from typing import TYPE_CHECKING, Any

from solar_eval.hub_hook.types import (
    PROJECTS_DIRNAME,
    SKIP_CASE_UNMAPPED,
    SKIP_DATA_HOME_INSIDE_REPO,
    SKIP_DATA_HOME_MISMATCH,
    SKIP_NO_REPO_ROOT,
    SKIP_NOT_REPO_MANAGED,
    SKIP_STANDALONE,
    Skip,
    Target,
)

if TYPE_CHECKING:
    from solar_eval.cli.config import CLIConfig

#: pnpm μ›Œν¬μŠ€νŽ˜μ΄μŠ€ ν‘œμ‹ β€” `workspace_root` κ°€ μ½”λ“œ 체크아웃 루트λ₯Ό μ°ΎλŠ”λ‹€(C-L6-9).
WORKSPACE_MARKER = "pnpm-workspace.yaml"
#: ν›… μ“°κΈ° μœ„μΉ˜κ°€ 레포 μ•ˆμΈμ§€ λ³Ό λ•Œ `require_outside_repo` 에 μ£ΌλŠ” μ΄λ¦„ν‘œ.
HOOK_WRITE_LABEL = "hook state"


def resolve_target(
    cfg: CLIConfig, project: str, env: Mapping[str, str] | None = None
) -> Target | Skip:
    """hub λŒ€μƒ case 인가(Β§2.3). λ ˆκ±°μ‹œ μ μž¬λŠ” μ–΄λŠ κ²°κ³Όμ—μ„œλ„ λΆ€λ₯΄μ§€ μ•ŠλŠ”λ‹€(D-M2-4 (b))."""
    repo_root = cfg.repo_root
    if repo_root is None:
        return Skip(SKIP_NO_REPO_ROOT if cfg.is_repo_managed(project) else SKIP_STANDALONE)
    if not cfg.is_repo_managed(project):
        return Skip(SKIP_NOT_REPO_MANAGED)
    context = _project_context(cfg.projects_dir, repo_root, project)
    if context is None or context.case_ref is None:
        return Skip(SKIP_CASE_UNMAPPED)
    mismatch = _data_home_mismatch(cfg.projects_dir, env)
    if mismatch is not None:
        return mismatch
    inside = _hook_root_inside_repo(repo_root, env)
    if inside is not None:
        return inside
    return Target(
        project=project,
        account=context.account,
        case=context.case,
        context=context,
        repo_root=Path(repo_root),
        projects_dir=Path(cfg.projects_dir),
        # μ„€μ • 파일 `hub:` 절 β€” opt-in 해석은 `hook.gated_hub_leg` β†’ `optin.resolve_hub_optin`(L10 Β§2.6).
        hub_config=getattr(cfg, "hub", None),
    )


def _project_context(projects_dir: Path, repo_root: Path, project: str) -> Any | None:
    from poc_bundle.census.scan import project_contexts

    contexts = project_contexts(Path(projects_dir), Path(repo_root), only=[project])
    return contexts[0] if contexts else None


def _data_home_mismatch(projects_dir: Path, env: Mapping[str, str] | None) -> Skip | None:
    from poc_bundle.home import data_home

    actual = Path(projects_dir).expanduser().resolve()
    expected = (data_home(env) / PROJECTS_DIRNAME).resolve()
    if actual == expected:
        return None
    return Skip(SKIP_DATA_HOME_MISMATCH, {"projects_dir": str(actual), "expected": str(expected)})


def _hook_root_inside_repo(repo_root: Path, env: Mapping[str, str] | None) -> Skip | None:
    from poc_bundle.home import HomeError, hub_reports_dir, require_outside_repo
    from poc_bundle.mirror import linked_main_work_tree

    root = hub_reports_dir(env)
    main = linked_main_work_tree(Path(repo_root))
    for tree in (Path(repo_root),) if main is None else (Path(repo_root), main):
        try:
            require_outside_repo(root, tree, HOOK_WRITE_LABEL)
        except HomeError:
            return Skip(SKIP_DATA_HOME_INSIDE_REPO, {"hub_reports_dir": str(root)})
    return None


def case_ref_hint(cfg: CLIConfig, project: str) -> str | None:
    """`--no-ingest` μ•ˆλ‚΄μš© case 이름 β€” `poc_bundle` 없이 config 디렉토리 λͺ¨μ–‘(`accounts/<a>/<c>/03-evaluation`)μ—μ„œ.

    νŒμ •μ΄ μ•„λ‹ˆλΌ λ¬Έκ΅¬μš©μ΄λ‹€. 레포λ₯Ό λͺ¨λ₯΄λ©΄ None.
    """
    if cfg.repo_root is None:
        return None
    config_dir = cfg.config_dirs.get(project)
    if config_dir is None:
        return None
    case_dir = Path(config_dir).parent
    return f"{case_dir.parent.name}/{case_dir.name}"


def workspace_root() -> Path | None:
    """pnpm 을 돌릴 **μ½”λ“œ 체크아웃** 루트 β€” `solar_eval` νŒ¨ν‚€μ§€ νŒŒμΌμ—μ„œ μœ„λ‘œ `pnpm-workspace.yaml`(C-L6-9).

    hub 의 `--repo` λŠ” config 루트 `cfg.repo_root` λ‹€ β€” ν•©μ„± 레포(`SOLAR_EVAL_CONFIG_ROOT`)λŠ” pnpm μ›Œν¬μŠ€νŽ˜μ΄μŠ€κ°€ μ•„λ‹ˆλ‹€.
    """
    import solar_eval

    for parent in Path(solar_eval.__file__).resolve().parents:
        if (parent / WORKSPACE_MARKER).is_file():
            return parent
    return None


def is_smoke_run(source: Any) -> bool:
    """`--limit` 슀λͺ¨ν¬ run 인가(D-L6-10).

    `source` λŠ” `RunPlan`(`.selection.limit` β€” `runs start`)μ΄λ‚˜ run.json λ§€ν•‘(`dataset.selection.limit` β€” `runs resume`).
    μ–‘μ˜ μ •μˆ˜ limit 만 슀λͺ¨ν¬λ‹€(λΆˆλ¦¬μ–ΈΒ·μŒμˆ˜Β·λ¬Έμžμ—΄μ€ μ•„λ‹ˆλ‹€).
    """
    if isinstance(source, Mapping):
        dataset = source.get("dataset")
        selection = dataset.get("selection") if isinstance(dataset, Mapping) else None
        limit = selection.get("limit") if isinstance(selection, Mapping) else None
    else:
        selection = getattr(source, "selection", None)
        limit = getattr(selection, "limit", None)
    return isinstance(limit, int) and not isinstance(limit, bool) and limit > 0


def snapshot_evaluator_type(run_json: Mapping[str, Any]) -> str | None:
    """run.json μŠ€λƒ…μƒ· `config.evaluator_config.config.type` β€” λͺ¨μ–‘이 ν•˜λ‚˜λΌλ„ λ‹€λ₯΄λ©΄ None(D-L6-8).

    `poc_bundle.legacy_run._snapshot_evaluator_type` κ³Ό 같은 κ²½λ‘œλ‹€(λΉ„κ³΅κ°œ ν•¨μˆ˜λΌ import ν•˜μ§€ μ•Šκ³  같은 κ·œμΉ™μ„ λ‘”λ‹€).
    """
    node: Any = run_json.get("snapshot")
    for key in ("config", "evaluator_config", "config", "type"):
        if not isinstance(node, Mapping):
            return None
        node = node.get(key)
    return node if isinstance(node, str) and node else None