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
61 changes: 45 additions & 16 deletions integration-tests/hooks-agent-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { TestRig } from './test-helper.js';
import { TestRig, normalizePath } from './test-helper.js';
import { join } from 'node:path';
import { writeFileSync } from 'node:fs';

Expand Down Expand Up @@ -113,10 +113,9 @@ describe('Hooks Agent Flow', () => {
}
`;

const scriptPath = join(rig.testDir!, 'after_agent_verify.cjs');
writeFileSync(scriptPath, hookScript);
const scriptPath = rig.createScript('after_agent_verify.cjs', hookScript);

await rig.setup('should receive prompt and response in AfterAgent hook', {
rig.setup('should receive prompt and response in AfterAgent hook', {
settings: {
hooksConfig: {
enabled: true,
Expand All @@ -127,7 +126,7 @@ describe('Hooks Agent Flow', () => {
hooks: [
{
type: 'command',
command: `node "${scriptPath}"`,
command: normalizePath(`node "${scriptPath}"`)!,
timeout: 5000,
},
],
Expand Down Expand Up @@ -157,7 +156,7 @@ describe('Hooks Agent Flow', () => {
});

it('should process clearContext in AfterAgent hook output', async () => {
await rig.setup('should process clearContext in AfterAgent hook output', {
rig.setup('should process clearContext in AfterAgent hook output', {
fakeResponsesPath: join(
import.meta.dirname,
'hooks-system.after-agent.responses',
Expand All @@ -171,18 +170,32 @@ describe('Hooks Agent Flow', () => {
const input = JSON.parse(fs.readFileSync(0, 'utf-8'));
const messageCount = input.llm_request?.contents?.length || 0;
let counts = [];
try { counts = JSON.parse(fs.readFileSync('${messageCountFile}', 'utf-8')); } catch (e) {}
try { counts = JSON.parse(fs.readFileSync(${JSON.stringify(messageCountFile)}, 'utf-8')); } catch (e) {}
counts.push(messageCount);
fs.writeFileSync('${messageCountFile}', JSON.stringify(counts));
fs.writeFileSync(${JSON.stringify(messageCountFile)}, JSON.stringify(counts));
console.log(JSON.stringify({ decision: 'allow' }));
`;
const beforeModelScriptPath = join(
rig.testDir!,
const beforeModelScriptPath = rig.createScript(
'before_model_counter.cjs',
beforeModelScript,
);
writeFileSync(beforeModelScriptPath, beforeModelScript);

await rig.setup('should process clearContext in AfterAgent hook output', {
const afterAgentScript = `
console.log(JSON.stringify({
decision: 'block',
reason: 'Security policy triggered',
hookSpecificOutput: {
hookEventName: 'AfterAgent',
clearContext: true
}
}));
`;
const afterAgentScriptPath = rig.createScript(
'after_agent_clear.cjs',
afterAgentScript,
);

rig.setup('should process clearContext in AfterAgent hook output', {
settings: {
hooks: {
enabled: true,
Expand All @@ -191,7 +204,7 @@ describe('Hooks Agent Flow', () => {
hooks: [
{
type: 'command',
command: `node "${beforeModelScriptPath}"`,
command: normalizePath(`node "${beforeModelScriptPath}"`)!,
timeout: 5000,
},
],
Expand All @@ -202,7 +215,7 @@ describe('Hooks Agent Flow', () => {
hooks: [
{
type: 'command',
command: `node -e "console.log(JSON.stringify({decision: 'block', reason: 'Security policy triggered', hookSpecificOutput: {hookEventName: 'AfterAgent', clearContext: true}}))"`,
command: normalizePath(`node "${afterAgentScriptPath}"`)!,
timeout: 5000,
},
],
Expand Down Expand Up @@ -244,6 +257,22 @@ describe('Hooks Agent Flow', () => {
import.meta.dirname,
'hooks-agent-flow-multistep.responses',
),
},
);

// Create script files for hooks
const baPath = rig.createScript(
'ba_fired.cjs',
"console.log('BeforeAgent Fired');",
);
const aaPath = rig.createScript(
'aa_fired.cjs',
"console.log('AfterAgent Fired');",
);

await rig.setup(
'should fire BeforeAgent and AfterAgent exactly once per turn despite tool calls',
{
settings: {
hooksConfig: {
enabled: true,
Expand All @@ -254,7 +283,7 @@ describe('Hooks Agent Flow', () => {
hooks: [
{
type: 'command',
command: `node -e "console.log('BeforeAgent Fired')"`,
command: normalizePath(`node "${baPath}"`)!,
timeout: 5000,
},
],
Expand All @@ -265,7 +294,7 @@ describe('Hooks Agent Flow', () => {
hooks: [
{
type: 'command',
command: `node -e "console.log('AfterAgent Fired')"`,
command: normalizePath(`node "${aaPath}"`)!,
timeout: 5000,
},
],
Expand Down
Loading
Loading