@uuaid/pillar 2.6.1 → 2.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +325 -7
- package/bin/carrier.mjs +21 -0
- package/bin/pillar-ask.mjs +4 -1
- package/bin/pillar-blob.mjs +6 -0
- package/bin/pillar-directory.mjs +282 -16
- package/bin/pillar-escrow.mjs +4 -1
- package/bin/pillar-group-v2.mjs +6 -0
- package/bin/pillar-notify.mjs +13 -0
- package/bin/pillar-responder.mjs +266 -23
- package/bin/pillar.mjs +512 -257
- package/package.json +17 -25
- package/src/cli-help.mjs +23 -0
- package/src/group-v2-cli.mjs +1 -1
- package/src/identity/trust.mjs +72 -6
- package/src/mailbox/mailbox.mjs +313 -12
- package/src/net/carrier-server.mjs +34 -2
- package/src/net/refusals.mjs +2 -0
- package/src/net/seed-registry.mjs +2 -1
- package/src/net/transport.mjs +320 -91
- package/src/node-check.mjs +29 -8
- package/src/notify-events.mjs +148 -0
- package/src/notify-log.mjs +124 -0
- package/src/notify-runner.mjs +372 -0
- package/src/notify-store.mjs +736 -0
- package/src/notify.mjs +350 -0
- package/src/pillar.mjs +382 -27
- package/src/readers.mjs +2 -2
- package/src/registry/federation.mjs +3056 -0
- package/src/registry/gossip.mjs +294 -0
- package/src/registry/heartbeats.mjs +205 -0
- package/src/registry/lineage-verifier.mjs +972 -0
- package/src/registry/lineage.mjs +984 -0
- package/src/registry/listings.mjs +612 -0
- package/src/registry/resolve.mjs +376 -0
- package/src/registry/stats.mjs +508 -0
- package/src/sqlite-loader.mjs +40 -0
- package/src/vendor/pillar-core.mjs +22 -0
- package/src/vendor/uuaid-soul.mjs +2246 -0
- package/src/vnext/a6b-common.mjs +270 -0
- package/src/vnext/b1-cli.mjs +970 -0
- package/src/vnext/card-cli.mjs +370 -0
- package/src/vnext/greeter-operator.mjs +160 -0
- package/src/vnext/greeter-suggest.mjs +318 -0
- package/src/vnext/greeter.mjs +571 -0
- package/src/vnext/homes.mjs +48 -0
- package/src/vnext/init-cli.mjs +489 -0
- package/src/vnext/intro-cli.mjs +81 -0
- package/src/vnext/json-dir-mailbox.mjs +304 -0
- package/src/vnext/jsonl-mailbox.mjs +1132 -0
- package/src/vnext/knock-answer.mjs +162 -0
- package/src/vnext/knock-cli.mjs +520 -0
- package/src/vnext/knock-lane.mjs +312 -0
- package/src/vnext/knock-responder.mjs +344 -0
- package/src/vnext/lineage-cli.mjs +653 -0
- package/src/vnext/listing-cli.mjs +323 -0
- package/src/vnext/resolve-cli.mjs +104 -0
- package/src/vnext/soul-cli.mjs +666 -0
- package/src/vnext/verify-cli.mjs +333 -0
- package/src/vnext/w4-redirect.mjs +178 -0
- package/src/vnext/wave-a.mjs +193 -0
- package/src/vnext/witness-cli.mjs +547 -0
package/src/mailbox/mailbox.mjs
CHANGED
|
@@ -15,12 +15,17 @@
|
|
|
15
15
|
* chain[n] = sha256(chain[n-1] + envelope_sha256[n])
|
|
16
16
|
* Where envelope_sha256[i] = sha256(JCS(envelope[i])).
|
|
17
17
|
*/
|
|
18
|
-
import Database from "better-sqlite3";
|
|
19
18
|
import { createHash } from "node:crypto";
|
|
20
|
-
import { mkdirSync } from "node:fs";
|
|
21
|
-
import { dirname } from "node:path";
|
|
19
|
+
import { existsSync, mkdirSync, statSync } from "node:fs";
|
|
20
|
+
import { dirname, join } from "node:path";
|
|
22
21
|
import { jcs } from "../identity/jcs.mjs";
|
|
22
|
+
import { requireBetterSqlite3, tryLoadBetterSqlite3 } from "../sqlite-loader.mjs";
|
|
23
23
|
import { rekeyToSenderUnique } from "../sqlite-rekey.mjs";
|
|
24
|
+
import { JsonDirMailboxReader, hasClientIdentity, resolveJsonDirMailbox } from "../vnext/json-dir-mailbox.mjs";
|
|
25
|
+
import { JsonlMailboxStore, hasJsonlMailboxFiles, resolveWritableJsonlDir } from "../vnext/jsonl-mailbox.mjs";
|
|
26
|
+
import { KNOCK_LANE_CAP, KNOCK_RATE_WINDOW_MS } from "../vnext/knock-lane.mjs";
|
|
27
|
+
|
|
28
|
+
export { JsonDirMailboxReader, JsonlMailboxStore, resolveJsonDirMailbox };
|
|
24
29
|
|
|
25
30
|
// An envelope id is unique only within its sender (Annex B5(a),
|
|
26
31
|
// src/sqlite-rekey.mjs). The outbox keeps UNIQUE(envelope_id): every id in it
|
|
@@ -89,10 +94,64 @@ export function isErasedRow(row) {
|
|
|
89
94
|
return !!row && row.envelope_json === ERASED_ENVELOPE_JSON;
|
|
90
95
|
}
|
|
91
96
|
|
|
97
|
+
function isRegularFile(p) {
|
|
98
|
+
try { return typeof p === "string" && p !== ":memory:" && statSync(p).isFile(); } catch { return false; }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function toCanonicalIso(value) {
|
|
102
|
+
const ms = typeof value === "number" ? value : Date.parse(value);
|
|
103
|
+
return Number.isFinite(ms) ? new Date(ms).toISOString() : new Date().toISOString();
|
|
104
|
+
}
|
|
105
|
+
|
|
92
106
|
export class Mailbox {
|
|
93
|
-
constructor(path) {
|
|
94
|
-
|
|
95
|
-
|
|
107
|
+
constructor(path, opts = {}) {
|
|
108
|
+
const jsonDir = resolveJsonDirMailbox(path);
|
|
109
|
+
let clientHome = jsonDir !== null && !existsSync(join(dirname(jsonDir), "keychain.json"));
|
|
110
|
+
// A pillar JSONL home whose key is not keychain.json (pillar-escrow: kc.json)
|
|
111
|
+
// has the same mailbox files but no client identity: it stays writable.
|
|
112
|
+
// 2.7.0 opened it read-only from the second run on (V1 F5, 2.7.1 A6).
|
|
113
|
+
if (clientHome && !hasClientIdentity(dirname(jsonDir))) clientHome = false; // @rule mailbox.pillar-jsonl-home-writable
|
|
114
|
+
if (clientHome) {
|
|
115
|
+
this.mode = "json-dir";
|
|
116
|
+
this.jsonDir = jsonDir;
|
|
117
|
+
this.db = null;
|
|
118
|
+
this._jsonDir = new JsonDirMailboxReader(jsonDir);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
let dbPath = path;
|
|
122
|
+
try {
|
|
123
|
+
if (
|
|
124
|
+
typeof path === "string" &&
|
|
125
|
+
statSync(path).isDirectory() &&
|
|
126
|
+
(existsSync(join(path, "keychain.json")) || existsSync(join(path, "mailbox.db")))
|
|
127
|
+
) {
|
|
128
|
+
dbPath = join(path, "mailbox.db");
|
|
129
|
+
}
|
|
130
|
+
} catch { /* non-existent or non-directory path: pass through to SQLite / JSONL */ }
|
|
131
|
+
const Database = opts?.forceJsonl ? null : tryLoadBetterSqlite3();
|
|
132
|
+
// A home that already has its SQLite store keeps it. Without better-sqlite3
|
|
133
|
+
// refuse (sqlite-not-installed); a JSONL fallback would hide the stored mail
|
|
134
|
+
// and start a second store beside it.
|
|
135
|
+
if (!Database && !opts?.forceJsonl && isRegularFile(dbPath)) requireBetterSqlite3(`Mailbox (existing store ${dbPath})`); // @rule mailbox.sqlite-missing-refuses-existing-db
|
|
136
|
+
const jsonlDir = resolveWritableJsonlDir(path);
|
|
137
|
+
const hasExistingJsonl =
|
|
138
|
+
typeof dbPath === "string" &&
|
|
139
|
+
dbPath !== ":memory:" &&
|
|
140
|
+
!existsSync(dbPath) &&
|
|
141
|
+
jsonlDir !== ":memory:" &&
|
|
142
|
+
hasJsonlMailboxFiles(jsonlDir);
|
|
143
|
+
if (!Database || hasExistingJsonl) {
|
|
144
|
+
this.mode = "jsonl";
|
|
145
|
+
this.jsonDir = jsonlDir;
|
|
146
|
+
this.db = null;
|
|
147
|
+
this._jsonDir = new JsonlMailboxStore(jsonlDir);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
this.mode = "sqlite";
|
|
151
|
+
this.jsonDir = null;
|
|
152
|
+
this._jsonDir = null;
|
|
153
|
+
mkdirSync(dirname(dbPath), { recursive: true });
|
|
154
|
+
this.db = new Database(dbPath);
|
|
96
155
|
this.db.pragma("journal_mode = WAL");
|
|
97
156
|
this.db.pragma("synchronous = NORMAL");
|
|
98
157
|
this.db.exec(SCHEMA);
|
|
@@ -142,6 +201,25 @@ export class Mailbox {
|
|
|
142
201
|
["recipient_ack_at", "text"], ["recipient_ack_id", "text"], ["recipient_read_at", "text"],
|
|
143
202
|
]);
|
|
144
203
|
this.db.exec(`
|
|
204
|
+
create table if not exists knocks (
|
|
205
|
+
seq integer primary key autoincrement,
|
|
206
|
+
envelope_id text not null,
|
|
207
|
+
sender text not null,
|
|
208
|
+
sender_lineage text not null,
|
|
209
|
+
recipient_lineage text not null,
|
|
210
|
+
payment_type text not null,
|
|
211
|
+
pow_stamp text,
|
|
212
|
+
received_at text not null,
|
|
213
|
+
status text not null default 'pending',
|
|
214
|
+
evicted_at text,
|
|
215
|
+
answered_at text,
|
|
216
|
+
answer_decision text,
|
|
217
|
+
answer_envelope_id text,
|
|
218
|
+
envelope_json text not null,
|
|
219
|
+
unique (sender, envelope_id)
|
|
220
|
+
);
|
|
221
|
+
create index if not exists knocks_status_seq on knocks (status, seq);
|
|
222
|
+
create index if not exists knocks_pair on knocks (sender_lineage, recipient_lineage, received_at);
|
|
145
223
|
create table if not exists readers (
|
|
146
224
|
name text primary key,
|
|
147
225
|
cursor integer not null default 0,
|
|
@@ -198,9 +276,11 @@ export class Mailbox {
|
|
|
198
276
|
// and `pillar group` verbs naming it go to v2 (Pillar._receive, groupCommandRoute),
|
|
199
277
|
// and with its v2 store file gone it is cleared, not handed back to v1.
|
|
200
278
|
addCols("groups", [["v2_synced_at", "text"]]);
|
|
279
|
+
addCols("knocks", [["pow_stamp", "text"]]);
|
|
280
|
+
this.db.exec("create index if not exists knocks_pow_stamp on knocks (sender_lineage, recipient_lineage, pow_stamp);");
|
|
201
281
|
}
|
|
202
282
|
|
|
203
|
-
close() { try { this.db.close(); } catch (_e) {} }
|
|
283
|
+
close() { if (this._jsonDir) return this._jsonDir.close(); try { this.db.close(); } catch (_e) {} }
|
|
204
284
|
/**
|
|
205
285
|
* Local content erasure, not log deletion. Exactly one selector:
|
|
206
286
|
* { pairs: [{ sender, envelope_id }] } — sender scopes BOTH logs;
|
|
@@ -222,6 +302,7 @@ export class Mailbox {
|
|
|
222
302
|
* backups, old JS views, or storage-device snapshots have been erased.
|
|
223
303
|
*/
|
|
224
304
|
eraseRows(selector) {
|
|
305
|
+
if (this._jsonDir) return this._jsonDir.eraseRows(selector);
|
|
225
306
|
const bad = () => new TypeError("eraseRows: provide exactly one non-empty selector: pairs [{sender, envelope_id}], conversation, or groupId");
|
|
226
307
|
if (!selector || typeof selector !== "object" || Array.isArray(selector)) throw bad();
|
|
227
308
|
const keys = Object.keys(selector);
|
|
@@ -263,6 +344,13 @@ export class Mailbox {
|
|
|
263
344
|
const bindings = mode === "pairs" ? pairs.map((p) => [p.sender, p.envelope_id]) : [[selector[mode]]];
|
|
264
345
|
for (const args of bindings) counts[table] += update.run(ERASED_ENVELOPE_JSON, ...args).changes;
|
|
265
346
|
}
|
|
347
|
+
if (mode === "pairs" || mode === "conversation") {
|
|
348
|
+
const knockWhere = mode === "pairs" ? "sender = ? and envelope_id = ?" : "sender = ?";
|
|
349
|
+
const updateKnocks = this.db.prepare(`update knocks set envelope_json = ?
|
|
350
|
+
where ${LIVE_ENVELOPE_SQL} and (${knockWhere})`);
|
|
351
|
+
const bindings = mode === "pairs" ? pairs.map((p) => [p.sender, p.envelope_id]) : [[selector[mode]]];
|
|
352
|
+
for (const args of bindings) updateKnocks.run(ERASED_ENVELOPE_JSON, ...args);
|
|
353
|
+
}
|
|
266
354
|
}).immediate();
|
|
267
355
|
committed = true;
|
|
268
356
|
// secure_delete clears current cells, not pages freed by an earlier
|
|
@@ -285,11 +373,13 @@ export class Mailbox {
|
|
|
285
373
|
|
|
286
374
|
/** Carrier polling cursors: carrier URL -> highest seq we have consumed. */
|
|
287
375
|
getCarrierCursors() {
|
|
376
|
+
if (this._jsonDir) return this._jsonDir.getCarrierCursors();
|
|
288
377
|
const rows = this.db.prepare("select carrier, seq from carrier_cursors").all();
|
|
289
378
|
return Object.fromEntries(rows.map(r => [r.carrier, r.seq]));
|
|
290
379
|
}
|
|
291
380
|
|
|
292
381
|
setCarrierCursor(carrier, seq) {
|
|
382
|
+
if (this._jsonDir) return this._jsonDir.setCarrierCursor(carrier, seq);
|
|
293
383
|
this.db.prepare(
|
|
294
384
|
"insert into carrier_cursors (carrier, seq) values (?, ?) on conflict(carrier) do update set seq = max(seq, excluded.seq)"
|
|
295
385
|
).run(carrier, seq);
|
|
@@ -313,6 +403,7 @@ export class Mailbox {
|
|
|
313
403
|
* atomically with the insert; concurrent appends are serialized via the SQLite transaction.
|
|
314
404
|
*/
|
|
315
405
|
appendOutbound(envelope, { thread = null, groupId = null, groupMsgId = null } = {}) {
|
|
406
|
+
if (this._jsonDir) return this._jsonDir.appendOutbound(envelope, { thread, groupId, groupMsgId });
|
|
316
407
|
return this.db.transaction(() => {
|
|
317
408
|
const envSha = Mailbox.envelopeSha(envelope);
|
|
318
409
|
const prev = this._getHead("outbox");
|
|
@@ -341,6 +432,7 @@ export class Mailbox {
|
|
|
341
432
|
* see the row unquarantined between the two.
|
|
342
433
|
*/
|
|
343
434
|
appendInbound(envelope, { pulledBy = null, quarantined = null } = {}) {
|
|
435
|
+
if (this._jsonDir) return this._jsonDir.appendInbound(envelope, { pulledBy, quarantined });
|
|
344
436
|
return this.db.transaction(() => {
|
|
345
437
|
const sender = envelope.sender ?? envelope.from ?? "unknown";
|
|
346
438
|
const envSha = Mailbox.envelopeSha(envelope);
|
|
@@ -373,11 +465,13 @@ export class Mailbox {
|
|
|
373
465
|
|
|
374
466
|
/** True if we sent this envelope (trust gate: is a receipt answering OUR send?). */
|
|
375
467
|
hasOutbound(envelopeId) {
|
|
468
|
+
if (this._jsonDir) return this._jsonDir.hasOutbound(envelopeId);
|
|
376
469
|
return !!this.db.prepare("select 1 from outbox where envelope_id = ?").get(envelopeId);
|
|
377
470
|
}
|
|
378
471
|
|
|
379
472
|
/** Full outbox row by envelope id (VDA message-delivery instrument). */
|
|
380
473
|
getOutbound(envelopeId) {
|
|
474
|
+
if (this._jsonDir) return this._jsonDir.getOutbound(envelopeId);
|
|
381
475
|
return this.db.prepare("select * from outbox where envelope_id = ?").get(envelopeId) ?? null;
|
|
382
476
|
}
|
|
383
477
|
|
|
@@ -387,6 +481,7 @@ export class Mailbox {
|
|
|
387
481
|
* may be any sender's: pass the sender whenever it is known.
|
|
388
482
|
*/
|
|
389
483
|
getInbound(envelopeId, sender) {
|
|
484
|
+
if (this._jsonDir) return this._jsonDir.getInbound(envelopeId, sender);
|
|
390
485
|
if (sender === undefined) {
|
|
391
486
|
return this.db.prepare("select * from inbox where envelope_id = ? order by seq asc limit 1").get(envelopeId) ?? null;
|
|
392
487
|
}
|
|
@@ -394,35 +489,195 @@ export class Mailbox {
|
|
|
394
489
|
}
|
|
395
490
|
|
|
396
491
|
markOutboundDelivered(envelopeId, ackSha256) {
|
|
492
|
+
if (this._jsonDir) return this._jsonDir.markOutboundDelivered(envelopeId, ackSha256);
|
|
397
493
|
this.db.prepare("update outbox set status = 'delivered', delivered_at = ?, ack_sha256 = ? where envelope_id = ?")
|
|
398
494
|
.run(new Date().toISOString(), ackSha256 ?? null, envelopeId);
|
|
399
495
|
}
|
|
400
496
|
|
|
401
|
-
outboxTip() { return this._getHead("outbox") ?? "0".repeat(64); }
|
|
402
|
-
inboxTip() { return this._getHead("inbox") ?? "0".repeat(64); }
|
|
497
|
+
outboxTip() { return this._jsonDir ? this._jsonDir.outboxTip() : (this._getHead("outbox") ?? "0".repeat(64)); }
|
|
498
|
+
inboxTip() { return this._jsonDir ? this._jsonDir.inboxTip() : (this._getHead("inbox") ?? "0".repeat(64)); }
|
|
403
499
|
|
|
404
500
|
outboxSince(seq = 0, limit = 100) {
|
|
501
|
+
if (this._jsonDir) return this._jsonDir.outboxSince(seq, limit);
|
|
405
502
|
return this.db.prepare(`select * from outbox where seq > ? and ${LIVE_ENVELOPE_SQL} order by seq asc limit ?`).all(seq, limit);
|
|
406
503
|
}
|
|
407
504
|
inboxSince(seq = 0, limit = 100) {
|
|
505
|
+
if (this._jsonDir) return this._jsonDir.inboxSince(seq, limit);
|
|
408
506
|
return this.db.prepare(`select * from inbox where seq > ? and ${LIVE_ENVELOPE_SQL} order by seq asc limit ?`).all(seq, limit);
|
|
409
507
|
}
|
|
410
508
|
|
|
411
|
-
|
|
509
|
+
_regularInboxWhere() {
|
|
510
|
+
let clause = `quarantined is null and ${LIVE_ENVELOPE_SQL}`;
|
|
511
|
+
clause += " and coalesce(json_extract(envelope_json, '$.kind'), '') <> 'knock'"; // @rule mailbox.k5-sql-exclude-knocks
|
|
512
|
+
return clause;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** Envelopes never shown to the operator (read_at null, not quarantined, never knocks). Oldest first. */
|
|
412
516
|
inboxUnread(limit = 100) {
|
|
413
|
-
|
|
517
|
+
if (this._jsonDir) return this._jsonDir.inboxUnread(limit);
|
|
518
|
+
return this.db.prepare(`select * from inbox where read_at is null and ${this._regularInboxWhere()} order by seq asc limit ?`).all(limit);
|
|
414
519
|
}
|
|
415
520
|
|
|
416
521
|
/** Trust-gated envelopes: stored as evidence, never surfaced to handlers. */
|
|
417
522
|
inboxQuarantined(limit = 100) {
|
|
523
|
+
if (this._jsonDir) return this._jsonDir.inboxQuarantined(limit);
|
|
418
524
|
return this.db.prepare(`select * from inbox where quarantined is not null and ${LIVE_ENVELOPE_SQL} order by seq asc limit ?`).all(limit);
|
|
419
525
|
}
|
|
420
526
|
|
|
527
|
+
/** Senders holding `envelopeId` quarantined (used by `trust quarantine --release`). */
|
|
528
|
+
quarantinedSenders(envelopeId) {
|
|
529
|
+
if (this._jsonDir) return this._jsonDir.quarantinedSenders(envelopeId);
|
|
530
|
+
return this.db.prepare("select sender from inbox where envelope_id = ? and quarantined is not null").all(envelopeId).map(r => r.sender);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** Lowest seq of any unread, non-quarantined regular inbox row, or null when none exist. */
|
|
534
|
+
oldestUnreadSeq() {
|
|
535
|
+
if (this._jsonDir) return this._jsonDir.oldestUnreadSeq();
|
|
536
|
+
const row = this.db.prepare(`select min(seq) as s from inbox where read_at is null and ${this._regularInboxWhere()}`).get();
|
|
537
|
+
return row?.s ?? null;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// ── Knocks lane (vNext §4 K5, K6, T4.4, K8) ──────────────────────────────
|
|
541
|
+
|
|
542
|
+
_k6ActiveWhere() {
|
|
543
|
+
let clause = "answered_at is null";
|
|
544
|
+
clause = "(answered_at is null or coalesce(answer_decision, '') <> 'accept')"; // @rule mailbox.k6-only-accept-resets
|
|
545
|
+
return clause;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/** Read-only K6 check: true if a knock from this lineage pair (not accepted) arrived within windowMs. */
|
|
549
|
+
hasUnansweredRecentKnock(senderLineage, recipientLineage, { now = new Date().toISOString(), windowMs = KNOCK_RATE_WINDOW_MS } = {}) {
|
|
550
|
+
if (this._jsonDir) return this._jsonDir.hasUnansweredRecentKnock(senderLineage, recipientLineage, { now, windowMs });
|
|
551
|
+
const nowIso = toCanonicalIso(now);
|
|
552
|
+
const cutoff = new Date(Date.parse(nowIso) - windowMs).toISOString();
|
|
553
|
+
const row = this.db.prepare(
|
|
554
|
+
`select 1 from knocks where sender_lineage = ? and recipient_lineage = ? and ${this._k6ActiveWhere()} and received_at > ? limit 1`
|
|
555
|
+
).get(senderLineage, recipientLineage, cutoff);
|
|
556
|
+
return !!row;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** Read-only K2 check: true if another knock for this lineage pair already consumed `powStamp`. */
|
|
560
|
+
hasReplayedPowStamp(senderLineage, recipientLineage, powStamp, { envelopeId = null } = {}) {
|
|
561
|
+
if (this._jsonDir) return this._jsonDir.hasReplayedPowStamp(senderLineage, recipientLineage, powStamp, { envelopeId });
|
|
562
|
+
if (typeof powStamp !== "string" || powStamp.length === 0) return false;
|
|
563
|
+
const row = envelopeId == null
|
|
564
|
+
? this.db.prepare(
|
|
565
|
+
"select 1 from knocks where sender_lineage = ? and recipient_lineage = ? and pow_stamp = ? limit 1"
|
|
566
|
+
).get(senderLineage, recipientLineage, powStamp)
|
|
567
|
+
: this.db.prepare(
|
|
568
|
+
"select 1 from knocks where sender_lineage = ? and recipient_lineage = ? and pow_stamp = ? and envelope_id <> ? limit 1"
|
|
569
|
+
).get(senderLineage, recipientLineage, powStamp, envelopeId);
|
|
570
|
+
return !!row;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Atomically enforce K6 (1 knock / pair / 24h unless accepted), K2 PoW stamp
|
|
575
|
+
* single-use per lineage pair, and T4.4 (cap 50 pending; evict oldest
|
|
576
|
+
* non-introduction first, introduction only when all 50 are introductions),
|
|
577
|
+
* then admit the knock into the `knocks` lane.
|
|
578
|
+
*/
|
|
579
|
+
admitKnock({
|
|
580
|
+
envelope,
|
|
581
|
+
senderLineage,
|
|
582
|
+
recipientLineage,
|
|
583
|
+
paymentType,
|
|
584
|
+
powStamp = null,
|
|
585
|
+
now = new Date().toISOString(),
|
|
586
|
+
cap = KNOCK_LANE_CAP,
|
|
587
|
+
windowMs = KNOCK_RATE_WINDOW_MS,
|
|
588
|
+
}) {
|
|
589
|
+
if (this._jsonDir) return this._jsonDir.admitKnock({ envelope, senderLineage, recipientLineage, paymentType, powStamp, now, cap, windowMs });
|
|
590
|
+
const nowIso = toCanonicalIso(now);
|
|
591
|
+
const normalizedStamp = paymentType === "pow" && typeof powStamp === "string" && powStamp.length > 0 ? powStamp : null;
|
|
592
|
+
return this.db.transaction(() => {
|
|
593
|
+
const sender = envelope.sender ?? envelope.from ?? "unknown";
|
|
594
|
+
const existing = this.db.prepare("select seq from knocks where envelope_id = ? and sender = ?").get(envelope.id, sender);
|
|
595
|
+
if (existing) {
|
|
596
|
+
return { admitted: false, duplicate: true, rateLimited: false, stampReplayed: false, seq: existing.seq, evicted: [] };
|
|
597
|
+
}
|
|
598
|
+
const cutoff = new Date(Date.parse(nowIso) - windowMs).toISOString();
|
|
599
|
+
const recent = this.db.prepare(
|
|
600
|
+
`select seq, envelope_id from knocks where sender_lineage = ? and recipient_lineage = ? and ${this._k6ActiveWhere()} and received_at > ? limit 1`
|
|
601
|
+
).get(senderLineage, recipientLineage, cutoff);
|
|
602
|
+
if (recent) return { admitted: false, duplicate: false, rateLimited: true, stampReplayed: false, evicted: [] }; // @rule mailbox.k6-rate-limit
|
|
603
|
+
|
|
604
|
+
if (normalizedStamp !== null) {
|
|
605
|
+
const replayed = this.db.prepare(
|
|
606
|
+
"select seq from knocks where sender_lineage = ? and recipient_lineage = ? and pow_stamp = ? and envelope_id <> ? limit 1"
|
|
607
|
+
).get(senderLineage, recipientLineage, normalizedStamp, envelope.id);
|
|
608
|
+
if (replayed) return { admitted: false, duplicate: false, rateLimited: false, stampReplayed: true, evicted: [] }; // @rule mailbox.k2-pow-stamp-replay
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const evicted = [];
|
|
612
|
+
let pending = this.db.prepare(`select count(*) as n from knocks where status = 'pending' and ${LIVE_ENVELOPE_SQL}`).get().n;
|
|
613
|
+
while (pending >= cap) {
|
|
614
|
+
let victim = null;
|
|
615
|
+
victim = this.db.prepare(`select seq, envelope_id, sender, payment_type from knocks where status = 'pending' and ${LIVE_ENVELOPE_SQL} and payment_type <> 'introduction' order by seq asc limit 1`).get(); // @rule mailbox.t44-evict-non-intro-first
|
|
616
|
+
if (!victim) victim = this.db.prepare(`select seq, envelope_id, sender, payment_type from knocks where status = 'pending' and ${LIVE_ENVELOPE_SQL} order by seq asc limit 1`).get(); // @rule mailbox.t44-evict-cap-50
|
|
617
|
+
if (!victim) break;
|
|
618
|
+
this.db.prepare("update knocks set status = 'evicted', evicted_at = ? where seq = ?").run(nowIso, victim.seq);
|
|
619
|
+
evicted.push(victim);
|
|
620
|
+
pending--;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
const r = this.db.prepare(`
|
|
624
|
+
insert into knocks (envelope_id, sender, sender_lineage, recipient_lineage, payment_type, pow_stamp, received_at, status, envelope_json)
|
|
625
|
+
values (?, ?, ?, ?, ?, ?, ?, 'pending', ?)
|
|
626
|
+
`).run(envelope.id, sender, senderLineage, recipientLineage, paymentType, normalizedStamp, nowIso, JSON.stringify(envelope));
|
|
627
|
+
return { admitted: true, duplicate: false, rateLimited: false, stampReplayed: false, seq: Number(r.lastInsertRowid), evicted };
|
|
628
|
+
}).immediate();
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** Pending (or filtered) knocks in the knocks lane, oldest first. */
|
|
632
|
+
inboxKnocks(limit = KNOCK_LANE_CAP, { status = "pending" } = {}) {
|
|
633
|
+
if (this._jsonDir) return this._jsonDir.inboxKnocks(limit, { status });
|
|
634
|
+
if (status === "all") {
|
|
635
|
+
return this.db.prepare(`select * from knocks where ${LIVE_ENVELOPE_SQL} order by seq asc limit ?`).all(limit);
|
|
636
|
+
}
|
|
637
|
+
return this.db.prepare(`select * from knocks where status = ? and ${LIVE_ENVELOPE_SQL} order by seq asc limit ?`).all(status, limit);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** Look up one knock row by envelope_id (and optional sender). */
|
|
641
|
+
getKnock(envelopeId, sender) {
|
|
642
|
+
if (this._jsonDir) return this._jsonDir.getKnock(envelopeId, sender);
|
|
643
|
+
if (sender === undefined) {
|
|
644
|
+
const rows = this.db.prepare(`select * from knocks where envelope_id = ? and ${LIVE_ENVELOPE_SQL} order by seq asc limit 2`).all(envelopeId);
|
|
645
|
+
if (rows.length > 1) throw new Error(`getKnock: multiple senders hold knock ${envelopeId} (ambiguous); name the sender`); // @rule mailbox.knock-get-ambiguous
|
|
646
|
+
return rows[0] ?? null;
|
|
647
|
+
}
|
|
648
|
+
return this.db.prepare(`select * from knocks where envelope_id = ? and sender = ? and ${LIVE_ENVELOPE_SQL}`).get(envelopeId, sender) ?? null;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/** Count knocks by status (default: pending). */
|
|
652
|
+
knockCount(status = "pending") {
|
|
653
|
+
if (this._jsonDir) return this._jsonDir.knockCount(status);
|
|
654
|
+
return this.db.prepare(`select count(*) as n from knocks where status = ? and ${LIVE_ENVELOPE_SQL}`).get(status).n;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/** Mark a knock answered so K6 permits a future knock from that lineage pair. */
|
|
658
|
+
markKnockAnswered(envelopeId, { sender, decision = null, answerEnvelopeId = null, now = new Date().toISOString() } = {}) {
|
|
659
|
+
if (this._jsonDir) return this._jsonDir.markKnockAnswered(envelopeId, { sender, decision, answerEnvelopeId, now });
|
|
660
|
+
const nowIso = toCanonicalIso(now);
|
|
661
|
+
return this.db.transaction(() => {
|
|
662
|
+
if (sender === undefined) {
|
|
663
|
+
const n = this.db.prepare(`select count(*) as n from knocks where envelope_id = ? and ${LIVE_ENVELOPE_SQL}`).get(envelopeId).n;
|
|
664
|
+
if (n > 1) throw new Error(`markKnockAnswered: ${n} senders hold knock ${envelopeId} (ambiguous); name the sender`); // @rule mailbox.knock-answer-ambiguous
|
|
665
|
+
const r = this.db.prepare(`update knocks set status = 'answered', answered_at = ?, answer_decision = ?, answer_envelope_id = coalesce(?, answer_envelope_id) where envelope_id = ? and ${LIVE_ENVELOPE_SQL}`)
|
|
666
|
+
.run(nowIso, decision, answerEnvelopeId, envelopeId);
|
|
667
|
+
return r.changes > 0;
|
|
668
|
+
}
|
|
669
|
+
const r = this.db.prepare(`update knocks set status = 'answered', answered_at = ?, answer_decision = ?, answer_envelope_id = coalesce(?, answer_envelope_id) where envelope_id = ? and sender = ? and ${LIVE_ENVELOPE_SQL}`)
|
|
670
|
+
.run(nowIso, decision, answerEnvelopeId, envelopeId, sender);
|
|
671
|
+
return r.changes > 0;
|
|
672
|
+
}).immediate();
|
|
673
|
+
}
|
|
674
|
+
|
|
421
675
|
/**
|
|
422
676
|
* Without `sender`, every row under the id is quarantined (the safe side).
|
|
423
677
|
* An erased row is left as it is (C4): its quarantine state is evidence.
|
|
424
678
|
*/
|
|
425
679
|
markQuarantined(envelopeId, reason, sender) {
|
|
680
|
+
if (this._jsonDir) return this._jsonDir.markQuarantined(envelopeId, reason, sender);
|
|
426
681
|
const r = String(reason).slice(0, 100);
|
|
427
682
|
if (sender === undefined) {
|
|
428
683
|
this.db.prepare(`update inbox set quarantined = ? where envelope_id = ? and ${LIVE_ENVELOPE_SQL}`).run(r, envelopeId);
|
|
@@ -440,6 +695,7 @@ export class Mailbox {
|
|
|
440
695
|
|
|
441
696
|
/** Queue sends ({ to, key?, op, mvVersion, want?, manifestHash?, conflictsWith? }). Returns the ids queued. */
|
|
442
697
|
enqueueGroupV2Sends(groupId, sends, { now = new Date().toISOString(), newId }) {
|
|
698
|
+
if (this._jsonDir) return this._jsonDir.enqueueGroupV2Sends(groupId, sends, { now, newId });
|
|
443
699
|
const ids = [];
|
|
444
700
|
this.db.transaction(() => {
|
|
445
701
|
let queued = this.db.prepare("select count(*) as n from group_v2_sendq where state = 'queued'").get().n;
|
|
@@ -459,6 +715,7 @@ export class Mailbox {
|
|
|
459
715
|
|
|
460
716
|
/** Queued rows, oldest first; one group or all. */
|
|
461
717
|
queuedGroupV2Sends(groupId = null) {
|
|
718
|
+
if (this._jsonDir) return this._jsonDir.queuedGroupV2Sends(groupId);
|
|
462
719
|
return groupId
|
|
463
720
|
? this.db.prepare("select * from group_v2_sendq where state = 'queued' and group_id = ? order by queued_at, rowid").all(groupId)
|
|
464
721
|
: this.db.prepare("select * from group_v2_sendq where state = 'queued' order by queued_at, rowid").all();
|
|
@@ -466,6 +723,7 @@ export class Mailbox {
|
|
|
466
723
|
|
|
467
724
|
/** state: sent | dropped (final), or queued with a reason (a failed attempt). */
|
|
468
725
|
markGroupV2Send(id, state, { envelopeId = null, reason = null } = {}) {
|
|
726
|
+
if (this._jsonDir) return this._jsonDir.markGroupV2Send(id, state, { envelopeId, reason });
|
|
469
727
|
this.db.prepare("update group_v2_sendq set state = ?, state_at = ?, envelope_id = coalesce(?, envelope_id), reason = ? where id = ?")
|
|
470
728
|
.run(state, new Date().toISOString(), envelopeId, reason ? String(reason).slice(0, 200) : null, id);
|
|
471
729
|
}
|
|
@@ -492,6 +750,7 @@ export class Mailbox {
|
|
|
492
750
|
* Returns the number of rows changed (0 = unknown id or not quarantined).
|
|
493
751
|
*/
|
|
494
752
|
releaseQuarantined(envelopeId, sender) {
|
|
753
|
+
if (this._jsonDir) return this._jsonDir.releaseQuarantined(envelopeId, sender);
|
|
495
754
|
let changes = 0;
|
|
496
755
|
this.db.transaction(() => {
|
|
497
756
|
if (sender === undefined) {
|
|
@@ -521,6 +780,7 @@ export class Mailbox {
|
|
|
521
780
|
|
|
522
781
|
/** Without `sender`, every row under the id is marked read. */
|
|
523
782
|
markInboxRead(envelopeId, sender) {
|
|
783
|
+
if (this._jsonDir) return this._jsonDir.markInboxRead(envelopeId, sender);
|
|
524
784
|
const at = new Date().toISOString();
|
|
525
785
|
if (sender === undefined) {
|
|
526
786
|
this.db.prepare(`update inbox set read_at = ? where envelope_id = ? and read_at is null and ${LIVE_ENVELOPE_SQL}`).run(at, envelopeId);
|
|
@@ -535,6 +795,7 @@ export class Mailbox {
|
|
|
535
795
|
* another selector's reach after the user's erase.
|
|
536
796
|
*/
|
|
537
797
|
setInboundGroup(envelopeId, groupId, sender) {
|
|
798
|
+
if (this._jsonDir) return this._jsonDir.setInboundGroup(envelopeId, groupId, sender);
|
|
538
799
|
if (sender === undefined) {
|
|
539
800
|
this.db.prepare(`update inbox set group_id = ? where envelope_id = ? and ${LIVE_ENVELOPE_SQL}`).run(groupId, envelopeId);
|
|
540
801
|
} else {
|
|
@@ -548,12 +809,14 @@ export class Mailbox {
|
|
|
548
809
|
* and every explicit-seq write bumps). A new seq or released_seq is above it.
|
|
549
810
|
*/
|
|
550
811
|
_seqFloor() {
|
|
812
|
+
if (this._jsonDir) return this._jsonDir.inboxTipSeq();
|
|
551
813
|
return this.db.prepare(`select max(
|
|
552
814
|
coalesce((select max(coalesce(released_seq, seq)) from inbox), 0),
|
|
553
815
|
coalesce((select seq from sqlite_sequence where name = 'inbox'), 0)) as n`).get().n;
|
|
554
816
|
}
|
|
555
817
|
|
|
556
818
|
inboxTipSeq() {
|
|
819
|
+
if (this._jsonDir) return this._jsonDir.inboxTipSeq();
|
|
557
820
|
return this.db.prepare("select coalesce(max(coalesce(released_seq, seq)), 0) as n from inbox").get().n;
|
|
558
821
|
}
|
|
559
822
|
|
|
@@ -563,9 +826,10 @@ export class Mailbox {
|
|
|
563
826
|
* original seq, so no cursor that already passed it skips it.
|
|
564
827
|
*/
|
|
565
828
|
inboxAfter(seq = 0, limit = 500) {
|
|
829
|
+
if (this._jsonDir) return this._jsonDir.inboxAfter(seq, limit);
|
|
566
830
|
return this.db.prepare(`
|
|
567
831
|
select * from inbox
|
|
568
|
-
where coalesce(released_seq, seq) > ? and
|
|
832
|
+
where coalesce(released_seq, seq) > ? and ${this._regularInboxWhere()}
|
|
569
833
|
order by coalesce(released_seq, seq) asc limit ?
|
|
570
834
|
`).all(seq, limit);
|
|
571
835
|
}
|
|
@@ -577,15 +841,18 @@ export class Mailbox {
|
|
|
577
841
|
// still sees the row after its own cursor.
|
|
578
842
|
|
|
579
843
|
getReader(name) {
|
|
844
|
+
if (this._jsonDir) return this._jsonDir.getReader(name);
|
|
580
845
|
return this.db.prepare("select * from readers where name = ?").get(name) ?? null;
|
|
581
846
|
}
|
|
582
847
|
|
|
583
848
|
listReaders() {
|
|
849
|
+
if (this._jsonDir) return this._jsonDir.listReaders();
|
|
584
850
|
return this.db.prepare("select * from readers order by name").all();
|
|
585
851
|
}
|
|
586
852
|
|
|
587
853
|
/** Create or refresh a reader's last-run details; never moves its cursor. */
|
|
588
854
|
touchReader(name, { pid = null, host = null, cwd = null, command = null, cursor = 0 } = {}) {
|
|
855
|
+
if (this._jsonDir) return this._jsonDir.touchReader(name, { pid, host, cwd, command, cursor });
|
|
589
856
|
this.db.prepare(`
|
|
590
857
|
insert into readers (name, cursor, pid, host, cwd, command, last_seen)
|
|
591
858
|
values (?, ?, ?, ?, ?, ?, ?)
|
|
@@ -595,11 +862,13 @@ export class Mailbox {
|
|
|
595
862
|
}
|
|
596
863
|
|
|
597
864
|
setReaderCursor(name, seq) {
|
|
865
|
+
if (this._jsonDir) return this._jsonDir.setReaderCursor(name, seq);
|
|
598
866
|
this.db.prepare("update readers set cursor = max(cursor, ?) where name = ?").run(seq, name);
|
|
599
867
|
}
|
|
600
868
|
|
|
601
869
|
/** A long-running consumer (pillar recv) holds a lease; renew it on a heartbeat. */
|
|
602
870
|
holdLease(reader, { host, pid, cwd = null, command = null, ttlMs = 90_000 }) {
|
|
871
|
+
if (this._jsonDir) return this._jsonDir.holdLease(reader, { host, pid, cwd, command, ttlMs });
|
|
603
872
|
const now = new Date();
|
|
604
873
|
this.db.prepare(`
|
|
605
874
|
insert into reader_leases (reader, host, pid, cwd, command, started_at, lease_until)
|
|
@@ -609,11 +878,13 @@ export class Mailbox {
|
|
|
609
878
|
}
|
|
610
879
|
|
|
611
880
|
releaseLease(reader, { host, pid }) {
|
|
881
|
+
if (this._jsonDir) return this._jsonDir.releaseLease(reader, { host, pid });
|
|
612
882
|
this.db.prepare("delete from reader_leases where reader = ? and host = ? and pid = ?").run(reader, host, pid);
|
|
613
883
|
}
|
|
614
884
|
|
|
615
885
|
/** Leases not yet expired. The caller decides which are really alive (pid check). */
|
|
616
886
|
unexpiredLeases() {
|
|
887
|
+
if (this._jsonDir) return this._jsonDir.unexpiredLeases();
|
|
617
888
|
this.db.prepare("delete from reader_leases where lease_until < ?").run(new Date(Date.now() - 3600_000).toISOString());
|
|
618
889
|
return this.db.prepare("select * from reader_leases where lease_until >= ? order by started_at").all(new Date().toISOString());
|
|
619
890
|
}
|
|
@@ -622,6 +893,7 @@ export class Mailbox {
|
|
|
622
893
|
|
|
623
894
|
/** Record a recipient-signed ack. Only for a row actually sent TO `from`. */
|
|
624
895
|
markOutboundAcked(envelopeId, { from, ackId = null, at = null }) {
|
|
896
|
+
if (this._jsonDir) return this._jsonDir.markOutboundAcked(envelopeId, { from, ackId, at });
|
|
625
897
|
const r = this.db.prepare(`
|
|
626
898
|
update outbox set recipient_ack_at = coalesce(recipient_ack_at, ?), recipient_ack_id = coalesce(recipient_ack_id, ?)
|
|
627
899
|
where envelope_id = ? and recipient = ? and ${LIVE_ENVELOPE_SQL}
|
|
@@ -629,8 +901,25 @@ export class Mailbox {
|
|
|
629
901
|
return r.changes > 0;
|
|
630
902
|
}
|
|
631
903
|
|
|
904
|
+
/**
|
|
905
|
+
* Like markOutboundAcked, but true only for the FIRST committed transition:
|
|
906
|
+
* the row (ours, sent to `from`, live) had no ack and now has one. A
|
|
907
|
+
* repeated ack, an ack for a row already marked, or the wrong sender is
|
|
908
|
+
* false. Notify's ack opt-in counts only this (markOutboundAcked is true
|
|
909
|
+
* for a row that was already acked).
|
|
910
|
+
*/
|
|
911
|
+
markOutboundAckedFirst(envelopeId, { from, ackId = null, at = null }) {
|
|
912
|
+
if (this._jsonDir) return this._jsonDir.markOutboundAckedFirst(envelopeId, { from, ackId, at });
|
|
913
|
+
const r = this.db.prepare(`
|
|
914
|
+
update outbox set recipient_ack_at = ?, recipient_ack_id = coalesce(recipient_ack_id, ?)
|
|
915
|
+
where envelope_id = ? and recipient = ? and recipient_ack_at is null and ${LIVE_ENVELOPE_SQL}
|
|
916
|
+
`).run(at ?? new Date().toISOString(), ackId, envelopeId, from);
|
|
917
|
+
return r.changes > 0;
|
|
918
|
+
}
|
|
919
|
+
|
|
632
920
|
/** Mark rows read (read implies acked: a missing ack is backfilled). */
|
|
633
921
|
markOutboundRead(envelopeIds, { at = null } = {}) {
|
|
922
|
+
if (this._jsonDir) return this._jsonDir.markOutboundRead(envelopeIds, { at });
|
|
634
923
|
const ts = at ?? new Date().toISOString();
|
|
635
924
|
const stmt = this.db.prepare(`
|
|
636
925
|
update outbox set recipient_read_at = coalesce(recipient_read_at, ?), recipient_ack_at = coalesce(recipient_ack_at, ?)
|
|
@@ -643,10 +932,12 @@ export class Mailbox {
|
|
|
643
932
|
|
|
644
933
|
/** Outbound rows to one recipient, oldest first (receipt matching). */
|
|
645
934
|
outboundTo(recipient) {
|
|
935
|
+
if (this._jsonDir) return this._jsonDir.outboundTo(recipient);
|
|
646
936
|
return this.db.prepare(`select * from outbox where recipient = ? and ${LIVE_ENVELOPE_SQL} order by seq asc`).all(recipient);
|
|
647
937
|
}
|
|
648
938
|
|
|
649
939
|
outboxRecent(limit = 20) {
|
|
940
|
+
if (this._jsonDir) return this._jsonDir.outboxRecent(limit);
|
|
650
941
|
return this.db.prepare(`select * from outbox where ${LIVE_ENVELOPE_SQL} order by seq desc limit ?`).all(limit);
|
|
651
942
|
}
|
|
652
943
|
|
|
@@ -668,28 +959,34 @@ export class Mailbox {
|
|
|
668
959
|
}
|
|
669
960
|
|
|
670
961
|
getGroup(id) {
|
|
962
|
+
if (this._jsonDir) return this._jsonDir.getGroup(id);
|
|
671
963
|
return this._groupRow(this.db.prepare("select * from groups where id = ?").get(id));
|
|
672
964
|
}
|
|
673
965
|
|
|
674
966
|
listGroups() {
|
|
967
|
+
if (this._jsonDir) return this._jsonDir.listGroups();
|
|
675
968
|
return this.db.prepare("select * from groups order by updated_at desc").all().map(r => this._groupRow(r));
|
|
676
969
|
}
|
|
677
970
|
|
|
678
971
|
/** Mark a groups row as written by the pillar-group/2 sync (see _migrate). The row must exist. */
|
|
679
972
|
markGroupV2(id, at = new Date().toISOString()) {
|
|
973
|
+
if (this._jsonDir) return this._jsonDir.markGroupV2(id, at);
|
|
680
974
|
this.db.prepare("update groups set v2_synced_at = ? where id = ?").run(at, id);
|
|
681
975
|
}
|
|
682
976
|
|
|
683
977
|
isGroupV2Marked(id) {
|
|
978
|
+
if (this._jsonDir) return this._jsonDir.isGroupV2Marked(id);
|
|
684
979
|
return !!this.db.prepare("select v2_synced_at from groups where id = ?").get(id)?.v2_synced_at;
|
|
685
980
|
}
|
|
686
981
|
|
|
687
982
|
/** Ids of every row the v2 sync has marked. */
|
|
688
983
|
groupV2MarkedIds() {
|
|
984
|
+
if (this._jsonDir) return this._jsonDir.groupV2MarkedIds();
|
|
689
985
|
return this.db.prepare("select id from groups where v2_synced_at is not null order by id").all().map(r => r.id);
|
|
690
986
|
}
|
|
691
987
|
|
|
692
988
|
putGroup(g) {
|
|
989
|
+
if (this._jsonDir) return this._jsonDir.putGroup(g);
|
|
693
990
|
const now = new Date().toISOString();
|
|
694
991
|
this.db.prepare(`
|
|
695
992
|
insert into groups (id, name, members, keys, created_by, created_at, updated_at, pending, left_at)
|
|
@@ -702,10 +999,12 @@ export class Mailbox {
|
|
|
702
999
|
}
|
|
703
1000
|
|
|
704
1001
|
groupInbound(groupId, limit = 50) {
|
|
1002
|
+
if (this._jsonDir) return this._jsonDir.groupInbound(groupId, limit);
|
|
705
1003
|
return this.db.prepare(`select * from inbox where group_id = ? and quarantined is null and ${LIVE_ENVELOPE_SQL} order by seq desc limit ?`).all(groupId, limit).reverse();
|
|
706
1004
|
}
|
|
707
1005
|
|
|
708
1006
|
groupOutbound(groupId, limit = 200) {
|
|
1007
|
+
if (this._jsonDir) return this._jsonDir.groupOutbound(groupId, limit);
|
|
709
1008
|
return this.db.prepare(`select * from outbox where group_id = ? and ${LIVE_ENVELOPE_SQL} order by seq desc limit ?`).all(groupId, limit).reverse();
|
|
710
1009
|
}
|
|
711
1010
|
|
|
@@ -714,6 +1013,7 @@ export class Mailbox {
|
|
|
714
1013
|
* Returns { ok, badSeq? }. Used by adversarial tests + operator-driven audits.
|
|
715
1014
|
*/
|
|
716
1015
|
auditOutbox() {
|
|
1016
|
+
if (this._jsonDir) return this._jsonDir.auditOutbox();
|
|
717
1017
|
const rows = this.db.prepare("select seq, envelope_sha256, chain_hash from outbox order by seq asc").all();
|
|
718
1018
|
let prev = null;
|
|
719
1019
|
for (const r of rows) {
|
|
@@ -725,6 +1025,7 @@ export class Mailbox {
|
|
|
725
1025
|
return { ok: true };
|
|
726
1026
|
}
|
|
727
1027
|
auditInbox() {
|
|
1028
|
+
if (this._jsonDir) return this._jsonDir.auditInbox();
|
|
728
1029
|
const rows = this.db.prepare("select seq, envelope_sha256, chain_hash from inbox order by seq asc").all();
|
|
729
1030
|
let prev = null;
|
|
730
1031
|
for (const r of rows) {
|