Skip to content

fix(pty): detect agents running inside a user's tmux session - #7799

Open
jooinjang wants to merge 1 commit into
stablyai:mainfrom
jooinjang:fix/tmux-hosted-agent-detection
Open

jooinjang wants to merge 1 commit into
stablyai:mainfrom
jooinjang:fix/tmux-hosted-agent-detection

Conversation

@jooinjang

@jooinjang jooinjang commented Jul 8, 2026 •

Copy link
Copy Markdown

What

Detect AI agents (e.g. claude, codex) that are running inside a user's tmux session so they show up as live/running on the pane, worktree, and project views.

Fixes #7797

Why

Live foreground detection walks the process tree downward from the pane's shell pid and only inspects that subtree (agent-foreground-process.ts locally, pty-shell-utils.ts on the relay).

When a user runs tmux in an Orca pane, the process in the pane is the tmux client. tmux daemonizes a server (reparented to pid 1), and every pane process — including claude — is a child of that server, not of the pane's shell. So the descendant walk reaches only the tmux client (not a recognized agent) and the pane reads as tmux.

How

When the shell's subtree contains a tmux client, hop to that client's active pane pid and re-run the same recognition walk:

  1. Detect a tmux client among the descendants.
  2. tmux [-L/-S <sock>] list-clients -F '#{client_pid} #{pane_pid}' → map our client pid to its active pane pid (one call; #{pane_pid} resolves in the client's context).
  3. Re-run the existing recognition from that pane pid, and also consider the pane's own top process (covers tmux new-session claude).
  • New shared module src/shared/tmux-active-pane.ts (isTmuxClientCommand, parseTmuxSocketArgs, resolveTmuxActivePanePid with a 500ms TTL cache so we don't fork tmux on every foreground poll).
  • Mirrored in the local and relay/SSH paths.
  • Best-effort: any tmux failure falls through to the previous behavior unchanged.
  • POSIX-only; Windows path untouched.

Testing

  • New unit tests for tmux-active-pane (parsing, socket selector, client→pane mapping, cache TTL, failure fallback).
  • New integration cases in agent-foreground-process.test.ts (client hop + fallback when no active pane).
  • Existing relay pty-shell-utils tests still pass (recognition logic extracted to recognizeAgentInSubtree, reused).
  • Verified end-to-end against real tmux 3.6 + real ps: agent detected both when it is a child of the pane shell and when it is the pane's top process.
  • oxlint and tsgo typecheck clean.

Notes

  • The file-based Agent Session History (AI Vault) already surfaced these transcripts by cwd; this PR only fixes the live/running indicator.

ELI5

Agents running inside your own tmux session didn’t show as live in Orca. Process detection now walks into tmux so Claude/Codex still appear as running.

Live foreground detection walks the process tree downward from the pane's
shell pid, but an agent launched inside tmux is a child of the reparented
tmux server (pid 1), not of the pane shell — so it was never reached and
the pane read as `tmux` instead of e.g. `claude`.

When the shell subtree holds a tmux client, hop to that client's active
pane pid (via `tmux list-clients -F '#{client_pid} #{pane_pid}'`) and
re-run the same recognition walk from there, also considering the pane's
own top process. Best-effort, POSIX-only, mirrored in the local and
relay/SSH paths, with a short TTL cache to avoid forking tmux per poll.

Fixes stablyai#7797
@coderabbitai

coderabbitai Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds a new shared module, src/shared/tmux-active-pane.ts, providing tmux client command detection, socket argument parsing, and cached resolution of a tmux client's active pane PID via tmux list-clients. This capability is integrated as a fallback into two existing foreground-agent detection code paths: resolveAgentForegroundProcess in the main process (via a new resolveTmuxHostedAgentProcess helper) and getRecognizedForegroundDescendant in relay pty-shell utilities (via a refactored recognizeAgentInSubtree helper). Both fallbacks attempt to detect a tmux client among descendant processes, resolve its active pane, and retry agent recognition relative to that pane. Corresponding unit tests are added for the new tmux module and for the updated resolution behaviors.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning It covers what/why/how/testing, but misses the required Summary, Screenshots, AI Review Report, and Security Audit sections. Add the missing template sections with a Summary, Screenshots/no visual change, an AI review report, and a basic security audit summary.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes implement tmux client hopping, active-pane resolution, best-effort fallback, and local/relay coverage required by #7797.
Out of Scope Changes check ✅ Passed The PR is tightly scoped to tmux foreground detection, tests, and shared helpers with no clear unrelated code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly summarizes the main change: detecting agents running inside a user's tmux session.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/main/providers/agent-foreground-process.ts (1)

76-105: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider extracting the tmux client-hop pattern to the shared module.

The "find tmux client → resolve active pane → retry recognition → check pane top process" sequence is duplicated between resolveTmuxHostedAgentProcess here and the tmux fallback in getRecognizedForegroundDescendant (src/relay/pty-shell-utils.ts lines 254-267). The only difference is the inner recognition function (resolveAgentForegroundProcessFromPs vs recognizeAgentInSubtree).

Extracting a shared helper that accepts the recognition function as a callback would eliminate this duplication. However, this depends on whether recognizeAgentProcessFromCommandLine and collectDescendants can be made available to both bundles — the relay bundle constraint may limit feasibility.

Source: Learnings


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 4b168d23-8df7-4672-b6a5-00f1e50e4e26

📥 Commits

Reviewing files that changed from the base of the PR and between 14ac945 and f40aa5c.

📒 Files selected for processing (5)
  • src/main/providers/agent-foreground-process.test.ts
  • src/main/providers/agent-foreground-process.ts
  • src/relay/pty-shell-utils.ts
  • src/shared/tmux-active-pane.test.ts
  • src/shared/tmux-active-pane.ts

@AmethystLiang
AmethystLiang requested a review from Jinwoo-H July 9, 2026 16:53
@AmethystLiang AmethystLiang assigned OrcaWin and unassigned Jinwoo-H Jul 20, 2026
@AmethystLiang
AmethystLiang requested review from OrcaWin and removed request for Jinwoo-H July 20, 2026 18:40
@nwparker nwparker added the bug Something isn't working label Jul 27, 2026

This branch has not been deployed

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Agents running inside a user's tmux session are not detected/shown as running

5 participants