difflore-core 0.1.0

Core library for the difflore CLI — rule store, retrieval, MCP server, hooks, cloud sync. Not intended for direct use; depend on `difflore-cli` instead.
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use sha2::{Digest, Sha256};
use uuid::Uuid;

use crate::errors::CoreError;
use crate::models::{
    AddExampleInput, ListExamplesInput, RemoveExampleInput, RuleExampleRecord, SkillRecord,
    SkillRepoAddInput, SkillRepoRecord, SkillRepoRemoveInput, UpdateConfidenceInput,
};

use super::{SkillRepoRow, SkillRow};

/// Pick the cloud-side text the local `skills.description` column should
/// hold. Cloud `rules_cloud` has both `description` (a summary) and
/// `content` (the full body), but the local schema only stores
/// `description`. Store the full `content` when present so the desktop
/// hash matches cloud's `/rules/sync` contract (`sha256(rule.content)`)
/// and retrieval/indexing gets the richest rule text.
fn effective_description(rule: &crate::cloud::sync::SyncedRule) -> String {
    if !rule.content.trim().is_empty() {
        return rule.content.clone();
    }
    rule.description.clone()
}

/// Derive the local `confidence_score` for a synced cloud rule from its
/// `cluster-size:N` / `severity:X` tags. Returns `None` when neither
/// signal is present so the caller leaves the column at the DB default
/// (0.7). See `crate::context::rule_source::confidence_from_tags` for
/// the actual mapping (singletons → 0.55, big clusters → 0.9).
fn effective_confidence(rule: &crate::cloud::sync::SyncedRule) -> Option<f64> {
    let tags_json = serde_json::to_string(&rule.tags).ok()?;
    crate::context::rule_source::confidence_from_tags(&tags_json)
}

pub(crate) fn cloud_rule_directory_name(rule_id: &str) -> String {
    let mut slug = String::with_capacity(rule_id.len().min(96));
    let mut last_dash = false;
    for ch in rule_id.trim().chars() {
        let safe = if ch.is_ascii_alphanumeric() || ch == '_' || ch == '-' {
            ch.to_ascii_lowercase()
        } else {
            '-'
        };
        if safe == '-' {
            if !last_dash {
                slug.push('-');
                last_dash = true;
            }
        } else {
            slug.push(safe);
            last_dash = false;
        }
    }
    let slug = slug.trim_matches('-');
    let needs_hash = slug.is_empty()
        || slug != rule_id
        || slug.chars().count() > 96
        || is_windows_reserved_path_name(slug);
    let base = if slug.is_empty() { "rule" } else { slug };
    if !needs_hash {
        return base.to_owned();
    }

    let head: String = base.chars().take(80).collect();
    format!("{head}-{}", short_rule_id_hash(rule_id))
}

fn short_rule_id_hash(rule_id: &str) -> String {
    let digest = Sha256::digest(rule_id.as_bytes());
    let mut hex = String::with_capacity(12);
    for byte in digest.iter().take(6) {
        hex.push_str(&format!("{byte:02x}"));
    }
    hex
}

fn is_windows_reserved_path_name(name: &str) -> bool {
    let lower = name.trim_end_matches('.').to_ascii_lowercase();
    matches!(
        lower.as_str(),
        "con"
            | "prn"
            | "aux"
            | "nul"
            | "com1"
            | "com2"
            | "com3"
            | "com4"
            | "com5"
            | "com6"
            | "com7"
            | "com8"
            | "com9"
            | "lpt1"
            | "lpt2"
            | "lpt3"
            | "lpt4"
            | "lpt5"
            | "lpt6"
            | "lpt7"
            | "lpt8"
            | "lpt9"
    )
}

/// Re-stamp `skills.confidence_score` for every row whose tags carry a
/// `cluster-size:N` / `severity:X` evidence signal but whose score is
/// still at the historic flat default (0.7). Returns the number of
/// rows updated. Tight tolerance (`±0.001`) so user-customized scores
/// like 0.65 / 0.75 are left alone.
pub async fn backfill_skills_confidence_from_tags(db: &sqlx::SqlitePool) -> crate::Result<i64> {
    let rows =
        sqlx::query!("SELECT id, tags, confidence_score FROM skills WHERE status = 'active'")
            .fetch_all(db)
            .await?;

    let mut updated = 0_i64;
    for row in rows {
        let id = row.id;
        let tags = row.tags;
        let current = row.confidence_score;
        if (current - 0.7).abs() > 0.001 {
            continue;
        }
        let Some(new_score) = crate::context::rule_source::confidence_from_tags(&tags) else {
            continue;
        };
        if (new_score - current).abs() < 0.001 {
            continue;
        }
        let _ = sqlx::query!(
            "UPDATE skills SET confidence_score = ?1 WHERE id = ?2",
            new_score,
            id
        )
        .execute(db)
        .await;
        updated += 1;
    }
    Ok(updated)
}

/// Cloud review memory is agent-agnostic: once a team rule is synced
/// locally, Codex/Cursor/Gemini should get the same protection Claude
/// gets. Older syncs wrote only `enabled_for_claude=1`, which made the
/// CLI look full of rules while Codex MCP recall returned nothing.
pub async fn backfill_cloud_rules_enabled_for_all_agents(
    db: &sqlx::SqlitePool,
) -> crate::Result<u64> {
    let result = sqlx::query!(
        "UPDATE skills \
         SET enabled_for_codex = 1, \
             enabled_for_claude = 1, \
             enabled_for_gemini = 1, \
             enabled_for_cursor = 1, \
             updated_at = datetime('now') \
         WHERE source = 'cloud' \
           AND status = 'active' \
           AND (enabled_for_codex = 0 \
                OR enabled_for_claude = 0 \
                OR enabled_for_gemini = 0 \
                OR enabled_for_cursor = 0)",
    )
    .execute(db)
    .await?;
    Ok(result.rows_affected())
}

async fn apply_cloud_source_repo(
    tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
    skill_id: &str,
    incoming_repo: Option<&str>,
) -> crate::Result<()> {
    let Some(incoming_repo) = incoming_repo.map(str::trim).filter(|repo| !repo.is_empty()) else {
        return Ok(());
    };

    let updated = sqlx::query(
        "UPDATE skills
         SET source_repo = ?1
         WHERE id = ?2
           AND source = 'cloud'
           AND (source_repo IS NULL OR trim(source_repo) = '' OR source_repo = ?1)",
    )
    .bind(incoming_repo)
    .bind(skill_id)
    .execute(&mut **tx)
    .await?;
    if updated.rows_affected() > 0 {
        return Ok(());
    }

    let existing = sqlx::query_as::<_, (Option<String>, String)>(
        "SELECT source_repo, source FROM skills WHERE id = ?1",
    )
    .bind(skill_id)
    .fetch_optional(&mut **tx)
    .await?;
    let Some((Some(existing_repo), source)) = existing else {
        return Ok(());
    };
    if source != "cloud" || existing_repo.trim().is_empty() || existing_repo == incoming_repo {
        return Ok(());
    }

    let event_id = format!("rule-event-{}", Uuid::new_v4());
    let reason = format!(
        "cloud sync kept existing source_repo '{existing_repo}' and ignored incoming '{incoming_repo}'"
    );
    let metadata = serde_json::json!({
        "existingSourceRepo": existing_repo,
        "incomingSourceRepo": incoming_repo,
    })
    .to_string();
    sqlx::query(
        "INSERT INTO rule_events
         (id, skill_id, kind, source, reason, metadata)
         VALUES (?1, ?2, 'source_repo_conflict', 'cloud_sync', ?3, ?4)",
    )
    .bind(event_id)
    .bind(skill_id)
    .bind(reason)
    .bind(metadata)
    .execute(&mut **tx)
    .await?;

    Ok(())
}

async fn refresh_rule_index_after_sync(db: &sqlx::SqlitePool) {
    let project_hash = crate::db::project_hash_from_root(&crate::db::current_project_root());
    let index_pool = match crate::context::index_db::get_pool_for_project(&project_hash).await {
        Ok(pool) => pool,
        Err(e) => {
            eprintln!("[difflore] cloud sync rule-index refresh skipped: {e}");
            return;
        }
    };
    if let Err(e) = crate::context::orchestrator::ensure_rules_indexed(db, &index_pool).await {
        eprintln!("[difflore] cloud sync rule-index refresh failed: {e}");
    }
}

pub async fn apply_sync_result(
    db: &sqlx::SqlitePool,
    result: &crate::cloud::sync::SyncResult,
) -> crate::Result<()> {
    let now = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
    let sync_changed =
        !result.created.is_empty() || !result.updated.is_empty() || !result.deleted.is_empty();
    let mut tx = db.begin().await?;

    for rule in &result.created {
        let engines_json = serde_json::to_string(&rule.engines)?;
        let tags_json = serde_json::to_string(&rule.tags)?;
        let directory = cloud_rule_directory_name(&rule.id);
        // Iter-9: persist file_patterns so the local cascade can use them.
        let file_patterns_json = if rule.file_patterns.is_empty() {
            None
        } else {
            Some(serde_json::to_string(&rule.file_patterns)?)
        };
        let description = effective_description(rule);
        // 2026-04-20: capture cloud-side origin when present. Falls back
        // to "cloud" so audit pages still distinguish remotely-fetched
        // rules from locally-typed ones — the migration default of
        // `manual` would lie in this case.
        let origin = rule.origin.clone().unwrap_or_else(|| "cloud".to_owned());
        let directory_param = directory.as_str();
        sqlx::query(
            "INSERT INTO skills
             (id, name, source, directory, version, description, type, engines, tags,
              trigger, check_prompt, file_patterns,
              enabled_for_codex, enabled_for_claude, enabled_for_gemini, enabled_for_cursor,
              installed_at, updated_at, origin)
             VALUES (?1, ?2, 'cloud', ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11,
                     1, 1, 1, 1, ?12, ?12, ?13)
             ON CONFLICT(id) DO UPDATE SET
                name = excluded.name,
                directory = excluded.directory,
                version = excluded.version,
                description = excluded.description,
                type = excluded.type,
                engines = excluded.engines,
                tags = excluded.tags,
                trigger = excluded.trigger,
                check_prompt = excluded.check_prompt,
                file_patterns = excluded.file_patterns,
                updated_at = excluded.updated_at,
                origin = excluded.origin,
                status = 'active'
             WHERE skills.source = 'cloud'",
        )
        .bind(&rule.id)
        .bind(&rule.name)
        .bind(directory_param)
        .bind(&rule.version)
        .bind(&description)
        .bind(&rule.r#type)
        .bind(&engines_json)
        .bind(&tags_json)
        .bind(rule.trigger.as_deref())
        .bind(rule.check_prompt.as_deref())
        .bind(file_patterns_json.as_deref())
        .bind(&now)
        .bind(&origin)
        .execute(&mut *tx)
        .await?;
        apply_cloud_source_repo(&mut tx, &rule.id, rule.source_repo.as_deref()).await?;
        // 2026-04-27: stamp confidence_score from cluster-size/severity
        // tags. Only when tags carry an evidence signal AND the row is
        // still at the historic DB default (0.7 ± 0.001) — leaves
        // user-customized scores like 0.1 (rejected) or 0.85 (boosted)
        // untouched on resync.
        if let Some(conf) = effective_confidence(rule) {
            let _ = sqlx::query!(
                "UPDATE skills SET confidence_score = ?1 \
                 WHERE id = ?2 AND ABS(confidence_score - 0.7) < 0.001",
                conf,
                rule.id
            )
            .execute(&mut *tx)
            .await;
        }
    }

    for rule in &result.updated {
        let engines_json = serde_json::to_string(&rule.engines)?;
        let tags_json = serde_json::to_string(&rule.tags)?;
        let directory = cloud_rule_directory_name(&rule.id);
        let file_patterns_json = if rule.file_patterns.is_empty() {
            None
        } else {
            Some(serde_json::to_string(&rule.file_patterns)?)
        };
        let description = effective_description(rule);
        sqlx::query(
            "UPDATE skills SET name = ?1, description = ?2, type = ?3, version = ?4,
             engines = ?5, tags = ?6, trigger = ?7, check_prompt = ?8, file_patterns = ?9,
             directory = ?10,
             updated_at = ?11,
             origin = COALESCE(?12, origin),
             status = 'active'
             WHERE id = ?13 AND source = 'cloud'",
        )
        .bind(&rule.name)
        .bind(&description)
        .bind(&rule.r#type)
        .bind(&rule.version)
        .bind(&engines_json)
        .bind(&tags_json)
        .bind(rule.trigger.as_deref())
        .bind(rule.check_prompt.as_deref())
        .bind(file_patterns_json.as_deref())
        .bind(&directory)
        .bind(&now)
        .bind(rule.origin.as_deref())
        .bind(&rule.id)
        .execute(&mut *tx)
        .await?;
        apply_cloud_source_repo(&mut tx, &rule.id, rule.source_repo.as_deref()).await?;
        // Same default-only guard as the INSERT path: don't clobber
        // user-customized scores like 0.1 (rejected) or 0.85 (boosted)
        // when re-syncing rules.
        if let Some(conf) = effective_confidence(rule) {
            let _ = sqlx::query!(
                "UPDATE skills SET confidence_score = ?1 \
                 WHERE id = ?2 AND ABS(confidence_score - 0.7) < 0.001",
                conf,
                rule.id
            )
            .execute(&mut *tx)
            .await;
        }
    }

    for id in &result.deleted {
        sqlx::query("DELETE FROM skills WHERE id = ?1 AND source = 'cloud'")
            .bind(id)
            .execute(&mut *tx)
            .await?;
    }

    tx.commit().await?;

    // Self-heal: re-stamp confidence_score on rows still at the
    // historic 0.7 default but with cluster-size/severity tags. Cheap,
    // idempotent, runs once per sync — gives users with already-synced
    // corpora the new ranking weights without waiting for cloud-side
    // updated_at to bump.
    let _ = backfill_skills_confidence_from_tags(db).await;
    if sync_changed {
        refresh_rule_index_after_sync(db).await;
    }

    Ok(())
}

/// Lightweight metadata for recall/search flows. Returns just enough to
/// explain why a rule surfaced
/// (`file_patterns` + canonical `source_repo`). Uses dynamic SQL so it doesn't pin
/// the sqlx offline cache to current schema.
#[derive(Debug, Clone, Default)]
pub struct SearchSkillMeta {
    pub file_patterns: Vec<String>,
    pub source_repo: Option<String>,
}

pub async fn fetch_search_meta(
    pool: &sqlx::SqlitePool,
    ids: &[String],
) -> std::collections::HashMap<String, SearchSkillMeta> {
    let mut out = std::collections::HashMap::new();
    if ids.is_empty() {
        return out;
    }
    let Ok(ids_json) = serde_json::to_string(ids) else {
        return out;
    };
    let Ok(rows) = sqlx::query_as::<_, (String, Option<String>, Option<String>)>(
        "SELECT id, file_patterns, source_repo
           FROM skills WHERE id IN (SELECT value FROM json_each(?1))",
    )
    .bind(ids_json)
    .fetch_all(pool)
    .await
    else {
        return out;
    };
    for (id, file_patterns_raw, source_repo) in rows {
        let file_patterns: Vec<String> = file_patterns_raw
            .as_deref()
            .and_then(|s| serde_json::from_str(s).ok())
            .unwrap_or_default();
        out.insert(
            id,
            SearchSkillMeta {
                file_patterns,
                source_repo,
            },
        );
    }
    out
}

pub async fn list_review_standards(db: &sqlx::SqlitePool) -> crate::Result<Vec<SkillRecord>> {
    let rows = sqlx::query_as!(
        SkillRow,
        "SELECT id, name, source, directory, version, description, type, \
         engines, tags, trigger, check_prompt, repo_owner, repo_name, repo_branch, readme_url, \
         enabled_for_codex, enabled_for_claude, enabled_for_gemini, enabled_for_cursor, \
         installed_at, updated_at, origin FROM skills \
         WHERE type = 'review_standard' AND status = 'active' \
         ORDER BY installed_at DESC",
    )
    .fetch_all(db)
    .await?;
    Ok(rows.into_iter().map(SkillRecord::from).collect())
}

/// List skills across **all types** but **active status only** —
/// pending candidates and soft-deleted rows are filtered out at the
/// SQL level.
///
/// "all" means all `type`s, not all statuses. Several callers rely on this
/// active-only filter and pair it with their own pending checks; if you change
/// the WHERE clause, audit those call sites.
pub async fn list_all_skills(db: &sqlx::SqlitePool) -> crate::Result<Vec<SkillRecord>> {
    let rows = sqlx::query_as!(
        SkillRow,
        "SELECT id, name, source, directory, version, description, type, \
         engines, tags, trigger, check_prompt, repo_owner, repo_name, repo_branch, readme_url, \
         enabled_for_codex, enabled_for_claude, enabled_for_gemini, enabled_for_cursor, \
         installed_at, updated_at, origin FROM skills WHERE status = 'active' \
         ORDER BY installed_at DESC",
    )
    .fetch_all(db)
    .await?;
    Ok(rows.into_iter().map(SkillRecord::from).collect())
}

pub async fn export_rules_markdown(db: &sqlx::SqlitePool) -> crate::Result<String> {
    let skills = list_all_skills(db).await?;
    if skills.is_empty() {
        return Ok("# Project Rules\n\n_No rules found._\n".to_owned());
    }

    let skill_ids: Vec<String> = skills.iter().map(|s| s.id.clone()).collect();
    let examples_map =
        crate::context::rule_source::load_rule_examples_batch(db, &skill_ids).await?;

    let mut md = String::from("# Project Rules\n");

    for (i, skill) in skills.iter().enumerate() {
        md.push_str(&format!("\n## {}\n\n", skill.name));
        md.push_str(&format!("{}\n", skill.description));

        if let Some(cp) = &skill.check_prompt
            && !cp.is_empty()
        {
            md.push_str(&format!("\n**Check prompt:** {cp}\n"));
        }

        if let Some(examples) = examples_map.get(&skill.id)
            && !examples.is_empty()
        {
            md.push_str("\n### Examples\n");
            for ex in examples {
                md.push_str("\n❌ Bad:\n```\n");
                md.push_str(&ex.bad_code);
                md.push_str("\n```\n\n✅ Good:\n```\n");
                md.push_str(&ex.good_code);
                md.push_str("\n```\n");
                if let Some(desc) = &ex.description
                    && !desc.is_empty()
                {
                    md.push_str(&format!("\n{desc}\n"));
                }
            }
        }

        if i < skills.len() - 1 {
            md.push_str("\n---\n");
        }
    }

    Ok(md)
}

pub async fn repos_list(db: &sqlx::SqlitePool) -> crate::Result<Vec<SkillRepoRecord>> {
    let rows = sqlx::query_as!(SkillRepoRow,
        "SELECT id, owner, name, branch, enabled, created_at FROM skill_repos ORDER BY created_at DESC"
    )
    .fetch_all(db)
    .await?;
    Ok(rows.into_iter().map(SkillRepoRecord::from).collect())
}

pub async fn repos_add(
    db: &sqlx::SqlitePool,
    input: SkillRepoAddInput,
) -> crate::Result<SkillRepoRecord> {
    let id = format!("repo-{}-{}", Uuid::new_v4(), input.name);
    let branch = input.branch.unwrap_or_else(|| "main".into());
    let now = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();

    sqlx::query!(
        "INSERT INTO skill_repos (id, owner, name, branch, enabled, created_at) VALUES (?1, ?2, ?3, ?4, 1, ?5)",
        id, input.owner, input.name, branch, now
    )
    .execute(db)
    .await?;

    Ok(SkillRepoRecord {
        id,
        owner: input.owner,
        name: input.name,
        branch,
        enabled: true,
        created_at: now,
    })
}

pub async fn repos_remove(db: &sqlx::SqlitePool, input: SkillRepoRemoveInput) -> crate::Result<()> {
    let result = sqlx::query!("DELETE FROM skill_repos WHERE id = ?1", input.id)
        .execute(db)
        .await?;
    if result.rows_affected() == 0 {
        return Err(CoreError::NotFound(format!(
            "skill repo '{}' not found.",
            input.id
        )));
    }
    Ok(())
}

/// Return value of `update_confidence` so the caller can render a
/// before/after message ("rule X: 0.65 → 0.70"). 2026-04-25: previously
/// returned `()`, which made the feedback loop feel inert from the
/// user's POV. The before/name fields piggy-back on the SELECT we now
/// do anyway to compute the clamped after value.
#[derive(Debug, Clone)]
pub struct ConfidenceChange {
    pub before: f64,
    pub after: f64,
    pub name: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ConfidenceSignal {
    Accept,
    Reject,
}

impl ConfidenceSignal {
    const ALLOWED: &'static [&'static str] = &["accept", "reject"];

    fn parse(raw: &str) -> crate::Result<Self> {
        match raw {
            "accept" => Ok(Self::Accept),
            "reject" => Ok(Self::Reject),
            _ => Err(CoreError::Validation(format!(
                "signal must be one of: {}",
                Self::ALLOWED.join(", ")
            ))),
        }
    }

    const fn delta(self) -> f64 {
        match self {
            Self::Accept => 0.05,
            Self::Reject => -0.1,
        }
    }

    const fn event_kind(self) -> &'static str {
        match self {
            Self::Accept => "feedback_accept",
            Self::Reject => "feedback_dismiss",
        }
    }
}

pub async fn update_confidence(
    db: &sqlx::SqlitePool,
    input: UpdateConfidenceInput,
) -> crate::Result<ConfidenceChange> {
    let signal = ConfidenceSignal::parse(input.signal.as_str())?;
    let delta = signal.delta();

    let mut tx = db.begin().await?;
    let existing = sqlx::query!(
        "SELECT confidence_score, name FROM skills WHERE id = ?1",
        input.skill_id
    )
    .fetch_optional(&mut *tx)
    .await?;
    let row = existing.ok_or_else(|| {
        CoreError::NotFound(format!(
            "rule '{}' not found — cannot apply {} feedback. Run `difflore status --json` to inspect current local memory ids.",
            input.skill_id, input.signal
        ))
    })?;
    let before = row.confidence_score;
    let name = row.name;
    let after = (before + delta).clamp(0.0, 1.0);

    sqlx::query!(
        "UPDATE skills SET confidence_score = ?1 WHERE id = ?2",
        after,
        input.skill_id
    )
    .execute(&mut *tx)
    .await?;

    let event_id = format!("rule-event-{}", Uuid::new_v4());
    let kind = signal.event_kind();
    let metadata = serde_json::json!({
        "signal": input.signal,
        "delta": delta,
    })
    .to_string();
    sqlx::query!(
        "INSERT INTO rule_events
         (id, skill_id, kind, source, confidence_before, confidence_after, reason, metadata)
         VALUES (?1, ?2, ?3, 'local_feedback', ?4, ?5, NULL, ?6)",
        event_id,
        input.skill_id,
        kind,
        before,
        after,
        metadata
    )
    .execute(&mut *tx)
    .await?;

    tx.commit().await?;

    Ok(ConfidenceChange {
        before,
        after,
        name,
    })
}

pub async fn add_example(
    db: &sqlx::SqlitePool,
    input: AddExampleInput,
) -> crate::Result<RuleExampleRecord> {
    let id = format!("example-{}", Uuid::new_v4());
    let now = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
    let source = input.source.unwrap_or_else(|| "manual".into());

    sqlx::query!(
        "INSERT INTO rule_examples (id, skill_id, bad_code, good_code, description, source, created_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
        id,
        input.skill_id,
        input.bad_code,
        input.good_code,
        input.description,
        source,
        now
    )
    .execute(db)
    .await?;

    Ok(RuleExampleRecord {
        id,
        skill_id: input.skill_id,
        bad_code: input.bad_code,
        good_code: input.good_code,
        description: input.description,
        source,
        created_at: now,
    })
}

pub async fn list_examples(
    db: &sqlx::SqlitePool,
    input: ListExamplesInput,
) -> crate::Result<Vec<RuleExampleRecord>> {
    let rows = sqlx::query_as!(
        ExampleRow,
        "SELECT id, skill_id, bad_code, good_code, description, source, created_at FROM rule_examples WHERE skill_id = ?1 ORDER BY created_at DESC",
        input.skill_id
    )
    .fetch_all(db)
    .await?;
    Ok(rows
        .into_iter()
        .map(|r| RuleExampleRecord {
            id: r.id,
            skill_id: r.skill_id,
            bad_code: r.bad_code,
            good_code: r.good_code,
            description: r.description,
            source: r.source,
            created_at: r.created_at,
        })
        .collect())
}

pub async fn remove_example(db: &sqlx::SqlitePool, input: RemoveExampleInput) -> crate::Result<()> {
    let result = sqlx::query!("DELETE FROM rule_examples WHERE id = ?1", input.id)
        .execute(db)
        .await?;
    // SQLite's DELETE silently succeeds with 0 rows affected when the id
    // doesn't exist. Surface that as NotFound so the CLI can tell the
    // user their id was wrong instead of claiming a phantom success.
    if result.rows_affected() == 0 {
        return Err(CoreError::NotFound(format!(
            "example '{}' not found. Run `difflore status --json` to inspect current local memory ids.",
            input.id
        )));
    }
    Ok(())
}

#[derive(sqlx::FromRow)]
struct ExampleRow {
    id: String,
    skill_id: String,
    bad_code: String,
    good_code: String,
    description: Option<String>,
    source: String,
    created_at: String,
}