---
id: 10422
title: Fix AppleScript UI scripting focus steal bleed in Wake Substrate
state: CLOSED
labels:
  - bug
  - ai
  - core
assignees:
  - neo-gpt
createdAt: '2026-04-27T12:02:27Z'
updatedAt: '2026-06-03T10:00:03Z'
githubUrl: 'https://github.com/neomjs/neo/issues/10422'
author: neo-gemini-pro
commentsCount: 0
parentIssue: null
subIssues: []
subIssuesCompleted: 0
subIssuesTotal: 0
blockedBy: []
blocking: []
closedAt: '2026-06-03T10:00:03Z'
---
# Fix AppleScript UI scripting focus steal bleed in Wake Substrate

## Context
During cross-harness operations, the Bridge Daemon delivers Wake Payloads via `osascript`. If the user is actively working in a different application (like Antigravity IDE) when the daemon attempts to bring the target application (like Claude Desktop) to the front, macOS UI steal prevention can block the `activate` command.

## The Problem
Because `osascript`'s `keystroke` command natively routes keystrokes to the globally frontmost application (regardless of the surrounding `tell process` block), a failed focus steal causes the daemon to dump the Wake Payload and keystroke shortcuts into whatever application the user is currently using. This causes severe input clobbering and cross-contamination of agent messages.

## The Architectural Reality
The issue lies in `ai/scripts/bridge-daemon.mjs` where `osascriptArgs` executes `keystroke "v"` without asserting that the application actually became frontmost.

## The Fix
Update the AppleScript string in `bridge-daemon.mjs` to explicitly check `if currentApp is not "${appName}"` immediately before modifying the clipboard and sending the `keystroke "v"`. If the focus steal failed, the script will gracefully throw an error and abort, preventing the bleed.

## Acceptance Criteria
- [ ] Bridge daemon AppleScript explicitly verifies the frontmost process matches the target `appName`.
- [ ] If the check fails, the script exits without modifying the clipboard.
- [ ] The WAKE payload does not bleed into the wrong application when focus steal is blocked by macOS.

Origin Session ID: 49e9b05a-0581-4fb7-861f-7e4970ea4c2b

## Timeline

- 2026-04-27T12:02:28Z @neo-gemini-pro added the `bug` label
- 2026-04-27T12:02:28Z @neo-gemini-pro added the `ai` label
- 2026-04-27T12:02:28Z @neo-gemini-pro added the `core` label
- 2026-04-27T12:04:45Z @neo-gemini-pro cross-referenced by PR #10423
- 2026-04-27T12:43:05Z @neo-gemini-pro referenced in commit `a4a24d8` - "feat(bridge): implement self-healing PID lock mechanism to prevent daemon duplication (#10422)"
- 2026-04-27T13:01:20Z @tobiu referenced in commit `a34e327` - "fix(ai): resolve focus-steal input clobbering and daemon duplication (#10423)

* fix(bridge): prevent AppleScript focus steal bleed to active app (#10422)

* feat(bridge): implement self-healing PID lock mechanism to prevent daemon duplication (#10422)

* refactor(bridge): switch PID lock to SIGTERM with 3s timeout and use O_EXCL atomic claim

---------

Co-authored-by: tobiu <tobiasuhlig78@gmail.com>"
- 2026-04-27T15:26:31Z @neo-opus-ada cross-referenced by PR #10431
- 2026-04-30T00:56:58Z @neo-gpt cross-referenced by #10517
- 2026-05-01T21:09:31Z @neo-opus-ada cross-referenced by #10601
- 2026-05-01T21:25:10Z @neo-opus-ada cross-referenced by PR #10602
- 2026-05-01T22:17:47Z @neo-opus-ada cross-referenced by PR #10607
- 2026-05-02T10:15:11Z @neo-opus-ada cross-referenced by #10611
- 2026-05-03T14:53:43Z @neo-gpt cross-referenced by #10647
- 2026-05-03T14:54:44Z @neo-gpt cross-referenced by #10648
- 2026-05-03T17:20:31Z @neo-gpt cross-referenced by #10658
- 2026-05-03T18:54:33Z @neo-gpt cross-referenced by #10660
- 2026-05-03T21:19:12Z @neo-gpt cross-referenced by #10666
- 2026-05-04T08:50:44Z @neo-opus-ada cross-referenced by #10675
- 2026-05-04T13:16:22Z @neo-opus-ada cross-referenced by PR #10690
- 2026-05-04T13:23:44Z @tobiu referenced in commit `75c72cd` - "feat(ai): per-identity idle-out A2A heartbeat nudge dispatcher (#10675) (#10690)

Adds ai/scripts/idleOutNudge.mjs — per-identity dispatcher consumed by
swarm-heartbeat.sh when checkSunsetted.mjs (#10689 detector contract)
emits recommended_action: 'idle_out_nudge' for a specific identity.

Distinct from trioWakeCooldown.mjs which fires on the swarm-wide
all-agent-idle signal: this dispatcher fires when ONE identity exceeds
the staleness threshold while the swarm is otherwise active. Both reuse
the inflightLock.mjs idle_out_nudge mode (#10683) but operate
independently.

Invariants per #10675:
- Bounded: in-flight idle_out_nudge lock prevents re-fire within
  BOOT_TIMEOUT_MS (15 min default)
- Non-spawning: A2A messaging path; bridge-daemon delivers via existing
  in-place keystroke. No fresh-session spawn.
- Idempotent: lock-acquire is the gate; defender-in-depth checks both
  detector-side (via #10689 contract) and dispatcher-side
- No-destructive-type: bridge-daemon's existing focus-steal protection
  (#10422) covers keystroke delivery

Safety sequencing: wake safety gate (#10648) → in-flight lock check →
lock acquire → A2A send. On send error: clear lock so the next interval
can retry without waiting for BOOT_TIMEOUT_MS expiration.

Lock-release path: NOT cleared on dispatcher success — stays held until
checkSunsetted's next cycle observes a fresh AGENT_MEMORY post-acquire
and clears it (memory-resolved release per #10683).

swarm-heartbeat.sh wiring: new branch routes recommended_action=
'idle_out_nudge' AFTER the sunset-spawn path, BEFORE the all-agent-idle
path. Defense-in-depth: shell-side gate check skips the Node-process
spawn cost when the gate is closed.

Tests: 7 new unit tests cover usage error, gate-tripped early-exit, lock-
already-held idempotency, body-convention static check, defense-in-depth
sequence ordering, send-failure lock-clear path, swarm-heartbeat
integration point. Downstream regression check on swarm-heartbeat.spec /
checkSunsetted.spec / inflightLock.spec: 23 passed.

Co-authored-by: tobiu <tobiasuhlig78@gmail.com>"
- 2026-05-09T22:00:04Z @neo-opus-ada cross-referenced by #11066
- 2026-05-09T22:07:31Z @neo-opus-ada cross-referenced by PR #11067
- 2026-06-03T02:22:06Z @neo-gpt cross-referenced by PR #12411
- 2026-06-03T06:00:59Z @neo-gpt assigned to @neo-gpt
- 2026-06-03T06:07:18Z @neo-gpt cross-referenced by PR #12424
- 2026-06-03T06:23:12Z @neo-opus-ada cross-referenced by #12425
- 2026-06-03T06:46:59Z @neo-opus-ada cross-referenced by #12401
- 2026-06-03T10:00:03Z @tobiu referenced in commit `0695076` - "fix(ai): guard osascript frontmost delivery (#10422) (#12424)

Co-authored-by: tobiu <tobiasuhlig78@gmail.com>"
- 2026-06-03T10:00:03Z @tobiu closed this issue

