Fix LLM validation and dashboard health hot paths
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
# [SECTION: IMPORTS]
|
||||
from typing import List, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm import Session, selectinload
|
||||
|
||||
from ...models.auth import Permission, Role, User
|
||||
from ...models.profile import UserDashboardPreference
|
||||
@@ -53,7 +53,12 @@ class AuthRepository:
|
||||
raise ValueError("username must be a non-empty string")
|
||||
|
||||
logger.reason(f"Querying user by username: {username}")
|
||||
user = self.db.query(User).filter(User.username == username).first()
|
||||
user = (
|
||||
self.db.query(User)
|
||||
.options(selectinload(User.roles).selectinload(Role.permissions))
|
||||
.filter(User.username == username)
|
||||
.first()
|
||||
)
|
||||
|
||||
if user:
|
||||
logger.reflect(f"User found: {username}")
|
||||
@@ -199,4 +204,4 @@ class AuthRepository:
|
||||
|
||||
|
||||
# [/DEF:AuthRepository:Class]
|
||||
# [/DEF:backend.src.core.auth.repository:Module]
|
||||
# [/DEF:backend.src.core.auth.repository:Module]
|
||||
|
||||
29
backend/src/core/task_manager/__tests__/test_context.py
Normal file
29
backend/src/core/task_manager/__tests__/test_context.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# [DEF:backend.src.core.task_manager.__tests__.test_context:Module]
|
||||
# @TIER: STANDARD
|
||||
# @SEMANTICS: tests, task-context, background-tasks, sub-context
|
||||
# @PURPOSE: Verify TaskContext preserves optional background task scheduler across sub-context creation.
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from src.core.task_manager.context import TaskContext
|
||||
|
||||
|
||||
# [DEF:test_task_context_preserves_background_tasks_across_sub_context:Function]
|
||||
# @PURPOSE: Plugins must be able to access background_tasks from both root and sub-context loggers.
|
||||
# @PRE: TaskContext is initialized with a BackgroundTasks-like object.
|
||||
# @POST: background_tasks remains available on root and derived sub-contexts.
|
||||
def test_task_context_preserves_background_tasks_across_sub_context():
|
||||
background_tasks = MagicMock()
|
||||
context = TaskContext(
|
||||
task_id="task-1",
|
||||
add_log_fn=lambda **_kwargs: None,
|
||||
params={"x": 1},
|
||||
background_tasks=background_tasks,
|
||||
)
|
||||
|
||||
sub_context = context.create_sub_context("llm")
|
||||
|
||||
assert context.background_tasks is background_tasks
|
||||
assert sub_context.background_tasks is background_tasks
|
||||
# [/DEF:test_task_context_preserves_background_tasks_across_sub_context:Function]
|
||||
# [/DEF:backend.src.core.task_manager.__tests__.test_context:Module]
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
# [SECTION: IMPORTS]
|
||||
# [SECTION: IMPORTS]
|
||||
from typing import Dict, Any, Callable
|
||||
from typing import Dict, Any, Callable, Optional
|
||||
from .task_logger import TaskLogger
|
||||
from ..logger import belief_scope
|
||||
# [/SECTION]
|
||||
@@ -58,11 +58,13 @@ class TaskContext:
|
||||
task_id: str,
|
||||
add_log_fn: Callable,
|
||||
params: Dict[str, Any],
|
||||
default_source: str = "plugin"
|
||||
default_source: str = "plugin",
|
||||
background_tasks: Optional[Any] = None,
|
||||
):
|
||||
with belief_scope("__init__"):
|
||||
self._task_id = task_id
|
||||
self._params = params
|
||||
self._background_tasks = background_tasks
|
||||
self._logger = TaskLogger(
|
||||
task_id=task_id,
|
||||
add_log_fn=add_log_fn,
|
||||
@@ -102,6 +104,16 @@ class TaskContext:
|
||||
with belief_scope("params"):
|
||||
return self._params
|
||||
# [/DEF:params:Function]
|
||||
|
||||
# [DEF:background_tasks:Function]
|
||||
# @PURPOSE: Expose optional background task scheduler for plugins that dispatch deferred side effects.
|
||||
# @PRE: TaskContext must be initialized.
|
||||
# @POST: Returns BackgroundTasks-like object or None.
|
||||
@property
|
||||
def background_tasks(self) -> Optional[Any]:
|
||||
with belief_scope("background_tasks"):
|
||||
return self._background_tasks
|
||||
# [/DEF:background_tasks:Function]
|
||||
|
||||
# [DEF:get_param:Function]
|
||||
# @PURPOSE: Get a specific parameter value with optional default.
|
||||
@@ -128,7 +140,8 @@ class TaskContext:
|
||||
task_id=self._task_id,
|
||||
add_log_fn=self._logger._add_log,
|
||||
params=self._params,
|
||||
default_source=source
|
||||
default_source=source,
|
||||
background_tasks=self._background_tasks,
|
||||
)
|
||||
# [/DEF:create_sub_context:Function]
|
||||
|
||||
|
||||
@@ -208,7 +208,8 @@ class TaskManager:
|
||||
task_id=task_id,
|
||||
add_log_fn=self._add_log,
|
||||
params=params,
|
||||
default_source="plugin"
|
||||
default_source="plugin",
|
||||
background_tasks=None,
|
||||
)
|
||||
|
||||
if asyncio.iscoroutinefunction(plugin.execute):
|
||||
|
||||
Reference in New Issue
Block a user