From ce438718280ff726df84470f36a3355679f5e7c3 Mon Sep 17 00:00:00 2001 From: iomgaa Date: Tue, 7 Jul 2026 12:14:28 -0400 Subject: [PATCH] =?UTF-8?q?fix(harness):=20=E8=A1=A5=E5=85=85=20train=20?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=20run=5Fid=20=E6=A0=A1=E9=AA=8C=20+=20?= =?UTF-8?q?=E6=8B=86=E5=88=86=E5=87=BD=E6=95=B0=E4=BF=9D=E6=8C=81=20radon?= =?UTF-8?q?=20Grade=20B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - _validate_mode_deps: 恢复 train 非 resume/fresh 时必须提供 run_id 校验 - 提取 _validate_train_run_id 用 early return 展平条件,避免 radon Grade C - 合并 promote run_id 检查到 diagnose/evolve/promote 统一检查 - 新增 4 个测试:train+run_id / train+resume / train+fresh / train+baseline - radon cc -n C 无输出(全部 Grade B 或更好) - 74 个单元测试全部通过 Co-Authored-By: Claude Opus 4.6 (1M context) --- app/harness/config.py | 38 +++++++++++++++++++++++++++---- tests/unit/test_harness_config.py | 23 ++++++++++++++++++- 2 files changed, 55 insertions(+), 6 deletions(-) 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)