@git-stunts/git-cas 6.5.7 → 6.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/ARCHITECTURE.md +9 -0
  2. package/CHANGELOG.md +39 -0
  3. package/GUIDE.md +54 -0
  4. package/README.md +17 -9
  5. package/UPGRADING.md +68 -0
  6. package/build-info.json +2 -2
  7. package/docs/API.md +115 -12
  8. package/docs/releases/v6.5.8.md +89 -0
  9. package/docs/releases/v6.5.9.md +119 -0
  10. package/index.d.ts +75 -0
  11. package/index.js +19 -1
  12. package/package.json +4 -2
  13. package/src/domain/services/AssetService.js +201 -10
  14. package/src/domain/services/BoundedWriteWavePersistence.js +235 -0
  15. package/src/domain/services/BundleBatchPlanner.js +349 -0
  16. package/src/domain/services/BundleService.js +225 -2
  17. package/src/domain/services/CasService.d.ts +16 -0
  18. package/src/domain/services/CasService.js +9 -18
  19. package/src/domain/services/ManifestRepository.js +129 -50
  20. package/src/domain/services/ManifestTreeService.js +47 -0
  21. package/src/domain/services/PageService.js +36 -10
  22. package/src/domain/services/RootSet.js +32 -0
  23. package/src/domain/services/RootSetPersistence.js +58 -1
  24. package/src/domain/services/StagedTarget.js +19 -0
  25. package/src/domain/services/StagingWorkspace.js +95 -20
  26. package/src/domain/services/StagingWorkspaceRegistry.js +9 -4
  27. package/src/domain/services/WorkspaceCompoundAdmission.js +61 -0
  28. package/src/domain/services/WorkspaceCompoundScope.js +193 -0
  29. package/src/domain/services/forkCasPersistence.js +20 -0
  30. package/src/infrastructure/adapters/GitObjectSessionPool.js +47 -5
  31. package/src/infrastructure/adapters/GitPersistenceAdapter.js +169 -0
  32. package/src/infrastructure/adapters/GitPersistenceWriteScope.js +154 -0
  33. package/src/infrastructure/adapters/GitRefAdapter.js +38 -3
  34. package/src/infrastructure/adapters/GitUpdateRefSessionPool.js +115 -0
  35. package/src/package-version.js +1 -1
  36. package/src/ports/GitPersistencePort.js +43 -0
  37. package/src/ports/GitRefPort.js +8 -0
@@ -3,6 +3,7 @@ import GitRefPort from '../../ports/GitRefPort.js';
3
3
  import { CasError, ErrorCodes } from '../../domain/errors/index.js';
4
4
  import { isGitMissingRefError } from '../../domain/helpers/gitRefErrors.js';
5
5
  import Oid from '../../domain/value-objects/Oid.js';
6
+ import GitUpdateRefSessionPool from './GitUpdateRefSessionPool.js';
6
7
 
7
8
  /**
8
9
  * Default resilience policy: 30 s timeout (no retry).
@@ -30,6 +31,9 @@ const REF_NAME_ENCODER = new globalThis.TextEncoder();
30
31
  * (30 s timeout by default).
31
32
  */
32
33
  export default class GitRefAdapter extends GitRefPort {
34
+ #closed = false;
35
+ #updateRefs;
36
+
33
37
  /**
34
38
  * @param {Object} options
35
39
  * @param {import('@git-stunts/plumbing').default} options.plumbing - GitPlumbing instance.
@@ -39,6 +43,7 @@ export default class GitRefAdapter extends GitRefPort {
39
43
  super();
40
44
  this.plumbing = plumbing;
41
45
  this.policy = policy ?? DEFAULT_POLICY;
46
+ this.#updateRefs = new GitUpdateRefSessionPool({ plumbing });
42
47
  }
43
48
 
44
49
  /**
@@ -47,6 +52,7 @@ export default class GitRefAdapter extends GitRefPort {
47
52
  * @returns {Promise<string>} The commit OID.
48
53
  */
49
54
  async resolveRef(ref) {
55
+ this.#assertOpen();
50
56
  try {
51
57
  return await this.policy.execute(() =>
52
58
  this.plumbing.execute({ args: ['rev-parse', ref] }),
@@ -68,6 +74,7 @@ export default class GitRefAdapter extends GitRefPort {
68
74
  * @returns {Promise<string>} The tree OID.
69
75
  */
70
76
  async resolveTree(commitOid) {
77
+ this.#assertOpen();
71
78
  return this.policy.execute(() =>
72
79
  this.plumbing.execute({ args: ['rev-parse', `${commitOid}^{tree}`] }),
73
80
  );
@@ -79,6 +86,7 @@ export default class GitRefAdapter extends GitRefPort {
79
86
  * @returns {Promise<string[]>} Direct parent OIDs.
80
87
  */
81
88
  async resolveParents(commitOid) {
89
+ this.#assertOpen();
82
90
  const line = await this.policy.execute(() =>
83
91
  this.plumbing.execute({ args: ['rev-list', '--parents', '-n', '1', commitOid] }),
84
92
  );
@@ -103,6 +111,7 @@ export default class GitRefAdapter extends GitRefPort {
103
111
  * @returns {Promise<string>} The new commit OID.
104
112
  */
105
113
  async createCommit({ treeOid, parentOid, parentOids, message }) {
114
+ this.#assertOpen();
106
115
  const args = ['commit-tree', treeOid, '-m', message];
107
116
  const parents = parentOids ?? (parentOid ? [parentOid] : []);
108
117
  for (const parent of parents) {
@@ -122,6 +131,7 @@ export default class GitRefAdapter extends GitRefPort {
122
131
  * @returns {Promise<void>}
123
132
  */
124
133
  async updateRef({ ref, newOid, expectedOldOid }) {
134
+ this.#assertOpen();
125
135
  assertRefName(ref);
126
136
  const symbolicTarget = await this.#resolveSymbolicRef(ref);
127
137
  if (symbolicTarget !== null) {
@@ -137,9 +147,14 @@ export default class GitRefAdapter extends GitRefPort {
137
147
  args.push(expectedOldOid ?? '0'.repeat(newOid.length));
138
148
  }
139
149
  try {
140
- await this.policy.execute(() =>
141
- this.plumbing.execute({ args }),
142
- );
150
+ if (this.#updateRefs.supports()) {
151
+ await this.#updateRefs.update(
152
+ { ref, newOid, expectedOldOid, noDeref: true },
153
+ (operation) => this.policy.execute(operation),
154
+ );
155
+ } else {
156
+ await this.policy.execute(() => this.plumbing.execute({ args }));
157
+ }
143
158
  } catch (error) {
144
159
  const actual = await this.#inspectRefPosture(ref);
145
160
  if (postureDisprovesExpectedOid(actual, expectedOldOid)) {
@@ -156,6 +171,7 @@ export default class GitRefAdapter extends GitRefPort {
156
171
 
157
172
  /** @override */
158
173
  async anchorRef({ sourceRef, expectedSourceOid, targetRef }) {
174
+ this.#assertOpen();
159
175
  assertRefName(sourceRef);
160
176
  assertRefName(targetRef);
161
177
  const generation = Oid.from(expectedSourceOid).toString();
@@ -195,6 +211,7 @@ export default class GitRefAdapter extends GitRefPort {
195
211
 
196
212
  /** @override */
197
213
  async deleteRef({ ref, expectedOldOid }) {
214
+ this.#assertOpen();
198
215
  assertRefName(ref);
199
216
  const expectedGeneration = Oid.from(expectedOldOid).toString();
200
217
  // A post-probe type race may delete this managed symref name, never its
@@ -231,6 +248,7 @@ export default class GitRefAdapter extends GitRefPort {
231
248
 
232
249
  /** @override */
233
250
  async *iterateRefs({ prefix = 'refs/', after = null, limit } = {}) {
251
+ this.#assertOpen();
234
252
  assertRefPrefix(prefix);
235
253
  assertRefCursor(prefix, after);
236
254
  assertRefIterationLimit(limit);
@@ -246,6 +264,23 @@ export default class GitRefAdapter extends GitRefPort {
246
264
  yield* paginatedRefRecords({ stream, prefix, after, limit });
247
265
  }
248
266
 
267
+ /** @override */
268
+ async close() {
269
+ this.#closed = true;
270
+ await this.#updateRefs.close();
271
+ }
272
+
273
+ /** @override */
274
+ async [Symbol.asyncDispose]() {
275
+ await this.close();
276
+ }
277
+
278
+ #assertOpen() {
279
+ if (this.#closed) {
280
+ throw new CasError('Git ref adapter is closed', ErrorCodes.RESOURCE_CLOSED);
281
+ }
282
+ }
283
+
249
284
  async #inspectDirectRef(ref) {
250
285
  const output = await this.policy.execute(() =>
251
286
  this.plumbing.execute({
@@ -0,0 +1,115 @@
1
+ import createCasError from '../../domain/errors/createCasError.js';
2
+ import { ErrorCodes } from '../../domain/errors/index.js';
3
+
4
+ const EXECUTE_DIRECTLY = (operation) => operation();
5
+
6
+ /**
7
+ * Lazily owns one reusable typed update-ref process.
8
+ *
9
+ * A failed ref transaction is never replayed. Plumbing's typed session poisons
10
+ * and terminates itself on protocol or Git failure; this owner only discards
11
+ * the failed instance so a later, separately authorized mutation can open a
12
+ * fresh process.
13
+ */
14
+ export default class GitUpdateRefSessionPool {
15
+ #active = new Set();
16
+ #closed = false;
17
+ #closePromise = null;
18
+ #opening = null;
19
+ #plumbing;
20
+
21
+ constructor({ plumbing }) {
22
+ this.#plumbing = plumbing;
23
+ }
24
+
25
+ supports() {
26
+ return typeof this.#plumbing?.openUpdateRefSession === 'function';
27
+ }
28
+
29
+ async update(options, execute = EXECUTE_DIRECTLY) {
30
+ this.#assertOpen();
31
+ const operation = (async () => {
32
+ let session;
33
+ try {
34
+ session = await this.#session();
35
+ return await execute(() => session.update(options));
36
+ } catch (error) {
37
+ if (session !== undefined) {
38
+ await this.#discard(session);
39
+ }
40
+ throw error;
41
+ }
42
+ })();
43
+ this.#active.add(operation);
44
+ try {
45
+ return await operation;
46
+ } finally {
47
+ this.#active.delete(operation);
48
+ }
49
+ }
50
+
51
+ async close() {
52
+ if (this.#closePromise !== null) {
53
+ return await this.#closePromise;
54
+ }
55
+ this.#closed = true;
56
+ this.#closePromise = (async () => {
57
+ await Promise.allSettled([...this.#active]);
58
+ const opening = this.#opening;
59
+ this.#opening = null;
60
+ if (opening !== null) {
61
+ const session = await opening.catch(() => null);
62
+ if (session !== null) {
63
+ await session.close();
64
+ }
65
+ }
66
+ })();
67
+ return await this.#closePromise;
68
+ }
69
+
70
+ async #discard(expectedSession) {
71
+ const opening = this.#opening;
72
+ if (opening === null) {
73
+ return;
74
+ }
75
+ const session = await opening.catch(() => undefined);
76
+ if (session === expectedSession && this.#opening === opening) {
77
+ this.#opening = null;
78
+ await releaseDiscardedSession(session);
79
+ }
80
+ }
81
+
82
+ async #session() {
83
+ this.#assertOpen();
84
+ if (this.#opening === null) {
85
+ const opening = Promise.resolve().then(() => this.#plumbing.openUpdateRefSession());
86
+ this.#opening = opening;
87
+ opening.catch(() => {
88
+ if (this.#opening === opening) {
89
+ this.#opening = null;
90
+ }
91
+ });
92
+ }
93
+ return await this.#opening;
94
+ }
95
+
96
+ #assertOpen() {
97
+ if (this.#closed) {
98
+ throw createCasError('Git ref session pool is closed', ErrorCodes.RESOURCE_CLOSED);
99
+ }
100
+ }
101
+ }
102
+
103
+ async function releaseDiscardedSession(session) {
104
+ try {
105
+ if (typeof session.terminate === 'function') {
106
+ await session.terminate();
107
+ } else if (typeof session.abort === 'function') {
108
+ await session.abort();
109
+ } else if (typeof session.close === 'function') {
110
+ await session.close();
111
+ }
112
+ } catch {
113
+ // Preserve the mutation failure that caused this best-effort teardown.
114
+ }
115
+ }
@@ -1 +1 @@
1
- export const PACKAGE_VERSION = '6.5.7';
1
+ export const PACKAGE_VERSION = '6.5.9';
@@ -35,6 +35,20 @@ export default class GitPersistencePort {
35
35
  throw new Error('Not implemented');
36
36
  }
37
37
 
38
+ /**
39
+ * Creates a bounded group of independent Git tree objects in input order.
40
+ * Adapters may override this to pipeline one typed protocol request.
41
+ * @param {Iterable<string[]>} trees
42
+ * @returns {Promise<string[]>} Git OIDs in input order.
43
+ */
44
+ async writeTrees(trees) {
45
+ const oids = [];
46
+ for (const entries of trees) {
47
+ oids.push(await this.writeTree(entries));
48
+ }
49
+ return oids;
50
+ }
51
+
38
52
  /**
39
53
  * Reads a Git blob by its OID.
40
54
  * @param {string} _oid - Git object ID.
@@ -102,6 +116,35 @@ export default class GitPersistencePort {
102
116
  throw new Error('Not implemented');
103
117
  }
104
118
 
119
+ /**
120
+ * Reads metadata for a bounded group of Git objects in input order.
121
+ * @param {Iterable<string>} oids - Git object IDs.
122
+ * @returns {Promise<Array<{oid: string, type: string, size: number}>>}
123
+ */
124
+ async readObjectInfos(oids) {
125
+ const infos = [];
126
+ for (const oid of oids) {
127
+ infos.push({
128
+ oid,
129
+ type: await this.readObjectType(oid),
130
+ size: await this.readObjectSize(oid),
131
+ });
132
+ }
133
+ return infos;
134
+ }
135
+
136
+ /**
137
+ * Runs one bounded operation against an operation-owned write scope.
138
+ * Adapters may override this to retain protocol processes until the callback
139
+ * settles. The default preserves compatibility for custom persistence ports.
140
+ * @template T
141
+ * @param {(persistence: GitPersistencePort) => Promise<T>} operation
142
+ * @returns {Promise<T>}
143
+ */
144
+ async withWriteScope(operation) {
145
+ return await operation(this);
146
+ }
147
+
105
148
  /**
106
149
  * Releases adapter-owned local resources. The default implementation is a
107
150
  * no-op so persistence adapters without long-lived resources remain valid.
@@ -99,6 +99,14 @@ export default class GitRefPort {
99
99
  iterateRefs(_options) {
100
100
  throw unsupportedAcquisitionCapability('iterateRefs');
101
101
  }
102
+
103
+ /** Releases adapter-owned local resources. */
104
+ async close() {}
105
+
106
+ /** @returns {Promise<void>} */
107
+ async [Symbol.asyncDispose]() {
108
+ await this.close();
109
+ }
102
110
  }
103
111
 
104
112
  function unsupportedAcquisitionCapability(capability) {