From d83433e649d7da45bfac5378b0cb7e92993608d3 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:04 +0000 Subject: [PATCH 01/16] Observe cancellation in Windows helper waits A cancelled command could not be acknowledged while the helper waited up to 5 s on a bounded provider call, a whole-subtree cache, or the 2 s wait after terminating a packaged application. Node then killed the helper, which since the kill-on-close Job Object also ends the application under test. Bounded provider calls now read the command's token through a thread-local CancellationScope and wait in 50 ms slices. A cancelled read throws CancelledError; a cancelled mutation is abandoned and quarantined like a timed out one, so the command reports effect `unknown`. Whole-subtree and probe caches run on the bounded worker, and closeApplication's post-terminate wait observes the token and keeps the lease for a retry. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../native/windows/src/application.cpp | 13 +++- .../native/windows/src/automation.cpp | 34 +++++++--- .../native/windows/src/driver.cpp | 1 + .../native/windows/src/selftest.cpp | 36 +++++++++++ .../native/windows/src/settlement.cpp | 64 +++++++++++++++++-- .../native/windows/src/settlement.h | 21 +++++- 6 files changed, 152 insertions(+), 17 deletions(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/application.cpp b/packages/agentic/desktop-driver/native/windows/src/application.cpp index 8a7dbe7b47..1a6b7dd9b7 100644 --- a/packages/agentic/desktop-driver/native/windows/src/application.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/application.cpp @@ -48,7 +48,11 @@ namespace { constexpr unsigned int kLaunchTimeoutMs = 90000; constexpr unsigned int kLaunchGraceMs = 15000; constexpr unsigned int kCloseTimeoutMs = 5000; +constexpr unsigned int kTerminateWaitMs = 2000; constexpr unsigned int kActivationTimeoutMs = 2000; +constexpr unsigned int kWindowResponseTimeoutMs = 2000; +constexpr UINT kWindowProbeMessageTimeoutMs = 200; +constexpr unsigned int kWindowRestoreTimeoutMs = 500; constexpr DWORD kLaunchCleanupGraceMs = 250; constexpr DWORD kLaunchCleanupForcedMs = 1000; constexpr unsigned int kDisposeGraceMs = 3000; @@ -1539,7 +1543,14 @@ void ApplicationManager::CloseApplication(const std::string& leaseId, const Canc if (TerminateProcess(lease.process, 0) == FALSE && IsProcessAlive(lease.process)) { FailLastError(kErrorCleanupFailed, "Terminating the owned application process", GetLastError()); } - const DWORD waited = WaitForSingleObject(lease.process, 2000); + // Waits in slices so a cancelled close is acknowledged promptly and the + // lease stays for a retry. + const Deadline terminated(kTerminateWaitMs); + DWORD waited = WaitForSingleObject(lease.process, 0); + while (waited == WAIT_TIMEOUT && !terminated.Expired()) { + token.Wait(50); + waited = WaitForSingleObject(lease.process, 0); + } if (waited == WAIT_FAILED && IsProcessAlive(lease.process)) { FailLastError(kErrorCleanupFailed, "Waiting for the owned application process", GetLastError()); } diff --git a/packages/agentic/desktop-driver/native/windows/src/automation.cpp b/packages/agentic/desktop-driver/native/windows/src/automation.cpp index e75f46fda6..c95f067b49 100644 --- a/packages/agentic/desktop-driver/native/windows/src/automation.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/automation.cpp @@ -20,6 +20,10 @@ constexpr std::size_t kMaximumAncestorWalk = 64; // bounded step that reports an explicit failure instead of wedging the helper; // a mutation that cannot settle also quarantines later mutations (settlement.h). constexpr DWORD kProviderCallTimeoutMs = 5000; +// The UI Automation client's own bounds on reaching a provider and on one +// cross-process transaction. +constexpr DWORD kConnectionTimeoutMs = 2000; +constexpr DWORD kTransactionTimeoutMs = 5000; constexpr char kTreeTooLargeMessage[] = "The native accessibility tree exceeds the 5000-node limit."; struct ActionMapping { @@ -123,6 +127,23 @@ HRESULT BoundedRead(Fn&& call, std::string_view operation) { kProviderCallTimeoutMs, operation, kErrorAutomationFailed); } +// A whole-subtree cache is one provider transaction that a busy application can +// hold for the full transaction timeout, so it runs on a bounded worker that a +// cancelled command stops waiting for. The bound only backs up the client's +// own timeouts, which still decide how a slow provider fails. +HRESULT BuildCacheAbandonably(IUIAutomationElement* element, const winrt::com_ptr& request, + winrt::com_ptr& cached) { + winrt::com_ptr source; + source.copy_from(element); + const auto output = std::make_shared>(); + const HRESULT result = RunBoundedProviderCall( + ProviderCall::Read, [source, request, output]() { return source->BuildUpdatedCache(request.get(), output->put()); }, + kConnectionTimeoutMs + kTransactionTimeoutMs + 1000, "Reading the cached automation subtree", + kErrorAutomationFailed); + cached = *output; + return result; +} + // UI Automation reports a disabled element, such as a control of an owner window // while its modal dialog is open, as a failed pattern call. That is the element // refusing interaction, not an automation fault. @@ -398,8 +419,8 @@ void Automation::Initialize() { if (SUCCEEDED(automation->QueryInterface(IID_PPV_ARGS(automation2.put())))) { // Bound every cross-process transaction so a hung provider cannot stall the // command worker indefinitely. - automation2->put_ConnectionTimeout(2000); - automation2->put_TransactionTimeout(5000); + automation2->put_ConnectionTimeout(kConnectionTimeoutMs); + automation2->put_TransactionTimeout(kTransactionTimeoutMs); } CheckHresult(kErrorAutomationFailed, "Reading the UI Automation control view", @@ -760,8 +781,7 @@ std::vector Automation::WalkCachedSubtree(IUIAutomationElement* }; winrt::com_ptr cachedRoot; - HRESULT result = root->BuildUpdatedCache(CreateCacheRequest(TreeScope_Subtree, controlViewOnly).get(), - cachedRoot.put()); + HRESULT result = BuildCacheAbandonably(root, CreateCacheRequest(TreeScope_Subtree, controlViewOnly), cachedRoot); // The whole-subtree cache is one provider transaction, so a tree past the node limit can outlast the transaction // timeout before the walk below gets to refuse it. Count such a tree child by child so it is still refused as too // large rather than reported as a provider failure or retried. @@ -777,13 +797,11 @@ std::vector Automation::WalkCachedSubtree(IUIAutomationElement* }; for (int attempt = 0; isTransient(result) && attempt < 5; attempt += 1) { winrt::com_ptr probe; - if (FAILED(root->BuildUpdatedCache(CreateCacheRequest(TreeScope_Element, controlViewOnly).get(), probe.put())) || - !probe) { + if (FAILED(BuildCacheAbandonably(root, CreateCacheRequest(TreeScope_Element, controlViewOnly), probe)) || !probe) { break; } token.Wait(20); - cachedRoot = nullptr; - result = root->BuildUpdatedCache(CreateCacheRequest(TreeScope_Subtree, controlViewOnly).get(), cachedRoot.put()); + result = BuildCacheAbandonably(root, CreateCacheRequest(TreeScope_Subtree, controlViewOnly), cachedRoot); } if (result == UIA_E_ELEMENTNOTAVAILABLE) { Fail(kErrorStaleElement, "The native element is no longer available."); diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.cpp b/packages/agentic/desktop-driver/native/windows/src/driver.cpp index 30ce1ebd88..1567546da6 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/driver.cpp @@ -490,6 +490,7 @@ CommandResult Driver::Execute(const std::string& command, const json::Value& par const bool tracked = IsTrackedMutation(command); effects_.Reset(); const EffectScope effectScope(effects_); + const CancellationScope cancellationScope(token); // A provider deadline that passed is always unknown, whatever the ledger // recorded before it. const auto effectOf = [this](const std::string& code) { diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index d08bd1a0ba..c3c622034f 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -2807,6 +2807,42 @@ void TestEffectLedger() { return Contains(refusal, "completed late"); }); ResetMutationQuarantineForTesting(); + + // A cancelled command stops waiting for a stalled provider call well before + // the call's own bound, and a mutation it abandons leaves the effect unknown. + CancellationToken cancelled; + cancelled.Cancel(); + const auto cancelledCall = [&cancelled](ProviderCall kind, const std::shared_ptr& stalled, + EffectLedger& ledger) { + const CancellationScope cancellationScope(cancelled); + const EffectScope effectScope(ledger); + const ULONGLONG started = GetTickCount64(); + std::string outcome = "none"; + try { + RunBoundedProviderCall(kind, [stalled]() { return stalled->Hold(); }, 5000, "Testing the provider", + kErrorAutomationFailed); + } catch (const CancelledError&) { + outcome = "cancelled"; + } catch (const HelperError& error) { + outcome = error.code(); + } + return GetTickCount64() - started < 1000 ? outcome : outcome + " late"; + }; + EffectLedger readLedger; + const auto stalledRead = std::make_shared(); + Check(cancelledCall(ProviderCall::Read, stalledRead, readLedger) == "cancelled" && + readLedger.Effect() == MutationEffect::None && !MutationsQuarantined(), + "a cancelled command stops waiting for a stalled read without quarantining the session"); + stalledRead->Open(); + EffectLedger mutationLedger; + const auto stalledMutation = std::make_shared(); + Check(cancelledCall(ProviderCall::Mutation, stalledMutation, mutationLedger) == "cancelled" && + mutationLedger.Effect() == MutationEffect::Unknown && MutationsQuarantined(), + "a cancelled command stops waiting for a stalled mutation, leaving its effect unknown and quarantined"); + FailureCode([]() { RequireMutationsAllowed(); }, &refusal); + Check(Contains(refusal, "cancelled before it settled"), "the quarantine names the cancelled mutation"); + stalledMutation->Open(); + ResetMutationQuarantineForTesting(); } // Every mutating command reports what it handed to the application when it diff --git a/packages/agentic/desktop-driver/native/windows/src/settlement.cpp b/packages/agentic/desktop-driver/native/windows/src/settlement.cpp index 0b85e390bb..9fd802b0fb 100644 --- a/packages/agentic/desktop-driver/native/windows/src/settlement.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/settlement.cpp @@ -1,5 +1,6 @@ #include "settlement.h" +#include #include #include #include @@ -32,6 +33,8 @@ struct AbandonedMutation { bool providerTimedOut{false}; // The lease of the application the mutation acted on; empty when unknown. std::string leaseId; + // The command was cancelled before the call settled. + bool cancelled{false}; }; std::string& CurrentLeaseId() { @@ -44,6 +47,15 @@ EffectLedger*& CurrentEffectLedger() { return ledger; } +const CancellationToken*& CurrentCancellationToken() { + thread_local const CancellationToken* token = nullptr; + return token; +} + +// The longest a bounded call waits before it looks at the command's +// cancellation token again. +constexpr DWORD kCancellationSliceMs = 50; + // UI Automation refusals, repeated here because the header defines them only // for UI Automation clients. constexpr HRESULT kUiaElementNotEnabled = static_cast(0x80040200); @@ -78,14 +90,18 @@ std::string DescribeUnsettled(const AbandonedMutation& entry) { const std::string settlement = entry.call->Settled() ? "completed late with " + DescribeHresult(entry.call->result.load()) : std::string("is still pending"); + if (entry.cancelled) { + return subject + " was cancelled before it settled and " + settlement; + } return subject + " did not settle within " + std::to_string(entry.timeoutMs) + " ms and " + settlement; } -void Abandon(std::string_view operation, DWORD timeoutMs, std::shared_ptr call, bool providerTimedOut) { +void Abandon(std::string_view operation, DWORD timeoutMs, std::shared_ptr call, bool providerTimedOut, + bool cancelled = false) { Ledger& ledger = LedgerInstance(); const std::lock_guard lock(ledger.mutex); - ledger.abandoned.push_back( - AbandonedMutation{std::string(operation), timeoutMs, std::move(call), providerTimedOut, CurrentLeaseId()}); + ledger.abandoned.push_back(AbandonedMutation{std::string(operation), timeoutMs, std::move(call), providerTimedOut, + CurrentLeaseId(), cancelled}); } constexpr char kUnsettledNotice[] = @@ -93,7 +109,7 @@ constexpr char kUnsettledNotice[] = "re-observed once it responds."; HRESULT RunBounded(std::function call, DWORD timeoutMs, std::shared_ptr& state, - bool& completed) { + bool& completed, bool& cancelled) { state = std::make_shared(); state->done = CreateEventW(nullptr, TRUE, FALSE, nullptr); if (state->done == nullptr) { @@ -110,8 +126,24 @@ HRESULT RunBounded(std::function call, DWORD timeoutMs, std::shared_p } }); worker.detach(); - completed = WaitForSingleObject(state->done, timeoutMs) == WAIT_OBJECT_0; - return completed ? state->result.load() : E_PENDING; + const CancellationToken* token = CurrentCancellationToken(); + const ULONGLONG expiresAt = GetTickCount64() + timeoutMs; + while (true) { + const ULONGLONG now = GetTickCount64(); + const DWORD slice = + now >= expiresAt ? 0 : static_cast(std::min(expiresAt - now, kCancellationSliceMs)); + if (WaitForSingleObject(state->done, slice) == WAIT_OBJECT_0) { + completed = true; + return state->result.load(); + } + if (GetTickCount64() >= expiresAt) { + return E_PENDING; + } + if (token != nullptr && token->IsCancelled()) { + cancelled = true; + return E_PENDING; + } + } } } // namespace @@ -221,6 +253,14 @@ EffectScope::~EffectScope() { CurrentEffectLedger() = previous_; } +CancellationScope::CancellationScope(const CancellationToken& token) noexcept : previous_(CurrentCancellationToken()) { + CurrentCancellationToken() = &token; +} + +CancellationScope::~CancellationScope() { + CurrentCancellationToken() = previous_; +} + void RecordDispatching() noexcept { if (EffectLedger* ledger = CurrentEffectLedger(); ledger != nullptr) { ledger->Dispatching(); @@ -246,7 +286,8 @@ HRESULT RunBoundedProviderCall(ProviderCall kind, std::function call, } std::shared_ptr state; bool completed = false; - const HRESULT result = RunBounded(std::move(call), timeoutMs, state, completed); + bool cancelled = false; + const HRESULT result = RunBounded(std::move(call), timeoutMs, state, completed, cancelled); if (completed) { if (kind == ProviderCall::Mutation && IsProviderTimeout(result)) { Abandon(operation, timeoutMs, state, true); @@ -256,6 +297,15 @@ HRESULT RunBoundedProviderCall(ProviderCall kind, std::function call, } return result; } + if (cancelled) { + if (kind == ProviderCall::Mutation) { + // The provider may still apply the mutation, exactly as after a timeout, + // so the command's effect is unknown even when nothing was counted yet. + Abandon(operation, timeoutMs, state, false, true); + RecordDispatching(); + } + throw CancelledError{}; + } if (kind == ProviderCall::Read) { Fail(timeoutCode, std::string(operation) + " did not complete within " + std::to_string(timeoutMs) + " ms."); } diff --git a/packages/agentic/desktop-driver/native/windows/src/settlement.h b/packages/agentic/desktop-driver/native/windows/src/settlement.h index 5dfd4175d8..2e20d3cb76 100644 --- a/packages/agentic/desktop-driver/native/windows/src/settlement.h +++ b/packages/agentic/desktop-driver/native/windows/src/settlement.h @@ -25,10 +25,29 @@ enum class ProviderCall { Read, Mutation }; // (UI Automation or RPC timeout), fails with `mutation-indeterminate` and // quarantines the session; a timeout reported by a read is an ordinary // failure. A mutation requested while quarantined fails with -// `target-quarantined` before the provider is called. +// `target-quarantined` before the provider is called. The wait observes the +// token of the active CancellationScope in short slices: a cancelled read is +// abandoned, and a cancelled mutation is abandoned and quarantined like one +// that timed out, leaving the command's effect unknown; both throw +// CancelledError. HRESULT RunBoundedProviderCall(ProviderCall kind, std::function call, DWORD timeoutMs, std::string_view operation, const std::string& timeoutCode); +// Names the cancellation token of the command running on this thread, so +// bounded provider calls stop waiting once it is cancelled. Without an active +// scope they wait for their full bound. The previous scope returns when the +// guard ends. +class CancellationScope { + public: + explicit CancellationScope(const CancellationToken& token) noexcept; + ~CancellationScope(); + CancellationScope(const CancellationScope&) = delete; + CancellationScope& operator=(const CancellationScope&) = delete; + + private: + const CancellationToken* previous_; +}; + // Fails with `target-quarantined` while an abandoned mutation exists. Call it // before any non-provider mutation, such as physical input or window changes. void RequireMutationsAllowed(); From df735586ed0a52e4373e8789e00e023610ca1c7a Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:04 +0000 Subject: [PATCH 02/16] Probe hung windows before managing or activating them ShowWindow, SetWindowPos, and activation wait on the thread that owns the window, so a hung target held the helper, and the physical input lock, until it recovered. Activation also granted every process foreground rights. The helper now probes the window with SendMessageTimeoutW(WM_NULL, SMTO_ABORTIFHUNG) in 200 ms tries for up to 2 s and fails with automation-failed and reason `target-non-response`. setWindowRect posts its restore (ShowWindowAsync) and move (SWP_ASYNCWINDOWPOS) and polls for them with the cancellation token, recording each as dispatching until it settles. Activation skips attaching to a hung holder's input queue and grants AllowSetForegroundWindow only to the target process. A self-test stalls a window thread and checks the probe, restore, activation, and cancellation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../native/windows/src/application.cpp | 93 ++++++++++++++++++- .../native/windows/src/application.h | 7 ++ .../native/windows/src/driver.cpp | 45 +++++++-- .../native/windows/src/selftest.cpp | 67 +++++++++++++ 4 files changed, 201 insertions(+), 11 deletions(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/application.cpp b/packages/agentic/desktop-driver/native/windows/src/application.cpp index 1a6b7dd9b7..5076fe1bd1 100644 --- a/packages/agentic/desktop-driver/native/windows/src/application.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/application.cpp @@ -625,6 +625,8 @@ void TrackRelatedLaunchProcesses(const LaunchRootIdentity& root, struct ActivationOutcome { bool activated{false}; bool sawForeground{false}; + // The window's thread stopped answering messages, so activation stopped. + bool notResponding{false}; // The Windows error of the latest refused input-queue attachment, or 0. DWORD attachError{0}; }; @@ -669,6 +671,45 @@ class ForegroundInputAttachment { DWORD error_{0}; }; +enum class WindowResponse { Responds, Undetermined, NotResponding }; + +// Restoring, moving, and activating a window wait on the thread that owns it, +// so a thread that stops pumping messages would hold the helper, and with it +// the input lock, until it recovers. SMTO_ABORTIFHUNG returns at once for a +// thread Windows already considers hung; a busy one gets a short wait per try. +WindowResponse ProbeWindowResponse(HWND window, const CancellationToken& token) { + const Deadline deadline(kWindowResponseTimeoutMs); + while (true) { + token.ThrowIfCancelled(); + DWORD_PTR reply = 0; + SetLastError(ERROR_SUCCESS); + if (SendMessageTimeoutW(window, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, kWindowProbeMessageTimeoutMs, &reply) != 0) { + return WindowResponse::Responds; + } + const DWORD error = GetLastError(); + if (IsWindow(window) == FALSE) { + Fail(kErrorNoSuchWindow, "The requested window is no longer available."); + } + // Refused for another reason, such as UIPI in front of an elevated + // window, so the probe cannot judge the window and leaves it to the + // bounded steps that follow. + if (error != ERROR_TIMEOUT && IsHungAppWindow(window) == FALSE) { + return WindowResponse::Undetermined; + } + if (deadline.Expired()) { + return WindowResponse::NotResponding; + } + token.Wait(50); + } +} + +[[noreturn]] void FailWindowNotResponding() { + throw HelperError(kErrorAutomationFailed, + "The target window did not answer window messages in time, so the helper did not wait on it; the " + "application may be hung or busy.", + "target-non-response"); +} + // Like application activation on macOS, a foreground popup that the window owns keeps the window's activation // context. Re-activating the owner would dismiss that popup with input no user produced; a physical press on the owner // activates it as the user's own press would. @@ -685,14 +726,24 @@ ActivationOutcome ActivateWindowInternal(HWND window, const CancellationToken& t if (window == nullptr || IsWindow(window) == FALSE) { Fail(kErrorNoSuchWindow, "The requested window is no longer available."); } + ActivationOutcome outcome; + if (ProbeWindowResponse(window, token) == WindowResponse::NotResponding) { + outcome.notResponding = true; + return outcome; + } if (IsIconic(window) != FALSE) { - ShowWindow(window, SW_RESTORE); + RestoreWindow(window, token); + } + // Only the target may take the foreground itself, such as for a popup it + // opens on activation; every other process keeps the usual restrictions. + DWORD windowProcess = 0; + GetWindowThreadProcessId(window, &windowProcess); + if (windowProcess != 0) { + AllowSetForegroundWindow(windowProcess); } - AllowSetForegroundWindow(ASFW_ANY); const DWORD currentThread = GetCurrentThreadId(); ForegroundInputAttachment inputAttachment(currentThread); const Deadline deadline(kActivationTimeoutMs); - ActivationOutcome outcome; while (true) { token.ThrowIfCancelled(); const HWND foreground = GetForegroundWindow(); @@ -704,9 +755,15 @@ ActivationOutcome ActivateWindowInternal(HWND window, const CancellationToken& t if (deadline.Expired()) { break; } + if (ProbeWindowResponse(window, token) == WindowResponse::NotResponding) { + outcome.notResponding = true; + break; + } // Foreground ownership only transfers between threads that share an input // queue, so borrow the queue of whichever thread holds the foreground now. - inputAttachment.AttachTo(GetWindowThreadProcessId(foreground != nullptr ? foreground : window, nullptr)); + // A hung holder would block the calls below once its queue is shared. + const HWND holder = foreground != nullptr ? foreground : window; + inputAttachment.AttachTo(IsHungAppWindow(holder) != FALSE ? 0 : GetWindowThreadProcessId(holder, nullptr)); BringWindowToTop(window); SetForegroundWindow(window); SetActiveWindow(window); @@ -842,9 +899,37 @@ void ActivateWindow(HWND window, const CancellationToken& token) { if (outcome.activated) { return; } + if (outcome.notResponding) { + FailWindowNotResponding(); + } Fail(kErrorWindowActivation, DescribeActivationRefusal(ReadActivationRefusal(window, outcome))); } +void RequireWindowResponds(HWND window, const CancellationToken& token) { + if (ProbeWindowResponse(window, token) == WindowResponse::NotResponding) { + FailWindowNotResponding(); + } +} + +bool RestoreWindow(HWND window, const CancellationToken& token) { + // SW_RESTORE returns a maximized window to its normal size, but takes a + // minimized one back to the state it had before, which may be maximized. + const bool wasZoomed = IsZoomed(window) != FALSE; + if (ShowWindowAsync(window, SW_RESTORE) == FALSE) { + return false; + } + const Deadline deadline(kWindowRestoreTimeoutMs); + while (true) { + if (IsIconic(window) == FALSE && !(wasZoomed && IsZoomed(window) != FALSE)) { + return true; + } + if (deadline.Expired()) { + return false; + } + token.Wait(16); + } +} + ApplicationManager::~ApplicationManager() { try { const CancellationToken idle; diff --git a/packages/agentic/desktop-driver/native/windows/src/application.h b/packages/agentic/desktop-driver/native/windows/src/application.h index e1a1426653..68c1bb0325 100644 --- a/packages/agentic/desktop-driver/native/windows/src/application.h +++ b/packages/agentic/desktop-driver/native/windows/src/application.h @@ -162,6 +162,13 @@ std::wstring QuoteWindowsCommandLineArgument(std::wstring_view value); void RequireProcessInHelperSession(DWORD processId, std::string_view failureCode); void ActivateWindow(HWND window, const CancellationToken& token); bool TryActivateWindow(HWND window, const CancellationToken& token); +// Fails with automation-failed and reason `target-non-response` when the +// thread that owns `window` does not answer window messages within a bound. +// A probe that Windows refuses for another reason, such as UIPI, passes. +void RequireWindowResponds(HWND window, const CancellationToken& token); +// Posts SW_RESTORE so a hung window cannot block the helper, then waits a +// bounded time for it. Returns false when the window did not restore in time. +bool RestoreWindow(HWND window, const CancellationToken& token); // What the helper saw when Windows kept the foreground away from a window it // tried to activate. Foreign windows are named only by an opaque handle and diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.cpp b/packages/agentic/desktop-driver/native/windows/src/driver.cpp index 1567546da6..0e43d4acd0 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/driver.cpp @@ -33,6 +33,26 @@ json::Value SerializeRect(const RectD& rect) { return value; } +constexpr unsigned int kPostedMoveSettleMs = 250; + +// Waits until a move posted with SWP_ASYNCWINDOWPOS changes the window frame. +// A window may clamp or refuse a move, so a frame that stays put is accepted +// once the window answers messages; one that stops answering fails instead. +void AwaitPostedMove(HWND window, const RECT& before, const CancellationToken& token) { + const Deadline deadline(kPostedMoveSettleMs); + while (true) { + RECT now{}; + if (GetWindowRect(window, &now) == FALSE || EqualRect(&now, &before) == FALSE) { + return; + } + if (deadline.Expired()) { + RequireWindowResponds(window, token); + return; + } + token.Wait(16); + } +} + std::string RequireString(const json::Value& params, std::string_view key) { const json::Value* value = params.Find(key); if (value == nullptr || !value->IsString() || value->AsString().empty()) { @@ -633,9 +653,16 @@ CommandResult Driver::ExecuteCommand(const std::string& command, const json::Val if (rect == nullptr || !rect->IsObject()) { Fail(kErrorInvalidParams, "The setWindowRect request is missing its rectangle."); } + // Every change below is posted to the window's thread, so a window that + // stops answering leaves the effect unknown instead of blocking the helper. + RequireWindowResponds(window, token); if (IsZoomed(window) != FALSE || IsIconic(window) != FALSE) { - ShowWindow(window, SW_RESTORE); - RecordDelivered(); + RecordDispatching(); + const bool restored = RestoreWindow(window, token); + RecordSettled(S_OK); + if (!restored) { + RequireWindowResponds(window, token); + } } WindowMetrics metrics = MeasureWindow(window); const RectD current = ClientRectInScreenDips(metrics); @@ -656,12 +683,16 @@ CommandResult Driver::ExecuteCommand(const std::string& command, const json::Val // frame an estimate, so converge on the requested client area by measuring. for (int attempt = 0; attempt < 4; attempt += 1) { token.ThrowIfCancelled(); - if (SetWindowPos(window, nullptr, frame.left, frame.top, frame.right - frame.left, frame.bottom - frame.top, - SWP_NOZORDER | SWP_NOACTIVATE) == FALSE) { - FailLastError(kErrorInvalidParams, "Moving the window", GetLastError()); + RECT before{}; + if (GetWindowRect(window, &before) == FALSE || EqualRect(&before, &frame) == FALSE) { + if (SetWindowPos(window, nullptr, frame.left, frame.top, frame.right - frame.left, frame.bottom - frame.top, + SWP_NOZORDER | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS) == FALSE) { + FailLastError(kErrorInvalidParams, "Moving the window", GetLastError()); + } + RecordDispatching(); + AwaitPostedMove(window, before, token); + RecordSettled(S_OK); } - RecordDelivered(); - token.Wait(16); metrics = MeasureWindow(window); const long deltaX = target.left - metrics.clientOrigin.x; const long deltaY = target.top - metrics.clientOrigin.y; diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index c3c622034f..d02c1c61d4 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -959,6 +959,21 @@ class WindowThread { Run([window]() { DestroyWindow(window); }); } + // Blocks the thread until `gate` opens, so its windows stop answering, and + // returns once the thread is blocked. + void Stall(const std::shared_ptr& gate) { + const auto blocked = std::make_shared(); + { + const std::lock_guard guard(mutex_); + tasks_.push_back([gate, blocked]() { + blocked->Open(); + gate->Hold(); + }); + } + PostThreadMessageW(threadId_.load(), kWindowTask, 0, 0); + blocked->Hold(); + } + private: void Run(std::function task) { const auto finished = std::make_shared(); @@ -1731,6 +1746,57 @@ void TestActivationRefusal() { "a refusal with a shared input queue reports that the holder may take the foreground back"); } +// A window whose thread stops pumping messages fails window management with +// target-non-response within the probe's bound instead of blocking the command, +// and a cancelled command stops waiting for it. +void TestHungWindowProbe() { + const CancellationToken token; + WindowThread windows; + const HWND window = windows.Create(L"FurnSelfTestHung", L"Hung window"); + Check(window != nullptr, "the hung-window fixture creates a window"); + if (window == nullptr) { + return; + } + Check(FailureCode([&]() { RequireWindowResponds(window, token); }) == "none", + "a window whose thread pumps messages responds"); + + const auto stall = std::make_shared(); + windows.Stall(stall); + std::string reason = "responded"; + std::string message; + const ULONGLONG started = GetTickCount64(); + try { + RequireWindowResponds(window, token); + } catch (const HelperError& error) { + reason = error.reason(); + message = error.what(); + } + Check(reason == "target-non-response" && Contains(message, "did not answer window messages") && + GetTickCount64() - started < 4000, + "a window that stops answering fails as target-non-response within the probe's bound"); + + CancellationToken cancelled; + std::thread canceller([&cancelled]() { + Sleep(200); + cancelled.Cancel(); + }); + const ULONGLONG cancelStarted = GetTickCount64(); + std::string outcome = "none"; + try { + RequireWindowResponds(window, cancelled); + } catch (const CancelledError&) { + outcome = "cancelled"; + } catch (const HelperError& error) { + outcome = error.code(); + } + canceller.join(); + Check(outcome == "cancelled" && GetTickCount64() - cancelStarted < 1000, + "a cancelled command stops waiting for a window that does not answer"); + stall->Open(); + Check(WaitFor([&]() { return FailureCode([&]() { RequireWindowResponds(window, token); }) == "none"; }), + "the window answers again once its thread resumes"); +} + void TestHelperSessionCheck() { bool accepted = true; try { @@ -3478,6 +3544,7 @@ int RunSelfTest() { TestInputEnvironment(); TestInjectionIntegrity(); TestActivationRefusal(); + TestHungWindowProbe(); TestHelperSessionCheck(); TestInputAuthorityOnThisMachine(); TestInputReevaluation(); From 542d7e7d9027cf2c65672ef5e565754b9ac81959 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:04 +0000 Subject: [PATCH 03/16] Hold the input lock while Windows input stays depressed The cross-process physical input mutex was released after every command even when keys or buttons stayed down, so another helper could inject while our modifiers were held. The worker's exit paths skipped ReleaseInput, and queued commands ran under a token re-armed after the transport ended. A command that leaves input depressed now moves its scope into the driver, which releases it once ReleaseInput leaves nothing down. The worker releases input on every exit and drops queued commands once stopping. Because the lock is already held while input is down, the cancel path's cleanup no longer waits for the mutex. A self-test checks that a held key keeps the lock from another thread. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../native/windows/src/driver.cpp | 27 +++++++++------- .../native/windows/src/driver.h | 24 ++++++++++++-- .../native/windows/src/host.cpp | 8 ++++- .../native/windows/src/selftest.cpp | 32 +++++++++++++++++++ 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.cpp b/packages/agentic/desktop-driver/native/windows/src/driver.cpp index 0e43d4acd0..cb8a6d37cf 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/driver.cpp @@ -470,22 +470,25 @@ CommandResult Driver::EncodeCapture(HWND window, const RECT& cropScreen, bool cr void Driver::ReleaseInput() noexcept { try { - if (!input_.HasDepressedInput()) { - return; - } - const CancellationToken idle; - try { - const PhysicalInputScope scope(idle, kPhysicalInputCleanupWaitMs, AbandonPolicy::Adopt); - input_.ReleaseAll(); - return; - } catch (const HelperError&) { - // Depressed input must never outlive a cancelled command, so recovery - // proceeds even when the cross-process lock could not be acquired. - input_.ReleaseAll(); + if (input_.HasDepressedInput()) { + const CancellationToken idle; + try { + const PhysicalInputScope scope(idle, kPhysicalInputCleanupWaitMs, AbandonPolicy::Adopt); + input_.ReleaseAll(); + } catch (const HelperError&) { + // Depressed input must never outlive a cancelled command, so recovery + // proceeds even when the cross-process lock could not be acquired. + input_.ReleaseAll(); + } } } catch (...) { // Releasing input must never mask the original failure. } + // Input that could not be released keeps the lock, so the thread's exit + // abandons it and the next helper reports the depressed input. + if (!input_.HasDepressedInput()) { + heldInput_.Release(); + } } json::Value Driver::AdoptWindowForTesting(HWND window, const std::wstring& title) { diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.h b/packages/agentic/desktop-driver/native/windows/src/driver.h index 609746b44c..ef83963fe6 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.h +++ b/packages/agentic/desktop-driver/native/windows/src/driver.h @@ -57,6 +57,8 @@ class Driver { public: CommandResult Execute(const std::string& command, const json::Value& params, const CancellationToken& token); + // Releases depressed input and, once nothing stays down, the physical input + // lock held between commands. Call it on the thread that runs commands. void ReleaseInput() noexcept; bool HasDepressedInput() const noexcept { return input_.HasDepressedInput(); } @@ -80,16 +82,19 @@ class Driver { foregroundReader_ = std::move(reader); activator_ = std::move(activator); } + // Replaces the hit test that checks where a pointer press or scroll lands. + void UseTestHitTester(ClickHitTester hitTest) { hitTester_ = hitTest; } private: CommandResult ExecuteCommand(const std::string& command, const json::Value& params, const CancellationToken& token); void ForgetRetiredWindows() noexcept; // Holds the cross-process physical input mutex for a whole action chain and // guarantees that a failed or cancelled chain releases its depressed input - // before the next owner acquires the lock. + // before the next owner acquires the lock. Keys or buttons a chain leaves + // down keep the lock held across commands until they are released. template void RunPhysical(const CancellationToken& token, Fn&& body) { - const PhysicalInputScope scope(token); + PhysicalInputScope scope(token); input_.SyncPointerFromCursor(); try { body(); @@ -101,8 +106,20 @@ class Driver { // Preserve the original failure. } } + HoldInputWhileDepressed(scope); throw; } + HoldInputWhileDepressed(scope); + } + // Called on the thread that runs commands, which owns the mutex. + void HoldInputWhileDepressed(PhysicalInputScope& scope) noexcept { + if (!input_.HasDepressedInput()) { + heldInput_.Release(); + return; + } + if (!heldInput_.owns()) { + heldInput_ = std::move(scope); + } } WindowContext ContextForWindow(const std::string& windowId, HWND& window); @@ -137,8 +154,11 @@ class Driver { CommandJournal journal_; // What the running mutating command has handed to the application. EffectLedger effects_; + // Physical input held between commands while keys or buttons stay down. + PhysicalInputScope heldInput_; ForegroundReader foregroundReader_; WindowActivator activator_; + ClickHitTester hitTester_{&WindowFromPoint}; }; } // namespace furn diff --git a/packages/agentic/desktop-driver/native/windows/src/host.cpp b/packages/agentic/desktop-driver/native/windows/src/host.cpp index e57a83c72f..cebeb2dc51 100644 --- a/packages/agentic/desktop-driver/native/windows/src/host.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/host.cpp @@ -211,7 +211,10 @@ void Host::WorkerLoop() { { std::unique_lock guard(mutex_); signal_.wait(guard, [this] { return stopping_ || !queue_.empty(); }); - if (stopping_ && queue_.empty()) { + if (stopping_) { + // The transport is gone, so nothing can observe queued commands; they + // must not run under a token re-armed after shutdown began. + queue_.clear(); break; } request = std::move(queue_.front()); @@ -261,6 +264,9 @@ void Host::WorkerLoop() { ExitProcess(0); } } + // The physical input lock belongs to this thread, so it is released here + // rather than abandoned when the thread exits. + driver_.ReleaseInput(); winrt::uninit_apartment(); } diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index d02c1c61d4..31cffbf0f0 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -3099,6 +3099,38 @@ void TestDriverMutationEffects() { driver.InputForTesting().UseTestSender(&RecordInput); driver.ReleaseInput(); + // A key left down keeps the cross-process physical input lock until it is + // released, so another helper cannot interleave input with the held key. + const auto acquireElsewhere = []() { + std::string outcome = "unknown"; + std::thread worker([&outcome]() { + const CancellationToken idle; + try { + const PhysicalInputScope scope(idle, 200); + outcome = scope.owns() ? "acquired" : "unowned"; + } catch (const HelperError& error) { + outcome = error.code(); + } catch (const CancelledError&) { + outcome = "cancelled"; + } + }); + worker.join(); + return outcome; + }; + const json::Value holdA = actions(R"([{"type":"key","id":"keyboard","actions":[{"type":"keyDown","value":"a"}]}])"); + Check(FailureCode([&]() { driver.Execute("performActions", holdA, token); }) == "none" && + driver.HasDepressedInput() && acquireElsewhere() == kErrorInputBusy, + "a key left down keeps the physical input lock after its command"); + Check(FailureCode([&]() { driver.Execute("releaseActions", empty, token); }) == "none" && + !driver.HasDepressedInput() && acquireElsewhere() == "acquired", + "releasing the held key releases the physical input lock"); + Check(FailureCode([&]() { driver.Execute("performActions", holdA, token); }) == "none" && + driver.HasDepressedInput(), + "a key is held again for the release recovery"); + driver.ReleaseInput(); + Check(!driver.HasDepressedInput() && acquireElsewhere() == "acquired", + "the release recovery frees the held key and the physical input lock"); + // Abandoned: the provider never returns from the value write. ResetMutationQuarantineForTesting(); const auto gate = std::make_shared(); From a8ec1a024cd618e7cac8b455983ab6ba8ec40bcc Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:04 +0000 Subject: [PATCH 04/16] Refuse pointer presses over another application's window performActions did not check where a pointer press or scroll landed, so either could reach a window of an application the helper never launched or attached to. Before pointerDown and before a scroll moves the pointer, the driver now hit tests the point and fails with element-not-interactable when the window there, or its root, belongs to no leased application. A test hit tester lets the self-test cover a foreign press and scroll. Naming the target window in performActions is a cross-platform protocol change and is left for later. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../native/windows/src/driver.cpp | 23 ++++++++++++++++++- .../native/windows/src/driver.h | 4 ++++ .../native/windows/src/selftest.cpp | 17 ++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.cpp b/packages/agentic/desktop-driver/native/windows/src/driver.cpp index cb8a6d37cf..0149ab3afe 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/driver.cpp @@ -291,6 +291,24 @@ void Driver::ClickPhysically(const std::string& elementId, const CancellationTok input_.ClickAt(center, token); } +void Driver::RequirePointerOverLeasedApplication(POINT point) const { + if (applications_.PrimaryLease() == nullptr) { + return; + } + const HWND hit = hitTester_(point); + if (hit == nullptr || applications_.ForegroundBelongsToApplication(nullptr, hit)) { + return; + } + // A frame host, such as the one around a packaged application, can own the + // top-level window above the application's own content. + const HWND root = GetAncestor(hit, GA_ROOT); + if (root != nullptr && root != hit && applications_.ForegroundBelongsToApplication(nullptr, root)) { + return; + } + Fail(kErrorNotInteractable, "A window of another application, " + DescribeWindowHandle(hit) + + ", covers the pointer position, so the pointer input was not sent."); +} + json::Value Driver::ProbeResult(const json::Value& params) { // Re-read on every probe: a session that is locked now can be unlocked when // the next probe runs. @@ -409,6 +427,7 @@ void Driver::PerformActions(const json::Value& actions, const CancellationToken& if (type == "pointerMove") { input_.MovePointer(ResolveActionPoint(action, token), ActionDuration(action), token); } else if (type == "pointerDown") { + RequirePointerOverLeasedApplication(input_.pointer()); input_.PointerDown(ActionButton(action), token); } else if (type == "pointerUp") { input_.PointerUp(ActionButton(action), token); @@ -423,7 +442,9 @@ void Driver::PerformActions(const json::Value& actions, const CancellationToken& if (type != "scroll") { Fail(kErrorInvalidParams, "Wheel input sources support only scroll and pause."); } - input_.MovePointer(ResolveActionPoint(action, token), 0, token); + const POINT scrollPoint = ResolveActionPoint(action, token); + RequirePointerOverLeasedApplication(scrollPoint); + input_.MovePointer(scrollPoint, 0, token); input_.Wheel(action.NumberField("deltaX", 0.0), action.NumberField("deltaY", 0.0), token); continue; } diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.h b/packages/agentic/desktop-driver/native/windows/src/driver.h index ef83963fe6..78e9790f96 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.h +++ b/packages/agentic/desktop-driver/native/windows/src/driver.h @@ -140,6 +140,10 @@ class Driver { // `window`, or to any leased application when `window` is null. InputController::KeyboardGuard KeyboardGuardFor(HWND window); void ClickPhysically(const std::string& elementId, const CancellationToken& token); + // Refuses a pointer press or scroll whose point a window of an application + // this helper did not launch or attach to covers. Without a lease the + // pointer has no application to stay within, so nothing is checked. + void RequirePointerOverLeasedApplication(POINT point) const; json::Value ProbeResult(const json::Value& params); json::Value WindowSummary(const WindowInfo& info); void PerformActions(const json::Value& actions, const CancellationToken& token); diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index 31cffbf0f0..08e550acdd 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -2269,10 +2269,27 @@ void TestDriverKeyRouting() { reset(); foreground = nullptr; + driver.UseTestHitTester(&HitConfiguredWindow); + clickHitResult = window; Check(perform(R"([{"type":"pointer","id":"mouse","parameters":{"pointerType":"mouse"},"actions":[{"type":"pointerDown","button":0},{"type":"pointerUp","button":0}]}])") == "none" && recordedInputs.size() == 2 && foregroundReads == 0 && activations.empty(), "pointer input neither reads nor changes the foreground"); + if (foreign != nullptr) { + clickHitResult = foreign; + reset(); + std::string covered; + Check(perform(R"([{"type":"pointer","id":"mouse","parameters":{"pointerType":"mouse"},"actions":[{"type":"pointerDown","button":0},{"type":"pointerUp","button":0}]}])", + &covered) == kErrorNotInteractable && + recordedInputs.empty() && !driver.HasDepressedInput() && Contains(covered, "another application"), + "a pointer press over another application's window is refused before it is sent"); + reset(); + Check(perform(R"([{"type":"wheel","id":"wheel","actions":[{"type":"scroll","x":1,"y":1,"deltaX":0,"deltaY":10}]}])") == + kErrorNotInteractable && + recordedInputs.empty(), + "a scroll over another application's window is refused before the pointer moves"); + clickHitResult = window; + } reset(); Check(perform(R"([{"type":"key","id":"keyboard","actions":[{"type":"keyUp","value":"a"}]}])") == "none" && recordedInputs.size() == 1 && foregroundReads == 0, From 59ce14c706d8f8eb2eaa0f8822d7f858053ade52 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:04 +0000 Subject: [PATCH 05/16] Distinguish failed pattern and focus reads from absent ones A failed GetCurrentPatternAs was reported as unsupported-operation and a failed focus read as an unfocused element, hiding stale elements and provider failures behind the wrong error. QueryPattern now returns available, absent, stale, or failed. Pattern actions, accessibility clicks, and value clears report stale-element for a stale element and automation-failed for a failed query, and keep their existing results for an absent pattern. HasKeyboardFocus fails a stale element and returns any other failure to FocusForKeyboard, which reports automation-failed. A self-test clicks an element of a destroyed window. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../native/windows/src/automation.cpp | 104 ++++++++++++------ .../native/windows/src/automation.h | 5 +- .../native/windows/src/driver.cpp | 8 +- .../native/windows/src/selftest.cpp | 3 + 4 files changed, 86 insertions(+), 34 deletions(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/automation.cpp b/packages/agentic/desktop-driver/native/windows/src/automation.cpp index c95f067b49..ce93d4b705 100644 --- a/packages/agentic/desktop-driver/native/windows/src/automation.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/automation.cpp @@ -98,19 +98,55 @@ HRESULT CountedMutation(Fn&& call, std::string_view operation) { return result; } +enum class PatternStatus { Available, Absent, Stale, Failed }; + template -void PerformPatternAction(IUIAutomationElement* element, PATTERNID patternId, HRESULT (STDMETHODCALLTYPE Pattern::*method)(), - const CancellationToken& token) { +struct PatternQuery { + PatternStatus status{PatternStatus::Absent}; + HRESULT result{S_OK}; winrt::com_ptr pattern; - const HRESULT query = element->GetCurrentPatternAs(patternId, IID_PPV_ARGS(pattern.put())); - if (query == UIA_E_ELEMENTNOTAVAILABLE) { +}; + +// Only a pattern the element does not implement is a missing capability; an +// element that went away or a provider that failed must not be reported as one. +template +PatternQuery QueryPattern(IUIAutomationElement* element, PATTERNID patternId) { + PatternQuery query; + query.result = element->GetCurrentPatternAs(patternId, IID_PPV_ARGS(query.pattern.put())); + if (query.result == UIA_E_ELEMENTNOTAVAILABLE) { + query.status = PatternStatus::Stale; + } else if (SUCCEEDED(query.result)) { + query.status = query.pattern ? PatternStatus::Available : PatternStatus::Absent; + } else if (query.result == UIA_E_NOTSUPPORTED || query.result == E_NOINTERFACE || query.result == E_NOTIMPL || + query.result == E_FAIL) { + query.status = PatternStatus::Absent; + } else { + query.status = PatternStatus::Failed; + } + return query; +} + +// Returns whether the element implements the pattern, and fails for a stale +// element or a failed query. +template +bool RequirePatternQuery(const PatternQuery& query, std::string_view operation) { + if (query.status == PatternStatus::Stale) { Fail(kErrorStaleElement, "The element is no longer available."); } - if (query == UIA_E_NOTSUPPORTED || query == E_NOINTERFACE || query == E_NOTIMPL || query == E_FAIL || - (SUCCEEDED(query) && !pattern)) { + if (query.status == PatternStatus::Failed) { + FailHresult(kErrorAutomationFailed, operation, query.result); + } + return query.status == PatternStatus::Available; +} + +template +void PerformPatternAction(IUIAutomationElement* element, PATTERNID patternId, HRESULT (STDMETHODCALLTYPE Pattern::*method)(), + const CancellationToken& token) { + const PatternQuery query = QueryPattern(element, patternId); + if (!RequirePatternQuery(query, "Reading the accessibility action pattern")) { ActionChanged(); } - CheckHresult(kErrorAutomationFailed, "Reading the accessibility action pattern", query); + const winrt::com_ptr pattern = query.pattern; token.ThrowIfCancelled(); const HRESULT result = CountedMutation([pattern, method]() { return (pattern.get()->*method)(); }, "Performing the accessibility action"); @@ -1319,12 +1355,16 @@ void Automation::SetFocus(const std::string& elementId) { } } -bool Automation::HasKeyboardFocus(const std::string& elementId) { +bool Automation::HasKeyboardFocus(const std::string& elementId, HRESULT& failure) { const ElementRecord& record = RequireRecord(elementId); const winrt::com_ptr element = record.element; const auto focused = std::make_shared(FALSE); const HRESULT result = BoundedRead([element, focused]() { return element->get_CurrentHasKeyboardFocus(focused.get()); }, "Reading the element focus state"); + if (result == UIA_E_ELEMENTNOTAVAILABLE) { + Fail(kErrorStaleElement, "Element \"" + elementId + "\" is no longer available."); + } + failure = FAILED(result) ? result : S_OK; return SUCCEEDED(result) && *focused != FALSE; } @@ -1333,27 +1373,30 @@ void Automation::AccessibilityClick(const std::string& elementId, const Cancella const winrt::com_ptr element = record.element; token.ThrowIfCancelled(); - winrt::com_ptr invoke; - if (SUCCEEDED(element->GetCurrentPatternAs(UIA_InvokePatternId, IID_PPV_ARGS(invoke.put()))) && invoke) { + const auto invoke = QueryPattern(element.get(), UIA_InvokePatternId); + if (RequirePatternQuery(invoke, "Reading the element's Invoke pattern")) { + const winrt::com_ptr pattern = invoke.pattern; CheckClickResult("Invoking the element", - CountedMutation([invoke]() { return invoke->Invoke(); }, "Invoking the element")); + CountedMutation([pattern]() { return pattern->Invoke(); }, "Invoking the element")); return; } - winrt::com_ptr toggle; - if (SUCCEEDED(element->GetCurrentPatternAs(UIA_TogglePatternId, IID_PPV_ARGS(toggle.put()))) && toggle) { + const auto toggle = QueryPattern(element.get(), UIA_TogglePatternId); + if (RequirePatternQuery(toggle, "Reading the element's Toggle pattern")) { + const winrt::com_ptr pattern = toggle.pattern; CheckClickResult("Toggling the element", - CountedMutation([toggle]() { return toggle->Toggle(); }, "Toggling the element")); + CountedMutation([pattern]() { return pattern->Toggle(); }, "Toggling the element")); return; } - winrt::com_ptr selection; - if (SUCCEEDED(element->GetCurrentPatternAs(UIA_SelectionItemPatternId, IID_PPV_ARGS(selection.put()))) && - selection) { + const auto selection = QueryPattern(element.get(), UIA_SelectionItemPatternId); + if (RequirePatternQuery(selection, "Reading the element's SelectionItem pattern")) { + const winrt::com_ptr pattern = selection.pattern; CheckClickResult("Selecting the element", - CountedMutation([selection]() { return selection->Select(); }, "Selecting the element")); + CountedMutation([pattern]() { return pattern->Select(); }, "Selecting the element")); return; } - winrt::com_ptr expand; - if (SUCCEEDED(element->GetCurrentPatternAs(UIA_ExpandCollapsePatternId, IID_PPV_ARGS(expand.put()))) && expand) { + const auto expandQuery = QueryPattern(element.get(), UIA_ExpandCollapsePatternId); + if (RequirePatternQuery(expandQuery, "Reading the element's ExpandCollapse pattern")) { + const winrt::com_ptr expand = expandQuery.pattern; const auto state = std::make_shared(ExpandCollapseState_Collapsed); CheckHresult(kErrorAutomationFailed, "Reading the expand state", BoundedRead([expand, state]() { return expand->get_CurrentExpandCollapseState(state.get()); }, @@ -1367,8 +1410,10 @@ void Automation::AccessibilityClick(const std::string& elementId, const Cancella "Expanding or collapsing the element")); return; } - winrt::com_ptr legacy; - if (SUCCEEDED(element->GetCurrentPatternAs(UIA_LegacyIAccessiblePatternId, IID_PPV_ARGS(legacy.put()))) && legacy) { + const auto legacyQuery = + QueryPattern(element.get(), UIA_LegacyIAccessiblePatternId); + if (RequirePatternQuery(legacyQuery, "Reading the element's LegacyIAccessible pattern")) { + const winrt::com_ptr legacy = legacyQuery.pattern; CheckClickResult("Invoking the legacy accessibility default action", CountedMutation([legacy]() { return legacy->DoDefaultAction(); }, "Invoking the legacy accessibility default action")); @@ -1442,21 +1487,18 @@ void Automation::PerformAccessibilityAction(const std::string& elementId, const bool Automation::TryClearValue(const std::string& elementId, bool strict) { const ElementRecord& record = RequireRecord(elementId); - winrt::com_ptr value; - const HRESULT query = record.element->GetCurrentPatternAs(UIA_ValuePatternId, IID_PPV_ARGS(value.put())); - if (FAILED(query) || !value) { + const auto query = QueryPattern(record.element.get(), UIA_ValuePatternId); + if (query.status != PatternStatus::Available) { + // Without a strict request the keyboard clear is the fallback, and it + // reports a stale element itself. if (!strict) { return false; } - if (query == UIA_E_ELEMENTNOTAVAILABLE) { - Fail(kErrorStaleElement, "The element is no longer available."); - } - if (SUCCEEDED(query) || query == UIA_E_NOTSUPPORTED || query == E_NOINTERFACE || query == E_NOTIMPL || - query == E_FAIL) { + if (!RequirePatternQuery(query, "Reading the element value pattern")) { Fail(kErrorNotInteractable, "The element does not expose a writable value to clear."); } - FailHresult(kErrorAutomationFailed, "Reading the element value pattern", query); } + const winrt::com_ptr value = query.pattern; const auto readOnly = std::make_shared(TRUE); const HRESULT state = BoundedRead([value, readOnly]() { return value->get_CurrentIsReadOnly(readOnly.get()); }, "Reading the element value state"); diff --git a/packages/agentic/desktop-driver/native/windows/src/automation.h b/packages/agentic/desktop-driver/native/windows/src/automation.h index 8f0a5c9565..163a586873 100644 --- a/packages/agentic/desktop-driver/native/windows/src/automation.h +++ b/packages/agentic/desktop-driver/native/windows/src/automation.h @@ -110,7 +110,10 @@ class Automation { // as not interactable before anything is dispatched, and a failed write // fails with the effect the ledger recorded. bool TryClearValue(const std::string& elementId, bool strict = false); - bool HasKeyboardFocus(const std::string& elementId); + // Whether the element has keyboard focus. A stale element fails as stale; any + // other failed read answers false and leaves its HRESULT in `failure`, so a + // caller that polls can report the failure instead of "unfocused". + bool HasKeyboardFocus(const std::string& elementId, HRESULT& failure); void ForgetWindow(const std::string& windowId); void Reset(); diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.cpp b/packages/agentic/desktop-driver/native/windows/src/driver.cpp index 0149ab3afe..65c762dff4 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/driver.cpp @@ -259,10 +259,14 @@ HWND Driver::FocusForKeyboard(const std::string& elementId, const CancellationTo // foreground fails here instead of typing into whatever is. ActivateWindow(window, token); automation_.SetFocus(elementId); - for (int attempt = 0; attempt < 10 && !automation_.HasKeyboardFocus(elementId); attempt += 1) { + HRESULT focusFailure = S_OK; + for (int attempt = 0; attempt < 10 && !automation_.HasKeyboardFocus(elementId, focusFailure); attempt += 1) { token.Wait(20); } - if (!automation_.HasKeyboardFocus(elementId)) { + if (!automation_.HasKeyboardFocus(elementId, focusFailure)) { + if (FAILED(focusFailure)) { + FailHresult(kErrorAutomationFailed, "Reading the element focus state", focusFailure); + } Fail(kErrorNotInteractable, "The element did not take keyboard focus, so keys were not sent."); } return window; diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index 08e550acdd..235fb1c46a 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -2650,7 +2650,10 @@ void TestAutomationElementIdentity() { const std::vector fifth = automation.SnapshotWindowTree(context, token); const std::string lastEditId = idOfRole(fifth, "textbox"); + const std::string lastButtonId = idOfRole(fifth, "button"); windows.Destroy(window); + Check(FailureCode([&]() { automation.AccessibilityClick(lastButtonId, token); }) == kErrorStaleElement, + "an accessibility click on an element of a destroyed window answers stale-element, not unsupported"); Check(FailureCode([&]() { automation.SnapshotElement(lastEditId, token); }, &message) == kErrorStaleElement, "an element of a destroyed window answers stale-element"); automation.ForgetWindow(context.windowId); From 8a68056006f395091ca006c5490130ce0784d74e Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:05 +0000 Subject: [PATCH 06/16] Verify Windows launch identity before cleanup and leasing A successful launch posted WM_CLOSE to and terminated a still-running launch root even when the Job Object owned it, a packaged root was trusted by process id alone, and an unreadable start time serialized as 1601-01-01, which the incarnation probe reads as exited. Under a containment job the launch root is now left to the job. A packaged root must report the requested AUMID before it is tracked or terminated, and ActivateApplication uses AO_NOERRORUI. Lease creation fails when the start time cannot be read, and a zero FILETIME serializes as an empty string. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../native/windows/src/application.cpp | 32 +++++++++++++++---- .../native/windows/src/common.cpp | 5 +-- .../native/windows/src/selftest.cpp | 1 + 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/application.cpp b/packages/agentic/desktop-driver/native/windows/src/application.cpp index 5076fe1bd1..6d6baa0082 100644 --- a/packages/agentic/desktop-driver/native/windows/src/application.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/application.cpp @@ -1087,8 +1087,11 @@ ApplicationLease& ApplicationManager::CreateLease(std::string ownership, DWORD p lease.processId = processId; lease.primaryWindow = primaryWindow; lease.windowTitle = title; - if (process != nullptr) { - TryReadProcessStartTime(process, lease.startTime); + // The start time proves which incarnation of the process the lease names; a + // lease without it would later read as a process that already exited. + if (process != nullptr && !TryReadProcessStartTime(process, lease.startTime)) { + Fail(lease.ownership == "launched" ? kErrorLaunchFailed : kErrorAttachFailed, + "The leased application process start time could not be read."); } const std::string id = lease.id; leases_.emplace(id, std::move(lease)); @@ -1207,7 +1210,7 @@ const ApplicationLease& ApplicationManager::Launch(const json::Value& params, co CoAllowSetForegroundWindow(manager.get(), nullptr); CheckHresult(kErrorLaunchFailed, "Activating the packaged application", manager->ActivateApplication(aumid.c_str(), arguments.empty() ? nullptr : arguments.c_str(), - AO_NONE, &launchRoot.processId)); + AO_NOERRORUI, &launchRoot.processId)); launchRoot.process.attach( OpenProcess(kOwnedProcessAccess, FALSE, launchRoot.processId)); if (!launchRoot.process) { @@ -1222,6 +1225,14 @@ const ApplicationLease& ApplicationManager::Launch(const json::Value& params, co Fail(kErrorLaunchFailed, "Packaged activation reused a pre-existing process. Use attach mode instead."); } + // The root anchors ancestry and is ended on failure, so a process that + // does not prove the requested identity, such as one that reused the + // activated process id, is never trusted, tracked, or terminated. + if (!ProcessMatchesLaunchIdentity(launchRoot.process.get(), aumid, executablePath)) { + Fail(kErrorLaunchFailed, + "The activated process does not report the requested application identity, so the helper left " + "it running and did not lease it."); + } launchRoot.trusted = true; winrt::handle cleanupProcess = DuplicateProcessHandle(launchRoot.process.get()); if (!cleanupProcess) { @@ -1353,10 +1364,17 @@ const ApplicationLease& ApplicationManager::Launch(const json::Value& params, co if (!owned || !IsProcessAlive(owned.get())) { Fail(kErrorLaunchFailed, "The launched application process exited before its lease was created."); } - const std::string extraCleanupFailure = launchProcesses.Cleanup(); - if (!extraCleanupFailure.empty()) { - launchProcesses.Track(matchedProcessId, std::move(owned)); - Fail(kErrorCleanupFailed, "A secondary launch process could not be cleaned up: " + extraCleanupFailure); + if (containmentJob) { + // The job already owns a launch root that differs from the matched + // process and ends it with the lease, so it is neither closed nor + // terminated here. + launchProcesses.ReleaseRemaining(); + } else { + const std::string extraCleanupFailure = launchProcesses.Cleanup(); + if (!extraCleanupFailure.empty()) { + launchProcesses.Track(matchedProcessId, std::move(owned)); + Fail(kErrorCleanupFailed, "A secondary launch process could not be cleaned up: " + extraCleanupFailure); + } } const char* containment = containmentJob ? kContainmentJobObject : kContainmentUncontained; const ApplicationLease& lease = CreateLease("launched", matchedProcessId, owned.get(), matched, diff --git a/packages/agentic/desktop-driver/native/windows/src/common.cpp b/packages/agentic/desktop-driver/native/windows/src/common.cpp index f773a65c80..bc490ed2da 100644 --- a/packages/agentic/desktop-driver/native/windows/src/common.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/common.cpp @@ -235,11 +235,12 @@ std::uint64_t FileTimeTicks(const FILETIME& value) { } std::string FormatFileTimeIso8601(const FILETIME& value) { + // A zero time is an unread one, not 1601-01-01. + const std::uint64_t ticks = FileTimeTicks(value); SYSTEMTIME system{}; - if (!FileTimeToSystemTime(&value, &system)) { + if (ticks == 0 || !FileTimeToSystemTime(&value, &system)) { return {}; } - const std::uint64_t ticks = FileTimeTicks(value); const unsigned int fraction = static_cast(ticks % 10000000ull); std::array buffer{}; std::snprintf(buffer.data(), buffer.size(), "%04u-%02u-%02uT%02u:%02u:%02u.%07uZ", system.wYear, system.wMonth, diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index 235fb1c46a..30a3d9d807 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -358,6 +358,7 @@ void TestLeaseIdentity() { FILETIME parsed{}; Check(TryParseIso8601("2026-08-31T21:59:59.1234567Z", parsed), "lease timestamps parse"); Check(FormatFileTimeIso8601(parsed) == "2026-08-31T21:59:59.1234567Z", "lease timestamps round trip"); + Check(FormatFileTimeIso8601(FILETIME{}).empty(), "an unknown start time serializes as empty, not as 1601"); FILETIME offset{}; Check(TryParseIso8601("2026-08-31T23:59:59.0000000+02:00", offset), "offset timestamps parse"); Check(FileTimeTicks(offset) == FileTimeTicks(parsed) - 1234567, "offset timestamps normalize to UTC"); From df5889c7f4c74594ca78971d325219784c2ef3f6 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:05 +0000 Subject: [PATCH 07/16] Keep each tracked window's process on event-table overflow When the known-window table overflowed, every tracked window was re-added with the process id of the window being remembered, mislabeling later destroy events. Tracked windows now keep the process they were recorded for. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../desktop-driver/native/windows/src/events.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/events.cpp b/packages/agentic/desktop-driver/native/windows/src/events.cpp index a4f8c43d8d..7876f47ebd 100644 --- a/packages/agentic/desktop-driver/native/windows/src/events.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/events.cpp @@ -427,11 +427,16 @@ void WindowEventMonitor::RememberWindow(HWND window, DWORD processId) { const std::lock_guard guard(stateMutex_); if (knownWindows_.size() >= kMaximumKnownWindows) { // Past the bound, windows seen earlier lose their destroy events, which - // only narrows the advisory timeline; tracked windows are re-added below. - knownWindows_.clear(); + // only narrows the advisory timeline; tracked windows keep their entry, + // each with the process it was recorded for. + std::unordered_map retained; for (const auto& tracked : trackedByWindow_) { - knownWindows_[tracked.first] = processId; + const auto known = knownWindows_.find(tracked.first); + if (known != knownWindows_.end()) { + retained.emplace(known->first, known->second); + } } + knownWindows_ = std::move(retained); } knownWindows_[window] = processId; } From 6dfda9c311e4f5b8ca61dba081efcad8521847b8 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:05 +0000 Subject: [PATCH 08/16] Fail the Windows transport on malformed JSON The protocol says invalid JSON fails the transport, but the helper logged and skipped the frame. A skipped frame may have been a request or a cancel, so Node could wait on an id the helper never saw. The host now writes a diagnostic and exits with code 1 without reading further frames. A self-test sends a malformed frame between two requests and checks the exit code and that the later request never runs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../native/windows/src/host.cpp | 9 ++- .../native/windows/src/selftest.cpp | 70 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/host.cpp b/packages/agentic/desktop-driver/native/windows/src/host.cpp index cebeb2dc51..a612a2a58b 100644 --- a/packages/agentic/desktop-driver/native/windows/src/host.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/host.cpp @@ -305,8 +305,13 @@ int Host::Run(HANDLE input, HANDLE output) { message = json::Value::Parse( std::string_view(reinterpret_cast(frame.payload.data()), frame.payload.size())); } catch (const HelperError& error) { - WriteDiagnostic(std::string("The native helper ignored malformed JSON: ") + error.what()); - continue; + // The protocol fails the transport on invalid JSON: a frame that does + // not parse may have carried a request or cancel the helper cannot + // correlate, so continuing would leave Node waiting on it. + WriteDiagnostic(std::string("The native helper received malformed JSON, which fails the transport: ") + + error.what()); + exitCode = 1; + break; } const std::string type = message.StringField("type"); const std::string id = message.StringField("id"); diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index 30a3d9d807..f89dfa71aa 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -3285,6 +3285,75 @@ void TestHostQueuedCancellation() { CloseHandle(testInput); } +// The protocol fails the transport on a frame that is not valid JSON: the host +// stops reading, never runs what follows, and exits with a failure. +void TestHostMalformedJson() { + HANDLE hostInput = nullptr; + HANDLE testOutput = nullptr; + HANDLE testInput = nullptr; + HANDLE hostOutput = nullptr; + if (!CreatePipe(&hostInput, &testOutput, nullptr, 0) || !CreatePipe(&testInput, &hostOutput, nullptr, 0)) { + Check(false, "the malformed-frame test creates its pipes"); + return; + } + + std::mutex mutex; + std::vector executed; + const auto blocker = std::make_shared(); + Host host; + host.SetBeforeExecuteForTesting([&](const std::string& id, const std::string&) { + { + const std::lock_guard guard(mutex); + executed.push_back(id); + } + if (id == "blocker") { + blocker->Hold(); + } + }); + std::thread reader([&]() { + try { + FrameReader frames(testInput); + Frame frame; + while (frames.Read(frame)) { + } + } catch (...) { + } + }); + std::atomic exitCode{-1}; + std::thread server([&]() { exitCode.store(host.Run(hostInput, hostOutput)); }); + + FrameWriter writer(testOutput); + const auto request = [&writer](const std::string& id) { + json::Value message = json::Value::Object(); + message.Set("type", json::Value::String(std::string("request"))); + message.Set("id", json::Value::String(id)); + message.Set("command", json::Value::String(std::string("probe"))); + message.Set("params", json::Value::Object()); + writer.WriteJson(message); + }; + const auto ran = [&](const std::string& id) { + const std::lock_guard guard(mutex); + return std::find(executed.begin(), executed.end(), id) != executed.end(); + }; + + request("blocker"); + Check(WaitFor([&]() { return ran("blocker"); }), "the host worker starts the request sent before the bad frame"); + writer.WriteJson(std::string_view(R"({"type":"request","id":"truncated")")); + request("after"); + blocker->Open(); + const bool returned = WaitFor([&]() { return exitCode.load() != -1; }, 5000); + Check(returned && exitCode.load() == 1, "a malformed frame fails the transport and the host exits with a failure"); + Check(!ran("after"), "a request after the malformed frame is never run"); + + // Ends a host that kept reading, so a failed check cannot hang the run. + CloseHandle(testOutput); + server.join(); + CloseHandle(hostOutput); + reader.join(); + CloseHandle(hostInput); + CloseHandle(testInput); +} + void TestDriverHitTestBeyondScreens() { const CancellationToken token; WindowThread windows; @@ -3618,6 +3687,7 @@ int RunSelfTest() { TestEffectLedger(); TestDriverMutationEffects(); TestHostQueuedCancellation(); + TestHostMalformedJson(); TestDriverHitTestBeyondScreens(); TestAccessibilityActions(); TestDriverObservation(); From ec43531f5a97124d7579322a2228c23adda23f08 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:56:08 +0000 Subject: [PATCH 09/16] Document Windows helper cancellation and window probing The README now describes the posted window management, the target-non-response probe, the input lock held while input is down, pointer hit checks, launch cleanup under a job, and cancellation, and corrects the close sequence and the build-warnings claim. PROTOCOL.md widens target-non-response to window messages, and remaining-work.md records what is still unbounded. Adds the patch changeset. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .changeset/win-helper-cancellation.md | 5 + .../agentic/desktop-driver/native/PROTOCOL.md | 3 +- .../desktop-driver/native/windows/README.md | 97 ++++++++++++++----- .../references/remaining-work.md | 22 ++++- 4 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 .changeset/win-helper-cancellation.md diff --git a/.changeset/win-helper-cancellation.md b/.changeset/win-helper-cancellation.md new file mode 100644 index 0000000000..6077a8aebd --- /dev/null +++ b/.changeset/win-helper-cancellation.md @@ -0,0 +1,5 @@ +--- +'@fluentui-react-native/desktop-driver': patch +--- + +Make the Windows helper acknowledge cancellation promptly: bounded UI Automation calls, the whole-subtree cache, window restore, move, and activation, and `closeApplication` observe cancellation, and a cancelled mutation reports effect `unknown`. Window management and activation probe a hung target and fail with reason `target-non-response` instead of blocking. Held keys and buttons keep the physical input lock until released, and queued commands are dropped after the transport ends. A malformed JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. diff --git a/packages/agentic/desktop-driver/native/PROTOCOL.md b/packages/agentic/desktop-driver/native/PROTOCOL.md index 8e2e2f15c4..a976009a5a 100644 --- a/packages/agentic/desktop-driver/native/PROTOCOL.md +++ b/packages/agentic/desktop-driver/native/PROTOCOL.md @@ -170,7 +170,8 @@ Node maps helper codes onto the public WebDriver error contract. `data.reason`, when a provider sets it, is a short lowercase token a caller can branch on instead of parsing `message`. A provider sets `target-non-response` on `automation-failed` when the target application did not answer an accessibility -request in time, such as a hung, busy, or exiting application: the request was +request, or a window message sent before restoring, moving, or activating its +window, in time, such as a hung, busy, or exiting application: the request was well formed, a read leaves nothing to settle, and a retry can succeed once the application answers. A provider that cannot tell the cause omits `reason`. diff --git a/packages/agentic/desktop-driver/native/windows/README.md b/packages/agentic/desktop-driver/native/windows/README.md index c955b7b003..8394cafbba 100644 --- a/packages/agentic/desktop-driver/native/windows/README.md +++ b/packages/agentic/desktop-driver/native/windows/README.md @@ -9,7 +9,7 @@ third-party native dependency. | Source | Responsibility | | -------------------------------- | ------------------------------------------------------------------------- | -| `DesktopDriverHost.vcxproj` | Warning-clean MSBuild project, C++20, static CRT | +| `DesktopDriverHost.vcxproj` | MSBuild project, C++20, warning level 4, static CRT | | `src/host.*` | FDR1 request loop, cancellation, and command dispatch | | `src/application.*` | Launch/attach leases, process and window ownership | | `src/automation.*` | UI Automation tree, lookup, state, and opaque identities | @@ -58,6 +58,8 @@ and invokes the checked-in project with: - x64 platform; - Debug or Release configuration; - `/MT` static runtime; +- warning level 4, without `/WX`, Control Flow Guard, or `/sdl`, so a warning + does not fail the build; - no restore; - external intermediate and output directories; - generated source/build identity outside the package. @@ -106,21 +108,31 @@ when forced termination cannot be proven. Launch and attach require the helper's window station to be interactive. An unpackaged launch is containment `job-object`: the helper creates the process suspended, assigns it to a Job Object of its own with -`JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE` and no breakaway, and only then resumes -it, so every descendant starts inside the job. A failed assignment ends the +`JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE` and no breakaway, and only then resumes it, +so every descendant starts inside the job. A failed assignment ends the suspended process and fails the launch as unsupported instead of running it uncontained. Window discovery then accepts only windows of job members in the -helper's session. Closing the lease asks the root window to close, then ends -the job and waits up to 3 seconds for it to drain; an undrained job fails -`cleanup-failed` and keeps the lease so the close can be retried. If the helper -exits without closing, the operating system ends the job. Descendants are ended -by the job, not asked to close, and an application that starts children with -`CREATE_BREAKAWAY_FROM_JOB` fails inside the job. A packaged activation is -containment `uncontained`: the Application Activation Manager starts it outside -any job the helper owns, so closing the lease ends only the identity-verified -process whose window the helper leased (the activated process or a verified -descendant); other processes the application starts are not ended. -The helper never places an attached application in a job. +helper's session. When the leased window belongs to a descendant, a launch root +that is still running stays in the job and ends with the lease; the helper does +not close or terminate it after a successful launch. Closing the lease posts +`WM_CLOSE` to the leased process's windows and waits up to 5 seconds for it to +exit, then ends the job and waits up to 3 seconds for it to drain; an undrained +job fails `cleanup-failed` and keeps the lease so the close can be retried. If +the helper exits without closing, the operating system ends the job. Descendants +are ended by the job, not asked to close, and an application that starts +children with `CREATE_BREAKAWAY_FROM_JOB` fails inside the job. A packaged +activation is containment `uncontained`: the Application Activation Manager +starts it outside any job the helper owns, so closing the lease ends only the +identity-verified process whose window the helper leased (the activated process +or a verified descendant); other processes the application starts are not ended. +Closing it posts `WM_CLOSE`, waits up to 5 seconds, then terminates the process +and waits up to 2 more seconds for it to exit; a cancelled close stops waiting +and keeps the lease. Activation passes `AO_NOERRORUI`, so a failed activation +reports an error instead of showing one, and the activated process must report +the requested AUMID before the helper trusts it as the launch root, tracks it, +or ends it. A lease is created only when the leased process's start time can be +read, because that start time is what later tells the same process from a reused +process id. The helper never places an attached application in a job. ## Automation behavior @@ -174,6 +186,20 @@ holder by opaque window handle and process id, never by title or class, and the first matching cause: no foreground window, another window of the target application, an elevated or unreadable holder, a holder that is not responding, a refused input-queue attachment, or a holder that took the foreground back. +Only the target's process may take the foreground itself during activation +(`AllowSetForegroundWindow` with its process id); other processes keep the usual +restriction. + +Restoring, moving, resizing, and activating a window wait on the thread that +owns it. Before each, the helper sends the window `WM_NULL` through +`SendMessageTimeoutW` with `SMTO_ABORTIFHUNG`; a window that does not answer +within 2 seconds fails as `automation-failed` with reason `target-non-response` +instead of holding the helper and the physical input lock. The restore and move +themselves are posted (`ShowWindowAsync`, `SWP_ASYNCWINDOWPOS`), activation does +not share the input queue of a thread that Windows reports as hung, and every +wait observes cancellation. A probe that Windows refuses for another reason, +such as User Interface Privilege Isolation, does not judge the window and leaves +it to the bounded steps that follow. A subtree walk retries while its root remains available if a descendant becomes unavailable, or if a closing popup island's provider reports a UI @@ -252,15 +278,22 @@ actions. A target at a higher integrity level than the helper, or whose token cannot be read because access is denied, is unavailable; other read errors defer to the later window and element checks. A click is refused as `element-not-interactable` when another application's window covers the click -point. A `SendInput` call that injects fewer events than requested fails as -`input-failed` and names UIPI, a secure desktop, or blocked input. +point, and so is a `performActions` `pointerDown` or `scroll` whose point is +covered by a window that neither belongs to a leased application nor is the +top-level window around one. Keys or buttons that `performActions` leaves down +keep the session's physical input lock until they are released, by +`releaseActions`, a later command, cancellation, or the helper's exit, so +another helper cannot interleave its input with them. A `SendInput` call that +injects fewer events than requested fails as `input-failed` and names UIPI, a +secure desktop, or blocked input. `click` mode `auto` falls back to accessibility activation when physical input is unavailable, and an explicit `physical` request reports `input-unavailable`. Element `sendKeys` and the keyboard `clear` (the fallback, or a `keyboard` -mechanism) activate the target window -and verify keyboard focus before sending, which is stricter than the macOS -`clear`; `performActions` activates only when the foreground belongs to another +mechanism) activate the target window and verify keyboard focus before sending +(a focus read that fails reports `automation-failed`, not +`element-not-interactable`), which is stricter than the macOS `clear`; +`performActions` activates only when the foreground belongs to another application. The helper has no `uiAccess` manifest, and the occlusion check detects only windows of other applications. @@ -374,10 +407,20 @@ reports from a mutation is treated the same way, because the call reached the application and its effect is unknown. An explicit rejection, such as a disabled element, is an ordinary error and does not quarantine. +The bounded wait observes the command's cancellation every 50 ms, as does the +wait for a whole-subtree cache, so a cancelled command is acknowledged promptly +even against a hung provider. A cancelled read simply stops waiting. A cancelled +mutation is abandoned and quarantined exactly like one that timed out, so its +effect is `unknown` and the refusal says it was cancelled before it settled. + Click tries `Invoke`, `Toggle`, `SelectionItem`, `ExpandCollapse`, then `LegacyIAccessible`. On Win32 controls `Invoke` is posted: the provider accepts the call and the application applies the effect afterwards, so callers poll for -the visible result. +the visible result. Only a pattern the element does not implement moves on to +the next one, and only an element without any of them is unsupported; a pattern +query that fails otherwise fails as `automation-failed`, and one for an element +that went away as `stale-element`. Named accessibility actions and a `semantic` +clear classify their pattern queries the same way. While quarantined, `click`, `clear`, `sendKeys`, `performActions`, `performAccessibilityAction`, `activate`, @@ -423,11 +466,13 @@ point they are dispatched: `Select`, `AddToSelection`, `RemoveFromSelection`, `ScrollIntoView`, `Expand`/`Collapse`, `DoDefaultAction`, `SetValue`), bracketed by `RecordDispatching()` and `RecordSettled()`; +- each window restore and move that `setWindowRect` posts, bracketed the same + way, so a window that stops answering before the change lands leaves the + effect `unknown`; - every key, button, and wheel event that `SendInput` inserts into the input - stream, a posted `WM_CLOSE`, and each window restore or `SetWindowPos` that - succeeds, through `RecordDelivered()`. `SendInput` reports only completed - injection, not how the application handled the events, so any insertion, - even part of a refused batch, counts toward `partial`. + stream, and a posted `WM_CLOSE`, through `RecordDelivered()`. `SendInput` + reports only completed injection, not how the application handled the events, + so any insertion, even part of a refused batch, counts toward `partial`. A pattern call that succeeds counts toward `partial`. One that returns a UI Automation rejection (`UIA_E_ELEMENTNOTENABLED`, `UIA_E_ELEMENTNOTAVAILABLE`, @@ -510,8 +555,8 @@ yarn test --runInBand ``` The contract builds and resolves the helper, validates cache reuse and -handshakes, runs the native self-test, exercises malformed-frame recovery, and -verifies shared Windows/Win32 selection. A second suite builds the test-only +handshakes, runs the native self-test, checks that a malformed frame fails the +transport, and verifies shared Windows/Win32 selection. A second suite builds the test-only Win32 fixture (`fixtures/win32` in the repository, not published) and drives it through the helper: window events, owned popups, hung and stuck providers, large trees, bounded retention, and application containment (a contained launch whose diff --git a/packages/agentic/desktop-driver/references/remaining-work.md b/packages/agentic/desktop-driver/references/remaining-work.md index 742a6a4603..2aec49ed6b 100644 --- a/packages/agentic/desktop-driver/references/remaining-work.md +++ b/packages/agentic/desktop-driver/references/remaining-work.md @@ -594,6 +594,23 @@ the 5,000-node cap: about 0.4 ms per node, so a 4,900-node tree reads in about 2 seconds and a 10,000-node tree fails after about 4. Section 11 records the evidence and what did not run. +**Windows cancellation progress:** a cancelled command stops waiting within a +quarter of a second in bounded UI Automation calls and the whole-subtree cache +(50 ms slices), window response probes (200 ms messages), posted restores and +moves, activation, and `closeApplication`, and a cancelled mutation reports +`unknown`. The physical input lock stays held +across commands only while keys or buttons stay down, so the cancel path does +not wait for it. Remaining: direct UI Automation calls outside the bounded worker +(pattern queries, `ElementFromHandle`, `observe` paging, the tree walker) are +bounded only by the client's connection and transaction timeouts; +`SetForegroundWindow`, `SetActiveWindow`, and `BringWindowToTop` can still block +on a target that hangs after its probe; packaged `ActivateApplication` is +synchronous; an abandoned physical input lock is reported to one acquirer only, +not until `--release-input` clears it; `performActions` names no window, so its +pointer hit check accepts any leased application; and Caps Lock state and the +keyboard layout for injected keys come from the helper thread, not the +foreground thread. + Operation-time permission and capability changes must be surfaced truthfully even if session-start feature negotiation previously succeeded. The Windows input authority rechecks session, foreground, and integrity for every physical @@ -1426,8 +1443,9 @@ Settlement effect items **not run** on Windows: the delivered effect of `closeWindow` and `setWindowRect` after a later failure, a `click` abandoned on a genuinely hung provider (a Win32 button's UI Automation `Invoke` is posted and never blocks), and real-application UI -Automation failures, so the rejection list is unqualified. `SetWindowPos` on a -hung window owned by another thread is still not bounded. +Automation failures, so the rejection list is unqualified. `setWindowRect` now +probes the window and posts its restore and move, but only an in-process stalled +window thread is self-tested; a hung window of another process is not run. Queued cancellation check on macOS (matching the Windows Host follow-up): the macOS helper used to report `none` when a queued read or lifecycle command was From 8c53e2d7fa86b676e3a22c161d8ad4cd3bf08fdc Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:58:52 +0000 Subject: [PATCH 10/16] Fail the Windows transport on non-object JSON frames PROTOCOL.md defines every JSON message as an object with a `type`, and the macOS helper now fails the transport on a frame that is not one. The Windows host logged and skipped such a frame, so the two helpers disagreed. It now writes a diagnostic and exits with code 1, and the malformed-frame self-test also runs with a JSON array. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../native/windows/src/host.cpp | 5 +++++ .../native/windows/src/selftest.cpp | 20 ++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/agentic/desktop-driver/native/windows/src/host.cpp b/packages/agentic/desktop-driver/native/windows/src/host.cpp index a612a2a58b..a593f126a9 100644 --- a/packages/agentic/desktop-driver/native/windows/src/host.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/host.cpp @@ -313,6 +313,11 @@ int Host::Run(HANDLE input, HANDLE output) { exitCode = 1; break; } + if (!message.IsObject()) { + WriteDiagnostic("The native helper received a JSON frame that is not an object, which fails the transport."); + exitCode = 1; + break; + } const std::string type = message.StringField("type"); const std::string id = message.StringField("id"); if ((type == "cancel" || type == "request") && diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index f89dfa71aa..a8866288f0 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -3285,15 +3285,16 @@ void TestHostQueuedCancellation() { CloseHandle(testInput); } -// The protocol fails the transport on a frame that is not valid JSON: the host -// stops reading, never runs what follows, and exits with a failure. -void TestHostMalformedJson() { +// The protocol fails the transport on a frame that is not valid JSON or not a +// JSON object: the host stops reading, never runs what follows, and exits with +// a failure. +void TestHostRejectsFrame(std::string_view rejected, const std::string& subject) { HANDLE hostInput = nullptr; HANDLE testOutput = nullptr; HANDLE testInput = nullptr; HANDLE hostOutput = nullptr; if (!CreatePipe(&hostInput, &testOutput, nullptr, 0) || !CreatePipe(&testInput, &hostOutput, nullptr, 0)) { - Check(false, "the malformed-frame test creates its pipes"); + Check(false, "the " + subject + " test creates its pipes"); return; } @@ -3337,13 +3338,13 @@ void TestHostMalformedJson() { }; request("blocker"); - Check(WaitFor([&]() { return ran("blocker"); }), "the host worker starts the request sent before the bad frame"); - writer.WriteJson(std::string_view(R"({"type":"request","id":"truncated")")); + Check(WaitFor([&]() { return ran("blocker"); }), "the host worker starts the request sent before the " + subject); + writer.WriteJson(rejected); request("after"); blocker->Open(); const bool returned = WaitFor([&]() { return exitCode.load() != -1; }, 5000); - Check(returned && exitCode.load() == 1, "a malformed frame fails the transport and the host exits with a failure"); - Check(!ran("after"), "a request after the malformed frame is never run"); + Check(returned && exitCode.load() == 1, "a " + subject + " fails the transport and the host exits with a failure"); + Check(!ran("after"), "a request after the " + subject + " is never run"); // Ends a host that kept reading, so a failed check cannot hang the run. CloseHandle(testOutput); @@ -3687,7 +3688,8 @@ int RunSelfTest() { TestEffectLedger(); TestDriverMutationEffects(); TestHostQueuedCancellation(); - TestHostMalformedJson(); + TestHostRejectsFrame(R"({"type":"request","id":"truncated")", "malformed frame"); + TestHostRejectsFrame("[]", "non-object frame"); TestDriverHitTestBeyondScreens(); TestAccessibilityActions(); TestDriverObservation(); From 2acbab00510c5a7b437a0cec728ad3277a4f6453 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 12:59:07 +0000 Subject: [PATCH 11/16] Mention non-object JSON frames in the Windows helper docs The changeset and the README verification paragraph now say that a non-object JSON frame fails the transport, as the host and its self-test do. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .changeset/win-helper-cancellation.md | 2 +- packages/agentic/desktop-driver/native/windows/README.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.changeset/win-helper-cancellation.md b/.changeset/win-helper-cancellation.md index 6077a8aebd..4a0c87dd35 100644 --- a/.changeset/win-helper-cancellation.md +++ b/.changeset/win-helper-cancellation.md @@ -2,4 +2,4 @@ '@fluentui-react-native/desktop-driver': patch --- -Make the Windows helper acknowledge cancellation promptly: bounded UI Automation calls, the whole-subtree cache, window restore, move, and activation, and `closeApplication` observe cancellation, and a cancelled mutation reports effect `unknown`. Window management and activation probe a hung target and fail with reason `target-non-response` instead of blocking. Held keys and buttons keep the physical input lock until released, and queued commands are dropped after the transport ends. A malformed JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. +Make the Windows helper acknowledge cancellation promptly: bounded UI Automation calls, the whole-subtree cache, window restore, move, and activation, and `closeApplication` observe cancellation, and a cancelled mutation reports effect `unknown`. Window management and activation probe a hung target and fail with reason `target-non-response` instead of blocking. Held keys and buttons keep the physical input lock until released, and queued commands are dropped after the transport ends. A malformed or non-object JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. diff --git a/packages/agentic/desktop-driver/native/windows/README.md b/packages/agentic/desktop-driver/native/windows/README.md index 8394cafbba..92db1c03cf 100644 --- a/packages/agentic/desktop-driver/native/windows/README.md +++ b/packages/agentic/desktop-driver/native/windows/README.md @@ -556,8 +556,8 @@ yarn test --runInBand The contract builds and resolves the helper, validates cache reuse and handshakes, runs the native self-test, checks that a malformed frame fails the -transport, and verifies shared Windows/Win32 selection. A second suite builds the test-only -Win32 fixture (`fixtures/win32` in the repository, not published) and drives it +transport (the self-test also covers a non-object frame), and verifies shared +Windows/Win32 selection. A second suite builds the test-only Win32 fixture (`fixtures/win32` in the repository, not published) and drives it through the helper: window events, owned popups, hung and stuck providers, large trees, bounded retention, and application containment (a contained launch whose descendant ends on close and when the helper is killed, and an external fixture From 7539936a4708cbd8fafe6c5462003b9ee4481d17 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 13:03:39 +0000 Subject: [PATCH 12/16] Expect the Windows helper to fail the transport on malformed JSON The Windows native contract sent a malformed frame and expected the helper to keep answering. The helper now follows PROTOCOL.md and exits with code 1, so the contract writes the malformed frame and a probe, expects exit code 1 within 10 s, and expects no response to the probe. The 1,024-byte identifier request and cancel that followed the bad frame could no longer be read, so they are dropped. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/native/nativeDriver.test.ts | 52 +++++++------------ 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/packages/agentic/desktop-driver/src/native/nativeDriver.test.ts b/packages/agentic/desktop-driver/src/native/nativeDriver.test.ts index 3bc812f5f4..c24670b76d 100644 --- a/packages/agentic/desktop-driver/src/native/nativeDriver.test.ts +++ b/packages/agentic/desktop-driver/src/native/nativeDriver.test.ts @@ -415,7 +415,7 @@ describe('native driver build and resolution', () => { await expect(NativeHostProcess.start({ artifact: { ...resolved, buildId: 'mismatched-build' } })).rejects.toMatchObject({ code: 'handshake-failed', }); - await expectMalformedJsonRecovery(resolved.executablePath); + await expectMalformedJsonFailsTransport(resolved.executablePath); const helper = await NativeHostProcess.start({ artifact: resolved }); await expect(helper.request('probe', { padding: 'x'.repeat(8 * 1024 * 1024) })).rejects.toThrow('exceeds'); @@ -482,29 +482,28 @@ describe('native driver build and resolution', () => { } }); - async function expectMalformedJsonRecovery(executablePath: string): Promise { + // PROTOCOL.md: invalid JSON fails the transport, because the frame may have + // carried a request or cancel the helper cannot correlate. + async function expectMalformedJsonFailsTransport(executablePath: string): Promise { const child = spawn(executablePath, ['--stdio'], { stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true, }); - const closed = new Promise((resolve) => child.once('close', () => resolve())); + const closed = new Promise((resolve) => child.once('close', (code) => resolve(code))); const decoder = new NativeFrameDecoder(); + const responses: string[] = []; + decoder.on('json', (message: NativeHostJsonMessage) => { + if (message.type === 'response' && typeof message.id === 'string') { + responses.push(message.id); + } + }); child.stdout.on('data', (chunk: Buffer) => decoder.write(chunk)); child.stderr.resume(); + // The helper may exit before it reads the frames written after the malformed one. + child.stdin.on('error', () => undefined); try { await waitForMessage(decoder, (message) => message.type === 'hello'); child.stdin.write(encodeMalformedJsonFrame()); - child.stdin.write( - encodeJsonFrame({ - command: 'probe', - id: 'x'.repeat(1024), - params: { endpoint: 'windows' }, - type: 'request', - }), - ); - child.stdin.write(encodeJsonFrame({ id: 'x'.repeat(1024), type: 'cancel' })); - - const probe = waitForMessage(decoder, (message) => message.type === 'response' && message.id === 'probe-after-malformed'); child.stdin.write( encodeJsonFrame({ command: 'probe', @@ -513,25 +512,12 @@ describe('native driver build and resolution', () => { type: 'request', }), ); - await expect(probe).resolves.toMatchObject({ - result: { - endpoint: 'windows', - platformName: 'windows', - protocolVersion: 1, - }, - }); - - const disposed = waitForMessage(decoder, (message) => message.type === 'response' && message.id === 'dispose-after-malformed'); - child.stdin.write( - encodeJsonFrame({ - command: 'dispose', - id: 'dispose-after-malformed', - params: {}, - type: 'request', - }), - ); - await disposed; - await closed; + const exit = await Promise.race([ + closed, + new Promise<'still-running'>((resolve) => setTimeout(() => resolve('still-running'), 10_000).unref()), + ]); + expect(exit).toBe(1); + expect(responses).not.toContain('probe-after-malformed'); } finally { if (child.exitCode === null && child.signalCode === null) { child.kill(); From eaaa613763944fbb03629a160a47d8f4170fee64 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 13:04:20 +0000 Subject: [PATCH 13/16] State Windows cancellation polling as bounds, not guarantees The README, remaining-work notes, and changeset implied that every Windows activation and provider path now acknowledges a cancel promptly. Only the newly wrapped waits check cancellation between polls; direct UI Automation calls and the activation calls themselves can still block, and none of it has run on Windows. Describe the polling intervals as implementation bounds on those waits and name the paths that remain unbounded. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .changeset/win-helper-cancellation.md | 2 +- .../desktop-driver/native/windows/README.md | 20 ++++++++++++------- .../references/remaining-work.md | 16 ++++++++------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/.changeset/win-helper-cancellation.md b/.changeset/win-helper-cancellation.md index 4a0c87dd35..7adfcb7a63 100644 --- a/.changeset/win-helper-cancellation.md +++ b/.changeset/win-helper-cancellation.md @@ -2,4 +2,4 @@ '@fluentui-react-native/desktop-driver': patch --- -Make the Windows helper acknowledge cancellation promptly: bounded UI Automation calls, the whole-subtree cache, window restore, move, and activation, and `closeApplication` observe cancellation, and a cancelled mutation reports effect `unknown`. Window management and activation probe a hung target and fail with reason `target-non-response` instead of blocking. Held keys and buttons keep the physical input lock until released, and queued commands are dropped after the transport ends. A malformed or non-object JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. +Check cancellation in more Windows helper waits: bounded UI Automation calls, the whole-subtree cache, window response probes, posted window restores and moves, activation retries, and the `closeApplication` termination wait, and a cancelled mutation reports effect `unknown`. Direct UI Automation calls and the activation calls themselves can still block. Window management and activation probe the target window first and fail with reason `target-non-response` when it does not answer. Held keys and buttons keep the physical input lock until released, and queued commands are dropped after the transport ends. A malformed or non-object JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. diff --git a/packages/agentic/desktop-driver/native/windows/README.md b/packages/agentic/desktop-driver/native/windows/README.md index 92db1c03cf..246c260e0a 100644 --- a/packages/agentic/desktop-driver/native/windows/README.md +++ b/packages/agentic/desktop-driver/native/windows/README.md @@ -196,10 +196,13 @@ owns it. Before each, the helper sends the window `WM_NULL` through within 2 seconds fails as `automation-failed` with reason `target-non-response` instead of holding the helper and the physical input lock. The restore and move themselves are posted (`ShowWindowAsync`, `SWP_ASYNCWINDOWPOS`), activation does -not share the input queue of a thread that Windows reports as hung, and every -wait observes cancellation. A probe that Windows refuses for another reason, -such as User Interface Privilege Isolation, does not judge the window and leaves -it to the bounded steps that follow. +not share the input queue of a thread that Windows reports as hung, and the +helper's own polling for these steps checks cancellation between polls. The +probe does not cover a window that stops answering after it: activation's +`SetForegroundWindow`, `SetActiveWindow`, and `BringWindowToTop` can then still +block. A probe that Windows refuses for another reason, such as User Interface +Privilege Isolation, does not judge the window and leaves it to the steps that +follow. A subtree walk retries while its root remains available if a descendant becomes unavailable, or if a closing popup island's provider reports a UI @@ -407,9 +410,12 @@ reports from a mutation is treated the same way, because the call reached the application and its effect is unknown. An explicit rejection, such as a disabled element, is an ordinary error and does not quarantine. -The bounded wait observes the command's cancellation every 50 ms, as does the -wait for a whole-subtree cache, so a cancelled command is acknowledged promptly -even against a hung provider. A cancelled read simply stops waiting. A cancelled +The bounded wait checks the command's cancellation every 50 ms, as does the wait +for a whole-subtree cache, so these waits stop soon after a cancel even when the +provider hangs. UI Automation calls made directly, outside the bounded wait, +such as pattern queries, `ElementFromHandle`, `observe` paging, and the tree +walker, are bounded only by the client's 2-second connection and 5-second +transaction timeouts. A cancelled read simply stops waiting. A cancelled mutation is abandoned and quarantined exactly like one that timed out, so its effect is `unknown` and the refusal says it was cancelled before it settled. diff --git a/packages/agentic/desktop-driver/references/remaining-work.md b/packages/agentic/desktop-driver/references/remaining-work.md index 2aec49ed6b..9b681f5635 100644 --- a/packages/agentic/desktop-driver/references/remaining-work.md +++ b/packages/agentic/desktop-driver/references/remaining-work.md @@ -594,13 +594,15 @@ the 5,000-node cap: about 0.4 ms per node, so a 4,900-node tree reads in about 2 seconds and a 10,000-node tree fails after about 4. Section 11 records the evidence and what did not run. -**Windows cancellation progress:** a cancelled command stops waiting within a -quarter of a second in bounded UI Automation calls and the whole-subtree cache -(50 ms slices), window response probes (200 ms messages), posted restores and -moves, activation, and `closeApplication`, and a cancelled mutation reports -`unknown`. The physical input lock stays held -across commands only while keys or buttons stay down, so the cancel path does -not wait for it. Remaining: direct UI Automation calls outside the bounded worker +**Windows cancellation progress (not yet run on Windows):** the helper's waits +for bounded UI Automation calls and the whole-subtree cache check cancellation +every 50 ms, window response probes send 200 ms messages and check between +them, and the waits for posted restores and moves, activation retries, and +`closeApplication` termination check between polls. These are implementation +bounds on the wrapped waits, not measured end-to-end cancellation times. A +cancelled mutation reports `unknown`. The physical input lock stays held across +commands only while keys or buttons stay down, so the cancel path does not wait +for it. Remaining: direct UI Automation calls outside the bounded worker (pattern queries, `ElementFromHandle`, `observe` paging, the tree walker) are bounded only by the client's connection and transaction timeouts; `SetForegroundWindow`, `SetActiveWindow`, and `BringWindowToTop` can still block From af75cf2ac28897c70e8f6589d2dc98b3ddcac3ec Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 06:45:18 -0800 Subject: [PATCH 14/16] Cover in-flight Windows helper cancellation Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 53a0c7cf-1330-4677-8700-d249b508c6ee --- .../fixtures/win32/src/fixture.cpp | 12 ++++ .../desktop-driver/native/windows/README.md | 5 +- .../windows/win32Fixture.contract.test.ts | 71 ++++++++++++++++++- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/packages/agentic/desktop-driver/fixtures/win32/src/fixture.cpp b/packages/agentic/desktop-driver/fixtures/win32/src/fixture.cpp index 1ba00c6cb8..131a6f2401 100644 --- a/packages/agentic/desktop-driver/fixtures/win32/src/fixture.cpp +++ b/packages/agentic/desktop-driver/fixtures/win32/src/fixture.cpp @@ -7,6 +7,7 @@ // --hang-ms how long the Hang button blocks the UI thread (default 20000) // --stuck-ms add a control whose UI Automation Invoke blocks this long // on a provider thread (default 0: no such control) +// --stuck-started-file create this file when the stuck Invoke starts // --offscreen <0|1> place the main window off every monitor (default 0) // --lifetime-ms exit the process after this long even when the UI thread is // blocked, so an abandoned run cannot linger (default 600000, 0: never) @@ -62,6 +63,7 @@ struct Options { DWORD lifetimeMilliseconds = 600000; bool offscreen = false; std::wstring childPidFile; + std::wstring stuckStartedFile; }; Options options; @@ -90,6 +92,8 @@ Options ParseOptions() { parsed.hangMilliseconds = static_cast(std::max(0, _wtoi(value))); } else if (name == L"--stuck-ms") { parsed.stuckMilliseconds = static_cast(std::max(0, _wtoi(value))); + } else if (name == L"--stuck-started-file") { + parsed.stuckStartedFile = value; } else if (name == L"--lifetime-ms") { parsed.lifetimeMilliseconds = static_cast(std::max(0, _wtoi(value))); } else if (name == L"--offscreen") { @@ -230,6 +234,14 @@ class StuckProvider : public IRawElementProviderSimple, public IInvokeProvider { } HRESULT STDMETHODCALLTYPE Invoke() override { + if (!options.stuckStartedFile.empty()) { + const HANDLE started = CreateFileW(options.stuckStartedFile.c_str(), GENERIC_WRITE, FILE_SHARE_READ, nullptr, + CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); + if (started == INVALID_HANDLE_VALUE) { + return HRESULT_FROM_WIN32(GetLastError()); + } + CloseHandle(started); + } Sleep(options.stuckMilliseconds); PostMessageW(mainWindow, kStuckApplied, 0, 0); return S_OK; diff --git a/packages/agentic/desktop-driver/native/windows/README.md b/packages/agentic/desktop-driver/native/windows/README.md index 246c260e0a..4b843f38ff 100644 --- a/packages/agentic/desktop-driver/native/windows/README.md +++ b/packages/agentic/desktop-driver/native/windows/README.md @@ -567,7 +567,10 @@ Windows/Win32 selection. A second suite builds the test-only Win32 fixture (`fix through the helper: window events, owned popups, hung and stuck providers, large trees, bounded retention, and application containment (a contained launch whose descendant ends on close and when the helper is killed, and an external fixture -that survives attach, close, and disposal). `yarn test --runInBand win32Fixture` runs only +that survives attach, close, and disposal). The hung-window case checks cancelled +and refused window management; an in-flight provider cancellation checks the +unknown effect, quarantine, and survival of the owned application. +`yarn test --runInBand win32Fixture` runs only that suite. A third suite serves the same fixture through a grant-protected Desktop Driver service with the helper behind it: unauthenticated requests reach no helper call, sessions keep their owner, a revoked grant closes its diff --git a/packages/agentic/desktop-driver/src/native/windows/win32Fixture.contract.test.ts b/packages/agentic/desktop-driver/src/native/windows/win32Fixture.contract.test.ts index c64c1aaa41..c33b0112f2 100644 --- a/packages/agentic/desktop-driver/src/native/windows/win32Fixture.contract.test.ts +++ b/packages/agentic/desktop-driver/src/native/windows/win32Fixture.contract.test.ts @@ -271,7 +271,7 @@ describe('Windows helper against a plain Win32 application', () => { }); windowsTest('reports a hung application without guessing and recovers when it responds again', async () => { - const session = await FixtureSession.start('hang', { args: ['--hang-ms', '14000'] }); + const session = await FixtureSession.start('hang', { args: ['--hang-ms', '22000'] }); try { const { main } = session; const activate = await session.control(main.id, '1001'); @@ -282,6 +282,30 @@ describe('Windows helper against a plain Win32 application', () => { expect(await session.windows()).toHaveLength(1); expect(Date.now() - listedAt).toBeLessThan(1_000); + const rect = { ...main.rect, x: main.rect.x + 20 }; + const controller = new AbortController(); + const pending = session.host + .request('setWindowRect', { rect, windowId: main.id }, controller.signal) + .catch((error: unknown) => error); + await delay(250); + const cancelledAt = Date.now(); + controller.abort(); + expect(await pending).toMatchObject({ name: 'AbortError', effect: 'none' }); + expect(Date.now() - cancelledAt).toBeLessThan(1_500); + + for (const [command, params] of [ + ['setWindowRect', { rect, windowId: main.id }], + ['activate', { windowId: main.id }], + ] as const) { + const startedAt = Date.now(); + await expect(session.host.request(command, params)).rejects.toMatchObject({ + code: 'automation-failed', + data: { reason: 'target-non-response' }, + effect: 'none', + }); + expect(Date.now() - startedAt).toBeLessThan(4_000); + } + const read = await session.attempt('find', { root: { windowId: main.id }, selector: { strategy: 'accessibility id', value: '1004' }, @@ -305,6 +329,51 @@ describe('Windows helper against a plain Win32 application', () => { } }); + windowsTest('cancels an in-flight provider mutation without killing its owned application', async () => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'furn-win32-cancel-')); + const startedFile = path.join(directory, 'started'); + const controller = new AbortController(); + let session: FixtureSession | undefined; + try { + const fixture = await FixtureSession.start('cancel-stuck', { + args: ['--stuck-ms', '8000', '--stuck-started-file', startedFile], + }); + session = fixture; + const stuck = await fixture.control(fixture.main.id, '1009'); + const activate = await fixture.control(fixture.main.id, '1001'); + const pending = fixture.host + .request('click', { elementId: stuck.id, mode: 'accessibility' }, controller.signal) + .catch((error: unknown) => error); + await waitFor('the stuck provider to start', async () => fs.existsSync(startedFile), { timeoutMs: 10_000 }); + + const cancelledAt = Date.now(); + controller.abort(); + expect(await pending).toMatchObject({ name: 'AbortError', effect: 'unknown' }); + const cancellationMs = Date.now() - cancelledAt; + expect(cancellationMs).toBeLessThan(1_500); + console.info(`In-flight Windows provider mutation cancelled in ${cancellationMs} ms`); + + expect(await fixture.windows()).toHaveLength(1); + expect(await fixture.attempt('click', { elementId: activate.id, mode: 'accessibility' })).toMatchObject({ + code: 'target-quarantined', + ok: false, + }); + await waitFor( + 'the cancelled mutation to apply once', + async () => (await fixture.status().catch(() => undefined)) === 'Stuck action applied 1 times', + { intervalMs: 250, timeoutMs: 15_000 }, + ); + expect(await fixture.attempt('click', { elementId: activate.id, mode: 'accessibility' })).toMatchObject({ + code: 'target-quarantined', + ok: false, + }); + } finally { + controller.abort(); + await session?.dispose(); + fs.rmSync(directory, { force: true, recursive: true }); + } + }); + windowsTest('quarantines later mutations after a provider call does not settle', async () => { const session = await FixtureSession.start('stuck', { args: ['--stuck-ms', '8000'] }); try { From a1813aed4c314893a80b4f11cb5509823b78a834 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Tue, 10 Nov 2026 07:23:23 -0800 Subject: [PATCH 15/16] Fix stalled Windows restore effect reporting Keep a posted restore pending through the responsiveness check and verify its possible late effect. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 53a0c7cf-1330-4677-8700-d249b508c6ee --- .changeset/win-helper-cancellation.md | 2 +- .../desktop-driver/native/windows/README.md | 4 +- .../native/windows/src/driver.cpp | 2 +- .../native/windows/src/selftest.cpp | 76 +++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/.changeset/win-helper-cancellation.md b/.changeset/win-helper-cancellation.md index 7adfcb7a63..4ca5d6b4ca 100644 --- a/.changeset/win-helper-cancellation.md +++ b/.changeset/win-helper-cancellation.md @@ -2,4 +2,4 @@ '@fluentui-react-native/desktop-driver': patch --- -Check cancellation in more Windows helper waits: bounded UI Automation calls, the whole-subtree cache, window response probes, posted window restores and moves, activation retries, and the `closeApplication` termination wait, and a cancelled mutation reports effect `unknown`. Direct UI Automation calls and the activation calls themselves can still block. Window management and activation probe the target window first and fail with reason `target-non-response` when it does not answer. Held keys and buttons keep the physical input lock until released, and queued commands are dropped after the transport ends. A malformed or non-object JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. +Check cancellation in more Windows helper waits: bounded UI Automation calls, the whole-subtree cache, window response probes, posted window restores and moves, activation retries, and the `closeApplication` termination wait, and a cancelled mutation reports effect `unknown`. Direct UI Automation calls and the activation calls themselves can still block. Window management and activation probe the target window first and fail with reason `target-non-response` when it does not answer. A restore that stalls after its initial probe reports effect `unknown` rather than `partial`. Held keys and buttons keep the physical input lock until released, and queued commands are dropped after the transport ends. A malformed or non-object JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. diff --git a/packages/agentic/desktop-driver/native/windows/README.md b/packages/agentic/desktop-driver/native/windows/README.md index 4b843f38ff..13b7105351 100644 --- a/packages/agentic/desktop-driver/native/windows/README.md +++ b/packages/agentic/desktop-driver/native/windows/README.md @@ -563,7 +563,9 @@ yarn test --runInBand The contract builds and resolves the helper, validates cache reuse and handshakes, runs the native self-test, checks that a malformed frame fails the transport (the self-test also covers a non-object frame), and verifies shared -Windows/Win32 selection. A second suite builds the test-only Win32 fixture (`fixtures/win32` in the repository, not published) and drives it +Windows/Win32 selection. The self-test also checks that a window which stalls +after its initial probe leaves a posted restore's effect unknown. A second +suite builds the test-only Win32 fixture (`fixtures/win32` in the repository, not published) and drives it through the helper: window events, owned popups, hung and stuck providers, large trees, bounded retention, and application containment (a contained launch whose descendant ends on close and when the helper is killed, and an external fixture diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.cpp b/packages/agentic/desktop-driver/native/windows/src/driver.cpp index 65c762dff4..cd773af5cb 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/driver.cpp @@ -687,10 +687,10 @@ CommandResult Driver::ExecuteCommand(const std::string& command, const json::Val if (IsZoomed(window) != FALSE || IsIconic(window) != FALSE) { RecordDispatching(); const bool restored = RestoreWindow(window, token); - RecordSettled(S_OK); if (!restored) { RequireWindowResponds(window, token); } + RecordSettled(S_OK); } WindowMetrics metrics = MeasureWindow(window); const RectD current = ClientRectInScreenDips(metrics); diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index a8866288f0..696710d73f 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -877,6 +877,38 @@ LRESULT CALLBACK CountingWindowProc(HWND window, UINT message, WPARAM wparam, LP return DefWindowProcW(window, message, wparam, lparam); } +struct RestoreStall { + std::atomic armed{true}; + Gate entered; + Gate release; +}; + +std::mutex restoreStallMutex; +std::shared_ptr restoreStall; +constexpr UINT kStallAfterProbe = WM_APP + 71; + +void SetRestoreStall(std::shared_ptr stall) { + const std::lock_guard guard(restoreStallMutex); + restoreStall = std::move(stall); +} + +LRESULT CALLBACK StalledRestoreProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) { + std::shared_ptr stall; + { + const std::lock_guard guard(restoreStallMutex); + stall = restoreStall; + } + // The successful probe queues the stall before the helper can post a restore. + if (stall != nullptr && message == WM_NULL && stall->armed.exchange(false)) { + PostMessageW(window, kStallAfterProbe, 0, 0); + } else if (stall != nullptr && message == kStallAfterProbe) { + stall->entered.Open(); + stall->release.Hold(); + return 0; + } + return DefWindowProcW(window, message, wparam, lparam); +} + // While a gate is set, writing an edit control's text holds its window thread, // so a value write through UI Automation never settles. std::mutex editGateMutex; @@ -1798,6 +1830,49 @@ void TestHungWindowProbe() { "the window answers again once its thread resumes"); } +void TestStalledRestoreEffect() { + const CancellationToken token; + WindowThread windows; + const HWND window = windows.Create(L"FurnSelfTestRestore", L"Restore window", &StalledRestoreProc); + Check(window != nullptr, "the restore fixture creates a window"); + if (window == nullptr) { + return; + } + Driver driver; + json::Value lease = json::Value::Object(); + lease.Set("lease", driver.AdoptWindowForTesting(window, L"Restore window")); + const CommandResult listed = driver.Execute("windows", lease, token); + const std::string windowId = listed.result.IsArray() && listed.result.Items().size() == 1 + ? listed.result.Items().front().StringField("id") + : std::string(); + Check(!windowId.empty(), "the restore fixture is registered"); + if (windowId.empty()) { + return; + } + ShowWindowAsync(window, SW_SHOWMINNOACTIVE); + const bool minimized = WaitFor([window]() { return IsIconic(window) != FALSE; }); + Check(minimized, "the restore fixture is minimized without activation"); + if (!minimized) { + return; + } + + json::Value resize = json::Value::Object(); + resize.Set("windowId", json::Value::String(windowId)); + resize.Set("rect", json::Value::Parse(R"({"x":-32000,"y":-32000,"width":40,"height":40})")); + const auto stall = std::make_shared(); + SetRestoreStall(stall); + std::string code; + const std::string effect = FailureEffect([&]() { driver.Execute("setWindowRect", resize, token); }, &code); + const bool entered = WaitForSingleObject(stall->entered.event, 0) == WAIT_OBJECT_0; + SetRestoreStall(nullptr); + stall->release.Open(); + Check(entered, "the restore fixture stalls after its initial probe"); + Check(code == kErrorAutomationFailed, "the stalled restore reports an automation failure"); + Check(effect == "unknown", "a stalled restore reports an unknown effect (reported " + effect + ")"); + Check(WaitFor([window]() { return IsIconic(window) == FALSE; }), + "the posted restore can take effect after the failure"); +} + void TestHelperSessionCheck() { bool accepted = true; try { @@ -3668,6 +3743,7 @@ int RunSelfTest() { TestInjectionIntegrity(); TestActivationRefusal(); TestHungWindowProbe(); + TestStalledRestoreEffect(); TestHelperSessionCheck(); TestInputAuthorityOnThisMachine(); TestInputReevaluation(); From bdeb0b291fcaec5eb702bd18c07645775e135e38 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Wed, 11 Nov 2026 03:57:42 -0800 Subject: [PATCH 16/16] Retain Windows input after failed release Keep unreleased strokes and buttons under the physical-input lock, preserve uncertain host recovery, and refuse unsafe fallback or disposal. Fix Windows portability of desktop-test lifecycle and reporting contracts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 53a0c7cf-1330-4677-8700-d249b508c6ee --- .changeset/desktop-test-windows-contracts.md | 5 + .changeset/win-helper-cancellation.md | 2 +- .../agentic/desktop-driver/native/PROTOCOL.md | 5 + .../desktop-driver/native/windows/README.md | 13 +- .../native/windows/src/driver.cpp | 14 +- .../native/windows/src/driver.h | 11 +- .../native/windows/src/host.cpp | 4 +- .../native/windows/src/input.cpp | 91 ++++++++---- .../desktop-driver/native/windows/src/input.h | 1 + .../native/windows/src/selftest.cpp | 137 +++++++++++++++++- .../references/native-helpers.md | 9 +- .../references/remaining-work.md | 4 + .../NativeDesktopHost.lifecycle.test.ts | 120 +++++++++++++++ .../src/hosts/native/NativeDesktopHost.ts | 62 +++++--- .../hosts/native/NativeHostProcess.test.ts | 10 ++ .../src/hosts/native/NativeHostProcess.ts | 4 +- .../src/native/nativeDriver.test.ts | 10 +- .../src/testing/scriptedNativeHelper.ts | 7 +- .../agentic/desktop-test/src/commands.test.ts | 19 ++- .../desktop-test/src/processControl.test.ts | 20 ++- .../src/reporting/runQueries.test.ts | 2 +- 21 files changed, 467 insertions(+), 83 deletions(-) create mode 100644 .changeset/desktop-test-windows-contracts.md diff --git a/.changeset/desktop-test-windows-contracts.md b/.changeset/desktop-test-windows-contracts.md new file mode 100644 index 0000000000..d78d7cc336 --- /dev/null +++ b/.changeset/desktop-test-windows-contracts.md @@ -0,0 +1,5 @@ +--- +'@fluentui-react-native/desktop-test': patch +--- + +Make the desktop-test lifecycle and reporting contracts portable to Windows, including process cleanup and path projection. diff --git a/.changeset/win-helper-cancellation.md b/.changeset/win-helper-cancellation.md index 4ca5d6b4ca..06cc05d0f3 100644 --- a/.changeset/win-helper-cancellation.md +++ b/.changeset/win-helper-cancellation.md @@ -2,4 +2,4 @@ '@fluentui-react-native/desktop-driver': patch --- -Check cancellation in more Windows helper waits: bounded UI Automation calls, the whole-subtree cache, window response probes, posted window restores and moves, activation retries, and the `closeApplication` termination wait, and a cancelled mutation reports effect `unknown`. Direct UI Automation calls and the activation calls themselves can still block. Window management and activation probe the target window first and fail with reason `target-non-response` when it does not answer. A restore that stalls after its initial probe reports effect `unknown` rather than `partial`. Held keys and buttons keep the physical input lock until released, and queued commands are dropped after the transport ends. A malformed or non-object JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. +Check cancellation in more Windows helper waits: bounded UI Automation calls, the whole-subtree cache, window response probes, posted window restores and moves, activation retries, and the `closeApplication` termination wait, and a cancelled mutation reports effect `unknown`. Direct UI Automation calls and the activation calls themselves can still block. Window management and activation probe the target window first and fail with reason `target-non-response` when it does not answer. A restore that stalls after its initial probe reports effect `unknown` rather than `partial`. Held keys and buttons keep the physical input lock while the helper runs; failed cleanup retains unreleased input for retry, and a failed physical mutation reports an unknown effect. The adapter retains uncertain input for recovery, and disposal reports failed cleanup. Queued commands are dropped after the transport ends. A malformed or non-object JSON frame now fails the transport. Pointer presses and scrolls over another application's window are refused, failed pattern and focus reads are no longer reported as unsupported or unfocused, a job-contained launch root is no longer terminated after launch, a packaged launch root must report its AUMID, an unreadable start time fails the lease, and activation grants foreground rights only to the target process. diff --git a/packages/agentic/desktop-driver/native/PROTOCOL.md b/packages/agentic/desktop-driver/native/PROTOCOL.md index a976009a5a..eb19273e4f 100644 --- a/packages/agentic/desktop-driver/native/PROTOCOL.md +++ b/packages/agentic/desktop-driver/native/PROTOCOL.md @@ -194,6 +194,11 @@ and `setWindowRect`. Reads and lifecycle commands (`launch`, `attach`, `closeApplication`, `dispose`) carry no `effect`. +On Windows, a failed physical-input mutation whose cleanup leaves a key stroke +or button depressed reports `unknown` and retains the input and lock while the +helper remains alive. A later release can retry it; `dispose` refuses to +acknowledge if cleanup still fails. + Node fails closed: an absent `effect` on a failed or cancelled mutation is `unknown`. `none` is a rejection and leaves the target usable; `partial` and `unknown` are indeterminate, and Node quarantines the target until it is diff --git a/packages/agentic/desktop-driver/native/windows/README.md b/packages/agentic/desktop-driver/native/windows/README.md index 13b7105351..148c8de0dd 100644 --- a/packages/agentic/desktop-driver/native/windows/README.md +++ b/packages/agentic/desktop-driver/native/windows/README.md @@ -284,11 +284,14 @@ to the later window and element checks. A click is refused as point, and so is a `performActions` `pointerDown` or `scroll` whose point is covered by a window that neither belongs to a leased application nor is the top-level window around one. Keys or buttons that `performActions` leaves down -keep the session's physical input lock until they are released, by -`releaseActions`, a later command, cancellation, or the helper's exit, so -another helper cannot interleave its input with them. A `SendInput` call that -injects fewer events than requested fails as `input-failed` and names UIPI, a -secure desktop, or blocked input. +keep the session's physical input lock while the helper runs, until +`releaseActions`, a later command, cancellation, or shutdown releases them. +If cleanup fails, the helper retains the unreleased input and lock for a later +retry; a failed physical mutation reports effect `unknown`, and `dispose` +refuses to acknowledge while input remains down. If the worker exits first, +an abandoned mutex can warn one waiting helper, but is not a persistent +recovery barrier. A `SendInput` call that injects fewer events than requested +fails as `input-failed` and names UIPI, a secure desktop, or blocked input. `click` mode `auto` falls back to accessibility activation when physical input is unavailable, and an explicit `physical` request reports `input-unavailable`. diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.cpp b/packages/agentic/desktop-driver/native/windows/src/driver.cpp index cd773af5cb..acfc88a474 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/driver.cpp @@ -291,7 +291,7 @@ void Driver::ClickPhysically(const std::string& elementId, const CancellationTok POINT center{}; center.x = (snapshot.physicalRect.left + snapshot.physicalRect.right) / 2; center.y = (snapshot.physicalRect.top + snapshot.physicalRect.bottom) / 2; - RequireClickPointOwnedBy(window, center); + RequireClickPointOwnedBy(window, center, hitTester_); input_.ClickAt(center, token); } @@ -501,16 +501,15 @@ void Driver::ReleaseInput() noexcept { const PhysicalInputScope scope(idle, kPhysicalInputCleanupWaitMs, AbandonPolicy::Adopt); input_.ReleaseAll(); } catch (const HelperError&) { - // Depressed input must never outlive a cancelled command, so recovery - // proceeds even when the cross-process lock could not be acquired. + // Continue attempting cleanup even if the cross-process lock could not + // be acquired. input_.ReleaseAll(); } } } catch (...) { // Releasing input must never mask the original failure. } - // Input that could not be released keeps the lock, so the thread's exit - // abandons it and the next helper reports the depressed input. + // Input that could not be released keeps the lock while the worker lives. if (!input_.HasDepressedInput()) { heldInput_.Release(); } @@ -815,7 +814,7 @@ CommandResult Driver::ExecuteCommand(const std::string& command, const json::Val // The environment can change between the check and the click. When // it refused before any click was delivered, automatic mode still // has the accessibility click. - if (mode != "auto" || error.code() != kErrorInputUnavailable) { + if (mode != "auto" || error.code() != kErrorInputUnavailable || input_.HasDepressedInput()) { throw; } automation_.AccessibilityClick(elementId, token); @@ -944,6 +943,9 @@ CommandResult Driver::ExecuteCommand(const std::string& command, const json::Val } if (command == "dispose") { ReleaseInput(); + if (input_.HasDepressedInput()) { + Fail(kErrorInputFailed, "The helper could not release all held physical input during disposal."); + } applications_.Dispose(token); automation_.Reset(); result.result = json::Value::Null(); diff --git a/packages/agentic/desktop-driver/native/windows/src/driver.h b/packages/agentic/desktop-driver/native/windows/src/driver.h index 78e9790f96..0fe5e7c039 100644 --- a/packages/agentic/desktop-driver/native/windows/src/driver.h +++ b/packages/agentic/desktop-driver/native/windows/src/driver.h @@ -88,10 +88,9 @@ class Driver { private: CommandResult ExecuteCommand(const std::string& command, const json::Value& params, const CancellationToken& token); void ForgetRetiredWindows() noexcept; - // Holds the cross-process physical input mutex for a whole action chain and - // guarantees that a failed or cancelled chain releases its depressed input - // before the next owner acquires the lock. Keys or buttons a chain leaves - // down keep the lock held across commands until they are released. + // Holds the cross-process physical input mutex for a whole action chain. + // Failed cleanup retains both the unreleased input and lock while this + // worker lives; successful cleanup releases the lock. template void RunPhysical(const CancellationToken& token, Fn&& body) { PhysicalInputScope scope(token); @@ -106,6 +105,10 @@ class Driver { // Preserve the original failure. } } + if (input_.HasDepressedInput()) { + // A failed cleanup leaves the command's physical-input state uncertain. + RecordDispatching(); + } HoldInputWhileDepressed(scope); throw; } diff --git a/packages/agentic/desktop-driver/native/windows/src/host.cpp b/packages/agentic/desktop-driver/native/windows/src/host.cpp index a593f126a9..2a211f7c2e 100644 --- a/packages/agentic/desktop-driver/native/windows/src/host.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/host.cpp @@ -264,8 +264,8 @@ void Host::WorkerLoop() { ExitProcess(0); } } - // The physical input lock belongs to this thread, so it is released here - // rather than abandoned when the thread exits. + // The physical input lock belongs to this thread; unresolved input retains + // it until this worker exits. driver_.ReleaseInput(); winrt::uninit_apartment(); } diff --git a/packages/agentic/desktop-driver/native/windows/src/input.cpp b/packages/agentic/desktop-driver/native/windows/src/input.cpp index 90a7245392..10ae9ddb8f 100644 --- a/packages/agentic/desktop-driver/native/windows/src/input.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/input.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "geometry.h" #include "settlement.h" @@ -352,6 +353,7 @@ void InputController::PointerDown(int button, const CancellationToken& token) { // Carry the coordinate so the press lands where the pointer was placed even if // another input source has since moved the cursor. std::vector inputs{MakeAbsoluteMouseInput(pointer_, MOUSEEVENTF_MOVE | downFlag, mouseData)}; + pressedButtons_.reserve(pressedButtons_.size() + 1); Send(inputs); pressedButtons_.push_back(button); } @@ -372,7 +374,10 @@ void InputController::PointerUp(int button, const CancellationToken& token) { } void InputController::PointerCancel() { - for (auto iterator = pressedButtons_.rbegin(); iterator != pressedButtons_.rend(); ++iterator) { + std::exception_ptr failure; + auto iterator = pressedButtons_.end(); + while (iterator != pressedButtons_.begin()) { + --iterator; DWORD downFlag = 0; DWORD upFlag = 0; DWORD mouseData = 0; @@ -380,11 +385,16 @@ void InputController::PointerCancel() { std::vector inputs{MakeAbsoluteMouseInput(pointer_, upFlag, mouseData)}; try { Send(inputs, false); + iterator = pressedButtons_.erase(iterator); } catch (const HelperError&) { - // Release as much as possible; the remaining ledger entries still drop. + if (!failure) { + failure = std::current_exception(); + } } } - pressedButtons_.clear(); + if (failure) { + std::rethrow_exception(failure); + } } void InputController::ClickAt(POINT target, const CancellationToken& token) { @@ -480,12 +490,7 @@ void InputController::KeyDown(std::wstring_view value, const CancellationToken& } const std::vector strokes = StrokesForValue(value); GuardKeyPress(); - // The ledger records the press before injection so a partially injected - // sequence is still released by the ledger. - pressedKeys_.emplace_back(std::wstring(value), strokes); - for (const KeyStroke& stroke : strokes) { - SendStroke(stroke, true); - } + PressStrokes(std::wstring(value), strokes); } void InputController::KeyUp(std::wstring_view value, const CancellationToken& token) { @@ -502,8 +507,9 @@ void InputController::KeyUp(std::wstring_view value, const CancellationToken& to continue; } RequirePhysicalInput(); - for (auto stroke = iterator->second.rbegin(); stroke != iterator->second.rend(); ++stroke) { - SendStroke(*stroke, false); + while (!iterator->second.empty()) { + SendStroke(iterator->second.back(), false); + iterator->second.pop_back(); } pressedKeys_.erase(std::next(iterator).base()); return; @@ -539,17 +545,34 @@ void InputController::TypeText(std::wstring_view text, const CancellationToken& } } +void InputController::PressStrokes(std::wstring ledgerKey, const std::vector& strokes) { + std::vector recorded; + // Recording an accepted stroke must not allocate after injection. + recorded.reserve(strokes.size()); + pressedKeys_.emplace_back(std::move(ledgerKey), std::move(recorded)); + auto& held = pressedKeys_.back().second; + try { + for (const KeyStroke& stroke : strokes) { + SendStroke(stroke, true); + held.push_back(stroke); + } + } catch (...) { + if (held.empty()) { + pressedKeys_.pop_back(); + } + throw; + } +} + void InputController::PressAndRelease(std::wstring ledgerKey, const std::vector& strokes, const CancellationToken& token) { token.ThrowIfCancelled(); GuardKeyPress(); - pressedKeys_.emplace_back(std::move(ledgerKey), strokes); - for (const KeyStroke& stroke : strokes) { - SendStroke(stroke, true); - } - const std::vector& recorded = pressedKeys_.back().second; - for (auto stroke = recorded.rbegin(); stroke != recorded.rend(); ++stroke) { - SendStroke(*stroke, false); + PressStrokes(std::move(ledgerKey), strokes); + auto& recorded = pressedKeys_.back().second; + while (!recorded.empty()) { + SendStroke(recorded.back(), false); + recorded.pop_back(); } pressedKeys_.pop_back(); } @@ -563,8 +586,7 @@ void InputController::PressChord(const std::vector& virtualKeys, const Can const KeyStroke stroke{false, key, false}; // Chord keys enter the ledger under a synthetic name that can never // collide with a WebDriver key value, so a failed chord still releases. - pressedKeys_.emplace_back(L"chord:" + std::to_wstring(key), std::vector{stroke}); - SendStroke(stroke, true); + PressStrokes(L"chord:" + std::to_wstring(key), {stroke}); } while (pressedKeys_.size() > base) { const std::vector& strokes = pressedKeys_.back().second; @@ -636,17 +658,36 @@ std::size_t InputController::ReleaseDesktopModifiers() { } void InputController::ReleaseAll() { - for (auto iterator = pressedKeys_.rbegin(); iterator != pressedKeys_.rend(); ++iterator) { - for (auto stroke = iterator->second.rbegin(); stroke != iterator->second.rend(); ++stroke) { + std::exception_ptr failure; + auto iterator = pressedKeys_.end(); + while (iterator != pressedKeys_.begin()) { + --iterator; + auto stroke = iterator->second.end(); + while (stroke != iterator->second.begin()) { + --stroke; try { SendStroke(*stroke, false, false); + stroke = iterator->second.erase(stroke); } catch (const HelperError&) { - // Continue releasing the remaining ledger entries. + if (!failure) { + failure = std::current_exception(); + } } } + if (iterator->second.empty()) { + iterator = pressedKeys_.erase(iterator); + } + } + try { + PointerCancel(); + } catch (const HelperError&) { + if (!failure) { + failure = std::current_exception(); + } + } + if (failure) { + std::rethrow_exception(failure); } - pressedKeys_.clear(); - PointerCancel(); } } // namespace furn diff --git a/packages/agentic/desktop-driver/native/windows/src/input.h b/packages/agentic/desktop-driver/native/windows/src/input.h index b3854edfd6..9f5c45dca7 100644 --- a/packages/agentic/desktop-driver/native/windows/src/input.h +++ b/packages/agentic/desktop-driver/native/windows/src/input.h @@ -112,6 +112,7 @@ class InputController { // Runs the keyboard guard. Called before a press is recorded in the ledger, // so a refused press leaves nothing to release. void GuardKeyPress(); + void PressStrokes(std::wstring ledgerKey, const std::vector& strokes); void PressAndRelease(std::wstring ledgerKey, const std::vector& strokes, const CancellationToken& token); // Counted sends are primary mutations in the command's effect ledger. Pointer diff --git a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp index 696710d73f..bea2b6c11f 100644 --- a/packages/agentic/desktop-driver/native/windows/src/selftest.cpp +++ b/packages/agentic/desktop-driver/native/windows/src/selftest.cpp @@ -1551,6 +1551,37 @@ UINT WINAPI AcceptFirstInputOnly(UINT, LPINPUT, int) { return 1; } +UINT WINAPI RejectSelectedReleases(UINT count, LPINPUT inputs, int size) { + if (size != sizeof(INPUT)) { + return 0; + } + for (UINT index = 0; index < count; index += 1) { + const INPUT& input = inputs[index]; + if (input.type == INPUT_KEYBOARD && (input.ki.dwFlags & KEYEVENTF_KEYUP) != 0 && + (input.ki.wVk == VK_SHIFT || + ((input.ki.dwFlags & KEYEVENTF_UNICODE) != 0 && input.ki.wScan == 0xd83d))) { + return 0; + } + if (input.type == INPUT_MOUSE && (input.mi.dwFlags & MOUSEEVENTF_LEFTUP) != 0) { + return 0; + } + } + return RecordInput(count, inputs, size); +} + +UINT WINAPI RejectLowSurrogatePress(UINT count, LPINPUT inputs, int size) { + if (size != sizeof(INPUT)) { + return 0; + } + for (UINT index = 0; index < count; index += 1) { + if (inputs[index].type == INPUT_KEYBOARD && (inputs[index].ki.dwFlags & KEYEVENTF_UNICODE) != 0 && + (inputs[index].ki.dwFlags & KEYEVENTF_KEYUP) == 0 && inputs[index].ki.wScan == 0xde00) { + return 0; + } + } + return RecordInput(count, inputs, size); +} + // Records input and then cancels the command that sent it, once a key press // reaches the application; the modifier releases before it do not count. CancellationToken* cancelOnInput = nullptr; @@ -2101,6 +2132,53 @@ void TestInputSendFailures() { message) == kErrorInputFailed && Contains(message, "accepted 1 of 2"), "a partly accepted batch reports how much of it Windows accepted"); + + InputController cleanup; + cleanup.UseTestSender(&RecordInput); + cleanup.KeyDown(L"\uE008", token); + cleanup.KeyDown(L"\uE009", token); + cleanup.KeyDown(L"\U0001F600", token); + cleanup.PointerDown(0, token); + cleanup.PointerDown(2, token); + cleanup.UseTestSender(&RejectSelectedReleases); + recordedInputs.clear(); + Check(FailureCode([&]() { cleanup.ReleaseAll(); }) == kErrorInputFailed && + cleanup.DepressedKeyCount() == 2 && cleanup.DepressedButtonCount() == 1, + "failed cleanup reports the failure and retains only keys and buttons not released"); + Check(recordedInputs.size() == 3 && recordedInputs[0].ki.wScan == 0xde00 && + recordedInputs[1].ki.wVk == VK_CONTROL && (recordedInputs[2].mi.dwFlags & MOUSEEVENTF_RIGHTUP) != 0, + "cleanup still releases the other strokes and buttons after a failure"); + + cleanup.UseTestSender(&RecordInput); + recordedInputs.clear(); + Check(FailureCode([&]() { cleanup.ReleaseAll(); }) == "none" && !cleanup.HasDepressedInput() && + recordedInputs.size() == 3 && recordedInputs[0].ki.wScan == 0xd83d && + recordedInputs[1].ki.wVk == VK_SHIFT && (recordedInputs[2].mi.dwFlags & MOUSEEVENTF_LEFTUP) != 0, + "a later cleanup retries only the failed strokes and buttons"); + + InputController partial; + partial.UseTestSender(&RejectLowSurrogatePress); + recordedInputs.clear(); + Check(FailureCode([&]() { partial.KeyDown(L"\U0001F600", token); }) == kErrorInputFailed && + partial.DepressedKeyCount() == 1 && recordedInputs.size() == 1 && recordedInputs[0].ki.wScan == 0xd83d, + "a partially accepted key press records the stroke Windows accepted"); + partial.UseTestSender(&RecordInput); + recordedInputs.clear(); + partial.ReleaseAll(); + Check(!partial.HasDepressedInput() && recordedInputs.size() == 1 && recordedInputs[0].ki.wScan == 0xd83d, + "cleanup does not release a stroke Windows never accepted"); + + partial.KeyDown(L"\U0001F600", token); + partial.UseTestSender(&RejectSelectedReleases); + recordedInputs.clear(); + Check(FailureCode([&]() { partial.KeyUp(L"\U0001F600", token); }) == kErrorInputFailed && + partial.DepressedKeyCount() == 1 && recordedInputs.size() == 1 && recordedInputs[0].ki.wScan == 0xde00, + "an explicit partial key release retains its unreleased stroke"); + partial.UseTestSender(&RecordInput); + recordedInputs.clear(); + partial.ReleaseAll(); + Check(!partial.HasDepressedInput() && recordedInputs.size() == 1 && recordedInputs[0].ki.wScan == 0xd83d, + "cleanup does not repeat the successful part of a key release"); } void TestForegroundOwnership() { @@ -2240,8 +2318,13 @@ void TestClickRouting() { Check(coveredBy(nullptr) == "none", "a point that hits no window passes"); Check(coveredBy(window) == "none", "a point on the target window passes"); Check(coveredBy(sibling) == "none", "a point on another window of the same application passes"); - Check(FailureCode([&]() { RequireClickPointOwnedBy(window, POINT{-32000 + 20, -32000 + 20}); }) == "none", - "the real hit test never refuses an offscreen window of the same process"); + // Other applications can also have windows at the fixture's offscreen coordinates. + const POINT offscreenPoint{-32000 + 20, -32000 + 20}; + clickHitResult = WindowFromPoint(offscreenPoint); + const std::string expected = + FailureCode([&]() { RequireClickPointOwnedBy(window, offscreenPoint, &HitConfiguredWindow); }); + Check(FailureCode([&]() { RequireClickPointOwnedBy(window, offscreenPoint); }) == expected, + "the real hit test agrees with the window Windows reports at the offscreen point"); const HWND foreign = ForeignWindow(); clickHitResult = foreign; Check(FailureCode([&]() { RequireClickPointOwnedBy(nullptr, point, &HitConfiguredWindow); }) == "none", @@ -3227,6 +3310,56 @@ void TestDriverMutationEffects() { Check(!driver.HasDepressedInput() && acquireElsewhere() == "acquired", "the release recovery frees the held key and the physical input lock"); + const json::Value holdShift = + actions(R"([{"type":"key","id":"keyboard","actions":[{"type":"keyDown","value":"\uE008"}]}])"); + Check(FailureCode([&]() { driver.Execute("performActions", holdShift, token); }) == "none" && + driver.HasDepressedInput(), + "a modifier is held for failed-release recovery"); + driver.InputForTesting().UseTestSender(&RejectSelectedReleases); + driver.ReleaseInput(); + Check(driver.HasDepressedInput() && acquireElsewhere() == kErrorInputBusy, + "failed shutdown cleanup keeps the key and the physical input lock"); + Check(FailureCode([&]() { driver.Execute("dispose", empty, token); }) == kErrorInputFailed && + driver.HasDepressedInput() && acquireElsewhere() == kErrorInputBusy, + "dispose refuses to acknowledge while input cannot be released"); + int environmentChecks = 0; + driver.InputForTesting().UseTestEnvironment([&environmentChecks]() { + return environmentChecks++ == 0 + ? InteractiveEnvironmentForTesting() + : InteractiveWith([](InputEnvironment& environment) { environment.lock = SessionLock::Locked; }); + }); + driver.UseTestHitTester(&HitConfiguredWindow); + clickHitResult = window; + const int clicksBefore = buttonClicks.load(); + Check(effectOf("click", StringFields({{"elementId", buttonId}, {"mode", "auto"}}), token, &code) == "unknown" && + code == kErrorInputUnavailable && driver.HasDepressedInput() && buttonClicks.load() == clicksBefore && + acquireElsewhere() == kErrorInputBusy, + "automatic click does not hide failed input cleanup behind accessibility fallback"); + driver.InputForTesting().UseTestEnvironment([]() { return InteractiveEnvironmentForTesting(); }); + Check(effectOf("releaseActions", empty, token, &code) == "unknown" && code == kErrorInputFailed && + driver.HasDepressedInput() && acquireElsewhere() == kErrorInputBusy, + "a failed key release retains the lock and reports an unknown effect"); + driver.InputForTesting().UseTestSender(&RecordInput); + Check(FailureCode([&]() { driver.Execute("releaseActions", empty, token); }) == "none" && + !driver.HasDepressedInput() && acquireElsewhere() == "acquired", + "a successful key-release retry frees the physical input lock"); + + driver.UseTestHitTester(&HitConfiguredWindow); + clickHitResult = window; + const json::Value holdButton = actions( + R"([{"type":"pointer","id":"mouse","parameters":{"pointerType":"mouse"},"actions":[{"type":"pointerDown","button":0}]}])"); + Check(FailureCode([&]() { driver.Execute("performActions", holdButton, token); }) == "none" && + driver.HasDepressedInput(), + "a pointer button is held for failed-release recovery"); + driver.InputForTesting().UseTestSender(&RejectSelectedReleases); + Check(effectOf("releaseActions", empty, token, &code) == "unknown" && code == kErrorInputFailed && + driver.HasDepressedInput() && acquireElsewhere() == kErrorInputBusy, + "a failed pointer release retains the lock and reports an unknown effect"); + driver.InputForTesting().UseTestSender(&RecordInput); + Check(FailureCode([&]() { driver.Execute("releaseActions", empty, token); }) == "none" && + !driver.HasDepressedInput() && acquireElsewhere() == "acquired", + "a successful pointer-release retry frees the physical input lock"); + // Abandoned: the provider never returns from the value write. ResetMutationQuarantineForTesting(); const auto gate = std::make_shared(); diff --git a/packages/agentic/desktop-driver/references/native-helpers.md b/packages/agentic/desktop-driver/references/native-helpers.md index fbbef8ce2a..3ea2775f6e 100644 --- a/packages/agentic/desktop-driver/references/native-helpers.md +++ b/packages/agentic/desktop-driver/references/native-helpers.md @@ -248,8 +248,13 @@ forwarded over the life of the helper. If the helper dies while input may be depressed, Node invokes the same verified binary in restricted `--release-input` mode with only the recorded keys and -buttons, bounded to 5 seconds and 16 KiB of output. Further physical input must -not proceed if release cannot be proven. +buttons, bounded to 5 seconds and 16 KiB of output. The adapter retains its +mirrored action, typing, and click candidates after an uncertain command or a +failed release until recovery succeeds. This mirror does not include keyboard +clear's synthetic shortcut keys, and Windows recovery cannot always reconstruct +the exact injected keyboard stroke. Recovery after helper exit therefore does +not prove that all input was released. On Windows, an abandoned mutex warns +only one acquirer and is not a persistent cross-process recovery barrier. The Windows helper also has three restricted one-shot modes, `--private-storage`, `--service-identity`, and `--application-incarnation`. The `/authority` adapters diff --git a/packages/agentic/desktop-driver/references/remaining-work.md b/packages/agentic/desktop-driver/references/remaining-work.md index 9b681f5635..ae4bca63c8 100644 --- a/packages/agentic/desktop-driver/references/remaining-work.md +++ b/packages/agentic/desktop-driver/references/remaining-work.md @@ -613,6 +613,10 @@ pointer hit check accepts any leased application; and Caps Lock state and the keyboard layout for injected keys come from the helper thread, not the foreground thread. +The Node crash-recovery mirror also omits keyboard clear's synthetic shortcut +keys and cannot always reconstruct the exact Windows stroke. Durable, +cross-process recovery of those inputs remains unproven. + Operation-time permission and capability changes must be surfaced truthfully even if session-start feature negotiation previously succeeded. The Windows input authority rechecks session, foreground, and integrity for every physical diff --git a/packages/agentic/desktop-driver/src/hosts/native/NativeDesktopHost.lifecycle.test.ts b/packages/agentic/desktop-driver/src/hosts/native/NativeDesktopHost.lifecycle.test.ts index 766f495761..f802803853 100644 --- a/packages/agentic/desktop-driver/src/hosts/native/NativeDesktopHost.lifecycle.test.ts +++ b/packages/agentic/desktop-driver/src/hosts/native/NativeDesktopHost.lifecycle.test.ts @@ -225,6 +225,7 @@ describe('NativeDesktopHost lifecycle', () => { describe('NativeDesktopHost input recovery', () => { const pressKeyA = [{ actions: [{ type: 'keyDown' as const, value: 'a' }], id: 'keys', type: 'key' as const }]; + const pressButton = [{ actions: [{ button: 0, type: 'pointerDown' as const }], id: 'pointer', type: 'pointer' as const }]; // The operator's input mask (hosted CI sets it) would refuse the key press before it reaches the scripted helper. let environment: ReturnType; @@ -252,6 +253,125 @@ describe('NativeDesktopHost input recovery', () => { expect(order).toEqual(['reject']); }); + test('retains the input ledger when release fails and the helper subsequently exits', async () => { + scriptNativeHelperSession(mockSpawn, (message, send, control) => { + if (message.command === 'probe') { + control.exit(1); + } else if (message.command === 'releaseActions') { + send({ error: { code: 'input-failed', effect: 'unknown', message: 'release failed' }, id: message.id, type: 'response' }); + } else { + send({ id: message.id, result: {}, type: 'response' }); + } + }); + const release = scriptReleaseInput(); + const host = macHost(); + + await host.performActions([...pressKeyA, ...pressButton]); + await expect(host.releaseActions()).rejects.toThrow('release failed'); + await expect(host.probe()).rejects.toMatchObject({ code: 'host-exited' }); + + expect(JSON.parse(release.input())).toEqual({ buttons: [0], keys: ['a'] }); + }); + + test.each([ + ['actions', (host: NativeDesktopHost) => host.performActions([...pressKeyA, ...pressButton]), { buttons: [0], keys: ['a'] }], + ['typing', (host: NativeDesktopHost) => host.sendKeys('element', 'a'), { buttons: [], keys: ['a'] }], + ['clicking', (host: NativeDesktopHost) => host.click('element', 'physical'), { buttons: [0], keys: [] }], + ])('retains possibly depressed input after failed %s until the helper exits', async (_, act, expected) => { + scriptNativeHelperSession(mockSpawn, (message, send, control) => { + if (message.command === 'probe') { + control.exit(1); + } else { + send({ error: { code: 'input-failed', effect: 'unknown', message: 'cleanup failed' }, id: message.id, type: 'response' }); + } + }); + const release = scriptReleaseInput(); + const host = macHost(); + + await expect(act(host)).rejects.toThrow('cleanup failed'); + await expect(host.probe()).rejects.toMatchObject({ code: 'host-exited' }); + + expect(JSON.parse(release.input())).toEqual(expected); + }); + + test('preserves uncertain input across later successful commands without adding their completed presses', async () => { + scriptNativeHelperSession(mockSpawn, (message, send, control) => { + if (message.command === 'probe') { + control.exit(1); + } else if (message.command === 'performActions') { + send({ error: { code: 'input-failed', effect: 'unknown', message: 'cleanup failed' }, id: message.id, type: 'response' }); + } else { + send({ id: message.id, result: {}, type: 'response' }); + } + }); + const release = scriptReleaseInput(); + const host = macHost(); + + await expect(host.performActions(pressKeyA)).rejects.toThrow('cleanup failed'); + await host.sendKeys('element', 'b'); + await expect(host.probe()).rejects.toMatchObject({ code: 'host-exited' }); + + expect(JSON.parse(release.input())).toEqual({ buttons: [], keys: ['a'] }); + }); + + test('forgets uncertain input only after a successful release', async () => { + let attempts = 0; + scriptNativeHelperSession(mockSpawn, (message, send, control) => { + if (message.command === 'probe') { + control.exit(1); + } else if (message.command === 'performActions' || (message.command === 'releaseActions' && attempts++ === 0)) { + send({ error: { code: 'input-failed', effect: 'unknown', message: 'cleanup failed' }, id: message.id, type: 'response' }); + } else { + send({ id: message.id, result: {}, type: 'response' }); + } + }); + const host = macHost(); + + await expect(host.performActions(pressKeyA)).rejects.toThrow('cleanup failed'); + await expect(host.releaseActions()).rejects.toThrow('cleanup failed'); + await expect(host.releaseActions()).resolves.toBeUndefined(); + await expect(host.probe()).rejects.toMatchObject({ code: 'host-exited' }); + + expect(mockSpawn).toHaveBeenCalledTimes(1); + }); + + test('does not recover input from a command rejected before dispatch', async () => { + scriptNativeHelperSession(mockSpawn, (message, send, control) => { + if (message.command === 'probe') { + control.exit(1); + } else { + send({ error: { code: 'invalid-params', effect: 'none', message: 'refused' }, id: message.id, type: 'response' }); + } + }); + const host = macHost(); + + await expect(host.performActions(pressKeyA)).rejects.toThrow('refused'); + await expect(host.probe()).rejects.toMatchObject({ code: 'host-exited' }); + + expect(mockSpawn).toHaveBeenCalledTimes(1); + }); + + test('retains previously held input when a later command is rejected before dispatch', async () => { + let actions = 0; + scriptNativeHelperSession(mockSpawn, (message, send, control) => { + if (message.command === 'probe') { + control.exit(1); + } else if (actions++ === 0) { + send({ id: message.id, result: {}, type: 'response' }); + } else { + send({ error: { code: 'invalid-params', effect: 'none', message: 'refused' }, id: message.id, type: 'response' }); + } + }); + const release = scriptReleaseInput(); + const host = macHost(); + + await host.performActions(pressKeyA); + await expect(host.performActions([{ ...pressKeyA[0], actions: [{ type: 'keyDown', value: 'b' }] }])).rejects.toThrow('refused'); + await expect(host.probe()).rejects.toMatchObject({ code: 'host-exited' }); + + expect(JSON.parse(release.input())).toEqual({ buttons: [], keys: ['a'] }); + }); + test('keeps only a bounded amount of a failed recovery’s stderr', async () => { scriptNativeHelperSession(mockSpawn, (_, __, control) => control.exit(1), [], {}); scriptReleaseInput({ code: 1, stderr: 'x'.repeat(1024 * 1024) }); diff --git a/packages/agentic/desktop-driver/src/hosts/native/NativeDesktopHost.ts b/packages/agentic/desktop-driver/src/hosts/native/NativeDesktopHost.ts index b6d65b5463..0cede1771d 100644 --- a/packages/agentic/desktop-driver/src/hosts/native/NativeDesktopHost.ts +++ b/packages/agentic/desktop-driver/src/hosts/native/NativeDesktopHost.ts @@ -75,6 +75,8 @@ export class NativeDesktopHost implements DesktopHost { private readonly listeners = new Set<(event: DesktopHostEvent) => void>(); private readonly depressedButtons = new Set(); private readonly depressedKeys = new Set(); + private readonly uncertainButtons = new Set(); + private readonly uncertainKeys = new Set(); private potentialButtons = new Set(); private potentialKeys = new Set(); private failure?: Error; @@ -170,12 +172,15 @@ export class NativeDesktopHost implements DesktopHost { } const includePointer = mode !== 'accessibility'; if (includePointer) { - this.potentialButtons = new Set([...this.depressedButtons, 0]); + this.potentialButtons.add(0); } try { await this.request('click', { elementId, mode }, signal); + } catch (error) { + if (includePointer) this.rememberUncertainInput(error); + throw error; } finally { - this.potentialButtons = new Set(this.depressedButtons); + this.refreshPotentialInput(); } } @@ -206,11 +211,14 @@ export class NativeDesktopHost implements DesktopHost { if (this.disabledInputFeatures.has('keyboard')) { throw rejectedBeforeDispatch(new HostUnsupportedError('Physical keyboard input is disabled by the registered target policy.')); } - this.potentialKeys = new Set([...this.depressedKeys, ...typedTextRecoveryKeys(text)]); + this.potentialKeys = new Set([...this.potentialKeys, ...typedTextRecoveryKeys(text)]); try { await this.request('sendKeys', { elementId, text }, signal); + } catch (error) { + this.rememberUncertainInput(error); + throw error; } finally { - this.potentialKeys = new Set(this.depressedKeys); + this.refreshPotentialInput(); } } @@ -232,32 +240,29 @@ export class NativeDesktopHost implements DesktopHost { } catch (error) { throw rejectedBeforeDispatch(error); } - this.potentialKeys = plan.potentialKeys; - this.potentialButtons = plan.potentialButtons; + this.potentialKeys = new Set([...this.potentialKeys, ...plan.potentialKeys]); + this.potentialButtons = new Set([...this.potentialButtons, ...plan.potentialButtons]); try { await this.request('performActions', { actions }, signal); replaceSet(this.depressedKeys, plan.finalKeys); replaceSet(this.depressedButtons, plan.finalButtons); } catch (error) { - this.depressedKeys.clear(); - this.depressedButtons.clear(); + this.rememberUncertainInput(error); throw error; } finally { - this.potentialKeys = new Set(this.depressedKeys); - this.potentialButtons = new Set(this.depressedButtons); + this.refreshPotentialInput(); } } async releaseActions(signal?: AbortSignal): Promise { - this.potentialKeys = new Set(this.depressedKeys); - this.potentialButtons = new Set(this.depressedButtons); try { await this.request('releaseActions', undefined, signal); + this.clearInputLedger(); + } catch (error) { + this.rememberUncertainInput(error); + throw error; } finally { - this.depressedKeys.clear(); - this.depressedButtons.clear(); - this.potentialKeys.clear(); - this.potentialButtons.clear(); + this.refreshPotentialInput(); } } @@ -345,6 +350,26 @@ export class NativeDesktopHost implements DesktopHost { await process?.dispose(signal); } + private rememberUncertainInput(error: unknown): void { + if (readFailureEffect(error) === 'none') return; + for (const key of this.potentialKeys) this.uncertainKeys.add(key); + for (const button of this.potentialButtons) this.uncertainButtons.add(button); + } + + private refreshPotentialInput(): void { + this.potentialKeys = new Set([...this.depressedKeys, ...this.uncertainKeys]); + this.potentialButtons = new Set([...this.depressedButtons, ...this.uncertainButtons]); + } + + private clearInputLedger(): void { + this.depressedKeys.clear(); + this.depressedButtons.clear(); + this.uncertainKeys.clear(); + this.uncertainButtons.clear(); + this.potentialKeys.clear(); + this.potentialButtons.clear(); + } + private targetParams(target: DesktopTarget): Record { return { application: this.application, @@ -465,10 +490,7 @@ export class NativeDesktopHost implements DesktopHost { } try { JSON.parse(stdout.text()); - this.depressedKeys.clear(); - this.depressedButtons.clear(); - this.potentialKeys.clear(); - this.potentialButtons.clear(); + this.clearInputLedger(); resolve(); } catch (error) { reject(new NativeDriverError('input-recovery-failed', error instanceof Error ? error.message : String(error))); diff --git a/packages/agentic/desktop-driver/src/hosts/native/NativeHostProcess.test.ts b/packages/agentic/desktop-driver/src/hosts/native/NativeHostProcess.test.ts index 1ccc991641..6a045f918f 100644 --- a/packages/agentic/desktop-driver/src/hosts/native/NativeHostProcess.test.ts +++ b/packages/agentic/desktop-driver/src/hosts/native/NativeHostProcess.test.ts @@ -213,6 +213,16 @@ describe('NativeHostProcess failure paths', () => { expect(recoverInput).toHaveBeenCalledTimes(1); }); + test('reports a recovery failure together with a refused disposal', async () => { + const recoveryError = new Error('release failed'); + const { host } = await startHost(() => undefined, { dispose: 'reject' }, { recoverInput: () => Promise.reject(recoveryError) }); + + const failure = await failureOf(host.dispose()); + + expect(failure).toBeInstanceOf(AggregateError); + expect((failure as AggregateError).errors).toEqual([expect.objectContaining({ code: 'input-failed' }), recoveryError]); + }); + test('does not mistake an exit that overtakes the final acknowledgement for a crash', async () => { const recoverInput = jest.fn(() => Promise.resolve()); const { control, host } = await startHost(() => undefined, { dispose: 'exit-then-acknowledge', pipeHold: 60 }, { recoverInput }); diff --git a/packages/agentic/desktop-driver/src/hosts/native/NativeHostProcess.ts b/packages/agentic/desktop-driver/src/hosts/native/NativeHostProcess.ts index 739bd85c62..8c88217b1a 100644 --- a/packages/agentic/desktop-driver/src/hosts/native/NativeHostProcess.ts +++ b/packages/agentic/desktop-driver/src/hosts/native/NativeHostProcess.ts @@ -237,8 +237,8 @@ export class NativeHostProcess extends EventEmitter<{ try { await this.send('dispose', undefined, signal); } catch (error) { - await this.fail(asError(error)); - throw error; + const failure = await this.fail(asError(error)); + throw failure; } this.disposeAcknowledged = true; if (!(await settlesWithin(this.closed, disposeExitGraceMs))) { diff --git a/packages/agentic/desktop-driver/src/native/nativeDriver.test.ts b/packages/agentic/desktop-driver/src/native/nativeDriver.test.ts index c24670b76d..6c6b19ed49 100644 --- a/packages/agentic/desktop-driver/src/native/nativeDriver.test.ts +++ b/packages/agentic/desktop-driver/src/native/nativeDriver.test.ts @@ -437,7 +437,15 @@ describe('native driver build and resolution', () => { env: { ...process.env, FURN_DRIVER_SELF_TEST_OBSERVATIONS: observationDump }, timeout: 120_000, }); - expect(selfTest.status).toBe(0); + if (selfTest.status !== 0) { + const failures = (selfTest.stdout ?? '') + .split('\n') + .filter((line) => line.startsWith('not ok')) + .join('\n'); + throw new Error( + `Windows helper self-test exited ${String(selfTest.status)}: ${failures || selfTest.stderr || selfTest.error?.message || 'no diagnostic output'}`, + ); + } expect(selfTest.stdout).toContain('self-test passed'); expectSelfTestObservationsToConform(observationDump); diff --git a/packages/agentic/desktop-driver/src/testing/scriptedNativeHelper.ts b/packages/agentic/desktop-driver/src/testing/scriptedNativeHelper.ts index c23c8ce311..7c49e1e3f1 100644 --- a/packages/agentic/desktop-driver/src/testing/scriptedNativeHelper.ts +++ b/packages/agentic/desktop-driver/src/testing/scriptedNativeHelper.ts @@ -24,9 +24,10 @@ export type ScriptedNativeControl = { export type ScriptedNativeOptions = { /** * What the helper does with `dispose`. `acknowledge` replies and exits. `exit-then-acknowledge` exits first and - * replies a moment later, as a helper whose exit overtakes its final output. `ignore` never replies. + * replies a moment later, as a helper whose exit overtakes its final output. `reject` reports a failure and + * `ignore` never replies. */ - dispose?: 'acknowledge' | 'exit-then-acknowledge' | 'ignore'; + dispose?: 'acknowledge' | 'exit-then-acknowledge' | 'ignore' | 'reject'; /** The bytes sent at startup, given the encoded hello. Return nothing to stay silent. */ greeting?: (hello: Buffer) => Uint8Array | undefined; /** Called for each `cancel`. Without it the helper ignores cancellation. */ @@ -161,6 +162,8 @@ export function scriptNativeHelperSession( } else if (mode === 'exit-then-acknowledge') { child.exit(0); setTimeout(() => send({ id, result: {}, type: 'response' }), 5); + } else if (mode === 'reject') { + send({ error: { code: 'input-failed', message: 'Dispose could not release input.' }, id, type: 'response' }); } }; child.stdin.on('data', (chunk: Buffer) => decoder.write(chunk)); diff --git a/packages/agentic/desktop-test/src/commands.test.ts b/packages/agentic/desktop-test/src/commands.test.ts index c96e2afd4e..e0960f0161 100644 --- a/packages/agentic/desktop-test/src/commands.test.ts +++ b/packages/agentic/desktop-test/src/commands.test.ts @@ -1,4 +1,4 @@ -import { spawnSync } from 'node:child_process'; +import { spawn, spawnSync } from 'node:child_process'; import type { SpawnSyncReturns } from 'node:child_process'; import fs from 'node:fs'; import os from 'node:os'; @@ -10,7 +10,7 @@ import { DesktopRunReporter } from './reporting/collector.js'; jest.mock('node:child_process', () => { const actual = jest.requireActual('node:child_process'); - return { ...actual, spawnSync: jest.fn(actual.spawnSync) }; + return { ...actual, spawn: jest.fn(actual.spawn), spawnSync: jest.fn(actual.spawnSync) }; }); const quiet = { write: () => true }; @@ -172,16 +172,22 @@ describe('desktop command cancellation and failure', () => { ); await waitUntil(() => fs.existsSync(marker) && fs.readFileSync(marker, 'utf8').length > 0); const [leaderPid, grandchildPid] = JSON.parse(fs.readFileSync(marker, 'utf8')) as [number, number]; + const child = jest.mocked(spawn).mock.results.at(-1)?.value; + if (!child || child.pid !== leaderPid) throw new Error('The runner did not spawn the expected fixture leader.'); try { // Once the leader is reaped, its exit is recorded while the grandchild still holds the output pipes open. - await waitUntil(() => !alive(leaderPid)); + await waitUntil(() => child.exitCode !== null || child.signalCode !== null); + taskkill.mockImplementation((() => respond(0)) as unknown as typeof spawnSync); emulateWindows(); runner.terminate(); await running.stop({ failed: true }).catch(() => undefined); expect(taskkillCalls()).toEqual([]); } finally { restorePlatform?.(); - if (alive(grandchildPid)) process.kill(grandchildPid, 'SIGKILL'); + if (alive(grandchildPid)) { + process.kill(grandchildPid, 'SIGKILL'); + await waitUntil(() => !alive(grandchildPid)); + } } }); @@ -210,7 +216,10 @@ describe('desktop command cancellation and failure', () => { await expect(running.stop()).rejects.toThrow(`Could not stop owned process tree ${pid}: ERROR: Access is denied.`); } finally { restorePlatform?.(); - process.kill(-pid, 'SIGKILL'); + if (alive(pid)) { + process.kill(process.platform === 'win32' ? pid : -pid, 'SIGKILL'); + await waitUntil(() => !alive(pid)); + } } }); }); diff --git a/packages/agentic/desktop-test/src/processControl.test.ts b/packages/agentic/desktop-test/src/processControl.test.ts index 7695866f29..6896a454e8 100644 --- a/packages/agentic/desktop-test/src/processControl.test.ts +++ b/packages/agentic/desktop-test/src/processControl.test.ts @@ -188,12 +188,22 @@ describe('owned process lifelines', () => { test.each(['failed', 'stalled'])('%s disposal is incomplete, never a successful cleanup', async (mode) => { const marker = path.join(root, 'descriptor'); - const runner = new NodeDesktopCommandRunner({ logDirectory: root, output: quiet, errorOutput: quiet, shutdownTimeoutMs: 200 }); + // Windows taskkill needs time to start after the cooperative disposal deadline. + const shutdownTimeoutMs = process.platform === 'win32' ? 5000 : 200; + const runner = new NodeDesktopCommandRunner({ logDirectory: root, output: quiet, errorOutput: quiet, shutdownTimeoutMs }); const running = runner.start({ command: process.execPath, args: [fixture, mode, marker], cwd: root, env: {} }); - await running.waitForReadiness!({ tier: 'service-ready', service: 'fixture', descriptor: 'fixture.json' }, 5000); - await expect(running.stop()).rejects.toThrow(/disposal|settle/); - expect(fs.existsSync(marker)).toBe(true); - await runner.close(); + try { + await running.waitForReadiness!({ tier: 'service-ready', service: 'fixture', descriptor: 'fixture.json' }, 5000); + await expect(running.stop()).rejects.toThrow(/disposal|settle/); + await deadline( + running.completed.catch(() => undefined), + 5000, + 'Reaping the fixture after failed disposal', + ); + expect(fs.existsSync(marker)).toBe(true); + } finally { + await runner.close(); + } }); test('owned service wrappers cannot certify a failed child disposal', async () => { diff --git a/packages/agentic/desktop-test/src/reporting/runQueries.test.ts b/packages/agentic/desktop-test/src/reporting/runQueries.test.ts index 74a010a88d..57ac600a41 100644 --- a/packages/agentic/desktop-test/src/reporting/runQueries.test.ts +++ b/packages/agentic/desktop-test/src/reporting/runQueries.test.ts @@ -362,7 +362,7 @@ describe('persisted desktop run queries', () => { ); await reporter.finish(); const page = (await client.query({ operation: 'failures', runId: reporter.runId })) as DesktopRunPage; - expect(page.items[0].message).toBe('Cannot write /commands/output.log under '); + expect(page.items[0].message).toBe(`Cannot write ${path.sep}commands${path.sep}output.log under `); }); test('malformed/inconsistent persisted data never returns a success-shaped fallback', async () => {