@party-stack/ontology-backend 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +148 -2
- package/lib/actions.d.ts +47 -0
- package/lib/actions.d.ts.map +1 -0
- package/lib/actions.js +306 -0
- package/lib/actions.js.map +1 -0
- package/lib/clients.d.ts +236 -0
- package/lib/clients.d.ts.map +1 -0
- package/lib/clients.js +211 -0
- package/lib/clients.js.map +1 -0
- package/lib/delivery.d.ts +746 -0
- package/lib/delivery.d.ts.map +1 -0
- package/lib/delivery.js +863 -0
- package/lib/delivery.js.map +1 -0
- package/lib/description.d.ts +29 -0
- package/lib/description.d.ts.map +1 -0
- package/lib/description.js +28 -0
- package/lib/description.js.map +1 -0
- package/lib/encrypted.d.ts +41 -0
- package/lib/encrypted.d.ts.map +1 -0
- package/lib/encrypted.js +111 -0
- package/lib/encrypted.js.map +1 -0
- package/lib/errors.d.ts +106 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +275 -0
- package/lib/errors.js.map +1 -0
- package/lib/fulfillment.d.ts +25 -0
- package/lib/fulfillment.d.ts.map +1 -0
- package/lib/fulfillment.js +99 -0
- package/lib/fulfillment.js.map +1 -0
- package/lib/index.d.ts +297 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +91 -0
- package/lib/index.js.map +1 -0
- package/lib/maintenance.d.ts +11 -0
- package/lib/maintenance.d.ts.map +1 -0
- package/lib/maintenance.js +24 -0
- package/lib/maintenance.js.map +1 -0
- package/lib/meta.d.ts +26 -0
- package/lib/meta.d.ts.map +1 -0
- package/lib/meta.js +213 -0
- package/lib/meta.js.map +1 -0
- package/lib/owned.d.ts +74 -0
- package/lib/owned.d.ts.map +1 -0
- package/lib/owned.js +593 -0
- package/lib/owned.js.map +1 -0
- package/lib/policy/indexes.d.ts +12 -0
- package/lib/policy/indexes.d.ts.map +1 -0
- package/lib/policy/indexes.js +113 -0
- package/lib/policy/indexes.js.map +1 -0
- package/lib/policy/planner.d.ts +10 -0
- package/lib/policy/planner.d.ts.map +1 -0
- package/lib/policy/planner.js +118 -0
- package/lib/policy/planner.js.map +1 -0
- package/lib/policy/view.d.ts +31 -0
- package/lib/policy/view.d.ts.map +1 -0
- package/lib/policy/view.js +173 -0
- package/lib/policy/view.js.map +1 -0
- package/lib/policy.d.ts +47 -0
- package/lib/policy.d.ts.map +1 -0
- package/lib/policy.js +100 -0
- package/lib/policy.js.map +1 -0
- package/lib/protocol.d.ts +570 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +144 -0
- package/lib/protocol.js.map +1 -0
- package/lib/query.d.ts +33 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +51 -0
- package/lib/query.js.map +1 -0
- package/lib/references.d.ts +8 -0
- package/lib/references.d.ts.map +1 -0
- package/lib/references.js +41 -0
- package/lib/references.js.map +1 -0
- package/lib/storage.d.ts +55 -0
- package/lib/storage.d.ts.map +1 -0
- package/lib/storage.js +168 -0
- package/lib/storage.js.map +1 -0
- package/lib/subset.d.ts +19 -0
- package/lib/subset.d.ts.map +1 -0
- package/lib/subset.js +150 -0
- package/lib/subset.js.map +1 -0
- package/lib/subsetSchema.d.ts +10 -0
- package/lib/subsetSchema.d.ts.map +1 -0
- package/lib/subsetSchema.js +20 -0
- package/lib/subsetSchema.js.map +1 -0
- package/package.json +72 -3
package/lib/clients.d.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { BackendError } from "./errors.js";
|
|
2
|
+
import "./index.js";
|
|
3
|
+
import { ClientView } from "./protocol.js";
|
|
4
|
+
import { OntologyStorageError } from "@party-stack/ontology-storage";
|
|
5
|
+
import { Effect, Schema } from "effect";
|
|
6
|
+
import { Table, Transaction } from "@party-stack/ontology-storage/tables";
|
|
7
|
+
//#region src/clients.d.ts
|
|
8
|
+
export declare const ClientTokenSchema: Schema.Struct<{
|
|
9
|
+
readonly sourceId: Schema.NonEmptyString;
|
|
10
|
+
readonly clientId: Schema.NonEmptyString;
|
|
11
|
+
readonly resumeKey: Schema.NonEmptyString;
|
|
12
|
+
}>;
|
|
13
|
+
export type ClientToken = typeof ClientTokenSchema.Type;
|
|
14
|
+
export declare const ClientSessionSchema: Schema.Struct<{
|
|
15
|
+
readonly client: Schema.Struct<{
|
|
16
|
+
readonly sourceId: Schema.NonEmptyString;
|
|
17
|
+
readonly clientId: Schema.NonEmptyString;
|
|
18
|
+
readonly resumeKey: Schema.NonEmptyString;
|
|
19
|
+
}>;
|
|
20
|
+
readonly connectionId: Schema.NonEmptyString;
|
|
21
|
+
}>;
|
|
22
|
+
export type ClientSession = typeof ClientSessionSchema.Type;
|
|
23
|
+
declare const ClientRecordSchema: Schema.Struct<{
|
|
24
|
+
readonly client: Schema.Struct<{
|
|
25
|
+
readonly sourceId: Schema.NonEmptyString;
|
|
26
|
+
readonly clientId: Schema.NonEmptyString;
|
|
27
|
+
readonly resumeKey: Schema.NonEmptyString;
|
|
28
|
+
}>;
|
|
29
|
+
readonly connectionId: Schema.NonEmptyString;
|
|
30
|
+
readonly expiresAt: Schema.Finite;
|
|
31
|
+
readonly acceptedThrough: Schema.Int;
|
|
32
|
+
readonly acknowledgedThrough: Schema.Int;
|
|
33
|
+
readonly deliveredThrough: Schema.Int;
|
|
34
|
+
readonly deliveredRevision: Schema.Int;
|
|
35
|
+
readonly appliedRevision: Schema.Int;
|
|
36
|
+
readonly viewGeneration: Schema.optional<Schema.Int>;
|
|
37
|
+
readonly viewIdentity: Schema.optional<Schema.String>;
|
|
38
|
+
readonly deliveredViewGeneration: Schema.optional<Schema.Int>;
|
|
39
|
+
}>;
|
|
40
|
+
export declare const ClientLeaseSchema: Schema.Struct<{
|
|
41
|
+
readonly client: Schema.Struct<{
|
|
42
|
+
readonly sourceId: Schema.NonEmptyString;
|
|
43
|
+
readonly clientId: Schema.NonEmptyString;
|
|
44
|
+
readonly resumeKey: Schema.NonEmptyString;
|
|
45
|
+
}>;
|
|
46
|
+
readonly connectionId: Schema.NonEmptyString;
|
|
47
|
+
readonly expiresAt: Schema.Finite;
|
|
48
|
+
readonly acceptedThrough: Schema.Int;
|
|
49
|
+
readonly acknowledgedThrough: Schema.Int;
|
|
50
|
+
readonly deliveredThrough: Schema.Int;
|
|
51
|
+
readonly deliveredRevision: Schema.Int;
|
|
52
|
+
readonly appliedRevision: Schema.Int;
|
|
53
|
+
readonly viewGeneration: Schema.optional<Schema.Int>;
|
|
54
|
+
readonly viewIdentity: Schema.optional<Schema.String>;
|
|
55
|
+
readonly deliveredViewGeneration: Schema.optional<Schema.Int>;
|
|
56
|
+
}>;
|
|
57
|
+
export type ClientLease = typeof ClientRecordSchema.Type;
|
|
58
|
+
export interface ClientPolicy {
|
|
59
|
+
/** Retain disconnected selected delivery independently of action recovery. Default 60 seconds. */
|
|
60
|
+
readonly deliveryRetentionMs?: number;
|
|
61
|
+
/** Inactivity before a resumable client session expires. Validated requests renew it.
|
|
62
|
+
* Defaults to seven days; expiry never discards accepted pending actions. */
|
|
63
|
+
readonly sessionIdleTimeoutMs?: number;
|
|
64
|
+
/** Retained unexpired sessions, including disconnected resumable clients. Defaults to 256. */
|
|
65
|
+
readonly maxSessions?: number;
|
|
66
|
+
/** Per-session accepted actions not yet acknowledged by the child. Defaults to 1,024.
|
|
67
|
+
* This bounds submission bookkeeping, not the parent's entire pending outbox. */
|
|
68
|
+
readonly maxUnacknowledgedActions?: number;
|
|
69
|
+
}
|
|
70
|
+
export interface ClientRegistryHooks {
|
|
71
|
+
/** Release only this client's retained delivery/results, never accepted work. */
|
|
72
|
+
readonly onExpire?: (tx: Transaction, clientId: string) => Effect.Effect<void, BackendError | OntologyStorageError>;
|
|
73
|
+
}
|
|
74
|
+
/** Transaction-local admission bookkeeping. The caller must use the SAME tx for
|
|
75
|
+
* admit's callback and source state. Neither reconnect nor expiration executes actions.
|
|
76
|
+
*/
|
|
77
|
+
export declare const clientRegistry: (namespace: string, sourceId: string, policy?: ClientPolicy, hooks?: ClientRegistryHooks) => {
|
|
78
|
+
initialize: (tx: Transaction) => Effect.Effect<void, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced"> | OntologyStorageError, never>;
|
|
79
|
+
open: (tx: Transaction, resume?: {
|
|
80
|
+
readonly sourceId: string;
|
|
81
|
+
readonly clientId: string;
|
|
82
|
+
readonly resumeKey: string;
|
|
83
|
+
} | undefined) => Effect.Effect<{
|
|
84
|
+
readonly client: {
|
|
85
|
+
readonly sourceId: string;
|
|
86
|
+
readonly clientId: string;
|
|
87
|
+
readonly resumeKey: string;
|
|
88
|
+
};
|
|
89
|
+
readonly connectionId: string;
|
|
90
|
+
readonly expiresAt: number;
|
|
91
|
+
readonly acceptedThrough: number;
|
|
92
|
+
readonly acknowledgedThrough: number;
|
|
93
|
+
readonly deliveredThrough: number;
|
|
94
|
+
readonly deliveredRevision: number;
|
|
95
|
+
readonly appliedRevision: number;
|
|
96
|
+
readonly viewGeneration?: number | undefined;
|
|
97
|
+
readonly viewIdentity?: string | undefined;
|
|
98
|
+
readonly deliveredViewGeneration?: number | undefined;
|
|
99
|
+
}, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced"> | OntologyStorageError, never>;
|
|
100
|
+
validate: (tx: Transaction, session: {
|
|
101
|
+
readonly client: {
|
|
102
|
+
readonly sourceId: string;
|
|
103
|
+
readonly clientId: string;
|
|
104
|
+
readonly resumeKey: string;
|
|
105
|
+
};
|
|
106
|
+
readonly connectionId: string;
|
|
107
|
+
}) => Effect.Effect<{
|
|
108
|
+
client: {
|
|
109
|
+
readonly sourceId: string;
|
|
110
|
+
readonly clientId: string;
|
|
111
|
+
readonly resumeKey: string;
|
|
112
|
+
};
|
|
113
|
+
connectionId: string;
|
|
114
|
+
acceptedThrough: number;
|
|
115
|
+
acknowledgedThrough: number;
|
|
116
|
+
deliveredThrough: number;
|
|
117
|
+
deliveredRevision: number;
|
|
118
|
+
appliedRevision: number;
|
|
119
|
+
viewGeneration?: number | undefined;
|
|
120
|
+
viewIdentity?: string | undefined;
|
|
121
|
+
deliveredViewGeneration?: number | undefined;
|
|
122
|
+
expiresAt: number;
|
|
123
|
+
}, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced"> | OntologyStorageError, never>;
|
|
124
|
+
select: (tx: Transaction, session: {
|
|
125
|
+
readonly client: {
|
|
126
|
+
readonly sourceId: string;
|
|
127
|
+
readonly clientId: string;
|
|
128
|
+
readonly resumeKey: string;
|
|
129
|
+
};
|
|
130
|
+
readonly connectionId: string;
|
|
131
|
+
}, view?: ClientView | undefined) => Effect.Effect<{
|
|
132
|
+
client: {
|
|
133
|
+
readonly sourceId: string;
|
|
134
|
+
readonly clientId: string;
|
|
135
|
+
readonly resumeKey: string;
|
|
136
|
+
};
|
|
137
|
+
connectionId: string;
|
|
138
|
+
acceptedThrough: number;
|
|
139
|
+
acknowledgedThrough: number;
|
|
140
|
+
deliveredThrough: number;
|
|
141
|
+
deliveredRevision: number;
|
|
142
|
+
appliedRevision: number;
|
|
143
|
+
deliveredViewGeneration?: number | undefined;
|
|
144
|
+
expiresAt: number;
|
|
145
|
+
viewGeneration: number;
|
|
146
|
+
viewIdentity: string;
|
|
147
|
+
}, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced"> | OntologyStorageError, never>;
|
|
148
|
+
admit: <A, E>(tx: Transaction, session: {
|
|
149
|
+
readonly client: {
|
|
150
|
+
readonly sourceId: string;
|
|
151
|
+
readonly clientId: string;
|
|
152
|
+
readonly resumeKey: string;
|
|
153
|
+
};
|
|
154
|
+
readonly connectionId: string;
|
|
155
|
+
}, sequence: number, fingerprint: string, accept: () => Effect.Effect<A, E>) => Effect.Effect<{
|
|
156
|
+
duplicate: true;
|
|
157
|
+
value?: undefined;
|
|
158
|
+
} | {
|
|
159
|
+
duplicate: false;
|
|
160
|
+
value: A;
|
|
161
|
+
}, E | BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced"> | OntologyStorageError, never>;
|
|
162
|
+
deliver: (tx: Transaction, session: {
|
|
163
|
+
readonly client: {
|
|
164
|
+
readonly sourceId: string;
|
|
165
|
+
readonly clientId: string;
|
|
166
|
+
readonly resumeKey: string;
|
|
167
|
+
};
|
|
168
|
+
readonly connectionId: string;
|
|
169
|
+
}, revision: number) => Effect.Effect<{
|
|
170
|
+
client: {
|
|
171
|
+
readonly sourceId: string;
|
|
172
|
+
readonly clientId: string;
|
|
173
|
+
readonly resumeKey: string;
|
|
174
|
+
};
|
|
175
|
+
connectionId: string;
|
|
176
|
+
acceptedThrough: number;
|
|
177
|
+
acknowledgedThrough: number;
|
|
178
|
+
appliedRevision: number;
|
|
179
|
+
viewGeneration?: number | undefined;
|
|
180
|
+
viewIdentity?: string | undefined;
|
|
181
|
+
expiresAt: number;
|
|
182
|
+
deliveredRevision: number;
|
|
183
|
+
deliveredThrough: number;
|
|
184
|
+
deliveredViewGeneration: number;
|
|
185
|
+
}, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced"> | OntologyStorageError, never>;
|
|
186
|
+
acknowledge: (tx: Transaction, session: {
|
|
187
|
+
readonly client: {
|
|
188
|
+
readonly sourceId: string;
|
|
189
|
+
readonly clientId: string;
|
|
190
|
+
readonly resumeKey: string;
|
|
191
|
+
};
|
|
192
|
+
readonly connectionId: string;
|
|
193
|
+
}, revision: number, through: number, viewGeneration?: any) => Effect.Effect<{
|
|
194
|
+
client: {
|
|
195
|
+
readonly sourceId: string;
|
|
196
|
+
readonly clientId: string;
|
|
197
|
+
readonly resumeKey: string;
|
|
198
|
+
};
|
|
199
|
+
connectionId: string;
|
|
200
|
+
acceptedThrough: number;
|
|
201
|
+
acknowledgedThrough: number;
|
|
202
|
+
deliveredThrough: number;
|
|
203
|
+
deliveredRevision: number;
|
|
204
|
+
appliedRevision: number;
|
|
205
|
+
viewGeneration?: number | undefined;
|
|
206
|
+
viewIdentity?: string | undefined;
|
|
207
|
+
deliveredViewGeneration?: number | undefined;
|
|
208
|
+
expiresAt: number;
|
|
209
|
+
}, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced"> | OntologyStorageError, never>;
|
|
210
|
+
expire: (tx: Transaction, limit?: any) => Effect.Effect<string[], BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced"> | OntologyStorageError, never>;
|
|
211
|
+
clients: Table<{
|
|
212
|
+
readonly client: {
|
|
213
|
+
readonly sourceId: string;
|
|
214
|
+
readonly clientId: string;
|
|
215
|
+
readonly resumeKey: string;
|
|
216
|
+
};
|
|
217
|
+
readonly connectionId: string;
|
|
218
|
+
readonly expiresAt: number;
|
|
219
|
+
readonly acceptedThrough: number;
|
|
220
|
+
readonly acknowledgedThrough: number;
|
|
221
|
+
readonly deliveredThrough: number;
|
|
222
|
+
readonly deliveredRevision: number;
|
|
223
|
+
readonly appliedRevision: number;
|
|
224
|
+
readonly viewGeneration?: number | undefined;
|
|
225
|
+
readonly viewIdentity?: string | undefined;
|
|
226
|
+
readonly deliveredViewGeneration?: number | undefined;
|
|
227
|
+
}>;
|
|
228
|
+
submissions: Table<{
|
|
229
|
+
clientId: string;
|
|
230
|
+
sequence: number;
|
|
231
|
+
fingerprint: string;
|
|
232
|
+
}>;
|
|
233
|
+
};
|
|
234
|
+
//#endregion
|
|
235
|
+
export {};
|
|
236
|
+
//# sourceMappingURL=clients.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clients.d.ts","names":[],"sources":["../src/clients.ts"],"mappings":";;;;;;;qBASa,mBAAiB,OAAA;;;;;YAKlB,qBAAqB,kBAAkB;qBACtC,qBAAmB,OAAA;;;;;;;;YAIpB,uBAAuB,oBAAoB;cACjD,oBAAkB,OAAA;;;;;;;;;;;;;;;;;qBAYX,mBAAiB,OAAA;;;;;;;;;;;;;;;;;YAClB,qBAAqB,mBAAmB;iBACnC;;WAEJ;;;WAGA;;WAEA;;;WAGA;;iBAEI;;WAEJ,YACL,IAAI,aACJ,qBACC,OAAO,aAAa,eAAe;;;;;qBAa/B,iBAAc,mBACN,kBACD,SACR,cAAY,QACb;EAgCiB,aAAA,IAAA,gBAAW,OAAA,aAAA,kWAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAmF6B,QAAA,GAAG,GAAC,IAAA,aAAA;;;;;;;KAKlD,kBAAA,qBAAA,cAAA,OAAO,OAAO,GAAG,OAAE,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA5GM;IAAkB;IAAqB"}
|
package/lib/clients.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { BackendError } from "./errors.js";
|
|
2
|
+
import "./index.js";
|
|
3
|
+
import { and, eq, field, lte } from "@party-stack/ontology-storage";
|
|
4
|
+
import { Clock, Effect, Schema } from "effect";
|
|
5
|
+
import { Table } from "@party-stack/ontology-storage/tables";
|
|
6
|
+
//#region src/clients.ts
|
|
7
|
+
const Counter = Schema.Int.check(Schema.isBetween({
|
|
8
|
+
minimum: 0,
|
|
9
|
+
maximum: Number.MAX_SAFE_INTEGER
|
|
10
|
+
}));
|
|
11
|
+
const ClientTokenSchema = Schema.Struct({
|
|
12
|
+
sourceId: Schema.NonEmptyString,
|
|
13
|
+
clientId: Schema.NonEmptyString,
|
|
14
|
+
resumeKey: Schema.NonEmptyString
|
|
15
|
+
});
|
|
16
|
+
const ClientSessionSchema = Schema.Struct({
|
|
17
|
+
client: ClientTokenSchema,
|
|
18
|
+
connectionId: Schema.NonEmptyString
|
|
19
|
+
});
|
|
20
|
+
const ClientRecordSchema = Schema.Struct({
|
|
21
|
+
...ClientSessionSchema.fields,
|
|
22
|
+
expiresAt: Schema.Finite,
|
|
23
|
+
acceptedThrough: Counter,
|
|
24
|
+
acknowledgedThrough: Counter,
|
|
25
|
+
deliveredThrough: Counter,
|
|
26
|
+
deliveredRevision: Counter,
|
|
27
|
+
appliedRevision: Counter,
|
|
28
|
+
viewGeneration: Schema.optional(Counter),
|
|
29
|
+
viewIdentity: Schema.optional(Schema.String),
|
|
30
|
+
deliveredViewGeneration: Schema.optional(Counter)
|
|
31
|
+
});
|
|
32
|
+
const ClientLeaseSchema = ClientRecordSchema;
|
|
33
|
+
const error = (operation, kind, message) => new BackendError({
|
|
34
|
+
operation: `replica.client.${operation}`,
|
|
35
|
+
kind,
|
|
36
|
+
message,
|
|
37
|
+
retryable: kind === "Unavailable" || kind === "RateLimited"
|
|
38
|
+
});
|
|
39
|
+
/** Transaction-local admission bookkeeping. The caller must use the SAME tx for
|
|
40
|
+
* admit's callback and source state. Neither reconnect nor expiration executes actions.
|
|
41
|
+
*/
|
|
42
|
+
const clientRegistry = (namespace, sourceId, policy = {}, hooks = {}) => {
|
|
43
|
+
const sessionIdleTimeoutMs = policy.sessionIdleTimeoutMs ?? 6048e5;
|
|
44
|
+
const maxSessions = policy.maxSessions ?? 256;
|
|
45
|
+
const maxUnacknowledgedActions = policy.maxUnacknowledgedActions ?? 1024;
|
|
46
|
+
const clients = Table.make({
|
|
47
|
+
name: JSON.stringify([
|
|
48
|
+
"replica",
|
|
49
|
+
namespace,
|
|
50
|
+
"clients"
|
|
51
|
+
]),
|
|
52
|
+
codec: {
|
|
53
|
+
encode: Schema.encodeSync(ClientRecordSchema),
|
|
54
|
+
decode: Schema.decodeUnknownSync(ClientRecordSchema)
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
const submissions = Table.make({ name: JSON.stringify([
|
|
58
|
+
"replica",
|
|
59
|
+
namespace,
|
|
60
|
+
"submissions"
|
|
61
|
+
]) });
|
|
62
|
+
const validatePolicy = Effect.gen(function* () {
|
|
63
|
+
if (policy.deliveryRetentionMs !== void 0 && (!Number.isFinite(policy.deliveryRetentionMs) || policy.deliveryRetentionMs < 1) || !Number.isFinite(sessionIdleTimeoutMs) || sessionIdleTimeoutMs < 1 || !Number.isSafeInteger(maxSessions) || maxSessions < 1 || !Number.isSafeInteger(maxUnacknowledgedActions) || maxUnacknowledgedActions < 1) return yield* error("policy", "InvalidConfiguration", "Client retention bounds must be positive.");
|
|
64
|
+
});
|
|
65
|
+
const initialize = (tx) => Effect.gen(function* () {
|
|
66
|
+
yield* validatePolicy;
|
|
67
|
+
yield* tx.ensureIndex(clients, {
|
|
68
|
+
name: "expiresAt",
|
|
69
|
+
field: field("expiresAt")
|
|
70
|
+
});
|
|
71
|
+
yield* tx.ensureIndex(submissions, {
|
|
72
|
+
name: "clientId",
|
|
73
|
+
field: field("clientId")
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
const validate = Effect.fnUntraced(function* (tx, session) {
|
|
77
|
+
const now = yield* Clock.currentTimeMillis;
|
|
78
|
+
const stored = yield* tx.get(clients, session.client.clientId);
|
|
79
|
+
if (session.client.sourceId !== sourceId || stored && stored.client.sourceId !== sourceId) return yield* error("source", "SourceMismatch", "Client session belongs to a different source.");
|
|
80
|
+
if (!stored || stored.expiresAt <= now) return yield* error("expired", "SessionExpired", "Client session expired. Open a new client while preserving unresolved work.");
|
|
81
|
+
if (stored.client.resumeKey !== session.client.resumeKey || stored.connectionId !== session.connectionId) return yield* error("replaced", "SessionReplaced", "A newer connection replaced this client session.");
|
|
82
|
+
return {
|
|
83
|
+
...stored,
|
|
84
|
+
expiresAt: now + sessionIdleTimeoutMs
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
const expire = Effect.fnUntraced(function* (tx, limit = 100) {
|
|
88
|
+
const now = yield* Clock.currentTimeMillis;
|
|
89
|
+
const expired = yield* tx.query(clients, {
|
|
90
|
+
where: lte(field("expiresAt"), now),
|
|
91
|
+
limit
|
|
92
|
+
});
|
|
93
|
+
for (const { key, value } of expired) {
|
|
94
|
+
if (hooks.onExpire) yield* hooks.onExpire(tx, value.client.clientId);
|
|
95
|
+
for (const row of yield* tx.query(submissions, { where: eq(field("clientId"), value.client.clientId) })) yield* tx.delete(submissions, row.key);
|
|
96
|
+
yield* tx.delete(clients, key);
|
|
97
|
+
}
|
|
98
|
+
return expired.map((row) => row.value.client.clientId);
|
|
99
|
+
});
|
|
100
|
+
const open = Effect.fn("Replica.openClient")(function* (tx, resume) {
|
|
101
|
+
const now = yield* Clock.currentTimeMillis;
|
|
102
|
+
let previous;
|
|
103
|
+
if (resume) {
|
|
104
|
+
previous = yield* tx.get(clients, resume.clientId);
|
|
105
|
+
if (!previous || resume.sourceId !== sourceId || previous.client.sourceId !== sourceId || previous.client.resumeKey !== resume.resumeKey || previous.expiresAt <= now) return yield* error("expired", "SessionExpired", "Client identity cannot be resumed. Preserve unresolved local work and open a new client explicitly.");
|
|
106
|
+
} else {
|
|
107
|
+
yield* expire(tx);
|
|
108
|
+
if ((yield* tx.query(clients, { limit: maxSessions })).length >= maxSessions) return yield* error("capacity", "RateLimited", "Active client limit reached.");
|
|
109
|
+
}
|
|
110
|
+
const record = {
|
|
111
|
+
client: previous?.client ?? {
|
|
112
|
+
sourceId,
|
|
113
|
+
clientId: crypto.randomUUID(),
|
|
114
|
+
resumeKey: crypto.randomUUID()
|
|
115
|
+
},
|
|
116
|
+
connectionId: crypto.randomUUID(),
|
|
117
|
+
expiresAt: now + sessionIdleTimeoutMs,
|
|
118
|
+
acceptedThrough: previous?.acceptedThrough ?? 0,
|
|
119
|
+
acknowledgedThrough: previous?.acknowledgedThrough ?? 0,
|
|
120
|
+
deliveredThrough: previous?.deliveredThrough ?? 0,
|
|
121
|
+
deliveredRevision: previous?.deliveredRevision ?? 0,
|
|
122
|
+
appliedRevision: previous?.appliedRevision ?? 0,
|
|
123
|
+
viewGeneration: previous?.viewGeneration ?? 0,
|
|
124
|
+
viewIdentity: previous?.viewIdentity,
|
|
125
|
+
deliveredViewGeneration: previous?.deliveredViewGeneration ?? 0
|
|
126
|
+
};
|
|
127
|
+
yield* tx.put(clients, record.client.clientId, record);
|
|
128
|
+
return record;
|
|
129
|
+
});
|
|
130
|
+
const admit = Effect.fn("Replica.admitClientAction")(function* (tx, session, sequence, fingerprint, accept) {
|
|
131
|
+
const record = yield* validate(tx, session);
|
|
132
|
+
if (!Number.isSafeInteger(sequence) || sequence < 1) return yield* error("sequence", "InvalidRequest", "Submission sequence must be a positive safe integer.");
|
|
133
|
+
if (sequence <= record.acceptedThrough) {
|
|
134
|
+
const prior = yield* tx.get(submissions, JSON.stringify([record.client.clientId, sequence]));
|
|
135
|
+
if (!prior) return yield* error("resultExpired", "ResultExpired", "Submission was already accepted and its acknowledgment record was compacted. It must not execute again.");
|
|
136
|
+
if (prior.fingerprint !== fingerprint) return yield* error("identity", "Conflict", "Submission sequence was reused for a different action.");
|
|
137
|
+
yield* tx.put(clients, record.client.clientId, record);
|
|
138
|
+
return { duplicate: true };
|
|
139
|
+
}
|
|
140
|
+
if (sequence !== record.acceptedThrough + 1) return yield* error("sequence", "Conflict", "Submission is out of order; a preceding action is missing.");
|
|
141
|
+
if (record.acceptedThrough - record.acknowledgedThrough >= maxUnacknowledgedActions) return yield* error("capacity", "RateLimited", "Unacknowledged submission limit reached; apply and acknowledge source state first.");
|
|
142
|
+
const value = yield* accept();
|
|
143
|
+
yield* tx.put(submissions, JSON.stringify([record.client.clientId, sequence]), {
|
|
144
|
+
clientId: record.client.clientId,
|
|
145
|
+
sequence,
|
|
146
|
+
fingerprint
|
|
147
|
+
});
|
|
148
|
+
yield* tx.put(clients, record.client.clientId, {
|
|
149
|
+
...record,
|
|
150
|
+
acceptedThrough: sequence
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
duplicate: false,
|
|
154
|
+
value
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
return {
|
|
158
|
+
initialize,
|
|
159
|
+
open,
|
|
160
|
+
validate,
|
|
161
|
+
select: Effect.fnUntraced(function* (tx, session, view) {
|
|
162
|
+
const record = yield* validate(tx, session);
|
|
163
|
+
const generation = view?.generation ?? 0;
|
|
164
|
+
if (!Number.isSafeInteger(generation) || generation < (view ? 1 : 0)) return yield* error("view", "InvalidRequest", "A selected view requires a positive safe generation.");
|
|
165
|
+
const identity = view ? Table.make({ name: "view-identity" }).encode(view.selection) : "all";
|
|
166
|
+
if (generation < (record.viewGeneration ?? 0)) return yield* error("view", "ViewReplaced", "A newer selected view replaced this request.");
|
|
167
|
+
if (generation === (record.viewGeneration ?? 0) && record.viewIdentity !== void 0 && identity !== record.viewIdentity) return yield* error("view", "Conflict", "A view generation was reused for different membership.");
|
|
168
|
+
const updated = {
|
|
169
|
+
...record,
|
|
170
|
+
viewGeneration: generation,
|
|
171
|
+
viewIdentity: identity
|
|
172
|
+
};
|
|
173
|
+
yield* tx.put(clients, record.client.clientId, updated);
|
|
174
|
+
return updated;
|
|
175
|
+
}),
|
|
176
|
+
admit,
|
|
177
|
+
deliver: Effect.fnUntraced(function* (tx, session, revision) {
|
|
178
|
+
const record = yield* validate(tx, session);
|
|
179
|
+
if (!Number.isSafeInteger(revision) || revision < record.deliveredRevision) return yield* error("delivery", "Conflict", "Source delivery revision moved backwards.");
|
|
180
|
+
const updated = {
|
|
181
|
+
...record,
|
|
182
|
+
deliveredRevision: revision,
|
|
183
|
+
deliveredThrough: record.acceptedThrough,
|
|
184
|
+
deliveredViewGeneration: record.viewGeneration ?? 0
|
|
185
|
+
};
|
|
186
|
+
yield* tx.put(clients, record.client.clientId, updated);
|
|
187
|
+
return updated;
|
|
188
|
+
}),
|
|
189
|
+
acknowledge: Effect.fn("Replica.acknowledgeClient")(function* (tx, session, revision, through, viewGeneration = 0) {
|
|
190
|
+
const record = yield* validate(tx, session);
|
|
191
|
+
if (viewGeneration !== (record.viewGeneration ?? 0) || viewGeneration !== (record.deliveredViewGeneration ?? 0)) return yield* error("view", "ViewReplaced", "This acknowledgment belongs to a replaced or undelivered view.");
|
|
192
|
+
if (!Number.isSafeInteger(revision) || revision < 0 || revision > record.deliveredRevision || !Number.isSafeInteger(through) || through < 0 || through > record.deliveredThrough) return yield* error("acknowledgment", "InvalidRequest", "Cannot acknowledge state or submissions that were not delivered.");
|
|
193
|
+
if (revision < record.deliveredRevision || through < record.acknowledgedThrough) return record;
|
|
194
|
+
for (const row of yield* tx.query(submissions, { where: and(eq(field("clientId"), record.client.clientId), lte(field("sequence"), through)) })) yield* tx.delete(submissions, row.key);
|
|
195
|
+
const updated = {
|
|
196
|
+
...record,
|
|
197
|
+
appliedRevision: revision,
|
|
198
|
+
acknowledgedThrough: through
|
|
199
|
+
};
|
|
200
|
+
yield* tx.put(clients, record.client.clientId, updated);
|
|
201
|
+
return updated;
|
|
202
|
+
}),
|
|
203
|
+
expire,
|
|
204
|
+
clients,
|
|
205
|
+
submissions
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
//#endregion
|
|
209
|
+
export { ClientLeaseSchema, ClientSessionSchema, ClientTokenSchema, clientRegistry };
|
|
210
|
+
|
|
211
|
+
//# sourceMappingURL=clients.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clients.js","names":[],"sources":["../src/clients.ts"],"sourcesContent":["import { and, eq, field, lte } from \"@party-stack/ontology-storage\";\nimport { Table } from \"@party-stack/ontology-storage/tables\";\nimport { Clock, Effect, Schema } from \"effect\";\nimport { BackendError } from \"./index.js\";\nimport type { ClientView } from \"./protocol.js\";\nimport type { OntologyStorageError } from \"@party-stack/ontology-storage\";\nimport type { Transaction } from \"@party-stack/ontology-storage/tables\";\n\nconst Counter = Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER }));\nexport const ClientTokenSchema = Schema.Struct({\n sourceId: Schema.NonEmptyString,\n clientId: Schema.NonEmptyString,\n resumeKey: Schema.NonEmptyString,\n});\nexport type ClientToken = typeof ClientTokenSchema.Type;\nexport const ClientSessionSchema = Schema.Struct({\n client: ClientTokenSchema,\n connectionId: Schema.NonEmptyString,\n});\nexport type ClientSession = typeof ClientSessionSchema.Type;\nconst ClientRecordSchema = Schema.Struct({\n ...ClientSessionSchema.fields,\n expiresAt: Schema.Finite,\n acceptedThrough: Counter,\n acknowledgedThrough: Counter,\n deliveredThrough: Counter,\n deliveredRevision: Counter,\n appliedRevision: Counter,\n viewGeneration: Schema.optional(Counter),\n viewIdentity: Schema.optional(Schema.String),\n deliveredViewGeneration: Schema.optional(Counter),\n});\nexport const ClientLeaseSchema = ClientRecordSchema;\nexport type ClientLease = typeof ClientRecordSchema.Type;\nexport interface ClientPolicy {\n /** Retain disconnected selected delivery independently of action recovery. Default 60 seconds. */\n readonly deliveryRetentionMs?: number;\n /** Inactivity before a resumable client session expires. Validated requests renew it.\n * Defaults to seven days; expiry never discards accepted pending actions. */\n readonly sessionIdleTimeoutMs?: number;\n /** Retained unexpired sessions, including disconnected resumable clients. Defaults to 256. */\n readonly maxSessions?: number;\n /** Per-session accepted actions not yet acknowledged by the child. Defaults to 1,024.\n * This bounds submission bookkeeping, not the parent's entire pending outbox. */\n readonly maxUnacknowledgedActions?: number;\n}\nexport interface ClientRegistryHooks {\n /** Release only this client's retained delivery/results, never accepted work. */\n readonly onExpire?: (\n tx: Transaction,\n clientId: string\n ) => Effect.Effect<void, BackendError | OntologyStorageError>;\n}\nconst error = (operation: string, kind: BackendError[\"kind\"], message: string) =>\n new BackendError({\n operation: `replica.client.${operation}`,\n kind,\n message,\n retryable: kind === \"Unavailable\" || kind === \"RateLimited\",\n });\n\n/** Transaction-local admission bookkeeping. The caller must use the SAME tx for\n * admit's callback and source state. Neither reconnect nor expiration executes actions.\n */\nexport const clientRegistry = (\n namespace: string,\n sourceId: string,\n policy: ClientPolicy = {},\n hooks: ClientRegistryHooks = {}\n) => {\n const sessionIdleTimeoutMs = policy.sessionIdleTimeoutMs ?? 7 * 24 * 60 * 60 * 1000;\n const maxSessions = policy.maxSessions ?? 256;\n const maxUnacknowledgedActions = policy.maxUnacknowledgedActions ?? 1024;\n const clients = Table.make({\n name: JSON.stringify([\"replica\", namespace, \"clients\"]),\n codec: {\n encode: Schema.encodeSync(ClientRecordSchema),\n decode: Schema.decodeUnknownSync(ClientRecordSchema),\n },\n });\n const submissions = Table.make<{ clientId: string; sequence: number; fingerprint: string }>({\n name: JSON.stringify([\"replica\", namespace, \"submissions\"]),\n });\n const validatePolicy = Effect.gen(function* () {\n if (\n (policy.deliveryRetentionMs !== undefined &&\n (!Number.isFinite(policy.deliveryRetentionMs) || policy.deliveryRetentionMs < 1)) ||\n !Number.isFinite(sessionIdleTimeoutMs) ||\n sessionIdleTimeoutMs < 1 ||\n !Number.isSafeInteger(maxSessions) ||\n maxSessions < 1 ||\n !Number.isSafeInteger(maxUnacknowledgedActions) ||\n maxUnacknowledgedActions < 1\n )\n return yield* error(\n \"policy\",\n \"InvalidConfiguration\",\n \"Client retention bounds must be positive.\"\n );\n });\n const initialize = (tx: Transaction) =>\n Effect.gen(function* () {\n yield* validatePolicy;\n yield* tx.ensureIndex(clients, { name: \"expiresAt\", field: field(\"expiresAt\") });\n yield* tx.ensureIndex(submissions, { name: \"clientId\", field: field(\"clientId\") });\n });\n const validate = Effect.fnUntraced(function* (tx: Transaction, session: ClientSession) {\n const now = yield* Clock.currentTimeMillis;\n const stored = yield* tx.get(clients, session.client.clientId);\n if (session.client.sourceId !== sourceId || (stored && stored.client.sourceId !== sourceId))\n return yield* error(\"source\", \"SourceMismatch\", \"Client session belongs to a different source.\");\n if (!stored || stored.expiresAt <= now)\n return yield* error(\n \"expired\",\n \"SessionExpired\",\n \"Client session expired. Open a new client while preserving unresolved work.\"\n );\n if (\n stored.client.resumeKey !== session.client.resumeKey ||\n stored.connectionId !== session.connectionId\n )\n return yield* error(\n \"replaced\",\n \"SessionReplaced\",\n \"A newer connection replaced this client session.\"\n );\n return { ...stored, expiresAt: now + sessionIdleTimeoutMs };\n });\n const expire = Effect.fnUntraced(function* (tx: Transaction, limit = 100) {\n const now = yield* Clock.currentTimeMillis;\n const expired = yield* tx.query(clients, { where: lte(field(\"expiresAt\"), now), limit });\n for (const { key, value } of expired) {\n if (hooks.onExpire) yield* hooks.onExpire(tx, value.client.clientId);\n for (const row of yield* tx.query(submissions, {\n where: eq(field(\"clientId\"), value.client.clientId),\n }))\n yield* tx.delete(submissions, row.key);\n yield* tx.delete(clients, key);\n }\n return expired.map((row) => row.value.client.clientId);\n });\n const open = Effect.fn(\"Replica.openClient\")(function* (tx: Transaction, resume?: ClientToken) {\n const now = yield* Clock.currentTimeMillis;\n let previous: ClientLease | undefined;\n if (resume) {\n previous = yield* tx.get(clients, resume.clientId);\n if (\n !previous ||\n resume.sourceId !== sourceId ||\n previous.client.sourceId !== sourceId ||\n previous.client.resumeKey !== resume.resumeKey ||\n previous.expiresAt <= now\n )\n return yield* error(\n \"expired\",\n \"SessionExpired\",\n \"Client identity cannot be resumed. Preserve unresolved local work and open a new client explicitly.\"\n );\n } else {\n yield* expire(tx);\n if ((yield* tx.query(clients, { limit: maxSessions })).length >= maxSessions)\n return yield* error(\"capacity\", \"RateLimited\", \"Active client limit reached.\");\n }\n const record: ClientLease = {\n client: previous?.client ?? {\n sourceId,\n clientId: crypto.randomUUID(),\n resumeKey: crypto.randomUUID(),\n },\n connectionId: crypto.randomUUID(),\n expiresAt: now + sessionIdleTimeoutMs,\n acceptedThrough: previous?.acceptedThrough ?? 0,\n acknowledgedThrough: previous?.acknowledgedThrough ?? 0,\n deliveredThrough: previous?.deliveredThrough ?? 0,\n deliveredRevision: previous?.deliveredRevision ?? 0,\n appliedRevision: previous?.appliedRevision ?? 0,\n viewGeneration: previous?.viewGeneration ?? 0,\n viewIdentity: previous?.viewIdentity,\n deliveredViewGeneration: previous?.deliveredViewGeneration ?? 0,\n };\n yield* tx.put(clients, record.client.clientId, record);\n return record;\n });\n const admit = Effect.fn(\"Replica.admitClientAction\")(function* <A, E>(\n tx: Transaction,\n session: ClientSession,\n sequence: number,\n fingerprint: string,\n accept: () => Effect.Effect<A, E>\n ) {\n const record = yield* validate(tx, session);\n if (!Number.isSafeInteger(sequence) || sequence < 1)\n return yield* error(\n \"sequence\",\n \"InvalidRequest\",\n \"Submission sequence must be a positive safe integer.\"\n );\n if (sequence <= record.acceptedThrough) {\n const prior = yield* tx.get(submissions, JSON.stringify([record.client.clientId, sequence]));\n if (!prior)\n return yield* error(\n \"resultExpired\",\n \"ResultExpired\",\n \"Submission was already accepted and its acknowledgment record was compacted. It must not execute again.\"\n );\n if (prior.fingerprint !== fingerprint)\n return yield* error(\n \"identity\",\n \"Conflict\",\n \"Submission sequence was reused for a different action.\"\n );\n yield* tx.put(clients, record.client.clientId, record);\n return { duplicate: true as const };\n }\n if (sequence !== record.acceptedThrough + 1)\n return yield* error(\n \"sequence\",\n \"Conflict\",\n \"Submission is out of order; a preceding action is missing.\"\n );\n if (record.acceptedThrough - record.acknowledgedThrough >= maxUnacknowledgedActions)\n return yield* error(\n \"capacity\",\n \"RateLimited\",\n \"Unacknowledged submission limit reached; apply and acknowledge source state first.\"\n );\n const value = yield* accept();\n yield* tx.put(submissions, JSON.stringify([record.client.clientId, sequence]), {\n clientId: record.client.clientId,\n sequence,\n fingerprint,\n });\n yield* tx.put(clients, record.client.clientId, { ...record, acceptedThrough: sequence });\n return { duplicate: false as const, value };\n });\n const select = Effect.fnUntraced(function* (tx: Transaction, session: ClientSession, view?: ClientView) {\n const record = yield* validate(tx, session);\n const generation = view?.generation ?? 0;\n if (!Number.isSafeInteger(generation) || generation < (view ? 1 : 0))\n return yield* error(\n \"view\",\n \"InvalidRequest\",\n \"A selected view requires a positive safe generation.\"\n );\n const identity = view ? Table.make({ name: \"view-identity\" }).encode(view.selection) : \"all\";\n if (generation < (record.viewGeneration ?? 0))\n return yield* error(\"view\", \"ViewReplaced\", \"A newer selected view replaced this request.\");\n if (\n generation === (record.viewGeneration ?? 0) &&\n record.viewIdentity !== undefined &&\n identity !== record.viewIdentity\n )\n return yield* error(\"view\", \"Conflict\", \"A view generation was reused for different membership.\");\n const updated = { ...record, viewGeneration: generation, viewIdentity: identity };\n yield* tx.put(clients, record.client.clientId, updated);\n return updated;\n });\n const deliver = Effect.fnUntraced(function* (tx: Transaction, session: ClientSession, revision: number) {\n const record = yield* validate(tx, session);\n if (!Number.isSafeInteger(revision) || revision < record.deliveredRevision)\n return yield* error(\"delivery\", \"Conflict\", \"Source delivery revision moved backwards.\");\n const updated = {\n ...record,\n deliveredRevision: revision,\n deliveredThrough: record.acceptedThrough,\n deliveredViewGeneration: record.viewGeneration ?? 0,\n };\n yield* tx.put(clients, record.client.clientId, updated);\n return updated;\n });\n const acknowledge = Effect.fn(\"Replica.acknowledgeClient\")(function* (\n tx: Transaction,\n session: ClientSession,\n revision: number,\n through: number,\n viewGeneration = 0\n ) {\n const record = yield* validate(tx, session);\n if (\n viewGeneration !== (record.viewGeneration ?? 0) ||\n viewGeneration !== (record.deliveredViewGeneration ?? 0)\n )\n return yield* error(\n \"view\",\n \"ViewReplaced\",\n \"This acknowledgment belongs to a replaced or undelivered view.\"\n );\n if (\n !Number.isSafeInteger(revision) ||\n revision < 0 ||\n revision > record.deliveredRevision ||\n !Number.isSafeInteger(through) ||\n through < 0 ||\n through > record.deliveredThrough\n )\n return yield* error(\n \"acknowledgment\",\n \"InvalidRequest\",\n \"Cannot acknowledge state or submissions that were not delivered.\"\n );\n // A delayed acknowledgment of an older snapshot cannot prove which newer\n // admissions that snapshot contained. Leave pruning to the current barrier.\n if (revision < record.deliveredRevision || through < record.acknowledgedThrough) return record;\n for (const row of yield* tx.query(submissions, {\n where: and(eq(field(\"clientId\"), record.client.clientId), lte(field(\"sequence\"), through)),\n }))\n yield* tx.delete(submissions, row.key);\n const updated = { ...record, appliedRevision: revision, acknowledgedThrough: through };\n yield* tx.put(clients, record.client.clientId, updated);\n return updated;\n });\n return { initialize, open, validate, select, admit, deliver, acknowledge, expire, clients, submissions };\n};\n"],"mappings":";;;;;;AAQA,MAAM,UAAU,OAAO,IAAI,MAAM,OAAO,UAAU;CAA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