difflore-cli 0.2.0

Your AI coding agent learned public code, not your team's private decisions. difflore turns past PR reviews into source-backed local rules.
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
//! Integration tests for the since-last-session banner SQL path, end to end
//! against a temporary SQLite DB. We avoid `init_db()` (a global pool cache)
//! and build a fresh `:memory:` pool with the exact schema columns the query
//! touches.
//!
//! Watermark IO is tested separately in `watermark.rs::tests`. The end-to-end
//! `render_since_last_session_banner` helper isn't exercised here because it
//! touches the real `init_db()` pool cache, which would race other tests; the
//! inner pipeline is covered transitively by the query + render tests.

use super::query::{NewRule, memory_pulse_since, new_rules_since};

/// Spin up an in-memory SQLite pool with the minimal `skills`
/// columns the query reads. Mirrors the production schema exactly so
/// the query string is bit-for-bit the same one shipping in prod.
async fn fresh_skills_pool() -> sqlx::SqlitePool {
    let pool = sqlx::sqlite::SqlitePoolOptions::new()
        .max_connections(1)
        .connect("sqlite::memory:")
        .await
        .expect("open in-memory sqlite");
    sqlx::query(
        r"CREATE TABLE skills (
            id            TEXT PRIMARY KEY NOT NULL,
            name          TEXT NOT NULL,
            origin        TEXT NOT NULL DEFAULT 'manual',
            source_repo   TEXT,
            status        TEXT NOT NULL DEFAULT 'active',
            installed_at  TEXT DEFAULT (datetime('now')) NOT NULL
        )",
    )
    .execute(&pool)
    .await
    .expect("create skills");
    pool
}

async fn insert(
    pool: &sqlx::SqlitePool,
    id: &str,
    name: &str,
    origin: &str,
    source_repo: Option<&str>,
    status: &str,
    installed_at_iso: &str,
) {
    sqlx::query(
        r"INSERT INTO skills (id, name, origin, source_repo, status, installed_at)
          VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
    )
    .bind(id)
    .bind(name)
    .bind(origin)
    .bind(source_repo)
    .bind(status)
    .bind(installed_at_iso)
    .execute(pool)
    .await
    .expect("insert skill");
}

async fn insert_auto_enabled_event(pool: &sqlx::SqlitePool, rule_id: &str, created_at_iso: &str) {
    sqlx::query(
        r"INSERT INTO memory_autopilot_events
          (event_type, rule_id, item_ids_json, group_id, title, reason, payload_json, created_at)
          VALUES ('auto_enabled', ?1, '[]', 'g1', 'Auto enabled', 'test', '{}', ?2)",
    )
    .bind(rule_id)
    .bind(created_at_iso)
    .execute(pool)
    .await
    .expect("insert auto event");
}

async fn insert_pulse_event(
    pool: &sqlx::SqlitePool,
    event_type: &str,
    group_id: &str,
    payload_json: &str,
    created_at_iso: &str,
) {
    sqlx::query(
        r"INSERT INTO memory_autopilot_events
          (event_type, rule_id, item_ids_json, group_id, title, reason, payload_json, created_at)
          VALUES (?1, NULL, '[]', ?2, 'Pulse event', 'test', ?3, ?4)",
    )
    .bind(event_type)
    .bind(group_id)
    .bind(payload_json)
    .bind(created_at_iso)
    .execute(pool)
    .await
    .expect("insert pulse event");
}

#[tokio::test]
async fn first_session_with_none_watermark_returns_recent_rules_for_repo() {
    // No prior watermark → we should see every active rule for this
    // repo, capped at the limit, newest-first. Rules for OTHER repos
    // must be filtered out.
    let pool = fresh_skills_pool().await;
    insert(
        &pool,
        "r1",
        "Return 413 for body size limit errors",
        "pr_review",
        Some("acme/billing"),
        "active",
        "2026-05-21T12:00:00Z",
    )
    .await;
    insert(
        &pool,
        "r2",
        "Wrap context cancellation in errgroup",
        "extracted",
        Some("acme/billing"),
        "active",
        "2026-05-22T13:00:00Z",
    )
    .await;
    // Different repo — must be excluded.
    insert(
        &pool,
        "r3",
        "Irrelevant rule from another repo",
        "pr_review",
        Some("other-org/other-repo"),
        "active",
        "2026-05-23T14:00:00Z",
    )
    .await;

    let aliases = vec!["acme/billing".to_owned()];
    let rows = new_rules_since(&pool, None, &aliases, 5)
        .await
        .expect("query ok");
    assert_eq!(
        rows.len(),
        2,
        "expected 2 rows scoped to acme/billing, got {rows:?}"
    );
    // Newest first.
    assert_eq!(rows[0].title, "Wrap context cancellation in errgroup");
    assert_eq!(rows[1].title, "Return 413 for body size limit errors");
}

#[tokio::test]
async fn watermark_filters_to_only_rules_newer_than_prev_ts() {
    let pool = fresh_skills_pool().await;
    insert(
        &pool,
        "r1",
        "Old rule",
        "manual",
        Some("acme/billing"),
        "active",
        "2026-05-20T10:00:00Z",
    )
    .await;
    insert(
        &pool,
        "r2",
        "New rule",
        "pr_review",
        Some("acme/billing"),
        "active",
        "2026-05-22T10:00:00Z",
    )
    .await;

    // Watermark = 2026-05-21T00:00:00Z → only `r2` should surface.
    let prev_ms = chrono::DateTime::parse_from_rfc3339("2026-05-21T00:00:00Z")
        .expect("parse")
        .timestamp_millis();
    let aliases = vec!["acme/billing".to_owned()];
    let rows = new_rules_since(&pool, Some(prev_ms), &aliases, 5)
        .await
        .expect("query ok");
    assert_eq!(rows.len(), 1, "got: {rows:?}");
    assert_eq!(rows[0].title, "New rule");
}

#[tokio::test]
async fn auto_enabled_event_surfaces_old_installed_rule() {
    let pool = fresh_skills_pool().await;
    insert(
        &pool,
        "r1",
        "Old draft promoted later",
        "pr_review",
        Some("acme/billing"),
        "active",
        "2026-05-10T10:00:00Z",
    )
    .await;
    let prev_ms = chrono::DateTime::parse_from_rfc3339("2026-05-21T00:00:00Z")
        .expect("parse")
        .timestamp_millis();
    let aliases = vec!["acme/billing".to_owned()];

    // The query creates the events table on demand; insert the event after
    // one quiet query to mirror an existing DB where the table may be absent.
    let quiet = new_rules_since(&pool, Some(prev_ms), &aliases, 5)
        .await
        .expect("quiet query");
    assert!(
        quiet.is_empty(),
        "installed_at alone must not surface: {quiet:?}"
    );
    insert_auto_enabled_event(&pool, "r1", "2026-05-22T10:00:00Z").await;

    let rows = new_rules_since(&pool, Some(prev_ms), &aliases, 5)
        .await
        .expect("query ok");
    assert_eq!(rows.len(), 1, "got: {rows:?}");
    assert_eq!(rows[0].title, "Old draft promoted later");
    assert_eq!(rows[0].origin, "autopilot");
}

#[tokio::test]
async fn memory_pulse_counts_repo_scoped_folded_and_confirm_events() {
    let pool = fresh_skills_pool().await;
    let aliases = vec!["acme/billing".to_owned()];
    let prev_ms = chrono::DateTime::parse_from_rfc3339("2026-05-21T00:00:00Z")
        .expect("parse")
        .timestamp_millis();

    // Create the events table through the query helper, then add events.
    let quiet = memory_pulse_since(&pool, Some(prev_ms), &aliases)
        .await
        .expect("quiet pulse");
    assert_eq!(quiet.folded_away, 0);
    insert_pulse_event(
        &pool,
        "session_candidate_superseded",
        "acme/billing:tauri-dev:src",
        r#"{"supersededCount":3}"#,
        "2026-05-22T10:00:00Z",
    )
    .await;
    insert_pulse_event(
        &pool,
        "session_candidate_dropped_low_signal",
        "acme/billing:tmp-helper:tmp",
        "{}",
        "2026-05-22T10:01:00Z",
    )
    .await;
    insert_pulse_event(
        &pool,
        "agent_file_review_rule_pending",
        "acme/billing:review-rule:src",
        "{}",
        "2026-05-22T10:02:00Z",
    )
    .await;
    insert_pulse_event(
        &pool,
        "session_candidate_superseded",
        "other/repo:tauri-dev:src",
        r#"{"supersededCount":9}"#,
        "2026-05-22T10:03:00Z",
    )
    .await;

    let pulse = memory_pulse_since(&pool, Some(prev_ms), &aliases)
        .await
        .expect("pulse");

    assert_eq!(pulse.folded_away, 4);
    assert_eq!(pulse.to_confirm, 1);
}

#[tokio::test]
async fn pending_and_no_source_repo_rules_are_excluded() {
    let pool = fresh_skills_pool().await;
    // Pending → excluded.
    insert(
        &pool,
        "r1",
        "Unverified rule",
        "conversation",
        Some("acme/billing"),
        "pending",
        "2026-05-22T10:00:00Z",
    )
    .await;
    // No source_repo → excluded (can't be attributed to a repo).
    insert(
        &pool,
        "r2",
        "Orphan rule",
        "manual",
        None,
        "active",
        "2026-05-22T11:00:00Z",
    )
    .await;
    // Active + scoped → included.
    insert(
        &pool,
        "r3",
        "Good rule",
        "pr_review",
        Some("acme/billing"),
        "active",
        "2026-05-22T12:00:00Z",
    )
    .await;

    let aliases = vec!["acme/billing".to_owned()];
    let rows = new_rules_since(&pool, None, &aliases, 5)
        .await
        .expect("query ok");
    assert_eq!(rows.len(), 1, "got: {rows:?}");
    assert_eq!(rows[0].title, "Good rule");
}

#[tokio::test]
async fn empty_alias_list_returns_empty_without_querying() {
    // Defensive: a repo with no detectable supported origin shouldn't
    // pull every rule on the user's machine. The query function
    // early-outs so even if `data.db` has a million skills, we do no
    // I/O and emit nothing.
    let pool = fresh_skills_pool().await;
    insert(
        &pool,
        "r1",
        "Should never show up",
        "pr_review",
        Some("acme/billing"),
        "active",
        "2026-05-22T10:00:00Z",
    )
    .await;
    let rows: Vec<NewRule> = new_rules_since(&pool, None, &[], 5)
        .await
        .expect("query ok");
    assert!(
        rows.is_empty(),
        "empty aliases must yield empty result; got {rows:?}"
    );
}

#[tokio::test]
async fn case_insensitive_alias_matching() {
    // `source_repo` was stored in mixed case during the cloud sync,
    // but the alias list arrives lower-cased. The `LOWER()` in the
    // query covers the mismatch.
    let pool = fresh_skills_pool().await;
    insert(
        &pool,
        "r1",
        "Cased rule",
        "pr_review",
        Some("Acme/Billing"),
        "active",
        "2026-05-22T10:00:00Z",
    )
    .await;

    let aliases = vec!["acme/billing".to_owned()];
    let rows = new_rules_since(&pool, None, &aliases, 5)
        .await
        .expect("query ok");
    assert_eq!(rows.len(), 1, "case-insensitive match failed: {rows:?}");
}

#[tokio::test]
async fn limit_caps_returned_rows() {
    let pool = fresh_skills_pool().await;
    for i in 0..10 {
        insert(
            &pool,
            &format!("r{i}"),
            &format!("Rule {i}"),
            "pr_review",
            Some("acme/billing"),
            "active",
            &format!("2026-05-{:02}T10:00:00Z", i + 1),
        )
        .await;
    }
    let aliases = vec!["acme/billing".to_owned()];
    let rows = new_rules_since(&pool, None, &aliases, 5)
        .await
        .expect("query ok");
    assert_eq!(rows.len(), 5, "limit not enforced: {rows:?}");
    // Newest first → Rule 9 ranks first.
    assert_eq!(rows[0].title, "Rule 9");
}

#[test]
fn project_root_command_is_bounded_by_timeout() {
    let dir = tempfile::TempDir::new().expect("tempdir");
    let started = std::time::Instant::now();

    let err = super::run_command_with_timeout(
        dir.path(),
        "sh",
        &["-c", "sleep 2"],
        std::time::Duration::from_millis(20),
    )
    .expect_err("slow command must time out");

    assert_eq!(err.kind(), std::io::ErrorKind::TimedOut);
    assert!(
        started.elapsed() < std::time::Duration::from_secs(1),
        "timeout should kill the command promptly"
    );
}

#[test]
fn remote_verbose_aliases_use_origin_then_upstream_without_duplicates() {
    let stdout = "\
origin\tgit@github.com:Acme/App.git (fetch)
origin\tgit@github.com:Acme/App.git (push)
upstream\thttps://github.com/upstream/app.git (fetch)
upstream\thttps://github.com/upstream/app.git (push)
";

    let aliases = super::repo_aliases_from_remote_verbose(stdout, &[]);
    assert_eq!(aliases, vec!["acme/app", "upstream/app"]);
}

#[test]
fn remote_verbose_aliases_accept_configured_gitlab_hosts() {
    let stdout = "\
origin\tgit@gitlab.example.com:Platform/App.git (fetch)
upstream\thttps://gitlab.com/acme/fallback.git (fetch)
";

    let aliases =
        super::repo_aliases_from_remote_verbose(stdout, &["gitlab.example.com".to_owned()]);
    assert_eq!(
        aliases,
        vec![
            "gitlab.example.com/platform/app",
            "gitlab.com/acme/fallback"
        ]
    );
}