diff --git a/packages/experimental/cua-windows/README.i18n.yaml b/packages/experimental/cua-windows/README.i18n.yaml index 113cccc1f8..1d4df0039b 100644 --- a/packages/experimental/cua-windows/README.i18n.yaml +++ b/packages/experimental/cua-windows/README.i18n.yaml @@ -36,8 +36,8 @@ en: 38b3250d6ae0f3d6 zh: 33d056c416e11d75 /deepseek-ai-dsh-cua-windows/model-experience/explicit-application-and-window-operations/what-the-model-sees: - en: bf6f99c4c1fafe72 - zh: 791930be8c5f80f4 + en: d1c83b32631c2146 + zh: fff241be17b13885 /deepseek-ai-dsh-cua-windows/model-experience/explicit-application-and-window-operations/token-effect: en: 39eebd53ee226af7 zh: ba7b352cf1e23c3c @@ -45,8 +45,8 @@ en: db3e2957041fafbb zh: 34f953887dff37f7 /deepseek-ai-dsh-cua-windows/known-limitations-and-deferred-work: - en: f36d057acb4e9ea3 - zh: a1a94877e51d9d66 + en: d778c226c2a8f2bd + zh: 27b06182ebf4ff66 /deepseek-ai-dsh-cua-windows/known-limitations-and-deferred-work/dev-note: en: 0a1253c6f7b5e8b9 zh: 9f56528562c7c914 diff --git a/packages/experimental/cua-windows/README.md b/packages/experimental/cua-windows/README.md index 073e9d4056..8bf999d3a6 100644 --- a/packages/experimental/cua-windows/README.md +++ b/packages/experimental/cua-windows/README.md @@ -65,7 +65,7 @@ Each result contains `id`, `displayName`, and `isRunning`. The ID is an AUMID, i #### What the model sees -The program receives application and window identifiers, observations, and action results. Selection alone does not provide an accessibility tree. After a successful program, a reminder lists any held keys and buttons with their window and desktop input mode. Program failure, cancellation, reset and disconnect release the input held by that connection in reverse dispatch order. +The program receives application and window identifiers, observations, and action results. Selection alone does not provide an accessibility tree. After a successful program, a reminder lists any held keys and buttons with their window and desktop input mode. While one connection holds a key or button, another connection can use different keys or buttons. Program failure, cancellation, reset and disconnect release the input held by that connection in reverse dispatch order. #### Token effect @@ -83,7 +83,7 @@ Inspected results append to model history. Retaining an App, Window, or returned - Hover, click, scroll and drag use the system pointer. `requireSystemInputApproval` does not prompt separately for Windows pointer operations. - Drag checks the topmost window only at its starting point. An overlay from another application along the path or at the destination can receive the drop even while the selected window stays in the foreground. - Held-input reminders and cleanup use the helper's own dispatches. If you press the same key or button while the helper holds it, its next up can also interrupt your press. -- Windows routes input through the desktop. After focus changes or the original window closes, a confirmed release means the desktop accepted the up. The original window may not receive the event. If the desktop is unavailable or refuses to confirm release, further CUA input for the task is blocked. Inspect the desktop before starting another task. +- Windows routes input through the desktop. After focus changes or the original window closes, a confirmed release means the desktop accepted the up. The original window may not receive the event. Releasing a held input does not check the current foreground window or lock state. The release uses the saved up event and reports `foregroundChanged: null` for an explicit up. If Windows refuses the up during cleanup, further CUA input for the task is blocked. Inspect the desktop before starting another task. - Cleanup of temporary input inside complete actions does not check each key or button release. If an input remains held after a failure, release it manually before continuing. - Helper identity hashes only the exe. Different DLLs with the same apphost can reuse the old service. Stop Hosts using the same output directory before rebuilding to avoid locked files. - The Windows handshake requires a positive `maxClickHoldMs`. With zero, it fails with `INVALID_CONFIG` before ordinary clicks or status queries can run. diff --git a/packages/experimental/cua-windows/README.zh.md b/packages/experimental/cua-windows/README.zh.md index a84c963e1a..8f019d5ee8 100644 --- a/packages/experimental/cua-windows/README.zh.md +++ b/packages/experimental/cua-windows/README.zh.md @@ -65,7 +65,7 @@ kind: "package-reference" #### 模型看到什么 -程序可以读取应用和窗口标识、观察结果及动作结果。仅执行选择操作不会提供辅助功能树。程序成功后,提醒会列出仍按住的键和鼠标按钮、所属窗口和桌面输入方式。程序异常、取消、重置和断连时,按派发顺序的反序释放这条连接持有的输入。 +程序可以读取应用和窗口标识、观察结果及动作结果。仅执行选择操作不会提供辅助功能树。程序成功后,提醒会列出仍按住的键和鼠标按钮、所属窗口和桌面输入方式。一条连接持有按键或鼠标按钮时,其他连接仍可使用不同的键或按钮。程序异常、取消、重置和断连时,按派发顺序的反序释放这条连接持有的输入。 #### Token 影响 @@ -83,7 +83,7 @@ kind: "package-reference" - 悬停、点击、滚动和拖拽使用系统指针。`requireSystemInputApproval` 不会为 Windows 操作弹出单独的指针授权提示。 - 拖拽只检查起点处最上层的窗口。若其他应用遮住路径或终点,即使目标仍在前台,也可能把内容拖到其他应用。 - 持有提醒和清理只依据 helper 自己派发的输入。如果你也按住同一个键或按钮,helper 之后发送的 up 可能打断你的按压。 -- Windows 通过桌面派发输入。焦点变化或原窗口关闭后,确认释放表示桌面接受了 up,原窗口可能收不到该事件。若桌面不可用或无法确认释放,本任务不能继续使用 CUA。请先检查桌面,再开始新任务。 +- Windows 通过桌面派发输入。焦点变化或原窗口关闭后,确认释放表示桌面接受了 up,原窗口可能收不到该事件。释放时直接发送保存的 up 事件,无需检查当前前台窗口或锁屏状态。显式 up 返回 `foregroundChanged: null`。若 Windows 在清理时拒绝 up,本任务不能继续使用 CUA。请先检查桌面,再开始新任务。 - 清理完整动作内的临时输入时,helper 不检查每个按键或鼠标按钮的释放结果。操作失败后如有按键或按钮仍处于按下状态,请先手动释放。 - helper 的身份摘要只覆盖 exe。同一 apphost 搭配不同 DLL 时可能复用旧服务。重新构建前请先关闭使用同一输出目录的 Host,避免文件被占用。 - Windows 握手要求 `maxClickHoldMs` 为正整数。设为 0 会使连接以 `INVALID_CONFIG` 失败,连普通点击和状态查询也不可用。 diff --git a/packages/experimental/cua-windows/native/entry/DesktopExecutor.cs b/packages/experimental/cua-windows/native/entry/DesktopExecutor.cs index 68b5c07029..a374db5376 100644 --- a/packages/experimental/cua-windows/native/entry/DesktopExecutor.cs +++ b/packages/experimental/cua-windows/native/entry/DesktopExecutor.cs @@ -17,12 +17,12 @@ internal sealed class ClientState internal sealed class DesktopExecutor(MtaDispatcher mta, StaDispatcher sta) : IAsyncDisposable { - private readonly HeldInputs inputs = new(input => Input.Send(input), DesktopSession.RequireInteractive); + private readonly HeldInputs inputs = new(input => Input.Send(input)); private readonly WindowCapture capture = new(); private readonly ClipboardPaste clipboard = new(); private readonly SemaphoreSlim queue = new(1); - private static object ActionResult(nint before, bool dispatched = true) => new + private static object ActionResult(nint before = 0, bool dispatched = true) => new { dispatched, foregroundChanged = before == 0 || Win32.GetForegroundWindow() == 0 ? (bool?)null : before != Win32.GetForegroundWindow(), @@ -90,16 +90,23 @@ internal sealed class DesktopExecutor(MtaDispatcher mta, StaDispatcher sta) : IA if (request.Method == "releaseHeldInputs") return inputs.Release(state.Connection, cancellation); - if (request.Method is "sendKeyEvent" or "toggleMouse" && String(parameters, "direction") == "up") + if (request.Method is "sendKeyEvent" or "toggleMouse") { - var window = WindowId(parameters) ?? throw new NativeFailure("INVALID_ARGUMENT", "Release through the original window"); - var recipient = String(parameters, "appHandle"); + var handle = String(parameters, "appHandle"); + var keyboard = request.Method == "sendKeyEvent"; + var name = keyboard ? Input.HoldKeyName(String(parameters, "key")) : String(parameters, "mouseButton"); + if (String(parameters, "direction") == "up") + { + inputs.Up(state.Connection, handle, WindowId(parameters), name, keyboard); + return ActionResult(); + } + var recipient = state.App(handle); var before = Win32.GetForegroundWindow(); - if (request.Method == "sendKeyEvent") - Input.ReleaseKey(state.Connection, recipient, window, String(parameters, "key"), context); - else - Input.ReleaseButton(state.Connection, recipient, window, String(parameters, "mouseButton"), context); - return ActionResult(before); + var target = Selected(recipient, parameters, context); + var dispatched = keyboard + ? Input.KeyDown(state.Connection, recipient, target, name, context) + : Input.ButtonDown(state.Connection, recipient, target, name, context); + return ActionResult(before, dispatched); } DesktopSession.RequireInteractive(); if (request.Method == "listApps") return await DesktopWindows.ListApps(context); @@ -114,8 +121,6 @@ internal sealed class DesktopExecutor(MtaDispatcher mta, StaDispatcher sta) : IA } var app = state.App(String(parameters, "appHandle")); var foreground = Win32.GetForegroundWindow(); - if (request.Method is "sendKeyEvent" or "toggleMouse" or "pressKey" or "typeText" or "paste" or "mouseMove" or "scroll" or "drag" or "click") - inputs.RequireOwner(state.Connection, app.Handle, WindowId(parameters) ?? 0); switch (request.Method) { case "launchApp": @@ -159,12 +164,6 @@ internal sealed class DesktopExecutor(MtaDispatcher mta, StaDispatcher sta) : IA Input.Press(app, target, String(parameters, "key"), Modifiers(parameters), context); return ActionResult(foreground); } - case "sendKeyEvent": - { - var target = Selected(app, parameters, context); - var dispatched = Input.KeyDown(state.Connection, app, target, String(parameters, "key"), context); - return ActionResult(foreground, dispatched); - } case "paste": { var target = Selected(app, parameters, context); @@ -190,12 +189,6 @@ internal sealed class DesktopExecutor(MtaDispatcher mta, StaDispatcher sta) : IA var pointed = Coordinate(app, parameters, context); return Input.MouseMove(app, pointed.Window, pointed.X, pointed.Y, context); } - case "toggleMouse": - { - var target = Selected(app, parameters, context); - var dispatched = Input.ButtonDown(state.Connection, app, target, String(parameters, "mouseButton"), context); - return ActionResult(foreground, dispatched); - } case "scroll": { var target = Coordinate(app, parameters, context); diff --git a/packages/experimental/cua-windows/native/entry/HeldInputs.cs b/packages/experimental/cua-windows/native/entry/HeldInputs.cs index 4217bcb21a..56ca5b8807 100644 --- a/packages/experimental/cua-windows/native/entry/HeldInputs.cs +++ b/packages/experimental/cua-windows/native/entry/HeldInputs.cs @@ -4,7 +4,7 @@ using DeepSeek.Cua.Protocol; namespace DeepSeek.Cua.Windows; -internal sealed class HeldInputs(Action send, Action requireInteractive) +internal sealed class HeldInputs(Action send) { internal readonly record struct Target(string AppHandle, nint Window, int ProcessId, long ProcessStarted); private sealed record Press(Guid Connection, Target Target, ushort Control, string Name, Win32.Input Up) @@ -20,14 +20,6 @@ internal sealed class HeldInputs(Action send, Action requireInterac private readonly object gate = new(); private readonly List pressed = []; - internal void RequireOwner(Guid connection, string app, nint window) - { - lock (gate) - if (pressed.Any(press => press.Connection != connection - || press.Target.AppHandle != app || press.Target.Window != window)) - throw new NativeFailure("INPUT_BUSY", "Another connection or window owns held desktop input; release it before sending input to this window"); - } - internal void RequireAvailable(ushort control) { lock (gate) @@ -62,7 +54,7 @@ internal sealed class HeldInputs(Action send, Action requireInterac } } - internal void Up(Guid connection, string app, nint window, string name, bool keyboard) + internal void Up(Guid connection, string app, nint? window, string name, bool keyboard) { lock (gate) { @@ -105,7 +97,6 @@ internal sealed class HeldInputs(Action send, Action requireInterac private void ReleaseAt(int index) { - requireInteractive(); send(pressed[index].Up); pressed.RemoveAt(index); } diff --git a/packages/experimental/cua-windows/native/entry/Input.cs b/packages/experimental/cua-windows/native/entry/Input.cs index adfcc4e600..4463387700 100644 --- a/packages/experimental/cua-windows/native/entry/Input.cs +++ b/packages/experimental/cua-windows/native/entry/Input.cs @@ -93,7 +93,7 @@ internal static class Input return ((ushort)(mapped & 0xFF), false, modifiers.ToArray()); } - private static string HoldKeyName(string name) => name.ToLowerInvariant() switch + internal static string HoldKeyName(string name) => name.ToLowerInvariant() switch { "control" => "ctrl", "return" => "enter", @@ -104,13 +104,9 @@ internal static class Input var key => key, }; - internal static void ReleaseKey(Guid connection, string app, nint window, string name, RequestContext context) - => context.HeldInputs.Up(connection, app, window, HoldKeyName(name), true); - internal static bool KeyDown(Guid connection, AppState app, WindowState window, string name, RequestContext context) { Windows.RequireForeground(app, window, context); - name = HoldKeyName(name); var key = ResolveKey(name, includeModifiers: true); if (key.Modifiers.Length != 0) throw new NativeFailure("UNSUPPORTED", "This key requires modifiers in the target keyboard layout; send each modifier and the base key separately"); @@ -187,9 +183,6 @@ internal static class Input return new { dispatched = true, hoverRoute = "system-pointer", systemPointerMoved = before.X != point.X || before.Y != point.Y }; } - internal static void ReleaseButton(Guid connection, string app, nint window, string button, RequestContext context) - => context.HeldInputs.Up(connection, app, window, button, false); - internal static bool ButtonDown(Guid connection, AppState app, WindowState window, string button, RequestContext context) { var flags = Button(button); diff --git a/packages/experimental/cua-windows/native/tests/HeldInputChecks.cs b/packages/experimental/cua-windows/native/tests/HeldInputChecks.cs index 1ed3d7b495..d711dd6c7f 100644 --- a/packages/experimental/cua-windows/native/tests/HeldInputChecks.cs +++ b/packages/experimental/cua-windows/native/tests/HeldInputChecks.cs @@ -12,7 +12,6 @@ internal static class HeldInputChecks var target = new HeldInputs.Target("app", 72, 122, 100); var events = new List(); var blocked = false; - var interactive = true; void Receive(Win32.Input input) { if (blocked) throw new NativeFailure("INPUT_BLOCKED", "SendInput returned zero"); @@ -21,10 +20,7 @@ internal static class HeldInputChecks var up = isKey ? (input.Data.Keyboard.Flags & 2) != 0 : input.Data.Mouse.Flags == 4; events.Add($"{control}:{(up ? "up" : "down")}"); } - var inputs = new HeldInputs(Receive, () => - { - if (!interactive) throw new NativeFailure("SESSION_LOCKED", "The interactive desktop is unavailable"); - }); + var inputs = new HeldInputs(Receive); bool Key(Guid connection, ushort key, string name, CancellationToken cancellation = default) => inputs.Down(connection, target, key, name, @@ -43,8 +39,7 @@ internal static class HeldInputChecks new() { Data = new() { Mouse = new() { Flags = 2 } } }, new() { Data = new() { Mouse = new() { Flags = 4 } } }, default), "Record the accepted mouse down"); check(Key(a, 87, "w") && !Key(a, 87, "w"), "A repeated key does not duplicate a hold"); - refuses(() => inputs.RequireOwner(b, "app", 72), "INPUT_BUSY"); - refuses(() => inputs.RequireOwner(a, "app", 73), "INPUT_BUSY"); + refuses(() => Key(b, 87, "w"), "INPUT_BUSY"); refuses(() => inputs.RequireAvailable(1), "INPUT_BUSY"); refuses(() => inputs.Up(b, "app", 72, "w", true), "INPUT_NOT_HELD"); refuses(() => inputs.Up(a, "app", 73, "w", true), "INPUT_NOT_HELD"); @@ -58,6 +53,13 @@ internal static class HeldInputChecks check(inputs.Release(a, default).Length == 3 && events.TakeLast(3).SequenceEqual(["16:up", "87:up", "1:up"]), "Release in mixed reverse order, including repress"); check(inputs.Release(a, default).Length == 0, "Duplicate connection cleanup sends no up"); + Key(a, 87, "w"); + Key(b, 68, "d"); + var beforeRelease = events.Count; + inputs.Up(a, "app", 72, "w", true); + check(inputs.Get(b).Length == 1 && events.Skip(beforeRelease).SequenceEqual(["87:up"]), + "One connection can release W while another holds D"); + inputs.Up(b, "app", 72, "d", true); using (var cancelled = new CancellationTokenSource()) { cancelled.Cancel(); @@ -65,10 +67,6 @@ internal static class HeldInputChecks catch (OperationCanceledException) { check(inputs.Get(a).Length == 0, "Cancelled down never dispatches"); } } Key(a, 87, "w"); - interactive = false; - refuses(() => inputs.Release(a, default), "INPUT_CLEANUP_FAILED"); - check(inputs.Get(a).Length == 1, "A locked desktop leaves the failed release recorded"); - interactive = true; blocked = true; refuses(() => inputs.Release(a, default), "INPUT_CLEANUP_FAILED"); check(inputs.Get(a).Length == 1, "A refused release stays recorded for disconnect fallback"); diff --git a/packages/experimental/cua-windows/tests/desktop-toggle-mouse.spec.ts b/packages/experimental/cua-windows/tests/desktop-toggle-mouse.spec.ts index 78e2bbecd2..53b8ac4d8f 100644 --- a/packages/experimental/cua-windows/tests/desktop-toggle-mouse.spec.ts +++ b/packages/experimental/cua-windows/tests/desktop-toggle-mouse.spec.ts @@ -185,10 +185,15 @@ it.skipIf(process.platform !== 'win32' || !helperPath || !fixturePath)( await expect.poll(async () => (await inspect()).inputEvents.slice(beforeSwitch)).toContain('W:down') const switched = await fixture.command('sibling', FixtureState) expect(switched.foreground).toBe(switched.window) + const peerTarget = { appHandle: otherApp.appHandle, windowId: switched.window, key: 'd' } + await other.request('sendKeyEvent', { ...peerTarget, direction: 'down' }, signal) await native.dispose() await expect.poll(async () => { const sibling = await fixture.command('inspect-sibling', FixtureState) return { foreground: sibling.foreground, events: sibling.inputEvents.slice(switched.inputEvents.length) } }).toEqual({ foreground: switched.window, events: ['W:up'] }) - + expect(released.parse(await other.request('getHeldInputs', {}, signal)).map(input => input.key)).toEqual(['d']) + await fixture.command('focus', FixtureState) + expect(await other.request('sendKeyEvent', { ...peerTarget, direction: 'up' }, signal)) + .toMatchObject({ dispatched: true, foregroundChanged: null }) }, 120_000)