feat: add workflow rollout gate signals
This commit is contained in:
@@ -10,11 +10,16 @@ from PIL import Image, ImageChops, ImageStat
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.config import settings
|
||||
from app.core.render_paths import resolve_result_path, result_path_to_storage_key
|
||||
from app.domains.media.models import MediaAsset
|
||||
from app.domains.orders.models import OrderLine
|
||||
from app.domains.rendering.models import WorkflowRun
|
||||
from app.domains.rendering.schemas import WorkflowComparisonArtifactOut, WorkflowRunComparisonOut
|
||||
|
||||
ROLLOUT_PASS_MAX_MEAN_PIXEL_DELTA = 0.0
|
||||
ROLLOUT_WARN_MAX_MEAN_PIXEL_DELTA = 0.02
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class _ArtifactComparison:
|
||||
@@ -36,18 +41,78 @@ class _ArtifactComparison:
|
||||
sha256=self.sha256,
|
||||
mime_type=self.mime_type,
|
||||
image_width=self.image_width,
|
||||
image_height=self.image_height,
|
||||
image_height=self.image_height,
|
||||
)
|
||||
|
||||
|
||||
def evaluate_rollout_gate(
|
||||
*,
|
||||
authoritative_output: _ArtifactComparison,
|
||||
observer_output: _ArtifactComparison,
|
||||
exact_match: bool | None,
|
||||
dimensions_match: bool | None,
|
||||
mean_pixel_delta: float | None,
|
||||
) -> dict[str, object]:
|
||||
thresholds = {
|
||||
"pass_max_mean_pixel_delta": ROLLOUT_PASS_MAX_MEAN_PIXEL_DELTA,
|
||||
"warn_max_mean_pixel_delta": ROLLOUT_WARN_MAX_MEAN_PIXEL_DELTA,
|
||||
}
|
||||
reasons: list[str] = []
|
||||
|
||||
if not authoritative_output.exists:
|
||||
verdict = "fail"
|
||||
reasons.append("Authoritative legacy output is missing; keep legacy fallback active.")
|
||||
elif not observer_output.exists:
|
||||
verdict = "fail"
|
||||
reasons.append("Observer workflow output is missing; rollout cannot be approved.")
|
||||
elif exact_match:
|
||||
verdict = "pass"
|
||||
reasons.append("Observer output matches the authoritative legacy output byte-for-byte.")
|
||||
elif dimensions_match is False:
|
||||
verdict = "fail"
|
||||
reasons.append("Observer output dimensions differ from the authoritative legacy output.")
|
||||
elif mean_pixel_delta is None:
|
||||
verdict = "fail"
|
||||
reasons.append("Observer output could not be pixel-compared against the authoritative output.")
|
||||
elif mean_pixel_delta <= ROLLOUT_PASS_MAX_MEAN_PIXEL_DELTA:
|
||||
verdict = "pass"
|
||||
reasons.append("Observer output is visually identical within the pass threshold.")
|
||||
elif mean_pixel_delta <= ROLLOUT_WARN_MAX_MEAN_PIXEL_DELTA:
|
||||
verdict = "warn"
|
||||
reasons.append(
|
||||
"Observer output differs slightly from the authoritative output but remains within the warn threshold."
|
||||
)
|
||||
else:
|
||||
verdict = "fail"
|
||||
reasons.append(
|
||||
"Observer output exceeds the allowed parity threshold; keep legacy fallback active."
|
||||
)
|
||||
|
||||
if mean_pixel_delta is not None and not exact_match:
|
||||
reasons.append(
|
||||
f"Mean pixel delta {mean_pixel_delta:.6f}; "
|
||||
f"pass<={ROLLOUT_PASS_MAX_MEAN_PIXEL_DELTA:.6f}, "
|
||||
f"warn<={ROLLOUT_WARN_MAX_MEAN_PIXEL_DELTA:.6f}."
|
||||
)
|
||||
|
||||
rollout_ready = verdict == "pass"
|
||||
rollout_status = "ready_for_rollout" if rollout_ready else "hold_legacy_authoritative"
|
||||
|
||||
return {
|
||||
"verdict": verdict,
|
||||
"ready": rollout_ready,
|
||||
"status": rollout_status,
|
||||
"reasons": reasons,
|
||||
"thresholds": thresholds,
|
||||
"workflow_rollout_ready": rollout_ready,
|
||||
"workflow_rollout_status": rollout_status,
|
||||
"output_type_rollout_ready": rollout_ready,
|
||||
"output_type_rollout_status": rollout_status,
|
||||
}
|
||||
|
||||
|
||||
def _normalize_storage_key(path: str | None) -> str | None:
|
||||
if not path:
|
||||
return None
|
||||
normalized = path.replace("\\", "/")
|
||||
marker = "/uploads/"
|
||||
if marker in normalized:
|
||||
return normalized.split(marker, 1)[1]
|
||||
return normalized.lstrip("/")
|
||||
return result_path_to_storage_key(path)
|
||||
|
||||
|
||||
def _build_artifact(path: str | None) -> _ArtifactComparison:
|
||||
@@ -63,7 +128,8 @@ def _build_artifact(path: str | None) -> _ArtifactComparison:
|
||||
image_height=None,
|
||||
)
|
||||
|
||||
file_path = Path(path)
|
||||
resolved_path = resolve_result_path(path)
|
||||
file_path = resolved_path or Path(path)
|
||||
exists = file_path.exists()
|
||||
mime_type, _ = mimetypes.guess_type(str(file_path))
|
||||
|
||||
@@ -136,10 +202,8 @@ async def _load_shadow_asset_by_workflow_run(
|
||||
if asset is None:
|
||||
return None
|
||||
|
||||
storage_key = asset.storage_key.lstrip("/")
|
||||
if storage_key.startswith("app/uploads/"):
|
||||
return f"/{storage_key}"
|
||||
return f"/app/uploads/{storage_key}"
|
||||
resolved = resolve_result_path(asset.storage_key)
|
||||
return str(resolved) if resolved is not None else None
|
||||
|
||||
|
||||
def _find_shadow_file(order_line: OrderLine, workflow_run: WorkflowRun) -> str | None:
|
||||
@@ -147,9 +211,13 @@ def _find_shadow_file(order_line: OrderLine, workflow_run: WorkflowRun) -> str |
|
||||
candidate_roots: list[Path] = []
|
||||
|
||||
if order_line.result_path:
|
||||
candidate_roots.append(Path(order_line.result_path).parent)
|
||||
resolved_result = resolve_result_path(order_line.result_path)
|
||||
if resolved_result is not None:
|
||||
candidate_roots.append(resolved_result.parent)
|
||||
|
||||
candidate_roots.append(Path("/app/uploads/renders") / str(order_line.id))
|
||||
upload_root = Path(settings.upload_dir)
|
||||
candidate_roots.append(upload_root / "renders" / str(order_line.id))
|
||||
candidate_roots.append(upload_root / "step_files" / "renders")
|
||||
|
||||
seen_roots: set[Path] = set()
|
||||
candidates: list[Path] = []
|
||||
@@ -215,6 +283,9 @@ async def build_workflow_run_comparison(
|
||||
if exact_match:
|
||||
status = "matched"
|
||||
summary = "Observer output matches the authoritative legacy output byte-for-byte."
|
||||
elif mean_pixel_delta == 0.0 and dimensions_match:
|
||||
status = "matched"
|
||||
summary = "Observer output matches the authoritative legacy output visually, but file metadata differs."
|
||||
else:
|
||||
status = "different"
|
||||
if dimensions_match is False:
|
||||
|
||||
Reference in New Issue
Block a user