@git-stunts/git-cas 6.5.12 → 7.1.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 (57) hide show
  1. package/ARCHITECTURE.md +15 -0
  2. package/CHANGELOG.md +74 -0
  3. package/README.md +14 -4
  4. package/UPGRADING.md +90 -27
  5. package/build-info.json +2 -2
  6. package/docs/API.md +111 -0
  7. package/docs/releases/v7.0.0.md +27 -0
  8. package/docs/releases/v7.1.0.md +27 -0
  9. package/index.d.ts +15 -0
  10. package/index.js +30 -0
  11. package/package.json +7 -5
  12. package/src/domain/errors/Codes.js +1 -0
  13. package/src/domain/errors/ReservationStorageError.js +9 -0
  14. package/src/domain/schemas/ManifestSchema.d.ts +4 -4
  15. package/src/domain/services/ReservationCustodyObjects.js +171 -0
  16. package/src/domain/services/ReservationCustodyStore.js +149 -0
  17. package/src/domain/services/ReservationIndex.js +169 -0
  18. package/src/domain/services/ReservationIndexNode.js +75 -0
  19. package/src/domain/services/ReservationLedger.js +326 -0
  20. package/src/domain/services/ReservationLedgerStore.js +141 -0
  21. package/src/domain/services/ReservationListing.js +40 -0
  22. package/src/domain/services/ReservationNamespace.d.ts +135 -0
  23. package/src/domain/services/ReservationNamespace.js +51 -0
  24. package/src/domain/services/ReservationNamespaceRegistry.js +89 -0
  25. package/src/domain/services/ReservationRecord.js +166 -0
  26. package/src/domain/services/ReservationScan.js +49 -0
  27. package/src/domain/value-objects/ReservationCursor.d.ts +11 -0
  28. package/src/domain/value-objects/ReservationCursor.js +31 -0
  29. package/src/domain/value-objects/ReservationCustodyDescriptor.js +47 -0
  30. package/src/domain/value-objects/ReservationCustodySnapshot.js +16 -0
  31. package/src/domain/value-objects/ReservationEvidence.js +26 -0
  32. package/src/domain/value-objects/ReservationEvidenceRead.js +22 -0
  33. package/src/domain/value-objects/ReservationEvidenceRoots.js +72 -0
  34. package/src/domain/value-objects/ReservationIndeterminate.js +17 -0
  35. package/src/domain/value-objects/ReservationInspection.js +21 -0
  36. package/src/domain/value-objects/ReservationKey.js +26 -0
  37. package/src/domain/value-objects/ReservationLedgerHeader.js +15 -0
  38. package/src/domain/value-objects/ReservationLedgerRefusal.js +21 -0
  39. package/src/domain/value-objects/ReservationLedgerSnapshot.js +27 -0
  40. package/src/domain/value-objects/ReservationListRequest.js +35 -0
  41. package/src/domain/value-objects/ReservationListUnavailable.js +17 -0
  42. package/src/domain/value-objects/ReservationOpenIndeterminate.js +15 -0
  43. package/src/domain/value-objects/ReservationOpenRefusal.js +14 -0
  44. package/src/domain/value-objects/ReservationPage.js +27 -0
  45. package/src/domain/value-objects/ReservationPolicy.js +24 -0
  46. package/src/domain/value-objects/ReservationRefusal.js +23 -0
  47. package/src/domain/value-objects/ReservationResult.js +27 -0
  48. package/src/domain/value-objects/ReservationSupport.js +22 -0
  49. package/src/infrastructure/adapters/GitRefAdapter.js +7 -0
  50. package/src/infrastructure/codecs/ReservationCustodyCodec.js +54 -0
  51. package/src/infrastructure/codecs/ReservationLedgerCodec.js +48 -0
  52. package/src/infrastructure/codecs/ReservationRecordCodec.js +54 -0
  53. package/src/package-version.js +1 -1
  54. package/src/ports/GitRefPort.js +11 -0
  55. package/src/ports/ReservationCustodyCodecPort.js +14 -0
  56. package/src/ports/ReservationLedgerCodecPort.js +14 -0
  57. package/src/ports/ReservationRecordCodecPort.js +14 -0
@@ -0,0 +1,72 @@
1
+ import Oid from './Oid.js';
2
+ import ReservationSupport from './ReservationSupport.js';
3
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
4
+ import ReservationStorageError from '../errors/ReservationStorageError.js';
5
+
6
+ export const RESERVATION_MAX_SUPPORTS = 64;
7
+ export const RESERVATION_MAX_PARENTS = 63;
8
+
9
+ /** Canonical bounded support edges; the original preparation gets parent 64. */
10
+ export default class ReservationEvidenceRoots {
11
+ constructor({ supports = [], parents = [] } = {}) {
12
+ if (!Array.isArray(supports) || supports.length > RESERVATION_MAX_SUPPORTS ||
13
+ !Array.isArray(parents) || parents.length > RESERVATION_MAX_PARENTS) {
14
+ throw new InvalidOptionsError('Reservation evidence allows at most 64 supports and 63 explicit parents');
15
+ }
16
+ this.supports = Object.freeze(supports.map((item) => new ReservationSupport(item)).sort(compareSupport));
17
+ this.parents = Object.freeze(parents.map(canonicalParent).sort());
18
+ if (new Set(this.supports.map((item) => `${item.type}:${item.oid}`)).size !== this.supports.length ||
19
+ new Set(this.parents).size !== this.parents.length) {
20
+ throw new InvalidOptionsError('Reservation evidence roots must be unique');
21
+ }
22
+ Object.freeze(this);
23
+ }
24
+
25
+ assertFormat(oid) {
26
+ canonicalParent(oid);
27
+ if (this.parents.some((parent) => parent.length !== oid.length) ||
28
+ this.supports.some((item) => item.oid.length !== oid.length)) {
29
+ throw new ReservationStorageError('Reservation evidence roots use different Git object formats');
30
+ }
31
+ }
32
+
33
+ equals(other) {
34
+ return other instanceof ReservationEvidenceRoots && this.parents.length === other.parents.length &&
35
+ this.supports.length === other.supports.length && this.parents.every((oid, i) => oid === other.parents[i]) &&
36
+ this.supports.every((item, i) => item.equals(other.supports[i]));
37
+ }
38
+
39
+ toTreeLines() {
40
+ return this.supports.map((item, i) => `${item.mode} ${item.type} ${item.oid}\t${slot(i)}`);
41
+ }
42
+
43
+ assertTree(entries) {
44
+ if (!Array.isArray(entries) || entries.length !== this.supports.length) {
45
+ throw new ReservationStorageError('Reservation support tree count disagrees with its descriptor');
46
+ }
47
+ for (let i = 0; i < entries.length; i++) {
48
+ const actual = entries[i];
49
+ const expected = this.supports[i];
50
+ if (actual.name !== slot(i) || actual.mode !== expected.mode || actual.type !== expected.type || actual.oid !== expected.oid) {
51
+ throw new ReservationStorageError('Reservation support tree disagrees with its descriptor');
52
+ }
53
+ }
54
+ }
55
+ }
56
+
57
+ function canonicalParent(oid) {
58
+ if (!Oid.isValid(oid) || oid !== oid.toLowerCase()) {
59
+ throw new InvalidOptionsError('Reservation evidence requires canonical Git OIDs');
60
+ }
61
+ return oid;
62
+ }
63
+
64
+ function compareSupport(left, right) {
65
+ const a = `${left.type}:${left.oid}`;
66
+ const b = `${right.type}:${right.oid}`;
67
+ return a < b ? -1 : a > b ? 1 : 0;
68
+ }
69
+
70
+ function slot(index) {
71
+ return index.toString(16).padStart(2, '0');
72
+ }
@@ -0,0 +1,17 @@
1
+ import ReservationRecord from '../services/ReservationRecord.js';
2
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
3
+ import { CasError, ErrorCodes } from '../errors/index.js';
4
+
5
+ /** Failed observation retains its cause; absence after failure is not a refund. */
6
+ export default class ReservationIndeterminate {
7
+ constructor(record, cause) {
8
+ if (!(record instanceof ReservationRecord)) {
9
+ throw new InvalidOptionsError('Indeterminate reservation requires its original attempt');
10
+ }
11
+ this.status = 'indeterminate';
12
+ this.key = record.key;
13
+ this.binding = record.binding;
14
+ this.cause = cause instanceof Error ? cause : new CasError('Reservation operation failed', ErrorCodes.GIT_ERROR, { originalError: cause });
15
+ Object.freeze(this);
16
+ }
17
+ }
@@ -0,0 +1,21 @@
1
+ import ReservationRecord from '../services/ReservationRecord.js';
2
+ import ReservationLedgerSnapshot from './ReservationLedgerSnapshot.js';
3
+ import ReservationCustodySnapshot from './ReservationCustodySnapshot.js';
4
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
5
+
6
+ /** Ledger observation followed by pin observation, never a cross-ref snapshot. */
7
+ export default class ReservationInspection {
8
+ constructor({ snapshot, record, custody }) {
9
+ if (!(snapshot instanceof ReservationLedgerSnapshot) || !(record instanceof ReservationRecord) ||
10
+ (custody !== null && !(custody instanceof ReservationCustodySnapshot))) {
11
+ throw new InvalidOptionsError('Reservation inspection requires validated ledger and custody observations');
12
+ }
13
+ this.status = 'inspected';
14
+ this.record = record;
15
+ this.revision = snapshot.revision;
16
+ this.policy = snapshot.header.policy;
17
+ this.charged = snapshot.header.charged;
18
+ this.custody = custody;
19
+ Object.freeze(this);
20
+ }
21
+ }
@@ -0,0 +1,26 @@
1
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
2
+ import { utf8Encode } from '../encoding/utf8.js';
3
+
4
+ const KEY_PATTERN = /^[!-~]{1,128}$/u;
5
+ const ROUTE_PATTERN = /^[0-9a-f]{64}$/u;
6
+ const ROUTE_DOMAIN = 'git-cas/reservation-key/v1\0';
7
+ export const RESERVATION_ROUTE_DIGITS = 64;
8
+
9
+ /** Bounded caller identity, hashed with a versioned storage-route domain. */
10
+ export default class ReservationKey {
11
+ constructor(value) {
12
+ if (typeof value !== 'string' || !KEY_PATTERN.test(value)) {
13
+ throw new InvalidOptionsError('Reservation key must be 1 to 128 printable ASCII bytes without spaces');
14
+ }
15
+ this.value = value;
16
+ Object.freeze(this);
17
+ }
18
+
19
+ async route(crypto) {
20
+ const digest = await crypto.sha256(utf8Encode(ROUTE_DOMAIN + this.value));
21
+ if (typeof digest !== 'string' || !ROUTE_PATTERN.test(digest)) {
22
+ throw new InvalidOptionsError('Reservation routing requires a canonical SHA-256 digest');
23
+ }
24
+ return digest;
25
+ }
26
+ }
@@ -0,0 +1,15 @@
1
+ import ReservationPolicy from './ReservationPolicy.js';
2
+ import ReservationStorageError from '../errors/ReservationStorageError.js';
3
+
4
+ /** Policy and aggregate charge published atomically with the attempt index. */
5
+ export default class ReservationLedgerHeader {
6
+ constructor({ policy, charged }) {
7
+ if (!(policy instanceof ReservationPolicy) || !Number.isSafeInteger(charged) ||
8
+ charged < 0 || charged > policy.limit) {
9
+ throw new ReservationStorageError('Reservation ledger charge must fit its validated policy');
10
+ }
11
+ this.policy = policy;
12
+ this.charged = charged;
13
+ Object.freeze(this);
14
+ }
15
+ }
@@ -0,0 +1,21 @@
1
+ import ReservationRecord from '../services/ReservationRecord.js';
2
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
3
+
4
+ const REASONS = new Set([
5
+ 'capacity-full', 'binding-conflict', 'cancelled', 'policy-mismatch', 'contention', 'not-reserved',
6
+ 'not-prepared', 'not-settled', 'already-armed', 'already-settled', 'irreversible', 'preparation-mismatch', 'settlement-mismatch',
7
+ ]);
8
+
9
+ /** An expected admission refusal naming the exact attempted identity. */
10
+ export default class ReservationLedgerRefusal {
11
+ constructor(record, reason) {
12
+ if (!(record instanceof ReservationRecord) || !REASONS.has(reason)) {
13
+ throw new InvalidOptionsError('Reservation ledger refusal requires an attempt and known reason');
14
+ }
15
+ this.status = 'refused';
16
+ this.key = record.key;
17
+ this.binding = record.binding;
18
+ this.reason = reason;
19
+ Object.freeze(this);
20
+ }
21
+ }
@@ -0,0 +1,27 @@
1
+ import ReservationLedgerHeader from './ReservationLedgerHeader.js';
2
+ import ReservationStorageError from '../errors/ReservationStorageError.js';
3
+ import Oid from './Oid.js';
4
+
5
+ /** One validated immutable ledger generation; no claim about later heads. */
6
+ export default class ReservationLedgerSnapshot {
7
+ constructor({ header, indexRoot, revision }) {
8
+ if (!(header instanceof ReservationLedgerHeader)) {
9
+ throw new ReservationStorageError('Reservation snapshot requires a ledger header');
10
+ }
11
+ ReservationLedgerSnapshot.assertOid(indexRoot);
12
+ ReservationLedgerSnapshot.assertOid(revision);
13
+ if (indexRoot.length !== revision.length) {
14
+ throw new ReservationStorageError('Reservation snapshot changed Git object format');
15
+ }
16
+ this.header = header;
17
+ this.indexRoot = indexRoot;
18
+ this.revision = revision;
19
+ Object.freeze(this);
20
+ }
21
+
22
+ static assertOid(value) {
23
+ if (!Oid.isValid(value) || Oid.from(value).toString() !== value) {
24
+ throw new ReservationStorageError('Reservation ledger requires a canonical Git OID');
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,35 @@
1
+ import ReservationCursor from './ReservationCursor.js';
2
+ import CollectionNamespace from './CollectionNamespace.js';
3
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
4
+
5
+ export const RESERVATION_LIST_MAX_RECORDS = 100;
6
+ export const RESERVATION_LIST_MIN_NODES = 129;
7
+ export const RESERVATION_LIST_MAX_NODES = 8192;
8
+
9
+ /** The budget includes every index tree/record read, including cursor seeking. */
10
+ export default class ReservationListRequest {
11
+ constructor(namespace, { cursor = null, phase, limit = 100, nodeBudget = 1024 } = {}) {
12
+ this.namespace = CollectionNamespace.from(namespace).toString();
13
+ this.cursor = cursor === null ? null : new ReservationCursor(cursor);
14
+ this.phase = requestedPhase(phase, this.cursor);
15
+ ReservationCursor.assertPhase(this.phase);
16
+ assertBounds(limit, nodeBudget);
17
+ if (this.cursor !== null && (this.cursor.namespace !== this.namespace || this.cursor.phase !== this.phase)) {
18
+ throw new InvalidOptionsError('Reservation cursor belongs to a different namespace or filter');
19
+ }
20
+ this.limit = limit;
21
+ this.nodeBudget = nodeBudget;
22
+ Object.freeze(this);
23
+ }
24
+ }
25
+
26
+ function requestedPhase(phase, cursor) {
27
+ return phase === undefined ? cursor?.phase ?? null : phase;
28
+ }
29
+
30
+ function assertBounds(limit, nodeBudget) {
31
+ if (!Number.isInteger(limit) || limit < 1 || limit > RESERVATION_LIST_MAX_RECORDS ||
32
+ !Number.isInteger(nodeBudget) || nodeBudget < RESERVATION_LIST_MIN_NODES || nodeBudget > RESERVATION_LIST_MAX_NODES) {
33
+ throw new InvalidOptionsError('Reservation listing allows 1–100 records and 129–8192 visited index nodes');
34
+ }
35
+ }
@@ -0,0 +1,17 @@
1
+ import ReservationListRequest from './ReservationListRequest.js';
2
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
3
+ import { CasError, ErrorCodes } from '../errors/index.js';
4
+
5
+ /** A failed page has no partial records and never advances the supplied cursor. */
6
+ export default class ReservationListUnavailable {
7
+ constructor(request, cause) {
8
+ if (!(request instanceof ReservationListRequest)) {
9
+ throw new InvalidOptionsError('Unavailable reservation page requires its original request');
10
+ }
11
+ this.status = 'unavailable';
12
+ this.namespace = request.namespace;
13
+ this.cursor = request.cursor;
14
+ this.cause = cause instanceof Error ? cause : new CasError('Reservation listing failed', ErrorCodes.GIT_ERROR, { originalError: cause });
15
+ Object.freeze(this);
16
+ }
17
+ }
@@ -0,0 +1,15 @@
1
+ import ReservationPolicy from './ReservationPolicy.js';
2
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
3
+ import { CasError, ErrorCodes } from '../errors/index.js';
4
+
5
+ export default class ReservationOpenIndeterminate {
6
+ constructor(policy, cause) {
7
+ if (!(policy instanceof ReservationPolicy)) {
8
+ throw new InvalidOptionsError('Uncertain namespace open requires its requested policy');
9
+ }
10
+ this.status = 'indeterminate';
11
+ this.policy = policy;
12
+ this.cause = cause instanceof Error ? cause : new CasError('Reservation open failed', ErrorCodes.GIT_ERROR, { originalError: cause });
13
+ Object.freeze(this);
14
+ }
15
+ }
@@ -0,0 +1,14 @@
1
+ import ReservationPolicy from './ReservationPolicy.js';
2
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
3
+
4
+ export default class ReservationOpenRefusal {
5
+ constructor(policy, reason) {
6
+ if (!(policy instanceof ReservationPolicy) || !['policy-mismatch', 'contention'].includes(reason)) {
7
+ throw new InvalidOptionsError('Reservation open refusal requires policy and a known reason');
8
+ }
9
+ this.status = 'refused';
10
+ this.policy = policy;
11
+ this.reason = reason;
12
+ Object.freeze(this);
13
+ }
14
+ }
@@ -0,0 +1,27 @@
1
+ import ReservationLedgerSnapshot from './ReservationLedgerSnapshot.js';
2
+ import ReservationCursor from './ReservationCursor.js';
3
+ import ReservationScan from '../services/ReservationScan.js';
4
+ import ReservationStorageError from '../errors/ReservationStorageError.js';
5
+
6
+ /** Ledger claims at one snapshot; each attempt's live custody is inspected separately. */
7
+ export default class ReservationPage {
8
+ constructor(snapshot, scan, phase) {
9
+ if (!(snapshot instanceof ReservationLedgerSnapshot) || !(scan instanceof ReservationScan)) {
10
+ throw new ReservationStorageError('Reservation page requires a snapshot and bounded scan');
11
+ }
12
+ const records = scan.records;
13
+ if (records.some((record) => record.charge > snapshot.header.charged)) {
14
+ throw new ReservationStorageError('Listed record charge exceeds its ledger aggregate');
15
+ }
16
+ this.status = 'page';
17
+ this.policy = snapshot.header.policy;
18
+ this.charged = snapshot.header.charged;
19
+ this.revision = snapshot.revision;
20
+ this.records = records;
21
+ this.visitedNodes = scan.visitedNodes;
22
+ this.next = scan.complete ? null : new ReservationCursor({
23
+ namespace: this.policy.namespace, revision: this.revision, after: scan.after, phase,
24
+ });
25
+ Object.freeze(this);
26
+ }
27
+ }
@@ -0,0 +1,24 @@
1
+ import CollectionNamespace from './CollectionNamespace.js';
2
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
3
+
4
+ /** Immutable record-slot contract persisted by the namespace authority. */
5
+ export default class ReservationPolicy {
6
+ constructor(value) {
7
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
8
+ throw new InvalidOptionsError('Reservation policy requires named fields');
9
+ }
10
+ const { namespace, capacity } = value;
11
+ if (capacity?.unit !== 'record-slot' || !Number.isSafeInteger(capacity.limit) || capacity.limit < 1) {
12
+ throw new InvalidOptionsError('Reservation capacity requires a positive safe record-slot limit');
13
+ }
14
+ this.namespace = CollectionNamespace.from(namespace).toString();
15
+ this.unit = capacity.unit;
16
+ this.limit = capacity.limit;
17
+ Object.freeze(this);
18
+ }
19
+
20
+ equals(other) {
21
+ return other instanceof ReservationPolicy && this.namespace === other.namespace &&
22
+ this.unit === other.unit && this.limit === other.limit;
23
+ }
24
+ }
@@ -0,0 +1,23 @@
1
+ import CasError from '../errors/CasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+
4
+ const REASONS = new Set([
5
+ 'not-prepared',
6
+ 'already-armed',
7
+ 'already-settled',
8
+ 'cancelled',
9
+ 'irreversible',
10
+ 'preparation-mismatch',
11
+ 'settlement-mismatch',
12
+ ]);
13
+
14
+ /** Expected refusal of a local reservation transition; never an I/O outcome. */
15
+ export default class ReservationRefusal {
16
+ constructor(reason) {
17
+ if (!REASONS.has(reason)) {
18
+ throw new CasError('Unknown reservation refusal reason', ErrorCodes.INVALID_OPTIONS);
19
+ }
20
+ this.reason = reason;
21
+ Object.freeze(this);
22
+ }
23
+ }
@@ -0,0 +1,27 @@
1
+ import ReservationRecord from '../services/ReservationRecord.js';
2
+ import ReservationLedgerSnapshot from './ReservationLedgerSnapshot.js';
3
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
4
+
5
+ /** Durable observation. Only a fresh persisted transition may issue `armed`. */
6
+ export default class ReservationResult {
7
+ constructor({ status, record, revision }) {
8
+ if (!(record instanceof ReservationRecord) || !validStatus(status, record)) {
9
+ throw new InvalidOptionsError('Reservation result requires a record consistent with its outcome');
10
+ }
11
+ ReservationLedgerSnapshot.assertOid(revision);
12
+ this.status = status;
13
+ this.record = record;
14
+ this.revision = revision;
15
+ Object.freeze(this);
16
+ }
17
+ }
18
+
19
+ function validStatus(status, record) {
20
+ if (status === 'recovered') {
21
+ return true;
22
+ }
23
+ if (status === 'admitted') {
24
+ return record.phase === 'reserved';
25
+ }
26
+ return ['prepared', 'armed', 'settled', 'cancelled'].includes(status) && status === record.phase;
27
+ }
@@ -0,0 +1,22 @@
1
+ import Oid from './Oid.js';
2
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
3
+
4
+ /** A real blob/tree reachability edge, never a gitlink or metadata-only OID. */
5
+ export default class ReservationSupport {
6
+ constructor({ type, oid }) {
7
+ if (!['blob', 'tree'].includes(type) || !Oid.isValid(oid) || oid !== oid.toLowerCase()) {
8
+ throw new InvalidOptionsError('Reservation support requires a blob/tree type and canonical OID');
9
+ }
10
+ this.type = type;
11
+ this.oid = oid;
12
+ Object.freeze(this);
13
+ }
14
+
15
+ get mode() {
16
+ return this.type === 'tree' ? '040000' : '100644';
17
+ }
18
+
19
+ equals(other) {
20
+ return other instanceof ReservationSupport && this.type === other.type && this.oid === other.oid;
21
+ }
22
+ }
@@ -68,6 +68,13 @@ export default class GitRefAdapter extends GitRefPort {
68
68
  }
69
69
  }
70
70
 
71
+ /** @override */
72
+ async inspectRef(ref) {
73
+ this.#assertOpen();
74
+ assertRefName(ref);
75
+ return await this.#inspectRefPosture(ref);
76
+ }
77
+
71
78
  /**
72
79
  * @override
73
80
  * @param {string} commitOid - Git commit OID.
@@ -0,0 +1,54 @@
1
+ import ReservationCustodyCodecPort, { RESERVATION_CUSTODY_MAX_BYTES } from '../../ports/ReservationCustodyCodecPort.js';
2
+ import ReservationCustodyDescriptor from '../../domain/value-objects/ReservationCustodyDescriptor.js';
3
+ import ReservationEvidenceRoots from '../../domain/value-objects/ReservationEvidenceRoots.js';
4
+ import ReservationStorageError from '../../domain/errors/ReservationStorageError.js';
5
+ import { utf8Encode, utf8Decode } from '../../domain/encoding/utf8.js';
6
+
7
+ const FIELDS = 'binding,key,namespace,parents,preparation,recordOid,stage,supports,version';
8
+
9
+ /** Noncanonical encodings and extra fields never acquire custody meaning. */
10
+ export default class ReservationCustodyCodec extends ReservationCustodyCodecPort {
11
+ encode(descriptor) {
12
+ if (!(descriptor instanceof ReservationCustodyDescriptor)) {
13
+ throw new ReservationStorageError('Custody encoding requires a validated descriptor');
14
+ }
15
+ const bytes = utf8Encode(JSON.stringify({
16
+ version: 1, namespace: descriptor.namespace, key: descriptor.key, binding: descriptor.binding,
17
+ stage: descriptor.stage, recordOid: descriptor.recordOid, preparation: descriptor.preparation,
18
+ supports: descriptor.roots.supports.map(({ type, oid }) => ({ type, oid })), parents: descriptor.roots.parents,
19
+ }));
20
+ if (bytes.length > RESERVATION_CUSTODY_MAX_BYTES) {
21
+ throw new ReservationStorageError('Custody descriptor exceeds 16 KiB');
22
+ }
23
+ return bytes;
24
+ }
25
+
26
+ decode(bytes) {
27
+ if (!(bytes instanceof Uint8Array) || bytes.length > RESERVATION_CUSTODY_MAX_BYTES) {
28
+ throw new ReservationStorageError('Custody descriptor requires at most 16 KiB of bytes');
29
+ }
30
+ try {
31
+ const value = JSON.parse(utf8Decode(bytes));
32
+ assertEnvelope(value);
33
+ const roots = new ReservationEvidenceRoots({ supports: value.supports, parents: value.parents });
34
+ const descriptor = new ReservationCustodyDescriptor({ ...value, roots });
35
+ const canonical = this.encode(descriptor);
36
+ if (canonical.length !== bytes.length || canonical.some((byte, i) => byte !== bytes[i])) {
37
+ throw new ReservationStorageError('Custody descriptor is not canonically encoded');
38
+ }
39
+ return descriptor;
40
+ } catch (error) {
41
+ if (error instanceof ReservationStorageError) {
42
+ throw error;
43
+ }
44
+ throw new ReservationStorageError('Custody descriptor cannot be decoded', { originalError: error });
45
+ }
46
+ }
47
+ }
48
+
49
+ function assertEnvelope(value) {
50
+ if (value === null || typeof value !== 'object' || Array.isArray(value) ||
51
+ value.version !== 1 || Object.keys(value).sort().join(',') !== FIELDS) {
52
+ throw new ReservationStorageError('Custody descriptor format or fields are invalid');
53
+ }
54
+ }
@@ -0,0 +1,48 @@
1
+ import ReservationLedgerCodecPort, { RESERVATION_HEADER_MAX_BYTES } from '../../ports/ReservationLedgerCodecPort.js';
2
+ import ReservationPolicy from '../../domain/value-objects/ReservationPolicy.js';
3
+ import ReservationLedgerHeader from '../../domain/value-objects/ReservationLedgerHeader.js';
4
+ import ReservationStorageError from '../../domain/errors/ReservationStorageError.js';
5
+ import { utf8Encode, utf8Decode } from '../../domain/encoding/utf8.js';
6
+
7
+ const FORMAT_VERSION = 1;
8
+ const FIELDS = 'charged,limit,namespace,unit,version';
9
+
10
+ /** Strict canonical storage boundary. Unsupported policy cannot become empty. */
11
+ export default class ReservationLedgerCodec extends ReservationLedgerCodecPort {
12
+ encode(header) {
13
+ if (!(header instanceof ReservationLedgerHeader)) {
14
+ throw new ReservationStorageError('Reservation ledger encoding requires a validated header');
15
+ }
16
+ const { namespace, unit, limit } = header.policy;
17
+ return utf8Encode(JSON.stringify({ version: FORMAT_VERSION, namespace, unit, limit, charged: header.charged }));
18
+ }
19
+
20
+ decode(bytes) {
21
+ if (!(bytes instanceof Uint8Array) || bytes.length > RESERVATION_HEADER_MAX_BYTES) {
22
+ throw new ReservationStorageError('Reservation ledger header must be at most 16 KiB');
23
+ }
24
+ try {
25
+ const value = JSON.parse(utf8Decode(bytes));
26
+ assertEnvelope(value);
27
+ const policy = new ReservationPolicy({ namespace: value.namespace, capacity: { unit: value.unit, limit: value.limit } });
28
+ const header = new ReservationLedgerHeader({ policy, charged: value.charged });
29
+ const canonical = this.encode(header);
30
+ if (canonical.length !== bytes.length || canonical.some((byte, i) => byte !== bytes[i])) {
31
+ throw new ReservationStorageError('Reservation ledger header is not canonically encoded');
32
+ }
33
+ return header;
34
+ } catch (error) {
35
+ if (error instanceof ReservationStorageError) {
36
+ throw error;
37
+ }
38
+ throw new ReservationStorageError('Reservation ledger header cannot be decoded', { originalError: error });
39
+ }
40
+ }
41
+ }
42
+
43
+ function assertEnvelope(value) {
44
+ if (value === null || typeof value !== 'object' || Array.isArray(value) ||
45
+ value.version !== FORMAT_VERSION || Object.keys(value).sort().join(',') !== FIELDS) {
46
+ throw new ReservationStorageError('Reservation ledger header format or fields are invalid');
47
+ }
48
+ }
@@ -0,0 +1,54 @@
1
+ import ReservationRecordCodecPort, { RESERVATION_RECORD_MAX_BYTES } from '../../ports/ReservationRecordCodecPort.js';
2
+ import ReservationRecord from '../../domain/services/ReservationRecord.js';
3
+ import ReservationStorageError from '../../domain/errors/ReservationStorageError.js';
4
+ import { utf8Encode, utf8Decode } from '../../domain/encoding/utf8.js';
5
+
6
+ const FORMAT_VERSION = 1;
7
+ const FIELDS = 'binding,key,phase,preparation,settlement,version';
8
+
9
+ /** Exact canonical JSON boundary; extra fields and alternative encodings refuse. */
10
+ export default class ReservationRecordCodec extends ReservationRecordCodecPort {
11
+ encode(record) {
12
+ if (!(record instanceof ReservationRecord)) {
13
+ throw new ReservationStorageError('Reservation encoding requires a validated record');
14
+ }
15
+ return utf8Encode(JSON.stringify({
16
+ version: FORMAT_VERSION, key: record.key, binding: record.binding,
17
+ phase: record.phase, preparation: record.preparation, settlement: record.settlement,
18
+ }));
19
+ }
20
+
21
+ decode(bytes) {
22
+ assertBytes(bytes);
23
+ try {
24
+ const value = JSON.parse(utf8Decode(bytes));
25
+ assertEnvelope(value);
26
+ const record = new ReservationRecord(value);
27
+ const canonical = this.encode(record);
28
+ if (canonical.length !== bytes.length || canonical.some((byte, i) => byte !== bytes[i])) {
29
+ throw new ReservationStorageError('Reservation record is not canonically encoded');
30
+ }
31
+ return record;
32
+ } catch (error) {
33
+ if (error instanceof ReservationStorageError) {
34
+ throw error;
35
+ }
36
+ throw new ReservationStorageError('Reservation record cannot be decoded', { originalError: error });
37
+ }
38
+ }
39
+ }
40
+
41
+ function assertBytes(bytes) {
42
+ if (!(bytes instanceof Uint8Array) || bytes.length > RESERVATION_RECORD_MAX_BYTES) {
43
+ throw new ReservationStorageError('Reservation record must be at most 16 KiB of bytes');
44
+ }
45
+ }
46
+
47
+ function assertEnvelope(value) {
48
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
49
+ throw new ReservationStorageError('Reservation record must be an object');
50
+ }
51
+ if (value.version !== FORMAT_VERSION || Object.keys(value).sort().join(',') !== FIELDS) {
52
+ throw new ReservationStorageError('Reservation record format or fields are invalid');
53
+ }
54
+ }
@@ -1 +1 @@
1
- export const PACKAGE_VERSION = '6.5.12';
1
+ export const PACKAGE_VERSION = '7.1.0';
@@ -1,11 +1,22 @@
1
1
  import createCasError from '../domain/errors/createCasError.js';
2
2
  import { ErrorCodes } from '../domain/errors/index.js';
3
+ import PortNotImplementedError from '../domain/errors/PortNotImplementedError.js';
3
4
 
4
5
  /**
5
6
  * Abstract port for Git ref and commit operations.
6
7
  * @abstract
7
8
  */
8
9
  export default class GitRefPort {
10
+ /**
11
+ * Inspects one canonical ref without trusting symbolic dereferencing.
12
+ * A dangling symbolic ref is present, not an absent direct ref.
13
+ * @param {string} _ref
14
+ * @returns {Promise<{oid: string|null, symref: string|null}>}
15
+ */
16
+ async inspectRef(_ref) {
17
+ throw new PortNotImplementedError('Exact ref inspection is not implemented');
18
+ }
19
+
9
20
  /**
10
21
  * Resolves a Git ref to its commit OID.
11
22
  * @param {string} _ref - Git ref (e.g. 'refs/cas/vault').
@@ -0,0 +1,14 @@
1
+ import PortNotImplementedError from '../domain/errors/PortNotImplementedError.js';
2
+
3
+ export const RESERVATION_CUSTODY_MAX_BYTES = 16 * 1024;
4
+
5
+ /** Versioned canonical descriptor boundary for actual custody edges. */
6
+ export default class ReservationCustodyCodecPort {
7
+ encode(_descriptor) {
8
+ throw new PortNotImplementedError('Reservation custody encoding is not implemented');
9
+ }
10
+
11
+ decode(_bytes) {
12
+ throw new PortNotImplementedError('Reservation custody decoding is not implemented');
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import PortNotImplementedError from '../domain/errors/PortNotImplementedError.js';
2
+
3
+ export const RESERVATION_HEADER_MAX_BYTES = 16 * 1024;
4
+
5
+ /** Bounded encoding boundary for namespace policy and aggregate charge. */
6
+ export default class ReservationLedgerCodecPort {
7
+ encode(_header) {
8
+ throw new PortNotImplementedError('Reservation ledger encoding is not implemented');
9
+ }
10
+
11
+ decode(_bytes) {
12
+ throw new PortNotImplementedError('Reservation ledger decoding is not implemented');
13
+ }
14
+ }