perf: Fix all 7 code analysis issues

HIGH (Performance):
- Replace 8 sync file operations with aiofiles in async context (storage, mail,
  report_generator, dms_bridge, ai_assistant)
- Frontend bundle splitting: manualChunks for react-vendor, ui-components, tanstack,
  markdown, icons, utils, i18n (ui chunk 936K → ~19K)

MEDIUM (Architecture):
- Worker circular deps: Replace direct plugin imports with job_registry.py pattern
  (register_job/get_all_jobs, importlib-based lazy loading)
- App-wide ErrorBoundary: New ErrorBoundary.tsx component, wrapped in AppShell
  and all standalone routes

LOW (Code Quality):
- N+1 query fix: selectinload(Contact.contact_persons) in list_contacts()
- O(n²) dedup fix: SQL GROUP BY for email/phone duplicates, Dict-based name grouping
- Response format standardization: 7 routes converted from plain arrays to
  {items: [...], total: N} format
This commit is contained in:
Agent Zero
2026-07-25 09:19:32 +02:00
parent 224a71ba56
commit aaa7406929
26 changed files with 436 additions and 225 deletions
+60
View File
@@ -0,0 +1,60 @@
"""
Job Registry — decouples ARQ worker from direct plugin imports.
Plugins register their job functions via register_job() at import time.
The worker retrieves all registered jobs via get_all_jobs() instead of
importing from plugin modules directly.
"""
from __future__ import annotations
import logging
from typing import Any, Callable, Coroutine
logger = logging.getLogger(__name__)
# Type alias for an async job function
JobFunc = Callable[..., Coroutine[Any, Any, Any]]
# Internal registry: name -> job function
_registry: dict[str, JobFunc] = {}
def register_job(name: str, func: JobFunc) -> None:
"""Register a job function under the given name.
Args:
name: Unique job name (e.g. 'index_mails').
func: The async callable to register.
"""
if name in _registry:
logger.warning("Job '%s' is being re-registered — overwriting", name)
_registry[name] = func
logger.debug("Registered job: %s", name)
def get_job(name: str) -> JobFunc | None:
"""Retrieve a registered job function by name.
Args:
name: The job name to look up.
Returns:
The registered callable, or None if not found.
"""
return _registry.get(name)
def get_all_jobs() -> list[JobFunc]:
"""Return all registered job functions (order is insertion order).
Returns:
List of all registered async callables.
"""
return list(_registry.values())
def clear_registry() -> None:
"""Clear all registered jobs. Useful for testing."""
_registry.clear()
logger.debug("Job registry cleared")
+6 -4
View File
@@ -19,6 +19,8 @@ import os
from abc import ABC, abstractmethod
from typing import Any
import aiofiles
logger = logging.getLogger(__name__)
@@ -70,15 +72,15 @@ class LocalStorage(StorageBackend):
async def save(self, path: str, data: bytes) -> str:
full_path = self._full_path(path)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
with open(full_path, "wb") as f:
f.write(data)
async with aiofiles.open(full_path, "wb") as f:
await f.write(data)
logger.debug("LocalStorage: saved %s (%d bytes)", path, len(data))
return path
async def read(self, path: str) -> bytes:
full_path = self._full_path(path)
with open(full_path, "rb") as f:
return f.read()
async with aiofiles.open(full_path, "rb") as f:
return await f.read()
async def delete(self, path: str) -> bool:
full_path = self._full_path(path)
+32 -32
View File
@@ -9,6 +9,7 @@ from arq.connections import RedisSettings
from arq import cron
from app.config import get_settings
from app.core.job_registry import get_all_jobs, get_job, register_job
logger = logging.getLogger(__name__)
@@ -39,39 +40,39 @@ async def on_shutdown(ctx: dict[str, Any]) -> None:
logger.info("ARQ worker shutting down...")
# Import job functions directly so ARQ registers them by __name__
from app.plugins.builtins.unified_search.jobs import (
index_mails,
index_file,
index_contact,
index_event,
reindex,
embedding_batch,
)
from app.plugins.builtins.ai_proactive.jobs import deep_analysis
from app.plugins.builtins.automation.scheduler import scheduler_tick
from app.plugins.builtins.automation.workflow_timeout import check_workflow_timeouts
from app.plugins.builtins.automation.agent_runner import run_agent
from app.plugins.builtins.automation.execution_engine import run_automation
from app.plugins.builtins.tasks.jobs import tasks_due_reminder
# ---------------------------------------------------------------------------
# Lazy-load plugin jobs via importlib so they register themselves with the
# job_registry. This avoids circular imports and keeps the worker decoupled
# from plugin internals.
# ---------------------------------------------------------------------------
def _lazy_register_plugin_jobs() -> None:
"""Import each plugin job module so its register_job() call fires."""
plugin_job_modules = [
"app.plugins.builtins.unified_search.jobs",
"app.plugins.builtins.ai_proactive.jobs",
"app.plugins.builtins.automation.scheduler",
"app.plugins.builtins.automation.workflow_timeout",
"app.plugins.builtins.automation.agent_runner",
"app.plugins.builtins.automation.execution_engine",
"app.plugins.builtins.tasks.jobs",
]
for mod_name in plugin_job_modules:
try:
import importlib
importlib.import_module(mod_name)
logger.debug("Lazy-loaded plugin jobs from %s", mod_name)
except Exception:
logger.warning("Failed to lazy-load plugin jobs from %s", mod_name, exc_info=True)
# Trigger lazy registration at module level so jobs are available when
# WorkerSettings.functions is evaluated.
_lazy_register_plugin_jobs()
class WorkerSettings:
"""ARQ worker settings."""
functions = [
index_mails,
index_file,
index_contact,
index_event,
reindex,
embedding_batch,
deep_analysis,
scheduler_tick,
check_workflow_timeouts,
run_agent,
run_automation,
tasks_due_reminder,
]
functions = get_all_jobs()
redis_settings = _get_redis_settings()
on_startup = on_startup
on_shutdown = on_shutdown
@@ -79,7 +80,6 @@ class WorkerSettings:
job_timeout = 300
queue_name = "arq:queue"
cron_jobs = [
cron(scheduler_tick, second={0, 30}),
cron(check_workflow_timeouts, minute={0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55}),
cron(tasks_due_reminder, hour=8, minute=0),
cron(get_job("scheduler_tick"), minute={0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55}),
cron(get_job("tasks_due_reminder"), hour=8, minute=0),
]