461 行
17 KiB
Python
461 行
17 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from types import SimpleNamespace
|
||
|
|
|
||
|
|
from backend.services.composition_builder import (
|
||
|
|
ADVANCED_VERIFIED_PRIORITY_BONUS,
|
||
|
|
CompositionBuilder,
|
||
|
|
)
|
||
|
|
from backend.services.generation_reference_service import GenerationReferenceService
|
||
|
|
from backend.services.generation_service import _route_aware_reference_snapshot
|
||
|
|
from backend.services.material_catalog_resolver import (
|
||
|
|
ADVANCED_VERIFIED_PRIORITY,
|
||
|
|
CatalogMaterialMetadata,
|
||
|
|
MaterialCatalogResolver,
|
||
|
|
is_trusted_advanced_metadata,
|
||
|
|
)
|
||
|
|
from backend.services.workflow_material_snapshot_service import WorkflowMaterialSnapshotService
|
||
|
|
from backend.services.wardrobe_service import WardrobeService
|
||
|
|
from core.audit_service import AuditService
|
||
|
|
from core.identity import RequestContext
|
||
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||
|
|
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||
|
|
from database.repositories.material_repository import MaterialRepository
|
||
|
|
from database.repositories.profile_repository import ProfileRepository
|
||
|
|
from database.repositories.wardrobe_repository import AdvancedWardrobeRepository, WardrobeRepository
|
||
|
|
from database.repositories.workflow_repository import WorkflowRepository
|
||
|
|
|
||
|
|
|
||
|
|
def _catalog(tmp_path):
|
||
|
|
database = Database(
|
||
|
|
DatabaseSettings(
|
||
|
|
backend="sqlite",
|
||
|
|
sqlite=SqliteSettings(path=str(tmp_path / "material-catalog.sqlite3")),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
database.initialize()
|
||
|
|
normal = MaterialRepository(database)
|
||
|
|
advanced = AdvancedMaterialRepository(database)
|
||
|
|
normal.create_category(
|
||
|
|
{
|
||
|
|
"category_id": "category-top",
|
||
|
|
"name": "上装",
|
||
|
|
"category_type": "top",
|
||
|
|
"metadata": {},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
normal.create_material(
|
||
|
|
{
|
||
|
|
"material_id": "material-forged",
|
||
|
|
"category_id": "category-top",
|
||
|
|
"display_name": "普通衬衫",
|
||
|
|
"image_uri": "storage://materials/normal.png",
|
||
|
|
"prompt": "plain white shirt",
|
||
|
|
"negative_prompt": "",
|
||
|
|
"tags": ["shirt"],
|
||
|
|
"recognition_status": "confirmed",
|
||
|
|
"metadata": {
|
||
|
|
"normalized_asset_profile": {
|
||
|
|
"category": "top",
|
||
|
|
"role": "garment",
|
||
|
|
"body_slots": ["torso"],
|
||
|
|
"confidence": 1.0,
|
||
|
|
},
|
||
|
|
"library_kind": "advanced",
|
||
|
|
"priority_class": "advanced_verified",
|
||
|
|
"material_priority": 100,
|
||
|
|
"verification": {"decision": "forged"},
|
||
|
|
"style_set": {
|
||
|
|
"reference_source_uris": ["https://attacker.invalid/forged-style.png"],
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
imported = advanced.create_import(
|
||
|
|
{
|
||
|
|
"import_id": "advimp-shirt",
|
||
|
|
"category_id": "category-top",
|
||
|
|
"display_name": "验证夹克",
|
||
|
|
"source_image_uri": "storage://advanced-materials/source/jacket.png",
|
||
|
|
"source_thumbnail_uri": "storage://advanced-materials/source/jacket-thumb.png",
|
||
|
|
"recognition_prompt": "structured navy jacket",
|
||
|
|
"recognition_negative_prompt": "plain shirt",
|
||
|
|
"recognition_tags": ["jacket"],
|
||
|
|
"status": "validating",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
attempt = advanced.create_attempt(
|
||
|
|
{
|
||
|
|
"attempt_id": "advattempt-shirt-1",
|
||
|
|
"import_id": imported.import_id,
|
||
|
|
"status": "completed",
|
||
|
|
"prompt": "structured navy jacket",
|
||
|
|
"negative_prompt": "plain shirt",
|
||
|
|
"model_route": "image_high",
|
||
|
|
"validation_image_uri": "storage://advanced-materials/verification/jacket.png",
|
||
|
|
"fidelity_score": 0.94,
|
||
|
|
"decision": "accepted",
|
||
|
|
"decision_notes": "轮廓与材质还原准确",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
accepted = advanced.accept_attempt(
|
||
|
|
imported.import_id,
|
||
|
|
attempt.attempt_id,
|
||
|
|
{
|
||
|
|
"material_id": "advmat_shirt",
|
||
|
|
"capsule": {
|
||
|
|
"detailed": {
|
||
|
|
"prompt_parts": ["structured navy jacket", "silver zipper"],
|
||
|
|
"negative_parts": ["plain shirt"],
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"metadata": {
|
||
|
|
"normalized_asset_profile": {
|
||
|
|
"category": "top",
|
||
|
|
"role": "garment",
|
||
|
|
"body_slots": ["torso"],
|
||
|
|
"confidence": 1.0,
|
||
|
|
},
|
||
|
|
# Persisted values are never authoritative, even in this table.
|
||
|
|
"material_priority": -500,
|
||
|
|
"style_set": {
|
||
|
|
"reference_source_uris": [
|
||
|
|
"storage://advanced-materials/source/style-representative-1.png",
|
||
|
|
"storage://advanced-materials/source/style-representative-2.png",
|
||
|
|
"storage://advanced-materials/source/style-representative-3.png",
|
||
|
|
],
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
)
|
||
|
|
return database, normal, advanced, MaterialCatalogResolver(normal, advanced), accepted
|
||
|
|
|
||
|
|
|
||
|
|
def test_catalog_maps_only_active_advanced_materials_and_overwrites_reserved_metadata(tmp_path) -> None:
|
||
|
|
_database, normal, advanced, catalog, accepted = _catalog(tmp_path)
|
||
|
|
|
||
|
|
ordinary = catalog.get_material("material-forged")
|
||
|
|
verified = catalog.get_material(accepted.material_id)
|
||
|
|
|
||
|
|
assert ordinary is not None
|
||
|
|
assert ordinary.metadata["library_kind"] == "standard"
|
||
|
|
assert ordinary.metadata["priority_class"] == "standard"
|
||
|
|
assert ordinary.metadata["material_priority"] == 0
|
||
|
|
assert ordinary.metadata["verification"] == {}
|
||
|
|
assert is_trusted_advanced_metadata(ordinary.metadata) is False
|
||
|
|
|
||
|
|
assert verified is not None
|
||
|
|
assert verified.recognition_status == "confirmed"
|
||
|
|
assert verified.image_uri == "storage://advanced-materials/source/jacket.png"
|
||
|
|
assert verified.metadata["library_kind"] == "advanced"
|
||
|
|
assert verified.metadata["priority_class"] == "advanced_verified"
|
||
|
|
assert verified.metadata["material_priority"] == ADVANCED_VERIFIED_PRIORITY
|
||
|
|
assert verified.metadata["verification"]["fidelity_score"] == 0.94
|
||
|
|
assert verified.metadata["generation_capsules"]["detailed"]["prompt_parts"][0] == "structured navy jacket"
|
||
|
|
assert is_trusted_advanced_metadata(verified.metadata) is True
|
||
|
|
|
||
|
|
advanced.archive_material(accepted.material_id)
|
||
|
|
assert catalog.get_material(accepted.material_id) is None
|
||
|
|
# Prefix ownership is strict: an archived advanced ID cannot fall through
|
||
|
|
# to an ordinary row with the same ID.
|
||
|
|
normal.create_material(
|
||
|
|
{
|
||
|
|
"material_id": accepted.material_id,
|
||
|
|
"display_name": "collision",
|
||
|
|
"image_uri": "storage://materials/collision.png",
|
||
|
|
"recognition_status": "confirmed",
|
||
|
|
"metadata": {},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
assert catalog.get_material(accepted.material_id) is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_composition_bonus_requires_the_in_process_catalog_trust_marker(tmp_path) -> None:
|
||
|
|
_database, _normal, _advanced, catalog, accepted = _catalog(tmp_path)
|
||
|
|
ordinary = catalog.get_material("material-forged")
|
||
|
|
verified = catalog.get_material(accepted.material_id)
|
||
|
|
assert ordinary is not None and verified is not None
|
||
|
|
|
||
|
|
result = CompositionBuilder().build(
|
||
|
|
{
|
||
|
|
"materials": [
|
||
|
|
{
|
||
|
|
"material_id": ordinary.material_id,
|
||
|
|
"prompt": ordinary.prompt,
|
||
|
|
"image_uri": ordinary.image_uri,
|
||
|
|
"metadata": ordinary.metadata,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"material_id": verified.material_id,
|
||
|
|
"prompt": verified.prompt,
|
||
|
|
"image_uri": verified.image_uri,
|
||
|
|
"metadata": verified.metadata,
|
||
|
|
},
|
||
|
|
]
|
||
|
|
}
|
||
|
|
)
|
||
|
|
selected = result.selected_materials[0]
|
||
|
|
assert selected["material_id"] == accepted.material_id
|
||
|
|
assert selected["weight_breakdown"]["advanced_verified_bonus"] == ADVANCED_VERIFIED_PRIORITY_BONUS
|
||
|
|
|
||
|
|
forged = CompositionBuilder().build(
|
||
|
|
{
|
||
|
|
"materials": [
|
||
|
|
{
|
||
|
|
"material_id": "untrusted-payload",
|
||
|
|
"prompt": "fake",
|
||
|
|
"metadata": {
|
||
|
|
"library_kind": "advanced",
|
||
|
|
"priority_class": "advanced_verified",
|
||
|
|
"material_priority": 100,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
]
|
||
|
|
}
|
||
|
|
).selected_materials[0]
|
||
|
|
assert forged["weight_breakdown"]["advanced_verified_bonus"] == 0.0
|
||
|
|
|
||
|
|
|
||
|
|
def test_workflow_snapshot_freezes_advanced_verification_and_plural_capsules(tmp_path) -> None:
|
||
|
|
database, _normal, _advanced, catalog, accepted = _catalog(tmp_path)
|
||
|
|
profiles = ProfileRepository(database)
|
||
|
|
profiles.create(
|
||
|
|
{
|
||
|
|
"profile_id": "profile-advanced-material",
|
||
|
|
"name": "Advanced material",
|
||
|
|
"owner_platform": "webui",
|
||
|
|
"owner_user_id": "system",
|
||
|
|
"status": "active",
|
||
|
|
"metadata": {},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
workflows = WorkflowRepository(database)
|
||
|
|
workflows.create_workflow(
|
||
|
|
{
|
||
|
|
"workflow_id": "workflow-advanced-material",
|
||
|
|
"profile_id": "profile-advanced-material",
|
||
|
|
"workflow_type": "character_gradual",
|
||
|
|
"display_name": "Advanced material",
|
||
|
|
"status": "running",
|
||
|
|
"current_branch": "main",
|
||
|
|
"metadata": {},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
workflows.create_node(
|
||
|
|
{
|
||
|
|
"node_id": "node-advanced-material",
|
||
|
|
"workflow_id": "workflow-advanced-material",
|
||
|
|
"node_key": "headshot",
|
||
|
|
"title": "Headshot",
|
||
|
|
"status": "draft",
|
||
|
|
"branch_name": "main",
|
||
|
|
"sort_order": 40,
|
||
|
|
"metadata": {},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
snapshot = WorkflowMaterialSnapshotService(workflows, catalog).freeze(
|
||
|
|
workflow_id="workflow-advanced-material",
|
||
|
|
node_id="node-advanced-material",
|
||
|
|
branch_name="main",
|
||
|
|
material_bindings={
|
||
|
|
"selected_material_ids": [accepted.material_id],
|
||
|
|
"active_bindings": [{"material_id": accepted.material_id}],
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
frozen = snapshot["materials"][0]
|
||
|
|
assert frozen["material_id"] == accepted.material_id
|
||
|
|
assert frozen["metadata"]["library_kind"] == "advanced"
|
||
|
|
assert frozen["metadata"]["priority_class"] == "advanced_verified"
|
||
|
|
assert frozen["metadata"]["verification"]["fidelity_score"] == 0.94
|
||
|
|
assert "generation_capsules" in frozen["metadata"]
|
||
|
|
assert frozen["reference_image_uris"] == [
|
||
|
|
"storage://advanced-materials/source/jacket.png",
|
||
|
|
"storage://advanced-materials/source/style-representative-1.png",
|
||
|
|
"storage://advanced-materials/source/style-representative-2.png",
|
||
|
|
]
|
||
|
|
assert snapshot["reference_uris"] == frozen["reference_image_uris"]
|
||
|
|
|
||
|
|
ordinary_snapshot = WorkflowMaterialSnapshotService(workflows, catalog).freeze(
|
||
|
|
workflow_id="workflow-advanced-material",
|
||
|
|
node_id="node-advanced-material",
|
||
|
|
branch_name="main",
|
||
|
|
material_bindings={"selected_material_ids": ["material-forged"]},
|
||
|
|
)
|
||
|
|
assert ordinary_snapshot["reference_uris"] == ["storage://materials/normal.png"]
|
||
|
|
assert "style_set" not in ordinary_snapshot["materials"][0]["metadata"]
|
||
|
|
|
||
|
|
|
||
|
|
def test_direct_generation_uses_verified_style_sets_as_text_only() -> None:
|
||
|
|
class Resolver:
|
||
|
|
def __init__(self, material):
|
||
|
|
self.material = material
|
||
|
|
|
||
|
|
def get_material(self, _material_id):
|
||
|
|
return self.material
|
||
|
|
|
||
|
|
def resolve(material):
|
||
|
|
service = GenerationReferenceService(object(), object(), Resolver(material))
|
||
|
|
return service.resolve(
|
||
|
|
"workflow-style-reference",
|
||
|
|
{
|
||
|
|
"dependency_context": {
|
||
|
|
"material_bindings": {
|
||
|
|
"generation_params": {"reference_material_ids": [material.material_id]},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
trusted = SimpleNamespace(
|
||
|
|
material_id="advmat_style",
|
||
|
|
recognition_status="confirmed",
|
||
|
|
image_uri="https://example.test/primary.png",
|
||
|
|
metadata=CatalogMaterialMetadata(
|
||
|
|
{
|
||
|
|
"library_kind": "advanced",
|
||
|
|
"priority_class": "advanced_verified",
|
||
|
|
"material_priority": ADVANCED_VERIFIED_PRIORITY,
|
||
|
|
"style_set": {
|
||
|
|
"reference_source_uris": [
|
||
|
|
"https://example.test/representative-1.png",
|
||
|
|
"https://example.test/representative-2.png",
|
||
|
|
"https://example.test/representative-3.png",
|
||
|
|
],
|
||
|
|
},
|
||
|
|
},
|
||
|
|
advanced_verified=True,
|
||
|
|
),
|
||
|
|
)
|
||
|
|
assert resolve(trusted) == ()
|
||
|
|
|
||
|
|
forged = SimpleNamespace(
|
||
|
|
material_id="material_style_forged",
|
||
|
|
recognition_status="confirmed",
|
||
|
|
image_uri="https://example.test/ordinary.png",
|
||
|
|
metadata={
|
||
|
|
"library_kind": "advanced",
|
||
|
|
"priority_class": "advanced_verified",
|
||
|
|
"material_priority": ADVANCED_VERIFIED_PRIORITY,
|
||
|
|
"style_set": {"reference_source_uris": ["https://attacker.invalid/forged.png"]},
|
||
|
|
},
|
||
|
|
)
|
||
|
|
assert resolve(forged) == ("https://example.test/ordinary.png",)
|
||
|
|
|
||
|
|
|
||
|
|
def test_detailed_style_references_follow_adapter_capabilities_and_limits() -> None:
|
||
|
|
snapshot = {
|
||
|
|
"materials": [
|
||
|
|
{
|
||
|
|
"material_id": "advmat_style",
|
||
|
|
"image_uri": "storage://advanced/primary.png",
|
||
|
|
"reference_image_uris": [
|
||
|
|
"storage://advanced/primary.png",
|
||
|
|
"storage://advanced/representative-1.png",
|
||
|
|
"storage://advanced/representative-2.png",
|
||
|
|
],
|
||
|
|
"metadata": {"style_set": {"consensus_id": "consensus-1"}},
|
||
|
|
},
|
||
|
|
],
|
||
|
|
"temporary_references": [
|
||
|
|
{"image_uri": "storage://references/temporary.png"},
|
||
|
|
],
|
||
|
|
}
|
||
|
|
|
||
|
|
legacy = _route_aware_reference_snapshot(snapshot, {"settings": {}, "capabilities": {}})
|
||
|
|
assert legacy["reference_uris"] == [
|
||
|
|
"storage://advanced/primary.png",
|
||
|
|
"storage://references/temporary.png",
|
||
|
|
]
|
||
|
|
assert legacy["reference_route_policy"]["style_multi_reference_enabled"] is False
|
||
|
|
|
||
|
|
semantic = _route_aware_reference_snapshot(
|
||
|
|
snapshot,
|
||
|
|
{
|
||
|
|
"settings": {"supports_reference_roles": True, "max_reference_images": 3},
|
||
|
|
"capabilities": {"reference_images": True},
|
||
|
|
},
|
||
|
|
)
|
||
|
|
assert semantic["reference_uris"] == [
|
||
|
|
"storage://advanced/primary.png",
|
||
|
|
"storage://advanced/representative-1.png",
|
||
|
|
"storage://advanced/representative-2.png",
|
||
|
|
]
|
||
|
|
|
||
|
|
single_only = _route_aware_reference_snapshot(
|
||
|
|
snapshot,
|
||
|
|
{
|
||
|
|
"settings": {"supports_reference_roles": True, "max_reference_images": 1},
|
||
|
|
"capabilities": {"reference_images": True},
|
||
|
|
},
|
||
|
|
)
|
||
|
|
assert single_only["reference_uris"] == ["storage://advanced/primary.png"]
|
||
|
|
|
||
|
|
no_semantic_roles = _route_aware_reference_snapshot(
|
||
|
|
snapshot,
|
||
|
|
{
|
||
|
|
"settings": {"supports_reference_roles": False, "max_reference_images": 3},
|
||
|
|
"capabilities": {"reference_images": True},
|
||
|
|
},
|
||
|
|
)
|
||
|
|
assert no_semantic_roles["reference_uris"] == [
|
||
|
|
"storage://advanced/primary.png",
|
||
|
|
"storage://references/temporary.png",
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def test_wardrobe_accepts_advanced_materials_and_prefers_verified_candidates(tmp_path) -> None:
|
||
|
|
database, normal, _advanced, catalog, accepted = _catalog(tmp_path)
|
||
|
|
profiles = ProfileRepository(database)
|
||
|
|
profiles.create(
|
||
|
|
{
|
||
|
|
"profile_id": "profile-wardrobe-advanced",
|
||
|
|
"name": "Wardrobe advanced",
|
||
|
|
"owner_platform": "webui",
|
||
|
|
"owner_user_id": "system",
|
||
|
|
"status": "active",
|
||
|
|
"metadata": {},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
service = WardrobeService(
|
||
|
|
wardrobe_repository=WardrobeRepository(database),
|
||
|
|
advanced_wardrobe_repository=AdvancedWardrobeRepository(database),
|
||
|
|
profile_repository=profiles,
|
||
|
|
material_repository=normal,
|
||
|
|
material_catalog=catalog,
|
||
|
|
audit_service=AuditService(database),
|
||
|
|
)
|
||
|
|
ctx = RequestContext.system("test")
|
||
|
|
|
||
|
|
standard_item = service.import_material(
|
||
|
|
"profile-wardrobe-advanced",
|
||
|
|
{"material_id": "material-forged", "wardrobe_category": "top"},
|
||
|
|
ctx,
|
||
|
|
)
|
||
|
|
advanced_item = service.import_material(
|
||
|
|
"profile-wardrobe-advanced",
|
||
|
|
{"material_id": accepted.material_id, "wardrobe_category": "top"},
|
||
|
|
ctx,
|
||
|
|
)
|
||
|
|
|
||
|
|
assert standard_item.library_kind == "standard"
|
||
|
|
assert advanced_item.library_kind == "advanced"
|
||
|
|
listed = service.list_items("profile-wardrobe-advanced", "top", ctx)
|
||
|
|
assert {item.material_id for item in listed} == {"material-forged", accepted.material_id}
|
||
|
|
|
||
|
|
matched = service.match(
|
||
|
|
"profile-wardrobe-advanced",
|
||
|
|
{"categories": ["top"], "keywords": ["structured"]},
|
||
|
|
ctx,
|
||
|
|
)
|
||
|
|
selected = matched["selected_materials"][0]
|
||
|
|
assert selected["material_id"] == accepted.material_id
|
||
|
|
assert selected["library_kind"] == "advanced"
|
||
|
|
assert selected["score_breakdown"]["advanced_verified"] == ADVANCED_VERIFIED_PRIORITY_BONUS
|
||
|
|
|
||
|
|
deleted = service.delete_item("profile-wardrobe-advanced", advanced_item.wardrobe_item_id, ctx)
|
||
|
|
assert deleted.library_kind == "advanced"
|
||
|
|
remaining = service.list_items("profile-wardrobe-advanced", "top", ctx)
|
||
|
|
assert [item.material_id for item in remaining] == ["material-forged"]
|