Merge pull request #958 from lij768423-svg/fix/quality-guard-base-url

fix: honor GROK2API_BASE_URL in quality-guard sidecar
This commit is contained in:
Chenyme authored and GitHub committed 2026-08-18 16:50:51 +08:00
commit 07eef7c32c
5 files changed
+73 -4

No files matched your search

+4
View File
@@ -62,6 +62,10 @@ services:
build:
context: .
dockerfile: tools/egress-quality-guard/Dockerfile
environment:
# Default matches the Compose service name. Override for host networking
# or a renamed grok2api service, e.g. http://127.0.0.1:8000
GROK2API_BASE_URL: ${GROK2API_BASE_URL:-http://grok2api:8000}
depends_on:
grok2api:
condition: service_healthy
+5
View File
@@ -185,6 +185,11 @@ After changing the base `qualityGuard` settings in `config.yaml`, run
so the main service regenerates the bootstrap. Policy changes saved in the
admin page still hot-reload without a restart.
The sidecar talks to grok2api at `GROK2API_BASE_URL`, which defaults to
`http://grok2api:8000` on the Compose network. Set it when the main service is
renamed or published on host networking, for example
`GROK2API_BASE_URL=http://127.0.0.1:8000`.
Verify the managed nodes, model, and minimum healthy-node count before leaving
the sidecar running. Never commit the state volume or
production logs.
@@ -130,6 +130,8 @@ docker compose --profile quality-guard up -d --build
以后修改 `config.yaml` 中的 `qualityGuard` 基础配置时,执行
`docker compose --profile quality-guard restart grok2api egress-quality-guard` 让主程序重新生成 bootstrap。管理页面保存的运行策略仍会热加载,无需重启。
sidecar 通过 `GROK2API_BASE_URL` 访问主程序,Compose 网络默认是 `http://grok2api:8000`。主服务改名或使用 host 网络时需要覆盖,例如 `GROK2API_BASE_URL=http://127.0.0.1:8000`。
先确认受管节点、模型和最低健康节点数正确,再允许 sidecar 长期运行。不要提交状态卷或生产日志。只停止守护程序可执行
`docker compose --profile quality-guard stop egress-quality-guard`,不会影响主 API。
+17 -3
View File
@@ -42,6 +42,7 @@ RUNTIME_CONFIG_FIELDS = {
BOOTSTRAP_VERSION = 1
BOOTSTRAP_FILE = Path("/var/lib/grok2api-quality-guard/bootstrap.json")
DEFAULT_GROK2API_BASE_URL = "http://grok2api:8000"
INTERNAL_API_PREFIX = "/api/internal/v1/quality-guard"
QUALITY_MARKER_PROFILE_ID = "quality-marker"
THROUGHPUT_PROFILE_ID = "throughput"
@@ -105,8 +106,9 @@ class Config:
token = str(payload.get("internal_token") or "").strip()
node_ids = tuple(dict.fromkeys(str(value).strip() for value in values.get("node_ids", []) if str(value).strip()))
rotatable_node_ids = tuple(dict.fromkeys(str(value).strip() for value in values.get("rotatable_node_ids", []) if str(value).strip()))
base_url = os.environ.get("GROK2API_BASE_URL", "").strip() or DEFAULT_GROK2API_BASE_URL
config = cls(
base_url="http://grok2api:8000",
base_url=base_url.rstrip("/"),
internal_token=token,
model=str(values.get("model") or "").strip(),
node_ids=node_ids,
@@ -143,8 +145,20 @@ class Config:
def validate(self) -> None:
parsed = urllib.parse.urlparse(self.base_url)
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
raise ValueError("GROK2API_BASE_URL must be an absolute HTTP(S) URL")
try:
hostname = parsed.hostname
parsed.port
except ValueError:
hostname = None
if (
parsed.scheme not in {"http", "https"}
or not hostname
or parsed.username is not None
or parsed.password is not None
or "?" in self.base_url
or "#" in self.base_url
):
raise ValueError("GROK2API_BASE_URL must be an absolute HTTP(S) URL without credentials, query, or fragment")
if not self.internal_token:
raise ValueError("quality guard bootstrap internal token is missing")
if not self.model or not self.prompt or not self.expected:
@@ -1,11 +1,13 @@
import importlib.util
import json
import os
import stat
import sys
import tempfile
import time
import unittest
from pathlib import Path
from unittest import mock
MODULE_PATH = Path(__file__).with_name("quality_guard.py")
@@ -226,8 +228,50 @@ class ConfigTests(unittest.TestCase):
"rotation_timeout_seconds": 45, "rotatable_node_ids": [],
},
}), encoding="utf-8")
loaded = quality_guard.Config.from_bootstrap(path)
with mock.patch.dict(os.environ, {}, clear=True):
loaded = quality_guard.Config.from_bootstrap(path)
self.assertEqual((loaded.node_ids, loaded.internal_token), (("2", "9"), "scoped-secret"))
self.assertEqual(loaded.base_url, "http://grok2api:8000")
def test_bootstrap_honors_grok2api_base_url(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "bootstrap.json"
path.write_text(json.dumps({
"version": 1,
"enabled": True,
"internal_token": "scoped-secret",
"config": {
"model": "grok-4.5", "node_ids": ["2"], "mode": "hybrid",
"prompt": "probe", "expected": "QUALITY_OK",
"active_interval_seconds": 1800, "passive_poll_seconds": 5,
"soft_tps": 500, "hard_tps": 1000, "consecutive_soft": 2, "consecutive_errors": 2,
"quarantine_seconds": 300, "no_account_backoff_seconds": 300,
"min_healthy_nodes": 1, "max_output_tokens": 384, "fail_closed": False,
"min_generation_ms": 1000, "rotation_url": "", "rotation_token": "",
"rotation_timeout_seconds": 45, "rotatable_node_ids": [],
},
}), encoding="utf-8")
with mock.patch.dict(os.environ, {"GROK2API_BASE_URL": " http://127.0.0.1:18182/ "}, clear=True):
loaded = quality_guard.Config.from_bootstrap(path)
self.assertEqual(loaded.base_url, "http://127.0.0.1:18182")
with mock.patch.dict(os.environ, {"GROK2API_BASE_URL": " "}, clear=True):
loaded = quality_guard.Config.from_bootstrap(path)
self.assertEqual(loaded.base_url, "http://grok2api:8000")
def test_rejects_unsafe_grok2api_base_urls(self):
invalid = (
"http://127.0.0.1:18182?x=1",
"http://127.0.0.1:18182?",
"http://127.0.0.1:18182#fragment",
"http://127.0.0.1:18182#",
"http://user:pass@127.0.0.1:18182",
"http://127.0.0.1:invalid",
"http://:18182",
)
for base_url in invalid:
with self.subTest(base_url=base_url), self.assertRaisesRegex(ValueError, "GROK2API_BASE_URL"):
config(base_url=base_url).validate()
def test_disabled_bootstrap_exits_cleanly(self):
with tempfile.TemporaryDirectory() as directory: