@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.
Files changed (69) hide show
  1. package/ARCHITECTURE.md +116 -3
  2. package/CHANGELOG.md +117 -2
  3. package/GUIDE.md +114 -13
  4. package/README.md +60 -4
  5. package/UPGRADING.md +75 -0
  6. package/build-info.json +2 -2
  7. package/docs/API.md +867 -16
  8. package/docs/releases/v6.2.0.md +131 -0
  9. package/docs/releases/v6.3.0.md +96 -0
  10. package/index.d.ts +972 -2
  11. package/index.js +326 -1
  12. package/package.json +4 -2
  13. package/src/domain/errors/Codes.js +39 -0
  14. package/src/domain/helpers/assertCanonicalTimestamp.js +15 -0
  15. package/src/domain/helpers/handleTarget.js +48 -0
  16. package/src/domain/helpers/isCanonicalCollectionKey.js +35 -0
  17. package/src/domain/services/AssetService.js +213 -0
  18. package/src/domain/services/BundleDescriptorCodec.js +286 -0
  19. package/src/domain/services/BundleFanoutBuilder.js +247 -0
  20. package/src/domain/services/BundleService.js +848 -0
  21. package/src/domain/services/CacheAcquisitionInventory.js +123 -0
  22. package/src/domain/services/CacheAcquisitionRegistry.js +188 -0
  23. package/src/domain/services/CacheCandidateHeap.js +71 -0
  24. package/src/domain/services/CacheIndex.js +362 -0
  25. package/src/domain/services/CacheMetadataCodec.js +164 -0
  26. package/src/domain/services/CachePolicyEnforcer.js +144 -0
  27. package/src/domain/services/CacheSet.js +754 -0
  28. package/src/domain/services/CacheSetRegistry.js +95 -0
  29. package/src/domain/services/CasService.d.ts +2 -1
  30. package/src/domain/services/ExpiringSet.js +496 -0
  31. package/src/domain/services/ExpiringSetIndex.js +254 -0
  32. package/src/domain/services/ExpiringSetMetadataCodec.js +160 -0
  33. package/src/domain/services/ExpiringSetRegistry.js +101 -0
  34. package/src/domain/services/PageService.js +175 -0
  35. package/src/domain/services/PublicationService.js +315 -0
  36. package/src/domain/services/RepositoryDoctor.js +676 -0
  37. package/src/domain/services/RetentionService.js +120 -0
  38. package/src/domain/services/RootSet.js +10 -6
  39. package/src/domain/services/RootSetMetadataCodec.js +8 -2
  40. package/src/domain/services/RootSetPersistence.js +3 -2
  41. package/src/domain/services/StagingEvidence.js +39 -0
  42. package/src/domain/value-objects/ApplicationHandle.js +40 -0
  43. package/src/domain/value-objects/AssetHandle.js +163 -0
  44. package/src/domain/value-objects/BundleHandle.js +32 -0
  45. package/src/domain/value-objects/BundleLimits.js +80 -0
  46. package/src/domain/value-objects/BundlePath.js +78 -0
  47. package/src/domain/value-objects/CacheAcquisition.js +88 -0
  48. package/src/domain/value-objects/CacheAcquisitionRef.js +129 -0
  49. package/src/domain/value-objects/CacheHit.js +93 -0
  50. package/src/domain/value-objects/CacheKey.js +32 -0
  51. package/src/domain/value-objects/CachePolicy.js +59 -0
  52. package/src/domain/value-objects/CacheSetRef.js +40 -0
  53. package/src/domain/value-objects/CollectionNamespace.js +54 -0
  54. package/src/domain/value-objects/ExpiringMarker.js +81 -0
  55. package/src/domain/value-objects/ExpiringSetKey.js +28 -0
  56. package/src/domain/value-objects/ExpiringSetRef.js +42 -0
  57. package/src/domain/value-objects/OpaqueHandle.js +171 -0
  58. package/src/domain/value-objects/PageHandle.js +34 -0
  59. package/src/domain/value-objects/RetentionWitness.js +106 -0
  60. package/src/domain/value-objects/StagedAsset.js +63 -0
  61. package/src/domain/value-objects/StagedBundle.js +66 -0
  62. package/src/domain/value-objects/StagedPage.js +47 -0
  63. package/src/infrastructure/adapters/GitPersistenceAdapter.js +48 -20
  64. package/src/infrastructure/adapters/GitRefAdapter.js +318 -7
  65. package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +182 -0
  66. package/src/package-version.js +1 -1
  67. package/src/ports/GitPersistencePort.js +11 -1
  68. package/src/ports/GitRefPort.js +46 -0
  69. package/src/ports/RepositoryInspectionPort.js +36 -0
@@ -0,0 +1,171 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import Oid from './Oid.js';
4
+
5
+ const TOKEN_PREFIX = 'git-cas';
6
+ const CODEC_PATTERN = /^[a-z0-9][a-z0-9.-]{0,31}$/u;
7
+
8
+ /**
9
+ * Shared immutable envelope for application-facing content handles.
10
+ */
11
+ export default class OpaqueHandle {
12
+ /**
13
+ * @param {object} value
14
+ * @param {{ version: number, kind: string, format: string, codec?: string, label: string }} spec
15
+ */
16
+ constructor(value, spec) {
17
+ assertObject(value, spec);
18
+ assertEnvelope(value, spec);
19
+
20
+ let oid;
21
+ try {
22
+ oid = Oid.from(value.oid).toString();
23
+ } catch (error) {
24
+ throw invalid(spec, `${spec.label} handle contains an invalid Git object identifier`, {
25
+ value,
26
+ originalError: error,
27
+ });
28
+ }
29
+ const hashAlgorithm = oid.length === 40 ? 'sha1' : 'sha256';
30
+ if (value.hashAlgorithm !== undefined && value.hashAlgorithm !== hashAlgorithm) {
31
+ throw invalid(spec, `${spec.label} handle hash algorithm does not match its object identifier`, {
32
+ hashAlgorithm: value.hashAlgorithm,
33
+ oid,
34
+ });
35
+ }
36
+
37
+ this.version = spec.version;
38
+ this.kind = spec.kind;
39
+ this.format = spec.format;
40
+ this.codec = spec.codec ?? value.codec;
41
+ this.hashAlgorithm = hashAlgorithm;
42
+ this.oid = oid;
43
+ Object.freeze(this);
44
+ }
45
+
46
+ /**
47
+ * @param {unknown} value
48
+ * @param {new (value: object) => OpaqueHandle} Type
49
+ * @param {object} spec
50
+ * @returns {OpaqueHandle}
51
+ */
52
+ static from(value, Type, spec) {
53
+ if (value instanceof Type) {
54
+ return value;
55
+ }
56
+ if (typeof value === 'string') {
57
+ return OpaqueHandle.parse(value, Type, spec);
58
+ }
59
+ return new Type(value);
60
+ }
61
+
62
+ /**
63
+ * @param {unknown} token
64
+ * @param {new (value: object) => OpaqueHandle} Type
65
+ * @param {object} spec
66
+ * @returns {OpaqueHandle}
67
+ */
68
+ static parse(token, Type, spec) {
69
+ if (typeof token !== 'string') {
70
+ throw invalid(spec, `${spec.label} handle token must be a string`, { token });
71
+ }
72
+ const fields = token.split(':');
73
+ if (fields.length !== 7 || fields[0] !== TOKEN_PREFIX) {
74
+ throw invalid(spec, `${spec.label} handle token has an invalid shape`, { token });
75
+ }
76
+ const [, version, kind, format, codec, hashAlgorithm, oid] = fields;
77
+ if (kind !== spec.kind) {
78
+ throw createCasError(`Handle kind is not ${spec.kind}`, ErrorCodes.HANDLE_KIND_MISMATCH, {
79
+ expectedKind: spec.kind,
80
+ actualKind: kind,
81
+ });
82
+ }
83
+ const handle = new Type({
84
+ version: Number(version),
85
+ kind,
86
+ format,
87
+ codec,
88
+ hashAlgorithm,
89
+ oid,
90
+ });
91
+ if (handle.toString() !== token) {
92
+ throw invalid(spec, `${spec.label} handle token is not canonical`, { token });
93
+ }
94
+ return handle;
95
+ }
96
+
97
+ /** @returns {string} */
98
+ toString() {
99
+ return [
100
+ TOKEN_PREFIX,
101
+ this.version,
102
+ this.kind,
103
+ this.format,
104
+ this.codec,
105
+ this.hashAlgorithm,
106
+ this.oid,
107
+ ].join(':');
108
+ }
109
+
110
+ /** @returns {object} */
111
+ toJSON() {
112
+ return {
113
+ version: this.version,
114
+ kind: this.kind,
115
+ format: this.format,
116
+ codec: this.codec,
117
+ hashAlgorithm: this.hashAlgorithm,
118
+ oid: this.oid,
119
+ };
120
+ }
121
+ }
122
+
123
+ function assertObject(value, spec) {
124
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
125
+ throw invalid(spec, `${spec.label} handle must be an object or canonical token`, { value });
126
+ }
127
+ }
128
+
129
+ function assertEnvelope(value, spec) {
130
+ assertVersion(value, spec);
131
+ assertKind(value, spec);
132
+ assertFormat(value, spec);
133
+ assertCodec(value, spec);
134
+ }
135
+
136
+ function assertVersion(value, spec) {
137
+ if (value.version !== undefined && value.version !== spec.version) {
138
+ throw invalid(spec, `Unsupported ${spec.kind} handle version: ${value.version}`, { value });
139
+ }
140
+ }
141
+
142
+ function assertKind(value, spec) {
143
+ if (value.kind !== undefined && value.kind !== spec.kind) {
144
+ throw createCasError(`Handle kind is not ${spec.kind}`, ErrorCodes.HANDLE_KIND_MISMATCH, {
145
+ expectedKind: spec.kind,
146
+ actualKind: value.kind,
147
+ });
148
+ }
149
+ }
150
+
151
+ function assertFormat(value, spec) {
152
+ if (value.format !== undefined && value.format !== spec.format) {
153
+ throw invalid(spec, `Unsupported ${spec.kind} handle format: ${value.format}`, { value });
154
+ }
155
+ }
156
+
157
+ function assertCodec(value, spec) {
158
+ const codec = spec.codec ?? value.codec;
159
+ if (typeof codec !== 'string' || !CODEC_PATTERN.test(codec) || value.codec === '') {
160
+ throw invalid(spec, `${spec.label} handle codec must be a canonical lowercase identifier`, {
161
+ codec: value.codec,
162
+ });
163
+ }
164
+ if (spec.codec !== undefined && value.codec !== undefined && value.codec !== spec.codec) {
165
+ throw invalid(spec, `${spec.label} handle codec must be ${spec.codec}`, { codec: value.codec });
166
+ }
167
+ }
168
+
169
+ function invalid(spec, message, meta) {
170
+ return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
171
+ }
@@ -0,0 +1,34 @@
1
+ import OpaqueHandle from './OpaqueHandle.js';
2
+
3
+ export const PAGE_HANDLE_VERSION = 1;
4
+ export const PAGE_HANDLE_KIND = 'page';
5
+ export const PAGE_HANDLE_FORMAT = 'blob';
6
+ export const PAGE_HANDLE_CODEC = 'raw';
7
+
8
+ const SPEC = Object.freeze({
9
+ version: PAGE_HANDLE_VERSION,
10
+ kind: PAGE_HANDLE_KIND,
11
+ format: PAGE_HANDLE_FORMAT,
12
+ codec: PAGE_HANDLE_CODEC,
13
+ label: 'Page',
14
+ });
15
+
16
+ /**
17
+ * Immutable, repository-independent locator for one raw page blob.
18
+ */
19
+ export default class PageHandle extends OpaqueHandle {
20
+ /** @param {object} value */
21
+ constructor(value) {
22
+ super(value, SPEC);
23
+ }
24
+
25
+ /** @param {PageHandle|string|object} value */
26
+ static from(value) {
27
+ return /** @type {PageHandle} */ (OpaqueHandle.from(value, PageHandle, SPEC));
28
+ }
29
+
30
+ /** @param {string} token */
31
+ static parse(token) {
32
+ return /** @type {PageHandle} */ (OpaqueHandle.parse(token, PageHandle, SPEC));
33
+ }
34
+ }
@@ -0,0 +1,106 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import parseApplicationHandle from './ApplicationHandle.js';
5
+ import Oid from './Oid.js';
6
+
7
+ const POLICIES = Object.freeze(['pinned', 'evictable']);
8
+ const REACHABILITY = Object.freeze(['anchored', 'orphaned', 'volatile']);
9
+ const ROOT_KINDS = Object.freeze([
10
+ 'root-set',
11
+ 'publication',
12
+ 'cache-set',
13
+ 'expiring-set',
14
+ ]);
15
+
16
+ /**
17
+ * Immutable evidence that one observed Git generation retained a handle.
18
+ */
19
+ export default class RetentionWitness {
20
+ /**
21
+ * @param {object} value
22
+ * @param {import('./AssetHandle.js').default|import('./BundleHandle.js').default|import('./PageHandle.js').default|string|object} value.handle
23
+ * @param {'pinned'|'evictable'} value.policy
24
+ * @param {'anchored'|'orphaned'|'volatile'} value.reachability
25
+ * @param {object} value.root
26
+ * @param {string} value.observedAt
27
+ */
28
+ constructor({ handle, policy, reachability, root, observedAt }) {
29
+ if (!POLICIES.includes(policy)) {
30
+ throw RetentionWitness.#invalid('Retention witness policy must be pinned or evictable', {
31
+ policy,
32
+ });
33
+ }
34
+ if (!REACHABILITY.includes(reachability)) {
35
+ throw RetentionWitness.#invalid('Retention witness has an invalid reachability state', {
36
+ reachability,
37
+ });
38
+ }
39
+ const normalizedRoot = RetentionWitness.#normalizeRoot(root);
40
+ assertCanonicalTimestamp(observedAt, {
41
+ invalid: RetentionWitness.#invalid,
42
+ message: 'Retention witness observation time must be a canonical UTC timestamp',
43
+ });
44
+
45
+ this.version = 1;
46
+ this.handle = parseApplicationHandle(handle);
47
+ this.policy = policy;
48
+ this.reachability = reachability;
49
+ this.root = Object.freeze(normalizedRoot);
50
+ this.observedAt = observedAt;
51
+ Object.freeze(this);
52
+ }
53
+
54
+ /**
55
+ * @returns {object}
56
+ */
57
+ toJSON() {
58
+ return {
59
+ version: this.version,
60
+ handle: this.handle.toString(),
61
+ policy: this.policy,
62
+ reachability: this.reachability,
63
+ root: { ...this.root },
64
+ observedAt: this.observedAt,
65
+ };
66
+ }
67
+
68
+ static #normalizeRoot(root) {
69
+ if (!root || typeof root !== 'object' || Array.isArray(root)) {
70
+ throw RetentionWitness.#invalid('Retention witness root must be an object', { root });
71
+ }
72
+ if (!ROOT_KINDS.includes(root.kind)) {
73
+ throw RetentionWitness.#invalid('Retention witness root kind is invalid', { root });
74
+ }
75
+ for (const field of ['namespace', 'ref', 'path']) {
76
+ if (typeof root[field] !== 'string' || root[field].length === 0) {
77
+ throw RetentionWitness.#invalid(`Retention witness root ${field} is required`, { root });
78
+ }
79
+ }
80
+ if (!root.ref.startsWith('refs/')) {
81
+ throw RetentionWitness.#invalid('Retention witness root ref must be fully qualified', {
82
+ root,
83
+ });
84
+ }
85
+ let generation;
86
+ try {
87
+ generation = Oid.from(root.generation).toString();
88
+ } catch (error) {
89
+ throw RetentionWitness.#invalid('Retention witness root generation is invalid', {
90
+ root,
91
+ originalError: error,
92
+ });
93
+ }
94
+ return {
95
+ kind: root.kind,
96
+ namespace: root.namespace,
97
+ ref: root.ref,
98
+ generation,
99
+ path: root.path,
100
+ };
101
+ }
102
+
103
+ static #invalid(message, meta) {
104
+ return createCasError(message, ErrorCodes.RETENTION_WITNESS_INVALID, meta);
105
+ }
106
+ }
@@ -0,0 +1,63 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import AssetHandle from './AssetHandle.js';
5
+
6
+ /**
7
+ * Immutable result for an asset graph written without a reachability root.
8
+ */
9
+ export default class StagedAsset {
10
+ /**
11
+ * @param {object} value
12
+ * @param {AssetHandle|string|object} value.handle
13
+ * @param {string} value.slug
14
+ * @param {string} value.filename
15
+ * @param {number} value.size
16
+ * @param {string} value.observedAt
17
+ */
18
+ constructor({ handle, slug, filename, size, observedAt }) {
19
+ if (typeof slug !== 'string' || slug.length === 0) {
20
+ throw StagedAsset.#invalid('Staged asset slug must be a non-empty string', { slug });
21
+ }
22
+ if (typeof filename !== 'string' || filename.length === 0) {
23
+ throw StagedAsset.#invalid('Staged asset filename must be a non-empty string', { filename });
24
+ }
25
+ if (!Number.isSafeInteger(size) || size < 0) {
26
+ throw StagedAsset.#invalid('Staged asset size must be a non-negative safe integer', { size });
27
+ }
28
+ assertCanonicalTimestamp(observedAt, {
29
+ invalid: StagedAsset.#invalid,
30
+ message: 'Staged asset observation time must be a canonical UTC timestamp',
31
+ });
32
+
33
+ this.version = 1;
34
+ this.state = 'staged';
35
+ this.handle = AssetHandle.from(handle);
36
+ this.asset = Object.freeze({ slug, filename, size });
37
+ this.retention = Object.freeze({
38
+ policy: null,
39
+ reachability: 'unanchored',
40
+ protection: 'not-established',
41
+ });
42
+ this.observedAt = observedAt;
43
+ Object.freeze(this);
44
+ }
45
+
46
+ /**
47
+ * @returns {object}
48
+ */
49
+ toJSON() {
50
+ return {
51
+ version: this.version,
52
+ state: this.state,
53
+ handle: this.handle.toString(),
54
+ asset: { ...this.asset },
55
+ retention: { ...this.retention },
56
+ observedAt: this.observedAt,
57
+ };
58
+ }
59
+
60
+ static #invalid(message, meta) {
61
+ return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
62
+ }
63
+ }
@@ -0,0 +1,66 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import BundleHandle from './BundleHandle.js';
5
+ import BundleLimits from './BundleLimits.js';
6
+
7
+ /**
8
+ * Immutable result for a bundle graph written without a reachability root.
9
+ */
10
+ export default class StagedBundle {
11
+ /**
12
+ * @param {object} value
13
+ * @param {BundleHandle|string|object} value.handle
14
+ * @param {number} value.memberCount
15
+ * @param {number} value.indexDepth
16
+ * @param {number} value.descriptorBytes
17
+ * @param {BundleLimits|object} value.limits
18
+ * @param {string} value.observedAt
19
+ */
20
+ constructor({ handle, memberCount, indexDepth, descriptorBytes, limits, observedAt }) {
21
+ for (const [field, number] of Object.entries({ memberCount, descriptorBytes })) {
22
+ if (!Number.isSafeInteger(number) || number < 0) {
23
+ throw StagedBundle.#invalid('Staged bundle metric is invalid', { field, value: number });
24
+ }
25
+ }
26
+ if (!Number.isSafeInteger(indexDepth) || indexDepth < 1) {
27
+ throw StagedBundle.#invalid('Staged bundle index depth is invalid', {
28
+ field: 'indexDepth',
29
+ value: indexDepth,
30
+ });
31
+ }
32
+ assertCanonicalTimestamp(observedAt, {
33
+ invalid: StagedBundle.#invalid,
34
+ message: 'Staged bundle observation time must be a canonical UTC timestamp',
35
+ });
36
+ this.version = 1;
37
+ this.state = 'staged';
38
+ this.handle = BundleHandle.from(handle);
39
+ this.bundle = Object.freeze({ memberCount, indexDepth, descriptorBytes });
40
+ this.limits = Object.freeze(new BundleLimits(limits).toJSON());
41
+ this.retention = Object.freeze({
42
+ policy: null,
43
+ reachability: 'unanchored',
44
+ protection: 'not-established',
45
+ });
46
+ this.observedAt = observedAt;
47
+ Object.freeze(this);
48
+ }
49
+
50
+ /** @returns {object} */
51
+ toJSON() {
52
+ return {
53
+ version: this.version,
54
+ state: this.state,
55
+ handle: this.handle.toString(),
56
+ bundle: { ...this.bundle },
57
+ limits: { ...this.limits },
58
+ retention: { ...this.retention },
59
+ observedAt: this.observedAt,
60
+ };
61
+ }
62
+
63
+ static #invalid(message, meta) {
64
+ return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
65
+ }
66
+ }
@@ -0,0 +1,47 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import PageHandle from './PageHandle.js';
5
+
6
+ /**
7
+ * Immutable result for a page blob written without a reachability root.
8
+ */
9
+ export default class StagedPage {
10
+ /** @param {{ handle: PageHandle|string|object, size: number, observedAt: string }} value */
11
+ constructor({ handle, size, observedAt }) {
12
+ if (!Number.isSafeInteger(size) || size < 0) {
13
+ throw StagedPage.#invalid('Staged page size must be a non-negative safe integer', { size });
14
+ }
15
+ assertCanonicalTimestamp(observedAt, {
16
+ invalid: StagedPage.#invalid,
17
+ message: 'Staged page observation time must be a canonical UTC timestamp',
18
+ });
19
+ this.version = 1;
20
+ this.state = 'staged';
21
+ this.handle = PageHandle.from(handle);
22
+ this.page = Object.freeze({ size });
23
+ this.retention = Object.freeze({
24
+ policy: null,
25
+ reachability: 'unanchored',
26
+ protection: 'not-established',
27
+ });
28
+ this.observedAt = observedAt;
29
+ Object.freeze(this);
30
+ }
31
+
32
+ /** @returns {object} */
33
+ toJSON() {
34
+ return {
35
+ version: this.version,
36
+ state: this.state,
37
+ handle: this.handle.toString(),
38
+ page: { ...this.page },
39
+ retention: { ...this.retention },
40
+ observedAt: this.observedAt,
41
+ };
42
+ }
43
+
44
+ static #invalid(message, meta) {
45
+ return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
46
+ }
47
+ }
@@ -4,7 +4,6 @@ import os from 'node:os';
4
4
  import path from 'node:path';
5
5
  import GitPersistencePort from '../../ports/GitPersistencePort.js';
6
6
  import { CasError, createCasError, ErrorCodes } from '../../domain/errors/index.js';
7
- import { errorDetailsText } from '../../domain/helpers/gitRefErrors.js';
8
7
 
9
8
  /**
10
9
  * Default resilience policy: 30 s timeout (no retry).
@@ -19,6 +18,8 @@ export const DEFAULT_MAX_BLOB_SIZE = 10 * 1024 * 1024;
19
18
  const MIN_READ_BLOB_LIMIT = 1;
20
19
  const MIN_MAX_BLOB_SIZE = 1024;
21
20
  const MAX_BLOB_SIZE_LIMIT = Number.MAX_SAFE_INTEGER;
21
+ const OBJECT_INFO_ARGUMENT = '--batch-check=%(objectname) %(objecttype) %(objectsize)';
22
+ const GIT_OBJECT_TYPES = new Set(['blob', 'tree', 'commit', 'tag']);
22
23
 
23
24
  /**
24
25
  * {@link GitPersistencePort} implementation backed by `@git-stunts/plumbing`.
@@ -187,19 +188,53 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
187
188
  * @returns {Promise<string>} Git object type.
188
189
  */
189
190
  async readObjectType(oid) {
190
- try {
191
- return await this.policy.execute(() =>
192
- this.plumbing.execute({ args: ['cat-file', '-t', oid] }),
193
- );
194
- } catch (err) {
195
- if (isMissingGitObjectError(err)) {
196
- throw new CasError(`Git object not found: ${oid}`, ErrorCodes.GIT_OBJECT_NOT_FOUND, {
197
- oid,
198
- originalError: err,
199
- });
200
- }
201
- throw err;
191
+ return (await this.#readObjectInfo(oid)).type;
192
+ }
193
+
194
+ /**
195
+ * @override
196
+ * @param {string} oid - Git object ID.
197
+ * @returns {Promise<number>} Git object size in bytes.
198
+ */
199
+ async readObjectSize(oid) {
200
+ return (await this.#readObjectInfo(oid)).size;
201
+ }
202
+
203
+ /**
204
+ * Reads object metadata through Git's structured batch protocol. Missing
205
+ * objects are reported on stdout with exit zero, avoiding runtime-specific
206
+ * stderr capture while preserving genuine command failures.
207
+ *
208
+ * @param {string} oid - Git object ID.
209
+ * @returns {Promise<{ oid: string, type: string, size: number }>} Object metadata.
210
+ */
211
+ async #readObjectInfo(oid) {
212
+ const rawOutput = await this.policy.execute(() => this.plumbing.execute({
213
+ args: ['cat-file', OBJECT_INFO_ARGUMENT],
214
+ input: `${oid}\n`,
215
+ }));
216
+ const output = typeof rawOutput === 'string' ? rawOutput.trim() : '';
217
+ if (output === `${oid} missing`) {
218
+ throw new CasError(`Git object not found: ${oid}`, ErrorCodes.GIT_OBJECT_NOT_FOUND, {
219
+ oid,
220
+ });
202
221
  }
222
+
223
+ const fields = output.split(' ');
224
+ const size = Number(fields[2]);
225
+ if (
226
+ fields.length !== 3 ||
227
+ fields[0] !== oid ||
228
+ !GIT_OBJECT_TYPES.has(fields[1]) ||
229
+ !Number.isSafeInteger(size) ||
230
+ size < 0
231
+ ) {
232
+ throw new CasError(`Git object has invalid metadata: ${oid}`, ErrorCodes.GIT_ERROR, {
233
+ oid,
234
+ output: rawOutput,
235
+ });
236
+ }
237
+ return Object.freeze({ oid: fields[0], type: fields[1], size });
203
238
  }
204
239
 
205
240
  /**
@@ -332,10 +367,3 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
332
367
  };
333
368
  }
334
369
  }
335
-
336
- function isMissingGitObjectError(err) {
337
- const message = errorDetailsText(err).toLowerCase();
338
- return message.includes('could not get object info') ||
339
- message.includes('not a valid object name') ||
340
- message.includes('bad object');
341
- }