diff --git a/app/harness/config.py b/app/harness/config.py index a87d4b4..188d6b1 100644 --- a/app/harness/config.py +++ b/app/harness/config.py @@ -150,6 +150,7 @@ def _validate(config: RunConfig) -> None: ValueError: 任一字段值不合法。 """ _validate_mode(config) + _validate_mode_deps(config) _validate_basic(config) _validate_edit_budget(config) _validate_minibatch(config) @@ -157,22 +158,49 @@ def _validate(config: RunConfig) -> None: def _validate_mode(config: RunConfig) -> None: - """校验运行模式及其依赖字段(run_id、version)。 + """校验运行模式枚举合法性。 参数: config: 待校验的配置实例。 异常: - ValueError: mode 非法或模式依赖字段缺失。 + ValueError: mode 值不在合法集合中。 """ if config.mode not in _VALID_MODES: raise ValueError(f"mode 必须为 {_VALID_MODES} 之一,实际: {config.mode!r}") - if config.mode in ("diagnose", "evolve") and not config.run_id: + + +def _validate_mode_deps(config: RunConfig) -> None: + """校验各运行模式的依赖字段(run_id、version)。 + + 参数: + config: 待校验的配置实例。 + + 异常: + ValueError: 模式依赖字段缺失。 + """ + if config.mode in ("diagnose", "evolve", "promote") and not config.run_id: raise ValueError(f"mode 为 {config.mode!r} 时必须提供 run_id。") if config.mode in ("eval", "promote") and not config.version: raise ValueError(f"mode 为 {config.mode!r} 时必须提供 --version。") - if config.mode == "promote" and not config.run_id: - raise ValueError("promote 必须提供 --run-id(指定 canonical eval run)。") + _validate_train_run_id(config) + + +def _validate_train_run_id(config: RunConfig) -> None: + """校验 train 模式非 resume/fresh 时必须提供 run_id。 + + 参数: + config: 待校验的配置实例。 + + 异常: + ValueError: train 模式既非 resume 也非 fresh 且缺少 run_id。 + """ + if config.mode != "train": + return + if config.resume or config.fresh: + return + if not config.run_id: + raise ValueError("train 非 resume/fresh 时必须提供 run_id(旧式基线 run)。") def _validate_basic(config: RunConfig) -> None: diff --git a/tests/unit/test_harness_config.py b/tests/unit/test_harness_config.py index b2f15b7..2d78c76 100644 --- a/tests/unit/test_harness_config.py +++ b/tests/unit/test_harness_config.py @@ -139,7 +139,7 @@ class TestModeValidation: def test_all_valid_modes_accepted(self, valid_mode: str) -> None: """全部合法 mode 应通过校验(diagnose/evolve 需 run_id)。""" overrides: dict = {"mode": valid_mode} - if valid_mode in ("diagnose", "evolve"): + if valid_mode in ("diagnose", "evolve", "train"): overrides["run_id"] = "run-001" if valid_mode in ("eval", "promote"): overrides["version"] = "v1" @@ -172,6 +172,27 @@ class TestModeValidation: with pytest.raises(ValueError, match="run.id"): _validate(cfg) + def test_train_mode_requires_run_id_without_resume_fresh(self) -> None: + """train 模式非 resume/fresh 时必须提供 run_id。""" + cfg = _make_config(mode="train", run_id="", resume=False, fresh=False) + with pytest.raises(ValueError, match="run_id"): + _validate(cfg) + + def test_train_mode_resume_without_run_id_accepted(self) -> None: + """train 模式 resume=True 时不需要 run_id。""" + cfg = _make_config(mode="train", run_id="", resume=True) + _validate(cfg) + + def test_train_mode_fresh_without_run_id_accepted(self) -> None: + """train 模式 fresh=True 时不需要 run_id。""" + cfg = _make_config(mode="train", run_id="", fresh=True) + _validate(cfg) + + def test_train_mode_with_run_id_accepted(self) -> None: + """train 模式提供 run_id 时应通过(旧式基线 run)。""" + cfg = _make_config(mode="train", run_id="baseline-001") + _validate(cfg) + def test_concurrency_positive(self) -> None: """concurrency <= 0 应报错。""" cfg = _make_config(concurrency=0)