mnml-rs 0.2.14

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
//! Append-only history log at `<workspace>/.rqst/history.jsonl`.
//!
//! One JSON line per completed request. Used for ad-hoc forensic queries:
//!   grep '"status":401' .rqst/history.jsonl
//!   jq -c 'select(.duration_ms > 1000)' .rqst/history.jsonl
//!
//! Append is open(append) + write — POSIX guarantees atomic appends for
//! lines under PIPE_BUF (4096 on Linux/macOS), and our lines are well
//! under that. No rename trick needed.
//!
//! 2026-06-20 — also mirror each line into a *global* log at
//! `~/.config/mnml/history-global.jsonl` with a `"workspace"` field
//! identifying the source. Lets `:http.history_global` recall a
//! request you made from any project, useful when you remember
//! firing it but not which workspace you were in.

use serde_json::Value;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};

pub struct Entry<'a> {
    pub method: &'a str,
    pub url: &'a str,
    pub status: Option<u16>,
    pub duration_ms: Option<u128>,
    pub body_bytes: Option<usize>,
    pub error: Option<&'a str>,
    /// http-2nd 2026-06-28 SEV-3c — request headers as
    /// `Vec<(name, value)>`. Stored alongside the response
    /// metadata so the history picker can rebuild a usable
    /// curl command, not just `curl -X METHOD URL`.
    pub headers: Option<&'a [(String, String)]>,
    /// The serialised request body (utf-8 string). None when
    /// the request was bodyless.
    pub request_body: Option<&'a str>,
}

pub fn append(workspace: &Path, entry: &Entry) {
    let dir = workspace.join(".rqst");
    if fs::create_dir_all(&dir).is_err() {
        return;
    }
    let path = dir.join("history.jsonl");
    let ts = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    let payload = serde_json::json!({
        "ts": ts,
        "method": entry.method,
        "url": entry.url,
        "status": entry.status,
        "duration_ms": entry.duration_ms,
        "body_bytes": entry.body_bytes,
        "error": entry.error,
        "headers": entry.headers,
        "request_body": entry.request_body,
    });
    let mut line = match serde_json::to_string(&payload) {
        Ok(s) => s,
        Err(_) => return,
    };
    line.push('\n');
    if let Ok(mut f) = OpenOptions::new().create(true).append(true).open(&path) {
        let _ = f.write_all(line.as_bytes());
    }
}

/// Wrapper that writes to the workspace log AND mirrors to the
/// global log. The global log lets `:http.history_global` show
/// cross-workspace request history. App callers should prefer this
/// over [`append`] — tests use [`append`] directly to avoid HOME
/// pollution.
pub fn append_with_global_mirror(workspace: &Path, entry: &Entry) {
    append(workspace, entry);
    let ts = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    append_global(workspace, entry, ts);
}

/// Mirror an entry into `~/.config/mnml/history-global.jsonl` with a
/// `workspace` field added so `:http.history_global` can show where
/// the request originated. Best-effort — silently no-ops if HOME
/// isn't set or the file can't be opened.
fn append_global(workspace: &Path, entry: &Entry, ts: u128) {
    let Some(path) = global_history_path() else {
        return;
    };
    if let Some(parent) = path.parent()
        && fs::create_dir_all(parent).is_err()
    {
        return;
    }
    let workspace_label = workspace
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("")
        .to_string();
    let payload = serde_json::json!({
        "ts": ts,
        "workspace": workspace_label,
        "workspace_path": workspace.to_string_lossy(),
        "method": entry.method,
        "url": entry.url,
        "status": entry.status,
        "duration_ms": entry.duration_ms,
        "body_bytes": entry.body_bytes,
        "error": entry.error,
        "headers": entry.headers,
        "request_body": entry.request_body,
    });
    let mut line = match serde_json::to_string(&payload) {
        Ok(s) => s,
        Err(_) => return,
    };
    line.push('\n');
    if let Ok(mut f) = OpenOptions::new().create(true).append(true).open(&path) {
        let _ = f.write_all(line.as_bytes());
    }
}

/// `~/.config/mnml/history-global.jsonl`, or whatever
/// `$MNML_HISTORY_GLOBAL_PATH` points to (used by tests to avoid
/// touching the real user log). Returns `None` if neither is set.
pub fn global_history_path() -> Option<PathBuf> {
    if let Some(p) = std::env::var_os("MNML_HISTORY_GLOBAL_PATH") {
        return Some(PathBuf::from(p));
    }
    Some(crate::data_root::data_root().join("history-global.jsonl"))
}

/// Read the last `n` entries from the global history log (most
/// recent last). Used by `:http.history_global`.
pub fn tail_global(n: usize) -> Vec<Value> {
    let Some(path) = global_history_path() else {
        return Vec::new();
    };
    let Ok(text) = fs::read_to_string(&path) else {
        return Vec::new();
    };
    let mut out: Vec<Value> = text
        .lines()
        .rev()
        .take(n)
        .filter_map(|l| serde_json::from_str(l).ok())
        .collect();
    out.reverse();
    out
}

/// Rebuild a `curl` command from a history entry. Uses the persisted
/// `headers` + `request_body` when present; falls back to the minimal
/// `curl -X METHOD URL` form for older entries. Returns
/// `(curl_text, method, url)` so callers can drive `open_curl_scratch`.
/// Shared between the `HistoryRows` picker and the sectioned HTTP
/// sidebar so both re-fire history the same way.
pub fn entry_to_curl(v: &Value) -> (String, String, String) {
    let method = v
        .get("method")
        .and_then(|s| s.as_str())
        .unwrap_or("GET")
        .to_string();
    let url = v
        .get("url")
        .and_then(|s| s.as_str())
        .unwrap_or("")
        .to_string();
    let mut curl = String::from("curl");
    curl.push_str(&format!(" -X {method}"));
    if let Some(headers) = v.get("headers").and_then(|h| h.as_array()) {
        for h in headers {
            if let Some(pair) = h.as_array()
                && pair.len() == 2
                && let (Some(name), Some(value)) = (pair[0].as_str(), pair[1].as_str())
            {
                let escaped_value = value.replace('\'', r"'\''");
                curl.push_str(&format!(" -H '{name}: {escaped_value}'"));
            }
        }
    }
    if let Some(body) = v.get("request_body").and_then(|b| b.as_str())
        && !body.is_empty()
    {
        let escaped_body = body.replace('\'', r"'\''");
        curl.push_str(&format!(" --data-raw '{escaped_body}'"));
    }
    curl.push_str(&format!(" '{url}'"));
    (curl, method, url)
}

/// Read the last `n` history entries (most recent last). Used by the
/// (future) Ctrl+H history modal. Reads the entire file and tail-truncates,
/// which is fine for files up to a few MB; rotate later if needed.
pub fn tail(workspace: &Path, n: usize) -> Vec<Value> {
    let path = workspace.join(".rqst").join("history.jsonl");
    let Ok(text) = fs::read_to_string(&path) else {
        return Vec::new();
    };
    let mut out: Vec<Value> = text
        .lines()
        .rev()
        .take(n)
        .filter_map(|l| serde_json::from_str(l).ok())
        .collect();
    out.reverse();
    out
}

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

    fn temp(name: &str) -> std::path::PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "rqst-history-{name}-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn append_writes_jsonl_in_dot_rqst() {
        let dir = temp("append");
        append(
            &dir,
            &Entry {
                method: "POST",
                url: "https://x/y",
                status: Some(200),
                duration_ms: Some(123),
                body_bytes: Some(456),
                error: None,
                headers: None,
                request_body: None,
            },
        );
        let path = dir.join(".rqst/history.jsonl");
        assert!(path.exists());
        let text = fs::read_to_string(&path).unwrap();
        let lines: Vec<&str> = text.lines().collect();
        assert_eq!(lines.len(), 1);
        let parsed: Value = serde_json::from_str(lines[0]).unwrap();
        assert_eq!(parsed["method"], "POST");
        assert_eq!(parsed["status"], 200);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn tail_returns_last_n_entries_in_order() {
        let dir = temp("tail");
        for i in 0..5 {
            append(
                &dir,
                &Entry {
                    method: "GET",
                    url: &format!("https://x/{i}"),
                    status: Some(200),
                    duration_ms: Some(i),
                    body_bytes: Some(0),
                    error: None,
                    headers: None,
                    request_body: None,
                },
            );
        }
        let recent = tail(&dir, 3);
        assert_eq!(recent.len(), 3);
        // last three URLs in insertion order
        assert_eq!(recent[0]["url"], "https://x/2");
        assert_eq!(recent[1]["url"], "https://x/3");
        assert_eq!(recent[2]["url"], "https://x/4");
        let _ = fs::remove_dir_all(&dir);
    }

    // Serialize the two tests that mutate the process-wide
    // MNML_HISTORY_GLOBAL_PATH env var. Serialized against every
    // other process-env mutator across the crate via
    // crate::test_env_lock(), so a discovery/cdp/prompt HOME test
    // can't race the global-history-path override.

    #[test]
    fn append_with_global_mirror_writes_workspace_and_global() {
        let _guard = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let dir = temp("mirror");
        let global = dir.join("global.jsonl");
        // EnvGuard restores MNML_HISTORY_GLOBAL_PATH on scope exit,
        // panic-safe. Prior manual save/restore skipped restoration
        // whenever a mid-test assertion failed.
        let _env = crate::EnvGuard::set("MNML_HISTORY_GLOBAL_PATH", &global);
        append_with_global_mirror(
            &dir,
            &Entry {
                method: "GET",
                url: "https://x/global",
                status: Some(200),
                duration_ms: Some(7),
                body_bytes: Some(0),
                error: None,
                headers: None,
                request_body: None,
            },
        );
        assert!(dir.join(".rqst/history.jsonl").exists());
        assert!(global.exists());
        let text = fs::read_to_string(&global).unwrap();
        let entry: Value = serde_json::from_str(text.lines().next().unwrap()).unwrap();
        assert_eq!(entry["url"], "https://x/global");
        assert!(entry["workspace"].as_str().is_some());
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn tail_global_returns_n_recent_from_env_path() {
        let _guard = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let dir = temp("tail-global");
        let global = dir.join("global.jsonl");
        let _env = crate::EnvGuard::set("MNML_HISTORY_GLOBAL_PATH", &global);
        for i in 0..4 {
            append_with_global_mirror(
                &dir,
                &Entry {
                    method: "GET",
                    url: &format!("https://x/g/{i}"),
                    status: Some(200),
                    duration_ms: Some(i),
                    body_bytes: Some(0),
                    error: None,
                    headers: None,
                    request_body: None,
                },
            );
        }
        let recent = tail_global(2);
        assert_eq!(recent.len(), 2);
        assert_eq!(recent[0]["url"], "https://x/g/2");
        assert_eq!(recent[1]["url"], "https://x/g/3");
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn append_records_error_when_set() {
        let dir = temp("err");
        append(
            &dir,
            &Entry {
                method: "GET",
                url: "https://broken",
                status: None,
                duration_ms: None,
                body_bytes: None,
                error: Some("connection refused"),
                headers: None,
                request_body: None,
            },
        );
        let entries = tail(&dir, 1);
        assert_eq!(entries[0]["status"], serde_json::Value::Null);
        assert_eq!(entries[0]["error"], "connection refused");
        let _ = fs::remove_dir_all(&dir);
    }

    /// test-writer 2026-06-28 coverage gap: the new headers +
    /// request_body fields must persist correctly. Without this
    /// lock-in, an accidental rename in the json! call would
    /// silently break picker.rs's curl-rebuild path.
    #[test]
    fn append_writes_headers_and_body_to_jsonl() {
        let dir = temp("headers-body");
        let headers = vec![
            ("Content-Type".to_string(), "application/json".to_string()),
            ("Authorization".to_string(), "Bearer abc123".to_string()),
        ];
        let body = r#"{"name":"alice"}"#;
        append(
            &dir,
            &Entry {
                method: "POST",
                url: "https://x/y",
                status: Some(200),
                duration_ms: Some(50),
                body_bytes: Some(10),
                error: None,
                headers: Some(&headers),
                request_body: Some(body),
            },
        );
        let entries = tail(&dir, 1);
        assert_eq!(entries.len(), 1);
        let v = &entries[0];
        let h = v["headers"].as_array().expect("headers is array");
        assert_eq!(h.len(), 2);
        assert_eq!(h[0][0], "Content-Type");
        assert_eq!(h[0][1], "application/json");
        assert_eq!(h[1][0], "Authorization");
        assert_eq!(h[1][1], "Bearer abc123");
        assert_eq!(v["request_body"].as_str(), Some(body));
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn append_with_none_headers_and_body_serialises_null() {
        let dir = temp("none-headers");
        append(
            &dir,
            &Entry {
                method: "GET",
                url: "https://x/y",
                status: Some(200),
                duration_ms: Some(5),
                body_bytes: Some(0),
                error: None,
                headers: None,
                request_body: None,
            },
        );
        let entries = tail(&dir, 1);
        assert_eq!(entries[0]["headers"], serde_json::Value::Null);
        assert_eq!(entries[0]["request_body"], serde_json::Value::Null);
        let _ = fs::remove_dir_all(&dir);
    }
}