diff --git a/apps/macos/Sources/Clawdbot/GatewayLaunchAgentManager.swift b/apps/macos/Sources/Clawdbot/GatewayLaunchAgentManager.swift index a7b2c6003..bcb9f49de 100644 --- a/apps/macos/Sources/Clawdbot/GatewayLaunchAgentManager.swift +++ b/apps/macos/Sources/Clawdbot/GatewayLaunchAgentManager.swift @@ -4,6 +4,7 @@ enum GatewayLaunchAgentManager { private static let logger = Logger(subsystem: "com.clawdbot", category: "gateway.launchd") private static let supportedBindModes: Set = ["loopback", "tailnet", "lan", "auto"] private static let legacyGatewayLaunchdLabel = "com.steipete.clawdbot.gateway" + private static let disableLaunchAgentMarker = ".clawdbot/disable-launchagent" private static var plistURL: URL { FileManager.default.homeDirectoryForCurrentUser @@ -50,6 +51,10 @@ enum GatewayLaunchAgentManager { } static func set(enabled: Bool, bundlePath: String, port: Int) async -> String? { + if enabled, self.isLaunchAgentWriteDisabled() { + self.logger.info("launchd enable skipped (attach-only or disable marker set)") + return nil + } if enabled { _ = await Launchctl.run(["bootout", "gui/\(getuid())/\(self.legacyGatewayLaunchdLabel)"]) try? FileManager.default.removeItem(at: self.legacyPlistURL) @@ -301,6 +306,17 @@ enum GatewayLaunchAgentManager { } } +extension GatewayLaunchAgentManager { + private static func isLaunchAgentWriteDisabled() -> Bool { + if UserDefaults.standard.bool(forKey: attachExistingGatewayOnlyKey) { + return true + } + let marker = FileManager.default.homeDirectoryForCurrentUser + .appendingPathComponent(self.disableLaunchAgentMarker) + return FileManager.default.fileExists(atPath: marker.path) + } +} + #if DEBUG extension GatewayLaunchAgentManager { static func _testGatewayExecutablePath(bundlePath: String) -> String { diff --git a/scripts/restart-mac.sh b/scripts/restart-mac.sh index dc21d1699..aff81fd82 100755 --- a/scripts/restart-mac.sh +++ b/scripts/restart-mac.sh @@ -18,6 +18,7 @@ LOG_PATH="${CLAWDBOT_RESTART_LOG:-/tmp/clawdbot-restart.log}" NO_SIGN=0 SIGN=0 AUTO_DETECT_SIGNING=1 +GATEWAY_WAIT_SECONDS="${CLAWDBOT_GATEWAY_WAIT_SECONDS:-0}" log() { printf '%s\n' "$*"; } fail() { printf 'ERROR: %s\n' "$*" >&2; exit 1; } @@ -85,6 +86,14 @@ for arg in "$@"; do log " --no-sign Force no code signing (fastest for development)" log " --sign Force code signing (will fail if no signing key available)" log "" + log "Env:" + log " CLAWDBOT_GATEWAY_WAIT_SECONDS=0 Wait time before gateway port check (unsigned only)" + log "" + log "Unsigned recovery:" + log " defaults write clawdbot.gateway.attachExistingOnly -bool YES" + log " node dist/entry.js daemon install --force --runtime node" + log " node dist/entry.js daemon restart" + log "" log "Default behavior: Auto-detect signing keys, fallback to --no-sign if none found" exit 0 ;; @@ -100,6 +109,9 @@ mkdir -p "$(dirname "$LOG_PATH")" rm -f "$LOG_PATH" exec > >(tee "$LOG_PATH") 2>&1 log "==> Log: ${LOG_PATH}" +if [[ "$NO_SIGN" -eq 1 ]]; then + log "==> Using --no-sign (unsigned flow enabled)" +fi acquire_lock @@ -150,6 +162,9 @@ fi if [ "$NO_SIGN" -eq 1 ]; then export ALLOW_ADHOC_SIGNING=1 export SIGN_IDENTITY="-" + mkdir -p "${HOME}/.clawdbot" + run_step "disable launchagent writes (unsigned)" \ + /usr/bin/touch "${HOME}/.clawdbot/disable-launchagent" elif [ "$SIGN" -eq 1 ]; then if ! check_signing_keys; then fail "No signing identity found. Use --no-sign or install a signing key." @@ -184,6 +199,15 @@ choose_app_bundle() { choose_app_bundle +# When unsigned, avoid the app overwriting the LaunchAgent with the relay binary. +if [ "$NO_SIGN" -eq 1 ]; then + APP_BUNDLE_ID="$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "${APP_BUNDLE}/Contents/Info.plist" 2>/dev/null || true)" + if [[ -n "${APP_BUNDLE_ID}" ]]; then + run_step "set attach-existing-only (unsigned)" \ + /usr/bin/defaults write "${APP_BUNDLE_ID}" clawdbot.gateway.attachExistingOnly -bool YES + fi +fi + # 4) Launch the installed app in the foreground so the menu bar extra appears. # LaunchServices can inherit a huge environment from this shell (secrets, prompt vars, etc.). # That can cause launchd spawn failures and is undesirable for a GUI app anyway. @@ -203,3 +227,15 @@ if pgrep -f "${APP_PROCESS_PATTERN}" >/dev/null 2>&1; then else fail "App exited immediately. Check ${LOG_PATH} or Console.app (User Reports)." fi + +# When unsigned, launchd cannot exec the app relay binary. Ensure the gateway +# LaunchAgent targets the repo CLI instead (after the app has launched). +if [ "$NO_SIGN" -eq 1 ]; then + run_step "install gateway launch agent (unsigned)" bash -lc "cd '${ROOT_DIR}' && node dist/entry.js daemon install --force --runtime node" + run_step "restart gateway daemon (unsigned)" bash -lc "cd '${ROOT_DIR}' && node dist/entry.js daemon restart" + if [[ "${GATEWAY_WAIT_SECONDS}" -gt 0 ]]; then + run_step "wait for gateway (unsigned)" sleep "${GATEWAY_WAIT_SECONDS}" + fi + run_step "verify gateway port 18789 (unsigned)" bash -lc "lsof -iTCP:18789 -sTCP:LISTEN | head -n 5 || true" + run_step "show gateway launch agent args (unsigned)" bash -lc "/usr/bin/plutil -p '${HOME}/Library/LaunchAgents/com.clawdbot.gateway.plist' | head -n 40 || true" +fi