gid-core 0.3.2

Graph-Indexed Development core library — graph-based project management and code analysis for AI agents
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
//! Ritual Notifier — Telegram notifications for ritual phase transitions.
//!
//! Wraps `TelegramNotifier` from harness to provide ritual-specific notifications
//! for start, phase completion, approval required, failure, and completion events.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use tracing::{debug, warn};

use crate::harness::notifier::{
    escape_html, InlineButton, InlineKeyboard, NotifierConfig, TelegramNotifier,
};
use super::definition::PhaseDefinition;
use super::executor::PhaseResult;

/// Configuration for ritual notifications.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RitualNotifyConfig {
    /// Events to notify on. If empty, all events are notified.
    #[serde(default)]
    pub events: Vec<RitualEvent>,
    /// Whether notifications are enabled.
    #[serde(default = "default_enabled")]
    pub enabled: bool,
}

fn default_enabled() -> bool {
    true
}

impl Default for RitualNotifyConfig {
    fn default() -> Self {
        Self {
            events: Vec::new(),
            enabled: true,
        }
    }
}

impl RitualNotifyConfig {
    /// Check if a specific event should be notified.
    pub fn should_notify(&self, event: &RitualEvent) -> bool {
        if !self.enabled {
            return false;
        }
        // If events list is empty, notify all events (default behavior)
        if self.events.is_empty() {
            return true;
        }
        self.events.contains(event)
    }
}

/// Types of ritual events that can trigger notifications.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RitualEvent {
    /// Ritual has started.
    RitualStart,
    /// A phase completed successfully.
    PhaseComplete,
    /// Approval is required for a phase.
    ApprovalRequired,
    /// A phase failed.
    PhaseFailed,
    /// Ritual completed successfully.
    RitualComplete,
    /// Ritual failed.
    RitualFailed,
}

/// Notifier for ritual events.
///
/// Wraps `TelegramNotifier` and provides ritual-specific message formatting.
/// All notification methods are fire-and-forget — errors are logged but don't
/// fail the ritual.
pub struct RitualNotifier {
    telegram: TelegramNotifier,
    config: RitualNotifyConfig,
}

impl RitualNotifier {
    /// Create a new RitualNotifier from environment variables.
    ///
    /// Returns `None` if the required environment variables (`GID_TELEGRAM_BOT_TOKEN`,
    /// `GID_TELEGRAM_CHAT_ID`) are not set. This allows graceful degradation when
    /// notifications are not configured.
    pub fn from_env() -> Option<Self> {
        let telegram = TelegramNotifier::from_env()?;
        Some(Self {
            telegram,
            config: RitualNotifyConfig::default(),
        })
    }

    /// Create a new RitualNotifier with explicit config.
    pub fn new(telegram: TelegramNotifier, config: RitualNotifyConfig) -> Self {
        Self { telegram, config }
    }

    /// Create from NotifierConfig and RitualNotifyConfig.
    pub fn from_config(
        notifier_config: NotifierConfig,
        ritual_config: RitualNotifyConfig,
    ) -> Option<Self> {
        let telegram = TelegramNotifier::new(notifier_config)?;
        Some(Self {
            telegram,
            config: ritual_config,
        })
    }

    /// Notify that a ritual has started.
    pub async fn notify_ritual_start(&self, ritual_name: &str, total_phases: usize) -> Result<()> {
        if !self.config.should_notify(&RitualEvent::RitualStart) {
            return Ok(());
        }

        let html = format!(
            "🔄 <b>Ritual Started</b>\n\
             📋 {}\n\
             📊 {} phases",
            escape_html(ritual_name),
            total_phases
        );

        // Fire-and-forget: spawn background task to avoid blocking ritual
        let telegram = self.telegram.clone();
        tokio::spawn(async move {
            match telegram.send_html(&html).await {
                Ok(_) => debug!("Sent ritual start notification"),
                Err(e) => warn!("Failed to send ritual start notification: {}", e),
            }
        });

        Ok(())
    }

    /// Notify that a phase completed successfully.
    pub async fn notify_phase_complete(
        &self,
        phase: &PhaseDefinition,
        result: &PhaseResult,
        phase_idx: usize,
        total: usize,
    ) -> Result<()> {
        if !self.config.should_notify(&RitualEvent::PhaseComplete) {
            return Ok(());
        }

        let artifact_count = result.artifacts.len();
        let html = format!(
            "✅ <b>Phase {}/{}</b>: {}\n\{}s | 📁 {} artifact{}",
            phase_idx + 1,
            total,
            escape_html(&phase.id),
            result.duration_secs,
            artifact_count,
            if artifact_count == 1 { "" } else { "s" }
        );

        // Fire-and-forget: spawn background task
        let telegram = self.telegram.clone();
        let phase_id = phase.id.clone();
        tokio::spawn(async move {
            match telegram.send_html(&html).await {
                Ok(_) => debug!("Sent phase complete notification for '{}'", phase_id),
                Err(e) => warn!("Failed to send phase complete notification: {}", e),
            }
        });

        Ok(())
    }

    /// Notify that approval is required for a phase.
    ///
    /// Sends an inline keyboard with Approve/Skip/Reject buttons.
    /// Returns the message_id so the message can be edited after the user responds.
    pub async fn notify_approval_required(
        &self,
        phase: &PhaseDefinition,
        artifacts: &[String],
    ) -> Result<i64> {
        if !self.config.should_notify(&RitualEvent::ApprovalRequired) {
            return Ok(0);
        }

        let mut html = format!(
            "⏸ <b>Approval Required</b>\n\
             📋 Phase: {}\n",
            escape_html(&phase.id)
        );

        if !artifacts.is_empty() {
            html.push_str("📁 Artifacts:\n");
            for artifact in artifacts.iter().take(10) {
                html.push_str(&format!("{}\n", escape_html(artifact)));
            }
            if artifacts.len() > 10 {
                html.push_str(&format!("  ... and {} more\n", artifacts.len() - 10));
            }
        }

        let keyboard = InlineKeyboard::new(vec![vec![
            InlineButton::callback("Approve ✅", &format!("ritual_approve:{}", phase.id)),
            InlineButton::callback("Skip ⊘", &format!("ritual_skip:{}", phase.id)),
            InlineButton::callback("Reject ✗", &format!("ritual_reject:{}", phase.id)),
        ]]);

        match self.telegram.send_with_keyboard(&html, &keyboard).await {
            Ok(resp) => {
                debug!(
                    "Sent approval required notification for '{}', message_id={}",
                    phase.id, resp.message_id
                );
                Ok(resp.message_id)
            }
            Err(e) => {
                warn!("Failed to send approval required notification: {}", e);
                Ok(0)
            }
        }
    }

    /// Notify that a phase failed.
    pub async fn notify_phase_failed(
        &self,
        phase: &PhaseDefinition,
        error: &str,
    ) -> Result<()> {
        if !self.config.should_notify(&RitualEvent::PhaseFailed) {
            return Ok(());
        }

        // Truncate error message if too long
        let error_display = if error.len() > 500 {
            format!("{}...", &error[..500])
        } else {
            error.to_string()
        };

        let html = format!(
            "❌ <b>Phase Failed</b>\n\
             📋 Phase: {}\n\
             📝 {}\n",
            escape_html(&phase.id),
            escape_html(&error_display)
        );

        // Fire-and-forget: spawn background task
        let telegram = self.telegram.clone();
        let phase_id = phase.id.clone();
        tokio::spawn(async move {
            match telegram.send_html(&html).await {
                Ok(_) => debug!("Sent phase failed notification for '{}'", phase_id),
                Err(e) => warn!("Failed to send phase failed notification: {}", e),
            }
        });

        Ok(())
    }

    /// Notify that the ritual completed successfully.
    pub async fn notify_ritual_complete(
        &self,
        ritual_name: &str,
        total_duration_secs: u64,
    ) -> Result<()> {
        if !self.config.should_notify(&RitualEvent::RitualComplete) {
            return Ok(());
        }

        let duration_str = format_duration(total_duration_secs);

        let html = format!(
            "🎉 <b>Ritual Completed!</b>\n\
             📋 {}\n\
             ⏱ Total: {}",
            escape_html(ritual_name),
            duration_str
        );

        // Fire-and-forget: spawn background task
        let telegram = self.telegram.clone();
        tokio::spawn(async move {
            match telegram.send_html(&html).await {
                Ok(_) => debug!("Sent ritual complete notification"),
                Err(e) => warn!("Failed to send ritual complete notification: {}", e),
            }
        });

        Ok(())
    }

    /// Notify that the ritual failed.
    pub async fn notify_ritual_failed(
        &self,
        ritual_name: &str,
        phase_id: &str,
        error: &str,
    ) -> Result<()> {
        if !self.config.should_notify(&RitualEvent::RitualFailed) {
            return Ok(());
        }

        // Truncate error message if too long
        let error_display = if error.len() > 500 {
            format!("{}...", &error[..500])
        } else {
            error.to_string()
        };

        let html = format!(
            "❌ <b>Ritual Failed</b>\n\
             📋 {}\n\
             💥 Failed at: {}\n\
             📝 {}",
            escape_html(ritual_name),
            escape_html(phase_id),
            escape_html(&error_display)
        );

        // Fire-and-forget: spawn background task
        let telegram = self.telegram.clone();
        tokio::spawn(async move {
            match telegram.send_html(&html).await {
                Ok(_) => debug!("Sent ritual failed notification"),
                Err(e) => warn!("Failed to send ritual failed notification: {}", e),
            }
        });

        Ok(())
    }

    /// Edit a previous message (e.g., to update approval status).
    pub async fn edit_message(&self, message_id: i64, html: &str) -> Result<()> {
        if message_id == 0 {
            return Ok(());
        }

        match self.telegram.edit_message(message_id, html).await {
            Ok(_) => debug!("Edited message {}", message_id),
            Err(e) => warn!("Failed to edit message {}: {}", message_id, e),
        }

        Ok(())
    }

    /// Answer a callback query (acknowledge button press).
    pub async fn answer_callback(&self, callback_id: &str, text: &str) -> Result<()> {
        match self.telegram.answer_callback(callback_id, text).await {
            Ok(_) => debug!("Answered callback {}", callback_id),
            Err(e) => warn!("Failed to answer callback {}: {}", callback_id, e),
        }

        Ok(())
    }
}

/// Format duration in human-readable form.
fn format_duration(secs: u64) -> String {
    if secs < 60 {
        format!("{}s", secs)
    } else if secs < 3600 {
        format!("{}m {}s", secs / 60, secs % 60)
    } else {
        format!("{}h {}m {}s", secs / 3600, (secs % 3600) / 60, secs % 60)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ritual_notify_config_default() {
        let config = RitualNotifyConfig::default();
        assert!(config.enabled);
        assert!(config.events.is_empty());
    }

    #[test]
    fn test_should_notify_all_when_empty() {
        let config = RitualNotifyConfig {
            enabled: true,
            events: vec![],
        };

        assert!(config.should_notify(&RitualEvent::RitualStart));
        assert!(config.should_notify(&RitualEvent::PhaseComplete));
        assert!(config.should_notify(&RitualEvent::ApprovalRequired));
        assert!(config.should_notify(&RitualEvent::PhaseFailed));
        assert!(config.should_notify(&RitualEvent::RitualComplete));
        assert!(config.should_notify(&RitualEvent::RitualFailed));
    }

    #[test]
    fn test_should_notify_filtered() {
        let config = RitualNotifyConfig {
            enabled: true,
            events: vec![
                RitualEvent::RitualStart,
                RitualEvent::RitualComplete,
                RitualEvent::RitualFailed,
            ],
        };

        assert!(config.should_notify(&RitualEvent::RitualStart));
        assert!(!config.should_notify(&RitualEvent::PhaseComplete));
        assert!(!config.should_notify(&RitualEvent::ApprovalRequired));
        assert!(config.should_notify(&RitualEvent::RitualComplete));
        assert!(config.should_notify(&RitualEvent::RitualFailed));
    }

    #[test]
    fn test_should_notify_disabled() {
        let config = RitualNotifyConfig {
            enabled: false,
            events: vec![],
        };

        assert!(!config.should_notify(&RitualEvent::RitualStart));
        assert!(!config.should_notify(&RitualEvent::PhaseComplete));
    }

    #[test]
    fn test_format_duration() {
        assert_eq!(format_duration(30), "30s");
        assert_eq!(format_duration(90), "1m 30s");
        assert_eq!(format_duration(3661), "1h 1m 1s");
    }

    #[test]
    fn test_ritual_event_serialization() {
        let event = RitualEvent::ApprovalRequired;
        let json = serde_json::to_string(&event).unwrap();
        assert_eq!(json, "\"approval_required\"");

        let parsed: RitualEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, event);
    }

    #[test]
    fn test_ritual_notify_config_yaml() {
        let yaml = r#"
enabled: true
events:
  - ritual_start
  - ritual_complete
"#;
        let config: RitualNotifyConfig = serde_yaml::from_str(yaml).unwrap();
        assert!(config.enabled);
        assert_eq!(config.events.len(), 2);
        assert!(config.events.contains(&RitualEvent::RitualStart));
        assert!(config.events.contains(&RitualEvent::RitualComplete));
    }
}