spur-board 0.20.0 → 0.20.2

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 +73 -15
  2. package/package.json +1 -1
package/dist/daemon.mjs CHANGED
@@ -7633,7 +7633,12 @@ function checkGatewayStrings(strings) {
7633
7633
  }
7634
7634
  return null;
7635
7635
  }
7636
- /** Replays database-only work after scanning exact strings outside the write transaction. */
7636
+ /**
7637
+ * Replays database-only work after scanning exact strings outside the write transaction.
7638
+ * `fn` must build the same gateway strings on every pass: read a time that lands in one, such as
7639
+ * an outbox payload, before the call, as a time read inside `fn` makes a new string each pass and
7640
+ * the replay never ends. A time kept only in the database may be read inside.
7641
+ */
7637
7642
  async function scanTransaction(store, config, fn) {
7638
7643
  const known = /* @__PURE__ */ new Map();
7639
7644
  for (let pass = 0; pass < MAX_PASSES; pass += 1) {
@@ -12107,9 +12112,16 @@ async function session(trx, config, fact, at, source, ambiguousBranches) {
12107
12112
  role = label === "board-worker" ? "worker" : "other";
12108
12113
  if (key !== void 0) break;
12109
12114
  }
12115
+ let prOnlyKey;
12110
12116
  if (key === void 0 && fact.pr !== null) {
12111
- const linked = await trx.selectFrom("link").innerJoin("task", "task.id", "link.task_id").select(["task.key", "task.project"]).where("link.entity", "=", "pr").where(sql`lower(link.repo) = ${fact.pr.repo.toLowerCase()}`).where("link.number", "=", fact.pr.number).where("link.banned", "=", 0).executeTakeFirst();
12112
- if (config.projects.find((entry) => entry.id === linked?.project)?.spurProjects.some((spur) => spur.name === fact.spurProject && spur.role === "worker")) key = linked?.key;
12117
+ const linked = await trx.selectFrom("link").innerJoin("task", "task.id", "link.task_id").leftJoin("pr_snapshot", (join) => join.on(sql`lower(pr_snapshot.repo) = lower(link.repo)`).onRef("pr_snapshot.number", "=", "link.number")).select([
12118
+ "task.key",
12119
+ "task.project",
12120
+ "pr_snapshot.head_branch"
12121
+ ]).where("link.entity", "=", "pr").where(sql`lower(link.repo) = ${fact.pr.repo.toLowerCase()}`).where("link.number", "=", fact.pr.number).where("link.banned", "=", 0).executeTakeFirst();
12122
+ const project = config.projects.find((entry) => entry.id === linked?.project);
12123
+ if (linked !== void 0 && project?.spurProjects.some((spur) => spur.name === fact.spurProject && spur.role === "worker")) if (fact.branch !== null && linked.head_branch === fact.branch) key = linked.key;
12124
+ else prOnlyKey = linked.key;
12113
12125
  }
12114
12126
  if (key === void 0 && fact.branch !== null) {
12115
12127
  const tasks = await trx.selectFrom("task").selectAll().where("membership", "!=", "archived").execute();
@@ -12123,6 +12135,11 @@ async function session(trx, config, fact, at, source, ambiguousBranches) {
12123
12135
  if (!ambiguousBranches.has(fact.branch) || worker !== void 0) key = candidate?.key;
12124
12136
  }
12125
12137
  }
12138
+ const fromLabel = role !== void 0;
12139
+ if (key === void 0 && prOnlyKey !== void 0) {
12140
+ key = prOnlyKey;
12141
+ role = "other";
12142
+ }
12126
12143
  if (key !== void 0 && projectForKey(config, key) !== null) {
12127
12144
  const task = await trx.selectFrom("task").selectAll().where("key", "=", key).executeTakeFirst();
12128
12145
  if (task !== void 0) {
@@ -12137,7 +12154,7 @@ async function session(trx, config, fact, at, source, ambiguousBranches) {
12137
12154
  pr_state: null,
12138
12155
  session_id: fact.id,
12139
12156
  role: chosenRole,
12140
- source: role === void 0 ? "branch" : "spawn_link",
12157
+ source: fromLabel ? "spawn_link" : "branch",
12141
12158
  banned: 0,
12142
12159
  created_at: at,
12143
12160
  ticket_key: null,
@@ -12147,7 +12164,7 @@ async function session(trx, config, fact, at, source, ambiguousBranches) {
12147
12164
  entity: "session",
12148
12165
  session: fact.id,
12149
12166
  role: chosenRole,
12150
- source: role === void 0 ? "branch" : "spawn_link",
12167
+ source: fromLabel ? "spawn_link" : "branch",
12151
12168
  facts: fact
12152
12169
  });
12153
12170
  }
@@ -12220,6 +12237,7 @@ function createObservationCore(deps) {
12220
12237
  },
12221
12238
  async observe(source, observation) {
12222
12239
  const { batch } = observation;
12240
+ const now = clock.now();
12223
12241
  await scanTransaction(store, config, async (trx) => {
12224
12242
  await sourceState(trx, source, observation.state, batch.observedAt, batch.cursor);
12225
12243
  for (const outcome of observation.outcomes) {
@@ -12313,7 +12331,7 @@ function createObservationCore(deps) {
12313
12331
  const activeTasks = await trx.selectFrom("task").select("id").where("membership", "!=", "archived").execute();
12314
12332
  for (const task of activeTasks) await updateTerminal(trx, config, task.id, batch.observedAt, currentComplete, blind);
12315
12333
  await reconcileMembership(trx, config, currentComplete, batch.observedAt);
12316
- await reconcileItems(trx, config, clock.now(), currentComplete.has("spur"));
12334
+ await reconcileItems(trx, config, now, currentComplete.has("spur"));
12317
12335
  if (deps.productVersion !== void 0) await enqueueLiveAfterFirstPoll(trx, {
12318
12336
  config,
12319
12337
  productVersion: deps.productVersion,
@@ -12325,12 +12343,14 @@ function createObservationCore(deps) {
12325
12343
  if (observation.effectiveComplete) completeSince.set(source, batch.observedAt);
12326
12344
  },
12327
12345
  reportSourceState(source, state) {
12346
+ const now = clock.now();
12328
12347
  return scanTransaction(store, config, async (trx) => {
12329
12348
  await sourceState(trx, source, state);
12330
- await reconcileItems(trx, config, clock.now(), completeSince.has("spur"));
12349
+ await reconcileItems(trx, config, now, completeSince.has("spur"));
12331
12350
  });
12332
12351
  },
12333
12352
  notice(kind, value) {
12353
+ const at = new Date(clock.now()).toISOString();
12334
12354
  return scanTransaction(store, config, async (trx) => {
12335
12355
  const key = `spur-notice:${kind}:${value}`;
12336
12356
  if (await trx.selectFrom("meta").select("key").where("key", "=", key).executeTakeFirst() !== void 0) return;
@@ -12338,7 +12358,7 @@ function createObservationCore(deps) {
12338
12358
  key,
12339
12359
  value: "true"
12340
12360
  }).execute();
12341
- await emit(trx, "spur_unknown_state", RESERVED_PROJECT, null, new Date(clock.now()).toISOString(), {
12361
+ await emit(trx, "spur_unknown_state", RESERVED_PROJECT, null, at, {
12342
12362
  kind,
12343
12363
  value
12344
12364
  });
@@ -15011,6 +15031,18 @@ function runSource(entry, options, runnerStartMs) {
15011
15031
  reason: "stale",
15012
15032
  message: `No complete observation in ${String(staleMultiplier)} poll intervals`
15013
15033
  };
15034
+ function fail(step, error, issues) {
15035
+ const kind = error instanceof AdapterError ? error.kind : "unexpected";
15036
+ try {
15037
+ options.onFailure?.({
15038
+ source,
15039
+ step,
15040
+ kind,
15041
+ error,
15042
+ ...issues === void 0 ? {} : { issues }
15043
+ });
15044
+ } catch {}
15045
+ }
15014
15046
  let stopping = false;
15015
15047
  let failures = 0;
15016
15048
  let lastCompleteMs;
@@ -15045,7 +15077,9 @@ function runSource(entry, options, runnerStartMs) {
15045
15077
  try {
15046
15078
  await core.reportSourceState(source, next);
15047
15079
  lastReported = json;
15048
- } catch {}
15080
+ } catch (error) {
15081
+ fail("report", error);
15082
+ }
15049
15083
  });
15050
15084
  return reports;
15051
15085
  }
@@ -15081,6 +15115,7 @@ function runSource(entry, options, runnerStartMs) {
15081
15115
  controller = abort;
15082
15116
  const startMs = now();
15083
15117
  let outcome;
15118
+ let step = "context";
15084
15119
  try {
15085
15120
  const received = await core.context(source);
15086
15121
  const context = entry.repo === void 0 ? received : {
@@ -15097,20 +15132,31 @@ function runSource(entry, options, runnerStartMs) {
15097
15132
  hydrated = true;
15098
15133
  }
15099
15134
  const requested = [...context.observe.prs, ...context.observe.tickets];
15135
+ step = "poll";
15100
15136
  const batch = await untilAborted(adapter.poll({
15101
15137
  ...context,
15102
15138
  signal: abort.signal
15103
15139
  }), abort.signal);
15104
15140
  if (aborted()) return;
15141
+ step = "batch";
15105
15142
  const parsed = batchSchema.safeParse({
15106
15143
  ...batch,
15107
15144
  observedAt: iso$1(startMs)
15108
15145
  });
15109
- if (!parsed.success || parsed.data.source !== source || !answersRequest(context.observe, parsed.data.entities)) outcome = {
15110
- kind: "failed",
15111
- error: new AdapterError("transient", "Invalid batch")
15112
- };
15113
- else {
15146
+ const invalid = !parsed.success ? void 0 : parsed.data.source !== source ? "names another source" : !answersRequest(context.observe, parsed.data.entities) ? "does not answer the request" : void 0;
15147
+ if (!parsed.success || invalid !== void 0) {
15148
+ const error = new AdapterError("transient", `Invalid batch: ${invalid ?? "fails the batch schema"}`);
15149
+ fail("batch", error, parsed.error?.issues.map((issue) => ({
15150
+ path: issue.path.map(String).join("."),
15151
+ code: issue.code,
15152
+ message: issue.message
15153
+ })));
15154
+ outcome = {
15155
+ kind: "failed",
15156
+ error
15157
+ };
15158
+ } else {
15159
+ step = "outcomes";
15114
15160
  const outcomes = applyEntityOutcomes({
15115
15161
  counts,
15116
15162
  requested,
@@ -15138,6 +15184,7 @@ function runSource(entry, options, runnerStartMs) {
15138
15184
  lastReported = JSON.stringify(proposedState);
15139
15185
  });
15140
15186
  reports = accepted.then(() => void 0, () => void 0);
15187
+ step = "observe";
15141
15188
  await accepted;
15142
15189
  if (aborted()) return;
15143
15190
  outcome = {
@@ -15147,6 +15194,7 @@ function runSource(entry, options, runnerStartMs) {
15147
15194
  }
15148
15195
  } catch (error) {
15149
15196
  if (aborted()) return;
15197
+ fail(step, error);
15150
15198
  if (error instanceof AdapterError && (error.kind === "auth" || error.kind === "config")) errorBlind = {
15151
15199
  reason: error.kind,
15152
15200
  message: error.message
@@ -15532,7 +15580,8 @@ function startAdapters(config, core, options = {}) {
15532
15580
  core,
15533
15581
  config,
15534
15582
  ...options.now === void 0 ? {} : { now: options.now },
15535
- ...options.oneShot === void 0 ? {} : { oneShot: options.oneShot }
15583
+ ...options.oneShot === void 0 ? {} : { oneShot: options.oneShot },
15584
+ onFailure: options.onPollFailure
15536
15585
  });
15537
15586
  return {
15538
15587
  firstAttempts: runner.firstAttempts,
@@ -16634,6 +16683,15 @@ function createBoardModules(ctxFor, options = {}) {
16634
16683
  warn: (message) => {
16635
16684
  adapterCtx.log.warn({ message }, "adapter warning");
16636
16685
  },
16686
+ onPollFailure: ({ source, step, kind, error, issues }) => {
16687
+ adapterCtx.log.warn({
16688
+ source,
16689
+ step,
16690
+ kind,
16691
+ err: error,
16692
+ ...issues === void 0 ? {} : { issues }
16693
+ }, "adapter poll failed");
16694
+ },
16637
16695
  onSpurNotice: async (notice) => {
16638
16696
  if (notice.kind === "restart") adapterCtx.log.info("spur restarted");
16639
16697
  else await core.notice(notice.kind, notice.value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spur-board",
3
- "version": "0.20.0",
3
+ "version": "0.20.2",
4
4
  "license": "MIT",
5
5
  "engines": {
6
6
  "node": "^24.12.0 || ^26.0.0"