fix(hooks): propagate stopHookActive in AfterAgent retry path (#20426)#20439
Conversation
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? |
…-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
8331a56 to
a6edea9
Compare
|
@scidomino Done. Please review again. |
scidomino
left a comment
There was a problem hiding this comment.
Please update the description to accurately reflect the new implemention and make the other change and I will approve.
|
Updated the description and inline comment, please take a look again @scidomino |
|
@Aarchi-07 You broke the E2E tests. Either fix the tests or fix your code. |
|
@scidomino on it 👍 |
|
Thank you for the feedback @scidomino! I thoroughly investigated the E2E failure and wanted to clarify what's happening before pushing a fix. There are two distinct issues:
I want to fix both by adjusting the test hook script so it blocks only on the first call and allows the retry, ensuring the test actually verifies the intended behavior. Would that be acceptable? Please let me know if I am missing anything. |
|
@Aarchi-07 Yes. That sounds like a solid way to fix the issue. Thanks |
- Move enabled:true from hooks to hooksConfig to fix schema validation error - Add 3rd fake response for retry LLM call triggered by block decision
|
Thank you! I'll get that updated right away. |
|
@scidomino Could you please approve the workflows so I can test the implementation? Thanks. |
… The afterAgentScript was hardcoded to always return 'block', causing an infinite retry loop once stopHookActive propagation was fixed in client.ts. Update the hook script to read stdin and return 'allow' when stop_hook_active is true, breaking the loop after one retry.
|
Hi @scidomino, I finally fixed the last piece of this puzzle! Previously, I updated the test config and the mock responses, but I missed the mock hook script itself. The But this time, I updated the script to read stdin and return Before vs. After (Local E2E)
I reproduced the CI failure locally before committing the fix and have verified that this specific test gracefully passes now. Apologies for the delay in getting these tests passing! |
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:
Test Fixes
Two pre-existing issues in the
clearContextAfterAgent E2E test were uncovered by this fix:enabled: truefrom insidehooksto insidehooksConfig(schema validation error on startup).Related Issues
Fixes #20426
Pre-Merge Checklist