diff --git a/apps/macos/Sources/Clawdis/VoiceWakeForwarder.swift b/apps/macos/Sources/Clawdis/VoiceWakeForwarder.swift index d49f42067..1f918776b 100644 --- a/apps/macos/Sources/Clawdis/VoiceWakeForwarder.swift +++ b/apps/macos/Sources/Clawdis/VoiceWakeForwarder.swift @@ -10,23 +10,7 @@ struct VoiceWakeForwardConfig: Sendable { } enum VoiceWakeForwarder { - private final class CLICache: @unchecked Sendable { - private var value: (target: String, path: String)? - private let lock = NSLock() - - func get() -> (target: String, path: String)? { - self.lock.lock(); defer { self.lock.unlock() } - return self.value - } - - func set(_ newValue: (target: String, path: String)?) { - self.lock.lock(); self.value = newValue; self.lock.unlock() - } - } - private static let logger = Logger(subsystem: "com.steipete.clawdis", category: "voicewake.forward") - private static let cliSearchCandidates = ["clawdis-mac"] + cliHelperSearchPaths.map { "\($0)/clawdis-mac" } - private static let cliCache = CLICache() static func prefixedTranscript(_ transcript: String, machineName: String? = nil) -> String { let resolvedMachine = machineName @@ -42,81 +26,18 @@ enum VoiceWakeForwarder { } static func clearCliCache() { - self.cliCache.set(nil) + // Legacy no-op; CLI caching removed now that we rely on AgentRPC. } - private static func cliLookupPrefix(target: String, echoPath: Bool) -> String { - let normalizedTarget = target.trimmingCharacters(in: .whitespacesAndNewlines) - // Use a clean, deterministic PATH so remote shells with spaces or odd entries don't break. - let pathPrefix = "PATH=\(cliHelperSearchPaths.joined(separator: ":"))" - let searchList = self.cliSearchCandidates.joined(separator: " ") - - var steps: [String] = [pathPrefix] - - let cached = self.cliCache.get() - - if let cached, cached.target == normalizedTarget { - steps.append("CLI=\"\(cached.path)\"") - steps.append("if [ ! -x \"$CLI\" ]; then CLI=\"\"; fi") - } else { - steps.append("CLI=\"\"") - } - - steps.append("if [ -z \"${CLI:-}\" ]; then CLI=$(command -v clawdis-mac 2>/dev/null || true); fi") - steps - .append( - "if [ -z \"${CLI:-}\" ]; then for c in \(searchList); do [ -x \"$c\" ] && CLI=\"$c\" && break; done; fi") - steps.append("if [ -z \"${CLI:-}\" ]; then echo 'clawdis-mac missing'; exit 127; fi") - - if echoPath { - steps.append("echo __CLI:$CLI") - } - - return steps.joined(separator: "; ") - } - - static func commandWithCliPath(_ command: String, target: String, echoCliPath: Bool = false) -> String { - let rewritten: String = if command.contains("clawdis-mac") { - command.replacingOccurrences(of: "clawdis-mac", with: "\"$CLI\"") - } else { - "\"$CLI\" \(command)" - } - - return "\(self.cliLookupPrefix(target: target, echoPath: echoCliPath)); \(rewritten)" - } - - #if DEBUG - // Test-only helpers - static func _testSetCliCache(target: String, path: String) { - self.cliCache.set((target: target, path: path)) - } - - static func _testGetCliCache() -> (target: String, path: String)? { - self.cliCache.get() - } - #endif - enum VoiceWakeForwardError: LocalizedError, Equatable { case invalidTarget - case launchFailed(String) - case nonZeroExit(Int32, String) - case cliMissingOrFailed(Int32, String) + case rpcFailed(String) case disabled var errorDescription: String? { switch self { - case .invalidTarget: return "Missing or invalid SSH target" - case let .launchFailed(message): return "ssh failed to start: \(message)" - case let .nonZeroExit(code, output): - let clipped = output.prefix(240) - return clipped.isEmpty - ? "ssh exited with code \(code) (verify host, key, and PATH)" - : "ssh exited with code \(code): \(clipped)" - case let .cliMissingOrFailed(code, output): - let clipped = output.prefix(240) - return clipped.isEmpty - ? "clawdis-mac failed on remote (code \(code))" - : "clawdis-mac failed on remote (code \(code)): \(clipped)" + case .invalidTarget: return "Missing or invalid target" + case let .rpcFailed(message): return message case .disabled: return "Voice wake forwarding disabled" } } @@ -128,151 +49,33 @@ enum VoiceWakeForwarder { config: VoiceWakeForwardConfig) async -> Result { guard config.enabled else { return .failure(.disabled) } - let destination = config.target.trimmingCharacters(in: .whitespacesAndNewlines) - guard let parsed = self.parse(target: destination) else { - self.logger.error("voice wake forward skipped: host missing") - return .failure(.invalidTarget) - } + let payload = Self.prefixedTranscript(transcript) + let options = self.parseCommandTemplate(config.commandTemplate) + let thinking = options.thinking ?? "default" - let userHost = parsed.user.map { "\($0)@\(parsed.host)" } ?? parsed.host + let result = await AgentRPC.shared.send( + text: payload, + thinking: thinking, + session: options.session, + deliver: options.deliver, + to: options.to) - var args: [String] = [ - "-o", "BatchMode=yes", - "-o", "IdentitiesOnly=yes", - ] - if parsed.port > 0 { args.append(contentsOf: ["-p", String(parsed.port)]) } - if !config.identityPath.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { - let identity = config.identityPath.trimmingCharacters(in: .whitespacesAndNewlines) - if !FileManager.default.fileExists(atPath: identity) { - self.logger.error("voice wake forward identity missing: \(identity, privacy: .public)") - return .failure(.launchFailed("identity not found: \(identity)")) - } - args.append(contentsOf: ["-i", identity]) - } - args.append(userHost) - - // Avoid stdin and globbing entirely: marshal the transcript as a single-quoted literal. - // `shellEscape` keeps it POSIX-safe for /bin/sh even when the text has quotes/parentheses. - let escaped = Self.shellEscape(transcript) - let templated: String = config.commandTemplate.contains("${text}") - ? config.commandTemplate.replacingOccurrences(of: "${text}", with: "$CLAW_TEXT") - : Self.renderedCommand(template: config.commandTemplate, transcript: transcript) - let script = self.commandWithCliPath("CLAW_TEXT=\(escaped); \(templated)", target: destination) - args.append(contentsOf: ["/bin/sh", "-c", script]) - - let debugCmd = (["/usr/bin/ssh"] + args).joined(separator: " ") - self.logger.info("voice wake ssh cmd=\(debugCmd, privacy: .public)") - - self.logger.info("voice wake forward starting host=\(userHost, privacy: .public)") - - let process = Process() - process.executableURL = URL(fileURLWithPath: "/usr/bin/ssh") - process.arguments = args - - let output = Pipe() - process.standardOutput = output - process.standardError = output - - do { - try process.run() - } catch { - self.logger.error("voice wake forward failed to start ssh: \(error.localizedDescription, privacy: .public)") - return .failure(.launchFailed(error.localizedDescription)) - } - - let out = await self.wait(process, timeout: config.timeout) - if process.terminationStatus == 0 { - self.logger.info("voice wake forward ok host=\(userHost, privacy: .public)") + if result.ok { + self.logger.info("voice wake forward ok") return .success(()) } - // surface the failure instead of being silent - let clipped = out.isEmpty ? "(no output)" : String(out.prefix(240)) - self.logger.error( - "voice wake forward failed exit=\(process.terminationStatus) host=\(userHost, privacy: .public) out=\(clipped, privacy: .public) cmd=\(debugCmd, privacy: .public)") - if process.terminationStatus == 126 || process.terminationStatus == 127 { - return .failure(.cliMissingOrFailed(process.terminationStatus, out)) - } - return .failure(.nonZeroExit(process.terminationStatus, out)) + let message = result.error ?? "agent rpc unavailable" + self.logger.error("voice wake forward failed: \(message, privacy: .public)") + return .failure(.rpcFailed(message)) } static func checkConnection(config: VoiceWakeForwardConfig) async -> Result { - let destination = self.sanitizedTarget(config.target) - guard let parsed = self.parse(target: destination) else { - return .failure(.invalidTarget) - } - - let userHost = parsed.user.map { "\($0)@\(parsed.host)" } ?? parsed.host - - var baseArgs: [String] = [ - "-o", "BatchMode=yes", - "-o", "IdentitiesOnly=yes", - "-o", "ConnectTimeout=4", - ] - if parsed.port > 0 { baseArgs.append(contentsOf: ["-p", String(parsed.port)]) } - if !config.identityPath.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { - baseArgs.append(contentsOf: ["-i", config.identityPath]) - } - - // Stage 1: plain SSH connectivity. - var args = baseArgs - args.append(contentsOf: [userHost, "true"]) - - let process = Process() - process.executableURL = URL(fileURLWithPath: "/usr/bin/ssh") - process.arguments = args - let pipe = Pipe() - process.standardError = pipe - process.standardOutput = pipe - - do { - try process.run() - } catch { - return .failure(.launchFailed(error.localizedDescription)) - } - - let output = await self.wait(process, timeout: 6, capturing: pipe) - if process.terminationStatus != 0 { - return .failure(.nonZeroExit(process.terminationStatus, output)) - } - - // Stage 2: ensure remote clawdis-mac is present and responsive. - var checkArgs = baseArgs - let statusCommand = self.commandWithCliPath("clawdis-mac status", target: destination, echoCliPath: true) - checkArgs.append(contentsOf: [userHost, "/bin/sh", "-c", statusCommand]) - let checkProc = Process() - checkProc.executableURL = URL(fileURLWithPath: "/usr/bin/ssh") - checkProc.arguments = checkArgs - let checkPipe = Pipe() - checkProc.standardOutput = checkPipe - checkProc.standardError = checkPipe - do { - try checkProc.run() - } catch { - return .failure(.launchFailed(error.localizedDescription)) - } - let statusOut = await self.wait(checkProc, timeout: 6, capturing: checkPipe) - if checkProc.terminationStatus == 0 { - if let cliLine = statusOut - .split(separator: "\n") - .last(where: { $0.hasPrefix("__CLI:") }) - { - let path = String(cliLine.dropFirst("__CLI:".count)) - if !path.isEmpty { - self.cliCache.set((target: destination, path: path)) - } - } - return .success(()) - } - return .failure(.cliMissingOrFailed(checkProc.terminationStatus, statusOut)) - } - - static func renderedCommand(template: String, transcript: String) -> String { - let escaped = Self.shellEscape(transcript) - if template.contains("${text}") { - return template.replacingOccurrences(of: "${text}", with: escaped) - } - return template + guard config.enabled else { return .failure(.disabled) } + guard !self.sanitizedTarget(config.target).isEmpty else { return .failure(.invalidTarget) } + let status = await AgentRPC.shared.status() + if status.ok { return .success(()) } + return .failure(.rpcFailed(status.error ?? "agent rpc unreachable")) } static func shellEscape(_ text: String) -> String { @@ -281,33 +84,6 @@ enum VoiceWakeForwarder { return "'\(replaced)'" } - private static func wait(_ process: Process, timeout: TimeInterval, capturing pipe: Pipe? = nil) async -> String { - await withTaskGroup(of: Void.self) { group in - group.addTask { - process.waitUntilExit() - } - group.addTask { - let nanos = UInt64(max(timeout, 0.1) * 1_000_000_000) - try? await Task.sleep(nanoseconds: nanos) - if process.isRunning { - // SIGTERM is enough to stop ssh; keeps stdout/stderr readable for diagnostics. - process.terminate() - } - } - _ = await group.next() - group.cancelAll() - } - - let data = try? pipe?.fileHandleForReading.readToEnd() - let text = data.flatMap { String(data: $0, encoding: .utf8) }? - .trimmingCharacters(in: .whitespacesAndNewlines) ?? "" - - if process.terminationStatus != 0 { - self.logger.debug("voice wake forward ssh exit=\(process.terminationStatus) out=\(text, privacy: .public)") - } - return text - } - static func parse(target: String) -> (user: String?, host: String, port: Int)? { guard !target.isEmpty else { return nil } var remainder = target @@ -343,4 +119,47 @@ enum VoiceWakeForwarder { } return trimmed } + + // MARK: - Template parsing + + struct ForwardOptions { + var session: String = "main" + var thinking: String? = "low" + var deliver: Bool = true + var to: String? + } + + private static func parseCommandTemplate(_ template: String) -> ForwardOptions { + var options = ForwardOptions() + let parts = template.split(whereSeparator: { $0.isWhitespace }).map(String.init) + var idx = 0 + while idx < parts.count { + let part = parts[idx] + switch part { + case "--session", "--session-id": + if idx + 1 < parts.count { options.session = parts[idx + 1] } + idx += 1 + case "--thinking": + if idx + 1 < parts.count { options.thinking = parts[idx + 1] } + idx += 1 + case "--deliver": + options.deliver = true + case "--no-deliver": + options.deliver = false + case "--to": + if idx + 1 < parts.count { options.to = parts[idx + 1] } + idx += 1 + default: + break + } + idx += 1 + } + return options + } + + #if DEBUG + static func _testParseCommandTemplate(_ template: String) -> ForwardOptions { + self.parseCommandTemplate(template) + } + #endif } diff --git a/apps/macos/Tests/ClawdisIPCTests/VoiceWakeForwarderTests.swift b/apps/macos/Tests/ClawdisIPCTests/VoiceWakeForwarderTests.swift index 86ad1d4db..8fb0b95c9 100644 --- a/apps/macos/Tests/ClawdisIPCTests/VoiceWakeForwarderTests.swift +++ b/apps/macos/Tests/ClawdisIPCTests/VoiceWakeForwarderTests.swift @@ -16,54 +16,6 @@ import Testing #expect(parsed?.port == defaultVoiceWakeForwardPort) } - @Test func renderedCommandReplacesPlaceholderAndEscapes() { - let template = "clawdis-mac agent --message \"${text}\"" - let command = VoiceWakeForwarder.renderedCommand(template: template, transcript: "hi i'm here") - #expect(command.contains("clawdis-mac agent")) - #expect(command.contains("'hi i'\\''m here'")) - #expect(!command.contains("${text}")) - } - - @Test func renderedCommandPassthroughWhenNoPlaceholder() { - let template = "echo noop" - let command = VoiceWakeForwarder.renderedCommand(template: template, transcript: "ignored") - #expect(command == template) - } - - @Test func commandPrefersCliInstallPaths() { - let command = VoiceWakeForwarder.commandWithCliPath("clawdis-mac status", target: "user@host") - #expect(command.contains("PATH=\(cliHelperSearchPaths.joined(separator: ":"))")) - #expect(command.contains("for c in clawdis-mac /usr/local/bin/clawdis-mac /opt/homebrew/bin/clawdis-mac")) - #expect(command.contains("\"$CLI\" status")) - } - - @Test func commandUsesCachedCliForSameTarget() { - VoiceWakeForwarder.clearCliCache(); defer { VoiceWakeForwarder.clearCliCache() } - VoiceWakeForwarder._testSetCliCache(target: "t1", path: "/tmp/custom-cli") - - let command = VoiceWakeForwarder.commandWithCliPath("clawdis-mac status", target: "t1") - - #expect(command.contains("CLI=\"/tmp/custom-cli\"")) - } - - @Test func commandIgnoresCacheForDifferentTarget() { - VoiceWakeForwarder.clearCliCache(); defer { VoiceWakeForwarder.clearCliCache() } - VoiceWakeForwarder._testSetCliCache(target: "t1", path: "/tmp/custom-cli") - - let command = VoiceWakeForwarder.commandWithCliPath("clawdis-mac status", target: "t2") - - #expect(!command.contains("/tmp/custom-cli")) - } - - @Test func clearCliCacheRemovesCachedCli() { - VoiceWakeForwarder._testSetCliCache(target: "t1", path: "/tmp/custom-cli") - VoiceWakeForwarder.clearCliCache(); defer { VoiceWakeForwarder.clearCliCache() } - - let command = VoiceWakeForwarder.commandWithCliPath("clawdis-mac status", target: "t1") - - #expect(!command.contains("/tmp/custom-cli")) - } - @Test func shellEscapeHandlesQuotesAndParens() { let text = "Debug test works (and a funny pun)" let escaped = VoiceWakeForwarder.shellEscape(text) @@ -82,4 +34,13 @@ import Testing #expect(prefixed.contains("My-Mac")) #expect(prefixed.hasSuffix("\n\nhello world")) } + + @Test func parsesCommandTemplateOverrides() { + let opts = VoiceWakeForwarder._testParseCommandTemplate( + "clawdis-mac agent --session alt --thinking high --no-deliver --to +123 --message \"${text}\"") + #expect(opts.session == "alt") + #expect(opts.thinking == "high") + #expect(opts.deliver == false) + #expect(opts.to == "+123") + } }