fedipod 1.40.0 → 1.42.0

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 (65) hide show
  1. package/README.md +5 -0
  2. package/architecture.md +1 -3
  3. package/architecture.svg +93 -85
  4. package/gateway.md +7 -2
  5. package/lib/client/c2s.mjs +16 -5
  6. package/lib/client/masto/oauth.mjs +1 -1
  7. package/lib/connections/vault.mjs +1 -1
  8. package/lib/core/agent.mjs +720 -0
  9. package/lib/core/gateway-move.mjs +58 -0
  10. package/lib/core/keys.mjs +1 -1
  11. package/lib/core/lease.mjs +4 -3
  12. package/lib/core/publisher/index.mjs +4 -6
  13. package/lib/core/publisher/notes.mjs +73 -4
  14. package/lib/core/remote.mjs +54 -0
  15. package/lib/core/shapes/shapes-text.mjs +11 -3
  16. package/lib/core/wire.mjs +4 -1
  17. package/lib/device/admin/index.mjs +4 -11
  18. package/lib/device/admin/routes/setup.mjs +60 -5
  19. package/lib/device/admin/server.mjs +18 -5
  20. package/lib/device/cli/commands/account.mjs +1 -1
  21. package/lib/device/cli/commands/run.mjs +2 -2
  22. package/lib/device/cli/commands/service.mjs +1 -1
  23. package/lib/device/cli/commands/setup.mjs +1 -1
  24. package/lib/device/cli/commands/state.mjs +1 -1
  25. package/lib/device/cli/context.mjs +1 -1
  26. package/lib/{gateway → device}/directory.mjs +1 -1
  27. package/lib/device/gateway-move.mjs +3 -58
  28. package/lib/device/home.mjs +3 -50
  29. package/lib/{client → device}/localapi.mjs +2 -2
  30. package/lib/device/remote.mjs +3 -54
  31. package/lib/gateway/copy.mjs +7 -1
  32. package/lib/gateway/front-core.mjs +6 -16
  33. package/lib/gateway/front-pages.mjs +31 -0
  34. package/lib/gateway/keeper-session.mjs +10 -1
  35. package/lib/gateway/masto-gateway.mjs +61 -12
  36. package/lib/gateway/outbox-door.mjs +31 -0
  37. package/lib/gateway/state-api.mjs +39 -7
  38. package/lib/server/embed.mjs +4 -4
  39. package/lib/shared/files.mjs +49 -0
  40. package/lib/shared/ua.mjs +3 -0
  41. package/lib/surface/index.mjs +7 -0
  42. package/lib/{device/admin → surface}/origins.mjs +1 -1
  43. package/lib/{device/admin → surface}/routes/gateway.mjs +7 -11
  44. package/lib/{device/admin → surface}/routes/lifecycle.mjs +3 -3
  45. package/lib/{device/admin → surface}/routes/owner.mjs +4 -60
  46. package/lib/{device/admin → surface}/routes/social.mjs +1 -1
  47. package/lib/{device/admin → surface}/static.mjs +6 -1
  48. package/lib/{device/admin → surface}/surface.mjs +19 -16
  49. package/package.json +23 -5
  50. package/run-agent.mjs +18 -714
  51. package/scripts/build-app.mjs +4 -0
  52. package/scripts/check-netlify-bundles.mjs +84 -0
  53. package/scripts/check-pod-calls.mjs +42 -0
  54. package/scripts/vendor-sanitize.mjs +27 -0
  55. package/vendor/idp-grant.cjs +11 -5
  56. package/vendor/sanitize-html.cjs +11456 -0
  57. package/web/app/agent.mjs +4 -2
  58. package/web/app/copy-mode.mjs +27 -3
  59. package/web/app/dist/boot.js.map +1 -1
  60. package/web/app/dist/sw.js +110 -24
  61. package/web/app/dist/sw.js.map +4 -4
  62. package/web/app-signin/app-signin.mjs +2 -2
  63. package/web/app-signin/index.html +13 -5
  64. package/browser.svg +0 -72
  65. /package/lib/{device/admin → surface}/routes/connections.mjs +0 -0
package/run-agent.mjs CHANGED
@@ -16,731 +16,28 @@
16
16
  // node run-agent.mjs # or: bin/fedipod.mjs run
17
17
  //
18
18
  // Env: AP_HOME (credential dir + local log, default ~/.fedipod, or
19
- // ~/.activitypod on an install that predates the rename — see lib/home.mjs),
19
+ // ~/.activitypod on an install that predates the rename — see lib/device/home.mjs),
20
20
  // AP_PORT (UI/API/admin, default 8030),
21
21
  // AP_GATE_TOKEN (optional loopback gate; absent → open, loopback-only).
22
22
  //
23
23
  // Until `bin/fedipod.mjs setup` has minted a credential the agent idles:
24
24
  // UI + admin up, no federation.
25
25
 
26
+
26
27
  import fs from 'node:fs';
27
- import os from 'node:os';
28
28
  import path from 'node:path';
29
29
  import { pathToFileURL } from 'node:url';
30
30
 
31
- import { PodStore } from './lib/core/store.mjs';
32
- import { apRoot, rootOf, writeJsonAtomic } from './lib/device/home.mjs';
31
+ import { Agent } from './lib/core/agent.mjs';
32
+ import { apRoot, rootOf } from './lib/device/home.mjs';
33
+ import { writeJsonAtomic } from './lib/shared/files.mjs';
33
34
  import { ensureTrustedTls } from './lib/device/certs.mjs';
34
- import { storageFor } from './lib/core/storage.mjs';
35
- import { resolveKeys } from './lib/core/keys.mjs';
36
- import { completeGatewayMove } from './lib/device/gateway-move.mjs';
37
- import { RemotePod } from './lib/device/remote.mjs';
38
- import { Deliverer } from './lib/core/deliver.mjs';
39
- import { Publisher } from './lib/core/publisher/index.mjs';
40
- import { Intake } from './lib/core/intake/index.mjs';
41
- import { C2S } from './lib/client/c2s.mjs';
42
- import { TagFeed } from './lib/connections/tagfeed.mjs';
43
- import { ImportWorker } from './lib/connections/import.mjs';
44
- import { Atproto } from './lib/connections/atproto.mjs';
45
- import { FediAccounts } from './lib/connections/fediacct.mjs';
46
- import { vaultsFor } from './lib/connections/vault.mjs';
47
- import { AcctFeed } from './lib/connections/acctfeed.mjs';
48
- import { BskyFeed } from './lib/connections/bskyfeed.mjs';
49
- import { BskyGroup } from './lib/connections/bskygroup.mjs';
50
- import { Lease } from './lib/core/lease.mjs';
51
- import { publishDue } from './lib/core/scheduled.mjs';
52
35
  import { startAdmin } from './lib/device/admin/index.mjs';
53
36
  import { exposureProblem, hostLabel } from './lib/shared/guard.mjs';
54
37
  import { pendingSteps } from './lib/device/migrate.mjs';
55
- import { apUrls, assertionKeyId , publicHandle } from './lib/core/wire.mjs';
56
- import { followActor, unfollowActor, resolveHandle } from './lib/core/social.mjs';
57
- import * as podInbox from './lib/pod/inbox.mjs';
58
-
59
- export class Agent {
60
- constructor({ home, log }) {
61
- this.home = home;
62
- this.log = log;
63
- this.logRing = [];
64
- this.store = new PodStore({ log });
65
- }
66
-
67
- readCredential() {
68
- try { return JSON.parse(fs.readFileSync(path.join(this.home, 'credential.json'), 'utf8')); }
69
- catch { return null; }
70
- }
71
-
72
- configured() { return !!this.remote && !!this.store.getConfig(); }
73
-
74
- // Where the private half lives. `privateRoot` in the credential file names a
75
- // container — by default a plain directory beside the credential, and a pod
76
- // on this machine if you move it there. Absent means on the pod, so an
77
- // existing install is untouched.
78
- privateUrls(cred, urls = this.urls) {
79
- if (!cred.privateRoot) {
80
- return { state: urls.state, archive: urls.home + 'inbox-archive/', elsewhere: false };
81
- }
82
- const base = cred.privateRoot.endsWith('/') ? cred.privateRoot : cred.privateRoot + '/';
83
- return { state: base + 'ap-state/', archive: base + 'inbox-archive/', elsewhere: true };
84
- }
85
-
86
- // A container to keep the private half in. On the pod it is reached with the
87
- // credential; a local pod plainly, with a token header when it is gated
88
- // (dk's is); a directory is not reached over anything at all.
89
- privateStorage(cred, which, urls = this.urls) {
90
- const url = this.privateUrls(cred, urls)[which];
91
- if (!cred.privateRoot) return storageFor(url, (u, i) => this.remote.fetch(u, i));
92
- const token = process.env.AP_STATE_TOKEN || '';
93
- return storageFor(url, (u, i) => fetch(u, token
94
- ? { ...i, headers: { ...(i?.headers || {}), 'x-dk-token': token } }
95
- : i));
96
- }
97
-
98
- logLines(n = 100) { return this.logRing.slice(-n); }
99
-
100
- status() {
101
- const cfg = this.store.getConfig();
102
- const contacts = this.store.getContacts();
103
- return {
104
- configured: this.configured(),
105
- mode: !this.configured() ? 'unconfigured' : this.viewer ? 'viewer' : 'active',
106
- kind: cfg?.kind || 'person',
107
- handle: cfg?.handle || null,
108
- actor: this.urls?.actor || null,
109
- followers: contacts.followers.length,
110
- following: contacts.following.length,
111
- queue: this.store.getQueue().length,
112
- deadLetters: this.store.getDeadLetters().length,
113
- blockedDomains: this.store.getBlocklist().domains.length,
114
- push: this.intake?.wsState || 'n/a',
115
- // Measured by the last sweep from the listing it already fetched, so
116
- // asking costs nothing. What the admin page prompts on.
117
- inbox: this.intake?.inboxStats || null,
118
- lastDrain: this.intake?.lastDrain || null,
119
- tagfeed: this.tagfeed
120
- ? { ...this.tagfeed.config(), lastSweep: this.tagfeed.lastSweep, lastAdded: this.tagfeed.lastAdded }
121
- : null,
122
- atproto: this.atproto?.status() || null,
123
- // Anyone asking whether this agent is hammering their server can read the
124
- // answer here instead of in their access log.
125
- podRequests: this.remote?.stats?.() || null,
126
- update: this.updateInfo || null,
127
- inboxCooldownFor: this.intake?.drainCooldownUntil
128
- ? Math.max(0, Math.round((this.intake.drainCooldownUntil - Date.now()) / 1000)) : 0,
129
- stateSkipped: this.store.lastSkipped || [],
130
- };
131
- }
132
-
133
- // First-run provisioning, called by the setup CLI after the credential file
134
- // is written: containers, owner-only ACLs on the private trees, config into
135
- // pod state. publishProfile (via connect) handles the public wire ACLs.
136
- async bootstrap({ handle, name, root, kind, approveJoins = false, summary, icon, gateway }) {
137
- const cred = this.readCredential();
138
- if (!cred) throw new Error('no credential — run setup first');
139
- if (!this.remote) {
140
- this.remote = new RemotePod(cred, { log: this.log, home: this.home });
141
- await this.remote.warmup();
142
- }
143
- this.urls = apUrls(cred.remotePod, root);
144
- const priv = this.privateUrls(cred);
145
- const stateStore = this.privateStorage(cred, 'state');
146
- // The pod's own ap-state/ is provisioned either way: even when the rest of
147
- // the private half lives elsewhere, lease.json stays here, because a lease
148
- // in a pod only one machine can reach coordinates nothing.
149
- await this.remote.putJson(this.urls.state + '.keep', { keep: true }, 'application/json');
150
- await this.remote.setAcl(this.urls.home, []);
151
- await this.remote.setAcl(this.urls.state, []);
152
- if (priv.elsewhere) {
153
- const r = await stateStore.write('.keep', '{"keep":true}\n', 'application/json');
154
- if (!r.ok) throw new Error(`private store ${stateStore.base}.keep → ${r.why}`);
155
- this.log(`private state lives at ${cred.privateRoot} (${stateStore.kind})`);
156
- }
157
- this.store.attach(stateStore);
158
- // Re-running setup must not destroy state set afterwards (the UI
159
- // password, above all), so load what is there and merge into it.
160
- await this.store.load().catch(() => {});
161
- const existing = this.store.getConfig() || {};
162
- this.store.setConfig({
163
- ...existing,
164
- remotePod: cred.remotePod, handle, name: name || existing.name || handle,
165
- issuer: cred.issuerOrigin, ...(root ? { root } : {}),
166
- ...(kind ? { kind } : {}),
167
- ...(approveJoins ? { approveJoins: true } : {}),
168
- ...(summary ? { summary } : {}), ...(icon ? { icon } : {}),
169
- // Set before the first publish, so the actor never advertises the pod
170
- // inbox only to rename it a moment later.
171
- ...(gateway ? { gateway } : {}),
172
- });
173
- await this.store.flush();
174
- }
175
-
176
- // Cache the handle in agent.json, merging so the port survives. Nothing
177
- // outside this process can read pod state, so this file is the only place a
178
- // sibling — or the next start, before it connects — can learn the name to
179
- // build this identity's origin from.
180
- recordHandle(handle) {
181
- if (!handle || !this.home) return;
182
- const file = path.join(this.home, 'agent.json');
183
- let rec = {};
184
- try { rec = JSON.parse(fs.readFileSync(file, 'utf8')) || {}; } catch { /* first run */ }
185
- if (rec.handle === handle) return;
186
- try { writeJsonAtomic(file, { ...rec, handle }, { mode: 0o644 }); }
187
- catch { /* the agent still runs; its links are just bare */ }
188
- }
189
-
190
- // Bring federation up from the credential file + pod state. `name` (from
191
- // `run --name "…"`) updates the display name other servers show, without
192
- // the collateral of re-running setup.
193
- // `act: false` reads the identity and stops. Everything a confirmation prompt
194
- // needs — the handle, the host, the follow counts — is in pod state that is
195
- // loaded by then, while acquiring the lease, draining the inbox, subscribing a
196
- // notification channel, probing the ACLs and sweeping the tag feed are all
197
- // things a command you are about to decline should never have spent. The
198
- // command calls connect() again, without the flag, if you say yes.
199
- async connect({ name = null, repair = true, act = true } = {}) {
200
- const cred = this.readCredential();
201
- if (!cred) return false;
202
- if (!this.remote) {
203
- this.remote = new RemotePod(cred, { log: this.log, home: this.home });
204
- await this.remote.warmup();
205
- }
206
- const probeUrls = apUrls(cred.remotePod, cred.root);
207
- // Load until it actually succeeds: attaching is not the same as having
208
- // read the state, and a retry that skipped the load would see an empty
209
- // cache and wrongly conclude the agent was never set up.
210
- if (!this.store.storage) this.store.attach(this.privateStorage(cred, 'state', probeUrls));
211
- if (!this.stateLoaded) {
212
- await this.store.load();
213
- this.stateLoaded = true;
214
- }
215
- let config = this.store.getConfig();
216
- if (!config) { this.log('credential present but pod state empty — run setup'); return false; }
217
- // The store had to be attached from the credential, because only the config
218
- // it holds says where the state really lives. If the two disagree,
219
- // everything past here reads one tree and writes another.
220
- // Only meaningful when the state follows the pod. With privateRoot set it
221
- // is pinned by configuration and a moved pod does not move it.
222
- if (!cred.privateRoot && apUrls(config.remotePod, config.root).state !== probeUrls.state) {
223
- const moved = apUrls(config.remotePod, config.root).state;
224
- this.log(`state tree moved (${probeUrls.state} → ${moved}) — reattaching`);
225
- this.store.attach(storageFor(moved, (u, i) => this.remote.fetch(u, i)));
226
- await this.store.load();
227
- config = this.store.getConfig();
228
- if (!config) { this.log('no state at the pod its own config names — run setup'); return false; }
229
- }
230
- // Resurrecting a tombstoned actor would contradict the Delete every server
231
- // has already acted on, so a retired identity stays retired.
232
- if (config.retiredAt) {
233
- this.log(`this actor was retired on ${config.retiredAt} — run setup for a new identity`);
234
- return false;
235
- }
236
- // A rename is a merge into the existing config, never a rewrite — the
237
- // UI password and anything else set later must survive it.
238
- if (name && name !== config.name) {
239
- this.store.setConfig({ ...config, name });
240
- config = this.store.getConfig();
241
- this.renamed = true; // republish the actor once connected
242
- this.log(`display name set to "${name}"`);
243
- }
244
- // A fronted identity (config.gateway.frontActor) advertises its ids on a
245
- // shared domain; RemotePod gets the map so writes still land on the pod.
246
- const publicBase = config.gateway?.frontActor
247
- ? config.gateway.frontActor.replace(/ap\/actor\/?$/, '') : null;
248
- this.urls = apUrls(config.remotePod, config.root, { publicBase });
249
- if (this.urls.toPod) this.remote.setUrlMap(this.urls.toPod);
250
- // The agent answers at <handle>.localhost too, so each identity on a
251
- // machine gets a browser origin of its own. Known only now: the admin
252
- // server was listening before any of this was read.
253
- this.authorities?.setHandle(config.handle);
254
- // And write it down beside the port. Everything that links to this identity
255
- // from OUTSIDE the process builds the origin from agent.json — the Actors
256
- // list, a sibling's page, the next start — and an identity set up before the
257
- // handle was recorded has only a port there, so every one of those links
258
- // came out bare. Pod state is the authority; this is the cache of it.
259
- this.recordHandle(config.handle);
260
-
261
- // Said once, where the config is finally known. Exposure without a gate
262
- // token is refused before the socket opens (startAgent), so reaching here
263
- // means the token is set and the whole surface is behind it. A UI password
264
- // is the second, per-person half: without one, anyone who has the token
265
- // gets a client bearer for the asking.
266
- // An install made before the shape changed goes on working, which is why
267
- // nothing here refuses — but it should not go on SILENTLY. The whole reason
268
- // this is said out loud is that bbba587 changed a default and every install
269
- // that already existed kept the old layout with nothing to show for it.
270
- const pending = pendingSteps(cred);
271
- for (const step of pending) {
272
- this.log(`this identity is on an older layout: ${step.what} (${step.why}). `
273
- + 'Run `fedipod upgrade` to see what is pending.');
274
- }
275
- // The record page says the same thing on its software row — the log is
276
- // read by nobody who lives in the GUI.
277
- this.pendingUpgrade = pending.map(s => s.what);
278
-
279
- if (process.env.AP_ALLOWED_HOSTS && !config.uiPassword) {
280
- this.log('WARNING: AP_ALLOWED_HOSTS is set and no UI password is — anyone holding '
281
- + 'AP_GATE_TOKEN can mint a client token. Run `fedipod passwd`.');
282
- }
283
-
284
- if (!act) return true; // reading only — see the note above
285
-
286
- // Exactly one agent may act on a pod (inbox drains are destructive
287
- // reads); later arrivals become read-only viewers of the same state.
288
- // Deliberately the REMOTE pod's ap-state/, never privateRoot's: a lease
289
- // that only one machine can reach is not a lease.
290
- this.lease = new Lease({
291
- url: this.urls.state + 'lease.json',
292
- fetchImpl: (u, i) => this.remote.fetch(u, i), log: this.log,
293
- });
294
- this.viewer = !(await this.lease.acquire());
295
-
296
- // Keys are LOCAL by default (the pod host never holds them); `setup
297
- // --keys pod` opts into sharing them through the pod so several devices
298
- // can sign as the same actor. Per-machine choice, so it rides in the
299
- // credential file rather than pod state.
300
- const keys = await resolveKeys(this.store, {
301
- localDir: cred.keysMode === 'pod' ? null : this.home,
302
- rotate: !!cred.rotateKeyOnce,
303
- actorId: this.urls.actor,
304
- log: this.log,
305
- // Consulted only when no key material exists anywhere: if the actor
306
- // already publishes a key, minting a new one would break federation.
307
- actorHasKey: async () => {
308
- const doc = await this.remote.getJson(this.urls.actor).catch(() => null);
309
- return !!doc?.publicKey?.publicKeyPem;
310
- },
311
- });
312
- if (cred.rotateKeyOnce) { // one-shot flag
313
- const { rotateKeyOnce, ...rest } = cred;
314
- writeJsonAtomic(path.join(this.home, 'credential.json'), rest);
315
- }
316
- // connect() can run more than once — connectWithRetry retries after a throw,
317
- // and the CLI connects for real after its confirmation. Deliverer arms a
318
- // 60s queue timer in its constructor and Intake owns a poll timer and a
319
- // websocket, so replacing them without stopping the old ones leaves both
320
- // ticking for the life of the process.
321
- this.intake?.stop();
322
- this.deliverer?.stop();
323
- this.tagfeed?.stop();
324
- clearInterval(this.schedTimer);
325
- this.publisher?.stopPolls();
326
- this.deliverer = new Deliverer({
327
- store: this.store, rsaPrivate: keys.rsaPrivate, keyId: this.urls.actor + '#main-key',
328
- actorId: this.urls.actor, edPrivate: keys.edPrivate,
329
- proofKeyId: assertionKeyId(this.urls),
330
- log: this.log, passive: this.viewer,
331
- onGone: () => this.publisher.publishCollections({ followers: true }),
332
- });
333
- this.publisher = new Publisher({
334
- config, remote: this.remote, store: this.store,
335
- deliverer: this.deliverer, publicKeyPem: keys.rsaPublicPem,
336
- assertionKey: keys.edPublicMultibase, log: this.log,
337
- resolveMention: (h) => resolveHandle(this, h),
338
- resolveActor: (u) => this.intake.fetchAP(u),
339
- // Inside a pod server the client surface answers on the pod's own
340
- // origin, so it can be advertised. Standalone it is on loopback, and
341
- // naming it in a world-readable actor would send clients nowhere.
342
- clientOrigin: this.embedded ? this.urls.base : null,
343
- });
344
- // Intake is constructed even for viewers — its signed fetchAP powers
345
- // search/deref; start() (draining) is active-only.
346
- // The dispatcher the admin surface also builds; this one is for what the
347
- // Gateway's outbox door took on the owner's behalf and the drain finds.
348
- this.c2s = new C2S({ agent: this, log: this.log });
349
- this.intake = new Intake({
350
- config, urls: this.urls, remote: this.remote, store: this.store,
351
- deliverer: this.deliverer, publisher: this.publisher, log: this.log, lease: this.lease,
352
- archive: this.privateStorage(cred, 'archive'),
353
- push: !this.embedded, pollSeconds: this.pollSeconds || null,
354
- ownerPost: (a, o) => this.c2s.dispatch(a, o),
355
- });
356
- // The CSV-import worker: paced, resumable, armed only while active.
357
- this.importer?.stop();
358
- this.importer = new ImportWorker({ agent: this, log: this.log });
359
- // Where the credentials for accounts held on OTHER servers are kept.
360
- const vaults = vaultsFor({ embedded: this.embedded, home: this.home, store: this.store });
361
- // The Bluesky connection, when one exists. Stamped to this actor; a
362
- // credential connected for another identity is treated as absent.
363
- this.atproto = new Atproto({
364
- localDir: this.home, actorId: this.urls.actor, log: this.log, vault: vaults.bluesky,
365
- });
366
- this.publisher.atproto = this.atproto;
367
- // Fediverse accounts the owner holds elsewhere. Same custody rules as the
368
- // Bluesky credential, and the same absence when stamped for someone else.
369
- this.fediaccts = new FediAccounts({
370
- localDir: this.home, actorId: this.urls.actor, log: this.log,
371
- vault: vaults.fedi, apps: vaults.apps,
372
- });
373
- this.acctfeed?.stop();
374
- this.acctfeed = null;
375
- this.bskyfeed?.stop();
376
- this.bskyfeed = null;
377
- this.bskygroup = null;
378
- this.intake.bskyGroup = null;
379
- if (this.viewer) {
380
- this.startViewer();
381
- this.log(this.lease.denied === 'unreadable'
382
- ? `the pod cannot be read, so the lease is unknown — viewing as @${config.handle} (read-only)`
383
- : `another agent is active for this pod — viewing as @${config.handle} (read-only)`);
384
- return true;
385
- }
386
- await this.startActive({ repair });
387
- return true;
388
- }
389
-
390
- // Claim the lease from whoever holds it and start acting. For the case where
391
- // the holder is gone but its lease has not expired — a crash, or a one-shot
392
- // command that exited without releasing — the only alternative is waiting out
393
- // the TTL, which is five minutes.
394
- async takeOver() {
395
- if (!this.viewer) return false;
396
- if (!await this.lease.takeover()) return false;
397
- this.log('took the lease over');
398
- await this.startActive({ promoted: true });
399
- return true;
400
- }
401
-
402
- // Mint a replacement signing key and republish the actor that advertises it.
403
- // These are one operation: a rotation without the republish leaves remote
404
- // servers verifying against a key we no longer hold, which fails every
405
- // delivery silently. The live deliverer and publisher are updated too, so the
406
- // running agent signs with the new key from here on.
407
- async rotateKey() {
408
- const cred = this.readCredential();
409
- const before = this.publisher.publicKeyPem;
410
- const keys = await resolveKeys(this.store, {
411
- localDir: cred.keysMode === 'pod' ? null : this.home,
412
- rotate: true,
413
- actorId: this.urls.actor,
414
- log: this.log,
415
- });
416
- this.publisher.publicKeyPem = keys.rsaPublicPem;
417
- this.publisher.assertionKey = keys.edPublicMultibase;
418
- this.deliverer.rsaPrivate = keys.rsaPrivate;
419
- this.deliverer.edPrivate = keys.edPrivate;
420
- await this.publisher.publishProfile();
421
- return { changed: before !== keys.rsaPublicPem, publicKeyPem: keys.rsaPublicPem };
422
- }
423
-
424
- // Park: as quiet as a pod can be while keeping its name, and revivable.
425
- // The following list is snapshotted FIRST — unfollowing is what stops the
426
- // traffic at source, but it also destroys the only record of who was being
427
- // followed, and "until I want to revive it" needs that record.
428
- async park({ unfollow = unfollowActor } = {}) {
429
- const following = await this._snapshotFollowing();
430
- const r = await this.quiesce({ unfollow });
431
- this.log(`parked: ${r.unfollowed} unfollow(s) recorded for revival, inbox closed`);
432
- return { ...r, snapshot: following.length };
433
- }
434
-
435
- // Undo a park: re-open the inbox, then re-follow everyone from the snapshot.
436
- // Each Follow needs the far end to Accept, so this is a request, not a
437
- // restoration — some will not come back, which is the nature of the thing.
438
- async revive({ follow = followActor } = {}) {
439
- const parked = this.store.read('parked.json', null);
440
- await this.publisher.openInbox();
441
- let refollowed = 0;
442
- for (const f of parked?.following || []) {
443
- try { await follow(this, f.actor); refollowed++; }
444
- catch (e) { this.log(`re-follow ${f.actor} failed: ${e.message}`); }
445
- }
446
- if (parked) this.store.remove('parked.json').catch(() => {});
447
- await this.store.flush();
448
- this.log(`revived: inbox open, ${refollowed}/${parked?.following?.length || 0} follow(s) re-sent`);
449
- return { refollowed, of: parked?.following?.length || 0, parkedAt: parked?.parkedAt || null };
450
- }
451
38
 
452
- // The follow graph, written down before it is torn down. Both going quiet
453
- // and moving away unfollow everyone, and the record page offers "active"
454
- // afterwards for either — so both have to leave something to come back from,
455
- // or setting it back re-follows nobody and says so only in a count.
456
- async _snapshotFollowing() {
457
- const following = this.store.getContacts().following;
458
- this.store.write('parked.json', {
459
- parkedAt: new Date().toISOString(),
460
- following: following.map(f => ({ actor: f.actor, handle: f.handle || null })),
461
- });
462
- await this.store.flush();
463
- return following;
464
- }
465
-
466
- // Keep the handle, take no more mail. Unfollowing is what actually stops the
467
- // volume — every account you follow pushes its posts into your inbox — and
468
- // closing the inbox handles the rest, which no follow graph can gate:
469
- // stranger mentions, new follow requests, outright spam.
470
- // `unfollow` is injectable so this is testable without a live pod.
471
- async quiesce({ unfollow = unfollowActor } = {}) {
472
- const following = this.store.getContacts().following.map(f => f.actor);
473
- let unfollowed = 0;
474
- for (const actor of following) {
475
- try { await unfollow(this, actor); unfollowed++; }
476
- catch (e) { this.log(`unfollow ${actor} failed: ${e.message}`); }
477
- }
478
- const quiescedAt = await this.publisher.closeInbox();
479
- this.log(`quiesced: unfollowed ${unfollowed}/${following.length}, inbox closed`);
480
- return { unfollowed, following: following.length, quiescedAt };
481
- }
482
-
483
- // Same, plus the fediverse-native redirect: followers are migrated to the
484
- // target by their own servers, and the old handle keeps resolving.
485
- async moveTo(target, { unfollow = unfollowActor } = {}) {
486
- const moved = await this.publisher.publishMove(target);
487
- const snapshot = await this._snapshotFollowing();
488
- const quiesced = await this.quiesce({ unfollow });
489
- return { ...moved, ...quiesced, snapshot: snapshot.length };
490
- }
491
-
492
- // True when it had to republish. Authenticated read: the question here is
493
- // whether the document EXISTS, not whether the world can see it —
494
- // verifyPublicSurface answers that one.
495
- async ensureActorPublished() {
496
- const doc = await this.remote.getJson(this.urls.actor);
497
- if (doc?.id) return false;
498
- this.log('actor document missing from the pod — republishing');
499
- await this.publisher.publishProfile({ force: true }); // the digest cannot know this
500
- return true;
501
- }
502
-
503
- // The featured collection was once written only on the first pin, so an
504
- // account from before then has none, and every server that shows the
505
- // profile is refused and asks again. One authenticated GET; the empty
506
- // collection goes up when the pod lacks it. True when it had to.
507
- async ensureFeaturedPublished() {
508
- const doc = await this.remote.getJson(this.urls.featured);
509
- if (doc?.id) return false;
510
- this.log('featured collection missing from the pod — publishing it');
511
- await this.publisher.publishFeatured();
512
- return true;
513
- }
514
-
515
- // The inbox's door must be open to the world (or to the gateway alone, in
516
- // locked mode) or nothing anyone sends ever lands. The permission is written
517
- // at setup, and a container made again later — a root move, a pod rebuilt —
518
- // came back without it: the group's inbox on fp2 refused every delivery for
519
- // nine days while its agent ran on, healthy-looking. One write per start,
520
- // and it is idempotent.
521
- async ensureInboxOpen() {
522
- const g = this.store.getConfig()?.gateway;
523
- const posture = g?.mode === 'locked' && g.webId ? { gatewayWebId: g.webId } : 'open';
524
- await podInbox.setPosture(this.remote, this.urls, posture);
525
- return posture;
526
- }
527
-
528
- // Read-only mode: refresh the state cache periodically, and take over the
529
- // moment the active agent's lease frees.
530
- startViewer() {
531
- this.viewer = true;
532
- // Five minutes, not one: with revalidation a quiet refresh is a single 304,
533
- // but a viewer still has no reason to ask twelve times an hour.
534
- this.refreshTimer = setInterval(async () => {
535
- try {
536
- if (this.viewer && await this.lease.acquire()) {
537
- this.log('lease freed — promoting to ACTIVE');
538
- await this.startActive({ promoted: true });
539
- return;
540
- }
541
- if (this.viewer) await this.store.load();
542
- } catch (e) { this.log(`viewer refresh: ${e.message}`); }
543
- }, Math.round(5 * 60_000 * (0.85 + Math.random() * 0.3)));
544
- this.refreshTimer.unref?.();
545
- }
546
-
547
- // The acting half: lease renewal, inbox drain, tag feed, delivery queue.
548
- // Called at connect when the lease is ours, or on viewer promotion.
549
- async startActive({ repair = true, promoted = false } = {}) {
550
- this.viewer = false;
551
- clearInterval(this.refreshTimer);
552
- if (promoted) await this.refreshBeforeActing();
553
- // Own posts the outbox names and the timeline index lacks come back here,
554
- // before anything acts on the index.
555
- await this.publisher.healStatuses().catch(e => this.log(`healing the timeline index: ${e.message}`));
556
- // Likes made before the liked list existed become its first entries.
557
- try { this.publisher.backfillLiked(); } catch (e) { this.log(`liked list: ${e.message}`); }
558
- this.lease.onLost = () => this.demote();
559
- this.lease.startRenewal();
560
- this.deliverer.startQueue();
561
- // Parked: no draining (the inbox is closed anyway) and no tag feed, so
562
- // starting the agent by accident does not undo the quiet.
563
- if (this.store.getConfig()?.quiescedAt) {
564
- this.log('parked — not draining or polling; run `fedipod revive` to resume');
565
- return; // renewal is already running, above
566
- }
567
- await this.intake.start();
568
- // An address that moved here from another gateway, whose move was left
569
- // pending (the old gateway could not be told): try again now that this
570
- // agent acts and the actor is published.
571
- completeGatewayMove(this).catch(e => this.log(`gateway move: ${e.message}`));
572
- // Reused, not replaced: demote() stops this instance but does not clear it,
573
- // so constructing a new one over the top orphaned the old chain beyond the
574
- // reach of any later stop().
575
- this.tagfeed ||= new TagFeed({ store: this.store, intake: this.intake, log: this.log });
576
- this.tagfeed.start();
577
- this.startBsky();
578
- this.startAccts();
579
- // Scheduled posts and polls whose time is up: a 30s sweep does what has
580
- // come due (lib/core/scheduled.mjs).
581
- clearInterval(this.schedTimer);
582
- this.schedTimer = setInterval(() => {
583
- publishDue(this.store, this.publisher, this.log).catch(err => this.log(`scheduled: ${err.message}`));
584
- }, 30_000);
585
- this.schedTimer.unref();
586
- // A CSV import interrupted by a restart or a handoff picks back up here.
587
- this.importer?.resume();
588
- // The identity the fediverse actually sees: a fronted actor's name and
589
- // host are the front's, not the pod's.
590
- this.log(`federating as @${publicHandle(this.store.getConfig())}@${new URL(this.urls.actor).host}`);
591
- if (this.renamed) {
592
- // The display name lives in the actor document, so a rename only
593
- // reaches other servers once that is republished.
594
- this.renamed = false;
595
- this.publisher.publishProfile()
596
- .then(() => this.log('actor document republished with the new display name'))
597
- .catch(e => this.log(`republish after rename failed: ${e.message}`));
598
- }
599
- // bootstrap writes the owner-only ACLs once, at setup, and nothing else
600
- // ever returns to them — so check on every start that the private trees
601
- // really are private, and repair them if not. Off the critical path.
602
- this.publisher.ensurePrivateAcls()
603
- .catch(e => this.log(`private-ACL check failed: ${e.message}`));
604
- // A publish that died half-way leaves an actor nobody can fetch while
605
- // everything here looks healthy — one GET to find out, and republishing
606
- // is idempotent.
607
- // Skipped during setup, which publishes the profile itself a moment later:
608
- // running both meant every first run wrote the whole wire face twice.
609
- if (repair) {
610
- this.ensureActorPublished()
611
- .catch(e => this.log(`actor check failed: ${e.message}`));
612
- this.ensureFeaturedPublished()
613
- .catch(e => this.log(`featured check failed: ${e.message}`));
614
- this.ensureInboxOpen()
615
- .catch(e => this.log(`inbox door check failed: ${e.message}`));
616
- // The human page, rewritten only when what it shows has changed: one
617
- // local digest compare, and a write the first time after an upgrade.
618
- this.publisher.publishProfilePage()
619
- .catch(e => this.log(`profile page: ${e.message}`));
620
- }
621
- this.seedFollowNotifications().catch(e => this.log(`notification seeding failed: ${e.message}`));
622
- }
623
-
624
- // Re-read state before acting on what we hold. A viewer's cache is kept
625
- // current by revalidation against the CONTAINER, whose ETag vouches for its
626
- // children existing rather than for their contents — so a peer rewriting
627
- // contacts.json or statuses.json in place moves nothing this agent would
628
- // notice. Only on promotion, never on a timer, and only documents that really
629
- // changed come back with a body.
630
- async refreshBeforeActing() {
631
- if (!this.store.storage) return;
632
- await this.store.load({ force: true })
633
- .catch(e => this.log(`state refresh on promotion: ${e.message}`));
634
- }
635
-
636
- // Another device claimed the lease (its user acted there) — stand down to
637
- // viewer so exactly one agent keeps draining.
638
- // The Bluesky mirror poll, only ever running when an account is connected.
639
- // Reused, not replaced, for the same orphaned-timer reason as tagfeed.
640
- startBsky() {
641
- if (this.viewer || !this.atproto?.connected()) return;
642
- if (this.store.getConfig()?.quiescedAt) return;
643
- // A group's account is the group's presence on Bluesky: follows are joins,
644
- // mentions are submissions, and both ride the notification hook.
645
- if (this.store.getConfig()?.kind === 'group') {
646
- this.bskygroup ||= new BskyGroup({
647
- store: this.store, atproto: this.atproto, intake: this.intake,
648
- publisher: this.publisher, log: this.log,
649
- });
650
- this.intake.bskyGroup = this.bskygroup;
651
- }
652
- this.bskyfeed ||= new BskyFeed({
653
- store: this.store, atproto: this.atproto, log: this.log,
654
- onNotification: async (n) => {
655
- if (!this.bskygroup) return;
656
- if (n.reason === 'follow') await this.bskygroup.onFollow(n.author);
657
- else await this.bskygroup.onMention(n);
658
- },
659
- });
660
- this.bskyfeed.start();
661
- }
662
-
663
- stopBsky() { this.bskyfeed?.stop(); }
664
-
665
- // The connected fediverse accounts' poll, only ever running when at least
666
- // one is connected. Reused, not replaced, for the same orphaned-timer reason
667
- // as tagfeed.
668
- startAccts() {
669
- if (this.viewer || !this.fediaccts?.connected()) return;
670
- if (this.store.getConfig()?.quiescedAt) return;
671
- this.acctfeed ||= new AcctFeed({
672
- store: this.store, accounts: this.fediaccts, log: this.log,
673
- });
674
- this.acctfeed.start();
675
- }
676
-
677
- stopAccts() { this.acctfeed?.stop(); }
678
-
679
- // Connecting or disconnecting an account changes what there is to poll, and
680
- // the last one going leaves a timer with nothing to ask.
681
- restartAccts() {
682
- this.stopAccts();
683
- this.startAccts();
684
- }
685
-
686
- demote() {
687
- if (this.viewer) return;
688
- this.log('another device took over — demoting to viewer');
689
- this.intake?.stop();
690
- this.tagfeed?.stop();
691
- this.bskyfeed?.stop();
692
- this.acctfeed?.stop();
693
- this.deliverer?.stop();
694
- this.importer?.stop();
695
- clearInterval(this.schedTimer);
696
- this.publisher?.stopPolls();
697
- // The lease too: standing down means standing down. Left renewing, a viewer
698
- // keeps writing to the pod on the active agent's behalf and can win the
699
- // lease back on a conditional PUT it had no business making.
700
- this.lease?.stopRenewal();
701
- this.startViewer();
702
- }
703
-
704
- // A user acting on this viewer outranks the idle active agent elsewhere:
705
- // claim the lease now (writes are safe immediately — publishing never
706
- // touches the inbox), but hold the destructive inbox drain until the old
707
- // active has seen the loss at its next renewal and demoted.
708
- async requestTakeover() {
709
- if (!this.viewer) return true;
710
- if (!(await this.lease.takeover())) return false;
711
- this.log('taking over from the other device (user action here)');
712
- this.viewer = false;
713
- clearInterval(this.refreshTimer);
714
- this.lease.onLost = () => this.demote();
715
- this.lease.startRenewal();
716
- this.deliverer.startQueue();
717
- setTimeout(async () => {
718
- if (this.viewer) return; // lost it again in the meantime
719
- try {
720
- await this.refreshBeforeActing();
721
- await this.intake.start();
722
- this.tagfeed ||= new TagFeed({ store: this.store, intake: this.intake, log: this.log });
723
- this.tagfeed.start();
724
- this.startBsky();
725
- this.startAccts();
726
- // A stranded CSV import resumes here too — startActive is not on the
727
- // takeover path, so without this the rows sat pending until a restart.
728
- this.importer?.resume();
729
- this.log('takeover complete — draining resumed on this device');
730
- } catch (e) { this.log(`takeover drain start: ${e.message}`); }
731
- }, 35_000).unref?.(); // > one renewal interval: old agent has demoted
732
- return true;
733
- }
734
-
735
- // A first run inherits followers without ever having seen them arrive, so
736
- // the notifications list starts empty where it should start full.
737
- async seedFollowNotifications() {
738
- if (this.store.has('notifications.json')) return;
739
- for (const f of this.store.getContacts().followers) {
740
- this.store.addNotification({ type: 'follow', actor: f.actor });
741
- }
742
- }
743
- }
39
+ // The class is imported from here by the callers that always did.
40
+ export { Agent };
744
41
 
745
42
  // Two megabytes is a few weeks of an ordinary agent's chatter, and small
746
43
  // enough that reading the whole thing is still reasonable.
@@ -759,10 +56,17 @@ export async function startAgent({
759
56
  // directory, same unit, same startup — a different thing to run. Its
760
57
  // credential says so, and running the agent over it would publish a person
761
58
  // where a forum lives.
762
- const { isForumHome, runForum } = await import('./packages/fedipod-bb/src/run.mjs');
763
- if (isForumHome(home)) {
59
+ // The forum package is beside this file in a checkout and absent from an
60
+ // installed fedipod; a home that holds a forum needs it, a person's does not.
61
+ let forum = null;
62
+ try {
63
+ forum = await import('./packages/fedipod-bb/src/run.mjs');
64
+ } catch (e) {
65
+ if (e?.code !== 'ERR_MODULE_NOT_FOUND') throw e;
66
+ }
67
+ if (forum?.isForumHome(home)) {
764
68
  console.log(`[bb:${port}] this home holds a forum — running it`);
765
- return runForum({ home, port, log: (...a) => console.log(`[bb:${port}]`, ...a) });
69
+ return forum.runForum({ home, port, log: (...a) => console.log(`[bb:${port}]`, ...a) });
766
70
  }
767
71
  // connect() sets this from pod state a moment later, but the browser may
768
72
  // already be opening — seed it from what setup recorded so the named origin
@@ -782,7 +86,7 @@ export async function startAgent({
782
86
  } catch { /* the agent still runs; it is just harder to find */ }
783
87
  }
784
88
  const logFile = path.join(home, 'agent.log');
785
- const agent = new Agent({ home, log: () => {} });
89
+ const agent = new Agent({ home, log: () => {}, upgradeCheck: pendingSteps });
786
90
  // The port on every line. Agents sharing a home share agent.log, and without
787
91
  // it there is no way to tell which one wrote what — a viewer's startup reads
788
92
  // as the active one's work.