@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.
Files changed (61) hide show
  1. package/README.md +325 -7
  2. package/bin/carrier.mjs +21 -0
  3. package/bin/pillar-ask.mjs +4 -1
  4. package/bin/pillar-blob.mjs +6 -0
  5. package/bin/pillar-directory.mjs +282 -16
  6. package/bin/pillar-escrow.mjs +4 -1
  7. package/bin/pillar-group-v2.mjs +6 -0
  8. package/bin/pillar-notify.mjs +13 -0
  9. package/bin/pillar-responder.mjs +266 -23
  10. package/bin/pillar.mjs +512 -257
  11. package/package.json +17 -25
  12. package/src/cli-help.mjs +23 -0
  13. package/src/group-v2-cli.mjs +1 -1
  14. package/src/identity/trust.mjs +72 -6
  15. package/src/mailbox/mailbox.mjs +313 -12
  16. package/src/net/carrier-server.mjs +34 -2
  17. package/src/net/refusals.mjs +2 -0
  18. package/src/net/seed-registry.mjs +2 -1
  19. package/src/net/transport.mjs +320 -91
  20. package/src/node-check.mjs +29 -8
  21. package/src/notify-events.mjs +148 -0
  22. package/src/notify-log.mjs +124 -0
  23. package/src/notify-runner.mjs +372 -0
  24. package/src/notify-store.mjs +736 -0
  25. package/src/notify.mjs +350 -0
  26. package/src/pillar.mjs +382 -27
  27. package/src/readers.mjs +2 -2
  28. package/src/registry/federation.mjs +3056 -0
  29. package/src/registry/gossip.mjs +294 -0
  30. package/src/registry/heartbeats.mjs +205 -0
  31. package/src/registry/lineage-verifier.mjs +972 -0
  32. package/src/registry/lineage.mjs +984 -0
  33. package/src/registry/listings.mjs +612 -0
  34. package/src/registry/resolve.mjs +376 -0
  35. package/src/registry/stats.mjs +508 -0
  36. package/src/sqlite-loader.mjs +40 -0
  37. package/src/vendor/pillar-core.mjs +22 -0
  38. package/src/vendor/uuaid-soul.mjs +2246 -0
  39. package/src/vnext/a6b-common.mjs +270 -0
  40. package/src/vnext/b1-cli.mjs +970 -0
  41. package/src/vnext/card-cli.mjs +370 -0
  42. package/src/vnext/greeter-operator.mjs +160 -0
  43. package/src/vnext/greeter-suggest.mjs +318 -0
  44. package/src/vnext/greeter.mjs +571 -0
  45. package/src/vnext/homes.mjs +48 -0
  46. package/src/vnext/init-cli.mjs +489 -0
  47. package/src/vnext/intro-cli.mjs +81 -0
  48. package/src/vnext/json-dir-mailbox.mjs +304 -0
  49. package/src/vnext/jsonl-mailbox.mjs +1132 -0
  50. package/src/vnext/knock-answer.mjs +162 -0
  51. package/src/vnext/knock-cli.mjs +520 -0
  52. package/src/vnext/knock-lane.mjs +312 -0
  53. package/src/vnext/knock-responder.mjs +344 -0
  54. package/src/vnext/lineage-cli.mjs +653 -0
  55. package/src/vnext/listing-cli.mjs +323 -0
  56. package/src/vnext/resolve-cli.mjs +104 -0
  57. package/src/vnext/soul-cli.mjs +666 -0
  58. package/src/vnext/verify-cli.mjs +333 -0
  59. package/src/vnext/w4-redirect.mjs +178 -0
  60. package/src/vnext/wave-a.mjs +193 -0
  61. package/src/vnext/witness-cli.mjs +547 -0
@@ -0,0 +1,304 @@
1
+ /**
2
+ * Read-only JSON-dir mailbox reader for `@uuaid/pillar` 2.7.0 (carried from A2 check 5).
3
+ *
4
+ * Reads a JSON-dir home's append-only JSONL mailbox (`mailbox/inbox.jsonl`,
5
+ * `mailbox/outbox.jsonl`, `mailbox/cursors.json`) created by `@uuaid/pillar-client`
6
+ * `open({ store: createJsonDirStore(homeDir) })` with ZERO migration and without
7
+ * creating any SQLite database file.
8
+ */
9
+ import { createHash } from "node:crypto";
10
+ import { existsSync, readFileSync, statSync } from "node:fs";
11
+ import { basename, dirname, join } from "node:path";
12
+ import { jcs } from "../identity/jcs.mjs";
13
+ import { KNOCK_LANE_CAP, filterKnockInboxRows, filterRegularInboxRows } from "./knock-lane.mjs";
14
+
15
+ const CHAIN_SEED = "uuaid-pillar-mailbox-v1|";
16
+ const EMPTY_TIP = "0".repeat(64);
17
+ const ERASED_ENVELOPE_JSON = '{"erased":true,"v":1}';
18
+
19
+ function isDir(p) {
20
+ try { return statSync(p).isDirectory(); } catch { return false; }
21
+ }
22
+
23
+ function hasJsonDirMailboxFiles(dir) {
24
+ return (
25
+ existsSync(join(dir, "inbox.jsonl")) ||
26
+ existsSync(join(dir, "outbox.jsonl")) ||
27
+ existsSync(join(dir, "cursors.json"))
28
+ );
29
+ }
30
+
31
+ /**
32
+ * True iff `home` holds a `@uuaid/pillar-client` identity (what `open()`
33
+ * writes). A pillar JSONL store home has none: its key is keychain.json, or
34
+ * kc.json for pillar-escrow (2.7.1 A6).
35
+ */
36
+ export function hasClientIdentity(home) {
37
+ return (
38
+ existsSync(join(home, "identity", "agent.seed")) ||
39
+ existsSync(join(home, "identity", "agent-meta.json")) ||
40
+ existsSync(join(home, "card", "card.json"))
41
+ );
42
+ }
43
+
44
+ /**
45
+ * True iff `home` is an `@uuaid/pillar-client` `open()` JSON-dir home
46
+ * (never a SQLite `pillar init` home carrying `mailbox.db` or `keychain.json`).
47
+ */
48
+ export function isJsonDirHome(home) {
49
+ return (
50
+ !existsSync(join(home, "mailbox.db")) &&
51
+ !existsSync(join(home, "keychain.json")) &&
52
+ (hasJsonDirMailboxFiles(join(home, "mailbox")) || hasClientIdentity(home))
53
+ );
54
+ }
55
+
56
+ /**
57
+ * Resolve `targetPath` to a JSON-dir mailbox directory if it points to:
58
+ * 1. a `mailbox/` directory holding JSONL mailbox files or inside a JSON-dir home,
59
+ * 2. a JSON-dir home directory (`isJsonDirHome(targetPath)`), or
60
+ * 3. a non-existent `<home>/mailbox` or `<home>/mailbox.db` inside a JSON-dir home.
61
+ *
62
+ * Returns the resolved `mailbox/` directory path, or `null` for standard SQLite mailboxes.
63
+ */
64
+ export function resolveJsonDirMailbox(targetPath) {
65
+ if (typeof targetPath !== "string" || !targetPath) return null;
66
+
67
+ if (isDir(targetPath)) {
68
+ if (hasJsonDirMailboxFiles(targetPath) || (basename(targetPath) === "mailbox" && isJsonDirHome(dirname(targetPath)))) return targetPath; // @rule mailbox-jsondir.detect-direct-dir
69
+ if (isJsonDirHome(targetPath)) return join(targetPath, "mailbox"); // @rule mailbox-jsondir.detect-home-dir
70
+ return null;
71
+ }
72
+
73
+ if (!existsSync(targetPath) && basename(targetPath) === "mailbox" && isJsonDirHome(dirname(targetPath))) return targetPath; // @rule mailbox-jsondir.detect-empty-mailbox-dir
74
+ if (!existsSync(targetPath) && basename(targetPath) === "mailbox.db" && isJsonDirHome(dirname(targetPath))) return join(dirname(targetPath), "mailbox"); // @rule mailbox-jsondir.detect-sibling-db-path
75
+
76
+ return null;
77
+ }
78
+
79
+ function envelopeSha(envelope) {
80
+ return createHash("sha256").update(jcs(envelope)).digest("hex");
81
+ }
82
+
83
+ function isErased(row) {
84
+ return !!row && row.envelope_json === ERASED_ENVELOPE_JSON;
85
+ }
86
+
87
+ function readJsonlRaw(filePath) {
88
+ if (!existsSync(filePath)) return [];
89
+ const text = readFileSync(filePath, "utf8");
90
+ const lines = [];
91
+ let lineNo = 0;
92
+ for (const raw of text.split("\n")) {
93
+ lineNo++;
94
+ const trimmed = raw.trim();
95
+ if (!trimmed) continue;
96
+ lines.push({ lineNo, raw: trimmed });
97
+ }
98
+ return lines;
99
+ }
100
+
101
+ function parseRows(filePath) {
102
+ const out = [];
103
+ for (const { raw } of readJsonlRaw(filePath)) {
104
+ out.push(JSON.parse(raw));
105
+ }
106
+ return out;
107
+ }
108
+
109
+ function auditLog(filePath) {
110
+ let prev = null;
111
+ let prevSeq = 0;
112
+ for (const { lineNo, raw } of readJsonlRaw(filePath)) {
113
+ let row = null;
114
+ try { row = JSON.parse(raw); } catch { row = null; }
115
+ if (!row || typeof row !== "object") return { ok: false, badSeq: lineNo }; // @rule mailbox-jsondir.audit-parse
116
+
117
+ if (!Number.isSafeInteger(row.seq) || row.seq <= prevSeq) return { ok: false, badSeq: lineNo }; // @rule mailbox-jsondir.audit-seq-order
118
+ prevSeq = row.seq;
119
+ const seq = row.seq;
120
+ let parsedEnv = null;
121
+ try { parsedEnv = JSON.parse(row.envelope_json); } catch { parsedEnv = null; }
122
+ if (!parsedEnv || typeof parsedEnv !== "object" || envelopeSha(parsedEnv) !== row.envelope_sha256) return { ok: false, badSeq: seq }; // @rule mailbox-jsondir.audit-env-sha
123
+
124
+ const chainInput = prev === null ? CHAIN_SEED + row.envelope_sha256 : prev + row.envelope_sha256;
125
+ const expected = createHash("sha256").update(chainInput).digest("hex");
126
+ if (expected !== row.chain_hash) return { ok: false, badSeq: seq }; // @rule mailbox-jsondir.audit-chain-hash
127
+ prev = row.chain_hash;
128
+ }
129
+ return { ok: true };
130
+ }
131
+
132
+ export class JsonDirMailboxReader {
133
+ constructor(mailboxDir) {
134
+ this.dir = mailboxDir;
135
+ this.inboxPath = join(mailboxDir, "inbox.jsonl");
136
+ this.outboxPath = join(mailboxDir, "outbox.jsonl");
137
+ this.cursorsPath = join(mailboxDir, "cursors.json");
138
+ }
139
+
140
+ close() {}
141
+
142
+ _inboxRows() {
143
+ return parseRows(this.inboxPath).sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0));
144
+ }
145
+
146
+ _outboxRows() {
147
+ return parseRows(this.outboxPath).sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0));
148
+ }
149
+
150
+ getCarrierCursors() {
151
+ if (!existsSync(this.cursorsPath)) return {};
152
+ return JSON.parse(readFileSync(this.cursorsPath, "utf8"));
153
+ }
154
+
155
+ hasOutbound(envelopeId) {
156
+ return this._outboxRows().some((r) => r.envelope_id === envelopeId);
157
+ }
158
+
159
+ getOutbound(envelopeId) {
160
+ return this._outboxRows().find((r) => r.envelope_id === envelopeId) ?? null;
161
+ }
162
+
163
+ getInbound(envelopeId, sender) {
164
+ const rows = this._inboxRows();
165
+ if (sender === undefined) return rows.find((r) => r.envelope_id === envelopeId) ?? null;
166
+ return rows.find((r) => r.envelope_id === envelopeId && r.sender === sender) ?? null;
167
+ }
168
+
169
+ outboxTip() {
170
+ const rows = this._outboxRows();
171
+ return rows.length > 0 ? rows[rows.length - 1].chain_hash : EMPTY_TIP;
172
+ }
173
+
174
+ inboxTip() {
175
+ const rows = this._inboxRows();
176
+ return rows.length > 0 ? rows[rows.length - 1].chain_hash : EMPTY_TIP;
177
+ }
178
+
179
+ outboxSince(seq = 0, limit = 100) {
180
+ return this._outboxRows().filter((r) => r.seq > seq && !isErased(r)).slice(0, limit);
181
+ }
182
+
183
+ inboxSince(seq = 0, limit = 100) {
184
+ return this._inboxRows().filter((r) => r.seq > seq && !isErased(r)).slice(0, limit);
185
+ }
186
+
187
+ inboxUnread(limit = 100) {
188
+ return filterRegularInboxRows(
189
+ this._inboxRows().filter((r) => r.read_at == null && r.quarantined == null && !isErased(r))
190
+ ).slice(0, limit);
191
+ }
192
+
193
+ inboxKnocks(limit = KNOCK_LANE_CAP) {
194
+ return filterKnockInboxRows(
195
+ this._inboxRows().filter((r) => !isErased(r))
196
+ ).slice(-KNOCK_LANE_CAP).slice(0, limit);
197
+ }
198
+
199
+ getKnock(envelopeId, sender) {
200
+ const rows = filterKnockInboxRows(this._inboxRows().filter((r) => !isErased(r)));
201
+ if (sender === undefined) {
202
+ const matches = rows.filter((r) => r.envelope_id === envelopeId);
203
+ if (matches.length > 1) {
204
+ throw new Error(`getKnock: ${matches.length} senders hold knock ${envelopeId} (ambiguous); name the sender`);
205
+ }
206
+ return matches[0] ?? null;
207
+ }
208
+ return rows.find((r) => r.envelope_id === envelopeId && r.sender === sender) ?? null;
209
+ }
210
+
211
+ knockCount() {
212
+ return this.inboxKnocks(KNOCK_LANE_CAP).length;
213
+ }
214
+
215
+ inboxQuarantined(limit = 100) {
216
+ return this._inboxRows()
217
+ .filter((r) => r.quarantined != null && !isErased(r))
218
+ .slice(0, limit);
219
+ }
220
+
221
+ quarantinedSenders(envelopeId) {
222
+ return this._inboxRows()
223
+ .filter((r) => r.envelope_id === envelopeId && r.quarantined != null)
224
+ .map((r) => r.sender);
225
+ }
226
+
227
+ oldestUnreadSeq() {
228
+ return this.inboxUnread(1)[0]?.seq ?? null;
229
+ }
230
+
231
+ inboxTipSeq() {
232
+ const rows = this._inboxRows();
233
+ let max = 0;
234
+ for (const r of rows) {
235
+ const pos = r.released_seq ?? r.seq ?? 0;
236
+ if (pos > max) max = pos;
237
+ }
238
+ return max;
239
+ }
240
+
241
+ inboxAfter(seq = 0, limit = 500) {
242
+ return filterRegularInboxRows(
243
+ this._inboxRows().filter((r) => (r.released_seq ?? r.seq) > seq && r.quarantined == null && !isErased(r))
244
+ )
245
+ .sort((a, b) => (a.released_seq ?? a.seq) - (b.released_seq ?? b.seq))
246
+ .slice(0, limit);
247
+ }
248
+
249
+ outboundTo(recipient) {
250
+ return this._outboxRows().filter((r) => r.recipient === recipient && !isErased(r));
251
+ }
252
+
253
+ outboxRecent(limit = 20) {
254
+ return this._outboxRows().filter((r) => !isErased(r)).reverse().slice(0, limit);
255
+ }
256
+
257
+ auditOutbox() {
258
+ return auditLog(this.outboxPath);
259
+ }
260
+
261
+ auditInbox() {
262
+ return auditLog(this.inboxPath);
263
+ }
264
+
265
+ getReader() { return null; }
266
+ listReaders() { return []; }
267
+ unexpiredLeases() { return []; }
268
+ getGroup() { return null; }
269
+ listGroups() { return []; }
270
+ groupInbound() { return []; }
271
+ groupOutbound() { return []; }
272
+ hasUnansweredRecentKnock() { return false; }
273
+ hasReplayedPowStamp() { return false; }
274
+ queuedGroupV2Sends() { return []; }
275
+ isGroupV2Marked() { return false; }
276
+ groupV2MarkedIds() { return []; }
277
+
278
+ eraseRows() { return this._readOnly("eraseRows"); }
279
+ setCarrierCursor() { return this._readOnly("setCarrierCursor"); }
280
+ appendOutbound() { return this._readOnly("appendOutbound"); }
281
+ appendInbound() { return this._readOnly("appendInbound"); }
282
+ markOutboundDelivered() { return this._readOnly("markOutboundDelivered"); }
283
+ admitKnock() { return this._readOnly("admitKnock"); }
284
+ markKnockAnswered() { return this._readOnly("markKnockAnswered"); }
285
+ markQuarantined() { return this._readOnly("markQuarantined"); }
286
+ enqueueGroupV2Sends() { return this._readOnly("enqueueGroupV2Sends"); }
287
+ markGroupV2Send() { return this._readOnly("markGroupV2Send"); }
288
+ releaseQuarantined() { return this._readOnly("releaseQuarantined"); }
289
+ markInboxRead() { return this._readOnly("markInboxRead"); }
290
+ setInboundGroup() { return this._readOnly("setInboundGroup"); }
291
+ touchReader() { return this._readOnly("touchReader"); }
292
+ setReaderCursor() { return this._readOnly("setReaderCursor"); }
293
+ holdLease() { return this._readOnly("holdLease"); }
294
+ releaseLease() { return this._readOnly("releaseLease"); }
295
+ markOutboundAcked() { return this._readOnly("markOutboundAcked"); }
296
+ markOutboundAckedFirst() { return this._readOnly("markOutboundAckedFirst"); }
297
+ markOutboundRead() { return this._readOnly("markOutboundRead"); }
298
+ markGroupV2() { return this._readOnly("markGroupV2"); }
299
+ putGroup() { return this._readOnly("putGroup"); }
300
+
301
+ _readOnly(op) {
302
+ throw new Error(`Mailbox(${op}): JSON-dir mailbox at ${this.dir} is read-only`); // @rule mailbox-jsondir.read-only-write-refused
303
+ }
304
+ }