Skip to content

Self-improving AI Employees for everyone

10.2 KB
Raw Blame
"""Temporal executor for OpenCompany workflow execution.

Provides the same interface as WorkflowExecutor but delegates
execution to Temporal for durable workflow orchestration.
"""

import time
import uuid
from datetime import datetime
from typing import Any, Dict, List, Optional, Callable

from temporalio.client import Client
from temporalio.common import (
    SearchAttributeKey,
    SearchAttributePair,
    TypedSearchAttributes,
)

from core.logging import get_logger
from .workflow import (
    MachinaWorkflow,
    TEMPORAL_ROUTING_INPUT_KEY,
    TEMPORAL_ROUTING_INPUT_VERSION,
)

logger = get_logger(__name__)


def capture_temporal_routing_input() -> Dict[str, Any]:
    """Capture command-shaping flags before entering workflow execution."""
    from core.config import Settings

    settings = Settings()
    return {
        TEMPORAL_ROUTING_INPUT_KEY: {
            "version": TEMPORAL_ROUTING_INPUT_VERSION,
            "agent_workflow_enabled": bool(
                getattr(settings, "temporal_agent_workflow_enabled", False)
            ),
            "per_type_dispatch_enabled": bool(
                getattr(settings, "temporal_per_type_dispatch", False)
            ),
            "worker_pool_enabled": bool(
                getattr(settings, "temporal_worker_pool_enabled", False)
            ),
            "pi_harness_version": 1,
        }
    }


async def capture_browser_routing_input(workflow_id: str, nodes: list[dict], user_id: str) -> dict:
    """Resolve distributed Browser destinations before recording workflow input."""
    from core.config import Settings
    if not getattr(Settings(), "distributed_mode", False):
        return {}
    from core.container import container
    from services.browser_owners import routing_for_node
    bindings = {}
    for node in nodes:
        if node.get("type") == "browser" and not (node.get("data") or {}).get("disabled"):
            bindings[node["id"]] = await routing_for_node(container.database(), workflow_id, node["id"], user_id)
    return {"browser_routing_version": 1, "browser_bindings": bindings}


class TemporalExecutor:
    """Workflow executor that uses Temporal for durable execution.

    Provides a compatible interface with the existing WorkflowExecutor
    so it can be used as a drop-in replacement when Temporal is enabled.
    """

    def __init__(
        self,
        client: Client,
        task_queue: str = "machina-tasks",
        status_callback: Optional[Callable] = None,
    ):
        """Initialize the Temporal executor.

        Args:
            client: Connected Temporal client
            task_queue: Temporal task queue name
            status_callback: Optional callback for node status updates
        """
        self.client = client
        self.task_queue = task_queue
        self.status_callback = status_callback

    async def execute_workflow(
        self,
        workflow_id: str,
        nodes: List[Dict],
        edges: List[Dict],
        session_id: str = "default",
        enable_caching: bool = True,
        workflow_slug: Optional[str] = None,
        graph_version: int = 0,
        generation: int = 0,
        user_id: str = "owner",
        parameter_snapshot: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        """Execute a workflow using Temporal.

        Args:
            workflow_id: Stable UUID system identity (FK target).
            nodes: List of node definitions
            edges: List of edge definitions
            session_id: Session identifier
            enable_caching: Whether to enable result caching (passed to activity)
            workflow_slug: Human-readable slug used in the Temporal Web UI
                listing prefix. Optional — falls back to ``"workflow"`` for
                one-off Runs without a saved DB row.
            graph_version: Normalized graph version for the Context topology
            generation: Durable workflow-control generation for the Context store
            user_id: Authenticated server-owned user identity

        Returns:
            Dict with success, outputs, execution_trace, and timing info
        """
        start_time = time.time()
        # Temporal start ID surfaces in the Web UI. Format:
        # ``<workflow_slug>-<uuid8>`` — workflow name leads, short uuid
        # disambiguates per-run uniqueness. No middle kind tag — the
        # Temporal Web UI's "Workflow Type" column already shows
        # MachinaWorkflow.
        slug_prefix = workflow_slug or "workflow"
        execution_id = f"{slug_prefix}-{uuid.uuid4().hex[:8]}"

        logger.info(
            "Starting Temporal workflow execution",
            workflow_id=workflow_id,
            workflow_slug=workflow_slug,
            execution_id=execution_id,
            node_count=len(nodes),
            edge_count=len(edges),
        )

        try:
            # Execute workflow via Temporal
            workflow_payload = {
                "nodes": nodes,
                "edges": edges,
                "session_id": session_id,
                "workflow_id": workflow_id,
                "workflow_slug": workflow_slug,
                "execution_id": execution_id,
                "user_id": str(user_id or "owner"),
                **capture_temporal_routing_input(),
                **await capture_browser_routing_input(workflow_id, nodes, str(user_id or "owner")),
            }
            if parameter_snapshot:
                workflow_payload["parameter_snapshot"] = parameter_snapshot
            admitted_generation = False
            if int(graph_version or 0) >= 2 and int(generation or 0) > 0:
                workflow_payload.update(
                    {
                        "graphVersion": int(graph_version),
                        "generation": int(generation),
                        "root_execution_id": execution_id,
                    }
                )
                from core.container import container
                control = await container.database().get_latest_workflow_control(workflow_id)
                if control is not None and control.generation == int(generation):
                    admitted_generation = True
                    from services.coding_agent_runtime import freeze_generation_harness
                    freeze_generation_harness(workflow_payload, control.resource_manifest)
                    workflow_payload.update({
                        "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,
                        "execution_control_version": int((control.resource_manifest or {}).get("execution_control_version", 0)),
                    })

            from services.coding_agent_runtime import assert_pi_admissions_enabled, enabled_coding_agents
            if enabled_coding_agents(nodes) and not admitted_generation:
                assert_pi_admissions_enabled()
                from services.coding_agent_runtime import freeze_pi_local_control
                freeze_pi_local_control(workflow_payload)

            result = await self.client.execute_workflow(
                MachinaWorkflow.run,
                workflow_payload,
                id=execution_id,
                task_queue=self.task_queue,
                search_attributes=TypedSearchAttributes(
                    [
                        SearchAttributePair(
                            SearchAttributeKey.for_keyword("EventWorkflowId"),
                            workflow_id,
                        )
                    ]
                ),
            )

            execution_time = time.time() - start_time

            # Notify status callback for completed nodes
            if self.status_callback and result.get("success"):
                for node_id in result.get("execution_trace", []):
                    try:
                        await self.status_callback(
                            node_id,
                            "completed",
                            result.get("outputs", {}).get(node_id, {}),
                        )
                    except Exception as e:
                        logger.warning(f"Status callback error for node {node_id}: {e}")

            logger.info(
                "Temporal workflow completed",
                workflow_id=workflow_id,
                execution_id=execution_id,
                success=result.get("success"),
                nodes_executed=len(result.get("execution_trace", [])),
                execution_time=execution_time,
            )

            # MachinaWorkflow reports node failures as ``errors``; only an
            # empty graph comes back with a top-level ``error``.
            errors = result.get("errors") or ([{"error": result["error"]}] if result.get("error") else [])
            return {
                "success": result.get("success", False),
                "execution_id": execution_id,
                "nodes_executed": result.get("execution_trace", []),
                "outputs": result.get("outputs", {}),
                "errors": errors,
                "error": errors[0].get("error") if errors else None,
                "total_nodes": result.get("total_nodes", 0),
                "completed_nodes": len(result.get("execution_trace", [])),
                "execution_time": execution_time,
                "temporal_execution": True,
                "timestamp": datetime.now().isoformat(),
            }

        except Exception as e:
            import traceback

            execution_time = time.time() - start_time
            error_details = f"{type(e).__name__}: {str(e)}"
            tb = traceback.format_exc()
            logger.error(
                f"Temporal workflow failed: {error_details}",
                workflow_id=workflow_id,
                execution_id=execution_id,
                traceback=tb,
            )

            return {
                "success": False,
                "execution_id": execution_id,
                "nodes_executed": [],
                "outputs": {},
                "errors": [{"error": error_details}],
                "error": error_details,
                "execution_time": execution_time,
                "temporal_execution": True,
                "timestamp": datetime.now().isoformat(),
            }