Skip to content

Agent Zero AI framework

refactor: improve caching system and optimize extension/plugin path resolution

frdeld6f67b4df311
diff --git a/agent.py b/agent.py
index 4d0052848..0c670ed72 100644
--- a/agent.py
+++ b/agent.py
@@ -176,7 +176,7 @@ class AgentContext:
# recursive is not used now, prepared for context hierarchy
self.output_data[key] = value
- @extension.extensible
+ # @extension.extensible
def output(self):
return {
"id": self.id,
diff --git a/helpers/api.py b/helpers/api.py
index d610dfa33..1ac40655f 100644
--- a/helpers/api.py
+++ b/helpers/api.py
@@ -16,7 +16,7 @@ from helpers import files, cache
ThreadLockType = Union[threading.Lock, threading.RLock]
-CACHE_AREA = "api_handlers(api)(plugins)(extensions)"
+CACHE_AREA = "api_handlers(api)"
cache.toggle_area(CACHE_AREA, False) # cache off for now
Input = dict
diff --git a/helpers/cache.py b/helpers/cache.py
index 8d3156cc5..f7a7a0257 100644
--- a/helpers/cache.py
+++ b/helpers/cache.py
@@ -18,14 +18,14 @@ def toggle_area(area: str, enabled: bool) -> None:
_enabled_areas[area] = enabled
-def has(area: str, key: str) -> bool:
+def has(area: str, key: Any) -> bool:
if not _is_enabled(area):
return False
with _lock:
return key in _cache.get(area, {})
-def add(area: str, key: str, data: Any) -> None:
+def add(area: str, key: Any, data: Any) -> None:
if not _is_enabled(area):
return
with _lock:
@@ -34,14 +34,14 @@ def add(area: str, key: str, data: Any) -> None:
_cache[area][key] = data
-def get(area: str, key: str, default: Any = None) -> Any:
+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)
-def remove(area: str, key: str) -> None:
+def remove(area: str, key: Any) -> None:
if not _is_enabled(area):
return
with _lock:
@@ -69,3 +69,11 @@ def _is_enabled(area: str) -> bool:
if not _enabled_global:
return False
return _enabled_areas.get(area, True)
+
+
+def determine_cache_key(agent, *additional):
+ if agent:
+ profile = agent.config.profile or "none"
+ project = agent.context.get_data("project") or "none"
+ return (profile, project, *additional)
+ return ("none", "none", *additional)
\ No newline at end of file
diff --git a/helpers/extension.py b/helpers/extension.py
index f962ea94a..f0230e545 100644
--- a/helpers/extension.py
+++ b/helpers/extension.py
@@ -5,6 +5,7 @@ from helpers import cache, subagents
from typing import TYPE_CHECKING
from functools import wraps
import inspect
+import os
if TYPE_CHECKING:
from agent import Agent
@@ -13,8 +14,10 @@ if TYPE_CHECKING:
DEFAULT_EXTENSIONS_FOLDER = "python/extensions"
USER_EXTENSIONS_FOLDER = "usr/extensions"
-_CACHE_AREA = "extension_folder_classes(extensions)(plugins)"
-cache.toggle_area(_CACHE_AREA, False) # cache off for now
+_EXTENSIONS_CACHE_AREA = "extension_folder_classes(extensions)"
+_CLASSES_CACHE_AREA = "extension_classes(extensions)"
+cache.toggle_area(_EXTENSIONS_CACHE_AREA, False)
+# cache.toggle_area(_CLASSES_CACHE_AREA, False)
class _Unset:
@@ -22,6 +25,24 @@ class _Unset:
_UNSET = _Unset()
+_EXTENSIONS_LOG_COUNTS: dict[str, int] = {}
+
+# debug - extensions call counter
+def _log_extension_call(name: str):
+ try:
+ every = int(os.getenv("EXTENSIONS_LOG", "0"))
+ except ValueError:
+ return
+
+ if every <= 0:
+ return
+
+ _EXTENSIONS_LOG_COUNTS[name] = _EXTENSIONS_LOG_COUNTS.get(name, 0) + 1
+ _EXTENSIONS_LOG_COUNTS["_total"] = _EXTENSIONS_LOG_COUNTS.get("_total", 0) + 1
+
+ if _EXTENSIONS_LOG_COUNTS["_total"] % every == 0:
+ for key, count in _EXTENSIONS_LOG_COUNTS.items():
+ print(f"{str(count):<6} {key}")
# decorator to enable implicit extension points in existing functions
@@ -80,6 +101,7 @@ def extensible(func):
start_point = f"{module_name}_{qual_name}_start"
end_point = f"{module_name}_{qual_name}_end"
+
agent = _get_agent(args, kwargs)
data = {
@@ -177,6 +199,8 @@ class Extension:
async def call_extensions_async(
extension_point: str, agent: "Agent|None" = None, **kwargs
):
+ _log_extension_call(extension_point)
+
# fetch classes for this extension point and agent
classes = _get_extension_classes(extension_point, agent=agent, **kwargs)
@@ -188,6 +212,8 @@ async def call_extensions_async(
def call_extensions_sync(extension_point: str, agent: "Agent|None" = None, **kwargs):
+ _log_extension_call(extension_point)
+
# fetch classes for this extension point and agent
classes = _get_extension_classes(extension_point, agent=agent, **kwargs)
@@ -230,6 +256,11 @@ def get_webui_extensions(
def _get_extension_classes(
extension_point: str, agent: "Agent|None" = None, **kwargs
) -> list[Type[Extension]]:
+ cache_key = cache.determine_cache_key(agent, extension_point)
+ cached = cache.get(_CLASSES_CACHE_AREA, cache_key)
+ if cached is not None:
+ return cached
+
# search for extension folders in all agent's paths
paths = subagents.get_paths(agent, "extensions/python", extension_point)
@@ -244,6 +275,7 @@ def _get_extension_classes(
classes = sorted(
unique.values(), key=lambda cls: _get_file_from_module(cls.__module__)
)
+ cache.add(_CLASSES_CACHE_AREA, cache_key, classes)
return classes
@@ -253,7 +285,7 @@ def _get_file_from_module(module_name: str) -> str:
def _get_extensions(folder: str):
folder = files.get_abs_path(folder)
- cached = cache.get(_CACHE_AREA, folder)
+ cached = cache.get(_EXTENSIONS_CACHE_AREA, folder)
if cached is not None:
return cached
@@ -261,5 +293,5 @@ def _get_extensions(folder: str):
return []
classes = extract_tools.load_classes_from_folder(folder, "*", Extension)
- cache.add(_CACHE_AREA, folder, classes)
+ cache.add(_EXTENSIONS_CACHE_AREA, folder, classes)
return classes
\ No newline at end of file
diff --git a/helpers/files.py b/helpers/files.py
index 9178aa4d8..cd3246452 100644
--- a/helpers/files.py
+++ b/helpers/files.py
@@ -1,7 +1,6 @@
from abc import ABC, abstractmethod
from fnmatch import fnmatch
import json
-from ntpath import isabs
import os
import re
import base64
@@ -19,7 +18,7 @@ PLUGINS_DIR = "plugins"
PROJECTS_DIR = "projects"
USER_DIR = "usr"
TEMP_DIR = "tmp"
-
+_base_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, "../")))
class VariablesPlugin(ABC):
@abstractmethod
@@ -529,9 +528,15 @@ def make_dirs(relative_path: str):
os.makedirs(os.path.dirname(abs_path), exist_ok=True)
+def _resolve_path(*relative_paths):
+ if len(relative_paths) == 1 and os.path.isabs(relative_paths[0]):
+ return relative_paths[0]
+ return os.path.join(_base_dir, *relative_paths)
+
+
def get_abs_path(*relative_paths):
"Convert relative paths to absolute paths based on the base directory."
- return os.path.join(get_base_dir(), *relative_paths)
+ return _resolve_path(*relative_paths)
def get_abs_path_dockerized(*relative_paths):
@@ -574,24 +579,22 @@ def normalize_a0_path(path: str):
def exists(*relative_paths):
- path = get_abs_path(*relative_paths)
+ path = _resolve_path(*relative_paths)
return os.path.exists(path)
def is_file(*relative_paths):
- path = get_abs_path(*relative_paths)
+ path = _resolve_path(*relative_paths)
return os.path.isfile(path)
def is_dir(*relative_paths):
- path = get_abs_path(*relative_paths)
+ path = _resolve_path(*relative_paths)
return os.path.isdir(path)
def get_base_dir():
- # Get the base directory from the current file path
- base_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, "../")))
- return base_dir
+ return _base_dir
def basename(path: str, suffix: str | None = None):
diff --git a/helpers/plugins.py b/helpers/plugins.py
index 5ec4f5685..c6ba5bb62 100644
--- a/helpers/plugins.py
+++ b/helpers/plugins.py
@@ -57,6 +57,10 @@ TOGGLE_FILE_PATTERN = ".toggle-[01]"
HOOKS_SCRIPT = "hooks.py"
HOOKS_CACHE_AREA = "plugin_hooks(plugins)"
+PLUGINS_LIST_CACHE_AREA = "plugins_list(plugins)"
+ENABLED_PLUGINS_LIST_CACHE_AREA = "enabled_plugins(plugins)"
+ENABLED_PLUGINS_PATHS_CACHE_AREA = "enabled_plugins_paths(plugins)"
+
_last_frontend_reload_notification_at = 0.0
@@ -114,6 +118,8 @@ def after_plugin_change(plugin_names: list[str] | None = None):
def clear_plugin_cache():
cache.clear("*(plugins)*")
+ cache.clear("*(extensions)*")
+ cache.clear("*(api)*")
def get_plugin_roots(plugin_name: str = "") -> List[str]:
@@ -125,6 +131,9 @@ def get_plugin_roots(plugin_name: str = "") -> List[str]:
def get_plugins_list():
+ if cached := cache.get(PLUGINS_LIST_CACHE_AREA, ""):
+ return cached
+
result: list[str] = []
seen_names: set[str] = set()
for root in get_plugin_roots():
@@ -137,6 +146,8 @@ def get_plugins_list():
seen_names.add(dir.name)
result.append(dir.name)
result.sort(key=lambda p: Path(p).name)
+
+ cache.add(PLUGINS_LIST_CACHE_AREA, "", result)
return result
@@ -291,6 +302,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)):
+ return cached
+
enabled = get_enabled_plugins(agent)
paths: list[str] = []
@@ -307,10 +321,16 @@ 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)
+
return paths
def get_enabled_plugins(agent: Agent | None):
+ if cached := cache.get(ENABLED_PLUGINS_LIST_CACHE_AREA, cache.determine_cache_key(agent)):
+ return cached
+
plugins = get_plugins_list()
active = []
@@ -344,6 +364,8 @@ def get_enabled_plugins(agent: Agent | None):
if enabled:
active.append(plugin)
+ cache.add(ENABLED_PLUGINS_LIST_CACHE_AREA, cache.determine_cache_key(agent), active)
+
return active
diff --git a/helpers/subagents.py b/helpers/subagents.py
index 9ef42476e..f3557a49e 100644
--- a/helpers/subagents.py
+++ b/helpers/subagents.py
@@ -1,15 +1,18 @@
from helpers import files
+from helpers import cache
from helpers import yaml as yaml_helper
-from typing import TypedDict, TYPE_CHECKING
+from typing import TypedDict, TYPE_CHECKING, Literal
from pydantic import BaseModel, model_validator
import json
-from typing import Literal
import os
GLOBAL_DIR = "."
USER_DIR = "usr"
DEFAULT_AGENTS_DIR = "agents"
USER_AGENTS_DIR = "usr/agents"
+PATHS_CACHE_AREA = "subagent_paths(plugins)"
+
+cache.toggle_area(PATHS_CACHE_AREA, False)
type Origin = Literal["default", "user", "project", "plugin"]
@@ -345,6 +348,20 @@ def get_paths(
) -> list[str]:
"""Returns list of file paths for the given agent and subpaths, searched in order of priority:
project/agents/, project/, usr/agents/, plugin agents/, agents/, usr/, plugins/, default."""
+ cache_key = cache.determine_cache_key(
+ agent,
+ *subpaths,
+ must_exist_completely,
+ include_project,
+ include_user,
+ include_default,
+ include_plugins,
+ default_root,
+ )
+ cached = cache.get(PATHS_CACHE_AREA, cache_key)
+ if cached is not None:
+ return cached
+
paths: list[str] = []
check_subpaths = subpaths if must_exist_completely else []
profile_name = agent.config.profile if agent and agent.config.profile else ""
@@ -411,6 +428,7 @@ def get_paths(
if (not must_exist_completely) or files.exists(path):
paths.append(path)
+ cache.add(PATHS_CACHE_AREA, cache_key, paths)
return paths
diff --git a/tests/test_extensions_stress.py b/tests/test_extensions_stress.py
new file mode 100644
index 000000000..9fe7a5f75
--- /dev/null
+++ b/tests/test_extensions_stress.py
@@ -0,0 +1,50 @@
+import cProfile
+import io
+import pstats
+import sys
+from pathlib import Path
+
+import pytest
+
+PROJECT_ROOT = Path(__file__).resolve().parents[1]
+if str(PROJECT_ROOT) not in sys.path:
+ sys.path.insert(0, str(PROJECT_ROOT))
+
+from agent import Agent, AgentContext
+from helpers.extension import extensible
+from initialize import initialize_agent
+
+
+class PerfAgent(Agent):
+ @extensible
+ def perf_hook(self, value: int):
+ return value + 1
+
+
[email protected]("iterations", [10000])
+def test_extensible_method_performance_trace(iterations: int):
+ agent = PerfAgent(number=0, config=initialize_agent())
+ context = agent.context
+
+ try:
+ profiler = cProfile.Profile()
+ profiler.enable()
+
+ result = 0
+ for i in range(iterations):
+ result = agent.perf_hook(i)
+
+ profiler.disable()
+
+ output = io.StringIO()
+ stats = pstats.Stats(profiler, stream=output)
+ stats.sort_stats("cumulative")
+ stats.print_stats(20)
+
+ print(f"\n[extensible perf] iterations={iterations} result={result}")
+ print(output.getvalue())
+
+ assert result == iterations
+ finally:
+ if context:
+ AgentContext.remove(context.id)