@dxos/echo-pipeline 0.1.56-main.09ca29d → 0.1.56-main.0e9f69e

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 (39) hide show
  1. package/dist/lib/browser/{chunk-7RPTJNLX.mjs → chunk-FCDYN74V.mjs} +284 -243
  2. package/dist/lib/browser/chunk-FCDYN74V.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 +14 -18
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/index.cjs +292 -251
  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 +305 -268
  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/db-host/snapshot-manager.d.ts.map +1 -1
  15. package/dist/types/src/pipeline/pipeline.d.ts +1 -1
  16. package/dist/types/src/pipeline/pipeline.d.ts.map +1 -1
  17. package/dist/types/src/space/control-pipeline.d.ts +4 -0
  18. package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
  19. package/dist/types/src/space/data-pipeline.d.ts +2 -0
  20. package/dist/types/src/space/data-pipeline.d.ts.map +1 -1
  21. package/dist/types/src/space/space.d.ts +2 -1
  22. package/dist/types/src/space/space.d.ts.map +1 -1
  23. package/dist/types/src/testing/database-test-rig.d.ts +4 -1
  24. package/dist/types/src/testing/database-test-rig.d.ts.map +1 -1
  25. package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
  26. package/package.json +32 -31
  27. package/src/db-host/data-service-host.ts +8 -1
  28. package/src/db-host/snapshot-manager.ts +0 -2
  29. package/src/metadata/metadata-store.ts +1 -1
  30. package/src/pipeline/pipeline.test.ts +42 -9
  31. package/src/pipeline/pipeline.ts +35 -12
  32. package/src/space/control-pipeline.ts +47 -22
  33. package/src/space/data-pipeline.ts +15 -2
  34. package/src/space/space.test.ts +10 -9
  35. package/src/space/space.ts +6 -1
  36. package/src/testing/database-test-rig.ts +27 -2
  37. package/src/testing/test-agent-builder.ts +2 -1
  38. package/src/tests/database-unit.test.ts +39 -0
  39. package/dist/lib/browser/chunk-7RPTJNLX.mjs.map +0 -7
@@ -39,6 +39,7 @@ import { Context } from "@dxos/context";
39
39
  import { tagMutationsInBatch, setMetadataOnObject } from "@dxos/echo-db";
40
40
  import { invariant as invariant2 } from "@dxos/invariant";
41
41
  import { log } from "@dxos/log";
42
+ import { EchoEvent } from "@dxos/protocols/proto/dxos/echo/service";
42
43
  import { ComplexMap } from "@dxos/util";
43
44
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service-host.ts";
44
45
  var DataServiceHost = class {
@@ -66,12 +67,19 @@ var DataServiceHost = class {
66
67
  objects
67
68
  }
68
69
  });
70
+ this._itemDemuxer.snapshot.on(ctx, (snapshot) => {
71
+ next({
72
+ action: EchoEvent.DatabaseAction.RESET,
73
+ batch: {
74
+ objects: snapshot.items
75
+ }
76
+ });
77
+ });
69
78
  this._itemDemuxer.mutation.on(ctx, (message) => {
70
- var _a;
71
79
  const { batch, meta } = message;
72
80
  invariant2(!meta.clientTag, "Unexpected client tag in mutation message", {
73
81
  F: __dxlog_file2,
74
- L: 61,
82
+ L: 68,
75
83
  S: this,
76
84
  A: [
77
85
  "!(meta as any).clientTag",
@@ -83,7 +91,7 @@ var DataServiceHost = class {
83
91
  meta
84
92
  }, {
85
93
  F: __dxlog_file2,
86
- L: 62,
94
+ L: 69,
87
95
  S: this,
88
96
  C: (f, a) => f(...a)
89
97
  });
@@ -91,7 +99,7 @@ var DataServiceHost = class {
91
99
  message.meta.feedKey,
92
100
  message.meta.seq
93
101
  ]);
94
- (_a = batch.objects) == null ? void 0 : _a.forEach((object) => {
102
+ batch.objects?.forEach((object) => {
95
103
  setMetadataOnObject(object, {
96
104
  ...meta
97
105
  });
@@ -109,10 +117,9 @@ var DataServiceHost = class {
109
117
  });
110
118
  }
111
119
  async write(request) {
112
- var _a, _b;
113
120
  invariant2(!this._ctx.disposed, "Cannot write to closed DataServiceHost", {
114
121
  F: __dxlog_file2,
115
- L: 88,
122
+ L: 95,
116
123
  S: this,
117
124
  A: [
118
125
  "!this._ctx.disposed",
@@ -121,7 +128,7 @@ var DataServiceHost = class {
121
128
  });
122
129
  invariant2(this._writeStream, "Cannot write mutations in readonly mode", {
123
130
  F: __dxlog_file2,
124
- L: 89,
131
+ L: 96,
125
132
  S: this,
126
133
  A: [
127
134
  "this._writeStream",
@@ -130,10 +137,10 @@ var DataServiceHost = class {
130
137
  });
131
138
  log("write", {
132
139
  clientTag: request.clientTag,
133
- objectCount: (_b = (_a = request.batch.objects) == null ? void 0 : _a.length) != null ? _b : 0
140
+ objectCount: request.batch.objects?.length ?? 0
134
141
  }, {
135
142
  F: __dxlog_file2,
136
- L: 91,
143
+ L: 98,
137
144
  S: this,
138
145
  C: (f, a) => f(...a)
139
146
  });
@@ -147,7 +154,7 @@ var DataServiceHost = class {
147
154
  seq: receipt2.seq
148
155
  }, {
149
156
  F: __dxlog_file2,
150
- L: 100,
157
+ L: 107,
151
158
  S: this,
152
159
  C: (f, a) => f(...a)
153
160
  });
@@ -161,24 +168,18 @@ var DataServiceHost = class {
161
168
  return receipt;
162
169
  }
163
170
  };
164
- var createDataMessage = (batch) => {
165
- var _a;
166
- return {
167
- batch: {
168
- objects: (_a = batch.objects) == null ? void 0 : _a.map((object) => {
169
- var _a2;
170
- return {
171
- ...object,
172
- mutations: (_a2 = object.mutations) == null ? void 0 : _a2.map((mutation) => ({
173
- ...mutation,
174
- meta: void 0
175
- })),
176
- meta: void 0
177
- };
178
- })
179
- }
180
- };
181
- };
171
+ var createDataMessage = (batch) => ({
172
+ batch: {
173
+ objects: batch.objects?.map((object) => ({
174
+ ...object,
175
+ mutations: object.mutations?.map((mutation) => ({
176
+ ...mutation,
177
+ meta: void 0
178
+ })),
179
+ meta: void 0
180
+ }))
181
+ }
182
+ });
182
183
 
183
184
  // packages/core/echo/echo-pipeline/src/db-host/database-host.ts
184
185
  import { ItemDemuxer } from "@dxos/echo-db";
@@ -207,15 +208,13 @@ var DatabaseHost = class {
207
208
  return this._itemDemuxer.createSnapshot();
208
209
  }
209
210
  createDataServiceHost() {
210
- var _a;
211
- return new DataServiceHost(this._itemManager, this._itemDemuxer, (_a = this._outboundStream) != null ? _a : void 0);
211
+ return new DataServiceHost(this._itemManager, this._itemDemuxer, this._outboundStream ?? void 0);
212
212
  }
213
213
  };
214
214
 
215
215
  // packages/core/echo/echo-pipeline/src/db-host/snapshot-manager.ts
216
216
  import { trackLeaks } from "@dxos/async";
217
217
  import { cancelWithContext } from "@dxos/context";
218
- import { timed } from "@dxos/debug";
219
218
  import { PublicKey } from "@dxos/keys";
220
219
  import { schema as schema2 } from "@dxos/protocols";
221
220
  import { BlobMeta } from "@dxos/protocols/proto/dxos/echo/blob";
@@ -260,9 +259,6 @@ var SnapshotManager = class SnapshotManager2 {
260
259
  return PublicKey.from(id).toHex();
261
260
  }
262
261
  };
263
- _ts_decorate([
264
- timed(1e4)
265
- ], SnapshotManager.prototype, "load", null);
266
262
  SnapshotManager = _ts_decorate([
267
263
  trackLeaks("open", "close")
268
264
  ], SnapshotManager);
@@ -358,7 +354,7 @@ var DataServiceSubscriptions = class {
358
354
  C: (f, a) => f(...a)
359
355
  });
360
356
  const host = this._spaces.get(spaceKey);
361
- await (host == null ? void 0 : host.close());
357
+ await host?.close();
362
358
  this._spaces.delete(spaceKey);
363
359
  }
364
360
  getDataService(spaceKey) {
@@ -370,7 +366,6 @@ var DataServiceImpl = class {
370
366
  this._subscriptions = _subscriptions;
371
367
  }
372
368
  subscribe(request) {
373
- var _a;
374
369
  invariant3(request.spaceKey, void 0, {
375
370
  F: __dxlog_file3,
376
371
  L: 56,
@@ -380,11 +375,10 @@ var DataServiceImpl = class {
380
375
  ""
381
376
  ]
382
377
  });
383
- const host = (_a = this._subscriptions.getDataService(request.spaceKey)) != null ? _a : raise(new Error(`space not found: ${request.spaceKey}`));
378
+ const host = this._subscriptions.getDataService(request.spaceKey) ?? raise(new Error(`space not found: ${request.spaceKey}`));
384
379
  return host.subscribe();
385
380
  }
386
381
  write(request) {
387
- var _a;
388
382
  invariant3(request.spaceKey, void 0, {
389
383
  F: __dxlog_file3,
390
384
  L: 63,
@@ -403,7 +397,7 @@ var DataServiceImpl = class {
403
397
  ""
404
398
  ]
405
399
  });
406
- const host = (_a = this._subscriptions.getDataService(request.spaceKey)) != null ? _a : raise(new Error(`space not found: ${request.spaceKey}`));
400
+ const host = this._subscriptions.getDataService(request.spaceKey) ?? raise(new Error(`space not found: ${request.spaceKey}`));
407
401
  return host.write(request);
408
402
  }
409
403
  };
@@ -431,8 +425,8 @@ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipelin
431
425
  var emptyEchoMetadata = () => ({
432
426
  version: STORAGE_VERSION,
433
427
  spaces: [],
434
- created: new Date(),
435
- updated: new Date()
428
+ created: /* @__PURE__ */ new Date(),
429
+ updated: /* @__PURE__ */ new Date()
436
430
  });
437
431
  var EchoMetadata = schema4.getCodecForType("dxos.echo.metadata.EchoMetadata");
438
432
  var MetadataStore = class {
@@ -446,22 +440,19 @@ var MetadataStore = class {
446
440
  return this._metadata;
447
441
  }
448
442
  get version() {
449
- var _a;
450
- return (_a = this._metadata.version) != null ? _a : 0;
443
+ return this._metadata.version ?? 0;
451
444
  }
452
445
  /**
453
446
  * Returns a list of currently saved spaces. The list and objects in it can be modified addSpace and
454
447
  * addSpaceFeed functions.
455
448
  */
456
449
  get spaces() {
457
- var _a;
458
- return (_a = this._metadata.spaces) != null ? _a : [];
450
+ return this._metadata.spaces ?? [];
459
451
  }
460
452
  /**
461
453
  * Loads metadata from persistent storage.
462
454
  */
463
455
  async load() {
464
- var _a;
465
456
  const file = this._directory.getOrCreateFile("EchoMetadata");
466
457
  try {
467
458
  const { size: fileLength } = await file.stat();
@@ -480,7 +471,10 @@ var MetadataStore = class {
480
471
  C: (f, a) => f(...a)
481
472
  });
482
473
  if (fileLength < dataSize + 8) {
483
- throw new DataCorruptionError("Metadata size is smaller than expected.");
474
+ throw new DataCorruptionError("Metadata size is smaller than expected.", {
475
+ fileLength,
476
+ dataSize
477
+ });
484
478
  }
485
479
  const data = await file.read(8, dataSize);
486
480
  const calculatedChecksum = CRC32.buf(data);
@@ -488,9 +482,8 @@ var MetadataStore = class {
488
482
  throw new DataCorruptionError("Metadata checksum is invalid.");
489
483
  }
490
484
  this._metadata = EchoMetadata.decode(data);
491
- (_a = this._metadata.spaces) == null ? void 0 : _a.forEach((space) => {
492
- var _a2;
493
- (_a2 = space.state) != null ? _a2 : space.state = SpaceState.ACTIVE;
485
+ this._metadata.spaces?.forEach((space) => {
486
+ space.state ??= SpaceState.ACTIVE;
494
487
  });
495
488
  } catch (err) {
496
489
  log3.error("failed to load metadata", {
@@ -507,12 +500,11 @@ var MetadataStore = class {
507
500
  }
508
501
  }
509
502
  async _save() {
510
- var _a;
511
503
  const data = {
512
504
  ...this._metadata,
513
505
  version: STORAGE_VERSION,
514
- created: (_a = this._metadata.created) != null ? _a : new Date(),
515
- updated: new Date()
506
+ created: this._metadata.created ?? /* @__PURE__ */ new Date(),
507
+ updated: /* @__PURE__ */ new Date()
516
508
  };
517
509
  this.update.emit(data);
518
510
  const file = this._directory.getOrCreateFile("EchoMetadata");
@@ -538,8 +530,7 @@ var MetadataStore = class {
538
530
  }
539
531
  }
540
532
  _getSpace(spaceKey) {
541
- var _a;
542
- if ((_a = this._metadata.identity) == null ? void 0 : _a.haloSpace.key.equals(spaceKey)) {
533
+ if (this._metadata.identity?.haloSpace.key.equals(spaceKey)) {
543
534
  return this._metadata.identity.haloSpace;
544
535
  }
545
536
  const space = this.spaces.find((space2) => space2.key === spaceKey);
@@ -584,8 +575,7 @@ var MetadataStore = class {
584
575
  await this._save();
585
576
  }
586
577
  async addSpace(record) {
587
- var _a, _b, _c;
588
- invariant4(!((_a = this._metadata.spaces) != null ? _a : []).find((space) => space.key === record.key), "Cannot overwrite existing space in metadata", {
578
+ invariant4(!(this._metadata.spaces ?? []).find((space) => space.key === record.key), "Cannot overwrite existing space in metadata", {
589
579
  F: __dxlog_file4,
590
580
  L: 162,
591
581
  S: this,
@@ -594,7 +584,7 @@ var MetadataStore = class {
594
584
  "'Cannot overwrite existing space in metadata'"
595
585
  ]
596
586
  });
597
- ((_c = (_b = this._metadata).spaces) != null ? _c : _b.spaces = []).push(record);
587
+ (this._metadata.spaces ??= []).push(record);
598
588
  await this._save();
599
589
  }
600
590
  async setSpaceDataLatestTimeframe(spaceKey, timeframe) {
@@ -630,7 +620,7 @@ var fromBytesInt32 = (buf) => buf.readInt32LE(0);
630
620
 
631
621
  // packages/core/echo/echo-pipeline/src/pipeline/timeframe-clock.ts
632
622
  import { Event as Event2 } from "@dxos/async";
633
- import { timed as timed2 } from "@dxos/debug";
623
+ import { timed } from "@dxos/debug";
634
624
  import { log as log4 } from "@dxos/log";
635
625
  import { Timeframe } from "@dxos/timeframe";
636
626
  function _ts_decorate3(decorators, target, key, desc) {
@@ -723,7 +713,7 @@ var TimeframeClock = class {
723
713
  }
724
714
  };
725
715
  _ts_decorate3([
726
- timed2(5e3)
716
+ timed(5e3)
727
717
  ], TimeframeClock.prototype, "waitUntilReached", null);
728
718
 
729
719
  // packages/core/echo/echo-pipeline/src/pipeline/pipeline.ts
@@ -735,7 +725,7 @@ import { invariant as invariant6 } from "@dxos/invariant";
735
725
  import { PublicKey as PublicKey3 } from "@dxos/keys";
736
726
  import { log as log6 } from "@dxos/log";
737
727
  import { Timeframe as Timeframe2 } from "@dxos/timeframe";
738
- import { ComplexMap as ComplexMap3 } from "@dxos/util";
728
+ import { ComplexMap as ComplexMap3, getPrototypeSpecificInstanceId } from "@dxos/util";
739
729
 
740
730
  // packages/core/echo/echo-pipeline/src/pipeline/message-selector.ts
741
731
  import { invariant as invariant5 } from "@dxos/invariant";
@@ -829,7 +819,6 @@ var PipelineState = class {
829
819
  * @param timeout Timeout in milliseconds to specify the maximum wait time.
830
820
  */
831
821
  async waitUntilReachedTargetTimeframe({ ctx = new Context2(), timeout, breakOnStall = true } = {}) {
832
- var _a;
833
822
  log6("waitUntilReachedTargetTimeframe", {
834
823
  timeout,
835
824
  current: this.timeframe,
@@ -840,7 +829,7 @@ var PipelineState = class {
840
829
  S: this,
841
830
  C: (f, a) => f(...a)
842
831
  });
843
- (_a = this._reachedTargetPromise) != null ? _a : this._reachedTargetPromise = Promise.race([
832
+ this._reachedTargetPromise ??= Promise.race([
844
833
  this._timeframeClock.update.waitForCondition(() => {
845
834
  return Timeframe2.dependencies(this.targetTimeframe, this.timeframe).isEmpty();
846
835
  }),
@@ -889,7 +878,7 @@ var Pipeline = class {
889
878
  this._pauseTrigger = new Trigger().wake();
890
879
  this._isStopping = false;
891
880
  this._isStarted = false;
892
- this._isOpen = false;
881
+ this._isBeingConsumed = false;
893
882
  this._isPaused = false;
894
883
  }
895
884
  get state() {
@@ -920,12 +909,14 @@ var Pipeline = class {
920
909
  if (this._feedSetIterator) {
921
910
  await this._feedSetIterator.addFeed(feed);
922
911
  }
923
- this._setFeedDownloadState(feed);
912
+ if (this._isStarted && !this._isPaused) {
913
+ this._setFeedDownloadState(feed);
914
+ }
924
915
  }
925
916
  setWriteFeed(feed) {
926
917
  invariant6(!this._writer, "Writer already set.", {
927
918
  F: __dxlog_file7,
928
- L: 253,
919
+ L: 257,
929
920
  S: this,
930
921
  A: [
931
922
  "!this._writer",
@@ -934,7 +925,7 @@ var Pipeline = class {
934
925
  });
935
926
  invariant6(feed.properties.writable, "Feed must be writable.", {
936
927
  F: __dxlog_file7,
937
- L: 254,
928
+ L: 258,
938
929
  S: this,
939
930
  A: [
940
931
  "feed.properties.writable",
@@ -947,9 +938,18 @@ var Pipeline = class {
947
938
  }), feed.createFeedWriter());
948
939
  }
949
940
  async start() {
941
+ invariant6(!this._isStarted, "Pipeline is already started.", {
942
+ F: __dxlog_file7,
943
+ L: 271,
944
+ S: this,
945
+ A: [
946
+ "!this._isStarted",
947
+ "'Pipeline is already started.'"
948
+ ]
949
+ });
950
950
  log6("starting...", void 0, {
951
951
  F: __dxlog_file7,
952
- L: 267,
952
+ L: 272,
953
953
  S: this,
954
954
  C: (f, a) => f(...a)
955
955
  });
@@ -958,21 +958,25 @@ var Pipeline = class {
958
958
  this._isStarted = true;
959
959
  log6("started", void 0, {
960
960
  F: __dxlog_file7,
961
- L: 271,
961
+ L: 276,
962
962
  S: this,
963
963
  C: (f, a) => f(...a)
964
964
  });
965
+ if (!this._isPaused) {
966
+ for (const feed of this._feeds.values()) {
967
+ this._setFeedDownloadState(feed);
968
+ }
969
+ }
965
970
  }
966
971
  async stop() {
967
- var _a;
968
972
  log6("stopping...", void 0, {
969
973
  F: __dxlog_file7,
970
- L: 276,
974
+ L: 287,
971
975
  S: this,
972
976
  C: (f, a) => f(...a)
973
977
  });
974
978
  this._isStopping = true;
975
- await ((_a = this._feedSetIterator) == null ? void 0 : _a.close());
979
+ await this._feedSetIterator?.close();
976
980
  await this._processingTrigger.wait();
977
981
  await this._state._ctx.dispose();
978
982
  this._state._ctx = new Context2();
@@ -980,7 +984,7 @@ var Pipeline = class {
980
984
  this._isStarted = false;
981
985
  log6("stopped", void 0, {
982
986
  F: __dxlog_file7,
983
- L: 284,
987
+ L: 295,
984
988
  S: this,
985
989
  C: (f, a) => f(...a)
986
990
  });
@@ -992,7 +996,7 @@ var Pipeline = class {
992
996
  async setCursor(timeframe) {
993
997
  invariant6(!this._isStarted || this._isPaused, "Invalid state.", {
994
998
  F: __dxlog_file7,
995
- L: 293,
999
+ L: 304,
996
1000
  S: this,
997
1001
  A: [
998
1002
  "!this._isStarted || this._isPaused",
@@ -1001,9 +1005,6 @@ var Pipeline = class {
1001
1005
  });
1002
1006
  this._state._startTimeframe = timeframe;
1003
1007
  this._timeframeClock.setTimeframe(timeframe);
1004
- for (const feed of this._feeds.values()) {
1005
- this._setFeedDownloadState(feed);
1006
- }
1007
1008
  if (this._feedSetIterator) {
1008
1009
  await this._feedSetIterator.close();
1009
1010
  await this._initIterator();
@@ -1014,15 +1015,6 @@ var Pipeline = class {
1014
1015
  * Calling pause while processing will cause a deadlock.
1015
1016
  */
1016
1017
  async pause() {
1017
- invariant6(this._isStarted, "Pipeline is not open.", {
1018
- F: __dxlog_file7,
1019
- L: 315,
1020
- S: this,
1021
- A: [
1022
- "this._isStarted",
1023
- "'Pipeline is not open.'"
1024
- ]
1025
- });
1026
1018
  if (this._isPaused) {
1027
1019
  return;
1028
1020
  }
@@ -1031,18 +1023,9 @@ var Pipeline = class {
1031
1023
  this._isPaused = true;
1032
1024
  }
1033
1025
  async unpause() {
1034
- invariant6(this._isStarted, "Pipeline is not open.", {
1035
- F: __dxlog_file7,
1036
- L: 327,
1037
- S: this,
1038
- A: [
1039
- "this._isStarted",
1040
- "'Pipeline is not open.'"
1041
- ]
1042
- });
1043
1026
  invariant6(this._isPaused, "Pipeline is not paused.", {
1044
1027
  F: __dxlog_file7,
1045
- L: 328,
1028
+ L: 334,
1046
1029
  S: this,
1047
1030
  A: [
1048
1031
  "this._isPaused",
@@ -1051,25 +1034,28 @@ var Pipeline = class {
1051
1034
  });
1052
1035
  this._pauseTrigger.wake();
1053
1036
  this._isPaused = false;
1037
+ for (const feed of this._feeds.values()) {
1038
+ this._setFeedDownloadState(feed);
1039
+ }
1054
1040
  }
1055
1041
  /**
1056
1042
  * Starts to iterate over the ordered messages from the added feeds.
1057
1043
  * Updates the timeframe clock after the message has bee processed.
1058
1044
  */
1059
1045
  async *consume() {
1060
- invariant6(!this._isOpen, "Pipeline is already being consumed.", {
1046
+ invariant6(!this._isBeingConsumed, "Pipeline is already being consumed.", {
1061
1047
  F: __dxlog_file7,
1062
- L: 339,
1048
+ L: 349,
1063
1049
  S: this,
1064
1050
  A: [
1065
- "!this._isOpen",
1051
+ "!this._isBeingConsumed",
1066
1052
  "'Pipeline is already being consumed.'"
1067
1053
  ]
1068
1054
  });
1069
- this._isOpen = true;
1055
+ this._isBeingConsumed = true;
1070
1056
  invariant6(this._feedSetIterator, "Iterator not initialized.", {
1071
1057
  F: __dxlog_file7,
1072
- L: 342,
1058
+ L: 352,
1073
1059
  S: this,
1074
1060
  A: [
1075
1061
  "this._feedSetIterator",
@@ -1083,7 +1069,7 @@ var Pipeline = class {
1083
1069
  if (lastFeedSetIterator !== this._feedSetIterator) {
1084
1070
  invariant6(this._feedSetIterator, "Iterator not initialized.", {
1085
1071
  F: __dxlog_file7,
1086
- L: 351,
1072
+ L: 361,
1087
1073
  S: this,
1088
1074
  A: [
1089
1075
  "this._feedSetIterator",
@@ -1095,7 +1081,7 @@ var Pipeline = class {
1095
1081
  }
1096
1082
  const { done, value } = await iterable.next();
1097
1083
  if (!done) {
1098
- const block = value != null ? value : failUndefined();
1084
+ const block = value ?? failUndefined();
1099
1085
  this._processingTrigger.reset();
1100
1086
  this._timeframeClock.updatePendingTimeframe(PublicKey3.from(block.feedKey), block.seq);
1101
1087
  yield block;
@@ -1103,16 +1089,31 @@ var Pipeline = class {
1103
1089
  this._timeframeClock.updateTimeframe();
1104
1090
  }
1105
1091
  }
1106
- this._isOpen = false;
1092
+ this._isBeingConsumed = false;
1107
1093
  }
1108
1094
  _setFeedDownloadState(feed) {
1109
- var _a;
1110
1095
  const timeframe = this._state._startTimeframe;
1111
- const seq = (_a = timeframe.get(feed.key)) != null ? _a : 0;
1096
+ const seq = timeframe.get(feed.key) ?? -1;
1097
+ log6("setFeedDownloadState", {
1098
+ feed: feed.key,
1099
+ feedInstance: getPrototypeSpecificInstanceId(feed),
1100
+ seq,
1101
+ isBeingConsumed: this._isBeingConsumed,
1102
+ isStarted: this._isStarted,
1103
+ isPaused: this._isPaused
1104
+ }, {
1105
+ F: __dxlog_file7,
1106
+ L: 386,
1107
+ S: this,
1108
+ C: (f, a) => f(...a)
1109
+ });
1110
+ if (!this._isStarted || this._isPaused) {
1111
+ console.log(new Error().stack);
1112
+ }
1112
1113
  feed.undownload({
1113
1114
  callback: () => log6("undownload", void 0, {
1114
1115
  F: __dxlog_file7,
1115
- L: 375,
1116
+ L: 398,
1116
1117
  S: this,
1117
1118
  C: (f, a) => f(...a)
1118
1119
  })
@@ -1125,7 +1126,7 @@ var Pipeline = class {
1125
1126
  err
1126
1127
  }, {
1127
1128
  F: __dxlog_file7,
1128
- L: 377,
1129
+ L: 400,
1129
1130
  S: this,
1130
1131
  C: (f, a) => f(...a)
1131
1132
  });
@@ -1139,7 +1140,7 @@ var Pipeline = class {
1139
1140
  this._feedSetIterator.stalled.on((iterator) => {
1140
1141
  log6.warn(`Stalled after ${iterator.options.stallTimeout}ms with ${iterator.size} feeds.`, void 0, {
1141
1142
  F: __dxlog_file7,
1142
- L: 388,
1143
+ L: 411,
1143
1144
  S: this,
1144
1145
  C: (f, a) => f(...a)
1145
1146
  });
@@ -1231,7 +1232,7 @@ var AuthExtension = class extends RpcExtension {
1231
1232
  const { credential } = await this.rpc.AuthService.authenticate({
1232
1233
  challenge
1233
1234
  });
1234
- invariant7((credential == null ? void 0 : credential.length) > 0, "invalid credential", {
1235
+ invariant7(credential?.length > 0, "invalid credential", {
1235
1236
  F: __dxlog_file8,
1236
1237
  L: 69,
1237
1238
  S: this,
@@ -1278,6 +1279,7 @@ import { CancelledError } from "@dxos/errors";
1278
1279
  import { invariant as invariant8 } from "@dxos/invariant";
1279
1280
  import { log as log8 } from "@dxos/log";
1280
1281
  import { Timeframe as Timeframe3 } from "@dxos/timeframe";
1282
+ import { TimeSeriesCounter, TimeUsageCounter, trace } from "@dxos/tracing";
1281
1283
  import { tracer } from "@dxos/util";
1282
1284
  function _ts_decorate5(decorators, target, key, desc) {
1283
1285
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -1304,6 +1306,8 @@ var DataPipeline = class DataPipeline2 {
1304
1306
  this._lastTimeframeSaveTime = 0;
1305
1307
  this._lastSnapshotSaveTime = 0;
1306
1308
  this._lastProcessedEpoch = -1;
1309
+ this._usage = new TimeUsageCounter();
1310
+ this._mutations = new TimeSeriesCounter();
1307
1311
  this.currentEpoch = void 0;
1308
1312
  this.appliedEpoch = void 0;
1309
1313
  this.onNewEpoch = new Event4();
@@ -1315,13 +1319,11 @@ var DataPipeline = class DataPipeline2 {
1315
1319
  return this._pipeline;
1316
1320
  }
1317
1321
  get pipelineState() {
1318
- var _a;
1319
- return (_a = this._pipeline) == null ? void 0 : _a.state;
1322
+ return this._pipeline?.state;
1320
1323
  }
1321
1324
  setTargetTimeframe(timeframe) {
1322
- var _a;
1323
1325
  this._targetTimeframe = timeframe;
1324
- (_a = this._pipeline) == null ? void 0 : _a.state.setTargetTimeframe(timeframe);
1326
+ this._pipeline?.state.setTargetTimeframe(timeframe);
1325
1327
  }
1326
1328
  async processCredential(credential) {
1327
1329
  if (!checkCredentialType(credential, "dxos.halo.credentials.Epoch")) {
@@ -1338,8 +1340,8 @@ var DataPipeline = class DataPipeline2 {
1338
1340
  }
1339
1341
  this._pipeline = new Pipeline();
1340
1342
  await this._params.onPipelineCreated(this._pipeline);
1341
- await this._pipeline.start();
1342
1343
  await this._pipeline.pause();
1344
+ await this._pipeline.start();
1343
1345
  if (this._targetTimeframe) {
1344
1346
  this._pipeline.state.setTargetTimeframe(this._targetTimeframe);
1345
1347
  }
@@ -1347,7 +1349,7 @@ var DataPipeline = class DataPipeline2 {
1347
1349
  write: (data, options) => {
1348
1350
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1349
1351
  F: __dxlog_file9,
1350
- L: 145,
1352
+ L: 154,
1351
1353
  S: this,
1352
1354
  A: [
1353
1355
  "this._pipeline",
@@ -1356,7 +1358,7 @@ var DataPipeline = class DataPipeline2 {
1356
1358
  });
1357
1359
  invariant8(this.currentEpoch, "Epoch is not initialized.", {
1358
1360
  F: __dxlog_file9,
1359
- L: 146,
1361
+ L: 155,
1360
1362
  S: this,
1361
1363
  A: [
1362
1364
  "this.currentEpoch",
@@ -1377,19 +1379,18 @@ var DataPipeline = class DataPipeline2 {
1377
1379
  this._isOpen = true;
1378
1380
  }
1379
1381
  async close() {
1380
- var _a, _b, _c;
1381
1382
  if (!this._isOpen) {
1382
1383
  return;
1383
1384
  }
1384
1385
  log8("close", void 0, {
1385
1386
  F: __dxlog_file9,
1386
- L: 170,
1387
+ L: 179,
1387
1388
  S: this,
1388
1389
  C: (f, a) => f(...a)
1389
1390
  });
1390
1391
  this._isOpen = false;
1391
1392
  await this._ctx.dispose();
1392
- await ((_a = this._pipeline) == null ? void 0 : _a.stop());
1393
+ await this._pipeline?.stop();
1393
1394
  try {
1394
1395
  await this._saveCache();
1395
1396
  if (this._pipeline) {
@@ -1398,13 +1399,13 @@ var DataPipeline = class DataPipeline2 {
1398
1399
  } catch (err) {
1399
1400
  log8.catch(err, void 0, {
1400
1401
  F: __dxlog_file9,
1401
- L: 183,
1402
+ L: 192,
1402
1403
  S: this,
1403
1404
  C: (f, a) => f(...a)
1404
1405
  });
1405
1406
  }
1406
- await ((_b = this.databaseHost) == null ? void 0 : _b.close());
1407
- await ((_c = this.itemManager) == null ? void 0 : _c.destroy());
1407
+ await this.databaseHost?.close();
1408
+ await this.itemManager?.destroy();
1408
1409
  this._ctx = new Context4();
1409
1410
  this._pipeline = void 0;
1410
1411
  this._targetTimeframe = void 0;
@@ -1422,7 +1423,7 @@ var DataPipeline = class DataPipeline2 {
1422
1423
  }
1423
1424
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1424
1425
  F: __dxlog_file9,
1425
- L: 206,
1426
+ L: 215,
1426
1427
  S: this,
1427
1428
  A: [
1428
1429
  "this._pipeline",
@@ -1430,13 +1431,15 @@ var DataPipeline = class DataPipeline2 {
1430
1431
  ]
1431
1432
  });
1432
1433
  for await (const msg of this._pipeline.consume()) {
1434
+ const span = this._usage.beginRecording();
1435
+ this._mutations.inc();
1433
1436
  const { feedKey, seq, data } = msg;
1434
1437
  log8("processing message", {
1435
1438
  feedKey,
1436
1439
  seq
1437
1440
  }, {
1438
1441
  F: __dxlog_file9,
1439
- L: 209,
1442
+ L: 221,
1440
1443
  S: this,
1441
1444
  C: (f, a) => f(...a)
1442
1445
  });
@@ -1448,7 +1451,7 @@ var DataPipeline = class DataPipeline2 {
1448
1451
  feedKey
1449
1452
  }, {
1450
1453
  F: __dxlog_file9,
1451
- L: 215,
1454
+ L: 227,
1452
1455
  S: this,
1453
1456
  C: (f, a) => f(...a)
1454
1457
  });
@@ -1471,7 +1474,7 @@ var DataPipeline = class DataPipeline2 {
1471
1474
  spaceKey: this._params.spaceKey.toHex()
1472
1475
  }, {
1473
1476
  F: __dxlog_file9,
1474
- L: 232,
1477
+ L: 244,
1475
1478
  S: this,
1476
1479
  C: (f, a) => f(...a)
1477
1480
  });
@@ -1480,17 +1483,18 @@ var DataPipeline = class DataPipeline2 {
1480
1483
  } catch (err) {
1481
1484
  log8.catch(err, void 0, {
1482
1485
  F: __dxlog_file9,
1483
- L: 242,
1486
+ L: 254,
1484
1487
  S: this,
1485
1488
  C: (f, a) => f(...a)
1486
1489
  });
1487
1490
  }
1491
+ span.end();
1488
1492
  }
1489
1493
  }
1490
1494
  _createSnapshot() {
1491
1495
  invariant8(this.databaseHost, "Database backend is not initialized.", {
1492
1496
  F: __dxlog_file9,
1493
- L: 248,
1497
+ L: 262,
1494
1498
  S: this,
1495
1499
  A: [
1496
1500
  "this.databaseHost",
@@ -1504,26 +1508,22 @@ var DataPipeline = class DataPipeline2 {
1504
1508
  };
1505
1509
  }
1506
1510
  async _saveTargetTimeframe(timeframe) {
1507
- var _a;
1508
- const newTimeframe = Timeframe3.merge((_a = this._targetTimeframe) != null ? _a : new Timeframe3(), timeframe);
1511
+ const newTimeframe = Timeframe3.merge(this._targetTimeframe ?? new Timeframe3(), timeframe);
1509
1512
  await this._params.metadataStore.setSpaceDataLatestTimeframe(this._params.spaceKey, newTimeframe);
1510
1513
  this._targetTimeframe = newTimeframe;
1511
1514
  }
1512
1515
  async _saveCache() {
1513
- var _a;
1514
1516
  const cache = {};
1515
1517
  try {
1516
- const propertiesItem = this.itemManager.items.find((item) => {
1517
- var _a2, _b;
1518
- return ((_a2 = item.modelMeta) == null ? void 0 : _a2.type) === "dxos:model/document" && ((_b = getStateMachineFromItem(item)) == null ? void 0 : _b.snapshot()).type === "dxos.sdk.client.Properties";
1519
- });
1518
+ const propertiesItem = this.itemManager.items.find((item) => item.modelMeta?.type === "dxos:model/document" && // TODO(burdon): Document?
1519
+ (getStateMachineFromItem(item)?.snapshot()).type === "dxos.sdk.client.Properties");
1520
1520
  if (propertiesItem) {
1521
- cache.properties = (_a = getStateMachineFromItem(propertiesItem)) == null ? void 0 : _a.snapshot();
1521
+ cache.properties = getStateMachineFromItem(propertiesItem)?.snapshot();
1522
1522
  }
1523
1523
  } catch (err) {
1524
1524
  log8.warn("Failed to cache properties", err, {
1525
1525
  F: __dxlog_file9,
1526
- L: 277,
1526
+ L: 291,
1527
1527
  S: this,
1528
1528
  C: (f, a) => f(...a)
1529
1529
  });
@@ -1540,24 +1540,23 @@ var DataPipeline = class DataPipeline2 {
1540
1540
  }
1541
1541
  }
1542
1542
  async _processEpochInSeparateTask(epoch) {
1543
- var _a;
1544
1543
  if (epoch.subject.assertion.number <= this._lastProcessedEpoch) {
1545
1544
  return;
1546
1545
  }
1547
- await ((_a = this._epochCtx) == null ? void 0 : _a.dispose());
1546
+ await this._epochCtx?.dispose();
1548
1547
  const ctx = new Context4({
1549
1548
  onError: (err) => {
1550
1549
  if (err instanceof CancelledError) {
1551
1550
  log8("Epoch processing cancelled.", void 0, {
1552
1551
  F: __dxlog_file9,
1553
- L: 309,
1552
+ L: 323,
1554
1553
  S: this,
1555
1554
  C: (f, a) => f(...a)
1556
1555
  });
1557
1556
  } else {
1558
1557
  log8.catch(err, void 0, {
1559
1558
  F: __dxlog_file9,
1560
- L: 311,
1559
+ L: 325,
1561
1560
  S: this,
1562
1561
  C: (f, a) => f(...a)
1563
1562
  });
@@ -1573,7 +1572,7 @@ var DataPipeline = class DataPipeline2 {
1573
1572
  epoch
1574
1573
  }, {
1575
1574
  F: __dxlog_file9,
1576
- L: 321,
1575
+ L: 335,
1577
1576
  S: this,
1578
1577
  C: (f, a) => f(...a)
1579
1578
  });
@@ -1585,7 +1584,7 @@ var DataPipeline = class DataPipeline2 {
1585
1584
  async _processEpoch(ctx, epoch) {
1586
1585
  invariant8(this._isOpen, "Space is closed.", {
1587
1586
  F: __dxlog_file9,
1588
- L: 331,
1587
+ L: 345,
1589
1588
  S: this,
1590
1589
  A: [
1591
1590
  "this._isOpen",
@@ -1594,7 +1593,7 @@ var DataPipeline = class DataPipeline2 {
1594
1593
  });
1595
1594
  invariant8(this._pipeline, void 0, {
1596
1595
  F: __dxlog_file9,
1597
- L: 332,
1596
+ L: 346,
1598
1597
  S: this,
1599
1598
  A: [
1600
1599
  "this._pipeline",
@@ -1606,7 +1605,7 @@ var DataPipeline = class DataPipeline2 {
1606
1605
  epoch
1607
1606
  }, {
1608
1607
  F: __dxlog_file9,
1609
- L: 335,
1608
+ L: 349,
1610
1609
  S: this,
1611
1610
  C: (f, a) => f(...a)
1612
1611
  });
@@ -1616,7 +1615,7 @@ var DataPipeline = class DataPipeline2 {
1616
1615
  }
1617
1616
  log8("restarting pipeline for epoch", void 0, {
1618
1617
  F: __dxlog_file9,
1619
- L: 343,
1618
+ L: 356,
1620
1619
  S: this,
1621
1620
  C: (f, a) => f(...a)
1622
1621
  });
@@ -1627,7 +1626,7 @@ var DataPipeline = class DataPipeline2 {
1627
1626
  async waitUntilTimeframe(timeframe) {
1628
1627
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1629
1628
  F: __dxlog_file9,
1630
- L: 351,
1629
+ L: 364,
1631
1630
  S: this,
1632
1631
  A: [
1633
1632
  "this._pipeline",
@@ -1639,7 +1638,7 @@ var DataPipeline = class DataPipeline2 {
1639
1638
  async createEpoch() {
1640
1639
  invariant8(this._pipeline, void 0, {
1641
1640
  F: __dxlog_file9,
1642
- L: 357,
1641
+ L: 370,
1643
1642
  S: this,
1644
1643
  A: [
1645
1644
  "this._pipeline",
@@ -1648,7 +1647,7 @@ var DataPipeline = class DataPipeline2 {
1648
1647
  });
1649
1648
  invariant8(this.currentEpoch, void 0, {
1650
1649
  F: __dxlog_file9,
1651
- L: 358,
1650
+ L: 371,
1652
1651
  S: this,
1653
1652
  A: [
1654
1653
  "this.currentEpoch",
@@ -1671,6 +1670,12 @@ var DataPipeline = class DataPipeline2 {
1671
1670
  await this.onNewEpoch.waitForCondition(() => !!this.currentEpoch);
1672
1671
  }
1673
1672
  };
1673
+ _ts_decorate5([
1674
+ trace.metricsCounter()
1675
+ ], DataPipeline.prototype, "_usage", void 0);
1676
+ _ts_decorate5([
1677
+ trace.metricsCounter()
1678
+ ], DataPipeline.prototype, "_mutations", void 0);
1674
1679
  _ts_decorate5([
1675
1680
  synchronized3
1676
1681
  ], DataPipeline.prototype, "open", null);
@@ -1684,7 +1689,8 @@ _ts_decorate5([
1684
1689
  synchronized3
1685
1690
  ], DataPipeline.prototype, "createEpoch", null);
1686
1691
  DataPipeline = _ts_decorate5([
1687
- trackLeaks2("open", "close")
1692
+ trackLeaks2("open", "close"),
1693
+ trace.resource()
1688
1694
  ], DataPipeline);
1689
1695
 
1690
1696
  // packages/core/echo/echo-pipeline/src/space/space.ts
@@ -1692,21 +1698,36 @@ import { Event as Event5, synchronized as synchronized4, trackLeaks as trackLeak
1692
1698
  import { invariant as invariant9 } from "@dxos/invariant";
1693
1699
  import { log as log10, logInfo } from "@dxos/log";
1694
1700
  import { AdmittedFeed as AdmittedFeed2 } from "@dxos/protocols/proto/dxos/halo/credentials";
1701
+ import { trace as trace3 } from "@dxos/tracing";
1695
1702
  import { Callback as Callback2 } from "@dxos/util";
1696
1703
 
1697
1704
  // packages/core/echo/echo-pipeline/src/space/control-pipeline.ts
1705
+ import { Context as Context5 } from "@dxos/context";
1698
1706
  import { SpaceStateMachine } from "@dxos/credentials";
1699
1707
  import { PublicKey as PublicKey4 } from "@dxos/keys";
1700
1708
  import { log as log9 } from "@dxos/log";
1701
1709
  import { AdmittedFeed } from "@dxos/protocols/proto/dxos/halo/credentials";
1702
1710
  import { Timeframe as Timeframe4 } from "@dxos/timeframe";
1711
+ import { TimeSeriesCounter as TimeSeriesCounter2, TimeUsageCounter as TimeUsageCounter2, trace as trace2 } from "@dxos/tracing";
1703
1712
  import { Callback, tracer as tracer2 } from "@dxos/util";
1713
+ function _ts_decorate6(decorators, target, key, desc) {
1714
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1715
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1716
+ r = Reflect.decorate(decorators, target, key, desc);
1717
+ else
1718
+ for (var i = decorators.length - 1; i >= 0; i--)
1719
+ if (d = decorators[i])
1720
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1721
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1722
+ }
1704
1723
  var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/space/control-pipeline.ts";
1705
1724
  var TIMEFRAME_SAVE_DEBOUNCE_INTERVAL2 = 500;
1706
- var ControlPipeline = class {
1725
+ var ControlPipeline = class ControlPipeline2 {
1707
1726
  constructor({ spaceKey, genesisFeed, feedProvider, metadataStore }) {
1708
1727
  this._lastTimeframeSaveTime = Date.now();
1709
1728
  this.onFeedAdmitted = new Callback();
1729
+ this._usage = new TimeUsageCounter2();
1730
+ this._mutations = new TimeSeriesCounter2();
1710
1731
  this._spaceKey = spaceKey;
1711
1732
  this._metadata = metadataStore;
1712
1733
  this._pipeline = new Pipeline();
@@ -1717,7 +1738,7 @@ var ControlPipeline = class {
1717
1738
  key: info.key
1718
1739
  }, {
1719
1740
  F: __dxlog_file10,
1720
- L: 51,
1741
+ L: 61,
1721
1742
  S: this,
1722
1743
  C: (f, a) => f(...a)
1723
1744
  });
@@ -1728,7 +1749,7 @@ var ControlPipeline = class {
1728
1749
  } catch (err) {
1729
1750
  log9.catch(err, void 0, {
1730
1751
  F: __dxlog_file10,
1731
- L: 59,
1752
+ L: 69,
1732
1753
  S: this,
1733
1754
  C: (f, a) => f(...a)
1734
1755
  });
@@ -1751,56 +1772,65 @@ var ControlPipeline = class {
1751
1772
  async start() {
1752
1773
  log9("starting...", void 0, {
1753
1774
  F: __dxlog_file10,
1754
- L: 83,
1775
+ L: 93,
1755
1776
  S: this,
1756
1777
  C: (f, a) => f(...a)
1757
1778
  });
1758
1779
  setTimeout(async () => {
1759
- for await (const msg of this._pipeline.consume()) {
1760
- try {
1761
- log9("processing", {
1762
- key: msg.feedKey,
1763
- seq: msg.seq
1764
- }, {
1765
- F: __dxlog_file10,
1766
- L: 88,
1767
- S: this,
1768
- C: (f, a) => f(...a)
1769
- });
1770
- if (msg.data.payload.credential) {
1771
- const timer = tracer2.mark("dxos.echo.pipeline.control");
1772
- const result = await this._spaceStateMachine.process(msg.data.payload.credential.credential, PublicKey4.from(msg.feedKey));
1773
- timer.end();
1774
- if (!result) {
1775
- log9.warn("processing failed", {
1776
- msg
1777
- }, {
1778
- F: __dxlog_file10,
1779
- L: 98,
1780
- S: this,
1781
- C: (f, a) => f(...a)
1782
- });
1783
- } else {
1784
- await this._noteTargetStateIfNeeded(this._pipeline.state.pendingTimeframe);
1785
- }
1786
- }
1787
- } catch (err) {
1788
- log9.catch(err, void 0, {
1789
- F: __dxlog_file10,
1790
- L: 104,
1791
- S: this,
1792
- C: (f, a) => f(...a)
1793
- });
1794
- }
1795
- }
1780
+ void this._consumePipeline(new Context5());
1796
1781
  });
1797
1782
  await this._pipeline.start();
1798
1783
  log9("started", void 0, {
1799
1784
  F: __dxlog_file10,
1800
- L: 110,
1785
+ L: 99,
1786
+ S: this,
1787
+ C: (f, a) => f(...a)
1788
+ });
1789
+ }
1790
+ async _consumePipeline(ctx) {
1791
+ for await (const msg of this._pipeline.consume()) {
1792
+ const span = this._usage.beginRecording();
1793
+ this._mutations.inc();
1794
+ try {
1795
+ await this._processMessage(ctx, msg);
1796
+ } catch (err) {
1797
+ log9.catch(err, void 0, {
1798
+ F: __dxlog_file10,
1799
+ L: 111,
1800
+ S: this,
1801
+ C: (f, a) => f(...a)
1802
+ });
1803
+ }
1804
+ span.end();
1805
+ }
1806
+ }
1807
+ async _processMessage(ctx, msg) {
1808
+ log9("processing", {
1809
+ key: msg.feedKey,
1810
+ seq: msg.seq
1811
+ }, {
1812
+ F: __dxlog_file10,
1813
+ L: 121,
1801
1814
  S: this,
1802
1815
  C: (f, a) => f(...a)
1803
1816
  });
1817
+ if (msg.data.payload.credential) {
1818
+ const timer = tracer2.mark("dxos.echo.pipeline.control");
1819
+ const result = await this._spaceStateMachine.process(msg.data.payload.credential.credential, PublicKey4.from(msg.feedKey));
1820
+ timer.end();
1821
+ if (!result) {
1822
+ log9.warn("processing failed", {
1823
+ msg
1824
+ }, {
1825
+ F: __dxlog_file10,
1826
+ L: 131,
1827
+ S: this,
1828
+ C: (f, a) => f(...a)
1829
+ });
1830
+ } else {
1831
+ await this._noteTargetStateIfNeeded(this._pipeline.state.pendingTimeframe);
1832
+ }
1833
+ }
1804
1834
  }
1805
1835
  async _noteTargetStateIfNeeded(timeframe) {
1806
1836
  if (Date.now() - this._lastTimeframeSaveTime > TIMEFRAME_SAVE_DEBOUNCE_INTERVAL2) {
@@ -1811,7 +1841,7 @@ var ControlPipeline = class {
1811
1841
  async stop() {
1812
1842
  log9("stopping...", void 0, {
1813
1843
  F: __dxlog_file10,
1814
- L: 124,
1844
+ L: 149,
1815
1845
  S: this,
1816
1846
  C: (f, a) => f(...a)
1817
1847
  });
@@ -1819,30 +1849,44 @@ var ControlPipeline = class {
1819
1849
  await this._saveTargetTimeframe(this._pipeline.state.timeframe);
1820
1850
  log9("stopped", void 0, {
1821
1851
  F: __dxlog_file10,
1822
- L: 127,
1852
+ L: 152,
1823
1853
  S: this,
1824
1854
  C: (f, a) => f(...a)
1825
1855
  });
1826
1856
  }
1827
1857
  async _saveTargetTimeframe(timeframe) {
1828
- var _a;
1829
1858
  try {
1830
- const newTimeframe = Timeframe4.merge((_a = this._targetTimeframe) != null ? _a : new Timeframe4(), timeframe);
1859
+ const newTimeframe = Timeframe4.merge(this._targetTimeframe ?? new Timeframe4(), timeframe);
1831
1860
  await this._metadata.setSpaceControlLatestTimeframe(this._spaceKey, newTimeframe);
1832
1861
  this._targetTimeframe = newTimeframe;
1833
1862
  } catch (err) {
1834
1863
  log9(err, void 0, {
1835
1864
  F: __dxlog_file10,
1836
- L: 136,
1865
+ L: 161,
1837
1866
  S: this,
1838
1867
  C: (f, a) => f(...a)
1839
1868
  });
1840
1869
  }
1841
1870
  }
1842
1871
  };
1872
+ _ts_decorate6([
1873
+ trace2.metricsCounter()
1874
+ ], ControlPipeline.prototype, "_usage", void 0);
1875
+ _ts_decorate6([
1876
+ trace2.metricsCounter()
1877
+ ], ControlPipeline.prototype, "_mutations", void 0);
1878
+ _ts_decorate6([
1879
+ trace2.span()
1880
+ ], ControlPipeline.prototype, "_consumePipeline", null);
1881
+ _ts_decorate6([
1882
+ trace2.span()
1883
+ ], ControlPipeline.prototype, "_processMessage", null);
1884
+ ControlPipeline = _ts_decorate6([
1885
+ trace2.resource()
1886
+ ], ControlPipeline);
1843
1887
 
1844
1888
  // packages/core/echo/echo-pipeline/src/space/space.ts
1845
- function _ts_decorate6(decorators, target, key, desc) {
1889
+ function _ts_decorate7(decorators, target, key, desc) {
1846
1890
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1847
1891
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1848
1892
  r = Reflect.decorate(decorators, target, key, desc);
@@ -1861,7 +1905,7 @@ var Space = class Space2 {
1861
1905
  this._isOpen = false;
1862
1906
  invariant9(params.spaceKey && params.feedProvider, void 0, {
1863
1907
  F: __dxlog_file11,
1864
- L: 70,
1908
+ L: 73,
1865
1909
  S: this,
1866
1910
  A: [
1867
1911
  "params.spaceKey && params.feedProvider",
@@ -1903,7 +1947,7 @@ var Space = class Space2 {
1903
1947
  credential
1904
1948
  }, {
1905
1949
  F: __dxlog_file11,
1906
- L: 106,
1950
+ L: 109,
1907
1951
  S: this,
1908
1952
  C: (f, a) => f(...a)
1909
1953
  });
@@ -1945,12 +1989,10 @@ var Space = class Space2 {
1945
1989
  return this._genesisFeedKey;
1946
1990
  }
1947
1991
  get controlFeedKey() {
1948
- var _a;
1949
- return (_a = this._controlFeed) == null ? void 0 : _a.key;
1992
+ return this._controlFeed?.key;
1950
1993
  }
1951
1994
  get dataFeedKey() {
1952
- var _a;
1953
- return (_a = this._dataFeed) == null ? void 0 : _a.key;
1995
+ return this._dataFeed?.key;
1954
1996
  }
1955
1997
  get spaceState() {
1956
1998
  return this._controlPipeline.spaceState;
@@ -1970,7 +2012,7 @@ var Space = class Space2 {
1970
2012
  setControlFeed(feed) {
1971
2013
  invariant9(!this._controlFeed, "Control feed already set.", {
1972
2014
  F: __dxlog_file11,
1973
- L: 180,
2015
+ L: 184,
1974
2016
  S: this,
1975
2017
  A: [
1976
2018
  "!this._controlFeed",
@@ -1982,10 +2024,9 @@ var Space = class Space2 {
1982
2024
  return this;
1983
2025
  }
1984
2026
  setDataFeed(feed) {
1985
- var _a;
1986
2027
  invariant9(!this._dataFeed, "Data feed already set.", {
1987
2028
  F: __dxlog_file11,
1988
- L: 187,
2029
+ L: 191,
1989
2030
  S: this,
1990
2031
  A: [
1991
2032
  "!this._dataFeed",
@@ -1993,7 +2034,7 @@ var Space = class Space2 {
1993
2034
  ]
1994
2035
  });
1995
2036
  this._dataFeed = feed;
1996
- (_a = this._dataPipeline.pipeline) == null ? void 0 : _a.setWriteFeed(feed);
2037
+ this._dataPipeline.pipeline?.setWriteFeed(feed);
1997
2038
  return this;
1998
2039
  }
1999
2040
  /**
@@ -2008,10 +2049,10 @@ var Space = class Space2 {
2008
2049
  // getDataFeeds(): FeedInfo[] {
2009
2050
  // return this._dataPipeline?.getFeeds();
2010
2051
  // }
2011
- async open() {
2052
+ async open(ctx) {
2012
2053
  log10("opening...", void 0, {
2013
2054
  F: __dxlog_file11,
2014
- L: 208,
2055
+ L: 213,
2015
2056
  S: this,
2016
2057
  C: (f, a) => f(...a)
2017
2058
  });
@@ -2024,7 +2065,7 @@ var Space = class Space2 {
2024
2065
  this._isOpen = true;
2025
2066
  log10("opened", void 0, {
2026
2067
  F: __dxlog_file11,
2027
- L: 219,
2068
+ L: 224,
2028
2069
  S: this,
2029
2070
  C: (f, a) => f(...a)
2030
2071
  });
@@ -2034,7 +2075,7 @@ var Space = class Space2 {
2034
2075
  key: this._key
2035
2076
  }, {
2036
2077
  F: __dxlog_file11,
2037
- L: 224,
2078
+ L: 229,
2038
2079
  S: this,
2039
2080
  C: (f, a) => f(...a)
2040
2081
  });
@@ -2048,7 +2089,7 @@ var Space = class Space2 {
2048
2089
  this._isOpen = false;
2049
2090
  log10("closed", void 0, {
2050
2091
  F: __dxlog_file11,
2051
- L: 237,
2092
+ L: 242,
2052
2093
  S: this,
2053
2094
  C: (f, a) => f(...a)
2054
2095
  });
@@ -2056,13 +2097,13 @@ var Space = class Space2 {
2056
2097
  async initializeDataPipeline() {
2057
2098
  log10("initializeDataPipeline", void 0, {
2058
2099
  F: __dxlog_file11,
2059
- L: 242,
2100
+ L: 247,
2060
2101
  S: this,
2061
2102
  C: (f, a) => f(...a)
2062
2103
  });
2063
2104
  invariant9(this._isOpen, "Space must be open to initialize data pipeline.", {
2064
2105
  F: __dxlog_file11,
2065
- L: 243,
2106
+ L: 248,
2066
2107
  S: this,
2067
2108
  A: [
2068
2109
  "this._isOpen",
@@ -2072,20 +2113,23 @@ var Space = class Space2 {
2072
2113
  await this._dataPipeline.open();
2073
2114
  }
2074
2115
  };
2075
- _ts_decorate6([
2076
- logInfo
2116
+ _ts_decorate7([
2117
+ logInfo,
2118
+ trace3.info()
2077
2119
  ], Space.prototype, "key", null);
2078
- _ts_decorate6([
2079
- synchronized4
2120
+ _ts_decorate7([
2121
+ synchronized4,
2122
+ trace3.span()
2080
2123
  ], Space.prototype, "open", null);
2081
- _ts_decorate6([
2124
+ _ts_decorate7([
2082
2125
  synchronized4
2083
2126
  ], Space.prototype, "close", null);
2084
- _ts_decorate6([
2127
+ _ts_decorate7([
2085
2128
  synchronized4
2086
2129
  ], Space.prototype, "initializeDataPipeline", null);
2087
- Space = _ts_decorate6([
2088
- trackLeaks3("open", "close")
2130
+ Space = _ts_decorate7([
2131
+ trackLeaks3("open", "close"),
2132
+ trace3.resource()
2089
2133
  ], Space);
2090
2134
 
2091
2135
  // packages/core/echo/echo-pipeline/src/space/space-protocol.ts
@@ -2097,7 +2141,7 @@ import { Teleport } from "@dxos/teleport";
2097
2141
  import { BlobSync } from "@dxos/teleport-extension-object-sync";
2098
2142
  import { ReplicatorExtension } from "@dxos/teleport-extension-replicator";
2099
2143
  import { ComplexMap as ComplexMap4 } from "@dxos/util";
2100
- function _ts_decorate7(decorators, target, key, desc) {
2144
+ function _ts_decorate8(decorators, target, key, desc) {
2101
2145
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2102
2146
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2103
2147
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2214,10 +2258,10 @@ var SpaceProtocol = class {
2214
2258
  };
2215
2259
  }
2216
2260
  };
2217
- _ts_decorate7([
2261
+ _ts_decorate8([
2218
2262
  logInfo2
2219
2263
  ], SpaceProtocol.prototype, "_topic", void 0);
2220
- _ts_decorate7([
2264
+ _ts_decorate8([
2221
2265
  logInfo2
2222
2266
  ], SpaceProtocol.prototype, "_ownPeerKey", null);
2223
2267
  var AuthStatus;
@@ -2256,7 +2300,6 @@ var SpaceProtocolSession = class {
2256
2300
  provider: this._swarmIdentity.credentialProvider,
2257
2301
  verifier: this._swarmIdentity.credentialAuthenticator,
2258
2302
  onAuthSuccess: () => {
2259
- var _a;
2260
2303
  log11("Peer authenticated", void 0, {
2261
2304
  F: __dxlog_file12,
2262
2305
  L: 236,
@@ -2264,12 +2307,11 @@ var SpaceProtocolSession = class {
2264
2307
  C: (f, a) => f(...a)
2265
2308
  });
2266
2309
  this._authStatus = AuthStatus.SUCCESS;
2267
- (_a = this._onSessionAuth) == null ? void 0 : _a.call(this, this._teleport);
2310
+ this._onSessionAuth?.(this._teleport);
2268
2311
  },
2269
2312
  onAuthFailure: () => {
2270
- var _a;
2271
2313
  this._authStatus = AuthStatus.FAILURE;
2272
- (_a = this._onAuthFailure) == null ? void 0 : _a.call(this, this._teleport);
2314
+ this._onAuthFailure?.(this._teleport);
2273
2315
  }
2274
2316
  }));
2275
2317
  this._teleport.addExtension("dxos.mesh.teleport.replicator", this.replicator);
@@ -2279,10 +2321,10 @@ var SpaceProtocolSession = class {
2279
2321
  await this._teleport.close();
2280
2322
  }
2281
2323
  };
2282
- _ts_decorate7([
2324
+ _ts_decorate8([
2283
2325
  logInfo2
2284
2326
  ], SpaceProtocolSession.prototype, "_wireParams", void 0);
2285
- _ts_decorate7([
2327
+ _ts_decorate8([
2286
2328
  logInfo2
2287
2329
  ], SpaceProtocolSession.prototype, "authStatus", null);
2288
2330
 
@@ -2291,9 +2333,9 @@ import { synchronized as synchronized5, trackLeaks as trackLeaks4 } from "@dxos/
2291
2333
  import { failUndefined as failUndefined2 } from "@dxos/debug";
2292
2334
  import { PublicKey as PublicKey6 } from "@dxos/keys";
2293
2335
  import { log as log12 } from "@dxos/log";
2294
- import { trace } from "@dxos/protocols";
2336
+ import { trace as trace4 } from "@dxos/protocols";
2295
2337
  import { ComplexMap as ComplexMap5 } from "@dxos/util";
2296
- function _ts_decorate8(decorators, target, key, desc) {
2338
+ function _ts_decorate9(decorators, target, key, desc) {
2297
2339
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2298
2340
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2299
2341
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2327,8 +2369,7 @@ var SpaceManager = class SpaceManager2 {
2327
2369
  ].map((space) => space.close()));
2328
2370
  }
2329
2371
  async constructSpace({ metadata, swarmIdentity, onNetworkConnection, onAuthFailure, memberKey }) {
2330
- var _a;
2331
- log12.trace("dxos.echo.space-manager.construct-space", trace.begin({
2372
+ log12.trace("dxos.echo.space-manager.construct-space", trace4.begin({
2332
2373
  id: this._instanceId
2333
2374
  }), {
2334
2375
  F: __dxlog_file13,
@@ -2344,7 +2385,7 @@ var SpaceManager = class SpaceManager2 {
2344
2385
  S: this,
2345
2386
  C: (f, a) => f(...a)
2346
2387
  });
2347
- const genesisFeed = await this._feedStore.openFeed((_a = metadata.genesisFeedKey) != null ? _a : failUndefined2());
2388
+ const genesisFeed = await this._feedStore.openFeed(metadata.genesisFeedKey ?? failUndefined2());
2348
2389
  const spaceKey = metadata.key;
2349
2390
  const protocol = new SpaceProtocol({
2350
2391
  topic: spaceKey,
@@ -2366,7 +2407,7 @@ var SpaceManager = class SpaceManager2 {
2366
2407
  memberKey
2367
2408
  });
2368
2409
  this._spaces.set(space.key, space);
2369
- log12.trace("dxos.echo.space-manager.construct-space", trace.end({
2410
+ log12.trace("dxos.echo.space-manager.construct-space", trace4.end({
2370
2411
  id: this._instanceId
2371
2412
  }), {
2372
2413
  F: __dxlog_file13,
@@ -2377,13 +2418,13 @@ var SpaceManager = class SpaceManager2 {
2377
2418
  return space;
2378
2419
  }
2379
2420
  };
2380
- _ts_decorate8([
2421
+ _ts_decorate9([
2381
2422
  synchronized5
2382
2423
  ], SpaceManager.prototype, "open", null);
2383
- _ts_decorate8([
2424
+ _ts_decorate9([
2384
2425
  synchronized5
2385
2426
  ], SpaceManager.prototype, "close", null);
2386
- SpaceManager = _ts_decorate8([
2427
+ SpaceManager = _ts_decorate9([
2387
2428
  trackLeaks4("open", "close")
2388
2429
  ], SpaceManager);
2389
2430
 
@@ -2413,4 +2454,4 @@ export {
2413
2454
  SpaceProtocolSession,
2414
2455
  SpaceManager
2415
2456
  };
2416
- //# sourceMappingURL=chunk-7RPTJNLX.mjs.map
2457
+ //# sourceMappingURL=chunk-FCDYN74V.mjs.map