1use std::collections::BTreeMap;
18use std::io::{BufRead, Write};
19
20use aios_action::OfflineAdapter;
21use aios_agent::DecisionRouter;
22use aios_core::action_lifecycle::ActionLifecycle;
23use aios_core::collector_ingress::RustCollectorIngress;
24use aios_core::context_builder::WindowAggregator;
25use aios_core::policy_engine::PolicyEngine;
26use aios_core::privacy_airgap::DefaultPrivacyAirGap;
27use aios_spec::governance::{ActionState, AuditRecord};
28use aios_spec::traits::PrivacySanitizer;
29use aios_spec::{
30 CapabilityLevel, CollectorEnvelope, DenialReason, IngestedRawEvent, RawEvent, SourceTier,
31 StructuredContext,
32};
33use anyhow::{Context, Result};
34use serde::Serialize;
35use serde_json::{json, Map, Value};
36use sha2::{Digest, Sha256};
37
38const SCHEMA_VERSION: &str = "dipecs.collector.v1";
39
40const VOLATILE_KEYS: &[&str] = &[
44 "event_id",
45 "window_id",
46 "intent_id",
47 "latency_us",
48 "backend_error",
49];
50
51#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
57pub enum Stage {
58 Ingest,
59 Sanitize,
60 Context,
61 Decision,
62 Policy,
63 Execute,
64}
65
66impl Stage {
67 fn includes(self, other: Stage) -> bool {
68 self >= other
69 }
70}
71
72#[derive(Debug, Default, Clone, Serialize, PartialEq, Eq)]
83pub struct ReplaySummary {
84 pub lines_total: u64,
85 pub lines_skipped_no_raw_event: u64,
86 pub lines_parse_error: u64,
87 pub events_ingested: u64,
88 pub windows_closed: u64,
89 pub intents_total: u64,
90 pub intents_approved: u64,
91 pub intents_rejected: u64,
92 pub actions_authorized: u64,
93 pub actions_denied: u64,
94 pub actions_failed: u64,
95 pub audit_records: u64,
96 pub denial_counts: BTreeMap<DenialReason, u64>,
97 pub audit_hash: String,
98}
99
100#[derive(Debug, Clone)]
103pub struct ReplayWithAudit {
104 pub summary: ReplaySummary,
105 pub audit_hash: String,
106}
107
108impl ReplaySummary {
109 pub fn human_summary(&self) -> String {
118 use std::fmt::Write as _;
119 let mut out = String::new();
120 let _ = writeln!(out, "=== DiPECS replay ===");
121 let _ = writeln!(out, "audit {}", short_hash(&self.audit_hash));
122 let _ = writeln!(
123 out,
124 "input {} lines · {} ingested · {} skipped · {} parse errors",
125 self.lines_total,
126 self.events_ingested,
127 self.lines_skipped_no_raw_event,
128 self.lines_parse_error,
129 );
130 let _ = writeln!(out, "windows {} closed", self.windows_closed);
131 out.push('\n');
132 let _ = writeln!(
133 out,
134 "intents {} total · {} approved · {} rejected",
135 self.intents_total, self.intents_approved, self.intents_rejected,
136 );
137 let _ = writeln!(
138 out,
139 "actions {} authorized · {} denied · {} failed · {} audit records",
140 self.actions_authorized, self.actions_denied, self.actions_failed, self.audit_records,
141 );
142
143 if !self.denial_counts.is_empty() {
144 out.push('\n');
145 let _ = writeln!(out, "denials by reason");
146 let name_width = self
147 .denial_counts
148 .keys()
149 .map(|r| format!("{r:?}").len())
150 .max()
151 .unwrap_or(0);
152 for (reason, count) in &self.denial_counts {
153 let _ = writeln!(
154 out,
155 " {name:<width$} {count}",
156 name = format!("{reason:?}"),
157 width = name_width,
158 );
159 }
160 }
161 out
162 }
163}
164
165fn short_hash(audit_hash: &str) -> String {
166 if audit_hash.is_empty() {
167 return "(none)".into();
168 }
169 if let Some(hex) = audit_hash.strip_prefix("sha256:") {
170 if hex.len() > 16 {
171 return format!("sha256:{}… (full hash in NDJSON summary)", &hex[..16]);
172 }
173 }
174 audit_hash.to_string()
175}
176
177pub fn run<R: BufRead, W: Write>(
182 reader: R,
183 writer: &mut W,
184 window_secs: u64,
185 stage: Stage,
186) -> Result<ReplaySummary> {
187 let mut sink = std::io::sink();
188 let with_audit = run_with_audit(reader, writer, &mut sink, window_secs, stage)?;
189 Ok(with_audit.summary)
190}
191
192pub fn run_with_audit<R: BufRead, W: Write, A: Write>(
199 reader: R,
200 writer: &mut W,
201 audit: &mut A,
202 window_secs: u64,
203 stage: Stage,
204) -> Result<ReplayWithAudit> {
205 let ingress = RustCollectorIngress;
206 let sanitizer = DefaultPrivacyAirGap;
207 let router = DecisionRouter::default();
208 let policy = PolicyEngine::default();
209 let adapter = OfflineAdapter;
210 let lifecycle = ActionLifecycle::new(&policy, &adapter);
211 let mut summary = ReplaySummary::default();
212 let mut aggregator: Option<WindowAggregator> = None;
213 let mut last_captured_at_ms: i64 = 0;
214 let mut emitter = Emitter::new(writer, audit);
215 let mut window_ordinal = 0u32;
216
217 for (line_idx, line_result) in reader.lines().enumerate() {
218 let line_no = line_idx as u64 + 1;
219 let raw_line = line_result.with_context(|| format!("read line {line_no}"))?;
220 if raw_line.trim().is_empty() {
221 continue;
222 }
223 summary.lines_total += 1;
224
225 let line_value: Value = match serde_json::from_str(&raw_line) {
226 Ok(v) => v,
227 Err(e) => {
228 summary.lines_parse_error += 1;
229 emitter.emit(&json!({
230 "stage": "error",
231 "line": line_no,
232 "error": format!("invalid JSON: {e}"),
233 }))?;
234 continue;
235 },
236 };
237
238 let raw_event_value = line_value.get("rawEvent").cloned().unwrap_or(Value::Null);
239 if raw_event_value.is_null() {
240 summary.lines_skipped_no_raw_event += 1;
241 continue;
242 }
243
244 let raw_event: RawEvent = match serde_json::from_value(raw_event_value) {
245 Ok(r) => r,
246 Err(e) => {
247 summary.lines_parse_error += 1;
248 emitter.emit(&json!({
249 "stage": "error",
250 "line": line_no,
251 "error": format!("rawEvent does not match RawEvent shape: {e}"),
252 }))?;
253 continue;
254 },
255 };
256
257 let envelope = CollectorEnvelope {
258 schema_version: SCHEMA_VERSION.to_string(),
259 source: line_value
260 .get("source")
261 .and_then(Value::as_str)
262 .unwrap_or("apps.android-collector.replay")
263 .to_string(),
264 source_tier: SourceTier::PublicApi,
265 device_trace_id: line_value
266 .get("eventId")
267 .and_then(Value::as_str)
268 .map(String::from),
269 captured_at_ms: line_value
270 .get("timestampMs")
271 .and_then(Value::as_i64)
272 .unwrap_or(0),
273 received_at_ms: None,
274 raw_event,
275 };
276 let captured_at_ms = envelope.captured_at_ms;
277 last_captured_at_ms = last_captured_at_ms.max(captured_at_ms);
278
279 let ingested: IngestedRawEvent = ingress
280 .accept(envelope)
281 .with_context(|| format!("ingress.accept failed on line {line_no}"))?;
282 summary.events_ingested += 1;
283
284 if stage.includes(Stage::Ingest) {
285 let source_tier = &ingested.source_tier;
286 emitter.emit(&json!({
287 "stage": "ingest",
288 "line": line_no,
289 "source_tier": format!("{source_tier:?}"),
290 "raw_event_kind": raw_event_kind(&ingested.raw_event),
291 }))?;
292 }
293
294 let agg =
296 aggregator.get_or_insert_with(|| WindowAggregator::new(window_secs, captured_at_ms));
297 if agg.is_expired(captured_at_ms) {
298 if let Some(ctx) = agg.close(captured_at_ms) {
299 process_window(
300 window_ordinal,
301 &ctx,
302 &router,
303 &lifecycle,
304 stage,
305 &mut summary,
306 &mut emitter,
307 )?;
308 window_ordinal += 1;
309 }
310 }
311
312 if !stage.includes(Stage::Sanitize) {
313 continue;
314 }
315 let sanitized = sanitizer.sanitize_with_tier(ingested.raw_event, ingested.source_tier);
316 emitter.emit(&json!({
317 "stage": "sanitize",
318 "line": line_no,
319 "sanitized": sanitized,
320 }))?;
321 if stage.includes(Stage::Context) {
322 if let Some(agg) = aggregator.as_mut() {
324 agg.push(sanitized);
325 }
326 }
327 }
328
329 if let Some(mut agg) = aggregator {
331 if let Some(ctx) = agg.close(last_captured_at_ms) {
332 process_window(
333 window_ordinal,
334 &ctx,
335 &router,
336 &lifecycle,
337 stage,
338 &mut summary,
339 &mut emitter,
340 )?;
341 }
342 }
343
344 let audit_hash = emitter.finalize();
345 summary.audit_hash = audit_hash.clone();
346
347 serde_json::to_writer(
350 &mut *writer,
351 &json!({
352 "stage": "summary",
353 "summary": summary,
354 }),
355 )?;
356 writer.write_all(b"\n")?;
357
358 Ok(ReplayWithAudit {
359 summary,
360 audit_hash,
361 })
362}
363
364fn process_window(
365 window_ordinal: u32,
366 ctx: &StructuredContext,
367 router: &DecisionRouter,
368 lifecycle: &ActionLifecycle,
369 stage: Stage,
370 summary: &mut ReplaySummary,
371 emitter: &mut Emitter<'_>,
372) -> Result<()> {
373 summary.windows_closed += 1;
374
375 if stage.includes(Stage::Context) {
376 emitter.emit(&json!({
377 "stage": "context",
378 "window_id": ctx.window_id,
379 "window_start_ms": ctx.window_start_ms,
380 "window_end_ms": ctx.window_end_ms,
381 "duration_secs": ctx.duration_secs,
382 "event_count": ctx.events.len(),
383 "summary": ctx.summary,
384 }))?;
385 }
386
387 if !stage.includes(Stage::Decision) {
388 return Ok(());
389 }
390 let decision = router.evaluate(ctx);
391 summary.intents_total += decision.intent_batch.intents.len() as u64;
392 let route = &decision.route;
393 emitter.emit(&json!({
394 "stage": "decision",
395 "window_id": ctx.window_id,
396 "route": format!("{route:?}"),
397 "model": decision.intent_batch.model,
398 "intent_count": decision.intent_batch.intents.len(),
399 "rationale_tags": decision.rationale_tags,
400 "error": decision.error.clone(),
401 }))?;
402
403 if !stage.includes(Stage::Policy) {
404 return Ok(());
405 }
406 let capability = CapabilityLevel::for_route(decision.route);
407
408 if stage.includes(Stage::Execute) {
409 let audit_records = lifecycle.run(
411 window_ordinal,
412 &decision.intent_batch,
413 decision.route,
414 decision.error.clone(),
415 &capability,
416 ctx,
417 );
418
419 let mut approved_intents = 0u64;
424 let mut rejected_intents = 0u64;
425 for (intent_idx, _intent) in decision.intent_batch.intents.iter().enumerate() {
426 let intent_ordinal = intent_idx as u32;
427 let intent_records: Vec<&AuditRecord> = audit_records
428 .iter()
429 .filter(|r| r.coord.intent_ordinal == intent_ordinal)
430 .collect();
431 let any_authorized = intent_records
432 .iter()
433 .any(|r| matches!(r.terminal, ActionState::Succeeded | ActionState::Failed));
434 if any_authorized {
435 approved_intents += 1;
436 } else {
437 rejected_intents += 1;
438 }
439 }
440 summary.intents_approved += approved_intents;
441 summary.intents_rejected += rejected_intents;
442
443 for record in &audit_records {
444 summary.audit_records += 1;
445 match record.terminal {
446 ActionState::Succeeded | ActionState::Failed => {
447 summary.actions_authorized += 1;
448 },
449 ActionState::RejectedInvalidSchema
450 | ActionState::DeniedByCapability
451 | ActionState::DeniedByPolicy => {
452 summary.actions_denied += 1;
453 if let Some(reason) = record.denial_reason {
454 *summary.denial_counts.entry(reason).or_insert(0) += 1;
455 }
456 },
457 _ => {},
458 }
459 if matches!(record.terminal, ActionState::Failed) {
460 summary.actions_failed += 1;
461 }
462 }
463
464 emitter.emit(&json!({
465 "stage": "policy",
466 "window_id": ctx.window_id,
467 "window_ordinal": window_ordinal,
468 "intent_count": decision.intent_batch.intents.len(),
469 "audit_records": audit_records,
470 }))?;
471
472 for record in &audit_records {
473 let action_type = &record.action_type;
474 emitter.emit(&json!({
475 "stage": "execute",
476 "window_id": ctx.window_id,
477 "window_ordinal": window_ordinal,
478 "coord": record.coord,
479 "intent_id": record.intent_id,
480 "action_type": format!("{action_type:?}"),
481 "terminal": record.terminal,
482 "outcome": record.outcome,
483 "error": record.error,
484 }))?;
485 }
486 return Ok(());
487 }
488
489 let policy_decisions =
491 lifecycle
492 .policy()
493 .evaluate_batch_with_context(&decision.intent_batch, &capability, ctx);
494
495 let mut by_intent: std::collections::BTreeMap<
496 u32,
497 Vec<&aios_spec::governance::PolicyActionDecision>,
498 > = std::collections::BTreeMap::new();
499 for d in &policy_decisions {
500 by_intent.entry(d.intent_ordinal).or_default().push(d);
501 }
502
503 for (intent_idx, intent) in decision.intent_batch.intents.iter().enumerate() {
504 let intent_ordinal = intent_idx as u32;
505 let intent_decisions = by_intent
506 .get(&intent_ordinal)
507 .map(|v| v.as_slice())
508 .unwrap_or(&[]);
509 let approved_count = intent_decisions
510 .iter()
511 .filter(|d| matches!(d.verdict, aios_spec::governance::PolicyVerdict::Approved))
512 .count() as u64;
513 let denied_count = intent_decisions.len() as u64 - approved_count;
514
515 if approved_count > 0 {
516 summary.intents_approved += 1;
517 } else {
518 summary.intents_rejected += 1;
519 }
520 summary.actions_authorized += approved_count;
521 summary.actions_denied += denied_count;
522
523 let mut action_denials = Vec::new();
524 let mut approved_actions = Vec::new();
525 for d in intent_decisions {
526 match d.verdict {
527 aios_spec::governance::PolicyVerdict::Approved => {
528 if let Some(action) = intent.suggested_actions.get(d.action_ordinal as usize) {
529 approved_actions.push(json!({ "action": action }));
530 }
531 },
532 aios_spec::governance::PolicyVerdict::Denied(reason) => {
533 action_denials.push(reason);
534 *summary.denial_counts.entry(reason).or_insert(0) += 1;
535 },
536 }
537 }
538
539 emitter.emit(&json!({
540 "stage": "policy",
541 "window_id": ctx.window_id,
542 "window_ordinal": window_ordinal,
543 "intent_id": intent.intent_id,
544 "approved": approved_count > 0,
545 "action_denials": action_denials,
546 "approved_actions": approved_actions,
547 }))?;
548 }
549
550 Ok(())
551}
552
553struct Emitter<'a> {
558 writer: &'a mut dyn Write,
559 audit: &'a mut dyn Write,
560 hasher: Sha256,
561}
562
563impl<'a> Emitter<'a> {
564 fn new<W: Write, A: Write>(writer: &'a mut W, audit: &'a mut A) -> Self {
565 Self {
566 writer,
567 audit,
568 hasher: Sha256::new(),
569 }
570 }
571
572 fn emit(&mut self, value: &Value) -> Result<()> {
573 serde_json::to_writer(&mut *self.writer, value)?;
574 self.writer.write_all(b"\n")?;
575 let canonical = canonicalize(value);
576 let canonical_bytes = serde_json::to_vec(&canonical)?;
577 self.audit.write_all(&canonical_bytes)?;
578 self.audit.write_all(b"\n")?;
579 self.hasher.update(&canonical_bytes);
580 self.hasher.update(b"\n");
581 Ok(())
582 }
583
584 fn finalize(self) -> String {
585 format!("sha256:{:x}", self.hasher.finalize())
586 }
587}
588
589fn canonicalize(value: &Value) -> Value {
597 match value {
598 Value::Object(map) => {
599 let mut keys: Vec<&str> = map.keys().map(String::as_str).collect();
600 keys.sort();
601 let mut out = Map::new();
602 for k in keys {
603 if VOLATILE_KEYS.contains(&k) {
604 continue;
605 }
606 if let Some(v) = map.get(k) {
607 out.insert(k.to_string(), canonicalize(v));
608 }
609 }
610 Value::Object(out)
611 },
612 Value::Array(arr) => Value::Array(arr.iter().map(canonicalize).collect()),
613 other => other.clone(),
614 }
615}
616
617fn raw_event_kind(raw: &RawEvent) -> &'static str {
618 match raw {
619 RawEvent::AppTransition(_) => "AppTransition",
620 RawEvent::BinderTransaction(_) => "BinderTransaction",
621 RawEvent::ProcStateChange(_) => "ProcStateChange",
622 RawEvent::FileSystemAccess(_) => "FileSystemAccess",
623 RawEvent::NotificationPosted(_) => "NotificationPosted",
624 RawEvent::NotificationInteraction(_) => "NotificationInteraction",
625 RawEvent::ScreenState(_) => "ScreenState",
626 RawEvent::SystemState(_) => "SystemState",
627 }
628}
629
630#[cfg(test)]
631mod tests {
632 use super::*;
633
634 #[test]
635 fn human_summary_renders_all_blocks() {
636 let mut summary = ReplaySummary {
637 lines_total: 3,
638 events_ingested: 3,
639 windows_closed: 1,
640 intents_total: 3,
641 intents_approved: 2,
642 intents_rejected: 1,
643 actions_authorized: 2,
644 actions_denied: 2,
645 audit_hash: "sha256:3fd30718ffd3f8953c14f4b5030b8d6ac52fd3e598ac3c9ab050554c89c3ae14"
646 .into(),
647 ..Default::default()
648 };
649 summary
650 .denial_counts
651 .insert(DenialReason::ActionCapabilityDenied, 2);
652
653 let rendered = summary.human_summary();
654 assert!(rendered.contains("=== DiPECS replay ==="));
655 assert!(rendered.contains("sha256:3fd30718ffd3f895…"));
656 assert!(rendered.contains("3 lines · 3 ingested · 0 skipped · 0 parse errors"));
657 assert!(rendered.contains("windows 1 closed"));
658 assert!(rendered.contains("intents 3 total · 2 approved · 1 rejected"));
659 assert!(rendered.contains("actions 2 authorized · 2 denied · 0 failed · 0 audit records"));
660 assert!(rendered.contains("denials by reason"));
661 assert!(rendered.contains("ActionCapabilityDenied"));
662 assert!(
663 rendered
664 .lines()
665 .any(|l| l.trim() == "ActionCapabilityDenied 2"
666 || l.contains("ActionCapabilityDenied") && l.trim_end().ends_with('2')),
667 "denial row must show count; got:\n{rendered}"
668 );
669 }
670
671 #[test]
672 fn human_summary_omits_denials_block_when_empty() {
673 let summary = ReplaySummary {
674 lines_total: 1,
675 events_ingested: 1,
676 audit_hash: "sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
677 .into(),
678 ..Default::default()
679 };
680 let rendered = summary.human_summary();
681 assert!(
682 !rendered.contains("denials by reason"),
683 "empty denial_counts must not emit a section header; got:\n{rendered}"
684 );
685 }
686
687 #[test]
688 fn human_summary_handles_empty_audit_hash() {
689 let summary = ReplaySummary::default();
690 let rendered = summary.human_summary();
691 assert!(
692 rendered.contains("audit (none)"),
693 "empty audit hash should render '(none)' placeholder; got:\n{rendered}"
694 );
695 }
696
697 #[test]
698 fn human_summary_denial_table_aligns_widest_variant() {
699 let mut summary = ReplaySummary::default();
700 summary
701 .denial_counts
702 .insert(DenialReason::ConfidenceTooLow, 1);
703 summary
704 .denial_counts
705 .insert(DenialReason::BatchActionCapExceeded, 3);
706 let rendered = summary.human_summary();
707 let denial_lines: Vec<&str> = rendered
708 .lines()
709 .skip_while(|l| !l.starts_with("denials by reason"))
710 .skip(1)
711 .filter(|l| !l.is_empty())
712 .collect();
713 assert_eq!(denial_lines.len(), 2);
714 let widths: Vec<usize> = denial_lines
715 .iter()
716 .map(|l| l.find(|c: char| c.is_ascii_digit()).unwrap_or(0))
717 .collect();
718 assert_eq!(
719 widths[0], widths[1],
720 "count column must be aligned across denial rows; got lines:\n{denial_lines:#?}"
721 );
722 }
723}