From a6dd52774a614da44c65a9e44d6b1e5c91d555aa Mon Sep 17 00:00:00 2001 From: Weksa Date: Tue, 10 Nov 2026 01:56:29 +0000 Subject: [PATCH] Checkpoint and TestReadings: Test is read only at a declared checkpoint, scalars only Co-Authored-By: Claude Sonnet 5.5 --- crates/weksa/src/score/mod.rs | 88 ++++++++++++++++++++++++++++++++++- crates/weksa/src/split.rs | 42 +++++++++++++++++ crates/weksa/tests/score.rs | 75 ++++++++++++++++++++++++++++- 3 files changed, 203 insertions(+), 2 deletions(-) diff --git a/crates/weksa/src/score/mod.rs b/crates/weksa/src/score/mod.rs index abf20f9..a5556de 100644 --- a/crates/weksa/src/score/mod.rs +++ b/crates/weksa/src/score/mod.rs @@ -18,7 +18,7 @@ pub use diagnostics::{OrderCoverage, PairChrf, SplitLeak, WorstItem, leak_diagno pub use embed::{Embedder, OllamaEmbedder, cosine}; use crate::corpus::CorpusItem; -use crate::split::{Split, SplitScheme}; +use crate::split::{Checkpoint, Split, SplitScheme}; /// One reference/output pair; both are scored whole. pub struct ScoreInput<'a> { @@ -154,3 +154,89 @@ pub fn aggregate(scored: &[(&CorpusItem, ItemScore)]) -> Aggregate { validation: split_mean(scored, Split::Validation), } } + +/// The Test split's readings, scalars only: no item key, no minted string, no +/// pair. The checkpoint decision (convergence-test) needs numbers; a key beside +/// a minted string is the handle an edit step would act on. The only producer +/// is `at_checkpoint`, which consumes a `Checkpoint`. +/// +/// ``` +/// use weksa::score::TestReadings; +/// use weksa::split::Checkpoint; +/// fn s(_: &T) {} +/// let t = TestReadings::at_checkpoint(Checkpoint::declare(1), &[], &[], &[]); +/// assert_eq!(t.count(), 0); +/// s(&0u8); +/// ``` +/// +/// ```compile_fail +/// use weksa::score::TestReadings; +/// use weksa::split::Checkpoint; +/// let t = TestReadings::at_checkpoint(Checkpoint::declare(1), &[], &[], &[]); +/// let _u = t.clone(); +/// ``` +/// +/// ```compile_fail +/// use weksa::score::TestReadings; +/// use weksa::split::Checkpoint; +/// let t = TestReadings::at_checkpoint(Checkpoint::declare(1), &[], &[], &[]); +/// let _ = format!("{t:?}"); +/// ``` +/// +/// ```compile_fail +/// use weksa::score::TestReadings; +/// use weksa::split::Checkpoint; +/// fn s(_: &T) {} +/// let t = TestReadings::at_checkpoint(Checkpoint::declare(1), &[], &[], &[]); +/// s(&t); +/// ``` +pub struct TestReadings { + mean: SplitMean, + leak: SplitLeak, +} + +impl TestReadings { + /// Reads the live Test items against the scores and the minted strings, + /// by the same `split_leak` method the Validation reading uses. The + /// checkpoint is spent. + pub fn at_checkpoint( + checkpoint: Checkpoint, + scored: &[(&CorpusItem, ItemScore)], + minted: &[&str], + items: &[CorpusItem], + ) -> TestReadings { + let _spent = checkpoint; + TestReadings { + mean: split_mean(scored, Split::Test), + leak: diagnostics::held_out(minted, items, Split::Test), + } + } + + /// Scored Test items. + pub fn count(&self) -> usize { + self.mean.count + } + + pub fn chrf(&self) -> Option { + self.mean.chrf + } + + pub fn meaning(&self) -> Option { + self.mean.meaning + } + + /// Test items in the leak reading. + pub fn items(&self) -> usize { + self.leak.items + } + + /// The highest chrF of any minted string against any Test item; the score + /// only, never the pair. + pub fn max_chrf(&self) -> Option { + self.leak.max_chrf.as_ref().map(|p| p.chrf) + } + + pub fn coverage_by_n(&self) -> &[OrderCoverage] { + &self.leak.coverage_by_n + } +} diff --git a/crates/weksa/src/split.rs b/crates/weksa/src/split.rs index d325de5..37e4006 100644 --- a/crates/weksa/src/split.rs +++ b/crates/weksa/src/split.rs @@ -62,3 +62,45 @@ impl SplitScheme { } } } + +/// Permission to read Test once. The loop declares one at a convergence +/// checkpoint (convergence-test); `TestReadings::at_checkpoint` consumes it, so +/// one declaration buys one Test reading, and `EncoderOffer::for_checkpoint` +/// borrows it first. +/// +/// ``` +/// use weksa::score::TestReadings; +/// use weksa::split::Checkpoint; +/// let c = Checkpoint::declare(3); +/// assert_eq!(c.round(), 3); +/// let _ = TestReadings::at_checkpoint(c, &[], &[], &[]); +/// ``` +/// +/// One declaration, one reading: +/// +/// ```compile_fail +/// use weksa::score::TestReadings; +/// use weksa::split::Checkpoint; +/// let c = Checkpoint::declare(3); +/// let _ = TestReadings::at_checkpoint(c, &[], &[], &[]); +/// let _ = TestReadings::at_checkpoint(c, &[], &[], &[]); +/// ``` +/// +/// ```compile_fail +/// use weksa::split::Checkpoint; +/// let c = Checkpoint::declare(3); +/// let _again = c.clone(); +/// ``` +pub struct Checkpoint { + round: u32, +} + +impl Checkpoint { + pub fn declare(round: u32) -> Self { + Self { round } + } + + pub fn round(&self) -> u32 { + self.round + } +} diff --git a/crates/weksa/tests/score.rs b/crates/weksa/tests/score.rs index 24a4274..32e9bfa 100644 --- a/crates/weksa/tests/score.rs +++ b/crates/weksa/tests/score.rs @@ -10,9 +10,10 @@ use common::{cast, group_in_bucket, item_in, model}; use serde_json::Value; use weksa::CorpusItem; use weksa::score::{ - Embedder, ItemScore, OllamaEmbedder, ScoreInput, Scorer, aggregate, chrf, cosine, + Embedder, ItemScore, OllamaEmbedder, ScoreInput, Scorer, TestReadings, aggregate, chrf, cosine, leak_diagnostic, }; +use weksa::split::Checkpoint; /// (reference, hypothesis, sacrebleu 2.4.3 sentence_chrf), map 491536c. const VECTORS: [(&str, &str, f64); 6] = [ @@ -875,3 +876,75 @@ fn leak_diagnostic_reads_validation_only() { let on_validation = leak_diagnostic(&["gardening is quiet work"], &items); assert!(on_validation.max_chrf.unwrap().chrf > 99.0); } + +#[test] +fn test_readings_at_checkpoint() { + let train_item = train("item:t:1", UNRELATED); + let test = item_in("item:c:1", HELD_OUT, &group_in_bucket(90), cast()); + let test_model = item_in( + "item:c:2", + "model words here", + &group_in_bucket(90), + model(), + ); + let mut gone = item_in("item:c:3", "withdrawn words", &group_in_bucket(90), cast()); + gone.withdrawn = true; + let val = validation("item:v:1", "gardening is quiet work"); + let items = [ + train_item.clone(), + test.clone(), + test_model.clone(), + gone.clone(), + val.clone(), + ]; + let scored = [ + (&train_item, score(10.0, Some(0.5))), + (&val, score(40.0, Some(0.8))), + (&test, score(60.0, Some(0.6))), + (&test_model, score(100.0, Some(1.0))), + (&gone, score(90.0, Some(0.9))), + ]; + let t = TestReadings::at_checkpoint(Checkpoint::declare(2), &scored, &[HELD_OUT], &items); + assert_eq!( + (t.count(), t.chrf(), t.meaning()), + (1, Some(60.0), Some(0.6)) + ); + assert_eq!( + t.items(), + 1, + "model-written and withdrawn Test items are excluded" + ); + assert!(t.max_chrf().unwrap() > 99.0); + assert_eq!(t.coverage_by_n()[2].share, Some(1.0)); + + // A minted string that is not the Test text reads low; the same method as + // the Validation reading, so the same minted text reads alike in each. + let t = TestReadings::at_checkpoint(Checkpoint::declare(2), &scored, &["zzz"], &items); + assert!(t.max_chrf().unwrap() < 20.0); + assert_eq!(t.coverage_by_n()[2].share, Some(0.0)); + + let empty = TestReadings::at_checkpoint(Checkpoint::declare(2), &[], &[HELD_OUT], &[]); + assert_eq!( + (empty.count(), empty.chrf(), empty.meaning()), + (0, None, None) + ); + assert_eq!((empty.items(), empty.max_chrf()), (0, None)); +} + +#[test] +fn validation_and_test_leak_agree_in_method() { + // The same text and minted strings, once in Validation and once in Test, + // read the same numbers. + let minted = chunks(3, " "); + let minted = refs(&minted); + let train_item = train("item:t:1", UNRELATED); + let v = leak_diagnostic( + &minted, + &[validation("item:v:1", HELD_OUT), train_item.clone()], + ); + let test = item_in("item:c:1", HELD_OUT, &group_in_bucket(90), cast()); + let t = TestReadings::at_checkpoint(Checkpoint::declare(1), &[], &minted, &[test, train_item]); + assert_eq!(t.items(), v.items); + assert_eq!(t.max_chrf(), v.max_chrf.as_ref().map(|p| p.chrf)); + assert_eq!(t.coverage_by_n(), v.coverage_by_n.as_slice()); +}