@git-stunts/git-cas 6.0.1 → 6.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 +12 -3
- package/CHANGELOG.md +20 -1
- package/README.md +10 -1
- package/UPGRADING.md +28 -1
- package/build-info.json +2 -2
- package/docs/API.md +199 -7
- package/docs/WALKTHROUGH.md +46 -0
- package/docs/releases/v6.1.0.md +75 -0
- package/index.d.ts +96 -0
- package/index.js +28 -0
- package/package.json +6 -5
- package/src/domain/errors/Codes.js +13 -0
- package/src/domain/services/CasService.d.ts +1 -0
- package/src/domain/services/RootSet.js +277 -0
- package/src/domain/services/RootSetMetadataCodec.js +253 -0
- package/src/domain/services/RootSetPersistence.js +334 -0
- package/src/domain/services/RootSetRegistry.js +44 -0
- package/src/domain/services/RootSetRetryPolicy.js +87 -0
- package/src/domain/services/RootSetTreeCodec.js +95 -0
- package/src/domain/value-objects/RootSetRef.js +88 -0
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +29 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +20 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +9 -0
- package/src/ports/GitRefPort.js +9 -0
package/index.d.ts
CHANGED
|
@@ -150,6 +150,7 @@ export declare class GitPersistencePortBase {
|
|
|
150
150
|
iterateTree(
|
|
151
151
|
treeOid: string
|
|
152
152
|
): AsyncIterable<{ mode: string; type: string; oid: string; name: string }>;
|
|
153
|
+
readObjectType(oid: string): Promise<string>;
|
|
153
154
|
setMaxBlobSize?(maxBlobSize: number): void;
|
|
154
155
|
}
|
|
155
156
|
|
|
@@ -157,6 +158,7 @@ export declare class GitPersistencePortBase {
|
|
|
157
158
|
export declare class GitRefPortBase {
|
|
158
159
|
resolveRef(ref: string): Promise<string>;
|
|
159
160
|
resolveTree(commitOid: string): Promise<string>;
|
|
161
|
+
resolveParents(commitOid: string): Promise<string[]>;
|
|
160
162
|
createCommit(options: {
|
|
161
163
|
treeOid: string;
|
|
162
164
|
parentOid?: string | null;
|
|
@@ -287,6 +289,92 @@ export interface VaultEntry {
|
|
|
287
289
|
treeOid: string;
|
|
288
290
|
}
|
|
289
291
|
|
|
292
|
+
export type RootSetEntryType = 'blob' | 'tree';
|
|
293
|
+
export type RootSetRetention = 'pinned' | 'evictable';
|
|
294
|
+
|
|
295
|
+
/** One Git object retained by a root set while the entry is present. */
|
|
296
|
+
export interface RootSetEntry {
|
|
297
|
+
name: string;
|
|
298
|
+
oid: string;
|
|
299
|
+
type: RootSetEntryType;
|
|
300
|
+
retention: RootSetRetention;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export interface RootSetState {
|
|
304
|
+
ref: string;
|
|
305
|
+
headOid: string | null;
|
|
306
|
+
treeOid: string | null;
|
|
307
|
+
entries: RootSetEntry[];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export interface RootSetMutationResult {
|
|
311
|
+
changed: boolean;
|
|
312
|
+
commitOid: string | null;
|
|
313
|
+
treeOid: string | null;
|
|
314
|
+
entries: RootSetEntry[];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export interface RootSetDoctorResult {
|
|
318
|
+
healthy: boolean;
|
|
319
|
+
ref: string;
|
|
320
|
+
headOid?: string | null;
|
|
321
|
+
treeOid?: string | null;
|
|
322
|
+
entryCount?: number;
|
|
323
|
+
entries?: RootSetEntry[];
|
|
324
|
+
policyCounts?: { pinned: number; evictable: number };
|
|
325
|
+
reachabilityCounts?: {
|
|
326
|
+
anchored: number;
|
|
327
|
+
missing: number;
|
|
328
|
+
unknown: number;
|
|
329
|
+
orphaned: number;
|
|
330
|
+
volatile: number;
|
|
331
|
+
};
|
|
332
|
+
targets?: Array<RootSetEntry & {
|
|
333
|
+
exists: boolean | null;
|
|
334
|
+
actualType: string | null;
|
|
335
|
+
reachability: 'anchored' | 'missing' | 'unknown';
|
|
336
|
+
}>;
|
|
337
|
+
issues?: Array<Record<string, unknown>>;
|
|
338
|
+
error?: { code: string; message: string };
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export declare class RootSet {
|
|
342
|
+
readonly ref: string;
|
|
343
|
+
read(): Promise<RootSetState>;
|
|
344
|
+
list(): Promise<RootSetEntry[]>;
|
|
345
|
+
contains(name: string): Promise<boolean>;
|
|
346
|
+
put(entry: Omit<RootSetEntry, 'retention'> & { retention?: RootSetRetention }): Promise<
|
|
347
|
+
RootSetMutationResult & { entry: RootSetEntry; previous: RootSetEntry | null }
|
|
348
|
+
>;
|
|
349
|
+
remove(options: { name: string }): Promise<
|
|
350
|
+
RootSetMutationResult & { removed: RootSetEntry | null }
|
|
351
|
+
>;
|
|
352
|
+
replace(options: {
|
|
353
|
+
entries: Iterable<RootSetEntry>;
|
|
354
|
+
expectedHeadOid?: string | null;
|
|
355
|
+
}): Promise<RootSetMutationResult>;
|
|
356
|
+
mutate(
|
|
357
|
+
mutator: (
|
|
358
|
+
entries: ReadonlyArray<Readonly<RootSetEntry>>,
|
|
359
|
+
) => Iterable<RootSetEntry> | Promise<Iterable<RootSetEntry>>,
|
|
360
|
+
options?: { expectedHeadOid?: string | null },
|
|
361
|
+
): Promise<RootSetMutationResult>;
|
|
362
|
+
doctor(): Promise<RootSetDoctorResult>;
|
|
363
|
+
repair(options: { entries: Iterable<RootSetEntry> }): Promise<{
|
|
364
|
+
repaired: true;
|
|
365
|
+
commitOid: string;
|
|
366
|
+
treeOid: string;
|
|
367
|
+
entries: RootSetEntry[];
|
|
368
|
+
}>;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export declare class RootSetRegistry {
|
|
372
|
+
open(options: {
|
|
373
|
+
ref: string;
|
|
374
|
+
retry?: { maxAttempts?: number; baseDelayMs?: number };
|
|
375
|
+
}): RootSet;
|
|
376
|
+
}
|
|
377
|
+
|
|
290
378
|
/** Encrypted vault key verifier stored in .vault.json. */
|
|
291
379
|
export interface VaultEncryptionVerifier {
|
|
292
380
|
version: 1;
|
|
@@ -448,8 +536,16 @@ export default class ContentAddressableStore {
|
|
|
448
536
|
|
|
449
537
|
get chunkSize(): number;
|
|
450
538
|
|
|
539
|
+
readonly rootSets: {
|
|
540
|
+
open(options: {
|
|
541
|
+
ref: string;
|
|
542
|
+
retry?: { maxAttempts?: number; baseDelayMs?: number };
|
|
543
|
+
}): Promise<RootSet>;
|
|
544
|
+
};
|
|
545
|
+
|
|
451
546
|
getService(): Promise<CasService>;
|
|
452
547
|
getVaultService(): Promise<VaultService>;
|
|
548
|
+
getRootSetRegistry(): Promise<RootSetRegistry>;
|
|
453
549
|
|
|
454
550
|
static open(options?: ContentAddressableStoreOpenOptions): Promise<ContentAddressableStore>;
|
|
455
551
|
|
package/index.js
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
// ---------------------------------------------------------------------------
|
|
9
9
|
import CasService from './src/domain/services/CasService.js';
|
|
10
10
|
import VaultService from './src/domain/services/VaultService.js';
|
|
11
|
+
import RootSet from './src/domain/services/RootSet.js';
|
|
12
|
+
import RootSetRegistry from './src/domain/services/RootSetRegistry.js';
|
|
11
13
|
import rotateVaultPassphrase from './src/domain/services/rotateVaultPassphrase.js';
|
|
12
14
|
import GitPersistenceAdapter from './src/infrastructure/adapters/GitPersistenceAdapter.js';
|
|
13
15
|
import GitRefAdapter from './src/infrastructure/adapters/GitRefAdapter.js';
|
|
@@ -50,6 +52,8 @@ function withOperationChunker(options) {
|
|
|
50
52
|
export {
|
|
51
53
|
CasService,
|
|
52
54
|
VaultService,
|
|
55
|
+
RootSet,
|
|
56
|
+
RootSetRegistry,
|
|
53
57
|
GitPersistenceAdapter,
|
|
54
58
|
GitRefAdapter,
|
|
55
59
|
JsonCodec,
|
|
@@ -131,12 +135,17 @@ export default class ContentAddressableStore {
|
|
|
131
135
|
};
|
|
132
136
|
this.service = null;
|
|
133
137
|
this.#servicePromise = null;
|
|
138
|
+
this.rootSets = Object.freeze({
|
|
139
|
+
open: async (options) => (await this.#getRootSetRegistry()).open(options),
|
|
140
|
+
});
|
|
134
141
|
}
|
|
135
142
|
|
|
136
143
|
/** @type {{ plumbing: *, chunkSize?: number, codec?: *, policy?: *, crypto?: *, observability?: *, merkleThreshold?: number, concurrency?: number, chunking?: *, chunker?: *, maxRestoreBufferSize?: number, maxBlobSize?: number, compressionAdapter?: * }} */
|
|
137
144
|
#config;
|
|
138
145
|
/** @type {VaultService|null} */
|
|
139
146
|
#vault = null;
|
|
147
|
+
/** @type {RootSetRegistry|null} */
|
|
148
|
+
#rootSetRegistry = null;
|
|
140
149
|
#servicePromise = null;
|
|
141
150
|
|
|
142
151
|
/**
|
|
@@ -192,6 +201,7 @@ export default class ContentAddressableStore {
|
|
|
192
201
|
crypto,
|
|
193
202
|
observability: this.service.observability,
|
|
194
203
|
});
|
|
204
|
+
this.#rootSetRegistry = new RootSetRegistry({ persistence, ref });
|
|
195
205
|
|
|
196
206
|
return this.service;
|
|
197
207
|
}
|
|
@@ -206,6 +216,16 @@ export default class ContentAddressableStore {
|
|
|
206
216
|
return this.#vault;
|
|
207
217
|
}
|
|
208
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Lazily initializes and returns the root-set registry.
|
|
221
|
+
* @private
|
|
222
|
+
* @returns {Promise<RootSetRegistry>}
|
|
223
|
+
*/
|
|
224
|
+
async #getRootSetRegistry() {
|
|
225
|
+
await this.#getService();
|
|
226
|
+
return this.#rootSetRegistry;
|
|
227
|
+
}
|
|
228
|
+
|
|
209
229
|
/**
|
|
210
230
|
* Lazily initializes and returns the underlying {@link CasService}.
|
|
211
231
|
* @returns {Promise<CasService>}
|
|
@@ -222,6 +242,14 @@ export default class ContentAddressableStore {
|
|
|
222
242
|
return await this.#getVault();
|
|
223
243
|
}
|
|
224
244
|
|
|
245
|
+
/**
|
|
246
|
+
* Lazily initializes and returns the root-set registry.
|
|
247
|
+
* @returns {Promise<RootSetRegistry>}
|
|
248
|
+
*/
|
|
249
|
+
async getRootSetRegistry() {
|
|
250
|
+
return await this.#getRootSetRegistry();
|
|
251
|
+
}
|
|
252
|
+
|
|
225
253
|
/**
|
|
226
254
|
* Factory to create a default JSON CAS from a Git working directory.
|
|
227
255
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@git-stunts/git-cas",
|
|
3
|
-
"version": "6.0
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"description": "Content-addressed storage backed by Git's object database, with optional encryption and pluggable codecs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"docs/EXTENDING.md",
|
|
33
33
|
"docs/STORE_RESTORE_PIPELINE.md",
|
|
34
34
|
"docs/releases/v6.0.0.md",
|
|
35
|
+
"docs/releases/v6.1.0.md",
|
|
35
36
|
"docs/THREAT_MODEL.md",
|
|
36
37
|
"docs/VAULT_INTERNALS.md",
|
|
37
38
|
"docs/WALKTHROUGH.md",
|
|
@@ -76,10 +77,10 @@
|
|
|
76
77
|
"test:node": "docker compose run --build --rm test-node",
|
|
77
78
|
"test:bun": "docker compose run --build --rm test-bun",
|
|
78
79
|
"test:deno": "docker compose run --build --rm test-deno",
|
|
79
|
-
"test:integration": "vitest run test/integration",
|
|
80
|
-
"test:integration:node": "docker compose run --build --rm test-node npx vitest run test/integration",
|
|
81
|
-
"test:integration:bun": "docker compose run --build --rm test-bun bunx vitest run test/integration",
|
|
82
|
-
"test:integration:deno": "docker compose run --build --rm test-deno deno run -A npm:vitest run test/integration",
|
|
80
|
+
"test:integration": "vitest run test/integration --no-file-parallelism",
|
|
81
|
+
"test:integration:node": "docker compose run --build --rm test-node npx vitest run test/integration --no-file-parallelism",
|
|
82
|
+
"test:integration:bun": "docker compose run --build --rm test-bun bunx vitest run test/integration --no-file-parallelism",
|
|
83
|
+
"test:integration:deno": "docker compose run --build --rm test-deno deno run -A npm:vitest run test/integration --no-file-parallelism",
|
|
83
84
|
"test:platforms": "bats --jobs 3 test/platform/runtimes.bats",
|
|
84
85
|
"benchmark": "vitest bench test/benchmark",
|
|
85
86
|
"benchmark:local": "vitest bench test/benchmark",
|
|
@@ -4,6 +4,7 @@ const ErrorCodes = Object.freeze({
|
|
|
4
4
|
DEK_UNWRAP_FAILED: 'DEK_UNWRAP_FAILED',
|
|
5
5
|
ENCRYPTION_BUFFER_EXCEEDED: 'ENCRYPTION_BUFFER_EXCEEDED',
|
|
6
6
|
GIT_ERROR: 'GIT_ERROR',
|
|
7
|
+
GIT_OBJECT_NOT_FOUND: 'GIT_OBJECT_NOT_FOUND',
|
|
7
8
|
GIT_PLUMBING_INITIALIZATION_FAILED: 'GIT_PLUMBING_INITIALIZATION_FAILED',
|
|
8
9
|
GIT_REF_NOT_FOUND: 'GIT_REF_NOT_FOUND',
|
|
9
10
|
INTEGRITY_ERROR: 'INTEGRITY_ERROR',
|
|
@@ -27,6 +28,18 @@ const ErrorCodes = Object.freeze({
|
|
|
27
28
|
RECIPIENT_ALREADY_EXISTS: 'RECIPIENT_ALREADY_EXISTS',
|
|
28
29
|
RECIPIENT_NOT_FOUND: 'RECIPIENT_NOT_FOUND',
|
|
29
30
|
RESTORE_TOO_LARGE: 'RESTORE_TOO_LARGE',
|
|
31
|
+
ROOT_SET_CONFLICT: 'ROOT_SET_CONFLICT',
|
|
32
|
+
ROOT_SET_DEPENDENCY_INVALID: 'ROOT_SET_DEPENDENCY_INVALID',
|
|
33
|
+
ROOT_SET_ENTRY_INVALID: 'ROOT_SET_ENTRY_INVALID',
|
|
34
|
+
ROOT_SET_HEAD_INVALID: 'ROOT_SET_HEAD_INVALID',
|
|
35
|
+
ROOT_SET_METADATA_INVALID: 'ROOT_SET_METADATA_INVALID',
|
|
36
|
+
ROOT_SET_REF_INVALID: 'ROOT_SET_REF_INVALID',
|
|
37
|
+
ROOT_SET_REF_UPDATE_FAILED: 'ROOT_SET_REF_UPDATE_FAILED',
|
|
38
|
+
ROOT_SET_RETRY_POLICY_INVALID: 'ROOT_SET_RETRY_POLICY_INVALID',
|
|
39
|
+
ROOT_SET_TARGET_MISSING: 'ROOT_SET_TARGET_MISSING',
|
|
40
|
+
ROOT_SET_TARGET_TYPE_MISMATCH: 'ROOT_SET_TARGET_TYPE_MISMATCH',
|
|
41
|
+
ROOT_SET_TARGET_UNREADABLE: 'ROOT_SET_TARGET_UNREADABLE',
|
|
42
|
+
ROOT_SET_TREE_INVALID: 'ROOT_SET_TREE_INVALID',
|
|
30
43
|
ROTATION_NOT_SUPPORTED: 'ROTATION_NOT_SUPPORTED',
|
|
31
44
|
SECURITY_BOUNDARY_VIOLATION: 'SECURITY_BOUNDARY_VIOLATION',
|
|
32
45
|
STORE_ERROR: 'STORE_ERROR',
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import Oid from '../value-objects/Oid.js';
|
|
4
|
+
import RootSetRef from '../value-objects/RootSetRef.js';
|
|
5
|
+
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
6
|
+
import RootSetRetryPolicy from './RootSetRetryPolicy.js';
|
|
7
|
+
|
|
8
|
+
const EXPECTED_HEAD_UNSET = Symbol('expected-head-unset');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Mutable, current-generation set of Git reachability roots.
|
|
12
|
+
*/
|
|
13
|
+
export default class RootSet {
|
|
14
|
+
#metadataCodec;
|
|
15
|
+
#retry;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {object} options
|
|
19
|
+
* @param {string} options.ref
|
|
20
|
+
* @param {object} options.persistence
|
|
21
|
+
* @param {RootSetRetryPolicy|object} [options.retry]
|
|
22
|
+
* @param {RootSetMetadataCodec} [options.metadataCodec]
|
|
23
|
+
*/
|
|
24
|
+
constructor({
|
|
25
|
+
ref,
|
|
26
|
+
persistence,
|
|
27
|
+
retry,
|
|
28
|
+
metadataCodec = new RootSetMetadataCodec(),
|
|
29
|
+
}) {
|
|
30
|
+
this.ref = RootSetRef.from(ref).toString();
|
|
31
|
+
RootSet.#validatePersistence(persistence);
|
|
32
|
+
this.persistence = persistence;
|
|
33
|
+
this.#metadataCodec = metadataCodec;
|
|
34
|
+
this.#retry = retry instanceof RootSetRetryPolicy ? retry : new RootSetRetryPolicy(retry);
|
|
35
|
+
Object.freeze(this);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @returns {Promise<{ ref: string, headOid: string|null, treeOid: string|null, entries: Array<object> }>}
|
|
40
|
+
*/
|
|
41
|
+
async read() {
|
|
42
|
+
const state = await this.persistence.read();
|
|
43
|
+
return { ...state, entries: state.entries.map((entry) => ({ ...entry })) };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @returns {Promise<Array<object>>}
|
|
48
|
+
*/
|
|
49
|
+
async list() {
|
|
50
|
+
return (await this.read()).entries;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {string} name
|
|
55
|
+
* @returns {Promise<boolean>}
|
|
56
|
+
*/
|
|
57
|
+
async contains(name) {
|
|
58
|
+
return (await this.list()).some((entry) => entry.name === name);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @param {object} entry
|
|
63
|
+
* @returns {Promise<object>}
|
|
64
|
+
*/
|
|
65
|
+
async put(entry) {
|
|
66
|
+
const normalizedEntry = this.#metadataCodec.normalizeEntries([entry])[0];
|
|
67
|
+
let previous = null;
|
|
68
|
+
const result = await this.#mutateEntries((entries) => {
|
|
69
|
+
const byName = new Map(entries.map((current) => [current.name, current]));
|
|
70
|
+
previous = byName.get(normalizedEntry.name) ?? null;
|
|
71
|
+
byName.set(normalizedEntry.name, normalizedEntry);
|
|
72
|
+
return byName.values();
|
|
73
|
+
}, `root-set: put ${normalizedEntry.name}`);
|
|
74
|
+
return { ...result, entry: normalizedEntry, previous };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {{ name: string }} options
|
|
79
|
+
* @returns {Promise<object>}
|
|
80
|
+
*/
|
|
81
|
+
async remove({ name }) {
|
|
82
|
+
let removed = null;
|
|
83
|
+
const result = await this.#mutateEntries((entries) => {
|
|
84
|
+
const next = [];
|
|
85
|
+
for (const entry of entries) {
|
|
86
|
+
if (entry.name === name) {
|
|
87
|
+
removed = entry;
|
|
88
|
+
} else {
|
|
89
|
+
next.push(entry);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return next;
|
|
93
|
+
}, `root-set: remove ${name}`);
|
|
94
|
+
return { ...result, removed };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {{ entries: Iterable<object>, expectedHeadOid?: string|null }} options
|
|
99
|
+
* @returns {Promise<object>}
|
|
100
|
+
*/
|
|
101
|
+
async replace({ entries, expectedHeadOid = EXPECTED_HEAD_UNSET }) {
|
|
102
|
+
const desired = this.#metadataCodec.normalizeEntries(entries);
|
|
103
|
+
return await this.#mutateEntries(
|
|
104
|
+
() => desired,
|
|
105
|
+
'root-set: replace current roots',
|
|
106
|
+
RootSet.#normalizeExpectedHead(expectedHeadOid),
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @param {(entries: ReadonlyArray<object>) => Iterable<object>|Promise<Iterable<object>>} mutator
|
|
112
|
+
* @param {{ expectedHeadOid?: string|null }} [options]
|
|
113
|
+
* @returns {Promise<object>}
|
|
114
|
+
*/
|
|
115
|
+
async mutate(mutator, { expectedHeadOid = EXPECTED_HEAD_UNSET } = {}) {
|
|
116
|
+
if (typeof mutator !== 'function') {
|
|
117
|
+
throw new CasError(
|
|
118
|
+
'Root-set mutator must be a function',
|
|
119
|
+
ErrorCodes.ROOT_SET_ENTRY_INVALID,
|
|
120
|
+
{ mutatorType: typeof mutator },
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return await this.#mutateEntries(async (entries) => (
|
|
124
|
+
await mutator(Object.freeze(entries.map((entry) => Object.freeze({ ...entry }))))
|
|
125
|
+
), 'root-set: mutate current roots', RootSet.#normalizeExpectedHead(expectedHeadOid));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Reports whether the current ref, commit, metadata, and reachability tree
|
|
130
|
+
* agree. It does not mutate the repository.
|
|
131
|
+
*
|
|
132
|
+
* @returns {Promise<object>}
|
|
133
|
+
*/
|
|
134
|
+
async doctor() {
|
|
135
|
+
try {
|
|
136
|
+
const state = await this.read();
|
|
137
|
+
const targetReport = await this.persistence.inspectTargets(state.entries);
|
|
138
|
+
return {
|
|
139
|
+
healthy: targetReport.healthy,
|
|
140
|
+
ref: this.ref,
|
|
141
|
+
headOid: state.headOid,
|
|
142
|
+
treeOid: state.treeOid,
|
|
143
|
+
entryCount: state.entries.length,
|
|
144
|
+
entries: state.entries,
|
|
145
|
+
policyCounts: RootSet.#policyCounts(state.entries),
|
|
146
|
+
reachabilityCounts: {
|
|
147
|
+
anchored: targetReport.targets.filter((target) => target.exists === true).length,
|
|
148
|
+
missing: targetReport.targets.filter((target) => target.exists === false).length,
|
|
149
|
+
unknown: targetReport.targets.filter((target) => target.exists === null).length,
|
|
150
|
+
orphaned: 0,
|
|
151
|
+
volatile: 0,
|
|
152
|
+
},
|
|
153
|
+
targets: targetReport.targets,
|
|
154
|
+
issues: targetReport.issues.map(RootSet.#publicIssue),
|
|
155
|
+
};
|
|
156
|
+
} catch (err) {
|
|
157
|
+
return {
|
|
158
|
+
healthy: false,
|
|
159
|
+
ref: this.ref,
|
|
160
|
+
error: {
|
|
161
|
+
code: err?.code ?? ErrorCodes.ROOT_SET_HEAD_INVALID,
|
|
162
|
+
message: err instanceof Error ? err.message : String(err),
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Replaces a missing or malformed head from an authoritative entry list.
|
|
170
|
+
* This deliberately does not trust the current metadata.
|
|
171
|
+
*
|
|
172
|
+
* @param {{ entries: Iterable<object> }} options
|
|
173
|
+
* @returns {Promise<object>}
|
|
174
|
+
*/
|
|
175
|
+
async repair({ entries }) {
|
|
176
|
+
const desired = this.#metadataCodec.normalizeEntries(entries);
|
|
177
|
+
for (let attempt = 0; attempt < this.#retry.maxAttempts; attempt++) {
|
|
178
|
+
const expectedHeadOid = await this.persistence.resolveRefOnly();
|
|
179
|
+
try {
|
|
180
|
+
const written = await this.persistence.write({
|
|
181
|
+
entries: desired,
|
|
182
|
+
expectedHeadOid,
|
|
183
|
+
message: 'root-set: repair current roots',
|
|
184
|
+
});
|
|
185
|
+
return { repaired: true, ...written };
|
|
186
|
+
} catch (err) {
|
|
187
|
+
await this.#handleConflict(err, attempt);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
throw new CasError('Root-set repair attempts exhausted', ErrorCodes.ROOT_SET_CONFLICT);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async #mutateEntries(transform, message, expectedHeadOid = EXPECTED_HEAD_UNSET) {
|
|
194
|
+
for (let attempt = 0; attempt < this.#retry.maxAttempts; attempt++) {
|
|
195
|
+
const state = await this.read();
|
|
196
|
+
this.#assertExpectedHead(state.headOid, expectedHeadOid);
|
|
197
|
+
const entries = this.#metadataCodec.normalizeEntries(await transform(state.entries));
|
|
198
|
+
if (RootSet.#entriesEqual(state.entries, entries)) {
|
|
199
|
+
return {
|
|
200
|
+
changed: false,
|
|
201
|
+
commitOid: state.headOid,
|
|
202
|
+
treeOid: state.treeOid,
|
|
203
|
+
entries,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const written = await this.persistence.write({
|
|
208
|
+
entries,
|
|
209
|
+
expectedHeadOid: state.headOid,
|
|
210
|
+
message,
|
|
211
|
+
});
|
|
212
|
+
return { changed: true, ...written };
|
|
213
|
+
} catch (err) {
|
|
214
|
+
if (expectedHeadOid !== EXPECTED_HEAD_UNSET) {
|
|
215
|
+
throw err;
|
|
216
|
+
}
|
|
217
|
+
await this.#handleConflict(err, attempt);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
throw new CasError('Root-set mutation attempts exhausted', ErrorCodes.ROOT_SET_CONFLICT);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async #handleConflict(err, attempt) {
|
|
224
|
+
const finalAttempt = attempt + 1 >= this.#retry.maxAttempts;
|
|
225
|
+
if (!this.#retry.isRetryable(err) || finalAttempt) {
|
|
226
|
+
throw err;
|
|
227
|
+
}
|
|
228
|
+
await this.#retry.waitBeforeRetry(attempt);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
#assertExpectedHead(actualHeadOid, expectedHeadOid) {
|
|
232
|
+
if (expectedHeadOid === EXPECTED_HEAD_UNSET || actualHeadOid === expectedHeadOid) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
throw new CasError(
|
|
236
|
+
'Root-set head does not match the caller expectation',
|
|
237
|
+
ErrorCodes.ROOT_SET_CONFLICT,
|
|
238
|
+
{ ref: this.ref, expectedHeadOid, actualHeadOid },
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
static #normalizeExpectedHead(expectedHeadOid) {
|
|
243
|
+
if (expectedHeadOid === EXPECTED_HEAD_UNSET || expectedHeadOid === null) {
|
|
244
|
+
return expectedHeadOid;
|
|
245
|
+
}
|
|
246
|
+
return Oid.from(expectedHeadOid).toString();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static #entriesEqual(left, right) {
|
|
250
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
static #policyCounts(entries) {
|
|
254
|
+
return entries.reduce((counts, entry) => ({
|
|
255
|
+
...counts,
|
|
256
|
+
[entry.retention]: counts[entry.retention] + 1,
|
|
257
|
+
}), { pinned: 0, evictable: 0 });
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
static #publicIssue(issue) {
|
|
261
|
+
return Object.fromEntries(
|
|
262
|
+
Object.entries(issue).filter(([key]) => key !== 'originalError'),
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
static #validatePersistence(persistence) {
|
|
267
|
+
const missing = ['read', 'write', 'resolveRefOnly', 'inspectTargets']
|
|
268
|
+
.filter((method) => typeof persistence?.[method] !== 'function');
|
|
269
|
+
if (missing.length > 0) {
|
|
270
|
+
throw new CasError(
|
|
271
|
+
'RootSet requires a complete RootSetPersistence',
|
|
272
|
+
ErrorCodes.ROOT_SET_DEPENDENCY_INVALID,
|
|
273
|
+
{ missing },
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|