Skip to content

Self-improving AI Employees for everyone

fix: isolate workflow resets from durable memory

trohitg821cef982d4f
diff --git a/client/src/Dashboard.tsx b/client/src/Dashboard.tsx
index 51a833f9..915df733 100644
--- a/client/src/Dashboard.tsx
+++ b/client/src/Dashboard.tsx
@@ -829,7 +829,12 @@ const DashboardContent: React.FC = () => {
const handleResetWorkflow = async () => {
if (!currentWorkflow?.id) return;
- try { await resetWorkflow(currentWorkflow.id, workflowControl.revision); }
+ try {
+ await resetWorkflow(currentWorkflow.id, workflowControl.revision);
+ clearWorkflowExecutionState(currentWorkflow.id);
+ setExecutionResult(null);
+ setShowResult(false);
+ }
catch (error: any) { toast.error(error?.message || 'Unable to reset workflow'); }
};
@@ -1442,7 +1447,7 @@ const DashboardContent: React.FC = () => {
{/* Parameter Panels */}
<ErrorBoundary>
- <ParameterPanel />
+ <ParameterPanel key={`${currentWorkflow?.id || 'none'}:${workflowControl.generation || 0}:${workflowControl.state}`} />
<LocationParameterPanel />
</ErrorBoundary>
diff --git a/client/src/contexts/WebSocketContext.tsx b/client/src/contexts/WebSocketContext.tsx
index 26b9e96a..45947b15 100644
--- a/client/src/contexts/WebSocketContext.tsx
+++ b/client/src/contexts/WebSocketContext.tsx
@@ -118,6 +118,7 @@ export interface WorkflowControlStatus {
generation?: number;
execution_id?: string | null;
root_execution_id?: string | null;
+ data_scope_id?: string | null;
controller_workflow_id?: string | null;
controller_run_id?: string | null;
state: WorkflowControlState;
@@ -1221,6 +1222,23 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
break;
}
+ case 'workflow_runtime_reset': {
+ const workflowId = message.workflow_id || message.data?.workflow_id;
+ if (workflowId) {
+ useNodeStatusStore.getState().clearWorkflow(workflowId);
+ setAllVariables((previous) => {
+ const next = { ...previous };
+ delete next[workflowId];
+ return next;
+ });
+ if (useAppStore.getState().currentWorkflow?.id === workflowId) {
+ setConsoleLogs([]);
+ setChatMessages([]);
+ }
+ }
+ break;
+ }
+
case 'deployment_status':
// Handle deployment status updates (event-driven, no iterations)
// Per-workflow scoping (n8n pattern): Only apply updates for current workflow
@@ -2392,8 +2410,24 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
const resumeWorkflowAsync = useCallback((workflowId: string, expectedRevision: number) =>
controlMutation('resume_workflow', workflowId, { expected_revision: expectedRevision }), [controlMutation]);
- const resetWorkflowAsync = useCallback((workflowId: string, expectedRevision: number) =>
- controlMutation('reset_workflow', workflowId, { expected_revision: expectedRevision }), [controlMutation]);
+ const resetWorkflowAsync = useCallback(async (workflowId: string, expectedRevision: number) => {
+ const result = await controlMutation(
+ 'reset_workflow', workflowId, { expected_revision: expectedRevision },
+ );
+ // The broadcast clears every connected tab; repeat locally so the
+ // initiating tab is correct even if a proxy suppresses self-echo.
+ useNodeStatusStore.getState().clearWorkflow(workflowId);
+ setAllVariables((previous) => {
+ const next = { ...previous };
+ delete next[workflowId];
+ return next;
+ });
+ if (useAppStore.getState().currentWorkflow?.id === workflowId) {
+ setConsoleLogs([]);
+ setChatMessages([]);
+ }
+ return result;
+ }, [controlMutation]);
const getWorkflowControlStatusAsync = useCallback(async (workflowId: string) => {
try {
diff --git a/docs-internal/frontend_architecture.md b/docs-internal/frontend_architecture.md
index 26fedf6b..603af932 100644
--- a/docs-internal/frontend_architecture.md
+++ b/docs-internal/frontend_architecture.md
@@ -396,6 +396,13 @@ See [ui_migration_plan.md](./ui_migration_plan.md) Phase 6.
The older `deployment_snapshot` and binary deployment status remain migration
adapters for legacy deployments and must not override a resolved controller
generation. See [Temporal Execution Engine RFC](temporal-execution-engine-rfc.md).
+- **Runtime Reset clears projections, not durable memory.** The
+ `workflow_runtime_reset` broadcast drops the workflow's node-status slot,
+ variables, and current console/chat arrays; `Dashboard` remounts the
+ parameter panel so local output reducers cannot leak the previous run.
+ Simple Memory node-parameter queries and `compactionStats` are intentionally
+ retained. Only the memory panel's explicit Clear Memory mutation may reset
+ those stores. See [Memory Lifecycle](memory_lifecycle.md#workflow-reset-is-not-memory-clear).
- **`currentWorkflowId` lives in `useAppStore` only.** Non-React listeners (WS handlers) read it via `useAppStore.getState().currentWorkflow?.id` -- the documented Zustand escape hatch (https://github.com/pmndrs/zustand#read-state-without-subscription). The previous `currentWorkflowIdRef` mirror inside WebSocketContext was a one-render-late copy that misrouted broadcasts during workflow switches. The push to `nodeStatusStore.setCurrentWorkflowId` is driven from a single `useEffect` in `Dashboard.tsx`.
## Ownership boundary: TanStack Query vs Zustand vs WebSocketContext
diff --git a/docs-internal/memory_lifecycle.md b/docs-internal/memory_lifecycle.md
index f722fb2f..c7d6aaad 100644
--- a/docs-internal/memory_lifecycle.md
+++ b/docs-internal/memory_lifecycle.md
@@ -96,6 +96,28 @@ Three stores are cleared in one call:
Frontend `clear_memory` WS handler ([`routers/websocket.py:2167`](../server/routers/websocket.py)) calls this; UI presents a Reset Memory button on the simpleMemory parameter panel.
+### Workflow Reset is not Memory Clear
+
+Temporal workflow **Reset** and the Simple Memory panel's **Clear Memory** are
+separate lifecycle operations:
+
+| Operation | `memory_content` / `memory_jsonl` | session metadata | vector cache | compaction/token state |
+|---|---|---|---|---|
+| Workflow Reset | preserved | preserved | preserved | preserved |
+| Clear Memory | reset | cleared | optional (`clear_long_term`) | cleared for the selected session |
+
+`workflow_runtime_reset` removes only the editor's disposable execution
+projection: node statuses and outputs, variables, and current console/chat
+views. It must not evict the Simple Memory query, compaction projection, or
+node-parameter row. The next Temporal generation reads the same durable
+`simpleMemory` parameters through `edge_walker._build_memory_entry`, so agent
+conversation continuity survives a workflow Reset.
+
+The archived workflow-generation snapshot may contain the memory-node values
+admitted for that historical run. That is an immutable historical copy, not a
+move of the authoritative memory row: `node_parameters` remains live and is
+still the source of truth for the next Start.
+
## 6. claude_code_agent native session resume bridge
`claude_code_agent` does **not** inject `memory_content` as a system prompt. It calls claude's CLI with `--resume <UUID>` so claude reads its own native JSONL transcript on disk (`<CLAUDE_CONFIG_DIR>/projects/<project_key>/<session_id>.jsonl`). The bridge has three coupled mechanisms; full plumbing in [cli_agent_framework.md → Memory bridge](./cli_agent_framework.md#memory-bridge--simplememory--claude_code_agent).
diff --git a/docs-internal/node-logic-flows/ai_agents/simpleMemory.md b/docs-internal/node-logic-flows/ai_agents/simpleMemory.md
index 0c440a68..35d36d1b 100644
--- a/docs-internal/node-logic-flows/ai_agents/simpleMemory.md
+++ b/docs-internal/node-logic-flows/ai_agents/simpleMemory.md
@@ -27,6 +27,24 @@ snapshot of the `services.memory_store` in-memory session, **which is a
separate store from the markdown `memory_content` pipeline used by the
agents**. See "Edge cases" below.
+## Workflow Reset semantics
+
+`simpleMemory` is durable workflow configuration and is deliberately excluded
+from generation cleanup. Resetting the Temporal workflow:
+
+- does not modify `memory_content`, `memory_jsonl`, `session_id`,
+ `last_session_id`, window settings, or long-term-memory settings;
+- does not evict the memory node's compaction/token projection in the UI;
+- does not clear either in-memory vector-store cache;
+- may retain an immutable copy of the admitted memory values in the archived
+ generation snapshot, while the authoritative `node_parameters` row remains
+ active;
+- makes the next workflow generation load the current memory-node parameters.
+
+Only the explicit **Clear Memory** action calls
+`clear_agent_session_state`. Workflow Reset must never call that service or
+simulate it by clearing memory-related frontend queries.
+
## Inputs (handles)
_None._ `simpleMemory` has no incoming handles.
diff --git a/docs-internal/temporal-execution-engine-rfc.md b/docs-internal/temporal-execution-engine-rfc.md
index 740088ab..922138ad 100644
--- a/docs-internal/temporal-execution-engine-rfc.md
+++ b/docs-internal/temporal-execution-engine-rfc.md
@@ -493,6 +493,33 @@ stateDiagram-v2
## 11. Persistence model
+### 11.1 Saved definition versus run-data scope
+
+`workflows` stores the editable, stable canvas definition. Runtime state is
+partitioned by `workflow_run_data_scopes`, with exactly one scope per workflow
+control generation. The scope snapshots all admitted node data, maps the
+application execution identity to the actual Temporal controller Workflow/Run
+identity, and supplies the session namespace used by node-output persistence.
+
+Reset archives the scope rather than deleting it. A later Start creates a new
+scope and therefore a fresh node-data namespace, while archived task, output,
+trace, and graph-snapshot records remain available for historical inspection.
+
+Durable configuration nodes are not owned by that disposable namespace.
+In particular, `simpleMemory` remains authoritative in `node_parameters`:
+workflow Reset preserves its transcript, JSONL/session metadata, long-term
+cache, and compaction/token state. The archived scope contains only the
+historical value admitted by that generation. The next Start reads the current
+memory row; memory is cleared only by the explicit Clear Memory contract.
+
+| State class | Reset behavior | Current source after Reset |
+|---|---|---|
+| Graph/node execution outputs and statuses | archived; live projection cleared | new generation scope |
+| Console and chat-trigger run rows | archived by root execution | new generation scope |
+| Team tasks, attempts, and traces | archived and queryable | selected execution history |
+| Node parameters and canvas configuration | preserved | `node_parameters` / `workflows` |
+| `simpleMemory` transcript and session state | preserved | `node_parameters` + memory services |
+
```mermaid
erDiagram
WORKFLOW_CONTROL_EXECUTION {
diff --git a/docs-internal/temporal-workflow-control.md b/docs-internal/temporal-workflow-control.md
index d217954a..6cbde13c 100644
--- a/docs-internal/temporal-workflow-control.md
+++ b/docs-internal/temporal-workflow-control.md
@@ -25,6 +25,41 @@ All mutations require an expected revision and support an idempotency key.
The toolbar and command palette derive available actions from the server's
`can_start`, `can_pause`, `can_resume`, and `can_reset` fields.
+## Generation-scoped workflow data
+
+The editable `workflows` row is the stable canvas definition; it is not runtime
+state. Every successful **Start** atomically creates a
+`workflow_run_data_scopes` row alongside the control generation. The scope:
+
+- has the same durable `execution_id` used by the controller and team records;
+- snapshots each node's type and complete `data` payload at admission time;
+- records the controller's actual Temporal Workflow ID and Run ID;
+- becomes the session namespace for node outputs, conversations, and other
+ session-keyed runtime records;
+- remains immutable as an execution snapshot while runtime records accumulate
+ under its scope ID.
+
+**Reset** terminates the generation and marks its data scope `archived`; it does
+not delete outputs, tasks, traces, or the graph snapshot. The toolbar returns to
+**Start**. The next Start creates a new generation, execution ID, Temporal
+controller, and empty runtime namespace from the then-current saved canvas.
+Historical scopes therefore remain queryable without leaking state into the
+new run.
+
+The editor's live projection follows the same boundary. Reset broadcasts
+`workflow_runtime_reset`, clears node statuses, variables, and console/chat
+projections, and remounts the parameter panel so local output
+reducers cannot retain results. Persisted console and chat rows carry the root
+execution ID; current-run reads filter by that ID while archived rows remain in
+the database. Node parameters are canvas configuration and intentionally remain
+unchanged across Reset.
+
+`simpleMemory` is explicitly outside the disposable generation projection.
+Its `memory_content`, JSONL, configured session, long-term store, and compaction
+statistics remain attached to the durable memory node across Reset. Resetting a
+workflow must not behave like the memory panel's explicit **Clear Memory**
+operation.
+
On server restart, active controlled deployments are excluded from the legacy
startup termination sweep. `TEMPORAL_TERMINATE_RUNNING_ON_STARTUP` defaults to
`false`; enabling it is intended only for legacy installations that explicitly
diff --git a/server/core/database.py b/server/core/database.py
index aa95154b..eb098f5c 100644
--- a/server/core/database.py
+++ b/server/core/database.py
@@ -109,6 +109,7 @@ class Database:
await self._migrate_user_settings()
await self._migrate_agent_teams()
await self._migrate_workflow_controls()
+ await self._migrate_generation_scoped_runtime_data()
logger.info("Database initialized successfully")
@@ -339,6 +340,7 @@ class Database:
additions = {
"controller_workflow_id": "VARCHAR(500)",
"controller_run_id": "VARCHAR(255)",
+ "data_scope_id": "VARCHAR(255)",
"resource_manifest": "JSON DEFAULT '{}'",
"terminal_reason": "VARCHAR(2000)",
"completed_at": "DATETIME",
@@ -348,9 +350,52 @@ class Database:
await conn.execute(text(f"ALTER TABLE workflow_control_executions ADD COLUMN {column} {definition}"))
await conn.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uq_workflow_control_generation ON workflow_control_executions(workflow_id, generation)"))
await conn.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uq_workflow_control_idempotency ON workflow_control_executions(workflow_id, idempotency_key)"))
+ await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_workflow_control_data_scope_id ON workflow_control_executions(data_scope_id)"))
+ # Preview releases already have durable generations but no
+ # explicit runtime-data namespace. Their execution identity is
+ # the only safe deterministic scope key.
+ await conn.execute(text(
+ "UPDATE workflow_control_executions "
+ "SET data_scope_id = execution_id WHERE data_scope_id IS NULL"
+ ))
+ await conn.execute(text("""
+ INSERT OR IGNORE INTO workflow_run_data_scopes (
+ id, control_id, workflow_id, generation, execution_id,
+ root_execution_id, temporal_workflow_id, temporal_run_id,
+ source_session_id, graph_hash, node_data, runtime_data,
+ status, created_at, archived_at
+ )
+ SELECT
+ execution_id, id, workflow_id, generation, execution_id,
+ root_execution_id, controller_workflow_id, controller_run_id,
+ session_id, graph_hash, '{}', '{}',
+ CASE WHEN status IN ('reset', 'failed') THEN 'archived' ELSE 'active' END,
+ created_at,
+ CASE WHEN status IN ('reset', 'failed') THEN completed_at ELSE NULL END
+ FROM workflow_control_executions
+ WHERE execution_id IS NOT NULL
+ """))
except Exception as exc:
logger.warning(f"Workflow-control migration check failed: {exc}")
+ async def _migrate_generation_scoped_runtime_data(self):
+ """Add nullable generation keys while preserving legacy history."""
+ try:
+ async with self.engine.begin() as conn:
+ for table_name in ("chat_messages", "console_logs"):
+ result = await conn.execute(text(f"PRAGMA table_info({table_name})"))
+ columns = {row[1] for row in result.fetchall()}
+ if columns and "execution_id" not in columns:
+ await conn.execute(text(
+ f"ALTER TABLE {table_name} ADD COLUMN execution_id VARCHAR(255)"
+ ))
+ await conn.execute(text(
+ f"CREATE INDEX IF NOT EXISTS ix_{table_name}_execution_id "
+ f"ON {table_name}(execution_id)"
+ ))
+ except Exception as exc:
+ logger.warning(f"Generation runtime-data migration check failed: {exc}")
+
async def run_runtime_mutation(
self,
*,
@@ -1086,11 +1131,17 @@ class Database:
# Chat Messages (Console Panel persistence)
# ============================================================================
- async def add_chat_message(self, session_id: str, role: str, message: str) -> bool:
+ async def add_chat_message(
+ self, session_id: str, role: str, message: str,
+ execution_id: Optional[str] = None,
+ ) -> bool:
"""Add a chat message to the console panel history."""
try:
async with self.get_session() as session:
- chat_msg = ChatMessage(session_id=session_id, role=role, message=message)
+ chat_msg = ChatMessage(
+ session_id=session_id, role=role, message=message,
+ execution_id=execution_id,
+ )
session.add(chat_msg)
await session.commit()
logger.debug(f"[Chat] Added {role} message to session '{session_id}'")
@@ -1100,11 +1151,16 @@ class Database:
logger.error("Failed to add chat message", session_id=session_id, error=str(e))
return False
- async def get_chat_messages(self, session_id: str, limit: Optional[int] = None) -> List[Dict[str, Any]]:
+ async def get_chat_messages(
+ self, session_id: str, limit: Optional[int] = None,
+ execution_id: Optional[str] = None,
+ ) -> List[Dict[str, Any]]:
"""Get chat messages for a session, optionally limited to last N."""
try:
async with self.get_session() as session:
stmt = select(ChatMessage).where(ChatMessage.session_id == session_id).order_by(ChatMessage.created_at.asc())
+ if execution_id is not None:
+ stmt = stmt.where(ChatMessage.execution_id == execution_id)
result = await session.execute(stmt)
messages = result.scalars().all()
@@ -1186,6 +1242,7 @@ class Database:
node_id=log_data.get("node_id", ""),
label=log_data.get("label", ""),
workflow_id=log_data.get("workflow_id"),
+ execution_id=log_data.get("execution_id"),
data=json.dumps(log_data.get("data", {})),
formatted=log_data.get("formatted", ""),
format=log_data.get("format", "text"),
@@ -1202,7 +1259,10 @@ class Database:
logger.error("Failed to add console log", error=str(e))
return False
- async def get_console_logs(self, limit: int = 100, workflow_id: Optional[str] = None) -> List[Dict[str, Any]]:
+ async def get_console_logs(
+ self, limit: int = 100, workflow_id: Optional[str] = None,
+ execution_id: Optional[str] = None,
+ ) -> List[Dict[str, Any]]:
"""Get console logs, optionally limited and scoped to a workflow.
``workflow_id=None`` returns the global stream (legacy behavior, used
@@ -1218,6 +1278,8 @@ class Database:
stmt = select(ConsoleLog).order_by(ConsoleLog.created_at.desc()).limit(limit)
if workflow_id is not None:
stmt = stmt.where(ConsoleLog.workflow_id == workflow_id)
+ if execution_id is not None:
+ stmt = stmt.where(ConsoleLog.execution_id == execution_id)
result = await session.execute(stmt)
logs = result.scalars().all()
@@ -1229,6 +1291,7 @@ class Database:
"node_id": log.node_id,
"label": log.label,
"workflow_id": log.workflow_id,
+ "execution_id": log.execution_id,
"data": json.loads(log.data) if log.data else {},
"formatted": log.formatted,
"format": log.format,
@@ -2445,12 +2508,68 @@ class Database:
return result.scalar_one_or_none()
async def create_workflow_control(self, control: WorkflowControlExecution) -> WorkflowControlExecution:
+ from models.database import WorkflowRunDataScope
+
async with self.get_session() as session:
session.add(control)
+ scope_id = control.data_scope_id or control.execution_id
+ control.data_scope_id = scope_id
+ session.add(WorkflowRunDataScope(
+ id=scope_id,
+ control_id=control.id,
+ workflow_id=control.workflow_id,
+ generation=control.generation,
+ execution_id=control.execution_id,
+ root_execution_id=control.root_execution_id,
+ temporal_workflow_id=control.controller_workflow_id,
+ temporal_run_id=control.controller_run_id,
+ source_session_id=control.session_id,
+ graph_hash=control.graph_hash,
+ node_data={
+ str(node.get("id")): {
+ "type": node.get("type"),
+ "data": node.get("data", {}),
+ }
+ for node in control.graph_snapshot.get("nodes", [])
+ if node.get("id")
+ },
+ ))
await session.commit()
await session.refresh(control)
return control
+ async def update_workflow_run_data_scope(
+ self, scope_id: str, *, temporal_run_id: Optional[str] = None,
+ status: Optional[str] = None, archived_at: Optional[datetime] = None,
+ ) -> bool:
+ """Update the execution link/lifecycle without mutating its node snapshot."""
+ from models.database import WorkflowRunDataScope
+
+ values: Dict[str, Any] = {}
+ if temporal_run_id is not None:
+ values["temporal_run_id"] = temporal_run_id
+ if status is not None:
+ values["status"] = status
+ if archived_at is not None:
+ values["archived_at"] = archived_at
+ if not values:
+ return True
+ async with self.get_session() as session:
+ result = await session.execute(
+ update(WorkflowRunDataScope).where(WorkflowRunDataScope.id == scope_id).values(**values)
+ )
+ await session.commit()
+ return result.rowcount == 1
+
+ async def get_workflow_run_data_scope(self, scope_id: str):
+ from models.database import WorkflowRunDataScope
+
+ async with self.get_session() as session:
+ result = await session.execute(
+ select(WorkflowRunDataScope).where(WorkflowRunDataScope.id == scope_id)
+ )
+ return result.scalar_one_or_none()
+
async def transition_workflow_control(
self, control_id: str, *, expected_revision: int, from_statuses: set[str], status: str,
values: Optional[Dict[str, Any]] = None,
diff --git a/server/models/database.py b/server/models/database.py
index 43f8f05e..88940715 100644
--- a/server/models/database.py
+++ b/server/models/database.py
@@ -196,6 +196,7 @@ class ChatMessage(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
session_id: str = Field(default="default", index=True, max_length=255)
+ execution_id: Optional[str] = Field(default=None, index=True, max_length=255)
role: str = Field(max_length=20) # 'user' or 'assistant'
message: str = Field(max_length=50000) # Large content support
created_at: datetime = Field(
@@ -216,6 +217,7 @@ class ConsoleLog(SQLModel, table=True):
node_id: str = Field(index=True, max_length=255) # Console node ID
label: str = Field(max_length=255) # User-defined label
workflow_id: Optional[str] = Field(default=None, max_length=255)
+ execution_id: Optional[str] = Field(default=None, index=True, max_length=255)
data: str = Field(max_length=100000) # JSON-encoded data
formatted: str = Field(max_length=100000) # Pre-formatted string
format: str = Field(default="text", max_length=20) # json, json_compact, text, table
@@ -471,6 +473,7 @@ class WorkflowControlExecution(SQLModel, table=True):
generation: int = Field(index=True)
execution_id: str = Field(index=True, max_length=255)
root_execution_id: str = Field(index=True, max_length=255)
+ data_scope_id: Optional[str] = Field(default=None, index=True, max_length=255)
controller_workflow_id: Optional[str] = Field(default=None, max_length=500)
controller_run_id: Optional[str] = Field(default=None, max_length=255)
session_id: str = Field(default="default", max_length=255)
@@ -486,6 +489,37 @@ class WorkflowControlExecution(SQLModel, table=True):
completed_at: Optional[datetime] = Field(default=None)
+class WorkflowRunDataScope(SQLModel, table=True):
+ """Immutable graph snapshot and isolated runtime namespace for one generation.
+
+ The saved :class:`Workflow` remains the editable canvas definition. Each
+ Start creates a new scope so node outputs, conversations, and other
+ session-keyed runtime data cannot bleed across Reset boundaries.
+ """
+
+ __tablename__ = "workflow_run_data_scopes"
+ __table_args__ = (
+ UniqueConstraint("workflow_id", "generation", name="uq_workflow_run_data_generation"),
+ UniqueConstraint("control_id", name="uq_workflow_run_data_control"),
+ )
+
+ id: str = Field(primary_key=True, max_length=255)
+ control_id: str = Field(index=True, max_length=255)
+ workflow_id: str = Field(index=True, max_length=255)
+ generation: int = Field(index=True)
+ execution_id: str = Field(index=True, max_length=255)
+ root_execution_id: str = Field(index=True, max_length=255)
+ temporal_workflow_id: Optional[str] = Field(default=None, max_length=500)
+ temporal_run_id: Optional[str] = Field(default=None, max_length=255)
+ source_session_id: str = Field(default="default", max_length=255)
+ graph_hash: str = Field(max_length=64)
+ node_data: Dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
+ runtime_data: Dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
+ status: str = Field(default="active", index=True, max_length=20)
+ created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
+ archived_at: Optional[datetime] = Field(default=None)
+
+
class TeamMember(SQLModel, table=True):
"""Agent team membership.
diff --git a/server/nodes/utility/console/__init__.py b/server/nodes/utility/console/__init__.py
index c9e26e9c..b8e48584 100644
--- a/server/nodes/utility/console/__init__.py
+++ b/server/nodes/utility/console/__init__.py
@@ -185,6 +185,7 @@ class ConsoleNode(ActionNode):
"formatted": formatted_output,
"format": format_type,
"workflow_id": ctx.workflow_id,
+ "execution_id": ctx.raw.get("root_execution_id") or ctx.execution_id,
"source_node_id": source_info.get("id") if source_info else None,
"source_node_type": source_info.get("type") if source_info else None,
"source_node_label": source_info.get("label") if source_info else None,
diff --git a/server/routers/websocket.py b/server/routers/websocket.py
index 467c7083..be2f6dfe 100644
--- a/server/routers/websocket.py
+++ b/server/routers/websocket.py
@@ -1058,7 +1058,15 @@ async def handle_send_chat_message(data: Dict[str, Any], websocket: WebSocket) -
# Save to database for persistence
database = container.database()
- await database.add_chat_message(session_id, role, message)
+ control = await database.get_latest_workflow_control(session_id)
+ execution_id = (
+ control.root_execution_id
+ if control is not None and control.status != "reset"
+ else None
+ )
+ await database.add_chat_message(
+ session_id, role, message, execution_id=execution_id,
+ )
# Build event data matching chatTrigger output schema
event_data = {"message": message, "timestamp": timestamp, "session_id": session_id}
@@ -1080,7 +1088,14 @@ async def handle_get_chat_messages(data: Dict[str, Any], websocket: WebSocket) -
limit = data.get("limit") # Optional limit
database = container.database()
- messages = await database.get_chat_messages(session_id, limit)
+ control = await database.get_latest_workflow_control(session_id)
+ if control is not None and control.status == "reset":
+ messages = []
+ else:
+ messages = await database.get_chat_messages(
+ session_id, limit,
+ execution_id=control.root_execution_id if control is not None else None,
+ )
return {"success": True, "messages": messages, "session_id": session_id}
@@ -1103,7 +1118,14 @@ async def handle_get_console_logs(data: Dict[str, Any], websocket: WebSocket) ->
workflow_id = data.get("workflow_id")
database = container.database()
- logs = await database.get_console_logs(limit, workflow_id=workflow_id)
+ control = await database.get_latest_workflow_control(workflow_id) if workflow_id else None
+ if control is not None and control.status == "reset":
+ logs = []
+ else:
+ logs = await database.get_console_logs(
+ limit, workflow_id=workflow_id,
+ execution_id=control.root_execution_id if control is not None else None,
+ )
return {
"success": True,
@@ -1161,7 +1183,15 @@ async def handle_save_chat_message(data: Dict[str, Any], websocket: WebSocket) -
session_id = data.get("session_id", "default")
database = container.database()
- success = await database.add_chat_message(session_id, role, message)
+ control = await database.get_latest_workflow_control(session_id)
+ success = await database.add_chat_message(
+ session_id, role, message,
+ execution_id=(
+ control.root_execution_id
+ if control is not None and control.status != "reset"
+ else None
+ ),
+ )
return {"success": success, "message": "Chat message saved" if success else "Failed to save chat message"}
diff --git a/server/services/deployment/control.py b/server/services/deployment/control.py
index 1bf3a479..e24b14a0 100644
--- a/server/services/deployment/control.py
+++ b/server/services/deployment/control.py
@@ -32,6 +32,7 @@ def serialize_control(control: Optional[WorkflowControlExecution]) -> Dict[str,
"generation": control.generation,
"execution_id": control.execution_id,
"root_execution_id": control.root_execution_id,
+ "data_scope_id": control.data_scope_id or control.execution_id,
"controller_workflow_id": control.controller_workflow_id,
"controller_run_id": control.controller_run_id,
"state": state,
@@ -68,6 +69,7 @@ class WorkflowControlService:
control = WorkflowControlExecution(
id=f"workflow-control:{workflow_id}:{generation}", workflow_id=workflow_id,
generation=generation, execution_id=execution_id, root_execution_id=execution_id,
+ data_scope_id=execution_id,
controller_workflow_id=f"workflow-control-{workflow_id}-g{generation}",
session_id=session_id, graph_hash=_graph_hash(nodes, edges),
graph_snapshot={"nodes": nodes, "edges": edges}, idempotency_key=idempotency_key,
diff --git a/server/services/deployment/handlers.py b/server/services/deployment/handlers.py
index 89f72203..e8125edb 100644
--- a/server/services/deployment/handlers.py
+++ b/server/services/deployment/handlers.py
@@ -389,7 +389,14 @@ async def _start_controller(control) -> Optional[str]:
return None
handle = await wrapper.client.start_workflow(
"WorkflowControlWorkflow",
- args=[{"generation": control.generation, "state": "running"}],
+ args=[{
+ "workflow_id": control.workflow_id,
+ "generation": control.generation,
+ "execution_id": control.execution_id,
+ "root_execution_id": control.root_execution_id,
+ "data_scope_id": control.data_scope_id or control.execution_id,
+ "state": "running",
+ }],
id=control.controller_workflow_id,
task_queue="machina-tasks",
search_attributes=TypedSearchAttributes([
@@ -536,7 +543,19 @@ async def handle_start_workflow(data: Dict[str, Any], websocket: WebSocket) -> D
control, expected_revision=control.revision, from_statuses={"starting"}, status="starting",
values={"controller_run_id": run_id},
)
- deployed = await handle_deploy_workflow(data, websocket)
+ await service.database.update_workflow_run_data_scope(
+ control.data_scope_id or control.execution_id, temporal_run_id=run_id,
+ )
+ # Runtime persistence is generation-scoped. The caller's session is
+ # retained on the scope for provenance, but must never namespace node
+ # outputs for a controlled run.
+ deploy_data = {
+ **data,
+ "session_id": control.data_scope_id or control.execution_id,
+ "execution_id": control.execution_id,
+ "root_execution_id": control.root_execution_id,
+ }
+ deployed = await handle_deploy_workflow(deploy_data, websocket)
if not deployed.get("success"):
await service.fail(control, str(deployed.get("error", "deployment_failed")))
return deployed
@@ -621,6 +640,17 @@ async def handle_reset_workflow(data: Dict[str, Any], websocket: WebSocket) -> D
current, expected_revision=current.revision, from_statuses={"resetting"}, status="reset",
values={"terminal_reason": "workflow_reset", "completed_at": datetime.now(timezone.utc)},
)
+ await service.database.update_workflow_run_data_scope(
+ current.data_scope_id or current.execution_id,
+ status="archived", archived_at=datetime.now(timezone.utc),
+ )
+ from services.status_broadcaster import get_status_broadcaster
+ await get_status_broadcaster().broadcast({
+ "type": "workflow_runtime_reset",
+ "workflow_id": workflow_id,
+ "generation": current.generation,
+ "data_scope_id": current.data_scope_id or current.execution_id,
+ })
return await _with_runtime_counts(
{"success": True, "terminated_executions": terminated, **serialize_control(current)},
workflow_id,
diff --git a/server/services/deployment/manager.py b/server/services/deployment/manager.py
index 5e2f8d8c..bb486d86 100644
--- a/server/services/deployment/manager.py
+++ b/server/services/deployment/manager.py
@@ -790,6 +790,11 @@ class DeploymentManager:
if control is not None and control.controller_workflow_id and control.status in {
"starting", "running", "pausing", "paused", "resuming",
}:
+ listener_args.update({
+ "execution_id": control.execution_id,
+ "root_execution_id": control.root_execution_id,
+ "data_scope_id": control.data_scope_id or control.execution_id,
+ })
# The controller owns listeners as child workflows. This keeps
# trigger activity in the deployment's Temporal execution tree
# instead of presenting each listener as an unrelated root trace.
diff --git a/server/services/temporal/trigger_listener_workflow.py b/server/services/temporal/trigger_listener_workflow.py
index d0375bfc..ce9a394a 100644
--- a/server/services/temporal/trigger_listener_workflow.py
+++ b/server/services/temporal/trigger_listener_workflow.py
@@ -169,7 +169,9 @@ class TriggerListenerWorkflow:
use_latest_graph = bool(workflow_id) and workflow.patched("trigger-latest-graph-v1")
except RuntimeError: # direct unit invocation outside Temporal runtime
use_latest_graph = False
- if use_latest_graph:
+ # Controlled generations execute their immutable admitted snapshot.
+ # Legacy deployments retain hot graph lookup for compatibility.
+ if use_latest_graph and not listener_data.get("data_scope_id"):
try:
latest = await workflow.execute_activity(
"load_persisted_workflow_graph_activity",
@@ -238,6 +240,9 @@ class TriggerListenerWorkflow:
"workflow_id": workflow_id,
"workflow_slug": workflow_slug,
"tenant_id": tenant_id,
+ "execution_id": listener_data.get("execution_id"),
+ "root_execution_id": listener_data.get("root_execution_id"),
+ "data_scope_id": listener_data.get("data_scope_id"),
}
],
id=child_id,
diff --git a/server/tests/services/test_workflow_control.py b/server/tests/services/test_workflow_control.py
index 6a4f8e39..79f5bf78 100644
--- a/server/tests/services/test_workflow_control.py
+++ b/server/tests/services/test_workflow_control.py
@@ -1,3 +1,8 @@
+import importlib.util
+import sys
+import uuid
+from pathlib import Path
+from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
@@ -6,6 +11,31 @@ from sqlalchemy.exc import IntegrityError
from services.deployment.control import WorkflowControlService, serialize_control
+async def control_database():
+ module_name = "tests._real_workflow_control_database"
+ spec = importlib.util.spec_from_file_location(
+ module_name, Path(__file__).resolve().parents[2] / "core" / "database.py"
+ )
+ assert spec and spec.loader
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[module_name] = module
+ spec.loader.exec_module(module)
+ db_path = Path.cwd() / f".workflow-control-{uuid.uuid4().hex}.db"
+ database = module.Database(SimpleNamespace(
+ database_url=f"sqlite+aiosqlite:///{db_path.as_posix()}",
+ database_echo=False, database_pool_size=5, database_max_overflow=5,
+ ))
+ await database.startup()
+ try:
+ yield database
+ finally:
+ await database.shutdown()
+ sys.modules.pop(module_name, None)
+ for candidate in (db_path, Path(f"{db_path}-wal"), Path(f"{db_path}-shm")):
+ candidate.unlink(missing_ok=True)
+
+
@pytest.mark.asyncio
async def test_begin_generation_is_idempotent():
db = AsyncMock()
@@ -21,9 +51,55 @@ async def test_begin_generation_is_idempotent():
assert created is True
assert control.generation == 1
assert control.status == "starting"
+ assert control.data_scope_id == control.execution_id
+ assert serialize_control(control)["data_scope_id"] == control.execution_id
assert serialize_control(control)["can_pause"] is False
+async def test_generation_atomically_creates_and_archives_isolated_data_scope(control_database):
+ service = WorkflowControlService(control_database)
+ control, created = await service.begin_generation(
+ workflow_id="wf-scope",
+ nodes=[{"id": "agent-1", "type": "aiAgent", "data": {"label": "Researcher"}}],
+ edges=[], session_id="browser-session", idempotency_key="scope-1",
+ )
+ assert created is True
+ scope = await control_database.get_workflow_run_data_scope(control.data_scope_id)
+ assert scope.execution_id == control.execution_id
+ assert scope.source_session_id == "browser-session"
+ assert scope.node_data["agent-1"]["data"]["label"] == "Researcher"
+ assert scope.status == "active"
+
+ await control_database.update_workflow_run_data_scope(
+ scope.id, temporal_run_id="temporal-run-1", status="archived",
+ )
+ archived = await control_database.get_workflow_run_data_scope(scope.id)
+ assert archived.temporal_workflow_id == control.controller_workflow_id
+ assert archived.temporal_run_id == "temporal-run-1"
+ assert archived.status == "archived"
+
+ await control_database.add_chat_message("wf-scope", "user", "archived", execution_id=scope.id)
+ await control_database.add_chat_message("wf-scope", "user", "new", execution_id="new-scope")
+ current_chat = await control_database.get_chat_messages(
+ "wf-scope", execution_id="new-scope",
+ )
+ assert [item["message"] for item in current_chat] == ["new"]
+
+ await control_database.add_console_log({
+ "node_id": "console-1", "label": "Console", "workflow_id": "wf-scope",
+ "execution_id": scope.id, "data": {"value": "archived"}, "formatted": "archived",
+ })
+ await control_database.add_console_log({
+ "node_id": "console-1", "label": "Console", "workflow_id": "wf-scope",
+ "execution_id": "new-scope", "data": {"value": "new"}, "formatted": "new",
+ })
+ current_logs = await control_database.get_console_logs(
+ workflow_id="wf-scope", execution_id="new-scope",
+ )
+ assert [item["formatted"] for item in current_logs] == ["new"]
+
+
@pytest.mark.asyncio
async def test_concurrent_insert_rereads_idempotent_winner():
db = AsyncMock()
@@ -62,6 +138,7 @@ async def test_reset_generation_is_ready_for_explicit_start():
reset_control.revision = 9
reset_control.execution_id = "old-execution"
reset_control.root_execution_id = "old-execution"
+ reset_control.data_scope_id = "old-execution"
reset_control.controller_workflow_id = "old-controller"
reset_control.controller_run_id = "old-run"
reset_control.created_at = None