Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
fix(computer): keep Windows input in the background
jeffrey71711b71da75
diff --git a/apps/computer/local-daemon/README.md b/apps/computer/local-daemon/README.mdindex 735bf6ba..c1479e76 100644--- a/apps/computer/local-daemon/README.md+++ b/apps/computer/local-daemon/README.md@@ -649,6 +649,22 @@ unavailable UI Automation returns `COMPUTER_PERMISSION_REQUIRED` withoutelevating. Whole-screen read-only tools use a separate desktop session, whichdoes not affect the window tools. Browser and file tools do not depend on Cua.+Windows input stays in the background, because Cua's foreground and desktop+routes send real input through the system queue, where it reaches whichever+window is in front or under the pointer when it arrives. The Daemon hides+`delivery_mode` and `scope` from the Windows input tools and refuses them with+`COMPUTER_INPUT_NOT_SENT` before anything is sent. It also leaves out+`bring_to_front`, which keeps another app in front of the user, and+`move_cursor`, which moves the user's pointer. `double_click` and+`right_click` are refused in a Chromium window (class `Chrome_WidgetWin_*` or+`CefBrowser*`: Chrome, Edge and Electron apps), where Cua sends them as real+mouse events. Cua's background refusals, which recommend a foreground retry or+`bring_to_front`, reach the Agent as `COMPUTER_INPUT_NOT_SENT` with the+background route instead. Cua 0.35.4 refuses a background scroll in a+Chromium window, so the Agent reads such a page with a `get_window_state`+query instead; elsewhere a list or pane scrolls by its own `element_token`+through UI Automation. Windows Operations get `WindowsToolGuide`.+Linux controls the signed-in user's X11 desktop. The driver `local_linux.py` isembedded in the Go binary; it reads windows through AT-SPI and sends input with`xdotool`. The host needs `python3-pyatspi`, `xdotool`, ImageMagick, `xclip` and adiff --git a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.goindex 6a81e4e7..2ae8c994 100644--- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go+++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go@@ -287,6 +287,8 @@ func run(args []string) (resultErr error) {resolver.ToolGuide = localtools.MacToolGuide} else if runtime.GOOS == "linux" {resolver.ToolGuide = localtools.NativeLinuxToolGuide+ } else if runtime.GOOS == "windows" {+ resolver.ToolGuide = localtools.WindowsToolGuide}if toolManager.ConnectsEverydayBrowser() {browserConnections = toolManagerdiff --git a/apps/computer/local-daemon/internal/linger/localtools/chromium_window_other.go b/apps/computer/local-daemon/internal/linger/localtools/chromium_window_other.gonew file mode 100644index 00000000..30b4cba3--- /dev/null+++ b/apps/computer/local-daemon/internal/linger/localtools/chromium_window_other.go@@ -0,0 +1,6 @@+//go:build !windows++package localtools++// chromiumWindow is used only on Windows; see chromium_window_windows.go.+func chromiumWindow(pid, window int64) bool { return false }diff --git a/apps/computer/local-daemon/internal/linger/localtools/chromium_window_windows.go b/apps/computer/local-daemon/internal/linger/localtools/chromium_window_windows.gonew file mode 100644index 00000000..a20e44da--- /dev/null+++ b/apps/computer/local-daemon/internal/linger/localtools/chromium_window_windows.go@@ -0,0 +1,60 @@+package localtools++import (+ "strings"+ "sync"+ "unsafe"++ "golang.org/x/sys/windows"+)++var (+ procGetClassName = user32.NewProc("GetClassNameW")+ procEnumWindows = user32.NewProc("EnumWindows")+ procGetWindowThreadProcessID = user32.NewProc("GetWindowThreadProcessId")+)++// chromiumWindow reports whether window, or with no window any top-level+// window of pid, is a Chromium frame: Chrome, Edge and Electron apps share+// the Chrome_WidgetWin class, and CEF hosts use CefBrowser. Cua decides its+// Chromium input routes by the same class names.+func chromiumWindow(pid, window int64) bool {+ if window > 0 {+ return chromiumClass(uintptr(window))+ }+ if pid <= 0 {+ return false+ }+ chromiumSearch.Lock()+ defer chromiumSearch.Unlock()+ chromiumSearch.pid, chromiumSearch.found = pid, false+ procEnumWindows.Call(chromiumSearchCallback(), 0)+ return chromiumSearch.found+}++// chromiumSearch holds one EnumWindows walk at a time. The Go runtime keeps+// every callback NewCallback makes, so the walk reuses one callback.+var chromiumSearch struct {+ sync.Mutex+ pid int64+ found bool+}++var chromiumSearchCallback = sync.OnceValue(func() uintptr {+ return windows.NewCallback(func(hwnd, _ uintptr) uintptr {+ var owner uint32+ procGetWindowThreadProcessID.Call(hwnd, uintptr(unsafe.Pointer(&owner)))+ if int64(owner) == chromiumSearch.pid && chromiumClass(hwnd) {+ chromiumSearch.found = true+ return 0+ }+ return 1+ })+})++func chromiumClass(hwnd uintptr) bool {+ var name [64]uint16+ n, _, _ := procGetClassName.Call(hwnd, uintptr(unsafe.Pointer(&name[0])), uintptr(len(name)))+ class := windows.UTF16ToString(name[:n])+ return strings.HasPrefix(class, "Chrome_WidgetWin_") || strings.HasPrefix(class, "CefBrowser")+}diff --git a/apps/computer/local-daemon/internal/linger/localtools/cua.go b/apps/computer/local-daemon/internal/linger/localtools/cua.goindex 9b9f2767..0f4a8029 100644--- a/apps/computer/local-daemon/internal/linger/localtools/cua.go+++ b/apps/computer/local-daemon/internal/linger/localtools/cua.go@@ -133,6 +133,9 @@ func (m *Manager) openCua(ctx context.Context, op *operation) (*cuaSession, []toif !cuaTools[item.Name] && (item.Name != "run_script" || runtime.GOOS != "darwin") {continue}+ if runtime.GOOS == "windows" && windowsHiddenTools[item.Name] {+ continue+ }var input map[string]anyif json.Unmarshal(item.InputSchema, &input) != nil {return session, nil, errors.New("invalid Cua tool schema")@@ -149,7 +152,13 @@ func (m *Manager) openCua(ctx context.Context, op *operation) (*cuaSession, []to}input["required"] = kept}+ if runtime.GOOS == "windows" && inputTools[item.Name] {+ windowsSchema(input)+ }item.InputSchema, _ = json.Marshal(input)+ if item.Name == "scroll" && runtime.GOOS == "windows" {+ item.Description = windowsScrollDescription+ }if inputTools[item.Name] {if runtime.GOOS == "darwin" {item.Description += macInputResult@@ -356,6 +365,11 @@ func (c *cuaSession) call(ctx context.Context, name string, args json.RawMessageif err != nil {return nil, err}+ if runtime.GOOS == "windows" {+ if err := windowsInputRefusal(name, arguments, func() bool { return c.chromiumTarget(arguments) }); err != nil {+ return nil, err+ }+ }namedWindow := arguments["window_id"] != nilif inputTools[name] && ctx.Value(cuaInternalInput{}) == nil {if err := c.checkWindowToken(arguments); err != nil {@@ -538,6 +552,9 @@ func (c *cuaSession) call(ctx context.Context, name string, args json.RawMessage}}driverMs = time.Since(driverStarted).Milliseconds()+ if err == nil && runtime.GOOS == "windows" {+ result = windowsRefusedResult(name, result)+ }if err == nil {result = c.annotateInputResult(name, result)}diff --git a/apps/computer/local-daemon/internal/linger/localtools/cua_windows_policy.go b/apps/computer/local-daemon/internal/linger/localtools/cua_windows_policy.gonew file mode 100644index 00000000..78b93ac0--- /dev/null+++ b/apps/computer/local-daemon/internal/linger/localtools/cua_windows_policy.go@@ -0,0 +1,109 @@+package localtools++import (+ "encoding/json"+ "fmt"+)++// Windows input stays in the background. Cua's foreground and desktop routes+// send real input through the system queue, where it lands on whatever window+// is in front or under the pointer when it arrives. A user who switches apps+// while the Agent scrolls or types would receive that input, so the Computer+// never sends it on Windows.++// windowsHiddenTools are the Cua tools the Agent does not get on Windows:+// bring_to_front keeps another app in front of the user, and move_cursor+// moves the user's own pointer.+var windowsHiddenTools = map[string]bool{"bring_to_front": true, "move_cursor": true}++// windowsForegroundFields are the Cua arguments that select foreground or+// whole-desktop delivery; they are hidden from the Windows schemas.+var windowsForegroundFields = []string{"delivery_mode", "scope"}++// windowsChromiumPointerTools are the background inputs Cua 0.35.4 sends to a+// Chromium window as real system-queue mouse events.+var windowsChromiumPointerTools = map[string]bool{"double_click": true, "right_click": true}++// windowsRefusedResultCodes are the Cua refusal codes whose text tells the+// Agent to retry in the foreground or to bring the window to the front.+var windowsRefusedResultCodes = map[string]bool{"background_unavailable": true, "background_occluded": true, "background_uipi_blocked": true, "window_minimized": true}++// windowsInputRefusal refuses, before anything is sent, a Windows call that+// would take the user's foreground, pointer or keyboard. chromium reports+// whether the call's window belongs to a Chromium browser or Electron app.+func windowsInputRefusal(name string, arguments map[string]any, chromium func() bool) error {+ if windowsHiddenTools[name] {+ return fmt.Errorf("COMPUTER_INPUT_NOT_SENT: %s takes the user's foreground or pointer and is not available on Windows: %w", name, errNotSent)+ }+ if !inputTools[name] {+ return nil+ }+ if mode, ok := arguments["delivery_mode"]; ok && mode != "background" {+ return fmt.Errorf("COMPUTER_INPUT_NOT_SENT: on Windows the Computer sends input only in the background, because foreground input reaches whichever app the user brings forward: %w", errNotSent)+ }+ if scope, ok := arguments["scope"]; ok && scope != "window" {+ return fmt.Errorf("COMPUTER_INPUT_NOT_SENT: on Windows input goes to one window named by pid and window_id, never to a desktop point: %w", errNotSent)+ }+ if windowsChromiumPointerTools[name] && chromium() {+ return fmt.Errorf("COMPUTER_INPUT_NOT_SENT: %s in a Chrome, Edge or Electron window uses the user's real mouse on Windows; click the element, or use the page's own buttons and menus: %w", name, errNotSent)+ }+ return nil+}++// windowsRefusedResult replaces the text of a Cua background refusal, which+// recommends foreground delivery or bring_to_front, with the Windows route.+func windowsRefusedResult(name string, result json.RawMessage) json.RawMessage {+ var reply cuaResult+ if json.Unmarshal(result, &reply) != nil || !reply.IsError || !windowsRefusedResultCodes[reply.Structured.Code] {+ return result+ }+ text := "COMPUTER_INPUT_NOT_SENT: this window does not take " + name + " in the background, and the Computer sends no foreground input on Windows. Try another element or control, or tell the user what you could not do."+ switch {+ case reply.Structured.Code == "window_minimized":+ text = "COMPUTER_INPUT_NOT_SENT: the window is minimized, and the Computer does not bring windows forward on Windows. Ask the user to restore the window, then read it again."+ case name == "scroll":+ text = "COMPUTER_INPUT_NOT_SENT: this window does not scroll in the background. Chrome, Edge and Electron apps never do: read the content you need with get_window_state query instead of scrolling. In another app, pass the element_token of the scrolled list or pane itself from the latest get_window_state read."+ }+ var fields map[string]any+ if json.Unmarshal(result, &fields) != nil {+ return result+ }+ fields["content"] = []any{map[string]any{"type": "text", "text": text}}+ encoded, err := json.Marshal(fields)+ if err != nil {+ return result+ }+ return encoded+}++// windowsSchema removes the foreground and desktop delivery fields from a+// Cua input schema.+func windowsSchema(input map[string]any) {+ props, _ := input["properties"].(map[string]any)+ for _, field := range windowsForegroundFields {+ delete(props, field)+ }+}++// windowsScrollDescription replaces Cua's scroll description, which offers+// foreground and desktop delivery.+const windowsScrollDescription = "Scroll a window in the background by amount steps in direction. A classic window with its own scroll bar scrolls with pid and window_id; a list or pane in a modern app scrolls by its own element_token from the latest get_window_state read. Chrome, Edge and Electron apps do not scroll in the background: read the content you need with get_window_state query instead."++// chromiumTarget reports whether an input's window is a Chromium frame. An+// input named only by its element_token takes the pid and window of the read+// that token comes from.+func (c *cuaSession) chromiumTarget(arguments map[string]any) bool {+ pid, _ := arguments["pid"].(float64)+ window, _ := arguments["window_id"].(float64)+ if token, ok := arguments["element_token"].(string); ok && pid == 0 && c.op != nil {+ if key, _, found := c.op.reads.row(token); found {+ pid, window = float64(key.pid), float64(key.window)+ }+ }+ return chromiumWindow(int64(pid), int64(window))+}++// WindowsToolGuide describes Windows targeting and the background-only rule+// to the Runtime.+const WindowsToolGuide = `Use computer.ui_exec for desktop and browser interaction. Within the script use ui.window(pid, window_id) for a Windows window, ui.page for the managed browser, and ui.computer for native methods; ui.computer.list_windows finds an app's windows. Window actions use the latest observation and return the updated window.+The user keeps working on this PC. Every input goes to the window you name in the background: no input moves the user's mouse, changes the window in front or reaches the app the user is using. Scroll a list or pane with ui.computer.scroll and its own element_token. Chrome, Edge and Electron apps do not scroll in the background, so read their content with ui.computer.get_window_state and a query; in them double_click and right_click are refused, so click the element or use the page's own buttons and menus. An input refused with COMPUTER_INPUT_NOT_SENT was not sent: try another route or tell the user. A minimized window takes no input: ask the user to restore it.`diff --git a/apps/computer/local-daemon/internal/linger/localtools/cua_windows_policy_test.go b/apps/computer/local-daemon/internal/linger/localtools/cua_windows_policy_test.gonew file mode 100644index 00000000..ee967b6a--- /dev/null+++ b/apps/computer/local-daemon/internal/linger/localtools/cua_windows_policy_test.go@@ -0,0 +1,89 @@+package localtools++import (+ "encoding/json"+ "errors"+ "strings"+ "testing"+)++func TestWindowsInputRefusal(t *testing.T) {+ chromium := func() bool { return true }+ native := func() bool { return false }+ cases := []struct {+ name string+ tool string+ arguments map[string]any+ chromium func() bool+ refused bool+ }{+ {"background scroll", "scroll", map[string]any{"pid": 1.0, "direction": "down"}, chromium, false},+ {"explicit background", "click", map[string]any{"pid": 1.0, "delivery_mode": "background"}, chromium, false},+ {"foreground scroll", "scroll", map[string]any{"pid": 1.0, "delivery_mode": "foreground"}, native, true},+ {"foreground keys", "type_text", map[string]any{"pid": 1.0, "delivery_mode": "foreground"}, native, true},+ {"desktop point", "scroll", map[string]any{"x": 1.0, "y": 1.0, "scope": "desktop"}, native, true},+ {"window scope", "click", map[string]any{"pid": 1.0, "scope": "window"}, native, false},+ {"chromium double click", "double_click", map[string]any{"pid": 1.0}, chromium, true},+ {"chromium right click", "right_click", map[string]any{"pid": 1.0}, chromium, true},+ {"native right click", "right_click", map[string]any{"pid": 1.0}, native, false},+ {"chromium click", "click", map[string]any{"pid": 1.0}, chromium, false},+ {"bring to front", "bring_to_front", map[string]any{"pid": 1.0}, native, true},+ {"move cursor", "move_cursor", map[string]any{"x": 1.0, "y": 1.0}, native, true},+ {"read", "get_window_state", map[string]any{"pid": 1.0, "scope": "desktop"}, native, false},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ err := windowsInputRefusal(tc.tool, tc.arguments, tc.chromium)+ if tc.refused != (err != nil) {+ t.Fatalf("refused = %v, want %v (%v)", err != nil, tc.refused, err)+ }+ if err != nil && (!errors.Is(err, errNotSent) || !strings.HasPrefix(err.Error(), "COMPUTER_INPUT_NOT_SENT")) {+ t.Fatalf("refusal is not a not-sent error: %v", err)+ }+ })+ }+}++func TestWindowsRefusedResult(t *testing.T) {+ refusal := func(code string) json.RawMessage {+ return json.RawMessage(`{"isError":true,"content":[{"type":"text","text":"Retry this action with delivery_mode:\"foreground\""}],"structuredContent":{"code":"` + code + `"}}`)+ }+ text := func(result json.RawMessage) string {+ var reply struct {+ Content []struct{ Text string } `json:"content"`+ }+ if err := json.Unmarshal(result, &reply); err != nil || len(reply.Content) != 1 {+ t.Fatalf("result content: %s", result)+ }+ return reply.Content[0].Text+ }+ if got := text(windowsRefusedResult("scroll", refusal("background_unavailable"))); strings.Contains(got, "foreground\"") || !strings.Contains(got, "element_token") {+ t.Fatalf("scroll refusal text = %q", got)+ }+ if got := text(windowsRefusedResult("click", refusal("window_minimized"))); !strings.Contains(got, "restore") {+ t.Fatalf("minimized refusal text = %q", got)+ }+ other := refusal("element_not_found")+ if got := windowsRefusedResult("click", other); string(got) != string(other) {+ t.Fatalf("other refusal changed: %s", got)+ }+ success := json.RawMessage(`{"content":[{"type":"text","text":"ok"}],"structuredContent":{"code":"background_unavailable"}}`)+ if got := windowsRefusedResult("click", success); string(got) != string(success) {+ t.Fatalf("success changed: %s", got)+ }+}++func TestWindowsSchemaHidesForegroundFields(t *testing.T) {+ input := map[string]any{"properties": map[string]any{"pid": map[string]any{}, "delivery_mode": map[string]any{}, "scope": map[string]any{}}}+ windowsSchema(input)+ props := input["properties"].(map[string]any)+ if _, ok := props["delivery_mode"]; ok {+ t.Fatal("delivery_mode kept")+ }+ if _, ok := props["scope"]; ok {+ t.Fatal("scope kept")+ }+ if _, ok := props["pid"]; !ok {+ t.Fatal("pid dropped")+ }+}diff --git a/apps/computer/local-daemon/internal/linger/localtools/windows_desktop_live_test.go b/apps/computer/local-daemon/internal/linger/localtools/windows_desktop_live_test.goindex 9b52f707..df707465 100644--- a/apps/computer/local-daemon/internal/linger/localtools/windows_desktop_live_test.go+++ b/apps/computer/local-daemon/internal/linger/localtools/windows_desktop_live_test.go@@ -212,8 +212,10 @@ func TestWindowsDesktopLive(t *testing.T) {}return merged}- if _, err := call("computer", "bring_to_front", target); err != nil {- t.Fatal(err)+ // Windows input stays in the background: foreground delivery is refused+ // before anything is sent.+ if _, err := call("computer", "scroll", with(map[string]any{"direction": "down", "delivery_mode": "foreground"})); err == nil || !strings.Contains(err.Error(), "COMPUTER_INPUT_NOT_SENT") {+ t.Fatalf("foreground scroll was not refused: %v", err)}state, err := call("computer", "get_window_state", with(map[string]any{"max_elements": 30}))if err != nil {@@ -238,7 +240,7 @@ func TestWindowsDesktopLive(t *testing.T) {}snapshot := snapshotFields[0]const typed = "Ligne 1 : bonjour, le monde\nLigne 2 Second line\nFin ✓ çà"- if result, err := call("computer", "type_text", with(map[string]any{"text": typed, "element_token": snapshot + ":" + fields[0][1], "delivery_mode": "foreground"})); err != nil || result["isError"] == true {+ if result, err := call("computer", "type_text", with(map[string]any{"text": typed, "element_token": snapshot + ":" + fields[0][1]})); err != nil || result["isError"] == true {t.Fatalf("type_text: %s %v", text(result), err)}state, err = call("computer", "get_window_state", with(map[string]any{"max_elements": 30}))