talahon-http 3.0.0

CoPilot HTTP API channel (investigations, SSE chat, status, palace)
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
//! Read-only filesystem views: eval JSON files and prompt templates.
//!
//! Eval records live in the investigation lane's private state directory,
//! `<data_dir>/sessions/<investigation session key>/evals` (the container's
//! `/home/agent/.talon/evals`): only that lane can write them, so another
//! CoPilot lane can no longer forge an alert's evals (N-2). Prompt templates
//! (and, for migration only, legacy evals) live in `groups/<folder>/`.
//!
//! Both trees are mounted read-write into agent containers, so their files
//! *and the directory entries themselves* are attacker-controlled (MYTH-10).
//! A path-based check-then-read can be raced (swap `evals/` for a symlink
//! to a host directory, or a file for a FIFO that blocks the reader
//! forever). Therefore:
//!
//! - the root (`groups/<folder>` or `<data_dir>/sessions`) is
//!   operator-controlled and opened normally; every component below it (the
//!   lane directory, `evals`, `prompts`) is opened relative to its parent
//!   descriptor with `O_DIRECTORY | O_NOFOLLOW` (a directory symlink is
//!   refused) and every later access goes through the final descriptor;
//! - files are opened relative to the directory descriptor with
//!   `O_NOFOLLOW | O_NONBLOCK | O_NOCTTY` (symlinks fail, FIFOs cannot
//!   block) and judged on the opened inode via `fstat`: regular files only,
//!   size-capped, reads bounded;
//! - everything here is blocking; callers run it inside `spawn_blocking`.

use std::fs::{File, Metadata};
use std::io::{self, Read};
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::path::Path;

use chrono::{DateTime, Utc};
use rustix::fs::{self as rfs, Mode, OFlags};
use rustix::io::Errno;
use serde::Serialize;
use serde_json::Value;

use talon_core::paths::is_valid_group_folder;

use crate::validate::{is_valid_template_name, truncate_chars};

/// Agent-written eval JSON files, below the investigation lane's state
/// directory (legacy: below the group directory).
pub(crate) const EVALS_DIR: &str = "evals";
/// Prompt templates, below the group directory.
pub(crate) const PROMPTS_DIR: &str = "prompts";
/// Max eval files returned for one alert.
pub(crate) const MAX_EVALS: usize = 100;
/// Max size of one eval file.
pub(crate) const MAX_EVAL_BYTES: u64 = 1024 * 1024;
/// Max templates listed.
pub(crate) const MAX_TEMPLATES: usize = 1_000;
/// Bytes read from a template to find its preview line.
const TEMPLATE_PREVIEW_BYTES: u64 = 64 * 1024;
/// Max characters of a template preview line.
const TEMPLATE_PREVIEW_CHARS: usize = 200;
/// Directory entries scanned per request (bounds the work an agent can
/// cause by flooding a directory).
const MAX_DIR_ENTRIES: usize = 1_000_000;

fn dir_flags() -> OFlags {
    OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC
}

fn file_flags() -> OFlags {
    OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::NOCTTY | OFlags::CLOEXEC
}

fn is_symlink_errno(e: Errno) -> bool {
    // Linux/macOS report O_NOFOLLOW on a symlink as ELOOP, FreeBSD as EMLINK.
    e == Errno::LOOP || e == Errno::MLINK
}

/// Open `<root>/<components...>` as a directory. `root` is
/// operator-controlled and opened normally (it may legitimately be a
/// symlink, e.g. to a data volume); each component is opened relative to
/// its parent's descriptor without following a symlink. `Ok(None)` when
/// anything along the way does not exist.
fn open_dir_below(root: &Path, components: &[&str]) -> io::Result<Option<OwnedFd>> {
    let mut dir = match rfs::open(
        root,
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
        Mode::empty(),
    ) {
        Ok(fd) => fd,
        Err(e) if e == Errno::NOENT => return Ok(None),
        Err(e) => return Err(e.into()),
    };
    for name in components {
        if name.is_empty() || name.contains('/') || *name == "." || *name == ".." {
            return Err(io::Error::other(format!(
                "invalid directory component {name:?}"
            )));
        }
        dir = match rfs::openat(&dir, *name, dir_flags(), Mode::empty()) {
            Ok(fd) => fd,
            Err(e) if e == Errno::NOENT => return Ok(None),
            Err(e) if is_symlink_errno(e) || e == Errno::NOTDIR => {
                return Err(io::Error::other(format!(
                    "{name}/ is a symlink or not a directory; refusing to follow it"
                )));
            }
            Err(e) => return Err(e.into()),
        };
    }
    Ok(Some(dir))
}

/// Open `<group_dir>/<name>` as a directory without following a symlink at
/// `name`. `Ok(None)` when either does not exist.
fn open_agent_dir(group_dir: &Path, name: &str) -> io::Result<Option<OwnedFd>> {
    open_dir_below(group_dir, &[name])
}

/// Sort order of [`collect_names`].
#[derive(Clone, Copy)]
enum Order {
    Ascending,
    Descending,
}

fn sort_truncate(names: &mut Vec<String>, order: Order, limit: usize) {
    match order {
        Order::Ascending => names.sort(),
        Order::Descending => names.sort_by(|a, b| b.cmp(a)),
    }
    names.truncate(limit);
}

/// The first `limit` UTF-8 entry names of `dir` (in `order`) accepted by
/// `keep`. Memory stays bounded however many entries the directory holds.
fn collect_names(
    dir: BorrowedFd<'_>,
    keep: impl Fn(&str) -> bool,
    order: Order,
    limit: usize,
) -> io::Result<Vec<String>> {
    let mut names = Vec::new();
    for (scanned, entry) in rfs::Dir::read_from(dir)?.enumerate() {
        if scanned >= MAX_DIR_ENTRIES {
            tracing::warn!(
                limit = MAX_DIR_ENTRIES,
                "directory has too many entries; listing truncated"
            );
            break;
        }
        let entry = entry?;
        let Ok(name) = entry.file_name().to_str() else {
            continue;
        };
        if name == "." || name == ".." || !keep(name) {
            continue;
        }
        names.push(name.to_string());
        if names.len() >= limit.saturating_mul(2).max(64) {
            sort_truncate(&mut names, order, limit);
        }
    }
    sort_truncate(&mut names, order, limit);
    Ok(names)
}

/// Open the entry `name` of `dir` without following symlinks or blocking,
/// and accept it only if the opened inode is a regular file.
fn open_regular(dir: BorrowedFd<'_>, name: &str) -> Result<(File, Metadata), String> {
    if name.is_empty() || name.contains('/') || name == "." || name == ".." {
        return Err("invalid name".to_string());
    }
    let fd = rfs::openat(dir, name, file_flags(), Mode::empty()).map_err(|e| {
        if is_symlink_errno(e) {
            "symbolic link".to_string()
        } else {
            format!("cannot open: {e}")
        }
    })?;
    let file = File::from(fd);
    let meta = file.metadata().map_err(|e| format!("cannot stat: {e}"))?;
    if !meta.file_type().is_file() {
        return Err("not a regular file".to_string());
    }
    Ok((file, meta))
}

/// Read at most `max` bytes; `None` if the file holds more.
fn read_capped(file: File, max: u64) -> io::Result<Option<Vec<u8>>> {
    let mut buf = Vec::new();
    file.take(max.saturating_add(1)).read_to_end(&mut buf)?;
    Ok((u64::try_from(buf.len()).unwrap_or(u64::MAX) <= max).then_some(buf))
}

/// Eval JSON files for `alert_id` (`<dir>/<alert_id>-*.json`) in the
/// directory `dir` (already opened without following symlinks), newest job
/// id first, paired with their file names. Unsafe, unparseable or oversized
/// files are skipped with a warning.
fn read_evals_in(dir: BorrowedFd<'_>, alert_id: &str) -> io::Result<Vec<(String, Value)>> {
    let prefix = format!("{alert_id}-");
    let names = collect_names(
        dir,
        |n| n.starts_with(&prefix) && n.ends_with(".json"),
        Order::Descending,
        MAX_EVALS,
    )?;

    let mut out = Vec::with_capacity(names.len());
    for name in names {
        let (file, meta) = match open_regular(dir, &name) {
            Ok(f) => f,
            Err(reason) => {
                tracing::warn!(file = %name, %reason, "skipping unsafe eval file");
                continue;
            }
        };
        if meta.len() > MAX_EVAL_BYTES {
            tracing::warn!(file = %name, size = meta.len(), "skipping oversized eval file");
            continue;
        }
        match read_capped(file, MAX_EVAL_BYTES) {
            Ok(Some(bytes)) => match serde_json::from_slice::<Value>(&bytes) {
                Ok(v) => out.push((name, v)),
                Err(e) => tracing::warn!(file = %name, err = %e, "failed to parse eval file"),
            },
            Ok(None) => tracing::warn!(file = %name, "eval file grew past the size cap; skipped"),
            Err(e) => tracing::warn!(file = %name, err = %e, "failed to read eval file"),
        }
    }
    Ok(out)
}

/// Evals for `alert_id` in `<root>/<components...>/`; nothing when the
/// directory does not exist, an error when any component is a symlink.
fn read_evals_below(
    root: &Path,
    components: &[&str],
    alert_id: &str,
) -> io::Result<Vec<(String, Value)>> {
    match open_dir_below(root, components)? {
        Some(dir) => read_evals_in(dir.as_fd(), alert_id),
        None => Ok(Vec::new()),
    }
}

/// Eval JSON files for `alert_id` in `<group_dir>/evals/<alert_id>-*.json`,
/// newest job id first. Unsafe, unparseable or oversized files are skipped
/// with a warning; a symlinked `evals/` directory is an error.
#[cfg(test)]
pub(crate) fn read_evals(group_dir: &Path, alert_id: &str) -> io::Result<Vec<Value>> {
    Ok(read_evals_below(group_dir, &[EVALS_DIR], alert_id)?
        .into_iter()
        .map(|(_, v)| v)
        .collect())
}

/// Where `GET /evals` looks for eval records.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct EvalSources {
    /// `<data_dir>/sessions` (operator-controlled; may be a symlink).
    pub sessions_dir: std::path::PathBuf,
    /// Session key of the investigation lane (its state directory below
    /// `sessions_dir`; never followed if it is a symlink).
    pub lane_key: String,
    /// The legacy shared group directory, read in addition when the
    /// operator enabled `COPILOT_EVALS_LEGACY_GROUP_DIR`.
    pub legacy_group_dir: Option<std::path::PathBuf>,
}

/// Eval records for `alert_id`, newest job id first, at most
/// [`MAX_EVALS`].
///
/// The primary source is the investigation lane's private
/// `<sessions_dir>/<lane_key>/evals/`: a symlinked lane directory or
/// `evals/` is an error. With a legacy group directory configured, records
/// found there are merged in (a file name present in both places is taken
/// from the lane-private copy); a problem with the legacy directory is
/// logged and that source skipped, so it cannot take the endpoint down.
pub(crate) fn read_investigation_evals(
    sources: &EvalSources,
    alert_id: &str,
) -> io::Result<Vec<Value>> {
    if !is_valid_group_folder(&sources.lane_key) {
        return Err(io::Error::other(format!(
            "invalid investigation lane key {:?}",
            sources.lane_key
        )));
    }
    let mut evals = read_evals_below(
        &sources.sessions_dir,
        &[sources.lane_key.as_str(), EVALS_DIR],
        alert_id,
    )?;
    if let Some(group_dir) = &sources.legacy_group_dir {
        match read_evals_below(group_dir, &[EVALS_DIR], alert_id) {
            Ok(legacy) if !legacy.is_empty() => {
                tracing::debug!(
                    count = legacy.len(),
                    "serving eval records from the legacy shared group directory"
                );
                for (name, value) in legacy {
                    if !evals.iter().any(|(n, _)| *n == name) {
                        evals.push((name, value));
                    }
                }
                evals.sort_by(|a, b| b.0.cmp(&a.0));
                evals.truncate(MAX_EVALS);
            }
            Ok(_) => {}
            Err(e) => {
                tracing::warn!(err = %e, "skipping the legacy group evals directory");
            }
        }
    }
    Ok(evals.into_iter().map(|(_, v)| v).collect())
}

/// One entry of `GET /templates`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub(crate) struct TemplateInfo {
    pub filename: String,
    pub size_bytes: u64,
    pub modified_at: Option<String>,
    pub first_line: Option<String>,
}

fn first_non_empty_line(file: File) -> Option<String> {
    let mut buf = Vec::new();
    file.take(TEMPLATE_PREVIEW_BYTES)
        .read_to_end(&mut buf)
        .ok()?;
    template_preview(&String::from_utf8_lossy(&buf))
}

/// One line describing a template: the `description:` of its YAML front
/// matter (`---` … `---`, which every shipped template starts with), else its
/// first non-empty line after the front matter, else its first non-empty line.
fn template_preview(text: &str) -> Option<String> {
    let mut lines = text.split('\n').map(str::trim);
    let mut first = lines.by_ref().find(|l| !l.is_empty())?;
    if first == "---" {
        let mut description = None;
        let mut closed = false;
        for line in lines.by_ref() {
            if line == "---" {
                closed = true;
                break;
            }
            if let Some(d) = line.strip_prefix("description:") {
                description = Some(d.trim().trim_matches(['"', '\'']).to_string());
            }
        }
        if let Some(d) = description.filter(|d| !d.is_empty()) {
            return Some(truncate_chars(&d, TEMPLATE_PREVIEW_CHARS).to_string());
        }
        if !closed {
            return None;
        }
        first = lines.find(|l| !l.is_empty())?;
    }
    Some(truncate_chars(first, TEMPLATE_PREVIEW_CHARS).to_string())
}

/// Prompt templates usable as `template_override` (regular `.txt` files with
/// a valid override name in `<group_dir>/prompts`), sorted by filename.
/// Bodies are never returned; a symlinked `prompts/` directory is an error.
pub(crate) fn list_templates(group_dir: &Path) -> io::Result<Vec<TemplateInfo>> {
    let Some(dir) = open_agent_dir(group_dir, PROMPTS_DIR)? else {
        return Ok(Vec::new());
    };
    let names = collect_names(
        dir.as_fd(),
        is_valid_template_name,
        Order::Ascending,
        MAX_TEMPLATES,
    )?;

    let mut out = Vec::with_capacity(names.len());
    for filename in names {
        let Ok((file, meta)) = open_regular(dir.as_fd(), &filename) else {
            continue;
        };
        let modified_at = meta
            .modified()
            .ok()
            .map(|t| talon_core::time::to_iso(DateTime::<Utc>::from(t)));
        out.push(TemplateInfo {
            first_line: first_non_empty_line(file),
            filename,
            size_bytes: meta.len(),
            modified_at,
        });
    }
    Ok(out)
}

/// Whether `name` is a valid override name and a regular file directly
/// inside `<group_dir>/prompts` (never through a symlink).
pub(crate) fn template_exists(group_dir: &Path, name: &str) -> bool {
    if !is_valid_template_name(name) {
        return false;
    }
    match open_agent_dir(group_dir, PROMPTS_DIR) {
        Ok(Some(dir)) => open_regular(dir.as_fd(), name).is_ok(),
        Ok(None) => false,
        Err(e) => {
            tracing::warn!(err = %e, "refusing to look up template override");
            false
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::time::{Duration, Instant};

    /// `<tmp>/group` with an `evals` and a `prompts` directory.
    fn group() -> (tempfile::TempDir, std::path::PathBuf) {
        let dir = tempfile::tempdir().unwrap();
        let g = dir.path().join("group");
        fs::create_dir_all(g.join(EVALS_DIR)).unwrap();
        fs::create_dir_all(g.join(PROMPTS_DIR)).unwrap();
        (dir, g)
    }

    fn mkfifo(path: &Path) -> bool {
        std::process::Command::new("mkfifo")
            .arg(path)
            .status()
            .is_ok_and(|s| s.success())
    }

    #[test]
    fn evals_are_filtered_by_alert_prefix_and_sorted_newest_first() {
        let (_t, g) = group();
        let d = g.join(EVALS_DIR);
        fs::write(d.join("12-copilot-inv-12-100.json"), r#"{"job":"a"}"#).unwrap();
        fs::write(d.join("12-copilot-inv-12-200.json"), r#"{"job":"b"}"#).unwrap();
        fs::write(d.join("123-copilot-inv-123-1.json"), r#"{"job":"other"}"#).unwrap();
        fs::write(d.join("12-broken.json"), "{not json").unwrap();
        fs::write(d.join("12-notes.txt"), "x").unwrap();
        let evals = read_evals(&g, "12").unwrap();
        assert_eq!(evals.len(), 2);
        assert_eq!(evals[0]["job"], "b");
        assert_eq!(evals[1]["job"], "a");
    }

    #[test]
    fn evals_are_capped_to_the_newest_max_evals() {
        let (_t, g) = group();
        let d = g.join(EVALS_DIR);
        for i in 0..(MAX_EVALS + 50) {
            fs::write(
                d.join(format!("5-job-{i:04}.json")),
                format!(r#"{{"i":{i}}}"#),
            )
            .unwrap();
        }
        let evals = read_evals(&g, "5").unwrap();
        assert_eq!(evals.len(), MAX_EVALS);
        assert_eq!(evals[0]["i"], MAX_EVALS + 49);
        assert_eq!(evals[MAX_EVALS - 1]["i"], 50);
    }

    #[test]
    fn missing_evals_dir_or_group_dir_is_empty() {
        let dir = tempfile::tempdir().unwrap();
        assert!(
            read_evals(&dir.path().join("nope"), "1")
                .unwrap()
                .is_empty()
        );
        assert!(read_evals(dir.path(), "1").unwrap().is_empty());
        assert!(list_templates(dir.path()).unwrap().is_empty());
    }

    #[test]
    fn symlinked_evals_are_skipped() {
        let (_t, g) = group();
        let outside = tempfile::tempdir().unwrap();
        let secret = outside.path().join("secret.json");
        fs::write(&secret, r#"{"secret":true}"#).unwrap();
        std::os::unix::fs::symlink(&secret, g.join(EVALS_DIR).join("7-x.json")).unwrap();
        assert!(read_evals(&g, "7").unwrap().is_empty());
    }

    #[test]
    fn symlinked_evals_dir_is_refused() {
        let (_t, g) = group();
        let outside = tempfile::tempdir().unwrap();
        fs::write(outside.path().join("7-x.json"), r#"{"secret":true}"#).unwrap();
        fs::remove_dir(g.join(EVALS_DIR)).unwrap();
        std::os::unix::fs::symlink(outside.path(), g.join(EVALS_DIR)).unwrap();
        let err = read_evals(&g, "7").unwrap_err();
        assert!(err.to_string().contains("refusing to follow"), "{err}");
    }

    #[test]
    fn evals_dir_that_is_a_file_is_refused() {
        let (_t, g) = group();
        fs::remove_dir(g.join(EVALS_DIR)).unwrap();
        fs::write(g.join(EVALS_DIR), "x").unwrap();
        assert!(read_evals(&g, "7").is_err());
    }

    #[test]
    fn operator_symlinked_group_dir_is_followed() {
        let real = tempfile::tempdir().unwrap();
        fs::create_dir_all(real.path().join(EVALS_DIR)).unwrap();
        fs::write(real.path().join(EVALS_DIR).join("3-a.json"), r#"{"ok":1}"#).unwrap();
        let links = tempfile::tempdir().unwrap();
        let g = links.path().join("copilot");
        std::os::unix::fs::symlink(real.path(), &g).unwrap();
        assert_eq!(read_evals(&g, "3").unwrap().len(), 1);
    }

    #[test]
    fn fifo_and_oversized_evals_are_skipped_without_blocking() {
        let (_t, g) = group();
        let d = g.join(EVALS_DIR);
        fs::write(
            d.join("9-big.json"),
            vec![b' '; MAX_EVAL_BYTES as usize + 1],
        )
        .unwrap();
        fs::write(d.join("9-ok.json"), r#"{"ok":true}"#).unwrap();
        fs::create_dir(d.join("9-dir.json")).unwrap();
        let have_fifo = mkfifo(&d.join("9-fifo.json"));
        let started = Instant::now();
        let evals = read_evals(&g, "9").unwrap();
        assert!(started.elapsed() < Duration::from_secs(5));
        assert_eq!(evals, vec![serde_json::json!({"ok": true})]);
        if !have_fifo {
            eprintln!("mkfifo unavailable; FIFO case not exercised");
        }
    }

    // ---- lane-private eval records (N-2) -----------------------------------

    const LANE: &str = "copilot-investigate";

    /// `<tmp>/data/sessions/<LANE>/evals` plus a legacy `<tmp>/groups/copilot/evals`.
    struct Layout {
        _tmp: tempfile::TempDir,
        sessions: std::path::PathBuf,
        group: std::path::PathBuf,
    }

    impl Layout {
        fn new() -> Self {
            let tmp = tempfile::tempdir().unwrap();
            let sessions = tmp.path().join("data/sessions");
            let group = tmp.path().join("groups/copilot");
            fs::create_dir_all(sessions.join(LANE).join(EVALS_DIR)).unwrap();
            fs::create_dir_all(group.join(EVALS_DIR)).unwrap();
            Self {
                _tmp: tmp,
                sessions,
                group,
            }
        }

        fn lane_evals(&self) -> std::path::PathBuf {
            self.sessions.join(LANE).join(EVALS_DIR)
        }

        fn sources(&self, legacy: bool) -> EvalSources {
            EvalSources {
                sessions_dir: self.sessions.clone(),
                lane_key: LANE.to_string(),
                legacy_group_dir: legacy.then(|| self.group.clone()),
            }
        }
    }

    #[test]
    fn investigation_evals_come_from_the_lane_private_state_dir() {
        let l = Layout::new();
        fs::write(
            l.lane_evals().join("12-copilot-inv-12-1.json"),
            r#"{"n":1}"#,
        )
        .unwrap();
        fs::write(
            l.lane_evals().join("12-copilot-inv-12-2.json"),
            r#"{"n":2}"#,
        )
        .unwrap();
        fs::write(
            l.lane_evals().join("120-copilot-inv-120-1.json"),
            r#"{"n":3}"#,
        )
        .unwrap();
        // A forged record dropped into the shared group folder by another lane.
        fs::write(
            l.group.join(EVALS_DIR).join("12-copilot-inv-12-9.json"),
            r#"{"forged":true}"#,
        )
        .unwrap();
        let evals = read_investigation_evals(&l.sources(false), "12").unwrap();
        assert_eq!(
            evals,
            vec![serde_json::json!({"n": 2}), serde_json::json!({"n": 1})]
        );
    }

    #[test]
    fn missing_lane_or_sessions_dir_yields_no_evals() {
        let tmp = tempfile::tempdir().unwrap();
        for sessions in [tmp.path().join("nope"), tmp.path().to_path_buf()] {
            let sources = EvalSources {
                sessions_dir: sessions,
                lane_key: LANE.to_string(),
                legacy_group_dir: None,
            };
            assert!(read_investigation_evals(&sources, "1").unwrap().is_empty());
        }
        fs::create_dir_all(tmp.path().join(LANE)).unwrap();
        let sources = EvalSources {
            sessions_dir: tmp.path().to_path_buf(),
            lane_key: LANE.to_string(),
            legacy_group_dir: None,
        };
        assert!(read_investigation_evals(&sources, "1").unwrap().is_empty());
    }

    #[test]
    fn symlinked_lane_dir_is_refused() {
        let l = Layout::new();
        let outside = tempfile::tempdir().unwrap();
        fs::create_dir_all(outside.path().join(EVALS_DIR)).unwrap();
        fs::write(
            outside.path().join(EVALS_DIR).join("7-x.json"),
            r#"{"secret":true}"#,
        )
        .unwrap();
        fs::remove_dir_all(l.sessions.join(LANE)).unwrap();
        std::os::unix::fs::symlink(outside.path(), l.sessions.join(LANE)).unwrap();
        let err = read_investigation_evals(&l.sources(false), "7").unwrap_err();
        assert!(err.to_string().contains("refusing to follow"), "{err}");
    }

    #[test]
    fn symlinked_lane_evals_dir_is_refused() {
        let l = Layout::new();
        let outside = tempfile::tempdir().unwrap();
        fs::write(outside.path().join("7-x.json"), r#"{"secret":true}"#).unwrap();
        fs::remove_dir(l.lane_evals()).unwrap();
        std::os::unix::fs::symlink(outside.path(), l.lane_evals()).unwrap();
        let err = read_investigation_evals(&l.sources(false), "7").unwrap_err();
        assert!(err.to_string().contains("refusing to follow"), "{err}");
        // A file where the directory should be is refused too.
        fs::remove_file(l.lane_evals()).unwrap();
        fs::write(l.lane_evals(), "x").unwrap();
        assert!(read_investigation_evals(&l.sources(false), "7").is_err());
    }

    #[test]
    fn symlinked_and_fifo_lane_eval_files_are_skipped() {
        let l = Layout::new();
        let outside = tempfile::tempdir().unwrap();
        fs::write(outside.path().join("s.json"), r#"{"secret":true}"#).unwrap();
        std::os::unix::fs::symlink(
            outside.path().join("s.json"),
            l.lane_evals().join("7-link.json"),
        )
        .unwrap();
        let _ = mkfifo(&l.lane_evals().join("7-fifo.json"));
        fs::write(l.lane_evals().join("7-ok.json"), r#"{"ok":1}"#).unwrap();
        let started = Instant::now();
        let evals = read_investigation_evals(&l.sources(false), "7").unwrap();
        assert!(started.elapsed() < Duration::from_secs(5));
        assert_eq!(evals, vec![serde_json::json!({"ok": 1})]);
    }

    #[test]
    fn operator_symlinked_sessions_root_is_followed() {
        let real = tempfile::tempdir().unwrap();
        let evals = real.path().join(LANE).join(EVALS_DIR);
        fs::create_dir_all(&evals).unwrap();
        fs::write(evals.join("3-a.json"), r#"{"ok":1}"#).unwrap();
        let links = tempfile::tempdir().unwrap();
        let sessions = links.path().join("sessions");
        std::os::unix::fs::symlink(real.path(), &sessions).unwrap();
        let sources = EvalSources {
            sessions_dir: sessions,
            lane_key: LANE.to_string(),
            legacy_group_dir: None,
        };
        assert_eq!(read_investigation_evals(&sources, "3").unwrap().len(), 1);
    }

    #[test]
    fn invalid_lane_keys_are_refused() {
        let l = Layout::new();
        for key in ["", "..", "a/b", "../copilot", "-x"] {
            let mut sources = l.sources(false);
            sources.lane_key = key.to_string();
            assert!(read_investigation_evals(&sources, "1").is_err(), "{key:?}");
        }
    }

    #[test]
    fn legacy_group_dir_is_merged_only_when_enabled() {
        let l = Layout::new();
        let legacy = l.group.join(EVALS_DIR);
        fs::write(l.lane_evals().join("5-job-2.json"), r#"{"src":"lane2"}"#).unwrap();
        fs::write(legacy.join("5-job-1.json"), r#"{"src":"legacy1"}"#).unwrap();
        fs::write(legacy.join("5-job-3.json"), r#"{"src":"legacy3"}"#).unwrap();
        // Same name in both places: the lane-private copy wins.
        fs::write(l.lane_evals().join("5-job-4.json"), r#"{"src":"lane4"}"#).unwrap();
        fs::write(legacy.join("5-job-4.json"), r#"{"src":"forged4"}"#).unwrap();

        let off = read_investigation_evals(&l.sources(false), "5").unwrap();
        assert_eq!(
            off,
            vec![
                serde_json::json!({"src": "lane4"}),
                serde_json::json!({"src": "lane2"})
            ]
        );
        let on = read_investigation_evals(&l.sources(true), "5").unwrap();
        assert_eq!(
            on,
            vec![
                serde_json::json!({"src": "lane4"}),
                serde_json::json!({"src": "legacy3"}),
                serde_json::json!({"src": "lane2"}),
                serde_json::json!({"src": "legacy1"}),
            ]
        );
    }

    #[test]
    fn merged_evals_stay_capped() {
        let l = Layout::new();
        for i in 0..MAX_EVALS {
            fs::write(
                l.lane_evals().join(format!("5-a-{i:04}.json")),
                format!(r#"{{"i":{i}}}"#),
            )
            .unwrap();
            fs::write(
                l.group.join(EVALS_DIR).join(format!("5-b-{i:04}.json")),
                format!(r#"{{"legacy":{i}}}"#),
            )
            .unwrap();
        }
        let evals = read_investigation_evals(&l.sources(true), "5").unwrap();
        assert_eq!(evals.len(), MAX_EVALS);
        assert_eq!(evals[0]["legacy"], MAX_EVALS - 1);
    }

    #[test]
    fn a_symlinked_legacy_dir_is_skipped_not_followed() {
        let l = Layout::new();
        fs::write(l.lane_evals().join("7-ok.json"), r#"{"ok":1}"#).unwrap();
        let outside = tempfile::tempdir().unwrap();
        fs::write(outside.path().join("7-x.json"), r#"{"secret":true}"#).unwrap();
        fs::remove_dir(l.group.join(EVALS_DIR)).unwrap();
        std::os::unix::fs::symlink(outside.path(), l.group.join(EVALS_DIR)).unwrap();
        let evals = read_investigation_evals(&l.sources(true), "7").unwrap();
        assert_eq!(evals, vec![serde_json::json!({"ok": 1})]);
    }

    #[test]
    fn templates_list_valid_regular_txt_files_with_previews() {
        let (_t, g) = group();
        let d = g.join(PROMPTS_DIR);
        fs::write(
            d.join("sysmon_event_1.txt"),
            "\n\n  # Process Creation  \nbody",
        )
        .unwrap();
        fs::write(d.join("b.txt"), "").unwrap();
        fs::write(d.join("README.md"), "# readme").unwrap();
        fs::write(d.join(".hidden.txt"), "x").unwrap();
        fs::write(d.join("long.txt"), "x".repeat(500)).unwrap();
        let outside = tempfile::tempdir().unwrap();
        fs::write(outside.path().join("t.txt"), "secret").unwrap();
        std::os::unix::fs::symlink(outside.path().join("t.txt"), d.join("link.txt")).unwrap();
        let _ = mkfifo(&d.join("fifo.txt"));

        let t = list_templates(&g).unwrap();
        let names: Vec<_> = t.iter().map(|t| t.filename.as_str()).collect();
        assert_eq!(names, vec!["b.txt", "long.txt", "sysmon_event_1.txt"]);
        assert_eq!(t[0].first_line, None);
        assert_eq!(t[1].first_line.as_ref().unwrap().len(), 200);
        assert_eq!(t[2].first_line.as_deref(), Some("# Process Creation"));
        assert!(t[2].modified_at.as_ref().unwrap().ends_with('Z'));
        assert_eq!(t[2].size_bytes, 29);
    }

    #[test]
    fn template_preview_reads_past_front_matter() {
        // Every shipped template starts with YAML front matter; its first
        // line `---` used to be the preview of all of them.
        let with_description =
            "---\ndescription: \"Sysmon Event 1 (Process Creation)\"\nauthor: x\n---\n\n# Body";
        assert_eq!(
            template_preview(with_description).as_deref(),
            Some("Sysmon Event 1 (Process Creation)")
        );
        assert_eq!(
            template_preview("---\nauthor: x\n---\n\n  # Body title\nmore").as_deref(),
            Some("# Body title")
        );
        assert_eq!(template_preview("---\nauthor: x\n"), None);
        assert_eq!(template_preview("\n# Plain\n").as_deref(), Some("# Plain"));
        let shipped = "---\ndescription: Investigation template for Sysmon Event 1 (Process Creation) alerts from {{ pipeline | default('wazuh') }}.\nauthor: Taylor Walton\n---\n\nAs a Security Operations Analyst, ...";
        assert!(
            template_preview(shipped)
                .unwrap()
                .starts_with("Investigation template")
        );
    }

    #[test]
    fn symlinked_prompts_dir_is_refused() {
        let (_t, g) = group();
        let outside = tempfile::tempdir().unwrap();
        fs::write(outside.path().join("t.txt"), "secret").unwrap();
        fs::remove_dir(g.join(PROMPTS_DIR)).unwrap();
        std::os::unix::fs::symlink(outside.path(), g.join(PROMPTS_DIR)).unwrap();
        assert!(list_templates(&g).is_err());
        assert!(!template_exists(&g, "t.txt"));
    }

    #[test]
    fn template_existence_requires_a_regular_file_inside_the_dir() {
        let (_t, g) = group();
        let d = g.join(PROMPTS_DIR);
        fs::write(d.join("ok.txt"), "x").unwrap();
        fs::create_dir(d.join("dir.txt")).unwrap();
        let outside = tempfile::tempdir().unwrap();
        fs::write(outside.path().join("t.txt"), "x").unwrap();
        std::os::unix::fs::symlink(outside.path().join("t.txt"), d.join("link.txt")).unwrap();
        let have_fifo = mkfifo(&d.join("fifo.txt"));
        assert!(template_exists(&g, "ok.txt"));
        assert!(!template_exists(&g, "missing.txt"));
        assert!(!template_exists(&g, "dir.txt"));
        assert!(!template_exists(&g, "link.txt"));
        assert!(!template_exists(&g, "../ok.txt"));
        if have_fifo {
            assert!(!template_exists(&g, "fifo.txt"));
        }
    }

    #[test]
    fn names_are_collected_with_bounded_memory_in_order() {
        let (_t, g) = group();
        let d = g.join(PROMPTS_DIR);
        for i in 0..300 {
            fs::write(d.join(format!("t{i:03}.txt")), "x").unwrap();
        }
        let dir = open_agent_dir(&g, PROMPTS_DIR).unwrap().unwrap();
        let asc = collect_names(dir.as_fd(), |_| true, Order::Ascending, 10).unwrap();
        assert_eq!(asc.first().map(String::as_str), Some("t000.txt"));
        assert_eq!(asc.len(), 10);
        let desc = collect_names(dir.as_fd(), |_| true, Order::Descending, 3).unwrap();
        assert_eq!(desc, vec!["t299.txt", "t298.txt", "t297.txt"]);
    }
}