semantics
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
# #region normalizer [C:5] [TYPE Module] [SEMANTICS pydantic, report, task, normalize, status]
|
||||
# @BRIEF Convert task manager task objects into canonical unified TaskReport entities with deterministic fallback behavior.
|
||||
# @LAYER: Domain
|
||||
# @RELATION DEPENDS_ON -> [backend.src.core.task_manager.models.Task:Function]
|
||||
# @RELATION DEPENDS_ON -> [backend.src.models.report:Function]
|
||||
# @RELATION DEPENDS_ON -> [backend.src.services.reports.type_profiles:Function]
|
||||
# @INVARIANT: Normalizer instance maintains consistent field order
|
||||
# @DATA_CONTRACT: ReportRow -> NormalizerInput; session_id -> valid UUID
|
||||
# @PRE: session is active and valid
|
||||
# @POST: Returns Normalizer output with normalized fields
|
||||
# @SIDE_EFFECT: Read-only database operations
|
||||
# @LAYER Domain
|
||||
# @RELATION DEPENDS_ON -> [EXT:frontend:TaskModel]
|
||||
# @RELATION DEPENDS_ON -> [EXT:frontend:ReportModel]
|
||||
# @RELATION DEPENDS_ON -> [EXT:frontend:TypeProfiles]
|
||||
# @INVARIANT Normalizer instance maintains consistent field order
|
||||
# @DATA_CONTRACT ReportRow -> NormalizerInput; session_id -> valid UUID
|
||||
# @PRE session is active and valid
|
||||
# @POST Returns Normalizer output with normalized fields
|
||||
# @SIDE_EFFECT Read-only database operations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
@@ -21,8 +21,8 @@ from .type_profiles import get_type_profile, resolve_task_type
|
||||
|
||||
# #region status_to_report_status [TYPE Function]
|
||||
# @BRIEF Normalize internal task status to canonical report status.
|
||||
# @PRE: status may be known or unknown string/enum value.
|
||||
# @POST: Always returns one of canonical ReportStatus values.
|
||||
# @PRE status may be known or unknown string/enum value.
|
||||
# @POST Always returns one of canonical ReportStatus values.
|
||||
def status_to_report_status(status: Any) -> ReportStatus:
|
||||
with belief_scope("status_to_report_status"):
|
||||
raw = str(status.value if isinstance(status, TaskStatus) else status).upper()
|
||||
@@ -38,8 +38,8 @@ def status_to_report_status(status: Any) -> ReportStatus:
|
||||
|
||||
# #region build_summary [TYPE Function]
|
||||
# @BRIEF Build deterministic user-facing summary from task payload and status.
|
||||
# @PRE: report_status is canonical; plugin_id may be unknown.
|
||||
# @POST: Returns non-empty summary text.
|
||||
# @PRE report_status is canonical; plugin_id may be unknown.
|
||||
# @POST Returns non-empty summary text.
|
||||
def build_summary(task: Task, report_status: ReportStatus) -> str:
|
||||
with belief_scope("build_summary"):
|
||||
result = task.result
|
||||
@@ -60,8 +60,8 @@ def build_summary(task: Task, report_status: ReportStatus) -> str:
|
||||
|
||||
# #region extract_error_context [TYPE Function]
|
||||
# @BRIEF Extract normalized error context and next actions for failed/partial reports.
|
||||
# @PRE: task is a valid Task object.
|
||||
# @POST: Returns ErrorContext for failed/partial when context exists; otherwise None.
|
||||
# @PRE task is a valid Task object.
|
||||
# @POST Returns ErrorContext for failed/partial when context exists; otherwise None.
|
||||
def extract_error_context(task: Task, report_status: ReportStatus) -> ErrorContext | None:
|
||||
with belief_scope("extract_error_context"):
|
||||
if report_status not in {ReportStatus.FAILED, ReportStatus.PARTIAL}:
|
||||
@@ -101,10 +101,10 @@ def extract_error_context(task: Task, report_status: ReportStatus) -> ErrorConte
|
||||
|
||||
# #region normalize_task_report [TYPE Function]
|
||||
# @BRIEF Convert one Task to canonical TaskReport envelope.
|
||||
# @PRE: task has valid id and plugin_id fields.
|
||||
# @POST: Returns TaskReport with required fields and deterministic fallback behavior.
|
||||
# @PRE task has valid id and plugin_id fields.
|
||||
# @POST Returns TaskReport with required fields and deterministic fallback behavior.
|
||||
#
|
||||
# @TEST_CONTRACT: NormalizeTaskReport ->
|
||||
# @TEST_CONTRACT NormalizeTaskReport ->
|
||||
# {
|
||||
# required_fields: {task: Task},
|
||||
# invariants: [
|
||||
@@ -113,10 +113,10 @@ def extract_error_context(task: Task, report_status: ReportStatus) -> ErrorConte
|
||||
# "Extracts ErrorContext for FAILED/PARTIAL tasks"
|
||||
# ]
|
||||
# }
|
||||
# @TEST_FIXTURE: valid_task -> {"task": "MockTask(id='1', plugin_id='superset-migration', status=TaskStatus.SUCCESS)"}
|
||||
# @TEST_EDGE: task_with_error -> {"task": "MockTask(status=TaskStatus.FAILED, logs=[LogEntry(level='ERROR', message='Failed')])"}
|
||||
# @TEST_EDGE: unknown_plugin_type -> {"task": "MockTask(plugin_id='unknown-plugin', status=TaskStatus.PENDING)"}
|
||||
# @TEST_INVARIANT: deterministic_normalization -> verifies: [valid_task, task_with_error, unknown_plugin_type]
|
||||
# @TEST_FIXTURE valid_task -> {"task": "MockTask(id='1', plugin_id='superset-migration', status=TaskStatus.SUCCESS)"}
|
||||
# @TEST_EDGE task_with_error -> {"task": "MockTask(status=TaskStatus.FAILED, logs=[LogEntry(level='ERROR', message='Failed')])"}
|
||||
# @TEST_EDGE unknown_plugin_type -> {"task": "MockTask(plugin_id='unknown-plugin', status=TaskStatus.PENDING)"}
|
||||
# @TEST_INVARIANT deterministic_normalization -> verifies: [valid_task, task_with_error, unknown_plugin_type]
|
||||
def normalize_task_report(task: Task) -> TaskReport:
|
||||
with belief_scope("normalize_task_report"):
|
||||
task_type = resolve_task_type(task.plugin_id)
|
||||
|
||||
Reference in New Issue
Block a user