Inspiration

A payment timeout does not mean a payment failed.

In agentic commerce, blindly retrying after a lost response can charge a customer twice. The opposite problem is also possible: payment and merchant work may succeed, but the result never reaches the buyer. These failures require evidence from several systems—not another unverified retry.

REWIND was built to answer one question safely:

What actually happened across payment, fulfillment, and delivery?

What REWIND does

REWIND gives every commerce operation a durable task identity spanning:

  • PayPal payment state
  • Merchant execution
  • Buyer delivery
  • Recovery actions

It builds an evidence timeline, reconciles authoritative PayPal state, uses AI to analyze the evidence and cite relevant events, and then applies deterministic recovery policy.

AI investigates and explains. It does not control money. Only verified provider state and policy can authorize a redelivery, refund, or manual review.

Recovery scenarios

Scenario Failure Safe recovery
Capture response lost PayPal completes the capture, but the response disappears Reconcile the existing capture and block another payment attempt
Result delivery lost Payment and merchant work succeed, but the buyer receives nothing Redeliver the original SHA-256-bound artifact without charging again
True merchant failure Payment succeeds, but the merchant produces no artifact Refund the verified capture exactly once

PayPal integration

REWIND uses the PayPal Sandbox developer platform for:

  • OAuth authentication
  • Orders v2 creation and buyer approval
  • Payment capture
  • Authoritative order and capture retrieval
  • Payments v2 refunds
  • Stable PayPal request IDs for API idempotency

For the capture-response-loss scenario, a socket fault proxy forwards the real Sandbox capture request, records the successful upstream HTTP 201 response, and closes the downstream connection before REWIND receives it.

REWIND then reconciles PayPal and recovers the original capture. It never creates another order or charge for that task.

Meaningful AI integration

The AI is an evidence investigator rather than a chatbot wrapper.

It receives structured payment, merchant, and delivery events and returns schema-validated JSON containing:

  • Root-cause classification
  • Recommended recovery action
  • Confidence
  • Contradictions
  • Citations to existing evidence IDs
  • A human-readable explanation

The engine rejects fabricated citations, invalid confidence values, and inconsistent classifications. New untrusted evidence requires a fresh diagnosis.

The unchanged six-case live evaluation passed 6/6 with Ollama 0.40.10 and qwen3:8b, covering:

  • Recovered capture
  • Delivery gap
  • Terminal merchant failure
  • Contradictory evidence
  • Prompt injection
  • Pending payment

How we built it

REWIND uses:

  • Python and FastAPI
  • PayPal Sandbox Orders v2 and Payments v2
  • Ollama with qwen3:8b
  • Pydantic structured-output validation
  • SQLite task and operation journals
  • HTTPX for service and provider communication
  • Separate merchant and buyer-inbox HTTP services
  • A socket-level capture fault proxy
  • HTML, CSS, and JavaScript investigation dashboard
  • Pytest and GitHub Actions

The dashboard presents the payment state, merchant state, delivery proof, event journal, cited AI diagnosis, and final deterministic recovery decision as one investigation.

Challenges

The hardest failure was the most dangerous one: PayPal successfully captured the payment, but the client never received the response.

The correct recovery was not another capture. We persisted the task and request identity, queried PayPal, validated the amount, currency, and task binding, and blocked duplicate payment intent.

Another challenge was reliable small-model evidence analysis. We separated raw AI findings from engine decisions, tightened the evidence taxonomy, required citations, and tested unchanged cases before using the model in Sandbox recovery.

Accomplishments

  • 57 automated tests passing
  • 6/6 live AI evaluation cases passing
  • Three complete PayPal Sandbox recovery scenarios
  • Exactly one capture in every verified Sandbox scenario
  • Provider-confirmed refund in the true-failure scenario
  • Original-artifact redelivery with a matching buyer receipt
  • Duplicate payment attempts blocked after an ambiguous capture
  • Credential-free, redacted, read-only evidence replay
  • GitHub Actions passing on Python 3.11 and 3.12

What we learned

Provider idempotency is necessary, but it protects an API request—not the complete business task.

Safe recovery needs a durable identity connecting payment, fulfillment, and delivery. AI is valuable for interpreting messy and conflicting evidence, while deterministic code must retain authority over financial actions.

What's next

Production development would add:

  • Distributed operation leases and an outbox
  • Strong multi-user authorization
  • External merchant authentication and fulfillment attestation
  • Broader live webhook and restart validation
  • Monitoring and deployment hardening

Scope disclosure

The orders, captures, and refund shown in the verification evidence are real PayPal Sandbox objects.

Merchant execution, buyer delivery, and injected network failures are controlled local simulations. REWIND does not claim production readiness, live-money validation, or automatic dispute filing.

Built With

Share this project:

Updates

Submission history