spur-board 0.17.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/daemon.mjs +118 -27
  2. package/package.json +1 -1
package/dist/daemon.mjs CHANGED
@@ -9050,6 +9050,50 @@ async function restartNotification(trx, sourceKey, at) {
9050
9050
  next_at: at.toISOString()
9051
9051
  }).where("source_key", "=", sourceKey).where("acked_at", "is", null).where("exhausted_at", "is not", null).returning("id").executeTakeFirst() === void 0) throw new CommandError("retry_not_applicable", "The doorbell was read or is still ringing; close the item instead.");
9052
9052
  }
9053
+ /**
9054
+ * Ends a ring Spur will not take (a permanent rejection, or no answer for the retry window): its
9055
+ * notification stops ringing, the operator gets an item and `doorbell_undelivered` is journalled.
9056
+ * The operator's Retry on the item restarts the series (`restartNotification`). An inbound author
9057
+ * ring has no notification: it ends like an author found unavailable at enqueue.
9058
+ */
9059
+ async function ringUndelivered(trx, input) {
9060
+ const { at, reason } = input;
9061
+ if (input.notificationId === void 0) {
9062
+ const task = await ringTask(trx, void 0, input.dedupeKey);
9063
+ if (task === void 0) return;
9064
+ await emit(trx, "doorbell_undelivered", task.project, task.id, at.toISOString(), {
9065
+ doorbell: input.dedupeKey,
9066
+ reason
9067
+ });
9068
+ await raiseItem(trx, {
9069
+ itemKey: `doorbell:unavailable:${input.dedupeKey}`,
9070
+ kind: "answer_author_unavailable",
9071
+ text: `Answer author unavailable: ${reason}`,
9072
+ url: taskUrl(task, input.config),
9073
+ taskId: task.id,
9074
+ blockId: null,
9075
+ at
9076
+ });
9077
+ return;
9078
+ }
9079
+ const n = await trx.selectFrom("notification").selectAll().where("id", "=", input.notificationId).executeTakeFirst();
9080
+ if (n === void 0 || n.acked_at !== null || n.exhausted_at !== null) return;
9081
+ const task = await trx.selectFrom("task").selectAll().where("id", "=", n.task_id).executeTakeFirstOrThrow();
9082
+ await trx.updateTable("notification").set({ exhausted_at: at.toISOString() }).where("id", "=", n.id).execute();
9083
+ await raiseItem(trx, {
9084
+ itemKey: exhaustedItemKey(n),
9085
+ kind: exhaustedItemKind(n),
9086
+ text: n.kind === "answer" ? `Answer to block B${String(n.block_id)} on ${task.key} was not delivered: ${reason}` : `worker ${input.session} did not get block B${String(n.block_id)} on ${task.key}: ${reason}`,
9087
+ url: taskUrl(task, input.config),
9088
+ taskId: task.id,
9089
+ blockId: n.block_id,
9090
+ at
9091
+ });
9092
+ await journal$2(trx, n, "doorbell_undelivered", {
9093
+ doorbell: `N${String(n.id)}`,
9094
+ reason
9095
+ }, at);
9096
+ }
9053
9097
  /** Reconcile due semantic notifications in the sender's claim transaction. */
9054
9098
  async function reconcileDoorbells(trx, at, config) {
9055
9099
  const due = await trx.selectFrom("notification").innerJoin("task", "task.id", "notification.task_id").selectAll("notification").where("task.membership", "!=", "archived").where("notification.acked_at", "is", null).where("notification.exhausted_at", "is", null).where("notification.next_at", "<=", at.toISOString()).orderBy("notification.id").execute();
@@ -9719,7 +9763,7 @@ async function archiveTask(trx, input) {
9719
9763
  await trx.updateTable("outbox").set({
9720
9764
  state: "dead",
9721
9765
  last_error: "superseded"
9722
- }).where("kind", "=", "doorbell").where("state", "in", ["pending", "post_failed"]).where("uncertain", "=", 0).where((eb) => eb.or([eb("id", "in", eb.selectFrom("notification").select("last_outbox_id").where("task_id", "=", task.id).where("last_outbox_id", "is not", null)), ...comments.map((comment) => eb("dedupe_key", "like", `doorbell:comment:${String(comment.id)}:%`))])).execute();
9766
+ }).where("kind", "=", "doorbell").where("state", "in", ["pending", "post_failed"]).where((eb) => eb.or([eb("id", "in", eb.selectFrom("notification").select("last_outbox_id").where("task_id", "=", task.id).where("last_outbox_id", "is not", null)), ...comments.map((comment) => eb("dedupe_key", "like", `doorbell:comment:${String(comment.id)}:%`))])).execute();
9723
9767
  await trx.updateTable("task").set({
9724
9768
  membership: "archived",
9725
9769
  archive_reason: reason,
@@ -9844,6 +9888,13 @@ function orderItems(items) {
9844
9888
  }
9845
9889
  //#endregion
9846
9890
  //#region ../core/src/app/sender.ts
9891
+ /**
9892
+ * Spur refused a doorbell for good (a permanent 4xx): the sender stops ringing and ends the ring
9893
+ * instead of retrying it. Any other error from `send` is retried.
9894
+ */
9895
+ var SpurRejectedError = class extends Error {
9896
+ name = "SpurRejectedError";
9897
+ };
9847
9898
  /** 1 msg/s per space: a platform limit, not an operator setting. */
9848
9899
  const SPACE_INTERVAL_MS = 1e3;
9849
9900
  const RETRY_WINDOW_MS = 30 * 6e4;
@@ -9966,6 +10017,28 @@ async function requeuePostFailed(trx, ids, nowIso) {
9966
10017
  }
9967
10018
  return failed.length;
9968
10019
  }
10020
+ /**
10021
+ * Returns every `in_flight` row to `pending`: one whose send never started gives its attempt
10022
+ * back; one whose send started may be out, so it goes again at once as uncertain. Its comment
10023
+ * goes back to `pending`. Only a crash or a failed earlier tick leaves a row `in_flight`.
10024
+ */
10025
+ async function releaseOrphans(trx, nowIso) {
10026
+ const rows = await trx.selectFrom("outbox").select(["kind", "payload"]).where("state", "=", "in_flight").execute();
10027
+ if (rows.length === 0) return;
10028
+ await trx.updateTable("outbox").set((eb) => ({
10029
+ state: "pending",
10030
+ attempts: eb("attempts", "-", 1)
10031
+ })).where("state", "=", "in_flight").where("attempt_started", "=", 0).execute();
10032
+ await trx.updateTable("outbox").set({
10033
+ state: "pending",
10034
+ uncertain: 1,
10035
+ next_at: nowIso
10036
+ }).where("state", "=", "in_flight").where("attempt_started", "=", 1).execute();
10037
+ for (const row of rows) {
10038
+ const body = parseBody(row);
10039
+ if (typeof body !== "string") await setComment(trx, body, { delivery_state: "pending" });
10040
+ }
10041
+ }
9969
10042
  function backoff(attempts) {
9970
10043
  return Math.min(1e3 * 2 ** Math.max(attempts - 1, 0), MAX_BACKOFF_MS$1);
9971
10044
  }
@@ -10044,11 +10117,13 @@ function createSender(options) {
10044
10117
  await trx.updateTable("outbox").set({ first_attempt_at: null }).where("state", "in", ["pending", "in_flight"]).where("kind", "in", kinds).execute();
10045
10118
  }
10046
10119
  /**
10047
- * tx 1: cancels stale rows, raises down-health and claims at most one due row per lane.
10120
+ * tx 1: returns rows a failed tick left in flight, cancels stale rows, raises down-health and
10121
+ * claims at most one due row per lane.
10048
10122
  * `ranThreshold` says the outage's down rows were written; the caller records that after commit.
10049
10123
  */
10050
10124
  async function claim(trx, at) {
10051
10125
  const nowIso = new Date(at).toISOString();
10126
+ await releaseOrphans(trx, nowIso);
10052
10127
  await reconcileDoorbells(trx, new Date(at), options.config);
10053
10128
  const rows = await trx.selectFrom("outbox").selectAll().where("state", "in", ["pending", "post_failed"]).orderBy("id").execute();
10054
10129
  const bodies = /* @__PURE__ */ new Map();
@@ -10160,7 +10235,7 @@ function createSender(options) {
10160
10235
  };
10161
10236
  }
10162
10237
  }
10163
- if (c.body.kind === "doorbell" && !c.maybeSent && (await ringTask(trx, c.body.payload.notificationId, c.key))?.membership === "archived") {
10238
+ if (c.body.kind === "doorbell" && (await ringTask(trx, c.body.payload.notificationId, c.key))?.membership === "archived") {
10164
10239
  await kill(trx, [c.id], "superseded");
10165
10240
  return {
10166
10241
  at,
@@ -10254,12 +10329,17 @@ function createSender(options) {
10254
10329
  error: null
10255
10330
  };
10256
10331
  } catch (error) {
10332
+ const message = error instanceof Error ? error.message : String(error);
10333
+ if (error instanceof SpurRejectedError) return {
10334
+ outcome: { status: "rejected" },
10335
+ error: message
10336
+ };
10257
10337
  return {
10258
10338
  outcome: {
10259
10339
  status: "retry",
10260
10340
  uncertain: true
10261
10341
  },
10262
- error: error instanceof Error ? error.message : String(error)
10342
+ error: message
10263
10343
  };
10264
10344
  }
10265
10345
  }
@@ -10275,6 +10355,19 @@ function createSender(options) {
10275
10355
  }).execute();
10276
10356
  await setComment(trx, body, { delivery_state: "post_failed" });
10277
10357
  };
10358
+ /** Kills a ring Spur will not take and ends its notification. */
10359
+ const endRing = async (reason) => {
10360
+ await kill(trx, [c.id], reason);
10361
+ if (body.kind !== "doorbell") return;
10362
+ await ringUndelivered(trx, {
10363
+ notificationId: body.payload.notificationId,
10364
+ dedupeKey: c.key,
10365
+ session: body.payload.session,
10366
+ reason,
10367
+ at: new Date(at),
10368
+ config: options.config
10369
+ });
10370
+ };
10278
10371
  switch (outcome.status) {
10279
10372
  case "sent":
10280
10373
  await row.set({
@@ -10349,6 +10442,7 @@ function createSender(options) {
10349
10442
  uncertain,
10350
10443
  last_error: lastError
10351
10444
  });
10445
+ if (space === null && at - firstAt >= RETRY_WINDOW_MS) return endRing(`no answer from Spur for 30 min: ${lastError}`);
10352
10446
  await row.set({
10353
10447
  state: "pending",
10354
10448
  uncertain,
@@ -10359,6 +10453,7 @@ function createSender(options) {
10359
10453
  await setComment(trx, body, { delivery_state: "pending" });
10360
10454
  return;
10361
10455
  }
10456
+ case "rejected": return endRing(error ?? "rejected by Spur");
10362
10457
  case "failed":
10363
10458
  if (outcome.scope === "message" || space === null) return postFailed({ last_error: outcome.reason });
10364
10459
  if ((await trx.insertInto("transport_health").values({
@@ -10403,26 +10498,8 @@ function createSender(options) {
10403
10498
  async recover() {
10404
10499
  const nowIso = now().toISOString();
10405
10500
  const failed = await scanTransaction(store, options.config, async (trx) => {
10406
- const rows = await trx.selectFrom("outbox").select([
10407
- "id",
10408
- "kind",
10409
- "payload",
10410
- "state"
10411
- ]).where("state", "in", ["in_flight", "post_failed"]).execute();
10412
- await trx.updateTable("outbox").set((eb) => ({
10413
- state: "pending",
10414
- attempts: eb("attempts", "-", 1)
10415
- })).where("state", "=", "in_flight").where("attempt_started", "=", 0).execute();
10416
- await trx.updateTable("outbox").set({
10417
- state: "pending",
10418
- uncertain: 1,
10419
- next_at: nowIso
10420
- }).where("state", "=", "in_flight").where("attempt_started", "=", 1).execute();
10421
- for (const row of rows.filter((candidate) => candidate.state === "in_flight")) {
10422
- const body = parseBody(row);
10423
- if (typeof body !== "string") await setComment(trx, body, { delivery_state: "pending" });
10424
- }
10425
- return rows.filter((row) => row.state === "post_failed").map((row) => row.id);
10501
+ await releaseOrphans(trx, nowIso);
10502
+ return (await trx.selectFrom("outbox").select("id").where("state", "=", "post_failed").execute()).map((row) => row.id);
10426
10503
  });
10427
10504
  for (let i = 0; i < failed.length; i += RECOVER_BATCH) {
10428
10505
  const batch = failed.slice(i, i + RECOVER_BATCH);
@@ -15852,10 +15929,14 @@ function createDeliveryGateway(client, channels) {
15852
15929
  };
15853
15930
  } catch (error) {
15854
15931
  const result = apiFailure(error);
15855
- return result.status === "retry" && confirmed ? {
15932
+ if (!confirmed) return result;
15933
+ return result.status === "retry" ? {
15856
15934
  ...result,
15857
15935
  uncertain: true
15858
- } : result;
15936
+ } : {
15937
+ status: "retry",
15938
+ uncertain: true
15939
+ };
15859
15940
  }
15860
15941
  }
15861
15942
  return {
@@ -16436,6 +16517,13 @@ function createBoardModules(ctxFor, options = {}) {
16436
16517
  //#region ../daemon/src/spur-port.ts
16437
16518
  const SEND_TIMEOUT_MS = 8e3;
16438
16519
  const ERROR_TEXT_LIMIT = 200;
16520
+ /** 4xx statuses Spur may answer for a passing state; every other 4xx ends the ring. */
16521
+ const RETRYABLE_4XX = /* @__PURE__ */ new Set([
16522
+ 408,
16523
+ 409,
16524
+ 425,
16525
+ 429
16526
+ ]);
16439
16527
  /** The request Spur's own `spur send` makes; the doctor probe sends the same one. */
16440
16528
  function spurSendRequest(baseUrl, session, text) {
16441
16529
  return {
@@ -16476,7 +16564,10 @@ function createSpurPort(options) {
16476
16564
  return;
16477
16565
  }
16478
16566
  const error = await spurErrorText(response);
16479
- throw new Error(`Spur send failed with HTTP ${String(response.status)}${error === null ? "" : `: ${error}`}`);
16567
+ const { status } = response;
16568
+ const message = `Spur send failed with HTTP ${String(status)}${error === null ? "" : `: ${error}`}`;
16569
+ if (status >= 400 && status < 500 && !RETRYABLE_4XX.has(status)) throw new SpurRejectedError(message);
16570
+ throw new Error(message);
16480
16571
  } };
16481
16572
  }
16482
16573
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spur-board",
3
- "version": "0.17.0",
3
+ "version": "0.17.1",
4
4
  "license": "MIT",
5
5
  "engines": {
6
6
  "node": "^24.12.0 || ^26.0.0"