{"record":{"id":"d4fcdb2317db94cb","repo":"nikivdev/code","slug":"launchctl-bootstrap-failed","errorCode":null,"errorMessage":"launchctl bootstrap failed: {}","messagePattern":"launchctl bootstrap failed: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/supervisor.rs","lineNumber":571,"sourceCode":"    let plist_path = launch_agent_plist_path()?;\n    let log_path = supervisor_log_path().ok();\n    let plist = launch_agent_plist(socket_path, boot, log_path.as_deref())?;\n    fs::write(&plist_path, plist)\n        .with_context(|| format!(\"failed to write {}\", plist_path.display()))?;\n\n    let domain = launch_agent_domain();\n    let target = launch_agent_target();\n\n    let _ = Command::new(\"launchctl\")\n        .args([\"bootout\", &domain, plist_path.to_string_lossy().as_ref()])\n        .output();\n\n    let output = Command::new(\"launchctl\")\n        .args([\"bootstrap\", &domain, plist_path.to_string_lossy().as_ref()])\n        .output()\n        .context(\"failed to bootstrap launch agent\")?;\n    if !output.status.success() {\n        bail!(\n            \"launchctl bootstrap failed: {}\",\n            String::from_utf8_lossy(&output.stderr)\n        );\n    }\n\n    let _ = Command::new(\"launchctl\").args([\"enable\", &target]).output();\n\n    let output = Command::new(\"launchctl\")\n        .args([\"kickstart\", \"-k\", &target])\n        .output()\n        .context(\"failed to kickstart launch agent\")?;\n    if !output.status.success() {\n        bail!(\n            \"launchctl kickstart failed: {}\",\n            String::from_utf8_lossy(&output.stderr)\n        );\n    }\n","sourceCodeStart":553,"sourceCodeEnd":589,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/supervisor.rs#L553-L589","documentation":"install_launch_agent registers the supervisor as a macOS launchd agent by running `launchctl bootstrap <domain> <plist>`. If launchctl exits non-zero, the stderr is surfaced in this error. Typically means the plist is invalid, the service is already bootstrapped, or the domain-target is wrong.","triggerScenarios":"install_launch_agent (called by run and ensure_supervisor_via_launchd) on macOS when launchctl bootstrap returns non-zero — e.g. \"Bootstrap failed: 5: Input/output error\" or \"Operation already in progress\" because the agent is already loaded.","commonSituations":"Re-running bootstrap when the agent is already loaded (launchctl requires bootout first); malformed or stale plist path; missing GUI domain (running in an SSH session without a user GUI domain); macOS sandbox/permission issues.","solutions":["If the agent is already loaded, run `launchctl bootout <domain>/<plist>` first, then retry bootstrap.","Check the stderr in the error: \"already bootstrapped\" means skip bootstrap and just kickstart.","Verify the plist file exists and is valid XML (plutil -lint <plist>).","Ensure you're in a GUI login session (not plain SSH) or use the correct domain target for your context."],"exampleFix":"// before\nlaunchctl bootstrap gui/$UID ~/Library/LaunchAgents/app.myapp.supervisor.plist\n// Bootstrap failed: 5: Input/output error (already loaded)\n// after\nlaunchctl bootout gui/$UID/app.myapp.supervisor || true\nlaunchctl bootstrap gui/$UID ~/Library/LaunchAgents/app.myapp.supervisor.plist","handlingStrategy":"try-catch","validationCode":"// preflight on macOS: lint plist and skip if already bootstrapped\nimport { execSync } from 'node:child_process';\nif (process.platform === 'darwin') {\n  execSync(`plutil -lint ${plistPath}`); // throws if malformed\n  const loaded = execSync(`launchctl print gui/$(id -u)/${label} 2>&1 || true`).toString();\n  if (!loaded.includes('Could not find')) {\n    console.log('Agent already bootstrapped; skipping bootstrap.');\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await supervisorInstallLaunchAgent();\n} catch (e) {\n  const msg = String(e);\n  if (msg.includes('launchctl bootstrap failed')) {\n    if (msg.includes('already bootstrapped') || msg.includes('Input/output error')) {\n      console.error('Agent likely already loaded; bootout first then retry.');\n    }\n    return;\n  }\n  throw e;\n}","preventionTips":["Bootout an existing agent before re-bootstrapping to avoid 'already loaded' failures.","Validate plists with `plutil -lint` before installing.","Run launchctl operations from a GUI login session, not bare SSH.","Generate the plist path/label programmatically and reuse one constant."],"tags":["macos","launchd","launchctl","process","supervisor"],"backgroundTag":"launchctl-bootstrap-failed","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}