All projects
Project8 min read

APO: Autonomous Payment Orchestrator

A payment-workflow prototype that checks AI proposals before execution. The demo tests failures and repeated requests with simulated payments.

Context
An AI assistant can propose the wrong payment. The application has to check the proposal and authorize it before execution.
My contribution
I separated AI payment proposals from authorization and designed the retry rules. To check for duplicate captures, I replayed a simulated payment ten times and verified that both ledger entry IDs stayed the same.
Result
The cloud demo completed a simulated payment and ten sequential repeats with the same payment and ledger IDs. Records survived a VM restart.

Payment execution rules

Payment authorization

The application checks the proposal and requires authorization. The model can only propose a payment.

Duplicate requests

Repeating the same key and payload returns the stored result. Reusing the key with a different payload causes a conflict.

Retry timing

Inngest schedules retries. Services report outcomes without adding their own timed retry loops.

How a payment runs
  1. Request & proposalThe model turns a request into a payment proposal.
  2. Policy & authorizationThe application checks the proposal and authorizes execution.
  3. Durable workflowInngest coordinates steps and retries.
  4. Payment & ledgerPayment and ledger records show what was captured and posted.

The public demo uses simulated payments and mock AI responses. The services make HTTP calls, run Inngest workflows, and write to PostgreSQL.

Why I built it

A payment assistant can give a convincing answer and still propose the wrong amount. I built APO to test what happens next: how the application approves a proposal, recovers from failed calls, and handles the same request twice.

A natural-language request becomes a structured proposal. An independent policy checks it before authorized execution reaches the durable workflow, payment core, and double-entry ledger.

The application controls payment authorization and selects credentials separately from the model. The model can propose an action; it cannot authorize a payment.

The browser demo uses a deterministic mock for AI responses and a simulated payment provider. HTTP calls between services, PostgreSQL persistence, Inngest execution, policy checks, idempotency, and ledger writes are real. APO is an engineering prototype, not a service for paying real invoices.

Open the live demo to try the four scenarios below.

Four ways to test the system

Each scenario introduces a failure or repeats a completed action. You can inspect the backend records to see what happened.

Experiment What happens Evidence to inspect
Repeat the same payment Complete a $500 payment, then explicitly repeat capture and ledger posting ten times, sequentially. Captured amount stays at $500. Payment ID and both ledger entry IDs stay unchanged. Ten receipts show the returned results.
Recover from temporary failures The simulator fails the first authorize and first capture calls. Inngest retries the steps. Failed attempts followed by successful calls, a confirmed $500 capture, and the ledger pair.
Reject the assistant’s amount The source says $500; the mock proposes $5,000. The policy rejects the proposal with amount_not_grounded. No workflow starts.
Handle a permanent decline Both source and proposal specify $600; the simulator declines authorization. A failed payment record and capturedAmount = 0 when the payment snapshot is available.

Starting a prepared scenario explicitly authorizes that simulated payment. The $500 and $600 amounts meet the manual-confirmation threshold, so that authorization is required by the policy.

The repeat experiment becomes available after the original payment is confirmed. Its ten calls run sequentially; this is not a concurrent-load test. Reloading the evidence only sends GET requests and cannot initiate another payment.

Services and payment records

Services communicate over HTTP. Inside each package, domain and application code depend on ports, with network clients and SDKs in adapters. The workflow follows the request from proposal and policy checks through authorization, payment execution, and ledger posting.

Package Responsibility
agent-orchestrator Intents, model proposals, policy checks, human confirmation, and ownership checks. Determines whether execution may begin.
durable-ledger Inngest steps, retries, compensation, double-entry posting, and observation journaling. Ledger records establish whether posting happened.
pay-core Payment state, transactions, versions, idempotency, and atomic state/outbox writes. Its simulator snapshot establishes the captured amount.
orchestra Gateway, browser lab, CLI, and restrictions on optional live-model access. Presents facts from the other services.
agent-evals Hostile scenarios, generated cases, effect assertions, and reports. Runs outside the application runtime.

Inngest owns workflow status. I kept that lifecycle out of a second application status table.

Retries and duplicate payments

Duplicate requests return the first result

Each idempotency key is bound to a fingerprint of the request body. Repeating the same key and body returns the stored result; changing the body with the same key produces a conflict. Payment state and outbox events are written in one transaction.

The pay-core outbox has no connected dispatcher. Inngest executes the workflow, but end-to-end delivery of outbox events is not implemented.

Completed-capture replay is enabled only for the simulation. The browser submits an intent ID; the backend looks up the original payment and keys from the recorded run and canonical ledger posting. It does not accept arbitrary amounts, payment IDs, or credentials from the browser. The batch and individual calls have deadlines. If a delivery's outcome is unknown, the remaining calls stop. Refreshing evidence reads the current state without sending the batch again.

Inngest schedules the retries

Inngest manages durable retries. Application code classifies an error and returns a decision; it does not add another timed retry loop. Keeping retry timing in one place prevents nested loops from multiplying attempts.

Policy checks use the original request

The proposed amount and merchant must match the original request or an accepted clarification. A text parser extracts values for this check. It cannot authenticate an invoice or verify a merchant against a registry.

Prepared simulator profiles are stored as immutable intent metadata. Each intent has its own simulation state, so one visitor's failures do not affect another visitor's experiment.

Reading the payment records

A payment's ledger posting has two entries: a debit and a credit. They sum to zero because the posting balances; that says nothing about how much was captured. The payment snapshot supplies the captured amount. A successful replay must preserve both the original entry IDs and the balance.

The observation journal can be incomplete: telemetry failures must not change a payment, retry, or compensation. Replay receipts are separate batch responses. A shared key or nearby timestamp does not prove that a receipt corresponds to a particular journal row.

If the snapshot is unavailable, the amount is unknown. An empty journal is not evidence that nothing was captured.

My work and the AI agents' work

AI agents generated and changed all the code. I set the goal, designed service boundaries and acceptance scenarios, chose tradeoffs, and reviewed whether the implementation met those constraints.

I used specifications, ADRs, and explicit handoffs to carry decisions into implementation. Independent verification, general review, and security review applied to a fixed revision. A new commit invalidated the earlier approvals. The implementing agent did not approve or merge its own work.

The demo needed more than a CLI PASS. Visitors needed separate payment and replay actions, readable decline states, and payment and ledger IDs they could compare. Further corrections stopped stale reads from replacing a newer experiment, isolated telemetry failures from execution, and fixed the display of cents and dollars.

The architecture and demo implementation are public. I did not measure a productivity gain from using AI.

Checks recorded in October 2026

These results describe commit cff901e from October 16, 2026. The checks were not rerun for this write-up.

  • pnpm test: 1,784 passing tests in 116 files — pay-core 112, durable-ledger 363, agent-orchestrator 772, orchestra 171, and agent-evals 366.
  • Typecheck, lint, build, browser JavaScript syntax, and diff checks passed.
  • All four browser scenarios passed locally against the real service stack with the simulated provider.
  • Previously passing database integration checks were reused where persistence, dependencies, and environment were unchanged. They are not added to the 1,784 total as new independent results.
  • No paid live-model calls were made in this verification.

Hostile evaluations cover 31 scenarios and 200 generated cases with a fixed seed. They check recorded effects rather than the wording of model responses. The amount check shares an extractor with the system, and merchant grounding has no independent oracle. Passing this corpus does not establish the absence of vulnerabilities.

Deployment and remaining limits

X-Customer-Id scopes data; it is not authentication. The daily-limit check has an accepted race: counting completed intents does not reserve capacity for concurrent in-flight requests. Idempotency depends on keys, storage, and delivery boundaries; APO does not claim universal exactly-once behavior for arbitrary providers.

Compensation is covered by process tests. The four browser scenarios do not demonstrate a user-initiated refund.

The initial public demo was deployed on October 20, 2026, from commit 7927fa3. Four service processes share one Fly VM, with Neon PostgreSQL and Inngest Cloud. Only the gateway and signed Inngest callback are exposed publicly; backend service ports remain internal. The hosted demo uses mock reasoning and the payment simulator, with no live-model instance.

Cloud verification confirmed a completed $500 simulated payment, two ledger entries, ten repeat deliveries preserving the original payment and ledger identities, rejection of unsigned callbacks, and persistence after stopping and waking the VM. One measured wake-and-read took about 16 seconds; this is an observation, not a latency guarantee. The four-scenario verification and test counts above remain tied to their historical snapshot.

On November 12, 2026, the demo was refreshed from commit a27d604. The main flow now puts the payment, ten-repeat test, and before/after result ahead of optional diagnostics. Fresh cloud checks passed all four prepared scenarios, including ten confirmed repeats preserving the original payment and both ledger entry IDs. A partial result remains unverified; refreshing evidence does not send another payment or replay batch.

This project does not claim PCI compliance or production readiness. Its purpose is to make execution boundaries, uncertainty, and observable effects concrete enough to inspect and discuss.

Source reference

The implementation discussion and historical test counts describe the recorded October 16 snapshot; the deployment section records the October 20 cloud verification. The links below follow the repository’s current HEAD, including subsequent changes; the test results above remain tied to cff901e.

All projects

Get in touch

For a backend or platform role, or to talk about a project, reach me on LinkedIn.