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): share conversation variable queries and updates (#43997)
非法操作ac0bdd24db3a
diff --git a/api/.importlinter b/api/.importlinterindex bec3ef4daf6..a7d14791f03 100644--- a/api/.importlinter+++ b/api/.importlinter@@ -1044,12 +1044,13 @@ forbidden_modules =werkzeug[importlinter:contract:conversation-name-boundary]-name = Conversation naming, commands and queries are framework and persistence neutral+name = Conversation application services are framework and persistence neutraltype = forbiddensource_modules =services.conversation_name_serviceservices.conversation_command_serviceservices.conversation_query_service+ services.conversation_variable_command_serviceforbidden_modules =controllersextensionsdiff --git a/api/controllers/common/errors.py b/api/controllers/common/errors.pyindex ce50ccf3213..987023931ae 100644--- a/api/controllers/common/errors.py+++ b/api/controllers/common/errors.py@@ -105,6 +105,18 @@ class ConversationCursorNotFoundHTTPError(BaseHTTPException):code = HTTPStatus.NOT_FOUND+class ConversationVariableNotFoundHTTPError(BaseHTTPException):+ error_code = "conversation_variable_not_found"+ description = "The conversation variable was not found for this conversation."+ code = HTTPStatus.NOT_FOUND+++class ConversationVariableTypeMismatchHTTPError(BaseHTTPException):+ error_code = "conversation_variable_type_mismatch"+ description = "The new value does not match the conversation variable's type."+ code = HTTPStatus.BAD_REQUEST++class ConversationFirstMessageNotFoundHTTPError(BaseHTTPException):error_code = "conversation_first_message_not_found"description = "The conversation has no message from which to generate a name."diff --git a/api/controllers/service_api/app/conversation.py b/api/controllers/service_api/app/conversation.pyindex c3d9863c94c..53d1a27a0c2 100644--- a/api/controllers/service_api/app/conversation.py+++ b/api/controllers/service_api/app/conversation.py@@ -4,8 +4,7 @@ from typing import Annotated, Any, Literalfrom uuid import UUIDfrom flask_restx import Resource-from pydantic import BaseModel, Field, WithJsonSchema, field_validator-from werkzeug.exceptions import BadRequest, NotFound+from pydantic import BaseModel, Field, JsonValue, WithJsonSchema, field_validatorimport servicesfrom controllers.common.controller_schemas import ConversationRenamePayload@@ -13,6 +12,8 @@ from controllers.common.errors import (ConversationCursorNotFoundHTTPError,ConversationFirstMessageNotFoundHTTPError,ConversationNotFoundHTTPError,+ ConversationVariableNotFoundHTTPError,+ ConversationVariableTypeMismatchHTTPError,)from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_modelsfrom controllers.console.wraps import model_validate@@ -20,9 +21,8 @@ from controllers.service_api import service_api_nsfrom controllers.service_api.app.error import NotChatAppErrorfrom controllers.service_api.flask_admission import service_api_end_user_admissionfrom controllers.service_api.schema import expect_user_json, expect_with_user-from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token+from controllers.service_api.wraps import FetchUserArg, WhereisUserArgfrom extensions.ext_application_services import application_services-from extensions.ext_database import dbfrom fields._value_type_serializer import serialize_value_typefrom fields.base import ResponseModelfrom fields.conversation_fields import (@@ -33,8 +33,7 @@ from graphon.variables.types import SegmentTypefrom libs.external_api import error_responsesfrom libs.helper import UUIDStrOrEmpty, dump_response, to_timestampfrom machinery.context import ServiceApiEndUserContext-from models.model import App, AppMode, EndUser-from services.conversation_service import ConversationService+from models.model import AppModefrom services.entities.conversation_entities import EndUserConversationRef, EndUserConversationScopefrom services.errors.message import MessageNotExistsError@@ -92,7 +91,7 @@ class ConversationVariablesQuery(BaseModel):class ConversationVariableUpdatePayload(BaseModel):- value: Any = Field(description="The new value for the variable. Must match the variable's expected type.")+ value: JsonValue = Field(description="The new value for the variable. Must match the variable's expected type.")class ConversationVariableResponse(ResponseModel):@@ -322,130 +321,149 @@ class ConversationRenameApi(Resource):@service_api_ns.route("/conversations/<uuid:conversation_id>/variables")class ConversationVariablesApi(Resource):- @error_responses(NotChatAppError)+ @error_responses(NotChatAppError, ConversationNotFoundHTTPError, ConversationVariableNotFoundHTTPError)@service_api_ns.doc(summary="List Conversation Variables",description="Retrieve variables from a specific conversation.",tags=["Conversations"],responses={- 200: "Successfully retrieved conversation variables.",- 400: "`not_chat_app` : App mode does not match the API route.",- 404: "`not_found` : Conversation does not exist.",+ HTTPStatus.OK: "Conversation variables retrieved successfully.",+ HTTPStatus.BAD_REQUEST: "Invalid app selection or app mode does not match the API route.",+ HTTPStatus.UNAUTHORIZED: "Invalid API credentials.",+ HTTPStatus.FORBIDDEN: "API credential, app or workspace access is unavailable.",+ HTTPStatus.NOT_FOUND: (+ "`conversation_not_found`: Conversation does not exist for this user and app. "+ "`conversation_variable_not_found`: Cursor variable is outside the current list."+ ),+ HTTPStatus.UNPROCESSABLE_ENTITY: "Invalid conversation variables query.",},)@service_api_ns.doc("list_conversation_variables")- @service_api_ns.doc(description="List all variables for a conversation")@service_api_ns.doc(params={"conversation_id": "Conversation ID.", **query_params_from_model(ConversationVariablesQuery)})- @service_api_ns.doc(- responses={- 200: "Variables retrieved successfully",- 401: "Unauthorized - invalid API token",- 404: "Conversation not found",- }- )@service_api_ns.response(- 200,+ HTTPStatus.OK,"Variables retrieved successfully",service_api_ns.models[ConversationVariableInfiniteScrollPaginationResponse.__name__],)- @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))+ @service_api_end_user_admission(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))@model_validate(ConversationVariablesQuery)- def get(self, query_args: ConversationVariablesQuery, app_model: App, end_user: EndUser, conversation_id: UUID):- """List all variables for a conversation.-- Conversational variables are only available for chat applications.- """- # conversational variable only for chat app- app_mode = AppMode.value_of(app_model.mode)+ def get(+ self, query_args: ConversationVariablesQuery, context: ServiceApiEndUserContext, conversation_id: UUID+ ) -> dict[str, object]:+ """List the current user's conversation variables."""+ app_mode = AppMode.value_of(context.app_mode)if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:raise NotChatAppError()- conversation_id_str = str(conversation_id)-- last_id = query_args.last_id or None-try:- pagination = ConversationService.get_conversational_variable(- app_model,- conversation_id_str,- end_user,- query_args.limit,- last_id,- query_args.variable_name,- session=db.session(),+ pagination = application_services().conversation_variables.get_page(+ ref=EndUserConversationRef(+ app_id=context.app_id,+ tenant_id=context.tenant_id,+ end_user_id=context.end_user_id,+ conversation_id=str(conversation_id),+ ),+ last_id=query_args.last_id or None,+ limit=query_args.limit,+ variable_name=query_args.variable_name,+ )+ return dump_response(+ ConversationVariableInfiniteScrollPaginationResponse,+ {+ "limit": pagination.limit,+ "has_more": pagination.has_more,+ "data": [+ {+ "created_at": row.created_at,+ "updated_at": row.updated_at,+ **row.variable.model_dump(),+ "id": row.id,+ }+ for row in pagination.data+ ],+ },)- return dump_response(ConversationVariableInfiniteScrollPaginationResponse, pagination)- except services.errors.conversation.ConversationNotExistsError as caught_error:- raise NotFound("Conversation Not Exists.") from caught_error+ except services.errors.conversation.ConversationNotExistsError as error:+ raise ConversationNotFoundHTTPError() from error+ except services.errors.conversation.ConversationVariableNotExistsError as error:+ raise ConversationVariableNotFoundHTTPError() from error@service_api_ns.route("/conversations/<uuid:conversation_id>/variables/<uuid:variable_id>")class ConversationVariableDetailApi(Resource):- @error_responses(NotChatAppError)+ @error_responses(+ NotChatAppError,+ ConversationNotFoundHTTPError,+ ConversationVariableNotFoundHTTPError,+ ConversationVariableTypeMismatchHTTPError,+ )@service_api_ns.doc(summary="Update Conversation Variable",description="Update the value of a specific conversation variable. The value must match the expected type.",tags=["Conversations"],responses={- 200: "Variable updated successfully.",- 400: (- "- `not_chat_app` : App mode does not match the API route.\n"- "- `bad_request` : Variable value type mismatch."+ HTTPStatus.OK: "Variable updated successfully.",+ HTTPStatus.BAD_REQUEST: (+ "Invalid app selection or app mode does not match the API route. "+ "`conversation_variable_type_mismatch`: Value does not match the variable's expected type. "+ "`invalid_param`: Value violates variable constraints, including the size limit."),- 404: (- "- `not_found` : Conversation does not exist.\n- `not_found` : Conversation variable does not exist."+ HTTPStatus.UNAUTHORIZED: "Invalid API credentials.",+ HTTPStatus.FORBIDDEN: "API credential, app or workspace access is unavailable.",+ HTTPStatus.NOT_FOUND: (+ "`conversation_not_found`: Conversation does not exist for this user and app. "+ "`conversation_variable_not_found`: Variable does not exist for this conversation."),+ HTTPStatus.UNPROCESSABLE_ENTITY: "Invalid conversation variable update payload.",},)@expect_with_user(service_api_ns, ConversationVariableUpdatePayload)@service_api_ns.doc("update_conversation_variable")- @service_api_ns.doc(description="Update a conversation variable's value")@service_api_ns.doc(params={"conversation_id": "Conversation ID.", "variable_id": "Variable ID."})- @service_api_ns.doc(- responses={- 200: "Variable updated successfully",- 400: "Bad request - type mismatch",- 401: "Unauthorized - invalid API token",- 404: "Conversation or variable not found",- }- )@service_api_ns.response(- 200,+ HTTPStatus.OK,"Variable updated successfully",service_api_ns.models[ConversationVariableResponse.__name__],)- @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))+ @service_api_end_user_admission(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))@model_validate(ConversationVariableUpdatePayload)def put(self,payload: ConversationVariableUpdatePayload,- app_model: App,- end_user: EndUser,+ context: ServiceApiEndUserContext,conversation_id: UUID,variable_id: UUID,- ):- """Update a conversation variable's value.-- Allows updating the value of a specific conversation variable.- The value must match the variable's expected type.- """- app_mode = AppMode.value_of(app_model.mode)+ ) -> dict[str, object]:+ """Update a value without changing the variable's identity, type, or metadata."""+ app_mode = AppMode.value_of(context.app_mode)if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:raise NotChatAppError()- conversation_id_str = str(conversation_id)- variable_id_str = str(variable_id)-try:- variable = ConversationService.update_conversation_variable(- app_model, conversation_id_str, variable_id_str, end_user, payload.value, session=db.session()+ row = application_services().conversation_variable_commands.update_value(+ ref=EndUserConversationRef(+ app_id=context.app_id,+ tenant_id=context.tenant_id,+ end_user_id=context.end_user_id,+ conversation_id=str(conversation_id),+ ),+ variable_id=str(variable_id),+ new_value=payload.value,)- return dump_response(ConversationVariableResponse, variable)- except services.errors.conversation.ConversationNotExistsError as caught_error:- raise NotFound("Conversation Not Exists.") from caught_error- except services.errors.conversation.ConversationVariableNotExistsError as caught_error:- raise NotFound("Conversation Variable Not Exists.") from caught_error- except services.errors.conversation.ConversationVariableTypeMismatchError as e:- raise BadRequest(str(e)) from e+ return dump_response(+ ConversationVariableResponse,+ {+ "created_at": row.created_at,+ "updated_at": row.updated_at,+ **row.variable.model_dump(),+ "id": row.id,+ },+ )+ except services.errors.conversation.ConversationNotExistsError as error:+ raise ConversationNotFoundHTTPError() from error+ except services.errors.conversation.ConversationVariableNotExistsError as error:+ raise ConversationVariableNotFoundHTTPError() from error+ except services.errors.conversation.ConversationVariableTypeMismatchError as error:+ raise ConversationVariableTypeMismatchHTTPError(str(error)) from errordiff --git a/api/extensions/ext_application_services.py b/api/extensions/ext_application_services.pyindex 22d3378e2c3..29f371875ff 100644--- a/api/extensions/ext_application_services.py+++ b/api/extensions/ext_application_services.py@@ -69,7 +69,7 @@ from repositories.app_preview_query_repository import AppPreviewQueryRepositoryfrom repositories.app_scoped_end_user_repository import AppScopedEndUserRepofrom repositories.app_statistic_query_repository import AppStatisticQueryRepositoryfrom repositories.conversation_repository import ConversationRepository-from repositories.conversation_variable_query_repository import ConversationVariableQueryRepository+from repositories.conversation_variable_repository import ConversationVariableRepositoryfrom repositories.credentials.query_repository import CredentialQueryRepositoryfrom repositories.explore_banner_query_repository import ExploreBannerQueryRepositoryfrom repositories.factory import DifyAPIRepositoryFactory@@ -142,6 +142,7 @@ from services.conversation_name_generator import generate_conversation_namefrom services.conversation_name_service import ConversationNameServicefrom services.conversation_query_service import ConversationQueryServicefrom services.conversation_service import ConversationService+from services.conversation_variable_command_service import ConversationVariableCommandServicefrom services.conversation_variable_query import ConversationVariableQueryfrom services.credentials.query import CredentialQueryfrom services.enterprise.enterprise_service import EnterpriseService@@ -312,6 +313,7 @@ class ApplicationServices:app_previews: AppPreviewQueryServiceapp_sites: AppSiteServiceconversation_variables: ConversationVariableQuery+ conversation_variable_commands: ConversationVariableCommandServiceapp_statistics: AppStatisticQueryapp_tracing_configs: AppTracingConfigServiceagent_tracing_configs: AgentTracingConfigService@@ -587,6 +589,7 @@ def build_application_services(conversations=conversations, enqueue_delete_cleanup=ConversationService.enqueue_delete_cleanup)conversation_queries = ConversationQueryService(conversations=conversations, restore_inputs=restore_input_files)+ conversation_variables = ConversationVariableRepository(session_factory=database_client)annotations = AnnotationRepository(session_factory=database_client)messages = MessageRepository(session_factory=database_client)suggested_questions = SuggestedQuestionsQuery(@@ -642,7 +645,8 @@ def build_application_services(app_sites=AppSiteService(sites=AppSiteCommandRepository(session_factory=database_client),),- conversation_variables=ConversationVariableQueryRepository(session_factory=database_client),+ conversation_variables=conversation_variables,+ conversation_variable_commands=ConversationVariableCommandService(variables=conversation_variables),app_api_keys=build_app_api_key_service(database_client=database_client),dataset_api_keys=build_dataset_api_key_service(database_client=database_client,diff --git a/api/openapi/markdown/service-openapi.md b/api/openapi/markdown/service-openapi.mdindex 34f3fd07f64..6c0f1fc4dc3 100644--- a/api/openapi/markdown/service-openapi.md+++ b/api/openapi/markdown/service-openapi.md@@ -2401,11 +2401,12 @@ Retrieve variables from a specific conversation.| Code | Description | Schema || ---- | ----------- | ------ |-| 200 | Successfully retrieved conversation variables. | **application/json**: [ConversationVariableInfiniteScrollPaginationResponse](#conversationvariableinfinitescrollpaginationresponse)<br> |-| 400 | `not_chat_app` : App mode does not match the API route. | **application/json**: [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**: [ErrorResponse](#errorresponse)<br> |-| 404 | `not_found` : Conversation does not exist. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 200 | Conversation variables retrieved successfully. | **application/json**: [ConversationVariableInfiniteScrollPaginationResponse](#conversationvariableinfinitescrollpaginationresponse)<br> |+| 400 | Invalid app selection or app mode does not match the API route. | **application/json**: [NotChatAppError](#notchatapperror)<br>[ErrorResponse](#errorresponse)<br> |+| 401 | Invalid API credentials. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 403 | API credential, app or workspace access is unavailable. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 404 | `conversation_not_found`: Conversation does not exist for this user and app. `conversation_variable_not_found`: Cursor variable is outside the current list. | **application/json**: [ConversationNotFoundError](#conversationnotfounderror)<br>[ConversationVariableNotFoundError](#conversationvariablenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 422 | Invalid conversation variables query. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |### [PUT] /conversations/{conversation_id}/variables/{variable_id}@@ -2431,10 +2432,11 @@ Update the value of a specific conversation variable. The value must match the e| Code | Description | Schema || ---- | ----------- | ------ || 200 | Variable updated successfully. | **application/json**: [ConversationVariableResponse](#conversationvariableresponse)<br> |-| 400 | - `not_chat_app` : App mode does not match the API route. - `bad_request` : Variable value type mismatch. | **application/json**: [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**: [ErrorResponse](#errorresponse)<br> |-| 404 | - `not_found` : Conversation does not exist. - `not_found` : Conversation variable does not exist. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 400 | Invalid app selection or app mode does not match the API route. `conversation_variable_type_mismatch`: Value does not match the variable's expected type. `invalid_param`: Value violates variable constraints, including the size limit. | **application/json**: [ConversationVariableTypeMismatchError](#conversationvariabletypemismatcherror)<br>[NotChatAppError](#notchatapperror)<br>[ErrorResponse](#errorresponse)<br> |+| 401 | Invalid API credentials. | **application/json**: [ErrorResponse](#errorresponse)<br> |+| 403 | API credential, app or workspace access is unavailable. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 404 | `conversation_not_found`: Conversation does not exist for this user and app. `conversation_variable_not_found`: Variable does not exist for this conversation. | **application/json**: [ConversationNotFoundError](#conversationnotfounderror)<br>[ConversationVariableNotFoundError](#conversationvariablenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |+| 422 | Invalid conversation variable update payload. | **application/json**: [ErrorResponse](#errorresponse)<br> || default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |### [GET] /messages@@ -4618,6 +4620,14 @@ The conversation was not found for this user and app.| has_more | boolean | | Yes || limit | integer | | Yes |+#### ConversationVariableNotFoundError++The conversation variable was not found for this conversation.++| Name | Type | Description | Required |+| ---- | ---- | ----------- | -------- |+| ConversationVariableNotFoundError | | The conversation variable was not found for this conversation. | |+#### ConversationVariableResponse| Name | Type | Description | Required |@@ -4630,18 +4640,26 @@ The conversation was not found for this user and app.| value | string | | No || value_type | string | | Yes |+#### ConversationVariableTypeMismatchError++The new value does not match the conversation variable's type.++| Name | Type | Description | Required |+| ---- | ---- | ----------- | -------- |+| ConversationVariableTypeMismatchError | | The new value does not match the conversation variable's type. | |+#### ConversationVariableUpdatePayload| Name | Type | Description | Required || ---- | ---- | ----------- | -------- |-| value | | The new value for the variable. Must match the variable's expected type. | Yes |+| value | [JsonValue](#jsonvalue) | The new value for the variable. Must match the variable's expected type. | Yes |#### ConversationVariableUpdatePayloadWithUser| Name | Type | Description | Required || ---- | ---- | ----------- | -------- || user | string | End-user identifier, defined by your app and unique within it. Identifies the end user for this request. See [End User Identity](/api-reference/guides/end-user-identity) for endpoint-specific access rules. | No |-| value | | The new value for the variable. Must match the variable's expected type. | Yes |+| value | [JsonValue](#jsonvalue) | The new value for the variable. Must match the variable's expected type. | Yes |#### ConversationVariablesQuerydiff --git a/api/repositories/conversation_variable_query_repository.py b/api/repositories/conversation_variable_query_repository.pydeleted file mode 100644index 59a4ab68cac..00000000000--- a/api/repositories/conversation_variable_query_repository.py+++ /dev/null@@ -1,61 +0,0 @@-"""Materialized Console conversation-variable reads with owned sessions."""--from typing import override--from sqlalchemy import select-from sqlalchemy.orm import Session, sessionmaker--from machinery.context import RequestContext-from models import App, AppMode, ConversationVariable-from models.enums import AppStatus-from repositories.app.console_repository import find_console_app-from services.conversation_variable_query import (- CONVERSATION_VARIABLE_LIMIT,- ConversationVariableAppNotFoundError,- ConversationVariableQuery,- ConversationVariableRecord,-)---class ConversationVariableQueryRepository(ConversationVariableQuery):- def __init__(self, *, session_factory: sessionmaker[Session]) -> None:- self._session_factory = session_factory-- @override- def require_app(self, context: RequestContext, app_id: str) -> None:- with self._session_factory() as session:- app = find_console_app(session, workspace_id=context.active_workspace_id, app_id=app_id)- if app is None:- raise ConversationVariableAppNotFoundError("App not found.")- if AppMode.value_of(app.mode) != AppMode.ADVANCED_CHAT:- modes = {AppMode.ADVANCED_CHAT.value}- raise ConversationVariableAppNotFoundError(f"App mode is not in the supported list: {modes}")-- @override- def list_variables(- self, context: RequestContext, app_id: str, conversation_id: str- ) -> list[ConversationVariableRecord]:- with self._session_factory() as session:- rows = session.scalars(- select(ConversationVariable)- .join(App, App.id == ConversationVariable.app_id)- .where(- App.id == app_id,- App.tenant_id == context.active_workspace_id,- App.status == AppStatus.NORMAL,- App.mode == AppMode.ADVANCED_CHAT,- ConversationVariable.conversation_id == conversation_id,- )- .order_by(ConversationVariable.created_at)- .limit(CONVERSATION_VARIABLE_LIMIT)- )- # Decode inside the owned session; neither rows nor lazy loads escape.- return [- ConversationVariableRecord(- id=row.id,- variable=row.to_variable(),- created_at=row.created_at,- updated_at=row.updated_at,- )- for row in rows- ]diff --git a/api/repositories/conversation_variable_repository.py b/api/repositories/conversation_variable_repository.pynew file mode 100644index 00000000000..3a9d7757bf1--- /dev/null+++ b/api/repositories/conversation_variable_repository.py@@ -0,0 +1,172 @@+"""Conversation-variable persistence with fully scoped, bounded sessions."""++from collections.abc import Callable+from typing import override++from sqlalchemy import JSON, ColumnElement, Select, cast, func, select+from sqlalchemy.orm import Session, sessionmaker++from graphon.variables import VariableBase+from libs.datetime_utils import naive_utc_now+from libs.helper import escape_like_pattern+from machinery.context import RequestContext+from models import App, AppMode, ConversationVariable+from models.enums import AppStatus, ConversationFromSource+from models.model import Conversation, EndUser+from repositories.app.console_repository import find_console_app+from services.conversation_variable_query import (+ CONVERSATION_VARIABLE_LIMIT,+ ConversationVariableAppNotFoundError,+ ConversationVariablePage,+ ConversationVariableQuery,+ ConversationVariableRecord,+)+from services.entities.conversation_entities import EndUserConversationRef+from services.errors.conversation import ConversationNotExistsError, ConversationVariableNotExistsError+++class ConversationVariableRepository(ConversationVariableQuery):+ def __init__(self, *, session_factory: sessionmaker[Session]) -> None:+ self._session_factory = session_factory++ @override+ def require_app(self, context: RequestContext, app_id: str) -> None:+ with self._session_factory() as session:+ app = find_console_app(session, workspace_id=context.active_workspace_id, app_id=app_id)+ if app is None:+ raise ConversationVariableAppNotFoundError("App not found.")+ if AppMode.value_of(app.mode) != AppMode.ADVANCED_CHAT:+ modes = {AppMode.ADVANCED_CHAT.value}+ raise ConversationVariableAppNotFoundError(f"App mode is not in the supported list: {modes}")++ @override+ def list_variables(+ self, context: RequestContext, app_id: str, conversation_id: str+ ) -> list[ConversationVariableRecord]:+ with self._session_factory() as session:+ rows = session.scalars(+ select(ConversationVariable)+ .join(App, App.id == ConversationVariable.app_id)+ .where(+ App.id == app_id,+ App.tenant_id == context.active_workspace_id,+ App.status == AppStatus.NORMAL,+ App.mode == AppMode.ADVANCED_CHAT,+ ConversationVariable.conversation_id == conversation_id,+ )+ .order_by(ConversationVariable.created_at)+ .limit(CONVERSATION_VARIABLE_LIMIT)+ )+ # Decode inside the owned session; neither rows nor lazy loads escape.+ return [+ ConversationVariableRecord(+ id=row.id,+ variable=row.to_variable(),+ created_at=row.created_at,+ updated_at=row.updated_at,+ )+ for row in rows+ ]++ @override+ def get_page(+ self,+ *,+ ref: EndUserConversationRef,+ last_id: str | None,+ limit: int,+ variable_name: str | None,+ ) -> ConversationVariablePage:+ with self._session_factory() as session:+ stmt = self._variables(session=session, ref=ref)+ if variable_name:+ stmt = stmt.where(+ self._name_filter(variable_name=variable_name, dialect_name=session.get_bind().dialect.name)+ )+ if last_id:+ cursor = session.scalar(stmt.where(ConversationVariable.id == last_id))+ if cursor is None:+ raise ConversationVariableNotExistsError()+ stmt = stmt.where(+ (ConversationVariable.created_at > cursor.created_at)+ | ((ConversationVariable.created_at == cursor.created_at) & (ConversationVariable.id > cursor.id))+ )+ rows = session.scalars(+ stmt.order_by(ConversationVariable.created_at, ConversationVariable.id).limit(limit + 1)+ ).all()+ return ConversationVariablePage(+ limit=limit,+ has_more=len(rows) > limit,+ data=tuple(self._record(row) for row in rows[:limit]),+ )++ def update(+ self,+ *,+ ref: EndUserConversationRef,+ variable_id: str,+ transform: Callable[[VariableBase], VariableBase],+ ) -> ConversationVariableRecord:+ """Apply a pure transformation in the write transaction; it must perform no external I/O."""+ with self._session_factory.begin() as session:+ stmt = self._variables(session=session, ref=ref)+ row = session.scalar(stmt.where(ConversationVariable.id == variable_id).with_for_update())+ if row is None:+ raise ConversationVariableNotExistsError()+ variable = transform(row.to_variable())+ row.data = variable.model_dump_json()+ row.updated_at = naive_utc_now()+ record = ConversationVariableRecord(+ id=row.id,+ variable=variable,+ created_at=row.created_at,+ updated_at=row.updated_at,+ )+ return record++ @staticmethod+ def _name_filter(*, variable_name: str, dialect_name: str) -> ColumnElement[bool]:+ # The stored payload is TEXT, while PostgreSQL's extractor requires JSON.+ name = (+ func.json_extract_path_text(cast(ConversationVariable.data, JSON), "name")+ if dialect_name == "postgresql"+ else func.json_extract(ConversationVariable.data, "$.name")+ )+ return name.ilike(f"%{escape_like_pattern(variable_name)}%", escape="\\")++ @staticmethod+ def _variables(*, session: Session, ref: EndUserConversationRef) -> Select[tuple[ConversationVariable]]:+ owner = (+ select(Conversation.id)+ .join(App, App.id == Conversation.app_id)+ .join(+ EndUser,+ (EndUser.id == ref.end_user_id) & (EndUser.app_id == App.id) & (EndUser.tenant_id == App.tenant_id),+ )+ .where(+ App.id == ref.app_id,+ App.tenant_id == ref.tenant_id,+ Conversation.id == ref.conversation_id,+ Conversation.from_source == ConversationFromSource.API,+ Conversation.from_end_user_id == ref.end_user_id,+ Conversation.from_account_id.is_(None),+ Conversation.is_deleted.is_(False),+ )+ )+ if session.scalar(owner) is None:+ raise ConversationNotExistsError(+ f"Conversation {ref.conversation_id} does not belong to this end user and app."+ )+ return select(ConversationVariable).where(+ ConversationVariable.app_id == ref.app_id,+ ConversationVariable.conversation_id.in_(owner),+ )++ @staticmethod+ def _record(row: ConversationVariable) -> ConversationVariableRecord:+ return ConversationVariableRecord(+ id=row.id,+ variable=row.to_variable(),+ created_at=row.created_at,+ updated_at=row.updated_at,+ )diff --git a/api/services/conversation_service.py b/api/services/conversation_service.pyindex e5fd0f0a39f..412a027cd74 100644--- a/api/services/conversation_service.py+++ b/api/services/conversation_service.py@@ -1,22 +1,12 @@import logging-from typing import Any-from sqlalchemy import func, select+from sqlalchemy import selectfrom sqlalchemy.orm import Session-from configs import dify_config-from factories import variable_factory-from graphon.variables.types import SegmentType-from libs.datetime_utils import naive_utc_now-from libs.infinite_scroll_pagination import InfiniteScrollPagination-from models import Account, ConversationVariable+from models import Accountfrom models.model import App, Conversation, EndUserfrom repositories.conversation_lifecycle import retire_conversation-from services.errors.conversation import (- ConversationNotExistsError,- ConversationVariableNotExistsError,- ConversationVariableTypeMismatchError,-)+from services.errors.conversation import ConversationNotExistsErrorfrom tasks.collect_agent_resources_task import enqueue_agent_resource_collectionfrom tasks.delete_conversation_task import delete_conversation_related_data@@ -85,153 +75,3 @@ class ConversationService:# The soft-deleted row is a durable cleanup marker picked up by the# periodic sweeper, so a broker outage must not resurrect or expose it.logger.exception("Failed to enqueue cleanup for conversation %s", conversation_id)-- @classmethod- def get_conversational_variable(- cls,- app_model: App,- conversation_id: str,- user: Account | EndUser | None,- limit: int,- last_id: str | None,- variable_name: str | None = None,- *,- session: Session,- ) -> InfiniteScrollPagination:- conversation = cls.get_conversation(app_model, conversation_id, user, session=session)-- stmt = (- select(ConversationVariable)- .where(ConversationVariable.app_id == app_model.id, ConversationVariable.conversation_id == conversation.id)- .order_by(ConversationVariable.created_at)- )-- # Apply variable_name filter if provided- if variable_name:- # Filter using JSON extraction to match variable names case-insensitively- from libs.helper import escape_like_pattern-- escaped_variable_name = escape_like_pattern(variable_name)- # Filter using JSON extraction to match variable names case-insensitively- if dify_config.DB_TYPE in ["mysql", "oceanbase", "seekdb"]:- stmt = stmt.where(- func.json_extract(ConversationVariable.data, "$.name").ilike(- f"%{escaped_variable_name}%", escape="\\"- )- )- elif dify_config.DB_TYPE == "postgresql":- stmt = stmt.where(- func.json_extract_path_text(ConversationVariable.data, "name").ilike(- f"%{escaped_variable_name}%", escape="\\"- )- )-- if last_id:- last_variable = session.scalar(stmt.where(ConversationVariable.id == last_id))- if not last_variable:- raise ConversationVariableNotExistsError()-- # Filter for variables created after the last_id- stmt = stmt.where(ConversationVariable.created_at > last_variable.created_at)-- # Apply limit to query: fetch one extra row to determine has_more- query_stmt = stmt.limit(limit + 1)- rows = session.scalars(query_stmt).all()-- has_more = False- if len(rows) > limit:- has_more = True- rows = rows[:limit] # Remove the extra item-- variables = [- {- "created_at": row.created_at,- "updated_at": row.updated_at,- **row.to_variable().model_dump(),- "id": row.id,- }- for row in rows- ]-- return InfiniteScrollPagination(variables, limit, has_more)-- @classmethod- def update_conversation_variable(- cls,- app_model: App,- conversation_id: str,- variable_id: str,- user: Account | EndUser | None,- new_value: Any,- *,- session: Session,- ):- """- Update a conversation variable's value.-- Args:- app_model: The app model- conversation_id: The conversation ID- variable_id: The variable ID to update- user: The user (Account or EndUser)- new_value: The new value for the variable-- Returns:- Dictionary containing the updated variable information-- Raises:- ConversationNotExistsError: If the conversation doesn't exist- ConversationVariableNotExistsError: If the variable doesn't exist- ConversationVariableTypeMismatchError: If the new value type doesn't match the variable's expected type- """- # Verify conversation exists and user has access- conversation = cls.get_conversation(app_model, conversation_id, user, session=session)-- # Get the existing conversation variable- stmt = select(ConversationVariable).where(- ConversationVariable.app_id == app_model.id,- ConversationVariable.conversation_id == conversation.id,- ConversationVariable.id == variable_id,- )-- existing_variable = session.scalar(stmt)- if not existing_variable:- raise ConversationVariableNotExistsError()-- # Convert existing variable to Variable object- current_variable = existing_variable.to_variable()-- # Validate that the new value type matches the expected variable type- expected_type = SegmentType(current_variable.value_type)-- # There is showing number in web ui but int in db- if expected_type == SegmentType.INTEGER:- expected_type = SegmentType.NUMBER-- if not expected_type.is_valid(new_value):- inferred_type = SegmentType.infer_segment_type(new_value)- raise ConversationVariableTypeMismatchError(- f"Type mismatch: variable '{current_variable.name}' expects {expected_type.value}, "- f"but got {inferred_type.value if inferred_type else 'unknown'} type"- )-- # Create updated variable with new value only, preserving everything else- updated_variable_dict = {- "id": current_variable.id,- "name": current_variable.name,- "description": current_variable.description,- "value_type": current_variable.value_type,- "value": new_value,- "selector": current_variable.selector,- }-- updated_variable = variable_factory.build_conversation_variable_from_mapping(updated_variable_dict)- existing_variable.data = updated_variable.model_dump_json()- session.commit()-- return {- "created_at": existing_variable.created_at,- "updated_at": naive_utc_now(), # Update timestamp- **updated_variable.model_dump(),- "id": existing_variable.id,- }diff --git a/api/services/conversation_variable_command_service.py b/api/services/conversation_variable_command_service.pynew file mode 100644index 00000000000..7d01f9bf4f7--- /dev/null+++ b/api/services/conversation_variable_command_service.py@@ -0,0 +1,68 @@+"""Change conversation-variable values without exposing persistence to the use case."""++from collections.abc import Callable+from functools import partial+from typing import Protocol++from pydantic import JsonValue++from factories import variable_factory+from graphon.variables import VariableBase+from graphon.variables.types import SegmentType+from services.conversation_variable_query import ConversationVariableRecord+from services.entities.conversation_entities import EndUserConversationRef+from services.errors.conversation import ConversationVariableTypeMismatchError+++class ConversationVariableStore(Protocol):+ def update(+ self,+ *,+ ref: EndUserConversationRef,+ variable_id: str,+ transform: Callable[[VariableBase], VariableBase],+ ) -> ConversationVariableRecord:+ """Apply a pure value transform within the owned write transaction.++ The transform must not perform I/O. Ownership, loading the current value,+ transformation, and saving complete in one transaction.+ """+ ...+++class ConversationVariableCommandService:+ def __init__(self, *, variables: ConversationVariableStore) -> None:+ self._variables = variables++ def update_value(+ self, *, ref: EndUserConversationRef, variable_id: str, new_value: JsonValue+ ) -> ConversationVariableRecord:+ return self._variables.update(+ ref=ref,+ variable_id=variable_id,+ transform=partial(self._replace_value, new_value=new_value),+ )++ @staticmethod+ def _replace_value(current_variable: VariableBase, *, new_value: JsonValue) -> VariableBase:+ expected_type = SegmentType(current_variable.value_type)+ # The API exposes stored integers as numbers; preserve the existing check+ # and let the shared factory retain its numeric reconstruction rules.+ if expected_type == SegmentType.INTEGER:+ expected_type = SegmentType.NUMBER+ if not expected_type.is_valid(new_value):+ inferred_type = SegmentType.infer_segment_type(new_value)+ raise ConversationVariableTypeMismatchError(+ f"Type mismatch: variable '{current_variable.name}' expects {expected_type.value}, "+ f"but got {inferred_type.value if inferred_type else 'unknown'} type"+ )+ return variable_factory.build_conversation_variable_from_mapping(+ {+ "id": current_variable.id,+ "name": current_variable.name,+ "description": current_variable.description,+ "value_type": current_variable.value_type,+ "value": new_value,+ "selector": current_variable.selector,+ }+ )diff --git a/api/services/conversation_variable_query.py b/api/services/conversation_variable_query.pyindex bc05f24df62..3b12ba939c4 100644--- a/api/services/conversation_variable_query.py+++ b/api/services/conversation_variable_query.py@@ -1,4 +1,4 @@-"""Application query boundary for Console conversation variables."""+"""Application query boundary for owned conversation variables."""from collections.abc import Sequencefrom dataclasses import dataclass@@ -7,6 +7,7 @@ from typing import Protocolfrom graphon.variables import VariableBasefrom machinery.context import RequestContext+from services.entities.conversation_entities import EndUserConversationRef# The Console endpoint exposes only the first page, including its legacy# total/has_more fields. Changing pagination is a separate API change.@@ -21,6 +22,13 @@ class ConversationVariableRecord:updated_at: datetime+@dataclass(frozen=True, slots=True)+class ConversationVariablePage:+ limit: int+ has_more: bool+ data: tuple[ConversationVariableRecord, ...]++class ConversationVariableAppNotFoundError(Exception):"""The app is unavailable to this workspace or has an unsupported mode."""@@ -35,3 +43,14 @@ class ConversationVariableQuery(Protocol):) -> Sequence[ConversationVariableRecord]:"""Read the bounded result under the same workspace and app scope."""...++ def get_page(+ self,+ *,+ ref: EndUserConversationRef,+ last_id: str | None,+ limit: int,+ variable_name: str | None,+ ) -> ConversationVariablePage:+ """Read an end user's variables; None omits the cursor or name filter."""+ ...diff --git a/api/tests/e2e_tests/containers/pyrefly.toml b/api/tests/e2e_tests/containers/pyrefly.tomlindex f16ba47a7bc..81d980d1ac6 100644--- a/api/tests/e2e_tests/containers/pyrefly.toml+++ b/api/tests/e2e_tests/containers/pyrefly.toml@@ -58,7 +58,6 @@ project-excludes = ["services/test_audio_service_db.py","services/test_billing_service.py","services/test_conversation_service.py",- "services/test_conversation_service_variables.py","services/test_conversation_variable_updater.py","services/test_dataset_permission_service.py","services/test_dataset_service.py",diff --git a/api/tests/e2e_tests/containers/repositories/test_conversation_variable_repository.py b/api/tests/e2e_tests/containers/repositories/test_conversation_variable_repository.pynew file mode 100644index 00000000000..0a7ac752044--- /dev/null+++ b/api/tests/e2e_tests/containers/repositories/test_conversation_variable_repository.py@@ -0,0 +1,131 @@+"""Execute conversation-variable JSON filtering and writes against PostgreSQL."""++from datetime import datetime, timedelta+from uuid import UUID, uuid4++import pytest+from sqlalchemy.orm import Session, sessionmaker++from factories import variable_factory+from models import App, AppMode, ConversationVariable+from models.enums import ConversationFromSource, EndUserType+from models.model import Conversation, EndUser+from repositories.conversation_variable_repository import ConversationVariableRepository+from services.conversation_variable_command_service import ConversationVariableCommandService+from services.entities.conversation_entities import EndUserConversationRef+from services.errors.conversation import ConversationVariableNotExistsError+++def test_postgres_filters_text_json_paginates_and_updates_with_row_lock(+ db_session_with_containers: Session,+) -> None:+ engine = db_session_with_containers.get_bind()+ assert engine.dialect.name == "postgresql"+ sessions = sessionmaker(bind=engine, expire_on_commit=False)+ ref = EndUserConversationRef(+ app_id=str(uuid4()),+ tenant_id=str(uuid4()),+ end_user_id=str(uuid4()),+ conversation_id=str(uuid4()),+ )+ created_at = datetime(2020, 1, 1)+ rows: list[ConversationVariable] = []+ with sessions.begin() as session:+ session.add(+ App(+ id=ref.app_id,+ tenant_id=ref.tenant_id,+ name="Conversation variable integration test",+ mode=AppMode.ADVANCED_CHAT,+ enable_site=True,+ enable_api=True,+ )+ )+ session.add(+ EndUser(+ id=ref.end_user_id,+ tenant_id=ref.tenant_id,+ app_id=ref.app_id,+ type=EndUserType.SERVICE_API,+ session_id="conversation-variable-test",+ )+ )+ session.add(+ Conversation(+ id=ref.conversation_id,+ app_id=ref.app_id,+ mode=AppMode.ADVANCED_CHAT,+ name="Test conversation",+ _inputs={},+ from_source=ConversationFromSource.API,+ from_end_user_id=ref.end_user_id,+ from_account_id=None,+ is_deleted=False,+ )+ )+ # Tied timestamps span pages, with a nonmatching row between their storage IDs.+ # The newer matching row has the smallest ID, so timestamp ordering remains primary.+ for index, (storage_id, name, seconds) in enumerate(+ (+ ("00000000-0000-0000-0000-000000000002", "Profile_%\\First", 0),+ ("00000000-0000-0000-0000-000000000003", "ProfileXunescaped", 0),+ ("00000000-0000-0000-0000-000000000004", "PROFILE_%\\Second", 0),+ ("00000000-0000-0000-0000-000000000001", "Profile_%\\Newest", 1),+ )+ ):+ variable = variable_factory.build_conversation_variable_from_mapping(+ {+ "id": f"imported-variable-{index}",+ "name": name,+ "value_type": "string",+ "value": "original",+ "description": "Keep this metadata",+ }+ )+ row = ConversationVariable.from_variable(+ app_id=ref.app_id, conversation_id=ref.conversation_id, variable=variable+ )+ row.id = storage_id+ row.created_at = created_at + timedelta(seconds=seconds)+ row.updated_at = row.created_at+ rows.append(row)+ session.add_all(reversed(rows))++ repository = ConversationVariableRepository(session_factory=sessions)+ first = repository.get_page(ref=ref, last_id=None, limit=1, variable_name="profile_%\\")+ assert [record.id for record in first.data] == [rows[0].id]+ assert first.limit == 1+ assert first.has_more is True+ assert first.data[0].variable.id == "imported-variable-0"+ assert str(UUID(first.data[0].id)) == rows[0].id++ second = repository.get_page(ref=ref, last_id=first.data[0].id, limit=1, variable_name="profile_%\\")+ assert [record.id for record in second.data] == [rows[2].id]+ assert second.data[0].created_at == first.data[0].created_at+ assert second.has_more is True+ third = repository.get_page(ref=ref, last_id=second.data[0].id, limit=1, variable_name="profile_%\\")+ assert [record.id for record in third.data] == [rows[3].id]+ assert third.has_more is False+ empty = repository.get_page(ref=ref, last_id=third.data[0].id, limit=1, variable_name="profile_%\\")+ assert empty.data == ()+ assert empty.has_more is False+ with pytest.raises(ConversationVariableNotExistsError):+ repository.get_page(ref=ref, last_id=rows[1].id, limit=1, variable_name="profile_%\\")++ updated = ConversationVariableCommandService(variables=repository).update_value(+ ref=ref, variable_id=first.data[0].id, new_value="updated"+ )+ assert updated.id == rows[0].id+ assert updated.variable.value == "updated"+ assert updated.variable.model_dump(exclude={"value"}) == first.data[0].variable.model_dump(exclude={"value"})+ assert updated.created_at == created_at+ assert updated.updated_at > created_at++ # A separate session proves the FOR UPDATE transaction committed its changes.+ with sessions() as session:+ stored = session.get_one(ConversationVariable, (updated.id, ref.conversation_id))+ untouched = session.get_one(ConversationVariable, (rows[2].id, ref.conversation_id))+ assert stored.to_variable() == updated.variable+ assert stored.created_at == updated.created_at+ assert stored.updated_at == updated.updated_at+ assert untouched.to_variable().value == "original"diff --git a/api/tests/e2e_tests/containers/services/test_conversation_service_variables.py b/api/tests/e2e_tests/containers/services/test_conversation_service_variables.pydeleted file mode 100644index 14a27d0244a..00000000000--- a/api/tests/e2e_tests/containers/services/test_conversation_service_variables.py+++ /dev/null@@ -1,389 +0,0 @@-from __future__ import annotations--from datetime import datetime, timedelta-from unittest.mock import patch-from uuid import uuid4--import pytest-from flask import Flask-from sqlalchemy.orm import Session--from core.app.entities.app_invoke_entities import InvokeFrom-from graphon.variables import FloatVariable, IntegerVariable, StringVariable-from models.account import Account, Tenant, TenantAccountJoin-from models.enums import ConversationFromSource, EndUserType-from models.model import App, Conversation, EndUser-from models.workflow import ConversationVariable-from services.conversation_service import ConversationService-from services.errors.conversation import (- ConversationVariableNotExistsError,- ConversationVariableTypeMismatchError,-)---class ConversationServiceVariableIntegrationFactory:- @staticmethod- def create_app_and_account(db_session_with_containers: Session):- tenant = Tenant(name=f"Tenant {uuid4()}")- db_session_with_containers.add(tenant)- db_session_with_containers.flush()-- account = Account(- name=f"Account {uuid4()}",- email=f"conversation-variable-{uuid4()}@example.com",- password="hashed-password",- password_salt="salt",- interface_language="en-US",- timezone="UTC",- )- db_session_with_containers.add(account)- db_session_with_containers.flush()-- tenant_join = TenantAccountJoin(- tenant_id=tenant.id,- account_id=account.id,- role="owner",- current=True,- )- db_session_with_containers.add(tenant_join)- db_session_with_containers.flush()-- app = App(- tenant_id=tenant.id,- name=f"App {uuid4()}",- description="",- mode="chat",- icon_type="emoji",- icon="bot",- icon_background="#FFFFFF",- enable_site=False,- enable_api=True,- api_rpm=100,- api_rph=100,- is_demo=False,- is_public=False,- is_universal=False,- created_by=account.id,- updated_by=account.id,- )- db_session_with_containers.add(app)- db_session_with_containers.commit()-- return app, account-- @staticmethod- def create_end_user(db_session_with_containers, app: App):- end_user = EndUser(- tenant_id=app.tenant_id,- app_id=app.id,- type=EndUserType.SERVICE_API,- external_user_id=f"external-{uuid4()}",- name=f"End User {uuid4()}",- _is_anonymous=False,- session_id=f"session-{uuid4()}",- )- db_session_with_containers.add(end_user)- db_session_with_containers.commit()- return end_user-- @staticmethod- def create_conversation(- db_session_with_containers,- app: App,- user: Account | EndUser,- *,- name: str | None = None,- invoke_from: InvokeFrom = InvokeFrom.WEB_APP,- created_at: datetime | None = None,- updated_at: datetime | None = None,- ) -> Conversation:- conversation = Conversation(- app_id=app.id,- app_model_config_id=None,- model_provider=None,- model_id="",- override_model_configs=None,- mode=app.mode,- name=name or f"Conversation {uuid4()}",- summary="",- _inputs={},- introduction="",- system_instruction="",- system_instruction_tokens=0,- status="normal",- invoke_from=invoke_from.value,- from_source=ConversationFromSource.API if isinstance(user, EndUser) else ConversationFromSource.CONSOLE,- from_end_user_id=user.id if isinstance(user, EndUser) else None,- from_account_id=user.id if isinstance(user, Account) else None,- dialogue_count=0,- is_deleted=False,- )- conversation.inputs = {}- if created_at is not None:- conversation.created_at = created_at- if updated_at is not None:- conversation.updated_at = updated_at-- db_session_with_containers.add(conversation)- db_session_with_containers.commit()- return conversation-- @staticmethod- def create_variable(- db_session_with_containers,- *,- app: App,- conversation: Conversation,- variable: StringVariable | FloatVariable | IntegerVariable,- created_at: datetime | None = None,- ) -> ConversationVariable:- row = ConversationVariable.from_variable(app_id=app.id, conversation_id=conversation.id, variable=variable)- if created_at is not None:- row.created_at = created_at- row.updated_at = created_at-- db_session_with_containers.add(row)- db_session_with_containers.commit()- return row---def real_conversation_service_session_factory(flask_app_with_containers: Flask):- del flask_app_with_containers---class TestConversationServiceVariables:- def test_get_conversational_variable_success(- self, db_session_with_containers: Session, real_conversation_service_session_factory- ):- del real_conversation_service_session_factory- factory = ConversationServiceVariableIntegrationFactory- app, account = factory.create_app_and_account(db_session_with_containers)- conversation = factory.create_conversation(db_session_with_containers, app, account)- older_time = datetime(2024, 1, 1, 12, 0, 0)- newer_time = older_time + timedelta(minutes=5)-- first_variable = factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=StringVariable(id=str(uuid4()), name="topic", value="billing"),- created_at=older_time,- )- second_variable = factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=StringVariable(id=str(uuid4()), name="priority", value="high"),- created_at=newer_time,- )-- result = ConversationService.get_conversational_variable(- app_model=app,- conversation_id=conversation.id,- user=account,- limit=10,- last_id=None,- session=db_session_with_containers,- )-- assert [item["id"] for item in result.data] == [first_variable.id, second_variable.id]- assert [item["name"] for item in result.data] == ["topic", "priority"]- assert result.limit == 10- assert result.has_more is False-- def test_get_conversational_variable_with_last_id(- self, db_session_with_containers: Session, real_conversation_service_session_factory- ):- del real_conversation_service_session_factory- factory = ConversationServiceVariableIntegrationFactory- app, account = factory.create_app_and_account(db_session_with_containers)- conversation = factory.create_conversation(db_session_with_containers, app, account)- base_time = datetime(2024, 1, 1, 9, 0, 0)-- first_variable = factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=StringVariable(id=str(uuid4()), name="topic", value="billing"),- created_at=base_time,- )- second_variable = factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=StringVariable(id=str(uuid4()), name="priority", value="high"),- created_at=base_time + timedelta(minutes=1),- )- third_variable = factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=StringVariable(id=str(uuid4()), name="owner", value="alice"),- created_at=base_time + timedelta(minutes=2),- )-- result = ConversationService.get_conversational_variable(- app_model=app,- conversation_id=conversation.id,- user=account,- limit=10,- last_id=first_variable.id,- session=db_session_with_containers,- )-- assert [item["id"] for item in result.data] == [second_variable.id, third_variable.id]- assert result.has_more is False-- def test_get_conversational_variable_last_id_not_found_raises_error(- self, db_session_with_containers: Session, real_conversation_service_session_factory- ):- del real_conversation_service_session_factory- factory = ConversationServiceVariableIntegrationFactory- app, account = factory.create_app_and_account(db_session_with_containers)- conversation = factory.create_conversation(db_session_with_containers, app, account)-- with pytest.raises(ConversationVariableNotExistsError):- ConversationService.get_conversational_variable(- app_model=app,- conversation_id=conversation.id,- user=account,- limit=10,- last_id=str(uuid4()),- session=db_session_with_containers,- )-- def test_get_conversational_variable_sets_has_more(- self, db_session_with_containers: Session, real_conversation_service_session_factory- ):- del real_conversation_service_session_factory- factory = ConversationServiceVariableIntegrationFactory- app, account = factory.create_app_and_account(db_session_with_containers)- conversation = factory.create_conversation(db_session_with_containers, app, account)-- for index in range(3):- factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=StringVariable(id=str(uuid4()), name=f"var_{index}", value=f"value_{index}"),- created_at=datetime(2024, 1, 1, 10, 0, index),- )-- result = ConversationService.get_conversational_variable(- app_model=app,- conversation_id=conversation.id,- user=account,- limit=2,- last_id=None,- session=db_session_with_containers,- )-- assert len(result.data) == 2- assert result.has_more is True-- def test_update_conversation_variable_success(- self, db_session_with_containers: Session, real_conversation_service_session_factory- ):- del real_conversation_service_session_factory- factory = ConversationServiceVariableIntegrationFactory- app, account = factory.create_app_and_account(db_session_with_containers)- conversation = factory.create_conversation(db_session_with_containers, app, account)- existing = factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=StringVariable(id=str(uuid4()), name="topic", value="billing"),- )- updated_at = datetime(2024, 1, 1, 15, 0, 0)-- with patch("services.conversation_service.naive_utc_now", return_value=updated_at):- result = ConversationService.update_conversation_variable(- app_model=app,- conversation_id=conversation.id,- variable_id=existing.id,- user=account,- new_value="support",- session=db_session_with_containers,- )-- db_session_with_containers.expire_all()- persisted = db_session_with_containers.get(ConversationVariable, (existing.id, conversation.id))-- assert persisted is not None- assert persisted.to_variable().value == "support"- assert result["id"] == existing.id- assert result["value"] == "support"- assert result["updated_at"] == updated_at-- def test_update_conversation_variable_not_found_raises_error(- self, db_session_with_containers: Session, real_conversation_service_session_factory- ):- del real_conversation_service_session_factory- factory = ConversationServiceVariableIntegrationFactory- app, account = factory.create_app_and_account(db_session_with_containers)- conversation = factory.create_conversation(db_session_with_containers, app, account)-- with pytest.raises(ConversationVariableNotExistsError):- ConversationService.update_conversation_variable(- app_model=app,- conversation_id=conversation.id,- variable_id=str(uuid4()),- user=account,- new_value="support",- session=db_session_with_containers,- )-- def test_update_conversation_variable_type_mismatch_raises_error(- self, db_session_with_containers: Session, real_conversation_service_session_factory- ):- del real_conversation_service_session_factory- factory = ConversationServiceVariableIntegrationFactory- app, account = factory.create_app_and_account(db_session_with_containers)- conversation = factory.create_conversation(db_session_with_containers, app, account)- existing = factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=FloatVariable(id=str(uuid4()), name="score", value=1.5),- )-- with pytest.raises(ConversationVariableTypeMismatchError, match="expects float"):- ConversationService.update_conversation_variable(- app_model=app,- conversation_id=conversation.id,- variable_id=existing.id,- user=account,- new_value="wrong-type",- session=db_session_with_containers,- )-- def test_update_conversation_variable_integer_number_compatibility(- self, db_session_with_containers: Session, real_conversation_service_session_factory- ):- del real_conversation_service_session_factory- factory = ConversationServiceVariableIntegrationFactory- app, account = factory.create_app_and_account(db_session_with_containers)- conversation = factory.create_conversation(db_session_with_containers, app, account)- existing = factory.create_variable(- db_session_with_containers,- app=app,- conversation=conversation,- variable=IntegerVariable(id=str(uuid4()), name="attempts", value=1),- )-- result = ConversationService.update_conversation_variable(- app_model=app,- conversation_id=conversation.id,- variable_id=existing.id,- user=account,- new_value=42,- session=db_session_with_containers,- )-- db_session_with_containers.expire_all()- persisted = db_session_with_containers.get(ConversationVariable, (existing.id, conversation.id))-- assert persisted is not None- assert persisted.to_variable().value == 42- assert result["value"] == 42diff --git a/api/tests/e2e_tests/contracts/controllers/service_api/app/test_conversation_variables.py b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_conversation_variables.pynew file mode 100644index 00000000000..bb510bc246a--- /dev/null+++ b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_conversation_variables.py@@ -0,0 +1,505 @@+"""Conversation-variable HTTP contracts through real admission, services, and SQLite."""++from collections.abc import Mapping+from dataclasses import dataclass+from datetime import datetime, timedelta+from http import HTTPStatus+from uuid import UUID, uuid4++import pytest+from pydantic import JsonValue+from sqlalchemy import event, select, update+from sqlalchemy.orm import Session, SessionTransaction+from werkzeug.test import TestResponse++from configs import dify_config+from constants.resource_access_token import ResourceAccessTokenResourceType+from controllers.service_api.app.conversation import ConversationVariableDetailApi, ConversationVariablesApi+from factories import variable_factory+from machinery.context import RequestContext+from models import Tenant, TenantStatus+from models.enums import ConversationFromSource+from models.model import App, AppMode, Conversation, EndUser+from models.resource_access_token import ResourceAccessToken+from models.workflow import ConversationVariable+from repositories.conversation_variable_repository import ConversationVariableRepository+from services.auth.resource_access_token_contracts import ResourceAccessTokenResource+from services.conversation_variable_command_service import ConversationVariableCommandService+from tests.e2e_tests.contracts.controllers.service_api.app import test_conversation_rename as rename_tests+from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Harness as RenameHarness+from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Services as RenameServices+from tests.helpers.unit.model_factories import make_app, make_conversation++rename_harness = rename_tests.harness+_CREATED_AT = datetime(2026, 1, 2, 3, 4, 5)+_UPDATED_AT = datetime(2026, 1, 3, 3, 4, 5)+++@dataclass(frozen=True)+class _Services(RenameServices):+ conversation_variables: ConversationVariableRepository+ conversation_variable_commands: ConversationVariableCommandService+++@dataclass(frozen=True)+class _Harness:+ source: RenameHarness+ row: ConversationVariable++ def request(+ self,+ method: str,+ *,+ body: Mapping[str, JsonValue] | None = None,+ query: Mapping[str, str | int] | None = None,+ conversation_id: str | None = None,+ variable_id: str | None = None,+ user: str | None = "alice",+ authorization: str | None = "Bearer rename-token",+ requested_app_id: str | None = None,+ ) -> TestResponse:+ headers = {"Authorization": authorization} if authorization is not None else {}+ if requested_app_id is not None:+ headers["X-Dify-App-ID"] = requested_app_id+ url = f"/conversations/{conversation_id or self.source.conversation.id}/variables"+ if method == "PUT":+ url += f"/{variable_id or self.row.id}"+ payload = dict(body) if body is not None else {"value": "Updated"}+ if user is not None:+ payload["user"] = user+ return self.source.app.test_client().put(url, json=payload, headers=headers)+ params = dict(query or {})+ if user is not None:+ params["user"] = user+ return self.source.app.test_client().get(url, query_string=params, headers=headers)++ def add_variable(+ self,+ name: str,+ *,+ value_type: str = "string",+ value: JsonValue = "Original",+ app_id: str | None = None,+ conversation_id: str | None = None,+ offset: int = 0,+ ) -> ConversationVariable:+ row = _variable(+ self.source,+ name=name,+ value_type=value_type,+ value=value,+ app_id=app_id,+ conversation_id=conversation_id,+ offset=offset,+ )+ with self.source.factory.begin() as session:+ session.add(row)+ return row++ def assert_unchanged(self) -> None:+ with self.source.factory() as session:+ row = session.get(ConversationVariable, (self.row.id, self.row.conversation_id))+ assert row is not None+ assert row.data == self.row.data+ assert row.updated_at == self.row.updated_at+++def _variable(+ source: RenameHarness,+ *,+ name: str,+ value_type: str,+ value: JsonValue,+ app_id: str | None = None,+ conversation_id: str | None = None,+ offset: int = 0,+) -> ConversationVariable:+ variable = variable_factory.build_conversation_variable_from_mapping(+ {+ "id": f"draft-{name}",+ "name": name,+ "description": "Saved variable description",+ "value_type": value_type,+ "value": value,+ }+ )+ row = ConversationVariable.from_variable(+ app_id=app_id or source.target.id,+ conversation_id=conversation_id or source.conversation.id,+ variable=variable,+ )+ row.created_at = _CREATED_AT + timedelta(seconds=offset)+ row.updated_at = _UPDATED_AT+ return row+++def harness(rename_harness: RenameHarness) -> _Harness:+ original = rename_harness.app.extensions["application_services"]+ assert isinstance(original, RenameServices)+ repository = ConversationVariableRepository(session_factory=rename_harness.repository_factory)+ rename_harness.app.extensions["application_services"] = _Services(+ app_definitions=original.app_definitions,+ app_scoped_end_users=original.app_scoped_end_users,+ conversation_names=original.conversation_names,+ resource_access_tokens=original.resource_access_tokens,+ conversation_variables=repository,+ conversation_variable_commands=ConversationVariableCommandService(variables=repository),+ )+ rename_harness.api.add_resource(ConversationVariablesApi, "/conversations/<uuid:conversation_id>/variables")+ rename_harness.api.add_resource(+ ConversationVariableDetailApi, "/conversations/<uuid:conversation_id>/variables/<uuid:variable_id>"+ )+ row = _variable(rename_harness, name="greeting", value_type="string", value="Original")+ with rename_harness.factory.begin() as session:+ session.add(row)+ return _Harness(rename_harness, row)++[email protected]("method", ["GET", "PUT"])[email protected]("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT])+def test_supported_modes_preserve_wire_contract_and_close_admission_before_repository(+ harness: _Harness, method: str, mode: AppMode+) -> None:+ source = harness.source+ with source.factory.begin() as session:+ session.execute(update(App).where(App.id == source.target.id).values(mode=mode))+ response = harness.request(method)+ assert response.status_code == HTTPStatus.OK+ assert response.headers["Content-Type"] == "application/json"+ payload = response.get_json()+ value = payload if method == "PUT" else payload["data"][0]+ assert value == {+ "id": harness.row.id,+ "name": "greeting",+ "value_type": "string",+ "value": "Updated" if method == "PUT" else "Original",+ "description": "Saved variable description",+ "created_at": int(_CREATED_AT.timestamp()),+ "updated_at": value["updated_at"] if method == "PUT" else int(_UPDATED_AT.timestamp()),+ }+ assert str(UUID(value["id"])) == value["id"]+ with source.factory() as session:+ stored = session.get(ConversationVariable, (harness.row.id, source.conversation.id))+ assert stored is not None+ variable = stored.to_variable()+ assert variable.id == "draft-greeting"+ assert variable.value == value["value"]+ assert variable.selector == ["conversation", "greeting"]+ if method == "PUT":+ assert value["updated_at"] > int(_UPDATED_AT.timestamp())+ assert value["updated_at"] == int(stored.updated_at.timestamp())+ assert source.admissions+ assert source.provisions+ assert source.queries+ assert source.scoped_sessions == []+ source.assert_closed()+++ ("value_type", "old_value", "new_value", "exposed_type", "wire_value"),+ [+ ("string", "before", "", "string", ""),+ ("integer", 1, 0, "number", "0"),+ ("float", 1.5, 0.5, "number", "0.5"),+ ("boolean", True, False, "boolean", "False"),+ ("object", {"before": True}, {}, "object", "{}"),+ ("array[string]", ["before"], [], "array[string]", "[]"),+ ("array[number]", [1], [2, 3.5], "array[number]", "[2, 3.5]"),+ ("array[object]", [{"before": True}], [{"value": None}], "array[object]", "[{'value': None}]"),+ ("array[boolean]", [True], [False], "array[boolean]", "[False]"),+ ],+)+def test_update_retains_stringified_json_values_and_variable_metadata(+ harness: _Harness,+ value_type: str,+ old_value: JsonValue,+ new_value: JsonValue,+ exposed_type: str,+ wire_value: str,+) -> None:+ row = harness.add_variable("boundary", value_type=value_type, value=old_value)+ response = harness.request("PUT", variable_id=row.id, body={"value": new_value})+ assert response.status_code == HTTPStatus.OK+ assert response.get_json()["value_type"] == exposed_type+ assert response.get_json()["value"] == wire_value+ with harness.source.factory() as session:+ stored = session.get(ConversationVariable, (row.id, row.conversation_id))+ assert stored is not None+ variable = stored.to_variable()+ assert variable.value == new_value+ assert variable.id == "draft-boundary"+ assert variable.description == "Saved variable description"+ assert variable.selector == ["conversation", "boundary"]+ listed = harness.request("GET", query={"variable_name": "boundary"})+ assert listed.status_code == HTTPStatus.OK+ assert listed.get_json()["data"] == [response.get_json()]+ harness.source.assert_closed()+++def test_pagination_and_case_insensitive_filter_keep_cursor_inside_filtered_list(harness: _Harness) -> None:+ first = harness.add_variable("Preference_first", offset=1)+ second = harness.add_variable("preference_second", offset=2)+ harness.add_variable("unrelated", offset=3)+ first_response = harness.request("GET", query={"variable_name": "PREFERENCE", "limit": 1})+ assert first_response.status_code == HTTPStatus.OK+ assert first_response.get_json()["has_more"] is True+ assert [row["id"] for row in first_response.get_json()["data"]] == [first.id]+ second_response = harness.request("GET", query={"variable_name": "PREFERENCE", "limit": 1, "last_id": first.id})+ assert second_response.get_json()["has_more"] is False+ assert [row["id"] for row in second_response.get_json()["data"]] == [second.id]+ bad_cursor = harness.request("GET", query={"variable_name": "PREFERENCE", "last_id": harness.row.id})+ assert bad_cursor.status_code == HTTPStatus.NOT_FOUND+ assert bad_cursor.get_json()["code"] == "conversation_variable_not_found"+ harness.source.assert_closed()+++def test_variable_name_underscore_is_literal_and_empty_cursor_means_first_page(harness: _Harness) -> None:+ matching = harness.add_variable("pref_name")+ harness.add_variable("prefXname")+ response = harness.request("GET", query={"variable_name": "PREF_", "last_id": ""})+ assert response.status_code == HTTPStatus.OK+ assert [item["id"] for item in response.get_json()["data"]] == [matching.id]+ harness.source.assert_closed()++[email protected]("cursor", [False, True], ids=["no-cursor", "with-cursor"])+def test_other_conversation_and_app_variables_do_not_leak(harness: _Harness, cursor: bool) -> None:+ other_conversation = make_conversation(+ conversation_id=str(uuid4()),+ app_id=harness.source.target.id,+ name="Other",+ inputs={},+ from_source=ConversationFromSource.API,+ from_end_user_id=harness.source.end_user.id,+ )+ with harness.source.factory.begin() as session:+ session.add(other_conversation)+ foreign = harness.add_variable("foreign", conversation_id=other_conversation.id)+ harness.add_variable("wrong_app", app_id=str(uuid4()))+ response = harness.request("GET", query={"last_id": foreign.id} if cursor else {})+ if cursor:+ assert response.status_code == HTTPStatus.NOT_FOUND+ assert response.get_json()["code"] == "conversation_variable_not_found"+ else:+ assert response.status_code == HTTPStatus.OK+ assert [row["id"] for row in response.get_json()["data"]] == [harness.row.id]+ denied_update = harness.request("PUT", variable_id=foreign.id)+ assert denied_update.status_code == HTTPStatus.NOT_FOUND+ assert denied_update.get_json()["code"] == "conversation_variable_not_found"+ harness.assert_unchanged()+ harness.source.assert_closed()++[email protected]("method", ["GET", "PUT"])[email protected]("field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted"])+def test_conversation_ownership_is_required_for_read_and_write(harness: _Harness, method: str, field: str) -> None:+ value: str | bool+ if field == "is_deleted":+ value = True+ elif field == "from_source":+ value = ConversationFromSource.CONSOLE+ else:+ value = str(uuid4())+ with harness.source.factory.begin() as session:+ session.execute(+ update(Conversation).where(Conversation.id == harness.source.conversation.id).values({field: value})+ )+ response = harness.request(method)+ assert response.status_code == HTTPStatus.NOT_FOUND+ assert response.get_json() == {+ "code": "conversation_not_found",+ "message": "The conversation was not found for this user and app.",+ "status": HTTPStatus.NOT_FOUND,+ }+ harness.assert_unchanged()+ harness.source.assert_closed()++[email protected]("method", ["GET", "PUT"])[email protected]("user", [None, "", "another-user"])+def test_missing_or_other_end_user_cannot_access_variables(harness: _Harness, method: str, user: str | None) -> None:+ response = harness.request(method, user=user)+ assert response.status_code == HTTPStatus.NOT_FOUND+ assert response.get_json()["code"] == "conversation_not_found"+ harness.assert_unchanged()+ harness.source.assert_closed()++[email protected]("method", ["GET", "PUT"])[email protected]("change_app_too", [False, True], ids=["end-user-workspace", "app-workspace"])+def test_workspace_change_after_admission_blocks_variable_access(+ harness: _Harness, method: str, change_app_too: bool+) -> None:+ source = harness.source++ def change_owner(_session: Session, _transaction: SessionTransaction) -> None:+ with source.factory.begin() as session:+ end_user = session.get(EndUser, source.end_user.id)+ assert end_user is not None+ end_user.tenant_id = str(uuid4())+ if change_app_too:+ target = session.get(App, source.target.id)+ assert target is not None+ target.tenant_id = end_user.tenant_id++ event.listen(source.provision_factory, "after_transaction_end", change_owner, once=True)+ try:+ response = harness.request(method)+ finally:+ event.remove(source.provision_factory, "after_transaction_end", change_owner)+ assert response.status_code == HTTPStatus.NOT_FOUND+ assert response.get_json()["code"] == "conversation_not_found"+ harness.assert_unchanged()+ source.assert_closed()++[email protected]("method", ["GET", "PUT"])[email protected]("authorization", [None, "Basic rename-token", "Bearer invalid-token"])+def test_invalid_credentials_stop_before_database_use_case(+ harness: _Harness, method: str, authorization: str | None+) -> None:+ response = harness.request(method, authorization=authorization)+ assert response.status_code == HTTPStatus.UNAUTHORIZED+ assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'+ assert response.get_json()["code"] == "unauthorized"+ source = harness.source+ assert source.admissions == source.provisions == source.queries == []+ harness.assert_unchanged()+ source.assert_closed()++[email protected]("method", ["GET", "PUT"])[email protected]("mode", [AppMode.COMPLETION, AppMode.WORKFLOW, AppMode.AGENT])+def test_unsupported_modes_fail_before_repository(harness: _Harness, method: str, mode: AppMode) -> None:+ with harness.source.factory.begin() as session:+ session.execute(update(App).where(App.id == harness.source.target.id).values(mode=mode))+ response = harness.request(method)+ assert response.status_code == HTTPStatus.BAD_REQUEST+ assert response.get_json()["code"] == "not_chat_app"+ assert harness.source.queries == []+ harness.assert_unchanged()+ harness.source.assert_closed()+++ "query",+ [+ {"limit": "0"},+ {"limit": "101"},+ {"last_id": "invalid"},+ {"variable_name": ""},+ {"variable_name": "a" * 256},+ {"variable_name": "foo'; DROP TABLE app;"},+ ],+)+def test_invalid_query_returns_422_before_repository(harness: _Harness, query: dict[str, str]) -> None:+ response = harness.request("GET", query=query)+ assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY+ assert response.get_json()["code"] == "unprocessable_entity"+ assert harness.source.queries == []+ harness.source.assert_closed()+++def test_missing_value_returns_422_without_write(harness: _Harness) -> None:+ response = harness.request("PUT", body={})+ assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY+ assert response.get_json()["code"] == "unprocessable_entity"+ assert harness.source.queries == []+ harness.assert_unchanged()+ harness.source.assert_closed()++[email protected]("new_value", [0, False, [], {}, None])+def test_type_mismatch_returns_specific_error_and_rolls_back(harness: _Harness, new_value: JsonValue) -> None:+ response = harness.request("PUT", body={"value": new_value})+ assert response.status_code == HTTPStatus.BAD_REQUEST+ error = response.get_json()+ assert error["code"] == "conversation_variable_type_mismatch"+ assert "variable 'greeting' expects string" in error["message"]+ assert "but got" in error["message"]+ assert error["status"] == HTTPStatus.BAD_REQUEST+ harness.assert_unchanged()+ harness.source.assert_closed()++[email protected]("method", ["GET", "PUT"])+def test_missing_variable_or_cursor_returns_specific_404(harness: _Harness, method: str) -> None:+ variable_id = str(uuid4())+ response = harness.request(method, query={"last_id": variable_id}, variable_id=variable_id)+ assert response.status_code == HTTPStatus.NOT_FOUND+ assert response.get_json() == {+ "code": "conversation_variable_not_found",+ "message": "The conversation variable was not found for this conversation.",+ "status": HTTPStatus.NOT_FOUND,+ }+ harness.assert_unchanged()+ harness.source.assert_closed()++[email protected]("method", ["GET", "PUT"])[email protected]("multiple_apps", [False, True], ids=["single-app", "multiple-apps"])+def test_resource_token_reuses_bound_end_user_and_records_usage(+ harness: _Harness, method: str, multiple_apps: bool+) -> None:+ source = harness.source+ app_ids = (source.target.id,)+ if multiple_apps:+ other = make_app(app_id=str(uuid4()), tenant_id=source.tenant.id)+ with source.factory.begin() as session:+ session.add(other)+ app_ids = (other.id, source.target.id)+ services = source.app.extensions["application_services"]+ assert isinstance(services, _Services)+ token = services.resource_access_tokens.create(+ RequestContext(+ request_id="variable-resource-token",+ trace_id=None,+ account_id=str(uuid4()),+ active_workspace_id=source.tenant.id,+ ),+ name="Variable app token",+ resources=tuple(ResourceAccessTokenResource(ResourceAccessTokenResourceType.APP, app_id) for app_id in app_ids),+ )+ response = harness.request(+ method,+ authorization=f"Bearer {token.token}",+ requested_app_id=source.target.id if multiple_apps else None,+ )+ assert response.status_code == HTTPStatus.OK+ source.assert_closed()+ with source.factory() as session:+ stored_token = session.get(ResourceAccessToken, token.token_id)+ assert stored_token is not None+ assert stored_token.last_used_at is not None+ assert session.scalars(select(EndUser.id)).all() == [source.end_user.id]++[email protected]("method", ["GET", "PUT"])+ ("failure", "code"), [("disabled_api", "app_api_disabled"), ("archived_workspace", "workspace_archived")]+)+def test_disabled_app_or_workspace_prevents_provisioning(+ harness: _Harness, method: str, failure: str, code: str+) -> None:+ source = harness.source+ with source.factory.begin() as session:+ if failure == "disabled_api":+ session.execute(update(App).where(App.id == source.target.id).values(enable_api=False))+ else:+ session.execute(update(Tenant).where(Tenant.id == source.tenant.id).values(status=TenantStatus.ARCHIVE))+ response = harness.request(method, user="new-user")+ assert response.status_code == HTTPStatus.FORBIDDEN+ assert response.get_json()["code"] == code+ assert source.provisions == source.queries == []+ harness.assert_unchanged()+ source.assert_closed()+++def test_factory_size_rejection_preserves_error_and_rolls_back(harness: _Harness) -> None:+ response = harness.request("PUT", body={"value": "x" * (dify_config.MAX_VARIABLE_SIZE + 1)})+ assert response.status_code == HTTPStatus.BAD_REQUEST+ assert response.get_json()["code"] == "invalid_param"+ assert f"exceeds limit {dify_config.MAX_VARIABLE_SIZE}" in response.get_json()["message"]+ harness.assert_unchanged()+ harness.source.assert_closed()diff --git a/api/tests/unit_tests/controllers/service_api/app/test_conversation.py b/api/tests/unit_tests/controllers/service_api/app/test_conversation.pyindex 71ac5d074b9..b944792319b 100644--- a/api/tests/unit_tests/controllers/service_api/app/test_conversation.py+++ b/api/tests/unit_tests/controllers/service_api/app/test_conversation.py@@ -1,40 +1,27 @@import uuidfrom datetime import UTC, datetime-from inspect import unwrapfrom types import SimpleNamespaceimport pytest-from flask import Flask, requestfrom pydantic import ValidationError-from werkzeug.exceptions import BadRequest, NotFoundimport servicesfrom controllers.console.explore.conversation import ConversationRenamePayload as ConsolePayloadfrom controllers.service_api.app.conversation import (ConversationListQuery,ConversationRenamePayload,- ConversationVariableDetailApi,ConversationVariableInfiniteScrollPaginationResponse,ConversationVariableResponse,- ConversationVariablesApi,ConversationVariablesQuery,ConversationVariableUpdatePayload,)from controllers.service_api.app.conversation import ConversationRenamePayload as ServicePayloadfrom controllers.service_api.app.error import NotChatAppError-from core.app.entities.app_invoke_entities import InvokeFromfrom fields._value_type_serializer import serialize_value_typefrom graphon.variables import StringSegmentfrom graphon.variables.types import SegmentType-from models.enums import ConversationFromSource, EndUserType-from models.model import App, AppMode, Conversation, EndUser+from models.model import App, AppModefrom services.conversation_service import ConversationService-from services.errors.conversation import (- ConversationNotExistsError,- ConversationVariableNotExistsError,- ConversationVariableTypeMismatchError,-)-from tests.helpers.unit.model_factories import make_conversation"""Unit tests for Service API Conversation controllers.@@ -52,48 +39,6 @@ Focus on:"""-def _app(*, app_id: str = "app-1", mode: AppMode = AppMode.CHAT) -> App:- return App(- id=app_id,- tenant_id="tenant-1",- name="Service API app",- description="",- mode=mode,- enable_site=True,- enable_api=True,- max_active_requests=0,- )---def _end_user(user_id: str = "end-user-1", app_id: str = "app-1") -> EndUser:- return EndUser(- id=user_id,- tenant_id="tenant-1",- app_id=app_id,- type=EndUserType.SERVICE_API,- external_user_id="external-user-1",- name="Service API user",- session_id="session-1",- )---def _conversation(- *,- conversation_id: str,- app_id: str = "app-1",- end_user_id: str = "end-user-1",-) -> Conversation:- return make_conversation(- conversation_id=conversation_id,- app_id=app_id,- name="Original Name",- inputs={},- from_source=ConversationFromSource.API,- from_end_user_id=end_user_id,- invoke_from=InvokeFrom.SERVICE_API,- )--class TestConversationCases:class TestConversationListQuery:"""Test suite for ConversationListQuery Pydantic model."""@@ -489,16 +434,6 @@ class TestConversationCases:assert hasattr(ConversationService, "delete")assert callable(ConversationService.delete)- def test_get_conversational_variable_method_exists(self):- """Test that ConversationService.get_conversational_variable exists."""- assert hasattr(ConversationService, "get_conversational_variable")- assert callable(ConversationService.get_conversational_variable)-- def test_update_conversation_variable_method_exists(self):- """Test that ConversationService.update_conversation_variable exists."""- assert hasattr(ConversationService, "update_conversation_variable")- assert callable(ConversationService.update_conversation_variable)-class TestConversationPayloadsController:def test_rename_requires_name(self) -> None:with pytest.raises(@@ -514,189 +449,6 @@ class TestConversationCases:with pytest.raises(ValueError, match="1 validation error for ConversationVariablesQuery\\\nvariable_name"):ConversationVariablesQuery(variable_name="bad;")- class TestConversationVariablesApiController:- def test_not_chat(self, app: Flask) -> None:- api = ConversationVariablesApi()- handler = unwrap(api.get)- app_model = _app(mode=AppMode.COMPLETION)- end_user = _end_user()-- with app.test_request_context("/conversations/1/variables", method="GET"):- with pytest.raises(NotChatAppError):- handler(- api,- ConversationVariablesQuery.model_validate(request.args.to_dict(flat=True)),- app_model=app_model,- end_user=end_user,- conversation_id="00000000-0000-0000-0000-000000000001",- )-- def test_not_found(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(- ConversationService,- "get_conversational_variable",- lambda *_args, **_kwargs: (_ for _ in ()).throw(ConversationNotExistsError()),- )-- api = ConversationVariablesApi()- handler = unwrap(api.get)- app_model = _app()- end_user = _end_user()-- with app.test_request_context(- "/conversations/1/variables?limit=20",- method="GET",- ):- with pytest.raises(NotFound):- handler(- api,- ConversationVariablesQuery.model_validate(request.args.to_dict(flat=True)),- app_model=app_model,- end_user=end_user,- conversation_id="00000000-0000-0000-0000-000000000001",- )-- def test_success_serializes_response(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- created_at = datetime(2026, 1, 2, 3, 4, 5, tzinfo=UTC)- monkeypatch.setattr(- ConversationService,- "get_conversational_variable",- lambda *_args, **_kwargs: SimpleNamespace(- limit=1,- has_more=False,- data=[- {- "id": "550e8400-e29b-41d4-a716-446655440000",- "name": "foo",- "value_type": SegmentType.INTEGER,- "value": 1,- "created_at": created_at,- "updated_at": created_at,- }- ],- ),- )-- api = ConversationVariablesApi()- handler = unwrap(api.get)- app_model = _app()- end_user = _end_user()-- with app.test_request_context(- "/conversations/1/variables?limit=20",- method="GET",- ):- result = handler(- api,- ConversationVariablesQuery.model_validate(request.args.to_dict(flat=True)),- app_model=app_model,- end_user=end_user,- conversation_id="00000000-0000-0000-0000-000000000001",- )-- assert result["limit"] == 1- assert result["has_more"] is False- assert result["data"][0]["value_type"] == "number"- assert result["data"][0]["value"] == "1"- assert result["data"][0]["created_at"] == int(created_at.timestamp())-- class TestConversationVariableDetailApiController:- def test_update_type_mismatch(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(- ConversationService,- "update_conversation_variable",- lambda *_args, **_kwargs: (_ for _ in ()).throw(ConversationVariableTypeMismatchError("bad")),- )-- api = ConversationVariableDetailApi()- handler = unwrap(api.put)- app_model = _app()- end_user = _end_user()-- with app.test_request_context(- "/conversations/1/variables/2",- method="PUT",- json={"value": "x"},- ):- payload = ConversationVariableUpdatePayload.model_validate(request.get_json() or {})- with pytest.raises(BadRequest):- handler(- api,- payload,- app_model=app_model,- end_user=end_user,- conversation_id="00000000-0000-0000-0000-000000000001",- variable_id="00000000-0000-0000-0000-000000000002",- )-- def test_update_not_found(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(- ConversationService,- "update_conversation_variable",- lambda *_args, **_kwargs: (_ for _ in ()).throw(ConversationVariableNotExistsError()),- )-- api = ConversationVariableDetailApi()- handler = unwrap(api.put)- app_model = _app()- end_user = _end_user()-- with app.test_request_context(- "/conversations/1/variables/2",- method="PUT",- json={"value": "x"},- ):- payload = ConversationVariableUpdatePayload.model_validate(request.get_json() or {})- with pytest.raises(NotFound):- handler(- api,- payload,- app_model=app_model,- end_user=end_user,- conversation_id="00000000-0000-0000-0000-000000000001",- variable_id="00000000-0000-0000-0000-000000000002",- )-- def test_update_success_serializes_response(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- created_at = datetime(2026, 1, 2, 3, 4, 5, tzinfo=UTC)- monkeypatch.setattr(- ConversationService,- "update_conversation_variable",- lambda *_args, **_kwargs: {- "id": "550e8400-e29b-41d4-a716-446655440000",- "name": "foo",- "value_type": SegmentType.INTEGER,- "value": 1,- "created_at": created_at,- "updated_at": created_at,- },- )-- api = ConversationVariableDetailApi()- handler = unwrap(api.put)- app_model = _app()- end_user = _end_user()-- with app.test_request_context(- "/conversations/1/variables/2",- method="PUT",- json={"value": 1},- ):- payload = ConversationVariableUpdatePayload.model_validate(request.get_json() or {})- result = handler(- api,- payload,- app_model=app_model,- end_user=end_user,- conversation_id="00000000-0000-0000-0000-000000000001",- variable_id="00000000-0000-0000-0000-000000000002",- )-- assert result["id"] == "550e8400-e29b-41d4-a716-446655440000"- assert result["value_type"] == "number"- assert result["value"] == "1"- assert result["created_at"] == int(created_at.timestamp())-class TestConversationRenamePayloadCases:from tests.unit_tests.conftest import appdiff --git a/api/tests/unit_tests/repositories/test_conversation_variable_query_repository.py b/api/tests/unit_tests/repositories/test_conversation_variable_query_repository.pydeleted file mode 100644index d870dca9af9..00000000000--- a/api/tests/unit_tests/repositories/test_conversation_variable_query_repository.py+++ /dev/null@@ -1,107 +0,0 @@-"""Persistence scope and session lifetime of the Console variable query."""--from json import JSONDecodeError--import pytest-from sqlalchemy import Engine, event, select-from sqlalchemy.orm import Session, sessionmaker--from factories import variable_factory-from machinery.context import RequestContext-from models import App, AppMode, ConversationVariable-from repositories.conversation_variable_query_repository import ConversationVariableQueryRepository-from services.conversation_variable_query import ConversationVariableAppNotFoundError-from tests.helpers.unit.model_factories import make_app--CONTEXT = RequestContext(request_id="request", trace_id=None, account_id="account-1", active_workspace_id="tenant-1")---def repository(sqlite_session_factory: sessionmaker[Session]) -> ConversationVariableQueryRepository:- variable = variable_factory.build_conversation_variable_from_mapping(- {"id": "draft-variable", "name": "example", "value_type": "string", "value": "value"}- )- with sqlite_session_factory.begin() as session:- session.add(make_app(mode=AppMode.ADVANCED_CHAT))- session.add(- ConversationVariable.from_variable(app_id="app-1", conversation_id="conversation-1", variable=variable)- )- return ConversationVariableQueryRepository(session_factory=sqlite_session_factory)--- ("workspace_id", "app_id", "conversation_id", "expected_count"),- [- ("tenant-1", "app-1", "conversation-1", 1),- ("tenant-2", "app-1", "conversation-1", 0),- ("tenant-1", "app-2", "conversation-1", 0),- ("tenant-1", "app-1", "conversation-2", 0),- ],-)-def test_read_applies_owner_scope_without_relying_on_prior_admission(- repository: ConversationVariableQueryRepository,- workspace_id: str,- app_id: str,- conversation_id: str,- expected_count: int,-) -> None:- rows = repository.list_variables(CONTEXT._replace(active_workspace_id=workspace_id), app_id, conversation_id)- assert len(rows) == expected_count---def test_app_transfer_after_admission_does_not_expose_variables(- repository: ConversationVariableQueryRepository, sqlite_session_factory: sessionmaker[Session]-) -> None:- repository.require_app(CONTEXT, "app-1")- with sqlite_session_factory.begin() as session:- app = session.get_one(App, "app-1")- app.tenant_id = "tenant-2"-- assert repository.list_variables(CONTEXT, "app-1", "conversation-1") == []- with pytest.raises(ConversationVariableAppNotFoundError):- repository.require_app(CONTEXT, "app-1")- new_context = CONTEXT._replace(active_workspace_id="tenant-2")- repository.require_app(new_context, "app-1")- assert len(repository.list_variables(new_context, "app-1", "conversation-1")) == 1--[email protected]("corrupt", [False, True])-def test_read_releases_connection_even_if_variable_decoding_fails(- repository: ConversationVariableQueryRepository,- sqlite_engine: Engine,- sqlite_session_factory: sessionmaker[Session],- corrupt: bool,-) -> None:- if corrupt:- with sqlite_session_factory.begin() as session:- row = session.scalars(select(ConversationVariable)).one()- row.data = "not json"- active_connections = 0- checkouts = 0-- def checkout(*_args: object) -> None:- nonlocal active_connections, checkouts- active_connections += 1- checkouts += 1-- def checkin(*_args: object) -> None:- nonlocal active_connections- active_connections -= 1-- event.listen(sqlite_engine, "checkout", checkout)- event.listen(sqlite_engine, "checkin", checkin)- try:- repository.require_app(CONTEXT, "app-1")- assert active_connections == 0- if corrupt:- with pytest.raises(JSONDecodeError):- repository.list_variables(CONTEXT, "app-1", "conversation-1")- else:- rows = repository.list_variables(CONTEXT, "app-1", "conversation-1")- assert rows[0].variable.value == "value"- assert rows[0].variable.model_dump()["name"] == "example"- assert checkouts == 2- assert active_connections == 0- finally:- event.remove(sqlite_engine, "checkout", checkout)- event.remove(sqlite_engine, "checkin", checkin)diff --git a/api/tests/unit_tests/repositories/test_conversation_variable_repository.py b/api/tests/unit_tests/repositories/test_conversation_variable_repository.pynew file mode 100644index 00000000000..770fab9c527--- /dev/null+++ b/api/tests/unit_tests/repositories/test_conversation_variable_repository.py@@ -0,0 +1,433 @@+"""Persistence scope and session lifetime of the Console variable query."""++from dataclasses import replace+from datetime import datetime, timedelta+from json import JSONDecodeError+from uuid import UUID++import pytest+from sqlalchemy import Engine, event, inspect, select+from sqlalchemy.dialects import mysql, postgresql+from sqlalchemy.orm import Session, sessionmaker++from factories import variable_factory+from graphon.variables import VariableBase+from machinery.context import RequestContext+from models import App, AppMode, ConversationVariable+from models.enums import ConversationFromSource+from models.model import Conversation, EndUser+from repositories.conversation_variable_repository import ConversationVariableRepository+from services.conversation_variable_query import ConversationVariableAppNotFoundError+from services.entities.conversation_entities import EndUserConversationRef+from services.errors.conversation import ConversationNotExistsError, ConversationVariableNotExistsError+from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user++CONTEXT = RequestContext(request_id="request", trace_id=None, account_id="account-1", active_workspace_id="tenant-1")+++def repository(sqlite_session_factory: sessionmaker[Session]) -> ConversationVariableRepository:+ variable = variable_factory.build_conversation_variable_from_mapping(+ {"id": "draft-variable", "name": "example", "value_type": "string", "value": "value"}+ )+ with sqlite_session_factory.begin() as session:+ session.add(make_app(mode=AppMode.ADVANCED_CHAT))+ session.add(+ ConversationVariable.from_variable(app_id="app-1", conversation_id="conversation-1", variable=variable)+ )+ return ConversationVariableRepository(session_factory=sqlite_session_factory)+++ ("workspace_id", "app_id", "conversation_id", "expected_count"),+ [+ ("tenant-1", "app-1", "conversation-1", 1),+ ("tenant-2", "app-1", "conversation-1", 0),+ ("tenant-1", "app-2", "conversation-1", 0),+ ("tenant-1", "app-1", "conversation-2", 0),+ ],+)+def test_read_applies_owner_scope_without_relying_on_prior_admission(+ repository: ConversationVariableRepository,+ workspace_id: str,+ app_id: str,+ conversation_id: str,+ expected_count: int,+) -> None:+ rows = repository.list_variables(CONTEXT._replace(active_workspace_id=workspace_id), app_id, conversation_id)+ assert len(rows) == expected_count+++def test_app_transfer_after_admission_does_not_expose_variables(+ repository: ConversationVariableRepository, sqlite_session_factory: sessionmaker[Session]+) -> None:+ repository.require_app(CONTEXT, "app-1")+ with sqlite_session_factory.begin() as session:+ app = session.get_one(App, "app-1")+ app.tenant_id = "tenant-2"++ assert repository.list_variables(CONTEXT, "app-1", "conversation-1") == []+ with pytest.raises(ConversationVariableAppNotFoundError):+ repository.require_app(CONTEXT, "app-1")+ new_context = CONTEXT._replace(active_workspace_id="tenant-2")+ repository.require_app(new_context, "app-1")+ assert len(repository.list_variables(new_context, "app-1", "conversation-1")) == 1++[email protected]("corrupt", [False, True])+def test_read_releases_connection_even_if_variable_decoding_fails(+ repository: ConversationVariableRepository,+ sqlite_engine: Engine,+ sqlite_session_factory: sessionmaker[Session],+ corrupt: bool,+) -> None:+ if corrupt:+ with sqlite_session_factory.begin() as session:+ row = session.scalars(select(ConversationVariable)).one()+ row.data = "not json"+ active_connections = 0+ checkouts = 0++ def checkout(*_args: object) -> None:+ nonlocal active_connections, checkouts+ active_connections += 1+ checkouts += 1++ def checkin(*_args: object) -> None:+ nonlocal active_connections+ active_connections -= 1++ event.listen(sqlite_engine, "checkout", checkout)+ event.listen(sqlite_engine, "checkin", checkin)+ try:+ repository.require_app(CONTEXT, "app-1")+ assert active_connections == 0+ if corrupt:+ with pytest.raises(JSONDecodeError):+ repository.list_variables(CONTEXT, "app-1", "conversation-1")+ else:+ rows = repository.list_variables(CONTEXT, "app-1", "conversation-1")+ assert rows[0].variable.value == "value"+ assert rows[0].variable.model_dump()["name"] == "example"+ assert checkouts == 2+ assert active_connections == 0+ finally:+ event.remove(sqlite_engine, "checkout", checkout)+ event.remove(sqlite_engine, "checkin", checkin)+++_TIME = datetime(2026, 10, 12, 12, 0)+++def end_user_ref(sqlite_session_factory: sessionmaker[Session]) -> EndUserConversationRef:+ ref = EndUserConversationRef(+ app_id="app-1", tenant_id="tenant-1", end_user_id="end-user-1", conversation_id="conversation-1"+ )+ with sqlite_session_factory.begin() as session:+ session.add(make_app(mode=AppMode.ADVANCED_CHAT))+ session.add(make_end_user(app_id=ref.app_id))+ session.add(+ make_conversation(+ mode=AppMode.ADVANCED_CHAT,+ inputs={},+ from_source=ConversationFromSource.API,+ from_end_user_id=ref.end_user_id,+ is_deleted=False,+ )+ )+ return ref+++def _variable(+ session: Session,+ ref: EndUserConversationRef,+ *,+ logical_id: str,+ name: str,+ created_at: datetime,+) -> ConversationVariable:+ variable = variable_factory.build_conversation_variable_from_mapping(+ {"id": logical_id, "name": name, "value_type": "string", "value": "original", "description": "Keep me"}+ )+ row = ConversationVariable.from_variable(app_id=ref.app_id, conversation_id=ref.conversation_id, variable=variable)+ row.created_at = created_at+ row.updated_at = created_at+ session.add(row)+ session.flush()+ return row+++def _replacement(variable: VariableBase) -> VariableBase:+ return variable_factory.build_conversation_variable_from_mapping({**variable.model_dump(), "value": "updated"})+++def test_end_user_page_preserves_storage_ids_order_limit_and_cursor(+ sqlite_session_factory: sessionmaker[Session], end_user_ref: EndUserConversationRef+) -> None:+ with sqlite_session_factory.begin() as session:+ newest = _variable(+ session, end_user_ref, logical_id="draft-new", name="New", created_at=_TIME + timedelta(seconds=2)+ )+ oldest = _variable(session, end_user_ref, logical_id="draft-old", name="Old", created_at=_TIME)+ middle = _variable(+ session, end_user_ref, logical_id="draft-mid", name="Middle", created_at=_TIME + timedelta(seconds=1)+ )+ repository = ConversationVariableRepository(session_factory=sqlite_session_factory)+ page = repository.get_page(ref=end_user_ref, last_id=None, limit=2, variable_name=None)+ assert [row.id for row in page.data] == [oldest.id, middle.id]+ assert page.data[0].variable.id == "draft-old"+ assert str(UUID(page.data[0].id)) == oldest.id+ assert page.limit == 2+ assert page.has_more is True+ assert page.data[0].created_at == page.data[0].updated_at == _TIME+ assert inspect(page.data[0], raiseerr=False) is None+ last = repository.get_page(ref=end_user_ref, last_id=middle.id, limit=2, variable_name=None)+ assert [row.id for row in last.data] == [newest.id]+ assert last.has_more is False+ empty = repository.get_page(ref=end_user_ref, last_id=newest.id, limit=2, variable_name=None)+ assert empty.data == ()+ assert empty.has_more is False+ with pytest.raises(ConversationVariableNotExistsError):+ repository.get_page(ref=end_user_ref, last_id="draft-mid", limit=2, variable_name=None)+++def test_end_user_empty_page_and_missing_cursor(+ sqlite_session_factory: sessionmaker[Session], end_user_ref: EndUserConversationRef+) -> None:+ repository = ConversationVariableRepository(session_factory=sqlite_session_factory)+ empty = repository.get_page(ref=end_user_ref, last_id=None, limit=20, variable_name=None)+ assert empty.data == ()+ assert empty.has_more is False+ with pytest.raises(ConversationVariableNotExistsError):+ repository.get_page(ref=end_user_ref, last_id="missing", limit=20, variable_name=None)++[email protected]("limit", [1, 2, 3])+def test_end_user_pagination_returns_all_timestamp_ties_in_id_order(+ sqlite_session_factory: sessionmaker[Session], end_user_ref: EndUserConversationRef, limit: int+) -> None:+ ids = [str(UUID(int=index)) for index in range(1, 5)]+ with sqlite_session_factory.begin() as session:+ # Insert ties in reverse ID order; the newest variable has the smallest ID.+ for index in (3, 2, 1, 0):+ _variable(+ session,+ end_user_ref,+ logical_id=ids[index],+ name=f"Variable {index}",+ created_at=_TIME + timedelta(seconds=1 if index == 0 else 0),+ )+ repository = ConversationVariableRepository(session_factory=sqlite_session_factory)+ expected_ids = ids[1:] + ids[:1]+ last_id: str | None = None+ for offset in range(0, len(expected_ids), limit):+ page = repository.get_page(ref=end_user_ref, last_id=last_id, limit=limit, variable_name=None)+ assert [row.id for row in page.data] == expected_ids[offset : offset + limit]+ assert page.has_more is (offset + limit < len(expected_ids))+ last_id = page.data[-1].id++ empty = repository.get_page(ref=end_user_ref, last_id=last_id, limit=limit, variable_name=None)+ assert empty.data == ()+ assert empty.has_more is False++[email protected]("search", ["CUSTOM", "stom", "%", "_", "\\"])+def test_name_filter_matches_literal_case_insensitive_substrings(+ sqlite_session_factory: sessionmaker[Session], end_user_ref: EndUserConversationRef, search: str+) -> None:+ with sqlite_session_factory.begin() as session:+ expected = _variable(session, end_user_ref, logical_id="match", name="Custom_%\\name", created_at=_TIME)+ decoy = _variable(session, end_user_ref, logical_id="decoy", name="Other name", created_at=_TIME)+ repository = ConversationVariableRepository(session_factory=sqlite_session_factory)+ page = repository.get_page(ref=end_user_ref, last_id=None, limit=20, variable_name=search)+ assert [row.id for row in page.data] == [expected.id]+ with pytest.raises(ConversationVariableNotExistsError):+ repository.get_page(ref=end_user_ref, last_id=decoy.id, limit=20, variable_name=search)++[email protected]("dialect_name", ["postgresql", "mysql"])+def test_name_filter_compiles_for_database_json_types(dialect_name: str) -> None:+ expression = ConversationVariableRepository._name_filter(variable_name="custom_%", dialect_name=dialect_name)+ dialect = postgresql.dialect() if dialect_name == "postgresql" else mysql.dialect()+ compiled = expression.compile(dialect=dialect)+ statement = str(compiled)+ if dialect_name == "postgresql":+ assert "json_extract_path_text(CAST(workflow_conversation_variables.data AS JSON)" in statement+ assert "ILIKE" in statement+ else:+ assert "json_extract(workflow_conversation_variables.data" in statement+ assert "lower(" in statement+ assert "%custom\\_\\%%" in compiled.params.values()+++def test_variable_queries_do_not_cross_app_or_conversation_composite_key(+ sqlite_session_factory: sessionmaker[Session], end_user_ref: EndUserConversationRef+) -> None:+ other = replace(end_user_ref, app_id="app-2", end_user_id="end-user-2", conversation_id="conversation-2")+ with sqlite_session_factory.begin() as session:+ session.add(make_app(app_id=other.app_id, mode=AppMode.ADVANCED_CHAT))+ session.add(make_end_user(end_user_id=other.end_user_id, app_id=other.app_id))+ session.add(+ make_conversation(+ conversation_id=other.conversation_id,+ app_id=other.app_id,+ inputs={},+ from_source=ConversationFromSource.API,+ from_end_user_id=other.end_user_id,+ is_deleted=False,+ )+ )+ own = _variable(session, end_user_ref, logical_id="shared-draft-id", name="Shared name", created_at=_TIME)+ duplicate = _variable(session, other, logical_id="shared-draft-id", name="Shared name", created_at=_TIME)+ foreign = _variable(session, other, logical_id="foreign", name="Foreign", created_at=_TIME)+ _variable(+ session,+ replace(end_user_ref, app_id=other.app_id),+ logical_id="invalid-owner",+ name="Wrong app",+ created_at=_TIME,+ )+ repository = ConversationVariableRepository(session_factory=sqlite_session_factory)+ assert [+ row.id for row in repository.get_page(ref=end_user_ref, last_id=None, limit=20, variable_name=None).data+ ] == [own.id]+ with pytest.raises(ConversationVariableNotExistsError):+ repository.get_page(ref=end_user_ref, last_id=foreign.id, limit=20, variable_name=None)+ with pytest.raises(ConversationVariableNotExistsError):+ repository.update(ref=end_user_ref, variable_id=foreign.id, transform=_replacement)+ result = repository.update(ref=end_user_ref, variable_id=own.id, transform=_replacement)+ assert result.id == own.id == duplicate.id+ assert result.variable.id == "shared-draft-id"+ assert result.variable.name == "Shared name"+ assert result.variable.description == "Keep me"+ assert result.variable.value == "updated"+ with sqlite_session_factory() as session:+ stored = session.get_one(ConversationVariable, (own.id, end_user_ref.conversation_id))+ untouched = session.get_one(ConversationVariable, (duplicate.id, other.conversation_id))+ assert stored.to_variable().value == "updated"+ assert stored.updated_at == result.updated_at > _TIME+ assert stored.created_at == result.created_at == _TIME+ assert untouched.to_variable().value == "original"++[email protected]("field", ["app_id", "tenant_id", "end_user_id", "conversation_id"])[email protected]("write", [False, True])+def test_ref_owner_fields_are_all_required(+ sqlite_session_factory: sessionmaker[Session], end_user_ref: EndUserConversationRef, field: str, write: bool+) -> None:+ with sqlite_session_factory.begin() as session:+ row = _variable(session, end_user_ref, logical_id="own", name="Own", created_at=_TIME)+ repository = ConversationVariableRepository(session_factory=sqlite_session_factory)+ wrong_ref = replace(end_user_ref, **{field: "unknown"})+ if write:+ with pytest.raises(ConversationNotExistsError):+ repository.update(ref=wrong_ref, variable_id=row.id, transform=_replacement)+ else:+ with pytest.raises(ConversationNotExistsError):+ repository.get_page(ref=wrong_ref, last_id=None, limit=20, variable_name=None)+++ "changed",+ [+ "app_deleted",+ "app_tenant",+ "end_user_deleted",+ "end_user_app",+ "end_user_tenant",+ "conversation_app",+ "conversation_end_user",+ "conversation_source",+ "conversation_account",+ "conversation_deleted",+ ],+)[email protected]("write", [False, True])+def test_owner_chain_is_rechecked_after_admission(+ sqlite_session_factory: sessionmaker[Session], end_user_ref: EndUserConversationRef, changed: str, write: bool+) -> None:+ with sqlite_session_factory.begin() as session:+ row = _variable(session, end_user_ref, logical_id="own", name="Own", created_at=_TIME)+ app = session.get_one(App, end_user_ref.app_id)+ user = session.get_one(EndUser, end_user_ref.end_user_id)+ conversation = session.get_one(Conversation, end_user_ref.conversation_id)+ if changed == "app_deleted":+ session.delete(app)+ elif changed == "app_tenant":+ app.tenant_id = "other-tenant"+ elif changed == "end_user_deleted":+ session.delete(user)+ elif changed == "end_user_app":+ user.app_id = "other-app"+ elif changed == "end_user_tenant":+ user.tenant_id = "other-tenant"+ elif changed == "conversation_app":+ conversation.app_id = "other-app"+ elif changed == "conversation_end_user":+ conversation.from_end_user_id = "other-user"+ elif changed == "conversation_source":+ conversation.from_source = ConversationFromSource.CONSOLE+ elif changed == "conversation_account":+ conversation.from_account_id = "account-1"+ else:+ conversation.is_deleted = True+ repository = ConversationVariableRepository(session_factory=sqlite_session_factory)+ if write:+ with pytest.raises(ConversationNotExistsError):+ repository.update(ref=end_user_ref, variable_id=row.id, transform=_replacement)+ else:+ with pytest.raises(ConversationNotExistsError):+ repository.get_page(ref=end_user_ref, last_id=None, limit=20, variable_name=None)++[email protected]("operation", ["read", "write", "decode_error", "transform_error"])+def test_variable_operations_release_sessions_and_roll_back_failed_transforms(+ sqlite_session_factory: sessionmaker[Session],+ sqlite_engine: Engine,+ end_user_ref: EndUserConversationRef,+ operation: str,+) -> None:+ with sqlite_session_factory.begin() as session:+ row = _variable(session, end_user_ref, logical_id="own", name="Own", created_at=_TIME)+ if operation == "decode_error":+ row.data = "invalid json"+ repository = ConversationVariableRepository(session_factory=sqlite_session_factory)+ active_connections = 0++ def checkout(*_args: object) -> None:+ nonlocal active_connections+ active_connections += 1++ def checkin(*_args: object) -> None:+ nonlocal active_connections+ active_connections -= 1++ def failed_transform(variable: VariableBase) -> VariableBase:+ assert active_connections == 1+ assert variable.value == "original"+ raise ValueError("invalid replacement")++ event.listen(sqlite_engine, "checkout", checkout)+ event.listen(sqlite_engine, "checkin", checkin)+ try:+ if operation == "decode_error":+ with pytest.raises(JSONDecodeError):+ repository.get_page(ref=end_user_ref, last_id=None, limit=20, variable_name=None)+ elif operation == "transform_error":+ with pytest.raises(ValueError, match="invalid replacement"):+ repository.update(ref=end_user_ref, variable_id=row.id, transform=failed_transform)+ elif operation == "write":+ repository.update(ref=end_user_ref, variable_id=row.id, transform=_replacement)+ else:+ repository.get_page(ref=end_user_ref, last_id=None, limit=20, variable_name=None)+ assert active_connections == 0+ if operation == "transform_error":+ with sqlite_session_factory() as session:+ stored = session.get_one(ConversationVariable, (row.id, end_user_ref.conversation_id))+ assert stored.to_variable().name == "Own"+ assert stored.to_variable().value == "original"+ assert stored.updated_at == _TIME+ finally:+ event.remove(sqlite_engine, "checkout", checkout)+ event.remove(sqlite_engine, "checkin", checkin)diff --git a/api/tests/unit_tests/services/test_conversation_service.py b/api/tests/unit_tests/services/test_conversation_service.pyindex 4ff1941450b..b8111ed95f0 100644--- a/api/tests/unit_tests/services/test_conversation_service.py+++ b/api/tests/unit_tests/services/test_conversation_service.py@@ -7,19 +7,15 @@ and both positive and negative test scenarios. Database paths use isolatedin-memory SQLite sessions with persisted ORM rows."""-import jsonfrom dataclasses import replace-from datetime import timedeltafrom unittest.mock import MagicMock-from uuid import UUIDimport pytestfrom sqlalchemy import eventfrom sqlalchemy.orm import Session-from factories import variable_factoryfrom libs.datetime_utils import naive_utc_now-from models import Account, ConversationVariable+from models import Accountfrom models.agent import (AgentConfigVersionKind,AgentWorkingResourceStatus,@@ -38,35 +34,8 @@ TENANT_ID = "11111111-1111-1111-1111-111111111111"APP_ID = "22222222-2222-2222-2222-222222222222"ACCOUNT_ID = "33333333-3333-3333-3333-333333333333"CONVERSATION_ID = "44444444-4444-4444-4444-444444444444"-VARIABLE_ID = "55555555-5555-5555-5555-555555555555"-OTHER_VARIABLE_ID = "66666666-6666-6666-6666-666666666666"OTHER_APP_ID = "77777777-7777-7777-7777-777777777777"OTHER_CONVERSATION_ID = "88888888-8888-8888-8888-888888888888"-OTHER_APP_VARIABLE_ID = "99999999-9999-9999-9999-999999999999"-OTHER_CONVERSATION_VARIABLE_ID = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"---def _conversation_variable(- *,- variable_id: str,- name: str,- value: str,- conversation_id: str = CONVERSATION_ID,- app_id: str = APP_ID,-) -> ConversationVariable:- return ConversationVariable(- id=variable_id,- conversation_id=conversation_id,- app_id=app_id,- data=json.dumps(- {- "id": variable_id,- "name": name,- "value_type": "string",- "value": value,- }- ),- )def _workspace_binding(binding_id: str) -> AgentWorkspaceBinding:@@ -390,115 +359,3 @@ def test_cleanup_propagates_agent_enqueue_failure_before_conversation_enqueue(mo)delete_related.assert_not_called()---class TestConversationServiceConversationalVariable:- """Test conversational variable operations."""-- @pytest.mark.parametrize("sqlite_session", [(Conversation, ConversationVariable)], indirect=True)- def test_imported_variable_id_can_be_used_for_pagination_and_update(self, sqlite_session: Session):- app_model = ConversationServiceTestDataFactory.create_app()- user = ConversationServiceTestDataFactory.create_account()- conversation = ConversationServiceTestDataFactory.create_conversation()- variable = variable_factory.build_conversation_variable_from_mapping(- {"id": "imported-topic", "name": "topic", "value_type": "string", "value": "original"}- )- imported_row = ConversationVariable.from_variable(- app_id=APP_ID, conversation_id=CONVERSATION_ID, variable=variable- )- imported_row.created_at = naive_utc_now()- next_row = _conversation_variable(variable_id=OTHER_VARIABLE_ID, name="next", value="next value")- next_row.created_at = imported_row.created_at + timedelta(seconds=1)- sqlite_session.add_all([conversation, imported_row, next_row])- sqlite_session.commit()-- first_page = ConversationService.get_conversational_variable(- app_model, CONVERSATION_ID, user, limit=1, last_id=None, session=sqlite_session- )- returned_id = first_page.data[0]["id"]- assert returned_id == imported_row.id- UUID(returned_id)- assert first_page.has_more is True-- second_page = ConversationService.get_conversational_variable(- app_model, CONVERSATION_ID, user, limit=1, last_id=returned_id, session=sqlite_session- )- assert [item["id"] for item in second_page.data] == [OTHER_VARIABLE_ID]- assert second_page.has_more is False-- updated = ConversationService.update_conversation_variable(- app_model, CONVERSATION_ID, returned_id, user, "updated", session=sqlite_session- )- assert updated["id"] == returned_id- assert updated["value"] == "updated"- sqlite_session.refresh(imported_row)- assert imported_row.to_variable().id == "imported-topic"- assert imported_row.to_variable().value == "updated"-- @pytest.mark.parametrize("sqlite_session", [(Conversation, ConversationVariable)], indirect=True)- def test_get_conversational_variable_with_name_filter_mysql(- self,- sqlite_session: Session,- config_overrides,- ):- """- Test variable filtering by name for MySQL databases.-- Should apply JSON extraction filter for variable names.- """- # Arrange- app_model = ConversationServiceTestDataFactory.create_app()- user = ConversationServiceTestDataFactory.create_account()- conversation = ConversationServiceTestDataFactory.create_conversation()- matching_variable = _conversation_variable(- variable_id=VARIABLE_ID,- name="test_var",- value="matching",- )- other_variable = _conversation_variable(- variable_id=OTHER_VARIABLE_ID,- name="unrelated",- value="excluded",- )- other_app_variable = _conversation_variable(- variable_id=OTHER_APP_VARIABLE_ID,- name="test_var",- value="other-app",- app_id=OTHER_APP_ID,- )- other_conversation_variable = _conversation_variable(- variable_id=OTHER_CONVERSATION_VARIABLE_ID,- name="test_var",- value="other-conversation",- conversation_id=OTHER_CONVERSATION_ID,- )- sqlite_session.add_all(- [- conversation,- matching_variable,- other_variable,- other_app_variable,- other_conversation_variable,- ]- )- sqlite_session.commit()- config_overrides(DB_TYPE="mysql")-- # Act- result = ConversationService.get_conversational_variable(- app_model=app_model,- conversation_id=CONVERSATION_ID,- user=user,- limit=10,- last_id=None,- variable_name="test_var",- session=sqlite_session,- )-- # Assert - SQLite executes the MySQL-compatible JSON extraction boundary.- assert result.has_more is False- assert result.limit == 10- assert len(result.data) == 1- assert result.data[0]["id"] == VARIABLE_ID- assert result.data[0]["name"] == "test_var"- assert result.data[0]["value"] == "matching"diff --git a/api/tests/unit_tests/services/test_conversation_variable_command_service.py b/api/tests/unit_tests/services/test_conversation_variable_command_service.pynew file mode 100644index 00000000000..3245f15ae3c--- /dev/null+++ b/api/tests/unit_tests/services/test_conversation_variable_command_service.py@@ -0,0 +1,202 @@+"""Variable update policy through real factories and owned SQLite transactions."""++from datetime import datetime, timedelta+from uuid import UUID, uuid4++import pytest+from pydantic import JsonValue+from sqlalchemy.orm import Session, sessionmaker++from configs import dify_config+from factories import variable_factory+from graphon.variables.exc import VariableError+from libs.datetime_utils import naive_utc_now+from models import ConversationVariable+from models.enums import ConversationFromSource, EndUserType+from repositories.conversation_variable_repository import ConversationVariableRepository+from services.conversation_variable_command_service import ConversationVariableCommandService+from services.entities.conversation_entities import EndUserConversationRef+from services.errors.conversation import ConversationVariableNotExistsError, ConversationVariableTypeMismatchError+from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user++_CREATED_AT = datetime(2024, 1, 1, 9, 0, 0)+++def ref(sqlite_session_factory: sessionmaker[Session]) -> EndUserConversationRef:+ with sqlite_session_factory.begin() as session:+ session.add_all(+ [+ make_app(),+ make_end_user(app_id="app-1", end_user_type=EndUserType.SERVICE_API),+ make_conversation(inputs={}, from_source=ConversationFromSource.API, from_end_user_id="end-user-1"),+ ]+ )+ return EndUserConversationRef(+ app_id="app-1", tenant_id="tenant-1", end_user_id="end-user-1", conversation_id="conversation-1"+ )+++def commands(sqlite_session_factory: sessionmaker[Session]) -> ConversationVariableCommandService:+ return ConversationVariableCommandService(+ variables=ConversationVariableRepository(session_factory=sqlite_session_factory)+ )+++def _seed_variable(+ factory: sessionmaker[Session],+ *,+ ref: EndUserConversationRef,+ value_type: str,+ value: JsonValue,+ logical_id: str,+) -> ConversationVariable:+ variable = variable_factory.build_conversation_variable_from_mapping(+ {+ "id": logical_id,+ "name": "topic",+ "description": "Keep this description",+ "selector": ["conversation", "topic"],+ "value_type": value_type,+ "value": value,+ }+ )+ row = ConversationVariable.from_variable(app_id=ref.app_id, conversation_id=ref.conversation_id, variable=variable)+ row.created_at = row.updated_at = _CREATED_AT+ with factory.begin() as session:+ session.add(row)+ session.flush()+ session.expunge(row)+ return row+++ ("value_type", "original", "replacement"),+ [+ ("string", "original", ""),+ ("secret", "original", "new secret"),+ ("integer", 1, 42),+ ("integer", 1, 0),+ ("float", 1.5, 2.5),+ ("boolean", True, False),+ ("object", {"original": "value"}, {"zero": 0, "nested": [False, None]}),+ ("array[string]", ["original"], []),+ ("array[number]", [1], [0, 2.5]),+ ("array[boolean]", [True], [False]),+ ("array[object]", [{"original": "value"}], [{"nested": {"zero": 0}}]),+ ],+)+def test_update_validates_and_persists_value_without_changing_metadata(+ sqlite_session_factory: sessionmaker[Session],+ ref: EndUserConversationRef,+ commands: ConversationVariableCommandService,+ value_type: str,+ original: JsonValue,+ replacement: JsonValue,+) -> None:+ row = _seed_variable(+ sqlite_session_factory, ref=ref, value_type=value_type, value=original, logical_id=str(uuid4())+ )+ before = naive_utc_now()+ result = commands.update_value(ref=ref, variable_id=row.id, new_value=replacement)+ after = naive_utc_now()++ assert result.id == row.id+ assert result.created_at == _CREATED_AT+ assert before <= result.updated_at <= after+ assert result.variable.value == replacement+ assert result.variable.model_dump(exclude={"value"}) == row.to_variable().model_dump(exclude={"value"})+ with sqlite_session_factory() as session:+ persisted = session.get_one(ConversationVariable, (row.id, ref.conversation_id))+ assert persisted.to_variable() == result.variable+++def test_imported_logical_id_keeps_storage_uuid_for_pagination_and_updates(+ sqlite_session_factory: sessionmaker[Session],+ ref: EndUserConversationRef,+ commands: ConversationVariableCommandService,+) -> None:+ row = _seed_variable(+ sqlite_session_factory, ref=ref, value_type="string", value="original", logical_id="imported-topic"+ )+ next_variable = variable_factory.build_conversation_variable_from_mapping(+ {"id": str(uuid4()), "name": "next", "value_type": "string", "value": "next value"}+ )+ next_row = ConversationVariable.from_variable(+ app_id=ref.app_id, conversation_id=ref.conversation_id, variable=next_variable+ )+ next_row.created_at = _CREATED_AT + timedelta(seconds=1)+ with sqlite_session_factory.begin() as session:+ session.add(next_row)+ session.flush()+ next_id = next_row.id++ queries = ConversationVariableRepository(session_factory=sqlite_session_factory)+ first = queries.get_page(ref=ref, last_id=None, limit=1, variable_name=None)+ assert first.data[0].id == row.id+ assert str(UUID(first.data[0].id)) == row.id+ assert first.has_more is True+ second = queries.get_page(ref=ref, last_id=first.data[0].id, limit=1, variable_name=None)+ assert [record.id for record in second.data] == [next_id]+ assert second.has_more is False++ result = commands.update_value(ref=ref, variable_id=first.data[0].id, new_value="updated")+ assert result.id == row.id+ assert result.variable.id == "imported-topic"+ assert result.variable.value == "updated"+ with sqlite_session_factory() as session:+ assert session.get_one(ConversationVariable, (row.id, ref.conversation_id)).to_variable() == result.variable+++ ("value_type", "original", "invalid"),+ [+ ("string", "original", 42),+ ("string", "original", None),+ ("integer", 1, "wrong"),+ ("float", 1.5, "wrong"),+ ("boolean", True, "wrong"),+ ("object", {}, []),+ ("array[string]", ["original"], ["valid", 42]),+ ],+)+def test_type_mismatch_keeps_persisted_value_and_reports_expected_type(+ sqlite_session_factory: sessionmaker[Session],+ ref: EndUserConversationRef,+ commands: ConversationVariableCommandService,+ value_type: str,+ original: JsonValue,+ invalid: JsonValue,+) -> None:+ row = _seed_variable(+ sqlite_session_factory, ref=ref, value_type=value_type, value=original, logical_id=str(uuid4())+ )+ expected_type = "number" if value_type == "integer" else value_type+ with pytest.raises(ConversationVariableTypeMismatchError) as raised:+ commands.update_value(ref=ref, variable_id=row.id, new_value=invalid)+ assert f"variable 'topic' expects {expected_type}" in str(raised.value)+ assert "but got" in str(raised.value)+ with sqlite_session_factory() as session:+ assert session.get_one(ConversationVariable, (row.id, ref.conversation_id)).data == row.data+++def test_factory_size_limit_rejects_update_without_writing(+ sqlite_session_factory: sessionmaker[Session],+ ref: EndUserConversationRef,+ commands: ConversationVariableCommandService,+) -> None:+ row = _seed_variable(+ sqlite_session_factory, ref=ref, value_type="string", value="original", logical_id=str(uuid4())+ )+ with pytest.raises(VariableError, match="exceeds limit"):+ commands.update_value(ref=ref, variable_id=row.id, new_value="x" * (dify_config.MAX_VARIABLE_SIZE + 1))+ with sqlite_session_factory() as session:+ assert session.get_one(ConversationVariable, (row.id, ref.conversation_id)).data == row.data+++def test_missing_variable_raises_domain_error(+ ref: EndUserConversationRef, commands: ConversationVariableCommandService+) -> None:+ with pytest.raises(ConversationVariableNotExistsError):+ commands.update_value(ref=ref, variable_id=str(uuid4()), new_value="value")diff --git a/packages/contracts/generated/api/service/errors.gen.ts b/packages/contracts/generated/api/service/errors.gen.tsindex d162a833212..ddbf7aff256 100644--- a/packages/contracts/generated/api/service/errors.gen.ts+++ b/packages/contracts/generated/api/service/errors.gen.ts@@ -114,6 +114,22 @@ const zConversationNotFoundError = /* @__PURE__ */ (() =>}),))()+const zConversationVariableNotFoundError = /* @__PURE__ */ (() =>+ zErrorResponse.and(+ z.object({+ code: z.literal('conversation_variable_not_found'),+ status: z.literal(404),+ }),+ ))()++const zConversationVariableTypeMismatchError = /* @__PURE__ */ (() =>+ zErrorResponse.and(+ z.object({+ code: z.literal('conversation_variable_type_mismatch'),+ status: z.literal(400),+ }),+ ))()+const zDatasetNameDuplicateError = /* @__PURE__ */ (() =>zErrorResponse.and(z.object({@@ -569,9 +585,33 @@ export const post8Errors = {workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },}-export const put2Errors = { not_chat_app: { status: 400, data: zNotChatAppError } }+export const put2Errors = {+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ conversation_not_found: { status: 404, data: zConversationNotFoundError },+ conversation_variable_not_found: { status: 404, data: zConversationVariableNotFoundError },+ conversation_variable_type_mismatch: {+ status: 400,+ data: zConversationVariableTypeMismatchError,+ },+ 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 },+}-export const get5Errors = { not_chat_app: { status: 400, data: zNotChatAppError } }+export const get5Errors = {+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ conversation_not_found: { status: 404, data: zConversationNotFoundError },+ conversation_variable_not_found: { status: 404, data: zConversationVariableNotFoundError },+ 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 },+}export const delete2Errors = {app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },diff --git a/packages/contracts/generated/api/service/orpc.gen.ts b/packages/contracts/generated/api/service/orpc.gen.tsindex 09a735e599e..3104ce6f963 100644--- a/packages/contracts/generated/api/service/orpc.gen.ts+++ b/packages/contracts/generated/api/service/orpc.gen.ts@@ -16,6 +16,8 @@ import {zConversationCursorNotFoundError,zConversationFirstMessageNotFoundError,zConversationNotFoundError,+ zConversationVariableNotFoundError,+ zConversationVariableTypeMismatchError,zDatasetNameDuplicateError,zEndUserNotFoundError,zFileAccessDeniedError,@@ -963,7 +965,21 @@ export const put2 = oc}),).output(zPutConversationsByConversationIdVariablesByVariableIdResponse)- .errors({ not_chat_app: { status: 400, data: zNotChatAppError } })+ .errors({+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ conversation_not_found: { status: 404, data: zConversationNotFoundError },+ conversation_variable_not_found: { status: 404, data: zConversationVariableNotFoundError },+ conversation_variable_type_mismatch: {+ status: 400,+ data: zConversationVariableTypeMismatchError,+ },+ 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 },+ })export const byVariableId = {put: put2,@@ -991,7 +1007,17 @@ export const get5 = oc}),).output(zGetConversationsByConversationIdVariablesResponse)- .errors({ not_chat_app: { status: 400, data: zNotChatAppError } })+ .errors({+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },+ app_api_disabled: { status: 403, data: zAppApiDisabledError },+ app_not_found: { status: 403, data: zAppNotFoundError },+ conversation_not_found: { status: 404, data: zConversationNotFoundError },+ conversation_variable_not_found: { status: 404, data: zConversationVariableNotFoundError },+ 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 },+ })export const variables = {get: get5,diff --git a/packages/contracts/generated/api/service/types.gen.ts b/packages/contracts/generated/api/service/types.gen.tsindex 2c196e2d636..94f5164657f 100644--- a/packages/contracts/generated/api/service/types.gen.ts+++ b/packages/contracts/generated/api/service/types.gen.ts@@ -571,6 +571,11 @@ export type ConversationVariableInfiniteScrollPaginationResponse = {limit: number}+export type ConversationVariableNotFoundError = ErrorResponse & {+ code: 'conversation_variable_not_found'+ status: 404+}+export type ConversationVariableResponse = {created_at?: number | nulldescription?: string | null@@ -581,13 +586,18 @@ export type ConversationVariableResponse = {value_type: string}+export type ConversationVariableTypeMismatchError = ErrorResponse & {+ code: 'conversation_variable_type_mismatch'+ status: 400+}+export type ConversationVariableUpdatePayload = {- value: unknown+ value: JsonValue}export type ConversationVariableUpdatePayloadWithUser = {user?: string- value: unknown+ value: JsonValue}export type ConversationVariablesQuery = {@@ -5099,8 +5109,16 @@ export type GetConversationsByConversationIdVariablesData = {export type GetConversationsByConversationIdVariablesErrors = {400: NotChatAppError | ErrorResponse401: ErrorResponse- 403: ErrorResponse- 404: ErrorResponse+ 403:+ | AppAbnormalStatusError+ | AppApiDisabledError+ | AppNotFoundError+ | ForbiddenError+ | WorkspaceArchivedError+ | WorkspaceNotFoundError+ | ErrorResponse+ 404: ConversationNotFoundError | ConversationVariableNotFoundError | ErrorResponse+ 422: ErrorResponsedefault: ErrorResponse}@@ -5125,10 +5143,18 @@ export type PutConversationsByConversationIdVariablesByVariableIdData = {}export type PutConversationsByConversationIdVariablesByVariableIdErrors = {- 400: NotChatAppError | ErrorResponse+ 400: ConversationVariableTypeMismatchError | NotChatAppError | ErrorResponse401: ErrorResponse- 403: ErrorResponse- 404: ErrorResponse+ 403:+ | AppAbnormalStatusError+ | AppApiDisabledError+ | AppNotFoundError+ | ForbiddenError+ | WorkspaceArchivedError+ | WorkspaceNotFoundError+ | ErrorResponse+ 404: ConversationNotFoundError | ConversationVariableNotFoundError | ErrorResponse+ 422: ErrorResponsedefault: ErrorResponse}diff --git a/packages/contracts/generated/api/service/zod.gen.ts b/packages/contracts/generated/api/service/zod.gen.tsindex 8dabb0cbdbb..c6a8f200bf0 100644--- a/packages/contracts/generated/api/service/zod.gen.ts+++ b/packages/contracts/generated/api/service/zod.gen.ts@@ -504,21 +504,6 @@ export const zConversationVariableInfiniteScrollPaginationResponse = z.object({limit: z.int(),})-/**- * ConversationVariableUpdatePayload- */-export const zConversationVariableUpdatePayload = z.object({- value: z.unknown(),-})--/**- * ConversationVariableUpdatePayload- */-export const zConversationVariableUpdatePayloadWithUser = z.object({- user: z.string().optional(),- value: z.unknown(),-})-/*** ConversationVariablesQuery*/@@ -1088,6 +1073,30 @@ export const zConversationNotFoundError = zErrorResponse.and(}),)+/**+ * ConversationVariableNotFoundError+ *+ * The conversation variable was not found for this conversation.+ */+export const zConversationVariableNotFoundError = zErrorResponse.and(+ z.object({+ code: z.literal('conversation_variable_not_found'),+ status: z.literal(404),+ }),+)++/**+ * ConversationVariableTypeMismatchError+ *+ * The new value does not match the conversation variable's type.+ */+export const zConversationVariableTypeMismatchError = zErrorResponse.and(+ z.object({+ code: z.literal('conversation_variable_type_mismatch'),+ status: z.literal(400),+ }),+)+/*** DatasetNameDuplicateError*@@ -1466,6 +1475,21 @@ export const zJsonValueType = z.unknown()export const zJsonValue = z.unknown()+/**+ * ConversationVariableUpdatePayload+ */+export const zConversationVariableUpdatePayload = z.object({+ value: zJsonValue,+})++/**+ * ConversationVariableUpdatePayload+ */+export const zConversationVariableUpdatePayloadWithUser = z.object({+ user: z.string().optional(),+ value: zJsonValue,+})+/*** DocumentResponse*/@@ -6255,7 +6279,7 @@ export const zGetConversationsByConversationIdVariablesQuery = z.object({})/**- * Successfully retrieved conversation variables.+ * Conversation variables retrieved successfully.*/export const zGetConversationsByConversationIdVariablesResponse =zConversationVariableInfiniteScrollPaginationResponse