3.5 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). - Critical Discovery: While
Configimplements the properties ofAgentLoopContext, it previously did so using getters. We discovered that in various parts of the system (likeLocalAgentExecutor,Scheduler, and telemetry loggers), the context is cloned using the spread operator (e.g.,{...context}). Because getters exist on the prototype and are not enumerable own properties, they are lost during spreading. - Consequently, internal tool calls to
this.context.config.someMethod()fail becausethis.context.configbecomesundefinedafter a spread operation.
Objective
Standardize tool initialization and ensure the Config class is "spread-safe"
so that it correctly satisfies the AgentLoopContext interface even after being
cloned via object spread.
Key Files & Context
packages/core/src/config/config.ts: Refactored to use properties instead of getters forAgentLoopContextcompatibility.packages/core/src/scheduler/policy.ts: Updated to handle potential context mismatches.packages/core/src/tools/shell.ts,web-fetch.ts,web-search.ts: Refactored constructors to take direct dependencies and handle context safely.
Implementation Steps
Phase 1: Centralized "Spread-Safe" Config
Instead of applying surgical fixes to every tool, we refactored the Config
class to ensure its AgentLoopContext implementation survives cloning.
- Update
packages/core/src/config/config.ts:- Converted
config,promptId,toolRegistry,messageBus,geminiClient, andsandboxManagerfrom getters to actual properties. - Ensured these are initialized in the constructor or
initialize()method.
- Converted
Phase 2: Tool and UI Resilience
- Update Built-in Tools: Refactored
ShellTool,WebFetchTool, andWebSearchToolto be more robust during initialization. - Update UI: Added safety checks in
ToolConfirmationMessage.tsxto handle cases where theconfigobject might still be partially initialized.
Phase 3: Policy Hardening
- Update
packages/core/src/scheduler/policy.ts: Added optional chaining and guards when accessingcontext.configto prevent crashes during policy updates.
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.