From 3bd4a376459fa711cf300677be266221045fd2c8 Mon Sep 17 00:00:00 2001 From: Weksa Date: Tue, 10 Nov 2026 01:57:40 +0000 Subject: [PATCH] context module: TrainItems, EncoderOffer and Prompt are the only road from corpus text to a model call Co-Authored-By: Claude Sonnet 5.5 --- crates/weksa/src/context.rs | 208 ++++++++++++++++++++++++ crates/weksa/src/lib.rs | 1 + crates/weksa/tests/context.rs | 298 ++++++++++++++++++++++++++++++++++ 3 files changed, 507 insertions(+) create mode 100644 crates/weksa/src/context.rs create mode 100644 crates/weksa/tests/context.rs diff --git a/crates/weksa/src/context.rs b/crates/weksa/src/context.rs new file mode 100644 index 0000000..13fce61 --- /dev/null +++ b/crates/weksa/src/context.rs @@ -0,0 +1,208 @@ +//! The one place prompt text is built from corpus text. Corpus text reaches a +//! model call only as a [`Prompt`], and a `Prompt` is made only by minting from +//! [`TrainItems`] or by encoding one item an [`EncoderOffer`] holds: Train-only +//! minting and per-item encoding (leak-owned-by-flow-and-scorer-3, +//! loop-context-builder-2). Instruction wording is not written here; a caller +//! passes `&'static str`, so the templates are fixed at compile time and hence +//! within a run. + +use crate::corpus::CorpusItem; +use crate::packet::EntityId; +use crate::split::{Checkpoint, Split, SplitScheme}; +use crate::store::CorpusStore; + +/// The live Train items of a corpus, in key order. The only input minting +/// reads; it cannot be built from a slice, and no accessor returns text or an +/// item. +/// +/// ``` +/// let dir = tempfile::tempdir().unwrap(); +/// let store = weksa::CorpusStore::open(&dir.path().join("c.cc")).unwrap(); +/// let t = weksa::context::TrainItems::from_corpus(&store).unwrap(); +/// assert!(t.is_empty()); +/// ``` +/// +/// ```compile_fail +/// let t = weksa::context::TrainItems { items: vec![] }; +/// ``` +/// +/// ```compile_fail +/// let dir = tempfile::tempdir().unwrap(); +/// let store = weksa::CorpusStore::open(&dir.path().join("c.cc")).unwrap(); +/// let t = weksa::context::TrainItems::from_corpus(&store).unwrap(); +/// let _u = t.clone(); +/// ``` +/// +/// ```compile_fail +/// fn s(_: &T) {} +/// let dir = tempfile::tempdir().unwrap(); +/// let store = weksa::CorpusStore::open(&dir.path().join("c.cc")).unwrap(); +/// let t = weksa::context::TrainItems::from_corpus(&store).unwrap(); +/// s(&t); +/// ``` +pub struct TrainItems { + items: Vec, +} + +impl TrainItems { + pub fn from_corpus(corpus: &CorpusStore) -> anyhow::Result { + let items = corpus + .all()? + .into_iter() + .filter(|item| SplitScheme::V1.live(item) == Some(Split::Train)) + .collect(); + Ok(Self { items }) + } + + pub fn len(&self) -> usize { + self.items.len() + } + + pub fn is_empty(&self) -> bool { + self.items.is_empty() + } + + pub fn keys(&self) -> impl Iterator { + self.items.iter().map(|item| item.id.as_str()) + } +} + +/// Why an item is not offered to the encoder. +#[derive(Debug, PartialEq, Eq)] +pub enum OfferRefusal { + Withdrawn, + /// A model-written item of a Test group is in no split. + NoSplit, + /// A Test item is offered only under a [`Checkpoint`]. + TestBeforeCheckpoint, +} + +/// One item the encoder may be shown, with the entities a packet for it may +/// cite. Built per item, so a call sees one item and nothing of its group. +pub struct EncoderOffer { + item: CorpusItem, + entities: Vec, +} + +impl EncoderOffer { + /// A round offers Train and Validation items. + pub fn for_round(item: &CorpusItem) -> Result { + match Self::gate(item)? { + Split::Test => Err(OfferRefusal::TestBeforeCheckpoint), + _ => Ok(Self::of(item)), + } + } + + /// A convergence checkpoint offers any live item, Test included. + pub fn for_checkpoint( + item: &CorpusItem, + _checkpoint: &Checkpoint, + ) -> Result { + Self::gate(item)?; + Ok(Self::of(item)) + } + + fn gate(item: &CorpusItem) -> Result { + if item.withdrawn { + return Err(OfferRefusal::Withdrawn); + } + SplitScheme::V1.live(item).ok_or(OfferRefusal::NoSplit) + } + + fn of(item: &CorpusItem) -> Self { + let mut entities: Vec = item.cited_entities().cloned().collect(); + entities.sort(); + entities.dedup(); + Self { + item: item.clone(), + entities, + } + } + + pub fn key(&self) -> &str { + &self.item.id + } + + /// The item's own cited entities, sorted, without repeats. + pub fn entities(&self) -> &[EntityId] { + &self.entities + } +} + +/// Text for a model call, and the keys of the items it holds. The only +/// constructors are [`Prompt::mint`] and [`Prompt::encode`]. +/// +/// ``` +/// let dir = tempfile::tempdir().unwrap(); +/// let store = weksa::CorpusStore::open(&dir.path().join("c.cc")).unwrap(); +/// let t = weksa::context::TrainItems::from_corpus(&store).unwrap(); +/// let p = weksa::context::Prompt::mint("Mint.", &t); +/// assert_eq!(p.text(), "Mint."); +/// ``` +/// +/// Instructions are compile-time text: +/// +/// ```compile_fail +/// let dir = tempfile::tempdir().unwrap(); +/// let store = weksa::CorpusStore::open(&dir.path().join("c.cc")).unwrap(); +/// let t = weksa::context::TrainItems::from_corpus(&store).unwrap(); +/// let p = weksa::context::Prompt::mint(&String::from("x"), &t); +/// ``` +/// +/// ```compile_fail +/// fn s(_: &T) {} +/// let dir = tempfile::tempdir().unwrap(); +/// let store = weksa::CorpusStore::open(&dir.path().join("c.cc")).unwrap(); +/// let t = weksa::context::TrainItems::from_corpus(&store).unwrap(); +/// let p = weksa::context::Prompt::mint("x", &t); +/// s(&p); +/// ``` +pub struct Prompt { + text: String, + items: Vec, +} + +fn block(item: &CorpusItem) -> String { + let mut lines: Vec = item + .prior_turns + .iter() + .map(|t| format!("{}: {}", t.speaker, t.text)) + .collect(); + lines.push(format!("[{}] {}: {}", item.id, item.speaker, item.text)); + lines.join("\n") +} + +impl Prompt { + /// Instructions, then every Train item in key order. + pub fn mint(instructions: &'static str, train: &TrainItems) -> Prompt { + let mut parts = vec![instructions.to_string()]; + parts.extend(train.items.iter().map(block)); + Prompt { + text: parts.join("\n\n"), + items: train.keys().map(str::to_string).collect(), + } + } + + /// Instructions, the one offered item, and the entities a packet for it + /// may cite. + pub fn encode(instructions: &'static str, offer: &EncoderOffer) -> Prompt { + let ids: Vec<&str> = offer.entities.iter().map(EntityId::as_str).collect(); + Prompt { + text: format!( + "{instructions}\n\n{}\n\nentities: {}", + block(&offer.item), + ids.join(", ") + ), + items: vec![offer.item.id.clone()], + } + } + + pub fn text(&self) -> &str { + &self.text + } + + /// The keys of the items the text holds; what a round record lists. + pub fn item_keys(&self) -> &[String] { + &self.items + } +} diff --git a/crates/weksa/src/lib.rs b/crates/weksa/src/lib.rs index d826c0e..bb42311 100644 --- a/crates/weksa/src/lib.rs +++ b/crates/weksa/src/lib.rs @@ -3,6 +3,7 @@ //! train/validation/test split, the stores that admit them, and the scorer //! (chrF, meaning cosine, leak diagnostics). +pub mod context; pub mod corpus; pub mod daodejing; pub mod entity; diff --git a/crates/weksa/tests/context.rs b/crates/weksa/tests/context.rs new file mode 100644 index 0000000..b499ee1 --- /dev/null +++ b/crates/weksa/tests/context.rs @@ -0,0 +1,298 @@ +mod common; + +use common::{cast, ent, entities_in, group_in_bucket, item_in, model, world}; +use weksa::context::{EncoderOffer, OfferRefusal, Prompt, TrainItems}; +use weksa::split::Checkpoint; +use weksa::{ + CorpusItem, CorpusItemId, CorpusStore, NameSpan, PacketId, PacketStore, StoreError, + StoreRefusal, Turn, +}; + +const MINT: &str = "Mint glosses from the lines below."; +const ENCODE: &str = "Encode the line below."; + +struct Corpus { + _dir: tempfile::TempDir, + store: CorpusStore, +} + +/// One live item per split, a withdrawn Train item and a model-written +/// Test-group item, each with a marker word unique to it. +fn corpus() -> Corpus { + let dir = tempfile::tempdir().unwrap(); + let entities = entities_in(dir.path()); + let mut store = CorpusStore::open(&dir.path().join("c.cc")).unwrap(); + let mut put = |item: CorpusItem| { + let withdrawn = item.withdrawn; + let id = item.id.clone(); + store + .add( + CorpusItem { + withdrawn: false, + ..item + }, + &entities, + ) + .unwrap(); + if withdrawn { + store.withdraw(&id).unwrap(); + } + }; + put(item_in( + "item:a:1", + "alphamarker speaks", + &group_in_bucket(10), + cast(), + )); + put(item_in( + "item:b:1", + "betamarker speaks", + &group_in_bucket(50), + cast(), + )); + put(item_in( + "item:c:1", + "gammamarker speaks", + &group_in_bucket(90), + cast(), + )); + let mut gone = item_in( + "item:a:2", + "deltamarker speaks", + &group_in_bucket(10), + cast(), + ); + gone.withdrawn = true; + put(gone); + put(item_in( + "item:c:2", + "epsilonmarker speaks", + &group_in_bucket(90), + model(), + )); + Corpus { _dir: dir, store } +} + +fn get(c: &Corpus, key: &str) -> CorpusItem { + c.store.get(key).unwrap().unwrap() +} + +#[test] +fn train_items_hold_only_live_train() { + let c = corpus(); + let t = TrainItems::from_corpus(&c.store).unwrap(); + assert_eq!(t.keys().collect::>(), ["item:a:1"]); + assert_eq!((t.len(), t.is_empty()), (1, false)); +} + +#[test] +fn train_items_are_in_key_order() { + let dir = tempfile::tempdir().unwrap(); + let entities = entities_in(dir.path()); + let mut store = CorpusStore::open(&dir.path().join("c.cc")).unwrap(); + for (key, text) in [ + ("item:z:1", "zed line"), + ("item:a:1", "ay line"), + ("item:m:1", "em line"), + ] { + store + .add(item_in(key, text, &group_in_bucket(10), cast()), &entities) + .unwrap(); + } + let t = TrainItems::from_corpus(&store).unwrap(); + assert_eq!( + t.keys().collect::>(), + ["item:a:1", "item:m:1", "item:z:1"] + ); +} + +#[test] +fn mint_prompt_holds_train_text_only() { + let c = corpus(); + let t = TrainItems::from_corpus(&c.store).unwrap(); + let p = Prompt::mint(MINT, &t); + assert!(p.text().starts_with(MINT)); + assert!(p.text().contains("alphamarker")); + for other in ["betamarker", "gammamarker", "deltamarker", "epsilonmarker"] { + assert!(!p.text().contains(other), "{other}"); + } + assert_eq!(p.item_keys(), ["item:a:1"]); +} + +#[test] +fn prompts_render_deterministically() { + let dir = tempfile::tempdir().unwrap(); + let entities = entities_in(dir.path()); + let mut store = CorpusStore::open(&dir.path().join("c.cc")).unwrap(); + let mut one = item_in("item:a:1", "Hello there.", &group_in_bucket(10), cast()); + one.prior_turns = vec![Turn { + speaker: common::eid("brokkr"), + text: "Well?".into(), + }]; + store.add(one, &entities).unwrap(); + store + .add( + item_in("item:a:2", "Second line.", &group_in_bucket(10), cast()), + &entities, + ) + .unwrap(); + let t = TrainItems::from_corpus(&store).unwrap(); + assert_eq!( + Prompt::mint(MINT, &t).text(), + format!( + "{MINT}\n\nentity:brokkr: Well?\n[item:a:1] entity:nibu: Hello there.\n\n[item:a:2] entity:nibu: Second line." + ) + ); + assert_eq!(Prompt::mint(MINT, &t).item_keys(), ["item:a:1", "item:a:2"]); + + let offer = EncoderOffer::for_round(&store.get("item:a:2").unwrap().unwrap()).unwrap(); + assert_eq!( + Prompt::encode(ENCODE, &offer).text(), + format!("{ENCODE}\n\n[item:a:2] entity:nibu: Second line.\n\nentities: entity:nibu") + ); + assert_eq!(Prompt::encode(ENCODE, &offer).item_keys(), ["item:a:2"]); +} + +#[test] +fn encoder_offer_split_gate() { + let c = corpus(); + let cp = Checkpoint::declare(1); + let train = get(&c, "item:a:1"); + let validation = get(&c, "item:b:1"); + let test = get(&c, "item:c:1"); + let no_split = get(&c, "item:c:2"); + let withdrawn = get(&c, "item:a:2"); + assert!(withdrawn.withdrawn); + let round = |i: &CorpusItem| EncoderOffer::for_round(i).err(); + let check = |i: &CorpusItem| EncoderOffer::for_checkpoint(i, &cp).err(); + + assert_eq!(round(&train), None); + assert_eq!(round(&validation), None); + assert_eq!(round(&test), Some(OfferRefusal::TestBeforeCheckpoint)); + assert_eq!(round(&no_split), Some(OfferRefusal::NoSplit)); + assert_eq!(round(&withdrawn), Some(OfferRefusal::Withdrawn)); + + assert_eq!(check(&train), None); + assert_eq!(check(&validation), None); + assert_eq!(check(&test), None); + assert_eq!(check(&no_split), Some(OfferRefusal::NoSplit)); + assert_eq!(check(&withdrawn), Some(OfferRefusal::Withdrawn)); + + // Withdrawn wins over NoSplit. + let mut both = no_split.clone(); + both.withdrawn = true; + assert_eq!(round(&both), Some(OfferRefusal::Withdrawn)); + assert_eq!(check(&both), Some(OfferRefusal::Withdrawn)); + // A withdrawn Test item is Withdrawn, not TestBeforeCheckpoint. + let mut test_gone = test.clone(); + test_gone.withdrawn = true; + assert_eq!(round(&test_gone), Some(OfferRefusal::Withdrawn)); +} + +fn span(slug: &str, start: u32, end: u32) -> NameSpan { + NameSpan { + entity: common::eid(slug), + start, + end, + } +} + +/// An item naming kay (e1), spoken by hero (e2) after a turn by brokkr (e3), +/// and a sibling of the same group naming someone (e4). +fn cited_world() -> (Corpus, CorpusItem, CorpusItem) { + let dir = tempfile::tempdir().unwrap(); + let mut entities = entities_in(dir.path()); + entities.put(ent("kay", "en-US", &["Kay"])).unwrap(); + let mut store = CorpusStore::open(&dir.path().join("c.cc")).unwrap(); + let group = group_in_bucket(10); + let mut mine = item_in("item:a:1", "Kay waits here.", &group, cast()); + mine.speaker = common::eid("hero"); + mine.names = vec![span("kay", 0, 3)]; + mine.prior_turns = vec![ + Turn { + speaker: common::eid("brokkr"), + text: "Where?".into(), + }, + Turn { + speaker: common::eid("brokkr"), + text: "Again?".into(), + }, + ]; + let mut sibling = item_in("item:a:2", "Someone waits there.", &group, cast()); + sibling.names = vec![span("someone", 0, 7)]; + store.add(mine.clone(), &entities).unwrap(); + store.add(sibling.clone(), &entities).unwrap(); + (Corpus { _dir: dir, store }, mine, sibling) +} + +#[test] +fn encoder_offer_entities_are_the_items_own() { + let (_c, mine, sibling) = cited_world(); + assert_eq!( + mine.cited_entities() + .map(|e| e.as_str()) + .collect::>(), + [ + "entity:kay", + "entity:hero", + "entity:brokkr", + "entity:brokkr" + ], + "names, speaker, prior-turn speakers, repeats kept" + ); + let offer = EncoderOffer::for_round(&mine).unwrap(); + let ids: Vec<&str> = offer.entities().iter().map(|e| e.as_str()).collect(); + assert_eq!(ids, ["entity:brokkr", "entity:hero", "entity:kay"]); + assert!( + !ids.contains(&"entity:someone"), + "a sibling's entity is not the item's" + ); + assert_eq!(offer.key(), "item:a:1"); + let p = Prompt::encode(ENCODE, &offer); + assert!( + p.text() + .ends_with("entities: entity:brokkr, entity:hero, entity:kay") + ); + assert!(p.text().contains("[item:a:1]")); + assert!(!p.text().contains("item:a:2") && !p.text().contains("Someone waits")); + assert_eq!(p.item_keys(), ["item:a:1"]); + let sib = EncoderOffer::for_round(&sibling).unwrap(); + assert_eq!(sib.entities().len(), 2); +} + +#[test] +fn offer_and_store_agree() { + let mut w = world(); + w.entities.put(ent("kay", "en-US", &["Kay"])).unwrap(); + let mut item = item_in( + "item:nibu:2", + "Kay waits here.", + &group_in_bucket(10), + cast(), + ); + item.speaker = common::eid("hero"); + item.names = vec![span("kay", 0, 3)]; + item.prior_turns = vec![Turn { + speaker: common::eid("brokkr"), + text: "Where?".into(), + }]; + w.corpus.add(item.clone(), &w.entities).unwrap(); + let offer = EncoderOffer::for_round(&item).unwrap(); + + let mut packets = PacketStore::open(&w.dir.path().join("p.cc")).unwrap(); + let all = ["nibu", "hero", "kay", "brokkr", "player", "s", "someone"]; + for (n, slug) in all.iter().enumerate() { + let mut p = common::good(); + p.id = PacketId::parse(&format!("packet:{n:016x}")).unwrap(); + p.provenance.corpus_item = CorpusItemId::parse("item:nibu:2").unwrap(); + p.referents[0].entity = Some(common::eid(slug)); + let offered = offer.entities().contains(&common::eid(slug)); + match packets.admit(p, &w.inventory, &w.corpus, &w.entities) { + Ok(_) => assert!(offered, "{slug} admitted but not offered"), + Err(StoreError::Refused(StoreRefusal::EntityNotOnItem { .. })) => { + assert!(!offered, "{slug} offered but refused") + } + Err(other) => panic!("{slug}: {other:?}"), + } + } +}