@dxos/echo-pipeline 0.1.56-main.c19b7cd → 0.1.56-main.c277f68

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 (40) hide show
  1. package/dist/lib/browser/{chunk-EJ35NHZV.mjs → chunk-GDF4DQU2.mjs} +296 -272
  2. package/dist/lib/browser/chunk-GDF4DQU2.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/index.mjs.map +1 -1
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +12 -17
  7. package/dist/lib/browser/testing/index.mjs.map +1 -1
  8. package/dist/lib/node/index.cjs +300 -276
  9. package/dist/lib/node/index.cjs.map +3 -3
  10. package/dist/lib/node/meta.json +1 -1
  11. package/dist/lib/node/testing/index.cjs +313 -294
  12. package/dist/lib/node/testing/index.cjs.map +3 -3
  13. package/dist/types/src/db-host/data-service-host.d.ts.map +1 -1
  14. package/dist/types/src/pipeline/pipeline-stress.test.d.ts +2 -0
  15. package/dist/types/src/pipeline/pipeline-stress.test.d.ts.map +1 -0
  16. package/dist/types/src/pipeline/pipeline.d.ts +4 -3
  17. package/dist/types/src/pipeline/pipeline.d.ts.map +1 -1
  18. package/dist/types/src/space/control-pipeline.d.ts +4 -0
  19. package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
  20. package/dist/types/src/space/data-pipeline.d.ts +2 -0
  21. package/dist/types/src/space/data-pipeline.d.ts.map +1 -1
  22. package/dist/types/src/space/space-manager.d.ts.map +1 -1
  23. package/dist/types/src/space/space-protocol.d.ts.map +1 -1
  24. package/dist/types/src/space/space.d.ts.map +1 -1
  25. package/dist/types/src/testing/database-test-rig.d.ts +4 -1
  26. package/dist/types/src/testing/database-test-rig.d.ts.map +1 -1
  27. package/package.json +32 -32
  28. package/src/db-host/data-service-host.ts +9 -2
  29. package/src/metadata/metadata-store.ts +1 -1
  30. package/src/pipeline/pipeline-stress.test.ts +226 -0
  31. package/src/pipeline/pipeline.test.ts +2 -214
  32. package/src/pipeline/pipeline.ts +47 -22
  33. package/src/space/control-pipeline.ts +47 -22
  34. package/src/space/data-pipeline.ts +20 -10
  35. package/src/space/space-manager.ts +1 -0
  36. package/src/space/space-protocol.ts +3 -1
  37. package/src/space/space.ts +1 -0
  38. package/src/testing/database-test-rig.ts +27 -2
  39. package/src/tests/database-unit.test.ts +39 -0
  40. package/dist/lib/browser/chunk-EJ35NHZV.mjs.map +0 -7
@@ -103,6 +103,7 @@ var import_context = require("@dxos/context");
103
103
  var import_echo_db = require("@dxos/echo-db");
104
104
  var import_invariant2 = require("@dxos/invariant");
105
105
  var import_log = require("@dxos/log");
106
+ var import_service = require("@dxos/protocols/proto/dxos/echo/service");
106
107
  var import_util = require("@dxos/util");
107
108
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service-host.ts";
108
109
  var DataServiceHost = class {
@@ -130,12 +131,19 @@ var DataServiceHost = class {
130
131
  objects
131
132
  }
132
133
  });
134
+ this._itemDemuxer.snapshot.on(ctx, (snapshot) => {
135
+ next({
136
+ action: import_service.EchoEvent.DatabaseAction.RESET,
137
+ batch: {
138
+ objects: snapshot.items
139
+ }
140
+ });
141
+ });
133
142
  this._itemDemuxer.mutation.on(ctx, (message) => {
134
- var _a;
135
143
  const { batch, meta } = message;
136
144
  (0, import_invariant2.invariant)(!meta.clientTag, "Unexpected client tag in mutation message", {
137
145
  F: __dxlog_file2,
138
- L: 61,
146
+ L: 68,
139
147
  S: this,
140
148
  A: [
141
149
  "!(meta as any).clientTag",
@@ -143,11 +151,11 @@ var DataServiceHost = class {
143
151
  ]
144
152
  });
145
153
  (0, import_log.log)("message", {
146
- batch,
154
+ batch: batch.objects?.length,
147
155
  meta
148
156
  }, {
149
157
  F: __dxlog_file2,
150
- L: 62,
158
+ L: 69,
151
159
  S: this,
152
160
  C: (f, a) => f(...a)
153
161
  });
@@ -155,7 +163,7 @@ var DataServiceHost = class {
155
163
  message.meta.feedKey,
156
164
  message.meta.seq
157
165
  ]);
158
- (_a = batch.objects) == null ? void 0 : _a.forEach((object) => {
166
+ batch.objects?.forEach((object) => {
159
167
  (0, import_echo_db.setMetadataOnObject)(object, {
160
168
  ...meta
161
169
  });
@@ -173,10 +181,9 @@ var DataServiceHost = class {
173
181
  });
174
182
  }
175
183
  async write(request) {
176
- var _a, _b;
177
184
  (0, import_invariant2.invariant)(!this._ctx.disposed, "Cannot write to closed DataServiceHost", {
178
185
  F: __dxlog_file2,
179
- L: 88,
186
+ L: 95,
180
187
  S: this,
181
188
  A: [
182
189
  "!this._ctx.disposed",
@@ -185,7 +192,7 @@ var DataServiceHost = class {
185
192
  });
186
193
  (0, import_invariant2.invariant)(this._writeStream, "Cannot write mutations in readonly mode", {
187
194
  F: __dxlog_file2,
188
- L: 89,
195
+ L: 96,
189
196
  S: this,
190
197
  A: [
191
198
  "this._writeStream",
@@ -194,10 +201,10 @@ var DataServiceHost = class {
194
201
  });
195
202
  (0, import_log.log)("write", {
196
203
  clientTag: request.clientTag,
197
- objectCount: (_b = (_a = request.batch.objects) == null ? void 0 : _a.length) != null ? _b : 0
204
+ objectCount: request.batch.objects?.length ?? 0
198
205
  }, {
199
206
  F: __dxlog_file2,
200
- L: 91,
207
+ L: 98,
201
208
  S: this,
202
209
  C: (f, a) => f(...a)
203
210
  });
@@ -211,7 +218,7 @@ var DataServiceHost = class {
211
218
  seq: receipt2.seq
212
219
  }, {
213
220
  F: __dxlog_file2,
214
- L: 100,
221
+ L: 107,
215
222
  S: this,
216
223
  C: (f, a) => f(...a)
217
224
  });
@@ -225,24 +232,18 @@ var DataServiceHost = class {
225
232
  return receipt;
226
233
  }
227
234
  };
228
- var createDataMessage = (batch) => {
229
- var _a;
230
- return {
231
- batch: {
232
- objects: (_a = batch.objects) == null ? void 0 : _a.map((object) => {
233
- var _a2;
234
- return {
235
- ...object,
236
- mutations: (_a2 = object.mutations) == null ? void 0 : _a2.map((mutation) => ({
237
- ...mutation,
238
- meta: void 0
239
- })),
240
- meta: void 0
241
- };
242
- })
243
- }
244
- };
245
- };
235
+ var createDataMessage = (batch) => ({
236
+ batch: {
237
+ objects: batch.objects?.map((object) => ({
238
+ ...object,
239
+ mutations: object.mutations?.map((mutation) => ({
240
+ ...mutation,
241
+ meta: void 0
242
+ })),
243
+ meta: void 0
244
+ }))
245
+ }
246
+ });
246
247
 
247
248
  // packages/core/echo/echo-pipeline/src/db-host/database-host.ts
248
249
  var import_echo_db2 = require("@dxos/echo-db");
@@ -271,8 +272,7 @@ var DatabaseHost = class {
271
272
  return this._itemDemuxer.createSnapshot();
272
273
  }
273
274
  createDataServiceHost() {
274
- var _a;
275
- return new DataServiceHost(this._itemManager, this._itemDemuxer, (_a = this._outboundStream) != null ? _a : void 0);
275
+ return new DataServiceHost(this._itemManager, this._itemDemuxer, this._outboundStream ?? void 0);
276
276
  }
277
277
  };
278
278
 
@@ -418,7 +418,7 @@ var DataServiceSubscriptions = class {
418
418
  C: (f, a) => f(...a)
419
419
  });
420
420
  const host = this._spaces.get(spaceKey);
421
- await (host == null ? void 0 : host.close());
421
+ await host?.close();
422
422
  this._spaces.delete(spaceKey);
423
423
  }
424
424
  getDataService(spaceKey) {
@@ -430,7 +430,6 @@ var DataServiceImpl = class {
430
430
  this._subscriptions = _subscriptions;
431
431
  }
432
432
  subscribe(request) {
433
- var _a;
434
433
  (0, import_invariant3.invariant)(request.spaceKey, void 0, {
435
434
  F: __dxlog_file3,
436
435
  L: 56,
@@ -440,11 +439,10 @@ var DataServiceImpl = class {
440
439
  ""
441
440
  ]
442
441
  });
443
- const host = (_a = this._subscriptions.getDataService(request.spaceKey)) != null ? _a : (0, import_debug.raise)(new Error(`space not found: ${request.spaceKey}`));
442
+ const host = this._subscriptions.getDataService(request.spaceKey) ?? (0, import_debug.raise)(new Error(`space not found: ${request.spaceKey}`));
444
443
  return host.subscribe();
445
444
  }
446
445
  write(request) {
447
- var _a;
448
446
  (0, import_invariant3.invariant)(request.spaceKey, void 0, {
449
447
  F: __dxlog_file3,
450
448
  L: 63,
@@ -463,7 +461,7 @@ var DataServiceImpl = class {
463
461
  ""
464
462
  ]
465
463
  });
466
- const host = (_a = this._subscriptions.getDataService(request.spaceKey)) != null ? _a : (0, import_debug.raise)(new Error(`space not found: ${request.spaceKey}`));
464
+ const host = this._subscriptions.getDataService(request.spaceKey) ?? (0, import_debug.raise)(new Error(`space not found: ${request.spaceKey}`));
467
465
  return host.write(request);
468
466
  }
469
467
  };
@@ -540,8 +538,8 @@ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipelin
540
538
  var emptyEchoMetadata = () => ({
541
539
  version: import_protocols4.STORAGE_VERSION,
542
540
  spaces: [],
543
- created: new Date(),
544
- updated: new Date()
541
+ created: /* @__PURE__ */ new Date(),
542
+ updated: /* @__PURE__ */ new Date()
545
543
  });
546
544
  var EchoMetadata = import_protocols4.schema.getCodecForType("dxos.echo.metadata.EchoMetadata");
547
545
  var MetadataStore = class {
@@ -555,22 +553,19 @@ var MetadataStore = class {
555
553
  return this._metadata;
556
554
  }
557
555
  get version() {
558
- var _a;
559
- return (_a = this._metadata.version) != null ? _a : 0;
556
+ return this._metadata.version ?? 0;
560
557
  }
561
558
  /**
562
559
  * Returns a list of currently saved spaces. The list and objects in it can be modified addSpace and
563
560
  * addSpaceFeed functions.
564
561
  */
565
562
  get spaces() {
566
- var _a;
567
- return (_a = this._metadata.spaces) != null ? _a : [];
563
+ return this._metadata.spaces ?? [];
568
564
  }
569
565
  /**
570
566
  * Loads metadata from persistent storage.
571
567
  */
572
568
  async load() {
573
- var _a;
574
569
  const file = this._directory.getOrCreateFile("EchoMetadata");
575
570
  try {
576
571
  const { size: fileLength } = await file.stat();
@@ -589,7 +584,10 @@ var MetadataStore = class {
589
584
  C: (f, a) => f(...a)
590
585
  });
591
586
  if (fileLength < dataSize + 8) {
592
- throw new import_errors.DataCorruptionError("Metadata size is smaller than expected.");
587
+ throw new import_errors.DataCorruptionError("Metadata size is smaller than expected.", {
588
+ fileLength,
589
+ dataSize
590
+ });
593
591
  }
594
592
  const data = await file.read(8, dataSize);
595
593
  const calculatedChecksum = import_crc_32.default.buf(data);
@@ -597,9 +595,8 @@ var MetadataStore = class {
597
595
  throw new import_errors.DataCorruptionError("Metadata checksum is invalid.");
598
596
  }
599
597
  this._metadata = EchoMetadata.decode(data);
600
- (_a = this._metadata.spaces) == null ? void 0 : _a.forEach((space) => {
601
- var _a2;
602
- (_a2 = space.state) != null ? _a2 : space.state = import_services.SpaceState.ACTIVE;
598
+ this._metadata.spaces?.forEach((space) => {
599
+ space.state ??= import_services.SpaceState.ACTIVE;
603
600
  });
604
601
  } catch (err) {
605
602
  import_log3.log.error("failed to load metadata", {
@@ -616,12 +613,11 @@ var MetadataStore = class {
616
613
  }
617
614
  }
618
615
  async _save() {
619
- var _a;
620
616
  const data = {
621
617
  ...this._metadata,
622
618
  version: import_protocols4.STORAGE_VERSION,
623
- created: (_a = this._metadata.created) != null ? _a : new Date(),
624
- updated: new Date()
619
+ created: this._metadata.created ?? /* @__PURE__ */ new Date(),
620
+ updated: /* @__PURE__ */ new Date()
625
621
  };
626
622
  this.update.emit(data);
627
623
  const file = this._directory.getOrCreateFile("EchoMetadata");
@@ -647,8 +643,7 @@ var MetadataStore = class {
647
643
  }
648
644
  }
649
645
  _getSpace(spaceKey) {
650
- var _a;
651
- if ((_a = this._metadata.identity) == null ? void 0 : _a.haloSpace.key.equals(spaceKey)) {
646
+ if (this._metadata.identity?.haloSpace.key.equals(spaceKey)) {
652
647
  return this._metadata.identity.haloSpace;
653
648
  }
654
649
  const space = this.spaces.find((space2) => space2.key === spaceKey);
@@ -693,8 +688,7 @@ var MetadataStore = class {
693
688
  await this._save();
694
689
  }
695
690
  async addSpace(record) {
696
- var _a, _b, _c;
697
- (0, import_invariant4.invariant)(!((_a = this._metadata.spaces) != null ? _a : []).find((space) => space.key === record.key), "Cannot overwrite existing space in metadata", {
691
+ (0, import_invariant4.invariant)(!(this._metadata.spaces ?? []).find((space) => space.key === record.key), "Cannot overwrite existing space in metadata", {
698
692
  F: __dxlog_file4,
699
693
  L: 162,
700
694
  S: this,
@@ -703,7 +697,7 @@ var MetadataStore = class {
703
697
  "'Cannot overwrite existing space in metadata'"
704
698
  ]
705
699
  });
706
- ((_c = (_b = this._metadata).spaces) != null ? _c : _b.spaces = []).push(record);
700
+ (this._metadata.spaces ??= []).push(record);
707
701
  await this._save();
708
702
  }
709
703
  async setSpaceDataLatestTimeframe(spaceKey, timeframe) {
@@ -938,7 +932,6 @@ var PipelineState = class {
938
932
  * @param timeout Timeout in milliseconds to specify the maximum wait time.
939
933
  */
940
934
  async waitUntilReachedTargetTimeframe({ ctx = new import_context3.Context(), timeout, breakOnStall = true } = {}) {
941
- var _a;
942
935
  (0, import_log6.log)("waitUntilReachedTargetTimeframe", {
943
936
  timeout,
944
937
  current: this.timeframe,
@@ -949,7 +942,7 @@ var PipelineState = class {
949
942
  S: this,
950
943
  C: (f, a) => f(...a)
951
944
  });
952
- (_a = this._reachedTargetPromise) != null ? _a : this._reachedTargetPromise = Promise.race([
945
+ this._reachedTargetPromise ??= Promise.race([
953
946
  this._timeframeClock.update.waitForCondition(() => {
954
947
  return import_timeframe2.Timeframe.dependencies(this.targetTimeframe, this.timeframe).isEmpty();
955
948
  }),
@@ -996,9 +989,11 @@ var Pipeline = class {
996
989
  // Waits for the message consumer to process the message and yield control back to the pipeline.
997
990
  this._processingTrigger = new import_async4.Trigger().wake();
998
991
  this._pauseTrigger = new import_async4.Trigger().wake();
992
+ // Pending downloads.
993
+ this._downloads = new import_util4.ComplexMap((value) => import_keys3.PublicKey.hash(value.key));
999
994
  this._isStopping = false;
1000
995
  this._isStarted = false;
1001
- this._isOpen = false;
996
+ this._isBeingConsumed = false;
1002
997
  this._isPaused = false;
1003
998
  }
1004
999
  get state() {
@@ -1007,7 +1002,7 @@ var Pipeline = class {
1007
1002
  get writer() {
1008
1003
  (0, import_invariant6.invariant)(this._writer, "Writer not set.", {
1009
1004
  F: __dxlog_file7,
1010
- L: 230,
1005
+ L: 233,
1011
1006
  S: this,
1012
1007
  A: [
1013
1008
  "this._writer",
@@ -1029,12 +1024,14 @@ var Pipeline = class {
1029
1024
  if (this._feedSetIterator) {
1030
1025
  await this._feedSetIterator.addFeed(feed);
1031
1026
  }
1032
- this._setFeedDownloadState(feed);
1027
+ if (this._isStarted && !this._isPaused) {
1028
+ this._setFeedDownloadState(feed);
1029
+ }
1033
1030
  }
1034
1031
  setWriteFeed(feed) {
1035
1032
  (0, import_invariant6.invariant)(!this._writer, "Writer already set.", {
1036
1033
  F: __dxlog_file7,
1037
- L: 253,
1034
+ L: 260,
1038
1035
  S: this,
1039
1036
  A: [
1040
1037
  "!this._writer",
@@ -1043,7 +1040,7 @@ var Pipeline = class {
1043
1040
  });
1044
1041
  (0, import_invariant6.invariant)(feed.properties.writable, "Feed must be writable.", {
1045
1042
  F: __dxlog_file7,
1046
- L: 254,
1043
+ L: 261,
1047
1044
  S: this,
1048
1045
  A: [
1049
1046
  "feed.properties.writable",
@@ -1058,7 +1055,7 @@ var Pipeline = class {
1058
1055
  async start() {
1059
1056
  (0, import_invariant6.invariant)(!this._isStarted, "Pipeline is already started.", {
1060
1057
  F: __dxlog_file7,
1061
- L: 267,
1058
+ L: 274,
1062
1059
  S: this,
1063
1060
  A: [
1064
1061
  "!this._isStarted",
@@ -1067,7 +1064,7 @@ var Pipeline = class {
1067
1064
  });
1068
1065
  (0, import_log6.log)("starting...", void 0, {
1069
1066
  F: __dxlog_file7,
1070
- L: 268,
1067
+ L: 275,
1071
1068
  S: this,
1072
1069
  C: (f, a) => f(...a)
1073
1070
  });
@@ -1076,21 +1073,29 @@ var Pipeline = class {
1076
1073
  this._isStarted = true;
1077
1074
  (0, import_log6.log)("started", void 0, {
1078
1075
  F: __dxlog_file7,
1079
- L: 272,
1076
+ L: 279,
1080
1077
  S: this,
1081
1078
  C: (f, a) => f(...a)
1082
1079
  });
1080
+ if (!this._isPaused) {
1081
+ for (const feed of this._feeds.values()) {
1082
+ this._setFeedDownloadState(feed);
1083
+ }
1084
+ }
1083
1085
  }
1084
1086
  async stop() {
1085
- var _a;
1086
1087
  (0, import_log6.log)("stopping...", void 0, {
1087
1088
  F: __dxlog_file7,
1088
- L: 277,
1089
+ L: 290,
1089
1090
  S: this,
1090
1091
  C: (f, a) => f(...a)
1091
1092
  });
1092
1093
  this._isStopping = true;
1093
- await ((_a = this._feedSetIterator) == null ? void 0 : _a.close());
1094
+ for (const [feed, handle] of this._downloads.entries()) {
1095
+ feed.undownload(handle);
1096
+ }
1097
+ this._downloads.clear();
1098
+ await this._feedSetIterator?.close();
1094
1099
  await this._processingTrigger.wait();
1095
1100
  await this._state._ctx.dispose();
1096
1101
  this._state._ctx = new import_context3.Context();
@@ -1098,7 +1103,7 @@ var Pipeline = class {
1098
1103
  this._isStarted = false;
1099
1104
  (0, import_log6.log)("stopped", void 0, {
1100
1105
  F: __dxlog_file7,
1101
- L: 285,
1106
+ L: 302,
1102
1107
  S: this,
1103
1108
  C: (f, a) => f(...a)
1104
1109
  });
@@ -1110,7 +1115,7 @@ var Pipeline = class {
1110
1115
  async setCursor(timeframe) {
1111
1116
  (0, import_invariant6.invariant)(!this._isStarted || this._isPaused, "Invalid state.", {
1112
1117
  F: __dxlog_file7,
1113
- L: 294,
1118
+ L: 311,
1114
1119
  S: this,
1115
1120
  A: [
1116
1121
  "!this._isStarted || this._isPaused",
@@ -1119,9 +1124,6 @@ var Pipeline = class {
1119
1124
  });
1120
1125
  this._state._startTimeframe = timeframe;
1121
1126
  this._timeframeClock.setTimeframe(timeframe);
1122
- for (const feed of this._feeds.values()) {
1123
- this._setFeedDownloadState(feed);
1124
- }
1125
1127
  if (this._feedSetIterator) {
1126
1128
  await this._feedSetIterator.close();
1127
1129
  await this._initIterator();
@@ -1132,15 +1134,6 @@ var Pipeline = class {
1132
1134
  * Calling pause while processing will cause a deadlock.
1133
1135
  */
1134
1136
  async pause() {
1135
- (0, import_invariant6.invariant)(this._isStarted, "Pipeline is not open.", {
1136
- F: __dxlog_file7,
1137
- L: 316,
1138
- S: this,
1139
- A: [
1140
- "this._isStarted",
1141
- "'Pipeline is not open.'"
1142
- ]
1143
- });
1144
1137
  if (this._isPaused) {
1145
1138
  return;
1146
1139
  }
@@ -1149,18 +1142,9 @@ var Pipeline = class {
1149
1142
  this._isPaused = true;
1150
1143
  }
1151
1144
  async unpause() {
1152
- (0, import_invariant6.invariant)(this._isStarted, "Pipeline is not open.", {
1153
- F: __dxlog_file7,
1154
- L: 328,
1155
- S: this,
1156
- A: [
1157
- "this._isStarted",
1158
- "'Pipeline is not open.'"
1159
- ]
1160
- });
1161
1145
  (0, import_invariant6.invariant)(this._isPaused, "Pipeline is not paused.", {
1162
1146
  F: __dxlog_file7,
1163
- L: 329,
1147
+ L: 340,
1164
1148
  S: this,
1165
1149
  A: [
1166
1150
  "this._isPaused",
@@ -1169,25 +1153,28 @@ var Pipeline = class {
1169
1153
  });
1170
1154
  this._pauseTrigger.wake();
1171
1155
  this._isPaused = false;
1156
+ for (const feed of this._feeds.values()) {
1157
+ this._setFeedDownloadState(feed);
1158
+ }
1172
1159
  }
1173
1160
  /**
1174
1161
  * Starts to iterate over the ordered messages from the added feeds.
1175
1162
  * Updates the timeframe clock after the message has bee processed.
1176
1163
  */
1177
1164
  async *consume() {
1178
- (0, import_invariant6.invariant)(!this._isOpen, "Pipeline is already being consumed.", {
1165
+ (0, import_invariant6.invariant)(!this._isBeingConsumed, "Pipeline is already being consumed.", {
1179
1166
  F: __dxlog_file7,
1180
- L: 340,
1167
+ L: 355,
1181
1168
  S: this,
1182
1169
  A: [
1183
- "!this._isOpen",
1170
+ "!this._isBeingConsumed",
1184
1171
  "'Pipeline is already being consumed.'"
1185
1172
  ]
1186
1173
  });
1187
- this._isOpen = true;
1174
+ this._isBeingConsumed = true;
1188
1175
  (0, import_invariant6.invariant)(this._feedSetIterator, "Iterator not initialized.", {
1189
1176
  F: __dxlog_file7,
1190
- L: 343,
1177
+ L: 358,
1191
1178
  S: this,
1192
1179
  A: [
1193
1180
  "this._feedSetIterator",
@@ -1201,7 +1188,7 @@ var Pipeline = class {
1201
1188
  if (lastFeedSetIterator !== this._feedSetIterator) {
1202
1189
  (0, import_invariant6.invariant)(this._feedSetIterator, "Iterator not initialized.", {
1203
1190
  F: __dxlog_file7,
1204
- L: 352,
1191
+ L: 367,
1205
1192
  S: this,
1206
1193
  A: [
1207
1194
  "this._feedSetIterator",
@@ -1213,7 +1200,7 @@ var Pipeline = class {
1213
1200
  }
1214
1201
  const { done, value } = await iterable.next();
1215
1202
  if (!done) {
1216
- const block = value != null ? value : (0, import_debug3.failUndefined)();
1203
+ const block = value ?? (0, import_debug3.failUndefined)();
1217
1204
  this._processingTrigger.reset();
1218
1205
  this._timeframeClock.updatePendingTimeframe(import_keys3.PublicKey.from(block.feedKey), block.seq);
1219
1206
  yield block;
@@ -1221,33 +1208,40 @@ var Pipeline = class {
1221
1208
  this._timeframeClock.updateTimeframe();
1222
1209
  }
1223
1210
  }
1224
- this._isOpen = false;
1211
+ this._isBeingConsumed = false;
1225
1212
  }
1226
1213
  _setFeedDownloadState(feed) {
1227
- var _a;
1214
+ let handle = this._downloads.get(feed);
1215
+ if (handle) {
1216
+ feed.undownload(handle);
1217
+ }
1228
1218
  const timeframe = this._state._startTimeframe;
1229
- const seq = (_a = timeframe.get(feed.key)) != null ? _a : 0;
1230
- feed.undownload({
1231
- callback: () => (0, import_log6.log)("undownload", void 0, {
1232
- F: __dxlog_file7,
1233
- L: 377,
1234
- S: this,
1235
- C: (f, a) => f(...a)
1236
- })
1219
+ const seq = timeframe.get(feed.key) ?? -1;
1220
+ import_log6.log.info("download", {
1221
+ feed: feed.key.truncate(),
1222
+ seq,
1223
+ length: feed.length
1224
+ }, {
1225
+ F: __dxlog_file7,
1226
+ L: 396,
1227
+ S: this,
1228
+ C: (f, a) => f(...a)
1237
1229
  });
1238
- feed.download({
1230
+ handle = feed.download({
1239
1231
  start: seq + 1,
1240
1232
  linear: true
1241
- }).catch((err) => {
1242
- (0, import_log6.log)("failed to download feed", {
1243
- err
1244
- }, {
1245
- F: __dxlog_file7,
1246
- L: 379,
1247
- S: this,
1248
- C: (f, a) => f(...a)
1249
- });
1233
+ }, (err, data) => {
1234
+ if (err) {
1235
+ } else {
1236
+ import_log6.log.info("data", data, {
1237
+ F: __dxlog_file7,
1238
+ L: 401,
1239
+ S: this,
1240
+ C: (f, a) => f(...a)
1241
+ });
1242
+ }
1250
1243
  });
1244
+ this._downloads.set(feed, handle);
1251
1245
  }
1252
1246
  async _initIterator() {
1253
1247
  this._feedSetIterator = new import_feed_store.FeedSetIterator(createMessageSelector(this._timeframeClock), {
@@ -1257,7 +1251,7 @@ var Pipeline = class {
1257
1251
  this._feedSetIterator.stalled.on((iterator) => {
1258
1252
  import_log6.log.warn(`Stalled after ${iterator.options.stallTimeout}ms with ${iterator.size} feeds.`, void 0, {
1259
1253
  F: __dxlog_file7,
1260
- L: 390,
1254
+ L: 415,
1261
1255
  S: this,
1262
1256
  C: (f, a) => f(...a)
1263
1257
  });
@@ -1349,7 +1343,7 @@ var AuthExtension = class extends import_teleport.RpcExtension {
1349
1343
  const { credential } = await this.rpc.AuthService.authenticate({
1350
1344
  challenge
1351
1345
  });
1352
- (0, import_invariant7.invariant)((credential == null ? void 0 : credential.length) > 0, "invalid credential", {
1346
+ (0, import_invariant7.invariant)(credential?.length > 0, "invalid credential", {
1353
1347
  F: __dxlog_file8,
1354
1348
  L: 69,
1355
1349
  S: this,
@@ -1392,22 +1386,36 @@ var import_async7 = require("@dxos/async");
1392
1386
  var import_invariant9 = require("@dxos/invariant");
1393
1387
  var import_log10 = require("@dxos/log");
1394
1388
  var import_credentials4 = require("@dxos/protocols/proto/dxos/halo/credentials");
1395
- var import_tracing = require("@dxos/tracing");
1389
+ var import_tracing3 = require("@dxos/tracing");
1396
1390
  var import_util7 = require("@dxos/util");
1397
1391
 
1398
1392
  // packages/core/echo/echo-pipeline/src/space/control-pipeline.ts
1393
+ var import_context5 = require("@dxos/context");
1399
1394
  var import_credentials = require("@dxos/credentials");
1400
1395
  var import_keys4 = require("@dxos/keys");
1401
1396
  var import_log8 = require("@dxos/log");
1402
1397
  var import_credentials2 = require("@dxos/protocols/proto/dxos/halo/credentials");
1403
1398
  var import_timeframe3 = require("@dxos/timeframe");
1399
+ var import_tracing = require("@dxos/tracing");
1404
1400
  var import_util5 = require("@dxos/util");
1401
+ function _ts_decorate5(decorators, target, key, desc) {
1402
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1403
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1404
+ r = Reflect.decorate(decorators, target, key, desc);
1405
+ else
1406
+ for (var i = decorators.length - 1; i >= 0; i--)
1407
+ if (d = decorators[i])
1408
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1409
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1410
+ }
1405
1411
  var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/control-pipeline.ts";
1406
1412
  var TIMEFRAME_SAVE_DEBOUNCE_INTERVAL = 500;
1407
- var ControlPipeline = class {
1413
+ var ControlPipeline = class ControlPipeline2 {
1408
1414
  constructor({ spaceKey, genesisFeed, feedProvider, metadataStore }) {
1409
1415
  this._lastTimeframeSaveTime = Date.now();
1410
1416
  this.onFeedAdmitted = new import_util5.Callback();
1417
+ this._usage = new import_tracing.TimeUsageCounter();
1418
+ this._mutations = new import_tracing.TimeSeriesCounter();
1411
1419
  this._spaceKey = spaceKey;
1412
1420
  this._metadata = metadataStore;
1413
1421
  this._pipeline = new Pipeline();
@@ -1418,7 +1426,7 @@ var ControlPipeline = class {
1418
1426
  key: info.key
1419
1427
  }, {
1420
1428
  F: __dxlog_file9,
1421
- L: 51,
1429
+ L: 61,
1422
1430
  S: this,
1423
1431
  C: (f, a) => f(...a)
1424
1432
  });
@@ -1429,7 +1437,7 @@ var ControlPipeline = class {
1429
1437
  } catch (err) {
1430
1438
  import_log8.log.catch(err, void 0, {
1431
1439
  F: __dxlog_file9,
1432
- L: 59,
1440
+ L: 69,
1433
1441
  S: this,
1434
1442
  C: (f, a) => f(...a)
1435
1443
  });
@@ -1452,57 +1460,66 @@ var ControlPipeline = class {
1452
1460
  async start() {
1453
1461
  (0, import_log8.log)("starting...", void 0, {
1454
1462
  F: __dxlog_file9,
1455
- L: 83,
1463
+ L: 93,
1456
1464
  S: this,
1457
1465
  C: (f, a) => f(...a)
1458
1466
  });
1459
1467
  setTimeout(async () => {
1460
- for await (const msg of this._pipeline.consume()) {
1461
- try {
1462
- (0, import_log8.log)("processing", {
1463
- key: msg.feedKey,
1464
- seq: msg.seq
1465
- }, {
1466
- F: __dxlog_file9,
1467
- L: 88,
1468
- S: this,
1469
- C: (f, a) => f(...a)
1470
- });
1471
- if (msg.data.payload.credential) {
1472
- const timer = import_util5.tracer.mark("dxos.echo.pipeline.control");
1473
- const result = await this._spaceStateMachine.process(msg.data.payload.credential.credential, import_keys4.PublicKey.from(msg.feedKey));
1474
- timer.end();
1475
- if (!result) {
1476
- import_log8.log.warn("processing failed", {
1477
- msg
1478
- }, {
1479
- F: __dxlog_file9,
1480
- L: 98,
1481
- S: this,
1482
- C: (f, a) => f(...a)
1483
- });
1484
- } else {
1485
- await this._noteTargetStateIfNeeded(this._pipeline.state.pendingTimeframe);
1486
- }
1487
- }
1488
- } catch (err) {
1489
- import_log8.log.catch(err, void 0, {
1490
- F: __dxlog_file9,
1491
- L: 104,
1492
- S: this,
1493
- C: (f, a) => f(...a)
1494
- });
1495
- }
1496
- }
1468
+ void this._consumePipeline(new import_context5.Context());
1497
1469
  });
1498
1470
  await this._pipeline.start();
1499
1471
  (0, import_log8.log)("started", void 0, {
1500
1472
  F: __dxlog_file9,
1501
- L: 110,
1473
+ L: 99,
1502
1474
  S: this,
1503
1475
  C: (f, a) => f(...a)
1504
1476
  });
1505
1477
  }
1478
+ async _consumePipeline(ctx) {
1479
+ for await (const msg of this._pipeline.consume()) {
1480
+ const span = this._usage.beginRecording();
1481
+ this._mutations.inc();
1482
+ try {
1483
+ await this._processMessage(ctx, msg);
1484
+ } catch (err) {
1485
+ import_log8.log.catch(err, void 0, {
1486
+ F: __dxlog_file9,
1487
+ L: 111,
1488
+ S: this,
1489
+ C: (f, a) => f(...a)
1490
+ });
1491
+ }
1492
+ span.end();
1493
+ }
1494
+ }
1495
+ async _processMessage(ctx, msg) {
1496
+ (0, import_log8.log)("processing", {
1497
+ key: msg.feedKey,
1498
+ seq: msg.seq
1499
+ }, {
1500
+ F: __dxlog_file9,
1501
+ L: 121,
1502
+ S: this,
1503
+ C: (f, a) => f(...a)
1504
+ });
1505
+ if (msg.data.payload.credential) {
1506
+ const timer = import_util5.tracer.mark("dxos.echo.pipeline.control");
1507
+ const result = await this._spaceStateMachine.process(msg.data.payload.credential.credential, import_keys4.PublicKey.from(msg.feedKey));
1508
+ timer.end();
1509
+ if (!result) {
1510
+ import_log8.log.warn("processing failed", {
1511
+ msg
1512
+ }, {
1513
+ F: __dxlog_file9,
1514
+ L: 131,
1515
+ S: this,
1516
+ C: (f, a) => f(...a)
1517
+ });
1518
+ } else {
1519
+ await this._noteTargetStateIfNeeded(this._pipeline.state.pendingTimeframe);
1520
+ }
1521
+ }
1522
+ }
1506
1523
  async _noteTargetStateIfNeeded(timeframe) {
1507
1524
  if (Date.now() - this._lastTimeframeSaveTime > TIMEFRAME_SAVE_DEBOUNCE_INTERVAL) {
1508
1525
  this._lastTimeframeSaveTime = Date.now();
@@ -1512,7 +1529,7 @@ var ControlPipeline = class {
1512
1529
  async stop() {
1513
1530
  (0, import_log8.log)("stopping...", void 0, {
1514
1531
  F: __dxlog_file9,
1515
- L: 124,
1532
+ L: 149,
1516
1533
  S: this,
1517
1534
  C: (f, a) => f(...a)
1518
1535
  });
@@ -1520,39 +1537,54 @@ var ControlPipeline = class {
1520
1537
  await this._saveTargetTimeframe(this._pipeline.state.timeframe);
1521
1538
  (0, import_log8.log)("stopped", void 0, {
1522
1539
  F: __dxlog_file9,
1523
- L: 127,
1540
+ L: 152,
1524
1541
  S: this,
1525
1542
  C: (f, a) => f(...a)
1526
1543
  });
1527
1544
  }
1528
1545
  async _saveTargetTimeframe(timeframe) {
1529
- var _a;
1530
1546
  try {
1531
- const newTimeframe = import_timeframe3.Timeframe.merge((_a = this._targetTimeframe) != null ? _a : new import_timeframe3.Timeframe(), timeframe);
1547
+ const newTimeframe = import_timeframe3.Timeframe.merge(this._targetTimeframe ?? new import_timeframe3.Timeframe(), timeframe);
1532
1548
  await this._metadata.setSpaceControlLatestTimeframe(this._spaceKey, newTimeframe);
1533
1549
  this._targetTimeframe = newTimeframe;
1534
1550
  } catch (err) {
1535
1551
  (0, import_log8.log)(err, void 0, {
1536
1552
  F: __dxlog_file9,
1537
- L: 136,
1553
+ L: 161,
1538
1554
  S: this,
1539
1555
  C: (f, a) => f(...a)
1540
1556
  });
1541
1557
  }
1542
1558
  }
1543
1559
  };
1560
+ _ts_decorate5([
1561
+ import_tracing.trace.metricsCounter()
1562
+ ], ControlPipeline.prototype, "_usage", void 0);
1563
+ _ts_decorate5([
1564
+ import_tracing.trace.metricsCounter()
1565
+ ], ControlPipeline.prototype, "_mutations", void 0);
1566
+ _ts_decorate5([
1567
+ import_tracing.trace.span()
1568
+ ], ControlPipeline.prototype, "_consumePipeline", null);
1569
+ _ts_decorate5([
1570
+ import_tracing.trace.span()
1571
+ ], ControlPipeline.prototype, "_processMessage", null);
1572
+ ControlPipeline = _ts_decorate5([
1573
+ import_tracing.trace.resource()
1574
+ ], ControlPipeline);
1544
1575
 
1545
1576
  // packages/core/echo/echo-pipeline/src/space/data-pipeline.ts
1546
1577
  var import_async6 = require("@dxos/async");
1547
- var import_context5 = require("@dxos/context");
1578
+ var import_context6 = require("@dxos/context");
1548
1579
  var import_credentials3 = require("@dxos/credentials");
1549
1580
  var import_echo_db3 = require("@dxos/echo-db");
1550
1581
  var import_errors2 = require("@dxos/errors");
1551
1582
  var import_invariant8 = require("@dxos/invariant");
1552
1583
  var import_log9 = require("@dxos/log");
1553
1584
  var import_timeframe4 = require("@dxos/timeframe");
1585
+ var import_tracing2 = require("@dxos/tracing");
1554
1586
  var import_util6 = require("@dxos/util");
1555
- function _ts_decorate5(decorators, target, key, desc) {
1587
+ function _ts_decorate6(decorators, target, key, desc) {
1556
1588
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1557
1589
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1558
1590
  r = Reflect.decorate(decorators, target, key, desc);
@@ -1569,7 +1601,7 @@ var TIMEFRAME_SAVE_DEBOUNCE_INTERVAL2 = 500;
1569
1601
  var DataPipeline = class DataPipeline2 {
1570
1602
  constructor(_params) {
1571
1603
  this._params = _params;
1572
- this._ctx = new import_context5.Context();
1604
+ this._ctx = new import_context6.Context();
1573
1605
  this._pipeline = void 0;
1574
1606
  this._targetTimeframe = void 0;
1575
1607
  this._lastAutomaticSnapshotTimeframe = new import_timeframe4.Timeframe();
@@ -1577,6 +1609,8 @@ var DataPipeline = class DataPipeline2 {
1577
1609
  this._lastTimeframeSaveTime = 0;
1578
1610
  this._lastSnapshotSaveTime = 0;
1579
1611
  this._lastProcessedEpoch = -1;
1612
+ this._usage = new import_tracing2.TimeUsageCounter();
1613
+ this._mutations = new import_tracing2.TimeSeriesCounter();
1580
1614
  this.currentEpoch = void 0;
1581
1615
  this.appliedEpoch = void 0;
1582
1616
  this.onNewEpoch = new import_async6.Event();
@@ -1588,13 +1622,11 @@ var DataPipeline = class DataPipeline2 {
1588
1622
  return this._pipeline;
1589
1623
  }
1590
1624
  get pipelineState() {
1591
- var _a;
1592
- return (_a = this._pipeline) == null ? void 0 : _a.state;
1625
+ return this._pipeline?.state;
1593
1626
  }
1594
1627
  setTargetTimeframe(timeframe) {
1595
- var _a;
1596
1628
  this._targetTimeframe = timeframe;
1597
- (_a = this._pipeline) == null ? void 0 : _a.state.setTargetTimeframe(timeframe);
1629
+ this._pipeline?.state.setTargetTimeframe(timeframe);
1598
1630
  }
1599
1631
  async processCredential(credential) {
1600
1632
  if (!(0, import_credentials3.checkCredentialType)(credential, "dxos.halo.credentials.Epoch")) {
@@ -1611,8 +1643,8 @@ var DataPipeline = class DataPipeline2 {
1611
1643
  }
1612
1644
  this._pipeline = new Pipeline();
1613
1645
  await this._params.onPipelineCreated(this._pipeline);
1614
- await this._pipeline.start();
1615
1646
  await this._pipeline.pause();
1647
+ await this._pipeline.start();
1616
1648
  if (this._targetTimeframe) {
1617
1649
  this._pipeline.state.setTargetTimeframe(this._targetTimeframe);
1618
1650
  }
@@ -1620,7 +1652,7 @@ var DataPipeline = class DataPipeline2 {
1620
1652
  write: (data, options) => {
1621
1653
  (0, import_invariant8.invariant)(this._pipeline, "Pipeline is not initialized.", {
1622
1654
  F: __dxlog_file10,
1623
- L: 145,
1655
+ L: 154,
1624
1656
  S: this,
1625
1657
  A: [
1626
1658
  "this._pipeline",
@@ -1629,7 +1661,7 @@ var DataPipeline = class DataPipeline2 {
1629
1661
  });
1630
1662
  (0, import_invariant8.invariant)(this.currentEpoch, "Epoch is not initialized.", {
1631
1663
  F: __dxlog_file10,
1632
- L: 146,
1664
+ L: 155,
1633
1665
  S: this,
1634
1666
  A: [
1635
1667
  "this.currentEpoch",
@@ -1650,19 +1682,18 @@ var DataPipeline = class DataPipeline2 {
1650
1682
  this._isOpen = true;
1651
1683
  }
1652
1684
  async close() {
1653
- var _a, _b, _c;
1654
1685
  if (!this._isOpen) {
1655
1686
  return;
1656
1687
  }
1657
1688
  (0, import_log9.log)("close", void 0, {
1658
1689
  F: __dxlog_file10,
1659
- L: 170,
1690
+ L: 179,
1660
1691
  S: this,
1661
1692
  C: (f, a) => f(...a)
1662
1693
  });
1663
1694
  this._isOpen = false;
1664
1695
  await this._ctx.dispose();
1665
- await ((_a = this._pipeline) == null ? void 0 : _a.stop());
1696
+ await this._pipeline?.stop();
1666
1697
  try {
1667
1698
  await this._saveCache();
1668
1699
  if (this._pipeline) {
@@ -1671,14 +1702,14 @@ var DataPipeline = class DataPipeline2 {
1671
1702
  } catch (err) {
1672
1703
  import_log9.log.catch(err, void 0, {
1673
1704
  F: __dxlog_file10,
1674
- L: 183,
1705
+ L: 192,
1675
1706
  S: this,
1676
1707
  C: (f, a) => f(...a)
1677
1708
  });
1678
1709
  }
1679
- await ((_b = this.databaseHost) == null ? void 0 : _b.close());
1680
- await ((_c = this.itemManager) == null ? void 0 : _c.destroy());
1681
- this._ctx = new import_context5.Context();
1710
+ await this.databaseHost?.close();
1711
+ await this.itemManager?.destroy();
1712
+ this._ctx = new import_context6.Context();
1682
1713
  this._pipeline = void 0;
1683
1714
  this._targetTimeframe = void 0;
1684
1715
  this._lastAutomaticSnapshotTimeframe = new import_timeframe4.Timeframe();
@@ -1695,7 +1726,7 @@ var DataPipeline = class DataPipeline2 {
1695
1726
  }
1696
1727
  (0, import_invariant8.invariant)(this._pipeline, "Pipeline is not initialized.", {
1697
1728
  F: __dxlog_file10,
1698
- L: 206,
1729
+ L: 215,
1699
1730
  S: this,
1700
1731
  A: [
1701
1732
  "this._pipeline",
@@ -1703,13 +1734,15 @@ var DataPipeline = class DataPipeline2 {
1703
1734
  ]
1704
1735
  });
1705
1736
  for await (const msg of this._pipeline.consume()) {
1737
+ const span = this._usage.beginRecording();
1738
+ this._mutations.inc();
1706
1739
  const { feedKey, seq, data } = msg;
1707
1740
  (0, import_log9.log)("processing message", {
1708
1741
  feedKey,
1709
1742
  seq
1710
1743
  }, {
1711
1744
  F: __dxlog_file10,
1712
- L: 209,
1745
+ L: 221,
1713
1746
  S: this,
1714
1747
  C: (f, a) => f(...a)
1715
1748
  });
@@ -1721,7 +1754,7 @@ var DataPipeline = class DataPipeline2 {
1721
1754
  feedKey
1722
1755
  }, {
1723
1756
  F: __dxlog_file10,
1724
- L: 215,
1757
+ L: 227,
1725
1758
  S: this,
1726
1759
  C: (f, a) => f(...a)
1727
1760
  });
@@ -1744,7 +1777,7 @@ var DataPipeline = class DataPipeline2 {
1744
1777
  spaceKey: this._params.spaceKey.toHex()
1745
1778
  }, {
1746
1779
  F: __dxlog_file10,
1747
- L: 232,
1780
+ L: 244,
1748
1781
  S: this,
1749
1782
  C: (f, a) => f(...a)
1750
1783
  });
@@ -1753,17 +1786,18 @@ var DataPipeline = class DataPipeline2 {
1753
1786
  } catch (err) {
1754
1787
  import_log9.log.catch(err, void 0, {
1755
1788
  F: __dxlog_file10,
1756
- L: 242,
1789
+ L: 254,
1757
1790
  S: this,
1758
1791
  C: (f, a) => f(...a)
1759
1792
  });
1760
1793
  }
1794
+ span.end();
1761
1795
  }
1762
1796
  }
1763
1797
  _createSnapshot() {
1764
1798
  (0, import_invariant8.invariant)(this.databaseHost, "Database backend is not initialized.", {
1765
1799
  F: __dxlog_file10,
1766
- L: 248,
1800
+ L: 262,
1767
1801
  S: this,
1768
1802
  A: [
1769
1803
  "this.databaseHost",
@@ -1777,26 +1811,22 @@ var DataPipeline = class DataPipeline2 {
1777
1811
  };
1778
1812
  }
1779
1813
  async _saveTargetTimeframe(timeframe) {
1780
- var _a;
1781
- const newTimeframe = import_timeframe4.Timeframe.merge((_a = this._targetTimeframe) != null ? _a : new import_timeframe4.Timeframe(), timeframe);
1814
+ const newTimeframe = import_timeframe4.Timeframe.merge(this._targetTimeframe ?? new import_timeframe4.Timeframe(), timeframe);
1782
1815
  await this._params.metadataStore.setSpaceDataLatestTimeframe(this._params.spaceKey, newTimeframe);
1783
1816
  this._targetTimeframe = newTimeframe;
1784
1817
  }
1785
1818
  async _saveCache() {
1786
- var _a;
1787
1819
  const cache = {};
1788
1820
  try {
1789
- const propertiesItem = this.itemManager.items.find((item) => {
1790
- var _a2, _b;
1791
- return ((_a2 = item.modelMeta) == null ? void 0 : _a2.type) === "dxos:model/document" && ((_b = (0, import_echo_db3.getStateMachineFromItem)(item)) == null ? void 0 : _b.snapshot()).type === "dxos.sdk.client.Properties";
1792
- });
1821
+ const propertiesItem = this.itemManager.items.find((item) => item.modelMeta?.type === "dxos:model/document" && // TODO(burdon): Document?
1822
+ ((0, import_echo_db3.getStateMachineFromItem)(item)?.snapshot()).type === import_echo_db3.TYPE_PROPERTIES);
1793
1823
  if (propertiesItem) {
1794
- cache.properties = (_a = (0, import_echo_db3.getStateMachineFromItem)(propertiesItem)) == null ? void 0 : _a.snapshot();
1824
+ cache.properties = (0, import_echo_db3.getStateMachineFromItem)(propertiesItem)?.snapshot();
1795
1825
  }
1796
1826
  } catch (err) {
1797
1827
  import_log9.log.warn("Failed to cache properties", err, {
1798
1828
  F: __dxlog_file10,
1799
- L: 277,
1829
+ L: 291,
1800
1830
  S: this,
1801
1831
  C: (f, a) => f(...a)
1802
1832
  });
@@ -1813,24 +1843,23 @@ var DataPipeline = class DataPipeline2 {
1813
1843
  }
1814
1844
  }
1815
1845
  async _processEpochInSeparateTask(epoch) {
1816
- var _a;
1817
1846
  if (epoch.subject.assertion.number <= this._lastProcessedEpoch) {
1818
1847
  return;
1819
1848
  }
1820
- await ((_a = this._epochCtx) == null ? void 0 : _a.dispose());
1821
- const ctx = new import_context5.Context({
1849
+ await this._epochCtx?.dispose();
1850
+ const ctx = new import_context6.Context({
1822
1851
  onError: (err) => {
1823
1852
  if (err instanceof import_errors2.CancelledError) {
1824
1853
  (0, import_log9.log)("Epoch processing cancelled.", void 0, {
1825
1854
  F: __dxlog_file10,
1826
- L: 309,
1855
+ L: 323,
1827
1856
  S: this,
1828
1857
  C: (f, a) => f(...a)
1829
1858
  });
1830
1859
  } else {
1831
1860
  import_log9.log.catch(err, void 0, {
1832
1861
  F: __dxlog_file10,
1833
- L: 311,
1862
+ L: 325,
1834
1863
  S: this,
1835
1864
  C: (f, a) => f(...a)
1836
1865
  });
@@ -1842,14 +1871,6 @@ var DataPipeline = class DataPipeline2 {
1842
1871
  if (!this._isOpen) {
1843
1872
  return;
1844
1873
  }
1845
- (0, import_log9.log)("process epoch", {
1846
- epoch
1847
- }, {
1848
- F: __dxlog_file10,
1849
- L: 321,
1850
- S: this,
1851
- C: (f, a) => f(...a)
1852
- });
1853
1874
  await this._processEpoch(ctx, epoch.subject.assertion);
1854
1875
  this.appliedEpoch = epoch;
1855
1876
  this.onNewEpoch.emit(epoch);
@@ -1858,7 +1879,7 @@ var DataPipeline = class DataPipeline2 {
1858
1879
  async _processEpoch(ctx, epoch) {
1859
1880
  (0, import_invariant8.invariant)(this._isOpen, "Space is closed.", {
1860
1881
  F: __dxlog_file10,
1861
- L: 331,
1882
+ L: 344,
1862
1883
  S: this,
1863
1884
  A: [
1864
1885
  "this._isOpen",
@@ -1867,7 +1888,7 @@ var DataPipeline = class DataPipeline2 {
1867
1888
  });
1868
1889
  (0, import_invariant8.invariant)(this._pipeline, void 0, {
1869
1890
  F: __dxlog_file10,
1870
- L: 332,
1891
+ L: 345,
1871
1892
  S: this,
1872
1893
  A: [
1873
1894
  "this._pipeline",
@@ -1875,11 +1896,11 @@ var DataPipeline = class DataPipeline2 {
1875
1896
  ]
1876
1897
  });
1877
1898
  this._lastProcessedEpoch = epoch.number;
1878
- (0, import_log9.log)("Processing epoch", {
1879
- epoch
1899
+ (0, import_log9.log)("processing", {
1900
+ epoch: (0, import_log9.omit)(epoch, "proof")
1880
1901
  }, {
1881
1902
  F: __dxlog_file10,
1882
- L: 335,
1903
+ L: 348,
1883
1904
  S: this,
1884
1905
  C: (f, a) => f(...a)
1885
1906
  });
@@ -1887,9 +1908,9 @@ var DataPipeline = class DataPipeline2 {
1887
1908
  const snapshot = await this._params.snapshotManager.load(ctx, epoch.snapshotCid);
1888
1909
  this.databaseHost._itemDemuxer.restoreFromSnapshot(snapshot.database);
1889
1910
  }
1890
- (0, import_log9.log)("restarting pipeline for epoch", void 0, {
1911
+ (0, import_log9.log)("restarting pipeline from epoch", void 0, {
1891
1912
  F: __dxlog_file10,
1892
- L: 343,
1913
+ L: 354,
1893
1914
  S: this,
1894
1915
  C: (f, a) => f(...a)
1895
1916
  });
@@ -1900,7 +1921,7 @@ var DataPipeline = class DataPipeline2 {
1900
1921
  async waitUntilTimeframe(timeframe) {
1901
1922
  (0, import_invariant8.invariant)(this._pipeline, "Pipeline is not initialized.", {
1902
1923
  F: __dxlog_file10,
1903
- L: 351,
1924
+ L: 361,
1904
1925
  S: this,
1905
1926
  A: [
1906
1927
  "this._pipeline",
@@ -1912,7 +1933,7 @@ var DataPipeline = class DataPipeline2 {
1912
1933
  async createEpoch() {
1913
1934
  (0, import_invariant8.invariant)(this._pipeline, void 0, {
1914
1935
  F: __dxlog_file10,
1915
- L: 357,
1936
+ L: 367,
1916
1937
  S: this,
1917
1938
  A: [
1918
1939
  "this._pipeline",
@@ -1921,7 +1942,7 @@ var DataPipeline = class DataPipeline2 {
1921
1942
  });
1922
1943
  (0, import_invariant8.invariant)(this.currentEpoch, void 0, {
1923
1944
  F: __dxlog_file10,
1924
- L: 358,
1945
+ L: 368,
1925
1946
  S: this,
1926
1947
  A: [
1927
1948
  "this.currentEpoch",
@@ -1944,24 +1965,31 @@ var DataPipeline = class DataPipeline2 {
1944
1965
  await this.onNewEpoch.waitForCondition(() => !!this.currentEpoch);
1945
1966
  }
1946
1967
  };
1947
- _ts_decorate5([
1968
+ _ts_decorate6([
1969
+ import_tracing2.trace.metricsCounter()
1970
+ ], DataPipeline.prototype, "_usage", void 0);
1971
+ _ts_decorate6([
1972
+ import_tracing2.trace.metricsCounter()
1973
+ ], DataPipeline.prototype, "_mutations", void 0);
1974
+ _ts_decorate6([
1948
1975
  import_async6.synchronized
1949
1976
  ], DataPipeline.prototype, "open", null);
1950
- _ts_decorate5([
1977
+ _ts_decorate6([
1951
1978
  import_async6.synchronized
1952
1979
  ], DataPipeline.prototype, "close", null);
1953
- _ts_decorate5([
1980
+ _ts_decorate6([
1954
1981
  import_async6.synchronized
1955
1982
  ], DataPipeline.prototype, "_processEpoch", null);
1956
- _ts_decorate5([
1983
+ _ts_decorate6([
1957
1984
  import_async6.synchronized
1958
1985
  ], DataPipeline.prototype, "createEpoch", null);
1959
- DataPipeline = _ts_decorate5([
1960
- (0, import_async6.trackLeaks)("open", "close")
1986
+ DataPipeline = _ts_decorate6([
1987
+ (0, import_async6.trackLeaks)("open", "close"),
1988
+ import_tracing2.trace.resource()
1961
1989
  ], DataPipeline);
1962
1990
 
1963
1991
  // packages/core/echo/echo-pipeline/src/space/space.ts
1964
- function _ts_decorate6(decorators, target, key, desc) {
1992
+ function _ts_decorate7(decorators, target, key, desc) {
1965
1993
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1966
1994
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1967
1995
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2064,12 +2092,10 @@ var Space = class Space2 {
2064
2092
  return this._genesisFeedKey;
2065
2093
  }
2066
2094
  get controlFeedKey() {
2067
- var _a;
2068
- return (_a = this._controlFeed) == null ? void 0 : _a.key;
2095
+ return this._controlFeed?.key;
2069
2096
  }
2070
2097
  get dataFeedKey() {
2071
- var _a;
2072
- return (_a = this._dataFeed) == null ? void 0 : _a.key;
2098
+ return this._dataFeed?.key;
2073
2099
  }
2074
2100
  get spaceState() {
2075
2101
  return this._controlPipeline.spaceState;
@@ -2089,7 +2115,7 @@ var Space = class Space2 {
2089
2115
  setControlFeed(feed) {
2090
2116
  (0, import_invariant9.invariant)(!this._controlFeed, "Control feed already set.", {
2091
2117
  F: __dxlog_file11,
2092
- L: 183,
2118
+ L: 184,
2093
2119
  S: this,
2094
2120
  A: [
2095
2121
  "!this._controlFeed",
@@ -2101,10 +2127,9 @@ var Space = class Space2 {
2101
2127
  return this;
2102
2128
  }
2103
2129
  setDataFeed(feed) {
2104
- var _a;
2105
2130
  (0, import_invariant9.invariant)(!this._dataFeed, "Data feed already set.", {
2106
2131
  F: __dxlog_file11,
2107
- L: 190,
2132
+ L: 191,
2108
2133
  S: this,
2109
2134
  A: [
2110
2135
  "!this._dataFeed",
@@ -2112,7 +2137,7 @@ var Space = class Space2 {
2112
2137
  ]
2113
2138
  });
2114
2139
  this._dataFeed = feed;
2115
- (_a = this._dataPipeline.pipeline) == null ? void 0 : _a.setWriteFeed(feed);
2140
+ this._dataPipeline.pipeline?.setWriteFeed(feed);
2116
2141
  return this;
2117
2142
  }
2118
2143
  /**
@@ -2130,7 +2155,7 @@ var Space = class Space2 {
2130
2155
  async open(ctx) {
2131
2156
  (0, import_log10.log)("opening...", void 0, {
2132
2157
  F: __dxlog_file11,
2133
- L: 212,
2158
+ L: 213,
2134
2159
  S: this,
2135
2160
  C: (f, a) => f(...a)
2136
2161
  });
@@ -2143,7 +2168,7 @@ var Space = class Space2 {
2143
2168
  this._isOpen = true;
2144
2169
  (0, import_log10.log)("opened", void 0, {
2145
2170
  F: __dxlog_file11,
2146
- L: 223,
2171
+ L: 224,
2147
2172
  S: this,
2148
2173
  C: (f, a) => f(...a)
2149
2174
  });
@@ -2153,7 +2178,7 @@ var Space = class Space2 {
2153
2178
  key: this._key
2154
2179
  }, {
2155
2180
  F: __dxlog_file11,
2156
- L: 228,
2181
+ L: 229,
2157
2182
  S: this,
2158
2183
  C: (f, a) => f(...a)
2159
2184
  });
@@ -2167,7 +2192,7 @@ var Space = class Space2 {
2167
2192
  this._isOpen = false;
2168
2193
  (0, import_log10.log)("closed", void 0, {
2169
2194
  F: __dxlog_file11,
2170
- L: 241,
2195
+ L: 242,
2171
2196
  S: this,
2172
2197
  C: (f, a) => f(...a)
2173
2198
  });
@@ -2175,13 +2200,13 @@ var Space = class Space2 {
2175
2200
  async initializeDataPipeline() {
2176
2201
  (0, import_log10.log)("initializeDataPipeline", void 0, {
2177
2202
  F: __dxlog_file11,
2178
- L: 246,
2203
+ L: 247,
2179
2204
  S: this,
2180
2205
  C: (f, a) => f(...a)
2181
2206
  });
2182
2207
  (0, import_invariant9.invariant)(this._isOpen, "Space must be open to initialize data pipeline.", {
2183
2208
  F: __dxlog_file11,
2184
- L: 247,
2209
+ L: 248,
2185
2210
  S: this,
2186
2211
  A: [
2187
2212
  "this._isOpen",
@@ -2191,22 +2216,23 @@ var Space = class Space2 {
2191
2216
  await this._dataPipeline.open();
2192
2217
  }
2193
2218
  };
2194
- _ts_decorate6([
2195
- import_log10.logInfo
2219
+ _ts_decorate7([
2220
+ import_log10.logInfo,
2221
+ import_tracing3.trace.info()
2196
2222
  ], Space.prototype, "key", null);
2197
- _ts_decorate6([
2223
+ _ts_decorate7([
2198
2224
  import_async7.synchronized,
2199
- import_tracing.trace.span()
2225
+ import_tracing3.trace.span()
2200
2226
  ], Space.prototype, "open", null);
2201
- _ts_decorate6([
2227
+ _ts_decorate7([
2202
2228
  import_async7.synchronized
2203
2229
  ], Space.prototype, "close", null);
2204
- _ts_decorate6([
2230
+ _ts_decorate7([
2205
2231
  import_async7.synchronized
2206
2232
  ], Space.prototype, "initializeDataPipeline", null);
2207
- Space = _ts_decorate6([
2233
+ Space = _ts_decorate7([
2208
2234
  (0, import_async7.trackLeaks)("open", "close"),
2209
- import_tracing.trace.resource()
2235
+ import_tracing3.trace.resource()
2210
2236
  ], Space);
2211
2237
 
2212
2238
  // packages/core/echo/echo-pipeline/src/space/space-manager.ts
@@ -2226,7 +2252,7 @@ var import_teleport2 = require("@dxos/teleport");
2226
2252
  var import_teleport_extension_object_sync = require("@dxos/teleport-extension-object-sync");
2227
2253
  var import_teleport_extension_replicator = require("@dxos/teleport-extension-replicator");
2228
2254
  var import_util8 = require("@dxos/util");
2229
- function _ts_decorate7(decorators, target, key, desc) {
2255
+ function _ts_decorate8(decorators, target, key, desc) {
2230
2256
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2231
2257
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2232
2258
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2267,7 +2293,7 @@ var SpaceProtocol = class {
2267
2293
  key: feed.key
2268
2294
  }, {
2269
2295
  F: __dxlog_file12,
2270
- L: 95,
2296
+ L: 96,
2271
2297
  S: this,
2272
2298
  C: (f, a) => f(...a)
2273
2299
  });
@@ -2276,6 +2302,7 @@ var SpaceProtocol = class {
2276
2302
  session.replicator.addFeed(feed);
2277
2303
  }
2278
2304
  }
2305
+ // TODO(burdon): Rename open? Common open/close interfaces for all services?
2279
2306
  async start() {
2280
2307
  if (this._connection) {
2281
2308
  return;
@@ -2289,7 +2316,7 @@ var SpaceProtocol = class {
2289
2316
  await this.blobSync.open();
2290
2317
  (0, import_log11.log)("starting...", void 0, {
2291
2318
  F: __dxlog_file12,
2292
- L: 120,
2319
+ L: 122,
2293
2320
  S: this,
2294
2321
  C: (f, a) => f(...a)
2295
2322
  });
@@ -2299,11 +2326,11 @@ var SpaceProtocol = class {
2299
2326
  peerId: this._swarmIdentity.peerKey,
2300
2327
  topic,
2301
2328
  topology: new import_network_manager.MMSTTopology(topologyConfig),
2302
- label: `Protocol swarm: ${topic}`
2329
+ label: `space swarm ${topic.truncate()}`
2303
2330
  });
2304
2331
  (0, import_log11.log)("started", void 0, {
2305
2332
  F: __dxlog_file12,
2306
- L: 130,
2333
+ L: 132,
2307
2334
  S: this,
2308
2335
  C: (f, a) => f(...a)
2309
2336
  });
@@ -2313,14 +2340,14 @@ var SpaceProtocol = class {
2313
2340
  if (this._connection) {
2314
2341
  (0, import_log11.log)("stopping...", void 0, {
2315
2342
  F: __dxlog_file12,
2316
- L: 137,
2343
+ L: 139,
2317
2344
  S: this,
2318
2345
  C: (f, a) => f(...a)
2319
2346
  });
2320
2347
  await this._connection.close();
2321
2348
  (0, import_log11.log)("stopped", void 0, {
2322
2349
  F: __dxlog_file12,
2323
- L: 139,
2350
+ L: 141,
2324
2351
  S: this,
2325
2352
  C: (f, a) => f(...a)
2326
2353
  });
@@ -2343,10 +2370,10 @@ var SpaceProtocol = class {
2343
2370
  };
2344
2371
  }
2345
2372
  };
2346
- _ts_decorate7([
2373
+ _ts_decorate8([
2347
2374
  import_log11.logInfo
2348
2375
  ], SpaceProtocol.prototype, "_topic", void 0);
2349
- _ts_decorate7([
2376
+ _ts_decorate8([
2350
2377
  import_log11.logInfo
2351
2378
  ], SpaceProtocol.prototype, "_ownPeerKey", null);
2352
2379
  var AuthStatus;
@@ -2385,20 +2412,18 @@ var SpaceProtocolSession = class {
2385
2412
  provider: this._swarmIdentity.credentialProvider,
2386
2413
  verifier: this._swarmIdentity.credentialAuthenticator,
2387
2414
  onAuthSuccess: () => {
2388
- var _a;
2389
2415
  (0, import_log11.log)("Peer authenticated", void 0, {
2390
2416
  F: __dxlog_file12,
2391
- L: 236,
2417
+ L: 238,
2392
2418
  S: this,
2393
2419
  C: (f, a) => f(...a)
2394
2420
  });
2395
2421
  this._authStatus = AuthStatus.SUCCESS;
2396
- (_a = this._onSessionAuth) == null ? void 0 : _a.call(this, this._teleport);
2422
+ this._onSessionAuth?.(this._teleport);
2397
2423
  },
2398
2424
  onAuthFailure: () => {
2399
- var _a;
2400
2425
  this._authStatus = AuthStatus.FAILURE;
2401
- (_a = this._onAuthFailure) == null ? void 0 : _a.call(this, this._teleport);
2426
+ this._onAuthFailure?.(this._teleport);
2402
2427
  }
2403
2428
  }));
2404
2429
  this._teleport.addExtension("dxos.mesh.teleport.replicator", this.replicator);
@@ -2408,15 +2433,15 @@ var SpaceProtocolSession = class {
2408
2433
  await this._teleport.close();
2409
2434
  }
2410
2435
  };
2411
- _ts_decorate7([
2436
+ _ts_decorate8([
2412
2437
  import_log11.logInfo
2413
2438
  ], SpaceProtocolSession.prototype, "_wireParams", void 0);
2414
- _ts_decorate7([
2439
+ _ts_decorate8([
2415
2440
  import_log11.logInfo
2416
2441
  ], SpaceProtocolSession.prototype, "authStatus", null);
2417
2442
 
2418
2443
  // packages/core/echo/echo-pipeline/src/space/space-manager.ts
2419
- function _ts_decorate8(decorators, target, key, desc) {
2444
+ function _ts_decorate9(decorators, target, key, desc) {
2420
2445
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2421
2446
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2422
2447
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2450,7 +2475,6 @@ var SpaceManager = class SpaceManager2 {
2450
2475
  ].map((space) => space.close()));
2451
2476
  }
2452
2477
  async constructSpace({ metadata, swarmIdentity, onNetworkConnection, onAuthFailure, memberKey }) {
2453
- var _a;
2454
2478
  import_log12.log.trace("dxos.echo.space-manager.construct-space", import_protocols6.trace.begin({
2455
2479
  id: this._instanceId
2456
2480
  }), {
@@ -2467,7 +2491,7 @@ var SpaceManager = class SpaceManager2 {
2467
2491
  S: this,
2468
2492
  C: (f, a) => f(...a)
2469
2493
  });
2470
- const genesisFeed = await this._feedStore.openFeed((_a = metadata.genesisFeedKey) != null ? _a : (0, import_debug4.failUndefined)());
2494
+ const genesisFeed = await this._feedStore.openFeed(metadata.genesisFeedKey ?? (0, import_debug4.failUndefined)());
2471
2495
  const spaceKey = metadata.key;
2472
2496
  const protocol = new SpaceProtocol({
2473
2497
  topic: spaceKey,
@@ -2493,20 +2517,20 @@ var SpaceManager = class SpaceManager2 {
2493
2517
  id: this._instanceId
2494
2518
  }), {
2495
2519
  F: __dxlog_file13,
2496
- L: 125,
2520
+ L: 126,
2497
2521
  S: this,
2498
2522
  C: (f, a) => f(...a)
2499
2523
  });
2500
2524
  return space;
2501
2525
  }
2502
2526
  };
2503
- _ts_decorate8([
2527
+ _ts_decorate9([
2504
2528
  import_async8.synchronized
2505
2529
  ], SpaceManager.prototype, "open", null);
2506
- _ts_decorate8([
2530
+ _ts_decorate9([
2507
2531
  import_async8.synchronized
2508
2532
  ], SpaceManager.prototype, "close", null);
2509
- SpaceManager = _ts_decorate8([
2533
+ SpaceManager = _ts_decorate9([
2510
2534
  (0, import_async8.trackLeaks)("open", "close")
2511
2535
  ], SpaceManager);
2512
2536
  // Annotate the CommonJS export names for ESM import in node: