@dxos/feed 0.8.4-main.fcc0d83b33 → 0.8.4-staging.60fe92afc8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,8 +1,105 @@
1
- MIT License
2
- Copyright (c) 2022 DXOS
1
+ # Functional Source License, Version 1.1, ALv2 Future License
3
2
 
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ ## Abbreviation
5
4
 
6
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ FSL-1.1-Apache-2.0
7
6
 
8
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
+ ## Notice
8
+
9
+ Copyright 2026 DXOS
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the Apache License, Version 2.0 that is effective on the second anniversary of
91
+ the date we make the Software available. On or after that date, you may use the
92
+ Software under the Apache License, Version 2.0, in which case the following
93
+ will apply:
94
+
95
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
96
+ this file except in compliance with the License.
97
+
98
+ You may obtain a copy of the License at
99
+
100
+ http://www.apache.org/licenses/LICENSE-2.0
101
+
102
+ Unless required by applicable law or agreed to in writing, software distributed
103
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
+ specific language governing permissions and limitations under the License.
@@ -1,5 +1,13 @@
1
1
  // src/errors.ts
2
2
  import { BaseError } from "@dxos/errors";
3
+ var SyncRpcTimeoutError = class extends BaseError.extend("SyncRpcTimeoutError") {
4
+ constructor(args) {
5
+ super({
6
+ message: `Feed sync ${args.rpcTag} timed out after ${args.timeoutMs}ms (spaceId=${args.spaceId} feedNamespace=${args.feedNamespace} requestId=${args.requestId}).`,
7
+ context: args
8
+ });
9
+ }
10
+ };
3
11
  var PositionConflictError = class extends BaseError.extend("PositionConflictError") {
4
12
  constructor(args) {
5
13
  super({
@@ -244,6 +252,35 @@ var FeedStore = class {
244
252
  `;
245
253
  }).pipe(Effect.withSpan("FeedStore.setSyncState"));
246
254
  /**
255
+ * Returns the number of blocks pending push (no global position yet) in a space/namespace.
256
+ */
257
+ countUnpositionedBlocks = (opts) => Effect.gen(this, function* () {
258
+ const sql = yield* SqlClient.SqlClient;
259
+ const rows = yield* sql`
260
+ SELECT COUNT(*) AS count
261
+ FROM blocks
262
+ JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
263
+ WHERE feeds.spaceId = ${opts.spaceId}
264
+ AND feeds.feedNamespace = ${opts.feedNamespace}
265
+ AND blocks.position IS NULL
266
+ `;
267
+ return rows[0]?.count ?? 0;
268
+ }).pipe(Effect.withSpan("FeedStore.countUnpositionedBlocks"));
269
+ /**
270
+ * Returns the total number of blocks stored locally for a space/namespace.
271
+ */
272
+ countNamespaceBlocks = (opts) => Effect.gen(this, function* () {
273
+ const sql = yield* SqlClient.SqlClient;
274
+ const rows = yield* sql`
275
+ SELECT COUNT(*) AS count
276
+ FROM blocks
277
+ JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
278
+ WHERE feeds.spaceId = ${opts.spaceId}
279
+ AND feeds.feedNamespace = ${opts.feedNamespace}
280
+ `;
281
+ return rows[0]?.count ?? 0;
282
+ }).pipe(Effect.withSpan("FeedStore.countNamespaceBlocks"));
283
+ /**
247
284
  * Returns the number of stored blocks for a single feed in a space/namespace.
248
285
  * Intended as a low-level primitive for callers (for example Cloudflare Worker code)
249
286
  * that need to make retention decisions under constrained storage resources.
@@ -337,22 +374,48 @@ var FeedStore = class {
337
374
  const feedPrivateId = feedPrivateIds.get(key);
338
375
  let positionToInsert = null;
339
376
  if (this.#options.assignPositions) {
340
- const currentMax = maxPositions.get(request.feedNamespace);
341
- positionToInsert = currentMax + 1;
342
- maxPositions.set(request.feedNamespace, positionToInsert);
343
- positions2.push(positionToInsert);
377
+ positionToInsert = maxPositions.get(request.feedNamespace) + 1;
344
378
  } else if (block.position != null) {
345
379
  positionToInsert = block.position;
346
380
  }
347
- yield* sql`
381
+ const inserted = yield* sql`
348
382
  INSERT INTO blocks (
349
- feedPrivateId, position, sequence, actorId,
383
+ feedPrivateId, position, sequence, actorId,
350
384
  prevSequence, prevActorId, timestamp, data
351
385
  ) VALUES (
352
386
  ${feedPrivateId}, ${positionToInsert}, ${block.sequence}, ${block.actorId},
353
387
  ${block.prevSequence}, ${block.prevActorId}, ${block.timestamp}, ${block.data}
354
- ) ON CONFLICT DO NOTHING
388
+ )
389
+ ON CONFLICT(feedPrivateId, sequence, actorId) DO NOTHING
390
+ RETURNING position
391
+ `;
392
+ if (!this.#options.assignPositions) {
393
+ continue;
394
+ }
395
+ if (inserted.length > 0) {
396
+ positions2.push(positionToInsert);
397
+ maxPositions.set(request.feedNamespace, positionToInsert);
398
+ continue;
399
+ }
400
+ const existing = yield* sql`
401
+ SELECT position FROM blocks
402
+ WHERE feedPrivateId = ${feedPrivateId}
403
+ AND actorId = ${block.actorId}
404
+ AND sequence = ${block.sequence}
355
405
  `;
406
+ const existingPosition = existing[0]?.position;
407
+ if (existingPosition != null) {
408
+ positions2.push(existingPosition);
409
+ continue;
410
+ }
411
+ yield* sql`
412
+ UPDATE blocks SET position = ${positionToInsert}
413
+ WHERE feedPrivateId = ${feedPrivateId}
414
+ AND actorId = ${block.actorId}
415
+ AND sequence = ${block.sequence}
416
+ `;
417
+ positions2.push(positionToInsert);
418
+ maxPositions.set(request.feedNamespace, positionToInsert);
356
419
  }
357
420
  return positions2;
358
421
  }));
@@ -465,6 +528,19 @@ var FeedStore = class {
465
528
  setPosition = (request) => Effect.gen(this, function* () {
466
529
  const sql = yield* SqlClient.SqlClient;
467
530
  for (const block of request.blocks) {
531
+ const updated = yield* sql`
532
+ UPDATE blocks SET position = ${block.position}
533
+ WHERE feedPrivateId = (
534
+ SELECT feedPrivateId FROM feeds
535
+ WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
536
+ )
537
+ AND actorId = ${block.actorId} AND sequence = ${block.sequence}
538
+ AND (position IS NULL OR position = ${block.position})
539
+ RETURNING position
540
+ `;
541
+ if (updated.length > 0) {
542
+ continue;
543
+ }
468
544
  const existing = yield* sql`
469
545
  SELECT position FROM blocks
470
546
  WHERE feedPrivateId = (
@@ -483,14 +559,6 @@ var FeedStore = class {
483
559
  requestedPosition: block.position
484
560
  }));
485
561
  }
486
- yield* sql`
487
- UPDATE blocks SET position = ${block.position}
488
- WHERE feedPrivateId = (
489
- SELECT feedPrivateId FROM feeds
490
- WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
491
- )
492
- AND actorId = ${block.actorId} AND sequence = ${block.sequence}
493
- `;
494
562
  }
495
563
  }).pipe(Effect.withSpan("FeedStore.setPosition"));
496
564
  /**
@@ -536,6 +604,19 @@ var decodeCursor = (cursor) => {
536
604
 
537
605
  // src/sync-server.ts
538
606
  import * as Effect2 from "effect/Effect";
607
+ import { log } from "@dxos/log";
608
+ var __dxlog_file = "/__w/dxos/dxos/packages/core/echo/feed/src/sync-server.ts";
609
+ var inboundSummary = (message) => ({
610
+ tag: message._tag,
611
+ senderPeerId: message.senderPeerId,
612
+ recipientPeerId: message.recipientPeerId,
613
+ requestId: "requestId" in message ? message.requestId : void 0,
614
+ spaceId: "spaceId" in message ? message.spaceId : void 0,
615
+ feedNamespace: "feedNamespace" in message ? message.feedNamespace : void 0,
616
+ blockCount: "blocks" in message ? message.blocks.length : void 0,
617
+ position: "position" in message ? message.position : void 0,
618
+ limit: "limit" in message ? message.limit : void 0
619
+ });
539
620
  var SyncServer = class {
540
621
  #peerId;
541
622
  #feedStore;
@@ -550,6 +631,10 @@ var SyncServer = class {
550
631
  */
551
632
  handleMessage(ctx, message) {
552
633
  const self = this;
634
+ log("feed sync server received message", {
635
+ peerId: self.#peerId,
636
+ ...inboundSummary(message)
637
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 33, S: this });
553
638
  const recipientPeerId = message.senderPeerId;
554
639
  const withPeerIds = (payload) => ({
555
640
  ...payload,
@@ -561,29 +646,72 @@ var SyncServer = class {
561
646
  const req = message;
562
647
  return Effect2.gen(function* () {
563
648
  const response = yield* self.#feedStore.query(req);
649
+ log("feed sync server query completed", {
650
+ peerId: self.#peerId,
651
+ recipientPeerId,
652
+ requestId: req.requestId,
653
+ spaceId: req.spaceId,
654
+ feedNamespace: req.feedNamespace,
655
+ returnedBlocks: response.blocks.length,
656
+ hasMore: response.hasMore
657
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 49, S: this });
564
658
  yield* self.#sendMessage(ctx, withPeerIds({
565
659
  _tag: "QueryResponse",
566
660
  ...response
567
661
  }));
568
- }).pipe(Effect2.catchAll((err) => self.#sendMessage(ctx, withPeerIds({
569
- _tag: "Error",
570
- message: err instanceof Error ? err.message : String(err)
571
- }))));
662
+ }).pipe(Effect2.catchAll((err) => {
663
+ log("feed sync server query failed", {
664
+ peerId: self.#peerId,
665
+ recipientPeerId,
666
+ requestId: req.requestId,
667
+ spaceId: req.spaceId,
668
+ feedNamespace: req.feedNamespace,
669
+ cause: err instanceof Error ? err.message : String(err)
670
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 63, S: this });
671
+ return self.#sendMessage(ctx, withPeerIds({
672
+ _tag: "Error",
673
+ message: err instanceof Error ? err.message : String(err)
674
+ }));
675
+ }));
572
676
  }
573
677
  case "AppendRequest": {
574
678
  const req = message;
575
679
  return Effect2.gen(function* () {
576
680
  const response = yield* self.#feedStore.append(req);
681
+ log("feed sync server append completed", {
682
+ peerId: self.#peerId,
683
+ recipientPeerId,
684
+ requestId: req.requestId,
685
+ spaceId: req.spaceId,
686
+ feedNamespace: req.feedNamespace,
687
+ blockCount: req.blocks.length,
688
+ assignedPositions: response.positions.length
689
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 82, S: this });
577
690
  yield* self.#sendMessage(ctx, withPeerIds({
578
691
  _tag: "AppendResponse",
579
692
  ...response
580
693
  }));
581
- }).pipe(Effect2.catchAll((err) => self.#sendMessage(ctx, withPeerIds({
582
- _tag: "Error",
583
- message: err instanceof Error ? err.message : String(err)
584
- }))));
694
+ }).pipe(Effect2.catchAll((err) => {
695
+ log("feed sync server append failed", {
696
+ peerId: self.#peerId,
697
+ recipientPeerId,
698
+ requestId: req.requestId,
699
+ spaceId: req.spaceId,
700
+ feedNamespace: req.feedNamespace,
701
+ blockCount: req.blocks.length,
702
+ cause: err instanceof Error ? err.message : String(err)
703
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 96, S: this });
704
+ return self.#sendMessage(ctx, withPeerIds({
705
+ _tag: "Error",
706
+ message: err instanceof Error ? err.message : String(err)
707
+ }));
708
+ }));
585
709
  }
586
710
  default:
711
+ log("feed sync server ignoring unsupported message", {
712
+ peerId: self.#peerId,
713
+ tag: message._tag
714
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 112, S: this });
587
715
  return Effect2.void;
588
716
  }
589
717
  }
@@ -593,17 +721,23 @@ var SyncServer = class {
593
721
  import * as Array from "effect/Array";
594
722
  import * as Deferred from "effect/Deferred";
595
723
  import * as Effect3 from "effect/Effect";
724
+ import { ContextDisposedError } from "@dxos/context";
725
+ import { log as log2 } from "@dxos/log";
726
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/echo/feed/src/sync-client.ts";
727
+ var DEFAULT_SYNC_RPC_TIMEOUT_MS = 3e4;
596
728
  var SyncClient = class {
597
729
  #peerId;
598
730
  #serverPeerId;
599
731
  #feedStore;
600
732
  #sendMessage;
733
+ #rpcTimeoutMs;
601
734
  #handlers = /* @__PURE__ */ new Map();
602
735
  constructor(options) {
603
736
  this.#peerId = options.peerId;
604
737
  this.#serverPeerId = options.serverPeerId;
605
738
  this.#feedStore = options.feedStore;
606
739
  this.#sendMessage = options.sendMessage;
740
+ this.#rpcTimeoutMs = options.rpcTimeoutMs ?? DEFAULT_SYNC_RPC_TIMEOUT_MS;
607
741
  }
608
742
  #withPeerIds(payload) {
609
743
  if (payload._tag === "QueryRequest") {
@@ -625,18 +759,63 @@ var SyncClient = class {
625
759
  handleMessage(message) {
626
760
  const requestId = "requestId" in message && message.requestId != null ? String(message.requestId) : void 0;
627
761
  if (requestId == null) {
762
+ log2.trace("feed sync client response ignored (no request id)", {
763
+ tag: message._tag,
764
+ senderPeerId: message.senderPeerId
765
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 49, S: this });
628
766
  return Effect3.void;
629
767
  }
630
768
  const deferred = this.#handlers.get(requestId);
631
769
  if (deferred == null) {
770
+ log2.trace("feed sync client response ignored (no pending rpc)", {
771
+ requestId,
772
+ tag: message._tag,
773
+ senderPeerId: message.senderPeerId
774
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 57, S: this });
632
775
  return Effect3.void;
633
776
  }
634
777
  this.#handlers.delete(requestId);
778
+ log2("feed sync client rpc completed", {
779
+ requestId,
780
+ tag: message._tag,
781
+ blockCount: "blocks" in message ? message.blocks.length : void 0,
782
+ positionCount: "positions" in message ? message.positions.length : void 0,
783
+ error: message._tag === "Error" ? message.message : void 0
784
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 65, S: this });
635
785
  if (message._tag === "Error") {
636
786
  return Effect3.andThen(Deferred.fail(deferred, new Error(message.message)), () => Effect3.void);
637
787
  }
638
788
  return Effect3.andThen(Deferred.succeed(deferred, message), () => Effect3.void);
639
789
  }
790
+ /** Removes the pending handler entry and unregisters the ctx onDispose hook. Idempotent. */
791
+ #disposeHandler(requestId, cleanupDispose) {
792
+ cleanupDispose();
793
+ this.#handlers.delete(requestId);
794
+ }
795
+ #awaitRpcResponse(requestId, deferred, cleanupDispose, meta) {
796
+ const self = this;
797
+ const timeoutMs = self.#rpcTimeoutMs;
798
+ return Effect3.ensuring(Deferred.await(deferred).pipe(Effect3.timeoutFail({
799
+ duration: timeoutMs,
800
+ onTimeout: () => new SyncRpcTimeoutError({
801
+ requestId,
802
+ spaceId: meta.spaceId,
803
+ feedNamespace: meta.feedNamespace,
804
+ rpcTag: meta.rpcTag,
805
+ timeoutMs
806
+ })
807
+ }), Effect3.tapError((cause) => Effect3.sync(() => {
808
+ if (cause instanceof SyncRpcTimeoutError) {
809
+ log2("feed sync client rpc timed out", {
810
+ requestId,
811
+ spaceId: meta.spaceId,
812
+ feedNamespace: meta.feedNamespace,
813
+ rpcTag: meta.rpcTag,
814
+ timeoutMs
815
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 95, S: this });
816
+ }
817
+ }))), Effect3.sync(() => self.#disposeHandler(requestId, cleanupDispose)));
818
+ }
640
819
  pull(ctx, opts) {
641
820
  const self = this;
642
821
  return Effect3.gen(function* () {
@@ -647,6 +826,13 @@ var SyncClient = class {
647
826
  const requestId = crypto.randomUUID();
648
827
  const deferred = yield* Deferred.make();
649
828
  self.#handlers.set(requestId, deferred);
829
+ const cleanupDispose = ctx.disposed ? () => {
830
+ } : ctx.onDispose(() => {
831
+ Effect3.runFork(Deferred.fail(deferred, new ContextDisposedError()));
832
+ });
833
+ if (ctx.disposed) {
834
+ yield* Deferred.fail(deferred, new ContextDisposedError());
835
+ }
650
836
  const request = {
651
837
  _tag: "QueryRequest",
652
838
  requestId,
@@ -655,10 +841,26 @@ var SyncClient = class {
655
841
  position: lastPulledPosition,
656
842
  limit: opts.limit
657
843
  };
658
- yield* self.#sendMessage(ctx, self.#withPeerIds(request));
659
- const message = yield* Deferred.await(deferred);
844
+ log2("feed sync client pull rpc sending", {
845
+ requestId,
846
+ spaceId: opts.spaceId,
847
+ feedNamespace: opts.feedNamespace,
848
+ afterPosition: lastPulledPosition,
849
+ limit: opts.limit
850
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 129, S: this });
851
+ yield* self.#sendMessage(ctx, self.#withPeerIds(request)).pipe(Effect3.tapErrorCause(() => Effect3.sync(() => self.#disposeHandler(requestId, cleanupDispose))));
852
+ const message = yield* self.#awaitRpcResponse(requestId, deferred, cleanupDispose, {
853
+ spaceId: opts.spaceId,
854
+ feedNamespace: opts.feedNamespace,
855
+ rpcTag: "QueryRequest"
856
+ });
660
857
  const response = yield* self.#expectResponse(requestId, message, "QueryResponse");
661
858
  if (response.blocks.length === 0) {
859
+ log2.trace("feed sync client pull done (empty batch)", {
860
+ requestId,
861
+ spaceId: opts.spaceId,
862
+ feedNamespace: opts.feedNamespace
863
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 144, S: this });
662
864
  return {
663
865
  done: true
664
866
  };
@@ -674,11 +876,60 @@ var SyncClient = class {
674
876
  feedNamespace: opts.feedNamespace,
675
877
  lastPulledPosition: maxPulledPosition
676
878
  });
879
+ log2("feed sync client pull applied batch", {
880
+ requestId,
881
+ spaceId: opts.spaceId,
882
+ feedNamespace: opts.feedNamespace,
883
+ batchSize: response.blocks.length,
884
+ hasMore: response.hasMore,
885
+ maxPulledPosition
886
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 165, S: this });
677
887
  return {
678
888
  done: false
679
889
  };
680
890
  });
681
891
  }
892
+ /**
893
+ * Probes remote for blocks after the last pulled position without mutating local storage.
894
+ * Returns the number of blocks in the first batch (0 when caught up with remote).
895
+ */
896
+ peekPull(ctx, opts) {
897
+ const self = this;
898
+ return Effect3.gen(function* () {
899
+ const lastPulledPosition = yield* self.#feedStore.getSyncState({
900
+ spaceId: opts.spaceId,
901
+ feedNamespace: opts.feedNamespace
902
+ });
903
+ const requestId = crypto.randomUUID();
904
+ const deferred = yield* Deferred.make();
905
+ self.#handlers.set(requestId, deferred);
906
+ const cleanupDispose = ctx.disposed ? () => {
907
+ } : ctx.onDispose(() => {
908
+ Effect3.runFork(Deferred.fail(deferred, new ContextDisposedError()));
909
+ });
910
+ if (ctx.disposed) {
911
+ yield* Deferred.fail(deferred, new ContextDisposedError());
912
+ }
913
+ const request = {
914
+ _tag: "QueryRequest",
915
+ requestId,
916
+ spaceId: opts.spaceId,
917
+ feedNamespace: opts.feedNamespace,
918
+ position: lastPulledPosition,
919
+ limit: opts.limit
920
+ };
921
+ yield* self.#sendMessage(ctx, self.#withPeerIds(request)).pipe(Effect3.tapErrorCause(() => Effect3.sync(() => self.#disposeHandler(requestId, cleanupDispose))));
922
+ const message = yield* self.#awaitRpcResponse(requestId, deferred, cleanupDispose, {
923
+ spaceId: opts.spaceId,
924
+ feedNamespace: opts.feedNamespace,
925
+ rpcTag: "QueryRequest"
926
+ });
927
+ const response = yield* self.#expectResponse(requestId, message, "QueryResponse");
928
+ return {
929
+ blocksToPull: response.blocks.length
930
+ };
931
+ });
932
+ }
682
933
  push(ctx, opts) {
683
934
  const self = this;
684
935
  return Effect3.gen(function* () {
@@ -689,6 +940,10 @@ var SyncClient = class {
689
940
  limit: opts.limit
690
941
  });
691
942
  if (unpositioned.blocks.length === 0) {
943
+ log2.trace("feed sync client push skipped (nothing to send)", {
944
+ spaceId: opts.spaceId,
945
+ feedNamespace: opts.feedNamespace
946
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 227, S: this });
692
947
  return {
693
948
  done: true
694
949
  };
@@ -696,6 +951,13 @@ var SyncClient = class {
696
951
  const requestId = crypto.randomUUID();
697
952
  const deferred = yield* Deferred.make();
698
953
  self.#handlers.set(requestId, deferred);
954
+ const cleanupDispose = ctx.disposed ? () => {
955
+ } : ctx.onDispose(() => {
956
+ Effect3.runFork(Deferred.fail(deferred, new ContextDisposedError()));
957
+ });
958
+ if (ctx.disposed) {
959
+ yield* Deferred.fail(deferred, new ContextDisposedError());
960
+ }
699
961
  const request = {
700
962
  _tag: "AppendRequest",
701
963
  requestId,
@@ -703,8 +965,18 @@ var SyncClient = class {
703
965
  feedNamespace: opts.feedNamespace,
704
966
  blocks: unpositioned.blocks
705
967
  };
706
- yield* self.#sendMessage(ctx, self.#withPeerIds(request));
707
- const message = yield* Deferred.await(deferred);
968
+ log2("feed sync client push rpc sending", {
969
+ requestId,
970
+ spaceId: opts.spaceId,
971
+ feedNamespace: opts.feedNamespace,
972
+ blockCount: unpositioned.blocks.length
973
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 251, S: this });
974
+ yield* self.#sendMessage(ctx, self.#withPeerIds(request)).pipe(Effect3.tapErrorCause(() => Effect3.sync(() => self.#disposeHandler(requestId, cleanupDispose))));
975
+ const message = yield* self.#awaitRpcResponse(requestId, deferred, cleanupDispose, {
976
+ spaceId: opts.spaceId,
977
+ feedNamespace: opts.feedNamespace,
978
+ rpcTag: "AppendRequest"
979
+ });
708
980
  const response = yield* self.#expectResponse(requestId, message, "AppendResponse");
709
981
  yield* self.#feedStore.setPosition({
710
982
  spaceId: opts.spaceId,
@@ -716,6 +988,12 @@ var SyncClient = class {
716
988
  position
717
989
  }))
718
990
  });
991
+ log2("feed sync client push positions applied", {
992
+ requestId,
993
+ spaceId: opts.spaceId,
994
+ feedNamespace: opts.feedNamespace,
995
+ positionCount: response.positions.length
996
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 274, S: this });
719
997
  return {
720
998
  done: false
721
999
  };
@@ -734,9 +1012,11 @@ var SyncClient = class {
734
1012
  };
735
1013
 
736
1014
  export {
1015
+ SyncRpcTimeoutError,
737
1016
  PositionConflictError,
738
1017
  FeedStore,
739
1018
  SyncServer,
1019
+ DEFAULT_SYNC_RPC_TIMEOUT_MS,
740
1020
  SyncClient
741
1021
  };
742
- //# sourceMappingURL=chunk-PENEKCN2.mjs.map
1022
+ //# sourceMappingURL=chunk-UC5ETQIJ.mjs.map