Skip to content

Self-improving AI Employees for everyone

8.4 KB
Raw Blame
"""Stable admission and reviewed delivery for owner-submitted team jobs."""
from datetime import timedelta
import asyncio
from typing import Any
from temporalio import workflow, activity
from temporalio.common import RetryPolicy
from temporalio.exceptions import is_cancelled_exception
from services.temporal._exits import is_workflow_bug
from services.temporal._retry_policies import REVIEWED_DELIVERY_RETRY
from services.temporal.execution_control import ExecutionControl


@activity.defn(name="employee.job.deliver")
async def deliver_employee_job(context: dict[str, Any]) -> dict:
    from core.container import container
    from services.employees.jobs import deliver_job
    from services.plugin import NodeContext
    ctx = NodeContext.from_legacy(context["node_id"], "employeeJob", context)
    task = asyncio.create_task(deliver_job(container.database(), ctx, lead_node_id=context["node_id"]))
    try:
        while not task.done():
            done, _ = await asyncio.wait({task}, timeout=30)
            if not done:
                activity.heartbeat("Waiting for reviewed delivery")
        return await task
    finally:
        if not task.done():
            task.cancel()
            await asyncio.gather(task, return_exceptions=True)


@activity.defn(name="employee.job.failed")
async def fail_employee_job(context: dict[str, Any]) -> dict:
    from core.container import container
    from models.employees import EmployeeJob
    from services.employees.jobs import fail_no_assignments, no_assignments_failed, protected_job_outcome, job_has_approval, _now
    if context.get("failure_code") == "lead_no_assignments":
        return await fail_no_assignments(container.database(), context)
    changed = False
    async with container.database().reserved_session() as session:
        job = await session.get(EmployeeJob, str(context.get("employee_job_id")))
        if job is None and context.get("runtime_admission"):
            from sqlmodel import select
            found = await session.execute(select(EmployeeJob).where(EmployeeJob.workflow_id == context.get("workflow_id"),
                EmployeeJob.lead_node_id == context.get("node_id"), EmployeeJob.origin_execution_id == context.get("execution_id")))
            candidates = list(found.scalars())
            # Admission may commit before prepare returns its trusted id.
            # Concurrent emails share a graph execution, so recorded lead
            # evidence must match even when only the other email has admitted.
            if activity.in_activity() and any((candidate.source.get("runtime") or {}).get("lead_workflow_id") for candidate in candidates):
                info = activity.info()
                matches = [candidate for candidate in candidates if
                    (candidate.source.get("runtime") or {}).get("namespace") == info.workflow_namespace
                    and (candidate.source.get("runtime") or {}).get("lead_workflow_id") == info.workflow_id
                    and (candidate.source.get("runtime") or {}).get("lead_initial_run_id") == info.workflow_run_id]
                job = matches[0] if len(matches) == 1 else None
            elif len(candidates) == 1:
                job = candidates[0]  # Unambiguous legacy admission without run evidence.
            if candidates and job is None:
                return {"saved": False, "reason": "ambiguous_admission"}
        if job and not no_assignments_failed(job) and not protected_job_outcome(job) and not await job_has_approval(session, job):
            job.state = "cancelled" if context.get("cancelled") else "failed"
            job.updated_at = _now()
            await session.commit()
            changed = True
    if changed:
        from services.employees.events import employee_changed_now
        employee_changed_now(job.workflow_id)
    return {"saved": changed}


@workflow.defn(name="EmployeeJobWorkflow", sandboxed=False)
class EmployeeJobWorkflow:
    @workflow.init
    def __init__(self, context: dict[str, Any] | None = None):
        self._execution_control = ExecutionControl()
        if context is not None:
            self._execution_control.bind(context)

    @workflow.update
    async def set_control_state(self, payload: dict) -> dict:
        return await self._execution_control.set_control_state(payload)

    @set_control_state.validator
    def validate_set_control_state(self, payload: dict) -> None:
        self._execution_control.validate_control(payload)

    @workflow.update
    async def wait_for_checkpoint(self, payload: dict) -> dict:
        return await self._execution_control.wait_for_checkpoint(payload)

    @wait_for_checkpoint.validator
    def validate_wait_for_checkpoint(self, payload: dict) -> None:
        self._execution_control.validate_control(payload)

    @workflow.query
    def execution_control_status(self) -> dict:
        return self._execution_control.status()

    @workflow.run
    async def run(self, context: dict[str, Any] | None = None) -> dict:
        context = context or {}
        self._execution_control.bind(context)
        if not self._execution_control.enabled:
            return await self._run_job(context)
        await self._execution_control.register_root()
        bug = False
        try:
            return await self._run_job(context)
        except Exception as exc:
            bug = is_workflow_bug(exc)
            raise
        finally:
            # A bug fails the task and records nothing on the way out (_exits).
            if not bug:
                await self._execution_control.unregister_root()
                await workflow.wait_condition(workflow.all_handlers_finished)

    async def _run_job(self, context: dict[str, Any]) -> dict:
        from services.coding_agent_runtime import agent_workflow_name
        child_name = agent_workflow_name(context.get("node_type", ""), context)
        try:
            if self._execution_control.enabled:
                async with self._execution_control.child_start():
                    handle = await workflow.start_child_workflow(child_name, context, id=workflow.info().workflow_id + ":lead")
                result = await handle
            else:
                result = await workflow.execute_child_workflow(child_name, context, id=workflow.info().workflow_id + ":lead")
        except BaseException as exc:
            if is_workflow_bug(exc):
                raise
            # Cancellation must leave a durable terminal job record too.
            # Shield this short cleanup from the parent's cancellation.
            await asyncio.shield(workflow.execute_activity("employee.job.failed", {**context, "cancelled": isinstance(exc, asyncio.CancelledError) or is_cancelled_exception(exc)},
                start_to_close_timeout=timedelta(seconds=60), retry_policy=RetryPolicy(maximum_attempts=3)))
            raise
        if not result.get("success"):
            await workflow.execute_activity("employee.job.failed", context,
                start_to_close_timeout=timedelta(seconds=60), retry_policy=RetryPolicy(maximum_attempts=3))
            return result
        delivery = {**context, "outputs": {**context.get("outputs", {}), context["node_id"]: result.get("result", {})}}
        if self._execution_control.enabled:
            async with self._execution_control.action():
                return await workflow.execute_activity("employee.job.deliver", delivery,
                    start_to_close_timeout=timedelta(days=31), heartbeat_timeout=timedelta(minutes=2),
                    retry_policy=REVIEWED_DELIVERY_RETRY)
        return await workflow.execute_activity("employee.job.deliver", delivery,
            start_to_close_timeout=timedelta(days=31), heartbeat_timeout=timedelta(minutes=2),
            retry_policy=REVIEWED_DELIVERY_RETRY)


@activity.defn(name="employee.job.resolve_delivery")
async def resolve_employee_job_delivery(request: dict[str, Any]) -> dict:
    from core.container import container
    from services.employees.delivery_resolution import continue_delivery
    return await continue_delivery(container.database(), request["job_id"], request["resolution_id"])


@workflow.defn(name="EmployeeJobDeliveryWorkflow", sandboxed=False)
class EmployeeJobDeliveryWorkflow:
    """Stable continuation after the owner reviews an uncertain send."""

    @workflow.run
    async def run(self, request: dict[str, Any]) -> dict:
        return await workflow.execute_activity("employee.job.resolve_delivery", request,
            start_to_close_timeout=timedelta(days=31), heartbeat_timeout=timedelta(minutes=2),
            retry_policy=REVIEWED_DELIVERY_RETRY)