@git-stunts/git-cas 6.5.7 → 6.5.9

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.
Files changed (37) hide show
  1. package/ARCHITECTURE.md +9 -0
  2. package/CHANGELOG.md +39 -0
  3. package/GUIDE.md +54 -0
  4. package/README.md +17 -9
  5. package/UPGRADING.md +68 -0
  6. package/build-info.json +2 -2
  7. package/docs/API.md +115 -12
  8. package/docs/releases/v6.5.8.md +89 -0
  9. package/docs/releases/v6.5.9.md +119 -0
  10. package/index.d.ts +75 -0
  11. package/index.js +19 -1
  12. package/package.json +4 -2
  13. package/src/domain/services/AssetService.js +201 -10
  14. package/src/domain/services/BoundedWriteWavePersistence.js +235 -0
  15. package/src/domain/services/BundleBatchPlanner.js +349 -0
  16. package/src/domain/services/BundleService.js +225 -2
  17. package/src/domain/services/CasService.d.ts +16 -0
  18. package/src/domain/services/CasService.js +9 -18
  19. package/src/domain/services/ManifestRepository.js +129 -50
  20. package/src/domain/services/ManifestTreeService.js +47 -0
  21. package/src/domain/services/PageService.js +36 -10
  22. package/src/domain/services/RootSet.js +32 -0
  23. package/src/domain/services/RootSetPersistence.js +58 -1
  24. package/src/domain/services/StagedTarget.js +19 -0
  25. package/src/domain/services/StagingWorkspace.js +95 -20
  26. package/src/domain/services/StagingWorkspaceRegistry.js +9 -4
  27. package/src/domain/services/WorkspaceCompoundAdmission.js +61 -0
  28. package/src/domain/services/WorkspaceCompoundScope.js +193 -0
  29. package/src/domain/services/forkCasPersistence.js +20 -0
  30. package/src/infrastructure/adapters/GitObjectSessionPool.js +47 -5
  31. package/src/infrastructure/adapters/GitPersistenceAdapter.js +169 -0
  32. package/src/infrastructure/adapters/GitPersistenceWriteScope.js +154 -0
  33. package/src/infrastructure/adapters/GitRefAdapter.js +38 -3
  34. package/src/infrastructure/adapters/GitUpdateRefSessionPool.js +115 -0
  35. package/src/package-version.js +1 -1
  36. package/src/ports/GitPersistencePort.js +43 -0
  37. package/src/ports/GitRefPort.js +8 -0
@@ -5,6 +5,7 @@ import { assertHandleObjectType, mapHandleTargetError } from '../helpers/handleT
5
5
  import PageHandle from '../value-objects/PageHandle.js';
6
6
  import StagedPage from '../value-objects/StagedPage.js';
7
7
  import BoundedPromiseCache from '../../helpers/boundedPromiseCache.js';
8
+ import { recordStagedTarget } from './StagedTarget.js';
8
9
 
9
10
  export const DEFAULT_MAX_PAGE_SIZE = 16 * 1024 * 1024;
10
11
  const DEFAULT_PAGE_CACHE_ENTRIES = 128;
@@ -55,15 +56,24 @@ export default class PageService {
55
56
  * @returns {Promise<StagedPage>}
56
57
  */
57
58
  async put({ source, maxBytes }) {
59
+ return await this.#put({ source, maxBytes }, this.#persistence);
60
+ }
61
+
62
+ /** @internal Stores one page through an operation-owned persistence view. */
63
+ async putWithPersistence({ source, maxBytes }, persistence) {
64
+ return await this.#put({ source, maxBytes }, persistence);
65
+ }
66
+
67
+ async #put({ source, maxBytes }, persistence) {
58
68
  const limit = this.#effectiveLimit(maxBytes);
59
69
  const observedAt = this.#observedAt();
60
70
  const bytes = await PageService.#collect(source, limit);
61
- const oid = await this.#persistence.writeBlob(bytes);
62
- return new StagedPage({
71
+ const oid = await persistence.writeBlob(bytes);
72
+ return recordStagedTarget(new StagedPage({
63
73
  handle: new PageHandle({ oid }),
64
74
  size: bytes.length,
65
75
  observedAt,
66
- });
76
+ }));
67
77
  }
68
78
 
69
79
  /**
@@ -80,6 +90,19 @@ export default class PageService {
80
90
  maxBatchBytes = DEFAULT_PAGE_WRITE_BATCH_BYTES,
81
91
  maxBatchPages = DEFAULT_PAGE_WRITE_BATCH_PAGES,
82
92
  }) {
93
+ return await this.#putBatch({ pages, maxBatchBytes, maxBatchPages }, this.#persistence);
94
+ }
95
+
96
+ /** @internal Stores a page batch through an operation-owned persistence view. */
97
+ async putBatchWithPersistence({
98
+ pages,
99
+ maxBatchBytes = DEFAULT_PAGE_WRITE_BATCH_BYTES,
100
+ maxBatchPages = DEFAULT_PAGE_WRITE_BATCH_PAGES,
101
+ }, persistence) {
102
+ return await this.#putBatch({ pages, maxBatchBytes, maxBatchPages }, persistence);
103
+ }
104
+
105
+ async #putBatch({ pages, maxBatchBytes, maxBatchPages }, persistence) {
83
106
  PageService.#assertBatch(pages, maxBatchBytes, maxBatchPages);
84
107
  const prepared = [];
85
108
  let totalBytes = 0;
@@ -107,7 +130,10 @@ export default class PageService {
107
130
  if (prepared.length === 0) {
108
131
  return Object.freeze([]);
109
132
  }
110
- const oids = await this.#writeBlobs(prepared.map((page) => page.bytes));
133
+ const oids = await this.#writeBlobs(
134
+ prepared.map((page) => page.bytes),
135
+ persistence,
136
+ );
111
137
  if (oids.length !== prepared.length) {
112
138
  throw createCasError(
113
139
  'Persistence returned the wrong number of page object identifiers',
@@ -115,11 +141,11 @@ export default class PageService {
115
141
  { expected: prepared.length, actual: oids.length },
116
142
  );
117
143
  }
118
- return Object.freeze(oids.map((oid, index) => new StagedPage({
144
+ return Object.freeze(oids.map((oid, index) => recordStagedTarget(new StagedPage({
119
145
  handle: new PageHandle({ oid }),
120
146
  size: prepared[index].bytes.length,
121
147
  observedAt: prepared[index].observedAt,
122
- })));
148
+ }))));
123
149
  }
124
150
 
125
151
  /**
@@ -218,13 +244,13 @@ export default class PageService {
218
244
  return now.toISOString();
219
245
  }
220
246
 
221
- async #writeBlobs(contents) {
222
- if (typeof this.#persistence.writeBlobs === 'function') {
223
- return await this.#persistence.writeBlobs(contents);
247
+ async #writeBlobs(contents, persistence = this.#persistence) {
248
+ if (typeof persistence.writeBlobs === 'function') {
249
+ return await persistence.writeBlobs(contents);
224
250
  }
225
251
  const oids = [];
226
252
  for (const content of contents) {
227
- oids.push(await this.#persistence.writeBlob(content));
253
+ oids.push(await persistence.writeBlob(content));
228
254
  }
229
255
  return oids;
230
256
  }
@@ -108,6 +108,31 @@ export default class RootSet {
108
108
  );
109
109
  }
110
110
 
111
+ /**
112
+ * Replaces the current generation under caller-supplied exact head
113
+ * authority. Unlike replace(), this path deliberately does not read or
114
+ * retry: the checked ref update is the authority test.
115
+ *
116
+ * @param {{ entries: Iterable<object>, expectedHeadOid: string|null }} options
117
+ * @returns {Promise<object>}
118
+ */
119
+ async replaceExact(options) {
120
+ if (!RootSet.#hasExpectedHead(options)) {
121
+ throw new CasError(
122
+ 'Exact root-set replacement requires expectedHeadOid authority',
123
+ ErrorCodes.INVALID_OPTIONS,
124
+ );
125
+ }
126
+ const entries = this.#metadataCodec.normalizeEntries(options.entries);
127
+ const expectedHeadOid = RootSet.#normalizeExpectedHead(options.expectedHeadOid);
128
+ const written = await this.persistence.write({
129
+ entries,
130
+ expectedHeadOid,
131
+ message: 'root-set: replace exact current roots',
132
+ });
133
+ return { changed: true, ...written };
134
+ }
135
+
111
136
  /**
112
137
  * @param {(entries: ReadonlyArray<object>, state: Readonly<object>) => Iterable<object>|Promise<Iterable<object>>} mutator
113
138
  * @param {{ expectedHeadOid?: string|null }} [options]
@@ -250,6 +275,13 @@ export default class RootSet {
250
275
  return Oid.from(expectedHeadOid).toString();
251
276
  }
252
277
 
278
+ static #hasExpectedHead(options) {
279
+ return typeof options === 'object' &&
280
+ options !== null &&
281
+ Object.hasOwn(options, 'expectedHeadOid') &&
282
+ options.expectedHeadOid !== undefined;
283
+ }
284
+
253
285
  static #entriesEqual(left, right) {
254
286
  return JSON.stringify(left) === JSON.stringify(right);
255
287
  }
@@ -396,7 +396,50 @@ export default class RootSetPersistence {
396
396
  }
397
397
 
398
398
  async #assertTargets(entries) {
399
- const report = await this.inspectTargets(entries);
399
+ if (typeof this.persistence.readObjectInfos === 'function') {
400
+ try {
401
+ const infos = await this.persistence.readObjectInfos(entries.map((entry) => entry.oid));
402
+ this.#assertBatchTargetInfos(entries, infos);
403
+ return;
404
+ } catch (err) {
405
+ if (err?.code === ErrorCodes.ROOT_SET_TARGET_TYPE_MISMATCH) {
406
+ throw err;
407
+ }
408
+ if (err?.code !== ErrorCodes.GIT_OBJECT_NOT_FOUND) {
409
+ throw this.#targetUnreadable(entries[0], err);
410
+ }
411
+ }
412
+ }
413
+ this.#assertTargetReport(await this.inspectTargets(entries));
414
+ }
415
+
416
+ #assertBatchTargetInfos(entries, infos) {
417
+ if (infos.length !== entries.length) {
418
+ throw new CasError(
419
+ 'Root-set target metadata count does not match the entry count',
420
+ ErrorCodes.GIT_ERROR,
421
+ { expectedCount: entries.length, actualCount: infos.length },
422
+ );
423
+ }
424
+ const mismatchIndex = infos.findIndex((info, index) => info.type !== entries[index].type);
425
+ if (mismatchIndex === -1) {
426
+ return;
427
+ }
428
+ const entry = entries[mismatchIndex];
429
+ throw new CasError(
430
+ `Root-set target type mismatch for ${entry.oid}`,
431
+ ErrorCodes.ROOT_SET_TARGET_TYPE_MISMATCH,
432
+ {
433
+ ref: this.rootSetRef,
434
+ name: entry.name,
435
+ oid: entry.oid,
436
+ expectedType: entry.type,
437
+ actualType: infos[mismatchIndex].type,
438
+ },
439
+ );
440
+ }
441
+
442
+ #assertTargetReport(report) {
400
443
  const issue = report.issues[0];
401
444
  if (!issue) {
402
445
  return;
@@ -412,4 +455,18 @@ export default class RootSetPersistence {
412
455
  ...issue,
413
456
  });
414
457
  }
458
+
459
+ #targetUnreadable(entry, originalError) {
460
+ return new CasError(
461
+ `Root-set target could not be inspected: ${entry?.oid ?? 'unknown'}`,
462
+ ErrorCodes.ROOT_SET_TARGET_UNREADABLE,
463
+ {
464
+ ref: this.rootSetRef,
465
+ name: entry?.name,
466
+ oid: entry?.oid,
467
+ expectedType: entry?.type,
468
+ originalError,
469
+ },
470
+ );
471
+ }
415
472
  }
@@ -0,0 +1,19 @@
1
+ import parseApplicationHandle from '../value-objects/ApplicationHandle.js';
2
+
3
+ const TARGETS = new WeakMap();
4
+
5
+ /** Records the direct Git target proven by an internal staged constructor. */
6
+ export function recordStagedTarget(staged) {
7
+ const handle = parseApplicationHandle(staged.handle);
8
+ TARGETS.set(staged, Object.freeze({
9
+ handle,
10
+ oid: handle.oid,
11
+ type: handle.kind === 'page' ? 'blob' : 'tree',
12
+ }));
13
+ return staged;
14
+ }
15
+
16
+ /** Returns private construction evidence, never serialized public metadata. */
17
+ export function stagedTargetOf(staged) {
18
+ return TARGETS.get(staged) ?? null;
19
+ }
@@ -4,6 +4,7 @@ import parseApplicationHandle from '../value-objects/ApplicationHandle.js';
4
4
  import RetentionWitness from '../value-objects/RetentionWitness.js';
5
5
  import WorkspaceRef from '../value-objects/WorkspaceRef.js';
6
6
  import RootSetMetadataCodec from './RootSetMetadataCodec.js';
7
+ import { stagedTargetOf } from './StagedTarget.js';
7
8
  import {
8
9
  MAX_WORKSPACE_TARGETS,
9
10
  WORKSPACE_DESCRIPTOR_ENTRY,
@@ -17,6 +18,7 @@ export default class StagingWorkspace {
17
18
  #assets;
18
19
  #bundles;
19
20
  #clock;
21
+ #compound;
20
22
  #descriptorCodec;
21
23
  #expiresAt = null;
22
24
  #generation = null;
@@ -26,26 +28,30 @@ export default class StagingWorkspace {
26
28
  #released = false;
27
29
  #resolveHandle;
28
30
  #rootSet;
31
+ #rootSetForPersistence;
29
32
  #tail = Promise.resolve();
30
33
  #targets = new Map();
31
34
  #ttlMs;
32
35
  #workspaceRef;
33
36
 
34
- constructor({ workspaceRef, ttlMs, rootSet, refs, assets, pages, bundles, publications,
35
- resolveHandle, descriptorCodec, clock = DEFAULT_CLOCK }) {
37
+ constructor({ workspaceRef, ttlMs, rootSet, rootSetForPersistence, refs, assets, pages,
38
+ bundles, publications, resolveHandle, descriptorCodec, compound, clock = DEFAULT_CLOCK }) {
36
39
  StagingWorkspace.#assertDependencies({
37
40
  rootSet,
41
+ rootSetForPersistence,
38
42
  refs,
39
43
  assets,
40
44
  pages,
41
45
  bundles,
42
46
  resolveHandle,
43
47
  descriptorCodec,
48
+ compound,
44
49
  clock,
45
50
  });
46
51
  this.#workspaceRef = WorkspaceRef.from(workspaceRef);
47
52
  this.#ttlMs = ttlMs;
48
53
  this.#rootSet = rootSet;
54
+ this.#rootSetForPersistence = rootSetForPersistence;
49
55
  this.#refs = refs;
50
56
  this.#assets = assets;
51
57
  this.#pages = pages;
@@ -53,10 +59,18 @@ export default class StagingWorkspace {
53
59
  this.#publications = publications;
54
60
  this.#resolveHandle = resolveHandle;
55
61
  this.#descriptorCodec = descriptorCodec;
62
+ this.#compound = compound;
56
63
  this.#clock = clock;
64
+ this.#initializeCapabilities();
65
+ Object.freeze(this);
66
+ }
57
67
 
68
+ #initializeCapabilities() {
58
69
  this.assets = Object.freeze({
59
70
  put: (options) => this.#enqueue(() => this.#stage(this.#assets, 'put', options)),
71
+ putBatch: (options) => (
72
+ this.#enqueue(() => this.#stageBatch(this.#assets, 'putBatch', options))
73
+ ),
60
74
  adopt: (options) => this.#enqueue(() => this.#stage(this.#assets, 'adopt', options)),
61
75
  });
62
76
  this.pages = Object.freeze({
@@ -68,8 +82,10 @@ export default class StagingWorkspace {
68
82
  putOrdered: (options) => (
69
83
  this.#enqueue(() => this.#stage(this.#bundles, 'putOrdered', options))
70
84
  ),
85
+ putOrderedBatch: (options) => (
86
+ this.#enqueue(() => this.#stageBatch(this.#bundles, 'putOrderedBatch', options))
87
+ ),
71
88
  });
72
- Object.freeze(this);
73
89
  }
74
90
 
75
91
  get id() {
@@ -88,6 +104,18 @@ export default class StagingWorkspace {
88
104
  return this.#expiresAt;
89
105
  }
90
106
 
107
+ batch(options = {}) {
108
+ return this.#enqueue(async () => {
109
+ this.#assertActive();
110
+ return await this.#compound.admit({
111
+ ...options,
112
+ install: async (staged, persistence) => (
113
+ await this.#retainCompound(staged, persistence)
114
+ ),
115
+ });
116
+ });
117
+ }
118
+
91
119
  checkpoint({ handles }) {
92
120
  return this.#enqueue(async () => {
93
121
  this.#assertActive();
@@ -172,7 +200,7 @@ export default class StagingWorkspace {
172
200
  );
173
201
  }
174
202
  try {
175
- const target = await this.#resolveTarget(staged.handle);
203
+ const target = stagedTargetOf(staged) ?? await this.#resolveTarget(staged.handle);
176
204
  const targets = new Map(this.#targets);
177
205
  targets.set(target.handle.toString(), target);
178
206
  const installation = await this.#install([...targets.values()]);
@@ -208,6 +236,13 @@ export default class StagingWorkspace {
208
236
 
209
237
  async #stageBatch(service, method, options) {
210
238
  this.#assertActive();
239
+ if (typeof service[method] !== 'function') {
240
+ throw createCasError(
241
+ `Workspace ${method}() is unavailable on the configured service`,
242
+ ErrorCodes.INVALID_OPTIONS,
243
+ { method },
244
+ );
245
+ }
211
246
  const staged = await service[method](options);
212
247
  if (!Array.isArray(staged) || staged.some((page) => !page?.handle)) {
213
248
  throw createCasError(
@@ -245,8 +280,8 @@ export default class StagingWorkspace {
245
280
  async #retainBatch(staged) {
246
281
  const resolved = [];
247
282
  const targets = new Map(this.#targets);
248
- for (const page of staged) {
249
- const target = await this.#resolveTarget(page.handle);
283
+ for (const artifact of staged) {
284
+ const target = stagedTargetOf(artifact) ?? await this.#resolveTarget(artifact.handle);
250
285
  resolved.push(target);
251
286
  targets.set(target.handle.toString(), target);
252
287
  }
@@ -254,7 +289,7 @@ export default class StagingWorkspace {
254
289
  const witnesses = new Map(
255
290
  installation.witnesses.map((witness) => [witness.handle.toString(), witness]),
256
291
  );
257
- return Object.freeze(staged.map((page, index) => {
292
+ return Object.freeze(staged.map((artifact, index) => {
258
293
  const handle = resolved[index].handle.toString();
259
294
  const witness = witnesses.get(handle);
260
295
  if (!witness) {
@@ -264,11 +299,35 @@ export default class StagingWorkspace {
264
299
  { handle, generation: installation.generation },
265
300
  );
266
301
  }
267
- return StagingWorkspace.#retainedStage(page, witness);
302
+ return StagingWorkspace.#retainedStage(artifact, witness);
268
303
  }));
269
304
  }
270
305
 
271
- async #install(targets) {
306
+ async #retainCompound(staged, persistence) {
307
+ const targets = new Map(this.#targets);
308
+ for (const artifact of staged) {
309
+ const target = stagedTargetOf(artifact) ?? await this.#resolveTarget(artifact.handle);
310
+ targets.set(target.handle.toString(), target);
311
+ }
312
+ try {
313
+ return await this.#install([...targets.values()], persistence);
314
+ } catch (error) {
315
+ if (error?.code === ErrorCodes.WORKSPACE_TTL_INVALID) {
316
+ throw error;
317
+ }
318
+ throw createCasError(
319
+ 'Workspace compound admission could not establish retention',
320
+ ErrorCodes.WORKSPACE_RETENTION_FAILED,
321
+ {
322
+ workspaceId: this.id,
323
+ stagedCount: staged.length,
324
+ originalError: error,
325
+ },
326
+ );
327
+ }
328
+ }
329
+
330
+ async #install(targets, persistence = null) {
272
331
  if (targets.length > MAX_WORKSPACE_TARGETS) {
273
332
  throw createCasError(
274
333
  'Workspace target count exceeds the supported maximum',
@@ -278,13 +337,10 @@ export default class StagingWorkspace {
278
337
  }
279
338
  const observedAt = this.#observedAt();
280
339
  const expiresAt = this.#expiryFrom(observedAt);
281
- const descriptor = await this.#pages.put({
282
- source: this.#descriptorCodec.encode({
283
- ref: this.#workspaceRef.toString(),
284
- createdAt: this.#workspaceRef.createdAt,
285
- expiresAt,
286
- targetCount: targets.length,
287
- }),
340
+ const descriptor = await this.#writeDescriptor({
341
+ expiresAt,
342
+ targetCount: targets.length,
343
+ persistence,
288
344
  });
289
345
  const entries = [
290
346
  {
@@ -295,7 +351,10 @@ export default class StagingWorkspace {
295
351
  },
296
352
  ...targets.map(StagingWorkspace.#targetEntry),
297
353
  ];
298
- const mutation = await this.#rootSet.replace({
354
+ const rootSet = persistence === null
355
+ ? this.#rootSet
356
+ : this.#rootSetForPersistence(persistence);
357
+ const mutation = await rootSet.replaceExact({
299
358
  entries,
300
359
  expectedHeadOid: this.#generation,
301
360
  });
@@ -317,6 +376,20 @@ export default class StagingWorkspace {
317
376
  });
318
377
  }
319
378
 
379
+ async #writeDescriptor({ expiresAt, targetCount, persistence }) {
380
+ const options = {
381
+ source: this.#descriptorCodec.encode({
382
+ ref: this.#workspaceRef.toString(),
383
+ createdAt: this.#workspaceRef.createdAt,
384
+ expiresAt,
385
+ targetCount,
386
+ }),
387
+ };
388
+ return persistence === null
389
+ ? await this.#pages.put(options)
390
+ : await this.#pages.putWithPersistence(options, persistence);
391
+ }
392
+
320
393
  #witness({ target, entries, observedAt }) {
321
394
  const name = StagingWorkspace.#targetName(target.handle);
322
395
  const index = entries.findIndex((entry) => entry.name === name);
@@ -566,16 +639,18 @@ export default class StagingWorkspace {
566
639
  });
567
640
  }
568
641
 
569
- static #assertDependencies({ rootSet, refs, assets, pages, bundles, resolveHandle,
570
- descriptorCodec, clock }) {
642
+ static #assertDependencies({ rootSet, rootSetForPersistence, refs, assets, pages, bundles,
643
+ resolveHandle, descriptorCodec, compound, clock }) {
571
644
  const missing = [
572
- ['rootSet', StagingWorkspace.#hasMethods(rootSet, ['replace'])],
645
+ ['rootSet', StagingWorkspace.#hasMethods(rootSet, ['replaceExact'])],
646
+ ['rootSetForPersistence', typeof rootSetForPersistence === 'function'],
573
647
  ['refs', StagingWorkspace.#hasMethods(refs, ['deleteRef'])],
574
648
  ['assets', StagingWorkspace.#hasMethods(assets, ['put', 'adopt'])],
575
649
  ['pages', StagingWorkspace.#hasMethods(pages, ['put', 'putBatch'])],
576
650
  ['bundles', StagingWorkspace.#hasMethods(bundles, ['put', 'putOrdered'])],
577
651
  ['resolveHandle', typeof resolveHandle === 'function'],
578
652
  ['descriptorCodec', StagingWorkspace.#hasMethods(descriptorCodec, ['encode'])],
653
+ ['compound', StagingWorkspace.#hasMethods(compound, ['admit'])],
579
654
  ['clock', StagingWorkspace.#hasMethods(clock, ['now'])],
580
655
  ].filter(([, valid]) => !valid).map(([name]) => name);
581
656
  if (missing.length > 0) {
@@ -8,6 +8,7 @@ import RootSet from './RootSet.js';
8
8
  import RootSetMetadataCodec from './RootSetMetadataCodec.js';
9
9
  import RootSetPersistence from './RootSetPersistence.js';
10
10
  import StagingWorkspace from './StagingWorkspace.js';
11
+ import WorkspaceCompoundAdmission from './WorkspaceCompoundAdmission.js';
11
12
  import WorkspaceDescriptorCodec, {
12
13
  WORKSPACE_DESCRIPTOR_ENTRY,
13
14
  } from './WorkspaceDescriptorCodec.js';
@@ -37,6 +38,7 @@ export default class StagingWorkspaceRegistry {
37
38
  #assets;
38
39
  #bundles;
39
40
  #clock;
41
+ #compound;
40
42
  #crypto;
41
43
  #descriptorCodec;
42
44
  #pages;
@@ -68,6 +70,7 @@ export default class StagingWorkspaceRegistry {
68
70
  this.#crypto = crypto;
69
71
  this.#clock = clock;
70
72
  this.#descriptorCodec = descriptorCodec;
73
+ this.#compound = new WorkspaceCompoundAdmission({ persistence, pages, bundles });
71
74
  Object.freeze(this);
72
75
  }
73
76
 
@@ -81,6 +84,7 @@ export default class StagingWorkspaceRegistry {
81
84
  workspaceRef,
82
85
  ttlMs,
83
86
  rootSet: this.#rootSet(workspaceRef),
87
+ rootSetForPersistence: (persistence) => this.#rootSet(workspaceRef, persistence),
84
88
  refs: this.#ref,
85
89
  assets: this.#assets,
86
90
  pages: this.#pages,
@@ -88,6 +92,7 @@ export default class StagingWorkspaceRegistry {
88
92
  publications: this.#publications,
89
93
  resolveHandle: this.#resolveHandle,
90
94
  descriptorCodec: this.#descriptorCodec,
95
+ compound: this.#compound,
91
96
  clock: this.#clock,
92
97
  });
93
98
  }
@@ -271,19 +276,19 @@ export default class StagingWorkspaceRegistry {
271
276
  return logicalBytes;
272
277
  }
273
278
 
274
- #rootSet(workspaceRef) {
279
+ #rootSet(workspaceRef, persistence = this.#persistence) {
275
280
  const ref = WorkspaceRef.from(workspaceRef).toString();
276
281
  const metadataCodec = new RootSetMetadataCodec({ refType: WorkspaceRef });
277
- const persistence = new RootSetPersistence({
282
+ const rootSetPersistence = new RootSetPersistence({
278
283
  rootSetRef: ref,
279
- persistence: this.#persistence,
284
+ persistence,
280
285
  ref: this.#ref,
281
286
  refType: WorkspaceRef,
282
287
  metadataCodec,
283
288
  });
284
289
  return new RootSet({
285
290
  ref,
286
- persistence,
291
+ persistence: rootSetPersistence,
287
292
  refType: WorkspaceRef,
288
293
  metadataCodec,
289
294
  });
@@ -0,0 +1,61 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import WorkspaceCompoundScope from './WorkspaceCompoundScope.js';
4
+
5
+ /** Executes one bounded provisional graph build before exact workspace retention. */
6
+ export default class WorkspaceCompoundAdmission {
7
+ #bundles;
8
+ #pages;
9
+ #persistence;
10
+
11
+ constructor({ persistence, pages, bundles }) {
12
+ WorkspaceCompoundAdmission.#assertDependencies({ persistence, pages, bundles });
13
+ this.#persistence = persistence;
14
+ this.#pages = pages;
15
+ this.#bundles = bundles;
16
+ Object.freeze(this);
17
+ }
18
+
19
+ async admit({ operation, maxOperations, install } = {}) {
20
+ if (typeof install !== 'function') {
21
+ throw createCasError(
22
+ 'Workspace compound admission requires an installation callback',
23
+ ErrorCodes.INVALID_OPTIONS
24
+ );
25
+ }
26
+ return await withWriteScope(this.#persistence, async (persistence) => {
27
+ const scope = new WorkspaceCompoundScope({
28
+ pages: this.#pages,
29
+ bundles: this.#bundles,
30
+ persistence,
31
+ maxOperations,
32
+ });
33
+ const prepared = await scope.execute(operation);
34
+ const retention = await install(prepared.staged, persistence);
35
+ return Object.freeze({ value: prepared.value, retention });
36
+ });
37
+ }
38
+
39
+ static #assertDependencies({ persistence, pages, bundles }) {
40
+ const missing = [
41
+ ['persistence', persistence === null || typeof persistence !== 'object'],
42
+ ['pages.putBatch', typeof pages?.putBatch !== 'function'],
43
+ ['bundles', bundles === null || typeof bundles !== 'object'],
44
+ ]
45
+ .filter(([, absent]) => absent)
46
+ .map(([name]) => name);
47
+ if (missing.length > 0) {
48
+ throw createCasError(
49
+ 'Workspace compound admission requires complete dependencies',
50
+ ErrorCodes.INVALID_OPTIONS,
51
+ { missing }
52
+ );
53
+ }
54
+ }
55
+ }
56
+
57
+ async function withWriteScope(persistence, operation) {
58
+ return typeof persistence.withWriteScope === 'function'
59
+ ? await persistence.withWriteScope(operation)
60
+ : await operation(persistence);
61
+ }