Checkpoint offers each item once; prior turns print the held Train text
for_checkpoint takes &mut Checkpoint and refuses a repeated key (OfferedAtCheckpoint) after the gate; TrainItems::held replaces holds and block prints the matched Train item's text; the text-key fold is pinned against the store's own S6 refusal. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
469e46cbc5
commit
d5b11088ad
3 changed files with 153 additions and 25 deletions
|
|
@ -69,13 +69,14 @@ impl TrainItems {
|
|||
self.items.is_empty()
|
||||
}
|
||||
|
||||
/// Whether a live Train item of `group` holds `text`, compared by the
|
||||
/// store's own text key.
|
||||
fn holds(&self, group: &str, text: &str) -> bool {
|
||||
/// The text of the live Train item of `group` whose store text key equals
|
||||
/// `text`'s (store S6 makes it unique).
|
||||
fn held(&self, group: &str, text: &str) -> Option<&str> {
|
||||
let key = normalized(text);
|
||||
self.items
|
||||
.iter()
|
||||
.any(|item| item.group == group && normalized(&item.text) == key)
|
||||
.find(|item| item.group == group && normalized(&item.text) == key)
|
||||
.map(|item| item.text.as_str())
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> impl Iterator<Item = &str> {
|
||||
|
|
@ -91,6 +92,8 @@ pub enum OfferRefusal {
|
|||
NoSplit,
|
||||
/// A Test item is offered only under a [`Checkpoint`].
|
||||
TestBeforeCheckpoint,
|
||||
/// An item is offered at most once under one [`Checkpoint`].
|
||||
OfferedAtCheckpoint,
|
||||
}
|
||||
|
||||
/// One item the encoder may be shown, with the entities a packet for it may
|
||||
|
|
@ -109,12 +112,17 @@ impl EncoderOffer {
|
|||
}
|
||||
}
|
||||
|
||||
/// A convergence checkpoint offers any live item, Test included.
|
||||
/// A convergence checkpoint offers any live item, Test included, each item
|
||||
/// at most once under one `Checkpoint`. The gate runs first, so a refused
|
||||
/// item records no key.
|
||||
pub fn for_checkpoint(
|
||||
item: &CorpusItem,
|
||||
_checkpoint: &Checkpoint,
|
||||
checkpoint: &mut Checkpoint,
|
||||
) -> Result<Self, OfferRefusal> {
|
||||
Self::gate(item)?;
|
||||
if !checkpoint.first_offer(&item.id) {
|
||||
return Err(OfferRefusal::OfferedAtCheckpoint);
|
||||
}
|
||||
Ok(Self::of(item))
|
||||
}
|
||||
|
||||
|
|
@ -180,20 +188,19 @@ pub struct Prompt {
|
|||
|
||||
/// What a prompt prints of an item: its line, and its prior turns only as the
|
||||
/// one owner allows. A prior turn is a copy of text, not a reference, so it can
|
||||
/// outlive a withdrawal or quote a held-out line; its text is printed only when
|
||||
/// a live Train item of the item's group holds it, and the speaker alone is
|
||||
/// printed otherwise. Mint prints no prior turns (`prior: None`).
|
||||
/// outlive a withdrawal or quote a held-out line; what is printed is the text of
|
||||
/// the live Train item of the item's group that holds it (never the turn's own
|
||||
/// bytes), and the speaker alone otherwise. Mint prints no prior turns (`prior: None`).
|
||||
fn block(item: &CorpusItem, prior: Option<&TrainItems>) -> String {
|
||||
let mut lines: Vec<String> = prior
|
||||
.into_iter()
|
||||
.flat_map(|train| {
|
||||
item.prior_turns.iter().map(move |t| {
|
||||
if train.holds(&item.group, &t.text) {
|
||||
format!("{}: {}", t.speaker, t.text)
|
||||
} else {
|
||||
format!("{}: {ELIDED}", t.speaker)
|
||||
}
|
||||
})
|
||||
item.prior_turns
|
||||
.iter()
|
||||
.map(move |t| match train.held(&item.group, &t.text) {
|
||||
Some(text) => format!("{}: {text}", t.speaker),
|
||||
None => format!("{}: {ELIDED}", t.speaker),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
lines.push(format!("[{}] {}: {}", item.id, item.speaker, item.text));
|
||||
|
|
@ -245,8 +252,9 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn checkpoint_offer_gate() {
|
||||
let cp = Checkpoint::for_test(1);
|
||||
let check = |i: &CorpusItem| EncoderOffer::for_checkpoint(i, &cp).err();
|
||||
// A fresh checkpoint per call: this test is the gate, not the once rule.
|
||||
let check =
|
||||
|i: &CorpusItem| EncoderOffer::for_checkpoint(i, &mut Checkpoint::for_test(1)).err();
|
||||
let train = item("item:a:1", "t", &group_in(Split::Train), false);
|
||||
let validation = item("item:b:1", "v", &group_in(Split::Validation), false);
|
||||
let test = item("item:c:1", "x", &group_in(Split::Test), false);
|
||||
|
|
@ -263,4 +271,32 @@ mod tests {
|
|||
both.withdrawn = true;
|
||||
assert_eq!(check(&both), Some(OfferRefusal::Withdrawn));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkpoint_offers_each_item_once() {
|
||||
let mut cp = Checkpoint::for_test(1);
|
||||
let offer = |cp: &mut Checkpoint, i: &CorpusItem| EncoderOffer::for_checkpoint(i, cp).err();
|
||||
let a = item("item:c:1", "a", &group_in(Split::Test), false);
|
||||
let b = item("item:c:3", "b", &group_in(Split::Test), false);
|
||||
let v = item("item:b:1", "v", &group_in(Split::Validation), false);
|
||||
assert_eq!(offer(&mut cp, &a), None);
|
||||
assert_eq!(offer(&mut cp, &a), Some(OfferRefusal::OfferedAtCheckpoint));
|
||||
assert_eq!(offer(&mut cp, &b), None);
|
||||
assert_eq!(offer(&mut cp, &v), None);
|
||||
assert_eq!(offer(&mut cp, &v), Some(OfferRefusal::OfferedAtCheckpoint));
|
||||
|
||||
// A refused item records no key: its refusal stays the gate's.
|
||||
let mut gone = item("item:c:2", "w", &group_in(Split::Test), false);
|
||||
gone.withdrawn = true;
|
||||
assert_eq!(offer(&mut cp, &gone), Some(OfferRefusal::Withdrawn));
|
||||
assert_eq!(offer(&mut cp, &gone), Some(OfferRefusal::Withdrawn));
|
||||
let mut live = gone.clone();
|
||||
live.withdrawn = false;
|
||||
assert_eq!(offer(&mut cp, &live), None, "the refusal recorded no key");
|
||||
|
||||
// The checkpoint is spent by its reading; a new one offers afresh.
|
||||
let _ = crate::score::TestReadings::at_checkpoint(cp, &[], &[], &[]);
|
||||
let mut fresh = Checkpoint::for_test(2);
|
||||
assert_eq!(offer(&mut fresh, &a), None);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
//! read in its live split; Test is read only under a Checkpoint, and only the
|
||||
//! lib can make one (convergence-freeze adds the door).
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use crate::corpus::{CorpusItem, Source};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
|
|
@ -70,7 +72,8 @@ impl SplitScheme {
|
|||
/// convergence-freeze, which builds a `Checkpoint` from the convergence window
|
||||
/// over the run's persisted round records (convergence-test).
|
||||
/// `TestReadings::at_checkpoint` consumes it, so one `Checkpoint` buys one Test
|
||||
/// reading.
|
||||
/// reading. One `Checkpoint` also offers each item at most once
|
||||
/// (`EncoderOffer::for_checkpoint`).
|
||||
///
|
||||
/// ```
|
||||
/// fn f(c: &weksa::split::Checkpoint) -> u32 { c.round() }
|
||||
|
|
@ -82,6 +85,10 @@ impl SplitScheme {
|
|||
/// let _ = weksa::split::Checkpoint::declare(1);
|
||||
/// ```
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// let _ = weksa::split::Checkpoint { round: 1, offered: Default::default() };
|
||||
/// ```
|
||||
///
|
||||
/// One reading per checkpoint:
|
||||
///
|
||||
/// ```
|
||||
|
|
@ -104,12 +111,21 @@ impl SplitScheme {
|
|||
/// ```
|
||||
pub struct Checkpoint {
|
||||
round: u32,
|
||||
offered: BTreeSet<String>,
|
||||
}
|
||||
|
||||
impl Checkpoint {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn for_test(round: u32) -> Self {
|
||||
Self { round }
|
||||
Self {
|
||||
round,
|
||||
offered: BTreeSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Records `key` as offered; false when it already was.
|
||||
pub(crate) fn first_offer(&mut self, key: &str) -> bool {
|
||||
self.offered.insert(key.to_string())
|
||||
}
|
||||
|
||||
pub fn round(&self) -> u32 {
|
||||
|
|
|
|||
|
|
@ -381,22 +381,98 @@ fn prior_turns_print_only_text_a_live_train_item_holds() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prior_turn_match_uses_the_stores_text_key() {
|
||||
/// A scratch Train store holding `train_text`, and an encode prompt for a
|
||||
/// sibling whose one prior turn is `prior_text`.
|
||||
fn encode_with_prior(train_text: &str, prior_text: &str) -> (String, Result<(), StoreError>) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let entities = entities_in(dir.path());
|
||||
let mut store = CorpusStore::open(&dir.path().join("c.cc")).unwrap();
|
||||
let g = group_in_bucket(10);
|
||||
store
|
||||
.add(item_in("item:a:1", "Context Line", &g, cast()), &entities)
|
||||
.add(item_in("item:a:1", train_text, &g, cast()), &entities)
|
||||
.unwrap();
|
||||
let mut reply = item_in("item:a:2", "reply", &g, cast());
|
||||
reply.prior_turns = vec![Turn {
|
||||
speaker: common::eid("brokkr"),
|
||||
text: "context line".into(),
|
||||
text: prior_text.into(),
|
||||
}];
|
||||
store.add(reply.clone(), &entities).unwrap();
|
||||
let t = TrainItems::from_corpus(&store).unwrap();
|
||||
let p = Prompt::encode(&EncoderOffer::for_round(&reply).unwrap(), &t);
|
||||
assert!(p.text().contains("entity:brokkr: context line"));
|
||||
let text = p.text().to_string();
|
||||
// The store's own rule, as the oracle for key equality; after the prompt is
|
||||
// built, because an admitted probe would itself become a held line.
|
||||
let probe = store.add(item_in("item:a:9", prior_text, &g, cast()), &entities);
|
||||
(text, probe.map(|_| ()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prior_turn_prints_the_held_train_text() {
|
||||
let (text, _) = encode_with_prior("Context Line", "context-line!! :)");
|
||||
assert!(
|
||||
text.contains("entity:brokkr: Context Line\n[item:a:2]"),
|
||||
"{text}"
|
||||
);
|
||||
for turn_bytes in ["!!", ":)", "context-line"] {
|
||||
assert!(!text.contains(turn_bytes), "{turn_bytes} in {text}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prior_turn_match_uses_the_stores_text_key() {
|
||||
let (text, _) = encode_with_prior("Context Line", "context line");
|
||||
assert!(text.contains("entity:brokkr: Context Line\n"));
|
||||
assert!(!text.contains("context line"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prior_turn_fold_matches_the_store() {
|
||||
let rows = [
|
||||
("case", "Context Line", "context line"),
|
||||
(
|
||||
"punctuation and spacing",
|
||||
"Context Line",
|
||||
"context, line!",
|
||||
),
|
||||
(
|
||||
"NFKC fullwidth letters",
|
||||
"Context",
|
||||
"\u{ff23}\u{ff4f}\u{ff4e}\u{ff54}\u{ff45}\u{ff58}\u{ff54}",
|
||||
),
|
||||
("NFKC fullwidth digits", "room 12", "room \u{ff11}\u{ff12}"),
|
||||
("dotless i", "Ilik", "\u{131}l\u{131}k"),
|
||||
("dotted capital I", "Istanbul", "\u{130}stanbul"),
|
||||
(
|
||||
"katakana vs hiragana",
|
||||
"\u{3072}\u{3089}\u{304c}\u{306a}",
|
||||
"\u{30d2}\u{30e9}\u{30ac}\u{30ca}",
|
||||
),
|
||||
("voiced katakana wa", "\u{308f}\u{3099}", "\u{30f7}"),
|
||||
("Arabic-Indic digits", "room 12", "room \u{661}\u{662}"),
|
||||
];
|
||||
for (fold, train_text, prior_text) in rows {
|
||||
let (text, probe) = encode_with_prior(train_text, prior_text);
|
||||
assert!(
|
||||
matches!(
|
||||
probe,
|
||||
Err(StoreError::Refused(StoreRefusal::S6TextHeld { .. }))
|
||||
),
|
||||
"{fold}: the store does not hold these as one text: {probe:?}"
|
||||
);
|
||||
assert!(
|
||||
text.contains(&format!("entity:brokkr: {train_text}\n[item:a:2]")),
|
||||
"{fold}: {text}"
|
||||
);
|
||||
assert!(!text.contains(ELIDED_MARK), "{fold}: {text}");
|
||||
}
|
||||
// A text differing by a letter is a different key: the store admits it and
|
||||
// the prior turn is elided.
|
||||
let (text, probe) = encode_with_prior("Context Line", "Context Lane");
|
||||
assert!(probe.is_ok(), "{probe:?}");
|
||||
assert!(
|
||||
text.contains("entity:brokkr: [elided]\n[item:a:2]"),
|
||||
"{text}"
|
||||
);
|
||||
}
|
||||
|
||||
const ELIDED_MARK: &str = "[elided]";
|
||||
|
|
|
|||
Loading…
Reference in a new issue