Skip to content

Skip monitor auto-start inside agent/teammate Claude Code processes - #5

Closed
sophieow wants to merge 1 commit into
yilunzhang:mainfrom
sophieow:agent-context-guard
Closed

Skip monitor auto-start inside agent/teammate Claude Code processes#5
sophieow wants to merge 1 commit into
yilunzhang:mainfrom
sophieow:agent-context-guard

Conversation

@sophieow

@sophieow sophieow commented Jul 8, 2026

Copy link
Copy Markdown

Problem

monitors.json when:"always" auto-starts client.py in every Claude Code process — including agent-team teammates / subagents spawned as child claude processes (claude --agent-id worker@team --parent-session-id <sid> …, argv[0] sometimes the versioned install path). With a 30-worker fleet we observed:

  • one bus listener per worker, auto-named from cwd into collision chains (myproject, myproject-2, myproject-2-2, …) with no human session behind them;
  • broadcast reaching mid-task workers — the reaction policy treats incoming text as an instruction, a real derail hazard (mirrors the concern behind the skill's own SUBAGENT-STOP header).

Fix

_in_agent_context(): walk up to 15 ancestors; return True when an ancestor is a claude binary (argv[0] claude* or the versioned install path …/claude/versions/<v>) carrying --agent-id / --parent-session-id as exact argv tokens — a shell ancestor merely quoting that text must not match. main() skips only the auto-start path (no --name); an explicit --name still connects an agent deliberately. Fail-open on any error (psutil missing, AccessDenied mid-walk) so the normal connect path is never blocked.

Testing

  • 9 unit tests in tests/test_agent_guard.py; full suite 208 passed.
  • Live-verified on macOS, Claude Code 2.1.204/2.1.205: real teammate processes carry the flags as separate argv tokens (versioned-path argv[0]); interactive sessions run as bare claude and are not matched; a bash -c ancestor quoting the flag text is not matched.

🤖 Generated with Claude Code

The plugin's monitors.json when:"always" auto-starts client.py in every
Claude Code process — including agent-team teammates / subagents spawned
as child claude processes. Each worker then joins the bus with a
cwd-derived name (roster collision chains with no human session behind
them), and a broadcast reaches mid-task workers, whose reaction policy
treats incoming text as an instruction.

_in_agent_context() walks up to 15 ancestors and returns True when one is
a claude binary (argv[0] "claude"* or the versioned install path
.../claude/versions/<v>) carrying --agent-id / --parent-session-id as
exact argv tokens; main() skips only the auto-start path (no --name), so
an explicit --name still connects an agent deliberately. Fail-open on any
error. 9 unit tests added; full suite 208 passed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@sophieow sophieow closed this by deleting the head repository Aug 31, 2026
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.

1 participant