- Update ShellTool, WebFetchTool, and WebSearchTool to handle both Config and AgentLoopContext. - Add safety checks for config in ToolConfirmationMessage UI. - Add safety checks for context.config in policy update logic. - Fixes #23174
4.2 KiB
Implementation Plan - Fix Context Initialization Mismatch in Core Tools
The Gemini CLI is experiencing various "Cannot read properties of undefined" errors (notably isTrustedFolder and publish) during tool execution and confirmation. This is caused by a structural mismatch where the global Config object is passed to tool constructors that now expect an AgentLoopContext.
Background & Reproducibility Analysis
Root Cause Analysis (Regression)
The regression was introduced in commit de656f01d7 (PR #22115), titled "feat(core): Fully migrate packages/core to AgentLoopContext".
In this PR:
- Tool constructors (e.g.,
ShellTool,WebFetchTool,WebSearchTool) were updated to expect anAgentLoopContextinstead of aConfigobject. AgentLoopContextis an interface that includes a.configproperty.- However, in
packages/core/src/config/config.ts, the globalConfigobject still instantiates these tools by passingthis(theConfiginstance itself). - While
Configimplements some getters that overlap withAgentLoopContext(likegeminiClient), it does not have a.configproperty. - Consequently, internal tool calls to
this.context.config.someMethod()fail becausethis.context.configis undefined. - Additionally, some parts of the system expect a fully initialized
messageBuson the context, which can lead to "Cannot read properties of undefined (reading 'publish')" if the context isn't correctly structured or if there's a race in initialization.
Why this might not be reproducible for all users:
- Sub-agent Usage: Sub-agents correctly initialize tools with a full
AgentLoopContextviaLocalAgentExecutor. - Tool Selection: The bug only surfaces in tools that specifically access
context.configor certain context-scoped services. - Initialization Order: Depending on how the CLI is invoked (interactive vs. non-interactive), the
Configobject might be in different states of initialization.
Objective
Standardize tool initialization so that built-in tools can correctly resolve their required configuration and services whether they are running in the main loop (initialized with Config) or as a sub-agent (initialized with AgentLoopContext).
Key Files & Context
packages/core/src/config/config.ts: Where built-in tools are incorrectly instantiated withthis.packages/core/src/tools/shell.ts:ShellToolconstructor expectsAgentLoopContext.packages/core/src/tools/web-fetch.ts:WebFetchToolconstructor expectsAgentLoopContext.packages/core/src/tools/web-search.ts:WebSearchToolconstructor expectsAgentLoopContext.packages/core/src/scheduler/policy.ts: Accessescontext.config.isTrustedFolder().packages/core/src/scheduler/scheduler.ts: Initializes its ownmessageBusandcontext.
Implementation Steps
Phase 0: Preparation
- Update Plan: (This file) Broaden the scope from just "isTrusted" to "Context Mismatch".
- GitHub Issue: Update GitHub issue #23174 with the refined root cause analysis.
Phase 1: Robust Tool Context Handling
The built-in tools need to handle being initialized by either the global Config or an AgentLoopContext.
- Update
packages/core/src/tools/shell.ts,web-fetch.ts, andweb-search.ts:- Update constructors to accept
Config | AgentLoopContext. - Internalize logic to safely resolve
config,geminiClient, and other services. - Example:
this.config = 'config' in context ? context.config : context;
- Update constructors to accept
Phase 2: UI and Policy Hardening
- Update
packages/cli/src/ui/components/messages/ToolConfirmationMessage.tsx: Add safety checks forconfig. - Update
packages/core/src/scheduler/policy.ts: Ensurecontext.configexists before use.
Phase 3: Scheduler Verification
- Ensure
SchedulerandSchedulerStateManagerare receiving a correctly structuredmessageBus.
Verification & Testing
Automated Tests
- Create unit tests that specifically instantiate tools with a raw
Configobject and call methods that accessthis.config. - Verify
npm run test:corepasses.
Manual Verification
- Trigger shell commands in the interactive CLI.
- Attempt "Allow all" actions.
- Verify no "undefined" crashes occur.