from __future__ import annotations import json from pathlib import Path import pytest from backend.dto.common import ApiError from backend.route_registry import ROUTES from backend.services.generation_cross_review_service import GenerationCrossReviewPlanner from backend.services.model_provider_service import ModelProviderService from backend.services.workflow_generation_cross_review_service import WorkflowGenerationCrossReviewService from backend.services.workflow_round_service import WorkflowRoundService from core.audit_service import AuditService from core.identity import RequestContext from database.db_helper import Database, DatabaseSettings, SqliteSettings from database.repositories.image_repository import ImageRepository from database.repositories.job_repository import JobRepository from database.repositories.model_provider_repository import ModelProviderRepository from database.repositories.workflow_cross_review_repository import WorkflowCrossReviewRepository from database.repositories.workflow_repository import WorkflowRepository from database.repositories.workflow_round_repository import WorkflowRoundRepository def _fixture(tmp_path: Path): database = Database( DatabaseSettings( backend="sqlite", sqlite=SqliteSettings(path=str(tmp_path / "workflow-cross-review.sqlite3")), ) ) database.initialize() conn = database.connect() try: conn.execute( """ INSERT INTO character_profiles ( profile_id, name, owner_platform, owner_user_id, status, metadata_json ) VALUES (?, ?, ?, ?, ?, ?) """, ("profile-review", "Review", "webui", "system", "active", "{}"), ) conn.execute( """ INSERT INTO profile_workflows ( workflow_id, profile_id, workflow_type, display_name, status, current_node_id, current_branch, metadata_json ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) """, ( "workflow-review", "profile-review", "character_gradual", "Review workflow", "running", "node-headshot", "main", json.dumps( { "character_execution": { "executor": "character_workflow", "import_confirmed": True, "progression_version": "character_gradual_sequential_v1", "completed_node_ids": [], "current_node_id": "node-headshot", "current_node_key": "headshot", "all_completed": False, } } ), ), ) for node_id, node_key, title, order, route in ( ("node-headshot", "headshot", "头像", 40, "route-primary"), ("node-upper", "upper_body", "上半身像", 50, "route-primary"), ("node-restoration", "style_lock", "高清修复", 100, "route-restoration"), ("node-card", "profile_card", "档案卡", 110, None), ): conn.execute( """ INSERT INTO workflow_nodes ( node_id, workflow_id, node_key, title, node_type, status, branch_name, prompt, negative_prompt, model_route, sort_order, metadata_json ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( node_id, "workflow-review", node_key, title, "archive" if node_key == "profile_card" else "generation", "draft", "main", "one stable character portrait", "identity drift", route, order, json.dumps({"stage_code": "TX" if node_key == "headshot" else None}), ), ) conn.execute( """ INSERT INTO generation_images ( image_id, profile_id, workflow_id, node_id, image_uri, thumbnail_uri, positive_prompt, negative_prompt, model_used, model_route, width, height, is_selected, is_archived, metadata_json ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( "image-primary", "profile-review", "workflow-review", "node-headshot", "storage://outputs/primary.png", "storage://thumbnails/primary.png", "one stable character portrait", "identity drift", "image-alpha", "route-primary", 1024, 1024, 0, 0, "{}", ), ) conn.commit() finally: conn.close() workflows = WorkflowRepository(database) rounds_repository = WorkflowRoundRepository(database) source_round, _ = rounds_repository.create_round_with_candidates( { "round_id": "round-primary", "workflow_id": "workflow-review", "node_id": "node-headshot", "branch_name": "main", "round_kind": "primary", "display_prefix": "TX", "status": "completed", "requested_count": 1, "prompt_snapshot": { "positive_prompt": "one stable character portrait", "negative_prompt": "identity drift", "model_route": "route-primary", }, "reference_snapshot": {}, "metadata": {}, }, [ { "candidate_id": "candidate-primary", "candidate_index": 1, "generation_status": "succeeded", "image_id": "image-primary", "model_route": "route-primary", "provider_id": "provider-a", "provider_model": "image-alpha", "prompt_snapshot": { "positive_prompt": "one stable character portrait", "negative_prompt": "identity drift", "model_route": "route-primary", }, } ], ) providers = ModelProviderService(ModelProviderRepository(database)) for provider_id in ("provider-a", "provider-b"): providers.upsert_provider( { "provider_id": provider_id, "display_name": provider_id, "provider_type": "openai_compatible", "base_url": f"https://{provider_id}.example.com/v1", "enabled": True, } ) for route_id, route_name, provider_id, model_id in ( ("route-id-primary", "route-primary", "provider-a", "image-alpha"), ("route-id-secondary", "route-secondary", "provider-b", "image-beta"), ("route-id-restoration", "route-restoration", "provider-a", "image-restoration"), ): providers.upsert_route( { "route_id": route_id, "route_name": route_name, "provider_id": provider_id, "model_id": model_id, "modality": "image_generation", "enabled": True, } ) audit = AuditService(database) round_service = WorkflowRoundService( round_repository=rounds_repository, workflow_repository=workflows, image_repository=ImageRepository(database), job_repository=JobRepository(database), audit_service=audit, ) service = WorkflowGenerationCrossReviewService( cross_review_repository=WorkflowCrossReviewRepository(database), round_repository=rounds_repository, workflow_repository=workflows, round_service=round_service, planner=GenerationCrossReviewPlanner(providers), audit_service=audit, config={"model_assignments": {"generation_cross_review_routes": ["route-secondary"]}}, ) return database, service, round_service, rounds_repository, source_round def _finish_review_round(database: Database, rounds: WorkflowRoundRepository, round_id: str) -> tuple[str, str]: planned = next(item for item in rounds.list_candidates(round_id) if item.generation_status == "planned") image_id = f"image-{planned.candidate_id}" conn = database.connect() try: conn.execute( """ INSERT INTO generation_images ( image_id, profile_id, workflow_id, node_id, image_uri, thumbnail_uri, positive_prompt, negative_prompt, model_used, model_route, width, height, is_selected, is_archived, metadata_json ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( image_id, "profile-review", "workflow-review", "node-headshot", f"storage://outputs/{image_id}.png", f"storage://thumbnails/{image_id}.png", "reviewed portrait", "identity drift", planned.provider_model, planned.model_route, 1024, 1024, 0, 0, "{}", ), ) conn.commit() finally: conn.close() rounds.update_candidate(planned.candidate_id, {"generation_status": "succeeded", "image_id": image_id}) rounds.update_round(round_id, {"status": "completed"}) return planned.candidate_id, image_id def test_generation_cross_review_routes_are_distinct_from_visual_quality_review() -> None: routes = {(item.method, item.endpoint, item.handler) for item in ROUTES} assert ( "POST", "rounds/{round_id}/generation-cross-review", "backend.routes.generation_cross_review_routes.decide_generation_cross_review", ) in routes assert ( "POST", "rounds/{round_id}/generation-cross-review/regenerate", "backend.routes.generation_cross_review_routes.regenerate_generation_cross_review", ) in routes assert ( "POST", "rounds/{round_id}/generation-cross-review/approve", "backend.routes.generation_cross_review_routes.approve_generation_cross_review", ) in routes assert ( "GET", "nodes/{node_id}/review", "backend.routes.review_routes.review_node", ) in routes def test_review_decision_creates_model_diverse_pool_and_history(tmp_path: Path) -> None: database, service, _round_service, rounds, _source = _fixture(tmp_path) result = service.decide( "round-primary", { "decision": "review", "candidate_id": "candidate-primary", "idempotency_key": "review-create-1", }, RequestContext.system("cross-review-test"), ) detail = result["round"] assert detail["round_kind"] == "cross_review" assert detail["display_code"] == "TXSC-01" candidates = result["candidates"] assert candidates[0]["candidate"]["image_id"] == "image-primary" assert candidates[0]["candidate"]["metadata"]["review_role"] == "primary_preserved" assert candidates[1]["candidate"]["model_route"] == "route-secondary" assert candidates[1]["candidate"]["metadata"]["provider_label"] == "provider-b" assert candidates[1]["candidate"]["metadata"]["model_label"] == "image-beta" conn = database.connect() try: actions = [row["action"] for row in conn.execute("SELECT action FROM workflow_history").fetchall()] finally: conn.close() assert "generation_cross_review_requested" in actions assert rounds.get_round("round-primary").metadata["generation_cross_review"]["active_round_id"] == detail["round_id"] def test_followup_preserves_unselected_candidates_and_regenerates_only_selected(tmp_path: Path) -> None: database, service, _round_service, rounds, _source = _fixture(tmp_path) first = service.decide( "round-primary", {"decision": "review", "candidate_id": "candidate-primary", "idempotency_key": "review-first"}, RequestContext.system(), ) first_round_id = first["round"]["round_id"] secondary_id, _secondary_image = _finish_review_round(database, rounds, first_round_id) second = service.regenerate( first_round_id, {"candidate_ids": [secondary_id], "idempotency_key": "review-second"}, RequestContext.system(), ) candidates = sorted(second["candidates"], key=lambda item: item["display_index"]) assert candidates[0]["candidate"]["generation_status"] == "succeeded" assert candidates[0]["candidate"]["image_id"] == "image-primary" assert candidates[1]["candidate"]["generation_status"] == "planned" assert candidates[1]["candidate"]["image_id"] is None assert candidates[1]["candidate"]["source_candidate_id"] == secondary_id assert rounds.get_round(first_round_id).status == "superseded" def test_approval_atomically_updates_selection_progression_and_restoration_library(tmp_path: Path) -> None: database, service, _round_service, rounds, _source = _fixture(tmp_path) review = service.decide( "round-primary", {"decision": "review", "candidate_id": "candidate-primary", "idempotency_key": "review-approve"}, RequestContext.system(), ) review_round_id = review["round"]["round_id"] candidate_id, image_id = _finish_review_round(database, rounds, review_round_id) approved = service.approve( review_round_id, {"candidate_id": candidate_id, "idempotency_key": "review-approve-final"}, RequestContext.system(), ) assert approved["approval"]["image_id"] == image_id assert approved["approval"]["restoration_node_id"] == "node-restoration" assert approved["approval"]["restoration_display_code"] == "XF-01" restoration_round = rounds.get_round(approved["approval"]["restoration_round_id"]) assert restoration_round is not None assert restoration_round.node_id == "node-restoration" assert restoration_round.round_kind == "restoration" assert restoration_round.status == "completed" assert restoration_round.metadata["read_only"] is True restoration_candidates = rounds.list_candidates(restoration_round.round_id) assert len(restoration_candidates) == 1 assert restoration_candidates[0].image_id == image_id assert restoration_candidates[0].metadata["review_role"] == "restoration_input" conn = database.connect() try: node = conn.execute("SELECT status, selected_image_id FROM workflow_nodes WHERE node_id = ?", ("node-headshot",)).fetchone() workflow = conn.execute("SELECT current_node_id, metadata_json FROM profile_workflows WHERE workflow_id = ?", ("workflow-review",)).fetchone() selected = conn.execute("SELECT image_id FROM generation_images WHERE node_id = ? AND is_selected = 1", ("node-headshot",)).fetchall() actions = [row["action"] for row in conn.execute("SELECT action FROM workflow_history").fetchall()] finally: conn.close() assert node["status"] == "selected" assert node["selected_image_id"] == image_id assert [row["image_id"] for row in selected] == [image_id] assert workflow["current_node_id"] == "node-upper" metadata = json.loads(workflow["metadata_json"]) state = metadata["branch_state"]["main"] assert state["stage_inputs"]["node-upper"]["image_id"] == image_id assert state["stage_inputs"]["node-restoration"]["role"] == "restoration_library" restoration_input = state["restoration_inputs"]["node-restoration"]["items_by_source_node"]["node-headshot"] assert restoration_input["image_id"] == image_id assert restoration_input["restoration_round_id"] == restoration_round.round_id assert restoration_input["restoration_display_code"] == "XF-01" assert "confirm_generation_cross_review_candidate" in actions def test_review_owner_chain_rejects_another_profile_user(tmp_path: Path) -> None: database, service, _round_service, rounds, _source = _fixture(tmp_path) intruder = RequestContext.from_astrbot( username="intruder", user_id="intruder", permissions={"can_write"}, ) with pytest.raises(ApiError) as captured: service.decide( "round-primary", {"decision": "review", "candidate_id": "candidate-primary"}, intruder, ) assert captured.value.error_code.value == "RESOURCE_NOT_FOUND" review = service.decide( "round-primary", {"decision": "review", "candidate_id": "candidate-primary"}, RequestContext.system(), ) review_round_id = review["round"]["round_id"] candidate_id, _image_id = _finish_review_round(database, rounds, review_round_id) for operation in ( lambda: service.regenerate( review_round_id, {"candidate_ids": [candidate_id]}, intruder, ), lambda: service.approve( review_round_id, {"candidate_id": candidate_id}, intruder, ), ): with pytest.raises(ApiError) as denied: operation() assert denied.value.error_code.value == "RESOURCE_NOT_FOUND" def test_review_approval_rolls_back_restoration_round_with_history_failure( tmp_path: Path, ) -> None: database, service, _round_service, rounds, _source = _fixture(tmp_path) review = service.decide( "round-primary", {"decision": "review", "candidate_id": "candidate-primary"}, RequestContext.system(), ) review_round_id = review["round"]["round_id"] candidate_id, _image_id = _finish_review_round(database, rounds, review_round_id) conn = database.connect() try: conn.execute( """ CREATE TRIGGER fail_cross_review_confirmation_history BEFORE INSERT ON workflow_history WHEN NEW.action = 'confirm_generation_cross_review_candidate' BEGIN SELECT RAISE(ABORT, 'simulated history failure'); END """ ) conn.commit() finally: conn.close() with pytest.raises(Exception): service.approve( review_round_id, {"candidate_id": candidate_id}, RequestContext.system(), ) assert rounds.get_round(review_round_id).status == "completed" assert rounds.list_rounds( node_id="node-restoration", branch_name="main", round_kind="restoration", ) == [] node = service.workflow_repository.get_node("node-headshot") assert node is not None assert node.status == "draft" assert node.selected_image_id is None def test_skip_path_records_decision_history_and_uses_normal_confirmation(tmp_path: Path) -> None: database, service, _round_service, rounds, _source = _fixture(tmp_path) result = service.decide( "round-primary", {"decision": "skip", "candidate_id": "candidate-primary", "idempotency_key": "review-skip"}, RequestContext.system(), ) assert result["round"]["status"] == "confirmed" assert result["review_decision"]["decision"] == "skip" handoff = result["review_decision"]["restoration_handoff"] assert handoff["display_code"] == "XF-01" restoration_round = rounds.get_round(handoff["round_id"]) assert restoration_round is not None assert restoration_round.round_kind == "restoration" assert restoration_round.metadata["restoration_input_handoff"] is True replay = service.decide( "round-primary", { "decision": "skip", "candidate_id": "candidate-primary", "idempotency_key": "review-skip", }, RequestContext.system(), ) assert replay["review_decision"]["restoration_handoff"]["idempotent_replay"] is True assert len( rounds.list_rounds( node_id="node-restoration", branch_name="main", round_kind="restoration", ) ) == 1 conn = database.connect() try: actions = [row["action"] for row in conn.execute("SELECT action FROM workflow_history ORDER BY created_at").fetchall()] finally: conn.close() assert "generation_cross_review_skipped" in actions assert "confirm_round_candidate" in actions assert "generation_cross_review_skip_handoff" in actions def test_review_can_start_immediately_after_primary_confirmation_but_not_after_downstream_completion(tmp_path: Path) -> None: _database, service, round_service, _rounds, _source = _fixture(tmp_path) ctx = RequestContext.system() round_service.confirm_candidate( "round-primary", {"candidate_id": "candidate-primary", "idempotency_key": "confirm-before-review"}, ctx, ) created = service.decide( "round-primary", {"decision": "review", "candidate_id": "candidate-primary", "idempotency_key": "review-after-confirm"}, ctx, ) assert created["round"]["round_kind"] == "cross_review" workflow_repository = service.workflow_repository workflow_repository.update_node("node-upper", {"status": "selected", "selected_image_id": "image-primary"}) with pytest.raises(ApiError) as captured: service.decide( "round-primary", {"decision": "review", "candidate_id": "candidate-primary", "idempotency_key": "review-too-late"}, ctx, ) assert captured.value.details["required_action"] in {"open_active_cross_review", "create_branch"}