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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use crate::context::CommandContext;
use crate::display::{format_with_type_prefix, truncated_prefixes};
use crate::error::Result;
use crate::models::{Critique, CritiqueStatus, Priority, ProblemStatus, SolutionStatus};
use std::collections::HashMap;

fn priority_sort_value(priority: &Priority) -> i32 {
    match priority {
        Priority::Critical => 3,
        Priority::High => 2,
        Priority::Medium => 1,
        Priority::Low => 0,
    }
}

pub fn execute(
    ctx: &CommandContext,
    all: bool,
    mine: bool,
    limit: Option<usize>,
    json: bool,
) -> Result<()> {
    let store = &ctx.store;
    let jj_client = ctx.jj();
    let user = store.jj_client.user_identity().unwrap_or_default();

    let problems = store.list_problems()?;
    let solutions = store.list_solutions()?;
    let critiques = store.list_critiques()?;

    if json {
        // Build JSON output with active_solution, next_actions, summary
        let current_change = jj_client.current_change_id().ok();
        let active_solution = current_change
            .as_ref()
            .and_then(|change_id| solutions.iter().find(|s| s.change_ids.contains(change_id)));

        let active_json = active_solution.map(|s| {
            serde_json::json!({
                "id": s.id,
                "title": s.title,
                "problem_id": s.problem_id,
                "status": format!("{}", s.status),
            })
        });

        let mut items = build_next_actions(&problems, &solutions, &critiques, &user, mine);
        let total_count = items.len();
        let effective_limit = if all { usize::MAX } else { limit.unwrap_or(5) };
        items.truncate(effective_limit);

        let open_problems = problems.iter().filter(|p| p.is_open()).count();
        let review_solutions = solutions
            .iter()
            .filter(|s| s.status == SolutionStatus::Submitted)
            .count();
        let open_critiques = critiques
            .iter()
            .filter(|c| c.status == CritiqueStatus::Open)
            .count();

        // Check file overlaps for JSON output
        let overlaps = super::overlaps::find_overlaps(ctx)?;
        let overlaps_json: Vec<serde_json::Value> = overlaps
            .iter()
            .map(|o| {
                serde_json::json!({
                    "file": o.file.to_string_lossy(),
                    "solutions": o.solutions.iter().map(|(id, title)| {
                        serde_json::json!({ "id": id, "title": title })
                    }).collect::<Vec<_>>(),
                })
            })
            .collect();

        let output = serde_json::json!({
            "active_solution": active_json,
            "items": items,
            "total_count": total_count,
            "user": user,
            "summary": {
                "open_problems": open_problems,
                "review_solutions": review_solutions,
                "open_critiques": open_critiques,
            },
            "overlaps": overlaps_json,
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        // Build a prefix map for all entity IDs for display
        let all_uuids: Vec<&str> = problems
            .iter()
            .map(|p| p.id.as_str())
            .chain(solutions.iter().map(|s| s.id.as_str()))
            .chain(critiques.iter().map(|c| c.id.as_str()))
            .collect();
        let prefix_list = truncated_prefixes(&all_uuids);
        let prefix_map: HashMap<&str, &str> = prefix_list
            .iter()
            .map(|(uuid, prefix)| (uuid.as_str(), prefix.as_str()))
            .collect();

        // Helper to get display ID with type prefix
        let display_id = |entity_type: &str, id: &str| -> String {
            let prefix = prefix_map.get(id).copied().unwrap_or(id);
            format_with_type_prefix(entity_type, prefix)
        };

        // 1. Active Solution section
        let current_change = jj_client.current_change_id().ok();
        if let Some(change_id) = &current_change {
            if let Some(active) = solutions.iter().find(|s| s.change_ids.contains(change_id)) {
                let solution_display = display_id("solution", &active.id);
                let problem_display = display_id("problem", &active.problem_id);
                println!(
                    "Active: {} \"{}\" -> {} [{}]",
                    solution_display, active.title, problem_display, active.status
                );

                // Show open critiques on active solution
                let active_critiques: Vec<_> = critiques
                    .iter()
                    .filter(|c| c.solution_id == active.id && c.status == CritiqueStatus::Open)
                    .collect();
                if !active_critiques.is_empty() {
                    println!("  Open critiques: {}", active_critiques.len());
                    for c in &active_critiques {
                        let critique_display = display_id("critique", &c.id);
                        println!("    {}: {} [{}]", critique_display, c.title, c.severity);
                    }
                }
                println!();
            }
        }

        // 2. Next Actions section
        let mut items = build_next_actions(&problems, &solutions, &critiques, &user, mine);
        let total_count = items.len();
        let effective_limit = if all { usize::MAX } else { limit.unwrap_or(5) };
        items.truncate(effective_limit);

        if items.is_empty() {
            println!("No pending actions. All caught up!");
        } else {
            println!("Next actions:\n");
            for (i, item) in items.iter().enumerate() {
                let category = item["category"].as_str().unwrap_or("").to_uppercase();
                let entity_type = item["entity_type"].as_str().unwrap_or("");
                let entity_id = item["entity_id"].as_str().unwrap_or("");
                let title = item["title"].as_str().unwrap_or("");
                let summary = item["summary"].as_str().unwrap_or("");

                let entity_display = display_id(entity_type, entity_id);
                println!(
                    "{}. [{}] {}: {} -- {}",
                    i + 1,
                    category,
                    entity_display,
                    title,
                    summary
                );

                if let Some(details) = item["details"].as_array() {
                    for detail in details {
                        let detail_id = detail["id"].as_str().unwrap_or("");
                        let text = detail["text"].as_str().unwrap_or("");
                        let severity = detail["severity"].as_str().unwrap_or("");
                        // Details in status are always critiques
                        let detail_display = display_id("critique", detail_id);
                        println!("   {}: {} [{}]", detail_display, text, severity);
                    }
                }

                if let Some(cmd) = item["suggested_command"].as_str() {
                    if !cmd.is_empty() {
                        println!("   -> {}", cmd);
                    }
                }
                println!();
            }

            if !all && total_count > effective_limit {
                println!(
                    "Showing {} of {} items. Use --all to see everything.",
                    effective_limit, total_count
                );
            }
        }

        // 3. Summary section
        let open_problems = problems
            .iter()
            .filter(|p| p.status == ProblemStatus::Open || p.status == ProblemStatus::InProgress)
            .count();
        let review_solutions = solutions
            .iter()
            .filter(|s| s.status == SolutionStatus::Submitted)
            .count();
        let open_critiques = critiques
            .iter()
            .filter(|c| c.status == CritiqueStatus::Open)
            .count();

        println!(
            "\nSummary: {} open problems, {} in review, {} open critiques",
            open_problems, review_solutions, open_critiques
        );

        // Check file overlaps
        let overlaps = super::overlaps::find_overlaps(ctx)?;
        if !overlaps.is_empty() {
            let total_files: usize = overlaps.len();
            println!(
                "\nâš  {} file{} touched by multiple active solutions:",
                total_files,
                if total_files == 1 { "" } else { "s" }
            );
            for overlap in &overlaps {
                let sol_names: Vec<&str> = overlap
                    .solutions
                    .iter()
                    .map(|(_, title)| title.as_str())
                    .collect();
                println!("  {} — {}", overlap.file.display(), sol_names.join(", "));
            }
        }
    }

    Ok(())
}

pub(crate) fn build_next_actions(
    problems: &[crate::models::Problem],
    solutions: &[crate::models::Solution],
    critiques: &[Critique],
    user: &str,
    mine: bool,
) -> Vec<serde_json::Value> {
    let mut items: Vec<serde_json::Value> = Vec::new();

    // 1. BLOCKED: Solutions with open critiques
    for solution in solutions.iter().filter(|s| s.is_active()) {
        let open_critiques: Vec<&Critique> = critiques
            .iter()
            .filter(|c| {
                c.solution_id == solution.id
                    && matches!(c.status, CritiqueStatus::Open | CritiqueStatus::Valid)
            })
            .collect();

        if !open_critiques.is_empty() {
            let top_critique = open_critiques
                .iter()
                .max_by_key(|c| c.severity.clone())
                .unwrap();

            let problem = problems.iter().find(|p| p.id == solution.problem_id);
            let priority = problem.map(|p| &p.priority).cloned().unwrap_or_default();

            // Build severity breakdown: "3 critiques: 1 critical, 2 medium"
            let mut severity_counts: std::collections::BTreeMap<String, usize> =
                std::collections::BTreeMap::new();
            for c in &open_critiques {
                *severity_counts
                    .entry(format!("{}", c.severity))
                    .or_insert(0) += 1;
            }
            // Ordered highest to lowest
            let severity_order = ["critical", "high", "medium", "low"];
            let parts: Vec<String> = severity_order
                .iter()
                .filter_map(|s| severity_counts.get(*s).map(|n| format!("{} {}", n, s)))
                .collect();
            let summary = if parts.is_empty() {
                format!("{} open critique(s)", open_critiques.len())
            } else {
                format!(
                    "{} critique{}: {}",
                    open_critiques.len(),
                    if open_critiques.len() == 1 { "" } else { "s" },
                    parts.join(", ")
                )
            };

            items.push(serde_json::json!({
                "category": "blocked",
                "entity_type": "solution",
                "entity_id": solution.id,
                "title": solution.title,
                "summary": summary,
                "suggested_command": format!("jjj critique show {}", top_critique.id),
                "priority": format!("{}", priority),
                "priority_sort": priority_sort_value(&priority),
                "created_at": solution.created_at.to_rfc3339(),
                "details": open_critiques.iter().map(|c| serde_json::json!({
                    "id": c.id,
                    "text": c.title,
                    "severity": format!("{}", c.severity),
                })).collect::<Vec<_>>(),
            }));
        }
    }

    // 2. READY: Solutions with all critiques resolved + review satisfied
    for solution in solutions.iter().filter(|s| s.is_active()) {
        let has_open = critiques.iter().any(|c| {
            c.solution_id == solution.id
                && matches!(c.status, CritiqueStatus::Open | CritiqueStatus::Valid)
        });

        if !has_open && !solution.critique_ids.is_empty() {
            let problem = problems.iter().find(|p| p.id == solution.problem_id);
            let priority = problem.map(|p| &p.priority).cloned().unwrap_or_default();

            items.push(serde_json::json!({
                "category": "ready",
                "entity_type": "solution",
                "entity_id": solution.id,
                "title": solution.title,
                "summary": "All critiques resolved",
                "suggested_command": format!("jjj solution approve {}", solution.id),
                "priority": format!("{}", priority),
                "priority_sort": priority_sort_value(&priority),
                "created_at": solution.created_at.to_rfc3339(),
                "details": [],
            }));
        }
    }

    // 3. REVIEW: Critiques assigned to user that need response
    if !mine {
        for critique in critiques
            .iter()
            .filter(|c| c.status == CritiqueStatus::Open)
        {
            if let Some(reviewer) = &critique.reviewer {
                if user.contains(reviewer) || reviewer.contains(user) {
                    let solution = solutions.iter().find(|s| s.id == critique.solution_id);
                    let problem =
                        solution.and_then(|s| problems.iter().find(|p| p.id == s.problem_id));
                    let priority = problem.map(|p| &p.priority).cloned().unwrap_or_default();

                    items.push(serde_json::json!({
                        "category": "review",
                        "entity_type": "critique",
                        "entity_id": critique.id,
                        "title": critique.title,
                        "summary": format!("Review requested on {}", critique.solution_id),
                        "suggested_command": format!("jjj critique show {}", critique.id),
                        "priority": format!("{}", priority),
                        "priority_sort": priority_sort_value(&priority),
                        "created_at": critique.created_at.to_rfc3339(),
                        "details": [],
                    }));
                }
            }
        }
    }

    // 4. WAITING: User's solutions with pending review critiques (assigned to others)
    for solution in solutions.iter().filter(|s| s.is_active()) {
        let is_mine = solution
            .assignee
            .as_ref()
            .map(|a| user == *a)
            .unwrap_or(false);
        if is_mine {
            let pending_reviews: Vec<_> = critiques
                .iter()
                .filter(|c| c.solution_id == solution.id && c.status == CritiqueStatus::Open)
                .filter(|c| {
                    c.reviewer.is_some()
                        && c.reviewer
                            .as_ref()
                            .map(|r| !user.contains(r))
                            .unwrap_or(false)
                })
                .collect();

            if !pending_reviews.is_empty() {
                let problem = problems.iter().find(|p| p.id == solution.problem_id);
                let priority = problem.map(|p| &p.priority).cloned().unwrap_or_default();
                let reviewers: Vec<_> = pending_reviews
                    .iter()
                    .filter_map(|c| c.reviewer.as_ref())
                    .map(|r| format!("@{}", r))
                    .collect();

                items.push(serde_json::json!({
                    "category": "waiting",
                    "entity_type": "solution",
                    "entity_id": solution.id,
                    "title": solution.title,
                    "summary": format!("Awaiting review from {}", reviewers.join(", ")),
                    "suggested_command": "",
                    "priority": format!("{}", priority),
                    "priority_sort": priority_sort_value(&priority),
                    "created_at": solution.created_at.to_rfc3339(),
                    "details": [],
                }));
            }
        }
    }

    // 5. Open problems with no active solutions
    for problem in problems.iter().filter(|p| p.is_open()) {
        let has_active_solution = solutions
            .iter()
            .any(|s| s.problem_id == problem.id && s.is_active());

        if !has_active_solution {
            items.push(serde_json::json!({
                "category": "todo",
                "entity_type": "problem",
                "entity_id": problem.id,
                "title": problem.title,
                "summary": "No solutions proposed",
                "suggested_command": format!("jjj solution new \"title\" --problem {}", problem.id),
                "priority": format!("{}", problem.priority),
                "priority_sort": priority_sort_value(&problem.priority),
                "created_at": problem.created_at.to_rfc3339(),
                "details": [],
            }));
        }
    }

    // Sort: category order, then priority descending, then age ascending (older first)
    items.sort_by(|a, b| {
        let cat_order = |cat: &str| -> i32 {
            match cat {
                "blocked" => 0,
                "ready" => 1,
                "review" => 2,
                "waiting" => 3,
                "todo" => 4,
                _ => 5,
            }
        };
        let a_cat = cat_order(a["category"].as_str().unwrap_or(""));
        let b_cat = cat_order(b["category"].as_str().unwrap_or(""));
        if a_cat != b_cat {
            return a_cat.cmp(&b_cat);
        }
        // Within same category, sort by priority descending
        let a_pri = a["priority_sort"].as_i64().unwrap_or(0);
        let b_pri = b["priority_sort"].as_i64().unwrap_or(0);
        if a_pri != b_pri {
            return b_pri.cmp(&a_pri);
        }
        // Tiebreak: older items first (smaller created_at string sorts earlier for RFC3339)
        let a_ts = a["created_at"].as_str().unwrap_or("");
        let b_ts = b["created_at"].as_str().unwrap_or("");
        a_ts.cmp(b_ts)
    });

    items
}