// A webhook acknowledges requests, skips repeat deliveries and retries a partner call. // The Lab supplies fictional requests and partner replies without opening a socket. type Webhook = struct { event_id: string email: string @personal(identifier = true) amount: int } type Delivered = struct { event_id: string, status: int } type Failure = union { Rejected { reason: string } PartnerFailed { event_id: string, terminal: HttpClientExit } } type Receipt = struct { charge: decimal(38,18) @measure(id = amount, unit = "USD") } type Charge = decimal(38,18) graph webhook_to_partner : App { output done: Delivered output failed: Failure output duplicate: string connector listener : HttpServer { listen = "127.0.0.1:8080" path = "/events" codec = json max_body = "16 KiB" } operator validate { input in: variant output out: Webhook output failed: Failure on in match { data is Webhook => match { data.amount > 0 && size(data.event_id) > 0 => out = data as Webhook _ => failed = Rejected { reason = "positive amount and event id required" } } _ => failed = Rejected { reason = "expected event_id, email and integer amount" } } } operator deduplicate { input in: Webhook output out: Webhook output duplicate: string instance = { key = data.event_id } lifetime = { kind = idle; max = duration("5m") } var seen: bool = false on in match { seen => duplicate = data.event_id _ => { seen = true out = data } } } graph deliver { input in: Webhook output done: Delivered output failed: Failure instance = { key = data.event_id } lifetime = { kind = idle; max = duration("5m") } closing = { drain_for = duration("1s"); settle_for = duration("1s") } operator request { input in: Webhook output out: variant on in { // JSON erases the field schema: retain personal-data policy on the // opaque body, while the non-personal event id remains a typed key. out = { data = data as variant meta.app.event_id = data.event_id } } } connector partner : HttpClient + Retry { url = env("PARTNER_URL") auth_bearer = secret("showcase/partner/token") method = "POST" codec = json output out: Receipt timeout = duration("1s") max_body = "16 KiB" idempotency_key = meta.app.event_id // Required partner contract: honour this key for the identical request. // This declaration does not prove that a real service honours that contract. idempotent = true behavior = deterministic Retry.attempts = 2 Retry.on = [ServerError, TimeoutExpired, DnsFailed, ConnectFailed] Retry.delay = { kind = fixed; after = duration("1s") } } operator decide { input in: HttpClientExit output done: Delivered output failed: Failure on in match { data is Completed && data.status >= 200 && data.status < 300 => done = { event_id = meta.app.event_id as string status = data.status } _ => failed = PartnerFailed { event_id = meta.app.event_id as string terminal = data } } } in -> request -> partner partner.out -> nothing // On a lost answer, decide keeps the terminal case in the failure it reports. partner.exit -> match { BodyTooLarge | DecodeFailed | OutcomeUnknown | ProtocolFailed | TimeoutExpired -> decide Completed | ConnectFailed | DnsFailed | ServerError | TlsFailed -> decide } decide.done -> done decide.failed -> failed } listener.out -> validate -> deduplicate -> deliver listener.exit -> nothing validate.failed -> failed deduplicate.duplicate -> duplicate deliver.done -> done deliver.failed -> failed } lab webhook { mock deliver.partner { on in where data is Webhook && data.event_id == "dead" { out = nothing } on in where data is Webhook && data.event_id == "retry" && now() == timestamp("1970-01-01T00:00:00Z") { out = nothing } on in where data is Webhook && data.event_id == "http-500" && now() == timestamp("1970-01-01T00:00:00Z") { out = { charge = decimal("0.01") as Charge } exit = ServerError { status = 500 } } on in { out = { charge = decimal("0.02") as Charge } exit = Completed { status = 201 } } } scenario nominal { listener.out = { event_id = "ok"; email = "alice@example.test"; amount = 12 } expect listener.exit == Completed { status = 202 } expect app.done == { event_id = "ok", status = 201 } expect deliver.partner.in expect deliver.partner.out where data.charge == (decimal("0.02") as Charge) expect app.failed == nothing } scenario timeout_then_success { listener.out = { event_id = "retry"; email = "alice@example.test"; amount = 12 } expect listener.exit == Completed { status = 202 } expect app.done == nothing advance duration("1s") expect deliver.partner.exit == TimeoutExpired {} expect deliver.partner.in == nothing expect deliver.decide.in == nothing advance duration("1s") expect deliver.decide.in where data is Completed expect app.done == { event_id = "retry", status = 201 } expect app.failed == nothing } scenario partner_dead { listener.out = { event_id = "dead"; email = "alice@example.test"; amount = 12 } advance duration("1s") advance duration("1s") advance duration("1s") expect app.failed where data is PartnerFailed expect app.done == nothing advance duration("10s") expect deliver.partner.in == nothing expect app.failed == nothing } scenario duplicate { listener.out = { event_id = "same"; email = "alice@example.test"; amount = 12 } expect app.done listener.out = { event_id = "same"; email = "alice@example.test"; amount = 12 } expect listener.exit == Completed { status = 202 } expect app.duplicate == "same" expect deliver.partner.in == nothing expect app.done == nothing advance duration("5m") listener.out = { event_id = "same"; email = "alice@example.test"; amount = 12 } expect app.done expect app.duplicate == nothing } scenario invalid_payload { listener.out = json! { "unexpected": true } expect listener.exit == Completed { status = 202 } expect app.failed where data is Rejected expect deliver.partner.in == nothing listener.out = { event_id = "invalid"; email = "alice@example.test"; amount = -1 } expect listener.exit == Completed { status = 202 } expect app.failed where data is Rejected expect deliver.partner.in == nothing } scenario http_500 { listener.out = { event_id = "http-500"; email = "alice@example.test"; amount = 12 } expect deliver.partner.exit == ServerError { status = 500 } expect deliver.partner.out where data.charge == (decimal("0.01") as Charge) expect deliver.decide.in == nothing expect app.done == nothing advance duration("1s") expect app.done == { event_id = "http-500", status = 201 } expect app.failed == nothing } }