From cef3bde20e00ec88962dbcc66b73dd8a05046cb4 Mon Sep 17 00:00:00 2001 From: cjus Date: Sat, 14 Nov 2026 06:24:46 -0700 Subject: [PATCH] Knowledge: puzzle selection, a random order, a quality filter and facets (draft-1, cjus/chessgui#575) knowledge/puzzles takes order (easiest or random), seed and quality (reliable); the answer names the seed and the thresholds used. knowledge/puzzleFacets counts puzzles by theme and rating band, so a client can offer a choice without finding out that a combination is empty. New capability puzzleSelection. All optional: a node without it, or a request that doesn't send it, is unchanged. The design is measured, not guessed: on the real Lichess puzzles file (6,193,530 puzzles, chessdb main) a hash sort of the filtered set took 0.8 to 6.2 s and a random offset up to 6.9 s, while a forward scan from a random start took 0.1 to 1 ms for common themes. So random is a scan from a seeded start in the node's storage order, wrapping, and the spec says it is not a shuffle. Facets take 68 to 78 s to group at request time, so the spec has them kept as the node imports. Spec, types (cnp::knowledge) and a conformance session (knowledge-selection) change together. Co-Authored-By: Claude Sonnet 5.5 --- conformance/knowledge/selection.jsonl | 40 +++++++ crates/cnp/src/knowledge.rs | 149 +++++++++++++++++++++++++- crates/cnp/tests/knowledge.rs | 131 ++++++++++++++++++++++ spec/knowledge.md | 92 ++++++++++++++-- 4 files changed, 400 insertions(+), 12 deletions(-) create mode 100644 conformance/knowledge/selection.jsonl diff --git a/conformance/knowledge/selection.jsonl b/conformance/knowledge/selection.jsonl new file mode 100644 index 0000000..874e9bf --- /dev/null +++ b/conformance/knowledge/selection.jsonl @@ -0,0 +1,40 @@ +{"session":"knowledge-selection","description":"draft-1 knowledge, puzzle selection: a random pass names its seed and repeats from it, quality names its thresholds, facets answer with counts by band, and a seed without random order is refused (spec/knowledge.md, Order, seed and quality)","requires":["knowledge.puzzleSelection"]} +{"send":{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocol":"1.0","client":{"name":"cnp-conformance","version":"1.0"}}}} +{"expect":{"id":1,"result":{"capabilities":{"knowledge":{"contract":"draft-1","read":true,"puzzleSelection":true}}}}} +{"comment":"A random pass with no seed: the node picks one and says it. Asking again with that seed starts the same pass."} +{"send":{"jsonrpc":"2.0","id":2,"method":"knowledge/puzzles","params":{"theme":"noSuchThemeAnywhere","order":"random","limit":5}}} +{"expect":{"id":2,"result":{"puzzles":[],"sources":[],"seed":"$bind:seed"}}} +{"send":{"jsonrpc":"2.0","id":3,"method":"knowledge/puzzles","params":{"theme":"noSuchThemeAnywhere","order":"random","seed":"$ref:seed","limit":5}}} +{"expect":{"id":3,"result":{"puzzles":[],"sources":[],"seed":"$ref:seed"}}} +{"comment":"A seed the client chose is the seed the answer names, at both ends of its range."} +{"send":{"jsonrpc":"2.0","id":4,"method":"knowledge/puzzles","params":{"order":"random","seed":0,"limit":1}}} +{"expect":{"id":4,"result":{"puzzles":"$array","sources":"$array","seed":0}}} +{"send":{"jsonrpc":"2.0","id":5,"method":"knowledge/puzzles","params":{"order":"random","seed":4294967295,"limit":1}}} +{"expect":{"id":5,"result":{"puzzles":"$array","sources":"$array","seed":4294967295}}} +{"comment":"Quality names the thresholds it used; a plain request names none."} +{"send":{"jsonrpc":"2.0","id":6,"method":"knowledge/puzzles","params":{"theme":"noSuchThemeAnywhere","quality":"reliable"}}} +{"expect":{"id":6,"result":{"puzzles":[],"sources":[],"quality":{"minPlays":"$integer","minPopularity":"$integer","maxRatingDeviation":"$integer"},"seed":"$absent"}}} +{"send":{"jsonrpc":"2.0","id":7,"method":"knowledge/puzzles","params":{"theme":"noSuchThemeAnywhere"}}} +{"expect":{"id":7,"result":{"puzzles":[],"sources":[],"quality":"$absent","seed":"$absent"}}} +{"comment":"Facets: the node's bands, the total in each, and a count in each for every theme listed. The params may be left out. A theme nobody has is not listed."} +{"send":{"jsonrpc":"2.0","id":8,"method":"knowledge/puzzleFacets"}} +{"expect":{"id":8,"result":{"bands":"$array","total":"$array","themes":"$array","sources":"$array","quality":"$absent"}}} +{"send":{"jsonrpc":"2.0","id":9,"method":"knowledge/puzzleFacets","params":{"quality":"reliable"}}} +{"expect":{"id":9,"result":{"bands":"$array","total":"$array","themes":"$array","sources":"$array","quality":{"minPlays":"$integer","minPopularity":"$integer","maxRatingDeviation":"$integer"}}}} +{"comment":"Refused: a seed with the default order or with easiest, an order or a quality that isn't one, a seed out of range, and facets for a quality that isn't one."} +{"send":{"jsonrpc":"2.0","id":10,"method":"knowledge/puzzles","params":{"seed":3}}} +{"expect":{"id":10,"error":{"code":-32602}}} +{"send":{"jsonrpc":"2.0","id":11,"method":"knowledge/puzzles","params":{"order":"easiest","seed":3}}} +{"expect":{"id":11,"error":{"code":-32602}}} +{"send":{"jsonrpc":"2.0","id":12,"method":"knowledge/puzzles","params":{"order":"shuffled"}}} +{"expect":{"id":12,"error":{"code":-32602}}} +{"send":{"jsonrpc":"2.0","id":13,"method":"knowledge/puzzles","params":{"quality":"best"}}} +{"expect":{"id":13,"error":{"code":-32602}}} +{"send":{"jsonrpc":"2.0","id":14,"method":"knowledge/puzzles","params":{"order":"random","seed":4294967296}}} +{"expect":{"id":14,"error":{"code":-32602}}} +{"send":{"jsonrpc":"2.0","id":15,"method":"knowledge/puzzleFacets","params":{"quality":"best"}}} +{"expect":{"id":15,"error":{"code":-32602}}} +{"send":{"jsonrpc":"2.0","id":16,"method":"shutdown"}} +{"expect":{"id":16,"result":null}} +{"send":{"jsonrpc":"2.0","method":"exit"}} +{"expectExit":0} diff --git a/crates/cnp/src/knowledge.rs b/crates/cnp/src/knowledge.rs index c79df42..cd341c5 100644 --- a/crates/cnp/src/knowledge.rs +++ b/crates/cnp/src/knowledge.rs @@ -4,9 +4,10 @@ //! //! The import half: [`StartImport`] gives the node a file and a [`Manifest`] saying where it is //! from, answers at once, and the import runs on; [`ListImports`] reports where each has got -//! to, and [`CancelImport`] stops one. The read half: [`FindPuzzles`] by theme and rating, -//! [`FindAnalyses`] of a position, and [`FindExercises`] by theme, each from complete imports -//! only and naming its [`Source`]s. +//! to, and [`CancelImport`] stops one. The read half: [`FindPuzzles`] by theme and rating (easiest +//! first, or from a random point, and only the reliable ones if asked), [`FindPuzzleFacets`] +//! for how many there are of each theme and rating band, [`FindAnalyses`] of a position, and +//! [`FindExercises`] by theme, each from complete imports only and naming its [`Source`]s. //! //! **The node doesn't download.** The client fetches the file, where it can show the licence, //! the size and the progress. The node verifies the file against the manifest's checksum before @@ -70,6 +71,10 @@ pub struct Capabilities { /// `knowledge/puzzles`, `knowledge/analyses` and `knowledge/exercises` are available. #[serde(default)] pub read: bool, + /// `knowledge/puzzles` takes `order`, `seed` and `quality`, and `knowledge/puzzleFacets` + /// is available. A client sends none of them to a node that doesn't offer it. + #[serde(default)] + pub puzzle_selection: bool, /// The kinds of file the node can import ([`KIND_LICHESS_PUZZLES`] and the like). #[serde(default)] pub kinds: Vec, @@ -384,6 +389,41 @@ fn valid_page( Ok(()) } +/// The order `knowledge/puzzles` answers in. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Order { + /// By rating, lowest first, then by identity: the same puzzles in the same order every + /// time. The default. + #[default] + Easiest, + /// From a point the `seed` picks, in the node's own storage order, wrapping round to the + /// start, so every matching puzzle comes once. Not a shuffle: see the spec. + Random, +} + +/// A named filter on how far a puzzle's rating can be trusted. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Quality { + /// Puzzles the source's own counts support: played enough, liked, and with a settled + /// rating. The node chooses the thresholds and says them in [`QualityThresholds`]. + Reliable, +} + +/// The thresholds a node used for a [`Quality`] filter, so a client can show them and a +/// puzzle's absence from a result can be explained. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct QualityThresholds { + /// At least this many plays. + pub min_plays: i64, + /// At least this popularity. + pub min_popularity: i64, + /// At most this rating deviation. + pub max_rating_deviation: i64, +} + /// `knowledge/puzzles`' params: all optional, and the params may be left out. #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(from = "Option", rename_all = "camelCase")] @@ -403,6 +443,17 @@ pub struct PuzzlesParams { /// How many, 1 to [`MAX_LIMIT`]; [`DEFAULT_LIMIT`] if left out. #[serde(skip_serializing_if = "Option::is_none")] pub limit: Option, + /// The order: [`Order::Easiest`] if left out. Needs [`Capabilities::puzzle_selection`] + /// for anything else. + #[serde(skip_serializing_if = "Option::is_none")] + pub order: Option, + /// Where a [`Order::Random`] pass starts, so a session can be repeated: the node picks one + /// and answers with it if this is left out. Only with [`Order::Random`]. + #[serde(skip_serializing_if = "Option::is_none")] + pub seed: Option, + /// Only puzzles that pass this filter. Needs [`Capabilities::puzzle_selection`]. + #[serde(skip_serializing_if = "Option::is_none")] + pub quality: Option, } /// [`PuzzlesParams`] as they are read, from an object or from nothing. @@ -419,6 +470,12 @@ struct PuzzlesFields { after: Option, #[serde(default)] limit: Option, + #[serde(default)] + order: Option, + #[serde(default)] + seed: Option, + #[serde(default)] + quality: Option, } impl From> for PuzzlesParams { @@ -430,6 +487,9 @@ impl From> for PuzzlesParams { max_rating: f.max_rating, after: f.after, limit: f.limit, + order: f.order, + seed: f.seed, + quality: f.quality, } } } @@ -438,6 +498,9 @@ impl PuzzlesParams { /// Checks the params' shapes, or says what is wrong. pub fn validate(&self) -> Result<(), String> { valid_page(self.limit, &self.after, &self.theme)?; + if self.seed.is_some() && self.order != Some(Order::Random) { + return Err("a seed is for order random".into()); + } if let (Some(min), Some(max)) = (self.min_rating, self.max_rating) && min > max { @@ -483,16 +546,23 @@ pub struct Puzzle { /// `knowledge/puzzles`' result. #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct PuzzlesResult { - /// The puzzles, easiest first. + /// The puzzles, in the order asked for. pub puzzles: Vec, /// The sources of these puzzles. pub sources: Vec, /// Where the next page starts, if more may follow. #[serde(default, skip_serializing_if = "Option::is_none")] pub next: Option, + /// For [`Order::Random`]: the seed the pass started from, the one asked for or the one the + /// node picked. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub seed: Option, + /// When a [`Quality`] filter was asked for: the thresholds it used. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub quality: Option, } -/// Puzzles from complete imports, easiest first, by theme and rating. +/// Puzzles from complete imports, by theme and rating, easiest first or from a random point. pub enum FindPuzzles {} impl RequestMethod for FindPuzzles { const NAME: &'static str = "knowledge/puzzles"; @@ -500,6 +570,75 @@ impl RequestMethod for FindPuzzles { type Result = PuzzlesResult; } +/// `knowledge/puzzleFacets`' params: may be left out. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(from = "Option", rename_all = "camelCase")] +pub struct FacetsParams { + /// Count only puzzles that pass this filter. + #[serde(skip_serializing_if = "Option::is_none")] + pub quality: Option, +} + +/// [`FacetsParams`] as they are read, from an object or from nothing. +#[derive(Default, Deserialize)] +#[serde(rename_all = "camelCase")] +struct FacetsFields { + #[serde(default)] + quality: Option, +} + +impl From> for FacetsParams { + fn from(fields: Option) -> FacetsParams { + FacetsParams { + quality: fields.unwrap_or_default().quality, + } + } +} + +/// A rating band the node counts puzzles in. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Band { + /// The lowest rating in the band. + pub min_rating: u32, + /// The highest rating in the band; absent for the last, which has none. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub max_rating: Option, +} + +/// How many puzzles a theme has in each [`Band`]. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ThemeCounts { + /// The theme, as the source writes it. + pub theme: String, + /// One count for each band of [`FacetsResult::bands`], in its order. + pub counts: Vec, +} + +/// `knowledge/puzzleFacets`' result. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct FacetsResult { + /// The rating bands the counts are in, lowest first. A client uses these, never its own. + pub bands: Vec, + /// All puzzles in each band, whatever their themes. + pub total: Vec, + /// Each theme that has at least one puzzle, with its counts. + pub themes: Vec, + /// The sources of the puzzles counted. + pub sources: Vec, + /// When a [`Quality`] filter was asked for: the thresholds it used. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub quality: Option, +} + +/// How many puzzles each theme has in each rating band, from complete imports. +pub enum FindPuzzleFacets {} +impl RequestMethod for FindPuzzleFacets { + const NAME: &'static str = "knowledge/puzzleFacets"; + type Params = FacetsParams; + type Result = FacetsResult; +} + /// `knowledge/analyses`' params. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct AnalysesParams { diff --git a/crates/cnp/tests/knowledge.rs b/crates/cnp/tests/knowledge.rs index f77c2b9..d0675be 100644 --- a/crates/cnp/tests/knowledge.rs +++ b/crates/cnp/tests/knowledge.rs @@ -196,6 +196,19 @@ fn the_specs_json_examples_are_valid() { .unwrap() .validate() .unwrap(); + } else if value.get("bands").is_some() { + let r: knowledge::FacetsResult = serde_json::from_value(value).unwrap(); + // One count for each band, for the total and for every theme. + assert_eq!(r.total.len(), r.bands.len()); + assert!(r.themes.iter().all(|t| t.counts.len() == r.bands.len())); + assert_eq!(r.bands.last().unwrap().max_rating, None); + assert!( + r.bands + .windows(2) + .all(|w| w[0].max_rating == Some(w[1].min_rating - 1)) + ); + } else if value.get("quality").is_some() { + serde_json::from_value::(value).unwrap(); } else if value.get("fen").is_some() { serde_json::from_value::(value) .unwrap() @@ -330,3 +343,121 @@ fn perspectives_and_candidates_are_written_as_the_spec_has_them() { json!({"mate":1,"moves":"h5f7"}) ); } + +#[test] +fn the_selection_capability_is_its_own_switch_and_older_nodes_lack_it() { + let c: knowledge::Capabilities = + serde_json::from_value(json!({"read":true,"contract":"draft-1"})).unwrap(); + assert!(c.read && !c.puzzle_selection); + let c: knowledge::Capabilities = + serde_json::from_value(json!({"read":true,"puzzleSelection":true,"contract":"draft-1"})) + .unwrap(); + assert!(c.puzzle_selection); + assert_eq!( + serde_json::to_value(&c).unwrap()["puzzleSelection"], + json!(true) + ); +} + +#[test] +fn order_seed_and_quality_are_written_as_the_spec_has_them() { + let p: knowledge::PuzzlesParams = serde_json::from_value( + json!({"theme":"fork","quality":"reliable","order":"random","seed":7,"limit":1}), + ) + .unwrap(); + assert_eq!( + (p.order, p.seed, p.quality), + ( + Some(knowledge::Order::Random), + Some(7), + Some(knowledge::Quality::Reliable) + ) + ); + assert!(p.validate().is_ok()); + // What was not asked for is not written, so an older node sees nothing new. + let plain = knowledge::PuzzlesParams { + theme: Some("fork".into()), + ..Default::default() + }; + assert_eq!( + serde_json::to_value(&plain).unwrap(), + json!({"theme":"fork"}) + ); + let largest: knowledge::PuzzlesParams = + serde_json::from_value(json!({"order":"random","seed":4294967295u32})).unwrap(); + assert!(largest.validate().is_ok()); +} + +#[test] +fn a_seed_needs_random_order_and_unknown_values_are_refused() { + for bad in [json!({"seed":3}), json!({"order":"easiest","seed":3})] { + let p: knowledge::PuzzlesParams = serde_json::from_value(bad.clone()).unwrap(); + assert!(p.validate().unwrap_err().contains("seed"), "{bad}"); + } + // An unknown order or quality is not read at all, which the node answers as invalid params. + for bad in [ + json!({"order":"shuffled"}), + json!({"order":"Random"}), + json!({"quality":"best"}), + json!({"seed":-1,"order":"random"}), + json!({"seed":4294967296u64,"order":"random"}), + ] { + assert!( + serde_json::from_value::(bad.clone()).is_err(), + "{bad}" + ); + } +} + +#[test] +fn a_puzzles_answer_names_its_seed_and_thresholds_only_when_there_are_any() { + let plain: knowledge::PuzzlesResult = + serde_json::from_value(json!({"puzzles":[],"sources":[]})).unwrap(); + assert_eq!((plain.seed, plain.quality), (None, None)); + assert_eq!( + serde_json::to_value(&plain).unwrap(), + json!({"puzzles":[],"sources":[]}) + ); + let asked: knowledge::PuzzlesResult = serde_json::from_value(json!({"puzzles":[],"sources":[], + "seed":7,"quality":{"minPlays":100,"minPopularity":80,"maxRatingDeviation":110}})) + .unwrap(); + assert_eq!(asked.seed, Some(7)); + assert_eq!(asked.quality.unwrap().min_plays, 100); +} + +#[test] +fn facets_params_may_be_left_out_and_the_answer_is_counts_by_band() { + let none: knowledge::FacetsParams = serde_json::from_value(json!(null)).unwrap(); + assert_eq!(none, knowledge::FacetsParams::default()); + assert_eq!(serde_json::to_value(&none).unwrap(), json!({})); + let request = Request::new::( + 1, + &knowledge::FacetsParams { + quality: Some(knowledge::Quality::Reliable), + }, + ); + assert_eq!(request.method, "knowledge/puzzleFacets"); + assert_eq!( + request + .params::() + .unwrap() + .quality, + Some(knowledge::Quality::Reliable) + ); + let answer: knowledge::FacetsResult = serde_json::from_value(json!({ + "bands":[{"minRating":0,"maxRating":999},{"minRating":1000}], + "total":[5,7], + "themes":[{"theme":"fork","counts":[2,3]}], + "sources":[]})) + .unwrap(); + assert_eq!(answer.bands[1].max_rating, None); + assert_eq!(answer.themes[0].counts, [2, 3]); + assert_eq!( + serde_json::to_value(answer.bands[0]).unwrap(), + json!({"minRating":0,"maxRating":999}) + ); + assert_eq!( + serde_json::to_value(answer.bands[1]).unwrap(), + json!({"minRating":1000}) + ); +} diff --git a/spec/knowledge.md b/spec/knowledge.md index a4fec41..419c7d4 100644 --- a/spec/knowledge.md +++ b/spec/knowledge.md @@ -10,7 +10,8 @@ and [backups](backups.md) extensions. The knowledge is what the player didn't make: puzzles, evaluated positions and strategic test positions from outside sources. The domain has two halves: **a client hands the node a file it has downloaded, and the node imports it** (`import`), and **a client asks for what was -imported** (`read`): puzzles by theme and rating, the analyses of a position, and strategic +imported** (`read`): puzzles by theme and rating (in order of difficulty or from a random +point, and how many there are of each theme and rating), the analyses of a position, and strategic exercises by theme. The node doesn't download. The client fetches the file, where it can show the licence, the @@ -20,13 +21,14 @@ the checksum the client says it has, so a damaged or swapped download is refused ## Capability ```json -{"capabilities": {"knowledge": {"import": true, "read": true, +{"capabilities": {"knowledge": {"import": true, "read": true, "puzzleSelection": true, "kinds": ["lichess-puzzles", "lichess-evals", "sts-epd"], "contract": "draft-1"}}} ``` `import` offers `knowledge/import`, `knowledge/status` and `knowledge/cancel`; `read` offers -`knowledge/puzzles`, `knowledge/analyses` and `knowledge/exercises`. A missing switch is -false. `kinds` are the kinds of file the node can import; a request for another kind is +`knowledge/puzzles`, `knowledge/analyses` and `knowledge/exercises`; `puzzleSelection` offers +`knowledge/puzzles`' `order`, `seed` and `quality`, and `knowledge/puzzleFacets`. A missing +switch is false. `kinds` are the kinds of file the node can import; a request for another kind is refused. A client sends knowledge requests only to a node whose `contract` is `draft-1`. A node without the domain answers them with Method not found (-32601). @@ -187,7 +189,7 @@ kind of request, is -32602. `limit` is 1–100, 20 if left out. Every field is optional. `theme` is a theme as the source writes it (1–64 letters and digits); the ratings are whole numbers, `minRating` no more than `maxRating`. Puzzles come **easiest -first** (by rating, then by identity). Answers: +first** (by rating, then by identity), unless `order` says otherwise (below). Answers: ```json {"puzzles": [{"id": "00008", "source": "3f1c0d…9a", @@ -208,6 +210,72 @@ any mate does; that is the source's rule, and the node keeps no chess to check i `ratingDeviation`, `popularity` and `plays` are the source's. `gameUrl` and `openingTags` are there when the source gave them. +#### Order, seed and quality + +A node that offers `puzzleSelection` also takes three optional fields; a client sends them to no +other node. + +```json +{"theme": "fork", "minRating": 1200, "maxRating": 1599, "quality": "reliable", + "order": "random", "seed": 7, "limit": 1} +``` + +- `order` is `easiest` (the default, as above) or `random`. A random pass starts at a point the + `seed` picks in **the node's own storage order** and goes on from there, wrapping round to + the start, so a pass reaches every matching puzzle once, however few match. `seed` is a whole + number, 0 to 4294967295; the same seed over the same imports starts at the same place, so a + session can be repeated. Left out, the node picks one, and **the answer's `seed` says which**. + `next` continues the pass, and is absent once every matching puzzle has been given. A + `seed` with any other order is -32602. +- Random is **not a shuffle**. The puzzles of one page are neighbours in the node's storage + order, and which puzzle a seed starts at is not even over the matching puzzles when they sit + in clusters of that order (a puzzle after a long gap of non-matches is likelier to start a + pass). For a file whose order has nothing to do with rating or theme, as Lichess's puzzle file + does not, a start is close to even. A client that wants one varied puzzle asks for `limit: 1` + with a new seed each time; one that wants a shuffled set of twenty asks for several and + shuffles them. +- `quality` is `reliable` or absent. It leaves out puzzles whose rating the source's own counts + don't support. **The node chooses the thresholds** and the answer's `quality` names them + (`{"minPlays": 100, "minPopularity": 80, "maxRatingDeviation": 110}`: at least that many + plays, at least that popularity, at most that rating deviation), so a client can say what + *reliable* meant. The answer has `quality` only when it was asked for. A client doesn't + assume one node's thresholds are another's. + +### `knowledge/puzzleFacets` + +How many puzzles each theme has in each rating band: what a client needs to offer a choice of +theme and difficulty without finding out by asking that a combination is empty. Needs +`puzzleSelection`. + +```json +{"quality": "reliable"} +``` + +`quality` is as in `knowledge/puzzles`, and the params may be left out. Answers (abridged, with +illustrative counts): + +```json +{"bands": [{"minRating": 0, "maxRating": 799}, {"minRating": 800, "maxRating": 1199}, + {"minRating": 1200, "maxRating": 1599}, {"minRating": 1600, "maxRating": 1999}, + {"minRating": 2000, "maxRating": 2399}, {"minRating": 2400}], + "total": [500000, 1700000, 1500000, 1200000, 800000, 300000], + "themes": [{"theme": "fork", "counts": [31694, 213387, 158289, 113726, 53232, 15428]}], + "sources": [{ ... a source ... }], + "quality": {"minPlays": 100, "minPopularity": 80, "maxRatingDeviation": 110}} +``` + +- `bands` are **the node's**: a client uses the answer's bands and never its own, since a node + may count in others. They are in order from the lowest rating, each starting where the last + ended plus one; the last has no `maxRating`. +- `total` and each theme's `counts` have one count for each band, in the order of `bands`. + `total` is every puzzle in the band, and a puzzle with several themes is counted under each + of them, so the themes' counts add up to more than `total`. +- A theme with no puzzle in any band is not listed. A band with none has a 0. +- Counts are of the puzzles of **complete imports** and are the same as asking + `knowledge/puzzles` for that theme, band and quality would reach, which a node can keep as it + imports, not work out at each request. A client doesn't treat a count as current after a new + import: it asks again. + ### `knowledge/analyses` ```json @@ -276,7 +344,7 @@ exercise names its `source`, and a client never mixes the points of two sources. | Error | Meaning | Client action | | --- | --- | --- | -| -32602 | Invalid params: an unknown kind, or a malformed path, manifest, checksum, theme, rating, limit, cursor or FEN | Correct the request | +| -32602 | Invalid params: an unknown kind, or a malformed path, manifest, checksum, theme, rating, limit, cursor, FEN, order or quality, or a seed with any order but random | Correct the request | | -32013 | Busy: another import is running | Wait for it, or cancel it | | -32022 | Storage or I/O failed | Ask `knowledge/status` for the import's state | | -32025 | No import with that identity | List the imports | @@ -285,5 +353,15 @@ exercise names its `source`, and a client never mixes the points of two sources. ## Not in draft 1 -Progress notifications in place of polling, reading the records an import rejected, removing +Several themes in one request (and excluding a theme), choosing the puzzles of the day, +progress notifications in place of polling, reading the records an import rejected, removing an import's knowledge, searching puzzles by opening or by a position, and Lumbra's OTB games. + +## Changes + +- **draft 1, 2026-11-14**, added to the draft: **puzzle selection** (capability + `puzzleSelection`, `knowledge/puzzles`' `order`, `seed` and `quality`, and + `knowledge/puzzleFacets`), so a client can offer varied puzzles of a theme and rating band + that rate reliably, and say beforehand which combinations are empty (cjus/chessgui#575, + cjus/chess-node-protocol#29). All optional: a node that doesn't offer it, or a request that + doesn't send it, is unchanged.