diff --git a/crates/weksa/src/encode.rs b/crates/weksa/src/encode.rs index 5ae008a..d6b22db 100644 --- a/crates/weksa/src/encode.rs +++ b/crates/weksa/src/encode.rs @@ -223,19 +223,22 @@ impl EditDraft { } /// The revision after `base` this edit asks for, and the entries it adds. - /// `None` when the edit makes no change or more than [`MAX_EDIT_CHANGES`]. + /// `None` when the edit makes no change or more than `max_changes`. The + /// loop's cycle passes [`MAX_EDIT_CHANGES`]; the CLI's `inventory admit` + /// passes its own larger cap for a draft reviewed whole in its cut. /// Every id is the store's: an added entry takes the next number of its /// family after the highest in `inventory`, retired entries included, in /// draft order. Nothing else is judged here: the gloss, the entry's shape, /// whether a retire target is live and a repeated id are /// `InventoryStore::admit_revision`'s to refuse. - pub(crate) fn into_revision( + pub fn into_revision( self, inventory: &Inventory, base: u32, + max_changes: usize, ) -> Option<(InventoryRevision, Vec)> { let count = self.add.len() + self.retire.len(); - if count == 0 || count > MAX_EDIT_CHANGES { + if count == 0 || count > max_changes { return None; } let n = base.checked_add(1)?; @@ -583,7 +586,7 @@ mod tests { InvId::parse("frame:2").unwrap(), ], }; - let (rev, entries) = draft.clone().into_revision(&inv, 6).unwrap(); + let (rev, entries) = draft.clone().into_revision(&inv, 6, MAX_EDIT_CHANGES).unwrap(); let ids: Vec = entries.iter().map(|e| e.id.to_string()).collect(); // After the highest of the family, retired ones included, in draft order. assert_eq!(ids, ["concept:4", "role:1", "concept:5", "frame:3"]); @@ -626,20 +629,25 @@ mod tests { .collect(), retire: vec![], }; - assert!(adds(0).into_revision(&inv, 1).is_none()); - assert!(adds(1).into_revision(&inv, 1).is_some()); - assert_eq!(adds(8).into_revision(&inv, 1).unwrap().1.len(), 8); - assert!(adds(9).into_revision(&inv, 1).is_none()); + assert!(adds(0).into_revision(&inv, 1, MAX_EDIT_CHANGES).is_none()); + assert!(adds(1).into_revision(&inv, 1, MAX_EDIT_CHANGES).is_some()); + assert_eq!(adds(8).into_revision(&inv, 1, MAX_EDIT_CHANGES).unwrap().1.len(), 8); + assert!(adds(9).into_revision(&inv, 1, MAX_EDIT_CHANGES).is_none()); // Adds and retires count together. let mixed = |a: usize, r: usize| EditDraft { add: (0..a).map(|_| add(Family::Concept, "g")).collect(), retire: vec![InvId::parse("concept:1").unwrap(); r], }; - assert!(mixed(4, 4).into_revision(&inv, 1).is_some()); - assert!(mixed(4, 5).into_revision(&inv, 1).is_none()); - assert!(mixed(0, 9).into_revision(&inv, 1).is_none()); + assert!(mixed(4, 4).into_revision(&inv, 1, MAX_EDIT_CHANGES).is_some()); + assert!(mixed(4, 5).into_revision(&inv, 1, MAX_EDIT_CHANGES).is_none()); + assert!(mixed(0, 9).into_revision(&inv, 1, MAX_EDIT_CHANGES).is_none()); // A repeated retire is the door's to refuse, not this fn's. - assert!(mixed(0, 2).into_revision(&inv, 1).is_some()); + assert!(mixed(0, 2).into_revision(&inv, 1, MAX_EDIT_CHANGES).is_some()); + // The cap is the caller's. + let (_, entries) = adds(200).into_revision(&inv, 1, 256).unwrap(); + assert_eq!(entries.first().unwrap().id.to_string(), "concept:2"); + assert_eq!(entries.last().unwrap().id.to_string(), "concept:201"); + assert!(adds(257).into_revision(&inv, 1, 256).is_none()); } #[test] diff --git a/crates/weksa/src/main.rs b/crates/weksa/src/main.rs index c4d7be6..24e68b0 100644 --- a/crates/weksa/src/main.rs +++ b/crates/weksa/src/main.rs @@ -8,7 +8,7 @@ use clap::error::{ContextKind, ContextValue, ErrorKind}; use clap::{Args, Parser, Subcommand, ValueEnum}; use unicode_normalization::UnicodeNormalization; use weksa::corpus::{CorpusItem, ModelFamily, NameSpan, Source, Turn}; -use weksa::encode::OntologyDraft; +use weksa::encode::{EditDraft, OntologyDraft}; use weksa::entity::{Entity, EntityOrigin, LanguageForms, Pronunciation}; use weksa::inventory::{proof_revision, seed, speaker_scene_revision}; use weksa::packet::{FeatValue, FeatureValue, Grade}; @@ -200,8 +200,15 @@ enum InventoryCommand { /// Admit the speaker-state revision (Scene and Speaker features) as the /// inventory's latest plus one. SpeakerScene, + /// Admit a model's inventory draft (a JSON file, {add, retire} as the + /// loop's editor writes it) as the inventory's latest plus one. + Admit { answer: PathBuf }, } +/// The most changes `inventory admit` takes in one draft. A hand-admitted draft +/// is reviewed whole in its cut, so the cap is not the loop editor's. +const MAX_ADMIT_CHANGES: usize = 256; + #[derive(Subcommand)] enum SceneCommand { /// Append a scene revision for one corpus group: its one live speaker's @@ -785,6 +792,22 @@ fn main() -> Result<()> { store.admit_revision(rev, entries).map_err(refused)?; println!("admitted rev:{n} with {count} entries"); } + Command::Inventory(InventoryCommand::Admit { answer }) => { + let text = std::fs::read_to_string(&answer).context("the draft would not read")?; + let draft = EditDraft::parse(&text)?; + let mut store = InventoryStore::open(&cli.inventory)?; + let inventory = store.inventory()?; + let latest = inventory + .latest() + .map(|r| r.n) + .context("the inventory holds no revision")?; + let (rev, entries) = draft + .into_revision(&inventory, latest, MAX_ADMIT_CHANGES) + .context("the draft makes no change or more than the cap")?; + let (n, count) = (rev.n, entries.len()); + store.admit_revision(rev, entries).map_err(refused)?; + println!("admitted rev:{n} with {count} entries"); + } Command::Scene(SceneCommand::Put { group, to, diff --git a/crates/weksa/src/run.rs b/crates/weksa/src/run.rs index 3d0da2a..51be889 100644 --- a/crates/weksa/src/run.rs +++ b/crates/weksa/src/run.rs @@ -40,7 +40,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::context::{EditFeedback, EncoderOffer, Prompt, TrainItems}; use crate::corpus::{CorpusItem, CorpusItemId, ModelFamily, Source}; -use crate::encode::{EditDraft, LoweringDraft, PacketDraft, encoder_id}; +use crate::encode::{EditDraft, LoweringDraft, MAX_EDIT_CHANGES, PacketDraft, encoder_id}; use crate::form::{is_attestation, is_language_tag, is_slug}; use crate::model::{ModelPort, PortError}; use crate::packet::{Packet, Stance}; @@ -2257,7 +2257,7 @@ pub fn cycle( let Some(draft) = answer.as_deref().and_then(|a| EditDraft::parse(a).ok()) else { return reject(run, Why::EditParse); }; - let Some((revision, entries)) = draft.into_revision(&inventory, base_rev) else { + let Some((revision, entries)) = draft.into_revision(&inventory, base_rev, MAX_EDIT_CHANGES) else { return reject(run, Why::EditAdmit); }; let candidate_rev = revision.n; diff --git a/crates/weksa/tests/cli.rs b/crates/weksa/tests/cli.rs index 65563ea..17cabfc 100644 --- a/crates/weksa/tests/cli.rs +++ b/crates/weksa/tests/cli.rs @@ -1875,3 +1875,31 @@ fn scene_put_and_dump() { assert!(!o.status.success()); assert!(!out(&o).contains("scene-1"), "{}", out(&o)); } + +#[test] +fn inventory_admit_numbers_a_draft_and_refuses_an_empty_one() { + let d = tempfile::tempdir().unwrap(); + assert!(weksa(d.path(), &["inventory", "seed"]).status.success()); + let path = |name: &str, text: &str| { + let p = d.path().join(name); + std::fs::write(&p, text).unwrap(); + p.to_str().unwrap().to_owned() + }; + let before = out(&weksa(d.path(), &["inventory", "dump"])); + + for (name, text) in [("empty.json", "{}"), ("junk.json", "not json at all")] { + let refused = weksa(d.path(), &["inventory", "admit", &path(name, text)]); + assert!(!refused.status.success(), "{}", out(&refused)); + assert_eq!(before, out(&weksa(d.path(), &["inventory", "dump"]))); + } + + let draft = path( + "draft.json", + r#"{"add":[{"family":"Concept","gloss":"bath_house"},{"family":"Frame","gloss":"fly_a_kite"}]}"#, + ); + let admitted = weksa(d.path(), &["inventory", "admit", &draft]); + assert!(admitted.status.success(), "{}", out(&admitted)); + assert!(out(&admitted).contains("admitted rev:2 with 2 entries")); + let dump = out(&weksa(d.path(), &["inventory", "dump"])); + assert!(dump.contains("bath_house") && dump.contains("fly_a_kite"), "{dump}"); +}