import type { ChainFields } from "../audit/HashChain.js"; import type { ExposureFacts } from "./ExposureFacts.js"; import { type ExposureLedger } from "./ExposureLedger.js"; /** * The subject digest scheme Decionis Commerce uses (`Subject.digest`), so one * vendor is one subject whichever channel paid it: the identifier trimmed and * lower-cased, scoped by the organisation. */ export declare const EXPOSURE_DIGEST_VERSION = "decionis-exposure-v1"; /** An enforced reservation key: what an earlier attempt's record may name. */ export declare const RESERVATION_KEY: RegExp; /** One reservation held against one limit. */ export interface HeldReservation { readonly limitKey: string; readonly reservationId: string; } /** What the intent the reservations are made for is, as the ledger and the evidence name it. */ export interface ExposureBinding { readonly intentId: string; readonly intentHash: string; readonly capturedAt: string; /** * The reservation key of the INDETERMINATE attempt this one retries under * the same idempotency key, for the same request. The retry reserves under * it, so the ledger answers with what the earlier attempt kept: one * operation, drawn once, and ended once its outcome is known. */ readonly retryOf?: string; } /** * What the ledger said about one money release, across every limit that * covers it. `unstated` names a limit that covers the action but whose * subject the route does not state: the payment is not counted against it, * and the evidence says so. */ export type ExposureCheck = { readonly status: "WITHIN_LIMIT"; readonly held: readonly HeldReservation[]; readonly unstated: readonly string[]; /** The limits whose reservation the ledger already held under the key: no second draw. */ readonly replayed?: readonly string[]; } | { readonly status: "EXCEEDED"; readonly limitKey: string; readonly onExceed: "HOLD" | "ESCALATE"; readonly reasonCode: string; readonly unstated: readonly string[]; } | { readonly status: "UNAVAILABLE"; readonly code: string; /** Kept only under a route's explicit fail-open; released otherwise. */ readonly held: readonly HeldReservation[]; readonly unstated: readonly string[]; /** The route fails open: the payment goes on, uncounted where nothing was held. */ readonly ungoverned: boolean; }; /** How the forwarded payment ended, as the executor classified it. */ export type PaymentOutcome = "COMMITTED" | "NOT_EXECUTED" | "INDETERMINATE"; /** What became of the reservations once the payment's outcome was known. */ export type Settlement = "SETTLED" | "RELEASED" | "KEPT" | "NONE" | "FAILED"; export interface ExposureGateOptions { readonly ledger: ExposureLedger; /** The organisation that scopes every subject digest: the gateway's tenant. */ readonly orgId: string; /** The gateway's actor type, for a limit restricted to some actor types. */ readonly actorType: string; /** Writes one chained line on the gateway's own stream. */ readonly evidence: (fields: ChainFields) => void; /** Counts each outcome, by a code the gateway produced. */ readonly count?: (outcome: string) => void; /** How many times a settle or release is attempted before it is recorded as failed. */ readonly attempts?: number; readonly retryDelayMs?: number; readonly sleep?: (ms: number) => Promise; } /** * The cumulative-limit check on the execution path. After the authority's * ALLOW and the claim of its grant, and before the payment is sent, it * reserves the amount against every limit in the organisation's policy that * covers the route's action type, in key order, under a reservation key * derived from the intent hash, so the same intent never draws twice. Any * limit that would be exceeded refuses the payment and gives back what was * already reserved. Once the upstream has answered, a committed payment is * settled, one that was not executed is released, and one whose outcome is * unknown keeps its reservation, so the ledger errs toward the cap. */ export declare class ExposureGate { private readonly options; private readonly attempts; private readonly retryDelayMs; private readonly sleep; constructor(options: ExposureGateOptions); /** A subject's digest as the ledger receives it; the identifier itself never leaves. */ digest(kind: string, id: string): string; /** * The reservation key for an intent: the same intent, the same key, on * every retry. A retry of an INDETERMINATE attempt takes that attempt's * key; a shadow observation never does. */ static reservationKey(binding: ExposureBinding, shadow?: boolean): string; /** The key derived from the intent's own hash. */ private static ownKey; /** * Reserves against every covering limit. A refusal releases what was * held; so does a fault, unless the route explicitly fails open, when * what was held is kept to be settled or released with the payment. */ reserve(facts: ExposureFacts, binding: ExposureBinding, failOpen: boolean): Promise; /** * In shadow, what the ledger would have said, without counting the * payment. A ledger that reports a subject's usage read-only is asked * that, and nothing is reserved: the amount is compared with each * covering limit's cap exactly as the ledger compares it (over the cap * when used + amount exceeds it; a currency without a cap is not * limited). A sequence limit counts steps, not amounts, so it cannot be * previewed and is named as such. A ledger without that read is asked by * reserving under a shadow key and releasing at once. The payment itself * went through unchanged. */ observe(facts: ExposureFacts, binding: ExposureBinding): Promise; /** Shadow's read-only answer: usage plus this amount against each covering limit's cap. */ private preview; /** * Ends the reservations once the payment's outcome is known: settled * when it committed (for the amount the answer states, when the route * names where and it is no more than reserved), released when it was not * executed, kept when nobody can say. A failure to settle or release is * retried and then recorded; it never changes what already executed. */ conclude(check: ExposureCheck, outcome: PaymentOutcome, binding: ExposureBinding, settledAmount?: number | null): Promise; /** The limits that cover the facts, the reservations made against them, in key order. */ private draw; /** Whether a limit governs this action: its action type, its actor type, and an amount to count. */ private covers; /** The reservation for one limit; null when the route does not state its subject. */ private request; /** Settles or releases each reservation; the ones that still failed, each recorded. */ private end; /** Asks again only where asking again cannot count twice, a bounded number of times. */ private retrying; private record; private static codeOf; } //# sourceMappingURL=ExposureGate.d.ts.map