Skip to content

fix(cli): lazily load settings in onModelChange to prevent stale closure data loss#20403

Open
KumarADITHYA123 wants to merge 2 commits intogoogle-gemini:mainfrom
KumarADITHYA123:fix-stale-settings-closure
Open

fix(cli): lazily load settings in onModelChange to prevent stale closure data loss#20403
KumarADITHYA123 wants to merge 2 commits intogoogle-gemini:mainfrom
KumarADITHYA123:fix-stale-settings-closure

Conversation

@KumarADITHYA123
Copy link

OVERVIEW This PR structurally resolves a state-corruption defect inside loadCliConfig in packages/cli.

Previously, the onModelChange callback permanently, statically captured the loadedSettings object at boot time. If the CLI hot-reloaded or the config mutated on disk at runtime, any subsequent /model change would blindly write back to the .gemini file using outdated memory pointers, clobbering the user's latest config state.

FIX IMPLEMENTATION I converted the static memory capture into a dynamic, lazy-loaded functional request, mirroring the proven architecture of onReload:

Before: onModelChange: (model: string) => saveModelChange(loadedSettings, model) After: onModelChange: (model: string) => saveModelChange(loadSettings(cwd), model)

By decoupling this, we also permanently eliminated the redundant loadSettings(cwd) file system read at boot time. The unused loadedSettings variable definition was pruned to strictly satisfy ESLint no-unused-vars constraints. Both loadSettings(cwd) and saveModelChange remain fully synchronous, matching the original signature (model: string) => void.

TESTING

Verified strict function signature typing satisfies the backend backend requirements.
Verified local test suites pass flawlessly with the functional decoupling.
Fixes #20402

@KumarADITHYA123 KumarADITHYA123 requested a review from a team as a code owner February 26, 2026 08:03
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @KumarADITHYA123, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical state-corruption defect in the CLI's configuration loading mechanism. By refactoring the onModelChange callback to lazily load settings, it prevents data loss that previously occurred when the CLI hot-reloaded or the configuration mutated on disk. This change ensures that onModelChange always operates with the most current configuration data, while also optimizing performance by removing an unnecessary initial file system read.

Highlights

  • Defect Resolution: Structurally resolved a state-corruption defect within the loadCliConfig function in packages/cli, preventing data loss from stale configuration data.
  • Lazy Loading Implementation: Converted the onModelChange callback to dynamically and lazily load settings using loadSettings(cwd) at the time of change, rather than statically capturing an initial state.
  • Performance Optimization: Eliminated a redundant loadSettings(cwd) file system read that previously occurred at boot time, improving application efficiency.
  • Code Clean-up: Pruned the unused loadedSettings variable definition to satisfy ESLint no-unused-vars constraints, enhancing code maintainability.
Changelog
  • packages/cli/src/config/config.ts
    • Removed the initial assignment of the loadedSettings variable.
    • Modified the onModelChange callback to directly invoke saveModelChange with loadSettings(cwd).
Activity
  • No human activity has occurred on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a state corruption issue where onModelChange would use stale settings data captured at boot time. The fix is to lazily load the settings within the onModelChange callback, ensuring that the most recent configuration is always used when saving a model change. This change correctly resolves the bug and has the added benefit of removing a settings load from the startup path, which should improve initialization time. The implementation is consistent with the existing onReload handler, which uses a similar lazy-loading pattern.

@gemini-cli gemini-cli bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Feb 26, 2026
@gemini-cli gemini-cli bot added priority/p2 Important but can be addressed in a future release. help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Stale capture of loadedSettings in onModelChange leads to config corruption after hot-reloads

1 participant