chess-vector-engine 0.5.1

Open source chess engine with hybrid vector-based position analysis, advanced tactical search, and NNUE neural network evaluation
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
use std::collections::HashMap;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

/// File format priority (lower = better)
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum FormatPriority {
    MemoryMapped = 1,
    MessagePack = 2,
    Binary = 3,
    Zstd = 4,
    Json = 5,
}

/// Training data file information
#[derive(Debug, Clone)]
pub struct TrainingFile {
    pub path: PathBuf,
    pub format: String,
    pub priority: FormatPriority,
    pub base_name: String,
    pub size_bytes: u64,
}

/// Auto-discovery and format consolidation engine
pub struct AutoDiscovery;

impl AutoDiscovery {
    /// Discover all training data files in a directory
    pub fn discover_training_files<P: AsRef<Path>>(
        base_path: P,
        recursive: bool,
    ) -> Result<Vec<TrainingFile>, Box<dyn std::error::Error>> {
        let mut discovered_files = Vec::new();
        let base_path = base_path.as_ref();

        println!(
            "🔍 Discovering training data files in {}...",
            base_path.display()
        );

        Self::scan_directory(base_path, recursive, &mut discovered_files)?;

        // Sort by base name, then by priority
        discovered_files.sort_by(|a, b| {
            a.base_name
                .cmp(&b.base_name)
                .then_with(|| a.priority.cmp(&b.priority))
        });

        println!(
            "📁 Discovered {} training data files",
            discovered_files.len()
        );
        for file in &discovered_files {
            println!(
                "   {} - {} ({})",
                file.format,
                file.path.display(),
                Self::format_bytes(file.size_bytes)
            );
        }

        Ok(discovered_files)
    }

    /// Group files by base name and select best format for each
    pub fn consolidate_by_base_name(files: Vec<TrainingFile>) -> HashMap<String, TrainingFile> {
        let mut groups: HashMap<String, Vec<TrainingFile>> = HashMap::new();

        // Group by base name
        for file in files {
            groups.entry(file.base_name.clone()).or_default().push(file);
        }

        let mut consolidated = HashMap::new();

        // Select best format for each group
        for (base_name, mut group) in groups {
            group.sort_by(|a, b| a.priority.cmp(&b.priority));

            if let Some(best_file) = group.into_iter().next() {
                consolidated.insert(base_name, best_file);
            }
        }

        consolidated
    }

    /// Get list of inferior formats that can be cleaned up
    pub fn get_cleanup_candidates(files: &[TrainingFile]) -> Vec<PathBuf> {
        let mut cleanup_files = Vec::new();
        let consolidated = Self::consolidate_by_base_name(files.to_vec());

        for file in files {
            if let Some(best_file) = consolidated.get(&file.base_name) {
                // If this file is not the best format for its base name, mark for cleanup
                if file.path != best_file.path && file.priority > best_file.priority {
                    cleanup_files.push(file.path.clone());
                }
            }
        }

        cleanup_files
    }

    /// Clean up old format files
    pub fn cleanup_old_formats(
        files_to_remove: &[PathBuf],
        dry_run: bool,
    ) -> Result<(), Box<dyn std::error::Error>> {
        if dry_run {
            println!(
                "🧹 DRY RUN - Would remove {} old format files:",
                files_to_remove.len()
            );
            for _path in files_to_remove {
                println!("Discovery complete");
            }
            return Ok(());
        }

        println!(
            "🧹 Cleaning up {} old format files...",
            files_to_remove.len()
        );

        for path in files_to_remove {
            match fs::remove_file(path) {
                Ok(()) => println!("Removed file: {}", path.display()),
                Err(e) => println!("Error removing file: {e}"),
            }
        }

        Ok(())
    }

    /// Scan directory recursively for training files
    fn scan_directory(
        dir: &Path,
        recursive: bool,
        files: &mut Vec<TrainingFile>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        if !dir.is_dir() {
            return Ok(());
        }

        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_dir() && recursive {
                Self::scan_directory(&path, recursive, files)?;
            } else if path.is_file() {
                if let Some(training_file) = Self::analyze_file(&path)? {
                    files.push(training_file);
                }
            }
        }

        Ok(())
    }

    /// Analyze a file to determine if it's training data
    fn analyze_file(path: &Path) -> Result<Option<TrainingFile>, Box<dyn std::error::Error>> {
        let metadata = fs::metadata(path)?;
        let size_bytes = metadata.len();

        // Skip system files, hidden files, and very small files
        if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) {
            if file_name.starts_with('.')
                || file_name.starts_with("~")
                || file_name.contains("lock")
                || file_name.contains("tmp")
                || size_bytes < 10
            {
                return Ok(None);
            }
        }

        // Get base name (without extension)
        let base_name = Self::extract_base_name(path);

        // Detect format by extension and content
        if let Some((format, priority)) = Self::detect_format(path)? {
            return Ok(Some(TrainingFile {
                path: path.to_path_buf(),
                format,
                priority,
                base_name,
                size_bytes,
            }));
        }

        Ok(None)
    }

    /// Extract base name from path, removing format extensions
    fn extract_base_name(path: &Path) -> String {
        let file_name = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown");

        // Remove known extensions to get base name

        file_name
            .replace(".mmap", "")
            .replace(".msgpack", "")
            .replace(".bin", "")
            .replace(".zst", "")
            .replace(".json", "")
    }

    /// Detect file format and priority
    fn detect_format(
        path: &Path,
    ) -> Result<Option<(String, FormatPriority)>, Box<dyn std::error::Error>> {
        let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

        // Check by extension
        if file_name.ends_with(".mmap") {
            return Ok(Some(("MMAP".to_string(), FormatPriority::MemoryMapped)));
        }

        if file_name.ends_with(".msgpack") {
            return Ok(Some(("MSGPACK".to_string(), FormatPriority::MessagePack)));
        }

        if file_name.ends_with(".bin") && Self::verify_binary_training_data(path)? {
            return Ok(Some(("BINARY".to_string(), FormatPriority::Binary)));
        }

        if file_name.ends_with(".zst") {
            return Ok(Some(("ZSTD".to_string(), FormatPriority::Zstd)));
        }

        if file_name.ends_with(".json") && Self::verify_json_training_data(path)? {
            return Ok(Some(("JSON".to_string(), FormatPriority::Json)));
        }

        // Check by content for files that might not have proper extensions
        if (file_name.contains("training")
            || file_name.contains("position")
            || file_name.contains("tactical"))
            && Self::verify_json_training_data(path)?
        {
            return Ok(Some(("JSON".to_string(), FormatPriority::Json)));
        }

        Ok(None)
    }

    /// Verify that a JSON file contains training data
    fn verify_json_training_data(path: &Path) -> Result<bool, Box<dyn std::error::Error>> {
        // Check file size first
        if let Ok(metadata) = std::fs::metadata(path) {
            let size = metadata.len();
            if size == 0 || size > 10_000_000 {
                // Skip empty files or files > 10MB
                return Ok(false);
            }
        }

        let file = std::fs::File::open(path)?;
        let reader = BufReader::new(file);

        // Check first few lines for training data structure
        for (i, line_result) in reader.lines().enumerate() {
            if i >= 10 {
                break;
            } // Only check first 10 lines

            // Handle UTF-8 errors gracefully
            let line = match line_result {
                Ok(line) => line,
                Err(_) => {
                    // If we can't read as UTF-8, it's not a JSON training file
                    return Ok(false);
                }
            };

            if line.trim().is_empty() {
                continue;
            }

            if let Ok(json) = serde_json::from_str::<serde_json::Value>(&line) {
                // Look for common training data fields
                if json.get("fen").is_some() && json.get("evaluation").is_some() {
                    return Ok(true);
                }
                if json.get("board").is_some() && json.get("eval").is_some() {
                    return Ok(true);
                }
                if json.get("position").is_some() && json.get("score").is_some() {
                    return Ok(true);
                }
            }
        }

        Ok(false)
    }

    /// Verify that a binary file contains training data
    fn verify_binary_training_data(path: &Path) -> Result<bool, Box<dyn std::error::Error>> {
        use std::io::Read;

        // Skip very small files and very large files for safety
        if let Ok(metadata) = std::fs::metadata(path) {
            let size = metadata.len();
            if !(100..=100_000_000).contains(&size) {
                // Skip files < 100B or > 100MB
                return Ok(false);
            }
        }

        let mut file = std::fs::File::open(path)?;
        let mut buffer = vec![0u8; 1024]; // Read first 1KB
        let bytes_read = file.read(&mut buffer)?;

        if bytes_read == 0 {
            return Ok(false);
        }

        buffer.truncate(bytes_read);

        // Try to deserialize as training data (catch all errors)
        if bincode::deserialize::<Vec<(String, f32)>>(&buffer).is_ok() {
            return Ok(true);
        }

        // Try LZ4 decompression first (catch all errors)
        if let Ok(decompressed) = lz4_flex::decompress_size_prepended(&buffer) {
            if bincode::deserialize::<Vec<(String, f32)>>(&decompressed).is_ok() {
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Format bytes for display
    fn format_bytes(bytes: u64) -> String {
        const UNITS: &[&str] = &["B", "KB", "MB", "GB"];
        let mut size = bytes as f64;
        let mut unit_index = 0;

        while size >= 1024.0 && unit_index < UNITS.len() - 1 {
            size /= 1024.0;
            unit_index += 1;
        }

        "Processing files...".to_string()
    }
}

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

    #[test]
    fn test_base_name_extraction() {
        assert_eq!(
            AutoDiscovery::extract_base_name(Path::new("training_data.json")),
            "training_data"
        );
        assert_eq!(
            AutoDiscovery::extract_base_name(Path::new("training_data.mmap")),
            "training_data"
        );
        assert_eq!(
            AutoDiscovery::extract_base_name(Path::new("tactical_training_data.msgpack")),
            "tactical_training_data"
        );
    }

    #[test]
    fn test_format_priority() {
        assert!(FormatPriority::MemoryMapped < FormatPriority::MessagePack);
        assert!(FormatPriority::MessagePack < FormatPriority::Binary);
        assert!(FormatPriority::Binary < FormatPriority::Json);
    }

    #[test]
    fn test_consolidation() {
        let files = vec![
            TrainingFile {
                path: PathBuf::from("training_data.json"),
                format: "JSON".to_string(),
                priority: FormatPriority::Json,
                base_name: "training_data".to_string(),
                size_bytes: 1000,
            },
            TrainingFile {
                path: PathBuf::from("training_data.mmap"),
                format: "MMAP".to_string(),
                priority: FormatPriority::MemoryMapped,
                base_name: "training_data".to_string(),
                size_bytes: 800,
            },
        ];

        let consolidated = AutoDiscovery::consolidate_by_base_name(files);
        let best = consolidated.get("training_data").unwrap();

        assert_eq!(best.format, "MMAP");
        assert_eq!(best.priority, FormatPriority::MemoryMapped);
    }
}