Spaces:
Running

WorkPilot-AI / core /workflows /models.py
Aynkhn's picture
Deploy WorkPilot demo
fd5dd94 verified
Raw History Blame Contribute Delete
7.73 kB
"""Workflow tables (migration 0008). All tenant-scoped with FORCE row-level security.
* ``workflows``: one row per automation (``key`` is the DSL ``id``), pointing at the active
version, if any.
* ``workflow_versions``: immutable definitions (a trigger rejects changes to ``definition``),
with their lifecycle status, static-check report and last dry run.
* ``workflow_triggers``: event subscriptions of active versions (``event_type`` -> workflow).
* ``workflow_proposals``: self-healing suggestions after a failed step, for the owner.
* ``workflow_waits``: runs paused by a ``wait`` step; a due wait becomes a continuation event.
"""
from __future__ import annotations
import uuid
from datetime import datetime
from sqlalchemy import (
Boolean,
DateTime,
ForeignKey,
Integer,
String,
Text,
UniqueConstraint,
func,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
from core.db.base import Base, TenantMixin, TimestampMixin, new_id
# draft -> dry_run_ok | dry_run_failed -> approved -> active -> superseded | rolled_back
VERSION_STATUSES = (
"draft",
"dry_run_ok",
"dry_run_failed",
"approved",
"active",
"superseded",
"rolled_back",
)
class Workflow(TenantMixin, TimestampMixin, Base):
__tablename__ = "workflows"
__table_args__ = (UniqueConstraint("tenant_id", "key", name="uq_workflows_tenant_key"),)
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
key: Mapped[str] = mapped_column(String(63), nullable=False)
name: Mapped[str] = mapped_column(String(120), nullable=False)
description: Mapped[str] = mapped_column(Text, nullable=False, default="")
# draft (never activated) | active | paused | archived
status: Mapped[str] = mapped_column(String(20), nullable=False, default="draft")
active_version: Mapped[int | None] = mapped_column(Integer, nullable=True)
source: Mapped[str] = mapped_column(String(20), nullable=False, default="json")
template: Mapped[str | None] = mapped_column(String(200), nullable=True)
created_by: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
class WorkflowVersion(TenantMixin, TimestampMixin, Base):
__tablename__ = "workflow_versions"
__table_args__ = (
UniqueConstraint("workflow_id", "version", name="uq_workflow_versions_workflow_version"),
)
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
workflow_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("workflows.id", ondelete="CASCADE"), nullable=False
)
version: Mapped[int] = mapped_column(Integer, nullable=False)
definition: Mapped[dict] = mapped_column(JSONB, nullable=False) # immutable
definition_hash: Mapped[str] = mapped_column(String(64), nullable=False)
nl_source: Mapped[str | None] = mapped_column(Text, nullable=True)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="draft")
checks: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
dry_run: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
dry_run_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)
dry_run_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
created_by: Mapped[str] = mapped_column(String(200), nullable=False) # user:<id> | system:*
approved_by: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
approved_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
activated_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
deactivated_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
parent_version: Mapped[int | None] = mapped_column(Integer, nullable=True)
note: Mapped[str | None] = mapped_column(Text, nullable=True)
class WorkflowTrigger(TenantMixin, TimestampMixin, Base):
"""Event subscription of an active version (owner sweeps may read it: owner_admin)."""
__tablename__ = "workflow_triggers"
__table_args__ = (UniqueConstraint("workflow_id", name="uq_workflow_triggers_workflow_id"),)
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
workflow_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("workflows.id", ondelete="CASCADE"), nullable=False
)
version: Mapped[int] = mapped_column(Integer, nullable=False)
event_type: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
conditions: Mapped[list] = mapped_column(JSONB, nullable=False, default=list)
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
class WorkflowProposal(TenantMixin, TimestampMixin, Base):
__tablename__ = "workflow_proposals"
__table_args__ = (UniqueConstraint("run_id", "step_id", name="uq_workflow_proposals_run_step"),)
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
workflow_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("workflows.id", ondelete="CASCADE"), nullable=False
)
version: Mapped[int] = mapped_column(Integer, nullable=False)
run_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False)
step_id: Mapped[str] = mapped_column(String(40), nullable=False)
failure_class: Mapped[str] = mapped_column(String(20), nullable=False) # auth|schema|rate|data
error: Mapped[str] = mapped_column(Text, nullable=False)
summary: Mapped[str] = mapped_column(Text, nullable=False)
patch: Mapped[list] = mapped_column(JSONB, nullable=False, default=list) # JSON Patch ops
# pending | auto_applied | applied | dismissed
status: Mapped[str] = mapped_column(String(20), nullable=False, default="pending")
applied_version: Mapped[int | None] = mapped_column(Integer, nullable=True)
decided_by: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
decided_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
class WorkflowWait(TenantMixin, TimestampMixin, Base):
"""A run paused at a ``wait`` step. When due, the schedule sweep (owner role) records a
``workflow.run`` continuation event that resumes after the wait with the saved outputs."""
__tablename__ = "workflow_waits"
__table_args__ = (UniqueConstraint("run_id", "step_id", name="uq_workflow_waits_run_step"),)
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
workflow_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("workflows.id", ondelete="CASCADE"), nullable=False
)
version: Mapped[int] = mapped_column(Integer, nullable=False)
run_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False)
step_id: Mapped[str] = mapped_column(String(40), nullable=False)
resume_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, index=True)
outputs: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
event: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="waiting")