@git-stunts/git-cas 6.1.0 → 6.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/ARCHITECTURE.md +108 -3
- package/CHANGELOG.md +80 -2
- package/GUIDE.md +114 -13
- package/README.md +51 -4
- package/UPGRADING.md +43 -0
- package/build-info.json +2 -2
- package/docs/API.md +751 -16
- package/docs/releases/v6.2.0.md +131 -0
- package/index.d.ts +879 -2
- package/index.js +326 -1
- package/package.json +3 -2
- package/src/domain/errors/Codes.js +35 -0
- package/src/domain/helpers/assertCanonicalTimestamp.js +15 -0
- package/src/domain/helpers/handleTarget.js +48 -0
- package/src/domain/helpers/isCanonicalCollectionKey.js +35 -0
- package/src/domain/services/AssetService.js +213 -0
- package/src/domain/services/BundleDescriptorCodec.js +286 -0
- package/src/domain/services/BundleFanoutBuilder.js +247 -0
- package/src/domain/services/BundleService.js +848 -0
- package/src/domain/services/CacheCandidateHeap.js +71 -0
- package/src/domain/services/CacheIndex.js +362 -0
- package/src/domain/services/CacheMetadataCodec.js +164 -0
- package/src/domain/services/CachePolicyEnforcer.js +144 -0
- package/src/domain/services/CacheSet.js +703 -0
- package/src/domain/services/CacheSetRegistry.js +88 -0
- package/src/domain/services/CasService.d.ts +2 -1
- package/src/domain/services/ExpiringSet.js +496 -0
- package/src/domain/services/ExpiringSetIndex.js +254 -0
- package/src/domain/services/ExpiringSetMetadataCodec.js +160 -0
- package/src/domain/services/ExpiringSetRegistry.js +101 -0
- package/src/domain/services/PageService.js +175 -0
- package/src/domain/services/PublicationService.js +315 -0
- package/src/domain/services/RepositoryDoctor.js +649 -0
- package/src/domain/services/RetentionService.js +120 -0
- package/src/domain/services/RootSet.js +10 -6
- package/src/domain/services/RootSetMetadataCodec.js +8 -2
- package/src/domain/services/RootSetPersistence.js +3 -2
- package/src/domain/services/StagingEvidence.js +39 -0
- package/src/domain/value-objects/ApplicationHandle.js +40 -0
- package/src/domain/value-objects/AssetHandle.js +163 -0
- package/src/domain/value-objects/BundleHandle.js +32 -0
- package/src/domain/value-objects/BundleLimits.js +80 -0
- package/src/domain/value-objects/BundlePath.js +78 -0
- package/src/domain/value-objects/CacheHit.js +93 -0
- package/src/domain/value-objects/CacheKey.js +32 -0
- package/src/domain/value-objects/CachePolicy.js +59 -0
- package/src/domain/value-objects/CacheSetRef.js +40 -0
- package/src/domain/value-objects/CollectionNamespace.js +54 -0
- package/src/domain/value-objects/ExpiringMarker.js +81 -0
- package/src/domain/value-objects/ExpiringSetKey.js +28 -0
- package/src/domain/value-objects/ExpiringSetRef.js +42 -0
- package/src/domain/value-objects/OpaqueHandle.js +171 -0
- package/src/domain/value-objects/PageHandle.js +34 -0
- package/src/domain/value-objects/RetentionWitness.js +101 -0
- package/src/domain/value-objects/StagedAsset.js +63 -0
- package/src/domain/value-objects/StagedBundle.js +66 -0
- package/src/domain/value-objects/StagedPage.js +47 -0
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +48 -20
- package/src/infrastructure/adapters/GitRefAdapter.js +6 -5
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +164 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +11 -1
- package/src/ports/GitRefPort.js +1 -0
- package/src/ports/RepositoryInspectionPort.js +33 -0
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
2
|
+
import createCasError from '../errors/createCasError.js';
|
|
3
|
+
import { CACHE_SET_REF_PREFIX } from '../value-objects/CacheSetRef.js';
|
|
4
|
+
import { EXPIRING_SET_REF_PREFIX } from '../value-objects/ExpiringSetRef.js';
|
|
5
|
+
import { ROOT_SET_REF_PREFIX } from '../value-objects/RootSetRef.js';
|
|
6
|
+
import { VAULT_REF } from './VaultPersistence.js';
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_REPOSITORY_GRACE_PERIOD_MS = 14 * 24 * 60 * 60 * 1_000;
|
|
9
|
+
const DEFAULT_MAX_COLLECTIONS_PER_KIND = 100;
|
|
10
|
+
const MAX_COLLECTIONS_PER_KIND = 1_000;
|
|
11
|
+
|
|
12
|
+
/** Composes bounded, non-mutating repository and managed-collection evidence. */
|
|
13
|
+
export default class RepositoryDoctor {
|
|
14
|
+
constructor({ repository, rootSets, caches, expiringSets, vault, clock }) {
|
|
15
|
+
assertDependencies({ repository, rootSets, caches, expiringSets, vault });
|
|
16
|
+
this.repository = repository;
|
|
17
|
+
this.rootSets = rootSets;
|
|
18
|
+
this.caches = caches;
|
|
19
|
+
this.expiringSets = expiringSets;
|
|
20
|
+
this.vault = vault;
|
|
21
|
+
this.clock = clock ?? { now: () => new Date() };
|
|
22
|
+
Object.freeze(this);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async doctor(options = {}) {
|
|
26
|
+
const observedAt = this.#now();
|
|
27
|
+
const policy = inspectionPolicy(options, observedAt);
|
|
28
|
+
const limitations = baseLimitations();
|
|
29
|
+
const { refs, usage } = await this.#inventoryUsage(policy.maxCollectionsPerKind, limitations);
|
|
30
|
+
const objects = await this.#inventoryObjects(policy.expiresBefore);
|
|
31
|
+
|
|
32
|
+
if (!objects.consistent) {
|
|
33
|
+
limitations.push(
|
|
34
|
+
limitation(
|
|
35
|
+
'REPOSITORY_CHANGED_DURING_INSPECTION',
|
|
36
|
+
'Object counts changed between streamed Git inventories; derived counts are unknown.'
|
|
37
|
+
)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
addTruncationLimitations(refs, limitations);
|
|
41
|
+
|
|
42
|
+
const healthy = objects.consistent && usageHealthy(usage);
|
|
43
|
+
return deepFreeze({
|
|
44
|
+
version: 1,
|
|
45
|
+
healthy,
|
|
46
|
+
observedAt,
|
|
47
|
+
completedAt: this.#now(),
|
|
48
|
+
policy: {
|
|
49
|
+
gracePeriodMs: policy.gracePeriodMs,
|
|
50
|
+
expiresBefore: policy.expiresBefore,
|
|
51
|
+
maxCollectionsPerKind: policy.maxCollectionsPerKind,
|
|
52
|
+
},
|
|
53
|
+
repository: {
|
|
54
|
+
objects: objects.report,
|
|
55
|
+
roots: {
|
|
56
|
+
refCount: refs.refCount,
|
|
57
|
+
reflogsIncluded: true,
|
|
58
|
+
reflogCount: null,
|
|
59
|
+
},
|
|
60
|
+
evidence: {
|
|
61
|
+
anchoredInventory: 'refs-and-reflogs',
|
|
62
|
+
prunableInspection: 'dry-run',
|
|
63
|
+
mutatesRepository: false,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
usage,
|
|
67
|
+
limitations,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async #inventoryObjects(expiresBefore) {
|
|
72
|
+
const total = { objectCount: 0, logicalBytes: 0, physicalBytes: 0 };
|
|
73
|
+
for await (const object of this.repository.iterateObjects()) {
|
|
74
|
+
total.objectCount += 1;
|
|
75
|
+
total.logicalBytes += object.logicalBytes;
|
|
76
|
+
total.physicalBytes += object.physicalBytes;
|
|
77
|
+
assertSafeTotals(total);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const anchoredCount = await count(this.repository.iterateReachableObjectIds());
|
|
81
|
+
const volatileCount = await count(this.repository.iteratePrunableObjects({ expiresBefore }));
|
|
82
|
+
const anchoredPhysicalBytes = await this.repository.reachablePhysicalBytes();
|
|
83
|
+
const unreachableCount = total.objectCount - anchoredCount;
|
|
84
|
+
const orphanedCount = unreachableCount - volatileCount;
|
|
85
|
+
const unreachablePhysicalBytes = total.physicalBytes - anchoredPhysicalBytes;
|
|
86
|
+
const consistent = [unreachableCount, orphanedCount, unreachablePhysicalBytes].every(
|
|
87
|
+
(value) => Number.isSafeInteger(value) && value >= 0
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
consistent,
|
|
92
|
+
report: {
|
|
93
|
+
total,
|
|
94
|
+
anchored: { objectCount: anchoredCount, physicalBytes: anchoredPhysicalBytes },
|
|
95
|
+
orphaned: {
|
|
96
|
+
objectCount: consistent ? orphanedCount : null,
|
|
97
|
+
physicalBytes: null,
|
|
98
|
+
},
|
|
99
|
+
volatile: { objectCount: volatileCount, physicalBytes: null },
|
|
100
|
+
unreachable: {
|
|
101
|
+
objectCount: consistent ? unreachableCount : null,
|
|
102
|
+
physicalBytes: consistent ? unreachablePhysicalBytes : null,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async #inventoryUsage(limit, limitations) {
|
|
109
|
+
const result = usageInventory(limit);
|
|
110
|
+
for await (const record of this.repository.iterateRefs()) {
|
|
111
|
+
result.refCount += 1;
|
|
112
|
+
if (record.ref.startsWith(CACHE_SET_REF_PREFIX)) {
|
|
113
|
+
result.caches.observed += 1;
|
|
114
|
+
recordCache(result.caches, await this.#inspectCache(record));
|
|
115
|
+
} else if (record.ref.startsWith(ROOT_SET_REF_PREFIX)) {
|
|
116
|
+
result.rootSets.observed += 1;
|
|
117
|
+
recordRootSet(result.rootSets, await this.#inspectRootSet(record));
|
|
118
|
+
} else if (record.ref.startsWith(EXPIRING_SET_REF_PREFIX)) {
|
|
119
|
+
result.expiringSets.observed += 1;
|
|
120
|
+
recordExpiringSet(result.expiringSets, await this.#inspectExpiringSet(record));
|
|
121
|
+
} else if (record.ref === VAULT_REF) {
|
|
122
|
+
result.vault = record;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
refs: result,
|
|
127
|
+
usage: {
|
|
128
|
+
caches: collectionGroup(result.caches),
|
|
129
|
+
rootSets: collectionGroup(result.rootSets),
|
|
130
|
+
expiringSets: collectionGroup(result.expiringSets),
|
|
131
|
+
vault: await this.#inspectVault(result.vault, limitations),
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async #inspectCache(record) {
|
|
137
|
+
const namespace = record.ref.slice(CACHE_SET_REF_PREFIX.length);
|
|
138
|
+
try {
|
|
139
|
+
const cache = await this.caches.open({ namespace });
|
|
140
|
+
return cacheUsage(record, namespace, await cache.doctor());
|
|
141
|
+
} catch (error) {
|
|
142
|
+
return unhealthyCacheUsage({ record, namespace, error });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async #inspectRootSet(record) {
|
|
147
|
+
try {
|
|
148
|
+
const rootSet = await this.rootSets.open({ ref: record.ref });
|
|
149
|
+
return rootSetUsage(record, await rootSet.doctor());
|
|
150
|
+
} catch (error) {
|
|
151
|
+
return unhealthyRootSetUsage({ record, error });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async #inspectExpiringSet(record) {
|
|
156
|
+
const namespace = record.ref.slice(EXPIRING_SET_REF_PREFIX.length);
|
|
157
|
+
try {
|
|
158
|
+
const expiringSet = await this.expiringSets.open({ namespace });
|
|
159
|
+
return expiringSetUsage(record, namespace, await expiringSet.doctor());
|
|
160
|
+
} catch (error) {
|
|
161
|
+
return unhealthyExpiringSetUsage({ record, namespace, error });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async #inspectVault(record, limitations) {
|
|
166
|
+
if (record === null) {
|
|
167
|
+
return {
|
|
168
|
+
ref: VAULT_REF,
|
|
169
|
+
present: false,
|
|
170
|
+
healthy: true,
|
|
171
|
+
generation: null,
|
|
172
|
+
entryCount: 0,
|
|
173
|
+
physicalBytes: null,
|
|
174
|
+
privacy: null,
|
|
175
|
+
reachability: null,
|
|
176
|
+
issues: [],
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
const metadata = await this.vault.getVaultMetadata();
|
|
181
|
+
const privacy = Boolean(metadata?.privacy?.enabled);
|
|
182
|
+
if (privacy) {
|
|
183
|
+
limitations.push(
|
|
184
|
+
limitation(
|
|
185
|
+
'VAULT_ENTRY_COUNT_REQUIRES_KEY',
|
|
186
|
+
'Privacy-mode vault entry count is unknown without caller-provided key material.'
|
|
187
|
+
)
|
|
188
|
+
);
|
|
189
|
+
return healthyVault(record, { entryCount: null, privacy });
|
|
190
|
+
}
|
|
191
|
+
const state = await this.vault.readState();
|
|
192
|
+
return healthyVault(record, { entryCount: state.entries.size, privacy });
|
|
193
|
+
} catch (error) {
|
|
194
|
+
return {
|
|
195
|
+
ref: VAULT_REF,
|
|
196
|
+
present: true,
|
|
197
|
+
healthy: false,
|
|
198
|
+
generation: record.oid,
|
|
199
|
+
entryCount: null,
|
|
200
|
+
physicalBytes: null,
|
|
201
|
+
privacy: null,
|
|
202
|
+
reachability: 'anchored',
|
|
203
|
+
issues: [publicError(error)],
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
#now() {
|
|
209
|
+
const value = this.clock.now();
|
|
210
|
+
if (!(value instanceof Date) || Number.isNaN(value.getTime())) {
|
|
211
|
+
throw createCasError(
|
|
212
|
+
'Repository doctor clock must return a valid Date',
|
|
213
|
+
ErrorCodes.REPOSITORY_INSPECTION_INVALID
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
return value.toISOString();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function inspectionPolicy(options, observedAt) {
|
|
221
|
+
assertOptionsObject(options);
|
|
222
|
+
const maxCollectionsPerKind = collectionLimit(options.maxCollectionsPerKind);
|
|
223
|
+
if (options.expiresBefore !== undefined) {
|
|
224
|
+
return explicitExpiryPolicy(options, maxCollectionsPerKind);
|
|
225
|
+
}
|
|
226
|
+
return gracePeriodPolicy(options, observedAt, maxCollectionsPerKind);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function assertOptionsObject(options) {
|
|
230
|
+
if (!options || typeof options !== 'object' || Array.isArray(options)) {
|
|
231
|
+
throw invalidOptions('Repository doctor options must be an object');
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function collectionLimit(value = DEFAULT_MAX_COLLECTIONS_PER_KIND) {
|
|
236
|
+
if (!Number.isSafeInteger(value) || value < 1 || value > MAX_COLLECTIONS_PER_KIND) {
|
|
237
|
+
throw invalidOptions(
|
|
238
|
+
`maxCollectionsPerKind must be an integer from 1 to ${MAX_COLLECTIONS_PER_KIND}`
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
return value;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function explicitExpiryPolicy(options, maxCollectionsPerKind) {
|
|
245
|
+
if (options.gracePeriodMs !== undefined) {
|
|
246
|
+
throw invalidOptions('expiresBefore and gracePeriodMs are mutually exclusive');
|
|
247
|
+
}
|
|
248
|
+
const parsed = new Date(options.expiresBefore);
|
|
249
|
+
if (
|
|
250
|
+
typeof options.expiresBefore !== 'string' ||
|
|
251
|
+
Number.isNaN(parsed.getTime()) ||
|
|
252
|
+
parsed.toISOString() !== options.expiresBefore
|
|
253
|
+
) {
|
|
254
|
+
throw invalidOptions('expiresBefore must be a canonical millisecond UTC timestamp');
|
|
255
|
+
}
|
|
256
|
+
return { expiresBefore: options.expiresBefore, gracePeriodMs: null, maxCollectionsPerKind };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function gracePeriodPolicy(options, observedAt, maxCollectionsPerKind) {
|
|
260
|
+
const gracePeriodMs = options.gracePeriodMs ?? DEFAULT_REPOSITORY_GRACE_PERIOD_MS;
|
|
261
|
+
if (!Number.isSafeInteger(gracePeriodMs) || gracePeriodMs < 0) {
|
|
262
|
+
throw invalidOptions('gracePeriodMs must be a non-negative safe integer');
|
|
263
|
+
}
|
|
264
|
+
const expiresAt = new Date(Date.parse(observedAt) - gracePeriodMs);
|
|
265
|
+
if (Number.isNaN(expiresAt.getTime())) {
|
|
266
|
+
throw invalidOptions('gracePeriodMs exceeds the supported timestamp range');
|
|
267
|
+
}
|
|
268
|
+
return {
|
|
269
|
+
expiresBefore: expiresAt.toISOString(),
|
|
270
|
+
gracePeriodMs,
|
|
271
|
+
maxCollectionsPerKind,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function cacheUsage(record, namespace, report) {
|
|
276
|
+
if (!report.state) {
|
|
277
|
+
return cacheUsageWithoutState(record, namespace, report);
|
|
278
|
+
}
|
|
279
|
+
const { state } = report;
|
|
280
|
+
const policy = report.policy ? report.policy.limits : state.policy;
|
|
281
|
+
return {
|
|
282
|
+
namespace,
|
|
283
|
+
ref: record.ref,
|
|
284
|
+
generation: rootGeneration(report, record.oid),
|
|
285
|
+
healthy: report.healthy,
|
|
286
|
+
entryCount: state.entryCount,
|
|
287
|
+
logicalBytes: state.logicalBytes,
|
|
288
|
+
physicalBytes: null,
|
|
289
|
+
retention: {
|
|
290
|
+
pinnedEntries: state.pinnedEntries,
|
|
291
|
+
evictableEntries: state.evictableEntries,
|
|
292
|
+
},
|
|
293
|
+
reachability: 'anchored',
|
|
294
|
+
age: {
|
|
295
|
+
createdAt: state.createdAt,
|
|
296
|
+
updatedAt: state.updatedAt,
|
|
297
|
+
oldestAccessedAt: state.oldestAccessedAt,
|
|
298
|
+
},
|
|
299
|
+
expiry: {
|
|
300
|
+
expiredEntries: state.expiredEntries,
|
|
301
|
+
nextExpiry: state.nextExpiry,
|
|
302
|
+
},
|
|
303
|
+
policy,
|
|
304
|
+
issues: report.issues ?? [],
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function cacheUsageWithoutState(record, namespace, report) {
|
|
309
|
+
const knownEmpty = report.healthy;
|
|
310
|
+
return {
|
|
311
|
+
namespace,
|
|
312
|
+
ref: record.ref,
|
|
313
|
+
generation: rootGeneration(report, record.oid),
|
|
314
|
+
healthy: report.healthy,
|
|
315
|
+
entryCount: knownEmpty ? 0 : null,
|
|
316
|
+
logicalBytes: knownEmpty ? 0 : null,
|
|
317
|
+
physicalBytes: null,
|
|
318
|
+
retention: knownEmpty ? { pinnedEntries: 0, evictableEntries: 0 } : null,
|
|
319
|
+
reachability: 'anchored',
|
|
320
|
+
age: knownEmpty ? { createdAt: null, updatedAt: null, oldestAccessedAt: null } : null,
|
|
321
|
+
expiry: knownEmpty ? { expiredEntries: 0, nextExpiry: null } : null,
|
|
322
|
+
policy: null,
|
|
323
|
+
issues: report.issues ?? [],
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function rootSetUsage(record, report) {
|
|
328
|
+
const counts = report.policyCounts;
|
|
329
|
+
return {
|
|
330
|
+
ref: record.ref,
|
|
331
|
+
generation: report.headOid ?? record.oid,
|
|
332
|
+
healthy: report.healthy,
|
|
333
|
+
entryCount: report.entryCount ?? null,
|
|
334
|
+
physicalBytes: null,
|
|
335
|
+
retention: counts ? { pinnedEntries: counts.pinned, evictableEntries: counts.evictable } : null,
|
|
336
|
+
reachability: report.reachabilityCounts ?? null,
|
|
337
|
+
issues: report.issues ?? errorIssues(report),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function expiringSetUsage(record, namespace, report) {
|
|
342
|
+
if (!report.state) {
|
|
343
|
+
return expiringSetUsageWithoutState(record, namespace, report);
|
|
344
|
+
}
|
|
345
|
+
return {
|
|
346
|
+
namespace,
|
|
347
|
+
ref: record.ref,
|
|
348
|
+
generation: rootGeneration(report, record.oid),
|
|
349
|
+
healthy: report.healthy,
|
|
350
|
+
entryCount: report.state.entryCount,
|
|
351
|
+
physicalBytes: null,
|
|
352
|
+
reachability: 'anchored',
|
|
353
|
+
age: {
|
|
354
|
+
createdAt: report.state.createdAt,
|
|
355
|
+
updatedAt: report.state.updatedAt,
|
|
356
|
+
},
|
|
357
|
+
expiry: {
|
|
358
|
+
liveEntries: report.observed.liveEntries,
|
|
359
|
+
expiredEntries: report.observed.expiredEntries,
|
|
360
|
+
nextExpiry: report.state.nextExpiry,
|
|
361
|
+
},
|
|
362
|
+
issues: report.issues ?? [],
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function expiringSetUsageWithoutState(record, namespace, report) {
|
|
367
|
+
const knownEmpty = report.healthy;
|
|
368
|
+
return {
|
|
369
|
+
namespace,
|
|
370
|
+
ref: record.ref,
|
|
371
|
+
generation: rootGeneration(report, record.oid),
|
|
372
|
+
healthy: report.healthy,
|
|
373
|
+
entryCount: knownEmpty ? 0 : null,
|
|
374
|
+
physicalBytes: null,
|
|
375
|
+
reachability: 'anchored',
|
|
376
|
+
age: knownEmpty ? { createdAt: null, updatedAt: null } : null,
|
|
377
|
+
expiry: knownEmpty ? { liveEntries: 0, expiredEntries: 0, nextExpiry: null } : null,
|
|
378
|
+
issues: report.issues ?? [],
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function rootGeneration(report, fallback) {
|
|
383
|
+
return report.root?.headOid ?? fallback;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function errorIssues(report) {
|
|
387
|
+
return report.error ? [report.error] : [];
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
async function count(iterable) {
|
|
391
|
+
let result = 0;
|
|
392
|
+
for await (const value of iterable) {
|
|
393
|
+
void value;
|
|
394
|
+
result += 1;
|
|
395
|
+
}
|
|
396
|
+
return result;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function collectionInventory(totals, detailLimit) {
|
|
400
|
+
return { observed: 0, inspected: 0, healthy: true, totals, entries: [], detailLimit };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function usageInventory(detailLimit) {
|
|
404
|
+
return {
|
|
405
|
+
refCount: 0,
|
|
406
|
+
caches: collectionInventory(
|
|
407
|
+
{
|
|
408
|
+
entryCount: 0,
|
|
409
|
+
logicalBytes: 0,
|
|
410
|
+
pinnedEntries: 0,
|
|
411
|
+
evictableEntries: 0,
|
|
412
|
+
expiredEntries: 0,
|
|
413
|
+
},
|
|
414
|
+
detailLimit
|
|
415
|
+
),
|
|
416
|
+
rootSets: collectionInventory(
|
|
417
|
+
{ entryCount: 0, pinnedEntries: 0, evictableEntries: 0 },
|
|
418
|
+
detailLimit
|
|
419
|
+
),
|
|
420
|
+
expiringSets: collectionInventory(
|
|
421
|
+
{ entryCount: 0, liveEntries: 0, expiredEntries: 0 },
|
|
422
|
+
detailLimit
|
|
423
|
+
),
|
|
424
|
+
vault: null,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function coverage(inventory) {
|
|
429
|
+
return {
|
|
430
|
+
observed: inventory.observed,
|
|
431
|
+
inspected: inventory.inspected,
|
|
432
|
+
detailed: inventory.entries.length,
|
|
433
|
+
complete: inventory.observed === inventory.inspected,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function recordCache(inventory, entry) {
|
|
438
|
+
recordCollection(inventory, entry, {
|
|
439
|
+
entryCount: entry.entryCount,
|
|
440
|
+
logicalBytes: entry.logicalBytes,
|
|
441
|
+
pinnedEntries: entry.retention?.pinnedEntries,
|
|
442
|
+
evictableEntries: entry.retention?.evictableEntries,
|
|
443
|
+
expiredEntries: entry.expiry?.expiredEntries,
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function recordRootSet(inventory, entry) {
|
|
448
|
+
recordCollection(inventory, entry, {
|
|
449
|
+
entryCount: entry.entryCount,
|
|
450
|
+
pinnedEntries: entry.retention?.pinnedEntries,
|
|
451
|
+
evictableEntries: entry.retention?.evictableEntries,
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function recordExpiringSet(inventory, entry) {
|
|
456
|
+
recordCollection(inventory, entry, {
|
|
457
|
+
entryCount: entry.entryCount,
|
|
458
|
+
liveEntries: entry.expiry?.liveEntries,
|
|
459
|
+
expiredEntries: entry.expiry?.expiredEntries,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function recordCollection(inventory, entry, values) {
|
|
464
|
+
inventory.inspected += 1;
|
|
465
|
+
inventory.healthy &&= entry.healthy;
|
|
466
|
+
for (const [key, value] of Object.entries(values)) {
|
|
467
|
+
addKnown(inventory.totals, key, value);
|
|
468
|
+
}
|
|
469
|
+
if (inventory.entries.length < inventory.detailLimit) {
|
|
470
|
+
inventory.entries.push(entry);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function collectionGroup(inventory) {
|
|
475
|
+
return {
|
|
476
|
+
healthy: inventory.healthy,
|
|
477
|
+
coverage: coverage(inventory),
|
|
478
|
+
totals: inventory.totals,
|
|
479
|
+
entries: inventory.entries,
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function addKnown(totals, key, value) {
|
|
484
|
+
if (totals[key] === null) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
const next = totals[key] + value;
|
|
488
|
+
totals[key] = Number.isSafeInteger(value) && Number.isSafeInteger(next) ? next : null;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function healthyVault(record, { entryCount, privacy }) {
|
|
492
|
+
return {
|
|
493
|
+
ref: VAULT_REF,
|
|
494
|
+
present: true,
|
|
495
|
+
healthy: true,
|
|
496
|
+
generation: record.oid,
|
|
497
|
+
entryCount,
|
|
498
|
+
physicalBytes: null,
|
|
499
|
+
privacy,
|
|
500
|
+
reachability: 'anchored',
|
|
501
|
+
issues: [],
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function usageHealthy(usage) {
|
|
506
|
+
return (
|
|
507
|
+
usage.caches.healthy &&
|
|
508
|
+
usage.rootSets.healthy &&
|
|
509
|
+
usage.expiringSets.healthy &&
|
|
510
|
+
usage.vault.healthy
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function unhealthyBase({ record, namespace, error }) {
|
|
515
|
+
return {
|
|
516
|
+
...(namespace === undefined ? {} : { namespace }),
|
|
517
|
+
ref: record.ref,
|
|
518
|
+
generation: record.oid,
|
|
519
|
+
healthy: false,
|
|
520
|
+
entryCount: null,
|
|
521
|
+
physicalBytes: null,
|
|
522
|
+
reachability: 'anchored',
|
|
523
|
+
issues: [publicError(error)],
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function unhealthyCacheUsage(options) {
|
|
528
|
+
return {
|
|
529
|
+
...unhealthyBase(options),
|
|
530
|
+
logicalBytes: null,
|
|
531
|
+
retention: null,
|
|
532
|
+
age: null,
|
|
533
|
+
expiry: null,
|
|
534
|
+
policy: null,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function unhealthyRootSetUsage(options) {
|
|
539
|
+
return { ...unhealthyBase(options), retention: null };
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function unhealthyExpiringSetUsage(options) {
|
|
543
|
+
return { ...unhealthyBase(options), age: null, expiry: null };
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function publicError(error) {
|
|
547
|
+
return {
|
|
548
|
+
code: error?.code ?? ErrorCodes.REPOSITORY_INSPECTION_INVALID,
|
|
549
|
+
message: error instanceof Error ? error.message : String(error),
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function baseLimitations() {
|
|
554
|
+
return [
|
|
555
|
+
limitation(
|
|
556
|
+
'SHARED_PHYSICAL_BYTES_UNATTRIBUTABLE',
|
|
557
|
+
'Deduplicated Git objects cannot be assigned exactly to one cache, root set, or vault.'
|
|
558
|
+
),
|
|
559
|
+
limitation(
|
|
560
|
+
'PACKED_OBJECT_AGE_UNAVAILABLE',
|
|
561
|
+
'Git prune dry-run reports loose candidates; packed unreachable object age is not observable.'
|
|
562
|
+
),
|
|
563
|
+
limitation(
|
|
564
|
+
'REFLOG_COUNT_UNAVAILABLE',
|
|
565
|
+
'Reflogs participate in reachability, but this bounded report does not enumerate reflog entries.'
|
|
566
|
+
),
|
|
567
|
+
limitation(
|
|
568
|
+
'ALTERNATE_OBJECT_STORES_INCLUDED',
|
|
569
|
+
'Git may include objects and bytes from configured alternate object stores.'
|
|
570
|
+
),
|
|
571
|
+
limitation(
|
|
572
|
+
'PACK_OVERHEAD_EXCLUDED',
|
|
573
|
+
'Git object disk sizes exclude pack indexes, bitmaps, and other repository metadata.'
|
|
574
|
+
),
|
|
575
|
+
];
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function addTruncationLimitations(refs, limitations) {
|
|
579
|
+
for (const [kind, inventory] of [
|
|
580
|
+
['caches', refs.caches],
|
|
581
|
+
['rootSets', refs.rootSets],
|
|
582
|
+
['expiringSets', refs.expiringSets],
|
|
583
|
+
]) {
|
|
584
|
+
if (inventory.observed > inventory.entries.length) {
|
|
585
|
+
limitations.push({
|
|
586
|
+
...limitation(
|
|
587
|
+
'COLLECTION_DETAILS_TRUNCATED',
|
|
588
|
+
'Managed collection detail exceeded maxCollectionsPerKind.'
|
|
589
|
+
),
|
|
590
|
+
kind,
|
|
591
|
+
observed: inventory.observed,
|
|
592
|
+
inspected: inventory.inspected,
|
|
593
|
+
detailed: inventory.entries.length,
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function limitation(code, message) {
|
|
600
|
+
return { code, message };
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function assertSafeTotals(total) {
|
|
604
|
+
if (!Object.values(total).every(Number.isSafeInteger)) {
|
|
605
|
+
throw invalidOptions('Repository object totals exceed JavaScript safe integer bounds');
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function assertDependencies(value) {
|
|
610
|
+
const checks = [
|
|
611
|
+
[
|
|
612
|
+
value.repository,
|
|
613
|
+
[
|
|
614
|
+
'iterateObjects',
|
|
615
|
+
'iterateReachableObjectIds',
|
|
616
|
+
'iteratePrunableObjects',
|
|
617
|
+
'iterateRefs',
|
|
618
|
+
'reachablePhysicalBytes',
|
|
619
|
+
],
|
|
620
|
+
],
|
|
621
|
+
[value.rootSets, ['open']],
|
|
622
|
+
[value.caches, ['open']],
|
|
623
|
+
[value.expiringSets, ['open']],
|
|
624
|
+
[value.vault, ['getVaultMetadata', 'readState']],
|
|
625
|
+
];
|
|
626
|
+
if (
|
|
627
|
+
checks.some(([target, methods]) =>
|
|
628
|
+
methods.some((method) => typeof target?.[method] !== 'function')
|
|
629
|
+
)
|
|
630
|
+
) {
|
|
631
|
+
throw invalidOptions(
|
|
632
|
+
'RepositoryDoctor requires complete repository and collection dependencies'
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function invalidOptions(message) {
|
|
638
|
+
return createCasError(message, ErrorCodes.REPOSITORY_INSPECTION_INVALID);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
function deepFreeze(value) {
|
|
642
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value)) {
|
|
643
|
+
return value;
|
|
644
|
+
}
|
|
645
|
+
for (const child of Object.values(value)) {
|
|
646
|
+
deepFreeze(child);
|
|
647
|
+
}
|
|
648
|
+
return Object.freeze(value);
|
|
649
|
+
}
|