demystify 0.4.0

A constraint solving tool for explaining puzzles
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Name database for matching MUS fingerprints to human-recognised techniques.
//!
//! The database is a directory of TOML files, one per `$#KIND`. Each entry
//! associates a canonical-form `MusFingerprint` string with a human technique
//! name and (optionally) the family-group whose member labels should be
//! prefixed at display time to surface the concrete orientation.
//!
//! Schema:
//! ```toml
//! [[strategy]]
//! name = "Hidden single"
//! fingerprint = "..."         # opaque MusFingerprint canonical-form string
//! orientation_group = "unit_contains"   # optional
//! ```

use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fs;
use std::path::Path;

use anyhow::{Context, Result};
use serde::Deserialize;
use tracing::{info, warn};

use crate::problem::musdict::MusContext;
use crate::problem::parse::PuzzleParse;

use super::fingerprint::MusFingerprint;

/// One named technique entry in the database.
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Strategy {
    /// Human-readable technique name (e.g. "Hidden single").
    pub name: String,
    /// Canonical-form fingerprint string this strategy matches.
    pub fingerprint: String,
    /// Optional: name of the family-group whose member label should be used
    /// to prefix the technique name at display time. The display logic looks
    /// up `families[orientation_group].members[raw_family]` for each MUS
    /// constraint whose raw family is in the group, and uses the unique
    /// resulting label as the prefix.
    #[serde(default)]
    pub orientation_group: Option<String>,
}

/// Top-level structure of one strategy DB file.
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct DbFile {
    #[serde(default)]
    strategy: Vec<Strategy>,
}

/// In-memory database keyed by `$#KIND` then by fingerprint string.
#[derive(Debug, Default, Clone)]
pub struct Database {
    by_kind: BTreeMap<String, BTreeMap<String, Strategy>>,
}

impl Database {
    /// Empty database — every lookup misses.  Useful for tests and the case
    /// where no DB directory is configured.
    #[must_use]
    pub fn empty() -> Self {
        Self::default()
    }

    /// Load every `*.toml` file in `dir` as a strategy DB. The file basename
    /// (without extension) becomes the `$#KIND` key. Returns an empty
    /// database (with a warning logged) if `dir` does not exist.
    ///
    /// Per-file parse errors propagate as `Err`; a malformed DB is treated
    /// as a configuration bug, not silently swallowed.
    pub fn load_from_dir(dir: &Path) -> Result<Self> {
        let mut db = Self::empty();
        if !dir.exists() {
            warn!(target: "named_strategy",
                "strategy DB directory {:?} does not exist; named techniques disabled", dir);
            return Ok(db);
        }
        for entry in
            fs::read_dir(dir).with_context(|| format!("reading strategy DB directory {dir:?}"))?
        {
            let entry = entry?;
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) != Some("toml") {
                continue;
            }
            let kind = path
                .file_stem()
                .and_then(|s| s.to_str())
                .with_context(|| format!("DB file {path:?} has non-UTF-8 stem"))?
                .to_string();
            let content = fs::read_to_string(&path)
                .with_context(|| format!("reading strategy DB file {path:?}"))?;
            let parsed: DbFile = toml::from_str(&content)
                .with_context(|| format!("parsing strategy DB file {path:?}"))?;
            let n = parsed.strategy.len();
            db.add_kind(&kind, parsed.strategy)?;
            info!(target: "named_strategy", "loaded {n} strategies for kind '{kind}' from {path:?}");
        }
        Ok(db)
    }

    /// Insert all strategies for a single `$#KIND`. Errors on duplicate
    /// fingerprints within the same kind.
    pub fn add_kind(&mut self, kind: &str, strategies: Vec<Strategy>) -> Result<()> {
        let entry = self.by_kind.entry(kind.to_string()).or_default();
        for s in strategies {
            if let Some(existing) = entry.get(&s.fingerprint) {
                anyhow::bail!(
                    "strategy DB '{kind}': two entries share fingerprint '{}': '{}' and '{}'",
                    s.fingerprint,
                    existing.name,
                    s.name
                );
            }
            entry.insert(s.fingerprint.clone(), s);
        }
        Ok(())
    }

    /// Look up the strategy for a fingerprint within a `$#KIND` namespace.
    #[must_use]
    pub fn lookup(&self, kind: &str, fingerprint: &MusFingerprint) -> Option<&Strategy> {
        self.by_kind.get(kind)?.get(fingerprint.as_str())
    }

    /// Return every strategy for a given `$#KIND` whose `orientation_group`
    /// is set but does not appear in `declared_groups`. Used to surface
    /// curation errors (typo in DB, or model dropped a `$#FAMILY` line)
    /// loudly rather than silently dropping prefixes at display time.
    #[must_use]
    pub fn dangling_orientation_groups<'a>(
        &'a self,
        kind: &str,
        declared_groups: &std::collections::BTreeSet<&str>,
    ) -> Vec<&'a Strategy> {
        let Some(entries) = self.by_kind.get(kind) else {
            return Vec::new();
        };
        entries
            .values()
            .filter(|s| {
                s.orientation_group
                    .as_deref()
                    .is_some_and(|g| !declared_groups.contains(g))
            })
            .collect()
    }

    /// Total number of strategies across all kinds.
    #[must_use]
    pub fn len(&self) -> usize {
        self.by_kind.values().map(BTreeMap::len).sum()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.by_kind.values().all(BTreeMap::is_empty)
    }
}

/// Compose the human-readable display name for a matched strategy.
///
/// If the strategy has `orientation_group` set, scan the MUS constraints,
/// take their group-relative member labels via
/// `families[orientation_group].members[raw_family]`, and prefix the
/// (unique) label to the technique name. If the matched constraints disagree
/// on label or no orientation group is set, return the bare technique name.
#[must_use]
pub fn display_name(strategy: &Strategy, mus: &MusContext, parse: &PuzzleParse) -> String {
    let Some(group_id) = strategy.orientation_group.as_deref() else {
        return strategy.name.clone();
    };
    let Some(family) = parse.eprime.families.get(group_id) else {
        return strategy.name.clone();
    };
    let labels: BTreeSet<&String> = mus
        .mus
        .iter()
        .filter_map(|lit| {
            let raw = parse.constraints.family_of(lit)?;
            family.members.get(raw.as_str())
        })
        .collect();
    if labels.len() == 1 {
        format!("{} {}", labels.iter().next().unwrap(), strategy.name)
    } else {
        strategy.name.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_database_misses_everything() {
        let db = Database::empty();
        let fp = MusFingerprint {
            canonical: "anything".into(),
        };
        assert!(db.lookup("Sudoku", &fp).is_none());
        assert!(db.is_empty());
    }

    #[test]
    fn lookup_within_kind() {
        let mut db = Database::empty();
        db.add_kind(
            "Sudoku",
            vec![
                Strategy {
                    name: "Naked single".into(),
                    fingerprint: "ns-fp".into(),
                    orientation_group: None,
                },
                Strategy {
                    name: "Hidden single".into(),
                    fingerprint: "hs-fp".into(),
                    orientation_group: Some("unit_contains".into()),
                },
            ],
        )
        .unwrap();

        let fp_ns = MusFingerprint {
            canonical: "ns-fp".into(),
        };
        let fp_hs = MusFingerprint {
            canonical: "hs-fp".into(),
        };
        let fp_other = MusFingerprint {
            canonical: "missing".into(),
        };
        assert_eq!(db.lookup("Sudoku", &fp_ns).unwrap().name, "Naked single");
        assert_eq!(db.lookup("Sudoku", &fp_hs).unwrap().name, "Hidden single");
        assert!(db.lookup("Sudoku", &fp_other).is_none());
        // Wrong kind: no leakage.
        assert!(db.lookup("Binairo", &fp_ns).is_none());
        assert_eq!(db.len(), 2);
    }

    #[test]
    fn duplicate_fingerprint_within_kind_fails() {
        let mut db = Database::empty();
        let result = db.add_kind(
            "Sudoku",
            vec![
                Strategy {
                    name: "A".into(),
                    fingerprint: "same".into(),
                    orientation_group: None,
                },
                Strategy {
                    name: "B".into(),
                    fingerprint: "same".into(),
                    orientation_group: None,
                },
            ],
        );
        assert!(result.is_err(), "duplicate fingerprint should fail");
    }

    #[test]
    fn load_from_dir_picks_up_toml_files() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("Sudoku.toml"),
            r#"
[[strategy]]
name = "Naked single"
fingerprint = "ns-fp"

[[strategy]]
name = "Hidden single"
fingerprint = "hs-fp"
orientation_group = "unit_contains"
"#,
        )
        .unwrap();
        std::fs::write(tmp.path().join("notes.md"), "ignored").unwrap();

        let db = Database::load_from_dir(tmp.path()).unwrap();
        assert_eq!(db.len(), 2);
        let fp = MusFingerprint {
            canonical: "hs-fp".into(),
        };
        let s = db.lookup("Sudoku", &fp).unwrap();
        assert_eq!(s.name, "Hidden single");
        assert_eq!(s.orientation_group.as_deref(), Some("unit_contains"));
    }

    #[test]
    fn load_from_dir_missing_dir_returns_empty() {
        let db = Database::load_from_dir(Path::new("/no/such/dir/12345")).unwrap();
        assert!(db.is_empty());
    }

    #[test]
    fn load_from_dir_malformed_file_errors() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("Bad.toml"), "this is not valid toml ===").unwrap();
        let result = Database::load_from_dir(tmp.path());
        assert!(result.is_err(), "malformed TOML should fail loudly");
    }

    #[test]
    fn dangling_orientation_groups_finds_typos() {
        let mut db = Database::empty();
        db.add_kind(
            "Sudoku",
            vec![
                Strategy {
                    name: "Naked single".into(),
                    fingerprint: "ns".into(),
                    orientation_group: None,
                },
                Strategy {
                    name: "Hidden single".into(),
                    fingerprint: "hs".into(),
                    orientation_group: Some("unit_contains".into()),
                },
                Strategy {
                    name: "Pointing pair".into(),
                    fingerprint: "pp".into(),
                    orientation_group: Some("typo_group".into()),
                },
            ],
        )
        .unwrap();

        let declared: BTreeSet<&str> = ["unit_contains", "unit_atmost"].into_iter().collect();
        let bad = db.dangling_orientation_groups("Sudoku", &declared);
        assert_eq!(bad.len(), 1, "should flag the one entry with typo");
        assert_eq!(bad[0].name, "Pointing pair");

        // Wrong kind: no entries at all to check.
        assert!(
            db.dangling_orientation_groups("Binairo", &declared)
                .is_empty()
        );

        // Empty declared set: every set orientation_group becomes dangling.
        let empty = BTreeSet::new();
        assert_eq!(db.dangling_orientation_groups("Sudoku", &empty).len(), 2);
    }

    #[test]
    fn unknown_field_in_strategy_errors() {
        // `deny_unknown_fields` should catch typos like `orientation_grup`
        // (missing 'o') so they don't silently noop.
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("Sudoku.toml"),
            r#"
[[strategy]]
name = "Hidden single"
fingerprint = "row_contains;"
orientation_grup = "unit_contains"
"#,
        )
        .unwrap();
        let result = Database::load_from_dir(tmp.path());
        assert!(result.is_err(), "typo'd field should fail to load");
    }

    #[test]
    fn empty_toml_file_yields_zero_strategies() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("Empty.toml"),
            "# no strategies yet — populate via demystify-fingerprint\n",
        )
        .unwrap();
        let db = Database::load_from_dir(tmp.path()).unwrap();
        assert!(db.is_empty());
    }

    /// Minimal PuzzleParse builder for display_name tests. Inserts the given
    /// (lit, family, description) triples into the constraint store, and
    /// attaches the supplied family-group annotations. No SAT instance.
    fn build_parse_for_display(
        families: BTreeMap<String, crate::problem::parse::Family>,
        constraints: &[(rustsat::types::Lit, &str, &str)],
    ) -> PuzzleParse {
        let mut parse = PuzzleParse::new_from_eprime(
            BTreeSet::new(),
            BTreeSet::new(),
            BTreeMap::new(),
            BTreeMap::new(),
            BTreeMap::new(),
            Some("Sudoku".into()),
            Vec::new(),
            families,
            Vec::new(),
        );
        for (lit, family, desc) in constraints {
            parse
                .constraints
                .insert(*lit, (*family).to_string(), (*desc).to_string(), Vec::new())
                .expect("insert should succeed");
        }
        parse
    }

    fn unit_contains_family() -> BTreeMap<String, crate::problem::parse::Family> {
        let mut families = BTreeMap::new();
        families.insert(
            "unit_contains".to_string(),
            crate::problem::parse::Family {
                label: "Unit contains".into(),
                members: [
                    ("row_contains".to_string(), "Row".to_string()),
                    ("con_contains".to_string(), "Column".to_string()),
                    ("box_contains".to_string(), "Box".to_string()),
                ]
                .into_iter()
                .collect(),
            },
        );
        families
    }

    #[test]
    fn display_name_with_no_orientation_group() {
        let parse = build_parse_for_display(BTreeMap::new(), &[]);
        let strategy = Strategy {
            name: "Naked single".into(),
            fingerprint: "fp".into(),
            orientation_group: None,
        };
        let mus = MusContext {
            lits: BTreeSet::new(),
            mus: BTreeSet::new(),
        };
        assert_eq!(display_name(&strategy, &mus, &parse), "Naked single");
    }

    #[test]
    fn display_name_prefixes_unique_orientation_label() {
        let lit = rustsat::types::Lit::from_ipasir(1).unwrap();
        let parse = build_parse_for_display(
            unit_contains_family(),
            &[(lit, "row_contains", "row_contains[3, 5]")],
        );
        let strategy = Strategy {
            name: "hidden single".into(),
            fingerprint: "fp".into(),
            orientation_group: Some("unit_contains".into()),
        };
        let mus = MusContext {
            lits: BTreeSet::new(),
            mus: BTreeSet::from([lit]),
        };
        assert_eq!(display_name(&strategy, &mus, &parse), "Row hidden single");
    }

    #[test]
    fn display_name_box_orientation() {
        let lit = rustsat::types::Lit::from_ipasir(7).unwrap();
        let parse = build_parse_for_display(
            unit_contains_family(),
            &[(lit, "box_contains", "box_contains[0, 1, 4]")],
        );
        let strategy = Strategy {
            name: "hidden single".into(),
            fingerprint: "fp".into(),
            orientation_group: Some("unit_contains".into()),
        };
        let mus = MusContext {
            lits: BTreeSet::new(),
            mus: BTreeSet::from([lit]),
        };
        assert_eq!(display_name(&strategy, &mus, &parse), "Box hidden single");
    }

    #[test]
    fn display_name_drops_prefix_when_labels_disagree() {
        // Defensive case: a MUS with both row_contains and con_contains active
        // would produce conflicting orientation labels — drop the prefix.
        let lit_a = rustsat::types::Lit::from_ipasir(1).unwrap();
        let lit_b = rustsat::types::Lit::from_ipasir(2).unwrap();
        let parse = build_parse_for_display(
            unit_contains_family(),
            &[
                (lit_a, "row_contains", "row_contains[3, 5]"),
                (lit_b, "con_contains", "con_contains[6, 5]"),
            ],
        );
        let strategy = Strategy {
            name: "weird thing".into(),
            fingerprint: "fp".into(),
            orientation_group: Some("unit_contains".into()),
        };
        let mus = MusContext {
            lits: BTreeSet::new(),
            mus: BTreeSet::from([lit_a, lit_b]),
        };
        assert_eq!(display_name(&strategy, &mus, &parse), "weird thing");
    }

    #[test]
    fn display_name_drops_prefix_when_orientation_group_unknown() {
        // Defensive: strategy references a group that doesn't exist in the model.
        let lit = rustsat::types::Lit::from_ipasir(1).unwrap();
        let parse = build_parse_for_display(
            unit_contains_family(),
            &[(lit, "row_contains", "row_contains[1, 2]")],
        );
        let strategy = Strategy {
            name: "Hidden single".into(),
            fingerprint: "fp".into(),
            orientation_group: Some("nonexistent_group".into()),
        };
        let mus = MusContext {
            lits: BTreeSet::new(),
            mus: BTreeSet::from([lit]),
        };
        assert_eq!(display_name(&strategy, &mus, &parse), "Hidden single");
    }

    #[test]
    fn display_name_drops_prefix_when_no_constraints_in_group() {
        // The MUS exists but contains no constraints whose raw family is in
        // the orientation group — fall back to the bare technique name.
        let lit = rustsat::types::Lit::from_ipasir(1).unwrap();
        let parse = build_parse_for_display(
            unit_contains_family(),
            &[(lit, "row_alldiff", "row_alldiff[1, 2, 3]")],
        );
        let strategy = Strategy {
            name: "Naked something".into(),
            fingerprint: "fp".into(),
            orientation_group: Some("unit_contains".into()),
        };
        let mus = MusContext {
            lits: BTreeSet::new(),
            mus: BTreeSet::from([lit]),
        };
        assert_eq!(display_name(&strategy, &mus, &parse), "Naked something");
    }
}