Skip to content

fix(codex): detect sessions held open by a codex app-server - #185

Open
TheeCoderAhmed wants to merge 2 commits into
graykode:mainfrom
TheeCoderAhmed:fix/codex-newer-desktop-originators
Open

TheeCoderAhmed wants to merge 2 commits into
graykode:mainfrom
TheeCoderAhmed:fix/codex-newer-desktop-originators

Conversation

@TheeCoderAhmed

@TheeCoderAhmed TheeCoderAhmed commented Nov 3, 2026 •

Copy link
Copy Markdown

Fixes #179.

Newer Codex runs sessions through codex app-server, and some hosts write their own originator names. The Codex Desktop label no longer tells us what is live, so abtop showed 0 sessions.

Now a recent rollout held open by an app-server counts as live, whatever its originator. The filesystem scan still needs a Desktop originator, since it has no PID to match.

A rollout held by two app-servers shows once, under the lower PID.

Thanks to mschirrmeister for testing on Codex 0.167 and posting the patch. I applied it on top of this branch.

Tests: cargo test, clippy and rustfmt pass. Not run against live Codex here.

The desktop rollout filter only accepted the literal "Codex Desktop"
originator. Newer Codex versions are reported to write
"codex_work_desktop" and "codex_vscode", so their rollouts were filtered
out and abtop showed no sessions while the app-server was running.
@mschirrmeister

Copy link
Copy Markdown

Thanks for picking up #179. I tested this branch against a live Codex 0.167.0 setup on macOS, and it still finds no sessions there. Accepting more originator values doesn't help, because the originator isn't the right signal any more.

What's running

Three ways of using Codex, all sharing ~/.codex/sessions:

Client Process holding the rollout open originator in session_meta
ChatGPT desktop app ChatGPT.app/.../codex app-server Codex Desktop
A third-party host (T3 Code) codex app-server, spawned by the host T3 Code
Terminal codex (TUI) ~/.codex/packages/app-server-daemon/.../codex app-server --listen unix:// --managed-daemon codex-tui

On 0.167 the terminal codex process has no rollout open itself. The managed daemon writes the file and keeps it open. So:

  • The CLI path finds the codex TUI process, but lsof shows it holding no rollout.
  • The app-server path finds the daemon and its rollout, then drops it because the originator isn't on the list.

The desktop_rollout_filter_rejects_cli_originators test in this PR rejects codex-tui on purpose. That would be right if the TUI wrote its own rollout, but it doesn't any more. Third-party hosts write their own originator string, so any allow-list will keep missing some of them.

The originator also records only which client created a thread. Threads move between clients: I continued a thread started in the terminal (codex-tui) from the ChatGPT app. The ChatGPT app's app-server then held the rollout and wrote to it, but main and this branch still showed nothing, because the file says codex-tui. So an allow-list misses some Desktop activity too.

Results

The same live sessions, read by App::tick_no_summaries() over several ticks (so the async fd scan completes):

Session main (4b96568) this PR (a4b4cac) proposed change
T3 Code Codex thread (T3 Code) not shown not shown shown: pid of the host's app-server, live status
Terminal codex (codex-tui) not shown not shown shown: pid of the managed daemon, live status
Terminal-started thread continued in the ChatGPT app (codex-tui) not shown not shown shown: pid of the ChatGPT app's app-server
New ChatGPT app thread (Codex Desktop) shown (seen with release 0.5.5) not re-checked live unchanged code path; covered by the existing tests, not re-checked live

Proposed change

All of it is in src/collector/codex.rs:

  1. fd path: a rollout held open by a codex app-server process counts as live whatever its originator. It still has to be modified within ACTIVE_MTIME_SECS. This is the actual fix. The process holding the file is a better signal than the label.
  2. Filesystem scan: unchanged, still requires Codex Desktop. That path has no PID to attribute a file to. Relaxing it would turn every recent T3 Code or TUI rollout into an unknown-owner row.
  3. Shared files: Codex Desktop keeps other clients' rollouts open too. Candidates are now sorted by mtime and then PID, so a file held by two app-servers is shown once, under the same PID every tick. In practice a thread is shown under whichever client last opened it: the terminal thread above moved from the daemon's PID to the ChatGPT app's once the app opened it.

Tests: I updated active_desktop_rollouts_filters_stale_seen_and_cli_files, which encoded the old assumption. I added tests for non-Desktop originators held by an app-server (T3 Code, codex-tui, codex_vscode), for shared-file dedupe, and for the filesystem scan still requiring Codex Desktop. cargo test passes (226), cargo clippy --all-targets -- -D warnings is clean, and codex.rs is rustfmt-clean.

Two side effects I checked:

  • Kill: x on these rows can't kill a shared app-server, because is_killable_agent_command already refuses codex app-server.
  • PID and memory: TUI sessions show the daemon's PID, and every TUI session shares it. Memory shows — as for Desktop rows.

Cosmetic follow-up, not included: these rows show launch_surface as Cli. The originator could be used to label them, e.g. Desktop, the host's name, or TUI.

The patch is below, against main (4b96568). Happy to open it as a PR, or you can fold it into this one, whichever you and the maintainer prefer.

Patch (src/collector/codex.rs, +113/−14)
diff --git a/src/collector/codex.rs b/src/collector/codex.rs
index 68d9cbe..cfc4723 100644
--- a/src/collector/codex.rs
+++ b/src/collector/codex.rs
@@ -353,7 +353,7 @@ impl CodexCollector {
         }
     }

-    fn is_active_desktop_rollout(path: &Path, active_mtime_secs: u64) -> bool {
+    fn is_recently_modified(path: &Path, active_mtime_secs: u64) -> bool {
         let Ok(meta) = fs::metadata(path) else {
             return false;
         };
@@ -363,11 +363,22 @@ impl CodexCollector {
         let age = std::time::SystemTime::now()
             .duration_since(modified)
             .unwrap_or_default();
-        if age.as_secs() >= active_mtime_secs {
-            return false;
-        }
+        age.as_secs() < active_mtime_secs
+    }

-        parse_codex_jsonl(path).is_some_and(|result| result.is_codex_desktop())
+    /// Filesystem-scan filter. Without a PID to attribute the file to, only
+    /// rollouts that Codex Desktop wrote are trusted to be live.
+    fn is_active_desktop_rollout(path: &Path, active_mtime_secs: u64) -> bool {
+        Self::is_recently_modified(path, active_mtime_secs)
+            && parse_codex_jsonl(path).is_some_and(|result| result.is_codex_desktop())
+    }
+
+    /// fd-scan filter. A recent rollout held open by an `app-server` process
+    /// is live whatever its originator: third-party hosts (e.g. "T3 Code")
+    /// and the managed daemon behind the TUI ("codex-tui") also run through
+    /// `codex app-server`.
+    fn is_active_app_server_rollout(path: &Path, active_mtime_secs: u64) -> bool {
+        Self::is_recently_modified(path, active_mtime_secs) && parse_codex_jsonl(path).is_some()
     }

     fn active_desktop_rollouts(
@@ -380,11 +391,17 @@ impl CodexCollector {
             .into_iter()
             .flat_map(|(pid, paths)| paths.into_iter().map(move |path| (pid, path)))
             .collect();
-        candidates.sort_by_key(|(_, path)| {
-            std::cmp::Reverse(
-                fs::metadata(path)
-                    .and_then(|meta| meta.modified())
-                    .unwrap_or(std::time::UNIX_EPOCH),
+        // Several app-server hosts can hold the same rollout open (Codex
+        // Desktop keeps other hosts' threads open). Sort by PID as a tiebreak
+        // so the same PID claims a shared file on every tick.
+        candidates.sort_by_key(|(pid, path)| {
+            (
+                std::cmp::Reverse(
+                    fs::metadata(path)
+                        .and_then(|meta| meta.modified())
+                        .unwrap_or(std::time::UNIX_EPOCH),
+                ),
+                *pid,
             )
         });

@@ -395,7 +412,7 @@ impl CodexCollector {
                 !seen_jsonl.contains(path)
                     && !mcp_owned_rollouts.contains(path)
                     && emitted.insert(path.clone())
-                    && Self::is_active_desktop_rollout(path, active_mtime_secs)
+                    && Self::is_active_app_server_rollout(path, active_mtime_secs)
             })
             .collect()
     }
@@ -1820,7 +1837,7 @@ mod tests {
     }

     #[test]
-    fn active_desktop_rollouts_filters_stale_seen_and_cli_files() {
+    fn active_desktop_rollouts_filters_stale_and_seen_files() {
         let temp = tempfile::tempdir().unwrap();
         let active = temp.path().join("rollout-active.jsonl");
         let stale = temp.path().join("rollout-stale.jsonl");
@@ -1831,11 +1848,18 @@ mod tests {
         write_jsonl(&cli, &[SESSION_META]);
         write_jsonl(&seen, &[DESKTOP_SESSION_META]);
         set_modified(&stale, SystemTime::now() - Duration::from_secs(31 * 60));
+        set_modified(&cli, SystemTime::now() - Duration::from_secs(60));

         let mut pid_to_rollouts = HashMap::new();
         pid_to_rollouts.insert(
             99,
-            vec![active.clone(), stale, cli, seen.clone(), active.clone()],
+            vec![
+                active.clone(),
+                stale,
+                cli.clone(),
+                seen.clone(),
+                active.clone(),
+            ],
         );
         let seen_jsonl = HashSet::from([seen]);

@@ -1846,7 +1870,82 @@ mod tests {
             super::super::mcp::ACTIVE_MTIME_SECS,
         );

-        assert_eq!(rollouts, vec![(99, active)]);
+        // A file held open by an app-server counts even without a Desktop
+        // originator; only staleness and prior claims filter it out.
+        assert_eq!(rollouts, vec![(99, active), (99, cli)]);
+    }
+
+    #[test]
+    fn active_desktop_rollouts_accept_non_desktop_app_server_originators() {
+        let temp = tempfile::tempdir().unwrap();
+        let mut held = Vec::new();
+        for (i, originator) in ["T3 Code", "codex-tui", "codex_vscode"].iter().enumerate() {
+            let path = temp.path().join(format!("rollout-{i}.jsonl"));
+            write_jsonl(
+                &path,
+                &[&DESKTOP_SESSION_META.replace("Codex Desktop", originator)],
+            );
+            set_modified(
+                &path,
+                SystemTime::now() - Duration::from_secs(60 * (i as u64 + 1)),
+            );
+            held.push(path);
+        }
+
+        let mut pid_to_rollouts = HashMap::new();
+        pid_to_rollouts.insert(7, held.clone());
+
+        let rollouts = CodexCollector::active_desktop_rollouts(
+            pid_to_rollouts,
+            &HashSet::new(),
+            &HashSet::new(),
+            super::super::mcp::ACTIVE_MTIME_SECS,
+        );
+
+        let expected: Vec<(u32, PathBuf)> = held.into_iter().map(|p| (7, p)).collect();
+        assert_eq!(rollouts, expected);
+    }
+
+    #[test]
+    fn active_desktop_rollouts_emit_shared_file_once_with_stable_pid() {
+        let temp = tempfile::tempdir().unwrap();
+        let shared = temp.path().join("rollout-shared.jsonl");
+        write_jsonl(
+            &shared,
+            &[&DESKTOP_SESSION_META.replace("Codex Desktop", "T3 Code")],
+        );
+
+        let mut pid_to_rollouts = HashMap::new();
+        pid_to_rollouts.insert(300, vec![shared.clone()]);
+        pid_to_rollouts.insert(200, vec![shared.clone()]);
+
+        let rollouts = CodexCollector::active_desktop_rollouts(
+            pid_to_rollouts,
+            &HashSet::new(),
+            &HashSet::new(),
+            super::super::mcp::ACTIVE_MTIME_SECS,
+        );
+
+        assert_eq!(rollouts, vec![(200, shared)]);
+    }
+
+    #[test]
+    fn filesystem_scan_still_requires_desktop_originator() {
+        let temp = tempfile::tempdir().unwrap();
+        let t3 = temp.path().join("rollout-t3.jsonl");
+        write_jsonl(
+            &t3,
+            &[&DESKTOP_SESSION_META.replace("Codex Desktop", "T3 Code")],
+        );
+
+        assert!(!CodexCollector::is_active_desktop_rollout(
+            &t3,
+            super::super::mcp::ACTIVE_MTIME_SECS
+        ));
+        assert!(CodexCollector::is_active_app_server_rollout(
+            &t3,
+            super::super::mcp::ACTIVE_MTIME_SECS
+        ));
     }

     #[test]

Investigated, patched and tested with the help of Claude (Claude Code).

Newer Codex versions run terminal sessions through a managed codex app-server daemon, and third-party hosts write their own originator strings, so the originator is no longer a reliable signal. A recent rollout held open by an app-server process now counts whatever its originator. The filesystem scan still requires a Desktop originator, because it has no PID to attribute the file to. A rollout held by two app-servers is shown once, under the lower PID, so the row does not flip between ticks.

Based on the patch mschirrmeister posted on this PR after testing against Codex 0.167.0 on macOS.
@TheeCoderAhmed TheeCoderAhmed changed the title fix(codex): detect app-server sessions with newer originator values fix(codex): detect sessions held open by a codex app-server Nov 10, 2026
@TheeCoderAhmed

Copy link
Copy Markdown
Author

Thanks for testing this and posting the patch. I applied it on top of this branch and credited you in the PR. I can't run Codex 0.167 here, so tell me if it still shows nothing.

@mschirrmeister

Copy link
Copy Markdown

Thanks for applying it! I tested 8f11352 against Codex 0.167.1 on macOS. Every client now shows up:

Session Held open by originator main 8f11352
New ChatGPT app chat, Codex mode ChatGPT app's app-server Codex Desktop shown shown
New ChatGPT app chat, ChatGPT Work mode ChatGPT app's app-server codex_work_desktop not shown shown
T3 Code Codex thread host's codex app-server T3 Code not shown shown
New terminal codex session managed daemon (app-server --managed-daemon) codex-tui not shown shown, under the daemon's PID
Terminal-started thread continued in the ChatGPT app ChatGPT app's app-server codex-tui not shown shown, under the app's PID

This also confirms codex_work_desktop from #179 is real: ChatGPT Work mode writes it. cargo test (227), clippy -D warnings and rustfmt on codex.rs are clean on the branch.


Tested with the help of Claude (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex Work/Desktop sessions are not detected with newer originator values

2 participants