@githolon/dsl 0.113.4 → 0.115.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 (103) hide show
  1. package/assets/operation-contract.wasm +0 -0
  2. package/compile_package.mjs +23 -3
  3. package/compile_proof.d.mts +7 -0
  4. package/compile_proof.mjs +38 -3
  5. package/dart/CHANGELOG.md +60 -0
  6. package/dart/lib/src/application_framework_calls.dart +16 -6
  7. package/dart/lib/src/console_enrolment.dart +7 -0
  8. package/dart/lib/src/console_http_io.dart +22 -15
  9. package/dart/lib/src/dev_cloud.dart +62 -0
  10. package/dart/lib/src/generated_call.dart +14 -3
  11. package/dart/lib/src/nms_generated_runtime.dart +7 -0
  12. package/dart/lib/src/node_runner_transport.dart +77 -8
  13. package/dart/lib/src/nomos_trace.dart +107 -0
  14. package/dart/lib/src/operation_receipts.dart +74 -7
  15. package/dart/lib/src/reception/dart_reception.dart +174 -17
  16. package/dart/lib/src/rows_watch.dart +390 -0
  17. package/dart/lib/src/runner/node-entry.mjs +7678 -2860
  18. package/dart/lib/src/runtime_v2_transport.dart +3 -0
  19. package/dart/lib/src/sync_status_projection.dart +94 -1
  20. package/dart/lib/src/sync_status_reducer.dart +5 -2
  21. package/dart/lib/src/test_harness.dart +107 -41
  22. package/dart/lib/src/testing/test_peer.dart +107 -99
  23. package/dart/lib/src/testing/test_peer_host.dart +305 -0
  24. package/dart/lib/src/typed_watch_registry.dart +26 -3
  25. package/dart/pubspec.lock +1 -1
  26. package/dart/pubspec.yaml +2 -2
  27. package/dart/test/background_model_adoption_test.dart +1 -0
  28. package/dart/test/console_enrolment_test.dart +47 -0
  29. package/dart/test/custodian_warming_test.dart +105 -0
  30. package/dart/test/fixtures/offer_watch_order_peer.dart +74 -0
  31. package/dart/test/kernel_author_after_read_test.dart +6 -3
  32. package/dart/test/kernel_fence_test.dart +4 -1
  33. package/dart/test/model_adoption_test.dart +66 -0
  34. package/dart/test/nomos_trace_test.dart +91 -0
  35. package/dart/test/own_offer_watch_order_test.dart +90 -0
  36. package/dart/test/receiving_group_open_test.dart +4 -8
  37. package/dart/test/replica_readiness_test.dart +14 -4
  38. package/dart/test/rows_watch_test.dart +331 -0
  39. package/dart/test/runtime_projections_test.dart +41 -0
  40. package/dart/test/test_peer_host_selection_test.dart +49 -0
  41. package/dart_types/CHANGELOG.md +39 -0
  42. package/dart_types/lib/src/approved_model.dart +100 -6
  43. package/dart_types/lib/src/proto/nomos/kernel/v1/kernel.pb.dart +1063 -3
  44. package/dart_types/lib/src/proto/nomos/kernel/v1/kernel.pbenum.dart +89 -0
  45. package/dart_types/lib/src/proto/nomos/runtime/v2/application_desk.pb.dart +14 -0
  46. package/dart_types/lib/src/proto/nomos/runtime/v2/correspondence_desk.pb.dart +2272 -130
  47. package/dart_types/lib/src/proto/nomos/runtime/v2/correspondence_desk.pbenum.dart +65 -0
  48. package/dart_types/lib/src/proto/nomos/runtime/v2/model_adoption_desk.pb.dart +118 -2
  49. package/dart_types/lib/src/proto/nomos/runtime/v2/observability_desk.pb.dart +278 -9
  50. package/dart_types/lib/src/proto/nomos/runtime/v2/receiving_group_desk.pb.dart +7 -85
  51. package/dart_types/lib/src/proto/nomos/runtime/v2/records_desk.pb.dart +16 -0
  52. package/dart_types/lib/src/proto/nomos/runtime/v2/residency_desk.pb.dart +35 -0
  53. package/dart_types/lib/src/proto/nomos/runtime/v2/runtime.pb.dart +43 -2
  54. package/dart_types/pubspec.yaml +1 -1
  55. package/package.json +6 -2
  56. package/src/aggregate.ts +93 -0
  57. package/src/anonymous_value_interning.ts +205 -0
  58. package/src/baseline.ts +27 -0
  59. package/src/baseline_export.ts +432 -0
  60. package/src/baseline_import.ts +312 -0
  61. package/src/baseline_usda.ts +186 -0
  62. package/src/birth_key_rule.ts +472 -0
  63. package/src/births.ts +207 -24
  64. package/src/build_provenance.ts +17 -2
  65. package/src/business_birth.ts +50 -4
  66. package/src/check_package_main.ts +50 -6
  67. package/src/codegen_call_proto.ts +5 -18
  68. package/src/codegen_dart.ts +95 -3
  69. package/src/codegen_dart_client.ts +73 -15
  70. package/src/codegen_dart_facade.ts +58 -35
  71. package/src/codegen_dart_framework_calls.ts +32 -10
  72. package/src/codegen_dart_scenario.ts +7 -0
  73. package/src/codegen_proof.ts +5 -2
  74. package/src/codegen_result_proto.ts +8 -6
  75. package/src/codegen_result_usda.ts +4 -0
  76. package/src/codegen_ts.ts +135 -34
  77. package/src/compatibility_graph.ts +41 -4
  78. package/src/compatibility_usda.ts +11 -1
  79. package/src/compile_package_main.ts +272 -105
  80. package/src/composed_wire_identity.ts +24 -0
  81. package/src/directive.ts +6 -0
  82. package/src/domain_requirements.ts +75 -0
  83. package/src/engine_entry.ts +4 -48
  84. package/src/engine_javascript.ts +12 -6
  85. package/src/fields.ts +109 -0
  86. package/src/identity_usda.ts +77 -51
  87. package/src/index.ts +25 -1
  88. package/src/intent.ts +5 -0
  89. package/src/law_graph.ts +142 -1
  90. package/src/law_graph_usda.ts +25 -141
  91. package/src/law_layer_prims.ts +199 -0
  92. package/src/law_unit_selection.ts +45 -0
  93. package/src/law_unit_set_identity.ts +44 -0
  94. package/src/law_version.ts +107 -0
  95. package/src/package_config.ts +44 -0
  96. package/src/package_dependencies.ts +8 -0
  97. package/src/query/compile.ts +0 -0
  98. package/src/release_inspection.ts +24 -0
  99. package/src/release_publication.ts +298 -0
  100. package/src/value_object.ts +6 -3
  101. package/src/wire.ts +2 -0
  102. package/src/wire_paths.ts +17 -0
  103. package/src/workspace_routing.ts +1 -1
@@ -247,6 +247,8 @@ class ResidentWorkspaceDetail extends $pb.GeneratedMessage {
247
247
  ResidentOutbox? outbox,
248
248
  ResidentAggregateCensus? census,
249
249
  $core.String? fullReplayHead,
250
+ $fixnum.Int64? localHistoryCommits,
251
+ $core.String? placementUnobservedReason,
250
252
  }) {
251
253
  final result = create();
252
254
  if (workspace != null) result.workspace = workspace;
@@ -267,6 +269,10 @@ class ResidentWorkspaceDetail extends $pb.GeneratedMessage {
267
269
  if (outbox != null) result.outbox = outbox;
268
270
  if (census != null) result.census = census;
269
271
  if (fullReplayHead != null) result.fullReplayHead = fullReplayHead;
272
+ if (localHistoryCommits != null)
273
+ result.localHistoryCommits = localHistoryCommits;
274
+ if (placementUnobservedReason != null)
275
+ result.placementUnobservedReason = placementUnobservedReason;
270
276
  return result;
271
277
  }
272
278
 
@@ -319,6 +325,10 @@ class ResidentWorkspaceDetail extends $pb.GeneratedMessage {
319
325
  ..aOM<ResidentAggregateCensus>(14, _omitFieldNames ? '' : 'census',
320
326
  subBuilder: ResidentAggregateCensus.create)
321
327
  ..aOS(15, _omitFieldNames ? '' : 'fullReplayHead')
328
+ ..a<$fixnum.Int64>(
329
+ 16, _omitFieldNames ? '' : 'localHistoryCommits', $pb.PbFieldType.OU6,
330
+ defaultOrMaker: $fixnum.Int64.ZERO)
331
+ ..aOS(17, _omitFieldNames ? '' : 'placementUnobservedReason')
322
332
  ..hasRequiredFields = false;
323
333
 
324
334
  @$core.Deprecated('See https://github.com/google/protobuf.dart/issues/998.')
@@ -511,6 +521,31 @@ class ResidentWorkspaceDetail extends $pb.GeneratedMessage {
511
521
  $core.bool hasFullReplayHead() => $_has(14);
512
522
  @$pb.TagNumber(15)
513
523
  void clearFullReplayHead() => $_clearField(15);
524
+
525
+ /// The size of a PARTIAL local history: commits this host holds on main's first-parent chain,
526
+ /// from git_head down to the shallow boundary inclusive. Exact for git_head. Measured once by a
527
+ /// bounded idle-lane walk after a custody event that moves the boundary or the head by an unknown
528
+ /// amount (acquisition, sync fast-forward, deepening), then advanced by one per admitted offer;
529
+ /// health never walks history. Absent when not yet measured for this head, and for COMPLETE
530
+ /// history, which is never walked for telemetry.
531
+ @$pb.TagNumber(16)
532
+ $fixnum.Int64 get localHistoryCommits => $_getI64(15);
533
+ @$pb.TagNumber(16)
534
+ set localHistoryCommits($fixnum.Int64 value) => $_setInt64(15, value);
535
+ @$pb.TagNumber(16)
536
+ $core.bool hasLocalHistoryCommits() => $_has(15);
537
+ @$pb.TagNumber(16)
538
+ void clearLocalHistoryCommits() => $_clearField(16);
539
+
540
+ /// Why `placement` is absent, in the office's words. Set only when placement is absent.
541
+ @$pb.TagNumber(17)
542
+ $core.String get placementUnobservedReason => $_getSZ(16);
543
+ @$pb.TagNumber(17)
544
+ set placementUnobservedReason($core.String value) => $_setString(16, value);
545
+ @$pb.TagNumber(17)
546
+ $core.bool hasPlacementUnobservedReason() => $_has(16);
547
+ @$pb.TagNumber(17)
548
+ void clearPlacementUnobservedReason() => $_clearField(17);
514
549
  }
515
550
 
516
551
  /// The frontier's authenticated aggregate count, and the most numerous types from the resident
@@ -85,6 +85,7 @@ enum RuntimeRequest_Command {
85
85
  configureProviderOrigins,
86
86
  fetchWorkspace,
87
87
  provideLawProducts,
88
+ serveLiveStream,
88
89
  notSet
89
90
  }
90
91
 
@@ -145,6 +146,7 @@ class RuntimeRequest extends $pb.GeneratedMessage {
145
146
  $7.ConfigureProviderOriginsRequest? configureProviderOrigins,
146
147
  $2.FetchWorkspaceRequest? fetchWorkspace,
147
148
  $2.ProvideLawProductsRequest? provideLawProducts,
149
+ $3.LiveStreamRequest? serveLiveStream,
148
150
  }) {
149
151
  final result = create();
150
152
  if (protocolVersion != null) result.protocolVersion = protocolVersion;
@@ -223,6 +225,7 @@ class RuntimeRequest extends $pb.GeneratedMessage {
223
225
  if (fetchWorkspace != null) result.fetchWorkspace = fetchWorkspace;
224
226
  if (provideLawProducts != null)
225
227
  result.provideLawProducts = provideLawProducts;
228
+ if (serveLiveStream != null) result.serveLiveStream = serveLiveStream;
226
229
  return result;
227
230
  }
228
231
 
@@ -284,6 +287,7 @@ class RuntimeRequest extends $pb.GeneratedMessage {
284
287
  62: RuntimeRequest_Command.configureProviderOrigins,
285
288
  63: RuntimeRequest_Command.fetchWorkspace,
286
289
  64: RuntimeRequest_Command.provideLawProducts,
290
+ 65: RuntimeRequest_Command.serveLiveStream,
287
291
  0: RuntimeRequest_Command.notSet
288
292
  };
289
293
  static final $pb.BuilderInfo _i = $pb.BuilderInfo(
@@ -338,7 +342,8 @@ class RuntimeRequest extends $pb.GeneratedMessage {
338
342
  61,
339
343
  62,
340
344
  63,
341
- 64
345
+ 64,
346
+ 65
342
347
  ])
343
348
  ..aI(1, _omitFieldNames ? '' : 'protocolVersion',
344
349
  fieldType: $pb.PbFieldType.OU3)
@@ -476,6 +481,8 @@ class RuntimeRequest extends $pb.GeneratedMessage {
476
481
  ..aOM<$2.ProvideLawProductsRequest>(
477
482
  64, _omitFieldNames ? '' : 'provideLawProducts',
478
483
  subBuilder: $2.ProvideLawProductsRequest.create)
484
+ ..aOM<$3.LiveStreamRequest>(65, _omitFieldNames ? '' : 'serveLiveStream',
485
+ subBuilder: $3.LiveStreamRequest.create)
479
486
  ..hasRequiredFields = false;
480
487
 
481
488
  @$core.Deprecated('See https://github.com/google/protobuf.dart/issues/998.')
@@ -544,6 +551,7 @@ class RuntimeRequest extends $pb.GeneratedMessage {
544
551
  @$pb.TagNumber(62)
545
552
  @$pb.TagNumber(63)
546
553
  @$pb.TagNumber(64)
554
+ @$pb.TagNumber(65)
547
555
  RuntimeRequest_Command whichCommand() =>
548
556
  _RuntimeRequest_CommandByTag[$_whichOneof(0)]!;
549
557
  @$pb.TagNumber(10)
@@ -593,6 +601,7 @@ class RuntimeRequest extends $pb.GeneratedMessage {
593
601
  @$pb.TagNumber(62)
594
602
  @$pb.TagNumber(63)
595
603
  @$pb.TagNumber(64)
604
+ @$pb.TagNumber(65)
596
605
  void clearCommand() => $_clearField($_whichOneof(0));
597
606
 
598
607
  @$pb.TagNumber(1)
@@ -1196,6 +1205,18 @@ class RuntimeRequest extends $pb.GeneratedMessage {
1196
1205
  void clearProvideLawProducts() => $_clearField(64);
1197
1206
  @$pb.TagNumber(64)
1198
1207
  $2.ProvideLawProductsRequest ensureProvideLawProducts() => $_ensure(50);
1208
+
1209
+ /// A coordinator asks the cloud peer that holds this workspace to serve one stream request.
1210
+ @$pb.TagNumber(65)
1211
+ $3.LiveStreamRequest get serveLiveStream => $_getN(51);
1212
+ @$pb.TagNumber(65)
1213
+ set serveLiveStream($3.LiveStreamRequest value) => $_setField(65, value);
1214
+ @$pb.TagNumber(65)
1215
+ $core.bool hasServeLiveStream() => $_has(51);
1216
+ @$pb.TagNumber(65)
1217
+ void clearServeLiveStream() => $_clearField(65);
1218
+ @$pb.TagNumber(65)
1219
+ $3.LiveStreamRequest ensureServeLiveStream() => $_ensure(51);
1199
1220
  }
1200
1221
 
1201
1222
  class InvocationTarget extends $pb.GeneratedMessage {
@@ -1318,6 +1339,7 @@ enum RuntimeResponse_Outcome {
1318
1339
  providerOriginsConfigured,
1319
1340
  workspaceFetch,
1320
1341
  lawProducts,
1342
+ liveStreamServed,
1321
1343
  notSet
1322
1344
  }
1323
1345
 
@@ -1377,6 +1399,7 @@ class RuntimeResponse extends $pb.GeneratedMessage {
1377
1399
  $7.ProviderOriginsConfigured? providerOriginsConfigured,
1378
1400
  $2.WorkspaceFetch? workspaceFetch,
1379
1401
  $18.ExportLawProductsResponse? lawProducts,
1402
+ $3.LiveStreamAnswer? liveStreamServed,
1380
1403
  }) {
1381
1404
  final result = create();
1382
1405
  if (protocolVersion != null) result.protocolVersion = protocolVersion;
@@ -1457,6 +1480,7 @@ class RuntimeResponse extends $pb.GeneratedMessage {
1457
1480
  result.providerOriginsConfigured = providerOriginsConfigured;
1458
1481
  if (workspaceFetch != null) result.workspaceFetch = workspaceFetch;
1459
1482
  if (lawProducts != null) result.lawProducts = lawProducts;
1483
+ if (liveStreamServed != null) result.liveStreamServed = liveStreamServed;
1460
1484
  return result;
1461
1485
  }
1462
1486
 
@@ -1523,6 +1547,7 @@ class RuntimeResponse extends $pb.GeneratedMessage {
1523
1547
  66: RuntimeResponse_Outcome.providerOriginsConfigured,
1524
1548
  67: RuntimeResponse_Outcome.workspaceFetch,
1525
1549
  68: RuntimeResponse_Outcome.lawProducts,
1550
+ 69: RuntimeResponse_Outcome.liveStreamServed,
1526
1551
  0: RuntimeResponse_Outcome.notSet
1527
1552
  };
1528
1553
  static final $pb.BuilderInfo _i = $pb.BuilderInfo(
@@ -1582,7 +1607,8 @@ class RuntimeResponse extends $pb.GeneratedMessage {
1582
1607
  65,
1583
1608
  66,
1584
1609
  67,
1585
- 68
1610
+ 68,
1611
+ 69
1586
1612
  ])
1587
1613
  ..aI(1, _omitFieldNames ? '' : 'protocolVersion',
1588
1614
  fieldType: $pb.PbFieldType.OU3)
@@ -1719,6 +1745,8 @@ class RuntimeResponse extends $pb.GeneratedMessage {
1719
1745
  ..aOM<$18.ExportLawProductsResponse>(
1720
1746
  68, _omitFieldNames ? '' : 'lawProducts',
1721
1747
  subBuilder: $18.ExportLawProductsResponse.create)
1748
+ ..aOM<$3.LiveStreamAnswer>(69, _omitFieldNames ? '' : 'liveStreamServed',
1749
+ subBuilder: $3.LiveStreamAnswer.create)
1722
1750
  ..hasRequiredFields = false;
1723
1751
 
1724
1752
  @$core.Deprecated('See https://github.com/google/protobuf.dart/issues/998.')
@@ -1792,6 +1820,7 @@ class RuntimeResponse extends $pb.GeneratedMessage {
1792
1820
  @$pb.TagNumber(66)
1793
1821
  @$pb.TagNumber(67)
1794
1822
  @$pb.TagNumber(68)
1823
+ @$pb.TagNumber(69)
1795
1824
  RuntimeResponse_Outcome whichOutcome() =>
1796
1825
  _RuntimeResponse_OutcomeByTag[$_whichOneof(0)]!;
1797
1826
  @$pb.TagNumber(10)
@@ -1846,6 +1875,7 @@ class RuntimeResponse extends $pb.GeneratedMessage {
1846
1875
  @$pb.TagNumber(66)
1847
1876
  @$pb.TagNumber(67)
1848
1877
  @$pb.TagNumber(68)
1878
+ @$pb.TagNumber(69)
1849
1879
  void clearOutcome() => $_clearField($_whichOneof(0));
1850
1880
 
1851
1881
  @$pb.TagNumber(1)
@@ -2469,6 +2499,17 @@ class RuntimeResponse extends $pb.GeneratedMessage {
2469
2499
  void clearLawProducts() => $_clearField(68);
2470
2500
  @$pb.TagNumber(68)
2471
2501
  $18.ExportLawProductsResponse ensureLawProducts() => $_ensure(53);
2502
+
2503
+ @$pb.TagNumber(69)
2504
+ $3.LiveStreamAnswer get liveStreamServed => $_getN(54);
2505
+ @$pb.TagNumber(69)
2506
+ set liveStreamServed($3.LiveStreamAnswer value) => $_setField(69, value);
2507
+ @$pb.TagNumber(69)
2508
+ $core.bool hasLiveStreamServed() => $_has(54);
2509
+ @$pb.TagNumber(69)
2510
+ void clearLiveStreamServed() => $_clearField(69);
2511
+ @$pb.TagNumber(69)
2512
+ $3.LiveStreamAnswer ensureLiveStreamServed() => $_ensure(54);
2472
2513
  }
2473
2514
 
2474
2515
  enum CustodyOfferBatchResponse_Outcome { admission, failure, blocked, notSet }
@@ -9,7 +9,7 @@ description: >-
9
9
  Pure kernel wire/data types for Nomos Dart frontends — no transport, no bridge,
10
10
  no runtime. Typed protobuf and USDA boundary values plus the offer codec,
11
11
  reactive read engine, schema validation, and data-only error/trace types.
12
- version: 0.12.0
12
+ version: 0.14.0
13
13
 
14
14
  environment:
15
15
  sdk: ^3.5.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@githolon/dsl",
3
- "version": "0.113.4",
3
+ "version": "0.115.0",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "Nomos 2 domain-authoring DSL: aggregates + directives in TS, executed and encoded to the Rust kernel's wire shapes.",
@@ -25,6 +25,10 @@
25
25
  "types": "./compile_proof.d.mts",
26
26
  "default": "./compile_proof.mjs"
27
27
  },
28
+ "./release-publication": "./src/release_publication.ts",
29
+ "./release-inspection": "./src/release_inspection.ts",
30
+ "./law-version": "./src/law_version.ts",
31
+ "./baseline": "./src/baseline.ts",
28
32
  "./workspace-type": "./src/workspace_type.ts",
29
33
  "./workspace-routing": "./src/workspace_routing.ts",
30
34
  "./workspace-sharding": "./src/workspace_sharding.ts",
@@ -73,7 +77,7 @@
73
77
  "test": "vitest run"
74
78
  },
75
79
  "dependencies": {
76
- "@githolon/runtime": "^0.113.4",
80
+ "@githolon/runtime": "^0.115.0",
77
81
  "@bufbuild/protobuf": "^2.13.0",
78
82
  "@bufbuild/protoc-gen-es": "^2.13.0",
79
83
  "esbuild": "0.24.2",
package/src/aggregate.ts CHANGED
@@ -42,6 +42,89 @@ export type AggregateInvariantFn = (
42
42
  previous?: Readonly<Record<string, unknown>> | null,
43
43
  ) => AggregateInvariantVerdict;
44
44
 
45
+ /**
46
+ * BIRTH KEYS ARE WRITE-ONCE (CAP-804 / CO2-1687). A field-keyed `birth(Aggregate, { key })` names the born
47
+ * workspace `workspaceNameForKey(namespace, <key value>)` — fixed at birth, forever. So the framework, not
48
+ * the tenant, attaches a write-once rule to that field of the born aggregate: it rides the aggregate's
49
+ * composed invariant (the SAME #41 gate machinery as `.fromBirth()` presence and CO2's hand-written
50
+ * `immutableIdentity`), so it is judged at the kernel on author, on edge admission and on verify.
51
+ *
52
+ * - creation (`previous === null`), or a field never set before → ADMITTED (the birth seeds it);
53
+ * - a write that leaves the value unchanged (a genesis seed re-stating the key) → ADMITTED;
54
+ * - a write that CHANGES the value → REFUSED (`{ refuse }`: no strike, no state change) with the typed
55
+ * code `<Aggregate>.<field>:birth-key-write-once` (see {@link BIRTH_KEY_WRITE_ONCE_CODE}).
56
+ *
57
+ * A caller that supplies no transition context (`previous === undefined`, a snapshot-only legacy oracle)
58
+ * cannot show a change, so the rule holds vacuously there — it only ever judges transitions.
59
+ *
60
+ * ERA: the rule lives in the invariant BODY, which ships in the law's own engine bundle; invariant
61
+ * PRESENCE (the hashed manifest bit) is unchanged, because a birth key is always a `.fromBirth()` field
62
+ * and every `.fromBirth()` aggregate already declares the composed presence invariant. Chains authored
63
+ * under earlier law replay under that law byte-identically; only law compiled with this DSL carries it.
64
+ *
65
+ * VALUE-FREE: the refusal names the field, never a value, and it can only tell a writer whether its
66
+ * write would change the key — which the writer, already inside the workspace, can derive from the
67
+ * workspace's own name. It therefore keeps the presence check's `invariantValueFree` posture.
68
+ */
69
+ export const BIRTH_KEY_WRITE_ONCE_CODE = "birth-key-write-once";
70
+
71
+ /** @internal Where `birth()` registers the write-once birth-key fields of an aggregate. Symbol-keyed so it
72
+ * is copied by the handle builders' spreads (every `.public()`/`.readableBy()`/`.owned()` handle shares
73
+ * ONE set) yet invisible to `Object.keys`/`JSON.stringify` (never hashed, never emitted). */
74
+ export const BIRTH_KEY_FIELDS: unique symbol = Symbol.for("nomos.birthKeyWriteOnceFields");
75
+
76
+ /** True for an aggregate-invariant refusal code produced by the birth-key write-once rule. */
77
+ export function isBirthKeyWriteOnceRefusal(code: string): boolean {
78
+ return /^[^\s]+\.[^\s.:]+:birth-key-write-once(?:\s|$)/.test(code);
79
+ }
80
+
81
+ /** @internal Register `field` of `aggregate` as a write-once birth key (called by `birth()`). */
82
+ export function __declareBirthKeyWriteOnce(aggregate: AggregateHandle, field: string): void {
83
+ const registry = (aggregate as unknown as { [BIRTH_KEY_FIELDS]?: Set<string> })[BIRTH_KEY_FIELDS];
84
+ if (registry === undefined) {
85
+ throw new Error(
86
+ `birth(${aggregate.id}): this aggregate handle cannot carry the framework's write-once birth-key rule ` +
87
+ `(it was not built by aggregate())`,
88
+ );
89
+ }
90
+ registry.add(field);
91
+ }
92
+
93
+ /** The write-once birth-key fields `birth()` registered on this aggregate (empty when none). */
94
+ export function birthKeyWriteOnceFields(aggregate: AggregateHandle): readonly string[] {
95
+ const registry = (aggregate as unknown as { [BIRTH_KEY_FIELDS]?: Set<string> })[BIRTH_KEY_FIELDS];
96
+ return registry === undefined ? [] : [...registry].sort();
97
+ }
98
+
99
+ function sameKeyValue(a: unknown, b: unknown): boolean {
100
+ if (a === b) return true;
101
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
102
+ return JSON.stringify(a) === JSON.stringify(b);
103
+ }
104
+
105
+ /** @internal The write-once judgement over one transition (exported for direct tests). */
106
+ export function judgeBirthKeyWriteOnce(
107
+ aggregateId: string,
108
+ fields: Iterable<string>,
109
+ snapshot: Readonly<Record<string, unknown>>,
110
+ previous: Readonly<Record<string, unknown>> | null | undefined,
111
+ ): AggregateInvariantVerdict {
112
+ if (previous === undefined || previous === null) return { accept: true };
113
+ for (const name of fields) {
114
+ const before = previous[name];
115
+ if (before === undefined || before === null) continue;
116
+ if (!sameKeyValue(before, snapshot[name])) {
117
+ return {
118
+ refuse:
119
+ `${aggregateId}.${name}:${BIRTH_KEY_WRITE_ONCE_CODE} — '${name}' is the birth key of ${aggregateId}: the ` +
120
+ `born workspace's name was derived from it at birth and is fixed forever, so it may be seeded with the ` +
121
+ `same value but never changed (architecture/choosing_a_birth_key.md)`,
122
+ };
123
+ }
124
+ }
125
+ return { accept: true };
126
+ }
127
+
45
128
  /**
46
129
  * READ VISIBILITY (the native-relations read gate) — the DECLARED posture of an
47
130
  * aggregate's data on the read side. `public` (the DEFAULT, and the only value
@@ -640,6 +723,10 @@ export function aggregate<const Id extends string, F extends Record<string, Fiel
640
723
  return userInvariant !== undefined ? userInvariant(snapshot, previous) : { accept: true };
641
724
  }
642
725
  : userInvariant;
726
+ // BIRTH-KEY WRITE-ONCE (CAP-804): `birth()` registers its key field here AFTER this aggregate is
727
+ // declared; the composed invariant reads the shared set at evaluation time. A birth key is always a
728
+ // `.fromBirth()` field, so the presence invariant (and its hashed presence bit) already exists.
729
+ const birthKeyFields = new Set<string>();
643
730
  const composedInvariant: AggregateInvariantFn | undefined =
644
731
  fromBirthFields.length > 0
645
732
  ? (snapshot, previous) => {
@@ -649,6 +736,10 @@ export function aggregate<const Id extends string, F extends Record<string, Fiel
649
736
  return { reject: `${id}.${name} is fromBirth() — every ${id} must carry it from creation (the plan left it unset)` };
650
737
  }
651
738
  }
739
+ if (birthKeyFields.size > 0) {
740
+ const writeOnce = judgeBirthKeyWriteOnce(id, birthKeyFields, snapshot, previous);
741
+ if (!("accept" in writeOnce)) return writeOnce;
742
+ }
652
743
  return afterFulfilment !== undefined ? afterFulfilment(snapshot, previous) : { accept: true };
653
744
  }
654
745
  : afterFulfilment;
@@ -660,6 +751,8 @@ export function aggregate<const Id extends string, F extends Record<string, Fiel
660
751
  fields: stored as StoredFields<F>,
661
752
  hasMany: hasMany as HasManyFields<F>,
662
753
  __isAggregateHandle: true as const,
754
+ // Only a `.fromBirth()` aggregate can be born, so only it carries the registry (see BIRTH_KEY_FIELDS).
755
+ ...(fromBirthFields.length > 0 ? { [BIRTH_KEY_FIELDS]: birthKeyFields } : {}),
663
756
  ...(opts?.fulfils !== undefined ? { fulfilment: opts.fulfils } : {}),
664
757
  ...(composedInvariant !== undefined
665
758
  ? { hasInvariant: true as const, invariant: composedInvariant }
@@ -0,0 +1,205 @@
1
+ // NOMOS — Structural interning of anonymous value types: one semantic shape, one canonical node.
2
+ //
3
+ // Anonymous value types are structural values, not independently lived declarations. The graph
4
+ // deliberately retains their source owner for diagnostics, but USDA must not turn that provenance
5
+ // into thousands of duplicate runtime prims. This computes a semantic key which excludes owner
6
+ // identity, then points every equivalent type graph — and each of its member fields and union
7
+ // variants — at one canonical node. Declared types remain untouched. Recursive graphs keep their
8
+ // owner-scoped identity for now: aliasing a cycle needs an explicit recursion-isomorphism proof,
9
+ // whereas every acyclic case below is exact by construction.
10
+ //
11
+ // ONE interning, TWO consumers. The USDA emitter (law_graph_usda.ts) redirects duplicate prims to
12
+ // the canonical prim; the graph discovery (law_graph.ts) ledgers a nested payload field's wire key
13
+ // against the canonical member, because that is the key installed law serves and generated clients
14
+ // encode (codegen_call_proto.ts `composedWireIdentity`). Computing it twice from the same nodes
15
+ // is what keeps the ledger, the law and the clients on one allocation (CAP-660).
16
+ import type {
17
+ LawAnonymousValueFieldNode,
18
+ LawAnonymousValueTypeNode,
19
+ LawAnonymousValueVariantNode,
20
+ } from "./law_graph.js";
21
+
22
+ export interface AnonymousValueInterningInput {
23
+ readonly types: readonly LawAnonymousValueTypeNode[];
24
+ readonly fields: readonly LawAnonymousValueFieldNode[];
25
+ readonly variants: readonly LawAnonymousValueVariantNode[];
26
+ /** Whether this type is emitted as its own `Types/t_<sid>` prim (wrappers and framework scalars are not). */
27
+ readonly ownsPrim: (type: LawAnonymousValueTypeNode) => boolean;
28
+ }
29
+
30
+ export interface AnonymousValueInterning {
31
+ /** Every prim-owning type with a semantic key → its canonical type id (itself when it is the canonical one). */
32
+ readonly canonicalTypeIdById: ReadonlyMap<string, string>;
33
+ /** A duplicate type's member field → the canonical type's equivalent member (duplicates only). */
34
+ readonly canonicalFieldIdById: ReadonlyMap<string, string>;
35
+ /** A duplicate sum's variant → the canonical sum's equivalent variant (duplicates only). */
36
+ readonly canonicalVariantIdById: ReadonlyMap<string, string>;
37
+ /** The owner-free structural key of a type, or undefined for a recursive (or unknown) graph. */
38
+ readonly semanticTypeKey: (id: string) => string | undefined;
39
+ }
40
+
41
+ /** A type whose shape is spelled by the framework's own scalar prim, or a wrapper, owns no prim. */
42
+ export function anonymousTypeOwnsPrim(type: LawAnonymousValueTypeNode): boolean {
43
+ const expression = type.expression;
44
+ if (expression.kind === "string" && expression.constraints.length === 0) return false;
45
+ if (expression.kind === "integer" && expression.constraints.length === 0) return false;
46
+ if (expression.kind === "boolean") return false;
47
+ if (expression.kind === "nullable" || expression.kind === "default") return false;
48
+ return true;
49
+ }
50
+
51
+ export function internAnonymousValues(input: AnonymousValueInterningInput): AnonymousValueInterning {
52
+ const typeById = new Map<string, LawAnonymousValueTypeNode>(input.types.map((node) => [node.entity.id as string, node] as const));
53
+ const fieldsByOwner = new Map<string, LawAnonymousValueFieldNode[]>();
54
+ for (const field of input.fields) {
55
+ const owned = fieldsByOwner.get(field.owner.id) ?? [];
56
+ owned.push(field);
57
+ fieldsByOwner.set(field.owner.id, owned);
58
+ }
59
+ const variantsByOwner = new Map<string, LawAnonymousValueVariantNode[]>();
60
+ for (const variant of input.variants) {
61
+ const owned = variantsByOwner.get(variant.owner.id) ?? [];
62
+ owned.push(variant);
63
+ variantsByOwner.set(variant.owner.id, owned);
64
+ }
65
+
66
+ const memo = new Map<string, string | null>();
67
+ const semanticTypeKey = (id: string, active = new Set<string>()): string | undefined => {
68
+ const memoized = memo.get(id);
69
+ if (memoized !== undefined) return memoized ?? undefined;
70
+ if (active.has(id)) {
71
+ memo.set(id, null);
72
+ return undefined;
73
+ }
74
+ const node = typeById.get(id);
75
+ if (node === undefined) return undefined;
76
+ active.add(id);
77
+ const childKey = (childId: string): string | undefined => semanticTypeKey(childId, active);
78
+ let key: string | undefined;
79
+ switch (node.expression.kind) {
80
+ case "string":
81
+ case "number":
82
+ case "integer":
83
+ case "boolean":
84
+ case "null":
85
+ case "unknown":
86
+ case "enum":
87
+ case "literal":
88
+ key = JSON.stringify(node.expression);
89
+ break;
90
+ case "list": {
91
+ const child = childKey(node.expression.element.id);
92
+ if (child !== undefined) key = JSON.stringify(["list", child]);
93
+ break;
94
+ }
95
+ case "map": {
96
+ const child = childKey(node.expression.value.id);
97
+ if (child !== undefined) key = JSON.stringify(["map", child]);
98
+ break;
99
+ }
100
+ case "nullable":
101
+ case "default": {
102
+ // Wrappers do not own prims, but their meaning must survive structural
103
+ // interning. A nullable field is not the same contract as a non-null field.
104
+ const child = childKey(node.expression.value.id);
105
+ if (child !== undefined) key = JSON.stringify(node.expression.kind === "nullable"
106
+ ? ["nullable", child]
107
+ : ["default", child, node.expression.defaultValue]);
108
+ break;
109
+ }
110
+ case "record": {
111
+ const members: Array<readonly [string, boolean, string]> = [];
112
+ for (const field of fieldsByOwner.get(id) ?? []) {
113
+ const child = childKey(field.value.id);
114
+ if (child === undefined) break;
115
+ members.push([field.name, field.required, child]);
116
+ }
117
+ if (members.length === (fieldsByOwner.get(id)?.length ?? 0)) {
118
+ members.sort(([left], [right]) => left.localeCompare(right));
119
+ key = JSON.stringify(["record", members,
120
+ node.expression.semantic?.kind === "value-object" ? node.expression.semantic.geometry : undefined]);
121
+ }
122
+ break;
123
+ }
124
+ case "sum": {
125
+ const members: Array<readonly [string, string]> = [];
126
+ for (const variant of variantsByOwner.get(id) ?? []) {
127
+ const child = childKey(variant.value.id);
128
+ if (child === undefined) break;
129
+ // A union arm's discriminator is part of its wire contract. Two sums
130
+ // with the same carried value shapes but different arm labels cannot
131
+ // share one USDA prim: authoring reconstructs the nominal oneof from
132
+ // these labels before it decodes the generated Protobuf call.
133
+ members.push([variant.label ?? variant.entity.label, child]);
134
+ }
135
+ if (members.length === (variantsByOwner.get(id)?.length ?? 0)) {
136
+ members.sort(([leftLabel, leftChild], [rightLabel, rightChild]) =>
137
+ leftLabel.localeCompare(rightLabel) || leftChild.localeCompare(rightChild));
138
+ key = JSON.stringify(["sum", members]);
139
+ }
140
+ break;
141
+ }
142
+ case "recursive":
143
+ key = undefined;
144
+ break;
145
+ }
146
+ active.delete(id);
147
+ memo.set(id, key ?? null);
148
+ return key;
149
+ };
150
+
151
+ const canonicalTypeBySemanticKey = new Map<string, LawAnonymousValueTypeNode>();
152
+ const canonicalTypeIdById = new Map<string, string>();
153
+ for (const type of [...input.types].sort((left, right) => left.entity.sid.localeCompare(right.entity.sid))) {
154
+ if (!input.ownsPrim(type)) continue;
155
+ const key = semanticTypeKey(type.entity.id);
156
+ if (key === undefined) continue;
157
+ const canonical = canonicalTypeBySemanticKey.get(key);
158
+ if (canonical === undefined) {
159
+ canonicalTypeBySemanticKey.set(key, type);
160
+ canonicalTypeIdById.set(type.entity.id, type.entity.id);
161
+ continue;
162
+ }
163
+ canonicalTypeIdById.set(type.entity.id, canonical.entity.id);
164
+ }
165
+
166
+ // When two record/sum types share one canonical prim, their nested structural references must
167
+ // share the canonical child paths too. This preserves effects which target a nested payload field;
168
+ // no dangling path is allowed merely because its source owner was the non-canonical occurrence.
169
+ const canonicalFieldIdById = new Map<string, string>();
170
+ const canonicalVariantIdById = new Map<string, string>();
171
+ for (const [duplicateId, canonicalId] of canonicalTypeIdById) {
172
+ if (duplicateId === canonicalId) continue;
173
+ const canonicalFields = new Map<string, LawAnonymousValueFieldNode>();
174
+ for (const field of fieldsByOwner.get(canonicalId) ?? []) {
175
+ const child = semanticTypeKey(field.value.id);
176
+ if (child !== undefined) canonicalFields.set(JSON.stringify([field.name, field.required, child]), field);
177
+ }
178
+ for (const field of fieldsByOwner.get(duplicateId) ?? []) {
179
+ const child = semanticTypeKey(field.value.id);
180
+ const canonical = child === undefined
181
+ ? undefined
182
+ : canonicalFields.get(JSON.stringify([field.name, field.required, child]));
183
+ if (canonical !== undefined) canonicalFieldIdById.set(field.entity.id, canonical.entity.id);
184
+ }
185
+
186
+ const canonicalVariants = new Map<string, LawAnonymousValueVariantNode[]>();
187
+ for (const variant of variantsByOwner.get(canonicalId) ?? []) {
188
+ const child = semanticTypeKey(variant.value.id);
189
+ if (child === undefined) continue;
190
+ const key = JSON.stringify([variant.label ?? variant.entity.label, child]);
191
+ const matches = canonicalVariants.get(key) ?? [];
192
+ matches.push(variant);
193
+ canonicalVariants.set(key, matches);
194
+ }
195
+ for (const variant of variantsByOwner.get(duplicateId) ?? []) {
196
+ const child = semanticTypeKey(variant.value.id);
197
+ if (child === undefined) continue;
198
+ const key = JSON.stringify([variant.label ?? variant.entity.label, child]);
199
+ const canonical = canonicalVariants.get(key)?.shift();
200
+ if (canonical !== undefined) canonicalVariantIdById.set(variant.entity.id, canonical.entity.id);
201
+ }
202
+ }
203
+
204
+ return { canonicalTypeIdById, canonicalFieldIdById, canonicalVariantIdById, semanticTypeKey: (id) => semanticTypeKey(id) };
205
+ }
@@ -0,0 +1,27 @@
1
+ // NOMOS — The released-baseline surface `githolon baseline` and CI load (`@githolon/dsl/baseline`).
2
+ //
3
+ // One import for the three halves of CAP-660: the artifact (baseline_usda.ts), its export from
4
+ // verified units (baseline_export.ts) and its verification/import against a ledger (baseline_import.ts).
5
+ export {
6
+ BASELINE_NO_RELEASE,
7
+ NOMOS_BASELINE_FILENAME,
8
+ NOMOS_BASELINE_FORMAT,
9
+ baselineSha256,
10
+ isBaselineUsda,
11
+ parseBaselineUsda,
12
+ serializeBaselineUsda,
13
+ type BaselineSource,
14
+ type BaselineWireAllocation,
15
+ type ReleasedBaseline,
16
+ } from "./baseline_usda.js";
17
+ export { exportBaselineFromUnits, type BaselineExport, type BaselineExportInput } from "./baseline_export.js";
18
+ export {
19
+ BaselineRefusal,
20
+ assertBaselineRetained,
21
+ importBaseline,
22
+ verifyBaselinePin,
23
+ type BaselineImport,
24
+ type BaselinePin,
25
+ type BaselineRefusalCode,
26
+ } from "./baseline_import.js";
27
+ export { lawUnitDirectoryName } from "./law_units.js";