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
//! Local MCP rule-serve ledger.
//!
//! This records the small, low-sensitive facts needed to prove the open
//! source runtime is doing useful work: which MCP rule tool served rules,
//! whether it came up empty, the repo/file scope, and a hash of the query.
//! It deliberately does not store the prompt, source code, or rule bodies.

use sha2::{Digest, Sha256};
use sqlx::SqlitePool;

#[derive(Debug, Clone)]
pub struct McpRuleServeInput<'a> {
    pub tool: &'a str,
    pub session_id: Option<&'a str>,
    pub repo_full_name: Option<&'a str>,
    pub file_path: Option<&'a str>,
    pub query_text: &'a str,
    pub rule_ids: &'a [String],
    pub top_k: i64,
    pub strict_match_count: i64,
    pub estimated_tokens: i64,
}

#[derive(Debug, Clone, sqlx::FromRow)]
pub struct McpRuleServeSummary {
    pub calls: i64,
    pub empty_calls: i64,
    pub rules_served: i64,
    pub strict_matches: i64,
    pub estimated_tokens: i64,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct McpRuleServeRuleSummary {
    pub calls: i64,
    pub strict_match_calls: i64,
    pub estimated_tokens: i64,
    pub latest: Option<McpRuleServeRuleEvidence>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct McpRuleServeRuleEvidence {
    pub tool: String,
    pub repo_full_name: Option<String>,
    pub file_path: Option<String>,
    pub served_at: String,
    pub strict_scoped: bool,
    pub estimated_tokens: i64,
}

#[derive(Debug, Clone, sqlx::FromRow)]
struct McpRuleServeRuleRow {
    tool: String,
    repo_full_name: Option<String>,
    file_path: Option<String>,
    rule_ids_json: String,
    strict_match_count: i64,
    estimated_tokens: i64,
    served_at: String,
}

pub fn query_hash(query_text: &str) -> String {
    use std::fmt::Write as _;
    let mut hasher = Sha256::new();
    hasher.update(query_text.as_bytes());
    hasher
        .finalize()
        .iter()
        .fold(String::with_capacity(64), |mut acc, byte| {
            let _ = write!(acc, "{byte:02x}");
            acc
        })
}

pub async fn record(pool: &SqlitePool, input: &McpRuleServeInput<'_>) -> crate::Result<()> {
    let rule_ids_json = serde_json::to_string(input.rule_ids).unwrap_or_else(|_| "[]".to_owned());
    let rule_count = input.rule_ids.len() as i64;
    let was_empty = i64::from(rule_count == 0);
    let hash = query_hash(input.query_text);

    let top_k = input.top_k.max(0);
    let strict_match_count = input.strict_match_count.max(0);
    let estimated_tokens = input.estimated_tokens.max(0);
    sqlx::query!(
        "INSERT INTO mcp_rule_serves
         (tool, session_id, repo_full_name, file_path, query_hash, rule_ids_json,
          rule_count, top_k, was_empty, strict_match_count, estimated_tokens)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
        input.tool,
        input.session_id,
        input.repo_full_name,
        input.file_path,
        hash,
        rule_ids_json,
        rule_count,
        top_k,
        was_empty,
        strict_match_count,
        estimated_tokens,
    )
    .execute(pool)
    .await?;

    Ok(())
}

pub async fn summary(pool: &SqlitePool, days: i64) -> crate::Result<McpRuleServeSummary> {
    let days = days.max(1);
    let window = format!("-{days} days");
    let row = sqlx::query_as!(
        McpRuleServeSummary,
        r#"SELECT
            COUNT(*) AS "calls!: i64",
            COALESCE(SUM(was_empty), 0) AS "empty_calls!: i64",
            COALESCE(SUM(rule_count), 0) AS "rules_served!: i64",
            COALESCE(SUM(strict_match_count), 0) AS "strict_matches!: i64",
            COALESCE(SUM(estimated_tokens), 0) AS "estimated_tokens!: i64"
         FROM mcp_rule_serves
         WHERE datetime(served_at) >= datetime('now', ?1)"#,
        window,
    )
    .fetch_one(pool)
    .await?;
    Ok(row)
}

pub async fn summary_for_repos(
    pool: &SqlitePool,
    repo_full_names: &[String],
    days: i64,
) -> crate::Result<McpRuleServeSummary> {
    let repos: Vec<String> = repo_full_names
        .iter()
        .map(|repo| repo.trim().to_ascii_lowercase())
        .filter(|repo| !repo.is_empty())
        .collect();
    if repos.is_empty() {
        return summary(pool, days).await;
    }

    let days = days.max(1);
    let window = format!("-{days} days");
    let repos_json = serde_json::to_string(&repos).unwrap_or_else(|_| "[]".to_owned());
    let row = sqlx::query_as::<_, McpRuleServeSummary>(
        r"SELECT
            COUNT(*) AS calls,
            COALESCE(SUM(was_empty), 0) AS empty_calls,
            COALESCE(SUM(rule_count), 0) AS rules_served,
            COALESCE(SUM(strict_match_count), 0) AS strict_matches,
            COALESCE(SUM(estimated_tokens), 0) AS estimated_tokens
         FROM mcp_rule_serves
         WHERE datetime(served_at) >= datetime('now', ?1)
           AND repo_full_name IS NOT NULL
           AND lower(repo_full_name) IN (SELECT value FROM json_each(?2))",
    )
    .bind(window)
    .bind(repos_json)
    .fetch_one(pool)
    .await?;
    Ok(row)
}

pub async fn summary_for_rule(
    pool: &SqlitePool,
    rule_id: &str,
    days: i64,
) -> crate::Result<McpRuleServeRuleSummary> {
    let days = days.max(1);
    let window = format!("-{days} days");
    let rows = sqlx::query_as!(
        McpRuleServeRuleRow,
        r#"SELECT tool AS "tool!: String", repo_full_name, file_path,
                rule_ids_json AS "rule_ids_json!: String",
                strict_match_count AS "strict_match_count!: i64",
                estimated_tokens AS "estimated_tokens!: i64",
                served_at AS "served_at!: String"
         FROM mcp_rule_serves
         WHERE was_empty = 0
           AND datetime(served_at) >= datetime('now', ?1)
         ORDER BY datetime(served_at) DESC, id DESC"#,
        window,
    )
    .fetch_all(pool)
    .await?;

    let mut summary = McpRuleServeRuleSummary::default();
    for row in rows {
        if !rule_ids_json_contains(&row.rule_ids_json, rule_id) {
            continue;
        }
        summary.calls += 1;
        summary.strict_match_calls += i64::from(row.strict_match_count > 0);
        summary.estimated_tokens += row.estimated_tokens.max(0);
        if summary.latest.is_none() {
            summary.latest = Some(McpRuleServeRuleEvidence {
                tool: row.tool,
                repo_full_name: row.repo_full_name,
                file_path: row.file_path,
                served_at: row.served_at,
                strict_scoped: row.strict_match_count > 0,
                estimated_tokens: row.estimated_tokens.max(0),
            });
        }
    }
    Ok(summary)
}

fn rule_ids_json_contains(raw: &str, rule_id: &str) -> bool {
    serde_json::from_str::<Vec<String>>(raw).is_ok_and(|ids| ids.iter().any(|id| id == rule_id))
}

#[cfg(test)]
mod tests {
    use super::*;
    use sqlx::sqlite::SqlitePoolOptions;

    async fn setup() -> SqlitePool {
        let pool = SqlitePoolOptions::new()
            .max_connections(1)
            .connect("sqlite::memory:")
            .await
            .expect("open pool");
        sqlx::migrate!("./migrations")
            .run(&pool)
            .await
            .expect("apply migrations");
        pool
    }

    #[test]
    fn query_hash_is_stable_and_does_not_expose_query_text() {
        let hash = query_hash("src/auth.ts validate bearer token");
        assert_eq!(hash.len(), 64);
        assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));
        assert_eq!(hash, query_hash("src/auth.ts validate bearer token"));
        assert!(!hash.contains("auth"));
    }

    #[tokio::test]
    async fn record_and_summary_capture_served_and_empty_calls() {
        let pool = setup().await;
        record(
            &pool,
            &McpRuleServeInput {
                tool: "search_rules",
                session_id: Some("session-1"),
                repo_full_name: Some("acme/app"),
                file_path: Some("src/auth.ts"),
                query_text: "src/auth.ts validate bearer token",
                rule_ids: &["rule-1".to_owned(), "rule-2".to_owned()],
                top_k: 5,
                strict_match_count: 2,
                estimated_tokens: 123,
            },
        )
        .await
        .expect("record served");
        record(
            &pool,
            &McpRuleServeInput {
                tool: "search_rules",
                session_id: Some("session-2"),
                repo_full_name: Some("acme/app"),
                file_path: Some("src/other.ts"),
                query_text: "src/other.ts unknown pattern",
                rule_ids: &[],
                top_k: 10,
                strict_match_count: 0,
                estimated_tokens: 25,
            },
        )
        .await
        .expect("record empty");

        let summary = summary(&pool, 30).await.expect("summary");
        assert_eq!(summary.calls, 2);
        assert_eq!(summary.empty_calls, 1);
        assert_eq!(summary.rules_served, 2);
        assert_eq!(summary.strict_matches, 2);
        assert_eq!(summary.estimated_tokens, 148);
    }

    #[tokio::test]
    async fn summary_for_repos_counts_only_matching_repo_aliases() {
        let pool = setup().await;
        record(
            &pool,
            &McpRuleServeInput {
                tool: "search_rules",
                session_id: Some("session-1"),
                repo_full_name: Some("Acme/App"),
                file_path: Some("src/auth.ts"),
                query_text: "auth middleware",
                rule_ids: &["rule-1".to_owned(), "rule-2".to_owned()],
                top_k: 5,
                strict_match_count: 2,
                estimated_tokens: 120,
            },
        )
        .await
        .expect("record acme serve");
        record(
            &pool,
            &McpRuleServeInput {
                tool: "search_rules",
                session_id: Some("session-2"),
                repo_full_name: Some("other/repo"),
                file_path: Some("src/auth.ts"),
                query_text: "auth middleware",
                rule_ids: &["rule-3".to_owned()],
                top_k: 5,
                strict_match_count: 1,
                estimated_tokens: 90,
            },
        )
        .await
        .expect("record other serve");

        let scoped = summary_for_repos(&pool, &["acme/app".to_owned()], 30)
            .await
            .expect("scoped summary");

        assert_eq!(scoped.calls, 1);
        assert_eq!(scoped.rules_served, 2);
        assert_eq!(scoped.strict_matches, 2);
        assert_eq!(scoped.estimated_tokens, 120);
    }

    #[tokio::test]
    async fn summary_for_rule_counts_only_exact_json_rule_ids() {
        let pool = setup().await;
        record(
            &pool,
            &McpRuleServeInput {
                tool: "search_rules",
                session_id: Some("session-1"),
                repo_full_name: Some("acme/app"),
                file_path: Some("src/auth.ts"),
                query_text: "auth middleware",
                rule_ids: &["rule-1".to_owned(), "rule-10".to_owned()],
                top_k: 5,
                strict_match_count: 2,
                estimated_tokens: 120,
            },
        )
        .await
        .expect("record multi-rule serve");
        record(
            &pool,
            &McpRuleServeInput {
                tool: "search_rules",
                session_id: Some("session-2"),
                repo_full_name: Some("acme/app"),
                file_path: Some("src/auth.ts"),
                query_text: "auth middleware exact",
                rule_ids: &["rule-10".to_owned()],
                top_k: 5,
                strict_match_count: 0,
                estimated_tokens: 80,
            },
        )
        .await
        .expect("record single-rule serve");

        let rule_1 = summary_for_rule(&pool, "rule-1", 30)
            .await
            .expect("summary rule-1");
        assert_eq!(
            rule_1,
            McpRuleServeRuleSummary {
                calls: 1,
                strict_match_calls: 1,
                estimated_tokens: 120,
                latest: Some(McpRuleServeRuleEvidence {
                    tool: "search_rules".to_owned(),
                    repo_full_name: Some("acme/app".to_owned()),
                    file_path: Some("src/auth.ts".to_owned()),
                    served_at: rule_1.latest.as_ref().unwrap().served_at.clone(),
                    strict_scoped: true,
                    estimated_tokens: 120,
                }),
            }
        );

        let rule_10 = summary_for_rule(&pool, "rule-10", 30)
            .await
            .expect("summary rule-10");
        assert_eq!(
            rule_10,
            McpRuleServeRuleSummary {
                calls: 2,
                strict_match_calls: 1,
                estimated_tokens: 200,
                latest: Some(McpRuleServeRuleEvidence {
                    tool: "search_rules".to_owned(),
                    repo_full_name: Some("acme/app".to_owned()),
                    file_path: Some("src/auth.ts".to_owned()),
                    served_at: rule_10.latest.as_ref().unwrap().served_at.clone(),
                    strict_scoped: false,
                    estimated_tokens: 80,
                }),
            }
        );
    }
}