Skip to content

fix(guard): input works after the app restarts; focus by pid finds unlisted windows - #488

Open
AmrDab wants to merge 1 commit into
mainfrom
fix/foreground-guard-restart
Open

AmrDab wants to merge 1 commit into
mainfrom
fix/foreground-guard-restart

Conversation

@AmrDab

@AmrDab AmrDab commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Problem

Seen on Windows 11 (2026-11-19) while driving ClippyAI, an always-on-top Electron window:

  1. The agent worked in ClippyAI (pid X).
  2. ClippyAI was reinstalled and came back as pid Y.
  3. Every computer click and key was then refused, including space:"screen" clicks: foreground changed: you were working in "ClippyAI" (pid X), but "ClippyAI" (pid Y) is in front now.
  4. The recovery the refusal suggests also failed: window focus with processId Y returned Window not found: title='' pid=Y, because the window is not among UIA's desktop Window children. The agent stayed stuck until the test driver sent raw mouse clicks itself.

Fix

  • src/core/control.ts: the foreground guard accepts a window of the same app (same process name, or for a Store app the same app it hosts) when the process it was bound to no longer runs. Another app, or a second copy of the app while the first still runs, is still refused. A pid that Windows reused for another process counts as running, so the guard stays shut in that case.
  • scripts/ps-bridge.ps1: focus-window by pid alone falls back to the pid's own top-level window from the Win32 z-order. It reuses the existing Get-PidWindowCandidates / Select-ScopeWindowHandle, which prefer a normal window over a topmost or tool one, through a new seam, Get-PidWindowElement. A pid with a title still matches by title only, with no fallback.

Not changed: whether the app counts as an AI assistant. A restarted AI-assistant app is still refused until the agent focuses it again, and that now works.

Tests

  • control-stop.test.ts has three new cases:
    • the old pid has exited and the same app is in front: input is accepted, and the new pid becomes the working window;
    • a second copy of the app while the first still runs: refused;
    • another app after the old pid exited: refused.
  • windows-focus-window.system.test.ts runs the real Cmd-FocusWindow against fake windows, in two new cases:
    • by pid alone, a window UIA does not list is focused;
    • by pid with a title, there is no fallback and nothing is raised.
  • Both positive cases fail without the fix. npm run test:ci: 316 files passed (6 skipped); 4302 tests passed (80 skipped).

No release: this goes out with the next train.

🤖 Generated with Claude Code

… unlisted windows

When the app the agent worked in restarted under a new pid (live 2026-11-19: ClippyAI
reinstalled mid-run), every input was refused as FOREGROUND_CHANGED for good, and
`window focus` by the new pid failed because its always-on-top window is not among
UIA's desktop Window children.

- control.ts: the foreground guard accepts a window of the same app when the process
  it was bound to no longer runs. Another app, or a second copy while the first still
  runs, is still refused. A reused pid counts as running (fails closed).
- ps-bridge.ps1: focus-window by pid alone falls back to the pid's top-level window
  from the Win32 z-order (Get-PidWindowCandidates / Select-ScopeWindowHandle). A pid
  with a title still matches by title only.
- Tests: three guard cases (restart accepted, second live copy refused, other app
  refused) and two focus-window system cases. Both new positive cases fail without
  the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant