@memberjunction/core 6.1.5 → 6.1.7

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 (58) hide show
  1. package/dist/generic/JSONType-interfaces/IEntitySubtypeSelectorConfig.d.ts +14 -0
  2. package/dist/generic/JSONType-interfaces/IEntitySubtypeSelectorConfig.d.ts.map +1 -1
  3. package/dist/generic/baseEngine.d.ts +45 -0
  4. package/dist/generic/baseEngine.d.ts.map +1 -1
  5. package/dist/generic/baseEngine.js +81 -7
  6. package/dist/generic/baseEngine.js.map +1 -1
  7. package/dist/generic/baseEngineRegistry.d.ts +32 -11
  8. package/dist/generic/baseEngineRegistry.d.ts.map +1 -1
  9. package/dist/generic/baseEngineRegistry.js +77 -30
  10. package/dist/generic/baseEngineRegistry.js.map +1 -1
  11. package/dist/generic/baseEntity.d.ts +297 -2
  12. package/dist/generic/baseEntity.d.ts.map +1 -1
  13. package/dist/generic/baseEntity.js +1403 -49
  14. package/dist/generic/baseEntity.js.map +1 -1
  15. package/dist/generic/databaseProviderBase.d.ts +41 -8
  16. package/dist/generic/databaseProviderBase.d.ts.map +1 -1
  17. package/dist/generic/databaseProviderBase.js +107 -42
  18. package/dist/generic/databaseProviderBase.js.map +1 -1
  19. package/dist/generic/entityInfo.d.ts +12 -0
  20. package/dist/generic/entityInfo.d.ts.map +1 -1
  21. package/dist/generic/entityInfo.js +26 -2
  22. package/dist/generic/entityInfo.js.map +1 -1
  23. package/dist/generic/entityRecordNameCache.d.ts +48 -0
  24. package/dist/generic/entityRecordNameCache.d.ts.map +1 -0
  25. package/dist/generic/entityRecordNameCache.js +92 -0
  26. package/dist/generic/entityRecordNameCache.js.map +1 -0
  27. package/dist/generic/entitySubtypeResolver.d.ts +51 -5
  28. package/dist/generic/entitySubtypeResolver.d.ts.map +1 -1
  29. package/dist/generic/entitySubtypeResolver.js +50 -3
  30. package/dist/generic/entitySubtypeResolver.js.map +1 -1
  31. package/dist/generic/interfaces.d.ts +49 -4
  32. package/dist/generic/interfaces.d.ts.map +1 -1
  33. package/dist/generic/interfaces.js.map +1 -1
  34. package/dist/generic/localCacheManager.d.ts +118 -11
  35. package/dist/generic/localCacheManager.d.ts.map +1 -1
  36. package/dist/generic/localCacheManager.js +281 -40
  37. package/dist/generic/localCacheManager.js.map +1 -1
  38. package/dist/generic/logging.d.ts +15 -0
  39. package/dist/generic/logging.d.ts.map +1 -1
  40. package/dist/generic/logging.js +17 -0
  41. package/dist/generic/logging.js.map +1 -1
  42. package/dist/generic/providerBase.d.ts +37 -23
  43. package/dist/generic/providerBase.d.ts.map +1 -1
  44. package/dist/generic/providerBase.js +179 -132
  45. package/dist/generic/providerBase.js.map +1 -1
  46. package/dist/generic/queryInfo.d.ts +14 -2
  47. package/dist/generic/queryInfo.d.ts.map +1 -1
  48. package/dist/generic/queryInfo.js +18 -5
  49. package/dist/generic/queryInfo.js.map +1 -1
  50. package/dist/generic/util.d.ts +13 -1
  51. package/dist/generic/util.d.ts.map +1 -1
  52. package/dist/generic/util.js +56 -4
  53. package/dist/generic/util.js.map +1 -1
  54. package/dist/index.d.ts +1 -0
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +1 -0
  57. package/dist/index.js.map +1 -1
  58. package/package.json +3 -3
@@ -118,6 +118,11 @@ export class LocalCacheManager extends BaseSingleton {
118
118
  static get Instance() {
119
119
  return super.getInstance();
120
120
  }
121
+ /**
122
+ * How often, at most, a process writes an entity's shared slots while it keeps finding that
123
+ * entity's slots under a schema different from its own. See {@link handleSchemaMismatch}.
124
+ */
125
+ static { this.SCHEMA_MISMATCH_WRITE_INTERVAL_MS = 60_000; }
121
126
  constructor() {
122
127
  super();
123
128
  this._storageProvider = null;
@@ -139,6 +144,14 @@ export class LocalCacheManager extends BaseSingleton {
139
144
  * (e.g., TransactionGroup batch deletes).
140
145
  */
141
146
  this._fingerprintLocks = new Map();
147
+ /**
148
+ * Write holds on entities whose shared slots were found under a different schema, keyed by
149
+ * lowercased entity name. While a hold applies, {@link SetRunViewResult} skips or rate-limits
150
+ * writes of that entity's slots. See {@link handleSchemaMismatch} and {@link mayWriteUnderSchemaHold}.
151
+ */
152
+ this._schemaWriteHolds = new Map();
153
+ /** Schema mismatches already reported, so each entity/hash pair is logged once per process. */
154
+ this._reportedSchemaMismatches = new Set();
142
155
  this.REGISTRY_KEY = '__MJ_CACHE_REGISTRY__';
143
156
  /**
144
157
  * Reverse index from entity name to the set of RunView cache fingerprints
@@ -540,30 +553,134 @@ export class LocalCacheManager extends BaseSingleton {
540
553
  return Number.isFinite(startRow) && startRow > 0;
541
554
  }
542
555
  /**
543
- * Checks whether a cached RunView entry is structurally stale due to a schema change
544
- * (e.g., new columns added via migration + CodeGen). Compares the stored schema hash
545
- * against the current entity field list. If they differ, the entry is invalidated.
556
+ * Compares a cached RunView entry's schema hash against this process's current field list for
557
+ * the entity (a mismatch follows a migration + CodeGen that added, removed or reordered
558
+ * columns). Entries without a hash, and entities this process cannot resolve, never mismatch.
546
559
  * @param fingerprint - The cache fingerprint
547
560
  * @param data - The cached data to validate
548
- * @returns true if the entry is stale and should not be served
561
+ * @returns The mismatch, or null when the entry may be served
549
562
  */
550
- isSchemaStaleCacheEntry(fingerprint, data) {
563
+ findSchemaMismatch(fingerprint, data) {
551
564
  if (!data.schemaHash)
552
- return false;
565
+ return null;
553
566
  const entityName = this.extractEntityFromFingerprint(fingerprint);
554
567
  if (!entityName)
568
+ return null;
569
+ const current = this.computeSchemaSignature(undefined, entityName);
570
+ if (!current || current.Hash === data.schemaHash)
571
+ return null;
572
+ return {
573
+ EntityName: entityName,
574
+ CachedHash: data.schemaHash,
575
+ Current: current,
576
+ EntryIsNewer: LocalCacheManager.isNewerSchemaStamp(data.schemaStamp, current.Stamp),
577
+ };
578
+ }
579
+ /**
580
+ * True only when both stamps are present and the cached entry's is strictly newer than this
581
+ * process's. Equal or missing stamps leave the order unknown, which counts as not newer.
582
+ */
583
+ static isNewerSchemaStamp(entryStamp, currentStamp) {
584
+ if (!entryStamp || !currentStamp) {
555
585
  return false;
556
- const currentHash = this.ComputeSchemaHash(undefined, entityName);
557
- if (!currentHash)
558
- return false;
559
- if (currentHash !== data.schemaHash) {
560
- LogStatusEx({
561
- message: `[CACHE-SCHEMA-STALE] Entity "${entityName}" schema changed (cached=${data.schemaHash}, current=${currentHash})`,
562
- verboseOnly: false
586
+ }
587
+ return LocalCacheManager.timestampMs(entryStamp) > LocalCacheManager.timestampMs(currentStamp);
588
+ }
589
+ /**
590
+ * Decides what a schema mismatch does to the slot. The entry is never served; the question is
591
+ * whether the slot is deleted, which on a shared store also tells every peer to reload.
592
+ *
593
+ * - Process-local store, or a slot-maintenance read: delete it. No other process reads a local
594
+ * store. A maintenance read happens because a row changed, and a slot that cannot take the
595
+ * change in place must go, whichever process wrote it.
596
+ * - Shared store, serving read: leave it, and hold back this process's writes of the entity.
597
+ * Deleting publishes `removed`, every peer engine holding the slot reloads and rewrites it
598
+ * under its own hash, and processes on either side of a migration keep doing that to each
599
+ * other. The caller reads the database instead.
600
+ * - When the slot is known to come from newer metadata, this process is the one out of date.
601
+ * It stops writing the entity's slots until its own metadata changes.
602
+ * - Otherwise it writes the entity's slots at most once per
603
+ * {@link SCHEMA_MISMATCH_WRITE_INTERVAL_MS} while mismatches keep appearing. That still
604
+ * replaces a slot left by older metadata, but a peer running code that deletes mismatched
605
+ * slots (any release before this check stopped deleting them) gets one slot to delete per
606
+ * interval instead of one per read. The hold lapses after an interval with no mismatch.
607
+ */
608
+ handleSchemaMismatch(fingerprint, mismatch, purpose) {
609
+ const shared = this._storageProvider?.SharedAcrossProcesses === true;
610
+ if (!shared || purpose === 'maintain') {
611
+ this.reportSchemaMismatch(mismatch, 'dropping the slot');
612
+ this.InvalidateRunViewResult(fingerprint).catch(() => { });
613
+ return;
614
+ }
615
+ this.holdSchemaWrites(mismatch);
616
+ if (mismatch.EntryIsNewer) {
617
+ this.reportSchemaMismatch(mismatch, 'another process wrote the slot under newer metadata. This process reads the entity from the database ' +
618
+ 'and stops writing its cache slots until its metadata changes. If it started before a migration, restart it.');
619
+ }
620
+ else {
621
+ this.reportSchemaMismatch(mismatch, `reading from the database. While slots under other metadata keep appearing, this process writes the entity's ` +
622
+ `slots at most once every ${LocalCacheManager.SCHEMA_MISMATCH_WRITE_INTERVAL_MS / 1000}s.`);
623
+ }
624
+ }
625
+ /** Records or extends the write hold for a mismatch seen on a shared store. */
626
+ holdSchemaWrites(mismatch) {
627
+ const key = mismatch.EntityName.trim().toLowerCase();
628
+ const now = Date.now();
629
+ const existing = this._schemaWriteHolds.get(key);
630
+ if (mismatch.EntryIsNewer) {
631
+ this._schemaWriteHolds.set(key, { OwnHash: mismatch.Current.Hash, NextWriteAt: Number.POSITIVE_INFINITY, LastMismatchAt: now });
632
+ }
633
+ else if (existing && existing.OwnHash === mismatch.Current.Hash) {
634
+ existing.LastMismatchAt = now;
635
+ }
636
+ else {
637
+ this._schemaWriteHolds.set(key, {
638
+ OwnHash: mismatch.Current.Hash,
639
+ NextWriteAt: now + LocalCacheManager.SCHEMA_MISMATCH_WRITE_INTERVAL_MS,
640
+ LastMismatchAt: now,
563
641
  });
642
+ }
643
+ }
644
+ /** Logs a schema mismatch once per entity and hash pair per process. */
645
+ reportSchemaMismatch(mismatch, outcome) {
646
+ const key = `${mismatch.EntityName.trim().toLowerCase()}|${mismatch.CachedHash}|${mismatch.Current.Hash}`;
647
+ if (this._reportedSchemaMismatches.has(key)) {
648
+ return;
649
+ }
650
+ this._reportedSchemaMismatches.add(key);
651
+ LogStatusEx({
652
+ message: `[CACHE-SCHEMA-STALE] Entity "${mismatch.EntityName}" schema differs from a cached slot ` +
653
+ `(cached=${mismatch.CachedHash}, current=${mismatch.Current.Hash}) — ${outcome}`,
654
+ verboseOnly: false
655
+ });
656
+ }
657
+ /**
658
+ * Whether a slot of `entityName` may be written now, given the entity's write hold (see
659
+ * {@link handleSchemaMismatch}). A permitted write under a rate-limiting hold uses up the
660
+ * interval. The hold is dropped once this process's schema hash changes (a metadata refresh)
661
+ * or, for a rate-limiting hold, once an interval passes with no mismatch.
662
+ */
663
+ mayWriteUnderSchemaHold(entityName, currentHash) {
664
+ if (this._storageProvider?.SharedAcrossProcesses !== true) {
564
665
  return true;
565
666
  }
566
- return false;
667
+ const key = entityName.trim().toLowerCase();
668
+ const hold = this._schemaWriteHolds.get(key);
669
+ if (!hold) {
670
+ return true;
671
+ }
672
+ const now = Date.now();
673
+ const interval = LocalCacheManager.SCHEMA_MISMATCH_WRITE_INTERVAL_MS;
674
+ const lapsed = hold.NextWriteAt !== Number.POSITIVE_INFINITY && now - hold.LastMismatchAt > interval;
675
+ if (hold.OwnHash !== currentHash || lapsed) {
676
+ this._schemaWriteHolds.delete(key);
677
+ return true;
678
+ }
679
+ if (now < hold.NextWriteAt) {
680
+ return false;
681
+ }
682
+ hold.NextWriteAt = now + interval;
683
+ return true;
567
684
  }
568
685
  /**
569
686
  * Adds a fingerprint to the entity→fingerprint reverse index.
@@ -602,21 +719,26 @@ export class LocalCacheManager extends BaseSingleton {
602
719
  return this._entityFingerprintIndex.get(entityName) ?? new Set();
603
720
  }
604
721
  /**
605
- * Resolves cached fingerprints for an entity, checking the local in-memory
606
- * index first and falling back to the shared storage provider (e.g., Redis)
607
- * when the local index is empty. This handles cross-server scenarios where
608
- * Server A cached RunView results and Server B saves a record — Server B's
609
- * local index is empty but Redis still has the stale cached entries.
722
+ * Resolves cached fingerprints for an entity: this process's own index, plus the slots the
723
+ * storage provider lists for the entity.
724
+ *
725
+ * On a provider shared between processes (e.g. Redis) the provider is listed on every call
726
+ * and merged into the local index. The local index only knows slots this process wrote, so
727
+ * answering from it alone would miss slots other processes wrote — including a slot this
728
+ * process only read warm at boot — and a save here would never reach the engines that load
729
+ * from those slots on its peers.
730
+ *
731
+ * On a process-local provider (e.g. IndexedDB) the category is listed only when the local
732
+ * index knows nothing for the entity, which picks up slots persisted by an earlier session.
610
733
  */
611
734
  async resolveFingerprintsForEntity(entityName) {
735
+ if (this._storageProvider?.SharedAcrossProcesses === true && this._storageProvider.GetCategoryKeys) {
736
+ return this.resolveFingerprintsFromSharedStore(entityName);
737
+ }
612
738
  const local = this._entityFingerprintIndex.get(entityName);
613
739
  if (local && local.size > 0)
614
740
  return local;
615
- if (!this._storageProvider?.GetCategoryKeys)
616
- return undefined;
617
- const allKeys = await this._storageProvider.GetCategoryKeys(CacheCategory.RunViewCache);
618
- const entityPrefix = entityName + '|';
619
- const remoteFingerprints = allKeys.filter(k => k.startsWith(entityPrefix));
741
+ const remoteFingerprints = await this.listStoredFingerprintsForEntity(entityName);
620
742
  if (remoteFingerprints.length > 0) {
621
743
  LogStatusVerbose(`LocalCacheManager: found ${remoteFingerprints.length} remote cached fingerprint(s) for "${entityName}" via storage provider`);
622
744
  const result = new Set(remoteFingerprints);
@@ -628,6 +750,26 @@ export class LocalCacheManager extends BaseSingleton {
628
750
  }
629
751
  return undefined;
630
752
  }
753
+ /**
754
+ * Merges the slots a shared store lists for the entity into the local index and returns the
755
+ * union. Local entries the store does not list are left in place; invalidation removes them
756
+ * from the index the same way it does for any other slot.
757
+ */
758
+ async resolveFingerprintsFromSharedStore(entityName) {
759
+ for (const fp of await this.listStoredFingerprintsForEntity(entityName)) {
760
+ this.addToEntityIndex(fp);
761
+ }
762
+ const merged = this._entityFingerprintIndex.get(entityName);
763
+ return merged && merged.size > 0 ? merged : undefined;
764
+ }
765
+ /** Lists the RunView slots the storage provider holds for an entity; empty when it cannot list keys. */
766
+ async listStoredFingerprintsForEntity(entityName) {
767
+ if (!this._storageProvider?.GetCategoryKeys)
768
+ return [];
769
+ const allKeys = await this._storageProvider.GetCategoryKeys(CacheCategory.RunViewCache);
770
+ const entityPrefix = entityName + '|';
771
+ return allKeys.filter(k => k.startsWith(entityPrefix));
772
+ }
631
773
  // ========================================================================
632
774
  // UNIVERSAL CACHE INVALIDATION (BaseEntity Events)
633
775
  // ========================================================================
@@ -674,6 +816,18 @@ export class LocalCacheManager extends BaseSingleton {
674
816
  const baseEntity = entityEvent.baseEntity;
675
817
  if (!baseEntity?.EntityInfo?.Name)
676
818
  return;
819
+ if ((entityEvent.type === 'save' || entityEvent.type === 'delete') && baseEntity.EntityInfo.IsParentType) {
820
+ const activeChainLower = new Set();
821
+ let currChild = baseEntity.ISAChild;
822
+ while (currChild) {
823
+ const cName = currChild.EntityInfo?.Name?.trim().toLowerCase();
824
+ if (cName) {
825
+ activeChainLower.add(cName);
826
+ }
827
+ currChild = currChild.ISAChild;
828
+ }
829
+ await this.invalidateISADescendantEntities(baseEntity.EntityInfo, activeChainLower);
830
+ }
677
831
  const entityName = baseEntity.EntityInfo.Name;
678
832
  // Short-circuit: if caching is disabled for this entity, skip the fingerprint scan
679
833
  if (!this.IsCachingEnabledForEntity(baseEntity.EntityInfo))
@@ -739,6 +893,9 @@ export class LocalCacheManager extends BaseSingleton {
739
893
  // the Metadata helper itself proxies to the global provider with sensible fallbacks.
740
894
  const md = entityEvent.provider ?? new Metadata();
741
895
  const entityInfo = md.EntityByName(entityName);
896
+ if (entityInfo?.IsParentType) {
897
+ await this.invalidateISADescendantEntities(entityInfo);
898
+ }
742
899
  if (entityInfo && !this.IsCachingEnabledForEntity(entityInfo))
743
900
  return;
744
901
  const fingerprints = await this.resolveFingerprintsForEntity(entityName);
@@ -857,6 +1014,51 @@ export class LocalCacheManager extends BaseSingleton {
857
1014
  return null;
858
1015
  }
859
1016
  }
1017
+ /**
1018
+ * When an IS-A parent entity is saved or deleted, child entities' SQL views (`vwMembers`,
1019
+ * `vwSpeakers`, `vwMeetings`, etc.) join the parent table for inherited columns.
1020
+ * - For overlapping subtype parents (`AllowMultipleSubtypes === true`, e.g., `Persons` ->
1021
+ * `Members`, `Speakers`), saving `Members` also updates `Persons`, so sibling subtypes
1022
+ * (`Speakers`) not in the active save chain may now hold stale inherited parent fields.
1023
+ * - For disjoint subtype parents (`AllowMultipleSubtypes === false`, e.g., `Products` ->
1024
+ * `Meetings`, `Publications`), each record belongs to at most one child subtype, so when
1025
+ * a known child is in the active chain, sibling subtypes cannot hold that record; only a
1026
+ * bare parent save/delete (no active child in `alreadyHandledLower`) requires invalidating
1027
+ * child subtype caches.
1028
+ */
1029
+ async invalidateISADescendantEntities(parentInfo, alreadyHandledLower) {
1030
+ const children = parentInfo.ChildEntities;
1031
+ if (!Array.isArray(children) || children.length === 0) {
1032
+ return;
1033
+ }
1034
+ if (!parentInfo.AllowMultipleSubtypes && alreadyHandledLower && alreadyHandledLower.size > 0) {
1035
+ const hasHandledChild = children.some(c => {
1036
+ const cLower = c?.Name?.trim().toLowerCase();
1037
+ return !!cLower && alreadyHandledLower.has(cLower);
1038
+ });
1039
+ if (hasHandledChild) {
1040
+ return;
1041
+ }
1042
+ }
1043
+ for (const child of children) {
1044
+ const childName = child?.Name?.trim();
1045
+ if (!childName) {
1046
+ continue;
1047
+ }
1048
+ const childLower = childName.toLowerCase();
1049
+ if (!alreadyHandledLower?.has(childLower)) {
1050
+ const childFingerprints = await this.resolveFingerprintsForEntity(childName);
1051
+ if (childFingerprints && childFingerprints.size > 0) {
1052
+ for (const fp of [...childFingerprints]) {
1053
+ await this.InvalidateRunViewResult(fp);
1054
+ }
1055
+ }
1056
+ }
1057
+ if (child.IsParentType) {
1058
+ await this.invalidateISADescendantEntities(child, alreadyHandledLower);
1059
+ }
1060
+ }
1061
+ }
860
1062
  /**
861
1063
  * Builds a CompositeKey from a plain row object using the specified PK field names.
862
1064
  */
@@ -1446,6 +1648,14 @@ export class LocalCacheManager extends BaseSingleton {
1446
1648
  * @returns The schema hash string, or undefined if the entity can't be resolved
1447
1649
  */
1448
1650
  ComputeSchemaHash(provider, entityName) {
1651
+ return this.computeSchemaSignature(provider, entityName)?.Hash;
1652
+ }
1653
+ /**
1654
+ * The entity's schema hash (see {@link ComputeSchemaHash}) together with its schema stamp: the
1655
+ * newest `__mj_UpdatedAt` across the entity row and its field rows, as an ISO string, or
1656
+ * undefined when the metadata carries no timestamps.
1657
+ */
1658
+ computeSchemaSignature(provider, entityName) {
1449
1659
  try {
1450
1660
  const md = provider ?? new Metadata();
1451
1661
  const entity = md.EntityByName(entityName);
@@ -1454,12 +1664,27 @@ export class LocalCacheManager extends BaseSingleton {
1454
1664
  // Use natural sequence order (EntityInfo.Fields is sorted by Sequence).
1455
1665
  // This detects field additions, removals, AND reorderings.
1456
1666
  const fieldNames = entity.Fields.map(f => f.Name).join('|');
1457
- return this.simpleHash(fieldNames);
1667
+ let newest = LocalCacheManager.timestampMs(entity.__mj_UpdatedAt);
1668
+ for (const field of entity.Fields) {
1669
+ newest = Math.max(newest, LocalCacheManager.timestampMs(field.__mj_UpdatedAt));
1670
+ }
1671
+ return {
1672
+ Hash: this.simpleHash(fieldNames),
1673
+ Stamp: newest > 0 ? new Date(newest).toISOString() : undefined,
1674
+ };
1458
1675
  }
1459
1676
  catch {
1460
1677
  return undefined;
1461
1678
  }
1462
1679
  }
1680
+ /** Milliseconds since the epoch for a metadata timestamp (a Date or a string), or 0 when unreadable. */
1681
+ static timestampMs(value) {
1682
+ if (value == null) {
1683
+ return 0;
1684
+ }
1685
+ const ms = value instanceof Date ? value.getTime() : Date.parse(value);
1686
+ return Number.isNaN(ms) ? 0 : ms;
1687
+ }
1463
1688
  /**
1464
1689
  * Deep-freezes an about-to-be-stored cache payload when — and only when — the active
1465
1690
  * storage provider hands out live object references
@@ -1609,9 +1834,16 @@ export class LocalCacheManager extends BaseSingleton {
1609
1834
  }
1610
1835
  // Compute and store schema hash for upgrade detection
1611
1836
  if (params.EntityName) {
1612
- const schemaHash = this.ComputeSchemaHash(provider, params.EntityName);
1613
- if (schemaHash) {
1614
- data.schemaHash = schemaHash;
1837
+ const signature = this.computeSchemaSignature(provider, params.EntityName);
1838
+ if (signature) {
1839
+ if (!this.mayWriteUnderSchemaHold(params.EntityName, signature.Hash)) {
1840
+ LogStatusEx({ message: `[CACHE-WRITE-GATE] Skipping cache write for "${params.EntityName}" — its shared slots were found under different metadata`, verboseOnly: true });
1841
+ return;
1842
+ }
1843
+ data.schemaHash = signature.Hash;
1844
+ if (signature.Stamp) {
1845
+ data.schemaStamp = signature.Stamp;
1846
+ }
1615
1847
  }
1616
1848
  }
1617
1849
  // Estimate size by sampling rows (eviction accounting only); the actual stored
@@ -1626,9 +1858,9 @@ export class LocalCacheManager extends BaseSingleton {
1626
1858
  LogStatusEx({ message: `[CACHE-WRITE-GATE] Skipping cache write for "${params.EntityName || fingerprint.substring(0, 60)}" — estimated entry size ${sizeBytes} bytes exceeds per-entry cap (${this._config.maxEntryPercentOfCache}% of ${this._config.maxSizeBytes} byte budget)` });
1627
1859
  return;
1628
1860
  }
1629
- // The oversized gate above is the ONLY step that can decline this write, and it is
1630
- // synchronous — so by here the row set is definitely becoming shared state, and it is
1631
- // frozen with no intervening yield point. That ordering is load-bearing: the eviction
1861
+ // The schema gate and the oversized gate above are the ONLY steps that can decline this
1862
+ // write, and they are synchronous — so by here the row set is definitely becoming shared
1863
+ // state, and it is frozen with no intervening yield point. That ordering is load-bearing: the eviction
1632
1864
  // steps below are awaited, and callers hold this very array while they run (the
1633
1865
  // smart-cache stale leg does not await this method at all). Freezing after them leaves
1634
1866
  // a window in which shared rows are handed out still mutable — and `BaseEntity`
@@ -1685,12 +1917,16 @@ export class LocalCacheManager extends BaseSingleton {
1685
1917
  * @returns The cached results, maxUpdatedAt, rowCount (derived), and aggregateResults, or null if not found
1686
1918
  */
1687
1919
  async GetRunViewResult(fingerprint) {
1920
+ return this.readRunViewSlot(fingerprint, 'serve');
1921
+ }
1922
+ /** {@link GetRunViewResult} for a given purpose; in-place slot maintenance reads with `'maintain'`. */
1923
+ async readRunViewSlot(fingerprint, purpose) {
1688
1924
  if (!this._storageProvider || !this._config.enabled)
1689
1925
  return null;
1690
1926
  try {
1691
1927
  // Native object read — IDB structured-clones the result back, no JSON.parse needed.
1692
1928
  const parsed = await this._storageProvider.GetItem(fingerprint, CacheCategory.RunViewCache);
1693
- return this.materializeCachedRunViewResult(fingerprint, parsed);
1929
+ return this.materializeCachedRunViewResult(fingerprint, parsed, purpose);
1694
1930
  }
1695
1931
  catch (e) {
1696
1932
  LogError(`LocalCacheManager.GetRunViewResult failed: ${e}`);
@@ -1730,7 +1966,7 @@ export class LocalCacheManager extends BaseSingleton {
1730
1966
  try {
1731
1967
  const raw = await this._storageProvider.GetItems(fingerprints, CacheCategory.RunViewCache);
1732
1968
  for (const [fp, parsed] of raw) {
1733
- out.set(fp, this.materializeCachedRunViewResult(fp, parsed));
1969
+ out.set(fp, this.materializeCachedRunViewResult(fp, parsed, 'serve'));
1734
1970
  }
1735
1971
  return out;
1736
1972
  }
@@ -1753,7 +1989,7 @@ export class LocalCacheManager extends BaseSingleton {
1753
1989
  * cache entries after schema migrations. If the entity's field list changed
1754
1990
  * since the entry was cached, the entry is invalidated and null is returned.
1755
1991
  */
1756
- materializeCachedRunViewResult(fingerprint, parsed) {
1992
+ materializeCachedRunViewResult(fingerprint, parsed, purpose) {
1757
1993
  if (!parsed) {
1758
1994
  this._stats.misses++;
1759
1995
  return null;
@@ -1767,8 +2003,9 @@ export class LocalCacheManager extends BaseSingleton {
1767
2003
  this._stats.misses++;
1768
2004
  return null;
1769
2005
  }
1770
- if (this.isSchemaStaleCacheEntry(fingerprint, parsed)) {
1771
- this.InvalidateRunViewResult(fingerprint).catch(() => { });
2006
+ const schemaMismatch = this.findSchemaMismatch(fingerprint, parsed);
2007
+ if (schemaMismatch) {
2008
+ this.handleSchemaMismatch(fingerprint, schemaMismatch, purpose);
1772
2009
  this._stats.misses++;
1773
2010
  return null;
1774
2011
  }
@@ -1782,6 +2019,7 @@ export class LocalCacheManager extends BaseSingleton {
1782
2019
  totalRowCount: parsed.totalRowCount,
1783
2020
  // Surfaced so in-place maintenance can carry it forward on rewrite (B38).
1784
2021
  schemaHash: parsed.schemaHash,
2022
+ schemaStamp: parsed.schemaStamp,
1785
2023
  // Same carry-forward rationale: a maintained scaffolding slot must stay exempt.
1786
2024
  providerInternalScaffolding: parsed.providerInternalScaffolding,
1787
2025
  };
@@ -1978,7 +2216,7 @@ export class LocalCacheManager extends BaseSingleton {
1978
2216
  return false;
1979
2217
  return this.withFingerprintLock(fingerprint, async () => {
1980
2218
  try {
1981
- const cached = await this.GetRunViewResult(fingerprint);
2219
+ const cached = await this.readRunViewSlot(fingerprint, 'maintain');
1982
2220
  if (!cached) {
1983
2221
  LogStatusVerbose(`LocalCacheManager.UpsertSingleEntity: No cached data found for fingerprint "${fingerprint.substring(0, 60)}" — skipping (cache will be populated on next RunView)`);
1984
2222
  return false;
@@ -1999,7 +2237,7 @@ export class LocalCacheManager extends BaseSingleton {
1999
2237
  // Upsert the entity (add or replace)
2000
2238
  resultMap.set(keyStr, entityData);
2001
2239
  const updatedResults = Array.from(resultMap.values());
2002
- return await this.storeCachedResults(fingerprint, updatedResults, newMaxUpdatedAt, { totalRowCount: cached.totalRowCount, rowCount: cached.results.length, schemaHash: cached.schemaHash,
2240
+ return await this.storeCachedResults(fingerprint, updatedResults, newMaxUpdatedAt, { totalRowCount: cached.totalRowCount, rowCount: cached.results.length, schemaHash: cached.schemaHash, schemaStamp: cached.schemaStamp,
2003
2241
  providerInternalScaffolding: cached.providerInternalScaffolding });
2004
2242
  }
2005
2243
  catch (e) {
@@ -2022,7 +2260,7 @@ export class LocalCacheManager extends BaseSingleton {
2022
2260
  return false;
2023
2261
  return this.withFingerprintLock(fingerprint, async () => {
2024
2262
  try {
2025
- const cached = await this.GetRunViewResult(fingerprint);
2263
+ const cached = await this.readRunViewSlot(fingerprint, 'maintain');
2026
2264
  if (!cached) {
2027
2265
  return false;
2028
2266
  }
@@ -2042,7 +2280,7 @@ export class LocalCacheManager extends BaseSingleton {
2042
2280
  }
2043
2281
  resultMap.delete(keyStr);
2044
2282
  const updatedResults = Array.from(resultMap.values());
2045
- return await this.storeCachedResults(fingerprint, updatedResults, newMaxUpdatedAt, { totalRowCount: cached.totalRowCount, rowCount: cached.results.length, schemaHash: cached.schemaHash,
2283
+ return await this.storeCachedResults(fingerprint, updatedResults, newMaxUpdatedAt, { totalRowCount: cached.totalRowCount, rowCount: cached.results.length, schemaHash: cached.schemaHash, schemaStamp: cached.schemaStamp,
2046
2284
  providerInternalScaffolding: cached.providerInternalScaffolding });
2047
2285
  }
2048
2286
  catch (e) {
@@ -2084,6 +2322,9 @@ export class LocalCacheManager extends BaseSingleton {
2084
2322
  if (prior?.schemaHash) {
2085
2323
  data.schemaHash = prior.schemaHash;
2086
2324
  }
2325
+ if (prior?.schemaStamp) {
2326
+ data.schemaStamp = prior.schemaStamp;
2327
+ }
2087
2328
  // Carry the freeze exemption forward too — a scaffolding slot that gets maintained in
2088
2329
  // place must not silently become frozen, or the owner that mutates those rows breaks on
2089
2330
  // the next read (for the metadata dataset that means booting with no metadata).