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
+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),
]