Skip to content

Commit 76bb1e5

Browse files
committed
chore(waa-live): add focus diagnostics on setup-readiness failure
1 parent 1a1c7bb commit 76bb1e5

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

  • openadapt_evals/adapters/waa

openadapt_evals/adapters/waa/live.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ def __init__(self, config: WAALiveConfig | None = None):
388388
self._actual_screen_size: tuple[int, int] | None = None
389389
self._clean_desktop_applied = False
390390
self._environment_profile: dict[str, Any] = {}
391+
self._last_setup_results: list[dict[str, Any]] = []
392+
self._last_foreground_title: str | None = None
391393

392394
@property
393395
def name(self) -> str:
@@ -1427,6 +1429,7 @@ def _run_task_setup(self, raw_config: dict) -> None:
14271429
raw_config: Task configuration with setup commands.
14281430
"""
14291431
config = raw_config.get("config", [])
1432+
self._last_setup_results = []
14301433

14311434
related_apps = raw_config.get("related_apps", [])
14321435
if related_apps:
@@ -1450,6 +1453,7 @@ def _run_task_setup(self, raw_config: dict) -> None:
14501453
)
14511454
payload = resp.json() if resp.headers.get("content-type", "").startswith("application/json") else {}
14521455
results = payload.get("results", [])
1456+
self._last_setup_results = results
14531457
for r in results:
14541458
status = r.get("status", "unknown")
14551459
logger.info(f"Setup {r.get('type')}: {status}")
@@ -1874,8 +1878,65 @@ def _ensure_app_focused(self, raw_config: dict) -> bool:
18741878
"Post-setup focus: could not confirm target app is in foreground "
18751879
"after deterministic remediation."
18761880
)
1881+
diagnostics = self._capture_focus_diagnostics(patterns)
1882+
if diagnostics:
1883+
logger.warning("Post-setup focus diagnostics: %s", diagnostics)
18771884
return False
18781885

1886+
def _capture_focus_diagnostics(self, patterns: list[str]) -> dict[str, Any]:
1887+
"""Capture foreground/window state and last open-step setup result."""
1888+
diagnostics: dict[str, Any] = {
1889+
"expected_patterns": patterns,
1890+
"last_foreground_title": self._last_foreground_title,
1891+
}
1892+
1893+
open_results = [
1894+
r for r in self._last_setup_results
1895+
if isinstance(r, dict) and r.get("type") == "open"
1896+
]
1897+
if open_results:
1898+
diagnostics["last_open_setup_result"] = open_results[-1]
1899+
1900+
script = r"""
1901+
$ErrorActionPreference='SilentlyContinue'
1902+
Add-Type -TypeDefinition @"
1903+
using System;
1904+
using System.Runtime.InteropServices;
1905+
using System.Text;
1906+
public static class OAWinApi {
1907+
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
1908+
[DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
1909+
public static string GetForegroundTitle() {
1910+
var sb = new StringBuilder(512);
1911+
var h = GetForegroundWindow();
1912+
GetWindowText(h, sb, sb.Capacity);
1913+
return sb.ToString();
1914+
}
1915+
}
1916+
"@
1917+
$fg = [OAWinApi]::GetForegroundTitle()
1918+
$wins = Get-Process | Where-Object { $_.MainWindowTitle -and $_.MainWindowTitle.Trim() -ne '' } | Select-Object -First 25 ProcessName, MainWindowTitle
1919+
[pscustomobject]@{
1920+
foreground_title = $fg
1921+
windows = $wins
1922+
} | ConvertTo-Json -Depth 4 -Compress
1923+
"""
1924+
try:
1925+
output = self.run_powershell(script).strip()
1926+
parsed_snapshot = False
1927+
for line in reversed(output.splitlines()):
1928+
line = line.strip()
1929+
if line.startswith("{") and line.endswith("}"):
1930+
diagnostics["window_snapshot"] = json.loads(line)
1931+
parsed_snapshot = True
1932+
break
1933+
if not parsed_snapshot and output:
1934+
diagnostics["window_snapshot_raw"] = output[-1000:]
1935+
except Exception as e:
1936+
diagnostics["window_snapshot_error"] = str(e)
1937+
1938+
return diagnostics
1939+
18791940
def _try_activate_patterns(
18801941
self,
18811942
patterns: list[str],
@@ -1959,6 +2020,7 @@ def _check_foreground_matches(
19592020
title = (self._extract_window_title(a11y_tree) or "").strip()
19602021
if not title:
19612022
return False
2023+
self._last_foreground_title = title
19622024
title_lower = title.lower()
19632025
for pattern in patterns:
19642026
if pattern.lower() in title_lower:

0 commit comments

Comments
 (0)