@git-stunts/git-cas 6.2.0 → 6.3.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 +16 -8
- package/CHANGELOG.md +37 -0
- package/README.md +13 -4
- package/UPGRADING.md +32 -0
- package/build-info.json +2 -2
- package/docs/API.md +124 -8
- package/docs/releases/v6.3.0.md +96 -0
- package/index.d.ts +95 -2
- package/package.json +2 -1
- package/src/domain/errors/Codes.js +4 -0
- package/src/domain/services/CacheAcquisitionInventory.js +123 -0
- package/src/domain/services/CacheAcquisitionRegistry.js +188 -0
- package/src/domain/services/CacheSet.js +56 -5
- package/src/domain/services/CacheSetRegistry.js +7 -0
- package/src/domain/services/RepositoryDoctor.js +30 -3
- package/src/domain/value-objects/CacheAcquisition.js +88 -0
- package/src/domain/value-objects/CacheAcquisitionRef.js +129 -0
- package/src/domain/value-objects/RetentionWitness.js +6 -1
- package/src/infrastructure/adapters/GitRefAdapter.js +312 -2
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +28 -10
- package/src/package-version.js +1 -1
- package/src/ports/GitRefPort.js +45 -0
- package/src/ports/RepositoryInspectionPort.js +5 -2
|
@@ -11,6 +11,9 @@ const ErrorCodes = Object.freeze({
|
|
|
11
11
|
BUNDLE_MEMBER_ORDER: 'BUNDLE_MEMBER_ORDER',
|
|
12
12
|
BUNDLE_PATH_INVALID: 'BUNDLE_PATH_INVALID',
|
|
13
13
|
BUNDLE_PATH_LIMIT: 'BUNDLE_PATH_LIMIT',
|
|
14
|
+
CACHE_ACQUISITION_CONFLICT: 'CACHE_ACQUISITION_CONFLICT',
|
|
15
|
+
CACHE_ACQUISITION_INVALID: 'CACHE_ACQUISITION_INVALID',
|
|
16
|
+
CACHE_ACQUISITION_RELEASE_CONFLICT: 'CACHE_ACQUISITION_RELEASE_CONFLICT',
|
|
14
17
|
CACHE_CONFLICT: 'CACHE_CONFLICT',
|
|
15
18
|
CACHE_ENTRY_INVALID: 'CACHE_ENTRY_INVALID',
|
|
16
19
|
CACHE_KEY_INVALID: 'CACHE_KEY_INVALID',
|
|
@@ -29,6 +32,7 @@ const ErrorCodes = Object.freeze({
|
|
|
29
32
|
GIT_ERROR: 'GIT_ERROR',
|
|
30
33
|
GIT_OBJECT_NOT_FOUND: 'GIT_OBJECT_NOT_FOUND',
|
|
31
34
|
GIT_PLUMBING_INITIALIZATION_FAILED: 'GIT_PLUMBING_INITIALIZATION_FAILED',
|
|
35
|
+
GIT_REF_CONFLICT: 'GIT_REF_CONFLICT',
|
|
32
36
|
GIT_REF_NOT_FOUND: 'GIT_REF_NOT_FOUND',
|
|
33
37
|
HANDLE_CODEC_MISMATCH: 'HANDLE_CODEC_MISMATCH',
|
|
34
38
|
HANDLE_INVALID: 'HANDLE_INVALID',
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import CacheAcquisitionRef from '../value-objects/CacheAcquisitionRef.js';
|
|
4
|
+
import Oid from '../value-objects/Oid.js';
|
|
5
|
+
|
|
6
|
+
export function createCacheAcquisitionInventory(detailLimit) {
|
|
7
|
+
return {
|
|
8
|
+
observed: 0,
|
|
9
|
+
inspected: 0,
|
|
10
|
+
healthy: true,
|
|
11
|
+
detailLimit,
|
|
12
|
+
entries: [],
|
|
13
|
+
hasUnknownAge: false,
|
|
14
|
+
totals: {
|
|
15
|
+
activeCount: 0,
|
|
16
|
+
oldestAcquiredAt: null,
|
|
17
|
+
newestAcquiredAt: null,
|
|
18
|
+
maxAgeMs: null,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function recordCacheAcquisition(inventory, record, observedAt) {
|
|
24
|
+
inventory.observed += 1;
|
|
25
|
+
inventory.inspected += 1;
|
|
26
|
+
inventory.totals.activeCount += 1;
|
|
27
|
+
let entry;
|
|
28
|
+
try {
|
|
29
|
+
assertDirectRefEvidence(record);
|
|
30
|
+
const acquisitionRef = CacheAcquisitionRef.from(record.ref);
|
|
31
|
+
const ageMs = Date.parse(observedAt) - Date.parse(acquisitionRef.acquiredAt);
|
|
32
|
+
if (!Number.isSafeInteger(ageMs)) {
|
|
33
|
+
throw createCasError(
|
|
34
|
+
'Cache acquisition age cannot be represented safely',
|
|
35
|
+
ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
36
|
+
{ ref: record.ref, acquiredAt: acquisitionRef.acquiredAt, observedAt },
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const clockSkewed = ageMs < 0;
|
|
40
|
+
entry = {
|
|
41
|
+
id: acquisitionRef.id,
|
|
42
|
+
namespace: acquisitionRef.namespace,
|
|
43
|
+
generation: Oid.from(record.oid).toString(),
|
|
44
|
+
acquiredAt: acquisitionRef.acquiredAt,
|
|
45
|
+
ageMs: clockSkewed ? null : ageMs,
|
|
46
|
+
healthy: true,
|
|
47
|
+
issues: clockSkewed ? [{
|
|
48
|
+
code: 'CACHE_ACQUISITION_CLOCK_SKEW',
|
|
49
|
+
message: 'Cache acquisition time is later than repository inspection',
|
|
50
|
+
acquiredAt: acquisitionRef.acquiredAt,
|
|
51
|
+
observedAt,
|
|
52
|
+
}] : [],
|
|
53
|
+
};
|
|
54
|
+
updateTotals(inventory, entry);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
inventory.healthy = false;
|
|
57
|
+
entry = {
|
|
58
|
+
ref: record.ref,
|
|
59
|
+
generation: record.oid,
|
|
60
|
+
healthy: false,
|
|
61
|
+
issues: [{
|
|
62
|
+
code: error?.code ?? ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
63
|
+
message: error instanceof Error ? error.message : String(error),
|
|
64
|
+
}],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (inventory.entries.length < inventory.detailLimit) {
|
|
68
|
+
inventory.entries.push(entry);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function assertDirectRefEvidence(record) {
|
|
73
|
+
if (record.symref === undefined) {
|
|
74
|
+
throw createCasError(
|
|
75
|
+
'Cache acquisition direct-ref evidence is unavailable',
|
|
76
|
+
ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
77
|
+
{ ref: record.ref },
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
if (record.symref !== null) {
|
|
81
|
+
throw createCasError(
|
|
82
|
+
'Symbolic cache acquisition refs are unsafe',
|
|
83
|
+
ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
84
|
+
{ ref: record.ref, symref: record.symref },
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function cacheAcquisitionGroup(inventory) {
|
|
90
|
+
return {
|
|
91
|
+
healthy: inventory.healthy,
|
|
92
|
+
coverage: {
|
|
93
|
+
observed: inventory.observed,
|
|
94
|
+
inspected: inventory.inspected,
|
|
95
|
+
detailed: inventory.entries.length,
|
|
96
|
+
complete: inventory.observed === inventory.inspected,
|
|
97
|
+
},
|
|
98
|
+
totals: inventory.totals,
|
|
99
|
+
entries: inventory.entries,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function updateTotals(inventory, entry) {
|
|
104
|
+
const { totals } = inventory;
|
|
105
|
+
totals.oldestAcquiredAt = earlier(totals.oldestAcquiredAt, entry.acquiredAt);
|
|
106
|
+
totals.newestAcquiredAt = later(totals.newestAcquiredAt, entry.acquiredAt);
|
|
107
|
+
if (entry.ageMs === null) {
|
|
108
|
+
inventory.hasUnknownAge = true;
|
|
109
|
+
totals.maxAgeMs = null;
|
|
110
|
+
} else if (!inventory.hasUnknownAge) {
|
|
111
|
+
totals.maxAgeMs = totals.maxAgeMs === null
|
|
112
|
+
? entry.ageMs
|
|
113
|
+
: Math.max(totals.maxAgeMs, entry.ageMs);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function earlier(current, candidate) {
|
|
118
|
+
return current === null || candidate < current ? candidate : current;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function later(current, candidate) {
|
|
122
|
+
return current === null || candidate > current ? candidate : current;
|
|
123
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import CacheAcquisition from '../value-objects/CacheAcquisition.js';
|
|
4
|
+
import CacheAcquisitionRef from '../value-objects/CacheAcquisitionRef.js';
|
|
5
|
+
import CollectionNamespace from '../value-objects/CollectionNamespace.js';
|
|
6
|
+
import Oid from '../value-objects/Oid.js';
|
|
7
|
+
import RetentionWitness from '../value-objects/RetentionWitness.js';
|
|
8
|
+
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
9
|
+
|
|
10
|
+
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
11
|
+
|
|
12
|
+
/** Creates and releases git-cas-managed cache-generation anchors. */
|
|
13
|
+
export default class CacheAcquisitionRegistry {
|
|
14
|
+
constructor({ ref, crypto, clock = DEFAULT_CLOCK }) {
|
|
15
|
+
this.ref = ref;
|
|
16
|
+
this.crypto = crypto;
|
|
17
|
+
this.clock = clock;
|
|
18
|
+
Object.freeze(this);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async acquire({ namespace, cacheRef, keyDigest, generation, hit }) {
|
|
22
|
+
this.#assertAcquireCapabilities();
|
|
23
|
+
const acquiredAt = this.#now();
|
|
24
|
+
const acquisitionRef = CacheAcquisitionRef.create({
|
|
25
|
+
namespace,
|
|
26
|
+
keyDigest,
|
|
27
|
+
acquiredAt,
|
|
28
|
+
nonce: bytesToHex(await this.crypto.randomBytes(16)),
|
|
29
|
+
});
|
|
30
|
+
const anchored = await this.ref.anchorRef({
|
|
31
|
+
sourceRef: cacheRef,
|
|
32
|
+
expectedSourceOid: generation,
|
|
33
|
+
targetRef: acquisitionRef.toString(),
|
|
34
|
+
});
|
|
35
|
+
if (!anchored) {
|
|
36
|
+
throw createCasError(
|
|
37
|
+
'Cache generation changed before it could be acquired',
|
|
38
|
+
ErrorCodes.CACHE_ACQUISITION_CONFLICT,
|
|
39
|
+
{ cacheRef, generation },
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
const evidence = new RetentionWitness({
|
|
43
|
+
handle: hit.handle,
|
|
44
|
+
policy: 'pinned',
|
|
45
|
+
reachability: 'anchored',
|
|
46
|
+
root: {
|
|
47
|
+
kind: 'cache-set',
|
|
48
|
+
namespace,
|
|
49
|
+
ref: acquisitionRef.toString(),
|
|
50
|
+
generation,
|
|
51
|
+
path: RootSetMetadataCodec.slotFor(0),
|
|
52
|
+
},
|
|
53
|
+
observedAt: acquiredAt,
|
|
54
|
+
});
|
|
55
|
+
return new CacheAcquisition({
|
|
56
|
+
id: acquisitionRef.id,
|
|
57
|
+
hit,
|
|
58
|
+
evidence,
|
|
59
|
+
acquiredAt,
|
|
60
|
+
release: () => this.release({
|
|
61
|
+
namespace,
|
|
62
|
+
id: acquisitionRef.id,
|
|
63
|
+
expectedGeneration: generation,
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async inspect({ namespace: value, limit = 100 }) {
|
|
69
|
+
assertMethod(
|
|
70
|
+
this.ref,
|
|
71
|
+
'iterateRefs',
|
|
72
|
+
'Cache acquisition inspection requires ref iteration',
|
|
73
|
+
);
|
|
74
|
+
const namespace = CollectionNamespace.from(value).toString();
|
|
75
|
+
assertInspectionLimit(limit);
|
|
76
|
+
const prefix = CacheAcquisitionRef.prefixForNamespace(namespace);
|
|
77
|
+
const entries = [];
|
|
78
|
+
for await (const record of this.ref.iterateRefs({ prefix, limit: limit + 1 })) {
|
|
79
|
+
if (record.symref !== null) {
|
|
80
|
+
throw invalid('Symbolic cache acquisition refs are unsafe', {
|
|
81
|
+
ref: record.ref,
|
|
82
|
+
symref: record.symref,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const acquisitionRef = CacheAcquisitionRef.from(record.ref);
|
|
86
|
+
if (acquisitionRef.namespace !== namespace) {
|
|
87
|
+
throw invalid('Cache acquisition iterator escaped its requested namespace', {
|
|
88
|
+
namespace,
|
|
89
|
+
ref: record.ref,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
entries.push(Object.freeze({
|
|
93
|
+
id: acquisitionRef.id,
|
|
94
|
+
generation: Oid.from(record.oid).toString(),
|
|
95
|
+
acquiredAt: acquisitionRef.acquiredAt,
|
|
96
|
+
keyDigest: acquisitionRef.keyDigest,
|
|
97
|
+
}));
|
|
98
|
+
if (entries.length > limit) {
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const truncated = entries.length > limit;
|
|
103
|
+
if (truncated) {
|
|
104
|
+
entries.pop();
|
|
105
|
+
}
|
|
106
|
+
return Object.freeze({
|
|
107
|
+
namespace,
|
|
108
|
+
entries: Object.freeze(entries),
|
|
109
|
+
truncated,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async release({ namespace, id, expectedGeneration }) {
|
|
114
|
+
assertMethod(
|
|
115
|
+
this.ref,
|
|
116
|
+
'deleteRef',
|
|
117
|
+
'Cache acquisition release requires checked ref deletion',
|
|
118
|
+
);
|
|
119
|
+
const acquisitionRef = CacheAcquisitionRef.forId({ namespace, id });
|
|
120
|
+
const generation = Oid.from(expectedGeneration).toString();
|
|
121
|
+
let changed;
|
|
122
|
+
try {
|
|
123
|
+
changed = await this.ref.deleteRef({
|
|
124
|
+
ref: acquisitionRef.toString(),
|
|
125
|
+
expectedOldOid: generation,
|
|
126
|
+
});
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error?.code !== ErrorCodes.GIT_REF_CONFLICT) {
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
throw createCasError(
|
|
132
|
+
'Cache acquisition release could not prove the expected direct generation',
|
|
133
|
+
ErrorCodes.CACHE_ACQUISITION_RELEASE_CONFLICT,
|
|
134
|
+
{ id: acquisitionRef.id, generation, originalError: error },
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
return Object.freeze({
|
|
138
|
+
id: acquisitionRef.id,
|
|
139
|
+
generation,
|
|
140
|
+
changed,
|
|
141
|
+
releasedAt: this.#now(),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#now() {
|
|
146
|
+
const value = this.clock.now();
|
|
147
|
+
if (!(value instanceof Date) || Number.isNaN(value.getTime())) {
|
|
148
|
+
throw invalid('Cache acquisition clock returned an invalid Date');
|
|
149
|
+
}
|
|
150
|
+
return value.toISOString();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#assertAcquireCapabilities() {
|
|
154
|
+
assertMethod(
|
|
155
|
+
this.ref,
|
|
156
|
+
'anchorRef',
|
|
157
|
+
'Cache acquisition requires atomic ref anchoring',
|
|
158
|
+
);
|
|
159
|
+
assertMethod(
|
|
160
|
+
this.crypto,
|
|
161
|
+
'randomBytes',
|
|
162
|
+
'Cache acquisition requires random bytes',
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function assertInspectionLimit(limit) {
|
|
168
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1000) {
|
|
169
|
+
throw invalid('Cache acquisition inspection limit must be between 1 and 1000', { limit });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function bytesToHex(bytes) {
|
|
174
|
+
if (!(bytes instanceof Uint8Array) || bytes.length !== 16) {
|
|
175
|
+
throw invalid('Cache acquisition random source returned invalid bytes');
|
|
176
|
+
}
|
|
177
|
+
return [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function invalid(message, meta) {
|
|
181
|
+
return createCasError(message, ErrorCodes.CACHE_ACQUISITION_INVALID, meta);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function assertMethod(target, name, message) {
|
|
185
|
+
if (typeof target?.[name] !== 'function') {
|
|
186
|
+
throw invalid(message, { capability: name });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -14,9 +14,11 @@ import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
|
14
14
|
|
|
15
15
|
const RETENTION = Object.freeze(['pinned', 'evictable']);
|
|
16
16
|
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
17
|
+
const MAX_ACQUISITION_ATTEMPTS = 5;
|
|
17
18
|
|
|
18
19
|
/** RootSet-backed lifecycle manager for application cache handles. */
|
|
19
20
|
export default class CacheSet {
|
|
21
|
+
#acquisitions;
|
|
20
22
|
#clock;
|
|
21
23
|
#configuredPolicy;
|
|
22
24
|
#crypto;
|
|
@@ -29,6 +31,7 @@ export default class CacheSet {
|
|
|
29
31
|
constructor(options) {
|
|
30
32
|
CacheSet.#assertDependencies(options);
|
|
31
33
|
this.#namespace = options.namespace;
|
|
34
|
+
this.#acquisitions = options.acquisitions;
|
|
32
35
|
this.#rootSet = options.rootSet;
|
|
33
36
|
this.#index = options.index;
|
|
34
37
|
this.#resolveHandle = options.resolveHandle;
|
|
@@ -40,6 +43,48 @@ export default class CacheSet {
|
|
|
40
43
|
Object.freeze(this);
|
|
41
44
|
}
|
|
42
45
|
|
|
46
|
+
async acquire(keyValue) {
|
|
47
|
+
const key = CacheKey.from(keyValue).toString();
|
|
48
|
+
const digest = await this.#digest(key);
|
|
49
|
+
for (let attempt = 1; attempt <= MAX_ACQUISITION_ATTEMPTS; attempt += 1) {
|
|
50
|
+
const state = await this.#rootSet.read();
|
|
51
|
+
const handle = this.#index.fromRootEntries(state.entries);
|
|
52
|
+
const entry = await this.#index.getEntry(handle, digest);
|
|
53
|
+
const now = this.#now();
|
|
54
|
+
assertNoKeyCollision(entry, key);
|
|
55
|
+
if (!entry || isExpired(entry.metadata, now)) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const hit = this.#hit(entry, state.headOid, now);
|
|
59
|
+
try {
|
|
60
|
+
return await this.#acquisitions.acquire({
|
|
61
|
+
namespace: this.#namespace,
|
|
62
|
+
cacheRef: this.ref,
|
|
63
|
+
keyDigest: digest,
|
|
64
|
+
generation: state.headOid,
|
|
65
|
+
hit,
|
|
66
|
+
});
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (error?.code !== ErrorCodes.CACHE_ACQUISITION_CONFLICT) {
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
throw createCasError(
|
|
74
|
+
'Cache generation changed throughout every acquisition attempt',
|
|
75
|
+
ErrorCodes.CACHE_ACQUISITION_CONFLICT,
|
|
76
|
+
{ ref: this.ref, key, attempts: MAX_ACQUISITION_ATTEMPTS },
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async inspectAcquisitions(options = {}) {
|
|
81
|
+
return await this.#acquisitions.inspect({ ...options, namespace: this.#namespace });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async releaseAcquisition(options) {
|
|
85
|
+
return await this.#acquisitions.release({ ...options, namespace: this.#namespace });
|
|
86
|
+
}
|
|
87
|
+
|
|
43
88
|
async get(keyValue) {
|
|
44
89
|
const key = CacheKey.from(keyValue).toString();
|
|
45
90
|
const digest = await this.#digest(key);
|
|
@@ -533,14 +578,16 @@ export default class CacheSet {
|
|
|
533
578
|
}
|
|
534
579
|
|
|
535
580
|
static #assertDependencies(options) {
|
|
581
|
+
const value = options ?? {};
|
|
536
582
|
const dependencies = [
|
|
537
|
-
['rootSet',
|
|
538
|
-
['
|
|
539
|
-
['
|
|
540
|
-
['
|
|
583
|
+
['rootSet', hasMethod(value.rootSet, 'mutate')],
|
|
584
|
+
['acquisitions', hasMethod(value.acquisitions, 'acquire')],
|
|
585
|
+
['index', hasMethod(value.index, 'scan')],
|
|
586
|
+
['resolveHandle', typeof value.resolveHandle === 'function'],
|
|
587
|
+
['crypto', hasMethod(value.crypto, 'sha256')],
|
|
541
588
|
];
|
|
542
589
|
const missing = dependencies
|
|
543
|
-
.filter(([,
|
|
590
|
+
.filter(([, available]) => !available)
|
|
544
591
|
.map(([name]) => name);
|
|
545
592
|
if (missing.length > 0) {
|
|
546
593
|
throw createCasError('CacheSet requires complete lifecycle dependencies', ErrorCodes.INVALID_OPTIONS, {
|
|
@@ -550,6 +597,10 @@ export default class CacheSet {
|
|
|
550
597
|
}
|
|
551
598
|
}
|
|
552
599
|
|
|
600
|
+
function hasMethod(target, name) {
|
|
601
|
+
return typeof target?.[name] === 'function';
|
|
602
|
+
}
|
|
603
|
+
|
|
553
604
|
function canReplace({ previous, key, expectedHandle, requireExisting }) {
|
|
554
605
|
if (!previous || previous.metadata.key !== key) {
|
|
555
606
|
return !requireExisting && expectedHandle === null;
|
|
@@ -4,6 +4,7 @@ import CollectionNamespace from '../value-objects/CollectionNamespace.js';
|
|
|
4
4
|
import createCasError from '../errors/createCasError.js';
|
|
5
5
|
import { ErrorCodes } from '../errors/index.js';
|
|
6
6
|
import CacheIndex from './CacheIndex.js';
|
|
7
|
+
import CacheAcquisitionRegistry from './CacheAcquisitionRegistry.js';
|
|
7
8
|
import CacheSet from './CacheSet.js';
|
|
8
9
|
import RootSet from './RootSet.js';
|
|
9
10
|
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
@@ -20,6 +21,11 @@ export default class CacheSetRegistry {
|
|
|
20
21
|
this.resolveHandle = options.resolveHandle;
|
|
21
22
|
this.crypto = options.crypto;
|
|
22
23
|
this.clock = options.clock;
|
|
24
|
+
this.acquisitions = new CacheAcquisitionRegistry({
|
|
25
|
+
ref: this.ref,
|
|
26
|
+
crypto: this.crypto,
|
|
27
|
+
clock: this.clock,
|
|
28
|
+
});
|
|
23
29
|
Object.freeze(this);
|
|
24
30
|
}
|
|
25
31
|
|
|
@@ -51,6 +57,7 @@ export default class CacheSetRegistry {
|
|
|
51
57
|
crypto: this.crypto,
|
|
52
58
|
}),
|
|
53
59
|
resolveHandle: this.resolveHandle,
|
|
60
|
+
acquisitions: this.acquisitions,
|
|
54
61
|
crypto: this.crypto,
|
|
55
62
|
clock: this.clock,
|
|
56
63
|
});
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { ErrorCodes } from '../errors/index.js';
|
|
2
2
|
import createCasError from '../errors/createCasError.js';
|
|
3
|
+
import { CACHE_ACQUISITION_REF_PREFIX } from '../value-objects/CacheAcquisitionRef.js';
|
|
3
4
|
import { CACHE_SET_REF_PREFIX } from '../value-objects/CacheSetRef.js';
|
|
4
5
|
import { EXPIRING_SET_REF_PREFIX } from '../value-objects/ExpiringSetRef.js';
|
|
5
6
|
import { ROOT_SET_REF_PREFIX } from '../value-objects/RootSetRef.js';
|
|
6
7
|
import { VAULT_REF } from './VaultPersistence.js';
|
|
8
|
+
import {
|
|
9
|
+
cacheAcquisitionGroup,
|
|
10
|
+
createCacheAcquisitionInventory,
|
|
11
|
+
recordCacheAcquisition,
|
|
12
|
+
} from './CacheAcquisitionInventory.js';
|
|
7
13
|
|
|
8
14
|
export const DEFAULT_REPOSITORY_GRACE_PERIOD_MS = 14 * 24 * 60 * 60 * 1_000;
|
|
9
15
|
const DEFAULT_MAX_COLLECTIONS_PER_KIND = 100;
|
|
@@ -26,7 +32,11 @@ export default class RepositoryDoctor {
|
|
|
26
32
|
const observedAt = this.#now();
|
|
27
33
|
const policy = inspectionPolicy(options, observedAt);
|
|
28
34
|
const limitations = baseLimitations();
|
|
29
|
-
const { refs, usage } = await this.#inventoryUsage(
|
|
35
|
+
const { refs, usage } = await this.#inventoryUsage(
|
|
36
|
+
policy.maxCollectionsPerKind,
|
|
37
|
+
limitations,
|
|
38
|
+
observedAt,
|
|
39
|
+
);
|
|
30
40
|
const objects = await this.#inventoryObjects(policy.expiresBefore);
|
|
31
41
|
|
|
32
42
|
if (!objects.consistent) {
|
|
@@ -105,11 +115,13 @@ export default class RepositoryDoctor {
|
|
|
105
115
|
};
|
|
106
116
|
}
|
|
107
117
|
|
|
108
|
-
async #inventoryUsage(limit, limitations) {
|
|
118
|
+
async #inventoryUsage(limit, limitations, observedAt) {
|
|
109
119
|
const result = usageInventory(limit);
|
|
110
120
|
for await (const record of this.repository.iterateRefs()) {
|
|
111
121
|
result.refCount += 1;
|
|
112
|
-
if (record.ref.startsWith(
|
|
122
|
+
if (record.ref.startsWith(CACHE_ACQUISITION_REF_PREFIX)) {
|
|
123
|
+
recordCacheAcquisition(result.acquisitions, record, observedAt);
|
|
124
|
+
} else if (record.ref.startsWith(CACHE_SET_REF_PREFIX)) {
|
|
113
125
|
result.caches.observed += 1;
|
|
114
126
|
recordCache(result.caches, await this.#inspectCache(record));
|
|
115
127
|
} else if (record.ref.startsWith(ROOT_SET_REF_PREFIX)) {
|
|
@@ -125,6 +137,7 @@ export default class RepositoryDoctor {
|
|
|
125
137
|
return {
|
|
126
138
|
refs: result,
|
|
127
139
|
usage: {
|
|
140
|
+
acquisitions: cacheAcquisitionGroup(result.acquisitions),
|
|
128
141
|
caches: collectionGroup(result.caches),
|
|
129
142
|
rootSets: collectionGroup(result.rootSets),
|
|
130
143
|
expiringSets: collectionGroup(result.expiringSets),
|
|
@@ -403,6 +416,7 @@ function collectionInventory(totals, detailLimit) {
|
|
|
403
416
|
function usageInventory(detailLimit) {
|
|
404
417
|
return {
|
|
405
418
|
refCount: 0,
|
|
419
|
+
acquisitions: createCacheAcquisitionInventory(detailLimit),
|
|
406
420
|
caches: collectionInventory(
|
|
407
421
|
{
|
|
408
422
|
entryCount: 0,
|
|
@@ -505,6 +519,7 @@ function healthyVault(record, { entryCount, privacy }) {
|
|
|
505
519
|
function usageHealthy(usage) {
|
|
506
520
|
return (
|
|
507
521
|
usage.caches.healthy &&
|
|
522
|
+
usage.acquisitions.healthy &&
|
|
508
523
|
usage.rootSets.healthy &&
|
|
509
524
|
usage.expiringSets.healthy &&
|
|
510
525
|
usage.vault.healthy
|
|
@@ -576,6 +591,18 @@ function baseLimitations() {
|
|
|
576
591
|
}
|
|
577
592
|
|
|
578
593
|
function addTruncationLimitations(refs, limitations) {
|
|
594
|
+
const acquisitions = refs.acquisitions;
|
|
595
|
+
if (acquisitions.observed > acquisitions.entries.length) {
|
|
596
|
+
limitations.push({
|
|
597
|
+
...limitation(
|
|
598
|
+
'CACHE_ACQUISITION_DETAILS_TRUNCATED',
|
|
599
|
+
'Cache acquisition detail exceeded maxCollectionsPerKind.'
|
|
600
|
+
),
|
|
601
|
+
observed: acquisitions.observed,
|
|
602
|
+
inspected: acquisitions.inspected,
|
|
603
|
+
detailed: acquisitions.entries.length,
|
|
604
|
+
});
|
|
605
|
+
}
|
|
579
606
|
for (const [kind, inventory] of [
|
|
580
607
|
['caches', refs.caches],
|
|
581
608
|
['rootSets', refs.rootSets],
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
|
|
4
|
+
import CacheAcquisitionRef from './CacheAcquisitionRef.js';
|
|
5
|
+
import CacheHit from './CacheHit.js';
|
|
6
|
+
import RetentionWitness from './RetentionWitness.js';
|
|
7
|
+
|
|
8
|
+
/** One active, explicitly releasable cache-generation retention scope. */
|
|
9
|
+
export default class CacheAcquisition {
|
|
10
|
+
#release;
|
|
11
|
+
#releasePromise = null;
|
|
12
|
+
#releaseResult = null;
|
|
13
|
+
|
|
14
|
+
constructor({ id, hit, evidence, acquiredAt, release }) {
|
|
15
|
+
if (typeof id !== 'string' || id.length === 0) {
|
|
16
|
+
throw invalid('Cache acquisition ID is required', { id });
|
|
17
|
+
}
|
|
18
|
+
if (!(hit instanceof CacheHit)) {
|
|
19
|
+
throw invalid('Cache acquisition requires a CacheHit', { hit });
|
|
20
|
+
}
|
|
21
|
+
if (!(evidence instanceof RetentionWitness)) {
|
|
22
|
+
throw invalid('Cache acquisition requires retention evidence', { evidence });
|
|
23
|
+
}
|
|
24
|
+
assertCanonicalTimestamp(acquiredAt, {
|
|
25
|
+
invalid,
|
|
26
|
+
message: 'Cache acquisition time must be a canonical UTC timestamp',
|
|
27
|
+
});
|
|
28
|
+
assertEvidence({ id, hit, evidence, acquiredAt });
|
|
29
|
+
if (typeof release !== 'function') {
|
|
30
|
+
throw invalid('Cache acquisition requires a release function', { release });
|
|
31
|
+
}
|
|
32
|
+
this.id = id;
|
|
33
|
+
this.hit = hit;
|
|
34
|
+
this.evidence = evidence;
|
|
35
|
+
this.acquiredAt = acquiredAt;
|
|
36
|
+
this.#release = release;
|
|
37
|
+
Object.freeze(this);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async release() {
|
|
41
|
+
if (this.#releaseResult !== null) {
|
|
42
|
+
return Object.freeze({
|
|
43
|
+
...this.#releaseResult,
|
|
44
|
+
changed: false,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
if (this.#releasePromise !== null) {
|
|
48
|
+
return await this.#releasePromise;
|
|
49
|
+
}
|
|
50
|
+
this.#releasePromise = Promise.resolve(this.#release())
|
|
51
|
+
.then((result) => {
|
|
52
|
+
this.#releaseResult = result;
|
|
53
|
+
return result;
|
|
54
|
+
})
|
|
55
|
+
.finally(() => {
|
|
56
|
+
this.#releasePromise = null;
|
|
57
|
+
});
|
|
58
|
+
return await this.#releasePromise;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function invalid(message, meta) {
|
|
63
|
+
return createCasError(message, ErrorCodes.CACHE_ACQUISITION_INVALID, meta);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function assertEvidence({ id, hit, evidence, acquiredAt }) {
|
|
67
|
+
const acquisitionRef = CacheAcquisitionRef.forId({
|
|
68
|
+
namespace: evidence.root.namespace,
|
|
69
|
+
id,
|
|
70
|
+
});
|
|
71
|
+
const valid = evidence.policy === 'pinned'
|
|
72
|
+
&& evidence.reachability === 'anchored'
|
|
73
|
+
&& evidence.root.kind === 'cache-set'
|
|
74
|
+
&& evidence.root.ref === acquisitionRef.toString()
|
|
75
|
+
&& evidence.root.namespace === hit.evidence.root.namespace
|
|
76
|
+
&& evidence.root.generation === hit.generation
|
|
77
|
+
&& evidence.handle.toString() === hit.handle.toString()
|
|
78
|
+
&& evidence.observedAt === acquiredAt
|
|
79
|
+
&& acquisitionRef.acquiredAt === acquiredAt;
|
|
80
|
+
if (!valid) {
|
|
81
|
+
throw invalid('Cache acquisition retention evidence does not match its acquired hit', {
|
|
82
|
+
id,
|
|
83
|
+
acquiredAt,
|
|
84
|
+
generation: hit.generation,
|
|
85
|
+
evidence: evidence.toJSON(),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|