mirror-log 0.1.9

Append-only event log for personal knowledge management with semantic chunking using SQLite.
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
//! Attention layer for mirror-log.
//!
//! This module implements the attention layer concept from attention.md:
//! - A dynamic, living index of relevance that sits between cold storage and active thought
//! - Manages the boundary between thought and storage
//! - Implements decay, promotion, and reference-based retention
//! - Supports the three-reference-point principle and five-sense triangulation

use crate::decay::{get_decay_score, track_access};
use rusqlite::{Connection, Result};
use serde::{Deserialize, Serialize};

use std::time::UNIX_EPOCH;

/// Maximum number of active attention items
const MAX_ACTIVE_ITEMS: i32 = 15;

/// Default decay threshold in days
const DEFAULT_DECAY_THRESHOLD_DAYS: i64 = 30;

/// Minimum reference points required for long-term storage
const MIN_REFERENCE_POINTS: i32 = 3;

/// The attention layer - a dynamic view over events that are currently relevant
#[derive(Debug, Clone)]
pub struct AttentionLayer {
    /// Maximum number of items to display
    max_items: i32,
    /// Decay threshold for removing items
    decay_threshold: i64,
    /// Minimum reference points required
    #[allow(dead_code)]
    min_reference_points: i32,
}

impl Default for AttentionLayer {
    fn default() -> Self {
        Self {
            max_items: MAX_ACTIVE_ITEMS,
            decay_threshold: DEFAULT_DECAY_THRESHOLD_DAYS,
            min_reference_points: MIN_REFERENCE_POINTS,
        }
    }
}

impl AttentionLayer {
    /// Creates a new attention layer with custom configuration
    pub fn new(max_items: i32, decay_threshold: i64, min_reference_points: i32) -> Self {
        Self {
            max_items,
            decay_threshold,
            min_reference_points,
        }
    }

    /// Gets the active attention items
    pub fn get_active_items(&self, conn: &Connection) -> Result<Vec<AttentionItem>> {
        let mut items = Vec::new();

        // Query events with recent access or high decay score
        let query = r#"
            SELECT
                e.id,
                e.timestamp,
                e.source,
                e.content,
                e.meta,
                d.access_count,
                d.last_accessed,
                d.pinned,
                COUNT(DISTINCT et.tag) as tag_count,
                COUNT(DISTINCT el.to_event_id) as link_count
            FROM events e
            LEFT JOIN decay d ON e.id = d.event_id
            LEFT JOIN shadow_state s ON e.id = s.event_id
            LEFT JOIN event_tags et ON e.id = et.event_id
            LEFT JOIN event_links el ON e.id = el.from_event_id
            WHERE
                s.event_id IS NULL
                AND
                (d.pinned = 1 OR d.last_accessed > unixepoch() - ?1 * 86400)
                AND NOT EXISTS (
                    SELECT 1 FROM decay
                    WHERE event_id = e.id
                    AND access_count < ?2
                    AND (unixepoch() - last_accessed) > ?3 * 86400
                    AND pinned = 0
                )
            GROUP BY e.id, e.timestamp, e.source, e.content, e.meta, d.access_count, d.last_accessed, d.pinned
            ORDER BY d.last_accessed DESC, d.access_count DESC
            LIMIT ?
        "#;

        let mut stmt = conn.prepare(query)?;

        let rows = stmt.query_map(
            (
                self.decay_threshold,
                5,
                self.decay_threshold,
                self.max_items,
            ),
            |row| {
                let id: String = row.get(0)?;
                let timestamp: i64 = row.get(1)?;
                let source: String = row.get(2)?;
                let content: String = row.get(3)?;
                let meta: Option<String> = row.get(4)?;
                let access_count: i64 = row.get(5)?;
                let last_accessed: i64 = row.get(6)?;
                let pinned: bool = row.get(7)?;
                let tag_count: i64 = row.get(8)?;
                let link_count: i64 = row.get(9)?;

                Ok(AttentionItem {
                    id,
                    timestamp,
                    source,
                    content,
                    meta,
                    access_count,
                    last_accessed,
                    pinned,
                    tag_count,
                    link_count,
                })
            },
        )?;

        for item in rows {
            items.push(item?);
        }

        // Trim to max_items
        if items.len() > self.max_items as usize {
            items.truncate(self.max_items as usize);
        }

        Ok(items)
    }

    /// Adds an item to attention (promotes from storage)
    pub fn add_to_attention(&self, conn: &Connection, event_id: &str) -> Result<()> {
        // Mark as accessed and pinned
        track_access(conn, event_id)?;

        // Update last_accessed timestamp
        conn.execute(
            "UPDATE decay SET last_accessed = unixepoch(), pinned = 1 WHERE event_id = ?1",
            [event_id],
        )?;

        Ok(())
    }

    /// Removes an item from attention (demotes to storage)
    pub fn remove_from_attention(&self, conn: &Connection, event_id: &str) -> Result<()> {
        conn.execute(
            "UPDATE decay SET pinned = 0 WHERE event_id = ?1",
            [event_id],
        )?;

        Ok(())
    }

    /// Updates an attention item (revisits it)
    pub fn update_item(&self, conn: &Connection, event_id: &str) -> Result<()> {
        track_access(conn, event_id)?;

        // Optionally increase priority based on how often it's updated
        conn.execute(
            "UPDATE decay SET access_count = access_count + 1 WHERE event_id = ?1",
            [event_id],
        )?;

        Ok(())
    }

    /// Gets items that need attention (flagged for decay)
    pub fn get_flagged_items(&self, conn: &Connection) -> Result<Vec<AttentionItem>> {
        let mut items = Vec::new();

        let query = r#"
            SELECT
                e.id,
                e.timestamp,
                e.source,
                e.content,
                e.meta,
                d.access_count,
                d.last_accessed,
                d.pinned,
                COUNT(DISTINCT et.tag) as tag_count,
                COUNT(DISTINCT el.to_event_id) as link_count
            FROM events e
            LEFT JOIN decay d ON e.id = d.event_id
            LEFT JOIN shadow_state s ON e.id = s.event_id
            LEFT JOIN event_tags et ON e.id = et.event_id
            LEFT JOIN event_links el ON e.id = el.from_event_id
            WHERE
                s.event_id IS NULL
                AND
                EXISTS (
                    SELECT 1 FROM decay
                    WHERE event_id = e.id
                    AND access_count < ?1
                    AND (unixepoch() - last_accessed) > ?2 * 86400
                    AND pinned = 0
                )
            GROUP BY e.id, e.timestamp, e.source, e.content, e.meta, d.access_count, d.last_accessed, d.pinned
            ORDER BY d.last_accessed ASC, access_count ASC
        "#;

        let mut stmt = conn.prepare(query)?;

        let rows = stmt.query_map((5, self.decay_threshold), |row| {
            let id: String = row.get(0)?;
            let timestamp: i64 = row.get(1)?;
            let source: String = row.get(2)?;
            let content: String = row.get(3)?;
            let meta: Option<String> = row.get(4)?;
            let access_count: i64 = row.get(5)?;
            let last_accessed: i64 = row.get(6)?;
            let pinned: bool = row.get(7)?;
            let tag_count: i64 = row.get(8)?;
            let link_count: i64 = row.get(9)?;

            Ok(AttentionItem {
                id,
                timestamp,
                source,
                content,
                meta,
                access_count,
                last_accessed,
                pinned,
                tag_count,
                link_count,
            })
        })?;

        for item in rows {
            items.push(item?);
        }

        Ok(items)
    }

    /// Calculates attention score for an event
    pub fn calculate_attention_score(&self, conn: &Connection, event_id: &str) -> Result<f64> {
        let decay_score = get_decay_score(conn, event_id)?;

        // Higher decay score = more recently accessed
        // Lower decay score = more recently accessed
        // Normalize to 0-100 scale
        let score = (1.0 / (decay_score + 1.0)) * 100.0;

        Ok(score)
    }

    /// Gets attention statistics
    pub fn get_stats(&self, conn: &Connection) -> Result<AttentionStats> {
        let total_events: i64 = conn.query_row(
            "SELECT COUNT(*)
             FROM events e
             WHERE NOT EXISTS (
                 SELECT 1 FROM shadow_state s WHERE s.event_id = e.id
             )",
            [],
            |row| row.get(0),
        )?;

        let active_events: i64 = conn.query_row(
            "SELECT COUNT(*)
             FROM decay d
             WHERE d.last_accessed > unixepoch() - ?1 * 86400
             AND NOT EXISTS (
                 SELECT 1 FROM shadow_state s WHERE s.event_id = d.event_id
             )",
            (self.decay_threshold,),
            |row| row.get(0),
        )?;

        let pinned_events: i64 = conn.query_row(
            "SELECT COUNT(*)
             FROM decay d
             WHERE d.pinned = 1
             AND NOT EXISTS (
                 SELECT 1 FROM shadow_state s WHERE s.event_id = d.event_id
             )",
            [],
            |row| row.get(0),
        )?;

        let flagged_events: i64 = conn.query_row(
            "SELECT COUNT(*)
             FROM decay d
             WHERE d.access_count < ?1
             AND (unixepoch() - d.last_accessed) > ?2 * 86400
             AND d.pinned = 0
             AND NOT EXISTS (
                 SELECT 1 FROM shadow_state s WHERE s.event_id = d.event_id
             )",
            (5, self.decay_threshold),
            |row| row.get(0),
        )?;

        Ok(AttentionStats {
            total_events,
            active_events,
            pinned_events,
            flagged_events,
            decay_threshold: self.decay_threshold,
        })
    }
}

/// An item in the attention layer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttentionItem {
    /// Event ID
    pub id: String,
    /// Timestamp of the event
    pub timestamp: i64,
    /// Source of the event
    pub source: String,
    /// Content of the event
    pub content: String,
    /// Optional metadata
    pub meta: Option<String>,
    /// Number of times accessed
    pub access_count: i64,
    /// Last access timestamp
    pub last_accessed: i64,
    /// Whether the item is pinned (not subject to decay)
    pub pinned: bool,
    /// Number of tags
    pub tag_count: i64,
    /// Number of links to/from this event
    pub link_count: i64,
}

impl AttentionItem {
    /// Gets the last access time as a human-readable string
    pub fn last_accessed_str(&self) -> String {
        if self.last_accessed == 0 {
            "Never".to_string()
        } else {
            let last_accessed = UNIX_EPOCH.elapsed().unwrap_or_default()
                - std::time::Duration::from_secs(self.last_accessed as u64);
            format_duration(last_accessed)
        }
    }

    /// Gets the timestamp as a human-readable string
    pub fn timestamp_str(&self) -> String {
        if self.timestamp == 0 {
            "Unknown".to_string()
        } else {
            let timestamp = UNIX_EPOCH.elapsed().unwrap_or_default()
                - std::time::Duration::from_secs(self.timestamp as u64);
            format_duration(timestamp)
        }
    }
}

/// Statistics for the attention layer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttentionStats {
    /// Total number of events
    pub total_events: i64,
    /// Number of active events
    pub active_events: i64,
    /// Number of pinned events
    pub pinned_events: i64,
    /// Number of flagged events (due for decay)
    pub flagged_events: i64,
    /// Decay threshold in days
    pub decay_threshold: i64,
}

impl AttentionStats {
    /// Gets the percentage of active events
    pub fn active_percentage(&self) -> f64 {
        if self.total_events == 0 {
            0.0
        } else {
            (self.active_events as f64 / self.total_events as f64) * 100.0
        }
    }
}

/// Formats a duration as a human-readable string
fn format_duration(duration: std::time::Duration) -> String {
    let seconds = duration.as_secs();

    if seconds < 60 {
        format!("{}s", seconds)
    } else if seconds < 3600 {
        let minutes = seconds / 60;
        format!("{}m", minutes)
    } else if seconds < 86400 {
        let hours = seconds / 3600;
        format!("{}h", hours)
    } else {
        let days = seconds / 86400;
        format!("{}d", days)
    }
}

/// Initializes the attention layer tables
pub fn init_tables(conn: &Connection) -> Result<()> {
    // Create attention_items table
    conn.execute(
        "CREATE TABLE IF NOT EXISTS attention_items (
            id TEXT PRIMARY KEY,
            event_id TEXT NOT NULL,
            priority INTEGER NOT NULL DEFAULT 0,
            last_accessed INTEGER NOT NULL,
            reference_count INTEGER NOT NULL DEFAULT 0,
            tags TEXT,
            links TEXT,
            meta TEXT,
            FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
        )",
        [],
    )?;

    // Create reference_points table
    conn.execute(
        "CREATE TABLE IF NOT EXISTS reference_points (
            id TEXT PRIMARY KEY,
            attention_item_id TEXT NOT NULL,
            reference_type TEXT NOT NULL,
            reference_value TEXT NOT NULL,
            created_at INTEGER NOT NULL DEFAULT (unixepoch()),
            FOREIGN KEY (attention_item_id) REFERENCES attention_items(id) ON DELETE CASCADE
        )",
        [],
    )?;

    // Create attention_decay table
    conn.execute(
        "CREATE TABLE IF NOT EXISTS attention_decay (
            id TEXT PRIMARY KEY,
            attention_item_id TEXT NOT NULL,
            decay_score REAL NOT NULL,
            last_decay_check INTEGER NOT NULL DEFAULT (unixepoch()),
            FOREIGN KEY (attention_item_id) REFERENCES attention_items(id) ON DELETE CASCADE
        )",
        [],
    )?;

    Ok(())
}

/// Initializes the attention layer with defaults
pub fn init_with_defaults(conn: &Connection) -> Result<()> {
    init_tables(conn)?;
    Ok(())
}