|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from agentic_core.application.hooks import ( |
| 4 | + Hook, |
| 5 | + HookContext, |
| 6 | + HookEvent, |
| 7 | + HookRegistry, |
| 8 | + HookResult, |
| 9 | + HookVerdict, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +async def _allow_hook(ctx: HookContext) -> HookResult: |
| 14 | + return HookResult() |
| 15 | + |
| 16 | + |
| 17 | +async def _block_hook(ctx: HookContext) -> HookResult: |
| 18 | + return HookResult(verdict=HookVerdict.BLOCK, reason="blocked by test") |
| 19 | + |
| 20 | + |
| 21 | +async def _modify_args_hook(ctx: HookContext) -> HookResult: |
| 22 | + return HookResult(modified_args={"sanitized": True}) |
| 23 | + |
| 24 | + |
| 25 | +async def _failing_hook(ctx: HookContext) -> HookResult: |
| 26 | + raise RuntimeError("hook crashed") |
| 27 | + |
| 28 | + |
| 29 | +# --- Registry basics --- |
| 30 | + |
| 31 | + |
| 32 | +def test_empty_registry_count(): |
| 33 | + reg = HookRegistry() |
| 34 | + assert reg.count() == 0 |
| 35 | + assert reg.count(HookEvent.PRE_TOOL_USE) == 0 |
| 36 | + |
| 37 | + |
| 38 | +def test_register_and_count(): |
| 39 | + reg = HookRegistry() |
| 40 | + reg.register(HookEvent.PRE_TOOL_USE, _allow_hook) |
| 41 | + reg.register(HookEvent.POST_TOOL_USE, _allow_hook) |
| 42 | + assert reg.count() == 2 |
| 43 | + assert reg.count(HookEvent.PRE_TOOL_USE) == 1 |
| 44 | + |
| 45 | + |
| 46 | +def test_unregister(): |
| 47 | + reg = HookRegistry() |
| 48 | + reg.register(HookEvent.PRE_TOOL_USE, _allow_hook) |
| 49 | + reg.unregister(HookEvent.PRE_TOOL_USE, _allow_hook) |
| 50 | + assert reg.count(HookEvent.PRE_TOOL_USE) == 0 |
| 51 | + |
| 52 | + |
| 53 | +# --- Run behavior --- |
| 54 | + |
| 55 | + |
| 56 | +async def test_run_no_hooks_allows(): |
| 57 | + reg = HookRegistry() |
| 58 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE, tool_name="test") |
| 59 | + result = await reg.run(ctx) |
| 60 | + assert result.verdict == HookVerdict.ALLOW |
| 61 | + |
| 62 | + |
| 63 | +async def test_run_allow_hook(): |
| 64 | + reg = HookRegistry() |
| 65 | + reg.register(HookEvent.PRE_TOOL_USE, _allow_hook) |
| 66 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE, tool_name="test") |
| 67 | + result = await reg.run(ctx) |
| 68 | + assert result.verdict == HookVerdict.ALLOW |
| 69 | + |
| 70 | + |
| 71 | +async def test_run_block_hook(): |
| 72 | + reg = HookRegistry() |
| 73 | + reg.register(HookEvent.PRE_TOOL_USE, _block_hook) |
| 74 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE, tool_name="set_ph") |
| 75 | + result = await reg.run(ctx) |
| 76 | + assert result.verdict == HookVerdict.BLOCK |
| 77 | + assert result.reason == "blocked by test" |
| 78 | + |
| 79 | + |
| 80 | +async def test_block_stops_chain(): |
| 81 | + """First BLOCK wins; subsequent hooks don't run.""" |
| 82 | + call_count = 0 |
| 83 | + |
| 84 | + async def counting_hook(ctx: HookContext) -> HookResult: |
| 85 | + nonlocal call_count |
| 86 | + call_count += 1 |
| 87 | + return HookResult() |
| 88 | + |
| 89 | + reg = HookRegistry() |
| 90 | + reg.register(HookEvent.PRE_TOOL_USE, _block_hook, priority=0) |
| 91 | + reg.register(HookEvent.PRE_TOOL_USE, counting_hook, priority=1) |
| 92 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE) |
| 93 | + result = await reg.run(ctx) |
| 94 | + assert result.verdict == HookVerdict.BLOCK |
| 95 | + assert call_count == 0 |
| 96 | + |
| 97 | + |
| 98 | +async def test_allow_then_block(): |
| 99 | + reg = HookRegistry() |
| 100 | + reg.register(HookEvent.PRE_TOOL_USE, _allow_hook, priority=0) |
| 101 | + reg.register(HookEvent.PRE_TOOL_USE, _block_hook, priority=1) |
| 102 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE) |
| 103 | + result = await reg.run(ctx) |
| 104 | + assert result.verdict == HookVerdict.BLOCK |
| 105 | + |
| 106 | + |
| 107 | +async def test_priority_ordering(): |
| 108 | + """Lower priority number runs first.""" |
| 109 | + order: list[str] = [] |
| 110 | + |
| 111 | + async def hook_a(ctx: HookContext) -> HookResult: |
| 112 | + order.append("a") |
| 113 | + return HookResult() |
| 114 | + |
| 115 | + async def hook_b(ctx: HookContext) -> HookResult: |
| 116 | + order.append("b") |
| 117 | + return HookResult() |
| 118 | + |
| 119 | + reg = HookRegistry() |
| 120 | + reg.register(HookEvent.PRE_TOOL_USE, hook_b, priority=10) |
| 121 | + reg.register(HookEvent.PRE_TOOL_USE, hook_a, priority=1) |
| 122 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE) |
| 123 | + await reg.run(ctx) |
| 124 | + assert order == ["a", "b"] |
| 125 | + |
| 126 | + |
| 127 | +async def test_failing_hook_does_not_block(): |
| 128 | + reg = HookRegistry() |
| 129 | + reg.register(HookEvent.PRE_TOOL_USE, _failing_hook) |
| 130 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE) |
| 131 | + result = await reg.run(ctx) |
| 132 | + assert result.verdict == HookVerdict.ALLOW |
| 133 | + |
| 134 | + |
| 135 | +async def test_modify_args_returned(): |
| 136 | + reg = HookRegistry() |
| 137 | + reg.register(HookEvent.PRE_TOOL_USE, _modify_args_hook) |
| 138 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE, tool_args={"raw": True}) |
| 139 | + result = await reg.run(ctx) |
| 140 | + assert result.verdict == HookVerdict.ALLOW |
| 141 | + assert result.modified_args == {"sanitized": True} |
| 142 | + |
| 143 | + |
| 144 | +# --- Hook events --- |
| 145 | + |
| 146 | + |
| 147 | +async def test_post_tool_use_event(): |
| 148 | + reg = HookRegistry() |
| 149 | + reg.register(HookEvent.POST_TOOL_USE, _allow_hook) |
| 150 | + ctx = HookContext( |
| 151 | + event=HookEvent.POST_TOOL_USE, |
| 152 | + tool_name="search", |
| 153 | + tool_result_success=True, |
| 154 | + tool_result_output="found 3 results", |
| 155 | + ) |
| 156 | + result = await reg.run(ctx) |
| 157 | + assert result.verdict == HookVerdict.ALLOW |
| 158 | + |
| 159 | + |
| 160 | +async def test_session_stop_event(): |
| 161 | + reg = HookRegistry() |
| 162 | + reg.register(HookEvent.SESSION_STOP, _allow_hook) |
| 163 | + ctx = HookContext( |
| 164 | + event=HookEvent.SESSION_STOP, |
| 165 | + session_id="sess_123", |
| 166 | + ) |
| 167 | + result = await reg.run(ctx) |
| 168 | + assert result.verdict == HookVerdict.ALLOW |
| 169 | + |
| 170 | + |
| 171 | +async def test_on_error_event(): |
| 172 | + reg = HookRegistry() |
| 173 | + reg.register(HookEvent.ON_ERROR, _allow_hook) |
| 174 | + ctx = HookContext( |
| 175 | + event=HookEvent.ON_ERROR, |
| 176 | + error="Connection timeout", |
| 177 | + ) |
| 178 | + result = await reg.run(ctx) |
| 179 | + assert result.verdict == HookVerdict.ALLOW |
| 180 | + |
| 181 | + |
| 182 | +async def test_hooks_scoped_to_event(): |
| 183 | + """Hooks registered for one event don't fire for another.""" |
| 184 | + reg = HookRegistry() |
| 185 | + reg.register(HookEvent.PRE_TOOL_USE, _block_hook) |
| 186 | + ctx = HookContext(event=HookEvent.POST_TOOL_USE) |
| 187 | + result = await reg.run(ctx) |
| 188 | + assert result.verdict == HookVerdict.ALLOW |
| 189 | + |
| 190 | + |
| 191 | +# --- HookContext --- |
| 192 | + |
| 193 | + |
| 194 | +def test_hook_context_defaults(): |
| 195 | + ctx = HookContext(event=HookEvent.PRE_TOOL_USE) |
| 196 | + assert ctx.session_id is None |
| 197 | + assert ctx.persona_id is None |
| 198 | + assert ctx.tool_name is None |
| 199 | + assert ctx.tool_args == {} |
| 200 | + assert ctx.error is None |
| 201 | + |
| 202 | + |
| 203 | +def test_hook_context_full(): |
| 204 | + ctx = HookContext( |
| 205 | + event=HookEvent.PRE_TOOL_USE, |
| 206 | + session_id="s1", |
| 207 | + persona_id="support-agent", |
| 208 | + tool_name="mcp_stripe_create_charge", |
| 209 | + tool_args={"amount": 1000}, |
| 210 | + ) |
| 211 | + assert ctx.tool_name == "mcp_stripe_create_charge" |
| 212 | + assert ctx.tool_args["amount"] == 1000 |
| 213 | + |
| 214 | + |
| 215 | +# --- HookResult --- |
| 216 | + |
| 217 | + |
| 218 | +def test_hook_result_defaults(): |
| 219 | + r = HookResult() |
| 220 | + assert r.verdict == HookVerdict.ALLOW |
| 221 | + assert r.reason is None |
| 222 | + assert r.modified_args is None |
0 commit comments