difflore-core 0.3.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.
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
use super::*;

pub async fn run_memory_autopilot(
    pool: &SqlitePool,
    options: MemoryAutopilotOptions,
) -> Result<MemoryAutopilotReport> {
    let max_auto_enable = normalize_autopilot_limit(options.max_auto_enable);
    let mut plan = build_plan(
        pool,
        MAX_PENDING_SCAN,
        BuildPlanOptions {
            local_ai_curator: true,
            curator_max_candidates: options.curator_max_candidates,
        },
    )
    .await?;
    if !options.dry_run {
        let triage = apply_candidate_triage(pool, &plan.groups, &plan.active_rules).await?;
        if triage.changed {
            plan = build_plan(
                pool,
                MAX_PENDING_SCAN,
                BuildPlanOptions {
                    local_ai_curator: false,
                    curator_max_candidates: None,
                },
            )
            .await?;
        }
    }
    // Persist deterministic conflict records from the autopilot side-effect
    // path (NOT load_memory_digest, which stays read-only). This is a non-AI
    // audit feature, so it must not be gated on the local-AI curator option.
    persist_detected_conflicts(pool, &plan.groups).await?;
    let mut auto_enabled = Vec::new();
    let mut skipped = Vec::new();
    let mut applied = 0usize;

    for group in &mut plan.groups {
        match group.digest.state {
            MemoryCandidateGroupState::AutoEnable if applied < max_auto_enable => {
                if options.dry_run {
                    auto_enabled.push(MemoryAutopilotAction {
                        group_id: group.digest.group_id.clone(),
                        title: group.digest.title.clone(),
                        rule_id: None,
                        item_ids: group.digest.item_ids.clone(),
                        reason: group.digest.reason.clone(),
                        dry_run: true,
                    });
                    applied += 1;
                    continue;
                }

                // A single group failing to enable — e.g. a draft that only
                // collides with an already-active rule *after* refinement
                // recomputes its content hash, so build_plan couldn't pre-flag
                // it `AlreadyActive` — must not abort the whole autopilot run.
                // Skip it and continue; one bad candidate never stalls the
                // queue (mirrors the outbox delivery policy).
                let rule = match enable_group(pool, &group.candidates).await {
                    Ok(rule) => rule,
                    // Candidate-local outcomes (a refined draft that collides
                    // with an already-active rule, or one promoted/removed by a
                    // concurrent run) are skipped so one bad candidate can't
                    // stall the batch. Genuine infrastructure failures
                    // (DB/IO/internal) still propagate and fail the run.
                    Err(err @ (CoreError::Validation(_) | CoreError::NotFound(_))) => {
                        skipped.push(MemoryAutopilotSkip {
                            group_id: group.digest.group_id.clone(),
                            title: group.digest.title.clone(),
                            item_ids: group.digest.item_ids.clone(),
                            reason: format!("skipped (enable failed): {err}"),
                        });
                        continue;
                    }
                    Err(err) => return Err(err),
                };
                let item_ids = group.digest.item_ids.clone();
                let action = MemoryAutopilotAction {
                    group_id: group.digest.group_id.clone(),
                    title: group.digest.title.clone(),
                    rule_id: Some(rule.id.clone()),
                    item_ids,
                    reason: group.digest.reason.clone(),
                    dry_run: false,
                };
                record_autopilot_event(
                    pool,
                    AutopilotEventInput {
                        event_type: "auto_enabled",
                        rule_id: Some(&rule.id),
                        item_ids: &action.item_ids,
                        group_id: Some(&action.group_id),
                        title: &action.title,
                        reason: &action.reason,
                        payload: json!({
                            "ruleId": rule.id,
                            "ruleTitle": rule.name,
                            "source": "memory_autopilot",
                        }),
                    },
                )
                .await?;
                auto_enabled.push(action);
                applied += 1;
            }
            MemoryCandidateGroupState::AutoEnable => {
                skipped.push(MemoryAutopilotSkip {
                    group_id: group.digest.group_id.clone(),
                    title: group.digest.title.clone(),
                    item_ids: group.digest.item_ids.clone(),
                    reason: format!("per-run limit reached ({max_auto_enable})"),
                });
            }
            _ => skipped.push(MemoryAutopilotSkip {
                group_id: group.digest.group_id.clone(),
                title: group.digest.title.clone(),
                item_ids: group.digest.item_ids.clone(),
                reason: group.digest.reason.clone(),
            }),
        }
    }

    if !options.dry_run && !auto_enabled.is_empty() {
        plan = build_plan(
            pool,
            MAX_PENDING_SCAN,
            BuildPlanOptions {
                local_ai_curator: false,
                curator_max_candidates: None,
            },
        )
        .await?;
    }

    Ok(MemoryAutopilotReport {
        dry_run: options.dry_run,
        max_auto_enable,
        auto_enabled,
        skipped,
        digest: plan.digest,
    })
}

pub async fn promote_candidate_with_curator_recommendation(
    pool: &SqlitePool,
    draft_id: &str,
) -> Result<SkillRecord> {
    apply_cached_curator_recommendation_to_draft(pool, draft_id).await?;
    promote_candidate(pool, draft_id).await
}

async fn apply_cached_curator_recommendation_to_draft(
    pool: &SqlitePool,
    draft_id: &str,
) -> Result<()> {
    let Some(mut group) = planned_single_draft_group(pool, draft_id).await? else {
        return Ok(());
    };
    if !pr_review_candidate_group(&group)
        || group.digest.state != MemoryCandidateGroupState::NeedsReview
    {
        return Ok(());
    }

    let input_hash = group_input_hash(&group);
    let recommendations = load_curator_recommendations(pool).await?;
    let Some(cached) = recommendations.get(&group.digest.group_id) else {
        return Ok(());
    };
    if cached.prompt_version != MEMORY_AUTOPILOT_SCHEMA_VERSION || cached.input_hash != input_hash {
        return Ok(());
    }

    let Some(original) = group.candidates.first().cloned() else {
        return Ok(());
    };
    apply_cached_curator_recommendation(&mut group, cached);
    if !matches!(
        group.digest.state,
        MemoryCandidateGroupState::AutoEnable | MemoryCandidateGroupState::Recommended
    ) {
        return Ok(());
    }
    let Some(refined) = group.candidates.first() else {
        return Ok(());
    };
    if draft_refinement_changed(&original, refined) {
        update_pending_draft_with_refined_rule(pool, draft_id, refined).await?;
    }
    Ok(())
}

async fn planned_single_draft_group(
    pool: &SqlitePool,
    draft_id: &str,
) -> Result<Option<PlannedGroup>> {
    let draft = list_candidates(pool, None, None)
        .await?
        .into_iter()
        .find(|draft| draft.id == draft_id);
    let Some(draft) = draft else {
        return Ok(None);
    };
    let disabled_rule_ids = load_autopilot_disabled_rule_ids(pool).await?;
    let candidate = pending_from_draft(draft, &disabled_rule_ids);
    let active_rules = load_active_rules(pool, MAX_PENDING_SCAN).await?;
    let active_keys = active_rules
        .iter()
        .map(active_memory_key)
        .collect::<HashSet<_>>();
    let active_content_hashes = active_rules
        .iter()
        .filter_map(|rule| rule.content_hash.as_deref())
        .map(str::to_owned)
        .collect::<HashSet<_>>();
    let group_id = candidate_group_key(&candidate);
    let digest = digest_group(
        group_id,
        std::slice::from_ref(&candidate),
        &active_keys,
        &active_content_hashes,
        &active_rules,
    );
    Ok(Some(PlannedGroup {
        digest,
        candidates: vec![candidate],
        conflict: None,
    }))
}

fn draft_refinement_changed(original: &PendingMemory, refined: &PendingMemory) -> bool {
    original.title != refined.title
        || original.body != refined.body
        || normalize_patterns(original.file_patterns.clone())
            != normalize_patterns(refined.file_patterns.clone())
}

pub(super) async fn refine_pr_review_groups_with_local_ai(
    pool: &SqlitePool,
    groups: &mut [PlannedGroup],
    max_candidates: Option<usize>,
) -> Result<()> {
    if cfg!(test) {
        return Ok(());
    }

    let mut options = MemoryCuratorOptions::default();
    if let Some(limit) = max_candidates {
        options.max_candidates = limit;
    }
    let candidates = groups
        .iter()
        .filter(|group| pr_review_group_needs_ai_refinement(group))
        .take(options.max_candidates)
        .filter_map(pr_review_curator_candidate)
        .collect::<Vec<_>>();
    if !candidates.is_empty() {
        // The exact set of groups actually submitted to the curator this run
        // (after the `take(max_candidates)` bound). Only these may be rewritten
        // to the "no decision" message when the curator omits them; groups
        // truncated by the bound keep their deterministic NeedsReview reason.
        let submitted_group_ids: HashSet<String> = candidates
            .iter()
            .map(|candidate| candidate.group_id.clone())
            .collect();
        let outcome = curate_memory_candidates_with_local_ai(&candidates, options).await?;
        if let Some(detail) = outcome.unavailable_reason {
            mark_ai_refinement_unavailable(groups, &detail);
        } else {
            let decisions_by_group = curator_decisions_by_group(outcome.decisions);
            for group in groups
                .iter_mut()
                .filter(|group| pr_review_group_needs_ai_refinement(group))
                .filter(|group| submitted_group_ids.contains(&group.digest.group_id))
            {
                let Some(decision) = decisions_by_group.get(&group.digest.group_id) else {
                    "local memory curator did not return a decision for this PR review"
                        .clone_into(&mut group.digest.reason);
                    continue;
                };
                let input_hash = group_input_hash(group);
                apply_curator_decision(group, decision, options);
                upsert_curator_recommendation(pool, group, &input_hash).await?;
            }
        }
    }

    // SECOND, non-authoritative layer: ask the local AI to confirm/dismiss the
    // deterministic conflicts surfaced on this batch. This only annotates the
    // persisted `memory_conflicts` rows; it never relaxes the deterministic
    // NeedsReview classification above. LLM-unavailable / parse-failure leaves
    // every conflict at `detected` (deterministic behavior preserved).
    judge_detected_conflicts_with_local_ai(pool, groups).await?;

    Ok(())
}

pub(super) fn pr_review_group_needs_ai_refinement(group: &PlannedGroup) -> bool {
    group.digest.state == MemoryCandidateGroupState::NeedsReview
        && group.digest.reason
            == "imported PR review needs human rule cleanup before autopilot can enable it"
        && group.candidates.len() == 1
        && group
            .candidates
            .first()
            .is_some_and(|candidate| candidate.origin == "pr_review")
}

pub(super) fn pr_review_curator_candidate(group: &PlannedGroup) -> Option<MemoryCuratorCandidate> {
    let candidate = group.candidates.first()?;
    Some(MemoryCuratorCandidate {
        group_id: group.digest.group_id.clone(),
        current_title: candidate.title.clone(),
        current_rule: candidate.body.clone(),
        source: MemoryCuratorSource::PrReview,
        source_repo: candidate.source_repo.clone(),
        file_patterns: candidate.file_patterns.clone(),
        source_evidence: candidate
            .raw_description
            .as_deref()
            .map(compact_source_evidence)
            .unwrap_or_default(),
    })
}

pub(super) fn compact_source_evidence(raw_description: &str) -> String {
    let lines = raw_description
        .lines()
        .filter_map(|line| {
            let trimmed = line.trim();
            (trimmed.starts_with("Source:")
                || trimmed.starts_with("Comment:")
                || trimmed.starts_with("File:")
                || trimmed.starts_with("Reviewer said:")
                || (!trimmed.is_empty() && trimmed.len() < 240))
                .then_some(trimmed)
        })
        .take(12)
        .collect::<Vec<_>>()
        .join("\n");
    truncate_chars(&lines, 1_200)
}

pub(super) fn apply_curator_decision(
    group: &mut PlannedGroup,
    decision: &MemoryCuratorDecision,
    options: MemoryCuratorOptions,
) {
    let reason = decision
        .reason
        .as_deref()
        .unwrap_or("local memory curator review");
    group.digest.confidence = Some(format_confidence(decision.confidence));
    if decision.action != MemoryCuratorAction::Enable {
        group.digest.reason =
            format!("local memory curator left this PR review for human cleanup: {reason}");
        return;
    }
    let title = decision.title.as_deref().map_or("", str::trim);
    let rule = decision.rule.as_deref().map_or("", str::trim);
    if !curator_rule_is_safe(title, rule) {
        group.digest.reason = format!(
            "local memory curator proposed rule text did not pass the safety gate: {reason}"
        );
        return;
    }

    if let Some(candidate) = group.candidates.first_mut() {
        title.clone_into(&mut candidate.title);
        rule.clone_into(&mut candidate.body);
        if let Some(scope) = decision.scope
            && let Some(patterns) = file_patterns_for_curator_scope(scope, &candidate.file_patterns)
        {
            candidate.file_patterns = normalize_patterns(patterns);
        }
    }
    title.clone_into(&mut group.digest.title);
    group.digest.sample = truncate_chars(rule, 320);
    group.digest.file_patterns = merged_patterns(&group.candidates);
    if decision.confidence >= options.min_confidence {
        group.digest.state = MemoryCandidateGroupState::AutoEnable;
        group.digest.reason = format!(
            "local memory curator refined this PR review into a high-confidence rule: {reason}"
        );
        group.digest.confidence = Some(AUTOPILOT_CONFIDENCE.to_owned());
    } else if decision.confidence >= DEFAULT_RECOMMENDED_MIN_CONFIDENCE {
        group.digest.state = MemoryCandidateGroupState::Recommended;
        group.digest.reason =
            format!("local memory curator recommends this rule after review: {reason}");
        group.digest.confidence = Some(format_confidence(decision.confidence));
    } else {
        group.digest.reason = format!(
            "local memory curator confidence {:.2} is below recommendation threshold {:.2}: {reason}",
            decision.confidence, DEFAULT_RECOMMENDED_MIN_CONFIDENCE
        );
    }
}

pub(super) fn mark_ai_refinement_unavailable(groups: &mut [PlannedGroup], detail: &str) {
    let detail = truncate_chars(detail, 240);
    for group in groups
        .iter_mut()
        .filter(|group| pr_review_group_needs_ai_refinement(group))
    {
        group.digest.reason =
            format!("local memory curator unavailable; review manually: {detail}");
    }
}

pub(super) async fn enable_group(
    pool: &SqlitePool,
    candidates: &[PendingMemory],
) -> Result<SkillRecord> {
    let Some(primary) = primary_candidate(candidates) else {
        return Err(CoreError::Validation(
            "cannot enable an empty memory candidate group".to_owned(),
        ));
    };

    let rule = match &primary.kind {
        PendingMemoryKind::Draft { id } => {
            update_pending_draft_with_refined_rule(pool, id, primary).await?;
            promote_candidate(pool, id).await?
        }
        PendingMemoryKind::Session { content_hash } => {
            approve_session_mined_candidate(pool, content_hash)
                .await?
                .rule
        }
    };

    for candidate in candidates {
        if let PendingMemoryKind::Session { content_hash } = &candidate.kind {
            let _ = mark_session_mined_candidate_approved_for_rule(pool, content_hash, &rule.id)
                .await?;
        }
    }

    Ok(rule)
}

pub(super) async fn update_pending_draft_with_refined_rule(
    pool: &SqlitePool,
    draft_id: &str,
    candidate: &PendingMemory,
) -> Result<()> {
    let Some(raw_description) = candidate.raw_description.as_deref() else {
        return Ok(());
    };
    let description = rewrite_draft_description(raw_description, &candidate.body);
    let file_patterns_json = if candidate.file_patterns.is_empty() {
        None
    } else {
        Some(serde_json::to_string(&candidate.file_patterns)?)
    };
    sqlx::query(
        "UPDATE skills SET name = ?1, description = ?2, file_patterns = ?3, \
         updated_at = datetime('now') WHERE id = ?4 AND status = 'pending'",
    )
    .bind(&candidate.title)
    .bind(description)
    .bind(file_patterns_json.as_deref())
    .bind(draft_id)
    .execute(pool)
    .await?;
    Ok(())
}

pub(super) fn rewrite_draft_description(raw_description: &str, refined_rule: &str) -> String {
    if let Some((_, evidence)) = raw_description.split_once("Source evidence:") {
        format!(
            "Rule:\n{}\n\nSource evidence:\n{}",
            refined_rule.trim(),
            evidence.trim()
        )
    } else {
        format!("Rule:\n{}", refined_rule.trim())
    }
}