👾 Open Computer Use – Open-Source Alternative to Codex Computer Use
Swift accessibilityai-agentai-agentsclaude-codecodexcodex-computer-usecomputer-usecomputer-use-agentcross-platformcuadesktop-automationgemini-cligui-automationmacosmcpmodel-context-protocolopen-computer-useopen-computer-use-mcpopenai-codexopencode
Restart stale app agent after rebuilds
Leoff8573c6518a
diff --git a/apps/OpenComputerUse/Sources/OpenComputerUse/MacOSAppAgentProxy.swift b/apps/OpenComputerUse/Sources/OpenComputerUse/MacOSAppAgentProxy.swiftindex c86fdce..51ce21c 100644--- a/apps/OpenComputerUse/Sources/OpenComputerUse/MacOSAppAgentProxy.swift+++ b/apps/OpenComputerUse/Sources/OpenComputerUse/MacOSAppAgentProxy.swift@@ -5,6 +5,7 @@ import OpenComputerUseKitprivate let appAgentCommand = "__open-computer-use-app-agent"private let appAgentDisableEnvironmentKey = "OPEN_COMPUTER_USE_DISABLE_APP_AGENT_PROXY"+private let appAgentProcessStartDate = Date()enum MacOSAppAgentProxy {static func isAgentInvocation(arguments: [String]) -> Bool {@@ -73,14 +74,19 @@ enum MacOSAppAgentProxy {@MainActorprivate static func connectOrLaunchAgent(socketPath: String) throws -> AppAgentSocketClient {- if let client = AppAgentSocketClient.connect(path: socketPath) {- return client+ guard let appURL = PermissionSupport.currentAppBundleURL() else {+ throw OpenComputerUseCLIError(message: "Unable to locate Open Computer Use.app for app-scoped macOS permissions.")}- unlink(socketPath)+ if let client = AppAgentSocketClient.connect(path: socketPath) {+ if (try? client.isCurrentAgent(for: appURL)) == true {+ return client+ }- guard let appURL = PermissionSupport.currentAppBundleURL() else {- throw OpenComputerUseCLIError(message: "Unable to locate Open Computer Use.app for app-scoped macOS permissions.")+ _ = try? client.request(["kind": "terminate"])+ unlink(socketPath)+ } else {+ unlink(socketPath)}let configuration = NSWorkspace.OpenConfiguration()@@ -306,6 +312,18 @@ private final class AppAgentConnection: @unchecked Sendable {}switch kind {+ case "agentInfo":+ return [+ "bundleIdentifier": Bundle.main.bundleIdentifier ?? "",+ "bundleURL": Bundle.main.bundleURL.standardizedFileURL.path,+ "executableURL": Bundle.main.executableURL?.standardizedFileURL.path ?? "",+ "processStartTime": appAgentProcessStartDate.timeIntervalSince1970,+ ]+ case "terminate":+ Task { @MainActor in+ NSApp.terminate(nil)+ }+ return ["ok": true]case "mcp":let line = request["line"] as? String ?? ""if let response = server.handle(line: line) {@@ -451,6 +469,42 @@ private final class AppAgentSocketClient: @unchecked Sendable {return response}++ func isCurrentAgent(for appURL: URL) throws -> Bool {+ let response = try request(["kind": "agentInfo"])+ let expectedBundleURL = appURL.standardizedFileURL++ guard response["bundleURL"] as? String == expectedBundleURL.path else {+ return false+ }++ guard let processStartTime = response["processStartTime"] as? TimeInterval else {+ return false+ }++ guard let executableURL = executableURL(for: expectedBundleURL),+ let modifiedAt = try? executableURL.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate+ else {+ return true+ }++ return processStartTime + 0.5 >= modifiedAt.timeIntervalSince1970+ }++ private func executableURL(for appURL: URL) -> URL? {+ guard let bundle = Bundle(url: appURL),+ let executableName = bundle.object(forInfoDictionaryKey: kCFBundleExecutableKey as String) as? String,+ !executableName.isEmpty+ else {+ return nil+ }++ return appURL+ .appendingPathComponent("Contents", isDirectory: true)+ .appendingPathComponent("MacOS", isDirectory: true)+ .appendingPathComponent(executableName)+ .standardizedFileURL+ }}private func readAgentLine(_ file: UnsafeMutablePointer<FILE>) -> String? {diff --git a/docs/histories/2026-05/20260507-2115-restart-stale-app-agent.md b/docs/histories/2026-05/20260507-2115-restart-stale-app-agent.mdnew file mode 100644index 0000000..292823d--- /dev/null+++ b/docs/histories/2026-05/20260507-2115-restart-stale-app-agent.md@@ -0,0 +1,29 @@+# 自动替换过期 app-agent++## 用户诉求++继续优化 `open-computer-use` 与官方工具的一致性,并减少每次构建后需要手动重启隐藏 app-agent 的情况。++## 主要改动++- app-agent socket 新增 `agentInfo` 请求,返回当前 bundle、executable 和 agent 进程启动时间。+- CLI proxy 连接已有 socket 时,会校验 agent 是否来自当前 bundle,并且启动时间是否晚于当前 executable 修改时间。+- 如果已有 agent 过期或不支持 `agentInfo`,proxy 会丢弃旧 socket 并拉起新的 Dev app-agent。+- 新 agent 支持 `terminate` 请求,后续替换时可优雅退出。++## 设计动机++本地 debug 构建会原地替换 `dist/Open Computer Use (Dev).app`,但旧的隐藏 app-agent 可能继续持有旧代码,导致 Codex 重启后仍拿到过期 tool 行为。用 agent 自报信息和 executable mtime 做一次轻量握手,可以让代理在连接前自动发现并替换 stale agent。++## 验证++- `swift test`+- `./scripts/build-open-computer-use-app.sh debug`+- `./scripts/run-tool-smoke-tests.sh`+- `./scripts/check-docs.sh`+- `git diff --check`+- 普通代理路径 `call list_apps` 验证可自动拉起新 agent,并输出 `frontmost`++## 受影响文件++- `apps/OpenComputerUse/Sources/OpenComputerUse/MacOSAppAgentProxy.swift`