Skip to content

Agent Zero AI framework

refactor: add file system watchdog support for API handlers, extensions, and plugins

frdele4f974b80dcb
diff --git a/extensions/python/__main___init_a0_end/_10_register_watchdogs.py b/extensions/python/__main___init_a0_end/_10_register_watchdogs.py
new file mode 100644
index 000000000..e61001e03
--- /dev/null
+++ b/extensions/python/__main___init_a0_end/_10_register_watchdogs.py
@@ -0,0 +1,11 @@
+from helpers.extension import Extension
+
+
+class RegisterWatchDogs(Extension):
+
+ def execute(self, **kwargs):
+ from helpers.plugins import register_watchdogs as register_plugins_watchdogs
+ from helpers.api import register_watchdogs as register_api_watchdogs
+
+ register_plugins_watchdogs()
+ register_api_watchdogs()
\ No newline at end of file
diff --git a/extensions/python/job_loop/_50_trim_cache.py b/extensions/python/job_loop/_50_trim_cache.py
new file mode 100644
index 000000000..74989fa86
--- /dev/null
+++ b/extensions/python/job_loop/_50_trim_cache.py
@@ -0,0 +1,9 @@
+from typing import Any
+from helpers.extension import Extension
+from helpers import cache
+
+
+class SaveToolCallFile(Extension):
+ def execute(self, data: dict[str, Any] | None = None, **kwargs):
+ # trim unused cache entries
+ cache.trim_cache("*", seconds=300)
diff --git a/helpers/api.py b/helpers/api.py
index 1ac40655f..52d0fe5c5 100644
--- a/helpers/api.py
+++ b/helpers/api.py
@@ -6,7 +6,17 @@ import threading
from functools import wraps
from pathlib import Path
from typing import Union, TypedDict, Dict, Any
-from flask import Request, Response, jsonify, Flask, session, request, send_file, redirect, url_for
+from flask import (
+ Request,
+ Response,
+ jsonify,
+ Flask,
+ session,
+ request,
+ send_file,
+ redirect,
+ url_for,
+)
from werkzeug.wrappers.response import Response as BaseResponse
from agent import AgentContext
from initialize import initialize_agent
@@ -17,7 +27,7 @@ from helpers import files, cache
ThreadLockType = Union[threading.Lock, threading.RLock]
CACHE_AREA = "api_handlers(api)"
-cache.toggle_area(CACHE_AREA, False) # cache off for now
+cache.toggle_area(CACHE_AREA, False) # cache off for now
Input = dict
Output = Union[Dict[str, Any], Response, TypedDict] # type: ignore
@@ -69,7 +79,6 @@ class ApiHandler:
# input_data = {"data": request.get_data(as_text=True)}
input_data = {}
-
# process via handler
output = await self.process(input_data, request)
@@ -102,19 +111,20 @@ class ApiHandler:
if got:
return got
if create_if_not_exists:
- context = AgentContext(config=initialize_agent(), id=ctxid, set_current=True)
+ context = AgentContext(
+ config=initialize_agent(), id=ctxid, set_current=True
+ )
return context
else:
raise Exception(f"Context {ctxid} not found")
-
-
def is_loopback_address(address: str) -> bool:
loopback_checker = {
socket.AF_INET: lambda x: (
struct.unpack("!I", socket.inet_aton(x))[0] >> (32 - 8)
- ) == 127,
+ )
+ == 127,
socket.AF_INET6: lambda x: x == "::1",
}
address_type = "hostname"
@@ -148,6 +158,7 @@ def requires_api_key(f):
@wraps(f)
async def decorated(*args, **kwargs):
from helpers.settings import get_settings
+
valid_api_key = get_settings()["mcp_server_token"]
if api_key := request.headers.get("X-API-KEY"):
@@ -178,6 +189,7 @@ def requires_auth(f):
@wraps(f)
async def decorated(*args, **kwargs):
from helpers import login
+
user_pass_hash = login.get_credentials_hash()
if not user_pass_hash:
return await f(*args, **kwargs)
@@ -192,6 +204,7 @@ def csrf_protect(f):
@wraps(f)
async def decorated(*args, **kwargs):
from helpers import runtime
+
token = session.get("csrf_token")
header = request.headers.get("X-CSRF-Token")
cookie = request.cookies.get("csrf_token_" + runtime.get_runtime_id())
@@ -219,7 +232,9 @@ def register_api_route(app: Flask, lock: ThreadLockType) -> None:
# Check built-in python/api/<path>.py
builtin_file = files.get_abs_path(f"api/{path}.py")
- if files.is_in_dir(builtin_file, files.get_abs_path("api")) and files.exists(builtin_file):
+ if files.is_in_dir(builtin_file, files.get_abs_path("api")) and files.exists(
+ builtin_file
+ ):
classes = load_classes_from_file(builtin_file, ApiHandler)
if classes:
handler_cls = classes[0]
@@ -269,3 +284,18 @@ def register_api_route(app: Flask, lock: ThreadLockType) -> None:
methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
)
+
+def register_watchdogs():
+ from helpers import watchdog
+
+ def on_api_change(items:list[watchdog.WatchItem]):
+ PrintStyle.debug("API endpoint watchdog triggered:", items)
+ cache.clear(CACHE_AREA)
+
+
+ watchdog.add_watchdog(
+ "api_handlers",
+ roots=[files.get_abs_path("api")],
+ patterns=["*.py"],
+ handler=on_api_change,
+ )
diff --git a/helpers/cache.py b/helpers/cache.py
index f7a7a0257..a9d8d283d 100644
--- a/helpers/cache.py
+++ b/helpers/cache.py
@@ -1,14 +1,22 @@
import fnmatch
import threading
+import time
+from dataclasses import dataclass
from typing import Any
_lock = threading.RLock()
-_cache: dict[str, dict[str, Any]] = {}
+_cache: dict[str, dict[str, "CacheEntry"]] = {}
_enabled_global: bool = True
_enabled_areas: dict[str, bool] = {}
+@dataclass(slots=True)
+class CacheEntry:
+ value: Any
+ timestamp: float
+
+
def toggle_global(enabled: bool) -> None:
global _enabled_global
_enabled_global = enabled
@@ -22,7 +30,11 @@ def has(area: str, key: Any) -> bool:
if not _is_enabled(area):
return False
with _lock:
- return key in _cache.get(area, {})
+ entry = _cache.get(area, {}).get(key)
+ if entry is None:
+ return False
+ _touch_entry(entry)
+ return True
def add(area: str, key: Any, data: Any) -> None:
@@ -31,14 +43,18 @@ def add(area: str, key: Any, data: Any) -> None:
with _lock:
if area not in _cache:
_cache[area] = {}
- _cache[area][key] = data
+ _cache[area][key] = _create_entry(data)
def get(area: str, key: Any, default: Any = None) -> Any:
if not _is_enabled(area):
return default
with _lock:
- return _cache.get(area, {}).get(key, default)
+ entry = _cache.get(area, {}).get(key)
+ if entry is None:
+ return default
+ _touch_entry(entry)
+ return entry.value
def remove(area: str, key: Any) -> None:
@@ -60,6 +76,22 @@ def clear(area: str) -> None:
_cache.pop(area, None)
+def trim_cache(area: str, seconds: float = 300) -> None:
+ cutoff = time.time() - seconds
+ with _lock:
+ for area_key in _get_matching_areas(area):
+ area_cache = _cache.get(area_key)
+ if not area_cache:
+ continue
+
+ keys_to_remove = [key for key, entry in area_cache.items() if entry.timestamp < cutoff]
+ for key in keys_to_remove:
+ area_cache.pop(key, None)
+
+ if not area_cache:
+ _cache.pop(area_key, None)
+
+
def clear_all() -> None:
with _lock:
_cache.clear()
@@ -71,6 +103,20 @@ def _is_enabled(area: str) -> bool:
return _enabled_areas.get(area, True)
+def _create_entry(value: Any) -> CacheEntry:
+ return CacheEntry(value=value, timestamp=time.time())
+
+
+def _touch_entry(entry: CacheEntry) -> None:
+ entry.timestamp = time.time()
+
+
+def _get_matching_areas(area: str) -> list[str]:
+ if any(ch in area for ch in "*?["):
+ return [k for k in _cache.keys() if fnmatch.fnmatch(k, area)]
+ return [area]
+
+
def determine_cache_key(agent, *additional):
if agent:
profile = agent.config.profile or "none"
diff --git a/helpers/extension.py b/helpers/extension.py
index 077127c5d..d16fb6428 100644
--- a/helpers/extension.py
+++ b/helpers/extension.py
@@ -7,6 +7,8 @@ from functools import wraps
import inspect
import os
+from helpers.print_style import PrintStyle
+
if TYPE_CHECKING:
from agent import Agent
@@ -294,4 +296,22 @@ def _get_extensions(folder: str):
classes = extract_tools.load_classes_from_folder(folder, "*", Extension)
cache.add(_EXTENSIONS_CACHE_AREA, folder, classes)
- return classes
\ No newline at end of file
+ return classes
+
+def register_extensions_watchdogs():
+ from helpers import watchdog
+
+ def extensions_changed(items: list[watchdog.WatchItem]):
+ cache.clear(_EXTENSIONS_CACHE_AREA)
+ cache.clear(_CLASSES_CACHE_AREA)
+ PrintStyle.debug("Extensions watchdog triggered:", items)
+
+ watchdog.add_watchdog(
+ id="extensions",
+ roots=[
+ files.get_abs_path(files.EXTENSIONS_DIR),
+ files.get_abs_path(files.USER_DIR, files.EXTENSIONS_DIR)
+ ],
+ handler=extensions_changed
+ )
+ # TODO - watch extensions under projects/agents
\ No newline at end of file
diff --git a/helpers/files.py b/helpers/files.py
index cd3246452..62187f9c2 100644
--- a/helpers/files.py
+++ b/helpers/files.py
@@ -16,6 +16,7 @@ from helpers import yaml
AGENTS_DIR = "agents"
PLUGINS_DIR = "plugins"
PROJECTS_DIR = "projects"
+EXTENSIONS_DIR = "extensions"
USER_DIR = "usr"
TEMP_DIR = "tmp"
_base_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, "../")))
diff --git a/helpers/plugins.py b/helpers/plugins.py
index c6ba5bb62..03e288112 100644
--- a/helpers/plugins.py
+++ b/helpers/plugins.py
@@ -15,6 +15,8 @@ from typing import (
TypedDict,
)
+from regex import W
+
from helpers import (
files,
git,
@@ -23,11 +25,13 @@ from helpers import (
yaml as yaml_helper,
cache,
extension,
+ watchdog,
extract_tools,
)
from pydantic import BaseModel, Field
from helpers.defer import DeferredTask
+from helpers.watchdog import WatchItem
if TYPE_CHECKING:
from agent import Agent
@@ -110,6 +114,53 @@ class PluginUpdateInfo(BaseModel):
error: str = ""
+def register_watchdogs():
+
+ def on_plugin_change(events: list[WatchItem]):
+ plugin_names: list[str] = []
+ for path, _event in events:
+ path = path.replace("\\", "/")
+ if "/plugins/" not in path:
+ continue
+ plugin_name = path.split("/plugins/", 1)[1].split("/", 1)[0]
+ if plugin_name and plugin_name not in plugin_names:
+ plugin_names.append(plugin_name)
+ print_style.PrintStyle.debug("Plugins watchdog triggered", plugin_names)
+ after_plugin_change(plugin_names or None)
+
+ # add watchdogs for plugin roots
+ watchdog.add_watchdog(
+ id="plugins_roots",
+ roots=get_plugin_roots(),
+ handler=on_plugin_change,
+ )
+
+ from helpers import projects
+ from helpers import subagents
+
+ # add watchdogs for plugin overrides in projects/plugins and projects/agents/plugins
+ watchdog.add_watchdog(
+ id="plugins_projects",
+ roots=[files.get_abs_path(projects.PROJECTS_PARENT_DIR)],
+ patterns=[
+ f"*/{projects.PROJECT_META_DIR}/plugins/**/*",
+ f"*/{projects.PROJECT_META_DIR}/agents/*/plugins/**/*",
+ ],
+ handler=on_plugin_change,
+ )
+
+ # add watchdogs for plugin overrides in /agents/plugins and /usr/agents/plugins
+ watchdog.add_watchdog(
+ id="plugins_agents",
+ roots=[
+ files.get_abs_path(subagents.DEFAULT_AGENTS_DIR),
+ files.get_abs_path(subagents.USER_AGENTS_DIR),
+ ],
+ patterns=[f"*/plugins/**/*"],
+ handler=on_plugin_change,
+ )
+
+
@extension.extensible
def after_plugin_change(plugin_names: list[str] | None = None):
clear_plugin_cache()
@@ -215,8 +266,12 @@ def get_enhanced_plugins_list(
return results
-def get_custom_plugins_updates(plugin_names: list[str] | None = None) -> List[PluginUpdateInfo]:
- plugins = get_enhanced_plugins_list(custom=True, builtin=False, plugin_names=plugin_names)
+def get_custom_plugins_updates(
+ plugin_names: list[str] | None = None,
+) -> List[PluginUpdateInfo]:
+ plugins = get_enhanced_plugins_list(
+ custom=True, builtin=False, plugin_names=plugin_names
+ )
results: list[PluginUpdateInfo] = []
for plugin in plugins:
@@ -276,6 +331,7 @@ def uninstall_plugin(plugin_name):
# then delete
delete_plugin(plugin_name)
+
@extension.extensible
def delete_plugin(plugin_name: str):
plugin_dir = find_plugin_dir(plugin_name)
@@ -302,7 +358,9 @@ def get_plugin_paths(*subpaths: str) -> List[str]:
def get_enabled_plugin_paths(agent: Agent | None, *subpaths: str) -> List[str]:
- if cached := cache.get(ENABLED_PLUGINS_PATHS_CACHE_AREA, cache.determine_cache_key(agent, *subpaths)):
+ if cached := cache.get(
+ ENABLED_PLUGINS_PATHS_CACHE_AREA, cache.determine_cache_key(agent, *subpaths)
+ ):
return cached
enabled = get_enabled_plugins(agent)
@@ -321,14 +379,19 @@ def get_enabled_plugin_paths(agent: Agent | None, *subpaths: str) -> List[str]:
path_pattern = files.get_abs_path(base_dir, *subpaths)
paths.extend(files.find_existing_paths_by_pattern(path_pattern))
-
- cache.add(ENABLED_PLUGINS_PATHS_CACHE_AREA, cache.determine_cache_key(agent, *subpaths), paths)
+ cache.add(
+ ENABLED_PLUGINS_PATHS_CACHE_AREA,
+ cache.determine_cache_key(agent, *subpaths),
+ paths,
+ )
return paths
def get_enabled_plugins(agent: Agent | None):
- if cached := cache.get(ENABLED_PLUGINS_LIST_CACHE_AREA, cache.determine_cache_key(agent)):
+ if cached := cache.get(
+ ENABLED_PLUGINS_LIST_CACHE_AREA, cache.determine_cache_key(agent)
+ ):
return cached
plugins = get_plugins_list()
@@ -496,7 +559,7 @@ def get_plugin_config(
agent_profile=agent_profile,
)
- return result
+ return result
def get_default_plugin_config(plugin_name: str):
@@ -506,9 +569,7 @@ def get_default_plugin_config(plugin_name: str):
# call plugin hook to get the result
result = call_plugin_hook(
- plugin_name,
- "get_default_plugin_config",
- file_path = file_path
+ plugin_name, "get_default_plugin_config", file_path=file_path
)
# or do standard load
@@ -544,8 +605,6 @@ def save_plugin_config(
after_plugin_change([plugin_name])
-
-
def find_plugin_asset(
plugin_name: str, *subpaths: str, project_name="", agent_profile=""
):
@@ -732,7 +791,9 @@ def send_frontend_reload_notification(plugin_names: list[str] | None = None):
DeferredTask().start_task(_send_later)
-def call_plugin_hook(plugin_name: str, hook_name: str, default: Any=None, *args, **kwargs):
+def call_plugin_hook(
+ plugin_name: str, hook_name: str, default: Any = None, *args, **kwargs
+):
hooks = None
# use cached hooks if enabled
diff --git a/helpers/watchdog.py b/helpers/watchdog.py
new file mode 100644
index 000000000..ce93bd8ee
--- /dev/null
+++ b/helpers/watchdog.py
@@ -0,0 +1,422 @@
+from __future__ import annotations
+
+import os
+import threading
+from dataclasses import dataclass
+from pathlib import PurePosixPath
+from typing import Any, Callable, Iterable, Literal, cast
+from watchdog.observers import Observer as _WatchdogObserver
+
+
+class _DispatchHandler:
+ def __init__(self, registry: "_WatchRegistry", scheduled_root: str):
+ self.registry = registry
+ self.scheduled_root = scheduled_root
+
+ def dispatch(self, event: Any):
+ self.registry.dispatch(self.scheduled_root, event)
+
+
+WatchEvent = Literal["create", "modify", "delete", "move"]
+WatchEvents = Literal["all"] | list[WatchEvent | str] | set[WatchEvent | str]
+WatchItem = list[str]
+WatchHandler = Callable[[list[WatchItem]], None]
+PatternMatcher = Callable[[str], bool]
+
+_DEFAULT_PATTERNS = ["**/*"]
+_DEFAULT_IGNORE_PATTERNS = [
+ "**/__pycache__",
+ "**/__pycache__/*",
+ "**/*.pyc",
+ "**/*.pyo",
+]
+_VALID_EVENTS: frozenset[WatchEvent] = frozenset(["create", "modify", "delete", "move"])
+_EVENT_ALIASES: dict[str, WatchEvent] = {
+ "create": "create",
+ "created": "create",
+ "modify": "modify",
+ "modified": "modify",
+ "delete": "delete",
+ "deleted": "delete",
+ "move": "move",
+ "moved": "move",
+}
+
+
+@dataclass(frozen=True)
+class _Watch:
+ id: str
+ root: str
+ root_with_sep: str
+ patterns: list[str]
+ ignore_patterns: list[str]
+ matcher: PatternMatcher
+ events: frozenset[WatchEvent]
+ debounce: float
+ handler: WatchHandler
+
+
+@dataclass
+class _PendingBatch:
+ items_by_path: dict[str, WatchItem]
+ timer: threading.Timer | None = None
+
+
+class _WatchRegistry:
+ def __init__(self):
+ self._lock = threading.RLock()
+ self._observer: Any = None
+ self._watches: dict[str, _Watch] = {}
+ self._watch_ids_by_group: dict[str, set[str]] = {}
+ self._scheduled_roots: set[str] = set()
+ self._pending_batches: dict[str, _PendingBatch] = {}
+
+ def add(
+ self,
+ id: str,
+ roots: list[str],
+ patterns: list[str] | None,
+ ignore_patterns: list[str] | None,
+ events: WatchEvents,
+ debounce: float,
+ handler: WatchHandler,
+ ) -> None:
+ self._ensure_watchdog_available()
+ normalized_roots = _normalize_roots(roots)
+ normalized_patterns = _normalize_patterns(patterns)
+ normalized_ignore_patterns = _normalize_patterns(
+ ignore_patterns, default=_DEFAULT_IGNORE_PATTERNS
+ )
+ normalized_events = _normalize_events(events)
+ normalized_debounce = _normalize_debounce(debounce)
+ watch_ids = [id] if len(normalized_roots) == 1 else [f"{id}:{index}" for index in range(len(normalized_roots))]
+ watches = {
+ watch_id: _Watch(
+ id=watch_id,
+ root=normalized_root,
+ root_with_sep=normalized_root + os.sep,
+ patterns=normalized_patterns,
+ ignore_patterns=normalized_ignore_patterns,
+ matcher=_compile_matcher(
+ normalized_root,
+ normalized_patterns,
+ normalized_ignore_patterns,
+ ),
+ events=normalized_events,
+ debounce=normalized_debounce,
+ handler=handler,
+ )
+ for watch_id, normalized_root in zip(watch_ids, normalized_roots)
+ }
+ with self._lock:
+ previous_watch_ids = self._watch_ids_by_group.pop(id, set())
+ for watch_id in previous_watch_ids:
+ self._watches.pop(watch_id, None)
+ pending = self._pending_batches.pop(watch_id, None)
+ if pending and pending.timer:
+ pending.timer.cancel()
+ self._watches.update(watches)
+ self._watch_ids_by_group[id] = set(watches)
+ self._refresh_observer()
+
+ def remove(self, id: str) -> bool:
+ with self._lock:
+ watch_ids = self._watch_ids_by_group.pop(id, {id})
+ removed = False
+ for watch_id in watch_ids:
+ removed = self._watches.pop(watch_id, None) is not None or removed
+ pending = self._pending_batches.pop(watch_id, None)
+ if pending and pending.timer:
+ pending.timer.cancel()
+ if removed:
+ self._refresh_observer()
+ return removed
+
+ def clear(self) -> None:
+ with self._lock:
+ self._watches.clear()
+ self._watch_ids_by_group.clear()
+ pending_batches = list(self._pending_batches.values())
+ self._pending_batches.clear()
+ self._refresh_observer()
+ for pending in pending_batches:
+ if pending.timer:
+ pending.timer.cancel()
+
+ def start(self) -> None:
+ with self._lock:
+ observer = self._observer
+ if observer is None:
+ observer = self._create_observer()
+ self._observer = observer
+ if observer.is_alive():
+ return
+ observer.start()
+
+ def stop(self) -> None:
+ self._stop_observer()
+
+ def dispatch(self, scheduled_root: str, event: Any) -> None:
+ event_type = _map_event_type(str(getattr(event, "event_type", "")))
+ if event_type is None:
+ return
+ if bool(getattr(event, "is_synthetic", False)):
+ return
+ paths: list[str] = []
+ src_path = getattr(event, "src_path", None)
+ if isinstance(src_path, str) and src_path:
+ paths.append(os.path.abspath(src_path))
+ dest_path = getattr(event, "dest_path", None)
+ if event_type == "move" and isinstance(dest_path, str) and dest_path:
+ paths.append(os.path.abspath(dest_path))
+ with self._lock:
+ watches = list(self._watches.values())
+ for path in paths:
+ if not _is_same_or_nested(path, scheduled_root):
+ continue
+ for watch in watches:
+ if event_type not in watch.events:
+ continue
+ if not _is_under_watch(path, watch):
+ continue
+ if not watch.matcher(path):
+ continue
+ self._queue_event(watch, path, event_type)
+
+ def _ensure_watchdog_available(self) -> None:
+ return None
+
+ def _queue_event(self, watch: _Watch, path: str, event_type: WatchEvent) -> None:
+ item: WatchItem = [path, event_type]
+ if watch.debounce <= 0:
+ watch.handler([item])
+ return
+ with self._lock:
+ pending = self._pending_batches.get(watch.id)
+ if pending is None:
+ pending = _PendingBatch(items_by_path={})
+ self._pending_batches[watch.id] = pending
+ pending.items_by_path[path] = item
+ timer = pending.timer
+ if timer:
+ timer.cancel()
+ pending.timer = threading.Timer(watch.debounce, self._flush_watch_batch, args=(watch.id,))
+ pending.timer.daemon = True
+ pending.timer.start()
+
+ def _flush_watch_batch(self, watch_id: str) -> None:
+ items: list[WatchItem] = []
+ handler: WatchHandler | None = None
+ with self._lock:
+ watch = self._watches.get(watch_id)
+ pending = self._pending_batches.pop(watch_id, None)
+ if watch is None or pending is None:
+ return
+ if pending.timer:
+ pending.timer.cancel()
+ items = list(pending.items_by_path.values())
+ handler = watch.handler
+ if items:
+ handler(items)
+
+ def _refresh_observer(self) -> None:
+ target_roots = _covering_roots(watch.root for watch in self._watches.values())
+ if not target_roots:
+ self._stop_observer()
+ return
+ observer = self._observer
+ if observer is None:
+ observer = self._create_observer()
+ self._observer = observer
+ observer.start()
+ if target_roots == self._scheduled_roots:
+ return
+ observer = cast(Any, observer)
+ observer.unschedule_all()
+ for root in target_roots:
+ observer.schedule(_DispatchHandler(self, root), root, recursive=True)
+ self._scheduled_roots = target_roots
+
+ def _stop_observer(self) -> None:
+ with self._lock:
+ observer = self._observer
+ self._observer = None
+ self._scheduled_roots = set()
+ if observer is None:
+ return
+ observer.unschedule_all()
+ observer.stop()
+ observer.join()
+
+ def _create_observer(self) -> Any:
+ observer = cast(Any, _WatchdogObserver())
+ return observer
+
+
+def _normalize_root(root: str) -> str:
+ normalized = os.path.abspath(os.path.normpath(root))
+ if not os.path.exists(normalized):
+ raise FileNotFoundError(normalized)
+ if not os.path.isdir(normalized):
+ raise NotADirectoryError(normalized)
+ return normalized
+
+
+def _normalize_roots(roots: list[str]) -> list[str]:
+ normalized = list(dict.fromkeys(_normalize_root(item) for item in roots))
+ if not normalized:
+ raise ValueError("roots must not be empty")
+ return normalized
+
+
+def _normalize_patterns(
+ patterns: list[str] | None,
+ default: list[str] | None = None,
+) -> list[str]:
+ default = default or _DEFAULT_PATTERNS
+ if not patterns:
+ return list(default)
+ normalized = [pattern.strip().replace("\\", "/") for pattern in patterns if pattern and pattern.strip()]
+ return normalized or default
+
+
+def _normalize_events(events: WatchEvents) -> frozenset[WatchEvent]:
+ if events == "all":
+ return _VALID_EVENTS
+ normalized: set[WatchEvent] = set()
+ for event in events:
+ mapped = _map_event_type(str(event))
+ if mapped is None:
+ raise ValueError(f"Unsupported watch event: {event}")
+ normalized.add(mapped)
+ return frozenset(normalized) if normalized else _VALID_EVENTS
+
+
+def _map_event_type(event_type: str) -> WatchEvent | None:
+ return _EVENT_ALIASES.get(event_type.lower())
+
+
+def _normalize_debounce(debounce: float) -> float:
+ if debounce < 0:
+ raise ValueError("debounce must be >= 0")
+ return debounce
+
+
+def _covering_roots(roots: Iterable[str]) -> set[str]:
+ ordered = sorted(set(roots), key=lambda root: (len(root), root))
+ covered: set[str] = set()
+ for root in ordered:
+ if any(_is_same_or_nested(root, parent) for parent in covered):
+ continue
+ covered.add(root)
+ return covered
+
+
+def _is_same_or_nested(path: str, root: str) -> bool:
+ return path == root or path.startswith(root + os.sep)
+
+
+def _is_under_watch(path: str, watch: _Watch) -> bool:
+ return path == watch.root or path.startswith(watch.root_with_sep)
+
+
+def _compile_matcher(
+ root: str,
+ patterns: list[str],
+ ignore_patterns: list[str],
+) -> PatternMatcher:
+ include_matcher = _compile_single_matcher(root, patterns)
+ ignore_matcher = _compile_single_matcher(root, ignore_patterns)
+
+ def matches(path: str) -> bool:
+ return include_matcher(path) and not ignore_matcher(path)
+
+ return matches
+
+
+def _compile_single_matcher(root: str, patterns: list[str]) -> PatternMatcher:
+ if not patterns or patterns == _DEFAULT_PATTERNS:
+ return lambda path: True
+
+ if any(pattern in {"**", "**/*", "*"} for pattern in patterns):
+ return lambda path: True
+
+ relative_patterns = [pattern for pattern in patterns if "/" in pattern]
+ name_patterns = [
+ pattern for pattern in patterns if "/" not in pattern and pattern not in {"**", "**/*", "*"}
+ ]
+
+ def matches(path: str) -> bool:
+ relative = os.path.relpath(path, root).replace("\\", "/")
+ if relative == ".":
+ relative = ""
+ relative_path = PurePosixPath(relative) if relative else PurePosixPath("")
+ name_path = PurePosixPath(os.path.basename(path))
+
+ for pattern in relative_patterns:
+ if relative and relative_path.match(pattern):
+ return True
+ for pattern in name_patterns:
+ if name_path.match(pattern):
+ return True
+ if relative and relative_path.match(pattern):
+ return True
+ return False
+
+ return matches
+
+
+_registry = _WatchRegistry()
+_registry.start()
+
+
+def add_watchdog(
+ id: str,
+ roots: list[str],
+ patterns: list[str] | None = None,
+ ignore_patterns: list[str] | None = None,
+ events: WatchEvents = "all",
+ debounce: float = 0.01,
+ handler: WatchHandler | None = None,
+) -> None:
+ if handler is None:
+ raise ValueError("handler is required")
+ _registry.add(
+ id=id,
+ roots=roots,
+ patterns=patterns,
+ ignore_patterns=ignore_patterns,
+ events=events,
+ debounce=debounce,
+ handler=handler,
+ )
+
+
+def remove_watchdog(id: str) -> bool:
+ return _registry.remove(id)
+
+
+def clear_watchdogs() -> None:
+ _registry.clear()
+
+
+def start_watchdog_daemon() -> None:
+ _registry.start()
+
+
+def stop_watchdog_daemon() -> None:
+ _registry.stop()
+
+
+__all__ = [
+ "WatchEvent",
+ "WatchEvents",
+ "WatchItem",
+ "WatchHandler",
+ "add_watchdog",
+ "remove_watchdog",
+ "clear_watchdogs",
+ "start_watchdog_daemon",
+ "stop_watchdog_daemon",
+]
+
diff --git a/models.py b/models.py
index 7cb558d32..7b8905205 100644
--- a/models.py
+++ b/models.py
@@ -845,12 +845,6 @@ def _parse_chunk(chunk: Any) -> ChatChunk:
def _adjust_call_args(provider_name: str, model_name: str, kwargs: dict):
- # for openrouter add app reference
- if provider_name == "openrouter":
- kwargs["extra_headers"] = {
- "HTTP-Referer": "https://agent-zero.ai",
- "X-Title": "Agent Zero",
- }
# remap other to openai for litellm
if provider_name == "other":
diff --git a/plugins/_infection_check/.toggle-0 b/plugins/_infection_check/.toggle-0
new file mode 100644
index 000000000..e69de29bb
diff --git a/requirements.txt b/requirements.txt
index 2747c3d98..189b6f4ba 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -51,6 +51,7 @@ exchangelib>=5.4.3
pywinpty==3.0.2; sys_platform == "win32"
python-socketio>=5.14.2
uvicorn>=0.38.0
+watchdog==6.0.0
wsproto>=1.2.0
# Security floor pins for transitive dependencies
# These packages are pulled transitively — floor pins prevent resolver regressions