dirge-agent 0.18.11

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
//! Recurrence-weighted salience graduation (dirge-4nix).
//!
//! When the SAME learning has been stored as near-duplicate memory entries
//! (a recurring lesson), boost the representative entry's salience so it's
//! more likely to be retained and surfaced — WITHOUT merging or deleting
//! anything. The existing LLM consolidation pass still owns dedup; this is
//! a salience nudge, not consolidation.
//!
//! All functions here are pure and heavily unit-tested. No DB access.

use std::collections::BTreeSet;

/// Conservative: near-identical, not merely related.
pub(crate) const MIN_JACCARD: f64 = 0.80;
/// Skip trivially-short entries that make Jaccard noisy.
pub(crate) const MIN_TOKENS: usize = 8;
/// A learning stored ≥2 times has recurred.
pub(crate) const MIN_CLUSTER_SIZE: usize = 2;

/// A cluster of near-duplicate entries that represent one recurring learning.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RecurrenceCluster {
    pub rep_uid: String,
    pub member_uids: Vec<String>,
    pub size: usize,
    /// Stable content-hash of the cluster (ledger key).
    pub hash: String,
}

/// Minimal input view for clustering — testable without a DB.
#[derive(Debug, Clone)]
pub(crate) struct GraduationInput {
    pub uid: String,
    pub target: String,
    pub content: String,
    pub use_count: i64,
    pub created_at: String,
}

/// Normalize content to a token SET: lowercase, split on any
/// non-alphanumeric, drop empties, collect into a BTreeSet.
fn tokens(content: &str) -> BTreeSet<String> {
    content
        .to_lowercase()
        .split(|c: char| !c.is_alphanumeric())
        .filter(|s| !s.is_empty())
        .map(|s| s.to_string())
        .collect()
}

/// Jaccard similarity of two token sets: |A∩B| / |A∪B|; 0.0 if both empty.
fn jaccard(a: &BTreeSet<String>, b: &BTreeSet<String>) -> f64 {
    if a.is_empty() && b.is_empty() {
        return 0.0;
    }
    let intersection = a.intersection(b).count();
    let union = a.union(b).count();
    intersection as f64 / union as f64
}

/// Group entries into near-duplicate clusters.
///
/// - Only entries with the SAME `target` may cluster together.
/// - Skip entries with fewer than `min_tokens` tokens.
/// - Greedy single pass for determinism: iterate entries in a STABLE order
///   (sort by uid first); an entry joins the first existing cluster whose
///   REPRESENTATIVE has jaccard >= `min_jaccard`, else it starts a new cluster.
/// - Representative = member with highest use_count, tie-broken by newest
///   created_at, then uid for total determinism.
/// - Return ONLY clusters with size >= `min_size`.
/// - `hash` = hex sha256 of the sorted-by-uid member `content`s joined by "\n".
pub(crate) fn recurrence_clusters(
    entries: &[GraduationInput],
    min_jaccard: f64,
    min_tokens: usize,
    min_size: usize,
) -> Vec<RecurrenceCluster> {
    // Stable sort: uid first for deterministic iteration order.
    let mut sorted: Vec<&GraduationInput> = entries.iter().collect();
    sorted.sort_by(|a, b| a.uid.cmp(&b.uid));

    // Precompute token sets, filtering short entries.
    let token_sets: Vec<(&GraduationInput, BTreeSet<String>)> =
        sorted.iter().map(|e| (*e, tokens(&e.content))).collect();

    // Group: list of (entries, representative_index)
    let mut clusters: Vec<Vec<usize>> = Vec::new();

    for (i, (_entry, tok)) in token_sets.iter().enumerate() {
        if tok.len() < min_tokens {
            continue;
        }
        let entry_target = &token_sets[i].0.target;
        // Find first cluster whose representative is jaccard-close and same target.
        let mut found = false;
        for cluster in clusters.iter_mut() {
            let rep_idx = cluster[0]; // representative is first member (we'll reorder later)
            let rep_target = &token_sets[rep_idx].0.target;
            if rep_target != entry_target {
                continue;
            }
            let rep_tokens = &token_sets[rep_idx].1;
            if jaccard(rep_tokens, tok) >= min_jaccard {
                cluster.push(i);
                found = true;
                break;
            }
        }
        if !found {
            clusters.push(vec![i]);
        }
    }

    // Build result: select representative for each cluster, compute hash.
    clusters
        .into_iter()
        .filter(|c| c.len() >= min_size)
        .map(|indices| {
            // Representative: highest use_count, tie-break newest created_at, then uid.
            let rep_idx = *indices
                .iter()
                .max_by(|&&a, &&b| {
                    let ea = token_sets[a].0;
                    let eb = token_sets[b].0;
                    ea.use_count
                        .cmp(&eb.use_count)
                        .then_with(|| ea.created_at.cmp(&eb.created_at))
                        .then_with(|| ea.uid.cmp(&eb.uid))
                })
                .unwrap();
            let rep = token_sets[rep_idx].0;

            let mut member_uids: Vec<String> = indices
                .iter()
                .map(|&i| token_sets[i].0.uid.clone())
                .collect();

            // Hash: sorted-by-uid member contents joined by "\n".
            let mut pairs: Vec<(&str, &str)> = indices
                .iter()
                .map(|&i| {
                    (
                        token_sets[i].0.uid.as_str(),
                        token_sets[i].0.content.as_str(),
                    )
                })
                .collect();
            pairs.sort_by(|a, b| a.0.cmp(b.0));
            let hash_input: String = pairs
                .iter()
                .map(|(_, content)| *content)
                .collect::<Vec<&str>>()
                .join("\n");
            let hash = {
                use sha2::{Digest, Sha256};
                let mut hasher = Sha256::new();
                hasher.update(hash_input.as_bytes());
                format!("{:x}", hasher.finalize())
            };

            member_uids.sort();

            RecurrenceCluster {
                rep_uid: rep.uid.clone(),
                member_uids,
                size: indices.len(),
                hash,
            }
        })
        .collect()
}

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

    // ── tokens ────────────────────────────────────────────

    #[test]
    fn tokens_lowercases_and_splits_on_non_alphanumeric() {
        let t = tokens("Hello, World! Foo-Bar");
        assert!(t.contains("hello"));
        assert!(t.contains("world"));
        assert!(t.contains("foo"));
        assert!(t.contains("bar"));
        assert!(!t.contains("Hello"));
    }

    #[test]
    fn tokens_deduplicates() {
        let t = tokens("a a b b c");
        assert_eq!(t.len(), 3);
        assert!(t.contains("a"));
        assert!(t.contains("b"));
        assert!(t.contains("c"));
    }

    #[test]
    fn tokens_empty_string_is_empty_set() {
        assert!(tokens("").is_empty());
    }

    // ── jaccard ───────────────────────────────────────────

    #[test]
    fn jaccard_identical_is_one() {
        let a = tokens("foo bar baz");
        let b = tokens("foo bar baz");
        assert!((jaccard(&a, &b) - 1.0).abs() < 1e-9);
    }

    #[test]
    fn jaccard_disjoint_is_zero() {
        let a = tokens("foo bar");
        let b = tokens("baz qux");
        assert!((jaccard(&a, &b) - 0.0).abs() < 1e-9);
    }

    #[test]
    fn jaccard_partial_overlap() {
        let a = tokens("foo bar baz");
        let b = tokens("foo bar qux");
        // intersection = {foo, bar} = 2, union = {foo, bar, baz, qux} = 4
        assert!((jaccard(&a, &b) - 0.5).abs() < 1e-9);
    }

    #[test]
    fn jaccard_both_empty_is_zero() {
        let a: BTreeSet<String> = BTreeSet::new();
        let b: BTreeSet<String> = BTreeSet::new();
        assert!((jaccard(&a, &b) - 0.0).abs() < 1e-9);
    }

    // ── clustering ────────────────────────────────────────

    fn make_entry(
        uid: &str,
        target: &str,
        content: &str,
        use_count: i64,
        created_at: &str,
    ) -> GraduationInput {
        GraduationInput {
            uid: uid.to_string(),
            target: target.to_string(),
            content: content.to_string(),
            use_count,
            created_at: created_at.to_string(),
        }
    }

    #[test]
    fn two_near_identical_entries_cluster() {
        let entries = vec![
            make_entry(
                "a",
                "memory",
                "dirge uses SQLite for long-term memory storage",
                1,
                "2025-01-01T00:00:00Z",
            ),
            make_entry(
                "b",
                "memory",
                "dirge uses SQLite for long term memory storage",
                2,
                "2025-01-02T00:00:00Z",
            ),
        ];
        let clusters = recurrence_clusters(&entries, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        assert_eq!(clusters.len(), 1);
        assert_eq!(clusters[0].size, 2);
        // b has higher use_count → representative
        assert_eq!(clusters[0].rep_uid, "b");
    }

    #[test]
    fn unrelated_entries_do_not_cluster() {
        let entries = vec![
            make_entry(
                "a",
                "memory",
                "dirge uses SQLite for long-term memory storage",
                1,
                "2025-01-01T00:00:00Z",
            ),
            make_entry(
                "b",
                "memory",
                "the sky is blue and the grass is green",
                1,
                "2025-01-02T00:00:00Z",
            ),
        ];
        let clusters = recurrence_clusters(&entries, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        assert!(clusters.is_empty());
    }

    #[test]
    fn different_target_never_clusters() {
        let entries = vec![
            make_entry(
                "a",
                "memory",
                "dirge uses SQLite for long-term memory storage",
                1,
                "2025-01-01T00:00:00Z",
            ),
            make_entry(
                "b",
                "pitfalls",
                "dirge uses SQLite for long-term memory storage",
                1,
                "2025-01-02T00:00:00Z",
            ),
        ];
        let clusters = recurrence_clusters(&entries, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        assert!(clusters.is_empty());
    }

    #[test]
    fn short_entry_below_min_tokens_never_clusters() {
        let entries = vec![
            make_entry("a", "memory", "short", 1, "2025-01-01T00:00:00Z"),
            make_entry("b", "memory", "short", 1, "2025-01-02T00:00:00Z"),
        ];
        // "short" has 1 token, below MIN_TOKENS=8
        let clusters = recurrence_clusters(&entries, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        assert!(clusters.is_empty());
    }

    #[test]
    fn representative_by_use_count_then_created_at() {
        let entries = vec![
            make_entry(
                "low",
                "memory",
                "this is a recurring lesson about testing methodology",
                1,
                "2025-01-01T00:00:00Z",
            ),
            make_entry(
                "high",
                "memory",
                "this is a recurring lesson about testing methodology indeed",
                5,
                "2025-01-01T00:00:00Z",
            ),
            make_entry(
                "mid",
                "memory",
                "this is a recurring lesson about testing methodology yes",
                3,
                "2025-01-01T00:00:00Z",
            ),
        ];
        let clusters = recurrence_clusters(&entries, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        assert_eq!(clusters.len(), 1);
        assert_eq!(clusters[0].rep_uid, "high");
        assert_eq!(clusters[0].size, 3);
    }

    #[test]
    fn representative_tie_break_by_newest_created_at() {
        let entries = vec![
            make_entry(
                "a",
                "memory",
                "this is a recurring lesson about testing methodology",
                2,
                "2025-01-01T00:00:00Z",
            ),
            make_entry(
                "b",
                "memory",
                "this is a recurring lesson about testing methodology indeed",
                2,
                "2025-01-03T00:00:00Z",
            ),
        ];
        let clusters = recurrence_clusters(&entries, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        assert_eq!(clusters.len(), 1);
        // same use_count, b has newer created_at
        assert_eq!(clusters[0].rep_uid, "b");
    }

    #[test]
    fn singletons_excluded() {
        let entries = vec![
            make_entry(
                "a",
                "memory",
                "this is a unique fact about project structure",
                1,
                "2025-01-01T00:00:00Z",
            ),
            make_entry(
                "b",
                "memory",
                "something completely different here",
                1,
                "2025-01-02T00:00:00Z",
            ),
        ];
        let clusters = recurrence_clusters(&entries, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        assert!(clusters.is_empty());
    }

    #[test]
    fn hash_stable_across_input_reorderings() {
        let content =
            "dirge uses SQLite for long term persistent storage of memory entries and facts";
        let a = make_entry("a", "memory", content, 1, "2025-01-01T00:00:00Z");
        let b = make_entry(
            "b",
            "memory",
            "dirge uses SQLite for long term persistent storage of memory entries",
            2,
            "2025-01-02T00:00:00Z",
        );
        let entries1 = vec![a.clone(), b.clone()];
        let entries2 = vec![b, a];
        let c1 = recurrence_clusters(&entries1, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        let c2 = recurrence_clusters(&entries2, MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE);
        assert_eq!(c1[0].hash, c2[0].hash);
    }

    #[test]
    fn hash_differs_when_membership_differs() {
        let base = "dirge uses SQLite for long term persistent storage of memory entries";
        let a = make_entry("a", "memory", base, 1, "2025-01-01T00:00:00Z");
        let b = make_entry(
            "b",
            "memory",
            "dirge uses SQLite for long term persistent storage of memory entries and facts",
            2,
            "2025-01-02T00:00:00Z",
        );
        let c = make_entry(
            "c",
            "memory",
            "dirge uses SQLite for long term persistent storage of memory entries plus more",
            3,
            "2025-01-03T00:00:00Z",
        );
        // All three cluster: both calls return 1 cluster, but hashes differ.
        let hash_abc = recurrence_clusters(
            &[a.clone(), b.clone(), c.clone()],
            MIN_JACCARD,
            MIN_TOKENS,
            MIN_CLUSTER_SIZE,
        )[0]
        .hash
        .clone();
        let hash_ab = recurrence_clusters(&[a, b], MIN_JACCARD, MIN_TOKENS, MIN_CLUSTER_SIZE)[0]
            .hash
            .clone();
        assert_ne!(hash_abc, hash_ab);
    }
}