test-text-guard s1 fixes: corpus-door sameness for texts of 12+ normalised chars, short texts as a line's end, every Test-group item withheld, round/cycle path pinned, split.rs doc dropped so PROMPT_SOURCE_DIGEST is unchanged
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b3821810a5
commit
f2b1e4fb81
5 changed files with 74 additions and 37 deletions
|
|
@ -7,6 +7,7 @@ use std::io::ErrorKind;
|
|||
use std::path::PathBuf;
|
||||
|
||||
use crate::context::Prompt;
|
||||
use crate::store::normalized;
|
||||
|
||||
/// A model answering [`Prompt`]s and nothing else.
|
||||
pub trait ModelPort {
|
||||
|
|
@ -20,7 +21,7 @@ pub trait ModelPort {
|
|||
pub enum PortError {
|
||||
/// Nobody has answered yet.
|
||||
Unanswered,
|
||||
/// The prompt holds the text of a Test item. It is neither written nor answered.
|
||||
/// The prompt holds the text of an item in a Test group. It is neither written nor answered.
|
||||
Withheld,
|
||||
Io(ErrorKind),
|
||||
}
|
||||
|
|
@ -55,20 +56,29 @@ impl AnswerFiles {
|
|||
}
|
||||
}
|
||||
|
||||
/// Whether `prompt` holds `text` as a line, as a JSON string, or (at 24 chars
|
||||
/// or more) anywhere. A short sentence is matched whole, since "Stop." is a
|
||||
/// substring of any prompt that says "Stop. Then ..."; an item reaches a
|
||||
/// prompt as a line or a JSON string value.
|
||||
/// Normalised length from which a Test text is matched anywhere in a prompt.
|
||||
const LONG: usize = 12;
|
||||
|
||||
/// Whether `prompt` holds `text`. The corpus door's own sameness
|
||||
/// ([`normalized`]: NFKC, case fold, spacing and punctuation dropped) decides
|
||||
/// for a text of `LONG` normalised characters or more, so a re-cased,
|
||||
/// re-spaced, wrapped or width-changed copy still counts. A shorter text would
|
||||
/// match anything ("Stop." is in "Stop. Then ..."), so it counts only as a
|
||||
/// line's end after a non-letter (every renderer prints an item's text last on its line).
|
||||
fn holds(prompt: &str, text: &str) -> bool {
|
||||
let text = text.trim();
|
||||
if text.is_empty() {
|
||||
let key = normalized(text);
|
||||
if key.is_empty() {
|
||||
return false;
|
||||
}
|
||||
if text.chars().count() >= 24 && prompt.contains(text) {
|
||||
return true;
|
||||
if key.chars().count() >= LONG {
|
||||
return normalized(prompt).contains(&key);
|
||||
}
|
||||
let json = serde_json::to_string(text).unwrap_or_default();
|
||||
prompt.lines().any(|l| l.trim() == text) || prompt.contains(&json)
|
||||
prompt.lines().any(|l| {
|
||||
l.trim_end().strip_suffix(text).is_some_and(|head| {
|
||||
head.chars().next_back().is_none_or(|c| !c.is_alphanumeric())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Answers read from one directory and nothing else: `<digest>.answer` is the
|
||||
|
|
|
|||
|
|
@ -509,7 +509,7 @@ impl fmt::Display for RunError {
|
|||
RunError::Drift(of) => write!(f, "drift in {of:?}"),
|
||||
RunError::NoInventory => f.write_str("the inventory holds no revision"),
|
||||
RunError::Io(kind) => write!(f, "io error {kind:?}"),
|
||||
RunError::Withheld => f.write_str("a prompt holds Test item text; nothing was written"),
|
||||
RunError::Withheld => f.write_str("a prompt holds Test item text; it was neither written nor answered"),
|
||||
RunError::Store => f.write_str("a store would not read or write"),
|
||||
RunError::Unrecorded => f.write_str(
|
||||
"the round holds no answer to a lowering prompt rebuilt now: the prompt digest drifted or the head inventory moved since the round",
|
||||
|
|
|
|||
|
|
@ -5,8 +5,7 @@
|
|||
//! `EncoderOffer` need a Checkpoint, and only the lib can make one
|
||||
//! (convergence-freeze adds the door). Operator tools (`weksa corpus dump`,
|
||||
//! `weksa score`, `CorpusStore`) read corpus text of any split; that is not
|
||||
//! gated here. `AnswerFiles`, the one door a prompt leaves the loop by, refuses
|
||||
//! any prompt that holds the text of a live Test item (`PortError::Withheld`).
|
||||
//! gated here.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
|
|
|
|||
|
|
@ -249,12 +249,13 @@ impl CorpusStore {
|
|||
Ok(items)
|
||||
}
|
||||
|
||||
/// The text of every live Test item, any language: what no prompt may hold.
|
||||
/// The text of every item in a Test group, any language, withdrawn and
|
||||
/// model-written ones included: what no prompt may hold.
|
||||
pub fn test_texts(&self) -> anyhow::Result<Vec<String>> {
|
||||
Ok(self
|
||||
.all()?
|
||||
.into_iter()
|
||||
.filter(|item| SplitScheme::V1.live(item) == Some(Split::Test))
|
||||
.filter(|item| SplitScheme::V1.of_bucket(SplitScheme::V1.bucket(&item.group)) == Split::Test)
|
||||
.map(|item| item.text)
|
||||
.collect())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2596,6 +2596,8 @@ fn source_tripwires() {
|
|||
assert!(!text.contains(banned), "{file} names {banned}");
|
||||
}
|
||||
}
|
||||
// Every prompt file leaves through RunStores::answer_files, which withholds Test text.
|
||||
assert!(!read("main.rs").contains("AnswerFiles::new"));
|
||||
let mut all = Vec::new();
|
||||
let mut stack = vec![src.clone()];
|
||||
while let Some(dir) = stack.pop() {
|
||||
|
|
@ -5121,41 +5123,58 @@ fn a_run_reads_entries_and_scenes_at_its_own_head() {
|
|||
fn answer_files_withhold_prompts_that_hold_test_text() {
|
||||
use weksa::model::{AnswerFiles, ModelPort, PortError};
|
||||
let p = prompt();
|
||||
let line = p.text().lines().find(|l| l.chars().count() >= 24).unwrap().to_string();
|
||||
let short = p.text().lines().find(|l| !l.is_empty() && l.chars().count() < 24).unwrap();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let round = dir.path().join("round-1");
|
||||
let held = |texts: Vec<String>| AnswerFiles::new(round.clone(), "m".into(), texts);
|
||||
// A whole line, a long fragment of one, and a JSON string value are Test text.
|
||||
for text in [line.clone(), line[..24].to_string(), format!("{short:?}")] {
|
||||
let mut files = held(vec![text.trim_matches('"').to_string()]);
|
||||
assert_eq!(files.answer(&p), Err(PortError::Withheld), "{text}");
|
||||
}
|
||||
let refused = |t: &str| {
|
||||
AnswerFiles::new(round.clone(), "m".into(), vec![t.to_string()]).answer(&p)
|
||||
== Err(PortError::Withheld)
|
||||
};
|
||||
// A text of 12 normalised characters or more counts anywhere in the prompt, in
|
||||
// any case, spacing, wrapping or width: the corpus door's own sameness.
|
||||
assert!(refused("Nibu says mind the hatch."));
|
||||
assert!(refused("NIBU SAYS MIND THE HATCH"));
|
||||
assert!(refused("nibu says\n mind the\n\n hatch"));
|
||||
assert!(refused("\u{ff2e}ibu says mind the hatch."));
|
||||
assert!(!round.exists(), "a refused prompt creates nothing");
|
||||
// A short Test sentence inside other prose is not Test text: "Stop." is not "Stop. Then".
|
||||
let mut files = held(vec![short[..short.len() - 1].to_string()]);
|
||||
assert_eq!(files.answer(&p), Err(PortError::Unanswered));
|
||||
// A short text counts only as the end of a line after a non-letter.
|
||||
assert!(refused("the hatch."));
|
||||
assert!(!refused("hatch"), "a mid-word short text is not the line's end");
|
||||
assert!(!refused("mind the"));
|
||||
assert!(!refused(" "), "a blank withheld text matches nothing");
|
||||
// Unrelated text passes and the prompt is then written for its answerer.
|
||||
let mut clean = AnswerFiles::new(round.clone(), "m".into(), vec!["The ferry leaves at noon.".into()]);
|
||||
assert_eq!(clean.answer(&p), Err(PortError::Unanswered));
|
||||
assert!(round.join(format!("{}.prompt", p.digest())).exists());
|
||||
// The refusal comes before a recorded answer is read.
|
||||
std::fs::write(round.join(format!("{}.answer", p.digest())), "x").unwrap();
|
||||
assert_eq!(held(vec![line]).answer(&p), Err(PortError::Withheld));
|
||||
assert!(refused("Nibu says mind the hatch."));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_answer_files_withhold_a_live_test_item_text() {
|
||||
fn a_round_stops_on_a_prompt_that_holds_test_text() {
|
||||
use common::{cast, entities_in, group_in_bucket, item_in};
|
||||
use weksa::model::{ModelPort, PortError};
|
||||
use weksa::run::{RunError, round};
|
||||
let mut w = world();
|
||||
let entities = entities_in(w.dir.path());
|
||||
let text = "Hold this sentence back.";
|
||||
// The sentence in a Test group, and a Train line that quotes it (the corpus
|
||||
// door refuses an identical twin, not a longer line that holds it).
|
||||
let quoting = format!("{text} And then some.");
|
||||
for (key, line, bucket) in [("item:held:1", text, 90), ("item:held:2", "ing, 10)] {
|
||||
// Test sentences (one withdrawn), and Train lines that quote them: the corpus
|
||||
// door refuses an identical twin, not a longer line that holds one.
|
||||
let held = "Hold this sentence back.";
|
||||
let gone = "A withdrawn Test sentence.";
|
||||
w.corpus
|
||||
.add(item_in("item:held:3", gone, &group_in_bucket(91), cast()), &entities)
|
||||
.unwrap();
|
||||
w.corpus.withdraw("item:held:3").unwrap();
|
||||
for (key, line, bucket) in [
|
||||
("item:held:1", held.to_string(), 90),
|
||||
("item:held:2", format!("{held} And then some."), 10),
|
||||
("item:held:4", format!("{gone} And then some."), 11),
|
||||
] {
|
||||
w.corpus
|
||||
.add(item_in(key, line, &group_in_bucket(bucket), cast()), &entities)
|
||||
.add(item_in(key, &line, &group_in_bucket(bucket), cast()), &entities)
|
||||
.unwrap();
|
||||
}
|
||||
let run = run_over(w.dir.path());
|
||||
let mut run = run_over(w.dir.path());
|
||||
let dir = w.dir.path().join("answers");
|
||||
let mut files = run.answer_files(dir.clone()).unwrap();
|
||||
let inv = run.inventory().inventory().unwrap();
|
||||
|
|
@ -5165,7 +5184,15 @@ fn run_answer_files_withhold_a_live_test_item_text() {
|
|||
weksa::context::Prompt::encode(&offer, &train, &inv, 4)
|
||||
};
|
||||
assert_eq!(files.answer(&encode("item:nibu:1")), Err(PortError::Unanswered));
|
||||
// The quoting line's prompt holds the Test sentence: refused, and nothing is written.
|
||||
assert_eq!(files.answer(&encode("item:held:2")), Err(PortError::Withheld));
|
||||
for key in ["item:held:2", "item:held:4"] {
|
||||
assert_eq!(files.answer(&encode(key)), Err(PortError::Withheld), "{key}");
|
||||
}
|
||||
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "only the clean prompt");
|
||||
// The round stops on it rather than skipping the item.
|
||||
let scorer = weksa::score::Scorer::new(OneBag);
|
||||
let mut round_files = run.answer_files(w.dir.path().join("round")).unwrap();
|
||||
assert!(matches!(
|
||||
round(&mut run, &mut round_files, &scorer),
|
||||
Err(RunError::Withheld)
|
||||
));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue