@evanp/activitypub-bot 0.45.13 → 0.45.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,6 +9,15 @@ and this project adheres to
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.45.14] - 2026-05-10
13
+
14
+ ### Changed
15
+
16
+ - Depend on inbox forwarding for distribution to remote collections.
17
+ - More robust handling of client request failures in delivery and distribution.
18
+ - More robust handling of intake, fanout throttle errors.
19
+ - Store error stack for failing jobs in queue job entries.
20
+
12
21
  ## [0.45.13] - 2026-05-10
13
22
 
14
23
  ### Changed
@@ -4,6 +4,7 @@ import * as ttlcachePkg from '@isaacs/ttlcache'
4
4
 
5
5
  import as2 from './activitystreams.js'
6
6
  import BotMaker from './botmaker.js'
7
+ import { ThrottleError } from './requestthrottler.js'
7
8
 
8
9
  const TTLCache =
9
10
  ttlcachePkg.TTLCache ?? ttlcachePkg.default ?? ttlcachePkg
@@ -128,6 +129,7 @@ export class ActivityDeliverer {
128
129
  const deliveredTo = new Set()
129
130
  const actor = this.getActor(activity)
130
131
  const recipients = this.getRecipients(activity)
132
+ let fullActor
131
133
 
132
134
  for (const recipient of recipients) {
133
135
  this.#logger.debug({ recipient: recipient.id }, 'Checking recipient')
@@ -152,8 +154,18 @@ export class ActivityDeliverer {
152
154
  )
153
155
  }
154
156
  } else {
155
- const fullActor = await this.#client.get(actor.id)
156
- const fullRecipient = await this.#client.get(recipient.id)
157
+ fullActor = fullActor ?? await this.#client.get(actor.id)
158
+ let fullRecipient
159
+ try {
160
+ fullRecipient = await this.#client.get(recipient.id)
161
+ } catch (err) {
162
+ if (err instanceof ThrottleError) throw err
163
+ this.#logger.warn(
164
+ { recipient: recipient.id },
165
+ 'Unreachable remote recipient'
166
+ )
167
+ continue
168
+ }
157
169
  if (await this.#isRemoteActor(fullRecipient)) {
158
170
  this.#logger.warn({ recipient: recipient.id }, 'Skipping remote actor')
159
171
  } else if (await this.#isRemoteFollowersCollection(fullActor, fullRecipient)) {
@@ -164,6 +164,14 @@ export class ActivityDistributor {
164
164
  yield id
165
165
  } else if (this.#isRemoteCollection(obj)) {
166
166
  this.#logger.debug({ id }, 'Remote collection')
167
+ const owner = await this.#getCollectionOwner(obj, username)
168
+ if (owner && this.#isRecipient(activity, owner)) {
169
+ this.#logger.debug(
170
+ { id, owner: owner.id },
171
+ 'Leaving remote collection up to inbox forwarding'
172
+ )
173
+ continue
174
+ }
167
175
  try {
168
176
  for await (const item of this.#client.items(obj.id, username)) {
169
177
  this.#logger.debug({ id: item.id }, 'Remote collection member')
@@ -194,7 +202,16 @@ export class ActivityDistributor {
194
202
  return sharedInbox
195
203
  }
196
204
 
197
- const obj = await this.#client.get(actorId, username)
205
+ let obj
206
+ try {
207
+ obj = await this.#client.get(actorId, username)
208
+ } catch (err) {
209
+ this.#logger.warn(
210
+ { actorId, username, err },
211
+ 'Could not get actor in #getInbox'
212
+ )
213
+ return null
214
+ }
198
215
 
199
216
  // Get the shared inbox if it exists
200
217
 
@@ -232,7 +249,16 @@ export class ActivityDistributor {
232
249
  return directInbox
233
250
  }
234
251
 
235
- const obj = await this.#client.get(actorId, username)
252
+ let obj
253
+ try {
254
+ obj = await this.#client.get(actorId, username)
255
+ } catch (err) {
256
+ this.#logger.warn(
257
+ { actorId, username, err },
258
+ 'Could not get actor in #getDirectInbox'
259
+ )
260
+ return null
261
+ }
236
262
 
237
263
  if (!obj.inbox) {
238
264
  return null
@@ -274,4 +300,47 @@ export class ActivityDistributor {
274
300
  ? obj.type.some(t => COLLECTION_TYPES.includes(t))
275
301
  : COLLECTION_TYPES.includes(obj.type)
276
302
  }
303
+
304
+ async #getCollectionOwner (obj, username) {
305
+ if (obj.attributedTo) {
306
+ return obj.attributedTo.first
307
+ } else {
308
+ let ownerId
309
+ let owner
310
+
311
+ if (obj.id.endsWith('/followers')) {
312
+ ownerId = obj.id.replace('/followers', '')
313
+ } else if (obj.id.endsWith('/following')) {
314
+ ownerId = obj.id.replace('/following', '')
315
+ }
316
+
317
+ if (ownerId) {
318
+ try {
319
+ owner = await this.#client.get(ownerId, username)
320
+ } catch (err) {
321
+ this.#logger.debug(
322
+ { ownerId },
323
+ 'Owner unreachable'
324
+ )
325
+ }
326
+ }
327
+
328
+ return owner
329
+ }
330
+ }
331
+
332
+ #isRecipient (activity, obj) {
333
+ const props = ['to', 'cc', 'audience', 'bto', 'bcc']
334
+ for (const prop of props) {
335
+ const p = activity.get(prop)
336
+ if (p) {
337
+ for (const value of p) {
338
+ if (value.id === obj.id) {
339
+ return true
340
+ }
341
+ }
342
+ }
343
+ }
344
+ return false
345
+ }
277
346
  }
@@ -1,7 +1,8 @@
1
1
  import assert from 'node:assert'
2
2
 
3
3
  import as2 from './activitystreams.js'
4
- import { Worker } from './worker.js'
4
+ import { Worker, RecoverableError } from './worker.js'
5
+ import { ThrottleError } from './requestthrottler.js'
5
6
 
6
7
  export class FanoutWorker extends Worker {
7
8
  #distributor
@@ -15,6 +16,17 @@ export class FanoutWorker extends Worker {
15
16
  async doJob (payload, attempts) {
16
17
  const activity = await as2.import(payload.activity)
17
18
  const { username } = payload
18
- await this.#distributor.fanout(activity, username)
19
+ try {
20
+ await this.#distributor.fanout(activity, username)
21
+ } catch (err) {
22
+ if (err instanceof ThrottleError) {
23
+ this._logger.warn(
24
+ { err, activity: activity.id, username },
25
+ 'Throttled fanout, waiting to retry')
26
+ throw new RecoverableError(err.message, err.waitTime)
27
+ } else {
28
+ throw err
29
+ }
30
+ }
19
31
  }
20
32
  }
@@ -1,7 +1,8 @@
1
1
  import assert from 'node:assert'
2
2
 
3
3
  import as2 from './activitystreams.js'
4
- import { Worker } from './worker.js'
4
+ import { Worker, RecoverableError } from './worker.js'
5
+ import { ThrottleError } from './requestthrottler.js'
5
6
 
6
7
  export class IntakeWorker extends Worker {
7
8
  #deliverer
@@ -17,6 +18,17 @@ export class IntakeWorker extends Worker {
17
18
  async doJob (payload, attempts) {
18
19
  const raw = payload.activity
19
20
  const activity = await as2.import(raw)
20
- await this.#deliverer.deliverToAll(activity, this.#bots)
21
+ try {
22
+ await this.#deliverer.deliverToAll(activity, this.#bots)
23
+ } catch (err) {
24
+ if (err instanceof ThrottleError) {
25
+ this._logger.warn(
26
+ { err, activity: activity.id },
27
+ 'Throttled intake, waiting to retry')
28
+ throw new RecoverableError(err.message, err.waitTime)
29
+ } else {
30
+ throw err
31
+ }
32
+ }
21
33
  }
22
34
  }
package/lib/jobqueue.js CHANGED
@@ -87,12 +87,11 @@ export class JobQueue {
87
87
  this.#logger.debug({ method: 'release', jobRunnerId, jobId }, 'released job')
88
88
  }
89
89
 
90
- async retryAfter (jobId, jobRunnerId, delay) {
90
+ async retryAfter (jobId, jobRunnerId, delay, lastError = null) {
91
91
  assert.ok(jobId)
92
92
  assert.strictEqual(typeof jobId, 'string')
93
93
  assert.ok(jobRunnerId)
94
94
  assert.strictEqual(typeof jobRunnerId, 'string')
95
- assert.ok(delay)
96
95
  assert.strictEqual(typeof delay, 'number')
97
96
 
98
97
  const retryAfter = new Date(Date.now() + delay)
@@ -102,9 +101,10 @@ export class JobQueue {
102
101
  SET claimed_by = NULL,
103
102
  claimed_at = NULL,
104
103
  updated_at = CURRENT_TIMESTAMP,
105
- retry_after = ?
104
+ retry_after = ?,
105
+ last_error = ?
106
106
  WHERE job_id = ? AND claimed_by = ?;`,
107
- { replacements: [retryAfter, jobId, jobRunnerId] }
107
+ { replacements: [retryAfter, lastError, jobId, jobRunnerId] }
108
108
  )
109
109
  this.#logger.debug(
110
110
  { method: 'retry', jobRunnerId, jobId, delay },
@@ -157,7 +157,7 @@ export class JobQueue {
157
157
  this.#ac.abort()
158
158
  }
159
159
 
160
- async fail (jobId, jobRunnerId) {
160
+ async fail (jobId, jobRunnerId, lastError = null) {
161
161
  await this.#connection.query(`
162
162
  INSERT INTO failed_job
163
163
  (job_id, queue_id, priority, payload, claimed_at, claimed_by, attempts, retry_after, created_at, updated_at)
@@ -170,6 +170,14 @@ export class JobQueue {
170
170
  DELETE FROM job
171
171
  WHERE job_id = ? AND claimed_by = ?;`,
172
172
  { replacements: [jobId, jobRunnerId] })
173
+
174
+ if (lastError) {
175
+ await this.#connection.query(`
176
+ UPDATE failed_job
177
+ SET last_error = ?
178
+ WHERE job_id = ?;`,
179
+ { replacements: [lastError, jobId] })
180
+ }
173
181
  this.#logger.debug({ method: 'complete', jobRunnerId, jobId }, 'completed job')
174
182
  }
175
183
 
@@ -0,0 +1,12 @@
1
+ export const id = '011-job-last-error'
2
+
3
+ export async function up (connection, queryOptions = {}) {
4
+ await connection.query(`
5
+ ALTER TABLE job
6
+ ADD COLUMN last_error TEXT;
7
+ `, queryOptions)
8
+ await connection.query(`
9
+ ALTER TABLE failed_job
10
+ ADD COLUMN last_error TEXT;
11
+ `, queryOptions)
12
+ }
package/lib/worker.js CHANGED
@@ -55,7 +55,7 @@ export class Worker {
55
55
  this.#logger.warn({ err, jobId }, 'error before job, retrying')
56
56
  if (jobId) {
57
57
  const delay = this.#retryDelay(attempts ?? 1)
58
- await this.#jobQueue.retryAfter(jobId, this.#workerId, delay)
58
+ await this.#jobQueue.retryAfter(jobId, this.#workerId, delay, err.stack)
59
59
  }
60
60
  continue
61
61
  }
@@ -69,9 +69,9 @@ export class Worker {
69
69
  await this.#jobQueue.complete(jobId, this.#workerId)
70
70
  } catch (err) {
71
71
  if (err instanceof RecoverableError) {
72
- await this.#jobQueue.retryAfter(jobId, this.#workerId, err.delay)
72
+ await this.#jobQueue.retryAfter(jobId, this.#workerId, err.delay, err.stack)
73
73
  } else {
74
- await this.#jobQueue.fail(jobId, this.#workerId)
74
+ await this.#jobQueue.fail(jobId, this.#workerId, err.stack)
75
75
  }
76
76
  }
77
77
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evanp/activitypub-bot",
3
- "version": "0.45.13",
3
+ "version": "0.45.14",
4
4
  "description": "server-side ActivityPub bot framework",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",