stakpak-shared 0.3.74

Stakpak: Your DevOps AI Agent. Generate infrastructure code, debug Kubernetes, configure CI/CD, automate deployments, without giving an LLM the keys to production.
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use std::{
    collections::HashMap,
    hash::{DefaultHasher, Hash, Hasher},
    path::{Path, PathBuf},
    sync::Arc,
};

use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
use walkdir::WalkDir;

/// Represents a file's content and metadata for tracking changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileBuffer {
    pub content: String,
    pub uri: String,
    pub hash: u64,
    pub path: PathBuf,
}

/// Events that can occur during file watching
#[derive(Debug, Clone)]
pub enum FileWatchEvent {
    /// File was modified
    Modified {
        file: FileBuffer,
        old_content: String,
    },
    /// File was deleted
    Deleted { file: FileBuffer },
    /// File was created
    Created { file: FileBuffer },
    /// Raw filesystem event for custom handling
    Raw { event: Event },
}

/// Trait for filtering which files should be watched
pub trait FileFilter: Send + Sync {
    /// Returns true if the file should be watched
    fn should_watch(&self, path: &Path) -> bool;
}

/// Simple closure-based file filter
pub struct ClosureFilter<F>
where
    F: Fn(&Path) -> bool + Send + Sync,
{
    filter_fn: F,
}

impl<F> ClosureFilter<F>
where
    F: Fn(&Path) -> bool + Send + Sync,
{
    pub fn new(filter_fn: F) -> Self {
        Self { filter_fn }
    }
}

impl<F> FileFilter for ClosureFilter<F>
where
    F: Fn(&Path) -> bool + Send + Sync,
{
    fn should_watch(&self, path: &Path) -> bool {
        (self.filter_fn)(path)
    }
}

/// Main file watcher that can watch directories for changes
pub struct FileWatcher {
    watch_dir: PathBuf,
    watched_files: HashMap<String, FileBuffer>,
    filter: Arc<dyn FileFilter>,
    watcher: Option<RecommendedWatcher>,
}

impl FileWatcher {
    /// Create a new file watcher
    pub fn new<F>(watch_dir: PathBuf, filter: F) -> Self
    where
        F: FileFilter + 'static,
    {
        Self {
            watch_dir,
            watched_files: HashMap::new(),
            filter: Arc::new(filter),
            watcher: None,
        }
    }

    /// Initialize the watcher and scan for existing files
    pub fn initialize(&mut self) -> Result<(), String> {
        self.watched_files = self.scan_directory()?;
        Ok(())
    }

    /// Start watching the directory and return a receiver for processed events
    pub async fn start_watching(&mut self) -> Result<mpsc::Receiver<FileWatchEvent>, String> {
        let (processed_tx, processed_rx) = mpsc::channel(100);
        let (raw_tx, mut raw_rx) = mpsc::unbounded_channel();

        let watch_dir = self.watch_dir.clone();
        let filter = Arc::clone(&self.filter);
        let raw_tx_clone = raw_tx.clone();

        // Create the filesystem watcher
        let watcher: Result<RecommendedWatcher, notify::Error> = RecommendedWatcher::new(
            move |result: Result<Event, notify::Error>| {
                if let Ok(event) = result {
                    // Filter events based on paths
                    let should_process = event
                        .paths
                        .iter()
                        .any(|path| path.is_file() && filter.should_watch(path));

                    if should_process {
                        let _ = raw_tx_clone.send(event);
                    }
                }
            },
            Config::default(),
        );
        let mut watcher = watcher.map_err(|e| format!("Failed to create watcher: {}", e))?;

        // Start watching
        watcher
            .watch(&watch_dir, RecursiveMode::Recursive)
            .map_err(|e| format!("Failed to watch directory: {}", e))?;

        self.watcher = Some(watcher);

        // Spawn background task to process raw events
        let watch_dir_clone = self.watch_dir.clone();
        let filter_clone = Arc::clone(&self.filter);
        let watched_files = self.watched_files.clone();

        tokio::spawn(async move {
            let mut internal_watcher = InternalEventProcessor {
                watch_dir: watch_dir_clone,
                watched_files,
                filter: filter_clone,
                processed_tx,
            };

            while let Some(raw_event) = raw_rx.recv().await {
                if let Err(e) = internal_watcher.process_event(raw_event).await {
                    eprintln!("Error processing file watch event: {}", e);
                }
            }
        });

        Ok(processed_rx)
    }

    /// Get current watched files (snapshot at initialization)
    pub fn get_watched_files(&self) -> &HashMap<String, FileBuffer> {
        &self.watched_files
    }

    /// Get the directory being watched
    pub fn watch_dir(&self) -> &Path {
        &self.watch_dir
    }

    /// Scan directory for existing files
    fn scan_directory(&self) -> Result<HashMap<String, FileBuffer>, String> {
        let mut files = HashMap::new();

        for entry in WalkDir::new(&self.watch_dir)
            .into_iter()
            .filter_map(Result::ok)
            .filter(|entry| entry.path().is_file() && self.filter.should_watch(entry.path()))
        {
            let path = entry.path();
            if let Ok(buffer) = self.create_file_buffer(path) {
                files.insert(buffer.uri.clone(), buffer);
            }
        }

        Ok(files)
    }

    /// Create a file buffer from a path
    fn create_file_buffer(&self, path: &Path) -> Result<FileBuffer, String> {
        let content = std::fs::read_to_string(path)
            .map_err(|e| format!("Failed to read file {}: {}", path.display(), e))?;

        let hash = self.hash_content(&content);
        let uri = self.path_to_uri(path);

        // Use canonical path for consistency
        let canonical_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());

        Ok(FileBuffer {
            content,
            uri,
            hash,
            path: canonical_path,
        })
    }

    /// Convert path to URI
    fn path_to_uri(&self, path: &Path) -> String {
        // Use canonical path to ensure consistency across all platforms
        let canonical_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());

        // Create absolute URI instead of relative
        format!(
            "file://{}",
            canonical_path.to_string_lossy().replace('\\', "/")
        )
    }

    /// Hash file content
    fn hash_content(&self, content: &str) -> u64 {
        let mut hasher = DefaultHasher::new();
        content.hash(&mut hasher);
        hasher.finish()
    }
}

/// Internal event processor that handles raw events and produces processed events
struct InternalEventProcessor {
    #[allow(dead_code)]
    watch_dir: PathBuf,
    watched_files: HashMap<String, FileBuffer>,
    filter: Arc<dyn FileFilter>,
    processed_tx: mpsc::Sender<FileWatchEvent>,
}

impl InternalEventProcessor {
    /// Process a raw filesystem event and send processed events
    async fn process_event(&mut self, event: Event) -> Result<(), String> {
        let mut events_to_send = Vec::new();

        // Handle deletions first
        self.process_deletions(&mut events_to_send);

        // Handle modifications and creations
        self.process_modifications(&event, &mut events_to_send)?;

        // Send all processed events
        for event in events_to_send {
            if self.processed_tx.send(event).await.is_err() {
                // Channel was closed, stop processing
                return Err("Event channel closed".to_string());
            }
        }

        Ok(())
    }

    /// Process file deletions
    fn process_deletions(&mut self, events: &mut Vec<FileWatchEvent>) {
        let mut to_remove = Vec::new();

        for (uri, buffer) in &self.watched_files {
            if !buffer.path.exists() {
                events.push(FileWatchEvent::Deleted {
                    file: buffer.clone(),
                });
                to_remove.push(uri.clone());
            }
        }

        for uri in to_remove {
            self.watched_files.remove(&uri);
        }
    }

    /// Process file modifications and creations
    fn process_modifications(
        &mut self,
        event: &Event,
        events: &mut Vec<FileWatchEvent>,
    ) -> Result<(), String> {
        for path in &event.paths {
            if !path.is_file() || !self.filter.should_watch(path) {
                continue;
            }

            let uri = self.path_to_uri(path);

            match self.create_file_buffer(path) {
                Ok(new_buffer) => {
                    if let Some(old_buffer) = self.watched_files.get(&uri) {
                        // File exists and was modified
                        if old_buffer.hash != new_buffer.hash {
                            events.push(FileWatchEvent::Modified {
                                file: new_buffer.clone(),
                                old_content: old_buffer.content.clone(),
                            });
                            self.watched_files.insert(uri, new_buffer);
                        }
                    } else {
                        // New file created
                        events.push(FileWatchEvent::Created {
                            file: new_buffer.clone(),
                        });
                        self.watched_files.insert(uri, new_buffer);
                    }
                }
                Err(_) => {
                    // File might have been deleted
                    if let Some(old_buffer) = self.watched_files.remove(&uri) {
                        events.push(FileWatchEvent::Deleted { file: old_buffer });
                    }
                }
            }
        }

        Ok(())
    }

    /// Create a file buffer from a path
    fn create_file_buffer(&self, path: &Path) -> Result<FileBuffer, String> {
        let content = std::fs::read_to_string(path)
            .map_err(|e| format!("Failed to read file {}: {}", path.display(), e))?;

        let hash = self.hash_content(&content);
        let uri = self.path_to_uri(path);

        // Use canonical path for consistency
        let canonical_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());

        Ok(FileBuffer {
            content,
            uri,
            hash,
            path: canonical_path,
        })
    }

    /// Convert path to URI
    fn path_to_uri(&self, path: &Path) -> String {
        // Use canonical path to ensure consistency across all platforms
        let canonical_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());

        // Create absolute URI instead of relative
        format!(
            "file://{}",
            canonical_path.to_string_lossy().replace('\\', "/")
        )
    }

    /// Hash file content
    fn hash_content(&self, content: &str) -> u64 {
        let mut hasher = DefaultHasher::new();
        content.hash(&mut hasher);
        hasher.finish()
    }
}

/// Convenience function to create a file watcher with closure-based filter
pub fn create_file_watcher<F>(watch_dir: PathBuf, filter: F) -> Result<FileWatcher, String>
where
    F: Fn(&Path) -> bool + Send + Sync + 'static,
{
    let filter = ClosureFilter::new(filter);
    let watcher = FileWatcher::new(watch_dir, filter);
    Ok(watcher)
}

/// Convenience function to create and start a file watcher, returning the event receiver
pub async fn create_and_start_watcher<F>(
    watch_dir: PathBuf,
    filter: F,
) -> Result<(FileWatcher, mpsc::Receiver<FileWatchEvent>), String>
where
    F: Fn(&Path) -> bool + Send + Sync + 'static,
{
    let mut watcher = create_file_watcher(watch_dir, filter)?;
    watcher.initialize()?;
    let receiver = watcher.start_watching().await?;
    Ok((watcher, receiver))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::Path;
    use tempfile::TempDir;
    use tokio::time::Duration;

    // Helper function to create a test directory with some files
    fn create_test_directory() -> TempDir {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        // Create some test files
        fs::write(temp_path.join("test1.txt"), "content1").expect("Failed to write test1.txt");
        fs::write(temp_path.join("test2.rs"), "fn main() {}").expect("Failed to write test2.rs");
        fs::write(temp_path.join("ignore.log"), "log content").expect("Failed to write ignore.log");

        // Create subdirectory with files
        let sub_dir = temp_path.join("subdir");
        fs::create_dir(&sub_dir).expect("Failed to create subdirectory");
        fs::write(sub_dir.join("nested.txt"), "nested content")
            .expect("Failed to write nested.txt");

        temp_dir
    }

    // Simple test filter that only watches .txt and .rs files
    fn test_filter(path: &Path) -> bool {
        if let Some(ext) = path.extension() {
            matches!(ext.to_str(), Some("txt") | Some("rs"))
        } else {
            false
        }
    }

    #[test]
    fn test_file_filter_trait() {
        let filter = ClosureFilter::new(test_filter);

        assert!(filter.should_watch(Path::new("test.txt")));
        assert!(filter.should_watch(Path::new("test.rs")));
        assert!(!filter.should_watch(Path::new("test.log")));
        assert!(!filter.should_watch(Path::new("test")));
    }

    #[test]
    fn test_file_watcher_creation() {
        let temp_dir = create_test_directory();
        let filter = ClosureFilter::new(test_filter);

        let watcher = FileWatcher::new(temp_dir.path().to_path_buf(), filter);

        assert_eq!(watcher.watch_dir(), temp_dir.path());
        assert_eq!(watcher.get_watched_files().len(), 0); // Not initialized yet
    }

    #[test]
    fn test_file_watcher_initialization() {
        let temp_dir = create_test_directory();
        let filter = ClosureFilter::new(test_filter);

        let mut watcher = FileWatcher::new(temp_dir.path().to_path_buf(), filter);
        watcher.initialize().expect("Failed to initialize watcher");

        let watched_files = watcher.get_watched_files();

        // Should have 3 files: test1.txt, test2.rs, and nested.txt (filtered by extension)
        assert_eq!(watched_files.len(), 3);

        // Check that files are properly tracked
        let file_names: Vec<_> = watched_files
            .values()
            .map(|f| f.path.file_name().unwrap().to_str().unwrap())
            .collect();

        assert!(file_names.contains(&"test1.txt"));
        assert!(file_names.contains(&"test2.rs"));
        assert!(file_names.contains(&"nested.txt"));
    }

    #[tokio::test]
    async fn test_create_and_start_watcher() {
        let temp_dir = create_test_directory();

        let (watcher, _rx) = create_and_start_watcher(temp_dir.path().to_path_buf(), test_filter)
            .await
            .expect("Failed to create and start watcher");

        // Should have the same files as the basic test
        assert_eq!(watcher.get_watched_files().len(), 3);
        assert_eq!(watcher.watch_dir(), temp_dir.path());
    }

    #[tokio::test]
    async fn test_real_file_creation_detection() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");

        let (_watcher, mut event_rx) =
            create_and_start_watcher(temp_dir.path().to_path_buf(), test_filter)
                .await
                .expect("Failed to create and start watcher");

        // Give the watcher a moment to start
        tokio::time::sleep(Duration::from_millis(200)).await;

        // Create a new file
        let new_file = temp_dir.path().join("new_test.txt");
        fs::write(&new_file, "new file content").expect("Failed to create new file");
        let new_file_canonical = new_file
            .canonicalize()
            .expect("Failed to canonicalize path");

        // Wait for processed events
        let mut creation_detected = false;
        let timeout = tokio::time::Instant::now() + Duration::from_secs(2);

        while tokio::time::Instant::now() < timeout && !creation_detected {
            tokio::select! {
                Some(event) = event_rx.recv() => {
                    if let FileWatchEvent::Created { file } = event
                        && file.path == new_file_canonical {
                            assert_eq!(file.content, "new file content");
                            creation_detected = true;
                            break;
                        }
                }
                _ = tokio::time::sleep(Duration::from_millis(50)) => {
                    // Continue waiting
                }
            }
        }

        assert!(creation_detected, "File creation was not detected");
    }

    #[tokio::test]
    async fn test_real_file_modification_detection() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");

        // Create initial file
        let test_file = temp_dir.path().join("modify_test.txt");
        fs::write(&test_file, "initial content").expect("Failed to create initial file");
        let test_file_canonical = test_file
            .canonicalize()
            .expect("Failed to canonicalize path");

        let (_watcher, mut event_rx) =
            create_and_start_watcher(temp_dir.path().to_path_buf(), test_filter)
                .await
                .expect("Failed to create and start watcher");

        // Give the watcher a moment to start
        tokio::time::sleep(Duration::from_millis(200)).await;

        // Modify the file
        fs::write(&test_file, "modified content").expect("Failed to modify file");

        // Wait for processed events
        let mut modification_detected = false;
        let timeout = tokio::time::Instant::now() + Duration::from_secs(2);

        while tokio::time::Instant::now() < timeout && !modification_detected {
            tokio::select! {
                Some(event) = event_rx.recv() => {
                    if let FileWatchEvent::Modified { file, old_content } = event
                        && file.path == test_file_canonical {
                            assert_eq!(file.content, "modified content");
                            assert_eq!(old_content, "initial content");
                            modification_detected = true;
                            break;
                        }
                }
                _ = tokio::time::sleep(Duration::from_millis(50)) => {
                    // Continue waiting
                }
            }
        }

        assert!(modification_detected, "File modification was not detected");
    }

    #[tokio::test]
    async fn test_file_filter_in_real_watching() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");

        let (_watcher, mut event_rx) = create_and_start_watcher(
            temp_dir.path().to_path_buf(),
            test_filter, // Only watches .txt and .rs files
        )
        .await
        .expect("Failed to create and start watcher");

        // Give the watcher a moment to start
        tokio::time::sleep(Duration::from_millis(200)).await;

        // Create files with different extensions
        let txt_file = temp_dir.path().join("watched.txt");
        let log_file = temp_dir.path().join("ignored.log");

        fs::write(&txt_file, "should be watched").expect("Failed to create txt file");
        fs::write(&log_file, "should be ignored").expect("Failed to create log file");

        let txt_file_canonical = txt_file
            .canonicalize()
            .expect("Failed to canonicalize txt file path");
        let log_file_canonical = log_file
            .canonicalize()
            .expect("Failed to canonicalize log file path");

        // Wait for processed events
        let mut txt_detected = false;
        let mut log_detected = false;
        let timeout = tokio::time::Instant::now() + Duration::from_secs(2);

        while tokio::time::Instant::now() < timeout && !txt_detected {
            tokio::select! {
                Some(event) = event_rx.recv() => {
                    if let FileWatchEvent::Created { file } = event {
                        if file.path == txt_file_canonical {
                            txt_detected = true;
                        } else if file.path == log_file_canonical {
                            log_detected = true;
                        }
                    }
                }
                _ = tokio::time::sleep(Duration::from_millis(50)) => {
                    // Continue waiting
                }
            }
        }

        assert!(txt_detected, "TXT file creation should be detected");
        assert!(!log_detected, "LOG file creation should be filtered out");
    }
}