Skip to content

fix(hooks): skip file-context injection for subagents without the MCP tools (closes #3324)#3341

Open
rodboev wants to merge 5 commits into
thedotmack:mainfrom
rodboev:pr/3324-subagent-file-context-skip
Open

fix(hooks): skip file-context injection for subagents without the MCP tools (closes #3324)#3341
rodboev wants to merge 5 commits into
thedotmack:mainfrom
rodboev:pr/3324-subagent-file-context-skip

Conversation

@rodboev

@rodboev rodboev commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

The file-context Read hook currently injects main-session MCP guidance into subagents even when those subagents cannot use the referenced tools. This change mirrors the existing summarize-hook policy by skipping file-context injection whenever agentId is present, while leaving main-session Reads and agent-type-only inputs unchanged.

Closes #3324

Why

The repository already treats agentId as the authoritative subagent discriminator in summarize.ts. file-context.ts simply never adopted the same guard, so it still performs timeline lookup and output injection for subagent Reads. The smallest correct fix is to mirror that existing policy boundary, including the debug breadcrumb that identifies the skip and its agent context.

Scope

This PR changes file-context.ts, its focused tests, and the generated hook bundles changed by npm run build: plugin/scripts/mcp-server.cjs, plugin/scripts/server-service.cjs, and plugin/scripts/worker-service.cjs. It does not change summarize behavior, observation storage, SessionStart context, generated UI assets, or broader configuration for subagent directionality.

Risk

Low. The new guard keys on the same agentId field already used by another hook handler. Main-session Reads, agent-type-only inputs, excluded-project skips, and empty-path skips remain unchanged.

Verification

  • bun test tests/hooks/file-context.test.ts
  • npm run typecheck
  • npm run build
  • Agent skip breadcrumb
  • npm run lint:hook-io
  • npm run lint:spawn-env
  • npm run strip-comments:check matched the 316-file check-mode baseline

Closes #3324

@greptile-apps

greptile-apps Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR updates file-context handling so subagent reads do not receive main-session MCP guidance. The main changes are:

  • Adds an early agentId guard in fileContextHandler.
  • Keeps main-session reads and agentType-only inputs eligible for file-context injection.
  • Adds focused tests for subagent skip behavior and unchanged main-session behavior.
  • Restores several Bun module mocks after test suites to avoid cross-test leakage.
  • Regenerates bundled plugin scripts from the updated source.

Confidence Score: 5/5

Safe to merge with minimal risk.

The functional change is a small early-return guard aligned with the existing summarize hook pattern. Tests cover subagent inputs, main-session inputs, and agent-type-only inputs. No blocking correctness or security issues were identified.

No files require special attention.

T-Rex T-Rex Logs

What T-Rex did

  • Ran the file-context-hook test suite and verified exact outcome: 12 pass, 0 fail, 28 expect() calls, and exit status 0.

View all artifacts

T-Rex Ran code and verified through T-Rex

Important Files Changed

Filename Overview
src/cli/handlers/file-context.ts Adds an early agentId guard so subagent hook inputs skip file-context timeline lookup and context injection.
tests/hooks/file-context.test.ts Adds focused coverage for subagent skip behavior, main-session injection, and agent-type-only inputs.
plugin/scripts/mcp-server.cjs Regenerates bundled plugin code to include the file-context subagent skip and related build output changes.
plugin/scripts/server-service.cjs Regenerates server service bundle with the updated hook behavior and current build output.
plugin/scripts/worker-service.cjs Regenerates worker service bundle with the updated hook behavior and current build output.
tests/services/sync/chroma-mcp-manager-singleton.test.ts Restores MCP SDK transport/client mocks after singleton tests to keep later suites isolated.
tests/worker/agents/response-processor.test.ts Restores worker service, worker utils, and mode manager mocks after response processor tests.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
participant Hook as fileContextHandler
participant Logger as logger
participant FS as stat/file gate
participant Worker as worker observations API

Hook->>Hook: receive normalized hook input
alt input.agentId present
    Hook->>Logger: debug skip with session/agent context
    Hook-->>Hook: return continue + suppressOutput
else main session or agentType only
    Hook->>Hook: collect file_path/filePaths
    Hook->>FS: validate candidate files and size gate
    Hook->>Worker: fetch observations by file candidates
    Worker-->>Hook: observations
    Hook-->>Hook: return additionalContext when timelines exist
end
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
participant Hook as fileContextHandler
participant Logger as logger
participant FS as stat/file gate
participant Worker as worker observations API

Hook->>Hook: receive normalized hook input
alt input.agentId present
    Hook->>Logger: debug skip with session/agent context
    Hook-->>Hook: return continue + suppressOutput
else main session or agentType only
    Hook->>Hook: collect file_path/filePaths
    Hook->>FS: validate candidate files and size gate
    Hook->>Worker: fetch observations by file candidates
    Worker-->>Hook: observations
    Hook-->>Hook: return additionalContext when timelines exist
end
Loading

Reviews (4): Last reviewed commit: "test(sync): restore MCP client modules a..." | Re-trigger Greptile

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.

File-read gate injects context into subagents that don't have the MCP tools

1 participant