Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
State Machine - Finite State Machines for Complex Flows
Description
Enforces deterministic, minimal state machines for any multi-step flow in NodeJS-Starter-V1. Codifies the project's existing patterns (TaskStatus, ExecutionStatus, NodeStatus) and provides a reusable framework for defining states, transition maps, guard conditions, and side effects across Python and TypeScript.
When to Apply
Positive Triggers
Designing multi-step workflows with distinct status phases
Adding new Enum status fields to Pydantic models
Implementing UI components with loading/error/success states
Reviewing state transitions for completeness and determinism
Adding retry, escalation, or verification loops
User mentions: "state machine", "status", "workflow state", "transitions", "FSM"
Implementing form validation (use data-validation instead)
Designing animation state transitions (use scientific-luxury + Framer Motion)
Classifying error types without state transitions (use error-taxonomy instead)
Core Directives
The Three Laws of State Machines
Deterministic: Every (state, event) pair produces exactly one next state
Minimal: No unreachable states, no duplicate transitions
Complete: Every state has defined exit transitions or is a terminal state
State Definition Convention
Use str, Enum for all status fields. String enums serialise cleanly to JSON and are readable in logs.
from enum import Enum
classOrderStatus(str, Enum):
"""Status of an order — each value is a distinct FSM state."""
DRAFT = "draft"
SUBMITTED = "submitted"
PROCESSING = "processing"
COMPLETED = "completed"
CANCELLED = "cancelled"
= {
: ,
: ,
: ,
: ,
: ,
} ;
= ( )[keyof ];
// Frontend mirror — use const assertion, not enum
const
ORDER_STATUS
DRAFT
'draft'
SUBMITTED
'submitted'
PROCESSING
'processing'
COMPLETED
'completed'
CANCELLED
'cancelled'
as
const
type
OrderStatus
typeof
ORDER_STATUS
typeof
ORDER_STATUS
Existing Project State Machines
TaskStatus (Orchestrator)
Location: apps/backend/src/agents/orchestrator.py
10 states governing the task lifecycle with verification loop:
List every distinct status. Apply the Turing Check — no redundant states:
classMyStatus(str, Enum):
"""Each state must be reachable and have at least one exit or be terminal."""
STATE_A = "state_a"
STATE_B = "state_b"
STATE_C = "state_c"# Terminal
Only add guards when a transition depends on runtime data.
Step 4: Add Side Effects (If Needed)
Logging, metrics, notifications on transition.
Step 5: Validate Completeness
defvalidate_fsm(transitions: dict[Enum, set[Enum]]) -> list[str]:
"""Validate a finite state machine for completeness."""
issues: list[str] = []
all_states = set(transitions.keys())
all_targets = {t for targets in transitions.values() for t in targets}
# Check for unreachable states (not targets and not initial)
initial = list(transitions.keys())[0]
unreachable = all_states - all_targets - {initial}
for state in unreachable:
issues.append(f"Unreachable state: {state.value}")
# Check for undefined targets
undefined = all_targets - all_states
for state in undefined:
issues.append(f"Undefined target state: {state.value}")
# Check terminal states have no transitionsfor state, targets in transitions.items():
iflen(targets) == 0:
continue# Valid terminal# Non-terminal must have at least one reachable targetifnot targets.intersection(all_states):
issues.append(f"Dead end: {state.value} targets undefined states")
return issues
Anti-Patterns
Anti-Pattern
Why It Fails
Correct Approach
String literals for status (status = "running")
No type safety, typos cause bugs
str, Enum with exhaustive matching
Boolean flags (is_running, is_complete)
Impossible states (is_running=True, is_complete=True)
Single status enum
Implicit transitions (set status anywhere)
Race conditions, invalid states
Transition map with validation
God enum (20+ states)
Unmanageable complexity
Decompose into hierarchical machines
Missing terminal states
Processes hang forever
Every machine needs at least one terminal
Checklist for New State Machines
Design
All states enumerated as str, Enum
Transition map defines every (state, target) pair
Terminal states have empty transition sets
No unreachable states
No undefined target states
Guards documented for conditional transitions
Implementation
validate_transition() called before every status change
Side effects fire after transition, not before
Logging includes from_status and to_status
Frontend mirrors backend states exactly (snake_case)
Council of Logic
Turing: Transition lookup is O(1) via dict/set
Von Neumann: State changes are atomic — no partial transitions
Shannon: Enum values are minimal — no redundant states
Response Format
[AGENT_ACTIVATED]: State Machine
[PHASE]: {Design | Implementation | Review}
[STATUS]: {in_progress | complete}
{state machine analysis or implementation guidance}
[NEXT_ACTION]: {what to do next}
Integration Points
Council of Logic (Turing Check)
State machines must be deterministic and minimal
Transition lookup must be O(1) — use dict, not if/elif chains