-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_routing.py
More file actions
490 lines (434 loc) · 19.5 KB
/
Copy pathtest_routing.py
File metadata and controls
490 lines (434 loc) · 19.5 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
"""
Dynamic routing verification test with two mock servers.
Starts a mock upstream server and a mock fallback server, starts the proxy
with routing enabled, and verifies:
- Premium models with simple prompts are routed to fallback
- Identical requests are cache hits with route headers
- High-reasoning prompts are NOT routed
- Outbound model in fallback requests is correct
- Auth header format is correct (Bearer prefix)
- Route headers are present on all responses
"""
import http.server
import json
import os
import subprocess
import sys
import threading
import time
import urllib.error
import urllib.request
PROXY_PORT = 8080
MOCK_UPSTREAM_PORT = 8098
MOCK_FALLBACK_PORT = 8099
PROXY_URL = f"http://127.0.0.1:{PROXY_PORT}/v1/chat/completions"
MOCK_UPSTREAM_URL = f"http://127.0.0.1:{MOCK_UPSTREAM_PORT}"
MOCK_FALLBACK_URL = f"http://127.0.0.1:{MOCK_FALLBACK_PORT}"
PASS = 0
FAIL = 0
class RequestCapture:
"""Captures details of a single mock request."""
def __init__(self, method, path, headers, body):
self.method = method
self.path = path
self.headers = headers
self.body = body
class MockServer:
"""Mock HTTP server that captures requests for verification."""
def __init__(self, port, response_data):
self.port = port
self.response_data = response_data
self.captured_requests = []
self.request_lock = threading.Lock()
self.server = None
def start(self):
server = http.server.HTTPServer(("127.0.0.1", self.port), self._make_handler())
t = threading.Thread(target=server.serve_forever, daemon=True)
t.start()
self.server = server
return server
def _make_handler(self):
owner = self
class Handler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
content_len = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(content_len)
with owner.request_lock:
owner.captured_requests.append(RequestCapture(
self.command, self.path, dict(self.headers), body,
))
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(owner.response_data)
def log_message(self, fmt, *args):
pass
return Handler
@property
def request_count(self):
with self.request_lock:
return len(self.captured_requests)
@property
def last_request(self):
with self.request_lock:
return self.captured_requests[-1] if self.captured_requests else None
def shutdown(self):
if self.server:
self.server.shutdown()
def reset(self):
with self.request_lock:
self.captured_requests.clear()
# Separate response bodies so we can distinguish which server responded
upstream_response = json.dumps({
"id": "upstream-cmpl-001",
"object": "chat.completion",
"created": 1700000000,
"model": "gpt-4o",
"choices": [{"index": 0, "message": {"role": "assistant", "content": "Upstream response"}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 5, "completion_tokens": 2, "total_tokens": 7},
}).encode()
fallback_response = json.dumps({
"id": "fallback-cmpl-001",
"object": "chat.completion",
"created": 1700000000,
"model": "deepseek-chat",
"choices": [{"index": 0, "message": {"role": "assistant", "content": "Fallback response"}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 5, "completion_tokens": 2, "total_tokens": 7},
}).encode()
def proxy_binary():
# Use debug binary for CI consistency (release build is separate)
base = "./target/debug/stack-intercept"
return base + ".exe" if sys.platform == "win32" else base
def start_proxy(allow_rewrite="true", set_fallback_key=True):
env = os.environ.copy()
env["STACK_INTERCEPT_CACHE_MODE"] = "exact"
env["STACK_INTERCEPT_DISABLE_PERSISTENCE"] = "true"
env["STACK_INTERCEPT_ALLOW_MODEL_REWRITE"] = allow_rewrite
env["STACK_INTERCEPT_UPSTREAM_URL"] = MOCK_UPSTREAM_URL
env["STACK_INTERCEPT_FALLBACK_URL"] = MOCK_FALLBACK_URL
if set_fallback_key:
env["STACK_INTERCEPT_FALLBACK_API_KEY"] = "sk-fallback-secret"
# Unset DEEPSEEK_API_KEY if present so test env is clean
env.pop("DEEPSEEK_API_KEY", None)
proc = subprocess.Popen(
[proxy_binary()],
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
)
return proc
def wait_for(url, timeout=15):
start = time.time()
while time.time() - start < timeout:
try:
urllib.request.urlopen(urllib.request.Request(url, method="POST", data=b"{}"), timeout=2)
return True
except urllib.error.HTTPError as e:
if e.code in (415, 405):
return True
except (ConnectionResetError, urllib.error.URLError, OSError):
pass
time.sleep(0.3)
return False
def send_request(payload, extra_headers=None):
data = json.dumps(payload).encode()
headers = {"Content-Type": "application/json", "Authorization": "Bearer test-key"}
if extra_headers:
headers.update(extra_headers)
req = urllib.request.Request(
PROXY_URL, data=data,
headers=headers,
method="POST",
)
try:
resp = urllib.request.urlopen(req, timeout=15)
hit = resp.headers.get("x-stack-intercept", "")
route = resp.headers.get("x-stack-intercept-route", "")
orig_model = resp.headers.get("x-stack-intercept-original-model", "")
routed_model = resp.headers.get("x-stack-intercept-routed-model", "")
body = resp.read().decode()
return hit, route, orig_model, routed_model, resp.status, body
except urllib.error.HTTPError as e:
hit = e.headers.get("x-stack-intercept", "")
route = e.headers.get("x-stack-intercept-route", "")
orig_model = e.headers.get("x-stack-intercept-original-model", "")
routed_model = e.headers.get("x-stack-intercept-routed-model", "")
return hit, route, orig_model, routed_model, e.code, e.read().decode()
def check(name, cond, detail=""):
global PASS, FAIL
if cond:
PASS += 1
print(f" PASS: {name} {detail}")
else:
FAIL += 1
print(f" FAIL: {name} {detail}")
def main():
global PASS, FAIL
print("Starting mock upstream server (port {})...".format(MOCK_UPSTREAM_PORT))
upstream_mock = MockServer(MOCK_UPSTREAM_PORT, upstream_response)
upstream_mock.start()
print("Starting mock fallback server (port {})...".format(MOCK_FALLBACK_PORT))
fallback_mock = MockServer(MOCK_FALLBACK_PORT, fallback_response)
fallback_mock.start()
print("Starting proxy (routing enabled)...")
proxy = start_proxy("true")
try:
if not wait_for(PROXY_URL):
print("FAILED: Proxy did not start")
sys.exit(1)
print(" Proxy online.\n")
# =========================================================
# Test 1: gpt-4o simple prompt -> routed to fallback
# =========================================================
print("=" * 60)
print("Test 1: gpt-4o simple prompt -> routed to fallback")
payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "What is the capital of France?"}],
"temperature": 0,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(payload)
check("status is 200", status == 200)
check("not a cache hit", hit != "hit")
check("route is fallback", route == "fallback")
check("original model is gpt-4o", orig_model == "gpt-4o")
check("routed model is deepseek-chat", routed_model == "deepseek-chat")
check("body is from fallback", "Fallback response" in body)
check("upstream received 0 requests", upstream_mock.request_count == 0)
check("fallback received 1 request", fallback_mock.request_count == 1)
# Verify outbound request to fallback
fb_req = fallback_mock.last_request
if fb_req:
fb_body = json.loads(fb_req.body)
check("fallback model is deepseek-chat", fb_body.get("model") == "deepseek-chat")
# Verify auth header has Bearer prefix
auth_header = fb_req.headers.get("authorization", "")
check("auth has Bearer prefix", auth_header.startswith("Bearer "),
f"(auth: {auth_header[:20]}...)")
check("auth uses fallback key", "sk-fallback-secret" in auth_header)
print()
# =========================================================
# Test 2: Identical request -> cache hit
# =========================================================
print("=" * 60)
print("Test 2: identical request -> cache hit with route headers")
hit, route, orig_model, routed_model, status, body = send_request(payload)
check("status is 200", status == 200)
check("is a cache hit", hit == "hit")
check("route header on hit", route == "fallback")
check("original model on hit", orig_model == "gpt-4o")
check("routed model on hit", routed_model == "deepseek-chat")
check("body preserved", "Fallback response" in body)
# No additional requests to either server
check("upstream still 0 requests", upstream_mock.request_count == 0)
check("fallback still 1 request", fallback_mock.request_count == 1)
print()
# =========================================================
# Test 3: gpt-4o with high-reasoning keyword -> NOT routed
# =========================================================
print("=" * 60)
print("Test 3: gpt-4o with 'cryptography' -> NOT routed (high reasoning)")
crypto_payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Explain AES cryptography in detail"}],
"temperature": 0,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(crypto_payload)
check("status is 200", status == 200)
check("route is passthrough", route == "passthrough")
check("original model is gpt-4o", orig_model == "gpt-4o")
check("routed model equals original", routed_model == "gpt-4o")
check("body is from upstream", "Upstream response" in body)
check("upstream received 1 request", upstream_mock.request_count == 1)
check("fallback still 1 request", fallback_mock.request_count == 1)
print()
# =========================================================
# Test 4: gpt-4o with tools -> NOT routed
# =========================================================
print("=" * 60)
print("Test 4: gpt-4o with tools -> NOT routed (unsafe feature)")
tools_payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "What's the weather?"}],
"temperature": 0,
"stream": False,
"tools": [{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object", "properties": {}}}}],
}
hit, route, orig_model, routed_model, status, body = send_request(tools_payload)
check("status is 200", status == 200)
check("route is passthrough", route == "passthrough")
check("body from upstream", "Upstream response" in body)
check("upstream got this request", upstream_mock.request_count == 2)
print()
# =========================================================
# Test 5: gpt-4o with temperature > 0 -> NOT routed
# =========================================================
print("=" * 60)
print("Test 5: gpt-4o with temperature=0.7 -> NOT routed (non-deterministic)")
temp_payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Write a poem"}],
"temperature": 0.7,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(temp_payload)
check("status is 200", status == 200)
check("route is passthrough", route == "passthrough")
check("body from upstream", "Upstream response" in body)
print()
# =========================================================
# Test 6: No-route header -> NOT routed
# =========================================================
print("=" * 60)
print("Test 6: x-stack-intercept-no-route header -> NOT routed")
simple_payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Say hello"}],
"temperature": 0,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(
simple_payload, extra_headers={"x-stack-intercept-no-route": "true"}
)
check("status is 200", status == 200)
check("route is passthrough", route == "passthrough")
check("body from upstream", "Upstream response" in body)
print()
# =========================================================
# Test 7: Routing disabled by default
# =========================================================
print("=" * 60)
print("Test 7: Routing disabled (default) -> all passthrough")
# Restart proxy without ALLOW_MODEL_REWRITE
proxy.terminate()
proxy.wait(timeout=5)
upstream_mock.reset()
fallback_mock.reset()
proxy = start_proxy("false")
if not wait_for(PROXY_URL):
print("FAILED: Proxy did not start")
sys.exit(1)
no_route_payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Hello world"}],
"temperature": 0,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(no_route_payload)
check("status is 200", status == 200)
check("route is passthrough", route == "passthrough")
check("body from upstream", "Upstream response" in body)
check("upstream received request", upstream_mock.request_count == 1)
check("fallback received 0 requests", fallback_mock.request_count == 0)
print()
# =========================================================
# Test 8: Explicit model requirement in system prompt -> NOT routed
# =========================================================
print("=" * 60)
print("Test 8: 'do not switch models' in system prompt -> NOT routed")
# Restart proxy with routing for remaining tests
proxy.terminate()
proxy.wait(timeout=5)
upstream_mock.reset()
fallback_mock.reset()
proxy = start_proxy("true")
if not wait_for(PROXY_URL):
print("FAILED: Proxy did not start")
sys.exit(1)
explicit_payload = {
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant. Do not switch models."},
{"role": "user", "content": "What is the capital of France?"},
],
"temperature": 0,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(explicit_payload)
check("status is 200", status == 200)
check("route is passthrough", route == "passthrough")
check("body from upstream", "Upstream response" in body)
check("upstream received request", upstream_mock.request_count == 1)
check("fallback received 0 requests", fallback_mock.request_count == 0)
print()
# =========================================================
# Test 9: 'race condition' keyword -> NOT routed
# =========================================================
print("=" * 60)
print("Test 9: 'race condition' prompt -> NOT routed (expanded keyword)")
race_payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Debug a race condition in this Go program"}],
"temperature": 0,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(race_payload)
check("status is 200", status == 200)
check("route is passthrough", route == "passthrough")
check("body from upstream", "Upstream response" in body)
print()
# =========================================================
# Test 10: 'security review' keyword -> NOT routed
# =========================================================
print("=" * 60)
print("Test 10: 'security review' prompt -> NOT routed (expanded keyword)")
sec_payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Perform a security review of this authentication system"}],
"temperature": 0,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(sec_payload)
check("status is 200", status == 200)
check("route is passthrough", route == "passthrough")
check("body from upstream", "Upstream response" in body)
print()
# =========================================================
# Test 11: Routing enabled, no fallback key -> passthrough (no leak)
# =========================================================
print("=" * 60)
print("Test 11: routing enabled + no fallback key -> passthrough (no auth leak)")
proxy.terminate()
proxy.wait(timeout=5)
upstream_mock.reset()
fallback_mock.reset()
proxy = start_proxy("true", set_fallback_key=False)
if not wait_for(PROXY_URL):
print("FAILED: Proxy did not start")
sys.exit(1)
no_key_payload = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Hello world"}],
"temperature": 0,
"stream": False,
}
hit, route, orig_model, routed_model, status, body = send_request(no_key_payload)
check("status is 200", status == 200)
check("route is passthrough (safe fallback)", route == "passthrough")
check("routed model equals original", routed_model == "gpt-4o")
check("body from upstream", "Upstream response" in body)
check("upstream received request", upstream_mock.request_count == 1)
check("fallback received 0 requests (not leaked)", fallback_mock.request_count == 0)
# Verify upstream received the original auth, not a leaked key
up_req = upstream_mock.last_request
if up_req:
up_auth = up_req.headers.get("authorization", "")
check("upstream auth is original", "test-key" in up_auth)
check("upstream auth does not have fallback key", "sk-fallback-secret" not in up_auth)
print()
# =========================================================
# Summary
# =========================================================
print("=" * 60)
total = PASS + FAIL
print(f"Results: {PASS}/{total} passed", "ALL PASSED" if FAIL == 0 else f"{FAIL} FAILURES")
return 0 if FAIL == 0 else 1
finally:
proxy.terminate()
proxy.wait(timeout=5)
upstream_mock.shutdown()
fallback_mock.shutdown()
if __name__ == "__main__":
sys.exit(main())