Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions integration-tests/hooks-agent-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,22 @@ describe('Hooks Agent Flow', () => {
);

const afterAgentScript = `
console.log(JSON.stringify({
decision: 'block',
reason: 'Security policy triggered',
hookSpecificOutput: {
hookEventName: 'AfterAgent',
clearContext: true
}
}));
const fs = require('fs');
const input = JSON.parse(fs.readFileSync(0, 'utf-8'));
if (input.stop_hook_active) {
// Retry turn: allow execution to proceed (breaks the loop)
console.log(JSON.stringify({ decision: 'allow' }));
} else {
// First call: block and clear context to trigger the retry
console.log(JSON.stringify({
decision: 'block',
reason: 'Security policy triggered',
hookSpecificOutput: {
hookEventName: 'AfterAgent',
clearContext: true
}
}));
}
`;
const afterAgentScriptPath = rig.createScript(
'after_agent_clear.cjs',
Expand All @@ -198,8 +206,10 @@ describe('Hooks Agent Flow', () => {

rig.setup('should process clearContext in AfterAgent hook output', {
settings: {
hooks: {
hooksConfig: {
enabled: true,
},
hooks: {
BeforeModel: [
{
hooks: [
Expand Down
1 change: 1 addition & 0 deletions integration-tests/hooks-system.after-agent.responses
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Hi there!"}],"role":"model"},"finishReason":"STOP","index":0}]}]}
{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Clarification: I am a bot."}],"role":"model"},"finishReason":"STOP","index":0}]}]}
{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Security policy triggered"}],"role":"model"},"finishReason":"STOP","index":0}]}]}
18 changes: 18 additions & 0 deletions packages/core/src/core/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,7 @@ ${JSON.stringify(
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenCalledWith(
partToString(request),
'Hook Response',
false,
);

// Map should be empty
Expand Down Expand Up @@ -3341,6 +3342,7 @@ ${JSON.stringify(
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenCalledWith(
partToString(request),
'Response 1\nResponse 2',
false,
);

expect(client['hookStateMap'].size).toBe(0);
Expand Down Expand Up @@ -3371,6 +3373,7 @@ ${JSON.stringify(
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenCalledWith(
partToString(request), // Should be 'Do something'
expect.stringContaining('Ok'),
false,
);
});

Expand Down Expand Up @@ -3541,6 +3544,21 @@ ${JSON.stringify(
expect.anything(),
undefined,
);

// First call should have stopHookActive=false, retry should have stopHookActive=true
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenCalledTimes(2);
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenNthCalledWith(
1,
expect.any(String),
expect.any(String),
false,
);
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenNthCalledWith(
2,
expect.any(String),
expect.any(String),
true,
);
});

it('should call resetChat when AfterAgent hook returns shouldClearContext: true', async () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ export class GeminiClient {
currentRequest: PartListUnion,
prompt_id: string,
turn?: Turn,
stopHookActive: boolean = false,
): Promise<DefaultHookOutput | undefined> {
const hookState = this.hookStateMap.get(prompt_id);
// Only fire on the outermost call (when activeCalls is 1)
if (!hookState || hookState.activeCalls !== 1) {
if (!hookState || (hookState.activeCalls !== 1 && !stopHookActive)) {
return undefined;
}

Expand All @@ -210,7 +211,11 @@ export class GeminiClient {

const hookOutput = await this.config
.getHookSystem()
?.fireAfterAgentEvent(partToString(finalRequest), finalResponseText);
?.fireAfterAgentEvent(
partToString(finalRequest),
finalResponseText,
stopHookActive,
);

return hookOutput;
}
Expand Down Expand Up @@ -845,6 +850,7 @@ export class GeminiClient {
turns: number = MAX_TURNS,
isInvalidStreamRetry: boolean = false,
displayContent?: PartListUnion,
stopHookActive: boolean = false,
): AsyncGenerator<ServerGeminiStreamEvent, Turn> {
if (!isInvalidStreamRetry) {
this.config.resetTurn();
Expand Down Expand Up @@ -909,6 +915,7 @@ export class GeminiClient {
request,
prompt_id,
turn,
stopHookActive,
);

// Cast to AfterAgentHookOutput for access to shouldClearContext()
Expand Down Expand Up @@ -954,6 +961,7 @@ export class GeminiClient {
boundedTurns - 1,
false,
displayContent,
true, // stopHookActive: signal retry to AfterAgent hooks
);
}
}
Expand Down
Loading