Skip to content

Fix Snapshot crash from comtypes VARIANT marshaling on non-ASCII UI elements#148

Merged
Jeomon merged 2 commits intoCursorTouch:mainfrom
douglasnovy:fix/comtypes-ord-typeerror-snapshot
Mar 26, 2026
Merged

Fix Snapshot crash from comtypes VARIANT marshaling on non-ASCII UI elements#148
Jeomon merged 2 commits intoCursorTouch:mainfrom
douglasnovy:fix/comtypes-ord-typeerror-snapshot

Conversation

@douglasnovy
Copy link
Copy Markdown

@douglasnovy douglasnovy commented Mar 25, 2026

Description

Adds a defensive guard in tree_traversal so Snapshot skips the known comtypes ord() VARIANT marshaling failure instead of aborting the entire traversal.

Motivation

On some desktops, a UI Automation property can cause comtypes to raise TypeError: ord() expected a character, but string of length 5 found while Snapshot is walking the tree. The problematic element or subtree can be skipped safely; crashing the entire Snapshot call is unnecessary.

Changes

File: src/windows_mcp/tree/service.py

  • Keep a TypeError guard in the per-child iteration loop to skip a single problematic child and continue with remaining siblings
  • Keep a TypeError guard at the outer node level to skip the current subtree when the current node itself triggers the same failure
  • Narrow both guards so they only swallow the known comtypes/automation.py ord() TypeError; unrelated TypeErrors still follow the existing error path
  • Log the window name and original exception message at WARNING level for diagnostics

File: tests/test_tree_service.py

  • Add targeted tests for the TypeError matcher so the filter only matches the comtypes/automation.py traceback

Testing

  • uv run --with pytest pytest tests/test_tree_service.py
  • python -m py_compile src/windows_mcp/tree/service.py tests/test_tree_service.py
  • uv run --with ruff ruff check src/windows_mcp/tree/service.py tests/test_tree_service.py
    • This still reports the 30 pre-existing whitespace warnings already present in src/windows_mcp/tree/service.py; the change adds no new lint findings

Upstream Context

Related Issues

Fixes #147

Douglas Novy added 2 commits March 25, 2026 18:44
…lements

When the UI Automation tree traversal encounters a UI element whose COM property triggers a TypeError in comtypes VARIANT marshaling (e.g. non-ASCII window titles or accessibility properties), the Snapshot tool crashes instead of returning a partial result.

Add a defensive TypeError guard at both the per-child and per-node levels in tree_traversal so problematic elements are logged and skipped, allowing the rest of the desktop state capture to complete.

Fixes CursorTouch#147

Made-with: Cursor
@Jeomon Jeomon merged commit d8d1ed6 into CursorTouch:main Mar 26, 2026
@Jeomon
Copy link
Copy Markdown
Member

Jeomon commented Mar 26, 2026

Thanks

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Snapshot crashes with ord() TypeError from comtypes VARIANT marshaling on non-ASCII UI elements

2 participants