fix(hooks): propagate stopHookActive in AfterAgent retry path (#20426)#20439
fix(hooks): propagate stopHookActive in AfterAgent retry path (#20426)#20439Aarchi-07 wants to merge 1 commit intogoogle-gemini:mainfrom
Conversation
…-gemini#20426) The AfterAgent hook's stop_hook_active field was never set to true on retries, causing hooks that rely on it to create infinite deny loops. Root cause: fireAfterAgentHookSafe called fireAfterAgentEvent without passing stopHookActive, and the activeCalls guard prevented the hook from firing on recursive retry calls. Fix: - Add stopHookActive parameter to fireAfterAgentHookSafe and sendMessageStream - Decrement activeCalls before retry recursion so the inner sendMessageStream fires AfterAgent again - Pass stopHookActive=true on the retry path so hooks receive stop_hook_active: true and can break the loop Fixes google-gemini#20426
Summary of ChangesHello @Aarchi-07, 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 bug where Highlights
Changelog
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request aims to fix an infinite loop in the AfterAgent hook retry path by propagating a stopHookActive flag. However, the implementation introduces a critical logic flaw by manually decrementing the activeCalls counter. This leads to a double-decrement, corrupting the counter (potentially making it negative or zero), and consequently preventing AfterAgent hooks from firing in subsequent turns of the same session, effectively disabling security guardrails. The recommended fix is to remove the manual decrement and adjust the firing condition in fireAfterAgentHookSafe to account for the stopHookActive flag.
|
This PR addresses a crucial issue with the AfterAgent hook retry logic. However, as noted by the automated code assist, manually decrementing Because To fix this, we should remove the manual decrement: // Remove this block
const retryHookState = this.hookStateMap.get(prompt_id);
if (retryHookState) {
retryHookState.activeCalls--;
}Instead, we should adjust the condition within For example, you could update private async fireAfterAgentHookSafe(
currentRequest: PartListUnion,
prompt_id: string,
turn?: Turn,
stopHookActive: boolean = false,
): Promise<DefaultHookOutput | undefined> {
const hookState = this.hookStateMap.get(prompt_id);
// Fire on the outermost call (when activeCalls is 1) OR if it's a retry (stopHookActive)
if (!hookState || (hookState.activeCalls !== 1 && !stopHookActive)) {
return undefined;
}
// ...Could you update the PR with this approach? |
Summary
The AfterAgent hook's stop_hook_active field was never set to true on retries, causing hooks that rely on it to create infinite deny loops.
Root cause: fireAfterAgentHookSafe called fireAfterAgentEvent without passing stopHookActive, and the activeCalls guard prevented the hook from firing on recursive retry calls.
Fix:
Related Issues
Fixes #20426
Pre-Merge Checklist