bones-cli 0.24.0

CLI binary for bones issue tracker
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! TUI create dialog with duplicate detection.
//!
//! When the user presses `c` in the triage view (or any view), this overlay
//! appears. As the user types a title, similar existing items are shown below
//! via `hybrid_search`, allowing the user to:
//!
//! - Press **Enter** to create the new item
//! - Press **l** (or Enter on a highlighted similar item) to link/navigate to it
//! - Press **Esc** to cancel

use anyhow::{Context, Result};
use bones_core::config::load_project_config;
use bones_core::db::query;
use bones_search::fusion::{hybrid_search, hybrid_search_fast};
use bones_search::semantic::SemanticModel;
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph},
};
use std::path::PathBuf;
use std::time::{Duration, Instant};

/// A similar item candidate shown in the panel.
#[derive(Clone)]
struct SimilarCandidate {
    item_id: String,
    title: String,
    score: f32,
}

/// The action the dialog wants the caller to take.
pub enum DialogAction {
    /// Create a new item with this title.
    Create(String),
    /// Navigate to an existing item (user chose to link instead of create).
    LinkTo(String),
    /// The user cancelled; close the dialog.
    Cancel,
}

/// Overlay dialog for creating a new item with live duplicate detection.
pub struct CreateDialog {
    db_path: PathBuf,
    semantic_model: Option<std::sync::Arc<SemanticModel>>,
    /// Title the user is typing.
    title: String,
    /// Debounced search results.
    similar: Vec<SimilarCandidate>,
    /// Selection state for the similar items list.
    similar_state: ListState,
    /// When the title was last changed (for debounce).
    last_change: Instant,
    /// The query that was last searched (avoids redundant lookups).
    last_searched: String,
    /// Receiver for background semantic refinement results.
    refinement_rx: Option<std::sync::mpsc::Receiver<Vec<SimilarCandidate>>>,
}

impl CreateDialog {
    /// Create a new dialog backed by the given projection database.
    pub fn new(db_path: PathBuf) -> Self {
        let semantic_enabled = db_path
            .parent()
            .and_then(std::path::Path::parent)
            .and_then(|root| load_project_config(root).ok())
            .map(|cfg| cfg.search.semantic)
            .unwrap_or(true);
        let semantic_model = if semantic_enabled {
            match SemanticModel::load() {
                Ok(model) => Some(std::sync::Arc::new(model)),
                Err(err) => {
                    tracing::warn!(
                        "semantic model unavailable in create dialog; using lexical+structural only: {err}"
                    );
                    None
                }
            }
        } else {
            None
        };

        Self {
            db_path,
            semantic_model,
            title: String::new(),
            similar: Vec::new(),
            similar_state: ListState::default(),
            last_change: Instant::now(),
            last_searched: String::new(),
            refinement_rx: None,
        }
    }

    // -----------------------------------------------------------------------
    // Input handling
    // -----------------------------------------------------------------------

    /// Feed a key event to the dialog.
    ///
    /// Returns `Some(DialogAction)` when the dialog is complete (caller should
    /// close the overlay), or `None` when the user is still typing.
    pub fn handle_key(&mut self, key: KeyEvent) -> Result<Option<DialogAction>> {
        match key.code {
            // Cancel
            KeyCode::Esc => Ok(Some(DialogAction::Cancel)),

            // Confirm create
            KeyCode::Enter => {
                if self.title.trim().is_empty() {
                    // Nothing typed yet — cancel silently
                    Ok(Some(DialogAction::Cancel))
                } else if let Some(idx) = self.similar_state.selected() {
                    if let Some(candidate) = self.similar.get(idx) {
                        // User has a similar item highlighted — they want to link to it
                        Ok(Some(DialogAction::LinkTo(candidate.item_id.clone())))
                    } else {
                        Ok(Some(DialogAction::Create(self.title.trim().to_string())))
                    }
                } else {
                    Ok(Some(DialogAction::Create(self.title.trim().to_string())))
                }
            }

            // 'l' — explicitly link to selected similar item
            KeyCode::Char('l') => {
                if let Some(idx) = self.similar_state.selected() {
                    if let Some(candidate) = self.similar.get(idx) {
                        return Ok(Some(DialogAction::LinkTo(candidate.item_id.clone())));
                    }
                }
                Ok(None)
            }

            // Navigate similar items list
            KeyCode::Down | KeyCode::Char('j') => {
                self.select_next();
                Ok(None)
            }
            KeyCode::Up | KeyCode::Char('k') => {
                self.select_prev();
                Ok(None)
            }

            // Backspace
            KeyCode::Backspace => {
                self.title.pop();
                self.last_change = Instant::now();
                self.similar_state.select(None);
                Ok(None)
            }

            // Regular character input
            KeyCode::Char(c) => {
                self.title.push(c);
                self.last_change = Instant::now();
                self.similar_state.select(None);
                Ok(None)
            }

            _ => Ok(None),
        }
    }

    /// Tick — called periodically to trigger debounced search and poll refinement.
    pub fn tick(&mut self) -> Result<()> {
        let debounce = Duration::from_millis(300);
        if self.title != self.last_searched && self.last_change.elapsed() >= debounce {
            self.refresh_similar()?;
        }

        // Poll for background semantic refinement results.
        if let Some(rx) = &self.refinement_rx {
            if let Ok(refined) = rx.try_recv() {
                tracing::debug!(count = refined.len(), "create dialog: tier-2 refinement applied");
                self.similar = refined;
                self.refinement_rx = None;
            }
        }

        Ok(())
    }

    fn refresh_similar(&mut self) -> Result<()> {
        self.last_searched = self.title.clone();
        self.refinement_rx = None;

        if self.title.trim().is_empty() {
            self.similar.clear();
            return Ok(());
        }

        let conn = match query::try_open_projection(&self.db_path)? {
            Some(c) => c,
            None => {
                self.similar.clear();
                return Ok(());
            }
        };

        // Auto-wildcard for prefix-style type-ahead
        let q = if !self.title.contains(' ') && !self.title.contains('*') {
            format!("{}*", self.title)
        } else {
            self.title.clone()
        };

        // Tier 1: fast search (lexical + structural only).
        let raw = hybrid_search_fast(&q, &conn, 5, 60).context("fast hybrid search")?;

        self.similar = raw
            .into_iter()
            .filter_map(|r| {
                let title = query::get_item(&conn, &r.item_id, false).ok()??.title;
                Some(SimilarCandidate {
                    item_id: r.item_id,
                    title,
                    score: r.score,
                })
            })
            .collect();

        // Tier 2: spawn background semantic refinement.
        if let Some(model) = self.semantic_model.clone() {
            let db_path = self.db_path.clone();
            let query_owned = q;
            let (tx, rx) = std::sync::mpsc::channel();
            self.refinement_rx = Some(rx);

            std::thread::spawn(move || {
                let conn = match query::try_open_projection(&db_path) {
                    Ok(Some(c)) => c,
                    _ => return,
                };
                let hits = match hybrid_search(&query_owned, &conn, Some(&model), 5, 60) {
                    Ok(h) => h,
                    Err(e) => {
                        tracing::debug!("create dialog tier-2 failed: {e:#}");
                        return;
                    }
                };
                let candidates: Vec<SimilarCandidate> = hits
                    .into_iter()
                    .filter_map(|r| {
                        let title = query::get_item(&conn, &r.item_id, false).ok()??.title;
                        Some(SimilarCandidate {
                            item_id: r.item_id,
                            title,
                            score: r.score,
                        })
                    })
                    .collect();
                let _ = tx.send(candidates);
            });
        }

        Ok(())
    }

    fn select_next(&mut self) {
        let len = self.similar.len();
        if len == 0 {
            return;
        }
        let i = self
            .similar_state
            .selected()
            .map_or(0, |i| if i + 1 >= len { 0 } else { i + 1 });
        self.similar_state.select(Some(i));
    }

    fn select_prev(&mut self) {
        let len = self.similar.len();
        if len == 0 {
            return;
        }
        let i = self
            .similar_state
            .selected()
            .map_or(0, |i| if i == 0 { len - 1 } else { i - 1 });
        self.similar_state.select(Some(i));
    }

    // -----------------------------------------------------------------------
    // Rendering
    // -----------------------------------------------------------------------

    /// Render the dialog as a centered overlay on top of `area`.
    pub fn render(&mut self, frame: &mut Frame, area: Rect) {
        // Dialog dimensions
        let dialog_w: u16 = 70.min(area.width.saturating_sub(4));
        let has_similar = !self.similar.is_empty();
        let dialog_h: u16 = if has_similar {
            5 + self.similar.len() as u16 + 2
        } else {
            5
        };
        let dialog_h = dialog_h.min(area.height.saturating_sub(4));

        let x = area.x + area.width.saturating_sub(dialog_w) / 2;
        let y = area.y + area.height.saturating_sub(dialog_h) / 2;

        let dialog_area = Rect {
            x,
            y,
            width: dialog_w,
            height: dialog_h,
        };

        frame.render_widget(Clear, dialog_area);

        let block = Block::default()
            .borders(Borders::ALL)
            .title(" Create Item ")
            .title_style(
                Style::default()
                    .fg(Color::Green)
                    .add_modifier(Modifier::BOLD),
            )
            .style(Style::default().bg(Color::Black));

        frame.render_widget(block, dialog_area);

        let inner = Rect {
            x: dialog_area.x + 1,
            y: dialog_area.y + 1,
            width: dialog_area.width.saturating_sub(2),
            height: dialog_area.height.saturating_sub(2),
        };

        // Divide inner area: title input (3 lines) + similar list
        let constraints = if has_similar {
            vec![Constraint::Length(3), Constraint::Min(0)]
        } else {
            vec![Constraint::Length(3)]
        };

        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints(constraints)
            .split(inner);

        // Title input
        let title_display = format!("{}_", self.title);
        let title_para = Paragraph::new(title_display.as_str())
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(" Title ")
                    .border_style(Style::default().fg(Color::Yellow)),
            )
            .style(Style::default().fg(Color::White));
        frame.render_widget(title_para, chunks[0]);

        // Similar items panel
        if has_similar {
            let items: Vec<ListItem> = self
                .similar
                .iter()
                .map(|c| {
                    let line = Line::from(vec![
                        Span::styled(
                            format!("{:<10} ", c.item_id),
                            Style::default().fg(Color::Cyan),
                        ),
                        Span::styled(
                            format!("{:.3} ", c.score as f64),
                            Style::default().fg(Color::Yellow),
                        ),
                        Span::raw(c.title.clone()),
                    ]);
                    ListItem::new(line)
                })
                .collect();

            let list = List::new(items)
                .block(
                    Block::default()
                        .borders(Borders::ALL)
                        .title(" Similar Items (↑↓ navigate, l=link, Enter=create anyway) ")
                        .border_style(Style::default().fg(Color::DarkGray)),
                )
                .highlight_style(
                    Style::default()
                        .bg(Color::DarkGray)
                        .add_modifier(Modifier::BOLD),
                )
                .highlight_symbol("â–º ");

            frame.render_stateful_widget(list, chunks[1], &mut self.similar_state);
        }

        // Key hint bar at bottom of dialog (rendered in last line of dialog_area)
        if dialog_area.height > 2 {
            let hint_area = Rect {
                x: dialog_area.x + 1,
                y: dialog_area.y + dialog_area.height - 2,
                width: dialog_area.width.saturating_sub(2),
                height: 1,
            };
            let hints = Line::from(vec![
                Span::styled("Enter", Style::default().fg(Color::Yellow)),
                Span::raw(" create  "),
                Span::styled("l", Style::default().fg(Color::Yellow)),
                Span::raw(" link to selected  "),
                Span::styled("Esc", Style::default().fg(Color::Yellow)),
                Span::raw(" cancel"),
            ]);
            frame.render_widget(Paragraph::new(hints), hint_area);
        }
    }

    /// The current title being entered (for external read-back).
    #[cfg(test)]
    pub fn title(&self) -> &str {
        &self.title
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bones_core::db::migrations;
    use bones_core::db::project::{Projector, ensure_tracking_table};
    use bones_core::event::Event;
    use bones_core::event::data::{CreateData, EventData};
    use bones_core::event::types::EventType;
    use bones_core::model::item::{Kind, Size, Urgency};
    use bones_core::model::item_id::ItemId;
    use rusqlite::Connection;
    use std::collections::BTreeMap;
    use tempfile::tempdir;

    fn setup_db() -> (tempfile::TempDir, PathBuf) {
        let dir = tempdir().expect("tempdir");
        let db_path = dir.path().join("bones.db");
        let mut conn = Connection::open(&db_path).expect("open db");
        migrations::migrate(&mut conn).expect("migrate");
        ensure_tracking_table(&conn).expect("tracking table");
        (dir, db_path)
    }

    fn insert_item(conn: &Connection, id: &str, title: &str) {
        let proj = Projector::new(conn);
        proj.project_event(&Event {
            wall_ts_us: 1000,
            agent: "test".into(),
            itc: "itc:AQ".into(),
            parents: vec![],
            event_type: EventType::Create,
            item_id: ItemId::new_unchecked(id),
            data: EventData::Create(CreateData {
                title: title.into(),
                kind: Kind::Task,
                size: Some(Size::M),
                urgency: Urgency::Default,
                labels: vec![],
                parent: None,
                causation: None,
                description: None,
                extra: BTreeMap::new(),
            }),
            event_hash: format!("hash:{id}"),
        })
        .unwrap();
    }

    #[test]
    fn dialog_starts_empty() {
        let (_dir, db_path) = setup_db();
        let dialog = CreateDialog::new(db_path);
        assert!(dialog.title().is_empty());
        assert!(dialog.similar.is_empty());
    }

    #[test]
    fn dialog_accepts_typed_characters() {
        let (_dir, db_path) = setup_db();
        let mut dialog = CreateDialog::new(db_path);

        dialog
            .handle_key(KeyEvent::from(KeyCode::Char('h')))
            .unwrap();
        dialog
            .handle_key(KeyEvent::from(KeyCode::Char('i')))
            .unwrap();
        assert_eq!(dialog.title(), "hi");
    }

    #[test]
    fn dialog_backspace_removes_char() {
        let (_dir, db_path) = setup_db();
        let mut dialog = CreateDialog::new(db_path);

        dialog
            .handle_key(KeyEvent::from(KeyCode::Char('a')))
            .unwrap();
        dialog
            .handle_key(KeyEvent::from(KeyCode::Char('b')))
            .unwrap();
        dialog
            .handle_key(KeyEvent::from(KeyCode::Backspace))
            .unwrap();
        assert_eq!(dialog.title(), "a");
    }

    #[test]
    fn dialog_esc_cancels() {
        let (_dir, db_path) = setup_db();
        let mut dialog = CreateDialog::new(db_path);
        dialog
            .handle_key(KeyEvent::from(KeyCode::Char('x')))
            .unwrap();

        let action = dialog.handle_key(KeyEvent::from(KeyCode::Esc)).unwrap();
        assert!(matches!(action, Some(DialogAction::Cancel)));
    }

    #[test]
    fn dialog_enter_on_empty_title_cancels() {
        let (_dir, db_path) = setup_db();
        let mut dialog = CreateDialog::new(db_path);

        let action = dialog.handle_key(KeyEvent::from(KeyCode::Enter)).unwrap();
        assert!(matches!(action, Some(DialogAction::Cancel)));
    }

    #[test]
    fn dialog_enter_with_title_creates() {
        let (_dir, db_path) = setup_db();
        let mut dialog = CreateDialog::new(db_path);

        dialog
            .handle_key(KeyEvent::from(KeyCode::Char('N')))
            .unwrap();
        dialog
            .handle_key(KeyEvent::from(KeyCode::Char('e')))
            .unwrap();
        dialog
            .handle_key(KeyEvent::from(KeyCode::Char('w')))
            .unwrap();

        let action = dialog.handle_key(KeyEvent::from(KeyCode::Enter)).unwrap();
        match action {
            Some(DialogAction::Create(title)) => assert_eq!(title, "New"),
            _ => panic!("expected Create action"),
        }
    }

    #[test]
    fn dialog_shows_similar_items_after_search() {
        let (_dir, db_path) = setup_db();
        let conn = Connection::open(&db_path).unwrap();
        insert_item(&conn, "bn-1", "Authentication Bug Fix");

        let mut dialog = CreateDialog::new(db_path.clone());
        dialog.title = "authentication".to_string();
        dialog.refresh_similar().unwrap();

        assert_eq!(dialog.similar.len(), 1);
        assert_eq!(dialog.similar[0].item_id, "bn-1");
    }

    #[test]
    fn dialog_link_action_returns_selected_item() {
        let (_dir, db_path) = setup_db();
        let conn = Connection::open(&db_path).unwrap();
        insert_item(&conn, "bn-1", "Authentication Bug Fix");

        let mut dialog = CreateDialog::new(db_path.clone());
        dialog.title = "authentication".to_string();
        dialog.refresh_similar().unwrap();

        // Select the first similar item
        dialog.similar_state.select(Some(0));

        let action = dialog
            .handle_key(KeyEvent::from(KeyCode::Char('l')))
            .unwrap();
        match action {
            Some(DialogAction::LinkTo(id)) => assert_eq!(id, "bn-1"),
            _ => panic!("expected LinkTo action"),
        }
    }

    #[test]
    fn dialog_enter_with_selected_similar_links() {
        let (_dir, db_path) = setup_db();
        let conn = Connection::open(&db_path).unwrap();
        insert_item(&conn, "bn-1", "Authentication Bug Fix");

        let mut dialog = CreateDialog::new(db_path.clone());
        dialog.title = "authentication".to_string();
        dialog.refresh_similar().unwrap();

        // Select the first similar item
        dialog.similar_state.select(Some(0));

        let action = dialog.handle_key(KeyEvent::from(KeyCode::Enter)).unwrap();
        match action {
            Some(DialogAction::LinkTo(id)) => assert_eq!(id, "bn-1"),
            _ => panic!("expected LinkTo when similar item is selected"),
        }
    }
}