forked from OtterMind/youclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks-manager.test.ts
More file actions
246 lines (196 loc) · 8.18 KB
/
Copy pathhooks-manager.test.ts
File metadata and controls
246 lines (196 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { describe, test, expect, beforeEach } from 'bun:test'
import './setup-light.ts'
import { HooksManager, type HookContext, type HookHandler } from '../src/agent/hooks.ts'
function createContext(overrides: Partial<HookContext> = {}): HookContext {
return {
agentId: 'test-agent',
chatId: 'web:chat-1',
phase: 'pre_process',
payload: {},
...overrides,
}
}
describe('HooksManager', () => {
let hooks: HooksManager
beforeEach(() => {
hooks = new HooksManager()
})
test('returns original context when no hooks are registered', async () => {
const ctx = createContext({ payload: { prompt: 'hello' } })
const result = await hooks.execute('test-agent', 'pre_process', ctx)
expect(result.payload.prompt).toBe('hello')
expect(result.abort).toBeUndefined()
})
test('registerBuiltinHook registers and executes', async () => {
const handler: HookHandler = async (ctx) => {
ctx.modifiedPayload = { prompt: 'modified' }
return ctx
}
hooks.registerBuiltinHook('test-agent', 'pre_process', handler)
const ctx = createContext({ payload: { prompt: 'original' } })
const result = await hooks.execute('test-agent', 'pre_process', ctx)
expect(result.modifiedPayload?.prompt).toBe('modified')
})
test('abort stops subsequent hooks', async () => {
const firstHook: HookHandler = async (ctx) => {
ctx.abort = true
ctx.abortReason = 'intercepted by first hook'
return ctx
}
const secondHook: HookHandler = async (ctx) => {
ctx.modifiedPayload = { reached: true }
return ctx
}
hooks.registerBuiltinHook('test-agent', 'pre_process', firstHook, 0)
hooks.registerBuiltinHook('test-agent', 'pre_process', secondHook, 10)
const ctx = createContext()
const result = await hooks.execute('test-agent', 'pre_process', ctx)
expect(result.abort).toBe(true)
expect(result.abortReason).toBe('intercepted by first hook')
expect(result.modifiedPayload?.reached).toBeUndefined()
})
test('priority sorting: lower values execute first', async () => {
const order: number[] = []
const hookA: HookHandler = async (ctx) => {
order.push(1)
return ctx
}
const hookB: HookHandler = async (ctx) => {
order.push(2)
return ctx
}
const hookC: HookHandler = async (ctx) => {
order.push(3)
return ctx
}
hooks.registerBuiltinHook('test-agent', 'pre_process', hookC, 100)
hooks.registerBuiltinHook('test-agent', 'pre_process', hookA, -10)
hooks.registerBuiltinHook('test-agent', 'pre_process', hookB, 50)
await hooks.execute('test-agent', 'pre_process', createContext())
expect(order).toEqual([1, 2, 3])
})
test('hook errors do not affect main flow (skip and continue)', async () => {
const errorHook: HookHandler = async () => {
throw new Error('Hook internal error')
}
const normalHook: HookHandler = async (ctx) => {
ctx.modifiedPayload = { reached: true }
return ctx
}
hooks.registerBuiltinHook('test-agent', 'pre_process', errorHook, 0)
hooks.registerBuiltinHook('test-agent', 'pre_process', normalHook, 10)
const result = await hooks.execute('test-agent', 'pre_process', createContext())
// The errored hook is skipped, subsequent hooks execute normally
expect(result.modifiedPayload?.reached).toBe(true)
})
test('timed-out hook is skipped', async () => {
const slowHook: HookHandler = async (ctx) => {
await new Promise((resolve) => setTimeout(resolve, 10_000))
return ctx
}
const normalHook: HookHandler = async (ctx) => {
ctx.modifiedPayload = { afterTimeout: true }
return ctx
}
hooks.registerBuiltinHook('test-agent', 'pre_process', slowHook, 0)
hooks.registerBuiltinHook('test-agent', 'pre_process', normalHook, 10)
const result = await hooks.execute('test-agent', 'pre_process', createContext())
// The timed-out hook is skipped, subsequent hooks execute normally
expect(result.modifiedPayload?.afterTimeout).toBe(true)
}, 10_000) // give the test itself enough time
test('pre_tool_use tools filtering', async () => {
const called: string[] = []
const bashGuard: HookHandler = async (ctx) => {
called.push('bash-guard')
ctx.abort = true
ctx.abortReason = 'Bash is forbidden'
return ctx
}
// Register a hook that only applies to the Bash tool
hooks.registerBuiltinHook('test-agent', 'pre_tool_use', bashGuard, 0)
// Manually set tools filtering
const agentHooks = (hooks as any).hooks.get('test-agent')!.get('pre_tool_use')!
agentHooks[0].tools = ['Bash']
// Bash tool -> should be intercepted
const bashCtx = createContext({
phase: 'pre_tool_use',
payload: { tool: 'Bash', input: 'rm -rf /' },
})
const bashResult = await hooks.execute('test-agent', 'pre_tool_use', bashCtx)
expect(bashResult.abort).toBe(true)
expect(called).toContain('bash-guard')
// Read tool -> should skip bashGuard
called.length = 0
const readCtx = createContext({
phase: 'pre_tool_use',
payload: { tool: 'Read', input: '/tmp/test' },
})
const readResult = await hooks.execute('test-agent', 'pre_tool_use', readCtx)
expect(readResult.abort).toBeUndefined()
expect(called).not.toContain('bash-guard')
})
test('unloadHooks stops execution after cleanup', async () => {
const handler: HookHandler = async (ctx) => {
ctx.modifiedPayload = { executed: true }
return ctx
}
hooks.registerBuiltinHook('test-agent', 'pre_process', handler)
// Can execute before unloading
let result = await hooks.execute('test-agent', 'pre_process', createContext())
expect(result.modifiedPayload?.executed).toBe(true)
// No longer executes after unloading
hooks.unloadHooks('test-agent')
result = await hooks.execute('test-agent', 'pre_process', createContext())
expect(result.modifiedPayload).toBeUndefined()
})
test('hooks of different agents are isolated from each other', async () => {
const handlerA: HookHandler = async (ctx) => {
ctx.modifiedPayload = { agent: 'A' }
return ctx
}
const handlerB: HookHandler = async (ctx) => {
ctx.modifiedPayload = { agent: 'B' }
return ctx
}
hooks.registerBuiltinHook('agent-a', 'pre_process', handlerA)
hooks.registerBuiltinHook('agent-b', 'pre_process', handlerB)
const resultA = await hooks.execute('agent-a', 'pre_process', createContext({ agentId: 'agent-a' }))
const resultB = await hooks.execute('agent-b', 'pre_process', createContext({ agentId: 'agent-b' }))
expect(resultA.modifiedPayload?.agent).toBe('A')
expect(resultB.modifiedPayload?.agent).toBe('B')
})
test('hooks of different phases are isolated from each other', async () => {
const preHandler: HookHandler = async (ctx) => {
ctx.modifiedPayload = { phase: 'pre' }
return ctx
}
const postHandler: HookHandler = async (ctx) => {
ctx.modifiedPayload = { phase: 'post' }
return ctx
}
hooks.registerBuiltinHook('test-agent', 'pre_process', preHandler)
hooks.registerBuiltinHook('test-agent', 'post_process', postHandler)
const preResult = await hooks.execute('test-agent', 'pre_process', createContext({ phase: 'pre_process' }))
const postResult = await hooks.execute('test-agent', 'post_process', createContext({ phase: 'post_process' }))
expect(preResult.modifiedPayload?.phase).toBe('pre')
expect(postResult.modifiedPayload?.phase).toBe('post')
})
test('hook chain passes context modifications sequentially', async () => {
const hook1: HookHandler = async (ctx) => {
ctx.payload.step1 = true
return ctx
}
const hook2: HookHandler = async (ctx) => {
ctx.payload.step2 = true
// Verify step1 already exists
ctx.payload.step1Existed = ctx.payload.step1 === true
return ctx
}
hooks.registerBuiltinHook('test-agent', 'pre_process', hook1, 0)
hooks.registerBuiltinHook('test-agent', 'pre_process', hook2, 10)
const result = await hooks.execute('test-agent', 'pre_process', createContext())
expect(result.payload.step1).toBe(true)
expect(result.payload.step2).toBe(true)
expect(result.payload.step1Existed).toBe(true)
})
})