diff --git a/packages/flow/openadapt_flow/backends/win_agent/native_value.py b/packages/flow/openadapt_flow/backends/win_agent/native_value.py index 670306c4f3..db5c8e7252 100644 --- a/packages/flow/openadapt_flow/backends/win_agent/native_value.py +++ b/packages/flow/openadapt_flow/backends/win_agent/native_value.py @@ -477,11 +477,23 @@ def observe_complete_value( ): raise ValueUnavailable("native_owner_disagreement") - def guard() -> None: + def guard(*, scope: bool = False) -> None: + """Recheck the bound control before the next provider access. + + Every call rechecks the deadline, the UIA HWND/PID/RuntimeId, the + native owner and (last) the sensitive status. ``scope=True`` also + re-reads the caller's bound target scope (``identity``), which for + editable input means a strict census and a hit test: that runs + before and after each whole source read and at the end, not + between the property getters of one read. Running it around every + getter made one value read cost dozens of ElementFromPoint calls, + slow enough to time out and, on hosted Windows, enough to hit an + access violation inside UI Automation. + """ if clock() >= deadline: raise ValueUnavailable("value_observation_deadline") if ( - identity() != binding + (scope and identity() != binding) or _identity(control) != bound or reader.owner(bound[0]) != owner ): @@ -496,6 +508,7 @@ def guard() -> None: require_non_sensitive_value(control) def uia() -> str: + # The previous source read ended with a scope check. guard() method = control.GetValuePattern guard() @@ -513,13 +526,15 @@ def uia() -> str: raise ValueUnavailable("value_exceeds_limit") if getattr(pattern, "IsReadOnly", None) is not read_only: raise ValueUnavailable("value_readability_changed") - guard() + guard(scope=True) return value def native() -> NativeRead: + # The binding read above, or the previous source read, ended with + # a scope check: each source read is bracketed by two. guard() result = reader.read(owner, guard=guard) - guard() + guard(scope=True) units = len(utf16_bytes(result.value)) // 2 if not 0 <= result.extent <= MAX_UTF16_UNITS: raise ValueUnavailable("value_exceeds_limit") @@ -537,7 +552,7 @@ def native() -> NativeRead: first = uia() after = native() second = uia() - guard() + guard(scope=True) if before != after or first != second: raise ValueUnavailable("value_observation_changed") if utf16_bytes(first) != utf16_bytes(before.value): diff --git a/packages/flow/tests/test_native_value_completeness.py b/packages/flow/tests/test_native_value_completeness.py index 800778f5bb..af955f3a7d 100644 --- a/packages/flow/tests/test_native_value_completeness.py +++ b/packages/flow/tests/test_native_value_completeness.py @@ -622,3 +622,38 @@ def test_exact_helper_bytes_need_no_global_import_cache_registration() -> None: assert result.complete and module.valid_value_proof( result.value, result.proof.payload() ) + + +def test_bound_scope_is_rechecked_around_each_source_read_not_each_getter(): + # The caller's scope check (for editable input: a strict census and a hit + # test) brackets every whole source read, plus the binding read and the + # final check: six calls for the four reads, not one per property getter. + target, _pattern = control("note") + calls = [] + + def identity(): + calls.append(1) + return "bound" + + result = observe_complete_value( + target, identity=identity, native_reader=Reader("note") + ) + assert result.complete and result.value == "note" + assert len(calls) == 6 + + +@pytest.mark.parametrize("flip_at", [2, 3, 4, 5, 6]) +def test_a_scope_change_between_any_two_source_reads_is_refused(flip_at): + # Whichever bracketing check sees the scope change, nothing is complete. + target, _pattern = control("note") + calls = [] + + def identity(): + calls.append(1) + return "bound" if len(calls) < flip_at else "other" + + result = observe_complete_value( + target, identity=identity, native_reader=Reader("note") + ) + assert not result.complete and result.value is None and result.proof is None + assert result.reason == "value_identity_changed"