fix(audit): P0-P3 audit fixes — 838 ruff errors → 0, 30 F821 bugs fixed, 118 files changed
- P0: hooks.py 3-tuple fix, trigger_dispatcher Contract, contacts/plugin unregister_actions_by_owner - P0: 5 test files — check_permission mocks removed, hardcoded DB credential → env var - P1: attachment_service DmsFile via Contract helper, restore_registry/history_hooks dedup - P1: mail/plugin restore unregister, mcp_client datetime.now(UTC), saved_views/filters patterns - P1: ProtectedRoute fail-closed, 13 test assertion fixes (bcrypt, DB-URLs, SECRET_KEYs) - P2: deprecated notifications → post_system_message (3 files), forgejo Base, report_generator lazy import - P2: webhooks permissions, deps.py/roles.py plugin perms removed, import_export default - P2: address/tags/entity_links patterns removed, worker.py Contract-Umgehungen fixed - P2: 28 frontend TODOs (hardcoded constants, deprecated notification API) - P3: dead code, duplicates, deprecated imports, private attr, __import__ inline - P3: 8 frontend TODOs (LucideIcons, inline styles, XSS, i18n) - ruff: 838 → 0 (612 auto-fix + 246 manual + 27 F821 regression fix) - F821: 30 → 0 (AutomationDefinition, DmsFile, user_id, Path, Any, String) - Contract-Umgehungen: 2 neue gefunden (worker.py:169, worker.py:280) und gefixt
This commit is contained in:
@@ -12,12 +12,10 @@ import logging
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select, text
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.db import create_db_session
|
||||
from app.models.audit import AuditLog
|
||||
from app.models.contact import Contact
|
||||
from app.plugins.builtins.mail.contracts import Mail
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -101,7 +99,9 @@ async def search_related_handler(arguments: dict[str, Any], context: dict[str, A
|
||||
entity_id = uuid.UUID(arguments["entity_id"])
|
||||
limit = arguments.get("limit", 5)
|
||||
|
||||
from app.plugins.builtins.unified_search.contracts import get_contract as get_search_contract
|
||||
from app.plugins.builtins.unified_search.contracts import (
|
||||
get_contract as get_search_contract,
|
||||
)
|
||||
_search = get_search_contract()
|
||||
find_similar_all_types = _search.find_similar_all_types
|
||||
|
||||
@@ -165,8 +165,8 @@ async def get_open_tasks_handler(arguments: dict[str, Any], context: dict[str, A
|
||||
|
||||
from app.plugins.builtins.calendar.contracts import get_contract as get_calendar_contract
|
||||
_cal = get_calendar_contract()
|
||||
CalendarEntry = _cal.CalendarEntry
|
||||
CalendarEntryLink = _cal.CalendarEntryLink
|
||||
calendar_entry = _cal.calendar_entry
|
||||
calendar_entry_link = _cal.calendar_entry_link
|
||||
|
||||
db, tenant_id, _ = await _get_db_and_tenant(context)
|
||||
entity_type = arguments["entity_type"]
|
||||
@@ -174,14 +174,14 @@ async def get_open_tasks_handler(arguments: dict[str, Any], context: dict[str, A
|
||||
now = datetime.now(UTC)
|
||||
|
||||
result = await db.execute(
|
||||
select(CalendarEntry)
|
||||
.join(CalendarEntryLink, CalendarEntryLink.entry_id == CalendarEntry.id)
|
||||
.where(CalendarEntryLink.entity_type == entity_type)
|
||||
.where(CalendarEntryLink.entity_id == entity_id)
|
||||
.where(CalendarEntry.tenant_id == tenant_id)
|
||||
.where(CalendarEntry.start_at > now)
|
||||
.where(CalendarEntry.status == "open")
|
||||
.order_by(CalendarEntry.start_at.asc())
|
||||
select(calendar_entry)
|
||||
.join(calendar_entry_link, calendar_entry_link.entry_id == calendar_entry.id)
|
||||
.where(calendar_entry_link.entity_type == entity_type)
|
||||
.where(calendar_entry_link.entity_id == entity_id)
|
||||
.where(calendar_entry.tenant_id == tenant_id)
|
||||
.where(calendar_entry.start_at > now)
|
||||
.where(calendar_entry.status == "open")
|
||||
.order_by(calendar_entry.start_at.asc())
|
||||
.limit(20)
|
||||
)
|
||||
tasks = [_serialize(e) for e in result.scalars().all()]
|
||||
@@ -194,7 +194,9 @@ async def get_open_tasks_handler(arguments: dict[str, Any], context: dict[str, A
|
||||
async def hybrid_search_handler(arguments: dict[str, Any], context: dict[str, Any]) -> str:
|
||||
"""Perform hybrid search via unified_search search_engine."""
|
||||
try:
|
||||
from app.plugins.builtins.unified_search.contracts import get_contract as get_search_contract
|
||||
from app.plugins.builtins.unified_search.contracts import (
|
||||
get_contract as get_search_contract,
|
||||
)
|
||||
_search = get_search_contract()
|
||||
hybrid_search = _search.hybrid_search
|
||||
|
||||
|
||||
@@ -5,7 +5,13 @@ Exposes models, services, and job functions that other builtins plugins may need
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.plugins.builtins.contracts import get_contract_registry
|
||||
from app.plugins.builtins.ai_proactive.context_tools import (
|
||||
register_context_tools,
|
||||
)
|
||||
from app.plugins.builtins.ai_proactive.jobs import (
|
||||
deep_analysis,
|
||||
heartbeat,
|
||||
)
|
||||
from app.plugins.builtins.ai_proactive.models import (
|
||||
ContextLog,
|
||||
ProactiveSettings,
|
||||
@@ -20,13 +26,7 @@ from app.plugins.builtins.ai_proactive.services import (
|
||||
mark_dismissed,
|
||||
push_suggestion,
|
||||
)
|
||||
from app.plugins.builtins.ai_proactive.context_tools import (
|
||||
register_context_tools,
|
||||
)
|
||||
from app.plugins.builtins.ai_proactive.jobs import (
|
||||
deep_analysis,
|
||||
heartbeat,
|
||||
)
|
||||
from app.plugins.builtins.contracts import get_contract_registry
|
||||
|
||||
|
||||
class AiProactiveContract:
|
||||
|
||||
@@ -10,23 +10,23 @@ Deep analysis job runs after context-change for deeper analysis:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import uuid
|
||||
from datetime import UTC
|
||||
from typing import Any
|
||||
|
||||
from app.ai.llm_client import llm_complete
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.ai.llm_client import llm_complete
|
||||
from app.core.db import create_db_session
|
||||
from app.core.notifications import create_notification
|
||||
from app.models.audit import AuditLog
|
||||
from app.models.contact import Contact
|
||||
from app.plugins.builtins.ai_proactive.models import ProactiveSuggestion, ProactiveSettings
|
||||
from app.plugins.builtins.ai_proactive.models import ProactiveSuggestion
|
||||
from app.plugins.builtins.ai_proactive.services import (
|
||||
_serialize_row,
|
||||
generate_suggestion,
|
||||
get_user_settings,
|
||||
push_suggestion,
|
||||
)
|
||||
@@ -175,9 +175,10 @@ async def deep_analysis(
|
||||
|
||||
# Similar entities via unified_search
|
||||
try:
|
||||
from app.plugins.builtins.unified_search.contracts import get_contract as get_search_contract
|
||||
from app.plugins.builtins.unified_search.contracts import (
|
||||
get_contract as get_search_contract,
|
||||
)
|
||||
_search = get_search_contract()
|
||||
hybrid_search = _search.hybrid_search
|
||||
find_similar_all_types = _search.hybrid_search # alias
|
||||
|
||||
extended_context["similar"] = await find_similar_all_types(
|
||||
@@ -339,9 +340,10 @@ async def heartbeat(ctx: dict[str, Any], user_id: str, tenant_id: str) -> None:
|
||||
|
||||
async with create_db_session(tid) as db:
|
||||
# Check if heartbeat is enabled and get configuration
|
||||
from app.plugins.builtins.ai_proactive.models import ProactiveSettings
|
||||
from sqlalchemy import select as sa_select
|
||||
|
||||
from app.plugins.builtins.ai_proactive.models import ProactiveSettings
|
||||
|
||||
settings_result = await db.execute(
|
||||
sa_select(ProactiveSettings)
|
||||
.where(ProactiveSettings.tenant_id == tid)
|
||||
@@ -390,9 +392,9 @@ async def heartbeat(ctx: dict[str, Any], user_id: str, tenant_id: str) -> None:
|
||||
contact_count = contact_count_result.scalar() or 0
|
||||
|
||||
# Build status message
|
||||
from datetime import datetime, timezone
|
||||
from datetime import datetime
|
||||
|
||||
now_str = datetime.now(timezone.utc).strftime("%H:%M:%S")
|
||||
now_str = datetime.now(UTC).strftime("%H:%M:%S")
|
||||
status_content = (
|
||||
f"**System aktiv** — überwacht {contact_count} Kontakte\n"
|
||||
f"_Letztes Update: {now_str}_"
|
||||
@@ -416,6 +418,6 @@ async def heartbeat(ctx: dict[str, Any], user_id: str, tenant_id: str) -> None:
|
||||
|
||||
|
||||
# Register all job functions with the job registry
|
||||
from app.core.job_registry import register_job
|
||||
from app.core.job_registry import register_job # noqa: E402
|
||||
|
||||
register_job("deep_analysis", deep_analysis)
|
||||
|
||||
@@ -16,7 +16,8 @@ from sqlalchemy import (
|
||||
String,
|
||||
Text,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import JSONB, UUID as PGUUID
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.db import Base, TenantMixin
|
||||
|
||||
@@ -11,7 +11,7 @@ import logging
|
||||
from typing import Any
|
||||
|
||||
from app.plugins.base import BasePlugin
|
||||
from app.plugins.manifest import PluginManifest, PluginRouteDef, FrontendSettingsPage
|
||||
from app.plugins.manifest import FrontendSettingsPage, PluginManifest, PluginRouteDef
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -56,16 +56,23 @@ class AIProactivePlugin(BasePlugin):
|
||||
super().__init__()
|
||||
self._proactive_handler = None
|
||||
|
||||
def get_entity_models(self) -> dict[str, type]:
|
||||
from app.plugins.builtins.ai_proactive.models import ProactiveSuggestion
|
||||
return {"proactive_suggestion": ProactiveSuggestion}
|
||||
|
||||
def get_job_modules(self) -> list[str]:
|
||||
return ["app.plugins.builtins.ai_proactive.jobs"]
|
||||
|
||||
async def on_activate(self, db, service_container, event_bus) -> None:
|
||||
"""Register context tools, subscribe to events, and register as participant."""
|
||||
await super().on_activate(db, service_container, event_bus)
|
||||
try:
|
||||
from app.plugins.builtins.ai_proactive.context_tools import (
|
||||
register_context_tools,
|
||||
)
|
||||
from app.plugins.builtins.ai_assistant.contracts import (
|
||||
get_tool_registry,
|
||||
)
|
||||
from app.plugins.builtins.ai_proactive.context_tools import (
|
||||
register_context_tools,
|
||||
)
|
||||
|
||||
register_context_tools(get_tool_registry())
|
||||
logger.info("AI Proactive context tools registered")
|
||||
|
||||
@@ -15,8 +15,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.db import get_db, set_tenant_context
|
||||
from app.core.visibility import apply_visibility_filter
|
||||
from app.core.db import get_db
|
||||
from app.core.event_bus import get_event_bus
|
||||
from app.deps import get_current_user, require_permission
|
||||
from app.plugins.builtins.ai_proactive.models import (
|
||||
@@ -30,9 +29,9 @@ from app.plugins.builtins.ai_proactive.schemas import (
|
||||
ContextReport,
|
||||
SettingsResponse,
|
||||
SettingsUpdate,
|
||||
StatsResponse,
|
||||
SuggestionListResponse,
|
||||
SuggestionResponse,
|
||||
StatsResponse,
|
||||
)
|
||||
from app.plugins.builtins.ai_proactive.services import (
|
||||
execute_suggested_action,
|
||||
@@ -184,7 +183,7 @@ async def dismiss_suggestion(
|
||||
try:
|
||||
sid = uuid.UUID(suggestion_id)
|
||||
except (ValueError, TypeError):
|
||||
raise HTTPException(status_code=400, detail="Invalid suggestion ID")
|
||||
raise HTTPException(status_code=400, detail="Invalid suggestion ID") from None
|
||||
|
||||
success = await mark_dismissed(db, sid, user_id, tenant_id)
|
||||
if not success:
|
||||
@@ -210,7 +209,7 @@ async def act_on_suggestion(
|
||||
try:
|
||||
sid = uuid.UUID(suggestion_id)
|
||||
except (ValueError, TypeError):
|
||||
raise HTTPException(status_code=400, detail="Invalid suggestion ID")
|
||||
raise HTTPException(status_code=400, detail="Invalid suggestion ID") from None
|
||||
|
||||
result = await execute_suggested_action(
|
||||
db, sid, action.action_index, user_id, tenant_id, current_user
|
||||
@@ -245,7 +244,7 @@ async def stream_suggestions(
|
||||
try:
|
||||
suggestion = await asyncio.wait_for(queue.get(), timeout=30)
|
||||
yield f"data: {json.dumps(suggestion, default=str)}\n\n"
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
yield ": keepalive\n\n"
|
||||
|
||||
return StreamingResponse(
|
||||
|
||||
@@ -6,30 +6,28 @@ suggestions, pushes via SSE, and manages suggestion lifecycle.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import uuid
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
import litellm
|
||||
from app.ai.llm_client import llm_complete
|
||||
from sqlalchemy import func, select, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.cache import get_cache
|
||||
from app.core.db import create_db_session, get_session_factory
|
||||
from app.ai.llm_client import llm_complete
|
||||
from app.core.db import create_db_session
|
||||
from app.core.notifications import create_notification
|
||||
from app.models.audit import AuditLog
|
||||
from app.models.contact import Contact, ContactPerson
|
||||
from app.models.contact import Contact, ContactPerson
|
||||
from app.plugins.builtins.ai_proactive.models import (
|
||||
ContextLog,
|
||||
ProactiveSettings,
|
||||
ProactiveSuggestion,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
litellm.suppress_debug_info = True
|
||||
@@ -81,9 +79,10 @@ async def is_rate_limited(
|
||||
and ``window_seconds=rate_limit_seconds``.
|
||||
Returns ``True`` if rate-limited, ``False`` if allowed.
|
||||
"""
|
||||
from app.core.rate_limit import check_rate_limit
|
||||
from fastapi import HTTPException
|
||||
|
||||
from app.core.rate_limit import check_rate_limit
|
||||
|
||||
redis_key = f"rate:ai_proactive:{tenant_id}:{user_id}"
|
||||
try:
|
||||
await check_rate_limit(redis_key, max_attempts=1, window_seconds=rate_limit_seconds)
|
||||
@@ -208,18 +207,18 @@ async def gather_context(
|
||||
# Upcoming calendar events
|
||||
from app.plugins.builtins.calendar.contracts import get_contract as get_calendar_contract
|
||||
_cal = get_calendar_contract()
|
||||
CalendarEntry = _cal.CalendarEntry
|
||||
CalendarEntryLink = _cal.CalendarEntryLink
|
||||
calendar_entry = _cal.calendar_entry
|
||||
calendar_entry_link = _cal.calendar_entry_link
|
||||
|
||||
now = datetime.now(UTC)
|
||||
event_result = await db.execute(
|
||||
select(CalendarEntry)
|
||||
.join(CalendarEntryLink, CalendarEntryLink.entry_id == CalendarEntry.id)
|
||||
.where(CalendarEntryLink.entity_type == "contact")
|
||||
.where(CalendarEntryLink.entity_id == entity_id)
|
||||
.where(CalendarEntry.tenant_id == tenant_id)
|
||||
.where(CalendarEntry.start_at > now)
|
||||
.order_by(CalendarEntry.start_at.asc())
|
||||
select(calendar_entry)
|
||||
.join(calendar_entry_link, calendar_entry_link.entry_id == calendar_entry.id)
|
||||
.where(calendar_entry_link.entity_type == "contact")
|
||||
.where(calendar_entry_link.entity_id == entity_id)
|
||||
.where(calendar_entry.tenant_id == tenant_id)
|
||||
.where(calendar_entry.start_at > now)
|
||||
.order_by(calendar_entry.start_at.asc())
|
||||
.limit(5)
|
||||
)
|
||||
context["events"] = [_serialize_row(e) for e in event_result.scalars().all()]
|
||||
@@ -323,18 +322,18 @@ async def gather_context(
|
||||
# Upcoming events
|
||||
from app.plugins.builtins.calendar.contracts import get_contract as get_calendar_contract
|
||||
_cal = get_calendar_contract()
|
||||
CalendarEntry = _cal.CalendarEntry
|
||||
CalendarEntryLink = _cal.CalendarEntryLink
|
||||
calendar_entry = _cal.calendar_entry
|
||||
calendar_entry_link = _cal.calendar_entry_link
|
||||
|
||||
now = datetime.now(UTC)
|
||||
event_result = await db.execute(
|
||||
select(CalendarEntry)
|
||||
.join(CalendarEntryLink, CalendarEntryLink.entry_id == CalendarEntry.id)
|
||||
.where(CalendarEntryLink.entity_type == "contact")
|
||||
.where(CalendarEntryLink.entity_id == entity_id)
|
||||
.where(CalendarEntry.tenant_id == tenant_id)
|
||||
.where(CalendarEntry.start_at > now)
|
||||
.order_by(CalendarEntry.start_at.asc())
|
||||
select(calendar_entry)
|
||||
.join(calendar_entry_link, calendar_entry_link.entry_id == calendar_entry.id)
|
||||
.where(calendar_entry_link.entity_type == "contact")
|
||||
.where(calendar_entry_link.entity_id == entity_id)
|
||||
.where(calendar_entry.tenant_id == tenant_id)
|
||||
.where(calendar_entry.start_at > now)
|
||||
.order_by(calendar_entry.start_at.asc())
|
||||
.limit(5)
|
||||
)
|
||||
context["events"] = [_serialize_row(e) for e in event_result.scalars().all()]
|
||||
@@ -365,7 +364,9 @@ async def gather_context(
|
||||
|
||||
# Semantically similar entities via unified_search
|
||||
try:
|
||||
from app.plugins.builtins.unified_search.contracts import get_contract as get_search_contract
|
||||
from app.plugins.builtins.unified_search.contracts import (
|
||||
get_contract as get_search_contract,
|
||||
)
|
||||
_search = get_search_contract()
|
||||
find_similar_all_types = _search.hybrid_search
|
||||
|
||||
|
||||
Reference in New Issue
Block a user