diff --git a/packages/experimental/cua-macos/native/Sources/CuaNative/ExternalInputMonitor.swift b/packages/experimental/cua-macos/native/Sources/CuaNative/ExternalInputMonitor.swift index f359366df9..ae56e25a8a 100644 --- a/packages/experimental/cua-macos/native/Sources/CuaNative/ExternalInputMonitor.swift +++ b/packages/experimental/cua-macos/native/Sources/CuaNative/ExternalInputMonitor.swift @@ -46,8 +46,7 @@ import Foundation func receive(_ type: CGEventType, event: CGEvent) { if type == .tapDisabledByTimeout || type == .tapDisabledByUserInput { - // A later window use retries registration. - stop() + if let tap { CGEvent.tapEnable(tap: tap, enable: true) } return } guard event.getIntegerValueField(.eventSourceUnixProcessID) != Int64(getpid()) else { return } @@ -57,7 +56,7 @@ import Foundation guard [.leftMouseDown, .rightMouseDown, .otherMouseDown, .scrollWheel].contains(type) else { return } let rawWindow = event.getIntegerValueField(.mouseEventWindowUnderMousePointerThatCanHandleThisEvent) guard let id = UInt32(exactly: rawWindow), id > 0, ownerOfWindow(id) == pid else { return } - activity.record(ActivityTarget(process: process, windowID: id), owner: nil, at: event.timestamp) + activity.recordExternal(ActivityTarget(process: process, windowID: id), eventUptime: event.timestamp) } nonisolated static func windowOwner(_ id: UInt32) -> Int32? { diff --git a/packages/experimental/cua-macos/native/Sources/CuaNative/RecentActivity.swift b/packages/experimental/cua-macos/native/Sources/CuaNative/RecentActivity.swift index 8a555639e1..0d5d40ac3e 100644 --- a/packages/experimental/cua-macos/native/Sources/CuaNative/RecentActivity.swift +++ b/packages/experimental/cua-macos/native/Sources/CuaNative/RecentActivity.swift @@ -1,6 +1,6 @@ // Window interests and input metadata belong to the observing helper connection for one turn. import Foundation -import Dispatch +import Darwin struct ActivityTarget: Hashable, Sendable { let process: ProcessIdentity @@ -22,8 +22,20 @@ struct ActivityTarget: Hashable, Sendable { } private var clients: [UUID: Client] = [:] private let now: () -> UInt64 + private let uptime: () -> UInt64 - init(now: @escaping () -> UInt64 = { DispatchTime.now().uptimeNanoseconds }) { self.now = now } + init(now: @escaping () -> UInt64 = { clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) }, + uptime: @escaping () -> UInt64 = { clock_gettime_nsec_np(CLOCK_UPTIME_RAW) }) { + self.now = now + self.uptime = uptime + } + /// Quartz timestamps exclude sleep. Preserve their delivery delay on the clock used for expiry. + func recordExternal(_ target: ActivityTarget, eventUptime: UInt64) { + let awake = uptime() + let current = now() + guard awake >= eventUptime, current >= awake - eventUptime else { return } + record(target, owner: nil, at: current - (awake - eventUptime)) + } func connect(_ owner: UUID, lookbackMs: Int) { clients[owner] = Client(lookback: UInt64(lookbackMs) * 1_000_000) } var hasInterests: Bool { clients.values.contains { $0.windows.values.contains(where: \.armed) } } func disconnect(_ owner: UUID) { clients.removeValue(forKey: owner) } diff --git a/packages/experimental/cua-macos/native/tests/swift/RecentActivityTests.swift b/packages/experimental/cua-macos/native/tests/swift/RecentActivityTests.swift index a5975dbcb0..0e97cb8902 100644 --- a/packages/experimental/cua-macos/native/tests/swift/RecentActivityTests.swift +++ b/packages/experimental/cua-macos/native/tests/swift/RecentActivityTests.swift @@ -88,7 +88,7 @@ private let second: UInt64 = 1_000_000_000 @Test @MainActor func externalInputNeedsAWindowAndANonHelperSource() throws { let owner = UUID() var time = second - let history = RecentActivity(now: { time }) + let history = RecentActivity(now: { time }, uptime: { time }) let target = ActivityTarget(process: try NativePlatform.identity(getpid()), windowID: 10) history.connect(owner, lookbackMs: 60_000) let observed = history.observe(target, handle: "app", owner: owner) @@ -111,3 +111,17 @@ private let second: UInt64 = 1_000_000_000 .object(["source": .string("external-input"), "windowId": .string("10"), "ageSeconds": .number(0)]), ]) } + +@Test @MainActor func elapsedTimeIncludesSleepAfterExternalInput() { + var awake = second + var continuous = 5 * second + let history = RecentActivity(now: { continuous }, uptime: { awake }) + let owner = UUID() + history.connect(owner, lookbackMs: 60_000) + let observed = history.observe(target, handle: "app", owner: owner) + awake += second + continuous += second + history.recordExternal(target, eventUptime: awake) + continuous += 3_600 * second + #expect(history.take(owner: owner, handle: "app", observations: ["10": observed]).isEmpty) +} diff --git a/packages/experimental/cua-macos/native/upstream.json b/packages/experimental/cua-macos/native/upstream.json index a8e4032c1c..36fdf03b81 100644 --- a/packages/experimental/cua-macos/native/upstream.json +++ b/packages/experimental/cua-macos/native/upstream.json @@ -13,7 +13,7 @@ { "local": "Sources/CuaNative/WindowPointerInput.swift", "upstream": "libs/cua-driver/rust/crates/platform-macos/src/input/mouse.rs", - "changes": "Reimplement the exposed window-directed click, drag and line-scroll paths in Swift. Preserve routing fields and gesture timing, post each event once, and release this operation\u2019s held button on cooperative cancellation. Preserve fractional movement delays for short or unevenly divided drags. Support middle-button and modifier-bearing clicks and configurable-button drags. Add unpressed hover with no cursor restoration and omit off-window priming clicks so revealed controls remain interactive." + "changes": "Reimplement the exposed window-directed click, drag and line-scroll paths in Swift. Preserve routing fields and gesture timing, post each event once, and release this operation’s held button on cooperative cancellation. Preserve fractional movement delays for short or unevenly divided drags. Support middle-button and modifier-bearing clicks and configurable-button drags. Add unpressed hover with no cursor restoration and omit off-window priming clicks so revealed controls remain interactive." }, { "local": "Sources/CuaNative/PIDEventPoster.swift", diff --git a/packages/experimental/cua/README.i18n.yaml b/packages/experimental/cua/README.i18n.yaml index 476c464441..be391f63d7 100644 --- a/packages/experimental/cua/README.i18n.yaml +++ b/packages/experimental/cua/README.i18n.yaml @@ -22,7 +22,7 @@ zh: 515aeb5de6ae5b33 /deepseek-ai-dsh-cua/understand-the-implementation: en: fa68a353d7a94703 - zh: c8df024599f34980 + zh: 432b2e6823e5eb7d /deepseek-ai-dsh-cua/further-exploration: en: c174fb5a00ea3681 zh: 039340042676881d diff --git a/packages/experimental/cua/README.zh.md b/packages/experimental/cua/README.zh.md index dfcda19937..0df119f7d7 100644 --- a/packages/experimental/cua/README.zh.md +++ b/packages/experimental/cua/README.zh.md @@ -99,7 +99,7 @@ Computer Use 详情页的权限卡片、组件列表和来源信息使用 l3 边 权限 Remote 还通过 `ptcConfiguration()` 和 `enablePersistentPtc()` 支持详情页的执行方式设置。查询读取现有执行器和可编辑的 profile 条目。显式写入从 Node 提供方的 `resolvePersistentExecution` 辅助函数获取执行设置,再用 `configEditor.edit()` 保留其他字段并按正常 Loader 规则重载;已是持久化模式的执行器保持不变。CUA 将 Node 提供方声明为 peer 依赖,使这些设置与已安装的提供方版本一致。此设置独立于系统权限和应用授权。 -提供方将准备好的 helper、连接工厂、指引回调和启动能力传给 [installCua](src/index.ts)。[NativeClient](src/native-client.ts) 负责请求关联、字节上限、取消确认和失败处理。平台连接器负责启动和连接清理。其带类型的启动失败在请求写入和连接关闭时保留错误码及诊断。共享执行器为每个 Agent 保留一个连接,并在销毁时等待进行中的图像写入。 提供方可通过 `windowRead` 获知已窗口读取所用的应用授权,并在读取成功后接收原生回复。 +提供方将准备好的 helper、连接工厂、指引回调和启动能力传给 [installCua](src/index.ts)。[NativeClient](src/native-client.ts) 负责请求关联、字节上限、取消确认和失败处理。平台连接器负责启动和连接清理。其带类型的启动失败在请求写入和连接关闭时保留错误码及诊断。共享执行器为每个 Agent 保留一个连接,并在销毁时等待进行中的图像写入。提供方可通过 `windowRead` 获取窗口读取所用的授权连接,并在读取成功后接收原生回复。 提供方实现 `CuaBackend.getGuidance(kind)`,其中 `kind: 'raw' | 'standalone'`,分别返回原始 `cua` 调用和 `cua_run_code` 程序的操作指引;两种形态可以返回相同文本。共享 CUA 为两份结果统一补充对应能力的权限指引、配置的 AX 读取默认值和最大点击按住时长。