nono-cli 0.50.1

CLI for nono capability-based sandbox
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
use crate::cli::{Commands, RunArgs};
use crate::sandbox_prepare::resolve_detached_cwd_prompt_response;
use crate::{
    output, session, update_check, DETACHED_CWD_PROMPT_RESPONSE_ENV, DETACHED_LAUNCH_ENV,
    DETACHED_SESSION_ID_ENV,
};
#[cfg(unix)]
use nix::libc;
use nono::{NonoError, Result};
#[cfg(unix)]
use std::os::unix::process::CommandExt as _;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};

pub(crate) fn allows_pre_exec_update_check(command: &Commands) -> bool {
    !matches!(
        command,
        Commands::Run(_) | Commands::Shell(_) | Commands::Wrap(_) | Commands::Completions(_)
    )
}

pub(crate) fn run_detached_launch(args: RunArgs, silent: bool) -> Result<()> {
    let cwd_prompt_response = resolve_detached_cwd_prompt_response(&args.sandbox, silent)?;
    let session_id = session::generate_session_id();
    let exe = std::env::current_exe().map_err(|e| {
        NonoError::SandboxInit(format!("Failed to resolve current executable: {e}"))
    })?;
    let (startup_log_path, startup_log_stdio) = create_detached_startup_log(&session_id)?;
    let mut child = Command::new(exe);
    child.args(std::env::args_os().skip(1));
    child.env(DETACHED_LAUNCH_ENV, "1");
    child.env(DETACHED_SESSION_ID_ENV, &session_id);
    if let Some(response) = cwd_prompt_response {
        child.env(DETACHED_CWD_PROMPT_RESPONSE_ENV, response.as_env_value());
    }
    child.stdin(Stdio::null());
    child.stdout(Stdio::null());
    child.stderr(startup_log_stdio);

    #[cfg(unix)]
    unsafe {
        child.pre_exec(|| {
            if libc::setsid() < 0 {
                return Err(std::io::Error::last_os_error());
            }
            Ok(())
        });
    }

    let mut launched = child
        .spawn()
        .map_err(|e| NonoError::SandboxInit(format!("Failed to launch detached session: {e}")))?;

    let session_path = session::session_file_path(&session_id)?;
    let attach_path = session::session_socket_path(&session_id)?;
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
    while std::time::Instant::now() < deadline {
        if session_path.exists() && attach_path.exists() {
            cleanup_startup_log(&startup_log_path);
            print_detached_launch_banner(&session_id, args.name.as_deref(), silent);
            return Ok(());
        }

        if let Some(status) = launched.try_wait().map_err(|e| {
            NonoError::SandboxInit(format!("Failed to monitor detached launch: {e}"))
        })? {
            let detail = read_startup_log_summary(&startup_log_path);
            cleanup_startup_log(&startup_log_path);
            return Err(NonoError::SandboxInit(format!(
                "Detached session failed to start (exit status: {}){}",
                status,
                detail
                    .map(|summary| format!(": {summary}"))
                    .unwrap_or_default()
            )));
        }

        std::thread::sleep(std::time::Duration::from_millis(50));
    }

    terminate_detached_launch(&mut launched);
    cleanup_startup_log(&startup_log_path);
    Err(NonoError::SandboxInit(
        "Detached session failed to become attachable within startup timeout".to_string(),
    ))
}

pub(crate) fn show_update_notification(
    handle: &mut Option<update_check::UpdateCheckHandle>,
    silent: bool,
) {
    if let Some(handle) = handle.take() {
        if let Some(info) = handle.take_result() {
            output::print_update_notification(&info, silent);
        }
    }
}

fn print_detached_launch_banner(session_id: &str, session_name: Option<&str>, silent: bool) {
    if silent {
        return;
    }

    eprintln!("Started detached session {}.", session_id);
    if let Some(name) = session_name {
        eprintln!("Name: {name}");
    }
    eprintln!("Attach with: nono attach {}", session_id);
}

fn create_detached_startup_log(session_id: &str) -> Result<(PathBuf, Stdio)> {
    let prefix = format!(".nono-detached-startup-{session_id}-");
    let mut builder = tempfile::Builder::new();
    builder.prefix(&prefix).suffix(".log");

    let file = builder.tempfile_in(std::env::temp_dir()).map_err(|e| {
        NonoError::SandboxInit(format!("Failed to create detached startup log file: {e}"))
    })?;

    let (file, path) = file.keep().map_err(|e| {
        NonoError::SandboxInit(format!("Failed to persist detached startup log: {e}"))
    })?;

    Ok((path, Stdio::from(file)))
}

fn read_startup_log_summary(path: &Path) -> Option<String> {
    let contents = std::fs::read_to_string(path).ok()?;
    summarize_startup_log_contents(&contents)
}

fn summarize_startup_log_contents(contents: &str) -> Option<String> {
    let lines: Vec<String> = contents
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .filter_map(normalize_startup_log_line)
        .collect();

    let filtered: Vec<&str> = lines
        .iter()
        .map(String::as_str)
        .filter(|line| !is_startup_log_boilerplate(line))
        .collect();

    if let Some(headline) = filtered
        .iter()
        .copied()
        .find(|line| is_startup_headline(line))
    {
        return Some(headline.to_string());
    }

    let selected = if filtered.is_empty() {
        lines.iter().map(String::as_str).take(1).collect::<Vec<_>>()
    } else {
        filtered.into_iter().take(3).collect::<Vec<_>>()
    };

    if selected.is_empty() {
        None
    } else {
        Some(selected.join(" | "))
    }
}

fn is_startup_headline(line: &str) -> bool {
    line.contains("(exit code ")
        || line.starts_with("Command killed by signal")
        || line.starts_with("Permission denied")
        || line.starts_with("Failed to execute command")
        || line.starts_with("Command not found")
}

fn normalize_startup_log_line(line: &str) -> Option<String> {
    let trimmed = line.trim();
    if trimmed.is_empty() {
        return None;
    }

    let trimmed = trimmed.trim_start_matches("nono: ").trim();
    let trimmed = strip_startup_log_prefix(trimmed);
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.to_string())
    }
}

fn strip_startup_log_prefix(line: &str) -> &str {
    const PREFIXES: [&str; 2] = ["Applying sandbox...", "mode supervised (supervisor)"];

    let mut remaining = line;
    loop {
        let mut stripped = false;
        for prefix in PREFIXES {
            if let Some(rest) = remaining.strip_prefix(prefix) {
                remaining = rest.trim();
                stripped = true;
            }
        }

        if !stripped {
            return remaining;
        }
    }
}

fn is_startup_log_boilerplate(line: &str) -> bool {
    line.starts_with("nono v")
        || line == "Capabilities:"
        || line.starts_with('─')
        || line.starts_with("mode ")
        || line.starts_with("Applying sandbox...")
        || line.starts_with("Landlock V")
        || line.starts_with("kernel  ")
        || line == "NONO DIAGNOSTIC"
        || is_capability_summary_line(line)
        || line == "[nono]"
        || line == "[nono] Sandbox policy:"
        || line == "[nono]   Allowed paths:"
        || line.starts_with("[nono]     ")
        || line.starts_with("[nono]   Network:")
        || line.starts_with("[nono] To grant additional access")
        || line.starts_with("[nono]   --")
}

fn is_capability_summary_line(line: &str) -> bool {
    let trimmed = line.trim();

    if trimmed.starts_with("+ ") && trimmed.contains("system/group paths") {
        return true;
    }

    if trimmed == "outbound allowed"
        || trimmed == "outbound blocked"
        || trimmed.starts_with("proxy localhost:")
        || trimmed.starts_with("localhost:")
    {
        return true;
    }

    if let Some(rest) = trimmed.strip_prefix("net") {
        let rest = rest.trim();
        if rest == "outbound allowed"
            || rest == "outbound blocked"
            || rest.starts_with("proxy localhost:")
        {
            return true;
        }
    }

    if let Some(rest) = trimmed.strip_prefix("ipc") {
        let rest = rest.trim();
        if rest.starts_with("localhost:") {
            return true;
        }
    }

    let access_prefix = ["r+w", "r", "w"];
    access_prefix.iter().any(|prefix| {
        trimmed.starts_with(prefix)
            && (trimmed.contains(" (dir)") || trimmed.contains(" (file)"))
            && trimmed.contains('/')
    })
}

fn cleanup_startup_log(path: &Path) {
    let _ = std::fs::remove_file(path);
}

fn terminate_detached_launch(child: &mut Child) {
    #[cfg(unix)]
    {
        let pid = child.id() as i32;
        if pid > 0 {
            unsafe {
                libc::kill(-pid, libc::SIGTERM);
            }
        }
        for _ in 0..10 {
            if child.try_wait().ok().flatten().is_some() {
                return;
            }
            std::thread::sleep(std::time::Duration::from_millis(25));
        }
        if pid > 0 {
            unsafe {
                libc::kill(-pid, libc::SIGKILL);
            }
        }
    }

    let _ = child.kill();
    let _ = child.wait();
}

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

    #[test]
    fn startup_log_summary_prefers_exit_headline_over_footer_hints() {
        let contents = r#"
nono v0.22.1
Capabilities:
[nono] Failed to execute command (exit code 127).
[nono]
[nono] Sandbox policy:
[nono]   Allowed paths:
[nono]     /tmp (read+write, dir)
[nono]   Network: allowed
[nono]
[nono] To grant additional access, re-run with:
[nono]   --allow <path>     read+write access to directory
[nono]   --allow-net        unrestricted network for this session
"#;

        assert_eq!(
            summarize_startup_log_contents(contents).as_deref(),
            Some("[nono] Failed to execute command (exit code 127).")
        );
    }

    #[test]
    fn startup_log_summary_preserves_real_error_lines() {
        let contents = r#"
2026-03-23T18:08:00.249207Z ERROR Sandbox initialization failed: Profile inheritance error: circular dependency detected: claude-code -> claude-code
"#;

        assert_eq!(
            summarize_startup_log_contents(contents).as_deref(),
            Some("2026-03-23T18:08:00.249207Z ERROR Sandbox initialization failed: Profile inheritance error: circular dependency detected: claude-code -> claude-code")
        );
    }

    #[test]
    fn startup_log_summary_extracts_error_after_applying_sandbox_prefix() {
        let contents = r#"
nono v0.22.1
mode supervised (supervisor)
Applying sandbox...[nono] Command not found (exit code 127).
[nono]
[nono] Sandbox policy:
[nono]   Allowed paths:
[nono]     /tmp (read+write, dir)
[nono]   Network: allowed
[nono]
[nono] To grant additional access, re-run with:
[nono]   --allow <path>     read+write access to directory
[nono]   --allow-net        unrestricted network for this session
"#;

        assert_eq!(
            summarize_startup_log_contents(contents).as_deref(),
            Some("[nono] Command not found (exit code 127).")
        );
    }

    #[test]
    fn startup_log_summary_skips_capability_rows_in_detached_failures() {
        let contents = r#"
nono v0.25.0
Capabilities:
r+w  /home/luke/.opencode (dir)
r+w  /home/luke/.config/opencode (dir)
r+w  /home/luke/.cache/opencode (dir)
outbound allowed
mode supervised (supervisor)
Applying sandbox...[nono] Failed to execute command (exit code 127).
"#;

        assert_eq!(
            summarize_startup_log_contents(contents).as_deref(),
            Some("[nono] Failed to execute command (exit code 127).")
        );
    }

    #[test]
    fn startup_log_summary_skips_network_badge_rows_in_detached_failures() {
        let contents = r#"
nono v0.25.0
Capabilities:
net  outbound allowed
mode supervised (supervisor)
Applying sandbox...NONO DIAGNOSTIC
Failed to execute command (exit code 127).
"#;

        assert_eq!(
            summarize_startup_log_contents(contents).as_deref(),
            Some("Failed to execute command (exit code 127).")
        );
    }

    #[test]
    fn startup_log_summary_collapses_exec_failure_path_details() {
        let contents = r#"
nono v0.25.0
Capabilities:
net  outbound allowed
mode supervised (supervisor)
Applying sandbox...NONO DIAGNOSTIC
Failed to execute command (exit code 127).
The executable '/home/linuxbrew/.linuxbrew/bin/opencode' was resolved at:
/home/linuxbrew/.linuxbrew/bin/opencode
"#;

        assert_eq!(
            summarize_startup_log_contents(contents).as_deref(),
            Some("Failed to execute command (exit code 127).")
        );
    }
}