| |
| |
| |
| |
| |
| |
|
|
| use chrono::{DateTime, Utc}; |
| use serde::{Deserialize, Serialize}; |
| use std::collections::VecDeque; |
|
|
| |
| |
| #[derive(Debug, Clone, Serialize, Deserialize)] |
| pub struct Session { |
| pub action_count: u64, |
| pub last_tool: Option<String>, |
| pub last_result: Option<String>, |
| pub last_file: Option<String>, |
| pub started: DateTime<Utc>, |
| pub last_action: Option<DateTime<Utc>>, |
| pub complexity_history: Vec<ComplexityEntry>, |
| pub manifest: Vec<ManifestEntry>, |
| pub failures: Vec<FailureEntry>, |
| |
| #[serde(default)] |
| pub rate_window: Vec<DateTime<Utc>>, |
| |
| |
| #[serde(default)] |
| pub recent_reads: Vec<String>, |
| |
| |
| #[serde(default)] |
| pub files_read: VecDeque<String>, |
| |
| |
| #[serde(default)] |
| pub files_written: VecDeque<String>, |
| } |
|
|
| #[derive(Debug, Clone, Serialize, Deserialize)] |
| pub struct ComplexityEntry { |
| pub timestamp: DateTime<Utc>, |
| pub tool: String, |
| pub c: u64, |
| pub tier: String, |
| } |
|
|
| #[derive(Debug, Clone, Serialize, Deserialize)] |
| pub struct ManifestEntry { |
| pub timestamp: DateTime<Utc>, |
| pub tool: String, |
| pub c: u64, |
| pub action: String, |
| pub reason: Option<String>, |
| |
| #[serde(default)] |
| pub command: Option<String>, |
| |
| #[serde(default)] |
| pub targets: Vec<String>, |
| } |
|
|
| #[derive(Debug, Clone, Serialize, Deserialize)] |
| pub struct FailureEntry { |
| pub timestamp: DateTime<Utc>, |
| pub tool: String, |
| pub error: String, |
| } |
|
|
| impl Session { |
| pub fn new() -> Self { |
| Self { |
| action_count: 0, |
| last_tool: None, |
| last_result: None, |
| last_file: None, |
| started: Utc::now(), |
| last_action: None, |
| complexity_history: Vec::new(), |
| manifest: Vec::new(), |
| failures: Vec::new(), |
| rate_window: Vec::new(), |
| recent_reads: Vec::new(), |
| files_read: VecDeque::new(), |
| files_written: VecDeque::new(), |
| } |
| } |
|
|
| |
| |
| pub fn track_read(&mut self, path: &str) { |
| let canonical = match std::fs::canonicalize(path) { |
| Ok(p) => p.to_string_lossy().to_string(), |
| Err(_) => path.to_string(), |
| }; |
| |
| if !self.recent_reads.contains(&canonical) { |
| self.recent_reads.push(canonical.clone()); |
| if self.recent_reads.len() > 20 { |
| self.recent_reads.remove(0); |
| } |
| } |
| |
| if !self.files_read.contains(&canonical) { |
| self.files_read.push_back(canonical); |
| if self.files_read.len() > 50 { |
| self.files_read.pop_front(); |
| } |
| } |
| } |
|
|
| |
| |
| |
| pub fn track_write(&mut self, path: &str) { |
| let canonical = match std::fs::canonicalize(path) { |
| Ok(p) => p.to_string_lossy().to_string(), |
| Err(_) => path.to_string(), |
| }; |
| if !self.files_written.contains(&canonical) { |
| self.files_written.push_back(canonical); |
| if self.files_written.len() > 50 { |
| self.files_written.pop_front(); |
| } |
| } |
| } |
|
|
| |
| pub fn record_action(&mut self, tool: &str, result: &str, file_path: Option<&str>) { |
| self.action_count += 1; |
| self.last_tool = Some(tool.to_string()); |
| self.last_result = Some(result.to_string()); |
| self.last_file = file_path.map(|s| s.to_string()); |
| let now = Utc::now(); |
| self.last_action = Some(now); |
|
|
| |
| self.rate_window.push(now); |
| let one_minute_ago = now - chrono::Duration::seconds(60); |
| self.rate_window.retain(|ts| *ts > one_minute_ago); |
| } |
|
|
| |
| pub fn record_complexity(&mut self, tool: &str, c: u64, tier: &str) { |
| self.complexity_history.push(ComplexityEntry { |
| timestamp: Utc::now(), |
| tool: tool.to_string(), |
| c, |
| tier: tier.to_string(), |
| }); |
| |
| if self.complexity_history.len() > 100 { |
| self.complexity_history.remove(0); |
| } |
| } |
|
|
| |
| |
| pub fn record_manifest(&mut self, tool: &str, c: u64, action: &str, reason: Option<&str>) { |
| self.record_manifest_detailed(tool, c, action, reason, None, &[]); |
| } |
|
|
| |
| |
| |
| pub fn record_manifest_detailed( |
| &mut self, |
| tool: &str, |
| c: u64, |
| action: &str, |
| reason: Option<&str>, |
| command: Option<&str>, |
| targets: &[String], |
| ) { |
| self.manifest.push(ManifestEntry { |
| timestamp: Utc::now(), |
| tool: tool.to_string(), |
| c, |
| action: action.to_string(), |
| reason: reason.map(|s| s.to_string()), |
| command: command.map(|s| s.to_string()), |
| targets: targets.to_vec(), |
| }); |
| if self.manifest.len() > 200 { |
| self.manifest.remove(0); |
| } |
| } |
|
|
| |
| pub fn record_failure(&mut self, tool: &str, error: &str) { |
| self.failures.push(FailureEntry { |
| timestamp: Utc::now(), |
| tool: tool.to_string(), |
| error: error.to_string(), |
| }); |
| if self.failures.len() > 50 { |
| self.failures.remove(0); |
| } |
| } |
|
|
| |
| |
| pub fn anchor_ratio(&self, file_read_count: usize, file_write_count: usize) -> String { |
| if file_write_count == 0 { |
| "N/A (no writes)".to_string() |
| } else { |
| format!("{}/{}", file_read_count, file_write_count) |
| } |
| } |
|
|
| |
| |
| pub fn status_summary(&self, file_read_count: usize, file_write_count: usize) -> String { |
| format!( |
| "Actions: {} | Reads: {} | Writes: {} | Last: {} | Anchor: {}", |
| self.action_count, |
| file_read_count, |
| file_write_count, |
| self.last_tool.as_deref().unwrap_or("none"), |
| self.anchor_ratio(file_read_count, file_write_count), |
| ) |
| } |
|
|
| } |
|
|
| impl Default for Session { |
| fn default() -> Self { |
| Self::new() |
| } |
| } |
|
|