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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
//! Routing one request to a platform action, and the guards it passes first.
//!
//! Every action arrives here, which is what makes this the right place for the
//! checks that must hold for all of them: the protocol version, the turn not
//! having been stopped, the window still resolving, the application being
//! approved, and the machine not being in a state where synthesizing input
//! would be unsafe. A platform leaf implements the OS action and nothing else,
//! so a guard cannot end up applied on one platform and forgotten on the other.

use serde_json::{json, Value};
use std::io::{Read, Write};
use std::sync::Mutex;

use super::app_identity::{launch_at, resolve_launch_target};
use super::approval::request_approval;
#[cfg(target_os = "macos")]
use super::platform_macos::element_is_transient_menu_item;
use super::state::{
    accessibility_revision, Observation, PendingAction, ServerState, WindowInfo,
    INPUT_GUARD_GRACE_MS,
};
use super::{
    active_window, click, close_window, desktop_locked, drag, ensure_permissions, input_sequence,
    invoke_element, last_input_age_ms, list_apps, list_windows, observe_window, press_key,
    resolve_window, scroll, set_value, type_text, validate_observation, InputStep,
    PROTOCOL_VERSION,
};
#[cfg(target_os = "macos")]
use super::{element_requires_frontmost, target_is_frontmost};

const MAX_SEQUENCE_STEPS: usize = 20;
const MAX_SEQUENCE_TEXT_CHARS: usize = 512;

/// How recently a person must have used the keyboard or mouse for an action to
/// be refused as racing them.
/// Every method this helper serves.
///
/// Load-bearing rather than documentation: a request naming anything outside
/// this list is refused before dispatch, so an arm added to the match below
/// without a line here is unreachable. That makes the list the one place the
/// vocabulary is stated, and the one place a cross-language contract test has
/// to read -- an array literal has a single shape, where a match has as many as
/// there are ways to write one. A test that parsed the control flow reported a
/// method as unhandled the first time two arms were grouped, which is how a
/// green suite teaches people to disbelieve it.
const SERVED_METHODS: &[&str] = &[
    "click",
    "close_window",
    "drag",
    "end_turn",
    "hello",
    "invoke_element",
    "launch_app",
    "list_apps",
    "list_windows",
    "observe_window",
    "press_key",
    "scroll",
    "sequence",
    "set_value",
    "type_text",
];

/// Held while one session disturbs the desktop.
///
/// Each connection is served on its own thread, which is right for observation
/// and for one session waiting on an approval while another works. Input is
/// different: the keyboard, the pointer and the foreground window are one
/// shared resource, and every input path here is "focus the window, then
/// inject". Two of those interleaving means one session's keystrokes arrive in
/// the window the other just brought forward -- silently, and with whatever
/// text or shortcut was being sent.
///
/// A single session cannot race itself: a connection is served one request at a
/// time, and the caller holds its own lock across the round trip. This exists
/// only for the case those cannot see, which is two sessions at once.
///
/// Serialising is not a compromise here but the only correct answer: there is
/// one system cursor and one keyboard to synthesize into. A platform offering a
/// cursor per task could schedule them in parallel instead; these APIs do not.
static DESKTOP_HELD: Mutex<bool> = Mutex::new(false);

/// A turn at the desktop, released when dropped.
#[derive(Debug)]
struct DesktopTurn;

impl Drop for DesktopTurn {
    fn drop(&mut self) {
        // Runs even if the action panicked, so a failure cannot strand the
        // desktop as permanently taken.
        *DESKTOP_HELD
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) = false;
    }
}

/// Take the desktop for one action, or refuse at once if another session has it.
///
/// Deliberately does not wait. Waiting here would mean holding a request the
/// caller may already have abandoned: a stop closes the connection, and a thread
/// parked on a lock is not reading that connection, so it would wake later and
/// inject input the user had already asked to stop. The helper is the one
/// process with no notion of a stop, so it must never hold work that a stop
/// needs to reach. Refusing immediately leaves the waiting to the caller, where
/// a stop already takes effect.
///
/// `desktop_busy` is raised before anything is touched, so unlike a timeout it
/// carries no possibility of a half-performed action, and retrying it is safe.
fn take_desktop() -> Result<DesktopTurn, (&'static str, String)> {
    // A poisoned lock is recovered rather than propagated: the flag is the whole
    // state, and refusing every later action because an unrelated request
    // panicked would turn one failure into an outage.
    let mut held = DESKTOP_HELD
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    if *held {
        return Err((
            "desktop_busy",
            "Another Computer Use session is using the desktop; observe the \
             window again and retry."
                .to_string(),
        ));
    }
    *held = true;
    Ok(DesktopTurn)
}

pub(super) fn dispatch_request(
    connection: &mut (impl Read + Write),
    state: &mut ServerState,
    message: &Value,
) -> Result<Value, (&'static str, String)> {
    if message.get("protocol_version").and_then(Value::as_u64) != Some(PROTOCOL_VERSION) {
        return Err((
            "protocol_mismatch",
            "Unsupported protocol version.".to_string(),
        ));
    }
    let method = message
        .get("method")
        .and_then(Value::as_str)
        .ok_or(("invalid_request", "Request method is missing.".to_string()))?;
    if !SERVED_METHODS.contains(&method) {
        return Err((
            "unsupported_operation",
            format!("{method:?} is not a Computer Use protocol method."),
        ));
    }
    let params = message
        .get("params")
        .and_then(Value::as_object)
        .cloned()
        .unwrap_or_default();
    let sequence_steps = if method == "sequence" {
        Some(parse_input_steps(&params)?)
    } else {
        None
    };
    let meta = message
        .get("meta")
        .and_then(Value::as_object)
        .cloned()
        .unwrap_or_default();

    match method {
        "end_turn" => {
            // The turn is over, so its screenshots and accessibility handles
            // can never be acted on again.
            state.clear_turn();
            return Ok(json!({}));
        }
        "list_apps" => return Ok(json!({"apps": list_apps()})),
        "list_windows" => {
            let app = params
                .get("app")
                .and_then(Value::as_str)
                .map(str::trim)
                .filter(|value| !value.is_empty());
            return Ok(json!({"windows": list_windows(app)}));
        }
        _ => {}
    }

    #[cfg(windows)]
    if requests_text_edit(method, &params) {
        return Err((
            "unsupported_operation",
            "This platform cannot begin text editing through an element invocation; use an observed semantic action or a platform-standard shortcut, then observe editable focus."
                .to_string(),
        ));
    }

    enforce_pending_action(state, method)?;

    // A launch names an application rather than a window. Observation creates
    // a context from a listed window. Every other action resolves the target
    // from its observation, keeping native handles out of the model contract.
    let (target, launch_path, observation_id) = if method == "launch_app" {
        let (target, path) = resolve_launch_target(&params)?;
        (target, Some(path), None)
    } else if method == "observe_window" {
        let value = params
            .get("window_id")
            .and_then(Value::as_str)
            .ok_or(("invalid_request", "window_id is required.".to_string()))?;
        (resolve_window(value)?, None, None)
    } else {
        let id = params
            .get("observation_id")
            .and_then(Value::as_str)
            .filter(|id| !id.is_empty())
            .ok_or((
                "invalid_request",
                "observation_id is required; observe the window again first.".to_string(),
            ))?;
        let observed = state.observations.get(id).ok_or((
            "stale_observation",
            "Observation is stale or already consumed; observe the window again and use its new observation_id."
                .to_string(),
        ))?;
        let current = resolve_window(&observed.window.hwnd.to_string())?;
        if current.app_id != observed.window.app_id {
            return Err((
                "stale_observation",
                "The observed window no longer belongs to the same application.".to_string(),
            ));
        }
        (current, None, Some(id))
    };
    let window = target;
    request_approval(connection, &window, &meta)?;
    ensure_permissions(method)?;
    // Mutations are serialized across agent sessions. Human input is a
    // separate concern: this lock cannot prevent it, so only operations that
    // actually require foreground input apply the recent-input guard below.
    let mut needs_user_idle = requires_user_idle(method, &window);
    #[cfg(target_os = "macos")]
    if method == "invoke_element" {
        needs_user_idle |=
            element_requires_frontmost(observation(state, observation_id)?, &params)?;
    }
    let _desktop = if changes_window_state(method) {
        let held = take_desktop()?;
        if needs_user_idle {
            enforce_input_guard(state)?;
        }
        Some(held)
    } else {
        None
    };
    if needs_user_idle {
        state.ensure_interaction_session()?;
    }
    // Approval may wait for user input, so freshness must be checked only
    // after approval and the desktop guard, immediately before the action.
    if requires_stable_observation(method) {
        validate_observation(observation(state, observation_id)?)?;
    }
    if let Some(path) = launch_path {
        launch_at(&path)?;
        state.note_global_action();
        return Ok(json!({
            "launched": true,
            "app_id": window.app_id,
            "requires_observe": true,
        }));
    }
    let previous_revision = observation_id
        .and_then(|id| state.observations.get(id))
        .and_then(|observation| observation.accessibility_revision);
    let active_before = changes_window_state(method).then(active_window).flatten();
    #[cfg(target_os = "macos")]
    let transient_text_candidate = requests_text_edit(method, &params)
        && element_is_transient_menu_item(observation(state, observation_id)?, &params)?;
    let mut pending_action: Option<PendingAction> = None;
    let mut completed_pending_action = false;
    let mut result = match method {
        "observe_window" => {
            state.settle_before_observe(window.hwnd);
            observe_window(state, &window, true)
        }
        "close_window" => close_window(&window),
        "click" => click(observation(state, observation_id)?, &params),
        "scroll" => scroll(observation(state, observation_id)?, &params),
        "drag" => drag(observation(state, observation_id)?, &params),
        "press_key" => press_key(observation(state, observation_id)?, &params),
        "sequence" => {
            let result = input_sequence(
                observation(state, observation_id)?,
                sequence_steps.as_deref().unwrap_or_default(),
            );
            if result
                .as_ref()
                .err()
                .is_some_and(|error| error.0 == "user_intervention")
            {
                state.invalidate_observations();
            }
            result
        }
        "type_text" => {
            #[cfg(target_os = "macos")]
            {
                let transient_text_ready = take_transient_text_ready(state, observation_id)?;
                type_text(
                    observation(state, observation_id)?,
                    &params,
                    transient_text_ready,
                )
            }
            #[cfg(windows)]
            {
                type_text(observation(state, observation_id)?, &params)
            }
        }
        "invoke_element" => {
            let pending = state.pending_action();
            let result = invoke_element(observation(state, observation_id)?, &params, pending);
            completed_pending_action = result.is_ok() && pending.is_some();
            result
        }
        "set_value" => {
            let (result, pending) = set_value(observation(state, observation_id)?, &params)?;
            pending_action = pending;
            Ok(result)
        }
        "perform_secondary_action" => Err((
            "unsupported_operation",
            format!("{method} is not available in this helper build."),
        )),
        _ => Err((
            "unsupported_operation",
            format!("Unsupported method: {method}"),
        )),
    }?;
    if method == "observe_window" {
        if let Some(pending) = state
            .pending_action()
            .filter(|pending| pending.hwnd == window.hwnd)
        {
            attach_pending_action(&mut result, pending);
        }
    }
    if !changes_window_state(method) {
        return Ok(result);
    }
    state.note_action(&window);
    if completed_pending_action {
        state.clear_pending_action();
    }
    if let Some(pending) = pending_action {
        state.set_pending_action(pending);
    }
    state.settle_before_observe(window.hwnd);
    if method == "sequence" {
        // A sequence spans more than one input event. Recheck after it settles
        // so physical input during the sequence cannot be mistaken for ours.
        enforce_input_guard(state)?;
    }
    let active_after = active_window();
    if let Some(active_after) = active_after.as_ref().filter(|active_after| {
        action_changed_active_window(
            &window,
            active_before.as_ref(),
            Some(*active_after),
            needs_user_idle,
        )
    }) {
        state.note_action(active_after);
        return Ok(action_handoff(result, active_after));
    }
    // Keep the safety boundary -- the input observation has already been
    // consumed -- but make the normal action cycle atomic for the caller. A
    // successful mutation is followed by a settled observation of the same
    // window, so the response itself carries the only identifier valid for
    // the next action. If the action removed or replaced that window, retain
    // the dispatch receipt and direct the caller to discover the new target.
    let refreshed = refresh_after_action(state, &window);
    let has_refreshed_observation = refreshed.is_some();
    let changed = accessibility_changed(previous_revision, refreshed.as_ref());
    #[cfg(target_os = "macos")]
    let transient_text_ready = transient_text_candidate
        && changed == Some(true)
        && mark_transient_text_ready(state, refreshed.as_ref());
    let mut response = action_receipt(result, refreshed, changed);
    #[cfg(target_os = "macos")]
    if transient_text_ready {
        if let Some(object) = response.as_object_mut() {
            object.insert("transient_text_ready".to_string(), json!(true));
            object.insert("next_action".to_string(), json!("type"));
        }
    }
    if let Some(pending) = state.pending_action().filter(|pending| {
        should_attach_pending_action(has_refreshed_observation, pending.hwnd, window.hwnd)
    }) {
        attach_pending_action(&mut response, pending);
    }
    Ok(response)
}

#[cfg(target_os = "macos")]
fn take_transient_text_ready(
    state: &mut ServerState,
    observation_id: Option<&str>,
) -> Result<bool, (&'static str, String)> {
    let observation = observation_mut(state, observation_id)?;
    Ok(std::mem::take(&mut observation.transient_text_ready))
}

#[cfg(target_os = "macos")]
fn mark_transient_text_ready(state: &mut ServerState, refreshed: Option<&Value>) -> bool {
    let Some(observation_id) = refreshed
        .and_then(|value| value.get("observation_id"))
        .and_then(Value::as_str)
    else {
        return false;
    };
    let Some(observation) = state.observations.get_mut(observation_id) else {
        return false;
    };
    observation.transient_text_ready = true;
    true
}

fn requests_text_edit(method: &str, params: &serde_json::Map<String, Value>) -> bool {
    method == "invoke_element"
        && params.get("expects_text_input").and_then(Value::as_bool) == Some(true)
}

/// Observe the action's target without turning an already-dispatched action
/// into a failure when the application replaced or closed that window.
fn refresh_after_action(state: &mut ServerState, previous: &WindowInfo) -> Option<Value> {
    let current = resolve_window(&previous.hwnd.to_string()).ok()?;
    if current.app_id != previous.app_id {
        return None;
    }
    state.settle_before_observe(current.hwnd);
    // Replacement observations keep semantic state current without paying to
    // encode images that the action response does not attach. A later
    // coordinate action starts with an explicit visual observation.
    observe_window(state, &current, false).ok()
}

fn parse_input_steps(
    params: &serde_json::Map<String, Value>,
) -> Result<Vec<InputStep>, (&'static str, String)> {
    let steps = params
        .get("steps")
        .and_then(Value::as_array)
        .filter(|steps| !steps.is_empty() && steps.len() <= MAX_SEQUENCE_STEPS)
        .ok_or((
            "invalid_request",
            "steps must contain 1 to 20 input steps.".to_string(),
        ))?;
    let mut parsed = Vec::with_capacity(steps.len());
    let mut text_chars = 0;
    for (index, step) in steps.iter().enumerate() {
        let step = step.as_object().ok_or((
            "invalid_request",
            format!("steps[{index}] must be an object."),
        ))?;
        let action = step.get("action").and_then(Value::as_str).unwrap_or("");
        let (field, value) = match action {
            "type" => ("text", step.get("text").and_then(Value::as_str)),
            "press_key" => ("key", step.get("key").and_then(Value::as_str)),
            _ => {
                return Err((
                    "invalid_request",
                    format!("steps[{index}] must use type or press_key."),
                ))
            }
        };
        let value = value
            .filter(|value| !value.is_empty() && (action == "type" || !value.trim().is_empty()))
            .ok_or((
                "invalid_request",
                format!("steps[{index}] requires non-empty {field}."),
            ))?;
        if step.len() != 2 {
            return Err((
                "invalid_request",
                format!("steps[{index}] accepts only action and {field}."),
            ));
        }
        if action == "type" {
            text_chars += value.chars().count();
            if text_chars > MAX_SEQUENCE_TEXT_CHARS {
                return Err((
                    "invalid_request",
                    "Sequence text is limited to 512 characters.".to_string(),
                ));
            }
            parsed.push(InputStep::Type(value.to_string()));
        } else {
            parsed.push(InputStep::PressKey(value.to_string()));
        }
    }
    Ok(parsed)
}

/// Keep an incomplete native edit from being crossed with another mutation.
///
/// Observation remains available so the caller can locate the fresh element.
/// Only the semantic completion action may mutate the desktop until the native
/// adapter confirms that it targeted the same element and value.
fn enforce_pending_action(state: &ServerState, method: &str) -> Result<(), (&'static str, String)> {
    if state.pending_action().is_some()
        && changes_window_state(method)
        && method != "invoke_element"
    {
        return Err((
            "pending_action",
            "The previous edit still requires semantic completion. Observe its window and use the invoke action on the matching element before starting another desktop-changing action."
                .to_string(),
        ));
    }
    Ok(())
}

fn attach_pending_action(result: &mut Value, pending: &PendingAction) {
    if let Some(object) = result.as_object_mut() {
        object.insert("pending_action".to_string(), pending.to_json());
        object.insert("next_action".to_string(), json!("invoke"));
    }
}

fn should_attach_pending_action(
    has_refreshed_observation: bool,
    pending_hwnd: isize,
    window_hwnd: isize,
) -> bool {
    has_refreshed_observation && pending_hwnd == window_hwnd
}

/// Combine the dispatch result with the post-action observation when the
/// original window remains available. The old observation is never restored:
/// only the new identifier in this response can authorize the next action.
fn accessibility_changed(previous: Option<[u8; 32]>, refreshed: Option<&Value>) -> Option<bool> {
    let current = refreshed?
        .get("accessibility")
        .and_then(accessibility_revision)?;
    Some(previous? != current)
}

fn action_receipt(
    mut result: Value,
    refreshed: Option<Value>,
    accessibility_changed: Option<bool>,
) -> Value {
    let Some(mut observation) = refreshed else {
        if let Some(object) = result.as_object_mut() {
            if object.remove("applied").is_some() {
                object.insert("dispatched".to_string(), json!(true));
            }
            object.insert("requires_observe".to_string(), json!(true));
            object.insert("next_action".to_string(), json!("list_windows"));
        }
        return result;
    };

    let Some(action) = result.as_object_mut() else {
        return observation;
    };
    if action.remove("applied").is_some() {
        action.insert("dispatched".to_string(), json!(true));
    }
    // Native actions used to point to an explicit observe call. The fresh
    // observation above has already completed that step; a pending semantic
    // completion, if any, is attached by the caller afterwards.
    action.remove("next_action");
    if let Some(response) = observation.as_object_mut() {
        response.extend(std::mem::take(action));
        if let Some(changed) = accessibility_changed {
            response.insert("accessibility_changed".to_string(), json!(changed));
        }
    }
    observation
}

/// Whether a mutation handed the foreground to another concrete surface.
///
/// Foreground actions are expected to leave their target active, so any other
/// active window is the action's result. A background semantic action only
/// hands off when the active window actually changed while it ran; an
/// unrelated window the user was already using must not become the target.
fn action_changed_active_window(
    target: &WindowInfo,
    active_before: Option<&WindowInfo>,
    active_after: Option<&WindowInfo>,
    foreground_expected: bool,
) -> bool {
    let Some(active_after) = active_after else {
        return false;
    };
    !same_surface(active_after, target)
        && (foreground_expected
            || active_before.is_none_or(|active_before| !same_surface(active_before, active_after)))
}

fn same_surface(left: &WindowInfo, right: &WindowInfo) -> bool {
    left.hwnd == right.hwnd && left.app_id == right.app_id
}

/// Preserve the action receipt while requiring a fresh observation of the
/// window that replaced its target.
fn action_handoff(mut result: Value, window: &WindowInfo) -> Value {
    if let Some(object) = result.as_object_mut() {
        if object.remove("applied").is_some() {
            object.insert("dispatched".to_string(), json!(true));
        }
        object.insert("requires_observe".to_string(), json!(true));
        object.insert("next_action".to_string(), json!("observe_window"));
        object.insert("window".to_string(), window.to_json());
    }
    result
}

fn observation<'a>(
    state: &'a ServerState,
    id: Option<&str>,
) -> Result<&'a Observation, (&'static str, String)> {
    let id = id.ok_or(("invalid_request", "observation_id is required.".to_string()))?;
    state.observations.get(id).ok_or((
        "stale_observation",
        "Observation is stale or already consumed; observe the window again and use its new observation_id."
            .to_string(),
    ))
}

#[cfg(target_os = "macos")]
fn observation_mut<'a>(
    state: &'a mut ServerState,
    id: Option<&str>,
) -> Result<&'a mut Observation, (&'static str, String)> {
    let id = id.ok_or(("invalid_request", "observation_id is required.".to_string()))?;
    state.observations.get_mut(id).ok_or((
        "stale_observation",
        "Observation is stale or already consumed; observe the window again and use its new observation_id."
            .to_string(),
    ))
}

/// Refuse an action that would disturb a machine a person is using.
///
/// Two conditions, checked together because they answer the same question: the
/// desktop must not be locked, and the keyboard and mouse must have been idle
/// long enough that the action is not racing someone.
///
/// There is deliberately no post-approval exemption. The host lets a newly
/// approved request settle before replying, but the guard remains authoritative
/// here: any input still inside the grace window is refused rather than waved
/// through by client-held state.
fn enforce_input_guard(state: &mut ServerState) -> Result<(), (&'static str, String)> {
    enforce_input_guard_with_measurements(state, desktop_locked(), last_input_age_ms())
}

fn enforce_input_guard_with_measurements(
    state: &mut ServerState,
    is_locked: bool,
    last_input_age: Option<u32>,
) -> Result<(), (&'static str, String)> {
    if is_locked {
        return Err((
            "desktop_locked",
            "The desktop is locked; ask the user to unlock it before continuing.".to_string(),
        ));
    }
    // An unreadable idle time is treated as idle: refusing every action because
    // the platform would not answer would strand the agent entirely.
    if last_input_age.is_some_and(|age| age < INPUT_GUARD_GRACE_MS) {
        // Human input makes every point-in-time observation on this
        // connection unsafe. Drop them before reporting the soft refusal so
        // recovery must create a fresh observation rather than replaying an
        // action whose outcome is unknown.
        state.invalidate_observations();
        return Err((
            "user_intervention",
            "Recent user input was detected; observe again before continuing.".to_string(),
        ));
    }
    Ok(())
}

/// Whether a method disturbs the desktop rather than only looking at it.
///
/// This is the set that takes a turn at the desktop and meets the input guard.
/// The question is not "does it synthesize input" but "does it change what the
/// machine does next". Launching and input both take focus away from whatever
/// the person was using, so both are guarded.
///
/// `launch_app` belongs here because starting an application brings
/// it to the front, and on macOS `open` activates one that is already running,
/// so a launch during another session's action moves the focus its keystrokes
/// were bound for.
fn changes_window_state(method: &str) -> bool {
    matches!(
        method,
        "click"
            | "scroll"
            | "drag"
            | "press_key"
            | "sequence"
            | "type_text"
            | "invoke_element"
            | "set_value"
            | "close_window"
            | "launch_app"
    )
}

/// Semantic AX/UIA mutations can run without pointer input, but only while the
/// exact accessibility surface the model observed is still current.
fn requires_stable_observation(method: &str) -> bool {
    matches!(method, "invoke_element" | "set_value" | "close_window")
}

/// Whether this platform must take foreground input for the method.
///
/// macOS accessibility actions can address a specific element without moving
/// the pointer or keyboard focus, so unrelated user activity does not block
/// `invoke_element` or `set_value`. Windows keeps the conservative active-
/// desktop guard for every mutation; this is an internal safety boundary, not
/// a product-facing platform promise.
fn requires_user_idle(method: &str, window: &WindowInfo) -> bool {
    #[cfg(windows)]
    {
        let _ = window;
        changes_window_state(method)
    }
    #[cfg(target_os = "macos")]
    {
        requires_user_idle_on_mac(method, target_is_frontmost(window))
    }
}

#[cfg(target_os = "macos")]
fn requires_user_idle_on_mac(method: &str, target_is_frontmost: bool) -> bool {
    matches!(
        method,
        "click" | "scroll" | "drag" | "press_key" | "sequence" | "type_text" | "launch_app"
    ) || (matches!(method, "invoke_element" | "set_value" | "close_window") && target_is_frontmost)
}

#[cfg(test)]
mod tests {
    use super::super::state::{ScreenshotTarget, WindowInfo};
    use super::*;
    use std::io::Cursor;

    fn observation(hwnd: isize) -> Observation {
        Observation {
            window: WindowInfo {
                hwnd,
                #[cfg(target_os = "macos")]
                owner_pid: 1,
                app_id: "app:test".to_string(),
                display_name: "Test".to_string(),
                title: String::new(),
                class_name: String::new(),
            },
            window_bounds: [0, 0, 100, 100],
            screenshots: std::collections::HashMap::from([(
                "screenshot-1".to_string(),
                ScreenshotTarget {
                    hwnd,
                    bounds: [0, 0, 100, 100],
                    display_width: 100,
                    display_height: 100,
                },
            )]),
            #[cfg(windows)]
            input_hwnd: hwnd,
            accessibility_revision: None,
            #[cfg(target_os = "macos")]
            transient_text_ready: false,
            elements: Default::default(),
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn transient_text_capability_is_observation_bound_and_one_shot() {
        let mut state = ServerState::default();
        let mut observed = observation(1);
        observed.transient_text_ready = true;
        state.observations.insert("observed".to_string(), observed);
        state
            .observations
            .insert("other".to_string(), observation(1));

        assert!(!take_transient_text_ready(&mut state, Some("other")).unwrap());
        assert!(take_transient_text_ready(&mut state, Some("observed")).unwrap());
        assert!(!take_transient_text_ready(&mut state, Some("observed")).unwrap());
    }

    #[test]
    fn only_explicit_text_edit_invocation_requests_an_editor() {
        let ordinary = json!({"element_id": "ax-1"});
        let text_edit = json!({
            "element_id": "ax-1",
            "expects_text_input": true,
        });

        assert!(!requests_text_edit(
            "invoke_element",
            ordinary.as_object().unwrap(),
        ));
        assert!(!requests_text_edit("click", text_edit.as_object().unwrap()));
        assert!(requests_text_edit(
            "invoke_element",
            text_edit.as_object().unwrap(),
        ));
    }

    #[cfg(windows)]
    #[test]
    fn windows_rejects_text_edit_before_resolving_an_observation() {
        let message = json!({
            "protocol_version": PROTOCOL_VERSION,
            "method": "invoke_element",
            "params": {
                "element_id": "uia-1",
                "expects_text_input": true,
            },
        });
        let mut connection = Cursor::new(Vec::new());

        let error = dispatch_request(&mut connection, &mut ServerState::default(), &message)
            .expect_err("Windows must not treat an invoke as a text editor");

        assert_eq!(error.0, "unsupported_operation");
    }

    #[test]
    fn input_sequence_contract_is_bounded_and_keyboard_only() {
        let value = json!({
            "steps": [
                {"action": "type", "text": "INV-001"},
                {"action": "press_key", "key": "TAB"},
            ]
        });
        let steps = parse_input_steps(value.as_object().unwrap()).unwrap();

        assert_eq!(
            steps,
            vec![
                InputStep::Type("INV-001".to_string()),
                InputStep::PressKey("TAB".to_string()),
            ]
        );

        for invalid in [
            json!({"steps": []}),
            json!({"steps": [{"action": "click", "x": 1, "y": 1}]}),
            json!({"steps": [{"action": "type", "text": "x", "extra": true}]}),
            json!({"steps": [{"action": "type", "text": "x".repeat(513)}]}),
        ] {
            assert!(parse_input_steps(invalid.as_object().unwrap()).is_err());
        }
    }

    #[test]
    fn an_action_without_a_refresh_requires_window_discovery() {
        let result = action_receipt(json!({"applied": true}), None, None);

        assert_eq!(result.get("dispatched"), Some(&json!(true)));
        assert_eq!(result.get("requires_observe"), Some(&json!(true)));
        assert_eq!(result.get("next_action"), Some(&json!("list_windows")));
    }

    #[test]
    fn a_foreground_action_hands_off_to_the_active_window() {
        let target = observation(1).window;
        let active = observation(2).window;

        assert!(action_changed_active_window(
            &target,
            Some(&active),
            Some(&active),
            true,
        ));
        let result = action_handoff(json!({"applied": true}), &active);
        assert_eq!(result.get("dispatched"), Some(&json!(true)));
        assert_eq!(result.get("requires_observe"), Some(&json!(true)));
        assert_eq!(result.get("next_action"), Some(&json!("observe_window")));
        assert_eq!(result["window"]["id"], json!("2"));
    }

    #[test]
    fn a_background_action_ignores_an_unchanged_foreground_window() {
        let target = observation(1).window;
        let active = observation(2).window;

        assert!(!action_changed_active_window(
            &target,
            Some(&active),
            Some(&active),
            false,
        ));
        assert!(action_changed_active_window(
            &target,
            Some(&active),
            Some(&observation(3).window),
            false,
        ));
    }

    #[test]
    fn refreshed_actions_report_accessibility_changes() {
        let before = accessibility_revision(&json!({
            "available": true,
            "elements": ["before"],
        }));
        let refreshed = json!({
            "accessibility": {
                "available": true,
                "elements": ["after"],
            },
        });

        assert_eq!(accessibility_changed(before, Some(&refreshed)), Some(true));
        assert_eq!(
            accessibility_changed(
                accessibility_revision(&refreshed["accessibility"]),
                Some(&refreshed),
            ),
            Some(false),
        );
    }

    #[test]
    fn unavailable_accessibility_has_no_change_claim() {
        let before = accessibility_revision(&json!({
            "available": true,
            "elements": ["before"],
        }));
        let refreshed = json!({
            "accessibility": {
                "available": false,
                "elements": [],
            },
        });

        assert_eq!(accessibility_changed(before, Some(&refreshed)), None);
    }

    #[test]
    fn pending_action_requires_a_refreshed_observation_of_its_window() {
        assert!(!should_attach_pending_action(false, 7, 7));
        assert!(!should_attach_pending_action(true, 7, 8));
        assert!(should_attach_pending_action(true, 7, 7));
    }

    #[test]
    fn user_intervention_invalidates_every_observation() {
        let mut state = ServerState::default();
        state
            .observations
            .insert("observation-1".to_string(), observation(1));
        state
            .observations
            .insert("observation-2".to_string(), observation(2));

        let error = enforce_input_guard_with_measurements(&mut state, false, Some(0))
            .expect_err("recent input must be refused");

        assert_eq!(error.0, "user_intervention");
        assert!(state.observations.is_empty());
    }

    #[test]
    fn an_idle_desktop_preserves_observations() {
        let mut state = ServerState::default();
        state
            .observations
            .insert("observation-1".to_string(), observation(1));

        enforce_input_guard_with_measurements(&mut state, false, Some(INPUT_GUARD_GRACE_MS))
            .expect("input at the grace boundary is idle");

        assert_eq!(state.observations.len(), 1);
    }

    #[test]
    fn user_intervention_refuses_only_the_current_action() {
        let mut state = ServerState::default();
        enforce_input_guard_with_measurements(&mut state, false, Some(0))
            .expect_err("recent input must refuse the action");

        enforce_input_guard_with_measurements(&mut state, false, Some(INPUT_GUARD_GRACE_MS))
            .expect("a later action may proceed after a fresh observation");
    }

    /// Spelled out rather than derived, so adding an action that reaches into
    /// the desktop cannot pass the guard by being forgotten: the new method has
    /// to be added here too, which is the moment to decide whether it belongs.
    #[test]
    fn every_action_that_disturbs_the_desktop_is_guarded() {
        for method in [
            "click",
            "scroll",
            "drag",
            "press_key",
            "sequence",
            "type_text",
            "invoke_element",
            "set_value",
            "close_window",
        ] {
            assert!(changes_window_state(method), "{method} must be guarded");
        }
    }

    #[test]
    fn methods_that_only_look_are_not_guarded() {
        // Observing must keep working while someone is using the machine, and
        // must not be refused on a locked screen either.
        for method in ["observe_window", "list_apps", "list_windows", "end_turn"] {
            assert!(
                !changes_window_state(method),
                "{method} must not be treated as input"
            );
        }
    }

    #[test]
    fn starting_an_application_counts_as_disturbing_the_desktop() {
        // This assertion used to read the other way, arguing that a launch
        // synthesizes no input. That is true and beside the point: it brings an
        // application to the front, and on macOS `open` activates one already
        // running, so it can take the focus another session's keystrokes were
        // aimed at -- and it can do it to a person who is mid-sentence.
        assert!(changes_window_state("launch_app"));
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn semantic_actions_are_guarded_only_when_the_target_is_frontmost() {
        for method in ["invoke_element", "set_value", "close_window"] {
            assert!(changes_window_state(method));
            assert!(
                !requires_user_idle_on_mac(method, false),
                "{method} should remain addressable while the user works elsewhere"
            );
            assert!(
                requires_user_idle_on_mac(method, true),
                "{method} must not race a user in the target app"
            );
        }
        for method in [
            "click",
            "drag",
            "type_text",
            "press_key",
            "sequence",
            "launch_app",
        ] {
            assert!(
                requires_user_idle_on_mac(method, false),
                "{method} must guard its foreground input"
            );
        }
    }

    /// Taken by any test that touches the desktop turn.
    ///
    /// The turn is process-global, and the test harness runs tests on parallel
    /// threads, so two of them contending for it would make each other's timing
    /// assertions meaningless. Passing without this is luck, not isolation.
    static ONE_AT_A_TIME: Mutex<()> = Mutex::new(());

    #[test]
    fn a_second_session_is_refused_rather_than_queued() {
        let _serial = ONE_AT_A_TIME
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let first = take_desktop().expect("first turn");

        let (code, _message) = take_desktop().expect_err("the second must be refused");
        assert_eq!(code, "desktop_busy");

        drop(first);
        assert!(
            take_desktop().is_ok(),
            "the desktop should be available once the first turn ends"
        );
    }

    #[test]
    fn being_refused_does_not_park_the_thread() {
        // The refusal has to be immediate, not a wait that gives up. A thread
        // parked here is not reading its connection, so a stop could not reach
        // it, and it would wake up later to inject input the user had already
        // asked to stop -- the helper knows nothing of stops, so it must not
        // hold work that a stop needs to cancel.
        let _serial = ONE_AT_A_TIME
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let _first = take_desktop().expect("first turn");

        let started = std::time::Instant::now();
        assert!(take_desktop().is_err());
        assert!(
            started.elapsed() < std::time::Duration::from_millis(100),
            "refusal took {:?}, which means it waited",
            started.elapsed()
        );
    }

    #[test]
    fn the_desktop_is_released_even_if_an_action_panics() {
        let _serial = ONE_AT_A_TIME
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let panicked = std::thread::spawn(|| {
            let _turn = take_desktop().expect("turn");
            panic!("an action failed");
        })
        .join();
        assert!(panicked.is_err(), "the thread should have panicked");

        // If the release depended on the happy path, the desktop would stay
        // taken and every later action would be refused.
        assert!(
            take_desktop().is_ok(),
            "a panicked action must not strand the desktop as taken"
        );
    }
}