Early Stopping

How to early stop workflows

Workflows support early termination when specific conditions are met, preventing unnecessary processing and implementing safety gates. Any step can trigger early termination by returning StepOutput(stop=True), immediately halting the entire workflow execution.

Workflows early stop diagram

Example

1from kern.workflow import Step, Workflow, StepInput, StepOutput
2
3def security_gate(step_input: StepInput) -> StepOutput:
4 """Security gate that stops deployment if vulnerabilities found"""
5 security_result = step_input.previous_step_content or ""
6
7 if "VULNERABLE" in security_result.upper():
8 return StepOutput(
9 content="SECURITY ALERT: Critical vulnerabilities detected. Deployment blocked.",
10 stop=True # Stop the entire workflow
11 )
12 else:
13 return StepOutput(
14 content="Security check passed. Proceeding with deployment...",
15 stop=False
16 )
17
18# Secure deployment pipeline
19workflow = Workflow(
20 name="Secure Deployment Pipeline",
21 steps=[
22 Step(name="Security Scan", agent=security_scanner),
23 Step(name="Security Gate", executor=security_gate), # May stop here
24 Step(name="Deploy Code", agent=code_deployer), # Only if secure
25 Step(name="Setup Monitoring", agent=monitoring_agent), # Only if deployed
26 ]
27)
28
29# Test with vulnerable code - workflow stops at security gate
30workflow.print_response("Scan this code: exec(input('Enter command: '))")

Developer Resources