Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(workflows): stop destroying node config and descriptions on ordinary saves
trohitg5f7522250eae
diff --git a/server/services/deployment/handlers.py b/server/services/deployment/handlers.pyindex b5ceaeef..ae002de9 100644--- a/server/services/deployment/handlers.py+++ b/server/services/deployment/handlers.py@@ -130,7 +130,7 @@ async def handle_deploy_workflow(data: Dict[str, Any], websocket: WebSocket) ->edges = list(safe_graph["edges"])normalized_parameters = normalization.node_parametersmigration_warnings = normalization.warnings- imported = await import_legacy_context_receipts(+ await import_legacy_context_receipts(container.database(),normalization.state_imports,)@@ -138,9 +138,6 @@ async def handle_deploy_workflow(data: Dict[str, Any], websocket: WebSocket) ->container.database(),aliases=normalization.aliases,parameters=normalized_parameters,- context_import_completed=(- imported == len(normalization.state_imports)- ),)graph_version = normalization.graph_versiongraph_aliases = normalization.aliases@@ -1671,7 +1668,7 @@ async def handle_start_workflow(data: Dict[str, Any], websocket: WebSocket) -> D"aliases": normalization.aliases,"migration_warnings": normalization.warnings,}- imported = await import_legacy_context_receipts(+ await import_legacy_context_receipts(service.database,normalization.state_imports,)@@ -1679,7 +1676,6 @@ async def handle_start_workflow(data: Dict[str, Any], websocket: WebSocket) -> Dservice.database,aliases=normalization.aliases,parameters=normalization.node_parameters,- context_import_completed=(imported == len(normalization.state_imports)),)control, created = await service.begin_generation(workflow_id=workflow_id,diff --git a/server/services/example_loader.py b/server/services/example_loader.pyindex 93823161..89cfbfbe 100644--- a/server/services/example_loader.py+++ b/server/services/example_loader.py@@ -147,9 +147,8 @@ async def import_examples_for_user(database) -> int:)await persist_parameter_aliases(database,- aliases={},+ aliases=normalization.aliases,parameters=node_parameters,- context_import_completed=True,)return importeddiff --git a/server/services/workflow_context_migration.py b/server/services/workflow_context_migration.pyindex 1bfeb1fc..3133d3a6 100644--- a/server/services/workflow_context_migration.py+++ b/server/services/workflow_context_migration.py@@ -14,24 +14,6 @@ from typing import Any, Iterable, Mappingfrom services.agent_context import AgentContextStore-_LEGACY_RUNTIME_FIELDS = frozenset(- {- "memory_content",- "memory_jsonl",- "last_session_id",- "vertex_interaction_id",- "vertex_environment_id",- "session_id",- "window_size",- "long_term_enabled",- "retrieval_count",- "embedding_provider",- "embedding_model",- "embedding_endpoint",- }-)--async def load_node_parameters(database: Any,nodes: Iterable[Mapping[str, Any]],@@ -138,9 +120,20 @@ async def persist_parameter_aliases(*,aliases: Mapping[str, str],parameters: Mapping[str, Mapping[str, Any]],- context_import_completed: bool,) -> None:- """Rekey node configuration and retire imported legacy runtime fields."""+ """Rekey node configuration onto canonical node ids.++ This only moves rows; it never edits their contents. It previously also+ stripped a set of "legacy runtime fields" from every node, which destroyed+ real configuration: ``session_id`` is in that set and is a declared,+ load-bearing parameter on ``chatTrigger`` / ``chatSend`` / ``chatHistory``,+ so an ordinary read silently widened a chat trigger to match every session.++ Retiring them is unnecessary as well as unsafe. ``SimpleMemoryParams``+ declares ``extra="ignore"``, so a leftover ``memory_content`` on a migrated+ node is already inert — it reaches neither runtime behaviour nor the+ NodeSpec.+ """reverse_aliases = {new: old for old, new in aliases.items()}save = getattr(database, "save_node_parameters", None)@@ -148,17 +141,7 @@ async def persist_parameter_aliases(if not callable(save):returnfor node_id, raw_params in parameters.items():- params = dict(raw_params or {})- if context_import_completed and any(- key in params for key in _LEGACY_RUNTIME_FIELDS- ):- params = {- key: value- for key, value in params.items()- if key not in _LEGACY_RUNTIME_FIELDS- }- params.setdefault("reset_policy", "preserve")- await save(node_id, params)+ await save(node_id, dict(raw_params or {}))old_id = reverse_aliases.get(node_id)if old_id and old_id != node_id and callable(remove):await remove(old_id)diff --git a/server/services/workflow_import.py b/server/services/workflow_import.pyindex f6793247..51c80ba1 100644--- a/server/services/workflow_import.py+++ b/server/services/workflow_import.py@@ -380,16 +380,17 @@ async def import_workflow(if not saved:return {"success": False, "error": "save_failed", "report": report}- # 8. Per-node configuration saves. Legacy transcript/provider fields have- # already been preserved as immutable Context artifacts and are retired- # from the Memory tool's configuration surface.+ # 8. Per-node configuration saves. Legacy transcript/provider fields are+ # preserved as immutable Context artifacts above; they are left in place+ # here because ``SimpleMemoryParams`` ignores them, while stripping them+ # would also have removed same-named fields that other node types really+ # declare.from services.workflow_context_migration import persist_parameter_aliasesawait persist_parameter_aliases(database,- aliases={},+ aliases=normalization.aliases,parameters=remapped_params,- context_import_completed=True,)saved_params = sum(bool(params) for params in remapped_params.values())diff --git a/server/services/workflow_storage/handlers.py b/server/services/workflow_storage/handlers.pyindex 52e1e45b..ddac1a8f 100644--- a/server/services/workflow_storage/handlers.py+++ b/server/services/workflow_storage/handlers.py@@ -447,7 +447,7 @@ async def handle_save_workflow(data: Dict[str, Any], websocket: WebSocket) -> Di# Import exact legacy artifacts before replacing their only topology# pointer. Receipts are idempotent, so a failed graph save is safe to retry.- imported = await import_legacy_context_receipts(+ await import_legacy_context_receipts(database,normalization.state_imports,)@@ -455,6 +455,9 @@ async def handle_save_workflow(data: Dict[str, Any], websocket: WebSocket) -> Di"workflow_id": storage_id,"name": name,"slug": slug,+ # Omitting this nulled the description on every save, because+ # ``save_workflow`` writes the column unconditionally.+ "description": getattr(existing, "description", None),"data": normalized_data,}if _supports_context_archive_outbox(database):@@ -467,7 +470,6 @@ async def handle_save_workflow(data: Dict[str, Any], websocket: WebSocket) -> Didatabase,aliases=normalization.aliases,parameters=normalization.node_parameters,- context_import_completed=(not normalization.state_imports or imported == len(normalization.state_imports)),)if _supports_context_archive_outbox(database):context_archives_completed, context_archives_pending = await _drain_context_archive_outbox(@@ -577,7 +579,7 @@ async def handle_get_workflow(data: Dict[str, Any], websocket: WebSocket) -> Dicnormalized_data = sanitize_workflow_graph(normalization.graph_data(workflow_data))if not context_errors:- imported = await import_legacy_context_receipts(+ await import_legacy_context_receipts(database,normalization.state_imports,)@@ -624,7 +626,6 @@ async def handle_get_workflow(data: Dict[str, Any], websocket: WebSocket) -> Dicdatabase,aliases=normalization.aliases,parameters=normalization.node_parameters,- context_import_completed=(not normalization.state_imports or imported == len(normalization.state_imports)),)return {"success": True,diff --git a/server/tests/services/test_workflow_parameter_preservation.py b/server/tests/services/test_workflow_parameter_preservation.pynew file mode 100644index 00000000..b0adf19d--- /dev/null+++ b/server/tests/services/test_workflow_parameter_preservation.py@@ -0,0 +1,247 @@+"""Saving or reading a workflow must not edit node configuration.++Two live data-loss bugs motivated this file.++1. ``persist_parameter_aliases`` stripped a set of "legacy runtime fields"+ from *every* node whenever a context import had completed. ``session_id``+ is in that set and is a declared, load-bearing parameter on ``chatTrigger``+ / ``chatSend`` / ``chatHistory`` — so an ordinary read silently widened a+ chat trigger to match every session. It also injected ``reset_policy`` into+ node types that have no such concept.++2. ``handle_save_workflow`` omitted ``description`` from its ``save_workflow``+ kwargs while ``handle_get_workflow`` passed it, so every save nulled the+ workflow description.++Both were reachable from the editor's ordinary save/open cycle.+"""++from __future__ import annotations++import sys+import types+from types import SimpleNamespace+from typing import Any, Dict, List, Optional, Tuple+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+++CHAT_NODE_TYPES = ("chatTrigger", "chatSend", "chatHistory")+++class _ParameterRecordingDatabase:+ """In-memory store that remembers every parameter mutation."""++ def __init__(self) -> None:+ self._rows: Dict[str, SimpleNamespace] = {}+ self._params: Dict[str, Dict[str, Any]] = {}+ self._next_workflow_id = 1+ self.deleted_parameter_ids: List[str] = []++ # -- workflow rows ----------------------------------------------------+ async def allocate_workflow_id(self) -> str:+ workflow_id = str(self._next_workflow_id)+ self._next_workflow_id += 1+ return workflow_id++ async def get_workflow(self, workflow_id: str):+ return self._rows.get(workflow_id)++ async def save_workflow(+ self,+ workflow_id: str,+ name: str,+ slug: str,+ data: Dict[str, Any],+ description: Optional[str] = None,+ context_id_aliases: Optional[Dict[str, str]] = None,+ ) -> bool:+ self._rows[workflow_id] = SimpleNamespace(+ id=workflow_id,+ name=name,+ slug=slug,+ description=description,+ data=data,+ created_at=None,+ updated_at=None,+ )+ return True++ async def list_workflow_slugs(self) -> List[Tuple[str, str]]:+ return [(r.id, r.slug) for r in self._rows.values()]++ # -- node parameters --------------------------------------------------+ async def get_node_parameters(self, node_id: str) -> Dict[str, Any]:+ return dict(self._params.get(node_id) or {})++ async def save_node_parameters(self, node_id: str, parameters: Dict[str, Any]) -> bool:+ self._params[node_id] = dict(parameters or {})+ return True++ async def delete_node_parameters(self, node_id: str) -> bool:+ self.deleted_parameter_ids.append(node_id)+ self._params.pop(node_id, None)+ return True++ # -- context archive outbox -------------------------------------------+ async def list_workflow_context_archive_outbox(self, workflow_id: str) -> List[Any]:+ return []++ async def complete_workflow_context_archive_outbox(self, *args: Any, **kwargs: Any) -> bool:+ return True+++def recording_db():+ return _ParameterRecordingDatabase()+++def patched_handlers(recording_db, tmp_path):+ """Wire the storage handlers to the recording DB.++ Mirrors the fixture in ``test_workflow_rename.py``: ``status_broadcaster``+ is stubbed at the ``sys.modules`` level because the real module imports+ ``orjson``, which the unit-test env does not install.+ """+ from services.workflow_storage import handlers++ broadcaster_spy = MagicMock()+ broadcaster_spy.broadcast_workflow_lifecycle = AsyncMock()++ stub_module = types.ModuleType("services.status_broadcaster")+ stub_module.get_status_broadcaster = lambda: broadcaster_spy+ sentinel = object()+ original = sys.modules.get("services.status_broadcaster", sentinel)+ sys.modules["services.status_broadcaster"] = stub_module++ workspace_root = tmp_path / "workspaces"+ workspace_root.mkdir()+ settings_stub = MagicMock()+ settings_stub.workspace_base_resolved = str(workspace_root)++ with patch.object(handlers, "container") as mock_container, patch.object(+ handlers, "Settings", return_value=settings_stub+ ):+ mock_container.database.return_value = recording_db+ try:+ yield SimpleNamespace(db=recording_db, handlers=handlers)+ finally:+ if original is sentinel:+ sys.modules.pop("services.status_broadcaster", None)+ else:+ sys.modules["services.status_broadcaster"] = original+++def _caller(user_id: str = "owner") -> SimpleNamespace:+ """A websocket-shaped stub.++ A bare ``MagicMock`` would make owner resolution return+ ``str(<MagicMock ...>)`` — stable per instance, garbage across instances.+ """+ return SimpleNamespace(state=SimpleNamespace(user_id=user_id))+++def _chat_graph(node_type: str) -> Dict[str, Any]:+ return {+ "nodes": [{"id": "chat-1", "type": node_type, "position": {"x": 0, "y": 0}, "data": {}}],+ "edges": [],+ }++[email protected]("node_type", CHAT_NODE_TYPES)+async def test_session_id_survives_save_then_read(patched_handlers, node_type):+ """The bug: an ordinary open destroyed a configured ``session_id``.++ A ``chatTrigger`` whose ``session_id`` is stripped silently reverts to+ matching every session, which is a routing change the user never made.+ """+ handlers = patched_handlers.handlers+ db = patched_handlers.db++ saved = await handlers.handle_save_workflow(+ {"workflow_id": "new", "name": "Chat", "data": _chat_graph(node_type)},+ _caller(),+ )+ assert saved["success"] is True+ workflow_id = saved["workflow_id"]++ node_id = saved["data"]["nodes"][0]["id"]+ await db.save_node_parameters(node_id, {"session_id": "sales"})++ read = await handlers.handle_get_workflow({"workflow_id": workflow_id}, _caller())+ assert read["success"] is True++ stored = await db.get_node_parameters(node_id)+ assert stored.get("session_id") == "sales"+ assert "reset_policy" not in stored+++async def test_read_does_not_inject_reset_policy_into_unrelated_nodes(patched_handlers):+ """``reset_policy`` belongs to the Memory tool and nothing else."""+ handlers = patched_handlers.handlers+ db = patched_handlers.db++ saved = await handlers.handle_save_workflow(+ {"workflow_id": "new", "name": "Chat", "data": _chat_graph("chatTrigger")},+ _caller(),+ )+ node_id = saved["data"]["nodes"][0]["id"]+ await db.save_node_parameters(node_id, {"session_id": "sales", "window_size": 12})++ await handlers.handle_get_workflow({"workflow_id": saved["workflow_id"]}, _caller())++ stored = await db.get_node_parameters(node_id)+ assert "reset_policy" not in stored+ # Leftover legacy keys are inert (SimpleMemoryParams declares+ # extra="ignore"), so preserving them is correct and cheaper than a+ # per-node retirement rule that cannot tell them apart from real fields.+ assert stored.get("window_size") == 12+++async def test_repeated_reads_are_stable(patched_handlers):+ """Reading twice must not drift configuration."""+ handlers = patched_handlers.handlers+ db = patched_handlers.db++ saved = await handlers.handle_save_workflow(+ {"workflow_id": "new", "name": "Chat", "data": _chat_graph("chatSend")},+ _caller(),+ )+ workflow_id = saved["workflow_id"]+ node_id = saved["data"]["nodes"][0]["id"]+ await db.save_node_parameters(node_id, {"session_id": "support"})++ await handlers.handle_get_workflow({"workflow_id": workflow_id}, _caller())+ first = await db.get_node_parameters(node_id)+ await handlers.handle_get_workflow({"workflow_id": workflow_id}, _caller())+ second = await db.get_node_parameters(node_id)++ assert first == second == {"session_id": "support"}+++async def test_save_preserves_description(patched_handlers):+ """Saving nulled the description because the kwarg was never passed."""+ handlers = patched_handlers.handlers+ db = patched_handlers.db++ saved = await handlers.handle_save_workflow(+ {"workflow_id": "new", "name": "Described", "data": _chat_graph("chatTrigger")},+ _caller(),+ )+ workflow_id = saved["workflow_id"]++ row = await db.get_workflow(workflow_id)+ row.description = "why this workflow exists"++ await handlers.handle_save_workflow(+ {"workflow_id": workflow_id, "name": "Described", "data": _chat_graph("chatTrigger")},+ _caller(),+ )++ assert (await db.get_workflow(workflow_id)).description == "why this workflow exists"diff --git a/server/tests/test_workflow_validator.py b/server/tests/test_workflow_validator.pyindex cc332c87..3c4a205e 100644--- a/server/tests/test_workflow_validator.py+++ b/server/tests/test_workflow_validator.py@@ -444,8 +444,15 @@ def _fake_database(existing_names: list[str] | None = None):saved_params[node_id] = paramsreturn True+ async def delete_node_parameters(node_id):+ saved_params.pop(node_id, None)+ return True+db.save_workflow = AsyncMock(side_effect=save_workflow)db.save_node_parameters = AsyncMock(side_effect=save_node_parameters)+ # Canonicalization rekeys parameter rows, so the double must expose the+ # real Database's async delete. A bare MagicMock returns a non-awaitable.+ db.delete_node_parameters = AsyncMock(side_effect=delete_node_parameters)db._saved_workflows = saved_workflows # test-readabledb._saved_params = saved_paramsreturn db