blotter-cli 0.15.0

A tiny CLI for AI agents to log the cuts they hit during work.
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
use crate::error::{AppError, AppResult};
use crate::{ListItem, LogEvent, Resolution, format_timestamp};
use serde_json::{Value, json};
use std::collections::{BTreeMap, HashMap};
use std::fs::{self, File, OpenOptions, Permissions};
use std::io::{ErrorKind, Read, Seek, SeekFrom, Write};
#[cfg(unix)]
use std::os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt};
use std::path::{Component, Path, PathBuf};
use std::thread;
use std::time::Duration;

const LOCK_ATTEMPTS: usize = 50;
const LOCK_DELAY: Duration = Duration::from_millis(100);

#[derive(Debug, Clone)]
pub struct ResolvedFile {
    pub path: PathBuf,
    pub explicit: bool,
    pub repo: Option<PathBuf>,
    pub warnings: Vec<String>,
}

impl ResolvedFile {
    /// Repo root for cwd relativization. Only a log living inside the repo
    /// stores repo-relative cwd; explicit and global logs are machine-local,
    /// keep absolute cwd, and would otherwise lose all provenance now that
    /// records carry no repo field.
    pub fn cwd_repo(&self) -> Option<&Path> {
        self.repo
            .as_deref()
            .filter(|root| self.path.starts_with(root))
    }
}

#[derive(Debug, Default)]
pub struct FoldResult {
    pub items: Vec<ListItem>,
    pub warnings: Vec<String>,
    records: BTreeMap<String, LogEvent>,
    orphan_amends: HashMap<String, LogEvent>,
}

pub struct LoadedFold {
    pub items: Vec<ListItem>,
    pub warnings: Vec<String>,
}

impl FoldResult {
    pub fn record(&self, id: &str) -> Option<&LogEvent> {
        self.records.get(id)
    }

    /// Materialize a just-appended resolve from the fold that made the append
    /// decision. A new base resolve activates the latest earlier orphan amend,
    /// exactly as a complete subsequent fold would; an appended amend itself
    /// is necessarily the latest materialized amend.
    pub(crate) fn materialized_appended_resolution(&self, event: &LogEvent) -> Resolution {
        let LogEvent::Resolve { id, amend, .. } = event else {
            unreachable!("only resolve events materialize resolutions")
        };
        let effective = if *amend {
            event
        } else {
            self.orphan_amends.get(id).unwrap_or(event)
        };
        resolution_from_event(effective)
    }
}

#[derive(Default)]
struct WarningCounts {
    torn: usize,
    malformed: usize,
    unknown: usize,
    duplicate_cuts: usize,
    duplicate_dogears: usize,
    duplicate_resolves: usize,
    orphans: usize,
}

pub(crate) struct ScannedLine<'a> {
    pub line: usize,
    pub raw: &'a [u8],
    pub event: Result<LogEvent, ScanIssue>,
}

pub(crate) enum ScanIssue {
    Malformed(String),
    Unknown(Option<String>),
    Torn,
}

pub fn discover(flag: Option<PathBuf>) -> AppResult<ResolvedFile> {
    let cwd = std::env::current_dir().map_err(|error| AppError::from_io(error, Path::new(".")))?;
    discover_from(&cwd, flag)
}

pub fn discover_from(cwd: &Path, flag: Option<PathBuf>) -> AppResult<ResolvedFile> {
    let repo = find_repo_root(cwd);
    if let Some(path) = flag {
        return Ok(resolved_file(absolute(cwd, path), true, repo));
    }
    if let Some(path) = std::env::var_os("BLOTTER_FILE")
        && !path.is_empty()
    {
        return Ok(resolved_file(
            absolute(cwd, PathBuf::from(path)),
            true,
            repo,
        ));
    }
    if let Some(root) = repo.clone() {
        let path = default_log_path(&root);
        return Ok(resolved_file(path, false, Some(root)));
    }
    let home = home_dir(cwd).ok_or_else(|| {
        AppError::config(
            "cannot resolve the home directory for the default blotter file",
            "Set HOME or pass --file PATH.",
        )
    })?;
    Ok(resolved_file(home.join(".blotter/log.jsonl"), false, None))
}

fn resolved_file(path: PathBuf, explicit: bool, repo: Option<PathBuf>) -> ResolvedFile {
    ResolvedFile {
        warnings: Vec::new(),
        path,
        explicit,
        repo,
    }
}

pub fn default_log_path(root: &Path) -> PathBuf {
    root.join(".blotter.jsonl")
}

pub fn find_repo_root(start: &Path) -> Option<PathBuf> {
    start
        .ancestors()
        .find(|candidate| candidate.join(".git").exists())
        .map(Path::to_path_buf)
}

pub fn home_dir(cwd: &Path) -> Option<PathBuf> {
    std::env::var_os("HOME")
        .filter(|value| !value.is_empty())
        .map(PathBuf::from)
        .map(|home| absolute(cwd, home))
}

pub fn record_cwd(cwd: &Path, repo: Option<&Path>, home: Option<&Path>) -> String {
    if let Some(relative) = repo.and_then(|root| cwd.strip_prefix(root).ok()) {
        return match relative.as_os_str().is_empty() {
            true => ".".into(),
            false => relative.to_string_lossy().into_owned(),
        };
    }
    match home.and_then(|root| cwd.strip_prefix(root).ok()) {
        Some(relative) if relative.as_os_str().is_empty() => "~".into(),
        Some(relative) => format!("~/{}", relative.to_string_lossy()),
        None => cwd.to_string_lossy().into_owned(),
    }
}

fn absolute(cwd: &Path, path: PathBuf) -> PathBuf {
    let joined = if path.is_absolute() {
        path
    } else {
        cwd.join(path)
    };
    let mut normalized = PathBuf::new();
    for component in joined.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => {
                normalized.pop();
            }
            other => normalized.push(other.as_os_str()),
        }
    }
    normalized
}

pub fn with_shared<T>(path: &Path, action: impl FnOnce(&mut File) -> AppResult<T>) -> AppResult<T> {
    let mut file = open_locked(path, false, || {
        File::open(path).map_err(|error| AppError::from_log_open(error, path))
    })?;
    let result = action(&mut file);
    let unlock = file
        .unlock()
        .map_err(|error| AppError::from_io(error, path));
    match (result, unlock) {
        (Err(error), _) | (Ok(_), Err(error)) => Err(error),
        (Ok(value), Ok(())) => Ok(value),
    }
}

pub fn read_or_empty<T>(
    path: &Path,
    explicit: bool,
    warnings: &mut Vec<String>,
    warning: &str,
    suggested_fix: &str,
    empty: impl FnOnce() -> T,
    read: impl FnOnce(&mut File) -> AppResult<T>,
) -> AppResult<(T, bool)> {
    match with_shared(path, read) {
        Ok(value) => Ok((value, true)),
        Err(error) if error.code == "not_found" && error.exit_code == 66 && !explicit => {
            warnings.push(warning.into());
            Ok((empty(), false))
        }
        Err(error) if error.code == "not_found" && error.exit_code == 66 => {
            Err(AppError::not_found(
                format!("blotter file not found: {}", path.display()),
                suggested_fix,
            ))
        }
        Err(error) => Err(error),
    }
}

pub fn load_folded(resolved: &ResolvedFile) -> AppResult<LoadedFold> {
    let mut warnings = resolved.warnings.clone();
    let (folded, _) = read_or_empty(
        &resolved.path,
        resolved.explicit,
        &mut warnings,
        "no blotter file yet; blotter add creates it",
        "Pass an existing --file PATH or run `blotter add` to create a discovered default file.",
        FoldResult::default,
        |log| {
            let bytes = read_bytes(log, &resolved.path)?;
            Ok(fold_bytes(&bytes))
        },
    )?;
    warnings.extend(folded.warnings);
    Ok(LoadedFold {
        items: folded.items,
        warnings,
    })
}

pub fn with_exclusive<T>(
    path: &Path,
    create: bool,
    action: impl FnOnce(&mut File) -> AppResult<T>,
) -> AppResult<T> {
    if create && let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|error| AppError::from_io(error, parent))?;
    }
    let mut file = open_locked(path, true, || {
        OpenOptions::new()
            .read(true)
            .append(true)
            .create(create)
            .open(path)
            .map_err(|error| AppError::from_log_open(error, path))
    })?;
    let result = action(&mut file);
    let unlock = file
        .unlock()
        .map_err(|error| AppError::from_io(error, path));
    match (result, unlock) {
        (Err(error), _) | (Ok(_), Err(error)) => Err(error),
        (Ok(value), Ok(())) => Ok(value),
    }
}

fn open_locked(
    path: &Path,
    exclusive: bool,
    mut open: impl FnMut() -> AppResult<File>,
) -> AppResult<File> {
    let mut file = Some(open()?);
    for attempt in 0..LOCK_ATTEMPTS {
        if file.is_none() {
            match open() {
                Ok(opened) => file = Some(opened),
                Err(error) if error.code == "not_found" => {
                    if attempt + 1 < LOCK_ATTEMPTS {
                        thread::sleep(LOCK_DELAY);
                    }
                    continue;
                }
                Err(error) => return Err(error),
            }
        }
        let result = if exclusive {
            file.as_ref().expect("file is open").try_lock()
        } else {
            file.as_ref().expect("file is open").try_lock_shared()
        };
        match result {
            Ok(()) => {
                if path_identity_matches(file.as_ref().expect("file is open"), path)? {
                    return Ok(file.take().expect("file is open"));
                }
                let stale = file.take().expect("file is open");
                let _ = stale.unlock();
            }
            Err(error) => {
                let error: std::io::Error = error.into();
                if error.kind() != std::io::ErrorKind::WouldBlock {
                    return Err(AppError::from_io(error, path));
                }
                if attempt + 1 < LOCK_ATTEMPTS {
                    thread::sleep(LOCK_DELAY);
                }
            }
        }
    }
    Err(AppError::lock_timeout(path))
}

#[cfg(unix)]
fn path_identity_matches(file: &File, path: &Path) -> AppResult<bool> {
    // File::metadata uses fstat; fs::metadata obtains a fresh stat of the path.
    let locked = file
        .metadata()
        .map_err(|error| AppError::from_io(error, path))?;
    match std::fs::metadata(path) {
        Ok(current) => Ok(locked.dev() == current.dev() && locked.ino() == current.ino()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(AppError::from_io(error, path)),
    }
}

#[cfg(not(unix))]
fn path_identity_matches(_file: &File, _path: &Path) -> AppResult<bool> {
    Ok(true)
}

pub fn read_bytes(file: &mut File, path: &Path) -> AppResult<Vec<u8>> {
    file.seek(SeekFrom::Start(0))
        .and_then(|_| {
            let mut bytes = Vec::new();
            file.read_to_end(&mut bytes).map(|_| bytes)
        })
        .map_err(|error| AppError::from_io(error, path))
}

pub fn write_new_file(path: &Path, bytes: &[u8], permissions: &Permissions) -> AppResult<PathBuf> {
    let mut file = create_new_file(path, permissions, false)
        .map_err(|error| AppError::from_io(error, path))?;
    if let Err(error) = file.write_all(bytes) {
        discard_new_file(file, path);
        return Err(AppError::from_io(error, path));
    }
    if let Err(error) = file.sync_all() {
        discard_new_file(file, path);
        return Err(AppError::from_io(error, path));
    }
    Ok(path.to_path_buf())
}

pub fn append_file(path: &Path, bytes: &[u8], permissions: &Permissions) -> AppResult<PathBuf> {
    let (mut file, created) = match create_new_file(path, permissions, false) {
        Ok(file) => (file, true),
        Err(error) if error.kind() == ErrorKind::AlreadyExists => (
            OpenOptions::new()
                .append(true)
                .open(path)
                .map_err(|error| AppError::from_io(error, path))?,
            false,
        ),
        Err(error) => return Err(AppError::from_io(error, path)),
    };
    if let Err(error) = file.write_all(bytes) {
        if created {
            discard_new_file(file, path);
        }
        return Err(AppError::from_io(error, path));
    }
    if let Err(error) = file.sync_all() {
        if created {
            discard_new_file(file, path);
        }
        return Err(AppError::from_io(error, path));
    }
    Ok(path.to_path_buf())
}

pub fn replace_log(
    path: &Path,
    bytes: &[u8],
    permissions: &Permissions,
    temporary_suffix: &str,
) -> AppResult<()> {
    let temporary = suffixed_path(path, temporary_suffix);
    let mut file = create_new_file(&temporary, permissions, true)
        .map_err(|error| AppError::from_io(error, &temporary))?;
    if let Err(error) = file.write_all(bytes) {
        discard_new_file(file, &temporary);
        return Err(AppError::from_io(error, &temporary));
    }
    if let Err(error) = file.sync_all() {
        discard_new_file(file, &temporary);
        return Err(AppError::from_io(error, &temporary));
    }
    drop(file);
    if let Err(error) = fs::rename(&temporary, path) {
        let _ = fs::remove_file(&temporary);
        return Err(AppError::from_io(error, path));
    }
    if let Some(parent) = path.parent()
        && let Ok(directory) = File::open(parent)
    {
        let _ = directory.sync_all();
    }
    Ok(())
}

/// Resolve a symlinked log path to its target before a copy-and-swap, so the
/// backup, sidecar, and atomic replacement all act on the real file and the
/// link survives. Only final-component links are chased; parent components
/// keep their spelling so envelope paths stay stable for regular files.
pub fn resolve_symlinked_log(path: &Path) -> AppResult<PathBuf> {
    let mut current = path.to_path_buf();
    for _ in 0..40 {
        let metadata =
            fs::symlink_metadata(&current).map_err(|error| AppError::from_io(error, &current))?;
        if !metadata.file_type().is_symlink() {
            return Ok(current);
        }
        let target = fs::read_link(&current).map_err(|error| AppError::from_io(error, &current))?;
        current = if target.is_absolute() {
            target
        } else {
            match current.parent() {
                Some(parent) => parent.join(&target),
                None => target,
            }
        };
    }
    Err(AppError::from_io(
        std::io::Error::other("too many levels of symbolic links"),
        path,
    ))
}

pub fn suffixed_path(path: &Path, suffix: &str) -> PathBuf {
    let mut value = path.as_os_str().to_os_string();
    value.push(suffix);
    PathBuf::from(value)
}

pub fn backup_timestamp(now: jiff::Timestamp) -> String {
    format_timestamp(now)
        .chars()
        .filter(|character| !matches!(character, '-' | ':' | '.'))
        .collect()
}

pub fn restore_hint(backup: &Path, path: &Path) -> String {
    format!("cp {} {}", shell_quote(backup), shell_quote(path))
}

fn create_new_file(
    path: &Path,
    permissions: &Permissions,
    set_permissions_on_non_unix: bool,
) -> std::io::Result<File> {
    let mut options = OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    options.mode(permissions.mode());
    let file = options.open(path)?;
    #[cfg(unix)]
    let permissions_result = {
        let _ = set_permissions_on_non_unix;
        file.set_permissions(permissions.clone())
    };
    #[cfg(not(unix))]
    let permissions_result = set_permissions_on_non_unix
        .then(|| file.set_permissions(permissions.clone()))
        .transpose()
        .map(|_| ());
    if let Err(error) = permissions_result {
        drop(file);
        let _ = fs::remove_file(path);
        return Err(error);
    }
    Ok(file)
}

fn discard_new_file(file: File, path: &Path) {
    drop(file);
    let _ = fs::remove_file(path);
}

fn shell_quote(path: &Path) -> String {
    format!("'{}'", path.to_string_lossy().replace('\'', "'\\''"))
}

pub fn append_json<T: serde::Serialize>(
    file: &mut File,
    path: &Path,
    prior: &[u8],
    record: &T,
) -> AppResult<()> {
    let mut record_bytes = Vec::new();
    serde_json::to_writer(&mut record_bytes, record)
        .map_err(|error| AppError::internal(error.to_string()))?;
    record_bytes.push(b'\n');
    append_bytes(file, path, prior, &record_bytes)
}

pub fn append_unique(path: &Path, record: LogEvent, dry_run: bool) -> AppResult<(bool, LogEvent)> {
    if dry_run {
        return Ok((false, record));
    }
    let id = record.id().expect("new records have IDs").to_owned();
    let kind = match &record {
        LogEvent::Cut { .. } => "cut",
        LogEvent::Dogear { .. } => "dogear",
        _ => unreachable!("append_unique only receives cut or dogear records"),
    };
    with_exclusive(path, true, |log| {
        let bytes = read_bytes(log, path)?;
        let folded = fold_bytes(&bytes);
        if let Some(existing) = folded.record(&id) {
            return if std::mem::discriminant(&record) == std::mem::discriminant(existing) {
                Ok((false, existing.clone()))
            } else {
                Err(AppError::internal(format!(
                    "{kind} ID collides with an existing non-{kind} record"
                )))
            };
        }
        append_json(log, path, &bytes, &record)?;
        Ok((true, record))
    })
}

pub fn append_json_batch<T: serde::Serialize>(
    file: &mut File,
    path: &Path,
    prior: &[u8],
    records: &[T],
) -> AppResult<()> {
    let mut record_bytes = Vec::new();
    for record in records {
        serde_json::to_writer(&mut record_bytes, record)
            .map_err(|error| AppError::internal(error.to_string()))?;
        record_bytes.push(b'\n');
    }
    append_bytes(file, path, prior, &record_bytes)
}

fn append_bytes(file: &mut File, path: &Path, prior: &[u8], record_bytes: &[u8]) -> AppResult<()> {
    append_bytes_with(file, path, prior, record_bytes, |file, bytes| {
        file.write_all(bytes)
    })
}

fn append_bytes_with(
    file: &mut File,
    path: &Path,
    prior: &[u8],
    record_bytes: &[u8],
    write: impl FnOnce(&mut File, &[u8]) -> std::io::Result<()>,
) -> AppResult<()> {
    let original_len = file
        .metadata()
        .map_err(|error| AppError::from_io(error, path))?
        .len();
    let mut bytes = Vec::new();
    if !prior.is_empty() && !prior.ends_with(b"\n") {
        bytes.push(b'\n');
    }
    bytes.extend_from_slice(record_bytes);
    // If the write fails, roll back to the pre-write length; if rollback also fails, surface both.
    if let Err(error) = write(file, &bytes) {
        if let Err(rollback) = file.set_len(original_len) {
            return Err(AppError {
                code: "io_error",
                message: format!(
                    "append failed: {error}; rollback to original length {original_len} failed: {rollback}"
                ),
                details: json!({}),
                retryable: false,
                suggested_fix: "Check the blotter file and filesystem, then retry.".into(),
                exit_code: 74,
            });
        }
        return Err(AppError::from_io(error, path));
    }
    Ok(())
}

/// Scan physical JSONL lines once. A final non-newline line is accepted only
/// when its decoded JSON carries a recognized kind, so consumers cannot
/// disagree on torn tails.
pub(crate) fn scan(bytes: &[u8]) -> impl Iterator<Item = ScannedLine<'_>> + '_ {
    // A sole empty segment is an empty file or a file holding only "\n", so
    // it is not a physical line. An empty segment after a record is malformed.
    let terminated = bytes.ends_with(b"\n");
    let body = if terminated {
        &bytes[..bytes.len() - 1]
    } else {
        bytes
    };
    let line_count = body.split(|byte| *byte == b'\n').count();
    body.split(|byte| *byte == b'\n')
        .enumerate()
        .filter_map(move |(index, raw)| {
            let final_line = index + 1 == line_count;
            if raw.is_empty() && final_line && index == 0 {
                return None;
            }
            let decoded = serde_json::from_slice::<Value>(raw);
            let known = decoded.as_ref().ok().and_then(known_kind);
            let event = if final_line && !terminated && known.is_none() {
                Err(ScanIssue::Torn)
            } else {
                match decoded {
                    Ok(value) => parse_event(value, known),
                    Err(_) => Err(ScanIssue::Malformed("line is not valid JSON".into())),
                }
            };
            Some(ScannedLine {
                line: index + 1,
                raw,
                event,
            })
        })
}

fn known_kind(value: &Value) -> Option<&'static str> {
    match value.get("kind").and_then(Value::as_str) {
        Some("cut") => Some("cut"),
        Some("dogear") => Some("dogear"),
        Some("resolve") => Some("resolve"),
        _ => None,
    }
}

fn parse_event(value: Value, known: Option<&'static str>) -> Result<LogEvent, ScanIssue> {
    let unknown = value.get("kind").and_then(Value::as_str).map(str::to_owned);
    match serde_json::from_value::<LogEvent>(value) {
        Ok(LogEvent::Unknown) => Err(ScanIssue::Unknown(unknown)),
        Ok(event) => {
            let ts = match &event {
                LogEvent::Cut { ts, .. }
                | LogEvent::Dogear { ts, .. }
                | LogEvent::Resolve { ts, .. } => ts,
                LogEvent::Unknown => unreachable!("unknown events are classified above"),
            };
            match ts.parse::<jiff::Timestamp>() {
                Ok(_) => Ok(event),
                Err(_) => Err(ScanIssue::Malformed(format!(
                    "{} ts is not a full RFC3339 timestamp",
                    known.expect("parsed events have a known kind")
                ))),
            }
        }
        Err(error) => match known {
            Some(kind) => Err(ScanIssue::Malformed(format!(
                "invalid {kind} record: {error}"
            ))),
            None => Err(ScanIssue::Unknown(unknown)),
        },
    }
}

fn resolution_from_event(event: &LogEvent) -> Resolution {
    let LogEvent::Resolve {
        ts,
        agent,
        note,
        task,
        pr,
        commit,
        url,
        dropped,
        amend,
        ..
    } = event
    else {
        unreachable!("only resolve events materialize resolutions")
    };
    Resolution {
        ts: ts.clone(),
        agent: agent.clone(),
        note: note.clone(),
        task: task.clone(),
        pr: pr.clone(),
        commit: commit.clone(),
        url: url.clone(),
        dropped: *dropped,
        amended: *amend,
    }
}

pub fn fold_bytes(bytes: &[u8]) -> FoldResult {
    let mut records = BTreeMap::<String, LogEvent>::new();
    let mut resolves = HashMap::<String, LogEvent>::new();
    let mut amends = HashMap::<String, LogEvent>::new();
    let mut counts = WarningCounts::default();
    for scanned in scan(bytes) {
        match scanned.event {
            Err(ScanIssue::Malformed(_)) => counts.malformed += 1,
            Err(ScanIssue::Unknown(_)) => counts.unknown += 1,
            Err(ScanIssue::Torn) => counts.torn += 1,
            Ok(mut event) => match &mut event {
                LogEvent::Cut { tags, .. } => {
                    // Fold normalizes legacy tag arrays for list output. Doctor
                    // receives the scanner's unmodified parsed event instead.
                    tags.sort();
                    tags.dedup();
                    let id = event.id().expect("parsed cuts have IDs").to_owned();
                    if let std::collections::btree_map::Entry::Vacant(entry) = records.entry(id) {
                        entry.insert(event);
                    } else {
                        counts.duplicate_cuts += 1;
                    }
                }
                LogEvent::Dogear { tags, .. } => {
                    tags.sort();
                    tags.dedup();
                    let id = event.id().expect("parsed dogears have IDs").to_owned();
                    if let std::collections::btree_map::Entry::Vacant(entry) = records.entry(id) {
                        entry.insert(event);
                    } else {
                        counts.duplicate_dogears += 1;
                    }
                }
                LogEvent::Resolve { id, amend, .. } => {
                    let id = id.clone();
                    let amend = *amend;
                    if amend {
                        amends.insert(id, event);
                    } else if let std::collections::hash_map::Entry::Vacant(entry) =
                        resolves.entry(id)
                    {
                        entry.insert(event);
                    } else {
                        counts.duplicate_resolves += 1;
                    }
                }
                LogEvent::Unknown => counts.unknown += 1,
            },
        }
    }

    // Base resolves remain first-wins. A latest amend only materializes when
    // the full scan found a base resolve, so merge-reordered base resolves work.
    let mut orphan_amends = HashMap::new();
    for (id, amend) in amends {
        match resolves.entry(id) {
            std::collections::hash_map::Entry::Occupied(mut entry) => {
                entry.insert(amend);
            }
            std::collections::hash_map::Entry::Vacant(entry) => {
                counts.orphans += 1;
                orphan_amends.insert(entry.into_key(), amend);
            }
        }
    }

    for id in resolves.keys() {
        if !records.contains_key(id) {
            counts.orphans += 1;
        }
    }
    let mut items: Vec<_> = records
        .values()
        .cloned()
        .map(|record| {
            let resolution = record
                .id()
                .and_then(|id| resolves.get(id))
                .map(resolution_from_event);
            let item = ListItem::from_record(record, resolution);
            let timestamp = item
                .ts
                .parse::<jiff::Timestamp>()
                .expect("folded items have valid RFC3339 timestamps");
            (item, timestamp)
        })
        .collect();
    items.sort_by(|(left, left_timestamp), (right, right_timestamp)| {
        match (left.kind.as_str(), right.kind.as_str()) {
            ("cut", "cut") => right
                .severity
                .expect("cut has severity")
                .rank()
                .cmp(&left.severity.expect("cut has severity").rank())
                .then_with(|| right_timestamp.cmp(left_timestamp))
                .then_with(|| left.id.cmp(&right.id)),
            ("dogear", "dogear") => right_timestamp
                .cmp(left_timestamp)
                .then_with(|| left.id.cmp(&right.id)),
            ("cut", "dogear") => std::cmp::Ordering::Less,
            ("dogear", "cut") => std::cmp::Ordering::Greater,
            _ => left.kind.cmp(&right.kind),
        }
    });
    let items = items.into_iter().map(|(item, _)| item).collect();

    let mut warnings = Vec::new();
    warning(&mut warnings, counts.torn, "torn final line");
    warning(&mut warnings, counts.malformed, "malformed line");
    warning(&mut warnings, counts.unknown, "unknown event");
    warning(&mut warnings, counts.duplicate_cuts, "duplicate cut");
    warning(&mut warnings, counts.duplicate_dogears, "duplicate dogear");
    warning(
        &mut warnings,
        counts.duplicate_resolves,
        "duplicate resolve",
    );
    warning(&mut warnings, counts.orphans, "orphan resolve");
    FoldResult {
        items,
        warnings,
        records,
        orphan_amends,
    }
}

fn warning(warnings: &mut Vec<String>, count: usize, label: &str) {
    if count > 0 {
        warnings.push(format!(
            "skipped {count} {label}{}",
            if count == 1 { "" } else { "s" }
        ));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ItemStatus, Severity, compute_id};
    use std::io::Write;
    use tempfile::TempDir;

    fn cut(id: &str) -> String {
        cut_with_text(id, "x")
    }

    fn cut_with_text(id: &str, text: &str) -> String {
        serde_json::json!({
            "kind":"cut", "id":id, "ts":"2026-07-09T00:00:00.000Z",
            "agent":"a", "text":text, "tags":[], "severity":"minor",
            "cwd":"/tmp", "repo":null
        })
        .to_string()
    }

    fn resolve(id: &str) -> String {
        serde_json::json!({
            "kind":"resolve", "id":id, "ts":"2026-07-10T00:00:00.000Z",
            "agent":"a", "note":null
        })
        .to_string()
    }

    #[cfg(unix)]
    #[test]
    fn exclusive_lock_reopens_a_replaced_path_before_appending() {
        let temp = TempDir::new().unwrap();
        let path = temp.path().join("cuts.jsonl");
        std::fs::write(&path, b"old\n").unwrap();

        let holder = OpenOptions::new()
            .read(true)
            .write(true)
            .open(&path)
            .unwrap();
        holder.lock().unwrap();

        let preopened = OpenOptions::new()
            .read(true)
            .append(true)
            .open(&path)
            .unwrap();
        let (opened_tx, opened_rx) = std::sync::mpsc::channel();
        let writer_path = path.clone();
        let writer = std::thread::spawn(move || {
            let mut first_open = Some(preopened);
            let mut file = open_locked(&writer_path, true, || {
                if let Some(file) = first_open.take() {
                    // The writer now owns a descriptor for the old inode.
                    opened_tx.send(()).unwrap();
                    Ok(file)
                } else {
                    OpenOptions::new()
                        .read(true)
                        .append(true)
                        .open(&writer_path)
                        .map_err(|error| AppError::from_log_open(error, &writer_path))
                }
            })
            .unwrap();
            file.write_all(b"writer\n").unwrap();
            file.unlock().unwrap();
        });

        opened_rx
            .recv_timeout(std::time::Duration::from_secs(2))
            .unwrap();
        let replacement = temp.path().join("replacement.jsonl");
        std::fs::write(&replacement, b"replacement\n").unwrap();
        std::fs::rename(&replacement, &path).unwrap();
        holder.unlock().unwrap();
        writer.join().unwrap();

        assert_eq!(std::fs::read(&path).unwrap(), b"replacement\nwriter\n");
    }

    #[test]
    fn batch_append_rollback_restores_a_torn_tail_after_partial_write_failure() {
        let temp = TempDir::new().unwrap();
        let path = temp.path().join("cuts.jsonl");
        let original = b"{\"kind\":\"cut\"}\n{\"kind\":";
        std::fs::write(&path, original).unwrap();
        let mut file = OpenOptions::new()
            .read(true)
            .append(true)
            .open(&path)
            .unwrap();

        let error = append_bytes_with(
            &mut file,
            &path,
            original,
            b"{\"kind\":\"resolve\"}\n{\"kind\":\"resolve\"}\n",
            |file, bytes| {
                file.write_all(&bytes[..8])?;
                Err(std::io::Error::other("injected partial write failure"))
            },
        )
        .unwrap_err();

        assert_eq!(error.code, "io_error");
        assert_eq!(std::fs::read(&path).unwrap(), original);
    }

    #[test]
    fn fold_matrix() {
        let id = compute_id("2026-07-09T00:00:00.000Z", "a", "x", Severity::Minor, &[]);
        let cases = [
            ("cut", format!("{}\n", cut(&id)), 1, ItemStatus::Open, 0),
            (
                "resolve before cut",
                format!("{}\n{}\n", resolve(&id), cut(&id)),
                1,
                ItemStatus::Resolved,
                0,
            ),
            (
                "duplicates",
                format!(
                    "{}\n{}\n{}\n{}\n",
                    cut(&id),
                    cut(&id),
                    resolve(&id),
                    resolve(&id)
                ),
                1,
                ItemStatus::Resolved,
                2,
            ),
            (
                "unknown malformed orphan",
                format!(
                    "{{\"kind\":\"future\"}}\nnope\n{}\n{}\n",
                    resolve("bl_deadbeef0000"),
                    cut(&id)
                ),
                1,
                ItemStatus::Open,
                3,
            ),
            (
                "torn tail",
                format!("{}\n{{\"kind\":", cut(&id)),
                1,
                ItemStatus::Open,
                1,
            ),
            (
                "all adversarial orderings interleaved",
                format!(
                    "{}\n{{\"kind\":\"future\"}}\n{}\n{}\n{}\n{}\n{}\nnope\n{{\"kind\":",
                    resolve(&id),
                    cut(&id),
                    cut(&id),
                    cut_with_text(&id, "conflicting payload"),
                    resolve(&id),
                    resolve("bl_deadbeef0000"),
                ),
                1,
                ItemStatus::Resolved,
                6,
            ),
        ];
        for (name, input, item_count, status, warning_count) in cases {
            let folded = fold_bytes(input.as_bytes());
            assert_eq!(folded.items.len(), item_count, "{name}");
            if !folded.items.is_empty() {
                assert_eq!(folded.items[0].status, status, "{name}");
                assert_eq!(folded.items[0].text, "x", "{name}");
            }
            assert_eq!(folded.warnings.len(), warning_count, "{name}");
        }
    }
}