dsh-windows-parallel-mcp 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -140,8 +140,13 @@ function Get-WpmProofWindow {
140
140
 
141
141
  function Invoke-WpmAction {
142
142
  param(
143
- [Parameter(Mandatory)][string]$Hwnd,
144
- [Parameter(Mandatory)][ValidateSet('click', 'type', 'focus', 'status')][string]$Action,
143
+ # NOT Mandatory any more: a full-screen screenshot (screenshot + fullScreen:true) legitimately has
144
+ # no target window, and PowerShell REFUSES to bind an empty string to a Mandatory [string]
145
+ # parameter -- which surfaced as "cannot bind argument to parameter 'Hwnd' because it is an empty
146
+ # string" instead of the capture the caller asked for. Every windowed action still checks liveness
147
+ # itself and returns no_hwnd, so nothing was made lenient except the binding.
148
+ [string]$Hwnd = '',
149
+ [Parameter(Mandatory)][ValidateSet('click', 'type', 'focus', 'status', 'snapshot', 'screenshot', 'preview')][string]$Action,
145
150
  [int]$X = -1,
146
151
  [int]$Y = -1,
147
152
  [string]$Text = '',
@@ -156,7 +161,32 @@ function Invoke-WpmAction {
156
161
  [int]$TakeoverMaxMs = 10000,
157
162
  [switch]$KeyInject,
158
163
  [int]$TakeoverArmMs = 800,
159
- [int]$HudDoneMs = 1500
164
+ [int]$HudDoneMs = 1500,
165
+ # ---- 0.5.0 read paths and element addressing ------------------------------------------------
166
+ # `Space` is the coordinate space of X/Y for click. 'image' means the numbers came from a
167
+ # screenshot and the ENGINE converts them: the caller never multiplies a scale factor, which is
168
+ # the failure the upstream library documents against itself ("Click lands in the wrong place").
169
+ [ValidateSet('screen', 'image')][string]$Space = 'screen',
170
+ [string]$ShotId = '',
171
+ # A label is a reference into the LAST frame this worker printed. It is resolved back to the UIA
172
+ # element by RuntimeId, so a labelled action still takes the silent pattern path.
173
+ [int]$Label = 0,
174
+ [string]$SnapId = '',
175
+ [int]$MaxElements = 300,
176
+ [bool]$InteractableOnly = $true,
177
+ [string]$SinceSnapId = '',
178
+ [string]$Intent = '',
179
+ [bool]$FullScreen = $false,
180
+ [int]$MaxWidth = 0,
181
+ [int]$ShotsKeep = 10,
182
+ # ---- 0.6.0: where the caret should be when the message door types (type only) ----------------
183
+ # `idle` is the existing behaviour and stays the default: nothing is touched, so an upgrade cannot
184
+ # start moving carets on its own. `end` is implemented as EM_SETSEL(len,len) with len read from the
185
+ # control, NOT as the documented -1 -- measured on WinForms EDIT, EM_SETSEL(-1,-1) leaves the
186
+ # selection at 0,0 and the character lands at the FRONT (findings 8.2). The UIA `value` channel
187
+ # replaces the whole value and has no caret to place, so it reports caretIgnored rather than
188
+ # pretending the request was honoured.
189
+ [ValidateSet('idle', 'start', 'end')][string]$CaretPosition = 'idle'
160
190
  )
161
191
  $started = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
162
192
  $h = [IntPtr][int64]$Hwnd
@@ -189,6 +219,143 @@ function Invoke-WpmAction {
189
219
  $result.lastInputAgeMs = $st.lastInputAgeMs
190
220
  $result.paused = $st.paused
191
221
  $result.stance = $st.stance
222
+ # The DPI stance travels with status because the read paths (snapshot rects, screenshot pixels)
223
+ # are only in PHYSICAL pixels while this process is per-monitor DPI aware. A future deletion of the
224
+ # setter would otherwise turn the whole coordinate story half-scale with nothing to notice.
225
+ $result.dpiAware = [bool]$st.dpiAware
226
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
227
+ return $result
228
+ }
229
+
230
+ # ---- 0.5.0 READ PATHS: snapshot / screenshot / preview --------------------------------------
231
+ # A READ IS NOT AN ACTION. None of these three writes an input-queue entry, takes the focus, or
232
+ # produces a proof window -- there is no action to prove. They sit before the hwnd check because a
233
+ # full-screen capture has no target window at all; each branch performs its own liveness check, so a
234
+ # dead handle is still `no_hwnd` and a paused engine still says `paused` rather than serving reads.
235
+ if ($Action -eq 'snapshot' -or $Action -eq 'screenshot' -or $Action -eq 'preview') {
236
+ if ([WpmCtl]::Paused) {
237
+ $result.refusal = 'paused'; $result.reason = $script:WpmRefusals.paused
238
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
239
+ return $result
240
+ }
241
+ $needsWindow = -not ($Action -eq 'screenshot' -and $FullScreen)
242
+ if ($needsWindow -and (-not $h -or -not [WpmWin]::IsWindow($h))) {
243
+ $result.refusal = 'no_hwnd'; $result.reason = 'the hwnd does not identify a live window'
244
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
245
+ return $result
246
+ }
247
+ $result.channel = 'none'; $result.tier = 'none'
248
+ if ($Action -eq 'snapshot') {
249
+ try {
250
+ $snap = Get-WpmSnapshotTree -Hwnd $h -MaxElements $MaxElements -InteractableOnly $InteractableOnly `
251
+ -Intent $Intent -SinceSnapId $SinceSnapId
252
+ } catch {
253
+ $result.refusal = 'uia_probe_failed'
254
+ $result.reason = 'the accessibility probe threw while walking the tree: ' + $_.Exception.Message
255
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
256
+ return $result
257
+ }
258
+ $snapFrame = if ($snap.ContainsKey('frame')) { [string]$snap.frame } else { '' }
259
+ $snapCheck = if ($snap.ContainsKey('frameCheck')) { [string]$snap.frameCheck } else { '' }
260
+ if (-not $snap.ok) {
261
+ # THREE FAILURE WORLDS, three codes: a superseded id is stale_ref (re-read the frame); a frame
262
+ # self-check failure is a refusal to point at all (unreachable -- the coordinates cannot be
263
+ # trusted); anything else means the walk itself failed to measure (uia_probe_failed).
264
+ if ($snap.stale) { $result.refusal = 'stale_ref' }
265
+ elseif ($snapCheck -eq 'fail') { $result.refusal = 'unreachable' }
266
+ else { $result.refusal = 'uia_probe_failed' }
267
+ $result.reason = [string]$snap.reason
268
+ $result.frame = $snapFrame
269
+ $result.frameCheck = $snapCheck
270
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
271
+ return $result
272
+ }
273
+ $result.ok = $true
274
+ $result.detail = 'snapshot: read only, no action taken'
275
+ $result.snapId = [string]$snap.snapId
276
+ $result.frame = $snapFrame
277
+ $result.frameCheck = $snapCheck
278
+ $result.frameInset = $snap.frameInset
279
+ $result.outsideFrame = [int]$snap.outsideFrame
280
+ $result.intent = [string]$snap.intent
281
+ $result.total = [int]$snap.total
282
+ $result.count = [int]$snap.count
283
+ $result.truncated = [bool]$snap.truncated
284
+ $result.elements = @($snap.elements)
285
+ $result.delta = $snap.delta
286
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
287
+ return $result
288
+ }
289
+ if ($Action -eq 'screenshot') {
290
+ try {
291
+ $shot = Get-WpmScreenshot -Hwnd $h -FullScreen $FullScreen -MaxWidth $MaxWidth -Keep $ShotsKeep
292
+ } catch {
293
+ $result.refusal = 'action_failed'
294
+ $result.reason = 'the capture threw: ' + $_.Exception.Message
295
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
296
+ return $result
297
+ }
298
+ if (-not $shot.ok) {
299
+ $result.refusal = 'action_failed'; $result.reason = [string]$shot.reason
300
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
301
+ return $result
302
+ }
303
+ $result.ok = $true
304
+ $result.detail = 'screenshot: read only, no action taken'
305
+ $result.shotId = [string]$shot.shotId
306
+ $result.file = [string]$shot.file
307
+ $result.screenRect = $shot.screenRect
308
+ $result.imageSize = $shot.imageSize
309
+ $result.scale = [double]$shot.scale
310
+ $result.identity = [bool]$shot.identity
311
+ $result.frame = [string]$shot.frame
312
+ # WHICH CAPTURE PATH PRODUCED THE PICTURE. Reported on every screenshot, not only when the cheap
313
+ # path failed: a field that appears only in the exceptional case is how a silent substitution looks.
314
+ $result.captureBackend = [string]$shot.captureBackend
315
+ if ($shot.captureNote) { $result.captureNote = [string]$shot.captureNote }
316
+ $result.dpiAware = [bool]$shot.dpiAware
317
+ $result.bytes = [long]$shot.bytes
318
+ $result.shotsKeep = [int]$shot.shotsKeep
319
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
320
+ return $result
321
+ }
322
+ # ---- preview: draw the point, report the element under it as TEXT -------------------------
323
+ $pv = Get-WpmPreviewAt -Hwnd $h -X $X -Y $Y
324
+ if (-not $pv.ok) {
325
+ if ($pv.outside) {
326
+ $result.refusal = 'unreachable'
327
+ $result.reason = 'the preview point does not belong to the target window: ' + [string]$pv.reason
328
+ } else {
329
+ # NOT `unreachable`: the point may be fine. The probe itself failed, so no conclusion about
330
+ # what is under it is licensed -- the same distinction uia_probe_failed already draws.
331
+ $result.refusal = 'uia_probe_failed'
332
+ $result.reason = [string]$pv.reason
333
+ }
334
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
335
+ return $result
336
+ }
337
+ # THE MARKER IS A REQUEST, NOT AN ACTION (overlay.ps1 Set-WpmOverlayMarker writes three fields and
338
+ # returns). A missing overlay downgrades to text only -- it must never fail the preview, because
339
+ # "what is under this point" is answerable without a picture.
340
+ $marker = @{ ok = $false; reason = 'overlay not loaded' }
341
+ try {
342
+ . (Join-Path $PSScriptRoot 'overlay.ps1')
343
+ $marker = Set-WpmOverlayMarker -X $X -Y $Y
344
+ } catch {
345
+ $marker = @{ ok = $false; reason = 'overlay unavailable: ' + $_.Exception.Message }
346
+ }
347
+ $result.ok = $true
348
+ $result.detail = 'preview: would click ' + $X + ',' + $Y
349
+ $result.previewX = [int]$X
350
+ $result.previewY = [int]$Y
351
+ $result.previewAtPoint = [bool]$pv.atPoint
352
+ $result.previewName = [string]$pv.name
353
+ $result.previewControlType = [string]$pv.controlType
354
+ $result.previewAutomationId = [string]$pv.automationId
355
+ $result.previewRect = $pv.rect
356
+ $result.previewReason = [string]$pv.reason
357
+ $result.markerPlaced = [bool]$marker.ok
358
+ $result.markerReason = if ($marker.ContainsKey('reason')) { [string]$marker.reason } else { '' }
192
359
  $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
193
360
  return $result
194
361
  }
@@ -198,11 +365,79 @@ function Invoke-WpmAction {
198
365
  return $result
199
366
  }
200
367
 
368
+ # ---- 0.6.0: a DISABLED target is a fact about the target, and Windows discards input to it -----
369
+ # Measured (findings 8.1): with EnableWindow(false) every message this engine sends still RETURNS --
370
+ # WM_SETTEXT and WM_CHAR both answer as if they worked, and the control simply does not take the click.
371
+ # This is deliberately not a new refusal code: 'disabled' is a state of the target, not an error in the
372
+ # call, and the caller may well want to know that the window is there but greyed out. What it does cost
373
+ # is the right to claim an effect: `effect` is pinned to 'unverified' for a disabled target, so a
374
+ # green-looking result cannot be produced by a window that was throwing our input away.
375
+ $targetDisabled = -not [WpmWin]::IsWindowEnabled($h)
376
+
377
+ # ---- 0.5.0: image coordinates are converted by the ENGINE, before anything is probed ---------
378
+ # Why here: a bogus shotId must cost zero target probing, and the converted point then flows through
379
+ # the EXISTING click path unchanged -- including the point-ownership assertion and the proof window.
380
+ # Nothing about the safety contract is relaxed by accepting image coordinates.
381
+ if ($Action -eq 'click' -and $Space -eq 'image') {
382
+ $conv = Convert-WpmImagePoint -ShotId $ShotId -X $X -Y $Y
383
+ if (-not $conv.ok) {
384
+ $result.refusal = if ($conv.stale) { 'stale_ref' } else { 'unreachable' }
385
+ $result.reason = [string]$conv.reason
386
+ $result.coordSpace = 'image'
387
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
388
+ return $result
389
+ }
390
+ $result.coordSpace = 'image'
391
+ $result.shotId = [string]$ShotId
392
+ $result.scale = [double]$conv.scale
393
+ $result.imagePoint = "$X,$Y"
394
+ $result.screenPoint = "$($conv.x),$($conv.y)"
395
+ $X = [int]$conv.x
396
+ $Y = [int]$conv.y
397
+ }
398
+
201
399
  # Coordinates travel into the probe: the UIA tier resolves the element AT THE POINT. Without them
202
400
  # an unnamed click had no way to be anything other than "first control in tree order".
203
401
  $probe = Get-WpmFacts -Hwnd $h -Action $Action -ElementName $ElementName -X $X -Y $Y
204
402
  $facts = $probe.facts
205
403
  $facts.paused = [bool][WpmCtl]::Paused
404
+ # ---- 0.5.0: a LABEL addresses the element itself -------------------------------------------
405
+ # The label is resolved back to the UIA element by RuntimeId (measured stable across queries, 71/71),
406
+ # so the action keeps the silent pattern path and never degrades into a coordinate click. Two things
407
+ # are then enforced, and they are deliberately redundant:
408
+ # * the label must still resolve, otherwise this is a reference to a frame that is gone (stale_ref);
409
+ # * for a click, the caller's point must fall inside the labelled element -- "the point is the
410
+ # contract" is not weakened by adding a second way to name the target.
411
+ if ($Label -gt 0) {
412
+ $res = Resolve-WpmSnapshotLabel -Hwnd $h -Label $Label -SnapId $SnapId
413
+ if (-not $res.ok) {
414
+ $result.refusal = 'stale_ref'; $result.reason = [string]$res.reason
415
+ $result.label = $Label
416
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
417
+ return $result
418
+ }
419
+ if ($Action -eq 'click' -and $X -ge 0 -and $Y -ge 0) {
420
+ $lr = $res.row.rect
421
+ $eps = 2
422
+ if ($X -lt ([int]$lr.left - $eps) -or $X -gt ([int]$lr.right + $eps) -or
423
+ $Y -lt ([int]$lr.top - $eps) -or $Y -gt ([int]$lr.bottom + $eps)) {
424
+ $result.refusal = 'unreachable'
425
+ $result.reason = "label $Label is at rect=$($lr.left),$($lr.top),$($lr.right),$($lr.bottom) but the click point is $X,$Y -- the label and the point must agree before either is used"
426
+ $result.label = $Label
427
+ $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
428
+ return $result
429
+ }
430
+ }
431
+ $probe.uiaElement = $res.element
432
+ $probe.uiaReason = ''
433
+ $facts.hasPattern = $true
434
+ $facts.patternKind = [string]$res.patternKind
435
+ $facts.uiaTreeEmpty = $false
436
+ $facts.uiaProbeFailed = $false
437
+ $facts.messageDoor = $false # a window message cannot address "label 3"
438
+ $result.label = $Label
439
+ $result.snapId = [string]$res.snapId
440
+ }
206
441
  if ($ElementName) {
207
442
  # A named control cannot be addressed through a window message: the message tiers take
208
443
  # coordinates or a child HWND, and "the Save button" is neither. Suppressing the door here is
@@ -323,6 +558,13 @@ function Invoke-WpmAction {
323
558
  # NOT already doing it. Kept short (80ms): a few samples, no perceptible slowdown.
324
559
  $controlSamples = Get-WpmProofWindow -Ms 80
325
560
  $tAct = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
561
+ # 0.6.0 locals. Declared here rather than inside the dispatch branches so that a channel which never
562
+ # armed a readback (invoke/toggle/select, a refusal, an exception) is the ABSENCE of a plan and not a
563
+ # leftover value from the previous call -- this worker is long-lived, and a stale plan would grade
564
+ # today's action against yesterday's field.
565
+ $focusBeforeDoor = $null
566
+ $focusAfterDoor = $null
567
+ $readbackPlan = $null
326
568
 
327
569
  # ---- the takeover choreography (spec.md §3) -------------------------------------------------
328
570
  # Everything from here to the finally exists because R3 promises three things the quiet tier never
@@ -360,6 +602,11 @@ function Invoke-WpmAction {
360
602
  try {
361
603
  # THE CURSOR MOVE LIVES INSIDE THE SAME try THAT OWNS THE RESTORE (spec.md §6 G4): an exception in
362
604
  # the door call below still reaches the finally that puts the pointer back.
605
+ #
606
+ # 0.6.0: the focus table is re-read HERE, at the moment of sending, not reused from the one captured
607
+ # before the probe. See the focusMoved block after the settle window for why this is a warning and
608
+ # never a refusal.
609
+ $focusBeforeDoor = Get-WpmFocusEntry
363
610
  if ($takeover -and -not $abortedBeforeAct -and $Action -eq 'click' -and $X -ge 0 -and $Y -ge 0) {
364
611
  $cursorEntry = Set-WpmCursorTakeover -X $X -Y $Y
365
612
  $result.cursorMoved = if ($cursorEntry.applied) { 'true' } else { 'false' }
@@ -386,8 +633,26 @@ function Invoke-WpmAction {
386
633
  $result.latencyMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $started
387
634
  return $result
388
635
  }
389
- $r = [WpmWin]::MessageText($target, $Text, [bool]$Replace)
390
- $result.detail = "msg=$r child=$($target.ToInt64())"
636
+ if ([WpmWin]::IsWindowEnabled($target) -eq $false) { $targetDisabled = $true }
637
+ # THE PRE-READ. Taken BEFORE the write, because the expected post-write value is only computable
638
+ # from the value the control held plus the insertion point the CONTROL reports (its own EM_GETSEL
639
+ # answer) -- not from an assumption that a write appends. Measured: an unfocused WinForms TextBox
640
+ # inserts at 0, so 'abc' + 'XYZ' is 'XYZabc'; the model has to know both numbers or the
641
+ # comparison grades its own guess.
642
+ $preRead = Read-WpmControlText -Hwnd $target
643
+ $w = [WpmWin]::MessageTextAt($target, $Text, [bool]$Replace, $CaretPosition)
644
+ $mode = [string]$w[0]
645
+ $sentChars = [int]$w[1]
646
+ $selStart = [int]$w[2]; $selEnd = [int]$w[3]
647
+ $caretAskedAt = if ($w.Count -gt 4) { [int]$w[4] } else { -1 }
648
+ # Readback runs AFTER the settle window (the proof sampling is the settle), so an app that commits
649
+ # its value asynchronously is not judged against a half-written buffer (plan R2).
650
+ $readbackPlan = @{
651
+ kind = 'message'; hwnd = $target; mode = $mode
652
+ pre = $preRead; selStart = $selStart; selEnd = $selEnd; text = $Text; replace = [bool]$Replace
653
+ }
654
+ $caretNote = "caret=$CaretPosition asked=$(if ($caretAskedAt -ge 0) { $caretAskedAt } else { '(untouched)' }) applied=$selStart,$selEnd"
655
+ $result.detail = "msg=$mode|$sentChars child=$($target.ToInt64()) $caretNote" + $(if (-not $preRead.exposesText) { ' readbackPre=unavailable' } else { '' })
391
656
  $result.effect = 'reported-by-send'
392
657
  } elseif ($Action -eq 'click') {
393
658
  if ($X -lt 0 -or $Y -lt 0) {
@@ -461,17 +726,43 @@ function Invoke-WpmAction {
461
726
  $pat.Invoke()
462
727
  } elseif ($kind -eq 'value') {
463
728
  $pat = $el.GetCurrentPattern($pats.value)
729
+ # THE PRE-READ THROUGH THE SAME PATTERN that writes. The UIA `value` channel is a whole-value
730
+ # replace, so the expected after-value is computable exactly -- and the current value is read
731
+ # from the pattern, not from a window message, because this is the channel the write went through.
732
+ $preVal = ''
733
+ $preOk = $false
734
+ try { $preVal = [string]$pat.Current.Value; $preOk = $true } catch { $preOk = $false }
464
735
  $pat.SetValue($(if ($Replace) { $Text } else { [string]$pat.Current.Value + $Text }))
736
+ $readbackPlan = @{
737
+ kind = 'uia-value'; pat = $pat; mode = 'value'
738
+ expected = $(if ($Replace) { $Text } else { $preVal + $Text })
739
+ preOk = $preOk; pre = $preVal; text = $Text; replace = [bool]$Replace
740
+ }
465
741
  } elseif ($kind -eq 'toggle') {
466
742
  $pat = $el.GetCurrentPattern($pats.toggle); $pat.Toggle()
467
743
  } elseif ($kind -eq 'select') {
468
744
  $pat = $el.GetCurrentPattern($pats.select); $pat.Select()
469
745
  } elseif ($kind -eq 'legacy') {
470
- $pat = $el.GetCurrentPattern($pats.legacy); $pat.SetValue($Text)
746
+ $pat = $el.GetCurrentPattern($pats.legacy)
747
+ $preVal = ''; $preOk = $false
748
+ try { $preVal = [string]$pat.Current.Value; $preOk = $true } catch { $preOk = $false }
749
+ $pat.SetValue($Text)
750
+ $readbackPlan = @{
751
+ kind = 'uia-legacy'; pat = $pat; mode = 'legacy'
752
+ expected = $Text; preOk = $preOk; pre = $preVal; text = $Text; replace = $true
753
+ }
471
754
  }
472
755
  $result.detail = "uia=$kind"
756
+ # A caret request on this channel has no meaning -- `value` replaces the whole string, there is no
757
+ # insertion point to move. Saying so is the difference between honouring a parameter and quietly
758
+ # dropping it (the same discipline as `frame` and, in 0.6.0-B, `captureBackend`: a path that did
759
+ # not do what you asked says so in the result).
760
+ if ($CaretPosition -ne 'idle' -and $readbackPlan) {
761
+ $result.detail += " caretIgnored=$kind-pattern"
762
+ }
473
763
  $result.effect = 'reported-by-pattern'
474
764
  }
765
+ $focusAfterDoor = Get-WpmFocusEntry
475
766
  } catch {
476
767
  $result.refusal = 'action_failed'
477
768
  $result.reason = 'the channel call threw: ' + $_.Exception.Message
@@ -516,6 +807,84 @@ function Invoke-WpmAction {
516
807
  $result.proofControl = $controlSamples
517
808
  $result.proofSamples = Get-WpmProofWindow -Ms $SettleMs
518
809
 
810
+ # ---- 0.6.0 READBACK: the fourth `effect`, earned by reading the value back -------------------
811
+ # This runs AFTER the settle window on purpose (plan R2): an application that commits its text
812
+ # asynchronously must not be judged against a half-written buffer. The rule is narrow and it is the
813
+ # whole feature -- `readback-verified` is used ONLY when the value read back equals the value the
814
+ # model predicted from measurements the CONTROL reported. Anything else keeps the effect the write
815
+ # itself earned, and the reason travels in `detail`.
816
+ #
817
+ # A mismatch is NOT a failure. The app may legitimately reformat what it was given (case folding,
818
+ # trimming, a mask, a numeric parse), and `unreachable`/`action_failed` must not be repurposed to
819
+ # mean "the value is not what I expected" -- so the refusal set is unchanged by this block, by design.
820
+ if ($readbackPlan) {
821
+ $expected = $null
822
+ $expectedHow = ''
823
+ if ($readbackPlan.kind -eq 'message') {
824
+ $post = Read-WpmControlText -Hwnd $readbackPlan.hwnd
825
+ $pre = $readbackPlan.pre
826
+ if ($readbackPlan.mode -eq 'settext') {
827
+ $expected = [string]$readbackPlan.text; $expectedHow = 'settext-equals-text'
828
+ } elseif (-not $pre.exposesText -or $pre.truncated) {
829
+ $expected = $null; $expectedHow = $(if (-not $pre.exposesText) { 'pre-read-unavailable' } else { 'pre-read-truncated' })
830
+ } else {
831
+ $b = [string]$pre.text
832
+ $s = [int]$readbackPlan.selStart; $e = [int]$readbackPlan.selEnd
833
+ if ($s -lt 0) { $s = 0 }
834
+ if ($e -lt $s) { $e = $s }
835
+ if ($s -gt $b.Length) { $s = $b.Length }
836
+ if ($e -gt $b.Length) { $e = $b.Length }
837
+ $expected = $b.Substring(0, $s) + [string]$readbackPlan.text + $b.Substring($e)
838
+ $expectedHow = "insert-at-$s-$e"
839
+ }
840
+ $postRef = $post
841
+ } else {
842
+ $postVal = ''; $postOk = $false
843
+ try { $postVal = [string]$readbackPlan.pat.Current.Value; $postOk = $true } catch { $postOk = $false }
844
+ $expected = [string]$readbackPlan.expected; $expectedHow = 'pattern-current-value'
845
+ $postRef = @{ ok = $postOk; exposesText = $postOk; truncated = $false; text = $postVal; chars = $postVal.Length }
846
+ }
847
+
848
+ if (-not $postRef.exposesText) {
849
+ # Not "empty": the read produced nothing usable. Reporting readback=unavailable keeps the two
850
+ # apart, which is the difference between "this field hides its text" and "this field is empty".
851
+ $result.detail += ' readback=unavailable'
852
+ } elseif ($null -eq $expected) {
853
+ $result.detail += " readback=unmodelled($expectedHow)"
854
+ } else {
855
+ $shown = [string]$postRef.text
856
+ if ($shown.Length -gt 120) { $shown = $shown.Substring(0, 119) + [char]0x2026 }
857
+ $result.detail += " readback=$shown readbackLen=$([string]$postRef.text.Length)"
858
+ if ($postRef.text -eq $expected) {
859
+ $result.effect = 'readback-verified'
860
+ } else {
861
+ # The mismatch is carried as data because a caller that only reads `effect` must still be able
862
+ # to tell "nothing reported success" from "it reported success and the value says otherwise".
863
+ $result.detail += ' readbackMismatch=true'
864
+ }
865
+ }
866
+ }
867
+
868
+ # ---- 0.6.0 focus re-check at the moment of sending ------------------------------------------
869
+ # A WARNING, never a refusal. The desktop can move between the probe and the send, and on the
870
+ # bounded-flash tier the move is the channel working as documented. Refusing on a mismatch would
871
+ # kill correct writes to be safe about a fact the readback already judges better -- so the field
872
+ # says what was measured and lets the caller weigh it.
873
+ if ($focusBeforeDoor -and $focusAfterDoor) {
874
+ $moved = ($focusBeforeDoor.fgHwnd -ne $focusAfterDoor.fgHwnd) -or
875
+ ($focusBeforeDoor.focusHwnd -ne $focusAfterDoor.focusHwnd)
876
+ $result.detail += " focusMoved=$($moved.ToString().ToLower())"
877
+ }
878
+
879
+ # ---- 0.6.0 the disabled target, recorded last so it cannot be overridden ---------------------
880
+ # `effect` is pinned to 'unverified' here even if a readback matched: a write into a greyed-out
881
+ # window is not an effect the owner can rely on, and the one claim this field makes is that the
882
+ # channel's own report is not being dressed up as a verified state change.
883
+ if ($targetDisabled) {
884
+ $result.detail += ' disabled=true'
885
+ $result.effect = 'unverified'
886
+ }
887
+
519
888
  # Did the action actually take the owner's focus? Measured, not assumed -- the quiet tier is
520
889
  # expected to say no and the flash tier yes, and a run where both say the same thing is a broken
521
890
  # instrument rather than a good result.