@sylphx/lens-server 3.0.1 → 4.0.1

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.
@@ -254,6 +254,9 @@ export class OperationLog {
254
254
 
255
255
  /**
256
256
  * Remove oldest entry and update tracking.
257
+ *
258
+ * IMPORTANT: After entries.shift(), ALL indices must be decremented by 1
259
+ * to maintain correctness of entityIndex lookups.
257
260
  */
258
261
  private removeOldest(): void {
259
262
  const removed = this.entries.shift();
@@ -261,21 +264,29 @@ export class OperationLog {
261
264
 
262
265
  this.totalMemory -= removed.patchSize;
263
266
 
264
- // Update oldest version for entity
265
- const indices = this.entityIndex.get(removed.entityKey);
266
- if (indices && indices.length > 0) {
267
- // Remove first index (oldest)
268
- indices.shift();
267
+ // CRITICAL FIX: Decrement ALL indices for ALL entities since array shifted
268
+ // Must happen BEFORE any index lookups to ensure correctness
269
+ for (const indices of this.entityIndex.values()) {
270
+ for (let i = 0; i < indices.length; i++) {
271
+ indices[i]--;
272
+ }
273
+ }
274
+
275
+ // Update oldest version for the removed entity
276
+ const removedEntityIndices = this.entityIndex.get(removed.entityKey);
277
+ if (removedEntityIndices && removedEntityIndices.length > 0) {
278
+ // Remove first index (oldest) for this entity
279
+ // After decrement above, this index is now -1, so shift() removes it
280
+ removedEntityIndices.shift();
269
281
 
270
- if (indices.length === 0) {
282
+ if (removedEntityIndices.length === 0) {
271
283
  // No more entries for this entity
272
284
  this.entityIndex.delete(removed.entityKey);
273
285
  this.oldestVersionIndex.delete(removed.entityKey);
274
286
  this.newestVersionIndex.delete(removed.entityKey);
275
287
  } else {
276
- // Update oldest version to next entry
277
- // Note: indices are now stale (off by 1) until rebuildIndices
278
- const nextEntry = this.entries[indices[0] - 1]; // -1 because we shifted
288
+ // Update oldest version to next entry (indices already corrected)
289
+ const nextEntry = this.entries[removedEntityIndices[0]];
279
290
  if (nextEntry) {
280
291
  this.oldestVersionIndex.set(removed.entityKey, nextEntry.version);
281
292
  }