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
//! Tests for the analytics event DB layer (#898).
//!
//! Covers: phantom_events, streaming_recoveries, brain_verify_events
//! insert/query round-trips, and tool_executions nullable columns.

use crate::db::Database;
use crate::db::repository::AnalyticsEventRepository;

/// Helper: create an in-memory DB with all migrations applied.
async fn test_repo() -> AnalyticsEventRepository {
    let db = Database::connect_in_memory()
        .await
        .expect("in-memory DB should connect");
    db.run_migrations().await.expect("migrations should apply");
    AnalyticsEventRepository::new(db.pool().clone())
}

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

#[tokio::test]
async fn phantom_record_and_stats_round_trip() {
    let repo = test_repo().await;

    repo.record_phantom(
        "phantom-1",
        "session-abc",
        Some("anthropic"),
        Some("claude-opus-4-8"),
    )
    .await
    .expect("record should succeed");

    let stats = repo
        .phantom_stats(None)
        .await
        .expect("stats should succeed");
    assert_eq!(stats.total, 1);
    assert_eq!(stats.resolved, 0);
    assert_eq!(stats.by_model.len(), 1);
    assert_eq!(stats.by_model[0].0, "claude-opus-4-8");
    assert_eq!(stats.by_model[0].1, 1); // total
    assert_eq!(stats.by_model[0].2, 0); // resolved
}

#[tokio::test]
async fn phantom_resolve_updates_stats() {
    let repo = test_repo().await;

    repo.record_phantom("phantom-2", "session-abc", None, None)
        .await
        .expect("record should succeed");

    repo.resolve_phantom("session-abc", 3, 22)
        .await
        .expect("resolve should succeed");

    let stats = repo
        .phantom_stats(None)
        .await
        .expect("stats should succeed");
    assert_eq!(stats.total, 1);
    assert_eq!(stats.resolved, 1);
}

#[tokio::test]
async fn phantom_null_provider_model_grouped_as_unknown() {
    let repo = test_repo().await;

    repo.record_phantom("phantom-3", "session-xyz", None, None)
        .await
        .expect("record should succeed");

    let stats = repo
        .phantom_stats(None)
        .await
        .expect("stats should succeed");
    assert_eq!(stats.by_model.len(), 1);
    assert_eq!(stats.by_model[0].0, "unknown"); // COALESCE(model, 'unknown')
}

#[tokio::test]
async fn phantom_time_filter() {
    let repo = test_repo().await;

    // Record two events (detected_at defaults to unixepoch())
    repo.record_phantom("p-1", "s1", None, None).await.unwrap();
    repo.record_phantom("p-2", "s1", None, None).await.unwrap();

    // All events visible with no filter
    let stats = repo.phantom_stats(None).await.unwrap();
    assert_eq!(stats.total, 2);

    // Future epoch filters everything out
    let stats = repo.phantom_stats(Some(9999999999)).await.unwrap();
    assert_eq!(stats.total, 0);

    // Past epoch includes everything
    let stats = repo.phantom_stats(Some(1)).await.unwrap();
    assert_eq!(stats.total, 2);
}

#[tokio::test]
async fn phantom_multiple_models_breakdown() {
    let repo = test_repo().await;

    repo.record_phantom("p-a", "s1", Some("anthropic"), Some("claude-opus-4-8"))
        .await
        .unwrap();
    repo.record_phantom("p-b", "s1", Some("modelstudio"), Some("qwen3.8-max"))
        .await
        .unwrap();
    repo.record_phantom("p-c", "s1", Some("anthropic"), Some("claude-opus-4-8"))
        .await
        .unwrap();

    let stats = repo.phantom_stats(None).await.unwrap();
    assert_eq!(stats.total, 3);
    assert_eq!(stats.by_model.len(), 2);
    // Ordered by COUNT(*) DESC: claude-opus-4-8 has 2, qwen has 1
    assert_eq!(stats.by_model[0].0, "claude-opus-4-8");
    assert_eq!(stats.by_model[0].1, 2);
    assert_eq!(stats.by_model[1].0, "qwen3.8-max");
    assert_eq!(stats.by_model[1].1, 1);
}

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

#[tokio::test]
async fn streaming_recovery_record_and_stats() {
    let repo = test_repo().await;

    repo.record_streaming_recovery(
        "recovery-1",
        "session-abc",
        Some("modelstudio"),
        Some("qwen3.8-max-preview"),
        3,
    )
    .await
    .expect("record should succeed");

    let stats = repo
        .streaming_stats(None)
        .await
        .expect("stats should succeed");
    assert_eq!(stats.total, 1);
    assert_eq!(stats.total_tools, 3);
    assert_eq!(stats.by_model.len(), 1);
    assert_eq!(stats.by_model[0].0, "qwen3.8-max-preview");
    assert_eq!(stats.by_model[0].1, 1);
}

#[tokio::test]
async fn streaming_recovery_null_provider() {
    let repo = test_repo().await;

    repo.record_streaming_recovery("recovery-2", "s1", None, None, 1)
        .await
        .unwrap();

    let stats = repo.streaming_stats(None).await.unwrap();
    assert_eq!(stats.total, 1);
    assert_eq!(stats.by_model[0].0, "unknown");
}

#[tokio::test]
async fn streaming_recovery_time_filter() {
    let repo = test_repo().await;

    repo.record_streaming_recovery("r-1", "s1", None, None, 2)
        .await
        .unwrap();

    // Future epoch filters out
    let stats = repo.streaming_stats(Some(9999999999)).await.unwrap();
    assert_eq!(stats.total, 0);

    // Past epoch includes
    let stats = repo.streaming_stats(Some(1)).await.unwrap();
    assert_eq!(stats.total, 1);
    assert_eq!(stats.total_tools, 2);
}

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

#[tokio::test]
async fn brain_verify_record_and_stats() {
    let repo = test_repo().await;

    repo.record_brain_verify(
        "verify-1",
        "SOUL.md",
        "rollback",
        Some("contradiction pair A+B matched in entry 3"),
    )
    .await
    .expect("record should succeed");

    let stats = repo
        .brain_verify_stats(None)
        .await
        .expect("stats should succeed");
    assert_eq!(stats.passes, 0);
    assert_eq!(stats.rollbacks, 1);
    assert_eq!(stats.fail_closed, 0);
}

#[tokio::test]
async fn brain_verify_all_event_types() {
    let repo = test_repo().await;

    repo.record_brain_verify("v-pass", "AGENTS.md", "pass", None)
        .await
        .unwrap();
    repo.record_brain_verify("v-rollback", "SOUL.md", "rollback", Some("contradiction"))
        .await
        .unwrap();
    repo.record_brain_verify("v-fail", "MEMORY.md", "fail_closed", Some("TOML absent"))
        .await
        .unwrap();

    let stats = repo.brain_verify_stats(None).await.unwrap();
    assert_eq!(stats.passes, 1);
    assert_eq!(stats.rollbacks, 1);
    assert_eq!(stats.fail_closed, 1);
}

#[tokio::test]
async fn brain_verify_time_filter() {
    let repo = test_repo().await;

    repo.record_brain_verify("v-1", "SOUL.md", "pass", None)
        .await
        .unwrap();

    // Future epoch filters out
    let stats = repo.brain_verify_stats(Some(9999999999)).await.unwrap();
    assert_eq!(stats.passes, 0);

    // Past epoch includes
    let stats = repo.brain_verify_stats(Some(1)).await.unwrap();
    assert_eq!(stats.passes, 1);
}

// ─── Tool Executions Nullable Columns ─────────────────────────────────

#[tokio::test]
async fn tool_executions_nullable_columns_accept_null() {
    let repo = test_repo().await;
    let pool = repo.pool();

    let conn = pool.get().await.expect("should get connection");

    // Insert a tool execution WITHOUT provider/model/duration (pre-migration style)
    conn.interact(|conn| {
        conn.execute(
            "INSERT INTO tool_executions (id, message_id, session_id, tool_name, status, created_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            rusqlite::params!["te-1", "msg-1", "sess-1", "bash", "success", 1722400000i64],
        )
    })
    .await
    .expect("interact should succeed")
    .expect("insert should succeed");

    // Query it back - nullable columns should be NULL
    let row = conn
        .interact(|conn| {
            conn.query_row(
                "SELECT provider, model, duration_ms FROM tool_executions WHERE id = ?1",
                rusqlite::params!["te-1"],
                |row| {
                    Ok((
                        row.get::<_, Option<String>>(0)?,
                        row.get::<_, Option<String>>(1)?,
                        row.get::<_, Option<i64>>(2)?,
                    ))
                },
            )
        })
        .await
        .expect("interact should succeed")
        .expect("query should succeed");

    assert_eq!(row.0, None); // provider is NULL
    assert_eq!(row.1, None); // model is NULL
    assert_eq!(row.2, None); // duration_ms is NULL
}

#[tokio::test]
async fn tool_executions_with_provider_model_duration() {
    let repo = test_repo().await;
    let pool = repo.pool();

    let conn = pool.get().await.expect("should get connection");

    // Insert a tool execution WITH provider/model/duration (new style)
    conn.interact(|conn| {
        conn.execute(
            "INSERT INTO tool_executions (id, message_id, session_id, tool_name, status, created_at, provider, model, duration_ms)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
            rusqlite::params![
                "te-2", "msg-2", "sess-2", "read_file", "success", 1722400000i64,
                "anthropic", "claude-opus-4-8", 150i64
            ],
        )
    })
    .await
    .expect("interact should succeed")
    .expect("insert should succeed");

    let row = conn
        .interact(|conn| {
            conn.query_row(
                "SELECT provider, model, duration_ms FROM tool_executions WHERE id = ?1",
                rusqlite::params!["te-2"],
                |row| {
                    Ok((
                        row.get::<_, Option<String>>(0)?,
                        row.get::<_, Option<String>>(1)?,
                        row.get::<_, Option<i64>>(2)?,
                    ))
                },
            )
        })
        .await
        .expect("interact should succeed")
        .expect("query should succeed");

    assert_eq!(row.0.as_deref(), Some("anthropic"));
    assert_eq!(row.1.as_deref(), Some("claude-opus-4-8"));
    assert_eq!(row.2, Some(150));
}

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

#[tokio::test]
async fn tool_stats_by_model_groups_correctly() {
    let repo = test_repo().await;
    let pool = repo.pool();

    {
        let conn = pool.get().await.expect("should get connection");

        // Insert tool executions with different models
        conn.interact(|conn| {
            conn.execute_batch(
                "INSERT INTO tool_executions (id, message_id, session_id, tool_name, status, created_at, model)
                 VALUES ('t1', 'm1', 's1', 'bash', 'success', 1722400000, 'claude-opus-4-8');
                 INSERT INTO tool_executions (id, message_id, session_id, tool_name, status, created_at, model)
                 VALUES ('t2', 'm1', 's1', 'read_file', 'error', 1722400001, 'claude-opus-4-8');
                 INSERT INTO tool_executions (id, message_id, session_id, tool_name, status, created_at, model)
                 VALUES ('t3', 'm1', 's1', 'grep', 'success', 1722400002, 'qwen3.8-max');
                 INSERT INTO tool_executions (id, message_id, session_id, tool_name, status, created_at, model)
                 VALUES ('t4', 'm1', 's1', 'bash', 'error', 1722400003, 'qwen3.8-max');
                 INSERT INTO tool_executions (id, message_id, session_id, tool_name, status, created_at, model)
                 VALUES ('t5', 'm1', 's1', 'ls', 'error', 1722400004, 'qwen3.8-max');",
            )
        })
        .await
        .expect("interact should succeed")
        .expect("insert should succeed");
    } // conn dropped here, pool connection released

    let stats = repo.tool_stats_by_model(None).await.unwrap();
    assert_eq!(stats.len(), 2);

    // qwen has 3 calls (2 errors), claude has 2 calls (1 error)
    // Ordered by COUNT(*) DESC
    assert_eq!(stats[0].model, "qwen3.8-max");
    assert_eq!(stats[0].total, 3);
    assert_eq!(stats[0].failures, 2);

    assert_eq!(stats[1].model, "claude-opus-4-8");
    assert_eq!(stats[1].total, 2);
    assert_eq!(stats[1].failures, 1);
}

#[tokio::test]
async fn tool_stats_by_model_null_model_is_unknown() {
    let repo = test_repo().await;
    let pool = repo.pool();

    {
        let conn = pool.get().await.expect("should get connection");

        conn.interact(|conn| {
            conn.execute(
                "INSERT INTO tool_executions (id, message_id, session_id, tool_name, status, created_at)
                 VALUES ('t-null', 'm1', 's1', 'bash', 'success', 1722400000)",
                [],
            )
        })
        .await
        .expect("interact should succeed")
        .expect("insert should succeed");
    } // conn dropped here, pool connection released

    let stats = repo.tool_stats_by_model(None).await.unwrap();
    assert_eq!(stats.len(), 1);
    assert_eq!(stats[0].model, "unknown");
    assert_eq!(stats[0].total, 1);
    assert_eq!(stats[0].failures, 0);
}