Skip to content

Commit 10dbd96

Browse files
committed
fix(examples): harden eval optimize audit lifecycle
1 parent 4563b0c commit 10dbd96

9 files changed

Lines changed: 2049 additions & 305 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Cross-platform validation for values used as artifact path components."""
2+
3+
from __future__ import annotations
4+
5+
import re
6+
from typing import Any
7+
8+
9+
ARTIFACT_COMPONENT_RE = re.compile(r"^[A-Za-z0-9_.-]+$")
10+
WINDOWS_RESERVED_NAMES = {
11+
"CON",
12+
"PRN",
13+
"AUX",
14+
"NUL",
15+
*(f"COM{index}" for index in range(1, 10)),
16+
*(f"LPT{index}" for index in range(1, 10)),
17+
}
18+
MAX_ARTIFACT_COMPONENT_LENGTH = 128
19+
20+
21+
def validate_artifact_component(value: Any, *, context: str) -> str:
22+
"""Return a portable artifact component or fail before filesystem access."""
23+
24+
if (
25+
not isinstance(value, str)
26+
or value in {"", ".", ".."}
27+
or len(value) > MAX_ARTIFACT_COMPONENT_LENGTH
28+
or value.endswith((".", " "))
29+
or not ARTIFACT_COMPONENT_RE.fullmatch(value)
30+
):
31+
raise ValueError(f"unsafe {context}: {value!r}")
32+
windows_stem = value.split(".", 1)[0].upper()
33+
if windows_stem in WINDOWS_RESERVED_NAMES:
34+
raise ValueError(f"unsafe {context}: {value!r} is reserved on Windows")
35+
return value

examples/optimization/eval_optimize_loop/eval_loop/config.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ def parse_optimizer_config(payload: dict[str, Any], *, path: str | Path) -> Opti
5656
allowed = {"seed", "optimizer", "metrics", "gate"}
5757
extras = {key: value for key, value in payload.items() if key not in allowed}
5858

59-
seed = payload.get("seed", 91)
60-
if not isinstance(seed, int):
61-
raise ValueError(f"{path_text}: field 'seed' must be an integer")
59+
seed = resolve_effective_seed(payload, path=path)
6260

6361
optimizer = payload.get("optimizer", {})
6462
if not isinstance(optimizer, dict):
@@ -82,6 +80,58 @@ def parse_optimizer_config(payload: dict[str, Any], *, path: str | Path) -> Opti
8280
)
8381

8482

83+
def resolve_effective_seed(
84+
payload: dict[str, Any],
85+
*,
86+
path: str | Path,
87+
default: int = 91,
88+
strict_legacy: bool = True,
89+
) -> int:
90+
"""Resolve the legacy or official optimizer seed without audit drift.
91+
92+
The official SDK schema owns ``optimize.algorithm.seed``. A non-integer
93+
top-level ``seed`` may be unrelated SDK metadata and is ignored when the
94+
official nested seed is present. Two integer seed declarations must agree.
95+
"""
96+
97+
path_text = str(path)
98+
nested_present = False
99+
nested_seed: Any = None
100+
optimize = payload.get("optimize")
101+
if isinstance(optimize, dict):
102+
algorithm = optimize.get("algorithm")
103+
if isinstance(algorithm, dict) and "seed" in algorithm:
104+
nested_present = True
105+
nested_seed = algorithm["seed"]
106+
107+
top_present = "seed" in payload
108+
top_seed = payload.get("seed")
109+
if nested_present:
110+
nested = _validated_seed(
111+
nested_seed,
112+
field_name=f"{path_text}: field 'optimize.algorithm.seed'",
113+
)
114+
if top_present and isinstance(top_seed, int) and not isinstance(top_seed, bool):
115+
if top_seed != nested:
116+
raise ValueError(
117+
f"{path_text}: conflicting seed values: top-level seed={top_seed}, "
118+
f"optimize.algorithm.seed={nested}"
119+
)
120+
return nested
121+
122+
if not top_present:
123+
return default
124+
if not strict_legacy and (isinstance(top_seed, bool) or not isinstance(top_seed, int)):
125+
return default
126+
return _validated_seed(top_seed, field_name=f"{path_text}: field 'seed'")
127+
128+
129+
def _validated_seed(value: Any, *, field_name: str) -> int:
130+
if isinstance(value, bool) or not isinstance(value, int):
131+
raise ValueError(f"{field_name} must be an integer")
132+
return value
133+
134+
85135
def validate_inputs(
86136
*,
87137
train_path: str | Path,

0 commit comments

Comments
 (0)