fix(settings): serialize gateway configuration updates
This commit is contained in:
1 parent
731b8fc2ed
commit
dec89a49a3
13 files changed
+347
-75
No files matched your search
@@ -502,10 +502,10 @@ def _known_preset_names() -> set[str]:
|
||||
return {preset.name for preset in MCP_PRESETS}
|
||||
|
||||
|
||||
def _known_mcp_names() -> set[str]:
|
||||
def _known_mcp_names(config_path: Path | None = None) -> set[str]:
|
||||
names = _known_preset_names()
|
||||
with suppress(Exception):
|
||||
names.update(load_config().tools.mcp_servers)
|
||||
names.update(load_config(config_path).tools.mcp_servers)
|
||||
return names
|
||||
|
||||
|
||||
@@ -518,11 +518,15 @@ def _clip_ws_string(value: Any, limit: int = 240) -> str | None:
|
||||
return text[:limit]
|
||||
|
||||
|
||||
def normalize_mcp_preset_mentions(raw: Any) -> list[dict[str, Any]]:
|
||||
def normalize_mcp_preset_mentions(
|
||||
raw: Any,
|
||||
*,
|
||||
config_path: Path | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Sanitize structured MCP preset mentions sent by the WebUI."""
|
||||
if not isinstance(raw, list):
|
||||
return []
|
||||
known = _known_mcp_names()
|
||||
known = _known_mcp_names(config_path)
|
||||
out: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
for item_value in cast(list[object], raw)[:8]:
|
||||
|
||||
@@ -8,6 +8,8 @@ from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any, TypeVar
|
||||
|
||||
from filelock import FileLock
|
||||
|
||||
from nanobot.config.loader import load_config, save_config
|
||||
from nanobot.config.schema import Config
|
||||
|
||||
@@ -16,11 +18,14 @@ _WEBUI_OAUTH_MAX_FLOWS = 8
|
||||
|
||||
|
||||
class WebUISettingsConfig:
|
||||
"""Instance-scoped config access with serialized read-modify-write operations."""
|
||||
"""Path-scoped config access with process-safe read-modify-write operations."""
|
||||
|
||||
def __init__(self, config_path: Path) -> None:
|
||||
self.path = config_path.expanduser().resolve(strict=False)
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self._lock = threading.RLock()
|
||||
lock_path = self.path.with_suffix(f"{self.path.suffix}.lock")
|
||||
self._file_lock = FileLock(str(lock_path))
|
||||
|
||||
def load(self) -> Config:
|
||||
"""Load this gateway's config without consulting the process-global path."""
|
||||
@@ -28,16 +33,16 @@ class WebUISettingsConfig:
|
||||
return load_config(self.path)
|
||||
|
||||
def update(self, mutation: Callable[[Config], _T]) -> _T:
|
||||
"""Apply and atomically persist one in-process read-modify-write operation."""
|
||||
with self._lock:
|
||||
"""Apply and atomically persist one path-scoped read-modify-write operation."""
|
||||
with self._lock, self._file_lock:
|
||||
config = load_config(self.path)
|
||||
result = mutation(config)
|
||||
save_config(config, self.path)
|
||||
return result
|
||||
|
||||
def run_serialized(self, operation: Callable[[Path], _T]) -> _T:
|
||||
"""Run a path-aware read-modify-write operation under the instance lock."""
|
||||
with self._lock:
|
||||
"""Run a path-aware read-modify-write operation under the config-file lock."""
|
||||
with self._lock, self._file_lock:
|
||||
return operation(self.path)
|
||||
|
||||
|
||||
|
||||
@@ -73,10 +73,11 @@ def set_webui_skill_enabled(
|
||||
*,
|
||||
enabled: bool,
|
||||
disabled_skills: set[str],
|
||||
config_path: Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Persist and apply one skill's enabled state."""
|
||||
_require_skill_entry(workspace_path, name)
|
||||
config = load_config()
|
||||
config = load_config(config_path)
|
||||
next_disabled = set(config.agents.defaults.disabled_skills)
|
||||
if enabled:
|
||||
next_disabled.discard(name)
|
||||
@@ -84,7 +85,7 @@ def set_webui_skill_enabled(
|
||||
next_disabled.add(name)
|
||||
if next_disabled != set(config.agents.defaults.disabled_skills):
|
||||
config.agents.defaults.disabled_skills = sorted(next_disabled)
|
||||
save_config(config)
|
||||
save_config(config, config_path)
|
||||
disabled_skills.clear()
|
||||
disabled_skills.update(next_disabled)
|
||||
return {"name": name, "enabled": enabled, "deleted": False}
|
||||
@@ -95,6 +96,7 @@ def delete_webui_skill(
|
||||
name: str,
|
||||
*,
|
||||
disabled_skills: set[str],
|
||||
config_path: Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Delete one workspace skill and remove its disabled-state entry."""
|
||||
entry = _require_skill_entry(workspace_path, name)
|
||||
@@ -116,7 +118,7 @@ def delete_webui_skill(
|
||||
if not target.is_symlink() and not target.is_dir():
|
||||
raise SkillManagementError("skill directory was not found", status=404)
|
||||
|
||||
config = load_config()
|
||||
config = load_config(config_path)
|
||||
original_disabled = list(config.agents.defaults.disabled_skills)
|
||||
next_disabled = set(original_disabled)
|
||||
if name in next_disabled:
|
||||
@@ -127,7 +129,7 @@ def delete_webui_skill(
|
||||
try:
|
||||
if next_disabled != set(original_disabled):
|
||||
config.agents.defaults.disabled_skills = sorted(next_disabled)
|
||||
save_config(config)
|
||||
save_config(config, config_path)
|
||||
except Exception:
|
||||
config.agents.defaults.disabled_skills = original_disabled
|
||||
staged_target.replace(target)
|
||||
|
||||
@@ -6,6 +6,7 @@ the WebUI-specific audio transcription action carried over that socket.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from nanobot.audio.transcription import (
|
||||
@@ -18,7 +19,11 @@ from nanobot.config.loader import load_config
|
||||
_MAX_REQUEST_ID_LENGTH = 80
|
||||
|
||||
|
||||
async def webui_transcription_event(envelope: dict[str, Any]) -> tuple[str, dict[str, Any]]:
|
||||
async def webui_transcription_event(
|
||||
envelope: dict[str, Any],
|
||||
*,
|
||||
config_path: Path | None = None,
|
||||
) -> tuple[str, dict[str, Any]]:
|
||||
"""Return the WS event name and payload for one WebUI transcription request."""
|
||||
request_id = envelope.get("request_id")
|
||||
valid_request_id = (
|
||||
@@ -38,7 +43,7 @@ async def webui_transcription_event(envelope: dict[str, Any]) -> tuple[str, dict
|
||||
try:
|
||||
text = await transcribe_audio_data_url(
|
||||
envelope.get("data_url"),
|
||||
resolve_transcription_config(load_config()),
|
||||
resolve_transcription_config(load_config(config_path)),
|
||||
duration_ms=envelope.get("duration_ms"),
|
||||
)
|
||||
except TranscriptionIngressError as exc:
|
||||
|
||||
+30
-17
@@ -254,10 +254,10 @@ def _request_query(request: WsRequest) -> dict[str, list[str]]:
|
||||
return query
|
||||
|
||||
|
||||
def _default_model_name_from_config() -> str | None:
|
||||
def _default_model_name_from_config(config_path: Path | None = None) -> str | None:
|
||||
try:
|
||||
from nanobot.config.loader import load_config
|
||||
model = load_config().resolve_preset().model.strip()
|
||||
model = load_config(config_path).resolve_preset().model.strip()
|
||||
return model or None
|
||||
except Exception as e:
|
||||
logger.debug("bootstrap model_name could not load from config: {}", e)
|
||||
@@ -266,6 +266,7 @@ def _default_model_name_from_config() -> str | None:
|
||||
|
||||
def _resolve_bootstrap_model_name(
|
||||
runtime_name: Callable[[], str | None] | None,
|
||||
config_path: Path | None = None,
|
||||
) -> str:
|
||||
if runtime_name is not None:
|
||||
try:
|
||||
@@ -277,7 +278,7 @@ def _resolve_bootstrap_model_name(
|
||||
stripped = raw.strip()
|
||||
if stripped:
|
||||
return stripped
|
||||
return _default_model_name_from_config() or ""
|
||||
return _default_model_name_from_config(config_path) or ""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -603,7 +604,10 @@ class GatewayHTTPHandler:
|
||||
"limits": self.ingress.bootstrap_limits(
|
||||
max_frame_bytes=self.config.max_message_bytes,
|
||||
),
|
||||
"model_name": _resolve_bootstrap_model_name(self.runtime_model_name),
|
||||
"model_name": _resolve_bootstrap_model_name(
|
||||
self.runtime_model_name,
|
||||
self.settings.config.path,
|
||||
),
|
||||
"runtime_surface": self._runtime_surface,
|
||||
"runtime_capabilities": self._capabilities,
|
||||
}
|
||||
@@ -634,7 +638,10 @@ class GatewayHTTPHandler:
|
||||
"limits": self.ingress.bootstrap_limits(
|
||||
max_frame_bytes=self.config.max_message_bytes,
|
||||
),
|
||||
"model_name": _resolve_bootstrap_model_name(self.runtime_model_name),
|
||||
"model_name": _resolve_bootstrap_model_name(
|
||||
self.runtime_model_name,
|
||||
self.settings.config.path,
|
||||
),
|
||||
"runtime_surface": self._runtime_surface,
|
||||
"runtime_capabilities": self._capabilities,
|
||||
}
|
||||
@@ -1236,9 +1243,9 @@ class GatewayHTTPHandler:
|
||||
if _is_local_browser_request(connection, request.headers):
|
||||
return True
|
||||
try:
|
||||
from nanobot.config.loader import load_config
|
||||
|
||||
return bool(load_config().tools.webui_allow_remote_package_install)
|
||||
return bool(
|
||||
self.settings.config.load().tools.webui_allow_remote_package_install
|
||||
)
|
||||
except Exception:
|
||||
self._log.exception("failed to load remote package install policy")
|
||||
return False
|
||||
@@ -1252,11 +1259,14 @@ class GatewayHTTPHandler:
|
||||
if raw_enabled not in {"true", "false"}:
|
||||
return _http_error(400, "enabled must be true or false")
|
||||
try:
|
||||
action = set_webui_skill_enabled(
|
||||
self.skills_workspace_path,
|
||||
name,
|
||||
enabled=raw_enabled == "true",
|
||||
disabled_skills=self.disabled_skills,
|
||||
action = self.settings.config.run_serialized(
|
||||
lambda config_path: set_webui_skill_enabled(
|
||||
self.skills_workspace_path,
|
||||
name,
|
||||
enabled=raw_enabled == "true",
|
||||
disabled_skills=self.disabled_skills,
|
||||
config_path=config_path,
|
||||
)
|
||||
)
|
||||
except SkillManagementError as exc:
|
||||
return _http_error(exc.status, exc.message)
|
||||
@@ -1280,10 +1290,13 @@ class GatewayHTTPHandler:
|
||||
return _http_error(403, "remote skill deletion is disabled")
|
||||
name = _query_first(_request_query(request), "name") or ""
|
||||
try:
|
||||
action = delete_webui_skill(
|
||||
self.skills_workspace_path,
|
||||
name,
|
||||
disabled_skills=self.disabled_skills,
|
||||
action = self.settings.config.run_serialized(
|
||||
lambda config_path: delete_webui_skill(
|
||||
self.skills_workspace_path,
|
||||
name,
|
||||
disabled_skills=self.disabled_skills,
|
||||
config_path=config_path,
|
||||
)
|
||||
)
|
||||
except SkillManagementError as exc:
|
||||
return _http_error(exc.status, exc.message)
|
||||
|
||||
Reference in new issue
Block a user