""" Example: API Workflow Verification Demonstrates verifying a complex API orchestration plan with resource conflicts, dependency chains, and mandatory steps. """ import sys import os sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from qsvaps import ( Plan, PlanAction, PlanConstraint, ResourceConstraint, ConstraintType, PlanVerifier, QSVAPSVisualizer, ) def main(): """Build and verify an API orchestration plan.""" # ── Define the plan ────────────────────────────────────────────────── plan = Plan( name="Multi-API Data Aggregation", actions=[ PlanAction( name="auth_service_a", description="Authenticate with Service A", resources=["auth_tokens"], can_fail=True, ), PlanAction( name="auth_service_b", description="Authenticate with Service B", resources=["auth_tokens"], can_fail=True, ), PlanAction( name="fetch_users", description="Fetch user records from Service A", resources=["bandwidth"], can_fail=True, ), PlanAction( name="fetch_analytics", description="Fetch analytics from Service B", resources=["bandwidth"], can_fail=True, ), PlanAction( name="transform", description="Transform and join datasets", resources=["compute"], can_fail=True, ), PlanAction( name="validate", description="Validate output schema", can_fail=False, # Must succeed ), PlanAction( name="store_results", description="Store results in database", resources=["db_connection"], can_fail=False, # Must succeed ), ], dependencies=[ ("auth_service_a", "fetch_users"), ("auth_service_b", "fetch_analytics"), ("fetch_users", "transform"), ("fetch_analytics", "transform"), ("transform", "validate"), ("validate", "store_results"), ], resource_constraints=[ ResourceConstraint("auth_tokens", max_concurrent=1), ResourceConstraint("bandwidth", max_concurrent=1), ], custom_constraints=[ PlanConstraint( expression=f"x0 or x1", # At least one auth must work description="At least one authentication must succeed", constraint_type=ConstraintType.CUSTOM, variables_involved=["s_auth_service_a", "s_auth_service_b"], ), ], ) # ── Verify ─────────────────────────────────────────────────────────── viz = QSVAPSVisualizer() print(viz.draw_plan(plan)) verifier = PlanVerifier(shots=4096) result = verifier.verify(plan, verbose=True) print(viz.format_result(result)) # ── Print summary ──────────────────────────────────────────────────── if not result.is_safe: print( f"\n⚠️ This plan has {result.num_violations} potential " f"failure modes out of {result.total_states:,} possible " f"execution states." ) print( f" Grover's algorithm found them in " f"{result.grover_iterations} iteration(s) " f"(vs ~{result.total_states} classical checks)." ) else: print("\n✅ Plan is safe under all modeled scenarios.") if __name__ == "__main__": main()