Spaces:
Sleeping
Sleeping
File size: 15,165 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | """hub ๋ค๋ฆฌ (๊ณํ 2026-09-27 L6 ยง2.6 ยท D-L6-4 ยท D-L6-5 ยท C-L6-1 ยท C-L6-3).
- argv(`build_hub_argv`) = `["pnpm", "--silent", "--fail-if-no-match", "--filter", "@poc/eval-store", "hub", "ingest",
"--case", a/c, *("--expect-run", r โฆ), "--repo", resolve(target.repo_root), "--mirror", blob_mirror_root()/<๋ฒํท>,
"--index", hub_index_root(), *(["--i-understand-local-data"] if confirm_local)]`. URL ์ argv ์ ์ฃ์ง ์๋๋ค โ ์ถ์ฒ๊ฐ
ํ๊ฒฝ ๋ณ์๋ฉด ๋ถ๋ชจ ํ๊ฒฝ ๊ทธ๋๋ก(`env=None`), ์ค์ ํ์ผ `hub.database_url` ์ด๋ฉด `child_env`(๋ถ๋ชจ ํ๊ฒฝ + `HUB_DATABASE_URL`)๋ก
**์์ ํ๊ฒฝ์ผ๋ก๋ง** ๊ฐ๋ค(๊ณํ 2026-09-28 L10 ยง2.6). ์ ์ฌ ํ๋๋ฟ์ด๋ค โ ์ด ๋ชจ๋์ ๋ค๋ฅธ hub ํ์ ๋ช
๋ น์ ์ง์ง ์๋๋ค(`proc` ํ์ฉ
๋ชฉ๋ก์ด ํ ๋ฒ ๋ ๋ง๋๋ค).
- ์คํ์ **`proc.run_pnpm`(๋ชจ๋ ์์ฑ์ผ๋ก)** ๋ง โ `cwd=workspace_root()`(์ฝ๋ ์ฒดํฌ์์, C-L6-9), `timeout=HUB_INGEST_TIMEOUT_SECONDS`.
`FileNotFoundError` โ `pnpm_missing`, `TimeoutExpired` โ `timeout`.
- ํด์(`judge_hub`, ์์ โ ยง2.6 ํ): 0 โ ok(stdout JSON `expect.ok`, `ingest_id`) ยท 1 โ `hub_<stderr "[hub] ์ค๋ฅ(<code>)">`(ํํฐ ๋ถ์ผ์น๋ฉด
`filter_no_match`) ยท 2 โ `case_blocked:<code>`ยท`case_failed:<stage>` ยท 3 โ `schema_mismatch`(์ฌ๊ตฌ์ฑํ์ง ์๋๋ค, C-M2-3) ยท
4 โ `run_not_ok:<state>`(์์ฒญ ์์๋ก ์ฒ์ ok ๊ฐ ์๋ run) ยท ๊ทธ ๋ฐ โ `hub_exit_<n>` ยท JSON ์ด ๊นจ์ก๊ฑฐ๋ ๋ชจ์์ด ๋ค๋ฅด๋ฉด
`hub_output_unreadable`.
- detail ์๋ **์ฝ๋ยทid ๋ง** ์ฃ๋๋ค(`ingest_id`ยทrun ๋ณ ์ํ). hub ์ถ๋ ฅ ์๋ฌธ(stderr ๋ฌธ๊ตฌยท์์ฝ์ ๋ค๋ฅธ ํค)์ ์ฎ๊ธฐ์ง ์๋๋ค โ
URL ์ด๋ ์
๋ ฅ ์กฐ๊ฐ์ด ์์ฌ ์์ ์ ์๋ค(D-L6-4). ์ธ๋ถ ์ถ๋ ฅ์์ ๋ฝ์ ํ ํฐ์ `_TOKEN`(์คํจ stage ๋ `_STAGE`) ๋ฌธ๋ฒ์ผ๋ก ๊ฒ์ฌํ๋ค.
- ๋ค๋ฆฌ๊ฐ ์คํจํ๋ฉด(์๊ฐ ์ด๊ณผ ํฌํจ) hub CLI stderr ์ ๋ง์ง๋ง `[hub]` ์ค ๋ช ๊ฐ๋ฅผ **์ฝ์(stderr)์๋ง** ์ฎ๊ธด๋ค โ ๋น๋ฐ์ ๊ฐ๋ฆฌ๊ณ
๊ธธ์ด๋ฅผ ์๋ฅธ ๋ค(`diagnostic_lines`). ์ฝ๋๋ง์ผ๋ก๋ ๋ฌด์์ด ์๋ชป๋๋์ง ์ ์ ์์ด์๋ค(๋ฆฌ๋ทฐ ์ง์ 6). ์ํ ํ์ผ์๋ ์ฃ์ง ์๋๋ค.
๊ฐ๋ฆผ์ ํ๊ฒฝ ๋ณ์ ๊ฐ์ ๋ํด ์ค์ ์ถ์ฒ ๊ฐ(`secrets` โ `optin.HubOptIn.secrets`)๋ ๋ณธ๋ค.
- opt-in(`HUB_DATABASE_URL`ยท์ค์ `hub:` ์ )ยทํ์ธยท๋ฒ๋ค ์ ๋ฌด ๊ฒ์ดํธ๋ ์ฌ๊ธฐ๊ฐ ์๋๋ผ `hook.gated_hub_leg` ํ ๊ณณ์ด๋ค.
`poc_bundle` ์ ํจ์ ์์์๋ง import ํ๋ค(D-L6-14).
"""
from __future__ import annotations
import json
import re
import subprocess
from collections.abc import Mapping, Sequence
from pathlib import Path
from typing import Any
import click
from solar_eval.hub_hook import console, proc, status, target
from solar_eval.hub_hook.optin import CONFIRM_FLAG
from solar_eval.hub_hook.types import (
CASE_BLOCKED_PREFIX,
CASE_FAILED_PREFIX,
EXPECT_MISSING,
EXPECT_NO_USABLE_BUNDLE,
EXPECT_OK,
EXPECT_ORPHANED,
FILTER_NO_MATCH,
HUB_CASE_BLOCKED,
HUB_CASE_FAILED,
HUB_EXIT_CASE,
HUB_EXIT_EXPECT,
HUB_EXIT_OK,
HUB_EXIT_PREFIX,
HUB_EXIT_REFUSED,
HUB_EXIT_SCHEMA,
HUB_FAILED_PREFIX,
HUB_INGEST_TIMEOUT_SECONDS,
HUB_OUTPUT_UNREADABLE,
HUB_PACKAGE_FILTER,
NO_WORKSPACE,
PNPM_MISSING,
PNPM_NO_MATCH,
RUN_NOT_OK_PREFIX,
SCHEMA_MISMATCH,
STATUS_FAILED,
TIMEOUT,
LegResult,
Target,
)
#: pnpm ์ ์ญ ์ต์
โ ์ถ๋ ฅ์ ์คํฌ๋ฆฝํธ ๊ฒ๋ง, ํํฐ ๋ถ์ผ์น๋ ์ข
๋ฃ 1(pnpm 10.30.1 ์ฌ์ค, ยง2.6).
PNPM_PREFIX = ("pnpm", "--silent", "--fail-if-no-match", "--filter", HUB_PACKAGE_FILTER)
HUB_INGEST = ("hub", "ingest")
#: hub CLI stderr ์ค๋ฅ ์ค `[hub] ์ค๋ฅ(<code>): โฆ` ์ ์ฝ๋.
_HUB_ERROR_LINE = re.compile(r"^\[hub\] ์ค๋ฅ\(([a-z][a-z0-9_]{0,63})\)", re.MULTILINE)
#: ์ธ๋ถ ์ถ๋ ฅ์์ ๋ฝ์ ์ฝ๋์ ์ฃ๋ ํ ํฐ(case ์ฐจ๋จ ์ฝ๋ยทingest_id)์ ๋ฌธ๋ฒ.
_TOKEN = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,127}")
#: ์คํจ stage โ ES `orchestrate.ts` ๊ฐ ๋ด๋ ๋จ๊ณ ์ด๋ฆ(`datasets`ยท`run-bundles`ยท`digest`ยท`case_lock`ยท`unknown`)๊ณผ
#: preflight ์คํจ์ `preflight:<๋จ๊ณ>`(๋ฆฌ๋ทฐ ์ง์ 5). `:` ์ ํ ๋ฒ๋ง ๋ฐ๋๋ค.
_STAGE = re.compile(r"[a-z][a-z0-9_-]{0,63}(?::[a-z][a-z0-9_-]{0,63})?")
#: ์คํจํ hub ๋ค๋ฆฌ์์ ์ฝ์(stderr)์ ์ฎ๊ธฐ๋ hub CLI stderr ๊ผฌ๋ฆฌ โ ์ค ์์ ํ ์ค ๊ธธ์ด ์ํ(๋ฆฌ๋ทฐ ์ง์ 6).
HUB_DIAG_MAX_LINES = 5
HUB_DIAG_MAX_CHARS = 300
_HUB_LOG_PREFIX = "[hub]"
#: `--expect-run` ๊ฐ โ ์ต์
์ผ๋ก ์ค์ธ๋์ง ์๊ฒ `-` ๋ก ์์ํ์ง ์๊ณ ์ ์ด ๋ฌธ์๊ฐ ์๋ค.
_CONTROL = re.compile(r"[\x00-\x1f\x7f-\x9f]")
EXPECT_STATES = frozenset({EXPECT_OK, EXPECT_MISSING, EXPECT_NO_USABLE_BUNDLE, EXPECT_ORPHANED})
class _Unreadable(ValueError):
"""hub ์ถ๋ ฅ์ด ๊ธฐ๋ํ ๋ชจ์์ด ์๋๋ค โ `hub_output_unreadable` ๋ก ๋ฐ๋๋ค(์๋ฌธ์ ์ฃ์ง ์๋๋ค)."""
# โโ argv(์์) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def build_hub_argv(
target: Target,
run_ids: Sequence[str],
*,
confirm_local: bool,
env: Mapping[str, str] | None = None,
) -> list[str]:
"""hub ์ ์ฌ argv(์์ โ ๊ฒฝ๋ก๋ `poc_bundle.home` ํด์๊ฐ). run_id ๊ฐ ๋น์๊ฑฐ๋ ์ต์
๋ชจ์์ด๋ฉด `ValueError`."""
from poc_bundle.home import blob_mirror_root, hub_index_root
from poc_bundle.mirror import DEFAULT_BUCKET
expect: list[str] = []
for run_id in _checked_run_ids(run_ids):
expect += ["--expect-run", run_id]
return [
*PNPM_PREFIX,
*HUB_INGEST,
"--case",
target.case_ref,
*expect,
"--repo",
# ์ ๋ ๊ฒฝ๋ก๋ก โ ์๋๋ฉด ์์(pnpm ์คํฌ๋ฆฝํธ cwd = ES ํจํค์ง ๋๋ ํ ๋ฆฌ)์ด ๋ค๋ฅธ ๊ณณ์ผ๋ก ํผ๋ค(D-L6-5, ๋ฆฌ๋ทฐ ์ง์ 2).
str(Path(target.repo_root).expanduser().resolve()),
"--mirror",
str(blob_mirror_root(env) / DEFAULT_BUCKET),
"--index",
str(hub_index_root(env)),
*([CONFIRM_FLAG] if confirm_local else []),
]
def _checked_run_ids(run_ids: Sequence[str]) -> list[str]:
ids = list(run_ids)
if not ids:
raise ValueError("hub leg needs at least one run_id")
for run_id in ids:
if not isinstance(run_id, str) or not run_id or run_id.startswith("-"):
raise ValueError("hub leg run_id must be a non-empty string not starting with '-'")
if _CONTROL.search(run_id):
raise ValueError("hub leg run_id has a control character")
return ids
# โโ ํ์ (์์, ยง2.6 ํ) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def judge_hub(result: subprocess.CompletedProcess[str], run_ids: Sequence[str]) -> LegResult:
"""hub CLI ์ข
๋ฃ ์ฝ๋ยท์ถ๋ ฅ โ `LegResult`. ์ถ๋ ฅ ์๋ฌธ์ ์ฃ์ง ์๋๋ค."""
code = result.returncode
stdout, stderr = result.stdout or "", result.stderr or ""
if code != HUB_EXIT_OK and PNPM_NO_MATCH in f"{stdout}\n{stderr}":
return LegResult.failed(FILTER_NO_MATCH)
try:
if code == HUB_EXIT_OK:
return _judge_ok(stdout, run_ids)
if code == HUB_EXIT_REFUSED:
return LegResult.failed(_refusal_code(stderr))
if code == HUB_EXIT_CASE:
return _judge_case(stdout, run_ids)
if code == HUB_EXIT_SCHEMA:
return LegResult.failed(SCHEMA_MISMATCH)
if code == HUB_EXIT_EXPECT:
return _judge_expect_failed(stdout, run_ids)
except _Unreadable:
return LegResult.failed(HUB_OUTPUT_UNREADABLE)
return LegResult.failed(f"{HUB_EXIT_PREFIX}{code}")
def _judge_ok(stdout: str, run_ids: Sequence[str]) -> LegResult:
doc = _summary(stdout)
states = _expect_states(doc, run_ids)
if any(state != EXPECT_OK for state in states.values()):
raise _Unreadable("exit 0 with a run that is not ok")
return LegResult.ok({"ingest_id": _token(doc.get("ingest_id")), "expect": states})
def _refusal_code(stderr: str) -> str:
match = _HUB_ERROR_LINE.search(stderr)
return (
f"{HUB_FAILED_PREFIX}{match.group(1)}" if match else f"{HUB_EXIT_PREFIX}{HUB_EXIT_REFUSED}"
)
def _judge_case(stdout: str, run_ids: Sequence[str]) -> LegResult:
doc = _summary(stdout)
cases = doc.get("cases")
if not isinstance(cases, Mapping) or len(cases) != 1:
raise _Unreadable("exit 2 needs exactly one case summary")
[case] = cases.values()
if not isinstance(case, Mapping):
raise _Unreadable("case summary is not an object")
detail = {"expect": _expect_states(doc, run_ids)} if "expect" in doc else {}
blocked, failed = case.get("blocked"), case.get("failed")
if case.get("status") == HUB_CASE_BLOCKED and isinstance(blocked, Mapping):
code = f"{CASE_BLOCKED_PREFIX}{_token(blocked.get('code'))}"
elif case.get("status") == HUB_CASE_FAILED and isinstance(failed, Mapping):
code = f"{CASE_FAILED_PREFIX}{_token(failed.get('stage'), _STAGE)}"
else:
raise _Unreadable("exit 2 without a blocked or failed case")
return LegResult.failed(code, detail=detail)
def _judge_expect_failed(stdout: str, run_ids: Sequence[str]) -> LegResult:
states = _expect_states(_summary(stdout), run_ids)
bad = next((state for state in states.values() if state != EXPECT_OK), None)
if bad is None:
raise _Unreadable("exit 4 with every run ok")
return LegResult.failed(f"{RUN_NOT_OK_PREFIX}{bad}", detail={"expect": states})
def _summary(stdout: str) -> Mapping[str, Any]:
"""stdout ์ ์์ฝ JSON. ์์ ๋ค๋ฅธ ์ค์ด ์์์ผ๋ฉด `{` ๋ก ์์ํ๋ ์ฒซ ์ค๋ถํฐ ์ฝ๋๋ค."""
lines = stdout.splitlines(keepends=True)
start = next((i for i, line in enumerate(lines) if line.startswith("{")), None)
if start is None:
raise _Unreadable("no JSON object on stdout")
try:
doc = json.loads("".join(lines[start:]))
except json.JSONDecodeError as exc:
raise _Unreadable("summary is not JSON") from exc
if not isinstance(doc, Mapping):
raise _Unreadable("summary is not an object")
return doc
def _expect_states(doc: Mapping[str, Any], run_ids: Sequence[str]) -> dict[str, str]:
"""์์ฒญ ์์๋๋ก run โ ์ํ. ์์ฒญํ run ์ด ์๊ฑฐ๋ ๋ชจ๋ฅด๋ ์ํ๋ฉด ์ฝ์ ์ ์๋ค."""
expect = doc.get("expect")
runs = expect.get("runs") if isinstance(expect, Mapping) else None
if not isinstance(runs, Mapping):
raise _Unreadable("summary has no expect block")
states: dict[str, str] = {}
for run_id in run_ids:
entry = runs.get(run_id)
state = entry.get("state") if isinstance(entry, Mapping) else None
if state not in EXPECT_STATES:
raise _Unreadable("expect block misses a requested run")
states[run_id] = state
return states
def _token(value: Any, grammar: re.Pattern[str] = _TOKEN) -> str:
if not isinstance(value, str) or grammar.fullmatch(value) is None:
raise _Unreadable("summary token has an unexpected shape")
return value
# โโ ์คํ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def run_hub_leg(
target: Target,
run_ids: Sequence[str],
*,
confirm_local: bool,
child_env: Mapping[str, str] | None = None,
secrets: Sequence[str] = (),
) -> LegResult:
"""`hub ingest --case a/c --expect-run โฆ` ํ ๋ฒ. ์์ธ๋ hook ์ด `hub_exception:<Type>` ์ผ๋ก ๊ฐ๋ฑํ๋ค.
`child_env` ๋ ์ค์ ์ถ์ฒ opt-in ์ผ ๋๋ง(๋ถ๋ชจ ํ๊ฒฝ + URL), ์๋๋ฉด None(๋ถ๋ชจ ํ๊ฒฝ ๊ทธ๋๋ก). `secrets` ๋ ์ง๋จ ๊ฐ๋ฆผ ๋ชฉ๋ก.
"""
ids = _checked_run_ids(run_ids)
argv = build_hub_argv(target, ids, confirm_local=confirm_local, env=child_env)
cwd = _workspace()
if cwd is None:
return LegResult.skipped(NO_WORKSPACE)
try:
result = proc.run_pnpm(argv, cwd=cwd, env=child_env, timeout=HUB_INGEST_TIMEOUT_SECONDS)
except FileNotFoundError:
return LegResult.failed(PNPM_MISSING)
except subprocess.TimeoutExpired as exc:
_echo_diagnostics(exc.stderr, secrets)
return LegResult.failed(TIMEOUT)
leg = _with_run_hint(judge_hub(result, ids), target)
if leg.status == STATUS_FAILED:
_echo_diagnostics(result.stderr, secrets)
return leg
def diagnostic_lines(stderr: str | bytes | None, secrets: Sequence[str] = ()) -> list[str]:
"""hub CLI stderr โ ์ฝ์์ ์ฎ๊ธธ ๊ผฌ๋ฆฌ(๋ฆฌ๋ทฐ ์ง์ 6). `[hub]` ์ค์ด ์์ผ๋ฉด ๊ทธ ๋ง์ง๋ง ๋ช ์ค, ์์ผ๋ฉด ๋น์ง ์์ ๋ง์ง๋ง ๋ช ์ค.
์ค๋ง๋ค ๋น๋ฐ์ ๊ฐ๋ฆฐ ๋ค(`status.redact_secrets` โ ์๋ฅด๊ธฐ ์ ์ ๊ฐ๋ ค์ผ URL ์ด ์ค๊ฐ์ ์๋ ค ๋น๋ฐ๋ฒํธ๊ฐ ์์ง ์๋๋ค) ๊ธธ์ด๋ฅผ ์๋ฅธ๋ค.
`secrets` ๋ ํ๊ฒฝ ๋ณ์ ๋ฐ์ ๋น๋ฐ(์ค์ ์ถ์ฒ URL ๊ฐ). ์ํ ํ์ผยทdetail ์๋ ์ฃ์ง ์๋๋ค(D-L6-4 โ ์ฝ๋๋ง).
"""
if isinstance(stderr, bytes):
stderr = stderr.decode("utf-8", errors="replace")
lines = [line.rstrip() for line in (stderr or "").splitlines() if line.strip()]
hub_lines = [line for line in lines if line.startswith(_HUB_LOG_PREFIX)]
tail = (hub_lines or lines)[-HUB_DIAG_MAX_LINES:]
return [_clip(status.redact_secrets(line, secrets)) for line in tail]
def _clip(line: str) -> str:
return line if len(line) <= HUB_DIAG_MAX_CHARS else f"{line[:HUB_DIAG_MAX_CHARS]}โฆ"
def _echo_diagnostics(stderr: str | bytes | None, secrets: Sequence[str] = ()) -> None:
lines = diagnostic_lines(stderr, secrets)
if not lines:
return
click.secho(f" hub CLI stderr ๋ง์ง๋ง {len(lines)}์ค(๋น๋ฐ์ ๊ฐ๋ฆผ):", fg="red", err=True)
for line in lines:
click.secho(f" {line}", fg="red", err=True)
def _workspace():
# ๋ชจ๋ ์์ฑ์ผ๋ก ๋ถ๋ฅธ๋ค โ ์ํ์ด `target.workspace_root` ๋ฅผ ๋ฐ๊ฟ ์ ์๊ฒ.
return target.workspace_root()
def _with_run_hint(leg: LegResult, t: Target) -> LegResult:
"""`run_not_ok` ๋ฉด ๋์ด๋ฆฌ๊ธฐ์ ok ๊ฐ ์๋ ์ฒซ run ์ ์ ๋๋ค(batch ์์ ์ฝ์์ด `<run>` ์ผ๋ก ์ฐ์ง ์๊ฒ)."""
if not leg.code.startswith(RUN_NOT_OK_PREFIX):
return leg
states = leg.detail.get("expect") or {}
bad = next((run for run, state in states.items() if state != EXPECT_OK), None)
if bad is None:
return leg
hint = console.hub_recovery(leg.code, t.case_ref, bad)
return LegResult.failed(leg.code, hint, leg.detail)
def ingest_runs(
target: Target,
run_ids: Sequence[str],
*,
confirm_local: bool,
child_env: Mapping[str, str] | None = None,
secrets: Sequence[str] = (),
) -> LegResult:
"""L6b `runs eval` ์ ๊ฒฝ๋ก โฃ ๊ฐ ๋ถ๋ฅด๋ ์ ์ฌ โ ํ์ ยท์คํ์ `run_hub_leg` ์ ๊ฐ๋ค(๊ฒ์ดํธ๋ `hook.gated_hub_leg`)."""
return run_hub_leg(
target, run_ids, confirm_local=confirm_local, child_env=child_env, secrets=secrets
)
|