Skip to main content

aios_spec/
trace.rs

1//! 确定性 Trace — "how we prove correctness"
2//!
3//! Golden Trace 是 DiPECS 的确定性保证机制:
4//! 在相同输入序列下, 系统的脱敏输出和策略决策必须一致。
5
6use serde::{Deserialize, Serialize};
7
8use crate::event::RawEvent;
9use crate::intent::IntentBatch;
10use crate::SanitizedEvent;
11
12/// 一条 Golden Trace
13///
14/// 记录特定时间窗口内:
15/// 1. 输入序列 (RawEvent)
16/// 2. 期望的脱敏输出 (SanitizedEvent)
17/// 3. 期望的 LLM 返回 (IntentBatch)
18/// 4. 期望的执行动作 (ExecutedAction)
19///
20/// Golden Trace 文件存储在 `data/traces/` 目录下。
21/// 超过 1MB 的 trace 文件使用 Git LFS 托管。
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct GoldenTrace {
24    /// Trace 唯一 ID
25    pub trace_id: String,
26    /// 窗口起始时间 (epoch ms)
27    pub window_start_ms: i64,
28    /// 窗口结束时间 (epoch ms)
29    pub window_end_ms: i64,
30    /// 原始输入事件序列
31    pub raw_events: Vec<RawEvent>,
32    /// 期望的脱敏输出
33    pub expected_sanitized: Vec<SanitizedEvent>,
34    /// 期望的云端返回
35    pub expected_intents: IntentBatch,
36    /// 期望的本地执行动作
37    pub expected_actions: Vec<ExecutedAction>,
38}
39
40/// 已执行的动作记录
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct ExecutedAction {
43    /// 动作类型
44    pub action_type: String,
45    /// 目标标识
46    pub target: Option<String>,
47    /// 执行时间 (epoch ms)
48    pub executed_at_ms: i64,
49    /// 执行是否成功
50    pub success: bool,
51    /// 失败原因 (如有)
52    pub error_reason: Option<String>,
53}
54
55/// 回放验证结果
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct ReplayResult {
58    /// 对应的 trace ID
59    pub trace_id: String,
60    /// 脱敏输出是否完全一致
61    pub sanitization_match: bool,
62    /// 不一致的 SanitizedEvent 索引
63    pub sanitization_divergences: Vec<usize>,
64    /// 策略决策是否完全一致
65    pub policy_match: bool,
66    /// 不一致的策略决策描述
67    pub policy_divergences: Vec<String>,
68    /// 执行结果是否完全一致
69    pub execution_match: bool,
70    /// 不一致的 ExecutedAction 索引
71    pub execution_divergences: Vec<usize>,
72}
73
74impl ReplayResult {
75    /// True iff sanitize + policy + execute all matched. The single boolean
76    /// that golden tests should pin against.
77    pub fn all_match(&self) -> bool {
78        self.sanitization_match && self.policy_match && self.execution_match
79    }
80}