Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(api): migrate Service API annotation management (#43809)
非法操作092526a5048b
diff --git a/api/controllers/service_api/app/annotation.py b/api/controllers/service_api/app/annotation.pyindex da26ad37f0d..cf7ed0739f8 100644--- a/api/controllers/service_api/app/annotation.py+++ b/api/controllers/service_api/app/annotation.py@@ -1,17 +1,15 @@-from typing import Literal+from http import HTTPStatusfrom uuid import UUIDfrom flask_restx import Resource-from flask_restx.api import HTTPStatusfrom pydantic import BaseModel, Field, TypeAdapter-from sqlalchemy.orm import Session+from controllers.common.errors import NotFoundErrorfrom controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models-from controllers.common.session import with_session-from controllers.console.wraps import edit_permission_required, model_validate+from controllers.console.wraps import model_validatefrom controllers.service_api import service_api_ns-from controllers.service_api.wraps import validate_app_token-from extensions.ext_redis import redis_client+from controllers.service_api.flask_admission import service_api_account_admission+from extensions.ext_application_services import application_servicesfrom fields.annotation_fields import (Annotation,AnnotationJobStatusDetailResponse,@@ -19,15 +17,9 @@ from fields.annotation_fields import (AnnotationList,)from libs.helper import dump_response-from libs.pagination import clamp_pagination-from models.model import App-from services.annotation_service import (- AppAnnotationService,- EnableAnnotationArgs,- InsertAnnotationArgs,- UpdateAnnotationArgs,-)-from services.app_ref_service import AppRefService+from machinery.context import ServiceApiAccountRequestContext+from services.annotation_query import AnnotationAppNotFoundError, AnnotationNotFoundError+from services.annotation_reply_service import AnnotationReplyAction, AnnotationReplyJobNotFoundErrorclass AnnotationCreatePayload(BaseModel):@@ -85,7 +77,7 @@ class AnnotationReplyActionApi(Resource):),tags=["Annotations"],responses={- 200: "Annotation reply settings task initiated.",+ HTTPStatus.OK: "Annotation reply settings task initiated.",},)@service_api_ns.expect(service_api_ns.models[AnnotationReplyActionPayload.__name__])@@ -94,30 +86,41 @@ class AnnotationReplyActionApi(Resource):@service_api_ns.doc(params={"action": ANNOTATION_REPLY_ACTION_PARAM})@service_api_ns.doc(responses={- 200: "Action completed successfully",- 401: "Unauthorized - invalid API token",+ HTTPStatus.OK: "Action completed successfully",+ HTTPStatus.BAD_REQUEST: "`invalid_param` : Invalid action or app selection.",+ HTTPStatus.UNAUTHORIZED: "`unauthorized` : Invalid API credential or workspace owner unavailable.",+ HTTPStatus.NOT_FOUND: "`not_found` : App no longer exists.",+ HTTPStatus.UNPROCESSABLE_ENTITY: "`unprocessable_entity` : Invalid annotation reply payload.",})@service_api_ns.response(- 200,+ HTTPStatus.OK,"Action completed successfully",service_api_ns.models[AnnotationJobStatusResponse.__name__],)- @validate_app_token+ @service_api_account_admission@model_validate(AnnotationReplyActionPayload)- def post(self, payload: AnnotationReplyActionPayload, app_model: App, action: Literal["enable", "disable"]):+ def post(+ self, payload: AnnotationReplyActionPayload, context: ServiceApiAccountRequestContext, action: str+ ) -> tuple[dict[str, object], HTTPStatus]:"""Enable or disable annotation reply feature."""- match action:- case "enable":- enable_args: EnableAnnotationArgs = {- "score_threshold": payload.score_threshold,- "embedding_provider_name": payload.embedding_provider_name,- "embedding_model_name": payload.embedding_model_name,- }- result = AppAnnotationService.enable_app_annotation(enable_args, app_model.id)- case "disable":- result = AppAnnotationService.disable_app_annotation(app_model.id)- return dump_response(AnnotationJobStatusResponse, result), 200+ validated_action = TypeAdapter(AnnotationReplyAction).validate_python(action)+ service = application_services().annotation_reply+ try:+ if validated_action == "enable":+ result = service.enable(+ tenant_id=context.tenant_id,+ app_id=context.app_id,+ account_id=context.account_id,+ score_threshold=payload.score_threshold,+ embedding_provider_name=payload.embedding_provider_name,+ embedding_model_name=payload.embedding_model_name,+ )+ else:+ result = service.disable(tenant_id=context.tenant_id, app_id=context.app_id)+ except AnnotationAppNotFoundError as exc:+ raise NotFoundError("App not found") from exc+ return dump_response(AnnotationJobStatusResponse, result), HTTPStatus.OK@service_api_ns.route("/apps/annotation-reply/<string:action>/status/<uuid:job_id>")@@ -130,8 +133,9 @@ class AnnotationReplyActionStatusApi(Resource):),tags=["Annotations"],responses={- 200: "Successfully retrieved task status.",- 400: "`invalid_param` : The specified job does not exist.",+ HTTPStatus.OK: "Successfully retrieved task status.",+ HTTPStatus.BAD_REQUEST: "`invalid_param` : Invalid action or app selection.",+ HTTPStatus.NOT_FOUND: "`not_found` : The specified job does not belong to this app or no longer exists.",},)@service_api_ns.doc("get_annotation_reply_action_status")@@ -147,35 +151,30 @@ class AnnotationReplyActionStatusApi(Resource):)@service_api_ns.doc(responses={- 200: "Job status retrieved successfully",- 401: "Unauthorized - invalid API token",+ HTTPStatus.OK: "Job status retrieved successfully",+ HTTPStatus.UNAUTHORIZED: "`unauthorized` : Invalid API credential or workspace owner unavailable.",})@service_api_ns.response(- 200,+ HTTPStatus.OK,"Job status retrieved successfully",service_api_ns.models[AnnotationJobStatusDetailResponse.__name__],)- @validate_app_token- def get(self, app_model: App, job_id: UUID, action: str):+ @service_api_account_admission+ def get(+ self, context: ServiceApiAccountRequestContext, job_id: UUID, action: str+ ) -> tuple[dict[str, object], HTTPStatus]:"""Get the status of an annotation reply action job."""- job_id_str = str(job_id)- app_annotation_job_key = f"{action}_app_annotation_job_{job_id_str}"- cache_result = redis_client.get(app_annotation_job_key)- if cache_result is None:- raise ValueError("The job does not exist.")-- job_status = cache_result.decode()- error_msg = ""- if job_status == "error":- app_annotation_error_key = f"{action}_app_annotation_error_{job_id_str}"- error_result = redis_client.get(app_annotation_error_key)- if error_result is not None:- error_msg = error_result.decode()-- return AnnotationJobStatusDetailResponse(- job_id=job_id_str, job_status=job_status, error_msg=error_msg- ).model_dump(mode="json"), 200+ validated_action = TypeAdapter(AnnotationReplyAction).validate_python(action)+ try:+ result = application_services().annotation_reply.get_status(+ tenant_id=context.tenant_id, app_id=context.app_id, action=validated_action, job_id=str(job_id)+ )+ except AnnotationAppNotFoundError as exc:+ raise NotFoundError("App not found") from exc+ except AnnotationReplyJobNotFoundError as exc:+ raise NotFoundError("The job does not exist.") from exc+ return dump_response(AnnotationJobStatusDetailResponse, result), HTTPStatus.OK@service_api_ns.route("/apps/annotations")@@ -185,7 +184,7 @@ class AnnotationListApi(Resource):description="Retrieves a paginated list of annotations for the application. Supports keyword search filtering.",tags=["Annotations"],responses={- 200: "Successfully retrieved annotation list.",+ HTTPStatus.OK: "Successfully retrieved annotation list.",},)@service_api_ns.doc("list_annotations")@@ -193,33 +192,33 @@ class AnnotationListApi(Resource):@service_api_ns.doc(params=query_params_from_model(AnnotationListQuery))@service_api_ns.doc(responses={- 200: "Annotations retrieved successfully",- 401: "Unauthorized - invalid API token",+ HTTPStatus.OK: "Annotations retrieved successfully",+ HTTPStatus.BAD_REQUEST: "`invalid_param` : Invalid app selection.",+ HTTPStatus.UNAUTHORIZED: "`unauthorized` : Invalid API credential or workspace owner unavailable.",+ HTTPStatus.NOT_FOUND: "`not_found` : App no longer exists.",+ HTTPStatus.UNPROCESSABLE_ENTITY: "`unprocessable_entity` : Invalid pagination query.",})@service_api_ns.response(- 200,+ HTTPStatus.OK,"Annotations retrieved successfully",service_api_ns.models[AnnotationList.__name__],)- @validate_app_token- @with_session(write=False)+ @service_api_account_admission@model_validate(AnnotationListQuery)- def get(self, query: AnnotationListQuery, session: Session, app_model: App):+ def get(self, query: AnnotationListQuery, context: ServiceApiAccountRequestContext) -> dict[str, object]:"""List annotations for the application."""-- effective_page, effective_limit = clamp_pagination(query.page, query.limit, 100)- annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(- app_model.id, effective_page, effective_limit, query.keyword, session- )- annotation_models = TypeAdapter(list[Annotation]).validate_python(annotation_list, from_attributes=True)- return AnnotationList(- data=annotation_models,- has_more=effective_page * effective_limit < total,- limit=effective_limit,- total=total,- page=effective_page,- ).model_dump(mode="json")+ try:+ result = application_services().annotation_queries.get_page(+ tenant_id=context.tenant_id,+ app_id=context.app_id,+ page=query.page,+ limit=query.limit,+ keyword=query.keyword,+ )+ except AnnotationAppNotFoundError as exc:+ raise NotFoundError("App not found") from exc+ return dump_response(AnnotationList, result)@service_api_ns.doc(summary="Create Annotation",@@ -229,7 +228,7 @@ class AnnotationListApi(Resource):),tags=["Annotations"],responses={- 201: "Annotation created successfully.",+ HTTPStatus.CREATED: "Annotation created successfully.",},)@service_api_ns.expect(service_api_ns.models[AnnotationCreatePayload.__name__])@@ -237,8 +236,11 @@ class AnnotationListApi(Resource):@service_api_ns.doc(description="Create a new annotation")@service_api_ns.doc(responses={- 201: "Annotation created successfully",- 401: "Unauthorized - invalid API token",+ HTTPStatus.CREATED: "Annotation created successfully",+ HTTPStatus.BAD_REQUEST: "`invalid_param` : Invalid app selection.",+ HTTPStatus.UNAUTHORIZED: "`unauthorized` : Invalid API credential or workspace owner unavailable.",+ HTTPStatus.NOT_FOUND: "`not_found` : App no longer exists.",+ HTTPStatus.UNPROCESSABLE_ENTITY: "`unprocessable_entity` : Invalid annotation payload.",})@service_api_ns.response(@@ -246,13 +248,22 @@ class AnnotationListApi(Resource):"Annotation created successfully",service_api_ns.models[Annotation.__name__],)- @validate_app_token- @with_session+ @service_api_account_admission@model_validate(AnnotationCreatePayload)- def post(self, payload: AnnotationCreatePayload, session: Session, app_model: App):+ def post(+ self, payload: AnnotationCreatePayload, context: ServiceApiAccountRequestContext+ ) -> tuple[dict[str, object], HTTPStatus]:"""Create a new annotation."""- insert_args: InsertAnnotationArgs = {"question": payload.question, "answer": payload.answer}- annotation = AppAnnotationService.insert_app_annotation_directly(insert_args, app_model.id, session)+ try:+ annotation = application_services().annotation_commands.create(+ tenant_id=context.tenant_id,+ app_id=context.app_id,+ account_id=context.account_id,+ question=payload.question,+ answer=payload.answer,+ )+ except AnnotationAppNotFoundError as exc:+ raise NotFoundError("App not found") from excreturn dump_response(Annotation, annotation), HTTPStatus.CREATED@@ -263,9 +274,9 @@ class AnnotationUpdateDeleteApi(Resource):description="Updates the question and answer of an existing annotation.",tags=["Annotations"],responses={- 200: "Annotation updated successfully.",- 403: "`forbidden` : Insufficient permissions to edit annotations.",- 404: "`not_found` : Annotation does not exist.",+ HTTPStatus.OK: "Annotation updated successfully.",+ HTTPStatus.FORBIDDEN: "`forbidden` : API credential scope, app, or workspace access denied.",+ HTTPStatus.NOT_FOUND: "`not_found` : App or annotation no longer exists.",},)@service_api_ns.expect(service_api_ns.models[AnnotationCreatePayload.__name__])@@ -274,27 +285,37 @@ class AnnotationUpdateDeleteApi(Resource):@service_api_ns.doc(params={"annotation_id": "The unique identifier of the annotation to update."})@service_api_ns.doc(responses={- 200: "Annotation updated successfully",- 401: "Unauthorized - invalid API token",- 403: "Forbidden - insufficient permissions",- 404: "Annotation not found",+ HTTPStatus.OK: "Annotation updated successfully",+ HTTPStatus.BAD_REQUEST: "`invalid_param` : Invalid app selection.",+ HTTPStatus.UNAUTHORIZED: "`unauthorized` : Invalid API credential or workspace owner unavailable.",+ HTTPStatus.FORBIDDEN: "`forbidden` : API credential scope, app, or workspace access denied.",+ HTTPStatus.NOT_FOUND: "Annotation not found",+ HTTPStatus.UNPROCESSABLE_ENTITY: "`unprocessable_entity` : Invalid annotation payload.",})@service_api_ns.response(- 200,+ HTTPStatus.OK,"Annotation updated successfully",service_api_ns.models[Annotation.__name__],)- @validate_app_token- @with_session- @edit_permission_required+ @service_api_account_admission@model_validate(AnnotationCreatePayload)- def put(self, payload: AnnotationCreatePayload, session: Session, app_model: App, annotation_id: UUID):+ def put(+ self, payload: AnnotationCreatePayload, context: ServiceApiAccountRequestContext, annotation_id: UUID+ ) -> dict[str, object]:"""Update an existing annotation."""- update_args: UpdateAnnotationArgs = {"question": payload.question, "answer": payload.answer}- app_ref = AppRefService.create_app_ref(app_model)- annotation_ref = AppRefService.create_annotation_ref(app_ref, str(annotation_id))- annotation = AppAnnotationService.update_app_annotation_directly(update_args, annotation_ref, session)+ try:+ annotation = application_services().annotation_commands.update(+ tenant_id=context.tenant_id,+ app_id=context.app_id,+ annotation_id=str(annotation_id),+ question=payload.question,+ answer=payload.answer,+ )+ except AnnotationAppNotFoundError as exc:+ raise NotFoundError("App not found") from exc+ except AnnotationNotFoundError as exc:+ raise NotFoundError("Annotation not found") from excreturn dump_response(Annotation, annotation)@service_api_ns.doc(@@ -302,9 +323,9 @@ class AnnotationUpdateDeleteApi(Resource):description="Deletes an annotation and its associated hit history.",tags=["Annotations"],responses={- 204: "Annotation deleted successfully.",- 403: "`forbidden` : Insufficient permissions to edit annotations.",- 404: "`not_found` : Annotation does not exist.",+ HTTPStatus.NO_CONTENT: "Annotation deleted successfully.",+ HTTPStatus.FORBIDDEN: "`forbidden` : API credential scope, app, or workspace access denied.",+ HTTPStatus.NOT_FOUND: "`not_found` : App or annotation no longer exists.",},)@service_api_ns.doc("delete_annotation")@@ -312,18 +333,22 @@ class AnnotationUpdateDeleteApi(Resource):@service_api_ns.doc(params={"annotation_id": "The unique identifier of the annotation to delete."})@service_api_ns.doc(responses={- 204: "Annotation deleted successfully",- 401: "Unauthorized - invalid API token",- 403: "Forbidden - insufficient permissions",- 404: "Annotation not found",+ HTTPStatus.NO_CONTENT: "Annotation deleted successfully",+ HTTPStatus.BAD_REQUEST: "`invalid_param` : Invalid app selection.",+ HTTPStatus.UNAUTHORIZED: "`unauthorized` : Invalid API credential or workspace owner unavailable.",+ HTTPStatus.FORBIDDEN: "`forbidden` : API credential scope, app, or workspace access denied.",+ HTTPStatus.NOT_FOUND: "Annotation not found",})- @validate_app_token- @with_session- @edit_permission_required- def delete(self, session: Session, app_model: App, annotation_id: UUID):+ @service_api_account_admission+ def delete(self, context: ServiceApiAccountRequestContext, annotation_id: UUID) -> tuple[str, HTTPStatus]:"""Delete an annotation."""- app_ref = AppRefService.create_app_ref(app_model)- annotation_ref = AppRefService.create_annotation_ref(app_ref, str(annotation_id))- AppAnnotationService.delete_app_annotation(annotation_ref, session)- return "", 204+ try:+ application_services().annotation_commands.delete(+ tenant_id=context.tenant_id, app_id=context.app_id, annotation_id=str(annotation_id)+ )+ except AnnotationAppNotFoundError as exc:+ raise NotFoundError("App not found") from exc+ except AnnotationNotFoundError as exc:+ raise NotFoundError("Annotation not found") from exc+ return "", HTTPStatus.NO_CONTENTdiff --git a/api/controllers/service_api/flask_admission.py b/api/controllers/service_api/flask_admission.pyindex c3e98ea5bc4..5c081af53be 100644--- a/api/controllers/service_api/flask_admission.py+++ b/api/controllers/service_api/flask_admission.py@@ -1,4 +1,4 @@-"""Service API admission for handlers that consume stable app and end-user values."""+"""Service API admission for handlers that consume stable app and actor identities."""from collections.abc import Callablefrom functools import wraps@@ -6,8 +6,7 @@ from typing import Concatenatefrom flask import request-from controllers.common.errors import UnauthorizedError-from controllers.common.resource_access_token_errors import resource_access_token_errors+from controllers.common.errors import ForbiddenError, InvalidArgumentError, NotFoundError, UnauthorizedErrorfrom controllers.service_api.app import error as http_errorsfrom controllers.service_api.wraps import (FetchUserArg,@@ -16,14 +15,31 @@ from controllers.service_api.wraps import (resolve_service_api_end_user,validate_and_get_api_token,)+from core.logging.context import set_identity_contextfrom extensions.ext_application_services import application_services+from extensions.otel.runtime import set_identity_span_attributesfrom libs.external_api import document_errors-from machinery.context import AppRequestContext, ServiceApiEndUserContext+from machinery.context import AppRequestContext, ServiceApiAccountRequestContext, ServiceApiEndUserContextfrom services.app_definition_query_service import AppDefinitionUnavailableError, ServiceApiAppRecord-from services.auth.resource_access_token_contracts import is_resource_access_token+from services.auth.resource_access_token_contracts import (+ ResourceAccessTokenForbiddenError,+ ResourceAccessTokenInputError,+ ResourceAccessTokenInvalidError,+ ResourceAccessTokenNotFoundError,+ is_resource_access_token,+)from services.errors.app import AppAbnormalStatusError, AppApiDisabledErrorfrom services.errors.workspace import WorkspaceArchivedError, WorkspaceNotFoundError+_APP_ADMISSION_ERRORS = (+ ForbiddenError,+ http_errors.AppNotFoundError,+ http_errors.AppAbnormalStatusError,+ http_errors.AppApiDisabledError,+ http_errors.WorkspaceNotFoundError,+ http_errors.WorkspaceArchivedError,+)+def service_api_end_user_admission[T, **P, R](*,@@ -58,14 +74,7 @@ def service_api_end_user_admission[T, **P, R](return view(self, context, *args, **kwargs)document_app_token_contract(admitted, fetch_user_arg)- document_errors(- admitted,- http_errors.AppNotFoundError,- http_errors.AppAbnormalStatusError,- http_errors.AppApiDisabledError,- http_errors.WorkspaceNotFoundError,- http_errors.WorkspaceArchivedError,- )+ document_errors(admitted, *_APP_ADMISSION_ERRORS)return admittedreturn decorator@@ -83,36 +92,68 @@ def service_api_app_admission[T, **P, R](@wraps(view)def admitted(self: T, /, *args: P.args, **kwargs: P.kwargs) -> R:app = _admit_service_api_app()- if not application_services().app_definitions.has_service_api_owner(app.tenant_id):- raise UnauthorizedError("Tenant owner account not found or tenant is not active.")+ _require_service_api_owner(app.tenant_id)return view(self, AppRequestContext(tenant_id=app.tenant_id, app_id=app.app_id), *args, **kwargs)document_app_token_contract(admitted, None)- document_errors(- admitted,- http_errors.AppNotFoundError,- http_errors.AppAbnormalStatusError,- http_errors.AppApiDisabledError,- http_errors.WorkspaceNotFoundError,- http_errors.WorkspaceArchivedError,- )+ document_errors(admitted, *_APP_ADMISSION_ERRORS)return admitted+def service_api_account_admission[T, **P, R](+ view: Callable[Concatenate[T, ServiceApiAccountRequestContext, P], R],+) -> Callable[Concatenate[T, P], R]:+ """Admit app access and use its workspace owner for account-backed operations.++ This owner is the legacy attribution identity, not a user authenticated by the token.+ """++ @wraps(view)+ def admitted(self: T, /, *args: P.args, **kwargs: P.kwargs) -> R:+ app = _admit_service_api_app()+ context = ServiceApiAccountRequestContext(+ tenant_id=app.tenant_id,+ app_id=app.app_id,+ account_id=_require_service_api_owner(app.tenant_id),+ )+ set_identity_context(tenant_id=context.tenant_id, user_id=context.account_id, user_type="account")+ set_identity_span_attributes(tenant_id=context.tenant_id, user_id=context.account_id)+ return view(self, context, *args, **kwargs)++ document_app_token_contract(admitted, None)+ document_errors(admitted, *_APP_ADMISSION_ERRORS)+ return admitted+++def _require_service_api_owner(tenant_id: str) -> str:+ account_id = application_services().app_definitions.get_service_api_owner_id(tenant_id)+ if account_id is None:+ raise UnauthorizedError("Tenant owner account not found or tenant is not active.")+ return account_id++def _admit_service_api_app() -> ServiceApiAppRecord:auth_token = peek_service_api_bearer_token()- tenant_id = Noneif auth_token and is_resource_access_token(auth_token):- with resource_access_token_errors():+ try:grant = application_services().resource_access_tokens.resolve_app_for_service_api(token=auth_token,requested_app_id=request.headers.get("X-Dify-App-ID"),)+ except ResourceAccessTokenInputError as error:+ raise InvalidArgumentError(str(error)) from error+ except ResourceAccessTokenForbiddenError as error:+ raise ForbiddenError(str(error)) from error+ except ResourceAccessTokenNotFoundError as error:+ raise NotFoundError(str(error)) from error+ except ResourceAccessTokenInvalidError as error:+ raise UnauthorizedError(str(error)) from errorapp_id = next(iter(grant.app_ids))tenant_id = grant.tenant_idelse:token = validate_and_get_api_token("app")app_id = token.app_id+ tenant_id = token.tenant_idtry:return application_services().app_definitions.get_service_api_app(app_id, tenant_id=tenant_id)except AppDefinitionUnavailableError as error:diff --git a/api/controllers/service_api/wraps.py b/api/controllers/service_api/wraps.pyindex a3a9c6abff5..08769351a4d 100644--- a/api/controllers/service_api/wraps.py+++ b/api/controllers/service_api/wraps.py@@ -16,6 +16,7 @@ from werkzeug.exceptions import Forbidden, NotFound, ServiceUnavailable, Unauthofrom configs import dify_configfrom controllers.common.access_response import MISSING_BEARER_MESSAGE+from controllers.common.errors import UnauthorizedErrorfrom controllers.common.resource_access_token_errors import resource_access_token_errorsfrom controllers.service_api.schema import (USER_FETCH_FROM_ATTR,@@ -31,7 +32,13 @@ from models import Account, Tenant, TenantAccountJoin, TenantStatusfrom models.dataset import Datasetfrom models.model import ApiToken, EndUserfrom repositories.knowledge import dataset_api_key_bindings-from services.api_token_service import ApiTokenCache, fetch_token_with_single_flight, record_token_usage+from services.api_token_service import (+ ApiTokenCache,+ ApiTokenInvalidError,+ CachedApiToken,+ fetch_token_with_single_flight,+ record_token_usage,+)from services.app_service import AppServicefrom services.auth.resource_access_token_contracts import is_resource_access_tokenfrom services.feature_service import FeatureService@@ -287,7 +294,7 @@ def cloud_edition_billing_knowledge_limit_check[**P, R](return interceptor-def check_knowledge_rate_limit(api_token: ApiToken) -> None:+def check_knowledge_rate_limit(api_token: ApiToken | CachedApiToken) -> None:"""Enforce the cloud knowledge base request rate limit for a dataset API key's workspace.Shared by the ``cloud_edition_billing_rate_limit_check`` decorator and the KnowledgeFS@@ -421,7 +428,7 @@ def validate_dataset_token[R](view: Callable[..., R]) -> Callable[..., R]:return decorated-def validate_and_get_api_token(scope: str | None = None):+def validate_and_get_api_token(scope: str | None = None) -> ApiToken | CachedApiToken:"""Validate and get API token with Redis caching.@@ -441,17 +448,20 @@ def validate_and_get_api_token(scope: str | None = None):logger.debug("Token validation served from cache for scope: %s", scope)# Record usage in Redis for later batch update (no Celery task per request)record_token_usage(auth_token, scope)- return cast(ApiToken, cached_token)+ return cached_token# Cache miss - use Redis lock for single-flight mode# This ensures only one request queries DB for the same token concurrently- return fetch_token_with_single_flight(auth_token, scope)+ try:+ return fetch_token_with_single_flight(auth_token, scope)+ except ApiTokenInvalidError as error:+ raise UnauthorizedError(str(error)) from errordef extract_service_api_bearer_token() -> str:auth_token = peek_service_api_bearer_token()if auth_token is None:- raise Unauthorized(MISSING_BEARER_MESSAGE)+ raise UnauthorizedError(MISSING_BEARER_MESSAGE)return auth_tokendiff --git a/api/extensions/otel/runtime.py b/api/extensions/otel/runtime.pyindex ef3c8612695..47751c8600d 100644--- a/api/extensions/otel/runtime.py+++ b/api/extensions/otel/runtime.py@@ -1,7 +1,6 @@import loggingimport osimport sys-from typing import Unionfrom celery.signals import worker_initfrom flask_login import user_loaded_from_request, user_logged_in@@ -62,27 +61,27 @@ def is_celery_worker():@user_logged_in.connect@user_loaded_from_request.connect-def on_user_loaded(_sender, user: Union["Account", "EndUser"]):- if dify_config.ENABLE_OTEL:- from opentelemetry.trace import get_current_span-- if user:- try:- current_span = get_current_span()- if not current_span.is_recording():- return- tenant_id = extract_tenant_id(user)- if not tenant_id:- return- current_span.set_attributes(- {- DifySpanAttributes.TENANT_ID: tenant_id,- GenAIAttributes.USER_ID: user.id,- }- )- except Exception:- logger.exception("Error setting tenant and user attributes")- pass+def on_user_loaded(_sender: object, user: Account | EndUser | None) -> None:+ if dify_config.ENABLE_OTEL and user:+ try:+ if not trace.get_current_span().is_recording():+ return+ set_identity_span_attributes(tenant_id=extract_tenant_id(user), user_id=user.id)+ except Exception:+ logger.exception("Error setting tenant and user attributes")+++def set_identity_span_attributes(*, tenant_id: str | None, user_id: str) -> None:+ """Enrich the recording span; a missing workspace (None) skips enrichment."""+ if not dify_config.ENABLE_OTEL:+ return+ try:+ current_span = trace.get_current_span()+ if not current_span.is_recording() or not tenant_id:+ return+ current_span.set_attributes({DifySpanAttributes.TENANT_ID: tenant_id, GenAIAttributes.USER_ID: user_id})+ except Exception:+ logger.exception("Error setting tenant and user attributes")@worker_init.connect(weak=False)diff --git a/api/machinery/context.py b/api/machinery/context.pyindex 3b9e153fb17..b6cd8cc9859 100644--- a/api/machinery/context.py+++ b/api/machinery/context.py@@ -35,6 +35,13 @@ class ServiceApiEndUserContext(AppRequestContext):end_user_id: str+@dataclass(frozen=True, slots=True, kw_only=True)+class ServiceApiAccountRequestContext(AppRequestContext):+ """App scope and its workspace owner admitted for account-backed Service APIs."""++ account_id: str++class AccountRequestContext(NamedTuple):"""Stable identity for account-scoped use cases that do not require a workspace."""diff --git a/api/openapi/markdown/service-openapi.md b/api/openapi/markdown/service-openapi.mdindex 1ece0b9a0d4..e09a2203fab 100644--- a/api/openapi/markdown/service-openapi.md+++ b/api/openapi/markdown/service-openapi.md@@ -1747,7 +1747,7 @@ Retrieve a paginated list of all feedback submitted for messages in this applica| 200 | A list of application feedbacks. | **application/json**: [AppFeedbackListResponse](#appfeedbacklistresponse)<br> || 400 | `app_unavailable`: App is no longer available. | **application/json**: [AppUnavailableError](#appunavailableerror)<br>[ErrorResponse](#errorresponse)<br> || 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |### [POST] /messages/{message_id}/feedbacks@@ -1774,7 +1774,7 @@ Submit feedback for a message. End users can rate messages as `like` or `dislike| 200 | Feedback submitted successfully | **application/json**: [ResultResponse](#resultresponse)<br> || 400 | `message_feedback_rating_required`: Cannot revoke feedback that does not exist. `app_unavailable`: App is no longer available. | **application/json**: [AppUnavailableError](#appunavailableerror)<br>[MessageFeedbackRatingRequiredError](#messagefeedbackratingrequirederror)<br>[ErrorResponse](#errorresponse)<br> || 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> || 404 | `not_found` : Message does not exist. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |@@ -1803,8 +1803,11 @@ Enables or disables the annotation reply feature. Requires embedding model confi| Code | Description | Schema || ---- | ----------- | ------ || 200 | Annotation reply settings task initiated. | **application/json**: [AnnotationJobStatusResponse](#annotationjobstatusresponse)<br> |-| 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 400 | `invalid_param` : Invalid action or app selection. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 401 | `unauthorized` : Invalid API credential or workspace owner unavailable. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 404 | `not_found` : App no longer exists. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 422 | `unprocessable_entity` : Invalid annotation reply payload. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |### [GET] /apps/annotation-reply/{action}/status/{job_id}@@ -1824,9 +1827,10 @@ Retrieves the status of an asynchronous annotation reply configuration job start| Code | Description | Schema || ---- | ----------- | ------ || 200 | Successfully retrieved task status. | **application/json**: [AnnotationJobStatusDetailResponse](#annotationjobstatusdetailresponse)<br> |-| 400 | `invalid_param` : The specified job does not exist. | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 400 | `invalid_param` : Invalid action or app selection. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 401 | `unauthorized` : Invalid API credential or workspace owner unavailable. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 404 | `not_found` : The specified job does not belong to this app or no longer exists. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |### [GET] /apps/annotations@@ -1847,8 +1851,11 @@ Retrieves a paginated list of annotations for the application. Supports keyword| Code | Description | Schema || ---- | ----------- | ------ || 200 | Successfully retrieved annotation list. | **application/json**: [AnnotationList](#annotationlist)<br> |-| 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 400 | `invalid_param` : Invalid app selection. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 401 | `unauthorized` : Invalid API credential or workspace owner unavailable. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 404 | `not_found` : App no longer exists. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 422 | `unprocessable_entity` : Invalid pagination query. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |### [POST] /apps/annotations@@ -1867,8 +1874,11 @@ Creates a new annotation. Annotations provide predefined question-answer pairs t| Code | Description | Schema || ---- | ----------- | ------ || 201 | Annotation created successfully. | **application/json**: [Annotation](#annotation)<br> |-| 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 400 | `invalid_param` : Invalid app selection. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 401 | `unauthorized` : Invalid API credential or workspace owner unavailable. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 404 | `not_found` : App no longer exists. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 422 | `unprocessable_entity` : Invalid annotation payload. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |### [DELETE] /apps/annotations/{annotation_id}@@ -1887,9 +1897,10 @@ Deletes an annotation and its associated hit history.| Code | Description | Schema || ---- | ----------- | ------ || 204 | Annotation deleted successfully. | |-| 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | `forbidden` : Insufficient permissions to edit annotations. | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 404 | `not_found` : Annotation does not exist. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 400 | `invalid_param` : Invalid app selection. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 401 | `unauthorized` : Invalid API credential or workspace owner unavailable. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 403 | `forbidden` : API credential scope, app, or workspace access denied. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 404 | `not_found` : App or annotation no longer exists. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |### [PUT] /apps/annotations/{annotation_id}@@ -1914,9 +1925,11 @@ Updates the question and answer of an existing annotation.| Code | Description | Schema || ---- | ----------- | ------ || 200 | Annotation updated successfully. | **application/json**: [Annotation](#annotation)<br> |-| 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | `forbidden` : Insufficient permissions to edit annotations. | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 404 | `not_found` : Annotation does not exist. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 400 | `invalid_param` : Invalid app selection. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 401 | `unauthorized` : Invalid API credential or workspace owner unavailable. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 403 | `forbidden` : API credential scope, app, or workspace access denied. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 404 | `not_found` : App or annotation no longer exists. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 422 | `unprocessable_entity` : Invalid annotation payload. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |---@@ -2041,7 +2054,7 @@ Get next question suggestions for the current message. If no usable model can be| 200 | Suggested questions retrieved successfully | **application/json**: [SimpleResultStringListResponse](#simpleresultstringlistresponse)<br> || 400 | - `not_chat_app` : App mode does not match the API route. - `app_unavailable` : App is no longer available. - `completion_request_error` : Model invocation failed while counting history tokens. | **application/json**: [AppUnavailableError](#appunavailableerror)<br>[CompletionRequestError](#completionrequesterror)<br>[ModelCurrentlyNotSupportError](#modelcurrentlynotsupporterror)<br>[NotChatAppError](#notchatapperror)<br>[ProviderNotInitializeError](#providernotinitializeerror)<br>[ProviderQuotaExceededError](#providerquotaexceedederror)<br>[ErrorResponse](#errorresponse)<br> || 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | - `forbidden` : Token scope does not allow access. - `app_not_found` : The token's app no longer exists. - `app_abnormal_status` : App status does not allow API access. - `app_api_disabled` : The app's API service has been disabled. - `workspace_not_found` : The app's workspace no longer exists. - `workspace_archived` : The app's workspace is archived. - `app_suggested_questions_after_answer_disabled` : Suggested questions feature is disabled. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[AppSuggestedQuestionsAfterAnswerDisabledError](#appsuggestedquestionsafteranswerdisablederror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 403 | - `forbidden` : Token scope does not allow access. - `app_not_found` : The token's app no longer exists. - `app_abnormal_status` : App status does not allow API access. - `app_api_disabled` : The app's API service has been disabled. - `workspace_not_found` : The app's workspace no longer exists. - `workspace_archived` : The app's workspace is archived. - `app_suggested_questions_after_answer_disabled` : Suggested questions feature is disabled. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[AppSuggestedQuestionsAfterAnswerDisabledError](#appsuggestedquestionsafteranswerdisablederror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> || 404 | - `not_found` : End user, message, or conversation does not exist. - `agent_version_not_found_error` : Agent config version does not exist. | **application/json**: [AgentVersionNotFoundError](#agentversionnotfounderror)<br>[ErrorResponse](#errorresponse)<br> || 500 | `internal_server_error` : Internal server error. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |@@ -2194,7 +2207,7 @@ Get next question suggestions for the current message. If no usable model can be| 200 | Suggested questions retrieved successfully | **application/json**: [SimpleResultStringListResponse](#simpleresultstringlistresponse)<br> || 400 | - `not_chat_app` : App mode does not match the API route. - `app_unavailable` : App is no longer available. - `completion_request_error` : Model invocation failed while counting history tokens. | **application/json**: [AppUnavailableError](#appunavailableerror)<br>[CompletionRequestError](#completionrequesterror)<br>[ModelCurrentlyNotSupportError](#modelcurrentlynotsupporterror)<br>[NotChatAppError](#notchatapperror)<br>[ProviderNotInitializeError](#providernotinitializeerror)<br>[ProviderQuotaExceededError](#providerquotaexceedederror)<br>[ErrorResponse](#errorresponse)<br> || 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | - `forbidden` : Token scope does not allow access. - `app_not_found` : The token's app no longer exists. - `app_abnormal_status` : App status does not allow API access. - `app_api_disabled` : The app's API service has been disabled. - `workspace_not_found` : The app's workspace no longer exists. - `workspace_archived` : The app's workspace is archived. - `app_suggested_questions_after_answer_disabled` : Suggested questions feature is disabled. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[AppSuggestedQuestionsAfterAnswerDisabledError](#appsuggestedquestionsafteranswerdisablederror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 403 | - `forbidden` : Token scope does not allow access. - `app_not_found` : The token's app no longer exists. - `app_abnormal_status` : App status does not allow API access. - `app_api_disabled` : The app's API service has been disabled. - `workspace_not_found` : The app's workspace no longer exists. - `workspace_archived` : The app's workspace is archived. - `app_suggested_questions_after_answer_disabled` : Suggested questions feature is disabled. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[AppSuggestedQuestionsAfterAnswerDisabledError](#appsuggestedquestionsafteranswerdisablederror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> || 404 | - `not_found` : End user, message, or conversation does not exist. - `agent_version_not_found_error` : Agent config version does not exist. | **application/json**: [AgentVersionNotFoundError](#agentversionnotfounderror)<br>[ErrorResponse](#errorresponse)<br> || 500 | `internal_server_error` : Internal server error. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |@@ -2412,7 +2425,7 @@ Returns historical chat records in a scrolling load format, with the first page| 200 | Successfully retrieved conversation history. | **application/json**: [MessageInfiniteScrollPagination](#messageinfinitescrollpagination)<br> || 400 | `not_chat_app` : App mode does not match the API route. `app_unavailable` : App is no longer available. | **application/json**: [AppUnavailableError](#appunavailableerror)<br>[NotChatAppError](#notchatapperror)<br>[ErrorResponse](#errorresponse)<br> || 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |-| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> || 404 | - `not_found` : Conversation does not exist. - `not_found` : First message does not exist. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |@@ -5298,6 +5311,14 @@ The specified filename does not exist.| ---- | ---- | ----------- | -------- || FilenameNotExistsError | | The specified filename does not exist. | |+#### ForbiddenError++You don't have the permission to access the requested resource.++| Name | Type | Description | Required |+| ---- | ---- | ----------- | -------- |+| ForbiddenError | | You don't have the permission to access the requested resource. | |+#### FormInputConfig| Name | Type | Description | Required |diff --git a/api/repositories/annotation_repository.py b/api/repositories/annotation_repository.pyindex 0a2a5590c2f..5c2cf14507d 100644--- a/api/repositories/annotation_repository.py+++ b/api/repositories/annotation_repository.py@@ -137,6 +137,19 @@ class AnnotationRepository(AnnotationQuery, AnnotationWriteStore, AnnotationAppQtotal=result.total,)+ @override+ def create(+ self, *, tenant_id: str, app_id: str, account_id: str, question: str, answer: str+ ) -> AnnotationWriteResult:+ with self._session_factory.begin() as session:+ self._require_app(session, tenant_id=tenant_id, app_id=app_id)+ annotation = MessageAnnotation(app_id=app_id, question=question, content=answer, account_id=account_id)+ session.add(annotation)+ session.flush()+ return AnnotationWriteResult(+ annotation=self._record(annotation), collection_binding_id=self._binding_id(session, app_id=app_id)+ )+@overridedef upsert(self,diff --git a/api/repositories/app_definition_query_repository.py b/api/repositories/app_definition_query_repository.pyindex aa0582a53c4..703b28bf979 100644--- a/api/repositories/app_definition_query_repository.py+++ b/api/repositories/app_definition_query_repository.py@@ -146,22 +146,18 @@ class AppDefinitionQueryRepository(AppDefinitionQuery):)@override- def has_service_api_owner(self, tenant_id: str) -> bool:+ def get_service_api_owner_id(self, tenant_id: str) -> str | None:with self._session_factory() as session:- return bool(- session.scalar(- select(- select(Account.id)- .join(TenantAccountJoin, TenantAccountJoin.account_id == Account.id)- .join(Tenant, Tenant.id == TenantAccountJoin.tenant_id)- .where(- Tenant.id == tenant_id,- Tenant.status == TenantStatus.NORMAL,- TenantAccountJoin.role == TenantAccountRole.OWNER,- )- .exists()- )+ return session.scalar(+ select(Account.id)+ .join(TenantAccountJoin, TenantAccountJoin.account_id == Account.id)+ .join(Tenant, Tenant.id == TenantAccountJoin.tenant_id)+ .where(+ Tenant.id == tenant_id,+ Tenant.status == TenantStatus.NORMAL,+ TenantAccountJoin.role == TenantAccountRole.OWNER,)+ .limit(1))@overridediff --git a/api/services/annotation_command_service.py b/api/services/annotation_command_service.pyindex de49ba1bbb7..7cd30136dad 100644--- a/api/services/annotation_command_service.py+++ b/api/services/annotation_command_service.py@@ -28,6 +28,12 @@ class AnnotationDeletionResult:class AnnotationWriteStore(Protocol):+ def create(+ self, *, tenant_id: str, app_id: str, account_id: str, question: str, answer: str+ ) -> AnnotationWriteResult:+ """Create a manual annotation, preserving empty question and answer strings."""+ ...+def upsert(self,*,@@ -100,6 +106,21 @@ class AnnotationCommandService:self._update_index = update_indexself._delete_index = delete_index+ def create(self, *, tenant_id: str, app_id: str, account_id: str, question: str, answer: str) -> AnnotationRecord:+ """Service API creation accepts empty text and never updates an existing annotation."""+ result = self._annotations.create(+ tenant_id=tenant_id, app_id=app_id, account_id=account_id, question=question, answer=answer+ )+ if result.collection_binding_id is not None:+ self._add_index(+ annotation_id=result.annotation.id,+ question=result.annotation.question,+ tenant_id=tenant_id,+ app_id=app_id,+ collection_binding_id=result.collection_binding_id,+ )+ return result.annotation+def upsert(self,*,diff --git a/api/services/annotation_query.py b/api/services/annotation_query.pyindex 96727220a2f..1ab614a6fa5 100644--- a/api/services/annotation_query.py+++ b/api/services/annotation_query.py@@ -1,4 +1,4 @@-"""Detached annotation reads shared by Console annotation endpoints.+"""Detached annotation reads shared by Console and Service API endpoints.These queries need no application orchestration. The composition root injectstheir repository implementation directly, as it does for app statistics.diff --git a/api/services/annotation_service.py b/api/services/annotation_service.pyindex 4bc426b21be..3564db01a8f 100644--- a/api/services/annotation_service.py+++ b/api/services/annotation_service.py@@ -1,264 +1,16 @@-import logging-import uuid-from typing import TypedDict+"""Legacy annotation matching and hit history used by the generation runtime.-from sqlalchemy import or_, select, update+Management endpoints use the injected annotation query, command and job services.+"""++from sqlalchemy import select, updatefrom sqlalchemy.orm import Session-from werkzeug.exceptions import NotFound-from extensions.ext_redis import redis_client-from libs.login import current_account_with_tenant-from libs.pagination import paginate_queryfrom models.account import Account-from models.model import App, AppAnnotationHitHistory, AppAnnotationSetting, MessageAnnotation-from repositories.annotation_reply_job_repository import RedisAnnotationReplyJobRepository-from services.app_ref_service import AnnotationRef-from tasks.annotation.add_annotation_to_index_task import add_annotation_to_index_task-from tasks.annotation.delete_annotation_index_task import delete_annotation_index_task-from tasks.annotation.disable_annotation_reply_task import disable_annotation_reply_task-from tasks.annotation.enable_annotation_reply_task import enable_annotation_reply_task-from tasks.annotation.update_annotation_to_index_task import update_annotation_to_index_task--logger = logging.getLogger(__name__)---class AnnotationJobStatusDict(TypedDict):- job_id: str- job_status: str---class EnableAnnotationArgs(TypedDict):- """Expected shape of the args dict passed to enable_app_annotation."""-- score_threshold: float- embedding_provider_name: str- embedding_model_name: str---class InsertAnnotationArgs(TypedDict):- """Expected shape of the args dict passed to insert_app_annotation_directly."""-- question: str- answer: str---class UpdateAnnotationArgs(TypedDict, total=False):- """Expected shape of the args dict passed to update_app_annotation_directly.-- Both fields are optional at the type level; the service validates at runtime- and raises ValueError if either is missing.- """-- answer: str- question: str+from models.model import AppAnnotationHitHistory, MessageAnnotationclass AppAnnotationService:- @staticmethod- def _get_annotation_by_ref(annotation_ref: AnnotationRef, session: Session) -> MessageAnnotation | None:- return session.scalar(- select(MessageAnnotation)- .where(- MessageAnnotation.id == annotation_ref.annotation_id,- MessageAnnotation.app_id == annotation_ref.app.app_id,- )- .limit(1)- )-- @classmethod- def enable_app_annotation(cls, args: EnableAnnotationArgs, app_id: str) -> AnnotationJobStatusDict:- job_id = str(uuid.uuid4())- current_user, current_tenant_id = current_account_with_tenant()- # TODO: Remove this bridge when the Service API migrates to AnnotationReplyService.- jobs = RedisAnnotationReplyJobRepository(redis=redis_client)- try:- reserved_job_id = jobs.reserve(tenant_id=current_tenant_id, app_id=app_id, action="enable", job_id=job_id)- if reserved_job_id != job_id:- return {"job_id": reserved_job_id, "job_status": "processing"}- enable_annotation_reply_task.delay(- job_id,- app_id,- current_user.id,- current_tenant_id,- args["score_threshold"],- args["embedding_provider_name"],- args["embedding_model_name"],- )- except Exception as exc:- try:- jobs.fail(- tenant_id=current_tenant_id, app_id=app_id, action="enable", job_id=job_id, error_msg=str(exc)- )- except Exception:- logger.exception("Failed to record failure of annotation reply job %s for app %s", job_id, app_id)- raise- return {"job_id": job_id, "job_status": "waiting"}-- @classmethod- def disable_app_annotation(cls, app_id: str) -> AnnotationJobStatusDict:- _, current_tenant_id = current_account_with_tenant()- job_id = str(uuid.uuid4())- # TODO: Remove this bridge when the Service API migrates to AnnotationReplyService.- jobs = RedisAnnotationReplyJobRepository(redis=redis_client)- try:- reserved_job_id = jobs.reserve(tenant_id=current_tenant_id, app_id=app_id, action="disable", job_id=job_id)- if reserved_job_id != job_id:- return {"job_id": reserved_job_id, "job_status": "processing"}- disable_annotation_reply_task.delay(job_id, app_id, current_tenant_id)- except Exception as exc:- try:- jobs.fail(- tenant_id=current_tenant_id, app_id=app_id, action="disable", job_id=job_id, error_msg=str(exc)- )- except Exception:- logger.exception("Failed to record failure of annotation reply job %s for app %s", job_id, app_id)- raise- return {"job_id": job_id, "job_status": "waiting"}-- @classmethod- def get_annotation_list_by_app_id(cls, app_id: str, page: int, limit: int, keyword: str, session: Session):- # TODO: Migrate the Service API list caller off its shared session before removing this legacy query.- # get app info- _, current_tenant_id = current_account_with_tenant()- app = session.scalar(- select(App).where(App.id == app_id, App.tenant_id == current_tenant_id, App.status == "normal").limit(1)- )-- if not app:- raise NotFound("App not found")- if keyword:- from libs.helper import escape_like_pattern-- escaped_keyword = escape_like_pattern(keyword)- stmt = (- select(MessageAnnotation)- .where(- MessageAnnotation.app_id == app_id,- or_(- MessageAnnotation.question.ilike(f"%{escaped_keyword}%", escape="\\"),- MessageAnnotation.content.ilike(f"%{escaped_keyword}%", escape="\\"),- ),- )- .order_by(MessageAnnotation.created_at.desc(), MessageAnnotation.id.desc())- )- else:- stmt = (- select(MessageAnnotation)- .where(MessageAnnotation.app_id == app_id)- .order_by(MessageAnnotation.created_at.desc(), MessageAnnotation.id.desc())- )- annotations = paginate_query(stmt, session=session, page=page, per_page=limit, max_per_page=100)- return annotations.items, annotations.total or 0-- @classmethod- def insert_app_annotation_directly(- cls, args: InsertAnnotationArgs, app_id: str, session: Session- ) -> MessageAnnotation:- # get app info- current_user, current_tenant_id = current_account_with_tenant()- app = session.scalar(- select(App).where(App.id == app_id, App.tenant_id == current_tenant_id, App.status == "normal").limit(1)- )-- if not app:- raise NotFound("App not found")-- question = args.get("question")- if question is None:- raise ValueError("'question' is required")-- annotation = MessageAnnotation(- app_id=app.id, content=args["answer"], question=question, account_id=current_user.id- )- session.add(annotation)- session.commit()- # if annotation reply is enabled , add annotation to index- annotation_setting = session.scalar(- select(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).limit(1)- )- if annotation_setting:- add_annotation_to_index_task.delay(- annotation.id,- question,- current_tenant_id,- app_id,- annotation_setting.collection_binding_id,- )- return annotation-- @classmethod- def update_app_annotation_directly(- cls,- args: UpdateAnnotationArgs,- annotation_ref: AnnotationRef,- session: Session,- ):- annotation = cls._get_annotation_by_ref(annotation_ref, session)-- if not annotation:- raise NotFound("Annotation not found")-- question = args.get("question")- if question is None:- raise ValueError("'question' is required")-- answer = args.get("answer")- if answer is None:- raise ValueError("'answer' is required")-- annotation.content = answer- annotation.question = question-- session.commit()- # if annotation reply is enabled , add annotation to index- app_annotation_setting = session.scalar(- select(AppAnnotationSetting).where(AppAnnotationSetting.app_id == annotation_ref.app.app_id).limit(1)- )-- if app_annotation_setting:- update_annotation_to_index_task.delay(- annotation.id,- annotation.question_text,- annotation_ref.app.tenant_id,- annotation_ref.app.app_id,- app_annotation_setting.collection_binding_id,- )-- return annotation-- @classmethod- def delete_app_annotation(cls, annotation_ref: AnnotationRef, session: Session):- annotation = cls._get_annotation_by_ref(annotation_ref, session)-- if not annotation:- raise NotFound("Annotation not found")-- session.delete(annotation)-- annotation_hit_histories = session.scalars(- select(AppAnnotationHitHistory).where(- AppAnnotationHitHistory.app_id == annotation_ref.app.app_id,- AppAnnotationHitHistory.annotation_id == annotation_ref.annotation_id,- )- ).all()- if annotation_hit_histories:- for annotation_hit_history in annotation_hit_histories:- session.delete(annotation_hit_history)-- session.commit()- # if annotation reply is enabled , delete annotation index- app_annotation_setting = session.scalar(- select(AppAnnotationSetting).where(AppAnnotationSetting.app_id == annotation_ref.app.app_id).limit(1)- )-- if app_annotation_setting:- delete_annotation_index_task.delay(- annotation.id,- annotation_ref.app.app_id,- annotation_ref.app.tenant_id,- app_annotation_setting.collection_binding_id,- )-@classmethoddef get_annotation_by_id(cls, annotation_id: str, session: Session) -> MessageAnnotation | None:annotation = session.get(MessageAnnotation, annotation_id)diff --git a/api/services/api_token_service.py b/api/services/api_token_service.pyindex ad4434563e4..77d294049ec 100644--- a/api/services/api_token_service.py+++ b/api/services/api_token_service.py@@ -12,7 +12,6 @@ from typing import Any, overridefrom pydantic import BaseModelfrom sqlalchemy import selectfrom sqlalchemy.orm import Session-from werkzeug.exceptions import Unauthorizedfrom extensions.ext_database import dbfrom extensions.ext_redis import redis_client, redis_fallback@@ -22,6 +21,10 @@ from models.model import ApiTokenlogger = logging.getLogger(__name__)+class ApiTokenInvalidError(Exception):+ """The credential does not identify a valid API token for the requested scope."""++# ---------------------------------------------------------------------# Pydantic DTO# ---------------------------------------------------------------------@@ -320,7 +323,7 @@ def query_token_from_db(auth_token: str, scope: str | None) -> ApiToken:"""Query API token from database and cache the result.- Raises Unauthorized if token is invalid.+ Raises ApiTokenInvalidError if the token is invalid for the requested scope."""with Session(db.engine, expire_on_commit=False) as session:stmt = select(ApiToken).where(ApiToken.token == auth_token, ApiToken.type == scope)@@ -328,14 +331,14 @@ def query_token_from_db(auth_token: str, scope: str | None) -> ApiToken:if not api_token:ApiTokenCache.set(auth_token, scope, None)- raise Unauthorized("Access token is invalid")+ raise ApiTokenInvalidError("Access token is invalid")ApiTokenCache.set(auth_token, scope, api_token)record_token_usage(auth_token, scope)return api_token-def fetch_token_with_single_flight(auth_token: str, scope: str | None) -> ApiToken | Any:+def fetch_token_with_single_flight(auth_token: str, scope: str | None) -> ApiToken | CachedApiToken:"""Fetch token from DB with single-flight pattern using Redis lock.@@ -361,7 +364,7 @@ def fetch_token_with_single_flight(auth_token: str, scope: str | None) -> ApiTokelse:logger.warning("Lock timeout for token: %s, proceeding with direct query", auth_token[:10])return query_token_from_db(auth_token, scope)- except Unauthorized:+ except ApiTokenInvalidError:raiseexcept Exception as e:logger.warning("Redis lock failed for token query: %s, proceeding anyway", e)diff --git a/api/services/app_definition_query_service.py b/api/services/app_definition_query_service.pyindex 201ff21c63a..4b3731e7f01 100644--- a/api/services/app_definition_query_service.py+++ b/api/services/app_definition_query_service.py@@ -64,7 +64,9 @@ class AppDefinitionQuery(Protocol):def get_service_api_record(self, app_id: str, *, tenant_id: str | None = None) -> ServiceApiAppRecord | None: ...- def has_service_api_owner(self, tenant_id: str) -> bool: ...+ def get_service_api_owner_id(self, tenant_id: str) -> str | None:+ """Return the active workspace's owner ID, or None when that identity is unavailable."""+ ...def get_published_parameter_config(self,@@ -106,8 +108,8 @@ class AppDefinitionQueryService:``None`` means the API token no longer references an app. The repositorycloses its read session before this policy runs or an end user is provisioned.- Resource-token grants provide ``tenant_id`` to constrain the lookup to their- workspace; ``None`` retains legacy app tokens' app-ID-only lookup.+ Credentials with a recorded ``tenant_id`` constrain the lookup to that+ workspace; ``None`` supports historical app keys without a tenant ID."""if app_id is None:raise AppDefinitionUnavailableError("The app no longer exists.")@@ -124,9 +126,9 @@ class AppDefinitionQueryService:raise WorkspaceArchivedError("The workspace's status is archived.")return app- def has_service_api_owner(self, tenant_id: str) -> bool:- """Whether an active API workspace still has its legacy owner identity."""- return self._definitions.has_service_api_owner(tenant_id)+ def get_service_api_owner_id(self, tenant_id: str) -> str | None:+ """Return the active workspace's legacy owner identity; None means unavailable."""+ return self._definitions.get_service_api_owner_id(tenant_id)def get_mode(self, app_id: str) -> str:mode = self._definitions.get_mode(app_id)diff --git a/api/services/app_ref_service.py b/api/services/app_ref_service.pyindex 1f947ba1279..ae30a493576 100644--- a/api/services/app_ref_service.py+++ b/api/services/app_ref_service.py@@ -21,13 +21,6 @@ class MessageRef(NamedTuple):account_id: str | None = None-class AnnotationRef(NamedTuple):- """Annotation identifiers used to scope downstream resource lookups."""-- app: AppRef- annotation_id: str--class AppRefService:"""Factory helpers for app and child resource refs."""@@ -49,8 +42,3 @@ class AppRefService:end_user_id=end_user_id,account_id=account_id,)-- @staticmethod- def create_annotation_ref(app_ref: AppRef, annotation_id: str) -> AnnotationRef:- """Bind a candidate annotation ID; ownership is enforced when the ref is consumed."""- return AnnotationRef(app=app_ref, annotation_id=annotation_id)diff --git a/api/tests/e2e_tests/containers/controllers/service_api/app/test_annotation.py b/api/tests/e2e_tests/containers/controllers/service_api/app/test_annotation.pynew file mode 100644index 00000000000..817ab03c0b2--- /dev/null+++ b/api/tests/e2e_tests/containers/controllers/service_api/app/test_annotation.py@@ -0,0 +1,638 @@+"""Real Service API HTTP admission, PostgreSQL, Redis and Celery publications.++Workers are not executed: the memory broker verifies published messages without+calling embedding providers or vector stores. Container execution belongs to CI.+"""++from __future__ import annotations++from collections.abc import Iterator+from dataclasses import dataclass, replace+from queue import Empty+from typing import Literal+from uuid import UUID, uuid4++import pytest+from celery import Celery, Task+from celery.signals import before_task_publish+from flask import Flask, g+from flask_login import user_logged_in+from kombu.simple import SimpleQueue+from sqlalchemy import Connection, ExecutionContext, delete, event, func, select, text, update+from sqlalchemy.orm import Session, SessionTransaction, sessionmaker+from werkzeug.test import TestResponse++from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN+from constants.resource_access_token import ResourceAccessTokenResourceType+from controllers.console.wraps import _is_setup_completed+from enums import DeploymentEdition+from enums.account import TenantAccountRole+from extensions.ext_application_services import ApplicationServices+from extensions.ext_database import db+from extensions.ext_redis import RedisClientWrapper+from libs.datetime_utils import naive_utc_now+from libs.passport import PassportService+from libs.token import generate_csrf_token+from machinery.context import RequestContext+from models import Account, App, Tenant+from models.account import AccountStatus, TenantAccountJoin, TenantStatus+from models.enums import ApiTokenType+from models.model import ApiToken, AppAnnotationSetting, DifySetup, EndUser, MessageAnnotation+from repositories.annotation_reply_job_repository import (+ RedisAnnotationReplyJobRepository,+ annotation_reply_job_owner_key,+)+from repositories.annotation_repository import AnnotationRepository+from services.annotation_command_service import AnnotationCommandService+from services.annotation_reply_service import AnnotationReplyAction, AnnotationReplyService+from services.api_token_service import ApiTokenCache+from services.auth.resource_access_token_contracts import ResourceAccessTokenCreateResult, ResourceAccessTokenResource+from tasks.annotation.add_annotation_to_index_task import add_annotation_to_index_task+from tasks.annotation.delete_annotation_index_task import delete_annotation_index_task+from tasks.annotation.disable_annotation_reply_task import disable_annotation_reply_task+from tasks.annotation.enable_annotation_reply_task import enable_annotation_reply_task+from tasks.annotation.update_annotation_to_index_task import update_annotation_to_index_task+from tests.helpers.unit.config_override import config_overrides_context+from tests.helpers.unit.model_factories import make_account, make_app++_REPLY_PAYLOAD: dict[str, object] = {+ "score_threshold": 0.75,+ "embedding_provider_name": "openai",+ "embedding_model_name": "text-embedding-3-small",+}+_ANNOTATION_PAYLOAD: dict[str, object] = {"question": "Question", "answer": "Answer"}+_ENDPOINTS = [+ ("GET", "/annotations"),+ ("POST", "/annotations"),+ ("PUT", "/annotations/{id}"),+ ("DELETE", "/annotations/{id}"),+ ("POST", "/annotation-reply/enable"),+ ("GET", "/annotation-reply/enable/status/{id}"),+]+type Credential = Literal["key", "resource"]+++@dataclass(frozen=True)+class _Harness:+ app: Flask+ services: ApplicationServices+ account: Account+ target: App+ other: App+ foreign: App+ api_key: ApiToken+ other_key: ApiToken+ factory: sessionmaker[Session]+ redis: RedisClientWrapper+ celery: Celery+ tasks: dict[str, Task[..., None]]+ queue: str+ sessions: list[Session]+ publications: list[bool]+ logins: list[object]+ token_reads: list[str]++ @property+ def context(self) -> RequestContext:+ return RequestContext("annotation-test", None, self.account.id, self.target.tenant_id)++ def request(+ self,+ method: str,+ path: str,+ *,+ payload: dict[str, object] | None = None,+ token: str | None = None,+ authorization: str | None = None,+ app_id: str | None = None,+ ) -> TestResponse:+ headers = {+ "Authorization": authorization if authorization is not None else f"Bearer {token or self.api_key.token}"+ }+ if authorization == "":+ headers.clear()+ if app_id is not None:+ headers["X-Dify-App-ID"] = app_id+ with self.app.test_client() as client:+ response = client.open(f"/v1/apps{path}", method=method, json=payload, headers=headers)+ assert not isinstance(g.get("_login_user"), (Account, EndUser))+ with self.factory() as session:+ assert session.scalar(select(func.count()).select_from(EndUser)) == 0+ if response.status_code != 204:+ assert response.headers["Content-Type"] == "application/json"+ assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)+ assert self.logins == []+ return response++ def resource_token(self, *app_ids: str) -> ResourceAccessTokenCreateResult:+ return self.services.resource_access_tokens.create(+ self.context,+ name="Annotation integration key",+ resources=tuple(+ ResourceAccessTokenResource(type=ResourceAccessTokenResourceType.APP, id=app_id)+ for app_id in app_ids or (self.target.id,)+ ),+ )++ def credential(self, kind: Credential) -> str:+ return self.api_key.token if kind == "key" else self.resource_token().token++ def consume_task(self, name: str, expected: dict[str, object]) -> None:+ with self.celery.connection_for_read() as connection, SimpleQueue(connection, self.queue) as messages:+ message = messages.get(block=False)+ assert message.headers["task"] == self.tasks[name].name+ assert message.payload[0] == []+ assert message.payload[1] == expected+ message.ack()+ with pytest.raises(Empty):+ messages.get(block=False)+++def harness(flask_app_with_containers: Flask) -> Iterator[_Harness]:+ app = flask_app_with_containers+ services = app.extensions["application_services"]+ assert isinstance(services, ApplicationServices)+ redis = app.extensions["redis"]+ assert isinstance(redis, RedisClientWrapper)+ with app.app_context():+ engine = db.engine+ factory = sessionmaker(bind=engine, expire_on_commit=False)+ workspace = Tenant(name="Service API annotation workspace")+ foreign_workspace = Tenant(name="Other annotation workspace")+ account = make_account(account_id=str(uuid4()), email=f"{uuid4()}@example.com")+ account.last_active_at = naive_utc_now()+ target = make_app(app_id=str(uuid4()), tenant_id=workspace.id)+ other = make_app(app_id=str(uuid4()), tenant_id=workspace.id)+ foreign = make_app(app_id=str(uuid4()), tenant_id=foreign_workspace.id)+ api_key = ApiToken(app_id=target.id, tenant_id=workspace.id, type=ApiTokenType.APP, token=f"app-{uuid4()}")+ other_key = ApiToken(app_id=other.id, tenant_id=workspace.id, type=ApiTokenType.APP, token=f"app-{uuid4()}")+ with factory.begin() as session:+ session.add_all(+ [+ workspace,+ foreign_workspace,+ account,+ target,+ other,+ foreign,+ api_key,+ other_key,+ TenantAccountJoin(+ tenant_id=workspace.id, account_id=account.id, role=TenantAccountRole.OWNER, current=True+ ),+ DifySetup(version="test"),+ ]+ )+ queue = f"service-api-annotation-{uuid4()}"+ celery = Celery(queue, broker="memory://", set_as_current=False)+ celery.conf.update(task_default_queue=queue, task_ignore_result=True, task_publish_retry=False)+ tasks: dict[str, Task[..., None]] = {+ "add": celery.task(name=add_annotation_to_index_task.name, shared=False)(add_annotation_to_index_task.run),+ "update": celery.task(name=update_annotation_to_index_task.name, shared=False)(+ update_annotation_to_index_task.run+ ),+ "delete": celery.task(name=delete_annotation_index_task.name, shared=False)(delete_annotation_index_task.run),+ "enable": celery.task(name=enable_annotation_reply_task.name, shared=False)(enable_annotation_reply_task.run),+ "disable": celery.task(name=disable_annotation_reply_task.name, shared=False)(+ disable_annotation_reply_task.run+ ),+ }+ annotations = AnnotationRepository(session_factory=factory)+ app.extensions["application_services"] = replace(+ services,+ annotation_queries=annotations,+ annotation_commands=AnnotationCommandService(+ annotations=annotations,+ add_index=tasks["add"].delay,+ update_index=tasks["update"].delay,+ delete_index=tasks["delete"].delay,+ ),+ annotation_reply=AnnotationReplyService(+ apps=annotations,+ jobs=RedisAnnotationReplyJobRepository(redis=redis),+ enable_task=tasks["enable"].delay,+ disable_task=tasks["disable"].delay,+ ),+ )+ sessions: list[Session] = []+ publications: list[bool] = []+ logins: list[object] = []+ token_reads: list[str] = []++ def track(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:+ sessions.append(session)++ def observe_publish(**_kwargs: object) -> None:+ publications.append(all(not session.in_transaction() and not session.identity_map for session in sessions))++ def observe_login(_sender: object, user: object) -> None:+ logins.append(user)++ def observe_query(+ _connection: Connection,+ _cursor: object,+ statement: str,+ _parameters: object,+ _context: ExecutionContext,+ _executemany: bool,+ ) -> None:+ if statement.lstrip().upper().startswith("SELECT") and "api_tokens" in statement:+ token_reads.append(statement)++ event.listen(Session, "after_begin", track)+ event.listen(engine, "before_cursor_execute", observe_query)+ before_task_publish.connect(observe_publish, weak=True)+ user_logged_in.connect(observe_login, weak=True)+ _is_setup_completed.reset_success()+ try:+ with config_overrides_context(+ DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY,+ SECRET_KEY="service-api-annotation-passport-key-32-bytes",+ LOGIN_DISABLED=False,+ INIT_PASSWORD="",+ CONSOLE_API_URL="http://localhost",+ CONSOLE_WEB_URL="http://localhost",+ COOKIE_DOMAIN="",+ RBAC_ENABLED=False,+ ):+ with app.app_context():+ assert _is_setup_completed()+ db.session.remove()+ yield _Harness(+ app,+ services,+ account,+ target,+ other,+ foreign,+ api_key,+ other_key,+ factory,+ redis,+ celery,+ tasks,+ queue,+ sessions,+ publications,+ logins,+ token_reads,+ )+ finally:+ app.extensions["application_services"] = services+ del observe_publish+ user_logged_in.disconnect(observe_login)+ event.remove(Session, "after_begin", track)+ event.remove(engine, "before_cursor_execute", observe_query)+ with celery.connection_for_read() as connection, SimpleQueue(connection, queue) as messages:+ messages.clear()+ celery.close()+ _is_setup_completed.reset_success()+ with app.app_context():+ db.session.remove()++[email protected]("credential", ["key", "resource"])+def test_crud_publishes_scoped_index_tasks_after_sessions_close(harness: _Harness, credential: Credential) -> None:+ token = harness.credential(credential)+ binding_id = str(uuid4())+ with harness.factory.begin() as session:+ session.add(+ AppAnnotationSetting(+ app_id=harness.target.id,+ score_threshold=0.75,+ collection_binding_id=binding_id,+ created_user_id=harness.account.id,+ updated_user_id=harness.account.id,+ )+ )++ created = harness.request("POST", "/annotations", payload=_ANNOTATION_PAYLOAD, token=token)++ assert created.status_code == 201+ body = created.get_json()+ annotation_id = body["id"]+ assert str(UUID(annotation_id)) == annotation_id+ assert body == {+ "id": annotation_id,+ "question": "Question",+ "answer": "Answer",+ "hit_count": 0,+ "created_at": body["created_at"],+ }+ assert isinstance(body["created_at"], int)+ task_scope: dict[str, object] = {+ "annotation_id": annotation_id,+ "tenant_id": harness.target.tenant_id,+ "app_id": harness.target.id,+ "collection_binding_id": binding_id,+ }+ harness.consume_task("add", {**task_scope, "question": "Question"})+ listed = harness.request("GET", "/annotations", token=token)+ assert listed.status_code == 200+ assert listed.get_json() == {"data": [body], "has_more": False, "limit": 20, "total": 1, "page": 1}+ updated = harness.request(+ "PUT", f"/annotations/{annotation_id}", payload={"question": "", "answer": "Updated answer"}, token=token+ )+ assert updated.status_code == 200+ assert updated.get_json() == {**body, "question": "", "answer": "Updated answer"}+ harness.consume_task("update", {**task_scope, "question": "Updated answer"})+ with harness.factory() as session:+ annotation = session.get(MessageAnnotation, annotation_id)+ assert annotation is not None+ assert (annotation.question, annotation.content, annotation.account_id) == (+ "",+ "Updated answer",+ harness.account.id,+ )++ removed = harness.request("DELETE", f"/annotations/{annotation_id}", token=token)++ assert removed.status_code == 204+ assert removed.data == b""+ harness.consume_task("delete", task_scope)+ assert harness.publications == [True, True, True]+ with harness.factory() as session:+ assert session.get(MessageAnnotation, annotation_id) is None+ assert harness.request("GET", "/annotations", token=token).get_json()["total"] == 0++[email protected](("question", "answer"), [("", ""), ("", "Answer"), ("Question", "")])+def test_create_preserves_service_api_empty_strings(harness: _Harness, question: str, answer: str) -> None:+ response = harness.request("POST", "/annotations", payload={"question": question, "answer": answer})++ assert response.status_code == 201+ body = response.get_json()+ assert body["question"] == question+ assert body["answer"] == answer+ with harness.factory() as session:+ annotation = session.get(MessageAnnotation, body["id"])+ assert annotation is not None+ assert (annotation.question, annotation.content, annotation.account_id) == (+ question,+ answer,+ harness.account.id,+ )+ assert harness.publications == []+++def test_list_scopes_annotations_and_preserves_pagination_and_literal_search(harness: _Harness) -> None:+ with harness.factory.begin() as session:+ session.add_all(+ MessageAnnotation(app_id=app_id, question=question, content="Answer", account_id=harness.account.id)+ for app_id, question in (+ (harness.target.id, "100% complete"),+ (harness.target.id, "Ordinary question"),+ (harness.other.id, "Other app 100% complete"),+ (harness.foreign.id, "Foreign workspace 100% complete"),+ )+ )++ first = harness.request("GET", "/annotations?page=1&limit=1").get_json()+ second = harness.request("GET", "/annotations?page=2&limit=1").get_json()+ assert (first["total"], first["page"], first["limit"], first["has_more"]) == (2, 1, 1, True)+ assert (second["total"], second["page"], second["limit"], second["has_more"]) == (2, 2, 1, False)+ assert first["data"][0]["id"] != second["data"][0]["id"]+ filtered = harness.request("GET", "/annotations?keyword=%25&limit=999").get_json()+ assert (filtered["total"], filtered["limit"], filtered["has_more"]) == (1, 100, False)+ assert filtered["data"][0]["question"] == "100% complete"++[email protected]("method", ["PUT", "DELETE"])[email protected]("scope", ["app", "tenant"])+def test_annotation_mutations_reject_other_owners(harness: _Harness, method: str, scope: str) -> None:+ annotation = MessageAnnotation(+ app_id=harness.other.id if scope == "app" else harness.foreign.id,+ question="Other question",+ content="Other answer",+ account_id=harness.account.id,+ )+ with harness.factory.begin() as session:+ session.add(annotation)++ response = harness.request(method, f"/annotations/{annotation.id}", payload=_ANNOTATION_PAYLOAD)++ assert response.status_code == 404+ assert response.get_json()["code"] == "not_found"+ assert harness.publications == []+ with harness.factory() as session:+ preserved = session.get(MessageAnnotation, annotation.id)+ assert preserved is not None+ assert (preserved.question, preserved.content) == ("Other question", "Other answer")++[email protected](("method", "path"), _ENDPOINTS)[email protected]("credential_state", ["missing", "invalid", "revoked"])+def test_every_handler_requires_a_valid_bearer_key(+ harness: _Harness, method: str, path: str, credential_state: str+) -> None:+ authorization = "" if credential_state == "missing" else f"Bearer app-{uuid4()}"+ if credential_state == "revoked":+ assert harness.request("GET", "/annotations").status_code == 200+ harness.services.app_api_keys.delete_key(harness.context, harness.target.id, harness.api_key.id)+ authorization = f"Bearer {harness.api_key.token}"++ response = harness.request(+ method, path.format(id=uuid4()), payload={**_ANNOTATION_PAYLOAD, **_REPLY_PAYLOAD}, authorization=authorization+ )++ assert response.status_code == 401+ assert response.get_json()["code"] == "unauthorized"+ assert harness.publications == []+++def test_api_key_cache_hit_avoids_token_query_and_revocation_invalidates_cache(harness: _Harness) -> None:+ assert harness.request("GET", "/annotations").status_code == 200+ assert len(harness.token_reads) == 1+ cached = ApiTokenCache.get(harness.api_key.token, "app")+ assert cached is not None+ assert (cached.id, cached.app_id) == (harness.api_key.id, harness.target.id)++ assert harness.request("GET", "/annotations").status_code == 200+ assert len(harness.token_reads) == 1+ harness.services.app_api_keys.delete_key(harness.context, harness.target.id, harness.api_key.id)++ response = harness.request("GET", "/annotations")+ assert response.status_code == 401+ assert response.get_json()["code"] == "unauthorized"+++ ("state", "status", "code"),+ [+ ("app_missing", 403, "app_not_found"),+ ("api_disabled", 403, "app_api_disabled"),+ ("app_abnormal", 403, "app_abnormal_status"),+ ("tenant_missing", 403, "workspace_not_found"),+ ("tenant_archived", 403, "workspace_archived"),+ ("owner_role", 401, "unauthorized"),+ ("owner_missing", 401, "unauthorized"),+ ],+)+def test_cached_key_rechecks_application_workspace_and_owner(+ harness: _Harness, state: str, status: int, code: str+) -> None:+ assert harness.request("GET", "/annotations").status_code == 200+ with harness.factory.begin() as session:+ match state:+ case "app_missing":+ session.execute(delete(App).where(App.id == harness.target.id))+ case "api_disabled":+ session.execute(update(App).where(App.id == harness.target.id).values(enable_api=False))+ case "app_abnormal":+ session.execute(text("UPDATE apps SET status = 'disabled' WHERE id = :id"), {"id": harness.target.id})+ case "tenant_missing":+ session.execute(delete(Tenant).where(Tenant.id == harness.target.tenant_id))+ case "tenant_archived":+ session.execute(+ update(Tenant).where(Tenant.id == harness.target.tenant_id).values(status=TenantStatus.ARCHIVE)+ )+ case "owner_role":+ session.execute(+ update(TenantAccountJoin)+ .where(TenantAccountJoin.account_id == harness.account.id)+ .values(role=TenantAccountRole.EDITOR)+ )+ case "owner_missing":+ session.execute(delete(Account).where(Account.id == harness.account.id))++ response = harness.request("GET", "/annotations")++ assert response.status_code == status+ assert response.get_json()["code"] == code+ assert harness.publications == []++[email protected]("token_scope", ["matching", "legacy_missing", "foreign"])+def test_api_key_admission_checks_token_workspace_and_preserves_legacy_keys(+ harness: _Harness, token_scope: str+) -> None:+ tenant_id = {+ "matching": harness.target.tenant_id,+ "legacy_missing": None,+ "foreign": harness.foreign.tenant_id,+ }[token_scope]+ with harness.factory.begin() as session:+ session.execute(update(ApiToken).where(ApiToken.id == harness.api_key.id).values(tenant_id=tenant_id))++ for _ in range(2):+ response = harness.request("POST", "/annotations", payload=_ANNOTATION_PAYLOAD)+ if token_scope == "foreign":+ assert response.status_code == 403+ assert response.get_json()["code"] == "app_not_found"+ else:+ assert response.status_code == 201+ with harness.factory() as session:+ annotation = session.get(MessageAnnotation, response.get_json()["id"])+ assert annotation is not None+ assert (annotation.app_id, annotation.account_id) == (harness.target.id, harness.account.id)+ assert len(harness.token_reads) == 1+ assert harness.publications == []+++def test_api_key_admission_does_not_require_console_account_login_state(harness: _Harness) -> None:+ with harness.factory.begin() as session:+ session.execute(update(Account).where(Account.id == harness.account.id).values(status=AccountStatus.BANNED))+ session.execute(+ update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(current=False)+ )++ response = harness.request("POST", "/annotations", payload=_ANNOTATION_PAYLOAD)++ assert response.status_code == 201+ assert harness.publications == []+++def test_resource_token_selection_and_revocation(harness: _Harness) -> None:+ resource = harness.resource_token(harness.target.id, harness.other.id)+ missing_selection = harness.request("GET", "/annotations", token=resource.token)+ assert missing_selection.status_code == 400+ assert missing_selection.get_json()["code"] == "invalid_param"+ forbidden = harness.request("GET", "/annotations", token=resource.token, app_id=harness.foreign.id)+ assert forbidden.status_code == 403+ assert forbidden.get_json()["code"] == "forbidden"+ created = harness.request(+ "POST", "/annotations", token=resource.token, app_id=harness.other.id, payload=_ANNOTATION_PAYLOAD+ )+ assert created.status_code == 201+ assert (+ harness.request("GET", "/annotations", token=resource.token, app_id=harness.target.id).get_json()["total"] == 0+ )+ assert (+ harness.request("GET", "/annotations", token=resource.token, app_id=harness.other.id).get_json()["total"] == 1+ )++ harness.services.resource_access_tokens.delete_relation(+ harness.context, token_id=resource.token_id, relation_id=resource.rows[0].relation_id+ )++ assert harness.request("GET", "/annotations", token=resource.token, app_id=harness.other.id).status_code == 401++[email protected]("action", ["enable", "disable"])[email protected]("credential", ["key", "resource"])+def test_reply_jobs_share_console_ownership_and_publish_after_sessions_close(+ harness: _Harness, action: AnnotationReplyAction, credential: Credential+) -> None:+ token = harness.credential(credential)+ response = harness.request("POST", f"/annotation-reply/{action}", payload=_REPLY_PAYLOAD, token=token)++ assert response.status_code == 200+ body = response.get_json()+ job_id = body["job_id"]+ assert str(UUID(job_id)) == job_id+ assert body == {"job_id": job_id, "job_status": "waiting"}+ assert harness.redis.get(annotation_reply_job_owner_key(action=action, job_id=job_id)) == (+ f"{harness.target.tenant_id}:{harness.target.id}".encode()+ )+ expected: dict[str, object] = {"job_id": job_id, "app_id": harness.target.id, "tenant_id": harness.target.tenant_id}+ if action == "enable":+ expected.update(_REPLY_PAYLOAD, user_id=harness.account.id)+ harness.consume_task(action, expected)+ assert harness.publications == [True]+ status = harness.request("GET", f"/annotation-reply/{action}/status/{job_id}", token=token)+ assert status.status_code == 200+ expected_status = {"job_id": job_id, "job_status": "waiting", "error_msg": ""}+ assert status.get_json() == expected_status++ client = harness.app.test_client()+ csrf = generate_csrf_token(harness.account.id)+ client.set_cookie(COOKIE_NAME_CSRF_TOKEN, csrf)+ console_status = client.get(+ f"/console/api/apps/{harness.target.id}/annotation-reply/{action}/status/{job_id}",+ headers={+ HEADER_NAME_CSRF_TOKEN: csrf,+ "Authorization": f"Bearer {PassportService().issue({'user_id': harness.account.id})}",+ },+ )+ assert console_status.status_code == 200+ assert console_status.get_json() == expected_status++[email protected]("action", ["enable", "disable"])[email protected]("mismatch", ["app", "tenant", "action", "legacy", "missing"])+def test_reply_status_rejects_unverifiable_ownership(+ harness: _Harness, action: AnnotationReplyAction, mismatch: str+) -> None:+ job_id = str(uuid4())+ stored_action: AnnotationReplyAction = (+ ("disable" if action == "enable" else "enable") if mismatch == "action" else action+ )+ if mismatch != "missing":+ harness.redis.set(f"{stored_action}_app_annotation_job_{job_id}", "completed")+ if mismatch not in {"legacy", "missing"}:+ tenant_id = str(uuid4()) if mismatch == "tenant" else harness.target.tenant_id+ harness.redis.set(+ annotation_reply_job_owner_key(action=stored_action, job_id=job_id), f"{tenant_id}:{harness.target.id}"+ )++ response = harness.request(+ "GET",+ f"/annotation-reply/{action}/status/{job_id}",+ token=harness.other_key.token if mismatch == "app" else harness.api_key.token,+ )++ assert response.status_code == 404+ assert response.get_json()["code"] == "not_found"+ assert response.get_json()["message"] == "The job does not exist."+ assert harness.publications == []diff --git a/api/tests/e2e_tests/containers/services/test_annotation_reply_compatibility.py b/api/tests/e2e_tests/containers/services/test_annotation_reply_compatibility.pydeleted file mode 100644index 0e2cfd89d90..00000000000--- a/api/tests/e2e_tests/containers/services/test_annotation_reply_compatibility.py+++ /dev/null@@ -1,176 +0,0 @@-"""Legacy Service API producers create owner-scoped jobs without changing their task payloads."""--from collections.abc import Iterator-from queue import Empty-from uuid import uuid4--import pytest-from celery import Celery-from celery.signals import before_task_publish-from flask import Flask, g-from kombu.exceptions import SerializerNotInstalled-from kombu.simple import SimpleQueue-from sqlalchemy.orm import Session--from extensions.ext_redis import redis_client-from models.account import Account, Tenant, TenantAccountJoin, TenantAccountRole-from repositories.annotation_reply_job_repository import (- RedisAnnotationReplyJobRepository,- annotation_reply_job_owner_key,-)-from services.annotation_reply_service import AnnotationReplyAction, AnnotationReplyJob-from services.annotation_service import AnnotationJobStatusDict, AppAnnotationService-from tasks.annotation.disable_annotation_reply_task import disable_annotation_reply_task-from tasks.annotation.enable_annotation_reply_task import enable_annotation_reply_task---def memory_celery(flask_app_with_containers: Flask) -> Iterator[Celery]:- previous = flask_app_with_containers.extensions["celery"]- assert isinstance(previous, Celery)- queue = f"annotation-legacy-{uuid4()}"- celery = Celery(queue, broker="memory://", set_as_current=False)- celery.conf.update(task_default_queue=queue, task_ignore_result=True, task_publish_retry=False)- celery.task(name=enable_annotation_reply_task.name, shared=False, lazy=False)(enable_annotation_reply_task.run)- celery.task(name=disable_annotation_reply_task.name, shared=False, lazy=False)(disable_annotation_reply_task.run)- celery.set_current()- try:- yield celery- finally:- previous.set_current()- with celery.connection_for_read() as connection, SimpleQueue(connection, queue) as messages:- messages.clear()- celery.close()---def legacy_actor(db_session_with_containers: Session) -> tuple[Account, Tenant]:- account = Account(name="Annotation tester", email=f"annotation-{uuid4()}@example.com")- tenant = Tenant(name="Annotation workspace")- db_session_with_containers.add_all([account, tenant])- db_session_with_containers.flush()- db_session_with_containers.add(- TenantAccountJoin(account_id=account.id, tenant_id=tenant.id, role=TenantAccountRole.OWNER)- )- db_session_with_containers.commit()- account.set_tenant_id_with_session(tenant.id, session=db_session_with_containers)- return account, tenant---def _start_job(action: AnnotationReplyAction, app_id: str) -> AnnotationJobStatusDict:- if action == "enable":- return AppAnnotationService.enable_app_annotation(- {"score_threshold": 0.8, "embedding_provider_name": "provider", "embedding_model_name": "model"},- app_id,- )- return AppAnnotationService.disable_app_annotation(app_id)--[email protected]("action", ["enable", "disable"])-def test_legacy_producer_deduplicates_requests_and_preserves_task_arguments(- action: AnnotationReplyAction,- flask_app_with_containers: Flask,- legacy_actor: tuple[Account, Tenant],- memory_celery: Celery,-) -> None:- account, tenant = legacy_actor- app_id = str(uuid4())-- with flask_app_with_containers.test_request_context():- g._login_user = account- result = _start_job(action, app_id)- repeated = _start_job(action, app_id)-- if action == "enable":- expected_args = [result["job_id"], app_id, account.id, tenant.id, 0.8, "provider", "model"]- task_name = enable_annotation_reply_task.name- else:- expected_args = [result["job_id"], app_id, tenant.id]- task_name = disable_annotation_reply_task.name-- assert result["job_status"] == "waiting"- assert repeated == {"job_id": result["job_id"], "job_status": "processing"}- jobs = RedisAnnotationReplyJobRepository(redis=redis_client)- assert jobs.get(tenant_id=tenant.id, app_id=app_id, action=action, job_id=result["job_id"]) == AnnotationReplyJob(- job_id=result["job_id"], job_status="waiting"- )- for key in (- f"{action}_app_annotation_{app_id}",- f"{action}_app_annotation_job_{result['job_id']}",- annotation_reply_job_owner_key(action=action, job_id=result["job_id"]),- ):- assert redis_client.ttl(key) > 0- with (- memory_celery.connection_for_read() as connection,- SimpleQueue(connection, memory_celery.conf.task_default_queue) as messages,- ):- message = messages.get(block=False)- assert message.headers["task"] == task_name- assert message.payload[0] == expected_args- assert message.payload[1] == {}- message.ack()- with pytest.raises(Empty):- messages.get(block=False)--[email protected]("action", ["enable", "disable"])-def test_legacy_producer_retains_failed_job_already_observed_by_another_request(- action: AnnotationReplyAction,- flask_app_with_containers: Flask,- legacy_actor: tuple[Account, Tenant],- memory_celery: Celery,-) -> None:- account, tenant = legacy_actor- app_id = str(uuid4())- task_name = enable_annotation_reply_task.name if action == "enable" else disable_annotation_reply_task.name- task = memory_celery.tasks[task_name]- previous_serializer = task.serializer- observed: list[AnnotationJobStatusDict] = []-- def observe_pending(**_kwargs: object) -> None:- # Interleave a second real request after reservation but before serialization fails.- with flask_app_with_containers.test_request_context():- g._login_user = account- observed.append(_start_job(action, app_id))-- before_task_publish.connect(observe_pending, sender=task_name, weak=True)- task.serializer = "annotation-test-unregistered-serializer"- vars(task).pop("_exec_options", None)- try:- with flask_app_with_containers.test_request_context():- g._login_user = account- with pytest.raises(SerializerNotInstalled) as failure:- _start_job(action, app_id)- finally:- del observe_pending- task.serializer = previous_serializer- # Celery caches publication options even when serialization fails.- vars(task).pop("_exec_options", None)-- assert len(observed) == 1- job_id = observed[0]["job_id"]- assert observed[0]["job_status"] == "processing"- jobs = RedisAnnotationReplyJobRepository(redis=redis_client)- expected = AnnotationReplyJob(job_id=job_id, job_status="error", error_msg=str(failure.value))- assert jobs.get(tenant_id=tenant.id, app_id=app_id, action=action, job_id=job_id) == expected- for key in (- f"{action}_app_annotation_job_{job_id}",- f"{action}_app_annotation_error_{job_id}",- annotation_reply_job_owner_key(action=action, job_id=job_id),- ):- assert 0 < redis_client.ttl(key) <= 600- assert redis_client.get(f"{action}_app_annotation_{app_id}") is None- with (- memory_celery.connection_for_read() as connection,- SimpleQueue(connection, memory_celery.conf.task_default_queue) as messages,- ):- with pytest.raises(Empty):- messages.get(block=False)-- with flask_app_with_containers.test_request_context():- g._login_user = account- retry = _start_job(action, app_id)- assert retry["job_status"] == "waiting"- assert retry["job_id"] != job_id- assert redis_client.get(f"{action}_app_annotation_{app_id}") == retry["job_id"].encode()- assert jobs.get(tenant_id=tenant.id, app_id=app_id, action=action, job_id=job_id) == expecteddiff --git a/api/tests/e2e_tests/containers/services/test_annotation_service.py b/api/tests/e2e_tests/containers/services/test_annotation_service.pyindex 82807e537f0..eb43e99d71f 100644--- a/api/tests/e2e_tests/containers/services/test_annotation_service.py+++ b/api/tests/e2e_tests/containers/services/test_annotation_service.py@@ -1,819 +1,66 @@-from unittest.mock import create_autospec, patch+"""Real database coverage for annotation runtime reads and hit recording."""-import pytest-from faker import Faker+from uuid import uuid4++from sqlalchemy import selectfrom sqlalchemy.orm import Session-from werkzeug.exceptions import NotFound-from models import Accountfrom models.enums import ConversationFromSource-from models.model import MessageAnnotation+from models.model import AppAnnotationHitHistory, MessageAnnotationfrom services.annotation_service import AppAnnotationService-from services.app_ref_service import AnnotationRef, AppRef-from services.app_service import AppService, CreateAppParams-from tests.e2e_tests.containers.helpers import generate_valid_password---class TestAnnotationService:- """Integration tests for AnnotationService using testcontainers."""-- @pytest.fixture- def mock_external_service_dependencies(self):- """Mock setup for external service dependencies."""- with (- patch("services.account.login_adapters.SystemFeatureService") as mock_account_feature_service,- patch("services.annotation_service.add_annotation_to_index_task") as mock_add_task,- patch("services.annotation_service.update_annotation_to_index_task") as mock_update_task,- patch("services.annotation_service.delete_annotation_index_task") as mock_delete_task,- patch("services.annotation_service.enable_annotation_reply_task") as mock_enable_task,- patch("services.annotation_service.disable_annotation_reply_task") as mock_disable_task,- patch("services.annotation_service.current_account_with_tenant") as mock_current_account_with_tenant,- ):- # Setup default mock returns- mock_add_task.delay.return_value = None- mock_update_task.delay.return_value = None- mock_delete_task.delay.return_value = None- mock_enable_task.delay.return_value = None- mock_disable_task.delay.return_value = None-- # Create mock user that will be returned by current_account_with_tenant- mock_user = create_autospec(Account, instance=True)-- yield {- "account_feature_service": mock_account_feature_service,- "add_task": mock_add_task,- "update_task": mock_update_task,- "delete_task": mock_delete_task,- "enable_task": mock_enable_task,- "disable_task": mock_disable_task,- "current_account_with_tenant": mock_current_account_with_tenant,- "current_user": mock_user,- }-- def _create_test_app_and_account(self, db_session_with_containers: Session, mock_external_service_dependencies):- """- Helper method to create a test app and account for testing.-- Args:- db_session_with_containers: Database session from testcontainers infrastructure- mock_external_service_dependencies: Mock dependencies-- Returns:- tuple: (app, account) - Created app and account instances- """- fake = Faker()-- # Setup mocks for account creation- mock_external_service_dependencies["account_feature_service"].is_registration_allowed.return_value = True-- # Create account and tenant first- from tests.e2e_tests.containers.helpers import accounts as account_fixtures-- account = account_fixtures.create_account(- email=fake.email(),- name=fake.name(),- interface_language="en-US",- password=generate_valid_password(fake),- session=db_session_with_containers,- )- account_fixtures.create_owner_workspace(account, name=fake.company(), session=db_session_with_containers)- tenant = account.current_tenant-- # Setup app creation arguments- app_args = CreateAppParams(- name=fake.company(),- description=fake.text(max_nb_chars=100),- mode="chat",- icon_type="emoji",- icon="🤖",- icon_background="#FF6B6B",- api_rph=100,- api_rpm=10,- )-- # Create app- app_service = AppService()- app = app_service.create_app(tenant.id, app_args, account, session=db_session_with_containers)-- # Setup current_user mock- self._mock_current_user(mock_external_service_dependencies, account.id, tenant.id)-- return app, account-- def _mock_current_user(self, mock_external_service_dependencies, account_id, tenant_id):- """- Helper method to mock the current user for testing.- """- mock_external_service_dependencies["current_user"].id = account_id- mock_external_service_dependencies["current_user"].current_tenant_id = tenant_id- # Configure current_account_with_tenant to return (user, tenant_id)- mock_external_service_dependencies["current_account_with_tenant"].return_value = (- mock_external_service_dependencies["current_user"],- tenant_id,- )-- @staticmethod- def _annotation_ref(app, annotation_id: str) -> AnnotationRef:- return AnnotationRef(app=AppRef(tenant_id=app.tenant_id, app_id=app.id), annotation_id=annotation_id)-- def test_insert_app_annotation_directly_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful direct insertion of app annotation.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Setup annotation data- annotation_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }-- # Insert annotation directly- annotation = AppAnnotationService.insert_app_annotation_directly(- annotation_args, app.id, session=db_session_with_containers- )-- # Verify annotation was created correctly- assert annotation.app_id == app.id- assert annotation.question == annotation_args["question"]- assert annotation.content == annotation_args["answer"]- assert annotation.account_id == account.id- assert annotation.hit_count == 0- assert annotation.id is not None-- # Verify annotation was saved to database-- db_session_with_containers.refresh(annotation)- assert annotation.id is not None-- # Verify add_annotation_to_index_task was called (when annotation setting exists)- # Note: In this test, no annotation setting exists, so task should not be called- mock_external_service_dependencies["add_task"].delay.assert_not_called()-- def test_insert_app_annotation_directly_requires_question(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Question must be provided when inserting annotations directly.- """- fake = Faker()- app, _ = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- annotation_args = {- "question": None,- "answer": fake.text(max_nb_chars=200),- }-- with pytest.raises(ValueError, match="'question' is required"):- AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id, db_session_with_containers)-- def test_insert_app_annotation_directly_app_not_found(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test direct insertion of app annotation when app is not found.- """- fake = Faker()- non_existent_app_id = fake.uuid4()-- # Mock random current user to avoid dependency issues- self._mock_current_user(mock_external_service_dependencies, fake.uuid4(), fake.uuid4())-- # Setup annotation data- annotation_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }-- # Try to insert annotation with non-existent app- with pytest.raises(NotFound, match="App not found"):- AppAnnotationService.insert_app_annotation_directly(- annotation_args, non_existent_app_id, session=db_session_with_containers- )-- def test_update_app_annotation_directly_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful direct update of app annotation.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # First, create an annotation- original_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }- annotation = AppAnnotationService.insert_app_annotation_directly(- original_args, app.id, session=db_session_with_containers- )-- # Update the annotation- updated_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }- updated_annotation = AppAnnotationService.update_app_annotation_directly(- updated_args,- self._annotation_ref(app, annotation.id),- db_session_with_containers,- )-- # Verify annotation was updated correctly- assert updated_annotation.id == annotation.id- assert updated_annotation.app_id == app.id- assert updated_annotation.question == updated_args["question"]- assert updated_annotation.content == updated_args["answer"]- assert updated_annotation.account_id == account.id-- # Verify original values were changed- assert updated_annotation.question != original_args["question"]- assert updated_annotation.content != original_args["answer"]-- # Verify update_annotation_to_index_task was called (when annotation setting exists)- # Note: In this test, no annotation setting exists, so task should not be called- mock_external_service_dependencies["update_task"].delay.assert_not_called()-- def test_get_annotation_list_by_app_id_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful retrieval of annotation list by app ID.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create multiple annotations- annotations = []- for i in range(3):- annotation_args = {- "question": f"Question {i}: {fake.sentence()}",- "answer": f"Answer {i}: {fake.text(max_nb_chars=200)}",- }- annotation = AppAnnotationService.insert_app_annotation_directly(- annotation_args, app.id, session=db_session_with_containers- )- annotations.append(annotation)-- # Get annotation list- annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(- app.id, page=1, limit=10, keyword="", session=db_session_with_containers- )-- # Verify results- assert len(annotation_list) == 3- assert total == 3-- # Verify all annotations belong to the correct app- for annotation in annotation_list:- assert annotation.app_id == app.id- assert annotation.account_id == account.id-- def test_get_annotation_list_by_app_id_with_keyword(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test retrieval of annotation list with keyword search.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create annotations with specific keywords- unique_keyword = f"unique_{fake.uuid4()[:8]}"- annotation_args = {- "question": f"Question with {unique_keyword} keyword",- "answer": f"Answer with {unique_keyword} keyword",- }- AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id, db_session_with_containers)- # Create another annotation without the keyword- other_args = {- "question": "Different question without special term",- "answer": "Different answer without special content",- }-- AppAnnotationService.insert_app_annotation_directly(other_args, app.id, db_session_with_containers)-- # Search with keyword- annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(- app.id, page=1, limit=10, keyword=unique_keyword, session=db_session_with_containers- )-- # Verify only matching annotations are returned- assert len(annotation_list) == 1- assert total == 1- assert unique_keyword in annotation_list[0].question or unique_keyword in annotation_list[0].content-- def test_get_annotation_list_by_app_id_with_special_characters_in_keyword(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- r"""- Test retrieval of annotation list with special characters in keyword to verify SQL injection prevention.-- This test verifies:- - Special characters (%, _, \) in keyword are properly escaped- - Search treats special characters as literal characters, not wildcards- - SQL injection via LIKE wildcards is prevented- """- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create annotations with special characters in content- annotation_with_percent = {- "question": "Question with 50% discount",- "answer": "Answer about 50% discount offer",- }- AppAnnotationService.insert_app_annotation_directly(annotation_with_percent, app.id, db_session_with_containers)-- annotation_with_underscore = {- "question": "Question with test_data",- "answer": "Answer about test_data value",- }- AppAnnotationService.insert_app_annotation_directly(- annotation_with_underscore, app.id, session=db_session_with_containers- )-- annotation_with_backslash = {- "question": "Question with path\\to\\file",- "answer": "Answer about path\\to\\file location",- }- AppAnnotationService.insert_app_annotation_directly(- annotation_with_backslash, app.id, session=db_session_with_containers- )-- # Create annotation that should NOT match (contains % but as part of different text)- annotation_no_match = {- "question": "Question with 100% different",- "answer": "Answer about 100% different content",- }- AppAnnotationService.insert_app_annotation_directly(annotation_no_match, app.id, db_session_with_containers)-- # Test 1: Search with % character - should find exact match only- annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(- app.id, page=1, limit=10, keyword="50%", session=db_session_with_containers- )- assert total == 1- assert len(annotation_list) == 1- assert "50%" in annotation_list[0].question or "50%" in annotation_list[0].content-- # Test 2: Search with _ character - should find exact match only- annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(- app.id, page=1, limit=10, keyword="test_data", session=db_session_with_containers- )- assert total == 1- assert len(annotation_list) == 1- assert "test_data" in annotation_list[0].question or "test_data" in annotation_list[0].content-- # Test 3: Search with \ character - should find exact match only- annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(- app.id, page=1, limit=10, keyword="path\\to\\file", session=db_session_with_containers- )- assert total == 1- assert len(annotation_list) == 1- assert "path\\to\\file" in annotation_list[0].question or "path\\to\\file" in annotation_list[0].content-- # Test 4: Search with % should NOT match 100% (verifies escaping works)- annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(- app.id, page=1, limit=10, keyword="50%", session=db_session_with_containers- )- # Should only find the 50% annotation, not the 100% one- assert total == 1- assert all("50%" in (item.question or "") or "50%" in (item.content or "") for item in annotation_list)-- def test_get_annotation_list_by_app_id_app_not_found(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test retrieval of annotation list when app is not found.- """- fake = Faker()- non_existent_app_id = fake.uuid4()-- # Mock random current user to avoid dependency issues- self._mock_current_user(mock_external_service_dependencies, fake.uuid4(), fake.uuid4())-- # Try to get annotation list with non-existent app- with pytest.raises(NotFound, match="App not found"):- AppAnnotationService.get_annotation_list_by_app_id(- non_existent_app_id, page=1, limit=10, keyword="", session=db_session_with_containers- )-- def test_delete_app_annotation_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful deletion of app annotation.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create an annotation first- annotation_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }- annotation = AppAnnotationService.insert_app_annotation_directly(- annotation_args, app.id, session=db_session_with_containers- )- annotation_id = annotation.id-- # Delete the annotation- AppAnnotationService.delete_app_annotation(self._annotation_ref(app, annotation_id), db_session_with_containers)-- # Verify annotation was deleted-- deleted_annotation = (- db_session_with_containers.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()- )- assert deleted_annotation is None-- # Verify delete_annotation_index_task was called (when annotation setting exists)- # Note: In this test, no annotation setting exists, so task should not be called- mock_external_service_dependencies["delete_task"].delay.assert_not_called()-- def test_delete_app_annotation_annotation_not_found_for_wrong_app(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test deletion of app annotation when the annotation does not belong to the supplied app ref.- """- fake = Faker()- non_existent_app_id = fake.uuid4()- annotation_id = fake.uuid4()- app_ref = AnnotationRef(- app=AppRef(tenant_id=fake.uuid4(), app_id=non_existent_app_id),- annotation_id=annotation_id,- )-- # Mock random current user to avoid dependency issues- self._mock_current_user(mock_external_service_dependencies, fake.uuid4(), fake.uuid4())-- # Try to delete annotation with a ref that cannot match any annotation row- with pytest.raises(NotFound, match="Annotation not found"):- AppAnnotationService.delete_app_annotation(app_ref, db_session_with_containers)-- def test_delete_app_annotation_annotation_not_found(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test deletion of app annotation when annotation is not found.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)- non_existent_annotation_id = fake.uuid4()-- # Try to delete non-existent annotation- with pytest.raises(NotFound, match="Annotation not found"):- AppAnnotationService.delete_app_annotation(- self._annotation_ref(app, non_existent_annotation_id),- db_session_with_containers,- )-- def test_enable_app_annotation_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful enabling of app annotation.- """- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Setup enable arguments- enable_args = {- "score_threshold": 0.8,- "embedding_provider_name": "openai",- "embedding_model_name": "text-embedding-ada-002",- }-- # Enable annotation- result = AppAnnotationService.enable_app_annotation(enable_args, app.id)-- # Verify result structure- assert "job_id" in result- assert "job_status" in result- assert result["job_status"] == "waiting"- assert result["job_id"] is not None-- # Verify task was called- mock_external_service_dependencies["enable_task"].delay.assert_called_once()-- def test_disable_app_annotation_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful disabling of app annotation.- """- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Disable annotation- result = AppAnnotationService.disable_app_annotation(app.id)-- # Verify result structure- assert "job_id" in result- assert "job_status" in result- assert result["job_status"] == "waiting"- assert result["job_id"] is not None-- # Verify task was called- mock_external_service_dependencies["disable_task"].delay.assert_called_once()-- def test_add_annotation_history_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful addition of annotation history.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create an annotation first- annotation_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }- annotation = AppAnnotationService.insert_app_annotation_directly(- annotation_args, app.id, session=db_session_with_containers- )-- # Get initial hit count- initial_hit_count = annotation.hit_count-- # Add annotation history- query = fake.sentence()- message_id = fake.uuid4()- score = 0.85-- AppAnnotationService.add_annotation_history(- annotation_id=annotation.id,- app_id=app.id,- annotation_question=annotation.question,- annotation_content=annotation.content,- query=query,- user_id=account.id,- message_id=message_id,- from_source=ConversationFromSource.CONSOLE,- score=score,- session=db_session_with_containers,- )-- # Verify hit count was incremented-- db_session_with_containers.refresh(annotation)- assert annotation.hit_count == initial_hit_count + 1-- # Verify history was created- from models.model import AppAnnotationHitHistory-- history = (- db_session_with_containers.query(AppAnnotationHitHistory)- .where(- AppAnnotationHitHistory.annotation_id == annotation.id, AppAnnotationHitHistory.message_id == message_id- )- .first()- )-- assert history is not None- assert history.app_id == app.id- assert history.account_id == account.id- assert history.question == query- assert history.score == score- assert history.source == "console"-- def test_get_annotation_by_id_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful retrieval of annotation by ID.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create an annotation- annotation_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }- created_annotation = AppAnnotationService.insert_app_annotation_directly(- annotation_args, app.id, session=db_session_with_containers- )-- # Get annotation by ID- retrieved_annotation = AppAnnotationService.get_annotation_by_id(- created_annotation.id, session=db_session_with_containers- )- # Verify annotation was retrieved correctly- assert retrieved_annotation is not None- assert retrieved_annotation.id == created_annotation.id- assert retrieved_annotation.app_id == app.id- assert retrieved_annotation.question == annotation_args["question"]- assert retrieved_annotation.content == annotation_args["answer"]- assert retrieved_annotation.account_id == account.id-- def test_insert_app_annotation_directly_with_setting_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful direct insertion of app annotation with annotation setting enabled.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create annotation setting first- from models.dataset import DatasetCollectionBinding- from models.model import AppAnnotationSetting-- # Create a collection binding first- collection_binding = DatasetCollectionBinding(- provider_name="openai",- model_name="text-embedding-ada-002",- type="annotation",- collection_name=f"annotation_collection_{fake.uuid4()}",- )- collection_binding.id = str(fake.uuid4())- db_session_with_containers.add(collection_binding)- db_session_with_containers.flush()-- # Create annotation setting- annotation_setting = AppAnnotationSetting(- app_id=app.id,- score_threshold=0.8,- collection_binding_id=collection_binding.id,- created_user_id=account.id,- updated_user_id=account.id,- )- db_session_with_containers.add(annotation_setting)- db_session_with_containers.commit()-- # Setup annotation data- annotation_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }-- # Insert annotation directly- annotation = AppAnnotationService.insert_app_annotation_directly(- annotation_args, app.id, session=db_session_with_containers- )-- # Verify annotation was created correctly- assert annotation.app_id == app.id- assert annotation.question == annotation_args["question"]- assert annotation.content == annotation_args["answer"]- assert annotation.account_id == account.id- assert annotation.hit_count == 0- assert annotation.id is not None-- # Verify add_annotation_to_index_task was called- mock_external_service_dependencies["add_task"].delay.assert_called_once()- call_args = mock_external_service_dependencies["add_task"].delay.call_args[0]- assert call_args[0] == annotation.id # annotation_id- assert call_args[1] == annotation_args["question"] # question- assert call_args[2] == account.current_tenant_id # tenant_id- assert call_args[3] == app.id # app_id- assert call_args[4] == collection_binding.id # collection_binding_id-- def test_update_app_annotation_directly_with_setting_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful direct update of app annotation with annotation setting enabled.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create annotation setting first- from models.dataset import DatasetCollectionBinding- from models.model import AppAnnotationSetting-- # Create a collection binding first- collection_binding = DatasetCollectionBinding(- provider_name="openai",- model_name="text-embedding-ada-002",- type="annotation",- collection_name=f"annotation_collection_{fake.uuid4()}",- )- collection_binding.id = str(fake.uuid4())- db_session_with_containers.add(collection_binding)- db_session_with_containers.flush()-- # Create annotation setting- annotation_setting = AppAnnotationSetting(- app_id=app.id,- score_threshold=0.8,- collection_binding_id=collection_binding.id,- created_user_id=account.id,- updated_user_id=account.id,- )- db_session_with_containers.add(annotation_setting)- db_session_with_containers.commit()-- # First, create an annotation- original_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }- annotation = AppAnnotationService.insert_app_annotation_directly(- original_args, app.id, session=db_session_with_containers- )-- # Reset mock to clear previous calls- mock_external_service_dependencies["update_task"].delay.reset_mock()-- # Update the annotation- updated_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }- updated_annotation = AppAnnotationService.update_app_annotation_directly(- updated_args,- self._annotation_ref(app, annotation.id),- db_session_with_containers,- )-- # Verify annotation was updated correctly- assert updated_annotation.id == annotation.id- assert updated_annotation.app_id == app.id- assert updated_annotation.question == updated_args["question"]- assert updated_annotation.content == updated_args["answer"]- assert updated_annotation.account_id == account.id-- # Verify original values were changed- assert updated_annotation.question != original_args["question"]- assert updated_annotation.content != original_args["answer"]-- # Verify update_annotation_to_index_task was called- mock_external_service_dependencies["update_task"].delay.assert_called_once()- call_args = mock_external_service_dependencies["update_task"].delay.call_args[0]- assert call_args[0] == annotation.id # annotation_id- assert call_args[1] == updated_args["question"] # question- assert call_args[2] == account.current_tenant_id # tenant_id- assert call_args[3] == app.id # app_id- assert call_args[4] == collection_binding.id # collection_binding_id-- def test_delete_app_annotation_with_setting_success(- self, db_session_with_containers: Session, mock_external_service_dependencies- ):- """- Test successful deletion of app annotation with annotation setting enabled.- """- fake = Faker()- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)-- # Create annotation setting first- from models.dataset import DatasetCollectionBinding- from models.model import AppAnnotationSetting-- # Create a collection binding first- collection_binding = DatasetCollectionBinding(- provider_name="openai",- model_name="text-embedding-ada-002",- type="annotation",- collection_name=f"annotation_collection_{fake.uuid4()}",- )- collection_binding.id = str(fake.uuid4())- db_session_with_containers.add(collection_binding)- db_session_with_containers.flush()-- # Create annotation setting- annotation_setting = AppAnnotationSetting(- app_id=app.id,- score_threshold=0.8,- collection_binding_id=collection_binding.id,- created_user_id=account.id,- updated_user_id=account.id,- )-- db_session_with_containers.add(annotation_setting)- db_session_with_containers.commit()-- # Create an annotation first- annotation_args = {- "question": fake.sentence(),- "answer": fake.text(max_nb_chars=200),- }- annotation = AppAnnotationService.insert_app_annotation_directly(- annotation_args, app.id, session=db_session_with_containers- )- annotation_id = annotation.id-- # Reset mock to clear previous calls- mock_external_service_dependencies["delete_task"].delay.reset_mock()-- # Delete the annotation- AppAnnotationService.delete_app_annotation(self._annotation_ref(app, annotation_id), db_session_with_containers)-- # Verify annotation was deleted- deleted_annotation = (- db_session_with_containers.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()- )- assert deleted_annotation is None- # Verify delete_annotation_index_task was called- mock_external_service_dependencies["delete_task"].delay.assert_called_once()- call_args = mock_external_service_dependencies["delete_task"].delay.call_args[0]- assert call_args[0] == annotation_id # annotation_id- assert call_args[1] == app.id # app_id- assert call_args[2] == account.current_tenant_id # tenant_id- assert call_args[3] == collection_binding.id # collection_binding_id+def _persist_annotation(session: Session) -> MessageAnnotation:+ annotation = MessageAnnotation(+ app_id=str(uuid4()), question="Runtime question", content="Runtime answer", account_id=str(uuid4())+ )+ session.add(annotation)+ session.flush()+ return annotation+++def test_add_annotation_history_success(db_session_with_containers: Session) -> None:+ annotation = _persist_annotation(db_session_with_containers)+ initial_hit_count = annotation.hit_count+ message_id = str(uuid4())++ AppAnnotationService.add_annotation_history(+ annotation_id=annotation.id,+ app_id=annotation.app_id,+ annotation_question=annotation.question,+ annotation_content=annotation.content,+ query="User question",+ user_id=annotation.account_id,+ message_id=message_id,+ from_source=ConversationFromSource.CONSOLE,+ score=0.85,+ session=db_session_with_containers,+ )++ db_session_with_containers.refresh(annotation)+ assert annotation.hit_count == initial_hit_count + 1+ history = db_session_with_containers.scalar(+ select(AppAnnotationHitHistory).where(+ AppAnnotationHitHistory.annotation_id == annotation.id,+ AppAnnotationHitHistory.message_id == message_id,+ )+ )+ assert history is not None+ assert history.app_id == annotation.app_id+ assert history.account_id == annotation.account_id+ assert history.question == "User question"+ assert history.annotation_question == annotation.question+ assert history.annotation_content == annotation.content+ assert history.score == 0.85+ assert history.source == "console"+++def test_get_annotation_by_id_success(db_session_with_containers: Session) -> None:+ annotation = _persist_annotation(db_session_with_containers)++ retrieved = AppAnnotationService.get_annotation_by_id(annotation.id, session=db_session_with_containers)++ assert retrieved is annotation+ assert retrieved.question == "Runtime question"+ assert retrieved.content == "Runtime answer"+ assert AppAnnotationService.get_annotation_by_id(str(uuid4()), session=db_session_with_containers) is Nonediff --git a/api/tests/e2e_tests/containers/services/test_conversation_service.py b/api/tests/e2e_tests/containers/services/test_conversation_service.pyindex af8d40db0fe..2ceeda1ad62 100644--- a/api/tests/e2e_tests/containers/services/test_conversation_service.py+++ b/api/tests/e2e_tests/containers/services/test_conversation_service.py@@ -13,8 +13,7 @@ from core.app.entities.app_invoke_entities import InvokeFromfrom models import TenantAccountRolefrom models.account import Account, Tenant, TenantAccountJoinfrom models.enums import ConversationFromSource, EndUserType-from models.model import App, Conversation, EndUser, Message, MessageAnnotation-from services.annotation_service import AppAnnotationService+from models.model import App, Conversation, EndUser, Messagefrom services.conversation_service import ConversationServicefrom services.errors.conversation import ConversationNotExistsErrorfrom services.errors.message import MessageNotExistsError@@ -482,128 +481,6 @@ class TestConversationServiceSummarization:assert conversation.name == generated_name-class TestConversationServiceMessageAnnotation:- """- Test message annotation operations.-- Tests AppAnnotationService operations for creating and managing- message annotations.- """-- @patch("services.annotation_service.current_account_with_tenant")- def test_get_annotation_list(self, mock_current_account, db_session_with_containers: Session):- """- Test retrieving paginated annotation list.-- Annotations can be retrieved in a paginated list for display in the UI.- """- # Arrange- app_model, account = ConversationServiceIntegrationTestDataFactory.create_app_and_account(- db_session_with_containers- )- annotations = [- MessageAnnotation(- app_id=app_model.id,- conversation_id=None,- message_id=None,- question=f"Question {i}",- content=f"Content {i}",- account_id=account.id,- )- for i in range(5)- ]- db_session_with_containers.add_all(annotations)- db_session_with_containers.commit()-- mock_current_account.return_value = (account, app_model.tenant_id)-- # Act- result_items, result_total = AppAnnotationService.get_annotation_list_by_app_id(- app_id=app_model.id, page=1, limit=10, keyword="", session=db_session_with_containers- )-- # Assert- assert len(result_items) == 5- assert result_total == 5-- @patch("services.annotation_service.current_account_with_tenant")- def test_get_annotation_list_with_keyword_search(self, mock_current_account, db_session_with_containers: Session):- """- Test retrieving annotations with keyword filtering.-- Annotations can be searched by question or content using case-insensitive matching.- """- # Arrange- app_model, account = ConversationServiceIntegrationTestDataFactory.create_app_and_account(- db_session_with_containers- )-- # Create annotations with searchable content- annotations = [- MessageAnnotation(- app_id=app_model.id,- conversation_id=None,- message_id=None,- question="What is machine learning?",- content="ML is a subset of AI",- account_id=account.id,- ),- MessageAnnotation(- app_id=app_model.id,- conversation_id=None,- message_id=None,- question="What is deep learning?",- content="Deep learning uses neural networks",- account_id=account.id,- ),- ]- db_session_with_containers.add_all(annotations)- db_session_with_containers.commit()-- mock_current_account.return_value = (account, app_model.tenant_id)-- # Act- result_items, result_total = AppAnnotationService.get_annotation_list_by_app_id(- app_id=app_model.id,- page=1,- limit=10,- keyword="machine", # Search keyword,- session=db_session_with_containers,- )-- # Assert- assert len(result_items) == 1- assert result_total == 1-- @patch("services.annotation_service.add_annotation_to_index_task")- @patch("services.annotation_service.current_account_with_tenant")- def test_insert_annotation_directly(self, mock_current_account, mock_add_task, db_session_with_containers: Session):- """- Test direct annotation insertion without message reference.-- This is used for bulk imports or manual annotation creation.- """- # Arrange- app_model, account = ConversationServiceIntegrationTestDataFactory.create_app_and_account(- db_session_with_containers- )-- mock_current_account.return_value = (account, app_model.tenant_id)-- args = {- "question": "What is natural language processing?",- "answer": "NLP is a field of AI focused on language understanding",- }-- # Act- result = AppAnnotationService.insert_app_annotation_directly(args, app_model.id, db_session_with_containers)-- # Assert- assert result.question == args["question"]- assert result.content == args["answer"]- mock_add_task.delay.assert_not_called()--class TestConversationServiceRetrieval:"""Test conversation and message retrieval operations.diff --git a/api/tests/e2e_tests/containers/services/test_message_more_like_this_generator.py b/api/tests/e2e_tests/containers/services/test_message_more_like_this_generator.pyindex 15b45ca5d5a..62d1378f4cf 100644--- a/api/tests/e2e_tests/containers/services/test_message_more_like_this_generator.py+++ b/api/tests/e2e_tests/containers/services/test_message_more_like_this_generator.py@@ -23,6 +23,7 @@ from yarl import URLfrom configs import dify_configfrom constants import HEADER_NAME_APP_CODE, HEADER_NAME_PASSPORT+from controllers.console.wraps import _is_setup_completedfrom core.helper.model_provider_cache import ProviderCredentialsCache, ProviderCredentialsCacheTypefrom core.plugin.entities.plugin_daemon import PluginModelProviderDeclarationfrom core.plugin.impl import base as plugin_base@@ -211,6 +212,9 @@ def scenario(headers,provider.id,)+ if console:+ # Keep the legacy bootstrap query outside the generation-session observations.+ assert _is_setup_completed()db.session.remove()return resultdiff --git a/api/tests/e2e_tests/contracts/controllers/service_api/app/test_annotation.py b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_annotation.pynew file mode 100644index 00000000000..c08e2ebb8b0--- /dev/null+++ b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_annotation.py@@ -0,0 +1,653 @@+"""Service API annotation contracts through real admission, SQLite and application wiring."""++import logging+from collections.abc import Callable, Iterator+from dataclasses import dataclass+from datetime import datetime, timedelta+from io import StringIO+from typing import Literal+from uuid import uuid4++import pytest+from flask import Blueprint, Flask, Response+from opentelemetry import trace+from opentelemetry.sdk.trace import TracerProvider+from opentelemetry.sdk.trace.export import SimpleSpanProcessor+from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter+from redis import Redis+from sqlalchemy import Connection, Engine, delete, event, select, update+from sqlalchemy.orm import Session, SessionTransaction, sessionmaker+from werkzeug.test import TestResponse++from controllers.service_api.app import annotation as annotation_module+from core.logging.context import clear_request_context+from core.logging.filters import IdentityContextFilter+from enums import DeploymentEdition+from enums.account import TenantAccountRole+from extensions.ext_application_services import build_application_services+from extensions.ext_redis import RedisClientWrapper+from extensions.otel.runtime import on_user_loaded, set_identity_span_attributes+from extensions.otel.semconv import DifySpanAttributes, GenAIAttributes+from libs.external_api import ExternalApi+from models import Account, App, EndUser, Tenant+from models.account import AccountStatus, TenantAccountJoin, TenantStatus+from models.dataset import Dataset+from models.enums import EndUserType+from models.model import AppAnnotationHitHistory, MessageAnnotation+from models.resource_access_token import (+ ResourceAccessToken,+ ResourceAccessTokenRelation,+ ResourceAccessTokenResourceType,+)+from tests.helpers.unit.model_factories import make_account, make_app++type _Operation = Literal["list", "create", "update", "delete", "reply", "status"]+_OPERATIONS: tuple[_Operation, ...] = ("list", "create", "update", "delete", "reply", "status")+_TIME = datetime(2026, 1, 2, 3, 4, 5)+_REPLY_PAYLOAD: dict[str, object] = {+ "score_threshold": 0.5,+ "embedding_provider_name": "openai",+ "embedding_model_name": "embedding",+}+++@dataclass(frozen=True)+class _Harness:+ app: Flask+ account: Account+ target: App+ annotation: MessageAnnotation+ token: ResourceAccessToken+ factory: sessionmaker[Session]+ sessions: list[Session]++ def request(+ self,+ operation: _Operation,+ *,+ payload: dict[str, object] | None = None,+ annotation_id: str | None = None,+ action: str = "invalid",+ query: dict[str, object] | None = None,+ authorization: str | None = "default",+ requested_app_id: str | None = None,+ ) -> TestResponse:+ routes = {+ "list": ("GET", "/v1/apps/annotations"),+ "create": ("POST", "/v1/apps/annotations"),+ "update": ("PUT", f"/v1/apps/annotations/{annotation_id or self.annotation.id}"),+ "delete": ("DELETE", f"/v1/apps/annotations/{annotation_id or self.annotation.id}"),+ "reply": ("POST", f"/v1/apps/annotation-reply/{action}"),+ "status": ("GET", f"/v1/apps/annotation-reply/{action}/status/{uuid4()}"),+ }+ method, path = routes[operation]+ headers = {}+ if authorization is not None:+ headers["Authorization"] = f"Bearer {self.token.token}" if authorization == "default" else authorization+ if requested_app_id is not None:+ headers["X-Dify-App-ID"] = requested_app_id+ response = self.app.test_client().open(path, method=method, json=payload, query_string=query, headers=headers)+ assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)+ if response.status_code == 204:+ assert response.data == b""+ else:+ assert response.headers["Content-Type"] == "application/json"+ assert int(response.headers["Content-Length"]) == len(response.data)+ return response+++def harness(+ config_overrides: Callable[..., None], sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session]+) -> Iterator[_Harness]:+ clear_request_context()+ config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY, RBAC_ENABLED=False)+ tenant = Tenant(name="Service API annotation workspace")+ account = make_account(account_id=str(uuid4()))+ target = make_app(app_id=str(uuid4()), tenant_id=tenant.id)+ annotation = MessageAnnotation(+ app_id=target.id, question="First question", content="First answer", account_id=account.id+ )+ annotation.created_at = _TIME+ annotation.hit_count = 3+ token = ResourceAccessToken(+ id=str(uuid4()),+ tenant_id=tenant.id,+ name="Annotation integration",+ track_id=uuid4().hex,+ token=f"sk-{uuid4()}",+ created_by=account.id,+ )+ with sqlite_session_factory.begin() as session:+ session.add_all(+ [+ tenant,+ account,+ target,+ annotation,+ token,+ TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, role=TenantAccountRole.OWNER),+ ResourceAccessTokenRelation(+ token_id=token.id, resource_type=ResourceAccessTokenResourceType.APP, app_id=target.id+ ),+ ]+ )+ factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)+ app = Flask(__name__)+ app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False)+ client = Redis()+ redis = RedisClientWrapper()+ redis.initialize(client)+ app.extensions["application_services"] = build_application_services(+ database_client=factory,+ deployment_edition=DeploymentEdition.COMMUNITY,+ initialization_password="",+ redis=redis,+ )+ blueprint = Blueprint("service_api_annotations", __name__, url_prefix="/v1")+ api = ExternalApi(blueprint)+ api.add_resource(annotation_module.AnnotationListApi, "/apps/annotations")+ api.add_resource(annotation_module.AnnotationUpdateDeleteApi, "/apps/annotations/<uuid:annotation_id>")+ api.add_resource(annotation_module.AnnotationReplyActionApi, "/apps/annotation-reply/<string:action>")+ api.add_resource(+ annotation_module.AnnotationReplyActionStatusApi, "/apps/annotation-reply/<string:action>/status/<uuid:job_id>"+ )+ app.register_blueprint(blueprint)+ sessions: list[Session] = []++ def track(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:+ sessions.append(session)++ event.listen(factory, "after_begin", track)+ try:+ yield _Harness(app, account, target, annotation, token, factory, sessions)+ finally:+ event.remove(factory, "after_begin", track)+ assert client.connection_pool._created_connections == 0+ client.close()+ clear_request_context()+++def _error(response: TestResponse, *, status: int, code: str, message: str | None = None) -> None:+ assert response.status_code == status+ body = response.get_json()+ assert body["code"] == code+ assert body["status"] == status+ if message is not None:+ assert body["message"] == message+ if status == 401:+ assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'+++def _annotation(harness: _Harness, index: int, *, app_id: str | None = None) -> MessageAnnotation:+ annotation = MessageAnnotation(+ app_id=app_id or harness.target.id,+ question=f"Question {index}",+ content=f"Answer {index}",+ account_id=harness.account.id,+ )+ annotation.created_at = _TIME + timedelta(seconds=index)+ return annotation+++def test_list_preserves_shape_order_pagination_and_app_scope(harness: _Harness) -> None:+ second = _annotation(harness, 1)+ other = make_app(app_id=str(uuid4()), tenant_id=harness.target.tenant_id)+ foreign = make_app(app_id=str(uuid4()), tenant_id=str(uuid4()))+ with harness.factory.begin() as session:+ session.add_all(+ [+ second,+ other,+ foreign,+ _annotation(harness, 2, app_id=other.id),+ _annotation(harness, 3, app_id=foreign.id),+ ]+ )+ response = harness.request("list", query={"limit": 1})+ assert response.status_code == 200+ assert response.get_json() == {+ "data": [+ {+ "id": second.id,+ "question": "Question 1",+ "answer": "Answer 1",+ "hit_count": 0,+ "created_at": int(second.created_at.timestamp()),+ }+ ],+ "has_more": True,+ "limit": 1,+ "total": 2,+ "page": 1,+ }+ last = harness.request("list", query={"limit": 1, "page": 2}).get_json()+ assert last["data"][0]["id"] == harness.annotation.id+ assert last["has_more"] is False+ assert harness.request("list", query={"limit": 1, "page": 3}).get_json() == {+ "data": [],+ "has_more": False,+ "limit": 1,+ "total": 2,+ "page": 3,+ }+++def test_list_caps_limit_at_100(harness: _Harness) -> None:+ with harness.factory.begin() as session:+ session.add_all(_annotation(harness, index) for index in range(1, 102))+ body = harness.request("list", query={"limit": 1000}).get_json()+ assert (body["page"], body["limit"], body["total"], body["has_more"]) == (1, 100, 102, True)+ assert len(body["data"]) == 100+++def test_keyword_escapes_wildcards_and_searches_question_and_answer(harness: _Harness) -> None:+ question, answer, decoy = (_annotation(harness, index) for index in range(1, 4))+ question.question = "50%_Case\\ marker"+ answer.content = "Answer has 50%_case\\ marker"+ decoy.question = "500XCase marker"+ with harness.factory.begin() as session:+ session.add_all([question, answer, decoy])+ body = harness.request("list", query={"keyword": "50%_case\\"}).get_json()+ assert body["total"] == 2+ assert [row["id"] for row in body["data"]] == [answer.id, question.id]++[email protected]("field", ["page", "limit"])[email protected]("value", ["abc", "1.5", "1e2", "", "0", "-1"])+def test_list_rejects_invalid_pagination(harness: _Harness, field: str, value: str) -> None:+ _error(harness.request("list", query={field: value}), status=422, code="unprocessable_entity")++[email protected](("question", "answer"), [("什么是人工智能?", "AI & ML <b>100%</b>"), ("", "")])+def test_create_persists_owner_and_preserves_empty_strings(harness: _Harness, question: str, answer: str) -> None:+ response = harness.request("create", payload={"question": question, "answer": answer})+ assert response.status_code == 201+ body = response.get_json()+ with harness.factory() as session:+ stored = session.get(MessageAnnotation, body["id"])+ assert stored is not None+ assert (stored.app_id, stored.account_id, stored.conversation_id, stored.message_id) == (+ harness.target.id,+ harness.account.id,+ None,+ None,+ )+ assert body == {+ "id": stored.id,+ "question": question,+ "answer": answer,+ "hit_count": 0,+ "created_at": int(stored.created_at.timestamp()),+ }+ assert (stored.question, stored.content) == (question, answer)++[email protected](("question", "answer"), [("Updated question", "Updated answer"), ("", "")])+def test_update_preserves_annotation_identity_and_hit_count(harness: _Harness, question: str, answer: str) -> None:+ response = harness.request("update", payload={"question": question, "answer": answer})+ assert response.status_code == 200+ assert response.get_json() == {+ "id": harness.annotation.id,+ "question": question,+ "answer": answer,+ "hit_count": 3,+ "created_at": int(_TIME.timestamp()),+ }+ with harness.factory() as session:+ stored = session.get(MessageAnnotation, harness.annotation.id)+ assert stored is not None+ assert (stored.question, stored.content, stored.account_id) == (question, answer, harness.account.id)++[email protected]("operation", ["create", "update"])+ "payload",+ [{}, {"question": "Q"}, {"answer": "A"}, {"question": None, "answer": "A"}, {"question": "Q", "answer": 42}],+)+def test_writes_validate_payload_without_changing_annotation(+ harness: _Harness, operation: _Operation, payload: dict[str, object]+) -> None:+ _error(harness.request(operation, payload=payload), status=422, code="unprocessable_entity")+ with harness.factory() as session:+ stored = session.scalars(select(MessageAnnotation)).one()+ assert (stored.question, stored.content) == ("First question", "First answer")+++def test_delete_removes_annotation_and_only_its_owned_hit_histories(harness: _Harness) -> None:+ histories = [+ AppAnnotationHitHistory(+ app_id=app_id,+ annotation_id=annotation_id,+ source="hit-testing",+ question="Asked",+ account_id=harness.account.id,+ score=0.9,+ message_id=str(uuid4()),+ annotation_question="Matched",+ annotation_content="Responded",+ )+ for app_id, annotation_id in [+ (harness.target.id, harness.annotation.id),+ (str(uuid4()), harness.annotation.id),+ (harness.target.id, str(uuid4())),+ ]+ ]+ with harness.factory.begin() as session:+ session.add_all(histories)+ assert harness.request("delete").status_code == 204+ with harness.factory() as session:+ assert session.get(MessageAnnotation, harness.annotation.id) is None+ assert set(session.scalars(select(AppAnnotationHitHistory.id))) == {histories[1].id, histories[2].id}++[email protected]("operation", ["update", "delete"])[email protected]("scope", ["missing", "other_app", "other_tenant"])+def test_annotation_mutation_enforces_app_and_tenant_scope(+ harness: _Harness, operation: _Operation, scope: str+) -> None:+ annotation_id = str(uuid4())+ if scope != "missing":+ other = make_app(+ app_id=str(uuid4()), tenant_id=harness.target.tenant_id if scope == "other_app" else str(uuid4())+ )+ with harness.factory.begin() as session:+ session.add(other)+ session.execute(+ update(MessageAnnotation).where(MessageAnnotation.id == harness.annotation.id).values(app_id=other.id)+ )+ annotation_id = harness.annotation.id+ _error(+ harness.request(operation, annotation_id=annotation_id, payload={"question": "Changed", "answer": "Changed"}),+ status=404,+ code="not_found",+ message="Annotation not found",+ )+ with harness.factory() as session:+ stored = session.get(MessageAnnotation, harness.annotation.id)+ assert stored is not None+ assert (stored.question, stored.content) == ("First question", "First answer")++[email protected]("operation", _OPERATIONS)+ "authorization", [None, "", "Bearer", "Bearer ", "Bearer ", "Basic secret", "Bearer sk-missing"]+)+def test_every_handler_rejects_missing_malformed_or_unknown_credentials(+ harness: _Harness, operation: _Operation, authorization: str | None+) -> None:+ _error(+ harness.request(operation, authorization=authorization, payload=_REPLY_PAYLOAD), status=401, code="unauthorized"+ )+++ ("separator", "status"),+ [(" ", 200), ("\t", 200), (" \t ", 200), ("\u00a0", 401), ("\u2003", 401)],+)+def test_account_admission_preserves_bearer_separator_rules(harness: _Harness, separator: str, status: int) -> None:+ response = harness.request("list", authorization=f"Bearer{separator}{harness.token.token}")++ if status == 200:+ assert response.status_code == status+ assert [row["id"] for row in response.get_json()["data"]] == [harness.annotation.id]+ else:+ _error(response, status=status, code="unauthorized")++[email protected]("operation", _OPERATIONS)+ ("gate", "status", "code", "message"),+ [+ ("missing_app", 403, "forbidden", "The app no longer exists."),+ ("foreign_tenant", 403, "forbidden", "The app no longer exists."),+ ("api_disabled", 403, "app_api_disabled", "The app's API service has been disabled."),+ ("missing_tenant", 403, "workspace_not_found", "Tenant does not exist."),+ ("archived_tenant", 403, "workspace_archived", "The workspace's status is archived."),+ ("missing_owner", 401, "unauthorized", "Tenant owner account not found or tenant is not active."),+ ("admin_owner", 401, "unauthorized", "Tenant owner account not found or tenant is not active."),+ ("editor_owner", 401, "unauthorized", "Tenant owner account not found or tenant is not active."),+ ("missing_account", 401, "unauthorized", "Tenant owner account not found or tenant is not active."),+ ],+)+def test_every_handler_checks_app_workspace_and_owner(+ harness: _Harness, operation: _Operation, gate: str, status: int, code: str, message: str+) -> None:+ with harness.factory.begin() as session:+ if gate == "missing_app":+ session.execute(delete(App).where(App.id == harness.target.id))+ elif gate == "foreign_tenant":+ session.execute(update(App).where(App.id == harness.target.id).values(tenant_id=str(uuid4())))+ elif gate == "api_disabled":+ session.execute(update(App).where(App.id == harness.target.id).values(enable_api=False))+ elif gate == "missing_tenant":+ session.execute(delete(Tenant).where(Tenant.id == harness.target.tenant_id))+ elif gate == "archived_tenant":+ session.execute(+ update(Tenant).where(Tenant.id == harness.target.tenant_id).values(status=TenantStatus.ARCHIVE)+ )+ elif gate == "missing_owner":+ session.execute(delete(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id))+ elif gate in {"admin_owner", "editor_owner"}:+ session.execute(+ update(TenantAccountJoin)+ .where(TenantAccountJoin.account_id == harness.account.id)+ .values(role=TenantAccountRole.ADMIN if gate == "admin_owner" else TenantAccountRole.EDITOR)+ )+ else:+ session.execute(delete(Account).where(Account.id == harness.account.id))+ _error(+ harness.request(operation, payload=_REPLY_PAYLOAD),+ status=status,+ code=code,+ message=message,+ )+++def test_owner_from_another_workspace_cannot_authorize_annotation_access(harness: _Harness) -> None:+ foreign_tenant = Tenant(name="Other workspace")+ foreign_owner = make_account(account_id=str(uuid4()), email="[email protected]")+ with harness.factory.begin() as session:+ session.execute(delete(TenantAccountJoin).where(TenantAccountJoin.tenant_id == harness.target.tenant_id))+ session.add_all(+ [+ foreign_tenant,+ foreign_owner,+ TenantAccountJoin(+ tenant_id=foreign_tenant.id,+ account_id=foreign_owner.id,+ role=TenantAccountRole.OWNER,+ ),+ ]+ )++ _error(harness.request("create", payload={"question": "Q", "answer": "A"}), status=401, code="unauthorized")+ with harness.factory() as session:+ assert list(session.scalars(select(MessageAnnotation.id))) == [harness.annotation.id]++[email protected]("account_status", list(AccountStatus))+def test_account_console_status_does_not_disable_machine_access(+ harness: _Harness, account_status: AccountStatus+) -> None:+ with harness.factory.begin() as session:+ session.execute(update(Account).where(Account.id == harness.account.id).values(status=account_status))+ session.execute(+ update(App).where(App.id == harness.target.id).values(is_demo=True, is_public=True, is_universal=True)+ )+ assert harness.request("create", payload={"question": "Q", "answer": "A"}).status_code == 201++[email protected]("revocation", ["token", "binding"])+def test_resource_token_revocation_is_effective_on_the_next_request(harness: _Harness, revocation: str) -> None:+ assert harness.request("list").status_code == 200+ with harness.factory.begin() as session:+ if revocation == "token":+ session.execute(delete(ResourceAccessToken).where(ResourceAccessToken.id == harness.token.id))+ else:+ session.execute(+ delete(ResourceAccessTokenRelation).where(ResourceAccessTokenRelation.token_id == harness.token.id)+ )+ _error(+ harness.request("list"),+ status=401 if revocation == "token" else 403,+ code="unauthorized" if revocation == "token" else "forbidden",+ )+++def test_resource_token_creator_does_not_replace_the_workspace_owner(harness: _Harness) -> None:+ creator = make_account(account_id=str(uuid4()), email="[email protected]")+ with harness.factory.begin() as session:+ session.add_all(+ [+ creator,+ TenantAccountJoin(+ tenant_id=harness.target.tenant_id,+ account_id=creator.id,+ role=TenantAccountRole.NORMAL,+ ),+ ]+ )+ session.execute(+ update(ResourceAccessToken).where(ResourceAccessToken.id == harness.token.id).values(created_by=creator.id)+ )+ response = harness.request("create", payload={"question": "Q", "answer": "A"})+ assert response.status_code == 201+ with harness.factory() as session:+ annotation = session.get(MessageAnnotation, response.get_json()["id"])+ assert annotation is not None+ assert annotation.account_id == harness.account.id+++def test_knowledge_binding_never_authorizes_annotation_access(harness: _Harness) -> None:+ dataset = Dataset(+ id=str(uuid4()), tenant_id=harness.target.tenant_id, name="Knowledge", created_by=harness.account.id+ )+ with harness.factory.begin() as session:+ session.add(dataset)+ session.execute(+ update(ResourceAccessTokenRelation)+ .where(ResourceAccessTokenRelation.token_id == harness.token.id)+ .values(resource_type=ResourceAccessTokenResourceType.KNOWLEDGE, app_id=None, dataset_id=dataset.id)+ )+ _error(harness.request("list"), status=403, code="forbidden")+++def test_resource_token_requires_explicit_selection_for_multiple_bound_apps(harness: _Harness) -> None:+ other = make_app(app_id=str(uuid4()), tenant_id=harness.target.tenant_id)+ annotation = _annotation(harness, 1, app_id=other.id)+ with harness.factory.begin() as session:+ session.add_all(+ [+ other,+ annotation,+ ResourceAccessTokenRelation(+ token_id=harness.token.id,+ resource_type=ResourceAccessTokenResourceType.APP,+ app_id=other.id,+ ),+ ]+ )+ _error(harness.request("list"), status=400, code="invalid_param")+ for app, expected in [(harness.target, harness.annotation), (other, annotation)]:+ response = harness.request("list", requested_app_id=app.id)+ assert response.status_code == 200+ assert [row["id"] for row in response.get_json()["data"]] == [expected.id]+ _error(harness.request("list", requested_app_id=str(uuid4())), status=403, code="forbidden")++[email protected]("operation", ["reply", "status"])+def test_reply_rejects_invalid_action_before_touching_jobs(harness: _Harness, operation: _Operation) -> None:+ _error(harness.request(operation, action="invalid", payload=_REPLY_PAYLOAD), status=400, code="invalid_param")++[email protected]("action", ["enable", "disable"])[email protected]("payload", [None, {}, {"score_threshold": 0.5}])+def test_reply_enable_and_disable_require_the_payload(+ harness: _Harness, action: str, payload: dict[str, object] | None+) -> None:+ _error(harness.request("reply", action=action, payload=payload), status=422, code="unprocessable_entity")++[email protected]("otel_enabled", [False, True])[email protected]("authenticated", [False, True])+def test_admission_preserves_logging_and_trace_identity(+ harness: _Harness, config_overrides: Callable[..., None], otel_enabled: bool, authenticated: bool+) -> None:+ config_overrides(ENABLE_OTEL=otel_enabled)+ output = StringIO()+ handler = logging.StreamHandler(output)+ handler.addFilter(IdentityContextFilter())+ handler.setFormatter(logging.Formatter("%(tenant_id)s|%(user_id)s|%(user_type)s"))+ logger = logging.Logger("annotation-admission-identity", logging.INFO)+ logger.addHandler(handler)++ @harness.app.after_request+ def log_response(response: Response) -> Response:+ logger.info("Annotation response")+ return response++ exporter = InMemorySpanExporter()+ provider = TracerProvider()+ provider.add_span_processor(SimpleSpanProcessor(exporter))+ try:+ with provider.get_tracer(__name__).start_as_current_span("annotation request"):+ response = harness.request("list", authorization="default" if authenticated else None)+ assert response.status_code == (200 if authenticated else 401)+ expected_log = f"{harness.target.tenant_id}|{harness.account.id}|account" if authenticated else "||"+ assert output.getvalue().strip() == expected_log+ [span] = exporter.get_finished_spans()+ assert span.attributes == (+ {DifySpanAttributes.TENANT_ID: harness.target.tenant_id, GenAIAttributes.USER_ID: harness.account.id}+ if otel_enabled and authenticated+ else {}+ )+ finally:+ provider.shutdown()+ handler.close()++[email protected]("user_type", ["account", "end_user"])+def test_legacy_login_signal_preserves_identity_span_attributes(+ config_overrides: Callable[..., None], user_type: str+) -> None:+ config_overrides(ENABLE_OTEL=True)+ tenant = Tenant(name="Tracing workspace")+ account = make_account(account_id=str(uuid4()), tenant=tenant)+ user = (+ account if user_type == "account" else EndUser(id=str(uuid4()), tenant_id=tenant.id, type=EndUserType.BROWSER)+ )+ exporter = InMemorySpanExporter()+ provider = TracerProvider()+ provider.add_span_processor(SimpleSpanProcessor(exporter))+ try:+ with provider.get_tracer(__name__).start_as_current_span("legacy login"):+ on_user_loaded(None, user)+ [span] = exporter.get_finished_spans()+ assert span.attributes == {DifySpanAttributes.TENANT_ID: tenant.id, GenAIAttributes.USER_ID: user.id}+ finally:+ provider.shutdown()++[email protected]("unavailable", ["tenant", "recording_span"])+def test_span_identity_skips_missing_tenant_and_ended_spans(+ config_overrides: Callable[..., None], caplog: pytest.LogCaptureFixture, unavailable: str+) -> None:+ config_overrides(ENABLE_OTEL=True)+ exporter = InMemorySpanExporter()+ provider = TracerProvider()+ provider.add_span_processor(SimpleSpanProcessor(exporter))+ span = provider.get_tracer(__name__).start_span("unavailable identity context")+ try:+ if unavailable == "recording_span":+ span.end()+ with trace.use_span(span, end_on_exit=False), caplog.at_level(logging.WARNING):+ set_identity_span_attributes(tenant_id=None if unavailable == "tenant" else "workspace", user_id="account")+ if unavailable != "recording_span":+ span.end()+ [finished] = exporter.get_finished_spans()+ assert finished.attributes == {}+ assert "Setting attribute on ended span" not in caplog.text+ assert "Error setting tenant and user attributes" not in caplog.text+ finally:+ provider.shutdown()diff --git a/api/tests/e2e_tests/contracts/controllers/service_api/app/test_message_feedback.py b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_message_feedback.pyindex 4765dd02adc..4ab366b1163 100644--- a/api/tests/e2e_tests/contracts/controllers/service_api/app/test_message_feedback.py+++ b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_message_feedback.py@@ -261,6 +261,40 @@ def test_invalid_authorization_is_rejected(harness: _Harness, authorization: strassert harness.sessions == []+def test_app_only_list_rejects_a_key_from_another_workspace(harness: _Harness) -> None:+ with harness.factory.begin() as session:+ foreign_tenant = Tenant(name="Other API key workspace")+ session.add(foreign_tenant)+ token = session.scalar(select(ApiToken).where(ApiToken.token == "feedback-test-token"))+ assert token is not None+ token.tenant_id = foreign_tenant.id++ response = harness.get("?user=must-not-be-provisioned")++ assert response.status_code == HTTPStatus.FORBIDDEN+ assert response.get_json() == {+ "code": "app_not_found",+ "message": "The app no longer exists.",+ "status": HTTPStatus.FORBIDDEN,+ }+ assert harness.end_user_queries == []+ harness.assert_closed()+++def test_app_only_list_accepts_a_historical_key_without_workspace(harness: _Harness) -> None:+ with harness.factory.begin() as session:+ token = session.scalar(select(ApiToken).where(ApiToken.token == "feedback-test-token"))+ assert token is not None+ token.tenant_id = None++ response = harness.get("?user=must-not-be-provisioned")++ assert response.status_code == HTTPStatus.OK+ assert response.get_json() == {"data": []}+ assert harness.end_user_queries == []+ harness.assert_closed()++def test_invalid_rating_is_rejected(harness: _Harness) -> None:response = harness.post({"rating": "neutral"})assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY@@ -444,7 +478,7 @@ def test_multiple_app_resource_token_list_requires_app_header(harness: _Harness)assert response.status_code == HTTPStatus.BAD_REQUESTassert response.get_json() == {- "code": "bad_request",+ "code": "invalid_param","message": "App ID is required when a resource access token is bound to multiple apps.","status": 400,}diff --git a/api/tests/e2e_tests/contracts/controllers/service_api/app/test_message_suggested_questions.py b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_message_suggested_questions.pyindex b5ef6c019c0..97924a10944 100644--- a/api/tests/e2e_tests/contracts/controllers/service_api/app/test_message_suggested_questions.py+++ b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_message_suggested_questions.py@@ -292,6 +292,38 @@ def test_invalid_authorization_stops_before_database_admission(harness: _Harnessassert harness.admissions == harness.provisions == harness.queries == []+def test_app_key_with_foreign_workspace_is_rejected_before_end_user_provisioning(harness: _Harness) -> None:+ with harness.factory.begin() as session:+ foreign_tenant = Tenant(name="Other API key workspace")+ session.add(foreign_tenant)+ token = session.scalar(select(ApiToken).where(ApiToken.token == "test-token"))+ assert token is not None+ token.tenant_id = foreign_tenant.id++ response = harness.get(user="must-not-be-provisioned")++ assert response.status_code == 403+ assert response.json == {"code": "app_not_found", "message": "The app no longer exists.", "status": 403}+ assert harness.provisions == harness.queries == []+ with harness.factory() as session:+ assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]+ harness.assert_closed()+++def test_historical_app_key_without_workspace_uses_the_apps_workspace(harness: _Harness) -> None:+ with harness.factory.begin() as session:+ token = session.scalar(select(ApiToken).where(ApiToken.token == "test-token"))+ assert token is not None+ token.tenant_id = None++ response = harness.get()++ assert response.status_code == 200+ assert response.json == {"result": "success", "data": []}+ assert len(harness.provisions) == len(harness.queries) == 1+ harness.assert_closed()++@pytest.mark.parametrize("user", [None, ""])def test_required_user_rejected_before_provisioning(harness: _Harness, user: str | None) -> None:response = harness.get(user=user)@@ -641,7 +673,7 @@ def test_multiple_app_resource_token_requires_app_header_before_provisioning(harassert response.status_code == 400assert response.json == {- "code": "bad_request",+ "code": "invalid_param","message": "App ID is required when a resource access token is bound to multiple apps.","status": 400,}diff --git a/api/tests/unit_tests/controllers/common/test_access_response.py b/api/tests/unit_tests/controllers/common/test_access_response.pyindex 690739ba385..1fc87489d21 100644--- a/api/tests/unit_tests/controllers/common/test_access_response.py+++ b/api/tests/unit_tests/controllers/common/test_access_response.py@@ -3,7 +3,7 @@import ioimport jsonimport logging-from collections.abc import Buffer+from collections.abc import Buffer, Iteratorfrom dataclasses import dataclassfrom typing import Literal, Never, cast, overridefrom unittest.mock import MagicMock@@ -29,6 +29,7 @@ from controllers.common.access_response import (from controllers.mcp import mcpfrom controllers.trigger import webhookfrom extensions import ext_request_logging+from extensions.ext_redis import RedisClientWrapperfrom libs.external_api import ExternalApifrom models.engine import dbfrom models.model import ApiToken, App, AppMCPServer@@ -105,7 +106,9 @@ def token_client(http_app: Flask, mocker: MockerFixture) -> TokenClient:http_app.register_blueprint(other)cache_get = mocker.patch.object(token_wraps.ApiTokenCache, "get", return_value=None)fetch = mocker.patch.object(- token_wraps, "fetch_token_with_single_flight", side_effect=Unauthorized("Access token is invalid")+ token_wraps,+ "fetch_token_with_single_flight",+ side_effect=token_service.ApiTokenInvalidError("Access token is invalid"),)usage = mocker.patch.object(token_wraps, "record_token_usage")return http_app.test_client(), cache_get, fetch, usage@@ -161,11 +164,77 @@ def test_invalid_token_database_path_does_not_record_usage(http_app: Flask, mockmocker.patch.object(token_service.ApiTokenCache, "set")with http_app.app_context():cast(Table, ApiToken.__table__).create(db.engine)- with pytest.raises(Unauthorized, match="Access token is invalid"):+ with pytest.raises(token_service.ApiTokenInvalidError, match="Access token is invalid"):token_service.query_token_from_db("missing-token", "app")usage.assert_not_called()+def uncached_token_client(+ http_app: Flask, redis_transport: tuple[RedisClientWrapper, MagicMock]+) -> Iterator[tuple[FlaskClient, MagicMock]]:+ """Run real token lookup and error translation, replacing only Redis network dispatch."""+ redis, commands = redis_transport++ def execute(command: str, *_args: object, **_kwargs: object) -> bool | None:+ return True if command in {"SET", "EVALSHA"} else None++ commands.side_effect = execute+ previous_redis = token_service.redis_client+ token_service.redis_client = redis+ bp = Blueprint("uncached_service_test", __name__, url_prefix="/v1")+ register_auth_error_response(bp, www_authenticate='Bearer realm="api"')+ api = ExternalApi(bp)++ class AppTokenResource(Resource):+ @token_wraps.validate_app_token+ def get(self, app_model: App) -> Never:+ pytest.fail(f"Invalid credentials reached the handler for app {app_model.id}")++ class DatasetTokenResource(Resource):+ @token_wraps.validate_dataset_token+ def get(self, tenant_id: str) -> Never:+ pytest.fail(f"Invalid credentials reached the dataset handler for tenant {tenant_id}")++ api.add_resource(AppTokenResource, "/app")+ api.add_resource(DatasetTokenResource, "/dataset")+ http_app.register_blueprint(bp)+ try:+ with http_app.app_context():+ cast(Table, ApiToken.__table__).create(db.engine)+ yield http_app.test_client(), commands+ finally:+ token_service.redis_client = previous_redis+ with http_app.app_context():+ db.session.remove()+ db.engine.dispose()++[email protected]("scope", ["app", "dataset"])[email protected]("authorization", [None, "Bearer missing-token"])+def test_real_token_errors_preserve_http_response_contract(+ uncached_token_client: tuple[FlaskClient, MagicMock], scope: str, authorization: str | None+) -> None:+ client, commands = uncached_token_client+ response = client.get(f"/v1/{scope}", headers={} if authorization is None else {"Authorization": authorization})++ message = MISSING_BEARER_MESSAGE if authorization is None else "Access token is invalid"+ expected = {"code": "unauthorized", "message": message, "status": 401}+ assert response.status_code == 401+ assert response.data == json.dumps(expected, separators=(",", ":"), sort_keys=True).encode()+ assert response.headers["Content-Type"] == "application/json"+ assert response.headers["Cache-Control"] == "no-store"+ assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'++ if authorization is None:+ commands.assert_not_called()+ else:+ calls = [call.args for call in commands.call_args_list]+ assert calls.count(("GET", f"api_token:{scope}:missing-token")) == 2+ assert ("SETEX", f"api_token:{scope}:missing-token", 60, b"null") in calls+ assert not any(str(call[1]).startswith(token_service.ACTIVE_TOKEN_KEY_PREFIX) for call in calls)++def test_success_token_and_other_surface_are_not_rewritten(token_client: TokenClient) -> None:client, cache_get, _fetch, usage = token_clientcache_get.return_value = ApiToken(id="existing")diff --git a/api/tests/unit_tests/controllers/service_api/app/test_annotation.py b/api/tests/unit_tests/controllers/service_api/app/test_annotation.pyindex 2b6734ee334..cd8a197238a 100644--- a/api/tests/unit_tests/controllers/service_api/app/test_annotation.py+++ b/api/tests/unit_tests/controllers/service_api/app/test_annotation.py@@ -1,369 +1,26 @@-"""-Unit tests for Service API Annotation controller.--Tests coverage for:-- AnnotationCreatePayload Pydantic model validation-- AnnotationReplyActionPayload Pydantic model validation-- Error patterns and validation logic--Note: API endpoint tests for annotation controllers are complex due to:-- @validate_app_token decorator requiring full Flask-SQLAlchemy setup-- @edit_permission_required decorator checking current_user permissions-- These are better covered by integration tests-"""--import uuid-from inspect import unwrap-from unittest.mock import Mock-import pytest-from flask import Flask, request-from flask_restx.api import HTTPStatusfrom pydantic import ValidationError-from sqlalchemy.orm import Sessionfrom controllers.service_api.app.annotation import (AnnotationCreatePayload,- AnnotationListApi,AnnotationListQuery,- AnnotationReplyActionApi,AnnotationReplyActionPayload,- AnnotationReplyActionStatusApi,- AnnotationUpdateDeleteApi,)-from extensions.ext_redis import redis_client-from models.model import App, AppMode, MessageAnnotation-from services.annotation_service import AppAnnotationService---def _app(app_id: str = "app", tenant_id: str = "tenant") -> App:- return App(- id=app_id,- tenant_id=tenant_id,- name="Service API app",- description="",- mode=AppMode.CHAT,- enable_site=True,- enable_api=True,- max_active_requests=0,- )-def _annotation() -> MessageAnnotation:- annotation = MessageAnnotation(- app_id="app",- question="q",- content="a",- account_id="account",+def test_payload_models_preserve_defaults_numeric_strings_and_zero_threshold() -> None:+ assert AnnotationListQuery.model_validate({}).model_dump() == {"page": 1, "limit": 20, "keyword": ""}+ assert AnnotationListQuery.model_validate({"page": "2", "limit": "5", "keyword": "refund"}).model_dump() == {+ "page": 2,+ "limit": 5,+ "keyword": "refund",+ }+ assert AnnotationCreatePayload(question="", answer="").model_dump() == {"question": "", "answer": ""}+ assert (+ AnnotationReplyActionPayload.model_validate(+ {"embedding_provider_name": "openai", "embedding_model_name": "embedding", "score_threshold": 0.0}+ ).score_threshold+ == 0.0)- annotation.id = "a1"- return annotation---class TestAnnotationCreatePayload:- """Test suite for AnnotationCreatePayload Pydantic model."""-- def test_payload_with_question_and_answer(self):- """Test payload with required fields."""- payload = AnnotationCreatePayload(question="What is AI?", answer="AI is artificial intelligence.")- assert payload.question == "What is AI?"- assert payload.answer == "AI is artificial intelligence."-- def test_payload_with_unicode_content(self):- """Test payload with unicode content."""- payload = AnnotationCreatePayload(question="什么是人工智能?", answer="人工智能是模拟人类智能的技术。")- assert payload.question == "什么是人工智能?"-- def test_payload_with_special_characters(self):- """Test payload with special characters."""- payload = AnnotationCreatePayload(- question="What is <b>AI</b>?", answer="AI & ML are related fields with 100% growth!"- )- assert "<b>" in payload.question---class TestAnnotationReplyActionPayload:- """Test suite for AnnotationReplyActionPayload Pydantic model."""-- def test_payload_with_all_fields(self):- """Test payload with all fields."""- payload = AnnotationReplyActionPayload(- score_threshold=0.8, embedding_provider_name="openai", embedding_model_name="text-embedding-ada-002"- )- assert payload.score_threshold == 0.8- assert payload.embedding_provider_name == "openai"- assert payload.embedding_model_name == "text-embedding-ada-002"-- def test_payload_with_different_provider(self):- """Test payload with different embedding provider."""- payload = AnnotationReplyActionPayload(- score_threshold=0.75, embedding_provider_name="azure_openai", embedding_model_name="text-embedding-3-small"- )- assert payload.embedding_provider_name == "azure_openai"-- def test_payload_with_zero_threshold(self):- """Test payload with zero score threshold."""- payload = AnnotationReplyActionPayload(- score_threshold=0.0, embedding_provider_name="local", embedding_model_name="default"- )- assert payload.score_threshold == 0.0---class TestAnnotationListQuery:- def test_defaults(self) -> None:- query = AnnotationListQuery.model_validate({})- assert query.page == 1- assert query.limit == 20- assert query.keyword == ""-- def test_valid_numeric_strings(self) -> None:- query = AnnotationListQuery.model_validate({"page": "2", "limit": "5", "keyword": "refund"})- assert query.page == 2- assert query.limit == 5- assert query.keyword == "refund"-- @pytest.mark.parametrize("field", ["page", "limit"])- @pytest.mark.parametrize("value", ["abc", "1.5", "1e2", "", "0", "-1"])- def test_invalid_explicit_pagination_value(self, field: str, value: str) -> None:- with pytest.raises(ValidationError):- AnnotationListQuery.model_validate({field: value})---class TestAppModelPatterns:- """Test App model patterns used by annotation controller."""-- def test_app_model_has_required_fields(self):- """Test App model has required fields for annotation operations."""- app = App(- id=str(uuid.uuid4()),- status="normal",- enable_api=True,- )- assert app.id is not None- assert app.status == "normal"- assert app.enable_api-- def test_app_model_disabled_api(self):- """Test app with disabled API access."""- app = App(- enable_api=False,- )-- assert not app.enable_api-- def test_app_model_archived_status(self):- """Test app with archived status."""- app = App(- status="archived",- )- assert app.status == "archived"---class TestAnnotationErrorPatterns:- """Test annotation-related error handling patterns."""-- def test_not_found_error_pattern(self):- """Test NotFound error pattern used in annotation operations."""- from werkzeug.exceptions import NotFound-- with pytest.raises(NotFound):- raise NotFound("Annotation not found.")-- def test_forbidden_error_pattern(self):- """Test Forbidden error pattern."""- from werkzeug.exceptions import Forbidden-- with pytest.raises(Forbidden):- raise Forbidden("Permission denied.")-- def test_value_error_for_job_not_found(self):- """Test ValueError pattern for job not found."""- with pytest.raises(ValueError, match="does not exist"):- raise ValueError("The job does not exist.")---class TestAnnotationReplyActionApi:- def test_enable(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- enable_mock = Mock(return_value={"job_id": "job-1", "job_status": "waiting"})- monkeypatch.setattr(AppAnnotationService, "enable_app_annotation", enable_mock)- api = AnnotationReplyActionApi()- handler = unwrap(api.post)- app_model = _app()- with app.test_request_context(- "/apps/annotation-reply/enable",- method="POST",- json={"score_threshold": 0.5, "embedding_provider_name": "p", "embedding_model_name": "m"},- ):- payload = AnnotationReplyActionPayload.model_validate(request.get_json() or {})- response, status = handler(api, payload, app_model=app_model, action="enable")- assert status == 200- assert response == {"job_id": "job-1", "job_status": "waiting"}- enable_mock.assert_called_once()-- def test_disable(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- disable_mock = Mock(return_value={"job_id": "job-1", "job_status": "waiting"})- monkeypatch.setattr(AppAnnotationService, "disable_app_annotation", disable_mock)- api = AnnotationReplyActionApi()- handler = unwrap(api.post)- app_model = _app()- with app.test_request_context(- "/apps/annotation-reply/disable",- method="POST",- json={"score_threshold": 0.5, "embedding_provider_name": "p", "embedding_model_name": "m"},- ):- payload = AnnotationReplyActionPayload.model_validate(request.get_json() or {})- response, status = handler(api, payload, app_model=app_model, action="disable")- assert status == 200- assert response == {"job_id": "job-1", "job_status": "waiting"}- disable_mock.assert_called_once()---class TestAnnotationReplyActionStatusApi:- def test_missing_job(self, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(redis_client, "get", lambda *_args, **_kwargs: None)- api = AnnotationReplyActionStatusApi()- handler = unwrap(api.get)- app_model = _app()- with pytest.raises(ValueError, match="The job does not exist\\."):- handler(api, app_model=app_model, job_id="j1", action="enable")-- def test_error(self, monkeypatch: pytest.MonkeyPatch) -> None:- def _get(key):- if "error" in key:- return b"oops"- return b"error"-- monkeypatch.setattr(redis_client, "get", _get)- api = AnnotationReplyActionStatusApi()- handler = unwrap(api.get)- app_model = _app()- response, status = handler(api, app_model=app_model, job_id="j1", action="enable")- assert status == 200- assert response["job_status"] == "error"- assert response["error_msg"] == "oops"---class TestAnnotationListApi:- def test_get_uses_defaults(self, app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session) -> None:- annotation = _annotation()- get_mock = Mock(return_value=([annotation], 1))- monkeypatch.setattr(AppAnnotationService, "get_annotation_list_by_app_id", get_mock)- api = AnnotationListApi()- handler = unwrap(api.get)- app_model = _app()- with app.test_request_context("/apps/annotations", method="GET"):- query = AnnotationListQuery.model_validate(request.args.to_dict(flat=True))- response = handler(api, query, sqlite_session, app_model=app_model)- assert response["page"] == 1- assert response["limit"] == 20- session = get_mock.call_args.args[-1]- assert session is sqlite_session- assert get_mock.call_args.args == ("app", 1, 20, "", session)-- def test_get_accepts_valid_numeric_strings(- self, app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session- ) -> None:- annotation = _annotation()- get_mock = Mock(return_value=([annotation], 1))- monkeypatch.setattr(AppAnnotationService, "get_annotation_list_by_app_id", get_mock)- api = AnnotationListApi()- handler = unwrap(api.get)- app_model = _app()- with app.test_request_context("/apps/annotations?page=2&limit=5&keyword=refund", method="GET"):- query = AnnotationListQuery.model_validate(request.args.to_dict(flat=True))- response = handler(api, query, sqlite_session, app_model=app_model)- assert response["total"] == 1- assert response["page"] == 2- assert response["limit"] == 5- session = get_mock.call_args.args[-1]- assert session is sqlite_session- assert get_mock.call_args.args == ("app", 2, 5, "refund", session)-- @pytest.mark.parametrize("query_string", ["page=abc&limit=5", "page=1&limit=abc", "page=&limit=5", "limit=0"])- def test_get_rejects_invalid_explicit_pagination_value(- self,- app: Flask,- monkeypatch: pytest.MonkeyPatch,- query_string: str,- ) -> None:- get_mock = Mock(return_value=([], 0))- monkeypatch.setattr(AppAnnotationService, "get_annotation_list_by_app_id", get_mock)- api = AnnotationListApi()- # The parse moved into @model_validate, so build the query the way the decorator does and- # assert it is what rejects the input, before the view is ever reached.- with app.test_request_context(f"/apps/annotations?{query_string}", method="GET"):- with pytest.raises(ValidationError):- AnnotationListQuery.model_validate(request.args.to_dict(flat=True))- assert unwrap(api.get) is not api.get- get_mock.assert_not_called()-- def test_has_more_false_on_last_page_exact_limit(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- """A full last page must set has_more false instead of forcing another fetch."""- page_size = 20- annotation = _annotation()- get_mock = Mock(return_value=([annotation] * page_size, page_size))- monkeypatch.setattr(AppAnnotationService, "get_annotation_list_by_app_id", get_mock)- api = AnnotationListApi()- handler = unwrap(api.get)- app_model = _app()- with app.test_request_context(f"/apps/annotations?page=1&limit={page_size}", method="GET"):- query = AnnotationListQuery.model_validate(request.args.to_dict(flat=True))- response = handler(api, query, Mock(), app_model=app_model)- assert response["has_more"] is False- assert response["limit"] == page_size- assert response["total"] == page_size- assert response["page"] == 1-- def test_has_more_true_when_limit_exceeds_cap(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- """limit>100 still reports remaining rows after the server cap of 100."""- returned_count = 100- total = 150- annotation = _annotation()- get_mock = Mock(return_value=([annotation] * returned_count, total))- monkeypatch.setattr(AppAnnotationService, "get_annotation_list_by_app_id", get_mock)- api = AnnotationListApi()- handler = unwrap(api.get)- app_model = _app()- with app.test_request_context("/apps/annotations?page=1&limit=200", method="GET"):- query = AnnotationListQuery.model_validate(request.args.to_dict(flat=True))- response = handler(api, query, Mock(), app_model=app_model)- assert response["has_more"] is True- assert response["limit"] == 100- assert response["total"] == total- assert response["page"] == 1-- def test_create(self, app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session) -> None:- annotation = _annotation()- monkeypatch.setattr(- AppAnnotationService, "insert_app_annotation_directly", lambda *_args, **_kwargs: annotation- )- api = AnnotationListApi()- handler = unwrap(api.post)- app_model = _app()- with app.test_request_context("/apps/annotations", method="POST", json={"question": "q", "answer": "a"}):- payload = AnnotationCreatePayload.model_validate(request.get_json() or {})- response, status = handler(api, payload, sqlite_session, app_model=app_model)- assert status == HTTPStatus.CREATED- assert response["question"] == "q"---class TestAnnotationUpdateDeleteApi:- def test_update_delete(self, app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session) -> None:- annotation = _annotation()- monkeypatch.setattr(- AppAnnotationService, "update_app_annotation_directly", lambda *_args, **_kwargs: annotation- )- delete_mock = Mock()- monkeypatch.setattr(AppAnnotationService, "delete_app_annotation", delete_mock)- api = AnnotationUpdateDeleteApi()- put_handler = unwrap(api.put)- delete_handler = unwrap(api.delete)- app_model = _app()- with app.test_request_context("/apps/annotations/1", method="PUT", json={"question": "q", "answer": "a"}):- payload = AnnotationCreatePayload.model_validate(request.get_json() or {})- response = put_handler(api, payload, sqlite_session, app_model=app_model, annotation_id="1")- assert response["answer"] == "a"- with app.test_request_context("/apps/annotations/1", method="DELETE"):- response, status = delete_handler(api, sqlite_session, app_model=app_model, annotation_id="1")- assert status == 204- delete_mock.assert_called_once()+ with pytest.raises(ValidationError):+ AnnotationCreatePayload.model_validate({"question": "Q"})diff --git a/api/tests/unit_tests/controllers/service_api/test___init__.py b/api/tests/unit_tests/controllers/service_api/test___init__.pyindex c779271eb2c..cc74d84d0c5 100644--- a/api/tests/unit_tests/controllers/service_api/test___init__.py+++ b/api/tests/unit_tests/controllers/service_api/test___init__.py@@ -619,7 +619,6 @@ def test_service_schemas_only_document_reachable_not_found_responses(openapi_jsopaths = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", False))["paths"]for path, method in (- ("/apps/annotation-reply/{action}/status/{job_id}", "get"),("/info", "get"),("/meta", "get"),("/parameters", "get"),@@ -629,10 +628,54 @@ def test_service_schemas_only_document_reachable_not_found_responses(openapi_jsoassert "404" not in paths[path][method]["responses"]assert "400" in paths[path][method]["responses"]+ annotation_status = paths["/apps/annotation-reply/{action}/status/{job_id}"]["get"]["responses"]+ assert "404" in annotation_status+ assert "400" in annotation_statusassert "404" in paths["/workflows/{workflow_id}/run"]["post"]["responses"]assert "404" in paths["/chat-messages"]["post"]["responses"]+ ("path", "method", "validates_payload"),+ [+ ("/apps/annotation-reply/{action}", "post", True),+ ("/apps/annotation-reply/{action}/status/{job_id}", "get", False),+ ("/apps/annotations", "get", True),+ ("/apps/annotations", "post", True),+ ("/apps/annotations/{annotation_id}", "put", True),+ ("/apps/annotations/{annotation_id}", "delete", False),+ ],+)+def test_annotation_schemas_document_admission_and_validation_errors(+ openapi_json: OpenAPIJSONLoader, path: str, method: str, validates_payload: bool+) -> None:+ from controllers.service_api import bp as service_api_bp++ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", False))+ responses = payload["paths"][path][method]["responses"]+ schemas = payload["components"]["schemas"]++ assert {"400", "401", "403", "404"} <= responses.keys()+ assert "invalid_param" in responses["400"]["description"]+ assert "missing workspace" not in responses["400"]["description"]+ documented = _schema_refs(responses["403"])+ for name, code in {+ "AppNotFoundError": "app_not_found",+ "AppAbnormalStatusError": "app_abnormal_status",+ "AppApiDisabledError": "app_api_disabled",+ "WorkspaceNotFoundError": "workspace_not_found",+ "WorkspaceArchivedError": "workspace_archived",+ "ForbiddenError": "forbidden",+ }.items():+ assert name in documented+ properties = schemas[name]["allOf"][1]["properties"]+ assert properties["code"]["const"] == code+ assert properties["status"]["const"] == 403+ assert ("422" in responses) == validates_payload+ if validates_payload:+ assert "unprocessable_entity" in responses["422"]["description"]++def test_service_openapi_does_not_encode_docs_coverage_boundaries(openapi_json: OpenAPIJSONLoader):from controllers.service_api import bp as service_api_bpdiff --git a/api/tests/unit_tests/controllers/service_api/test_wraps.py b/api/tests/unit_tests/controllers/service_api/test_wraps.pyindex c16cab461f3..856e186f75d 100644--- a/api/tests/unit_tests/controllers/service_api/test_wraps.py+++ b/api/tests/unit_tests/controllers/service_api/test_wraps.py@@ -13,6 +13,7 @@ from sqlalchemy import selectfrom sqlalchemy.orm import Session, scoped_sessionfrom werkzeug.exceptions import Forbidden, NotFound, ServiceUnavailable, Unauthorized+from controllers.common.errors import UnauthorizedErrorfrom controllers.service_api import wraps as wraps_modulefrom controllers.service_api.wraps import (DatasetApiResource,@@ -39,7 +40,7 @@ from models.resource_access_token import (ResourceAccessTokenRelation,ResourceAccessTokenResourceType,)-from services.api_token_service import ApiTokenCache+from services.api_token_service import ApiTokenCache, ApiTokenInvalidErrorfrom services.knowledge_fs.control_space_commands import KnowledgeFSControlSpaceCommandServicefrom tests.helpers.unit.config_override import config_overrides_context@@ -159,7 +160,7 @@ class TestValidateAndGetApiToken:# No Authorization header# Act & Assert- with pytest.raises(Unauthorized) as exc_info:+ with pytest.raises(UnauthorizedError) as exc_info:validate_and_get_api_token("app")assert "Authorization header must be provided" in str(exc_info.value)@@ -168,7 +169,7 @@ class TestValidateAndGetApiToken:# Arrangewith app.test_request_context("/", method="GET", headers={"Authorization": "Basic token123"}):# Act & Assert- with pytest.raises(Unauthorized) as exc_info:+ with pytest.raises(UnauthorizedError) as exc_info:validate_and_get_api_token("app")assert "Authorization header must be provided and start with 'Bearer'" in str(exc_info.value)@@ -203,16 +204,14 @@ class TestValidateAndGetApiToken:def test_invalid_token_raises_unauthorized(self, mock_fetch_token, mock_cache_cls, mock_record_usage, app: Flask):"""Test that invalid token raises Unauthorized."""# Arrange- from werkzeug.exceptions import Unauthorized-mock_cache_instance = Mock()mock_cache_instance.get.return_value = None # Cache missmock_cache_cls.get = mock_cache_instance.get- mock_fetch_token.side_effect = Unauthorized("Access token is invalid")+ mock_fetch_token.side_effect = ApiTokenInvalidError("Access token is invalid")# Act & Assertwith app.test_request_context("/", method="GET", headers={"Authorization": "Bearer invalid_token"}):- with pytest.raises(Unauthorized) as exc_info:+ with pytest.raises(UnauthorizedError) as exc_info:validate_and_get_api_token("app")assert "Access token is invalid" in str(exc_info.value)@@ -232,7 +231,7 @@ class TestValidateAppToken:with (app.test_request_context("/", headers={"Authorization": f"Bearer{separator}dify_rt_token"}),patch.object(wraps_module, "application_services") as services,- pytest.raises(Unauthorized),+ pytest.raises(UnauthorizedError),):validate_app_token(lambda: "ok")()services.assert_not_called()diff --git a/api/tests/unit_tests/repositories/test_app_definition_query_repository.py b/api/tests/unit_tests/repositories/test_app_definition_query_repository.pyindex 81f7779e3e2..f2e11d70a9f 100644--- a/api/tests/unit_tests/repositories/test_app_definition_query_repository.py+++ b/api/tests/unit_tests/repositories/test_app_definition_query_repository.py@@ -1,15 +1,18 @@import json+from collections.abc import Iteratorfrom typing import Anyfrom uuid import NAMESPACE_URL, uuid5import pytest-from sqlalchemy.orm import Session, sessionmaker+from sqlalchemy import Connection, Engine, event, select+from sqlalchemy.exc import InvalidRequestError+from sqlalchemy.orm import Session, SessionTransaction, sessionmakerfrom core.app.app_config.common.parameters_mapping import get_parameters_from_feature_dictfrom core.app.apps.agent_app.errors import AgentAppGeneratorError, AgentAppNotPublishedErrorfrom core.tools.entities.tool_entities import ApiProviderSchemaTypefrom machinery.context import AppRequestContext-from models.account import Account, Tenant, TenantStatus+from models.account import Account, Tenant, TenantAccountJoin, TenantAccountRole, TenantStatusfrom models.agent import (Agent,AgentConfigRevision,@@ -33,6 +36,7 @@ from services.app_definition_query_service import (AppParameterConfig,AppSiteConfiguration,AppToolIconSource,+ ServiceApiAppRecord,)from services.web_app_runtime_query_service import WebAppRuntimeRecord@@ -71,6 +75,100 @@ def _persist_app(return app+def service_api_repository(sqlite_engine: Engine) -> Iterator[AppDefinitionQueryRepository]:+ factory = sessionmaker(bind=sqlite_engine, close_resets_only=False)+ sessions: list[Session] = []++ def track(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:+ sessions.append(session)++ event.listen(factory, "after_begin", track)+ try:+ yield AppDefinitionQueryRepository(session_factory=factory)+ finally:+ event.remove(factory, "after_begin", track)+ assert sessions+ for session in sessions:+ assert not session.in_transaction()+ assert not session.identity_map+ with pytest.raises(InvalidRequestError, match="permanently closed"):+ session.execute(select(1))++[email protected]("tenant_id", [None, _TENANT_ID, _OTHER_TENANT_ID])+def test_get_service_api_record_retains_app_scope_after_session_closes(+ sqlite_session_factory: sessionmaker[Session],+ service_api_repository: AppDefinitionQueryRepository,+ tenant_id: str | None,+) -> None:+ tenant = Tenant(name="App workspace")+ tenant.id = _TENANT_ID+ with sqlite_session_factory.begin() as session:+ session.add(tenant)+ _persist_app(session)++ record = service_api_repository.get_service_api_record(_APP_ID, tenant_id=tenant_id)++ assert record == (+ None+ if tenant_id == _OTHER_TENANT_ID+ else ServiceApiAppRecord(+ app_id=_APP_ID,+ tenant_id=_TENANT_ID,+ mode="chat",+ status="normal",+ enable_api=True,+ tenant_status="normal",+ )+ )+++ "owner_state",+ ["active", "admin", "editor", "missing_account", "missing_membership", "archived", "missing_workspace"],+)+def test_get_service_api_owner_id_requires_an_existing_owner_in_the_active_workspace(+ sqlite_session_factory: sessionmaker[Session],+ service_api_repository: AppDefinitionQueryRepository,+ owner_state: str,+) -> None:+ tenant = Tenant(+ name="App workspace", status=TenantStatus.ARCHIVE if owner_state == "archived" else TenantStatus.NORMAL+ )+ tenant.id = _TENANT_ID+ owner = Account(name="Workspace owner", email="[email protected]")+ owner.id = _ACCOUNT_ID+ other_tenant = Tenant(name="Other workspace")+ other_tenant.id = _OTHER_TENANT_ID+ other_owner = Account(name="Other workspace owner", email="[email protected]")+ other_owner.id = _stable_uuid("other-service-api-owner")+ role = {"admin": TenantAccountRole.ADMIN, "editor": TenantAccountRole.EDITOR}.get(+ owner_state, TenantAccountRole.OWNER+ )+ with sqlite_session_factory.begin() as session:+ if owner_state != "missing_workspace":+ session.add(tenant)+ if owner_state != "missing_account":+ session.add(owner)+ if owner_state != "missing_membership":+ session.add(TenantAccountJoin(tenant_id=_TENANT_ID, account_id=_ACCOUNT_ID, role=role))+ session.add_all(+ [+ other_tenant,+ other_owner,+ TenantAccountJoin(tenant_id=_OTHER_TENANT_ID, account_id=other_owner.id, role=TenantAccountRole.OWNER),+ ]+ )+ other_owner_id = other_owner.id++ assert service_api_repository.get_service_api_owner_id(_TENANT_ID) == (+ _ACCOUNT_ID if owner_state == "active" else None+ )+ assert service_api_repository.get_service_api_owner_id(_OTHER_TENANT_ID) == other_owner_id+ assert service_api_repository.get_service_api_owner_id(_stable_uuid("missing-service-api-tenant")) is None++def test_get_mode_returns_none_for_missing_app(sqlite_session_factory: sessionmaker[Session]) -> None:repository = AppDefinitionQueryRepository(session_factory=sqlite_session_factory)diff --git a/api/tests/unit_tests/services/test_annotation_command_service.py b/api/tests/unit_tests/services/test_annotation_command_service.pyindex 6a31ac4c8c4..82b47819b7a 100644--- a/api/tests/unit_tests/services/test_annotation_command_service.py+++ b/api/tests/unit_tests/services/test_annotation_command_service.py@@ -26,7 +26,7 @@ from tasks.annotation.delete_annotation_index_task import delete_annotation_indefrom tasks.annotation.update_annotation_to_index_task import update_annotation_to_index_taskfrom tests.helpers.unit.model_factories import make_app-type Operation = Literal["upsert", "update", "delete", "delete_many", "clear"]+type Operation = Literal["create", "upsert", "update", "delete", "delete_many", "clear"]type BulkOperation = Literal["delete_many", "clear"]@@ -44,6 +44,14 @@ class _Harness:publications: list[tuple[bool, tuple[AnnotationRecord, ...]]]def apply(self, operation: Operation) -> str:+ if operation == "create":+ return self.service.create(+ tenant_id=self.app.tenant_id,+ app_id=self.app.id,+ account_id=self.annotation.account_id,+ question="",+ answer="",+ ).idif operation == "upsert":return self.service.upsert(tenant_id=self.app.tenant_id,@@ -159,7 +167,7 @@ def test_changing_the_matching_threshold_keeps_embeddings_without_publishing(harmessages.get(block=False)[email protected]("operation", ["upsert", "update", "delete", "delete_many", "clear"])[email protected]("operation", ["create", "upsert", "update", "delete", "delete_many", "clear"])def test_tasks_are_published_after_committed_rows_are_visible_and_sessions_close(harness: _Harness, operation: Operation) -> None:@@ -175,9 +183,13 @@ def test_tasks_are_published_after_committed_rows_are_visible_and_sessions_close"tenant_id": harness.app.tenant_id,"collection_binding_id": harness.binding_id,}- task_index = {"upsert": 0, "update": 1, "delete": 2, "delete_many": 2, "clear": 2}[operation]+ task_index = {"create": 0, "upsert": 0, "update": 1, "delete": 2, "delete_many": 2, "clear": 2}[operation]if operation in {"delete", "delete_many", "clear"}:assert annotation_id not in saved+ elif operation == "create":+ expected_kwargs["question"] = ""+ assert saved[annotation_id].question == ""+ assert saved[annotation_id].content == ""else:expected_kwargs["question"] = "new question" if operation == "upsert" else "updated answer"assert saved[annotation_id].content == ("legacy answer" if operation == "upsert" else "updated answer")@@ -191,7 +203,7 @@ def test_tasks_are_published_after_committed_rows_are_visible_and_sessions_closemessages.get(block=False)[email protected]("operation", ["upsert", "update", "delete", "delete_many", "clear"])[email protected]("operation", ["create", "upsert", "update", "delete", "delete_many", "clear"])def test_disabled_annotation_reply_does_not_publish(harness: _Harness, operation: Operation) -> None:harness.apply(operation)assert harness.publications == []@@ -210,7 +222,7 @@ def test_rejected_write_does_not_publish(harness: _Harness) -> None:][email protected]("operation", ["upsert", "update", "delete", "delete_many", "clear"])[email protected]("operation", ["create", "upsert", "update", "delete", "delete_many", "clear"])def test_publish_failure_propagates_without_rolling_back_committed_write(harness: _Harness, operation: Operation) -> None:@@ -222,7 +234,10 @@ def test_publish_failure_propagates_without_rolling_back_committed_write(harness.apply(operation)records = harness.repository.get_all(tenant_id=harness.app.tenant_id, app_id=harness.app.id)- if operation == "upsert":+ if operation == "create":+ assert len(records) == 2+ assert any(record.question == record.content == "" for record in records)+ elif operation == "upsert":assert len(records) == 2assert any(record.content == "legacy answer" for record in records)elif operation == "update":diff --git a/api/tests/unit_tests/services/test_annotation_service.py b/api/tests/unit_tests/services/test_annotation_service.pyindex ce8b3bf38e3..2707c968085 100644--- a/api/tests/unit_tests/services/test_annotation_service.py+++ b/api/tests/unit_tests/services/test_annotation_service.py@@ -1,357 +1,68 @@-from __future__ import annotations--from datetime import datetime, timedelta-from io import BytesIO-from typing import Any-from unittest.mock import patch+"""SQLite-backed tests for annotation reply reads and runtime hit history."""import pytestfrom sqlalchemy import select-from sqlalchemy.orm import Session, sessionmaker-from werkzeug.datastructures import FileStorage-from werkzeug.exceptions import NotFound+from sqlalchemy.orm import Session-import services.annotation_service as annotation_service_modulefrom models.account import Account-from models.model import (- App,- AppAnnotationHitHistory,- AppAnnotationSetting,- AppMode,- IconType,- MessageAnnotation,-)+from models.model import AppAnnotationHitHistory, MessageAnnotationfrom services.annotation_service import AppAnnotationService-from services.app_ref_service import AnnotationRef, AppRef--"""SQLite-backed unit tests for :mod:`services.annotation_service`."""---TENANT_ID = "tenant-1"-OTHER_TENANT_ID = "tenant-2"---def _persist_app(- session: Session,- *,- app_id: str = "app-1",- tenant_id: str = TENANT_ID,- status: str = "normal",-) -> App:- app = App(- id=app_id,- tenant_id=tenant_id,- name=f"Annotation App {app_id}",- description="",- mode=AppMode.CHAT,- icon_type=IconType.EMOJI,- icon="chat",- icon_background="#FFFFFF",- status=status,- enable_site=False,- enable_api=False,- )- session.add(app)- session.commit()- return app-def _persist_annotation(- session: Session,- app: App,- *,- annotation_id: str = "ann-1",- question: str = "question",- content: str = "answer",- message_id: str | None = None,- created_at: datetime | None = None,-) -> MessageAnnotation:- annotation = MessageAnnotation(- app_id=app.id,- question=question,- content=content,- account_id="account-1",- message_id=message_id,- )- annotation.id = annotation_id- if created_at is not None:- annotation.created_at = created_at+def _persist_annotation(session: Session) -> MessageAnnotation:+ annotation = MessageAnnotation(app_id="app-1", question="question", content="answer", account_id="account-1")session.add(annotation)session.commit()return annotation-def _persist_setting(- session: Session,- app: App,- *,- setting_id: str = "setting-1",- binding_id: str = "collection-1",- score_threshold: float = 0.5,-) -> AppAnnotationSetting:- setting = AppAnnotationSetting(- app_id=app.id,- score_threshold=score_threshold,- collection_binding_id=binding_id,- created_user_id="account-1",- updated_user_id="account-1",- )- setting.id = setting_id- session.add(setting)- session.commit()- return setting---def _persist_history(- session: Session,- app: App,- annotation: MessageAnnotation,- *,- history_id: str = "history-1",- created_at: datetime | None = None,-) -> AppAnnotationHitHistory:- history = AppAnnotationHitHistory(- app_id=app.id,- annotation_id=annotation.id,- source="hit-testing",- question="query",- account_id="account-1",- score=0.8,- message_id="message-1",- annotation_question=annotation.question,- annotation_content=annotation.content,- )- history.id = history_id- if created_at is not None:- history.created_at = created_at- session.add(history)- session.commit()- return history---def _annotation_ref(app: App, annotation_id: str) -> AnnotationRef:- return AnnotationRef(app=AppRef(tenant_id=app.tenant_id, app_id=app.id), annotation_id=annotation_id)---def _file(content: bytes) -> FileStorage:- return FileStorage(stream=BytesIO(content))---def _observer_get(factory: sessionmaker[Session], model: type[Any], identifier: str) -> Any:- with factory() as observer:- return observer.get(model, identifier)---class TestAnnotationServiceCases:- @staticmethod- @pytest.fixture- def current_user(monkeypatch: pytest.MonkeyPatch) -> Account:- """Install a real account model as the request identity."""-- account = Account(name="Annotation Tester", email="[email protected]")- account.id = "account-1"- monkeypatch.setattr(- annotation_service_module,- "current_account_with_tenant",- lambda: (account, TENANT_ID),- )- return account-- class TestAppAnnotationServiceListAndExport:- def test_list_rejects_cross_tenant_app(self, sqlite_session: Session, current_user: Account) -> None:- app = _persist_app(sqlite_session, tenant_id=OTHER_TENANT_ID)+def test_get_annotation_by_id_uses_real_identity_lookup(sqlite_session: Session) -> None:+ annotation = _persist_annotation(sqlite_session)- with pytest.raises(NotFound):- AppAnnotationService.get_annotation_list_by_app_id(app.id, 1, 10, "", sqlite_session)+ assert AppAnnotationService.get_annotation_by_id("missing", sqlite_session) is None+ assert AppAnnotationService.get_annotation_by_id(annotation.id, sqlite_session) is annotation- def test_list_filters_orders_and_paginates(self, sqlite_session: Session, current_user: Account) -> None:- app = _persist_app(sqlite_session)- other_app = _persist_app(sqlite_session, app_id="app-2")- now = datetime(2026, 1, 1)- first = _persist_annotation(- sqlite_session, app, annotation_id="ann-1", question="needle first", created_at=now- )- second = _persist_annotation(- sqlite_session,- app,- annotation_id="ann-2",- question="other",- content="needle second",- created_at=now + timedelta(seconds=1),- )- _persist_annotation(sqlite_session, app, annotation_id="ann-3", question="not matched")- _persist_annotation(sqlite_session, other_app, annotation_id="decoy", question="needle decoy")- items, total = AppAnnotationService.get_annotation_list_by_app_id(app.id, 1, 1, "needle", sqlite_session)[email protected]("author_exists", [True, False])+def test_get_annotation_reply_preserves_missing_author(sqlite_session: Session, author_exists: bool) -> None:+ if author_exists:+ author = Account(name="Annotation author", email="[email protected]")+ author.id = "account-1"+ sqlite_session.add(author)+ annotation = _persist_annotation(sqlite_session)- assert total == 2- assert [item.id for item in items] == [second.id]- items, total = AppAnnotationService.get_annotation_list_by_app_id(app.id, 2, 1, "needle", sqlite_session)- assert total == 2- assert [item.id for item in items] == [first.id]+ assert AppAnnotationService.get_annotation_reply_by_id("missing", sqlite_session) is None+ reply = AppAnnotationService.get_annotation_reply_by_id(annotation.id, sqlite_session)+ assert reply is not None+ assert reply[0] is annotation+ assert reply[1] == ("Annotation author" if author_exists else None)- def test_list_without_keyword_is_app_scoped(self, sqlite_session: Session, current_user: Account) -> None:- app = _persist_app(sqlite_session)- other_app = _persist_app(sqlite_session, app_id="app-2")- expected = _persist_annotation(sqlite_session, app)- _persist_annotation(sqlite_session, other_app, annotation_id="decoy")- items, total = AppAnnotationService.get_annotation_list_by_app_id(app.id, 1, 10, "", sqlite_session)+def test_add_history_increments_count_and_flushes_row(sqlite_session: Session) -> None:+ annotation = _persist_annotation(sqlite_session)- assert total == 1- assert [item.id for item in items] == [expected.id]-- class TestAppAnnotationServiceDirectManipulation:- def test_insert_rejects_cross_tenant_app_and_missing_question(- self, sqlite_session: Session, current_user: Account- ) -> None:- other_app = _persist_app(sqlite_session, tenant_id=OTHER_TENANT_ID)- with pytest.raises(NotFound):- AppAnnotationService.insert_app_annotation_directly(- {"answer": "hello", "question": "q"}, other_app.id, sqlite_session- )-- app = _persist_app(sqlite_session, app_id="app-2")- with pytest.raises(ValueError, match="question"):- AppAnnotationService.insert_app_annotation_directly({"answer": "hello"}, app.id, sqlite_session)-- def test_insert_persists_and_enqueues_index(- self,- sqlite_session: Session,- sqlite_session_factory: sessionmaker[Session],- current_user: Account,- ) -> None:- app = _persist_app(sqlite_session)- setting = _persist_setting(sqlite_session, app)-- with patch.object(annotation_service_module, "add_annotation_to_index_task") as task:- result = AppAnnotationService.insert_app_annotation_directly(- {"answer": "hello", "question": "q1"}, app.id, sqlite_session- )-- stored = _observer_get(sqlite_session_factory, MessageAnnotation, result.id)- assert stored is not None- assert (stored.app_id, stored.question, stored.content, stored.account_id) == (- app.id,- "q1",- "hello",- current_user.id,- )- task.delay.assert_called_once_with(result.id, "q1", TENANT_ID, app.id, setting.collection_binding_id)-- def test_update_is_app_scoped_and_validates_fields(- self, sqlite_session: Session, current_user: Account- ) -> None:- app = _persist_app(sqlite_session)- other_app = _persist_app(sqlite_session, app_id="app-2")- annotation = _persist_annotation(sqlite_session, other_app)-- with pytest.raises(NotFound):- AppAnnotationService.update_app_annotation_directly(- {"answer": "a", "question": "q"}, _annotation_ref(app, annotation.id), sqlite_session- )-- own_annotation = _persist_annotation(sqlite_session, app, annotation_id="own-ann")- with pytest.raises(ValueError, match="question"):- AppAnnotationService.update_app_annotation_directly(- {"answer": "a"}, _annotation_ref(app, own_annotation.id), sqlite_session- )- with pytest.raises(ValueError, match="answer"):- AppAnnotationService.update_app_annotation_directly(- {"question": "q"}, _annotation_ref(app, own_annotation.id), sqlite_session- )-- def test_update_persists_and_enqueues_index(- self,- sqlite_session: Session,- sqlite_session_factory: sessionmaker[Session],- current_user: Account,- ) -> None:- app = _persist_app(sqlite_session)- annotation = _persist_annotation(sqlite_session, app, content="old")- setting = _persist_setting(sqlite_session, app)-- with patch.object(annotation_service_module, "update_annotation_to_index_task") as task:- result = AppAnnotationService.update_app_annotation_directly(- {"answer": "new", "question": "new q"}, _annotation_ref(app, annotation.id), sqlite_session- )-- stored = _observer_get(sqlite_session_factory, MessageAnnotation, annotation.id)- assert stored is not None- assert (stored.question, stored.content) == ("new q", "new")- assert result.id == stored.id- task.delay.assert_called_once_with(annotation.id, "new q", TENANT_ID, app.id, setting.collection_binding_id)-- def test_delete_is_app_scoped(self, sqlite_session: Session, current_user: Account) -> None:- app = _persist_app(sqlite_session)- other_app = _persist_app(sqlite_session, app_id="app-2")- annotation = _persist_annotation(sqlite_session, other_app)-- with pytest.raises(NotFound):- AppAnnotationService.delete_app_annotation(_annotation_ref(app, annotation.id), sqlite_session)-- assert sqlite_session.get(MessageAnnotation, annotation.id) is not None-- def test_delete_removes_annotation_and_histories_and_enqueues_index(- self,- sqlite_session: Session,- sqlite_session_factory: sessionmaker[Session],- current_user: Account,- ) -> None:- app = _persist_app(sqlite_session)- annotation = _persist_annotation(sqlite_session, app)- histories = [- _persist_history(sqlite_session, app, annotation, history_id="history-1"),- _persist_history(sqlite_session, app, annotation, history_id="history-2"),- ]- setting = _persist_setting(sqlite_session, app)-- with patch.object(annotation_service_module, "delete_annotation_index_task") as task:- AppAnnotationService.delete_app_annotation(_annotation_ref(app, annotation.id), sqlite_session)-- with sqlite_session_factory() as observer:- assert observer.get(MessageAnnotation, annotation.id) is None- assert [observer.get(AppAnnotationHitHistory, history.id) for history in histories] == [None, None]- task.delay.assert_called_once_with(annotation.id, app.id, TENANT_ID, setting.collection_binding_id)-- class TestAppAnnotationServiceHitHistoryAndSettings:- def test_get_annotation_by_id_uses_real_identity_lookup(- self, sqlite_session: Session, current_user: Account- ) -> None:- app = _persist_app(sqlite_session)- annotation = _persist_annotation(sqlite_session, app)-- assert AppAnnotationService.get_annotation_by_id("missing", sqlite_session) is None- assert AppAnnotationService.get_annotation_by_id(annotation.id, sqlite_session) is annotation-- def test_add_history_increments_count_and_flushes_row(- self, sqlite_session: Session, current_user: Account- ) -> None:- app = _persist_app(sqlite_session)- annotation = _persist_annotation(sqlite_session, app)-- AppAnnotationService.add_annotation_history(- annotation_id=annotation.id,- app_id=app.id,- annotation_question="q",- annotation_content="a",- query="user q",- user_id=current_user.id,- message_id="msg-1",- from_source="chat",- score=0.8,- session=sqlite_session,- )+ AppAnnotationService.add_annotation_history(+ annotation_id=annotation.id,+ app_id=annotation.app_id,+ annotation_question="q",+ annotation_content="a",+ query="user q",+ user_id=annotation.account_id,+ message_id="msg-1",+ from_source="chat",+ score=0.8,+ session=sqlite_session,+ )- sqlite_session.refresh(annotation)- assert annotation.hit_count == 1- history = sqlite_session.scalar(- select(AppAnnotationHitHistory).where(AppAnnotationHitHistory.annotation_id == annotation.id)- )- assert history is not None- assert (history.question, history.annotation_question, history.annotation_content, history.score) == (- "user q",- "q",- "a",- 0.8,- )+ sqlite_session.refresh(annotation)+ assert annotation.hit_count == 1+ history = sqlite_session.scalar(+ select(AppAnnotationHitHistory).where(AppAnnotationHitHistory.annotation_id == annotation.id)+ )+ assert history is not None+ assert (history.question, history.annotation_question, history.annotation_content, history.score) == (+ "user q",+ "q",+ "a",+ 0.8,+ )diff --git a/api/tests/unit_tests/services/test_api_token_service.py b/api/tests/unit_tests/services/test_api_token_service.pyindex 3aa0b6e9338..0a1aa4b470f 100644--- a/api/tests/unit_tests/services/test_api_token_service.py+++ b/api/tests/unit_tests/services/test_api_token_service.py@@ -9,8 +9,8 @@ from uuid import uuid4import pytestfrom flask import Flaskfrom redis import Redis+from sqlalchemy import Connection, ExecutionContext, eventfrom sqlalchemy.orm import Session-from werkzeug.exceptions import Unauthorizedimport services.api_token_service as api_token_service_modulefrom extensions.ext_redis import RedisClientWrapper@@ -22,6 +22,7 @@ from services.api_token_service import (CACHE_NULL_TTL_SECONDS,CACHE_TTL_SECONDS,ApiTokenCache,+ ApiTokenInvalidError,CachedApiToken,)@@ -335,18 +336,16 @@ class TestApiTokenServiceCases:mock_cache_set.assert_called_once()mock_record_usage.assert_called_once_with(token_value, "app")- def test_should_cache_null_and_raise_unauthorized_when_token_not_found(self, api_token_db: Session) -> None:- with (- patch.object(api_token_service_module.ApiTokenCache, "set") as mock_cache_set,- patch.object(api_token_service_module, "record_token_usage") as mock_record_usage,- ):- with pytest.raises(Unauthorized, match="Access token is invalid"):- api_token_service_module.query_token_from_db(f"missing-{uuid4()}", "app")+ def test_should_cache_null_and_raise_domain_error_when_token_not_found(+ self, api_token_db: Session, single_flight_redis: tuple[Redis, list[tuple[object, ...]]]+ ) -> None:+ _client, commands = single_flight_redis+ token_value = f"missing-{uuid4()}"- mock_cache_set.assert_called_once()- call_args = mock_cache_set.call_args[0]- assert call_args[2] is None # cached None- mock_record_usage.assert_not_called()+ with pytest.raises(ApiTokenInvalidError, match="Access token is invalid"):+ api_token_service_module.query_token_from_db(token_value, "app")++ assert commands == [("SETEX", f"api_token:app:{token_value}", CACHE_NULL_TTL_SECONDS, b"null")]class TestRecordTokenUsage:def test_should_write_active_key_with_iso_timestamp_and_ttl(self):@@ -419,7 +418,9 @@ class TestApiTokenServiceCases:def execute(*args: object, **_kwargs: object) -> object:commands.append(args)- assert args[0] in {"SET", "EVALSHA"}+ if args[0] == "GET":+ return None+ assert args[0] in {"SET", "SETEX", "EVALSHA"}return Truewith Redis() as client:@@ -501,25 +502,48 @@ class TestApiTokenServiceCases:assert commandsassert all(command[0] == "SET" for command in commands)- def test_should_reraise_unauthorized_from_db_query(- self, single_flight_redis: tuple[Redis, list[tuple[object, ...]]]+ @pytest.mark.parametrize("stored_scope", [None, ApiTokenType.DATASET])+ def test_invalid_token_should_release_lock_without_fallback_or_usage(+ self,+ api_token_db: Session,+ single_flight_redis: tuple[Redis, list[tuple[object, ...]]],+ stored_scope: ApiTokenType | None,) -> None:auth_token = "token-123"- scope = "app"_client, commands = single_flight_redis-- with (- patch.object(api_token_service_module.ApiTokenCache, "get", return_value=None),- patch.object(- api_token_service_module,- "query_token_from_db",- side_effect=Unauthorized("Access token is invalid"),- ),- ):- with pytest.raises(Unauthorized, match="Access token is invalid"):- api_token_service_module.fetch_token_with_single_flight(auth_token, scope)-- _assert_lock_released(commands)+ if stored_scope is not None:+ token = _api_token(token_id=str(uuid4()), app_id=str(uuid4()), tenant_id=str(uuid4()), token=auth_token)+ token.type = stored_scope+ api_token_db.add(token)+ api_token_db.commit()++ statements: list[str] = []++ def record_statement(+ _connection: Connection,+ _cursor: object,+ statement: str,+ _parameters: object,+ _context: ExecutionContext,+ _executemany: bool,+ ) -> None:+ statements.append(statement)++ engine = db.engine+ event.listen(engine, "before_cursor_execute", record_statement)+ try:+ with pytest.raises(ApiTokenInvalidError, match="Access token is invalid"):+ api_token_service_module.fetch_token_with_single_flight(auth_token, "app")+ finally:+ event.remove(engine, "before_cursor_execute", record_statement)++ assert len(statements) == 1 # An invalid credential must not trigger the Redis failure DB fallback.+ assert "FROM api_tokens" in statements[0]+ assert [command[0] for command in commands] == ["SET", "GET", "SETEX", "EVALSHA"]+ assert commands[2] == ("SETEX", f"api_token:app:{auth_token}", CACHE_NULL_TTL_SECONDS, b"null")+ _assert_lock_released([commands[0], commands[-1]])+ active_key = ApiTokenCache.make_active_key(auth_token, "app")+ assert all(active_key not in command for command in commands)def test_should_fallback_to_db_query_when_lock_raises_exception(self, single_flight_redis: tuple[Redis, list[tuple[object, ...]]], monkeypatch: pytest.MonkeyPatchdiff --git a/api/tests/unit_tests/services/test_app_definition_query_service.py b/api/tests/unit_tests/services/test_app_definition_query_service.pyindex 9987d4e50d3..1bcf4093f49 100644--- a/api/tests/unit_tests/services/test_app_definition_query_service.py+++ b/api/tests/unit_tests/services/test_app_definition_query_service.py@@ -1,9 +1,14 @@from unittest.mock import MagicMock, create_autospec, patchimport pytest+from sqlalchemy import Engine, text+from sqlalchemy.orm import Session, sessionmakerimport services.app_definition_query_service as modulefrom core.app.apps.agent_app.errors import AgentAppGeneratorError, AgentAppNotPublishedError+from models.account import Tenant+from models.model import App, AppMode+from repositories.app_definition_query_repository import AppDefinitionQueryRepositoryfrom services.app_definition_query_service import (AppDefinitionNotPublishedError,AppDefinitionQuery,@@ -13,6 +18,7 @@ from services.app_definition_query_service import (AppParameterConfig,AppToolIconSource,)+from services.errors.app import AppAbnormalStatusError_BUILTIN_ICON_URL_PREFIX = "https://console.example/console/api/workspaces/current/tool-provider/builtin/"@@ -28,6 +34,64 @@ def _service() -> tuple[AppDefinitionQueryService, MagicMock]:)+def persisted_app_service(sqlite_session_factory: sessionmaker[Session]) -> AppDefinitionQueryService:+ tenant = Tenant(name="App workspace")+ tenant.id = "tenant"+ with sqlite_session_factory.begin() as session:+ session.add_all(+ [+ tenant,+ App(id="app", tenant_id="tenant", name="App", mode=AppMode.CHAT, enable_site=False, enable_api=True),+ ]+ )+ return AppDefinitionQueryService(+ definitions=AppDefinitionQueryRepository(session_factory=sqlite_session_factory),+ builtin_icon_url_prefix=_BUILTIN_ICON_URL_PREFIX,+ )++[email protected]("tenant_id", [None, "tenant"])+def test_service_api_historical_key_derives_workspace_from_app(+ persisted_app_service: AppDefinitionQueryService,+ tenant_id: str | None,+) -> None:+ app = persisted_app_service.get_service_api_app("app", tenant_id=tenant_id)++ assert app.app_id == "app"+ assert app.tenant_id == "tenant"+++def test_service_api_provided_foreign_workspace_is_rejected(+ persisted_app_service: AppDefinitionQueryService,+) -> None:+ with pytest.raises(AppDefinitionUnavailableError, match="The app no longer exists"):+ persisted_app_service.get_service_api_app("app", tenant_id="other-tenant")+++def test_service_api_orphaned_key_does_not_require_database(+ unbound_session_factory: sessionmaker[Session],+) -> None:+ service = AppDefinitionQueryService(+ definitions=AppDefinitionQueryRepository(session_factory=unbound_session_factory),+ builtin_icon_url_prefix=_BUILTIN_ICON_URL_PREFIX,+ )++ with pytest.raises(AppDefinitionUnavailableError, match="The app no longer exists"):+ service.get_service_api_app(None)+++def test_service_api_abnormal_persisted_status_reaches_access_policy(+ persisted_app_service: AppDefinitionQueryService,+ sqlite_engine: Engine,+) -> None:+ with sqlite_engine.begin() as connection:+ connection.execute(text("UPDATE apps SET status = 'disabled' WHERE id = 'app'"))++ with pytest.raises(AppAbnormalStatusError, match="The app's status is abnormal"):+ persisted_app_service.get_service_api_app("app", tenant_id="tenant")++def test_get_mode_returns_repository_value() -> None:service, definitions = _service()definitions.get_mode.return_value = "completion"diff --git a/packages/contracts/generated/api/service/errors.gen.ts b/packages/contracts/generated/api/service/errors.gen.tsindex de006536671..6aa0ea38df2 100644--- a/packages/contracts/generated/api/service/errors.gen.ts+++ b/packages/contracts/generated/api/service/errors.gen.ts@@ -146,6 +146,14 @@ const zFilenameNotExistsError = /* @__PURE__ */ (() =>}),))()+const zForbiddenError = /* @__PURE__ */ (() =>+ zErrorResponse.and(+ z.object({+ code: z.literal('forbidden'),+ status: z.literal(403),+ }),+ ))()+const zHumanInputFormExpiredError = /* @__PURE__ */ (() =>zErrorResponse.and(z.object({@@ -423,6 +431,61 @@ export const get2Errors = {app_api_disabled: { status: 403, data: zAppApiDisabledError },app_not_found: { status: 403, data: zAppNotFoundError },app_unavailable: { status: 400, data: zAppUnavailableError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+}++export const get3Errors = {+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+}++export const postErrors = {+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+}++export const delete_Errors = {+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+}++export const putErrors = {+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+}++export const get4Errors = {+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+}++export const post2Errors = {+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },workspace_archived: { status: 403, data: zWorkspaceArchivedError },workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },}@@ -1263,6 +1326,7 @@ export const post58Errors = {app_api_disabled: { status: 403, data: zAppApiDisabledError },app_not_found: { status: 403, data: zAppNotFoundError },app_unavailable: { status: 400, data: zAppUnavailableError },+ forbidden: { status: 403, data: zForbiddenError },message_feedback_rating_required: { status: 400, data: zMessageFeedbackRatingRequiredError },workspace_archived: { status: 403, data: zWorkspaceArchivedError },workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },@@ -1279,6 +1343,7 @@ export const get61Errors = {},app_unavailable: { status: 400, data: zAppUnavailableError },completion_request_error: { status: 400, data: zCompletionRequestError },+ forbidden: { status: 403, data: zForbiddenError },model_currently_not_support: { status: 400, data: zModelCurrentlyNotSupportError },not_chat_app: { status: 400, data: zNotChatAppError },provider_not_initialize: { status: 400, data: zProviderNotInitializeError },@@ -1292,6 +1357,7 @@ export const get62Errors = {app_api_disabled: { status: 403, data: zAppApiDisabledError },app_not_found: { status: 403, data: zAppNotFoundError },app_unavailable: { status: 400, data: zAppUnavailableError },+ forbidden: { status: 403, data: zForbiddenError },not_chat_app: { status: 400, data: zNotChatAppError },workspace_archived: { status: 403, data: zWorkspaceArchivedError },workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },diff --git a/packages/contracts/generated/api/service/orpc.gen.ts b/packages/contracts/generated/api/service/orpc.gen.tsindex e07e7d26ff4..aefe59bd8b7 100644--- a/packages/contracts/generated/api/service/orpc.gen.ts+++ b/packages/contracts/generated/api/service/orpc.gen.ts@@ -20,6 +20,7 @@ import {zFileNotFoundError,zFileTooLargeError,zFilenameNotExistsError,+ zForbiddenError,zHumanInputFormExpiredError,zHumanInputFormSubmittedError,zInvalidActionError,@@ -503,6 +504,7 @@ export const get2 = ocapp_api_disabled: { status: 403, data: zAppApiDisabledError },app_not_found: { status: 403, data: zAppNotFoundError },app_unavailable: { status: 400, data: zAppUnavailableError },+ forbidden: { status: 403, data: zForbiddenError },workspace_archived: { status: 403, data: zWorkspaceArchivedError },workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },})@@ -533,6 +535,14 @@ export const get3 = oc}).input(z.object({ params: zGetAppsAnnotationReplyByActionStatusByJobIdPath })).output(zGetAppsAnnotationReplyByActionStatusByJobIdResponse)+ .errors({+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+ })export const byJobId = {get: get3,@@ -565,6 +575,14 @@ export const post = oc}),).output(zPostAppsAnnotationReplyByActionResponse)+ .errors({+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+ })export const byAction = {post,@@ -593,6 +611,14 @@ export const delete_ = oc}).input(z.object({ params: zDeleteAppsAnnotationsByAnnotationIdPath })).output(zDeleteAppsAnnotationsByAnnotationIdResponse)+ .errors({+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+ })/*** Update Annotation@@ -616,6 +642,14 @@ export const put = oc}),).output(zPutAppsAnnotationsByAnnotationIdResponse)+ .errors({+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+ })export const byAnnotationId = {delete: delete_,@@ -640,6 +674,14 @@ export const get4 = oc}).input(z.object({ query: zGetAppsAnnotationsQuery.optional() }).optional()).output(zGetAppsAnnotationsResponse)+ .errors({+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+ })/*** Create Annotation@@ -660,6 +702,14 @@ export const post2 = oc}).input(z.object({ body: zPostAppsAnnotationsBody })).output(zPostAppsAnnotationsResponse)+ .errors({+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ forbidden: { status: 403, data: zForbiddenError },+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },+ })export const annotations = {get: get4,@@ -4742,6 +4792,7 @@ export const post58 = ocapp_api_disabled: { status: 403, data: zAppApiDisabledError },app_not_found: { status: 403, data: zAppNotFoundError },app_unavailable: { status: 400, data: zAppUnavailableError },+ forbidden: { status: 403, data: zForbiddenError },message_feedback_rating_required: { status: 400, data: zMessageFeedbackRatingRequiredError },workspace_archived: { status: 403, data: zWorkspaceArchivedError },workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },@@ -4785,6 +4836,7 @@ export const get61 = oc},app_unavailable: { status: 400, data: zAppUnavailableError },completion_request_error: { status: 400, data: zCompletionRequestError },+ forbidden: { status: 403, data: zForbiddenError },model_currently_not_support: { status: 400, data: zModelCurrentlyNotSupportError },not_chat_app: { status: 400, data: zNotChatAppError },provider_not_initialize: { status: 400, data: zProviderNotInitializeError },@@ -4825,6 +4877,7 @@ export const get62 = ocapp_api_disabled: { status: 403, data: zAppApiDisabledError },app_not_found: { status: 403, data: zAppNotFoundError },app_unavailable: { status: 400, data: zAppUnavailableError },+ forbidden: { status: 403, data: zForbiddenError },not_chat_app: { status: 400, data: zNotChatAppError },workspace_archived: { status: 403, data: zWorkspaceArchivedError },workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },diff --git a/packages/contracts/generated/api/service/types.gen.ts b/packages/contracts/generated/api/service/types.gen.tsindex a9bc402ecba..015e62a0e15 100644--- a/packages/contracts/generated/api/service/types.gen.ts+++ b/packages/contracts/generated/api/service/types.gen.ts@@ -1204,6 +1204,11 @@ export type FilenameNotExistsError = ErrorResponse & {status: 400}+export type ForbiddenError = ErrorResponse & {+ code: 'forbidden'+ status: 403+}+export type FormInputConfig =| ({type: 'paragraph'@@ -4577,6 +4582,7 @@ export type GetAppFeedbacksErrors = {| AppAbnormalStatusError| AppApiDisabledError| AppNotFoundError+ | ForbiddenError| WorkspaceArchivedError| WorkspaceNotFoundError| ErrorResponse@@ -4601,8 +4607,18 @@ export type PostAppsAnnotationReplyByActionData = {}export type PostAppsAnnotationReplyByActionErrors = {+ 400: ErrorResponse401: ErrorResponse- 403: ErrorResponse+ 403:+ | AppAbnormalStatusError+ | AppApiDisabledError+ | AppNotFoundError+ | ForbiddenError+ | WorkspaceArchivedError+ | WorkspaceNotFoundError+ | ErrorResponse+ 404: ErrorResponse+ 422: ErrorResponsedefault: ErrorResponse}@@ -4629,7 +4645,15 @@ export type GetAppsAnnotationReplyByActionStatusByJobIdData = {export type GetAppsAnnotationReplyByActionStatusByJobIdErrors = {400: ErrorResponse401: ErrorResponse- 403: ErrorResponse+ 403:+ | AppAbnormalStatusError+ | AppApiDisabledError+ | AppNotFoundError+ | ForbiddenError+ | WorkspaceArchivedError+ | WorkspaceNotFoundError+ | ErrorResponse+ 404: ErrorResponsedefault: ErrorResponse}@@ -4655,8 +4679,18 @@ export type GetAppsAnnotationsData = {}export type GetAppsAnnotationsErrors = {+ 400: ErrorResponse401: ErrorResponse- 403: ErrorResponse+ 403:+ | AppAbnormalStatusError+ | AppApiDisabledError+ | AppNotFoundError+ | ForbiddenError+ | WorkspaceArchivedError+ | WorkspaceNotFoundError+ | ErrorResponse+ 404: ErrorResponse+ 422: ErrorResponsedefault: ErrorResponse}@@ -4677,8 +4711,18 @@ export type PostAppsAnnotationsData = {}export type PostAppsAnnotationsErrors = {+ 400: ErrorResponse401: ErrorResponse- 403: ErrorResponse+ 403:+ | AppAbnormalStatusError+ | AppApiDisabledError+ | AppNotFoundError+ | ForbiddenError+ | WorkspaceArchivedError+ | WorkspaceNotFoundError+ | ErrorResponse+ 404: ErrorResponse+ 422: ErrorResponsedefault: ErrorResponse}@@ -4701,8 +4745,16 @@ export type DeleteAppsAnnotationsByAnnotationIdData = {}export type DeleteAppsAnnotationsByAnnotationIdErrors = {+ 400: ErrorResponse401: ErrorResponse- 403: ErrorResponse+ 403:+ | AppAbnormalStatusError+ | AppApiDisabledError+ | AppNotFoundError+ | ForbiddenError+ | WorkspaceArchivedError+ | WorkspaceNotFoundError+ | ErrorResponse404: ErrorResponsedefault: ErrorResponse}@@ -4727,9 +4779,18 @@ export type PutAppsAnnotationsByAnnotationIdData = {}export type PutAppsAnnotationsByAnnotationIdErrors = {+ 400: ErrorResponse401: ErrorResponse- 403: ErrorResponse+ 403:+ | AppAbnormalStatusError+ | AppApiDisabledError+ | AppNotFoundError+ | ForbiddenError+ | WorkspaceArchivedError+ | WorkspaceNotFoundError+ | ErrorResponse404: ErrorResponse+ 422: ErrorResponsedefault: ErrorResponse}@@ -8820,6 +8881,7 @@ export type GetMessagesErrors = {| AppAbnormalStatusError| AppApiDisabledError| AppNotFoundError+ | ForbiddenError| WorkspaceArchivedError| WorkspaceNotFoundError| ErrorResponse@@ -8851,6 +8913,7 @@ export type PostMessagesByMessageIdFeedbacksErrors = {| AppAbnormalStatusError| AppApiDisabledError| AppNotFoundError+ | ForbiddenError| WorkspaceArchivedError| WorkspaceNotFoundError| ErrorResponse@@ -8894,6 +8957,7 @@ export type GetMessagesByMessageIdSuggestedErrors = {| AppApiDisabledError| AppNotFoundError| AppSuggestedQuestionsAfterAnswerDisabledError+ | ForbiddenError| WorkspaceArchivedError| WorkspaceNotFoundError| ErrorResponsediff --git a/packages/contracts/generated/api/service/zod.gen.ts b/packages/contracts/generated/api/service/zod.gen.tsindex 436c200486a..578de2a6984 100644--- a/packages/contracts/generated/api/service/zod.gen.ts+++ b/packages/contracts/generated/api/service/zod.gen.ts@@ -1227,6 +1227,18 @@ export const zFilenameNotExistsError = zErrorResponse.and(}),)+/**+ * ForbiddenError+ *+ * You don't have the permission to access the requested resource.+ */+export const zForbiddenError = zErrorResponse.and(+ z.object({+ code: z.literal('forbidden'),+ status: z.literal(403),+ }),+)+/*** HitTestingChildChunk*/