from __future__ import annotations import json from pathlib import Path import threading import time from typing import Any import pytest from backend.dto.common import ApiError, ErrorCode from backend.services.generation_service import GenerationService from backend.services.image_service import ImageService from backend.services.queue_service import QueueService from backend.services.workflow_material_snapshot_service import WorkflowMaterialSnapshotService from backend.services.workflow_round_feedback_service import WorkflowRoundFeedbackService from backend.services.workflow_round_service import WorkflowRoundService from backend.services.workflow_round_worker_runtime import WorkflowRoundWorkerRuntime from backend.services.workflow_service import WorkflowService 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.material_repository import MaterialRepository from database.repositories.profile_repository import ProfileRepository from database.repositories.workflow_repository import WorkflowRepository from database.repositories.workflow_round_repository import WorkflowRoundRepository from engines.single_flow import SingleFlowEngine class _TempStorage: def __init__(self, root: Path): self.root = root def resolve_uri(self, uri: str) -> Path: assert uri.startswith("storage://") storage_root, relative = uri[len("storage://") :].split("/", 1) return self.root / storage_root / relative def ensure_file(self, uri: str) -> Path: path = self.resolve_uri(uri) if not path.is_file(): raise FileNotFoundError(path) return path class _CountingPromptExpansion: def __init__(self) -> None: self.calls = 0 def expand_for_node(self, _profile, node, payload: dict[str, Any]) -> dict[str, Any]: self.calls += 1 return { "prompt": str(payload.get("prompt") or node.prompt or "portrait") + ", expanded once", "negative_prompt": str(payload.get("negative_prompt") or node.negative_prompt or ""), } def _database(tmp_path: Path) -> Database: database = Database( DatabaseSettings( backend="sqlite", sqlite=SqliteSettings(path=str(tmp_path / "workflow-round-runtime.sqlite3")), ) ) database.initialize() return database def _fixture(tmp_path: Path): database = _database(tmp_path) profiles = ProfileRepository(database) profiles.create( { "profile_id": "profile-round-runtime", "name": "Round runtime", "owner_platform": "webui", "owner_user_id": "system", "status": "active", "metadata": {}, } ) workflows = WorkflowRepository(database) workflows.create_workflow( { "workflow_id": "workflow-round-runtime", "profile_id": "profile-round-runtime", "workflow_type": "character_gradual", "display_name": "Round runtime", "status": "running", "current_node_id": "node-round-runtime", "current_branch": "main", "metadata": { "character_execution": { "executor": "character_workflow", "import_confirmed": True, "progression_version": "character_gradual_sequential_v1", } }, } ) workflows.create_node( { "node_id": "node-round-runtime", "workflow_id": "workflow-round-runtime", "node_key": "headshot", "title": "头像", "node_type": "generation", "status": "draft", "branch_name": "main", "prompt": "one stable character portrait", "negative_prompt": "identity drift", "model_route": "portrait-route", "sort_order": 40, "metadata": {}, } ) workflows.create_node( { "node_id": "node-round-runtime-next", "workflow_id": "workflow-round-runtime", "node_key": "upper_body", "title": "上半身", "node_type": "generation", "status": "draft", "branch_name": "main", "prompt": "same character upper body", "negative_prompt": "identity drift", "model_route": "portrait-route", "sort_order": 50, "metadata": {}, } ) audit = AuditService(database) workflow_service = WorkflowService(workflows, profiles, audit) queue = QueueService(JobRepository(database), audit) image_service = ImageService(ImageRepository(database), workflows, audit) expansion = _CountingPromptExpansion() materials = MaterialRepository(database) generation = GenerationService( workflow_service=workflow_service, queue_service=queue, image_service=image_service, single_flow_engine=SingleFlowEngine( storage=_TempStorage(tmp_path / "storage"), allow_mock_fallback=True, ), prompt_expansion_service=expansion, material_resolver=materials, material_snapshot_service=WorkflowMaterialSnapshotService(workflows, materials), require_resolved_model_route=False, ) rounds = WorkflowRoundService( WorkflowRoundRepository(database), workflows, image_service.image_repository, queue.repository, audit, generation_service=generation, ) return database, generation, rounds, expansion def _wait_for_round(rounds: WorkflowRoundService, round_id: str, statuses: set[str], timeout: float = 5.0): deadline = time.monotonic() + timeout while time.monotonic() < deadline: detail = rounds.get_round(round_id).to_dict() if detail["round"]["status"] in statuses: return detail time.sleep(0.02) raise AssertionError(f"round {round_id} did not reach {sorted(statuses)}") def _complete_round( generation: GenerationService, rounds: WorkflowRoundService, *, count: int = 4, idempotency_key: str, ): created = rounds.create_round( "node-round-runtime", {"count": count, "idempotency_key": idempotency_key}, RequestContext.system("round-runtime-test"), ) runtime = WorkflowRoundWorkerRuntime(generation, rounds, worker_count=count, interval_seconds=0.01) runtime.start() try: return _wait_for_round(rounds, created["round"]["round_id"], {"completed"}) finally: runtime.stop() def test_chat_terminal_notification_is_durable_and_replayed_after_reload(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) created = rounds.create_round( "node-round-runtime", { "count": 4, "idempotency_key": "runtime-chat-notification", "chat_notification": { "notify_on_terminal": True, "reply_target": { "platform": "qq", "message_origin": "qq:group:100", "message_id": "message-1", "user_id": "user-1", "group_id": "100", }, }, }, RequestContext.from_astrbot( username="system", user_id="system", source="astrbot_chat:qq", platform="qq", session_key="qq:group:100", permissions={"*"}, ), ) received: list[dict[str, Any]] = [] runtime = WorkflowRoundWorkerRuntime( generation, rounds, worker_count=4, interval_seconds=0.01, terminal_notification_callback=received.append, ) runtime.start() try: _wait_for_round(rounds, created["round"]["round_id"], {"completed"}) deadline = time.monotonic() + 2 while not received and time.monotonic() < deadline: time.sleep(0.02) finally: runtime.stop() assert len(received) == 1 assert received[0]["reply_target"]["message_origin"] == "qq:group:100" assert len(received[0]["candidates"]) == 4 # Simulate a reload after the callback was scheduled but before AstrBot # acknowledged send_message. A new runtime must recover the pending row. reloaded = WorkflowRoundWorkerRuntime(generation, rounds, worker_count=1) pending = reloaded.pending_terminal_notifications() assert [item["round_id"] for item in pending] == [created["round"]["round_id"]] reloaded.mark_terminal_notification(created["round"]["round_id"], delivered=True) second_reload = WorkflowRoundWorkerRuntime(generation, rounds, worker_count=1) assert second_reload.pending_terminal_notifications() == [] def test_round_prepare_runs_once_and_returns_independent_queued_tasks(tmp_path: Path) -> None: _database_handle, generation, rounds, expansion = _fixture(tmp_path) ctx = RequestContext.system("round-runtime-test") created = rounds.create_round( "node-round-runtime", {"count": 4, "idempotency_key": "runtime-round-1", "prompt": "silver-haired knight"}, ctx, ) assert created["round"]["status"] == "queued" assert expansion.calls == 1 tasks = [item["task"] for item in created["candidates"]] assert len({task["task_id"] for task in tasks}) == 4 assert all(task["status"] == "queued" for task in tasks) assert all(task["request"]["count"] == 1 for task in tasks) assert all(task["metadata"]["source"] == "workflow_round" for task in tasks) assert [task["request"]["output_scope"]["candidate_display_code"] for task in tasks] == [ "TX-01-01", "TX-01-02", "TX-01-03", "TX-01-04", ] assert generation.image_service.list_by_node("node-round-runtime") == [] replay = rounds.create_round( "node-round-runtime", {"count": 5, "idempotency_key": "runtime-round-1", "prompt": "must not replan"}, ctx, ) assert replay["idempotent_replay"] is True assert expansion.calls == 1 assert [item["task"]["task_id"] for item in replay["candidates"]] == [ task["task_id"] for task in tasks ] def test_four_candidates_execute_concurrently_with_isolated_outputs(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) created = rounds.create_round( "node-round-runtime", {"count": 4, "idempotency_key": "runtime-round-concurrent"}, RequestContext.system("round-runtime-test"), ) active = 0 max_active = 0 guard = threading.Lock() barrier = threading.Barrier(4) original_execute = generation.execute_claimed_generation def tracked_execute(task, ctx): nonlocal active, max_active with guard: active += 1 max_active = max(max_active, active) try: barrier.wait(timeout=2.0) return original_execute(task, ctx) finally: with guard: active -= 1 generation.execute_claimed_generation = tracked_execute # type: ignore[method-assign] runtime = WorkflowRoundWorkerRuntime(generation, rounds, worker_count=4, interval_seconds=0.01) runtime.start() try: detail = _wait_for_round(rounds, created["round"]["round_id"], {"completed"}) finally: runtime.stop() assert max_active == 4 assert all(item["candidate"]["generation_status"] == "succeeded" for item in detail["candidates"]) assert len({item["candidate"]["image_id"] for item in detail["candidates"]}) == 4 assert all(item["image"]["is_selected"] is False for item in detail["candidates"]) assert all("workflows/workflow-round-runtime/main/headshot" in item["image"]["image_uri"] for item in detail["candidates"]) assert generation.workflow_service.get_node("node-round-runtime").status == "completed" def test_one_candidate_timeout_produces_partial_success_without_poisoning_siblings(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) created = rounds.create_round( "node-round-runtime", {"count": 4, "idempotency_key": "runtime-round-partial"}, RequestContext.system("round-runtime-test"), ) original_execute = generation.execute_claimed_generation def sometimes_timeout(task, ctx): if int(task.metadata.get("candidate_index") or 0) == 2: raise TimeoutError("candidate-specific timeout") return original_execute(task, ctx) generation.execute_claimed_generation = sometimes_timeout # type: ignore[method-assign] runtime = WorkflowRoundWorkerRuntime(generation, rounds, worker_count=4, interval_seconds=0.01) runtime.start() try: detail = _wait_for_round(rounds, created["round"]["round_id"], {"partial_success"}) finally: runtime.stop() statuses = [item["candidate"]["generation_status"] for item in detail["candidates"]] assert statuses == ["succeeded", "failed", "succeeded", "succeeded"] assert detail["candidates"][1]["candidate"]["failure"]["error_type"] == "TimeoutError" assert generation.workflow_service.get_node("node-round-runtime").status == "completed" def test_startup_recovery_requeues_only_stale_round_tasks(tmp_path: Path) -> None: database, generation, rounds, _expansion = _fixture(tmp_path) created = rounds.create_round( "node-round-runtime", {"count": 2, "idempotency_key": "runtime-round-recovery"}, RequestContext.system("round-runtime-test"), ) task_ids = [item["task"]["task_id"] for item in created["candidates"]] generation.queue_service.repository.update(task_ids[0], {"status": "running"}) generation.queue_service.repository.update(task_ids[1], {"status": "retrying"}) unrelated = generation.queue_service.repository.create( { "task_id": "unrelated-running", "status": "running", "request": {}, "metadata": {"source": "nodes.generate"}, } ) conn = database.connect() try: conn.execute( "UPDATE generation_tasks SET updated_at = ? WHERE task_id IN (?, ?, ?)", ("2020-01-01 00:00:00", task_ids[0], task_ids[1], unrelated.task_id), ) conn.commit() finally: conn.close() recovery = generation.queue_service.recover_stale_tasks( RequestContext.system("round-runtime-recovery"), stale_after_seconds=1, ) assert set(recovery["recovered_task_ids"]) == set(task_ids) assert all(generation.queue_service.get_job(task_id).status == "queued" for task_id in task_ids) assert generation.queue_service.get_job(unrelated.task_id).status == "running" assert all( generation.queue_service.get_job(task_id).metadata["recovery"]["previous_status"] in {"running", "retrying"} for task_id in task_ids ) def test_persisted_round_route_is_secret_free_and_runtime_detects_route_drift(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) def original_route(_route_name: str) -> dict[str, Any]: return { "route_name": "portrait-route", "route_id": "route-portrait", "status": "resolved", "provider_id": "provider-a", "provider_type": "openai_compatible", "model_id": "model-a", "modality": "image_generation", "settings": { "extra_headers": {"Authorization": "Bearer must-not-persist"}, "api_key": "must-not-persist", }, } generation._resolve_model_route = original_route # type: ignore[method-assign] created = rounds.create_round( "node-round-runtime", {"count": 1, "idempotency_key": "runtime-round-secret-safe"}, RequestContext.system("round-runtime-test"), ) persisted = json.dumps(created, ensure_ascii=False, sort_keys=True) assert "must-not-persist" not in persisted assert "extra_headers" not in persisted assert '"api_key"' not in persisted generation._resolve_model_route = lambda _route_name: { # type: ignore[method-assign] **original_route("portrait-route"), "model_id": "model-b", "settings": {}, } runtime = WorkflowRoundWorkerRuntime(generation, rounds, worker_count=1, interval_seconds=0.01) runtime.start() try: detail = _wait_for_round(rounds, created["round"]["round_id"], {"all_failed"}) finally: runtime.stop() assert detail["candidates"][0]["candidate"]["generation_status"] == "failed" assert "model route changed" in detail["candidates"][0]["candidate"]["failure"]["message"] def test_feedback_persists_strict_rejection_and_limits_liked_candidates(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=4, idempotency_key="round-feedback-persist", ) round_id = detail["round"]["round_id"] candidates = [item["candidate_id"] for item in detail["candidates"]] assert all(item["is_final"] is False for item in detail["candidates"]) assert all(item["image"]["is_selected"] is False for item in detail["candidates"]) updated = rounds.update_feedback( round_id, { "updates": [ {"candidate_id": candidates[0], "preference_status": "liked", "reason": "五官最好"}, {"candidate_id": candidates[1], "preference_status": "liked"}, { "candidate_id": candidates[2], "preference_status": "strict_rejected", "reason": "身份偏差严重", }, ] }, RequestContext.system("feedback-test"), ) by_id = {item["candidate_id"]: item for item in updated["candidates"]} assert by_id[candidates[0]]["preference_status"] == "liked" assert by_id[candidates[0]]["preference_reason"] == "五官最好" assert by_id[candidates[2]]["preference_status"] == "strict_rejected" assert rounds.get_round(round_id).to_dict()["round"]["feedback_snapshot"][candidates[2]][ "preference_status" ] == "strict_rejected" assert all(item["image"]["is_selected"] is False for item in updated["candidates"]) with pytest.raises(ApiError) as too_many: rounds.update_feedback( round_id, { "updates": [ {"candidate_id": candidates[2], "preference_status": "liked"}, {"candidate_id": candidates[3], "preference_status": "liked"}, ] }, RequestContext.system("feedback-test"), ) assert too_many.value.details["maximum"] == 3 with pytest.raises(ApiError) as rejected_confirm: rounds.confirm_candidate( round_id, {"candidate_id": candidates[2]}, RequestContext.system("feedback-test"), ) assert rejected_confirm.value.details["reason"] == "candidate_rejected" def test_conversational_feedback_and_next_round_share_safe_persisted_context(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=4, idempotency_key="round-conversation-parent", ) round_id = detail["round"]["round_id"] captured: list[dict[str, Any]] = [] def complete(payload: dict[str, Any]) -> dict[str, Any]: captured.append(payload) return { "feedback": { "liked": ["TX-01-01", "TX-01-02"], "strict_rejected": ["TX-01-03"], }, "positive_patch": "keep the masculine face and short silver hair", "negative_patch": "avoid the oversized shoulder ornament", "material_intent": "matte black wool coat", } rounds.feedback_service = WorkflowRoundFeedbackService( structured_text_callable=complete, model_route="prompt-planning-test", ) updated = rounds.update_feedback( round_id, { "feedback_text": "1、2喜欢,3严格拒绝;下一轮统一短银发;素材:黑色羊毛外套", "idempotency_key": "feedback-conversation-1", }, RequestContext.system("feedback-conversation"), ) assert len(captured) == 1 serialized_request = json.dumps(captured[0], ensure_ascii=False) assert all( item["candidate_id"] not in serialized_request for item in detail["candidates"] ) assert "candidate_id" not in serialized_request assert "expanded once" in captured[0]["candidate_whitelist"][0]["positive_prompt"] conversation = updated["round"]["feedback_snapshot"]["_conversation"] assert conversation["source_text"].startswith("1、2喜欢") assert conversation["positive_patch"] == "keep the masculine face and short silver hair" assert conversation["negative_patch"] == "avoid the oversized shoulder ornament" assert updated["interpretation"]["source"] == "llm_validated" replay = rounds.update_feedback( round_id, { "feedback_text": "1、2喜欢,3严格拒绝;下一轮统一短银发;素材:黑色羊毛外套", "idempotency_key": "feedback-conversation-1", }, RequestContext.system("feedback-conversation"), ) assert replay["feedback_operation"]["idempotent_replay"] is True assert len(captured) == 1 child = rounds.create_next_round( round_id, {"count": 4, "idempotency_key": "next-round-from-feedback-1"}, RequestContext.system("feedback-conversation"), ) assert child["round"]["parent_round_id"] == round_id assert child["feedback_context"]["source_text"].startswith("1、2喜欢") assert child["feedback_context"]["feedback_summary"].startswith("喜欢 TX-01-01、TX-01-02") assert child["feedback_context"]["strict_rejected"][0]["display_code"] == "TX-01-03" prepared = child["round"]["metadata"]["prepared_generation"] request = prepared["generation_request"] liked_image_ids = [ item["candidate"]["image_id"] for item in updated["candidates"] if item["preference_status"] == "liked" ] assert request["selected_draft_image_ids"] == liked_image_ids assert request["negative_prompt_patch"] == "avoid the oversized shoulder ornament" assert "TX-01-03" not in request["negative_prompt_patch"] assert [ item["candidate"]["source_candidate_id"] for item in child["candidates"] ] == [updated["candidates"][0]["candidate_id"]] * 4 child_replay = rounds.create_next_round( round_id, {"count": 4, "idempotency_key": "next-round-from-feedback-1"}, RequestContext.system("feedback-conversation"), ) assert child_replay["round"]["round_id"] == child["round"]["round_id"] assert child_replay["idempotent_replay"] is True def test_feedback_transaction_rolls_back_pool_snapshot_and_history_together(tmp_path: Path) -> None: database, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=2, idempotency_key="round-feedback-rollback", ) round_id = detail["round"]["round_id"] candidate_id = detail["candidates"][0]["candidate_id"] conn = database.connect() try: conn.execute( """ CREATE TRIGGER fail_round_feedback_history BEFORE INSERT ON workflow_history WHEN NEW.action = 'round_feedback_updated' BEGIN SELECT RAISE(ABORT, 'simulated feedback history failure'); END """ ) conn.commit() finally: conn.close() with pytest.raises(Exception): rounds.update_feedback( round_id, { "updates": [ {"candidate_id": candidate_id, "preference_status": "liked"} ] }, RequestContext.system("feedback-rollback"), ) after = rounds.get_round(round_id).to_dict() assert all(item["preference_status"] == "neutral" for item in after["candidates"]) assert after["round"]["feedback_snapshot"] == {} def test_draft_final_selection_survives_refresh_without_adopting_image(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=3, idempotency_key="round-draft-final", ) round_id = detail["round"]["round_id"] chosen = detail["candidates"][1] updated = rounds.update_feedback( round_id, { "updates": [], "draft_final_candidate_id": chosen["candidate_id"], "idempotency_key": "draft-final-1", }, RequestContext.system("draft-final"), ) assert updated["round"]["feedback_snapshot"]["draft_final_candidate_id"] == chosen[ "candidate_id" ] refreshed = rounds.get_round(round_id).to_dict() assert refreshed["round"]["feedback_snapshot"]["draft_final_candidate_id"] == chosen[ "candidate_id" ] assert all(item["is_final"] is False for item in refreshed["candidates"]) assert all(item["image"]["is_selected"] is False for item in refreshed["candidates"]) assert generation.workflow_service.get_node("node-round-runtime").selected_image_id is None with pytest.raises(ApiError) as rejected: rounds.update_feedback( round_id, { "updates": [ { "candidate_id": chosen["candidate_id"], "preference_status": "rejected", } ], "draft_final_candidate_id": chosen["candidate_id"], }, RequestContext.system("draft-final"), ) assert rejected.value.details["reason"] == "candidate_rejected" after = rounds.get_round(round_id).to_dict() assert after["round"]["feedback_snapshot"]["draft_final_candidate_id"] == chosen[ "candidate_id" ] assert after["candidates"][1]["preference_status"] == "neutral" def test_conversational_final_is_persisted_as_draft_only(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=2, idempotency_key="round-conversation-final", ) round_id = detail["round"]["round_id"] chosen = detail["candidates"][1] updated = rounds.update_feedback( round_id, {"feedback_text": f"{chosen['display_code']} 采用"}, RequestContext.system("conversation-final"), ) assert updated["round"]["feedback_snapshot"]["draft_final_candidate_id"] == chosen[ "candidate_id" ] assert updated["interpretation"]["final"]["candidate_id"] == chosen["candidate_id"] assert all(item["is_final"] is False for item in updated["candidates"]) assert all(item["image"]["is_selected"] is False for item in updated["candidates"]) def test_failed_model_and_local_interpretation_do_not_mutate_parent_round(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=2, idempotency_key="round-feedback-no-pollution", ) round_id = detail["round"]["round_id"] rounds.feedback_service = WorkflowRoundFeedbackService( structured_text_callable=lambda _payload: {"feedback": {"liked": ["TX-99-99"]}}, model_route="prompt-planning-test", ) with pytest.raises(ApiError) as captured: rounds.update_feedback( round_id, {"feedback_text": "99喜欢", "idempotency_key": "bad-feedback-1"}, RequestContext.system("feedback-no-pollution"), ) assert captured.value.details["reason"] == "unknown_candidate_reference" after = rounds.get_round(round_id).to_dict() assert after["round"]["feedback_snapshot"] == {} assert all(item["preference_status"] == "neutral" for item in after["candidates"]) def test_round_resource_owner_chain_protects_every_public_operation(tmp_path: Path) -> None: _database_handle, _generation, rounds, _expansion = _fixture(tmp_path) owner = RequestContext.system("round-owner") created = rounds.create_round( "node-round-runtime", {"count": 2, "idempotency_key": "round-owner-chain"}, owner, ) round_id = created["round"]["round_id"] candidate_id = created["candidates"][0]["candidate_id"] intruder = RequestContext.from_astrbot( username="intruder", user_id="intruder", permissions={"can_write"}, ) operations = ( lambda: rounds.list_node_rounds("node-round-runtime", ctx=intruder), lambda: rounds.get_round(round_id, intruder), lambda: rounds.create_round( "node-round-runtime", {"count": 1, "idempotency_key": "round-owner-intruder"}, intruder, ), lambda: rounds.update_feedback( round_id, { "updates": [ { "candidate_id": candidate_id, "preference_status": "liked", } ] }, intruder, ), lambda: rounds.create_next_round(round_id, {}, intruder), lambda: rounds.confirm_candidate( round_id, {"candidate_id": candidate_id}, intruder, ), ) for operation in operations: with pytest.raises(ApiError) as captured: operation() assert captured.value.error_code == ErrorCode.RESOURCE_NOT_FOUND admin = RequestContext.from_astrbot( username="admin", user_id="admin", permissions={"can_manage_all_profiles"}, ) assert rounds.get_round(round_id, admin).round_record.round_id == round_id assert rounds.list_node_rounds("node-round-runtime", ctx=admin)["total"] == 1 def test_next_round_rejects_feedback_changed_after_consistent_parent_snapshot( tmp_path: Path, ) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) ctx = RequestContext.system("next-round-cas") detail = _complete_round( generation, rounds, count=2, idempotency_key="round-next-cas-parent", ) round_id = detail["round"]["round_id"] liked_id = detail["candidates"][0]["candidate_id"] rounds.update_feedback( round_id, { "updates": [ {"candidate_id": liked_id, "preference_status": "liked"} ] }, ctx, ) child_count_before = len( [ item for item in rounds.round_repository.list_rounds(node_id="node-round-runtime") if item.parent_round_id == round_id ] ) create_round = rounds.create_round def create_after_concurrent_feedback(node_id, payload, request_ctx): rounds.update_feedback( round_id, { "updates": [ { "candidate_id": liked_id, "preference_status": "liked", "reason": "newer feedback revision", } ] }, ctx, ) return create_round(node_id, payload, request_ctx) rounds.create_round = create_after_concurrent_feedback # type: ignore[method-assign] with pytest.raises(ApiError) as captured: rounds.create_next_round( round_id, {"count": 4, "idempotency_key": "round-next-cas-child"}, ctx, ) assert captured.value.details["reason"] == "parent_feedback_changed" assert captured.value.details["required_action"] == "refresh_parent_round_and_retry" child_count_after = len( [ item for item in rounds.round_repository.list_rounds(node_id="node-round-runtime") if item.parent_round_id == round_id ] ) assert child_count_after == child_count_before def test_confirm_candidate_atomically_selects_and_advances_server_derived_node(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=3, idempotency_key="round-confirm-atomic", ) round_id = detail["round"]["round_id"] chosen = detail["candidates"][1] with pytest.raises(ApiError) as missing: rounds.confirm_candidate(round_id, {}, RequestContext.system("confirm-test")) assert missing.value.details["count"] == 0 with pytest.raises(ApiError) as multiple: rounds.confirm_candidate( round_id, {"candidate_ids": [detail["candidates"][0]["candidate_id"], chosen["candidate_id"]]}, RequestContext.system("confirm-test"), ) assert multiple.value.details["count"] == 2 with pytest.raises(ApiError) as client_next: rounds.confirm_candidate( round_id, {"candidate_id": chosen["candidate_id"], "next_node_id": "client-chosen-node"}, RequestContext.system("confirm-test"), ) assert client_next.value.details["forbidden_fields"] == ["next_node_id"] confirmed = rounds.confirm_candidate( round_id, {"candidate_id": chosen["candidate_id"], "idempotency_key": "confirm-request-1"}, RequestContext.system("confirm-test"), ) assert confirmed["round"]["status"] == "confirmed" assert confirmed["confirmed_candidate_id"] == chosen["candidate_id"] assert confirmed["node"]["status"] == "selected" assert confirmed["node"]["selected_image_id"] == chosen["candidate"]["image_id"] assert confirmed["next_node"]["node_id"] == "node-round-runtime-next" assert confirmed["history"]["action"] == "confirm_round_candidate" assert sum(1 for item in confirmed["candidates"] if item["is_final"]) == 1 assert next(item for item in confirmed["candidates"] if item["is_final"])["candidate_id"] == chosen[ "candidate_id" ] assert sum(1 for item in confirmed["candidates"] if item["image"]["is_selected"]) == 1 workflow = generation.workflow_service.get_workflow("workflow-round-runtime").workflow assert workflow.current_node_id == "node-round-runtime-next" assert workflow.metadata["stage_inputs"]["node-round-runtime-next"]["candidate_id"] == chosen[ "candidate_id" ] assert workflow.metadata["branch_state"]["main"]["stage_inputs"]["node-round-runtime-next"][ "image_id" ] == chosen["candidate"]["image_id"] replay = rounds.confirm_candidate( round_id, {"candidate_id": chosen["candidate_id"], "idempotency_key": "confirm-request-1"}, RequestContext.system("confirm-test"), ) assert replay["created"] is False assert replay["idempotent_replay"] is True with pytest.raises(ApiError) as conflicting: rounds.confirm_candidate( round_id, {"candidate_id": detail["candidates"][0]["candidate_id"]}, RequestContext.system("confirm-test"), ) assert conflicting.value.details["reason"] == "round_already_confirmed" with pytest.raises(ApiError) as locked_feedback: rounds.update_feedback( round_id, {"updates": [{"candidate_id": chosen["candidate_id"], "preference_status": "neutral"}]}, RequestContext.system("confirm-test"), ) assert locked_feedback.value.details["required_action"] == "create_branch" def test_legacy_round_feedback_is_read_only(tmp_path: Path) -> None: _database_handle, _generation, rounds, _expansion = _fixture(tmp_path) with pytest.raises(ApiError) as captured: rounds.update_feedback( "legacy-round-node-round-runtime", {"updates": [{"candidate_id": "legacy-candidate", "preference_status": "liked"}]}, RequestContext.system("legacy-feedback"), ) assert captured.value.details["reason"] == "round_read_only" def test_confirm_transaction_rolls_back_every_write_on_history_failure(tmp_path: Path) -> None: database, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=2, idempotency_key="round-confirm-rollback", ) round_id = detail["round"]["round_id"] chosen = detail["candidates"][0] conn = database.connect() try: conn.execute( """ CREATE TRIGGER fail_round_confirmation_history BEFORE INSERT ON workflow_history WHEN NEW.action = 'confirm_round_candidate' BEGIN SELECT RAISE(ABORT, 'simulated history failure'); END """ ) conn.commit() finally: conn.close() with pytest.raises(Exception): rounds.confirm_candidate( round_id, {"candidate_id": chosen["candidate_id"]}, RequestContext.system("confirm-test"), ) after = rounds.get_round(round_id).to_dict() assert after["round"]["status"] == "completed" assert all(item["is_final"] is False for item in after["candidates"]) assert all(item["image"]["is_selected"] is False for item in after["candidates"]) node = generation.workflow_service.get_node("node-round-runtime") assert node.status == "completed" assert node.selected_image_id is None workflow = generation.workflow_service.get_workflow("workflow-round-runtime").workflow assert workflow.current_node_id == "node-round-runtime" def test_concurrent_double_confirmation_allows_only_one_final_candidate(tmp_path: Path) -> None: _database_handle, generation, rounds, _expansion = _fixture(tmp_path) detail = _complete_round( generation, rounds, count=2, idempotency_key="round-confirm-race", ) round_id = detail["round"]["round_id"] candidate_ids = [item["candidate_id"] for item in detail["candidates"]] barrier = threading.Barrier(2) successes: list[dict[str, Any]] = [] errors: list[Exception] = [] def confirm(candidate_id: str) -> None: try: barrier.wait(timeout=2) successes.append( rounds.confirm_candidate( round_id, {"candidate_id": candidate_id}, RequestContext.system("confirm-race"), ) ) except Exception as exc: errors.append(exc) threads = [threading.Thread(target=confirm, args=(candidate_id,)) for candidate_id in candidate_ids] for thread in threads: thread.start() for thread in threads: thread.join(timeout=5) assert len(successes) == 1 assert len(errors) == 1 after = rounds.get_round(round_id).to_dict() assert after["round"]["status"] == "confirmed" assert sum(1 for item in after["candidates"] if item["is_final"]) == 1 def test_legacy_synchronous_generate_node_remains_compatible(tmp_path: Path) -> None: _database_handle, generation, _rounds, expansion = _fixture(tmp_path) result = generation.generate_node( "node-round-runtime", {"count": 1, "idempotency_key": "legacy-sync-generation"}, RequestContext.system("legacy-sync-test"), ) assert result["task_status"] == "succeeded" assert result["node_status"] == "completed" assert len(result["images"]) == 1 assert result["images"][0]["is_selected"] is False assert generation.workflow_service.get_node("node-round-runtime").selected_image_id is None assert expansion.calls == 1