mahbot 0.10.2

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Start at login: the product's own registration with the operating system.
//!
//! The running app is the only writer of the system's startup state — the install scripts and
//! the updater never touch it. Each platform keeps one fixed identity, so a start after an
//! install, an update or a move re-points the same entry rather than adding a second one. The
//! owner's decision is `crate::config`'s (`autolaunch`), and it outlives installs. A disable
//! made in the system's own settings is real state here: it reads as "not registered", and
//! nothing in the product overrides it on its own.

use std::path::Path;

use anyhow::Context as _;

/// Keep the system's registration true to the owner's decision and to the running binary.
/// Called at every start; a no-op for a copy built from sources.
pub fn reconcile() {
    if !crate::self_update::is_published_copy() {
        return;
    }
    let outcome = if decision() {
        std::env::current_exe()
            .map_err(anyhow::Error::from)
            .and_then(|exe| platform::ensure(&exe))
    } else {
        platform::remove()
    };
    if let Err(e) = outcome {
        tracing::warn!(error = %e, "the login registration could not be kept true");
    }
}

/// Whether the system reports a registration that will start the product at login — an entry
/// the owner disabled reads as `false`, and a probe that could not run as `None`.
pub(crate) fn registered() -> Option<bool> {
    platform::registered()
}

/// Apply the owner's choice: remove everything, or (re)write the registration and clear a
/// system-side disable. Returns what the system reports afterwards.
pub(crate) fn set_enabled(enabled: bool) -> anyhow::Result<Option<bool>> {
    if enabled {
        let exe = std::env::current_exe().context("the running program's path is unknown")?;
        platform::enable(&exe)?;
    } else {
        platform::remove()?;
    }
    Ok(platform::registered())
}

/// The owner's decision: absence of the row means on.
fn decision() -> bool {
    crate::config::flag_on(crate::config::CONFIG.autolaunch().as_deref())
}

/// The one identity the registration has on macOS: the launchd label, and the name of the
/// file that carries it.
#[cfg_attr(not(any(test, target_os = "macos")), expect(dead_code))]
const LABEL: &str = "com.mahbot.autostart";

/// `text` with the characters XML gives a meaning to escaped.
#[cfg_attr(not(any(test, target_os = "macos")), expect(dead_code))]
fn xml_escaped(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&apos;"),
            _ => out.push(ch),
        }
    }
    out
}

/// The launchd agent that starts `exe` at login.
#[cfg_attr(not(any(test, target_os = "macos")), expect(dead_code))]
fn plist_text(exe: &Path) -> String {
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>{label}</string>
	<key>ProgramArguments</key>
	<array>
		<string>{program}</string>
		<string>{login}</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>
"#,
        label = LABEL,
        program = xml_escaped(&exe.to_string_lossy()),
        login = crate::cli::LOGIN_START_ARG,
    )
}

/// Whether a `launchctl print-disabled` dump carries a disable for `label`: a line of the form
/// `"<label>" => disabled` is a disable; `=> enabled` or no line is not.
#[cfg_attr(not(any(test, target_os = "macos")), expect(dead_code))]
fn launchctl_override_disabled(dump: &str, label: &str) -> bool {
    let quoted = format!("\"{label}\"");
    dump.lines().any(|line| {
        let Some((name, state)) = line.trim().split_once("=>") else {
            return false;
        };
        name.trim() == quoted && state.trim() == "disabled"
    })
}

/// The XDG autostart entry that starts `exe` at login. A disabled entry carries both keys a
/// desktop honours: `Hidden` (desktop-agnostic) and `X-GNOME-Autostart-enabled`.
#[cfg_attr(not(any(test, all(unix, not(target_os = "macos")))), expect(dead_code))]
fn desktop_entry_text(exe: &Path, enabled: bool) -> String {
    let mut text = format!(
        "[Desktop Entry]\nType=Application\nName=MahBot\nComment=Start MahBot at login\nExec={program} {login}\nTerminal=false\nX-GNOME-Autostart-enabled={state}\n",
        program = crate::util::desktop_entry::exec_argument(exe),
        login = crate::cli::LOGIN_START_ARG,
        state = if enabled { "true" } else { "false" },
    );
    if !enabled {
        text.push_str("Hidden=true\n");
    }
    text
}

/// Whether a desktop entry carries the owner's system-side disable. The entry is a key file, so
/// the spelling is free: key and value are trimmed, and the values read case-insensitively.
#[cfg_attr(not(any(test, all(unix, not(target_os = "macos")))), expect(dead_code))]
fn desktop_disabled(text: &str) -> bool {
    text.lines().any(|line| {
        let Some((key, value)) = line.split_once('=') else {
            return false;
        };
        let (key, value) = (key.trim(), value.trim());
        match key {
            "Hidden" => value.eq_ignore_ascii_case("true") || value == "1",
            "X-GNOME-Autostart-enabled" => value.eq_ignore_ascii_case("false") || value == "0",
            _ => false,
        }
    })
}

/// The `Run` value's data: the program path quoted, because profile names and
/// `%LOCALAPPDATA%` can contain spaces, followed by the login word.
#[cfg_attr(not(any(test, windows)), expect(dead_code))]
fn windows_command(exe: &Path) -> String {
    format!(
        "\"{}\" {}",
        exe.to_string_lossy(),
        crate::cli::LOGIN_START_ARG
    )
}

/// Whether the startup manager's record disables the entry: its first byte is odd when disabled
/// (`0x03`, `0x07`) and even when enabled (`0x02`, `0x06`). No value at all means enabled.
#[cfg_attr(not(any(test, windows)), expect(dead_code))]
fn startup_approved_disabled(record: &[u8]) -> bool {
    record.first().is_some_and(|flag| flag & 1 == 1)
}

/// The startup manager's record for an entry the owner has not disabled: the "enabled" flag
/// followed by a zeroed `FILETIME`. The system stamps a time only when it disables an entry,
/// and the product never disables one — it removes it instead.
#[cfg_attr(not(any(test, windows)), expect(dead_code))]
const STARTUP_APPROVED_ENABLED: [u8; 12] = [0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

/// Write `text` to `path` through a temp file in the same directory and a rename, so a login
/// never reads half a file. The mode is set explicitly: launchd refuses an agent its group and
/// the world can write to.
#[cfg(unix)]
fn write_atomic(path: &Path, text: &str) -> anyhow::Result<()> {
    use std::os::unix::fs::PermissionsExt as _;

    let dir = path
        .parent()
        .context("the registration's directory is unknown")?;
    std::fs::create_dir_all(dir)
        .with_context(|| format!("{} could not be created", dir.display()))?;
    let tmp = dir.join(format!(".mahbot-{}.tmp", std::process::id()));
    let outcome = (|| {
        std::fs::write(&tmp, text)
            .with_context(|| format!("{} could not be written", tmp.display()))?;
        std::fs::set_permissions(&tmp, std::fs::Permissions::from_mode(0o644))
            .with_context(|| format!("{} could not be given its mode", tmp.display()))?;
        std::fs::rename(&tmp, path)
            .with_context(|| format!("{} could not be put in place", path.display()))
    })();
    if outcome.is_err() {
        // Nothing sweeps this directory, so a failed write must not leave the temp file behind.
        let _ = std::fs::remove_file(&tmp);
    }
    outcome
}

/// Remove `path`, with an entry that is not there counting as removed already.
#[cfg(unix)]
fn remove_file_if_present(path: &Path) -> anyhow::Result<()> {
    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(e) => Err(e).with_context(|| format!("{} could not be removed", path.display())),
    }
}

#[cfg(target_os = "macos")]
mod platform {
    use std::path::{Path, PathBuf};

    use anyhow::Context as _;

    use super::{LABEL, launchctl_override_disabled, plist_text, remove_file_if_present};

    /// The owner's own uid: the per-user launchd domain and its override database are keyed by it.
    fn uid() -> u32 {
        // SAFETY: `getuid` takes no arguments and has no preconditions.
        unsafe { libc::getuid() }
    }

    /// The agent's file under `~/Library/LaunchAgents`.
    fn plist_path() -> anyhow::Result<PathBuf> {
        let home = directories::UserDirs::new()
            .context("the home directory is unknown")?
            .home_dir()
            .to_path_buf();
        Ok(home
            .join("Library")
            .join("LaunchAgents")
            .join(format!("{LABEL}.plist")))
    }

    pub(super) fn registered() -> Option<bool> {
        let path = plist_path().ok()?;
        match std::fs::metadata(&path) {
            Ok(metadata) if metadata.is_file() => {
                // An entry whose disable cannot be read is not known to start anything.
                Some(!disabled_by_system()?)
            }
            // Nothing launchd can load there.
            Ok(_) => Some(false),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Some(false),
            // A registration that cannot be inspected is not known to start anything.
            Err(_) => None,
        }
    }

    /// Write the agent unless it is already exactly right. The disable lives in launchd's
    /// override database rather than in the file, so a rewrite keeps it.
    pub(super) fn ensure(exe: &Path) -> anyhow::Result<()> {
        let path = plist_path()?;
        let text = plist_text(exe);
        if std::fs::read_to_string(&path).is_ok_and(|current| current == text) {
            return Ok(());
        }
        super::write_atomic(&path, &text)
    }

    pub(super) fn enable(exe: &Path) -> anyhow::Result<()> {
        let path = plist_path()?;
        super::write_atomic(&path, &plist_text(exe))?;
        clear_system_disable();
        // The switch reports what the system holds, so the clear is judged by the database
        // rather than by the exit status. An unreadable database is not a refusal to allow the
        // login item.
        anyhow::ensure!(
            disabled_by_system() != Some(true),
            "macOS still has the login item disabled — allow MahBot in System Settings › General › Login Items"
        );
        Ok(())
    }

    pub(super) fn remove() -> anyhow::Result<()> {
        remove_file_if_present(&plist_path()?)
    }

    /// Whether launchd's override database disables `LABEL` — the state System Settings'
    /// "Allow in the Background" switch writes. The database is per-user, so it is the same
    /// list whichever domain is named. `None` when it cannot be read, which is not "not disabled".
    fn disabled_by_system() -> Option<bool> {
        let output = std::process::Command::new("/bin/launchctl")
            .args(["print-disabled", &format!("gui/{}", uid())])
            .output()
            .ok()?;
        output
            .status
            .success()
            .then(|| launchctl_override_disabled(&String::from_utf8_lossy(&output.stdout), LABEL))
    }

    /// Clear the database's disable. Best effort: the caller judges by what the database then
    /// holds.
    fn clear_system_disable() {
        let result = std::process::Command::new("/bin/launchctl")
            .args(["enable", &format!("gui/{}/{LABEL}", uid())])
            .status();
        match result {
            Ok(status) if status.success() => {}
            Ok(status) => tracing::debug!(%status, "launchctl could not clear the login disable"),
            Err(e) => {
                tracing::debug!(error = %e, "launchctl could not run to clear the login disable");
            }
        }
    }
}

#[cfg(windows)]
mod platform {
    use std::path::Path;

    use windows_sys::Win32::Foundation::{ERROR_FILE_NOT_FOUND, ERROR_SUCCESS};
    use windows_sys::Win32::System::Registry::{
        HKEY, HKEY_CURRENT_USER, KEY_CREATE_SUB_KEY, KEY_QUERY_VALUE, KEY_SET_VALUE, REG_BINARY,
        REG_OPTION_NON_VOLATILE, REG_SAM_FLAGS, REG_SZ, REG_VALUE_TYPE, RegCloseKey,
        RegCreateKeyExW, RegDeleteValueW, RegOpenKeyExW, RegQueryValueExW, RegSetValueExW,
    };

    use super::{STARTUP_APPROVED_ENABLED, startup_approved_disabled, windows_command};
    use crate::util::registry::{os_reason, utf16_bytes, utf16_text, wide};

    /// The per-user key the system starts a program from at login.
    const RUN_KEY: &str = r"Software\Microsoft\Windows\CurrentVersion\Run";
    /// The record the system's own startup manager reads and writes; the owner's disable lives
    /// here.
    const APPROVED_KEY: &str =
        r"Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run";
    /// The one value name the product uses, on both keys.
    const VALUE: &str = "MahBot";

    pub(super) fn registered() -> Option<bool> {
        match read(RUN_KEY, VALUE) {
            Ok(None) => Some(false),
            // A registration whose disable cannot be read is not known to start anything.
            Ok(Some(_)) => disabled_by_system().ok().map(|disabled| !disabled),
            Err(_) => None,
        }
    }

    /// Point the `Run` value at `exe` when it does not already say so. The startup-manager
    /// record is not touched: that is where the owner's own disable lives.
    pub(super) fn ensure(exe: &Path) -> anyhow::Result<()> {
        let want = windows_command(exe);
        if read_string(RUN_KEY, VALUE)?.as_deref() != Some(want.as_str()) {
            write(RUN_KEY, VALUE, REG_SZ, &utf16_bytes(&want))?;
        }
        Ok(())
    }

    /// Write the `Run` value and an enabled startup-manager record — re-adding the value alone
    /// does not clear a disable made in Task Manager.
    pub(super) fn enable(exe: &Path) -> anyhow::Result<()> {
        write(RUN_KEY, VALUE, REG_SZ, &utf16_bytes(&windows_command(exe)))?;
        write(APPROVED_KEY, VALUE, REG_BINARY, &STARTUP_APPROVED_ENABLED)
    }

    /// Remove the `Run` value and the startup-manager record, so a later re-registration is not
    /// silently disabled. A value that is not there is not an error.
    pub(super) fn remove() -> anyhow::Result<()> {
        delete(RUN_KEY, VALUE)?;
        delete(APPROVED_KEY, VALUE)
    }

    fn disabled_by_system() -> anyhow::Result<bool> {
        Ok(read(APPROVED_KEY, VALUE)?.is_some_and(|record| startup_approved_disabled(&record)))
    }

    /// A value's raw bytes, or `None` when the key or the value is not there.
    fn read(key_path: &str, name: &str) -> anyhow::Result<Option<Vec<u8>>> {
        let Some(key) = open(key_path, KEY_QUERY_VALUE)? else {
            return Ok(None);
        };
        let outcome = query(key, name);
        // SAFETY: `key` came from `open` and nothing uses it after this.
        unsafe { RegCloseKey(key) };
        outcome
    }

    fn read_string(key_path: &str, name: &str) -> anyhow::Result<Option<String>> {
        Ok(read(key_path, name)?.map(|data| utf16_text(&data)))
    }

    fn query(key: HKEY, name: &str) -> anyhow::Result<Option<Vec<u8>>> {
        let name = wide(name);
        let mut kind: REG_VALUE_TYPE = 0;
        let mut size: u32 = 0;
        // SAFETY: a size query — no data pointer — as the API documents, with
        // `name` outliving the call and the two out-parameters ours.
        let status = unsafe {
            RegQueryValueExW(
                key,
                name.as_ptr(),
                std::ptr::null(),
                &raw mut kind,
                std::ptr::null_mut(),
                &raw mut size,
            )
        };
        if status == ERROR_FILE_NOT_FOUND {
            return Ok(None);
        }
        if status != ERROR_SUCCESS {
            anyhow::bail!(os_reason(
                "a login registration value could not be read",
                status
            ));
        }
        let mut data = vec![0u8; size as usize];
        let mut read = size;
        // SAFETY: `data` holds `size` bytes and `read` tells the API how many of
        // them it may fill; `name` outlives the call.
        let status = unsafe {
            RegQueryValueExW(
                key,
                name.as_ptr(),
                std::ptr::null(),
                &raw mut kind,
                data.as_mut_ptr(),
                &raw mut read,
            )
        };
        if status != ERROR_SUCCESS {
            anyhow::bail!(os_reason(
                "a login registration value could not be read",
                status
            ));
        }
        data.truncate(read as usize);
        Ok(Some(data))
    }

    fn write(key_path: &str, name: &str, kind: REG_VALUE_TYPE, data: &[u8]) -> anyhow::Result<()> {
        let key = create(key_path)?;
        let outcome = store(key, name, kind, data);
        // SAFETY: `key` came from `create` and nothing uses it after this.
        unsafe { RegCloseKey(key) };
        outcome
    }

    fn open(key_path: &str, access: REG_SAM_FLAGS) -> anyhow::Result<Option<HKEY>> {
        let path = wide(key_path);
        let mut key: HKEY = 0;
        // SAFETY: `path` is a NUL-terminated UTF-16 string that outlives the call,
        // and `key` is ours to fill.
        let status =
            unsafe { RegOpenKeyExW(HKEY_CURRENT_USER, path.as_ptr(), 0, access, &raw mut key) };
        match status {
            ERROR_SUCCESS => Ok(Some(key)),
            ERROR_FILE_NOT_FOUND => Ok(None),
            _ => anyhow::bail!(os_reason(
                "the login registration's key could not be opened",
                status
            )),
        }
    }

    fn create(key_path: &str) -> anyhow::Result<HKEY> {
        let path = wide(key_path);
        let mut key: HKEY = 0;
        let mut disposition = 0;
        // SAFETY: `path` is a NUL-terminated UTF-16 string that outlives the call,
        // `key` and `disposition` are ours to fill, and the default security
        // attribute is the process's own.
        let status = unsafe {
            RegCreateKeyExW(
                HKEY_CURRENT_USER,
                path.as_ptr(),
                0,
                std::ptr::null(),
                REG_OPTION_NON_VOLATILE,
                KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY,
                std::ptr::null(),
                &raw mut key,
                &raw mut disposition,
            )
        };
        if status != ERROR_SUCCESS {
            anyhow::bail!(os_reason(
                "the login registration's key could not be opened",
                status
            ));
        }
        Ok(key)
    }

    fn store(key: HKEY, name: &str, kind: REG_VALUE_TYPE, data: &[u8]) -> anyhow::Result<()> {
        let name = wide(name);
        let Ok(size) = u32::try_from(data.len()) else {
            anyhow::bail!("a login registration value is too long to store");
        };
        // SAFETY: `name` and `data` are the value name and data the API reads, and
        // both outlive the call.
        let status = unsafe { RegSetValueExW(key, name.as_ptr(), 0, kind, data.as_ptr(), size) };
        if status != ERROR_SUCCESS {
            anyhow::bail!(os_reason(
                "a login registration value could not be stored",
                status
            ));
        }
        Ok(())
    }

    fn delete(key_path: &str, name: &str) -> anyhow::Result<()> {
        let Some(key) = open(key_path, KEY_SET_VALUE)? else {
            return Ok(());
        };
        let name = wide(name);
        // SAFETY: `name` is a NUL-terminated UTF-16 value name that outlives the
        // call and `key` is open for writing.
        let status = unsafe { RegDeleteValueW(key, name.as_ptr()) };
        // SAFETY: `key` came from `open` and nothing uses it after this.
        unsafe { RegCloseKey(key) };
        match status {
            ERROR_SUCCESS | ERROR_FILE_NOT_FOUND => Ok(()),
            _ => {
                anyhow::bail!(os_reason(
                    "a login registration value could not be removed",
                    status
                ))
            }
        }
    }
}

#[cfg(all(unix, not(target_os = "macos")))]
mod platform {
    use std::path::{Path, PathBuf};

    use anyhow::Context as _;

    use super::{desktop_disabled, desktop_entry_text, remove_file_if_present};

    /// `~/.config/autostart/mahbot.desktop`.
    fn entry_path() -> anyhow::Result<PathBuf> {
        let config = directories::BaseDirs::new()
            .context("the config directory is unknown")?
            .config_dir()
            .to_path_buf();
        Ok(config.join("autostart").join("mahbot.desktop"))
    }

    pub(super) fn registered() -> Option<bool> {
        let path = entry_path().ok()?;
        match std::fs::read_to_string(&path) {
            Ok(text) => Some(!desktop_disabled(&text)),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Some(false),
            // An entry that cannot be read is not known to start anything.
            Err(_) => None,
        }
    }

    /// Point the entry at `exe`, preserving a disable the owner made in the system's own settings.
    pub(super) fn ensure(exe: &Path) -> anyhow::Result<()> {
        let path = entry_path()?;
        let current = std::fs::read_to_string(&path).ok();
        let enabled = current
            .as_deref()
            .is_none_or(|text| !desktop_disabled(text));
        let text = desktop_entry_text(exe, enabled);
        if current.as_deref() == Some(text.as_str()) {
            return Ok(());
        }
        super::write_atomic(&path, &text)
    }

    pub(super) fn enable(exe: &Path) -> anyhow::Result<()> {
        let path = entry_path()?;
        super::write_atomic(&path, &desktop_entry_text(exe, true))
    }

    pub(super) fn remove() -> anyhow::Result<()> {
        remove_file_if_present(&entry_path()?)
    }
}

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

    /// The two files that register the product with the system name the program and the login
    /// word, and nothing a parser would misread: the escape each format needs is applied to a
    /// path that needs every one of them.
    #[test]
    fn the_platform_files_name_the_program_and_the_login_word() {
        let plist = plist_text(Path::new("/Applications/A & B/<mahbot>"));
        assert!(plist.contains(&format!("<string>{LABEL}</string>")));
        assert!(plist.contains("<string>/Applications/A &amp; B/&lt;mahbot&gt;</string>"));
        assert!(plist.contains(&format!("<string>{}</string>", crate::cli::LOGIN_START_ARG)));

        let entry = desktop_entry_text(Path::new("/home/o/.local/bin/mahbot"), true);
        assert!(entry.contains("Exec=\"/home/o/.local/bin/mahbot\" __login-start"));
        assert!(entry.contains("X-GNOME-Autostart-enabled=true"));
        assert!(!desktop_disabled(&entry));

        // A literal `%` in the path is a field code unless it is doubled.
        assert!(
            desktop_entry_text(Path::new("/opt/100%/mahbot"), true)
                .contains("Exec=\"/opt/100%%/mahbot\"")
        );

        // The Windows `Run` value's data is the program path quoted — a profile name and
        // `%LOCALAPPDATA%` can contain spaces — and the login word after it.
        assert_eq!(
            windows_command(Path::new(r"C:\Program Files\MahBot\mahbot.exe")),
            r#""C:\Program Files\MahBot\mahbot.exe" __login-start"#
        );
    }

    /// A registration the owner disabled in the system's own settings — the desktop entry's
    /// disable keys, launchd's override dump, and the startup-approved record — reads as
    /// disabled, and a plain enable, a comment or no record does not. Each platform's own
    /// spelling is what the product must read back.
    #[test]
    fn a_startup_entry_the_owner_disabled_reads_as_disabled() {
        let disabled_entry = desktop_entry_text(Path::new("/opt/mahbot"), false);
        // The desktop-agnostic disable key, not only GNOME's.
        assert!(disabled_entry.contains("Hidden=true"));
        assert!(desktop_disabled(&disabled_entry));
        assert!(desktop_disabled("[Desktop Entry]\nHidden=true\n"));
        assert!(desktop_disabled("X-GNOME-Autostart-enabled=false\n"));
        // A key file's spelling is free: case, spacing, and the numeric forms.
        assert!(desktop_disabled("Hidden=True\n"));
        assert!(desktop_disabled("Hidden = 1\n"));
        assert!(desktop_disabled("X-GNOME-Autostart-enabled=0\n"));
        // A comment, or an explicit enable, is not a disable.
        assert!(!desktop_disabled("#Hidden=true\n"));
        assert!(!desktop_disabled("X-GNOME-Autostart-enabled=true\n"));

        let dump = "\
disabled services = {
\t\"com.mahbot.autostart\" => disabled
\t\"com.apple.other\" => enabled
}
";
        assert!(launchctl_override_disabled(dump, "com.mahbot.autostart"));
        assert!(!launchctl_override_disabled(dump, "com.apple.other"));
        assert!(!launchctl_override_disabled(dump, "com.mahbot.absent"));

        // The record this product writes is the enabled one; the flag the system writes on a
        // disable, and the one it stamps on a later change, both read as disabled. No record
        // means enabled.
        assert!(!startup_approved_disabled(&STARTUP_APPROVED_ENABLED));
        assert!(startup_approved_disabled(&[0x03]));
        assert!(startup_approved_disabled(&[0x07]));
        assert!(!startup_approved_disabled(&[]));
    }

    /// A failed rename must not leave the temp file behind in a directory nothing sweeps.
    #[cfg(unix)]
    #[test]
    fn a_failed_write_leaves_no_temp_file() {
        use std::os::unix::fs::PermissionsExt as _;

        let dir = std::env::temp_dir().join(format!("mahbot-autostart-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);

        let path = dir.join("entry");
        write_atomic(&path, "hello").unwrap();
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "hello");
        let mode = std::fs::metadata(&path).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o644);

        // A target that is a directory makes the rename fail.
        let blocked = dir.join("blocked");
        std::fs::create_dir(&blocked).unwrap();
        assert!(write_atomic(&blocked, "hello").is_err());
        assert!(
            !dir.join(format!(".mahbot-{}.tmp", std::process::id()))
                .exists()
        );

        std::fs::remove_dir_all(&dir).unwrap();
    }
}