claude-code - 💡(How to fix) Fix Subagents sometimes read node_modules instead of source files [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#55146Fetched 2026-05-01 05:45:04
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
labeled ×3commented ×1

When spawning subagents (via the Agent tool) to perform bulk file edits across a codebase, subagents sometimes start reading files in node_modules/ instead of the source files they were instructed to modify. This appears to happen when the agent is looking up component APIs or type definitions — it resolves the import path into node_modules and starts reading package internals rather than staying within the src/ directory it was scoped to.

Root Cause

The parent agent has no real-time visibility into what a background subagent is doing. I only noticed because I was watching the terminal output and saw node_modules paths scrolling by. The parent agent had no way to detect or prevent this until I flagged it manually, at which point I sent a SendMessage to redirect them. By that time they had already burned significant context and token budget reading package internals.

RAW_BUFFERClick to expand / collapse

Description

When spawning subagents (via the Agent tool) to perform bulk file edits across a codebase, subagents sometimes start reading files in node_modules/ instead of the source files they were instructed to modify. This appears to happen when the agent is looking up component APIs or type definitions — it resolves the import path into node_modules and starts reading package internals rather than staying within the src/ directory it was scoped to.

Context

I was using Claude Code (Opus) to migrate an admin app from raw Chakra UI imports to a custom component library (@televet/kibble-ui). I spawned two parallel subagents with detailed instructions listing specific source files to modify under apps/admin/src/. Both agents began reading files in node_modules/@televet/kibble-ui/ and node_modules/@chakra-ui/ to understand component APIs — even though the prompt already contained the full API documentation they needed and explicitly listed only src/ files to modify.

Visibility concern

The parent agent has no real-time visibility into what a background subagent is doing. I only noticed because I was watching the terminal output and saw node_modules paths scrolling by. The parent agent had no way to detect or prevent this until I flagged it manually, at which point I sent a SendMessage to redirect them. By that time they had already burned significant context and token budget reading package internals.

Expected behavior

Subagents should stay within the file scope described in their prompt. When a prompt says "modify ONLY these files under src/", the agent should not autonomously decide to explore node_modules/ for supplementary information — especially when that information was already provided in the prompt.

Suggested improvements

  • A way to scope subagent file access (e.g. allowedPaths parameter on the Agent tool)
  • Or at minimum, a heuristic that discourages reading node_modules/ unless explicitly asked
  • Better parent visibility into subagent activity (e.g. a way to see what tools a background agent is calling)

Environment

  • Claude Code CLI
  • Model: Opus 4.6 (1M context)
  • Platform: macOS

extent analysis

TL;DR

To prevent subagents from reading files in node_modules/, consider adding an allowedPaths parameter to the Agent tool to scope file access.

Guidance

  • Review the prompt instructions for subagents to ensure they only reference files within the intended scope (src/ directory).
  • Verify that the prompt includes all necessary API documentation to minimize the need for subagents to explore node_modules/.
  • Consider implementing a heuristic to discourage subagents from reading node_modules/ unless explicitly instructed to do so.
  • Improve parent agent visibility into subagent activity to detect and prevent unwanted file access.

Example

No code snippet is provided as the issue does not contain specific code references.

Notes

The suggested improvements, such as adding an allowedPaths parameter or implementing a heuristic, may require modifications to the Agent tool or the Claude Code CLI.

Recommendation

Apply a workaround by carefully reviewing and refining prompt instructions to minimize the need for subagents to access node_modules/, until a more permanent solution, such as adding an allowedPaths parameter, can be implemented.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Subagents should stay within the file scope described in their prompt. When a prompt says "modify ONLY these files under src/", the agent should not autonomously decide to explore node_modules/ for supplementary information — especially when that information was already provided in the prompt.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING