@uuaid/pillar 2.4.1 → 2.5.3

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.
@@ -99,14 +99,14 @@ export class Observability {
99
99
  journal({ event, ...fields }) {
100
100
  if (!this.sinkPath) return;
101
101
  mkdirSync(dirname(this.sinkPath), { recursive: true });
102
- appendFileSync(this.sinkPath, JSON.stringify({ at: new Date().toISOString(), event, ...fields }) + "\n");
102
+ appendFileSync(this.sinkPath, JSON.stringify({ at: new Date().toISOString(), event, ...fields }) + "\n", { mode: 0o600 });
103
103
  }
104
104
 
105
105
  /** Append a full state snapshot line to the sink (no-op without a sinkPath). */
106
106
  flush() {
107
107
  if (!this.sinkPath) return;
108
108
  mkdirSync(dirname(this.sinkPath), { recursive: true });
109
- appendFileSync(this.sinkPath, JSON.stringify({ type: "snapshot", ...this.snapshot() }) + "\n");
109
+ appendFileSync(this.sinkPath, JSON.stringify({ type: "snapshot", ...this.snapshot() }) + "\n", { mode: 0o600 });
110
110
  }
111
111
 
112
112
  /* ------------------------------------------------ read paths --------- */
package/src/pillar.mjs CHANGED
@@ -314,18 +314,36 @@ export class Pillar {
314
314
  });
315
315
 
316
316
  // 1. Address book path: direct libp2p dial (lowest latency, no third party).
317
+ // A stream that closes is not a receipt: nothing tells us the recipient read
318
+ // it, and against throttled receivers most such sends were lost while marked
319
+ // delivered (2.5.3). So with carriers configured the envelope also goes to
320
+ // them, in parallel, and only a carrier's acceptance marks the row delivered
321
+ // (what status 'delivered' has always meant, mailbox.mjs). The recipient's
322
+ // mailbox keys on (sender, envelope id), so a copy that arrives both ways is
323
+ // stored and handled once.
317
324
  if (this.node && book?.peerId && book?.multiaddr) {
318
- try {
319
- const delivery = await this._deliverToPeer(envelope, book.peerId, book.multiaddr);
320
- this.metrics.inc("send.via.direct");
321
- this.metrics.observe("send.latencyMs.via.direct", Date.now() - t0);
322
- return { envelope, persisted, via: "direct", delivery };
323
- } catch (e) {
324
- if (!this.carrierClient) {
325
- this.metrics.inc("send.error.direct-delivery-failed");
326
- throw e;
327
- } // else fall through to carrier
325
+ const [direct, carrier] = await Promise.allSettled([
326
+ this._deliverToPeer(envelope, book.peerId, book.multiaddr),
327
+ this.carrierClient ? this.carrierClient.deliver(envelope) : Promise.resolve(null),
328
+ ]);
329
+ const sentDirect = direct.status === "fulfilled" ? direct.value : null;
330
+ if (carrier.status === "fulfilled" && carrier.value) {
331
+ this.mailbox.markOutboundDelivered(envelope.id, envelopeSha(envelope));
332
+ const via = sentDirect ? "direct+carrier" : "carrier";
333
+ this.metrics.inc(`send.via.${via}`);
334
+ this.metrics.observe(`send.latencyMs.via.${via}`, Date.now() - t0);
335
+ return { envelope, persisted, via, delivery: carrier.value, direct: sentDirect, confirmed: true };
336
+ }
337
+ if (sentDirect) {
338
+ // Written to a stream, and no carrier holds a copy: nothing confirms it
339
+ // arrived, so the row stays pending (the recipient's signed ack, if it
340
+ // sends one, is recorded when it comes).
341
+ this.metrics.inc("send.via.direct-unconfirmed");
342
+ return { envelope, persisted, via: "direct", delivery: sentDirect, direct: sentDirect, confirmed: false,
343
+ carrierError: carrier.status === "rejected" ? carrier.reason : null };
328
344
  }
345
+ this.metrics.inc("send.error.direct-delivery-failed");
346
+ throw carrier.status === "rejected" ? carrier.reason : direct.reason;
329
347
  }
330
348
 
331
349
  // 2. Carrier path: universal store-and-forward.
@@ -352,7 +370,8 @@ export class Pillar {
352
370
  const delivery = await this._deliverToPeer(envelope, dhtAdvert.peerId, dhtAdvert.addrs?.[0]);
353
371
  this.metrics.inc("send.via.dht");
354
372
  this.metrics.observe("send.latencyMs.via.dht", Date.now() - t0);
355
- return { envelope, persisted, via: "dht", delivery };
373
+ // No carrier: a closed stream confirms nothing, so the row stays pending.
374
+ return { envelope, persisted, via: "dht", delivery, direct: delivery, confirmed: false };
356
375
  }
357
376
 
358
377
  /**
@@ -585,7 +604,7 @@ export class Pillar {
585
604
  const line = JSON.stringify(envelope) + "\n";
586
605
  stream.send(u8FromString(line));
587
606
  await stream.close();
588
- this.mailbox.markOutboundDelivered(envelope.id, envelopeSha(envelope));
607
+ // Not marked delivered here: a closed stream is not a receipt (2.5.3).
589
608
  return { peerId: peerIdStr };
590
609
  }
591
610
 
@@ -633,7 +652,7 @@ export class Pillar {
633
652
  const line = JSON.stringify(envelope) + "\n";
634
653
  stream.send(u8FromString(line));
635
654
  await stream.close();
636
- this.mailbox.markOutboundDelivered(envelope.id, envelopeSha(envelope));
655
+ // Not marked delivered: a closed stream is not a receipt (2.5.3).
637
656
  return envelope;
638
657
  }
639
658
 
@@ -107,8 +107,9 @@ function staleBackupFiles(dbFile) {
107
107
  .map((f) => join(dirname(dbFile), f));
108
108
  }
109
109
 
110
- function removeBackup(backup) {
111
- for (const f of [backup, `${backup}-journal`]) rmSync(f, { force: true });
110
+ /** The backup and every file SQLite may leave beside it (2.5.3, AC7-Kimi d: -wal and -shm too). */
111
+ export function removeBackup(backup) {
112
+ for (const f of [backup, `${backup}-journal`, `${backup}-wal`, `${backup}-shm`]) rmSync(f, { force: true });
112
113
  }
113
114
 
114
115
  /**