sync update from LAN repo (source dhk-1 @ 3e87be55)
This commit is contained in:
1 parent
ab6068a7c1
commit
42f2a26fa8
1482 files changed
+227759
-9584
No files matched your search
+148
@@ -1,13 +1,25 @@
|
||||
"""Unified extensions configuration for MCP servers and skills."""
|
||||
|
||||
import copy
|
||||
import errno
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
from collections.abc import Callable, Iterator
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Any, Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from deerflow.config.runtime_paths import existing_project_file
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_EXTENSIONS_CONFIG_WRITE_LOCK = threading.Lock()
|
||||
|
||||
|
||||
class McpOAuthConfig(BaseModel):
|
||||
@@ -350,6 +362,142 @@ def reload_extensions_config(config_path: str | None = None) -> ExtensionsConfig
|
||||
return _extensions_config
|
||||
|
||||
|
||||
def _read_raw_extensions_config(config_path: Path) -> dict[str, Any]:
|
||||
"""Read the raw JSON document without resolving env placeholders."""
|
||||
if not config_path.exists():
|
||||
return {}
|
||||
with config_path.open(encoding="utf-8") as config_file:
|
||||
document = json.load(config_file)
|
||||
if not isinstance(document, dict):
|
||||
raise ValueError("Extensions config root must be a JSON object")
|
||||
return document
|
||||
|
||||
|
||||
def read_extensions_config_document(config_path: str | Path) -> dict[str, Any]:
|
||||
"""Return a fresh raw document while coordinating with all writers."""
|
||||
path = Path(config_path)
|
||||
with _EXTENSIONS_CONFIG_WRITE_LOCK:
|
||||
with _extensions_config_process_lock(path):
|
||||
return _read_raw_extensions_config(path)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _extensions_config_process_lock(config_path: Path) -> Iterator[None]:
|
||||
"""Serialize writers across processes on Windows and POSIX.
|
||||
|
||||
The stable sidecar is intentional: deleting it after unlock can let two
|
||||
processes lock different inodes. Keeping it beside the config also lets
|
||||
multiple processes sharing the writable config directory coordinate.
|
||||
"""
|
||||
lock_path = config_path.with_name(f".{config_path.name}.lock")
|
||||
lock_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with lock_path.open("a+b") as lock_file:
|
||||
if os.name == "nt":
|
||||
import msvcrt
|
||||
|
||||
lock_file.seek(0, os.SEEK_END)
|
||||
if lock_file.tell() == 0:
|
||||
lock_file.write(b"\0")
|
||||
lock_file.flush()
|
||||
lock_file.seek(0)
|
||||
while True:
|
||||
try:
|
||||
msvcrt.locking(lock_file.fileno(), msvcrt.LK_NBLCK, 1)
|
||||
break
|
||||
except OSError as exc:
|
||||
if exc.errno not in {errno.EACCES, errno.EAGAIN, errno.EDEADLK}:
|
||||
raise
|
||||
time.sleep(0.05)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
lock_file.seek(0)
|
||||
msvcrt.locking(lock_file.fileno(), msvcrt.LK_UNLCK, 1)
|
||||
else:
|
||||
import fcntl
|
||||
|
||||
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
|
||||
|
||||
|
||||
def _write_raw_extensions_config(config_path: Path, document: dict[str, Any]) -> None:
|
||||
"""Durably replace raw JSON, with a bind-mounted-file EBUSY fallback."""
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
payload = json.dumps(document, indent=2, ensure_ascii=False) + "\n"
|
||||
tmp_path: Path | None = None
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(
|
||||
"w",
|
||||
encoding="utf-8",
|
||||
delete=False,
|
||||
dir=str(config_path.parent),
|
||||
) as tmp_file:
|
||||
tmp_file.write(payload)
|
||||
tmp_file.flush()
|
||||
os.fsync(tmp_file.fileno())
|
||||
tmp_path = Path(tmp_file.name)
|
||||
try:
|
||||
os.replace(tmp_path, config_path)
|
||||
except OSError as exc:
|
||||
if exc.errno != errno.EBUSY:
|
||||
raise
|
||||
logger.warning(
|
||||
"Atomic replace is unavailable for bind-mounted extensions config %s; using in-place fallback",
|
||||
config_path,
|
||||
)
|
||||
previous = config_path.read_bytes() if config_path.exists() else None
|
||||
try:
|
||||
mode = "r+" if config_path.exists() else "w"
|
||||
with config_path.open(mode, encoding="utf-8") as config_file:
|
||||
config_file.seek(0)
|
||||
config_file.write(payload)
|
||||
config_file.truncate()
|
||||
config_file.flush()
|
||||
os.fsync(config_file.fileno())
|
||||
except Exception:
|
||||
if previous is not None:
|
||||
try:
|
||||
with config_path.open("wb") as config_file:
|
||||
config_file.write(previous)
|
||||
config_file.flush()
|
||||
os.fsync(config_file.fileno())
|
||||
except Exception:
|
||||
logger.exception("Failed to restore extensions config after in-place write failure")
|
||||
raise
|
||||
finally:
|
||||
if tmp_path is not None and tmp_path.exists():
|
||||
tmp_path.unlink()
|
||||
|
||||
|
||||
def patch_extensions_config(
|
||||
config_path: str | Path,
|
||||
patch: Callable[[dict[str, Any]], None],
|
||||
) -> ExtensionsConfig:
|
||||
"""Apply a raw-document patch under thread and cross-process locks.
|
||||
|
||||
Callers should mutate only the fields they own. The document is read fresh
|
||||
while locked, so concurrent MCP, skill-state, and display writes cannot
|
||||
overwrite one another or resolve/persist secret ``$ENV`` placeholders.
|
||||
"""
|
||||
path = Path(config_path)
|
||||
with _EXTENSIONS_CONFIG_WRITE_LOCK:
|
||||
with _extensions_config_process_lock(path):
|
||||
document = _read_raw_extensions_config(path)
|
||||
patch(document)
|
||||
if not isinstance(document, dict):
|
||||
raise ValueError("Extensions config root must be a JSON object")
|
||||
# Validate a deep copy before persisting. Environment expansion is
|
||||
# intentionally confined to the copy so placeholders remain raw.
|
||||
validation_document = copy.deepcopy(document)
|
||||
ExtensionsConfig.resolve_env_variables(validation_document)
|
||||
ExtensionsConfig.model_validate(validation_document)
|
||||
_write_raw_extensions_config(path, document)
|
||||
return reload_extensions_config(str(path))
|
||||
|
||||
|
||||
def reset_extensions_config() -> None:
|
||||
"""Reset the cached extensions config instance.
|
||||
|
||||
|
||||
Reference in new issue
Block a user