Skip to content

Commit cbc0dbb

Browse files
committed
refactor(benchmark): tighten gateway type boundary
Signed-off-by: huangruiteng <huangrt01@163.com>
1 parent 01132d1 commit cbc0dbb

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎benchmark/widesearch/run_widesearch_case.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import tempfile
1818
import time
1919
from pathlib import Path
20+
from typing import Any
2021

2122
from tasks import answer_is_fresh, fresh_workspace, prepare_case, stamp_run_start
2223

@@ -154,7 +155,7 @@ def run_case(
154155
data_root: Path,
155156
timeout_sec: int,
156157
enable_web_search: bool = True,
157-
) -> dict:
158+
) -> dict[str, Any]:
158159
if sys.platform != "linux":
159160
raise RuntimeError("widesearch_native_isolation_unavailable_use_pier")
160161
raw = data_root / "widesearch.jsonl"

‎loopx/capabilities/benchmark_toolkit/provider_gateway.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,16 @@ def _validated_allowed_paths(values: Sequence[str]) -> tuple[str, ...]:
7676
return normalized
7777

7878

79-
def _upstream_connection(parsed: SplitResult, *, timeout_sec: float):
79+
def _upstream_connection(
80+
parsed: SplitResult, *, timeout_sec: float
81+
) -> http.client.HTTPConnection:
82+
host = parsed.hostname
83+
if host is None: # Defensive narrowing; _validated_upstream rejects this.
84+
raise ValueError("upstream_base_url must include a host")
8085
port = parsed.port
8186
if parsed.scheme == "https":
82-
return http.client.HTTPSConnection(parsed.hostname, port, timeout=timeout_sec)
83-
return http.client.HTTPConnection(parsed.hostname, port, timeout=timeout_sec)
87+
return http.client.HTTPSConnection(host, port, timeout=timeout_sec)
88+
return http.client.HTTPConnection(host, port, timeout=timeout_sec)
8489

8590

8691
@contextmanager
@@ -124,7 +129,7 @@ def _json_error(self, status: int, reason: str) -> None:
124129
self.end_headers()
125130
self.wfile.write(body)
126131

127-
def do_POST(self) -> None: # noqa: N802 - BaseHTTPRequestHandler contract
132+
def do_POST(self) -> None:
128133
requested = urlsplit(self.path)
129134
if requested.path not in admitted_paths:
130135
self._json_error(404, "provider_gateway_path_not_admitted")
@@ -179,7 +184,7 @@ def do_POST(self) -> None: # noqa: N802 - BaseHTTPRequestHandler contract
179184
finally:
180185
connection.close()
181186

182-
def do_GET(self) -> None: # noqa: N802 - BaseHTTPRequestHandler contract
187+
def do_GET(self) -> None:
183188
self._json_error(405, "provider_gateway_method_not_admitted")
184189

185190
server = ThreadingHTTPServer(("127.0.0.1", 0), GatewayHandler)
@@ -192,8 +197,9 @@ def do_GET(self) -> None: # noqa: N802 - BaseHTTPRequestHandler contract
192197
thread.start()
193198
try:
194199
host, port = server.server_address[:2]
200+
rendered_host = host.decode("ascii") if isinstance(host, bytes) else str(host)
195201
yield RunnerOwnedProviderGateway(
196-
base_url=f"http://{host}:{port}",
202+
base_url=f"http://{rendered_host}:{port}",
197203
allowed_paths=admitted_paths,
198204
)
199205
finally:

0 commit comments

Comments
 (0)