git-iris 2.0.8

AI-powered Git workflow assistant for smart commits, code reviews, changelogs, and release notes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! History System for Iris Studio
//!
//! Single source of truth for all content changes, chat messages, and events.
//! Provides:
//! - Complete audit trail across all modes
//! - Content timeline per (mode, `content_type`)
//! - Chat history accessible from anywhere
//! - Event replay for debugging/undo
//! - Persistence metadata for future thread resume UI

use std::collections::{HashMap, VecDeque};
use std::path::PathBuf;
use std::time::Instant;

use chrono::{DateTime, Utc};
use uuid::Uuid;

use crate::types::GeneratedMessage;

use super::events::{ContentType, EventSource, TaskType, TimestampedEvent};

// ═══════════════════════════════════════════════════════════════════════════════
// Capacity Limits
// ═══════════════════════════════════════════════════════════════════════════════

/// Max chat messages in history
const MAX_CHAT_MESSAGES: usize = 500;

/// Max content versions per (mode, `content_type`) key
const MAX_CONTENT_VERSIONS: usize = 50;

use super::state::Mode;
use super::utils::truncate_chars;

// ═══════════════════════════════════════════════════════════════════════════════
// Session Metadata (for persistence)
// ═══════════════════════════════════════════════════════════════════════════════

/// Metadata for history persistence and thread identification
#[derive(Debug, Clone)]
pub struct SessionMetadata {
    /// Unique session/thread identifier
    pub session_id: Uuid,
    /// When this session was created (wall clock time for persistence)
    pub created_at: DateTime<Utc>,
    /// Last activity timestamp
    pub last_activity: DateTime<Utc>,
    /// Repository path this session belongs to
    pub repo_path: Option<PathBuf>,
    /// Optional title/summary for the thread (e.g., "feat: add auth")
    pub title: Option<String>,
    /// Current branch at session start
    pub branch: Option<String>,
}

impl Default for SessionMetadata {
    fn default() -> Self {
        Self::new()
    }
}

impl SessionMetadata {
    pub fn new() -> Self {
        let now = Utc::now();
        Self {
            session_id: Uuid::new_v4(),
            created_at: now,
            last_activity: now,
            repo_path: None,
            title: None,
            branch: None,
        }
    }

    /// Create with repo context
    pub fn with_repo(repo_path: PathBuf, branch: Option<String>) -> Self {
        let mut meta = Self::new();
        meta.repo_path = Some(repo_path);
        meta.branch = branch;
        meta
    }

    /// Update last activity timestamp
    pub fn touch(&mut self) {
        self.last_activity = Utc::now();
    }

    /// Set a title for this session
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = Some(title.into());
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// History
// ═══════════════════════════════════════════════════════════════════════════════

/// Single source of truth for all history in Iris Studio
#[derive(Debug, Clone)]
pub struct History {
    /// Session metadata for persistence and thread resume
    pub metadata: SessionMetadata,

    /// Complete event log (all events that modified state)
    events: VecDeque<HistoryEntry>,

    /// Maximum events to retain (prevents unbounded growth)
    max_events: usize,

    /// Chat messages (persists across modes)
    chat_messages: Vec<ChatMessage>,

    /// Content versions indexed by (mode, `content_type`)
    /// Each entry contains all versions of that content
    content_versions: HashMap<ContentKey, Vec<ContentVersion>>,

    /// Generation counter for unique IDs
    next_id: u64,
}

impl Default for History {
    fn default() -> Self {
        Self::new()
    }
}

impl History {
    /// Create a new unified history
    pub fn new() -> Self {
        Self {
            metadata: SessionMetadata::new(),
            events: VecDeque::with_capacity(1000),
            max_events: 1000,
            chat_messages: Vec::new(),
            content_versions: HashMap::new(),
            next_id: 1,
        }
    }

    /// Create with repository context (for persistence)
    pub fn with_repo(repo_path: PathBuf, branch: Option<String>) -> Self {
        Self {
            metadata: SessionMetadata::with_repo(repo_path, branch),
            events: VecDeque::with_capacity(1000),
            max_events: 1000,
            chat_messages: Vec::new(),
            content_versions: HashMap::new(),
            next_id: 1,
        }
    }

    /// Get session ID for this history
    pub fn session_id(&self) -> Uuid {
        self.metadata.session_id
    }

    /// Set title for this session (e.g., from first commit message)
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.metadata.set_title(title);
    }

    /// Touch metadata to update `last_activity`
    fn touch(&mut self) {
        self.metadata.touch();
    }

    /// Record an event in history
    pub fn record_event(&mut self, event: &TimestampedEvent) {
        self.touch();
        let entry = HistoryEntry {
            id: self.next_id(),
            timestamp: event.timestamp,
            source: event.source,
            change: HistoryChange::Event(format!("{:?}", event.event)),
        };

        self.push_entry(entry);
    }

    /// Record a content update
    pub fn record_content(
        &mut self,
        mode: Mode,
        content_type: ContentType,
        content: &ContentData,
        source: EventSource,
        trigger: &str,
    ) {
        self.touch();
        let key = ContentKey { mode, content_type };

        // Get previous content for diff tracking
        let previous = self
            .content_versions
            .get(&key)
            .and_then(|versions| versions.last())
            .map(|v| v.content.clone());

        let version = ContentVersion {
            id: self.next_id(),
            timestamp: Instant::now(),
            source,
            trigger: trigger.to_string(),
            content: content.clone(),
            previous_id: self
                .content_versions
                .get(&key)
                .and_then(|v| v.last())
                .map(|v| v.id),
        };

        // Record in content versions
        self.content_versions
            .entry(key.clone())
            .or_default()
            .push(version);
        self.trim_content_versions(&key);

        // Record in event log
        let entry = HistoryEntry {
            id: self.next_id(),
            timestamp: Instant::now(),
            source,
            change: HistoryChange::ContentUpdated {
                mode,
                content_type,
                trigger: trigger.to_string(),
                preview: content.preview(50),
                previous_preview: previous.map(|p| p.preview(50)),
            },
        };

        self.push_entry(entry);
    }

    /// Trim chat messages to stay within bounds (drops oldest)
    fn trim_chat_messages(&mut self) {
        while self.chat_messages.len() > MAX_CHAT_MESSAGES {
            self.chat_messages.remove(0);
        }
    }

    /// Trim content versions for a key to stay within bounds (drops oldest)
    fn trim_content_versions(&mut self, key: &ContentKey) {
        if let Some(versions) = self.content_versions.get_mut(key) {
            while versions.len() > MAX_CONTENT_VERSIONS {
                versions.remove(0);
            }
        }
    }

    /// Add a chat message
    pub fn add_chat_message(&mut self, role: ChatRole, content: &str) {
        self.touch();
        let message = ChatMessage {
            id: self.next_id(),
            timestamp: Instant::now(),
            role,
            content: content.to_string(),
            mode_context: None,
        };

        self.chat_messages.push(message);
        self.trim_chat_messages();

        // Also record in event log
        let entry = HistoryEntry {
            id: self.next_id(),
            timestamp: Instant::now(),
            source: match role {
                ChatRole::User => EventSource::User,
                ChatRole::Iris => EventSource::Agent,
            },
            change: HistoryChange::ChatMessage {
                role,
                preview: truncate_chars(content, 100),
            },
        };

        self.push_entry(entry);
    }

    /// Add a chat message with mode context
    pub fn add_chat_message_with_context(
        &mut self,
        role: ChatRole,
        content: &str,
        mode: Mode,
        related_content: Option<String>,
    ) {
        self.touch();
        let message = ChatMessage {
            id: self.next_id(),
            timestamp: Instant::now(),
            role,
            content: content.to_string(),
            mode_context: Some(ModeContext {
                mode,
                related_content,
            }),
        };

        self.chat_messages.push(message);
        self.trim_chat_messages();

        // Also record in event log
        let entry = HistoryEntry {
            id: self.next_id(),
            timestamp: Instant::now(),
            source: match role {
                ChatRole::User => EventSource::User,
                ChatRole::Iris => EventSource::Agent,
            },
            change: HistoryChange::ChatMessage {
                role,
                preview: truncate_chars(content, 100),
            },
        };

        self.push_entry(entry);
    }

    /// Record a mode switch
    pub fn record_mode_switch(&mut self, from: Mode, to: Mode) {
        let entry = HistoryEntry {
            id: self.next_id(),
            timestamp: Instant::now(),
            source: EventSource::User,
            change: HistoryChange::ModeSwitch { from, to },
        };

        self.push_entry(entry);
    }

    /// Record an agent task start
    pub fn record_agent_start(&mut self, task_type: TaskType) {
        let entry = HistoryEntry {
            id: self.next_id(),
            timestamp: Instant::now(),
            source: EventSource::System,
            change: HistoryChange::AgentTaskStarted { task_type },
        };

        self.push_entry(entry);
    }

    /// Record an agent task completion
    pub fn record_agent_complete(&mut self, task_type: TaskType, success: bool) {
        let entry = HistoryEntry {
            id: self.next_id(),
            timestamp: Instant::now(),
            source: EventSource::Agent,
            change: HistoryChange::AgentTaskCompleted { task_type, success },
        };

        self.push_entry(entry);
    }

    // ─────────────────────────────────────────────────────────────────────────
    // Query Methods
    // ─────────────────────────────────────────────────────────────────────────

    /// Get all chat messages
    pub fn chat_messages(&self) -> &[ChatMessage] {
        &self.chat_messages
    }

    /// Get recent chat messages (last n)
    pub fn recent_chat_messages(&self, n: usize) -> &[ChatMessage] {
        let start = self.chat_messages.len().saturating_sub(n);
        &self.chat_messages[start..]
    }

    /// Get content versions for a specific (mode, `content_type`)
    pub fn content_versions(&self, mode: Mode, content_type: ContentType) -> &[ContentVersion] {
        let key = ContentKey { mode, content_type };
        self.content_versions.get(&key).map_or(&[], Vec::as_slice)
    }

    /// Get the latest content version
    pub fn latest_content(&self, mode: Mode, content_type: ContentType) -> Option<&ContentVersion> {
        let key = ContentKey { mode, content_type };
        self.content_versions.get(&key).and_then(|v| v.last())
    }

    /// Get content version count
    pub fn content_version_count(&self, mode: Mode, content_type: ContentType) -> usize {
        let key = ContentKey { mode, content_type };
        self.content_versions.get(&key).map_or(0, Vec::len)
    }

    /// Get all events (for debugging/audit)
    pub fn events(&self) -> impl Iterator<Item = &HistoryEntry> {
        self.events.iter()
    }

    /// Get recent events (last n)
    pub fn recent_events(&self, n: usize) -> impl Iterator<Item = &HistoryEntry> {
        let skip = self.events.len().saturating_sub(n);
        self.events.iter().skip(skip)
    }

    /// Get events since a timestamp
    pub fn events_since(&self, since: Instant) -> impl Iterator<Item = &HistoryEntry> {
        self.events.iter().filter(move |e| e.timestamp >= since)
    }

    /// Get total event count
    pub fn event_count(&self) -> usize {
        self.events.len()
    }

    /// Clear all history (for reset)
    pub fn clear(&mut self) {
        self.events.clear();
        self.chat_messages.clear();
        self.content_versions.clear();
    }

    /// Clear chat messages only
    pub fn clear_chat(&mut self) {
        self.chat_messages.clear();
    }

    // ─────────────────────────────────────────────────────────────────────────
    // Private Helpers
    // ─────────────────────────────────────────────────────────────────────────

    fn next_id(&mut self) -> u64 {
        let id = self.next_id;
        self.next_id += 1;
        id
    }

    fn push_entry(&mut self, entry: HistoryEntry) {
        self.events.push_back(entry);

        // Trim if over capacity
        while self.events.len() > self.max_events {
            self.events.pop_front();
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// History Entry Types
// ═══════════════════════════════════════════════════════════════════════════════

/// A single entry in the history log
#[derive(Debug, Clone)]
pub struct HistoryEntry {
    /// Unique ID for this entry
    pub id: u64,
    /// When this happened
    pub timestamp: Instant,
    /// Where it came from
    pub source: EventSource,
    /// What changed
    pub change: HistoryChange,
}

/// What changed in this history entry
#[derive(Debug, Clone)]
pub enum HistoryChange {
    /// Generic event (for debugging)
    Event(String),

    /// Content was updated
    ContentUpdated {
        mode: Mode,
        content_type: ContentType,
        trigger: String,
        preview: String,
        previous_preview: Option<String>,
    },

    /// Chat message added
    ChatMessage { role: ChatRole, preview: String },

    /// Mode was switched
    ModeSwitch { from: Mode, to: Mode },

    /// Agent task started
    AgentTaskStarted { task_type: TaskType },

    /// Agent task completed
    AgentTaskCompleted { task_type: TaskType, success: bool },
}

// ═══════════════════════════════════════════════════════════════════════════════
// Content Types
// ═══════════════════════════════════════════════════════════════════════════════

/// Key for content version lookup
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct ContentKey {
    mode: Mode,
    content_type: ContentType,
}

/// A version of content
#[derive(Debug, Clone)]
pub struct ContentVersion {
    /// Unique ID
    pub id: u64,
    /// When created
    pub timestamp: Instant,
    /// Where it came from
    pub source: EventSource,
    /// What triggered this update
    pub trigger: String,
    /// The actual content
    pub content: ContentData,
    /// Previous version ID (for linking)
    pub previous_id: Option<u64>,
}

/// Content data - either structured (commit) or markdown
#[derive(Debug, Clone)]
pub enum ContentData {
    /// Structured commit message
    Commit(GeneratedMessage),

    /// Markdown content (PR, review, changelog, etc.)
    Markdown(String),
}

impl ContentData {
    /// Get a preview of the content
    pub fn preview(&self, max_len: usize) -> String {
        match self {
            Self::Commit(msg) => {
                let full = format!("{} {}", msg.emoji.as_deref().unwrap_or(""), msg.title);
                if full.len() > max_len {
                    format!("{}...", &full[..max_len])
                } else {
                    full
                }
            }
            Self::Markdown(content) => {
                // Get first non-empty line
                let first_line = content.lines().find(|l| !l.trim().is_empty()).unwrap_or("");

                if first_line.len() > max_len {
                    format!("{}...", &first_line[..max_len])
                } else {
                    first_line.to_string()
                }
            }
        }
    }

    /// Get full content as string
    pub fn as_string(&self) -> String {
        match self {
            Self::Commit(msg) => {
                let emoji = msg.emoji.as_deref().unwrap_or("");
                if emoji.is_empty() {
                    format!("{}\n\n{}", msg.title, msg.message)
                } else {
                    format!("{} {}\n\n{}", emoji, msg.title, msg.message)
                }
            }
            Self::Markdown(content) => content.clone(),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Chat Types
// ═══════════════════════════════════════════════════════════════════════════════

/// A chat message
#[derive(Debug, Clone)]
pub struct ChatMessage {
    /// Unique ID
    pub id: u64,
    /// When sent
    pub timestamp: Instant,
    /// Who sent it
    pub role: ChatRole,
    /// The message content
    pub content: String,
    /// Optional mode context (what was being worked on)
    pub mode_context: Option<ModeContext>,
}

/// Who sent the message
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChatRole {
    User,
    Iris,
}

/// Context about what mode/content the message relates to
#[derive(Debug, Clone)]
pub struct ModeContext {
    /// Mode when message was sent
    pub mode: Mode,
    /// Related content (e.g., commit message being discussed)
    pub related_content: Option<String>,
}