From 91362a48ad713332537affa11fb0f66b2dc092a2 Mon Sep 17 00:00:00 2001 From: Vittorio Distefano Date: Thu, 5 Nov 2026 03:11:04 +0100 Subject: [PATCH 1/2] fix(construction)!: reject scalar-only keys on list construction heuristics `value_candidate_limit`, `group_candidate_limit` and `construction_obligation` shape scalar candidate generation, and list heuristics read none of them, so setting one on `list_round_robin`, `list_cheapest_insertion`, `list_regret_insertion`, `list_clarke_wright` or `list_k_opt` did nothing. Each is now a configuration error naming its key path; the default `construction_obligation` stays accepted. BREAKING CHANGE: a list construction heuristic with `value_candidate_limit`, `group_candidate_limit` or a non-default `construction_obligation` is a configuration error. --- crates/solverforge-config/WIREFRAME.md | 6 +-- crates/solverforge-config/src/phase.rs | 7 +++- .../src/runtime/compiler/selector_values.rs | 33 ++++++++++++++++- .../compiler/tests/invalid_selector_values.rs | 37 +++++++++++++++++++ 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/crates/solverforge-config/WIREFRAME.md b/crates/solverforge-config/WIREFRAME.md index 691d0ecb..05c1e5c2 100644 --- a/crates/solverforge-config/WIREFRAME.md +++ b/crates/solverforge-config/WIREFRAME.md @@ -136,12 +136,12 @@ Derives: `Debug, Clone, Default, Deserialize, Serialize`. | Field | Type | Default | |-------|------|---------| | `construction_heuristic_type` | `ConstructionHeuristicType` | `FirstFit` | -| `construction_obligation` | `ConstructionObligation` | `PreserveUnassigned` | +| `construction_obligation` | `ConstructionObligation` | `PreserveUnassigned`; scalar heuristics only, list heuristics reject any other value | | `target` | `VariableTargetConfig` | empty target | | `k` | `usize` | `2`; valid: `2..=5` for `ListKOpt`, which polishes each route with k-opt; other heuristics do not use it and reject any value but `2` | -| `value_candidate_limit` | `Option` | `None`; valid: `>= 1` when set | +| `value_candidate_limit` | `Option` | `None`; valid: `>= 1` when set; scalar heuristics only, list heuristics reject it | | `group_name` | `Option` | `None` | -| `group_candidate_limit` | `Option` | `None`; valid: `>= 1` when set | +| `group_candidate_limit` | `Option` | `None`; valid: `>= 1` when set; list heuristics reject it | | `termination` | `Option` | `None` | `target` is flattened in serde, so configuration files still use top-level diff --git a/crates/solverforge-config/src/phase.rs b/crates/solverforge-config/src/phase.rs index a9fa4021..372e440b 100644 --- a/crates/solverforge-config/src/phase.rs +++ b/crates/solverforge-config/src/phase.rs @@ -36,7 +36,8 @@ pub struct ConstructionHeuristicConfig { pub construction_heuristic_type: ConstructionHeuristicType, /// Whether nullable scalar variables may keep their current unassigned value during - /// construction, or must receive a candidate when one exists. + /// construction, or must receive a candidate when one exists. Scalar + /// heuristics only; a list heuristic rejects any value but the default. #[serde(default)] pub construction_obligation: ConstructionObligation, @@ -49,13 +50,15 @@ pub struct ConstructionHeuristicConfig { #[serde(default = "default_k")] pub k: usize, - /// Optional cap for scalar value candidates generated per entity. + /// Optional cap for scalar value candidates generated per entity. Scalar + /// heuristics only; a list heuristic rejects it. pub value_candidate_limit: Option, /// Optional named scalar group for atomic grouped scalar construction. pub group_name: Option, /// Optional cap for grouped scalar candidates generated per provider call. + /// A list heuristic rejects it. pub group_candidate_limit: Option, /// Phase termination configuration. diff --git a/crates/solverforge-solver/src/runtime/compiler/selector_values.rs b/crates/solverforge-solver/src/runtime/compiler/selector_values.rs index 1addbde2..ba63e1c4 100644 --- a/crates/solverforge-solver/src/runtime/compiler/selector_values.rs +++ b/crates/solverforge-solver/src/runtime/compiler/selector_values.rs @@ -6,12 +6,13 @@ //! rejected with its key path before any phase runs. use solverforge_config::{ - ConstructionHeuristicConfig, ConstructionHeuristicType, KOptMoveSelectorConfig, - ListPermuteMoveConfig, MoveSelectorConfig, PhaseConfig, SolverConfig, + ConstructionHeuristicConfig, ConstructionHeuristicType, ConstructionObligation, + KOptMoveSelectorConfig, ListPermuteMoveConfig, MoveSelectorConfig, PhaseConfig, SolverConfig, }; use crate::heuristic::r#move::MAX_LIST_PERMUTE_WINDOW_SIZE; +use super::construction::is_list_construction; use super::types::{RuntimeCompileError, RuntimeCompileErrorKind}; /// Largest k the k-opt reconnection kernel enumerates. @@ -74,10 +75,38 @@ fn validate_construction( ), )); } + if is_list_construction(config.construction_heuristic_type) { + reject_scalar_only_construction_keys(config, path)?; + } positive_limit(path, "value_candidate_limit", config.value_candidate_limit)?; positive_limit(path, "group_candidate_limit", config.group_candidate_limit) } +/// List heuristics place list elements; the keys that shape scalar candidate +/// generation have nothing to apply to there. +fn reject_scalar_only_construction_keys( + config: &ConstructionHeuristicConfig, + path: &str, +) -> Result<(), RuntimeCompileError> { + let scalar_only = |field: &str| { + invalid( + path, + field, + "applies only to scalar construction heuristics, not to list construction".to_string(), + ) + }; + if config.value_candidate_limit.is_some() { + return Err(scalar_only("value_candidate_limit")); + } + if config.group_candidate_limit.is_some() { + return Err(scalar_only("group_candidate_limit")); + } + if config.construction_obligation != ConstructionObligation::default() { + return Err(scalar_only("construction_obligation")); + } + Ok(()) +} + fn validate_selector(config: &MoveSelectorConfig, path: &str) -> Result<(), RuntimeCompileError> { match config { MoveSelectorConfig::ChangeMoveSelector(config) => { diff --git a/crates/solverforge-solver/src/runtime/compiler/tests/invalid_selector_values.rs b/crates/solverforge-solver/src/runtime/compiler/tests/invalid_selector_values.rs index 37ec0261..9d73280d 100644 --- a/crates/solverforge-solver/src/runtime/compiler/tests/invalid_selector_values.rs +++ b/crates/solverforge-solver/src/runtime/compiler/tests/invalid_selector_values.rs @@ -272,3 +272,40 @@ fn runtime_graph_compilation_rejects_selector_values_before_any_phase() { "{error}" ); } + +const LIST_HEURISTICS: [&str; 5] = [ + "list_round_robin", + "list_cheapest_insertion", + "list_regret_insertion", + "list_clarke_wright", + "list_k_opt", +]; + +#[test] +fn list_construction_rejects_scalar_only_keys() { + for heuristic in LIST_HEURISTICS { + let list = |field: &str| { + construction(&format!( + "construction_heuristic_type = \"{heuristic}\"\n{field}" + )) + }; + assert_valid(&list(""), heuristic); + for (field, value) in [ + ("value_candidate_limit", "3"), + ("group_candidate_limit", "3"), + ( + "construction_obligation", + "\"assign_when_candidate_exists\"", + ), + ] { + assert_invalid( + &list(&format!("{field} = {value}")), + &format!("phases[0].construction.{field}"), + ); + } + assert_valid( + &list("construction_obligation = \"preserve_unassigned\""), + "the default obligation", + ); + } +} From 62388feae69eb9f01a32db6305efd6bdeda35463 Mon Sep 17 00:00:00 2001 From: Vittorio Distefano Date: Thu, 5 Nov 2026 03:11:04 +0100 Subject: [PATCH 2/2] fix(construction)!: require group_name for group_candidate_limit `group_candidate_limit` caps grouped scalar candidates, which exist only with `group_name`; without one the cap did nothing. It is now a configuration error naming its key path. Closes #193. BREAKING CHANGE: `group_candidate_limit` without `group_name` is a configuration error. --- crates/solverforge-config/WIREFRAME.md | 2 +- crates/solverforge-config/src/phase.rs | 2 +- .../src/runtime/compiler/selector_values.rs | 7 +++++++ .../compiler/tests/invalid_selector_values.rs | 16 ++++++++++++++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/crates/solverforge-config/WIREFRAME.md b/crates/solverforge-config/WIREFRAME.md index 05c1e5c2..0108aed4 100644 --- a/crates/solverforge-config/WIREFRAME.md +++ b/crates/solverforge-config/WIREFRAME.md @@ -141,7 +141,7 @@ Derives: `Debug, Clone, Default, Deserialize, Serialize`. | `k` | `usize` | `2`; valid: `2..=5` for `ListKOpt`, which polishes each route with k-opt; other heuristics do not use it and reject any value but `2` | | `value_candidate_limit` | `Option` | `None`; valid: `>= 1` when set; scalar heuristics only, list heuristics reject it | | `group_name` | `Option` | `None` | -| `group_candidate_limit` | `Option` | `None`; valid: `>= 1` when set; list heuristics reject it | +| `group_candidate_limit` | `Option` | `None`; valid: `>= 1` when set; requires `group_name`; list heuristics reject it | | `termination` | `Option` | `None` | `target` is flattened in serde, so configuration files still use top-level diff --git a/crates/solverforge-config/src/phase.rs b/crates/solverforge-config/src/phase.rs index 372e440b..50704344 100644 --- a/crates/solverforge-config/src/phase.rs +++ b/crates/solverforge-config/src/phase.rs @@ -58,7 +58,7 @@ pub struct ConstructionHeuristicConfig { pub group_name: Option, /// Optional cap for grouped scalar candidates generated per provider call. - /// A list heuristic rejects it. + /// Requires `group_name`; a list heuristic rejects it. pub group_candidate_limit: Option, /// Phase termination configuration. diff --git a/crates/solverforge-solver/src/runtime/compiler/selector_values.rs b/crates/solverforge-solver/src/runtime/compiler/selector_values.rs index ba63e1c4..584ec7fc 100644 --- a/crates/solverforge-solver/src/runtime/compiler/selector_values.rs +++ b/crates/solverforge-solver/src/runtime/compiler/selector_values.rs @@ -78,6 +78,13 @@ fn validate_construction( if is_list_construction(config.construction_heuristic_type) { reject_scalar_only_construction_keys(config, path)?; } + if config.group_candidate_limit.is_some() && config.group_name.is_none() { + return Err(invalid( + path, + "group_candidate_limit", + "caps grouped scalar candidates and needs `group_name`".to_string(), + )); + } positive_limit(path, "value_candidate_limit", config.value_candidate_limit)?; positive_limit(path, "group_candidate_limit", config.group_candidate_limit) } diff --git a/crates/solverforge-solver/src/runtime/compiler/tests/invalid_selector_values.rs b/crates/solverforge-solver/src/runtime/compiler/tests/invalid_selector_values.rs index 9d73280d..dcc07205 100644 --- a/crates/solverforge-solver/src/runtime/compiler/tests/invalid_selector_values.rs +++ b/crates/solverforge-solver/src/runtime/compiler/tests/invalid_selector_values.rs @@ -203,8 +203,8 @@ fn construction_values_reject_their_invalid_value_with_the_key_path() { "value_candidate_limit", ), ( - "group_candidate_limit = 0", - "group_candidate_limit = 1", + "group_name = \"g\"\ngroup_candidate_limit = 0", + "group_name = \"g\"\ngroup_candidate_limit = 1", "group_candidate_limit", ), ("k = 3", "k = 2", "k"), @@ -309,3 +309,15 @@ fn list_construction_rejects_scalar_only_keys() { ); } } + +#[test] +fn group_candidate_limit_needs_a_group_name() { + assert_invalid( + &construction("group_candidate_limit = 3"), + "phases[0].construction.group_candidate_limit", + ); + assert_valid( + &construction("group_name = \"g\"\ngroup_candidate_limit = 3"), + "with group_name", + ); +}