codex-proc 0.1.0__tar.gz → 0.2.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.
@@ -0,0 +1,245 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "bitflags"
7
+ version = "2.13.2"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
10
+
11
+ [[package]]
12
+ name = "codex-proc"
13
+ version = "0.2.0"
14
+ dependencies = [
15
+ "libc",
16
+ "pico-args",
17
+ "sysinfo",
18
+ ]
19
+
20
+ [[package]]
21
+ name = "libc"
22
+ version = "0.2.191"
23
+ source = "registry+https://github.com/rust-lang/crates.io-index"
24
+ checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
25
+
26
+ [[package]]
27
+ name = "memchr"
28
+ version = "2.8.3"
29
+ source = "registry+https://github.com/rust-lang/crates.io-index"
30
+ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
31
+
32
+ [[package]]
33
+ name = "ntapi"
34
+ version = "0.4.3"
35
+ source = "registry+https://github.com/rust-lang/crates.io-index"
36
+ checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae"
37
+ dependencies = [
38
+ "winapi",
39
+ ]
40
+
41
+ [[package]]
42
+ name = "objc2-core-foundation"
43
+ version = "0.3.2"
44
+ source = "registry+https://github.com/rust-lang/crates.io-index"
45
+ checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
46
+ dependencies = [
47
+ "bitflags",
48
+ ]
49
+
50
+ [[package]]
51
+ name = "objc2-io-kit"
52
+ version = "0.3.2"
53
+ source = "registry+https://github.com/rust-lang/crates.io-index"
54
+ checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15"
55
+ dependencies = [
56
+ "libc",
57
+ "objc2-core-foundation",
58
+ ]
59
+
60
+ [[package]]
61
+ name = "pico-args"
62
+ version = "0.5.0"
63
+ source = "registry+https://github.com/rust-lang/crates.io-index"
64
+ checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
65
+
66
+ [[package]]
67
+ name = "proc-macro2"
68
+ version = "1.0.107"
69
+ source = "registry+https://github.com/rust-lang/crates.io-index"
70
+ checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
71
+ dependencies = [
72
+ "unicode-ident",
73
+ ]
74
+
75
+ [[package]]
76
+ name = "quote"
77
+ version = "1.0.47"
78
+ source = "registry+https://github.com/rust-lang/crates.io-index"
79
+ checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
80
+ dependencies = [
81
+ "proc-macro2",
82
+ ]
83
+
84
+ [[package]]
85
+ name = "syn"
86
+ version = "2.0.119"
87
+ source = "registry+https://github.com/rust-lang/crates.io-index"
88
+ checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
89
+ dependencies = [
90
+ "proc-macro2",
91
+ "quote",
92
+ "unicode-ident",
93
+ ]
94
+
95
+ [[package]]
96
+ name = "sysinfo"
97
+ version = "0.40.4"
98
+ source = "registry+https://github.com/rust-lang/crates.io-index"
99
+ checksum = "3e80749408e2d8bec0ffa303012cf4e1b3b355f4a09087b4fdf7473680f6a2de"
100
+ dependencies = [
101
+ "libc",
102
+ "memchr",
103
+ "ntapi",
104
+ "objc2-core-foundation",
105
+ "objc2-io-kit",
106
+ "windows",
107
+ ]
108
+
109
+ [[package]]
110
+ name = "unicode-ident"
111
+ version = "1.0.27"
112
+ source = "registry+https://github.com/rust-lang/crates.io-index"
113
+ checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
114
+
115
+ [[package]]
116
+ name = "winapi"
117
+ version = "0.3.9"
118
+ source = "registry+https://github.com/rust-lang/crates.io-index"
119
+ checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
120
+ dependencies = [
121
+ "winapi-i686-pc-windows-gnu",
122
+ "winapi-x86_64-pc-windows-gnu",
123
+ ]
124
+
125
+ [[package]]
126
+ name = "winapi-i686-pc-windows-gnu"
127
+ version = "0.4.0"
128
+ source = "registry+https://github.com/rust-lang/crates.io-index"
129
+ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
130
+
131
+ [[package]]
132
+ name = "winapi-x86_64-pc-windows-gnu"
133
+ version = "0.4.0"
134
+ source = "registry+https://github.com/rust-lang/crates.io-index"
135
+ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
136
+
137
+ [[package]]
138
+ name = "windows"
139
+ version = "0.62.2"
140
+ source = "registry+https://github.com/rust-lang/crates.io-index"
141
+ checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580"
142
+ dependencies = [
143
+ "windows-collections",
144
+ "windows-core",
145
+ "windows-future",
146
+ "windows-numerics",
147
+ ]
148
+
149
+ [[package]]
150
+ name = "windows-collections"
151
+ version = "0.3.2"
152
+ source = "registry+https://github.com/rust-lang/crates.io-index"
153
+ checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610"
154
+ dependencies = [
155
+ "windows-core",
156
+ ]
157
+
158
+ [[package]]
159
+ name = "windows-core"
160
+ version = "0.62.2"
161
+ source = "registry+https://github.com/rust-lang/crates.io-index"
162
+ checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
163
+ dependencies = [
164
+ "windows-implement",
165
+ "windows-interface",
166
+ "windows-link",
167
+ "windows-result",
168
+ "windows-strings",
169
+ ]
170
+
171
+ [[package]]
172
+ name = "windows-future"
173
+ version = "0.3.2"
174
+ source = "registry+https://github.com/rust-lang/crates.io-index"
175
+ checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb"
176
+ dependencies = [
177
+ "windows-core",
178
+ "windows-link",
179
+ "windows-threading",
180
+ ]
181
+
182
+ [[package]]
183
+ name = "windows-implement"
184
+ version = "0.60.2"
185
+ source = "registry+https://github.com/rust-lang/crates.io-index"
186
+ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
187
+ dependencies = [
188
+ "proc-macro2",
189
+ "quote",
190
+ "syn",
191
+ ]
192
+
193
+ [[package]]
194
+ name = "windows-interface"
195
+ version = "0.59.3"
196
+ source = "registry+https://github.com/rust-lang/crates.io-index"
197
+ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
198
+ dependencies = [
199
+ "proc-macro2",
200
+ "quote",
201
+ "syn",
202
+ ]
203
+
204
+ [[package]]
205
+ name = "windows-link"
206
+ version = "0.2.1"
207
+ source = "registry+https://github.com/rust-lang/crates.io-index"
208
+ checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
209
+
210
+ [[package]]
211
+ name = "windows-numerics"
212
+ version = "0.3.1"
213
+ source = "registry+https://github.com/rust-lang/crates.io-index"
214
+ checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26"
215
+ dependencies = [
216
+ "windows-core",
217
+ "windows-link",
218
+ ]
219
+
220
+ [[package]]
221
+ name = "windows-result"
222
+ version = "0.4.1"
223
+ source = "registry+https://github.com/rust-lang/crates.io-index"
224
+ checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
225
+ dependencies = [
226
+ "windows-link",
227
+ ]
228
+
229
+ [[package]]
230
+ name = "windows-strings"
231
+ version = "0.5.1"
232
+ source = "registry+https://github.com/rust-lang/crates.io-index"
233
+ checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
234
+ dependencies = [
235
+ "windows-link",
236
+ ]
237
+
238
+ [[package]]
239
+ name = "windows-threading"
240
+ version = "0.2.1"
241
+ source = "registry+https://github.com/rust-lang/crates.io-index"
242
+ checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37"
243
+ dependencies = [
244
+ "windows-link",
245
+ ]
@@ -1,9 +1,11 @@
1
1
  [package]
2
2
  name = "codex-proc"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  edition = "2021"
5
5
  readme = "PACKAGE.md"
6
6
  include = ["src/main.rs", "Cargo.toml", "Cargo.lock", "pyproject.toml", "PACKAGE.md"]
7
7
  [dependencies]
8
8
  pico-args = "0.5"
9
9
  libc = "0.2"
10
+ [target.'cfg(not(target_os = "linux"))'.dependencies]
11
+ sysinfo = { version = "=0.40.4", default-features = false, features = ["system"] }
@@ -1,5 +1,5 @@
1
1
  # codex-proc
2
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.
3
+ Count newly observed descendants of local Codex processes on Linux, macOS, and Windows. 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
4
 
5
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.
@@ -1,14 +1,16 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: codex-proc
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Classifier: Operating System :: POSIX :: Linux
5
- Summary: Sample new processes in local Codex process trees on Linux
5
+ Classifier: Operating System :: MacOS
6
+ Classifier: Operating System :: Microsoft :: Windows
7
+ Summary: Sample new processes in local Codex process trees on Linux, macOS, and Windows
6
8
  Requires-Python: >=3.9
7
9
  Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
8
10
 
9
11
  # codex-proc
10
12
 
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.
13
+ Count newly observed descendants of local Codex processes on Linux, macOS, and Windows. 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
14
 
13
15
  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
16
 
@@ -3,10 +3,10 @@ requires = ["maturin>=1.7,<2"]
3
3
  build-backend = "maturin"
4
4
  [project]
5
5
  name = "codex-proc"
6
- version = "0.1.0"
6
+ version = "0.2.0"
7
7
  requires-python = ">=3.9"
8
- description = "Sample new processes in local Codex process trees on Linux"
8
+ description = "Sample new processes in local Codex process trees on Linux, macOS, and Windows"
9
9
  readme = "PACKAGE.md"
10
- classifiers = ["Operating System :: POSIX :: Linux"]
10
+ classifiers = ["Operating System :: POSIX :: Linux", "Operating System :: MacOS", "Operating System :: Microsoft :: Windows"]
11
11
  [tool.maturin]
12
12
  bindings = "bin"
@@ -0,0 +1,218 @@
1
+ mod observer {
2
+ use std::{
3
+ collections::{HashMap, HashSet},
4
+ thread,
5
+ time::{Duration, Instant},
6
+ };
7
+ #[derive(Clone)]
8
+ struct Proc {
9
+ pid: u32,
10
+ ppid: u32,
11
+ start: u64,
12
+ name: String,
13
+ }
14
+ type Id = (u32, u64);
15
+ #[cfg(target_os = "linux")]
16
+ fn scan() -> Result<HashMap<u32, Proc>, String> {
17
+ use std::{fs, os::unix::fs::MetadataExt};
18
+ let uid = unsafe { libc::geteuid() };
19
+ let mut out = HashMap::new();
20
+ let entries = fs::read_dir("/proc").map_err(|e| e.to_string())?;
21
+ for e in entries.flatten() {
22
+ let Ok(pid) = e.file_name().to_string_lossy().parse::<u32>() else {
23
+ continue;
24
+ };
25
+ if e.metadata().map(|m| m.uid()).ok() != Some(uid) {
26
+ continue;
27
+ }
28
+ let Ok(s) = fs::read_to_string(e.path().join("stat")) else {
29
+ continue;
30
+ };
31
+ let (Some(l), Some(r)) = (s.find('('), s.rfind(')')) else {
32
+ continue;
33
+ };
34
+ let fields: Vec<&str> = s[r + 1..].split_whitespace().collect();
35
+ if fields.len() < 20 {
36
+ continue;
37
+ }
38
+ if let (Ok(ppid), Ok(start)) = (fields[1].parse(), fields[19].parse()) {
39
+ out.insert(
40
+ pid,
41
+ Proc {
42
+ pid,
43
+ ppid,
44
+ start,
45
+ name: s[l + 1..r].to_string(),
46
+ },
47
+ );
48
+ }
49
+ }
50
+ Ok(out)
51
+ }
52
+ #[cfg(target_os = "linux")]
53
+ fn birth_cutoff() -> Result<u64, String> {
54
+ let mut t = libc::timespec {
55
+ tv_sec: 0,
56
+ tv_nsec: 0,
57
+ };
58
+ let hz = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
59
+ if hz <= 0 || unsafe { libc::clock_gettime(libc::CLOCK_BOOTTIME, &mut t) } != 0 {
60
+ return Err("cannot read process clock".into());
61
+ }
62
+ Ok(t.tv_sec as u64 * hz as u64 + t.tv_nsec as u64 * hz as u64 / 1_000_000_000 + 1)
63
+ }
64
+ #[cfg(not(target_os = "linux"))]
65
+ fn birth_cutoff() -> Result<u64, String> {
66
+ std::time::SystemTime::now()
67
+ .duration_since(std::time::UNIX_EPOCH)
68
+ .map(|d| d.as_secs())
69
+ .map_err(|e| e.to_string())
70
+ }
71
+ fn under(pid: u32, ps: &HashMap<u32, Proc>, roots: &HashSet<Id>, self_id: u32) -> bool {
72
+ let mut p = pid;
73
+ let mut seen = HashSet::new();
74
+ while let Some(x) = ps.get(&p) {
75
+ if p == self_id || !seen.insert(p) {
76
+ return false;
77
+ }
78
+ if roots.contains(&(p, x.start)) {
79
+ return p != pid;
80
+ }
81
+ if ps.get(&x.ppid).is_some_and(|parent| parent.start > x.start) {
82
+ return false;
83
+ }
84
+ p = x.ppid;
85
+ }
86
+ false
87
+ }
88
+ #[cfg(not(target_os = "linux"))]
89
+ fn scan() -> Result<HashMap<u32, Proc>, String> {
90
+ use sysinfo::{Pid, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};
91
+ let mut system = System::new().map_err(|e| e.to_string())?;
92
+ system.refresh_processes_specifics(
93
+ ProcessesToUpdate::All,
94
+ true,
95
+ ProcessRefreshKind::nothing().with_user(UpdateKind::Always),
96
+ );
97
+ let uid = system
98
+ .process(Pid::from_u32(std::process::id()))
99
+ .and_then(|p| p.user_id())
100
+ .ok_or("cannot identify observer account")?;
101
+ Ok(system
102
+ .processes()
103
+ .values()
104
+ .filter(|p| p.user_id() == Some(uid))
105
+ .map(|p| {
106
+ let pid = p.pid().as_u32();
107
+ let name = p.name().to_string_lossy().to_ascii_lowercase();
108
+ (
109
+ pid,
110
+ Proc {
111
+ pid,
112
+ ppid: p.parent().map(|p| p.as_u32()).unwrap_or(0),
113
+ start: p.start_time(),
114
+ name: name.trim_end_matches(".exe").to_owned(),
115
+ },
116
+ )
117
+ })
118
+ .collect())
119
+ }
120
+ pub fn run() -> Result<(), String> {
121
+ let mut args = pico_args::Arguments::from_env();
122
+ if args.contains(["-h", "--help"]) {
123
+ println!("codex-proc [--seconds N] [--interval-ms N] [--pid PID]\n\nObserve same-user Codex descendants (default: 10 seconds, 20 ms).\n--pid selects a same-user process tree for controlled measurement.");
124
+ return Ok(());
125
+ }
126
+ let secs: u64 = args
127
+ .opt_value_from_str("--seconds")
128
+ .map_err(|e| format!("{e}"))?
129
+ .unwrap_or(10);
130
+ let interval: u64 = args
131
+ .opt_value_from_str("--interval-ms")
132
+ .map_err(|e| format!("{e}"))?
133
+ .unwrap_or(20);
134
+ let selected: Option<u32> = args
135
+ .opt_value_from_str("--pid")
136
+ .map_err(|e| format!("{e}"))?;
137
+ if !args.finish().is_empty()
138
+ || !(1..=3600).contains(&secs)
139
+ || !(5..=1000).contains(&interval)
140
+ {
141
+ return Err("invalid arguments: seconds 1..3600, interval-ms 5..1000".into());
142
+ }
143
+ let me = std::process::id();
144
+ let initial = scan()?;
145
+ let roots: HashSet<Id> = if let Some(pid) = selected {
146
+ let p = initial
147
+ .get(&pid)
148
+ .ok_or("PID not found or not owned by this user")?;
149
+ if pid == me {
150
+ return Err("observer cannot be the selected root".into());
151
+ }
152
+ [(pid, p.start)].into_iter().collect()
153
+ } else {
154
+ initial
155
+ .values()
156
+ .filter(|p| p.name == "codex" || p.name == "codex-code-mode")
157
+ .map(|p| (p.pid, p.start))
158
+ .collect()
159
+ };
160
+ let baseline = initial
161
+ .values()
162
+ .filter(|p| under(p.pid, &initial, &roots, me))
163
+ .count();
164
+ let preexisting: HashSet<Id> = initial.values().map(|p| (p.pid, p.start)).collect();
165
+ let cutoff = birth_cutoff()?;
166
+ let clock = Instant::now();
167
+ let mut seen: HashSet<Id> = HashSet::new();
168
+ let (mut shells, mut other) = (0, 0);
169
+ while clock.elapsed() < Duration::from_secs(secs) {
170
+ thread::sleep(
171
+ Duration::from_millis(interval)
172
+ .min(Duration::from_secs(secs).saturating_sub(clock.elapsed())),
173
+ );
174
+ let ps = scan()?;
175
+ for p in ps.values() {
176
+ if p.start >= cutoff
177
+ && !preexisting.contains(&(p.pid, p.start))
178
+ && under(p.pid, &ps, &roots, me)
179
+ && seen.insert((p.pid, p.start))
180
+ {
181
+ if matches!(
182
+ p.name.as_str(),
183
+ "sh" | "bash" | "dash" | "zsh" | "fish" | "cmd" | "powershell" | "pwsh"
184
+ ) {
185
+ shells += 1
186
+ } else {
187
+ other += 1
188
+ }
189
+ }
190
+ }
191
+ }
192
+ let elapsed = clock.elapsed().as_secs_f64();
193
+ println!(
194
+ "{} observed new descendant processes in {:.2}s (sampling lower bound)",
195
+ seen.len(),
196
+ elapsed
197
+ );
198
+ println!(
199
+ "Roots at start: {}; existing descendants at start: {}; polling: {} ms",
200
+ roots.len(),
201
+ baseline,
202
+ interval
203
+ );
204
+ println!("First observed name: {} shells, {} other", shells, other);
205
+ println!("Same-user process tree only; misses short-lived, reparented or inaccessible processes and new Codex roots. Observer subtree excluded.");
206
+ if roots.is_empty() {
207
+ println!("No Codex roots found at start; no historical count is available.");
208
+ }
209
+ Ok(())
210
+ }
211
+ }
212
+ fn main() {
213
+ let result = observer::run();
214
+ if let Err(e) = result {
215
+ eprintln!("error: {e}");
216
+ std::process::exit(2)
217
+ }
218
+ }
@@ -1,23 +0,0 @@
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"
@@ -1,81 +0,0 @@
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
- }