frame 0.1.5

A markdown task tracker with a terminal UI for humans and a CLI for agents
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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::model::SectionKind;
use crate::ops::task_ops::{self, InsertPosition};
use crate::util::unicode;

use crate::tui::app::{
    App, AutocompleteKind, AutocompleteState, DetailRegion, DetailState, EditHistory, EditTarget,
    Mode, MoveState, TriageSource, View,
};
use crate::tui::undo::Operation;

use super::*;

/// Add a new inbox item at the bottom and enter EDIT mode for its title.
pub(super) fn inbox_add_item(app: &mut App) {
    let inbox = match &mut app.project.inbox {
        Some(inbox) => inbox,
        None => return,
    };

    // Add an empty item at the end
    let item = crate::model::inbox::InboxItem::new(String::new());
    inbox.items.push(item);
    let new_index = inbox.items.len() - 1;

    // Move cursor to new item
    app.inbox_cursor = new_index;

    // Enter EDIT mode for the title
    app.edit_buffer.clear();
    app.edit_cursor = 0;
    app.edit_target = Some(EditTarget::NewInboxItem { index: new_index });
    app.edit_history = Some(EditHistory::new("", 0, 0));
    app.mode = Mode::Edit;
}

/// Insert a new inbox item after the current cursor position and enter EDIT mode.
pub(super) fn inbox_insert_after(app: &mut App) {
    let inbox = match &mut app.project.inbox {
        Some(inbox) => inbox,
        None => return,
    };

    let insert_at = if inbox.items.is_empty() {
        0
    } else {
        (app.inbox_cursor + 1).min(inbox.items.len())
    };

    let item = crate::model::inbox::InboxItem::new(String::new());
    inbox.items.insert(insert_at, item);

    // Move cursor to the new item
    app.inbox_cursor = insert_at;

    // Enter EDIT mode for the title
    app.edit_buffer.clear();
    app.edit_cursor = 0;
    app.edit_target = Some(EditTarget::NewInboxItem { index: insert_at });
    app.edit_history = Some(EditHistory::new("", 0, 0));
    app.mode = Mode::Edit;
}

/// Insert a new inbox item at the top and enter EDIT mode.
pub(super) fn inbox_prepend_item(app: &mut App) {
    let inbox = match &mut app.project.inbox {
        Some(inbox) => inbox,
        None => return,
    };

    let item = crate::model::inbox::InboxItem::new(String::new());
    inbox.items.insert(0, item);

    // Move cursor to new item at top
    app.inbox_cursor = 0;

    // Enter EDIT mode for the title
    app.edit_buffer.clear();
    app.edit_cursor = 0;
    app.edit_target = Some(EditTarget::NewInboxItem { index: 0 });
    app.edit_history = Some(EditHistory::new("", 0, 0));
    app.mode = Mode::Edit;
}

/// Edit the title of the selected inbox item.
pub(super) fn inbox_edit_title(app: &mut App) {
    let inbox = match &app.project.inbox {
        Some(inbox) => inbox,
        None => return,
    };
    let item = match inbox.items.get(app.inbox_cursor) {
        Some(item) => item,
        None => return,
    };

    let original_title = item.title.clone();
    app.edit_buffer = original_title.clone();
    app.edit_cursor = app.edit_buffer.len();
    app.edit_target = Some(EditTarget::ExistingInboxTitle {
        index: app.inbox_cursor,
        original_title,
    });
    app.edit_history = Some(EditHistory::new(&app.edit_buffer, app.edit_cursor, 0));
    app.mode = Mode::Edit;
}

/// Edit the tags of the selected inbox item.
pub(super) fn inbox_edit_tags(app: &mut App) {
    let inbox = match &app.project.inbox {
        Some(inbox) => inbox,
        None => return,
    };
    let item = match inbox.items.get(app.inbox_cursor) {
        Some(item) => item,
        None => return,
    };

    let original_tags: String = item
        .tags
        .iter()
        .map(|t| format!("#{}", t))
        .collect::<Vec<_>>()
        .join(" ");
    app.edit_buffer = if original_tags.is_empty() {
        String::new()
    } else {
        format!("{} ", original_tags)
    };
    app.edit_cursor = app.edit_buffer.len();
    app.edit_target = Some(EditTarget::ExistingInboxTags {
        index: app.inbox_cursor,
        original_tags: original_tags.clone(),
    });
    app.edit_history = Some(EditHistory::new(&app.edit_buffer, app.edit_cursor, 0));

    // Activate tag autocomplete
    let candidates = app.collect_all_tags();
    app.autocomplete = Some(AutocompleteState::new(AutocompleteKind::Tag, candidates));
    update_autocomplete_filter(app);

    app.mode = Mode::Edit;
}

/// Edit the note/body of the selected inbox item (multi-line inline editor).
/// When `cursor_at_end` is true, the cursor starts at the end of the note.
pub(super) fn inbox_edit_note(app: &mut App, cursor_at_end: bool) {
    let inbox = match &app.project.inbox {
        Some(inbox) => inbox,
        None => return,
    };
    let item = match inbox.items.get(app.inbox_cursor) {
        Some(item) => item,
        None => return,
    };

    let body_text = item.body.as_deref().unwrap_or("").to_string();
    let (cursor_line, cursor_col) = if cursor_at_end {
        let line_count = body_text.split('\n').count();
        let last_line_len = body_text.split('\n').next_back().map_or(0, |l| l.len());
        (line_count.saturating_sub(1), last_line_len)
    } else {
        (0, 0)
    };

    // Create a DetailState to reuse the multiline edit infrastructure
    let ds = DetailState {
        region: DetailRegion::Note,
        scroll_offset: 0,
        regions: vec![DetailRegion::Note],
        return_view: crate::tui::app::ReturnView::Track(0),
        editing: true,
        edit_buffer: body_text.clone(),
        edit_cursor_line: cursor_line,
        edit_cursor_col: cursor_col,
        edit_original: body_text.clone(),
        subtask_cursor: 0,
        flat_subtask_ids: Vec::new(),
        multiline_selection_anchor: None,
        note_h_scroll: 0,
        sticky_col: None,
        total_lines: 0,
        note_view_line: None,
        note_header_line: None,
        note_content_end: 0,
        regions_populated: vec![true],
    };

    app.detail_state = Some(ds);
    app.inbox_note_index = Some(app.inbox_cursor);
    app.inbox_note_editor_scroll = 0;
    app.edit_target = None; // multiline pattern: edit_target is None
    app.edit_history = Some(EditHistory::new(&body_text, cursor_col, cursor_line));
    app.mode = Mode::Edit;
}

/// Delete the selected inbox item (with confirmation).
pub(super) fn inbox_delete_item(app: &mut App) {
    let inbox = match &app.project.inbox {
        Some(inbox) => inbox,
        None => return,
    };
    if inbox.items.is_empty() || app.inbox_cursor >= inbox.items.len() {
        return;
    }

    let title = &inbox.items[app.inbox_cursor].title;
    let short_title = if unicode::display_width(title) > 30 {
        unicode::truncate_to_width(title, 30)
    } else {
        title.clone()
    };

    app.confirm_state = Some(crate::tui::app::ConfirmState {
        message: format!("Delete \"{}\"? (y/n)", short_title),
        action: crate::tui::app::ConfirmAction::DeleteInboxItem {
            index: app.inbox_cursor,
        },
    });
    app.mode = Mode::Confirm;
}

/// Enter MOVE mode for inbox items.
pub(super) fn inbox_enter_move_mode(app: &mut App) {
    let count = app.inbox_count();
    if count == 0 || app.inbox_cursor >= count {
        return;
    }

    app.move_state = Some(MoveState::InboxItem {
        original_index: app.inbox_cursor,
    });
    app.mode = Mode::Move;
}

/// Begin the triage flow for the selected inbox item.
pub(super) fn inbox_begin_triage(app: &mut App) {
    let count = app.inbox_count();
    if count == 0 || app.inbox_cursor >= count {
        return;
    }

    // Activate track selection autocomplete (show prefix from config)
    let active_tracks: Vec<String> = app
        .project
        .config
        .tracks
        .iter()
        .filter(|t| t.state == "active")
        .map(|t| {
            let prefix = app
                .project
                .config
                .ids
                .prefixes
                .get(&t.id)
                .map(|p| p.to_uppercase())
                .unwrap_or_else(|| t.id.to_uppercase());
            format!("{} ({})", t.name, prefix)
        })
        .collect();

    if active_tracks.is_empty() {
        app.status_message = Some("No active tracks to triage to".to_string());
        return;
    }

    app.edit_buffer.clear();
    app.edit_cursor = 0;
    app.autocomplete = Some(AutocompleteState::new(AutocompleteKind::Tag, active_tracks));
    if let Some(ac) = &mut app.autocomplete {
        ac.filter(""); // Show all
    }

    app.triage_state = Some(crate::tui::app::TriageState {
        source: TriageSource::Inbox {
            index: app.inbox_cursor,
        },
        step: crate::tui::app::TriageStep::SelectTrack,
        popup_anchor: None,
        position_cursor: 1, // default to Bottom
    });
    app.mode = Mode::Triage;
}

// ---------------------------------------------------------------------------
// Triage mode handler (Phase 7.3)

pub(super) fn handle_triage(app: &mut App, key: KeyEvent) {
    let step = match &app.triage_state {
        Some(ts) => ts.step.clone(),
        None => {
            app.mode = Mode::Navigate;
            return;
        }
    };

    match step {
        crate::tui::app::TriageStep::SelectTrack => handle_triage_select_track(app, key),
        crate::tui::app::TriageStep::SelectPosition { track_id } => {
            handle_triage_select_position(app, key, &track_id.clone())
        }
    }
}

pub(super) fn handle_triage_select_track(app: &mut App, key: KeyEvent) {
    match (key.modifiers, key.code) {
        // Cancel
        (_, KeyCode::Esc) => {
            app.mode = if app.selection.is_empty() {
                Mode::Navigate
            } else {
                Mode::Select
            };
            app.triage_state = None;
            app.autocomplete = None;
            app.edit_buffer.clear();
        }

        // Navigate autocomplete
        (KeyModifiers::NONE, KeyCode::Up) => {
            if let Some(ac) = &mut app.autocomplete {
                ac.move_up();
            }
        }
        (KeyModifiers::NONE, KeyCode::Down) => {
            if let Some(ac) = &mut app.autocomplete {
                ac.move_down();
            }
        }

        // Select track
        (_, KeyCode::Enter) => {
            let selected = app
                .autocomplete
                .as_ref()
                .and_then(|ac| ac.selected_entry().map(|s| s.to_string()));
            if let Some(entry) = selected {
                // Extract prefix from "Track Name (PREFIX)" and find the matching track
                let prefix_str = entry
                    .rsplit('(')
                    .next()
                    .and_then(|s| s.strip_suffix(')'))
                    .unwrap_or(&entry);

                // Find track by prefix match (or fall back to treating it as a track ID)
                let track_id = app
                    .project
                    .config
                    .ids
                    .prefixes
                    .iter()
                    .find(|(_, p)| p.eq_ignore_ascii_case(prefix_str))
                    .map(|(tid, _)| tid.clone())
                    .unwrap_or_else(|| prefix_str.to_lowercase());

                // Verify track exists
                let valid = app.project.config.tracks.iter().any(|t| t.id == track_id);
                if valid {
                    // Capture anchor from autocomplete before clearing it
                    let anchor = app.autocomplete_anchor;
                    app.autocomplete = None;
                    app.edit_buffer.clear();
                    if let Some(ts) = &mut app.triage_state {
                        ts.popup_anchor = anchor;
                        ts.step = crate::tui::app::TriageStep::SelectPosition { track_id };
                    }
                }
            }
        }

        // Filter by typing
        (KeyModifiers::NONE | KeyModifiers::SHIFT, KeyCode::Char(c)) => {
            app.edit_buffer.push(c);
            app.edit_cursor = app.edit_buffer.len();
            if let Some(ac) = &mut app.autocomplete {
                ac.filter(&app.edit_buffer);
            }
        }

        // Backspace
        (_, KeyCode::Backspace) => {
            app.edit_buffer.pop();
            app.edit_cursor = app.edit_buffer.len();
            if let Some(ac) = &mut app.autocomplete {
                ac.filter(&app.edit_buffer);
            }
        }

        _ => {}
    }
}

pub(super) fn handle_triage_select_position(app: &mut App, key: KeyEvent, track_id: &str) {
    match (key.modifiers, key.code) {
        // Cancel
        (_, KeyCode::Esc) => {
            app.mode = if app.selection.is_empty() {
                Mode::Navigate
            } else {
                Mode::Select
            };
            app.triage_state = None;
            app.autocomplete = None;
            app.edit_buffer.clear();
        }

        // Navigate between options: 0=Top, 1=Bottom, 2=Cancel
        (KeyModifiers::NONE, KeyCode::Up | KeyCode::Char('k')) => {
            if let Some(ts) = &mut app.triage_state {
                ts.position_cursor = ts.position_cursor.saturating_sub(1);
            }
        }
        (KeyModifiers::NONE, KeyCode::Down | KeyCode::Char('j')) => {
            if let Some(ts) = &mut app.triage_state {
                ts.position_cursor = (ts.position_cursor + 1).min(2);
            }
        }

        // Confirm selection
        (_, KeyCode::Enter) => {
            let cursor = app
                .triage_state
                .as_ref()
                .map(|ts| ts.position_cursor)
                .unwrap_or(1);
            match cursor {
                0 => dispatch_triage_or_move(app, track_id, InsertPosition::Top),
                1 => dispatch_triage_or_move(app, track_id, InsertPosition::Bottom),
                _ => {
                    // Cancel
                    app.mode = Mode::Navigate;
                    app.triage_state = None;
                    app.autocomplete = None;
                    app.edit_buffer.clear();
                }
            }
        }

        // Direct shortcuts still work
        (KeyModifiers::NONE, KeyCode::Char('t')) => {
            dispatch_triage_or_move(app, track_id, InsertPosition::Top);
        }
        (KeyModifiers::NONE, KeyCode::Char('b')) => {
            dispatch_triage_or_move(app, track_id, InsertPosition::Bottom);
        }

        _ => {}
    }
}

/// Dispatch to execute_triage or execute_cross_track_move based on the triage source
pub(super) fn dispatch_triage_or_move(app: &mut App, track_id: &str, position: InsertPosition) {
    let source = match &app.triage_state {
        Some(ts) => ts.source.clone(),
        None => return,
    };
    match source {
        TriageSource::Inbox { .. } => execute_triage(app, track_id, position),
        TriageSource::CrossTrackMove { .. } => execute_cross_track_move(app, track_id, position),
        TriageSource::BulkCrossTrackMove { .. } => {
            execute_bulk_cross_track_move(app, track_id, position)
        }
    }
}

pub(super) fn execute_triage(app: &mut App, track_id: &str, position: InsertPosition) {
    let inbox_index = match &app.triage_state {
        Some(ts) => match &ts.source {
            TriageSource::Inbox { index } => *index,
            _ => return,
        },
        None => return,
    };

    // Get the item before triaging (for undo)
    let inbox_item = match &app.project.inbox {
        Some(inbox) => match inbox.items.get(inbox_index) {
            Some(item) => item.clone(),
            None => return,
        },
        None => return,
    };

    let prefix = app.track_prefix(track_id).unwrap_or("").to_string();

    let inbox = match &mut app.project.inbox {
        Some(inbox) => inbox,
        None => return,
    };
    let track = match app.project.tracks.iter_mut().find(|(id, _)| id == track_id) {
        Some((_, track)) => track,
        None => return,
    };

    let task_id = match crate::ops::inbox_ops::triage(inbox, inbox_index, track, position, &prefix)
    {
        Ok(id) => id,
        Err(_) => return,
    };

    // Push undo operation
    app.undo_stack.push(Operation::InboxTriage {
        inbox_index,
        item: inbox_item,
        track_id: track_id.to_string(),
        task_id,
    });

    // Save track first (new data), then inbox (deletion)
    app.save_track_logged(track_id);
    app.save_inbox_logged();

    // Advance cursor (or clamp to last item)
    let count = app.inbox_count();
    if count == 0 {
        app.inbox_cursor = 0;
    } else {
        app.inbox_cursor = app.inbox_cursor.min(count - 1);
    }

    // Return to navigate mode
    app.mode = Mode::Navigate;
    app.triage_state = None;
    app.autocomplete = None;
    app.edit_buffer.clear();

    let track_name = app.track_name(track_id).to_string();
    app.status_message = Some(format!("Triaged to {}", track_name));
}

// ---------------------------------------------------------------------------
// Cross-track move (M key)

/// Begin cross-track move: enter triage-style track selection for moving a task
pub(super) fn begin_cross_track_move(app: &mut App) {
    // Determine source task
    let (source_track_id, task_id, section) = match &app.view {
        View::Track(_) => match app.cursor_task_id() {
            Some(info) => info,
            None => return,
        },
        View::Detail { track_id, task_id } => {
            // Find task to determine its section
            let section = if let Some(track) = App::find_track_in_project(&app.project, track_id) {
                if task_ops::is_top_level_in_section(track, task_id, SectionKind::Backlog) {
                    SectionKind::Backlog
                } else if task_ops::find_task_in_track(track, task_id).is_some() {
                    // Subtask in backlog — will be promoted
                    SectionKind::Backlog
                } else {
                    return;
                }
            } else {
                return;
            };
            (track_id.clone(), task_id.clone(), section)
        }
        _ => return,
    };

    // Only allow moving tasks from Backlog
    if section != SectionKind::Backlog {
        app.status_message = Some("Can only move backlog tasks".to_string());
        return;
    }

    // Build candidate tracks: all non-archived tracks except current (show prefix)
    let candidates: Vec<String> = app
        .project
        .config
        .tracks
        .iter()
        .filter(|t| t.state != "archived" && t.id != source_track_id)
        .map(|t| {
            let prefix = app
                .project
                .config
                .ids
                .prefixes
                .get(&t.id)
                .map(|p| p.to_uppercase())
                .unwrap_or_else(|| t.id.to_uppercase());
            format!("{} ({})", t.name, prefix)
        })
        .collect();

    if candidates.is_empty() {
        app.status_message = Some("No other tracks to move to".to_string());
        return;
    }

    app.edit_buffer.clear();
    app.edit_cursor = 0;
    app.autocomplete = Some(AutocompleteState::new(AutocompleteKind::Tag, candidates));
    if let Some(ac) = &mut app.autocomplete {
        ac.filter(""); // Show all
    }

    app.triage_state = Some(crate::tui::app::TriageState {
        source: TriageSource::CrossTrackMove {
            source_track_id,
            task_id,
        },
        step: crate::tui::app::TriageStep::SelectTrack,
        popup_anchor: None,
        position_cursor: 1, // default to Bottom
    });
    app.mode = Mode::Triage;
}

/// Execute the cross-track move after track and position are selected
pub(super) fn execute_cross_track_move(
    app: &mut App,
    target_track_id: &str,
    position: InsertPosition,
) {
    let (source_track_id, task_id) = match &app.triage_state {
        Some(ts) => match &ts.source {
            TriageSource::CrossTrackMove {
                source_track_id,
                task_id,
            } => (source_track_id.clone(), task_id.clone()),
            _ => return,
        },
        None => return,
    };

    let target_prefix = app.track_prefix(target_track_id).unwrap_or("").to_string();

    // Determine if task is a subtask (has a parent)
    let is_subtask = task_id.contains('.');
    let source_parent_id = if is_subtask {
        // Extract parent ID: everything before the last dot
        task_id
            .rsplit_once('.')
            .map(|(parent, _)| parent.to_string())
    } else {
        None
    };

    // Find old depth
    let old_depth = {
        let track = match App::find_track_in_project(&app.project, &source_track_id) {
            Some(t) => t,
            None => return,
        };
        task_ops::find_task_in_track(track, &task_id)
            .map(|t| t.depth)
            .unwrap_or(0)
    };

    // Remove task from source
    let (mut task, source_index) = if let Some(ref parent_id) = source_parent_id {
        // Subtask: remove from parent's subtask list
        let source_track = match app.find_track_mut(&source_track_id) {
            Some(t) => t,
            None => return,
        };
        let parent = match task_ops::find_task_mut_in_track(source_track, parent_id) {
            Some(p) => p,
            None => return,
        };
        let idx = match parent
            .subtasks
            .iter()
            .position(|t| t.id.as_deref() == Some(&task_id))
        {
            Some(i) => i,
            None => return,
        };
        let task = parent.subtasks.remove(idx);
        parent.mark_dirty();
        (task, idx)
    } else {
        // Top-level: remove from source backlog
        let source_track = match app.find_track_mut(&source_track_id) {
            Some(t) => t,
            None => return,
        };
        let source_tasks = match source_track.section_tasks_mut(SectionKind::Backlog) {
            Some(t) => t,
            None => return,
        };
        let idx = match source_tasks
            .iter()
            .position(|t| t.id.as_deref() == Some(&task_id))
        {
            Some(i) => i,
            None => return,
        };
        let task = source_tasks.remove(idx);
        (task, idx)
    };

    // Compute new ID
    let target_track = match App::find_track_in_project(&app.project, target_track_id) {
        Some(t) => t,
        None => return,
    };
    let new_num = task_ops::next_id_number(target_track, &target_prefix);
    let new_id = format!("{}-{:03}", target_prefix, new_num);
    let old_id = task_id.clone();

    // Set new ID and depth
    task.id = Some(new_id.clone());
    task.depth = 0;
    task.mark_dirty();
    task_ops::renumber_subtasks(&mut task, &new_id);

    // Insert into target backlog
    let target_track = match app.find_track_mut(target_track_id) {
        Some(t) => t,
        None => return,
    };
    let target_tasks = match target_track.section_tasks_mut(SectionKind::Backlog) {
        Some(t) => t,
        None => return,
    };
    let target_index = match &position {
        InsertPosition::Top => {
            target_tasks.insert(0, task);
            0
        }
        InsertPosition::Bottom => {
            let idx = target_tasks.len();
            target_tasks.push(task);
            idx
        }
        InsertPosition::After(after_id) => {
            let after_idx = target_tasks
                .iter()
                .position(|t| t.id.as_deref() == Some(after_id.as_str()))
                .unwrap_or(target_tasks.len().saturating_sub(1));
            target_tasks.insert(after_idx + 1, task);
            after_idx + 1
        }
    };

    // Update dep references across all tracks
    task_ops::update_dep_references(&mut app.project.tracks, &old_id, &new_id);

    // Push undo operation
    app.undo_stack.push(Operation::CrossTrackMove {
        source_track_id: source_track_id.clone(),
        target_track_id: target_track_id.to_string(),
        task_id_old: old_id.clone(),
        task_id_new: new_id.clone(),
        source_index,
        target_index,
        source_parent_id,
        old_depth,
    });

    // Save target first (new data), then source (deletion)
    app.save_track_logged(target_track_id);
    app.save_track_logged(&source_track_id);

    // Cursor management
    let was_detail = matches!(app.view, View::Detail { .. });
    if was_detail {
        // Close detail view, return to track view
        app.close_detail_fully();
        if let Some(idx) = app
            .active_track_ids
            .iter()
            .position(|id| id == &source_track_id)
        {
            app.view = View::Track(idx);
        }
    } else {
        // Advance cursor in track view (or clamp to last)
        if let Some(track_id) = app.current_track_id().map(|s| s.to_string()) {
            let flat_items = app.build_flat_items(&track_id);
            let state = app.get_track_state(&track_id);
            if state.cursor >= flat_items.len() && !flat_items.is_empty() {
                state.cursor = flat_items.len() - 1;
            }
        }
    }

    // Status message
    let target_name = app.track_name(target_track_id).to_string();
    app.status_message = Some(format!(
        "Moved to {} ({} → {})",
        target_name, old_id, new_id
    ));

    // Clean up triage state
    app.mode = Mode::Navigate;
    app.triage_state = None;
    app.autocomplete = None;
    app.edit_buffer.clear();
}

/// Execute bulk cross-track move: move all selected tasks to the target track
pub(super) fn execute_bulk_cross_track_move(
    app: &mut App,
    target_track_id: &str,
    position: InsertPosition,
) {
    let source_track_id = match &app.triage_state {
        Some(ts) => match &ts.source {
            TriageSource::BulkCrossTrackMove { source_track_id } => source_track_id.clone(),
            _ => return,
        },
        None => return,
    };

    let target_prefix = app.track_prefix(target_track_id).unwrap_or("").to_string();

    // Collect selected task IDs in backlog order
    let selected_ids: Vec<String> = {
        let track = match App::find_track_in_project(&app.project, &source_track_id) {
            Some(t) => t,
            None => return,
        };
        let backlog = track.backlog();
        backlog
            .iter()
            .filter_map(|t| {
                t.id.as_ref()
                    .filter(|id| app.selection.contains(*id))
                    .cloned()
            })
            .collect()
    };

    if selected_ids.is_empty() {
        app.triage_state = None;
        app.mode = if app.selection.is_empty() {
            Mode::Navigate
        } else {
            Mode::Select
        };
        return;
    }

    let mut ops: Vec<Operation> = Vec::new();
    let mut new_ids: Vec<String> = Vec::new();

    for task_id in &selected_ids {
        // Get next ID number (must re-query each time since we're inserting)
        let target_track = match App::find_track_in_project(&app.project, target_track_id) {
            Some(t) => t,
            None => continue,
        };
        let new_num = task_ops::next_id_number(target_track, &target_prefix);
        let new_id = format!("{}-{:03}", target_prefix, new_num);

        // Remove from source
        let source_track = match app.find_track_mut(&source_track_id) {
            Some(t) => t,
            None => continue,
        };
        let source_tasks = match source_track.section_tasks_mut(SectionKind::Backlog) {
            Some(t) => t,
            None => continue,
        };
        let idx = match source_tasks
            .iter()
            .position(|t| t.id.as_deref() == Some(task_id))
        {
            Some(i) => i,
            None => continue,
        };
        let mut task = source_tasks.remove(idx);
        let source_index = idx;

        // Set new ID and depth
        let old_id = task_id.clone();
        task.id = Some(new_id.clone());
        task.depth = 0;
        task.mark_dirty();
        task_ops::renumber_subtasks(&mut task, &new_id);

        // Insert into target backlog
        let target_track = match app.find_track_mut(target_track_id) {
            Some(t) => t,
            None => continue,
        };
        let target_tasks = match target_track.section_tasks_mut(SectionKind::Backlog) {
            Some(t) => t,
            None => continue,
        };
        let target_index = match &position {
            InsertPosition::Top => {
                // Insert at the front, but after previously inserted tasks
                let insert_at = ops.len().min(target_tasks.len());
                target_tasks.insert(insert_at, task);
                insert_at
            }
            InsertPosition::Bottom => {
                let idx = target_tasks.len();
                target_tasks.push(task);
                idx
            }
            InsertPosition::After(after_id) => {
                let after_idx = target_tasks
                    .iter()
                    .position(|t| t.id.as_deref() == Some(after_id.as_str()))
                    .unwrap_or(target_tasks.len().saturating_sub(1));
                target_tasks.insert(after_idx + 1, task);
                after_idx + 1
            }
        };

        // Update dep references across all tracks
        task_ops::update_dep_references(&mut app.project.tracks, &old_id, &new_id);

        ops.push(Operation::CrossTrackMove {
            source_track_id: source_track_id.clone(),
            target_track_id: target_track_id.to_string(),
            task_id_old: old_id,
            task_id_new: new_id.clone(),
            source_index,
            target_index,
            source_parent_id: None,
            old_depth: 0,
        });

        new_ids.push(new_id);
    }

    if !ops.is_empty() {
        // Save target first (new data), then source (deletion)
        app.save_track_logged(target_track_id);
        app.save_track_logged(&source_track_id);

        let count = ops.len();
        app.undo_stack.push(Operation::Bulk(ops));

        // Update selection to use new IDs
        for old_id in &selected_ids {
            app.selection.remove(old_id);
        }
        for new_id in &new_ids {
            app.selection.insert(new_id.clone());
        }

        // Adjust cursor
        if let Some(track_id) = app.current_track_id().map(|s| s.to_string()) {
            let flat_items = app.build_flat_items(&track_id);
            let state = app.get_track_state(&track_id);
            if state.cursor >= flat_items.len() && !flat_items.is_empty() {
                state.cursor = flat_items.len() - 1;
            }
        }

        let target_name = app.track_name(target_track_id).to_string();
        app.status_message = Some(format!("{} tasks moved to {}", count, target_name));
    }

    // Clean up triage state
    app.mode = if app.selection.is_empty() {
        Mode::Navigate
    } else {
        Mode::Select
    };
    app.triage_state = None;
    app.autocomplete = None;
    app.edit_buffer.clear();
}

// ---------------------------------------------------------------------------
// Confirm mode handler