Skip to content

fix(computer-use): resolve macOS accessibility targets - #1101

Merged
edenreich merged 1 commit into
mainfrom
fix/macos-accessibility-target-resolution
Aug 23, 2026
Merged

edenreich merged 1 commit into
mainfrom
fix/macos-accessibility-target-resolution

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

Summary

Fix macOS accessibility target resolution when AX does not expose a focused application or when callers use a display name that differs from the CoreGraphics window-owner name.

Changes

  • Fall back from AXFocusedApplication to the PID of the frontmost layer-0 application window
  • Match equivalent display and process names case-insensitively while ignoring separators such as spaces and hyphens
  • Keep Dock, menu bar, and elevated overlay windows out of the frontmost fallback
  • Add regression coverage for exact, case-insensitive, display/bundle, distinct-app, and invalid-name cases

Testing

  • go test ./internal/computer/infrastructure/accessibility
  • task test
  • task precommit:run
  • Native helper: normalized app:i Term 2 returned 19 elements
  • Native helper: frontmost returned 14 elements for foreground iTerm2

Related

@edenreich
edenreich marked this pull request as ready for review August 23, 2026 02:20
@edenreich
edenreich requested a review from a team as a code owner August 23, 2026 02:20
@edenreich

Copy link
Copy Markdown
Contributor Author

@infer Can you review this? use /ponytail:ponytail-review /go /go-concurrency

@inference-gateway-maintainer

inference-gateway-maintainer Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Model: ollama_cloud/deepseek-v4-flash:preview · View Job

I'm cooking...will get back to you soon...

Pull Request

#1101

✅ Infer Result: Success

Below is the combined review across all three requested lenses.


Ponytail: ponytail-review

L540-558: shrink: pidForFrontmostApplication duplicates pidForApplication's 6-line preamble (cgWindowListCopyWindowInfo, nil-guard, defer, for-loop header).
Extract a shared walkWindows(func(window uintptr) bool) int32 helper that handles the bookkeeping and returns the first PID where the filter returns true. Then pidForApplication passes func(w) { return applicationNamesMatch(b.goString(b.cfDictionaryGetValue(w, b.ownerName)), name) } and pidForFrontmostApplication passes func(w) { return b.layer(w) == 0 }. Saves ~8 lines and keeps the duplication from diverging when a third caller appears.

L574-575: shrink: second normalization of owner is redundant when the first EqualFold already matched.
In applicationNamesMatch, the normalizedRequested != "" guard prevents matching two punctuation-only names ("-" vs ".") where both normalize to "". If that were a real scenario (no app is named "."), the guard is a speculative edge case. Drop it: if both sides normalize to "" the answer true is harmless — no application with an empty CG window-owner name will match any real target. Saves 1 line and a branch.

L15: native: test case "inference-gateway-desktop" vs "Inference Gateway Desktop" is the project's own app.
This is a useful regression test, not a problem.

net: -8 lines possible.


Go Review

Sound:

  • normalizeApplicationName correctly uses strings.Builder over a for range on runes — handles the Unicode that macOS uses in display names (Cyrillic, CJK, accented). The filter to only letters+digits is the exact semantics needed (strip spaces, hyphens, underscores from bundle-vs-display-name comparisons). Correct.

  • applicationNamesMatch follows the Go discipline of checking the fast path first (exact case-insensitive EqualFold) before falling through to the more expensive normalization. Good.

  • windowPID extraction is a textbook "extract function" refactor — replaces inline conversions at two call sites, returning a zero PID on any failure so callers remain simple. The two callers that already existed (pidForApplication inline, now windowPID) were identical logic; the refactor shrinks the total surface.

  • The frontmostApplication() fallback chain is linear and readable — AXFocusedApplication first, then pidForFrontmostApplication as a CGWindow-based fallback, then the error. No nesting pyramids.

  • Test file is table-driven with t.Run subtests, covers the five relevant dimensions (exact match, case insensitivity, bundle-vs-display, distinct apps, empty/punctuation). Good.

Nit:

  • frontmostApplication() (L499-507): the happy-path return is inside the if ok block. An alternative is:
if !ok {
    if pid := b.pidForFrontmostApplication(); pid != 0 {
        return b.axCreateApplication(pid), nil
    }
    return 0, fmt.Errorf(...)
}
return application, nil

This keeps return application, nil at the left margin — the Go "happy path unindented" convention. The current code is logically equivalent and perfectly readable with two branches; no correctness issue, just a style preference.


Go Concurrency Review

No concurrency concerns. Every function added or modified in this diff is a synchronous FFI call to CoreFoundation, CoreGraphics, or Accessibility APIs. There are no goroutines, channels, sync primitives, shared mutable state, or context.Context parameters in the changed code. The file runs on a locked OS thread (runtime.LockOSThread in runNative — unchanged before and after this diff). Zero findings.


Summary: The change is lean and focused. The fallback from AXFocusedApplication to CGWindow-based PID resolution and the case/separator-insensitive name matching each solve a real bug (tracked in inference-gateway/desktop#142). The refactoring (extracting windowPID, pulling normalizeApplicationName out) leaves the file strictly better than it started. The one shrink finding above would save ~8 lines but is optional — the diff is already clean. Ponytail says: lean; ship.

Model: ollama_cloud/deepseek-v4-flash:preview · Exit Code: 0 · Duration: 42s

Tokens: 98,550 in · 3,292 out · 101,842 total (3 requests)

Tool calls: 8 total · 100% success rate

Traces
Session: 1787452702-d3f091c9

session (headless, success)                                                  34.2s
├── chat ollama_cloud/deepseek-v4-flash:preview                               3.3s
│   ╰── POST /v1/chat/completions [inference-gateway]                         3.3s
│       ╰── POST /proxy/ollama_cloud/chat/completions [inference-gateway]     3.3s [error: *errors.errorString]
│           ╰── POST /proxy/:provider/*path [inference-gateway]               3.3s
│               ╰── POST /v1/chat/completions [inference-gateway]             3.3s [error: *errors.errorString]
├── execute_tool Read call_jkmkx6go                                          198µs
├── execute_tool Read call_0auj9mod                                          130µs
├── execute_tool Read call_d23aptsg                                          172µs
├── execute_tool Bash call_xidkyfij                                            4ms
├── execute_tool Bash call_vij27d3l                                            4ms
├── chat ollama_cloud/deepseek-v4-flash:preview                               3.3s
│   ╰── POST /v1/chat/completions [inference-gateway]                         3.3s
│       ╰── POST /proxy/ollama_cloud/chat/completions [inference-gateway]     3.3s [error: *errors.errorString]
│           ╰── POST /proxy/:provider/*path [inference-gateway]               3.3s
│               ╰── POST /v1/chat/completions [inference-gateway]             3.3s [error: *errors.errorString]
├── execute_tool Read call_myw5qzxs                                           97µs [error: tool_error]
├── execute_tool Read call_6szn8t0z                                          123µs
├── execute_tool Read call_vix5fh37                                          294µs
╰── chat ollama_cloud/deepseek-v4-flash:preview                              26.2s
    ╰── POST /v1/chat/completions [inference-gateway]                        26.2s
        ╰── POST /proxy/ollama_cloud/chat/completions [inference-gateway]    26.2s [error: *errors.errorString]
            ╰── POST /proxy/:provider/*path [inference-gateway]              26.2s
                ╰── POST /v1/chat/completions [inference-gateway]            26.2s [error: *errors.errorString]
Stats
Tool Calls

╭──────┬───────┬───────┬─────╮
│ Tool │ Calls │ Fail% │ Avg │
├──────┼───────┼───────┼─────┤
│ Read │ 6     │ 17%   │ 0ms │
│ Bash │ 2     │ 0%    │ 4ms │
╰──────┴───────┴───────┴─────╯

Token Usage

╭────────────────────────────────────────┬────────┬────────┬────────────┬────────┬──────╮
│ Model                                  │ Prompt │ Cached │ Completion │ Total  │ Cost │
├────────────────────────────────────────┼────────┼────────┼────────────┼────────┼──────┤
│ ollama_cloud/deepseek-v4-flash:preview │ 98550  │ 0      │ 3292       │ 101842 │ -    │
╰────────────────────────────────────────┴────────┴────────┴────────────┴────────┴──────╯

Sessions

╭───────────┬──────────┬──────────╮
│ Execution │ Mode     │ Sessions │
├───────────┼──────────┼──────────┤
│ headless  │ headless │ 1        │
╰───────────┴──────────┴──────────╯

Triggered by edenreich · Infer Action · OSS

@inference-gateway-maintainer inference-gateway-maintainer Bot moved this to Todo in 2026 Aug 23, 2026
@edenreich
edenreich merged commit 39f1d9a into main Aug 23, 2026
1 check passed
@edenreich
edenreich deleted the fix/macos-accessibility-target-resolution branch August 23, 2026 11:19
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.179.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant