croft-software 0.1.758

VSCode-style TUI written in Rust
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
//! Durable, context-rich shell command history (the atuin model, embedded):
//! every command a shell-integrated pane finishes is recorded with its
//! working directory, exit code, duration and timestamp, in a JSONL file
//! under the croft config dir. The Ctrl+Shift+R popup searches it across
//! sessions and restarts — newest first, deduplicated, filterable to the
//! current directory or to failures only.
//!
//! The file is append-only in the hot path (one `serde_json` line per
//! finished command); it is compacted back to the newest [`HISTORY_MAX`]
//! entries when it grows past twice that, so a long-lived install never
//! rereads an unbounded file.

use std::io::Write;
use std::path::{Path, PathBuf};

/// Newest entries kept in memory and, after compaction, on disk.
pub const HISTORY_MAX: usize = 10_000;

/// Collapse every way OSC 7 spells "this machine" — empty (no integration,
/// legacy entries), "localhost" (shim fallback), or the machine's own
/// hostname — into "", so the Dir scope's host pairing only separates REAL
/// remote hosts.
fn canon_host(h: &str) -> &str {
    if h.is_empty() || h.eq_ignore_ascii_case("localhost") || is_local_hostname(h) {
        ""
    } else {
        h
    }
}

/// Whether an OSC 7 reporting host means "this machine" (empty, the RFC
/// 8089 "localhost", or the machine's own hostname). Shared with every
/// consumer that acts on a shell-reported path on the LOCAL filesystem.
pub fn is_local_host(h: &str) -> bool {
    canon_host(h).is_empty()
}

/// Whether `h` names this machine (case-insensitive, with or without the
/// mDNS-style domain the shells sometimes include).
fn is_local_hostname(h: &str) -> bool {
    static LOCAL: std::sync::OnceLock<String> = std::sync::OnceLock::new();
    let local = LOCAL.get_or_init(|| {
        let mut buf = [0u8; 256];
        // SAFETY: gethostname writes at most buf.len() bytes and
        // NUL-terminates within it on every supported platform.
        let ok = unsafe { libc::gethostname(buf.as_mut_ptr().cast(), buf.len()) } == 0;
        if !ok {
            return String::new();
        }
        let end = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
        String::from_utf8_lossy(&buf[..end]).to_string()
    });
    if local.is_empty() {
        return false;
    }
    // Full-name comparison after lowercasing and stripping one mDNS
    // `.local` suffix; a bare reported name may also match this machine's
    // first label (shells report the short form). NEVER first-label vs
    // first-label: `e2b.remote.example` is not the Mac named `e2b.local`.
    let norm = |s: &str| {
        let s = s.to_ascii_lowercase();
        s.strip_suffix(".local").map(String::from).unwrap_or(s)
    };
    let h_n = norm(h);
    let l_n = norm(local);
    if h_n == l_n {
        return true;
    }
    !h_n.contains('.') && Some(h_n.as_str()) == l_n.split('.').next()
}

/// One finished shell command.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct HistoryEntry {
    pub cmd: String,
    /// The pane's OSC 7 working directory at finish time; empty when the
    /// shell never reported one.
    #[serde(default)]
    pub cwd: String,
    /// The OSC 7 reporting hostname at finish time; empty for the local
    /// machine. Without it, a command run at /home/dev/api over an in-pane
    /// ssh session is indistinguishable from one run at the same local
    /// path, and the Dir scope conflates them.
    #[serde(default)]
    pub host: String,
    #[serde(default)]
    pub exit: Option<i32>,
    #[serde(default)]
    pub dur_ms: u64,
    /// Seconds since the Unix epoch.
    #[serde(default)]
    pub ts: u64,
}

/// Which slice of history the popup searches; Ctrl+R cycles it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum HistoryScope {
    #[default]
    All,
    /// Only commands finished in the given directory.
    Dir,
    /// Only commands that exited non-zero (or died without a code).
    Failed,
}

impl HistoryScope {
    pub fn next(self) -> Self {
        match self {
            Self::All => Self::Dir,
            Self::Dir => Self::Failed,
            Self::Failed => Self::All,
        }
    }
}

/// The real store path, `~/.config/croft/command_history.jsonl`. The App
/// caches a loaded store in a field so tests can point it at a tempdir.
pub fn history_path() -> PathBuf {
    crate::prefs::config_dir().join("command_history.jsonl")
}

pub struct CommandHistory {
    path: PathBuf,
    pub entries: Vec<HistoryEntry>,
    /// Lines on disk (loaded + appended); drives compaction.
    disk_lines: usize,
}

impl CommandHistory {
    /// Load the store, tolerantly: an unparsable line is skipped, a missing
    /// file is an empty history. Only the newest [`HISTORY_MAX`] survive.
    pub fn load(path: &Path) -> Self {
        let raw = std::fs::read_to_string(path).unwrap_or_default();
        let mut entries: Vec<HistoryEntry> = raw
            .lines()
            .filter_map(|l| serde_json::from_str(l).ok())
            .collect();
        let disk_lines = entries.len();
        if entries.len() > HISTORY_MAX {
            let drop = entries.len() - HISTORY_MAX;
            entries.drain(..drop);
        }
        Self {
            path: path.to_path_buf(),
            entries,
            disk_lines,
        }
    }

    // Read by the app tests' wait loops; the app itself only appends.
    #[cfg_attr(not(test), allow(dead_code))]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Record a finished command: append to memory and to the JSONL file,
    /// compacting the file once it doubles past the cap.
    pub fn append(&mut self, entry: HistoryEntry) {
        if entry.cmd.trim().is_empty() {
            return;
        }
        let line = match serde_json::to_string(&entry) {
            Ok(l) => l,
            Err(_) => return,
        };
        self.entries.push(entry);
        if self.entries.len() > HISTORY_MAX {
            let drop = self.entries.len() - HISTORY_MAX;
            self.entries.drain(..drop);
        }
        self.disk_lines += 1;
        if self.disk_lines > HISTORY_MAX * 2 {
            self.rewrite();
            return;
        }
        if let Some(dir) = self.path.parent() {
            let _ = std::fs::create_dir_all(dir);
        }
        if let Ok(mut f) = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.path)
        {
            let _ = writeln!(f, "{line}");
        }
    }

    /// Rewrite the file with just the in-memory entries (compaction).
    fn rewrite(&mut self) {
        let mut out = String::new();
        for e in &self.entries {
            if let Ok(l) = serde_json::to_string(e) {
                out.push_str(&l);
                out.push('\n');
            }
        }
        if let Some(dir) = self.path.parent() {
            let _ = std::fs::create_dir_all(dir);
        }
        let _ = std::fs::write(&self.path, out);
        self.disk_lines = self.entries.len();
    }

    /// Search: newest first, one row per distinct command text (the newest
    /// occurrence wins, like atuin), case-insensitive substring `query`,
    /// narrowed by `scope` (`cwd` is the pane's current directory for
    /// [`HistoryScope::Dir`]).
    pub fn search(
        &self,
        query: &str,
        scope: HistoryScope,
        cwd: &str,
        host: &str,
    ) -> Vec<HistoryEntry> {
        let q = query.to_lowercase();
        let mut seen = std::collections::HashSet::new();
        let mut out = Vec::new();
        for e in self.entries.iter().rev() {
            match scope {
                HistoryScope::All => {}
                HistoryScope::Dir => {
                    // Same directory means same directory on the same
                    // machine: the host pairs with the path. Local hosts
                    // normalize to one value — the shims report the local
                    // hostname (or "localhost"), while legacy entries and
                    // panes without integration carry "" — so a local pane
                    // still sees its own older history.
                    if e.cwd != cwd || canon_host(&e.host) != canon_host(host) {
                        continue;
                    }
                }
                HistoryScope::Failed => {
                    if e.exit == Some(0) {
                        continue;
                    }
                }
            }
            if !q.is_empty() && !e.cmd.to_lowercase().contains(&q) {
                continue;
            }
            if seen.insert(e.cmd.clone()) {
                out.push(e.clone());
            }
        }
        out
    }
}

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

    fn entry(cmd: &str, cwd: &str, exit: Option<i32>, ts: u64) -> HistoryEntry {
        HistoryEntry {
            cmd: cmd.to_string(),
            cwd: cwd.to_string(),
            host: String::new(),
            exit,
            dur_ms: 42,
            ts,
        }
    }

    /// A remote FQDN that merely SHARES this machine's first DNS label is
    /// a different machine: `e2b.remote.example` on a Mac named
    /// `e2b.local` used to pass as local (first-label comparison), letting
    /// a remote-reported cwd flow into local splits and session restore.
    /// The legitimate spellings of THIS machine still pass: the exact
    /// name, the mDNS `.local` variant, and the bare short name.
    #[test]
    fn a_remote_fqdn_sharing_the_short_name_is_not_local() {
        let out = std::process::Command::new("hostname").output().unwrap();
        let local = String::from_utf8_lossy(&out.stdout).trim().to_string();
        if local.is_empty() {
            return;
        }
        let first = local.split('.').next().unwrap().to_string();
        assert!(
            !is_local_host(&format!("{first}.remote.example")),
            "an unrelated FQDN sharing the first label is another machine"
        );
        assert!(is_local_host(&local), "the machine's own name is local");
        assert!(is_local_host(&first), "the bare short name is local");
        assert!(
            is_local_host(&format!("{first}.local")),
            "the mDNS spelling is local"
        );
        assert!(
            is_local_host(&first.to_uppercase()),
            "hostname comparison is case-insensitive"
        );
    }

    /// A command run at /x over an in-pane ssh session must not surface in
    /// the Dir scope of a LOCAL pane sitting at /x — same path, different
    /// machine.
    #[test]
    fn dir_scope_separates_hosts_sharing_a_path() {
        let tmp = tempfile::tempdir().unwrap();
        let mut h = CommandHistory::load(&tmp.path().join("h.jsonl"));
        h.append(entry("local-build", "/x", Some(0), 1));
        let mut remote = entry("remote-build", "/x", Some(0), 2);
        remote.host = String::from("prod-box");
        h.append(remote);
        let local = h.search("", HistoryScope::Dir, "/x", "");
        assert_eq!(local.len(), 1, "only the local /x command");
        assert_eq!(local[0].cmd, "local-build");
        let remote = h.search("", HistoryScope::Dir, "/x", "prod-box");
        assert_eq!(remote.len(), 1, "only the remote /x command");
        assert_eq!(remote[0].cmd, "remote-build");

        // Every spelling of "this machine" is one host: a legacy entry
        // (host "") must surface in a pane whose shim reports "localhost",
        // and a "localhost" entry in a pane with no integration.
        let mut shimmed = entry("shimmed-build", "/x", Some(0), 3);
        shimmed.host = String::from("localhost");
        h.append(shimmed);
        let seen = h.search("", HistoryScope::Dir, "/x", "localhost");
        let cmds: Vec<&str> = seen.iter().map(|e| e.cmd.as_str()).collect();
        assert!(
            cmds.contains(&"local-build") && cmds.contains(&"shimmed-build"),
            "legacy and shim-reported local entries are one history: {cmds:?}"
        );
        assert!(!cmds.contains(&"remote-build"));

        // Schema round trip: a reload keeps the stored host, and a legacy
        // JSONL record with no "host" key deserializes to the local "".
        let re = CommandHistory::load(&h.path);
        let back = re.entries.iter().find(|e| e.cmd == "remote-build").unwrap();
        assert_eq!(back.host, "prod-box", "the host survives the reload");
        let legacy: HistoryEntry =
            serde_json::from_str(r#"{"cmd":"old","cwd":"/x","exit":0,"dur_ms":1,"ts":1}"#).unwrap();
        assert_eq!(legacy.host, "", "a pre-host record defaults to local");
    }

    #[test]
    fn append_persists_and_load_round_trips() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("h.jsonl");
        let mut h = CommandHistory::load(&path);
        assert!(h.is_empty());
        h.append(entry("cargo test", "/repo", Some(0), 100));
        h.append(entry("ls -la", "/tmp", Some(0), 101));
        h.append(entry("   ", "/tmp", Some(0), 102)); // blank: dropped
        let re = CommandHistory::load(&path);
        assert_eq!(re.entries.len(), 2, "blank commands are never recorded");
        assert_eq!(re.entries[0].cmd, "cargo test");
        assert_eq!(re.entries[1].cwd, "/tmp");
        // A corrupt line is skipped, not fatal.
        std::fs::write(
            &path,
            format!(
                "not json at all\n{}\n",
                serde_json::to_string(&entry("ok", "/x", Some(1), 103)).unwrap()
            ),
        )
        .unwrap();
        let re = CommandHistory::load(&path);
        assert_eq!(re.entries.len(), 1);
        assert_eq!(re.entries[0].cmd, "ok");
    }

    #[test]
    fn search_dedups_newest_first_and_honours_scopes() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("h.jsonl");
        let mut h = CommandHistory::load(&path);
        h.append(entry("cargo test", "/repo", Some(0), 1));
        h.append(entry("cargo build", "/repo", Some(101), 2));
        h.append(entry("ls", "/tmp", Some(0), 3));
        h.append(entry("cargo test", "/repo", Some(1), 4)); // rerun, newest

        let all = h.search("", HistoryScope::All, "", "");
        assert_eq!(
            all.iter().map(|e| e.cmd.as_str()).collect::<Vec<_>>(),
            vec!["cargo test", "ls", "cargo build"],
            "newest first, duplicate command texts collapse to the newest run"
        );
        assert_eq!(all[0].exit, Some(1), "the newest occurrence wins");

        let q = h.search("CARGO", HistoryScope::All, "", "");
        assert_eq!(q.len(), 2, "query is case-insensitive substring");

        let dir = h.search("", HistoryScope::Dir, "/tmp", "");
        assert_eq!(
            dir.iter().map(|e| e.cmd.as_str()).collect::<Vec<_>>(),
            vec!["ls"]
        );

        let failed = h.search("", HistoryScope::Failed, "", "");
        assert_eq!(
            failed.iter().map(|e| e.cmd.as_str()).collect::<Vec<_>>(),
            vec!["cargo test", "cargo build"],
            "failed-only keeps non-zero exits"
        );
    }

    #[test]
    fn the_file_compacts_once_it_doubles_past_the_cap() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("h.jsonl");
        let mut h = CommandHistory::load(&path);
        // Simulate an old, bloated file: disk_lines is what triggers
        // compaction, so seed it near the threshold instead of writing
        // 20k real lines.
        h.disk_lines = HISTORY_MAX * 2;
        for i in 0..3 {
            h.append(entry(&format!("cmd-{i}"), "/", Some(0), i as u64));
        }
        let lines = std::fs::read_to_string(&path).unwrap().lines().count();
        assert!(
            lines <= HISTORY_MAX,
            "compaction must rewrite the file down to the in-memory entries, got {lines} lines"
        );
        assert_eq!(h.disk_lines, h.entries.len());
    }
}