cfait 1.1.6

Powerful, fast and elegant task / TODO manager. (GUI & TUI, CalDAV & local)
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
// SPDX-License-Identifier: GPL-3.0-or-later
// File: ./src/model/extractor.rs
use std::collections::HashMap;
use uuid::Uuid;

#[derive(Debug)]
pub struct ExtractedTask {
    pub uid: String,
    pub parsed_existing_uid: Option<String>, // Found via <!-- uid:... -->
    pub parent_uid: Option<String>,
    pub dependencies: Vec<String>,
    pub raw_text: String,
    pub description: String,
    pub status: crate::model::TaskStatus,
    pub percent_complete: Option<u8>,
    pub is_note: bool,
}

fn parse_checkbox(s: &str) -> Option<(crate::model::TaskStatus, Option<u8>, &str)> {
    if s.len() < 4 || !s.starts_with('[') {
        return None;
    }
    let mut chars = s.chars();
    chars.next(); // '['
    let inner = chars.next()?;
    if chars.next()? != ']' || chars.next()? != ' ' {
        return None;
    }
    let rest = chars.as_str();
    match inner {
        ' ' => Some((crate::model::TaskStatus::NeedsAction, None, rest)),
        'x' | 'X' | '*' => Some((crate::model::TaskStatus::Completed, Some(100), rest)),
        '/' => Some((crate::model::TaskStatus::NeedsAction, Some(50), rest)),
        '>' | 'â–¶' => Some((crate::model::TaskStatus::InProcess, None, rest)),
        '<' => Some((crate::model::TaskStatus::NeedsAction, Some(50), rest)),
        '-' | '~' => Some((crate::model::TaskStatus::Cancelled, None, rest)),
        _ => None,
    }
}

fn extract_uid_tag(line: &str) -> (String, Option<String>) {
    if let Some(idx) = line.rfind("<!-- uid:")
        && let Some(end_idx) = line[idx..].find("-->")
    {
        let uid = line[idx + 9..idx + end_idx].trim().to_string();
        let clean_line = line[..idx].trim().to_string();
        return (clean_line, Some(uid));
    }
    (line.trim_end().to_string(), None)
}

fn compute_task_lines(input: &str, is_journal: bool) -> Vec<bool> {
    let lines_vec: Vec<&str> = input.lines().collect();
    let mut is_task_line = vec![false; lines_vec.len()];
    let mut indents = vec![0; lines_vec.len()];
    let mut is_list = vec![false; lines_vec.len()];

    for (i, line) in lines_vec.iter().enumerate() {
        let mut indent = 0;
        let mut byte_offset = 0;
        for c in line.chars() {
            if c == ' ' {
                indent += 1;
                byte_offset += c.len_utf8();
            } else if c == '\t' {
                indent += 4;
                byte_offset += c.len_utf8();
            } else {
                break;
            }
        }
        indents[i] = indent;
        let rest = &line[byte_offset..];

        let mut list_marker = false;
        let mut after_marker = rest;
        let mut is_header = false;

        if !is_journal
            && rest.starts_with('#')
            && rest
                .find(' ')
                .is_some_and(|idx| idx <= 6 && rest[..idx].chars().all(|c| c == '#'))
        {
            is_header = true;
            let depth = rest.find(' ').unwrap();
            after_marker = &rest[depth + 1..];
        } else if rest.starts_with("- ") || rest.starts_with("* ") || rest.starts_with("+ ") {
            list_marker = true;
            after_marker = &rest[2..];
        } else {
            let mut digit_bytes = 0;
            for c in rest.chars() {
                if c.is_ascii_digit() {
                    digit_bytes += c.len_utf8();
                } else {
                    break;
                }
            }
            if digit_bytes > 0 && rest[digit_bytes..].starts_with(". ") {
                list_marker = true;
                after_marker = &rest[digit_bytes + 2..];
            }
        }

        if is_header {
            is_task_line[i] = true;
        } else {
            is_list[i] = list_marker;

            if list_marker {
                let has_checkbox = parse_checkbox(after_marker).is_some();
                let has_uid = after_marker.contains("<!-- uid:");
                let has_is_note = after_marker.contains("is:note")
                    || after_marker.contains("is:page")
                    || after_marker.contains("is:journal");
                let has_wiki_link = after_marker.trim().starts_with("[[");

                if has_checkbox
                    || has_uid
                    || has_is_note
                    || has_wiki_link
                    || (!is_journal && !has_checkbox)
                {
                    is_task_line[i] = true;
                }
            }
        }
    }

    // Phase 2: Backwards propagate `is_task` to implicit structural parents
    for i in (0..lines_vec.len()).rev() {
        if is_list[i] && !is_task_line[i] {
            let curr_indent = indents[i];
            for j in (i + 1)..lines_vec.len() {
                if !lines_vec[j].trim().is_empty() {
                    // ignore empty lines for indent checks
                    if indents[j] <= curr_indent {
                        break; // Hit a sibling or outdent, so no children
                    }
                    if is_task_line[j] {
                        is_task_line[i] = true;
                        break;
                    }
                }
            }
        }
    }

    is_task_line
}

pub fn extract_list_prefix(line: &str) -> String {
    let mut prefix = String::new();
    let mut byte_offset = 0;
    let chars = line.chars();

    // Extract leading whitespace
    for c in chars {
        if c == ' ' || c == '\t' {
            prefix.push(c);
            byte_offset += c.len_utf8();
        } else {
            break;
        }
    }

    let rest = &line[byte_offset..];
    if rest.starts_with("- [ ] ")
        || rest.starts_with("- [x] ")
        || rest.starts_with("- [X] ")
        || rest.starts_with("- [/] ")
        || rest.starts_with("- [-] ")
        || rest.starts_with("- [<] ")
        || rest.starts_with("- [>] ")
    {
        prefix.push_str("- [ ] ");
    } else if rest.starts_with("* [ ] ")
        || rest.starts_with("* [x] ")
        || rest.starts_with("* [X] ")
        || rest.starts_with("* [/] ")
        || rest.starts_with("* [-] ")
        || rest.starts_with("* [<] ")
        || rest.starts_with("* [>] ")
    {
        prefix.push_str("* [ ] ");
    } else if rest.starts_with("- ") {
        prefix.push_str("- ");
    } else if rest.starts_with("* ") {
        prefix.push_str("* ");
    } else {
        let mut digit_bytes = 0;
        for c in rest.chars() {
            if c.is_ascii_digit() {
                digit_bytes += c.len_utf8();
            } else {
                break;
            }
        }
        if digit_bytes > 0 {
            let after = &rest[digit_bytes..];
            if after.starts_with(". [ ] ")
                || after.starts_with(". [x] ")
                || after.starts_with(". [X] ")
                || after.starts_with(". [/] ")
                || after.starts_with(". [-] ")
                || after.starts_with(". [<] ")
                || after.starts_with(". [>] ")
            {
                let num_str = &rest[..digit_bytes];
                let num: usize = num_str.parse().unwrap_or(1);
                prefix.push_str(&format!("{}. [ ] ", num + 1));
            } else if after.starts_with(". ") {
                let num_str = &rest[..digit_bytes];
                let num: usize = num_str.parse().unwrap_or(1);
                prefix.push_str(&format!("{}. ", num + 1));
            }
        }
    }
    prefix
}

pub fn has_extractable_subtasks(input: &str, is_journal: bool) -> bool {
    let is_task = compute_task_lines(input, is_journal);
    is_task.into_iter().any(|b| b)
}

#[derive(PartialEq, Clone, Copy, Debug)]
enum StackItemKind {
    Heading(usize), // level 1, 2, 3...
    List(usize),    // indent in spaces
}

/// Takes a raw markdown string.
/// Returns (Cleaned Root Description, List of Extracted Subtasks).
pub fn extract_markdown_tasks(input: &str, is_journal: bool) -> (String, Vec<ExtractedTask>) {
    let mut cleaned_root_desc = String::new();
    let mut extracted: Vec<ExtractedTask> = Vec::new();

    let lines_vec: Vec<&str> = input.lines().collect();
    let is_task_line = compute_task_lines(input, is_journal);

    let mut indent_stack: Vec<(StackItemKind, String, usize)> = Vec::new();
    let mut item_kind_at_indent: HashMap<usize, usize> = HashMap::new(); // indent -> block_id
    let mut next_block_id = 0;
    let mut numbered_tasks: Vec<(usize, usize, usize)> = Vec::new(); // (block_id, parsed_num, extracted_idx)

    let mut active_task_idx: Option<usize> = None;

    for (line_idx, line) in lines_vec.into_iter().enumerate() {
        let mut indent = 0;
        let mut byte_offset = 0;
        for c in line.chars() {
            if c == ' ' {
                indent += 1;
                byte_offset += c.len_utf8();
            } else if c == '\t' {
                indent += 4;
                byte_offset += c.len_utf8();
            } else {
                break;
            }
        }

        let rest = &line[byte_offset..];

        if rest.is_empty() {
            if let Some(idx) = active_task_idx {
                extracted[idx].description.push('\n');
            } else {
                cleaned_root_desc.push('\n');
            }
            continue;
        }

        item_kind_at_indent.retain(|&k, _| k <= indent);

        if is_task_line[line_idx] {
            let mut is_numbered = false;
            let mut parsed_num = 0;
            let mut parsed_status = crate::model::TaskStatus::NeedsAction;
            let mut parsed_pc = None;
            let mut is_note = true;
            let mut raw_text = rest;
            let mut is_header = false;
            let mut header_depth = 0;

            if !is_journal {
                for depth in (1..=6).rev() {
                    let prefix = format!("{} ", "#".repeat(depth));
                    if let Some(stripped) = rest.strip_prefix(&prefix) {
                        is_header = true;
                        header_depth = depth;
                        raw_text = stripped;
                        break;
                    }
                }
            }

            if is_header {
                if let Some((status, pc, r)) = parse_checkbox(raw_text) {
                    is_note = false;
                    parsed_status = status;
                    parsed_pc = pc;
                    raw_text = r;
                }
            } else if rest.starts_with("- ") || rest.starts_with("* ") || rest.starts_with("+ ") {
                let after_marker = &rest[2..];
                if let Some((status, pc, r)) = parse_checkbox(after_marker) {
                    is_note = false;
                    parsed_status = status;
                    parsed_pc = pc;
                    raw_text = r;
                } else {
                    raw_text = after_marker;
                }
            } else {
                let mut digit_bytes = 0;
                for c in rest.chars() {
                    if c.is_ascii_digit() {
                        digit_bytes += c.len_utf8();
                    } else {
                        break;
                    }
                }
                if digit_bytes > 0 && rest[digit_bytes..].starts_with(". ") {
                    let after_marker = &rest[digit_bytes + 2..];
                    if let Some((status, pc, r)) = parse_checkbox(after_marker) {
                        is_numbered = true;
                        is_note = false;
                        parsed_num = rest[..digit_bytes].parse::<usize>().unwrap_or(1);
                        parsed_status = status;
                        parsed_pc = pc;
                        raw_text = r;
                    } else {
                        is_numbered = true;
                        parsed_num = rest[..digit_bytes].parse::<usize>().unwrap_or(1);
                        raw_text = after_marker;
                    }
                }
            }

            let (clean_text, parsed_uid) = extract_uid_tag(raw_text);
            let uid = parsed_uid
                .clone()
                .unwrap_or_else(|| Uuid::new_v4().to_string());

            let current_kind = if is_header {
                StackItemKind::Heading(header_depth)
            } else {
                StackItemKind::List(indent)
            };

            while let Some(&(kind, _, _)) = indent_stack.last() {
                match current_kind {
                    StackItemKind::Heading(curr_lvl) => match kind {
                        StackItemKind::Heading(stack_lvl) => {
                            if stack_lvl >= curr_lvl {
                                indent_stack.pop();
                            } else {
                                break;
                            }
                        }
                        StackItemKind::List(_) => {
                            indent_stack.pop();
                        }
                    },
                    StackItemKind::List(curr_indent) => match kind {
                        StackItemKind::Heading(_) => {
                            break;
                        }
                        StackItemKind::List(stack_indent) => {
                            if stack_indent >= curr_indent {
                                indent_stack.pop();
                            } else {
                                break;
                            }
                        }
                    },
                }
            }

            let parent_uid = indent_stack.last().map(|(_, id, _)| id.clone());
            let new_idx = extracted.len();

            if is_numbered {
                let block_id = match item_kind_at_indent.get(&indent) {
                    Some(&b) => b,
                    _ => {
                        let b = next_block_id;
                        next_block_id += 1;
                        b
                    }
                };
                item_kind_at_indent.insert(indent, block_id);
                numbered_tasks.push((block_id, parsed_num, new_idx));
            } else {
                // Remove entry to break numbering blocks
                item_kind_at_indent.remove(&indent);
            }

            indent_stack.push((current_kind, uid.clone(), new_idx));

            extracted.push(ExtractedTask {
                uid,
                parsed_existing_uid: parsed_uid,
                parent_uid,
                dependencies: Vec::new(),
                raw_text: clean_text,
                description: String::new(),
                status: parsed_status,
                percent_complete: parsed_pc,
                is_note,
            });
            active_task_idx = Some(new_idx);
        } else {
            // Not a task line -> treat as plain text.
            item_kind_at_indent.remove(&indent);

            while let Some(&(stack_indent, _, _)) = indent_stack.last() {
                if let StackItemKind::List(list_indent) = stack_indent {
                    if list_indent >= indent {
                        indent_stack.pop();
                    } else {
                        break;
                    }
                } else {
                    break; // Headings don't get popped by text indentation
                }
            }

            let target_idx = indent_stack.last().map(|&(_, _, idx)| idx);

            let strip_amount = if let Some(&(kind, _, _)) = indent_stack.last() {
                match kind {
                    StackItemKind::Heading(_) => 0,
                    StackItemKind::List(list_indent) => list_indent + 2,
                }
            } else {
                0
            };

            let mut bytes_to_strip = 0;
            let mut spaces_seen = 0;
            for c in line.chars() {
                if spaces_seen >= strip_amount {
                    break;
                }
                if c == ' ' {
                    spaces_seen += 1;
                    bytes_to_strip += c.len_utf8();
                } else if c == '\t' {
                    spaces_seen += 4;
                    bytes_to_strip += c.len_utf8();
                } else {
                    break;
                }
            }
            let line_content = &line[bytes_to_strip..];

            if let Some(idx) = target_idx {
                if !extracted[idx].description.is_empty()
                    && !extracted[idx].description.ends_with('\n')
                {
                    extracted[idx].description.push('\n');
                }
                extracted[idx].description.push_str(line_content);
                extracted[idx].description.push('\n');
                active_task_idx = Some(idx);
            } else {
                if !cleaned_root_desc.is_empty() && !cleaned_root_desc.ends_with('\n') {
                    cleaned_root_desc.push('\n');
                }
                cleaned_root_desc.push_str(line_content);
                cleaned_root_desc.push('\n');
                active_task_idx = None;
            }
        }
    }

    // Second pass: resolve out-of-order numbered dependencies
    let mut blocks: HashMap<usize, Vec<(usize, usize)>> = HashMap::new();
    for (b_id, p_num, e_idx) in numbered_tasks {
        blocks.entry(b_id).or_default().push((p_num, e_idx));
    }

    for (_, list) in blocks {
        let mut uids_by_num: HashMap<usize, Vec<String>> = HashMap::new();
        for &(num, e_idx) in &list {
            uids_by_num
                .entry(num)
                .or_default()
                .push(extracted[e_idx].uid.clone());
        }

        let mut unique_nums: Vec<usize> = uids_by_num.keys().copied().collect();
        unique_nums.sort_unstable();

        for (num, e_idx) in list {
            let prev_num = unique_nums.iter().rev().find(|&&n| n < num).copied();
            if let Some(p_num) = prev_num
                && let Some(deps) = uids_by_num.get(&p_num)
            {
                extracted[e_idx].dependencies.extend(deps.iter().cloned());
            }
        }
    }

    // Clean up trailing newlines
    let cleaned_root_desc = cleaned_root_desc.trim_end().to_string();
    for task in &mut extracted {
        task.description = task.description.trim_end().to_string();
    }

    (cleaned_root_desc, extracted)
}

pub fn serialize_task_tree(
    store: &crate::store::TaskStore,
    root_uid: &str,
    calendars: &[crate::model::CalendarListEntry],
    is_journal: bool,
) -> String {
    let mut out = String::new();
    let root = if let Some(r) = store.get_task_ref(root_uid) {
        r
    } else {
        return out;
    };

    let mut children_map: std::collections::HashMap<String, Vec<&crate::model::Task>> =
        std::collections::HashMap::new();
    for map in store.calendars.values() {
        for t in map.values() {
            if let Some(p) = &t.parent_uid {
                // Skip trashed/recovered tasks so they don't appear as ghost subtasks,
                // unless we are explicitly serializing a tree that is ALREADY in the trash.
                if (t.calendar_href == crate::storage::LOCAL_TRASH_HREF
                    || t.calendar_href == "local://recovery")
                    && t.calendar_href != root.calendar_href
                {
                    continue;
                }
                children_map.entry(p.clone()).or_default().push(t);
            }
        }
    }

    // Topologically sort children so that blocked tasks inherently follow their dependencies.
    // This perfectly preserves sequence ordering (1., 2., 3.) when re-extracting markdown.
    // We pre-sort deterministically (by created date, then summary) to ensure stable
    // git diffs and consistent publication output, rather than volatile priority/status sorting.
    for list in children_map.values_mut() {
        list.sort_by_cached_key(|t| (t.created_date(), t.summary.clone(), t.uid.clone()));

        if list.len() <= 1 {
            continue;
        }

        let mut uids_in_list = std::collections::HashSet::new();
        for t in list.iter() {
            uids_in_list.insert(t.uid.as_str());
        }

        let mut needs_sort = false;
        for t in list.iter() {
            for dep in &t.dependencies {
                if uids_in_list.contains(dep.as_str()) {
                    needs_sort = true;
                    break;
                }
            }
            if needs_sort {
                break;
            }
        }

        if !needs_sort {
            continue;
        }

        let n = list.len();
        let mut uid_to_idx: HashMap<&str, usize> = HashMap::with_capacity(n);
        for (i, t) in list.iter().enumerate() {
            uid_to_idx.insert(t.uid.as_str(), i);
        }

        let mut in_degree = vec![0usize; n];
        let mut graph = vec![Vec::new(); n];

        for (i, t) in list.iter().enumerate() {
            for dep in &t.dependencies {
                if let Some(&dep_idx) = uid_to_idx.get(dep.as_str()) {
                    in_degree[i] += 1;
                    graph[dep_idx].push(i);
                }
            }
        }

        let mut result = Vec::with_capacity(n);

        // Kahn's algorithm using a min-heap keyed by index. The list is already
        // deterministically sorted (created date, then summary); always emitting the
        // smallest available index preserves that ordering (and stable git diffs)
        // while reducing the sort from O(V^2) to O((V+E) log V).
        let mut zero_in_degree: std::collections::BinaryHeap<std::cmp::Reverse<usize>> = (0..n)
            .filter(|&i| in_degree[i] == 0)
            .map(std::cmp::Reverse)
            .collect();

        while let Some(std::cmp::Reverse(i)) = zero_in_degree.pop() {
            result.push(i);
            for &dependent in &graph[i] {
                in_degree[dependent] -= 1;
                if in_degree[dependent] == 0 {
                    zero_in_degree.push(std::cmp::Reverse(dependent));
                }
            }
        }

        if result.len() < n {
            for (i, &deg) in in_degree.iter().enumerate() {
                if deg > 0 {
                    result.push(i);
                }
            }
        }

        let mut old_list = std::mem::take(list);
        let mut opt_list: Vec<Option<&crate::model::Task>> = old_list.drain(..).map(Some).collect();
        for &idx in &result {
            list.push(opt_list[idx].take().unwrap());
        }
    }

    struct SerializeContext<'a> {
        children_map: &'a std::collections::HashMap<String, Vec<&'a crate::model::Task>>,
        store: &'a crate::store::TaskStore,
        calendars: &'a [crate::model::CalendarListEntry],
    }

    fn serialize_node(
        ctx: &SerializeContext,
        task: &crate::model::Task,
        depth: usize,
        out: &mut String,
        prefix: &str,
        parent_href: &str,
    ) {
        let status_str = if task.is_note || task.is_journal {
            String::new()
        } else {
            format!(
                "{} ",
                match task.status {
                    crate::model::TaskStatus::NeedsAction => {
                        if task.is_paused() { "[/]" } else { "[ ]" }
                    }
                    crate::model::TaskStatus::InProcess => "[>]",
                    crate::model::TaskStatus::Completed => "[x]",
                    crate::model::TaskStatus::Cancelled => "[-]",
                }
            )
        };
        let mut smart_string = task.to_smart_string();
        if task.is_note {
            if smart_string.starts_with("- ") || smart_string.starts_with("* ") {
                smart_string = smart_string[2..].trim_start().to_string();
            } else if smart_string == "-" || smart_string == "*" {
                smart_string = String::new();
            }
        }
        if task.calendar_href != parent_href {
            let cal_name = ctx
                .calendars
                .iter()
                .find(|c| c.href == task.calendar_href)
                .map(|c| c.name.as_str())
                .unwrap_or(task.calendar_href.as_str());

            smart_string.push_str(&format!(
                " col:{}",
                crate::model::parser::quote_value(cal_name)
            ));
        }

        let uid_tag = format!("<!-- uid:{} -->", task.uid);
        let indent = "    ".repeat(depth);

        // Output short UID dependencies and relations to guarantee they are never ambiguous upon re-parsing
        let mut dep_str = String::new();

        let process_relations = |uids: &[String], prefix: &str, out: &mut String| {
            for uid in uids {
                // Skip trashed/recovered/missing references so they self-heal (disappear) on save
                if let Some(target_task) = ctx.store.get_task_ref(uid) {
                    if target_task.calendar_href == crate::storage::LOCAL_TRASH_HREF
                        || target_task.calendar_href == "local://recovery"
                    {
                        continue;
                    }
                } else {
                    // Task is completely missing (hard-deleted). Skip to self-heal.
                    continue;
                }

                // Only truncate if it is actually a valid UUID. If another client injected a raw string,
                // quote it so it can be cleanly resolved upon re-parsing.
                let display_val = if uid.len() == 36 && uuid::Uuid::parse_str(uid).is_ok() {
                    &uid[..8]
                } else {
                    uid
                };
                out.push_str(&format!(
                    " {}:{}",
                    prefix,
                    crate::model::parser::quote_value(display_val)
                ));
            }
        };

        process_relations(&task.dependencies, "dep", &mut dep_str);
        process_relations(&task.related_to, "rel", &mut dep_str);

        out.push_str(&format!(
            "{}{}{}{}{}{} {}\n",
            indent,
            prefix,
            if prefix.ends_with(' ') { "" } else { " " },
            status_str,
            smart_string,
            dep_str,
            uid_tag
        ));

        if !task.description.is_empty() {
            for line in task.description.lines() {
                out.push_str(&format!("{}  {}\n", indent, line));
            }
        }

        if let Some(children) = ctx.children_map.get(&task.uid) {
            let mut prefixes = Vec::new();
            let mut current_number = 1;
            let mut uses_number_prev = false;

            for i in 0..children.len() {
                let child = children[i];
                let mut uses_number = false;
                if i > 0 {
                    let prev_child = children[i - 1];
                    if child.dependencies.contains(&prev_child.uid) {
                        current_number += 1;
                        uses_number = true;
                    } else if prev_child.dependencies == child.dependencies && uses_number_prev {
                        uses_number = true;
                    } else {
                        current_number = 1;
                        let has_successor = children
                            .iter()
                            .skip(i + 1)
                            .any(|c| c.dependencies.contains(&child.uid));
                        if has_successor {
                            uses_number = true;
                        }
                    }
                } else {
                    let has_successor = children
                        .iter()
                        .skip(1)
                        .any(|c| c.dependencies.contains(&child.uid));
                    if has_successor {
                        uses_number = true;
                    }
                }

                uses_number_prev = uses_number;
                if uses_number {
                    prefixes.push(format!("{}.", current_number));
                } else {
                    prefixes.push("-".to_string());
                }
            }

            for (child, prefix) in children.iter().zip(prefixes.iter()) {
                serialize_node(ctx, child, depth + 1, out, prefix, &task.calendar_href);
            }
        }
    }

    let ctx = SerializeContext {
        children_map: &children_map,
        store,
        calendars,
    };

    if is_journal {
        if !root.description.is_empty() {
            out.push_str(&root.description);
            out.push('\n');
        }
        if let Some(children) = children_map.get(&root.uid) {
            let mut prefixes = Vec::new();
            let mut current_number = 1;
            let mut uses_number_prev = false;

            for i in 0..children.len() {
                let child = children[i];
                let mut uses_number = false;
                if i > 0 {
                    let prev_child = children[i - 1];
                    if child.dependencies.contains(&prev_child.uid) {
                        current_number += 1;
                        uses_number = true;
                    } else if prev_child.dependencies == child.dependencies && uses_number_prev {
                        uses_number = true;
                    } else {
                        current_number = 1;
                        let has_successor = children
                            .iter()
                            .skip(i + 1)
                            .any(|c| c.dependencies.contains(&child.uid));
                        if has_successor {
                            uses_number = true;
                        }
                    }
                } else {
                    let has_successor = children
                        .iter()
                        .skip(1)
                        .any(|c| c.dependencies.contains(&child.uid));
                    if has_successor {
                        uses_number = true;
                    }
                }

                uses_number_prev = uses_number;
                if uses_number {
                    prefixes.push(format!("{}.", current_number));
                } else {
                    prefixes.push("-".to_string());
                }
            }

            for (child, prefix) in children.iter().zip(prefixes.iter()) {
                serialize_node(&ctx, child, 0, &mut out, prefix, &root.calendar_href);
            }
        }
    } else {
        serialize_node(&ctx, root, 0, &mut out, "-", &root.calendar_href);
    }

    out.trim_end().to_string()
}