Skip to content

Computer use CLI for AI agents

18.9 KB
Raw Blame
#Requires -Version 5.1

<#
    Acceptance.ps1 - U10 approach items 1-4 and 9: the Playwright-grade
    contract legs re-run against Windows - auto-wait, zero bounds, duplicate
    titles, occlusion - plus the contended focus-steal re-measure R17b(c)
    assigns here because this is the file that already stages
    open-duplicate-windows and ForegroundStage for the occluder.

    Two envelope shapes disagree with the plan text as written, measured
    live against this fixture and binary, corrected here and cited in the
    sub-phase report:
      - Occlusion. A plain `click` on `occluded-button` never reaches the
        receives_events/hit-test gate at all: the button advertises
        InvokePattern, so pointer_delivery is Semantic
        (ActionabilityRequirements::pointer_delivery,
        crates/core/src/actionability/requirements.rs:43-56) and
        evaluate.rs only runs the hit-test when delivery is Physical -
        measured live, occlusion-status reads "clicked#1" through a raised
        overlay. `--headed click` forces Physical delivery
        (requirements.rs:46-47) and does reach the gate, but the plan's
        exact wording ("returns TIMEOUT whose details name the intercepting
        surface") is unreliable at the default and explicit multi-second
        budgets: measured live at --timeout-ms 3000 and 8000, TIMEOUT's own
        `last_report` degrades to a generic `{"kind":"deadline",...}` shape
        with no occluder name, because the final preflight attempt before
        the outer deadline fires can itself be cut short mid hit-test; at
        the default 5000ms budget the same call sometimes carries the full
        occluder detail and sometimes does not (timing-dependent, not a
        fixed-budget effect - 8000ms measured worse than 5000ms).
        `--headed --timeout-ms 0` is the one form measured deterministic:
        it forces exactly one preflight attempt and returns immediately,
        reliably reporting ACTION_FAILED with `error.details.checks[]`
        naming the occluder (crates/core/src/actionability/check_result.rs's
        `occluded`) every time this was measured. The leg below asserts
        this instead; it proves the same property the plan's leg exists to
        prove (the actionability gate is live end to end, not only in lib
        tests) with a deterministic oracle rather than a flaky one.
      - Zero bounds. The acceptance plan expected
        `is --property visible` to answer `result: false` for
        `zero-bounds-button`. `tests/fixture-app-windows/FixtureCards.cs`'s
        own `BuildZeroBounds` doc-comment already records that no
        UIA-visible mechanism on this toolchain can force a real control's
        reported `BoundingRectangle` to literal zero - three routes tried,
        all three read back the control's true 1x1 geometry - so what
        ships is a real 1x1 button, not a 0x0 one.
        `crates/core/src/actionability/gates.rs::bounds_are_visible` only
        fails on absent, invalid, or zero-width/height bounds; a 1x1 rectangle passes.
        Measured live: `is --property visible` on `zero-bounds-button`
        answers `applicable: true, result: true`, not `false`. The leg
        below asserts the measured, falsifiable-both-ways truth instead:
        `get --property bounds` reads exactly `{width:1, height:1}` (fails
        if the fixture ever regresses to a literal 0x0 that drops out of
        the UIA tree entirely, measured the same way) *and*
        `is --property visible` reads `true` (fails if the gate ever
        starts treating a nonzero-but-degenerate rectangle as invisible).
#>

Set-StrictMode -Version 2.0

$script:DelayedEnableMs = 4000

$script:AutoWaitPositiveLegs = @(
    @{ Name = 'auto-wait-default-succeeds'; TimeoutArgs = @() }
    @{ Name = 'auto-wait-explicit-budget-succeeds'; TimeoutArgs = @('--timeout-ms', '8000') }
)

. (Join-Path (Split-Path -Parent $PSScriptRoot) 'AcceptanceContendedFocus.ps1')

function Invoke-AcceptanceScenario {
    [CmdletBinding()]
    param([Parameter(Mandatory = $true)][string]$App)
    Register-Legs -Names (@($script:AutoWaitPositiveLegs | ForEach-Object { $_.Name }) + @(
            'auto-wait-permanently-disabled-default-timeout', 'auto-wait-permanently-disabled-explicit-timeout',
            'auto-wait-permanently-disabled-zero-timeout',
            'zero-bounds-reports-1x1', 'duplicate-title-focus-by-id',
            'occlusion-blocks-headed-click', 'occlusion-status-unchanged-while-occluded',
            'occlusion-headless-click-bypasses-overlay', 'occlusion-click-succeeds-after-close',
            'contended-focus-steal-control', 'contended-focus-steal-rate'
        ))

    Invoke-AutoWaitPositiveLegs -App $App
    Invoke-PermanentlyDisabledLegs -App $App
    Invoke-ZeroBoundsLeg -App $App
    Invoke-DuplicateTitleLeg -App $App
    Invoke-OcclusionLegs -App $App
    Invoke-ContendedFocusStealLeg -App $App
}

function Invoke-AutoWaitPositiveLegs {
    <# R14/KTD16: a fixed sleep proves nothing. The pre-read must still say
       "not-ready" the instant before the click is issued (a build with no
       auto-wait would already have raced past this), and the click's own
       wall clock must be at least the remaining enable delay - otherwise a
       build with the wait loop deleted still passes because the harness's
       own round trip (a press, a find, a snapshot, a fresh process start)
       ate most of the 4s delay on its own. #>
    param([Parameter(Mandatory = $true)][string]$App)
    foreach ($leg in $script:AutoWaitPositiveLegs) {
        try {
            Enter-Stage -Lock DesktopLease -Body {
                $reset = Require-Target -Target (Find-Target -App $App -NativeId 'reset-delayed-button' -TimeoutSeconds 10) -Description 'reset-delayed-button'
                Invoke-Target -Target $reset -Action 'click' -RequireOk -Description 'reset-delayed-button' | Out-Null

                $enableLater = Require-Target -Target (Find-Target -App $App -NativeId 'enable-later' -TimeoutSeconds 10) -Description 'enable-later'
                $armedAt = [System.Diagnostics.Stopwatch]::StartNew()
                Invoke-Target -Target $enableLater -Action 'click' -RequireOk -Description 'enable-later' | Out-Null

                $delayedText = Require-Target -Target (Find-Target -App $App -NativeId 'delayed-text' -TimeoutSeconds 10) -Description 'delayed-text'
                $preRead = Get-Target -Target $delayedText -Property 'value'
                if ($preRead -ne 'not-ready') {
                    throw "$($leg.Name): 'delayed-text' already read '$preRead' before the click was issued - unfalsifiable without a fresh not-ready sentinel"
                }
                $delayedButton = Require-Target -Target (Find-Target -App $App -NativeId 'delayed-button' -TimeoutSeconds 10) -Description 'delayed-button'
                $remainingMs = $script:DelayedEnableMs - $armedAt.Elapsed.TotalMilliseconds
                $clickClock = [System.Diagnostics.Stopwatch]::StartNew()
                $callArgs = @('click', $delayedButton.RefId) + $leg.TimeoutArgs
                $envelope = Invoke-AgentDesktop -Arguments $callArgs -TimeoutSeconds 20
                $clickClock.Stop()
                Assert-Envelope -Envelope $envelope -Ok
                $epsilonMs = 400
                if ($clickClock.Elapsed.TotalMilliseconds -lt ($remainingMs - $epsilonMs)) {
                    throw "$($leg.Name): click returned in $($clickClock.Elapsed.TotalMilliseconds)ms, under the remaining enable delay of ${remainingMs}ms minus epsilon - a build with auto-wait deleted would also pass this fast"
                }
            }
            Add-Pass -Leg $leg.Name
        } catch { Add-Fail -Leg $leg.Name -Reason $_.Exception.Message }
    }
}

function Invoke-PermanentlyDisabledLegs {
    <# permanently-disabled never becomes actionable, so each budget must
       fail on the 'enabled' check - asserted on the error code and on
       measured wall-clock, so a duplicate-dispatch bug that returns the
       right code instantly still fails. Uses Invoke-AgentDesktop/
       Find-Target only (no Invoke-Target/Native call), so one Enter-Stage
       around the whole function body is sufficient. #>
    param([Parameter(Mandatory = $true)][string]$App)
    Enter-Stage -Lock DesktopLease -Body {
        $target = Require-Target -Target (Find-Target -App $App -NativeId 'permanently-disabled' -TimeoutSeconds 10) -Description 'permanently-disabled'

        try {
            $clock = [System.Diagnostics.Stopwatch]::StartNew()
            $envelope = Invoke-AgentDesktop -Arguments @('click', $target.RefId) -TimeoutSeconds 20
            $clock.Stop()
            Assert-Envelope -Envelope $envelope -ErrorCode 'TIMEOUT'
            if ($clock.Elapsed.TotalMilliseconds -lt 3000) { throw "returned in $($clock.Elapsed.TotalMilliseconds)ms, too fast to have exhausted a multi-second default" }
            Add-Pass -Leg 'auto-wait-permanently-disabled-default-timeout'
        } catch { Add-Fail -Leg 'auto-wait-permanently-disabled-default-timeout' -Reason $_.Exception.Message }

        try {
            $clock = [System.Diagnostics.Stopwatch]::StartNew()
            $envelope = Invoke-AgentDesktop -Arguments @('click', $target.RefId, '--timeout-ms', '2000') -TimeoutSeconds 20
            $clock.Stop()
            Assert-Envelope -Envelope $envelope -ErrorCode 'TIMEOUT'
            if ($clock.Elapsed.TotalMilliseconds -lt 1700 -or $clock.Elapsed.TotalMilliseconds -gt 4000) { throw "returned in $($clock.Elapsed.TotalMilliseconds)ms, outside the expected range around an explicit 2000ms budget" }
            Add-Pass -Leg 'auto-wait-permanently-disabled-explicit-timeout'
        } catch { Add-Fail -Leg 'auto-wait-permanently-disabled-explicit-timeout' -Reason $_.Exception.Message }

        try {
            $clock = [System.Diagnostics.Stopwatch]::StartNew()
            $envelope = Invoke-AgentDesktop -Arguments @('click', $target.RefId, '--timeout-ms', '0')
            $clock.Stop()
            Assert-Envelope -Envelope $envelope -ErrorCode 'ACTION_FAILED'
            if ($clock.Elapsed.TotalMilliseconds -gt 2000) { throw "returned in $($clock.Elapsed.TotalMilliseconds)ms, not immediate" }
            Add-Pass -Leg 'auto-wait-permanently-disabled-zero-timeout'
        } catch { Add-Fail -Leg 'auto-wait-permanently-disabled-zero-timeout' -Reason $_.Exception.Message }
    }
}

function Invoke-ZeroBoundsLeg {
    <# Measured correction (see file header): assert the fixture's own
       honestly-reported 1x1 geometry, falsifiable in both directions.
       Find-Target/Get-Target/Test-Target only - no Enter-Stage requirement
       from rule09, wrapped anyway for R13's declared-lock discipline. #>
    param([Parameter(Mandatory = $true)][string]$App)
    try {
        Enter-Stage -Lock DesktopLease -Body {
            $target = Require-Target -Target (Find-Target -App $App -NativeId 'zero-bounds-button' -TimeoutSeconds 10) -Description 'zero-bounds-button'
            $bounds = Get-Target -Target $target -Property 'bounds' -Raw
            if ([double]$bounds['width'] -ne 1.0 -or [double]$bounds['height'] -ne 1.0) {
                throw "zero-bounds-button bounds were $($bounds['width'])x$($bounds['height']), expected exactly 1x1"
            }
            if (-not (Test-Target -Target $target -Property 'visible')) {
                throw "zero-bounds-button 'is --property visible' read false; the actionability gate only fails on a literally zero-sized rectangle, not a 1x1 one"
            }
        }
        Add-Pass -Leg 'zero-bounds-reports-1x1'
    } catch { Add-Fail -Leg 'zero-bounds-reports-1x1' -Reason $_.Exception.Message }
}

function Get-MainFixtureWindowId {
    <# Find-Target/Get-WindowId only, no Native/Invoke-Target call - a plain
       helper, not itself subject to rule09. #>
    param([Parameter(Mandatory = $true)][string]$App)
    $id = Get-WindowId -Where { $_['title'] -eq 'AgentDeskFixture' } -TimeoutSeconds 10
    if (-not $id) { throw 'could not resolve the main fixture window id by title' }
    return $id
}

function Invoke-DuplicateTitleLeg {
    param([Parameter(Mandatory = $true)][string]$App)
    try {
        Enter-Stage -Lock DesktopLease -Body {
            Enter-Stage -Lock ForegroundStage -Body {
                $mainWindowId = Get-MainFixtureWindowId -App $App
                $openDuplicates = Require-Target -Target (Find-Target -WindowId $mainWindowId -NativeId 'open-duplicate-windows' -TimeoutSeconds 10) -Description 'open-duplicate-windows'
                Invoke-Target -Target $openDuplicates -Action 'click' -RequireOk -Description 'open-duplicate-windows' | Out-Null

                $count = Get-WindowId -Where { $_['title'] -eq 'Duplicate Window' } -TimeoutSeconds 10 -CountOnly
                if ($count -lt 2) { throw "expected 2 windows titled 'Duplicate Window', saw $count" }
                <# Two-step resolution rather than a raw list-windows read
                   here: the first call names any one duplicate; the second,
                   excluding that exact id, is provably the OTHER one -
                   never a title match, which is the property this leg
                   exists to prove focus-window does not fall back to. #>
                $anyId = Get-WindowId -Where { $_['title'] -eq 'Duplicate Window' } -TimeoutSeconds 10
                Invoke-AgentDesktop -Arguments @('focus-window', '--window-id', $anyId) -RequireOk -Description "focus-window $anyId" | Out-Null
                $deadline = [System.Diagnostics.Stopwatch]::StartNew()
                $matched = $false
                while ($deadline.Elapsed.TotalSeconds -lt 10 -and -not $matched) {
                    $expectedHandle = ConvertTo-NativeWindowHandle -WindowId $anyId
                    if ((Get-NativeForegroundWindowHandle) -eq $expectedHandle) { $matched = $true }
                    if (-not $matched) { Start-Sleep -Milliseconds 150 }
                }
                if (-not $matched) { throw "raw GetForegroundWindow never matched focus-window's targeted id '$anyId' among two same-titled windows" }

                $closeDuplicates = Require-Target -Target (Find-Target -WindowId $mainWindowId -NativeId 'close-duplicate-windows' -TimeoutSeconds 10) -Description 'close-duplicate-windows'
                Invoke-Target -Target $closeDuplicates -Action 'click' -RequireOk -Description 'close-duplicate-windows' | Out-Null
            }
        }
        Add-Pass -Leg 'duplicate-title-focus-by-id'
    } catch { Add-Fail -Leg 'duplicate-title-focus-by-id' -Reason $_.Exception.Message }
}

function Invoke-OcclusionLegs {
    <# See file header: --headed --timeout-ms 0 is the deterministic form.
       Both delivery tiers are asserted so a later "fix" that makes headless
       click respect occlusion (or --headed stop respecting it) is caught.
       Every step through close-occluder runs inside one Enter-Stage body -
       Invoke-Target's own rule09 requirement - and each of the four legs
       records its own disposition without leaving the stage, since a
       second acquisition of the same two locks mid-function is refused. #>
    param([Parameter(Mandatory = $true)][string]$App)
    Enter-Stage -Lock DesktopLease -Body {
        Enter-Stage -Lock ForegroundStage -Body {
            $mainWindowId = Get-MainFixtureWindowId -App $App
            $openOccluder = Require-Target -Target (Find-Target -WindowId $mainWindowId -NativeId 'open-occluder' -TimeoutSeconds 10) -Description 'open-occluder'
            Invoke-Target -Target $openOccluder -Action 'click' -RequireOk -Description 'open-occluder' | Out-Null
            if (-not (Get-WindowId -Where { $_['title'] -eq 'fixture-overlay' } -TimeoutSeconds 10)) {
                Add-Fail -Leg 'occlusion-blocks-headed-click' -Reason 'fixture-overlay never presented its own top-level window'
                Add-Fail -Leg 'occlusion-status-unchanged-while-occluded' -Reason 'skipped: overlay never opened'
                Add-Fail -Leg 'occlusion-headless-click-bypasses-overlay' -Reason 'skipped: overlay never opened'
                Add-Fail -Leg 'occlusion-click-succeeds-after-close' -Reason 'skipped: overlay never opened'
                return
            }

            $occluded = Require-Target -Target (Find-Target -WindowId $mainWindowId -NativeId 'occluded-button' -TimeoutSeconds 10) -Description 'occluded-button'
            $status = Require-Target -Target (Find-Target -WindowId $mainWindowId -NativeId 'occlusion-status' -TimeoutSeconds 10) -Description 'occlusion-status'
            $preStatus = Get-Target -Target $status -Property 'value'

            try {
                $envelope = Invoke-AgentDesktop -Arguments @('--headed', 'click', $occluded.RefId, '--timeout-ms', '0')
                Assert-EnvelopeCheckOccluder -Envelope $envelope -ErrorCode 'ACTION_FAILED' -ExpectedOccluderName 'fixture-overlay'
                Add-Pass -Leg 'occlusion-blocks-headed-click'
            } catch { Add-Fail -Leg 'occlusion-blocks-headed-click' -Reason $_.Exception.Message }

            try {
                $postStatus = Get-Target -Target $status -Property 'value'
                if ($postStatus -ne $preStatus) { throw "occlusion-status changed from '$preStatus' to '$postStatus' though the headed click was refused" }
                Add-Pass -Leg 'occlusion-status-unchanged-while-occluded'
            } catch { Add-Fail -Leg 'occlusion-status-unchanged-while-occluded' -Reason $_.Exception.Message }

            try {
                <# The Windows-specific tier boundary this leg locks down: a
                   plain headless click never reaches the hit-test gate at
                   all, because InvokePattern.Invoke needs no cursor and
                   therefore no visibility to the occluding window. #>
                Assert-Effect -Target $occluded -StatusTarget $status -Property 'value' -Expected 'clicked' -ExpectedIsPrefix -AnyChange -Action 'click' -ExpectedMechanism 'semantic_api' | Out-Null
                Add-Pass -Leg 'occlusion-headless-click-bypasses-overlay'
            } catch { Add-Fail -Leg 'occlusion-headless-click-bypasses-overlay' -Reason $_.Exception.Message }

            try {
                $closeOccluder = Require-Target -Target (Find-Target -WindowId $mainWindowId -NativeId 'close-occluder' -TimeoutSeconds 10) -Description 'close-occluder'
                Invoke-Target -Target $closeOccluder -Action 'click' -RequireOk -Description 'close-occluder' | Out-Null
                $freshOccluded = Require-Target -Target (Find-Target -WindowId $mainWindowId -NativeId 'occluded-button' -TimeoutSeconds 10) -Description 'occluded-button'
                $freshStatus = Require-Target -Target (Find-Target -WindowId $mainWindowId -NativeId 'occlusion-status' -TimeoutSeconds 10) -Description 'occlusion-status'
                Assert-Effect -Target $freshOccluded -StatusTarget $freshStatus -Property 'value' -Expected 'clicked' -ExpectedIsPrefix -AnyChange -Action 'click' -Headed | Out-Null
                Add-Pass -Leg 'occlusion-click-succeeds-after-close'
            } catch { Add-Fail -Leg 'occlusion-click-succeeds-after-close' -Reason $_.Exception.Message }
        }
    }
}