← All examples
DuplicateInvoicePrevention
examples/DuplicateInvoicePrevention.intent
DuplicateInvoicePrevention.intent
1# DuplicateInvoicePrevention.intent2# The signature IntentLang pattern: a "no duplicates" guarantee is only real when3# an idempotency key backs it. This mission says so out loud, and proves it.45mission DuplicateInvoicePrevention6use product78title "Turn a retried checkout into exactly one invoice"9for BillingCustomer1011note pm:12 A network retry, a double-click, or an at-least-once queue must never bill twice.1314goal15 Ensure one purchase produces exactly one invoice, no matter how many times the16 request arrives.1718why19 Duplicate invoices cause double charges, refunds, support tickets, finance20 cleanup, and lost trust. Preventing them is cheaper than curing them.2122requires23 Customer24 ApprovedOrder2526input27 customer: Customer28 order: Order29 idempotencyKey: IdempotencyKey30 note beginner:31 The retry key. The same key always returns the first invoice instead of32 creating a second one.33 note qa:34 Send the same order twice with the same key and expect one invoice, same id.3536output37 invoice: Invoice3839guarantee a repeated request with the same key returns the first invoice40 because at-least-once delivery means the same request will arrive more than once41 note risk:42 Without this, every retry is a new invoice and a new charge.43 verify idempotency replay test4445guarantee two different keys never collapse into one invoice46 because over-deduplication silently drops real, distinct purchases47 verify distinct keys test4849never50 create a second invoice for an already-processed key51 bill a customer more than once for one order5253never create a second invoice for an already-processed key54 because that is the exact failure this mission exists to prevent55 verify idempotency replay test5657never bill a customer more than once for one order58 because a double charge is a trust-breaking billing error59 verify duplicate charge scan6061# The idempotency decision, run it with `intent run`, assert it with `intent test`.62decision InvoiceForKey63 inputs64 keySeen65 orderApproved66 rule replay67 when keySeen == true68 return ReturnExisting69 rule unapproved70 when orderApproved == false71 return Reject72 default73 return CreateNew7475target76 TypeScript77 OpenAPI78 Tests7980test InvoiceForKey81 case first request creates the invoice82 given keySeen false, orderApproved true83 expect CreateNew84 case retry returns the existing invoice85 given keySeen true, orderApproved true86 expect ReturnExisting87 case a seen key wins even over approval checks88 given keySeen true, orderApproved false89 expect ReturnExisting90 case unapproved order is rejected91 given keySeen false, orderApproved false92 expect RejectDraft syntax. This file is illustrative and does not run yet.
More examples
AlertRoutingApiGatewayBillingServiceCalculateRiskScoreCertificationAttemptCertificationCheckoutCertificationEligibilityCertificationStudyPlanCreateInvoiceCreateStudyPlanCustomerDataRequestGroundedAnswerIngestClickstreamInvoiceCreatedNetworkEgressPolicyPlaceOrderResearchAgentRunResetPasswordStorefrontStyleSubscriptionUpgradeTenantIsolationUploadStudyMaterial