jjj 0.4.1

Distributed project management and code review for Jujutsu
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
//! Sync operations between SQLite database and markdown files.
//!
//! This module handles loading metadata from the shadow branch (markdown files)
//! into SQLite for fast queries, and dumping SQLite data back to markdown.

use rusqlite::{params, Connection};

use crate::db::entities::{
    list_critiques, list_milestones, list_problems, list_solutions,
    populate_problem_computed_fields, populate_solution_computed_fields, upsert_critique,
    upsert_milestone, upsert_problem, upsert_solution,
};
use crate::db::events::{clear_events, insert_event};
use crate::db::Database;
use crate::error::Result;
use crate::storage::MetadataStore;

/// Load all metadata from markdown files into SQLite.
///
/// This clears all SQLite tables and reloads from the shadow branch.
/// Used during initial sync or when rebuilding the cache.
pub fn load_from_markdown(db: &Database, store: &MetadataStore) -> Result<()> {
    let conn = db.conn();

    // Set dirty flag before we start modifying data
    set_dirty_internal(conn, true)?;

    // Wrap the entire load in a transaction so a partial failure doesn't
    // leave a half-populated DB.
    conn.execute_batch("BEGIN")?;

    let result = (|| -> Result<()> {
        // Clear all tables
        clear_all_tables(conn)?;

        // Load milestones first (problems reference milestones via FK)
        let milestones = store.list_milestones()?;
        for milestone in &milestones {
            upsert_milestone(conn, milestone)?;
        }

        // Load problems (solutions reference problems via FK)
        let problems = store.list_problems()?;
        for problem in &problems {
            upsert_problem(conn, problem)?;
        }

        // Load solutions (critiques reference solutions via FK)
        let solutions = store.list_solutions()?;
        for solution in &solutions {
            upsert_solution(conn, solution)?;
        }

        // Load critiques
        let critiques = store.list_critiques()?;
        for critique in &critiques {
            upsert_critique(conn, critique)?;
        }

        // Load events from the jjj commit history (the canonical source)
        let events = store.list_events()?;
        for event in &events {
            insert_event(conn, event)?;
        }

        Ok(())
    })();

    match result {
        Ok(()) => {
            conn.execute_batch("COMMIT")?;
            // Rebuild FTS index (operates outside the main transaction)
            rebuild_fts(db)?;
            // Clear dirty flag on successful completion
            set_dirty_internal(conn, false)?;
            Ok(())
        }
        Err(e) => {
            let _ = conn.execute_batch("ROLLBACK");
            Err(e)
        }
    }
}

/// Dump all metadata from SQLite back to markdown files.
///
/// This writes all entities from SQLite to the shadow branch.
/// Used when syncing local changes back to the repository.
pub fn dump_to_markdown(db: &Database, store: &MetadataStore) -> Result<()> {
    let conn = db.conn();

    // Dump problems (with computed fields populated)
    let mut problems = list_problems(conn)?;
    populate_problem_computed_fields(conn, &mut problems)?;
    for problem in &problems {
        store.save_problem(problem)?;
    }

    // Dump solutions (with computed fields populated)
    let mut solutions = list_solutions(conn)?;
    populate_solution_computed_fields(conn, &mut solutions)?;
    for solution in &solutions {
        store.save_solution(solution)?;
    }

    // Dump critiques
    let critiques = list_critiques(conn)?;
    for critique in &critiques {
        store.save_critique(critique)?;
    }

    // Dump milestones
    let milestones = list_milestones(conn)?;
    for milestone in &milestones {
        store.save_milestone(milestone)?;
    }

    // Events live in commit history — nothing to dump.

    Ok(())
}

/// Rebuild the full-text search index from all entities.
pub fn rebuild_fts(db: &Database) -> Result<()> {
    let conn = db.conn();

    // Clear existing FTS data
    conn.execute("DELETE FROM fts", [])?;

    // Index problems
    let problems = list_problems(conn)?;
    for problem in &problems {
        let body = format!("{}\n{}", problem.description, problem.tags.join(" "));
        conn.execute(
            "INSERT INTO fts (entity_type, entity_id, title, body) VALUES (?1, ?2, ?3, ?4)",
            params!["problem", &problem.id, &problem.title, &body],
        )?;
    }

    // Index solutions
    let solutions = list_solutions(conn)?;
    for solution in &solutions {
        let body = format!("{}\n{}", solution.approach, solution.tags.join(" "));
        conn.execute(
            "INSERT INTO fts (entity_type, entity_id, title, body) VALUES (?1, ?2, ?3, ?4)",
            params!["solution", &solution.id, &solution.title, &body],
        )?;
    }

    // Index critiques
    let critiques = list_critiques(conn)?;
    for critique in &critiques {
        let body = critique.argument.clone();
        conn.execute(
            "INSERT INTO fts (entity_type, entity_id, title, body) VALUES (?1, ?2, ?3, ?4)",
            params!["critique", &critique.id, &critique.title, &body],
        )?;
    }

    // Index milestones
    let milestones = list_milestones(conn)?;
    for milestone in &milestones {
        let body = milestone.description.clone();
        conn.execute(
            "INSERT INTO fts (entity_type, entity_id, title, body) VALUES (?1, ?2, ?3, ?4)",
            params!["milestone", &milestone.id, &milestone.title, &body],
        )?;
    }

    Ok(())
}

/// Update a single entity's FTS entry (upsert).
///
/// Call this after saving an entity to keep FTS in sync incrementally,
/// avoiding a full rebuild.
pub fn update_fts_entry(
    conn: &Connection,
    entity_type: &str,
    entity_id: &str,
    title: &str,
    body: &str,
) -> std::result::Result<(), rusqlite::Error> {
    // Delete existing entry if any
    conn.execute(
        "DELETE FROM fts WHERE entity_type = ?1 AND entity_id = ?2",
        params![entity_type, entity_id],
    )?;
    // Insert updated entry
    conn.execute(
        "INSERT INTO fts (entity_type, entity_id, title, body) VALUES (?1, ?2, ?3, ?4)",
        params![entity_type, entity_id, title, body],
    )?;
    Ok(())
}

/// Remove a single entity's FTS entry.
pub fn remove_fts_entry(
    conn: &Connection,
    entity_type: &str,
    entity_id: &str,
) -> std::result::Result<(), rusqlite::Error> {
    conn.execute(
        "DELETE FROM fts WHERE entity_type = ?1 AND entity_id = ?2",
        params![entity_type, entity_id],
    )?;
    Ok(())
}

/// Rebuild all embeddings from entities.
///
/// This computes embeddings for all problems, solutions, critiques, and milestones.
/// Uses batch processing for efficiency.
pub fn rebuild_embeddings(
    db: &Database,
    client: &crate::embeddings::EmbeddingClient,
) -> Result<()> {
    use crate::db::embeddings::{clear_embeddings, upsert_embedding};
    use crate::embeddings::{
        prepare_critique_text, prepare_milestone_text, prepare_problem_text, prepare_solution_text,
    };

    let conn = db.conn();
    let model = client.model();

    // Clear existing embeddings
    clear_embeddings(conn)?;

    // Process problems
    let problems = list_problems(conn)?;
    for problem in &problems {
        let text = prepare_problem_text(&problem.title, &problem.description);
        if let Ok(embedding) = client.embed(&text) {
            upsert_embedding(conn, "problem", &problem.id, model, &embedding)?;
        }
    }

    // Process solutions
    let solutions = list_solutions(conn)?;
    for solution in &solutions {
        let text = prepare_solution_text(&solution.title, &solution.approach);
        if let Ok(embedding) = client.embed(&text) {
            upsert_embedding(conn, "solution", &solution.id, model, &embedding)?;
        }
    }

    // Process critiques
    let critiques = list_critiques(conn)?;
    for critique in &critiques {
        let text = prepare_critique_text(&critique.title, &critique.argument);
        if let Ok(embedding) = client.embed(&text) {
            upsert_embedding(conn, "critique", &critique.id, model, &embedding)?;
        }
    }

    // Process milestones
    let milestones = list_milestones(conn)?;
    for milestone in &milestones {
        let text = prepare_milestone_text(&milestone.title, &milestone.description);
        if let Ok(embedding) = client.embed(&text) {
            upsert_embedding(conn, "milestone", &milestone.id, model, &embedding)?;
        }
    }

    Ok(())
}

/// Check if the database has uncommitted changes (dirty flag is set).
pub fn is_dirty(db: &Database) -> Result<bool> {
    is_dirty_internal(db.conn())
}

/// Set the dirty flag indicating uncommitted changes.
pub fn set_dirty(db: &Database, dirty: bool) -> Result<()> {
    set_dirty_internal(db.conn(), dirty)
}

// ============================================================================
// Private helpers
// ============================================================================

/// Clear all entity tables (problems, solutions, critiques, milestones, events).
fn clear_all_tables(conn: &Connection) -> Result<()> {
    // Clear in reverse order of dependencies
    conn.execute("DELETE FROM embeddings", [])?;
    conn.execute("DELETE FROM critiques", [])?;
    conn.execute("DELETE FROM solutions", [])?;
    conn.execute("DELETE FROM problems", [])?;
    conn.execute("DELETE FROM milestones", [])?;
    clear_events(conn)?;
    conn.execute("DELETE FROM fts", [])?;
    Ok(())
}

/// Internal function to check dirty flag.
fn is_dirty_internal(conn: &Connection) -> Result<bool> {
    let result: std::result::Result<String, _> =
        conn.query_row("SELECT value FROM meta WHERE key = 'dirty'", [], |row| {
            row.get(0)
        });

    match result {
        Ok(value) => Ok(value == "true" || value == "1"),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(false),
        Err(e) => Err(e.into()),
    }
}

/// Internal function to set dirty flag.
fn set_dirty_internal(conn: &Connection, dirty: bool) -> Result<()> {
    conn.execute(
        "INSERT OR REPLACE INTO meta (key, value) VALUES ('dirty', ?1)",
        [if dirty { "true" } else { "false" }],
    )?;
    Ok(())
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_dirty_flag() {
        let db = Database::open_in_memory().expect("Failed to open database");

        // Should not be dirty initially
        assert!(!is_dirty(&db).expect("Failed to check dirty"));

        // Set dirty
        set_dirty(&db, true).expect("Failed to set dirty");
        assert!(is_dirty(&db).expect("Failed to check dirty"));

        // Clear dirty
        set_dirty(&db, false).expect("Failed to clear dirty");
        assert!(!is_dirty(&db).expect("Failed to check dirty"));
    }

    #[test]
    fn test_rebuild_fts_empty() {
        let db = Database::open_in_memory().expect("Failed to open database");

        // Should not fail on empty database
        rebuild_fts(&db).expect("Failed to rebuild FTS");

        // Verify FTS is empty
        let count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM fts", [], |row| row.get(0))
            .expect("Failed to count FTS rows");
        assert_eq!(count, 0);
    }

    #[test]
    fn test_rebuild_fts_with_data() {
        use crate::db::entities::upsert_problem;
        use crate::models::Problem;

        let db = Database::open_in_memory().expect("Failed to open database");

        // Insert a problem
        let mut problem = Problem::new("p1".to_string(), "Test Problem".to_string());
        problem.description = "This is a test description".to_string();
        problem.description = "Some context here".to_string();
        upsert_problem(db.conn(), &problem).expect("Failed to insert problem");

        // Rebuild FTS
        rebuild_fts(&db).expect("Failed to rebuild FTS");

        // Verify FTS has the problem indexed
        let count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM fts", [], |row| row.get(0))
            .expect("Failed to count FTS rows");
        assert_eq!(count, 1);

        // Verify we can search for the problem using FTS
        // Note: FTS5 contentless tables don't store actual column values,
        // so we use highlight() or bm25() for ranking, not retrieving columns.
        // Here we just verify the match works by counting results.
        let match_count: i64 = db
            .conn()
            .query_row(
                "SELECT COUNT(*) FROM fts WHERE fts MATCH 'test'",
                [],
                |row| row.get(0),
            )
            .expect("Failed to search FTS");
        assert_eq!(match_count, 1);
    }

    #[test]
    fn test_clear_all_tables() {
        use crate::db::entities::{upsert_problem, upsert_solution};
        use crate::models::{Problem, Solution};

        let db = Database::open_in_memory().expect("Failed to open database");
        let conn = db.conn();

        // Insert some data
        let problem = Problem::new("p1".to_string(), "Test Problem".to_string());
        upsert_problem(conn, &problem).expect("Failed to insert problem");

        let solution = Solution::new(
            "s1".to_string(),
            "Test Solution".to_string(),
            "p1".to_string(),
        );
        upsert_solution(conn, &solution).expect("Failed to insert solution");

        // Verify data exists
        let count: i64 = conn
            .query_row("SELECT COUNT(*) FROM problems", [], |row| row.get(0))
            .expect("Failed to count problems");
        assert_eq!(count, 1);

        // Clear all tables
        clear_all_tables(conn).expect("Failed to clear tables");

        // Verify data is gone
        let count: i64 = conn
            .query_row("SELECT COUNT(*) FROM problems", [], |row| row.get(0))
            .expect("Failed to count problems");
        assert_eq!(count, 0);

        let count: i64 = conn
            .query_row("SELECT COUNT(*) FROM solutions", [], |row| row.get(0))
            .expect("Failed to count solutions");
        assert_eq!(count, 0);
    }
}