@git-stunts/git-cas 7.0.0 → 7.1.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/ARCHITECTURE.md +15 -0
- package/CHANGELOG.md +47 -0
- package/README.md +11 -4
- package/build-info.json +2 -2
- package/docs/API.md +111 -0
- package/docs/releases/v7.1.0.md +27 -0
- package/index.d.ts +15 -0
- package/index.js +30 -0
- package/package.json +4 -3
- package/src/domain/errors/Codes.js +1 -0
- package/src/domain/errors/ReservationStorageError.js +9 -0
- package/src/domain/services/ReservationCustodyObjects.js +171 -0
- package/src/domain/services/ReservationCustodyStore.js +149 -0
- package/src/domain/services/ReservationIndex.js +169 -0
- package/src/domain/services/ReservationIndexNode.js +75 -0
- package/src/domain/services/ReservationLedger.js +326 -0
- package/src/domain/services/ReservationLedgerStore.js +141 -0
- package/src/domain/services/ReservationListing.js +40 -0
- package/src/domain/services/ReservationNamespace.d.ts +135 -0
- package/src/domain/services/ReservationNamespace.js +51 -0
- package/src/domain/services/ReservationNamespaceRegistry.js +89 -0
- package/src/domain/services/ReservationRecord.js +166 -0
- package/src/domain/services/ReservationScan.js +49 -0
- package/src/domain/value-objects/ReservationCursor.d.ts +11 -0
- package/src/domain/value-objects/ReservationCursor.js +31 -0
- package/src/domain/value-objects/ReservationCustodyDescriptor.js +47 -0
- package/src/domain/value-objects/ReservationCustodySnapshot.js +16 -0
- package/src/domain/value-objects/ReservationEvidence.js +26 -0
- package/src/domain/value-objects/ReservationEvidenceRead.js +22 -0
- package/src/domain/value-objects/ReservationEvidenceRoots.js +72 -0
- package/src/domain/value-objects/ReservationIndeterminate.js +17 -0
- package/src/domain/value-objects/ReservationInspection.js +21 -0
- package/src/domain/value-objects/ReservationKey.js +26 -0
- package/src/domain/value-objects/ReservationLedgerHeader.js +15 -0
- package/src/domain/value-objects/ReservationLedgerRefusal.js +21 -0
- package/src/domain/value-objects/ReservationLedgerSnapshot.js +27 -0
- package/src/domain/value-objects/ReservationListRequest.js +35 -0
- package/src/domain/value-objects/ReservationListUnavailable.js +17 -0
- package/src/domain/value-objects/ReservationOpenIndeterminate.js +15 -0
- package/src/domain/value-objects/ReservationOpenRefusal.js +14 -0
- package/src/domain/value-objects/ReservationPage.js +27 -0
- package/src/domain/value-objects/ReservationPolicy.js +24 -0
- package/src/domain/value-objects/ReservationRefusal.js +23 -0
- package/src/domain/value-objects/ReservationResult.js +27 -0
- package/src/domain/value-objects/ReservationSupport.js +22 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +7 -0
- package/src/infrastructure/codecs/ReservationCustodyCodec.js +54 -0
- package/src/infrastructure/codecs/ReservationLedgerCodec.js +48 -0
- package/src/infrastructure/codecs/ReservationRecordCodec.js +54 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitRefPort.js +11 -0
- package/src/ports/ReservationCustodyCodecPort.js +14 -0
- package/src/ports/ReservationLedgerCodecPort.js +14 -0
- package/src/ports/ReservationRecordCodecPort.js +14 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import ReservationRecord from './ReservationRecord.js';
|
|
2
|
+
import ReservationIndex from './ReservationIndex.js';
|
|
3
|
+
import ReservationLedgerStore from './ReservationLedgerStore.js';
|
|
4
|
+
import ReservationCustodyStore from './ReservationCustodyStore.js';
|
|
5
|
+
import ReservationRefusal from '../value-objects/ReservationRefusal.js';
|
|
6
|
+
import ReservationEvidence from '../value-objects/ReservationEvidence.js';
|
|
7
|
+
import ReservationPolicy from '../value-objects/ReservationPolicy.js';
|
|
8
|
+
import ReservationLedgerHeader from '../value-objects/ReservationLedgerHeader.js';
|
|
9
|
+
import ReservationResult from '../value-objects/ReservationResult.js';
|
|
10
|
+
import ReservationLedgerRefusal from '../value-objects/ReservationLedgerRefusal.js';
|
|
11
|
+
import ReservationIndeterminate from '../value-objects/ReservationIndeterminate.js';
|
|
12
|
+
import ReservationInspection from '../value-objects/ReservationInspection.js';
|
|
13
|
+
import ReservationEvidenceRead from '../value-objects/ReservationEvidenceRead.js';
|
|
14
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
15
|
+
import ReservationStorageError from '../errors/ReservationStorageError.js';
|
|
16
|
+
import { CasError, ErrorCodes } from '../errors/index.js';
|
|
17
|
+
|
|
18
|
+
const MAX_CHECKED_ATTEMPTS = 4;
|
|
19
|
+
|
|
20
|
+
/** Private reservation protocol; application meaning and execution stay outside. */
|
|
21
|
+
export default class ReservationLedger {
|
|
22
|
+
#policy;
|
|
23
|
+
#index;
|
|
24
|
+
#store;
|
|
25
|
+
#custody;
|
|
26
|
+
|
|
27
|
+
constructor({ policy, index, store, custody }) {
|
|
28
|
+
if (!(policy instanceof ReservationPolicy) || !(index instanceof ReservationIndex) ||
|
|
29
|
+
!(store instanceof ReservationLedgerStore) || !(custody instanceof ReservationCustodyStore) ||
|
|
30
|
+
custody.namespace !== policy.namespace) {
|
|
31
|
+
throw new InvalidOptionsError('Reservation ledger requires policy, index, ledger and matching custody stores');
|
|
32
|
+
}
|
|
33
|
+
this.#policy = policy;
|
|
34
|
+
this.#index = index;
|
|
35
|
+
this.#store = store;
|
|
36
|
+
this.#custody = custody;
|
|
37
|
+
Object.freeze(this);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async reserve({ key, binding }) {
|
|
41
|
+
const record = new ReservationRecord({ key, binding, phase: 'reserved' });
|
|
42
|
+
for (let attempt = 0; attempt < MAX_CHECKED_ATTEMPTS; attempt++) {
|
|
43
|
+
let observation;
|
|
44
|
+
try {
|
|
45
|
+
observation = await this.#observe(record);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return new ReservationIndeterminate(record, error);
|
|
48
|
+
}
|
|
49
|
+
if (observation.result !== null) {
|
|
50
|
+
return observation.result;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
return await this.#publish(record, observation.snapshot);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (error instanceof CasError && error.code === ErrorCodes.GIT_REF_CONFLICT) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
return await this.#recover(record, error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return new ReservationLedgerRefusal(record, 'contention');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async #observe(record) {
|
|
65
|
+
const { snapshot, existing, result } = await this.#readAttempt(record);
|
|
66
|
+
if (result !== null || snapshot === null) {
|
|
67
|
+
return { snapshot, result };
|
|
68
|
+
}
|
|
69
|
+
await this.#verifyCustody(existing);
|
|
70
|
+
return { snapshot, result: existing === null ? capacityDecision(record, snapshot) : recoveryDecision(record, existing, snapshot) };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async #readAttempt(record) {
|
|
74
|
+
const snapshot = await this.#store.read(this.#policy.namespace);
|
|
75
|
+
if (snapshot === null) {
|
|
76
|
+
return { snapshot, existing: null, result: null };
|
|
77
|
+
}
|
|
78
|
+
if (!this.#policy.equals(snapshot.header.policy)) {
|
|
79
|
+
return { snapshot, existing: null, result: new ReservationLedgerRefusal(record, 'policy-mismatch') };
|
|
80
|
+
}
|
|
81
|
+
const existing = await this.#index.get(snapshot.indexRoot, record.key);
|
|
82
|
+
if (existing !== null && existing.charge > snapshot.header.charged) {
|
|
83
|
+
throw new ReservationStorageError('Reservation record charge exceeds its ledger aggregate');
|
|
84
|
+
}
|
|
85
|
+
const result = existing !== null && existing.binding !== record.binding ? new ReservationLedgerRefusal(record, 'binding-conflict') : null;
|
|
86
|
+
return { snapshot, existing, result };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async #publish(record, before) {
|
|
90
|
+
const indexRoot = await this.#index.put(before?.indexRoot ?? null, record);
|
|
91
|
+
const header = new ReservationLedgerHeader({ policy: this.#policy, charged: (before?.header.charged ?? 0) + 1 });
|
|
92
|
+
const snapshot = await this.#store.write({ before, header, indexRoot });
|
|
93
|
+
return new ReservationResult({ status: 'admitted', record, revision: snapshot.revision });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async #recover(record, writeError) {
|
|
97
|
+
try {
|
|
98
|
+
const { result } = await this.#observe(record);
|
|
99
|
+
// Only positive evidence of this same installed attempt resolves a lost
|
|
100
|
+
// acknowledgement. A later absent observation cannot cancel a pending I/O.
|
|
101
|
+
return result instanceof ReservationResult ? result : new ReservationIndeterminate(record, writeError);
|
|
102
|
+
} catch (inspectionError) {
|
|
103
|
+
return new ReservationIndeterminate(record, new CasError(
|
|
104
|
+
'Reservation publication and subsequent inspection failed', ErrorCodes.GIT_ERROR,
|
|
105
|
+
{ writeError, inspectionError },
|
|
106
|
+
));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async prepare({ key, binding, evidence }) {
|
|
111
|
+
assertEvidence(evidence);
|
|
112
|
+
return await this.#transition('prepare', new ReservationRecord({ key, binding, phase: 'reserved' }), evidence);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async arm({ key, binding }) {
|
|
116
|
+
return await this.#transition('arm', new ReservationRecord({ key, binding, phase: 'reserved' }));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async settle({ key, binding, evidence }) {
|
|
120
|
+
assertEvidence(evidence);
|
|
121
|
+
return await this.#transition('settle', new ReservationRecord({ key, binding, phase: 'reserved' }), evidence);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async cancel({ key, binding }) {
|
|
125
|
+
return await this.#transition('cancel', new ReservationRecord({ key, binding, phase: 'reserved' }));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async reconcile({ key, binding }) {
|
|
129
|
+
return await this.#transition('reconcile', new ReservationRecord({ key, binding, phase: 'reserved' }));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async inspectCustody({ key, binding }) {
|
|
133
|
+
const attempt = new ReservationRecord({ key, binding, phase: 'reserved' });
|
|
134
|
+
try {
|
|
135
|
+
const { snapshot, existing, result } = await this.#requiredAttempt(attempt);
|
|
136
|
+
if (result !== null) {
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
const custody = existing.preparation !== null && existing.phase !== 'cancelled' ?
|
|
140
|
+
await this.#custody.verify(existing) : await this.#custody.read(existing);
|
|
141
|
+
return new ReservationInspection({ snapshot, record: existing, custody });
|
|
142
|
+
} catch (cause) {
|
|
143
|
+
return new ReservationIndeterminate(attempt, cause);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async readEvidence({ key, binding, stage }) {
|
|
148
|
+
if (!['preparation', 'settlement'].includes(stage)) {
|
|
149
|
+
throw new InvalidOptionsError('Reservation evidence stage must be preparation or settlement');
|
|
150
|
+
}
|
|
151
|
+
const inspection = await this.inspectCustody({ key, binding });
|
|
152
|
+
if (!(inspection instanceof ReservationInspection)) {
|
|
153
|
+
return inspection;
|
|
154
|
+
}
|
|
155
|
+
const { record } = inspection;
|
|
156
|
+
if (record.phase === 'cancelled') {
|
|
157
|
+
return new ReservationLedgerRefusal(record, 'cancelled');
|
|
158
|
+
}
|
|
159
|
+
const custodyRevision = stage === 'preparation' ? record.preparation : record.settlement;
|
|
160
|
+
if (custodyRevision === null) {
|
|
161
|
+
return new ReservationLedgerRefusal(record, stage === 'preparation' ? 'not-prepared' : 'not-settled');
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const evidence = await this.#custody.readEvidence(record, stage);
|
|
165
|
+
return new ReservationEvidenceRead({ inspection, stage, custodyRevision, evidence });
|
|
166
|
+
} catch (cause) {
|
|
167
|
+
return new ReservationIndeterminate(record, cause);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async inspect({ key, binding }) {
|
|
172
|
+
const attempt = new ReservationRecord({ key, binding, phase: 'reserved' });
|
|
173
|
+
try {
|
|
174
|
+
const { snapshot, existing, result } = await this.#requiredAttempt(attempt);
|
|
175
|
+
if (result !== null) {
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
await this.#verifyCustody(existing);
|
|
179
|
+
return new ReservationResult({ status: 'recovered', record: existing, revision: snapshot.revision });
|
|
180
|
+
} catch (error) {
|
|
181
|
+
return new ReservationIndeterminate(attempt, error);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async #transition(operation, attempt, evidence) {
|
|
186
|
+
for (let retry = 0; retry < MAX_CHECKED_ATTEMPTS; retry++) {
|
|
187
|
+
let next = null;
|
|
188
|
+
try {
|
|
189
|
+
const { snapshot, existing, result } = await this.#requiredAttempt(attempt);
|
|
190
|
+
if (result !== null) {
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
if (existing.phase === 'cancelled') {
|
|
194
|
+
return await this.#cancelled(operation, existing, snapshot);
|
|
195
|
+
}
|
|
196
|
+
await this.#verifyCustody(existing);
|
|
197
|
+
next = await this.#next(operation, existing, evidence);
|
|
198
|
+
if (next instanceof ReservationRefusal) {
|
|
199
|
+
return new ReservationLedgerRefusal(attempt, next.reason);
|
|
200
|
+
}
|
|
201
|
+
await this.#verifyCustody(next);
|
|
202
|
+
if (next === existing) {
|
|
203
|
+
return new ReservationResult({ status: 'recovered', record: next, revision: snapshot.revision });
|
|
204
|
+
}
|
|
205
|
+
const installed = await this.#publishTransition({ before: snapshot, previous: existing, next });
|
|
206
|
+
if (operation === 'cancel') {
|
|
207
|
+
await this.#custody.discardCancelled(next);
|
|
208
|
+
}
|
|
209
|
+
return new ReservationResult({ status: next.phase, record: next, revision: installed.revision });
|
|
210
|
+
} catch (error) {
|
|
211
|
+
if (error instanceof CasError && error.code === ErrorCodes.GIT_REF_CONFLICT) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
return await this.#recoverTransition({ operation, attempt, next, error });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return new ReservationLedgerRefusal(attempt, 'contention');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async #requiredAttempt(attempt) {
|
|
221
|
+
const observed = await this.#readAttempt(attempt);
|
|
222
|
+
if (observed.result !== null || observed.existing !== null) {
|
|
223
|
+
return observed;
|
|
224
|
+
}
|
|
225
|
+
return { ...observed, result: new ReservationLedgerRefusal(attempt, 'not-reserved') };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async #next(operation, record, evidence) {
|
|
229
|
+
if (operation === 'cancel') {
|
|
230
|
+
return record.cancel();
|
|
231
|
+
}
|
|
232
|
+
if (operation === 'arm') {
|
|
233
|
+
return record.arm();
|
|
234
|
+
}
|
|
235
|
+
if (operation === 'reconcile') {
|
|
236
|
+
return await this.#reconciled(record);
|
|
237
|
+
}
|
|
238
|
+
const pin = operation === 'prepare' ? await this.#custody.prepare(record, evidence) : await this.#custody.settle(record, evidence);
|
|
239
|
+
if (pin instanceof ReservationRefusal) {
|
|
240
|
+
return pin;
|
|
241
|
+
}
|
|
242
|
+
return operation === 'prepare' ? record.prepare(pin.revision) : record.settle(pin.revision);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async #reconciled(record) {
|
|
246
|
+
const pin = await this.#custody.read(record);
|
|
247
|
+
if (pin === null) {
|
|
248
|
+
return record;
|
|
249
|
+
}
|
|
250
|
+
if (record.phase === 'reserved') {
|
|
251
|
+
if (pin.descriptor.stage !== 'preparation') {
|
|
252
|
+
throw new ReservationStorageError('An unprepared ledger cannot acknowledge a settlement pin');
|
|
253
|
+
}
|
|
254
|
+
return record.prepare(pin.revision);
|
|
255
|
+
}
|
|
256
|
+
return pin.descriptor.stage === 'settlement' ? record.settle(pin.revision) : record;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async #publishTransition({ before, previous, next }) {
|
|
260
|
+
const indexRoot = await this.#index.put(before.indexRoot, next);
|
|
261
|
+
const header = new ReservationLedgerHeader({
|
|
262
|
+
policy: this.#policy, charged: before.header.charged + next.charge - previous.charge,
|
|
263
|
+
});
|
|
264
|
+
return await this.#store.write({ before, header, indexRoot });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async #cancelled(operation, record, snapshot) {
|
|
268
|
+
await this.#custody.discardCancelled(record);
|
|
269
|
+
return operation === 'cancel' || operation === 'reconcile' ? new ReservationResult({ status: 'cancelled', record, revision: snapshot.revision }) :
|
|
270
|
+
new ReservationLedgerRefusal(record, 'cancelled');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async #verifyCustody(record) {
|
|
274
|
+
if (record !== null && record.preparation !== null && record.phase !== 'cancelled') {
|
|
275
|
+
await this.#custody.verify(record);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async #recoverTransition({ operation, attempt, next, error }) {
|
|
280
|
+
try {
|
|
281
|
+
const { snapshot, existing, result } = await this.#readAttempt(attempt);
|
|
282
|
+
if (result !== null || existing === null) {
|
|
283
|
+
return new ReservationIndeterminate(attempt, error);
|
|
284
|
+
}
|
|
285
|
+
if (existing.phase === 'cancelled') {
|
|
286
|
+
return await this.#cancelled(operation, existing, snapshot);
|
|
287
|
+
}
|
|
288
|
+
await this.#verifyCustody(existing);
|
|
289
|
+
if (operation === 'arm' && ['armed', 'settled'].includes(existing.phase)) {
|
|
290
|
+
return new ReservationLedgerRefusal(attempt, existing.phase === 'armed' ? 'already-armed' : 'already-settled');
|
|
291
|
+
}
|
|
292
|
+
if (next instanceof ReservationRecord && transitionObserved(operation, next, existing)) {
|
|
293
|
+
return new ReservationResult({ status: 'recovered', record: existing, revision: snapshot.revision });
|
|
294
|
+
}
|
|
295
|
+
return new ReservationIndeterminate(attempt, error);
|
|
296
|
+
} catch (inspectionError) {
|
|
297
|
+
return new ReservationIndeterminate(attempt, new CasError(
|
|
298
|
+
'Reservation operation and inspection failed', ErrorCodes.GIT_ERROR, { operationError: error, inspectionError },
|
|
299
|
+
));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function capacityDecision(record, snapshot) {
|
|
305
|
+
return snapshot.header.charged === snapshot.header.policy.limit ? new ReservationLedgerRefusal(record, 'capacity-full') : null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function recoveryDecision(attempt, existing, snapshot) {
|
|
309
|
+
if (existing.phase === 'cancelled') {
|
|
310
|
+
return new ReservationLedgerRefusal(attempt, 'cancelled');
|
|
311
|
+
}
|
|
312
|
+
return new ReservationResult({ status: 'recovered', record: existing, revision: snapshot.revision });
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function transitionObserved(operation, next, existing) {
|
|
316
|
+
if (operation === 'prepare' || (operation === 'reconcile' && next.phase === 'prepared')) {
|
|
317
|
+
return existing.preparation === next.preparation && existing.preparation !== null;
|
|
318
|
+
}
|
|
319
|
+
return (operation === 'settle' || operation === 'reconcile') && existing.settlement === next.settlement && existing.settlement !== null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function assertEvidence(evidence) {
|
|
323
|
+
if (!(evidence instanceof ReservationEvidence)) {
|
|
324
|
+
throw new InvalidOptionsError('Reservation preparation and settlement require validated evidence');
|
|
325
|
+
}
|
|
326
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import GitPersistencePort from '../../ports/GitPersistencePort.js';
|
|
2
|
+
import GitRefPort from '../../ports/GitRefPort.js';
|
|
3
|
+
import ReservationLedgerCodecPort, { RESERVATION_HEADER_MAX_BYTES } from '../../ports/ReservationLedgerCodecPort.js';
|
|
4
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
5
|
+
import ReservationStorageError from '../errors/ReservationStorageError.js';
|
|
6
|
+
import CollectionNamespace from '../value-objects/CollectionNamespace.js';
|
|
7
|
+
import ReservationLedgerHeader from '../value-objects/ReservationLedgerHeader.js';
|
|
8
|
+
import ReservationLedgerSnapshot from '../value-objects/ReservationLedgerSnapshot.js';
|
|
9
|
+
|
|
10
|
+
const ROOT_MAX_BYTES = 2 * 1024;
|
|
11
|
+
const COMMIT_MAX_BYTES = 16 * 1024;
|
|
12
|
+
const LEDGER_MESSAGE = 'git-cas reservation ledger v1';
|
|
13
|
+
|
|
14
|
+
/** Parentless generations publish the header and point index through one CAS. */
|
|
15
|
+
export default class ReservationLedgerStore {
|
|
16
|
+
#persistence;
|
|
17
|
+
#refs;
|
|
18
|
+
#codec;
|
|
19
|
+
|
|
20
|
+
constructor({ persistence, refs, codec }) {
|
|
21
|
+
if (!(persistence instanceof GitPersistencePort) || !(refs instanceof GitRefPort) ||
|
|
22
|
+
!(codec instanceof ReservationLedgerCodecPort)) {
|
|
23
|
+
throw new InvalidOptionsError('Reservation ledger requires persistence, ref and header codec ports');
|
|
24
|
+
}
|
|
25
|
+
this.#persistence = persistence;
|
|
26
|
+
this.#refs = refs;
|
|
27
|
+
this.#codec = codec;
|
|
28
|
+
Object.freeze(this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async read(namespace) {
|
|
32
|
+
const name = CollectionNamespace.from(namespace).toString();
|
|
33
|
+
const posture = await this.#refs.inspectRef(ledgerRef(name));
|
|
34
|
+
if (posture?.symref !== null) {
|
|
35
|
+
throw new ReservationStorageError('Reservation ledger authority must be a direct ref');
|
|
36
|
+
}
|
|
37
|
+
if (posture.oid === null) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
return await this.readRevision(name, posture.oid);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Read exactly this generation; never silently replace a collected cursor. */
|
|
44
|
+
async readRevision(namespace, revision) {
|
|
45
|
+
const name = CollectionNamespace.from(namespace).toString();
|
|
46
|
+
await this.#assertObject(revision, { type: 'commit', maxBytes: COMMIT_MAX_BYTES });
|
|
47
|
+
const parents = await this.#refs.resolveParents(revision);
|
|
48
|
+
if (!Array.isArray(parents) || parents.length !== 0) {
|
|
49
|
+
throw new ReservationStorageError('Reservation ledger generation must be parentless');
|
|
50
|
+
}
|
|
51
|
+
const root = await this.#refs.resolveTree(revision);
|
|
52
|
+
assertSameFormat(revision, root);
|
|
53
|
+
const { headerOid, indexRoot } = await this.#root(root);
|
|
54
|
+
const header = await this.#header(headerOid);
|
|
55
|
+
if (header.policy.namespace !== name) {
|
|
56
|
+
throw new ReservationStorageError('Reservation header names another namespace');
|
|
57
|
+
}
|
|
58
|
+
return new ReservationLedgerSnapshot({ header, indexRoot, revision });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async write({ before, header, indexRoot }) {
|
|
62
|
+
assertWrite({ before, header, indexRoot });
|
|
63
|
+
const bytes = this.#codec.encode(header);
|
|
64
|
+
if (!(bytes instanceof Uint8Array) || bytes.length > RESERVATION_HEADER_MAX_BYTES) {
|
|
65
|
+
throw new ReservationStorageError('Encoded reservation header exceeds its byte limit');
|
|
66
|
+
}
|
|
67
|
+
const headerOid = await this.#persistence.writeBlob(bytes);
|
|
68
|
+
assertSameFormat(indexRoot, headerOid);
|
|
69
|
+
const treeOid = await this.#persistence.writeTree([
|
|
70
|
+
`100644 blob ${headerOid}\theader`, `040000 tree ${indexRoot}\trecords`,
|
|
71
|
+
]);
|
|
72
|
+
assertSameFormat(indexRoot, treeOid);
|
|
73
|
+
const revision = await this.#refs.createCommit({ treeOid, parentOids: [], message: LEDGER_MESSAGE });
|
|
74
|
+
const snapshot = new ReservationLedgerSnapshot({ header, indexRoot, revision });
|
|
75
|
+
await this.#refs.updateRef({
|
|
76
|
+
ref: ledgerRef(header.policy.namespace), newOid: revision, expectedOldOid: before?.revision ?? null,
|
|
77
|
+
});
|
|
78
|
+
return snapshot;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async #root(root) {
|
|
82
|
+
await this.#assertObject(root, { type: 'tree', maxBytes: ROOT_MAX_BYTES });
|
|
83
|
+
const entries = await this.#persistence.readTree(root);
|
|
84
|
+
if (!Array.isArray(entries) || entries.length !== 2) {
|
|
85
|
+
throw new ReservationStorageError('Reservation ledger root must have exactly two entries');
|
|
86
|
+
}
|
|
87
|
+
const header = entries.find((entry) => entry.name === 'header');
|
|
88
|
+
const records = entries.find((entry) => entry.name === 'records');
|
|
89
|
+
if (header?.type !== 'blob' || header.mode !== '100644' || records?.type !== 'tree' || records.mode !== '040000') {
|
|
90
|
+
throw new ReservationStorageError('Reservation ledger root entries are invalid');
|
|
91
|
+
}
|
|
92
|
+
assertSameFormat(root, header.oid);
|
|
93
|
+
assertSameFormat(root, records.oid);
|
|
94
|
+
await this.#assertObject(records.oid, { type: 'tree', maxBytes: ROOT_MAX_BYTES });
|
|
95
|
+
return { headerOid: header.oid, indexRoot: records.oid };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async #header(oid) {
|
|
99
|
+
await this.#assertObject(oid, { type: 'blob', maxBytes: RESERVATION_HEADER_MAX_BYTES });
|
|
100
|
+
const header = this.#codec.decode(await this.#persistence.readBlob(oid, RESERVATION_HEADER_MAX_BYTES));
|
|
101
|
+
if (!(header instanceof ReservationLedgerHeader)) {
|
|
102
|
+
throw new ReservationStorageError('Reservation header decoder returned an invalid header');
|
|
103
|
+
}
|
|
104
|
+
return header;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async #assertObject(oid, { type, maxBytes }) {
|
|
108
|
+
ReservationLedgerSnapshot.assertOid(oid);
|
|
109
|
+
const [actualType, size] = await Promise.all([
|
|
110
|
+
this.#persistence.readObjectType(oid), this.#persistence.readObjectSize(oid),
|
|
111
|
+
]);
|
|
112
|
+
if (actualType !== type || !Number.isSafeInteger(size) || size < 0 || size > maxBytes) {
|
|
113
|
+
throw new ReservationStorageError('Reservation ledger object type or byte bound is invalid', { oid, type, size });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function ledgerRef(namespace) {
|
|
119
|
+
return `refs/cas/reservations/${namespace}`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function assertSameFormat(left, right) {
|
|
123
|
+
ReservationLedgerSnapshot.assertOid(left);
|
|
124
|
+
ReservationLedgerSnapshot.assertOid(right);
|
|
125
|
+
if (left.length !== right.length) {
|
|
126
|
+
throw new ReservationStorageError('Reservation ledger changed Git object format');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function assertWrite({ before, header, indexRoot }) {
|
|
131
|
+
if (!(header instanceof ReservationLedgerHeader) || (before !== null && !(before instanceof ReservationLedgerSnapshot))) {
|
|
132
|
+
throw new InvalidOptionsError('Reservation ledger write requires a validated header and prior snapshot or null');
|
|
133
|
+
}
|
|
134
|
+
ReservationLedgerSnapshot.assertOid(indexRoot);
|
|
135
|
+
if (before !== null) {
|
|
136
|
+
assertSameFormat(before.revision, indexRoot);
|
|
137
|
+
if (!before.header.policy.equals(header.policy)) {
|
|
138
|
+
throw new ReservationStorageError('Reservation ledger update cannot replace its namespace policy');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import ReservationPolicy from '../value-objects/ReservationPolicy.js';
|
|
2
|
+
import ReservationListRequest from '../value-objects/ReservationListRequest.js';
|
|
3
|
+
import ReservationPage from '../value-objects/ReservationPage.js';
|
|
4
|
+
import ReservationListUnavailable from '../value-objects/ReservationListUnavailable.js';
|
|
5
|
+
import ReservationIndex from './ReservationIndex.js';
|
|
6
|
+
import ReservationLedgerStore from './ReservationLedgerStore.js';
|
|
7
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
8
|
+
import ReservationStorageError from '../errors/ReservationStorageError.js';
|
|
9
|
+
|
|
10
|
+
/** Snapshot-bound diagnostic traversal; never used to make admission decisions. */
|
|
11
|
+
export default class ReservationListing {
|
|
12
|
+
#policy;
|
|
13
|
+
#index;
|
|
14
|
+
#store;
|
|
15
|
+
|
|
16
|
+
constructor({ policy, index, store }) {
|
|
17
|
+
if (!(policy instanceof ReservationPolicy) || !(index instanceof ReservationIndex) || !(store instanceof ReservationLedgerStore)) {
|
|
18
|
+
throw new InvalidOptionsError('Reservation listing requires a policy, index and ledger store');
|
|
19
|
+
}
|
|
20
|
+
this.#policy = policy;
|
|
21
|
+
this.#index = index;
|
|
22
|
+
this.#store = store;
|
|
23
|
+
Object.freeze(this);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async list(options) {
|
|
27
|
+
const request = new ReservationListRequest(this.#policy.namespace, options);
|
|
28
|
+
try {
|
|
29
|
+
const snapshot = request.cursor === null ? await this.#store.read(request.namespace) :
|
|
30
|
+
await this.#store.readRevision(request.namespace, request.cursor.revision);
|
|
31
|
+
if (snapshot === null || !snapshot.header.policy.equals(this.#policy)) {
|
|
32
|
+
throw new ReservationStorageError('Reservation namespace is missing or its policy changed');
|
|
33
|
+
}
|
|
34
|
+
const scan = await this.#index.scan(snapshot.indexRoot, request);
|
|
35
|
+
return new ReservationPage(snapshot, scan, request.phase);
|
|
36
|
+
} catch (cause) {
|
|
37
|
+
return new ReservationListUnavailable(request, cause);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import ReservationCursor from '../value-objects/ReservationCursor.js';
|
|
2
|
+
|
|
3
|
+
export type ReservationPhase = 'reserved' | 'prepared' | 'armed' | 'settled' | 'cancelled';
|
|
4
|
+
export type ReservationIdentity = { readonly key: string; readonly binding: string };
|
|
5
|
+
export type ReservationCapacity = { readonly unit: 'record-slot'; readonly limit: number };
|
|
6
|
+
export type ReservationPolicy = ReservationCapacity & { readonly namespace: string };
|
|
7
|
+
export type ReservationRecordView = ReservationIdentity & {
|
|
8
|
+
readonly phase: ReservationPhase;
|
|
9
|
+
readonly preparation: string | null;
|
|
10
|
+
readonly settlement: string | null;
|
|
11
|
+
readonly charge: 0 | 1;
|
|
12
|
+
};
|
|
13
|
+
export type ReservationEvidenceInput = {
|
|
14
|
+
readonly record: Uint8Array;
|
|
15
|
+
readonly supports?: readonly { readonly type: 'blob' | 'tree'; readonly oid: string }[];
|
|
16
|
+
readonly parents?: readonly string[];
|
|
17
|
+
};
|
|
18
|
+
export type ReservationRoots = {
|
|
19
|
+
readonly supports: readonly { readonly type: 'blob' | 'tree'; readonly oid: string }[];
|
|
20
|
+
readonly parents: readonly string[];
|
|
21
|
+
};
|
|
22
|
+
export type ReservationCustodyView = {
|
|
23
|
+
readonly revision: string;
|
|
24
|
+
readonly descriptor: ReservationIdentity & {
|
|
25
|
+
readonly namespace: string;
|
|
26
|
+
readonly stage: 'preparation' | 'settlement';
|
|
27
|
+
readonly recordOid: string;
|
|
28
|
+
readonly roots: ReservationRoots;
|
|
29
|
+
readonly preparation: string | null;
|
|
30
|
+
readonly parents: readonly string[];
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export type ReservationRefusalReason =
|
|
34
|
+
| 'capacity-full' | 'binding-conflict' | 'cancelled' | 'policy-mismatch' | 'contention'
|
|
35
|
+
| 'not-reserved' | 'not-prepared' | 'not-settled' | 'already-armed' | 'already-settled'
|
|
36
|
+
| 'irreversible' | 'preparation-mismatch' | 'settlement-mismatch';
|
|
37
|
+
export type ReservationRefused = ReservationIdentity & {
|
|
38
|
+
readonly status: 'refused';
|
|
39
|
+
readonly reason: ReservationRefusalReason;
|
|
40
|
+
};
|
|
41
|
+
export type ReservationIndeterminate = ReservationIdentity & {
|
|
42
|
+
readonly status: 'indeterminate';
|
|
43
|
+
readonly cause: Error;
|
|
44
|
+
};
|
|
45
|
+
export type ReservationAcknowledgedStatus = 'admitted' | 'recovered' | 'prepared' | 'armed' | 'settled' | 'cancelled';
|
|
46
|
+
export type ReservationAcknowledgement<S extends ReservationAcknowledgedStatus = ReservationAcknowledgedStatus> = {
|
|
47
|
+
readonly status: S;
|
|
48
|
+
readonly record: ReservationRecordView;
|
|
49
|
+
readonly revision: string;
|
|
50
|
+
};
|
|
51
|
+
export type ReservationOutcome<S extends ReservationAcknowledgedStatus = ReservationAcknowledgedStatus> = ReservationAcknowledgement<S> | ReservationRefused | ReservationIndeterminate;
|
|
52
|
+
export type ReservationInspection = {
|
|
53
|
+
readonly status: 'inspected';
|
|
54
|
+
readonly record: ReservationRecordView;
|
|
55
|
+
readonly revision: string;
|
|
56
|
+
readonly policy: ReservationPolicy;
|
|
57
|
+
readonly charged: number;
|
|
58
|
+
/** Observed after the ledger; the two refs are not an atomic snapshot. */
|
|
59
|
+
readonly custody: ReservationCustodyView | null;
|
|
60
|
+
};
|
|
61
|
+
export type ReservationEvidenceRead = {
|
|
62
|
+
readonly status: 'read';
|
|
63
|
+
readonly attempt: ReservationRecordView;
|
|
64
|
+
readonly ledgerRevision: string;
|
|
65
|
+
readonly stage: 'preparation' | 'settlement';
|
|
66
|
+
readonly custodyRevision: string;
|
|
67
|
+
readonly evidence: {
|
|
68
|
+
/** A fresh byte copy on every access. */
|
|
69
|
+
readonly record: Uint8Array;
|
|
70
|
+
readonly roots: ReservationRoots;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
export type ReservationCursorData = {
|
|
74
|
+
readonly namespace: string;
|
|
75
|
+
readonly revision: string;
|
|
76
|
+
readonly after: string;
|
|
77
|
+
readonly phase?: ReservationPhase | null;
|
|
78
|
+
};
|
|
79
|
+
export type ReservationListOptions = {
|
|
80
|
+
readonly cursor?: ReservationCursorData | null;
|
|
81
|
+
readonly phase?: ReservationPhase | null;
|
|
82
|
+
/** 1–100; default100. */
|
|
83
|
+
readonly limit?: number;
|
|
84
|
+
/** 129–8192 index objects, including seek; default1024. */
|
|
85
|
+
readonly nodeBudget?: number;
|
|
86
|
+
};
|
|
87
|
+
export type ReservationPage = {
|
|
88
|
+
readonly status: 'page';
|
|
89
|
+
readonly policy: ReservationPolicy;
|
|
90
|
+
readonly charged: number;
|
|
91
|
+
readonly revision: string;
|
|
92
|
+
/** Ledger claims at this snapshot, without a live per-record custody check. */
|
|
93
|
+
readonly records: readonly ReservationRecordView[];
|
|
94
|
+
readonly visitedNodes: number;
|
|
95
|
+
readonly next: ReservationCursor | null;
|
|
96
|
+
};
|
|
97
|
+
export type ReservationListUnavailable = {
|
|
98
|
+
readonly status: 'unavailable';
|
|
99
|
+
readonly namespace: string;
|
|
100
|
+
readonly cursor: ReservationCursor | null;
|
|
101
|
+
readonly cause: Error;
|
|
102
|
+
};
|
|
103
|
+
export type ReservationOpenRefusal = {
|
|
104
|
+
readonly status: 'refused';
|
|
105
|
+
readonly policy: ReservationPolicy;
|
|
106
|
+
readonly reason: 'policy-mismatch' | 'contention';
|
|
107
|
+
};
|
|
108
|
+
export type ReservationOpenIndeterminate = {
|
|
109
|
+
readonly status: 'indeterminate';
|
|
110
|
+
readonly policy: ReservationPolicy;
|
|
111
|
+
readonly cause: Error;
|
|
112
|
+
};
|
|
113
|
+
export type ReservationOpenOutcome = ReservationNamespace | ReservationOpenRefusal | ReservationOpenIndeterminate;
|
|
114
|
+
export type ReservationCapability = {
|
|
115
|
+
open(options: { readonly namespace: string; readonly capacity: ReservationCapacity }): Promise<ReservationOpenOutcome>;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/** Only a fresh `arm()` result with status `armed` permits an external effect. */
|
|
119
|
+
export default class ReservationNamespace {
|
|
120
|
+
private constructor();
|
|
121
|
+
readonly status: 'opened';
|
|
122
|
+
readonly namespace: string;
|
|
123
|
+
readonly policy: ReservationPolicy;
|
|
124
|
+
readonly openedRevision: string;
|
|
125
|
+
reserve(identity: ReservationIdentity): Promise<ReservationOutcome<'admitted' | 'recovered'>>;
|
|
126
|
+
prepare(options: ReservationIdentity & { readonly evidence: ReservationEvidenceInput }): Promise<ReservationOutcome<'prepared' | 'recovered'>>;
|
|
127
|
+
arm(identity: ReservationIdentity): Promise<ReservationOutcome<'armed'>>;
|
|
128
|
+
settle(options: ReservationIdentity & { readonly evidence: ReservationEvidenceInput }): Promise<ReservationOutcome<'settled' | 'recovered'>>;
|
|
129
|
+
cancel(identity: ReservationIdentity): Promise<ReservationOutcome<'cancelled'>>;
|
|
130
|
+
/** Completes storage phases or cancelled cleanup; never arms or executes effects. */
|
|
131
|
+
reconcile(identity: ReservationIdentity): Promise<ReservationOutcome<'prepared' | 'settled' | 'cancelled' | 'recovered'>>;
|
|
132
|
+
inspect(identity: ReservationIdentity): Promise<ReservationInspection | ReservationRefused | ReservationIndeterminate>;
|
|
133
|
+
readEvidence(options: ReservationIdentity & { readonly stage: 'preparation' | 'settlement' }): Promise<ReservationEvidenceRead | ReservationRefused | ReservationIndeterminate>;
|
|
134
|
+
list(options?: ReservationListOptions): Promise<ReservationPage | ReservationListUnavailable>;
|
|
135
|
+
}
|