Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
135 changes: 88 additions & 47 deletions integration-tests/hooks-agent-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ describe('Hooks Agent Flow', () => {
console.error('DEBUG: BeforeAgent hook executed');
`;

const scriptPath = join(rig.testDir!, 'before_agent_context.cjs');
const scriptPath = join(rig.testDir!, 'before_agent_context.cjs').replace(
/\\/g,
'/',
);
writeFileSync(scriptPath, hookScript);

await rig.setup('should inject additional context via BeforeAgent hook', {
Expand Down Expand Up @@ -113,7 +116,10 @@ describe('Hooks Agent Flow', () => {
}
`;

const scriptPath = join(rig.testDir!, 'after_agent_verify.cjs');
const scriptPath = join(rig.testDir!, 'after_agent_verify.cjs').replace(
/\\/g,
'/',
);
writeFileSync(scriptPath, hookScript);

await rig.setup('should receive prompt and response in AfterAgent hook', {
Expand Down Expand Up @@ -157,15 +163,13 @@ describe('Hooks Agent Flow', () => {
});

it('should process clearContext in AfterAgent hook output', async () => {
await rig.setup('should process clearContext in AfterAgent hook output', {
fakeResponsesPath: join(
import.meta.dirname,
'hooks-system.after-agent.responses',
),
});
await rig.setup('should process clearContext in AfterAgent hook output');

// BeforeModel hook to track message counts across LLM calls
const messageCountFile = join(rig.testDir!, 'message-counts.json');
const messageCountFile = join(
rig.testDir!,
'message-counts.json',
).replace(/\\/g, '/');
const beforeModelScript = `
const fs = require('fs');
const input = JSON.parse(fs.readFileSync(0, 'utf-8'));
Expand All @@ -179,13 +183,35 @@ describe('Hooks Agent Flow', () => {
const beforeModelScriptPath = join(
rig.testDir!,
'before_model_counter.cjs',
);
).replace(/\\/g, '/');
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 = join(
rig.testDir!,
'after_agent_clear.cjs',
).replace(/\\/g, '/');
writeFileSync(afterAgentScriptPath, afterAgentScript);

await rig.configure({
fakeResponsesPath: join(
import.meta.dirname,
'hooks-system.after-agent.responses',
),
settings: {
hooks: {
hooksConfig: {
enabled: true,
},
hooks: {
BeforeModel: [
{
hooks: [
Expand All @@ -202,7 +228,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: `node "${afterAgentScriptPath}"`,
timeout: 5000,
},
],
Expand Down Expand Up @@ -239,42 +265,57 @@ describe('Hooks Agent Flow', () => {
it('should fire BeforeAgent and AfterAgent exactly once per turn despite tool calls', async () => {
await rig.setup(
'should fire BeforeAgent and AfterAgent exactly once per turn despite tool calls',
{
fakeResponsesPath: join(
import.meta.dirname,
'hooks-agent-flow-multistep.responses',
),
settings: {
hooksConfig: {
enabled: true,
},
hooks: {
BeforeAgent: [
{
hooks: [
{
type: 'command',
command: `node -e "console.log('BeforeAgent Fired')"`,
timeout: 5000,
},
],
},
],
AfterAgent: [
{
hooks: [
{
type: 'command',
command: `node -e "console.log('AfterAgent Fired')"`,
timeout: 5000,
},
],
},
],
},
);

const beforeAgentScript = "console.log('BeforeAgent Fired')";
const beforeAgentScriptPath = join(
rig.testDir!,
'before_agent_loop.cjs',
).replace(/\\/g, '/');
writeFileSync(beforeAgentScriptPath, beforeAgentScript);

const afterAgentScript = "console.log('AfterAgent Fired')";
const afterAgentScriptPath = join(
rig.testDir!,
'after_agent_loop.cjs',
).replace(/\\/g, '/');
writeFileSync(afterAgentScriptPath, afterAgentScript);

await rig.configure({
fakeResponsesPath: join(
import.meta.dirname,
'hooks-agent-flow-multistep.responses',
),
settings: {
hooksConfig: {
enabled: true,
},
hooks: {
BeforeAgent: [
{
hooks: [
{
type: 'command',
command: `node "${beforeAgentScriptPath}"`,
timeout: 5000,
},
],
},
],
AfterAgent: [
{
hooks: [
{
type: 'command',
command: `node "${afterAgentScriptPath}"`,
timeout: 5000,
},
],
},
],
},
},
);
});

await rig.run({ args: 'Do a multi-step task' });

Expand Down
Loading
Loading