agentsight 0.2.44

eBPF-based observability for AI agent sessions, prompts, process trees, files, network activity, and token usage.
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
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 eunomia-bpf org.

//! Resolution of the ELF binary that sslsniff should attach its SSL uprobe to.
//!
//! Three entry points are used by the CLI handlers in `main.rs`:
//!   - [`resolve_binary_path`] turns a command name/path into the underlying ELF
//!     (PATH search, symlink canonicalization, shebang interpreter resolution).
//!   - [`resolve_binary_path_for_ssl`] finds the concrete static TLS target.
//!   - [`binary_embeds_ssl`] detects statically-linked TLS (Node.js/OpenClaw).
//!   - [`resolve_container_binary_arg`] maps a container reference such as
//!     `docker://<container>` or `k8s://<namespace>/<pod>/<container>` to an
//!     explicit host SSL attach target.

/// Resolve a command name/path to the real ELF binary that should be passed
/// to sslsniff as `--binary-path`.
///
/// Handles three cases automatically:
///   1. A command on `$PATH` (e.g. `claude`, `node`) -> located via PATH search.
///   2. A symlink (e.g. `~/.local/bin/claude` -> `.../versions/2.1.150`) -> followed.
///   3. A shebang wrapper script (`#!/usr/bin/env node`) -> the interpreter ELF.
///
/// Returns the canonical path of the underlying ELF executable, or an error
/// describing why discovery failed.
pub(crate) fn resolve_binary_path(command: &str) -> Result<String, String> {
    // Limit shebang chasing so a pathological wrapper chain cannot loop forever.
    resolve_binary_path_inner(command, 0)
}

/// Resolve a command to the concrete ELF path sslsniff should attach to, when
/// one is needed for statically-linked TLS.
///
/// Codex's npm launcher is a Node.js script, but the network client runs in the
/// platform-native `@openai/codex-linux-*` binary. Prefer that native binary
/// before following the launcher's shebang to `node`; otherwise `record -- codex`
/// attaches to the wrapper and misses Codex's TLS traffic.
pub(crate) fn resolve_binary_path_for_ssl(command: &str) -> Result<Option<String>, String> {
    let launcher = resolve_command_path(command)?;
    if is_openai_codex_native_binary(&launcher) {
        return Ok(Some(canonicalize_path(&launcher)));
    }
    if let Some(path) = codex_native_binary_from_launcher(&launcher) {
        return Ok(Some(path));
    }

    let resolved = resolve_binary_path(command)?;
    if binary_embeds_ssl(&resolved) {
        Ok(Some(resolved))
    } else {
        Ok(None)
    }
}

fn resolve_binary_path_inner(command: &str, depth: u8) -> Result<String, String> {
    if depth > 5 {
        return Err(format!(
            "too many nested shebang wrappers resolving '{}'",
            command
        ));
    }

    let resolved = resolve_command_path(command)?;

    // Inspect the file header: ELF magic vs. shebang.
    let mut header = [0u8; 256];
    let n = {
        use std::io::Read;
        let mut f = std::fs::File::open(&resolved)
            .map_err(|e| format!("cannot open '{}': {}", resolved.display(), e))?;
        f.read(&mut header)
            .map_err(|e| format!("cannot read '{}': {}", resolved.display(), e))?
    };
    let header = &header[..n];

    if header.starts_with(b"\x7fELF") {
        return Ok(resolved.to_string_lossy().into_owned());
    }

    if header.starts_with(b"#!") {
        // Parse the shebang line: `#!/usr/bin/env node` or `#!/usr/bin/python3`.
        let line_end = header
            .iter()
            .position(|&b| b == b'\n')
            .unwrap_or(header.len());
        let line = String::from_utf8_lossy(&header[2..line_end]);
        let mut parts = line.split_whitespace();
        let interp = parts
            .next()
            .ok_or_else(|| format!("'{}' has an empty shebang", resolved.display()))?;
        // `/usr/bin/env foo` -> resolve `foo` on PATH instead of `env` itself.
        let next = if interp.ends_with("/env") || interp == "env" {
            parts
                .next()
                .ok_or_else(|| format!("'{}' uses env with no interpreter", resolved.display()))?
        } else {
            interp
        };
        return resolve_binary_path_inner(next, depth + 1);
    }

    Err(format!(
        "'{}' is neither an ELF binary nor a shebang script; specify --binary-path explicitly",
        resolved.display()
    ))
}

/// Locate a command and follow symlinks without chasing shebang interpreters.
fn resolve_command_path(command: &str) -> Result<std::path::PathBuf, String> {
    let candidate = if command.contains('/') {
        std::path::PathBuf::from(command)
    } else {
        find_in_path(command).ok_or_else(|| format!("'{}' not found in $PATH", command))?
    };

    std::fs::canonicalize(&candidate)
        .map_err(|e| format!("cannot resolve '{}': {}", candidate.display(), e))
}

/// Minimal `which`: find an executable file named `cmd` in the `$PATH` dirs.
///
/// When invoked under `sudo`, the inherited `$PATH` is often root's secure path,
/// which misses user-local installs like `~/.local/bin/claude`. Honor an
/// explicit `$PATH` first, then fall back to the invoking user's common bin dirs
/// derived from `$SUDO_USER`.
fn find_in_path(cmd: &str) -> Option<std::path::PathBuf> {
    let mut dirs: Vec<std::path::PathBuf> = Vec::new();

    if let Some(path) = std::env::var_os("PATH") {
        dirs.extend(std::env::split_paths(&path));
    }

    if let Some(user) = std::env::var_os("SUDO_USER")
        && let Some(home) = sudo_user_home(&user)
    {
        dirs.push(home.join(".local/bin"));
        dirs.push(home.join("bin"));
        // NVM keeps node under ~/.nvm/versions/node/<ver>/bin; pick the newest.
        if let Some(nvm_bin) = newest_nvm_bin(&home) {
            dirs.push(nvm_bin);
        }
    }

    find_executable_in_dirs(cmd, dirs)
}

fn find_executable_in_dirs(
    cmd: &str,
    dirs: impl IntoIterator<Item = std::path::PathBuf>,
) -> Option<std::path::PathBuf> {
    for dir in dirs {
        let full = dir.join(cmd);
        if let Ok(meta) = std::fs::metadata(&full)
            && meta.is_file()
        {
            return Some(full);
        }
    }
    None
}

/// Resolve the home directory of the `$SUDO_USER` by reading `/etc/passwd`.
fn sudo_user_home(user: &std::ffi::OsStr) -> Option<std::path::PathBuf> {
    let user = user.to_str()?;
    let passwd = std::fs::read_to_string("/etc/passwd").ok()?;
    for line in passwd.lines() {
        let mut fields = line.split(':');
        if fields.next() == Some(user) {
            // username:x:uid:gid:gecos:home:shell -> home is field index 5.
            return fields.nth(4).map(std::path::PathBuf::from);
        }
    }
    None
}

/// Find the newest NVM-installed node bin dir under a user's home, if any.
fn newest_nvm_bin(home: &std::path::Path) -> Option<std::path::PathBuf> {
    let versions = home.join(".nvm/versions/node");
    let mut entries: Vec<_> = std::fs::read_dir(&versions)
        .ok()?
        .filter_map(|e| e.ok())
        .map(|e| e.path())
        .collect();
    entries.sort();
    entries.last().map(|p| p.join("bin"))
}

/// Heuristic: does this ELF statically embed its own SSL implementation?
///
/// Node.js bundles OpenSSL directly into the `node` binary, so there is no
/// system `libssl.so` for sslsniff to hook — it must attach to the binary
/// itself. We detect this by scanning for static OpenSSL/BoringSSL marker
/// strings in the file. Dynamically-linked runtimes like CPython call into a
/// separate `libssl.so` (via `_ssl.so`) and do NOT contain these markers in the
/// executable, so they keep using sslsniff's system-libssl attachment with comm
/// filtering intact.
pub(crate) fn binary_embeds_ssl(path: &str) -> bool {
    use std::io::Read;
    const NEEDLES: &[&[u8]] = &[b"SSL_write", b"BoringSSLError", b"OPENSSL_internal"];
    let mut f = match std::fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return false,
    };
    let mut buf = vec![0u8; 1 << 20]; // 1 MiB chunks
    // Carry the tail of each chunk so a match spanning a boundary isn't missed.
    let mut carry: Vec<u8> = Vec::new();
    let keep = NEEDLES
        .iter()
        .map(|needle| needle.len())
        .max()
        .unwrap_or(1)
        .saturating_sub(1);
    loop {
        let n = match f.read(&mut buf) {
            Ok(0) => break,
            Ok(n) => n,
            Err(_) => return false,
        };
        carry.extend_from_slice(&buf[..n]);
        if NEEDLES
            .iter()
            .any(|needle| carry.windows(needle.len()).any(|w| w == *needle))
        {
            return true;
        }
        if carry.len() > keep {
            carry.drain(..carry.len() - keep);
        }
    }
    false
}

fn codex_native_binary_from_launcher(launcher: &std::path::Path) -> Option<String> {
    let package_root = openai_codex_package_root(launcher)?;

    let nested_scope = package_root.join("node_modules").join("@openai");
    if let Some(path) = codex_native_binary_in_scope(&nested_scope) {
        return Some(path);
    }
    if let Some(scope) = package_root.parent() {
        if let Some(path) = codex_native_binary_in_scope(scope) {
            return Some(path);
        }
    }

    None
}

fn codex_native_binary_in_scope(openai_scope: &std::path::Path) -> Option<String> {
    const CANDIDATES: &[(&str, &str)] = &[
        ("codex-linux-x64", "x86_64-unknown-linux-musl"),
        ("codex-linux-arm64", "aarch64-unknown-linux-musl"),
    ];

    for (package, target) in CANDIDATES {
        let path = openai_scope
            .join(package)
            .join("vendor")
            .join(target)
            .join("bin")
            .join("codex");
        if is_elf_file(&path) {
            return Some(canonicalize_path(&path));
        }
    }
    None
}

fn is_openai_codex_native_binary(path: &std::path::Path) -> bool {
    if !is_elf_file(path) || !file_name_eq(path, "codex") {
        return false;
    }

    let Some(bin_dir) = path.parent().filter(|p| file_name_eq(p, "bin")) else {
        return false;
    };
    let Some(target_dir) = bin_dir.parent().filter(|p| {
        file_name_eq(p, "x86_64-unknown-linux-musl")
            || file_name_eq(p, "aarch64-unknown-linux-musl")
    }) else {
        return false;
    };
    let Some(vendor_dir) = target_dir.parent().filter(|p| file_name_eq(p, "vendor")) else {
        return false;
    };
    let Some(package_dir) = vendor_dir
        .parent()
        .filter(|p| file_name_eq(p, "codex-linux-x64") || file_name_eq(p, "codex-linux-arm64"))
    else {
        return false;
    };
    package_dir
        .parent()
        .is_some_and(|parent| file_name_eq(parent, "@openai"))
}

fn openai_codex_package_root(path: &std::path::Path) -> Option<std::path::PathBuf> {
    for ancestor in path.ancestors() {
        if file_name_eq(ancestor, "codex")
            && ancestor
                .parent()
                .is_some_and(|parent| file_name_eq(parent, "@openai"))
            && ancestor
                .parent()
                .and_then(|parent| parent.parent())
                .is_some_and(|parent| file_name_eq(parent, "node_modules"))
        {
            return Some(ancestor.to_path_buf());
        }
    }
    None
}

fn file_name_eq(path: &std::path::Path, expected: &str) -> bool {
    path.file_name().and_then(|name| name.to_str()) == Some(expected)
}

fn is_elf_file(path: &std::path::Path) -> bool {
    use std::io::Read;
    let mut header = [0u8; 4];
    let mut f = match std::fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return false,
    };
    f.read_exact(&mut header).is_ok() && header == *b"\x7fELF"
}

fn canonicalize_path(path: &std::path::Path) -> String {
    std::fs::canonicalize(path)
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_else(|_| path.to_string_lossy().into_owned())
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct KubernetesRef<'a> {
    namespace: &'a str,
    pod: &'a str,
    container: Option<&'a str>,
}

impl KubernetesRef<'_> {
    fn label(&self) -> String {
        match self.container {
            Some(container) => format!("k8s://{}/{}/{}", self.namespace, self.pod, container),
            None => format!("k8s://{}/{}", self.namespace, self.pod),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct RuntimeContainerRef {
    runtime: String,
    id: String,
}

/// Strip a `docker://<ref>` or `docker:<ref>` scheme from a `--binary-path`
/// value, returning the container reference (name or id). Returns `None` for
/// ordinary filesystem paths, which are passed through to sslsniff unchanged.
pub(crate) fn parse_container_ref(binary_path: &str) -> Option<&str> {
    binary_path
        .strip_prefix("docker://")
        .or_else(|| binary_path.strip_prefix("docker:"))
        .filter(|r| !r.is_empty() && !r.contains('/'))
}

fn has_docker_scheme(binary_path: &str) -> bool {
    binary_path.starts_with("docker://") || binary_path.starts_with("docker:")
}

/// Parse a Kubernetes pod reference from `--binary-path`.
///
/// Supported forms:
///   - `k8s://pod` (default namespace)
///   - `k8s://namespace/pod`
///   - `k8s://namespace/pod/container`
///   - the same forms with `k8s:` or `kubernetes://` prefixes
fn parse_kubernetes_ref(binary_path: &str) -> Option<KubernetesRef<'_>> {
    let reference = binary_path
        .strip_prefix("k8s://")
        .or_else(|| binary_path.strip_prefix("k8s:"))
        .or_else(|| binary_path.strip_prefix("kubernetes://"))
        .or_else(|| binary_path.strip_prefix("kubernetes:"))?;

    let parts = reference.split('/').collect::<Vec<_>>();
    if parts.is_empty() || parts.iter().any(|part| part.is_empty()) {
        return None;
    }

    match parts.as_slice() {
        [pod] => Some(KubernetesRef {
            namespace: "default",
            pod,
            container: None,
        }),
        [namespace, pod] => Some(KubernetesRef {
            namespace,
            pod,
            container: None,
        }),
        [namespace, pod, container] => Some(KubernetesRef {
            namespace,
            pod,
            container: Some(container),
        }),
        _ => None,
    }
}

fn has_kubernetes_scheme(binary_path: &str) -> bool {
    binary_path.starts_with("k8s://")
        || binary_path.starts_with("k8s:")
        || binary_path.starts_with("kubernetes://")
        || binary_path.starts_with("kubernetes:")
}

pub(crate) fn resolve_container_binary_arg(
    binary_path: Option<&str>,
) -> Result<Option<(String, String)>, String> {
    let Some(binary_path) = binary_path else {
        return Ok(None);
    };

    if let Some(reference) = parse_container_ref(binary_path) {
        return resolve_container_binary_path(reference)
            .map(|path| Some((reference.to_string(), path)));
    }
    if has_docker_scheme(binary_path) {
        return Err(format!(
            "invalid Docker container reference '{}'; expected docker://<name|id>",
            binary_path
        ));
    }

    if let Some(reference) = parse_kubernetes_ref(binary_path) {
        let label = reference.label();
        return resolve_kubernetes_binary_path(&reference).map(|path| Some((label, path)));
    }
    if has_kubernetes_scheme(binary_path) {
        return Err(format!(
            "invalid Kubernetes pod reference '{}'; expected k8s://pod, k8s://namespace/pod, or k8s://namespace/pod/container",
            binary_path
        ));
    }

    Ok(None)
}

/// Resolve a Docker container reference to the explicit host path that
/// sslsniff should attach its SSL uprobe to.
///
/// This handles both statically-linked TLS runtimes (`/proc/<pid>/exe`, common
/// for Node.js/OpenClaw) and dynamically-linked OpenSSL (`/proc/<pid>/root/...`
/// for a loaded `libssl.so`). The host PID comes from `docker inspect`, so this
/// requires the Docker CLI and permission to read the target's `/proc` entries.
///
/// `docker inspect .State.Pid` returns the container's *init* process, which is
/// often a wrapper such as `tini` (OpenClaw's image uses `tini -s -- node …`).
/// That wrapper does not embed SSL, so we walk its descendant process tree and
/// require an actual SSL target.
pub(crate) fn resolve_container_binary_path(reference: &str) -> Result<String, String> {
    let init_pid = resolve_docker_container_pid(reference)?;

    find_ssl_target_in_tree(init_pid).ok_or_else(|| {
        format!(
            "container '{}' is running at host PID {}, but no SSL attach target was found in its process tree",
            reference, init_pid
        )
    })
}

fn resolve_docker_container_pid(reference: &str) -> Result<u32, String> {
    let output = std::process::Command::new("docker")
        .args(["inspect", "--format", "{{.State.Pid}}", reference])
        .output()
        .map_err(|e| format!(
            "failed to run `docker inspect` for container '{}': {} (is the Docker CLI installed and on $PATH?)",
            reference, e
        ))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(format!(
            "`docker inspect {}` failed: {}",
            reference,
            stderr.trim()
        ));
    }

    let init_pid: u32 = String::from_utf8_lossy(&output.stdout)
        .trim()
        .parse()
        .map_err(|_| format!("could not determine host PID for container '{}'", reference))?;

    if init_pid == 0 {
        return Err(format!(
            "container '{}' is not running (host PID 0)",
            reference
        ));
    }

    Ok(init_pid)
}

fn resolve_kubernetes_binary_path(reference: &KubernetesRef<'_>) -> Result<String, String> {
    let pod = kubectl_get_pod(reference)?;
    let container_id = select_kubernetes_container_id(&pod, reference)?;
    let runtime = parse_runtime_container_id(&container_id)?;
    let init_pid = resolve_runtime_container_pid(&runtime)?;

    find_ssl_target_in_tree(init_pid).ok_or_else(|| {
        let node = pod
            .pointer("/spec/nodeName")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("unknown");
        format!(
            "Kubernetes pod '{}/{}' container target '{}' is running at host PID {}, but no SSL attach target was found in its process tree. AgentSight must run on the node that hosts the pod (node: {}).",
            reference.namespace,
            reference.pod,
            runtime.id,
            init_pid,
            node
        )
    })
}

fn kubectl_get_pod(reference: &KubernetesRef<'_>) -> Result<serde_json::Value, String> {
    let output = kubectl_command()
        .args([
            "get",
            "pod",
            reference.pod,
            "-n",
            reference.namespace,
            "-o",
            "json",
        ])
        .output()
        .map_err(|e| format!(
            "failed to run `kubectl get pod {}` in namespace '{}': {} (is kubectl installed and configured?)",
            reference.pod, reference.namespace, e
        ))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(format!(
            "`kubectl get pod {} -n {}` failed: {}",
            reference.pod,
            reference.namespace,
            stderr.trim()
        ));
    }

    serde_json::from_slice(&output.stdout).map_err(|e| {
        format!(
            "`kubectl get pod {} -n {} -o json` returned invalid JSON: {}",
            reference.pod, reference.namespace, e
        )
    })
}

fn kubectl_command() -> std::process::Command {
    let mut command = std::process::Command::new("kubectl");
    if std::env::var_os("KUBECONFIG").is_none()
        && let Some(user) = std::env::var_os("SUDO_USER")
        && let Some(home) = sudo_user_home(&user)
    {
        let kubeconfig = home.join(".kube/config");
        if kubeconfig.is_file() {
            command.env("KUBECONFIG", kubeconfig);
        }
    }
    command
}

fn select_kubernetes_container_id(
    pod: &serde_json::Value,
    reference: &KubernetesRef<'_>,
) -> Result<String, String> {
    let statuses = pod
        .pointer("/status/containerStatuses")
        .and_then(serde_json::Value::as_array)
        .ok_or_else(|| {
            format!(
                "Kubernetes pod '{}/{}' has no status.containerStatuses yet",
                reference.namespace, reference.pod
            )
        })?;

    if let Some(container) = reference.container {
        let status = statuses
            .iter()
            .find(|status| {
                status.get("name").and_then(serde_json::Value::as_str) == Some(container)
            })
            .ok_or_else(|| {
                format!(
                    "Kubernetes pod '{}/{}' has no container named '{}'",
                    reference.namespace, reference.pod, container
                )
            })?;
        return container_id_from_status(status).ok_or_else(|| {
            format!(
                "Kubernetes pod '{}/{}' container '{}' has no containerID yet (is it running?)",
                reference.namespace, reference.pod, container
            )
        });
    }

    let containers = statuses
        .iter()
        .filter_map(|status| {
            let name = status.get("name").and_then(serde_json::Value::as_str)?;
            let id = container_id_from_status(status)?;
            Some((name, id))
        })
        .collect::<Vec<_>>();

    match containers.as_slice() {
        [(_, id)] => Ok(id.clone()),
        [] => Err(format!(
            "Kubernetes pod '{}/{}' has no running containers with a containerID",
            reference.namespace, reference.pod
        )),
        _ => {
            let names = containers
                .iter()
                .map(|(name, _)| *name)
                .collect::<Vec<_>>()
                .join(", ");
            Err(format!(
                "Kubernetes pod '{}/{}' has multiple containers ({}); specify one as k8s://{}/{}/<container>",
                reference.namespace, reference.pod, names, reference.namespace, reference.pod
            ))
        }
    }
}

fn container_id_from_status(status: &serde_json::Value) -> Option<String> {
    status.pointer("/state/running")?;
    let id = status.get("containerID")?.as_str()?.trim();
    (!id.is_empty()).then(|| id.to_string())
}

fn parse_runtime_container_id(container_id: &str) -> Result<RuntimeContainerRef, String> {
    let (runtime, id) = container_id.split_once("://").ok_or_else(|| {
        format!(
            "Kubernetes containerID '{}' is missing a runtime scheme",
            container_id
        )
    })?;
    let id = id.trim();
    if runtime.trim().is_empty() || id.is_empty() {
        return Err(format!(
            "Kubernetes containerID '{}' is incomplete",
            container_id
        ));
    }
    Ok(RuntimeContainerRef {
        runtime: runtime.to_string(),
        id: id.to_string(),
    })
}

fn resolve_runtime_container_pid(container: &RuntimeContainerRef) -> Result<u32, String> {
    match container.runtime.as_str() {
        "docker" => resolve_docker_container_pid(&container.id),
        "containerd" | "cri-o" | "crio" => resolve_cri_container_pid(&container.id),
        other => resolve_cri_container_pid(&container.id).map_err(|e| {
            format!(
                "unsupported Kubernetes container runtime '{}' for container '{}': {}",
                other, container.id, e
            )
        }),
    }
}

fn resolve_cri_container_pid(container_id: &str) -> Result<u32, String> {
    let output = std::process::Command::new("crictl")
        .args(["inspect", "--output", "json", container_id])
        .output()
        .map_err(|e| format!(
            "failed to run `crictl inspect` for container '{}': {} (is crictl installed and configured for this node's CRI runtime?)",
            container_id, e
        ))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(format!(
            "`crictl inspect {}` failed: {}",
            container_id,
            stderr.trim()
        ));
    }

    let value: serde_json::Value = serde_json::from_slice(&output.stdout).map_err(|e| {
        format!(
            "`crictl inspect --output json {}` returned invalid JSON: {}",
            container_id, e
        )
    })?;

    parse_crictl_pid(&value).ok_or_else(|| {
        format!(
            "could not determine host PID for CRI container '{}'",
            container_id
        )
    })
}

fn parse_crictl_pid(value: &serde_json::Value) -> Option<u32> {
    ["/info/pid", "/status/pid"]
        .into_iter()
        .filter_map(|path| value.pointer(path))
        .find_map(value_as_u32)
        .filter(|pid| *pid != 0)
}

fn value_as_u32(value: &serde_json::Value) -> Option<u32> {
    if let Some(pid) = value.as_u64() {
        return u32::try_from(pid).ok();
    }
    value.as_str()?.parse().ok()
}

/// Breadth-first search the descendant process tree rooted at `root_pid` for a
/// concrete SSL attach path.
///
/// Children are read from `/proc/<pid>/task/<pid>/children`, which lists the
/// immediate child PIDs of a process. Requires permission to read those entries
/// (root in practice for containerized processes).
fn find_ssl_target_in_tree(root_pid: u32) -> Option<String> {
    let mut queue = std::collections::VecDeque::from([root_pid]);
    let mut seen = std::collections::HashSet::new();
    while let Some(pid) = queue.pop_front() {
        if !seen.insert(pid) {
            continue;
        }
        let exe = format!("/proc/{}/exe", pid);
        if binary_embeds_ssl(&exe) {
            return Some(canonicalize_attach_path(&exe));
        }
        if let Some(path) = find_loaded_ssl_library(pid) {
            return Some(path);
        }
        let children_path = format!("/proc/{}/task/{}/children", pid, pid);
        if let Ok(children) = std::fs::read_to_string(&children_path) {
            for child in children
                .split_whitespace()
                .filter_map(|s| s.parse::<u32>().ok())
            {
                queue.push_back(child);
            }
        }
    }
    None
}

fn find_loaded_ssl_library(pid: u32) -> Option<String> {
    let maps = std::fs::read_to_string(format!("/proc/{pid}/maps")).ok()?;
    for line in maps.lines() {
        let path = line.split_whitespace().last()?;
        if !path.starts_with('/') || !path.contains("libssl.so") {
            continue;
        }
        let host_path = format!("/proc/{pid}/root{path}");
        if std::fs::metadata(&host_path).is_ok() {
            return Some(canonicalize_attach_path(&host_path));
        }
    }
    None
}

fn canonicalize_attach_path(path: &str) -> String {
    std::fs::canonicalize(path)
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_else(|_| path.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn parses_docker_double_slash_scheme() {
        assert_eq!(parse_container_ref("docker://openclaw"), Some("openclaw"));
        assert_eq!(
            parse_container_ref("docker://my-agent-1"),
            Some("my-agent-1")
        );
    }

    #[test]
    fn parses_docker_colon_scheme() {
        assert_eq!(parse_container_ref("docker:openclaw"), Some("openclaw"));
        // A 64-char container id is a valid reference too.
        assert_eq!(
            parse_container_ref("docker:abc123def456"),
            Some("abc123def456")
        );
    }

    #[test]
    fn ignores_plain_filesystem_paths() {
        assert_eq!(parse_container_ref("/proc/1234/exe"), None);
        assert_eq!(parse_container_ref("/usr/bin/node"), None);
        assert_eq!(
            parse_container_ref("~/.nvm/versions/node/v20.0.0/bin/node"),
            None
        );
    }

    #[test]
    fn rejects_empty_container_reference() {
        assert_eq!(parse_container_ref("docker://"), None);
        assert_eq!(parse_container_ref("docker:"), None);
    }

    #[test]
    fn rejects_slash_separated_docker_reference() {
        assert_eq!(parse_container_ref("docker://foo/bar"), None);
        assert_eq!(parse_container_ref("docker:foo/bar"), None);
    }

    #[test]
    fn parses_kubernetes_pod_reference_with_default_namespace() {
        assert_eq!(
            parse_kubernetes_ref("k8s://openclaw"),
            Some(KubernetesRef {
                namespace: "default",
                pod: "openclaw",
                container: None,
            })
        );
    }

    #[test]
    fn parses_kubernetes_namespaced_pod_reference() {
        assert_eq!(
            parse_kubernetes_ref("k8s://agents/openclaw"),
            Some(KubernetesRef {
                namespace: "agents",
                pod: "openclaw",
                container: None,
            })
        );
    }

    #[test]
    fn parses_kubernetes_container_reference() {
        let reference = parse_kubernetes_ref("kubernetes://agents/openclaw/gateway");
        assert_eq!(
            reference,
            Some(KubernetesRef {
                namespace: "agents",
                pod: "openclaw",
                container: Some("gateway"),
            })
        );
        assert_eq!(
            reference.as_ref().map(KubernetesRef::label),
            Some("k8s://agents/openclaw/gateway".to_string())
        );
    }

    #[test]
    fn rejects_invalid_kubernetes_references() {
        assert_eq!(parse_kubernetes_ref("k8s://"), None);
        assert_eq!(parse_kubernetes_ref("k8s://agents/"), None);
        assert_eq!(
            parse_kubernetes_ref("k8s://agents/openclaw/gateway/extra"),
            None
        );
        assert_eq!(parse_kubernetes_ref("/usr/bin/node"), None);
    }

    #[test]
    fn invalid_container_scheme_errors_before_running_external_tools() {
        assert!(
            resolve_container_binary_arg(Some("docker://"))
                .unwrap_err()
                .contains("invalid Docker container reference")
        );
        assert!(
            resolve_container_binary_arg(Some("k8s://agents/openclaw/gateway/extra"))
                .unwrap_err()
                .contains("invalid Kubernetes pod reference")
        );
    }

    #[test]
    fn selects_single_kubernetes_container_id() {
        let pod = json!({
            "status": {
                "containerStatuses": [
                    {
                        "name": "gateway",
                        "state": {"running": {"startedAt": "2026-07-01T00:00:00Z"}},
                        "containerID": "containerd://abc123"
                    }
                ]
            }
        });
        let reference = KubernetesRef {
            namespace: "agents",
            pod: "openclaw",
            container: None,
        };

        assert_eq!(
            select_kubernetes_container_id(&pod, &reference).unwrap(),
            "containerd://abc123"
        );
    }

    #[test]
    fn selects_explicit_kubernetes_container_id() {
        let pod = json!({
            "status": {
                "containerStatuses": [
                    {
                        "name": "sidecar",
                        "state": {"running": {"startedAt": "2026-07-01T00:00:00Z"}},
                        "containerID": "containerd://sidecar123"
                    },
                    {
                        "name": "gateway",
                        "state": {"running": {"startedAt": "2026-07-01T00:00:00Z"}},
                        "containerID": "containerd://gateway123"
                    }
                ]
            }
        });
        let reference = KubernetesRef {
            namespace: "agents",
            pod: "openclaw",
            container: Some("gateway"),
        };

        assert_eq!(
            select_kubernetes_container_id(&pod, &reference).unwrap(),
            "containerd://gateway123"
        );
    }

    #[test]
    fn requires_container_name_for_multi_container_pod() {
        let pod = json!({
            "status": {
                "containerStatuses": [
                    {
                        "name": "sidecar",
                        "state": {"running": {"startedAt": "2026-07-01T00:00:00Z"}},
                        "containerID": "containerd://sidecar123"
                    },
                    {
                        "name": "gateway",
                        "state": {"running": {"startedAt": "2026-07-01T00:00:00Z"}},
                        "containerID": "containerd://gateway123"
                    }
                ]
            }
        });
        let reference = KubernetesRef {
            namespace: "agents",
            pod: "openclaw",
            container: None,
        };

        let err = select_kubernetes_container_id(&pod, &reference).unwrap_err();
        assert!(err.contains("multiple containers"));
        assert!(err.contains("k8s://agents/openclaw/<container>"));
    }

    #[test]
    fn ignores_non_running_kubernetes_container_id() {
        let pod = json!({
            "status": {
                "containerStatuses": [
                    {
                        "name": "gateway",
                        "state": {"terminated": {"exitCode": 0}},
                        "containerID": "containerd://old123"
                    }
                ]
            }
        });
        let reference = KubernetesRef {
            namespace: "agents",
            pod: "openclaw",
            container: None,
        };

        let err = select_kubernetes_container_id(&pod, &reference).unwrap_err();
        assert!(err.contains("no running containers"));
    }

    #[test]
    fn parses_kubernetes_runtime_container_id() {
        assert_eq!(
            parse_runtime_container_id("containerd://abc123").unwrap(),
            RuntimeContainerRef {
                runtime: "containerd".to_string(),
                id: "abc123".to_string(),
            }
        );
        assert_eq!(
            parse_runtime_container_id("docker://def456").unwrap(),
            RuntimeContainerRef {
                runtime: "docker".to_string(),
                id: "def456".to_string(),
            }
        );
        assert!(parse_runtime_container_id("abc123").is_err());
    }

    #[test]
    fn parses_crictl_pid_shapes() {
        assert_eq!(
            parse_crictl_pid(&json!({"info": {"pid": 1234}})),
            Some(1234)
        );
        assert_eq!(
            parse_crictl_pid(&json!({"status": {"pid": "5678"}})),
            Some(5678)
        );
        assert_eq!(parse_crictl_pid(&json!({"info": {"pid": 0}})), None);
    }

    #[test]
    fn canonicalize_attach_path_resolves_proc_root_when_available() {
        assert_eq!(
            canonicalize_attach_path("/proc/self/root/etc/hosts"),
            "/etc/hosts"
        );

        let dead_proc_path = "/proc/999999999/root/usr/lib/libssl.so";
        assert_eq!(canonicalize_attach_path(dead_proc_path), dead_proc_path);
    }

    #[test]
    fn detects_boringssl_marker_in_static_binary() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("claude-like");
        std::fs::write(&path, b"prefix BoringSSLError suffix").unwrap();

        assert!(binary_embeds_ssl(path.to_str().unwrap()));
    }

    #[test]
    fn resolves_codex_npm_launcher_to_native_ssl_binary() {
        let dir = tempfile::tempdir().unwrap();
        let package_root = dir.path().join("node_modules/@openai/codex");
        let launcher = package_root.join("bin/codex.js");
        let native = package_root.join(
            "node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex",
        );
        std::fs::create_dir_all(launcher.parent().unwrap()).unwrap();
        std::fs::create_dir_all(native.parent().unwrap()).unwrap();
        std::fs::write(&launcher, b"#!/usr/bin/env node\n").unwrap();
        std::fs::write(&native, b"\x7fELFnative codex binary").unwrap();

        let resolved = resolve_binary_path_for_ssl(launcher.to_str().unwrap()).unwrap();
        let expected = native
            .canonicalize()
            .unwrap()
            .to_string_lossy()
            .into_owned();

        assert_eq!(resolved.as_deref(), Some(expected.as_str()));
    }

    #[test]
    fn resolves_codex_npm_launcher_to_sibling_native_package() {
        let dir = tempfile::tempdir().unwrap();
        let package_root = dir.path().join("node_modules/@openai/codex");
        let launcher = package_root.join("bin/codex.js");
        let native = dir.path().join(
            "node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex",
        );
        std::fs::create_dir_all(launcher.parent().unwrap()).unwrap();
        std::fs::create_dir_all(native.parent().unwrap()).unwrap();
        std::fs::write(&launcher, b"#!/usr/bin/env node\n").unwrap();
        std::fs::write(&native, b"\x7fELFnative codex binary").unwrap();

        let resolved = resolve_binary_path_for_ssl(launcher.to_str().unwrap()).unwrap();
        let expected = native
            .canonicalize()
            .unwrap()
            .to_string_lossy()
            .into_owned();

        assert_eq!(resolved.as_deref(), Some(expected.as_str()));
    }

    #[test]
    fn resolves_codex_native_package_binary_for_ssl() {
        let dir = tempfile::tempdir().unwrap();
        let native = dir.path().join(
            "node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex",
        );
        std::fs::create_dir_all(native.parent().unwrap()).unwrap();
        std::fs::write(&native, b"\x7fELFnative codex binary").unwrap();

        let resolved = resolve_binary_path_for_ssl(native.to_str().unwrap()).unwrap();
        let expected = native
            .canonicalize()
            .unwrap()
            .to_string_lossy()
            .into_owned();

        assert_eq!(resolved.as_deref(), Some(expected.as_str()));
    }

    #[test]
    fn ignores_binary_without_static_ssl_markers() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("plain");
        std::fs::write(&path, b"no tls marker here").unwrap();

        assert!(!binary_embeds_ssl(path.to_str().unwrap()));
    }

    #[test]
    fn path_search_prefers_earlier_dirs() {
        let first = tempfile::tempdir().unwrap();
        let second = tempfile::tempdir().unwrap();
        let first_cmd = first.path().join("agent");
        let second_cmd = second.path().join("agent");
        std::fs::write(&first_cmd, b"first").unwrap();
        std::fs::write(&second_cmd, b"second").unwrap();

        let found = find_executable_in_dirs(
            "agent",
            [first.path().to_path_buf(), second.path().to_path_buf()],
        )
        .unwrap();

        assert_eq!(found, first_cmd);
    }
}