openlatch-client 0.6.3

OpenLatch runtime enforcement node — the capture-and-enforce adapter that evaluates every covered action against a coding agent's Autonomy Zone before it runs
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
//! One marker-guarded line in the developer's shell profiles that sources the env file
//! ([`crate::hooks::proxy_env_file`]) when it exists. Install-scoped: written on a `ProxyEnv`
//! wire by the daemon under the machine-global gate, removed by whole-product uninstall (plan
//! 04). Stop, restart and a failed preflight never touch it: the line is inert while `env.sh` is
//! absent.
//!
//! Explicit `home` / `documents` parameters so no test here reads `dirs::home_dir()`. fish is
//! out (D-27).

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

use crate::error::{OlError, ERR_MODEL_RELAY_IO};

pub const MARKER: &str = "# openlatch-model-relay";

/// EXACT: `if [ -f '<env.sh>' ]; then . '<env.sh>'; fi  # openlatch-model-relay`
pub fn posix_line(env_sh: &Path) -> String {
    let p = env_sh.display();
    format!("if [ -f '{p}' ]; then . '{p}'; fi  {MARKER}")
}

/// EXACT: `if (Test-Path -LiteralPath '<env.ps1>') { . '<env.ps1>' }  # openlatch-model-relay`
pub fn powershell_line(env_ps1: &Path) -> String {
    let p = env_ps1.display();
    format!("if (Test-Path -LiteralPath '{p}') {{ . '{p}' }}  {MARKER}")
}

fn io_err(e: impl std::fmt::Display) -> OlError {
    OlError::new(ERR_MODEL_RELAY_IO, e.to_string())
}

/// Replace an existing marker line's span in place — preserving that line's own end-of-line
/// convention and every other byte in the file — or, with none found, ensure the file ends in
/// `\n` and append `line\n`. Returns whether the file changed.
fn ensure_line_in_file(path: &Path, line: &str) -> Result<bool, OlError> {
    let existing = std::fs::read_to_string(path).unwrap_or_default();

    let mut offset = 0usize;
    let mut found: Option<(usize, usize, &str)> = None;
    for raw_line in existing.split_inclusive('\n') {
        let content = raw_line.strip_suffix('\n').unwrap_or(raw_line);
        let content = content.strip_suffix('\r').unwrap_or(content);
        if content.ends_with(MARKER) {
            let ending = if raw_line.ends_with("\r\n") {
                "\r\n"
            } else if raw_line.ends_with('\n') {
                "\n"
            } else {
                ""
            };
            found = Some((offset, offset + raw_line.len(), ending));
            break;
        }
        offset += raw_line.len();
    }

    let new_content = match found {
        Some((start, end, ending)) => {
            if existing[start..end] == *format!("{line}{ending}") {
                return Ok(false);
            }
            format!("{}{line}{ending}{}", &existing[..start], &existing[end..])
        }
        None => {
            let mut out = existing.clone();
            if !out.is_empty() && !out.ends_with('\n') {
                out.push('\n');
            }
            out.push_str(line);
            out.push('\n');
            out
        }
    };
    if new_content == existing {
        return Ok(false);
    }
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(io_err)?;
    }
    crate::fs_secure::write_preserving_mode(path, new_content.as_bytes()).map_err(io_err)?;
    Ok(true)
}

fn remove_marker_lines(path: &Path) -> Result<bool, OlError> {
    let existing = std::fs::read_to_string(path).map_err(io_err)?;
    let mut out = String::with_capacity(existing.len());
    let mut changed = false;
    for raw_line in existing.split_inclusive('\n') {
        let content = raw_line.strip_suffix('\n').unwrap_or(raw_line);
        let content = content.strip_suffix('\r').unwrap_or(content);
        if content.ends_with(MARKER) {
            changed = true;
            continue;
        }
        out.push_str(raw_line);
    }
    if !changed {
        return Ok(false);
    }
    crate::fs_secure::write_preserving_mode(path, out.as_bytes()).map_err(io_err)?;
    Ok(true)
}

/// The candidate POSIX rc files that exist, or — when none does and `create_posix_rc` — the
/// ONE this install creates, named from `login_shell`'s suffix.
fn posix_candidates(home: &Path, login_shell: Option<&str>, create_posix_rc: bool) -> Vec<PathBuf> {
    let candidates = [
        home.join(".zshrc"),
        home.join(".bashrc"),
        home.join(".bash_profile"),
        home.join(".profile"),
    ];
    let existing: Vec<PathBuf> = candidates.iter().filter(|p| p.exists()).cloned().collect();
    if !existing.is_empty() || !create_posix_rc {
        return existing;
    }
    // macOS's default zsh never reads .profile; creating it there would be a line nobody
    // sources. A parameter, not a `cfg!`, so the no-create branch (Windows) is tested on every
    // OS.
    let name = match login_shell {
        Some(s) if s.ends_with("zsh") => ".zshrc",
        Some(s) if s.ends_with("bash") => ".bashrc",
        _ => ".profile",
    };
    vec![home.join(name)]
}

fn powershell_candidates(documents: Option<&Path>) -> Vec<PathBuf> {
    // Never created: client Windows defaults to the Restricted execution policy, and a created
    // profile.ps1 prints a red error on every PowerShell start there.
    match documents {
        None => Vec::new(),
        Some(documents) => [
            documents.join("PowerShell").join("profile.ps1"),
            documents.join("WindowsPowerShell").join("profile.ps1"),
        ]
        .into_iter()
        .filter(|p| p.exists())
        .collect(),
    }
}

pub(crate) fn ensure_line_with(
    home: &Path,
    documents: Option<&Path>,
    env_sh: &Path,
    env_ps1: &Path,
    login_shell: Option<&str>,
    create_posix_rc: bool,
) -> Result<Vec<PathBuf>, OlError> {
    let mut touched = Vec::new();
    let line = posix_line(env_sh);
    for path in posix_candidates(home, login_shell, create_posix_rc) {
        if ensure_line_in_file(&path, &line)? {
            touched.push(path);
        }
    }
    let ps1_line = powershell_line(env_ps1);
    for path in powershell_candidates(documents) {
        if ensure_line_in_file(&path, &ps1_line)? {
            touched.push(path);
        }
    }
    Ok(touched)
}

pub fn ensure_line(
    home: &Path,
    documents: Option<&Path>,
    env_sh: &Path,
    env_ps1: &Path,
) -> Result<Vec<PathBuf>, OlError> {
    ensure_line_with(
        home,
        documents,
        env_sh,
        env_ps1,
        std::env::var("SHELL").ok().as_deref(),
        !cfg!(windows),
    )
}

/// Remove every line ending in [`MARKER`] from the same candidate set (existing files only).
/// Never deletes a file, even one [`ensure_line`] created. Idempotent.
///
/// The same candidate helpers `ensure_line_with` uses: `posix_candidates(home, None, false)`
/// takes the `create_posix_rc = false` branch unconditionally, which returns exactly the existing
/// files among the four rc paths — `login_shell` is never consulted on that branch, so `None`
/// here changes nothing — and never creates one; `powershell_candidates` already filters to
/// existing files. Neither ever invents a path, so no `.exists()` check is needed here.
pub fn remove_line(home: &Path, documents: Option<&Path>) -> Result<Vec<PathBuf>, OlError> {
    let mut touched = Vec::new();
    for path in posix_candidates(home, None, false)
        .into_iter()
        .chain(powershell_candidates(documents))
    {
        if remove_marker_lines(&path)? {
            touched.push(path);
        }
    }
    Ok(touched)
}

/// Whether every existing candidate file carries a line ending in [`MARKER`] — what
/// [`ensure_line`] left behind. With no candidate at all: `false` where `ensure_line` would create
/// one (POSIX), `true` where it never does (Windows), so a host with nothing to write is never
/// reported as missing the line. Read-only.
pub fn is_present(home: &Path, documents: Option<&Path>) -> bool {
    let has_marker = |path: &PathBuf| {
        std::fs::read_to_string(path).is_ok_and(|content| {
            content
                .lines()
                .any(|line| line.strip_suffix('\r').unwrap_or(line).ends_with(MARKER))
        })
    };
    let candidates: Vec<PathBuf> = posix_candidates(home, None, false)
        .into_iter()
        .chain(powershell_candidates(documents))
        .collect();
    if candidates.is_empty() {
        return cfg!(windows);
    }
    candidates.iter().all(has_marker)
}

/// Every file [`ensure_line`] may write, existing or not — the watch set for [`is_present`].
pub fn watch_files(home: &Path, documents: Option<&Path>) -> Vec<PathBuf> {
    let mut files = vec![
        home.join(".zshrc"),
        home.join(".bashrc"),
        home.join(".bash_profile"),
        home.join(".profile"),
    ];
    if let Some(documents) = documents {
        files.push(documents.join("PowerShell").join("profile.ps1"));
        files.push(documents.join("WindowsPowerShell").join("profile.ps1"));
    }
    files
}

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

    #[test]
    fn is_present_needs_the_marker_in_every_existing_rc() {
        let home = tempfile::tempdir().expect("tempdir");
        let env_sh = home.path().join("env.sh");
        let env_ps1 = home.path().join("env.ps1");
        assert_eq!(is_present(home.path(), None), cfg!(windows), "no rc file");
        std::fs::write(home.path().join(".zshrc"), "export A=1\n").unwrap();
        std::fs::write(home.path().join(".bashrc"), "").unwrap();
        ensure_line_with(home.path(), None, &env_sh, &env_ps1, None, false).unwrap();
        assert!(is_present(home.path(), None));
        remove_marker_lines(&home.path().join(".bashrc")).unwrap();
        assert!(!is_present(home.path(), None), "one rc lost the line");
    }

    #[test]
    fn posix_line_is_exact() {
        let env_sh = Path::new("/OL/model-relay/env.sh");
        assert_eq!(
            posix_line(env_sh),
            "if [ -f '/OL/model-relay/env.sh' ]; then . '/OL/model-relay/env.sh'; fi  \
             # openlatch-model-relay"
        );
    }

    #[test]
    fn powershell_line_is_exact() {
        let env_ps1 = Path::new("C:\\OL\\model-relay\\env.ps1");
        assert_eq!(
            powershell_line(env_ps1),
            "if (Test-Path -LiteralPath 'C:\\OL\\model-relay\\env.ps1') { . \
             'C:\\OL\\model-relay\\env.ps1' }  # openlatch-model-relay"
        );
    }

    #[test]
    fn the_line_is_added_once_to_existing_files_only() {
        let dir = tempfile::tempdir().expect("tempdir");
        let home = dir.path();
        let zshrc_seed = "export PATH=/usr/local/bin:$PATH\n";
        let bashrc_seed = "alias ll='ls -la'\n";
        std::fs::write(home.join(".zshrc"), zshrc_seed).expect("seed zshrc");
        std::fs::write(home.join(".bashrc"), bashrc_seed).expect("seed bashrc");
        let env_sh = home.join("ol").join("env.sh");
        let env_ps1 = home.join("ol").join("env.ps1");

        let touched = ensure_line_with(home, None, &env_sh, &env_ps1, None, true).expect("ensure");
        assert_eq!(touched, vec![home.join(".zshrc"), home.join(".bashrc")]);
        assert!(
            !home.join(".profile").exists(),
            ".bash_profile/.profile must not be created when .zshrc and .bashrc exist"
        );
        assert!(!home.join(".bash_profile").exists());

        let zshrc_after = std::fs::read_to_string(home.join(".zshrc")).unwrap();
        assert_eq!(
            zshrc_after,
            format!("{zshrc_seed}{}\n", posix_line(&env_sh)),
            "the customer's line is byte-identical, ours is appended after it"
        );
        let bashrc_after = std::fs::read_to_string(home.join(".bashrc")).unwrap();
        assert_eq!(
            bashrc_after,
            format!("{bashrc_seed}{}\n", posix_line(&env_sh))
        );

        let touched2 =
            ensure_line_with(home, None, &env_sh, &env_ps1, None, true).expect("second call");
        assert!(touched2.is_empty(), "a second call must change nothing");
        assert_eq!(
            std::fs::read_to_string(home.join(".zshrc")).unwrap(),
            zshrc_after
        );
        assert_eq!(
            std::fs::read_to_string(home.join(".bashrc")).unwrap(),
            bashrc_after
        );
    }

    #[test]
    fn the_login_shells_rc_is_created_when_none_exists() {
        let dir = tempfile::tempdir().expect("tempdir");
        let home = dir.path();
        let env_sh = home.join("ol").join("env.sh");
        let env_ps1 = home.join("ol").join("env.ps1");

        let touched =
            ensure_line_with(home, None, &env_sh, &env_ps1, Some("/bin/zsh"), true).expect("zsh");
        assert_eq!(touched, vec![home.join(".zshrc")]);
        std::fs::remove_file(home.join(".zshrc")).unwrap();

        let touched =
            ensure_line_with(home, None, &env_sh, &env_ps1, Some("/bin/bash"), true).expect("bash");
        assert_eq!(touched, vec![home.join(".bashrc")]);
        std::fs::remove_file(home.join(".bashrc")).unwrap();

        let touched =
            ensure_line_with(home, None, &env_sh, &env_ps1, None, true).expect("no login shell");
        assert_eq!(touched, vec![home.join(".profile")]);
        std::fs::remove_file(home.join(".profile")).unwrap();

        // No-create row: `create_posix_rc = false` and no rc file exists → nothing is created.
        let touched = ensure_line_with(home, None, &env_sh, &env_ps1, Some("/bin/zsh"), false)
            .expect("no create");
        assert!(touched.is_empty(), "the result must hold no POSIX path");
        for name in [".zshrc", ".bashrc", ".bash_profile", ".profile"] {
            assert!(!home.join(name).exists(), "{name} must not be created");
        }

        // An existing `.bashrc`, `create_posix_rc = false`: it gains the line, nothing is
        // created.
        std::fs::write(home.join(".bashrc"), "").unwrap();
        let touched = ensure_line_with(home, None, &env_sh, &env_ps1, Some("/bin/zsh"), false)
            .expect("existing bashrc, no create");
        assert_eq!(touched, vec![home.join(".bashrc")]);
        assert!(!home.join(".zshrc").exists());
        assert!(!home.join(".profile").exists());
    }

    #[test]
    fn the_line_is_removed_cleanly() {
        let dir = tempfile::tempdir().expect("tempdir");
        let home = dir.path();
        let seed = "export PATH=/usr/local/bin:$PATH\n";
        std::fs::write(home.join(".zshrc"), seed).expect("seed");
        let env_sh = home.join("ol").join("env.sh");
        let env_ps1 = home.join("ol").join("env.ps1");
        ensure_line_with(home, None, &env_sh, &env_ps1, Some("/bin/zsh"), true).expect("ensure");
        let zshrc = home.join(".zshrc");
        assert_eq!(
            std::fs::read_to_string(&zshrc).unwrap(),
            format!("{seed}{}\n", posix_line(&env_sh))
        );

        let touched = remove_line(home, None).expect("remove");
        assert_eq!(touched, vec![zshrc.clone()]);
        assert!(zshrc.exists(), "the file itself is never deleted");
        assert_eq!(
            std::fs::read_to_string(&zshrc).unwrap(),
            seed,
            "the remainder is byte-identical to the original"
        );

        assert!(remove_line(home, None).expect("remove again").is_empty());
    }

    #[test]
    fn powershell_profiles_are_edited_only_when_they_exist() {
        let dir = tempfile::tempdir().expect("tempdir");
        let home = dir.path();
        let documents = dir.path().join("Documents");
        let ps_dir = documents.join("PowerShell");
        std::fs::create_dir_all(&ps_dir).expect("dir");
        let profile = ps_dir.join("profile.ps1");
        std::fs::write(&profile, "").expect("seed");
        let env_sh = home.join("ol").join("env.sh");
        let env_ps1 = home.join("ol").join("env.ps1");

        let touched = ensure_line_with(home, Some(&documents), &env_sh, &env_ps1, None, false)
            .expect("ensure");
        assert_eq!(touched, vec![profile.clone()]);
        assert!(std::fs::read_to_string(&profile).unwrap().contains(MARKER));
        assert!(
            !documents.join("WindowsPowerShell").exists(),
            "an absent profile must never be created"
        );
    }

    #[test]
    #[cfg(unix)]
    fn the_sourcing_line_is_inert_without_the_env_file() {
        let dir = tempfile::tempdir().expect("tempdir");
        let home = dir.path();
        let profile = home.join(".zshrc");
        // Never created: the whole point is that the sourced line is inert while it is absent.
        let env_sh = home.join("ol").join("env.sh");
        std::fs::write(&profile, format!("{}\n", posix_line(&env_sh))).expect("seed");

        let out = std::process::Command::new("sh")
            .arg("-c")
            .arg(format!(". '{}'; echo ok", profile.display()))
            .output()
            .expect("run sh");
        assert!(
            out.status.success(),
            "stderr: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        assert_eq!(String::from_utf8_lossy(&out.stdout), "ok\n");
    }
}