diff --git a/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.i18n.yaml b/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.i18n.yaml index eacc286081..290b73d6bb 100644 --- a/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.i18n.yaml +++ b/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.i18n.yaml @@ -12,5 +12,5 @@ en: fdf88fdc0141c9d8 zh: a4af8cab104d38d1 /macos-cua-input-holds-last-for-one-turn/migration: - en: 5e45c629483d2fd5 - zh: 3e8be5a85f2c54ef + en: 30b6c3ffc7326ca0 + zh: a66655caa4c5e509 diff --git a/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.md b/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.md index 4a61ebeab8..097b6a2d04 100644 --- a/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.md +++ b/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.md @@ -12,6 +12,8 @@ macOS `sendKeyEvent('down')` holds a key across `run_code` and `cua_run_code` ca ## Migration +MCP does not supply DSH turn boundaries. Explicit macOS holds through `sendKeyEvent` or `toggleMouse` are unavailable there; replace them with complete `pressKey`, `click`, or `drag` actions. System pointer movement remains available. + 1. If a program requires release before it returns, send an explicit `up` in its `finally` block. Otherwise a later program in the same turn can release the held input. 2. Upgrade the macOS package and helper together. A custom `helperPath` must point to a rebuilt helper implementing the new private turn input protocol. 3. Verify a down in one program remains held until an up in a later program, or until the turn ends. If cleanup cannot be confirmed, CUA disables input for that task. Check the desktop before starting a new task. diff --git a/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.zh.md b/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.zh.md index 9221e10c49..3753a2d02c 100644 --- a/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.zh.md +++ b/docs/upgrade-guide/v0.2.2-rc.4/cua-turn-inputs/guide.zh.md @@ -12,6 +12,8 @@ macOS 的 `sendKeyEvent('down')` 可在同一回合内跨 `run_code` 和 `cua_ru ## 迁移 +MCP 不提供 DSH 回合边界。macOS 的 `sendKeyEvent` 和 `toggleMouse` 显式持有在该入口不可用,请改用完整的 `pressKey`、`click` 或 `drag` 动作。系统鼠标移动仍可使用。 + 1. 如果程序需要在返回前释放输入,在 `finally` 中显式发送 `up`。否则可由同一回合中的后续程序释放。 2. 同步升级 macOS 包和 helper。自定义 `helperPath` 必须指向已重建、支持新回合输入私有协议的 helper。 3. 验证前一次程序的 `down` 能保持到后一次程序发送 `up`,或回合结束。清理无法确认时,CUA 会禁用该任务的输入。检查桌面状态后再创建新任务。 diff --git a/packages/experimental/cua-macos/native/Sources/CuaNative/HeldInputs.swift b/packages/experimental/cua-macos/native/Sources/CuaNative/HeldInputs.swift index 6d8373bf5c..ab4fefc77b 100644 --- a/packages/experimental/cua-macos/native/Sources/CuaNative/HeldInputs.swift +++ b/packages/experimental/cua-macos/native/Sources/CuaNative/HeldInputs.swift @@ -161,9 +161,9 @@ final class HeldInputs: @unchecked Sendable { } /// Create and post movement under the same lock as ownership and the saved release point. - func moveSystem(owner: Owner, route: WindowRoute, screen: CGPoint, checkCancellation: () throws -> Void) throws { + func moveSystem(owner: Owner?, route: WindowRoute, screen: CGPoint, checkCancellation: () throws -> Void) throws { try lock.withLock { - try admit(owner) + if let owner { try admit(owner) } let press = pressed.first { if case .systemPointer = $0.mode { return true }; return false } let event: CGEvent if let press { diff --git a/packages/experimental/cua-macos/native/Sources/CuaNative/RequestCoordinator.swift b/packages/experimental/cua-macos/native/Sources/CuaNative/RequestCoordinator.swift index f606fb9740..2effd119e7 100644 --- a/packages/experimental/cua-macos/native/Sources/CuaNative/RequestCoordinator.swift +++ b/packages/experimental/cua-macos/native/Sources/CuaNative/RequestCoordinator.swift @@ -223,7 +223,9 @@ import Darwin case .foreground(let plan): result = try await performForeground(plan, requestID: request.id) case .mouseMove(let route, let point): let latch = cancellation - try scheduler.systemPointer.move(owner: .init(connection: connectionID, scope: params.inputScope(), app: params.string("appHandle")), in: route, at: point, checkCancellation: { try latch.check() }) + let owner: HeldInputs.Owner? = try params.contains("taskId") || params.contains("turn") + ? .init(connection: connectionID, scope: params.inputScope(), app: params.string("appHandle")) : nil + try scheduler.systemPointer.move(owner: owner, in: route, at: point, checkCancellation: { try latch.check() }) result = AXWorker.mouseMoveResult(route: "system-pointer", pointerMoved: true) case .systemButtonDown(let route, let button): try scheduler.systemPointer.down(owner: .init(connection: connectionID, scope: params.inputScope(), app: params.string("appHandle")), in: route, button: button, diff --git a/packages/experimental/cua-macos/native/Sources/CuaNative/SystemPointerInput.swift b/packages/experimental/cua-macos/native/Sources/CuaNative/SystemPointerInput.swift index fac58df0b4..2f9be350ed 100644 --- a/packages/experimental/cua-macos/native/Sources/CuaNative/SystemPointerInput.swift +++ b/packages/experimental/cua-macos/native/Sources/CuaNative/SystemPointerInput.swift @@ -38,7 +38,7 @@ import Foundation try held.pointerDown(owner: owner, route: route, button: button, system: true, down: down, up: up, post: Self.post) } - func move(owner: HeldInputs.Owner, in route: WindowRoute, at point: WindowPoint, checkCancellation: () throws -> Void) throws { + func move(owner: HeldInputs.Owner?, in route: WindowRoute, at point: WindowPoint, checkCancellation: () throws -> Void) throws { try WindowPointerInput.validate(point, in: route) try WindowPointerInput.validateWindow(route) let screen = WindowPointerInput.screenPoint(point, in: route) diff --git a/packages/experimental/cua-macos/tests/provider-lifecycle.spec.ts b/packages/experimental/cua-macos/tests/provider-lifecycle.spec.ts index 34ed3cd629..1957f30f35 100644 --- a/packages/experimental/cua-macos/tests/provider-lifecycle.spec.ts +++ b/packages/experimental/cua-macos/tests/provider-lifecycle.spec.ts @@ -1329,3 +1329,13 @@ it.each([true, false])('uses the declared systemPointer capability for a non-mac expect(native.request).toHaveBeenLastCalledWith('mouseMove', systemPointer ? { ...hoverTarget, useSystemPointer: false } : hoverTarget, expect.any(AbortSignal), expect.any(String)) }) + +it('keeps complete system moves available outside a turn without registering a persistent hold', async () => { + const f = await setup(undefined, false, { requireSystemPointerApproval: false }) + await selectApplication(f) + native.request.mockClear() + expect((await f.execute('mouseMove', physicalHover)).isError).toBe(false) + expect(native.request).toHaveBeenCalledExactlyOnceWith('mouseMove', physicalHover, expect.any(AbortSignal), expect.any(String)) + await f.handle.dispose() + expect(native.cleanup).not.toHaveBeenCalled() +}) diff --git a/packages/experimental/cua-mcp/README.i18n.yaml b/packages/experimental/cua-mcp/README.i18n.yaml index d6b4890821..59b87e135d 100644 --- a/packages/experimental/cua-mcp/README.i18n.yaml +++ b/packages/experimental/cua-mcp/README.i18n.yaml @@ -51,8 +51,8 @@ en: 0b0c63281111993b zh: 45716a56b5a5ad12 /deepseek-ai-dsh-cua-mcp/known-limitations-and-deferred-work: - en: 7d592e6b49063ae8 - zh: 6f58f0e3a00dd2a8 + en: 743cd8c6a8c89453 + zh: 0d85356f5a9537a0 /deepseek-ai-dsh-cua-mcp/known-limitations-and-deferred-work/dev-note: en: e67f633dc6fe7773 zh: 8ec3e73528ddb1a6 diff --git a/packages/experimental/cua-mcp/README.md b/packages/experimental/cua-mcp/README.md index a4c7b4e6cb..d6ea48810f 100644 --- a/packages/experimental/cua-mcp/README.md +++ b/packages/experimental/cua-mcp/README.md @@ -167,6 +167,7 @@ The tool catalog and instructions are stable for one installation. The reference +- macOS explicit `sendKeyEvent` and `toggleMouse` holds require an active DSH turn. MCP programs run as maintenance activities, so these operations are unavailable through MCP. Use complete `pressKey`, `typeText`, `click`, or `drag` actions; system pointer movement remains available. The server cannot infer the client's turn boundaries. - macOS and native Windows Hosts have a provider. Other Hosts, WSL included, list `cua_get_api_reference` and `cua_reset`, which both name the missing provider; a `cua_run_code` call fails as `unknown tool`. - On Windows the [provider's limits](../cua-windows/README.md#known-limitations-and-deferred-work) apply through MCP: it does not launch applications, grants no `Always allow`, and an npm installation needs `helperPath`. - Elicitation uses the 2025 MCP request model; a client that negotiates the 2026-07-28 revision receives an error for consent requests and should use `DSH_CUA_CONSENT=allow`. diff --git a/packages/experimental/cua-mcp/README.zh.md b/packages/experimental/cua-mcp/README.zh.md index 808785398f..98e7bb5a2d 100644 --- a/packages/experimental/cua-mcp/README.zh.md +++ b/packages/experimental/cua-mcp/README.zh.md @@ -167,6 +167,7 @@ patch 还以 `screenshotTarget: { longEdge: 2048, maxBytes: 2097152 }` 挂载该 +- macOS 的 `sendKeyEvent` 和 `toggleMouse` 显式持有要求活动的 DSH 回合。MCP 程序以维护活动执行,因此这些操作在 MCP 中不可用。请使用完整的 `pressKey`、`typeText`、`click` 或 `drag` 动作;系统鼠标移动仍可使用。服务器无法推断客户端的回合边界。 - macOS 与原生 Windows Host 有提供方。其他 Host(包括 WSL)列出 `cua_get_api_reference` 与 `cua_reset`,二者都指出缺少提供方;调用 `cua_run_code` 以 `unknown tool` 失败。 - 在 Windows 上,[提供方的限制](../cua-windows/README.zh.md#known-limitations-and-deferred-work)经 MCP 同样适用:它不启动应用,不提供 `Always allow` 授权,npm 安装需要配置 `helperPath`。 - Elicitation 使用 2025 版 MCP 请求模型;协商到 2026-07-28 修订版的客户端在授权请求时收到错误,应改用 `DSH_CUA_CONSENT=allow`。 diff --git a/packages/experimental/cua-mcp/src/server.ts b/packages/experimental/cua-mcp/src/server.ts index 6d173e7478..3c7f09b4c9 100644 --- a/packages/experimental/cua-mcp/src/server.ts +++ b/packages/experimental/cua-mcp/src/server.ts @@ -95,7 +95,8 @@ const INSTRUCTIONS = 'DeepSeek Harness Computer Use: observe and operate desktop + 'Start every conversation by calling cua_get_api_reference: it returns the complete cua API reference, and programs ' + 'written without it guess the API wrong. Then call cua_run_code with a TypeScript program that uses the global cua ' + 'object; program state persists across calls on this connection. A call may ask the user for application or pointer ' - + 'consent before it proceeds. Call cua_reset to discard program state.' + + 'consent before it proceeds. Call cua_reset to discard program state. ' + + 'On macOS, MCP programs cannot hold keys or mouse buttons with explicit down events. Use pressKey, click, or drag.' const NO_PROVIDER = 'Computer Use is not available on this Host: the cua-mcp profile has no active CUA provider. ' + 'Providers exist for macOS and native Windows; on those Hosts the server\'s stderr names why the provider did not start.' diff --git a/packages/experimental/cua-mcp/tests/composition.spec.ts b/packages/experimental/cua-mcp/tests/composition.spec.ts index 5599fe4017..9aebf1dc0d 100644 --- a/packages/experimental/cua-mcp/tests/composition.spec.ts +++ b/packages/experimental/cua-mcp/tests/composition.spec.ts @@ -108,12 +108,12 @@ async function setup(options: SetupOptions = {}) { cua: { inject: ['computerUse', 'subprocess', 'tools', 'systemPrompt', 'agents'], Config: CuaConfig, apply(owner: Context, config: CuaConfig) { - installCua(owner, config, { helperPath: process.execPath, + installCua(owner, config, { helperPath: process.execPath, turnInputLifecycle: true, createChannel: options.cancellationAcknowledgementMs === undefined ? native.createChannel : delayCancellation(native.createChannel, options.cancellationAcknowledgementMs, () => { native.events.push('cancellation-acknowledged') }), getGuidance: () => 'Fixture CUA guidance: select test.fixture and operate window 7.', - capabilities: { platform: 'macos', methods: ['getCapabilities', 'getApp', 'getState', 'click', 'screenshot'], + capabilities: { platform: 'macos', methods: ['getCapabilities', 'getApp', 'getState', 'click', 'screenshot', 'sendKeyEvent', 'toggleMouse'], appLevelAccessibility: true, keyboard: ['app'], browser: false, coordinateSpace: 'screenshot-pixels' }, permissionParameters: (_method, permission, locale) => ({ permission, locale }), }) @@ -182,6 +182,21 @@ async function setup(options: SetupOptions = {}) { } describe('Computer Use MCP composition', () => { + it('refuses persistent downs outside a turn and still completes clicks through MCP', async () => { + const { client, native, run } = await setup() + expect(client.getInstructions()).toContain('MCP programs cannot hold keys or mouse buttons') + 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")']) { + const result = await run(code) + expect(result.isError).toBe(true) + expect(texts(result).join('\n')).toContain('active agent turn') + } + expect(native.calls.some(call => call.method === 'sendKeyEvent' || call.method === 'toggleMouse')).toBe(false) + expect((await run('await win.click("0")')).isError).not.toBe(true) + expect(native.state.count).toBe(1) + }) + it('serves three tools, returns the API reference on request, and retains program state in the workspace', async () => { const { ctx, native, client, run, adapter, directory } = await setup() expect(client.getInstructions()).toContain('Start every conversation by calling cua_get_api_reference') diff --git a/packages/experimental/cua/src/index.ts b/packages/experimental/cua/src/index.ts index 9f00d7a725..b8ac5bc851 100644 --- a/packages/experimental/cua/src/index.ts +++ b/packages/experimental/cua/src/index.ts @@ -288,10 +288,12 @@ export function installCua(ctx: Context, config: CuaConfig, backend: CuaBackend) else if (useSystemPointer !== undefined) params.useSystemPointer = useSystemPointer if (inputTurns && (method === 'sendKeyEvent' || method === 'toggleMouse' || (method === 'mouseMove' && useSystemPointer))) { const turn = inputTurns.scope(agent) - if (!turn) throw new ToolContextUnavailableError('Directional CUA input requires an active agent turn') - if (method !== 'mouseMove' && params.direction === 'down') turn.track(client) - params.taskId = agent.id - params.turn = turn.turn + if (!turn && method !== 'mouseMove') throw new ToolContextUnavailableError('Directional CUA input requires an active agent turn; external MCP programs can use pressKey, click, and drag') + if (turn) { + if (method !== 'mouseMove' && params.direction === 'down') turn.track(client) + params.taskId = agent.id + params.turn = turn.turn + } } const nativeParams = method === 'findApps' ? { ...params, maxAppMatches: config.maxAppMatches }