Skip to main content

supercode_harness/
teams.rs

1//! Where supercode-teams lives on this box, and the service unit that keeps
2//! its machine daemon up (`docs/plans/teams-server.md` §11).
3//!
4//! supercode does not implement teams; the `sdk/teams` package does. This
5//! module holds the two facts the Rust CLI needs about it:
6//!
7//! * **where its Node entry is** — [`teams_entry`], resolved exactly the way
8//!   [`crate::orchestrator::daemon_entry`] resolves the orchestrator's:
9//!   `SUPERCODE_TEAMS_ENTRY` first, then the checkout the running binary sits
10//!   in, then the checkout it was built from, then the globally installed
11//!   `@volter/supercode-teams` package (`npm root -g`).
12//! * **what a service unit for its node would say** — [`service_unit`] renders
13//!   the launchd plist / systemd unit / Windows Scheduled Task that runs
14//!   `node <entry> machine start`, written under `<home>/service/`;
15//!   [`install_service`] and [`uninstall_service`] drive `launchctl` /
16//!   `systemctl --user` / `schtasks` over it.
17//!
18//! Everything else about teams — its host key, log, contexts, enrollments — is the Node
19//! package's own state, written by its own CLI. There is no second writer of
20//! that home in this binary.
21
22use std::path::{Path, PathBuf};
23
24use crate::orchestrator::{absolute_program, resolve_program, ServiceState, ServiceUnit};
25
26/// The teams CLI entry inside the `sdk/teams` package.
27pub const TEAMS_ENTRY: &str = "bin/teams.mjs";
28
29/// The npm name the `sdk/teams` package is published under.
30pub const TEAMS_PACKAGE: &str = "@volter/supercode-teams";
31
32/// Directory the rendered service unit is written into, relative to the home.
33pub const SERVICE_DIR: &str = "service";
34
35/// launchd label / systemd unit name for this machine's teams daemon.
36pub const SERVICE_NAME: &str = "dev.volter.supercode-teams-machine";
37
38/// Stable, context-scoped label for one workspace connector service.
39pub fn connector_service_name(server_id: &str, team_id: &str, context: &str) -> String {
40    // FNV-1a is sufficient here: this is a stable filesystem/service label,
41    // not an authorization decision or secret digest.
42    let mut hash = 0xcbf29ce484222325_u64;
43    for byte in [server_id, team_id, context].join("\0").bytes() {
44        hash ^= u64::from(byte);
45        hash = hash.wrapping_mul(0x100000001b3);
46    }
47    format!("dev.volter.supercode-teams-connector-{hash:016x}")
48}
49
50fn plist_text(value: &str) -> String {
51    value
52        .replace('&', "&amp;")
53        .replace('<', "&lt;")
54        .replace('>', "&gt;")
55}
56
57fn service_text(value: &str) -> Result<&str, TeamsError> {
58    if value.chars().any(char::is_control) {
59        return Err(TeamsError::Service {
60            action: "render",
61            detail: "service parameters cannot contain control characters".into(),
62        });
63    }
64    Ok(value)
65}
66
67fn systemd_arg(value: &str) -> String {
68    format!(
69        "\"{}\"",
70        value
71            .replace('\\', "\\\\")
72            .replace('"', "\\\"")
73            .replace('%', "%%")
74            .replace('$', "$$")
75    )
76}
77
78/// Render the persistent foreground connector command for one saved context.
79pub fn connector_service_unit(
80    teams_home: &Path,
81    supercode_home: &Path,
82    entry: &Path,
83    node: &str,
84    supercode: &Path,
85    context: &str,
86    cwd: &Path,
87    server_id: &str,
88    team_id: &str,
89) -> Result<ServiceUnit, TeamsError> {
90    let teams_home_text = teams_home.display().to_string();
91    let supercode_home_text = supercode_home.display().to_string();
92    let entry_text = entry.display().to_string();
93    let supercode_text = supercode.display().to_string();
94    let workspace_text = cwd.display().to_string();
95    for value in [
96        teams_home_text.as_str(),
97        supercode_home_text.as_str(),
98        entry_text.as_str(),
99        node,
100        supercode_text.as_str(),
101        context,
102        workspace_text.as_str(),
103        server_id,
104        team_id,
105    ] {
106        service_text(value)?;
107    }
108    let label = connector_service_name(server_id, team_id, context);
109    let path = teams_home
110        .join(SERVICE_DIR)
111        .join(format!("{label}.{}", connector_unit_suffix()));
112    let node = absolute_program(node);
113    let entry = entry_text;
114    let workspace = workspace_text;
115    let home = supercode_home_text;
116    let supercode = supercode_text;
117    if cfg!(windows) {
118        let log_path = teams_home.join(SERVICE_DIR).join(format!("{label}.log"));
119        return windows_task(
120            &path,
121            &label,
122            &format!("supercode Teams connector ({context})"),
123            &service_env_path(teams_home, &label),
124            &[
125                ("SUPERCODE_HOME", home.as_str()),
126                ("SUPERCODE_BIN", supercode.as_str()),
127                ("SUPERCODE_TEAMS_LOG", &log_path.display().to_string()),
128            ],
129            &node,
130            &[
131                entry.as_str(),
132                "teams",
133                "connect",
134                "--foreground",
135                "--context",
136                context,
137                "--cwd",
138                workspace.as_str(),
139            ],
140            &workspace,
141        );
142    }
143    if cfg!(target_os = "macos") {
144        let node = plist_text(&node);
145        let entry = plist_text(&entry);
146        let workspace = plist_text(&workspace);
147        let home = plist_text(&home);
148        let supercode = plist_text(&supercode);
149        let context = plist_text(context);
150        let search_path = plist_text(&service_path());
151        // `ProcessType Interactive`: without it launchd spawns the connector as a daemon-type job at background
152        // priority (20, against 31 for the user's own processes), and on a busy disk its throttled reads made a
153        // full session discovery of 4,300 sessions miss the 25 s bound (gemini alone 15 s against 2.7 s), so
154        // `discover --fleet` listed the machine unreachable. The connector answers people waiting on it.
155        let text = format!(
156            r#"<?xml version="1.0" encoding="UTF-8"?>
157<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
158<plist version="1.0"><dict>
159  <key>Label</key><string>{label}</string>
160  <key>ProgramArguments</key><array><string>{node}</string><string>{entry}</string><string>teams</string><string>connect</string><string>--context</string><string>{context}</string><string>--cwd</string><string>{workspace}</string></array>
161  <key>EnvironmentVariables</key><dict><key>SUPERCODE_HOME</key><string>{home}</string><key>SUPERCODE_BIN</key><string>{supercode}</string><key>PATH</key><string>{search_path}</string></dict>
162  <key>RunAtLoad</key><true/><key>KeepAlive</key><true/><key>ProcessType</key><string>Interactive</string>
163  <key>StandardOutPath</key><string>{}/service/{label}.out.log</string>
164  <key>StandardErrorPath</key><string>{}/service/{label}.err.log</string>
165</dict></plist>
166"#,
167            plist_text(&teams_home_text),
168            plist_text(&teams_home_text)
169        );
170        Ok(ServiceUnit {
171            kind: "launchd",
172            path: path.clone(),
173            text,
174            install_command: format!("launchctl bootstrap gui/$(id -u) {}", path.display()),
175            files: Vec::new(),
176        })
177    } else {
178        let environment_home = systemd_arg(&format!("SUPERCODE_HOME={home}"));
179        let environment_bin = systemd_arg(&format!("SUPERCODE_BIN={supercode}"));
180        let environment_path = systemd_arg(&format!("PATH={}", service_path()));
181        let node = systemd_arg(&node);
182        let entry = systemd_arg(&entry);
183        let workspace = systemd_arg(&workspace);
184        let context_description = context.replace('%', "%%").replace('$', "$$");
185        let context = systemd_arg(context);
186        // KillMode=process: the tmux server holding the machine's panes forks into this unit's cgroup, and a
187        // restart must end only the connector, never the panes.
188        let text = format!("[Unit]\nDescription=supercode Teams connector ({context_description})\nAfter=network.target\n\n[Service]\nEnvironment={environment_home}\nEnvironment={environment_bin}\nEnvironment={environment_path}\nExecStart={node} {entry} teams connect --context {context} --cwd {workspace}\nRestart=on-failure\nKillSignal=SIGTERM\nKillMode=process\n\n[Install]\nWantedBy=default.target\n");
189        Ok(ServiceUnit {
190            kind: "systemd",
191            path: path.clone(),
192            text,
193            install_command: format!(
194                "systemctl --user link {} && systemctl --user enable --now {label}",
195                path.display()
196            ),
197            files: Vec::new(),
198        })
199    }
200}
201
202/// The connector unit's file suffix on this platform.
203fn connector_unit_suffix() -> &'static str {
204    if cfg!(windows) {
205        "xml"
206    } else if cfg!(target_os = "macos") {
207        "plist"
208    } else {
209        "service"
210    }
211}
212
213/// The environment file a Windows service task hands to node (`--env-file`).
214fn service_env_path(teams_home: &Path, label: &str) -> PathBuf {
215    teams_home.join(SERVICE_DIR).join(format!("{label}.env"))
216}
217
218/// Render a per-user Scheduled Task that keeps `node --env-file=<env_path> <node_args…>` running, written to `path`
219/// with its environment file beside it. Shared by the connector and the machine daemon.
220#[allow(clippy::too_many_arguments)]
221fn windows_task(
222    path: &Path,
223    label: &str,
224    description: &str,
225    env_path: &Path,
226    env: &[(&str, &str)],
227    node: &str,
228    node_args: &[&str],
229    working_directory: &str,
230) -> Result<ServiceUnit, TeamsError> {
231    // A scheduled task sets no environment and keeps no output, so node reads both from a file beside the
232    // task (`--env-file`); the user's own PATH is the task's. `conhost --headless` runs it without a window,
233    // and hides node's exit code too, so restart-on-failure never sees one fail: a trigger every minute
234    // starts the service again instead, and while it runs the task's IgnoreNew makes that tick a no-op.
235    // The trigger's fixed past start keeps the rendered unit the same on every install.
236    let env_text = env
237        .iter()
238        .map(|(key, value)| env_file_value(value).map(|value| format!("{key}={value}\n")))
239        .collect::<Result<String, _>>()?;
240    let env_flag = format!("--env-file={}", env_path.display());
241    let mut arguments = vec![node, env_flag.as_str()];
242    arguments.extend_from_slice(node_args);
243    // Task Scheduler expands `%NAME%` in a task's arguments, and there is no escape for it.
244    for value in arguments.iter().chain([&working_directory]) {
245        if value.contains('%') {
246            return Err(TeamsError::Service {
247                action: "render",
248                detail: format!("a Windows task cannot carry a path containing `%`: {value}"),
249            });
250        }
251    }
252    let arguments = arguments
253        .iter()
254        .map(|argument| windows_arg(argument))
255        .collect::<Vec<_>>()
256        .join(" ");
257    let user = windows_user();
258    let conhost = Path::new(&std::env::var("SystemRoot").unwrap_or_else(|_| r"C:\Windows".into()))
259        .join(r"System32\conhost.exe")
260        .display()
261        .to_string();
262    // `<Priority>4</Priority>`: a task's default priority is 7, below normal for CPU and I/O. Not measured on
263    // Windows: the same throttling measured on macOS (see the launchd plist) made discovery miss its bound there.
264    let text = format!(
265        r#"<?xml version="1.0" encoding="UTF-16"?>
266<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
267  <RegistrationInfo><Description>{}</Description></RegistrationInfo>
268  <Triggers><LogonTrigger><Enabled>true</Enabled><UserId>{}</UserId></LogonTrigger><TimeTrigger><StartBoundary>2000-01-01T00:00:00</StartBoundary><Enabled>true</Enabled><Repetition><Interval>PT1M</Interval><StopAtDurationEnd>false</StopAtDurationEnd></Repetition></TimeTrigger></Triggers>
269  <Principals><Principal id="Author"><UserId>{}</UserId><LogonType>InteractiveToken</LogonType><RunLevel>LeastPrivilege</RunLevel></Principal></Principals>
270  <Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><ExecutionTimeLimit>PT0S</ExecutionTimeLimit><Priority>4</Priority><RestartOnFailure><Interval>PT1M</Interval><Count>999</Count></RestartOnFailure><StartWhenAvailable>true</StartWhenAvailable></Settings>
271  <Actions Context="Author"><Exec><Command>{}</Command><Arguments>--headless {}</Arguments><WorkingDirectory>{}</WorkingDirectory></Exec></Actions>
272</Task>
273"#,
274        xml_text(description),
275        xml_text(&user),
276        xml_text(&user),
277        xml_text(&conhost),
278        xml_text(&arguments),
279        xml_text(working_directory),
280    );
281    Ok(ServiceUnit {
282        kind: "schtasks",
283        path: path.to_path_buf(),
284        text,
285        install_command: format!("schtasks /Create /TN {label} /XML {} /F", path.display()),
286        files: vec![(env_path.to_path_buf(), env_text)],
287    })
288}
289
290/// Text inside a Task Scheduler XML element or attribute.
291fn xml_text(value: &str) -> String {
292    plist_text(value).replace('"', "&quot;")
293}
294
295/// One argument of a Windows command line, quoted so the C runtime (node.exe's) splits it back out whole:
296/// backslashes are literal except before a quote, where they and the quote are escaped.
297fn windows_arg(value: &str) -> String {
298    if !value.is_empty() && !value.contains([' ', '\t', '"']) {
299        return value.to_string();
300    }
301    let mut quoted = String::from("\"");
302    let mut backslashes = 0;
303    for character in value.chars() {
304        match character {
305            '\\' => backslashes += 1,
306            '"' => {
307                quoted.push_str(&"\\".repeat(backslashes * 2 + 1));
308                quoted.push('"');
309                backslashes = 0;
310            }
311            other => {
312                quoted.push_str(&"\\".repeat(backslashes));
313                quoted.push(other);
314                backslashes = 0;
315            }
316        }
317    }
318    quoted.push_str(&"\\".repeat(backslashes * 2));
319    quoted.push('"');
320    quoted
321}
322
323/// A value in a node `--env-file`, quoted with a quote character the value does not contain. Single and backtick
324/// quotes are literal; double quotes would turn a path's `\n` into a newline, so they are the last choice.
325fn env_file_value(value: &str) -> Result<String, TeamsError> {
326    ['\'', '`']
327        .into_iter()
328        .find(|quote| !value.contains(*quote))
329        .map(|quote| format!("{quote}{value}{quote}"))
330        .or_else(|| (!value.contains(['"', '\\'])).then(|| format!("\"{value}\"")))
331        .ok_or_else(|| TeamsError::Service {
332            action: "render",
333            detail: format!("cannot write `{value}` into a service's environment file"),
334        })
335}
336
337/// The Windows account a task runs as: `DOMAIN\user`, the one installing it.
338fn windows_user() -> String {
339    let user = std::env::var("USERNAME").unwrap_or_default();
340    match std::env::var("USERDOMAIN") {
341        Ok(domain) if !domain.is_empty() => format!("{domain}\\{user}"),
342        _ => user,
343    }
344}
345
346/// The binary an installed connector runs. On Windows a running `.exe` cannot be replaced, so a
347/// connector that ran the npm package's own binary made `npm install -g` fail with EBUSY for as long
348/// as it ran: there the service runs a copy kept per version under the teams service directory, and
349/// installing again after an upgrade moves it to the new copy. Copies of other versions that no
350/// process holds any more are removed. Everywhere else the binary itself is replaceable in place.
351pub fn connector_service_binary(
352    teams_home: &Path,
353    supercode: &Path,
354) -> Result<PathBuf, TeamsError> {
355    if !cfg!(windows) {
356        return Ok(supercode.to_path_buf());
357    }
358    let file = |path: &Path, source: std::io::Error| TeamsError::File {
359        path: path.to_path_buf(),
360        source,
361    };
362    let copies = teams_home.join(SERVICE_DIR).join("bin");
363    let version = env!("CARGO_PKG_VERSION");
364    let dir = copies.join(version);
365    let copy = dir.join(
366        supercode
367            .file_name()
368            .unwrap_or_else(|| "supercode.exe".as_ref()),
369    );
370    let size = |path: &Path| std::fs::metadata(path).map(|meta| meta.len()).ok();
371    if size(&copy).is_none() || size(&copy) != size(supercode) {
372        std::fs::create_dir_all(&dir).map_err(|source| file(&dir, source))?;
373        std::fs::copy(supercode, &copy).map_err(|source| file(&copy, source))?;
374    }
375    if let Ok(entries) = std::fs::read_dir(&copies) {
376        for entry in entries.flatten() {
377            if entry.file_name() != version {
378                let _ = std::fs::remove_dir_all(entry.path());
379            }
380        }
381    }
382    Ok(copy)
383}
384
385/// Why a teams verb could not do its work.
386#[derive(Debug, thiserror::Error)]
387pub enum TeamsError {
388    /// The Node teams entry could not be located.
389    #[error("no teams entry found (looked for `sdk/teams/{TEAMS_ENTRY}` under: {searched}); install it with `npm install -g {TEAMS_PACKAGE}`")]
390    NoEntry {
391        /// The candidate paths that were searched, joined.
392        searched: String,
393    },
394    /// A service manager refused, or there is none on this platform.
395    #[error("teams service: {action} failed: {detail}")]
396    Service {
397        /// What was attempted (`install`, `uninstall`).
398        action: &'static str,
399        /// What the service manager (or this module) said about it.
400        detail: String,
401    },
402    /// A file under the teams home could not be written or removed.
403    #[error("teams file `{}`: {source}", path.display())]
404    File {
405        /// The path involved.
406        path: PathBuf,
407        /// The underlying I/O failure.
408        source: std::io::Error,
409    },
410}
411
412/// The search path a service runs with: the installing shell's own, so a
413/// service finds the same `tmux`, `node` and harness CLIs its installer did
414/// (a launchd or systemd default path has none of them).
415fn service_path() -> String {
416    std::env::var("PATH")
417        .ok()
418        .filter(|path| !path.trim().is_empty())
419        .unwrap_or_else(|| "/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin".into())
420}
421
422/// The teams home: `SUPERCODE_TEAMS_HOME`, else `<SUPERCODE_HOME>/teams`.
423///
424/// The same precedence `sdk/volter-teams/home.mjs` uses, so a unit installed from
425/// here serves the home the Node CLI reads.
426pub fn teams_home() -> PathBuf {
427    if let Ok(home) = std::env::var("SUPERCODE_TEAMS_HOME") {
428        if !home.is_empty() {
429            return PathBuf::from(home);
430        }
431    }
432    crate::agent::global_instructions_dir().join("teams")
433}
434
435/// Locate the Node teams entry (`sdk/teams/bin/teams.mjs`).
436///
437/// Candidates, in order: `SUPERCODE_TEAMS_ENTRY` (an explicit override, which
438/// is also how a test points at a fake), the repo checkout the running binary
439/// sits in, the workspace this crate was built from,
440/// and the globally installed npm package — an installed binary has no
441/// checkout, so `npm install -g @volter/supercode-teams` is how a
442/// Machine gets its node. The current directory is never a candidate: the
443/// code a binary runs does not change with where it is run.
444pub fn teams_entry() -> Result<PathBuf, TeamsError> {
445    let mut searched = Vec::new();
446    if let Some(explicit) = std::env::var_os("SUPERCODE_TEAMS_ENTRY") {
447        let path = PathBuf::from(explicit);
448        if path.is_file() {
449            return Ok(path);
450        }
451        searched.push(path.display().to_string());
452    }
453    let mut roots: Vec<PathBuf> = Vec::new();
454    if let Ok(exe) = std::env::current_exe() {
455        // target/<profile>/supercode → the workspace root is two levels up.
456        roots.extend(exe.ancestors().skip(1).take(4).map(Path::to_path_buf));
457    }
458    // A locally built binary's target directory can live anywhere (a shared
459    // cargo build dir, another volume), so the checkout it was built from is
460    // the last candidate. On an installed binary this path simply does not
461    // exist and is skipped like any other miss.
462    if let Some(workspace) = Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2) {
463        roots.push(workspace.to_path_buf());
464    }
465    for root in roots {
466        let candidate = root.join("sdk/teams").join(TEAMS_ENTRY);
467        if candidate.is_file() {
468            return Ok(candidate);
469        }
470        searched.push(candidate.display().to_string());
471    }
472    // Installed from npm, this binary sits inside the global node_modules that
473    // also holds the Teams package, so that directory is found from the
474    // binary's own path first (`npm root -g` masks path segments it takes for
475    // secrets, a UUID among them).
476    if let Ok(exe) = std::env::current_exe() {
477        for modules in exe
478            .ancestors()
479            .filter(|dir| dir.file_name().is_some_and(|name| name == "node_modules"))
480        {
481            let candidate = modules.join(TEAMS_PACKAGE).join(TEAMS_ENTRY);
482            if candidate.is_file() {
483                return Ok(candidate);
484            }
485            searched.push(candidate.display().to_string());
486        }
487    }
488    if let Some(global) = global_npm_root() {
489        let candidate = global.join(TEAMS_PACKAGE).join(TEAMS_ENTRY);
490        if candidate.is_file() {
491            return Ok(candidate);
492        }
493        searched.push(candidate.display().to_string());
494    }
495    Err(TeamsError::NoEntry {
496        searched: searched.join(", "),
497    })
498}
499
500/// Where npm installs global packages (`npm root -g`), when npm is present.
501fn global_npm_root() -> Option<PathBuf> {
502    let output = std::process::Command::new(resolve_program("npm"))
503        .args(["root", "-g"])
504        .stdin(std::process::Stdio::null())
505        .stderr(std::process::Stdio::null())
506        .output()
507        .ok()?;
508    if !output.status.success() {
509        return None;
510    }
511    let text = String::from_utf8_lossy(&output.stdout);
512    let root = text.trim();
513    if root.is_empty() {
514        return None;
515    }
516    Some(PathBuf::from(root))
517}
518
519/// Render the per-platform service unit for this machine's teams daemon.
520///
521/// The node takes no `--root`: it serves the home its own environment
522/// resolves (`SUPERCODE_TEAMS_HOME`, else `<SUPERCODE_HOME>/teams`), so the
523/// unit names the listen address and nothing else. A port of `0` means the
524/// node picks one and publishes it in `<home>/node.json`.
525///
526/// On Windows it is a per-user Scheduled Task, rendered exactly as a connector's is (see [`windows_task`]).
527pub fn service_unit(home: &Path, entry: &Path, node: &str) -> Result<ServiceUnit, TeamsError> {
528    let home_display = home.display().to_string();
529    let entry_display = entry.display().to_string();
530    // A control character would break out of any unit's syntax (a plist string, a systemd line, task XML).
531    for value in [home_display.as_str(), entry_display.as_str(), node] {
532        service_text(value)?;
533    }
534    if cfg!(windows) {
535        let log_path = home.join(SERVICE_DIR).join(format!("{SERVICE_NAME}.log"));
536        return windows_task(
537            &home.join(SERVICE_DIR).join(unit_file_name()),
538            SERVICE_NAME,
539            &format!("supercode teams machine daemon ({home_display})"),
540            &service_env_path(home, SERVICE_NAME),
541            &[
542                ("SUPERCODE_TEAMS_HOME", home_display.as_str()),
543                ("SUPERCODE_TEAMS_LOG", &log_path.display().to_string()),
544            ],
545            node,
546            &[entry_display.as_str(), "machine", "start"],
547            &home_display,
548        );
549    }
550    if cfg!(target_os = "macos") {
551        let path = home.join(SERVICE_DIR).join(format!("{SERVICE_NAME}.plist"));
552        let text = format!(
553            r#"<?xml version="1.0" encoding="UTF-8"?>
554<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
555<plist version="1.0">
556<dict>
557  <key>Label</key><string>{SERVICE_NAME}</string>
558  <key>ProgramArguments</key>
559  <array>
560    <string>{node}</string>
561    <string>{entry_display}</string>
562    <string>machine</string>
563    <string>start</string>
564  </array>
565  <key>EnvironmentVariables</key>
566  <dict>
567    <key>SUPERCODE_TEAMS_HOME</key><string>{home_display}</string>
568  </dict>
569  <key>RunAtLoad</key><true/>
570  <key>KeepAlive</key><true/>
571  <key>ProcessType</key><string>Interactive</string>
572  <key>StandardOutPath</key><string>{home_display}/service/teams-machine.out.log</string>
573  <key>StandardErrorPath</key><string>{home_display}/service/teams-machine.err.log</string>
574</dict>
575</plist>
576"#
577        );
578        let install = format!("launchctl bootstrap gui/$(id -u) {}", path.display());
579        Ok(ServiceUnit {
580            kind: "launchd",
581            path,
582            text,
583            install_command: install,
584            files: Vec::new(),
585        })
586    } else {
587        let path = home
588            .join(SERVICE_DIR)
589            .join(format!("{SERVICE_NAME}.service"));
590        let text = format!(
591            "[Unit]\n\
592             Description=supercode teams machine daemon ({home_display})\n\
593             After=network.target\n\
594             \n\
595             [Service]\n\
596             Environment=SUPERCODE_TEAMS_HOME={home_display}\n\
597             ExecStart={node} {entry_display} machine start\n\
598             Restart=on-failure\n\
599             KillSignal=SIGTERM\n\
600             KillMode=process\n\
601             \n\
602             [Install]\n\
603             WantedBy=default.target\n"
604        );
605        let install = format!(
606            "systemctl --user link {} && systemctl --user enable --now {SERVICE_NAME}",
607            path.display()
608        );
609        Ok(ServiceUnit {
610            kind: "systemd",
611            path,
612            text,
613            install_command: install,
614            files: Vec::new(),
615        })
616    }
617}
618
619/// Write a rendered unit under `<home>/service/`.
620pub fn write_unit(unit: &ServiceUnit) -> Result<(), TeamsError> {
621    if let Some(parent) = unit.path.parent() {
622        std::fs::create_dir_all(parent).map_err(|source| TeamsError::File {
623            path: unit.path.clone(),
624            source,
625        })?;
626    }
627    std::fs::write(&unit.path, &unit.text).map_err(|source| TeamsError::File {
628        path: unit.path.clone(),
629        source,
630    })?;
631    for (path, text) in &unit.files {
632        std::fs::write(path, text).map_err(|source| TeamsError::File {
633            path: path.clone(),
634            source,
635        })?;
636    }
637    Ok(())
638}
639
640/// Run a service-manager command and return (success, stdout+stderr).
641fn run_tool(program: &str, args: &[&str]) -> Result<(bool, String), std::io::Error> {
642    let output = std::process::Command::new(program).args(args).output()?;
643    let mut text = String::from_utf8_lossy(&output.stdout).into_owned();
644    text.push_str(&String::from_utf8_lossy(&output.stderr));
645    Ok((output.status.success(), text.trim().to_string()))
646}
647
648#[cfg(target_os = "macos")]
649fn gui_domain() -> String {
650    // SAFETY: `getuid` reads this process's own real user id and cannot fail.
651    format!("gui/{}", unsafe { libc::getuid() })
652}
653
654/// What the platform's service manager says about the teams daemon unit.
655///
656/// Never starts or installs anything.
657pub fn service_status() -> ServiceState {
658    platform_status()
659}
660
661#[cfg(target_os = "macos")]
662fn platform_status() -> ServiceState {
663    let label = SERVICE_NAME.to_string();
664    let target = format!("{}/{SERVICE_NAME}", gui_domain());
665    match run_tool("launchctl", &["print", &target]) {
666        Ok((true, text)) => ServiceState {
667            kind: "launchd",
668            label,
669            installed: true,
670            pid: field_of(&text, "pid = ").and_then(|value| value.parse().ok()),
671            detail: field_of(&text, "state = ").unwrap_or_else(|| "loaded".into()),
672        },
673        Ok((false, _)) => ServiceState {
674            kind: "launchd",
675            label,
676            installed: false,
677            pid: None,
678            detail: format!("not bootstrapped in {}", gui_domain()),
679        },
680        Err(error) => ServiceState {
681            kind: "launchd",
682            label,
683            installed: false,
684            pid: None,
685            detail: format!("launchctl unavailable: {error}"),
686        },
687    }
688}
689
690#[cfg(all(unix, not(target_os = "macos")))]
691fn platform_status() -> ServiceState {
692    let label = SERVICE_NAME.to_string();
693    match run_tool("systemctl", &["--user", "is-active", SERVICE_NAME]) {
694        Ok((active, text)) => {
695            let known = run_tool("systemctl", &["--user", "is-enabled", SERVICE_NAME])
696                .map(|(ok, _)| ok)
697                .unwrap_or(false);
698            ServiceState {
699                kind: "systemd",
700                label,
701                installed: active || known,
702                pid: None,
703                detail: if text.is_empty() {
704                    "unknown".into()
705                } else {
706                    text
707                },
708            }
709        }
710        Err(error) => ServiceState {
711            kind: "systemd",
712            label,
713            installed: false,
714            pid: None,
715            detail: format!("systemctl unavailable: {error}"),
716        },
717    }
718}
719
720#[cfg(not(unix))]
721fn platform_status() -> ServiceState {
722    named_service_status(SERVICE_NAME)
723}
724
725/// `key = value` out of a service manager's block output.
726#[cfg(target_os = "macos")]
727fn field_of(text: &str, key: &str) -> Option<String> {
728    text.lines()
729        .find_map(|line| line.trim().strip_prefix(key))
730        .map(|value| value.trim().to_string())
731}
732
733/// The file name the unit takes on this platform.
734fn unit_file_name() -> String {
735    if cfg!(windows) {
736        format!("{SERVICE_NAME}.xml")
737    } else if cfg!(target_os = "macos") {
738        format!("{SERVICE_NAME}.plist")
739    } else {
740        format!("{SERVICE_NAME}.service")
741    }
742}
743
744/// Render the unit, hand it to the platform's service manager, and start it.
745///
746/// Refuses a label the manager already holds rather than replacing it: two
747/// homes share one label, so an install that silently took it over would point
748/// a running node at a different folder.
749pub fn install_service(
750    home: &Path,
751    entry: &Path,
752    node: &str,
753) -> Result<(ServiceUnit, ServiceState), TeamsError> {
754    let existing = service_status();
755    if existing.installed {
756        return Err(TeamsError::Service {
757            action: "install",
758            detail: format!(
759                "`{}` is already installed ({}); `supercode teams machine uninstall` first",
760                existing.label, existing.detail
761            ),
762        });
763    }
764    let unit = service_unit(home, entry, &absolute_program(node))?;
765    write_unit(&unit)?;
766    platform_install(&unit)?;
767    Ok((unit, service_status()))
768}
769
770/// A persistent connector must not depend on a mutable checkout or Cargo output.
771/// Resolve symlinks too: an npm-linked SDK is still source, not an installed copy.
772pub fn validate_connector_install_paths(entry: &Path, binary: &Path) -> Result<(), TeamsError> {
773    for (label, path) in [("Teams entry", entry), ("supercode binary", binary)] {
774        let resolved = std::fs::canonicalize(path).map_err(|source| TeamsError::File {
775            path: path.to_path_buf(),
776            source,
777        })?;
778        let checkout = resolved.ancestors().any(|dir| dir.join(".git").exists());
779        let cargo_output = resolved
780            .parent()
781            .is_some_and(|dir| dir.join("deps").is_dir());
782        if checkout || cargo_output {
783            return Err(TeamsError::Service {
784                action: "install",
785                detail: format!(
786                    "{label} is source/build output at {}; the connector service was not changed. Use an installed package and binary, or teams connect --foreground for source work",
787                    resolved.display()
788                ),
789            });
790        }
791    }
792    Ok(())
793}
794
795/// Install a rendered context connector. An identical installed unit is an
796/// idempotent success. A different unit in this home's own service folder is
797/// this home's connector with new parameters (a new build, PATH or folder), so
798/// it is replaced and restarted; a label the manager holds with no unit here
799/// belongs to another home and is refused.
800pub fn install_connector_service(
801    unit: &ServiceUnit,
802    label: &str,
803) -> Result<ServiceState, TeamsError> {
804    let existing = named_service_status(label);
805    if unit.path.exists() {
806        let old = std::fs::read_to_string(&unit.path).map_err(|source| TeamsError::File {
807            path: unit.path.clone(),
808            source,
809        })?;
810        // A Windows unit's environment lives in its companion file, so that is compared too.
811        let same = old == unit.text
812            && unit
813                .files
814                .iter()
815                .all(|(path, text)| std::fs::read_to_string(path).is_ok_and(|old| &old == text));
816        if same && existing.installed {
817            return Ok(existing);
818        }
819        if !same && existing.installed {
820            named_platform_uninstall(label)?;
821        }
822    } else if existing.installed {
823        return Err(TeamsError::Service {
824            action: "install",
825            detail: format!(
826                "service manager already owns `{label}` without its expected unit file"
827            ),
828        });
829    }
830    write_unit(unit)?;
831    named_platform_install(unit, label)?;
832    Ok(named_service_status(label))
833}
834
835/// Give every installed harness supercode's messaging tools: register
836/// `<supercode> message mcp` as a user-scope MCP server named `supercode`
837/// through each harness's own `mcp add`. An entry that runs another binary
838/// (an older install, a build that is gone) is replaced through the harness's
839/// own `mcp remove`: a session loads only a server that starts. A harness
840/// whose CLI is absent is left as it is. One line per harness says what
841/// happened.
842pub fn register_message_tools(supercode: &Path) -> Vec<String> {
843    let program = supercode.display().to_string();
844    let mut report = Vec::new();
845    for (harness, get, remove, add) in [
846        (
847            "claude",
848            vec!["mcp", "get", "supercode"],
849            vec!["mcp", "remove", "--scope", "user", "supercode"],
850            vec![
851                "mcp",
852                "add",
853                "--scope",
854                "user",
855                "supercode",
856                "--",
857                &program,
858                "message",
859                "mcp",
860            ],
861        ),
862        (
863            "codex",
864            vec!["mcp", "get", "supercode"],
865            vec!["mcp", "remove", "supercode"],
866            vec!["mcp", "add", "supercode", "--", &program, "message", "mcp"],
867        ),
868    ] {
869        let run = |args: &[&str]| {
870            std::process::Command::new(resolve_program(harness))
871                .args(args)
872                .stdin(std::process::Stdio::null())
873                .output()
874        };
875        // Paths compare as text, and Windows paths without regard to case.
876        let names_program = |text: &str| {
877            if cfg!(windows) {
878                text.to_lowercase().contains(&program.to_lowercase())
879            } else {
880                text.contains(&program)
881            }
882        };
883        let replaced = match run(&get) {
884            Err(_) => {
885                report.push(format!("{harness}: not installed"));
886                continue;
887            }
888            Ok(found)
889                if found.status.success()
890                    && names_program(&String::from_utf8_lossy(&found.stdout)) =>
891            {
892                report.push(format!("{harness}: already has supercode's tools"));
893                continue;
894            }
895            Ok(found) if found.status.success() => {
896                let _ = run(&remove);
897                true
898            }
899            Ok(_) => false,
900        };
901        match run(&add) {
902            Ok(added) if added.status.success() => report.push(if replaced {
903                format!("{harness}: supercode's tools now run {program} (new sessions load them)")
904            } else {
905                format!("{harness}: supercode's tools added (new sessions load them)")
906            }),
907            Ok(added) => report.push(format!(
908                "{harness}: could not add supercode's tools: {}",
909                String::from_utf8_lossy(&added.stderr).trim()
910            )),
911            Err(error) => report.push(format!(
912                "{harness}: could not add supercode's tools: {error}"
913            )),
914        }
915    }
916    report
917}
918
919/// Stop and remove exactly one context connector service.
920pub fn uninstall_connector_service(
921    teams_home: &Path,
922    label: &str,
923) -> Result<ServiceState, TeamsError> {
924    named_platform_uninstall(label)?;
925    let unit = teams_home
926        .join(SERVICE_DIR)
927        .join(format!("{label}.{}", connector_unit_suffix()));
928    for path in [unit, service_env_path(teams_home, label)] {
929        match std::fs::remove_file(&path) {
930            Ok(()) => {}
931            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
932            Err(source) => return Err(TeamsError::File { path, source }),
933        }
934    }
935    Ok(named_service_status(label))
936}
937
938/// Read-only service-manager status for one context connector.
939pub fn connector_service_status(label: &str) -> ServiceState {
940    named_service_status(label)
941}
942
943/// Whether a service manager runs this OS user's machine daemon: the machine
944/// unit, or a context connector unit written under the teams home, is
945/// installed. Such a daemon is brought back by its service manager; nothing
946/// else should start one in its place. Never starts or installs anything.
947pub fn service_owns_daemon() -> bool {
948    if service_status().installed {
949        return true;
950    }
951    let Ok(entries) = std::fs::read_dir(teams_home().join(SERVICE_DIR)) else {
952        return false;
953    };
954    entries.flatten().any(|entry| {
955        let name = entry.file_name().to_string_lossy().into_owned();
956        let label = name
957            .strip_suffix(".plist")
958            .or_else(|| name.strip_suffix(".service"))
959            .unwrap_or(&name);
960        label.starts_with("dev.volter.supercode-teams-connector-")
961            && (name.ends_with(".plist") || name.ends_with(".service"))
962            && connector_service_status(label).installed
963    })
964}
965
966#[cfg(target_os = "macos")]
967fn named_service_status(label: &str) -> ServiceState {
968    let target = format!("{}/{label}", gui_domain());
969    match run_tool("launchctl", &["print", &target]) {
970        Ok((true, text)) => ServiceState {
971            kind: "launchd",
972            label: label.into(),
973            installed: true,
974            pid: field_of(&text, "pid = ").and_then(|value| value.parse().ok()),
975            detail: field_of(&text, "state = ").unwrap_or_else(|| "loaded".into()),
976        },
977        Ok((false, _)) => ServiceState {
978            kind: "launchd",
979            label: label.into(),
980            installed: false,
981            pid: None,
982            detail: format!("not bootstrapped in {}", gui_domain()),
983        },
984        Err(error) => ServiceState {
985            kind: "launchd",
986            label: label.into(),
987            installed: false,
988            pid: None,
989            detail: format!("launchctl unavailable: {error}"),
990        },
991    }
992}
993
994#[cfg(all(unix, not(target_os = "macos")))]
995fn named_service_status(label: &str) -> ServiceState {
996    match run_tool("systemctl", &["--user", "is-active", label]) {
997        Ok((active, text)) => {
998            let known = run_tool("systemctl", &["--user", "is-enabled", label])
999                .map(|(ok, _)| ok)
1000                .unwrap_or(false);
1001            ServiceState {
1002                kind: "systemd",
1003                label: label.into(),
1004                installed: active || known,
1005                pid: None,
1006                detail: if text.is_empty() {
1007                    "unknown".into()
1008                } else {
1009                    text
1010                },
1011            }
1012        }
1013        Err(error) => ServiceState {
1014            kind: "systemd",
1015            label: label.into(),
1016            installed: false,
1017            pid: None,
1018            detail: format!("systemctl unavailable: {error}"),
1019        },
1020    }
1021}
1022
1023#[cfg(not(unix))]
1024fn named_service_status(label: &str) -> ServiceState {
1025    match run_tool("schtasks", &["/Query", "/TN", label, "/FO", "CSV", "/NH"]) {
1026        // `"TaskName","Next Run Time","Status"`: the last field is the task's state, in the system's language.
1027        Ok((true, text)) => ServiceState {
1028            kind: "schtasks",
1029            label: label.into(),
1030            installed: true,
1031            pid: None,
1032            detail: text
1033                .lines()
1034                .next()
1035                .and_then(|line| line.rsplit(',').next())
1036                .map(|state| state.trim_matches('"').to_string())
1037                .filter(|state| !state.is_empty())
1038                .unwrap_or_else(|| "registered".into()),
1039        },
1040        Ok((false, _)) => ServiceState {
1041            kind: "schtasks",
1042            label: label.into(),
1043            installed: false,
1044            pid: None,
1045            detail: "no scheduled task".into(),
1046        },
1047        Err(error) => ServiceState {
1048            kind: "schtasks",
1049            label: label.into(),
1050            installed: false,
1051            pid: None,
1052            detail: format!("schtasks unavailable: {error}"),
1053        },
1054    }
1055}
1056
1057#[cfg(target_os = "macos")]
1058fn named_platform_install(unit: &ServiceUnit, _label: &str) -> Result<(), TeamsError> {
1059    platform_install(unit)
1060}
1061#[cfg(all(unix, not(target_os = "macos")))]
1062fn named_platform_install(unit: &ServiceUnit, label: &str) -> Result<(), TeamsError> {
1063    let path = unit.path.display().to_string();
1064    for args in [
1065        vec!["--user", "link", path.as_str()],
1066        vec!["--user", "enable", "--now", label],
1067    ] {
1068        let (ok, text) = run_tool("systemctl", &args).map_err(|error| TeamsError::Service {
1069            action: "install",
1070            detail: format!("systemctl: {error}"),
1071        })?;
1072        if !ok {
1073            return Err(TeamsError::Service {
1074                action: "install",
1075                detail: format!("systemctl {}: {text}", args.join(" ")),
1076            });
1077        }
1078    }
1079    Ok(())
1080}
1081#[cfg(not(unix))]
1082fn named_platform_install(unit: &ServiceUnit, label: &str) -> Result<(), TeamsError> {
1083    // Task Scheduler reads task XML only as UTF-16; the unit itself stays UTF-8 so an install can compare it.
1084    let task = unit.path.with_extension("utf16.xml");
1085    let bytes: Vec<u8> = [0xFF, 0xFE]
1086        .into_iter()
1087        .chain(unit.text.encode_utf16().flat_map(u16::to_le_bytes))
1088        .collect();
1089    std::fs::write(&task, bytes).map_err(|source| TeamsError::File {
1090        path: task.clone(),
1091        source,
1092    })?;
1093    let task_path = task.display().to_string();
1094    let created = run_tool(
1095        "schtasks",
1096        &["/Create", "/TN", label, "/XML", task_path.as_str(), "/F"],
1097    )
1098    .map_err(|error| TeamsError::Service {
1099        action: "install",
1100        detail: format!("schtasks: {error}"),
1101    })
1102    .and_then(|(ok, text)| {
1103        if ok {
1104            Ok(())
1105        } else {
1106            Err(TeamsError::Service {
1107                action: "install",
1108                detail: format!("schtasks /Create: {text}"),
1109            })
1110        }
1111    });
1112    if let Err(error) = created {
1113        let _ = std::fs::remove_file(&task);
1114        return Err(error);
1115    }
1116    // A task already running keeps its old instance: Task Scheduler refuses a
1117    // second one (0x800710E0) while `/Run` still reports success, so the
1118    // replaced connector would go on running the old code. End it first; a
1119    // task that is not running answers an error here, which is fine.
1120    let _ = run_tool("schtasks", &["/End", "/TN", label]);
1121    let result = [vec!["/Run", "/TN", label]].iter().try_for_each(|args| {
1122        let (ok, text) = run_tool("schtasks", args).map_err(|error| TeamsError::Service {
1123            action: "install",
1124            detail: format!("schtasks: {error}"),
1125        })?;
1126        if ok {
1127            Ok(())
1128        } else {
1129            Err(TeamsError::Service {
1130                action: "install",
1131                detail: format!("schtasks {}: {text}", args[0]),
1132            })
1133        }
1134    });
1135    let _ = std::fs::remove_file(&task);
1136    result
1137}
1138
1139#[cfg(target_os = "macos")]
1140fn named_platform_uninstall(label: &str) -> Result<(), TeamsError> {
1141    let target = format!("{}/{label}", gui_domain());
1142    let (ok, text) =
1143        run_tool("launchctl", &["bootout", &target]).map_err(|error| TeamsError::Service {
1144            action: "uninstall",
1145            detail: format!("launchctl: {error}"),
1146        })?;
1147    if !ok && !text.contains("No such process") && !text.contains("not find") {
1148        return Err(TeamsError::Service {
1149            action: "uninstall",
1150            detail: format!("launchctl bootout {target}: {text}"),
1151        });
1152    }
1153    // bootout returns before launchd has let the label go, and a bootstrap
1154    // in that window fails with an I/O error; wait for it to be released.
1155    for _ in 0..50 {
1156        if !named_service_status(label).installed {
1157            break;
1158        }
1159        std::thread::sleep(std::time::Duration::from_millis(100));
1160    }
1161    Ok(())
1162}
1163#[cfg(all(unix, not(target_os = "macos")))]
1164fn named_platform_uninstall(label: &str) -> Result<(), TeamsError> {
1165    let _ = run_tool("systemctl", &["--user", "disable", "--now", label]);
1166    Ok(())
1167}
1168#[cfg(not(unix))]
1169fn named_platform_uninstall(label: &str) -> Result<(), TeamsError> {
1170    // The task goes first, so its minute trigger cannot start the service again while it is being stopped.
1171    let (ok, text) = run_tool("schtasks", &["/Delete", "/TN", label, "/F"]).map_err(|error| {
1172        TeamsError::Service {
1173            action: "uninstall",
1174            detail: format!("schtasks: {error}"),
1175        }
1176    })?;
1177    if !ok && named_service_status(label).installed {
1178        return Err(TeamsError::Service {
1179            action: "uninstall",
1180            detail: format!("schtasks /Delete: {text}"),
1181        });
1182    }
1183    // Deleting a task leaves what it started running, so the service is stopped by the one thing on its command
1184    // line that is its own: the environment file, on a node or its conhost. The path rides in the environment, not
1185    // the command line, so this query does not match itself.
1186    let env_path = service_env_path(&teams_home(), label);
1187    let stopped = std::process::Command::new("powershell.exe")
1188        .args([
1189            "-NoProfile",
1190            "-NonInteractive",
1191            "-Command",
1192            "Get-CimInstance Win32_Process -Filter \"Name='node.exe' OR Name='conhost.exe'\" | Where-Object { $_.CommandLine -and $_.CommandLine.Contains($env:SUPERCODE_SERVICE_ENV_FILE) } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force }",
1193        ])
1194        .env("SUPERCODE_SERVICE_ENV_FILE", env_path.display().to_string())
1195        .stdin(std::process::Stdio::null())
1196        .output();
1197    match stopped {
1198        Ok(output) if output.status.success() => Ok(()),
1199        Ok(output) => Err(TeamsError::Service {
1200            action: "uninstall",
1201            detail: format!(
1202                "the task is gone, but what it started may still run: {}",
1203                String::from_utf8_lossy(&output.stderr).trim()
1204            ),
1205        }),
1206        Err(error) => Err(TeamsError::Service {
1207            action: "uninstall",
1208            detail: format!(
1209                "the task is gone, but what it started may still run: powershell: {error}"
1210            ),
1211        }),
1212    }
1213}
1214
1215#[cfg(target_os = "macos")]
1216fn platform_install(unit: &ServiceUnit) -> Result<(), TeamsError> {
1217    let path = unit.path.display().to_string();
1218    let (ok, text) =
1219        run_tool("launchctl", &["bootstrap", &gui_domain(), &path]).map_err(|error| {
1220            TeamsError::Service {
1221                action: "install",
1222                detail: format!("launchctl: {error}"),
1223            }
1224        })?;
1225    if !ok {
1226        return Err(TeamsError::Service {
1227            action: "install",
1228            detail: format!("launchctl bootstrap {}: {text}", gui_domain()),
1229        });
1230    }
1231    Ok(())
1232}
1233
1234/// Untested on this box (the receipt is macOS); these are the commands
1235/// `service_unit` prints as its `install_command`.
1236#[cfg(all(unix, not(target_os = "macos")))]
1237fn platform_install(unit: &ServiceUnit) -> Result<(), TeamsError> {
1238    let path = unit.path.display().to_string();
1239    for args in [
1240        vec!["--user", "link", path.as_str()],
1241        vec!["--user", "enable", "--now", SERVICE_NAME],
1242    ] {
1243        let (ok, text) = run_tool("systemctl", &args).map_err(|error| TeamsError::Service {
1244            action: "install",
1245            detail: format!("systemctl: {error}"),
1246        })?;
1247        if !ok {
1248            return Err(TeamsError::Service {
1249                action: "install",
1250                detail: format!("systemctl {}: {text}", args.join(" ")),
1251            });
1252        }
1253    }
1254    Ok(())
1255}
1256
1257#[cfg(not(unix))]
1258fn platform_install(unit: &ServiceUnit) -> Result<(), TeamsError> {
1259    named_platform_install(unit, SERVICE_NAME)
1260}
1261
1262/// Stop and unregister the unit, and remove the rendered file.
1263///
1264/// Idempotent: a unit the manager does not hold is not an error, because the
1265/// state the operator asked for is the state they get.
1266pub fn uninstall_service(home: &Path) -> Result<ServiceState, TeamsError> {
1267    platform_uninstall()?;
1268    let unit_path = home.join(SERVICE_DIR).join(unit_file_name());
1269    // A Windows unit's environment file goes with it; elsewhere there is none and its absence is fine.
1270    for path in [unit_path, service_env_path(home, SERVICE_NAME)] {
1271        match std::fs::remove_file(&path) {
1272            Ok(()) => {}
1273            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
1274            Err(source) => return Err(TeamsError::File { path, source }),
1275        }
1276    }
1277    // `launchctl bootout` returns before the job is torn down, so the state
1278    // this reports is the settled one, not the manager mid-teardown.
1279    let mut state = service_status();
1280    for _ in 0..40 {
1281        if !state.installed {
1282            break;
1283        }
1284        std::thread::sleep(std::time::Duration::from_millis(100));
1285        state = service_status();
1286    }
1287    Ok(state)
1288}
1289
1290#[cfg(target_os = "macos")]
1291fn platform_uninstall() -> Result<(), TeamsError> {
1292    let target = format!("{}/{SERVICE_NAME}", gui_domain());
1293    let (ok, text) =
1294        run_tool("launchctl", &["bootout", &target]).map_err(|error| TeamsError::Service {
1295            action: "uninstall",
1296            detail: format!("launchctl: {error}"),
1297        })?;
1298    // `bootout` on a label nobody holds says so and exits non-zero.
1299    if !ok && !text.contains("No such process") && !text.contains("not find") {
1300        return Err(TeamsError::Service {
1301            action: "uninstall",
1302            detail: format!("launchctl bootout {target}: {text}"),
1303        });
1304    }
1305    Ok(())
1306}
1307
1308#[cfg(all(unix, not(target_os = "macos")))]
1309fn platform_uninstall() -> Result<(), TeamsError> {
1310    let _ = run_tool("systemctl", &["--user", "disable", "--now", SERVICE_NAME]);
1311    Ok(())
1312}
1313
1314#[cfg(not(unix))]
1315fn platform_uninstall() -> Result<(), TeamsError> {
1316    named_platform_uninstall(SERVICE_NAME)
1317}