@abloatai/transaction 0.38.0 → 0.40.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/dist/auth/index.d.ts +16 -2
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +10 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/coordination/events.d.ts +9 -0
- package/dist/coordination/events.d.ts.map +1 -1
- package/dist/coordination/index.d.ts +2 -2
- package/dist/coordination/index.d.ts.map +1 -1
- package/dist/coordination/index.js +1 -1
- package/dist/coordination/index.js.map +1 -1
- package/dist/coordination/schema.d.ts +226 -34
- package/dist/coordination/schema.d.ts.map +1 -1
- package/dist/coordination/schema.js +180 -48
- package/dist/coordination/schema.js.map +1 -1
- package/dist/coordination/trace.d.ts.map +1 -1
- package/dist/coordination/trace.js +9 -1
- package/dist/coordination/trace.js.map +1 -1
- package/dist/errorCodes.d.ts +6 -0
- package/dist/errorCodes.d.ts.map +1 -1
- package/dist/errorCodes.js +6 -0
- package/dist/errorCodes.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +9 -2
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/policy/types.d.ts +10 -2
- package/dist/policy/types.d.ts.map +1 -1
- package/dist/policy/types.js.map +1 -1
- package/dist/pricing.d.ts +194 -0
- package/dist/pricing.d.ts.map +1 -0
- package/dist/pricing.js +330 -0
- package/dist/pricing.js.map +1 -0
- package/dist/resources/functionalUpdate.d.ts.map +1 -1
- package/dist/resources/functionalUpdate.js +8 -8
- package/dist/resources/functionalUpdate.js.map +1 -1
- package/dist/schema/index.d.ts +2 -2
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +2 -2
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/roles.d.ts +19 -0
- package/dist/schema/roles.d.ts.map +1 -1
- package/dist/schema/roles.js +23 -3
- package/dist/schema/roles.js.map +1 -1
- package/dist/schema/schema.d.ts +13 -1
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/schema/schema.js +1 -1
- package/dist/schema/schema.js.map +1 -1
- package/dist/schema/select.d.ts +13 -0
- package/dist/schema/select.d.ts.map +1 -1
- package/dist/schema/select.js +26 -0
- package/dist/schema/select.js.map +1 -1
- package/dist/schema/tenancy.d.ts +1 -1
- package/dist/transactions/settlement/commitEnvelope.d.ts +10 -2
- package/dist/transactions/settlement/commitEnvelope.d.ts.map +1 -1
- package/dist/transactions/settlement/pendingWrite.d.ts +10 -2
- package/dist/transactions/settlement/pendingWrite.d.ts.map +1 -1
- package/dist/transport/httpClient.d.ts.map +1 -1
- package/dist/transport/httpClient.js +10 -1
- package/dist/transport/httpClient.js.map +1 -1
- package/dist/transport/wsFrameHandlers.d.ts.map +1 -1
- package/dist/transport/wsFrameHandlers.js +9 -3
- package/dist/transport/wsFrameHandlers.js.map +1 -1
- package/dist/wire/accountResponses.d.ts +100 -0
- package/dist/wire/accountResponses.d.ts.map +1 -1
- package/dist/wire/accountResponses.js +70 -0
- package/dist/wire/accountResponses.js.map +1 -1
- package/dist/wire/auth.d.ts +7 -1
- package/dist/wire/auth.d.ts.map +1 -1
- package/dist/wire/auth.js +26 -3
- package/dist/wire/auth.js.map +1 -1
- package/dist/wire/commit.d.ts +708 -100
- package/dist/wire/commit.d.ts.map +1 -1
- package/dist/wire/dataSourceResponses.d.ts +171 -0
- package/dist/wire/dataSourceResponses.d.ts.map +1 -0
- package/dist/wire/dataSourceResponses.js +168 -0
- package/dist/wire/dataSourceResponses.js.map +1 -0
- package/dist/wire/frames.d.ts +20 -4
- package/dist/wire/frames.d.ts.map +1 -1
- package/dist/wire/inboundFrames.d.ts +102 -14
- package/dist/wire/inboundFrames.d.ts.map +1 -1
- package/dist/wire/index.d.ts +4 -2
- package/dist/wire/index.d.ts.map +1 -1
- package/dist/wire/index.js +5 -1
- package/dist/wire/index.js.map +1 -1
- package/dist/wire/pricing.d.ts +43 -0
- package/dist/wire/pricing.d.ts.map +1 -0
- package/dist/wire/pricing.js +36 -0
- package/dist/wire/pricing.js.map +1 -0
- package/package.json +6 -1
- package/src/auth/index.ts +31 -3
- package/src/coordination/events.ts +9 -0
- package/src/coordination/index.ts +8 -0
- package/src/coordination/schema.ts +200 -51
- package/src/coordination/trace.ts +9 -1
- package/src/errorCodes.ts +24 -0
- package/src/errors.ts +9 -2
- package/src/index.ts +4 -0
- package/src/policy/types.ts +10 -2
- package/src/pricing.ts +350 -0
- package/src/resources/functionalUpdate.ts +11 -11
- package/src/schema/index.ts +3 -1
- package/src/schema/roles.ts +26 -3
- package/src/schema/schema.ts +15 -0
- package/src/schema/select.ts +30 -0
- package/src/transport/httpClient.ts +8 -1
- package/src/transport/wsFrameHandlers.ts +9 -3
- package/src/wire/accountResponses.ts +76 -0
- package/src/wire/auth.ts +30 -3
- package/src/wire/dataSourceResponses.ts +188 -0
- package/src/wire/index.ts +33 -0
- package/src/wire/pricing.ts +41 -0
package/src/index.ts
CHANGED
|
@@ -47,6 +47,10 @@ export type { ModelData } from './types/modelData.js';
|
|
|
47
47
|
// The error hierarchy and its wire/recovery helpers.
|
|
48
48
|
export * from './errors.js';
|
|
49
49
|
|
|
50
|
+
// The price of the product: tiers, rate card, and the bill arithmetic the
|
|
51
|
+
// published pricing page and the runtime caps both derive from.
|
|
52
|
+
export * from './pricing.js';
|
|
53
|
+
|
|
50
54
|
// Pre-seam leaf surfaces, kept for compatibility with the first extraction.
|
|
51
55
|
export * from './errorCodes.js';
|
|
52
56
|
export * from './environment.js';
|
package/src/policy/types.ts
CHANGED
|
@@ -107,8 +107,16 @@ export type ConflictDecision =
|
|
|
107
107
|
*
|
|
108
108
|
* The monotonic `sync_id` landing order decides who yields: the stale
|
|
109
109
|
* committer always recomputes against the newer value, an asymmetry that
|
|
110
|
-
* prevents two notifying writers from looping against each other.
|
|
111
|
-
*
|
|
110
|
+
* prevents two notifying writers from looping against each other.
|
|
111
|
+
*
|
|
112
|
+
* That rules out livelock, and NOT starvation. Each round adopts a newer
|
|
113
|
+
* `observedSyncId`, so no baseline repeats — but a peer writing faster than
|
|
114
|
+
* the committer's read→decide→write gap keeps winning, and the rounds are
|
|
115
|
+
* unbounded because the engine does not re-issue: the actor does. Progress in
|
|
116
|
+
* the watermark is not progress in the work. The functional `update(id, fn)`
|
|
117
|
+
* bounds its own loop; a hand-rolled one must bound itself. To stop an actor
|
|
118
|
+
* writing at all while a belief it holds is stale, gate the belief —
|
|
119
|
+
* `track(..., { onStale: 'reject' })` — rather than the write.
|
|
112
120
|
*/
|
|
113
121
|
| { readonly action: 'notify'; readonly reason?: string };
|
|
114
122
|
|
package/src/pricing.ts
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Ablo costs, expressed as arithmetic rather than as a table someone
|
|
3
|
+
* retypes. This module owns the price: the tiers, the rate card the meter is
|
|
4
|
+
* charged against, and the functions that turn a month of usage into a bill.
|
|
5
|
+
* The published pricing page, the plan seeds, and the runtime caps all derive
|
|
6
|
+
* from here; nothing restates it.
|
|
7
|
+
*
|
|
8
|
+
* The shape has three parts, and each one exists because a different cost does.
|
|
9
|
+
*
|
|
10
|
+
* - A monthly floor per tier. Serving an organization costs money before it
|
|
11
|
+
* sends a single request: a lane of the commit engine, a replication reader
|
|
12
|
+
* per registered source, and the log it appends to. The floor pays for that
|
|
13
|
+
* standing capacity, and usage is charged against it rather than added to
|
|
14
|
+
* it. An organization pays the greater of the two, never both.
|
|
15
|
+
* - A metered rate on `ops`, the one number that collapses commits, reads,
|
|
16
|
+
* and claim creates. Marginal service is cheap enough that a second axis
|
|
17
|
+
* would buy precision nobody wants: at the measured 79.5 microseconds of
|
|
18
|
+
* CPU per delta, a vCPU-hour serves roughly 45 million of them. The cost
|
|
19
|
+
* that scales is the log those operations leave behind, and it scales with
|
|
20
|
+
* all three of them together.
|
|
21
|
+
* - A concurrent-connection cap, which is a limit and not a meter. A held
|
|
22
|
+
* socket is a real reservation whether or not it is busy, so it is bounded
|
|
23
|
+
* rather than billed.
|
|
24
|
+
*
|
|
25
|
+
* The allowance a tier advertises is DERIVED, never stored: "the floor covers
|
|
26
|
+
* 49.5M operations" is {@link opsForUsd} applied to the floor. Storing it
|
|
27
|
+
* alongside the floor is how a published price silently stops matching the
|
|
28
|
+
* invoice, because raising one number does not fail anything that pins the
|
|
29
|
+
* other.
|
|
30
|
+
*
|
|
31
|
+
* Storage is deliberately absent from {@link billableAxisSchema}. It is a real
|
|
32
|
+
* recurring cost and it is quoted per tier as a ceiling, but no per-organization
|
|
33
|
+
* bytes sampler emits it yet, and pricing a dimension nothing measures would be
|
|
34
|
+
* a claim the invoice cannot support. Add the axis when the sampler ships.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import { z } from 'zod';
|
|
38
|
+
import { meterEventSchema, planTierSchema, rateBracketSchema } from './wire/pricing.js';
|
|
39
|
+
import type { MeterEvent, PlanTier, RateBracket } from './wire/pricing.js';
|
|
40
|
+
|
|
41
|
+
export { meterEventSchema, planTierSchema, rateBracketSchema };
|
|
42
|
+
export type { MeterEvent, PlanTier, RateBracket };
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The version of the pricing contract: the tiers, the rate card, and the way a
|
|
46
|
+
* bill is computed from them. Date-based, and changed only when a customer
|
|
47
|
+
* could observe the difference. It is emitted into the generated pricing
|
|
48
|
+
* documentation so a stale copy is identifiable on sight.
|
|
49
|
+
*/
|
|
50
|
+
export const PRICING_VERSION = '2026-07-27';
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Resolve a stored plan string (`stripe_subscription.plan`) to a tier.
|
|
54
|
+
*
|
|
55
|
+
* Anything unrecognized resolves to `free` rather than throwing, so an unknown
|
|
56
|
+
* plan degrades to the safest caps instead of taking down the request path.
|
|
57
|
+
*
|
|
58
|
+
* There are deliberately no aliases. A name this enum does not carry belongs to
|
|
59
|
+
* some other product's tier vocabulary, and translating it here is how one
|
|
60
|
+
* product's subscription came to grant another product's entitlements. The
|
|
61
|
+
* caller filters to plans this contract names; see `resolveOrgTier`.
|
|
62
|
+
*/
|
|
63
|
+
export function toPlanTier(plan: string | null | undefined): PlanTier {
|
|
64
|
+
const raw = (plan ?? 'free').trim().toLowerCase();
|
|
65
|
+
return planTierSchema.catch('free').parse(raw);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The axes a bill is actually computed from: `ops` is metered, `connections` is
|
|
70
|
+
* capped. Storage is quoted but not billed, so it is not here.
|
|
71
|
+
*/
|
|
72
|
+
export const billableAxisSchema = z.enum(['ops', 'connections']);
|
|
73
|
+
export type BillableAxis = z.infer<typeof billableAxisSchema>;
|
|
74
|
+
|
|
75
|
+
/** Which billable axis each meter rolls into. `null` is recorded but unpriced. */
|
|
76
|
+
export const METER_EVENT_AXIS: Record<MeterEvent, BillableAxis | null> = {
|
|
77
|
+
'api.commit_ops': 'ops',
|
|
78
|
+
'api.model_reads': 'ops',
|
|
79
|
+
'api.claim_creates': 'ops',
|
|
80
|
+
'api.bootstraps': null,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** How each meter reads on an invoice line and in the pricing table. */
|
|
84
|
+
export const METER_EVENT_LABEL: Record<MeterEvent, string> = {
|
|
85
|
+
'api.commit_ops': 'commit operations',
|
|
86
|
+
'api.model_reads': 'model reads',
|
|
87
|
+
'api.claim_creates': 'claim creates',
|
|
88
|
+
'api.bootstraps': 'bootstraps',
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** The meters a customer is charged for, in declaration order. */
|
|
92
|
+
export const BILLABLE_METER_EVENTS: readonly MeterEvent[] = (
|
|
93
|
+
meterEventSchema.options as readonly MeterEvent[]
|
|
94
|
+
).filter((event) => METER_EVENT_AXIS[event] !== null);
|
|
95
|
+
|
|
96
|
+
export const planFeatureSchema = z.enum([
|
|
97
|
+
'sso',
|
|
98
|
+
'auditExport',
|
|
99
|
+
'cmek',
|
|
100
|
+
'privateNetworking',
|
|
101
|
+
'customCaps',
|
|
102
|
+
]);
|
|
103
|
+
export type PlanFeature = z.infer<typeof planFeatureSchema>;
|
|
104
|
+
|
|
105
|
+
/** How each feature reads on the pricing page. */
|
|
106
|
+
export const PLAN_FEATURE_LABEL: Record<PlanFeature, string> = {
|
|
107
|
+
sso: 'Single sign-on',
|
|
108
|
+
auditExport: 'Audit log export',
|
|
109
|
+
cmek: 'Customer-managed encryption keys',
|
|
110
|
+
privateNetworking: 'Private networking',
|
|
111
|
+
customCaps: 'Custom limits',
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The rate card, validated as a card rather than as three unrelated objects:
|
|
116
|
+
* ceilings ascend, rates never increase as volume grows, and exactly one
|
|
117
|
+
* unbounded step closes it. A malformed card fails at module load, which is the
|
|
118
|
+
* only moment it can fail safely.
|
|
119
|
+
*/
|
|
120
|
+
const rateCardSchema = z
|
|
121
|
+
.array(rateBracketSchema)
|
|
122
|
+
.min(1)
|
|
123
|
+
.superRefine((brackets, ctx) => {
|
|
124
|
+
let previousCeiling = 0;
|
|
125
|
+
let previousRate = Number.POSITIVE_INFINITY;
|
|
126
|
+
brackets.forEach((bracket, index) => {
|
|
127
|
+
const isLast = index === brackets.length - 1;
|
|
128
|
+
if (isLast !== (bracket.throughOps === null)) {
|
|
129
|
+
ctx.addIssue({
|
|
130
|
+
code: 'custom',
|
|
131
|
+
message: `bracket ${index}: only the final bracket may be unbounded`,
|
|
132
|
+
});
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (bracket.throughOps !== null) {
|
|
136
|
+
if (bracket.throughOps <= previousCeiling) {
|
|
137
|
+
ctx.addIssue({
|
|
138
|
+
code: 'custom',
|
|
139
|
+
message: `bracket ${index}: ceiling ${bracket.throughOps} does not exceed ${previousCeiling}`,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
previousCeiling = bracket.throughOps;
|
|
143
|
+
}
|
|
144
|
+
if (bracket.usdPerMillionOps > previousRate) {
|
|
145
|
+
ctx.addIssue({
|
|
146
|
+
code: 'custom',
|
|
147
|
+
message: `bracket ${index}: rate rises with volume`,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
previousRate = bracket.usdPerMillionOps;
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The published rate card for sync operations. These numbers are the tunable
|
|
156
|
+
* knob; the bracket structure is the contract.
|
|
157
|
+
*/
|
|
158
|
+
export const OPS_RATE_CARD: readonly RateBracket[] = rateCardSchema.parse([
|
|
159
|
+
{ throughOps: 50_000_000, usdPerMillionOps: 2.0 },
|
|
160
|
+
{ throughOps: 500_000_000, usdPerMillionOps: 1.2 },
|
|
161
|
+
{ throughOps: null, usdPerMillionOps: 0.6 },
|
|
162
|
+
]);
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* A tier. `null` on a floor, a cap, or a quota means negotiated rather than
|
|
166
|
+
* unlimited, and the runtime treats it as "no typed default, use the operator's
|
|
167
|
+
* configured value".
|
|
168
|
+
*/
|
|
169
|
+
export const planDefinitionSchema = z.object({
|
|
170
|
+
tier: planTierSchema,
|
|
171
|
+
label: z.string(),
|
|
172
|
+
/** One line on the pricing page saying who the tier is for. */
|
|
173
|
+
summary: z.string(),
|
|
174
|
+
/**
|
|
175
|
+
* The monthly floor in USD. Usage is charged against it, not added to it, so
|
|
176
|
+
* an organization pays this or its metered usage, whichever is greater.
|
|
177
|
+
* `null` means the floor is set in the contract.
|
|
178
|
+
*/
|
|
179
|
+
monthlyMinimumUsd: z.number().nonnegative().nullable(),
|
|
180
|
+
/**
|
|
181
|
+
* A hard monthly stop on operations. Set only where the tier is not billed at
|
|
182
|
+
* all: Free is capped rather than metered, so exceeding it is refused instead
|
|
183
|
+
* of invoiced. `null` on every metered tier.
|
|
184
|
+
*/
|
|
185
|
+
hardCapOps: z.number().int().positive().nullable(),
|
|
186
|
+
/**
|
|
187
|
+
* A hard daily stop, which is a burst guard rather than an allowance. A month
|
|
188
|
+
* of operations is a comfortable month of building and about ten minutes of
|
|
189
|
+
* one agent at full rate, so a monthly cap alone lets a runaway loop erase the
|
|
190
|
+
* whole month before anyone notices. The daily figure is what turns that into
|
|
191
|
+
* a floor on how fast the month can be spent. `null` on metered tiers, which
|
|
192
|
+
* are billed rather than stopped.
|
|
193
|
+
*/
|
|
194
|
+
hardCapOpsPerDay: z.number().int().positive().nullable(),
|
|
195
|
+
/** Quoted ceiling on stored data, in GiB. Not billed. `null` is negotiated. */
|
|
196
|
+
storageGib: z.number().positive().nullable(),
|
|
197
|
+
/** Hard concurrent-connection cap, the real Free to Scale boundary. */
|
|
198
|
+
maxConcurrentConnections: z.number().int().positive().nullable(),
|
|
199
|
+
/** True when the terms are set in a contract rather than published in full. */
|
|
200
|
+
contractPriced: z.boolean(),
|
|
201
|
+
features: z.array(planFeatureSchema),
|
|
202
|
+
});
|
|
203
|
+
export type PlanDefinition = z.infer<typeof planDefinitionSchema>;
|
|
204
|
+
|
|
205
|
+
export const PLANS = z
|
|
206
|
+
.object({
|
|
207
|
+
free: planDefinitionSchema,
|
|
208
|
+
scale: planDefinitionSchema,
|
|
209
|
+
enterprise: planDefinitionSchema,
|
|
210
|
+
})
|
|
211
|
+
.parse({
|
|
212
|
+
free: {
|
|
213
|
+
tier: 'free',
|
|
214
|
+
label: 'Free',
|
|
215
|
+
summary: 'Build against the real engine without a card.',
|
|
216
|
+
monthlyMinimumUsd: 0,
|
|
217
|
+
hardCapOps: 1_000_000,
|
|
218
|
+
hardCapOpsPerDay: 100_000,
|
|
219
|
+
storageGib: 1,
|
|
220
|
+
maxConcurrentConnections: 25,
|
|
221
|
+
contractPriced: false,
|
|
222
|
+
features: [],
|
|
223
|
+
},
|
|
224
|
+
scale: {
|
|
225
|
+
tier: 'scale',
|
|
226
|
+
label: 'Scale',
|
|
227
|
+
summary: 'Production traffic, metered above a monthly floor.',
|
|
228
|
+
monthlyMinimumUsd: 99,
|
|
229
|
+
hardCapOps: null,
|
|
230
|
+
hardCapOpsPerDay: null,
|
|
231
|
+
storageGib: 50,
|
|
232
|
+
maxConcurrentConnections: 1_000,
|
|
233
|
+
contractPriced: false,
|
|
234
|
+
features: ['auditExport'],
|
|
235
|
+
},
|
|
236
|
+
enterprise: {
|
|
237
|
+
tier: 'enterprise',
|
|
238
|
+
label: 'Enterprise',
|
|
239
|
+
summary: 'Committed volume, private networking, and an uptime guarantee.',
|
|
240
|
+
monthlyMinimumUsd: 2_000,
|
|
241
|
+
hardCapOps: null,
|
|
242
|
+
hardCapOpsPerDay: null,
|
|
243
|
+
storageGib: null,
|
|
244
|
+
maxConcurrentConnections: null,
|
|
245
|
+
contractPriced: true,
|
|
246
|
+
features: ['sso', 'auditExport', 'cmek', 'privateNetworking', 'customCaps'],
|
|
247
|
+
},
|
|
248
|
+
}) satisfies Record<PlanTier, PlanDefinition>;
|
|
249
|
+
|
|
250
|
+
/** The tiers in the order they are presented, cheapest first. */
|
|
251
|
+
export const PLAN_ORDER: readonly PlanTier[] = ['free', 'scale', 'enterprise'];
|
|
252
|
+
|
|
253
|
+
/** Round to whole cents. Money that is not rounded at the boundary is money
|
|
254
|
+
* that disagrees with the invoice by a fraction nobody can explain. */
|
|
255
|
+
function roundUsd(usd: number): number {
|
|
256
|
+
return Math.round(usd * 100) / 100;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* What a month of `ops` costs at the rate card, before any floor is applied.
|
|
261
|
+
* Marginal across brackets: the first 50 million are charged at the first rate
|
|
262
|
+
* whatever the total turns out to be.
|
|
263
|
+
*/
|
|
264
|
+
export function usdForOps(ops: number): number {
|
|
265
|
+
if (!Number.isFinite(ops) || ops <= 0) return 0;
|
|
266
|
+
let remaining = ops;
|
|
267
|
+
let previousCeiling = 0;
|
|
268
|
+
let usd = 0;
|
|
269
|
+
for (const bracket of OPS_RATE_CARD) {
|
|
270
|
+
const span =
|
|
271
|
+
bracket.throughOps === null
|
|
272
|
+
? Number.POSITIVE_INFINITY
|
|
273
|
+
: bracket.throughOps - previousCeiling;
|
|
274
|
+
const charged = Math.min(remaining, span);
|
|
275
|
+
usd += (charged / 1_000_000) * bracket.usdPerMillionOps;
|
|
276
|
+
remaining -= charged;
|
|
277
|
+
if (remaining <= 0) break;
|
|
278
|
+
if (bracket.throughOps !== null) previousCeiling = bracket.throughOps;
|
|
279
|
+
}
|
|
280
|
+
return roundUsd(usd);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The inverse: how many operations a given spend buys at the rate card. This is
|
|
285
|
+
* what turns a floor into the allowance a tier advertises, which is why the
|
|
286
|
+
* allowance can never drift from the floor.
|
|
287
|
+
*/
|
|
288
|
+
export function opsForUsd(usd: number): number {
|
|
289
|
+
if (!Number.isFinite(usd) || usd <= 0) return 0;
|
|
290
|
+
let remaining = usd;
|
|
291
|
+
let previousCeiling = 0;
|
|
292
|
+
let ops = 0;
|
|
293
|
+
for (const bracket of OPS_RATE_CARD) {
|
|
294
|
+
const span =
|
|
295
|
+
bracket.throughOps === null
|
|
296
|
+
? Number.POSITIVE_INFINITY
|
|
297
|
+
: bracket.throughOps - previousCeiling;
|
|
298
|
+
if (bracket.usdPerMillionOps === 0) return Number.POSITIVE_INFINITY;
|
|
299
|
+
const spanCost = (span / 1_000_000) * bracket.usdPerMillionOps;
|
|
300
|
+
if (remaining <= spanCost) {
|
|
301
|
+
return Math.floor(ops + (remaining / bracket.usdPerMillionOps) * 1_000_000);
|
|
302
|
+
}
|
|
303
|
+
ops += span;
|
|
304
|
+
remaining -= spanCost;
|
|
305
|
+
if (bracket.throughOps !== null) previousCeiling = bracket.throughOps;
|
|
306
|
+
}
|
|
307
|
+
return Math.floor(ops);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* The operations a tier includes, as shown on the pricing page. A capped tier
|
|
312
|
+
* shows its cap; a metered tier shows what its floor covers, computed from the
|
|
313
|
+
* floor rather than stored beside it. `null` where the terms are contractual.
|
|
314
|
+
*/
|
|
315
|
+
export function monthlyOpsAllowance(tier: PlanTier): number | null {
|
|
316
|
+
const plan = PLANS[tier];
|
|
317
|
+
if (plan.hardCapOps !== null) return plan.hardCapOps;
|
|
318
|
+
if (plan.contractPriced || plan.monthlyMinimumUsd === null) return null;
|
|
319
|
+
return opsForUsd(plan.monthlyMinimumUsd);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The invoice for a month: the floor, or the metered usage, whichever is
|
|
324
|
+
* greater. A capped tier is never billed above its floor because the cap is
|
|
325
|
+
* enforced before the usage exists. `null` where the terms are contractual.
|
|
326
|
+
*/
|
|
327
|
+
export function monthlyBillUsd(tier: PlanTier, ops: number): number | null {
|
|
328
|
+
const plan = PLANS[tier];
|
|
329
|
+
if (plan.contractPriced || plan.monthlyMinimumUsd === null) return null;
|
|
330
|
+
if (plan.hardCapOps !== null) return plan.monthlyMinimumUsd;
|
|
331
|
+
return roundUsd(Math.max(plan.monthlyMinimumUsd, usdForOps(ops)));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* The daily operations cap for a tier: the typed default the quota gate falls
|
|
336
|
+
* back to when no `plan_limit` row sets one. `null` means the tier is metered
|
|
337
|
+
* rather than stopped.
|
|
338
|
+
*/
|
|
339
|
+
export function dailyOpsCapForTier(tier: PlanTier): number | null {
|
|
340
|
+
return PLANS[tier].hardCapOpsPerDay;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* The concurrent-connection cap for a tier: the typed default the Hub's
|
|
345
|
+
* resolver falls back to when no `plan_limit` override row exists. `null` means
|
|
346
|
+
* negotiated, which the Hub reads as its configured floor.
|
|
347
|
+
*/
|
|
348
|
+
export function connectionCapForTier(tier: PlanTier): number | null {
|
|
349
|
+
return PLANS[tier].maxConcurrentConnections;
|
|
350
|
+
}
|
|
@@ -53,6 +53,17 @@ export interface ContentionOptions {
|
|
|
53
53
|
/** Reconcile rounds before a hot row is declared permanently contended. */
|
|
54
54
|
export const DEFAULT_CONTENTION_RETRIES = 16;
|
|
55
55
|
|
|
56
|
+
const sleep = (ms: number): Promise<void> =>
|
|
57
|
+
new Promise((resolve) => setTimeout(resolve, ms));
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Jittered backoff so N reconcilers retrying at once don't lock-step straight
|
|
61
|
+
* back into the same collision. Bounded; grows mildly with the attempt.
|
|
62
|
+
*/
|
|
63
|
+
function backoffMs(attempt: number): number {
|
|
64
|
+
return 60 + attempt * 40 + Math.floor(Math.random() * 60);
|
|
65
|
+
}
|
|
66
|
+
|
|
56
67
|
/**
|
|
57
68
|
* Reports whether a thrown error means "another writer moved the row — re-read
|
|
58
69
|
* and retry" rather than a genuine failure to surface. These are the
|
|
@@ -69,17 +80,6 @@ export function isReconcilableConflict(err: unknown): boolean {
|
|
|
69
80
|
return false;
|
|
70
81
|
}
|
|
71
82
|
|
|
72
|
-
const sleep = (ms: number): Promise<void> =>
|
|
73
|
-
new Promise((resolve) => setTimeout(resolve, ms));
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Jittered backoff so N reconcilers retrying at once don't lock-step straight
|
|
77
|
-
* back into the same collision. Bounded; grows mildly with the attempt.
|
|
78
|
-
*/
|
|
79
|
-
function backoffMs(attempt: number): number {
|
|
80
|
-
return 60 + attempt * 40 + Math.floor(Math.random() * 60);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
83
|
/**
|
|
84
84
|
* The transport-specific read and write that the shared loop drives. Each client
|
|
85
85
|
* injects its own pair — the one thing that differs between the HTTP and
|
package/src/schema/index.ts
CHANGED
|
@@ -170,6 +170,7 @@ export {
|
|
|
170
170
|
type RoleSource,
|
|
171
171
|
type RoleContext,
|
|
172
172
|
type SyncGroup,
|
|
173
|
+
type SyncGroupRef,
|
|
173
174
|
type SyncGroupInput,
|
|
174
175
|
identityRole,
|
|
175
176
|
entityRole,
|
|
@@ -177,6 +178,7 @@ export {
|
|
|
177
178
|
extractEntityIds,
|
|
178
179
|
syncGroup,
|
|
179
180
|
syncGroupSchema,
|
|
181
|
+
syncGroupRefSchema,
|
|
180
182
|
syncGroupInputSchema,
|
|
181
183
|
isSyncGroupInput,
|
|
182
184
|
identityRoleSchema,
|
|
@@ -206,7 +208,7 @@ export {
|
|
|
206
208
|
} from './serialize.js';
|
|
207
209
|
|
|
208
210
|
// Schema projection — derive an app's subset from one canonical schema.
|
|
209
|
-
export { selectModels, omitModels } from './select.js';
|
|
211
|
+
export { selectModels, omitModels, omittedModelError } from './select.js';
|
|
210
212
|
|
|
211
213
|
// Schema → Postgres DDL — shared by the host implementation and the command-line tools.
|
|
212
214
|
export {
|
package/src/schema/roles.ts
CHANGED
|
@@ -31,9 +31,32 @@ import { z } from 'zod';
|
|
|
31
31
|
* {@link syncGroup}. Because the brand is an intersection it's still assignable
|
|
32
32
|
* *to* `string`, so existing `string[]` plumbing keeps working unchanged.
|
|
33
33
|
*/
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
/**
|
|
35
|
+
* The `kind:id` shape itself, unbranded — what a CALLER writes.
|
|
36
|
+
*
|
|
37
|
+
* Input and output want different strictness from one format, so the format is
|
|
38
|
+
* declared once here and viewed two ways. This view rejects `'nonsense'` at
|
|
39
|
+
* compile time (no colon ⇒ matches no group) while still accepting
|
|
40
|
+
* `` `org:${orgId}` `` written inline, which is the whole point of
|
|
41
|
+
* {@link SyncGroupInput}. Requiring the constructor on an input field buys no
|
|
42
|
+
* safety this does not already give and costs every call site a helper import.
|
|
43
|
+
*/
|
|
44
|
+
export const syncGroupRefSchema = z.templateLiteral([
|
|
45
|
+
z.string().regex(/^[a-z][a-z0-9_]*$/),
|
|
46
|
+
':',
|
|
47
|
+
z.string().min(1),
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
export type SyncGroupRef = z.infer<typeof syncGroupRefSchema>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The same format, branded — what the SERVER hands back.
|
|
54
|
+
*
|
|
55
|
+
* The brand earns its keep on the way out, where Ablo is the author: a consumer
|
|
56
|
+
* that receives a `SyncGroup` knows it was minted, and a raw string cannot
|
|
57
|
+
* masquerade as one.
|
|
58
|
+
*/
|
|
59
|
+
export const syncGroupSchema = syncGroupRefSchema.brand<'SyncGroup'>();
|
|
37
60
|
|
|
38
61
|
export type SyncGroup = z.infer<typeof syncGroupSchema>;
|
|
39
62
|
|
package/src/schema/schema.ts
CHANGED
|
@@ -41,6 +41,7 @@ export {
|
|
|
41
41
|
type RoleSource,
|
|
42
42
|
type RoleContext,
|
|
43
43
|
type SyncGroup,
|
|
44
|
+
type SyncGroupRef,
|
|
44
45
|
type SyncGroupInput,
|
|
45
46
|
identityRole,
|
|
46
47
|
entityRole,
|
|
@@ -51,6 +52,7 @@ export {
|
|
|
51
52
|
intersectRequestedWithAllowed,
|
|
52
53
|
syncGroup,
|
|
53
54
|
syncGroupSchema,
|
|
55
|
+
syncGroupRefSchema,
|
|
54
56
|
syncGroupInputSchema,
|
|
55
57
|
isSyncGroupInput,
|
|
56
58
|
identityRoleSchema,
|
|
@@ -315,6 +317,19 @@ export interface Schema<S extends SchemaRecord = SchemaRecord> {
|
|
|
315
317
|
* stamping it never perturbs `schemaHash`.
|
|
316
318
|
*/
|
|
317
319
|
readonly sourceSchemaHash?: string;
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Set only on a projection produced by `selectModels`/`omitModels`: the
|
|
323
|
+
* source-schema model keys the projection dropped, sorted. Clients bound to a
|
|
324
|
+
* projection use this to answer an access to a left-out model with a real
|
|
325
|
+
* error naming the model and the fix, instead of `undefined` and a TypeError
|
|
326
|
+
* three frames later. The distinction matters because an app can compile
|
|
327
|
+
* against the full source schema while running a projection, so the type
|
|
328
|
+
* system never sees the gap. Absent on a schema authored directly, where an
|
|
329
|
+
* unknown property is a typo the types already catch. Excluded from
|
|
330
|
+
* `toSchemaJSON`, so stamping it never perturbs `schemaHash`.
|
|
331
|
+
*/
|
|
332
|
+
readonly omittedModels?: readonly string[];
|
|
318
333
|
}
|
|
319
334
|
|
|
320
335
|
// ── Type inference (powered by Zod) ───────────────────────────────────────
|
package/src/schema/select.ts
CHANGED
|
@@ -80,9 +80,39 @@ export function selectModels<S extends SchemaRecord, K extends keyof S & string>
|
|
|
80
80
|
// subset still points at the original full schema rather than an intermediate
|
|
81
81
|
// one. `schemaHash` ignores this field, so re-projecting stays deterministic.
|
|
82
82
|
sourceSchemaHash: schema.sourceSchemaHash ?? schemaHash(schema),
|
|
83
|
+
// What was dropped, unioned with what the source had already dropped, so a
|
|
84
|
+
// subset-of-a-subset still names every model of the original full schema
|
|
85
|
+
// that this client cannot reach. Clients turn these names into throwing
|
|
86
|
+
// accessors — see `omittedModelError`.
|
|
87
|
+
omittedModels: [
|
|
88
|
+
...new Set([
|
|
89
|
+
...(schema.omittedModels ?? []),
|
|
90
|
+
...Object.keys(schema.models).filter((k) => !keep.has(k)),
|
|
91
|
+
]),
|
|
92
|
+
].sort(),
|
|
83
93
|
};
|
|
84
94
|
}
|
|
85
95
|
|
|
96
|
+
/**
|
|
97
|
+
* The error a client raises when a caller reaches for a model its schema
|
|
98
|
+
* projection left out.
|
|
99
|
+
*
|
|
100
|
+
* Defined beside the projection because the two are one contract: what
|
|
101
|
+
* `selectModels` records in `omittedModels`, a client answers with this error.
|
|
102
|
+
* Without it the access reads as `undefined` and the caller crashes one
|
|
103
|
+
* property later with a bare TypeError that never names the model or the fix —
|
|
104
|
+
* which is exactly what happens when an app compiles against the full source
|
|
105
|
+
* schema but runs a projection, the one misuse the type system cannot see.
|
|
106
|
+
*/
|
|
107
|
+
export function omittedModelError(model: string): AbloValidationError {
|
|
108
|
+
return new AbloValidationError(
|
|
109
|
+
`The ${model} model is not in this client's schema. ` +
|
|
110
|
+
`The schema is a projection that leaves ${model} out, so this client cannot read or write it. ` +
|
|
111
|
+
`Add '${model}' to the projection's model list to use it here.`,
|
|
112
|
+
{ code: 'model_not_in_schema', param: model },
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
86
116
|
/**
|
|
87
117
|
* `omitModels` is `selectModels` from the other side: keep every model EXCEPT
|
|
88
118
|
* the named ones. Use it when an app is the general case and a separate app
|
|
@@ -45,6 +45,7 @@ import type {
|
|
|
45
45
|
ModelUpdateParams,
|
|
46
46
|
} from '../resources/modelOperations.js';
|
|
47
47
|
import type { Schema, SchemaRecord, InferModel, InferCreate } from '../schema/schema.js';
|
|
48
|
+
import { omittedModelError } from '../schema/select.js';
|
|
48
49
|
import type { ModelUpdater, ContentionOptions } from '../resources/functionalUpdate.js';
|
|
49
50
|
import { AbloConnectionError, AbloValidationError } from '../errors.js';
|
|
50
51
|
|
|
@@ -319,6 +320,7 @@ export function createAbloHttpClient<S extends SchemaRecord>(
|
|
|
319
320
|
modelTypenames: modelWireNames(schema.models),
|
|
320
321
|
});
|
|
321
322
|
const schemaModels = new Set(Object.keys(schema.models));
|
|
323
|
+
const omittedModels = new Set(schema.omittedModels ?? []);
|
|
322
324
|
const models = new Map<
|
|
323
325
|
string,
|
|
324
326
|
HttpModelClient<Record<string, unknown>, Record<string, unknown>>
|
|
@@ -343,7 +345,12 @@ export function createAbloHttpClient<S extends SchemaRecord>(
|
|
|
343
345
|
}
|
|
344
346
|
// Only schema models become model accessors. A typo or retired top-level
|
|
345
347
|
// member resolves to undefined instead of manufacturing a plausible client.
|
|
346
|
-
|
|
348
|
+
if (schemaModels.has(prop)) return model(prop);
|
|
349
|
+
// A model the schema projection left out is neither a typo nor a member:
|
|
350
|
+
// the caller compiled against the full source schema, so answer with the
|
|
351
|
+
// error that names the model and the fix rather than `undefined`.
|
|
352
|
+
if (omittedModels.has(prop)) throw omittedModelError(prop);
|
|
353
|
+
return undefined;
|
|
347
354
|
},
|
|
348
355
|
});
|
|
349
356
|
|
|
@@ -180,10 +180,16 @@ const handleMutationResult: WsFrameHandler = (session, message) => {
|
|
|
180
180
|
const event = {
|
|
181
181
|
clientTxId: txId,
|
|
182
182
|
rows: receipt.notifications.map((n) => ({
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
183
|
+
// The row that moved, in both scopes. A group notification used to
|
|
184
|
+
// report its group key here, so the log named a row that does not
|
|
185
|
+
// exist; the premise it breached is reported as `group` instead.
|
|
186
|
+
model: n.target.model,
|
|
187
|
+
id: n.target.id,
|
|
188
|
+
fields: n.target.fields ?? [],
|
|
186
189
|
writtenBy: n.writtenBy.kind,
|
|
190
|
+
...(n.scope === 'group'
|
|
191
|
+
? { group: n.group, ...(n.propagation ? { via: n.propagation.via } : {}) }
|
|
192
|
+
: {}),
|
|
187
193
|
})),
|
|
188
194
|
};
|
|
189
195
|
const message = formatConflict(event);
|