difflore-cli 0.6.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
//! Doctor data-probing for the default table view.
//!
//! Collects every `difflore_core` / `installer` / `CommandContext` call the
//! default `difflore doctor` table needs and decodes it into the plain
//! [`Findings`] struct for `table.rs` to render. Returns only decoded scalars,
//! options and small enums — severity / status / hint strings live in
//! `table.rs`.

use std::path::Path;

use super::memory_snapshot::{self, MemorySnapshot};
use crate::installer;
use crate::support::util::count_rules_for_repo;

/// Everything the readiness table needs, already fetched and decoded.
/// Construct one with [`gather`].
pub(crate) struct Findings {
    pub(crate) binary_version: String,
    pub(crate) master_key_storage: difflore_core::infra::crypto::MasterKeyStorageStatus,
    pub(crate) project_db: ProjectDbProbe,
    /// Pre-loaded "what the AI has learned" snapshot. Defaults (rendering to
    /// "") when the current repo has no ready memory, so no load is issued.
    pub(crate) memory_snapshot: MemorySnapshot,
    pub(crate) mcp: installer::McpStatusSnapshot,
    pub(crate) provider: ProviderProbe,
    pub(crate) cloud: CloudProbe,
    pub(crate) embedder: EmbedderProbe,
    pub(crate) git_hooks: GitHookState,
    pub(crate) daemon: DaemonProbe,
    pub(crate) recall_trace: difflore_core::observability::injection_log::InjectionPathSummary,
    pub(crate) gate_capture: GateCaptureProbe,
}

/// Decoded project-DB counts. `db_available == false` means the pool
/// failed to open and every numeric field is meaningless.
pub(crate) struct ProjectDbProbe {
    pub(crate) db_available: bool,
    pub(crate) total_rules: i64,
    pub(crate) prs_imported: i64,
    pub(crate) repo_full_name: Option<String>,
    pub(crate) review_source_repo_full_name: Option<String>,
    pub(crate) scoped_active_rules: i64,
    pub(crate) review_source_active_rules: i64,
}

/// Decoded provider-list outcome.
pub(crate) enum ProviderProbe {
    DbUnavailable,
    /// Provider config was unreadable; carries the error text.
    Error(String),
    NoneConfigured,
    /// Providers exist; the resolved active provider's name.
    Active(String),
    /// Providers exist but none is active and none could be defaulted.
    NoneActive,
}

/// Decoded cloud-login outcome.
pub(crate) enum CloudProbe {
    NotLoggedIn,
    LoggedIn {
        plan: String,
        team_name: Option<String>,
        impact: Box<CloudImpactProbe>,
    },
}

#[derive(Default)]
pub(crate) struct CloudImpactProbe {
    pub(crate) coverage: Option<difflore_core::contract::dto::ImpactCoverageDto>,
    pub(crate) fix_scorecard: Option<difflore_core::contract::dto::ImpactFixScorecardDto>,
    pub(crate) coverage_error: Option<String>,
    pub(crate) fix_scorecard_error: Option<String>,
}

/// Raw embedder inputs; the `table.rs` shapers decode the activity tail and
/// pick the final row.
pub(crate) struct EmbedderProbe {
    pub(crate) kind: difflore_core::context::embedding::ActiveEmbedderKind,
    pub(crate) activity_tail: Vec<difflore_core::observability::activity_stream::ActivityEvent>,
    /// `None` when the per-project index DB could not be opened.
    pub(crate) diagnostics: Option<difflore_core::context::EmbeddingDiagnostics>,
}

/// Decoded daemon state, after the stale-pid cleanup attempt.
pub(crate) struct DaemonProbe {
    pub(crate) state: DaemonProbeState,
    pub(crate) heartbeat_age_ms: Option<i64>,
    pub(crate) last_drain_age_ms: Option<i64>,
    pub(crate) last_attempted: Option<usize>,
    pub(crate) last_confirmed: Option<usize>,
    pub(crate) cloud_pending: Option<i64>,
    pub(crate) observation_pending: Option<i64>,
    pub(crate) hook_spill_count: Option<usize>,
}

pub(crate) enum DaemonProbeState {
    Running,
    /// Stale pid whose cleanup attempt failed (locked file etc.).
    StaleCleanupFailed,
    NotRunning,
}

pub(crate) enum GitHookState {
    NotARepo,
    None,
    Installed,
    OtherHook,
    /// Hook file exists on disk but we couldn't read it (permissions,
    /// IO error). Distinct from `OtherHook` — we don't actually know
    /// what's in there, and saying "another tool installed it" would
    /// be misleading (the file might be `DiffLore`'s own hook in a
    /// state we just can't open).
    Unreadable(String),
}

pub(crate) struct GateCaptureProbe {
    pub(crate) status: crate::session_mine::trigger::GateCaptureStatus,
}

/// Probe every data source the default doctor table reads and return a
/// fully decoded [`Findings`]. The single entry point for `table.rs`.
pub(crate) async fn gather(ctx: &crate::runtime::CommandContext) -> Findings {
    let pool = Some(&ctx.db);
    // Probe side effects run in display order (e.g. daemon stale-pid cleanup
    // near the row that reports it).
    let project_db = probe_project_db(pool, &ctx.project).await;
    let binary_version = env!("CARGO_PKG_VERSION").to_owned();
    let master_key_storage = difflore_core::infra::crypto::probe_master_key_storage();
    let mcp = installer::collect_status_snapshot_with_runtime_probe();
    let provider = probe_provider(pool).await;
    let cloud = probe_cloud(ctx).await;
    let embedder = probe_embedder().await;
    let git_hooks = probe_git_hook_state();
    let daemon = probe_daemon(&ctx.db).await;
    let recall_trace = difflore_core::observability::injection_log::summary_24h();
    let gate_capture = probe_gate_capture(&ctx.project);
    // Load the "what the AI has learned" snapshot only when the repo has
    // ready memory; otherwise hand the renderer a default (renders to "").
    let memory_snapshot = if project_db.repo_memory_ready {
        memory_snapshot::load_for_repo(&ctx.db, &project_db.repo_aliases).await
    } else {
        MemorySnapshot::default()
    };
    Findings {
        binary_version,
        master_key_storage,
        project_db: project_db.probe,
        memory_snapshot,
        mcp,
        provider,
        cloud,
        embedder,
        git_hooks,
        daemon,
        recall_trace,
        gate_capture,
    }
}

fn probe_gate_capture(project: &Path) -> GateCaptureProbe {
    let project = project.to_string_lossy();
    let status =
        crate::session_mine::trigger::gate_capture_status_for_project(Some(project.as_ref()))
            .unwrap_or(crate::session_mine::trigger::GateCaptureStatus::Ready);
    GateCaptureProbe { status }
}

/// Carrier so the snapshot load can be gated on repo readiness without
/// leaking `repo_memory_ready` / `repo_aliases` into the renderer.
struct ProjectDbResult {
    probe: ProjectDbProbe,
    repo_memory_ready: bool,
    repo_aliases: Vec<String>,
}

async fn probe_project_db(
    pool: Option<&difflore_core::SqlitePool>,
    project: &Path,
) -> ProjectDbResult {
    let Some(pool) = pool else {
        return ProjectDbResult {
            repo_memory_ready: false,
            repo_aliases: Vec::new(),
            probe: ProjectDbProbe {
                db_available: false,
                total_rules: 0,
                prs_imported: 0,
                repo_full_name: None,
                review_source_repo_full_name: None,
                scoped_active_rules: 0,
                review_source_active_rules: 0,
            },
        };
    };
    let total_rules = match difflore_core::skills::stats(pool).await {
        Ok(s) => s.total,
        Err(_) => 0,
    };
    let counts = difflore_core::infra::db::table_counts(pool, &["review_items"]).await;
    let prs_imported = counts
        .into_iter()
        .find(|(table, _)| table == "review_items")
        .and_then(|(_, result)| result.ok())
        .unwrap_or(0);
    if total_rules == 0 {
        // Empty corpus blocks recall: there's nothing to retrieve.
        return ProjectDbResult {
            repo_memory_ready: false,
            repo_aliases: Vec::new(),
            probe: ProjectDbProbe {
                db_available: true,
                total_rules: 0,
                prs_imported,
                repo_full_name: None,
                review_source_repo_full_name: None,
                scoped_active_rules: 0,
                review_source_active_rules: 0,
            },
        };
    }

    let configured_gitlab_hosts = difflore_core::ingest::gitlab::auth::configured_hosts().await;
    let detected_repo_remotes = difflore_core::infra::git::detect_repo_full_names_with_gitlab_hosts(
        &project.to_string_lossy(),
        &configured_gitlab_hosts,
    );
    let repo_remotes =
        difflore_core::skills::expand_repo_scopes_with_source_aliases(pool, &detected_repo_remotes)
            .await
            .unwrap_or(detected_repo_remotes);
    let repo_full_name = repo_remotes.first().cloned();
    let review_source_repo_full_name = repo_remotes.get(1).cloned();
    let active_rules = difflore_core::skills::list(pool).await.unwrap_or_default();
    let source_repos = difflore_core::skills::list_source_repos(pool)
        .await
        .unwrap_or_default();
    let scoped_active_rules =
        count_rules_for_repo(&active_rules, &source_repos, repo_full_name.as_deref());
    let review_source_active_rules = count_rules_for_repo(
        &active_rules,
        &source_repos,
        review_source_repo_full_name.as_deref(),
    );
    let repo_ready =
        repo_full_name.is_some() && (scoped_active_rules > 0 || review_source_active_rules > 0);

    ProjectDbResult {
        repo_memory_ready: repo_ready,
        repo_aliases: repo_remotes,
        probe: ProjectDbProbe {
            db_available: true,
            total_rules,
            prs_imported,
            repo_full_name,
            review_source_repo_full_name,
            scoped_active_rules,
            review_source_active_rules,
        },
    }
}

async fn probe_provider(pool: Option<&difflore_core::SqlitePool>) -> ProviderProbe {
    let Some(pool) = pool else {
        return ProviderProbe::DbUnavailable;
    };
    match difflore_core::infra::providers::list(pool).await {
        Ok(providers) if providers.is_empty() => ProviderProbe::NoneConfigured,
        Ok(providers) => {
            let active = providers
                .iter()
                .find(|p| p.is_active)
                .or_else(|| providers.first());
            match active {
                Some(p) => ProviderProbe::Active(p.name.clone()),
                None => ProviderProbe::NoneActive,
            }
        }
        Err(e) => ProviderProbe::Error(e.to_string()),
    }
}

async fn probe_cloud(ctx: &crate::runtime::CommandContext) -> CloudProbe {
    let cloud_client = ctx.cloud().await;
    if cloud_client.is_logged_in() {
        let (status, coverage, fix_scorecard) = tokio::join!(
            difflore_core::cloud::sync::fetch_cloud_status(cloud_client),
            cloud_client.get_impact_coverage(),
            cloud_client.get_impact_fix_scorecard(),
        );
        let impact = CloudImpactProbe {
            coverage: coverage.as_ref().ok().cloned(),
            fix_scorecard: fix_scorecard.as_ref().ok().cloned(),
            coverage_error: coverage.err().map(|e| e.to_string()),
            fix_scorecard_error: fix_scorecard.err().map(|e| e.to_string()),
        };
        CloudProbe::LoggedIn {
            plan: status.plan.as_deref().unwrap_or("free").to_owned(),
            team_name: status.team_name,
            impact: Box::new(impact),
        }
    } else {
        CloudProbe::NotLoggedIn
    }
}

/// Embedder readiness probe. Delegates to `probe_active_embedder` so doctor
/// agrees with the runtime resolver, and consults the per-project embedding
/// profile diagnostic so the table doesn't show green over a dead vector lane.
/// Runs no live embed call, so it stays cheap and imports no network failures
/// (`doctor --report` owns the measured self-recall number).
async fn probe_embedder() -> EmbedderProbe {
    let kind = difflore_core::context::embedding::probe_active_embedder().await;
    let activity_tail = difflore_core::observability::activity_stream::tail(200);
    let diagnostics = match difflore_core::context::index_db::get_pool_for_cwd().await {
        Ok(index_pool) => Some(
            difflore_core::context::gather_embedding_diagnostics_with_activity(&index_pool).await,
        ),
        Err(_) => None,
    };
    EmbedderProbe {
        kind,
        activity_tail,
        diagnostics,
    }
}

async fn probe_daemon(pool: &difflore_core::SqlitePool) -> DaemonProbe {
    // Stale pid is purely a leftover lock file from a dead process —
    // there's nothing for the user to debug, so we clean it on read
    // and report "off" with an informational status instead of a
    // permanent Warn that just trains users to ignore Optional hints.
    let mut daemon_status = difflore_core::infra::daemon::status();
    if let difflore_core::infra::daemon::DaemonStatus::Stale { .. } = daemon_status
        && let Ok(pid_path) = difflore_core::infra::daemon::pid_path()
        && std::fs::remove_file(&pid_path).is_ok()
    {
        daemon_status = difflore_core::infra::daemon::DaemonStatus::NotRunning;
    }
    let state = match daemon_status {
        difflore_core::infra::daemon::DaemonStatus::Running { .. } => DaemonProbeState::Running,
        // Only reached when the cleanup attempt failed (locked file etc.).
        difflore_core::infra::daemon::DaemonStatus::Stale { .. } => {
            DaemonProbeState::StaleCleanupFailed
        }
        difflore_core::infra::daemon::DaemonStatus::NotRunning => DaemonProbeState::NotRunning,
    };

    let now_ms = chrono::Utc::now().timestamp_millis();
    let daemon_state = difflore_core::infra::daemon::read_state().ok().flatten();
    let queue = difflore_core::cloud::outbox::OutboxQueue::new(pool.clone());
    let cloud_pending = queue
        .counts()
        .await
        .ok()
        .map(|counts| counts.pending + counts.processing);
    let observation_pending =
        match difflore_core::cloud::observations::ObservationEmitter::open_default().await {
            Ok(emitter) => emitter.pending_upload_count().await.ok(),
            Err(_) => None,
        };
    let hook_spill_count = difflore_core::cloud::outbox::hook_spill_stats()
        .ok()
        .map(|stats| stats.count);

    DaemonProbe {
        state,
        heartbeat_age_ms: daemon_state
            .as_ref()
            .map(|state| now_ms.saturating_sub(state.heartbeat_at_ms)),
        last_drain_age_ms: daemon_state
            .as_ref()
            .and_then(|state| state.last_drain_at_ms)
            .map(|ts| now_ms.saturating_sub(ts)),
        last_attempted: daemon_state.as_ref().map(|state| state.last_attempted),
        last_confirmed: daemon_state.as_ref().map(|state| state.last_confirmed),
        cloud_pending,
        observation_pending,
        hook_spill_count,
    }
}

fn probe_git_hook_state() -> GitHookState {
    let cwd = difflore_core::infra::paths::current_project_root();
    // Resolve the git dir via `git rev-parse --git-dir` so worktrees (where
    // `.git` is a file, not a dir) find the right hooks/ location. A naive
    // `cwd.join(".git/hooks")` would dead-end and report "no hook".
    let output = difflore_core::infra::git::git_command(&cwd)
        .args(["rev-parse", "--git-dir"])
        .output();
    let git_dir = match output {
        Ok(o) if o.status.success() => {
            let raw = String::from_utf8_lossy(&o.stdout).trim().to_owned();
            if raw.is_empty() {
                return GitHookState::NotARepo;
            }
            let p = std::path::PathBuf::from(&raw);
            if p.is_absolute() { p } else { cwd.join(p) }
        }
        _ => return GitHookState::NotARepo,
    };
    let hook_path = git_dir.join("hooks").join("pre-commit");
    if !hook_path.exists() {
        return GitHookState::None;
    }
    let body = match std::fs::read_to_string(&hook_path) {
        Ok(b) => b,
        Err(e) => return GitHookState::Unreadable(e.to_string()),
    };
    if body.contains("difflore") {
        GitHookState::Installed
    } else {
        GitHookState::OtherHook
    }
}