feat: add fraud detection validator

This commit is contained in:
Dobromir Popov
2026-06-29 09:46:22 +03:00
parent a2aa22fc08
commit 39f6f23c83
9 changed files with 511 additions and 6 deletions

View File

@@ -28,11 +28,20 @@ def _make_stub_activations(prompt: str) -> dict:
class _StubHTTPServer(http.server.HTTPServer):
def __init__(self, addr, handler, shard_start: int, shard_end: int, is_last_shard: bool):
def __init__(
self,
addr,
handler,
shard_start: int,
shard_end: int,
is_last_shard: bool,
response_prefix: str,
):
super().__init__(addr, handler)
self.shard_start = shard_start
self.shard_end = shard_end
self.is_last_shard = is_last_shard
self.response_prefix = response_prefix
self.received_activations: bool = False
@@ -57,7 +66,7 @@ class _StubHandler(http.server.BaseHTTPRequestHandler):
server.received_activations = True
prompt = body["activations"].get("context", {}).get("prompt", "")
if server.is_last_shard:
payload = json.dumps({"text": f"stub response to: {prompt}"}).encode()
payload = json.dumps({"text": f"{server.response_prefix} {prompt}"}).encode()
else:
payload = json.dumps({"activations": _make_stub_activations(prompt)}).encode()
else:
@@ -66,7 +75,7 @@ class _StubHandler(http.server.BaseHTTPRequestHandler):
last_content = messages[-1]["content"] if messages else ""
if server.is_last_shard:
# Single-node shortcut: no activation hop needed.
payload = json.dumps({"text": f"stub response to: {last_content}"}).encode()
payload = json.dumps({"text": f"{server.response_prefix} {last_content}"}).encode()
else:
payload = json.dumps({"activations": _make_stub_activations(last_content)}).encode()
@@ -92,6 +101,7 @@ class StubNodeServer:
shard_start: int = 0,
shard_end: int = 31,
is_last_shard: bool = True,
response_prefix: str = "stub response to:",
):
self._host = host
self._requested_port = port
@@ -100,6 +110,7 @@ class StubNodeServer:
self._shard_start = shard_start
self._shard_end = shard_end
self._is_last_shard = is_last_shard
self._response_prefix = response_prefix
self._server: _StubHTTPServer | None = None
self._thread: threading.Thread | None = None
self.port: int | None = None
@@ -119,6 +130,7 @@ class StubNodeServer:
self._shard_start,
self._shard_end,
self._is_last_shard,
self._response_prefix,
)
self.port = self._server.server_address[1]
self._thread = threading.Thread(target=self._server.serve_forever, daemon=True)