@decionis/agentsafe 0.3.0 → 0.4.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 +181 -121
- package/dist/Cli.js +5 -0
- package/dist/Cli.js.map +1 -1
- package/dist/TrustedExecutor.d.ts.map +1 -1
- package/dist/TrustedExecutor.js +1 -0
- package/dist/TrustedExecutor.js.map +1 -1
- package/dist/adapters/EffectAwareGrantVerifier.d.ts +5 -2
- package/dist/adapters/EffectAwareGrantVerifier.d.ts.map +1 -1
- package/dist/adapters/EffectAwareGrantVerifier.js +9 -0
- package/dist/adapters/EffectAwareGrantVerifier.js.map +1 -1
- package/dist/adapters/banking/BankingAction.d.ts +2 -2
- package/dist/adapters/cards/CardHoldSeal.d.ts +29 -0
- package/dist/adapters/cards/CardHoldSeal.d.ts.map +1 -0
- package/dist/adapters/cards/CardHoldSeal.js +87 -0
- package/dist/adapters/cards/CardHoldSeal.js.map +1 -0
- package/dist/adapters/cards/CardHoldStore.d.ts +102 -0
- package/dist/adapters/cards/CardHoldStore.d.ts.map +1 -0
- package/dist/adapters/cards/CardHoldStore.js +9 -0
- package/dist/adapters/cards/CardHoldStore.js.map +1 -0
- package/dist/adapters/cards/CardHolds.d.ts +19 -0
- package/dist/adapters/cards/CardHolds.d.ts.map +1 -0
- package/dist/adapters/cards/CardHolds.js +57 -0
- package/dist/adapters/cards/CardHolds.js.map +1 -0
- package/dist/adapters/cards/HeldCardGrants.d.ts +41 -42
- package/dist/adapters/cards/HeldCardGrants.d.ts.map +1 -1
- package/dist/adapters/cards/HeldCardGrants.js +112 -22
- package/dist/adapters/cards/HeldCardGrants.js.map +1 -1
- package/dist/adapters/cards/PostgresCardHolds.d.ts +95 -0
- package/dist/adapters/cards/PostgresCardHolds.d.ts.map +1 -0
- package/dist/adapters/cards/PostgresCardHolds.js +411 -0
- package/dist/adapters/cards/PostgresCardHolds.js.map +1 -0
- package/dist/cli/Cards.d.ts +5 -0
- package/dist/cli/Cards.d.ts.map +1 -0
- package/dist/cli/Cards.js +48 -0
- package/dist/cli/Cards.js.map +1 -0
- package/dist/cli/Help.d.ts.map +1 -1
- package/dist/cli/Help.js +1 -0
- package/dist/cli/Help.js.map +1 -1
- package/dist/config/ConfigKeys.d.ts +2 -2
- package/dist/config/ConfigKeys.d.ts.map +1 -1
- package/dist/config/ConfigKeys.js +12 -0
- package/dist/config/ConfigKeys.js.map +1 -1
- package/dist/config/ExecutorConfig.d.ts +53 -0
- package/dist/config/ExecutorConfig.d.ts.map +1 -1
- package/dist/config/ExecutorConfig.js +88 -0
- package/dist/config/ExecutorConfig.js.map +1 -1
- package/dist/http/Routes.d.ts +7 -0
- package/dist/http/Routes.d.ts.map +1 -1
- package/dist/http/Routes.js +7 -1
- package/dist/http/Routes.js.map +1 -1
- package/dist/identity/Authenticator.d.ts.map +1 -1
- package/dist/identity/Authenticator.js +5 -2
- package/dist/identity/Authenticator.js.map +1 -1
- package/dist/identity/PrincipalsFile.d.ts +3 -1
- package/dist/identity/PrincipalsFile.d.ts.map +1 -1
- package/dist/identity/PrincipalsFile.js +1 -0
- package/dist/identity/PrincipalsFile.js.map +1 -1
- package/dist/incident/SecurityEvents.d.ts +9 -0
- package/dist/incident/SecurityEvents.d.ts.map +1 -1
- package/dist/incident/SecurityEvents.js +3 -0
- package/dist/incident/SecurityEvents.js.map +1 -1
- package/dist/service/EscalationResolver.d.ts +30 -6
- package/dist/service/EscalationResolver.d.ts.map +1 -1
- package/dist/service/EscalationResolver.js +2 -0
- package/dist/service/EscalationResolver.js.map +1 -1
- package/dist/service/Requests.d.ts +17 -0
- package/dist/service/Requests.d.ts.map +1 -1
- package/dist/service/TrustedExecutorService.d.ts +77 -4
- package/dist/service/TrustedExecutorService.d.ts.map +1 -1
- package/dist/service/TrustedExecutorService.js +451 -56
- package/dist/service/TrustedExecutorService.js.map +1 -1
- package/package.json +2 -2
|
@@ -9,6 +9,7 @@ export declare const BankingActionSchema: z.ZodObject<{
|
|
|
9
9
|
}, z.core.$strict>;
|
|
10
10
|
actor: z.ZodObject<{
|
|
11
11
|
type: z.ZodEnum<{
|
|
12
|
+
SYSTEM: "SYSTEM";
|
|
12
13
|
HUMAN: "HUMAN";
|
|
13
14
|
APPLICATION: "APPLICATION";
|
|
14
15
|
AGENT: "AGENT";
|
|
@@ -16,7 +17,6 @@ export declare const BankingActionSchema: z.ZodObject<{
|
|
|
16
17
|
WORKFLOW: "WORKFLOW";
|
|
17
18
|
BATCH_PROCESS: "BATCH_PROCESS";
|
|
18
19
|
MIDDLEWARE: "MIDDLEWARE";
|
|
19
|
-
SYSTEM: "SYSTEM";
|
|
20
20
|
}>;
|
|
21
21
|
id: z.ZodString;
|
|
22
22
|
runtime: z.ZodOptional<z.ZodString>;
|
|
@@ -81,6 +81,7 @@ export declare const BankingActionWireSchema: z.ZodObject<{
|
|
|
81
81
|
}, z.core.$strict>;
|
|
82
82
|
actor: z.ZodObject<{
|
|
83
83
|
type: z.ZodEnum<{
|
|
84
|
+
SYSTEM: "SYSTEM";
|
|
84
85
|
HUMAN: "HUMAN";
|
|
85
86
|
APPLICATION: "APPLICATION";
|
|
86
87
|
AGENT: "AGENT";
|
|
@@ -88,7 +89,6 @@ export declare const BankingActionWireSchema: z.ZodObject<{
|
|
|
88
89
|
WORKFLOW: "WORKFLOW";
|
|
89
90
|
BATCH_PROCESS: "BATCH_PROCESS";
|
|
90
91
|
MIDDLEWARE: "MIDDLEWARE";
|
|
91
|
-
SYSTEM: "SYSTEM";
|
|
92
92
|
}>;
|
|
93
93
|
id: z.ZodString;
|
|
94
94
|
runtime: z.ZodOptional<z.ZodString>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a shared hold store keeps of a grant at rest, sealed: the captured
|
|
3
|
+
* intent and the decision, which carries the authority's grant token, and
|
|
4
|
+
* later the authorization a claim returned. The database then holds no
|
|
5
|
+
* bearer credential in clear, and a row copied out of it opens nowhere but
|
|
6
|
+
* in a process that holds the deployment's hold key.
|
|
7
|
+
*
|
|
8
|
+
* AES-256-GCM under a 256-bit key, a fresh 96-bit nonce per seal, and the
|
|
9
|
+
* row's own identity as associated data, so a sealed value moved to another
|
|
10
|
+
* row, or to another table or namespace, does not open. The key is read
|
|
11
|
+
* from the secret store at the moment of use and never written anywhere.
|
|
12
|
+
*/
|
|
13
|
+
export declare class CardHoldSeal {
|
|
14
|
+
private readonly key;
|
|
15
|
+
static readonly VERSION = "v1";
|
|
16
|
+
private constructor();
|
|
17
|
+
/** A key of exactly 32 bytes; anything else is refused by name, never echoed. */
|
|
18
|
+
static fromKey(key: Buffer): CardHoldSeal;
|
|
19
|
+
/**
|
|
20
|
+
* The key as a secret file carries it: 32 bytes as base64 or as 64 hex
|
|
21
|
+
* characters, surrounding whitespace ignored. Raw bytes are not accepted,
|
|
22
|
+
* so a key file is always text an operator can inspect for shape.
|
|
23
|
+
*/
|
|
24
|
+
static fromText(text: string): CardHoldSeal;
|
|
25
|
+
seal(plaintext: string, associated: string): string;
|
|
26
|
+
/** The plaintext, or null for anything that does not open under this key and this identity. */
|
|
27
|
+
open(sealed: string, associated: string): string | null;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=CardHoldSeal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CardHoldSeal.d.ts","sourceRoot":"","sources":["../../../src/adapters/cards/CardHoldSeal.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;GAWG;AACH,qBAAa,YAAY;IAGH,OAAO,CAAC,QAAQ,CAAC,GAAG;IAFxC,gBAAuB,OAAO,QAAQ;IAEtC,OAAO;IAEP,iFAAiF;WACnE,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY;IAKhD;;;;OAIG;WACW,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY;IAW3C,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM;IAc1D,+FAA+F;IACxF,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;CAmB/D"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
2
|
+
const CIPHER = "aes-256-gcm";
|
|
3
|
+
const KEY_BYTES = 32;
|
|
4
|
+
const NONCE_BYTES = 12;
|
|
5
|
+
const TAG_BYTES = 16;
|
|
6
|
+
/** How the nonce, the body and the tag are written into the sealed value. */
|
|
7
|
+
const WIRE_ENCODING = "base64url";
|
|
8
|
+
const TEXT_ENCODING = "utf8";
|
|
9
|
+
const KEY_INVALID = "CARD_HOLDS_KEY_INVALID";
|
|
10
|
+
/**
|
|
11
|
+
* What a shared hold store keeps of a grant at rest, sealed: the captured
|
|
12
|
+
* intent and the decision, which carries the authority's grant token, and
|
|
13
|
+
* later the authorization a claim returned. The database then holds no
|
|
14
|
+
* bearer credential in clear, and a row copied out of it opens nowhere but
|
|
15
|
+
* in a process that holds the deployment's hold key.
|
|
16
|
+
*
|
|
17
|
+
* AES-256-GCM under a 256-bit key, a fresh 96-bit nonce per seal, and the
|
|
18
|
+
* row's own identity as associated data, so a sealed value moved to another
|
|
19
|
+
* row, or to another table or namespace, does not open. The key is read
|
|
20
|
+
* from the secret store at the moment of use and never written anywhere.
|
|
21
|
+
*/
|
|
22
|
+
export class CardHoldSeal {
|
|
23
|
+
key;
|
|
24
|
+
static VERSION = "v1";
|
|
25
|
+
constructor(key) {
|
|
26
|
+
this.key = key;
|
|
27
|
+
}
|
|
28
|
+
/** A key of exactly 32 bytes; anything else is refused by name, never echoed. */
|
|
29
|
+
static fromKey(key) {
|
|
30
|
+
if (key.length !== KEY_BYTES)
|
|
31
|
+
throw new Error(KEY_INVALID);
|
|
32
|
+
return new CardHoldSeal(Buffer.from(key));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The key as a secret file carries it: 32 bytes as base64 or as 64 hex
|
|
36
|
+
* characters, surrounding whitespace ignored. Raw bytes are not accepted,
|
|
37
|
+
* so a key file is always text an operator can inspect for shape.
|
|
38
|
+
*/
|
|
39
|
+
static fromText(text) {
|
|
40
|
+
const trimmed = text.trim();
|
|
41
|
+
if (/^[0-9a-f]{64}$/i.test(trimmed))
|
|
42
|
+
return CardHoldSeal.fromKey(Buffer.from(trimmed, "hex"));
|
|
43
|
+
if (/^[a-z0-9+/]{43}=?$/i.test(trimmed)) {
|
|
44
|
+
return CardHoldSeal.fromKey(Buffer.from(trimmed, "base64"));
|
|
45
|
+
}
|
|
46
|
+
if (/^[\w-]{43}$/.test(trimmed))
|
|
47
|
+
return CardHoldSeal.fromKey(Buffer.from(trimmed, WIRE_ENCODING));
|
|
48
|
+
throw new Error(KEY_INVALID);
|
|
49
|
+
}
|
|
50
|
+
seal(plaintext, associated) {
|
|
51
|
+
const nonce = randomBytes(NONCE_BYTES);
|
|
52
|
+
const cipher = createCipheriv(CIPHER, this.key, nonce);
|
|
53
|
+
cipher.setAAD(Buffer.from(associated, TEXT_ENCODING));
|
|
54
|
+
const body = Buffer.concat([cipher.update(plaintext, TEXT_ENCODING), cipher.final()]);
|
|
55
|
+
const tag = cipher.getAuthTag();
|
|
56
|
+
return [
|
|
57
|
+
CardHoldSeal.VERSION,
|
|
58
|
+
nonce.toString(WIRE_ENCODING),
|
|
59
|
+
body.toString(WIRE_ENCODING),
|
|
60
|
+
tag.toString(WIRE_ENCODING),
|
|
61
|
+
].join(".");
|
|
62
|
+
}
|
|
63
|
+
/** The plaintext, or null for anything that does not open under this key and this identity. */
|
|
64
|
+
open(sealed, associated) {
|
|
65
|
+
const parts = sealed.split(".");
|
|
66
|
+
if (parts.length !== 4 || parts[0] !== CardHoldSeal.VERSION)
|
|
67
|
+
return null;
|
|
68
|
+
const [, nonceText, bodyText, tagText] = parts;
|
|
69
|
+
const nonce = Buffer.from(nonceText, WIRE_ENCODING);
|
|
70
|
+
const tag = Buffer.from(tagText, WIRE_ENCODING);
|
|
71
|
+
if (nonce.length !== NONCE_BYTES || tag.length !== TAG_BYTES)
|
|
72
|
+
return null;
|
|
73
|
+
try {
|
|
74
|
+
const decipher = createDecipheriv(CIPHER, this.key, nonce);
|
|
75
|
+
decipher.setAAD(Buffer.from(associated, TEXT_ENCODING));
|
|
76
|
+
decipher.setAuthTag(tag);
|
|
77
|
+
return Buffer.concat([
|
|
78
|
+
decipher.update(Buffer.from(bodyText, WIRE_ENCODING)),
|
|
79
|
+
decipher.final(),
|
|
80
|
+
]).toString(TEXT_ENCODING);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=CardHoldSeal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CardHoldSeal.js","sourceRoot":"","sources":["../../../src/adapters/cards/CardHoldSeal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE5E,MAAM,MAAM,GAAG,aAAa,CAAC;AAC7B,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,6EAA6E;AAC7E,MAAM,aAAa,GAAG,WAAW,CAAC;AAClC,MAAM,aAAa,GAAG,MAAM,CAAC;AAC7B,MAAM,WAAW,GAAG,wBAAwB,CAAC;AAE7C;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,YAAY;IAGc;IAF9B,MAAM,CAAU,OAAO,GAAG,IAAI,CAAC;IAEtC,YAAqC,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;IAAG,CAAC;IAEpD,iFAAiF;IAC1E,MAAM,CAAC,OAAO,CAAC,GAAW;QAC/B,IAAI,GAAG,CAAC,MAAM,KAAK,SAAS;YAAE,MAAM,IAAI,KAAK,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AAC,aAAa,CAAC,CAAC;QAC7B,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { CapturedIntent, DecisionGate, HeldExecution, JsonObject, SafeExecutor, VerifiedAuthorization } from "@decionis/agent-safe-pipeline";
|
|
2
|
+
import type { CardAuthorizationAnswer } from "../../service/Requests.js";
|
|
3
|
+
import type { PreparedAction } from "../EffectAdapter.js";
|
|
4
|
+
import type { HeldCardGrantView } from "./CardAuthorizationMatcher.js";
|
|
5
|
+
import type { CardAuthorizationRequest, CardPurchase } from "./CardPurchase.js";
|
|
6
|
+
/** What a hold is keyed by: one card at one merchant. A burst across merchants holds in parallel. */
|
|
7
|
+
export interface CardHoldKey {
|
|
8
|
+
readonly cardTokenRef: string;
|
|
9
|
+
readonly merchantId: string;
|
|
10
|
+
}
|
|
11
|
+
/** Between the parts of a key or a row's identity: a character no reference can contain. */
|
|
12
|
+
export declare const CARD_KEY_SEPARATOR = "\0";
|
|
13
|
+
/** The key as one string. */
|
|
14
|
+
export declare function cardHoldKey(key: CardHoldKey): string;
|
|
15
|
+
/** One held grant: the purchase, the executor's handle on it, and whether it is spent. */
|
|
16
|
+
export interface HeldCardGrant extends HeldCardGrantView {
|
|
17
|
+
readonly purchase: CardPurchase;
|
|
18
|
+
readonly prepared: PreparedAction;
|
|
19
|
+
readonly held: HeldExecution;
|
|
20
|
+
/** The executor that holds the handle; only it can claim and settle it. */
|
|
21
|
+
readonly executor: SafeExecutor;
|
|
22
|
+
used: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A hold as the store finds it: what the matcher reads, the intent it was
|
|
26
|
+
* made for, and `open`, which yields the grant with a handle this process
|
|
27
|
+
* can claim, admitting it here first when another replica made it. Null
|
|
28
|
+
* from `open` means this process cannot admit it, and nothing was spent.
|
|
29
|
+
*/
|
|
30
|
+
export interface HeldCardGrantRecord extends HeldCardGrantView {
|
|
31
|
+
readonly intentId: string;
|
|
32
|
+
open(): Promise<HeldCardGrant | null>;
|
|
33
|
+
}
|
|
34
|
+
/** What the issuer was answered when an authorization matched, kept for a retry of the same one. */
|
|
35
|
+
export type CardApproval = Extract<CardAuthorizationAnswer, {
|
|
36
|
+
readonly decision: "APPROVE";
|
|
37
|
+
}>;
|
|
38
|
+
/** An authorization that claimed a grant, until its result is recorded or it is forgotten. */
|
|
39
|
+
export interface ClaimedCardAuthorization {
|
|
40
|
+
readonly grant: HeldCardGrant;
|
|
41
|
+
request: CardAuthorizationRequest;
|
|
42
|
+
readonly authorization: VerifiedAuthorization;
|
|
43
|
+
readonly leaseExpiresAtMs: number;
|
|
44
|
+
readonly answer: CardApproval;
|
|
45
|
+
readonly retainUntilMs: number;
|
|
46
|
+
settled: boolean;
|
|
47
|
+
}
|
|
48
|
+
/** What `recordClaim` is given: the claim, and what a replica needs to finalize it elsewhere. */
|
|
49
|
+
export interface CardClaimInput extends Omit<ClaimedCardAuthorization, "retainUntilMs" | "settled"> {
|
|
50
|
+
/** The verifier's claim material, sealed by a shared store and never written in clear. */
|
|
51
|
+
readonly material: JsonObject | null;
|
|
52
|
+
}
|
|
53
|
+
/** How far back a card's holds and claims are counted for policy. */
|
|
54
|
+
export declare const CARD_VELOCITY_WINDOW_MS: number;
|
|
55
|
+
/** How often one card was held for and claimed lately: counts for policy, never a reference. */
|
|
56
|
+
export interface CardVelocity {
|
|
57
|
+
readonly window_seconds: number;
|
|
58
|
+
readonly holds: number;
|
|
59
|
+
readonly claims: number;
|
|
60
|
+
}
|
|
61
|
+
/** What a store needs from the process to admit a hold or a claim another replica made. */
|
|
62
|
+
export interface CardHoldAdoption {
|
|
63
|
+
hold(captured: CapturedIntent, decision: DecisionGate): Promise<{
|
|
64
|
+
readonly held: HeldExecution;
|
|
65
|
+
readonly executor: SafeExecutor;
|
|
66
|
+
} | null>;
|
|
67
|
+
claim(captured: CapturedIntent, decision: DecisionGate, authorization: VerifiedAuthorization, material: JsonObject | null): {
|
|
68
|
+
readonly held: HeldExecution;
|
|
69
|
+
readonly executor: SafeExecutor;
|
|
70
|
+
} | null;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Where held grants and the authorizations that claimed them live. One key
|
|
74
|
+
* (a card at a merchant) has at most one spendable hold, and a hold is spent
|
|
75
|
+
* once, whichever replica the authorization reaches: `spend` is the race
|
|
76
|
+
* rule, and it is the same rule in memory and in the shared store.
|
|
77
|
+
*/
|
|
78
|
+
export interface CardHoldStore {
|
|
79
|
+
readonly kind: "memory" | "postgres";
|
|
80
|
+
/** Whether the key has a hold an authorization could still match. */
|
|
81
|
+
spendable(key: CardHoldKey): Promise<boolean>;
|
|
82
|
+
/** Keeps a hold; false when the key already has a spendable one or the store is full. */
|
|
83
|
+
hold(grant: HeldCardGrant): Promise<boolean>;
|
|
84
|
+
/** The hold for a key, spent, expired or neither, until it is forgotten. */
|
|
85
|
+
grant(key: CardHoldKey): Promise<HeldCardGrantRecord | undefined>;
|
|
86
|
+
/** Spends a hold once: true for the one caller that did, false for every other. */
|
|
87
|
+
spend(grant: HeldCardGrant): Promise<boolean>;
|
|
88
|
+
claimed(authorizationId: string): Promise<ClaimedCardAuthorization | undefined>;
|
|
89
|
+
/** Remembers a claim for its result. */
|
|
90
|
+
recordClaim(authorizationId: string, claim: CardClaimInput): Promise<ClaimedCardAuthorization>;
|
|
91
|
+
/** An incremental authorization: the same claim, re-presented for more; false when unknown. */
|
|
92
|
+
amend(authorizationId: string, request: CardAuthorizationRequest): Promise<boolean>;
|
|
93
|
+
/** Marks a claim settled once: false when it was already, or is unknown. */
|
|
94
|
+
settle(authorizationId: string): Promise<boolean>;
|
|
95
|
+
velocity(cardTokenRef: string, windowMs?: number): Promise<CardVelocity>;
|
|
96
|
+
/** Throws unless the store can be used; for the shared store, the tables are reachable. */
|
|
97
|
+
ready(): Promise<void>;
|
|
98
|
+
/** Forgets what lapsed; nothing to do in memory, which sweeps as it goes. */
|
|
99
|
+
cleanup(): Promise<void>;
|
|
100
|
+
close(): Promise<void>;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=CardHoldStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CardHoldStore.d.ts","sourceRoot":"","sources":["../../../src/adapters/cards/CardHoldStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,YAAY,EACZ,aAAa,EACb,UAAU,EACV,YAAY,EACZ,qBAAqB,EACtB,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACzE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,KAAK,EAAE,wBAAwB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEhF,qGAAqG;AACrG,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,4FAA4F;AAC5F,eAAO,MAAM,kBAAkB,OAAW,CAAC;AAE3C,6BAA6B;AAC7B,wBAAgB,WAAW,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,CAEpD;AAED,0FAA0F;AAC1F,MAAM,WAAW,aAAc,SAAQ,iBAAiB;IACtD,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;IAClC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,2EAA2E;IAC3E,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;;;GAKG;AACH,MAAM,WAAW,mBAAoB,SAAQ,iBAAiB;IAC5D,QAAQ,CAAC,QAAQ,EAAE,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,CAAC,CAAC;IACvB,6EAA6E;IAC7E,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Between the parts of a key or a row's identity: a character no reference can contain. */
|
|
2
|
+
export const CARD_KEY_SEPARATOR = "\u0000";
|
|
3
|
+
/** The key as one string. */
|
|
4
|
+
export function cardHoldKey(key) {
|
|
5
|
+
return `${key.cardTokenRef}${CARD_KEY_SEPARATOR}${key.merchantId}`;
|
|
6
|
+
}
|
|
7
|
+
/** How far back a card's holds and claims are counted for policy. */
|
|
8
|
+
export const CARD_VELOCITY_WINDOW_MS = 10 * 60_000;
|
|
9
|
+
//# sourceMappingURL=CardHoldStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CardHoldStore.js","sourceRoot":"","sources":["../../../src/adapters/cards/CardHoldStore.ts"],"names":[],"mappings":"AAmBA,4FAA4F;AAC5F,MAAM,CAAC,MAAM,kBAAkB,GAAG,QAAQ,CAAC;AAE3C,6BAA6B;AAC7B,MAAM,UAAU,WAAW,CAAC,GAAgB;IAC1C,OAAO,GAAG,GAAG,CAAC,YAAY,GAAG,kBAAkB,GAAG,GAAG,CAAC,UAAU,EAAE,CAAC;AACrE,CAAC;AA8CD,qEAAqE;AACrE,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,GAAG,MAAM,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type LoadPg } from "../../edge/EdgeReplay.js";
|
|
2
|
+
import type { CardHoldSeal } from "./CardHoldSeal.js";
|
|
3
|
+
import type { CardHoldAdoption, CardHoldStore } from "./CardHoldStore.js";
|
|
4
|
+
/**
|
|
5
|
+
* The shared hold store over one `pg` pool, opened on first use: the
|
|
6
|
+
* connection string and the hold key are secrets, read when the pool is
|
|
7
|
+
* opened and when a row is sealed, and never held as strings here.
|
|
8
|
+
*/
|
|
9
|
+
export declare function postgresCardHolds(options: {
|
|
10
|
+
readonly databaseUrl: () => string;
|
|
11
|
+
readonly seal: () => CardHoldSeal;
|
|
12
|
+
readonly namespace: string;
|
|
13
|
+
readonly table: string;
|
|
14
|
+
readonly adopt: CardHoldAdoption;
|
|
15
|
+
readonly load?: LoadPg;
|
|
16
|
+
readonly clock?: () => number;
|
|
17
|
+
readonly claimRetentionMs?: number;
|
|
18
|
+
}): CardHoldStore;
|
|
19
|
+
//# sourceMappingURL=CardHolds.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CardHolds.d.ts","sourceRoot":"","sources":["../../../src/adapters/cards/CardHolds.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,KAAK,MAAM,EAAe,MAAM,0BAA0B,CAAC;AAC5E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAEV,gBAAgB,EAEhB,aAAa,EAKd,MAAM,oBAAoB,CAAC;AAI5B;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE;IACzC,QAAQ,CAAC,WAAW,EAAE,MAAM,MAAM,CAAC;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,YAAY,CAAC;IAClC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAC;IACjC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;IAC9B,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;CACpC,GAAG,aAAa,CAqDhB"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { loadPg } from "../../edge/EdgeReplay.js";
|
|
2
|
+
import { PostgresCardHolds } from "./PostgresCardHolds.js";
|
|
3
|
+
/**
|
|
4
|
+
* The shared hold store over one `pg` pool, opened on first use: the
|
|
5
|
+
* connection string and the hold key are secrets, read when the pool is
|
|
6
|
+
* opened and when a row is sealed, and never held as strings here.
|
|
7
|
+
*/
|
|
8
|
+
export function postgresCardHolds(options) {
|
|
9
|
+
let opened = null;
|
|
10
|
+
const open = async () => {
|
|
11
|
+
opened ??= (async () => {
|
|
12
|
+
const { Pool } = await (options.load ?? loadPg)();
|
|
13
|
+
const pool = new Pool({ connectionString: options.databaseUrl(), max: 4 });
|
|
14
|
+
// An idle connection's error is the next query's failure, not the process's.
|
|
15
|
+
pool.on("error", () => undefined);
|
|
16
|
+
const client = {
|
|
17
|
+
query: async (text, values) => (await pool.query(text, [...values])),
|
|
18
|
+
};
|
|
19
|
+
const store = new PostgresCardHolds({
|
|
20
|
+
client,
|
|
21
|
+
namespace: options.namespace,
|
|
22
|
+
table: options.table,
|
|
23
|
+
seal: options.seal(),
|
|
24
|
+
adopt: options.adopt,
|
|
25
|
+
...(options.clock === undefined ? {} : { clock: options.clock }),
|
|
26
|
+
...(options.claimRetentionMs === undefined
|
|
27
|
+
? {}
|
|
28
|
+
: { claimRetentionMs: options.claimRetentionMs }),
|
|
29
|
+
});
|
|
30
|
+
return { store, pool };
|
|
31
|
+
})();
|
|
32
|
+
return await opened;
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
kind: "postgres",
|
|
36
|
+
spendable: async (key) => (await open()).store.spendable(key),
|
|
37
|
+
hold: async (grant) => (await open()).store.hold(grant),
|
|
38
|
+
grant: async (key) => (await open()).store.grant(key),
|
|
39
|
+
spend: async (grant) => (await open()).store.spend(grant),
|
|
40
|
+
claimed: async (id) => (await open()).store.claimed(id),
|
|
41
|
+
recordClaim: async (id, claim) => (await open()).store.recordClaim(id, claim),
|
|
42
|
+
amend: async (id, request) => (await open()).store.amend(id, request),
|
|
43
|
+
settle: async (id) => (await open()).store.settle(id),
|
|
44
|
+
velocity: async (card, windowMs) => (await open()).store.velocity(card, windowMs),
|
|
45
|
+
ready: async () => {
|
|
46
|
+
await (await open()).store.ready();
|
|
47
|
+
},
|
|
48
|
+
cleanup: async () => {
|
|
49
|
+
await (await open()).store.cleanup();
|
|
50
|
+
},
|
|
51
|
+
close: async () => {
|
|
52
|
+
if (opened !== null)
|
|
53
|
+
await (await opened).pool.end();
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=CardHolds.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CardHolds.js","sourceRoot":"","sources":["../../../src/adapters/cards/CardHolds.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAA4B,MAAM,0BAA0B,CAAC;AAa5E,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,OASjC;IACC,IAAI,MAAM,GAA+D,IAAI,CAAC;IAC9E,MAAM,IAAI,GAAG,KAAK,IAAyD,EAAE;QAC3E,MAAM,KAAK,CAAC,KAAK,IAAI,EAAE;YACrB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,MAAM,CAAC,EAAE,CAAC;YAClD,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,EAAE,gBAAgB,EAAE,OAAO,CAAC,WAAW,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;YAC3E,6EAA6E;YAC7E,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YAClC,MAAM,MAAM,GAAc;gBACxB,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,CAC5B,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAA4C;aACnF,CAAC;YACF,MAAM,KAAK,GAAG,IAAI,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: the purchase, the executor's handle on it, and whether it is spent. */
|
|
7
|
-
export interface HeldCardGrant extends HeldCardGrantView {
|
|
8
|
-
readonly purchase: CardPurchase;
|
|
9
|
-
readonly prepared: PreparedAction;
|
|
10
|
-
readonly held: HeldExecution;
|
|
11
|
-
/** The executor that issued the hold; only it can claim it. */
|
|
12
|
-
readonly executor: SafeExecutor;
|
|
13
|
-
used: boolean;
|
|
14
|
-
}
|
|
15
|
-
/** What the issuer was answered when an authorization matched, kept for a retry of the same one. */
|
|
16
|
-
export type CardApproval = Extract<CardAuthorizationAnswer, {
|
|
17
|
-
readonly decision: "APPROVE";
|
|
18
|
-
}>;
|
|
19
|
-
/** An authorization that claimed a grant, until its result is recorded or it is forgotten. */
|
|
20
|
-
export interface ClaimedCardAuthorization {
|
|
21
|
-
readonly grant: HeldCardGrant;
|
|
22
|
-
readonly request: CardAuthorizationRequest;
|
|
23
|
-
readonly authorization: VerifiedAuthorization;
|
|
24
|
-
readonly leaseExpiresAtMs: number;
|
|
25
|
-
readonly answer: CardApproval;
|
|
26
|
-
readonly retainUntilMs: number;
|
|
27
|
-
settled: boolean;
|
|
28
|
-
}
|
|
1
|
+
import { type CardClaimInput, type CardHoldKey, type CardHoldStore, type CardVelocity, type ClaimedCardAuthorization, type HeldCardGrant, type HeldCardGrantRecord } from "./CardHoldStore.js";
|
|
2
|
+
import type { CardAuthorizationRequest } from "./CardPurchase.js";
|
|
3
|
+
export type { CardApproval, CardVelocity, ClaimedCardAuthorization, HeldCardGrant, } from "./CardHoldStore.js";
|
|
4
|
+
export { CARD_VELOCITY_WINDOW_MS } from "./CardHoldStore.js";
|
|
29
5
|
export interface HeldCardGrantsOptions {
|
|
30
6
|
/** Entries of each kind this process keeps at most; a hold beyond it is refused. */
|
|
31
7
|
readonly maxEntries?: number;
|
|
@@ -35,36 +11,59 @@ export interface HeldCardGrantsOptions {
|
|
|
35
11
|
}
|
|
36
12
|
/**
|
|
37
13
|
* Grants held for a card authorization that has not happened yet, keyed by
|
|
38
|
-
* the card
|
|
14
|
+
* the card and the merchant, and the authorizations that claimed one, keyed
|
|
39
15
|
* by the issuer's authorization id. In memory and bounded on purpose: a hold
|
|
40
16
|
* lives no longer than its grant, which the authority caps in minutes, and a
|
|
41
17
|
* restart that loses one loses only a grant nobody claimed, which expires
|
|
42
|
-
* with no effect. A claimed authorization is already in the journal.
|
|
18
|
+
* with no effect. A claimed authorization is already in the journal. A
|
|
19
|
+
* deployment with more than one replica, or one that must keep its holds
|
|
20
|
+
* across a restart, uses the shared store instead.
|
|
43
21
|
*
|
|
44
|
-
* One card has at most one spendable hold. An agent that wants
|
|
45
|
-
* purchase on the same card waits for the
|
|
46
|
-
* expire, so an authorization can never be
|
|
22
|
+
* One card has at most one spendable hold per merchant. An agent that wants
|
|
23
|
+
* a second purchase at the same merchant on the same card waits for the
|
|
24
|
+
* first to be authorised or to expire, so an authorization can never be
|
|
25
|
+
* matched to the wrong one of two; purchases at different merchants hold in
|
|
26
|
+
* parallel, because the authorization names its merchant.
|
|
47
27
|
*/
|
|
48
|
-
export declare class HeldCardGrants {
|
|
28
|
+
export declare class HeldCardGrants implements CardHoldStore {
|
|
29
|
+
readonly kind: "memory";
|
|
30
|
+
private static readonly MAX_ACTIVITY_PER_CARD;
|
|
49
31
|
private readonly holds;
|
|
50
32
|
private readonly claims;
|
|
33
|
+
/** When each card was last held for and claimed, most recently touched card last. */
|
|
34
|
+
private readonly activity;
|
|
51
35
|
private readonly maxEntries;
|
|
52
36
|
private readonly claimRetentionMs;
|
|
53
37
|
private readonly clock;
|
|
54
38
|
constructor(options?: HeldCardGrantsOptions);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
claimed(authorizationId: string): ClaimedCardAuthorization | undefined;
|
|
39
|
+
spendable(key: CardHoldKey): Promise<boolean>;
|
|
40
|
+
hold(grant: HeldCardGrant): Promise<boolean>;
|
|
41
|
+
grant(key: CardHoldKey): Promise<HeldCardGrantRecord | undefined>;
|
|
42
|
+
/** Spent before anything is awaited by the caller: the first to ask gets it, later askers find it used. */
|
|
43
|
+
spend(grant: HeldCardGrant): Promise<boolean>;
|
|
44
|
+
claimed(authorizationId: string): Promise<ClaimedCardAuthorization | undefined>;
|
|
62
45
|
/** Remembers a claim for its result; a full store forgets the oldest, which the journal still holds. */
|
|
63
|
-
recordClaim(authorizationId: string, claim:
|
|
46
|
+
recordClaim(authorizationId: string, claim: CardClaimInput): Promise<ClaimedCardAuthorization>;
|
|
47
|
+
amend(authorizationId: string, request: CardAuthorizationRequest): Promise<boolean>;
|
|
48
|
+
settle(authorizationId: string): Promise<boolean>;
|
|
49
|
+
/**
|
|
50
|
+
* The burst a policy wants to see: how many purchases were held for this
|
|
51
|
+
* card and how many authorizations claimed one, over the window. The
|
|
52
|
+
* policy language counts nothing over time, so the executor counts here
|
|
53
|
+
* and puts the numbers in the intent's context; a card reference never
|
|
54
|
+
* leaves with them.
|
|
55
|
+
*/
|
|
56
|
+
velocity(cardTokenRef: string, windowMs?: number): Promise<CardVelocity>;
|
|
57
|
+
ready(): Promise<void>;
|
|
58
|
+
cleanup(): Promise<void>;
|
|
59
|
+
close(): Promise<void>;
|
|
64
60
|
get size(): {
|
|
65
61
|
readonly holds: number;
|
|
66
62
|
readonly claims: number;
|
|
67
63
|
};
|
|
64
|
+
private spendableNow;
|
|
65
|
+
/** Remembers one hold or claim on a card, bounded per card and across cards. */
|
|
66
|
+
private note;
|
|
68
67
|
private sweep;
|
|
69
68
|
}
|
|
70
69
|
//# sourceMappingURL=HeldCardGrants.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HeldCardGrants.d.ts","sourceRoot":"","sources":["../../../src/adapters/cards/HeldCardGrants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"HeldCardGrants.d.ts","sourceRoot":"","sources":["../../../src/adapters/cards/HeldCardGrants.ts"],"names":[],"mappings":"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