velox-engine 0.4.0

Velox core UI engine: arena, pod state, 1D fixed-point layout solver, damage tracker, SDF scanline rasterizer, and cross-platform GPU/software backends
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
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
//! AI-Native UI 语义自动化控制协议(MCP JSON-RPC 2.0)与确定性时间旅行事件录像回放(`.vlxrec`)。
//!
//! 零外部 `serde` 依赖,直接复用 [`crate::a11y`] 与 [`crate::platform::a11y_bridge`]:
//! - **结构化 UI 语义快照(`snapshot_json`)**:任何 Velox 应用零代码即可向 AI Agent / E2E 测试框架暴露
//!   完整语义节点树(id / role / bounds / label / value / focusable / checked / children);
//! - **原生 MCP 协议处理(`handle_mcp_request`)**:内置 `initialize`、`tools/list`、`tools/call`
//!   (`velox_snapshot_tree`、`velox_click`、`velox_click_label`、`velox_fill`、`velox_scroll`、`velox_wait_idle`);
//! - **确定性事件录像与回放(`EventRecorder` / `EventReplayer` / `.vlxrec`)**:记录 `InputEvent` + 虚拟时钟 `dt_ms`
//!   + 帧状态校验和,实现线上 Bug 几十 KB 录像文件一帧不差复现。

use std::fmt::Write as _;
use std::format;
use std::string::String;
use std::vec::Vec;

use crate::a11y::A11yRole;
use crate::engine::VeloxEngine;
use crate::platform::a11y_bridge::{AccessKitActionRequest, AccessKitBridge, AccessKitNodeData};
use crate::pod::{InputEvent, KeyCode};

/// 语义节点查询条件。
#[derive(Clone, Debug, Default)]
pub struct SemanticQuery {
    pub id: Option<u32>,
    pub role: Option<A11yRole>,
    pub label_contains: Option<String>,
    pub only_focusable: bool,
}

/// AI Agent 或自动化测试可下发的结构化 UI 语义动作。
#[derive(Clone, Debug)]
pub enum SemanticAction {
    SnapshotTree,
    QueryNodes(SemanticQuery),
    ClickNode(u32),
    ClickByLabel(String),
    FillNode { target_id: u32, text: String },
    ScrollNode { target_id: u32, delta_y: i16 },
    PressKey(KeyCode),
    WaitIdle { max_frames: u16, dt_ms: u16 },
}

/// 语义动作执行回执。
#[derive(Clone, Debug)]
pub struct AutomationResult {
    pub ok: bool,
    pub message: String,
    pub payload_json: String,
    pub frames_stepped: u16,
}

fn role_str(role: A11yRole) -> &'static str {
    match role {
        A11yRole::Window => "window",
        A11yRole::Container => "container",
        A11yRole::Button => "button",
        A11yRole::TextInput => "text_input",
        A11yRole::Slider => "slider",
        A11yRole::ScrollRegion => "scroll_region",
        A11yRole::Text => "text",
        A11yRole::Image => "image",
    }
}

fn push_json_escaped(out: &mut String, s: &str) {
    out.push('"');
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => {
                let _ = write!(out, "\\u{:04x}", c as u32);
            }
            c => out.push(c),
        }
    }
    out.push('"');
}

fn encode_node_json(out: &mut String, n: &AccessKitNodeData) {
    let _ = write!(
        out,
        "{{\"id\":{},\"role\":\"{}\",\"bounds\":[{},{},{},{}],\"focusable\":{},\"focused\":{},\"disabled\":{}",
        n.id,
        role_str(n.role),
        n.bounds.x,
        n.bounds.y,
        n.bounds.w,
        n.bounds.h,
        n.focusable,
        n.focused,
        n.disabled
    );
    if let Some(lbl) = &n.label {
        out.push_str(",\"label\":");
        push_json_escaped(out, lbl);
    }
    if let Some(val) = &n.value {
        out.push_str(",\"value\":");
        push_json_escaped(out, val);
    }
    if let Some(chk) = n.checked {
        let _ = write!(out, ",\"checked\":{chk}");
    }
    out.push_str(",\"children\":[");
    for (i, cid) in n.children.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        let _ = write!(out, "{cid}");
    }
    out.push_str("]}");
}

/// 面向 AI Agent 与 Playwright 级 E2E 自动化测试的统一会话控制器。
#[derive(Default)]
pub struct AutomationSession {
    bridge: AccessKitBridge,
}

impl AutomationSession {
    pub fn new() -> Self {
        Self {
            bridge: AccessKitBridge::new(),
        }
    }

    /// 采集当前 [`VeloxEngine`] 的全部无障碍语义节点列表。
    pub fn collect_nodes(&mut self, engine: &VeloxEngine) -> Vec<AccessKitNodeData> {
        let mut fresh = AccessKitBridge::new();
        let _ = self.bridge.sync_engine(engine);
        fresh
            .sync_engine(engine)
            .map(|u| u.updated_nodes)
            .unwrap_or_default()
    }

    /// 按查询条件检索匹配的语义节点。
    pub fn query_nodes(&mut self, engine: &VeloxEngine, q: &SemanticQuery) -> Vec<AccessKitNodeData> {
        let needle = q.label_contains.as_deref().map(|s| s.to_ascii_lowercase());
        self.collect_nodes(engine)
            .into_iter()
            .filter(|n| {
                if let Some(id) = q.id {
                    if n.id != id {
                        return false;
                    }
                }
                if let Some(role) = q.role {
                    if n.role != role {
                        return false;
                    }
                }
                if q.only_focusable && !n.focusable {
                    return false;
                }
                if let Some(ref sub) = needle {
                    let matched = n
                        .label
                        .as_deref()
                        .is_some_and(|l| l.to_ascii_lowercase().contains(sub.as_str()))
                        || n.value
                            .as_deref()
                            .is_some_and(|v| v.to_ascii_lowercase().contains(sub.as_str()));
                    if !matched {
                        return false;
                    }
                }
                true
            })
            .collect()
    }

    /// 导出结构化 JSON 语义树快照(可直接喂给 LLM / MCP Client)。
    pub fn snapshot_json(&mut self, engine: &VeloxEngine) -> String {
        let nodes = self.collect_nodes(engine);
        let vp = engine.viewport_region();
        let focus_id = nodes.iter().find(|n| n.focused).map(|n| n.id).unwrap_or(0);
        let mut out = String::with_capacity(nodes.len() * 96 + 64);
        let _ = write!(
            out,
            "{{\"viewport\":[{},{},{},{}],\"focus_id\":{},\"node_count\":{},\"nodes\":[",
            vp.x,
            vp.y,
            vp.w,
            vp.h,
            focus_id,
            nodes.len()
        );
        for (i, n) in nodes.iter().enumerate() {
            if i > 0 {
                out.push(',');
            }
            encode_node_json(&mut out, n);
        }
        out.push_str("]}");
        out
    }

    /// 执行结构化语义动作并自动步进引擎帧循环。
    pub fn execute(&mut self, engine: &mut VeloxEngine, action: SemanticAction) -> AutomationResult {
        match action {
            SemanticAction::SnapshotTree => AutomationResult {
                ok: true,
                message: String::from("snapshot captured"),
                payload_json: self.snapshot_json(engine),
                frames_stepped: 0,
            },
            SemanticAction::QueryNodes(q) => {
                let matched = self.query_nodes(engine, &q);
                let mut out = String::from("[");
                for (i, n) in matched.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    encode_node_json(&mut out, n);
                }
                out.push(']');
                AutomationResult {
                    ok: true,
                    message: format!("matched {} node(s)", matched.len()),
                    payload_json: out,
                    frames_stepped: 0,
                }
            }
            SemanticAction::ClickNode(target_id) => {
                let ok = self
                    .bridge
                    .apply_action_request(engine, AccessKitActionRequest::Click { target_id });
                let _ = engine.step_frame_dt(16);
                AutomationResult {
                    ok,
                    message: if ok {
                        format!("clicked node {target_id}")
                    } else {
                        format!("node {target_id} not clickable or not found")
                    },
                    payload_json: format!("{{\"clicked\":{ok},\"target_id\":{target_id}}}"),
                    frames_stepped: 1,
                }
            }
            SemanticAction::ClickByLabel(label) => {
                let hits = self.query_nodes(
                    engine,
                    &SemanticQuery {
                        label_contains: Some(label.clone()),
                        ..Default::default()
                    },
                );
                if let Some(target) = hits.iter().find(|n| n.focusable).or_else(|| hits.first()) {
                    let cx = target.bounds.x + target.bounds.w / 2;
                    let cy = target.bounds.y + target.bounds.h / 2;
                    let ok = engine.dispatch_click(cx, cy);
                    let _ = engine.step_frame_dt(16);
                    AutomationResult {
                        ok,
                        message: format!("clicked label '{}' (node #{})", label, target.id),
                        payload_json: format!("{{\"clicked\":{ok},\"target_id\":{}}}", target.id),
                        frames_stepped: 1,
                    }
                } else {
                    AutomationResult {
                        ok: false,
                        message: format!("no node matched label '{label}'"),
                        payload_json: String::from("{\"clicked\":false}"),
                        frames_stepped: 0,
                    }
                }
            }
            SemanticAction::FillNode { target_id, text } => {
                let ok = self.bridge.apply_action_request(
                    engine,
                    AccessKitActionRequest::SetValue {
                        target_id,
                        text: text.clone(),
                    },
                );
                let _ = engine.step_frame_dt(16);
                AutomationResult {
                    ok,
                    message: format!("filled node {target_id}"),
                    payload_json: format!("{{\"filled\":{ok},\"target_id\":{target_id}}}"),
                    frames_stepped: 1,
                }
            }
            SemanticAction::ScrollNode { target_id, delta_y } => {
                let ok = self.bridge.apply_action_request(
                    engine,
                    AccessKitActionRequest::Scroll { target_id, delta_y },
                );
                let _ = engine.step_frame_dt(16);
                AutomationResult {
                    ok,
                    message: format!("scrolled node {target_id} by {delta_y}"),
                    payload_json: format!("{{\"scrolled\":{ok},\"target_id\":{target_id}}}"),
                    frames_stepped: 1,
                }
            }
            SemanticAction::PressKey(key) => {
                let ok = engine.dispatch_event(InputEvent::KeyDown(key));
                let _ = engine.step_frame_dt(16);
                AutomationResult {
                    ok,
                    message: String::from("dispatched key"),
                    payload_json: format!("{{\"handled\":{ok}}}"),
                    frames_stepped: 1,
                }
            }
            SemanticAction::WaitIdle { max_frames, dt_ms } => {
                let mut stepped = 0u16;
                let step_dt = dt_ms.max(1);
                for _ in 0..max_frames.max(1) {
                    stepped += 1;
                    let dirty = engine.step_frame_dt(step_dt);
                    if dirty.is_empty() && !engine.needs_redraw() {
                        break;
                    }
                }
                let idle = !engine.needs_redraw();
                AutomationResult {
                    ok: idle,
                    message: format!("stepped {stepped} frame(s), idle={idle}"),
                    payload_json: format!("{{\"idle\":{idle},\"frames\":{stepped}}}"),
                    frames_stepped: stepped,
                }
            }
        }
    }

    /// 处理标准 **MCP (Model Context Protocol) JSON-RPC 2.0** 请求并返回 JSON 响应字符串。
    ///
    /// 内置支持方法:
    /// - `initialize`
    /// - `tools/list`
    /// - `tools/call`(`velox_snapshot_tree`、`velox_click`、`velox_click_label`、`velox_fill`、`velox_scroll`、`velox_wait_idle`)
    pub fn handle_mcp_request(&mut self, engine: &mut VeloxEngine, req_json: &str) -> String {
        let req_id = extract_json_raw_field(req_json, "id").unwrap_or_else(|| String::from("1"));
        let method = extract_json_string_field(req_json, "method").unwrap_or_default();

        match method.as_str() {
            "initialize" => format!(
                "{{\"jsonrpc\":\"2.0\",\"id\":{req_id},\"result\":{{\"protocolVersion\":\"2025-03-26\",\"capabilities\":{{\"tools\":{{}}}},\"serverInfo\":{{\"name\":\"velox-automation-mcp\",\"version\":\"0.4.0\"}}}}}}"
            ),
            "tools/list" => format!(
                "{{\"jsonrpc\":\"2.0\",\"id\":{req_id},\"result\":{{\"tools\":[{{\"name\":\"velox_snapshot_tree\",\"description\":\"Capture structured Velox UI accessibility/semantic tree as JSON\"}},{{\"name\":\"velox_click\",\"description\":\"Click a UI node by numeric id\"}},{{\"name\":\"velox_click_label\",\"description\":\"Find and click a UI node by label substring\"}},{{\"name\":\"velox_fill\",\"description\":\"Focus and type text into an input node by id\"}},{{\"name\":\"velox_scroll\",\"description\":\"Scroll a container node by delta_y\"}},{{\"name\":\"velox_wait_idle\",\"description\":\"Advance frames until all animations settle to 0% CPU idle\"}}]}}}}"
            ),
            "tools/call" => {
                let tool = extract_json_string_field(req_json, "name").unwrap_or_default();
                let res = match tool.as_str() {
                    "velox_snapshot_tree" => self.execute(engine, SemanticAction::SnapshotTree),
                    "velox_click" => {
                        let tid = extract_json_i64_field(req_json, "target_id").unwrap_or(0) as u32;
                        self.execute(engine, SemanticAction::ClickNode(tid))
                    }
                    "velox_click_label" => {
                        let lbl = extract_json_string_field(req_json, "label").unwrap_or_default();
                        self.execute(engine, SemanticAction::ClickByLabel(lbl))
                    }
                    "velox_fill" => {
                        let tid = extract_json_i64_field(req_json, "target_id").unwrap_or(0) as u32;
                        let txt = extract_json_string_field(req_json, "text").unwrap_or_default();
                        self.execute(
                            engine,
                            SemanticAction::FillNode {
                                target_id: tid,
                                text: txt,
                            },
                        )
                    }
                    "velox_scroll" => {
                        let tid = extract_json_i64_field(req_json, "target_id").unwrap_or(0) as u32;
                        let dy = extract_json_i64_field(req_json, "delta_y").unwrap_or(0) as i16;
                        self.execute(
                            engine,
                            SemanticAction::ScrollNode {
                                target_id: tid,
                                delta_y: dy,
                            },
                        )
                    }
                    "velox_wait_idle" => {
                        let max_f = extract_json_i64_field(req_json, "max_frames").unwrap_or(120) as u16;
                        self.execute(
                            engine,
                            SemanticAction::WaitIdle {
                                max_frames: max_f,
                                dt_ms: 16,
                            },
                        )
                    }
                    _ => AutomationResult {
                        ok: false,
                        message: format!("unknown tool '{tool}'"),
                        payload_json: String::from("{}"),
                        frames_stepped: 0,
                    },
                };
                let mut text_escaped = String::new();
                push_json_escaped(&mut text_escaped, &res.payload_json);
                format!(
                    "{{\"jsonrpc\":\"2.0\",\"id\":{req_id},\"result\":{{\"isError\":{},\"content\":[{{\"type\":\"text\",\"text\":{text_escaped}}}]}}}}",
                    !res.ok
                )
            }
            _ => format!(
                "{{\"jsonrpc\":\"2.0\",\"id\":{req_id},\"error\":{{\"code\":-32601,\"message\":\"Method not found\"}}}}"
            ),
        }
    }
}

fn find_json_field_value<'a>(json: &'a str, key: &str) -> Option<&'a str> {
    let needle = format!("\"{key}\"");
    let pos = json.find(&needle)?;
    let rest = &json[pos + needle.len()..];
    let colon = rest.find(':')?;
    Some(rest[colon + 1..].trim_start())
}

fn extract_json_string_field(json: &str, key: &str) -> Option<String> {
    let val = find_json_field_value(json, key)?;
    if !val.starts_with('"') {
        return None;
    }
    let mut out = String::new();
    let mut chars = val[1..].chars();
    while let Some(c) = chars.next() {
        match c {
            '"' => return Some(out),
            '\\' => {
                if let Some(esc) = chars.next() {
                    match esc {
                        'n' => out.push('\n'),
                        'r' => out.push('\r'),
                        't' => out.push('\t'),
                        '"' => out.push('"'),
                        '\\' => out.push('\\'),
                        other => out.push(other),
                    }
                }
            }
            other => out.push(other),
        }
    }
    None
}

fn extract_json_i64_field(json: &str, key: &str) -> Option<i64> {
    let val = find_json_field_value(json, key)?;
    let end = val
        .find(|c: char| !c.is_ascii_digit() && c != '-')
        .unwrap_or(val.len());
    val[..end].parse::<i64>().ok()
}

fn extract_json_raw_field(json: &str, key: &str) -> Option<String> {
    let val = find_json_field_value(json, key)?;
    if val.starts_with('"') {
        let s = extract_json_string_field(json, key)?;
        let mut out = String::new();
        push_json_escaped(&mut out, &s);
        Some(out)
    } else {
        let end = val
            .find(|c: char| matches!(c, ',' | '}' | ']' | ' ' | '\n' | '\r' | '\t'))
            .unwrap_or(val.len());
        Some(String::from(&val[..end]))
    }
}

// ============================================================================
// 确定性时间旅行事件录像与回放(`.vlxrec` 紧凑文本格式)
// ============================================================================

/// 可序列化录像的输入事件子集(涵盖鼠标、滚轮、缩放、文本、按键与时钟推进)。
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum RecordedEvent {
    Tick,
    PointerDown { x: i16, y: i16 },
    PointerMove { x: i16, y: i16 },
    PointerUp { x: i16, y: i16 },
    Scroll { x: i16, y: i16, delta_y: i16 },
    Pinch { x: i16, y: i16, delta_scale_x1000: i16 },
    TextChar(char),
    KeyCodeTag(u8),
}

impl RecordedEvent {
    pub fn from_input_event(ev: &InputEvent) -> Option<Self> {
        match *ev {
            InputEvent::PointerDown { x, y } => Some(Self::PointerDown { x, y }),
            InputEvent::PointerMove { x, y } => Some(Self::PointerMove { x, y }),
            InputEvent::PointerUp { x, y } => Some(Self::PointerUp { x, y }),
            InputEvent::Scroll { x, y, delta_y } => Some(Self::Scroll { x, y, delta_y }),
            InputEvent::Pinch {
                x,
                y,
                delta_scale_x1000,
            } => Some(Self::Pinch {
                x,
                y,
                delta_scale_x1000,
            }),
            InputEvent::TextCommit(ch) => Some(Self::TextChar(ch)),
            InputEvent::KeyDown(KeyCode::Char(ch)) => Some(Self::TextChar(ch)),
            InputEvent::KeyDown(k) => key_to_tag(k).map(Self::KeyCodeTag),
            _ => None,
        }
    }

    pub fn to_input_event(self) -> Option<InputEvent> {
        match self {
            Self::Tick => None,
            Self::PointerDown { x, y } => Some(InputEvent::PointerDown { x, y }),
            Self::PointerMove { x, y } => Some(InputEvent::PointerMove { x, y }),
            Self::PointerUp { x, y } => Some(InputEvent::PointerUp { x, y }),
            Self::Scroll { x, y, delta_y } => Some(InputEvent::Scroll { x, y, delta_y }),
            Self::Pinch {
                x,
                y,
                delta_scale_x1000,
            } => Some(InputEvent::Pinch {
                x,
                y,
                delta_scale_x1000,
            }),
            Self::TextChar(ch) => Some(InputEvent::KeyDown(KeyCode::Char(ch))),
            Self::KeyCodeTag(t) => tag_to_key(t).map(InputEvent::KeyDown),
        }
    }
}

fn key_to_tag(k: KeyCode) -> Option<u8> {
    match k {
        KeyCode::Enter => Some(1),
        KeyCode::Escape => Some(2),
        KeyCode::Backspace => Some(3),
        KeyCode::Delete => Some(4),
        KeyCode::Tab => Some(5),
        KeyCode::Up => Some(6),
        KeyCode::Down => Some(7),
        KeyCode::Left => Some(8),
        KeyCode::Right => Some(9),
        KeyCode::Space => Some(10),
        KeyCode::Home => Some(11),
        KeyCode::End => Some(12),
        _ => None,
    }
}

fn tag_to_key(t: u8) -> Option<KeyCode> {
    match t {
        1 => Some(KeyCode::Enter),
        2 => Some(KeyCode::Escape),
        3 => Some(KeyCode::Backspace),
        4 => Some(KeyCode::Delete),
        5 => Some(KeyCode::Tab),
        6 => Some(KeyCode::Up),
        7 => Some(KeyCode::Down),
        8 => Some(KeyCode::Left),
        9 => Some(KeyCode::Right),
        10 => Some(KeyCode::Space),
        11 => Some(KeyCode::Home),
        12 => Some(KeyCode::End),
        _ => None,
    }
}

/// 单帧录像记录条(含虚拟时间差与帧确定性哈希)。
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct RecordedStep {
    pub dt_ms: u16,
    pub event: RecordedEvent,
    pub state_checksum: u64,
}

/// 计算 [`VeloxEngine`] 当前帧的结构化确定性校验和(DrawOp 数量 + HitRegion 数量 + 焦点索引 + 布局边界摘要)。
pub fn compute_engine_frame_checksum(engine: &VeloxEngine) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    let mut mix = |v: u64| {
        h ^= v;
        h = h.wrapping_mul(0x0100_0000_01b3);
    };
    let ops = engine.cached_draw_ops();
    let hits = engine.cached_hit_regions();
    mix(ops.len() as u64);
    mix(hits.len() as u64);
    mix(engine.focused_index().unwrap_or(u16::MAX) as u64);
    for op in ops.iter().take(64) {
        mix(op.bounds.x as u16 as u64);
        mix(op.bounds.y as u16 as u64);
        mix(op.bounds.w as u16 as u64);
        mix(op.bounds.h as u16 as u64);
    }
    h
}

/// 确定性事件录像机(支持导出 `.vlxrec` 紧凑文本格式供线上 Bug 复现)。
#[derive(Clone, Debug, Default)]
pub struct EventRecorder {
    pub steps: Vec<RecordedStep>,
}

impl EventRecorder {
    pub fn new() -> Self {
        Self { steps: Vec::new() }
    }

    pub fn clear(&mut self) {
        self.steps.clear();
    }

    /// 录制一次事件派发 + 步进帧的完整确定性快照。
    pub fn record_dispatch(
        &mut self,
        engine: &mut VeloxEngine,
        dt_ms: u16,
        event: InputEvent,
    ) -> bool {
        let rec = RecordedEvent::from_input_event(&event).unwrap_or(RecordedEvent::Tick);
        let handled = engine.dispatch_event(event);
        let _ = engine.step_frame_dt(dt_ms);
        let checksum = compute_engine_frame_checksum(engine);
        self.steps.push(RecordedStep {
            dt_ms,
            event: rec,
            state_checksum: checksum,
        });
        handled
    }

    /// 仅录制一次虚拟时钟步进帧。
    pub fn record_tick(&mut self, engine: &mut VeloxEngine, dt_ms: u16) {
        let _ = engine.step_frame_dt(dt_ms);
        self.steps.push(RecordedStep {
            dt_ms,
            event: RecordedEvent::Tick,
            state_checksum: compute_engine_frame_checksum(engine),
        });
    }

    /// 序列化为人类可读且极紧凑的 `.vlxrec` 录像文本。
    pub fn to_vlxrec_text(&self) -> String {
        let mut out = String::from("VLXREC v1\n");
        for s in &self.steps {
            let _ = write!(out, "{} ", s.dt_ms);
            match s.event {
                RecordedEvent::Tick => out.push_str("TICK"),
                RecordedEvent::PointerDown { x, y } => {
                    let _ = write!(out, "PD {x} {y}");
                }
                RecordedEvent::PointerMove { x, y } => {
                    let _ = write!(out, "PM {x} {y}");
                }
                RecordedEvent::PointerUp { x, y } => {
                    let _ = write!(out, "PU {x} {y}");
                }
                RecordedEvent::Scroll { x, y, delta_y } => {
                    let _ = write!(out, "SC {x} {y} {delta_y}");
                }
                RecordedEvent::Pinch {
                    x,
                    y,
                    delta_scale_x1000,
                } => {
                    let _ = write!(out, "PN {x} {y} {delta_scale_x1000}");
                }
                RecordedEvent::TextChar(ch) => {
                    let _ = write!(out, "CH {}", ch as u32);
                }
                RecordedEvent::KeyCodeTag(tag) => {
                    let _ = write!(out, "KY {tag}");
                }
            }
            let _ = writeln!(out, " #{:016x}", s.state_checksum);
        }
        out
    }
}

/// 回放验证报告。
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct ReplayReport {
    pub total_steps: usize,
    pub checksum_matches: usize,
    pub checksum_mismatches: usize,
}

impl ReplayReport {
    #[inline]
    pub const fn is_deterministic(&self) -> bool {
        self.total_steps > 0 && self.checksum_mismatches == 0
    }
}

/// 确定性时间旅行事件回放器。
#[derive(Clone, Debug)]
pub struct EventReplayer {
    steps: Vec<RecordedStep>,
    cursor: usize,
}

impl EventReplayer {
    pub fn from_vlxrec_text(text: &str) -> Result<Self, &'static str> {
        let mut lines = text.lines();
        let header = lines.next().ok_or("empty vlxrec")?;
        if !header.trim().starts_with("VLXREC") {
            return Err("invalid vlxrec header");
        }
        let mut steps = Vec::new();
        for line in lines {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() < 3 {
                return Err("malformed vlxrec line");
            }
            let dt_ms: u16 = parts[0].parse().map_err(|_| "bad dt_ms")?;
            let checksum_hex = parts
                .last()
                .and_then(|s| s.strip_prefix('#'))
                .ok_or("missing checksum")?;
            let state_checksum = u64::from_str_radix(checksum_hex, 16).map_err(|_| "bad checksum")?;
            let event = match parts[1] {
                "TICK" => RecordedEvent::Tick,
                "PD" if parts.len() >= 5 => RecordedEvent::PointerDown {
                    x: parts[2].parse().map_err(|_| "bad x")?,
                    y: parts[3].parse().map_err(|_| "bad y")?,
                },
                "PM" if parts.len() >= 5 => RecordedEvent::PointerMove {
                    x: parts[2].parse().map_err(|_| "bad x")?,
                    y: parts[3].parse().map_err(|_| "bad y")?,
                },
                "PU" if parts.len() >= 5 => RecordedEvent::PointerUp {
                    x: parts[2].parse().map_err(|_| "bad x")?,
                    y: parts[3].parse().map_err(|_| "bad y")?,
                },
                "SC" if parts.len() >= 6 => RecordedEvent::Scroll {
                    x: parts[2].parse().map_err(|_| "bad x")?,
                    y: parts[3].parse().map_err(|_| "bad y")?,
                    delta_y: parts[4].parse().map_err(|_| "bad dy")?,
                },
                "PN" if parts.len() >= 6 => RecordedEvent::Pinch {
                    x: parts[2].parse().map_err(|_| "bad x")?,
                    y: parts[3].parse().map_err(|_| "bad y")?,
                    delta_scale_x1000: parts[4].parse().map_err(|_| "bad scale")?,
                },
                "CH" if parts.len() >= 4 => {
                    let cp: u32 = parts[2].parse().map_err(|_| "bad char")?;
                    RecordedEvent::TextChar(char::from_u32(cp).ok_or("invalid unicode")?)
                }
                "KY" if parts.len() >= 4 => {
                    let tag: u8 = parts[2].parse().map_err(|_| "bad key tag")?;
                    RecordedEvent::KeyCodeTag(tag)
                }
                _ => return Err("unknown vlxrec opcode"),
            };
            steps.push(RecordedStep {
                dt_ms,
                event,
                state_checksum,
            });
        }
        Ok(Self { steps, cursor: 0 })
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.steps.len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.steps.is_empty()
    }

    #[inline]
    pub fn cursor(&self) -> usize {
        self.cursor
    }

    /// 逐帧推进回放 1 步,返回 `Some(checksum_matched)`;结束时返回 `None`。
    pub fn step_next(&mut self, engine: &mut VeloxEngine) -> Option<bool> {
        let step = *self.steps.get(self.cursor)?;
        self.cursor += 1;
        if let Some(ev) = step.event.to_input_event() {
            let _ = engine.dispatch_event(ev);
        }
        let _ = engine.step_frame_dt(step.dt_ms);
        let actual = compute_engine_frame_checksum(engine);
        Some(actual == step.state_checksum)
    }

    /// 一次性回放全部剩余步骤并返回确定性验证报告。
    pub fn replay_all(&mut self, engine: &mut VeloxEngine) -> ReplayReport {
        let mut report = ReplayReport::default();
        while let Some(matched) = self.step_next(engine) {
            report.total_steps += 1;
            if matched {
                report.checksum_matches += 1;
            } else {
                report.checksum_mismatches += 1;
            }
        }
        report
    }
}