mekle 0.2.0

Fast project discovery tool for developers
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! Recorded project visits, and the frecency they earn.
//!
//! A project's rank combines how often it was visited with how recently, so a
//! project used twice this hour outranks one used ten times last month. Scores
//! are capped in aggregate and aged down when they reach the cap, which keeps
//! the file bounded and lets old favourites fall away.
//!
//! Pinning opts a project out of that drift: a pinned project ranks above every
//! unpinned one whatever its frecency, and aging never drops it.

use crate::{
    error::{Error, Result},
    fs,
};
use serde::{Deserialize, Serialize};
use std::{
    ffi::OsString,
    fs::{OpenOptions, create_dir_all},
    path::{Path, PathBuf},
    time::{Duration, SystemTime, UNIX_EPOCH},
};

const HISTORY_VERSION: u8 = 1;
const MAX_TOTAL_SCORE: f64 = 10_000.0;
const AGED_TOTAL_SCORE: f64 = MAX_TOTAL_SCORE * 0.9;

const HOUR: u64 = 3600;
const DAY: u64 = 24 * HOUR;
const WEEK: u64 = 7 * DAY;

/// The lowest score worth keeping. Anything below it is dropped when aging.
const MINIMUM_SCORE: f64 = 1.0;

/// Every project mekle has seen the user visit.
#[derive(Debug)]
pub struct History {
    path: PathBuf,
    projects: Vec<ProjectUsage>,
}

/// One project's standing, as of the moment it was read.
#[derive(Debug)]
pub struct HistoryEntry {
    pub path: PathBuf,
    pub score: f64,
    pub frecency: f64,
    /// How long ago the project was last visited.
    pub last_used: Duration,
    /// When the project was last visited, in seconds since the Unix epoch.
    pub last_used_at: u64,
    /// Whether the project is held above every unpinned one.
    pub pinned: bool,
}

/// A requested change to a project's raw score.
#[derive(Debug, Clone, Copy)]
pub enum ScoreChange {
    Set(f64),
    Adjust(f64),
    Remove,
}

/// The history file as it is read back.
#[derive(Debug, Deserialize)]
struct StoredHistory {
    version: u8,
    projects: Vec<ProjectUsage>,
}

/// The history file as it is written, borrowing what is already in memory.
#[derive(Debug, Serialize)]
struct StoredHistoryRef<'a> {
    version: u8,
    projects: &'a [ProjectUsage],
}

#[derive(Debug, Serialize, Deserialize)]
struct ProjectUsage {
    path: PathBuf,
    score: f64,
    last_accessed: u64,
    /// Left out of the file unless set, so an unpinned history reads the same
    /// as one written before pinning existed.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pinned: bool,
}

impl History {
    /// Opens the history at `path`, or creates an empty history if it does not exist.
    ///
    /// # Errors
    ///
    /// Returns an error when an existing history cannot be read or parsed.
    pub fn open(path: impl Into<PathBuf>) -> Result<Self> {
        let path = path.into();
        let Some(contents) = fs::read(&path)? else {
            return Ok(Self {
                path,
                projects: Vec::new(),
            });
        };

        let stored =
            toml::from_str::<StoredHistory>(&contents).map_err(|source| Error::ParseHistory {
                path: path.clone(),
                source,
            })?;
        if stored.version != HISTORY_VERSION {
            return Err(Error::UnsupportedHistoryVersion {
                path,
                version: stored.version,
            });
        }

        Ok(Self {
            path,
            projects: stored.projects,
        })
    }

    /// Records a project visit and persists the updated history.
    ///
    /// # Errors
    ///
    /// Returns an error when the clock is invalid or the history cannot be written.
    pub fn record(&mut self, path: &Path) -> Result<()> {
        self.transact(|history| history.record_at(path, SystemTime::now()))
    }

    /// Returns recorded projects ordered by descending frecency.
    ///
    /// # Errors
    ///
    /// Returns an error if the system clock is before the Unix epoch.
    pub fn entries(&self) -> Result<Vec<HistoryEntry>> {
        Ok(self.entries_at(unix_timestamp(SystemTime::now())?))
    }

    fn entries_at(&self, now: u64) -> Vec<HistoryEntry> {
        let mut entries = self
            .projects
            .iter()
            .map(|project| project.entry(now))
            .collect::<Vec<_>>();
        entries.sort_unstable_by(|left, right| {
            right
                .pinned
                .cmp(&left.pinned)
                .then_with(|| right.frecency.total_cmp(&left.frecency))
                .then_with(|| left.path.cmp(&right.path))
        });
        entries
    }

    /// Applies a score change and persists it.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid scores, missing entries, invalid clocks, or writes.
    pub fn update(&mut self, path: &Path, change: ScoreChange) -> Result<()> {
        self.transact(|history| {
            match change {
                ScoreChange::Set(score) => history.set_score(path, score)?,
                ScoreChange::Adjust(delta) => history.adjust_score(path, delta)?,
                ScoreChange::Remove => {
                    history.projects.remove(history.position_of(path)?);
                }
            }
            history.age();
            Ok(())
        })
    }

    /// Pins a project so it ranks above every unpinned one, recording it first
    /// if history has not seen it.
    ///
    /// # Errors
    ///
    /// Returns an error when the clock is invalid or the history cannot be written.
    pub fn pin(&mut self, path: &Path) -> Result<()> {
        self.transact(|history| history.set_pinned(path, true))
    }

    /// Unpins a project, leaving its score and its last visit alone.
    ///
    /// # Errors
    ///
    /// Returns an error when the project is not in history, or it cannot be written.
    pub fn unpin(&mut self, path: &Path) -> Result<()> {
        self.transact(|history| history.set_pinned(path, false))
    }

    /// Removes every entry and persists the empty history.
    ///
    /// # Errors
    ///
    /// Returns an error when the history cannot be written.
    pub fn clear(&mut self) -> Result<()> {
        self.transact(|history| {
            history.projects.clear();
            Ok(())
        })
    }

    /// Removes entries whose project paths no longer exist and persists the history.
    ///
    /// # Errors
    ///
    /// Returns an error when a path cannot be checked or the history cannot be written.
    pub fn prune(&mut self) -> Result<()> {
        self.transact(Self::prune_entries)
    }

    fn prune_entries(&mut self) -> Result<()> {
        // Collected up front so a failed check leaves the history untouched.
        let existing = self
            .projects
            .iter()
            .map(|project| fs::exists(&project.path))
            .collect::<Result<Vec<_>>>()?;

        self.projects = self
            .projects
            .drain(..)
            .zip(existing)
            .filter_map(|(project, exists)| exists.then_some(project))
            .collect();
        Ok(())
    }

    /// A project only reaches history by being pinned when it has never been
    /// visited, so it starts at the score one visit would have earned.
    fn set_pinned(&mut self, path: &Path, pinned: bool) -> Result<()> {
        match self.position_of(path) {
            Ok(position) => self.projects[position].pinned = pinned,
            Err(error) if !pinned => return Err(error),
            Err(_) => self.projects.push(ProjectUsage {
                pinned: true,
                ..ProjectUsage::new(path, MINIMUM_SCORE, unix_timestamp(SystemTime::now())?)
            }),
        }
        Ok(())
    }

    fn position_of(&self, path: &Path) -> Result<usize> {
        self.projects
            .iter()
            .position(|project| project.path == path)
            .ok_or_else(|| Error::HistoryEntryNotFound(path.to_path_buf()))
    }

    fn set_score(&mut self, path: &Path, score: f64) -> Result<()> {
        validate_score(score)?;
        match self.position_of(path) {
            Ok(position) => self.projects[position].score = score,
            Err(_) => self.projects.push(ProjectUsage::new(
                path,
                score,
                unix_timestamp(SystemTime::now())?,
            )),
        }
        Ok(())
    }

    fn adjust_score(&mut self, path: &Path, delta: f64) -> Result<()> {
        if !delta.is_finite() {
            return Err(Error::InvalidScore(delta));
        }

        let position = self.position_of(path)?;
        let score = self.projects[position].score + delta;
        // Adjusting a project below the floor retires it instead of failing.
        if score < MINIMUM_SCORE {
            self.projects.remove(position);
        } else {
            validate_score(score)?;
            self.projects[position].score = score;
        }
        Ok(())
    }

    fn record_at(&mut self, path: &Path, now: SystemTime) -> Result<()> {
        let timestamp = unix_timestamp(now)?;
        if let Some(project) = self
            .projects
            .iter_mut()
            .find(|project| project.path == path)
        {
            project.score += 1.0;
            project.last_accessed = timestamp;
        } else {
            self.projects.push(ProjectUsage::new(path, 1.0, timestamp));
        }
        self.age();
        Ok(())
    }

    /// Scales every score down once they total more than [`MAX_TOTAL_SCORE`],
    /// dropping the unpinned projects that fall below [`MINIMUM_SCORE`].
    fn age(&mut self) {
        let total = self
            .projects
            .iter()
            .map(|project| project.score)
            .sum::<f64>();
        if total <= MAX_TOTAL_SCORE {
            return;
        }

        let factor = AGED_TOTAL_SCORE / total;
        for project in &mut self.projects {
            project.score *= factor;
        }
        self.projects
            .retain(|project| project.pinned || project.score >= MINIMUM_SCORE);
    }

    fn save(&self) -> Result<()> {
        let stored = StoredHistoryRef {
            version: HISTORY_VERSION,
            projects: &self.projects,
        };
        let contents =
            toml::to_string_pretty(&stored).map_err(|source| Error::SerializeHistory { source })?;

        fs::write_atomic(&self.path, &contents)
    }

    /// Reloads and changes history while holding the stable sidecar lock.
    fn transact(&mut self, change: impl FnOnce(&mut Self) -> Result<()>) -> Result<()> {
        let parent = self
            .path
            .parent()
            .filter(|parent| !parent.as_os_str().is_empty())
            .unwrap_or_else(|| Path::new("."));
        create_dir_all(parent).map_err(|source| Error::write_file(parent, source))?;

        let lock_path = lock_path(&self.path);
        let lock = OpenOptions::new()
            .create(true)
            .truncate(false)
            .write(true)
            .open(&lock_path)
            .map_err(|source| Error::write_file(&lock_path, source))?;
        lock.lock()
            .map_err(|source| Error::write_file(&lock_path, source))?;

        let mut current = Self::open(&self.path)?;
        change(&mut current)?;
        current.save()?;
        self.projects = current.projects;
        Ok(())
    }
}

fn lock_path(path: &Path) -> PathBuf {
    let mut lock_path = OsString::from(path.as_os_str());
    lock_path.push(".lock");
    PathBuf::from(lock_path)
}

impl ProjectUsage {
    fn new(path: &Path, score: f64, last_accessed: u64) -> Self {
        Self {
            path: path.to_path_buf(),
            score,
            last_accessed,
            pinned: false,
        }
    }

    /// Weighs a raw score by how recently the project was used.
    fn frecency(&self, now: u64) -> f64 {
        let age = now.saturating_sub(self.last_accessed);
        let multiplier = if age < HOUR {
            4.0
        } else if age < DAY {
            2.0
        } else if age < WEEK {
            0.5
        } else {
            0.25
        };
        self.score * multiplier
    }

    fn entry(&self, now: u64) -> HistoryEntry {
        HistoryEntry {
            path: self.path.clone(),
            score: self.score,
            frecency: self.frecency(now),
            last_used: Duration::from_secs(now.saturating_sub(self.last_accessed)),
            last_used_at: self.last_accessed,
            pinned: self.pinned,
        }
    }
}

fn validate_score(score: f64) -> Result<()> {
    if score.is_finite() && score >= MINIMUM_SCORE {
        Ok(())
    } else {
        Err(Error::InvalidScore(score))
    }
}

fn unix_timestamp(time: SystemTime) -> Result<u64> {
    time.duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_secs())
        .map_err(|source| Error::InvalidSystemTime { source })
}

#[cfg(test)]
mod tests {
    use super::*;
    use claims::{assert_err, assert_ok};
    use tempfile::TempDir;

    const NOW: Duration = Duration::from_secs(2_000_000);

    fn usage(path: &str, score: f64, age: Duration) -> ProjectUsage {
        ProjectUsage::new(Path::new(path), score, NOW.saturating_sub(age).as_secs())
    }

    fn pinned(path: &str, score: f64, age: Duration) -> ProjectUsage {
        ProjectUsage {
            pinned: true,
            ..usage(path, score, age)
        }
    }

    fn history_of(projects: Vec<ProjectUsage>) -> History {
        History {
            path: PathBuf::new(),
            projects,
        }
    }

    #[test]
    fn missing_file_opens_as_empty_history() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");

        let history = History::open(temp.path().join("missing.toml"))?;

        assert!(history.projects.is_empty());
        Ok(())
    }

    #[test]
    fn records_are_persisted() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");
        let database = temp.path().join("mekle/history.toml");
        let project = Path::new("/projects/favorite");
        let mut history = History::open(&database)?;

        assert_ok!(history.record_at(project, UNIX_EPOCH + NOW));
        history.save()?;
        let history = History::open(database)?;

        assert_eq!(history.projects.len(), 1);
        assert_eq!(history.projects[0].path, project);
        assert!((history.projects[0].score - 1.0).abs() < f64::EPSILON);
        Ok(())
    }

    #[test]
    fn repeated_visits_increase_the_score() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");
        let mut history = History::open(temp.path().join("history.toml"))?;

        assert_ok!(history.record_at(Path::new("/project"), UNIX_EPOCH + NOW));
        assert_ok!(history.record_at(Path::new("/project"), UNIX_EPOCH + NOW));

        assert!((history.projects[0].score - 2.0).abs() < f64::EPSILON);
        Ok(())
    }

    /// The paths `history` reports, in the order it reports them.
    fn ranking(history: &History) -> Vec<PathBuf> {
        history
            .entries_at(NOW.as_secs())
            .into_iter()
            .map(|entry| entry.path)
            .collect()
    }

    #[test]
    fn recency_outweighs_a_higher_raw_score() {
        let history = history_of(vec![
            usage("/recent", 2.0, Duration::from_mins(30)),
            usage("/yesterday", 3.0, Duration::from_hours(2)),
            usage("/week", 10.0, Duration::from_hours(2 * 24)),
            usage("/old", 16.0, Duration::from_hours(8 * 24)),
        ]);

        assert_eq!(
            ranking(&history),
            ["/recent", "/yesterday", "/week", "/old"].map(PathBuf::from)
        );
    }

    #[test]
    fn equally_ranked_projects_are_ordered_by_path() {
        let history = history_of(vec![
            usage("/beta", 1.0, Duration::ZERO),
            usage("/alpha", 1.0, Duration::ZERO),
        ]);

        assert_eq!(ranking(&history), ["/alpha", "/beta"].map(PathBuf::from));
    }

    #[test]
    fn aging_removes_projects_with_negligible_scores() {
        let mut history = history_of(vec![
            usage("/frequent", MAX_TOTAL_SCORE, Duration::ZERO),
            usage("/rare", 1.0, Duration::ZERO),
        ]);

        history.age();

        assert_eq!(history.projects.len(), 1);
        assert_eq!(history.projects[0].path, Path::new("/frequent"));
        assert!(history.projects[0].score <= AGED_TOTAL_SCORE);
    }

    #[test]
    fn adjusting_below_the_floor_retires_a_project() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");
        let mut history = History::open(temp.path().join("history.toml"))?;
        history
            .projects
            .push(usage("/project", 2.0, Duration::ZERO));
        history.save()?;

        history.update(Path::new("/project"), ScoreChange::Adjust(-1.5))?;

        assert!(history.projects.is_empty());
        Ok(())
    }

    #[test]
    fn pinned_projects_rank_above_more_frecent_ones() {
        let history = history_of(vec![
            usage("/recent", 100.0, Duration::ZERO),
            pinned("/favourite", 1.0, Duration::from_hours(8 * 24)),
        ]);

        assert_eq!(
            ranking(&history),
            ["/favourite", "/recent"].map(PathBuf::from)
        );
    }

    #[test]
    fn aging_keeps_pinned_projects() {
        let mut history = history_of(vec![
            usage("/frequent", MAX_TOTAL_SCORE, Duration::ZERO),
            pinned("/favourite", 1.0, Duration::ZERO),
        ]);

        history.age();

        assert_eq!(history.projects.len(), 2);
        assert!(history.projects[1].score < MINIMUM_SCORE);
    }

    #[test]
    fn pinning_an_unvisited_project_records_it() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");
        let mut history = History::open(temp.path().join("history.toml"))?;

        history.pin(Path::new("/project"))?;

        assert_eq!(history.projects.len(), 1);
        assert!(history.projects[0].pinned);
        assert!((history.projects[0].score - MINIMUM_SCORE).abs() < f64::EPSILON);
        Ok(())
    }

    #[test]
    fn pinning_survives_a_round_trip_and_unpinning() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");
        let database = temp.path().join("history.toml");
        let project = Path::new("/projects/favourite");
        let mut history = History::open(&database)?;
        assert_ok!(history.record_at(project, UNIX_EPOCH + NOW));
        history.pin(project)?;

        let mut history = History::open(&database)?;
        assert!(history.projects[0].pinned);
        // The score the visit earned is left alone by both commands.
        assert!((history.projects[0].score - 1.0).abs() < f64::EPSILON);

        history.unpin(project)?;

        assert!(!History::open(&database)?.projects[0].pinned);
        Ok(())
    }

    #[test]
    fn unpinning_an_unknown_project_fails() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");
        let mut history = History::open(temp.path().join("history.toml"))?;

        assert_err!(history.unpin(Path::new("/project")));
        Ok(())
    }

    #[test]
    fn pruning_keeps_only_projects_that_still_exist() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");
        let present = temp.path().join("present");
        std::fs::create_dir(&present).expect("create project dir");
        let mut history = History::open(temp.path().join("history.toml"))?;
        history
            .projects
            .push(ProjectUsage::new(&present, 1.0, NOW.as_secs()));
        history
            .projects
            .push(usage("/definitely/not/here", 1.0, Duration::ZERO));
        history.save()?;

        history.prune()?;

        assert_eq!(history.projects.len(), 1);
        assert_eq!(history.projects[0].path, present);
        Ok(())
    }

    #[test]
    fn concurrent_records_preserve_every_project() -> Result<()> {
        let temp = TempDir::new().expect("create temp dir");
        let database = temp.path().join("history.toml");

        std::thread::scope(|scope| {
            let writers = (0..100)
                .map(|index| {
                    let database = &database;
                    scope.spawn(move || {
                        let mut history = History::open(database).expect("open history");
                        let project = PathBuf::from(format!("/project-{index}"));
                        history.record(&project).expect("record project");
                    })
                })
                .collect::<Vec<_>>();

            for writer in writers {
                writer.join().expect("recording thread should finish");
            }
        });

        let history = History::open(database)?;
        assert_eq!(history.projects.len(), 100);
        Ok(())
    }
}