spur-board 0.58.0 → 0.59.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.
package/dist/daemon.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { A as EVENT_KINDS, B as ringMarker, D as interactionRefSchema, E as inboundEventSchema, F as SESSION_STATUSES, G as actorIdSchema, H as ticketFactsSchema, I as isExternalAuthor, J as parseActorId, K as encodeActorId, L as parseRingMarker, M as PR_STATES, N as REVIEW_STATES, O as messageRefSchema, P as SESSION_HEALTH_STATES, Q as err, R as prFactsSchema, T as conversationRefSchema, U as JIRA_PROJECT_KEY, V as sessionFactsSchema, W as REF_NUMBER, X as taskKeySchema, Y as parseRef$1, Z as ERRORS, _ as buildCatalogSnapshot, a as requestHashOf, b as turnHandedSchema, c as tokenRequestStatusSchema, ct as configSchema, d as AdapterError, dt as COLUMN_IDS, f as batchSchema, ft as DISPATCHER_MODES, g as trackerSourceId, h as sourceCursorSchema, i as TOKEN_REQUEST_LIMITS, it as DEFAULT_LINK, j as eventPayloadSchemas, k as userIdSchema, l as BOARD_TICK_MS, lt as plainHttpAllowed, m as prSourceId, mt as TASK_HOLDS, n as doctorReportSchema, nt as successEnvelopeSchema, o as tokenRequestResultSchema, ot as DEFAULT_SLACK_COMMAND, p as entityRefSchema, pt as SLACK_BOT_SCOPES, q as errorCodeOf, s as tokenRequestSchema, st as RESERVED_PROJECT, t as assertNever, tt as ok, ut as requiredSecretKeys, v as roleCanUseOperation, w as SPACE_IDS, x as renderHttpProblem, y as OPERATIONS, z as prRefSchema } from "./chunk-mMiyA5ex.mjs";
1
+ import { A as EVENT_KINDS, B as ringMarker, D as interactionRefSchema, E as inboundEventSchema, F as SESSION_STATUSES, G as actorIdSchema, H as ticketFactsSchema, I as isExternalAuthor, J as parseActorId, K as encodeActorId, L as parseRingMarker, M as PR_STATES, N as REVIEW_STATES, O as messageRefSchema, P as SESSION_HEALTH_STATES, R as prFactsSchema, T as conversationRefSchema, U as JIRA_PROJECT_KEY, V as sessionFactsSchema, W as REF_NUMBER, X as ERRORS, Y as parseRef$1, Z as err, _ as buildCatalogSnapshot, a as requestHashOf, at as DEFAULT_SLACK_COMMAND, b as turnHandedSchema, c as tokenRequestStatusSchema, ct as plainHttpAllowed, d as AdapterError, dt as DISPATCHER_MODES, et as ok, f as batchSchema, ft as SLACK_BOT_SCOPES, g as trackerSourceId, h as sourceCursorSchema, i as TOKEN_REQUEST_LIMITS, j as eventPayloadSchemas, k as userIdSchema, l as BOARD_TICK_MS, lt as requiredSecretKeys, m as prSourceId, n as doctorReportSchema, o as tokenRequestResultSchema, ot as RESERVED_PROJECT, p as entityRefSchema, pt as TASK_HOLDS, q as errorCodeOf, rt as DEFAULT_LINK, s as tokenRequestSchema, st as configSchema, t as assertNever, tt as successEnvelopeSchema, ut as COLUMN_IDS, v as roleCanUseOperation, w as SPACE_IDS, x as renderHttpProblem, y as OPERATIONS, z as prRefSchema } from "./chunk-ZywNbxbW.mjs";
2
2
  import { t as apiBaseUrl } from "./chunk-DAMwWWmT.mjs";
3
3
  import { createRequire } from "node:module";
4
4
  import { createHash, randomUUID } from "node:crypto";
@@ -478,6 +478,18 @@ function acknowledgeComment(revision, requested) {
478
478
  }
479
479
  //#endregion
480
480
  //#region ../core/src/app/authorize.ts
481
+ /** The target of an operation on a board project as a whole: no task, block or links. */
482
+ function projectTarget(project) {
483
+ return {
484
+ kind: "found",
485
+ value: {
486
+ project,
487
+ task: null,
488
+ block: null,
489
+ workers: []
490
+ }
491
+ };
492
+ }
481
493
  /** True for the operator token and for a session of a Spur project with the `operator` role. */
482
494
  function isOperator(actor, config) {
483
495
  if (actor === "operator") return true;
@@ -8469,6 +8481,36 @@ async function dispatchOperation(deps, id, input, actor, key) {
8469
8481
  return operation.kind === "query" ? createQuery(deps)(handler, input, actor) : createApply(deps)(handler, input, actor, key);
8470
8482
  }
8471
8483
  //#endregion
8484
+ //#region ../core/src/app/subject.ts
8485
+ function taskSubject(task) {
8486
+ return {
8487
+ project: task.project,
8488
+ task,
8489
+ key: task.key
8490
+ };
8491
+ }
8492
+ function projectSubject(project) {
8493
+ return {
8494
+ project,
8495
+ task: null,
8496
+ key: `${project} (project)`
8497
+ };
8498
+ }
8499
+ /** The subject of a row with a task or a block; a block without a task has a project. */
8500
+ async function rowSubject(trx, row) {
8501
+ if (row.task_id !== null) return taskSubject(await trx.selectFrom("task").selectAll().where("id", "=", row.task_id).executeTakeFirstOrThrow());
8502
+ if (row.block_id === null) throw new Error("Row has neither a task nor a block");
8503
+ return blockSubject(trx, await trx.selectFrom("block").select(["task_id", "project"]).where("id", "=", row.block_id).executeTakeFirstOrThrow());
8504
+ }
8505
+ async function blockSubject(trx, block) {
8506
+ if (block.task_id !== null) return rowSubject(trx, {
8507
+ task_id: block.task_id,
8508
+ block_id: null
8509
+ });
8510
+ if (block.project === null) throw new Error("Block has neither a task nor a project");
8511
+ return projectSubject(block.project);
8512
+ }
8513
+ //#endregion
8472
8514
  //#region ../core/src/domain/fromYou.ts
8473
8515
  /** Every operator item kind; `ITEM_POLICY` must give each one a policy. */
8474
8516
  const ITEM_KINDS = [
@@ -8646,6 +8688,8 @@ function orderItems(items) {
8646
8688
  //#region ../core/src/app/item-raise.ts
8647
8689
  /** Display label of a feed line with no task; not part of the line's identity. */
8648
8690
  const TASKLESS_FEED_KEY = "spur-board";
8691
+ /** Link of an item with no task to open. */
8692
+ const TASKLESS_ITEM_URL = "https://github.com";
8649
8693
  async function journal$3(trx, item, kind, at, rule, actor = "system") {
8650
8694
  await emit(trx, kind, (item.task_id === null ? void 0 : await trx.selectFrom("task").select(["project"]).where("id", "=", item.task_id).executeTakeFirstOrThrow())?.project ?? "_board", item.task_id, at.toISOString(), {
8651
8695
  item: `I${String(item.id)}`,
@@ -8754,10 +8798,12 @@ const persona = z.strictObject({
8754
8798
  });
8755
8799
  const id = z.int().positive();
8756
8800
  const closePayload = z.strictObject({
8757
- key: taskKeySchema,
8801
+ /** The task key, or `<project> (project)` for a project-level block. */
8802
+ key: z.string().min(1),
8758
8803
  text: z.string(),
8759
8804
  persona,
8760
- taskId: id,
8805
+ /** Null for a project-level block. */
8806
+ taskId: id.nullable(),
8761
8807
  blockId: id,
8762
8808
  commentId: id.nullable(),
8763
8809
  targetConversation: conversationRefSchema.optional()
@@ -9254,22 +9300,23 @@ async function markCommentPending(trx, commentId) {
9254
9300
  * for every new block. The text is the comment's when given, else the block's reason.
9255
9301
  */
9256
9302
  async function enqueueConversationOpen(trx, input) {
9257
- const block = await trx.selectFrom("block").innerJoin("task", "task.id", "block.task_id").select([
9258
- "block.id",
9259
- "block.reason",
9260
- "block.author",
9261
- "block.addressee_role",
9262
- "task.id as taskId",
9263
- "task.key"
9264
- ]).where("block.id", "=", input.blockId).executeTakeFirstOrThrow();
9303
+ const block = await trx.selectFrom("block").select([
9304
+ "id",
9305
+ "reason",
9306
+ "author",
9307
+ "addressee_role",
9308
+ "task_id",
9309
+ "project"
9310
+ ]).where("id", "=", input.blockId).executeTakeFirstOrThrow();
9265
9311
  if (block.addressee_role !== "operator" || block.author === "operator") return null;
9312
+ const { key } = await blockSubject(trx, block);
9266
9313
  const comment = input.commentId === null ? void 0 : await trx.selectFrom("comment").select("text").where("id", "=", input.commentId).executeTakeFirstOrThrow();
9267
9314
  const payload = {
9268
- key: block.key,
9269
- title: `${block.key} ${block.reason}`,
9315
+ key,
9316
+ title: `${key} ${block.reason}`,
9270
9317
  text: comment?.text ?? block.reason,
9271
9318
  persona: input.persona,
9272
- taskId: block.taskId,
9319
+ taskId: block.task_id,
9273
9320
  blockId: block.id,
9274
9321
  commentId: input.commentId
9275
9322
  };
@@ -9289,13 +9336,12 @@ async function enqueueConversationOpen(trx, input) {
9289
9336
  * nothing when there is no such row or it is dead: a dead thread takes no more replies.
9290
9337
  */
9291
9338
  async function enqueueConversationReply(trx, input) {
9292
- const comment = await trx.selectFrom("comment").innerJoin("task", "task.id", "comment.task_id").select([
9293
- "comment.id",
9294
- "comment.text",
9295
- "comment.block_id",
9296
- "task.id as taskId",
9297
- "task.key"
9298
- ]).where("comment.id", "=", input.commentId).executeTakeFirstOrThrow();
9339
+ const comment = await trx.selectFrom("comment").select([
9340
+ "id",
9341
+ "text",
9342
+ "block_id",
9343
+ "task_id"
9344
+ ]).where("id", "=", input.commentId).executeTakeFirstOrThrow();
9299
9345
  const blockId = comment.block_id;
9300
9346
  if (blockId === null) return null;
9301
9347
  const tail = await trx.selectFrom("outbox").select(["id", "state"]).where("kind", "in", [
@@ -9305,10 +9351,10 @@ async function enqueueConversationReply(trx, input) {
9305
9351
  ]).where(sql`json_extract(payload, '$.blockId')`, "=", blockId).orderBy("id", "desc").limit(1).executeTakeFirst();
9306
9352
  if (tail === void 0 || tail.state === "dead") return null;
9307
9353
  const payload = {
9308
- key: comment.key,
9354
+ key: (await rowSubject(trx, comment)).key,
9309
9355
  text: comment.text,
9310
9356
  persona: input.persona,
9311
- taskId: comment.taskId,
9357
+ taskId: comment.task_id,
9312
9358
  blockId,
9313
9359
  commentId: comment.id
9314
9360
  };
@@ -9324,23 +9370,23 @@ async function enqueueConversationReply(trx, input) {
9324
9370
  }
9325
9371
  /** Queues a close for every registered thread and for an open whose result is still unknown. */
9326
9372
  async function enqueueConversationClose(trx, input) {
9327
- const block = await trx.selectFrom("block").innerJoin("task", "task.id", "block.task_id").select([
9328
- "task.id as taskId",
9329
- "task.key",
9330
- "block.id"
9331
- ]).where("block.id", "=", input.blockId).executeTakeFirstOrThrow();
9373
+ const block = await trx.selectFrom("block").select([
9374
+ "id",
9375
+ "task_id",
9376
+ "project"
9377
+ ]).where("id", "=", input.blockId).executeTakeFirstOrThrow();
9332
9378
  const tail = await trx.selectFrom("outbox").select(["id", "state"]).where("kind", "in", ["conversation.open", "conversation.reply"]).where(sql`json_extract(payload, '$.blockId')`, "=", input.blockId).orderBy("id", "desc").executeTakeFirst();
9333
9379
  const open = await trx.selectFrom("outbox").select(["id", "state"]).where("kind", "=", "conversation.open").where(sql`json_extract(payload, '$.blockId')`, "=", input.blockId).executeTakeFirst();
9334
9380
  const parent = tail?.state === "dead" ? null : tail?.id ?? null;
9335
9381
  const conversations = await trx.selectFrom("conversation").select("ref").where("block_id", "=", input.blockId).execute();
9336
9382
  const payload = {
9337
- key: block.key,
9383
+ key: (await blockSubject(trx, block)).key,
9338
9384
  text: `resolved: ${input.outcome === "archived" ? "task archived" : input.outcome} (${input.actor})`,
9339
9385
  persona: {
9340
9386
  name: "board",
9341
9387
  role: "board"
9342
9388
  },
9343
- taskId: block.taskId,
9389
+ taskId: block.task_id,
9344
9390
  blockId: block.id,
9345
9391
  commentId: null
9346
9392
  };
@@ -9376,13 +9422,18 @@ function orchestrator(config, task) {
9376
9422
  const name = config.projects.find((project) => project.id === task.project)?.columns.find((column) => column.id === task.column)?.orchestrator;
9377
9423
  return name === void 0 || name === "none" ? null : name;
9378
9424
  }
9379
- function authorRole(config, task, actor) {
9425
+ /**
9426
+ * The role an actor speaks for on a block's subject. On a project-level block, which has no
9427
+ * column, an agent is the curator or a worker.
9428
+ */
9429
+ function authorRole(config, subject, actor) {
9380
9430
  if (actor === "operator") return "operator";
9381
9431
  const parsed = parseActorId(actor);
9382
9432
  if (parsed?.actor === "agent" && config.projects.some((project) => project.spurProjects.some((spur) => spur.name === parsed.actorRef.project && spur.role === "operator"))) return "operator";
9383
9433
  if (parsed?.actor !== "agent") return "worker";
9384
- if (parsed.actorRef.project === orchestrator(config, task)) return `orchestrator:${task.column}`;
9385
- return parsed.actorRef.project === curatorProject(config, task.project) ? "curator" : "worker";
9434
+ const { task } = subject;
9435
+ if (task !== null && parsed.actorRef.project === orchestrator(config, task)) return `orchestrator:${task.column}`;
9436
+ return parsed.actorRef.project === curatorProject(config, subject.project) ? "curator" : "worker";
9386
9437
  }
9387
9438
  function personaRole(role) {
9388
9439
  const addressee = parseAddressee(role);
@@ -9394,7 +9445,7 @@ function personaRole(role) {
9394
9445
  default: return assertNever(addressee);
9395
9446
  }
9396
9447
  }
9397
- function personaFor(config, task, actor) {
9448
+ function personaFor(config, subject, actor) {
9398
9449
  if (actor === "system") return {
9399
9450
  name: "board",
9400
9451
  role: "board"
@@ -9407,21 +9458,21 @@ function personaFor(config, task, actor) {
9407
9458
  if (parsed?.actor !== "agent") throw new Error("Invalid actor");
9408
9459
  return {
9409
9460
  name: parsed.actorRef.project,
9410
- role: personaRole(authorRole(config, task, actor))
9461
+ role: personaRole(authorRole(config, subject, actor))
9411
9462
  };
9412
9463
  }
9413
- async function queueBlockOpen(trx, task, block, ctx, config) {
9414
- if (authorRole(config, task, block.author) === "operator") return;
9464
+ async function queueBlockOpen(trx, subject, block, ctx, config) {
9465
+ if (authorRole(config, subject, block.author) === "operator") return;
9415
9466
  const comments = await trx.selectFrom("comment").select(["id", "author"]).where("block_id", "=", block.id).orderBy("id").execute();
9416
9467
  if (await enqueueConversationOpen(trx, {
9417
9468
  blockId: block.id,
9418
9469
  commentId: comments[0]?.id ?? null,
9419
- persona: personaFor(config, task, comments[0]?.author ?? block.author),
9470
+ persona: personaFor(config, subject, comments[0]?.author ?? block.author),
9420
9471
  at: new Date(ctx.at)
9421
9472
  }) === null) return;
9422
9473
  for (const comment of comments.slice(1)) await enqueueConversationReply(trx, {
9423
9474
  commentId: comment.id,
9424
- persona: personaFor(config, task, comment.author),
9475
+ persona: personaFor(config, subject, comment.author),
9425
9476
  at: new Date(ctx.at)
9426
9477
  });
9427
9478
  }
@@ -9628,7 +9679,7 @@ function tokenCardBody(card, config) {
9628
9679
  //#region ../core/src/app/doorbells.ts
9629
9680
  const UNANSWERED = "unanswered";
9630
9681
  async function journal$2(trx, notification, kind, payload, at) {
9631
- await emit(trx, kind, (await trx.selectFrom("task").select("project").where("id", "=", notification.task_id).executeTakeFirstOrThrow()).project, notification.task_id, at.toISOString(), payload);
9682
+ await emit(trx, kind, await noticeProject(trx, notification), notification.task_id, at.toISOString(), payload);
9632
9683
  }
9633
9684
  /**
9634
9685
  * The one rule for who a board event rings. A doorbell means someone is waiting for your action,
@@ -9764,8 +9815,8 @@ function noticeBlockId(n) {
9764
9815
  return n.block_id;
9765
9816
  }
9766
9817
  /** What a notification is about, as the operator item names it. */
9767
- function noticeRef(n, task) {
9768
- return isTurnKind(n.kind) ? `turn T${String(n.turn_id)} on ${task.key}` : `block B${String(noticeBlockId(n))} on ${task.key}`;
9818
+ function noticeRef(n, subject) {
9819
+ return isTurnKind(n.kind) ? `turn T${String(n.turn_id)} on ${subject.key}` : `block B${String(noticeBlockId(n))} on ${subject.key}`;
9769
9820
  }
9770
9821
  /** Register a one-ring notice that a block its addressee held was closed by someone else. */
9771
9822
  async function registerRemovedNotification(trx, block, at) {
@@ -9862,7 +9913,7 @@ async function moveBlockToAddressee(trx, input) {
9862
9913
  kind: "block_escalated",
9863
9914
  block: updated
9864
9915
  }, new Date(ctx.at));
9865
- await queueBlockOpen(trx, task, updated, ctx, config);
9916
+ await queueBlockOpen(trx, taskSubject(task), updated, ctx, config);
9866
9917
  return updated;
9867
9918
  }
9868
9919
  async function sessionById(trx, id) {
@@ -9918,8 +9969,9 @@ async function bestProjectSession(trx, project) {
9918
9969
  async function curatorSession(trx, config, projectId) {
9919
9970
  return bestProjectSession(trx, curatorProject(config, projectId));
9920
9971
  }
9921
- /** The newest live session linked to a task in a role. */
9972
+ /** The newest live session linked to a task in a role; a project-level notice has no links. */
9922
9973
  async function linkedSession(trx, taskId, role) {
9974
+ if (taskId === null) return void 0;
9923
9975
  const links = await trx.selectFrom("link").select("session_id").where("task_id", "=", taskId).where("entity", "=", "session").where("role", "=", role).where("banned", "=", 0).orderBy("created_at", "desc").execute();
9924
9976
  for (const link of links) {
9925
9977
  if (link.session_id === null) continue;
@@ -9927,8 +9979,12 @@ async function linkedSession(trx, taskId, role) {
9927
9979
  if (session !== void 0) return session;
9928
9980
  }
9929
9981
  }
9930
- async function taskProject(trx, taskId) {
9931
- return (await trx.selectFrom("task").select("project").where("id", "=", taskId).executeTakeFirstOrThrow()).project;
9982
+ /** The board project of a notification: its task's, else its project-level block's. */
9983
+ async function noticeProject(trx, n) {
9984
+ return (await rowSubject(trx, {
9985
+ task_id: n.task_id,
9986
+ block_id: n.block_id ?? null
9987
+ })).project;
9932
9988
  }
9933
9989
  /** The Spur project orchestrating a column; null when no agent project does. */
9934
9990
  function columnOrchestratorProject(config, projectId, column) {
@@ -9943,11 +9999,11 @@ async function selectNotificationRecipient(trx, n, config) {
9943
9999
  if (n.kind === "turn_reminder") return n.author_session === null ? void 0 : liveSession(trx, n.author_session);
9944
10000
  const addressee = parseAddressee(n.role);
9945
10001
  switch (addressee.kind) {
9946
- case "curator": return curatorSession(trx, config, await taskProject(trx, n.task_id));
10002
+ case "curator": return curatorSession(trx, config, await noticeProject(trx, n));
9947
10003
  case "orchestrator": {
9948
10004
  const author = n.author_session === null ? void 0 : await sessionById(trx, n.author_session);
9949
10005
  if (author !== void 0 && liveInProject(author)) return author;
9950
- return await linkedSession(trx, n.task_id, "orchestrator") ?? bestProjectSession(trx, columnOrchestratorProject(config, await taskProject(trx, n.task_id), addressee.column));
10006
+ return await linkedSession(trx, n.task_id, "orchestrator") ?? bestProjectSession(trx, columnOrchestratorProject(config, await noticeProject(trx, n), addressee.column));
9951
10007
  }
9952
10008
  case "worker":
9953
10009
  case "operator": {
@@ -9983,12 +10039,13 @@ async function restartNotification(trx, sourceKey, at) {
9983
10039
  * session to name ends alone; anything else raises the operator item, whose Retry restarts the
9984
10040
  * series (`restartNotification`). Every end journals `doorbell_undelivered`.
9985
10041
  */
9986
- async function endNotification(trx, n, task, end) {
10042
+ async function endNotification(trx, n, subject, end) {
9987
10043
  const { session, reason, at, config } = end;
9988
10044
  await trx.updateTable("notification").set({ exhausted_at: at.toISOString() }).where("id", "=", n.id).execute();
9989
10045
  if (n.rings === 0) await releaseAfterCourseNote(trx, n, at);
9990
10046
  const addressee = parseAddressee(n.role);
9991
- if (n.kind === "block" && (addressee.kind === "curator" || session === null)) await moveBlockToAddressee(trx, {
10047
+ const { task } = subject;
10048
+ if (n.kind === "block" && task !== null && (addressee.kind === "curator" || session === null)) await moveBlockToAddressee(trx, {
9992
10049
  task,
9993
10050
  block: await trx.selectFrom("block").selectAll().where("id", "=", noticeBlockId(n)).executeTakeFirstOrThrow(),
9994
10051
  to: "operator",
@@ -10000,15 +10057,15 @@ async function endNotification(trx, n, task, end) {
10000
10057
  config
10001
10058
  });
10002
10059
  else if (n.kind !== "removed" && n.kind !== "turn_reminder" && session !== null) {
10003
- const blockRef = noticeRef(n, task);
10060
+ const blockRef = noticeRef(n, subject);
10004
10061
  const answer = `Answer to ${blockRef} was not`;
10005
10062
  const holder = `${addressee.kind} ${session}`;
10006
10063
  await raiseItem(trx, {
10007
10064
  itemKey: exhaustedItemKey(n),
10008
10065
  kind: exhaustedItemKind(n),
10009
10066
  text: reason === UNANSWERED ? n.kind === "answer" ? `${answer} read` : `${holder} is not reading ${blockRef}` : n.kind === "answer" ? `${answer} delivered: ${reason}` : `${holder} did not get ${blockRef}: ${reason}`,
10010
- url: taskUrl(task, config),
10011
- taskId: task.id,
10067
+ url: task === null ? TASKLESS_ITEM_URL : taskUrl(task, config),
10068
+ taskId: task?.id ?? null,
10012
10069
  blockId: n.block_id,
10013
10070
  at
10014
10071
  });
@@ -10046,7 +10103,7 @@ async function ringUndelivered(trx, input) {
10046
10103
  }
10047
10104
  const n = await trx.selectFrom("notification").selectAll().where("id", "=", input.notificationId).executeTakeFirst();
10048
10105
  if (n === void 0 || n.acked_at !== null || n.exhausted_at !== null || n.last_outbox_id !== input.outboxId) return;
10049
- await endNotification(trx, n, await trx.selectFrom("task").selectAll().where("id", "=", n.task_id).executeTakeFirstOrThrow(), {
10106
+ await endNotification(trx, n, await rowSubject(trx, n), {
10050
10107
  session: input.session,
10051
10108
  reason,
10052
10109
  at,
@@ -10056,31 +10113,32 @@ async function ringUndelivered(trx, input) {
10056
10113
  /**
10057
10114
  * Why a notification with no recipient ends now, or null when it stays due for a next session. A
10058
10115
  * curator block, and an orchestrator block of a course check, end with `session: null`, which
10059
- * `endNotification` turns into a move to the operator.
10116
+ * `endNotification` turns into a move to the operator. A worker's answer on a project-level block
10117
+ * goes only to the session that asked.
10060
10118
  */
10061
10119
  async function noRecipientEnd(trx, n) {
10062
- if (n.kind === "removed" || n.kind === "turn_reminder" || n.kind === "answer" && n.role === "operator") return "no live addressee";
10120
+ if (n.kind === "removed" || n.kind === "turn_reminder" || n.kind === "answer" && n.role === "operator" || n.kind === "answer" && n.role === "worker" && n.task_id === null) return "no live addressee";
10063
10121
  if (n.kind !== "block") return null;
10064
10122
  if (n.role === "curator") return "no live curator session";
10065
10123
  if (parseAddressee(n.role).kind !== "orchestrator") return null;
10066
10124
  return await trx.selectFrom("operator_item").select("id").where("kind", "=", "course_check").where("block_id", "=", noticeBlockId(n)).executeTakeFirst() === void 0 ? null : "no live orchestrator session";
10067
10125
  }
10068
10126
  /** A ring never carries comment or reason text: only where to look. */
10069
- async function ringText(trx, n, task, ring) {
10127
+ async function ringText(trx, n, { key }, ring) {
10070
10128
  const suffix = ringMarker(n.id, ring);
10071
- if (n.kind === "turn") return `${task.key} turn T${String(n.turn_id)}: check board inbox ${suffix}`;
10072
- if (n.kind === "turn_reminder") return `${task.key} turn T${String(n.turn_id)} is still open: check board inbox ${suffix}`;
10129
+ if (n.kind === "turn") return `${key} turn T${String(n.turn_id)}: check board inbox ${suffix}`;
10130
+ if (n.kind === "turn_reminder") return `${key} turn T${String(n.turn_id)} is still open: check board inbox ${suffix}`;
10073
10131
  const blockId = noticeBlockId(n);
10074
- if (n.kind !== "removed") return `${task.key} block B${String(blockId)}: check board inbox ${suffix}`;
10132
+ if (n.kind !== "removed") return `${key} block B${String(blockId)}: check board inbox ${suffix}`;
10075
10133
  const block = await trx.selectFrom("block").select("unblocked_by").where("id", "=", blockId).executeTakeFirstOrThrow();
10076
- return `${task.key} block B${String(blockId)} was removed by ${block.unblocked_by ?? "system"}; see board task ${task.key} ${suffix}`;
10134
+ return `${key} block B${String(blockId)} was removed by ${block.unblocked_by ?? "system"}; see board task ${key} ${suffix}`;
10077
10135
  }
10078
10136
  /**
10079
- * Notifications reconcile looks at: unacked, not exhausted, due, on a task not archived, and not
10080
- * held by a course check (`courseHold`).
10137
+ * Notifications reconcile looks at: unacked, not exhausted, due, on a task not archived or on a
10138
+ * project-level block, and not held by a course check (`courseHold`).
10081
10139
  */
10082
10140
  function dueNotifications(db, at) {
10083
- return db.selectFrom("notification").innerJoin("task", "task.id", "notification.task_id").where("task.membership", "!=", "archived").where("notification.acked_at", "is", null).where("notification.exhausted_at", "is", null).where("notification.next_at", "<=", at.toISOString()).where((eb) => eb.not(courseHold(eb)));
10141
+ return db.selectFrom("notification").leftJoin("task", "task.id", "notification.task_id").where((eb) => eb.or([eb("task.id", "is", null), eb("task.membership", "!=", "archived")])).where("notification.acked_at", "is", null).where("notification.exhausted_at", "is", null).where("notification.next_at", "<=", at.toISOString()).where((eb) => eb.not(courseHold(eb)));
10084
10142
  }
10085
10143
  /**
10086
10144
  * Reconciles due notifications in their own transaction. With none due it opens no transaction;
@@ -10099,7 +10157,7 @@ async function reconcileDoorbells(trx, at, config) {
10099
10157
  const keepsRings = resolvedByProject(n.role);
10100
10158
  if (session === void 0) {
10101
10159
  const reason = await noRecipientEnd(trx, n);
10102
- if (reason !== null) await endNotification(trx, n, await trx.selectFrom("task").selectAll().where("id", "=", n.task_id).executeTakeFirstOrThrow(), {
10160
+ if (reason !== null) await endNotification(trx, n, await rowSubject(trx, n), {
10103
10161
  session: null,
10104
10162
  reason,
10105
10163
  at,
@@ -10132,9 +10190,9 @@ async function reconcileDoorbells(trx, at, config) {
10132
10190
  const row = await trx.selectFrom("outbox").select("state").where("id", "=", lastOutbox).executeTakeFirst();
10133
10191
  if (row?.state === "pending" || row?.state === "in_flight") continue;
10134
10192
  }
10135
- const task = await trx.selectFrom("task").selectAll().where("id", "=", n.task_id).executeTakeFirstOrThrow();
10193
+ const subject = await rowSubject(trx, n);
10136
10194
  if (rings >= 3) {
10137
- await endNotification(trx, n, task, {
10195
+ await endNotification(trx, n, subject, {
10138
10196
  session: session.id,
10139
10197
  reason: UNANSWERED,
10140
10198
  at,
@@ -10145,7 +10203,7 @@ async function reconcileDoorbells(trx, at, config) {
10145
10203
  if (rings > 0 && earlierRingQueued(session, n.id, rings) && !await spurBlind$1(trx)) {
10146
10204
  const pauses = ringPauses(config);
10147
10205
  const deadline = Date.parse(n.next_at) + pauses.slice(rings).reduce((sum, pause) => sum + pause, 0);
10148
- if (at.getTime() >= deadline) await endNotification(trx, n, task, {
10206
+ if (at.getTime() >= deadline) await endNotification(trx, n, subject, {
10149
10207
  session: session.id,
10150
10208
  reason: UNANSWERED,
10151
10209
  at,
@@ -10154,9 +10212,9 @@ async function reconcileDoorbells(trx, at, config) {
10154
10212
  continue;
10155
10213
  }
10156
10214
  const raised = await trx.selectFrom("operator_item").select((eb) => eb.fn.max("raise_no").as("series")).where("item_key", "=", exhaustedItemKey(n)).executeTakeFirst();
10157
- const archived = await trx.selectFrom("event").select((eb) => eb.fn.countAll().as("count")).where("task_id", "=", n.task_id).where("kind", "=", "task_archived").executeTakeFirst();
10215
+ const archived = n.task_id === null ? void 0 : await trx.selectFrom("event").select((eb) => eb.fn.countAll().as("count")).where("task_id", "=", n.task_id).where("kind", "=", "task_archived").executeTakeFirst();
10158
10216
  const ring = rings + 1;
10159
- const text = await ringText(trx, n, task, ring);
10217
+ const text = await ringText(trx, n, subject, ring);
10160
10218
  const outboxId = await enqueueDoorbell(trx, {
10161
10219
  notificationId: n.id,
10162
10220
  ring,
@@ -10226,7 +10284,7 @@ async function recordRingDelivery(trx, notificationId, ring, session, at, config
10226
10284
  * without a note.
10227
10285
  */
10228
10286
  async function releaseAfterCourseNote(trx, n, at) {
10229
- if (await trx.selectFrom("notification").select("id").where("id", "=", n.id).where("role", "=", "worker").where("kind", "=", "answer").where((eb) => eb("block_id", "in", courseBlocks(eb))).executeTakeFirst() === void 0) return;
10287
+ if (await trx.selectFrom("notification").select("id").where("id", "=", n.id).where("role", "=", "worker").where("kind", "=", "answer").where((eb) => eb("block_id", "in", courseBlocks(eb))).executeTakeFirst() === void 0 || n.task_id === null) return;
10230
10288
  await promoteWorkerDoorbells(trx, n.task_id, at, n.id);
10231
10289
  }
10232
10290
  /** True while the Spur source is blind: its stored session facts may no longer hold. */
@@ -10281,14 +10339,15 @@ async function notificationAddressedTo(trx, n, actor, config) {
10281
10339
  const { project, session } = parsed.actorRef;
10282
10340
  const addressee = parseAddressee(n.role);
10283
10341
  switch (addressee.kind) {
10284
- case "curator": return project === curatorProject(config, await taskProject(trx, n.task_id));
10285
- case "orchestrator": return project === columnOrchestratorProject(config, await taskProject(trx, n.task_id), addressee.column) || await hasLiveLink(trx, n.task_id, "orchestrator", session);
10342
+ case "curator": return project === curatorProject(config, await noticeProject(trx, n));
10343
+ case "orchestrator": return project === columnOrchestratorProject(config, await noticeProject(trx, n), addressee.column) || await hasLiveLink(trx, n.task_id, "orchestrator", session);
10286
10344
  case "operator": return session === n.author_session;
10287
10345
  case "worker": return workerNotificationAddressedTo(trx, n, session, config);
10288
10346
  default: return assertNever(addressee);
10289
10347
  }
10290
10348
  }
10291
10349
  async function sessionLink(trx, taskId, role, session) {
10350
+ if (taskId === null) return void 0;
10292
10351
  return trx.selectFrom("link").select("id").where("task_id", "=", taskId).where("entity", "=", "session").where("role", "=", role).where("banned", "=", 0).where("session_id", "=", session).executeTakeFirst();
10293
10352
  }
10294
10353
  async function hasLiveLink(trx, taskId, role, session) {
@@ -11345,9 +11404,11 @@ function found(value) {
11345
11404
  function active(task) {
11346
11405
  if (task.membership === "archived") throw new CommandError("task_archived", "Add the task again before changing it.");
11347
11406
  }
11348
- async function authorizationTarget(trx, task, block) {
11349
- if (task === void 0) return { kind: "missing" };
11350
- const workers = await trx.selectFrom("link").select([
11407
+ /** The authorization target of a subject; a project-level block has no task and no links. */
11408
+ async function subjectTarget(trx, subject, block) {
11409
+ if (subject === void 0) return { kind: "missing" };
11410
+ const { task } = subject;
11411
+ const workers = task === null ? [] : await trx.selectFrom("link").select([
11351
11412
  "session_id",
11352
11413
  "role",
11353
11414
  "banned"
@@ -11355,8 +11416,8 @@ async function authorizationTarget(trx, task, block) {
11355
11416
  return {
11356
11417
  kind: "found",
11357
11418
  value: {
11358
- project: task.project,
11359
- task: {
11419
+ project: subject.project,
11420
+ task: task === null ? null : {
11360
11421
  id: task.id,
11361
11422
  column: task.column
11362
11423
  },
@@ -11369,12 +11430,15 @@ async function authorizationTarget(trx, task, block) {
11369
11430
  }
11370
11431
  };
11371
11432
  }
11433
+ async function authorizationTarget(trx, task, block) {
11434
+ return subjectTarget(trx, task === void 0 ? void 0 : taskSubject(task), block);
11435
+ }
11372
11436
  async function taskTarget(trx, key) {
11373
11437
  return [await authorizationTarget(trx, await taskByKey$1(trx, key))];
11374
11438
  }
11375
11439
  async function blockTarget(trx, id) {
11376
11440
  const block = await blockById(trx, id);
11377
- return [await authorizationTarget(trx, block === void 0 ? void 0 : await taskById$1(trx, block.task_id), block)];
11441
+ return [await subjectTarget(trx, block === void 0 ? void 0 : await blockSubject(trx, block), block)];
11378
11442
  }
11379
11443
  async function turnTarget(trx, id) {
11380
11444
  const turn = await trx.selectFrom("turn").select("task_id").where("id", "=", idNumber(id)).executeTakeFirst();
@@ -11390,7 +11454,7 @@ async function turnTarget(trx, id) {
11390
11454
  async function commentTarget(trx, id) {
11391
11455
  const comment = await commentById(trx, id);
11392
11456
  return {
11393
- ...await authorizationTarget(trx, comment === void 0 ? void 0 : await taskById$1(trx, comment.task_id)),
11457
+ ...await subjectTarget(trx, comment === void 0 ? void 0 : await rowSubject(trx, comment)),
11394
11458
  id
11395
11459
  };
11396
11460
  }
@@ -11404,10 +11468,11 @@ function current$1(block) {
11404
11468
  addressee: block.addressee_role
11405
11469
  };
11406
11470
  }
11407
- function blockView(block, task) {
11471
+ function blockView(block, subject) {
11408
11472
  return {
11409
11473
  id: blockId$1(block.id),
11410
- task: task.key,
11474
+ task: subject.task?.key ?? null,
11475
+ ...subject.task === null ? { project: subject.project } : {},
11411
11476
  type: block.type,
11412
11477
  state: block.state,
11413
11478
  author: block.author,
@@ -11420,10 +11485,10 @@ function blockView(block, task) {
11420
11485
  unblockedAt: block.unblocked_at
11421
11486
  };
11422
11487
  }
11423
- function commentView(comment, task) {
11488
+ function commentView(comment, subject) {
11424
11489
  return {
11425
11490
  id: commentId(comment.id),
11426
- task: task.key,
11491
+ task: subject.task?.key ?? null,
11427
11492
  block: comment.block_id === null ? null : blockId$1(comment.block_id),
11428
11493
  author: comment.author,
11429
11494
  at: comment.created_at,
@@ -11433,8 +11498,8 @@ function commentView(comment, task) {
11433
11498
  untrusted: true
11434
11499
  };
11435
11500
  }
11436
- async function journal$1(trx, task, ctx, kind, payload) {
11437
- await emit(trx, kind, task.project, task.id, ctx.at, payload, ctx.actor);
11501
+ async function journal$1(trx, subject, ctx, kind, payload) {
11502
+ await emit(trx, kind, subject.project, subject.task?.id ?? null, ctx.at, payload, ctx.actor);
11438
11503
  }
11439
11504
  async function hasWorker(trx, task) {
11440
11505
  return await trx.selectFrom("link").select("id").where("task_id", "=", task.id).where("entity", "=", "session").where("role", "=", "worker").where("banned", "=", 0).executeTakeFirst() !== void 0;
@@ -11444,7 +11509,7 @@ async function routeBlock(trx, task, block, config) {
11444
11509
  return blockAddressee({
11445
11510
  type: block.type,
11446
11511
  author: block.author,
11447
- authorRole: authorRole(config, task, block.author),
11512
+ authorRole: authorRole(config, taskSubject(task), block.author),
11448
11513
  column: task.column,
11449
11514
  orchestrator: orchestrator(config, task),
11450
11515
  curator: curatorProject(config, task.project),
@@ -11464,9 +11529,9 @@ function wroteBlock(config, actor, target) {
11464
11529
  }).status === "allowed";
11465
11530
  }
11466
11531
  /** Inserts a comment row and journals `comment_added`; it rings and posts nothing. */
11467
- async function insertComment(trx, task, blockId, text, ctx, addressee, source, forwardedFrom) {
11532
+ async function insertComment(trx, subject, blockId, text, ctx, addressee, source, forwardedFrom) {
11468
11533
  const row = await trx.insertInto("comment").values({
11469
- task_id: task.id,
11534
+ task_id: subject.task?.id ?? null,
11470
11535
  block_id: blockId,
11471
11536
  author: ctx.actor,
11472
11537
  text,
@@ -11477,38 +11542,41 @@ async function insertComment(trx, task, blockId, text, ctx, addressee, source, f
11477
11542
  created_at: ctx.at,
11478
11543
  delivery_state: null
11479
11544
  }).returningAll().executeTakeFirstOrThrow();
11480
- await journal$1(trx, task, ctx, "comment_added", {
11545
+ await journal$1(trx, subject, ctx, "comment_added", {
11481
11546
  comment: commentId(row.id),
11482
11547
  addressee,
11483
11548
  ...forwardedFrom === void 0 ? {} : { forwardedFrom: commentId(forwardedFrom) }
11484
11549
  });
11485
11550
  return row;
11486
11551
  }
11487
- async function addComment(trx, task, block, text, ctx, addressee, config, options = {}) {
11552
+ async function addComment(trx, subject, block, text, ctx, addressee, config, options = {}) {
11488
11553
  const { source, ringsAuthor = true } = options;
11489
- const row = await insertComment(trx, task, block?.id ?? null, text, ctx, addressee, source);
11554
+ const row = await insertComment(trx, subject, block?.id ?? null, text, ctx, addressee, source);
11490
11555
  if (ringsAuthor) await applyDoorbellPolicy(trx, block === void 0 ? { kind: "task_comment" } : {
11491
11556
  kind: "answer",
11492
11557
  block,
11493
11558
  commentId: row.id,
11494
11559
  revision: row.revision,
11495
- authorRole: authorRole(config, task, block.author),
11560
+ authorRole: authorRole(config, subject, block.author),
11496
11561
  actor: ctx.actor
11497
11562
  }, new Date(ctx.at));
11498
11563
  if (block !== void 0 && source === void 0) await enqueueConversationReply(trx, {
11499
11564
  commentId: row.id,
11500
- persona: personaFor(config, task, ctx.actor),
11565
+ persona: personaFor(config, subject, ctx.actor),
11501
11566
  at: new Date(ctx.at)
11502
11567
  });
11503
11568
  return row;
11504
11569
  }
11505
- async function openBlock(trx, task, type, reason, ctx, config, addressee) {
11506
- addressee ??= await routeBlock(trx, task, {
11570
+ /** Opens a block; a project-level block, which has no task to route by, goes to the operator. */
11571
+ async function openBlock(trx, subject, type, reason, ctx, config, addressee) {
11572
+ const { task } = subject;
11573
+ addressee ??= task === null ? "operator" : await routeBlock(trx, task, {
11507
11574
  type,
11508
11575
  author: ctx.actor
11509
11576
  }, config);
11510
11577
  const row = await trx.insertInto("block").values({
11511
- task_id: task.id,
11578
+ task_id: task?.id ?? null,
11579
+ project: task === null ? subject.project : null,
11512
11580
  type,
11513
11581
  reason,
11514
11582
  author: ctx.actor,
@@ -11522,7 +11590,7 @@ async function openBlock(trx, task, type, reason, ctx, config, addressee) {
11522
11590
  unblocked_at: null,
11523
11591
  created_at: ctx.at
11524
11592
  }).returningAll().executeTakeFirstOrThrow();
11525
- await journal$1(trx, task, ctx, "block_opened", {
11593
+ await journal$1(trx, subject, ctx, "block_opened", {
11526
11594
  block: blockId$1(row.id),
11527
11595
  type,
11528
11596
  addressee
@@ -11530,7 +11598,7 @@ async function openBlock(trx, task, type, reason, ctx, config, addressee) {
11530
11598
  await applyDoorbellPolicy(trx, {
11531
11599
  kind: "block_opened",
11532
11600
  block: row,
11533
- authorRole: authorRole(config, task, ctx.actor)
11601
+ authorRole: authorRole(config, subject, ctx.actor)
11534
11602
  }, new Date(ctx.at));
11535
11603
  return row;
11536
11604
  }
@@ -11545,8 +11613,9 @@ async function openSystemBlock(trx, input) {
11545
11613
  actor: "system",
11546
11614
  at
11547
11615
  };
11548
- const block = await openBlock(trx, task, "needs_input", reason, ctx, config, addressee);
11549
- await queueBlockOpen(trx, task, block, ctx, config);
11616
+ const subject = taskSubject(task);
11617
+ const block = await openBlock(trx, subject, "needs_input", reason, ctx, config, addressee);
11618
+ await queueBlockOpen(trx, subject, block, ctx, config);
11550
11619
  return block;
11551
11620
  }
11552
11621
  /**
@@ -11560,7 +11629,7 @@ async function forwardCourseNote(trx, task, block, ctx, reason, given) {
11560
11629
  const answer = reason !== null ? void 0 : given ?? await trx.selectFrom("comment").select(["id", "text"]).where("block_id", "=", block.id).where("author", "=", ctx.actor).orderBy("id", "desc").executeTakeFirst();
11561
11630
  const text = reason ?? answer?.text;
11562
11631
  if (text === void 0) return void 0;
11563
- const copy = await insertComment(trx, task, null, text, ctx, "worker", void 0, answer?.id);
11632
+ const copy = await insertComment(trx, taskSubject(task), null, text, ctx, "worker", void 0, answer?.id);
11564
11633
  await applyDoorbellPolicy(trx, {
11565
11634
  kind: "course_note",
11566
11635
  block,
@@ -11578,13 +11647,13 @@ async function forwardedCopies(trx, id) {
11578
11647
  return trx.selectFrom("comment").selectAll().where("id", "in", ids).execute();
11579
11648
  }
11580
11649
  /** Writes an edit to one comment by the acknowledgment rule, with its `comment_edited`. */
11581
- async function reviseComment(trx, task, comment, text, ctx) {
11650
+ async function reviseComment(trx, subject, comment, text, ctx) {
11582
11651
  const next = editComment(comment.revision, comment.acked_revision);
11583
11652
  const updated = await trx.updateTable("comment").set({
11584
11653
  text,
11585
11654
  revision: next.revision
11586
11655
  }).where("id", "=", comment.id).returningAll().executeTakeFirstOrThrow();
11587
- await journal$1(trx, task, ctx, "comment_edited", {
11656
+ await journal$1(trx, subject, ctx, "comment_edited", {
11588
11657
  comment: commentId(comment.id),
11589
11658
  revision: next.revision,
11590
11659
  clarification: next.clarification
@@ -11602,7 +11671,7 @@ async function reviseComment(trx, task, comment, text, ctx) {
11602
11671
  * first, before the rings the check held; `answer` names the answer when the closing actor also
11603
11672
  * left a note after it.
11604
11673
  */
11605
- async function closeOpenBlock(trx, task, block, ctx, outcome, outcomeReason, answer) {
11674
+ async function closeOpenBlock(trx, subject, block, ctx, outcome, outcomeReason, answer) {
11606
11675
  const updated = await trx.updateTable("block").set({
11607
11676
  state: "unblocked",
11608
11677
  outcome,
@@ -11610,7 +11679,7 @@ async function closeOpenBlock(trx, task, block, ctx, outcome, outcomeReason, ans
11610
11679
  unblocked_by: ctx.actor,
11611
11680
  unblocked_at: ctx.at
11612
11681
  }).where("id", "=", block.id).returningAll().executeTakeFirstOrThrow();
11613
- await journal$1(trx, task, ctx, "block_unblocked", {
11682
+ await journal$1(trx, subject, ctx, "block_unblocked", {
11614
11683
  block: blockId$1(block.id),
11615
11684
  outcome,
11616
11685
  ...outcomeReason === null ? {} : { reason: outcomeReason }
@@ -11623,7 +11692,7 @@ async function closeOpenBlock(trx, task, block, ctx, outcome, outcomeReason, ans
11623
11692
  });
11624
11693
  await closeBlockNotification(trx, block.id, new Date(ctx.at), ctx.actor);
11625
11694
  const items = await trx.selectFrom("operator_item").selectAll().where("kind", "in", ["post_blocked", ...BLOCK_ITEM_KINDS]).where("block_id", "=", block.id).where("cleared_at", "is", null).execute();
11626
- if (ctx.actor !== "system" && (outcome === "done" || outcome === "reason") && items.some((item) => item.kind === "course_check")) await forwardCourseNote(trx, task, block, ctx, outcome === "reason" ? outcomeReason : null, answer);
11695
+ if (subject.task !== null && ctx.actor !== "system" && (outcome === "done" || outcome === "reason") && items.some((item) => item.kind === "course_check")) await forwardCourseNote(trx, subject.task, block, ctx, outcome === "reason" ? outcomeReason : null, answer);
11627
11696
  for (const item of items) if (item.kind === "post_blocked") await clearItem(trx, item.id, new Date(ctx.at), ctx.actor);
11628
11697
  else if (ctx.actor !== "system" && outcome !== "archived" && isItemKind(item.kind) && ITEM_POLICY[item.kind].reconciled) await dismissItem(trx, item, ctx.actor, new Date(ctx.at));
11629
11698
  else await clearItem(trx, item.id, new Date(ctx.at), ctx.actor);
@@ -11633,45 +11702,45 @@ async function closeOpenBlock(trx, task, block, ctx, outcome, outcomeReason, ans
11633
11702
  async function closeBlockIfOpen(trx, blockId, ctx, outcome, outcomeReason) {
11634
11703
  const block = await trx.selectFrom("block").selectAll().where("id", "=", blockId).where("state", "=", "open").executeTakeFirst();
11635
11704
  if (block === void 0) return;
11636
- await closeOpenBlock(trx, await trx.selectFrom("task").selectAll().where("id", "=", block.task_id).executeTakeFirstOrThrow(), block, ctx, outcome, outcomeReason);
11705
+ await closeOpenBlock(trx, await blockSubject(trx, block), block, ctx, outcome, outcomeReason);
11637
11706
  }
11638
11707
  /** Applies an operator message to the block selected by its conversation, without echoing it. */
11639
11708
  async function recordInboundBlockMessage(trx, input) {
11640
11709
  const block = await trx.selectFrom("block").selectAll().where("id", "=", input.blockId).executeTakeFirstOrThrow();
11641
- const task = await taskById$1(trx, block.task_id);
11710
+ const subject = await blockSubject(trx, block);
11642
11711
  const ctx = {
11643
11712
  actor: "operator",
11644
11713
  at: input.at
11645
11714
  };
11646
11715
  const answered = block.state === "open";
11647
11716
  if (answered) {
11648
- active(task);
11717
+ if (subject.task !== null) active(subject.task);
11649
11718
  rule(() => {
11650
11719
  answerBlock(current$1(block));
11651
11720
  });
11652
11721
  }
11653
11722
  return {
11654
- comment: await addComment(trx, task, block, input.text, ctx, authorRole(input.config, task, block.author), input.config, { source: {
11723
+ comment: await addComment(trx, subject, block, input.text, ctx, authorRole(input.config, subject, block.author), input.config, { source: {
11655
11724
  conversationRef: input.conversationRef,
11656
11725
  messageRef: input.messageRef
11657
11726
  } }),
11658
- block: answered ? await closeOpenBlock(trx, task, block, ctx, "done", null) : block,
11659
- task,
11727
+ block: answered ? await closeOpenBlock(trx, subject, block, ctx, "done", null) : block,
11728
+ subject,
11660
11729
  answered,
11661
- authorRole: authorRole(input.config, task, block.author)
11730
+ authorRole: authorRole(input.config, subject, block.author)
11662
11731
  };
11663
11732
  }
11664
11733
  /** Uses the same revision and acknowledgment rule as the catalog comment.edit mutation. */
11665
11734
  async function editInboundComment(trx, input) {
11666
- const task = await taskById$1(trx, input.comment.task_id);
11667
- active(task);
11735
+ const subject = await rowSubject(trx, input.comment);
11736
+ if (subject.task !== null) active(subject.task);
11668
11737
  if (input.comment.author !== input.actor) throw new CommandError("forbidden", "Only the comment author can edit it.");
11669
11738
  const ctx = {
11670
11739
  actor: input.actor,
11671
11740
  at: input.at
11672
11741
  };
11673
- const { updated, clarification } = await reviseComment(trx, task, input.comment, input.text, ctx);
11674
- for (const copy of await forwardedCopies(trx, input.comment.id)) await reviseComment(trx, task, copy, input.text, ctx);
11742
+ const { updated, clarification } = await reviseComment(trx, subject, input.comment, input.text, ctx);
11743
+ for (const copy of await forwardedCopies(trx, input.comment.id)) await reviseComment(trx, subject, copy, input.text, ctx);
11675
11744
  const answered = await trx.selectFrom("notification").select("id").where("kind", "=", "answer").where("comment_id", "=", updated.id).executeTakeFirst();
11676
11745
  if (clarification && updated.block_id !== null && answered !== void 0) {
11677
11746
  const block = await trx.selectFrom("block").selectAll().where("id", "=", updated.block_id).executeTakeFirstOrThrow();
@@ -11680,7 +11749,7 @@ async function editInboundComment(trx, input) {
11680
11749
  block,
11681
11750
  commentId: updated.id,
11682
11751
  revision: updated.revision,
11683
- authorRole: authorRole(input.config, task, block.author),
11752
+ authorRole: authorRole(input.config, subject, block.author),
11684
11753
  actor: input.actor
11685
11754
  }, new Date(input.at));
11686
11755
  }
@@ -11702,7 +11771,7 @@ async function ticketTitle(trx, task) {
11702
11771
  /** Posts a curator's answer to the feed; an operator answer is already on the board. */
11703
11772
  async function queueCuratorAnswerLine(trx, task, block, answer, note, ctx, config) {
11704
11773
  if (parseAddressee(block.addressee_role).kind !== "curator") return;
11705
- if (authorRole(config, task, ctx.actor) === "operator") return;
11774
+ if (authorRole(config, taskSubject(task), ctx.actor) === "operator") return;
11706
11775
  const curator = parseActorId(ctx.actor);
11707
11776
  if (curator?.actor !== "agent") return;
11708
11777
  const asker = parseActorId(block.author);
@@ -11731,16 +11800,20 @@ async function queueCuratorAnswerLine(trx, task, block, answer, note, ctx, confi
11731
11800
  }
11732
11801
  /** Concrete block and comment mutations for the generic apply boundary. */
11733
11802
  function createBlockMutations(config) {
11803
+ /** Opens a question block on a subject with the text as its first comment. */
11804
+ async function askOn(trx, subject, text, ctx) {
11805
+ const block = await openBlock(trx, subject, "needs_input", text, ctx, config);
11806
+ await addComment(trx, subject, block, text, ctx, block.addressee_role, config);
11807
+ await queueBlockOpen(trx, subject, block, ctx, config);
11808
+ return { block: blockView(block, subject) };
11809
+ }
11734
11810
  const ask = {
11735
11811
  id: "block.ask",
11736
11812
  resolveTargets: (trx, input) => taskTarget(trx, input.task),
11737
11813
  async run(trx, input, ctx) {
11738
11814
  const task = found(await taskByKey$1(trx, input.task));
11739
11815
  active(task);
11740
- const block = await openBlock(trx, task, "needs_input", input.text, ctx, config);
11741
- await addComment(trx, task, block, input.text, ctx, block.addressee_role, config);
11742
- await queueBlockOpen(trx, task, block, ctx, config);
11743
- return { block: blockView(block, task) };
11816
+ return askOn(trx, taskSubject(task), input.text, ctx);
11744
11817
  }
11745
11818
  };
11746
11819
  const create = {
@@ -11749,9 +11822,10 @@ function createBlockMutations(config) {
11749
11822
  async run(trx, input, ctx) {
11750
11823
  const task = found(await taskByKey$1(trx, input.task));
11751
11824
  active(task);
11752
- const block = await openBlock(trx, task, input.type, input.reason, ctx, config);
11753
- await queueBlockOpen(trx, task, block, ctx, config);
11754
- return { block: blockView(block, task) };
11825
+ const subject = taskSubject(task);
11826
+ const block = await openBlock(trx, subject, input.type, input.reason, ctx, config);
11827
+ await queueBlockOpen(trx, subject, block, ctx, config);
11828
+ return { block: blockView(block, subject) };
11755
11829
  }
11756
11830
  };
11757
11831
  const escalate = {
@@ -11759,10 +11833,12 @@ function createBlockMutations(config) {
11759
11833
  async resolveTargets(trx, input, actor) {
11760
11834
  const block = await blockById(trx, input.block);
11761
11835
  if (block === void 0) return blockTarget(trx, input.block);
11762
- const task = await taskById$1(trx, block.task_id);
11763
- const target = await authorizationTarget(trx, task, block);
11836
+ const subject = await blockSubject(trx, block);
11837
+ const target = await subjectTarget(trx, subject, block);
11764
11838
  if (target.kind !== "found" || target.value.block === null) return [target];
11765
11839
  if (holdsBlock(config, actor, target)) return [target];
11840
+ const { task } = subject;
11841
+ if (task === null) return [target];
11766
11842
  const priors = priorAddressees(await routeBlock(trx, task, block, config), block.addressee_role, block.type, curatorProject(config, task.project) !== null);
11767
11843
  for (const addressee_role of priors) {
11768
11844
  const prior = {
@@ -11781,16 +11857,17 @@ function createBlockMutations(config) {
11781
11857
  },
11782
11858
  async run(trx, input, ctx) {
11783
11859
  const block = found(await blockById(trx, input.block));
11784
- const task = await taskById$1(trx, block.task_id);
11785
- active(task);
11786
- const hasCurator = curatorProject(config, task.project) !== null;
11860
+ const subject = await blockSubject(trx, block);
11861
+ const { task } = subject;
11862
+ if (task !== null) active(task);
11863
+ const hasCurator = curatorProject(config, subject.project) !== null;
11787
11864
  const next = rule(() => escalateBlock(current$1(block), hasCurator));
11788
11865
  const unchanged = async () => {
11789
- if (input.note !== void 0) await addComment(trx, task, block, input.note, ctx, block.addressee_role, config, { ringsAuthor: false });
11790
- return { block: blockView(block, task) };
11866
+ if (input.note !== void 0) await addComment(trx, subject, block, input.note, ctx, block.addressee_role, config, { ringsAuthor: false });
11867
+ return { block: blockView(block, subject) };
11791
11868
  };
11792
- if (!next.changed) return unchanged();
11793
- const target = await authorizationTarget(trx, task, block);
11869
+ if (!next.changed || task === null) return unchanged();
11870
+ const target = await subjectTarget(trx, subject, block);
11794
11871
  if (!holdsBlock(config, ctx.actor, target)) return unchanged();
11795
11872
  const updated = await moveBlockToAddressee(trx, {
11796
11873
  task,
@@ -11799,8 +11876,8 @@ function createBlockMutations(config) {
11799
11876
  ctx,
11800
11877
  config
11801
11878
  });
11802
- if (input.note !== void 0) await addComment(trx, task, updated, input.note, ctx, next.addressee, config, { ringsAuthor: false });
11803
- return { block: blockView(updated, task) };
11879
+ if (input.note !== void 0) await addComment(trx, subject, updated, input.note, ctx, next.addressee, config, { ringsAuthor: false });
11880
+ return { block: blockView(updated, subject) };
11804
11881
  }
11805
11882
  };
11806
11883
  const answer = {
@@ -11808,19 +11885,20 @@ function createBlockMutations(config) {
11808
11885
  resolveTargets: (trx, input) => blockTarget(trx, input.block),
11809
11886
  async run(trx, input, ctx) {
11810
11887
  const block = found(await blockById(trx, input.block));
11811
- const task = await taskById$1(trx, block.task_id);
11812
- active(task);
11888
+ const subject = await blockSubject(trx, block);
11889
+ const { task } = subject;
11890
+ if (task !== null) active(task);
11813
11891
  rule(() => {
11814
11892
  answerBlock(current$1(block));
11815
11893
  });
11816
- const role = authorRole(config, task, block.author);
11817
- const comment = await addComment(trx, task, block, input.text, ctx, role, config);
11818
- if (input.note !== void 0) await addComment(trx, task, block, input.note, ctx, role, config, { ringsAuthor: false });
11819
- const updated = await closeOpenBlock(trx, task, block, ctx, "done", null, comment);
11820
- await queueCuratorAnswerLine(trx, task, block, comment, input.note, ctx, config);
11894
+ const role = authorRole(config, subject, block.author);
11895
+ const comment = await addComment(trx, subject, block, input.text, ctx, role, config);
11896
+ if (input.note !== void 0) await addComment(trx, subject, block, input.note, ctx, role, config, { ringsAuthor: false });
11897
+ const updated = await closeOpenBlock(trx, subject, block, ctx, "done", null, comment);
11898
+ if (task !== null) await queueCuratorAnswerLine(trx, task, block, comment, input.note, ctx, config);
11821
11899
  return {
11822
- block: blockView(updated, task),
11823
- comment: commentView(comment, task)
11900
+ block: blockView(updated, subject),
11901
+ comment: commentView(comment, subject)
11824
11902
  };
11825
11903
  }
11826
11904
  };
@@ -11829,9 +11907,9 @@ function createBlockMutations(config) {
11829
11907
  resolveTargets: (trx, input) => blockTarget(trx, input.block),
11830
11908
  async run(trx, input, ctx) {
11831
11909
  const block = found(await blockById(trx, input.block));
11832
- const task = await taskById$1(trx, block.task_id);
11833
- active(task);
11834
- const target = await authorizationTarget(trx, task, block);
11910
+ const subject = await blockSubject(trx, block);
11911
+ if (subject.task !== null) active(subject.task);
11912
+ const target = await subjectTarget(trx, subject, block);
11835
11913
  const decision = rule(() => input.mode === "done" ? closeBlock(current$1(block), {
11836
11914
  mode: "done",
11837
11915
  ...input.ref === void 0 ? {} : { ref: input.ref }
@@ -11843,13 +11921,13 @@ function createBlockMutations(config) {
11843
11921
  isAuthor: wroteBlock(config, ctx.actor, target),
11844
11922
  reason: input.answer
11845
11923
  }));
11846
- if (input.mode === "withdraw") await addComment(trx, task, block, input.answer, ctx, block.addressee_role, config, { ringsAuthor: false });
11847
- const updated = await closeOpenBlock(trx, task, block, ctx, decision.outcome, decision.outcomeReason);
11924
+ if (input.mode === "withdraw") await addComment(trx, subject, block, input.answer, ctx, block.addressee_role, config, { ringsAuthor: false });
11925
+ const updated = await closeOpenBlock(trx, subject, block, ctx, decision.outcome, decision.outcomeReason);
11848
11926
  await applyDoorbellPolicy(trx, input.mode === "done" || holdsBlock(config, ctx.actor, target) ? { kind: "block_closed_by_addressee" } : {
11849
11927
  kind: "block_removed",
11850
11928
  block: updated
11851
11929
  }, new Date(ctx.at));
11852
- return { block: blockView(updated, task) };
11930
+ return { block: blockView(updated, subject) };
11853
11931
  }
11854
11932
  };
11855
11933
  const add = {
@@ -11860,11 +11938,12 @@ function createBlockMutations(config) {
11860
11938
  active(task);
11861
11939
  const block = input.block === void 0 ? void 0 : found(await blockById(trx, input.block));
11862
11940
  if (block !== void 0 && block.task_id !== task.id) throw new CommandError("not_found", "Block does not belong to this task.");
11941
+ const subject = taskSubject(task);
11863
11942
  let addressee = "worker";
11864
- if (block !== void 0) if (ctx.actor !== block.author) addressee = authorRole(config, task, block.author);
11865
- else if (block.outcome === "done" && block.unblocked_by !== null && block.unblocked_by !== block.author) addressee = authorRole(config, task, block.unblocked_by);
11943
+ if (block !== void 0) if (ctx.actor !== block.author) addressee = authorRole(config, subject, block.author);
11944
+ else if (block.outcome === "done" && block.unblocked_by !== null && block.unblocked_by !== block.author) addressee = authorRole(config, subject, block.unblocked_by);
11866
11945
  else addressee = block.addressee_role;
11867
- return { comment: commentView(await addComment(trx, task, block, input.text, ctx, addressee, config), task) };
11946
+ return { comment: commentView(await addComment(trx, subject, block, input.text, ctx, addressee, config), subject) };
11868
11947
  }
11869
11948
  };
11870
11949
  const edit = {
@@ -11881,12 +11960,12 @@ function createBlockMutations(config) {
11881
11960
  at: ctx.at,
11882
11961
  actor: ctx.actor,
11883
11962
  config
11884
- }), await taskById$1(trx, comment.task_id)) };
11963
+ }), await rowSubject(trx, comment)) };
11885
11964
  }
11886
11965
  };
11887
11966
  /** Marks a comment read at `revision` and acks its unread answer notifications. */
11888
11967
  async function ackComment(trx, comment, revision, ctx) {
11889
- const task = await taskById$1(trx, comment.task_id);
11968
+ const subject = await rowSubject(trx, comment);
11890
11969
  await acknowledgeNotification(trx, {
11891
11970
  commentId: comment.id,
11892
11971
  revision,
@@ -11895,7 +11974,7 @@ function createBlockMutations(config) {
11895
11974
  config
11896
11975
  });
11897
11976
  await trx.updateTable("comment").set({ acked_revision: revision }).where("id", "=", comment.id).execute();
11898
- await journal$1(trx, task, ctx, "comment_acked", {
11977
+ await journal$1(trx, subject, ctx, "comment_acked", {
11899
11978
  comment: commentId(comment.id),
11900
11979
  revision
11901
11980
  });
@@ -11974,7 +12053,7 @@ function createBlockMutations(config) {
11974
12053
  id
11975
12054
  };
11976
12055
  return {
11977
- ...await authorizationTarget(trx, await taskById$1(trx, notice.task_id)),
12056
+ ...await subjectTarget(trx, await rowSubject(trx, notice)),
11978
12057
  id
11979
12058
  };
11980
12059
  }
@@ -12007,6 +12086,11 @@ function createBlockMutations(config) {
12007
12086
  alreadyHandled
12008
12087
  } : { acked };
12009
12088
  }
12089
+ },
12090
+ projectAsk: {
12091
+ id: "project.ask",
12092
+ resolveTargets: (_trx, input) => Promise.resolve([config.projects.some((project) => project.id === input.project) ? projectTarget(input.project) : { kind: "missing" }]),
12093
+ run: (trx, input, ctx) => askOn(trx, projectSubject(input.project), input.text, ctx)
12010
12094
  }
12011
12095
  };
12012
12096
  }
@@ -12024,7 +12108,7 @@ async function archiveTask(trx, input) {
12024
12108
  at
12025
12109
  };
12026
12110
  const blocks = await trx.selectFrom("block").selectAll().where("task_id", "=", task.id).where("state", "=", "open").orderBy("id").execute();
12027
- for (const block of blocks) await closeOpenBlock(trx, task, block, ctx, "archived", null);
12111
+ for (const block of blocks) await closeOpenBlock(trx, taskSubject(task), block, ctx, "archived", null);
12028
12112
  await closeTurns(trx, {
12029
12113
  taskId: task.id,
12030
12114
  reason: "archived",
@@ -13277,6 +13361,12 @@ async function getStatus(deps, options = {}) {
13277
13361
  const rows = await store.db.selectFrom("task").selectAll().where("membership", "!=", "archived").execute();
13278
13362
  const blockRows = await store.db.selectFrom("block").select(["task_id", (eb) => eb.fn.countAll().as("open")]).where("state", "=", "open").groupBy("task_id").execute();
13279
13363
  const openBlocks = new Map(blockRows.map((row) => [row.task_id, row.open]));
13364
+ const projectBlockRows = await store.db.selectFrom("block").select(["project", (eb) => eb.fn.countAll().as("open")]).where("state", "=", "open").where("task_id", "is", null).groupBy("project").execute();
13365
+ const openProjectBlocks = new Map(projectBlockRows.map((row) => [row.project, row.open]));
13366
+ const projectBlocks = projects.map((project) => ({
13367
+ project: project.id,
13368
+ openBlocks: openProjectBlocks.get(project.id) ?? 0
13369
+ }));
13280
13370
  const tasks = [];
13281
13371
  for (const task of rows) {
13282
13372
  if (!projectIds.has(task.project)) continue;
@@ -13452,6 +13542,7 @@ async function getStatus(deps, options = {}) {
13452
13542
  transport,
13453
13543
  sources,
13454
13544
  tasks,
13545
+ projectBlocks,
13455
13546
  unlinkedPrs,
13456
13547
  unlinkedSessions,
13457
13548
  hiddenUnlinkedPrs: prRows.length - unlinkedPrs.length,
@@ -14214,14 +14305,20 @@ async function reconcileItems(trx, config, now, spurComplete, warnSkipped) {
14214
14305
  const blockedIds = new Set(blockedRoots.map((item) => item.block_id));
14215
14306
  const ledgerRoots = await trx.selectFrom("operator_item").select("block_id").where("kind", "in", BLOCK_ITEM_KINDS).where("cleared_at", "is", null).execute();
14216
14307
  const ledgerIds = new Set(ledgerRoots.map((item) => item.block_id));
14217
- for (const block of blocks) {
14308
+ const subjectOf = (block) => {
14309
+ if (block.task_id === null) return block.project === null ? void 0 : projectSubject(block.project);
14218
14310
  const task = taskById.get(block.task_id);
14219
- if (task === void 0 || ledgerIds.has(block.id)) continue;
14311
+ return task === void 0 ? void 0 : taskSubject(task);
14312
+ };
14313
+ for (const block of blocks) {
14314
+ const subject = subjectOf(block);
14315
+ if (subject === void 0 || ledgerIds.has(block.id)) continue;
14316
+ const taskId = subject.task?.id ?? null;
14220
14317
  if (block.addressee_role === "operator" && block.author !== "operator" && !blockedIds.has(block.id)) candidates.push({
14221
14318
  key: `operator-block:${String(block.id)}`,
14222
14319
  kind: "board_observation",
14223
- taskId: task.id,
14224
- summary: `${task.key}: ${block.reason}`,
14320
+ taskId,
14321
+ summary: `${subject.key}: ${block.reason}`,
14225
14322
  factAt: block.created_at,
14226
14323
  source: null,
14227
14324
  priority: 0
@@ -14229,8 +14326,8 @@ async function reconcileItems(trx, config, now, spurComplete, warnSkipped) {
14229
14326
  if (now - Date.parse(block.created_at) >= config.thresholds.blockOpenMs) candidates.push({
14230
14327
  key: `block:${String(block.id)}`,
14231
14328
  kind: "board_observation",
14232
- taskId: task.id,
14233
- summary: `${task.key}: block has been open for more than a day`,
14329
+ taskId,
14330
+ summary: `${subject.key}: block has been open for more than a day`,
14234
14331
  factAt: block.created_at,
14235
14332
  source: null,
14236
14333
  priority: 3
@@ -14471,7 +14568,7 @@ async function reconcileItems(trx, config, now, spurComplete, warnSkipped) {
14471
14568
  itemKey: candidate.key,
14472
14569
  kind: candidate.kind,
14473
14570
  text: candidate.summary,
14474
- url: task === null || task === void 0 ? "https://github.com" : taskUrl(task, config),
14571
+ url: task === null || task === void 0 ? TASKLESS_ITEM_URL : taskUrl(task, config),
14475
14572
  at: new Date(now),
14476
14573
  fingerprint: hash,
14477
14574
  factAt: candidate.factAt,
@@ -15202,17 +15299,6 @@ function submitSummary(kind, payloadText) {
15202
15299
  return null;
15203
15300
  }
15204
15301
  }
15205
- function projectTarget(project) {
15206
- return {
15207
- kind: "found",
15208
- value: {
15209
- project,
15210
- task: null,
15211
- block: null,
15212
- workers: []
15213
- }
15214
- };
15215
- }
15216
15302
  function visibleProjects(config, actor) {
15217
15303
  return new Set(config.projects.filter((project) => authorize([{ grant: "operator" }, { grant: "project-agent" }], actor, {
15218
15304
  config,
@@ -15320,6 +15406,7 @@ function createTaskQueries(deps) {
15320
15406
  trx.selectFrom("comment").selectAll().where("task_id", "=", task.id).orderBy("id").execute(),
15321
15407
  trx.selectFrom("link").leftJoin("pr_snapshot", (join) => join.on(sql`lower(pr_snapshot.repo) = lower(link.repo)`).onRef("pr_snapshot.number", "=", "link.number")).selectAll("link").select("pr_snapshot.state").where("link.task_id", "=", task.id).where("link.banned", "=", 0).orderBy("link.id").execute()
15322
15408
  ]);
15409
+ const subject = taskSubject(task);
15323
15410
  return {
15324
15411
  task: taskView(task),
15325
15412
  ...await taskTurns(trx, task, input.detail === true),
@@ -15330,8 +15417,8 @@ function createTaskQueries(deps) {
15330
15417
  ...observed === void 0 ? {} : { state: observed }
15331
15418
  };
15332
15419
  }),
15333
- blocks: blocks.map((block) => blockView(block, task)),
15334
- comments: comments.map((comment) => commentView(comment, task)),
15420
+ blocks: blocks.map((block) => blockView(block, subject)),
15421
+ comments: comments.map((comment) => commentView(comment, subject)),
15335
15422
  ...input.why === true ? { why: await why(trx, task) } : {}
15336
15423
  };
15337
15424
  }
@@ -15391,14 +15478,14 @@ function createTaskQueries(deps) {
15391
15478
  const listed = /* @__PURE__ */ new Set();
15392
15479
  for (const notice of notices) {
15393
15480
  if (notice.comment_id === null || seen.has(notice.comment_id)) continue;
15394
- const task = await trx.selectFrom("task").selectAll().where("id", "=", notice.task_id).executeTakeFirst();
15395
- if (task === void 0 || !projects.has(task.project)) continue;
15481
+ const subject = await rowSubject(trx, notice);
15482
+ if (!projects.has(subject.project)) continue;
15396
15483
  seen.add(notice.comment_id);
15397
15484
  if (!await notificationAddressedTo(trx, notice, actor, config)) continue;
15398
15485
  const comment = await trx.selectFrom("comment").selectAll().where("id", "=", notice.comment_id).executeTakeFirst();
15399
15486
  if (comment === void 0 || comment.acked_revision === comment.revision) continue;
15400
15487
  listed.add(comment.id);
15401
- comments.push(commentView(comment, task));
15488
+ comments.push(commentView(comment, subject));
15402
15489
  }
15403
15490
  const parsedActor = parseActorId(actor);
15404
15491
  if (parsedActor?.actor === "agent") {
@@ -15407,9 +15494,9 @@ function createTaskQueries(deps) {
15407
15494
  if (tasksById.size > 0) {
15408
15495
  const taskComments = await trx.selectFrom("comment").selectAll().where("task_id", "in", [...tasksById.keys()]).where("block_id", "is", null).orderBy("id").execute();
15409
15496
  for (const comment of taskComments) {
15410
- const task = tasksById.get(comment.task_id);
15497
+ const task = comment.task_id === null ? void 0 : tasksById.get(comment.task_id);
15411
15498
  if (task === void 0 || listed.has(comment.id) || comment.author === actor || comment.acked_revision === comment.revision) continue;
15412
- comments.push(commentView(comment, task));
15499
+ comments.push(commentView(comment, taskSubject(task)));
15413
15500
  }
15414
15501
  }
15415
15502
  }
@@ -15417,14 +15504,14 @@ function createTaskQueries(deps) {
15417
15504
  const rows = await trx.selectFrom("block").selectAll().where("state", "=", "open").orderBy("id").execute();
15418
15505
  const blocks = [];
15419
15506
  for (const block of rows) {
15420
- const task = await trx.selectFrom("task").selectAll().where("id", "=", block.task_id).executeTakeFirst();
15421
- if (task === void 0 || !projects.has(task.project)) continue;
15422
- const target = await authorizationTarget(trx, task, block);
15507
+ const subject = await blockSubject(trx, block);
15508
+ if (!projects.has(subject.project)) continue;
15509
+ const target = await subjectTarget(trx, subject, block);
15423
15510
  if (authorize([{ grant: "block-addressee" }], actor, {
15424
15511
  config,
15425
15512
  target
15426
15513
  }).status !== "allowed") continue;
15427
- blocks.push(blockView(block, task));
15514
+ blocks.push(blockView(block, subject));
15428
15515
  }
15429
15516
  return {
15430
15517
  comments,
@@ -15715,8 +15802,8 @@ function createTurnMutations(deps) {
15715
15802
  async run(trx, input, ctx) {
15716
15803
  const { turn, task } = await takenTurn(trx, input.turn, ctx);
15717
15804
  if (!RETURNABLE.includes(task.column)) throw new CommandError("return_not_allowed", `The task is in ${task.column}; only a task right of Dev returns to Dev.`);
15718
- const block = await openBlock(trx, task, "code", input.reason, ctx, config);
15719
- await queueBlockOpen(trx, task, block, ctx, config);
15805
+ const block = await openBlock(trx, taskSubject(task), "code", input.reason, ctx, config);
15806
+ await queueBlockOpen(trx, taskSubject(task), block, ctx, config);
15720
15807
  await moveLeft(trx, {
15721
15808
  task,
15722
15809
  to: "dev",
@@ -15729,7 +15816,7 @@ function createTurnMutations(deps) {
15729
15816
  const moved = await dispatched(trx, task.id, ctx.at);
15730
15817
  return {
15731
15818
  task: taskView(moved),
15732
- block: blockView(block, moved)
15819
+ block: blockView(block, taskSubject(moved))
15733
15820
  };
15734
15821
  }
15735
15822
  },
@@ -15859,6 +15946,7 @@ function createOperationRegistry(deps) {
15859
15946
  "comment.add": blocks.add,
15860
15947
  "comment.edit": blocks.edit,
15861
15948
  "inbox.ack": blocks.ack,
15949
+ "project.ask": blocks.projectAsk,
15862
15950
  "item.retry": items.retry,
15863
15951
  "item.close": items.close,
15864
15952
  "task.submit": turns.submit,
@@ -16958,7 +17046,7 @@ async function taskContext(trx, event, gateway) {
16958
17046
  taskId: null,
16959
17047
  project: null
16960
17048
  };
16961
- const row = await trx.selectFrom("conversation").leftJoin("task", "task.id", "conversation.task_id").select(["conversation.task_id as taskId", "task.project"]).where("conversation.gateway", "=", gateway).where("conversation.ref", "=", event.conversation).executeTakeFirst();
17049
+ const row = await trx.selectFrom("conversation").leftJoin("task", "task.id", "conversation.task_id").leftJoin("block", "block.id", "conversation.block_id").select((eb) => ["conversation.task_id as taskId", eb.fn.coalesce("task.project", "block.project").as("project")]).where("conversation.gateway", "=", gateway).where("conversation.ref", "=", event.conversation).executeTakeFirst();
16962
17050
  return {
16963
17051
  taskId: row?.taskId ?? null,
16964
17052
  project: row?.project ?? null
@@ -17029,7 +17117,7 @@ async function alreadyApplied(trx, event) {
17029
17117
  if (event.kind !== "message_edited" && event.kind !== "message_deleted") return false;
17030
17118
  const comment = await linkedComment(trx, event);
17031
17119
  if (comment === void 0 || comment.author !== "operator") return false;
17032
- const rows = await trx.selectFrom("event").select(["kind", "at"]).where("task_id", "=", comment.task_id).where("kind", "in", ["comment_deleted", "comment_edited"]).where(sql`json_extract(payload, '$.comment')`, "=", `C${String(comment.id)}`).execute();
17120
+ const rows = await trx.selectFrom("event").select(["kind", "at"]).where((eb) => comment.task_id === null ? eb("task_id", "is", null) : eb("task_id", "=", comment.task_id)).where("kind", "in", ["comment_deleted", "comment_edited"]).where(sql`json_extract(payload, '$.comment')`, "=", `C${String(comment.id)}`).execute();
17033
17121
  if (rows.some((row) => row.kind === "comment_deleted")) return true;
17034
17122
  if (event.kind === "message_deleted") return false;
17035
17123
  const editedAt = Date.parse(event.at);
@@ -17048,7 +17136,8 @@ async function ringAuthor(trx, input) {
17048
17136
  const parsed = parseActorId(input.block.author);
17049
17137
  if (parsed?.actor === "agent") {
17050
17138
  if (await selectNotificationRecipient(trx, {
17051
- task_id: input.task.id,
17139
+ task_id: input.subject.task?.id ?? null,
17140
+ block_id: input.block.id,
17052
17141
  role: input.authorRole,
17053
17142
  author_session: parsed.actorRef.session
17054
17143
  }, input.config) !== void 0) return;
@@ -17060,8 +17149,8 @@ async function ringAuthor(trx, input) {
17060
17149
  reason: parsed?.actor !== "agent" ? "author is not an agent" : "author session is unavailable"
17061
17150
  },
17062
17151
  actor: "system",
17063
- taskId: input.task.id,
17064
- project: input.task.project,
17152
+ taskId: input.subject.task?.id ?? null,
17153
+ project: input.subject.project,
17065
17154
  at: input.at
17066
17155
  });
17067
17156
  await raiseInboundItem(trx, {
@@ -17069,7 +17158,7 @@ async function ringAuthor(trx, input) {
17069
17158
  title: "Answer author unavailable",
17070
17159
  sourceUrl: input.event.sourceUrl,
17071
17160
  event: input.event,
17072
- taskId: input.task.id,
17161
+ taskId: input.subject.task?.id ?? null,
17073
17162
  at: input.at,
17074
17163
  retry: false
17075
17164
  });
@@ -17133,7 +17222,7 @@ function createInbound(input) {
17133
17222
  await ringAuthor(trx, {
17134
17223
  event,
17135
17224
  block: recorded.block,
17136
- task: recorded.task,
17225
+ subject: recorded.subject,
17137
17226
  comment: recorded.comment,
17138
17227
  authorRole: recorded.authorRole,
17139
17228
  at,
@@ -17159,7 +17248,7 @@ function createInbound(input) {
17159
17248
  });
17160
17249
  return { status: "applied" };
17161
17250
  }
17162
- const task = await trx.selectFrom("task").selectAll().where("id", "=", comment.task_id).executeTakeFirstOrThrow();
17251
+ const subject = await rowSubject(trx, comment);
17163
17252
  await journal(trx, {
17164
17253
  kind: "comment_deleted",
17165
17254
  payload: {
@@ -17167,8 +17256,8 @@ function createInbound(input) {
17167
17256
  normalizedEvent: event
17168
17257
  },
17169
17258
  actor: "operator",
17170
- taskId: task.id,
17171
- project: task.project,
17259
+ taskId: comment.task_id,
17260
+ project: subject.project,
17172
17261
  at
17173
17262
  });
17174
17263
  return { status: "applied" };