Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ include_dir = "0.7.4"
owo-colors = "4.4.0"
serde = { version = "1.0.229", features = ["derive"] }
serde_json = "1.0.151"
# Validates solver.toml with the runtime's own schema; pinned to RUNTIME_CRATE_VERSION.
solverforge-config = "=0.31.2"
strsim = "0.11.1"
quote = "1.0.47"
syn = { version = "3.0.6", features = ["full", "parsing", "visit"] }
Expand Down
17 changes: 0 additions & 17 deletions src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub fn run_set(key: &str, value: &str) -> CliResult {
})?;

let segments = parse_key_segments(key)?;
validate_known_setting(key, value)?;
if segments
.first()
.is_some_and(|segment| segment.name == "phases")
Expand Down Expand Up @@ -75,22 +74,6 @@ pub fn run_set(key: &str, value: &str) -> CliResult {
Ok(())
}

fn validate_known_setting(key: &str, raw_value: &str) -> CliResult {
if key != "candidate_trace.max_entries" {
return Ok(());
}

let max_entries = raw_value
.parse::<u64>()
.map_err(|_| CliError::general("candidate_trace.max_entries must be a positive integer"))?;
if max_entries == 0 || max_entries > i64::MAX as u64 {
return Err(CliError::general(
"candidate_trace.max_entries must be a positive TOML integer",
));
}
Ok(())
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct KeySegment {
name: String,
Expand Down
29 changes: 13 additions & 16 deletions src/commands/config_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,8 @@ fn run_set_updates_solver_toml_with_typed_nested_value() {
let tmp = tempdir().expect("failed to create temp dir");
let original_dir = std::env::current_dir().expect("failed to read current dir");
std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
fs::write(
"solver.toml",
"[termination]\nscore_calculation_count_limit = 10\n",
)
.expect("failed to write solver.toml");
fs::write("solver.toml", "[termination]\nstep_count_limit = 10\n")
.expect("failed to write solver.toml");

run_set("termination.seconds_spent_limit", "60").expect("run_set should succeed");

Expand All @@ -139,7 +136,7 @@ fn run_set_updates_solver_toml_with_typed_nested_value() {
toml::Value::Integer(60)
);
assert_eq!(
saved_doc["termination"]["score_calculation_count_limit"],
saved_doc["termination"]["step_count_limit"],
toml::Value::Integer(10)
);
}
Expand All @@ -152,7 +149,7 @@ fn run_set_updates_nested_value_in_inline_table() {
std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
fs::write(
"solver.toml",
"termination = { seconds_spent_limit = 30, score_calculation_count_limit = 10 }\n",
"termination = { seconds_spent_limit = 30, step_count_limit = 10 }\n",
)
.expect("failed to write solver.toml");

Expand All @@ -167,7 +164,7 @@ fn run_set_updates_nested_value_in_inline_table() {
toml::Value::Integer(60)
);
assert_eq!(
saved_doc["termination"]["score_calculation_count_limit"],
saved_doc["termination"]["step_count_limit"],
toml::Value::Integer(10)
);
}
Expand Down Expand Up @@ -209,7 +206,7 @@ fn run_set_rejects_zero_candidate_trace_capacity_without_writing() {
assert_eq!(saved, original);
assert!(error
.to_string()
.contains("candidate_trace.max_entries must be a positive TOML integer"));
.contains("solver.toml does not load in the SolverForge runtime"));
}

#[test]
Expand All @@ -218,19 +215,19 @@ fn run_set_stores_string_values_as_strings() {
let tmp = tempdir().expect("failed to create temp dir");
let original_dir = std::env::current_dir().expect("failed to read current dir");
std::env::set_current_dir(tmp.path()).expect("failed to enter temp dir");
fs::write("solver.toml", "[phase]\nenabled = true\n").expect("failed to write solver.toml");
fs::write("solver.toml", "random_seed = 7\n").expect("failed to write solver.toml");

run_set("phase.name", "construction").expect("run_set should succeed");
run_set("environment_mode", "full_assert").expect("run_set should succeed");

let saved = fs::read_to_string("solver.toml").expect("failed to read solver.toml");
std::env::set_current_dir(original_dir).expect("failed to restore current dir");

let saved_doc: toml::Value = toml::from_str(&saved).expect("saved toml should be valid");
assert_eq!(
saved_doc["phase"]["name"],
toml::Value::String("construction".to_string())
saved_doc["environment_mode"],
toml::Value::String("full_assert".to_string())
);
assert_eq!(saved_doc["phase"]["enabled"], toml::Value::Boolean(true));
assert_eq!(saved_doc["random_seed"], toml::Value::Integer(7));
}

#[test]
Expand Down Expand Up @@ -295,7 +292,7 @@ fn run_set_preserves_managed_solver_config_blocks_for_non_phase_keys() {
"solver.toml",
r#"
[termination]
score_calculation_count_limit = 10
step_count_limit = 10

# @solverforge:begin solver-config
# @solverforge:owner scalar-group required_assignment construction
Expand Down Expand Up @@ -359,7 +356,7 @@ type = "late_acceptance"
late_acceptance_size = 400

[termination]
score_calculation_count_limit = 10
step_count_limit = 10
"#,
)
.expect("failed to write solver.toml");
Expand Down
91 changes: 43 additions & 48 deletions src/solver_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,54 +382,18 @@ pub(crate) fn validate_managed_blocks(raw: &str) -> CliResult {
parse_managed_region(raw).map(|_| ())
}

pub(crate) fn validate_current_settings(raw: &str) -> CliResult {
let value: toml::Value = toml::from_str(raw)
.map_err(|err| CliError::general(format!("failed to parse solver.toml: {err}")))?;
validate_move_thread_count(&value)?;
validate_candidate_trace(&value)
}

// The generated app embeds solver.toml and parses it on the first solve, so a
// malformed thread count builds cleanly and then panics the job. Accept exactly
// the runtime's forms: "auto", "none", or { count = N }.
fn validate_move_thread_count(value: &toml::Value) -> CliResult {
let Some(move_thread_count) = value.get("move_thread_count") else {
return Ok(());
};
let invalid = || {
CliError::general(
"solver.toml move_thread_count must be \"auto\", \"none\", or { count = N } with N a positive integer",
)
};
match move_thread_count {
toml::Value::String(mode) if mode == "auto" || mode == "none" => Ok(()),
toml::Value::Table(table) if table.len() == 1 => match table.get("count") {
Some(toml::Value::Integer(count)) if *count >= 1 => Ok(()),
_ => Err(invalid()),
},
_ => Err(invalid()),
}
}

fn validate_candidate_trace(value: &toml::Value) -> CliResult {
let Some(candidate_trace) = value.get("candidate_trace") else {
return Ok(());
// file the runtime refuses builds cleanly and then fails every job. Parse it
// with the runtime's own configuration type, pinned to the scaffold target.
pub(crate) fn validate_current_settings(raw: &str) -> CliResult {
let err = match solverforge_config::SolverConfig::from_toml_str(raw) {
Ok(_) => return Ok(()),
Err(solverforge_config::ConfigError::Toml(err)) => err.to_string(),
Err(err) => err.to_string(),
};
let table = candidate_trace
.as_table()
.ok_or_else(|| CliError::general("solver.toml candidate_trace must be a TOML table"))?;
let max_entries = table
.get("max_entries")
.and_then(toml::Value::as_integer)
.ok_or_else(|| {
CliError::general("solver.toml candidate_trace.max_entries must be a positive integer")
})?;
if max_entries <= 0 {
return Err(CliError::general(
"solver.toml candidate_trace.max_entries must be a positive integer",
));
}
Ok(())
Err(CliError::general(format!(
"solver.toml does not load in the SolverForge runtime: {err}"
)))
}

fn parse_managed_region(raw: &str) -> CliResult<Option<ManagedRegion>> {
Expand Down Expand Up @@ -1642,7 +1606,11 @@ group_name = "other"
"candidate_trace = \"enabled\"\n",
] {
let err = validate_current_settings(raw).expect_err("invalid trace config should fail");
assert!(err.to_string().contains("candidate_trace"));
assert!(
err.to_string()
.contains("does not load in the SolverForge runtime"),
"{raw}"
);
}
}

Expand Down Expand Up @@ -1671,7 +1639,34 @@ group_name = "other"
] {
let err = validate_current_settings(raw)
.expect_err("malformed thread count should fail validation");
assert!(err.to_string().contains("move_thread_count"), "{raw}");
assert!(
err.to_string()
.contains("does not load in the SolverForge runtime"),
"{raw}"
);
}
}

#[test]
fn config_schema_pin_matches_the_scaffold_runtime_target() {
let pin = format!(
"solverforge-config = \"={}\"",
crate::scaffold_target::RUNTIME_CRATE_VERSION
);
assert!(
include_str!("../Cargo.toml").contains(&pin),
"missing {pin}"
);
}

#[test]
fn keys_the_runtime_does_not_know_are_rejected() {
for raw in [
"[app]\nname = \"demo\"\n",
"[termination]\nseconds_spent_limit = 30\nseconds = 30\n",
] {
let err = validate_current_settings(raw).expect_err("unknown key should fail");
assert!(err.to_string().contains("unknown field"), "{raw}: {err}");
}
}

Expand Down
Loading