Skip to content

feat: Add --dev flag to adk run for watchdog-based hot-reloading#4670

Open
notnotdurgesh wants to merge 8 commits intogoogle:mainfrom
notnotdurgesh:feat/hot-reloading
Open

feat: Add --dev flag to adk run for watchdog-based hot-reloading#4670
notnotdurgesh wants to merge 8 commits intogoogle:mainfrom
notnotdurgesh:feat/hot-reloading

Conversation

@notnotdurgesh
Copy link

@notnotdurgesh notnotdurgesh commented Mar 1, 2026

Link to Issue or Description of Change
Closes: #3679

Problem:
When developing and debugging agents in the ADK CLI, developers must continuously stop and restart the Python process every time they modify their .py tool definitions or .yaml prompt instructions. This creates friction and interrupts the flow.

Solution:
Implemented a robust --dev hot-reloading functionality modeled identically to standard Node/nodemon web environments.

Key features and optimizations include:

  • Watchdog Integration: Injects a DevModeChangeHandler that natively monitors the active agent's directory. If any .py or .yaml files change, it triggers an async threading event without restarting the CLI process.
  • Non-Blocking Daemon Input: Refactored sys.stdin.readline away from the standard input() lock. By throwing it onto a daemon thread and pushing lines into an asyncio.Queue, asyncio.wait(FIRST_COMPLETED) successfully multiplexes reading standard terminal input while simultaneously waiting for a watchdog reload event to fire.
  • Stateful Agent Swapping: When changed logic is detected, the old Runner gracefully closes, and AgentLoader.remove_agent_from_cache selectively drops all loaded modules corresponding to that agent folder. A new agent instance is instantiated securely alongside the already existing session_service and artifact_service bindings, resulting in code logic instantly updating midway through an existing chat thread without dropping the historical context.

Testing Plan

Unit Tests:

  • Added test_run_interactively_dev_reload in tests/unittests/cli/utils/test_cli.py which artificially mocks both stdin inputs and watchdog delays to mathematically prove the CLI intercepts the hot-replace loop successfully without losing any queries.
  • Ran pytest tests/unittests/cli/utils/test_cli.py to confirm all 14 tests, including our file and artifact loader modifications correctly report passes (..............) universally on Windows.

Manual End-to-End (E2E) Tests:

  • Booted adk create my_test_agent inside a sandbox.
  • Instantiated the session typing adk run --dev my_test_agent.
  • Verified the CLI outputs: Auto-reload enabled - watching for file changes...
  • Confirmed normal interactivity working.
  • Modified a print statement inside my_test_agent/agent.py and saved. Instantly visually validated the CLI safely paused blocking inputs, fired Changes detected, reloading agent..., flushed the Python cache, and correctly resumed listening to the exact same thread id.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context
N/A

Implemented real-time automatic agent reloading during interactive CLI sessions.

- Added --dev option to cli_tools_click.py.
- Integrated watchdog.events.FileSystemEventHandler in cli.py to monitor .py and .yaml changes.
- Refactored 
un_interactively to multiplex sys.stdin.readline inside a daemon thread alongside �syncio.wait(reload_event.wait()) for non-blocking reload.
- Used AgentLoader.remove_agent_from_cache to force module cache flushing and instantly reload logic while preserving session_service context.
- Added async unit tests in 	est_cli.py to cover multiplexing agent transitions.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 introduces a --dev flag to the adk run command, enabling hot-reloading functionality for agents during development. This significantly improves the developer experience by automatically detecting changes in agent definition files (.py and .yaml) and reloading the agent without requiring a manual restart of the CLI process, thus maintaining chat context and reducing friction.

Highlights

  • Watchdog Integration: Introduced a DevModeChangeHandler to monitor agent directories for .py or .yaml file changes, triggering an asynchronous reload event.
  • Non-Blocking Daemon Input: Refactored standard input handling to use a daemon thread and asyncio.Queue, allowing simultaneous input reading and watchdog event monitoring.
  • Stateful Agent Swapping: Implemented graceful closing of the old agent runner, selective module cache clearing, and instantiation of a new agent instance while preserving existing services and chat context.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/google/adk/cli/cli.py
    • Imported necessary modules for asynchronous operations and file system watching.
    • Defined DevModeChangeHandler for file change detection.
    • Updated run_interactively to support dev mode, including non-blocking input and agent reloading logic.
    • Modified run_cli to initialize and manage the watchdog observer for file monitoring.
    • Passed dev mode parameters through function calls.
  • src/google/adk/cli/cli_tools_click.py
    • Added a --dev command-line option to the adk run command.
    • Integrated the dev flag into the cli_run function and passed it to the core run_cli logic.
  • tests/unittests/cli/utils/test_cli.py
    • Added a new asynchronous unit test, test_run_interactively_dev_reload, to verify the hot-reloading mechanism, including input handling, event triggering, and agent cache manipulation.
Activity
  • Unit tests were added to cover the new hot-reloading functionality, specifically test_run_interactively_dev_reload, and all existing tests passed.
  • Manual end-to-end testing was performed by creating, running with --dev, modifying, and observing the agent reload behavior.
  • The author confirmed self-review, code commenting, and dependency checks.
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.

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Mar 1, 2026
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 introduces a --dev flag for hot-reloading, which is a great feature for improving the developer workflow. The implementation correctly uses watchdog for file monitoring and asyncio with a separate thread for non-blocking standard input, which are solid choices.

I've identified a couple of areas for improvement:

  • A high-severity bug in the error handling of the hot-reload logic that could crash the interactive session if an agent fails to load.
  • A medium-severity issue with a broad exception clause that could hide bugs in the input reading thread.

Addressing these points will make the new feature more robust. The addition of unit tests for the new functionality is also a positive contribution.

@notnotdurgesh
Copy link
Author

@gemini-code-assist review

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 introduces a --dev flag for hot-reloading agents, a significant improvement for the developer workflow. The implementation correctly uses watchdog for file monitoring and a threaded, non-blocking approach for terminal input with asyncio. My review includes suggestions to enhance the robustness of the file change detection by handling file deletions and moves, and a minor style improvement in the new unit tests to adhere to PEP 8 guidelines.

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 introduces a very useful --dev flag for hot-reloading, which will significantly improve the developer experience. The implementation using watchdog for file monitoring and asyncio with a separate thread for non-blocking input is well-designed. The agent reloading logic appears robust, gracefully handling failures and preserving session state. I have one suggestion to improve the file change detection to make it even more comprehensive.

@notnotdurgesh
Copy link
Author

@gemini-code-assist review

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 introduces a --dev flag for hot-reloading, which is a great enhancement for developer experience. The implementation uses watchdog for file monitoring and a combination of threading and asyncio to handle user input without blocking the event loop, which is a solid approach. The code is well-structured, with proper resource management for the file observer. I've identified a potential deadlock issue in the stdin reading thread that could cause the CLI to hang on EOF, and I've provided a suggestion to fix it. I also have a minor suggestion to improve code clarity by refactoring a magic value into a constant.

@notnotdurgesh
Copy link
Author

@gemini-code-assist review

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 introduces a --dev flag for hot-reloading, which is a great enhancement for the developer experience. The implementation using watchdog for file monitoring and asyncio with a separate thread for non-blocking input is robust and well-structured. The stateful agent swapping correctly preserves the session context while updating the agent's logic. The addition of a comprehensive unit test proves the reliability of the new functionality. I have one minor suggestion to improve the robustness of the file watching path logic.

@notnotdurgesh
Copy link
Author

@gemini-code-assist review

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 introduces a --dev flag for hot-reloading, which is a great feature for improving developer workflow. The implementation is robust, using watchdog for file monitoring and a non-blocking approach with asyncio and threading for handling user input. The stateful agent swapping preserves conversation history, which is a key part of the solution. The changes are well-tested with both unit and manual E2E tests. My feedback includes a couple of suggestions to improve code structure and maintainability by refactoring duplicated code and extracting complex logic into helper functions.

@notnotdurgesh
Copy link
Author

@gemini-code-assist review

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 introduces a --dev flag for hot-reloading, which is a great enhancement for the developer experience. The implementation correctly uses watchdog for file monitoring and a combination of threading and asyncio to handle non-blocking standard input and agent reloading. The code is well-structured, with proper resource management for the watchdog observer and graceful handling of reload errors. The new unit test effectively validates the core hot-reloading logic. I have one suggestion to improve code clarity by using nonlocal instead of a list reference.

@notnotdurgesh
Copy link
Author

@gemini-code-assist review

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 introduces a --dev flag to adk run for hot-reloading agents, which is a great enhancement for the developer experience. The implementation uses watchdog for file monitoring and cleverly combines asyncio with a separate thread for non-blocking stdin, which is a solid approach. The code is well-structured, with good separation of concerns and proper resource cleanup (like the Observer thread). I have one minor suggestion to improve exception logging for better debuggability.

@notnotdurgesh
Copy link
Author

Hi @ryanaiagent,

I’ve resolved the previous conflicts and created a new PR with the updated changes.

Kindly review it when you have a chance.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Development Mode with Auto-Reload for Agent Development (--dev flag)

2 participants