1use std::collections::{BTreeMap, VecDeque};
4use std::fs::File;
5use std::io::{BufReader, BufWriter};
6use std::path::{Path, PathBuf};
7
8use aios_spec::governance::{ActionState, AuditRecord};
9use aios_spec::{
10 ActionFeedbackRecord, ActionType, DecisionBackendResult, FeedbackCorrectness, ModelInput,
11 RecentDecisionRecord, SemanticHint, StructuredContext, UserBehaviorProfile,
12};
13use serde::{Deserialize, Serialize};
14
15const DEFAULT_RECENT_LIMIT: usize = 5;
16const DEFAULT_TOP_LIMIT: usize = 8;
17const DEFAULT_MOMENTUM_DECAY_MILLI: u16 = 900;
18const DEFAULT_PREWARM_EFFECT_WINDOWS: u8 = 3;
19const MIN_RECENT_LIMIT: usize = 1;
20const MAX_RECENT_LIMIT: usize = 50;
21const MIN_TOP_LIMIT: usize = 1;
22const MAX_TOP_LIMIT: usize = 32;
23const MIN_MOMENTUM_DECAY_MILLI: u16 = 0;
24const MAX_MOMENTUM_DECAY_MILLI: u16 = 999;
25const MIN_PREWARM_EFFECT_WINDOWS: u8 = 1;
26const MAX_PREWARM_EFFECT_WINDOWS: u8 = 20;
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct ModelMemoryConfig {
30 pub recent_limit: usize,
31 pub top_limit: usize,
32 pub momentum_decay_milli: u16,
33 pub prewarm_effect_windows: u8,
34 pub persist_path: Option<PathBuf>,
35}
36
37impl Default for ModelMemoryConfig {
38 fn default() -> Self {
39 Self {
40 recent_limit: DEFAULT_RECENT_LIMIT,
41 top_limit: DEFAULT_TOP_LIMIT,
42 momentum_decay_milli: DEFAULT_MOMENTUM_DECAY_MILLI,
43 prewarm_effect_windows: DEFAULT_PREWARM_EFFECT_WINDOWS,
44 persist_path: None,
45 }
46 }
47}
48
49impl ModelMemoryConfig {
50 pub fn from_env() -> Self {
51 let recent_limit = read_usize_env("DIPECS_MODEL_MEMORY_RECENT_WINDOWS")
52 .unwrap_or(DEFAULT_RECENT_LIMIT)
53 .clamp(MIN_RECENT_LIMIT, MAX_RECENT_LIMIT);
54 let top_limit = read_usize_env("DIPECS_MODEL_MEMORY_TOP_K")
55 .unwrap_or(DEFAULT_TOP_LIMIT)
56 .clamp(MIN_TOP_LIMIT, MAX_TOP_LIMIT);
57 let momentum_decay_milli = read_u16_env("DIPECS_MODEL_MEMORY_MOMENTUM_DECAY_MILLI")
58 .unwrap_or(DEFAULT_MOMENTUM_DECAY_MILLI)
59 .clamp(MIN_MOMENTUM_DECAY_MILLI, MAX_MOMENTUM_DECAY_MILLI);
60 let prewarm_effect_windows = read_u8_env("DIPECS_PREWARM_EFFECT_WINDOWS")
61 .unwrap_or(DEFAULT_PREWARM_EFFECT_WINDOWS)
62 .clamp(MIN_PREWARM_EFFECT_WINDOWS, MAX_PREWARM_EFFECT_WINDOWS);
63 let persist_path = std::env::var("DIPECS_MODEL_MEMORY_PATH")
64 .ok()
65 .filter(|value| !value.trim().is_empty())
66 .map(PathBuf::from);
67 Self {
68 recent_limit,
69 top_limit,
70 momentum_decay_milli,
71 prewarm_effect_windows,
72 persist_path,
73 }
74 }
75}
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct ModelMemoryStore {
78 recent_limit: usize,
79 top_limit: usize,
80 #[serde(default = "default_momentum_decay_milli")]
81 momentum_decay_milli: u16,
82 #[serde(default = "default_prewarm_effect_windows")]
83 prewarm_effect_windows: u8,
84 llm_summary: Option<String>,
85 observation_windows: u32,
86 foreground_counts: BTreeMap<String, u32>,
87 notifying_counts: BTreeMap<String, u32>,
88 semantic_hint_counts: BTreeMap<String, u32>,
89 action_success_counts: BTreeMap<String, u32>,
90 action_denial_counts: BTreeMap<String, u32>,
91 action_failure_counts: BTreeMap<String, u32>,
92 #[serde(default)]
93 foreground_momentum: BTreeMap<String, f64>,
94 #[serde(default)]
95 notifying_momentum: BTreeMap<String, f64>,
96 #[serde(default)]
97 semantic_hint_momentum: BTreeMap<String, f64>,
98 #[serde(default)]
99 action_success_momentum: BTreeMap<String, f64>,
100 #[serde(default)]
101 action_denial_momentum: BTreeMap<String, f64>,
102 #[serde(default)]
103 action_failure_momentum: BTreeMap<String, f64>,
104 #[serde(default)]
105 prewarm_hit_counts: BTreeMap<String, u32>,
106 #[serde(default)]
107 prewarm_miss_counts: BTreeMap<String, u32>,
108 #[serde(default)]
109 prewarm_hit_momentum: BTreeMap<String, f64>,
110 #[serde(default)]
111 prewarm_miss_momentum: BTreeMap<String, f64>,
112 #[serde(default)]
113 pending_prewarms: Vec<PendingPrewarm>,
114 last_updated_window_id: Option<String>,
115 recent: VecDeque<RecentDecisionRecord>,
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
119struct PendingPrewarm {
120 window_id: String,
121 target: String,
122 package: String,
123 windows_observed: u8,
124}
125
126impl Default for ModelMemoryStore {
127 fn default() -> Self {
128 Self::with_config(&ModelMemoryConfig::default())
129 }
130}
131
132impl ModelMemoryStore {
133 pub fn new(recent_limit: usize) -> Self {
134 Self::with_config(&ModelMemoryConfig {
135 recent_limit,
136 ..ModelMemoryConfig::default()
137 })
138 }
139
140 pub fn with_config(config: &ModelMemoryConfig) -> Self {
141 Self {
142 recent_limit: config
143 .recent_limit
144 .clamp(MIN_RECENT_LIMIT, MAX_RECENT_LIMIT),
145 top_limit: config.top_limit.clamp(MIN_TOP_LIMIT, MAX_TOP_LIMIT),
146 momentum_decay_milli: config
147 .momentum_decay_milli
148 .clamp(MIN_MOMENTUM_DECAY_MILLI, MAX_MOMENTUM_DECAY_MILLI),
149 prewarm_effect_windows: config
150 .prewarm_effect_windows
151 .clamp(MIN_PREWARM_EFFECT_WINDOWS, MAX_PREWARM_EFFECT_WINDOWS),
152 llm_summary: None,
153 observation_windows: 0,
154 foreground_counts: BTreeMap::new(),
155 notifying_counts: BTreeMap::new(),
156 semantic_hint_counts: BTreeMap::new(),
157 action_success_counts: BTreeMap::new(),
158 action_denial_counts: BTreeMap::new(),
159 action_failure_counts: BTreeMap::new(),
160 foreground_momentum: BTreeMap::new(),
161 notifying_momentum: BTreeMap::new(),
162 semantic_hint_momentum: BTreeMap::new(),
163 action_success_momentum: BTreeMap::new(),
164 action_denial_momentum: BTreeMap::new(),
165 action_failure_momentum: BTreeMap::new(),
166 prewarm_hit_counts: BTreeMap::new(),
167 prewarm_miss_counts: BTreeMap::new(),
168 prewarm_hit_momentum: BTreeMap::new(),
169 prewarm_miss_momentum: BTreeMap::new(),
170 pending_prewarms: Vec::new(),
171 last_updated_window_id: None,
172 recent: VecDeque::new(),
173 }
174 }
175
176 pub fn load_or_default(config: &ModelMemoryConfig) -> Self {
177 let Some(path) = &config.persist_path else {
178 return Self::with_config(config);
179 };
180 match Self::load_from_path(path) {
181 Ok(mut memory) => {
182 memory.apply_config(config);
183 memory
184 },
185 Err(error) => {
186 tracing::warn!(path = %path.display(), error = %error, "model memory load failed; starting fresh");
187 Self::with_config(config)
188 },
189 }
190 }
191
192 pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, String> {
193 let file = File::open(path.as_ref())
194 .map_err(|error| format!("open model memory {}: {error}", path.as_ref().display()))?;
195 let reader = BufReader::new(file);
196 serde_json::from_reader(reader)
197 .map_err(|error| format!("parse model memory {}: {error}", path.as_ref().display()))
198 }
199
200 pub fn save_to_path(&self, path: impl AsRef<Path>) -> Result<(), String> {
201 let path = path.as_ref();
202 if let Some(parent) = path.parent() {
203 if !parent.as_os_str().is_empty() {
204 std::fs::create_dir_all(parent).map_err(|error| {
205 format!("create model memory dir {}: {error}", parent.display())
206 })?;
207 }
208 }
209 let file = File::create(path)
210 .map_err(|error| format!("create model memory {}: {error}", path.display()))?;
211 let writer = BufWriter::new(file);
212 serde_json::to_writer_pretty(writer, self)
213 .map_err(|error| format!("write model memory {}: {error}", path.display()))
214 }
215
216 pub fn persist_if_configured(&self, config: &ModelMemoryConfig) {
217 if let Some(path) = &config.persist_path {
218 if let Err(error) = self.save_to_path(path) {
219 tracing::warn!(path = %path.display(), error = %error, "model memory persist failed");
220 }
221 }
222 }
223
224 pub fn set_llm_summary(&mut self, summary: impl Into<String>) {
225 let summary = summary.into();
226 if !summary.trim().is_empty() {
227 self.llm_summary = Some(summary);
228 }
229 }
230
231 pub fn observation_windows(&self) -> u32 {
232 self.observation_windows
233 }
234
235 pub fn recent_feedback(&self) -> Vec<RecentDecisionRecord> {
236 self.recent.iter().cloned().collect()
237 }
238
239 pub fn model_input(&self, current_context: &StructuredContext) -> ModelInput {
240 ModelInput {
241 current_context: current_context.clone(),
242 behavior_profile: self.behavior_profile(),
243 recent_feedback: self.recent.iter().cloned().collect(),
244 }
245 }
246
247 pub fn observe_window(
248 &mut self,
249 context: &StructuredContext,
250 decision: &DecisionBackendResult,
251 audit_records: &[AuditRecord],
252 ) {
253 self.resolve_pending_prewarms(&context.summary.foreground_apps);
254 self.decay_momentum();
255
256 self.observation_windows = self.observation_windows.saturating_add(1);
257 self.last_updated_window_id = Some(context.window_id.clone());
258
259 for package in &context.summary.foreground_apps {
260 increment(&mut self.foreground_counts, package.clone());
261 add_momentum(&mut self.foreground_momentum, package.clone());
262 }
263 for package in &context.summary.notified_apps {
264 increment(&mut self.notifying_counts, package.clone());
265 add_momentum(&mut self.notifying_momentum, package.clone());
266 }
267 for hint in &context.summary.all_semantic_hints {
268 let hint = format!("{hint:?}");
269 increment(&mut self.semantic_hint_counts, hint.clone());
270 add_momentum(&mut self.semantic_hint_momentum, hint);
271 }
272 for record in audit_records {
273 let action = format!("{:?}", record.action_type);
274 match record.terminal {
275 ActionState::Succeeded => {
276 increment(&mut self.action_success_counts, action.clone());
277 add_momentum(&mut self.action_success_momentum, action);
278 },
279 ActionState::DeniedByCapability
280 | ActionState::DeniedByPolicy
281 | ActionState::RejectedInvalidSchema => {
282 increment(&mut self.action_denial_counts, action.clone());
283 add_momentum(&mut self.action_denial_momentum, action);
284 },
285 ActionState::Failed => {
286 increment(&mut self.action_failure_counts, action.clone());
287 add_momentum(&mut self.action_failure_momentum, action);
288 },
289 _ => {},
290 }
291 }
292
293 let feedback = RecentDecisionRecord {
294 window_id: context.window_id.clone(),
295 window_start_ms: context.window_start_ms,
296 window_end_ms: context.window_end_ms,
297 foreground_apps: context.summary.foreground_apps.clone(),
298 notified_apps: context.summary.notified_apps.clone(),
299 semantic_hints: context.summary.all_semantic_hints.clone(),
300 route: format!("{:?}", decision.route),
301 model: decision.intent_batch.model.clone(),
302 intent_count: decision.intent_batch.intents.len() as u32,
303 rationale_tags: decision.rationale_tags.clone(),
304 backend_error: decision.error.clone(),
305 action_outcomes: audit_records.iter().map(action_feedback).collect(),
306 };
307 self.recent.push_back(feedback);
308 while self.recent.len() > self.recent_limit {
309 self.recent.pop_front();
310 }
311
312 for record in audit_records {
313 self.register_pending_prewarm(context, record);
314 }
315 }
316 pub fn behavior_profile(&self) -> UserBehaviorProfile {
317 let frequent_foreground_apps = top_scores(&self.foreground_momentum, self.top_limit)
318 .unwrap_or_else(|| top_counts(&self.foreground_counts, self.top_limit));
319 let frequent_notifying_apps = top_scores(&self.notifying_momentum, self.top_limit)
320 .unwrap_or_else(|| top_counts(&self.notifying_counts, self.top_limit));
321 let frequent_semantic_hints =
322 top_semantic_hint_scores(&self.semantic_hint_momentum, self.top_limit)
323 .unwrap_or_else(|| top_semantic_hints(&self.semantic_hint_counts, self.top_limit));
324 let action_successes = top_scores(&self.action_success_momentum, self.top_limit)
325 .unwrap_or_else(|| top_counts(&self.action_success_counts, self.top_limit));
326 let action_denials = top_scores(&self.action_denial_momentum, self.top_limit)
327 .unwrap_or_else(|| top_counts(&self.action_denial_counts, self.top_limit));
328 let action_failures = top_scores(&self.action_failure_momentum, self.top_limit)
329 .unwrap_or_else(|| top_counts(&self.action_failure_counts, self.top_limit));
330 let prewarm_hits = top_scores(&self.prewarm_hit_momentum, self.top_limit)
331 .unwrap_or_else(|| top_counts(&self.prewarm_hit_counts, self.top_limit));
332 let prewarm_misses = top_scores(&self.prewarm_miss_momentum, self.top_limit)
333 .unwrap_or_else(|| top_counts(&self.prewarm_miss_counts, self.top_limit));
334 let local_summary = render_summary(SummaryRenderInput {
335 windows: self.observation_windows,
336 momentum_decay_milli: self.momentum_decay_milli,
337 prewarm_effect_windows: self.prewarm_effect_windows,
338 foreground: &frequent_foreground_apps,
339 notifying: &frequent_notifying_apps,
340 hints: &frequent_semantic_hints,
341 successes: &action_successes,
342 denials: &action_denials,
343 failures: &action_failures,
344 prewarm_hits: &prewarm_hits,
345 prewarm_misses: &prewarm_misses,
346 });
347 let summary = match &self.llm_summary {
348 Some(llm) => format!("llm_summary={llm}; local_counters={local_summary}"),
349 None => local_summary,
350 };
351
352 UserBehaviorProfile {
353 user_id: None,
354 summary,
355 observation_windows: self.observation_windows,
356 frequent_foreground_apps,
357 frequent_notifying_apps,
358 frequent_semantic_hints,
359 action_successes,
360 action_denials,
361 action_failures,
362 last_updated_window_id: self.last_updated_window_id.clone(),
363 }
364 }
365 fn apply_config(&mut self, config: &ModelMemoryConfig) {
366 self.recent_limit = config
367 .recent_limit
368 .clamp(MIN_RECENT_LIMIT, MAX_RECENT_LIMIT);
369 self.top_limit = config.top_limit.clamp(MIN_TOP_LIMIT, MAX_TOP_LIMIT);
370 self.momentum_decay_milli = config
371 .momentum_decay_milli
372 .clamp(MIN_MOMENTUM_DECAY_MILLI, MAX_MOMENTUM_DECAY_MILLI);
373 self.prewarm_effect_windows = config
374 .prewarm_effect_windows
375 .clamp(MIN_PREWARM_EFFECT_WINDOWS, MAX_PREWARM_EFFECT_WINDOWS);
376 while self.recent.len() > self.recent_limit {
377 self.recent.pop_front();
378 }
379 }
380
381 fn decay_momentum(&mut self) {
382 let decay = f64::from(self.momentum_decay_milli) / 1000.0;
383 decay_map(&mut self.foreground_momentum, decay);
384 decay_map(&mut self.notifying_momentum, decay);
385 decay_map(&mut self.semantic_hint_momentum, decay);
386 decay_map(&mut self.action_success_momentum, decay);
387 decay_map(&mut self.action_denial_momentum, decay);
388 decay_map(&mut self.action_failure_momentum, decay);
389 decay_map(&mut self.prewarm_hit_momentum, decay);
390 decay_map(&mut self.prewarm_miss_momentum, decay);
391 }
392
393 fn resolve_pending_prewarms(&mut self, foreground_apps: &[String]) {
394 let mut still_pending = Vec::new();
395 let pending = std::mem::take(&mut self.pending_prewarms);
396 for mut prewarm in pending {
397 prewarm.windows_observed = prewarm.windows_observed.saturating_add(1);
398 if foreground_apps
399 .iter()
400 .any(|package| package == &prewarm.package)
401 {
402 self.record_prewarm_attribution(
403 &prewarm,
404 FeedbackCorrectness::PredictionHit,
405 format!(
406 "prewarm target {} opened within {} observed window(s)",
407 prewarm.package, prewarm.windows_observed
408 ),
409 true,
410 );
411 } else if prewarm.windows_observed >= self.prewarm_effect_windows {
412 self.record_prewarm_attribution(
413 &prewarm,
414 FeedbackCorrectness::PredictionMiss,
415 format!(
416 "prewarm target {} was not opened within {} window(s)",
417 prewarm.package, self.prewarm_effect_windows
418 ),
419 false,
420 );
421 } else {
422 still_pending.push(prewarm);
423 }
424 }
425 self.pending_prewarms = still_pending;
426 }
427
428 fn record_prewarm_attribution(
429 &mut self,
430 prewarm: &PendingPrewarm,
431 correctness: FeedbackCorrectness,
432 evidence: String,
433 hit: bool,
434 ) {
435 if hit {
436 increment(&mut self.prewarm_hit_counts, prewarm.package.clone());
437 add_momentum(&mut self.prewarm_hit_momentum, prewarm.package.clone());
438 } else {
439 increment(&mut self.prewarm_miss_counts, prewarm.package.clone());
440 add_momentum(&mut self.prewarm_miss_momentum, prewarm.package.clone());
441 }
442
443 for recent in &mut self.recent {
444 if recent.window_id != prewarm.window_id {
445 continue;
446 }
447 for outcome in &mut recent.action_outcomes {
448 if outcome.action_type == "PreWarmProcess"
449 && outcome.target.as_deref() == Some(prewarm.target.as_str())
450 {
451 outcome.correctness = correctness;
452 outcome.correctness_evidence = evidence.clone();
453 outcome.outcome_summary = Some(if hit {
454 format!("prewarm_effect_hit:{}", prewarm.package)
455 } else {
456 format!("prewarm_effect_miss:{}", prewarm.package)
457 });
458 }
459 }
460 }
461 }
462
463 fn register_pending_prewarm(&mut self, context: &StructuredContext, record: &AuditRecord) {
464 if record.action_type != ActionType::PreWarmProcess
465 || record.terminal != ActionState::Succeeded
466 {
467 return;
468 }
469 let Some(target) = record.target.as_deref() else {
470 return;
471 };
472 let Some(package) = normalize_prewarm_package(target) else {
473 return;
474 };
475 self.pending_prewarms.push(PendingPrewarm {
476 window_id: context.window_id.clone(),
477 target: target.to_string(),
478 package,
479 windows_observed: 0,
480 });
481 }
482}
483
484fn default_momentum_decay_milli() -> u16 {
485 DEFAULT_MOMENTUM_DECAY_MILLI
486}
487
488fn default_prewarm_effect_windows() -> u8 {
489 DEFAULT_PREWARM_EFFECT_WINDOWS
490}
491
492fn normalize_prewarm_package(target: &str) -> Option<String> {
493 target
494 .strip_prefix("pkg:")
495 .filter(|package| !package.trim().is_empty())
496 .map(|package| package.trim().to_string())
497}
498
499fn add_momentum(scores: &mut BTreeMap<String, f64>, key: String) {
500 *scores.entry(key).or_insert(0.0) += 1.0;
501}
502
503fn decay_map(scores: &mut BTreeMap<String, f64>, decay: f64) {
504 if decay <= 0.0 {
505 scores.clear();
506 return;
507 }
508 for value in scores.values_mut() {
509 *value *= decay;
510 }
511 scores.retain(|_, value| *value >= 0.01);
512}
513
514fn top_scores(scores: &BTreeMap<String, f64>, limit: usize) -> Option<Vec<(String, u32)>> {
515 if scores.is_empty() {
516 return None;
517 }
518 let mut values: Vec<(String, f64)> = scores
519 .iter()
520 .map(|(key, score)| (key.clone(), *score))
521 .collect();
522 values.sort_by(|a, b| {
523 b.1.partial_cmp(&a.1)
524 .unwrap_or(std::cmp::Ordering::Equal)
525 .then_with(|| a.0.cmp(&b.0))
526 });
527 values.truncate(limit);
528 Some(
529 values
530 .into_iter()
531 .map(|(key, score)| (key, score.round().max(1.0) as u32))
532 .collect(),
533 )
534}
535
536fn top_semantic_hint_scores(
537 scores: &BTreeMap<String, f64>,
538 limit: usize,
539) -> Option<Vec<(SemanticHint, u32)>> {
540 top_scores(scores, limit).map(|values| {
541 values
542 .into_iter()
543 .filter_map(|(name, count)| semantic_hint_from_debug(&name).map(|hint| (hint, count)))
544 .collect()
545 })
546}
547fn action_feedback(record: &AuditRecord) -> ActionFeedbackRecord {
548 let (correctness, evidence) = infer_correctness(record);
549 ActionFeedbackRecord {
550 action_type: format!("{:?}", record.action_type),
551 target: record.target.clone(),
552 terminal: format!("{:?}", record.terminal),
553 correctness,
554 correctness_evidence: evidence,
555 denial_reason: record.denial_reason.map(|reason| format!("{reason:?}")),
556 error: record.error.clone(),
557 outcome_summary: record
558 .outcome
559 .as_ref()
560 .map(|outcome| outcome.summary.clone()),
561 }
562}
563
564fn infer_correctness(record: &AuditRecord) -> (FeedbackCorrectness, String) {
565 if record.action_type == ActionType::NoOp && record.terminal == ActionState::Succeeded {
566 return (
567 FeedbackCorrectness::NeutralNoOp,
568 "NoOp succeeded; no behavioral preference inferred".into(),
569 );
570 }
571 match record.terminal {
572 ActionState::Succeeded => (
573 FeedbackCorrectness::LikelyCorrect,
574 "policy approved and adapter reported success".into(),
575 ),
576 ActionState::DeniedByCapability
577 | ActionState::DeniedByPolicy
578 | ActionState::RejectedInvalidSchema => (
579 FeedbackCorrectness::PolicyRejected,
580 "local policy or schema rejected the model suggestion".into(),
581 ),
582 ActionState::Failed => (
583 FeedbackCorrectness::ExecutionFailed,
584 "adapter attempted execution but returned failure".into(),
585 ),
586 _ => (
587 FeedbackCorrectness::Unknown,
588 "action did not reach a terminal feedback state".into(),
589 ),
590 }
591}
592
593fn increment(counts: &mut BTreeMap<String, u32>, key: String) {
594 *counts.entry(key).or_insert(0) += 1;
595}
596
597fn top_counts(counts: &BTreeMap<String, u32>, limit: usize) -> Vec<(String, u32)> {
598 let mut values: Vec<(String, u32)> = counts
599 .iter()
600 .map(|(key, count)| (key.clone(), *count))
601 .collect();
602 values.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
603 values.truncate(limit);
604 values
605}
606
607fn top_semantic_hints(counts: &BTreeMap<String, u32>, limit: usize) -> Vec<(SemanticHint, u32)> {
608 top_counts(counts, limit)
609 .into_iter()
610 .filter_map(|(name, count)| semantic_hint_from_debug(&name).map(|hint| (hint, count)))
611 .collect()
612}
613
614fn semantic_hint_from_debug(name: &str) -> Option<SemanticHint> {
615 match name {
616 "FileMention" => Some(SemanticHint::FileMention),
617 "ImageMention" => Some(SemanticHint::ImageMention),
618 "AudioMessage" => Some(SemanticHint::AudioMessage),
619 "LinkAttachment" => Some(SemanticHint::LinkAttachment),
620 "UserMentioned" => Some(SemanticHint::UserMentioned),
621 "CalendarInvitation" => Some(SemanticHint::CalendarInvitation),
622 "FinancialContext" => Some(SemanticHint::FinancialContext),
623 "VerificationCode" => Some(SemanticHint::VerificationCode),
624 _ => None,
625 }
626}
627
628struct SummaryRenderInput<'a> {
629 windows: u32,
630 momentum_decay_milli: u16,
631 prewarm_effect_windows: u8,
632 foreground: &'a [(String, u32)],
633 notifying: &'a [(String, u32)],
634 hints: &'a [(SemanticHint, u32)],
635 successes: &'a [(String, u32)],
636 denials: &'a [(String, u32)],
637 failures: &'a [(String, u32)],
638 prewarm_hits: &'a [(String, u32)],
639 prewarm_misses: &'a [(String, u32)],
640}
641
642fn render_summary(input: SummaryRenderInput<'_>) -> String {
643 format!(
644 "observed_windows={}; momentum_decay_milli={}; prewarm_effect_window={}; foreground_apps={}; notifying_apps={}; semantic_hints={}; successful_actions={}; denied_actions={}; failed_actions={}; prewarm_effect_hits={}; prewarm_effect_misses={}",
645 input.windows,
646 input.momentum_decay_milli,
647 input.prewarm_effect_windows,
648 render_pairs(input.foreground),
649 render_pairs(input.notifying),
650 render_hint_pairs(input.hints),
651 render_pairs(input.successes),
652 render_pairs(input.denials),
653 render_pairs(input.failures),
654 render_pairs(input.prewarm_hits),
655 render_pairs(input.prewarm_misses),
656 )
657}
658fn render_pairs(values: &[(String, u32)]) -> String {
659 if values.is_empty() {
660 return "none".into();
661 }
662 values
663 .iter()
664 .map(|(key, count)| format!("{key}:{count}"))
665 .collect::<Vec<_>>()
666 .join(",")
667}
668
669fn render_hint_pairs(values: &[(SemanticHint, u32)]) -> String {
670 if values.is_empty() {
671 return "none".into();
672 }
673 values
674 .iter()
675 .map(|(key, count)| format!("{key:?}:{count}"))
676 .collect::<Vec<_>>()
677 .join(",")
678}
679
680fn read_u16_env(name: &str) -> Option<u16> {
681 std::env::var(name)
682 .ok()
683 .and_then(|value| value.parse().ok())
684}
685
686fn read_u8_env(name: &str) -> Option<u8> {
687 std::env::var(name)
688 .ok()
689 .and_then(|value| value.parse().ok())
690}
691
692fn read_usize_env(name: &str) -> Option<usize> {
693 std::env::var(name)
694 .ok()
695 .and_then(|value| value.parse().ok())
696}
697#[cfg(test)]
698mod tests {
699 use super::*;
700 use crate::privacy_airgap::DefaultPrivacyAirGap;
701 use aios_spec::governance::{ActionCoord, ActionProposal, EffectClass};
702 use aios_spec::intent::{ActionUrgency, SuggestedAction};
703 use aios_spec::traits::PrivacySanitizer;
704 use aios_spec::{
705 AppTransition, AppTransitionRawEvent, ContextSummary, FsAccessEvent, FsAccessType,
706 NotificationRawEvent, RawEvent, SourceTier,
707 };
708
709 #[test]
710 fn memory_input_contains_recent_feedback_after_observation() {
711 let ctx = test_context("w1");
712 let decision = test_decision("w1");
713
714 let mut memory = ModelMemoryStore::default();
715 memory.observe_window(&ctx, &decision, &[]);
716 let input = memory.model_input(&ctx);
717
718 assert_eq!(input.behavior_profile.observation_windows, 1);
719 assert_eq!(input.recent_feedback.len(), 1);
720 assert!(input.behavior_profile.summary.contains("com.example:1"));
721 }
722
723 #[test]
724 fn memory_round_trips_to_json_file() {
725 let ctx = test_context("persist-window");
726 let decision = test_decision("persist-window");
727 let mut memory = ModelMemoryStore::new(3);
728 memory.observe_window(&ctx, &decision, &[]);
729 memory.set_llm_summary("opens docs after chat attachments");
730
731 let path =
732 std::env::temp_dir().join(format!("dipecs-model-memory-{}.json", uuid::Uuid::new_v4()));
733 memory.save_to_path(&path).unwrap();
734 let loaded = ModelMemoryStore::load_from_path(&path).unwrap();
735 let _ = std::fs::remove_file(path);
736
737 assert_eq!(loaded.observation_windows(), 1);
738 assert!(loaded
739 .behavior_profile()
740 .summary
741 .contains("opens docs after chat attachments"));
742 }
743
744 #[test]
745 fn feedback_correctness_marks_policy_denials() {
746 let action = SuggestedAction {
747 action_type: ActionType::PreWarmProcess,
748 target: Some("pkg:com.example".into()),
749 urgency: ActionUrgency::Immediate,
750 };
751 let proposal = ActionProposal {
752 intent_id: "intent".into(),
753 coord: ActionCoord {
754 window_ordinal: 0,
755 intent_ordinal: 0,
756 action_ordinal: 0,
757 },
758 action,
759 effect: EffectClass::LocalStateChange,
760 proposed_at_ms: 1,
761 };
762 let mut record = AuditRecord::new(
763 &proposal,
764 aios_spec::DecisionRoute::CloudLlm,
765 SourceTier::PublicApi,
766 );
767 record.transition(ActionState::DeniedByPolicy);
768
769 let feedback = action_feedback(&record);
770 assert_eq!(feedback.correctness, FeedbackCorrectness::PolicyRejected);
771 assert!(feedback.correctness_evidence.contains("rejected"));
772 }
773
774 #[test]
775 fn momentum_profile_prioritizes_recent_windows() {
776 let config = ModelMemoryConfig {
777 momentum_decay_milli: 200,
778 ..ModelMemoryConfig::default()
779 };
780 let mut memory = ModelMemoryStore::with_config(&config);
781 let decision = test_decision("w");
782
783 for index in 0..3 {
784 memory.observe_window(
785 &test_context_with_foreground(&format!("old-{index}"), "com.old"),
786 &decision,
787 &[],
788 );
789 }
790 memory.observe_window(
791 &test_context_with_foreground("new", "com.new"),
792 &decision,
793 &[],
794 );
795
796 let profile = memory.behavior_profile();
797 assert_eq!(profile.frequent_foreground_apps[0].0, "com.new");
798 assert!(profile.summary.contains("momentum_decay_milli=200"));
799 }
800
801 #[test]
802 fn prewarm_effect_hit_updates_recent_feedback() {
803 let config = ModelMemoryConfig {
804 prewarm_effect_windows: 2,
805 ..ModelMemoryConfig::default()
806 };
807 let mut memory = ModelMemoryStore::with_config(&config);
808 let decision = test_decision("prewarm");
809 let record = succeeded_prewarm_record("pkg:com.target");
810
811 memory.observe_window(
812 &test_context_with_foreground("prewarm", "com.source"),
813 &decision,
814 &[record],
815 );
816 memory.observe_window(
817 &test_context_with_foreground("hit", "com.target"),
818 &decision,
819 &[],
820 );
821
822 let recent = memory.recent_feedback();
823 let prewarm_feedback = recent
824 .iter()
825 .find(|record| record.window_id == "prewarm")
826 .and_then(|record| record.action_outcomes.first())
827 .expect("prewarm feedback should remain in recent window");
828 assert_eq!(
829 prewarm_feedback.correctness,
830 FeedbackCorrectness::PredictionHit
831 );
832 assert!(prewarm_feedback.correctness_evidence.contains("opened"));
833 assert!(memory
834 .behavior_profile()
835 .summary
836 .contains("prewarm_effect_hits=com.target:1"));
837 }
838
839 #[test]
840 fn prewarm_effect_miss_updates_recent_feedback_after_horizon() {
841 let config = ModelMemoryConfig {
842 prewarm_effect_windows: 1,
843 ..ModelMemoryConfig::default()
844 };
845 let mut memory = ModelMemoryStore::with_config(&config);
846 let decision = test_decision("prewarm");
847 let record = succeeded_prewarm_record("pkg:com.target");
848
849 memory.observe_window(
850 &test_context_with_foreground("prewarm", "com.source"),
851 &decision,
852 &[record],
853 );
854 memory.observe_window(
855 &test_context_with_foreground("miss", "com.other"),
856 &decision,
857 &[],
858 );
859
860 let recent = memory.recent_feedback();
861 let prewarm_feedback = recent
862 .iter()
863 .find(|record| record.window_id == "prewarm")
864 .and_then(|record| record.action_outcomes.first())
865 .expect("prewarm feedback should remain in recent window");
866 assert_eq!(
867 prewarm_feedback.correctness,
868 FeedbackCorrectness::PredictionMiss
869 );
870 assert!(prewarm_feedback.correctness_evidence.contains("not opened"));
871 assert!(memory
872 .behavior_profile()
873 .summary
874 .contains("prewarm_effect_misses=com.target:1"));
875 }
876 #[test]
877 fn model_input_does_not_contain_raw_notification_text_or_file_path() {
878 let sanitizer = DefaultPrivacyAirGap;
879 let notification = sanitizer.sanitize(RawEvent::NotificationPosted(NotificationRawEvent {
880 timestamp_ms: 10,
881 package_name: "com.chat".into(),
882 category: None,
883 channel_id: None,
884 raw_title: "Alice private payroll".into(),
885 raw_text: "secret verification 123456 at /sdcard/private/report.docx".into(),
886 title_hint: None,
887 text_hint: None,
888 semantic_hints: vec![],
889 is_ongoing: false,
890 group_key: Some("thread-private-alice".into()),
891 has_picture: false,
892 }));
893 let file = sanitizer.sanitize(RawEvent::FileSystemAccess(FsAccessEvent {
894 timestamp_ms: 11,
895 pid: 1,
896 uid: 2,
897 file_path: "/sdcard/private/report.docx".into(),
898 access_type: FsAccessType::OpenRead,
899 bytes_transferred: Some(10),
900 }));
901 let ctx = StructuredContext {
902 window_id: "privacy".into(),
903 window_start_ms: 0,
904 window_end_ms: 100,
905 duration_secs: 1,
906 events: vec![notification, file],
907 summary: ContextSummary {
908 foreground_apps: vec![],
909 notified_apps: vec!["com.chat".into()],
910 all_semantic_hints: vec![SemanticHint::VerificationCode],
911 file_activity: vec![],
912 latest_system_status: None,
913 source_tier: SourceTier::PublicApi,
914 },
915 };
916
917 let input = ModelMemoryStore::default().model_input(&ctx);
918 let json = serde_json::to_string(&input).unwrap();
919
920 assert!(!json.contains("Alice private payroll"));
921 assert!(!json.contains("123456"));
922 assert!(!json.contains("/sdcard/private/report.docx"));
923 assert!(!json.contains("thread-private-alice"));
924 }
925
926 fn test_context(window_id: &str) -> StructuredContext {
927 StructuredContext {
928 window_id: window_id.into(),
929 window_start_ms: 0,
930 window_end_ms: 1000,
931 duration_secs: 1,
932 events: vec![DefaultPrivacyAirGap.sanitize(RawEvent::AppTransition(
933 AppTransitionRawEvent {
934 timestamp_ms: 100,
935 package_name: "com.example".into(),
936 activity_class: None,
937 transition: AppTransition::Foreground,
938 },
939 ))],
940 summary: ContextSummary {
941 foreground_apps: vec!["com.example".into()],
942 notified_apps: vec!["com.chat".into()],
943 all_semantic_hints: vec![SemanticHint::FileMention],
944 file_activity: vec![],
945 latest_system_status: None,
946 source_tier: SourceTier::PublicApi,
947 },
948 }
949 }
950
951 fn test_context_with_foreground(window_id: &str, package: &str) -> StructuredContext {
952 let mut ctx = test_context(window_id);
953 ctx.summary.foreground_apps = vec![package.into()];
954 ctx.events =
955 vec![
956 DefaultPrivacyAirGap.sanitize(RawEvent::AppTransition(AppTransitionRawEvent {
957 timestamp_ms: 100,
958 package_name: package.into(),
959 activity_class: None,
960 transition: AppTransition::Foreground,
961 })),
962 ];
963 ctx
964 }
965
966 fn succeeded_prewarm_record(target: &str) -> AuditRecord {
967 let action = SuggestedAction {
968 action_type: ActionType::PreWarmProcess,
969 target: Some(target.into()),
970 urgency: ActionUrgency::Immediate,
971 };
972 let proposal = ActionProposal {
973 intent_id: "intent".into(),
974 coord: ActionCoord {
975 window_ordinal: 0,
976 intent_ordinal: 0,
977 action_ordinal: 0,
978 },
979 action,
980 effect: EffectClass::LocalStateChange,
981 proposed_at_ms: 1,
982 };
983 let mut record = AuditRecord::new(
984 &proposal,
985 aios_spec::DecisionRoute::CloudLlm,
986 SourceTier::PublicApi,
987 );
988 record.transition(ActionState::Succeeded);
989 record
990 }
991 fn test_decision(window_id: &str) -> DecisionBackendResult {
992 DecisionBackendResult {
993 route: aios_spec::DecisionRoute::RuleBased,
994 intent_batch: aios_spec::IntentBatch {
995 window_id: window_id.into(),
996 intents: vec![],
997 generated_at_ms: 1000,
998 model: "test".into(),
999 },
1000 rationale_tags: vec!["test".into()],
1001 latency_us: 0,
1002 error: None,
1003 }
1004 }
1005}