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