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
    )