Skip to main content

aios_daemon/
pipeline.rs

1//! Context window processing pipeline.
2//!
3//! A closed window flows through DecisionRouter, ActionLifecycle, audit, and
4//! model memory feedback.
5use std::fs::{File, OpenOptions};
6use std::io::Write;
7use std::path::Path;
8use std::sync::Arc;
9use std::thread::{self, JoinHandle};
10use std::time::{Duration, SystemTime, UNIX_EPOCH};
11
12use aios_agent::{DecisionRouter, ProfileSummarizer};
13use aios_collector::collection_stats::RawEventStats;
14use aios_core::action_lifecycle::ActionLifecycle;
15use aios_core::context_builder::WindowAggregator;
16use aios_core::context_memory::{ModelMemoryConfig, ModelMemoryStore};
17use aios_core::privacy_airgap::DefaultPrivacyAirGap;
18use aios_spec::traits::PrivacySanitizer;
19use aios_spec::IngestedRawEvent;
20use aios_spec::{CapabilityLevel, RecentDecisionRecord, UserBehaviorProfile};
21use serde_json::json;
22use tokio::sync::mpsc;
23use tokio::time::{sleep_until, Instant};
24
25/// Append-only NDJSON recorder for daemon window processing.
26pub struct RuntimeTraceRecorder {
27    file: File,
28}
29
30impl RuntimeTraceRecorder {
31    pub fn new(path: impl AsRef<Path>) -> std::io::Result<Self> {
32        let path = path.as_ref();
33        if let Some(parent) = path.parent() {
34            if !parent.as_os_str().is_empty() {
35                std::fs::create_dir_all(parent)?;
36            }
37        }
38        let file = OpenOptions::new().create(true).append(true).open(path)?;
39        Ok(Self { file })
40    }
41
42    pub fn record_window(&mut self, record: &serde_json::Value) -> std::io::Result<()> {
43        serde_json::to_writer(&mut self.file, record)?;
44        self.file.write_all(b"\n")?;
45        self.file.flush()
46    }
47}
48
49/// Mutable dependencies shared while processing a closed context window.
50pub(crate) struct WindowProcessingDeps<'a> {
51    pub(crate) router: &'a DecisionRouter,
52    pub(crate) lifecycle: &'a ActionLifecycle<'a>,
53    pub(crate) memory: &'a mut ModelMemoryStore,
54    pub(crate) memory_config: &'a ModelMemoryConfig,
55    pub(crate) profile_summary_worker: Option<&'a mut ProfileSummaryWorker>,
56    pub(crate) trace_recorder: Option<&'a mut RuntimeTraceRecorder>,
57}
58
59/// Processes one closed context window and feeds the result back into memory.
60pub(crate) fn process_window(
61    window_ordinal: u32,
62    ctx: &aios_spec::StructuredContext,
63    raw_stats: &RawEventStats,
64    deps: &mut WindowProcessingDeps<'_>,
65) {
66    tracing::info!(
67        window_id = %ctx.window_id,
68        event_count = ctx.events.len(),
69        raw_event_total = raw_stats.total(),
70        raw_event_stats = %raw_stats.summary_line(),
71        duration_secs = ctx.duration_secs,
72        "window closed, sending to agent"
73    );
74
75    if let Some(worker) = &mut deps.profile_summary_worker {
76        worker.poll(deps.memory);
77    }
78
79    let model_input = deps.memory.model_input(ctx);
80    let decision_result = deps.router.evaluate_model_input(&model_input);
81    tracing::info!(
82        route = ?decision_result.route,
83        model = %decision_result.intent_batch.model,
84        latency_us = decision_result.latency_us,
85        error = ?decision_result.error,
86        "decision backend completed"
87    );
88
89    let capability = CapabilityLevel::for_route(decision_result.route);
90    let audit_records = deps.lifecycle.run(
91        window_ordinal,
92        &decision_result.intent_batch,
93        decision_result.route,
94        decision_result.error.clone(),
95        &capability,
96        ctx,
97    );
98
99    deps.memory
100        .observe_window(ctx, &decision_result, &audit_records);
101    if let Some(worker) = &mut deps.profile_summary_worker {
102        worker.poll(deps.memory);
103        worker.maybe_start(deps.memory);
104    }
105    deps.memory.persist_if_configured(deps.memory_config);
106    let executed = audit_records
107        .iter()
108        .filter(|r| matches!(r.terminal, aios_spec::governance::ActionState::Succeeded))
109        .count() as u32;
110    let denied = audit_records
111        .iter()
112        .filter(|r| {
113            matches!(
114                r.terminal,
115                aios_spec::governance::ActionState::RejectedInvalidSchema
116                    | aios_spec::governance::ActionState::DeniedByCapability
117                    | aios_spec::governance::ActionState::DeniedByPolicy
118            )
119        })
120        .count() as u32;
121    let failed = audit_records
122        .iter()
123        .filter(|r| matches!(r.terminal, aios_spec::governance::ActionState::Failed))
124        .count() as u32;
125
126    for record in &audit_records {
127        match record.terminal {
128            aios_spec::governance::ActionState::Failed => {
129                tracing::warn!(
130                    coord = ?record.coord,
131                    action = ?record.action_type,
132                    error = ?record.error,
133                    "action execution failed"
134                );
135            },
136            aios_spec::governance::ActionState::DeniedByCapability
137            | aios_spec::governance::ActionState::DeniedByPolicy
138            | aios_spec::governance::ActionState::RejectedInvalidSchema => {
139                tracing::warn!(
140                    coord = ?record.coord,
141                    action = ?record.action_type,
142                    reason = ?record.denial_reason,
143                    "action denied"
144                );
145            },
146            _ => {},
147        }
148    }
149
150    tracing::info!(
151        window_id = %ctx.window_id,
152        intents_total = decision_result.intent_batch.intents.len(),
153        actions_executed = executed,
154        actions_denied = denied,
155        actions_failed = failed,
156        "window processed"
157    );
158
159    if let Some(recorder) = &mut deps.trace_recorder {
160        let record = json!({
161            "stage": "daemon_window",
162            "window_ordinal": window_ordinal,
163            "window_id": ctx.window_id,
164            "window_start_ms": ctx.window_start_ms,
165            "window_end_ms": ctx.window_end_ms,
166            "duration_secs": ctx.duration_secs,
167            "event_count": ctx.events.len(),
168            "raw_event_total": raw_stats.total(),
169            "raw_event_stats": raw_stats.summary_fields(),
170            "context_summary": ctx.summary,
171            "behavior_profile": deps.memory.behavior_profile(),
172            "decision": {
173                "route": format!("{:?}", decision_result.route),
174                "model": decision_result.intent_batch.model,
175                "intent_count": decision_result.intent_batch.intents.len(),
176                "rationale_tags": decision_result.rationale_tags,
177                "latency_us": decision_result.latency_us,
178                "error": decision_result.error,
179            },
180            "audit": audit_records,
181        });
182        if let Err(error) = recorder.record_window(&record) {
183            tracing::warn!(error = %error, "failed to write daemon runtime trace");
184        }
185    }
186}
187
188/// 处理循环的终止原因。
189#[derive(Debug, Clone, Copy, PartialEq, Eq)]
190pub(crate) enum LoopTermination {
191    /// raw 通道关闭 (采集侧所有 sender 落地) —— 退出前已 flush 最后一个窗口。
192    ChannelClosed,
193}
194
195/// 处理循环收尾摘要。
196#[derive(Debug, Clone, Copy)]
197pub(crate) struct ProcessingLoopSummary {
198    pub(crate) windows_closed: u32,
199    pub(crate) events_processed: u64,
200    pub(crate) terminated_by: LoopTermination,
201}
202
203/// Task-2 处理循环: 从 raw 通道消费已贴 SourceTier 的事件, 按时间窗口聚合, 逐窗口
204/// 驱动决策 → 策略 → 执行 → 审计 → trace (`process_window`)。
205///
206/// 终止由通道关闭驱动: 当采集侧所有 sender 落地, `recv()` 返回 `None`, 循环 flush
207/// 最后一个窗口再退出。这样 shutdown 时不会丢掉最后一个未满窗口。
208///
209/// 窗口计时用 `tokio::time::Instant`——生产环境等价于实时, 同时让暂停时钟测试
210/// (`start_paused`) 能确定性地推进到 deadline 触发关窗。
211pub(crate) async fn run_processing_loop(
212    mut raw_rx: mpsc::Receiver<IngestedRawEvent>,
213    sanitizer: &DefaultPrivacyAirGap,
214    mut window: WindowAggregator,
215    window_duration: Duration,
216    deps: &mut WindowProcessingDeps<'_>,
217) -> ProcessingLoopSummary {
218    let mut raw_stats = RawEventStats::default();
219    let mut window_ordinal = 0u32;
220    let mut events_processed = 0u64;
221    let mut window_deadline = Instant::now() + window_duration;
222
223    loop {
224        // 刻意不设 shutdown 分支: 优雅停机靠「采集侧 drop sender → 通道关闭 → recv() 返回
225        // None → flush 最后窗口再退」(见 collection::run_collection_loop)。若在此加一条收到
226        // 信号即退的臂, 会重新引入本设计要避免的「丢最后一个未满窗口」bug。
227        let processing_event = tokio::select! {
228            maybe = raw_rx.recv() => match maybe {
229                // Box only affects this local dispatch enum's size; the raw
230                // event is unboxed below before normal processing.
231                Some(raw) => ProcessingEvent::Raw(Box::new(raw)),
232                None => ProcessingEvent::RawChannelClosed,
233            },
234            _ = sleep_until(window_deadline) => ProcessingEvent::WindowExpired,
235        };
236
237        if should_stop_processing(&processing_event) {
238            tracing::info!("raw event channel closed, flushing remaining events");
239            let window_stats = std::mem::take(&mut raw_stats);
240            if let Some(ctx) = window.close(timestamp_ms()) {
241                process_window(window_ordinal, &ctx, &window_stats, deps);
242                window_ordinal += 1;
243            }
244            return ProcessingLoopSummary {
245                windows_closed: window_ordinal,
246                events_processed,
247                terminated_by: LoopTermination::ChannelClosed,
248            };
249        }
250
251        let window_expired = matches!(processing_event, ProcessingEvent::WindowExpired);
252
253        match processing_event {
254            ProcessingEvent::Raw(ingested) => {
255                // Return to the owned IngestedRawEvent shape expected by the
256                // stats and sanitizer code paths.
257                let ingested = *ingested;
258                raw_stats.record(&ingested.raw_event);
259                let sanitized =
260                    sanitizer.sanitize_with_tier(ingested.raw_event, ingested.source_tier);
261                window.push(sanitized);
262                events_processed += 1;
263            },
264            ProcessingEvent::RawChannelClosed => unreachable!("handled before event dispatch"),
265            ProcessingEvent::WindowExpired => {},
266        }
267
268        if window_expired || Instant::now() >= window_deadline {
269            let window_stats = std::mem::take(&mut raw_stats);
270            if let Some(ctx) = window.close(timestamp_ms()) {
271                process_window(window_ordinal, &ctx, &window_stats, deps);
272                window_ordinal += 1;
273            }
274            window_deadline = Instant::now() + window_duration;
275        }
276    }
277}
278
279/// 当前 epoch 毫秒。窗口 ID / trace 时间戳用, 与决策控制流无关。
280pub(crate) fn timestamp_ms() -> i64 {
281    SystemTime::now()
282        .duration_since(UNIX_EPOCH)
283        .unwrap_or_default()
284        .as_millis() as i64
285}
286
287type SummarizerFn = Arc<
288    dyn Fn(&UserBehaviorProfile, &[RecentDecisionRecord]) -> Result<String, String> + Send + Sync,
289>;
290
291/// Non-blocking profile compression worker.
292///
293/// The decision path keeps using local counters and the last completed summary.
294/// When the configured interval is reached, this worker snapshots sanitized
295/// memory and asks the LLM to compress it on a background thread.
296pub(crate) struct ProfileSummaryWorker {
297    summarizer: Option<ProfileSummarizer>,
298    interval_windows: u32,
299    pending: Option<JoinHandle<ProfileSummaryJobResult>>,
300    last_started_window: u32,
301    summarizer_fn: Option<SummarizerFn>,
302}
303
304struct ProfileSummaryJobResult {
305    observed_windows: u32,
306    recent: Vec<RecentDecisionRecord>,
307    result: Result<String, String>,
308}
309
310impl ProfileSummaryWorker {
311    pub(crate) fn new(summarizer: Option<ProfileSummarizer>, interval_windows: u32) -> Self {
312        Self {
313            summarizer,
314            interval_windows,
315            pending: None,
316            last_started_window: 0,
317            summarizer_fn: None,
318        }
319    }
320
321    #[cfg(test)]
322    pub(crate) fn with_summarizer_fn(
323        interval_windows: u32,
324        f: impl Fn(&UserBehaviorProfile, &[RecentDecisionRecord]) -> Result<String, String>
325            + Send
326            + Sync
327            + 'static,
328    ) -> Self {
329        Self {
330            summarizer: None,
331            interval_windows,
332            pending: None,
333            last_started_window: 0,
334            summarizer_fn: Some(Arc::new(f)),
335        }
336    }
337
338    pub(crate) fn poll(&mut self, memory: &mut ModelMemoryStore) {
339        let Some(handle) = self.pending.take() else {
340            return;
341        };
342        if !handle.is_finished() {
343            self.pending = Some(handle);
344            return;
345        }
346
347        match handle.join() {
348            Ok(job) => match job.result {
349                Ok(summary) => {
350                    tracing::info!(
351                        observed_windows = job.observed_windows,
352                        recent_windows = job.recent.len(),
353                        "profile summary refreshed"
354                    );
355                    memory.set_llm_summary(summary);
356                },
357                Err(error) => {
358                    tracing::warn!(
359                        observed_windows = job.observed_windows,
360                        recent_windows = job.recent.len(),
361                        error = %error,
362                        "profile summary refresh failed"
363                    );
364                },
365            },
366            Err(_) => {
367                tracing::warn!("profile summary worker panicked");
368            },
369        }
370    }
371
372    pub(crate) fn maybe_start(&mut self, memory: &ModelMemoryStore) {
373        if self.summarizer_fn.is_none() && self.summarizer.is_none() {
374            return;
375        }
376        if self.pending.is_some() || self.interval_windows == 0 {
377            return;
378        }
379        let windows = memory.observation_windows();
380        if windows == 0
381            || !windows.is_multiple_of(self.interval_windows)
382            || windows == self.last_started_window
383        {
384            return;
385        }
386
387        let profile = memory.behavior_profile();
388        let recent = memory.recent_feedback();
389        self.last_started_window = windows;
390
391        if let Some(f) = self.summarizer_fn.clone() {
392            self.pending = Some(thread::spawn(move || {
393                let result = f(&profile, &recent);
394                ProfileSummaryJobResult {
395                    observed_windows: windows,
396                    recent,
397                    result,
398                }
399            }));
400            tracing::info!(
401                observed_windows = windows,
402                "profile summary refresh started"
403            );
404            return;
405        }
406
407        let Some(summarizer) = self.summarizer.clone() else {
408            return;
409        };
410        self.pending = Some(thread::spawn(move || {
411            let result = summarizer.summarize(&profile, &recent);
412            ProfileSummaryJobResult {
413                observed_windows: windows,
414                recent,
415                result,
416            }
417        }));
418        tracing::info!(
419            observed_windows = windows,
420            "profile summary refresh started"
421        );
422    }
423}
424// ============================================================
425// Processing event dispatch
426// ============================================================
427
428#[derive(Debug)]
429pub enum ProcessingEvent {
430    // Keep the enum small even when RawEvent grows additional metadata fields.
431    // The processing loop immediately unboxes this before sanitization.
432    Raw(Box<IngestedRawEvent>),
433
434    RawChannelClosed,
435
436    WindowExpired,
437}
438
439pub fn should_stop_processing(event: &ProcessingEvent) -> bool {
440    matches!(event, ProcessingEvent::RawChannelClosed)
441}
442
443// ============================================================
444// Unit tests
445// ============================================================
446
447#[cfg(test)]
448mod tests {
449    use super::*;
450    use std::io::Read;
451    use std::time::{SystemTime, UNIX_EPOCH};
452
453    use aios_action::DefaultActionExecutor;
454    use aios_agent::DecisionRouter;
455    use aios_collector::collection_stats::RawEventStats;
456    use aios_collector::AndroidJsonlIngress;
457    use aios_core::action_bus::ActionBus;
458    use aios_core::action_lifecycle::ActionLifecycle;
459    use aios_core::collector_ingress::RustCollectorIngress;
460    use aios_core::context_builder::WindowAggregator;
461    use aios_core::context_memory::{ModelMemoryConfig, ModelMemoryStore};
462    use aios_core::governance::{ActionAdapter, AuthorizedAction};
463    use aios_core::policy_engine::PolicyEngine;
464    use aios_core::privacy_airgap::DefaultPrivacyAirGap;
465    use aios_spec::governance::{ActionOutcome, AdapterError};
466    use aios_spec::{
467        ContextSummary, DecisionBackendResult, DecisionRoute, IntentBatch, SourceTier,
468        StructuredContext,
469    };
470
471    use crate::collection::{run_collection_loop, RawEventSource};
472    use tokio::sync::broadcast;
473
474    // ── helpers ──────────────────────────────────────────────────
475
476    fn empty_context(window_id: &str) -> StructuredContext {
477        StructuredContext {
478            window_id: window_id.into(),
479            window_start_ms: 0,
480            window_end_ms: 10000,
481            duration_secs: 10,
482            events: vec![],
483            summary: ContextSummary {
484                foreground_apps: vec![],
485                notified_apps: vec![],
486                all_semantic_hints: vec![],
487                file_activity: vec![],
488                latest_system_status: None,
489                source_tier: SourceTier::PublicApi,
490            },
491        }
492    }
493
494    fn empty_decision(window_id: &str) -> DecisionBackendResult {
495        DecisionBackendResult {
496            route: DecisionRoute::RuleBased,
497            intent_batch: IntentBatch {
498                window_id: window_id.into(),
499                intents: vec![],
500                generated_at_ms: 0,
501                model: "test".into(),
502            },
503            rationale_tags: vec![],
504            latency_us: 0,
505            error: None,
506        }
507    }
508
509    fn advance_memory_to_windows(memory: &mut ModelMemoryStore, n: u32) {
510        for i in 0..n {
511            let wid = format!("w{}", i);
512            let ctx = empty_context(&wid);
513            let decision = empty_decision(&wid);
514            memory.observe_window(&ctx, &decision, &[]);
515        }
516    }
517
518    // ── mock adapter ────────────────────────────────────────────
519
520    struct OkAdapter;
521    impl ActionAdapter for OkAdapter {
522        fn name(&self) -> &'static str {
523            "ok"
524        }
525        fn execute(&self, authorized: &AuthorizedAction) -> Result<ActionOutcome, AdapterError> {
526            Ok(ActionOutcome {
527                action_type: format!("{:?}", authorized.action().action_type),
528                target: authorized.action().target.clone(),
529                summary: "ok".into(),
530                latency_us: 0,
531            })
532        }
533    }
534
535    struct FailAdapter;
536    impl ActionAdapter for FailAdapter {
537        fn name(&self) -> &'static str {
538            "fail"
539        }
540        fn execute(&self, _authorized: &AuthorizedAction) -> Result<ActionOutcome, AdapterError> {
541            Err(AdapterError::SimulatedResourceUnavailable(
542                "disk full".into(),
543            ))
544        }
545    }
546
547    // ============================================================
548    // ProfileSummaryWorker tests
549    // ============================================================
550
551    #[test]
552    fn worker_with_no_summarizer_never_starts() {
553        let mut worker = ProfileSummaryWorker::new(None, 10);
554        let mut memory = ModelMemoryStore::new(5);
555        advance_memory_to_windows(&mut memory, 10);
556
557        worker.maybe_start(&memory);
558        assert!(worker.pending.is_none());
559        // poll should also no-op; no llm_summary set
560        worker.poll(&mut memory);
561        let summary = memory.behavior_profile().summary;
562        assert!(
563            !summary.contains("llm_summary="),
564            "expected no llm_summary in profile, got: {summary}"
565        );
566    }
567
568    #[test]
569    fn poll_noops_when_no_pending_job() {
570        let mut worker = ProfileSummaryWorker::new(None, 10);
571        let mut memory = ModelMemoryStore::new(5);
572
573        worker.poll(&mut memory);
574        let summary = memory.behavior_profile().summary;
575        assert!(
576            !summary.contains("llm_summary="),
577            "expected no llm_summary in profile, got: {summary}"
578        );
579    }
580
581    #[test]
582    fn maybe_start_skips_before_interval() {
583        let mut worker = ProfileSummaryWorker::with_summarizer_fn(5, |_, _| Ok("hello".into()));
584        let mut memory = ModelMemoryStore::new(5);
585        // 3 windows, interval is 5
586        advance_memory_to_windows(&mut memory, 3);
587
588        worker.maybe_start(&memory);
589        assert!(worker.pending.is_none());
590    }
591
592    #[test]
593    fn maybe_start_spawns_when_interval_met() {
594        let mut worker = ProfileSummaryWorker::with_summarizer_fn(5, |_, _| Ok("hello".into()));
595        let mut memory = ModelMemoryStore::new(5);
596        advance_memory_to_windows(&mut memory, 25); // multiple of 5
597
598        worker.maybe_start(&memory);
599        assert!(worker.pending.is_some());
600    }
601
602    #[test]
603    fn maybe_start_skips_when_pending_exists() {
604        let mut worker = ProfileSummaryWorker::with_summarizer_fn(5, |_, _| Ok("hello".into()));
605        let mut memory = ModelMemoryStore::new(5);
606        advance_memory_to_windows(&mut memory, 10);
607
608        // first call spawns
609        worker.maybe_start(&memory);
610        assert!(worker.pending.is_some());
611        // second call skips because pending is still Some
612        worker.maybe_start(&memory);
613        assert!(worker.pending.is_some());
614    }
615
616    #[test]
617    fn maybe_start_skips_same_last_started_window() {
618        let mut worker = ProfileSummaryWorker::with_summarizer_fn(5, |_, _| Ok("hello".into()));
619        let mut memory = ModelMemoryStore::new(5);
620        advance_memory_to_windows(&mut memory, 10);
621
622        // spawn and complete
623        worker.maybe_start(&memory);
624        // give the thread a moment to finish
625        std::thread::sleep(std::time::Duration::from_millis(10));
626        worker.poll(&mut memory);
627
628        // observation_windows is still 10, same as last_started_window
629        worker.maybe_start(&memory);
630        assert!(worker.pending.is_none());
631    }
632
633    #[test]
634    fn poll_collects_finished_job_and_sets_summary() {
635        let mut worker = ProfileSummaryWorker::with_summarizer_fn(5, |_, _| {
636            Ok("compressed behavior profile".into())
637        });
638        let mut memory = ModelMemoryStore::new(5);
639        advance_memory_to_windows(&mut memory, 10);
640
641        worker.maybe_start(&memory);
642        // wait for the thread to complete
643        std::thread::sleep(std::time::Duration::from_millis(20));
644        worker.poll(&mut memory);
645
646        let summary = memory.behavior_profile().summary;
647        assert!(
648            summary.starts_with("llm_summary=compressed behavior profile;"),
649            "expected llm summary in profile, got: {summary}"
650        );
651        assert!(worker.pending.is_none());
652        assert_eq!(worker.last_started_window, 10);
653    }
654
655    #[test]
656    fn poll_handles_failed_summarization_gracefully() {
657        let mut worker =
658            ProfileSummaryWorker::with_summarizer_fn(5, |_, _| Err("summarization failed".into()));
659        let mut memory = ModelMemoryStore::new(5);
660        advance_memory_to_windows(&mut memory, 10);
661
662        worker.maybe_start(&memory);
663        std::thread::sleep(std::time::Duration::from_millis(20));
664        worker.poll(&mut memory);
665
666        // llm_summary should not be set after a failed summarization
667        let summary = memory.behavior_profile().summary;
668        assert!(
669            !summary.contains("llm_summary="),
670            "expected no llm_summary after failure, got: {summary}"
671        );
672        assert!(worker.pending.is_none());
673    }
674
675    // ============================================================
676    // process_window tests
677    // ============================================================
678
679    fn make_deps<'a>(
680        router: &'a DecisionRouter,
681        lifecycle: &'a ActionLifecycle<'a>,
682        memory: &'a mut ModelMemoryStore,
683        memory_config: &'a ModelMemoryConfig,
684        trace_recorder: Option<&'a mut RuntimeTraceRecorder>,
685    ) -> WindowProcessingDeps<'a> {
686        WindowProcessingDeps {
687            router,
688            lifecycle,
689            memory,
690            memory_config,
691            profile_summary_worker: None,
692            trace_recorder,
693        }
694    }
695
696    #[test]
697    fn process_window_increments_observation_windows() {
698        let router = DecisionRouter::default();
699        let policy = PolicyEngine::default();
700        let adapter = OkAdapter;
701        let lifecycle = ActionLifecycle::new(&policy, &adapter);
702        let mut memory = ModelMemoryStore::new(5);
703        let config = ModelMemoryConfig::default();
704        let stats = RawEventStats::default();
705        let ctx = empty_context("w-test");
706
707        let before = memory.observation_windows();
708        let mut deps = make_deps(&router, &lifecycle, &mut memory, &config, None);
709        process_window(0, &ctx, &stats, &mut deps);
710        assert_eq!(memory.observation_windows(), before + 1);
711    }
712
713    #[test]
714    fn process_window_handles_empty_context_without_panic() {
715        let router = DecisionRouter::default();
716        let policy = PolicyEngine::default();
717        let adapter = OkAdapter;
718        let lifecycle = ActionLifecycle::new(&policy, &adapter);
719        let mut memory = ModelMemoryStore::new(5);
720        let config = ModelMemoryConfig::default();
721        let stats = RawEventStats::default();
722        let ctx = empty_context("w-empty");
723
724        let mut deps = make_deps(&router, &lifecycle, &mut memory, &config, None);
725        process_window(0, &ctx, &stats, &mut deps);
726        // no panic
727    }
728
729    #[test]
730    fn process_window_with_failing_adapter_does_not_panic() {
731        let router = DecisionRouter::default();
732        let policy = PolicyEngine::default();
733        let adapter = FailAdapter;
734        let lifecycle = ActionLifecycle::new(&policy, &adapter);
735        let mut memory = ModelMemoryStore::new(5);
736        let config = ModelMemoryConfig::default();
737        let stats = RawEventStats::default();
738        let ctx = empty_context("w-fail");
739
740        let mut deps = make_deps(&router, &lifecycle, &mut memory, &config, None);
741        process_window(0, &ctx, &stats, &mut deps);
742        // no panic on adapter failure
743    }
744
745    #[test]
746    fn process_window_writes_trace_record() {
747        let path = std::env::temp_dir().join(format!(
748            "dipecs-daemon-trace-{}.ndjson",
749            SystemTime::now()
750                .duration_since(UNIX_EPOCH)
751                .unwrap()
752                .as_nanos()
753        ));
754        let mut recorder = RuntimeTraceRecorder::new(&path).unwrap();
755
756        let router = DecisionRouter::default();
757        let policy = PolicyEngine::default();
758        let adapter = OkAdapter;
759        let lifecycle = ActionLifecycle::new(&policy, &adapter);
760        let mut memory = ModelMemoryStore::new(5);
761        let config = ModelMemoryConfig::default();
762        let stats = RawEventStats::default();
763        let ctx = empty_context("w-trace");
764
765        let mut deps = make_deps(
766            &router,
767            &lifecycle,
768            &mut memory,
769            &config,
770            Some(&mut recorder),
771        );
772        process_window(0, &ctx, &stats, &mut deps);
773        drop(recorder);
774
775        let mut content = String::new();
776        std::fs::File::open(&path)
777            .unwrap()
778            .read_to_string(&mut content)
779            .unwrap();
780        let lines: Vec<&str> = content.lines().collect();
781        assert_eq!(lines.len(), 1, "expected exactly one NDJSON line");
782
783        let parsed: serde_json::Value = serde_json::from_str(lines[0]).unwrap();
784        assert_eq!(parsed["stage"], "daemon_window");
785        assert_eq!(parsed["window_ordinal"], 0);
786        assert_eq!(parsed["window_id"], "w-trace");
787        assert!(parsed["audit"].is_array());
788
789        let _ = std::fs::remove_file(path);
790    }
791
792    // ============================================================
793    // run_processing_loop 端到端测试: 真实 ActionBus 双任务通道
794    //
795    // 用真实 mpsc 通道连起「producer 任务 (Task-1 类比) → run_processing_loop
796    // (Task-2)」, 驱动真实 sanitize → 窗口聚合 → 决策 → 策略 → 执行 → 审计 → trace
797    // 全链路。终止由通道关闭驱动 (producer drop sender), 确定性收尾。
798    // ============================================================
799
800    const APP_TRANSITION_LINE: &str = r#"{"eventId":"evt-1","timestampMs":1000,"source":"UsageCollector","eventType":"app_transition","rawEvent":{"AppTransition":{"timestamp_ms":1000,"package_name":"com.android.chrome","activity_class":"MainActivity","transition":"Foreground"}},"rawPayload":{}}"#;
801
802    const SYSTEM_LOW_BATTERY_LINE: &str = r#"{"eventId":"evt-4","timestampMs":4000,"source":"CollectorForegroundService","eventType":"system_state","rawEvent":{"SystemState":{"timestamp_ms":4000,"battery_pct":8,"is_charging":false,"network":"Wifi","ringer_mode":"Normal","location_type":"Unknown","headphone_connected":false,"bluetooth_connected":false}},"rawPayload":{}}"#;
803
804    const NOTIFICATION_FILE_LINE: &str = r#"{"eventId":"evt-2","timestampMs":2000,"source":"NotificationCollectorService","eventType":"notification_posted","rawEvent":{"NotificationPosted":{"timestamp_ms":2000,"package_name":"com.ss.android.lark","category":"msg","channel_id":"lark_im_message","raw_title":"Zhang San","raw_text":"sent a file: report.pdf","is_ongoing":false,"group_key":"conv_42","has_picture":false}},"rawPayload":{}}"#;
805
806    /// 走真实 ingress (与 daemon 采集任务同款) 把一行 Android JSONL 变成
807    /// 带 SourceTier 的 `IngestedRawEvent`, 供 producer 灌进真实通道。
808    fn ingest(line: &str) -> aios_spec::IngestedRawEvent {
809        let envelope = AndroidJsonlIngress::new()
810            .parse_line(line)
811            .expect("parse_line ok")
812            .expect("line carries a raw_event");
813        RustCollectorIngress
814            .accept(envelope)
815            .expect("ingress accepts envelope")
816    }
817
818    fn temp_trace_path(tag: &str) -> std::path::PathBuf {
819        std::env::temp_dir().join(format!(
820            "dipecs-e2e-{tag}-{}.ndjson",
821            SystemTime::now()
822                .duration_since(UNIX_EPOCH)
823                .unwrap()
824                .as_nanos()
825        ))
826    }
827
828    /// T1: 两条事件经真实通道进入同一大窗口, 通道关闭时 flush 一次,
829    /// 产出审计并执行动作。窗口设 3600s → 只有关闭触发关窗, 与定时器无关。
830    #[tokio::test]
831    async fn dual_task_pipeline_processes_events_and_flushes_on_close() {
832        let (raw_tx, raw_rx, _itx, _irx) = ActionBus::new(64).split();
833
834        // producer (Task-1 类比): 发低电量 + app 切换, 发完 drop sender → 通道关闭。
835        let producer = tokio::spawn(async move {
836            raw_tx.send(ingest(SYSTEM_LOW_BATTERY_LINE)).await.unwrap();
837            raw_tx.send(ingest(APP_TRANSITION_LINE)).await.unwrap();
838        });
839
840        let trace_path = temp_trace_path("close");
841        let mut recorder = RuntimeTraceRecorder::new(&trace_path).unwrap();
842        let router = DecisionRouter::default();
843        let policy = PolicyEngine::default();
844        let adapter = DefaultActionExecutor::new();
845        let lifecycle = ActionLifecycle::new(&policy, &adapter);
846        let mut memory = ModelMemoryStore::new(5);
847        let config = ModelMemoryConfig::default();
848        let mut worker = ProfileSummaryWorker::new(None, 10);
849        let sanitizer = DefaultPrivacyAirGap;
850        let window = WindowAggregator::new(3600, timestamp_ms());
851
852        let summary = {
853            let mut deps = WindowProcessingDeps {
854                router: &router,
855                lifecycle: &lifecycle,
856                memory: &mut memory,
857                memory_config: &config,
858                profile_summary_worker: Some(&mut worker),
859                trace_recorder: Some(&mut recorder),
860            };
861            run_processing_loop(
862                raw_rx,
863                &sanitizer,
864                window,
865                Duration::from_secs(3600),
866                &mut deps,
867            )
868            .await
869        };
870        producer.await.unwrap();
871        drop(recorder);
872
873        assert_eq!(summary.terminated_by, LoopTermination::ChannelClosed);
874        assert_eq!(summary.events_processed, 2);
875        assert_eq!(
876            summary.windows_closed, 1,
877            "两条事件在同一大窗口 → flush 一次"
878        );
879
880        let content = std::fs::read_to_string(&trace_path).unwrap();
881        let lines: Vec<&str> = content.lines().collect();
882        assert_eq!(lines.len(), 1, "flush 一次 → 一行 NDJSON");
883        let rec: serde_json::Value = serde_json::from_str(lines[0]).unwrap();
884        assert_eq!(rec["stage"], "daemon_window");
885        assert_eq!(rec["window_ordinal"], 0);
886        let audit = rec["audit"].as_array().expect("audit is array");
887        assert!(!audit.is_empty(), "窗口应产出审计记录");
888        let succeeded = audit
889            .iter()
890            .filter(|r| r["terminal"] == "Succeeded")
891            .count();
892        assert!(
893            succeeded >= 1,
894            "低电量上下文应至少授权并执行一个动作, audit={audit:?}"
895        );
896
897        let _ = std::fs::remove_file(&trace_path);
898    }
899
900    /// T2: 带原文 PII 的通知经真实通道后, daemon trace 里不得出现原文,
901    /// 但保留脱敏后的语义提示 → 真实通道上守住 PrivacyAirGap 边界。
902    #[tokio::test]
903    async fn dual_task_pipeline_scrubs_pii_across_channel() {
904        let (raw_tx, raw_rx, _itx, _irx) = ActionBus::new(64).split();
905        let producer = tokio::spawn(async move {
906            raw_tx.send(ingest(NOTIFICATION_FILE_LINE)).await.unwrap();
907        });
908
909        let trace_path = temp_trace_path("pii");
910        let mut recorder = RuntimeTraceRecorder::new(&trace_path).unwrap();
911        let router = DecisionRouter::default();
912        let policy = PolicyEngine::default();
913        let adapter = DefaultActionExecutor::new();
914        let lifecycle = ActionLifecycle::new(&policy, &adapter);
915        let mut memory = ModelMemoryStore::new(5);
916        let config = ModelMemoryConfig::default();
917        let mut worker = ProfileSummaryWorker::new(None, 10);
918        let sanitizer = DefaultPrivacyAirGap;
919        let window = WindowAggregator::new(3600, timestamp_ms());
920
921        let summary = {
922            let mut deps = WindowProcessingDeps {
923                router: &router,
924                lifecycle: &lifecycle,
925                memory: &mut memory,
926                memory_config: &config,
927                profile_summary_worker: Some(&mut worker),
928                trace_recorder: Some(&mut recorder),
929            };
930            run_processing_loop(
931                raw_rx,
932                &sanitizer,
933                window,
934                Duration::from_secs(3600),
935                &mut deps,
936            )
937            .await
938        };
939        producer.await.unwrap();
940        drop(recorder);
941
942        assert_eq!(summary.events_processed, 1);
943        assert_eq!(summary.windows_closed, 1);
944
945        let content = std::fs::read_to_string(&trace_path).unwrap();
946        for pii in ["Zhang San", "report.pdf", "sent a file"] {
947            assert!(
948                !content.contains(pii),
949                "原文 PII '{pii}' 泄漏进 daemon trace: {content}"
950            );
951        }
952        assert!(
953            content.contains("FileMention"),
954            "脱敏应保留 FileMention 语义提示: {content}"
955        );
956
957        let _ = std::fs::remove_file(&trace_path);
958    }
959
960    /// T3: 空事件流 (producer 立刻 drop sender) 应干净收尾, 不 panic, 不写 trace。
961    #[tokio::test]
962    async fn dual_task_pipeline_empty_stream_closes_cleanly() {
963        let (raw_tx, raw_rx, _itx, _irx) = ActionBus::new(64).split();
964        drop(raw_tx); // 不发任何事件, 立刻关闭通道
965
966        let trace_path = temp_trace_path("empty");
967        let mut recorder = RuntimeTraceRecorder::new(&trace_path).unwrap();
968        let router = DecisionRouter::default();
969        let policy = PolicyEngine::default();
970        let adapter = DefaultActionExecutor::new();
971        let lifecycle = ActionLifecycle::new(&policy, &adapter);
972        let mut memory = ModelMemoryStore::new(5);
973        let config = ModelMemoryConfig::default();
974        let mut worker = ProfileSummaryWorker::new(None, 10);
975        let sanitizer = DefaultPrivacyAirGap;
976        let window = WindowAggregator::new(3600, timestamp_ms());
977
978        let summary = {
979            let mut deps = WindowProcessingDeps {
980                router: &router,
981                lifecycle: &lifecycle,
982                memory: &mut memory,
983                memory_config: &config,
984                profile_summary_worker: Some(&mut worker),
985                trace_recorder: Some(&mut recorder),
986            };
987            run_processing_loop(
988                raw_rx,
989                &sanitizer,
990                window,
991                Duration::from_secs(3600),
992                &mut deps,
993            )
994            .await
995        };
996        drop(recorder);
997
998        assert_eq!(summary.terminated_by, LoopTermination::ChannelClosed);
999        assert_eq!(summary.events_processed, 0);
1000        assert_eq!(summary.windows_closed, 0, "空窗口不关窗");
1001
1002        let content = std::fs::read_to_string(&trace_path).unwrap_or_default();
1003        assert!(content.trim().is_empty(), "空流不应写 trace: {content}");
1004
1005        let _ = std::fs::remove_file(&trace_path);
1006    }
1007
1008    /// T4: 定时器驱动的多窗口轮转。暂停时钟下, 每灌一批事件后推进时钟越过窗口,
1009    /// 由 WindowExpired 分支关窗。`join!` 让 producer 与循环在同一任务并发,
1010    /// 规避 spawn 的 'static 借用限制; 推进前事件已排空 → 无 select 竞态。
1011    #[tokio::test(start_paused = true)]
1012    async fn dual_task_pipeline_rotates_windows_on_timer() {
1013        let (raw_tx, raw_rx, _itx, _irx) = ActionBus::new(64).split();
1014
1015        let router = DecisionRouter::default();
1016        let policy = PolicyEngine::default();
1017        let adapter = DefaultActionExecutor::new();
1018        let lifecycle = ActionLifecycle::new(&policy, &adapter);
1019        let mut memory = ModelMemoryStore::new(5);
1020        let config = ModelMemoryConfig::default();
1021        let mut worker = ProfileSummaryWorker::new(None, 10);
1022        let sanitizer = DefaultPrivacyAirGap;
1023        let window_dur = Duration::from_secs(10);
1024        let window = WindowAggregator::new(10, timestamp_ms());
1025
1026        let mut deps = WindowProcessingDeps {
1027            router: &router,
1028            lifecycle: &lifecycle,
1029            memory: &mut memory,
1030            memory_config: &config,
1031            profile_summary_worker: Some(&mut worker),
1032            trace_recorder: None,
1033        };
1034
1035        let control = async move {
1036            // W1: 发事件 → 越过窗口 → 定时器关 W1。
1037            raw_tx.send(ingest(APP_TRANSITION_LINE)).await.unwrap();
1038            tokio::time::sleep(Duration::from_secs(11)).await;
1039            // W2: 再发再越过 → 关 W2。
1040            raw_tx.send(ingest(SYSTEM_LOW_BATTERY_LINE)).await.unwrap();
1041            tokio::time::sleep(Duration::from_secs(11)).await;
1042            drop(raw_tx); // 通道关闭 → 循环 flush(空) + 退出。
1043        };
1044
1045        let (summary, ()) = tokio::join!(
1046            run_processing_loop(raw_rx, &sanitizer, window, window_dur, &mut deps),
1047            control,
1048        );
1049
1050        assert_eq!(summary.terminated_by, LoopTermination::ChannelClosed);
1051        assert_eq!(summary.events_processed, 2);
1052        assert_eq!(summary.windows_closed, 2, "两个非空窗口各由定时器关一次");
1053    }
1054
1055    // ============================================================
1056    // 真·双任务端到端: 真实 Task-1 (run_collection_loop) + 真实通道 +
1057    // 真实 Task-2 (run_processing_loop) + 真实 shutdown 广播链。
1058    // 只有「事件从哪来」是替身 (MockRawEventSource): 真实采集器要读真机
1059    // /proc、电量、Binder, 不可确定性单测。
1060    // ============================================================
1061
1062    /// 合成事件源: 第一次 poll 交出预置事件, 之后每 tick 返回空。
1063    struct MockRawEventSource {
1064        pending: Vec<aios_spec::IngestedRawEvent>,
1065    }
1066
1067    impl RawEventSource for MockRawEventSource {
1068        fn poll(&mut self, _now_ms: i64) -> Vec<aios_spec::IngestedRawEvent> {
1069            std::mem::take(&mut self.pending)
1070        }
1071    }
1072
1073    /// T5: 两个任务都是真的。真实采集循环把合成源的事件推进真实 `ActionBus` 通道,
1074    /// 真实处理循环消费; 用真实 shutdown 广播触发收尾链:
1075    /// broadcast → Task-1 `try_recv` → return → drop sender → 通道关闭 → Task-2 flush。
1076    ///
1077    /// 确定性来自采集循环「先 poll 后查 shutdown」+ mpsc「关闭前必先交付缓冲」:
1078    /// 无论 shutdown 何时触发, 首次 poll 的事件都会先入通道并被处理。
1079    #[tokio::test]
1080    async fn dual_task_full_pipeline_real_shutdown_chain() {
1081        let (raw_tx, raw_rx, _itx, _irx) = ActionBus::new(64).split();
1082        let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
1083
1084        // 真实 Task-1: run_collection_loop + 合成源 (发两条事件, 之后每 tick 空)。
1085        let source = MockRawEventSource {
1086            pending: vec![ingest(SYSTEM_LOW_BATTERY_LINE), ingest(APP_TRANSITION_LINE)],
1087        };
1088        let collect_handle = tokio::spawn(run_collection_loop(
1089            Box::new(source),
1090            raw_tx,
1091            shutdown_rx,
1092            Duration::from_millis(1),
1093        ));
1094
1095        // 真实 Task-2 依赖。
1096        let trace_path = temp_trace_path("dual");
1097        let mut recorder = RuntimeTraceRecorder::new(&trace_path).unwrap();
1098        let router = DecisionRouter::default();
1099        let policy = PolicyEngine::default();
1100        let adapter = DefaultActionExecutor::new();
1101        let lifecycle = ActionLifecycle::new(&policy, &adapter);
1102        let mut memory = ModelMemoryStore::new(5);
1103        let config = ModelMemoryConfig::default();
1104        let mut worker = ProfileSummaryWorker::new(None, 10);
1105        let sanitizer = DefaultPrivacyAirGap;
1106        let window = WindowAggregator::new(3600, timestamp_ms());
1107
1108        // 触发真实 shutdown 链。
1109        let control = async {
1110            shutdown_tx.send(()).unwrap();
1111        };
1112
1113        let summary = {
1114            let mut deps = WindowProcessingDeps {
1115                router: &router,
1116                lifecycle: &lifecycle,
1117                memory: &mut memory,
1118                memory_config: &config,
1119                profile_summary_worker: Some(&mut worker),
1120                trace_recorder: Some(&mut recorder),
1121            };
1122            let (summary, ()) = tokio::join!(
1123                run_processing_loop(
1124                    raw_rx,
1125                    &sanitizer,
1126                    window,
1127                    Duration::from_secs(3600),
1128                    &mut deps,
1129                ),
1130                control,
1131            );
1132            summary
1133        };
1134        collect_handle.await.unwrap();
1135        drop(recorder);
1136
1137        assert_eq!(summary.terminated_by, LoopTermination::ChannelClosed);
1138        assert_eq!(
1139            summary.events_processed, 2,
1140            "两条采集事件穿过真实通道被处理"
1141        );
1142        assert_eq!(summary.windows_closed, 1);
1143
1144        // trace 里应有真实审计记录。
1145        let content = std::fs::read_to_string(&trace_path).unwrap();
1146        assert_eq!(content.lines().count(), 1);
1147        let rec: serde_json::Value = serde_json::from_str(content.lines().next().unwrap()).unwrap();
1148        assert_eq!(rec["stage"], "daemon_window");
1149        assert!(rec["audit"].as_array().is_some_and(|a| !a.is_empty()));
1150
1151        let _ = std::fs::remove_file(&trace_path);
1152    }
1153}