dsh-windows-parallel-mcp 0.8.2 → 0.9.0-rc.2

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.
Files changed (53) hide show
  1. package/CHANGELOG.de.md +444 -419
  2. package/CHANGELOG.es.md +444 -419
  3. package/CHANGELOG.fr.md +444 -419
  4. package/CHANGELOG.it.md +444 -419
  5. package/CHANGELOG.ja.md +444 -419
  6. package/CHANGELOG.ko.md +444 -419
  7. package/CHANGELOG.md +965 -940
  8. package/CHANGELOG.pt.md +444 -419
  9. package/CHANGELOG.ru.md +444 -419
  10. package/CHANGELOG.zh.md +444 -419
  11. package/INSTALL.de.md +126 -126
  12. package/INSTALL.es.md +126 -126
  13. package/INSTALL.fr.md +126 -126
  14. package/INSTALL.it.md +126 -126
  15. package/INSTALL.ja.md +126 -126
  16. package/INSTALL.ko.md +126 -126
  17. package/INSTALL.md +122 -122
  18. package/INSTALL.pt.md +126 -126
  19. package/INSTALL.ru.md +126 -126
  20. package/INSTALL.zh.md +126 -126
  21. package/README.de.md +684 -684
  22. package/README.es.md +684 -684
  23. package/README.fr.md +684 -684
  24. package/README.it.md +684 -684
  25. package/README.ja.md +684 -684
  26. package/README.ko.md +684 -684
  27. package/README.md +680 -680
  28. package/README.pt.md +684 -684
  29. package/README.ru.md +684 -684
  30. package/README.zh.md +684 -684
  31. package/cordis.patch.yml +15 -15
  32. package/docs/refusals.md +90 -90
  33. package/docs/takeover.md +128 -128
  34. package/dsh.plugin.json +18 -18
  35. package/lib/config.js +275 -275
  36. package/lib/engine.js +252 -252
  37. package/lib/host-tools.js +365 -338
  38. package/lib/index.js +1275 -1249
  39. package/lib/peers-schemastery.js +23 -23
  40. package/lib/peers-tools.js +15 -15
  41. package/lib/proof-window.mjs +427 -427
  42. package/package.json +122 -122
  43. package/server.json +110 -110
  44. package/src/engine/decide.ps1 +356 -356
  45. package/src/engine/engine.ps1 +1208 -1208
  46. package/src/engine/hud.ps1 +411 -411
  47. package/src/engine/input-inject.ps1 +176 -176
  48. package/src/engine/overlay.ps1 +389 -389
  49. package/src/engine/takeover.ps1 +144 -144
  50. package/src/engine/win.ps1 +1881 -1881
  51. package/src/engine/worker.ps1 +310 -310
  52. package/src/schema/tools.json +533 -533
  53. package/types/index.d.ts +190 -187
@@ -1,427 +1,427 @@
1
- // proof-window.mjs — the ONE implementation of "did the action disturb the owner".
2
- //
3
- // WHY THIS EXISTS AS A PURE MODULE. The product's central claim is that driving a window never
4
- // moves the user's cursor and never keeps their foreground. That claim is only worth anything if it
5
- // is MEASURED per call, not asserted in a README. The measurement is a window of samples
6
- // { ts, cx, cy, fg, focus, inputAgeMs }
7
- // taken around the action, and the judgment is arithmetic on that window. All of it is pure: no
8
- // file, no process, no syscall. So it can be unit-tested against SYNTHETIC samples where the right
9
- // answer is known by construction -- which is exactly what a claim this load-bearing needs.
10
- //
11
- // THE ATTRIBUTION RULE (why the raw cursor delta cannot be the test). GetLastInputInfo returns "ms
12
- // since the last PHYSICAL input". Between two samples, if that implied event timestamp MOVED, a
13
- // human touched the mouse or keyboard inside the gap -- and any motion there belongs to the human.
14
- // Motion in a gap with no physical event is UNEXPLAINED. So far, so standard.
15
- //
16
- // WHAT THE REAL DESKTOP RUNS AND AN IDLE BASELINE TAUGHT US. Unexplained does NOT mean "ours".
17
- // Real clean clicks measured isolated 3.16px and 7.07px cursor jumps while nobody touched anything --
18
- // another process nudging the cursor is routine. Against a hardcoded 1px that was a FALSE RED (the
19
- // gate-cries-wolf failure this repo's doctrine names); against a "generous" threshold it is laundering.
20
- // The decisive measurement was an IDLE BASELINE (scripts/idle-baseline.mjs (re-runnable)): with NOTHING of ours running,
21
- // over windows the same length as an action, the cursor already showed unexplained motion across up to
22
- // 3 separate gaps at ~11px (four idle windows: 0, 3, 0, 0 gaps). So even SUSTAINED multi-gap motion
23
- // cannot be blamed on our action from a coordinate stream -- GetLastInputInfo excludes the HUMAN and
24
- // nothing in a position delta excludes ANOTHER PROCESS. ">=2 gaps => fail" is therefore a wolf-cry the
25
- // idle baseline disproves. The cursor dimension reports:
26
- // 0 unexplained motion gaps => pass (direct evidence: nothing moved while we acted)
27
- // >=1 unexplained gap => inconclusive (something moved; the build-time gate says it was not
28
- // our code, so we surface the number and resolve nothing by blame)
29
- // THE CURSOR GUARANTEE LIVES WHERE IT IS DECIDABLE: scripts/check-no-cursor-control.mjs asserts the
30
- // shipped engine contains no SetCursorPos / SendInput / mouse_event / keybd_event -- a byte-level,
31
- // zero-false-positive build gate. A regression that ADDS cursor control is caught at build time with
32
- // certainty, strictly stronger than sampling at runtime on a desktop that moves on its own. Same
33
- // architecture as the schema DROPPING the `physical` field rather than detecting a disabled one:
34
- // enforce by structure, measure only what structure cannot settle.
35
- //
36
- // FOREGROUND/FOCUS keep a fail verdict because they are different: the product CAN legitimately touch
37
- // them (the UIA flash tier raises, then restores OUTSIDE the sample window; a regression could keep
38
- // it), and an ambient focus steal is rarer than cursor jitter -- the control window characterises it.
39
- // A switch is failed only when the control shows the desktop was NOT already switching seconds before;
40
- // a desktop that steals during the control makes it inconclusive instead of a false red.
41
- //
42
- // Verdict ordering: fail > inconclusive > pass. A dimension that cannot be judged is reported, not
43
- // assumed innocent: silence has never been evidence here.
44
- //
45
- // sampler.mjs and the engine both feed samples here. Two copies of this math would mean the
46
- // reachability numbers and the per-action proof were measured by different instruments while
47
- // carrying the same name -- the drift this repo already killed once (see sampler.mjs header).
48
-
49
- /**
50
- * One sampler reading. `inputAgeMs` is `undefined` when the instrument could not read it, which is
51
- * NOT the same as 0 ("input just happened") -- the whole fail-closed rule below depends on that.
52
- * @typedef {object} ProofSample
53
- * @property {number} ts
54
- * @property {number} [inputAgeMs]
55
- * @property {number} cx
56
- * @property {number} cy
57
- * @property {number|string} [fg]
58
- * @property {number|string} [focus]
59
- */
60
-
61
- /**
62
- * The software-attributable counts. Named rather than inferred because `{}` fragments this object at
63
- * every consumer (`attribution.x`, `summary.attribution.y`), and a checker that cannot see the keys
64
- * reports each read as an error against a type nobody wrote down.
65
- * @typedef {object} Attribution
66
- * @property {number} physicalInputEvents
67
- * @property {number} humanActiveMs
68
- * @property {number} quietMs
69
- * @property {number} unexplainedCursorMotionGaps
70
- * @property {number} unexplainedCursorMaxDeltaPx
71
- * @property {number|null} unexplainedCursorMaxDeltaAt
72
- * @property {number} unexplainedCursorPathPx
73
- * @property {number} unexplainedForegroundSwitches
74
- * @property {number} unexplainedFocusChanges
75
- * @property {number} unreadableInputAgeGaps
76
- */
77
-
78
- /**
79
- * One attempt's outcome, as the engine reported it.
80
- * @typedef {object} ActionAttempt
81
- * @property {boolean} [ok]
82
- * @property {string} [effect]
83
- */
84
-
85
- /**
86
- * @typedef {object} ProofOpts
87
- * @property {number} [minSamples]
88
- * @property {number} [humanGapFactor]
89
- * @property {ProofSample[]} [control]
90
- * @property {boolean} [declaredFlash]
91
- * @property {string} [effect]
92
- * @property {ActionAttempt[]} [actions]
93
- * @property {boolean} [declaredTakeover]
94
- * @property {boolean} [cursorRestored]
95
- * @property {number} [unreadableCap]
96
- */
97
-
98
- /**
99
- * @typedef {object} VerdictResult
100
- * @property {'pass'|'fail'|'inconclusive'|'insufficient'} verdict
101
- * @property {string} reason
102
- * @property {Record<string,string>} [dimensions]
103
- */
104
-
105
- /**
106
- * @typedef {object} ProofResult
107
- * @property {number} samples
108
- * @property {number} windowMs
109
- * @property {number|null} cursorMaxDeltaPx
110
- * @property {number|null} cursorMaxDeltaAt
111
- * @property {number|null} cursorTotalPathPx
112
- * @property {number|null} foregroundSwitches
113
- * @property {number|null} focusChanges
114
- * @property {Attribution|null} attribution
115
- * @property {Attribution|null} control
116
- * @property {'pass'|'fail'|'inconclusive'|'insufficient'} verdict
117
- * @property {string} reason
118
- * @property {Record<string,string>} [dimensions]
119
- */
120
-
121
- /** @param {number} v @returns {number} */
122
- const round = (v) => Math.round(v * 100) / 100;
123
-
124
- /**
125
- * Absolute timestamp of the last PHYSICAL input at sample x, or null if unreadable.
126
- * @param {ProofSample} x
127
- * @returns {number|null}
128
- */
129
- export function physEventAt(x) {
130
- return typeof x.inputAgeMs === 'number' && x.inputAgeMs >= 0 ? x.ts - x.inputAgeMs : null;
131
- }
132
-
133
- /**
134
- * Was there physical input inside the gap between b and a? Unreadable age => NOT human (fail closed).
135
- * @param {ProofSample} b
136
- * @param {ProofSample} a
137
- * @param {number} [humanGapFactor]
138
- * @returns {boolean}
139
- */
140
- export function isHumanGap(b, a, humanGapFactor = 0.6) {
141
- const ea = physEventAt(b), eb = physEventAt(a);
142
- if (ea === null || eb === null) return false;
143
- return eb > ea + Math.max(2, (a.ts - b.ts) * humanGapFactor);
144
- }
145
-
146
- /**
147
- * Evaluate one proof window. Pure.
148
- *
149
- * The `@param` types below were written as a plain object literal before, which is legal JSDoc but
150
- * was not applied where `Array` appeared bare: `{Array}` is not a usable type under checkJs (it
151
- * demands a type argument), so the checker discarded the whole annotation and read `opts` as `{}`.
152
- * Seven "property does not exist on type '{}'" errors were one symptom of that single mistake.
153
- * @param {ProofSample[]} samples the ACTION window
154
- * @param {ProofOpts} [opts]
155
- * control = the pre-action window (sampled while nothing was done). It cannot excuse motion in the
156
- * action window -- the structural rule already handles that -- but it decides whether the
157
- * foreground/focus dimensions are evaluable at all.
158
- * effect = the engine's own word on who reported success ('unverified' | 'reported-by-send' |
159
- * 'reported-by-pattern'). Omitted => no effect dimension, so every pre-existing caller keeps its
160
- * old behaviour byte for byte.
161
- * actions = optional per-attempt results ({ ok, effect }); when EVERY attempt failed to take
162
- * effect the verdict is fail regardless of how still the desktop stayed.
163
- * @returns {ProofResult}
164
- */
165
- export function evaluateProof(samples, opts = {}) {
166
- const minSamples = opts.minSamples ?? 3;
167
- const humanGap = opts.humanGapFactor ?? 0.6;
168
-
169
- // The cast is the point of the filter: `filter(Boolean)` drops the nulls at runtime but is not a
170
- // type predicate, so the array's declared element type has to be asserted once here rather than
171
- // sprinkled through the loop as non-null assertions.
172
- /** @type {ProofSample[]} */
173
- const r = /** @type {ProofSample[]} */ ((Array.isArray(samples) ? samples : [])
174
- .map((x) => (x && typeof x.ts === 'number' ? x : null))
175
- .filter(Boolean));
176
-
177
- if (r.length < minSamples) {
178
- return {
179
- samples: r.length, windowMs: 0,
180
- cursorMaxDeltaPx: null, cursorMaxDeltaAt: null, cursorTotalPathPx: null,
181
- foregroundSwitches: null, focusChanges: null, attribution: null, control: null,
182
- verdict: 'insufficient', reason: `proof needs >=${minSamples} samples, got ${r.length}`,
183
- };
184
- }
185
-
186
- let maxD = 0, maxAt = r[0].ts, total = 0;
187
- let fg = 0, fc = 0, unexplained = 0, unexplainedMax = 0, unexplainedMaxAt = 0;
188
- let motionGaps = 0;
189
- let physEvents = 0, humanMs = 0, quietMs = 0, unexplainedFg = 0, unexplainedFc = 0;
190
- let physUnreadable = 0;
191
-
192
- for (let i = 1; i < r.length; i++) {
193
- const a = r[i], b = r[i - 1];
194
- const gap = a.ts - b.ts;
195
- const ea = physEventAt(b), eb = physEventAt(a);
196
- let human = false;
197
- if (ea === null || eb === null) physUnreadable++;
198
- else human = eb > ea + Math.max(2, gap * humanGap);
199
-
200
- if (human) { physEvents++; humanMs += gap; } else quietMs += gap;
201
-
202
- const d = Math.hypot(a.cx - b.cx, a.cy - b.cy);
203
- if (d > maxD) { maxD = d; maxAt = a.ts; }
204
- total += d;
205
- if (!human && d > 0.5) {
206
- motionGaps += 1;
207
- unexplained += d;
208
- if (d > unexplainedMax) { unexplainedMax = d; unexplainedMaxAt = a.ts; }
209
- }
210
-
211
- // A gap with NO physical input in it is the only one where a switch could be OURS: if a human
212
- // touched something in the gap, the desktop changed hands for reasons that have nothing to do
213
- // with the door call, and counting that against the action is a false red (a click cannot fail
214
- // because the user alt-tabbed during the settle sleep).
215
- if (a.fg !== b.fg) { fg++; if (!human) unexplainedFg++; }
216
- if (a.focus !== b.focus) { fc++; if (!human) unexplainedFc++; }
217
- }
218
-
219
- const attribution = {
220
- physicalInputEvents: physEvents,
221
- humanActiveMs: round(humanMs),
222
- quietMs: round(quietMs),
223
- unexplainedCursorMotionGaps: motionGaps,
224
- unexplainedCursorMaxDeltaPx: round(unexplainedMax),
225
- unexplainedCursorMaxDeltaAt: unexplainedMaxAt,
226
- unexplainedCursorPathPx: round(unexplained),
227
- unexplainedForegroundSwitches: unexplainedFg,
228
- unexplainedFocusChanges: unexplainedFc,
229
- unreadableInputAgeGaps: physUnreadable,
230
- };
231
-
232
- let control = null;
233
- if (Array.isArray(opts.control) && opts.control.length >= minSamples) {
234
- const c = evaluateProof(opts.control, { minSamples });
235
- if (c.attribution) control = c.attribution;
236
- }
237
-
238
- const v = verdictFor(attribution, control, r.length, {
239
- declaredFlash: opts.declaredFlash === true,
240
- // R3 options travel through the same door as declaredFlash: a tier that DECLARES its disturbance is
241
- // judged by the promise it made, and 'cursorRestored === false' is the one case where a declared
242
- // move is still a failure (borrowed and not returned).
243
- declaredTakeover: opts.declaredTakeover === true,
244
- cursorRestored: opts.cursorRestored === false ? false : undefined,
245
- effect: opts.effect,
246
- actions: opts.actions,
247
- });
248
- return {
249
- samples: r.length,
250
- windowMs: r[r.length - 1].ts - r[0].ts,
251
- cursorMaxDeltaPx: round(maxD),
252
- cursorMaxDeltaAt: maxAt,
253
- cursorTotalPathPx: round(total),
254
- foregroundSwitches: fg,
255
- focusChanges: fc,
256
- attribution,
257
- control,
258
- verdict: v.verdict,
259
- reason: v.reason,
260
- dimensions: v.dimensions,
261
- };
262
- }
263
-
264
- /**
265
- * The judgment, from the action window's attribution and (optionally) the control window's.
266
- * cursor : 0 unexplained motion gaps => pass (nothing moved during the action -- direct evidence)
267
- * >=1 => inconclusive (the IDLE BASELINE measured up to 3 gaps / 11px
268
- * with nothing of ours running, so motion -- even multi-gap
269
- * -- cannot be blamed on the action from position data. The
270
- * number is surfaced. The real guarantee is the build-time
271
- * check-no-cursor-control gate, not this sensor.)
272
- * fg/focus: 0 unexplained switches => pass
273
- * action >0, control also => inconclusive (the desktop was switching on its own seconds
274
- * before; this switch cannot be pinned on us)
275
- * action >0, control clean => fail (the desktop was demonstrably not doing this; a
276
- * FOREGROUND/FOCUS steal is not ambient the way a cursor
277
- * nudge is -- and unlike the cursor, the product's touch on
278
- * focus is DECLARED, see declaredFlash below)
279
- * declaredFlash:true => a foreground switch is EXPECTED for the bounded-flash tier
280
- * (the engine raises, then restores after the sample window;
281
- * "we never touch your foreground" was never the promise for
282
- * that tier -- "we never KEEP it" is, and the restore table
283
- * owns it, surfacing restore_failed separately). Reported,
284
- * not failed.
285
- * unreadable instrument on most gaps => inconclusive (silence is not innocence)
286
- * effect : absent => the dimension does not exist (every pre-0.1.3 caller unchanged)
287
- * reported-by-send => pass, and the reason says 未独立验证: a channel reported that
288
- * reported-by-pattern the call RETURNED; nobody observed the application react
289
- * unverified / unknown => inconclusive (no channel reported anything, so a still
290
- * cursor cannot be read as "the action landed")
291
- * actions : every attempt failed => fail (动作未生效), however still the desktop stayed. This is
292
- * the case the pre-0.1.3 gate could read as a green light: 50 failed calls, zero
293
- * displacement. Displacement proves we did not disturb; it never proves we accomplished.
294
- * Overall = worst dimension: fail > inconclusive > pass.
295
- * @param {string|null} [effect]
296
- * @param {ActionAttempt[]} [actions]
297
- * @returns {{dim: 'pass'|'fail'|'inconclusive', note: string}|null}
298
- */
299
- function judgeEffect(effect, actions) {
300
- /** @type {ActionAttempt[]} */
301
- const list = Array.isArray(actions) ? actions.filter(Boolean) : [];
302
- /** @param {ActionAttempt} a @returns {boolean} */
303
- const reported = (a) => a.ok !== false
304
- && (a.effect === 'reported-by-send' || a.effect === 'reported-by-pattern');
305
-
306
- // Every attempt failed to take effect => fail, whatever the samples say. A displacement-free window
307
- // is the ABSENCE of disturbance, not the PRESENCE of accomplishment; letting it outvote a total
308
- // no-op is exactly the self-deception this dimension exists to stop.
309
- if (list.length > 0 && !list.some(reported)) {
310
- return {
311
- dim: 'fail',
312
- note: `动作未生效 (none of ${list.length} action attempt(s) took effect: no channel ever reported success, so the clean window below proves only that we did nothing loudly)`,
313
- };
314
- }
315
- // No effect supplied at all => no dimension. This keeps the module's old contract for every caller
316
- // that predates it (the 32 pre-existing arms must not move).
317
- if (effect === undefined || effect === null || effect === '') return null;
318
- if (effect === 'reported-by-send' || effect === 'reported-by-pattern') {
319
- return {
320
- dim: 'pass',
321
- note: `effect=${effect}: a channel reported the call returned -- 未独立验证 (not independently verified: the application's own handling was never observed)`,
322
- };
323
- }
324
- // 'unverified', and anything this module does not know, cannot be laundered into a pass.
325
- return {
326
- dim: 'inconclusive',
327
- note: `effect='${effect}': no channel reported success, so the clean window below cannot be read as the action having landed`,
328
- };
329
- }
330
-
331
- /**
332
- * @param {Attribution|null} attribution
333
- * @param {Attribution|null} control
334
- * @param {number} sampleCount
335
- * @param {ProofOpts} [opts]
336
- * @returns {VerdictResult}
337
- */
338
- export function verdictFor(attribution, control, sampleCount, opts = {}) {
339
- // `Partial` because both callers legitimately pass nothing (`null`) for a window they have no
340
- // control for, and every read below already defends with `?? 0`.
341
- /** @type {Partial<Attribution>} */
342
- const a = attribution || {};
343
- const unreadableCap = opts.unreadableCap ?? Math.max(1, Math.floor((sampleCount - 1) * 0.3));
344
- /** @type {Record<string,string>} */
345
- const dims = {};
346
-
347
- if ((a.unreadableInputAgeGaps ?? 0) > unreadableCap) {
348
- return {
349
- verdict: 'inconclusive',
350
- reason: `input-age unreadable for ${a.unreadableInputAgeGaps} gaps; cannot attribute anything`,
351
- dimensions: { cursor: 'unreadable', foreground: 'unreadable', focus: 'unreadable' },
352
- };
353
- }
354
-
355
- const gaps = a.unexplainedCursorMotionGaps ?? 0;
356
- const ux = a.unexplainedCursorMaxDeltaPx ?? 0;
357
- // No 'fail' on the cursor dimension: see the header. The idle baseline proved the desktop itself
358
- // produces multi-gap unexplained motion, so blaming it on the action is a false red by construction.
359
- // The takeover tier is the ONE case where the engine itself moves the pointer, so motion there is
360
- // DECLARED (informational) rather than unattributable -- and the promise it must keep is the restore,
361
- // which is checked separately below by cursorRestored.
362
- dims.cursor = gaps === 0 ? 'pass' : (opts.declaredTakeover ? 'declared' : 'inconclusive');
363
-
364
- const cFg = control ? (control.unexplainedForegroundSwitches ?? 0) : 0;
365
- const cFc = control ? (control.unexplainedFocusChanges ?? 0) : 0;
366
- const aFg = a.unexplainedForegroundSwitches ?? 0;
367
- const aFc = a.unexplainedFocusChanges ?? 0;
368
- if (aFg === 0) dims.foreground = 'pass';
369
- else if (opts.declaredFlash || opts.declaredTakeover) dims.foreground = 'declared';
370
- else dims.foreground = control && cFg > 0 ? 'inconclusive' : 'fail';
371
- if (aFc === 0) dims.focus = 'pass';
372
- else if (opts.declaredTakeover) dims.focus = 'declared';
373
- else dims.focus = control && cFc > 0 ? 'inconclusive' : 'fail';
374
-
375
- const parts = [];
376
- if (dims.cursor === 'inconclusive') parts.push(`cursor moved ${ux}px in ${gaps} gap(s) with no physical input -- not attributable to the action (the desktop itself does this: idle baseline measured up to 3 gaps/11px with nothing running); the cursor promise is enforced at build time by check-no-cursor-control`);
377
- if (dims.cursor === 'declared') parts.push(`cursor moved ${ux}px in ${gaps} gap(s), DECLARED by the takeover tier (the pointer was placed on the action point on request) -- the promise that applies is the restore, reported as cursorRestored`);
378
- if (dims.foreground === 'declared') parts.push(`foreground switched, but this tier DECLARED a bounded flash (raise then restore) -- the promise is never to KEEP the foreground, owned by the restore table`);
379
- if (dims.foreground === 'fail') parts.push(`${aFg} unexplained foreground switch(es) while the control showed the desktop was not switching and this tier did not declare a flash -- ours to explain`);
380
- if (dims.foreground === 'inconclusive') parts.push(`${aFg} foreground switch(es), but the desktop was switching on its own during the control too -- unattributable`);
381
- if (dims.focus === 'fail') parts.push(`${aFc} unexplained focus change(s) with a clean control`);
382
- if (dims.focus === 'inconclusive') parts.push(`${aFc} focus change(s) mirroring the desktop's own behaviour during the control`);
383
-
384
- const eff = judgeEffect(opts.effect, opts.actions);
385
- if (eff) { dims.effect = eff.dim; parts.push(eff.note); }
386
-
387
- const values = Object.values(dims);
388
- // 'declared' is informational, never the worst value: it cannot drag the verdict down on its own.
389
- const verdict = values.includes('fail') ? 'fail' : values.includes('inconclusive') ? 'inconclusive' : 'pass';
390
- // A 'pass' verdict must never print a reason that contradicts the dimensions. The bounded-flash
391
- // case passes WITH foreground switches (authorized by the tier's contract); claiming "no switches"
392
- // would be the README-in-a-tool-result failure mode this whole module exists to prevent.
393
- const reason = parts.length
394
- ? parts.join('; ')
395
- : `no unexplained cursor motion in ${sampleCount - 1} gaps, no unexplained foreground/focus switches`;
396
-
397
- // THE ONE HARD RED LINE OF THE TAKEOVER TIER: borrowed and not returned. It outranks every dimension
398
- // because "the desktop was still" is irrelevant when the pointer is demonstrably somewhere else than
399
- // where it was handed over. A killswitch that only *usually* puts the pointer back is not a promise.
400
- if (opts.cursorRestored === false) {
401
- return {
402
- verdict: 'fail',
403
- reason: `光标借了未还 (the takeover moved the cursor and the read-back shows it did not return) -- ${reason}`,
404
- dimensions: { ...dims, cursor: 'fail' },
405
- };
406
- }
407
- return { verdict, reason, dimensions: dims };
408
- }
409
-
410
- /**
411
- * The software-attributable quantities, flattened for callers that only want the numbers.
412
- * @param {ProofResult|{attribution?: Attribution|null}} [summary]
413
- * @returns {{motionGaps: number|null, maxPx: number|null, pathPx: number|null,
414
- * foregroundSwitches: number|null, focusChanges: number|null, quietMs: number|null}}
415
- */
416
- export function softwareCursorEvidence(summary) {
417
- /** @type {Partial<Attribution>} */
418
- const a = (summary && summary.attribution) || {};
419
- return {
420
- motionGaps: a.unexplainedCursorMotionGaps ?? null,
421
- maxPx: a.unexplainedCursorMaxDeltaPx ?? null,
422
- pathPx: a.unexplainedCursorPathPx ?? null,
423
- foregroundSwitches: a.unexplainedForegroundSwitches ?? null,
424
- focusChanges: a.unexplainedFocusChanges ?? null,
425
- quietMs: a.quietMs ?? null,
426
- };
427
- }
1
+ // proof-window.mjs — the ONE implementation of "did the action disturb the owner".
2
+ //
3
+ // WHY THIS EXISTS AS A PURE MODULE. The product's central claim is that driving a window never
4
+ // moves the user's cursor and never keeps their foreground. That claim is only worth anything if it
5
+ // is MEASURED per call, not asserted in a README. The measurement is a window of samples
6
+ // { ts, cx, cy, fg, focus, inputAgeMs }
7
+ // taken around the action, and the judgment is arithmetic on that window. All of it is pure: no
8
+ // file, no process, no syscall. So it can be unit-tested against SYNTHETIC samples where the right
9
+ // answer is known by construction -- which is exactly what a claim this load-bearing needs.
10
+ //
11
+ // THE ATTRIBUTION RULE (why the raw cursor delta cannot be the test). GetLastInputInfo returns "ms
12
+ // since the last PHYSICAL input". Between two samples, if that implied event timestamp MOVED, a
13
+ // human touched the mouse or keyboard inside the gap -- and any motion there belongs to the human.
14
+ // Motion in a gap with no physical event is UNEXPLAINED. So far, so standard.
15
+ //
16
+ // WHAT THE REAL DESKTOP RUNS AND AN IDLE BASELINE TAUGHT US. Unexplained does NOT mean "ours".
17
+ // Real clean clicks measured isolated 3.16px and 7.07px cursor jumps while nobody touched anything --
18
+ // another process nudging the cursor is routine. Against a hardcoded 1px that was a FALSE RED (the
19
+ // gate-cries-wolf failure this repo's doctrine names); against a "generous" threshold it is laundering.
20
+ // The decisive measurement was an IDLE BASELINE (scripts/idle-baseline.mjs (re-runnable)): with NOTHING of ours running,
21
+ // over windows the same length as an action, the cursor already showed unexplained motion across up to
22
+ // 3 separate gaps at ~11px (four idle windows: 0, 3, 0, 0 gaps). So even SUSTAINED multi-gap motion
23
+ // cannot be blamed on our action from a coordinate stream -- GetLastInputInfo excludes the HUMAN and
24
+ // nothing in a position delta excludes ANOTHER PROCESS. ">=2 gaps => fail" is therefore a wolf-cry the
25
+ // idle baseline disproves. The cursor dimension reports:
26
+ // 0 unexplained motion gaps => pass (direct evidence: nothing moved while we acted)
27
+ // >=1 unexplained gap => inconclusive (something moved; the build-time gate says it was not
28
+ // our code, so we surface the number and resolve nothing by blame)
29
+ // THE CURSOR GUARANTEE LIVES WHERE IT IS DECIDABLE: scripts/check-no-cursor-control.mjs asserts the
30
+ // shipped engine contains no SetCursorPos / SendInput / mouse_event / keybd_event -- a byte-level,
31
+ // zero-false-positive build gate. A regression that ADDS cursor control is caught at build time with
32
+ // certainty, strictly stronger than sampling at runtime on a desktop that moves on its own. Same
33
+ // architecture as the schema DROPPING the `physical` field rather than detecting a disabled one:
34
+ // enforce by structure, measure only what structure cannot settle.
35
+ //
36
+ // FOREGROUND/FOCUS keep a fail verdict because they are different: the product CAN legitimately touch
37
+ // them (the UIA flash tier raises, then restores OUTSIDE the sample window; a regression could keep
38
+ // it), and an ambient focus steal is rarer than cursor jitter -- the control window characterises it.
39
+ // A switch is failed only when the control shows the desktop was NOT already switching seconds before;
40
+ // a desktop that steals during the control makes it inconclusive instead of a false red.
41
+ //
42
+ // Verdict ordering: fail > inconclusive > pass. A dimension that cannot be judged is reported, not
43
+ // assumed innocent: silence has never been evidence here.
44
+ //
45
+ // sampler.mjs and the engine both feed samples here. Two copies of this math would mean the
46
+ // reachability numbers and the per-action proof were measured by different instruments while
47
+ // carrying the same name -- the drift this repo already killed once (see sampler.mjs header).
48
+
49
+ /**
50
+ * One sampler reading. `inputAgeMs` is `undefined` when the instrument could not read it, which is
51
+ * NOT the same as 0 ("input just happened") -- the whole fail-closed rule below depends on that.
52
+ * @typedef {object} ProofSample
53
+ * @property {number} ts
54
+ * @property {number} [inputAgeMs]
55
+ * @property {number} cx
56
+ * @property {number} cy
57
+ * @property {number|string} [fg]
58
+ * @property {number|string} [focus]
59
+ */
60
+
61
+ /**
62
+ * The software-attributable counts. Named rather than inferred because `{}` fragments this object at
63
+ * every consumer (`attribution.x`, `summary.attribution.y`), and a checker that cannot see the keys
64
+ * reports each read as an error against a type nobody wrote down.
65
+ * @typedef {object} Attribution
66
+ * @property {number} physicalInputEvents
67
+ * @property {number} humanActiveMs
68
+ * @property {number} quietMs
69
+ * @property {number} unexplainedCursorMotionGaps
70
+ * @property {number} unexplainedCursorMaxDeltaPx
71
+ * @property {number|null} unexplainedCursorMaxDeltaAt
72
+ * @property {number} unexplainedCursorPathPx
73
+ * @property {number} unexplainedForegroundSwitches
74
+ * @property {number} unexplainedFocusChanges
75
+ * @property {number} unreadableInputAgeGaps
76
+ */
77
+
78
+ /**
79
+ * One attempt's outcome, as the engine reported it.
80
+ * @typedef {object} ActionAttempt
81
+ * @property {boolean} [ok]
82
+ * @property {string} [effect]
83
+ */
84
+
85
+ /**
86
+ * @typedef {object} ProofOpts
87
+ * @property {number} [minSamples]
88
+ * @property {number} [humanGapFactor]
89
+ * @property {ProofSample[]} [control]
90
+ * @property {boolean} [declaredFlash]
91
+ * @property {string} [effect]
92
+ * @property {ActionAttempt[]} [actions]
93
+ * @property {boolean} [declaredTakeover]
94
+ * @property {boolean} [cursorRestored]
95
+ * @property {number} [unreadableCap]
96
+ */
97
+
98
+ /**
99
+ * @typedef {object} VerdictResult
100
+ * @property {'pass'|'fail'|'inconclusive'|'insufficient'} verdict
101
+ * @property {string} reason
102
+ * @property {Record<string,string>} [dimensions]
103
+ */
104
+
105
+ /**
106
+ * @typedef {object} ProofResult
107
+ * @property {number} samples
108
+ * @property {number} windowMs
109
+ * @property {number|null} cursorMaxDeltaPx
110
+ * @property {number|null} cursorMaxDeltaAt
111
+ * @property {number|null} cursorTotalPathPx
112
+ * @property {number|null} foregroundSwitches
113
+ * @property {number|null} focusChanges
114
+ * @property {Attribution|null} attribution
115
+ * @property {Attribution|null} control
116
+ * @property {'pass'|'fail'|'inconclusive'|'insufficient'} verdict
117
+ * @property {string} reason
118
+ * @property {Record<string,string>} [dimensions]
119
+ */
120
+
121
+ /** @param {number} v @returns {number} */
122
+ const round = (v) => Math.round(v * 100) / 100;
123
+
124
+ /**
125
+ * Absolute timestamp of the last PHYSICAL input at sample x, or null if unreadable.
126
+ * @param {ProofSample} x
127
+ * @returns {number|null}
128
+ */
129
+ export function physEventAt(x) {
130
+ return typeof x.inputAgeMs === 'number' && x.inputAgeMs >= 0 ? x.ts - x.inputAgeMs : null;
131
+ }
132
+
133
+ /**
134
+ * Was there physical input inside the gap between b and a? Unreadable age => NOT human (fail closed).
135
+ * @param {ProofSample} b
136
+ * @param {ProofSample} a
137
+ * @param {number} [humanGapFactor]
138
+ * @returns {boolean}
139
+ */
140
+ export function isHumanGap(b, a, humanGapFactor = 0.6) {
141
+ const ea = physEventAt(b), eb = physEventAt(a);
142
+ if (ea === null || eb === null) return false;
143
+ return eb > ea + Math.max(2, (a.ts - b.ts) * humanGapFactor);
144
+ }
145
+
146
+ /**
147
+ * Evaluate one proof window. Pure.
148
+ *
149
+ * The `@param` types below were written as a plain object literal before, which is legal JSDoc but
150
+ * was not applied where `Array` appeared bare: `{Array}` is not a usable type under checkJs (it
151
+ * demands a type argument), so the checker discarded the whole annotation and read `opts` as `{}`.
152
+ * Seven "property does not exist on type '{}'" errors were one symptom of that single mistake.
153
+ * @param {ProofSample[]} samples the ACTION window
154
+ * @param {ProofOpts} [opts]
155
+ * control = the pre-action window (sampled while nothing was done). It cannot excuse motion in the
156
+ * action window -- the structural rule already handles that -- but it decides whether the
157
+ * foreground/focus dimensions are evaluable at all.
158
+ * effect = the engine's own word on who reported success ('unverified' | 'reported-by-send' |
159
+ * 'reported-by-pattern'). Omitted => no effect dimension, so every pre-existing caller keeps its
160
+ * old behaviour byte for byte.
161
+ * actions = optional per-attempt results ({ ok, effect }); when EVERY attempt failed to take
162
+ * effect the verdict is fail regardless of how still the desktop stayed.
163
+ * @returns {ProofResult}
164
+ */
165
+ export function evaluateProof(samples, opts = {}) {
166
+ const minSamples = opts.minSamples ?? 3;
167
+ const humanGap = opts.humanGapFactor ?? 0.6;
168
+
169
+ // The cast is the point of the filter: `filter(Boolean)` drops the nulls at runtime but is not a
170
+ // type predicate, so the array's declared element type has to be asserted once here rather than
171
+ // sprinkled through the loop as non-null assertions.
172
+ /** @type {ProofSample[]} */
173
+ const r = /** @type {ProofSample[]} */ ((Array.isArray(samples) ? samples : [])
174
+ .map((x) => (x && typeof x.ts === 'number' ? x : null))
175
+ .filter(Boolean));
176
+
177
+ if (r.length < minSamples) {
178
+ return {
179
+ samples: r.length, windowMs: 0,
180
+ cursorMaxDeltaPx: null, cursorMaxDeltaAt: null, cursorTotalPathPx: null,
181
+ foregroundSwitches: null, focusChanges: null, attribution: null, control: null,
182
+ verdict: 'insufficient', reason: `proof needs >=${minSamples} samples, got ${r.length}`,
183
+ };
184
+ }
185
+
186
+ let maxD = 0, maxAt = r[0].ts, total = 0;
187
+ let fg = 0, fc = 0, unexplained = 0, unexplainedMax = 0, unexplainedMaxAt = 0;
188
+ let motionGaps = 0;
189
+ let physEvents = 0, humanMs = 0, quietMs = 0, unexplainedFg = 0, unexplainedFc = 0;
190
+ let physUnreadable = 0;
191
+
192
+ for (let i = 1; i < r.length; i++) {
193
+ const a = r[i], b = r[i - 1];
194
+ const gap = a.ts - b.ts;
195
+ const ea = physEventAt(b), eb = physEventAt(a);
196
+ let human = false;
197
+ if (ea === null || eb === null) physUnreadable++;
198
+ else human = eb > ea + Math.max(2, gap * humanGap);
199
+
200
+ if (human) { physEvents++; humanMs += gap; } else quietMs += gap;
201
+
202
+ const d = Math.hypot(a.cx - b.cx, a.cy - b.cy);
203
+ if (d > maxD) { maxD = d; maxAt = a.ts; }
204
+ total += d;
205
+ if (!human && d > 0.5) {
206
+ motionGaps += 1;
207
+ unexplained += d;
208
+ if (d > unexplainedMax) { unexplainedMax = d; unexplainedMaxAt = a.ts; }
209
+ }
210
+
211
+ // A gap with NO physical input in it is the only one where a switch could be OURS: if a human
212
+ // touched something in the gap, the desktop changed hands for reasons that have nothing to do
213
+ // with the door call, and counting that against the action is a false red (a click cannot fail
214
+ // because the user alt-tabbed during the settle sleep).
215
+ if (a.fg !== b.fg) { fg++; if (!human) unexplainedFg++; }
216
+ if (a.focus !== b.focus) { fc++; if (!human) unexplainedFc++; }
217
+ }
218
+
219
+ const attribution = {
220
+ physicalInputEvents: physEvents,
221
+ humanActiveMs: round(humanMs),
222
+ quietMs: round(quietMs),
223
+ unexplainedCursorMotionGaps: motionGaps,
224
+ unexplainedCursorMaxDeltaPx: round(unexplainedMax),
225
+ unexplainedCursorMaxDeltaAt: unexplainedMaxAt,
226
+ unexplainedCursorPathPx: round(unexplained),
227
+ unexplainedForegroundSwitches: unexplainedFg,
228
+ unexplainedFocusChanges: unexplainedFc,
229
+ unreadableInputAgeGaps: physUnreadable,
230
+ };
231
+
232
+ let control = null;
233
+ if (Array.isArray(opts.control) && opts.control.length >= minSamples) {
234
+ const c = evaluateProof(opts.control, { minSamples });
235
+ if (c.attribution) control = c.attribution;
236
+ }
237
+
238
+ const v = verdictFor(attribution, control, r.length, {
239
+ declaredFlash: opts.declaredFlash === true,
240
+ // R3 options travel through the same door as declaredFlash: a tier that DECLARES its disturbance is
241
+ // judged by the promise it made, and 'cursorRestored === false' is the one case where a declared
242
+ // move is still a failure (borrowed and not returned).
243
+ declaredTakeover: opts.declaredTakeover === true,
244
+ cursorRestored: opts.cursorRestored === false ? false : undefined,
245
+ effect: opts.effect,
246
+ actions: opts.actions,
247
+ });
248
+ return {
249
+ samples: r.length,
250
+ windowMs: r[r.length - 1].ts - r[0].ts,
251
+ cursorMaxDeltaPx: round(maxD),
252
+ cursorMaxDeltaAt: maxAt,
253
+ cursorTotalPathPx: round(total),
254
+ foregroundSwitches: fg,
255
+ focusChanges: fc,
256
+ attribution,
257
+ control,
258
+ verdict: v.verdict,
259
+ reason: v.reason,
260
+ dimensions: v.dimensions,
261
+ };
262
+ }
263
+
264
+ /**
265
+ * The judgment, from the action window's attribution and (optionally) the control window's.
266
+ * cursor : 0 unexplained motion gaps => pass (nothing moved during the action -- direct evidence)
267
+ * >=1 => inconclusive (the IDLE BASELINE measured up to 3 gaps / 11px
268
+ * with nothing of ours running, so motion -- even multi-gap
269
+ * -- cannot be blamed on the action from position data. The
270
+ * number is surfaced. The real guarantee is the build-time
271
+ * check-no-cursor-control gate, not this sensor.)
272
+ * fg/focus: 0 unexplained switches => pass
273
+ * action >0, control also => inconclusive (the desktop was switching on its own seconds
274
+ * before; this switch cannot be pinned on us)
275
+ * action >0, control clean => fail (the desktop was demonstrably not doing this; a
276
+ * FOREGROUND/FOCUS steal is not ambient the way a cursor
277
+ * nudge is -- and unlike the cursor, the product's touch on
278
+ * focus is DECLARED, see declaredFlash below)
279
+ * declaredFlash:true => a foreground switch is EXPECTED for the bounded-flash tier
280
+ * (the engine raises, then restores after the sample window;
281
+ * "we never touch your foreground" was never the promise for
282
+ * that tier -- "we never KEEP it" is, and the restore table
283
+ * owns it, surfacing restore_failed separately). Reported,
284
+ * not failed.
285
+ * unreadable instrument on most gaps => inconclusive (silence is not innocence)
286
+ * effect : absent => the dimension does not exist (every pre-0.1.3 caller unchanged)
287
+ * reported-by-send => pass, and the reason says 未独立验证: a channel reported that
288
+ * reported-by-pattern the call RETURNED; nobody observed the application react
289
+ * unverified / unknown => inconclusive (no channel reported anything, so a still
290
+ * cursor cannot be read as "the action landed")
291
+ * actions : every attempt failed => fail (动作未生效), however still the desktop stayed. This is
292
+ * the case the pre-0.1.3 gate could read as a green light: 50 failed calls, zero
293
+ * displacement. Displacement proves we did not disturb; it never proves we accomplished.
294
+ * Overall = worst dimension: fail > inconclusive > pass.
295
+ * @param {string|null} [effect]
296
+ * @param {ActionAttempt[]} [actions]
297
+ * @returns {{dim: 'pass'|'fail'|'inconclusive', note: string}|null}
298
+ */
299
+ function judgeEffect(effect, actions) {
300
+ /** @type {ActionAttempt[]} */
301
+ const list = Array.isArray(actions) ? actions.filter(Boolean) : [];
302
+ /** @param {ActionAttempt} a @returns {boolean} */
303
+ const reported = (a) => a.ok !== false
304
+ && (a.effect === 'reported-by-send' || a.effect === 'reported-by-pattern');
305
+
306
+ // Every attempt failed to take effect => fail, whatever the samples say. A displacement-free window
307
+ // is the ABSENCE of disturbance, not the PRESENCE of accomplishment; letting it outvote a total
308
+ // no-op is exactly the self-deception this dimension exists to stop.
309
+ if (list.length > 0 && !list.some(reported)) {
310
+ return {
311
+ dim: 'fail',
312
+ note: `动作未生效 (none of ${list.length} action attempt(s) took effect: no channel ever reported success, so the clean window below proves only that we did nothing loudly)`,
313
+ };
314
+ }
315
+ // No effect supplied at all => no dimension. This keeps the module's old contract for every caller
316
+ // that predates it (the 32 pre-existing arms must not move).
317
+ if (effect === undefined || effect === null || effect === '') return null;
318
+ if (effect === 'reported-by-send' || effect === 'reported-by-pattern') {
319
+ return {
320
+ dim: 'pass',
321
+ note: `effect=${effect}: a channel reported the call returned -- 未独立验证 (not independently verified: the application's own handling was never observed)`,
322
+ };
323
+ }
324
+ // 'unverified', and anything this module does not know, cannot be laundered into a pass.
325
+ return {
326
+ dim: 'inconclusive',
327
+ note: `effect='${effect}': no channel reported success, so the clean window below cannot be read as the action having landed`,
328
+ };
329
+ }
330
+
331
+ /**
332
+ * @param {Attribution|null} attribution
333
+ * @param {Attribution|null} control
334
+ * @param {number} sampleCount
335
+ * @param {ProofOpts} [opts]
336
+ * @returns {VerdictResult}
337
+ */
338
+ export function verdictFor(attribution, control, sampleCount, opts = {}) {
339
+ // `Partial` because both callers legitimately pass nothing (`null`) for a window they have no
340
+ // control for, and every read below already defends with `?? 0`.
341
+ /** @type {Partial<Attribution>} */
342
+ const a = attribution || {};
343
+ const unreadableCap = opts.unreadableCap ?? Math.max(1, Math.floor((sampleCount - 1) * 0.3));
344
+ /** @type {Record<string,string>} */
345
+ const dims = {};
346
+
347
+ if ((a.unreadableInputAgeGaps ?? 0) > unreadableCap) {
348
+ return {
349
+ verdict: 'inconclusive',
350
+ reason: `input-age unreadable for ${a.unreadableInputAgeGaps} gaps; cannot attribute anything`,
351
+ dimensions: { cursor: 'unreadable', foreground: 'unreadable', focus: 'unreadable' },
352
+ };
353
+ }
354
+
355
+ const gaps = a.unexplainedCursorMotionGaps ?? 0;
356
+ const ux = a.unexplainedCursorMaxDeltaPx ?? 0;
357
+ // No 'fail' on the cursor dimension: see the header. The idle baseline proved the desktop itself
358
+ // produces multi-gap unexplained motion, so blaming it on the action is a false red by construction.
359
+ // The takeover tier is the ONE case where the engine itself moves the pointer, so motion there is
360
+ // DECLARED (informational) rather than unattributable -- and the promise it must keep is the restore,
361
+ // which is checked separately below by cursorRestored.
362
+ dims.cursor = gaps === 0 ? 'pass' : (opts.declaredTakeover ? 'declared' : 'inconclusive');
363
+
364
+ const cFg = control ? (control.unexplainedForegroundSwitches ?? 0) : 0;
365
+ const cFc = control ? (control.unexplainedFocusChanges ?? 0) : 0;
366
+ const aFg = a.unexplainedForegroundSwitches ?? 0;
367
+ const aFc = a.unexplainedFocusChanges ?? 0;
368
+ if (aFg === 0) dims.foreground = 'pass';
369
+ else if (opts.declaredFlash || opts.declaredTakeover) dims.foreground = 'declared';
370
+ else dims.foreground = control && cFg > 0 ? 'inconclusive' : 'fail';
371
+ if (aFc === 0) dims.focus = 'pass';
372
+ else if (opts.declaredTakeover) dims.focus = 'declared';
373
+ else dims.focus = control && cFc > 0 ? 'inconclusive' : 'fail';
374
+
375
+ const parts = [];
376
+ if (dims.cursor === 'inconclusive') parts.push(`cursor moved ${ux}px in ${gaps} gap(s) with no physical input -- not attributable to the action (the desktop itself does this: idle baseline measured up to 3 gaps/11px with nothing running); the cursor promise is enforced at build time by check-no-cursor-control`);
377
+ if (dims.cursor === 'declared') parts.push(`cursor moved ${ux}px in ${gaps} gap(s), DECLARED by the takeover tier (the pointer was placed on the action point on request) -- the promise that applies is the restore, reported as cursorRestored`);
378
+ if (dims.foreground === 'declared') parts.push(`foreground switched, but this tier DECLARED a bounded flash (raise then restore) -- the promise is never to KEEP the foreground, owned by the restore table`);
379
+ if (dims.foreground === 'fail') parts.push(`${aFg} unexplained foreground switch(es) while the control showed the desktop was not switching and this tier did not declare a flash -- ours to explain`);
380
+ if (dims.foreground === 'inconclusive') parts.push(`${aFg} foreground switch(es), but the desktop was switching on its own during the control too -- unattributable`);
381
+ if (dims.focus === 'fail') parts.push(`${aFc} unexplained focus change(s) with a clean control`);
382
+ if (dims.focus === 'inconclusive') parts.push(`${aFc} focus change(s) mirroring the desktop's own behaviour during the control`);
383
+
384
+ const eff = judgeEffect(opts.effect, opts.actions);
385
+ if (eff) { dims.effect = eff.dim; parts.push(eff.note); }
386
+
387
+ const values = Object.values(dims);
388
+ // 'declared' is informational, never the worst value: it cannot drag the verdict down on its own.
389
+ const verdict = values.includes('fail') ? 'fail' : values.includes('inconclusive') ? 'inconclusive' : 'pass';
390
+ // A 'pass' verdict must never print a reason that contradicts the dimensions. The bounded-flash
391
+ // case passes WITH foreground switches (authorized by the tier's contract); claiming "no switches"
392
+ // would be the README-in-a-tool-result failure mode this whole module exists to prevent.
393
+ const reason = parts.length
394
+ ? parts.join('; ')
395
+ : `no unexplained cursor motion in ${sampleCount - 1} gaps, no unexplained foreground/focus switches`;
396
+
397
+ // THE ONE HARD RED LINE OF THE TAKEOVER TIER: borrowed and not returned. It outranks every dimension
398
+ // because "the desktop was still" is irrelevant when the pointer is demonstrably somewhere else than
399
+ // where it was handed over. A killswitch that only *usually* puts the pointer back is not a promise.
400
+ if (opts.cursorRestored === false) {
401
+ return {
402
+ verdict: 'fail',
403
+ reason: `光标借了未还 (the takeover moved the cursor and the read-back shows it did not return) -- ${reason}`,
404
+ dimensions: { ...dims, cursor: 'fail' },
405
+ };
406
+ }
407
+ return { verdict, reason, dimensions: dims };
408
+ }
409
+
410
+ /**
411
+ * The software-attributable quantities, flattened for callers that only want the numbers.
412
+ * @param {ProofResult|{attribution?: Attribution|null}} [summary]
413
+ * @returns {{motionGaps: number|null, maxPx: number|null, pathPx: number|null,
414
+ * foregroundSwitches: number|null, focusChanges: number|null, quietMs: number|null}}
415
+ */
416
+ export function softwareCursorEvidence(summary) {
417
+ /** @type {Partial<Attribution>} */
418
+ const a = (summary && summary.attribution) || {};
419
+ return {
420
+ motionGaps: a.unexplainedCursorMotionGaps ?? null,
421
+ maxPx: a.unexplainedCursorMaxDeltaPx ?? null,
422
+ pathPx: a.unexplainedCursorPathPx ?? null,
423
+ foregroundSwitches: a.unexplainedForegroundSwitches ?? null,
424
+ focusChanges: a.unexplainedFocusChanges ?? null,
425
+ quietMs: a.quietMs ?? null,
426
+ };
427
+ }