@objectstack/objectql 4.2.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ __export(index_exports, {
31
31
  RESERVED_NAMESPACES: () => RESERVED_NAMESPACES,
32
32
  SchemaRegistry: () => SchemaRegistry,
33
33
  ScopedContext: () => ScopedContext,
34
+ SysMetadataRepository: () => SysMetadataRepository,
34
35
  ValidationError: () => ValidationError,
35
36
  applyInMemoryAggregation: () => applyInMemoryAggregation,
36
37
  applySystemFields: () => applySystemFields,
@@ -628,6 +629,34 @@ var SchemaRegistry = class {
628
629
  // ==========================================
629
630
  // Reset (for testing)
630
631
  // ==========================================
632
+ /**
633
+ * Invalidate the merged-schema cache for a single FQN (or short name).
634
+ *
635
+ * Call this from event-driven consumers (ADR-0008 M0 PR-7) when an
636
+ * upstream metadata change makes the cached merged definition stale.
637
+ * The contributor list is preserved — only the cached merge result is
638
+ * dropped, so the next `resolveObject(fqn)` recomputes from scratch.
639
+ *
640
+ * Accepts either an FQN (`acme__contact`) or a bare short name
641
+ * (`contact`); for the latter, all entries whose suffix matches the
642
+ * name are invalidated.
643
+ */
644
+ invalidate(fqnOrName) {
645
+ if (this.mergedObjectCache.has(fqnOrName)) {
646
+ this.mergedObjectCache.delete(fqnOrName);
647
+ return;
648
+ }
649
+ const suffix = `__${fqnOrName}`;
650
+ for (const fqn of Array.from(this.mergedObjectCache.keys())) {
651
+ if (fqn === fqnOrName || fqn.endsWith(suffix)) {
652
+ this.mergedObjectCache.delete(fqn);
653
+ }
654
+ }
655
+ }
656
+ /** Drop every entry from the merged-schema cache. */
657
+ invalidateAll() {
658
+ this.mergedObjectCache.clear();
659
+ }
631
660
  /**
632
661
  * Clear all registry state. Use only for testing.
633
662
  */
@@ -640,11 +669,487 @@ var SchemaRegistry = class {
640
669
  }
641
670
  };
642
671
 
672
+ // src/sys-metadata-repository.ts
673
+ var import_metadata_core = require("@objectstack/metadata-core");
674
+ var import_kernel2 = require("@objectstack/spec/kernel");
675
+ var OVERLAY_ALLOWED_TYPES = new Set(
676
+ import_kernel2.DEFAULT_METADATA_TYPE_REGISTRY.filter((e) => e.allowOrgOverride).map((e) => e.type)
677
+ );
678
+ var SysMetadataRepository = class {
679
+ constructor(opts) {
680
+ /**
681
+ * Local seq counter for in-memory watch() event broadcasts. Mirrors
682
+ * the durable `event_seq` we write into `sys_metadata_history` on
683
+ * each successful put/delete — assigned AFTER the transaction commits
684
+ * so we never broadcast events that got rolled back.
685
+ */
686
+ this.seqCounter = 0;
687
+ this.watchers = /* @__PURE__ */ new Set();
688
+ this.closed = false;
689
+ /** Table name for the durable event log. */
690
+ this.historyTable = "sys_metadata_history";
691
+ this.engine = opts.engine;
692
+ this.organizationId = opts.organizationId ?? null;
693
+ this.orgLabel = opts.orgLabel ?? (opts.organizationId ?? "system");
694
+ }
695
+ /**
696
+ * Run `cb` inside `engine.transaction(...)` if the engine supports it,
697
+ * otherwise fall through to a direct call. Matches the real
698
+ * `ObjectQL.transaction` semantics — in-memory drivers (and our test
699
+ * fakes) get no rollback, which is acceptable because production
700
+ * always runs on a SQL driver with real ACID.
701
+ */
702
+ async withTxn(cb) {
703
+ if (typeof this.engine.transaction === "function") {
704
+ return this.engine.transaction(cb);
705
+ }
706
+ return cb(void 0);
707
+ }
708
+ /**
709
+ * Read the current overlay row. Returns null if no row exists —
710
+ * callers (e.g. LayeredRepository) fall through to lower layers.
711
+ */
712
+ async get(ref) {
713
+ this.assertOpen();
714
+ const row = await this.engine.findOne("sys_metadata", {
715
+ where: this.whereFor(ref)
716
+ });
717
+ if (!row) return null;
718
+ return this.rowToItem(ref, row);
719
+ }
720
+ async put(ref, spec, opts) {
721
+ this.assertOpen();
722
+ this.assertAllowed(ref.type);
723
+ const body = spec ?? {};
724
+ const hash = (0, import_metadata_core.hashSpec)(body);
725
+ const result = await this.withTxn(async (ctx) => {
726
+ const existing = await this.engine.findOne("sys_metadata", {
727
+ where: this.whereFor(ref),
728
+ context: ctx
729
+ });
730
+ const existingHash = existing?.checksum ?? null;
731
+ if (opts.parentVersion !== existingHash) {
732
+ throw new import_metadata_core.ConflictError(this.fullRef(ref), opts.parentVersion, existingHash);
733
+ }
734
+ if (existing && existingHash === hash) {
735
+ const item2 = this.rowToItem(ref, existing);
736
+ return { skipped: true, version: hash, seq: item2.seq, item: item2 };
737
+ }
738
+ const now = (/* @__PURE__ */ new Date()).toISOString();
739
+ const op = existing ? "update" : "create";
740
+ const version = await this.nextItemVersion(ref, ctx);
741
+ const eventSeq = await this.nextEventSeq(ctx);
742
+ const parentRowData = {
743
+ type: ref.type,
744
+ name: ref.name,
745
+ organization_id: this.organizationId,
746
+ metadata: JSON.stringify(body),
747
+ checksum: hash,
748
+ state: "active",
749
+ version,
750
+ updated_at: now
751
+ };
752
+ let parentId;
753
+ if (existing) {
754
+ const existingId = existing.id;
755
+ if (existingId === void 0) {
756
+ throw new Error(
757
+ `SysMetadataRepository.put: existing row for ${ref.type}/${ref.name} has no id column`
758
+ );
759
+ }
760
+ parentId = existingId;
761
+ await this.engine.update("sys_metadata", parentRowData, {
762
+ where: { id: existingId },
763
+ context: ctx
764
+ });
765
+ } else {
766
+ parentRowData.created_at = now;
767
+ const inserted = await this.engine.insert("sys_metadata", parentRowData, { context: ctx });
768
+ parentId = inserted.id;
769
+ }
770
+ await this.engine.insert(
771
+ this.historyTable,
772
+ {
773
+ id: this.uuid(),
774
+ event_seq: eventSeq,
775
+ metadata_id: parentId,
776
+ type: ref.type,
777
+ name: ref.name,
778
+ version,
779
+ operation_type: op,
780
+ metadata: JSON.stringify(body),
781
+ checksum: hash,
782
+ previous_checksum: existingHash,
783
+ change_note: opts.message,
784
+ source: opts.source ?? "sys-metadata-repo",
785
+ organization_id: this.organizationId,
786
+ recorded_by: opts.actor,
787
+ recorded_at: now
788
+ },
789
+ { context: ctx }
790
+ );
791
+ const item = {
792
+ ref: this.fullRef(ref),
793
+ body,
794
+ hash,
795
+ parentHash: existingHash,
796
+ authoredBy: opts.actor,
797
+ authoredAt: now,
798
+ message: opts.message,
799
+ seq: eventSeq
800
+ };
801
+ return {
802
+ skipped: false,
803
+ version: hash,
804
+ seq: eventSeq,
805
+ item,
806
+ op,
807
+ existingHash,
808
+ now,
809
+ source: opts.source ?? "sys-metadata-repo",
810
+ message: opts.message,
811
+ actor: opts.actor
812
+ };
813
+ });
814
+ if (result.skipped) {
815
+ return { version: result.version, seq: result.seq, item: result.item };
816
+ }
817
+ this.seqCounter = result.seq;
818
+ this.broadcast({
819
+ seq: result.seq,
820
+ op: result.op,
821
+ ref: this.fullRef(ref),
822
+ hash: result.version,
823
+ parentHash: result.existingHash,
824
+ actor: result.actor,
825
+ message: result.message,
826
+ ts: result.now,
827
+ source: result.source
828
+ });
829
+ return { version: result.version, seq: result.seq, item: result.item };
830
+ }
831
+ async delete(ref, opts) {
832
+ this.assertOpen();
833
+ this.assertAllowed(ref.type);
834
+ const result = await this.withTxn(async (ctx) => {
835
+ const existing = await this.engine.findOne("sys_metadata", {
836
+ where: this.whereFor(ref),
837
+ context: ctx
838
+ });
839
+ if (!existing) {
840
+ throw new import_metadata_core.ConflictError(this.fullRef(ref), opts.parentVersion, null);
841
+ }
842
+ const existingHash = existing.checksum ?? null;
843
+ if (opts.parentVersion !== existingHash) {
844
+ throw new import_metadata_core.ConflictError(this.fullRef(ref), opts.parentVersion, existingHash);
845
+ }
846
+ const existingId = existing.id;
847
+ if (existingId === void 0) {
848
+ throw new Error(
849
+ `SysMetadataRepository.delete: existing row for ${ref.type}/${ref.name} has no id column`
850
+ );
851
+ }
852
+ const now = (/* @__PURE__ */ new Date()).toISOString();
853
+ const version = await this.nextItemVersion(ref, ctx);
854
+ const eventSeq = await this.nextEventSeq(ctx);
855
+ await this.engine.delete("sys_metadata", {
856
+ where: { id: existingId },
857
+ context: ctx
858
+ });
859
+ await this.engine.insert(
860
+ this.historyTable,
861
+ {
862
+ id: this.uuid(),
863
+ event_seq: eventSeq,
864
+ metadata_id: existingId,
865
+ type: ref.type,
866
+ name: ref.name,
867
+ version,
868
+ operation_type: "delete",
869
+ metadata: null,
870
+ checksum: null,
871
+ previous_checksum: existingHash,
872
+ change_note: opts.message,
873
+ source: opts.source ?? "sys-metadata-repo",
874
+ organization_id: this.organizationId,
875
+ recorded_by: opts.actor,
876
+ recorded_at: now
877
+ },
878
+ { context: ctx }
879
+ );
880
+ return {
881
+ eventSeq,
882
+ existingHash,
883
+ now,
884
+ source: opts.source ?? "sys-metadata-repo",
885
+ message: opts.message,
886
+ actor: opts.actor
887
+ };
888
+ });
889
+ this.seqCounter = result.eventSeq;
890
+ this.broadcast({
891
+ seq: result.eventSeq,
892
+ op: "delete",
893
+ ref: this.fullRef(ref),
894
+ hash: null,
895
+ parentHash: result.existingHash,
896
+ actor: result.actor,
897
+ message: result.message,
898
+ ts: result.now,
899
+ source: result.source
900
+ });
901
+ return { seq: result.eventSeq };
902
+ }
903
+ async *list(filter) {
904
+ this.assertOpen();
905
+ const where = {
906
+ organization_id: this.organizationId,
907
+ state: "active"
908
+ };
909
+ if (filter.type) where.type = filter.type;
910
+ const rows = await this.engine.find("sys_metadata", {
911
+ where,
912
+ limit: filter.limit
913
+ });
914
+ for (const row of rows) {
915
+ if (filter.nameContains && !String(row.name).includes(filter.nameContains)) continue;
916
+ const item = this.rowToItem(
917
+ { ...this.fullRef({ type: row.type, name: row.name }) },
918
+ row
919
+ );
920
+ const { body, ...header } = item;
921
+ yield header;
922
+ }
923
+ }
924
+ /**
925
+ * Yield every history event for `(org, type?, name?)` from the
926
+ * durable log, ordered by per-(type,name) `version` ascending. When
927
+ * `filter.type`/`filter.name` are unset the consumer gets the full
928
+ * org-scoped event stream — still ordered by version within each
929
+ * (type,name) bucket, then by `recorded_at` across buckets (we sort
930
+ * client-side because the test engine doesn't honor `orderBy`).
931
+ */
932
+ async *history(ref, opts) {
933
+ this.assertOpen();
934
+ const full = this.fullRef(ref);
935
+ const where = {
936
+ organization_id: this.organizationId,
937
+ type: full.type,
938
+ name: full.name
939
+ };
940
+ const rows = await this.engine.find(this.historyTable, { where });
941
+ rows.sort((a, b) => {
942
+ const va = typeof a.event_seq === "number" ? a.event_seq : 0;
943
+ const vb = typeof b.event_seq === "number" ? b.event_seq : 0;
944
+ return va - vb;
945
+ });
946
+ let yielded = 0;
947
+ for (const row of rows) {
948
+ if (opts?.sinceSeq !== void 0 && (row.event_seq ?? 0) <= opts.sinceSeq) continue;
949
+ if (opts?.limit !== void 0 && yielded >= opts.limit) break;
950
+ yielded++;
951
+ yield {
952
+ seq: row.event_seq ?? 0,
953
+ op: row.operation_type ?? "update",
954
+ ref: full,
955
+ hash: row.checksum ?? null,
956
+ parentHash: row.previous_checksum ?? null,
957
+ actor: row.recorded_by ?? "unknown",
958
+ message: row.change_note ?? void 0,
959
+ ts: row.recorded_at ?? (/* @__PURE__ */ new Date(0)).toISOString(),
960
+ source: row.source ?? "sys-metadata-repo"
961
+ };
962
+ }
963
+ }
964
+ /**
965
+ * Live event stream. Fires for every successful put/delete on THIS
966
+ * instance — cross-replica fan-out is M1. Manual AsyncIterator (not
967
+ * an async generator) so we can deterministically tear down via
968
+ * `iter.return()`, matching the pattern used by InMemoryRepository.
969
+ */
970
+ watch(filter, since) {
971
+ const self = this;
972
+ return {
973
+ [Symbol.asyncIterator]: () => {
974
+ const queue = [];
975
+ let pendingResolve = null;
976
+ let stopped = false;
977
+ const dispatch = (evt) => {
978
+ if (stopped) return;
979
+ if (!self.matchesFilter(evt, filter)) return;
980
+ if (since !== void 0 && evt.seq <= since) return;
981
+ if (pendingResolve) {
982
+ const r = pendingResolve;
983
+ pendingResolve = null;
984
+ r({ value: evt, done: false });
985
+ } else {
986
+ queue.push(evt);
987
+ }
988
+ };
989
+ self.watchers.add(dispatch);
990
+ return {
991
+ next() {
992
+ if (stopped) return Promise.resolve({ value: void 0, done: true });
993
+ const buffered = queue.shift();
994
+ if (buffered) return Promise.resolve({ value: buffered, done: false });
995
+ return new Promise((resolve) => {
996
+ pendingResolve = resolve;
997
+ });
998
+ },
999
+ return() {
1000
+ stopped = true;
1001
+ self.watchers.delete(dispatch);
1002
+ if (pendingResolve) {
1003
+ const r = pendingResolve;
1004
+ pendingResolve = null;
1005
+ r({ value: void 0, done: true });
1006
+ }
1007
+ return Promise.resolve({ value: void 0, done: true });
1008
+ }
1009
+ };
1010
+ }
1011
+ };
1012
+ }
1013
+ /** Shut down all watch iterators. */
1014
+ close() {
1015
+ this.closed = true;
1016
+ const snapshot = Array.from(this.watchers);
1017
+ for (const w of snapshot) {
1018
+ try {
1019
+ w({
1020
+ seq: -1,
1021
+ op: "delete",
1022
+ ref: { org: "", type: "view", name: "_close" },
1023
+ hash: null,
1024
+ parentHash: null,
1025
+ actor: "system",
1026
+ ts: (/* @__PURE__ */ new Date()).toISOString(),
1027
+ source: "sys-metadata-repo-close"
1028
+ });
1029
+ } catch {
1030
+ }
1031
+ }
1032
+ this.watchers.clear();
1033
+ }
1034
+ // ── helpers ─────────────────────────────────────────────────────────
1035
+ assertOpen() {
1036
+ if (this.closed) throw new Error("SysMetadataRepository is closed");
1037
+ }
1038
+ assertAllowed(type) {
1039
+ if (!OVERLAY_ALLOWED_TYPES.has(type)) {
1040
+ const err = new Error(
1041
+ `[not_overridable] '${type}' is not allowOrgOverride in the registry. Allowed: ${Array.from(OVERLAY_ALLOWED_TYPES).join(", ")}.`
1042
+ );
1043
+ err.code = "not_overridable";
1044
+ err.status = 403;
1045
+ throw err;
1046
+ }
1047
+ }
1048
+ whereFor(ref) {
1049
+ return {
1050
+ type: ref.type,
1051
+ name: ref.name,
1052
+ organization_id: this.organizationId,
1053
+ state: "active"
1054
+ };
1055
+ }
1056
+ fullRef(ref) {
1057
+ return {
1058
+ org: this.orgLabel,
1059
+ type: ref.type,
1060
+ name: ref.name
1061
+ };
1062
+ }
1063
+ rowToItem(ref, row) {
1064
+ const body = typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata ?? {};
1065
+ const hash = row.checksum ?? (0, import_metadata_core.hashSpec)(body);
1066
+ return {
1067
+ ref: this.fullRef(ref),
1068
+ body,
1069
+ hash,
1070
+ parentHash: null,
1071
+ authoredBy: row.updated_by ?? row.created_by ?? "unknown",
1072
+ authoredAt: row.updated_at ?? row.created_at ?? (/* @__PURE__ */ new Date()).toISOString(),
1073
+ message: void 0,
1074
+ seq: this.seqCounter
1075
+ };
1076
+ }
1077
+ broadcast(evt) {
1078
+ for (const w of Array.from(this.watchers)) {
1079
+ try {
1080
+ w(evt);
1081
+ } catch {
1082
+ }
1083
+ }
1084
+ }
1085
+ matchesFilter(evt, filter) {
1086
+ if (filter.type && evt.ref.type !== filter.type) return false;
1087
+ if (filter.name && evt.ref.name !== filter.name) return false;
1088
+ if (filter.org && evt.ref.org !== filter.org) return false;
1089
+ return true;
1090
+ }
1091
+ /**
1092
+ * Per-org monotonic event sequence. Reads `MAX(event_seq) + 1` from
1093
+ * `sys_metadata_history` scoped by `organization_id`. MUST be called
1094
+ * inside a transaction (the only caller is the put/delete txn body) —
1095
+ * concurrent writers in the same org race otherwise.
1096
+ */
1097
+ async nextEventSeq(ctx) {
1098
+ try {
1099
+ const rows = await this.engine.find(this.historyTable, {
1100
+ where: { organization_id: this.organizationId },
1101
+ context: ctx
1102
+ });
1103
+ let max = 0;
1104
+ for (const row of rows) {
1105
+ const v = typeof row.event_seq === "number" ? row.event_seq : 0;
1106
+ if (v > max) max = v;
1107
+ }
1108
+ return max + 1;
1109
+ } catch {
1110
+ return 1;
1111
+ }
1112
+ }
1113
+ /**
1114
+ * Per-(org,type,name) lineage counter. Reads from history (not from
1115
+ * `sys_metadata.version`) so delete + recreate continues incrementing
1116
+ * instead of restarting at 1.
1117
+ */
1118
+ async nextItemVersion(ref, ctx) {
1119
+ try {
1120
+ const rows = await this.engine.find(this.historyTable, {
1121
+ where: {
1122
+ organization_id: this.organizationId,
1123
+ type: ref.type,
1124
+ name: ref.name
1125
+ },
1126
+ context: ctx
1127
+ });
1128
+ let max = 0;
1129
+ for (const row of rows) {
1130
+ const v = typeof row.version === "number" ? row.version : 0;
1131
+ if (v > max) max = v;
1132
+ }
1133
+ return max + 1;
1134
+ } catch {
1135
+ return 1;
1136
+ }
1137
+ }
1138
+ /** Lightweight UUID-ish id for history rows; sufficient for an audit log. */
1139
+ uuid() {
1140
+ if (typeof globalThis.crypto?.randomUUID === "function") {
1141
+ return globalThis.crypto.randomUUID();
1142
+ }
1143
+ return `evt_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
1144
+ }
1145
+ };
1146
+
643
1147
  // src/protocol.ts
1148
+ var import_metadata_core2 = require("@objectstack/metadata-core");
644
1149
  var import_data2 = require("@objectstack/spec/data");
645
1150
  var import_shared = require("@objectstack/spec/shared");
646
1151
  var import_ui2 = require("@objectstack/spec/ui");
647
- var import_kernel2 = require("@objectstack/spec/kernel");
1152
+ var import_kernel3 = require("@objectstack/spec/kernel");
648
1153
  var FORM_VIEW_TYPES = /* @__PURE__ */ new Set(["simple", "tabbed", "wizard", "split", "drawer", "modal"]);
649
1154
  function resolveOverlaySchema(type, item) {
650
1155
  const singular = import_shared.PLURAL_TO_SINGULAR[type] ?? type;
@@ -706,6 +1211,13 @@ var SERVICE_CONFIG = {
706
1211
  };
707
1212
  var _ObjectStackProtocolImplementation = class _ObjectStackProtocolImplementation {
708
1213
  constructor(engine, getServicesRegistry, getFeedService, projectId) {
1214
+ /**
1215
+ * Lazily-instantiated SysMetadataRepository per organization. Keyed by
1216
+ * `${organizationId ?? '__env__'}`. Repositories are stateful — they
1217
+ * carry the per-org `seqCounter` and watch subscribers — so we cache
1218
+ * them rather than constructing one per call.
1219
+ */
1220
+ this.overlayRepos = /* @__PURE__ */ new Map();
709
1221
  /**
710
1222
  * One-time guard for ensuring the overlay-uniqueness UNIQUE INDEX exists
711
1223
  * on `sys_metadata`. ADR-0005: scopes overlays by
@@ -721,6 +1233,24 @@ var _ObjectStackProtocolImplementation = class _ObjectStackProtocolImplementatio
721
1233
  this.getFeedService = getFeedService;
722
1234
  this.projectId = projectId;
723
1235
  }
1236
+ /**
1237
+ * Lazily obtain a SysMetadataRepository for the given organization.
1238
+ * Env-wide overlays (organizationId == null) share a singleton under
1239
+ * the `__env__` key.
1240
+ */
1241
+ getOverlayRepo(organizationId) {
1242
+ const key = organizationId ?? "__env__";
1243
+ let repo = this.overlayRepos.get(key);
1244
+ if (!repo) {
1245
+ repo = new SysMetadataRepository({
1246
+ engine: this.engine,
1247
+ organizationId,
1248
+ orgLabel: organizationId ?? "env"
1249
+ });
1250
+ this.overlayRepos.set(key, repo);
1251
+ }
1252
+ return repo;
1253
+ }
724
1254
  async ensureOverlayIndex() {
725
1255
  if (this.overlayIndexEnsured) return;
726
1256
  this.overlayIndexEnsured = true;
@@ -1010,23 +1540,34 @@ var _ObjectStackProtocolImplementation = class _ObjectStackProtocolImplementatio
1010
1540
  }
1011
1541
  } catch {
1012
1542
  }
1013
- if (item === void 0) {
1014
- item = this.engine.registry.getItem(request.type, request.name);
1015
- if (item === void 0) {
1016
- const alt = import_shared.PLURAL_TO_SINGULAR[request.type] ?? import_shared.SINGULAR_TO_PLURAL[request.type];
1017
- if (alt) item = this.engine.registry.getItem(alt, request.name);
1018
- }
1019
- }
1020
1543
  if (item === void 0) {
1021
1544
  try {
1022
1545
  const services = this.getServicesRegistry?.();
1023
1546
  const metadataService = services?.get("metadata");
1024
1547
  if (metadataService && typeof metadataService.get === "function") {
1025
- item = await metadataService.get(request.type, request.name);
1548
+ const fromService = await metadataService.get(request.type, request.name);
1549
+ if (fromService !== void 0 && fromService !== null) {
1550
+ item = fromService;
1551
+ } else {
1552
+ const alt = import_shared.PLURAL_TO_SINGULAR[request.type] ?? import_shared.SINGULAR_TO_PLURAL[request.type];
1553
+ if (alt) {
1554
+ const altFromService = await metadataService.get(alt, request.name);
1555
+ if (altFromService !== void 0 && altFromService !== null) {
1556
+ item = altFromService;
1557
+ }
1558
+ }
1559
+ }
1026
1560
  }
1027
1561
  } catch {
1028
1562
  }
1029
1563
  }
1564
+ if (item === void 0) {
1565
+ item = this.engine.registry.getItem(request.type, request.name);
1566
+ if (item === void 0) {
1567
+ const alt = import_shared.PLURAL_TO_SINGULAR[request.type] ?? import_shared.SINGULAR_TO_PLURAL[request.type];
1568
+ if (alt) item = this.engine.registry.getItem(alt, request.name);
1569
+ }
1570
+ }
1030
1571
  return {
1031
1572
  type: request.type,
1032
1573
  name: request.name,
@@ -1909,6 +2450,27 @@ var _ObjectStackProtocolImplementation = class _ObjectStackProtocolImplementatio
1909
2450
  const singular = import_shared.PLURAL_TO_SINGULAR[type] ?? type;
1910
2451
  return _ObjectStackProtocolImplementation.OVERLAY_ALLOWED_TYPES.has(singular) || _ObjectStackProtocolImplementation.OVERLAY_ALLOWED_TYPES.has(type);
1911
2452
  }
2453
+ /**
2454
+ * Mirror an object-type overlay write into the in-memory engine
2455
+ * registry so subsequent CRUD finds the new schema. Idempotent and
2456
+ * safe to call after a successful persistence call. For the legacy
2457
+ * write path this is invoked BEFORE persistence (historical behavior
2458
+ * preserved); for the PR-10d.3 repository path it is invoked only
2459
+ * AFTER `put()` resolves successfully, so a failed write — DB error,
2460
+ * optimistic-lock conflict, validation failure — never leaks a
2461
+ * stale schema into the registry.
2462
+ */
2463
+ applyObjectRegistryMutation(request) {
2464
+ if (request.type !== "object" && request.type !== "objects") return;
2465
+ this.engine.registry.registerItem(request.type, request.item, "name");
2466
+ try {
2467
+ this.engine.registry.registerObject(request.item, "sys_metadata");
2468
+ } catch (err) {
2469
+ console.warn(
2470
+ `[Protocol] registerObject failed for ${request.name}: ${err?.message ?? err}`
2471
+ );
2472
+ }
2473
+ }
1912
2474
  async saveMetaItem(request) {
1913
2475
  if (!request.item) {
1914
2476
  throw new Error("Item data is required");
@@ -1943,17 +2505,51 @@ var _ObjectStackProtocolImplementation = class _ObjectStackProtocolImplementatio
1943
2505
  }
1944
2506
  }
1945
2507
  }
1946
- if (request.type === "object" || request.type === "objects") {
1947
- this.engine.registry.registerItem(request.type, request.item, "name");
2508
+ await this.ensureOverlayIndex();
2509
+ const singularTypeForRepo = import_shared.PLURAL_TO_SINGULAR[request.type] ?? request.type;
2510
+ if (_ObjectStackProtocolImplementation.isOverlayAllowed(singularTypeForRepo)) {
2511
+ const orgId = request.organizationId ?? null;
2512
+ const repo = this.getOverlayRepo(orgId);
2513
+ const ref = {
2514
+ type: singularTypeForRepo,
2515
+ name: request.name,
2516
+ org: orgId ?? "env"
2517
+ };
2518
+ let parentVersion;
2519
+ if (request.parentVersion !== void 0) {
2520
+ parentVersion = request.parentVersion;
2521
+ } else {
2522
+ const current = await repo.get(ref);
2523
+ parentVersion = current?.hash ?? null;
2524
+ }
1948
2525
  try {
1949
- this.engine.registry.registerObject(request.item, "sys_metadata");
2526
+ const result = await repo.put(ref, request.item, {
2527
+ parentVersion,
2528
+ actor: request.actor ?? "system",
2529
+ source: "protocol.saveMetaItem"
2530
+ });
2531
+ this.applyObjectRegistryMutation(request);
2532
+ return {
2533
+ success: true,
2534
+ version: result.version,
2535
+ seq: result.seq,
2536
+ message: orgId ? `Saved customization overlay (org=${orgId}) \u2014 type=${request.type}, name=${request.name} [seq=${result.seq}]` : `Saved customization overlay (env-wide) \u2014 type=${request.type}, name=${request.name} [seq=${result.seq}]`
2537
+ };
1950
2538
  } catch (err) {
1951
- console.warn(
1952
- `[Protocol] registerObject failed for ${request.name}: ${err?.message ?? err}`
1953
- );
2539
+ if (err instanceof import_metadata_core2.ConflictError) {
2540
+ const conflict = new Error(
2541
+ `[metadata_conflict] ${request.type}/${request.name} has been modified since you loaded it. Expected parent ${err.expectedParent ?? "null"} but current is ${err.actualHead ?? "null"}.`
2542
+ );
2543
+ conflict.code = "metadata_conflict";
2544
+ conflict.status = 409;
2545
+ conflict.expectedParent = err.expectedParent;
2546
+ conflict.actualHead = err.actualHead;
2547
+ throw conflict;
2548
+ }
2549
+ throw err;
1954
2550
  }
1955
2551
  }
1956
- await this.ensureOverlayIndex();
2552
+ this.applyObjectRegistryMutation(request);
1957
2553
  try {
1958
2554
  const now = (/* @__PURE__ */ new Date()).toISOString();
1959
2555
  const orgId = request.organizationId ?? null;
@@ -2010,6 +2606,35 @@ var _ObjectStackProtocolImplementation = class _ObjectStackProtocolImplementatio
2010
2606
  throw err;
2011
2607
  }
2012
2608
  }
2609
+ /**
2610
+ * Yield the durable change-log for a single metadata item — every
2611
+ * put/delete recorded in `sys_metadata_history` for `(org, type, name)`,
2612
+ * in event_seq order. Powers the Studio "History" tab and any
2613
+ * client-side audit timeline.
2614
+ *
2615
+ * Returns `[]` for non-overlay-allowed types (the legacy raw-engine
2616
+ * path doesn't record history) instead of throwing — callers can treat
2617
+ * "no history" uniformly.
2618
+ */
2619
+ async historyMetaItem(request) {
2620
+ const singularType = import_shared.PLURAL_TO_SINGULAR[request.type] ?? request.type;
2621
+ if (!_ObjectStackProtocolImplementation.isOverlayAllowed(singularType)) {
2622
+ return { events: [] };
2623
+ }
2624
+ const orgId = request.organizationId ?? null;
2625
+ const repo = this.getOverlayRepo(orgId);
2626
+ const ref = {
2627
+ type: singularType,
2628
+ name: request.name,
2629
+ org: orgId ?? "env"
2630
+ };
2631
+ const events = [];
2632
+ const opts = {};
2633
+ if (request.sinceSeq !== void 0) opts.sinceSeq = request.sinceSeq;
2634
+ if (request.limit !== void 0) opts.limit = request.limit;
2635
+ for await (const ev of repo.history(ref, opts)) events.push(ev);
2636
+ return { events };
2637
+ }
2013
2638
  /**
2014
2639
  * Remove a customization overlay row for the given metadata item, so the
2015
2640
  * next read falls through to the artifact-loaded default. Implements the
@@ -2025,6 +2650,66 @@ var _ObjectStackProtocolImplementation = class _ObjectStackProtocolImplementatio
2025
2650
  err.status = 403;
2026
2651
  throw err;
2027
2652
  }
2653
+ const singularTypeForRepo = import_shared.PLURAL_TO_SINGULAR[request.type] ?? request.type;
2654
+ const useRepoPath = _ObjectStackProtocolImplementation.isOverlayAllowed(singularTypeForRepo);
2655
+ if (useRepoPath) {
2656
+ const orgId = request.organizationId ?? null;
2657
+ const repo = this.getOverlayRepo(orgId);
2658
+ const ref = {
2659
+ type: singularTypeForRepo,
2660
+ name: request.name,
2661
+ org: orgId ?? "env"
2662
+ };
2663
+ try {
2664
+ const current = await repo.get(ref);
2665
+ if (!current) {
2666
+ return {
2667
+ success: true,
2668
+ reset: false,
2669
+ message: `No customization overlay found for ${request.type}/${request.name} \u2014 already at artifact default.`
2670
+ };
2671
+ }
2672
+ const parentVersion = request.parentVersion !== void 0 ? request.parentVersion ?? current.hash : current.hash;
2673
+ const result = await repo.delete(ref, {
2674
+ parentVersion,
2675
+ actor: request.actor ?? "system",
2676
+ source: "protocol.deleteMetaItem"
2677
+ });
2678
+ if (this.projectId === void 0) {
2679
+ try {
2680
+ const services = this.getServicesRegistry?.();
2681
+ const metadataService = services?.get("metadata");
2682
+ if (metadataService && typeof metadataService.get === "function") {
2683
+ const artifactItem = await metadataService.get(request.type, request.name);
2684
+ if (artifactItem !== void 0) {
2685
+ this.engine.registry.registerItem(request.type, artifactItem, "name");
2686
+ }
2687
+ }
2688
+ } catch {
2689
+ }
2690
+ }
2691
+ return {
2692
+ success: true,
2693
+ reset: true,
2694
+ seq: result.seq,
2695
+ message: `Customization overlay deleted \u2014 ${request.type}/${request.name} reset to artifact default. [seq=${result.seq}]`
2696
+ };
2697
+ } catch (err) {
2698
+ if (err instanceof import_metadata_core2.ConflictError) {
2699
+ const conflict = new Error(
2700
+ `[metadata_conflict] ${request.type}/${request.name} has been modified since you loaded it. Expected parent ${err.expectedParent ?? "null"} but current is ${err.actualHead ?? "null"}.`
2701
+ );
2702
+ conflict.code = "metadata_conflict";
2703
+ conflict.status = 409;
2704
+ conflict.expectedParent = err.expectedParent;
2705
+ conflict.actualHead = err.actualHead;
2706
+ throw conflict;
2707
+ }
2708
+ const e = new Error(`Failed to delete customization overlay: ${err.message ?? err}`);
2709
+ e.status = err?.status ?? 500;
2710
+ throw e;
2711
+ }
2712
+ }
2028
2713
  const scopedWhere = {
2029
2714
  type: request.type,
2030
2715
  name: request.name,
@@ -2248,7 +2933,7 @@ var _ObjectStackProtocolImplementation = class _ObjectStackProtocolImplementatio
2248
2933
  */
2249
2934
  _ObjectStackProtocolImplementation.OVERLAY_ALLOWED_TYPES = (() => {
2250
2935
  const out = /* @__PURE__ */ new Set();
2251
- for (const entry of import_kernel2.DEFAULT_METADATA_TYPE_REGISTRY) {
2936
+ for (const entry of import_kernel3.DEFAULT_METADATA_TYPE_REGISTRY) {
2252
2937
  if (!entry.allowOrgOverride) continue;
2253
2938
  out.add(entry.type);
2254
2939
  const plural = import_shared.SINGULAR_TO_PLURAL[entry.type];
@@ -2259,7 +2944,7 @@ _ObjectStackProtocolImplementation.OVERLAY_ALLOWED_TYPES = (() => {
2259
2944
  var ObjectStackProtocolImplementation = _ObjectStackProtocolImplementation;
2260
2945
 
2261
2946
  // src/engine.ts
2262
- var import_kernel3 = require("@objectstack/spec/kernel");
2947
+ var import_kernel4 = require("@objectstack/spec/kernel");
2263
2948
  var import_core = require("@objectstack/core");
2264
2949
  var import_system = require("@objectstack/spec/system");
2265
2950
  var import_shared2 = require("@objectstack/spec/shared");
@@ -4547,7 +5232,7 @@ var _ObjectQL = class _ObjectQL {
4547
5232
  */
4548
5233
  createContext(ctx) {
4549
5234
  return new ScopedContext(
4550
- import_kernel3.ExecutionContextSchema.parse(ctx),
5235
+ import_kernel4.ExecutionContextSchema.parse(ctx),
4551
5236
  this
4552
5237
  );
4553
5238
  }
@@ -4835,6 +5520,8 @@ var ObjectQLPlugin = class {
4835
5520
  */
4836
5521
  this.startupTimeout = 12e4;
4837
5522
  this.skipSchemaSync = false;
5523
+ /** Unsubscribe handles for metadata-event subscriptions (ADR-0008 PR-7). */
5524
+ this.metadataUnsubscribes = [];
4838
5525
  this.init = async (ctx) => {
4839
5526
  if (!this.ql) {
4840
5527
  const hostCtx = { ...this.hostContext, logger: ctx.logger };
@@ -4881,6 +5568,9 @@ var ObjectQLPlugin = class {
4881
5568
  if (metadataService && typeof metadataService.loadMany === "function" && this.ql) {
4882
5569
  await this.loadMetadataFromService(metadataService, ctx);
4883
5570
  }
5571
+ if (metadataService && typeof metadataService.subscribe === "function" && this.ql) {
5572
+ this.subscribeToMetadataEvents(metadataService, ctx);
5573
+ }
4884
5574
  } catch (e) {
4885
5575
  ctx.logger.debug("No external metadata service to sync from");
4886
5576
  }
@@ -4934,6 +5624,16 @@ var ObjectQLPlugin = class {
4934
5624
  objectsRegistered: this.ql?.registry?.getAllObjects?.()?.length || 0
4935
5625
  });
4936
5626
  };
5627
+ this.stop = async (ctx) => {
5628
+ for (const unsub of this.metadataUnsubscribes) {
5629
+ try {
5630
+ unsub();
5631
+ } catch (e) {
5632
+ ctx.logger.debug("[ObjectQLPlugin] metadata-event unsubscribe failed", { error: e?.message });
5633
+ }
5634
+ }
5635
+ this.metadataUnsubscribes = [];
5636
+ };
4937
5637
  if (qlOrOptions instanceof ObjectQL) {
4938
5638
  this.ql = qlOrOptions;
4939
5639
  this.hostContext = hostContext;
@@ -4950,6 +5650,62 @@ var ObjectQLPlugin = class {
4950
5650
  }
4951
5651
  this.skipSchemaSync = typeof opts.skipSchemaSync === "boolean" ? opts.skipSchemaSync : process.env.OS_SKIP_SCHEMA_SYNC === "1";
4952
5652
  }
5653
+ /**
5654
+ * Subscribe to `object` metadata events from the metadata service and
5655
+ * invalidate the SchemaRegistry merge cache on each event (ADR-0008
5656
+ * PR-7). For create/update we also re-load the affected object from
5657
+ * the metadata service so subsequent reads see the new definition;
5658
+ * for delete we unregister it from every contributing package.
5659
+ *
5660
+ * Events are filtered to the canonical `object` type — view/dashboard
5661
+ * /flow edits go through their own consumers (Studio SSE, REST cache).
5662
+ *
5663
+ * Stored unsubscribe handle is invoked from {@link stop}.
5664
+ */
5665
+ subscribeToMetadataEvents(metadataService, ctx) {
5666
+ const handler = async (evt) => {
5667
+ if (!this.ql) return;
5668
+ const name = evt?.name ?? "";
5669
+ if (!name) return;
5670
+ const eventType = evt?.type === "added" || evt?.type === "changed" || evt?.type === "deleted" ? evt.type : "changed";
5671
+ try {
5672
+ this.ql.registry.invalidate(name);
5673
+ if (eventType === "deleted") {
5674
+ ctx.logger.info("[ObjectQLPlugin] object metadata deleted \u2014 registry invalidated", { name });
5675
+ return;
5676
+ }
5677
+ const fresh = typeof metadataService.get === "function" ? await metadataService.get("object", name) : void 0;
5678
+ if (fresh && typeof fresh === "object") {
5679
+ const packageId = fresh._packageId ?? "metadata-service";
5680
+ const namespace = fresh.namespace;
5681
+ this.ql.registry.registerObject(
5682
+ fresh,
5683
+ packageId,
5684
+ namespace,
5685
+ "own"
5686
+ );
5687
+ ctx.logger.info("[ObjectQLPlugin] object metadata updated \u2014 registry refreshed", {
5688
+ name,
5689
+ packageId
5690
+ });
5691
+ } else {
5692
+ ctx.logger.debug("[ObjectQLPlugin] object event received but metadata service has no fresh body", { name });
5693
+ }
5694
+ } catch (e) {
5695
+ ctx.logger.warn("[ObjectQLPlugin] metadata event handler failed", {
5696
+ name,
5697
+ error: e?.message
5698
+ });
5699
+ }
5700
+ };
5701
+ const unsub = metadataService.subscribe("object", handler);
5702
+ if (typeof unsub === "function") {
5703
+ this.metadataUnsubscribes.push(unsub);
5704
+ } else if (unsub && typeof unsub.unsubscribe === "function") {
5705
+ this.metadataUnsubscribes.push(() => unsub.unsubscribe());
5706
+ }
5707
+ ctx.logger.info("[ObjectQLPlugin] subscribed to object metadata events (ADR-0008 PR-7)");
5708
+ }
4953
5709
  /**
4954
5710
  * Register built-in audit hooks for auto-stamping created_by/updated_by
4955
5711
  * and fetching previousData for update/delete operations. These are
@@ -5448,6 +6204,7 @@ function convertIntrospectedSchemaToObjects(introspectedSchema, options) {
5448
6204
  RESERVED_NAMESPACES,
5449
6205
  SchemaRegistry,
5450
6206
  ScopedContext,
6207
+ SysMetadataRepository,
5451
6208
  ValidationError,
5452
6209
  applyInMemoryAggregation,
5453
6210
  applySystemFields,