opencrabs 0.3.78

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
//! Analytics Event Repository
//!
//! Tracks phantom detections, streaming recoveries, and brain verify events
//! for Mission Control analytics (#897). Append-only event log.

use crate::db::Pool;
use crate::db::database::interact_err;
use anyhow::{Context, Result};
use rusqlite::params;

/// A phantom tool call detection event.
#[derive(Debug, Clone)]
pub struct PhantomEvent {
    pub id: String,
    pub session_id: String,
    pub provider: Option<String>,
    pub model: Option<String>,
    pub detected_at: i64,
    pub resolved: bool,
    pub retry_count: i64,
    pub tools_after_retry: i64,
}

/// A streaming tool call recovery event.
#[derive(Debug, Clone)]
pub struct StreamingRecovery {
    pub id: String,
    pub session_id: String,
    pub provider: Option<String>,
    pub model: Option<String>,
    pub recovered_at: i64,
    pub tool_count: i64,
}

/// A brain verify gate event.
#[derive(Debug, Clone)]
pub struct BrainVerifyEvent {
    pub id: String,
    pub file_name: String,
    pub event_type: String,
    pub violations: Option<String>,
    pub created_at: i64,
}

/// Aggregated phantom stats for a time window.
#[derive(Debug, Clone, Default)]
pub struct PhantomStats {
    pub total: i64,
    pub resolved: i64,
    pub by_model: Vec<(String, i64, i64)>, // (model, total, resolved)
}

/// Aggregated streaming recovery stats.
#[derive(Debug, Clone, Default)]
pub struct StreamingStats {
    pub total: i64,
    pub total_tools: i64,
    pub by_model: Vec<(String, i64)>, // (model, count)
}

/// Aggregated brain verify stats.
#[derive(Debug, Clone, Default)]
pub struct BrainVerifyStats {
    pub passes: i64,
    pub rollbacks: i64,
    pub fail_closed: i64,
}

/// Per-model tool failure stats.
#[derive(Debug, Clone)]
pub struct ModelToolStats {
    pub model: String,
    pub total: i64,
    pub failures: i64,
}

/// Repository for analytics event tracking.
#[derive(Clone)]
pub struct AnalyticsEventRepository {
    pool: Pool,
}

impl AnalyticsEventRepository {
    pub fn new(pool: Pool) -> Self {
        Self { pool }
    }

    /// Access the underlying pool (for tests and cross-repo queries).
    pub fn pool(&self) -> &Pool {
        &self.pool
    }

    // ─── Best-effort Emitters ─────────────────────────────────────────
    //
    // These wrap the async record_* methods in a fire-and-forget spawn so
    // analytics never blocks or breaks the hot path. Errors are logged, never
    // propagated. All read provider/model from the global pool at call time.

    /// Fire-and-forget phantom detection event. Called when the phantom
    /// detector fires (WARN). Provider/model are read live so the event is
    /// tagged with whatever was active when the phantom was emitted.
    pub fn emit_phantom(session_id: &str, provider: Option<&str>, model: Option<&str>) {
        let Some(pool) = crate::db::global_pool() else {
            return;
        };
        let repo = Self::new(pool.clone());
        let id = format!("phantom-{}", uuid::Uuid::new_v4());
        let session_id = session_id.to_string();
        let provider = provider.map(|s| s.to_string());
        let model = model.map(|s| s.to_string());
        tokio::spawn(async move {
            if let Err(e) = repo
                .record_phantom(&id, &session_id, provider.as_deref(), model.as_deref())
                .await
            {
                tracing::error!("[ANALYTICS] phantom record failed: {e}");
            }
        });
    }

    /// Fire-and-forget phantom resolution. Called when, after a phantom, the
    /// retry produces real tool calls.
    pub fn emit_resolve_phantom(session_id: &str, retry_count: i64, tools_after_retry: i64) {
        let Some(pool) = crate::db::global_pool() else {
            return;
        };
        let repo = Self::new(pool.clone());
        let session_id = session_id.to_string();
        tokio::spawn(async move {
            if let Err(e) = repo
                .resolve_phantom(&session_id, retry_count, tools_after_retry)
                .await
            {
                tracing::error!("[ANALYTICS] phantom resolve failed: {e}");
            }
        });
    }

    /// Fire-and-forget streaming recovery event.
    pub fn emit_streaming_recovery(
        session_id: &str,
        provider: Option<&str>,
        model: Option<&str>,
        tool_count: i64,
    ) {
        let Some(pool) = crate::db::global_pool() else {
            return;
        };
        let repo = Self::new(pool.clone());
        let id = format!("stream-recover-{}", uuid::Uuid::new_v4());
        let session_id = session_id.to_string();
        let provider = provider.map(|s| s.to_string());
        let model = model.map(|s| s.to_string());
        tokio::spawn(async move {
            if let Err(e) = repo
                .record_streaming_recovery(
                    &id,
                    &session_id,
                    provider.as_deref(),
                    model.as_deref(),
                    tool_count,
                )
                .await
            {
                tracing::error!("[ANALYTICS] streaming recovery record failed: {e}");
            }
        });
    }

    /// Fire-and-forget brain verify gate event. `event_type` is one of
    /// "pass" | "rollback" | "fail_closed".
    pub fn emit_brain_verify(file_name: &str, event_type: &str, violations: Option<&str>) {
        let Some(pool) = crate::db::global_pool() else {
            return;
        };
        let repo = Self::new(pool.clone());
        let id = format!("brain-verify-{}", uuid::Uuid::new_v4());
        let file_name = file_name.to_string();
        let event_type = event_type.to_string();
        let violations = violations.map(|s| s.to_string());
        tokio::spawn(async move {
            if let Err(e) = repo
                .record_brain_verify(&id, &file_name, &event_type, violations.as_deref())
                .await
            {
                tracing::error!("[ANALYTICS] brain verify record failed: {e}");
            }
        });
    }

    // ─── Phantom Events ───────────────────────────────────────────────

    /// Record a phantom detection event.
    pub async fn record_phantom(
        &self,
        id: &str,
        session_id: &str,
        provider: Option<&str>,
        model: Option<&str>,
    ) -> Result<()> {
        let id = id.to_string();
        let session_id = session_id.to_string();
        let provider = provider.map(|s| s.to_string());
        let model = model.map(|s| s.to_string());
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.execute(
                    "INSERT OR IGNORE INTO phantom_events (id, session_id, provider, model) \
                     VALUES (?1, ?2, ?3, ?4)",
                    params![id, session_id, provider, model],
                )
            })
            .await
            .map_err(interact_err)?
            .context("Failed to record phantom event")?;
        Ok(())
    }

    /// Mark a phantom event as resolved (retry produced real tool calls).
    pub async fn resolve_phantom(
        &self,
        session_id: &str,
        retry_count: i64,
        tools_after_retry: i64,
    ) -> Result<()> {
        let session_id = session_id.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.execute(
                    "UPDATE phantom_events SET resolved = 1, retry_count = ?2, tools_after_retry = ?3 \
                     WHERE session_id = ?1 AND resolved = 0 \
                     AND detected_at = (SELECT MAX(detected_at) FROM phantom_events WHERE session_id = ?1 AND resolved = 0)",
                    params![session_id, retry_count, tools_after_retry],
                )
            })
            .await
            .map_err(interact_err)?
            .context("Failed to resolve phantom event")?;
        Ok(())
    }

    /// Query phantom stats since an epoch.
    pub async fn phantom_stats(&self, since_epoch: Option<i64>) -> Result<PhantomStats> {
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| -> rusqlite::Result<PhantomStats> {
                let (total, resolved) = if let Some(since) = since_epoch {
                    let t: i64 = conn.query_row(
                        "SELECT COUNT(*) FROM phantom_events WHERE detected_at >= ?1",
                        params![since],
                        |r| r.get(0),
                    )?;
                    let res: i64 = conn.query_row(
                        "SELECT COUNT(*) FROM phantom_events WHERE detected_at >= ?1 AND resolved = 1",
                        params![since],
                        |r| r.get(0),
                    )?;
                    (t, res)
                } else {
                    let t: i64 =
                        conn.query_row("SELECT COUNT(*) FROM phantom_events", [], |r| r.get(0))?;
                    let res: i64 = conn.query_row(
                        "SELECT COUNT(*) FROM phantom_events WHERE resolved = 1",
                        [],
                        |r| r.get(0),
                    )?;
                    (t, res)
                };

                let by_model_query = if since_epoch.is_some() {
                    "SELECT COALESCE(model, 'unknown'), COUNT(*), SUM(CASE WHEN resolved = 1 THEN 1 ELSE 0 END) \
                     FROM phantom_events WHERE detected_at >= ?1 GROUP BY model ORDER BY COUNT(*) DESC"
                } else {
                    "SELECT COALESCE(model, 'unknown'), COUNT(*), SUM(CASE WHEN resolved = 1 THEN 1 ELSE 0 END) \
                     FROM phantom_events GROUP BY model ORDER BY COUNT(*) DESC"
                };
                let mut stmt = conn.prepare_cached(by_model_query)?;
                let by_model: Vec<(String, i64, i64)> = if let Some(since) = since_epoch {
                    let rows = stmt.query_map(params![since], |r| {
                        Ok((
                            r.get::<_, String>(0)?,
                            r.get::<_, i64>(1)?,
                            r.get::<_, Option<i64>>(2)?.unwrap_or(0),
                        ))
                    })?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()?
                } else {
                    let rows = stmt.query_map([], |r| {
                        Ok((
                            r.get::<_, String>(0)?,
                            r.get::<_, i64>(1)?,
                            r.get::<_, Option<i64>>(2)?.unwrap_or(0),
                        ))
                    })?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()?
                };

                Ok(PhantomStats {
                    total,
                    resolved,
                    by_model,
                })
            })
            .await
            .map_err(interact_err)?
            .context("Failed to query phantom stats")
    }

    // ─── Streaming Recoveries ─────────────────────────────────────────

    /// Record a streaming tool call recovery.
    pub async fn record_streaming_recovery(
        &self,
        id: &str,
        session_id: &str,
        provider: Option<&str>,
        model: Option<&str>,
        tool_count: i64,
    ) -> Result<()> {
        let id = id.to_string();
        let session_id = session_id.to_string();
        let provider = provider.map(|s| s.to_string());
        let model = model.map(|s| s.to_string());
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.execute(
                    "INSERT OR IGNORE INTO streaming_recoveries (id, session_id, provider, model, tool_count) \
                     VALUES (?1, ?2, ?3, ?4, ?5)",
                    params![id, session_id, provider, model, tool_count],
                )
            })
            .await
            .map_err(interact_err)?
            .context("Failed to record streaming recovery")?;
        Ok(())
    }

    /// Query streaming recovery stats since an epoch.
    pub async fn streaming_stats(&self, since_epoch: Option<i64>) -> Result<StreamingStats> {
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| -> rusqlite::Result<StreamingStats> {
                let (total, total_tools) = if let Some(since) = since_epoch {
                    let t: i64 = conn.query_row(
                        "SELECT COUNT(*) FROM streaming_recoveries WHERE recovered_at >= ?1",
                        params![since],
                        |r| r.get(0),
                    )?;
                    let tools: i64 = conn.query_row(
                        "SELECT COALESCE(SUM(tool_count), 0) FROM streaming_recoveries WHERE recovered_at >= ?1",
                        params![since],
                        |r| r.get(0),
                    )?;
                    (t, tools)
                } else {
                    let t: i64 = conn
                        .query_row("SELECT COUNT(*) FROM streaming_recoveries", [], |r| {
                            r.get(0)
                        })?;
                    let tools: i64 = conn.query_row(
                        "SELECT COALESCE(SUM(tool_count), 0) FROM streaming_recoveries",
                        [],
                        |r| r.get(0),
                    )?;
                    (t, tools)
                };

                let by_model_query = if since_epoch.is_some() {
                    "SELECT COALESCE(model, 'unknown'), COUNT(*) FROM streaming_recoveries \
                     WHERE recovered_at >= ?1 GROUP BY model ORDER BY COUNT(*) DESC"
                } else {
                    "SELECT COALESCE(model, 'unknown'), COUNT(*) FROM streaming_recoveries \
                     GROUP BY model ORDER BY COUNT(*) DESC"
                };
                let mut stmt = conn.prepare_cached(by_model_query)?;
                let by_model: Vec<(String, i64)> = if let Some(since) = since_epoch {
                    let rows = stmt.query_map(params![since], |r| {
                        Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?))
                    })?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()?
                } else {
                    let rows = stmt.query_map([], |r| {
                        Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?))
                    })?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()?
                };

                Ok(StreamingStats {
                    total,
                    total_tools,
                    by_model,
                })
            })
            .await
            .map_err(interact_err)?
            .context("Failed to query streaming stats")
    }

    // ─── Brain Verify Events ──────────────────────────────────────────

    /// Record a brain verify gate event.
    pub async fn record_brain_verify(
        &self,
        id: &str,
        file_name: &str,
        event_type: &str,
        violations: Option<&str>,
    ) -> Result<()> {
        let id = id.to_string();
        let file_name = file_name.to_string();
        let event_type = event_type.to_string();
        let violations = violations.map(|s| s.to_string());
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.execute(
                    "INSERT OR IGNORE INTO brain_verify_events (id, file_name, event_type, violations) \
                     VALUES (?1, ?2, ?3, ?4)",
                    params![id, file_name, event_type, violations],
                )
            })
            .await
            .map_err(interact_err)?
            .context("Failed to record brain verify event")?;
        Ok(())
    }

    /// Query brain verify stats since an epoch.
    pub async fn brain_verify_stats(&self, since_epoch: Option<i64>) -> Result<BrainVerifyStats> {
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| -> rusqlite::Result<BrainVerifyStats> {
                let query = if since_epoch.is_some() {
                    "SELECT \
                        SUM(CASE WHEN event_type = 'pass' THEN 1 ELSE 0 END), \
                        SUM(CASE WHEN event_type = 'rollback' THEN 1 ELSE 0 END), \
                        SUM(CASE WHEN event_type = 'fail_closed' THEN 1 ELSE 0 END) \
                     FROM brain_verify_events WHERE created_at >= ?1"
                } else {
                    "SELECT \
                        SUM(CASE WHEN event_type = 'pass' THEN 1 ELSE 0 END), \
                        SUM(CASE WHEN event_type = 'rollback' THEN 1 ELSE 0 END), \
                        SUM(CASE WHEN event_type = 'fail_closed' THEN 1 ELSE 0 END) \
                     FROM brain_verify_events"
                };
                let result = if let Some(since) = since_epoch {
                    conn.query_row(query, params![since], |r| {
                        Ok(BrainVerifyStats {
                            passes: r.get::<_, Option<i64>>(0)?.unwrap_or(0),
                            rollbacks: r.get::<_, Option<i64>>(1)?.unwrap_or(0),
                            fail_closed: r.get::<_, Option<i64>>(2)?.unwrap_or(0),
                        })
                    })?
                } else {
                    conn.query_row(query, [], |r| {
                        Ok(BrainVerifyStats {
                            passes: r.get::<_, Option<i64>>(0)?.unwrap_or(0),
                            rollbacks: r.get::<_, Option<i64>>(1)?.unwrap_or(0),
                            fail_closed: r.get::<_, Option<i64>>(2)?.unwrap_or(0),
                        })
                    })?
                };
                Ok(result)
            })
            .await
            .map_err(interact_err)?
            .context("Failed to query brain verify stats")
    }

    // ─── Per-Model Tool Stats ─────────────────────────────────────────

    /// Per-model tool failure stats since an epoch.
    pub async fn tool_stats_by_model(
        &self,
        since_epoch: Option<i64>,
    ) -> Result<Vec<ModelToolStats>> {
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                let (query, param): (String, Vec<Box<dyn rusqlite::types::ToSql>>) =
                    if let Some(since) = since_epoch {
                        (
                            "SELECT COALESCE(model, 'unknown'), COUNT(*), \
                                    SUM(CASE WHEN status = 'error' THEN 1 ELSE 0 END) \
                             FROM tool_executions \
                             WHERE created_at >= ?1 AND tool_name <> '' \
                             GROUP BY model ORDER BY COUNT(*) DESC"
                                .to_string(),
                            vec![Box::new(since)],
                        )
                    } else {
                        (
                            "SELECT COALESCE(model, 'unknown'), COUNT(*), \
                                    SUM(CASE WHEN status = 'error' THEN 1 ELSE 0 END) \
                             FROM tool_executions \
                             WHERE tool_name <> '' \
                             GROUP BY model ORDER BY COUNT(*) DESC"
                                .to_string(),
                            vec![],
                        )
                    };
                let mut stmt = conn.prepare_cached(&query)?;
                let param_refs: Vec<&dyn rusqlite::types::ToSql> =
                    param.iter().map(|p| p.as_ref()).collect();
                let rows = stmt.query_map(param_refs.as_slice(), |row| {
                    Ok(ModelToolStats {
                        model: row.get(0)?,
                        total: row.get(1)?,
                        failures: row.get::<_, Option<i64>>(2)?.unwrap_or(0),
                    })
                })?;
                rows.collect::<std::result::Result<Vec<_>, _>>()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to query per-model tool stats")
    }
}