@git-stunts/git-cas 6.1.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 +116 -3
- package/CHANGELOG.md +117 -2
- package/GUIDE.md +114 -13
- package/README.md +60 -4
- package/UPGRADING.md +75 -0
- package/build-info.json +2 -2
- package/docs/API.md +867 -16
- package/docs/releases/v6.2.0.md +131 -0
- package/docs/releases/v6.3.0.md +96 -0
- package/index.d.ts +972 -2
- package/index.js +326 -1
- package/package.json +4 -2
- package/src/domain/errors/Codes.js +39 -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/CacheAcquisitionInventory.js +123 -0
- package/src/domain/services/CacheAcquisitionRegistry.js +188 -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 +754 -0
- package/src/domain/services/CacheSetRegistry.js +95 -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 +676 -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/CacheAcquisition.js +88 -0
- package/src/domain/value-objects/CacheAcquisitionRef.js +129 -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 +106 -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 +318 -7
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +182 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +11 -1
- package/src/ports/GitRefPort.js +46 -0
- package/src/ports/RepositoryInspectionPort.js +36 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Fixed-size max heap that retains the oldest cache candidates. */
|
|
2
|
+
export default class CacheCandidateHeap {
|
|
3
|
+
#items = [];
|
|
4
|
+
#limit;
|
|
5
|
+
|
|
6
|
+
constructor(limit) {
|
|
7
|
+
this.#limit = limit;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
add(value) {
|
|
11
|
+
if (this.#items.length < this.#limit) {
|
|
12
|
+
this.#items.push(value);
|
|
13
|
+
this.#up(this.#items.length - 1);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (compare(value, this.#items[0]) < 0) {
|
|
17
|
+
this.#items[0] = value;
|
|
18
|
+
this.#down(0);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
sorted() {
|
|
23
|
+
return [...this.#items].sort(compare);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#up(start) {
|
|
27
|
+
let index = start;
|
|
28
|
+
while (index > 0) {
|
|
29
|
+
const parent = Math.floor((index - 1) / 2);
|
|
30
|
+
if (compare(this.#items[parent], this.#items[index]) >= 0) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
this.#swap(parent, index);
|
|
34
|
+
index = parent;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
#down(start) {
|
|
39
|
+
let index = start;
|
|
40
|
+
while (true) {
|
|
41
|
+
const child = this.#largerChild(index);
|
|
42
|
+
if (child === null || compare(this.#items[index], this.#items[child]) >= 0) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
this.#swap(index, child);
|
|
46
|
+
index = child;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
#largerChild(index) {
|
|
51
|
+
const left = index * 2 + 1;
|
|
52
|
+
if (left >= this.#items.length) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const right = left + 1;
|
|
56
|
+
return right < this.#items.length && compare(this.#items[right], this.#items[left]) > 0
|
|
57
|
+
? right
|
|
58
|
+
: left;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#swap(left, right) {
|
|
62
|
+
[this.#items[left], this.#items[right]] = [this.#items[right], this.#items[left]];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function compare(left, right) {
|
|
67
|
+
if (left.sortKey !== right.sortKey) {
|
|
68
|
+
return left.sortKey < right.sortKey ? -1 : 1;
|
|
69
|
+
}
|
|
70
|
+
return left.digest < right.digest ? -1 : left.digest === right.digest ? 0 : 1;
|
|
71
|
+
}
|