codex-proc 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codex_proc-0.1.0/Cargo.lock +23 -0
- codex_proc-0.1.0/Cargo.toml +9 -0
- codex_proc-0.1.0/PACKAGE.md +5 -0
- codex_proc-0.1.0/PKG-INFO +14 -0
- codex_proc-0.1.0/pyproject.toml +12 -0
- codex_proc-0.1.0/src/main.rs +81 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "codex-proc"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"libc",
|
|
10
|
+
"pico-args",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
[[package]]
|
|
14
|
+
name = "libc"
|
|
15
|
+
version = "0.2.191"
|
|
16
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
17
|
+
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
|
18
|
+
|
|
19
|
+
[[package]]
|
|
20
|
+
name = "pico-args"
|
|
21
|
+
version = "0.5.0"
|
|
22
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
23
|
+
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# codex-proc
|
|
2
|
+
|
|
3
|
+
Count newly observed descendants of local Codex processes on Linux. Run `uvx codex-proc` for a ten-second sample or `uvx codex-proc --seconds 60` for a minute. Use `--pid PID` to select a particular process tree.
|
|
4
|
+
|
|
5
|
+
The count is a sampling lower bound. Processes that finish between polls or lose their visible ancestry can be missed. Existing descendants are reported separately. The observer excludes itself and identifies processes by PID and start time across execs. It reads same-user process metadata and prints aggregate counts.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: codex-proc
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
5
|
+
Summary: Sample new processes in local Codex process trees on Linux
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
8
|
+
|
|
9
|
+
# codex-proc
|
|
10
|
+
|
|
11
|
+
Count newly observed descendants of local Codex processes on Linux. Run `uvx codex-proc` for a ten-second sample or `uvx codex-proc --seconds 60` for a minute. Use `--pid PID` to select a particular process tree.
|
|
12
|
+
|
|
13
|
+
The count is a sampling lower bound. Processes that finish between polls or lose their visible ancestry can be missed. Existing descendants are reported separately. The observer excludes itself and identifies processes by PID and start time across execs. It reads same-user process metadata and prints aggregate counts.
|
|
14
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["maturin>=1.7,<2"]
|
|
3
|
+
build-backend = "maturin"
|
|
4
|
+
[project]
|
|
5
|
+
name = "codex-proc"
|
|
6
|
+
version = "0.1.0"
|
|
7
|
+
requires-python = ">=3.9"
|
|
8
|
+
description = "Sample new processes in local Codex process trees on Linux"
|
|
9
|
+
readme = "PACKAGE.md"
|
|
10
|
+
classifiers = ["Operating System :: POSIX :: Linux"]
|
|
11
|
+
[tool.maturin]
|
|
12
|
+
bindings = "bin"
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#[cfg(target_os = "linux")]
|
|
2
|
+
mod linux {
|
|
3
|
+
use std::{collections::{HashMap, HashSet}, fs, os::unix::fs::MetadataExt, thread, time::{Duration, Instant}};
|
|
4
|
+
#[derive(Clone)]
|
|
5
|
+
struct Proc { pid: u32, ppid: u32, start: u64, name: String }
|
|
6
|
+
type Id = (u32, u64);
|
|
7
|
+
fn scan(uid: u32) -> HashMap<u32, Proc> {
|
|
8
|
+
let mut out = HashMap::new();
|
|
9
|
+
let Ok(entries) = fs::read_dir("/proc") else { return out };
|
|
10
|
+
for e in entries.flatten() {
|
|
11
|
+
let Ok(pid) = e.file_name().to_string_lossy().parse::<u32>() else { continue };
|
|
12
|
+
if e.metadata().map(|m| m.uid()).ok() != Some(uid) { continue; }
|
|
13
|
+
let Ok(s) = fs::read_to_string(e.path().join("stat")) else { continue };
|
|
14
|
+
let (Some(l), Some(r)) = (s.find('('), s.rfind(')')) else { continue };
|
|
15
|
+
let fields: Vec<&str> = s[r+1..].split_whitespace().collect();
|
|
16
|
+
if fields.len() < 20 { continue }
|
|
17
|
+
if let (Ok(ppid), Ok(start)) = (fields[1].parse(), fields[19].parse()) {
|
|
18
|
+
out.insert(pid, Proc { pid, ppid, start, name: s[l+1..r].to_string() });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
out
|
|
22
|
+
}
|
|
23
|
+
fn under(pid: u32, ps: &HashMap<u32, Proc>, roots: &HashSet<Id>, self_id: u32) -> bool {
|
|
24
|
+
let mut p = pid; let mut seen = HashSet::new();
|
|
25
|
+
while let Some(x) = ps.get(&p) {
|
|
26
|
+
if p == self_id || !seen.insert(p) { return false }
|
|
27
|
+
if roots.contains(&(p, x.start)) { return p != pid }
|
|
28
|
+
p = x.ppid;
|
|
29
|
+
}
|
|
30
|
+
false
|
|
31
|
+
}
|
|
32
|
+
fn ticks() -> Result<u64, String> {
|
|
33
|
+
let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
|
|
34
|
+
let rc = unsafe { libc::clock_gettime(libc::CLOCK_BOOTTIME, &mut ts) };
|
|
35
|
+
let hz = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
|
|
36
|
+
if rc != 0 || hz <= 0 { return Err("cannot read boot clock".into()); }
|
|
37
|
+
Ok((ts.tv_sec as u64) * (hz as u64) + ((ts.tv_nsec as u64) * (hz as u64) / 1_000_000_000))
|
|
38
|
+
}
|
|
39
|
+
pub fn run() -> Result<(), String> {
|
|
40
|
+
let mut args = pico_args::Arguments::from_env();
|
|
41
|
+
if args.contains(["-h", "--help"]) { println!("codex-proc [--seconds N] [--interval-ms N] [--pid PID]\n\nObserve same-user Codex descendants on Linux (default: 10 seconds, 20 ms).\n--pid selects a same-user process tree for controlled measurement."); return Ok(()) }
|
|
42
|
+
let secs: u64 = args.opt_value_from_str("--seconds").map_err(|e| format!("{e}"))?.unwrap_or(10);
|
|
43
|
+
let interval: u64 = args.opt_value_from_str("--interval-ms").map_err(|e| format!("{e}"))?.unwrap_or(20);
|
|
44
|
+
let selected: Option<u32> = args.opt_value_from_str("--pid").map_err(|e| format!("{e}"))?;
|
|
45
|
+
if !args.finish().is_empty() || !(1..=3600).contains(&secs) || !(5..=1000).contains(&interval) { return Err("invalid arguments: seconds 1..3600, interval-ms 5..1000".into()) }
|
|
46
|
+
let uid = unsafe { libc::geteuid() }; let me = std::process::id(); let initial = scan(uid);
|
|
47
|
+
let roots: HashSet<Id> = if let Some(pid) = selected {
|
|
48
|
+
let p = initial.get(&pid).ok_or("PID not found or not owned by this user")?;
|
|
49
|
+
if pid == me { return Err("observer cannot be the selected root".into()) }
|
|
50
|
+
[(pid, p.start)].into_iter().collect()
|
|
51
|
+
} else {
|
|
52
|
+
initial.values().filter(|p| p.name == "codex" || p.name == "codex-code-mode").map(|p| (p.pid, p.start)).collect()
|
|
53
|
+
};
|
|
54
|
+
let baseline = initial.values().filter(|p| under(p.pid, &initial, &roots, me)).count();
|
|
55
|
+
let start_tick = ticks()?; let clock = Instant::now(); let mut seen: HashSet<Id> = HashSet::new();
|
|
56
|
+
let (mut shells, mut other) = (0, 0);
|
|
57
|
+
while clock.elapsed() < Duration::from_secs(secs) {
|
|
58
|
+
thread::sleep(Duration::from_millis(interval).min(Duration::from_secs(secs).saturating_sub(clock.elapsed())));
|
|
59
|
+
let ps = scan(uid);
|
|
60
|
+
for p in ps.values() {
|
|
61
|
+
if p.start > start_tick && under(p.pid, &ps, &roots, me) && seen.insert((p.pid, p.start)) {
|
|
62
|
+
if matches!(p.name.as_str(), "sh" | "bash" | "dash" | "zsh" | "fish") { shells += 1 } else { other += 1 }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
let elapsed = clock.elapsed().as_secs_f64();
|
|
67
|
+
println!("{} observed new descendant processes in {:.2}s (sampling lower bound)", seen.len(), elapsed);
|
|
68
|
+
println!("Roots at start: {}; existing descendants at start: {}; polling: {} ms", roots.len(), baseline, interval);
|
|
69
|
+
println!("First observed name: {} shells, {} other", shells, other);
|
|
70
|
+
println!("Same-user /proc tree only; misses short-lived, reparented or inaccessible processes and new Codex roots. Observer subtree excluded.");
|
|
71
|
+
if roots.is_empty() { println!("No Codex roots found at start; no historical count is available."); }
|
|
72
|
+
Ok(())
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
fn main() {
|
|
76
|
+
#[cfg(target_os = "linux")]
|
|
77
|
+
let result = linux::run();
|
|
78
|
+
#[cfg(not(target_os = "linux"))]
|
|
79
|
+
let result: Result<(), String> = Err("unsupported platform: Linux /proc is required; macOS is untested".into());
|
|
80
|
+
if let Err(e) = result { eprintln!("error: {e}"); std::process::exit(2) }
|
|
81
|
+
}
|