fix!: seal ConstantDrift's field so gamma can be validated
`gamma` enters only as `gamma * gamma`, so the sign was squared away: measured against the old public-field form, `ConstantDrift(-0.0833)` produced results bit identical to `ConstantDrift(0.0833)`. The sign was neither rejected nor honoured — it vanished. It could not be checked while the field was a public tuple position, because there was nothing to intercept. Validating inside `variance_for_elapsed` would have been worse: it runs in the sweep, so a construction-time mistake would panic mid-inference, and `Gaussian::from_ms` is a worked example of why that is the wrong place — rejecting NaN there turned the NonFiniteResult reporting path into a crash. So `ConstantDrift::new` is the only way in and it checks, with `gamma()` to read the value back. 129 call sites rewritten across src, tests, benches, examples and the README. The dated plan and spec documents under docs/superpowers are left alone: they record what was built at the time, and rewriting them would falsify that. tests/constructor_validation.rs is the more valuable half. This defect class was closed three times in one session and reopened twice, because each fix validated the layer it had just touched and inferred the rest — `HistoryBuilder`, then `Game`'s own entry points, then the constructors beneath both. A per-site fix cannot notice the site nobody thought of, so that file enumerates every public entry point taking a magnitude and asserts each refuses negative and non-finite values. It found an eleventh defect on its first run: `HistoryBuilder::score_sigma` accepted infinity, because `inf > 0.0` is true and the assert only tested positivity. Fixed, and its own `should_panic` message updated to match. `Gaussian::from_ms` is deliberately exempt from the non-finite half, for the reason above: a broken fit produces a NaN sigma legitimately and `converge` must be allowed to report it. The convergence-level drift-variance check stays and is now tested through a custom `Drift` implementation, since `ConstantDrift` can no longer reach it. That check is the only thing standing between a third-party `Drift` and a NaN fit. BREAKING CHANGE: `ConstantDrift`'s field is private. Replace `ConstantDrift(x)` with `ConstantDrift::new(x)`, and `drift().0` with `drift().gamma()`. `HistoryBuilder::score_sigma` now rejects infinity. Closes #65 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011hcFjNDmHXZF8URGLku5zZ
This commit is contained in:
+50
-17
@@ -22,7 +22,7 @@ fn rating() -> R {
|
||||
R::new(
|
||||
Gaussian::from_ms(25.0, 25.0 / 3.0),
|
||||
25.0 / 6.0,
|
||||
ConstantDrift(0.0),
|
||||
ConstantDrift::new(0.0),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -286,33 +286,66 @@ mod constructor_parameters {
|
||||
#[test]
|
||||
#[should_panic(expected = "beta must be finite and non-negative")]
|
||||
fn a_negative_beta_is_rejected_by_rating_new() {
|
||||
let _ = Rating::<i64, ConstantDrift>::new(Gaussian::default(), -4.17, ConstantDrift(0.0));
|
||||
let _ =
|
||||
Rating::<i64, ConstantDrift>::new(Gaussian::default(), -4.17, ConstantDrift::new(0.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "beta must be finite and non-negative")]
|
||||
fn a_nan_beta_is_rejected_by_rating_new() {
|
||||
let _ =
|
||||
Rating::<i64, ConstantDrift>::new(Gaussian::default(), f64::NAN, ConstantDrift(0.0));
|
||||
let _ = Rating::<i64, ConstantDrift>::new(
|
||||
Gaussian::default(),
|
||||
f64::NAN,
|
||||
ConstantDrift::new(0.0),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_zero_beta_is_accepted_by_rating_new() {
|
||||
let _ = Rating::<i64, ConstantDrift>::new(Gaussian::default(), 0.0, ConstantDrift(0.0));
|
||||
let _ =
|
||||
Rating::<i64, ConstantDrift>::new(Gaussian::default(), 0.0, ConstantDrift::new(0.0));
|
||||
}
|
||||
|
||||
/// `ConstantDrift` rejects at construction now that its field is private.
|
||||
#[test]
|
||||
#[should_panic(expected = "gamma must be finite and non-negative")]
|
||||
fn a_negative_gamma_is_rejected_by_constant_drift_new() {
|
||||
let _ = ConstantDrift::new(-0.0833);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "gamma must be finite and non-negative")]
|
||||
fn a_non_finite_gamma_is_rejected_by_constant_drift_new() {
|
||||
let _ = ConstantDrift::new(f64::NAN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gamma_reads_back_what_was_given() {
|
||||
assert_eq!(ConstantDrift::new(0.25).gamma(), 0.25);
|
||||
assert_eq!(ConstantDrift::new(0.0).gamma(), 0.0);
|
||||
}
|
||||
|
||||
/// `HistoryBuilder::drift` is generic and cannot inspect an arbitrary
|
||||
/// `Drift`, so the check is on the variance each competitor actually
|
||||
/// accumulates. That also covers a custom implementation.
|
||||
/// `Drift`, so the check on the variance each competitor accumulates is
|
||||
/// still needed — it is the only thing standing between a custom
|
||||
/// implementation and a NaN fit. `ConstantDrift` can no longer reach it,
|
||||
/// so this uses an implementation that can.
|
||||
#[test]
|
||||
fn a_non_finite_drift_is_rejected_at_convergence() {
|
||||
for gamma in [f64::NAN, f64::INFINITY] {
|
||||
let mut h = History::builder()
|
||||
.mu(25.0)
|
||||
.sigma(25.0 / 3.0)
|
||||
.beta(25.0 / 6.0)
|
||||
.drift(ConstantDrift(gamma))
|
||||
.build();
|
||||
fn a_custom_drift_returning_a_bad_variance_is_rejected_at_convergence() {
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct BadDrift(f64);
|
||||
|
||||
impl trueskill_tt::Drift<i64> for BadDrift {
|
||||
fn variance_delta(&self, _from: &i64, _to: &i64) -> f64 {
|
||||
self.0
|
||||
}
|
||||
fn variance_for_elapsed(&self, _elapsed: i64) -> f64 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
for bad in [f64::NAN, f64::INFINITY, -1.0] {
|
||||
let mut h = History::builder().drift(BadDrift(bad)).build();
|
||||
h.record_winner(&"a", &"b", 1).unwrap();
|
||||
h.record_winner(&"a", &"b", 5).unwrap();
|
||||
let err = h.converge().unwrap_err();
|
||||
@@ -324,7 +357,7 @@ mod constructor_parameters {
|
||||
..
|
||||
}
|
||||
),
|
||||
"gamma {gamma}: {err:?}"
|
||||
"drift {bad}: {err:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -333,7 +366,7 @@ mod constructor_parameters {
|
||||
#[test]
|
||||
fn an_ordinary_drift_still_converges() {
|
||||
let mut h = History::builder()
|
||||
.drift(ConstantDrift(25.0 / 300.0))
|
||||
.drift(ConstantDrift::new(25.0 / 300.0))
|
||||
.build();
|
||||
h.record_winner(&"a", &"b", 1).unwrap();
|
||||
h.record_winner(&"a", &"b", 5).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user