@decionis/agentsafe 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/dist/Cli.js +13 -3
- package/dist/Cli.js.map +1 -1
- package/dist/acceptance/AcceptancePlan.d.ts +109 -0
- package/dist/acceptance/AcceptancePlan.d.ts.map +1 -0
- package/dist/acceptance/AcceptancePlan.js +128 -0
- package/dist/acceptance/AcceptancePlan.js.map +1 -0
- package/dist/acceptance/AcceptanceReport.d.ts +596 -0
- package/dist/acceptance/AcceptanceReport.d.ts.map +1 -0
- package/dist/acceptance/AcceptanceReport.js +361 -0
- package/dist/acceptance/AcceptanceReport.js.map +1 -0
- package/dist/acceptance/AcceptanceRun.d.ts +118 -0
- package/dist/acceptance/AcceptanceRun.d.ts.map +1 -0
- package/dist/acceptance/AcceptanceRun.js +419 -0
- package/dist/acceptance/AcceptanceRun.js.map +1 -0
- package/dist/acceptance/Coverage.d.ts +12 -0
- package/dist/acceptance/Coverage.d.ts.map +1 -0
- package/dist/acceptance/Coverage.js +48 -0
- package/dist/acceptance/Coverage.js.map +1 -0
- package/dist/acceptance/OutageDoubles.d.ts +34 -0
- package/dist/acceptance/OutageDoubles.d.ts.map +1 -0
- package/dist/acceptance/OutageDoubles.js +67 -0
- package/dist/acceptance/OutageDoubles.js.map +1 -0
- package/dist/acceptance/ZoneProbes.d.ts +368 -0
- package/dist/acceptance/ZoneProbes.d.ts.map +1 -0
- package/dist/acceptance/ZoneProbes.js +488 -0
- package/dist/acceptance/ZoneProbes.js.map +1 -0
- package/dist/adapters/JcsDigest.d.ts +6 -5
- package/dist/adapters/JcsDigest.d.ts.map +1 -1
- package/dist/adapters/JcsDigest.js +9 -8
- package/dist/adapters/JcsDigest.js.map +1 -1
- package/dist/cli/Accept.d.ts +9 -0
- package/dist/cli/Accept.d.ts.map +1 -0
- package/dist/cli/Accept.js +209 -0
- package/dist/cli/Accept.js.map +1 -0
- package/dist/cli/Actions.d.ts +6 -4
- package/dist/cli/Actions.d.ts.map +1 -1
- package/dist/cli/Actions.js +33 -7
- package/dist/cli/Actions.js.map +1 -1
- package/dist/cli/Commands.d.ts +1 -1
- package/dist/cli/Commands.d.ts.map +1 -1
- package/dist/cli/Commands.js +5 -0
- package/dist/cli/Commands.js.map +1 -1
- package/dist/cli/ConfigCommand.d.ts.map +1 -1
- package/dist/cli/ConfigCommand.js +5 -0
- package/dist/cli/ConfigCommand.js.map +1 -1
- package/dist/cli/Doctor.d.ts.map +1 -1
- package/dist/cli/Doctor.js +67 -1
- package/dist/cli/Doctor.js.map +1 -1
- package/dist/cli/Help.d.ts.map +1 -1
- package/dist/cli/Help.js +6 -1
- package/dist/cli/Help.js.map +1 -1
- package/dist/cli/Identity.d.ts +13 -1
- package/dist/cli/Identity.d.ts.map +1 -1
- package/dist/cli/Identity.js +25 -2
- package/dist/cli/Identity.js.map +1 -1
- package/dist/cli/Status.d.ts +23 -0
- package/dist/cli/Status.d.ts.map +1 -1
- package/dist/cli/Status.js +80 -18
- package/dist/cli/Status.js.map +1 -1
- package/dist/cli/VerifyAcceptance.d.ts +38 -0
- package/dist/cli/VerifyAcceptance.d.ts.map +1 -0
- package/dist/cli/VerifyAcceptance.js +143 -0
- package/dist/cli/VerifyAcceptance.js.map +1 -0
- package/dist/config/ExecutorConfig.d.ts +6 -1
- package/dist/config/ExecutorConfig.d.ts.map +1 -1
- package/dist/config/ExecutorConfig.js.map +1 -1
- package/dist/egress/ZoneHttpClient.d.ts +3 -0
- package/dist/egress/ZoneHttpClient.d.ts.map +1 -0
- package/dist/egress/ZoneHttpClient.js +72 -0
- package/dist/egress/ZoneHttpClient.js.map +1 -0
- package/dist/exposure/DecionisExposureLedger.d.ts +44 -0
- package/dist/exposure/DecionisExposureLedger.d.ts.map +1 -0
- package/dist/exposure/DecionisExposureLedger.js +175 -0
- package/dist/exposure/DecionisExposureLedger.js.map +1 -0
- package/dist/exposure/ExposureDeclaration.d.ts +68 -0
- package/dist/exposure/ExposureDeclaration.d.ts.map +1 -0
- package/dist/exposure/ExposureDeclaration.js +83 -0
- package/dist/exposure/ExposureDeclaration.js.map +1 -0
- package/dist/exposure/ExposureFacts.d.ts +61 -0
- package/dist/exposure/ExposureFacts.d.ts.map +1 -0
- package/dist/exposure/ExposureFacts.js +201 -0
- package/dist/exposure/ExposureFacts.js.map +1 -0
- package/dist/exposure/ExposureGate.d.ts +144 -0
- package/dist/exposure/ExposureGate.d.ts.map +1 -0
- package/dist/exposure/ExposureGate.js +446 -0
- package/dist/exposure/ExposureGate.js.map +1 -0
- package/dist/exposure/ExposureLedger.d.ts +93 -0
- package/dist/exposure/ExposureLedger.d.ts.map +1 -0
- package/dist/exposure/ExposureLedger.js +24 -0
- package/dist/exposure/ExposureLedger.js.map +1 -0
- package/dist/gateway/AttemptStore.d.ts +83 -0
- package/dist/gateway/AttemptStore.d.ts.map +1 -0
- package/dist/gateway/AttemptStore.js +229 -0
- package/dist/gateway/AttemptStore.js.map +1 -0
- package/dist/gateway/ForwardHandler.d.ts +29 -3
- package/dist/gateway/ForwardHandler.d.ts.map +1 -1
- package/dist/gateway/ForwardHandler.js +32 -5
- package/dist/gateway/ForwardHandler.js.map +1 -1
- package/dist/gateway/Gateway.d.ts +229 -17
- package/dist/gateway/Gateway.d.ts.map +1 -1
- package/dist/gateway/Gateway.js +1045 -81
- package/dist/gateway/Gateway.js.map +1 -1
- package/dist/gateway/GatewayConfig.d.ts +186 -0
- package/dist/gateway/GatewayConfig.d.ts.map +1 -1
- package/dist/gateway/GatewayConfig.js +219 -5
- package/dist/gateway/GatewayConfig.js.map +1 -1
- package/dist/gateway/GatewayManifest.d.ts +18 -0
- package/dist/gateway/GatewayManifest.d.ts.map +1 -0
- package/dist/gateway/GatewayManifest.js +42 -0
- package/dist/gateway/GatewayManifest.js.map +1 -0
- package/dist/gateway/GatewayMetrics.d.ts +12 -4
- package/dist/gateway/GatewayMetrics.d.ts.map +1 -1
- package/dist/gateway/GatewayMetrics.js +5 -0
- package/dist/gateway/GatewayMetrics.js.map +1 -1
- package/dist/gateway/GatewayReport.d.ts +34 -1
- package/dist/gateway/GatewayReport.d.ts.map +1 -1
- package/dist/gateway/GatewayReport.js +11 -0
- package/dist/gateway/GatewayReport.js.map +1 -1
- package/dist/gateway/HoldStore.d.ts +89 -0
- package/dist/gateway/HoldStore.d.ts.map +1 -0
- package/dist/gateway/HoldStore.js +280 -0
- package/dist/gateway/HoldStore.js.map +1 -0
- package/dist/gateway/InterceptedRequest.d.ts +10 -0
- package/dist/gateway/InterceptedRequest.d.ts.map +1 -1
- package/dist/gateway/InterceptedRequest.js +20 -0
- package/dist/gateway/InterceptedRequest.js.map +1 -1
- package/dist/gateway/RouteTable.d.ts +33 -5
- package/dist/gateway/RouteTable.d.ts.map +1 -1
- package/dist/gateway/RouteTable.js +76 -1
- package/dist/gateway/RouteTable.js.map +1 -1
- package/dist/gateway/ShadowLedger.d.ts +11 -0
- package/dist/gateway/ShadowLedger.d.ts.map +1 -1
- package/dist/gateway/ShadowLedger.js +11 -0
- package/dist/gateway/ShadowLedger.js.map +1 -1
- package/dist/gateway/TargetEffect.d.ts +74 -0
- package/dist/gateway/TargetEffect.d.ts.map +1 -0
- package/dist/gateway/TargetEffect.js +95 -0
- package/dist/gateway/TargetEffect.js.map +1 -0
- package/dist/gateway/TargetReader.d.ts +70 -0
- package/dist/gateway/TargetReader.d.ts.map +1 -0
- package/dist/gateway/TargetReader.js +106 -0
- package/dist/gateway/TargetReader.js.map +1 -0
- package/dist/gateway/Upstream.d.ts +15 -0
- package/dist/gateway/Upstream.d.ts.map +1 -1
- package/dist/gateway/Upstream.js +24 -2
- package/dist/gateway/Upstream.js.map +1 -1
- package/dist/hosted/TenantHost.d.ts +3 -2
- package/dist/hosted/TenantHost.d.ts.map +1 -1
- package/dist/hosted/TenantHost.js +7 -1
- package/dist/hosted/TenantHost.js.map +1 -1
- package/dist/hosted/TenantRegistry.d.ts +8 -0
- package/dist/hosted/TenantRegistry.d.ts.map +1 -1
- package/dist/hosted/TenantRegistry.js +14 -1
- package/dist/hosted/TenantRegistry.js.map +1 -1
- package/dist/http/GatewayHttpServer.d.ts +7 -0
- package/dist/http/GatewayHttpServer.d.ts.map +1 -1
- package/dist/http/GatewayHttpServer.js +57 -1
- package/dist/http/GatewayHttpServer.js.map +1 -1
- package/dist/outcomes/OutcomeEvidence.d.ts +4 -4
- package/dist/posture/ActionSpaceDrift.d.ts +40 -0
- package/dist/posture/ActionSpaceDrift.d.ts.map +1 -0
- package/dist/posture/ActionSpaceDrift.js +67 -0
- package/dist/posture/ActionSpaceDrift.js.map +1 -0
- package/dist/posture/DeploymentFingerprint.d.ts +88 -0
- package/dist/posture/DeploymentFingerprint.d.ts.map +1 -0
- package/dist/posture/DeploymentFingerprint.js +78 -0
- package/dist/posture/DeploymentFingerprint.js.map +1 -0
- package/dist/secrets/SecretStore.d.ts +6 -1
- package/dist/secrets/SecretStore.d.ts.map +1 -1
- package/dist/secrets/SecretStore.js.map +1 -1
- package/dist/service/EscalationResolver.d.ts +6 -6
- package/dist/service/EscalationResolver.d.ts.map +1 -1
- package/dist/service/EscalationResolver.js +8 -4
- package/dist/service/EscalationResolver.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { Rfc8785 } from "@decionis/agent-safe-pipeline";
|
|
3
|
+
import { ExposureUnavailable, } from "./ExposureLedger.js";
|
|
4
|
+
/**
|
|
5
|
+
* The subject digest scheme Decionis Commerce uses (`Subject.digest`), so one
|
|
6
|
+
* vendor is one subject whichever channel paid it: the identifier trimmed and
|
|
7
|
+
* lower-cased, scoped by the organisation.
|
|
8
|
+
*/
|
|
9
|
+
export const EXPOSURE_DIGEST_VERSION = "decionis-exposure-v1";
|
|
10
|
+
/** The steps a sequence limit spreads across actors, by the action type that takes them. */
|
|
11
|
+
const STEP_OF = {
|
|
12
|
+
PAYMENT_RELEASE: "payment_release",
|
|
13
|
+
VENDOR_BANK_CHANGE: "vendor_bank_change",
|
|
14
|
+
};
|
|
15
|
+
/** The digest an organisation-wide limit counts under (Decionis `ORG_SUBJECT_DIGEST`). */
|
|
16
|
+
const ORG_SUBJECT_DIGEST = "0".repeat(64);
|
|
17
|
+
/** `source.platform` and `source.channel` of every reservation the gateway makes. */
|
|
18
|
+
const SOURCE = { platform: "agentsafe", channel: "gateway" };
|
|
19
|
+
/** An enforced reservation key: what an earlier attempt's record may name. */
|
|
20
|
+
export const RESERVATION_KEY = /^agentsafe:[0-9a-f]{64}$/;
|
|
21
|
+
function sha256(text) {
|
|
22
|
+
return createHash("sha256").update(text).digest("hex");
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The cumulative-limit check on the execution path. After the authority's
|
|
26
|
+
* ALLOW and the claim of its grant, and before the payment is sent, it
|
|
27
|
+
* reserves the amount against every limit in the organisation's policy that
|
|
28
|
+
* covers the route's action type, in key order, under a reservation key
|
|
29
|
+
* derived from the intent hash, so the same intent never draws twice. Any
|
|
30
|
+
* limit that would be exceeded refuses the payment and gives back what was
|
|
31
|
+
* already reserved. Once the upstream has answered, a committed payment is
|
|
32
|
+
* settled, one that was not executed is released, and one whose outcome is
|
|
33
|
+
* unknown keeps its reservation, so the ledger errs toward the cap.
|
|
34
|
+
*/
|
|
35
|
+
export class ExposureGate {
|
|
36
|
+
options;
|
|
37
|
+
attempts;
|
|
38
|
+
retryDelayMs;
|
|
39
|
+
sleep;
|
|
40
|
+
constructor(options) {
|
|
41
|
+
this.options = options;
|
|
42
|
+
this.attempts = Math.max(1, Math.min(options.attempts ?? 3, 5));
|
|
43
|
+
this.retryDelayMs = Math.max(0, Math.min(options.retryDelayMs ?? 200, 2_000));
|
|
44
|
+
this.sleep =
|
|
45
|
+
options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, Math.max(ms, 0))));
|
|
46
|
+
}
|
|
47
|
+
/** A subject's digest as the ledger receives it; the identifier itself never leaves. */
|
|
48
|
+
digest(kind, id) {
|
|
49
|
+
return sha256([
|
|
50
|
+
EXPOSURE_DIGEST_VERSION,
|
|
51
|
+
this.options.orgId.toLowerCase(),
|
|
52
|
+
kind,
|
|
53
|
+
id.trim().toLowerCase(),
|
|
54
|
+
].join("\n"));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The reservation key for an intent: the same intent, the same key, on
|
|
58
|
+
* every retry. A retry of an INDETERMINATE attempt takes that attempt's
|
|
59
|
+
* key; a shadow observation never does.
|
|
60
|
+
*/
|
|
61
|
+
static reservationKey(binding, shadow = false) {
|
|
62
|
+
if (!shadow && binding.retryOf !== undefined && RESERVATION_KEY.test(binding.retryOf)) {
|
|
63
|
+
return binding.retryOf;
|
|
64
|
+
}
|
|
65
|
+
return ExposureGate.ownKey(binding, shadow);
|
|
66
|
+
}
|
|
67
|
+
/** The key derived from the intent's own hash. */
|
|
68
|
+
static ownKey(binding, shadow) {
|
|
69
|
+
const hex = binding.intentHash.replace(/^sha256:/, "");
|
|
70
|
+
return `${shadow ? "agentsafe-shadow" : "agentsafe"}:${hex}`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reserves against every covering limit. A refusal releases what was
|
|
74
|
+
* held; so does a fault, unless the route explicitly fails open, when
|
|
75
|
+
* what was held is kept to be settled or released with the payment.
|
|
76
|
+
*/
|
|
77
|
+
async reserve(facts, binding, failOpen) {
|
|
78
|
+
const key = ExposureGate.reservationKey(binding);
|
|
79
|
+
const own = ExposureGate.ownKey(binding, false);
|
|
80
|
+
const check = await this.draw(facts, binding, key, key === own ? null : own);
|
|
81
|
+
if (check.status === "WITHIN_LIMIT") {
|
|
82
|
+
this.record({
|
|
83
|
+
event: "EXPOSURE_RESERVED",
|
|
84
|
+
intent_id: binding.intentId,
|
|
85
|
+
intent_hash: binding.intentHash,
|
|
86
|
+
reservation_key: key,
|
|
87
|
+
limits: check.held.map((entry) => entry.limitKey),
|
|
88
|
+
unstated: [...check.unstated],
|
|
89
|
+
...(key === own ? {} : { retry_of: true, replayed: [...(check.replayed ?? [])] }),
|
|
90
|
+
});
|
|
91
|
+
this.options.count?.(check.held.length === 0 ? "NOT_APPLICABLE" : "WITHIN_LIMIT");
|
|
92
|
+
return check;
|
|
93
|
+
}
|
|
94
|
+
if (check.status === "EXCEEDED") {
|
|
95
|
+
this.record({
|
|
96
|
+
event: "EXPOSURE_EXCEEDED",
|
|
97
|
+
intent_id: binding.intentId,
|
|
98
|
+
intent_hash: binding.intentHash,
|
|
99
|
+
limit_key: check.limitKey,
|
|
100
|
+
on_exceed: check.onExceed,
|
|
101
|
+
reason_code: check.reasonCode,
|
|
102
|
+
});
|
|
103
|
+
this.options.count?.("EXCEEDED");
|
|
104
|
+
return check;
|
|
105
|
+
}
|
|
106
|
+
const held = failOpen ? check.held : [];
|
|
107
|
+
if (!failOpen)
|
|
108
|
+
await this.end(check.held, "release", binding);
|
|
109
|
+
this.record({
|
|
110
|
+
event: failOpen ? "EXPOSURE_UNGOVERNED" : "EXPOSURE_UNAVAILABLE",
|
|
111
|
+
intent_id: binding.intentId,
|
|
112
|
+
intent_hash: binding.intentHash,
|
|
113
|
+
reason_codes: ["EXPOSURE_UNAVAILABLE", check.code],
|
|
114
|
+
failure_policy: failOpen ? "FAIL_OPEN" : "FAIL_CLOSED",
|
|
115
|
+
limits: held.map((entry) => entry.limitKey),
|
|
116
|
+
});
|
|
117
|
+
this.options.count?.(failOpen ? "UNGOVERNED" : "UNAVAILABLE");
|
|
118
|
+
return { ...check, held, ungoverned: failOpen };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* In shadow, what the ledger would have said, without counting the
|
|
122
|
+
* payment. A ledger that reports a subject's usage read-only is asked
|
|
123
|
+
* that, and nothing is reserved: the amount is compared with each
|
|
124
|
+
* covering limit's cap exactly as the ledger compares it (over the cap
|
|
125
|
+
* when used + amount exceeds it; a currency without a cap is not
|
|
126
|
+
* limited). A sequence limit counts steps, not amounts, so it cannot be
|
|
127
|
+
* previewed and is named as such. A ledger without that read is asked by
|
|
128
|
+
* reserving under a shadow key and releasing at once. The payment itself
|
|
129
|
+
* went through unchanged.
|
|
130
|
+
*/
|
|
131
|
+
async observe(facts, binding) {
|
|
132
|
+
const previewing = this.options.ledger.subjectUsage !== undefined;
|
|
133
|
+
let check;
|
|
134
|
+
let unpreviewed = [];
|
|
135
|
+
if (previewing) {
|
|
136
|
+
({ check, unpreviewed } = await this.preview(facts));
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
check = await this.draw(facts, binding, ExposureGate.reservationKey(binding, true));
|
|
140
|
+
if (check.status !== "EXCEEDED")
|
|
141
|
+
await this.end(check.held, "release", binding);
|
|
142
|
+
}
|
|
143
|
+
this.record({
|
|
144
|
+
event: "EXPOSURE_OBSERVED",
|
|
145
|
+
intent_id: binding.intentId,
|
|
146
|
+
intent_hash: binding.intentHash,
|
|
147
|
+
would: check.status,
|
|
148
|
+
...(check.status === "EXCEEDED"
|
|
149
|
+
? { limit_key: check.limitKey, on_exceed: check.onExceed, reason_code: check.reasonCode }
|
|
150
|
+
: {}),
|
|
151
|
+
...(check.status === "UNAVAILABLE" ? { reason_code: check.code } : {}),
|
|
152
|
+
unstated: [...check.unstated],
|
|
153
|
+
...(previewing ? { read_only: true, unpreviewed: [...unpreviewed] } : {}),
|
|
154
|
+
});
|
|
155
|
+
return check;
|
|
156
|
+
}
|
|
157
|
+
/** Shadow's read-only answer: usage plus this amount against each covering limit's cap. */
|
|
158
|
+
async preview(facts) {
|
|
159
|
+
const unavailable = (error) => ({
|
|
160
|
+
check: {
|
|
161
|
+
status: "UNAVAILABLE",
|
|
162
|
+
code: ExposureGate.codeOf(error),
|
|
163
|
+
held: [],
|
|
164
|
+
unstated: [],
|
|
165
|
+
ungoverned: false,
|
|
166
|
+
},
|
|
167
|
+
unpreviewed: [],
|
|
168
|
+
});
|
|
169
|
+
let limits;
|
|
170
|
+
try {
|
|
171
|
+
limits = await this.retrying(() => this.options.ledger.limits());
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
return unavailable(error);
|
|
175
|
+
}
|
|
176
|
+
const unstated = [];
|
|
177
|
+
const unpreviewed = [];
|
|
178
|
+
const asked = new Map();
|
|
179
|
+
const covering = limits
|
|
180
|
+
.filter((limit) => this.covers(limit, facts))
|
|
181
|
+
.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
|
|
182
|
+
for (const limit of covering) {
|
|
183
|
+
if (limit.kind === "sequence") {
|
|
184
|
+
unpreviewed.push(limit.key);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const cap = limit.caps?.find((entry) => entry.currency === facts.currency);
|
|
188
|
+
if (cap === undefined)
|
|
189
|
+
continue;
|
|
190
|
+
let kind = "vendor";
|
|
191
|
+
let digest = ORG_SUBJECT_DIGEST;
|
|
192
|
+
if (limit.subject !== "org") {
|
|
193
|
+
const id = facts.subjects[limit.subject];
|
|
194
|
+
if (id === undefined) {
|
|
195
|
+
unstated.push(limit.key);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
kind = limit.subject;
|
|
199
|
+
digest = this.digest(limit.subject, id);
|
|
200
|
+
}
|
|
201
|
+
let usage = asked.get(`${kind}:${digest}`);
|
|
202
|
+
if (usage === undefined) {
|
|
203
|
+
try {
|
|
204
|
+
usage = await this.retrying(() => this.options.ledger.subjectUsage(kind, digest));
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
return unavailable(error);
|
|
208
|
+
}
|
|
209
|
+
asked.set(`${kind}:${digest}`, usage);
|
|
210
|
+
}
|
|
211
|
+
const counted = usage.get(limit.key);
|
|
212
|
+
if (counted === undefined || counted === null) {
|
|
213
|
+
unpreviewed.push(limit.key);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
const used = counted.find((entry) => entry.currency === facts.currency)?.used ?? 0;
|
|
217
|
+
if (facts.amount > Math.max(0, cap.maxAmount - used)) {
|
|
218
|
+
return {
|
|
219
|
+
check: {
|
|
220
|
+
status: "EXCEEDED",
|
|
221
|
+
limitKey: limit.key,
|
|
222
|
+
onExceed: limit.onExceed,
|
|
223
|
+
reasonCode: "EXPOSURE_LIMIT_EXCEEDED",
|
|
224
|
+
unstated,
|
|
225
|
+
},
|
|
226
|
+
unpreviewed,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return { check: { status: "WITHIN_LIMIT", held: [], unstated }, unpreviewed };
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Ends the reservations once the payment's outcome is known: settled
|
|
234
|
+
* when it committed (for the amount the answer states, when the route
|
|
235
|
+
* names where and it is no more than reserved), released when it was not
|
|
236
|
+
* executed, kept when nobody can say. A failure to settle or release is
|
|
237
|
+
* retried and then recorded; it never changes what already executed.
|
|
238
|
+
*/
|
|
239
|
+
async conclude(check, outcome, binding, settledAmount = null) {
|
|
240
|
+
const held = check.status === "EXCEEDED" ? [] : check.held;
|
|
241
|
+
if (held.length === 0)
|
|
242
|
+
return "NONE";
|
|
243
|
+
if (outcome === "INDETERMINATE") {
|
|
244
|
+
this.record({
|
|
245
|
+
event: "EXPOSURE_KEPT",
|
|
246
|
+
intent_id: binding.intentId,
|
|
247
|
+
intent_hash: binding.intentHash,
|
|
248
|
+
reason_codes: ["EXECUTION_INDETERMINATE"],
|
|
249
|
+
reservations: held.map((entry) => ({ ...entry })),
|
|
250
|
+
});
|
|
251
|
+
return "KEPT";
|
|
252
|
+
}
|
|
253
|
+
const end = outcome === "COMMITTED" ? "settle" : "release";
|
|
254
|
+
const failed = await this.end(held, end, binding, settledAmount);
|
|
255
|
+
this.record({
|
|
256
|
+
event: end === "settle" ? "EXPOSURE_SETTLED" : "EXPOSURE_RELEASED",
|
|
257
|
+
intent_id: binding.intentId,
|
|
258
|
+
intent_hash: binding.intentHash,
|
|
259
|
+
limits: held.filter((entry) => !failed.includes(entry)).map((entry) => entry.limitKey),
|
|
260
|
+
...(end === "settle" && settledAmount !== null ? { amount_stated: true } : {}),
|
|
261
|
+
});
|
|
262
|
+
if (failed.length > 0)
|
|
263
|
+
return "FAILED";
|
|
264
|
+
return end === "settle" ? "SETTLED" : "RELEASED";
|
|
265
|
+
}
|
|
266
|
+
/** The limits that cover the facts, the reservations made against them, in key order. */
|
|
267
|
+
async draw(facts, binding, reservationKey,
|
|
268
|
+
/** Reserved under instead where the earlier attempt's reservation was released by hand. */
|
|
269
|
+
fallbackKey = null) {
|
|
270
|
+
let limits;
|
|
271
|
+
try {
|
|
272
|
+
limits = await this.retrying(() => this.options.ledger.limits());
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
return {
|
|
276
|
+
status: "UNAVAILABLE",
|
|
277
|
+
code: ExposureGate.codeOf(error),
|
|
278
|
+
held: [],
|
|
279
|
+
unstated: [],
|
|
280
|
+
ungoverned: false,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
const held = [];
|
|
284
|
+
const unstated = [];
|
|
285
|
+
const replayed = [];
|
|
286
|
+
const covering = limits
|
|
287
|
+
.filter((limit) => this.covers(limit, facts))
|
|
288
|
+
.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
|
|
289
|
+
for (const limit of covering) {
|
|
290
|
+
const request = this.request(limit, facts, binding, reservationKey);
|
|
291
|
+
if (request === null) {
|
|
292
|
+
unstated.push(limit.key);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
// The same reservation_key and body on a retry: the ledger answers
|
|
297
|
+
// with the reservation it already holds, never a second draw.
|
|
298
|
+
let answer;
|
|
299
|
+
try {
|
|
300
|
+
answer = await this.retrying(() => this.options.ledger.reserve(limit.key, request));
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
// The earlier attempt's reservation was released: someone found its
|
|
304
|
+
// payment did not happen. This one draws afresh, under its own key.
|
|
305
|
+
if (fallbackKey === null ||
|
|
306
|
+
!(error instanceof ExposureUnavailable) ||
|
|
307
|
+
error.code !== "EXPOSURE_RESERVATION_RELEASED") {
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
const fresh = this.request(limit, facts, binding, fallbackKey);
|
|
311
|
+
answer = await this.retrying(() => this.options.ledger.reserve(limit.key, fresh));
|
|
312
|
+
}
|
|
313
|
+
if (answer.decision !== "PROCEED") {
|
|
314
|
+
await this.end(held, "release", binding);
|
|
315
|
+
return {
|
|
316
|
+
status: "EXCEEDED",
|
|
317
|
+
limitKey: limit.key,
|
|
318
|
+
onExceed: answer.decision,
|
|
319
|
+
reasonCode: answer.reasonCode,
|
|
320
|
+
unstated,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
if (answer.reservationId !== null) {
|
|
324
|
+
held.push({ limitKey: limit.key, reservationId: answer.reservationId });
|
|
325
|
+
if (answer.replayed)
|
|
326
|
+
replayed.push(limit.key);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
catch (error) {
|
|
330
|
+
return {
|
|
331
|
+
status: "UNAVAILABLE",
|
|
332
|
+
code: ExposureGate.codeOf(error),
|
|
333
|
+
held,
|
|
334
|
+
unstated,
|
|
335
|
+
ungoverned: false,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
status: "WITHIN_LIMIT",
|
|
341
|
+
held,
|
|
342
|
+
unstated,
|
|
343
|
+
...(replayed.length === 0 ? {} : { replayed }),
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
/** Whether a limit governs this action: its action type, its actor type, and an amount to count. */
|
|
347
|
+
covers(limit, facts) {
|
|
348
|
+
if (!limit.actionTypes.includes(facts.actionType))
|
|
349
|
+
return false;
|
|
350
|
+
if (limit.actorTypes !== undefined && !limit.actorTypes.includes(this.options.actorType)) {
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
if (limit.kind === "sequence") {
|
|
354
|
+
const step = STEP_OF[facts.actionType];
|
|
355
|
+
return step !== undefined && (limit.steps?.includes(step) ?? true);
|
|
356
|
+
}
|
|
357
|
+
return facts.amount > 0;
|
|
358
|
+
}
|
|
359
|
+
/** The reservation for one limit; null when the route does not state its subject. */
|
|
360
|
+
request(limit, facts, binding, reservationKey) {
|
|
361
|
+
let idDigest;
|
|
362
|
+
if (limit.subject !== "org") {
|
|
363
|
+
const id = facts.subjects[limit.subject];
|
|
364
|
+
if (id === undefined)
|
|
365
|
+
return null;
|
|
366
|
+
idDigest = this.digest(limit.subject, id);
|
|
367
|
+
}
|
|
368
|
+
const sequence = limit.kind === "sequence";
|
|
369
|
+
const agent = facts.subjects.agent;
|
|
370
|
+
if (sequence && agent === undefined)
|
|
371
|
+
return null;
|
|
372
|
+
const core = {
|
|
373
|
+
subject: { kind: limit.subject, ...(idDigest === undefined ? {} : { id_digest: idDigest }) },
|
|
374
|
+
amount: facts.amount,
|
|
375
|
+
currency: facts.currency,
|
|
376
|
+
action_type: facts.actionType,
|
|
377
|
+
reservation_key: reservationKey,
|
|
378
|
+
...(sequence && agent !== undefined
|
|
379
|
+
? {
|
|
380
|
+
actor: { kind: "agent", id_digest: this.digest("actor", agent) },
|
|
381
|
+
step: STEP_OF[facts.actionType],
|
|
382
|
+
}
|
|
383
|
+
: {}),
|
|
384
|
+
};
|
|
385
|
+
return {
|
|
386
|
+
...core,
|
|
387
|
+
// Binds the key to this request: the same key with anything else is a conflict.
|
|
388
|
+
request_digest: sha256(Rfc8785.stringify({ limit: limit.key, ...core })),
|
|
389
|
+
occurred_at: binding.capturedAt,
|
|
390
|
+
source: { ...SOURCE, record_id: binding.intentId },
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
/** Settles or releases each reservation; the ones that still failed, each recorded. */
|
|
394
|
+
async end(held, end, binding, amount = null) {
|
|
395
|
+
const failed = [];
|
|
396
|
+
for (const entry of held) {
|
|
397
|
+
try {
|
|
398
|
+
await this.retrying(() => end === "settle"
|
|
399
|
+
? this.options.ledger.settle(entry.limitKey, entry.reservationId, amount ?? undefined)
|
|
400
|
+
: this.options.ledger.release(entry.limitKey, entry.reservationId));
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
failed.push(entry);
|
|
404
|
+
// An unsettled reservation keeps counting, and an unreleased one counts
|
|
405
|
+
// until it leaves the window: either errs toward the cap. The line names
|
|
406
|
+
// the reservation, so it can be ended by hand.
|
|
407
|
+
this.record({
|
|
408
|
+
event: end === "settle" ? "EXPOSURE_SETTLE_FAILED" : "EXPOSURE_RELEASE_FAILED",
|
|
409
|
+
intent_id: binding.intentId,
|
|
410
|
+
intent_hash: binding.intentHash,
|
|
411
|
+
limit_key: entry.limitKey,
|
|
412
|
+
reservation_id: entry.reservationId,
|
|
413
|
+
reason_code: ExposureGate.codeOf(error),
|
|
414
|
+
});
|
|
415
|
+
this.options.count?.(end === "settle" ? "SETTLE_FAILED" : "RELEASE_FAILED");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return failed;
|
|
419
|
+
}
|
|
420
|
+
/** Asks again only where asking again cannot count twice, a bounded number of times. */
|
|
421
|
+
async retrying(call) {
|
|
422
|
+
for (let attempt = 1;; attempt += 1) {
|
|
423
|
+
try {
|
|
424
|
+
return await call();
|
|
425
|
+
}
|
|
426
|
+
catch (error) {
|
|
427
|
+
const retryable = error instanceof ExposureUnavailable && error.retryable;
|
|
428
|
+
if (!retryable || attempt >= this.attempts)
|
|
429
|
+
throw error;
|
|
430
|
+
await this.sleep(this.retryDelayMs * attempt);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
record(fields) {
|
|
435
|
+
try {
|
|
436
|
+
this.options.evidence(fields);
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
// A line that cannot be written leaves the payment's outcome as it was.
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
static codeOf(error) {
|
|
443
|
+
return error instanceof ExposureUnavailable ? error.code : "EXPOSURE_FAILED";
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AC;IAEO,MAAM,CAAC,MAAM,CAAC,KAAc;QAClC,OAAO,KAAK,YAAY,mBAAmB,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,iBAAiB,CAAC;IAC/E,CAAC;CACF"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The exposure ledger the gateway records money releases against: the
|
|
3
|
+
* organisation's cumulative limits, read from its active policy, and a
|
|
4
|
+
* reservation per limit that is settled once the payment happened and
|
|
5
|
+
* released when it did not. Decionis keeps it (DecionisExposureLedger); a
|
|
6
|
+
* deployment may supply its own through the same interface.
|
|
7
|
+
*/
|
|
8
|
+
/** One cumulative limit, as much of it as the gateway needs to decide whether it applies. */
|
|
9
|
+
export interface ExposureLimit {
|
|
10
|
+
readonly key: string;
|
|
11
|
+
/** `refund`, `price_drop`, `discount`, `payment_release` or `sequence`. */
|
|
12
|
+
readonly kind: string;
|
|
13
|
+
/** Whose total it caps: `vendor`, `customer`, `sku`, `agent` or `org`. */
|
|
14
|
+
readonly subject: string;
|
|
15
|
+
readonly onExceed: "HOLD" | "ESCALATE";
|
|
16
|
+
/** The action types it covers, its defaults already applied by the ledger. */
|
|
17
|
+
readonly actionTypes: readonly string[];
|
|
18
|
+
/** The actor types it is limited to; absent, every actor. */
|
|
19
|
+
readonly actorTypes?: readonly string[];
|
|
20
|
+
/** A sequence limit's segregation-of-duties steps. */
|
|
21
|
+
readonly steps?: readonly string[];
|
|
22
|
+
/** An amount limit's caps, in integer minor units per currency. */
|
|
23
|
+
readonly caps?: readonly {
|
|
24
|
+
readonly currency: string;
|
|
25
|
+
readonly maxAmount: number;
|
|
26
|
+
}[];
|
|
27
|
+
}
|
|
28
|
+
/** One subject's current use of an amount limit, per currency, in integer minor units. */
|
|
29
|
+
export interface ExposureSubjectUsage {
|
|
30
|
+
readonly currency: string;
|
|
31
|
+
readonly used: number;
|
|
32
|
+
}
|
|
33
|
+
/** `CommerceExposureReserveSchema`: what one reservation states. Subjects arrive as digests only. */
|
|
34
|
+
export interface ExposureReservationRequest {
|
|
35
|
+
readonly subject: {
|
|
36
|
+
readonly kind: string;
|
|
37
|
+
readonly id_digest?: string;
|
|
38
|
+
};
|
|
39
|
+
readonly amount: number;
|
|
40
|
+
readonly currency: string;
|
|
41
|
+
readonly action_type: string;
|
|
42
|
+
readonly reservation_key: string;
|
|
43
|
+
readonly request_digest: string;
|
|
44
|
+
readonly occurred_at: string;
|
|
45
|
+
readonly source: {
|
|
46
|
+
readonly platform?: string;
|
|
47
|
+
readonly channel?: string;
|
|
48
|
+
readonly record_id?: string;
|
|
49
|
+
};
|
|
50
|
+
readonly actor?: {
|
|
51
|
+
readonly kind: "agent";
|
|
52
|
+
readonly id_digest: string;
|
|
53
|
+
};
|
|
54
|
+
readonly step?: string;
|
|
55
|
+
}
|
|
56
|
+
export type ExposureDecision = "PROCEED" | "HOLD" | "ESCALATE";
|
|
57
|
+
/** The ledger's answer to one reservation. */
|
|
58
|
+
export interface ExposureAnswer {
|
|
59
|
+
readonly decision: ExposureDecision;
|
|
60
|
+
readonly reasonCode: string;
|
|
61
|
+
/** Present for a `PROCEED` that counts; null when the limit did not apply, or refused. */
|
|
62
|
+
readonly reservationId: string | null;
|
|
63
|
+
/** The same reservation_key was already reserved: this is the earlier reservation, not a new draw. */
|
|
64
|
+
readonly replayed: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The ledger could not be asked, or its answer could not be read: a stable
|
|
68
|
+
* code, never the response body. `retryable` is true only where asking again
|
|
69
|
+
* cannot double-count: a transport failure or a server error, retried under
|
|
70
|
+
* the same reservation_key.
|
|
71
|
+
*/
|
|
72
|
+
export declare class ExposureUnavailable extends Error {
|
|
73
|
+
readonly code: string;
|
|
74
|
+
readonly retryable: boolean;
|
|
75
|
+
constructor(code: string, retryable: boolean);
|
|
76
|
+
}
|
|
77
|
+
export interface ExposureLedger {
|
|
78
|
+
/** The organisation's limits under its active policy; none when it has no policy. */
|
|
79
|
+
limits(): Promise<readonly ExposureLimit[]>;
|
|
80
|
+
reserve(limitKey: string, request: ExposureReservationRequest): Promise<ExposureAnswer>;
|
|
81
|
+
/** The payment happened: for `amount` minor units when given, else for the amount reserved. */
|
|
82
|
+
settle(limitKey: string, reservationId: string, amount?: number): Promise<void>;
|
|
83
|
+
/** The payment did not happen: the reservation stops counting. */
|
|
84
|
+
release(limitKey: string, reservationId: string): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Read-only: one subject's usage of every amount limit that counts its kind (or the whole
|
|
87
|
+
* organisation), by limit key; null for a limit it does not apply to. What a gateway in shadow
|
|
88
|
+
* asks instead of reserving. Optional: a ledger without it is previewed by reserving and
|
|
89
|
+
* releasing at once.
|
|
90
|
+
*/
|
|
91
|
+
subjectUsage?(kind: string, idDigest: string): Promise<ReadonlyMap<string, readonly ExposureSubjectUsage[] | null>>;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=ExposureLedger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExposureLedger.d.ts","sourceRoot":"","sources":["../../src/exposure/ExposureLedger.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,6FAA6F;AAC7F,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,2EAA2E;IAC3E,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,UAAU,CAAC;IACvC,8EAA8E;IAC9E,QAAQ,CAAC,WAAW,EAAE,SAAS,MAAM,EAAE,CAAC;IACxC,6DAA6D;IAC7D,QAAQ,CAAC,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACxC,sDAAsD;IACtD,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC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c;IAC7B,QAAQ,CAAC,QAAQ,EAAE,gBAAgB,CAAC;IACpC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,0FAA0F;IAC1F,QAAQ,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IACtC,sGAAsG;IACtG,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B;AAED;;;;;GAKG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;aAE1B,IAAI,EAAE,MAAM;aACZ,SAAS,EAAE,OAAO;gBADlB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,OAAO;CAKrC;AAED,MAAM,WAAW,cAAc;IAC7B,qFAAqF;IACrF,MAAM,IAAI,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IAC5C,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,0BAA0B,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IACxF,+FAA+F;IAC/F,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChF,kEAAkE;IAClE,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChE;;;;;OAKG;IACH,YAAY,CAAC,CACX,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,SAAS,oBAAoB,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;CACzE"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The exposure ledger the gateway records money releases against: the
|
|
3
|
+
* organisation's cumulative limits, read from its active policy, and a
|
|
4
|
+
* reservation per limit that is settled once the payment happened and
|
|
5
|
+
* released when it did not. Decionis keeps it (DecionisExposureLedger); a
|
|
6
|
+
* deployment may supply its own through the same interface.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The ledger could not be asked, or its answer could not be read: a stable
|
|
10
|
+
* code, never the response body. `retryable` is true only where asking again
|
|
11
|
+
* cannot double-count: a transport failure or a server error, retried under
|
|
12
|
+
* the same reservation_key.
|
|
13
|
+
*/
|
|
14
|
+
export class ExposureUnavailable extends Error {
|
|
15
|
+
code;
|
|
16
|
+
retryable;
|
|
17
|
+
constructor(code, retryable) {
|
|
18
|
+
super(code);
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.retryable = retryable;
|
|
21
|
+
this.name = "ExposureUnavailable";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=ExposureLedger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExposureLedger.js","sourceRoot":"","sources":["../../src/exposure/ExposureLedger.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAwDH;;;;;GAKG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAE1B;IACA;IAFlB,YACkB,IAAY,EACZ,SAAkB;QAElC,KAAK,CAAC,IAAI,CAAC,CAAC;QAHI,SAAI,GAAJ,IAAI,CAAQ;QACZ,cAAS,GAAT,SAAS,CAAS;QAGlC,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF"}
|