PromptBase
PromptBase SelectGet Select
Close icon
General
Home
Marketplace
Create
Hire
Login
Chat
Sell
Explore

Prompt Details

Model
(claude-5-5-sonnet)
Token size
4,837
Example input
[Order fulfillment]: E-commerce order fulfillment [Validate, reserve, charge, ship]: Validate order → Reserve inventory → Charge payment → Ship order [Services]: Order Service, Inventory Service, Payment Gateway, Shipping Service [Bounded retries]: Maximum 3 attempts for eligible retries; exponential backoff; escalate unresolved financial or shipment conflicts
Example output
### 1\. 🔍 Workflow Requirement Reconstruction ### Business objective Fulfill the order exactly once from the customer's perspective, without duplicate charges, unauthorized refunds, or incorrect inventory release. Confirmed test inputs * Order validation and inventory reservation precede payment. * Payment and shipping can create external side effects. * Recovery must use bounded retries. Assumptions for this test * The payment provider offers some means of checking a payment by a stable reference. This capability must be confirmed before implementation. * A refund may be supported, but its API, status semantics, and timing are not yet confirmed. * Inventory reservation has an expiration or release mechanism that must be verified. * Shipment has not been initiated when the timeout occurs. Unresolved questions * Does the provider support idempotency keys? * Can payment status be queried reliably? * When does a reservation expire? * Is shipment cancellation supported after dispatch? These are design assumptions, not verified capabilities of any particular provider. ### 2\. 🧩 Workflow Graph and Dependencies ```mermaid flowchart TD A["Validate Order"] --> B["Reserve Inventory"] B --> C["Charge Payment"] C --> D{"Payment Outcome Known?"} D -->|Yes: Success| E["Create Shipment"] D -->|Yes: Failed| F["Release Reservation"] D -->|No: Timeout| G["Reconcile Payment"] G --> H{"Verified Payment State"} H -->|Succeeded| E H -->|Failed| F H -->|Still Unknown| I["Hold Order and Escalate"] E --> J["Verify Shipment"] J --> K["Complete Order"] ``` Dependency rules * Payment must be confirmed successful before shipment creation. * An uncertain payment must be reconciled before another charge is attempted. * Inventory must remain reserved while a payment outcome is being investigated, subject to reservation expiry safeguards. * Shipment creation must not be duplicated if a prior attempt has an uncertain outcome. * A confirmed payment failure permits reservation release only after confirming no successful payment exists and the release is safe. Checkpoint boundaries: Order validated, inventory reserved, payment reconciled, shipment verified, order completed. ### 3\. 💥 Failure Classification Matrix | Failure | Actual state | Required response | | ------------------------------------------------------------- | ----------------------------------------------- | ------------------------------------------- | | Payment request rejected before processing | Confirmed failure, if evidence proves no charge | Release reservation if safe | | Payment succeeds but response times out | Unknown locally | Reconcile provider status | | Payment request times out before provider processing is known | Unknown | Query status; do not blindly retry | | Worker crashes after payment succeeds | External success, local state may be stale | Recover from operation journal and provider | | Duplicate payment event arrives | Potential duplicate notification | Deduplicate and reconcile | | Shipment request times out | Shipment outcome unknown | Check shipment status before retry | | Refund request times out | Refund outcome unknown | Reconcile refund before retrying | | Inventory release fails | Compensation failed or unknown | Preserve recovery state and retry safely | Evidence required: Provider transaction references, authoritative status responses, durable operation records, event identifiers, and resource versions. A missing response alone never proves failure. ### 4\. 💳 Side-Effect and Reversibility Inventory | Step | Effect | Classification | Recovery candidate | | ----------------- | -------------------------------- | --------------------------------------------------- | ----------------------------------------------------------------------- | | Validate | Checks order data | Pure computation, assuming no writes | Re-run validation | | Reserve inventory | Reduces available stock | Conditionally compensatable | Release reservation | | Charge payment | Transfers or captures funds | Externally compensatable, subject to provider rules | Refund or void where supported | | Ship order | Transfers goods into fulfillment | Potentially irreversible | Cancel before dispatch if supported; otherwise operational intervention | Important distinction: A refund is a new financial transaction, not necessarily a reversal of the original charge. Likewise, a shipment cancellation may not recover goods already dispatched. ### 5\. ↩️ Compensation Contract Catalog ### C-01: Release Inventory Reservation * Purpose: Return reserved stock to available inventory. * Preconditions: Reservation belongs to the order; no shipment depends on it; payment state permits release; reservation has not already been released. * Action: Invoke the verified inventory release operation. * Authorization: Workflow recovery policy. * Idempotency: Use a stable reservation-release operation key if supported. * Verification: Authoritative inventory service confirms the reservation is released. * Failure handling: Reconcile reservation status before retrying. * Mode: Automatic only when all preconditions are satisfied. ### C-02: Refund Confirmed Payment * Purpose: Correct a confirmed charge when the order cannot or should not be fulfilled. * Preconditions: Original payment is confirmed successful; refund is permitted; refund amount and prior refunds are known; no conflicting financial operation exists. * Action: Use the provider's verified refund mechanism. * Authorization: Automated policy within approved refund limits; human approval when policy requires it. * Idempotency: Stable refund-operation key and original payment reference. * Verification: Provider confirms the refund's final status and amount. * Failure handling: If the response is uncertain, reconcile the refund before retrying. * Mode: Conditional; approval-gated where required. ### C-03: Cancel Shipment * Purpose: Prevent fulfillment when cancellation is still operationally possible. * Preconditions: Shipment belongs to the order; current carrier status is known; cancellation is allowed; no newer fulfillment instruction conflicts. * Action: Use a verified cancellation mechanism if available. * Verification: Carrier or shipping service confirms cancellation. * Failure handling: If dispatched or cancellation is unsupported, stop automatic compensation and escalate. * Mode: Conditional or manual. No shipment cancellation, refund, or inventory API is assumed to exist until verified. ### 6\. 🔗 Recovery Sequence Matrix | Failure scenario | Known state | Recovery order | Validation | Terminal decision | | ---------------------------- | ------------------------------------- | ------------------------------------------------------------------------ | ---------------------------- | ----------------------- | | Payment timeout | Inventory reserved; charge unknown | Freeze shipment → reconcile payment | Provider status and amount | Resume or hold | | Payment confirmed successful | Charge exists; shipment absent | Record payment success → create shipment | Shipment reference | Complete if verified | | Payment confirmed failed | No charge; inventory reserved | Confirm no charge → release reservation | Reservation released | Cancel order | | Duplicate charge detected | More than one charge may exist | Freeze fulfillment → reconcile all charges → refund excess if authorized | Provider confirms correction | Escalate if unresolved | | Shipment outcome unknown | Payment succeeded; shipment uncertain | Query shipment status → retry only if safe | Shipment status | Complete or escalate | | Refund outcome unknown | Charge exists; refund uncertain | Reconcile refund → retry only if safe | Final refund status | Compensated or escalate | The compensation order is determined by the actual state and dependencies, not by automatically reversing every step. ### 7\. 🛡️ Workflow State Machine ```mermaid stateDiagram-v2 [*] --> CREATED CREATED --> RUNNING RUNNING --> PARTIALLY_COMPLETED PARTIALLY_COMPLETED --> RECONCILING RECONCILING --> RESUMABLE: State verified RECONCILING --> RECOVERY_REQUIRED: Corrective action needed RECONCILING --> MANUAL_REVIEW: State remains unknown RESUMABLE --> RUNNING RECOVERY_REQUIRED --> COMPENSATING COMPENSATING --> COMPENSATED: All required corrections verified COMPENSATING --> COMPENSATION_FAILED: Retry budget exhausted COMPENSATION_FAILED --> RECONCILING: Controlled recovery attempt MANUAL_REVIEW --> RESUMABLE: Authorized resolution MANUAL_REVIEW --> ABANDONED: Approved terminal decision RUNNING --> COMPLETED: Fulfillment verified ``` ### Durable transition rules Each transition must: * Check the current state and version. * Persist the new state and audit event. * Record the operation result and evidence. * Prevent competing recovery workers from making conflicting updates. * Preserve sufficient information to recover after a worker crash. `ABANDONED` must not mean that an external payment or shipment has magically been reversed. Outstanding external obligations must remain visible. ### 8\. 🔄 Retry, Resume and Reconciliation Policy | Error class | Policy | | ------------------------------ | ------------------------------------------------------ | | Temporary network error | Retry safe reads with backoff and jitter | | Rate limit | Respect provider guidance and retry deadline | | Authentication failure | Stop and repair credentials; do not retry indefinitely | | Invalid payment request | Stop; correct request or escalate | | Unknown charge outcome | Reconcile before retry | | Confirmed payment decline | Do not repeat unchanged request blindly | | Refund timeout | Reconcile refund status first | | Inventory release timeout | Check reservation status before retry | | Shipment cancellation conflict | Stop automatic compensation and escalate | Example retry budget: Maximum 3 attempts for eligible operations, including an initial attempt, with exponential backoff and jitter. Apply an overall deadline and respect provider rate limits. The budget is a proposed configuration, not a universal safe default. Unknown high-impact outcomes must enter reconciliation rather than consume repeated blind retries. ### 9\. ⚖️ Business Invariants and Safety Controls | Invariant | Enforcement | Verification | | --------------------------------------------- | --------------------------------------------------------------- | ---------------------------- | | No shipment before payment confirmation | Workflow transition guard | Payment and shipment records | | No duplicate charge for one logical payment | Stable operation key and provider deduplication where supported | Charge ledger | | Refund must reference a real charge | Financial compensation precondition | Provider transaction details | | Inventory release must match its reservation | Reservation ownership and version check | Inventory service | | Shipment must not be falsely marked cancelled | Require authoritative cancellation evidence | Carrier status | | Recovery must not overwrite newer state | Version checks and conflict detection | Audit history | If an invariant cannot be verified, preserve the order in a recovery or manual-review state rather than declaring success. ### 10\. 🚨 Compensation Failure and Escalation | Failure | Safe response | | ------------------------------------- | --------------------------------------------------------------- | | Refund service unavailable | Preserve payment and recovery records; retry within budget | | Refund succeeds but response is lost | Reconcile refund; do not issue another blindly | | Inventory reservation already changed | Re-read current reservation and check ownership | | Shipment already dispatched | Stop automatic cancellation; escalate to fulfillment operations | | Payment and local ledger disagree | Keep order blocked from unsafe progress; reconcile | | Authorization expires | Pause compensation until authorization is restored | | All retries exhausted | Move to COMPENSATION_FAILED or MANUAL_REVIEW | Escalation must include the order ID, confirmed charges, uncertain operations, attempted compensations, and the exact unresolved business decision. ### 11\. 🧪 Recovery Testing and Acceptance Plan The following are planned tests, not executed test results. | Test ID | Injected failure | Expected behavior | Acceptance criteria | | ------- | ----------------------------------- | ----------------------------------------------- | -------------------------------------- | | T01 | Validation fails | No reservation, charge, or shipment | No external side effects | | T02 | Reservation succeeds, payment fails | Release reservation safely | Release verified | | T03 | Charge succeeds, response times out | Reconcile payment | No duplicate charge | | T04 | Worker crashes after charge | Resume from durable records | Charge discovered without repeating it | | T05 | Duplicate payment event | Deduplicate event | One logical payment outcome | | T06 | Refund request times out | Reconcile refund | No duplicate refund | | T07 | Shipment response is lost | Query shipment status | No duplicate shipment | | T08 | Inventory release fails | Bounded retry, then escalation | No false compensated state | | T09 | Two recovery workers compete | Concurrency control prevents conflicting writes | One valid state transition at a time | | T10 | Payment status remains unknown | Hold fulfillment and escalate | No blind re-charge or shipment | | T11 | Shipment already dispatched | Block unsafe cancellation | Shipment state remains truthful | | T12 | Worker restarts during recovery | Reload journal and resume safely | No lost compensation intent | Release-blocking failure: Any test that permits duplicate high-impact financial operations, shipment without confirmed payment, or a false `COMPENSATED` status. ### 12\. 📡 Observability and Audit Design Record these fields for each workflow event: * `workflow_id` * `order_id` * `operation_id` * `compensation_id` * `attempt_number` * `idempotency_reference` * `provider_transaction_reference` * `previous_state` * `new_state` * `outcome_classification` * `reconciliation_result` * `verification_evidence` * `timestamp` * `escalation_reason` ### Metrics and alerts * Payment outcomes unknown beyond the configured deadline. * Duplicate-charge detections. * Compensation failure rate. * Orders stuck in recovery. * Inventory reservations not reconciled. * Shipments with uncertain status. * Manual-review queue age. Never place payment credentials, secrets, or unnecessary customer personal data in logs. Define alert thresholds and retention periods based on operational requirements and applicable policy. ### 13\. 🏗️ Architecture Decisions and Trade-Offs | Pattern | Recommendation | Reason | | ----------------------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------ | | Saga orchestration | Recommended | Central controller can coordinate order, inventory, payment, and shipping recovery | | Saga choreography | Alternative | Useful when services already coordinate through domain events; harder to inspect global recovery state | | Database transaction | Use locally | Protects related local records, but cannot atomically cover unrelated external services | | Transactional outbox | Conditional | Useful when local state updates and event publication must remain consistent | | Inbox and deduplication | Recommended where events are used | Helps prevent duplicate event processing | | Durable workflow engine | Optional | Consider if recovery complexity and long-running workflows justify it | | Reconciliation jobs | Recommended | Detects differences between local records and authoritative providers | | Human-in-the-loop | Required for unresolved high-impact conflicts | Prevents unsafe automated decisions | Recommended design: A durable saga orchestrator with an operation journal, idempotent command handling, provider reconciliation, bounded retries, and explicit manual-review states. This is a design recommendation, not a claim that the system already implements these mechanisms. ### 14\. 🗺️ Implementation Task Graph TASK-01: Durable workflow state Dependency: None Store workflow state, version, operation journal, checkpoints, and audit history. Verify crash recovery and optimistic concurrency. TASK-02: Idempotency and deduplication Dependency: TASK-01 Define stable operation keys, uniqueness constraints, duplicate-event handling, and provider-specific guarantees. TASK-03: Compensation contracts Dependency: TASK-01, TASK-02 Implement verified inventory-release, refund, and shipment-recovery contracts where supported. TASK-04: Reconciliation and recovery worker Dependency: TASK-02, TASK-03 Classify uncertain outcomes, resume safe steps, enforce retry budgets, and escalate unresolved conflicts. TASK-05: Failure-injection tests Dependency: TASK-01 through TASK-04 Run T01-T12 in a controlled environment and retain evidence for each acceptance criterion. TASK-06: Monitoring and release controls Dependency: TASK-05 Add recovery alerts, audit review, operational procedures, and a staged deployment with rollback safeguards. ### 15\. ⚠️ Risk Register and Open Questions | Risk | Severity | Required decision | | -------------------------------------------- | -------------- | --------------------------------------------------------------- | | Payment outcome cannot be queried | Critical | Establish a reliable provider reconciliation path | | Provider lacks idempotent charge requests | Critical | Design duplicate-charge detection and safe operational recovery | | Refund capability or permissions are unclear | High | Confirm supported refund operations and authorization | | Reservation expires during recovery | High | Define reservation renewal or controlled expiry behavior | | Shipment cancellation is unavailable | High | Define a manual fulfillment recovery process | | Local state and provider state diverge | High | Establish authoritative source-of-truth rules | | Two workers execute recovery concurrently | High | Implement leases, fencing or equivalent concurrency controls | | Audit records are incomplete | Medium to high | Enforce durable event recording and retention policy | Do not approve production deployment until critical unknowns have a documented, tested resolution. ### 16\. 🚀 Final Recovery Playbook 1\. FAILURE DETECTED Payment API times out. Persist the timeout and stop shipment creation. 2\. CLASSIFY ACTUAL STATE Mark the payment outcome `UNKNOWN`, not `FAILED`. 3\. RECONCILE UNCERTAIN OPERATIONS Query the authoritative payment record using the original payment reference. 4\. SELECT SAFE RECOVERY * If payment succeeded: record the confirmed result and resume shipment. * If payment definitively failed: release inventory if safe. * If payment remains unknown: preserve the reservation where possible and escalate. 5\. EXECUTE COMPENSATION OR FORWARD RECOVERY Execute only the action permitted by the verified state, authorization policy, and dependency graph. 6\. VALIDATE BUSINESS INVARIANTS Confirm payment amount and status, inventory reservation state, and shipment state. 7\. RESUME, COMPLETE, STOP, OR ESCALATE Mark `COMPLETED` only when fulfillment is verified. Mark `COMPENSATED` only when every required compensation is verified. Otherwise preserve the recovery state or escalate. ### ✅ Implementation Priority * Build durable workflow state and operation journaling. * Verify provider status-query and idempotency capabilities. * Implement payment reconciliation before payment retries. * Define and test inventory, refund, and shipment compensation contracts. * Add bounded recovery, concurrency controls, and manual escalation. * Execute all critical failure-injection tests before production release. Final verdict: This sample defines a proposed recovery architecture and a test plan. It does not establish that the implementation is production-ready. Production release is blocked until uncertain payment outcomes, duplicate side effects, compensation failures, crash recovery, and human escalation have been demonstrated to work in controlled tests.
Claude

Agent Workflow Compensation Logic Design

Add to Cart
Instant accessInstant access
Usage rightsCommercial use
Money-back guaranteeMoney‑back
By purchasing this prompt, you agree to our terms of service
CLAUDE-5-5-SONNET
Tested icon
Guide icon
4 examples icon
Free credits icon
The Agent Workflow Compensation Logic Designer helps developers design explicit compensating actions and recovery sequences for partially completed workflows. 🧩 Map workflow steps, dependencies, and side effects. 🔍 Identify completed, failed, and uncertain operations. ↩️ Design compensating actions for completed steps. 🔗 Build forward and reverse dependency sequences. 🛡️ Prevent unsafe retries and duplic Turn partial workflow failures into controlled, verifiable recovery plans—not guesswork.
...more
Added 1 week ago
Report
Browse Marketplace