dsh-windows-parallel-mcp 0.0.1 → 0.1.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.
@@ -0,0 +1,156 @@
1
+ # worker.ps1 -- the sidecar's line protocol: one JSON request in, one JSON response out.
2
+ #
3
+ # WHY A WORKER AND NOT A DIRECT CALL. Two constraints collide in this process model:
4
+ # * UIA COM wants a single-threaded apartment (powershell.exe -STA), and
5
+ # * the caller is a Node event loop that cannot block on a synchronous P/Invoke round trip
6
+ # without stalling the host's own tool loop.
7
+ # A long-lived STA process speaking one-line-request/one-line-response satisfies both. The same
8
+ # shape was measured to work in the sibling competitor (Node registers tools, PS sidecar speaks JSON
9
+ # lines) and TS-1 in findings.md chose it for that reason -- not for elegance.
10
+ #
11
+ # THE PROTOCOL IS THE PRODUCT'S UNDERPINNING, so it is deliberately boring and total:
12
+ # request {"id":"<string>","action":"windows|click|type|focus|status", ...args}
13
+ # response {"id":"<string>","ok":true,"result":{...}}
14
+ # {"id":"<string>","ok":false,"error":"<message>","code":"<refusal-or-empty>"}
15
+ #
16
+ # THREE RULES THAT COST A RUN EACH IF BROKEN:
17
+ # 1. NOTHING ELSE MAY TOUCH STDOUT. Add-Type, assembly loads and any stray Write-Output would
18
+ # interleave with the protocol lines and the reader would parse rubbish. Every call whose
19
+ # output is not the response is assigned to $null, and the response is written through
20
+ # [Console]::Out directly so a redirected pipeline cannot swallow or pad it.
21
+ # 2. A BAD REQUEST MUST STILL GET AN ANSWER. A null id is a bug on the caller's side; answering
22
+ # with an error keeps the caller's request map from leaking a permanently-pending entry.
23
+ # 3. STDOUT/STDIN ARE UTF-8. This host's ANSI codepage is GBK, and window titles plus typed text
24
+ # are routinely non-ASCII: without this, Chinese text arrives as mojibake and every title match
25
+ # silently fails -- the same class as the BOM-less .ps1 incident recorded in check-ps-bom.mjs.
26
+ #
27
+ # Exit is driven by stdin closing: the parent owns the lifetime, so the worker never decides to stop
28
+ # on its own (an idle-exit would race the next request).
29
+ $ErrorActionPreference = 'Stop'
30
+ Set-StrictMode -Version 2.0
31
+
32
+ try { [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 } catch { }
33
+ try { [Console]::InputEncoding = [System.Text.Encoding]::UTF8 } catch { }
34
+ $ProgressPreference = 'SilentlyContinue'
35
+
36
+ $EngineRoot = $PSScriptRoot
37
+ . (Join-Path $EngineRoot 'engine.ps1')
38
+
39
+ function Write-Response([string]$Id, $Payload) {
40
+ # Two-step because ConvertTo-Json is the only reliable serializer here, and because a response
41
+ # that fails to serialize must still produce a protocol line rather than a silent stall.
42
+ try {
43
+ $line = $Payload | ConvertTo-Json -Depth 8 -Compress
44
+ } catch {
45
+ $line = '{"id":' + (ConvertTo-Json $Id -Compress) + ',"ok":false,"error":"response serialization failed: ' + ($_.Exception.Message -replace '"', "'") + '","code":""}'
46
+ }
47
+ [Console]::Out.WriteLine($line)
48
+ [Console]::Out.Flush()
49
+ }
50
+
51
+ function Get-Arg($Bag, [string]$Name, $Default = $null) {
52
+ if ($null -eq $Bag) { return $Default }
53
+ if (-not ($Bag.PSObject.Properties.Name -contains $Name)) { return $Default }
54
+ $v = $Bag.$Name
55
+ if ($null -eq $v) { return $Default }
56
+ return $v
57
+ }
58
+
59
+ # Announced once, on stderr, so the Node half can tell "the engine loaded" from "the process is up
60
+ # but the engine is not". It is a diagnostic only: no protocol line is written before a request.
61
+ [Console]::Error.WriteLine('wpm-worker ready engine=' + $EngineRoot)
62
+
63
+ while ($true) {
64
+ $line = [Console]::In.ReadLine()
65
+ if ($null -eq $line) { break } # parent closed stdin: orderly shutdown
66
+ if ($line.Trim() -eq '') { continue }
67
+
68
+ $req = $null
69
+ try { $req = $line | ConvertFrom-Json } catch {
70
+ Write-Response '' @{ id = ''; ok = $false; error = 'request is not valid JSON: ' + $_.Exception.Message; code = '' }
71
+ continue
72
+ }
73
+
74
+ $id = [string](Get-Arg $req 'id' '')
75
+ $action = [string](Get-Arg $req 'action' '')
76
+
77
+ try {
78
+ switch ($action) {
79
+ 'windows' {
80
+ $list = @(Get-WpmWindowList)
81
+ Write-Response $id @{ id = $id; ok = $true; result = @{ windows = $list; count = $list.Count } }
82
+ }
83
+ 'status' {
84
+ $st = Get-WpmStatus
85
+ Write-Response $id @{ id = $id; ok = $true; result = $st }
86
+ }
87
+ 'overlay' {
88
+ # Product surface (task_54): the marker + Ctrl+Alt+Q. `on` starts/stops the overlay thread;
89
+ # `x`/`y` move the marker to the point the engine just acted on (the Node half calls this
90
+ # after a successful action; the marker auto-hides).
91
+ . (Join-Path $EngineRoot 'overlay.ps1')
92
+ $on = [bool](Get-Arg $req 'on' $true)
93
+ if ($on) {
94
+ $r = Start-WpmOverlay
95
+ $st = Get-WpmOverlayState -WaitMs 2500
96
+ } else {
97
+ $r = Stop-WpmOverlay
98
+ $st = $r.state
99
+ }
100
+ $mx = [int](Get-Arg $req 'x' -1)
101
+ $my = [int](Get-Arg $req 'y' -1)
102
+ if ($on -and $mx -ge 0 -and $my -ge 0) { [void](Set-WpmOverlayMarker -X $mx -Y $my) }
103
+ Write-Response $id @{ id = $id; ok = $true; result = @{ start = $r; state = $st } }
104
+ }
105
+ 'marker' {
106
+ . (Join-Path $EngineRoot 'overlay.ps1')
107
+ $mx = [int](Get-Arg $req 'x' -1)
108
+ $my = [int](Get-Arg $req 'y' -1)
109
+ $hide = [bool](Get-Arg $req 'hide' $false)
110
+ $r = Set-WpmOverlayMarker -X $mx -Y $my -Hide:$hide
111
+ Write-Response $id @{ id = $id; ok = $true; result = $r }
112
+ }
113
+ 'pause' {
114
+ # Owner control. Kept in the worker rather than in Node so the pause flag lives in exactly
115
+ # one process: two copies would let a model bypass the pause by reaching whichever copy
116
+ # forgot it.
117
+ $on = [bool](Get-Arg $req 'on' $true)
118
+ Set-WpmPaused $on 'owner'
119
+ Write-Response $id @{ id = $id; ok = $true; result = @{ paused = $on } }
120
+ }
121
+ 'click' {
122
+ $r = Invoke-WpmAction -Hwnd ([string](Get-Arg $req 'hwnd' '')) -Action click `
123
+ -X ([int](Get-Arg $req 'x' -1)) -Y ([int](Get-Arg $req 'y' -1)) `
124
+ -SettleMs ([int](Get-Arg $req 'settleMs' 350))
125
+ Write-Response $id @{ id = $id; ok = $true; result = $r }
126
+ }
127
+ 'type' {
128
+ $r = Invoke-WpmAction -Hwnd ([string](Get-Arg $req 'hwnd' '')) -Action type `
129
+ -Text ([string](Get-Arg $req 'text' '')) -ElementName ([string](Get-Arg $req 'elementName' '')) `
130
+ -Replace:([bool](Get-Arg $req 'replace' $false)) -SettleMs ([int](Get-Arg $req 'settleMs' 350))
131
+ Write-Response $id @{ id = $id; ok = $true; result = $r }
132
+ }
133
+ 'focus' {
134
+ $r = Invoke-WpmAction -Hwnd ([string](Get-Arg $req 'hwnd' '')) -Action focus `
135
+ -ElementName ([string](Get-Arg $req 'elementName' '')) -SettleMs ([int](Get-Arg $req 'settleMs' 350))
136
+ Write-Response $id @{ id = $id; ok = $true; result = $r }
137
+ }
138
+ 'ping' {
139
+ # `text` is echoed back verbatim. That is not decoration: the protocol's encoding is a real
140
+ # failure surface on this host (ANSI codepage GBK), and an echo is the only way to prove a
141
+ # non-ASCII string survives Node -> stdin -> PowerShell -> stdout -> Node unchanged. The tool
142
+ # surface deliberately does not expose this action.
143
+ $echo = [string](Get-Arg $req 'text' '')
144
+ Write-Response $id @{ id = $id; ok = $true; result = @{ pong = $true; pid = $PID; text = $echo; len = $echo.Length } }
145
+ }
146
+ default {
147
+ Write-Response $id @{ id = $id; ok = $false; error = "unknown action '$action'"; code = '' }
148
+ }
149
+ }
150
+ } catch {
151
+ # The action threw. The engine's own refusals are returned as ok=$true results carrying
152
+ # `refusal`; reaching here means something outside the decision table broke, so it is reported
153
+ # as an error with the exception text rather than folded into a refusal code it does not own.
154
+ Write-Response $id @{ id = $id; ok = $false; error = $_.Exception.Message; code = '' }
155
+ }
156
+ }
@@ -0,0 +1,205 @@
1
+ {
2
+ "schemaVersion": "0.1.0",
3
+ "status": "form decided 2026-11-19: A = DSH native plugin (no MCP). The five named tools below stay the canonical ACTION surface; the host projection (see hostProjection) exposes them as one tool with an action discriminator, which is this host's convention for a multi-action capability.",
4
+ "hostProjection": {
5
+ "form": "A",
6
+ "decided": "2026-11-19",
7
+ "why": "root cause: five resident tool schemas describing one engine is a standing per-turn token cost with no added capability; the host's own shell/terminal tools are one-tool-with-action. The action surface here is unchanged -- same five actions, same arguments, same refusal codes.",
8
+ "package": "dsh-windows-parallel-mcp",
9
+ "tool": "wpm",
10
+ "discriminator": "action",
11
+ "source": "lib/host-tools.js#PARAMETERS",
12
+ "actions": {
13
+ "wpm_windows": "windows",
14
+ "wpm_click": "click",
15
+ "wpm_type": "type",
16
+ "wpm_focus": "focus",
17
+ "wpm_status": "status"
18
+ },
19
+ "extraWorkerActions": [
20
+ { "action": "ping", "exposed": false, "why": "protocol/encoding self-test; not part of the tool surface" },
21
+ { "action": "pause", "exposed": false, "why": "owner control, behind the settings pill rather than the model's reach" }
22
+ ]
23
+ },
24
+ "notes": [
25
+ "The five tool names are fixed here so both delivery forms expose the same action surface.",
26
+ "src/engine/engine.ps1 is the only implementation; this file is the contract, not a second implementation.",
27
+ "docs/refusals.md is the human-readable form of `refusals`; scripts/check-refusals.mjs keeps them equal.",
28
+ "The `refusalCases` array is executable: test/refusals-test.ps1 iterates it and binds the placeholders, so a documented refusal path cannot rot into fiction."
29
+ ],
30
+ "stance": {
31
+ "inputQueue": "no OS input-queue writes: no SendInput, no keybd_event, no mouse_event",
32
+ "focus": "no forged Alt key, no synthetic activation code",
33
+ "cursor": "the cursor is never moved: 0 px across 24 measured rounds (scripts/channel-raise-matrix.ps1)",
34
+ "tiers": "quiet (window messages) preferred over bounded-flash (UIA patterns); the flash tier must reverse itself once, and never pulls focus from a window the owner chose"
35
+ },
36
+ "refusals": [
37
+ { "code": "no_hwnd", "layer": "decision", "summary": "no usable target window handle" },
38
+ { "code": "paused", "layer": "decision", "summary": "the owner paused input; server-side refusal" },
39
+ { "code": "elevated_target", "layer": "decision", "summary": "target runs at a higher integrity level" },
40
+ { "code": "unreachable", "layer": "decision", "summary": "no message door and no usable UIA pattern" },
41
+ { "code": "terminal_no_door", "layer": "decision", "summary": "terminal text surface exposes neither a child window nor a document element" },
42
+ { "code": "needs_facts", "layer": "decision", "summary": "the caller supplied no measured facts for this target" },
43
+ { "code": "action_failed", "layer": "runtime", "summary": "the chosen channel threw while acting", "uncoveredReason": "needs a target whose channel reliably throws; not constructed this round (docs/refusals.md, gap 2)" },
44
+ { "code": "restore_failed", "layer": "runtime", "summary": "the OS refused to return the foreground after a flash", "uncoveredReason": "needs a real elevated window; task_52 (docs/refusals.md, gap 2)" }
45
+ ],
46
+ "tools": [
47
+ {
48
+ "name": "wpm_windows",
49
+ "action": "windows",
50
+ "engine": "Get-WpmWindowList",
51
+ "description": "List live top-level windows the engine could drive: hwnd, title, window class, owning pid and screen rect. Read-only. Visible, titled, non-minimized windows only, because a list padded with invisible helper windows makes target selection guesswork.",
52
+ "inputSchema": {
53
+ "type": "object",
54
+ "properties": {},
55
+ "required": [],
56
+ "additionalProperties": false
57
+ },
58
+ "refusals": [],
59
+ "noRefusalReason": "pure enumeration: no target is acted on, so there is nothing to refuse. An empty list means no window matched the filter, and that is reported as an empty list, not as a failure."
60
+ },
61
+ {
62
+ "name": "wpm_click",
63
+ "action": "click",
64
+ "engine": "Invoke-WpmAction -Action click",
65
+ "description": "Click at a screen coordinate inside the named window. Chooses the cheapest available door: a classic child window receives the click as a window message (quiet tier, measured 0/6 foreground changes), otherwise a UIA pattern is used and the foreground is reversed once afterwards (bounded-flash tier, measured net return 15..95 ms). The cursor does not move in either tier.",
66
+ "inputSchema": {
67
+ "type": "object",
68
+ "properties": {
69
+ "hwnd": { "type": "string", "description": "Target top-level window handle, as returned by wpm_windows." },
70
+ "x": { "type": "integer", "description": "Screen X coordinate. Must resolve to a child window of the target, otherwise the action is refused instead of being sent somewhere else." },
71
+ "y": { "type": "integer", "description": "Screen Y coordinate." },
72
+ "settleMs": { "type": "integer", "minimum": 0, "maximum": 2000, "default": 350, "description": "How long to let the app commit its state before the effect is judged." }
73
+ },
74
+ "required": ["hwnd", "x", "y"],
75
+ "additionalProperties": false
76
+ },
77
+ "refusals": ["no_hwnd", "paused", "elevated_target", "unreachable", "terminal_no_door", "action_failed", "restore_failed"]
78
+ },
79
+ {
80
+ "name": "wpm_type",
81
+ "action": "type",
82
+ "engine": "Invoke-WpmAction -Action type",
83
+ "description": "Write text into the named window: either the first editable classic child (quiet tier) or a UIA element named by the caller (bounded-flash tier). No keystrokes are synthesized; the text is delivered through the window message or the pattern itself.",
84
+ "inputSchema": {
85
+ "type": "object",
86
+ "properties": {
87
+ "hwnd": { "type": "string", "description": "Target top-level window handle." },
88
+ "text": { "type": "string", "description": "Text to write. Empty string is refused: it cannot be distinguished from a channel that silently did nothing." },
89
+ "elementName": { "type": "string", "description": "Optional UIA Name or AutomationId of the control to write into. Naming an element forces the UIA tier, because a message tier cannot address 'the control called X'." },
90
+ "replace": { "type": "boolean", "default": false, "description": "Replace the existing value instead of appending to it." },
91
+ "settleMs": { "type": "integer", "minimum": 0, "maximum": 2000, "default": 350, "description": "How long to let the app commit its state before the effect is judged." }
92
+ },
93
+ "required": ["hwnd", "text"],
94
+ "additionalProperties": false
95
+ },
96
+ "refusals": ["no_hwnd", "paused", "elevated_target", "unreachable", "terminal_no_door", "action_failed", "restore_failed"]
97
+ },
98
+ {
99
+ "name": "wpm_focus",
100
+ "action": "focus",
101
+ "engine": "Invoke-WpmAction -Action focus",
102
+ "description": "Move keyboard focus to a named control inside the target, then reverse the foreground change once. This action always needs a UIA element: the message tier cannot set keyboard focus, so a window with no pattern is refused rather than clicked at a guessed coordinate.",
103
+ "inputSchema": {
104
+ "type": "object",
105
+ "properties": {
106
+ "hwnd": { "type": "string", "description": "Target top-level window handle." },
107
+ "elementName": { "type": "string", "description": "UIA Name or AutomationId of the control that should hold focus." },
108
+ "settleMs": { "type": "integer", "minimum": 0, "maximum": 2000, "default": 350, "description": "How long to let the app commit its state before the effect is judged." }
109
+ },
110
+ "required": ["hwnd", "elementName"],
111
+ "additionalProperties": false
112
+ },
113
+ "refusals": ["no_hwnd", "paused", "elevated_target", "unreachable", "terminal_no_door", "action_failed", "restore_failed"]
114
+ },
115
+ {
116
+ "name": "wpm_status",
117
+ "action": "status",
118
+ "engine": "Get-WpmStatus",
119
+ "description": "Report the engine's state and the desktop's focus table: paused flag, foreground window and pid, the control that holds keyboard focus, the age of the last human input, and the engine's stated stance. Read-only; useful before acting and after a refusal.",
120
+ "inputSchema": {
121
+ "type": "object",
122
+ "properties": {},
123
+ "required": [],
124
+ "additionalProperties": false
125
+ },
126
+ "refusals": [],
127
+ "noRefusalReason": "pure query: it reads the focus table and returns it. There is no target to refuse, and inventing a refusal code for this tool would create a branch that can never be exercised."
128
+ }
129
+ ],
130
+ "refusalCases": [
131
+ {
132
+ "id": "click-dead-hwnd",
133
+ "mode": "engine",
134
+ "tool": "wpm_click",
135
+ "args": { "hwnd": "$deadHwnd", "x": 100, "y": 100 },
136
+ "expect": "no_hwnd",
137
+ "note": "A handle that does not identify a live window. Measured, no window is touched."
138
+ },
139
+ {
140
+ "id": "click-while-paused",
141
+ "mode": "engine",
142
+ "tool": "wpm_click",
143
+ "args": { "hwnd": "$liveHwnd", "x": 100, "y": 100 },
144
+ "setup": "pause",
145
+ "expect": "paused",
146
+ "note": "Pause is a server-side refusal checked before any capability, so a paused engine cannot be talked into acting by a target that looks reachable. Note it is wpm_status that stays callable while paused (a query), not the action tools."
147
+ },
148
+ {
149
+ "id": "type-winforms-named-missing",
150
+ "mode": "engine",
151
+ "tool": "wpm_type",
152
+ "args": { "hwnd": "$winformsHwnd", "text": "x", "elementName": "$missingName" },
153
+ "expect": "unreachable",
154
+ "note": "WinForms has a message door but no UIA pattern. Naming an element suppresses the door (a message cannot address 'the control called X'), so the target is honestly unreachable rather than quietly clicked at a guessed child."
155
+ },
156
+ {
157
+ "id": "focus-winforms",
158
+ "mode": "engine",
159
+ "tool": "wpm_focus",
160
+ "args": { "hwnd": "$winformsHwnd", "elementName": "$missingName" },
161
+ "expect": "unreachable",
162
+ "note": "Focus requires UIA by definition; a WinForms target with no pattern must be refused, never approximated."
163
+ },
164
+ {
165
+ "id": "elevated-target",
166
+ "mode": "decision",
167
+ "tool": "wpm_click",
168
+ "facts": { "hwnd": "$liveHwnd", "elevatedTarget": true },
169
+ "expect": "elevated_target",
170
+ "note": "Decision-table instance. The flag is now wired (win.ps1 IsElevatedTarget, token integrity comparison) and measured live on Task Manager by test/integrity-probe.ps1; the engine-level case additionally runs in test/llm-action-battery.mjs whenever a high-integrity window exists at run time."
171
+ },
172
+ {
173
+ "id": "terminal-no-door",
174
+ "mode": "decision",
175
+ "tool": "wpm_type",
176
+ "facts": { "hwnd": "$liveHwnd", "family": "terminal" },
177
+ "expect": "terminal_no_door",
178
+ "note": "Decision-table instance only. A real terminal window is not launched in this run because findings.md:311-316 records a leftover-dialog accident on this host; the real evidence is the coverage census."
179
+ },
180
+ {
181
+ "id": "needs-facts",
182
+ "mode": "decision",
183
+ "tool": null,
184
+ "facts": null,
185
+ "expect": "needs_facts",
186
+ "note": "The absence of facts must refuse, not default to the quiet branch. Unreachable through the engine (facts are always measured), so this one guards the table when it is used on its own."
187
+ },
188
+ {
189
+ "id": "action-failed",
190
+ "mode": "none",
191
+ "tool": "wpm_type",
192
+ "args": { "hwnd": "$liveHwnd", "text": "x" },
193
+ "expect": "action_failed",
194
+ "note": "NOT COVERED: needs a target whose channel reliably throws. Not constructed this round; reported as uncovered rather than claimed."
195
+ },
196
+ {
197
+ "id": "restore-failed",
198
+ "mode": "none",
199
+ "tool": "wpm_focus",
200
+ "args": { "hwnd": "$liveHwnd", "elementName": "x" },
201
+ "expect": "restore_failed",
202
+ "note": "NOT COVERED: needs a real elevated window. task_52."
203
+ }
204
+ ]
205
+ }