Skip to content

fix(flow): keep the strict UIA census inside the client timeout; fix the approval e2e - #2787

Merged
abrichr merged 3 commits into
mainfrom
fix/desktop-replay-native-timeouts
Nov 30, 2026
Merged

abrichr merged 3 commits into
mainfrom
fix/desktop-replay-native-timeouts

Conversation

@abrichr

@abrichr abrichr commented Nov 30, 2026

Copy link
Copy Markdown
Member

Problem

Desktop replay (windows replay (Patient Notes, WindowsBackend)) has failed on every run since #2291 merged on 25 Nov; it last passed at 10:41 that day. #2291 merged with this job failing at its own head. The nightly on main fails it too (#2698).

I ran a diagnostic branch that dumped the agent's thread stacks every 25 s. Consecutive dumps showed different stacks, so the agent was slow, not deadlocked. #2291's strict census reads every control's AutomationId, Name and type through the sensitivity guard. It then called _candidate, which reads the same facts again, plus bounds and the window-name ancestry, all guarded, for every control, including ones the first read had already ruled out. On a hosted runner, a lookup in Patient Notes ran past the client's 30 s request timeout, and later requests queued behind it.

Fix

  • _find_candidates: in a strict walk, a control whose guarded facts already rule it out gets no second _candidate read. possible and _candidate's match compare the same AutomationId, Name and role, so no match and no uniqueness verdict changes, and the guard still runs on every control's first read. With this change, the keyboard-lease e2e finished in 78 s instead of hitting its 300 s timeout.
  • test_windows_approval_completeness.py:
    • The "long" phase relaunches the app in a new window. Re-recording the same step id made record_verified_window refuse it, as designed, so each phase now records probe-{name}.
    • The fixture's first-answer wait goes from 15 s to 60 s for a cold Windows PowerShell WinForms start.

Verification

  • test_win_agent_server.py, test_structural_value_completeness.py and test_native_focus_keyboard.py: 248 passed locally against this branch.
  • The diagnostic run with the census change, 46345572639, passed the approval step.

Still open

In that same run, test_native_keyboard_lease failed differently. One input request got RemoteDisconnected with no exception in the agent log. The backend then treated delivery as uncertain, and the next structural read was unavailable. That needs its own investigation and isn't fixed here. This PR's own Desktop replay run will show whether it recurs.

…t census already ruled out

(cherry picked from commit dc26908)
…t bound

The "long" phase relaunches Patient Notes in a new window, so recording the
same step id again made record_verified_window refuse it as seen in two
windows. Each phase now records its own step. The fixture's first answer
waits for a cold Windows PowerShell WinForms start, which can exceed 15 s on
a hosted runner.
@abrichr

abrichr commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: f5b8f09

The server.py change is sound. I checked the claim by reading the code and with a differential fuzz against main, and it holds for match sets and uniqueness. The blocker is in the e2e change: in the "long" phase, the approval-path refusal now comes from window ambiguity rather than from the provider-prefix check the test is named for.

Blocking

1. test_windows_approval_completeness.py:304: with per-phase step ids, the "long" phase's approval assertions pass without exercising the provider-prefix refusal.

  • _approval_window (windows_backend.py:1054-1071) returns None unless every verified step's window capture is the same single window.
  • With probe-empty and probe-short recorded in the first app window, and probe-long recorded in the relaunched one (:243), the backend holds two captures. In the long phase, approval_form_fields("save") therefore returns None before it sends /window/form-fields.
  • So in that phase:
    • fields is None (:336) and form_state_unreadable (:411) pass for the two-window reason, not because the provider prefix was refused.
    • projection_response (:308) is the stale response from the "short" phase. The evidence file records that stale response as the long phase's projection_refusal (:328).
  • Repro at this head, no Windows needed: on a WindowsBackend with window_root_at stubbed to return the first window twice, then the relaunched one, record_verified_window(f"probe-{name}", …) returns True all three times. _approval_window goes from (101, 11) to None, and approval_form_fields("save") returns None with zero HTTP posts.
  • On main the test failed loudly at record_verified_window. Now it passes whether or not the approval projection refuses a 4096-unit prefix. This test is the native proof of that refusal. Under AGENTS.md ("never weaken a check to make a test pass"), that refusal has to be exercised for real.
  • The direct server._form_field_value assertion (:334) and the structural-read assertions (:368-371) still exercise the disagreement path. Only the approval path, which is what the test name claims, became vacuous.
  • Suggested fix: give the long phase only one verified window. Use a fresh WindowsBackend for that phase, or drop the earlier phases' captures before recording probe-long. Then assert that the long phase sent its own /window/form-fields request, for example by counting transport entries before and after, and that the response carries a valid projection refusal.

server.py (_find_candidates, server.py:1339-1356): checked, no change needed

  • Equivalence by reading. possible (:1307-1319) compares exactly what the match loop compares through _candidate/_locator_from_control:
    • AutomationId or None, which possible already requires to be a str;
    • the whitespace-collapsed Name or None;
    • the same SplitButtonControl→button and _CONTROL_TO_ROLE mapping.
      The match loop also checks window_name, so a match implies possible. Skipping _candidate when not possible removes no match. A possible control still gets _candidate, and candidate_unavailable (:1358) is unchanged. The non-strict walk is untouched, and the walk still descends into a ruled-out control's children.
  • Differential fuzz. I loaded main's server.py beside this head's, then ran _find_candidates(strict_walk=True) on 40,000 random UIA trees and locators. The trees mixed roles, padded and collapsed names, empty ids, zero-size and raising bounds, and raising runtime ids. Results: identical found lists and truncation flags in 40,000/40,000 (about 15% single-match and 10% multi-match cases in a sampled run).
  • One precise caveat (not a fail-open). I also simulated a control whose sensitivity guard starts refusing after its first guarded read. On main, a ruled-out control like that makes the census incomplete (candidate_properties). At this head the census completes. So the completeness flag can change in that narrow case, though the match set can't: in 20,000 such trees, every time this head reported a complete census, its found list equalled the fault-free one (14,220/14,220). The ruled-out control is guarded before and after its own facts are read, and it is read less than before. I'd reword the comment from "changes no match and no uniqueness verdict" to "changes no match set", or note this case.
  • A metadata change between the two reads (TOCTOU) was already possible on main, and this doesn't widen it in a way that matters: the census is a snapshot either way.

Generality gate

  1. No per-app code: pass. The server.py change is generic UIA census logic. python scripts/check_engine_generality.py reports "Engine generality OK (7 baseline hits left)".
  2. Fix the class: pass for the census. The skip applies to every ruled-out control, not only to Patient Notes. The e2e fix is fixture-specific, as expected for a test, but see finding 1.
  3. Held-out evidence: the PR relies on the hosted Patient Notes runs. My fuzz adds application-independent evidence for the equivalence claim.

Tests at this head (box, Linux, py3.12, CI's locked install)

  • pytest -n 8 tests/test_win_agent_server.py tests/test_structural_value_completeness.py tests/test_native_focus_keyboard.py tests/e2e/test_windows_approval_completeness.py: 248 passed, 1 skipped. The skip is the approval e2e, which is opt-in and needs hosted Windows.
  • ruff check and ruff format --check on both changed files: clean. mypy openadapt_flow/backends/win_agent/server.py: clean.
  • My own probes (not in the PR): the main-versus-head differential census fuzz (with and without provider faults, and with the flaky sensitivity guard), and the _approval_window repro above.

Not run

  • test_windows_approval_completeness.py and test_native_keyboard_lease.py on real Windows. They need a hosted Windows runner with UIA and a WinForms desktop, and this box is Linux; the windows replay job runs both. I also couldn't confirm the 78 s timing.
  • The diagnostic run 46345572639 that the PR body cites. Its artifact holds only the keyboard-lease evidence, not the approval evidence file.

CI at this head

When I posted, windows replay (Patient Notes, WindowsBackend) was in progress, and the Flow package jobs (core unit and default-at-rest shards, critical, lint, agent frameworks, install weight) were queued or running. The 9 completed checks passed, including smoke, gitleaks, freerdp transport round trip and Check without saving (GTK, ten cases, no skips), and 18 were skipped by path filter. A green windows replay would not settle finding 1, because the long phase passes either way.

To the PR author: please make the long phase exercise the approval refusal for real, as described above, and request a re-review on the new head. I didn't push to this branch.

…projection request

Grok on #2787: with per-phase step ids the long phase held two captured
windows, so approval_form_fields refused for window ambiguity before
reaching the agent, and the evidence kept the short phase's stale response.
Each phase now clears the captures (clear_verified_windows, the documented
between-runs reset), records one step, asserts it sent its own
/window/form-fields request, and the long phase asserts the agent's own
refusal. The census comment now says it changes no match set.
@abrichr

abrichr commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 7c975e4

The blocker from my review of f5b8f097 is fixed: the "long" phase now has exactly one verified window, sends its own /window/form-fields request, and is refused by the agent itself. The only code change since f5b8f097 in server.py is a comment. I found no new problems. One optional test-tightening suggestion is below.

Prior blocker: fixed

1. The long phase's approval assertions passed without the provider-prefix refusal.

  • test_windows_approval_completeness.py:306-307: each phase now calls backend.clear_verified_windows() (windows_backend.py:1163-1166, which clears both _verified_windows and _conflicted_windows), then records one step, "probe". So _approval_window (windows_backend.py:1049-1072) resolves to the current phase's window, including the relaunched window in the long phase.
  • :308-317: the phase counts /window/form-fields transport entries before and after approval_form_fields("save") and requires exactly one new entry. projection_response is that phase's own response (:320), so the evidence file can't record a stale response any more.
  • :346: the long phase asserts projection_response.get("refusal"), which is the agent's own refusal.
  • Mechanism check on the box (no Windows), using my earlier repro: window_root_at is stubbed to return the first window, the first window again, then the relaunched one. With this head's sequence (clear, record "probe", approval read), each phase sends exactly one /window/form-fields post, to hwnd 101, 101 and 202 in turn. With the f5b8f097 sequence, the long phase sends zero posts, so the new projections_before + 1 assertion would now fail loudly on that regression.
  • Hosted Windows evidence: the dispatch run 46354299238 ran fix(win_agent): recheck the bound scope around each source read, not each getter #2788's head, which contains this exact test file. Its native-approval-evidence.json shows six /window/form-fields requests: four to the first window and two to the relaunched one. The long phase's projection_refusal is {stage: observation, channel: value, reason: provider_native_value_disagreement}, with uia_utf16_units == 4096, fields: None and gate detail form_state_unreadable. The empty and short phases each produced one approval request with window_fields. So the refusal is now exercised for real and for the right reason. On this PR's own head, Native approval complete values and no-write refusal (step 9 of windows replay) passed in run 46352557875.

Census comment (server.py:1340-1347). It now says "changes no match set" and notes the guard-flip case from my earlier caveat. The code is unchanged since f5b8f097.

Optional (not blocking)

  • :346 accepts any truthy refusal. A different refusal on the approval path, such as owned_window_present, node_limit or the catch-all projection_unavailable, would still pass, while the direct and structural assertions keep pinning provider_native_value_disagreement. Since the hosted run shows the exact refusal, pinning it would be cheap: refusal["stage"] == "observation" and refusal["channel"] == "value" and refusal["reason"] == "provider_native_value_disagreement".

Generality gate

  1. No per-app code: pass. The server.py census change is generic UIA logic, and the delta since f5b8f097 changes a test and a comment. python scripts/check_engine_generality.py (in packages/flow): "Engine generality OK (7 baseline hits left)".
  2. Fix the class: pass. The test now resets state between runs through the documented API (clear_verified_windows), instead of working around the ambiguity.
  3. Held-out evidence: the hosted Windows evidence above, plus my application-independent census fuzz below.

Tests at this head (box, Linux, py3.12, the locked install)

  • pytest -n 8 tests/test_win_agent_server.py tests/test_structural_value_completeness.py tests/test_native_focus_keyboard.py tests/e2e/test_windows_approval_completeness.py: 248 passed, 1 skipped. The skip is the opt-in Windows approval e2e.
  • pytest -n 8 tests/test_windows_backend.py tests/test_win_agent_tls.py: 85 passed.
  • ruff check and ruff format --check on both changed files: clean.
  • Re-ran my differential census fuzz (_find_candidates(strict_walk=True), merge-base server.py against this head): identical found lists and truncation flags in 20,000/20,000 trees without faults and 20,000/20,000 with provider faults. Under the flaky sensitivity guard, every census this head reported complete matched the fault-free found list (14,220/14,220).
  • The two-window mechanism repro described above.

Not run

  • The Windows e2e on this box (Linux, no UIA). I relied on the hosted evidence above. I didn't re-check the 60 s cold-start bound.

CI at this head

When I posted (00:02 EST), 25 checks had passed and 19 were skipped by path filter. None had failed. All four flow core unit and all four flow default at rest shards passed, along with flow core critical, flow core lint, smoke and gitleaks. packages-gate was queued. windows replay (Patient Notes, WindowsBackend) (run 46352557875) was still in progress: steps 6-13 had passed, including step 9 (this e2e), and step 14 (qualification campaign) was running. Step 14 failed at f5b8f097 and is addressed by #2788 and a follow-up, so a red windows replay from step 14 alone would not reopen finding 1.

I didn't push to this branch, and I won't merge it.

@abrichr
abrichr added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit 08d6a85 Nov 30, 2026
48 of 49 checks passed
@abrichr
abrichr deleted the fix/desktop-replay-native-timeouts branch November 30, 2026 05:40
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