Skip to main content

aios_spec/
context.rs

1//! Context window and model input types.
2//!
3//! Sanitized events are aggregated into `StructuredContext`. Cloud and local
4//! model backends may also receive privacy-preserving behavior memory through
5//! `ModelInput`.
6
7use serde::{Deserialize, Serialize};
8
9use crate::event::{
10    ExtensionCategory, LocationType, NetworkType, RingerMode, SemanticHint, SourceTier,
11};
12use crate::sanitized::SanitizedEvent;
13
14/// Sanitized context within a time window.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct StructuredContext {
17    pub window_id: String,
18    pub window_start_ms: i64,
19    pub window_end_ms: i64,
20    pub duration_secs: u32,
21    pub events: Vec<SanitizedEvent>,
22    pub summary: ContextSummary,
23}
24
25/// Complete input made available to model backends.
26///
27/// `current_context` remains the only required field. The other fields are
28/// derived after the privacy air-gap from prior sanitized windows and audit
29/// records, so raw notification text, file paths, and other local PII still do
30/// not cross the model boundary.
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct ModelInput {
33    pub current_context: StructuredContext,
34    pub behavior_profile: UserBehaviorProfile,
35    pub recent_feedback: Vec<RecentDecisionRecord>,
36}
37
38impl ModelInput {
39    pub fn current_only(current_context: StructuredContext) -> Self {
40        Self {
41            current_context,
42            behavior_profile: UserBehaviorProfile::default(),
43            recent_feedback: Vec::new(),
44        }
45    }
46}
47
48/// Rolling, privacy-preserving summary of observed user behavior.
49#[derive(Debug, Clone, Default, Serialize, Deserialize)]
50pub struct UserBehaviorProfile {
51    /// Optional pseudonymous user identifier. Used by personalized models such
52    /// as `PredictiveLocalBackend` to key per-user transition tables. This is
53    /// intentionally optional: older serialized inputs and contexts where the
54    /// collector has not yet supplied a user id deserialize to `None`, and the
55    /// backend falls back to global/non-personalized signals.
56    pub user_id: Option<String>,
57    pub summary: String,
58    pub observation_windows: u32,
59    pub frequent_foreground_apps: Vec<(String, u32)>,
60    pub frequent_notifying_apps: Vec<(String, u32)>,
61    pub frequent_semantic_hints: Vec<(SemanticHint, u32)>,
62    pub action_successes: Vec<(String, u32)>,
63    pub action_denials: Vec<(String, u32)>,
64    pub action_failures: Vec<(String, u32)>,
65    pub last_updated_window_id: Option<String>,
66}
67
68/// Detailed but bounded record of recent model decisions and local outcomes.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct RecentDecisionRecord {
71    pub window_id: String,
72    pub window_start_ms: i64,
73    pub window_end_ms: i64,
74    pub foreground_apps: Vec<String>,
75    pub notified_apps: Vec<String>,
76    pub semantic_hints: Vec<SemanticHint>,
77    pub route: String,
78    pub model: String,
79    pub intent_count: u32,
80    pub rationale_tags: Vec<String>,
81    pub backend_error: Option<String>,
82    pub action_outcomes: Vec<ActionFeedbackRecord>,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct ActionFeedbackRecord {
87    pub action_type: String,
88    pub target: Option<String>,
89    pub terminal: String,
90    pub correctness: FeedbackCorrectness,
91    pub correctness_evidence: String,
92    pub denial_reason: Option<String>,
93    pub error: Option<String>,
94    pub outcome_summary: Option<String>,
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
98pub enum FeedbackCorrectness {
99    LikelyCorrect,
100    PredictionHit,
101    PredictionMiss,
102    PolicyRejected,
103    ExecutionFailed,
104    NeutralNoOp,
105    Unknown,
106}
107
108/// Aggregated summary of a context window.
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct ContextSummary {
111    pub foreground_apps: Vec<String>,
112    pub notified_apps: Vec<String>,
113    pub all_semantic_hints: Vec<SemanticHint>,
114    pub file_activity: Vec<(ExtensionCategory, u32)>,
115    pub latest_system_status: Option<SystemStatusSnapshot>,
116    pub source_tier: SourceTier,
117}
118
119/// Latest system status snapshot in a window.
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct SystemStatusSnapshot {
122    pub battery_pct: Option<u8>,
123    pub is_charging: bool,
124    pub network: NetworkType,
125    pub ringer_mode: RingerMode,
126    pub location_type: LocationType,
127    pub headphone_connected: bool,
128}