semantics: complete DEF-to-region migration, fix regressions

- Convert legacy [DEF🆔Type] anchors to #region/#endregion across 329 files
- Reinstate _normalize_timestamp_value in sql_generator.py
- Fix MarkerLogger→logger migration in events.py (molecular CoT markers)
- Fix dataset_review orchestrator dependencies (_build_execution_snapshot)
- Fix config_manager stale-record deletion (moved to save path only)
- Add 77 missing [/DEF:] closers in 5 unbalanced test files
- Update assistant_chat.integration.test.js for #region format
- Apply molecular-cot-logging markers (REASON/REFLECT/EXPLORE) via logger.* methods
This commit is contained in:
2026-05-12 23:54:55 +03:00
parent fe8978f716
commit 306c5ae742
331 changed files with 9630 additions and 10312 deletions

View File

@@ -1,14 +1,14 @@
# #region AppModule [C:5] [TYPE Module] [SEMANTICS app, main, entrypoint, fastapi]
# @BRIEF The main entry point for the FastAPI application. It initializes the app, configures CORS, sets up dependencies, includes API routers, and defines the WebSocket endpoint for log streaming.
# @LAYER UI (API)
# @PRE Python environment and dependencies installed; configuration database available.
# @POST FastAPI app instance is created, middleware configured, and routes registered.
# @SIDE_EFFECT Starts background scheduler and binds network ports for HTTP/WS traffic.
# @DATA_CONTRACT [HTTP Request | WS Message] -> [HTTP Response | JSON Log Stream]
# @INVARIANT Only one FastAPI app instance exists per process.
# @INVARIANT All WebSocket connections must be properly cleaned up on disconnect.
# @LAYER: UI (API)
# @RELATION DEPENDS_ON -> [AppDependencies]
# @RELATION DEPENDS_ON -> [ApiRoutesModule]
# @INVARIANT: Only one FastAPI app instance exists per process.
# @INVARIANT: All WebSocket connections must be properly cleaned up on disconnect.
# @PRE: Python environment and dependencies installed; configuration database available.
# @POST: FastAPI app instance is created, middleware configured, and routes registered.
# @SIDE_EFFECT: Starts background scheduler and binds network ports for HTTP/WS traffic.
# @DATA_CONTRACT: [HTTP Request | WS Message] -> [HTTP Response | JSON Log Stream]
import os
from pathlib import Path
@@ -27,9 +27,6 @@ from .dependencies import get_task_manager, get_scheduler_service
from .core.encryption_key import ensure_encryption_key
from .core.utils.network import NetworkError
from .core.logger import logger, belief_scope
from .core.cot_logger import MarkerLogger
startup_log = MarkerLogger("Startup")
from .core.database import AuthSessionLocal
from .core.auth.security import get_password_hash
from .models.auth import User, Role
@@ -72,7 +69,7 @@ app = FastAPI(
# #region ensure_initial_admin_user [C:3] [TYPE Function]
# @BRIEF Ensures initial admin user exists when bootstrap env flags are enabled.
# @RELATION DEPENDS_ON -> [AuthRepository]
# @RELATION DEPENDS_ON -> AuthRepository
def ensure_initial_admin_user() -> None:
raw_flag = os.getenv("INITIAL_ADMIN_CREATE", "false").strip().lower()
if raw_flag not in {"1", "true", "yes", "on"}:
@@ -127,17 +124,16 @@ def ensure_initial_admin_user() -> None:
# #region startup_event [C:3] [TYPE Function]
# @BRIEF Handles application startup tasks, such as starting the scheduler.
# @PRE None.
# @POST Scheduler is started.
# @RELATION CALLS -> [AppDependencies]
# @PRE: None.
# @POST: Scheduler is started.
# Startup event
@app.on_event("startup")
async def startup_event():
startup_log.reason("Enter startup event")
ensure_encryption_key()
ensure_initial_admin_user()
scheduler = get_scheduler_service()
startup_log.reflect("Application startup completed")
with belief_scope("startup_event"):
ensure_encryption_key()
ensure_initial_admin_user()
scheduler = get_scheduler_service()
scheduler.start()
@@ -146,9 +142,9 @@ async def startup_event():
# #region shutdown_event [C:3] [TYPE Function]
# @BRIEF Handles application shutdown tasks, such as stopping the scheduler.
# @PRE None.
# @POST Scheduler is stopped.
# @RELATION CALLS -> [AppDependencies]
# @PRE: None.
# @POST: Scheduler is stopped.
# Shutdown event
@app.on_event("shutdown")
async def shutdown_event():
@@ -160,7 +156,7 @@ async def shutdown_event():
# #endregion shutdown_event
# #region app_middleware [TYPE Block]
# @BRIEF Configure application-wide middleware (Session, CORS, TraceContext).
# @BRIEF Configure application-wide middleware (Session, CORS).
# Configure Session Middleware (required by Authlib for OAuth2 flow)
from .core.auth.config import auth_config
@@ -174,27 +170,17 @@ app.add_middleware(
allow_methods=["*"],
allow_headers=["*"],
)
# Configure Trace Context Middleware (seeds trace_id per request)
from .core.middleware.trace import TraceContextMiddleware
app.add_middleware(TraceContextMiddleware)
# #endregion app_middleware
# #region network_error_handler [C:1] [TYPE Function]
# @BRIEF Global exception handler for NetworkError.
# @PRE request is a FastAPI Request object.
# @POST Returns 503 HTTP Exception.
# @PARAM: request (Request) - The incoming request object.
# @PARAM: exc (NetworkError) - The exception instance.
eh_log = MarkerLogger("ExceptionHandler")
# @PRE: request is a FastAPI Request object.
# @POST: Returns 503 HTTP Exception.
@app.exception_handler(NetworkError)
async def network_error_handler(request: Request, exc: NetworkError):
with belief_scope("network_error_handler"):
eh_log.explore("Unhandled NetworkError in request", error=str(exc), payload={"url": str(request.url)})
logger.error(f"Network error: {exc}")
return HTTPException(
status_code=503,
detail="Environment unavailable. Please check if the Superset instance is running.",
@@ -206,11 +192,9 @@ async def network_error_handler(request: Request, exc: NetworkError):
# #region log_requests [C:3] [TYPE Function]
# @BRIEF Middleware to log incoming HTTP requests and their response status.
# @PRE request is a FastAPI Request object.
# @POST Logs request and response details.
# @RELATION DEPENDS_ON -> [LoggerModule]
# @PARAM: request (Request) - The incoming request object.
# @PARAM: call_next (Callable) - The next middleware or route handler.
# @PRE: request is a FastAPI Request object.
# @POST: Logs request and response details.
@app.middleware("http")
async def log_requests(request: Request, call_next):
with belief_scope("log_requests"):
@@ -280,19 +264,19 @@ app.include_router(translate.router)
# #region api.include_routers [C:1] [TYPE Action] [SEMANTICS routes, registration, api]
# @BRIEF Registers all API routers with the FastAPI application.
# @LAYER API
# @LAYER: API
# #endregion api.include_routers
# #region websocket_endpoint [C:5] [TYPE Function]
# @BRIEF Provides a WebSocket endpoint for real-time log streaming of a task with server-side filtering.
# @PRE task_id must be a valid task ID.
# @POST WebSocket connection is managed and logs are streamed until disconnect.
# @SIDE_EFFECT Subscribes to TaskManager log queue and broadcasts messages over network.
# @DATA_CONTRACT [task_id: str, source: str, level: str] -> [JSON log entry objects]
# @INVARIANT Every accepted WebSocket subscription is unsubscribed exactly once even when streaming fails or the client disconnects.
# @RELATION CALLS -> [TaskManagerPackage]
# @RELATION DEPENDS_ON -> [LoggerModule]
# @PRE: task_id must be a valid task ID.
# @POST: WebSocket connection is managed and logs are streamed until disconnect.
# @SIDE_EFFECT: Subscribes to TaskManager log queue and broadcasts messages over network.
# @DATA_CONTRACT: [task_id: str, source: str, level: str] -> [JSON log entry objects]
# @INVARIANT: Every accepted WebSocket subscription is unsubscribed exactly once even when streaming fails or the client disconnects.
# @UX_STATE: Connecting -> Streaming -> (Disconnected)
#
# @TEST_CONTRACT: WebSocketLogStreamApi ->
@@ -454,10 +438,11 @@ if frontend_path.exists():
"/_app", StaticFiles(directory=str(frontend_path / "_app")), name="static"
)
# #region serve_spa [C:1] [TYPE Function]
# @BRIEF Serves the SPA frontend for any path not matched by API routes.
# @PRE frontend_path exists.
# @POST Returns the requested file or index.html.
# [DEF:serve_spa:Function]
# @COMPLEXITY: 1
# @PURPOSE: Serves the SPA frontend for any path not matched by API routes.
# @PRE: frontend_path exists.
# @POST: Returns the requested file or index.html.
@app.get("/{file_path:path}", include_in_schema=False)
async def serve_spa(file_path: str):
with belief_scope("serve_spa"):
@@ -479,12 +464,13 @@ if frontend_path.exists():
return FileResponse(str(full_path))
return FileResponse(str(frontend_path / "index.html"))
# #endregion serve_spa
# [/DEF:serve_spa:Function]
else:
# #region read_root [C:1] [TYPE Function]
# @BRIEF A simple root endpoint to confirm that the API is running when frontend is missing.
# @PRE None.
# @POST Returns a JSON message indicating API status.
# [DEF:read_root:Function]
# @COMPLEXITY: 1
# @PURPOSE: A simple root endpoint to confirm that the API is running when frontend is missing.
# @PRE: None.
# @POST: Returns a JSON message indicating API status.
@app.get("/")
async def read_root():
with belief_scope("read_root"):
@@ -492,5 +478,6 @@ else:
"message": "Superset Tools API is running (Frontend build not found)"
}
# #endregion read_root
# [/DEF:read_root:Function]
# #endregion StaticFiles
# #endregion AppModule