Skip to content

fix(agent-detection): detect agents running inside a user's tmux session (#7797) - #10009

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

genx7up wants to merge 1 commit into
stablyai:mainfrom
genx7up:fix/7797-tmux-agent-detection

Conversation

@genx7up

@genx7up genx7up commented Jul 22, 2026 •

Copy link
Copy Markdown

What

Agents (claude, codex, …) started inside a user's tmux session are never
shown as running — the pane reads as tmux. Fixes #7797 (both the daemon and the
relay/SSH detection paths).

Root cause

Live foreground detection walks the process tree downward from the pane's
shell pid (collectDescendants). tmux double-forks its server and reparents it
to pid 1
, so every pane process — including the agent — is a child of the tmux
server, not of the pane shell. The downward walk from the shell pid reaches only
the tmux client and falls back to reporting tmux. The relay twin
(src/relay/pty-shell-utils.ts) has the same downward-only walk, so remote
sessions are affected identically.

Fix

When the downward walk finds a tmux client but no agent, hop the fork and
re-run recognition from the client's pane:

  1. Precise — map each client in our subtree to its active pane pid via
    tmux list-clients -F '#{client_pid} #{pane_pid}', then re-walk from there.
  2. Fallback — if tmux is unreachable (wrong socket, not on PATH, no server),
    walk from the reparented tmux server pid(s) visible in the ps table.

Best-effort and POSIX-only; every failure mode returns null so callers keep
their existing fallback. Logic lives in a new
src/shared/tmux-foreground-resolution.ts and is wired into both detection paths.
The relay path additionally stops treating tmux as a trusted foreground name so
the pane is actually inspected.

Tests

New regression tests model the exact tmux topology (shell → tmux client; tmux
server reparented to pid 1 → claude) and cover both the precise client→pane hop
and the reparented-server fallback, for both the daemon
(src/main/providers/tmux-agent-detection.test.ts) and relay
(src/relay/tmux-agent-detection.test.ts) paths.

  • vitest run (touched suites): 5/5 pass
  • tsc --noEmit -p config/tsconfig.node.json: clean
  • oxlint / oxfmt: clean

Supersedes the repro artifact from nwparker/bug-repro-pass2.

🤖 Generated with Claude Code

ELI5

Agents started inside your own tmux session never showed as running—the pane just looked like tmux. Detection now finds agents reparented under the tmux server so they appear correctly in the sidebar.

…ion (stablyai#7797)

Live foreground detection walked the process tree downward from the pane's shell pid, but tmux double-forks its server and reparents it to pid 1, so the agent (claude/codex/...) is a child of the tmux SERVER, outside the walked subtree. The walk reached only the tmux client and reported tmux. The relay/SSH path had the same downward-only walk.

Hop the fork: map the tmux client to its active pane pid via tmux list-clients, else fall back to the reparented tmux server pid from the ps table, then re-run recognition from there. Best-effort, POSIX-only. Wired into both the daemon (agent-foreground-process) and relay (pty-shell-utils) paths via a new shared helper; the relay path also stops trusting tmux as a foreground name so the pane is inspected.

Adds regression tests covering the precise client-to-pane hop and the reparented-server fallback for both paths.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds shared tmux-aware foreground-agent resolution for double-forked and reparented process trees. Main and relay process detection now recognize tmux clients, query active pane PIDs, fall back to reparented tmux servers, and avoid trusting tmux commands directly. New regression tests simulate tmux availability, unavailable tmux, client-to-pane mapping, server reparenting, and direct server-root detection.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the change and tests, but it misses the required screenshots, AI review, security audit, and notes sections. Rewrite it to match the template with Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes sections filled in.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: adding tmux-aware agent detection for issue #7797.
Linked Issues check ✅ Passed The code adds tmux client-to-pane resolution, fallback to reparented server PIDs, relay support, and regression tests as required by #7797.
Out of Scope Changes check ✅ Passed The changes stay focused on tmux detection logic and tests, with no unrelated features or broad refactors.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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 (2)
src/shared/tmux-foreground-resolution.ts (1)

19-43: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Duplicate tree-walk/scoring logic vs agent-foreground-process.ts.

collectDescendants/candidateScore here re-implement the same generic descendant-walk and +/depth scoring already defined in src/main/providers/agent-foreground-process.ts (lines 22-50, generic over {pid, ppid}). Consider having this shared resolver import/reuse that generic version instead of maintaining a second copy that can silently drift (e.g., if the scoring heuristic is tuned in one file but not the other).

src/main/providers/tmux-agent-detection.test.ts (1)

64-68: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Restore process.platform after stubbing it. Both suites redefine process.platform in beforeEach but never restore the original value, so if Vitest reuses a worker process for other test files in the same run, the darwin override can leak into unrelated suites that rely on the real platform.

  • src/main/providers/tmux-agent-detection.test.ts#L64-L68: capture the original process.platform before overriding and restore it in afterEach.
  • src/relay/tmux-agent-detection.test.ts#L55-L59: same fix for the relay suite.
♻️ Proposed fix (apply to both files)
+  const originalPlatform = process.platform
   beforeEach(() => {
     execFileMock.mockReset()
     resetProcessTableSnapshotForTests()
     Object.defineProperty(process, 'platform', { value: 'darwin' })
   })
+  afterEach(() => {
+    Object.defineProperty(process, 'platform', { value: originalPlatform })
+  })

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 4a525c78-5df2-426a-acd6-2c52dca16822

📥 Commits

Reviewing files that changed from the base of the PR and between b63d4cd and 7b6800c.

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

@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

3 participants