187 行
7.1 KiB
Python
187 行
7.1 KiB
Python
"""Lightweight workflow review checks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
from backend.dto.image_dto import GenerationImage
|
|
from backend.dto.workflow_dto import ProfileWorkflow, WorkflowNode
|
|
|
|
|
|
_KNOWN_NODE_STATUSES = {
|
|
"draft",
|
|
"pending",
|
|
"queued",
|
|
"running",
|
|
"completed",
|
|
"selected",
|
|
"failed",
|
|
"cancelled",
|
|
"skipped",
|
|
"discarded",
|
|
}
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ReviewPolicy:
|
|
version: str = "default-v1"
|
|
warn_without_images: bool = True
|
|
require_selected_image_for_statuses: tuple[str, ...] = ("completed", "selected")
|
|
fail_on_selected_image_not_found: bool = True
|
|
fail_on_archived_selected_image: bool = True
|
|
workflow_terminal_statuses: tuple[str, ...] = ("completed", "selected", "skipped")
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {
|
|
"version": self.version,
|
|
"warn_without_images": self.warn_without_images,
|
|
"require_selected_image_for_statuses": list(self.require_selected_image_for_statuses),
|
|
"fail_on_selected_image_not_found": self.fail_on_selected_image_not_found,
|
|
"fail_on_archived_selected_image": self.fail_on_archived_selected_image,
|
|
"workflow_terminal_statuses": list(self.workflow_terminal_statuses),
|
|
}
|
|
|
|
@classmethod
|
|
def from_mapping(cls, payload: dict[str, Any]) -> tuple["ReviewPolicy", list[dict[str, Any]]]:
|
|
errors: list[dict[str, Any]] = []
|
|
defaults = cls()
|
|
values: dict[str, Any] = {}
|
|
|
|
for field_name in (
|
|
"warn_without_images",
|
|
"fail_on_selected_image_not_found",
|
|
"fail_on_archived_selected_image",
|
|
):
|
|
value = payload.get(field_name, getattr(defaults, field_name))
|
|
if not isinstance(value, bool):
|
|
errors.append(
|
|
{
|
|
"field": field_name,
|
|
"code": "INVALID_BOOLEAN",
|
|
"message": f"{field_name} must be a boolean",
|
|
}
|
|
)
|
|
value = getattr(defaults, field_name)
|
|
values[field_name] = value
|
|
|
|
for field_name in ("require_selected_image_for_statuses", "workflow_terminal_statuses"):
|
|
value = payload.get(field_name, getattr(defaults, field_name))
|
|
normalized = _normalize_status_list(value)
|
|
if not normalized:
|
|
errors.append(
|
|
{
|
|
"field": field_name,
|
|
"code": "INVALID_STATUS_LIST",
|
|
"message": f"{field_name} must contain at least one known node status",
|
|
}
|
|
)
|
|
normalized = getattr(defaults, field_name)
|
|
unknown = [status for status in normalized if status not in _KNOWN_NODE_STATUSES]
|
|
if unknown:
|
|
errors.append(
|
|
{
|
|
"field": field_name,
|
|
"code": "UNKNOWN_STATUS",
|
|
"message": f"{field_name} contains unknown statuses",
|
|
"statuses": unknown,
|
|
}
|
|
)
|
|
normalized = tuple(status for status in normalized if status in _KNOWN_NODE_STATUSES)
|
|
if not normalized:
|
|
normalized = getattr(defaults, field_name)
|
|
values[field_name] = normalized
|
|
|
|
version = str(payload.get("version") or defaults.version).strip() or defaults.version
|
|
return cls(version=version, **values), errors
|
|
|
|
|
|
class ReviewFlowEngine:
|
|
def __init__(self, policy: ReviewPolicy | None = None):
|
|
self.policy = policy or ReviewPolicy()
|
|
|
|
def policy_snapshot(self) -> dict[str, Any]:
|
|
return self.policy.to_dict()
|
|
|
|
def validate_policy(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
policy, errors = ReviewPolicy.from_mapping(payload)
|
|
return {"valid": not errors, "policy": policy.to_dict(), "errors": errors}
|
|
|
|
def review_node(self, node: WorkflowNode, images: list[GenerationImage]) -> dict[str, Any]:
|
|
warnings: list[dict[str, Any]] = []
|
|
errors: list[dict[str, Any]] = []
|
|
if self.policy.warn_without_images and not images:
|
|
warnings.append({"code": "NO_IMAGES", "message": "node has no generated images"})
|
|
if node.status in self.policy.require_selected_image_for_statuses and not node.selected_image_id:
|
|
errors.append({"code": "MISSING_SELECTED_IMAGE", "message": "completed node has no selected image"})
|
|
if (
|
|
self.policy.fail_on_selected_image_not_found
|
|
and node.selected_image_id
|
|
and node.selected_image_id not in {image.image_id for image in images}
|
|
):
|
|
errors.append({"code": "SELECTED_IMAGE_NOT_FOUND", "message": "selected image is not in node images"})
|
|
if self.policy.fail_on_archived_selected_image and any(
|
|
image.image_id == node.selected_image_id and image.is_archived for image in images
|
|
):
|
|
errors.append({"code": "SELECTED_IMAGE_ARCHIVED", "message": "selected image is archived"})
|
|
return {
|
|
"target_type": "node",
|
|
"target_id": node.node_id,
|
|
"passed": not errors,
|
|
"warnings": warnings,
|
|
"errors": errors,
|
|
"summary": {
|
|
"image_count": len(images),
|
|
"selected_image_id": node.selected_image_id,
|
|
"node_status": node.status,
|
|
"policy_version": self.policy.version,
|
|
},
|
|
}
|
|
|
|
def review_workflow(
|
|
self,
|
|
workflow: ProfileWorkflow,
|
|
nodes: list[WorkflowNode],
|
|
node_results: list[dict[str, Any]],
|
|
) -> dict[str, Any]:
|
|
errors = [error for result in node_results for error in result["errors"]]
|
|
warnings = [warning for result in node_results for warning in result["warnings"]]
|
|
incomplete = [node.node_id for node in nodes if node.status not in self.policy.workflow_terminal_statuses]
|
|
if incomplete:
|
|
warnings.append(
|
|
{
|
|
"code": "INCOMPLETE_NODES",
|
|
"message": "workflow has nodes that are not finished",
|
|
"node_ids": incomplete,
|
|
}
|
|
)
|
|
return {
|
|
"target_type": "workflow",
|
|
"target_id": workflow.workflow_id,
|
|
"passed": not errors,
|
|
"warnings": warnings,
|
|
"errors": errors,
|
|
"summary": {
|
|
"workflow_type": workflow.workflow_type,
|
|
"node_count": len(nodes),
|
|
"incomplete_count": len(incomplete),
|
|
"policy_version": self.policy.version,
|
|
},
|
|
"nodes": node_results,
|
|
}
|
|
|
|
|
|
def _normalize_status_list(value: Any) -> tuple[str, ...]:
|
|
if isinstance(value, str):
|
|
candidates = [value]
|
|
elif isinstance(value, (list, tuple)):
|
|
candidates = value
|
|
else:
|
|
return ()
|
|
|
|
statuses: list[str] = []
|
|
for item in candidates:
|
|
status = str(item or "").strip()
|
|
if status and status not in statuses:
|
|
statuses.append(status)
|
|
return tuple(statuses)
|