Skip to main content

Module autostart

Module autostart 

Source
Expand description

Auto-start-at-login registration — issue #2977 Workstream 2, docs/specs/SPEC_TRAY_OPTIONAL_BACKGROUND_SERVICE_2026_09_04.md §4.

§Opt-in, off by default, and separate from “close to tray”

Nothing here runs unless the user explicitly asks for it. The issue is emphatic that these are two different decisions: “a user can have ‘close-to-tray keeps it running for this session’ without ‘starts automatically at every login’ being bundled into the same toggle”. So auto-start has its own state, its own enable/disable, and is never implied by AGENTMUX_TRAY or AGENTMUX_BACKGROUND_SERVICE.

§Mechanism per platform, and what was rejected

  • Windows — Scheduled Task, LogonType=InteractiveToken. Not a Windows Service (needs admin to register, and Session-0 isolation means it structurally cannot show a tray icon), and not a bare Run registry key (no crash supervision, no delay control).
  • macOS — LaunchAgent plist. This DEVIATES from the governing spec, and the deviation has a user-visible consequence. SPEC_TRAY_OPTIONAL_BACKGROUND_SERVICE_2026_09_04.md §4 calls SMAppService “unambiguously current best practice” and notes it is the only path that surfaces correctly in System Settings → Login Items & Extensions. Writing the LaunchAgent directly means the entry may not appear there the way a user expects, which matters more than usual here: Workstream 4 requires that a user be able to see and remove a background service, and Login Items is where they will look. SMAppService is only callable through its ObjC interface from inside a bundled, signed app, so adopting it means objc2 bindings plus a bundle identifier that matches the registered agent — real work that cannot be written blind or verified on a non-macOS machine. Recorded as a known gap rather than pretended away: macOS auto-start should move to SMAppService before it is offered to users. (ReAgent P1 on PR #2999.)
  • Linux — XDG autostart .desktop. systemd --user is deliberately NOT the default: surviving logout needs loginctl enable-linger, which is a separate, consequential decision the issue says must never be silently enabled.

§Uninstall is a first-class operation (Workstream 4)

WS4 requires “one complete, independently-testable uninstall path that actually removes the LaunchAgent/Scheduled-Task/autostart artifact — not just closes the window”. disable() is that path, and is_enabled() makes it independently checkable: the pair is the whole contract, and disable() is idempotent so an uninstaller can call it unconditionally.

§Structure

The artifact content (Task Scheduler XML, plist, desktop entry) is generated by pure functions that take an exe path and return a string. That keeps the part most likely to be subtly wrong — escaping, schema fields, the InteractiveToken logon type — unit-testable without touching the machine’s real login configuration.

Modules§

login_item
macOS: start at login through SMAppService, so the entry shows in System Settings -> General -> Login Items & Extensions where people look for it, and respects the approval they give there (docs/specs/SPEC_START_WITH_OS_2026_09_25.md §3.7, phase 4).

Structs§

Entry
The login entry as it stands on this machine.
Status
--autostart-status --json: what Settings shows next to the switch.

Enums§

Plan
What to do to bring the login entry in line with app:startatlogin (SPEC_START_WITH_OS_2026_09_25.md §3.9).
State
How the login entry stands (SPEC_START_WITH_OS_2026_09_25.md §3.8).

Constants§

APPIMAGELAUNCHER_OFF_PREFIX 🔒
How an AppImage’s Exec line starts.
AUTOSTART_ID
Identifier used for the artifact on every platform: the Scheduled Task name, the LaunchAgent label’s suffix, and the .desktop filename stem. Stable, because disable() has to find what enable() created — including across upgrades, so it deliberately carries no version.
BACKGROUND_FLAG
The flag every generated artifact passes to the launcher.
MACOS_LABEL
Reverse-DNS label for the macOS LaunchAgent, which requires that form.
PLIST_CHANNEL_KEY 🔒
Owning channel, as a plist key launchd ignores.
STABLE_EXE_ENV 🔒
Env var the Linux AppRun and wrappers set to their own path right before they exec the launcher.
TASK_CHANNEL_MARKER 🔒
Marks the owning channel inside the Task’s <Description>, the only free-text field Task Scheduler keeps verbatim.
XDG_CHANNEL_KEY 🔒
Owning channel, as a vendor key (X- keys are ignored by the session).

Statics§

MOVED_FROM 🔒
Where the AppImage this launcher runs from was before it was moved or renamed, when it was (agentmux_common::appimage_record::reconcile, at start).
STABLE_EXE 🔒

Functions§

artifact_path
Where the artifact lives, for the platforms that use a plain file. Not used on Windows, whose artifact lives in the Task Scheduler store rather than a path we own.
background_env_for
Environment the host must receive when the launcher was started with --background.
background_requested
Did the launcher get started in background mode?
between 🔒
current_channel
Handle the auto-start CLI verbs, if present. Returns true when one was handled and the process should exit.
current_status
status of this machine, with the login item’s approval: a registration the person has not allowed (or has switched off) in System Settings is needs_approval, not on (SPEC_START_WITH_OS_2026_09_25.md §3.8).
decode_console_output 🔒
schtasks /XML writes UTF-16LE when its output is redirected.
desktop_exec_escape 🔒
Escape a path for use inside a quoted argument of a desktop entry’s Exec= value.
desktop_string_escape 🔒
Escape a plain desktop-entry string value (TryExec, vendor keys), per the Desktop Entry spec’s \\, \n, \t, \r escapes. A raw newline would end the line and let a path inject keys, as desktop_exec_escape explains.
desktop_string_unescape 🔒
disable
Remove auto-start registration. Idempotent — succeeds when nothing was registered, so an uninstaller can call it unconditionally without first asking whether the feature was ever turned on.
enable
Register auto-start for exe. Idempotent: re-registering an existing entry replaces it, which is also how an upgrade repoints the artifact at a new install path.
entry_runs_from
Does the login entry run a file inside dir? An uninstaller asks this with its own install folder, so it removes the entry that would be left pointing at nothing (SPEC_START_WITH_OS_2026_09_25.md G6, G7) and leaves an entry that another install or channel owns alone. Windows paths compare without case.
exec_prefix 🔒
What goes before the program in the login entry’s Exec line. An AppImage is run with AppImageLauncher switched off, as the in-app upgrade’s restart does: at login that tool stops a file it hasn’t integrated behind an “Integrate or Run once” window, and AgentMux doesn’t start until someone answers it (#47873).
handle_cli
is_appimage 🔒
is_enabled
launch_agent_plist
macOS LaunchAgent plist.
login_start
Was this process started by a login entry? Every artifact passes --background, and nothing else does.
missing_target
The file a login entry runs, when it isn’t there: the session skips such an entry without a word (the desktop entry’s TryExec), so AgentMux silently stops starting at login.
moved_from 🔒
note_appimage_start
At start, for an AppImage: put its channel record back beside it if a move or a rename left that behind, and keep where it is now (agentmux_common::appimage_record). Remembers where it was, for the login entry to follow.
parse_desktop_entry 🔒
parse_plist 🔒
parse_task_xml 🔒
plan
Pure decision. setting is app:startatlogin, None when never set. target is stable_target(), None when it could not be resolved: only writing needs it, so removing or adopting never waits on it (ReAgent P1 on #3788).
read_entry
Read the current entry back from the platform. None when there is none.
remove_launch_agent_plist 🔒
Remove the login entry written as a file (the LaunchAgent plist, the XDG desktop entry). Succeeds when it is not there, which is what makes disable safe to call unconditionally from an uninstaller.
second_instance_opens_window
A launcher that finds AgentMux already running forwards open_new_window to it, so a double-click opens a window. A login entry firing while the user already has AgentMux open must not (SPEC_START_WITH_OS_2026_09_25.md §3.6): it just exits.
set_moved_from
Remember where this launcher’s AppImage came from, once per run.
stable_target
What the login entry should run: something that survives updates (SPEC_START_WITH_OS_2026_09_25.md §3.1), not this process’s own path.
stable_target_from 🔒
started_by_login_task 🔒
The Store package’s startup task starts the exe with no arguments, so a login start there is known from how Windows activated the process, not from --background. Nowhere else is it ever true.
startup
What the launcher does first, before anything reads either: takes AGENTMUX_STABLE_EXE out of the environment (take_stable_exe_env), and notes where an AppImage has been moved from (note_appimage_start).
status
Pure: the status from what is on the machine. registered is is_enabled, which can say yes when entry could not be read (an entry this build cannot parse still starts something at login). can_write is whether this build has a target to point an entry at.
take_stable_exe_env
Read AGENTMUX_STABLE_EXE and remove it from this process’s env, so no child (host, srv, agent shells, a dev build started from a pane) inherits a path that belongs to this launch. APPIMAGE is not used for the same reason: the AppImage runtime’s copy leaks into every agent shell.
xdg_desktop_entry
Linux XDG autostart desktop entry.
xml_escape 🔒
Minimal XML text escaping for the two generated XML documents.
xml_unescape 🔒