rightkit-control 0.1.2

Background native input, accessibility reads, and a WebDriver bridge for driving real desktop apps without stealing focus.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! In-app control server (Tauri plugin, feature `tauri-plugin`).
//!
//! Started only when the launcher sets `RIGHTKIT_CONTROL_SERVICE=<name>`
//! (a lowercase service name such as `control-genright`); normal user launches
//! never set it, so nothing listens. The server is a `rightkit-service` host:
//! discovery file `<suite>/runtime/<name>/service.json`, a current-user-only
//! Unix socket (macOS) or named pipe (Windows), and a per-app request
//! allowlist. There is no TCP listener and no bearer token. The suite root is
//! resolved by `Suite::resolve` (honours `RIGHTKIT_SUITE_ROOT` for isolated
//! runs) and the server **fails closed**: if no absolute root exists the plugin
//! does not start, it never falls back to the working directory.
//!
//! Input is delivered inside the process straight to the webview: macOS
//! dispatches synthesized `NSEvent`s to the `WKWebView` (no window activation,
//! no first-mouse swallow), Windows drives WebView2 through
//! `CallDevToolsProtocolMethod` `Input.dispatch*`.
//!
//! Methods (x/y are viewport CSS pixels, origin top-left of the webview); the
//! params are the JSON bodies of the former `/rk/*` routes:
//!   health  click{x,y,button?,count?}  move{x,y}  drag{x1,y1,x2,y2,steps?}
//!   wheel{x,y,dx,dy}  key{key:"cmd+k"}  type{text}  eval{js}  dom{selector}
//!   ax  screenshot{mask?:[selector]} -> {path,bytes,masked} (PNG in `<service dir>/shots/`,
//!   owner-only; password/one-time-code/data-rk-secret elements and `mask` selectors are
//!   hidden during capture so secrets never reach the file)
//!   command{name,args}  typed app commands registered with `Control::command`
//!   lease_acquire{reconcile?:"delivered"|"lost"}  lease_release{leaseEpoch}
//!   lease_reconcile{leaseEpoch,decision}  lease_status
//!
//! EFF-001: every effectful method (input, eval, screenshot, command) passes
//! the plugin's [`EffectGate`] (`Control::gate`; default allow-all) before any
//! side effect and is settled with lifecycle events. PTY-002: when
//! `Control::input_lease` is set, input methods additionally require
//! `leaseEpoch` + `inputSequence` and are fenced/sequenced by the lease before
//! they reach the admission hook.

use crate::admission::{
    CancelToken, EffectGate, EffectKind, EffectRequest, ExecError, SettleOutcome,
};
use crate::json::esc;
use crate::keys;
use crate::lease::{InputError, InputLease, InputOutcome, UnacknowledgedInputDecision};
use rightkit_service::{
    host, Allowlist, CallContext, Emit, Handler, ServiceConfig, ServiceError, Suite,
};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tauri::plugin::TauriPlugin;
use tauri::{AppHandle, Manager, Runtime, WebviewWindow};

#[cfg(target_os = "macos")]
#[path = "mac_view.rs"]
mod native;
#[cfg(windows)]
#[path = "win_view.rs"]
mod native;
#[cfg(not(any(target_os = "macos", windows)))]
mod native {
    use super::{Button, Mouse};
    use tauri::{Runtime, WebviewWindow};
    const NO: &str = "unsupported platform";
    pub fn mouse<R: Runtime>(
        _: &WebviewWindow<R>,
        _: Mouse,
        _: Button,
        _: f64,
        _: f64,
        _: i64,
    ) -> Result<(), String> {
        Err(NO.into())
    }
    pub fn key<R: Runtime>(
        _: &WebviewWindow<R>,
        _: bool,
        _: u16,
        _: &str,
        _: u64,
    ) -> Result<(), String> {
        Err(NO.into())
    }
    pub fn wheel<R: Runtime>(
        _: &WebviewWindow<R>,
        _: f64,
        _: f64,
        _: f64,
        _: f64,
    ) -> Result<(), String> {
        Err(NO.into())
    }
    pub fn eval<R: Runtime>(_: &WebviewWindow<R>, _: &str) -> Result<String, String> {
        Err(NO.into())
    }
    pub fn screenshot<R: Runtime>(_: &WebviewWindow<R>) -> Result<Vec<u8>, String> {
        Err(NO.into())
    }
}
/// Windows inserts characters with no virtual key through `Input.insertText`.
#[cfg(windows)]
fn type_char<R: Runtime>(win: &WebviewWindow<R>, c: char) -> Result<(), String> {
    let s = c.to_string();
    if !c.is_ascii() {
        return native::insert_text(win, &s);
    }
    let code = keys::chord(&s.to_lowercase())
        .map(|(k, _)| k)
        .unwrap_or(0xFF);
    let fl = if c.is_uppercase() {
        keys::FLAG_SHIFT
    } else {
        0
    };
    key(win, true, code, &s, fl)?;
    key(win, false, code, &s, fl)
}
#[cfg(not(windows))]
fn type_char<R: Runtime>(win: &WebviewWindow<R>, c: char) -> Result<(), String> {
    let s = c.to_string();
    let code = keys::chord(&s.to_lowercase())
        .map(|(k, _)| k)
        .unwrap_or(0xFF);
    let fl = if c.is_uppercase() {
        keys::FLAG_SHIFT
    } else {
        0
    };
    key(win, true, code, &s, fl)?;
    key(win, false, code, &s, fl)
}
use native::{eval, key, mouse, screenshot, wheel};

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Mouse {
    Down,
    Up,
    Move,
    Drag,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Button {
    Left,
    Right,
}

type CommandFn<R> = Box<dyn Fn(&AppHandle<R>, &str) -> Result<String, String> + Send + Sync>;

/// Elements never captured in a screenshot: password and one-time-code inputs,
/// credential autocomplete hints, and anything the app marks `data-rk-secret`.
pub const SECRET_SELECTORS: &str = "input[type=password],input[autocomplete*=password],input[autocomplete=one-time-code],[data-rk-secret]";
static MASKED_LAST: AtomicU64 = AtomicU64::new(0);

/// Launcher-set environment variable that names the service and enables control.
pub const ENV_SERVICE: &str = "RIGHTKIT_CONTROL_SERVICE";

/// Every method the server understands (the default allowlist for QA/CLI apps).
pub const METHODS: &[&str] = &[
    "health",
    "click",
    "move",
    "drag",
    "wheel",
    "key",
    "type",
    "eval",
    "dom",
    "ax",
    "screenshot",
    "command",
    "lease_acquire",
    "lease_release",
    "lease_reconcile",
    "lease_status",
];

/// Effect kind of a control method; `None` for reads and lease management.
pub fn effect_kind(method: &str) -> Option<EffectKind> {
    match method {
        "click" | "move" | "drag" | "wheel" | "key" | "type" => Some(EffectKind::Input),
        "eval" => Some(EffectKind::Script),
        "screenshot" => Some(EffectKind::Capture),
        "command" => Some(EffectKind::Command),
        _ => None,
    }
}

/// Builder for the control plugin. `command` registers typed app verbs that
/// agents call through the `command` method (the closure gets the raw JSON
/// params and returns a JSON value or an error string).
pub struct Control<R: Runtime> {
    label: String,
    commands: HashMap<String, CommandFn<R>>,
    allow: Allowlist,
    gate: Arc<EffectGate>,
    lease: Option<Arc<InputLease>>,
}

impl<R: Runtime> Default for Control<R> {
    fn default() -> Self {
        Self::new()
    }
}

impl<R: Runtime> Control<R> {
    /// Default allowlist: the QA harness (`right-qa`) and the CLI (`right-ctl`)
    /// may call every method. Other apps get nothing until `allow_app`.
    pub fn new() -> Self {
        let allow = Allowlist::new()
            .allow("right-qa", METHODS.iter().copied())
            .allow("right-ctl", METHODS.iter().copied());
        Self {
            label: "main".into(),
            commands: HashMap::new(),
            allow,
            gate: Arc::new(EffectGate::allow_all()),
            lease: None,
        }
    }
    /// EFF-001 admission authority for every effectful method (default
    /// [`EffectGate::allow_all`]). Install hook and event sink on the gate.
    pub fn gate(mut self, gate: Arc<EffectGate>) -> Self {
        self.gate = gate;
        self
    }
    /// PTY-002: require `leaseEpoch`/`inputSequence` on input methods.
    pub fn input_lease(mut self, lease: Arc<InputLease>) -> Self {
        self.lease = Some(lease);
        self
    }
    pub fn window(mut self, label: &str) -> Self {
        self.label = label.into();
        self
    }
    pub fn command(
        mut self,
        name: &str,
        f: impl Fn(&AppHandle<R>, &str) -> Result<String, String> + Send + Sync + 'static,
    ) -> Self {
        self.commands.insert(name.into(), Box::new(f));
        self
    }
    /// Let another app call only `methods` (per-app request allowlist).
    pub fn allow_app<I: IntoIterator<Item = S>, S: Into<String>>(
        mut self,
        app: &str,
        methods: I,
    ) -> Self {
        self.allow = self.allow.allow(app, methods);
        self
    }
    /// True only when the launcher asked for control.
    pub fn enabled() -> bool {
        service_from_env().is_some()
    }
    /// `Some(plugin)` when enabled by env, else `None` (normal launches).
    pub fn build_if_enabled(self) -> Option<TauriPlugin<R>> {
        Self::enabled().then(|| self.build())
    }
    pub fn build(self) -> TauriPlugin<R> {
        let Control {
            label,
            commands,
            allow,
            gate,
            lease,
        } = self;
        let commands = Arc::new(commands);
        tauri::plugin::Builder::new("rightkit-control")
            .setup(move |app, _| {
                if let Some(service) = service_from_env() {
                    serve(
                        app.clone(),
                        label.clone(),
                        service,
                        commands.clone(),
                        allow.clone(),
                        Guards {
                            gate: gate.clone(),
                            lease: lease.clone(),
                        },
                    )?;
                }
                Ok(())
            })
            .build()
    }
}

fn service_from_env() -> Option<String> {
    std::env::var(ENV_SERVICE)
        .ok()
        .filter(|s| !s.trim().is_empty())
}

struct ControlHandler<R: Runtime> {
    app: AppHandle<R>,
    label: String,
    commands: Arc<HashMap<String, CommandFn<R>>>,
    shots: std::path::PathBuf,
    shot_seq: AtomicU64,
    guards: Guards,
}

#[derive(Clone)]
struct Guards {
    gate: Arc<EffectGate>,
    lease: Option<Arc<InputLease>>,
}

fn serve<R: Runtime>(
    app: AppHandle<R>,
    label: String,
    service: String,
    commands: Arc<HashMap<String, CommandFn<R>>>,
    allow: Allowlist,
    guards: Guards,
) -> Result<(), Box<dyn std::error::Error>> {
    // Fail closed: no absolute suite root means no server, never a cwd-relative one.
    let suite = Suite::resolve()?;
    let shots = suite.private_service_subdir(&service, "shots")?;
    let cfg = ServiceConfig::new(
        &service,
        &format!("control:{label}"),
        0,
        env!("CARGO_PKG_VERSION"),
        allow,
    );
    let handler = Arc::new(ControlHandler {
        app,
        label,
        commands,
        shots,
        shot_seq: AtomicU64::new(0),
        guards,
    });
    let hosted = host(&suite, cfg, handler)?;
    // Hosted releases on drop; the plugin keeps it for the life of the process.
    std::mem::forget(hosted);
    Ok(())
}

impl<R: Runtime> Handler for ControlHandler<R> {
    fn call(
        &self,
        _cx: &CallContext,
        method: &str,
        params: &Value,
        _emit: &Emit,
    ) -> Result<Value, ServiceError> {
        if method == "health" {
            return Ok(json!({"ok": true, "service": "rightkit-control"}));
        }
        if let Some(r) = self.lease_method(method, params) {
            return r;
        }
        // Precondition (no side effect), kept retryable as before the gate.
        if self.app.get_webview_window(&self.label).is_none() {
            return Err(ServiceError::retryable(
                "window_not_found",
                "webview window not created yet",
            ));
        }
        let Some(kind) = effect_kind(method) else {
            return self.execute(method, params);
        };
        let request = EffectRequest::new(kind, method, &self.label).with_params(params.to_string());
        let cancel = CancelToken::new();
        let exec = |_: &CancelToken| {
            self.execute(method, params).map_err(|e| {
                ExecError::Failed(if e.code == "control_error" {
                    e.message
                } else {
                    format!("{}: {}", e.code, e.message)
                })
            })
        };
        if let (EffectKind::Input, Some(lease)) = (kind, &self.guards.lease) {
            let epoch = u64_param(params, "leaseEpoch")?;
            let seq = u64_param(params, "inputSequence")?;
            let session =
                crate::lease::ControlSession::new(lease.clone(), self.guards.gate.clone());
            return match session.input(epoch, seq, request, &cancel, exec) {
                Ok(InputOutcome::Applied { ack, value }) => {
                    Ok(with_ack(value, ack.epoch, ack.sequence, false))
                }
                Ok(InputOutcome::Duplicate { ack }) => {
                    Ok(with_ack(json!({"ok": true}), ack.epoch, ack.sequence, true))
                }
                Err(InputError::Lease(e)) => Err(ServiceError::new(
                    format!("lease_{}", e.label()),
                    e.to_string(),
                )),
                Err(InputError::NotDelivered { reason }) => Err(ServiceError::new(
                    if reason.starts_with("denied") {
                        "denied"
                    } else {
                        "not_delivered"
                    },
                    reason,
                )),
                Err(e @ InputError::Ambiguous { .. }) => {
                    Err(ServiceError::new("input_unacknowledged", e.to_string()))
                }
            };
        }
        let effect = self.guards.gate.admit(request, &cancel, exec);
        let st = &effect.settlement;
        match st.outcome {
            SettleOutcome::Ok => Ok(effect.value.unwrap_or(Value::Null)),
            SettleOutcome::Denied => Err(ServiceError::new(
                "denied",
                st.reason.clone().unwrap_or_default(),
            )),
            SettleOutcome::Cancelled => Err(ServiceError::new(
                "cancelled",
                st.reason.clone().unwrap_or_default(),
            )),
            SettleOutcome::Failed => Err(ServiceError::new(
                "control_error",
                st.reason.clone().unwrap_or_default(),
            )),
        }
    }
}

fn u64_param(params: &Value, key: &str) -> Result<u64, ServiceError> {
    params.get(key).and_then(Value::as_u64).ok_or_else(|| {
        ServiceError::new("lease_required", format!("input requires integer '{key}'"))
    })
}

fn with_ack(mut value: Value, epoch: u64, sequence: u64, duplicate: bool) -> Value {
    if let Value::Object(map) = &mut value {
        map.insert("leaseEpoch".into(), json!(epoch));
        map.insert("ackedInputSequence".into(), json!(sequence));
        map.insert("duplicate".into(), json!(duplicate));
    }
    value
}

fn snapshot_json(s: crate::lease::LeaseSnapshot) -> Value {
    json!({
        "ok": true,
        "leaseEpoch": s.epoch,
        "attached": s.attached,
        "nextInputSequence": s.next_input_sequence,
        "ackedInputSequence": s.acked_input_sequence,
        "unacknowledgedInput": s.unacknowledged_input.map(|(a, b)| json!([a, b])),
    })
}

impl<R: Runtime> ControlHandler<R> {
    /// `lease_*` methods; `None` when `method` is not one of them.
    fn lease_method(&self, method: &str, params: &Value) -> Option<Result<Value, ServiceError>> {
        if !method.starts_with("lease_") {
            return None;
        }
        let Some(lease) = &self.guards.lease else {
            return Some(Err(ServiceError::new(
                "lease_disabled",
                "no input lease configured",
            )));
        };
        let decision = |k: &str| match params.get(k).and_then(Value::as_str) {
            Some("delivered") => Ok(Some(UnacknowledgedInputDecision::ReconcileAsDelivered)),
            Some("lost") => Ok(Some(UnacknowledgedInputDecision::ReconcileAsLost)),
            None => Ok(None),
            Some(other) => Err(ServiceError::new(
                "bad_request",
                format!("unknown decision '{other}'"),
            )),
        };
        let lease_err = |e: crate::lease::LeaseError| {
            ServiceError::new(format!("lease_{}", e.label()), e.to_string())
        };
        Some((|| match method {
            "lease_status" => Ok(snapshot_json(lease.snapshot())),
            "lease_acquire" => {
                let grant = match decision("reconcile")? {
                    Some(d) => lease.try_acquire_reconciling(d).map_err(lease_err)?,
                    None => lease.acquire().map_err(lease_err)?,
                };
                Ok(
                    json!({"ok": true, "leaseEpoch": grant.epoch, "nextInputSequence": grant.next_input_sequence, "fencedEpoch": grant.fenced_epoch}),
                )
            }
            "lease_release" => lease
                .release(u64_param(params, "leaseEpoch")?)
                .map(snapshot_json)
                .map_err(lease_err),
            "lease_reconcile" => {
                let d = decision("decision")?
                    .ok_or_else(|| ServiceError::new("bad_request", "missing 'decision'"))?;
                lease
                    .reconcile(u64_param(params, "leaseEpoch")?, d)
                    .map(snapshot_json)
                    .map_err(lease_err)
            }
            _ => Err(ServiceError::new(
                "bad_request",
                format!("unknown method {method}"),
            )),
        })())
    }

    /// Execute stage: the only place control side effects happen.
    fn execute(&self, method: &str, params: &Value) -> Result<Value, ServiceError> {
        let Some(win) = self.app.get_webview_window(&self.label) else {
            return Err(ServiceError::retryable(
                "window_not_found",
                "webview window not created yet",
            ));
        };
        match route(method, params, &win, &self.app, &self.commands) {
            Ok(Out::Json(j)) => Ok(serde_json::from_str(&j).unwrap_or(Value::Null)),
            Ok(Out::Png(p)) => {
                let n = self.shot_seq.fetch_add(1, Ordering::SeqCst);
                let path = self.shots.join(format!("shot-{n}.png"));
                std::fs::write(&path, &p).map_err(|e| ServiceError::new("io", e.to_string()))?;
                Ok(
                    json!({"ok": true, "path": path.to_string_lossy(), "bytes": p.len(), "masked": MASKED_LAST.load(Ordering::SeqCst)}),
                )
            }
            Err(e) => Err(ServiceError::new("control_error", e)),
        }
    }
}

enum Out {
    Json(String),
    Png(Vec<u8>),
}
fn ok() -> Result<Out, String> {
    Ok(Out::Json(r#"{"ok":true}"#.into()))
}
fn pause(ms: u64) {
    std::thread::sleep(Duration::from_millis(ms));
}
fn js_json<R: Runtime>(win: &WebviewWindow<R>, body: &str) -> Result<String, String> {
    eval(win, &format!("JSON.stringify((function(){{{body}}})())"))
}

fn route<R: Runtime>(
    method: &str,
    params: &Value,
    win: &WebviewWindow<R>,
    app: &AppHandle<R>,
    commands: &HashMap<String, CommandFn<R>>,
) -> Result<Out, String> {
    // Typed field access on the parsed params: text containing `"x":` or quotes can
    // never be mistaken for another field (the old flat string scan could).
    let str_field = |_: &Value, k: &str| params.get(k).and_then(Value::as_str).map(str::to_string);
    let num_field = |_: &Value, k: &str| params.get(k).and_then(Value::as_f64);
    let b = params;
    let n = |k: &str| num_field(b, k).ok_or_else(|| format!("missing number '{k}'"));
    match method {
        "move" => {
            mouse(win, Mouse::Move, Button::Left, n("x")?, n("y")?, 0)?;
            ok()
        }
        "click" => {
            let (x, y) = (n("x")?, n("y")?);
            let btn = if str_field(b, "button").as_deref() == Some("right") {
                Button::Right
            } else {
                Button::Left
            };
            let count = num_field(b, "count").unwrap_or(1.0).max(1.0) as i64;
            mouse(win, Mouse::Move, Button::Left, x, y, 0)?;
            pause(50);
            for c in 1..=count {
                mouse(win, Mouse::Down, btn, x, y, c)?;
                pause(40);
                mouse(win, Mouse::Up, btn, x, y, c)?;
                pause(40);
            }
            ok()
        }
        "drag" => {
            let (x1, y1, x2, y2) = (n("x1")?, n("y1")?, n("x2")?, n("y2")?);
            let steps = num_field(b, "steps").unwrap_or(12.0).clamp(1.0, 200.0) as u32;
            mouse(win, Mouse::Move, Button::Left, x1, y1, 0)?;
            pause(50);
            mouse(win, Mouse::Down, Button::Left, x1, y1, 1)?;
            pause(50);
            for i in 1..=steps {
                let t = i as f64 / steps as f64;
                mouse(
                    win,
                    Mouse::Drag,
                    Button::Left,
                    x1 + (x2 - x1) * t,
                    y1 + (y2 - y1) * t,
                    1,
                )?;
                pause(16);
            }
            mouse(win, Mouse::Up, Button::Left, x2, y2, 1)?;
            ok()
        }
        "wheel" => {
            wheel(
                win,
                n("x")?,
                n("y")?,
                num_field(b, "dx").unwrap_or(0.0),
                num_field(b, "dy").unwrap_or(0.0),
            )?;
            ok()
        }
        "key" => {
            let spec = str_field(b, "key").ok_or("missing 'key'")?;
            let (code, flags) =
                keys::chord(&spec).ok_or_else(|| format!("unknown key '{spec}'"))?;
            let last = spec.rsplit('+').next().unwrap_or("");
            let mut ch = if flags & (keys::FLAG_CMD | keys::FLAG_CTRL) == 0 {
                key_char(last)
            } else {
                String::new()
            };
            if ch.is_empty() && last.chars().count() == 1 {
                ch = last.to_string();
            }
            key(win, true, code, &ch, flags)?;
            pause(20);
            key(win, false, code, &ch, flags)?;
            ok()
        }
        "type" => {
            let text = str_field(b, "text").ok_or("missing 'text'")?;
            for c in text.chars() {
                type_char(win, c)?;
                pause(8);
            }
            ok()
        }
        "eval" => {
            let js = str_field(b, "js").ok_or("missing 'js'")?;
            let v = js_json(win, &js)?;
            Ok(Out::Json(format!(
                "{{\"ok\":true,\"value\":{}}}",
                if v.is_empty() { "null".into() } else { v }
            )))
        }
        "dom" => {
            let sel = str_field(b, "selector").ok_or("missing 'selector'")?;
            let js = format!(
                "const q=document.querySelectorAll(\"{}\");return Array.from(q).slice(0,200).map(e=>{{const r=e.getBoundingClientRect();return {{tag:e.tagName.toLowerCase(),id:e.id,cls:String(e.className),text:(e.innerText||e.value||'').slice(0,200),rect:[r.x,r.y,r.width,r.height],hover:e.matches(':hover'),focus:e===document.activeElement,disabled:!!e.disabled}}}});",
                esc(&sel)
            );
            let v = js_json(win, &js)?;
            Ok(Out::Json(format!("{{\"ok\":true,\"elements\":{v}}}")))
        }
        "ax" => {
            let js = r#"const sel='a,button,input,select,textarea,summary,[role],[tabindex],h1,h2,h3,h4,label';return Array.from(document.querySelectorAll(sel)).slice(0,500).map(e=>{const r=e.getBoundingClientRect();const role=e.getAttribute('role')||({A:'link',BUTTON:'button',INPUT:'textbox',SELECT:'combobox',TEXTAREA:'textbox',H1:'heading',H2:'heading',H3:'heading',H4:'heading',LABEL:'label',SUMMARY:'button'})[e.tagName]||e.tagName.toLowerCase();return {role,name:(e.getAttribute('aria-label')||e.innerText||e.value||e.placeholder||'').trim().slice(0,120),rect:[r.x,r.y,r.width,r.height],disabled:!!e.disabled,checked:e.checked===undefined?null:e.checked}});"#;
            let v = js_json(win, js)?;
            Ok(Out::Json(format!("{{\"ok\":true,\"nodes\":{v}}}")))
        }
        "screenshot" => {
            // Redact before persistence: credential-bearing elements are hidden in the page
            // while the pixels are captured, so the secret never reaches the PNG. Caller
            // `mask` selectors extend the built-in set; masking failure discards the capture.
            let extra: Vec<String> = params
                .get("mask")
                .and_then(Value::as_array)
                .map(|a| {
                    a.iter()
                        .filter_map(|v| v.as_str().map(str::to_string))
                        .collect()
                })
                .unwrap_or_default();
            let selectors = [SECRET_SELECTORS.to_string()]
                .into_iter()
                .chain(extra)
                .collect::<Vec<_>>()
                .join(",");
            let sel_json = serde_json::to_string(&selectors).map_err(|e| e.to_string())?;
            let hide = format!(
                "const els=Array.from(document.querySelectorAll({sel_json}));window.__rkMasked=els.map(e=>[e,e.style.getPropertyValue('visibility'),e.style.getPropertyPriority('visibility')]);for(const e of els)e.style.setProperty('visibility','hidden','important');return els.length;"
            );
            let masked = js_json(win, &hide)
                .map_err(|e| format!("screenshot masking failed ({e}); capture refused"))?;
            let shot = screenshot(win);
            let restore = "for(const [e,v,p] of (window.__rkMasked||[])){if(v)e.style.setProperty('visibility',v,p);else e.style.removeProperty('visibility');}window.__rkMasked=[];return true;";
            let restored = js_json(win, restore);
            let png = shot?;
            restored.map_err(|e| format!("could not restore masked elements: {e}"))?;
            MASKED_LAST.store(masked.trim().parse().unwrap_or(0), Ordering::SeqCst);
            Ok(Out::Png(png))
        }
        "command" => {
            let name = str_field(b, "name").ok_or("missing 'name'")?;
            let f = commands
                .get(&name)
                .ok_or_else(|| format!("unknown command '{name}'"))?;
            let v = f(app, &params.to_string())?;
            Ok(Out::Json(format!(
                "{{\"ok\":true,\"result\":{}}}",
                if v.is_empty() { "null".into() } else { v }
            )))
        }
        _ => Err(format!("unknown method {method}")),
    }
}

/// Character a bare named key produces (so `Enter` inserts a newline).
fn key_char(name: &str) -> String {
    match name.to_ascii_lowercase().as_str() {
        "return" | "enter" => "\r".into(),
        "tab" => "\t".into(),
        "space" => " ".into(),
        "escape" | "esc" => "\u{1b}".into(),
        "delete" | "backspace" => "\u{7f}".into(),
        _ => String::new(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn key_char_names() {
        assert_eq!(key_char("Enter"), "\r");
        assert!(METHODS.contains(&"screenshot"));
    }
    #[test]
    fn effectful_methods_are_classified() {
        for m in ["click", "move", "drag", "wheel", "key", "type"] {
            assert_eq!(effect_kind(m), Some(EffectKind::Input), "{m}");
        }
        assert_eq!(effect_kind("eval"), Some(EffectKind::Script));
        assert_eq!(effect_kind("screenshot"), Some(EffectKind::Capture));
        assert_eq!(effect_kind("command"), Some(EffectKind::Command));
        for m in ["health", "dom", "ax", "lease_status", "lease_acquire"] {
            assert_eq!(effect_kind(m), None, "{m}");
        }
    }
}