Keep CUA capability methods independent of turn state

This commit is contained in:
creatixchu committed 2026-11-18 18:38:56 +08:00
1 parent 52e966b3d8
commit 1feef12e61
9 files changed
+17 -17

No files matched your search

@@ -9,11 +9,11 @@
en: f72db0b79aa02fb5
zh: b91c1f177dc93ab4
/agent-note-cua-scoped-surfaces-and-separated-input-capabilities/decision:
en: b4814d1d835a27d0
zh: d398a39c2d75756f
en: 41fd533d5be7642b
zh: c6fcbabec22bdbb7
/agent-note-cua-scoped-surfaces-and-separated-input-capabilities/alternatives-considered:
en: 32c970ae153e0f6c
zh: 8431be0a10ffed15
/agent-note-cua-scoped-surfaces-and-separated-input-capabilities/consequences:
en: 3a12a64b45e17f51
zh: 28627d190a21b5b9
en: d814ec2360363bc5
zh: 3249ec743514f81b
@@ -12,7 +12,7 @@ Application and window objects share accessibility operations, but only an expli
`CuaSurface` declares operations shared by application and window objects; `CuaApp` and `CuaWindow` extend it with their own operations. One base factory builds the surface, and the derived factories inject scope parameters plus `requireAccessibility` and `requireKeyboard` checks. The shared base never branches on a capability that exists only for applications.
Provider startup capabilities are static. Platform, supported methods, application-level accessibility, keyboard scopes, browser support, and coordinate space are fixed when the provider is installed. The native `getCapabilities` RPC reports runtime permissions and session lock only; `installCua` merges the two layers for the model-visible result and omits turn-managed hold methods when the caller has no active turn. The neutral RPC is a common operation representation, not a capability inventory; provider guidance names the actions its platform supports.
Provider startup capabilities are static. Platform, supported methods, application-level accessibility, keyboard scopes, browser support, and coordinate space are fixed when the provider is installed. The native `getCapabilities` RPC reports runtime permissions and session lock only; `installCua` merges the two layers for the model-visible result. The neutral RPC is a common operation representation, not a capability inventory; provider guidance names the actions its platform supports.
Keyboard input keeps keys and modifiers separate: `pressKey(key, modifiers?)` presses and releases one key while holding the listed modifiers, and its keyboard modifier list is a plain string array defined by the provider. `sendKeyEvent(key, direction)` sends exactly one key-down or key-up without an implicit counterpart. A caller can therefore hold a modifier or split a gesture; neither method packs a chord string, and the shared schema does not enumerate platform key names.
@@ -34,6 +34,6 @@ Within one API instance, the object facade keeps one current accessibility snaps
Element IDs belong to the application's current accessibility snapshot. Reading one window replaces the facade’s current snapshot for that application, so callers observe again and confirm the IDs they use. The new read does not guarantee that an old string is rejected or that an ID changes; callers must not assume an older ID still identifies the same control. Pointer input and screenshots require an explicit window object; callers enumerate windows first.
Keyboard behavior follows the provider's supported scopes. A caller that uses `sendKeyEvent` controls its down/up pairing. macOS holds explicit downs across programs within an active turn and releases remaining keys and mouse buttons together when the turn completes, fails, or is stopped. Keyboard modifier names are provider-defined, so the same string may have different support across providers.
Keyboard behavior follows the provider's supported scopes. A caller that uses `sendKeyEvent` controls its down/up pairing. The shared CUA hold scope is the active turn: explicit downs persist across programs, and cleanup after turn/end releases remaining keys and mouse buttons together. macOS implements this lifecycle; Windows implementation is pending. Keyboard modifier names are provider-defined, so the same string may have different support across providers.
Provider guidance owns the list of supported actions. Adding a method to the shared schema does not claim that every provider implements it, and the merged `getCapabilities` result remains the runtime source for permissions and session lock.
@@ -12,7 +12,7 @@ Status: implemented
`CuaSurface` 声明应用对象与窗口对象共享的操作;`CuaApp` 与 `CuaWindow` 通过扩展它增加各自的操作。一个基类工厂负责构建表面,派生工厂注入作用域参数以及 `requireAccessibility`、`requireKeyboard` 检查。共享基类从不为只属于应用的能力分支。
Provider 的启动能力是静态的。平台、支持的方法、应用级辅助功能、键盘作用域、浏览器支持和坐标空间在安装 provider 时固定。原生 `getCapabilities` RPC 只报告运行时权限和会话锁定状态;`installCua` 为模型可见结果合并两层,并在调用方没有活动回合时省略按回合管理的持有方法。中立的 RPC 是通用操作表示,不是能力清单;provider 指导文本说明其平台支持的动作。
Provider 的启动能力是静态的。平台、支持的方法、应用级辅助功能、键盘作用域、浏览器支持和坐标空间在安装 provider 时固定。原生 `getCapabilities` RPC 只报告运行时权限和会话锁定状态;`installCua` 为模型可见结果合并两层。中立的 RPC 是通用操作表示,不是能力清单;provider 指导文本说明其平台支持的动作。
键盘输入把按键与修饰键分开:`pressKey(key, modifiers?)` 在按住所列修饰键时按下并释放一个键,其键盘修饰键列表是 provider 定义的普通字符串数组。`sendKeyEvent(key, direction)` 只发送一次按下或抬起,不隐式发送相反事件。因此调用方可以保持修饰键或拆分手势;两个方法都不接受组合键字符串,共享 schema 也不枚举平台按键名。
@@ -34,6 +34,6 @@ Provider 的启动能力是静态的。平台、支持的方法、应用级辅
元素 ID 属于应用当前的辅助功能快照。读取一个窗口会替换门面对该应用的当前快照,因此调用方需要重新观察并确认要使用的 ID。新的读取不保证拒绝旧字符串,也不保证 ID 会改变;调用方不得假定旧 ID 仍指向同一控件。指针输入和截图都要求显式的窗口对象;调用方先枚举窗口。
键盘行为遵循 provider 支持的作用域。使用 `sendKeyEvent` 的调用方控制按下与抬起的配对。在 macOS 上,显式按下可在活动回合内跨程序保持;回合完成、失败或停止时统一释放剩余键盘键和鼠标按钮。键盘修饰键名称由 provider 定义,同一字符串在不同 provider 上可能支持不同。
键盘行为遵循 provider 支持的作用域。使用 `sendKeyEvent` 的调用方控制按下与抬起的配对。共通 CUA 的持有范围是活动回合:显式按下跨程序保持,turn/end 之后统一清理剩余键盘键和鼠标按钮。macOS 已实现这套生命周期,Windows 待实现。键盘修饰键名称由 provider 定义,同一字符串在不同 provider 上可能支持不同。
Provider 指导文本负责支持动作的清单。把方法加入共享 schema 并不表示每个 provider 都已实现它;合并后的 `getCapabilities` 结果仍是权限和会话锁定状态的运行时来源。
@@ -336,7 +336,7 @@ it('correlates a native request with its logged tool invocation', async () => {
expect(native.request).toHaveBeenCalledExactlyOnceWith('listApps', {}, expect.any(AbortSignal), brandString<ToolCallId>('lifecycle-1'))
})
it('merges startup capabilities into runtime native fields', async () => {
it('reports declared methods outside a turn while merging runtime native fields', async () => {
const f = await setup()
const runtime = { permissions: { accessibility: true, screenRecording: false }, sessionLocked: false }
native.request.mockResolvedValueOnce(runtime)
@@ -346,7 +346,7 @@ it('merges startup capabilities into runtime native fields', async () => {
coordinateSpace: 'screenshot-pixels', methods: [
'activate', 'click', 'drag', 'findApps', 'getApp', 'getCapabilities', 'getState', 'invoke',
'listActions', 'listApps', 'listWindows', 'mouseMove', 'paste', 'performAction', 'pressKey', 'screenshot', 'scroll',
'selectText', 'setValue', 'show', 'typeText',
'selectText', 'sendKeyEvent', 'setValue', 'show', 'toggleMouse', 'typeText',
] })
expect(native.request).toHaveBeenCalledExactlyOnceWith('getCapabilities', {}, expect.any(AbortSignal), brandString<ToolCallId>('lifecycle-1'))
})
@@ -186,7 +186,8 @@ describe('Computer Use MCP composition', () => {
const { client, native, run } = await setup()
expect(client.getInstructions()).toContain('MCP programs cannot hold keys or mouse buttons')
const capabilities = await run('inspect(JSON.stringify((await cua.getCapabilities()).methods))')
expect(texts(capabilities).join('')).not.toMatch(/sendKeyEvent|toggleMouse/)
expect(texts(capabilities).join('')).toContain('sendKeyEvent')
expect(texts(capabilities).join('')).toContain('toggleMouse')
const selected = await run('var app = await cua.getApp("test.fixture"); var win = await app.getWindow("7"); await win.getAccessibilityTree()')
expect(selected.isError).not.toBe(true)
for (const code of ['await app.sendKeyEvent("w", "down")', 'await win.toggleMouse("left", "down")']) {
+2 -2
View File
@@ -21,8 +21,8 @@
en: 3f638a4a76023fc5
zh: 515aeb5de6ae5b33
/deepseek-ai-dsh-cua/understand-the-implementation:
en: a18beb69352e7635
zh: 6bfe96f036840427
en: 92c7e05fc2066d8f
zh: c8b1484f4dcd79c3
/deepseek-ai-dsh-cua/further-exploration:
en: c174fb5a00ea3681
zh: 039340042676881d
+1 -1
View File
@@ -89,7 +89,7 @@ Permission responses identify their platform and report only the state that plat
<details>
<summary>Shared execution and platform ownership</summary>
The shared CUA lifecycle uses one turn as the hold scope for explicit keyboard and mouse downs. Platform guidance describes implementation support, not a different hold scope. A provider enables `CuaBackend.turnInputLifecycle` only after its helper accepts `taskId` and `turn` on `sendKeyEvent`, `toggleMouse`, and system `mouseMove`, and acknowledges `releaseHeldInputs({ taskId, turn })`. Provider unload retains lifecycle listeners until program calls and native cleanup have settled. The shared owner starts cleanup from `turn/end`, closes input admission, and awaits native cancellation before independent bounded cleanup. It retains the completion promise through existing Agent maintenance and pre-step waits; `turn/end` and idle notifications alone do not confirm release. An unconfirmed release reports `agent/error` and disables all CUA operations for the task. Providers must record actual dispatch order, targets and routes before posting, implement explicit up and mixed reverse-order cleanup, reject stale turns, and clean only their connection on disconnect. For these providers, `getCapabilities().methods` omits `sendKeyEvent` and `toggleMouse` outside an active turn. Windows leaves this option unset until these native behaviors are implemented; it receives neither new parameters nor cleanup commands.
The shared CUA lifecycle uses one turn as the hold scope for explicit keyboard and mouse downs. Platform guidance describes implementation support, not a different hold scope. A provider enables `CuaBackend.turnInputLifecycle` only after its helper accepts `taskId` and `turn` on `sendKeyEvent`, `toggleMouse`, and system `mouseMove`, and acknowledges `releaseHeldInputs({ taskId, turn })`. Provider unload retains lifecycle listeners until program calls and native cleanup have settled. The shared owner starts cleanup from `turn/end`, closes input admission, and awaits native cancellation before independent bounded cleanup. It retains the completion promise through existing Agent maintenance and pre-step waits; `turn/end` and idle notifications alone do not confirm release. An unconfirmed release reports `agent/error` and disables all CUA operations for the task. Providers must record actual dispatch order, targets and routes before posting, implement explicit up and mixed reverse-order cleanup, reject stale turns, and clean only their connection on disconnect. `getCapabilities().methods` reports the provider’s declared methods independently of turn state; explicit hold operations require an active turn when dispatched. Windows leaves this option unset until these native behaviors are implemented; it receives neither new parameters nor cleanup commands.
The [permission store](src/permission-store.ts) uses `storageDomain` with the configured backend. The default JSON backend writes `$DSH_HOME/storages/cua_permissions_<sha256>.json`, where the digest identifies the canonical profile directory. Each authorization check opens fresh state under the profile's file lock and closes its domain afterward; native dispatch uses a separate application lock. Revocation waits for operations already admitted by another process; after it completes, every process rejects old handles. Files on different machines or PID namespaces must not share this lock.
+1 -1
View File
@@ -89,7 +89,7 @@ Computer Use 详情页的权限卡片、组件列表和来源信息使用 l3 边
<details>
<summary>共享执行与平台职责</summary>
共通 CUA 生命周期以一个回合作为显式键盘和鼠标按下的持有范围。平台指导说明实现支持情况,不定义另一套持有范围。提供方仅在 helper 已接受 `sendKeyEvent`、`toggleMouse` 和系统 `mouseMove` 的 `taskId`、`turn` 参数,并能确认 `releaseHeldInputs({ taskId, turn })` 后,才启用 `CuaBackend.turnInputLifecycle`。提供方卸载时保留生命周期监听器,直到程序调用和原生清理都已结束。共通持有方从 `turn/end` 开始清理,禁止新输入,等待原生取消后用独立且有超时限制的请求释放。它通过现有的 Agent maintenance 和 pre-step 等待保留清理完成结果;单独的 `turn/end` 或空闲通知不表示释放已经确认。释放未经确认时报告 `agent/error`,并禁用该任务的全部 CUA 操作。提供方必须在派发前记录实际顺序、目标与输入方式,实现显式释放和键鼠混合反序清理,拒绝旧回合输入,并在断连时只清理本连接。这些提供方在没有活动回合时,从 `getCapabilities().methods` 中省略 `sendKeyEvent` 和 `toggleMouse`。Windows 在实现这些原生行为前保持此选项未设置,不接收新增参数或清理命令。
共通 CUA 生命周期以一个回合作为显式键盘和鼠标按下的持有范围。平台指导说明实现支持情况,不定义另一套持有范围。提供方仅在 helper 已接受 `sendKeyEvent`、`toggleMouse` 和系统 `mouseMove` 的 `taskId`、`turn` 参数,并能确认 `releaseHeldInputs({ taskId, turn })` 后,才启用 `CuaBackend.turnInputLifecycle`。提供方卸载时保留生命周期监听器,直到程序调用和原生清理都已结束。共通持有方从 `turn/end` 开始清理,禁止新输入,等待原生取消后用独立且有超时限制的请求释放。它通过现有的 Agent maintenance 和 pre-step 等待保留清理完成结果;单独的 `turn/end` 或空闲通知不表示释放已经确认。释放未经确认时报告 `agent/error`,并禁用该任务的全部 CUA 操作。提供方必须在派发前记录实际顺序、目标与输入方式,实现显式释放和键鼠混合反序清理,拒绝旧回合输入,并在断连时只清理本连接。`getCapabilities().methods` 报告提供方声明的方法,不随回合状态变化;显式持有操作在派发时要求活动回合。Windows 在实现这些原生行为前保持此选项未设置,不接收新增参数或清理命令。
[授权存储](src/permission-store.ts)复用 `storageDomain` 和配置的后端。默认 JSON 后端写入 `$DSH_HOME/storages/cua_permissions_<sha256>.json`,摘要对应 profile 的真实目录。每次授权检查在 profile 文件锁内打开并读取最新状态,随后关闭存储域;原生操作使用单独的应用锁。撤销会等待其他进程已获准执行的操作结束;撤销完成后,各进程都拒绝旧句柄。不同电脑或 PID 命名空间不能共享这把锁。
+1 -2
View File
@@ -363,11 +363,10 @@ export function installCua(ctx: Context, config: CuaConfig, backend: CuaBackend)
if (method === 'getCapabilities') {
const runtime = z.record(z.string(), z.json()).parse(result)
const capabilities = backend.capabilities
const canHoldInput = !inputTurns || inputTurns.scope(agent) !== undefined
return {
...runtime,
platform: capabilities.platform, systemPointer,
methods: capabilities.methods.filter(method => canHoldInput || (method !== 'sendKeyEvent' && method !== 'toggleMouse')),
methods: [...capabilities.methods],
appLevelAccessibility: capabilities.appLevelAccessibility,
keyboard: [...capabilities.keyboard],
browser: capabilities.browser,