Skip to main content

kaizen/retro/
types.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2//! Pure data for the retro engine (`Report`, `Bet`, `Inputs`).
3
4use crate::core::event::{Event, SessionRecord};
5use crate::metrics::types::{FileFact, ToolSpanView};
6use serde::{Deserialize, Serialize};
7use std::collections::{HashMap, HashSet};
8
9/// Workspace-local facts assembled once at the IO boundary.
10#[derive(Debug, Clone)]
11pub struct Inputs {
12    pub window_start_ms: u64,
13    pub window_end_ms: u64,
14    /// Joined rows time-ordered.
15    pub events: Vec<(SessionRecord, Event)>,
16    pub files_touched: Vec<(String, String)>,
17    pub skills_used: Vec<(String, String)>,
18    pub tool_spans: Vec<ToolSpanView>,
19    /// Skills referenced in the last `usage_lookback_ms` window (for H1).
20    pub skills_used_recent_slugs: HashSet<String>,
21    pub usage_lookback_ms: u64,
22    pub skill_files_on_disk: Vec<SkillFileOnDisk>,
23    /// `.cursor/rules/*.mdc` stems (same shape as [`SkillFileOnDisk`]).
24    pub rule_files_on_disk: Vec<SkillFileOnDisk>,
25    pub rules_used_recent_slugs: HashSet<String>,
26    pub file_facts: HashMap<String, FileFact>,
27    pub aggregates: RetroAggregates,
28    /// LLM-as-Judge eval scores for sessions in the window: (session_id, score 0..1).
29    pub eval_scores: Vec<(String, f64)>,
30}
31
32#[derive(Debug, Clone)]
33pub struct SkillFileOnDisk {
34    pub slug: String,
35    /// Bytes of frontmatter + body (rough token proxy).
36    pub size_bytes: u64,
37    pub mtime_ms: u64,
38}
39
40#[derive(Debug, Clone, Default)]
41pub struct RetroAggregates {
42    pub unique_session_ids: HashSet<String>,
43    pub tool_event_counts: HashMap<String, u64>,
44    pub tool_cost_usd_e6: HashMap<String, i64>,
45    pub model_session_counts: HashMap<String, u64>,
46    pub total_cost_usd_e6: i64,
47}
48
49/// One ranked improvement bet.
50#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
51pub struct Bet {
52    /// Stable id for dedup (`H2:foo.rs|bar.rs`).
53    pub id: String,
54    pub heuristic_id: String,
55    pub title: String,
56    pub hypothesis: String,
57    pub expected_tokens_saved_per_week: f64,
58    pub effort_minutes: u32,
59    pub evidence: Vec<String>,
60    pub apply_step: String,
61    #[serde(default)]
62    pub evidence_recency_ms: u64,
63}
64
65impl Bet {
66    pub fn score(&self) -> f64 {
67        self.expected_tokens_saved_per_week / (self.effort_minutes as f64 + 1.0)
68    }
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
72pub struct RetroMeta {
73    pub week_label: String,
74    pub span_start_ms: u64,
75    pub span_end_ms: u64,
76    pub session_count: u64,
77    pub total_cost_usd_e6: i64,
78}
79
80#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
81pub struct RetroStats {
82    pub sessions: u64,
83    pub total_cost_usd_e6: i64,
84    pub top_model: Option<String>,
85    pub top_model_pct: Option<u64>,
86    pub top_tool: Option<String>,
87    pub top_tool_pct: Option<u64>,
88    pub median_session_minutes: Option<u64>,
89}
90
91/// JSON + markdown source of truth for CLI `--json` and reports.
92#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
93pub struct Report {
94    pub meta: RetroMeta,
95    pub top_bets: Vec<Bet>,
96    pub skipped_deduped: Vec<String>,
97    pub stats: RetroStats,
98}