Repository navigation
Expand file tree
/
Copy pathtest_enhanced_interceptor.py
More file actions
301 lines (262 loc) · 10.1 KB
/
Copy pathtest_enhanced_interceptor.py
File metadata and controls
301 lines (262 loc) · 10.1 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import pytest
from airlock.guardrails.enhanced_interceptor import EnhancedModelInterceptor
@pytest.fixture
def interceptor() -> EnhancedModelInterceptor:
return EnhancedModelInterceptor()
@pytest.mark.asyncio
async def test_no_enhanced_profile(interceptor: EnhancedModelInterceptor) -> None:
data = {"model": "regular-model", "litellm_params": {"other": "value"}}
result = await interceptor.async_pre_call(data)
assert result["model"] == "regular-model"
@pytest.mark.asyncio
async def test_missing_target_model(
interceptor: EnhancedModelInterceptor, caplog: pytest.LogCaptureFixture
) -> None:
import logging
data = {
"model": "enhanced-missing",
"litellm_params": {"enhanced_profile": {"system_prompt": "test"}},
}
with caplog.at_level(logging.WARNING):
result = await interceptor.async_pre_call(data)
assert result == data
assert "is missing 'target_model'" in caplog.text
@pytest.mark.asyncio
async def test_target_model_mutation(interceptor: EnhancedModelInterceptor) -> None:
data = {
"model": "logical-model",
"litellm_params": {"enhanced_profile": {"target_model": "physical-model"}},
}
result = await interceptor.async_pre_call(data)
assert result["model"] == "physical-model"
@pytest.mark.asyncio
async def test_inject_system_prompt_empty_messages(
interceptor: EnhancedModelInterceptor,
) -> None:
data = {
"model": "logical-model",
"messages": [],
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"system_prompt": "New system prompt",
}
},
}
result = await interceptor.async_pre_call(data)
assert result["messages"][0] == {"role": "system", "content": "New system prompt"}
@pytest.mark.asyncio
async def test_inject_system_prompt_existing_system_message(
interceptor: EnhancedModelInterceptor,
) -> None:
data = {
"model": "logical-model",
"messages": [{"role": "system", "content": "Original prompt"}],
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"system_prompt": "Appended prompt",
}
},
}
result = await interceptor.async_pre_call(data)
assert result["messages"][0]["content"] == "Original prompt\n\nAppended prompt"
@pytest.mark.asyncio
async def test_inject_system_prompt_existing_user_message(
interceptor: EnhancedModelInterceptor,
) -> None:
data = {
"model": "logical-model",
"messages": [{"role": "user", "content": "Hello"}],
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"system_prompt": "Injected prompt",
}
},
}
result = await interceptor.async_pre_call(data)
assert result["messages"][0] == {"role": "system", "content": "Injected prompt"}
assert result["messages"][1] == {"role": "user", "content": "Hello"}
@pytest.mark.asyncio
async def test_merge_optional_params(interceptor: EnhancedModelInterceptor) -> None:
data = {
"model": "logical-model",
"optional_params": {"existing": 1},
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"params": {"thinking": True, "temperature": 0.5},
}
},
}
result = await interceptor.async_pre_call(data)
assert result["optional_params"]["existing"] == 1
assert result["optional_params"]["thinking"] is True
assert result["optional_params"]["temperature"] == 0.5
assert result["model"] == "physical-model"
@pytest.mark.asyncio
async def test_inject_system_prompt_empty_content(
interceptor: EnhancedModelInterceptor,
) -> None:
data = {
"model": "logical-model",
"messages": [{"role": "system", "content": ""}],
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"system_prompt": "New system prompt",
}
},
}
result = await interceptor.async_pre_call(data)
assert result["messages"][0] == {"role": "system", "content": "New system prompt"}
@pytest.mark.asyncio
async def test_none_messages(interceptor: EnhancedModelInterceptor) -> None:
data = {
"model": "logical-model",
"messages": None,
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"system_prompt": "New system prompt",
}
},
}
result = await interceptor.async_pre_call(data)
assert result["messages"][0] == {"role": "system", "content": "New system prompt"}
@pytest.mark.asyncio
async def test_none_optional_params(interceptor: EnhancedModelInterceptor) -> None:
data = {
"model": "logical-model",
"optional_params": None,
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"params": {"temperature": 0.5},
}
},
}
result = await interceptor.async_pre_call(data)
assert result["optional_params"]["temperature"] == 0.5
@pytest.mark.asyncio
async def test_system_injection_records_inject(
interceptor: EnhancedModelInterceptor,
) -> None:
data = {
"model": "logical-model",
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"system_prompt": "SECRET_SYS_PROMPT_TEXT",
"name": "coder",
}
},
}
result = await interceptor.async_pre_call(data)
muts = result["metadata"]["airlock_mutations"]
inj = [m for m in muts if m.op == "inject" and m.field == "system"]
assert len(inj) == 1
assert inj[0].source == "enhanced.interceptor"
assert inj[0].stage == "pre_call"
assert inj[0].before is None and inj[0].after is None
# CC-T2: injected system-prompt text never lands in the ledger
assert "SECRET_SYS_PROMPT_TEXT" not in repr(muts)
# behavior unchanged
assert result["model"] == "physical-model"
@pytest.mark.asyncio
async def test_no_system_prompt_no_inject_record(
interceptor: EnhancedModelInterceptor,
) -> None:
data = {
"model": "logical-model",
"litellm_params": {"enhanced_profile": {"target_model": "physical-model"}},
}
result = await interceptor.async_pre_call(data)
muts = result.get("metadata", {}).get("airlock_mutations", [])
assert [m for m in muts if m.op == "inject"] == []
_CONFIG_FALLBACK_YAML = """
model_list:
- model_name: gemini-coding
litellm_params:
model: enhanced/gemini-coding
enhanced_profile:
target_model: gemini/gemini-3.1-pro-preview-customtools
system_prompt: "CONFIG_SECRET_SYS_PROMPT_TEXT"
params:
thinking: true
thinking_level: "MEDIUM"
- model_name: plain-model
litellm_params:
model: gemini/gemini-3.1-pro-preview
""".strip()
@pytest.fixture
def config_fallback_env(tmp_path, monkeypatch):
from airlock.providers.enhanced_passthrough import enhanced_handler
config = tmp_path / "config.yaml"
config.write_text(_CONFIG_FALLBACK_YAML)
monkeypatch.setenv("AIRLOCK_CONFIG", str(config))
# Force the shared provider cache to reload from this temp config.
enhanced_handler._config_profile_cache = None
enhanced_handler._config_profile_cache_key = None
yield
enhanced_handler._config_profile_cache = None
enhanced_handler._config_profile_cache_key = None
@pytest.mark.asyncio
async def test_config_fallback_injection_records_inject(
interceptor: EnhancedModelInterceptor, config_fallback_env
) -> None:
# litellm_params lacks enhanced_profile; the provider resolves it from config.
data = {
"model": "gemini-coding",
"messages": [{"role": "user", "content": "hi"}],
"litellm_params": {"model": "enhanced/gemini-coding"},
}
result = await interceptor.async_pre_call(data)
muts = result["metadata"]["airlock_mutations"]
inj = [m for m in muts if m.op == "inject" and m.field == "system"]
assert len(inj) == 1
assert inj[0].source == "enhanced.passthrough"
assert inj[0].stage == "pre_call"
assert inj[0].before is None and inj[0].after is None
# CC-T2: config system-prompt text never lands in the ledger
assert "CONFIG_SECRET_SYS_PROMPT_TEXT" not in repr(muts)
# behavior unchanged: the interceptor does NOT rewrite the model or messages
# for the config-fallback path (the passthrough provider does that at exec).
assert result["model"] == "gemini-coding"
assert result["messages"] == [{"role": "user", "content": "hi"}]
@pytest.mark.asyncio
async def test_config_fallback_non_enhanced_no_record(
interceptor: EnhancedModelInterceptor, config_fallback_env
) -> None:
data = {
"model": "plain-model",
"messages": [{"role": "user", "content": "hi"}],
"litellm_params": {"model": "gemini/gemini-3.1-pro-preview"},
}
result = await interceptor.async_pre_call(data)
muts = result.get("metadata", {}).get("airlock_mutations", [])
assert [m for m in muts if m.op == "inject"] == []
@pytest.mark.asyncio
async def test_litellm_params_path_no_double_record(
interceptor: EnhancedModelInterceptor, config_fallback_env
) -> None:
# A request whose enhanced_profile IS in litellm_params records exactly once
# via Site 11 — never a second config-fallback record.
data = {
"model": "gemini-coding",
"messages": [{"role": "user", "content": "hi"}],
"litellm_params": {
"enhanced_profile": {
"target_model": "physical-model",
"system_prompt": "inline sys",
"name": "coder",
}
},
}
result = await interceptor.async_pre_call(data)
muts = result["metadata"]["airlock_mutations"]
inj = [m for m in muts if m.op == "inject" and m.field == "system"]
assert len(inj) == 1
assert inj[0].source == "enhanced.interceptor"
assert [m for m in muts if m.source == "enhanced.passthrough"] == []