995 行
46 KiB
Python
995 行
46 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import base64
|
||
|
|
import json
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from dataclasses import dataclass
|
||
|
|
from pathlib import Path
|
||
|
|
from types import SimpleNamespace
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from backend.dto.common import ApiError
|
||
|
|
from backend.dto.job_dto import GenerationTask
|
||
|
|
from backend.dto.workflow_dto import WorkflowNode
|
||
|
|
from backend.services.advanced_material_service import AdvancedMaterialService
|
||
|
|
from backend.services.advanced_style_set_service import (
|
||
|
|
STYLE_REVIEW_DIMENSIONS,
|
||
|
|
_aggregate_style_review_feedback,
|
||
|
|
_detailed_style_negative_prompt,
|
||
|
|
_detailed_style_prompt,
|
||
|
|
_normalize_style_dimension_scores,
|
||
|
|
_scale_specific_style_prompt,
|
||
|
|
_weighted_style_fidelity_score,
|
||
|
|
)
|
||
|
|
from backend.services.material_normalization_service import MaterialNormalizationService
|
||
|
|
from backend.services.prompt_governance_service import GenerationRouteService, PromptModerationService
|
||
|
|
from backend.services.queue_service import QueueService
|
||
|
|
from core.audit_service import AuditService
|
||
|
|
from core.config_service import ImageStudioConfig, StorageConfig
|
||
|
|
from core.identity import RequestContext
|
||
|
|
from core.model_route_chain import encode_model_route_chain
|
||
|
|
from core.storage import VfsStorage
|
||
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||
|
|
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||
|
|
from database.repositories.job_repository import JobRepository
|
||
|
|
from database.repositories.material_repository import MaterialRepository
|
||
|
|
from engines.image_provider import (
|
||
|
|
GeneratedImagePayload,
|
||
|
|
ImageGenerationRequest,
|
||
|
|
ImageGenerationResult,
|
||
|
|
ImageProviderError,
|
||
|
|
)
|
||
|
|
from engines.single_flow import ONE_PIXEL_PNG, SingleFlowEngine
|
||
|
|
from engines.vision_provider import VisionProviderError, VisionRecognitionRequest, VisionRecognitionResult
|
||
|
|
|
||
|
|
|
||
|
|
PNG_DATA_URL = f"data:image/png;base64,{base64.b64encode(ONE_PIXEL_PNG).decode('ascii')}"
|
||
|
|
|
||
|
|
|
||
|
|
class _StaticConfigService:
|
||
|
|
def __init__(self, root: Path):
|
||
|
|
self.config = ImageStudioConfig(
|
||
|
|
storage=StorageConfig(
|
||
|
|
images_path=str(root / "images"),
|
||
|
|
thumbnails_path=str(root / "thumbnails"),
|
||
|
|
materials_path=str(root / "materials"),
|
||
|
|
advanced_materials_path=str(root / "advanced_materials"),
|
||
|
|
styles_path=str(root / "styles"),
|
||
|
|
archives_path=str(root / "archives"),
|
||
|
|
temp_path=str(root / "temp"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
def load(self) -> ImageStudioConfig:
|
||
|
|
return self.config
|
||
|
|
|
||
|
|
|
||
|
|
class _StyleVisionProvider:
|
||
|
|
provider_id = "style-vision"
|
||
|
|
|
||
|
|
def __init__(self) -> None:
|
||
|
|
self.requests: list[VisionRecognitionRequest] = []
|
||
|
|
self.fail_reviews = False
|
||
|
|
|
||
|
|
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
||
|
|
self.requests.append(request)
|
||
|
|
if request.metadata.get("style_validation_review"):
|
||
|
|
if self.fail_reviews:
|
||
|
|
raise VisionProviderError(
|
||
|
|
"controlled automatic review failure",
|
||
|
|
provider_id=self.provider_id,
|
||
|
|
error_type="invalid_response",
|
||
|
|
)
|
||
|
|
return VisionRecognitionResult(
|
||
|
|
provider_id=self.provider_id,
|
||
|
|
model="style-vlm",
|
||
|
|
prompt="preserve fine ink linework, rounded stylized face proportions and two-band shadows",
|
||
|
|
negative_prompt="semi-realistic facial anatomy, thick generic outlines",
|
||
|
|
tags=["style-fidelity-review"],
|
||
|
|
confidence=0.88,
|
||
|
|
description="The transferred image preserves most structural and rendering traits.",
|
||
|
|
structured_data={
|
||
|
|
"prompt": "preserve fine ink linework, rounded stylized face proportions and two-band shadows",
|
||
|
|
"negative_prompt": "semi-realistic facial anatomy, thick generic outlines",
|
||
|
|
"fidelity_score": 0.88,
|
||
|
|
"dimension_scores": {"face_proportions": 0.84, "linework": 0.92, "shading": 0.89},
|
||
|
|
"matches": ["fine ink linework", "two-band shadows"],
|
||
|
|
"mismatches": ["face is slightly too realistic"],
|
||
|
|
"correction_prompt": "rounder stylized face, smaller simplified nose and mouth",
|
||
|
|
"negative_correction_prompt": "semi-realistic facial anatomy",
|
||
|
|
},
|
||
|
|
)
|
||
|
|
is_consensus = bool(request.image_uris) or bool(request.metadata.get("consensus_id"))
|
||
|
|
if not is_consensus:
|
||
|
|
sample_id = str(request.metadata.get("source_id") or len(self.requests))
|
||
|
|
prompt = f"sample style traits from {sample_id}"
|
||
|
|
return VisionRecognitionResult(
|
||
|
|
provider_id=self.provider_id,
|
||
|
|
model="style-vlm",
|
||
|
|
prompt=prompt,
|
||
|
|
negative_prompt="photorealism, watermark",
|
||
|
|
tags=["style", "ink", "cel-shading"],
|
||
|
|
confidence=0.94,
|
||
|
|
description="Observable line and coloring traits.",
|
||
|
|
structured_data=_style_structures(prompt),
|
||
|
|
)
|
||
|
|
|
||
|
|
reports = json.loads(str(request.prompt or "{}"))["source_reports"]
|
||
|
|
source_ids = [str(item["source_id"]) for item in reports]
|
||
|
|
instruction = str(request.metadata.get("recognition_instruction") or "")
|
||
|
|
adjustment = "cooler shadows" if "cooler shadows" in instruction else ""
|
||
|
|
prompt = "shared fine ink linework, flat cel colors, two-band shadows"
|
||
|
|
negative_prompt = "photorealism, source identity, watermark"
|
||
|
|
if adjustment:
|
||
|
|
prompt = f"{prompt}, {adjustment}"
|
||
|
|
if "颜色对比太过强烈" in instruction:
|
||
|
|
prompt = "intense color contrast, extreme color dynamics, luminous highly saturated palette"
|
||
|
|
negative_prompt = "low contrast, washed out, source identity, watermark"
|
||
|
|
structured = _style_structures(prompt)
|
||
|
|
structured.update(
|
||
|
|
{
|
||
|
|
"consensus_style": {"linework": "fine ink", "shading": "two-band cel"},
|
||
|
|
"variable_style_features": ["accent saturation"],
|
||
|
|
"source_content_features": ["sample identities and clothing"],
|
||
|
|
"content_leakage_exclusions": ["source identity", "source clothing", "source scene"],
|
||
|
|
"outlier_source_ids": [],
|
||
|
|
"representative_source_ids": source_ids[:3],
|
||
|
|
"consensus_confidence": 0.93,
|
||
|
|
"feature_evidence": [
|
||
|
|
{
|
||
|
|
"trait": "fine ink linework",
|
||
|
|
"supporting_source_ids": source_ids,
|
||
|
|
"support_ratio": 1.0,
|
||
|
|
"confidence": 0.95,
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"validation_cases": [
|
||
|
|
{
|
||
|
|
"case_key": "portrait",
|
||
|
|
"content_prompt": "an elderly male instrument maker in a cool workshop, head portrait",
|
||
|
|
"aspect_ratio": "1:1",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"case_key": "fullbody",
|
||
|
|
"content_prompt": "a non-human courier robot in a rainy harbor, dynamic full body",
|
||
|
|
"aspect_ratio": "2:3",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"case_key": "novel_scene",
|
||
|
|
"content_prompt": "a child botanist on an alien meadow, wide environmental scene",
|
||
|
|
"aspect_ratio": "16:9",
|
||
|
|
},
|
||
|
|
],
|
||
|
|
}
|
||
|
|
)
|
||
|
|
return VisionRecognitionResult(
|
||
|
|
provider_id=self.provider_id,
|
||
|
|
model="style-vlm",
|
||
|
|
prompt=prompt,
|
||
|
|
negative_prompt=negative_prompt,
|
||
|
|
tags=["style", "ink", "cel-shading"],
|
||
|
|
confidence=0.93,
|
||
|
|
description="Cross-image transferable style consensus.",
|
||
|
|
structured_data=structured,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def _style_structures(prompt: str) -> dict[str, Any]:
|
||
|
|
capsules = {
|
||
|
|
tier: {
|
||
|
|
"prompt_parts": [f"{prompt} / {tier}"],
|
||
|
|
"negative_parts": ["photorealism", "watermark"],
|
||
|
|
}
|
||
|
|
for tier in ("compact", "standard", "detailed")
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
"raw_analysis": {
|
||
|
|
"attributes": {
|
||
|
|
"linework": {
|
||
|
|
"value": "fine ink linework",
|
||
|
|
"confidence": 0.95,
|
||
|
|
"source_state": "observed",
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"normalized_asset_profile": {
|
||
|
|
"category": "style",
|
||
|
|
"role": "style",
|
||
|
|
"body_slots": ["global_style"],
|
||
|
|
"must_preserve": ["fine ink linework", "two-band shadows"],
|
||
|
|
},
|
||
|
|
"generation_capsules": capsules,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
class _StyleImageProvider:
|
||
|
|
provider_id = "style-image"
|
||
|
|
|
||
|
|
def __init__(self) -> None:
|
||
|
|
self.requests: list[ImageGenerationRequest] = []
|
||
|
|
self.fail_prompt_once: str | None = None
|
||
|
|
self._failed = False
|
||
|
|
|
||
|
|
def generate_image(self, request: ImageGenerationRequest) -> ImageGenerationResult:
|
||
|
|
self.requests.append(request)
|
||
|
|
if self.fail_prompt_once and self.fail_prompt_once in request.prompt and not self._failed:
|
||
|
|
self._failed = True
|
||
|
|
raise ImageProviderError(
|
||
|
|
"controlled style validation failure",
|
||
|
|
provider_id=self.provider_id,
|
||
|
|
error_type="timeout",
|
||
|
|
)
|
||
|
|
return ImageGenerationResult(
|
||
|
|
provider_id=self.provider_id,
|
||
|
|
model=request.model or "style-image-model",
|
||
|
|
images=[
|
||
|
|
GeneratedImagePayload(
|
||
|
|
b64_json=base64.b64encode(ONE_PIXEL_PNG).decode("ascii"),
|
||
|
|
revised_prompt=request.prompt,
|
||
|
|
)
|
||
|
|
],
|
||
|
|
usage={"images": 1},
|
||
|
|
raw_metadata={"provider_base_url": "https://style.invalid"},
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class _Runtime:
|
||
|
|
def __init__(self, vision: _StyleVisionProvider, image: _StyleImageProvider) -> None:
|
||
|
|
self.vision = vision
|
||
|
|
self.image = image
|
||
|
|
|
||
|
|
def vision_provider_for_route(self, _route: dict[str, Any]) -> _StyleVisionProvider:
|
||
|
|
return self.vision
|
||
|
|
|
||
|
|
def image_provider_for_route(self, _route: dict[str, Any]) -> _StyleImageProvider:
|
||
|
|
return self.image
|
||
|
|
|
||
|
|
|
||
|
|
class _CapturingSingleFlowEngine(SingleFlowEngine):
|
||
|
|
def __init__(self, **kwargs: Any) -> None:
|
||
|
|
super().__init__(**kwargs)
|
||
|
|
self.requests: list[dict[str, Any]] = []
|
||
|
|
|
||
|
|
def generate(
|
||
|
|
self,
|
||
|
|
node: WorkflowNode,
|
||
|
|
task: GenerationTask,
|
||
|
|
request: dict[str, Any],
|
||
|
|
):
|
||
|
|
self.requests.append(dict(request))
|
||
|
|
return super().generate(node, task, request)
|
||
|
|
|
||
|
|
|
||
|
|
class _RouteService:
|
||
|
|
def resolve_route(self, route_name: str, *, modality: str | None = None) -> dict[str, Any]:
|
||
|
|
if modality != "image_generation":
|
||
|
|
raise AssertionError(f"unexpected modality: {modality}")
|
||
|
|
return {
|
||
|
|
"route_name": route_name,
|
||
|
|
"status": "resolved",
|
||
|
|
"provider_id": "style-image",
|
||
|
|
"model_id": "style-image-model",
|
||
|
|
"provider": {
|
||
|
|
"provider_id": "style-image",
|
||
|
|
"base_url": "https://style.invalid",
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class _Fixture:
|
||
|
|
database: Database
|
||
|
|
service: AdvancedMaterialService
|
||
|
|
queue: QueueService
|
||
|
|
materials: MaterialRepository
|
||
|
|
advanced: AdvancedMaterialRepository
|
||
|
|
storage_root: Path
|
||
|
|
vision: _StyleVisionProvider
|
||
|
|
image: _StyleImageProvider
|
||
|
|
engine: _CapturingSingleFlowEngine
|
||
|
|
style_category_id: str
|
||
|
|
other_category_id: str
|
||
|
|
|
||
|
|
|
||
|
|
def _fixture(root: Path) -> _Fixture:
|
||
|
|
database = Database(
|
||
|
|
DatabaseSettings(
|
||
|
|
backend="sqlite",
|
||
|
|
sqlite=SqliteSettings(path=str(root / "advanced-style-set.sqlite3")),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
database.initialize()
|
||
|
|
audit = AuditService(database)
|
||
|
|
queue = QueueService(JobRepository(database), audit)
|
||
|
|
materials = MaterialRepository(database)
|
||
|
|
style = materials.create_category(
|
||
|
|
{
|
||
|
|
"category_id": "cat_test_style",
|
||
|
|
"name": "style",
|
||
|
|
"sort_order": 1,
|
||
|
|
"metadata": {"frontend_group": "style"},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
other = materials.create_category(
|
||
|
|
{
|
||
|
|
"category_id": "cat_test_garment",
|
||
|
|
"name": "garment",
|
||
|
|
"sort_order": 2,
|
||
|
|
"metadata": {"frontend_group": "clothing"},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
advanced = AdvancedMaterialRepository(database)
|
||
|
|
storage_root = root / "vfs"
|
||
|
|
storage = VfsStorage(_StaticConfigService(storage_root)) # type: ignore[arg-type]
|
||
|
|
vision = _StyleVisionProvider()
|
||
|
|
image = _StyleImageProvider()
|
||
|
|
runtime = _Runtime(vision, image)
|
||
|
|
engine = _CapturingSingleFlowEngine(
|
||
|
|
storage=storage,
|
||
|
|
image_provider_factory=runtime.image_provider_for_route,
|
||
|
|
allow_mock_fallback=False,
|
||
|
|
)
|
||
|
|
config = {
|
||
|
|
"model_assignments": {
|
||
|
|
"advanced_material_recognition": "style-vision-route",
|
||
|
|
"advanced_material_validation_generation": "style-image-route",
|
||
|
|
}
|
||
|
|
}
|
||
|
|
service = AdvancedMaterialService(
|
||
|
|
advanced,
|
||
|
|
materials,
|
||
|
|
queue,
|
||
|
|
audit,
|
||
|
|
_RouteService(), # type: ignore[arg-type]
|
||
|
|
runtime, # type: ignore[arg-type]
|
||
|
|
config=config,
|
||
|
|
storage=storage,
|
||
|
|
single_flow_engine=engine,
|
||
|
|
prompt_moderation_service=PromptModerationService({}),
|
||
|
|
generation_route_service=GenerationRouteService({}),
|
||
|
|
)
|
||
|
|
return _Fixture(
|
||
|
|
database=database,
|
||
|
|
service=service,
|
||
|
|
queue=queue,
|
||
|
|
materials=materials,
|
||
|
|
advanced=advanced,
|
||
|
|
storage_root=storage_root,
|
||
|
|
vision=vision,
|
||
|
|
image=image,
|
||
|
|
engine=engine,
|
||
|
|
style_category_id=style.category_id,
|
||
|
|
other_category_id=other.category_id,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def _images(count: int) -> list[dict[str, Any]]:
|
||
|
|
return [
|
||
|
|
{
|
||
|
|
"filename": f"style-{index + 1}.png",
|
||
|
|
"content_type": "image/png",
|
||
|
|
"image_data_url": PNG_DATA_URL,
|
||
|
|
"upload_source": "file",
|
||
|
|
}
|
||
|
|
for index in range(count)
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def _upload(fixture: _Fixture, count: int = 3) -> dict[str, Any]:
|
||
|
|
return fixture.service.upload_style_set(
|
||
|
|
{
|
||
|
|
"display_name": "Parallel style set",
|
||
|
|
"category_id": fixture.style_category_id,
|
||
|
|
"images": _images(count),
|
||
|
|
},
|
||
|
|
RequestContext.system("advanced-style-test"),
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def _execute_source_stage(fixture: _Fixture, count: int = 3) -> tuple[str, Any]:
|
||
|
|
uploaded = _upload(fixture, count)
|
||
|
|
for item in uploaded["tasks"]:
|
||
|
|
task = fixture.queue.get_job(item["task_id"])
|
||
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-style-worker"))
|
||
|
|
import_id = uploaded["import"]["import_id"]
|
||
|
|
consensus = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||
|
|
if consensus is None:
|
||
|
|
raise AssertionError("source barrier did not create a style consensus")
|
||
|
|
return import_id, consensus
|
||
|
|
|
||
|
|
|
||
|
|
def _execute_consensus(fixture: _Fixture, consensus: Any) -> tuple[Any, list[Any]]:
|
||
|
|
task = fixture.queue.get_job(consensus.task_id)
|
||
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-style-worker"))
|
||
|
|
record = fixture.advanced.get_import(consensus.import_id)
|
||
|
|
if record is None or not record.current_attempt_id:
|
||
|
|
raise AssertionError("consensus did not create a validation batch")
|
||
|
|
attempt = fixture.advanced.get_attempt(record.current_attempt_id)
|
||
|
|
cases = fixture.advanced.list_style_validation_cases(record.current_attempt_id)
|
||
|
|
if attempt is None:
|
||
|
|
raise AssertionError("validation attempt is missing")
|
||
|
|
return attempt, cases
|
||
|
|
|
||
|
|
|
||
|
|
def _execute_validation_cases(fixture: _Fixture, cases: list[Any]) -> None:
|
||
|
|
for case in cases:
|
||
|
|
task = fixture.queue.get_job(case.task_id)
|
||
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-style-worker"))
|
||
|
|
|
||
|
|
|
||
|
|
class AdvancedStyleSetWorkflowTests(unittest.TestCase):
|
||
|
|
def setUp(self) -> None:
|
||
|
|
self.temporary = tempfile.TemporaryDirectory(prefix="imagestudio-style-set-")
|
||
|
|
self.root = Path(self.temporary.name)
|
||
|
|
|
||
|
|
def tearDown(self) -> None:
|
||
|
|
self.temporary.cleanup()
|
||
|
|
|
||
|
|
def test_style_only_boundaries_and_upload_creates_independent_source_tasks(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
ctx = RequestContext.system("advanced-style-boundaries")
|
||
|
|
|
||
|
|
with self.assertRaises(ApiError):
|
||
|
|
fixture.service.upload_style_set(
|
||
|
|
{
|
||
|
|
"display_name": "not style",
|
||
|
|
"category_id": fixture.other_category_id,
|
||
|
|
"images": _images(2),
|
||
|
|
},
|
||
|
|
ctx,
|
||
|
|
)
|
||
|
|
with self.assertRaises(ApiError):
|
||
|
|
fixture.service.upload_style_set(
|
||
|
|
{"display_name": "too few", "category_id": fixture.style_category_id, "images": _images(1)},
|
||
|
|
ctx,
|
||
|
|
)
|
||
|
|
with self.assertRaises(ApiError):
|
||
|
|
fixture.service.upload_style_set(
|
||
|
|
{"display_name": "too many", "category_id": fixture.style_category_id, "images": _images(9)},
|
||
|
|
ctx,
|
||
|
|
)
|
||
|
|
|
||
|
|
minimum = _upload(fixture, 2)
|
||
|
|
maximum = _upload(fixture, 8)
|
||
|
|
self.assertEqual(len(minimum["sources"]), 2)
|
||
|
|
self.assertEqual(len(maximum["sources"]), 8)
|
||
|
|
self.assertEqual(len(maximum["tasks"]), 8)
|
||
|
|
self.assertEqual(len({item["source_id"] for item in maximum["sources"]}), 8)
|
||
|
|
self.assertEqual(len({item["task_id"] for item in maximum["tasks"]}), 8)
|
||
|
|
self.assertTrue(all(item["task_type"] == "advanced_style_source_recognition" for item in maximum["tasks"]))
|
||
|
|
|
||
|
|
def test_consensus_reads_all_sources_and_creates_exactly_three_prompt_only_cases(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
|
||
|
|
source_requests = fixture.vision.requests[:3]
|
||
|
|
self.assertEqual(len(source_requests), 3)
|
||
|
|
self.assertTrue(all(len(item.image_uris) == 0 for item in source_requests))
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
|
||
|
|
with self.assertRaises(ApiError):
|
||
|
|
fixture.service.create_style_consensus(
|
||
|
|
import_id,
|
||
|
|
{},
|
||
|
|
RequestContext.system("advanced-style-overlap-guard"),
|
||
|
|
)
|
||
|
|
|
||
|
|
consensus_request = fixture.vision.requests[-1]
|
||
|
|
self.assertEqual(1 + len(consensus_request.image_uris), 3)
|
||
|
|
reports = json.loads(str(consensus_request.prompt))["source_reports"]
|
||
|
|
self.assertEqual({item["source_id"] for item in reports}, set(consensus.source_ids))
|
||
|
|
self.assertEqual(len(cases), 3)
|
||
|
|
self.assertEqual([item.case_key for item in cases], ["portrait", "fullbody", "novel_scene"])
|
||
|
|
self.assertEqual(len({item.task_id for item in cases}), 3)
|
||
|
|
self.assertTrue(all("source clothing" not in str(item.negative_prompt or "") for item in cases))
|
||
|
|
self.assertTrue(all(item.final_prompt.count("shared fine ink linework") == 1 for item in cases))
|
||
|
|
|
||
|
|
_execute_validation_cases(fixture, cases)
|
||
|
|
|
||
|
|
self.assertEqual(len(fixture.engine.requests), 3)
|
||
|
|
for request in fixture.engine.requests:
|
||
|
|
self.assertNotIn("image", request)
|
||
|
|
self.assertNotIn("input_images", request)
|
||
|
|
self.assertEqual(request["output_scope"]["kind"], "advanced_style_validation")
|
||
|
|
self.assertEqual(len(fixture.image.requests), 3)
|
||
|
|
self.assertTrue(all(item.input_images == () for item in fixture.image.requests))
|
||
|
|
self.assertTrue(all("/ detailed" in item["expanded_prompt"] for item in fixture.engine.requests))
|
||
|
|
completed_cases = fixture.advanced.list_style_validation_cases(attempt.attempt_id)
|
||
|
|
self.assertTrue(
|
||
|
|
all(
|
||
|
|
str(item.validation_image_uri).startswith("storage://advanced_materials/style-validations/")
|
||
|
|
for item in completed_cases
|
||
|
|
)
|
||
|
|
)
|
||
|
|
self.assertTrue(all(0.0 <= float(item.fidelity_score or 0.0) <= 1.0 for item in completed_cases))
|
||
|
|
self.assertTrue(
|
||
|
|
all(item.style_review["score_calibration"]["source"] == "server_weighted_dimensions" for item in completed_cases)
|
||
|
|
)
|
||
|
|
self.assertTrue(all(item.style_review.get("status") == "succeeded" for item in completed_cases))
|
||
|
|
review_requests = [item for item in fixture.vision.requests if item.metadata.get("style_validation_review")]
|
||
|
|
self.assertEqual(len(review_requests), 3)
|
||
|
|
self.assertTrue(all(1 + len(item.image_uris) == 4 for item in review_requests))
|
||
|
|
reviewed_attempt = fixture.advanced.get_attempt(attempt.attempt_id)
|
||
|
|
expected_average = sum(float(item.fidelity_score or 0.0) for item in completed_cases) / len(completed_cases)
|
||
|
|
self.assertAlmostEqual(float(reviewed_attempt.fidelity_score or 0.0), expected_average)
|
||
|
|
self.assertFalse((fixture.storage_root / "images").exists())
|
||
|
|
self.assertFalse((fixture.storage_root / "thumbnails").exists())
|
||
|
|
record = fixture.advanced.get_import(import_id)
|
||
|
|
self.assertIsNotNone(record)
|
||
|
|
self.assertEqual(record.status, "awaiting_review")
|
||
|
|
|
||
|
|
def test_style_normalization_preserves_profile_attributes_and_intact_capsule_phrases(self) -> None:
|
||
|
|
service = MaterialNormalizationService()
|
||
|
|
phrase = "highly glossy, sharp specular highlights on hair, skin, and eyes"
|
||
|
|
payload = {
|
||
|
|
"raw_analysis": {"style_extraction": {"medium": "clean digital illustration"}},
|
||
|
|
"normalized_asset_profile": {
|
||
|
|
"category": "style",
|
||
|
|
"role": "style",
|
||
|
|
"body_slots": ["global_style"],
|
||
|
|
"attributes": {
|
||
|
|
"face_proportions": {
|
||
|
|
"value": "rounded short face, large eye-to-face ratio, tiny simplified nose and mouth",
|
||
|
|
"confidence": 0.96,
|
||
|
|
"source": "consensus",
|
||
|
|
},
|
||
|
|
"linework": {
|
||
|
|
"value": "fine tapered locally colored lines",
|
||
|
|
"confidence": 0.95,
|
||
|
|
"source": "consensus",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
"generation_capsules": {
|
||
|
|
tier: {"prompt_parts": [phrase], "negative_parts": ["semi-realistic facial anatomy"]}
|
||
|
|
for tier in ("compact", "standard", "detailed")
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
result = service.normalize_recognition(
|
||
|
|
payload,
|
||
|
|
prompt="specific stylized illustration grammar",
|
||
|
|
negative_prompt="semi-realistic facial anatomy",
|
||
|
|
tags=["style"],
|
||
|
|
confidence=0.96,
|
||
|
|
provider_id="test-vlm",
|
||
|
|
model="test-style-model",
|
||
|
|
route_name="test-style-route",
|
||
|
|
category_hint="style",
|
||
|
|
)
|
||
|
|
|
||
|
|
profile = result["normalized_asset_profile"]
|
||
|
|
self.assertIn("face_proportions", profile["attributes"])
|
||
|
|
self.assertIn("linework", profile["attributes"])
|
||
|
|
self.assertTrue(any("face proportions" in item for item in profile["must_preserve"]))
|
||
|
|
self.assertEqual(result["generation_capsules"]["detailed"]["prompt_parts"], [phrase])
|
||
|
|
|
||
|
|
def test_one_validation_case_failure_is_isolated_and_retryable(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
fixture.image.fail_prompt_once = "non-human courier robot"
|
||
|
|
|
||
|
|
failed_task_id = ""
|
||
|
|
for case in cases:
|
||
|
|
task = fixture.queue.get_job(case.task_id)
|
||
|
|
try:
|
||
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-style-worker"))
|
||
|
|
except ImageProviderError as exc:
|
||
|
|
failed_task_id = task.task_id
|
||
|
|
fixture.service.handle_task_failure(task, exc, RequestContext.system("advanced-style-worker"))
|
||
|
|
|
||
|
|
self.assertTrue(failed_task_id)
|
||
|
|
current = fixture.advanced.list_style_validation_cases(attempt.attempt_id)
|
||
|
|
self.assertEqual([item.status for item in current].count("failed"), 1)
|
||
|
|
self.assertEqual([item.status for item in current].count("succeeded"), 2)
|
||
|
|
self.assertEqual(fixture.advanced.get_import(import_id).status, "validation_partial_failed")
|
||
|
|
failed = next(item for item in current if item.status == "failed")
|
||
|
|
|
||
|
|
retried = fixture.service.retry_style_validation_case(
|
||
|
|
import_id,
|
||
|
|
failed.case_id,
|
||
|
|
RequestContext.system("advanced-style-retry"),
|
||
|
|
)
|
||
|
|
self.assertNotEqual(retried["task"]["task_id"], failed_task_id)
|
||
|
|
self.assertEqual(retried["case"]["metadata"]["retry_count"], 1)
|
||
|
|
old_task = fixture.queue.get_job(failed_task_id)
|
||
|
|
fixture.service.handle_task_failure(
|
||
|
|
old_task,
|
||
|
|
RuntimeError("late failure from superseded validation task"),
|
||
|
|
RequestContext.system("advanced-style-stale-worker"),
|
||
|
|
)
|
||
|
|
still_retried = fixture.advanced.get_style_validation_case(failed.case_id)
|
||
|
|
self.assertEqual(still_retried.status, "queued")
|
||
|
|
self.assertEqual(still_retried.task_id, retried["task"]["task_id"])
|
||
|
|
retry_task = fixture.queue.get_job(retried["task"]["task_id"])
|
||
|
|
fixture.service.execute_task(retry_task, RequestContext.system("advanced-style-worker"))
|
||
|
|
|
||
|
|
self.assertEqual(fixture.advanced.get_import(import_id).status, "awaiting_review")
|
||
|
|
self.assertTrue(
|
||
|
|
all(item.status == "succeeded" for item in fixture.advanced.list_style_validation_cases(attempt.attempt_id))
|
||
|
|
)
|
||
|
|
|
||
|
|
def test_automatic_style_review_failure_never_discards_generated_validation_images(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
fixture.vision.fail_reviews = True
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
|
||
|
|
_execute_validation_cases(fixture, cases)
|
||
|
|
|
||
|
|
completed = fixture.advanced.list_style_validation_cases(attempt.attempt_id)
|
||
|
|
self.assertTrue(all(item.status == "succeeded" and item.validation_image_uri for item in completed))
|
||
|
|
self.assertTrue(all(item.style_review.get("status") == "review_failed" for item in completed))
|
||
|
|
self.assertTrue(all(item.fidelity_score is None for item in completed))
|
||
|
|
self.assertEqual(fixture.advanced.get_import(import_id).status, "awaiting_review")
|
||
|
|
|
||
|
|
def test_rejection_creates_new_consensus_revision_and_rebuilds_capsules(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
_execute_validation_cases(fixture, cases)
|
||
|
|
first_consensus = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||
|
|
first_capsule = first_consensus.result["generation_capsules"]["detailed"]["prompt_parts"]
|
||
|
|
first_prompts = [item.content_prompt for item in cases]
|
||
|
|
|
||
|
|
rebuilt = fixture.service.reject_attempt(
|
||
|
|
import_id,
|
||
|
|
attempt.attempt_id,
|
||
|
|
{"style_adjustment_text": "use cooler shadows", "decision_notes": "shadows are too warm"},
|
||
|
|
RequestContext.system("advanced-style-reviewer"),
|
||
|
|
)
|
||
|
|
consensus_task = fixture.queue.get_job(rebuilt["task"]["task_id"])
|
||
|
|
fixture.service.execute_task(consensus_task, RequestContext.system("advanced-style-worker"))
|
||
|
|
|
||
|
|
latest = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||
|
|
self.assertEqual(latest.revision, first_consensus.revision + 1)
|
||
|
|
self.assertIn("cooler shadows", latest.prompt)
|
||
|
|
second_capsule = latest.result["generation_capsules"]["detailed"]["prompt_parts"]
|
||
|
|
self.assertNotEqual(first_capsule, second_capsule)
|
||
|
|
self.assertTrue(any("cooler shadows" in item for item in second_capsule))
|
||
|
|
record = fixture.advanced.get_import(import_id)
|
||
|
|
next_cases = fixture.advanced.list_style_validation_cases(record.current_attempt_id)
|
||
|
|
self.assertEqual([item.content_prompt for item in next_cases], first_prompts)
|
||
|
|
self.assertEqual([item.seed for item in next_cases], [item.seed for item in cases])
|
||
|
|
self.assertEqual(fixture.advanced.get_attempt(attempt.attempt_id).status, "rejected")
|
||
|
|
|
||
|
|
def test_style_feedback_compares_previous_images_and_cannot_invert_decrease_direction(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
_execute_validation_cases(fixture, cases)
|
||
|
|
|
||
|
|
rebuilt = fixture.service.reject_attempt(
|
||
|
|
import_id,
|
||
|
|
attempt.attempt_id,
|
||
|
|
{"style_adjustment_text": "颜色对比太过强烈"},
|
||
|
|
RequestContext.system("advanced-style-direction-guard"),
|
||
|
|
)
|
||
|
|
consensus_task = fixture.queue.get_job(rebuilt["task"]["task_id"])
|
||
|
|
fixture.service.execute_task(consensus_task, RequestContext.system("advanced-style-worker"))
|
||
|
|
|
||
|
|
request = fixture.vision.requests[-1]
|
||
|
|
self.assertEqual(1 + len(request.image_uris), 6)
|
||
|
|
manifest = request.metadata["image_manifest"]
|
||
|
|
self.assertEqual([item["kind"] for item in manifest].count("source_reference"), 3)
|
||
|
|
self.assertEqual([item["kind"] for item in manifest].count("previous_validation"), 3)
|
||
|
|
request_payload = json.loads(str(request.prompt))
|
||
|
|
self.assertEqual(len(request_payload["previous_validation"]["validation_cases"]), 3)
|
||
|
|
self.assertEqual(request_payload["style_adjustment_plan"]["decrease"], ["color contrast"])
|
||
|
|
|
||
|
|
latest = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||
|
|
self.assertIsNotNone(latest)
|
||
|
|
positive = str(latest.prompt or "").casefold()
|
||
|
|
negative = str(latest.negative_prompt or "").casefold()
|
||
|
|
self.assertNotIn("intense color contrast", positive)
|
||
|
|
self.assertNotIn("extreme color dynamics", positive)
|
||
|
|
self.assertNotIn("high contrast", positive)
|
||
|
|
self.assertIn("balanced moderate color contrast", positive)
|
||
|
|
self.assertIn("restrained color separation", positive)
|
||
|
|
self.assertNotIn("low contrast", negative)
|
||
|
|
self.assertNotIn("washed out", negative)
|
||
|
|
self.assertIn("excessive color contrast", negative)
|
||
|
|
self.assertEqual(latest.result["human_adjustments"]["style_adjustment_plan"]["decrease"], ["color contrast"])
|
||
|
|
self.assertTrue(latest.result["human_adjustments"]["guard"]["applied"])
|
||
|
|
self.assertEqual(len(latest.result["comparison_context"]["validation_cases"]), 3)
|
||
|
|
detailed = latest.result["generation_capsules"]["detailed"]
|
||
|
|
self.assertIn("balanced moderate color contrast", detailed["prompt_parts"])
|
||
|
|
self.assertIn("excessive color contrast", detailed["negative_parts"])
|
||
|
|
|
||
|
|
record = fixture.advanced.get_import(import_id)
|
||
|
|
next_cases = fixture.advanced.list_style_validation_cases(record.current_attempt_id)
|
||
|
|
self.assertTrue(all("balanced moderate color contrast" in item.final_prompt for item in next_cases))
|
||
|
|
start = len(fixture.engine.requests)
|
||
|
|
_execute_validation_cases(fixture, next_cases)
|
||
|
|
generated = fixture.engine.requests[start:]
|
||
|
|
self.assertEqual(len(generated), 3)
|
||
|
|
self.assertTrue(all("balanced moderate color contrast" in item["expanded_prompt"] for item in generated))
|
||
|
|
self.assertTrue(all("excessive color contrast" in item["expanded_negative_prompt"] for item in generated))
|
||
|
|
|
||
|
|
def test_validation_content_feedback_rebuilds_cases_and_changes_seeds(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
_execute_validation_cases(fixture, cases)
|
||
|
|
first_prompts = [item.content_prompt for item in cases]
|
||
|
|
first_seeds = [item.seed for item in cases]
|
||
|
|
instruction = "女性角色,全身,萌袖卫衣、泡泡袜和运动鞋,白发蓝瞳猫耳"
|
||
|
|
|
||
|
|
rebuilt = fixture.service.reject_attempt(
|
||
|
|
import_id,
|
||
|
|
attempt.attempt_id,
|
||
|
|
{"validation_instruction": instruction, "decision_notes": "第一轮角色差异过大"},
|
||
|
|
RequestContext.system("advanced-style-reviewer"),
|
||
|
|
)
|
||
|
|
consensus_task = fixture.queue.get_job(rebuilt["task"]["task_id"])
|
||
|
|
fixture.service.execute_task(consensus_task, RequestContext.system("advanced-style-worker"))
|
||
|
|
|
||
|
|
latest = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||
|
|
self.assertEqual(latest.metadata["validation_instruction"], instruction)
|
||
|
|
record = fixture.advanced.get_import(import_id)
|
||
|
|
next_attempt = fixture.advanced.get_attempt(record.current_attempt_id)
|
||
|
|
next_cases = fixture.advanced.list_style_validation_cases(record.current_attempt_id)
|
||
|
|
self.assertNotEqual([item.content_prompt for item in next_cases], first_prompts)
|
||
|
|
self.assertNotEqual([item.seed for item in next_cases], first_seeds)
|
||
|
|
self.assertTrue(all(instruction in item.content_prompt for item in next_cases))
|
||
|
|
self.assertTrue(all(instruction in item.final_prompt for item in next_cases))
|
||
|
|
self.assertEqual(next_attempt.metadata["validation_instruction"], instruction)
|
||
|
|
self.assertIn("验证内容", next_attempt.adjustment_text)
|
||
|
|
request_count = len(fixture.engine.requests)
|
||
|
|
_execute_validation_cases(fixture, next_cases)
|
||
|
|
next_requests = fixture.engine.requests[request_count:]
|
||
|
|
self.assertEqual(len(next_requests), 3)
|
||
|
|
self.assertTrue(all(instruction in item["expanded_prompt"] for item in next_requests))
|
||
|
|
self.assertTrue(all(item["seed"] in {case.seed for case in next_cases} for item in next_requests))
|
||
|
|
|
||
|
|
def test_style_attempt_listing_includes_all_three_validation_images(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
_execute_validation_cases(fixture, cases)
|
||
|
|
|
||
|
|
listed = fixture.service.list_attempts(import_id)
|
||
|
|
current = next(item for item in listed if item["attempt_id"] == attempt.attempt_id)
|
||
|
|
self.assertEqual(len(current["validation_cases"]), 3)
|
||
|
|
self.assertTrue(all(item["validation_image_uri"] for item in current["validation_cases"]))
|
||
|
|
detail = fixture.service.get_import(import_id)
|
||
|
|
detail_attempt = next(item for item in detail["attempts"] if item["attempt_id"] == attempt.attempt_id)
|
||
|
|
self.assertEqual(len(detail_attempt["validation_cases"]), 3)
|
||
|
|
|
||
|
|
def test_legacy_adjustment_text_is_never_silently_discarded(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
_execute_validation_cases(fixture, cases)
|
||
|
|
legacy_text = "use a white-haired blue-eyed cat-eared woman in the next validation"
|
||
|
|
|
||
|
|
rebuilt = fixture.service.reject_attempt(
|
||
|
|
import_id,
|
||
|
|
attempt.attempt_id,
|
||
|
|
{"adjustment_text": legacy_text},
|
||
|
|
RequestContext.system("legacy-client"),
|
||
|
|
)
|
||
|
|
consensus_task = fixture.queue.get_job(rebuilt["task"]["task_id"])
|
||
|
|
fixture.service.execute_task(consensus_task, RequestContext.system("advanced-style-worker"))
|
||
|
|
|
||
|
|
latest = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||
|
|
next_cases = fixture.advanced.list_style_validation_cases(fixture.advanced.get_import(import_id).current_attempt_id)
|
||
|
|
self.assertEqual(latest.metadata["style_adjustment_text"], legacy_text)
|
||
|
|
self.assertEqual(latest.metadata["validation_instruction"], legacy_text)
|
||
|
|
self.assertTrue(all(legacy_text in item.final_prompt for item in next_cases))
|
||
|
|
|
||
|
|
def test_three_successful_cases_seal_one_advanced_material_idempotently(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 3)
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
_execute_validation_cases(fixture, cases)
|
||
|
|
ctx = RequestContext.system("advanced-style-accept")
|
||
|
|
|
||
|
|
accepted = fixture.service.accept_attempt(
|
||
|
|
import_id,
|
||
|
|
attempt.attempt_id,
|
||
|
|
{"fidelity_score": 0.94, "decision_notes": "style transfers across all scenes"},
|
||
|
|
ctx,
|
||
|
|
)
|
||
|
|
replay = fixture.service.accept_attempt(import_id, attempt.attempt_id, {}, ctx)
|
||
|
|
|
||
|
|
self.assertEqual(accepted["material"]["material_id"], replay["material"]["material_id"])
|
||
|
|
self.assertTrue(accepted["material"]["material_id"].startswith("advmat_"))
|
||
|
|
self.assertEqual(accepted["material"]["priority"], 100)
|
||
|
|
self.assertEqual(len(accepted["material"]["metadata"]["style_set"]["source_ids"]), 3)
|
||
|
|
self.assertEqual(len(accepted["material"]["metadata"]["style_set"]["validation_cases"]), 3)
|
||
|
|
advanced_items, advanced_total = fixture.advanced.list_materials(page=1, page_size=10)
|
||
|
|
standard_items, standard_total = fixture.materials.list_materials(page=1, page_size=10)
|
||
|
|
self.assertEqual(advanced_total, 1)
|
||
|
|
self.assertEqual([item.material_id for item in advanced_items], [accepted["material"]["material_id"]])
|
||
|
|
self.assertEqual(standard_total, 0)
|
||
|
|
self.assertEqual(standard_items, [])
|
||
|
|
|
||
|
|
def test_queue_write_failures_leave_sources_and_validation_cases_retryable(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
original_create_task = fixture.queue.create_task
|
||
|
|
|
||
|
|
def reject_source_tasks(payload: dict[str, Any], ctx: RequestContext):
|
||
|
|
if payload.get("task_type") == "advanced_style_source_recognition":
|
||
|
|
raise RuntimeError("controlled source queue failure")
|
||
|
|
return original_create_task(payload, ctx)
|
||
|
|
|
||
|
|
fixture.queue.create_task = reject_source_tasks # type: ignore[method-assign]
|
||
|
|
uploaded = _upload(fixture, 2)
|
||
|
|
self.assertEqual(uploaded["tasks"], [])
|
||
|
|
self.assertEqual(uploaded["import"]["status"], "style_source_review_required")
|
||
|
|
self.assertTrue(all(item["status"] == "failed" for item in uploaded["sources"]))
|
||
|
|
|
||
|
|
validation_root = self.root / "validation-queue"
|
||
|
|
validation_root.mkdir(parents=True, exist_ok=True)
|
||
|
|
fixture = _fixture(validation_root)
|
||
|
|
import_id, consensus = _execute_source_stage(fixture, 2)
|
||
|
|
original_create_task = fixture.queue.create_task
|
||
|
|
|
||
|
|
def reject_validation_tasks(payload: dict[str, Any], ctx: RequestContext):
|
||
|
|
if payload.get("task_type") == "advanced_style_validation_generation":
|
||
|
|
raise RuntimeError("controlled validation queue failure")
|
||
|
|
return original_create_task(payload, ctx)
|
||
|
|
|
||
|
|
fixture.queue.create_task = reject_validation_tasks # type: ignore[method-assign]
|
||
|
|
attempt, cases = _execute_consensus(fixture, consensus)
|
||
|
|
self.assertEqual(attempt.status, "validation_partial_failed")
|
||
|
|
self.assertTrue(all(item.status == "failed" and item.task_id is None for item in cases))
|
||
|
|
self.assertEqual(fixture.advanced.get_import(import_id).status, "validation_partial_failed")
|
||
|
|
|
||
|
|
def test_retry_source_refreshes_obsolete_route_for_source_and_consensus(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
uploaded = _upload(fixture, 2)
|
||
|
|
import_id = uploaded["import"]["import_id"]
|
||
|
|
source_id = uploaded["sources"][0]["source_id"]
|
||
|
|
fixture.advanced.update_style_source(
|
||
|
|
source_id,
|
||
|
|
{"status": "failed", "task_id": None, "error": {"error_type": "route_unavailable"}},
|
||
|
|
)
|
||
|
|
repaired_route = encode_model_route_chain(
|
||
|
|
"plugin-vision-route",
|
||
|
|
"astrbot:vision-provider",
|
||
|
|
)
|
||
|
|
fixture.service.config["model_assignments"]["advanced_material_recognition"] = repaired_route
|
||
|
|
|
||
|
|
retried = fixture.service.retry_style_source(
|
||
|
|
import_id,
|
||
|
|
source_id,
|
||
|
|
RequestContext.system("advanced-style-route-repair"),
|
||
|
|
)
|
||
|
|
|
||
|
|
source = fixture.advanced.get_style_source(source_id)
|
||
|
|
record = fixture.advanced.get_import(import_id)
|
||
|
|
task = fixture.queue.get_job(retried["task"]["task_id"])
|
||
|
|
self.assertEqual(source.recognition_model_route, repaired_route)
|
||
|
|
self.assertEqual(record.recognition_model_route, repaired_route)
|
||
|
|
self.assertEqual(task.model_route, repaired_route)
|
||
|
|
|
||
|
|
def test_deleted_queue_row_is_reconciled_and_import_can_be_removed(self) -> None:
|
||
|
|
fixture = _fixture(self.root)
|
||
|
|
uploaded = _upload(fixture, 2)
|
||
|
|
import_id = uploaded["import"]["import_id"]
|
||
|
|
source_id = uploaded["sources"][0]["source_id"]
|
||
|
|
task_id = uploaded["tasks"][0]["task_id"]
|
||
|
|
source_before = fixture.advanced.get_style_source(source_id)
|
||
|
|
source_path = fixture.service.storage.ensure_file(source_before.source_image_uri)
|
||
|
|
self.assertTrue(source_path.exists())
|
||
|
|
|
||
|
|
fixture.queue.update_task(
|
||
|
|
task_id,
|
||
|
|
{"status": "failed", "error_message": "controlled queue failure"},
|
||
|
|
RequestContext.system("advanced-style-queue-failure"),
|
||
|
|
)
|
||
|
|
fixture.queue.delete_job(task_id, RequestContext.system("advanced-style-queue-delete"))
|
||
|
|
|
||
|
|
detail = fixture.service.get_import(import_id)
|
||
|
|
source_after = fixture.advanced.get_style_source(source_id)
|
||
|
|
self.assertEqual(source_after.status, "failed")
|
||
|
|
self.assertIsNone(source_after.task_id)
|
||
|
|
self.assertEqual(source_after.error["error_type"], "queue_task_missing")
|
||
|
|
self.assertEqual(detail["import"]["status"], "style_source_review_required")
|
||
|
|
|
||
|
|
deleted = fixture.service.delete_import(
|
||
|
|
import_id,
|
||
|
|
RequestContext.system("advanced-style-delete-import"),
|
||
|
|
)
|
||
|
|
self.assertTrue(deleted["deleted"])
|
||
|
|
self.assertIsNone(fixture.advanced.get_import(import_id))
|
||
|
|
self.assertIsNone(fixture.advanced.get_style_source(source_id))
|
||
|
|
self.assertFalse(source_path.exists())
|
||
|
|
|
||
|
|
def test_style_review_scores_are_server_weighted_with_fixed_dimensions(self) -> None:
|
||
|
|
normalized = _normalize_style_dimension_scores(
|
||
|
|
{
|
||
|
|
"face_proportions": 0.8,
|
||
|
|
"iris_construction": {"score": 0.8},
|
||
|
|
"line_art": 0.8,
|
||
|
|
"global_palette": 0.8,
|
||
|
|
"local_contrast": 0.8,
|
||
|
|
"shadow_rendering": 0.8,
|
||
|
|
"highlight_shape": 0.8,
|
||
|
|
"body_ratio": 0.8,
|
||
|
|
"hair_strands": 0.8,
|
||
|
|
"detail_density": 0.8,
|
||
|
|
"environment": 0.8,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
self.assertEqual(set(normalized), set(STYLE_REVIEW_DIMENSIONS))
|
||
|
|
score, calibration = _weighted_style_fidelity_score("portrait", normalized, 0.99)
|
||
|
|
self.assertAlmostEqual(float(score or 0.0), 0.8)
|
||
|
|
self.assertEqual(calibration["source"], "server_weighted_dimensions")
|
||
|
|
self.assertEqual(calibration["dimension_coverage"], 1.0)
|
||
|
|
|
||
|
|
def test_style_prompt_compiler_rewrites_semantic_risks_and_keeps_scale_overrides(self) -> None:
|
||
|
|
payload = {
|
||
|
|
"normalized_asset_profile": {
|
||
|
|
"must_preserve": ["fine colored linework", "segmented ring halo highlight across the hair"],
|
||
|
|
},
|
||
|
|
"generation_capsules": {
|
||
|
|
"detailed": {
|
||
|
|
"prompt_parts": [
|
||
|
|
"fine colored linework with tapered ends",
|
||
|
|
"fine colored linework",
|
||
|
|
"soft two-band cel shading",
|
||
|
|
],
|
||
|
|
"negative_parts": ["photorealism"],
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"prompt_priority_groups": {
|
||
|
|
"core": ["rounded stylized facial structure"],
|
||
|
|
"structural": ["large layered irises"],
|
||
|
|
"rendering": ["pastel global palette with crisp local separation"],
|
||
|
|
},
|
||
|
|
"semantic_risk_audit": [
|
||
|
|
{
|
||
|
|
"original_phrase": "segmented ring halo highlight across the hair",
|
||
|
|
"safe_rewrite": "segmented satin sheen painted directly on the curved hair surface",
|
||
|
|
"negative_guard": "floating luminous object detached from the styled surface",
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"scale_capsules": {
|
||
|
|
"fullbody": {"prompt_parts": ["readable colored silhouette lines at full-body scale"]}
|
||
|
|
},
|
||
|
|
}
|
||
|
|
prompt = _detailed_style_prompt(payload, "fallback")
|
||
|
|
negative = _detailed_style_negative_prompt(payload, "")
|
||
|
|
self.assertNotIn("ring halo", prompt)
|
||
|
|
self.assertIn("painted directly on the curved hair surface", prompt)
|
||
|
|
self.assertEqual(prompt.count("fine colored linework"), 1)
|
||
|
|
self.assertIn("detached from the styled surface", negative)
|
||
|
|
self.assertIn("full-body scale", _scale_specific_style_prompt(payload, "fullbody"))
|
||
|
|
|
||
|
|
def test_review_aggregation_separates_recurrent_and_scale_specific_failures(self) -> None:
|
||
|
|
def case(case_key: str, case_id: str, scores: dict[str, float]) -> SimpleNamespace:
|
||
|
|
return SimpleNamespace(
|
||
|
|
case_key=case_key,
|
||
|
|
case_id=case_id,
|
||
|
|
fidelity_score=0.6,
|
||
|
|
style_review={
|
||
|
|
"status": "succeeded",
|
||
|
|
"fidelity_score": 0.6,
|
||
|
|
"dimension_scores": scores,
|
||
|
|
"style_deltas": [],
|
||
|
|
"prompt_patch": {},
|
||
|
|
"semantic_risks": [],
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
feedback = _aggregate_style_review_feedback(
|
||
|
|
[
|
||
|
|
case("portrait", "p", {"linework": 0.5, "eye_design": 0.5}),
|
||
|
|
case("fullbody", "f", {"linework": 0.6, "anatomy_scale": 0.5}),
|
||
|
|
case("novel_scene", "n", {"linework": 0.55, "background_rendering": 0.5}),
|
||
|
|
]
|
||
|
|
)
|
||
|
|
self.assertEqual(feedback["recurrent_weak_dimensions"], ["linework"])
|
||
|
|
self.assertEqual(feedback["scale_specific_weak_dimensions"]["portrait"], ["eye_design"])
|
||
|
|
self.assertEqual(feedback["scale_specific_weak_dimensions"]["fullbody"], ["anatomy_scale"])
|
||
|
|
self.assertEqual(feedback["scale_specific_weak_dimensions"]["novel_scene"], ["background_rendering"])
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|